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 library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <sys/ioctl.h>
33 #define _LINUX_TIME_H 1
34 #include <linux/videodev.h>
37 static const int desired_video_buffers = 256;
47 int frame_format; /* V4L2_PIX_FMT_* */
48 enum io_method io_method;
57 unsigned int *buf_len;
61 enum PixelFormat ff_fmt;
65 static struct fmt_map fmt_conversion_table[] = {
67 .ff_fmt = PIX_FMT_YUV420P,
68 .v4l2_fmt = V4L2_PIX_FMT_YUV420,
71 .ff_fmt = PIX_FMT_YUV422P,
72 .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
75 .ff_fmt = PIX_FMT_YUV422,
76 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
79 .ff_fmt = PIX_FMT_UYVY422,
80 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
83 .ff_fmt = PIX_FMT_YUV411P,
84 .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
87 .ff_fmt = PIX_FMT_YUV410P,
88 .v4l2_fmt = V4L2_PIX_FMT_YUV410,
91 .ff_fmt = PIX_FMT_BGR24,
92 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
95 .ff_fmt = PIX_FMT_RGB24,
96 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
100 .ff_fmt = PIX_FMT_RGBA32,
101 .v4l2_fmt = V4L2_PIX_FMT_BGR32,
105 .ff_fmt = PIX_FMT_GRAY8,
106 .v4l2_fmt = V4L2_PIX_FMT_GREY,
110 static int device_open(const char *devname, uint32_t *capabilities)
112 struct v4l2_capability cap;
116 fd = open(devname, O_RDWR /*| O_NONBLOCK*/, 0);
118 av_log(NULL, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
119 devname, strerror(errno));
124 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
125 // ENOIOCTLCMD definition only availble on __KERNEL__
126 if (res < 0 && errno == 515)
128 av_log(NULL, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
134 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
140 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
141 av_log(NULL, AV_LOG_ERROR, "Not a video capture device\n");
146 *capabilities = cap.capabilities;
151 static int device_init(int fd, int *width, int *height, int pix_fmt)
153 struct v4l2_format fmt;
156 memset(&fmt, 0, sizeof(struct v4l2_format));
157 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
158 fmt.fmt.pix.width = *width;
159 fmt.fmt.pix.height = *height;
160 fmt.fmt.pix.pixelformat = pix_fmt;
161 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
162 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
163 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
164 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);
165 *width = fmt.fmt.pix.width;
166 *height = fmt.fmt.pix.height;
172 static int first_field(int fd)
177 res = ioctl(fd, VIDIOC_G_STD, &std);
181 if (std & V4L2_STD_NTSC) {
188 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
192 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
193 if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
194 return fmt_conversion_table[i].v4l2_fmt;
201 static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
205 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
206 if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
207 return fmt_conversion_table[i].ff_fmt;
214 static int mmap_init(struct video_data *s)
216 struct v4l2_requestbuffers req;
219 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
220 req.count = desired_video_buffers;
221 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
222 req.memory = V4L2_MEMORY_MMAP;
223 res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
225 if (errno == EINVAL) {
226 av_log(NULL, AV_LOG_ERROR, "Device does not support mmap\n");
228 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
235 av_log(NULL, AV_LOG_ERROR, "Insufficient buffer memory\n");
239 s->buffers = req.count;
240 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
241 if (s->buf_start == NULL) {
242 av_log(NULL, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
246 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
247 if (s->buf_len == NULL) {
248 av_log(NULL, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
249 av_free(s->buf_start);
254 for (i = 0; i < req.count; i++) {
255 struct v4l2_buffer buf;
257 memset(&buf, 0, sizeof(struct v4l2_buffer));
258 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
259 buf.memory = V4L2_MEMORY_MMAP;
261 res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
263 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
268 s->buf_len[i] = buf.length;
269 if (s->buf_len[i] < s->frame_size) {
270 av_log(NULL, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
274 s->buf_start[i] = mmap (NULL, buf.length,
275 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
276 if (s->buf_start[i] == MAP_FAILED) {
277 av_log(NULL, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
286 static int read_init(struct video_data *s)
291 static int mmap_read_frame(struct video_data *s, void *frame, int64_t *ts)
293 struct v4l2_buffer buf;
296 memset(&buf, 0, sizeof(struct v4l2_buffer));
297 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
298 buf.memory = V4L2_MEMORY_MMAP;
300 /* FIXME: Some special treatment might be needed in case of loss of signal... */
301 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 &&
302 ((errno == EAGAIN) || (errno == EINTR)));
304 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
308 assert (buf.index < s->buffers);
309 assert(buf.bytesused == s->frame_size);
310 /* Image is at s->buff_start[buf.index] */
311 memcpy(frame, s->buf_start[buf.index], buf.bytesused);
312 *ts = buf.timestamp.tv_sec * int64_t_C(1000000) + buf.timestamp.tv_usec;
314 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
316 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
321 return s->buf_len[buf.index];
324 static int read_frame(struct video_data *s, void *frame, int64_t *ts)
329 static int mmap_start(struct video_data *s)
331 enum v4l2_buf_type type;
334 for (i = 0; i < s->buffers; i++) {
335 struct v4l2_buffer buf;
337 memset(&buf, 0, sizeof(struct v4l2_buffer));
338 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
339 buf.memory = V4L2_MEMORY_MMAP;
342 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
344 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
350 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
351 res = ioctl (s->fd, VIDIOC_STREAMON, &type);
353 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
361 static void mmap_close(struct video_data *s)
363 enum v4l2_buf_type type;
366 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
367 /* We do not check for the result, because we could
368 * not do anything about it anyway...
370 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
371 for (i = 0; i < s->buffers; i++) {
372 munmap(s->buf_start[i], s->buf_len[i]);
374 av_free(s->buf_start);
378 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
380 struct video_data *s = s1->priv_data;
383 int res, frame_rate, frame_rate_base;
384 uint32_t desired_format, capabilities;
385 const char *video_device;
387 if (!ap || ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
388 av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
395 frame_rate = ap->time_base.den;
396 frame_rate_base = ap->time_base.num;
398 if((unsigned)width > 32767 || (unsigned)height > 32767) {
399 av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
404 st = av_new_stream(s1, 0);
408 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
412 s->frame_rate = frame_rate;
413 s->frame_rate_base = frame_rate_base;
415 video_device = ap->device;
417 video_device = "/dev/video";
420 s->fd = device_open(video_device, &capabilities);
426 av_log(s1, AV_LOG_ERROR, "[%d]Capabilities: %x\n", s->fd, capabilities);
428 desired_format = fmt_ff2v4l(ap->pix_fmt);
429 if (desired_format == 0 || (device_init(s->fd, &width, &height, desired_format) < 0)) {
434 desired_format = fmt_conversion_table[i].v4l2_fmt;
435 if (device_init(s->fd, &width, &height, desired_format) < 0) {
441 if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
446 if (desired_format == 0) {
447 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
453 s->frame_format = desired_format;
455 st->codec->pix_fmt = fmt_v4l2ff(desired_format);
456 s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
457 if (capabilities & V4L2_CAP_STREAMING) {
458 s->io_method = io_mmap;
462 s->io_method = io_read;
471 s->top_field_first = first_field(s->fd);
473 st->codec->codec_type = CODEC_TYPE_VIDEO;
474 st->codec->codec_id = CODEC_ID_RAWVIDEO;
475 st->codec->width = width;
476 st->codec->height = height;
477 st->codec->time_base.den = frame_rate;
478 st->codec->time_base.num = frame_rate_base;
479 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
484 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
486 struct video_data *s = s1->priv_data;
489 if (av_new_packet(pkt, s->frame_size) < 0)
492 if (s->io_method == io_mmap) {
493 res = mmap_read_frame(s, pkt->data, &pkt->pts);
494 } else if (s->io_method == io_read) {
495 res = read_frame(s, pkt->data, &pkt->pts);
503 if (s1->streams[0]->codec->coded_frame) {
504 s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
505 s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
508 return s->frame_size;
511 static int v4l2_read_close(AVFormatContext *s1)
513 struct video_data *s = s1->priv_data;
515 if (s->io_method == io_mmap) {
523 static AVInputFormat v4l2_format = {
526 sizeof(struct video_data),
531 .flags = AVFMT_NOFILE,
536 av_register_input_format(&v4l2_format);