41 #define V4L_ALLFORMATS 3
42 #define V4L_RAWFORMATS 1
43 #define V4L_COMPFORMATS 2
48 #define V4L_TS_DEFAULT 0
58 #define V4L_TS_MONO2ABS 2
65 #define V4L_TS_CONVERT_READY V4L_TS_DEFAULT
92 int (*
open_f)(
const char *file,
int oflag, ...);
95 int (*
ioctl_f)(
int fd,
unsigned long int request, ...);
109 struct v4l2_capability cap;
114 #define SET_WRAPPERS(prefix) do { \
115 s->open_f = prefix ## open; \
116 s->close_f = prefix ## close; \
117 s->dup_f = prefix ## dup; \
118 s->ioctl_f = prefix ## ioctl; \
119 s->read_f = prefix ## read; \
120 s->mmap_f = prefix ## mmap; \
121 s->munmap_f = prefix ## munmap; \
135 #define v4l2_open s->open_f
136 #define v4l2_close s->close_f
137 #define v4l2_dup s->dup_f
138 #define v4l2_ioctl s->ioctl_f
139 #define v4l2_read s->read_f
140 #define v4l2_mmap s->mmap_f
141 #define v4l2_munmap s->munmap_f
155 if (
v4l2_ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
163 fd, cap.capabilities);
165 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
171 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
173 "The device does not support the streaming I/O method.\n");
186 uint32_t pixelformat)
189 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
192 fmt.fmt.pix.width = *
width;
193 fmt.fmt.pix.height = *
height;
194 fmt.fmt.pix.pixelformat = pixelformat;
195 fmt.fmt.pix.field = V4L2_FIELD_ANY;
202 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
204 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
205 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
206 *width = fmt.fmt.pix.width;
207 *height = fmt.fmt.pix.height;
210 if (pixelformat != fmt.fmt.pix.pixelformat) {
212 "The V4L2 driver changed the pixel format "
213 "from 0x%08X to 0x%08X\n",
214 pixelformat, fmt.fmt.pix.pixelformat);
218 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
220 "The V4L2 driver is using the interlaced mode\n");
235 if (std & V4L2_STD_NTSC)
241 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
242 static void list_framesizes(
AVFormatContext *ctx, uint32_t pixelformat)
245 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
247 while(!
v4l2_ioctl(s->
fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
249 case V4L2_FRMSIZE_TYPE_DISCRETE:
251 vfse.discrete.width, vfse.discrete.height);
253 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
254 case V4L2_FRMSIZE_TYPE_STEPWISE:
256 vfse.stepwise.min_width,
257 vfse.stepwise.max_width,
258 vfse.stepwise.step_width,
259 vfse.stepwise.min_height,
260 vfse.stepwise.max_height,
261 vfse.stepwise.step_height);
271 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
279 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
283 fmt_name ? fmt_name :
"Unsupported",
285 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
289 codec ? codec->
name :
"Unsupported",
295 #ifdef V4L2_FMT_FLAG_EMULATED
296 if (vfd.flags & V4L2_FMT_FLAG_EMULATED)
299 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
300 list_framesizes(ctx, vfd.pixelformat);
310 struct v4l2_standard standard;
315 for (standard.index = 0; ; standard.index++) {
326 standard.index, (uint64_t)standard.id, standard.name);
334 struct v4l2_requestbuffers req = {
335 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
337 .memory = V4L2_MEMORY_MMAP
363 for (i = 0; i < req.count; i++) {
364 struct v4l2_buffer buf = {
365 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
367 .memory = V4L2_MEMORY_MMAP
378 "buf_len[%d] = %d < expected frame size %d\n",
383 PROT_READ | PROT_WRITE, MAP_SHARED,
384 s->
fd, buf.m.offset);
396 #if FF_API_DESTRUCT_PACKET
419 struct v4l2_buffer buf = { 0 };
420 struct buff_data *buf_descriptor = opaque;
423 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
424 buf.memory = V4L2_MEMORY_MMAP;
425 buf.index = buf_descriptor->
index;
431 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
432 static int64_t av_gettime_monotonic(
void)
445 ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE) {
450 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
452 now = av_gettime_monotonic();
454 (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE)) {
480 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
483 int64_t nowm = av_gettime_monotonic();
495 struct v4l2_buffer buf = {
496 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
497 .memory = V4L2_MEMORY_MMAP
502 while ((res =
v4l2_ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
504 if (errno == EAGAIN) {
530 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
556 pkt->
size = buf.bytesused;
557 #if FF_API_DESTRUCT_PACKET
559 pkt->
destruct = dummy_release_buffer;
564 if (!buf_descriptor) {
573 buf_descriptor->index = buf.index;
574 buf_descriptor->s =
s;
585 pkt->
pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
594 enum v4l2_buf_type
type;
597 for (i = 0; i < s->
buffers; i++) {
598 struct v4l2_buffer buf = {
599 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
601 .memory = V4L2_MEMORY_MMAP
613 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
626 enum v4l2_buf_type
type;
629 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
634 for (i = 0; i < s->
buffers; i++) {
644 struct v4l2_standard standard = { 0 };
645 struct v4l2_streamparm streamparm = { 0 };
646 struct v4l2_fract *tpf;
676 if (
v4l2_ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
683 "This device does not support any standard\n");
689 tpf = &standard.frameperiod;
699 tpf = &streamparm.parm.capture.timeperframe;
705 if (standard.id == s->
std_id) {
707 "Current standard: %s, id: %"PRIx64
", frameperiod: %d/%d\n",
708 standard.name, (uint64_t)standard.id, tpf->numerator, tpf->denominator);
713 tpf = &streamparm.parm.capture.timeperframe;
716 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
717 if (
v4l2_ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) < 0) {
723 if (framerate_q.
num && framerate_q.
den) {
724 if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
725 tpf = &streamparm.parm.capture.timeperframe;
728 framerate_q.
den, framerate_q.
num);
729 tpf->numerator = framerate_q.
den;
730 tpf->denominator = framerate_q.
num;
732 if (
v4l2_ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) < 0) {
739 if (framerate_q.
num != tpf->denominator ||
740 framerate_q.
den != tpf->numerator) {
742 "The driver changed the time per frame from "
744 framerate_q.
den, framerate_q.
num,
745 tpf->numerator, tpf->denominator);
749 "The driver does not allow to change time per frame\n");
752 if (tpf->denominator > 0 && tpf->numerator > 0) {
766 uint32_t *desired_format,
773 if (*desired_format) {
774 ret =
device_init(ctx, width, height, *desired_format);
782 if (!*desired_format) {
791 ret =
device_init(ctx, width, height, *desired_format);
794 else if (ret !=
AVERROR(EINVAL))
800 if (*desired_format == 0) {
802 "codec '%s' (id %d), pixel format '%s' (id %d)\n",
826 uint32_t desired_format;
829 struct v4l2_input input = { 0 };
839 v4l2_log_file = fopen(
"/dev/null",
"w");
871 av_log(ctx,
AV_LOG_DEBUG,
"Current input_channel: %d, input_name: %s, input_std: %"PRIx64
"\n",
872 s->
channel, input.name, (uint64_t)input.std);
906 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
909 "Querying the device for the current frame size\n");
917 s->
width = fmt.fmt.pix.width;
918 s->
height = fmt.fmt.pix.height;
920 "Setting frame size to %dx%d\n", s->
width, s->
height);
960 if (desired_format == V4L2_PIX_FMT_YVU420)
962 else if (desired_format == V4L2_PIX_FMT_YVU410)
1009 #define OFFSET(x) offsetof(struct video_data, x)
1010 #define DEC AV_OPT_FLAG_DECODING_PARAM
1046 .
name =
"video4linux2,v4l2",