]> git.sesse.net Git - ffmpeg/blob - libavformat/v4l2.c
print a hint when trying V4L2 on V4L device
[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 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.
17  *
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.
22  *
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
26  */
27 #include "avformat.h"
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <sys/mman.h>
32 #include <sys/time.h>
33 #define _LINUX_TIME_H 1
34 #include <linux/videodev.h>
35 #include <time.h>
36
37 static const int desired_video_buffers = 256;
38
39 enum io_method {
40     io_read,
41     io_mmap,
42     io_userptr
43 };
44
45 struct video_data {
46     int fd;
47     int frame_format; /* V4L2_PIX_FMT_* */
48     enum io_method io_method;
49     int width, height;
50     int frame_rate;
51     int frame_rate_base;
52     int frame_size;
53     int top_field_first;
54
55     int buffers;
56     void **buf_start;
57     unsigned int *buf_len;
58 };
59
60 struct fmt_map {
61     enum PixelFormat ff_fmt;
62     int32_t v4l2_fmt;
63 };
64
65 static struct fmt_map fmt_conversion_table[] = {
66     {
67         .ff_fmt = PIX_FMT_YUV420P,
68         .v4l2_fmt = V4L2_PIX_FMT_YUV420,
69     },
70     {
71         .ff_fmt = PIX_FMT_YUV422P,
72         .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
73     },
74     {
75         .ff_fmt = PIX_FMT_YUV422,
76         .v4l2_fmt = V4L2_PIX_FMT_YUYV,
77     },
78     {
79         .ff_fmt = PIX_FMT_UYVY422,
80         .v4l2_fmt = V4L2_PIX_FMT_UYVY,
81     },
82     {
83         .ff_fmt = PIX_FMT_YUV411P,
84         .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
85     },
86     {
87         .ff_fmt = PIX_FMT_YUV410P,
88         .v4l2_fmt = V4L2_PIX_FMT_YUV410,
89     },
90     {
91         .ff_fmt = PIX_FMT_BGR24,
92         .v4l2_fmt = V4L2_PIX_FMT_BGR24,
93     },
94     {
95         .ff_fmt = PIX_FMT_RGB24,
96         .v4l2_fmt = V4L2_PIX_FMT_RGB24,
97     },
98     /*
99     {
100         .ff_fmt = PIX_FMT_RGBA32,
101         .v4l2_fmt = V4L2_PIX_FMT_BGR32,
102     },
103     */
104     {
105         .ff_fmt = PIX_FMT_GRAY8,
106         .v4l2_fmt = V4L2_PIX_FMT_GREY,
107     },
108 };
109
110 static int device_open(const char *devname, uint32_t *capabilities)
111 {
112     struct v4l2_capability cap;
113     int fd;
114     int res;
115
116     fd = open(devname, O_RDWR /*| O_NONBLOCK*/, 0);
117     if (fd < 0) {
118         av_log(NULL, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
119                  devname, strerror(errno));
120
121         return -1;
122     }
123
124     res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
125     // ENOIOCTLCMD definition only availble on __KERNEL__
126     if (res < 0 && errno == 515)
127     {
128         av_log(NULL, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
129
130         return -1;
131     }
132     if (res < 0) {
133         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
134                  strerror(errno));
135
136         return -1;
137     }
138     if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
139         av_log(NULL, AV_LOG_ERROR, "Not a video capture device\n");
140
141         return -1;
142     }
143     *capabilities = cap.capabilities;
144
145     return fd;
146 }
147
148 static int device_init(int fd, int width, int height, int pix_fmt)
149 {
150     struct v4l2_format fmt;
151
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);
159 }
160
161 static int first_field(int fd)
162 {
163     int res;
164     v4l2_std_id std;
165
166     res = ioctl(fd, VIDIOC_G_STD, &std);
167     if (res < 0) {
168         return 0;
169     }
170     if (std & V4L2_STD_NTSC) {
171         return 0;
172     }
173
174     return 1;
175 }
176
177 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
178 {
179     int i;
180
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;
184         }
185     }
186
187     return 0;
188 }
189
190 static enum PixelFormat fmt_v4l2ff(uint32_t 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].v4l2_fmt == pix_fmt) {
196             return fmt_conversion_table[i].ff_fmt;
197         }
198     }
199
200     return -1;
201 }
202
203 static int mmap_init(struct video_data *s)
204 {
205     struct v4l2_requestbuffers req;
206     int i, res;
207
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);
213     if (res < 0) {
214         if (errno == EINVAL) {
215             av_log(NULL, AV_LOG_ERROR, "Device does not support mmap\n");
216         } else {
217             av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
218         }
219
220         return -1;
221     }
222
223     if (req.count < 2) {
224         av_log(NULL, AV_LOG_ERROR, "Insufficient buffer memory\n");
225
226         return -1;
227     }
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");
232
233         return -1;
234     }
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);
239
240         return -1;
241     }
242
243     for (i = 0; i < req.count; i++) {
244         struct v4l2_buffer buf;
245
246         memset(&buf, 0, sizeof(struct v4l2_buffer));
247         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
248         buf.memory = V4L2_MEMORY_MMAP;
249         buf.index = i;
250         res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
251         if (res < 0) {
252             av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
253
254             return -1;
255         }
256
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);
260
261             return -1;
262         }
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));
267
268             return -1;
269         }
270     }
271
272     return 0;
273 }
274
275 static int read_init(struct video_data *s)
276 {
277     return -1;
278 }
279
280 static int mmap_read_frame(struct video_data *s, void *frame, int64_t *ts)
281 {
282     struct v4l2_buffer buf;
283     int res;
284
285     memset(&buf, 0, sizeof(struct v4l2_buffer));
286     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
287     buf.memory = V4L2_MEMORY_MMAP;
288
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)));
292     if (res < 0) {
293         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
294
295         return -1;
296     }
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;
302
303     res = ioctl (s->fd, VIDIOC_QBUF, &buf);
304     if (res < 0) {
305         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
306
307         return -1;
308     }
309
310     return s->buf_len[buf.index];
311 }
312
313 static int read_frame(struct video_data *s, void *frame, int64_t *ts)
314 {
315     return -1;
316 }
317
318 static int mmap_start(struct video_data *s)
319 {
320     enum v4l2_buf_type type;
321     int i, res;
322
323     for (i = 0; i < s->buffers; i++) {
324         struct v4l2_buffer buf;
325
326         memset(&buf, 0, sizeof(struct v4l2_buffer));
327         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
328         buf.memory = V4L2_MEMORY_MMAP;
329         buf.index  = i;
330
331         res = ioctl (s->fd, VIDIOC_QBUF, &buf);
332         if (res < 0) {
333             av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
334
335             return -1;
336         }
337     }
338
339     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
340     res = ioctl (s->fd, VIDIOC_STREAMON, &type);
341     if (res < 0) {
342         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
343
344         return -1;
345     }
346
347     return 0;
348 }
349
350 static void mmap_close(struct video_data *s)
351 {
352     enum v4l2_buf_type type;
353     int i;
354
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...
358      */
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]);
362     }
363     av_free(s->buf_start);
364     av_free(s->buf_len);
365 }
366
367 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
368 {
369     struct video_data *s = s1->priv_data;
370     AVStream *st;
371     int width, height;
372     int res, frame_rate, frame_rate_base;
373     uint32_t desired_format, capabilities;
374     const char *video_device;
375
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");
378
379         return -1;
380     }
381
382     width = ap->width;
383     height = ap->height;
384     frame_rate = ap->time_base.den;
385     frame_rate_base = ap->time_base.num;
386
387     if((unsigned)width > 32767 || (unsigned)height > 32767) {
388         av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
389
390         return -1;
391     }
392
393     st = av_new_stream(s1, 0);
394     if (!st) {
395         return -ENOMEM;
396     }
397     av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
398
399     s->width = width;
400     s->height = height;
401     s->frame_rate      = frame_rate;
402     s->frame_rate_base = frame_rate_base;
403
404     video_device = ap->device;
405     if (!video_device) {
406         video_device = "/dev/video";
407     }
408     capabilities = 0;
409     s->fd = device_open(video_device, &capabilities);
410     if (s->fd < 0) {
411         av_free(st);
412
413         return AVERROR_IO;
414     }
415     av_log(s1, AV_LOG_ERROR, "[%d]Capabilities: %x\n", s->fd, capabilities);
416
417     desired_format = fmt_ff2v4l(ap->pix_fmt);
418     if (desired_format == 0 || (device_init(s->fd, width, height, desired_format) < 0)) {
419         int i, done;
420
421         done = 0; i = 0;
422         while (!done) {
423             desired_format = fmt_conversion_table[i].v4l2_fmt;
424             if (device_init(s->fd, width, height, desired_format) < 0) {
425                 desired_format = 0;
426                 i++;
427             } else {
428                done = 1;
429             }
430             if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
431                done = 1;
432             }
433         }
434     }
435     if (desired_format == 0) {
436         av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
437         close(s->fd);
438         av_free(st);
439
440         return AVERROR_IO;
441     }
442     s->frame_format = desired_format;
443
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;
448         res = mmap_init(s);
449         res = mmap_start(s);
450     } else {
451         s->io_method = io_read;
452         res = read_init(s);
453     }
454     if (res < 0) {
455         close(s->fd);
456         av_free(st);
457
458         return AVERROR_IO;
459     }
460     s->top_field_first = first_field(s->fd);
461
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;
469
470     return 0;
471 }
472
473 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
474 {
475     struct video_data *s = s1->priv_data;
476     int res;
477
478     if (av_new_packet(pkt, s->frame_size) < 0)
479         return AVERROR_IO;
480
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);
485     } else {
486         return AVERROR_IO;
487     }
488     if (res < 0) {
489         return AVERROR_IO;
490     }
491
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;
495     }
496
497     return s->frame_size;
498 }
499
500 static int v4l2_read_close(AVFormatContext *s1)
501 {
502     struct video_data *s = s1->priv_data;
503
504     if (s->io_method == io_mmap) {
505         mmap_close(s);
506     }
507
508     close(s->fd);
509     return 0;
510 }
511
512 static AVInputFormat v4l2_format = {
513     "video4linux2",
514     "video grab",
515     sizeof(struct video_data),
516     NULL,
517     v4l2_read_header,
518     v4l2_read_packet,
519     v4l2_read_close,
520     .flags = AVFMT_NOFILE,
521 };
522
523 int v4l2_init(void)
524 {
525     av_register_input_format(&v4l2_format);
526     return 0;
527 }