fbdev.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. * Copyright (c) 2009 Giliard B. de Freitas <giliarde@gmail.com>
  4. * Copyright (C) 2002 Gunnar Monell <gmo@linux.nu>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Linux framebuffer input device,
  25. * inspired by code from fbgrab.c by Gunnar Monell.
  26. * @see http://linux-fbdev.sourceforge.net/
  27. */
  28. /* #define DEBUG */
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/time.h>
  33. #include <sys/mman.h>
  34. #include <time.h>
  35. #include <linux/fb.h>
  36. #include "libavutil/log.h"
  37. #include "libavutil/mem.h"
  38. #include "libavutil/opt.h"
  39. #include "libavutil/parseutils.h"
  40. #include "libavutil/pixdesc.h"
  41. #include "avdevice.h"
  42. struct rgb_pixfmt_map_entry {
  43. int bits_per_pixel;
  44. int red_offset, green_offset, blue_offset, alpha_offset;
  45. enum PixelFormat pixfmt;
  46. };
  47. static struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = {
  48. // bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt
  49. { 32, 0, 8, 16, 24, PIX_FMT_RGBA },
  50. { 32, 16, 8, 0, 24, PIX_FMT_BGRA },
  51. { 32, 8, 16, 24, 0, PIX_FMT_ARGB },
  52. { 32, 3, 2, 8, 0, PIX_FMT_ABGR },
  53. { 24, 0, 8, 16, 0, PIX_FMT_RGB24 },
  54. { 24, 16, 8, 0, 0, PIX_FMT_BGR24 },
  55. };
  56. static enum PixelFormat get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
  57. {
  58. int i;
  59. for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) {
  60. struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i];
  61. if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
  62. entry->red_offset == varinfo->red.offset &&
  63. entry->green_offset == varinfo->green.offset &&
  64. entry->blue_offset == varinfo->blue.offset)
  65. return entry->pixfmt;
  66. }
  67. return PIX_FMT_NONE;
  68. }
  69. typedef struct {
  70. AVClass *class; ///< class for private options
  71. int frame_size; ///< size in bytes of a grabbed frame
  72. AVRational framerate_q; ///< framerate
  73. char *framerate; ///< framerate string set by a private option
  74. int64_t time_frame; ///< time for the next frame to output (in 1/1000000 units)
  75. int fd; ///< framebuffer device file descriptor
  76. int width, heigth; ///< assumed frame resolution
  77. int frame_linesize; ///< linesize of the output frame, it is assumed to be constant
  78. int bytes_per_pixel;
  79. struct fb_var_screeninfo varinfo; ///< variable info;
  80. struct fb_fix_screeninfo fixinfo; ///< fixed info;
  81. uint8_t *data; ///< framebuffer data
  82. } FBDevContext;
  83. av_cold static int fbdev_read_header(AVFormatContext *avctx,
  84. AVFormatParameters *ap)
  85. {
  86. FBDevContext *fbdev = avctx->priv_data;
  87. AVStream *st = NULL;
  88. enum PixelFormat pix_fmt;
  89. int ret, flags = O_RDONLY;
  90. ret = av_parse_video_rate(&fbdev->framerate_q, fbdev->framerate);
  91. if (ret < 0) {
  92. av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", fbdev->framerate);
  93. return ret;
  94. }
  95. #if FF_API_FORMAT_PARAMETERS
  96. if (ap->time_base.num)
  97. fbdev->framerate_q = (AVRational){ap->time_base.den, ap->time_base.num};
  98. #endif
  99. if (!(st = av_new_stream(avctx, 0)))
  100. return AVERROR(ENOMEM);
  101. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
  102. /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
  103. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  104. flags |= O_NONBLOCK;
  105. if ((fbdev->fd = open(avctx->filename, flags)) == -1) {
  106. ret = AVERROR(errno);
  107. av_log(avctx, AV_LOG_ERROR,
  108. "Could not open framebuffer device '%s': %s\n",
  109. avctx->filename, strerror(ret));
  110. return ret;
  111. }
  112. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  113. ret = AVERROR(errno);
  114. av_log(avctx, AV_LOG_ERROR,
  115. "FBIOGET_VSCREENINFO: %s\n", strerror(errno));
  116. goto fail;
  117. }
  118. if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
  119. ret = AVERROR(errno);
  120. av_log(avctx, AV_LOG_ERROR,
  121. "FBIOGET_FSCREENINFO: %s\n", strerror(errno));
  122. goto fail;
  123. }
  124. pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  125. if (pix_fmt == PIX_FMT_NONE) {
  126. ret = AVERROR(EINVAL);
  127. av_log(avctx, AV_LOG_ERROR,
  128. "Framebuffer pixel format not supported.\n");
  129. goto fail;
  130. }
  131. fbdev->width = fbdev->varinfo.xres;
  132. fbdev->heigth = fbdev->varinfo.yres;
  133. fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
  134. fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel;
  135. fbdev->frame_size = fbdev->frame_linesize * fbdev->heigth;
  136. fbdev->time_frame = AV_NOPTS_VALUE;
  137. fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
  138. if (fbdev->data == MAP_FAILED) {
  139. ret = AVERROR(errno);
  140. av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", strerror(errno));
  141. goto fail;
  142. }
  143. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  144. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  145. st->codec->width = fbdev->width;
  146. st->codec->height = fbdev->heigth;
  147. st->codec->pix_fmt = pix_fmt;
  148. st->codec->time_base = (AVRational){fbdev->framerate_q.den, fbdev->framerate_q.num};
  149. st->codec->bit_rate =
  150. fbdev->width * fbdev->heigth * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
  151. av_log(avctx, AV_LOG_INFO,
  152. "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n",
  153. fbdev->width, fbdev->heigth, fbdev->varinfo.bits_per_pixel,
  154. av_pix_fmt_descriptors[pix_fmt].name,
  155. fbdev->framerate_q.num, fbdev->framerate_q.den,
  156. st->codec->bit_rate);
  157. return 0;
  158. fail:
  159. close(fbdev->fd);
  160. return ret;
  161. }
  162. static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  163. {
  164. FBDevContext *fbdev = avctx->priv_data;
  165. int64_t curtime, delay;
  166. struct timespec ts;
  167. int i, ret;
  168. uint8_t *pin, *pout;
  169. if (fbdev->time_frame == AV_NOPTS_VALUE)
  170. fbdev->time_frame = av_gettime();
  171. /* wait based on the frame rate */
  172. while (1) {
  173. curtime = av_gettime();
  174. delay = fbdev->time_frame - curtime;
  175. av_dlog(avctx,
  176. "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
  177. fbdev->time_frame, curtime, delay);
  178. if (delay <= 0) {
  179. fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
  180. break;
  181. }
  182. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  183. return AVERROR(EAGAIN);
  184. ts.tv_sec = delay / 1000000;
  185. ts.tv_nsec = (delay % 1000000) * 1000;
  186. while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
  187. }
  188. if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
  189. return ret;
  190. /* refresh fbdev->varinfo, visible data position may change at each call */
  191. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
  192. av_log(avctx, AV_LOG_WARNING,
  193. "Error refreshing variable info: %s\n", strerror(errno));
  194. pkt->pts = curtime;
  195. /* compute visible data offset */
  196. pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
  197. fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
  198. pout = pkt->data;
  199. for (i = 0; i < fbdev->heigth; i++) {
  200. memcpy(pout, pin, fbdev->frame_linesize);
  201. pin += fbdev->fixinfo.line_length;
  202. pout += fbdev->frame_linesize;
  203. }
  204. return fbdev->frame_size;
  205. }
  206. av_cold static int fbdev_read_close(AVFormatContext *avctx)
  207. {
  208. FBDevContext *fbdev = avctx->priv_data;
  209. munmap(fbdev->data, fbdev->frame_size);
  210. close(fbdev->fd);
  211. return 0;
  212. }
  213. #define OFFSET(x) offsetof(FBDevContext, x)
  214. #define DEC AV_OPT_FLAG_DECODING_PARAM
  215. static const AVOption options[] = {
  216. { "framerate","", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  217. { NULL },
  218. };
  219. static const AVClass fbdev_class = {
  220. .class_name = "fbdev indev",
  221. .item_name = av_default_item_name,
  222. .option = options,
  223. .version = LIBAVUTIL_VERSION_INT,
  224. };
  225. AVInputFormat ff_fbdev_demuxer = {
  226. .name = "fbdev",
  227. .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
  228. .priv_data_size = sizeof(FBDevContext),
  229. .read_header = fbdev_read_header,
  230. .read_packet = fbdev_read_packet,
  231. .read_close = fbdev_read_close,
  232. .flags = AVFMT_NOFILE,
  233. .priv_class = &fbdev_class,
  234. };