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
32 #include "libavformat/avformat.h"
35 #include <sys/ioctl.h>
38 #if HAVE_SYS_VIDEOIO_H
39 #include <sys/videoio.h>
41 #include <asm/types.h>
42 #include <linux/videodev2.h>
46 #include "libavcore/imgutils.h"
48 static const int desired_video_buffers = 256;
58 int frame_format; /* V4L2_PIX_FMT_* */
59 enum io_method io_method;
66 unsigned int *buf_len;
75 enum PixelFormat ff_fmt;
76 enum CodecID codec_id;
80 static struct fmt_map fmt_conversion_table[] = {
82 .ff_fmt = PIX_FMT_YUV420P,
83 .codec_id = CODEC_ID_RAWVIDEO,
84 .v4l2_fmt = V4L2_PIX_FMT_YUV420,
87 .ff_fmt = PIX_FMT_YUV422P,
88 .codec_id = CODEC_ID_RAWVIDEO,
89 .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
92 .ff_fmt = PIX_FMT_YUYV422,
93 .codec_id = CODEC_ID_RAWVIDEO,
94 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
97 .ff_fmt = PIX_FMT_UYVY422,
98 .codec_id = CODEC_ID_RAWVIDEO,
99 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
102 .ff_fmt = PIX_FMT_YUV411P,
103 .codec_id = CODEC_ID_RAWVIDEO,
104 .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
107 .ff_fmt = PIX_FMT_YUV410P,
108 .codec_id = CODEC_ID_RAWVIDEO,
109 .v4l2_fmt = V4L2_PIX_FMT_YUV410,
112 .ff_fmt = PIX_FMT_RGB555,
113 .codec_id = CODEC_ID_RAWVIDEO,
114 .v4l2_fmt = V4L2_PIX_FMT_RGB555,
117 .ff_fmt = PIX_FMT_RGB565,
118 .codec_id = CODEC_ID_RAWVIDEO,
119 .v4l2_fmt = V4L2_PIX_FMT_RGB565,
122 .ff_fmt = PIX_FMT_BGR24,
123 .codec_id = CODEC_ID_RAWVIDEO,
124 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
127 .ff_fmt = PIX_FMT_RGB24,
128 .codec_id = CODEC_ID_RAWVIDEO,
129 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
132 .ff_fmt = PIX_FMT_BGRA,
133 .codec_id = CODEC_ID_RAWVIDEO,
134 .v4l2_fmt = V4L2_PIX_FMT_BGR32,
137 .ff_fmt = PIX_FMT_GRAY8,
138 .codec_id = CODEC_ID_RAWVIDEO,
139 .v4l2_fmt = V4L2_PIX_FMT_GREY,
142 .ff_fmt = PIX_FMT_NV12,
143 .codec_id = CODEC_ID_RAWVIDEO,
144 .v4l2_fmt = V4L2_PIX_FMT_NV12,
147 .ff_fmt = PIX_FMT_NONE,
148 .codec_id = CODEC_ID_MJPEG,
149 .v4l2_fmt = V4L2_PIX_FMT_MJPEG,
152 .ff_fmt = PIX_FMT_NONE,
153 .codec_id = CODEC_ID_MJPEG,
154 .v4l2_fmt = V4L2_PIX_FMT_JPEG,
158 static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
160 struct v4l2_capability cap;
165 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
168 fd = open(ctx->filename, flags, 0);
170 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
171 ctx->filename, strerror(errno));
173 return AVERROR(errno);
176 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
177 // ENOIOCTLCMD definition only availble on __KERNEL__
178 if (res < 0 && ((err = errno) == 515)) {
179 av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
185 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
191 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
192 av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
195 return AVERROR(ENODEV);
197 *capabilities = cap.capabilities;
202 static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
204 struct video_data *s = ctx->priv_data;
206 struct v4l2_format fmt;
209 memset(&fmt, 0, sizeof(struct v4l2_format));
210 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
211 fmt.fmt.pix.width = *width;
212 fmt.fmt.pix.height = *height;
213 fmt.fmt.pix.pixelformat = pix_fmt;
214 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
215 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
216 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
217 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);
218 *width = fmt.fmt.pix.width;
219 *height = fmt.fmt.pix.height;
222 if (pix_fmt != fmt.fmt.pix.pixelformat) {
223 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);
230 static int first_field(int fd)
235 res = ioctl(fd, VIDIOC_G_STD, &std);
239 if (std & V4L2_STD_NTSC) {
246 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt, enum CodecID codec_id)
250 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
251 if ((codec_id == CODEC_ID_NONE ||
252 fmt_conversion_table[i].codec_id == codec_id) &&
253 (pix_fmt == PIX_FMT_NONE ||
254 fmt_conversion_table[i].ff_fmt == pix_fmt)) {
255 return fmt_conversion_table[i].v4l2_fmt;
262 static enum PixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum CodecID codec_id)
266 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
267 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
268 fmt_conversion_table[i].codec_id == codec_id) {
269 return fmt_conversion_table[i].ff_fmt;
276 static enum CodecID fmt_v4l2codec(uint32_t v4l2_fmt)
280 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
281 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
282 return fmt_conversion_table[i].codec_id;
286 return CODEC_ID_NONE;
289 static int mmap_init(AVFormatContext *ctx)
291 struct video_data *s = ctx->priv_data;
292 struct v4l2_requestbuffers req;
295 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
296 req.count = desired_video_buffers;
297 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
298 req.memory = V4L2_MEMORY_MMAP;
299 res = ioctl(s->fd, VIDIOC_REQBUFS, &req);
301 if (errno == EINVAL) {
302 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
304 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
307 return AVERROR(errno);
311 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
313 return AVERROR(ENOMEM);
315 s->buffers = req.count;
316 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
317 if (s->buf_start == NULL) {
318 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
320 return AVERROR(ENOMEM);
322 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
323 if (s->buf_len == NULL) {
324 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
325 av_free(s->buf_start);
327 return AVERROR(ENOMEM);
330 for (i = 0; i < req.count; i++) {
331 struct v4l2_buffer buf;
333 memset(&buf, 0, sizeof(struct v4l2_buffer));
334 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
335 buf.memory = V4L2_MEMORY_MMAP;
337 res = ioctl(s->fd, VIDIOC_QUERYBUF, &buf);
339 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
341 return AVERROR(errno);
344 s->buf_len[i] = buf.length;
345 if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
346 av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
350 s->buf_start[i] = mmap (NULL, buf.length,
351 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
352 if (s->buf_start[i] == MAP_FAILED) {
353 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
355 return AVERROR(errno);
362 static int read_init(AVFormatContext *ctx)
367 static void mmap_release_buffer(AVPacket *pkt)
369 struct v4l2_buffer buf;
371 struct buff_data *buf_descriptor = pkt->priv;
373 if (pkt->data == NULL) {
377 memset(&buf, 0, sizeof(struct v4l2_buffer));
378 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
379 buf.memory = V4L2_MEMORY_MMAP;
380 buf.index = buf_descriptor->index;
381 fd = buf_descriptor->fd;
382 av_free(buf_descriptor);
384 res = ioctl(fd, VIDIOC_QBUF, &buf);
386 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
392 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
394 struct video_data *s = ctx->priv_data;
395 struct v4l2_buffer buf;
396 struct buff_data *buf_descriptor;
399 memset(&buf, 0, sizeof(struct v4l2_buffer));
400 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
401 buf.memory = V4L2_MEMORY_MMAP;
403 /* FIXME: Some special treatment might be needed in case of loss of signal... */
404 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
406 if (errno == EAGAIN) {
409 return AVERROR(EAGAIN);
411 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
413 return AVERROR(errno);
415 assert (buf.index < s->buffers);
416 if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
417 av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
419 return AVERROR_INVALIDDATA;
422 /* Image is at s->buff_start[buf.index] */
423 pkt->data= s->buf_start[buf.index];
424 pkt->size = buf.bytesused;
425 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
426 pkt->destruct = mmap_release_buffer;
427 buf_descriptor = av_malloc(sizeof(struct buff_data));
428 if (buf_descriptor == NULL) {
429 /* Something went wrong... Since av_malloc() failed, we cannot even
430 * allocate a buffer for memcopying into it
432 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
433 res = ioctl(s->fd, VIDIOC_QBUF, &buf);
435 return AVERROR(ENOMEM);
437 buf_descriptor->fd = s->fd;
438 buf_descriptor->index = buf.index;
439 pkt->priv = buf_descriptor;
441 return s->buf_len[buf.index];
444 static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
449 static int mmap_start(AVFormatContext *ctx)
451 struct video_data *s = ctx->priv_data;
452 enum v4l2_buf_type type;
455 for (i = 0; i < s->buffers; i++) {
456 struct v4l2_buffer buf;
458 memset(&buf, 0, sizeof(struct v4l2_buffer));
459 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
460 buf.memory = V4L2_MEMORY_MMAP;
463 res = ioctl(s->fd, VIDIOC_QBUF, &buf);
465 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
467 return AVERROR(errno);
471 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
472 res = ioctl(s->fd, VIDIOC_STREAMON, &type);
474 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
476 return AVERROR(errno);
482 static void mmap_close(struct video_data *s)
484 enum v4l2_buf_type type;
487 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
488 /* We do not check for the result, because we could
489 * not do anything about it anyway...
491 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
492 for (i = 0; i < s->buffers; i++) {
493 munmap(s->buf_start[i], s->buf_len[i]);
495 av_free(s->buf_start);
499 static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap)
501 struct video_data *s = s1->priv_data;
502 struct v4l2_input input;
503 struct v4l2_standard standard;
506 if (ap->channel>=0) {
507 /* set tv video input */
508 memset (&input, 0, sizeof (input));
509 input.index = ap->channel;
510 if (ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
511 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
515 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
516 ap->channel, input.name);
517 if (ioctl(s->fd, VIDIOC_S_INPUT, &input.index) < 0) {
518 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
525 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
527 /* set tv standard */
528 memset (&standard, 0, sizeof (standard));
531 if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
532 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
537 if (!strcasecmp(standard.name, ap->standard)) {
542 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
543 ap->standard, (uint64_t)standard.id);
544 if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
545 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
554 static uint32_t device_try_init(AVFormatContext *s1,
555 const AVFormatParameters *ap,
558 enum CodecID *codec_id)
560 uint32_t desired_format = fmt_ff2v4l(ap->pix_fmt, s1->video_codec_id);
562 if (desired_format == 0 ||
563 device_init(s1, width, height, desired_format) < 0) {
567 for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
568 if (s1->video_codec_id == CODEC_ID_NONE ||
569 fmt_conversion_table[i].codec_id == s1->video_codec_id) {
570 desired_format = fmt_conversion_table[i].v4l2_fmt;
571 if (device_init(s1, width, height, desired_format) >= 0) {
578 if (desired_format != 0) {
579 *codec_id = fmt_v4l2codec(desired_format);
580 assert(*codec_id != CODEC_ID_NONE);
583 return desired_format;
586 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
588 struct video_data *s = s1->priv_data;
591 uint32_t desired_format, capabilities;
592 enum CodecID codec_id;
594 st = av_new_stream(s1, 0);
596 return AVERROR(ENOMEM);
598 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
600 s->width = ap->width;
601 s->height = ap->height;
604 s->fd = device_open(s1, &capabilities);
608 av_log(s1, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n", s->fd, capabilities);
610 if (!s->width && !s->height) {
611 struct v4l2_format fmt;
613 av_log(s1, AV_LOG_VERBOSE, "Querying the device for the current frame size\n");
614 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
615 if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
616 av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", strerror(errno));
617 return AVERROR(errno);
619 s->width = fmt.fmt.pix.width;
620 s->height = fmt.fmt.pix.height;
621 av_log(s1, AV_LOG_VERBOSE, "Setting frame size to %dx%d\n", s->width, s->height);
624 desired_format = device_try_init(s1, ap, &s->width, &s->height, &codec_id);
625 if (desired_format == 0) {
626 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
627 "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, ap->pix_fmt);
632 if (av_image_check_size(s->width, s->height, 0, s1) < 0)
633 return AVERROR(EINVAL);
634 s->frame_format = desired_format;
636 if (v4l2_set_parameters(s1, ap) < 0)
639 st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
640 s->frame_size = avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
641 if (capabilities & V4L2_CAP_STREAMING) {
642 s->io_method = io_mmap;
645 res = mmap_start(s1);
648 s->io_method = io_read;
656 s->top_field_first = first_field(s->fd);
658 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
659 st->codec->codec_id = codec_id;
660 st->codec->width = s->width;
661 st->codec->height = s->height;
662 st->codec->time_base.den = ap->time_base.den;
663 st->codec->time_base.num = ap->time_base.num;
664 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
669 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
671 struct video_data *s = s1->priv_data;
674 if (s->io_method == io_mmap) {
676 res = mmap_read_frame(s1, pkt);
677 } else if (s->io_method == io_read) {
678 if (av_new_packet(pkt, s->frame_size) < 0)
681 res = read_frame(s1, pkt);
689 if (s1->streams[0]->codec->coded_frame) {
690 s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
691 s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
697 static int v4l2_read_close(AVFormatContext *s1)
699 struct video_data *s = s1->priv_data;
701 if (s->io_method == io_mmap) {
709 AVInputFormat v4l2_demuxer = {
711 NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
712 sizeof(struct video_data),
717 .flags = AVFMT_NOFILE,