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>
6 * This file is part of FFmpeg.
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.
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.
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
25 * Linux framebuffer input device,
26 * inspired by code from fbgrab.c by Gunnar Monell.
27 * See also http://linux-fbdev.sourceforge.net/.
34 #include <sys/ioctl.h>
40 #include "libavutil/mem.h"
41 #include "libavutil/pixdesc.h"
42 #include "libavformat/avformat.h"
44 struct rgb_pixfmt_map_entry {
46 int red_offset, green_offset, blue_offset, alpha_offset;
47 enum PixelFormat pixfmt;
50 static struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = {
51 // bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt
52 { 32, 0, 8, 16, 24, PIX_FMT_RGBA },
53 { 32, 16, 8, 0, 24, PIX_FMT_BGRA },
54 { 32, 8, 16, 24, 0, PIX_FMT_ARGB },
55 { 32, 3, 2, 8, 0, PIX_FMT_ABGR },
56 { 24, 0, 8, 16, 0, PIX_FMT_RGB24 },
57 { 24, 16, 8, 0, 0, PIX_FMT_BGR24 },
60 static enum PixelFormat get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
64 for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) {
65 struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i];
66 if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
67 entry->red_offset == varinfo->red.offset &&
68 entry->green_offset == varinfo->green.offset &&
69 entry->blue_offset == varinfo->blue.offset)
77 int frame_size; ///< size in bytes of a grabbed frame
78 AVRational time_base; ///< time base
79 int64_t time_frame; ///< time for the next frame to output (in 1/1000000 units)
81 int fd; ///< framebuffer device file descriptor
82 int width, heigth; ///< assumed frame resolution
83 int frame_linesize; ///< linesize of the output frame, it is assumed to be constant
86 struct fb_var_screeninfo varinfo; ///< variable info;
87 struct fb_fix_screeninfo fixinfo; ///< fixed info;
89 uint8_t *data; ///< framebuffer data
92 av_cold static int fbdev_read_header(AVFormatContext *avctx,
93 AVFormatParameters *ap)
95 FBDevContext *fbdev = avctx->priv_data;
97 enum PixelFormat pix_fmt;
98 int ret, flags = O_RDONLY;
100 if (!(st = av_new_stream(avctx, 0)))
101 return AVERROR(ENOMEM);
102 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in microseconds */
104 if (ap->time_base.den <= 0) {
105 av_log(avctx, AV_LOG_ERROR, "Invalid time base %d/%d\n",
106 ap->time_base.num, ap->time_base.den);
107 return AVERROR(EINVAL);
110 /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
111 if (avctx->flags & AVFMT_FLAG_NONBLOCK)
114 if ((fbdev->fd = open(avctx->filename, flags)) == -1) {
115 ret = AVERROR(errno);
116 av_log(avctx, AV_LOG_ERROR,
117 "Could not open framebuffer device '%s': %s\n",
118 avctx->filename, strerror(ret));
122 if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
123 ret = AVERROR(errno);
124 av_log(avctx, AV_LOG_ERROR,
125 "FBIOGET_VSCREENINFO: %s\n", strerror(errno));
129 if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
130 ret = AVERROR(errno);
131 av_log(avctx, AV_LOG_ERROR,
132 "FBIOGET_FSCREENINFO: %s\n", strerror(errno));
136 pix_fmt = get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
137 if (pix_fmt == PIX_FMT_NONE) {
138 ret = AVERROR(EINVAL);
139 av_log(avctx, AV_LOG_ERROR,
140 "Framebuffer pixel format not supported.\n");
144 fbdev->width = fbdev->varinfo.xres;
145 fbdev->heigth = fbdev->varinfo.yres;
146 fbdev->bytes_per_pixel = (fbdev->varinfo.bits_per_pixel + 7) >> 3;
147 fbdev->frame_linesize = fbdev->width * fbdev->bytes_per_pixel;
148 fbdev->frame_size = fbdev->frame_linesize * fbdev->heigth;
149 fbdev->time_base = ap->time_base;
150 fbdev->time_frame = AV_NOPTS_VALUE;
151 fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->fd, 0);
152 if (fbdev->data == MAP_FAILED) {
153 ret = AVERROR(errno);
154 av_log(avctx, AV_LOG_ERROR, "Error in mmap(): %s\n", strerror(errno));
158 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
159 st->codec->codec_id = CODEC_ID_RAWVIDEO;
160 st->codec->width = fbdev->width;
161 st->codec->height = fbdev->heigth;
162 st->codec->pix_fmt = pix_fmt;
163 st->codec->time_base = ap->time_base;
164 st->codec->bit_rate =
165 fbdev->width * fbdev->heigth * fbdev->bytes_per_pixel / av_q2d(ap->time_base) * 8;
167 av_log(avctx, AV_LOG_INFO,
168 "w:%d h:%d bpp:%d pixfmt:%s tb:%d/%d bit_rate:%d\n",
169 fbdev->width, fbdev->heigth, fbdev->varinfo.bits_per_pixel,
170 av_pix_fmt_descriptors[pix_fmt].name,
171 ap->time_base.num, ap->time_base.den,
172 st->codec->bit_rate);
180 static int fbdev_read_packet(AVFormatContext *avctx, AVPacket *pkt)
182 FBDevContext *fbdev = avctx->priv_data;
183 int64_t curtime, delay;
188 if (fbdev->time_frame == AV_NOPTS_VALUE)
189 fbdev->time_frame = av_gettime();
191 /* wait based on the frame rate */
193 curtime = av_gettime();
194 delay = fbdev->time_frame - curtime;
196 "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
197 fbdev->time_frame, curtime, delay);
199 fbdev->time_frame += INT64_C(1000000) * av_q2d(fbdev->time_base);
202 if (avctx->flags & AVFMT_FLAG_NONBLOCK)
203 return AVERROR(EAGAIN);
204 ts.tv_sec = delay / 1000000;
205 ts.tv_nsec = (delay % 1000000) * 1000;
206 while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
209 if ((ret = av_new_packet(pkt, fbdev->frame_size)) < 0)
212 /* refresh fbdev->varinfo, visible data position may change at each call */
213 if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
214 av_log(avctx, AV_LOG_WARNING,
215 "Error refreshing variable info: %s\n", strerror(errno));
219 /* compute visible data offset */
220 pin = fbdev->data + fbdev->bytes_per_pixel * fbdev->varinfo.xoffset +
221 fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
224 for (i = 0; i < fbdev->heigth; i++) {
225 memcpy(pout, pin, fbdev->frame_linesize);
226 pin += fbdev->fixinfo.line_length;
227 pout += fbdev->frame_linesize;
230 return fbdev->frame_size;
233 av_cold static int fbdev_read_close(AVFormatContext *avctx)
235 FBDevContext *fbdev = avctx->priv_data;
237 munmap(fbdev->data, fbdev->frame_size);
243 AVInputFormat ff_fbdev_demuxer = {
245 .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
246 .priv_data_size = sizeof(FBDevContext),
247 .read_header = fbdev_read_header,
248 .read_packet = fbdev_read_packet,
249 .read_close = fbdev_read_close,
250 .flags = AVFMT_NOFILE,