* more warning cleanups
[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
FB
55 int ret, frame_rate;
56
57 if (!ap || ap->width <= 0 || ap->height <= 0 || ap->frame_rate <= 0)
58 return -1;
de6d9b64 59
4972b26f
FB
60 width = ap->width;
61 height = ap->height;
62 frame_rate = ap->frame_rate;
63
64 s = av_mallocz(sizeof(VideoData));
65 if (!s)
66 return -ENOMEM;
67 st = av_mallocz(sizeof(AVStream));
68 if (!st) {
69 free(s);
70 return -ENOMEM;
71 }
72 s1->priv_data = s;
73 s1->nb_streams = 1;
74 s1->streams[0] = st;
75
76 s->width = width;
77 s->height = height;
78 s->frame_rate = frame_rate;
de6d9b64
FB
79
80 video_fd = open(v4l_device, O_RDWR);
81 if (video_fd < 0) {
82 perror(v4l_device);
4972b26f 83 goto fail;
de6d9b64
FB
84 }
85
86 if (ioctl(video_fd,VIDIOCGCAP,&video_cap) < 0) {
87 perror("VIDIOCGCAP");
88 goto fail;
89 }
90
91 if (!(video_cap.type & VID_TYPE_CAPTURE)) {
92 fprintf(stderr, "Fatal: grab device does not handle capture\n");
93 goto fail;
94 }
95
96 /* unmute audio */
97 ioctl(video_fd, VIDIOCGAUDIO, &audio);
5a56c87c 98 memcpy(&audio_saved, &audio, sizeof(audio));
de6d9b64
FB
99 audio.flags &= ~VIDEO_AUDIO_MUTE;
100 ioctl(video_fd, VIDIOCSAUDIO, &audio);
101
102 ret = ioctl(video_fd,VIDIOCGMBUF,&gb_buffers);
103 if (ret < 0) {
104 /* try to use read based access */
105 struct video_window win;
106 struct video_picture pict;
107 int val;
108
109 win.x = 0;
110 win.y = 0;
111 win.width = width;
112 win.height = height;
113 win.chromakey = -1;
114 win.flags = 0;
115
116 ioctl(video_fd, VIDIOCSWIN, &win);
117
118 ioctl(video_fd, VIDIOCGPICT, &pict);
119#if 0
120 printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
121 pict.colour,
122 pict.hue,
123 pict.brightness,
124 pict.contrast,
125 pict.whiteness);
126#endif
127 /* try to choose a suitable video format */
128 pict.palette=VIDEO_PALETTE_YUV420P;
129 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
130 if (ret < 0) {
131 pict.palette=VIDEO_PALETTE_YUV422;
132 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
133 if (ret < 0) {
134 pict.palette=VIDEO_PALETTE_RGB24;
135 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
136 if (ret < 0)
137 goto fail1;
138 }
139 }
140
141 s->frame_format = pict.palette;
142
143 val = 1;
144 ioctl(video_fd, VIDIOCCAPTURE, &val);
145
146 s->time_frame = gettime();
147 s->use_mmap = 0;
148 } else {
149 video_buf = mmap(0,gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
150 if ((unsigned char*)-1 == video_buf) {
151 perror("mmap");
152 goto fail;
153 }
154 gb_frame = 0;
155 s->time_frame = gettime();
156
157 /* start to grab the first frame */
5a56c87c 158 gb_buf.frame = (gb_frame + 1) % gb_buffers.frames;
de6d9b64
FB
159 gb_buf.height = height;
160 gb_buf.width = width;
161 gb_buf.format = VIDEO_PALETTE_YUV420P;
162
163 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
164 if (ret < 0 && errno != EAGAIN) {
165 /* try YUV422 */
166 gb_buf.format = VIDEO_PALETTE_YUV422;
167
168 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
169 if (ret < 0 && errno != EAGAIN) {
170 /* try RGB24 */
171 gb_buf.format = VIDEO_PALETTE_RGB24;
172 ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
173 }
174 }
175 if (ret < 0) {
176 if (errno != EAGAIN) {
177 fail1:
178 fprintf(stderr, "Fatal: grab device does not support suitable format\n");
179 } else {
180 fprintf(stderr,"Fatal: grab device does not receive any video signal\n");
181 }
182 goto fail;
183 }
184 s->frame_format = gb_buf.format;
185 s->use_mmap = 1;
186 }
187
188 switch(s->frame_format) {
189 case VIDEO_PALETTE_YUV420P:
190 frame_size = (width * height * 3) / 2;
4972b26f 191 st->codec.pix_fmt = PIX_FMT_YUV420P;
de6d9b64
FB
192 break;
193 case VIDEO_PALETTE_YUV422:
194 frame_size = width * height * 2;
4972b26f 195 st->codec.pix_fmt = PIX_FMT_YUV422;
de6d9b64
FB
196 break;
197 case VIDEO_PALETTE_RGB24:
198 frame_size = width * height * 3;
4972b26f 199 st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
de6d9b64
FB
200 break;
201 default:
202 goto fail;
203 }
204 s->fd = video_fd;
4972b26f
FB
205 s->frame_size = frame_size;
206
207 st->codec.codec_id = CODEC_ID_RAWVIDEO;
208 st->codec.width = width;
209 st->codec.height = height;
210 st->codec.frame_rate = frame_rate;
211
de6d9b64
FB
212 return 0;
213 fail:
4972b26f
FB
214 if (video_fd >= 0)
215 close(video_fd);
216 free(st);
217 free(s);
de6d9b64
FB
218 return -EIO;
219}
220
4972b26f 221static int v4l_mm_read_picture(VideoData *s, UINT8 *buf)
de6d9b64 222{
de6d9b64
FB
223 UINT8 *ptr;
224
225 gb_buf.frame = gb_frame;
226 if (ioctl(s->fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
227 if (errno == EAGAIN)
228 fprintf(stderr,"Cannot Sync\n");
229 else
230 perror("VIDIOCMCAPTURE");
231 return -EIO;
232 }
5a56c87c 233 gb_frame = (gb_frame + 1) % gb_buffers.frames;
de6d9b64
FB
234
235 while (ioctl(s->fd, VIDIOCSYNC, &gb_frame) < 0 &&
236 (errno == EAGAIN || errno == EINTR));
237
238 ptr = video_buf + gb_buffers.offsets[gb_frame];
4972b26f
FB
239 memcpy(buf, ptr, s->frame_size);
240 return s->frame_size;
de6d9b64
FB
241}
242
4972b26f 243static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
de6d9b64 244{
4972b26f
FB
245 VideoData *s = s1->priv_data;
246 INT64 curtime, delay;
247 struct timespec ts;
de6d9b64
FB
248
249 /* wait based on the frame rate */
4972b26f
FB
250 s->time_frame += (INT64_C(1000000) * FRAME_RATE_BASE) / s->frame_rate;
251 for(;;) {
de6d9b64 252 curtime = gettime();
4972b26f
FB
253 delay = s->time_frame - curtime;
254 if (delay <= 0)
255 break;
256 ts.tv_sec = delay / 1000000;
257 ts.tv_nsec = (delay % 1000000) * 1000;
258 nanosleep(&ts, NULL);
259 }
260
261 if (av_new_packet(pkt, s->frame_size) < 0)
262 return -EIO;
de6d9b64
FB
263
264 /* read one frame */
265 if (s->use_mmap) {
4972b26f 266 return v4l_mm_read_picture(s, pkt->data);
de6d9b64 267 } else {
4972b26f 268 if (read(s->fd, pkt->data, pkt->size) != pkt->size)
de6d9b64 269 return -EIO;
4972b26f 270 return s->frame_size;
de6d9b64
FB
271 }
272}
273
4972b26f 274static int grab_read_close(AVFormatContext *s1)
de6d9b64 275{
4972b26f 276 VideoData *s = s1->priv_data;
5a56c87c
FB
277 /* restore audio settings */
278 ioctl(s->fd, VIDIOCSAUDIO, &audio_saved);
279
de6d9b64
FB
280 close(s->fd);
281 free(s);
282 return 0;
283}
284
4972b26f
FB
285AVFormat video_grab_device_format = {
286 "video_grab_device",
287 "video grab",
288 "",
289 "",
290 CODEC_ID_NONE,
291 CODEC_ID_NONE,
292 NULL,
293 NULL,
294 NULL,
295
296 grab_read_header,
297 grab_read_packet,
298 grab_read_close,
de6d9b64 299 NULL,
4972b26f 300 AVFMT_NOFILE,
de6d9b64 301};