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;
63 enum PixelFormat ff_fmt;
67 static struct fmt_map fmt_conversion_table[] = {
69 .ff_fmt = PIX_FMT_YUV420P,
70 .v4l2_fmt = V4L2_PIX_FMT_YUV420,
73 .ff_fmt = PIX_FMT_YUV422P,
74 .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
77 .ff_fmt = PIX_FMT_YUV422,
78 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
81 .ff_fmt = PIX_FMT_UYVY422,
82 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
85 .ff_fmt = PIX_FMT_YUV411P,
86 .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
89 .ff_fmt = PIX_FMT_YUV410P,
90 .v4l2_fmt = V4L2_PIX_FMT_YUV410,
93 .ff_fmt = PIX_FMT_BGR24,
94 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
97 .ff_fmt = PIX_FMT_RGB24,
98 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
102 .ff_fmt = PIX_FMT_RGBA32,
103 .v4l2_fmt = V4L2_PIX_FMT_BGR32,
107 .ff_fmt = PIX_FMT_GRAY8,
108 .v4l2_fmt = V4L2_PIX_FMT_GREY,
112 static int device_open(const char *devname, uint32_t *capabilities)
114 struct v4l2_capability cap;
118 fd = open(devname, O_RDWR /*| O_NONBLOCK*/, 0);
120 av_log(NULL, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
121 devname, strerror(errno));
126 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
127 // ENOIOCTLCMD definition only availble on __KERNEL__
128 if (res < 0 && errno == 515)
130 av_log(NULL, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
136 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
142 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
143 av_log(NULL, AV_LOG_ERROR, "Not a video capture device\n");
148 *capabilities = cap.capabilities;
153 static int device_init(int fd, int *width, int *height, int pix_fmt)
155 struct v4l2_format fmt;
158 memset(&fmt, 0, sizeof(struct v4l2_format));
159 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
160 fmt.fmt.pix.width = *width;
161 fmt.fmt.pix.height = *height;
162 fmt.fmt.pix.pixelformat = pix_fmt;
163 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
164 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
165 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
166 av_log(NULL, 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);
167 *width = fmt.fmt.pix.width;
168 *height = fmt.fmt.pix.height;
174 static int first_field(int fd)
179 res = ioctl(fd, VIDIOC_G_STD, &std);
183 if (std & V4L2_STD_NTSC) {
190 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
194 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
195 if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
196 return fmt_conversion_table[i].v4l2_fmt;
203 static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
207 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
208 if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
209 return fmt_conversion_table[i].ff_fmt;
216 static int mmap_init(struct video_data *s)
218 struct v4l2_requestbuffers req;
221 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
222 req.count = desired_video_buffers;
223 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
224 req.memory = V4L2_MEMORY_MMAP;
225 res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
227 if (errno == EINVAL) {
228 av_log(NULL, AV_LOG_ERROR, "Device does not support mmap\n");
230 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
237 av_log(NULL, AV_LOG_ERROR, "Insufficient buffer memory\n");
241 s->buffers = req.count;
242 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
243 if (s->buf_start == NULL) {
244 av_log(NULL, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
248 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
249 if (s->buf_len == NULL) {
250 av_log(NULL, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
251 av_free(s->buf_start);
256 for (i = 0; i < req.count; i++) {
257 struct v4l2_buffer buf;
259 memset(&buf, 0, sizeof(struct v4l2_buffer));
260 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
261 buf.memory = V4L2_MEMORY_MMAP;
263 res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
265 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
270 s->buf_len[i] = buf.length;
271 if (s->buf_len[i] < s->frame_size) {
272 av_log(NULL, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
276 s->buf_start[i] = mmap (NULL, buf.length,
277 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
278 if (s->buf_start[i] == MAP_FAILED) {
279 av_log(NULL, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
288 static int read_init(struct video_data *s)
293 static int mmap_read_frame(struct video_data *s, void *frame, int64_t *ts)
295 struct v4l2_buffer buf;
298 memset(&buf, 0, sizeof(struct v4l2_buffer));
299 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
300 buf.memory = V4L2_MEMORY_MMAP;
302 /* FIXME: Some special treatment might be needed in case of loss of signal... */
303 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 &&
304 ((errno == EAGAIN) || (errno == EINTR)));
306 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
310 assert (buf.index < s->buffers);
311 if (buf.bytesused != s->frame_size) {
312 av_log(NULL, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
317 /* Image is at s->buff_start[buf.index] */
318 memcpy(frame, s->buf_start[buf.index], buf.bytesused);
319 *ts = buf.timestamp.tv_sec * int64_t_C(1000000) + buf.timestamp.tv_usec;
321 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
323 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
328 return s->buf_len[buf.index];
331 static int read_frame(struct video_data *s, void *frame, int64_t *ts)
336 static int mmap_start(struct video_data *s)
338 enum v4l2_buf_type type;
341 for (i = 0; i < s->buffers; i++) {
342 struct v4l2_buffer buf;
344 memset(&buf, 0, sizeof(struct v4l2_buffer));
345 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
346 buf.memory = V4L2_MEMORY_MMAP;
349 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
351 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
357 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
358 res = ioctl (s->fd, VIDIOC_STREAMON, &type);
360 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
368 static void mmap_close(struct video_data *s)
370 enum v4l2_buf_type type;
373 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
374 /* We do not check for the result, because we could
375 * not do anything about it anyway...
377 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
378 for (i = 0; i < s->buffers; i++) {
379 munmap(s->buf_start[i], s->buf_len[i]);
381 av_free(s->buf_start);
385 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
387 struct video_data *s = s1->priv_data;
390 int res, frame_rate, frame_rate_base;
391 uint32_t desired_format, capabilities;
392 const char *video_device;
394 if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
395 av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
402 frame_rate = ap->time_base.den;
403 frame_rate_base = ap->time_base.num;
405 if((unsigned)width > 32767 || (unsigned)height > 32767) {
406 av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
411 st = av_new_stream(s1, 0);
415 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
419 s->frame_rate = frame_rate;
420 s->frame_rate_base = frame_rate_base;
422 video_device = ap->device;
424 video_device = "/dev/video";
427 s->fd = device_open(video_device, &capabilities);
433 av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
435 desired_format = fmt_ff2v4l(ap->pix_fmt);
436 if (desired_format == 0 || (device_init(s->fd, &width, &height, desired_format) < 0)) {
441 desired_format = fmt_conversion_table[i].v4l2_fmt;
442 if (device_init(s->fd, &width, &height, desired_format) < 0) {
448 if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
453 if (desired_format == 0) {
454 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
460 s->frame_format = desired_format;
462 st->codec->pix_fmt = fmt_v4l2ff(desired_format);
463 s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
464 if (capabilities & V4L2_CAP_STREAMING) {
465 s->io_method = io_mmap;
471 s->io_method = io_read;
480 s->top_field_first = first_field(s->fd);
482 st->codec->codec_type = CODEC_TYPE_VIDEO;
483 st->codec->codec_id = CODEC_ID_RAWVIDEO;
484 st->codec->width = width;
485 st->codec->height = height;
486 st->codec->time_base.den = frame_rate;
487 st->codec->time_base.num = frame_rate_base;
488 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
493 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
495 struct video_data *s = s1->priv_data;
498 if (av_new_packet(pkt, s->frame_size) < 0)
501 if (s->io_method == io_mmap) {
502 res = mmap_read_frame(s, pkt->data, &pkt->pts);
503 } else if (s->io_method == io_read) {
504 res = read_frame(s, pkt->data, &pkt->pts);
512 if (s1->streams[0]->codec->coded_frame) {
513 s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
514 s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
517 return s->frame_size;
520 static int v4l2_read_close(AVFormatContext *s1)
522 struct video_data *s = s1->priv_data;
524 if (s->io_method == io_mmap) {
532 AVInputFormat v4l2_demuxer = {
535 sizeof(struct video_data),
540 .flags = AVFMT_NOFILE,