Replace BUILD_{SHARED,STATIC} with CONFIG_ counterparts
[FFMpeg-mirror/ordered_chapters.git] / libavdevice / v4l2.c
blob940db7139488a0ac1cca37c48871bed25c56d086
1 /*
2 * Video4Linux2 grab interface
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2006 Luca Abeni
6 * Part of this file is based on the V4L2 video capture example
7 * (http://v4l2spec.bytesex.org/v4l2spec/capture.c)
9 * Thanks to Michael Niedermayer for providing the mapping between
10 * V4L2_PIX_FMT_* and PIX_FMT_*
13 * This file is part of FFmpeg.
15 * FFmpeg is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * FFmpeg is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with FFmpeg; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
31 #include "config.h"
32 #include "libavformat/avformat.h"
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/ioctl.h>
36 #include <sys/mman.h>
37 #include <sys/time.h>
38 #if HAVE_SYS_VIDEOIO_H
39 #include <sys/videoio.h>
40 #else
41 #include <asm/types.h>
42 #include <linux/videodev2.h>
43 #endif
44 #include <time.h>
45 #include <strings.h>
47 static const int desired_video_buffers = 256;
49 enum io_method {
50 io_read,
51 io_mmap,
52 io_userptr
55 struct video_data {
56 int fd;
57 int frame_format; /* V4L2_PIX_FMT_* */
58 enum io_method io_method;
59 int width, height;
60 int frame_size;
61 int top_field_first;
63 int buffers;
64 void **buf_start;
65 unsigned int *buf_len;
68 struct buff_data {
69 int index;
70 int fd;
73 struct fmt_map {
74 enum PixelFormat ff_fmt;
75 int32_t v4l2_fmt;
78 static struct fmt_map fmt_conversion_table[] = {
80 .ff_fmt = PIX_FMT_YUV420P,
81 .v4l2_fmt = V4L2_PIX_FMT_YUV420,
84 .ff_fmt = PIX_FMT_YUV422P,
85 .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
88 .ff_fmt = PIX_FMT_YUYV422,
89 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
92 .ff_fmt = PIX_FMT_UYVY422,
93 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
96 .ff_fmt = PIX_FMT_YUV411P,
97 .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
100 .ff_fmt = PIX_FMT_YUV410P,
101 .v4l2_fmt = V4L2_PIX_FMT_YUV410,
104 .ff_fmt = PIX_FMT_RGB555,
105 .v4l2_fmt = V4L2_PIX_FMT_RGB555,
108 .ff_fmt = PIX_FMT_RGB565,
109 .v4l2_fmt = V4L2_PIX_FMT_RGB565,
112 .ff_fmt = PIX_FMT_BGR24,
113 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
116 .ff_fmt = PIX_FMT_RGB24,
117 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
120 .ff_fmt = PIX_FMT_BGRA,
121 .v4l2_fmt = V4L2_PIX_FMT_BGR32,
124 .ff_fmt = PIX_FMT_GRAY8,
125 .v4l2_fmt = V4L2_PIX_FMT_GREY,
129 static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
131 struct v4l2_capability cap;
132 int fd;
133 int res;
134 int flags = O_RDWR;
136 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
137 flags |= O_NONBLOCK;
139 fd = open(ctx->filename, flags, 0);
140 if (fd < 0) {
141 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
142 ctx->filename, strerror(errno));
144 return -1;
147 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
148 // ENOIOCTLCMD definition only availble on __KERNEL__
149 if (res < 0 && errno == 515)
151 av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
152 close(fd);
154 return -1;
156 if (res < 0) {
157 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
158 strerror(errno));
159 close(fd);
161 return -1;
163 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
164 av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
165 close(fd);
167 return -1;
169 *capabilities = cap.capabilities;
171 return fd;
174 static int device_init(AVFormatContext *ctx, int *width, int *height, int pix_fmt)
176 struct video_data *s = ctx->priv_data;
177 int fd = s->fd;
178 struct v4l2_format fmt;
179 int res;
181 memset(&fmt, 0, sizeof(struct v4l2_format));
182 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
183 fmt.fmt.pix.width = *width;
184 fmt.fmt.pix.height = *height;
185 fmt.fmt.pix.pixelformat = pix_fmt;
186 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
187 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
188 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
189 av_log(ctx, AV_LOG_INFO, "The V4L2 driver changed the video from %dx%d to %dx%d\n", *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
190 *width = fmt.fmt.pix.width;
191 *height = fmt.fmt.pix.height;
194 if (pix_fmt != fmt.fmt.pix.pixelformat) {
195 av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver changed the pixel format from 0x%08X to 0x%08X\n", pix_fmt, fmt.fmt.pix.pixelformat);
196 res = -1;
199 return res;
202 static int first_field(int fd)
204 int res;
205 v4l2_std_id std;
207 res = ioctl(fd, VIDIOC_G_STD, &std);
208 if (res < 0) {
209 return 0;
211 if (std & V4L2_STD_NTSC) {
212 return 0;
215 return 1;
218 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
220 int i;
222 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
223 if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
224 return fmt_conversion_table[i].v4l2_fmt;
228 return 0;
231 static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
233 int i;
235 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
236 if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
237 return fmt_conversion_table[i].ff_fmt;
241 return PIX_FMT_NONE;
244 static int mmap_init(AVFormatContext *ctx)
246 struct video_data *s = ctx->priv_data;
247 struct v4l2_requestbuffers req;
248 int i, res;
250 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
251 req.count = desired_video_buffers;
252 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
253 req.memory = V4L2_MEMORY_MMAP;
254 res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
255 if (res < 0) {
256 if (errno == EINVAL) {
257 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
258 } else {
259 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
262 return -1;
265 if (req.count < 2) {
266 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
268 return -1;
270 s->buffers = req.count;
271 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
272 if (s->buf_start == NULL) {
273 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
275 return -1;
277 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
278 if (s->buf_len == NULL) {
279 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
280 av_free(s->buf_start);
282 return -1;
285 for (i = 0; i < req.count; i++) {
286 struct v4l2_buffer buf;
288 memset(&buf, 0, sizeof(struct v4l2_buffer));
289 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
290 buf.memory = V4L2_MEMORY_MMAP;
291 buf.index = i;
292 res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
293 if (res < 0) {
294 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
296 return -1;
299 s->buf_len[i] = buf.length;
300 if (s->buf_len[i] < s->frame_size) {
301 av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
303 return -1;
305 s->buf_start[i] = mmap (NULL, buf.length,
306 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
307 if (s->buf_start[i] == MAP_FAILED) {
308 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
310 return -1;
314 return 0;
317 static int read_init(AVFormatContext *ctx)
319 return -1;
322 static void mmap_release_buffer(AVPacket *pkt)
324 struct v4l2_buffer buf;
325 int res, fd;
326 struct buff_data *buf_descriptor = pkt->priv;
328 if (pkt->data == NULL) {
329 return;
332 memset(&buf, 0, sizeof(struct v4l2_buffer));
333 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
334 buf.memory = V4L2_MEMORY_MMAP;
335 buf.index = buf_descriptor->index;
336 fd = buf_descriptor->fd;
337 av_free(buf_descriptor);
339 res = ioctl (fd, VIDIOC_QBUF, &buf);
340 if (res < 0) {
341 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
343 pkt->data = NULL;
344 pkt->size = 0;
347 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
349 struct video_data *s = ctx->priv_data;
350 struct v4l2_buffer buf;
351 struct buff_data *buf_descriptor;
352 int res;
354 memset(&buf, 0, sizeof(struct v4l2_buffer));
355 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
356 buf.memory = V4L2_MEMORY_MMAP;
358 /* FIXME: Some special treatment might be needed in case of loss of signal... */
359 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
360 if (res < 0) {
361 if (errno == EAGAIN) {
362 pkt->size = 0;
364 return AVERROR(EAGAIN);
366 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
368 return -1;
370 assert (buf.index < s->buffers);
371 if (buf.bytesused != s->frame_size) {
372 av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
374 return -1;
377 /* Image is at s->buff_start[buf.index] */
378 pkt->data= s->buf_start[buf.index];
379 pkt->size = buf.bytesused;
380 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
381 pkt->destruct = mmap_release_buffer;
382 buf_descriptor = av_malloc(sizeof(struct buff_data));
383 if (buf_descriptor == NULL) {
384 /* Something went wrong... Since av_malloc() failed, we cannot even
385 * allocate a buffer for memcopying into it
387 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
388 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
390 return -1;
392 buf_descriptor->fd = s->fd;
393 buf_descriptor->index = buf.index;
394 pkt->priv = buf_descriptor;
396 return s->buf_len[buf.index];
399 static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
401 return -1;
404 static int mmap_start(AVFormatContext *ctx)
406 struct video_data *s = ctx->priv_data;
407 enum v4l2_buf_type type;
408 int i, res;
410 for (i = 0; i < s->buffers; i++) {
411 struct v4l2_buffer buf;
413 memset(&buf, 0, sizeof(struct v4l2_buffer));
414 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
415 buf.memory = V4L2_MEMORY_MMAP;
416 buf.index = i;
418 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
419 if (res < 0) {
420 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
422 return -1;
426 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
427 res = ioctl (s->fd, VIDIOC_STREAMON, &type);
428 if (res < 0) {
429 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
431 return -1;
434 return 0;
437 static void mmap_close(struct video_data *s)
439 enum v4l2_buf_type type;
440 int i;
442 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
443 /* We do not check for the result, because we could
444 * not do anything about it anyway...
446 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
447 for (i = 0; i < s->buffers; i++) {
448 munmap(s->buf_start[i], s->buf_len[i]);
450 av_free(s->buf_start);
451 av_free(s->buf_len);
454 static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
456 struct video_data *s = s1->priv_data;
457 struct v4l2_input input;
458 struct v4l2_standard standard;
459 int i;
461 if(ap->channel>=0) {
462 /* set tv video input */
463 memset (&input, 0, sizeof (input));
464 input.index = ap->channel;
465 if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
466 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
467 return AVERROR(EIO);
470 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
471 ap->channel, input.name);
472 if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
473 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
474 ap->channel);
475 return AVERROR(EIO);
479 if(ap->standard) {
480 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
481 ap->standard );
482 /* set tv standard */
483 memset (&standard, 0, sizeof (standard));
484 for(i=0;;i++) {
485 standard.index = i;
486 if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
487 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
488 ap->standard);
489 return AVERROR(EIO);
492 if(!strcasecmp(standard.name, ap->standard)) {
493 break;
497 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
498 ap->standard, (uint64_t)standard.id);
499 if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
500 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
501 ap->standard);
502 return AVERROR(EIO);
506 return 0;
509 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
511 struct video_data *s = s1->priv_data;
512 AVStream *st;
513 int width, height;
514 int res;
515 uint32_t desired_format, capabilities;
517 if (ap->width <= 0 || ap->height <= 0) {
518 av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
519 return -1;
522 width = ap->width;
523 height = ap->height;
525 if(avcodec_check_dimensions(s1, ap->width, ap->height) < 0)
526 return -1;
528 st = av_new_stream(s1, 0);
529 if (!st) {
530 return AVERROR(ENOMEM);
532 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
534 s->width = width;
535 s->height = height;
537 capabilities = 0;
538 s->fd = device_open(s1, &capabilities);
539 if (s->fd < 0) {
540 return AVERROR(EIO);
542 av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
544 desired_format = fmt_ff2v4l(ap->pix_fmt);
545 if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {
546 int i, done;
548 done = 0; i = 0;
549 while (!done) {
550 desired_format = fmt_conversion_table[i].v4l2_fmt;
551 if (device_init(s1, &width, &height, desired_format) < 0) {
552 desired_format = 0;
553 i++;
554 } else {
555 done = 1;
557 if (i == FF_ARRAY_ELEMS(fmt_conversion_table)) {
558 done = 1;
562 if (desired_format == 0) {
563 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
564 close(s->fd);
566 return AVERROR(EIO);
568 s->frame_format = desired_format;
570 if( v4l2_set_parameters( s1, ap ) < 0 )
571 return AVERROR(EIO);
573 st->codec->pix_fmt = fmt_v4l2ff(desired_format);
574 s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
575 if (capabilities & V4L2_CAP_STREAMING) {
576 s->io_method = io_mmap;
577 res = mmap_init(s1);
578 if (res == 0) {
579 res = mmap_start(s1);
581 } else {
582 s->io_method = io_read;
583 res = read_init(s1);
585 if (res < 0) {
586 close(s->fd);
588 return AVERROR(EIO);
590 s->top_field_first = first_field(s->fd);
592 st->codec->codec_type = CODEC_TYPE_VIDEO;
593 st->codec->codec_id = CODEC_ID_RAWVIDEO;
594 st->codec->width = width;
595 st->codec->height = height;
596 st->codec->time_base.den = ap->time_base.den;
597 st->codec->time_base.num = ap->time_base.num;
598 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
600 return 0;
603 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
605 struct video_data *s = s1->priv_data;
606 int res;
608 if (s->io_method == io_mmap) {
609 av_init_packet(pkt);
610 res = mmap_read_frame(s1, pkt);
611 } else if (s->io_method == io_read) {
612 if (av_new_packet(pkt, s->frame_size) < 0)
613 return AVERROR(EIO);
615 res = read_frame(s1, pkt);
616 } else {
617 return AVERROR(EIO);
619 if (res < 0) {
620 return res;
623 if (s1->streams[0]->codec->coded_frame) {
624 s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
625 s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
628 return s->frame_size;
631 static int v4l2_read_close(AVFormatContext *s1)
633 struct video_data *s = s1->priv_data;
635 if (s->io_method == io_mmap) {
636 mmap_close(s);
639 close(s->fd);
640 return 0;
643 AVInputFormat v4l2_demuxer = {
644 "video4linux2",
645 NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
646 sizeof(struct video_data),
647 NULL,
648 v4l2_read_header,
649 v4l2_read_packet,
650 v4l2_read_close,
651 .flags = AVFMT_NOFILE,