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
31 #include "libavformat/avformat.h"
34 #include <sys/ioctl.h>
37 #include <asm/types.h>
38 #include <linux/videodev2.h>
42 static const int desired_video_buffers = 256;
52 int frame_format; /* V4L2_PIX_FMT_* */
53 enum io_method io_method;
62 unsigned int *buf_len;
71 enum PixelFormat ff_fmt;
75 static struct fmt_map fmt_conversion_table[] = {
77 .ff_fmt = PIX_FMT_YUV420P,
78 .v4l2_fmt = V4L2_PIX_FMT_YUV420,
81 .ff_fmt = PIX_FMT_YUV422P,
82 .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
85 .ff_fmt = PIX_FMT_YUYV422,
86 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
89 .ff_fmt = PIX_FMT_UYVY422,
90 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
93 .ff_fmt = PIX_FMT_YUV411P,
94 .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
97 .ff_fmt = PIX_FMT_YUV410P,
98 .v4l2_fmt = V4L2_PIX_FMT_YUV410,
101 .ff_fmt = PIX_FMT_BGR24,
102 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
105 .ff_fmt = PIX_FMT_RGB24,
106 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
110 .ff_fmt = PIX_FMT_RGB32,
111 .v4l2_fmt = V4L2_PIX_FMT_BGR32,
115 .ff_fmt = PIX_FMT_GRAY8,
116 .v4l2_fmt = V4L2_PIX_FMT_GREY,
120 static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
122 struct v4l2_capability cap;
127 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
130 fd = open(ctx->filename, flags, 0);
132 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
133 ctx->filename, strerror(errno));
138 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
139 // ENOIOCTLCMD definition only availble on __KERNEL__
140 if (res < 0 && errno == 515)
142 av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
148 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
154 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
155 av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
160 *capabilities = cap.capabilities;
165 static int device_init(AVFormatContext *ctx, int *width, int *height, int pix_fmt)
167 struct video_data *s = ctx->priv_data;
169 struct v4l2_format fmt;
172 memset(&fmt, 0, sizeof(struct v4l2_format));
173 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
174 fmt.fmt.pix.width = *width;
175 fmt.fmt.pix.height = *height;
176 fmt.fmt.pix.pixelformat = pix_fmt;
177 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
178 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
179 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
180 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);
181 *width = fmt.fmt.pix.width;
182 *height = fmt.fmt.pix.height;
188 static int first_field(int fd)
193 res = ioctl(fd, VIDIOC_G_STD, &std);
197 if (std & V4L2_STD_NTSC) {
204 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
208 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
209 if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
210 return fmt_conversion_table[i].v4l2_fmt;
217 static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
221 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
222 if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
223 return fmt_conversion_table[i].ff_fmt;
230 static int mmap_init(AVFormatContext *ctx)
232 struct video_data *s = ctx->priv_data;
233 struct v4l2_requestbuffers req;
236 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
237 req.count = desired_video_buffers;
238 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
239 req.memory = V4L2_MEMORY_MMAP;
240 res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
242 if (errno == EINVAL) {
243 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
245 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
252 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
256 s->buffers = req.count;
257 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
258 if (s->buf_start == NULL) {
259 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
263 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
264 if (s->buf_len == NULL) {
265 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
266 av_free(s->buf_start);
271 for (i = 0; i < req.count; i++) {
272 struct v4l2_buffer buf;
274 memset(&buf, 0, sizeof(struct v4l2_buffer));
275 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
276 buf.memory = V4L2_MEMORY_MMAP;
278 res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
280 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
285 s->buf_len[i] = buf.length;
286 if (s->buf_len[i] < s->frame_size) {
287 av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
291 s->buf_start[i] = mmap (NULL, buf.length,
292 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
293 if (s->buf_start[i] == MAP_FAILED) {
294 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
303 static int read_init(AVFormatContext *ctx)
308 static void mmap_release_buffer(AVPacket *pkt)
310 struct v4l2_buffer buf;
312 struct buff_data *buf_descriptor = pkt->priv;
314 memset(&buf, 0, sizeof(struct v4l2_buffer));
315 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
316 buf.memory = V4L2_MEMORY_MMAP;
317 buf.index = buf_descriptor->index;
318 fd = buf_descriptor->fd;
319 av_free(buf_descriptor);
321 res = ioctl (fd, VIDIOC_QBUF, &buf);
323 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
329 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
331 struct video_data *s = ctx->priv_data;
332 struct v4l2_buffer buf;
333 struct buff_data *buf_descriptor;
336 memset(&buf, 0, sizeof(struct v4l2_buffer));
337 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
338 buf.memory = V4L2_MEMORY_MMAP;
340 /* FIXME: Some special treatment might be needed in case of loss of signal... */
341 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
343 if (errno == EAGAIN) {
346 return AVERROR(EAGAIN);
348 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
352 assert (buf.index < s->buffers);
353 if (buf.bytesused != s->frame_size) {
354 av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
359 /* Image is at s->buff_start[buf.index] */
360 pkt->data= s->buf_start[buf.index];
361 pkt->size = buf.bytesused;
362 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
363 pkt->destruct = mmap_release_buffer;
364 buf_descriptor = av_malloc(sizeof(struct buff_data));
365 if (buf_descriptor == NULL) {
366 /* Something went wrong... Since av_malloc() failed, we cannot even
367 * allocate a buffer for memcopying into it
369 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
370 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
374 buf_descriptor->fd = s->fd;
375 buf_descriptor->index = buf.index;
376 pkt->priv = buf_descriptor;
378 return s->buf_len[buf.index];
381 static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
386 static int mmap_start(AVFormatContext *ctx)
388 struct video_data *s = ctx->priv_data;
389 enum v4l2_buf_type type;
392 for (i = 0; i < s->buffers; i++) {
393 struct v4l2_buffer buf;
395 memset(&buf, 0, sizeof(struct v4l2_buffer));
396 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
397 buf.memory = V4L2_MEMORY_MMAP;
400 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
402 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
408 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
409 res = ioctl (s->fd, VIDIOC_STREAMON, &type);
411 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
419 static void mmap_close(struct video_data *s)
421 enum v4l2_buf_type type;
424 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
425 /* We do not check for the result, because we could
426 * not do anything about it anyway...
428 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
429 for (i = 0; i < s->buffers; i++) {
430 munmap(s->buf_start[i], s->buf_len[i]);
432 av_free(s->buf_start);
436 static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
438 struct video_data *s = s1->priv_data;
439 struct v4l2_input input;
440 struct v4l2_standard standard;
444 /* set tv video input */
445 memset (&input, 0, sizeof (input));
446 input.index = ap->channel;
447 if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
448 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
452 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
453 ap->channel, input.name);
454 if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
455 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
462 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
464 /* set tv standard */
465 memset (&standard, 0, sizeof (standard));
468 if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
469 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
474 if(!strcasecmp(standard.name, ap->standard)) {
479 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
480 ap->standard, standard.id);
481 if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
482 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
491 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
493 struct video_data *s = s1->priv_data;
496 int res, frame_rate, frame_rate_base;
497 uint32_t desired_format, capabilities;
499 if (ap->width <= 0 || ap->height <= 0) {
500 av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
503 if (ap->time_base.den <= 0) {
504 av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
510 frame_rate = ap->time_base.den;
511 frame_rate_base = ap->time_base.num;
513 if((unsigned)width > 32767 || (unsigned)height > 32767) {
514 av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", width, height);
519 st = av_new_stream(s1, 0);
521 return AVERROR(ENOMEM);
523 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
527 s->frame_rate = frame_rate;
528 s->frame_rate_base = frame_rate_base;
531 s->fd = device_open(s1, &capabilities);
535 av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
537 desired_format = fmt_ff2v4l(ap->pix_fmt);
538 if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {
543 desired_format = fmt_conversion_table[i].v4l2_fmt;
544 if (device_init(s1, &width, &height, desired_format) < 0) {
550 if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
555 if (desired_format == 0) {
556 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
561 s->frame_format = desired_format;
563 if( v4l2_set_parameters( s1, ap ) < 0 )
566 st->codec->pix_fmt = fmt_v4l2ff(desired_format);
567 s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
568 if (capabilities & V4L2_CAP_STREAMING) {
569 s->io_method = io_mmap;
572 res = mmap_start(s1);
575 s->io_method = io_read;
583 s->top_field_first = first_field(s->fd);
585 st->codec->codec_type = CODEC_TYPE_VIDEO;
586 st->codec->codec_id = CODEC_ID_RAWVIDEO;
587 st->codec->width = width;
588 st->codec->height = height;
589 st->codec->time_base.den = frame_rate;
590 st->codec->time_base.num = frame_rate_base;
591 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
596 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
598 struct video_data *s = s1->priv_data;
601 if (s->io_method == io_mmap) {
603 res = mmap_read_frame(s1, pkt);
604 } else if (s->io_method == io_read) {
605 if (av_new_packet(pkt, s->frame_size) < 0)
608 res = read_frame(s1, pkt);
616 if (s1->streams[0]->codec->coded_frame) {
617 s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
618 s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
621 return s->frame_size;
624 static int v4l2_read_close(AVFormatContext *s1)
626 struct video_data *s = s1->priv_data;
628 if (s->io_method == io_mmap) {
636 AVInputFormat v4l2_demuxer = {
638 NULL_IF_CONFIG_SMALL("video grab"),
639 sizeof(struct video_data),
644 .flags = AVFMT_NOFILE,