You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
8.4KB

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