2 * Linux video grab interface
3 * Copyright (c) 2000,2001 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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
22 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
24 #include "libavutil/rational.h"
25 #include "libavformat/avformat.h"
26 #include "libavcodec/dsputil.h"
29 #include <sys/ioctl.h>
32 #define _LINUX_TIME_H 1
33 #include <linux/videodev.h>
39 int frame_format; /* see VIDEO_PALETTE_xxx */
45 struct video_capability video_cap;
46 struct video_audio audio_saved;
48 struct video_mbuf gb_buffers;
49 struct video_mmap gb_buf;
56 enum PixelFormat pix_fmt;
57 } video_formats [] = {
58 {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
59 {.palette = VIDEO_PALETTE_YUV422, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
60 {.palette = VIDEO_PALETTE_UYVY, .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
61 {.palette = VIDEO_PALETTE_YUYV, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
62 /* NOTE: v4l uses BGR24, not RGB24 */
63 {.palette = VIDEO_PALETTE_RGB24, .depth = 24, .pix_fmt = PIX_FMT_BGR24 },
64 {.palette = VIDEO_PALETTE_RGB565, .depth = 16, .pix_fmt = PIX_FMT_BGR565 },
65 {.palette = VIDEO_PALETTE_GREY, .depth = 8, .pix_fmt = PIX_FMT_GRAY8 },
69 static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
71 VideoData *s = s1->priv_data;
74 int video_fd, frame_size;
75 int desired_palette, desired_depth;
76 struct video_tuner tuner;
77 struct video_audio audio;
78 struct video_picture pict;
80 int vformat_num = FF_ARRAY_ELEMS(video_formats);
82 if (ap->width <= 0 || ap->height <= 0) {
83 av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
86 if (ap->time_base.den <= 0) {
87 av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
90 s->time_base = ap->time_base;
95 if((unsigned)width > 32767 || (unsigned)height > 32767) {
96 av_log(s1, AV_LOG_ERROR, "Capture size is out of range: %dx%d\n",
102 st = av_new_stream(s1, 0);
104 return AVERROR(ENOMEM);
105 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
110 video_fd = open(s1->filename, O_RDWR);
112 av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
116 if (ioctl(video_fd,VIDIOCGCAP, &s->video_cap) < 0) {
117 av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
121 if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
122 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
126 desired_palette = -1;
128 for (j = 0; j < vformat_num; j++) {
129 if (ap->pix_fmt == video_formats[j].pix_fmt) {
130 desired_palette = video_formats[j].palette;
131 desired_depth = video_formats[j].depth;
136 /* set tv standard */
137 if (ap->standard && !ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
138 if (!strcasecmp(ap->standard, "pal"))
139 tuner.mode = VIDEO_MODE_PAL;
140 else if (!strcasecmp(ap->standard, "secam"))
141 tuner.mode = VIDEO_MODE_SECAM;
143 tuner.mode = VIDEO_MODE_NTSC;
144 ioctl(video_fd, VIDIOCSTUNER, &tuner);
149 ioctl(video_fd, VIDIOCGAUDIO, &audio);
150 memcpy(&s->audio_saved, &audio, sizeof(audio));
151 audio.flags &= ~VIDEO_AUDIO_MUTE;
152 ioctl(video_fd, VIDIOCSAUDIO, &audio);
154 ioctl(video_fd, VIDIOCGPICT, &pict);
156 printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
163 /* try to choose a suitable video format */
164 pict.palette = desired_palette;
165 pict.depth= desired_depth;
166 if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
167 for (j = 0; j < vformat_num; j++) {
168 pict.palette = video_formats[j].palette;
169 pict.depth = video_formats[j].depth;
170 if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
173 if (j >= vformat_num)
177 if (ioctl(video_fd,VIDIOCGMBUF,&s->gb_buffers) < 0) {
178 /* try to use read based access */
179 struct video_window win;
189 ioctl(video_fd, VIDIOCSWIN, &win);
191 s->frame_format = pict.palette;
194 ioctl(video_fd, VIDIOCCAPTURE, &val);
196 s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
199 s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
200 if ((unsigned char*)-1 == s->video_buf) {
201 s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_PRIVATE,video_fd,0);
202 if ((unsigned char*)-1 == s->video_buf) {
203 av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
208 s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
210 /* start to grab the first frame */
211 s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
212 s->gb_buf.height = height;
213 s->gb_buf.width = width;
214 s->gb_buf.format = pict.palette;
216 if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
217 if (errno != EAGAIN) {
219 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not support suitable format\n");
221 av_log(s1, AV_LOG_ERROR,"Fatal: grab device does not receive any video signal\n");
225 for (j = 1; j < s->gb_buffers.frames; j++) {
227 ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
229 s->frame_format = s->gb_buf.format;
233 for (j = 0; j < vformat_num; j++) {
234 if (s->frame_format == video_formats[j].palette) {
235 frame_size = width * height * video_formats[j].depth / 8;
236 st->codec->pix_fmt = video_formats[j].pix_fmt;
241 if (j >= vformat_num)
245 s->frame_size = frame_size;
247 st->codec->codec_type = CODEC_TYPE_VIDEO;
248 st->codec->codec_id = CODEC_ID_RAWVIDEO;
249 st->codec->width = width;
250 st->codec->height = height;
251 st->codec->time_base = s->time_base;
252 st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
261 static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
265 while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
266 (errno == EAGAIN || errno == EINTR));
268 ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
269 memcpy(buf, ptr, s->frame_size);
271 /* Setup to capture the next frame */
272 s->gb_buf.frame = s->gb_frame;
273 if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
275 av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
277 av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
281 /* This is now the grabbing frame */
282 s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
284 return s->frame_size;
287 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
289 VideoData *s = s1->priv_data;
290 int64_t curtime, delay;
293 /* Calculate the time of the next frame */
294 s->time_frame += INT64_C(1000000);
296 /* wait based on the frame rate */
298 curtime = av_gettime();
299 delay = s->time_frame * s->time_base.num / s->time_base.den - curtime;
301 if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) {
302 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
303 s->time_frame += INT64_C(1000000);
307 ts.tv_sec = delay / 1000000;
308 ts.tv_nsec = (delay % 1000000) * 1000;
309 nanosleep(&ts, NULL);
312 if (av_new_packet(pkt, s->frame_size) < 0)
319 return v4l_mm_read_picture(s, pkt->data);
321 if (read(s->fd, pkt->data, pkt->size) != pkt->size)
323 return s->frame_size;
327 static int grab_read_close(AVFormatContext *s1)
329 VideoData *s = s1->priv_data;
332 munmap(s->video_buf, s->gb_buffers.size);
334 /* mute audio. we must force it because the BTTV driver does not
335 return its state correctly */
336 s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
337 ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
343 AVInputFormat v4l_demuxer = {
345 NULL_IF_CONFIG_SMALL("video grab"),
351 .flags = AVFMT_NOFILE,