2 * Linux video grab interface
3 * Copyright (c) 2000,2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/videodev.h>
23 #include <sys/ioctl.h>
30 int frame_format; /* see VIDEO_PALETTE_xxx */
38 const char *v4l_device = "/dev/video";
40 /* XXX: move all that to the context */
42 static struct video_capability video_cap;
43 static UINT8 *video_buf;
44 static struct video_mbuf gb_buffers;
45 static struct video_mmap gb_buf;
46 static struct video_audio audio, audio_saved;
47 static int gb_frame = 0;
49 static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
51 VideoData *s = s1->priv_data;
54 int video_fd, frame_size;
58 if (!ap || ap->width <= 0 || ap->height <= 0 || ap->frame_rate <= 0)
63 frame_rate = ap->frame_rate;
65 st = av_new_stream(s1, 0);
71 s->frame_rate = frame_rate;
73 video_fd = open(v4l_device, O_RDWR);
79 if (ioctl(video_fd,VIDIOCGCAP,&video_cap) < 0) {
84 if (!(video_cap.type & VID_TYPE_CAPTURE)) {
85 fprintf(stderr, "Fatal: grab device does not handle capture\n");
90 if (st->codec.pix_fmt == PIX_FMT_YUV420P) {
91 desired_palette = VIDEO_PALETTE_YUV420P;
92 } else if (st->codec.pix_fmt == PIX_FMT_YUV422) {
93 desired_palette = VIDEO_PALETTE_YUV422;
94 } else if (st->codec.pix_fmt == PIX_FMT_BGR24) {
95 desired_palette = VIDEO_PALETTE_RGB24;
99 ioctl(video_fd, VIDIOCGAUDIO, &audio);
100 memcpy(&audio_saved, &audio, sizeof(audio));
101 audio.flags &= ~VIDEO_AUDIO_MUTE;
102 ioctl(video_fd, VIDIOCSAUDIO, &audio);
104 ret = ioctl(video_fd,VIDIOCGMBUF,&gb_buffers);
106 /* try to use read based access */
107 struct video_window win;
108 struct video_picture pict;
118 ioctl(video_fd, VIDIOCSWIN, &win);
120 ioctl(video_fd, VIDIOCGPICT, &pict);
122 printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
129 /* try to choose a suitable video format */
130 pict.palette = desired_palette;
131 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
132 pict.palette=VIDEO_PALETTE_YUV420P;
133 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
135 pict.palette=VIDEO_PALETTE_YUV422;
136 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
138 pict.palette=VIDEO_PALETTE_RGB24;
139 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
146 s->frame_format = pict.palette;
149 ioctl(video_fd, VIDIOCCAPTURE, &val);
151 s->time_frame = gettime();
154 video_buf = mmap(0,gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
155 if ((unsigned char*)-1 == video_buf) {
160 s->time_frame = gettime();
162 /* start to grab the first frame */
163 gb_buf.frame = gb_frame % gb_buffers.frames;
164 gb_buf.height = height;
165 gb_buf.width = width;
166 gb_buf.format = desired_palette;
168 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf)) < 0) {
169 gb_buf.format = VIDEO_PALETTE_YUV420P;
171 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
172 if (ret < 0 && errno != EAGAIN) {
174 gb_buf.format = VIDEO_PALETTE_YUV422;
176 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
177 if (ret < 0 && errno != EAGAIN) {
179 gb_buf.format = VIDEO_PALETTE_RGB24;
180 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
185 if (errno != EAGAIN) {
187 fprintf(stderr, "Fatal: grab device does not support suitable format\n");
189 fprintf(stderr,"Fatal: grab device does not receive any video signal\n");
193 s->frame_format = gb_buf.format;
197 switch(s->frame_format) {
198 case VIDEO_PALETTE_YUV420P:
199 frame_size = (width * height * 3) / 2;
200 st->codec.pix_fmt = PIX_FMT_YUV420P;
202 case VIDEO_PALETTE_YUV422:
203 frame_size = width * height * 2;
204 st->codec.pix_fmt = PIX_FMT_YUV422;
206 case VIDEO_PALETTE_RGB24:
207 frame_size = width * height * 3;
208 st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
214 s->frame_size = frame_size;
216 st->codec.codec_type = CODEC_TYPE_VIDEO;
217 st->codec.codec_id = CODEC_ID_RAWVIDEO;
218 st->codec.width = width;
219 st->codec.height = height;
220 st->codec.frame_rate = frame_rate;
230 static int v4l_mm_read_picture(VideoData *s, UINT8 *buf)
234 //struct timeval tv_e;
237 /* Setup to capture the next frame */
238 gb_buf.frame = (gb_frame + 1) % gb_buffers.frames;
239 if (ioctl(s->fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
241 fprintf(stderr,"Cannot Sync\n");
243 perror("VIDIOCMCAPTURE");
247 gettimeofday(&tv_s, 0);
249 while (ioctl(s->fd, VIDIOCSYNC, &gb_frame) < 0 &&
250 (errno == EAGAIN || errno == EINTR));
253 gettimeofday(&tv_e, 0);
255 delay = (tv_e.tv_sec - tv_s.tv_sec) * 1000000 + tv_e.tv_usec - tv_s.tv_usec;
257 printf("VIDIOCSYNC took %d us\n", delay);
260 ptr = video_buf + gb_buffers.offsets[gb_frame];
261 memcpy(buf, ptr, s->frame_size);
263 /* This is now the grabbing frame */
264 gb_frame = gb_buf.frame;
266 return s->frame_size;
269 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
271 VideoData *s = s1->priv_data;
272 INT64 curtime, delay;
275 INT64 per_frame = (INT64_C(1000000) * FRAME_RATE_BASE) / s->frame_rate;
278 /* Calculate the time of the next frame */
279 s->time_frame += per_frame;
281 /* wait based on the frame rate */
282 for(first = 1;; first = 0) {
284 delay = s->time_frame - curtime;
286 if (delay < -per_frame) {
287 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
289 s->time_frame += per_frame;
293 ts.tv_sec = delay / 1000000;
294 ts.tv_nsec = (delay % 1000000) * 1000;
295 nanosleep(&ts, NULL);
298 if (av_new_packet(pkt, s->frame_size) < 0)
302 pkt->flags |= PKT_FLAG_DROPPED_FRAME;
306 return v4l_mm_read_picture(s, pkt->data);
308 if (read(s->fd, pkt->data, pkt->size) != pkt->size)
310 return s->frame_size;
314 static int grab_read_close(AVFormatContext *s1)
316 VideoData *s = s1->priv_data;
317 /* restore audio settings */
318 ioctl(s->fd, VIDIOCSAUDIO, &audio_saved);
324 AVInputFormat video_grab_device_format = {
335 int video_grab_init(void)
337 av_register_input_format(&video_grab_device_format);