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>
58 #define V4L_ALLFORMATS 3
59 #define V4L_RAWFORMATS 1
60 #define V4L_COMPFORMATS 2
118 struct v4l2_capability cap;
137 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
147 fd, cap.capabilities);
149 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
156 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
158 "The device does not support the streaming I/O method.\n");
176 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
177 struct v4l2_pix_format *pix = &fmt.fmt.pix;
184 pix->field = V4L2_FIELD_ANY;
186 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
188 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
190 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
191 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
192 *width = fmt.fmt.pix.width;
193 *height = fmt.fmt.pix.height;
196 if (pix_fmt != fmt.fmt.pix.pixelformat) {
198 "The V4L2 driver changed the pixel format "
199 "from 0x%08X to 0x%08X\n",
200 pix_fmt, fmt.fmt.pix.pixelformat);
204 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
217 res = ioctl(fd, VIDIOC_G_STD, &std);
221 if (std & V4L2_STD_NTSC) {
234 fmt_conversion_table[i].codec_id == codec_id) &&
237 return fmt_conversion_table[i].
v4l2_fmt;
249 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
250 fmt_conversion_table[i].codec_id == codec_id) {
251 return fmt_conversion_table[i].
ff_fmt;
263 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
264 return fmt_conversion_table[i].
codec_id;
271 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
272 static void list_framesizes(
AVFormatContext *ctx,
int fd, uint32_t pixelformat)
274 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
276 while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
278 case V4L2_FRMSIZE_TYPE_DISCRETE:
280 vfse.discrete.width, vfse.discrete.height);
282 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
283 case V4L2_FRMSIZE_TYPE_STEPWISE:
285 vfse.stepwise.min_width,
286 vfse.stepwise.max_width,
287 vfse.stepwise.step_width,
288 vfse.stepwise.min_height,
289 vfse.stepwise.max_height,
290 vfse.stepwise.step_height);
299 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
301 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
307 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
311 fmt_name ? fmt_name :
"Unsupported",
313 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
317 codec ? codec->
name :
"Unsupported",
323 #ifdef V4L2_FMT_FLAG_EMULATED
324 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
329 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
330 list_framesizes(ctx, fd, vfd.pixelformat);
340 struct v4l2_requestbuffers req = {
341 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
343 .memory = V4L2_MEMORY_MMAP
346 res = ioctl(s->
fd, VIDIOC_REQBUFS, &req);
348 if (errno == EINVAL) {
377 for (i = 0; i < req.count; i++) {
378 struct v4l2_buffer buf = {
379 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
381 .memory = V4L2_MEMORY_MMAP
384 res = ioctl(s->
fd, VIDIOC_QUERYBUF, &buf);
394 "Buffer len [%d] = %d != %d\n",
400 PROT_READ | PROT_WRITE, MAP_SHARED,
401 s->
fd, buf.m.offset);
413 #if FF_API_DESTRUCT_PACKET
414 static void dummy_release_buffer(
AVPacket *pkt)
422 struct v4l2_buffer buf = { 0 };
424 struct buff_data *buf_descriptor = opaque;
427 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
428 buf.memory = V4L2_MEMORY_MMAP;
429 buf.index = buf_descriptor->
index;
430 fd = buf_descriptor->
fd;
433 res = ioctl(fd, VIDIOC_QBUF, &buf);
443 struct v4l2_buffer buf = {
444 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
445 .memory = V4L2_MEMORY_MMAP
447 struct pollfd p = { .fd = s->
fd, .events = POLLIN };
454 if (!(p.revents & (POLLIN | POLLERR | POLLHUP)))
458 while ((res = ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
460 if (errno == EAGAIN) {
481 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
497 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
508 pkt->
size = buf.bytesused;
509 #if FF_API_DESTRUCT_PACKET
511 pkt->
destruct = dummy_release_buffer;
516 if (buf_descriptor ==
NULL) {
521 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
525 buf_descriptor->fd = s->
fd;
526 buf_descriptor->index = buf.index;
527 buf_descriptor->s =
s;
536 pkt->
pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
544 enum v4l2_buf_type type;
547 for (i = 0; i < s->
buffers; i++) {
548 struct v4l2_buffer buf = {
549 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
551 .memory = V4L2_MEMORY_MMAP
554 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
564 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
565 res = ioctl(s->
fd, VIDIOC_STREAMON, &type);
578 enum v4l2_buf_type type;
581 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
585 ioctl(s->
fd, VIDIOC_STREAMOFF, &type);
586 for (i = 0; i < s->
buffers; i++) {
596 struct v4l2_input input = { 0 };
597 struct v4l2_standard standard = { 0 };
598 struct v4l2_streamparm streamparm = { 0 };
599 struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
603 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
614 if (ioctl(s->
fd, VIDIOC_ENUMINPUT, &input) < 0) {
621 if (ioctl(s->
fd, VIDIOC_S_INPUT, &input.index) < 0) {
623 "The V4L2 driver ioctl set input(%d) failed\n",
634 if (ioctl(s->
fd, VIDIOC_ENUMSTD, &standard) < 0) {
636 "The V4L2 driver ioctl set standard(%s) failed\n",
647 "The V4L2 driver set standard: %s, id: %"PRIu64
"\n",
648 s->
standard, (uint64_t)standard.id);
649 if (ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
651 "The V4L2 driver ioctl set standard(%s) failed\n",
657 if (framerate_q.
num && framerate_q.
den) {
659 framerate_q.
den, framerate_q.
num);
660 tpf->numerator = framerate_q.
den;
661 tpf->denominator = framerate_q.
num;
663 if (ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) != 0) {
665 "ioctl set time per frame(%d/%d) failed\n",
666 framerate_q.
den, framerate_q.
num);
670 if (framerate_q.
num != tpf->denominator ||
671 framerate_q.
den != tpf->numerator) {
673 "The driver changed the time per frame from "
675 framerate_q.
den, framerate_q.
num,
676 tpf->numerator, tpf->denominator);
679 if (ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) != 0) {
703 if (desired_format == 0 ||
704 device_init(s1, width, height, desired_format) < 0) {
711 desired_format = fmt_conversion_table[i].
v4l2_fmt;
712 if (
device_init(s1, width, height, desired_format) >= 0) {
720 if (desired_format != 0) {
725 return desired_format;
733 uint32_t desired_format;
776 struct v4l2_format fmt;
779 "Querying the device for the current frame size\n");
780 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
781 if (ioctl(s->
fd, VIDIOC_G_FMT, &fmt) < 0) {
787 s->
width = fmt.fmt.pix.width;
788 s->
height = fmt.fmt.pix.height;
790 "Setting frame size to %dx%d\n", s->
width, s->
height);
795 if (desired_format == 0) {
868 #define OFFSET(x) offsetof(struct video_data, x)
869 #define DEC AV_OPT_FLAG_DECODING_PARAM
892 .
name =
"video4linux2",