suppressed mpeg demux mess - use now dynamic stream creation api
[libav.git] / libav / grab.c
CommitLineData
de6d9b64
FB
1/*
2 * Linux video grab interface
3 * Copyright (c) 2000,2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
8be1c656 19#include "avformat.h"
de6d9b64
FB
20#include <linux/videodev.h>
21#include <unistd.h>
22#include <fcntl.h>
23#include <sys/ioctl.h>
24#include <sys/mman.h>
de6d9b64 25#include <sys/time.h>
608d0dee 26#include <time.h>
de6d9b64
FB
27
28typedef struct {
29 int fd;
30 int frame_format; /* see VIDEO_PALETTE_xxx */
31 int use_mmap;
32 int width, height;
4972b26f 33 int frame_rate;
de6d9b64 34 INT64 time_frame;
4972b26f 35 int frame_size;
de6d9b64
FB
36} VideoData;
37
38const char *v4l_device = "/dev/video";
39
40/* XXX: move all that to the context */
41
42static struct video_capability video_cap;
43static UINT8 *video_buf;
44static struct video_mbuf gb_buffers;
45static struct video_mmap gb_buf;
5a56c87c 46static struct video_audio audio, audio_saved;
de6d9b64
FB
47static int gb_frame = 0;
48
4972b26f 49static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
de6d9b64 50{
4972b26f
FB
51 VideoData *s;
52 AVStream *st;
de6d9b64 53 int width, height;
de6d9b64 54 int video_fd, frame_size;
4972b26f 55 int ret, frame_rate;
4606ac8d 56 int desired_palette;
4972b26f
FB
57
58 if (!ap || ap->width <= 0 || ap->height <= 0 || ap->frame_rate <= 0)
59 return -1;
de6d9b64 60
4972b26f
FB
61 width = ap->width;
62 height = ap->height;
63 frame_rate = ap->frame_rate;
64
65 s = av_mallocz(sizeof(VideoData));
66 if (!s)
67 return -ENOMEM;
68 st = av_mallocz(sizeof(AVStream));
69 if (!st) {
1ea4f593 70 av_free(s);
4972b26f
FB
71 return -ENOMEM;
72 }
73 s1->priv_data = s;
74 s1->nb_streams = 1;
75 s1->streams[0] = st;
76
77 s->width = width;
78 s->height = height;
79 s->frame_rate = frame_rate;
de6d9b64
FB
80
81 video_fd = open(v4l_device, O_RDWR);
82 if (video_fd < 0) {
83 perror(v4l_device);
4972b26f 84 goto fail;
de6d9b64
FB
85 }
86
87 if (ioctl(video_fd,VIDIOCGCAP,&video_cap) < 0) {
88 perror("VIDIOCGCAP");
89 goto fail;
90 }
91
92 if (!(video_cap.type & VID_TYPE_CAPTURE)) {
93 fprintf(stderr, "Fatal: grab device does not handle capture\n");
94 goto fail;
95 }
4606ac8d
ZK
96
97 desired_palette = -1;
98 if (st->codec.pix_fmt == PIX_FMT_YUV420P) {
99 desired_palette = VIDEO_PALETTE_YUV420P;
100 } else if (st->codec.pix_fmt == PIX_FMT_YUV422) {
101 desired_palette = VIDEO_PALETTE_YUV422;
102 } else if (st->codec.pix_fmt == PIX_FMT_BGR24) {
103 desired_palette = VIDEO_PALETTE_RGB24;
104 }
de6d9b64
FB
105
106 /* unmute audio */
107 ioctl(video_fd, VIDIOCGAUDIO, &audio);
5a56c87c 108 memcpy(&audio_saved, &audio, sizeof(audio));
de6d9b64
FB
109 audio.flags &= ~VIDEO_AUDIO_MUTE;
110 ioctl(video_fd, VIDIOCSAUDIO, &audio);
111
112 ret = ioctl(video_fd,VIDIOCGMBUF,&gb_buffers);
113 if (ret < 0) {
114 /* try to use read based access */
115 struct video_window win;
116 struct video_picture pict;
117 int val;
118
119 win.x = 0;
120 win.y = 0;
121 win.width = width;
122 win.height = height;
123 win.chromakey = -1;
124 win.flags = 0;
125
126 ioctl(video_fd, VIDIOCSWIN, &win);
127
128 ioctl(video_fd, VIDIOCGPICT, &pict);
129#if 0
130 printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
131 pict.colour,
132 pict.hue,
133 pict.brightness,
134 pict.contrast,
135 pict.whiteness);
136#endif
137 /* try to choose a suitable video format */
4606ac8d
ZK
138 pict.palette = desired_palette;
139 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
140 pict.palette=VIDEO_PALETTE_YUV420P;
de6d9b64
FB
141 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
142 if (ret < 0) {
4606ac8d 143 pict.palette=VIDEO_PALETTE_YUV422;
de6d9b64 144 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
4606ac8d
ZK
145 if (ret < 0) {
146 pict.palette=VIDEO_PALETTE_RGB24;
147 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
148 if (ret < 0)
149 goto fail1;
150 }
de6d9b64
FB
151 }
152 }
153
154 s->frame_format = pict.palette;
155
156 val = 1;
157 ioctl(video_fd, VIDIOCCAPTURE, &val);
158
159 s->time_frame = gettime();
160 s->use_mmap = 0;
161 } else {
162 video_buf = mmap(0,gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
163 if ((unsigned char*)-1 == video_buf) {
164 perror("mmap");
165 goto fail;
166 }
167 gb_frame = 0;
168 s->time_frame = gettime();
169
170 /* start to grab the first frame */
4606ac8d 171 gb_buf.frame = gb_frame % gb_buffers.frames;
de6d9b64
FB
172 gb_buf.height = height;
173 gb_buf.width = width;
4606ac8d
ZK
174 gb_buf.format = desired_palette;
175
176 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf)) < 0) {
177 gb_buf.format = VIDEO_PALETTE_YUV420P;
de6d9b64
FB
178
179 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
4606ac8d
ZK
180 if (ret < 0 && errno != EAGAIN) {
181 /* try YUV422 */
182 gb_buf.format = VIDEO_PALETTE_YUV422;
183
184 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
185 if (ret < 0 && errno != EAGAIN) {
186 /* try RGB24 */
187 gb_buf.format = VIDEO_PALETTE_RGB24;
188 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
189 }
190 }
de6d9b64
FB
191 }
192 if (ret < 0) {
193 if (errno != EAGAIN) {
194 fail1:
195 fprintf(stderr, "Fatal: grab device does not support suitable format\n");
196 } else {
197 fprintf(stderr,"Fatal: grab device does not receive any video signal\n");
198 }
199 goto fail;
200 }
201 s->frame_format = gb_buf.format;
202 s->use_mmap = 1;
203 }
204
205 switch(s->frame_format) {
206 case VIDEO_PALETTE_YUV420P:
207 frame_size = (width * height * 3) / 2;
4972b26f 208 st->codec.pix_fmt = PIX_FMT_YUV420P;
de6d9b64
FB
209 break;
210 case VIDEO_PALETTE_YUV422:
211 frame_size = width * height * 2;
4972b26f 212 st->codec.pix_fmt = PIX_FMT_YUV422;
de6d9b64
FB
213 break;
214 case VIDEO_PALETTE_RGB24:
215 frame_size = width * height * 3;
4972b26f 216 st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
de6d9b64
FB
217 break;
218 default:
219 goto fail;
220 }
221 s->fd = video_fd;
4972b26f
FB
222 s->frame_size = frame_size;
223
28c66901 224 st->codec.codec_type = CODEC_TYPE_VIDEO;
4972b26f
FB
225 st->codec.codec_id = CODEC_ID_RAWVIDEO;
226 st->codec.width = width;
227 st->codec.height = height;
228 st->codec.frame_rate = frame_rate;
229
de6d9b64
FB
230 return 0;
231 fail:
4972b26f
FB
232 if (video_fd >= 0)
233 close(video_fd);
1ea4f593
FB
234 av_free(st);
235 av_free(s);
de6d9b64
FB
236 return -EIO;
237}
238
4972b26f 239static int v4l_mm_read_picture(VideoData *s, UINT8 *buf)
de6d9b64 240{
de6d9b64 241 UINT8 *ptr;
95c79a24
J
242 struct timeval tv_s;
243 //struct timeval tv_e;
244 //int delay;
de6d9b64 245
4606ac8d
ZK
246 /* Setup to capture the next frame */
247 gb_buf.frame = (gb_frame + 1) % gb_buffers.frames;
de6d9b64 248 if (ioctl(s->fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
28c66901
PG
249 if (errno == EAGAIN)
250 fprintf(stderr,"Cannot Sync\n");
251 else
de6d9b64 252 perror("VIDIOCMCAPTURE");
28c66901 253 return -EIO;
de6d9b64 254 }
4606ac8d
ZK
255
256 gettimeofday(&tv_s, 0);
de6d9b64
FB
257
258 while (ioctl(s->fd, VIDIOCSYNC, &gb_frame) < 0 &&
259 (errno == EAGAIN || errno == EINTR));
260
4606ac8d
ZK
261 /*
262 gettimeofday(&tv_e, 0);
263
264 delay = (tv_e.tv_sec - tv_s.tv_sec) * 1000000 + tv_e.tv_usec - tv_s.tv_usec;
265 if (delay > 10000)
266 printf("VIDIOCSYNC took %d us\n", delay);
267 */
268
de6d9b64 269 ptr = video_buf + gb_buffers.offsets[gb_frame];
4972b26f 270 memcpy(buf, ptr, s->frame_size);
4606ac8d
ZK
271
272 /* This is now the grabbing frame */
273 gb_frame = gb_buf.frame;
274
4972b26f 275 return s->frame_size;
de6d9b64
FB
276}
277
4972b26f 278static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
de6d9b64 279{
4972b26f
FB
280 VideoData *s = s1->priv_data;
281 INT64 curtime, delay;
282 struct timespec ts;
4606ac8d
ZK
283 int first;
284 INT64 per_frame = (INT64_C(1000000) * FRAME_RATE_BASE) / s->frame_rate;
285 int dropped = 0;
286
287 /* Calculate the time of the next frame */
288 s->time_frame += per_frame;
de6d9b64
FB
289
290 /* wait based on the frame rate */
4606ac8d 291 for(first = 1;; first = 0) {
de6d9b64 292 curtime = gettime();
4972b26f 293 delay = s->time_frame - curtime;
4606ac8d
ZK
294 if (delay <= 0) {
295 if (delay < -per_frame) {
296 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
297 dropped = 1;
298 s->time_frame += per_frame;
299 }
4972b26f 300 break;
4606ac8d 301 }
4972b26f
FB
302 ts.tv_sec = delay / 1000000;
303 ts.tv_nsec = (delay % 1000000) * 1000;
304 nanosleep(&ts, NULL);
305 }
306
307 if (av_new_packet(pkt, s->frame_size) < 0)
308 return -EIO;
de6d9b64 309
4606ac8d
ZK
310 if (dropped)
311 pkt->flags |= PKT_FLAG_DROPPED_FRAME;
312
de6d9b64
FB
313 /* read one frame */
314 if (s->use_mmap) {
4972b26f 315 return v4l_mm_read_picture(s, pkt->data);
de6d9b64 316 } else {
4972b26f 317 if (read(s->fd, pkt->data, pkt->size) != pkt->size)
de6d9b64 318 return -EIO;
4972b26f 319 return s->frame_size;
de6d9b64
FB
320 }
321}
322
4972b26f 323static int grab_read_close(AVFormatContext *s1)
de6d9b64 324{
4972b26f 325 VideoData *s = s1->priv_data;
5a56c87c
FB
326 /* restore audio settings */
327 ioctl(s->fd, VIDIOCSAUDIO, &audio_saved);
328
de6d9b64 329 close(s->fd);
1ea4f593 330 av_free(s);
de6d9b64
FB
331 return 0;
332}
333
4972b26f
FB
334AVFormat video_grab_device_format = {
335 "video_grab_device",
336 "video grab",
337 "",
338 "",
339 CODEC_ID_NONE,
340 CODEC_ID_NONE,
341 NULL,
342 NULL,
343 NULL,
344
345 grab_read_header,
346 grab_read_packet,
347 grab_read_close,
de6d9b64 348 NULL,
4972b26f 349 AVFMT_NOFILE,
de6d9b64 350};