fixed double free - fixed probe function
[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{
c9a65ca8 51 VideoData *s = s1->priv_data;
4972b26f 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
c9a65ca8
FB
65 st = av_new_stream(s1, 0);
66 if (!st)
4972b26f 67 return -ENOMEM;
4972b26f
FB
68
69 s->width = width;
70 s->height = height;
71 s->frame_rate = frame_rate;
de6d9b64
FB
72
73 video_fd = open(v4l_device, O_RDWR);
74 if (video_fd < 0) {
75 perror(v4l_device);
4972b26f 76 goto fail;
de6d9b64
FB
77 }
78
79 if (ioctl(video_fd,VIDIOCGCAP,&video_cap) < 0) {
80 perror("VIDIOCGCAP");
81 goto fail;
82 }
83
84 if (!(video_cap.type & VID_TYPE_CAPTURE)) {
85 fprintf(stderr, "Fatal: grab device does not handle capture\n");
86 goto fail;
87 }
4606ac8d
ZK
88
89 desired_palette = -1;
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;
96 }
de6d9b64
FB
97
98 /* unmute audio */
99 ioctl(video_fd, VIDIOCGAUDIO, &audio);
5a56c87c 100 memcpy(&audio_saved, &audio, sizeof(audio));
de6d9b64
FB
101 audio.flags &= ~VIDEO_AUDIO_MUTE;
102 ioctl(video_fd, VIDIOCSAUDIO, &audio);
103
104 ret = ioctl(video_fd,VIDIOCGMBUF,&gb_buffers);
105 if (ret < 0) {
106 /* try to use read based access */
107 struct video_window win;
108 struct video_picture pict;
109 int val;
110
111 win.x = 0;
112 win.y = 0;
113 win.width = width;
114 win.height = height;
115 win.chromakey = -1;
116 win.flags = 0;
117
118 ioctl(video_fd, VIDIOCSWIN, &win);
119
120 ioctl(video_fd, VIDIOCGPICT, &pict);
121#if 0
122 printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
123 pict.colour,
124 pict.hue,
125 pict.brightness,
126 pict.contrast,
127 pict.whiteness);
128#endif
129 /* try to choose a suitable video format */
4606ac8d
ZK
130 pict.palette = desired_palette;
131 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
132 pict.palette=VIDEO_PALETTE_YUV420P;
de6d9b64
FB
133 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
134 if (ret < 0) {
4606ac8d 135 pict.palette=VIDEO_PALETTE_YUV422;
de6d9b64 136 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
4606ac8d
ZK
137 if (ret < 0) {
138 pict.palette=VIDEO_PALETTE_RGB24;
139 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
140 if (ret < 0)
141 goto fail1;
142 }
de6d9b64
FB
143 }
144 }
145
146 s->frame_format = pict.palette;
147
148 val = 1;
149 ioctl(video_fd, VIDIOCCAPTURE, &val);
150
151 s->time_frame = gettime();
152 s->use_mmap = 0;
153 } else {
154 video_buf = mmap(0,gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
155 if ((unsigned char*)-1 == video_buf) {
156 perror("mmap");
157 goto fail;
158 }
159 gb_frame = 0;
160 s->time_frame = gettime();
161
162 /* start to grab the first frame */
4606ac8d 163 gb_buf.frame = gb_frame % gb_buffers.frames;
de6d9b64
FB
164 gb_buf.height = height;
165 gb_buf.width = width;
4606ac8d
ZK
166 gb_buf.format = desired_palette;
167
168 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf)) < 0) {
169 gb_buf.format = VIDEO_PALETTE_YUV420P;
de6d9b64
FB
170
171 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
4606ac8d
ZK
172 if (ret < 0 && errno != EAGAIN) {
173 /* try YUV422 */
174 gb_buf.format = VIDEO_PALETTE_YUV422;
175
176 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
177 if (ret < 0 && errno != EAGAIN) {
178 /* try RGB24 */
179 gb_buf.format = VIDEO_PALETTE_RGB24;
180 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
181 }
182 }
de6d9b64
FB
183 }
184 if (ret < 0) {
185 if (errno != EAGAIN) {
186 fail1:
187 fprintf(stderr, "Fatal: grab device does not support suitable format\n");
188 } else {
189 fprintf(stderr,"Fatal: grab device does not receive any video signal\n");
190 }
191 goto fail;
192 }
193 s->frame_format = gb_buf.format;
194 s->use_mmap = 1;
195 }
196
197 switch(s->frame_format) {
198 case VIDEO_PALETTE_YUV420P:
199 frame_size = (width * height * 3) / 2;
4972b26f 200 st->codec.pix_fmt = PIX_FMT_YUV420P;
de6d9b64
FB
201 break;
202 case VIDEO_PALETTE_YUV422:
203 frame_size = width * height * 2;
4972b26f 204 st->codec.pix_fmt = PIX_FMT_YUV422;
de6d9b64
FB
205 break;
206 case VIDEO_PALETTE_RGB24:
207 frame_size = width * height * 3;
4972b26f 208 st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
de6d9b64
FB
209 break;
210 default:
211 goto fail;
212 }
213 s->fd = video_fd;
4972b26f
FB
214 s->frame_size = frame_size;
215
28c66901 216 st->codec.codec_type = CODEC_TYPE_VIDEO;
4972b26f
FB
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;
221
de6d9b64
FB
222 return 0;
223 fail:
4972b26f
FB
224 if (video_fd >= 0)
225 close(video_fd);
1ea4f593 226 av_free(st);
de6d9b64
FB
227 return -EIO;
228}
229
4972b26f 230static int v4l_mm_read_picture(VideoData *s, UINT8 *buf)
de6d9b64 231{
de6d9b64 232 UINT8 *ptr;
95c79a24
J
233 struct timeval tv_s;
234 //struct timeval tv_e;
235 //int delay;
de6d9b64 236
4606ac8d
ZK
237 /* Setup to capture the next frame */
238 gb_buf.frame = (gb_frame + 1) % gb_buffers.frames;
de6d9b64 239 if (ioctl(s->fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
28c66901
PG
240 if (errno == EAGAIN)
241 fprintf(stderr,"Cannot Sync\n");
242 else
de6d9b64 243 perror("VIDIOCMCAPTURE");
28c66901 244 return -EIO;
de6d9b64 245 }
4606ac8d
ZK
246
247 gettimeofday(&tv_s, 0);
de6d9b64
FB
248
249 while (ioctl(s->fd, VIDIOCSYNC, &gb_frame) < 0 &&
250 (errno == EAGAIN || errno == EINTR));
251
4606ac8d
ZK
252 /*
253 gettimeofday(&tv_e, 0);
254
255 delay = (tv_e.tv_sec - tv_s.tv_sec) * 1000000 + tv_e.tv_usec - tv_s.tv_usec;
256 if (delay > 10000)
257 printf("VIDIOCSYNC took %d us\n", delay);
258 */
259
de6d9b64 260 ptr = video_buf + gb_buffers.offsets[gb_frame];
4972b26f 261 memcpy(buf, ptr, s->frame_size);
4606ac8d
ZK
262
263 /* This is now the grabbing frame */
264 gb_frame = gb_buf.frame;
265
4972b26f 266 return s->frame_size;
de6d9b64
FB
267}
268
4972b26f 269static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
de6d9b64 270{
4972b26f
FB
271 VideoData *s = s1->priv_data;
272 INT64 curtime, delay;
273 struct timespec ts;
4606ac8d
ZK
274 int first;
275 INT64 per_frame = (INT64_C(1000000) * FRAME_RATE_BASE) / s->frame_rate;
276 int dropped = 0;
277
278 /* Calculate the time of the next frame */
279 s->time_frame += per_frame;
de6d9b64
FB
280
281 /* wait based on the frame rate */
4606ac8d 282 for(first = 1;; first = 0) {
de6d9b64 283 curtime = gettime();
4972b26f 284 delay = s->time_frame - curtime;
4606ac8d
ZK
285 if (delay <= 0) {
286 if (delay < -per_frame) {
287 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
288 dropped = 1;
289 s->time_frame += per_frame;
290 }
4972b26f 291 break;
4606ac8d 292 }
4972b26f
FB
293 ts.tv_sec = delay / 1000000;
294 ts.tv_nsec = (delay % 1000000) * 1000;
295 nanosleep(&ts, NULL);
296 }
297
298 if (av_new_packet(pkt, s->frame_size) < 0)
299 return -EIO;
de6d9b64 300
4606ac8d
ZK
301 if (dropped)
302 pkt->flags |= PKT_FLAG_DROPPED_FRAME;
303
de6d9b64
FB
304 /* read one frame */
305 if (s->use_mmap) {
4972b26f 306 return v4l_mm_read_picture(s, pkt->data);
de6d9b64 307 } else {
4972b26f 308 if (read(s->fd, pkt->data, pkt->size) != pkt->size)
de6d9b64 309 return -EIO;
4972b26f 310 return s->frame_size;
de6d9b64
FB
311 }
312}
313
4972b26f 314static int grab_read_close(AVFormatContext *s1)
de6d9b64 315{
4972b26f 316 VideoData *s = s1->priv_data;
5a56c87c
FB
317 /* restore audio settings */
318 ioctl(s->fd, VIDIOCSAUDIO, &audio_saved);
319
de6d9b64 320 close(s->fd);
de6d9b64
FB
321 return 0;
322}
323
c9a65ca8 324AVInputFormat video_grab_device_format = {
4972b26f
FB
325 "video_grab_device",
326 "video grab",
c9a65ca8 327 sizeof(VideoData),
4972b26f 328 NULL,
4972b26f
FB
329 grab_read_header,
330 grab_read_packet,
331 grab_read_close,
c9a65ca8 332 flags: AVFMT_NOFILE,
de6d9b64 333};
c9a65ca8
FB
334
335int video_grab_init(void)
336{
337 av_register_input_format(&video_grab_device_format);
338 return 0;
339}