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
32 #include "libavcodec/dsputil.h"
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/ioctl.h>
36 #include <sys/mman.h>
37 #include <sys/time.h>
38 #define _LINUX_TIME_H 1
39 #include <linux/videodev.h>
41
42 typedef struct {
50 struct video_capability video_cap;
51 struct video_audio audio_saved;
52 struct video_window video_win;
54 struct video_mbuf gb_buffers;
55 struct video_mmap gb_buf;
57 int standard;
59
60 static const struct {
69 /* NOTE: v4l uses BGR24, not RGB24 */
73 };
74
75
77 {
80 int video_fd;
81 int desired_palette, desired_depth;
82 struct video_tuner tuner;
83 struct video_audio audio;
84 struct video_picture pict;
85 int j;
87
88 av_log(s1,
AV_LOG_WARNING,
"V4L input device is deprecated and will be removed in the next release.");
89
90 if (ap->time_base.den <= 0) {
92 return -1;
93 }
95
98
100 if (!st)
103
104 video_fd = open(s1->
filename, O_RDWR);
105 if (video_fd < 0) {
108 }
109
110 if (ioctl(video_fd, VIDIOCGCAP, &s->
video_cap) < 0) {
113 }
114
115 if (!(s->
video_cap.type & VID_TYPE_CAPTURE)) {
118 }
119
120 /* no values set, autodetect them */
125 }
126 }
127
129 return -1;
130
131 desired_palette = -1;
132 desired_depth = -1;
133 for (j = 0; j < vformat_num; j++) {
137 break;
138 }
139 }
140
141 /* set tv standard */
142 if (!ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
144 ioctl(video_fd, VIDIOCSTUNER, &tuner);
145 }
146
147 /* unmute audio */
148 audio.audio = 0;
149 ioctl(video_fd, VIDIOCGAUDIO, &audio);
151 audio.flags &= ~VIDEO_AUDIO_MUTE;
152 ioctl(video_fd, VIDIOCSAUDIO, &audio);
153
154 ioctl(video_fd, VIDIOCGPICT, &pict);
155 ff_dlog(s1,
"v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
156 pict.colour, pict.hue, pict.brightness, pict.contrast, pict.whiteness);
157 /* try to choose a suitable video format */
158 pict.palette = desired_palette;
159 pict.depth= desired_depth;
160 if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
161 for (j = 0; j < vformat_num; j++) {
164 if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
165 break;
166 }
167 if (j >= vformat_num)
168 goto fail1;
169 }
170
171 if (ioctl(video_fd, VIDIOCGMBUF, &s->
gb_buffers) < 0) {
172 /* try to use read based access */
174
179
180 if (ioctl(video_fd, VIDIOCSWIN, s->
video_win) < 0) {
183 }
184
186
187 val = 1;
188 if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) {
191 }
192
195 } else {
197 if ((
unsigned char*)-1 == s->
video_buf) {
198 s->
video_buf = mmap(0, s->
gb_buffers.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, video_fd, 0);
199 if ((
unsigned char*)-1 == s->
video_buf) {
202 }
203 }
206
207 /* start to grab the first frame */
211 s->
gb_buf.format = pict.palette;
212
213 if (ioctl(video_fd, VIDIOCMCAPTURE, &s->
gb_buf) < 0) {
214 if (errno != EAGAIN) {
215 fail1:
217 } else {
219 }
221 }
224 ioctl(video_fd, VIDIOCMCAPTURE, &s->
gb_buf);
225 }
228 }
229
230 for (j = 0; j < vformat_num; j++) {
234 break;
235 }
236 }
237
238 if (j >= vformat_num)
240
242
249
250 return 0;
252 if (video_fd >= 0)
253 close(video_fd);
255 }
256
258 {
260
261 while (ioctl(s->
fd, VIDIOCSYNC, &s->
gb_frame) < 0 &&
262 (errno == EAGAIN || errno == EINTR));
263
266
267 /* Setup to capture the next frame */
269 if (ioctl(s->
fd, VIDIOCMCAPTURE, &s->
gb_buf) < 0) {
270 if (errno == EAGAIN)
272 else
275 }
276
277 /* This is now the grabbing frame */
279
281 }
282
284 {
286 int64_t curtime, delay;
287 struct timespec ts;
288
289 /* Calculate the time of the next frame */
291
292 /* wait based on the frame rate */
293 for(;;) {
296 if (delay <= 0) {
298 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
300 }
301 break;
302 }
303 ts.tv_sec = delay / 1000000;
304 ts.tv_nsec = (delay % 1000000) * 1000;
305 nanosleep(&ts,
NULL);
306 }
307
310
312
313 /* read one frame */
316 } else {
320 }
321 }
322
324 {
326
329
330 /* mute audio. we must force it because the BTTV driver does not
331 return its state correctly */
334
336 return 0;
337 }
338
340 {
"standard",
"", offsetof(
VideoData, standard),
AV_OPT_TYPE_INT, {.i64 = VIDEO_MODE_NTSC}, VIDEO_MODE_PAL, VIDEO_MODE_NTSC,
AV_OPT_FLAG_DECODING_PARAM,
"standard" },
345 };
346
353 };
354
356 .
name =
"video4linux,v4l",
363 .priv_class = &v4l_class,
364 };
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
const char const char void * val
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
#define LIBAVUTIL_VERSION_INT
struct video_capability video_cap
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
struct video_window video_win
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
static double av_q2d(AVRational a)
Convert rational to double.
static av_cold int read_close(AVFormatContext *ctx)
enum AVPixelFormat pix_fmt
static const struct @128 video_formats[]
Main libavdevice API header.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVCodecContext * codec
Codec context associated with this stream.
common internal API header
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
static const AVClass v4l_class
char filename[1024]
input or output filename
int width
picture width / height.
static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
packed RGB 8:8:8, 24bpp, BGRBGR...
#define FF_ARRAY_ELEMS(a)
static int read_header(FFV1Context *f)
int64_t av_gettime(void)
Get the current time in microseconds.
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
enum AVMediaType codec_type
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Describe the class of an AVClass context structure.
rational number numerator/denominator
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
static const AVOption options[]
#define AV_PIX_FMT_BGR565
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
static int grab_read_close(AVFormatContext *s1)
struct video_mbuf gb_buffers
static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
AVInputFormat ff_v4l_demuxer
void * priv_data
Format private data.
struct video_audio audio_saved
AVPixelFormat
Pixel format.
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...