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 /**
24 * @file
25 * Linux framebuffer input device,
26 * inspired by code from fbgrab.c by Gunnar Monell.
27 * @see http://linux-fbdev.sourceforge.net/
28 */
29
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/ioctl.h>
33 #include <sys/mman.h>
35 #include <linux/fb.h>
36
45
50 };
51
53 // bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt
60 };
61
63 {
64 int i;
65
73 }
74
76 }
77
79 AVClass *
class;
///< class for private options
82 char *
framerate;
///< framerate string set by a private option
83 int64_t
time_frame;
///< time for the next frame to output (in 1/1000000 units)
84
85 int fd;
///< framebuffer device file descriptor
87 int frame_linesize;
///< linesize of the output frame, it is assumed to be constant
89
90 struct fb_var_screeninfo varinfo;
///< variable info;
91 struct fb_fix_screeninfo fixinfo;
///< fixed info;
92
95
97 {
102
104 if (ret < 0) {
107 }
108
112
113 /* NONBLOCK is ignored by the fbdev driver, only set for consistency */
115 flags |= O_NONBLOCK;
116
117 if ((fbdev->
fd = open(avctx->
filename, flags)) == -1) {
120 "Could not open framebuffer device '%s': %s\n",
123 }
124
125 if (ioctl(fbdev->
fd, FBIOGET_VSCREENINFO, &fbdev->
varinfo) < 0) {
128 "FBIOGET_VSCREENINFO: %s\n", strerror(errno));
129 goto fail;
130 }
131
132 if (ioctl(fbdev->
fd, FBIOGET_FSCREENINFO, &fbdev->
fixinfo) < 0) {
135 "FBIOGET_FSCREENINFO: %s\n", strerror(errno));
136 goto fail;
137 }
138
143 "Framebuffer pixel format not supported.\n");
144 goto fail;
145 }
146
153 fbdev->
data = mmap(NULL, fbdev->
fixinfo.smem_len, PROT_READ, MAP_SHARED, fbdev->
fd, 0);
154 if (fbdev->
data == MAP_FAILED) {
157 goto fail;
158 }
159
168
170 "w:%d h:%d bpp:%d pixfmt:%s fps:%d/%d bit_rate:%d\n",
175 return 0;
176
177 fail:
180 }
181
183 {
185 int64_t curtime, delay;
186 struct timespec ts;
189
192
193 /* wait based on the frame rate */
194 while (1) {
198 "time_frame:%"PRId64" curtime:%"PRId64" delay:%"PRId64"\n",
200 if (delay <= 0) {
202 break;
203 }
206 ts.tv_sec = delay / 1000000;
207 ts.tv_nsec = (delay % 1000000) * 1000;
208 while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
209 }
210
212 return ret;
213
214 /* refresh fbdev->varinfo, visible data position may change at each call */
215 if (ioctl(fbdev->
fd, FBIOGET_VSCREENINFO, &fbdev->
varinfo) < 0)
217 "Error refreshing variable info: %s\n", strerror(errno));
218
220
221 /* compute visible data offset */
225
226 for (i = 0; i < fbdev->
height; i++) {
228 pin += fbdev->
fixinfo.line_length;
230 }
231
233 }
234
236 {
238
241
242 return 0;
243 }
244
245 #define OFFSET(x) offsetof(FBDevContext, x)
246 #define DEC AV_OPT_FLAG_DECODING_PARAM
249 { NULL },
250 };
251
257 };
258
267 .priv_class = &fbdev_class,
268 };