]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/v4l2.c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
[frescor/ffmpeg.git] / 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     assert(buf.bytesused == s->frame_size);
312     /* Image is at s->buff_start[buf.index] */
313     memcpy(frame, s->buf_start[buf.index], buf.bytesused);
314     *ts = buf.timestamp.tv_sec * int64_t_C(1000000) + buf.timestamp.tv_usec;
315
316     res = ioctl (s->fd, VIDIOC_QBUF, &buf);
317     if (res < 0) {
318         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
319
320         return -1;
321     }
322
323     return s->buf_len[buf.index];
324 }
325
326 static int read_frame(struct video_data *s, void *frame, int64_t *ts)
327 {
328     return -1;
329 }
330
331 static int mmap_start(struct video_data *s)
332 {
333     enum v4l2_buf_type type;
334     int i, res;
335
336     for (i = 0; i < s->buffers; i++) {
337         struct v4l2_buffer buf;
338
339         memset(&buf, 0, sizeof(struct v4l2_buffer));
340         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
341         buf.memory = V4L2_MEMORY_MMAP;
342         buf.index  = i;
343
344         res = ioctl (s->fd, VIDIOC_QBUF, &buf);
345         if (res < 0) {
346             av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
347
348             return -1;
349         }
350     }
351
352     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
353     res = ioctl (s->fd, VIDIOC_STREAMON, &type);
354     if (res < 0) {
355         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
356
357         return -1;
358     }
359
360     return 0;
361 }
362
363 static void mmap_close(struct video_data *s)
364 {
365     enum v4l2_buf_type type;
366     int i;
367
368     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
369     /* We do not check for the result, because we could
370      * not do anything about it anyway...
371      */
372     ioctl(s->fd, VIDIOC_STREAMOFF, &type);
373     for (i = 0; i < s->buffers; i++) {
374         munmap(s->buf_start[i], s->buf_len[i]);
375     }
376     av_free(s->buf_start);
377     av_free(s->buf_len);
378 }
379
380 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
381 {
382     struct video_data *s = s1->priv_data;
383     AVStream *st;
384     int width, height;
385     int res, frame_rate, frame_rate_base;
386     uint32_t desired_format, capabilities;
387     const char *video_device;
388
389     if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
390         av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
391
392         return -1;
393     }
394
395     width = ap->width;
396     height = ap->height;
397     frame_rate = ap->time_base.den;
398     frame_rate_base = ap->time_base.num;
399
400     if((unsigned)width > 32767 || (unsigned)height > 32767) {
401         av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
402
403         return -1;
404     }
405
406     st = av_new_stream(s1, 0);
407     if (!st) {
408         return -ENOMEM;
409     }
410     av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
411
412     s->width = width;
413     s->height = height;
414     s->frame_rate      = frame_rate;
415     s->frame_rate_base = frame_rate_base;
416
417     video_device = ap->device;
418     if (!video_device) {
419         video_device = "/dev/video";
420     }
421     capabilities = 0;
422     s->fd = device_open(video_device, &capabilities);
423     if (s->fd < 0) {
424         av_free(st);
425
426         return AVERROR_IO;
427     }
428     av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
429
430     desired_format = fmt_ff2v4l(ap->pix_fmt);
431     if (desired_format == 0 || (device_init(s->fd, &width, &height, desired_format) < 0)) {
432         int i, done;
433
434         done = 0; i = 0;
435         while (!done) {
436             desired_format = fmt_conversion_table[i].v4l2_fmt;
437             if (device_init(s->fd, &width, &height, desired_format) < 0) {
438                 desired_format = 0;
439                 i++;
440             } else {
441                done = 1;
442             }
443             if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
444                done = 1;
445             }
446         }
447     }
448     if (desired_format == 0) {
449         av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
450         close(s->fd);
451         av_free(st);
452
453         return AVERROR_IO;
454     }
455     s->frame_format = desired_format;
456
457     st->codec->pix_fmt = fmt_v4l2ff(desired_format);
458     s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
459     if (capabilities & V4L2_CAP_STREAMING) {
460         s->io_method = io_mmap;
461         res = mmap_init(s);
462         if (res == 0) {
463             res = mmap_start(s);
464         }
465     } else {
466         s->io_method = io_read;
467         res = read_init(s);
468     }
469     if (res < 0) {
470         close(s->fd);
471         av_free(st);
472
473         return AVERROR_IO;
474     }
475     s->top_field_first = first_field(s->fd);
476
477     st->codec->codec_type = CODEC_TYPE_VIDEO;
478     st->codec->codec_id = CODEC_ID_RAWVIDEO;
479     st->codec->width = width;
480     st->codec->height = height;
481     st->codec->time_base.den = frame_rate;
482     st->codec->time_base.num = frame_rate_base;
483     st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
484
485     return 0;
486 }
487
488 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
489 {
490     struct video_data *s = s1->priv_data;
491     int res;
492
493     if (av_new_packet(pkt, s->frame_size) < 0)
494         return AVERROR_IO;
495
496     if (s->io_method == io_mmap) {
497         res = mmap_read_frame(s, pkt->data, &pkt->pts);
498     } else if (s->io_method == io_read) {
499         res = read_frame(s, pkt->data, &pkt->pts);
500     } else {
501         return AVERROR_IO;
502     }
503     if (res < 0) {
504         return AVERROR_IO;
505     }
506
507     if (s1->streams[0]->codec->coded_frame) {
508         s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
509         s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
510     }
511
512     return s->frame_size;
513 }
514
515 static int v4l2_read_close(AVFormatContext *s1)
516 {
517     struct video_data *s = s1->priv_data;
518
519     if (s->io_method == io_mmap) {
520         mmap_close(s);
521     }
522
523     close(s->fd);
524     return 0;
525 }
526
527 AVInputFormat v4l2_demuxer = {
528     "video4linux2",
529     "video grab",
530     sizeof(struct video_data),
531     NULL,
532     v4l2_read_header,
533     v4l2_read_packet,
534     v4l2_read_close,
535     .flags = AVFMT_NOFILE,
536 };