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.

270 lines
9.1KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/mman.h>
  33. #include <time.h>
  34. #include <linux/fb.h>
  35. #include "libavutil/log.h"
  36. #include "libavutil/mem.h"
  37. #include "libavutil/opt.h"
  38. #include "libavutil/time.h"
  39. #include "libavutil/parseutils.h"
  40. #include "libavutil/pixdesc.h"
  41. #include "libavformat/avformat.h"
  42. #include "libavformat/internal.h"
  43. struct rgb_pixfmt_map_entry {
  44. int bits_per_pixel;
  45. int red_offset, green_offset, blue_offset, alpha_offset;
  46. enum PixelFormat pixfmt;
  47. };
  48. static struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = {
  49. // bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt
  50. { 32, 0, 8, 16, 24, PIX_FMT_RGBA },
  51. { 32, 16, 8, 0, 24, PIX_FMT_BGRA },
  52. { 32, 8, 16, 24, 0, PIX_FMT_ARGB },
  53. { 32, 3, 2, 8, 0, PIX_FMT_ABGR },
  54. { 24, 0, 8, 16, 0, PIX_FMT_RGB24 },
  55. { 24, 16, 8, 0, 0, PIX_FMT_BGR24 },
  56. };
  57. static enum PixelFormat 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. 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 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. char *framerate; ///< framerate string set by a private option
  75. int64_t time_frame; ///< time for the next frame to output (in 1/1000000 units)
  76. int fd; ///< framebuffer device file descriptor
  77. int width, height; ///< assumed frame resolution
  78. int frame_linesize; ///< linesize of the output frame, it is assumed to be constant
  79. int bytes_per_pixel;
  80. struct fb_var_screeninfo varinfo; ///< variable info;
  81. struct fb_fix_screeninfo fixinfo; ///< fixed info;
  82. uint8_t *data; ///< framebuffer data
  83. } FBDevContext;
  84. static av_cold int fbdev_read_header(AVFormatContext *avctx)
  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 (!(st = avformat_new_stream(avctx, NULL)))
  96. return AVERROR(ENOMEM);
  97. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
  98. /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
  99. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  100. flags |= O_NONBLOCK;
  101. if ((fbdev->fd = open(avctx->filename, flags)) == -1) {
  102. ret = AVERROR(errno);
  103. av_log(avctx, AV_LOG_ERROR,
  104. "Could not open framebuffer device '%s': %s\n",
  105. avctx->filename, strerror(ret));
  106. return ret;
  107. }
  108. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  109. ret = AVERROR(errno);
  110. av_log(avctx, AV_LOG_ERROR,
  111. "FBIOGET_VSCREENINFO: %s\n", strerror(errno));
  112. goto fail;
  113. }
  114. if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
  115. ret = AVERROR(errno);
  116. av_log(avctx, AV_LOG_ERROR,
  117. "FBIOGET_FSCREENINFO: %s\n", strerror(errno));
  118. goto fail;
  119. }
  120. pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  121. if (pix_fmt == PIX_FMT_NONE) {
  122. ret = AVERROR(EINVAL);
  123. av_log(avctx, AV_LOG_ERROR,
  124. "Framebuffer pixel format not supported.\n");
  125. goto fail;
  126. }
  127. fbdev->width = fbdev->varinfo.xres;
  128. fbdev->height = fbdev->varinfo.yres;
  129. fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
  130. fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel;
  131. fbdev->frame_size = fbdev->frame_linesize * fbdev->height;
  132. fbdev->time_frame = AV_NOPTS_VALUE;
  133. fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
  134. if (fbdev->data == MAP_FAILED) {
  135. ret = AVERROR(errno);
  136. av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", strerror(errno));
  137. goto fail;
  138. }
  139. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  140. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  141. st->codec->width = fbdev->width;
  142. st->codec->height = fbdev->height;
  143. st->codec->pix_fmt = pix_fmt;
  144. st->codec->time_base = (AVRational){fbdev->framerate_q.den, fbdev->framerate_q.num};
  145. st->codec->bit_rate =
  146. fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
  147. av_log(avctx, AV_LOG_INFO,
  148. "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n",
  149. fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel,
  150. av_pix_fmt_descriptors[pix_fmt].name,
  151. fbdev->framerate_q.num, fbdev->framerate_q.den,
  152. st->codec->bit_rate);
  153. return 0;
  154. fail:
  155. close(fbdev->fd);
  156. return ret;
  157. }
  158. static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  159. {
  160. FBDevContext *fbdev = avctx->priv_data;
  161. int64_t curtime, delay;
  162. struct timespec ts;
  163. int i, ret;
  164. uint8_t *pin, *pout;
  165. if (fbdev->time_frame == AV_NOPTS_VALUE)
  166. fbdev->time_frame = av_gettime();
  167. /* wait based on the frame rate */
  168. curtime = av_gettime();
  169. delay = fbdev->time_frame - curtime;
  170. av_dlog(avctx,
  171. "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
  172. fbdev->time_frame, curtime, delay);
  173. if (delay > 0) {
  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. /* compute the time of the next frame */
  181. fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
  182. if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
  183. return ret;
  184. /* refresh fbdev->varinfo, visible data position may change at each call */
  185. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
  186. av_log(avctx, AV_LOG_WARNING,
  187. "Error refreshing variable info: %s\n", strerror(errno));
  188. pkt->pts = curtime;
  189. /* compute visible data offset */
  190. pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
  191. fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
  192. pout = pkt->data;
  193. // TODO it'd be nice if the lines were aligned
  194. for (i = 0; i < fbdev->height; i++) {
  195. memcpy(pout, pin, fbdev->frame_linesize);
  196. pin += fbdev->fixinfo.line_length;
  197. pout += fbdev->frame_linesize;
  198. }
  199. return fbdev->frame_size;
  200. }
  201. static av_cold int fbdev_read_close(AVFormatContext *avctx)
  202. {
  203. FBDevContext *fbdev = avctx->priv_data;
  204. munmap(fbdev->data, fbdev->frame_size);
  205. close(fbdev->fd);
  206. return 0;
  207. }
  208. #define OFFSET(x) offsetof(FBDevContext, x)
  209. #define DEC AV_OPT_FLAG_DECODING_PARAM
  210. static const AVOption options[] = {
  211. { "framerate","", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  212. { NULL },
  213. };
  214. static const AVClass fbdev_class = {
  215. .class_name = "fbdev indev",
  216. .item_name = av_default_item_name,
  217. .option = options,
  218. .version = LIBAVUTIL_VERSION_INT,
  219. };
  220. AVInputFormat ff_fbdev_demuxer = {
  221. .name = "fbdev",
  222. .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
  223. .priv_data_size = sizeof(FBDevContext),
  224. .read_header = fbdev_read_header,
  225. .read_packet = fbdev_read_packet,
  226. .read_close = fbdev_read_close,
  227. .flags = AVFMT_NOFILE,
  228. .priv_class = &fbdev_class,
  229. };