42 #define V4L_ALLFORMATS 3
43 #define V4L_RAWFORMATS 1
44 #define V4L_COMPFORMATS 2
49 #define V4L_TS_DEFAULT 0
59 #define V4L_TS_MONO2ABS 2
66 #define V4L_TS_CONVERT_READY V4L_TS_DEFAULT
93 int (*
open_f)(
const char *file,
int oflag, ...);
96 int (*
ioctl_f)(
int fd,
unsigned long int request, ...);
110 struct v4l2_capability cap;
115 #define SET_WRAPPERS(prefix) do { \
116 s->open_f = prefix ## open; \
117 s->close_f = prefix ## close; \
118 s->dup_f = prefix ## dup; \
119 s->ioctl_f = prefix ## ioctl; \
120 s->read_f = prefix ## read; \
121 s->mmap_f = prefix ## mmap; \
122 s->munmap_f = prefix ## munmap; \
136 #define v4l2_open s->open_f
137 #define v4l2_close s->close_f
138 #define v4l2_dup s->dup_f
139 #define v4l2_ioctl s->ioctl_f
140 #define v4l2_read s->read_f
141 #define v4l2_mmap s->mmap_f
142 #define v4l2_munmap s->munmap_f
156 if (
v4l2_ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
164 fd, cap.capabilities);
166 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
172 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
174 "The device does not support the streaming I/O method.\n");
187 uint32_t pixelformat)
190 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
193 fmt.fmt.pix.width = *
width;
194 fmt.fmt.pix.height = *
height;
195 fmt.fmt.pix.pixelformat = pixelformat;
196 fmt.fmt.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 (pixelformat != fmt.fmt.pix.pixelformat) {
213 "The V4L2 driver changed the pixel format "
214 "from 0x%08X to 0x%08X\n",
215 pixelformat, fmt.fmt.pix.pixelformat);
219 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
221 "The V4L2 driver is using the interlaced mode\n");
236 if (std & V4L2_STD_NTSC)
242 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
243 static void list_framesizes(
AVFormatContext *ctx, uint32_t pixelformat)
246 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
248 while(!
v4l2_ioctl(s->
fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
250 case V4L2_FRMSIZE_TYPE_DISCRETE:
252 vfse.discrete.width, vfse.discrete.height);
254 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
255 case V4L2_FRMSIZE_TYPE_STEPWISE:
257 vfse.stepwise.min_width,
258 vfse.stepwise.max_width,
259 vfse.stepwise.step_width,
260 vfse.stepwise.min_height,
261 vfse.stepwise.max_height,
262 vfse.stepwise.step_height);
272 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
280 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
284 fmt_name ? fmt_name :
"Unsupported",
286 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
290 desc ? desc->
name :
"Unsupported",
296 #ifdef V4L2_FMT_FLAG_EMULATED
297 if (vfd.flags & V4L2_FMT_FLAG_EMULATED)
300 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
301 list_framesizes(ctx, vfd.pixelformat);
311 struct v4l2_standard standard;
316 for (standard.index = 0; ; standard.index++) {
327 standard.index, (uint64_t)standard.id, standard.name);
335 struct v4l2_requestbuffers req = {
336 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
338 .memory = V4L2_MEMORY_MMAP
364 for (i = 0; i < req.count; i++) {
365 struct v4l2_buffer buf = {
366 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
368 .memory = V4L2_MEMORY_MMAP
379 "buf_len[%d] = %d < expected frame size %d\n",
384 PROT_READ | PROT_WRITE, MAP_SHARED,
385 s->
fd, buf.m.offset);
397 #if FF_API_DESTRUCT_PACKET
420 struct v4l2_buffer buf = { 0 };
421 struct buff_data *buf_descriptor = opaque;
424 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
425 buf.memory = V4L2_MEMORY_MMAP;
426 buf.index = buf_descriptor->
index;
432 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
433 static int64_t av_gettime_monotonic(
void)
446 ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE) {
451 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
453 now = av_gettime_monotonic();
455 (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE)) {
481 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
484 int64_t nowm = av_gettime_monotonic();
496 struct v4l2_buffer buf = {
497 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
498 .memory = V4L2_MEMORY_MMAP
503 while ((res =
v4l2_ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
505 if (errno == EAGAIN) {
531 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
557 pkt->
size = buf.bytesused;
558 #if FF_API_DESTRUCT_PACKET
560 pkt->
destruct = dummy_release_buffer;
565 if (!buf_descriptor) {
574 buf_descriptor->index = buf.index;
575 buf_descriptor->s =
s;
586 pkt->
pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
595 enum v4l2_buf_type
type;
598 for (i = 0; i < s->
buffers; i++) {
599 struct v4l2_buffer buf = {
600 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
602 .memory = V4L2_MEMORY_MMAP
614 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
627 enum v4l2_buf_type
type;
630 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
635 for (i = 0; i < s->
buffers; i++) {
645 struct v4l2_standard standard = { 0 };
646 struct v4l2_streamparm streamparm = { 0 };
647 struct v4l2_fract *tpf;
677 if (
v4l2_ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
684 "This device does not support any standard\n");
690 tpf = &standard.frameperiod;
700 tpf = &streamparm.parm.capture.timeperframe;
706 if (standard.id == s->
std_id) {
708 "Current standard: %s, id: %"PRIx64
", frameperiod: %d/%d\n",
709 standard.name, (uint64_t)standard.id, tpf->numerator, tpf->denominator);
714 tpf = &streamparm.parm.capture.timeperframe;
717 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
718 if (
v4l2_ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) < 0) {
724 if (framerate_q.
num && framerate_q.
den) {
725 if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
726 tpf = &streamparm.parm.capture.timeperframe;
729 framerate_q.
den, framerate_q.
num);
730 tpf->numerator = framerate_q.
den;
731 tpf->denominator = framerate_q.
num;
733 if (
v4l2_ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) < 0) {
740 if (framerate_q.
num != tpf->denominator ||
741 framerate_q.
den != tpf->numerator) {
743 "The driver changed the time per frame from "
745 framerate_q.
den, framerate_q.
num,
746 tpf->numerator, tpf->denominator);
750 "The driver does not permit changing the time per frame\n");
753 if (tpf->denominator > 0 && tpf->numerator > 0) {
767 uint32_t *desired_format,
774 if (*desired_format) {
775 ret =
device_init(ctx, width, height, *desired_format);
783 if (!*desired_format) {
792 ret =
device_init(ctx, width, height, *desired_format);
795 else if (ret !=
AVERROR(EINVAL))
801 if (*desired_format == 0) {
803 "codec '%s' (id %d), pixel format '%s' (id %d)\n",
827 uint32_t desired_format;
830 struct v4l2_input input = { 0 };
840 v4l2_log_file = fopen(
"/dev/null",
"w");
872 av_log(ctx,
AV_LOG_DEBUG,
"Current input_channel: %d, input_name: %s, input_std: %"PRIx64
"\n",
873 s->
channel, input.name, (uint64_t)input.std);
907 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
910 "Querying the device for the current frame size\n");
918 s->
width = fmt.fmt.pix.width;
919 s->
height = fmt.fmt.pix.height;
921 "Setting frame size to %dx%d\n", s->
width, s->
height);
961 if (desired_format == V4L2_PIX_FMT_YVU420)
963 else if (desired_format == V4L2_PIX_FMT_YVU410)
1012 return !strncmp(name,
"video", 5) ||
1013 !strncmp(name,
"radio", 5) ||
1014 !strncmp(name,
"vbi", 3) ||
1015 !strncmp(name,
"v4l-subdev", 10);
1022 struct dirent *entry;
1024 struct v4l2_capability cap;
1030 dir = opendir(
"/dev");
1036 while ((entry = readdir(dir))) {
1085 #define OFFSET(x) offsetof(struct video_data, x)
1086 #define DEC AV_OPT_FLAG_DECODING_PARAM
1090 {
"channel",
"set TV channel, used only by frame grabber",
OFFSET(channel),
AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX,
DEC },
1096 {
"list_formats",
"list available formats and exit",
OFFSET(list_format),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX,
DEC,
"list_formats" },
1101 {
"list_standards",
"list supported standards and exit",
OFFSET(list_standard),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1,
DEC,
"list_standards" },
1102 {
"all",
"show all supported standards",
OFFSET(list_standard),
AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0,
DEC,
"list_standards" },
1104 {
"timestamps",
"set type of timestamps for grabbed frames",
OFFSET(ts_mode),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2,
DEC,
"timestamps" },
1105 {
"ts",
"set type of timestamps for grabbed frames",
OFFSET(ts_mode),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2,
DEC,
"timestamps" },
1109 {
"use_libv4l2",
"use libv4l2 (v4l-utils) conversion functions",
OFFSET(use_libv4l2),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
DEC },
1122 .
name =
"video4linux2,v4l2",