]> git.sesse.net Git - ffmpeg/blob - libavdevice/v4l2.c
Add a mapping for the V4L2_PIX_FMT_NV12 format to PIX_FMT_NV12 for
[ffmpeg] / libavdevice / 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
30 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
31 #include "config.h"
32 #include "libavformat/avformat.h"
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/ioctl.h>
36 #include <sys/mman.h>
37 #include <sys/time.h>
38 #if HAVE_SYS_VIDEOIO_H
39 #include <sys/videoio.h>
40 #else
41 #include <asm/types.h>
42 #include <linux/videodev2.h>
43 #endif
44 #include <time.h>
45 #include <strings.h>
46 #include "libavcore/imgutils.h"
47
48 static const int desired_video_buffers = 256;
49
50 enum io_method {
51     io_read,
52     io_mmap,
53     io_userptr
54 };
55
56 struct video_data {
57     int fd;
58     int frame_format; /* V4L2_PIX_FMT_* */
59     enum io_method io_method;
60     int width, height;
61     int frame_size;
62     int top_field_first;
63
64     int buffers;
65     void **buf_start;
66     unsigned int *buf_len;
67 };
68
69 struct buff_data {
70     int index;
71     int fd;
72 };
73
74 struct fmt_map {
75     enum PixelFormat ff_fmt;
76     enum CodecID codec_id;
77     uint32_t v4l2_fmt;
78 };
79
80 static struct fmt_map fmt_conversion_table[] = {
81     {
82         .ff_fmt = PIX_FMT_YUV420P,
83         .codec_id = CODEC_ID_RAWVIDEO,
84         .v4l2_fmt = V4L2_PIX_FMT_YUV420,
85     },
86     {
87         .ff_fmt = PIX_FMT_YUV422P,
88         .codec_id = CODEC_ID_RAWVIDEO,
89         .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
90     },
91     {
92         .ff_fmt = PIX_FMT_YUYV422,
93         .codec_id = CODEC_ID_RAWVIDEO,
94         .v4l2_fmt = V4L2_PIX_FMT_YUYV,
95     },
96     {
97         .ff_fmt = PIX_FMT_UYVY422,
98         .codec_id = CODEC_ID_RAWVIDEO,
99         .v4l2_fmt = V4L2_PIX_FMT_UYVY,
100     },
101     {
102         .ff_fmt = PIX_FMT_YUV411P,
103         .codec_id = CODEC_ID_RAWVIDEO,
104         .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
105     },
106     {
107         .ff_fmt = PIX_FMT_YUV410P,
108         .codec_id = CODEC_ID_RAWVIDEO,
109         .v4l2_fmt = V4L2_PIX_FMT_YUV410,
110     },
111     {
112         .ff_fmt = PIX_FMT_RGB555,
113         .codec_id = CODEC_ID_RAWVIDEO,
114         .v4l2_fmt = V4L2_PIX_FMT_RGB555,
115     },
116     {
117         .ff_fmt = PIX_FMT_RGB565,
118         .codec_id = CODEC_ID_RAWVIDEO,
119         .v4l2_fmt = V4L2_PIX_FMT_RGB565,
120     },
121     {
122         .ff_fmt = PIX_FMT_BGR24,
123         .codec_id = CODEC_ID_RAWVIDEO,
124         .v4l2_fmt = V4L2_PIX_FMT_BGR24,
125     },
126     {
127         .ff_fmt = PIX_FMT_RGB24,
128         .codec_id = CODEC_ID_RAWVIDEO,
129         .v4l2_fmt = V4L2_PIX_FMT_RGB24,
130     },
131     {
132         .ff_fmt = PIX_FMT_BGRA,
133         .codec_id = CODEC_ID_RAWVIDEO,
134         .v4l2_fmt = V4L2_PIX_FMT_BGR32,
135     },
136     {
137         .ff_fmt = PIX_FMT_GRAY8,
138         .codec_id = CODEC_ID_RAWVIDEO,
139         .v4l2_fmt = V4L2_PIX_FMT_GREY,
140     },
141     {
142         .ff_fmt = PIX_FMT_NV12,
143         .codec_id = CODEC_ID_RAWVIDEO,
144         .v4l2_fmt = V4L2_PIX_FMT_NV12,
145     },
146     {
147         .ff_fmt = PIX_FMT_NONE,
148         .codec_id = CODEC_ID_MJPEG,
149         .v4l2_fmt = V4L2_PIX_FMT_MJPEG,
150     },
151     {
152         .ff_fmt = PIX_FMT_NONE,
153         .codec_id = CODEC_ID_MJPEG,
154         .v4l2_fmt = V4L2_PIX_FMT_JPEG,
155     },
156 };
157
158 static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
159 {
160     struct v4l2_capability cap;
161     int fd;
162     int res, err;
163     int flags = O_RDWR;
164
165     if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
166         flags |= O_NONBLOCK;
167     }
168     fd = open(ctx->filename, flags, 0);
169     if (fd < 0) {
170         av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
171                  ctx->filename, strerror(errno));
172
173         return AVERROR(errno);
174     }
175
176     res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
177     // ENOIOCTLCMD definition only availble on __KERNEL__
178     if (res < 0 && ((err = errno) == 515)) {
179         av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
180         close(fd);
181
182         return AVERROR(515);
183     }
184     if (res < 0) {
185         av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
186                  strerror(errno));
187         close(fd);
188
189         return AVERROR(err);
190     }
191     if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
192         av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
193         close(fd);
194
195         return AVERROR(ENODEV);
196     }
197     *capabilities = cap.capabilities;
198
199     return fd;
200 }
201
202 static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
203 {
204     struct video_data *s = ctx->priv_data;
205     int fd = s->fd;
206     struct v4l2_format fmt;
207     int res;
208
209     memset(&fmt, 0, sizeof(struct v4l2_format));
210     fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
211     fmt.fmt.pix.width = *width;
212     fmt.fmt.pix.height = *height;
213     fmt.fmt.pix.pixelformat = pix_fmt;
214     fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
215     res = ioctl(fd, VIDIOC_S_FMT, &fmt);
216     if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
217         av_log(ctx, 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);
218         *width = fmt.fmt.pix.width;
219         *height = fmt.fmt.pix.height;
220     }
221
222     if (pix_fmt != fmt.fmt.pix.pixelformat) {
223         av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver changed the pixel format from 0x%08X to 0x%08X\n", pix_fmt, fmt.fmt.pix.pixelformat);
224         res = -1;
225     }
226
227     return res;
228 }
229
230 static int first_field(int fd)
231 {
232     int res;
233     v4l2_std_id std;
234
235     res = ioctl(fd, VIDIOC_G_STD, &std);
236     if (res < 0) {
237         return 0;
238     }
239     if (std & V4L2_STD_NTSC) {
240         return 0;
241     }
242
243     return 1;
244 }
245
246 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt, enum CodecID codec_id)
247 {
248     int i;
249
250     for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
251         if ((codec_id == CODEC_ID_NONE ||
252              fmt_conversion_table[i].codec_id == codec_id) &&
253             (pix_fmt == PIX_FMT_NONE ||
254              fmt_conversion_table[i].ff_fmt == pix_fmt)) {
255             return fmt_conversion_table[i].v4l2_fmt;
256         }
257     }
258
259     return 0;
260 }
261
262 static enum PixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum CodecID codec_id)
263 {
264     int i;
265
266     for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
267         if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
268             fmt_conversion_table[i].codec_id == codec_id) {
269             return fmt_conversion_table[i].ff_fmt;
270         }
271     }
272
273     return PIX_FMT_NONE;
274 }
275
276 static enum CodecID fmt_v4l2codec(uint32_t v4l2_fmt)
277 {
278     int i;
279
280     for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
281         if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
282             return fmt_conversion_table[i].codec_id;
283         }
284     }
285
286     return CODEC_ID_NONE;
287 }
288
289 static int mmap_init(AVFormatContext *ctx)
290 {
291     struct video_data *s = ctx->priv_data;
292     struct v4l2_requestbuffers req;
293     int i, res;
294
295     memset(&req, 0, sizeof(struct v4l2_requestbuffers));
296     req.count = desired_video_buffers;
297     req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
298     req.memory = V4L2_MEMORY_MMAP;
299     res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
300     if (res < 0) {
301         if (errno == EINVAL) {
302             av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
303         } else {
304             av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
305         }
306
307         return AVERROR(errno);
308     }
309
310     if (req.count < 2) {
311         av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
312
313         return AVERROR(ENOMEM);
314     }
315     s->buffers = req.count;
316     s->buf_start = av_malloc(sizeof(void *) * s->buffers);
317     if (s->buf_start == NULL) {
318         av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
319
320         return AVERROR(ENOMEM);
321     }
322     s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
323     if (s->buf_len == NULL) {
324         av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
325         av_free(s->buf_start);
326
327         return AVERROR(ENOMEM);
328     }
329
330     for (i = 0; i < req.count; i++) {
331         struct v4l2_buffer buf;
332
333         memset(&buf, 0, sizeof(struct v4l2_buffer));
334         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
335         buf.memory = V4L2_MEMORY_MMAP;
336         buf.index = i;
337         res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
338         if (res < 0) {
339             av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
340
341             return AVERROR(errno);
342         }
343
344         s->buf_len[i] = buf.length;
345         if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
346             av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
347
348             return -1;
349         }
350         s->buf_start[i] = mmap (NULL, buf.length,
351                         PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
352         if (s->buf_start[i] == MAP_FAILED) {
353             av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
354
355             return AVERROR(errno);
356         }
357     }
358
359     return 0;
360 }
361
362 static int read_init(AVFormatContext *ctx)
363 {
364     return -1;
365 }
366
367 static void mmap_release_buffer(AVPacket *pkt)
368 {
369     struct v4l2_buffer buf;
370     int res, fd;
371     struct buff_data *buf_descriptor = pkt->priv;
372
373     if (pkt->data == NULL) {
374          return;
375     }
376
377     memset(&buf, 0, sizeof(struct v4l2_buffer));
378     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
379     buf.memory = V4L2_MEMORY_MMAP;
380     buf.index = buf_descriptor->index;
381     fd = buf_descriptor->fd;
382     av_free(buf_descriptor);
383
384     res = ioctl (fd, VIDIOC_QBUF, &buf);
385     if (res < 0) {
386         av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
387     }
388     pkt->data = NULL;
389     pkt->size = 0;
390 }
391
392 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
393 {
394     struct video_data *s = ctx->priv_data;
395     struct v4l2_buffer buf;
396     struct buff_data *buf_descriptor;
397     int res;
398
399     memset(&buf, 0, sizeof(struct v4l2_buffer));
400     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
401     buf.memory = V4L2_MEMORY_MMAP;
402
403     /* FIXME: Some special treatment might be needed in case of loss of signal... */
404     while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
405     if (res < 0) {
406         if (errno == EAGAIN) {
407             pkt->size = 0;
408
409             return AVERROR(EAGAIN);
410         }
411         av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
412
413         return AVERROR(errno);
414     }
415     assert (buf.index < s->buffers);
416     if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
417         av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
418
419         return AVERROR_INVALIDDATA;
420     }
421
422     /* Image is at s->buff_start[buf.index] */
423     pkt->data= s->buf_start[buf.index];
424     pkt->size = buf.bytesused;
425     pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
426     pkt->destruct = mmap_release_buffer;
427     buf_descriptor = av_malloc(sizeof(struct buff_data));
428     if (buf_descriptor == NULL) {
429         /* Something went wrong... Since av_malloc() failed, we cannot even
430          * allocate a buffer for memcopying into it
431          */
432         av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
433         res = ioctl (s->fd, VIDIOC_QBUF, &buf);
434
435         return AVERROR(ENOMEM);
436     }
437     buf_descriptor->fd = s->fd;
438     buf_descriptor->index = buf.index;
439     pkt->priv = buf_descriptor;
440
441     return s->buf_len[buf.index];
442 }
443
444 static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
445 {
446     return -1;
447 }
448
449 static int mmap_start(AVFormatContext *ctx)
450 {
451     struct video_data *s = ctx->priv_data;
452     enum v4l2_buf_type type;
453     int i, res;
454
455     for (i = 0; i < s->buffers; i++) {
456         struct v4l2_buffer buf;
457
458         memset(&buf, 0, sizeof(struct v4l2_buffer));
459         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
460         buf.memory = V4L2_MEMORY_MMAP;
461         buf.index  = i;
462
463         res = ioctl (s->fd, VIDIOC_QBUF, &buf);
464         if (res < 0) {
465             av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
466
467             return AVERROR(errno);
468         }
469     }
470
471     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
472     res = ioctl (s->fd, VIDIOC_STREAMON, &type);
473     if (res < 0) {
474         av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
475
476         return AVERROR(errno);
477     }
478
479     return 0;
480 }
481
482 static void mmap_close(struct video_data *s)
483 {
484     enum v4l2_buf_type type;
485     int i;
486
487     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
488     /* We do not check for the result, because we could
489      * not do anything about it anyway...
490      */
491     ioctl(s->fd, VIDIOC_STREAMOFF, &type);
492     for (i = 0; i < s->buffers; i++) {
493         munmap(s->buf_start[i], s->buf_len[i]);
494     }
495     av_free(s->buf_start);
496     av_free(s->buf_len);
497 }
498
499 static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
500 {
501     struct video_data *s = s1->priv_data;
502     struct v4l2_input input;
503     struct v4l2_standard standard;
504     int i;
505
506     if(ap->channel>=0) {
507         /* set tv video input */
508         memset (&input, 0, sizeof (input));
509         input.index = ap->channel;
510         if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
511             av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
512             return AVERROR(EIO);
513         }
514
515         av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
516                ap->channel, input.name);
517         if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
518             av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
519                    ap->channel);
520             return AVERROR(EIO);
521         }
522     }
523
524     if(ap->standard) {
525         av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
526                ap->standard );
527         /* set tv standard */
528         memset (&standard, 0, sizeof (standard));
529         for(i=0;;i++) {
530             standard.index = i;
531             if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
532                 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
533                        ap->standard);
534                 return AVERROR(EIO);
535             }
536
537             if(!strcasecmp(standard.name, ap->standard)) {
538                 break;
539             }
540         }
541
542         av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
543                ap->standard, (uint64_t)standard.id);
544         if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
545             av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
546                    ap->standard);
547             return AVERROR(EIO);
548         }
549     }
550
551     return 0;
552 }
553
554 static uint32_t device_try_init(AVFormatContext *s1,
555                                 const AVFormatParameters *ap,
556                                 int *width,
557                                 int *height,
558                                 enum CodecID *codec_id)
559 {
560     uint32_t desired_format = fmt_ff2v4l(ap->pix_fmt, s1->video_codec_id);
561
562     if (desired_format == 0 ||
563         device_init(s1, width, height, desired_format) < 0) {
564         int i;
565
566         desired_format = 0;
567         for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
568             if (s1->video_codec_id == CODEC_ID_NONE ||
569                 fmt_conversion_table[i].codec_id == s1->video_codec_id) {
570                 desired_format = fmt_conversion_table[i].v4l2_fmt;
571                 if (device_init(s1, width, height, desired_format) >= 0) {
572                     break;
573                 }
574                 desired_format = 0;
575             }
576         }
577     }
578     if (desired_format != 0) {
579         *codec_id = fmt_v4l2codec(desired_format);
580         assert(*codec_id != CODEC_ID_NONE);
581     }
582
583     return desired_format;
584 }
585
586 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
587 {
588     struct video_data *s = s1->priv_data;
589     AVStream *st;
590     int res;
591     uint32_t desired_format, capabilities;
592     enum CodecID codec_id;
593
594     st = av_new_stream(s1, 0);
595     if (!st) {
596         return AVERROR(ENOMEM);
597     }
598     av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
599
600     s->width  = ap->width;
601     s->height = ap->height;
602
603     capabilities = 0;
604     s->fd = device_open(s1, &capabilities);
605     if (s->fd < 0) {
606         return AVERROR(EIO);
607     }
608     av_log(s1, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n", s->fd, capabilities);
609
610     if (!s->width && !s->height) {
611         struct v4l2_format fmt;
612
613         av_log(s1, AV_LOG_VERBOSE, "Querying the device for the current frame size\n");
614         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
615         if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
616             av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", strerror(errno));
617             return AVERROR(errno);
618         }
619         s->width  = fmt.fmt.pix.width;
620         s->height = fmt.fmt.pix.height;
621         av_log(s1, AV_LOG_VERBOSE, "Setting frame size to %dx%d\n", s->width, s->height);
622     }
623
624     desired_format = device_try_init(s1, ap, &s->width, &s->height, &codec_id);
625     if (desired_format == 0) {
626         av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
627                "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, ap->pix_fmt);
628         close(s->fd);
629
630         return AVERROR(EIO);
631     }
632     if (av_check_image_size(s->width, s->height, 0, s1) < 0)
633         return AVERROR(EINVAL);
634     s->frame_format = desired_format;
635
636     if( v4l2_set_parameters( s1, ap ) < 0 )
637         return AVERROR(EIO);
638
639     st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
640     s->frame_size = avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
641     if (capabilities & V4L2_CAP_STREAMING) {
642         s->io_method = io_mmap;
643         res = mmap_init(s1);
644         if (res == 0) {
645             res = mmap_start(s1);
646         }
647     } else {
648         s->io_method = io_read;
649         res = read_init(s1);
650     }
651     if (res < 0) {
652         close(s->fd);
653
654         return AVERROR(EIO);
655     }
656     s->top_field_first = first_field(s->fd);
657
658     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
659     st->codec->codec_id = codec_id;
660     st->codec->width = s->width;
661     st->codec->height = s->height;
662     st->codec->time_base.den = ap->time_base.den;
663     st->codec->time_base.num = ap->time_base.num;
664     st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
665
666     return 0;
667 }
668
669 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
670 {
671     struct video_data *s = s1->priv_data;
672     int res;
673
674     if (s->io_method == io_mmap) {
675         av_init_packet(pkt);
676         res = mmap_read_frame(s1, pkt);
677     } else if (s->io_method == io_read) {
678         if (av_new_packet(pkt, s->frame_size) < 0)
679             return AVERROR(EIO);
680
681         res = read_frame(s1, pkt);
682     } else {
683         return AVERROR(EIO);
684     }
685     if (res < 0) {
686         return res;
687     }
688
689     if (s1->streams[0]->codec->coded_frame) {
690         s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
691         s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
692     }
693
694     return pkt->size;
695 }
696
697 static int v4l2_read_close(AVFormatContext *s1)
698 {
699     struct video_data *s = s1->priv_data;
700
701     if (s->io_method == io_mmap) {
702         mmap_close(s);
703     }
704
705     close(s->fd);
706     return 0;
707 }
708
709 AVInputFormat v4l2_demuxer = {
710     "video4linux2",
711     NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
712     sizeof(struct video_data),
713     NULL,
714     v4l2_read_header,
715     v4l2_read_packet,
716     v4l2_read_close,
717     .flags = AVFMT_NOFILE,
718 };