Fix previously broken x86_64 check, simplifying it in the process.
[libav.git] / libavdevice / v4l.c
CommitLineData
de6d9b64
FB
1/*
2 * Linux video grab interface
19720f15 3 * Copyright (c) 2000,2001 Fabrice Bellard.
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
8be1c656 21#include "avformat.h"
c64bab08 22#include "dsputil.h"
de6d9b64
FB
23#include <unistd.h>
24#include <fcntl.h>
25#include <sys/ioctl.h>
26#include <sys/mman.h>
de6d9b64 27#include <sys/time.h>
72a476c1
MN
28#define _LINUX_TIME_H 1
29#include <linux/videodev.h>
608d0dee 30#include <time.h>
de6d9b64
FB
31
32typedef struct {
33 int fd;
34 int frame_format; /* see VIDEO_PALETTE_xxx */
35 int use_mmap;
36 int width, height;
4972b26f 37 int frame_rate;
14bea432 38 int frame_rate_base;
0c1a9eda 39 int64_t time_frame;
4972b26f 40 int frame_size;
8949367e
FB
41 struct video_capability video_cap;
42 struct video_audio audio_saved;
0c1a9eda 43 uint8_t *video_buf;
8949367e
FB
44 struct video_mbuf gb_buffers;
45 struct video_mmap gb_buf;
46 int gb_frame;
de6d9b64
FB
47} VideoData;
48
1411f55b 49static const struct {
c7c64e9e
MH
50 int palette;
51 int depth;
52 enum PixelFormat pix_fmt;
53} video_formats [] = {
54 {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
55 {.palette = VIDEO_PALETTE_YUV422, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
69621690
MH
56 {.palette = VIDEO_PALETTE_UYVY, .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
57 {.palette = VIDEO_PALETTE_YUYV, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
c7c64e9e
MH
58 /* NOTE: v4l uses BGR24, not RGB24 */
59 {.palette = VIDEO_PALETTE_RGB24, .depth = 24, .pix_fmt = PIX_FMT_BGR24 },
69621690 60 {.palette = VIDEO_PALETTE_RGB565, .depth = 16, .pix_fmt = PIX_FMT_BGR565 },
c7c64e9e
MH
61 {.palette = VIDEO_PALETTE_GREY, .depth = 8, .pix_fmt = PIX_FMT_GRAY8 },
62};
63
64
4972b26f 65static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
de6d9b64 66{
c9a65ca8 67 VideoData *s = s1->priv_data;
4972b26f 68 AVStream *st;
de6d9b64 69 int width, height;
de6d9b64 70 int video_fd, frame_size;
14bea432 71 int ret, frame_rate, frame_rate_base;
76a77ff2 72 int desired_palette, desired_depth;
e3ee3283 73 struct video_tuner tuner;
8949367e 74 struct video_audio audio;
c787ba85 75 struct video_picture pict;
7aae3168 76 int j;
c7c64e9e 77 int vformat_num = sizeof(video_formats) / sizeof(video_formats[0]);
4972b26f 78
11b9c0f4
LA
79 if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
80 av_log(s1, AV_LOG_ERROR, "Bad capture size (%dx%d) or wrong time base (%d)\n",
81 ap->width, ap->height, ap->time_base.den);
82
4972b26f 83 return -1;
11b9c0f4 84 }
115329f1 85
4972b26f
FB
86 width = ap->width;
87 height = ap->height;
c0df9d75
MN
88 frame_rate = ap->time_base.den;
89 frame_rate_base = ap->time_base.num;
4972b26f 90
11b9c0f4
LA
91 if((unsigned)width > 32767 || (unsigned)height > 32767) {
92 av_log(s1, AV_LOG_ERROR, "Capture size is out of range: %dx%d\n",
93 width, height);
94
568e18b1 95 return -1;
11b9c0f4 96 }
115329f1 97
c9a65ca8
FB
98 st = av_new_stream(s1, 0);
99 if (!st)
8fa36ae0 100 return AVERROR(ENOMEM);
0a7b514f 101 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
4972b26f
FB
102
103 s->width = width;
104 s->height = height;
14bea432
MN
105 s->frame_rate = frame_rate;
106 s->frame_rate_base = frame_rate_base;
de6d9b64 107
cc58300e 108 video_fd = open(s1->filename, O_RDWR);
de6d9b64 109 if (video_fd < 0) {
9f74582c 110 av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
4972b26f 111 goto fail;
de6d9b64 112 }
115329f1 113
8949367e 114 if (ioctl(video_fd,VIDIOCGCAP, &s->video_cap) < 0) {
9f74582c 115 av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
de6d9b64
FB
116 goto fail;
117 }
118
8949367e 119 if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
bb270c08 120 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
de6d9b64
FB
121 goto fail;
122 }
4606ac8d
ZK
123
124 desired_palette = -1;
76a77ff2 125 desired_depth = -1;
c7c64e9e
MH
126 for (j = 0; j < vformat_num; j++) {
127 if (ap->pix_fmt == video_formats[j].pix_fmt) {
128 desired_palette = video_formats[j].palette;
129 desired_depth = video_formats[j].depth;
130 break;
131 }
115329f1 132 }
e3ee3283
AB
133
134 /* set tv standard */
135 if (ap->standard && !ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
bb270c08
DB
136 if (!strcasecmp(ap->standard, "pal"))
137 tuner.mode = VIDEO_MODE_PAL;
138 else if (!strcasecmp(ap->standard, "secam"))
139 tuner.mode = VIDEO_MODE_SECAM;
140 else
141 tuner.mode = VIDEO_MODE_NTSC;
142 ioctl(video_fd, VIDIOCSTUNER, &tuner);
e3ee3283 143 }
115329f1 144
de6d9b64 145 /* unmute audio */
8949367e 146 audio.audio = 0;
de6d9b64 147 ioctl(video_fd, VIDIOCGAUDIO, &audio);
8949367e 148 memcpy(&s->audio_saved, &audio, sizeof(audio));
de6d9b64
FB
149 audio.flags &= ~VIDEO_AUDIO_MUTE;
150 ioctl(video_fd, VIDIOCSAUDIO, &audio);
151
c787ba85
FPD
152 ioctl(video_fd, VIDIOCGPICT, &pict);
153#if 0
154 printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
155 pict.colour,
156 pict.hue,
157 pict.brightness,
158 pict.contrast,
159 pict.whiteness);
160#endif
161 /* try to choose a suitable video format */
162 pict.palette = desired_palette;
76a77ff2 163 pict.depth= desired_depth;
c787ba85 164 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
c7c64e9e
MH
165 for (j = 0; j < vformat_num; j++) {
166 pict.palette = video_formats[j].palette;
167 pict.depth = video_formats[j].depth;
168 if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
169 break;
c787ba85 170 }
c7c64e9e
MH
171 if (j >= vformat_num)
172 goto fail1;
c787ba85
FPD
173 }
174
8949367e 175 ret = ioctl(video_fd,VIDIOCGMBUF,&s->gb_buffers);
de6d9b64
FB
176 if (ret < 0) {
177 /* try to use read based access */
178 struct video_window win;
de6d9b64
FB
179 int val;
180
181 win.x = 0;
182 win.y = 0;
183 win.width = width;
184 win.height = height;
185 win.chromakey = -1;
186 win.flags = 0;
187
188 ioctl(video_fd, VIDIOCSWIN, &win);
189
de6d9b64
FB
190 s->frame_format = pict.palette;
191
192 val = 1;
193 ioctl(video_fd, VIDIOCCAPTURE, &val);
194
3834be58 195 s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
de6d9b64
FB
196 s->use_mmap = 0;
197 } else {
8949367e
FB
198 s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
199 if ((unsigned char*)-1 == s->video_buf) {
d037d797
MF
200 s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_PRIVATE,video_fd,0);
201 if ((unsigned char*)-1 == s->video_buf) {
9f74582c 202 av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
d037d797
MF
203 goto fail;
204 }
de6d9b64 205 }
8949367e 206 s->gb_frame = 0;
3834be58 207 s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
115329f1 208
de6d9b64 209 /* start to grab the first frame */
8949367e
FB
210 s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
211 s->gb_buf.height = height;
212 s->gb_buf.width = width;
c787ba85
FPD
213 s->gb_buf.format = pict.palette;
214
215 ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
de6d9b64
FB
216 if (ret < 0) {
217 if (errno != EAGAIN) {
218 fail1:
bc874dae 219 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not support suitable format\n");
de6d9b64 220 } else {
bc874dae 221 av_log(s1, AV_LOG_ERROR,"Fatal: grab device does not receive any video signal\n");
de6d9b64
FB
222 }
223 goto fail;
224 }
bb270c08
DB
225 for (j = 1; j < s->gb_buffers.frames; j++) {
226 s->gb_buf.frame = j;
227 ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
228 }
8949367e 229 s->frame_format = s->gb_buf.format;
de6d9b64
FB
230 s->use_mmap = 1;
231 }
232
c7c64e9e
MH
233 for (j = 0; j < vformat_num; j++) {
234 if (s->frame_format == video_formats[j].palette) {
235 frame_size = width * height * video_formats[j].depth / 8;
236 st->codec->pix_fmt = video_formats[j].pix_fmt;
237 break;
238 }
de6d9b64 239 }
c7c64e9e
MH
240
241 if (j >= vformat_num)
242 goto fail;
243
de6d9b64 244 s->fd = video_fd;
4972b26f 245 s->frame_size = frame_size;
115329f1 246
01f4895c
MN
247 st->codec->codec_type = CODEC_TYPE_VIDEO;
248 st->codec->codec_id = CODEC_ID_RAWVIDEO;
249 st->codec->width = width;
250 st->codec->height = height;
251 st->codec->time_base.den = frame_rate;
252 st->codec->time_base.num = frame_rate_base;
4209fad9 253 st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
45dd5c69 254
de6d9b64
FB
255 return 0;
256 fail:
4972b26f
FB
257 if (video_fd >= 0)
258 close(video_fd);
1ea4f593 259 av_free(st);
6f3e0b21 260 return AVERROR(EIO);
de6d9b64
FB
261}
262
0c1a9eda 263static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
de6d9b64 264{
0c1a9eda 265 uint8_t *ptr;
de6d9b64 266
7aae3168
MN
267 while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
268 (errno == EAGAIN || errno == EINTR));
269
270 ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
271 memcpy(buf, ptr, s->frame_size);
272
4606ac8d 273 /* Setup to capture the next frame */
7aae3168 274 s->gb_buf.frame = s->gb_frame;
8949367e 275 if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
28c66901 276 if (errno == EAGAIN)
bc874dae 277 av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
28c66901 278 else
9f74582c 279 av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
6f3e0b21 280 return AVERROR(EIO);
de6d9b64 281 }
4606ac8d 282
4606ac8d 283 /* This is now the grabbing frame */
7aae3168 284 s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
4606ac8d 285
4972b26f 286 return s->frame_size;
de6d9b64
FB
287}
288
4972b26f 289static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
de6d9b64 290{
4972b26f 291 VideoData *s = s1->priv_data;
0c1a9eda 292 int64_t curtime, delay;
4972b26f 293 struct timespec ts;
4606ac8d
ZK
294
295 /* Calculate the time of the next frame */
8da9266c 296 s->time_frame += INT64_C(1000000);
de6d9b64
FB
297
298 /* wait based on the frame rate */
45dd5c69 299 for(;;) {
a11bf0bd 300 curtime = av_gettime();
3834be58 301 delay = s->time_frame * s->frame_rate_base / s->frame_rate - curtime;
4606ac8d 302 if (delay <= 0) {
8da9266c 303 if (delay < INT64_C(-1000000) * s->frame_rate_base / s->frame_rate) {
4606ac8d 304 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
8da9266c 305 s->time_frame += INT64_C(1000000);
4606ac8d 306 }
4972b26f 307 break;
115329f1 308 }
4972b26f
FB
309 ts.tv_sec = delay / 1000000;
310 ts.tv_nsec = (delay % 1000000) * 1000;
311 nanosleep(&ts, NULL);
312 }
313
314 if (av_new_packet(pkt, s->frame_size) < 0)
6f3e0b21 315 return AVERROR(EIO);
de6d9b64 316
0a7b514f 317 pkt->pts = curtime;
45dd5c69 318
de6d9b64 319 /* read one frame */
3ab64e46 320 if (s->use_mmap) {
4972b26f 321 return v4l_mm_read_picture(s, pkt->data);
de6d9b64 322 } else {
4972b26f 323 if (read(s->fd, pkt->data, pkt->size) != pkt->size)
6f3e0b21 324 return AVERROR(EIO);
4972b26f 325 return s->frame_size;
de6d9b64
FB
326 }
327}
328
4972b26f 329static int grab_read_close(AVFormatContext *s1)
de6d9b64 330{
4972b26f 331 VideoData *s = s1->priv_data;
e61efa24
LB
332
333 if (s->use_mmap)
8949367e 334 munmap(s->video_buf, s->gb_buffers.size);
e61efa24 335
8949367e
FB
336 /* mute audio. we must force it because the BTTV driver does not
337 return its state correctly */
338 s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
339 ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
5a56c87c 340
de6d9b64 341 close(s->fd);
de6d9b64
FB
342 return 0;
343}
344
fd484da7 345AVInputFormat v4l_demuxer = {
8aa3ee32 346 "video4linux",
4972b26f 347 "video grab",
c9a65ca8 348 sizeof(VideoData),
4972b26f 349 NULL,
4972b26f
FB
350 grab_read_header,
351 grab_read_packet,
352 grab_read_close,
bb76a117 353 .flags = AVFMT_NOFILE,
de6d9b64 354};