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.

263 lines
8.9KB

  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. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/mman.h>
  32. #include <time.h>
  33. #include <linux/fb.h>
  34. #include "libavutil/log.h"
  35. #include "libavutil/mem.h"
  36. #include "libavutil/opt.h"
  37. #include "libavutil/time.h"
  38. #include "libavutil/parseutils.h"
  39. #include "libavutil/pixdesc.h"
  40. #include "avdevice.h"
  41. #include "libavformat/internal.h"
  42. struct rgb_pixfmt_map_entry {
  43. int bits_per_pixel;
  44. int red_offset, green_offset, blue_offset, alpha_offset;
  45. enum AVPixelFormat pixfmt;
  46. };
  47. static const 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, AV_PIX_FMT_RGBA },
  50. { 32, 16, 8, 0, 24, AV_PIX_FMT_BGRA },
  51. { 32, 8, 16, 24, 0, AV_PIX_FMT_ARGB },
  52. { 32, 3, 2, 8, 0, AV_PIX_FMT_ABGR },
  53. { 24, 0, 8, 16, 0, AV_PIX_FMT_RGB24 },
  54. { 24, 16, 8, 0, 0, AV_PIX_FMT_BGR24 },
  55. { 16, 11, 5, 0, 16, AV_PIX_FMT_RGB565 },
  56. };
  57. static enum AVPixelFormat get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
  58. {
  59. int i;
  60. for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) {
  61. const struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i];
  62. if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
  63. entry->red_offset == varinfo->red.offset &&
  64. entry->green_offset == varinfo->green.offset &&
  65. entry->blue_offset == varinfo->blue.offset)
  66. return entry->pixfmt;
  67. }
  68. return AV_PIX_FMT_NONE;
  69. }
  70. typedef struct {
  71. AVClass *class; ///< class for private options
  72. int frame_size; ///< size in bytes of a grabbed frame
  73. AVRational framerate_q; ///< framerate
  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, height; ///< 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. static av_cold int fbdev_read_header(AVFormatContext *avctx)
  84. {
  85. FBDevContext *fbdev = avctx->priv_data;
  86. AVStream *st = NULL;
  87. enum AVPixelFormat pix_fmt;
  88. int ret, flags = O_RDONLY;
  89. if (!(st = avformat_new_stream(avctx, NULL)))
  90. return AVERROR(ENOMEM);
  91. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
  92. /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
  93. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  94. flags |= O_NONBLOCK;
  95. if ((fbdev->fd = open(avctx->filename, flags)) == -1) {
  96. ret = AVERROR(errno);
  97. av_log(avctx, AV_LOG_ERROR,
  98. "Could not open framebuffer device '%s': %s\n",
  99. avctx->filename, strerror(ret));
  100. return ret;
  101. }
  102. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  103. ret = AVERROR(errno);
  104. av_log(avctx, AV_LOG_ERROR,
  105. "FBIOGET_VSCREENINFO: %s\n", strerror(errno));
  106. goto fail;
  107. }
  108. if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
  109. ret = AVERROR(errno);
  110. av_log(avctx, AV_LOG_ERROR,
  111. "FBIOGET_FSCREENINFO: %s\n", strerror(errno));
  112. goto fail;
  113. }
  114. pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  115. if (pix_fmt == AV_PIX_FMT_NONE) {
  116. ret = AVERROR(EINVAL);
  117. av_log(avctx, AV_LOG_ERROR,
  118. "Framebuffer pixel format not supported.\n");
  119. goto fail;
  120. }
  121. fbdev->width = fbdev->varinfo.xres;
  122. fbdev->height = fbdev->varinfo.yres;
  123. fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
  124. fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel;
  125. fbdev->frame_size = fbdev->frame_linesize * fbdev->height;
  126. fbdev->time_frame = AV_NOPTS_VALUE;
  127. fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
  128. if (fbdev->data == MAP_FAILED) {
  129. ret = AVERROR(errno);
  130. av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", strerror(errno));
  131. goto fail;
  132. }
  133. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  134. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  135. st->codec->width = fbdev->width;
  136. st->codec->height = fbdev->height;
  137. st->codec->pix_fmt = pix_fmt;
  138. st->codec->time_base = av_inv_q(fbdev->framerate_q);
  139. st->codec->bit_rate =
  140. fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
  141. av_log(avctx, AV_LOG_INFO,
  142. "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n",
  143. fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel,
  144. av_get_pix_fmt_name(pix_fmt),
  145. fbdev->framerate_q.num, fbdev->framerate_q.den,
  146. st->codec->bit_rate);
  147. return 0;
  148. fail:
  149. close(fbdev->fd);
  150. return ret;
  151. }
  152. static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  153. {
  154. FBDevContext *fbdev = avctx->priv_data;
  155. int64_t curtime, delay;
  156. struct timespec ts;
  157. int i, ret;
  158. uint8_t *pin, *pout;
  159. if (fbdev->time_frame == AV_NOPTS_VALUE)
  160. fbdev->time_frame = av_gettime();
  161. /* wait based on the frame rate */
  162. while (1) {
  163. curtime = av_gettime();
  164. delay = fbdev->time_frame - curtime;
  165. av_dlog(avctx,
  166. "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
  167. fbdev->time_frame, curtime, delay);
  168. if (delay <= 0) {
  169. fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
  170. break;
  171. }
  172. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  173. return AVERROR(EAGAIN);
  174. ts.tv_sec = delay / 1000000;
  175. ts.tv_nsec = (delay % 1000000) * 1000;
  176. while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
  177. }
  178. if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
  179. return ret;
  180. /* refresh fbdev->varinfo, visible data position may change at each call */
  181. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
  182. av_log(avctx, AV_LOG_WARNING,
  183. "Error refreshing variable info: %s\n", strerror(errno));
  184. pkt->pts = curtime;
  185. /* compute visible data offset */
  186. pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
  187. fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
  188. pout = pkt->data;
  189. for (i = 0; i < fbdev->height; i++) {
  190. memcpy(pout, pin, fbdev->frame_linesize);
  191. pin += fbdev->fixinfo.line_length;
  192. pout += fbdev->frame_linesize;
  193. }
  194. return fbdev->frame_size;
  195. }
  196. static av_cold int fbdev_read_close(AVFormatContext *avctx)
  197. {
  198. FBDevContext *fbdev = avctx->priv_data;
  199. munmap(fbdev->data, fbdev->frame_size);
  200. close(fbdev->fd);
  201. return 0;
  202. }
  203. #define OFFSET(x) offsetof(FBDevContext, x)
  204. #define DEC AV_OPT_FLAG_DECODING_PARAM
  205. static const AVOption options[] = {
  206. { "framerate","", OFFSET(framerate_q), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
  207. { NULL },
  208. };
  209. static const AVClass fbdev_class = {
  210. .class_name = "fbdev indev",
  211. .item_name = av_default_item_name,
  212. .option = options,
  213. .version = LIBAVUTIL_VERSION_INT,
  214. };
  215. AVInputFormat ff_fbdev_demuxer = {
  216. .name = "fbdev",
  217. .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
  218. .priv_data_size = sizeof(FBDevContext),
  219. .read_header = fbdev_read_header,
  220. .read_packet = fbdev_read_packet,
  221. .read_close = fbdev_read_close,
  222. .flags = AVFMT_NOFILE,
  223. .priv_class = &fbdev_class,
  224. };