1 /*
2 * Linux video grab interface
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
23
24 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
25 #include "config.h"
31 #include "libavcodec/dsputil.h"
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/ioctl.h>
35 #include <sys/mman.h>
36 #include <sys/time.h>
37 #define _LINUX_TIME_H 1
38 #include <linux/videodev.h>
40
41 typedef struct {
49 struct video_capability video_cap;
50 struct video_audio audio_saved;
51 struct video_window video_win;
53 struct video_mbuf gb_buffers;
54 struct video_mmap gb_buf;
56 int standard;
58
59 static const struct {
68 /* NOTE: v4l uses BGR24, not RGB24 */
72 };
73
74
76 {
79 int video_fd;
80 int desired_palette, desired_depth;
81 struct video_tuner tuner;
82 struct video_audio audio;
83 struct video_picture pict;
84 int j;
86
87 av_log(s1,
AV_LOG_WARNING,
"V4L input device is deprecated and will be removed in the next release.");
88
89 if (ap->time_base.den <= 0) {
91 return -1;
92 }
94
97
99 if (!st)
102
103 video_fd = open(s1->
filename, O_RDWR);
104 if (video_fd < 0) {
106 goto fail;
107 }
108
109 if (ioctl(video_fd, VIDIOCGCAP, &s->
video_cap) < 0) {
111 goto fail;
112 }
113
114 if (!(s->
video_cap.type & VID_TYPE_CAPTURE)) {
116 goto fail;
117 }
118
119 /* no values set, autodetect them */
123 goto fail;
124 }
125 }
126
128 return -1;
129
130 desired_palette = -1;
131 desired_depth = -1;
132 for (j = 0; j < vformat_num; j++) {
136 break;
137 }
138 }
139
140 /* set tv standard */
141 if (!ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
143 ioctl(video_fd, VIDIOCSTUNER, &tuner);
144 }
145
146 /* unmute audio */
147 audio.audio = 0;
148 ioctl(video_fd, VIDIOCGAUDIO, &audio);
150 audio.flags &= ~VIDEO_AUDIO_MUTE;
151 ioctl(video_fd, VIDIOCSAUDIO, &audio);
152
153 ioctl(video_fd, VIDIOCGPICT, &pict);
154 av_dlog(s1,
"v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
155 pict.colour, pict.hue, pict.brightness, pict.contrast, pict.whiteness);
156 /* try to choose a suitable video format */
157 pict.palette = desired_palette;
158 pict.depth= desired_depth;
159 if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
160 for (j = 0; j < vformat_num; j++) {
163 if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
164 break;
165 }
166 if (j >= vformat_num)
167 goto fail1;
168 }
169
170 if (ioctl(video_fd, VIDIOCGMBUF, &s->
gb_buffers) < 0) {
171 /* try to use read based access */
173
178
179 if (ioctl(video_fd, VIDIOCSWIN, s->
video_win) < 0) {
181 goto fail;
182 }
183
185
186 val = 1;
187 if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) {
189 goto fail;
190 }
191
194 } else {
196 if ((
unsigned char*)-1 == s->
video_buf) {
197 s->
video_buf = mmap(0, s->
gb_buffers.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, video_fd, 0);
198 if ((
unsigned char*)-1 == s->
video_buf) {
200 goto fail;
201 }
202 }
205
206 /* start to grab the first frame */
210 s->
gb_buf.format = pict.palette;
211
212 if (ioctl(video_fd, VIDIOCMCAPTURE, &s->
gb_buf) < 0) {
213 if (errno != EAGAIN) {
214 fail1:
216 } else {
218 }
219 goto fail;
220 }
223 ioctl(video_fd, VIDIOCMCAPTURE, &s->
gb_buf);
224 }
227 }
228
229 for (j = 0; j < vformat_num; j++) {
233 break;
234 }
235 }
236
237 if (j >= vformat_num)
238 goto fail;
239
241
248
249 return 0;
250 fail:
251 if (video_fd >= 0)
252 close(video_fd);
254 }
255
257 {
259
260 while (ioctl(s->
fd, VIDIOCSYNC, &s->
gb_frame) < 0 &&
261 (errno == EAGAIN || errno == EINTR));
262
265
266 /* Setup to capture the next frame */
268 if (ioctl(s->
fd, VIDIOCMCAPTURE, &s->
gb_buf) < 0) {
269 if (errno == EAGAIN)
271 else
274 }
275
276 /* This is now the grabbing frame */
278
280 }
281
283 {
285 int64_t curtime, delay;
286 struct timespec ts;
287
288 /* Calculate the time of the next frame */
290
291 /* wait based on the frame rate */
292 for(;;) {
295 if (delay <= 0) {
297 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
299 }
300 break;
301 }
302 ts.tv_sec = delay / 1000000;
303 ts.tv_nsec = (delay % 1000000) * 1000;
304 nanosleep(&ts,
NULL);
305 }
306
309
311
312 /* read one frame */
315 } else {
319 }
320 }
321
323 {
325
328
329 /* mute audio. we must force it because the BTTV driver does not
330 return its state correctly */
333
335 return 0;
336 }
337
339 {
"standard",
"", offsetof(
VideoData, standard),
AV_OPT_TYPE_INT, {.i64 = VIDEO_MODE_NTSC}, VIDEO_MODE_PAL, VIDEO_MODE_NTSC,
AV_OPT_FLAG_DECODING_PARAM,
"standard" },
344 };
345
352 };
353
355 .
name =
"video4linux,v4l",
362 .priv_class = &v4l_class,
363 };