fbdev_dec.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/internal.h"
  35. #include "libavutil/log.h"
  36. #include "libavutil/opt.h"
  37. #include "libavutil/time.h"
  38. #include "libavutil/parseutils.h"
  39. #include "libavutil/pixdesc.h"
  40. #include "libavformat/internal.h"
  41. #include "avdevice.h"
  42. #include "fbdev_common.h"
  43. typedef struct FBDevContext {
  44. AVClass *class; ///< class for private options
  45. int frame_size; ///< size in bytes of a grabbed frame
  46. AVRational framerate_q; ///< framerate
  47. int64_t time_frame; ///< time for the next frame to output (in 1/1000000 units)
  48. int fd; ///< framebuffer device file descriptor
  49. int width, height; ///< assumed frame resolution
  50. int frame_linesize; ///< linesize of the output frame, it is assumed to be constant
  51. int bytes_per_pixel;
  52. struct fb_var_screeninfo varinfo; ///< variable info;
  53. struct fb_fix_screeninfo fixinfo; ///< fixed info;
  54. uint8_t *data; ///< framebuffer data
  55. } FBDevContext;
  56. static av_cold int fbdev_read_header(AVFormatContext *avctx)
  57. {
  58. FBDevContext *fbdev = avctx->priv_data;
  59. AVStream *st = NULL;
  60. enum AVPixelFormat pix_fmt;
  61. int ret, flags = O_RDONLY;
  62. const char* device;
  63. if (!(st = avformat_new_stream(avctx, NULL)))
  64. return AVERROR(ENOMEM);
  65. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
  66. /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
  67. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  68. flags |= O_NONBLOCK;
  69. if (avctx->url[0])
  70. device = avctx->url;
  71. else
  72. device = ff_fbdev_default_device();
  73. if ((fbdev->fd = avpriv_open(device, flags)) == -1) {
  74. ret = AVERROR(errno);
  75. av_log(avctx, AV_LOG_ERROR,
  76. "Could not open framebuffer device '%s': %s\n",
  77. device, av_err2str(ret));
  78. return ret;
  79. }
  80. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  81. ret = AVERROR(errno);
  82. av_log(avctx, AV_LOG_ERROR,
  83. "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
  84. goto fail;
  85. }
  86. if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
  87. ret = AVERROR(errno);
  88. av_log(avctx, AV_LOG_ERROR,
  89. "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
  90. goto fail;
  91. }
  92. pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  93. if (pix_fmt == AV_PIX_FMT_NONE) {
  94. ret = AVERROR(EINVAL);
  95. av_log(avctx, AV_LOG_ERROR,
  96. "Framebuffer pixel format not supported.\n");
  97. goto fail;
  98. }
  99. fbdev->width = fbdev->varinfo.xres;
  100. fbdev->height = fbdev->varinfo.yres;
  101. fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
  102. fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel;
  103. fbdev->frame_size = fbdev->frame_linesize * fbdev->height;
  104. fbdev->time_frame = AV_NOPTS_VALUE;
  105. fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
  106. if (fbdev->data == MAP_FAILED) {
  107. ret = AVERROR(errno);
  108. av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
  109. goto fail;
  110. }
  111. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  112. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  113. st->codecpar->width = fbdev->width;
  114. st->codecpar->height = fbdev->height;
  115. st->codecpar->format = pix_fmt;
  116. st->avg_frame_rate = fbdev->framerate_q;
  117. st->codecpar->bit_rate =
  118. fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
  119. av_log(avctx, AV_LOG_INFO,
  120. "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%"PRId64"\n",
  121. fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel,
  122. av_get_pix_fmt_name(pix_fmt),
  123. fbdev->framerate_q.num, fbdev->framerate_q.den,
  124. st->codecpar->bit_rate);
  125. return 0;
  126. fail:
  127. close(fbdev->fd);
  128. return ret;
  129. }
  130. static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  131. {
  132. FBDevContext *fbdev = avctx->priv_data;
  133. int64_t curtime, delay;
  134. struct timespec ts;
  135. int i, ret;
  136. uint8_t *pin, *pout;
  137. if (fbdev->time_frame == AV_NOPTS_VALUE)
  138. fbdev->time_frame = av_gettime_relative();
  139. /* wait based on the frame rate */
  140. while (1) {
  141. curtime = av_gettime_relative();
  142. delay = fbdev->time_frame - curtime;
  143. av_log(avctx, AV_LOG_TRACE,
  144. "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
  145. fbdev->time_frame, curtime, delay);
  146. if (delay <= 0) {
  147. fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
  148. break;
  149. }
  150. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  151. return AVERROR(EAGAIN);
  152. ts.tv_sec = delay / 1000000;
  153. ts.tv_nsec = (delay % 1000000) * 1000;
  154. while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
  155. }
  156. if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
  157. return ret;
  158. /* refresh fbdev->varinfo, visible data position may change at each call */
  159. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  160. av_log(avctx, AV_LOG_WARNING,
  161. "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno)));
  162. }
  163. pkt->pts = av_gettime();
  164. /* compute visible data offset */
  165. pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
  166. fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
  167. pout = pkt->data;
  168. for (i = 0; i < fbdev->height; i++) {
  169. memcpy(pout, pin, fbdev->frame_linesize);
  170. pin += fbdev->fixinfo.line_length;
  171. pout += fbdev->frame_linesize;
  172. }
  173. return fbdev->frame_size;
  174. }
  175. static av_cold int fbdev_read_close(AVFormatContext *avctx)
  176. {
  177. FBDevContext *fbdev = avctx->priv_data;
  178. munmap(fbdev->data, fbdev->fixinfo.smem_len);
  179. close(fbdev->fd);
  180. return 0;
  181. }
  182. static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list)
  183. {
  184. return ff_fbdev_get_device_list(device_list);
  185. }
  186. #define OFFSET(x) offsetof(FBDevContext, x)
  187. #define DEC AV_OPT_FLAG_DECODING_PARAM
  188. static const AVOption options[] = {
  189. { "framerate","", OFFSET(framerate_q), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
  190. { NULL },
  191. };
  192. static const AVClass fbdev_class = {
  193. .class_name = "fbdev indev",
  194. .item_name = av_default_item_name,
  195. .option = options,
  196. .version = LIBAVUTIL_VERSION_INT,
  197. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  198. };
  199. const AVInputFormat ff_fbdev_demuxer = {
  200. .name = "fbdev",
  201. .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
  202. .priv_data_size = sizeof(FBDevContext),
  203. .read_header = fbdev_read_header,
  204. .read_packet = fbdev_read_packet,
  205. .read_close = fbdev_read_close,
  206. .get_device_list = fbdev_get_device_list,
  207. .flags = AVFMT_NOFILE,
  208. .priv_class = &fbdev_class,
  209. };