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
32 #include <sys/ioctl.h>
35 #include <asm/types.h>
36 #include <linux/videodev2.h>
39 static const int desired_video_buffers = 256;
49 int frame_format; /* V4L2_PIX_FMT_* */
50 enum io_method io_method;
59 unsigned int *buf_len;
68 enum PixelFormat ff_fmt;
72 static struct fmt_map fmt_conversion_table[] = {
74 .ff_fmt = PIX_FMT_YUV420P,
75 .v4l2_fmt = V4L2_PIX_FMT_YUV420,
78 .ff_fmt = PIX_FMT_YUV422P,
79 .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
82 .ff_fmt = PIX_FMT_YUYV422,
83 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
86 .ff_fmt = PIX_FMT_UYVY422,
87 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
90 .ff_fmt = PIX_FMT_YUV411P,
91 .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
94 .ff_fmt = PIX_FMT_YUV410P,
95 .v4l2_fmt = V4L2_PIX_FMT_YUV410,
98 .ff_fmt = PIX_FMT_BGR24,
99 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
102 .ff_fmt = PIX_FMT_RGB24,
103 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
107 .ff_fmt = PIX_FMT_RGB32,
108 .v4l2_fmt = V4L2_PIX_FMT_BGR32,
112 .ff_fmt = PIX_FMT_GRAY8,
113 .v4l2_fmt = V4L2_PIX_FMT_GREY,
117 static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
119 struct v4l2_capability cap;
123 fd = open(ctx->filename, O_RDWR /*| O_NONBLOCK*/, 0);
125 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
126 ctx->filename, strerror(errno));
131 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
132 // ENOIOCTLCMD definition only availble on __KERNEL__
133 if (res < 0 && errno == 515)
135 av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
141 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
147 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
148 av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
153 *capabilities = cap.capabilities;
158 static int device_init(AVFormatContext *ctx, int *width, int *height, int pix_fmt)
160 struct video_data *s = ctx->priv_data;
162 struct v4l2_format fmt;
165 memset(&fmt, 0, sizeof(struct v4l2_format));
166 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
167 fmt.fmt.pix.width = *width;
168 fmt.fmt.pix.height = *height;
169 fmt.fmt.pix.pixelformat = pix_fmt;
170 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
171 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
172 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
173 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);
174 *width = fmt.fmt.pix.width;
175 *height = fmt.fmt.pix.height;
181 static int first_field(int fd)
186 res = ioctl(fd, VIDIOC_G_STD, &std);
190 if (std & V4L2_STD_NTSC) {
197 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
201 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
202 if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
203 return fmt_conversion_table[i].v4l2_fmt;
210 static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
214 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
215 if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
216 return fmt_conversion_table[i].ff_fmt;
223 static int mmap_init(AVFormatContext *ctx)
225 struct video_data *s = ctx->priv_data;
226 struct v4l2_requestbuffers req;
229 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
230 req.count = desired_video_buffers;
231 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
232 req.memory = V4L2_MEMORY_MMAP;
233 res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
235 if (errno == EINVAL) {
236 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
238 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
245 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
249 s->buffers = req.count;
250 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
251 if (s->buf_start == NULL) {
252 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
256 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
257 if (s->buf_len == NULL) {
258 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
259 av_free(s->buf_start);
264 for (i = 0; i < req.count; i++) {
265 struct v4l2_buffer buf;
267 memset(&buf, 0, sizeof(struct v4l2_buffer));
268 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
269 buf.memory = V4L2_MEMORY_MMAP;
271 res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
273 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
278 s->buf_len[i] = buf.length;
279 if (s->buf_len[i] < s->frame_size) {
280 av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
284 s->buf_start[i] = mmap (NULL, buf.length,
285 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
286 if (s->buf_start[i] == MAP_FAILED) {
287 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
296 static int read_init(AVFormatContext *ctx)
301 static void mmap_release_buffer(AVPacket *pkt)
303 struct v4l2_buffer buf;
305 struct buff_data *buf_descriptor = pkt->priv;
307 memset(&buf, 0, sizeof(struct v4l2_buffer));
308 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
309 buf.memory = V4L2_MEMORY_MMAP;
310 buf.index = buf_descriptor->index;
311 fd = buf_descriptor->fd;
312 av_free(buf_descriptor);
314 res = ioctl (fd, VIDIOC_QBUF, &buf);
316 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
322 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
324 struct video_data *s = ctx->priv_data;
325 struct v4l2_buffer buf;
326 struct buff_data *buf_descriptor;
329 memset(&buf, 0, sizeof(struct v4l2_buffer));
330 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
331 buf.memory = V4L2_MEMORY_MMAP;
333 /* FIXME: Some special treatment might be needed in case of loss of signal... */
334 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 &&
335 ((errno == EAGAIN) || (errno == EINTR)));
337 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
341 assert (buf.index < s->buffers);
342 if (buf.bytesused != s->frame_size) {
343 av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
348 /* Image is at s->buff_start[buf.index] */
349 pkt->data= s->buf_start[buf.index];
350 pkt->size = buf.bytesused;
351 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
352 pkt->destruct = mmap_release_buffer;
353 buf_descriptor = av_malloc(sizeof(struct buff_data));
354 if (buf_descriptor == NULL) {
355 /* Something went wrong... Since av_malloc() failed, we cannot even
356 * allocate a buffer for memcopying into it
358 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
359 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
363 buf_descriptor->fd = s->fd;
364 buf_descriptor->index = buf.index;
365 pkt->priv = buf_descriptor;
367 return s->buf_len[buf.index];
370 static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
375 static int mmap_start(AVFormatContext *ctx)
377 struct video_data *s = ctx->priv_data;
378 enum v4l2_buf_type type;
381 for (i = 0; i < s->buffers; i++) {
382 struct v4l2_buffer buf;
384 memset(&buf, 0, sizeof(struct v4l2_buffer));
385 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
386 buf.memory = V4L2_MEMORY_MMAP;
389 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
391 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
397 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
398 res = ioctl (s->fd, VIDIOC_STREAMON, &type);
400 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
408 static void mmap_close(struct video_data *s)
410 enum v4l2_buf_type type;
413 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
414 /* We do not check for the result, because we could
415 * not do anything about it anyway...
417 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
418 for (i = 0; i < s->buffers; i++) {
419 munmap(s->buf_start[i], s->buf_len[i]);
421 av_free(s->buf_start);
425 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
427 struct video_data *s = s1->priv_data;
430 int res, frame_rate, frame_rate_base;
431 uint32_t desired_format, capabilities;
433 if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
434 av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
441 frame_rate = ap->time_base.den;
442 frame_rate_base = ap->time_base.num;
444 if((unsigned)width > 32767 || (unsigned)height > 32767) {
445 av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
450 st = av_new_stream(s1, 0);
452 return AVERROR(ENOMEM);
454 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
458 s->frame_rate = frame_rate;
459 s->frame_rate_base = frame_rate_base;
462 s->fd = device_open(s1, &capabilities);
468 av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
470 desired_format = fmt_ff2v4l(ap->pix_fmt);
471 if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {
476 desired_format = fmt_conversion_table[i].v4l2_fmt;
477 if (device_init(s1, &width, &height, desired_format) < 0) {
483 if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
488 if (desired_format == 0) {
489 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
495 s->frame_format = desired_format;
497 st->codec->pix_fmt = fmt_v4l2ff(desired_format);
498 s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
499 if (capabilities & V4L2_CAP_STREAMING) {
500 s->io_method = io_mmap;
503 res = mmap_start(s1);
506 s->io_method = io_read;
515 s->top_field_first = first_field(s->fd);
517 st->codec->codec_type = CODEC_TYPE_VIDEO;
518 st->codec->codec_id = CODEC_ID_RAWVIDEO;
519 st->codec->width = width;
520 st->codec->height = height;
521 st->codec->time_base.den = frame_rate;
522 st->codec->time_base.num = frame_rate_base;
523 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
528 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
530 struct video_data *s = s1->priv_data;
533 if (s->io_method == io_mmap) {
535 res = mmap_read_frame(s1, pkt);
536 } else if (s->io_method == io_read) {
537 if (av_new_packet(pkt, s->frame_size) < 0)
540 res = read_frame(s1, pkt);
548 if (s1->streams[0]->codec->coded_frame) {
549 s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
550 s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
553 return s->frame_size;
556 static int v4l2_read_close(AVFormatContext *s1)
558 struct video_data *s = s1->priv_data;
560 if (s->io_method == io_mmap) {
568 AVInputFormat v4l2_demuxer = {
571 sizeof(struct video_data),
576 .flags = AVFMT_NOFILE,