fbdev_enc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2013 Lukasz Marek
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/mman.h>
  24. #include <linux/fb.h>
  25. #include "libavutil/pixdesc.h"
  26. #include "libavutil/log.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/opt.h"
  29. #include "libavformat/avformat.h"
  30. #include "fbdev_common.h"
  31. #include "avdevice.h"
  32. typedef struct {
  33. AVClass *class; ///< class for private options
  34. int xoffset; ///< x coordinate of top left corner
  35. int yoffset; ///< y coordinate of top left corner
  36. struct fb_var_screeninfo varinfo; ///< framebuffer variable info
  37. struct fb_fix_screeninfo fixinfo; ///< framebuffer fixed info
  38. int fd; ///< framebuffer device file descriptor
  39. uint8_t *data; ///< framebuffer data
  40. } FBDevContext;
  41. static av_cold int fbdev_write_header(AVFormatContext *h)
  42. {
  43. FBDevContext *fbdev = h->priv_data;
  44. enum AVPixelFormat pix_fmt;
  45. int ret, flags = O_RDWR;
  46. const char* device;
  47. if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO) {
  48. av_log(fbdev, AV_LOG_ERROR, "Only a single video stream is supported.\n");
  49. return AVERROR(EINVAL);
  50. }
  51. if (h->filename[0])
  52. device = h->filename;
  53. else
  54. device = ff_fbdev_default_device();
  55. if ((fbdev->fd = avpriv_open(device, flags)) == -1) {
  56. ret = AVERROR(errno);
  57. av_log(h, AV_LOG_ERROR,
  58. "Could not open framebuffer device '%s': %s\n",
  59. device, av_err2str(ret));
  60. return ret;
  61. }
  62. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  63. ret = AVERROR(errno);
  64. av_log(h, AV_LOG_ERROR, "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
  65. goto fail;
  66. }
  67. if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
  68. ret = AVERROR(errno);
  69. av_log(h, AV_LOG_ERROR, "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
  70. goto fail;
  71. }
  72. pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  73. if (pix_fmt == AV_PIX_FMT_NONE) {
  74. ret = AVERROR(EINVAL);
  75. av_log(h, AV_LOG_ERROR, "Framebuffer pixel format not supported.\n");
  76. goto fail;
  77. }
  78. fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_WRITE, MAP_SHARED, fbdev->fd, 0);
  79. if (fbdev->data == MAP_FAILED) {
  80. ret = AVERROR(errno);
  81. av_log(h, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
  82. goto fail;
  83. }
  84. return 0;
  85. fail:
  86. close(fbdev->fd);
  87. return ret;
  88. }
  89. static int fbdev_write_packet(AVFormatContext *h, AVPacket *pkt)
  90. {
  91. FBDevContext *fbdev = h->priv_data;
  92. uint8_t *pin, *pout;
  93. enum AVPixelFormat fb_pix_fmt;
  94. int disp_height;
  95. int bytes_to_copy;
  96. AVCodecContext *codec_ctx = h->streams[0]->codec;
  97. enum AVPixelFormat video_pix_fmt = codec_ctx->pix_fmt;
  98. int video_width = codec_ctx->width;
  99. int video_height = codec_ctx->height;
  100. int bytes_per_pixel = ((codec_ctx->bits_per_coded_sample + 7) >> 3);
  101. int src_line_size = video_width * bytes_per_pixel;
  102. int i;
  103. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
  104. av_log(h, AV_LOG_WARNING,
  105. "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno)));
  106. fb_pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  107. if (fb_pix_fmt != video_pix_fmt) {
  108. av_log(h, AV_LOG_ERROR, "Pixel format %s is not supported, use %s\n",
  109. av_get_pix_fmt_name(video_pix_fmt), av_get_pix_fmt_name(fb_pix_fmt));
  110. return AVERROR(EINVAL);
  111. }
  112. disp_height = FFMIN(fbdev->varinfo.yres, video_height);
  113. bytes_to_copy = FFMIN(fbdev->varinfo.xres, video_width) * bytes_per_pixel;
  114. pin = pkt->data;
  115. pout = fbdev->data +
  116. bytes_per_pixel * fbdev->varinfo.xoffset +
  117. fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
  118. if (fbdev->xoffset) {
  119. if (fbdev->xoffset < 0) {
  120. if (-fbdev->xoffset >= video_width) //nothing to display
  121. return 0;
  122. bytes_to_copy += fbdev->xoffset * bytes_per_pixel;
  123. pin -= fbdev->xoffset * bytes_per_pixel;
  124. } else {
  125. int diff = (video_width + fbdev->xoffset) - fbdev->varinfo.xres;
  126. if (diff > 0) {
  127. if (diff >= video_width) //nothing to display
  128. return 0;
  129. bytes_to_copy -= diff * bytes_per_pixel;
  130. }
  131. pout += bytes_per_pixel * fbdev->xoffset;
  132. }
  133. }
  134. if (fbdev->yoffset) {
  135. if (fbdev->yoffset < 0) {
  136. if (-fbdev->yoffset >= video_height) //nothing to display
  137. return 0;
  138. disp_height += fbdev->yoffset;
  139. pin -= fbdev->yoffset * src_line_size;
  140. } else {
  141. int diff = (video_height + fbdev->yoffset) - fbdev->varinfo.yres;
  142. if (diff > 0) {
  143. if (diff >= video_height) //nothing to display
  144. return 0;
  145. disp_height -= diff;
  146. }
  147. pout += fbdev->yoffset * fbdev->fixinfo.line_length;
  148. }
  149. }
  150. for (i = 0; i < disp_height; i++) {
  151. memcpy(pout, pin, bytes_to_copy);
  152. pout += fbdev->fixinfo.line_length;
  153. pin += src_line_size;
  154. }
  155. return 0;
  156. }
  157. static av_cold int fbdev_write_trailer(AVFormatContext *h)
  158. {
  159. FBDevContext *fbdev = h->priv_data;
  160. munmap(fbdev->data, fbdev->fixinfo.smem_len);
  161. close(fbdev->fd);
  162. return 0;
  163. }
  164. static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list)
  165. {
  166. return ff_fbdev_get_device_list(device_list);
  167. }
  168. #define OFFSET(x) offsetof(FBDevContext, x)
  169. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  170. static const AVOption options[] = {
  171. { "xoffset", "set x coordinate of top left corner", OFFSET(xoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
  172. { "yoffset", "set y coordinate of top left corner", OFFSET(yoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
  173. { NULL }
  174. };
  175. static const AVClass fbdev_class = {
  176. .class_name = "fbdev outdev",
  177. .item_name = av_default_item_name,
  178. .option = options,
  179. .version = LIBAVUTIL_VERSION_INT,
  180. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
  181. };
  182. AVOutputFormat ff_fbdev_muxer = {
  183. .name = "fbdev",
  184. .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
  185. .priv_data_size = sizeof(FBDevContext),
  186. .audio_codec = AV_CODEC_ID_NONE,
  187. .video_codec = AV_CODEC_ID_RAWVIDEO,
  188. .write_header = fbdev_write_header,
  189. .write_packet = fbdev_write_packet,
  190. .write_trailer = fbdev_write_trailer,
  191. .get_device_list = fbdev_get_device_list,
  192. .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  193. .priv_class = &fbdev_class,
  194. };