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>
56 #define V4L_ALLFORMATS 3
57 #define V4L_RAWFORMATS 1
58 #define V4L_COMPFORMATS 2
114 struct v4l2_capability cap;
133 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
143 fd, cap.capabilities);
145 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
152 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
154 "The device does not support the streaming I/O method.\n");
172 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
173 struct v4l2_pix_format *pix = &fmt.fmt.pix;
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)) {
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) {
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) {
213 res = ioctl(fd, VIDIOC_G_STD, &std);
217 if (std & V4L2_STD_NTSC) {
230 fmt_conversion_table[i].codec_id == codec_id) &&
233 return fmt_conversion_table[i].
v4l2_fmt;
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;
259 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
260 return fmt_conversion_table[i].
codec_id;
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:
276 vfse.discrete.width, vfse.discrete.height);
278 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
279 case V4L2_FRMSIZE_TYPE_STEPWISE:
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);
295 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
297 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
303 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
307 fmt_name ? fmt_name :
"Unsupported",
309 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
313 codec ? codec->
name :
"Unsupported",
319 #ifdef V4L2_FMT_FLAG_EMULATED
320 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
325 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
326 list_framesizes(ctx, fd, vfd.pixelformat);
336 struct v4l2_requestbuffers req = {
337 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
339 .memory = V4L2_MEMORY_MMAP
342 res = ioctl(s->
fd, VIDIOC_REQBUFS, &req);
344 if (errno == EINVAL) {
373 for (i = 0; i < req.count; i++) {
374 struct v4l2_buffer buf = {
375 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
377 .memory = V4L2_MEMORY_MMAP
380 res = ioctl(s->
fd, VIDIOC_QUERYBUF, &buf);
390 "Buffer len [%d] = %d != %d\n",
396 PROT_READ | PROT_WRITE, MAP_SHARED,
397 s->
fd, buf.m.offset);
411 struct v4l2_buffer buf = { 0 };
418 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
419 buf.memory = V4L2_MEMORY_MMAP;
420 buf.index = buf_descriptor->
index;
421 fd = buf_descriptor->
fd;
424 res = ioctl(fd, VIDIOC_QBUF, &buf);
436 struct v4l2_buffer buf = {
437 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
438 .memory = V4L2_MEMORY_MMAP
441 struct pollfd p = { .
fd = s->
fd, .events = POLLIN };
448 if (!(p.revents & (POLLIN | POLLERR | POLLHUP)))
452 while ((res = ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
454 if (errno == EAGAIN) {
464 assert (buf.index < s->
buffers);
467 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
475 pkt->
size = buf.bytesused;
476 pkt->
pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
479 if (buf_descriptor ==
NULL) {
484 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
488 buf_descriptor->
fd = s->
fd;
489 buf_descriptor->
index = buf.index;
490 pkt->
priv = buf_descriptor;
498 enum v4l2_buf_type type;
501 for (i = 0; i < s->
buffers; i++) {
502 struct v4l2_buffer buf = {
503 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
505 .memory = V4L2_MEMORY_MMAP
508 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
517 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
518 res = ioctl(s->
fd, VIDIOC_STREAMON, &type);
531 enum v4l2_buf_type type;
534 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
538 ioctl(s->
fd, VIDIOC_STREAMOFF, &type);
539 for (i = 0; i < s->
buffers; i++) {
549 struct v4l2_input input = { 0 };
550 struct v4l2_standard standard = { 0 };
551 struct v4l2_streamparm streamparm = { 0 };
552 struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
556 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
567 if (ioctl(s->
fd, VIDIOC_ENUMINPUT, &input) < 0) {
574 if (ioctl(s->
fd, VIDIOC_S_INPUT, &input.index) < 0) {
576 "The V4L2 driver ioctl set input(%d) failed\n",
587 if (ioctl(s->
fd, VIDIOC_ENUMSTD, &standard) < 0) {
589 "The V4L2 driver ioctl set standard(%s) failed\n",
600 "The V4L2 driver set standard: %s, id: %"PRIu64
"\n",
601 s->
standard, (uint64_t)standard.id);
602 if (ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
604 "The V4L2 driver ioctl set standard(%s) failed\n",
610 if (framerate_q.
num && framerate_q.
den) {
612 framerate_q.
den, framerate_q.
num);
613 tpf->numerator = framerate_q.
den;
614 tpf->denominator = framerate_q.
num;
616 if (ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) != 0) {
618 "ioctl set time per frame(%d/%d) failed\n",
619 framerate_q.
den, framerate_q.
num);
623 if (framerate_q.
num != tpf->denominator ||
624 framerate_q.
den != tpf->numerator) {
626 "The driver changed the time per frame from "
628 framerate_q.
den, framerate_q.
num,
629 tpf->numerator, tpf->denominator);
632 if (ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) != 0) {
656 if (desired_format == 0 ||
657 device_init(s1, width, height, desired_format) < 0) {
664 desired_format = fmt_conversion_table[i].
v4l2_fmt;
665 if (
device_init(s1, width, height, desired_format) >= 0) {
673 if (desired_format != 0) {
678 return desired_format;
686 uint32_t desired_format;
735 struct v4l2_format fmt;
738 "Querying the device for the current frame size\n");
739 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
740 if (ioctl(s->
fd, VIDIOC_G_FMT, &fmt) < 0) {
747 s->
width = fmt.fmt.pix.width;
748 s->
height = fmt.fmt.pix.height;
750 "Setting frame size to %dx%d\n", s->
width, s->
height);
755 if (desired_format == 0) {
826 #define OFFSET(x) offsetof(struct video_data, x)
827 #define DEC AV_OPT_FLAG_DECODING_PARAM
850 .
name =
"video4linux2",