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 file is part of Libav.
15 * Libav 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.
20 * Libav 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.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with Libav; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
32 #include "libavformat/avformat.h"
33 #include "libavformat/internal.h"
36 #include <sys/ioctl.h>
40 #if HAVE_SYS_VIDEOIO_H
41 #include <sys/videoio.h>
43 #include <linux/videodev2.h>
45 #include "libavutil/imgutils.h"
46 #include "libavutil/log.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/parseutils.h"
49 #include "libavutil/pixdesc.h"
50 #include "libavutil/avstring.h"
51 #include "libavutil/mathematics.h"
53 static const int desired_video_buffers = 256;
55 #define V4L_ALLFORMATS 3
56 #define V4L_RAWFORMATS 1
57 #define V4L_COMPFORMATS 2
62 int frame_format; /* V4L2_PIX_FMT_* */
71 unsigned int *buf_len;
74 char *video_size; /**< String describing video size,
75 set by a private option. */
76 char *pixel_format; /**< Set by a private option. */
77 int list_format; /**< Set by a private option. */
78 char *framerate; /**< Set by a private option. */
87 enum PixelFormat ff_fmt;
88 enum AVCodecID codec_id;
92 static struct fmt_map fmt_conversion_table[] = {
93 //ff_fmt codec_id v4l2_fmt
94 { PIX_FMT_YUV420P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV420 },
95 { PIX_FMT_YUV422P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV422P },
96 { PIX_FMT_YUYV422, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUYV },
97 { PIX_FMT_UYVY422, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_UYVY },
98 { PIX_FMT_YUV411P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV411P },
99 { PIX_FMT_YUV410P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV410 },
100 { PIX_FMT_RGB555, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB555 },
101 { PIX_FMT_RGB565, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB565 },
102 { PIX_FMT_BGR24, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR24 },
103 { PIX_FMT_RGB24, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB24 },
104 { PIX_FMT_BGRA, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR32 },
105 { PIX_FMT_GRAY8, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_GREY },
106 { PIX_FMT_NV12, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12 },
107 { PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_MJPEG },
108 { PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_JPEG },
111 static int device_open(AVFormatContext *ctx)
113 struct v4l2_capability cap;
118 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
122 fd = open(ctx->filename, flags, 0);
126 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
127 ctx->filename, strerror(err));
132 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
135 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
141 av_log(ctx, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n",
142 fd, cap.capabilities);
144 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
145 av_log(ctx, AV_LOG_ERROR, "Not a video capture device.\n");
151 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
152 av_log(ctx, AV_LOG_ERROR,
153 "The device does not support the streaming I/O method.\n");
166 static int device_init(AVFormatContext *ctx, int *width, int *height,
169 struct video_data *s = ctx->priv_data;
171 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
172 struct v4l2_pix_format *pix = &fmt.fmt.pix;
177 pix->height = *height;
178 pix->pixelformat = pix_fmt;
179 pix->field = V4L2_FIELD_ANY;
181 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
183 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
184 av_log(ctx, AV_LOG_INFO,
185 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
186 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
187 *width = fmt.fmt.pix.width;
188 *height = fmt.fmt.pix.height;
191 if (pix_fmt != fmt.fmt.pix.pixelformat) {
192 av_log(ctx, AV_LOG_DEBUG,
193 "The V4L2 driver changed the pixel format "
194 "from 0x%08X to 0x%08X\n",
195 pix_fmt, fmt.fmt.pix.pixelformat);
199 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
200 av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver using the interlaced mode");
207 static int first_field(int fd)
212 res = ioctl(fd, VIDIOC_G_STD, &std);
216 if (std & V4L2_STD_NTSC) {
223 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt, enum AVCodecID codec_id)
227 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
228 if ((codec_id == AV_CODEC_ID_NONE ||
229 fmt_conversion_table[i].codec_id == codec_id) &&
230 (pix_fmt == PIX_FMT_NONE ||
231 fmt_conversion_table[i].ff_fmt == pix_fmt)) {
232 return fmt_conversion_table[i].v4l2_fmt;
239 static enum PixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum AVCodecID codec_id)
243 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
244 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
245 fmt_conversion_table[i].codec_id == codec_id) {
246 return fmt_conversion_table[i].ff_fmt;
253 static enum AVCodecID fmt_v4l2codec(uint32_t v4l2_fmt)
257 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
258 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
259 return fmt_conversion_table[i].codec_id;
263 return AV_CODEC_ID_NONE;
266 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
267 static void list_framesizes(AVFormatContext *ctx, int fd, uint32_t pixelformat)
269 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
271 while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
273 case V4L2_FRMSIZE_TYPE_DISCRETE:
274 av_log(ctx, AV_LOG_INFO, " %ux%u",
275 vfse.discrete.width, vfse.discrete.height);
277 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
278 case V4L2_FRMSIZE_TYPE_STEPWISE:
279 av_log(ctx, AV_LOG_INFO, " {%u-%u, %u}x{%u-%u, %u}",
280 vfse.stepwise.min_width,
281 vfse.stepwise.max_width,
282 vfse.stepwise.step_width,
283 vfse.stepwise.min_height,
284 vfse.stepwise.max_height,
285 vfse.stepwise.step_height);
292 static void list_formats(AVFormatContext *ctx, int fd, int type)
294 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
296 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
297 enum AVCodecID codec_id = fmt_v4l2codec(vfd.pixelformat);
298 enum PixelFormat pix_fmt = fmt_v4l2ff(vfd.pixelformat, codec_id);
302 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
303 type & V4L_RAWFORMATS) {
304 const char *fmt_name = av_get_pix_fmt_name(pix_fmt);
305 av_log(ctx, AV_LOG_INFO, "R : %9s : %20s :",
306 fmt_name ? fmt_name : "Unsupported",
308 } else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
309 type & V4L_COMPFORMATS) {
310 AVCodec *codec = avcodec_find_encoder(codec_id);
311 av_log(ctx, AV_LOG_INFO, "C : %9s : %20s :",
312 codec ? codec->name : "Unsupported",
318 #ifdef V4L2_FMT_FLAG_EMULATED
319 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
320 av_log(ctx, AV_LOG_WARNING, "%s", "Emulated");
324 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
325 list_framesizes(ctx, fd, vfd.pixelformat);
327 av_log(ctx, AV_LOG_INFO, "\n");
331 static int mmap_init(AVFormatContext *ctx)
334 struct video_data *s = ctx->priv_data;
335 struct v4l2_requestbuffers req = {
336 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
337 .count = desired_video_buffers,
338 .memory = V4L2_MEMORY_MMAP
341 res = ioctl(s->fd, VIDIOC_REQBUFS, &req);
343 if (errno == EINVAL) {
344 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
346 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
349 return AVERROR(errno);
353 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
355 return AVERROR(ENOMEM);
357 s->buffers = req.count;
358 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
359 if (s->buf_start == NULL) {
360 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
362 return AVERROR(ENOMEM);
364 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
365 if (s->buf_len == NULL) {
366 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
367 av_free(s->buf_start);
369 return AVERROR(ENOMEM);
372 for (i = 0; i < req.count; i++) {
373 struct v4l2_buffer buf = {
374 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
376 .memory = V4L2_MEMORY_MMAP
379 res = ioctl(s->fd, VIDIOC_QUERYBUF, &buf);
381 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
383 return AVERROR(errno);
386 s->buf_len[i] = buf.length;
387 if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
388 av_log(ctx, AV_LOG_ERROR,
389 "Buffer len [%d] = %d != %d\n",
390 i, s->buf_len[i], s->frame_size);
394 s->buf_start[i] = mmap(NULL, buf.length,
395 PROT_READ | PROT_WRITE, MAP_SHARED,
396 s->fd, buf.m.offset);
398 if (s->buf_start[i] == MAP_FAILED) {
399 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
401 return AVERROR(errno);
408 static void mmap_release_buffer(AVPacket *pkt)
410 struct v4l2_buffer buf = { 0 };
412 struct buff_data *buf_descriptor = pkt->priv;
414 if (pkt->data == NULL)
417 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
418 buf.memory = V4L2_MEMORY_MMAP;
419 buf.index = buf_descriptor->index;
420 fd = buf_descriptor->fd;
421 av_free(buf_descriptor);
423 res = ioctl(fd, VIDIOC_QBUF, &buf);
425 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
432 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
434 struct video_data *s = ctx->priv_data;
435 struct v4l2_buffer buf = {
436 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
437 .memory = V4L2_MEMORY_MMAP
439 struct buff_data *buf_descriptor;
440 struct pollfd p = { .fd = s->fd, .events = POLLIN };
443 res = poll(&p, 1, s->timeout);
445 return AVERROR(errno);
447 if (!(p.revents & (POLLIN | POLLERR | POLLHUP)))
448 return AVERROR(EAGAIN);
450 /* FIXME: Some special treatment might be needed in case of loss of signal... */
451 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
453 if (errno == EAGAIN) {
456 return AVERROR(EAGAIN);
458 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n",
461 return AVERROR(errno);
463 assert (buf.index < s->buffers);
464 if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
465 av_log(ctx, AV_LOG_ERROR,
466 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
467 buf.bytesused, s->frame_size);
469 return AVERROR_INVALIDDATA;
472 /* Image is at s->buff_start[buf.index] */
473 pkt->data= s->buf_start[buf.index];
474 pkt->size = buf.bytesused;
475 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
476 pkt->destruct = mmap_release_buffer;
477 buf_descriptor = av_malloc(sizeof(struct buff_data));
478 if (buf_descriptor == NULL) {
479 /* Something went wrong... Since av_malloc() failed, we cannot even
480 * allocate a buffer for memcopying into it
482 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
483 res = ioctl(s->fd, VIDIOC_QBUF, &buf);
485 return AVERROR(ENOMEM);
487 buf_descriptor->fd = s->fd;
488 buf_descriptor->index = buf.index;
489 pkt->priv = buf_descriptor;
491 return s->buf_len[buf.index];
494 static int mmap_start(AVFormatContext *ctx)
496 struct video_data *s = ctx->priv_data;
497 enum v4l2_buf_type type;
500 for (i = 0; i < s->buffers; i++) {
501 struct v4l2_buffer buf = {
502 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
504 .memory = V4L2_MEMORY_MMAP
507 res = ioctl(s->fd, VIDIOC_QBUF, &buf);
509 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
512 return AVERROR(errno);
516 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
517 res = ioctl(s->fd, VIDIOC_STREAMON, &type);
519 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n",
522 return AVERROR(errno);
528 static void mmap_close(struct video_data *s)
530 enum v4l2_buf_type type;
533 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
534 /* We do not check for the result, because we could
535 * not do anything about it anyway...
537 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
538 for (i = 0; i < s->buffers; i++) {
539 munmap(s->buf_start[i], s->buf_len[i]);
541 av_free(s->buf_start);
545 static int v4l2_set_parameters(AVFormatContext *s1)
547 struct video_data *s = s1->priv_data;
548 struct v4l2_input input = { 0 };
549 struct v4l2_standard standard = { 0 };
550 struct v4l2_streamparm streamparm = { 0 };
551 struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
552 AVRational framerate_q = { 0 };
555 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
558 (ret = av_parse_video_rate(&framerate_q, s->framerate)) < 0) {
559 av_log(s1, AV_LOG_ERROR, "Could not parse framerate '%s'.\n",
564 /* set tv video input */
565 input.index = s->channel;
566 if (ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
567 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
571 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
572 s->channel, input.name);
573 if (ioctl(s->fd, VIDIOC_S_INPUT, &input.index) < 0) {
574 av_log(s1, AV_LOG_ERROR,
575 "The V4L2 driver ioctl set input(%d) failed\n",
581 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
583 /* set tv standard */
586 if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
587 av_log(s1, AV_LOG_ERROR,
588 "The V4L2 driver ioctl set standard(%s) failed\n",
593 if (!av_strcasecmp(standard.name, s->standard)) {
598 av_log(s1, AV_LOG_DEBUG,
599 "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
600 s->standard, (uint64_t)standard.id);
601 if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
602 av_log(s1, AV_LOG_ERROR,
603 "The V4L2 driver ioctl set standard(%s) failed\n",
609 if (framerate_q.num && framerate_q.den) {
610 av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
611 framerate_q.den, framerate_q.num);
612 tpf->numerator = framerate_q.den;
613 tpf->denominator = framerate_q.num;
615 if (ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) {
616 av_log(s1, AV_LOG_ERROR,
617 "ioctl set time per frame(%d/%d) failed\n",
618 framerate_q.den, framerate_q.num);
622 if (framerate_q.num != tpf->denominator ||
623 framerate_q.den != tpf->numerator) {
624 av_log(s1, AV_LOG_INFO,
625 "The driver changed the time per frame from "
627 framerate_q.den, framerate_q.num,
628 tpf->numerator, tpf->denominator);
631 if (ioctl(s->fd, VIDIOC_G_PARM, &streamparm) != 0) {
632 av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n",
634 return AVERROR(errno);
637 s1->streams[0]->codec->time_base.den = tpf->denominator;
638 s1->streams[0]->codec->time_base.num = tpf->numerator;
641 av_rescale_q(1, s1->streams[0]->codec->time_base,
642 (AVRational){1, 1000});
647 static uint32_t device_try_init(AVFormatContext *s1,
648 enum PixelFormat pix_fmt,
651 enum AVCodecID *codec_id)
653 uint32_t desired_format = fmt_ff2v4l(pix_fmt, s1->video_codec_id);
655 if (desired_format == 0 ||
656 device_init(s1, width, height, desired_format) < 0) {
660 for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
661 if (s1->video_codec_id == AV_CODEC_ID_NONE ||
662 fmt_conversion_table[i].codec_id == s1->video_codec_id) {
663 desired_format = fmt_conversion_table[i].v4l2_fmt;
664 if (device_init(s1, width, height, desired_format) >= 0) {
672 if (desired_format != 0) {
673 *codec_id = fmt_v4l2codec(desired_format);
674 assert(*codec_id != AV_CODEC_ID_NONE);
677 return desired_format;
680 static int v4l2_read_header(AVFormatContext *s1)
682 struct video_data *s = s1->priv_data;
685 uint32_t desired_format;
686 enum AVCodecID codec_id;
687 enum PixelFormat pix_fmt = PIX_FMT_NONE;
689 st = avformat_new_stream(s1, NULL);
691 res = AVERROR(ENOMEM);
695 s->fd = device_open(s1);
701 if (s->list_format) {
702 list_formats(s1, s->fd, s->list_format);
707 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
710 (res = av_parse_video_size(&s->width, &s->height, s->video_size)) < 0) {
711 av_log(s1, AV_LOG_ERROR, "Could not parse video size '%s'.\n",
716 if (s->pixel_format) {
717 AVCodec *codec = avcodec_find_decoder_by_name(s->pixel_format);
720 s1->video_codec_id = codec->id;
722 pix_fmt = av_get_pix_fmt(s->pixel_format);
724 if (pix_fmt == PIX_FMT_NONE && !codec) {
725 av_log(s1, AV_LOG_ERROR, "No such input format: %s.\n",
728 res = AVERROR(EINVAL);
733 if (!s->width && !s->height) {
734 struct v4l2_format fmt;
736 av_log(s1, AV_LOG_VERBOSE,
737 "Querying the device for the current frame size\n");
738 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
739 if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
740 av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n",
742 res = AVERROR(errno);
746 s->width = fmt.fmt.pix.width;
747 s->height = fmt.fmt.pix.height;
748 av_log(s1, AV_LOG_VERBOSE,
749 "Setting frame size to %dx%d\n", s->width, s->height);
752 desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height,
754 if (desired_format == 0) {
755 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
756 "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, pix_fmt);
763 if ((res = av_image_check_size(s->width, s->height, 0, s1) < 0))
766 s->frame_format = desired_format;
768 if ((res = v4l2_set_parameters(s1) < 0))
771 st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
773 avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
775 if ((res = mmap_init(s1)) ||
776 (res = mmap_start(s1)) < 0) {
781 s->top_field_first = first_field(s->fd);
783 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
784 st->codec->codec_id = codec_id;
785 if (codec_id == AV_CODEC_ID_RAWVIDEO)
786 st->codec->codec_tag =
787 avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
788 st->codec->width = s->width;
789 st->codec->height = s->height;
790 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
796 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
798 struct video_data *s = s1->priv_data;
799 AVFrame *frame = s1->streams[0]->codec->coded_frame;
803 if ((res = mmap_read_frame(s1, pkt)) < 0) {
807 if (frame && s->interlaced) {
808 frame->interlaced_frame = 1;
809 frame->top_field_first = s->top_field_first;
815 static int v4l2_read_close(AVFormatContext *s1)
817 struct video_data *s = s1->priv_data;
825 #define OFFSET(x) offsetof(struct video_data, x)
826 #define DEC AV_OPT_FLAG_DECODING_PARAM
827 static const AVOption options[] = {
828 { "standard", "TV standard, used only by analog frame grabber", OFFSET(standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
829 { "channel", "TV channel, used only by frame grabber", OFFSET(channel), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
830 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
831 { "pixel_format", "Preferred pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
832 { "input_format", "Preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
833 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
834 { "list_formats", "List available formats and exit", OFFSET(list_format), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC, "list_formats" },
835 { "all", "Show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_ALLFORMATS }, 0, INT_MAX, DEC, "list_formats" },
836 { "raw", "Show only non-compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_RAWFORMATS }, 0, INT_MAX, DEC, "list_formats" },
837 { "compressed", "Show only compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_COMPFORMATS }, 0, INT_MAX, DEC, "list_formats" },
841 static const AVClass v4l2_class = {
842 .class_name = "V4L2 indev",
843 .item_name = av_default_item_name,
845 .version = LIBAVUTIL_VERSION_INT,
848 AVInputFormat ff_v4l2_demuxer = {
849 .name = "video4linux2",
850 .long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
851 .priv_data_size = sizeof(struct video_data),
852 .read_header = v4l2_read_header,
853 .read_packet = v4l2_read_packet,
854 .read_close = v4l2_read_close,
855 .flags = AVFMT_NOFILE,
856 .priv_class = &v4l2_class,