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");
190 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
191 struct v4l2_pix_format *pix = &fmt.fmt.pix;
198 pix->field = V4L2_FIELD_ANY;
203 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
205 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
206 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
207 *width = fmt.fmt.pix.width;
208 *height = fmt.fmt.pix.height;
211 if (pix_fmt != fmt.fmt.pix.pixelformat) {
213 "The V4L2 driver changed the pixel format "
214 "from 0x%08X to 0x%08X\n",
215 pix_fmt, fmt.fmt.pix.pixelformat);
219 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
221 "The V4L2 driver is using the interlaced mode\n");
237 if (std & V4L2_STD_NTSC) {
244 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
245 static void list_framesizes(
AVFormatContext *ctx,
int fd, uint32_t pixelformat)
248 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
250 while(!
v4l2_ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
252 case V4L2_FRMSIZE_TYPE_DISCRETE:
254 vfse.discrete.width, vfse.discrete.height);
256 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
257 case V4L2_FRMSIZE_TYPE_STEPWISE:
259 vfse.stepwise.min_width,
260 vfse.stepwise.max_width,
261 vfse.stepwise.step_width,
262 vfse.stepwise.min_height,
263 vfse.stepwise.max_height,
264 vfse.stepwise.step_height);
274 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
276 while(!
v4l2_ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
282 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
286 fmt_name ? fmt_name :
"Unsupported",
288 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
292 codec ? codec->
name :
"Unsupported",
298 #ifdef V4L2_FMT_FLAG_EMULATED
299 if (vfd.flags & V4L2_FMT_FLAG_EMULATED)
302 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
303 list_framesizes(ctx, fd, vfd.pixelformat);
313 struct v4l2_standard standard;
318 for (standard.index = 0; ; standard.index++) {
329 standard.index, (uint64_t)standard.id, standard.name);
337 struct v4l2_requestbuffers req = {
338 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
340 .memory = V4L2_MEMORY_MMAP
366 for (i = 0; i < req.count; i++) {
367 struct v4l2_buffer buf = {
368 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
370 .memory = V4L2_MEMORY_MMAP
381 "buf_len[%d] = %d < expected frame size %d\n",
386 PROT_READ | PROT_WRITE, MAP_SHARED,
387 s->
fd, buf.m.offset);
399 #if FF_API_DESTRUCT_PACKET
408 struct v4l2_buffer buf = { 0 };
410 struct buff_data *buf_descriptor = opaque;
413 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
414 buf.memory = V4L2_MEMORY_MMAP;
415 buf.index = buf_descriptor->
index;
427 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
428 static int64_t av_gettime_monotonic(
void)
441 ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE) {
446 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
448 now = av_gettime_monotonic();
450 (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE)) {
476 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
479 int64_t nowm = av_gettime_monotonic();
491 struct v4l2_buffer buf = {
492 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
493 .memory = V4L2_MEMORY_MMAP
498 while ((res =
v4l2_ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
500 if (errno == EAGAIN) {
525 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
553 pkt->
size = buf.bytesused;
554 #if FF_API_DESTRUCT_PACKET
556 pkt->
destruct = dummy_release_buffer;
561 if (buf_descriptor == NULL) {
571 buf_descriptor->index = buf.index;
572 buf_descriptor->s =
s;
584 pkt->
pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
593 enum v4l2_buf_type
type;
596 for (i = 0; i < s->
buffers; i++) {
597 struct v4l2_buffer buf = {
598 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
600 .memory = V4L2_MEMORY_MMAP
611 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
623 enum v4l2_buf_type
type;
626 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
631 for (i = 0; i < s->
buffers; i++) {
641 struct v4l2_standard standard = { 0 };
642 struct v4l2_streamparm streamparm = { 0 };
643 struct v4l2_fract *tpf;
673 if (
v4l2_ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
680 "This device does not support any standard\n");
686 tpf = &standard.frameperiod;
692 tpf = &streamparm.parm.capture.timeperframe;
698 if (standard.id == s->
std_id) {
700 "Current standard: %s, id: %"PRIx64
", frameperiod: %d/%d\n",
701 standard.name, (uint64_t)standard.id, tpf->numerator, tpf->denominator);
706 tpf = &streamparm.parm.capture.timeperframe;
709 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
710 if (
v4l2_ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) < 0) {
716 if (framerate_q.
num && framerate_q.
den) {
717 if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
718 tpf = &streamparm.parm.capture.timeperframe;
721 framerate_q.
den, framerate_q.
num);
722 tpf->numerator = framerate_q.
den;
723 tpf->denominator = framerate_q.
num;
725 if (
v4l2_ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) < 0) {
731 if (framerate_q.
num != tpf->denominator ||
732 framerate_q.
den != tpf->numerator) {
734 "The driver changed the time per frame from "
736 framerate_q.
den, framerate_q.
num,
737 tpf->numerator, tpf->denominator);
741 "The driver does not allow to change time per frame\n");
744 if (tpf->denominator > 0 && tpf->numerator > 0) {
758 uint32_t *desired_format,
765 if (*desired_format) {
766 ret =
device_init(s1, width, height, *desired_format);
774 if (!*desired_format) {
783 ret =
device_init(s1, width, height, *desired_format);
786 else if (ret !=
AVERROR(EINVAL))
792 if (*desired_format == 0) {
794 "codec '%s' (id %d), pixel format '%s' (id %d)\n",
811 uint32_t desired_format;
814 struct v4l2_input input = { 0 };
824 v4l2_log_file = fopen(
"/dev/null",
"w");
856 av_log(s1,
AV_LOG_DEBUG,
"Current input_channel: %d, input_name: %s, input_std: %"PRIx64
"\n",
857 s->
channel, input.name, (uint64_t)input.std);
891 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
894 "Querying the device for the current frame size\n");
901 s->
width = fmt.fmt.pix.width;
902 s->
height = fmt.fmt.pix.height;
904 "Setting frame size to %dx%d\n", s->
width, s->
height);
945 if (desired_format == V4L2_PIX_FMT_YVU420)
947 else if (desired_format == V4L2_PIX_FMT_YVU410)
990 #define OFFSET(x) offsetof(struct video_data, x)
991 #define DEC AV_OPT_FLAG_DECODING_PARAM
1027 .
name =
"video4linux2,v4l2",