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>
39 #if HAVE_SYS_VIDEOIO_H
40 #include <sys/videoio.h>
42 #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"
52 static const int desired_video_buffers = 256;
54 #define V4L_ALLFORMATS 3
55 #define V4L_RAWFORMATS 1
56 #define V4L_COMPFORMATS 2
61 int frame_format; /* V4L2_PIX_FMT_* */
69 unsigned int *buf_len;
72 char *video_size; /**< String describing video size,
73 set by a private option. */
74 char *pixel_format; /**< Set by a private option. */
75 int list_format; /**< Set by a private option. */
76 char *framerate; /**< Set by a private option. */
85 enum PixelFormat ff_fmt;
86 enum CodecID codec_id;
90 static struct fmt_map fmt_conversion_table[] = {
91 //ff_fmt codec_id v4l2_fmt
92 { PIX_FMT_YUV420P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV420 },
93 { PIX_FMT_YUV422P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV422P },
94 { PIX_FMT_YUYV422, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUYV },
95 { PIX_FMT_UYVY422, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_UYVY },
96 { PIX_FMT_YUV411P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV411P },
97 { PIX_FMT_YUV410P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV410 },
98 { PIX_FMT_RGB555, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB555 },
99 { PIX_FMT_RGB565, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB565 },
100 { PIX_FMT_BGR24, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR24 },
101 { PIX_FMT_RGB24, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB24 },
102 { PIX_FMT_BGRA, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR32 },
103 { PIX_FMT_GRAY8, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_GREY },
104 { PIX_FMT_NV12, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12 },
105 { PIX_FMT_NONE, CODEC_ID_MJPEG, V4L2_PIX_FMT_MJPEG },
106 { PIX_FMT_NONE, CODEC_ID_MJPEG, V4L2_PIX_FMT_JPEG },
109 static int device_open(AVFormatContext *ctx)
111 struct v4l2_capability cap;
116 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
120 fd = open(ctx->filename, flags, 0);
124 av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
125 ctx->filename, strerror(err));
130 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
133 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
139 av_log(ctx, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n",
140 fd, cap.capabilities);
142 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
143 av_log(ctx, AV_LOG_ERROR, "Not a video capture device.\n");
149 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
150 av_log(ctx, AV_LOG_ERROR,
151 "The device does not support the streaming I/O method.\n");
164 static int device_init(AVFormatContext *ctx, int *width, int *height,
167 struct video_data *s = ctx->priv_data;
169 struct v4l2_format fmt;
170 struct v4l2_pix_format *pix = &fmt.fmt.pix;
174 memset(&fmt, 0, sizeof(struct v4l2_format));
176 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
178 pix->height = *height;
179 pix->pixelformat = pix_fmt;
180 pix->field = V4L2_FIELD_ANY;
182 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
184 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
185 av_log(ctx, AV_LOG_INFO,
186 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
187 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
188 *width = fmt.fmt.pix.width;
189 *height = fmt.fmt.pix.height;
192 if (pix_fmt != fmt.fmt.pix.pixelformat) {
193 av_log(ctx, AV_LOG_DEBUG,
194 "The V4L2 driver changed the pixel format "
195 "from 0x%08X to 0x%08X\n",
196 pix_fmt, fmt.fmt.pix.pixelformat);
200 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
201 av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver using the interlaced mode");
208 static int first_field(int fd)
213 res = ioctl(fd, VIDIOC_G_STD, &std);
217 if (std & V4L2_STD_NTSC) {
224 static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt, enum CodecID codec_id)
228 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
229 if ((codec_id == CODEC_ID_NONE ||
230 fmt_conversion_table[i].codec_id == codec_id) &&
231 (pix_fmt == PIX_FMT_NONE ||
232 fmt_conversion_table[i].ff_fmt == pix_fmt)) {
233 return fmt_conversion_table[i].v4l2_fmt;
240 static enum PixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum CodecID codec_id)
244 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
245 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
246 fmt_conversion_table[i].codec_id == codec_id) {
247 return fmt_conversion_table[i].ff_fmt;
254 static enum CodecID fmt_v4l2codec(uint32_t v4l2_fmt)
258 for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
259 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
260 return fmt_conversion_table[i].codec_id;
264 return CODEC_ID_NONE;
267 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
268 static void list_framesizes(AVFormatContext *ctx, int fd, uint32_t pixelformat)
270 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
272 while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
274 case V4L2_FRMSIZE_TYPE_DISCRETE:
275 av_log(ctx, AV_LOG_INFO, " %ux%u",
276 vfse.discrete.width, vfse.discrete.height);
278 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
279 case V4L2_FRMSIZE_TYPE_STEPWISE:
280 av_log(ctx, AV_LOG_INFO, " {%u-%u, %u}x{%u-%u, %u}",
281 vfse.stepwise.min_width,
282 vfse.stepwise.max_width,
283 vfse.stepwise.step_width,
284 vfse.stepwise.min_height,
285 vfse.stepwise.max_height,
286 vfse.stepwise.step_height);
293 static void list_formats(AVFormatContext *ctx, int fd, int type)
295 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
297 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
298 enum CodecID codec_id = fmt_v4l2codec(vfd.pixelformat);
299 enum PixelFormat pix_fmt = fmt_v4l2ff(vfd.pixelformat, codec_id);
303 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
304 type & V4L_RAWFORMATS) {
305 const char *fmt_name = av_get_pix_fmt_name(pix_fmt);
306 av_log(ctx, AV_LOG_INFO, "R : %9s : %20s :",
307 fmt_name ? fmt_name : "Unsupported",
309 } else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
310 type & V4L_COMPFORMATS) {
311 AVCodec *codec = avcodec_find_encoder(codec_id);
312 av_log(ctx, AV_LOG_INFO, "C : %9s : %20s :",
313 codec ? codec->name : "Unsupported",
319 #ifdef V4L2_FMT_FLAG_EMULATED
320 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
321 av_log(ctx, AV_LOG_WARNING, "%s", "Emulated");
325 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
326 list_framesizes(ctx, fd, vfd.pixelformat);
328 av_log(ctx, AV_LOG_INFO, "\n");
332 static int mmap_init(AVFormatContext *ctx)
334 struct video_data *s = ctx->priv_data;
335 struct v4l2_requestbuffers req;
338 memset(&req, 0, sizeof(struct v4l2_requestbuffers));
339 req.count = desired_video_buffers;
340 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
341 req.memory = V4L2_MEMORY_MMAP;
342 res = ioctl(s->fd, VIDIOC_REQBUFS, &req);
344 if (errno == EINVAL) {
345 av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
347 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
350 return AVERROR(errno);
354 av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
356 return AVERROR(ENOMEM);
358 s->buffers = req.count;
359 s->buf_start = av_malloc(sizeof(void *) * s->buffers);
360 if (s->buf_start == NULL) {
361 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
363 return AVERROR(ENOMEM);
365 s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
366 if (s->buf_len == NULL) {
367 av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
368 av_free(s->buf_start);
370 return AVERROR(ENOMEM);
373 for (i = 0; i < req.count; i++) {
374 struct v4l2_buffer buf;
376 memset(&buf, 0, sizeof(struct v4l2_buffer));
377 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
378 buf.memory = V4L2_MEMORY_MMAP;
380 res = ioctl(s->fd, VIDIOC_QUERYBUF, &buf);
382 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
384 return AVERROR(errno);
387 s->buf_len[i] = buf.length;
388 if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
389 av_log(ctx, AV_LOG_ERROR,
390 "Buffer len [%d] = %d != %d\n",
391 i, s->buf_len[i], s->frame_size);
395 s->buf_start[i] = mmap(NULL, buf.length,
396 PROT_READ | PROT_WRITE, MAP_SHARED,
397 s->fd, buf.m.offset);
399 if (s->buf_start[i] == MAP_FAILED) {
400 av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
402 return AVERROR(errno);
409 static void mmap_release_buffer(AVPacket *pkt)
411 struct v4l2_buffer buf;
413 struct buff_data *buf_descriptor = pkt->priv;
415 if (pkt->data == NULL)
418 memset(&buf, 0, sizeof(struct v4l2_buffer));
419 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
420 buf.memory = V4L2_MEMORY_MMAP;
421 buf.index = buf_descriptor->index;
422 fd = buf_descriptor->fd;
423 av_free(buf_descriptor);
425 res = ioctl(fd, VIDIOC_QBUF, &buf);
427 av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
434 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
436 struct video_data *s = ctx->priv_data;
437 struct v4l2_buffer buf;
438 struct buff_data *buf_descriptor;
441 memset(&buf, 0, sizeof(struct v4l2_buffer));
442 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
443 buf.memory = V4L2_MEMORY_MMAP;
445 /* FIXME: Some special treatment might be needed in case of loss of signal... */
446 while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
448 if (errno == EAGAIN) {
451 return AVERROR(EAGAIN);
453 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n",
456 return AVERROR(errno);
458 assert (buf.index < s->buffers);
459 if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
460 av_log(ctx, AV_LOG_ERROR,
461 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
462 buf.bytesused, s->frame_size);
464 return AVERROR_INVALIDDATA;
467 /* Image is at s->buff_start[buf.index] */
468 pkt->data= s->buf_start[buf.index];
469 pkt->size = buf.bytesused;
470 pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
471 pkt->destruct = mmap_release_buffer;
472 buf_descriptor = av_malloc(sizeof(struct buff_data));
473 if (buf_descriptor == NULL) {
474 /* Something went wrong... Since av_malloc() failed, we cannot even
475 * allocate a buffer for memcopying into it
477 av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
478 res = ioctl(s->fd, VIDIOC_QBUF, &buf);
480 return AVERROR(ENOMEM);
482 buf_descriptor->fd = s->fd;
483 buf_descriptor->index = buf.index;
484 pkt->priv = buf_descriptor;
486 return s->buf_len[buf.index];
489 static int mmap_start(AVFormatContext *ctx)
491 struct video_data *s = ctx->priv_data;
492 enum v4l2_buf_type type;
495 for (i = 0; i < s->buffers; i++) {
496 struct v4l2_buffer buf;
498 memset(&buf, 0, sizeof(struct v4l2_buffer));
499 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
500 buf.memory = V4L2_MEMORY_MMAP;
503 res = ioctl(s->fd, VIDIOC_QBUF, &buf);
505 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
508 return AVERROR(errno);
512 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
513 res = ioctl(s->fd, VIDIOC_STREAMON, &type);
515 av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n",
518 return AVERROR(errno);
524 static void mmap_close(struct video_data *s)
526 enum v4l2_buf_type type;
529 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
530 /* We do not check for the result, because we could
531 * not do anything about it anyway...
533 ioctl(s->fd, VIDIOC_STREAMOFF, &type);
534 for (i = 0; i < s->buffers; i++) {
535 munmap(s->buf_start[i], s->buf_len[i]);
537 av_free(s->buf_start);
541 static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap)
543 struct video_data *s = s1->priv_data;
544 struct v4l2_input input;
545 struct v4l2_standard standard;
546 struct v4l2_streamparm streamparm = { 0 };
547 struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
549 AVRational framerate_q;
551 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
554 (ret = av_parse_video_rate(&framerate_q, s->framerate)) < 0) {
555 av_log(s1, AV_LOG_ERROR, "Could not parse framerate '%s'.\n",
560 /* set tv video input */
561 memset (&input, 0, sizeof (input));
562 input.index = s->channel;
563 if (ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
564 av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
568 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
569 s->channel, input.name);
570 if (ioctl(s->fd, VIDIOC_S_INPUT, &input.index) < 0) {
571 av_log(s1, AV_LOG_ERROR,
572 "The V4L2 driver ioctl set input(%d) failed\n",
578 av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
580 /* set tv standard */
581 memset (&standard, 0, sizeof (standard));
584 if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
585 av_log(s1, AV_LOG_ERROR,
586 "The V4L2 driver ioctl set standard(%s) failed\n",
591 if (!av_strcasecmp(standard.name, s->standard)) {
596 av_log(s1, AV_LOG_DEBUG,
597 "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
598 s->standard, (uint64_t)standard.id);
599 if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
600 av_log(s1, AV_LOG_ERROR,
601 "The V4L2 driver ioctl set standard(%s) failed\n",
607 if (framerate_q.num && framerate_q.den) {
608 av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
609 framerate_q.den, framerate_q.num);
610 tpf->numerator = framerate_q.den;
611 tpf->denominator = framerate_q.num;
613 if (ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) {
614 av_log(s1, AV_LOG_ERROR,
615 "ioctl set time per frame(%d/%d) failed\n",
616 framerate_q.den, framerate_q.num);
620 if (framerate_q.num != tpf->denominator ||
621 framerate_q.den != tpf->numerator) {
622 av_log(s1, AV_LOG_INFO,
623 "The driver changed the time per frame from "
625 framerate_q.den, framerate_q.num,
626 tpf->numerator, tpf->denominator);
629 if (ioctl(s->fd, VIDIOC_G_PARM, &streamparm) != 0) {
630 av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n",
632 return AVERROR(errno);
635 s1->streams[0]->codec->time_base.den = tpf->denominator;
636 s1->streams[0]->codec->time_base.num = tpf->numerator;
641 static uint32_t device_try_init(AVFormatContext *s1,
642 enum PixelFormat pix_fmt,
645 enum CodecID *codec_id)
647 uint32_t desired_format = fmt_ff2v4l(pix_fmt, s1->video_codec_id);
649 if (desired_format == 0 ||
650 device_init(s1, width, height, desired_format) < 0) {
654 for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
655 if (s1->video_codec_id == CODEC_ID_NONE ||
656 fmt_conversion_table[i].codec_id == s1->video_codec_id) {
657 desired_format = fmt_conversion_table[i].v4l2_fmt;
658 if (device_init(s1, width, height, desired_format) >= 0) {
666 if (desired_format != 0) {
667 *codec_id = fmt_v4l2codec(desired_format);
668 assert(*codec_id != CODEC_ID_NONE);
671 return desired_format;
674 static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
676 struct video_data *s = s1->priv_data;
679 uint32_t desired_format;
680 enum CodecID codec_id;
681 enum PixelFormat pix_fmt = PIX_FMT_NONE;
683 st = avformat_new_stream(s1, NULL);
685 res = AVERROR(ENOMEM);
689 s->fd = device_open(s1);
695 if (s->list_format) {
696 list_formats(s1, s->fd, s->list_format);
701 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
704 (res = av_parse_video_size(&s->width, &s->height, s->video_size)) < 0) {
705 av_log(s1, AV_LOG_ERROR, "Could not parse video size '%s'.\n",
710 if (s->pixel_format) {
711 AVCodec *codec = avcodec_find_decoder_by_name(s->pixel_format);
714 s1->video_codec_id = codec->id;
716 pix_fmt = av_get_pix_fmt(s->pixel_format);
718 if (pix_fmt == PIX_FMT_NONE && !codec) {
719 av_log(s1, AV_LOG_ERROR, "No such input format: %s.\n",
722 res = AVERROR(EINVAL);
727 if (!s->width && !s->height) {
728 struct v4l2_format fmt;
730 av_log(s1, AV_LOG_VERBOSE,
731 "Querying the device for the current frame size\n");
732 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
733 if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
734 av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n",
736 res = AVERROR(errno);
740 s->width = fmt.fmt.pix.width;
741 s->height = fmt.fmt.pix.height;
742 av_log(s1, AV_LOG_VERBOSE,
743 "Setting frame size to %dx%d\n", s->width, s->height);
746 desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height,
748 if (desired_format == 0) {
749 av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
750 "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, pix_fmt);
757 if ((res = av_image_check_size(s->width, s->height, 0, s1) < 0))
760 s->frame_format = desired_format;
762 if ((res = v4l2_set_parameters(s1, ap) < 0))
765 st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
767 avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
769 if ((res = mmap_init(s1)) ||
770 (res = mmap_start(s1)) < 0) {
775 s->top_field_first = first_field(s->fd);
777 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
778 st->codec->codec_id = codec_id;
779 if (codec_id == CODEC_ID_RAWVIDEO)
780 st->codec->codec_tag =
781 avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
782 st->codec->width = s->width;
783 st->codec->height = s->height;
784 st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
790 static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
792 struct video_data *s = s1->priv_data;
793 AVFrame *frame = s1->streams[0]->codec->coded_frame;
797 if ((res = mmap_read_frame(s1, pkt)) < 0) {
801 if (frame && s->interlaced) {
802 frame->interlaced_frame = 1;
803 frame->top_field_first = s->top_field_first;
809 static int v4l2_read_close(AVFormatContext *s1)
811 struct video_data *s = s1->priv_data;
819 #define OFFSET(x) offsetof(struct video_data, x)
820 #define DEC AV_OPT_FLAG_DECODING_PARAM
821 static const AVOption options[] = {
822 { "standard", "TV standard, used only by analog frame grabber", OFFSET(standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
823 { "channel", "TV channel, used only by frame grabber", OFFSET(channel), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, DEC },
824 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
825 { "pixel_format", "Preferred pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
826 { "input_format", "Preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
827 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
828 { "list_formats", "List available formats and exit", OFFSET(list_format), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, DEC, "list_formats" },
829 { "all", "Show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.dbl = V4L_ALLFORMATS }, 0, INT_MAX, DEC, "list_formats" },
830 { "raw", "Show only non-compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.dbl = V4L_RAWFORMATS }, 0, INT_MAX, DEC, "list_formats" },
831 { "compressed", "Show only compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.dbl = V4L_COMPFORMATS }, 0, INT_MAX, DEC, "list_formats" },
835 static const AVClass v4l2_class = {
836 .class_name = "V4L2 indev",
837 .item_name = av_default_item_name,
839 .version = LIBAVUTIL_VERSION_INT,
842 AVInputFormat ff_v4l2_demuxer = {
843 .name = "video4linux2",
844 .long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
845 .priv_data_size = sizeof(struct video_data),
846 .read_header = v4l2_read_header,
847 .read_packet = v4l2_read_packet,
848 .read_close = v4l2_read_close,
849 .flags = AVFMT_NOFILE,
850 .priv_class = &v4l2_class,