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 #include <asm/types.h>
39 #include <linux/videodev2.h>
43 static const int desired_video_buffers = 256;
53 int frame_format; /* V4L2_PIX_FMT_* */
54 enum io_method io_method;
63 unsigned int *buf_len;
72 enum PixelFormat ff_fmt;
76 static struct fmt_map fmt_conversion_table[] = {
78 .ff_fmt = PIX_FMT_YUV420P,
79 .v4l2_fmt = V4L2_PIX_FMT_YUV420,
82 .ff_fmt = PIX_FMT_YUV422P,
83 .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
86 .ff_fmt = PIX_FMT_YUYV422,
87 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
90 .ff_fmt = PIX_FMT_UYVY422,
91 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
94 .ff_fmt = PIX_FMT_YUV411P,
95 .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
98 .ff_fmt = PIX_FMT_YUV410P,
99 .v4l2_fmt = V4L2_PIX_FMT_YUV410,
102 .ff_fmt = PIX_FMT_RGB555,
103 .v4l2_fmt = V4L2_PIX_FMT_RGB555,
106 .ff_fmt = PIX_FMT_RGB565,
107 .v4l2_fmt = V4L2_PIX_FMT_RGB565,
110 .ff_fmt = PIX_FMT_BGR24,
111 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
114 .ff_fmt = PIX_FMT_RGB24,
115 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
118 .ff_fmt = PIX_FMT_BGRA,
119 .v4l2_fmt = V4L2_PIX_FMT_BGR32,
122 .ff_fmt = PIX_FMT_GRAY8,
123 .v4l2_fmt = V4L2_PIX_FMT_GREY,
127 static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
129 struct v4l2_capability cap;
134 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
137 fd = open(ctx->filename, flags, 0);
139 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
140 ctx->filename, strerror(errno));
145 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
146 // ENOIOCTLCMD definition only availble on __KERNEL__
147 if (res < 0 && errno == 515)
149 av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
155 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
161 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
162 av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
167 *capabilities = cap.capabilities;
172 static int device_init(AVFormatContext *ctx, int *width, int *height, int pix_fmt)
174 struct video_data *s = ctx->priv_data;
176 struct v4l2_format fmt;
179 memset(&fmt, 0, sizeof(struct v4l2_format));
180 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
181 fmt.fmt.pix.width = *width;
182 fmt.fmt.pix.height = *height;
183 fmt.fmt.pix.pixelformat = pix_fmt;
184 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
185 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
186 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
187 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);
188 *width = fmt.fmt.pix.width;
189 *height = fmt.fmt.pix.height;
195 static int first_field(int fd)
200 res = ioctl(fd, VIDIOC_G_STD, &std);
204 if (std & V4L2_STD_NTSC) {
211 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
215 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
216 if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
217 return fmt_conversion_table[i].v4l2_fmt;
224 static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
228 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
229 if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
230 return fmt_conversion_table[i].ff_fmt;
237 static int mmap_init(AVFormatContext *ctx)
239 struct video_data *s = ctx->priv_data;
240 struct v4l2_requestbuffers req;
243 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
244 req.count = desired_video_buffers;
245 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
246 req.memory = V4L2_MEMORY_MMAP;
247 res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
249 if (errno == EINVAL) {
250 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
252 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
259 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
263 s->buffers = req.count;
264 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
265 if (s->buf_start == NULL) {
266 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
270 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
271 if (s->buf_len == NULL) {
272 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
273 av_free(s->buf_start);
278 for (i = 0; i < req.count; i++) {
279 struct v4l2_buffer buf;
281 memset(&buf, 0, sizeof(struct v4l2_buffer));
282 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
283 buf.memory = V4L2_MEMORY_MMAP;
285 res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
287 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
292 s->buf_len[i] = buf.length;
293 if (s->buf_len[i] < s->frame_size) {
294 av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
298 s->buf_start[i] = mmap (NULL, buf.length,
299 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
300 if (s->buf_start[i] == MAP_FAILED) {
301 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
310 static int read_init(AVFormatContext *ctx)
315 static void mmap_release_buffer(AVPacket *pkt)
317 struct v4l2_buffer buf;
319 struct buff_data *buf_descriptor = pkt->priv;
321 memset(&buf, 0, sizeof(struct v4l2_buffer));
322 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
323 buf.memory = V4L2_MEMORY_MMAP;
324 buf.index = buf_descriptor->index;
325 fd = buf_descriptor->fd;
326 av_free(buf_descriptor);
328 res = ioctl (fd, VIDIOC_QBUF, &buf);
330 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
336 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
338 struct video_data *s = ctx->priv_data;
339 struct v4l2_buffer buf;
340 struct buff_data *buf_descriptor;
343 memset(&buf, 0, sizeof(struct v4l2_buffer));
344 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
345 buf.memory = V4L2_MEMORY_MMAP;
347 /* FIXME: Some special treatment might be needed in case of loss of signal... */
348 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
350 if (errno == EAGAIN) {
353 return AVERROR(EAGAIN);
355 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
359 assert (buf.index < s->buffers);
360 if (buf.bytesused != s->frame_size) {
361 av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
366 /* Image is at s->buff_start[buf.index] */
367 pkt->data= s->buf_start[buf.index];
368 pkt->size = buf.bytesused;
369 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
370 pkt->destruct = mmap_release_buffer;
371 buf_descriptor = av_malloc(sizeof(struct buff_data));
372 if (buf_descriptor == NULL) {
373 /* Something went wrong... Since av_malloc() failed, we cannot even
374 * allocate a buffer for memcopying into it
376 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
377 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
381 buf_descriptor->fd = s->fd;
382 buf_descriptor->index = buf.index;
383 pkt->priv = buf_descriptor;
385 return s->buf_len[buf.index];
388 static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
393 static int mmap_start(AVFormatContext *ctx)
395 struct video_data *s = ctx->priv_data;
396 enum v4l2_buf_type type;
399 for (i = 0; i < s->buffers; i++) {
400 struct v4l2_buffer buf;
402 memset(&buf, 0, sizeof(struct v4l2_buffer));
403 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
404 buf.memory = V4L2_MEMORY_MMAP;
407 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
409 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
415 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
416 res = ioctl (s->fd, VIDIOC_STREAMON, &type);
418 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
426 static void mmap_close(struct video_data *s)
428 enum v4l2_buf_type type;
431 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
432 /* We do not check for the result, because we could
433 * not do anything about it anyway...
435 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
436 for (i = 0; i < s->buffers; i++) {
437 munmap(s->buf_start[i], s->buf_len[i]);
439 av_free(s->buf_start);
443 static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
445 struct video_data *s = s1->priv_data;
446 struct v4l2_input input;
447 struct v4l2_standard standard;
451 /* set tv video input */
452 memset (&input, 0, sizeof (input));
453 input.index = ap->channel;
454 if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
455 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
459 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
460 ap->channel, input.name);
461 if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
462 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
469 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
471 /* set tv standard */
472 memset (&standard, 0, sizeof (standard));
475 if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
476 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
481 if(!strcasecmp(standard.name, ap->standard)) {
486 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
487 ap->standard, standard.id);
488 if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
489 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
498 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
500 struct video_data *s = s1->priv_data;
503 int res, frame_rate, frame_rate_base;
504 uint32_t desired_format, capabilities;
506 if (ap->width <= 0 || ap->height <= 0) {
507 av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
510 if (ap->time_base.den <= 0) {
511 av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
517 frame_rate = ap->time_base.den;
518 frame_rate_base = ap->time_base.num;
520 if((unsigned)width > 32767 || (unsigned)height > 32767) {
521 av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", width, height);
526 st = av_new_stream(s1, 0);
528 return AVERROR(ENOMEM);
530 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
534 s->frame_rate = frame_rate;
535 s->frame_rate_base = frame_rate_base;
538 s->fd = device_open(s1, &capabilities);
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)) {
550 desired_format = fmt_conversion_table[i].v4l2_fmt;
551 if (device_init(s1, &width, &height, desired_format) < 0) {
557 if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
562 if (desired_format == 0) {
563 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
568 s->frame_format = desired_format;
570 if( v4l2_set_parameters( s1, ap ) < 0 )
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;
579 res = mmap_start(s1);
582 s->io_method = io_read;
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 = frame_rate;
597 st->codec->time_base.num = frame_rate_base;
598 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
603 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
605 struct video_data *s = s1->priv_data;
608 if (s->io_method == io_mmap) {
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)
615 res = read_frame(s1, pkt);
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) {
643 AVInputFormat v4l2_demuxer = {
645 NULL_IF_CONFIG_SMALL("video grab"),
646 sizeof(struct video_data),
651 .flags = AVFMT_NOFILE,