2 * Linux video grab interface
3 * Copyright (c) 2000,2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
28 #include "libavutil/rational.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/log.h"
31 #include "libavutil/opt.h"
32 #include "libavformat/avformat.h"
33 #include "libavcodec/dsputil.h"
36 #include <sys/ioctl.h>
39 #define _LINUX_TIME_H 1
40 #include <linux/videodev.h>
47 int frame_format; /* see VIDEO_PALETTE_xxx */
52 struct video_capability video_cap;
53 struct video_audio audio_saved;
54 struct video_window video_win;
56 struct video_mbuf gb_buffers;
57 struct video_mmap gb_buf;
65 enum PixelFormat pix_fmt;
66 } video_formats [] = {
67 {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
68 {.palette = VIDEO_PALETTE_YUV422, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
69 {.palette = VIDEO_PALETTE_UYVY, .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
70 {.palette = VIDEO_PALETTE_YUYV, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
71 /* NOTE: v4l uses BGR24, not RGB24 */
72 {.palette = VIDEO_PALETTE_RGB24, .depth = 24, .pix_fmt = PIX_FMT_BGR24 },
73 {.palette = VIDEO_PALETTE_RGB565, .depth = 16, .pix_fmt = PIX_FMT_BGR565 },
74 {.palette = VIDEO_PALETTE_GREY, .depth = 8, .pix_fmt = PIX_FMT_GRAY8 },
78 static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
80 VideoData *s = s1->priv_data;
83 int desired_palette, desired_depth;
84 struct video_tuner tuner;
85 struct video_audio audio;
86 struct video_picture pict;
88 int vformat_num = FF_ARRAY_ELEMS(video_formats);
90 av_log(s1, AV_LOG_WARNING, "V4L input device is deprecated and will be removed in the next release.");
92 if (ap->time_base.den <= 0) {
93 av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
96 s->time_base = ap->time_base;
98 s->video_win.width = ap->width;
99 s->video_win.height = ap->height;
101 st = av_new_stream(s1, 0);
103 return AVERROR(ENOMEM);
104 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
106 video_fd = open(s1->filename, O_RDWR);
108 av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
112 if (ioctl(video_fd, VIDIOCGCAP, &s->video_cap) < 0) {
113 av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
117 if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
118 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
122 /* no values set, autodetect them */
123 if (s->video_win.width <= 0 || s->video_win.height <= 0) {
124 if (ioctl(video_fd, VIDIOCGWIN, &s->video_win, sizeof(s->video_win)) < 0) {
125 av_log(s1, AV_LOG_ERROR, "VIDIOCGWIN: %s\n", strerror(errno));
130 if(av_image_check_size(s->video_win.width, s->video_win.height, 0, s1) < 0)
133 desired_palette = -1;
135 for (j = 0; j < vformat_num; j++) {
136 if (ap->pix_fmt == video_formats[j].pix_fmt) {
137 desired_palette = video_formats[j].palette;
138 desired_depth = video_formats[j].depth;
143 /* set tv standard */
144 if (!ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
145 #if FF_API_FORMAT_PARAMETERS
147 if (!strcasecmp(ap->standard, "pal"))
148 s->standard = VIDEO_MODE_PAL;
149 else if (!strcasecmp(ap->standard, "secam"))
150 s->standard = VIDEO_MODE_SECAM;
152 s->standard = VIDEO_MODE_NTSC;
155 tuner.mode = s->standard;
156 ioctl(video_fd, VIDIOCSTUNER, &tuner);
161 ioctl(video_fd, VIDIOCGAUDIO, &audio);
162 memcpy(&s->audio_saved, &audio, sizeof(audio));
163 audio.flags &= ~VIDEO_AUDIO_MUTE;
164 ioctl(video_fd, VIDIOCSAUDIO, &audio);
166 ioctl(video_fd, VIDIOCGPICT, &pict);
167 av_dlog(s1, "v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
168 pict.colour, pict.hue, pict.brightness, pict.contrast, pict.whiteness);
169 /* try to choose a suitable video format */
170 pict.palette = desired_palette;
171 pict.depth= desired_depth;
172 if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
173 for (j = 0; j < vformat_num; j++) {
174 pict.palette = video_formats[j].palette;
175 pict.depth = video_formats[j].depth;
176 if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
179 if (j >= vformat_num)
183 if (ioctl(video_fd, VIDIOCGMBUF, &s->gb_buffers) < 0) {
184 /* try to use read based access */
189 s->video_win.chromakey = -1;
190 s->video_win.flags = 0;
192 if (ioctl(video_fd, VIDIOCSWIN, s->video_win) < 0) {
193 av_log(s1, AV_LOG_ERROR, "VIDIOCSWIN: %s\n", strerror(errno));
197 s->frame_format = pict.palette;
200 if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) {
201 av_log(s1, AV_LOG_ERROR, "VIDIOCCAPTURE: %s\n", strerror(errno));
205 s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
208 s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_SHARED, video_fd, 0);
209 if ((unsigned char*)-1 == s->video_buf) {
210 s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, video_fd, 0);
211 if ((unsigned char*)-1 == s->video_buf) {
212 av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
217 s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
219 /* start to grab the first frame */
220 s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
221 s->gb_buf.height = s->video_win.height;
222 s->gb_buf.width = s->video_win.width;
223 s->gb_buf.format = pict.palette;
225 if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
226 if (errno != EAGAIN) {
228 av_log(s1, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
230 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not receive any video signal\n");
234 for (j = 1; j < s->gb_buffers.frames; j++) {
236 ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
238 s->frame_format = s->gb_buf.format;
242 for (j = 0; j < vformat_num; j++) {
243 if (s->frame_format == video_formats[j].palette) {
244 s->frame_size = s->video_win.width * s->video_win.height * video_formats[j].depth / 8;
245 st->codec->pix_fmt = video_formats[j].pix_fmt;
250 if (j >= vformat_num)
255 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
256 st->codec->codec_id = CODEC_ID_RAWVIDEO;
257 st->codec->width = s->video_win.width;
258 st->codec->height = s->video_win.height;
259 st->codec->time_base = s->time_base;
260 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
269 static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
273 while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
274 (errno == EAGAIN || errno == EINTR));
276 ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
277 memcpy(buf, ptr, s->frame_size);
279 /* Setup to capture the next frame */
280 s->gb_buf.frame = s->gb_frame;
281 if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
283 av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
285 av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
289 /* This is now the grabbing frame */
290 s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
292 return s->frame_size;
295 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
297 VideoData *s = s1->priv_data;
298 int64_t curtime, delay;
301 /* Calculate the time of the next frame */
302 s->time_frame += INT64_C(1000000);
304 /* wait based on the frame rate */
306 curtime = av_gettime();
307 delay = s->time_frame * s->time_base.num / s->time_base.den - curtime;
309 if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) {
310 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
311 s->time_frame += INT64_C(1000000);
315 ts.tv_sec = delay / 1000000;
316 ts.tv_nsec = (delay % 1000000) * 1000;
317 nanosleep(&ts, NULL);
320 if (av_new_packet(pkt, s->frame_size) < 0)
327 return v4l_mm_read_picture(s, pkt->data);
329 if (read(s->fd, pkt->data, pkt->size) != pkt->size)
331 return s->frame_size;
335 static int grab_read_close(AVFormatContext *s1)
337 VideoData *s = s1->priv_data;
340 munmap(s->video_buf, s->gb_buffers.size);
342 /* mute audio. we must force it because the BTTV driver does not
343 return its state correctly */
344 s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
345 ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
351 static const AVOption options[] = {
352 { "standard", "", offsetof(VideoData, standard), FF_OPT_TYPE_INT, {.dbl = VIDEO_MODE_NTSC}, VIDEO_MODE_PAL, VIDEO_MODE_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" },
353 { "PAL", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
354 { "SECAM", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_SECAM}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
355 { "NTSC", "", 0, FF_OPT_TYPE_CONST, {.dbl = VIDEO_MODE_NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
359 static const AVClass v4l_class = {
360 .class_name = "V4L indev",
361 .item_name = av_default_item_name,
363 .version = LIBAVUTIL_VERSION_INT,
366 AVInputFormat ff_v4l_demuxer = {
368 NULL_IF_CONFIG_SMALL("Video4Linux device grab"),
374 .flags = AVFMT_NOFILE,
375 .priv_class = &v4l_class,
377 #endif /* FF_API_V4L */