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.

277 lines
9.5KB

  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. #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/internal.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 AVPixelFormat 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, AV_PIX_FMT_RGBA },
  51. { 32, 16, 8, 0, 24, AV_PIX_FMT_BGRA },
  52. { 32, 8, 16, 24, 0, AV_PIX_FMT_ARGB },
  53. { 32, 3, 2, 8, 0, AV_PIX_FMT_ABGR },
  54. { 24, 0, 8, 16, 0, AV_PIX_FMT_RGB24 },
  55. { 24, 16, 8, 0, 0, AV_PIX_FMT_BGR24 },
  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. 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 FBDevContext {
  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 AVPixelFormat pix_fmt;
  89. int ret, flags = O_RDONLY;
  90. char errbuf[128];
  91. ret = av_parse_video_rate(&fbdev->framerate_q, fbdev->framerate);
  92. if (ret < 0) {
  93. av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", fbdev->framerate);
  94. return ret;
  95. }
  96. if (!(st = avformat_new_stream(avctx, NULL)))
  97. return AVERROR(ENOMEM);
  98. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
  99. /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
  100. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  101. flags |= O_NONBLOCK;
  102. if ((fbdev->fd = avpriv_open(avctx->filename, flags)) == -1) {
  103. ret = AVERROR(errno);
  104. av_strerror(ret, errbuf, sizeof(errbuf));
  105. av_log(avctx, AV_LOG_ERROR,
  106. "Could not open framebuffer device '%s': %s\n",
  107. avctx->filename, errbuf);
  108. return ret;
  109. }
  110. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  111. ret = AVERROR(errno);
  112. av_strerror(ret, errbuf, sizeof(errbuf));
  113. av_log(avctx, AV_LOG_ERROR,
  114. "FBIOGET_VSCREENINFO: %s\n", errbuf);
  115. goto fail;
  116. }
  117. if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
  118. ret = AVERROR(errno);
  119. av_strerror(ret, errbuf, sizeof(errbuf));
  120. av_log(avctx, AV_LOG_ERROR,
  121. "FBIOGET_FSCREENINFO: %s\n", errbuf);
  122. goto fail;
  123. }
  124. pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  125. if (pix_fmt == AV_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->height = 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->height;
  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_strerror(ret, errbuf, sizeof(errbuf));
  141. av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", errbuf);
  142. goto fail;
  143. }
  144. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  145. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  146. st->codec->width = fbdev->width;
  147. st->codec->height = fbdev->height;
  148. st->codec->pix_fmt = pix_fmt;
  149. st->codec->time_base = (AVRational){fbdev->framerate_q.den, fbdev->framerate_q.num};
  150. st->codec->bit_rate =
  151. fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
  152. av_log(avctx, AV_LOG_INFO,
  153. "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n",
  154. fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel,
  155. av_get_pix_fmt_name(pix_fmt),
  156. fbdev->framerate_q.num, fbdev->framerate_q.den,
  157. st->codec->bit_rate);
  158. return 0;
  159. fail:
  160. close(fbdev->fd);
  161. return ret;
  162. }
  163. static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  164. {
  165. FBDevContext *fbdev = avctx->priv_data;
  166. int64_t curtime, delay;
  167. struct timespec ts;
  168. int i, ret;
  169. uint8_t *pin, *pout;
  170. if (fbdev->time_frame == AV_NOPTS_VALUE)
  171. fbdev->time_frame = av_gettime();
  172. /* wait based on the frame rate */
  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. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  180. return AVERROR(EAGAIN);
  181. ts.tv_sec = delay / 1000000;
  182. ts.tv_nsec = (delay % 1000000) * 1000;
  183. while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
  184. }
  185. /* compute the time of the next frame */
  186. fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
  187. if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
  188. return ret;
  189. /* refresh fbdev->varinfo, visible data position may change at each call */
  190. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  191. char errbuf[128];
  192. av_strerror(AVERROR(errno), errbuf, sizeof(errbuf));
  193. av_log(avctx, AV_LOG_WARNING,
  194. "Error refreshing variable info: %s\n", errbuf);
  195. }
  196. pkt->pts = curtime;
  197. /* compute visible data offset */
  198. pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
  199. fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
  200. pout = pkt->data;
  201. // TODO it'd be nice if the lines were aligned
  202. for (i = 0; i < fbdev->height; i++) {
  203. memcpy(pout, pin, fbdev->frame_linesize);
  204. pin += fbdev->fixinfo.line_length;
  205. pout += fbdev->frame_linesize;
  206. }
  207. return fbdev->frame_size;
  208. }
  209. static av_cold int fbdev_read_close(AVFormatContext *avctx)
  210. {
  211. FBDevContext *fbdev = avctx->priv_data;
  212. munmap(fbdev->data, fbdev->frame_size);
  213. close(fbdev->fd);
  214. return 0;
  215. }
  216. #define OFFSET(x) offsetof(FBDevContext, x)
  217. #define DEC AV_OPT_FLAG_DECODING_PARAM
  218. static const AVOption options[] = {
  219. { "framerate","", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  220. { NULL },
  221. };
  222. static const AVClass fbdev_class = {
  223. .class_name = "fbdev indev",
  224. .item_name = av_default_item_name,
  225. .option = options,
  226. .version = LIBAVUTIL_VERSION_INT,
  227. };
  228. AVInputFormat ff_fbdev_demuxer = {
  229. .name = "fbdev",
  230. .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
  231. .priv_data_size = sizeof(FBDevContext),
  232. .read_header = fbdev_read_header,
  233. .read_packet = fbdev_read_packet,
  234. .read_close = fbdev_read_close,
  235. .flags = AVFMT_NOFILE,
  236. .priv_class = &fbdev_class,
  237. };