fbdev_dec.c 8.0 KB

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