fbdev_enc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/file_open.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavutil/log.h"
  28. #include "libavutil/opt.h"
  29. #include "libavformat/avformat.h"
  30. #include "libavformat/mux.h"
  31. #include "fbdev_common.h"
  32. #include "avdevice.h"
  33. typedef struct {
  34. AVClass *class; ///< class for private options
  35. int xoffset; ///< x coordinate of top left corner
  36. int yoffset; ///< y coordinate of top left corner
  37. struct fb_var_screeninfo varinfo; ///< framebuffer variable info
  38. struct fb_fix_screeninfo fixinfo; ///< framebuffer fixed info
  39. int fd; ///< framebuffer device file descriptor
  40. uint8_t *data; ///< framebuffer data
  41. } FBDevContext;
  42. static av_cold int fbdev_write_header(AVFormatContext *h)
  43. {
  44. FBDevContext *fbdev = h->priv_data;
  45. enum AVPixelFormat pix_fmt;
  46. int ret, flags = O_RDWR;
  47. const char* device;
  48. if (h->nb_streams != 1 || h->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
  49. av_log(fbdev, AV_LOG_ERROR, "Only a single video stream is supported.\n");
  50. return AVERROR(EINVAL);
  51. }
  52. if (h->url[0])
  53. device = h->url;
  54. else
  55. device = ff_fbdev_default_device();
  56. if ((fbdev->fd = avpriv_open(device, flags)) == -1) {
  57. ret = AVERROR(errno);
  58. av_log(h, AV_LOG_ERROR,
  59. "Could not open framebuffer device '%s': %s\n",
  60. device, av_err2str(ret));
  61. return ret;
  62. }
  63. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  64. ret = AVERROR(errno);
  65. av_log(h, AV_LOG_ERROR, "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
  66. goto fail;
  67. }
  68. if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
  69. ret = AVERROR(errno);
  70. av_log(h, AV_LOG_ERROR, "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
  71. goto fail;
  72. }
  73. pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  74. if (pix_fmt == AV_PIX_FMT_NONE) {
  75. ret = AVERROR(EINVAL);
  76. av_log(h, AV_LOG_ERROR, "Framebuffer pixel format not supported.\n");
  77. goto fail;
  78. }
  79. fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_WRITE, MAP_SHARED, fbdev->fd, 0);
  80. if (fbdev->data == MAP_FAILED) {
  81. ret = AVERROR(errno);
  82. av_log(h, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
  83. goto fail;
  84. }
  85. return 0;
  86. fail:
  87. close(fbdev->fd);
  88. return ret;
  89. }
  90. static int fbdev_write_packet(AVFormatContext *h, AVPacket *pkt)
  91. {
  92. FBDevContext *fbdev = h->priv_data;
  93. const uint8_t *pin;
  94. uint8_t *pout;
  95. enum AVPixelFormat fb_pix_fmt;
  96. int disp_height;
  97. int bytes_to_copy;
  98. AVCodecParameters *par = h->streams[0]->codecpar;
  99. enum AVPixelFormat video_pix_fmt = par->format;
  100. int video_width = par->width;
  101. int video_height = par->height;
  102. int bytes_per_pixel = ((par->bits_per_coded_sample + 7) >> 3);
  103. int src_line_size = video_width * bytes_per_pixel;
  104. int i;
  105. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
  106. av_log(h, AV_LOG_WARNING,
  107. "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno)));
  108. fb_pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  109. if (fb_pix_fmt != video_pix_fmt) {
  110. av_log(h, AV_LOG_ERROR, "Pixel format %s is not supported, use %s\n",
  111. av_get_pix_fmt_name(video_pix_fmt), av_get_pix_fmt_name(fb_pix_fmt));
  112. return AVERROR(EINVAL);
  113. }
  114. disp_height = FFMIN(fbdev->varinfo.yres, video_height);
  115. bytes_to_copy = FFMIN(fbdev->varinfo.xres, video_width) * bytes_per_pixel;
  116. pin = pkt->data;
  117. pout = fbdev->data +
  118. bytes_per_pixel * fbdev->varinfo.xoffset +
  119. fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
  120. if (fbdev->xoffset) {
  121. if (fbdev->xoffset < 0) {
  122. if (-fbdev->xoffset >= video_width) //nothing to display
  123. return 0;
  124. bytes_to_copy += fbdev->xoffset * bytes_per_pixel;
  125. pin -= fbdev->xoffset * bytes_per_pixel;
  126. } else {
  127. int diff = (video_width + fbdev->xoffset) - fbdev->varinfo.xres;
  128. if (diff > 0) {
  129. if (diff >= video_width) //nothing to display
  130. return 0;
  131. bytes_to_copy -= diff * bytes_per_pixel;
  132. }
  133. pout += bytes_per_pixel * fbdev->xoffset;
  134. }
  135. }
  136. if (fbdev->yoffset) {
  137. if (fbdev->yoffset < 0) {
  138. if (-fbdev->yoffset >= video_height) //nothing to display
  139. return 0;
  140. disp_height += fbdev->yoffset;
  141. pin -= fbdev->yoffset * src_line_size;
  142. } else {
  143. int diff = (video_height + fbdev->yoffset) - fbdev->varinfo.yres;
  144. if (diff > 0) {
  145. if (diff >= video_height) //nothing to display
  146. return 0;
  147. disp_height -= diff;
  148. }
  149. pout += fbdev->yoffset * fbdev->fixinfo.line_length;
  150. }
  151. }
  152. for (i = 0; i < disp_height; i++) {
  153. memcpy(pout, pin, bytes_to_copy);
  154. pout += fbdev->fixinfo.line_length;
  155. pin += src_line_size;
  156. }
  157. return 0;
  158. }
  159. static av_cold int fbdev_write_trailer(AVFormatContext *h)
  160. {
  161. FBDevContext *fbdev = h->priv_data;
  162. munmap(fbdev->data, fbdev->fixinfo.smem_len);
  163. close(fbdev->fd);
  164. return 0;
  165. }
  166. static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list)
  167. {
  168. return ff_fbdev_get_device_list(device_list);
  169. }
  170. #define OFFSET(x) offsetof(FBDevContext, x)
  171. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  172. static const AVOption options[] = {
  173. { "xoffset", "set x coordinate of top left corner", OFFSET(xoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
  174. { "yoffset", "set y coordinate of top left corner", OFFSET(yoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
  175. { NULL }
  176. };
  177. static const AVClass fbdev_class = {
  178. .class_name = "fbdev outdev",
  179. .item_name = av_default_item_name,
  180. .option = options,
  181. .version = LIBAVUTIL_VERSION_INT,
  182. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
  183. };
  184. const FFOutputFormat ff_fbdev_muxer = {
  185. .p.name = "fbdev",
  186. .p.long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
  187. .priv_data_size = sizeof(FBDevContext),
  188. .p.audio_codec = AV_CODEC_ID_NONE,
  189. .p.video_codec = AV_CODEC_ID_RAWVIDEO,
  190. .write_header = fbdev_write_header,
  191. .write_packet = fbdev_write_packet,
  192. .write_trailer = fbdev_write_trailer,
  193. .get_device_list = fbdev_get_device_list,
  194. .p.flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  195. .p.priv_class = &fbdev_class,
  196. };