30 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
36 #include <sys/ioctl.h>
40 #if HAVE_SYS_VIDEOIO_H
41 #include <sys/videoio.h>
43 #include <linux/videodev2.h>
55 #define V4L_ALLFORMATS 3
56 #define V4L_RAWFORMATS 1
57 #define V4L_COMPFORMATS 2
113 struct v4l2_capability cap;
132 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
142 fd, cap.capabilities);
144 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
151 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
153 "The device does not support the streaming I/O method.\n");
171 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
172 struct v4l2_pix_format *pix = &fmt.fmt.pix;
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)) {
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) {
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) {
212 res = ioctl(fd, VIDIOC_G_STD, &std);
216 if (std & V4L2_STD_NTSC) {
229 fmt_conversion_table[i].codec_id == codec_id) &&
232 return fmt_conversion_table[i].
v4l2_fmt;
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;
258 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
259 return fmt_conversion_table[i].
codec_id;
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);
294 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
296 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
302 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
306 fmt_name ? fmt_name :
"Unsupported",
308 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
312 codec ? codec->
name :
"Unsupported",
318 #ifdef V4L2_FMT_FLAG_EMULATED
319 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
324 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
325 list_framesizes(ctx, fd, vfd.pixelformat);
335 struct v4l2_requestbuffers req = {
336 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
338 .memory = V4L2_MEMORY_MMAP
341 res = ioctl(s->
fd, VIDIOC_REQBUFS, &req);
343 if (errno == EINVAL) {
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);
389 "Buffer len [%d] = %d != %d\n",
395 PROT_READ | PROT_WRITE, MAP_SHARED,
396 s->
fd, buf.m.offset);
410 struct v4l2_buffer buf = { 0 };
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;
423 res = ioctl(fd, VIDIOC_QBUF, &buf);
435 struct v4l2_buffer buf = {
436 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
437 .memory = V4L2_MEMORY_MMAP
440 struct pollfd p = { .
fd = s->
fd, .events = POLLIN };
447 if (!(p.revents & (POLLIN | POLLERR | POLLHUP)))
451 while ((res = ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
453 if (errno == EAGAIN) {
463 assert (buf.index < s->
buffers);
466 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
474 pkt->
size = buf.bytesused;
475 pkt->
pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
478 if (buf_descriptor ==
NULL) {
483 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
487 buf_descriptor->
fd = s->
fd;
488 buf_descriptor->
index = buf.index;
489 pkt->
priv = buf_descriptor;
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);
516 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
517 res = ioctl(s->
fd, VIDIOC_STREAMON, &type);
530 enum v4l2_buf_type type;
533 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
537 ioctl(s->
fd, VIDIOC_STREAMOFF, &type);
538 for (i = 0; i < s->
buffers; i++) {
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;
555 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
566 if (ioctl(s->
fd, VIDIOC_ENUMINPUT, &input) < 0) {
573 if (ioctl(s->
fd, VIDIOC_S_INPUT, &input.index) < 0) {
575 "The V4L2 driver ioctl set input(%d) failed\n",
586 if (ioctl(s->
fd, VIDIOC_ENUMSTD, &standard) < 0) {
588 "The V4L2 driver ioctl set standard(%s) failed\n",
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) {
603 "The V4L2 driver ioctl set standard(%s) failed\n",
609 if (framerate_q.
num && framerate_q.
den) {
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) {
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) {
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) {
655 if (desired_format == 0 ||
656 device_init(s1, width, height, desired_format) < 0) {
663 desired_format = fmt_conversion_table[i].
v4l2_fmt;
664 if (
device_init(s1, width, height, desired_format) >= 0) {
672 if (desired_format != 0) {
677 return desired_format;
685 uint32_t desired_format;
734 struct v4l2_format fmt;
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) {
746 s->
width = fmt.fmt.pix.width;
747 s->
height = fmt.fmt.pix.height;
749 "Setting frame size to %dx%d\n", s->
width, s->
height);
754 if (desired_format == 0) {
825 #define OFFSET(x) offsetof(struct video_data, x)
826 #define DEC AV_OPT_FLAG_DECODING_PARAM
849 .
name =
"video4linux2",