fbdev_dec.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. 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 ((fbdev->fd = avpriv_open(avctx->filename, flags)) == -1) {
  70. ret = AVERROR(errno);
  71. av_log(avctx, AV_LOG_ERROR,
  72. "Could not open framebuffer device '%s': %s\n",
  73. avctx->filename, av_err2str(ret));
  74. return ret;
  75. }
  76. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  77. ret = AVERROR(errno);
  78. av_log(avctx, AV_LOG_ERROR,
  79. "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
  80. goto fail;
  81. }
  82. if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
  83. ret = AVERROR(errno);
  84. av_log(avctx, AV_LOG_ERROR,
  85. "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
  86. goto fail;
  87. }
  88. pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  89. if (pix_fmt == AV_PIX_FMT_NONE) {
  90. ret = AVERROR(EINVAL);
  91. av_log(avctx, AV_LOG_ERROR,
  92. "Framebuffer pixel format not supported.\n");
  93. goto fail;
  94. }
  95. fbdev->width = fbdev->varinfo.xres;
  96. fbdev->height = fbdev->varinfo.yres;
  97. fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
  98. fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel;
  99. fbdev->frame_size = fbdev->frame_linesize * fbdev->height;
  100. fbdev->time_frame = AV_NOPTS_VALUE;
  101. fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
  102. if (fbdev->data == MAP_FAILED) {
  103. ret = AVERROR(errno);
  104. av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
  105. goto fail;
  106. }
  107. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  108. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  109. st->codec->width = fbdev->width;
  110. st->codec->height = fbdev->height;
  111. st->codec->pix_fmt = pix_fmt;
  112. st->codec->time_base = av_inv_q(fbdev->framerate_q);
  113. st->codec->bit_rate =
  114. fbdev->width * fbdev->height * fbdev->bytes_per_pixel * av_q2d(fbdev->framerate_q) * 8;
  115. av_log(avctx, AV_LOG_INFO,
  116. "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n",
  117. fbdev->width, fbdev->height, fbdev->varinfo.bits_per_pixel,
  118. av_get_pix_fmt_name(pix_fmt),
  119. fbdev->framerate_q.num, fbdev->framerate_q.den,
  120. st->codec->bit_rate);
  121. return 0;
  122. fail:
  123. close(fbdev->fd);
  124. return ret;
  125. }
  126. static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  127. {
  128. FBDevContext *fbdev = avctx->priv_data;
  129. int64_t curtime, delay;
  130. struct timespec ts;
  131. int i, ret;
  132. uint8_t *pin, *pout;
  133. if (fbdev->time_frame == AV_NOPTS_VALUE)
  134. fbdev->time_frame = av_gettime();
  135. /* wait based on the frame rate */
  136. while (1) {
  137. curtime = av_gettime();
  138. delay = fbdev->time_frame - curtime;
  139. av_dlog(avctx,
  140. "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
  141. fbdev->time_frame, curtime, delay);
  142. if (delay <= 0) {
  143. fbdev->time_frame += INT64_C(1000000) / av_q2d(fbdev->framerate_q);
  144. break;
  145. }
  146. if (avctx->flags & AVFMT_FLAG_NONBLOCK)
  147. return AVERROR(EAGAIN);
  148. ts.tv_sec = delay / 1000000;
  149. ts.tv_nsec = (delay % 1000000) * 1000;
  150. while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
  151. }
  152. if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
  153. return ret;
  154. /* refresh fbdev->varinfo, visible data position may change at each call */
  155. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
  156. av_log(avctx, AV_LOG_WARNING,
  157. "Error refreshing variable info: %s\n", av_err2str(ret));
  158. pkt->pts = curtime;
  159. /* compute visible data offset */
  160. pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
  161. fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
  162. pout = pkt->data;
  163. for (i = 0; i < fbdev->height; i++) {
  164. memcpy(pout, pin, fbdev->frame_linesize);
  165. pin += fbdev->fixinfo.line_length;
  166. pout += fbdev->frame_linesize;
  167. }
  168. return fbdev->frame_size;
  169. }
  170. static av_cold int fbdev_read_close(AVFormatContext *avctx)
  171. {
  172. FBDevContext *fbdev = avctx->priv_data;
  173. munmap(fbdev->data, fbdev->fixinfo.smem_len);
  174. close(fbdev->fd);
  175. return 0;
  176. }
  177. static int fbdev_get_device_list(AVFormatContext *s, AVDeviceInfoList *device_list)
  178. {
  179. return ff_fbdev_get_device_list(device_list);
  180. }
  181. #define OFFSET(x) offsetof(FBDevContext, x)
  182. #define DEC AV_OPT_FLAG_DECODING_PARAM
  183. static const AVOption options[] = {
  184. { "framerate","", OFFSET(framerate_q), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
  185. { NULL },
  186. };
  187. static const AVClass fbdev_class = {
  188. .class_name = "fbdev indev",
  189. .item_name = av_default_item_name,
  190. .option = options,
  191. .version = LIBAVUTIL_VERSION_INT,
  192. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  193. };
  194. AVInputFormat ff_fbdev_demuxer = {
  195. .name = "fbdev",
  196. .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
  197. .priv_data_size = sizeof(FBDevContext),
  198. .read_header = fbdev_read_header,
  199. .read_packet = fbdev_read_packet,
  200. .read_close = fbdev_read_close,
  201. .get_device_list = fbdev_get_device_list,
  202. .flags = AVFMT_NOFILE,
  203. .priv_class = &fbdev_class,
  204. };