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");
133 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
138 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
139 av_log(NULL, AV_LOG_ERROR, "Not a video capture device\n");
143 *capabilities = cap.capabilities;
148 static int device_init(int fd, int width, int height, int pix_fmt)
150 struct v4l2_format fmt;
152 memset(&fmt, 0, sizeof(struct v4l2_format));
153 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
154 fmt.fmt.pix.width = width;
155 fmt.fmt.pix.height = height;
156 fmt.fmt.pix.pixelformat = pix_fmt;
157 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
158 return ioctl (fd, VIDIOC_S_FMT, &fmt);
161 static int first_field(int fd)
166 res = ioctl(fd, VIDIOC_G_STD, &std);
170 if (std & V4L2_STD_NTSC) {
177 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
181 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
182 if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
183 return fmt_conversion_table[i].v4l2_fmt;
190 static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
194 for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {
195 if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
196 return fmt_conversion_table[i].ff_fmt;
203 static int mmap_init(struct video_data *s)
205 struct v4l2_requestbuffers req;
208 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
209 req.count = desired_video_buffers;
210 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
211 req.memory = V4L2_MEMORY_MMAP;
212 res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
214 if (errno == EINVAL) {
215 av_log(NULL, AV_LOG_ERROR, "Device does not support mmap\n");
217 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
224 av_log(NULL, AV_LOG_ERROR, "Insufficient buffer memory\n");
228 s->buffers = req.count;
229 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
230 if (s->buf_start == NULL) {
231 av_log(NULL, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
235 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
236 if (s->buf_len == NULL) {
237 av_log(NULL, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
238 av_free(s->buf_start);
243 for (i = 0; i < req.count; i++) {
244 struct v4l2_buffer buf;
246 memset(&buf, 0, sizeof(struct v4l2_buffer));
247 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
248 buf.memory = V4L2_MEMORY_MMAP;
250 res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
252 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
257 s->buf_len[i] = buf.length;
258 if (s->buf_len[i] < s->frame_size) {
259 av_log(NULL, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
263 s->buf_start[i] = mmap (NULL, buf.length,
264 PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
265 if (s->buf_start[i] == MAP_FAILED) {
266 av_log(NULL, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
275 static int read_init(struct video_data *s)
280 static int mmap_read_frame(struct video_data *s, void *frame, int64_t *ts)
282 struct v4l2_buffer buf;
285 memset(&buf, 0, sizeof(struct v4l2_buffer));
286 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
287 buf.memory = V4L2_MEMORY_MMAP;
289 /* FIXME: Some special treatment might be needed in case of loss of signal... */
290 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 &&
291 ((errno == EAGAIN) || (errno == EINTR)));
293 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
297 assert (buf.index < s->buffers);
298 assert(buf.bytesused == s->frame_size);
299 /* Image is at s->buff_start[buf.index] */
300 memcpy(frame, s->buf_start[buf.index], buf.bytesused);
301 *ts = buf.timestamp.tv_sec * int64_t_C(1000000) + buf.timestamp.tv_usec;
303 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
305 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
310 return s->buf_len[buf.index];
313 static int read_frame(struct video_data *s, void *frame, int64_t *ts)
318 static int mmap_start(struct video_data *s)
320 enum v4l2_buf_type type;
323 for (i = 0; i < s->buffers; i++) {
324 struct v4l2_buffer buf;
326 memset(&buf, 0, sizeof(struct v4l2_buffer));
327 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
328 buf.memory = V4L2_MEMORY_MMAP;
331 res = ioctl (s->fd, VIDIOC_QBUF, &buf);
333 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
339 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
340 res = ioctl (s->fd, VIDIOC_STREAMON, &type);
342 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
350 static void mmap_close(struct video_data *s)
352 enum v4l2_buf_type type;
355 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
356 /* We do not check for the result, because we could
357 * not do anything about it anyway...
359 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
360 for (i = 0; i < s->buffers; i++) {
361 munmap(s->buf_start[i], s->buf_len[i]);
363 av_free(s->buf_start);
367 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
369 struct video_data *s = s1->priv_data;
372 int res, frame_rate, frame_rate_base;
373 uint32_t desired_format, capabilities;
374 const char *video_device;
376 if (!ap || ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
377 av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
384 frame_rate = ap->time_base.den;
385 frame_rate_base = ap->time_base.num;
387 if((unsigned)width > 32767 || (unsigned)height > 32767) {
388 av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
393 st = av_new_stream(s1, 0);
397 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
401 s->frame_rate = frame_rate;
402 s->frame_rate_base = frame_rate_base;
404 video_device = ap->device;
406 video_device = "/dev/video";
409 s->fd = device_open(video_device, &capabilities);
415 av_log(s1, AV_LOG_ERROR, "[%d]Capabilities: %x\n", s->fd, capabilities);
417 desired_format = fmt_ff2v4l(ap->pix_fmt);
418 if (desired_format == 0 || (device_init(s->fd, width, height, desired_format) < 0)) {
423 desired_format = fmt_conversion_table[i].v4l2_fmt;
424 if (device_init(s->fd, width, height, desired_format) < 0) {
430 if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
435 if (desired_format == 0) {
436 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
442 s->frame_format = desired_format;
444 st->codec->pix_fmt = fmt_v4l2ff(desired_format);
445 s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
446 if (capabilities & V4L2_CAP_STREAMING) {
447 s->io_method = io_mmap;
451 s->io_method = io_read;
460 s->top_field_first = first_field(s->fd);
462 st->codec->codec_type = CODEC_TYPE_VIDEO;
463 st->codec->codec_id = CODEC_ID_RAWVIDEO;
464 st->codec->width = width;
465 st->codec->height = height;
466 st->codec->time_base.den = frame_rate;
467 st->codec->time_base.num = frame_rate_base;
468 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
473 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
475 struct video_data *s = s1->priv_data;
478 if (av_new_packet(pkt, s->frame_size) < 0)
481 if (s->io_method == io_mmap) {
482 res = mmap_read_frame(s, pkt->data, &pkt->pts);
483 } else if (s->io_method == io_read) {
484 res = read_frame(s, pkt->data, &pkt->pts);
492 if (s1->streams[0]->codec->coded_frame) {
493 s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
494 s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
497 return s->frame_size;
500 static int v4l2_read_close(AVFormatContext *s1)
502 struct video_data *s = s1->priv_data;
504 if (s->io_method == io_mmap) {
512 static AVInputFormat v4l2_format = {
515 sizeof(struct video_data),
520 .flags = AVFMT_NOFILE,
525 av_register_input_format(&v4l2_format);