fbdev.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 also 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/mem.h"
  37. #include "libavutil/pixdesc.h"
  38. #include "avdevice.h"
  39. struct rgb_pixfmt_map_entry {
  40. int bits_per_pixel;
  41. int red_offset, green_offset, blue_offset, alpha_offset;
  42. enum PixelFormat pixfmt;
  43. };
  44. static struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = {
  45. // bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt
  46. { 32, 0, 8, 16, 24, PIX_FMT_RGBA },
  47. { 32, 16, 8, 0, 24, PIX_FMT_BGRA },
  48. { 32, 8, 16, 24, 0, PIX_FMT_ARGB },
  49. { 32, 3, 2, 8, 0, PIX_FMT_ABGR },
  50. { 24, 0, 8, 16, 0, PIX_FMT_RGB24 },
  51. { 24, 16, 8, 0, 0, PIX_FMT_BGR24 },
  52. };
  53. static enum PixelFormat get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
  54. {
  55. int i;
  56. for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) {
  57. struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i];
  58. if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
  59. entry->red_offset == varinfo->red.offset &&
  60. entry->green_offset == varinfo->green.offset &&
  61. entry->blue_offset == varinfo->blue.offset)
  62. return entry->pixfmt;
  63. }
  64. return PIX_FMT_NONE;
  65. }
  66. typedef struct {
  67. int frame_size; ///< size in bytes of a grabbed frame
  68. AVRational time_base; ///< time base
  69. int64_t time_frame; ///< time for the next frame to output (in 1/1000000 units)
  70. int fd; ///< framebuffer device file descriptor
  71. int width, heigth; ///< assumed frame resolution
  72. int frame_linesize; ///< linesize of the output frame, it is assumed to be constant
  73. int bytes_per_pixel;
  74. struct fb_var_screeninfo varinfo; ///< variable info;
  75. struct fb_fix_screeninfo fixinfo; ///< fixed info;
  76. uint8_t *data; ///< framebuffer data
  77. } FBDevContext;
  78. av_cold static int fbdev_read_header(AVFormatContext *avctx,
  79. AVFormatParameters *ap)
  80. {
  81. FBDevContext *fbdev = avctx->priv_data;
  82. AVStream *st = NULL;
  83. enum PixelFormat pix_fmt;
  84. int ret, flags = O_RDONLY;
  85. if (!(st = av_new_stream(avctx, 0)))
  86. return AVERROR(ENOMEM);
  87. av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
  88. if (ap->time_base.den <= 0) {
  89. av_log(avctx, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  90. ap->time_base.num, ap->time_base.den);
  91. return AVERROR(EINVAL);
  92. }
  93. /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
  94. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  95. flags |= O_NONBLOCK;
  96. if ((fbdev->fd = open(avctx->filename, flags)) == -1) {
  97. ret = AVERROR(errno);
  98. av_log(avctx, AV_LOG_ERROR,
  99. "Could not open framebuffer device '%s': %s\n",
  100. avctx->filename, strerror(ret));
  101. return ret;
  102. }
  103. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  104. ret = AVERROR(errno);
  105. av_log(avctx, AV_LOG_ERROR,
  106. "FBIOGET_VSCREENINFO: %s\n", strerror(errno));
  107. goto fail;
  108. }
  109. if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
  110. ret = AVERROR(errno);
  111. av_log(avctx, AV_LOG_ERROR,
  112. "FBIOGET_FSCREENINFO: %s\n", strerror(errno));
  113. goto fail;
  114. }
  115. pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  116. if (pix_fmt == PIX_FMT_NONE) {
  117. ret = AVERROR(EINVAL);
  118. av_log(avctx, AV_LOG_ERROR,
  119. "Framebuffer pixel format not supported.\n");
  120. goto fail;
  121. }
  122. fbdev->width = fbdev->varinfo.xres;
  123. fbdev->heigth = fbdev->varinfo.yres;
  124. fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
  125. fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel;
  126. fbdev->frame_size = fbdev->frame_linesize * fbdev->heigth;
  127. fbdev->time_base = ap->time_base;
  128. fbdev->time_frame = AV_NOPTS_VALUE;
  129. fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
  130. if (fbdev->data == MAP_FAILED) {
  131. ret = AVERROR(errno);
  132. av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", strerror(errno));
  133. goto fail;
  134. }
  135. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  136. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  137. st->codec->width = fbdev->width;
  138. st->codec->height = fbdev->heigth;
  139. st->codec->pix_fmt = pix_fmt;
  140. st->codec->time_base = ap->time_base;
  141. st->codec->bit_rate =
  142. fbdev->width * fbdev->heigth * fbdev->bytes_per_pixel / av_q2d(ap->time_base) * 8;
  143. av_log(avctx, AV_LOG_INFO,
  144. "w:%d h:%d bpp:%d pixfmt:%s tb:%d/%d bit_rate:%d\n",
  145. fbdev->width, fbdev->heigth, fbdev->varinfo.bits_per_pixel,
  146. av_pix_fmt_descriptors[pix_fmt].name,
  147. ap->time_base.num, ap->time_base.den,
  148. st->codec->bit_rate);
  149. return 0;
  150. fail:
  151. close(fbdev->fd);
  152. return ret;
  153. }
  154. static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  155. {
  156. FBDevContext *fbdev = avctx->priv_data;
  157. int64_t curtime, delay;
  158. struct timespec ts;
  159. int i, ret;
  160. uint8_t *pin, *pout;
  161. if (fbdev->time_frame == AV_NOPTS_VALUE)
  162. fbdev->time_frame = av_gettime();
  163. /* wait based on the frame rate */
  164. while (1) {
  165. curtime = av_gettime();
  166. delay = fbdev->time_frame - curtime;
  167. av_dlog(avctx,
  168. "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
  169. fbdev->time_frame, curtime, delay);
  170. if (delay <= 0) {
  171. fbdev->time_frame += INT64_C(1000000) * av_q2d(fbdev->time_base);
  172. break;
  173. }
  174. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  175. return AVERROR(EAGAIN);
  176. ts.tv_sec = delay / 1000000;
  177. ts.tv_nsec = (delay % 1000000) * 1000;
  178. while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
  179. }
  180. if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
  181. return ret;
  182. /* refresh fbdev->varinfo, visible data position may change at each call */
  183. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
  184. av_log(avctx, AV_LOG_WARNING,
  185. "Error refreshing variable info: %s\n", strerror(errno));
  186. pkt->pts = curtime;
  187. /* compute visible data offset */
  188. pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
  189. fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
  190. pout = pkt->data;
  191. for (i = 0; i < fbdev->heigth; i++) {
  192. memcpy(pout, pin, fbdev->frame_linesize);
  193. pin += fbdev->fixinfo.line_length;
  194. pout += fbdev->frame_linesize;
  195. }
  196. return fbdev->frame_size;
  197. }
  198. av_cold static int fbdev_read_close(AVFormatContext *avctx)
  199. {
  200. FBDevContext *fbdev = avctx->priv_data;
  201. munmap(fbdev->data, fbdev->frame_size);
  202. close(fbdev->fd);
  203. return 0;
  204. }
  205. AVInputFormat ff_fbdev_demuxer = {
  206. .name = "fbdev",
  207. .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
  208. .priv_data_size = sizeof(FBDevContext),
  209. .read_header = fbdev_read_header,
  210. .read_packet = fbdev_read_packet,
  211. .read_close = fbdev_read_close,
  212. .flags = AVFMT_NOFILE,
  213. };