]> git.sesse.net Git - ffmpeg/blob - libavformat/v4l2.c
use more portable PRIu64
[ffmpeg] / libavformat / v4l2.c
1 /*
2  * Video4Linux2 grab interface
3  * Copyright (c) 2000,2001 Fabrice Bellard.
4  * Copyright (c) 2006 Luca Abeni.
5  *
6  * Part of this file is based on the V4L2 video capture example
7  * (http://v4l2spec.bytesex.org/v4l2spec/capture.c)
8  *
9  * Thanks to Michael Niedermayer for providing the mapping between
10  * V4L2_PIX_FMT_* and PIX_FMT_*
11  *
12  *
13  * This file is part of FFmpeg.
14  *
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.
19  *
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.
24  *
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
28  */
29 #include "avformat.h"
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/ioctl.h>
33 #include <sys/mman.h>
34 #include <sys/time.h>
35 #include <asm/types.h>
36 #include <linux/videodev2.h>
37 #include <time.h>
38
39 static const int desired_video_buffers = 256;
40
41 enum io_method {
42     io_read,
43     io_mmap,
44     io_userptr
45 };
46
47 struct video_data {
48     int fd;
49     int frame_format; /* V4L2_PIX_FMT_* */
50     enum io_method io_method;
51     int width, height;
52     int frame_rate;
53     int frame_rate_base;
54     int frame_size;
55     int top_field_first;
56
57     int buffers;
58     void **buf_start;
59     unsigned int *buf_len;
60 };
61
62 struct fmt_map {
63     enum PixelFormat ff_fmt;
64     int32_t v4l2_fmt;
65 };
66
67 static struct fmt_map fmt_conversion_table[] = {
68     {
69         .ff_fmt = PIX_FMT_YUV420P,
70         .v4l2_fmt = V4L2_PIX_FMT_YUV420,
71     },
72     {
73         .ff_fmt = PIX_FMT_YUV422P,
74         .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
75     },
76     {
77         .ff_fmt = PIX_FMT_YUV422,
78         .v4l2_fmt = V4L2_PIX_FMT_YUYV,
79     },
80     {
81         .ff_fmt = PIX_FMT_UYVY422,
82         .v4l2_fmt = V4L2_PIX_FMT_UYVY,
83     },
84     {
85         .ff_fmt = PIX_FMT_YUV411P,
86         .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
87     },
88     {
89         .ff_fmt = PIX_FMT_YUV410P,
90         .v4l2_fmt = V4L2_PIX_FMT_YUV410,
91     },
92     {
93         .ff_fmt = PIX_FMT_BGR24,
94         .v4l2_fmt = V4L2_PIX_FMT_BGR24,
95     },
96     {
97         .ff_fmt = PIX_FMT_RGB24,
98         .v4l2_fmt = V4L2_PIX_FMT_RGB24,
99     },
100     /*
101     {
102         .ff_fmt = PIX_FMT_RGBA32,
103         .v4l2_fmt = V4L2_PIX_FMT_BGR32,
104     },
105     */
106     {
107         .ff_fmt = PIX_FMT_GRAY8,
108         .v4l2_fmt = V4L2_PIX_FMT_GREY,
109     },
110 };
111
112 static int device_open(const char *devname, uint32_t *capabilities)
113 {
114     struct v4l2_capability cap;
115     int fd;
116     int res;
117
118     fd = open(devname, O_RDWR /*| O_NONBLOCK*/, 0);
119     if (fd < 0) {
120         av_log(NULL, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
121                  devname, strerror(errno));
122
123         return -1;
124     }
125
126     res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
127     // ENOIOCTLCMD definition only availble on __KERNEL__
128     if (res < 0 && errno == 515)
129     {
130         av_log(NULL, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
131         close(fd);
132
133         return -1;
134     }
135     if (res < 0) {
136         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
137                  strerror(errno));
138         close(fd);
139
140         return -1;
141     }
142     if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
143         av_log(NULL, AV_LOG_ERROR, "Not a video capture device\n");
144         close(fd);
145
146         return -1;
147     }
148     *capabilities = cap.capabilities;
149
150     return fd;
151 }
152
153 static int device_init(int fd, int *width, int *height, int pix_fmt)
154 {
155     struct v4l2_format fmt;
156     int res;
157
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;
169     }
170
171     return res;
172 }
173
174 static int first_field(int fd)
175 {
176     int res;
177     v4l2_std_id std;
178
179     res = ioctl(fd, VIDIOC_G_STD, &std);
180     if (res < 0) {
181         return 0;
182     }
183     if (std & V4L2_STD_NTSC) {
184         return 0;
185     }
186
187     return 1;
188 }
189
190 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
191 {
192     int i;
193
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;
197         }
198     }
199
200     return 0;
201 }
202
203 static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
204 {
205     int i;
206
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;
210         }
211     }
212
213     return -1;
214 }
215
216 static int mmap_init(struct video_data *s)
217 {
218     struct v4l2_requestbuffers req;
219     int i, res;
220
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);
226     if (res < 0) {
227         if (errno == EINVAL) {
228             av_log(NULL, AV_LOG_ERROR, "Device does not support mmap\n");
229         } else {
230             av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
231         }
232
233         return -1;
234     }
235
236     if (req.count < 2) {
237         av_log(NULL, AV_LOG_ERROR, "Insufficient buffer memory\n");
238
239         return -1;
240     }
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");
245
246         return -1;
247     }
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);
252
253         return -1;
254     }
255
256     for (i = 0; i < req.count; i++) {
257         struct v4l2_buffer buf;
258
259         memset(&buf, 0, sizeof(struct v4l2_buffer));
260         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
261         buf.memory = V4L2_MEMORY_MMAP;
262         buf.index = i;
263         res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
264         if (res < 0) {
265             av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
266
267             return -1;
268         }
269
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);
273
274             return -1;
275         }
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));
280
281             return -1;
282         }
283     }
284
285     return 0;
286 }
287
288 static int read_init(struct video_data *s)
289 {
290     return -1;
291 }
292
293 static int mmap_read_frame(struct video_data *s, void *frame, int64_t *ts)
294 {
295     struct v4l2_buffer buf;
296     int res;
297
298     memset(&buf, 0, sizeof(struct v4l2_buffer));
299     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
300     buf.memory = V4L2_MEMORY_MMAP;
301
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)));
305     if (res < 0) {
306         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
307
308         return -1;
309     }
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);
313
314         return -1;
315     }
316
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_C(1000000) + buf.timestamp.tv_usec;
320
321     res = ioctl (s->fd, VIDIOC_QBUF, &buf);
322     if (res < 0) {
323         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
324
325         return -1;
326     }
327
328     return s->buf_len[buf.index];
329 }
330
331 static int read_frame(struct video_data *s, void *frame, int64_t *ts)
332 {
333     return -1;
334 }
335
336 static int mmap_start(struct video_data *s)
337 {
338     enum v4l2_buf_type type;
339     int i, res;
340
341     for (i = 0; i < s->buffers; i++) {
342         struct v4l2_buffer buf;
343
344         memset(&buf, 0, sizeof(struct v4l2_buffer));
345         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
346         buf.memory = V4L2_MEMORY_MMAP;
347         buf.index  = i;
348
349         res = ioctl (s->fd, VIDIOC_QBUF, &buf);
350         if (res < 0) {
351             av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
352
353             return -1;
354         }
355     }
356
357     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
358     res = ioctl (s->fd, VIDIOC_STREAMON, &type);
359     if (res < 0) {
360         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
361
362         return -1;
363     }
364
365     return 0;
366 }
367
368 static void mmap_close(struct video_data *s)
369 {
370     enum v4l2_buf_type type;
371     int i;
372
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...
376      */
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]);
380     }
381     av_free(s->buf_start);
382     av_free(s->buf_len);
383 }
384
385 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
386 {
387     struct video_data *s = s1->priv_data;
388     AVStream *st;
389     int width, height;
390     int res, frame_rate, frame_rate_base;
391     uint32_t desired_format, capabilities;
392     const char *video_device;
393
394     if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
395         av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
396
397         return -1;
398     }
399
400     width = ap->width;
401     height = ap->height;
402     frame_rate = ap->time_base.den;
403     frame_rate_base = ap->time_base.num;
404
405     if((unsigned)width > 32767 || (unsigned)height > 32767) {
406         av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
407
408         return -1;
409     }
410
411     st = av_new_stream(s1, 0);
412     if (!st) {
413         return -ENOMEM;
414     }
415     av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
416
417     s->width = width;
418     s->height = height;
419     s->frame_rate      = frame_rate;
420     s->frame_rate_base = frame_rate_base;
421
422     video_device = ap->device;
423     if (!video_device) {
424         video_device = "/dev/video";
425     }
426     capabilities = 0;
427     s->fd = device_open(video_device, &capabilities);
428     if (s->fd < 0) {
429         av_free(st);
430
431         return AVERROR_IO;
432     }
433     av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
434
435     desired_format = fmt_ff2v4l(ap->pix_fmt);
436     if (desired_format == 0 || (device_init(s->fd, &width, &height, desired_format) < 0)) {
437         int i, done;
438
439         done = 0; i = 0;
440         while (!done) {
441             desired_format = fmt_conversion_table[i].v4l2_fmt;
442             if (device_init(s->fd, &width, &height, desired_format) < 0) {
443                 desired_format = 0;
444                 i++;
445             } else {
446                done = 1;
447             }
448             if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
449                done = 1;
450             }
451         }
452     }
453     if (desired_format == 0) {
454         av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
455         close(s->fd);
456         av_free(st);
457
458         return AVERROR_IO;
459     }
460     s->frame_format = desired_format;
461
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;
466         res = mmap_init(s);
467         if (res == 0) {
468             res = mmap_start(s);
469         }
470     } else {
471         s->io_method = io_read;
472         res = read_init(s);
473     }
474     if (res < 0) {
475         close(s->fd);
476         av_free(st);
477
478         return AVERROR_IO;
479     }
480     s->top_field_first = first_field(s->fd);
481
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;
489
490     return 0;
491 }
492
493 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
494 {
495     struct video_data *s = s1->priv_data;
496     int res;
497
498     if (av_new_packet(pkt, s->frame_size) < 0)
499         return AVERROR_IO;
500
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);
505     } else {
506         return AVERROR_IO;
507     }
508     if (res < 0) {
509         return AVERROR_IO;
510     }
511
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;
515     }
516
517     return s->frame_size;
518 }
519
520 static int v4l2_read_close(AVFormatContext *s1)
521 {
522     struct video_data *s = s1->priv_data;
523
524     if (s->io_method == io_mmap) {
525         mmap_close(s);
526     }
527
528     close(s->fd);
529     return 0;
530 }
531
532 AVInputFormat v4l2_demuxer = {
533     "video4linux2",
534     "video grab",
535     sizeof(struct video_data),
536     NULL,
537     v4l2_read_header,
538     v4l2_read_packet,
539     v4l2_read_close,
540     .flags = AVFMT_NOFILE,
541 };