added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't...
[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 */
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <linux/videodev.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <sys/ioctl.h>
26#include <sys/mman.h>
27#include <errno.h>
28#include <sys/time.h>
29
30#include "avformat.h"
31
32typedef struct {
33 int fd;
34 int frame_format; /* see VIDEO_PALETTE_xxx */
35 int use_mmap;
36 int width, height;
37 float rate;
38 INT64 time_frame;
39} VideoData;
40
41const char *v4l_device = "/dev/video";
42
43/* XXX: move all that to the context */
44
45static struct video_capability video_cap;
46static UINT8 *video_buf;
47static struct video_mbuf gb_buffers;
48static struct video_mmap gb_buf;
49static struct video_audio audio;
50static int gb_frame = 0;
51
52static int v4l_init(URLContext *h)
53{
54 VideoData *s = h->priv_data;
55 int width, height;
56 int ret;
57 int video_fd, frame_size;
58
59 width = s->width;
60 height = s->height;
61
62 video_fd = open(v4l_device, O_RDWR);
63 if (video_fd < 0) {
64 perror(v4l_device);
65 return -EIO;
66 }
67
68 if (ioctl(video_fd,VIDIOCGCAP,&video_cap) < 0) {
69 perror("VIDIOCGCAP");
70 goto fail;
71 }
72
73 if (!(video_cap.type & VID_TYPE_CAPTURE)) {
74 fprintf(stderr, "Fatal: grab device does not handle capture\n");
75 goto fail;
76 }
77
78 /* unmute audio */
79 ioctl(video_fd, VIDIOCGAUDIO, &audio);
80 audio.flags &= ~VIDEO_AUDIO_MUTE;
81 ioctl(video_fd, VIDIOCSAUDIO, &audio);
82
83 ret = ioctl(video_fd,VIDIOCGMBUF,&gb_buffers);
84 if (ret < 0) {
85 /* try to use read based access */
86 struct video_window win;
87 struct video_picture pict;
88 int val;
89
90 win.x = 0;
91 win.y = 0;
92 win.width = width;
93 win.height = height;
94 win.chromakey = -1;
95 win.flags = 0;
96
97 ioctl(video_fd, VIDIOCSWIN, &win);
98
99 ioctl(video_fd, VIDIOCGPICT, &pict);
100#if 0
101 printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
102 pict.colour,
103 pict.hue,
104 pict.brightness,
105 pict.contrast,
106 pict.whiteness);
107#endif
108 /* try to choose a suitable video format */
109 pict.palette=VIDEO_PALETTE_YUV420P;
110 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
111 if (ret < 0) {
112 pict.palette=VIDEO_PALETTE_YUV422;
113 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
114 if (ret < 0) {
115 pict.palette=VIDEO_PALETTE_RGB24;
116 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
117 if (ret < 0)
118 goto fail1;
119 }
120 }
121
122 s->frame_format = pict.palette;
123
124 val = 1;
125 ioctl(video_fd, VIDIOCCAPTURE, &val);
126
127 s->time_frame = gettime();
128 s->use_mmap = 0;
129 } else {
130 video_buf = mmap(0,gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
131 if ((unsigned char*)-1 == video_buf) {
132 perror("mmap");
133 goto fail;
134 }
135 gb_frame = 0;
136 s->time_frame = gettime();
137
138 /* start to grab the first frame */
139 gb_buf.frame = 1 - gb_frame;
140 gb_buf.height = height;
141 gb_buf.width = width;
142 gb_buf.format = VIDEO_PALETTE_YUV420P;
143
144 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
145 if (ret < 0 && errno != EAGAIN) {
146 /* try YUV422 */
147 gb_buf.format = VIDEO_PALETTE_YUV422;
148
149 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
150 if (ret < 0 && errno != EAGAIN) {
151 /* try RGB24 */
152 gb_buf.format = VIDEO_PALETTE_RGB24;
153 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
154 }
155 }
156 if (ret < 0) {
157 if (errno != EAGAIN) {
158 fail1:
159 fprintf(stderr, "Fatal: grab device does not support suitable format\n");
160 } else {
161 fprintf(stderr,"Fatal: grab device does not receive any video signal\n");
162 }
163 goto fail;
164 }
165 s->frame_format = gb_buf.format;
166 s->use_mmap = 1;
167 }
168
169 switch(s->frame_format) {
170 case VIDEO_PALETTE_YUV420P:
171 frame_size = (width * height * 3) / 2;
172 break;
173 case VIDEO_PALETTE_YUV422:
174 frame_size = width * height * 2;
175 break;
176 case VIDEO_PALETTE_RGB24:
177 frame_size = width * height * 3;
178 break;
179 default:
180 goto fail;
181 }
182 s->fd = video_fd;
183 h->packet_size = frame_size;
184 return 0;
185 fail:
186 close(video_fd);
187 return -EIO;
188}
189
190static int v4l_mm_read_picture(URLContext *h, UINT8 *buf)
191{
192 VideoData *s = h->priv_data;
193 UINT8 *ptr;
194
195 gb_buf.frame = gb_frame;
196 if (ioctl(s->fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
197 if (errno == EAGAIN)
198 fprintf(stderr,"Cannot Sync\n");
199 else
200 perror("VIDIOCMCAPTURE");
201 return -EIO;
202 }
203 gb_frame = 1 - gb_frame;
204
205 while (ioctl(s->fd, VIDIOCSYNC, &gb_frame) < 0 &&
206 (errno == EAGAIN || errno == EINTR));
207
208 ptr = video_buf + gb_buffers.offsets[gb_frame];
209 memcpy(buf, ptr, h->packet_size);
210 return h->packet_size;
211}
212
213/* note: we support only one picture read at a time */
214static int video_read(URLContext *h, UINT8 *buf, int size)
215{
216 VideoData *s = h->priv_data;
217 INT64 curtime;
218
219 if (size != h->packet_size)
220 return -EINVAL;
221
222 /* wait based on the frame rate */
223 s->time_frame += (int)(1000000 / s->rate);
224 do {
225 curtime = gettime();
226 } while (curtime < s->time_frame);
227
228 /* read one frame */
229 if (s->use_mmap) {
230 return v4l_mm_read_picture(h, buf);
231 } else {
232 if (read(s->fd, buf, size) != size)
233 return -EIO;
234 return h->packet_size;
235 }
236}
237
238static int video_get_format(URLContext *h, URLFormat *f)
239{
240 VideoData *s = h->priv_data;
241
242 f->width = s->width;
243 f->height = s->height;
244 f->frame_rate = (int)(s->rate * FRAME_RATE_BASE);
245 strcpy(f->format_name, "rawvideo");
246
247 switch(s->frame_format) {
248 case VIDEO_PALETTE_YUV420P:
249 f->pix_fmt = PIX_FMT_YUV420P;
250 break;
251 case VIDEO_PALETTE_YUV422:
252 f->pix_fmt = PIX_FMT_YUV422;
253 break;
254 case VIDEO_PALETTE_RGB24:
255 f->pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
256 break;
257 default:
258 abort();
259 }
260 return 0;
261}
262
263/* URI syntax: 'video:width,height,rate'
264 */
265static int video_open(URLContext *h, const char *uri, int flags)
266{
267 VideoData *s;
268 const char *p;
269 int width, height;
270 int ret;
271 float rate;
272
273 /* extract parameters */
274 p = uri;
275 strstart(p, "video:", &p);
276 width = strtol(p, (char **)&p, 0);
277 if (width <= 0)
278 return -EINVAL;
279 if (*p == ',')
280 p++;
281 height = strtol(p, (char **)&p, 0);
282 if (height <= 0)
283 return -EINVAL;
284 if (*p == ',')
285 p++;
286 rate = strtod(p, (char **)&p);
287 if (rate <= 0)
288 return -EINVAL;
289
290 s = malloc(sizeof(VideoData));
291 if (!s)
292 return -ENOMEM;
293 h->priv_data = s;
294 h->is_streamed = 1;
295 s->width = width;
296 s->height = height;
297 s->rate = rate;
298 ret = v4l_init(h);
299 if (ret)
300 free(s);
301 return ret;
302}
303
304static int video_close(URLContext *h)
305{
306 VideoData *s = h->priv_data;
307
308 close(s->fd);
309 free(s);
310 return 0;
311}
312
313URLProtocol video_protocol = {
314 "video",
315 video_open,
316 video_read,
317 NULL,
318 NULL, /* seek */
319 video_close,
320 video_get_format,
321};