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.

278 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. { 16, 11, 5, 0, 0, AV_PIX_FMT_RGB565 },
  57. };
  58. static enum AVPixelFormat get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
  59. {
  60. int i;
  61. for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) {
  62. struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i];
  63. if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
  64. entry->red_offset == varinfo->red.offset &&
  65. entry->green_offset == varinfo->green.offset &&
  66. entry->blue_offset == varinfo->blue.offset)
  67. return entry->pixfmt;
  68. }
  69. return AV_PIX_FMT_NONE;
  70. }
  71. typedef struct FBDevContext {
  72. AVClass *class; ///< class for private options
  73. int frame_size; ///< size in bytes of a grabbed frame
  74. AVRational framerate_q; ///< framerate
  75. char *framerate; ///< framerate string set by a private option
  76. int64_t time_frame; ///< time for the next frame to output (in 1/1000000 units)
  77. int fd; ///< framebuffer device file descriptor
  78. int width, height; ///< assumed frame resolution
  79. int frame_linesize; ///< linesize of the output frame, it is assumed to be constant
  80. int bytes_per_pixel;
  81. struct fb_var_screeninfo varinfo; ///< variable info;
  82. struct fb_fix_screeninfo fixinfo; ///< fixed info;
  83. uint8_t *data; ///< framebuffer data
  84. } FBDevContext;
  85. static av_cold int fbdev_read_header(AVFormatContext *avctx)
  86. {
  87. FBDevContext *fbdev = avctx->priv_data;
  88. AVStream *st = NULL;
  89. enum AVPixelFormat pix_fmt;
  90. int ret, flags = O_RDONLY;
  91. char errbuf[128];
  92. ret = av_parse_video_rate(&fbdev->framerate_q, fbdev->framerate);
  93. if (ret < 0) {
  94. av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", fbdev->framerate);
  95. return ret;
  96. }
  97. if (!(st = avformat_new_stream(avctx, NULL)))
  98. return AVERROR(ENOMEM);
  99. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
  100. /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
  101. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  102. flags |= O_NONBLOCK;
  103. if ((fbdev->fd = avpriv_open(avctx->filename, flags)) == -1) {
  104. ret = AVERROR(errno);
  105. av_strerror(ret, errbuf, sizeof(errbuf));
  106. av_log(avctx, AV_LOG_ERROR,
  107. "Could not open framebuffer device '%s': %s\n",
  108. avctx->filename, errbuf);
  109. return ret;
  110. }
  111. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  112. ret = AVERROR(errno);
  113. av_strerror(ret, errbuf, sizeof(errbuf));
  114. av_log(avctx, AV_LOG_ERROR,
  115. "FBIOGET_VSCREENINFO: %s\n", errbuf);
  116. goto fail;
  117. }
  118. if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
  119. ret = AVERROR(errno);
  120. av_strerror(ret, errbuf, sizeof(errbuf));
  121. av_log(avctx, AV_LOG_ERROR,
  122. "FBIOGET_FSCREENINFO: %s\n", errbuf);
  123. goto fail;
  124. }
  125. pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  126. if (pix_fmt == AV_PIX_FMT_NONE) {
  127. ret = AVERROR(EINVAL);
  128. av_log(avctx, AV_LOG_ERROR,
  129. "Framebuffer pixel format not supported.\n");
  130. goto fail;
  131. }
  132. fbdev->width = fbdev->varinfo.xres;
  133. fbdev->height = fbdev->varinfo.yres;
  134. fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
  135. fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel;
  136. fbdev->frame_size = fbdev->frame_linesize * fbdev->height;
  137. fbdev->time_frame = AV_NOPTS_VALUE;
  138. fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
  139. if (fbdev->data == MAP_FAILED) {
  140. ret = AVERROR(errno);
  141. av_strerror(ret, errbuf, sizeof(errbuf));
  142. av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", errbuf);
  143. goto fail;
  144. }
  145. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  146. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  147. st->codecpar->width = fbdev->width;
  148. st->codecpar->height = fbdev->height;
  149. st->codecpar->format = pix_fmt;
  150. st->codecpar->bit_rate =
  151. fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
  152. st->avg_frame_rate = fbdev->framerate_q;
  153. av_log(avctx, AV_LOG_INFO,
  154. "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n",
  155. fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel,
  156. av_get_pix_fmt_name(pix_fmt),
  157. fbdev->framerate_q.num, fbdev->framerate_q.den,
  158. st->codecpar->bit_rate);
  159. return 0;
  160. fail:
  161. close(fbdev->fd);
  162. return ret;
  163. }
  164. static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  165. {
  166. FBDevContext *fbdev = avctx->priv_data;
  167. int64_t curtime, delay;
  168. struct timespec ts;
  169. int i, ret;
  170. uint8_t *pin, *pout;
  171. if (fbdev->time_frame == AV_NOPTS_VALUE)
  172. fbdev->time_frame = av_gettime();
  173. /* wait based on the frame rate */
  174. curtime = av_gettime();
  175. delay = fbdev->time_frame - curtime;
  176. av_log(avctx, AV_LOG_TRACE,
  177. "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
  178. fbdev->time_frame, curtime, delay);
  179. if (delay > 0) {
  180. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  181. return AVERROR(EAGAIN);
  182. ts.tv_sec = delay / 1000000;
  183. ts.tv_nsec = (delay % 1000000) * 1000;
  184. while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
  185. }
  186. /* compute the time of the next frame */
  187. fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
  188. if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
  189. return ret;
  190. /* refresh fbdev->varinfo, visible data position may change at each call */
  191. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  192. char errbuf[128];
  193. av_strerror(AVERROR(errno), errbuf, sizeof(errbuf));
  194. av_log(avctx, AV_LOG_WARNING,
  195. "Error refreshing variable info: %s\n", errbuf);
  196. }
  197. pkt->pts = curtime;
  198. /* compute visible data offset */
  199. pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
  200. fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
  201. pout = pkt->data;
  202. // TODO it'd be nice if the lines were aligned
  203. for (i = 0; i < fbdev->height; i++) {
  204. memcpy(pout, pin, fbdev->frame_linesize);
  205. pin += fbdev->fixinfo.line_length;
  206. pout += fbdev->frame_linesize;
  207. }
  208. return fbdev->frame_size;
  209. }
  210. static av_cold int fbdev_read_close(AVFormatContext *avctx)
  211. {
  212. FBDevContext *fbdev = avctx->priv_data;
  213. munmap(fbdev->data, fbdev->frame_size);
  214. close(fbdev->fd);
  215. return 0;
  216. }
  217. #define OFFSET(x) offsetof(FBDevContext, x)
  218. #define DEC AV_OPT_FLAG_DECODING_PARAM
  219. static const AVOption options[] = {
  220. { "framerate","", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  221. { NULL },
  222. };
  223. static const AVClass fbdev_class = {
  224. .class_name = "fbdev indev",
  225. .item_name = av_default_item_name,
  226. .option = options,
  227. .version = LIBAVUTIL_VERSION_INT,
  228. };
  229. AVInputFormat ff_fbdev_demuxer = {
  230. .name = "fbdev",
  231. .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
  232. .priv_data_size = sizeof(FBDevContext),
  233. .read_header = fbdev_read_header,
  234. .read_packet = fbdev_read_packet,
  235. .read_close = fbdev_read_close,
  236. .flags = AVFMT_NOFILE,
  237. .priv_class = &fbdev_class,
  238. };