Use tables symetry to reduce their size by half.
[ffmpeg-lucabe.git] / libavdevice / v4l.c
blobb58de9473876c611632aa7597b0bb94e52c52b04
1 /*
2 * Linux video grab interface
3 * Copyright (c) 2000,2001 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
23 #include "config.h"
24 #include "libavformat/avformat.h"
25 #include "libavcodec/dsputil.h"
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29 #include <sys/mman.h>
30 #include <sys/time.h>
31 #define _LINUX_TIME_H 1
32 #include <linux/videodev.h>
33 #include <time.h>
34 #include <strings.h>
36 typedef struct {
37 int fd;
38 int frame_format; /* see VIDEO_PALETTE_xxx */
39 int use_mmap;
40 int width, height;
41 int frame_rate;
42 int frame_rate_base;
43 int64_t time_frame;
44 int frame_size;
45 struct video_capability video_cap;
46 struct video_audio audio_saved;
47 uint8_t *video_buf;
48 struct video_mbuf gb_buffers;
49 struct video_mmap gb_buf;
50 int gb_frame;
51 } VideoData;
53 static const struct {
54 int palette;
55 int depth;
56 enum PixelFormat pix_fmt;
57 } video_formats [] = {
58 {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
59 {.palette = VIDEO_PALETTE_YUV422, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
60 {.palette = VIDEO_PALETTE_UYVY, .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
61 {.palette = VIDEO_PALETTE_YUYV, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
62 /* NOTE: v4l uses BGR24, not RGB24 */
63 {.palette = VIDEO_PALETTE_RGB24, .depth = 24, .pix_fmt = PIX_FMT_BGR24 },
64 {.palette = VIDEO_PALETTE_RGB565, .depth = 16, .pix_fmt = PIX_FMT_BGR565 },
65 {.palette = VIDEO_PALETTE_GREY, .depth = 8, .pix_fmt = PIX_FMT_GRAY8 },
69 static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
71 VideoData *s = s1->priv_data;
72 AVStream *st;
73 int width, height;
74 int video_fd, frame_size;
75 int ret, frame_rate, frame_rate_base;
76 int desired_palette, desired_depth;
77 struct video_tuner tuner;
78 struct video_audio audio;
79 struct video_picture pict;
80 int j;
81 int vformat_num = sizeof(video_formats) / sizeof(video_formats[0]);
83 if (ap->width <= 0 || ap->height <= 0) {
84 av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
85 return -1;
87 if (ap->time_base.den <= 0) {
88 av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
89 return -1;
92 width = ap->width;
93 height = ap->height;
94 frame_rate = ap->time_base.den;
95 frame_rate_base = ap->time_base.num;
97 if((unsigned)width > 32767 || (unsigned)height > 32767) {
98 av_log(s1, AV_LOG_ERROR, "Capture size is out of range: %dx%d\n",
99 width, height);
101 return -1;
104 st = av_new_stream(s1, 0);
105 if (!st)
106 return AVERROR(ENOMEM);
107 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
109 s->width = width;
110 s->height = height;
111 s->frame_rate = frame_rate;
112 s->frame_rate_base = frame_rate_base;
114 video_fd = open(s1->filename, O_RDWR);
115 if (video_fd < 0) {
116 av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
117 goto fail;
120 if (ioctl(video_fd,VIDIOCGCAP, &s->video_cap) < 0) {
121 av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
122 goto fail;
125 if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
126 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
127 goto fail;
130 desired_palette = -1;
131 desired_depth = -1;
132 for (j = 0; j < vformat_num; j++) {
133 if (ap->pix_fmt == video_formats[j].pix_fmt) {
134 desired_palette = video_formats[j].palette;
135 desired_depth = video_formats[j].depth;
136 break;
140 /* set tv standard */
141 if (ap->standard && !ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
142 if (!strcasecmp(ap->standard, "pal"))
143 tuner.mode = VIDEO_MODE_PAL;
144 else if (!strcasecmp(ap->standard, "secam"))
145 tuner.mode = VIDEO_MODE_SECAM;
146 else
147 tuner.mode = VIDEO_MODE_NTSC;
148 ioctl(video_fd, VIDIOCSTUNER, &tuner);
151 /* unmute audio */
152 audio.audio = 0;
153 ioctl(video_fd, VIDIOCGAUDIO, &audio);
154 memcpy(&s->audio_saved, &audio, sizeof(audio));
155 audio.flags &= ~VIDEO_AUDIO_MUTE;
156 ioctl(video_fd, VIDIOCSAUDIO, &audio);
158 ioctl(video_fd, VIDIOCGPICT, &pict);
159 #if 0
160 printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
161 pict.colour,
162 pict.hue,
163 pict.brightness,
164 pict.contrast,
165 pict.whiteness);
166 #endif
167 /* try to choose a suitable video format */
168 pict.palette = desired_palette;
169 pict.depth= desired_depth;
170 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
171 for (j = 0; j < vformat_num; j++) {
172 pict.palette = video_formats[j].palette;
173 pict.depth = video_formats[j].depth;
174 if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
175 break;
177 if (j >= vformat_num)
178 goto fail1;
181 ret = ioctl(video_fd,VIDIOCGMBUF,&s->gb_buffers);
182 if (ret < 0) {
183 /* try to use read based access */
184 struct video_window win;
185 int val;
187 win.x = 0;
188 win.y = 0;
189 win.width = width;
190 win.height = height;
191 win.chromakey = -1;
192 win.flags = 0;
194 ioctl(video_fd, VIDIOCSWIN, &win);
196 s->frame_format = pict.palette;
198 val = 1;
199 ioctl(video_fd, VIDIOCCAPTURE, &val);
201 s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
202 s->use_mmap = 0;
203 } else {
204 s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
205 if ((unsigned char*)-1 == s->video_buf) {
206 s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_PRIVATE,video_fd,0);
207 if ((unsigned char*)-1 == s->video_buf) {
208 av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
209 goto fail;
212 s->gb_frame = 0;
213 s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
215 /* start to grab the first frame */
216 s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
217 s->gb_buf.height = height;
218 s->gb_buf.width = width;
219 s->gb_buf.format = pict.palette;
221 ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
222 if (ret < 0) {
223 if (errno != EAGAIN) {
224 fail1:
225 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not support suitable format\n");
226 } else {
227 av_log(s1, AV_LOG_ERROR,"Fatal: grab device does not receive any video signal\n");
229 goto fail;
231 for (j = 1; j < s->gb_buffers.frames; j++) {
232 s->gb_buf.frame = j;
233 ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
235 s->frame_format = s->gb_buf.format;
236 s->use_mmap = 1;
239 for (j = 0; j < vformat_num; j++) {
240 if (s->frame_format == video_formats[j].palette) {
241 frame_size = width * height * video_formats[j].depth / 8;
242 st->codec->pix_fmt = video_formats[j].pix_fmt;
243 break;
247 if (j >= vformat_num)
248 goto fail;
250 s->fd = video_fd;
251 s->frame_size = frame_size;
253 st->codec->codec_type = CODEC_TYPE_VIDEO;
254 st->codec->codec_id = CODEC_ID_RAWVIDEO;
255 st->codec->width = width;
256 st->codec->height = height;
257 st->codec->time_base.den = frame_rate;
258 st->codec->time_base.num = frame_rate_base;
259 st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
261 return 0;
262 fail:
263 if (video_fd >= 0)
264 close(video_fd);
265 return AVERROR(EIO);
268 static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
270 uint8_t *ptr;
272 while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
273 (errno == EAGAIN || errno == EINTR));
275 ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
276 memcpy(buf, ptr, s->frame_size);
278 /* Setup to capture the next frame */
279 s->gb_buf.frame = s->gb_frame;
280 if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
281 if (errno == EAGAIN)
282 av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
283 else
284 av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
285 return AVERROR(EIO);
288 /* This is now the grabbing frame */
289 s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
291 return s->frame_size;
294 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
296 VideoData *s = s1->priv_data;
297 int64_t curtime, delay;
298 struct timespec ts;
300 /* Calculate the time of the next frame */
301 s->time_frame += INT64_C(1000000);
303 /* wait based on the frame rate */
304 for(;;) {
305 curtime = av_gettime();
306 delay = s->time_frame * s->frame_rate_base / s->frame_rate - curtime;
307 if (delay <= 0) {
308 if (delay < INT64_C(-1000000) * s->frame_rate_base / s->frame_rate) {
309 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
310 s->time_frame += INT64_C(1000000);
312 break;
314 ts.tv_sec = delay / 1000000;
315 ts.tv_nsec = (delay % 1000000) * 1000;
316 nanosleep(&ts, NULL);
319 if (av_new_packet(pkt, s->frame_size) < 0)
320 return AVERROR(EIO);
322 pkt->pts = curtime;
324 /* read one frame */
325 if (s->use_mmap) {
326 return v4l_mm_read_picture(s, pkt->data);
327 } else {
328 if (read(s->fd, pkt->data, pkt->size) != pkt->size)
329 return AVERROR(EIO);
330 return s->frame_size;
334 static int grab_read_close(AVFormatContext *s1)
336 VideoData *s = s1->priv_data;
338 if (s->use_mmap)
339 munmap(s->video_buf, s->gb_buffers.size);
341 /* mute audio. we must force it because the BTTV driver does not
342 return its state correctly */
343 s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
344 ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
346 close(s->fd);
347 return 0;
350 AVInputFormat v4l_demuxer = {
351 "video4linux",
352 NULL_IF_CONFIG_SMALL("video grab"),
353 sizeof(VideoData),
354 NULL,
355 grab_read_header,
356 grab_read_packet,
357 grab_read_close,
358 .flags = AVFMT_NOFILE,