fbdev_dec.c 8.1 KB

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