33 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
38 #include <sys/ioctl.h>
41 #if HAVE_SYS_VIDEOIO_H
42 #include <sys/videoio.h>
45 #include <asm/types.h>
47 #include <linux/videodev2.h>
62 #define v4l2_open open
63 #define v4l2_close close
65 #define v4l2_ioctl ioctl
66 #define v4l2_read read
67 #define v4l2_mmap mmap
68 #define v4l2_munmap munmap
73 #define V4L_ALLFORMATS 3
74 #define V4L_RAWFORMATS 1
75 #define V4L_COMPFORMATS 2
80 #define V4L_TS_DEFAULT 0
90 #define V4L_TS_MONO2ABS 2
97 #define V4L_TS_CONVERT_READY V4L_TS_DEFAULT
155 #ifdef V4L2_PIX_FMT_Y16
161 #ifdef V4L2_PIX_FMT_H264
164 #ifdef V4L2_PIX_FMT_CPIA1
171 struct v4l2_capability cap;
188 if (
v4l2_ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
196 fd, cap.capabilities);
198 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
204 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
206 "The device does not support the streaming I/O method.\n");
223 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
224 struct v4l2_pix_format *pix = &fmt.fmt.pix;
231 pix->field = V4L2_FIELD_ANY;
236 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
238 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
239 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
240 *width = fmt.fmt.pix.width;
241 *height = fmt.fmt.pix.height;
244 if (pix_fmt != fmt.fmt.pix.pixelformat) {
246 "The V4L2 driver changed the pixel format "
247 "from 0x%08X to 0x%08X\n",
248 pix_fmt, fmt.fmt.pix.pixelformat);
252 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
254 "The V4L2 driver is using the interlaced mode\n");
270 if (std & V4L2_STD_NTSC) {
283 fmt_conversion_table[i].codec_id == codec_id) &&
286 return fmt_conversion_table[i].
v4l2_fmt;
298 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
299 fmt_conversion_table[i].codec_id == codec_id) {
300 return fmt_conversion_table[i].
ff_fmt;
312 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
313 return fmt_conversion_table[i].
codec_id;
320 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
321 static void list_framesizes(
AVFormatContext *ctx,
int fd, uint32_t pixelformat)
323 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
325 while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
327 case V4L2_FRMSIZE_TYPE_DISCRETE:
329 vfse.discrete.width, vfse.discrete.height);
331 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
332 case V4L2_FRMSIZE_TYPE_STEPWISE:
334 vfse.stepwise.min_width,
335 vfse.stepwise.max_width,
336 vfse.stepwise.step_width,
337 vfse.stepwise.min_height,
338 vfse.stepwise.max_height,
339 vfse.stepwise.step_height);
348 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
350 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
356 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
360 fmt_name ? fmt_name :
"Unsupported",
362 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
366 codec ? codec->
name :
"Unsupported",
372 #ifdef V4L2_FMT_FLAG_EMULATED
373 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
378 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
379 list_framesizes(ctx, fd, vfd.pixelformat);
389 struct v4l2_standard standard;
394 for (standard.index = 0; ; standard.index++) {
405 standard.index, standard.id, standard.name);
413 struct v4l2_requestbuffers req = {
414 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
416 .memory = V4L2_MEMORY_MMAP
447 for (i = 0; i < req.count; i++) {
448 struct v4l2_buffer buf = {
449 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
451 .memory = V4L2_MEMORY_MMAP
462 "buf_len[%d] = %d < expected frame size %d\n",
467 PROT_READ | PROT_WRITE, MAP_SHARED,
468 s->
fd, buf.m.offset);
483 struct v4l2_buffer buf = { 0 };
485 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
486 buf.memory = V4L2_MEMORY_MMAP;
504 if (buf_descriptor->
index == -1) {
516 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
517 static int64_t av_gettime_monotonic(
void)
521 clock_gettime(CLOCK_MONOTONIC, &tv);
522 return (int64_t)tv.tv_sec * 1000000 + tv.tv_nsec / 1000;
533 ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE) {
538 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
539 now = av_gettime_monotonic();
541 (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE)) {
564 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
567 int64_t nowm = av_gettime_monotonic();
579 struct v4l2_buffer buf = {
580 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
581 .memory = V4L2_MEMORY_MMAP
587 while ((res =
v4l2_ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
609 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
616 if (buf_descriptor ==
NULL) {
624 buf_descriptor->
fd = s->
fd;
628 for (i = 0; i < s->
buffers; i++)
632 if (free_buffers == 0) {
639 buf_descriptor->
index = -1;
643 buf_descriptor->
index = buf.index;
646 pkt->
size = buf.bytesused;
647 pkt->
priv = buf_descriptor;
649 pkt->
pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
662 enum v4l2_buf_type type;
665 for (i = 0; i < s->
buffers; i++) {
666 struct v4l2_buffer buf = {
667 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
669 .memory = V4L2_MEMORY_MMAP
679 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
691 enum v4l2_buf_type type;
694 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
699 for (i = 0; i < s->
buffers; i++) {
710 struct v4l2_standard standard = { 0 };
711 struct v4l2_streamparm streamparm = { 0 };
712 struct v4l2_fract *tpf;
742 if (
v4l2_ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
749 "This device does not support any standard\n");
755 tpf = &standard.frameperiod;
763 if (standard.id == s->
std_id) {
765 "Current standard: %s, id: %"PRIu64
", frameperiod: %d/%d\n",
766 standard.name, (uint64_t)standard.id, tpf->numerator, tpf->denominator);
771 tpf = &streamparm.parm.capture.timeperframe;
774 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
775 if (
v4l2_ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) < 0) {
781 if (framerate_q.
num && framerate_q.
den) {
782 if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
783 tpf = &streamparm.parm.capture.timeperframe;
786 framerate_q.
den, framerate_q.
num);
787 tpf->numerator = framerate_q.
den;
788 tpf->denominator = framerate_q.
num;
790 if (
v4l2_ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) < 0) {
796 if (framerate_q.
num != tpf->denominator ||
797 framerate_q.
den != tpf->numerator) {
799 "The driver changed the time per frame from "
801 framerate_q.
den, framerate_q.
num,
802 tpf->numerator, tpf->denominator);
806 "The driver does not allow to change time per frame\n");
820 uint32_t *desired_format,
827 if (*desired_format) {
828 ret =
device_init(s1, width, height, *desired_format);
836 if (!*desired_format) {
844 *desired_format = fmt_conversion_table[i].
v4l2_fmt;
845 ret =
device_init(s1, width, height, *desired_format);
848 else if (ret !=
AVERROR(EINVAL))
854 if (*desired_format == 0) {
856 "codec '%s' (id %d), pixel format '%s' (id %d)\n",
873 uint32_t desired_format;
876 struct v4l2_input input = { 0 };
933 struct v4l2_format fmt;
936 "Querying the device for the current frame size\n");
937 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
944 s->
width = fmt.fmt.pix.width;
945 s->
height = fmt.fmt.pix.height;
947 "Setting frame size to %dx%d\n", s->
width, s->
height);
988 if (desired_format == V4L2_PIX_FMT_YVU420)
990 else if (desired_format == V4L2_PIX_FMT_YVU410)
1030 #define OFFSET(x) offsetof(struct video_data, x)
1031 #define DEC AV_OPT_FLAG_DECODING_PARAM
1066 .
name =
"video4linux2,v4l2",