57#define V4L_ALLFORMATS 3
58#define V4L_RAWFORMATS 1
59#define V4L_COMPFORMATS 2
64#define V4L_TS_DEFAULT 0
74#define V4L_TS_MONO2ABS 2
81#define V4L_TS_CONVERT_READY V4L_TS_DEFAULT
115 int (*
open_f)(
const char *file,
int oflag, ...);
119 int (*
ioctl_f)(
int fd,
int request, ...);
136 struct v4l2_capability cap;
141#define SET_WRAPPERS(prefix) do { \
142 s->open_f = prefix ## open; \
143 s->close_f = prefix ## close; \
144 s->dup_f = prefix ## dup; \
145 s->ioctl_f = prefix ## ioctl; \
146 s->read_f = prefix ## read; \
147 s->mmap_f = prefix ## mmap; \
148 s->munmap_f = prefix ## munmap; \
151 if (
s->use_libv4l2) {
162#define v4l2_open s->open_f
163#define v4l2_close s->close_f
164#define v4l2_dup s->dup_f
165#define v4l2_ioctl s->ioctl_f
166#define v4l2_read s->read_f
167#define v4l2_mmap s->mmap_f
168#define v4l2_munmap s->munmap_f
182 if (
v4l2_ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
190 fd, cap.capabilities);
192 if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
194 s->buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
195 }
else if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
197 s->buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
204 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
206 "The device does not support the streaming I/O method.\n");
219 uint32_t pixelformat)
222 struct v4l2_format fmt = { .type =
s->buf_type };
225 fmt.fmt.pix.width = *
width;
226 fmt.fmt.pix.height = *
height;
227 fmt.fmt.pix.pixelformat = pixelformat;
228 fmt.fmt.pix.field = V4L2_FIELD_ANY;
235 if ((*
width != fmt.fmt.pix.width) || (*
height != fmt.fmt.pix.height)) {
237 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
238 *
width, *
height, fmt.fmt.pix.width, fmt.fmt.pix.height);
239 *
width = fmt.fmt.pix.width;
240 *
height = fmt.fmt.pix.height;
243 if (pixelformat != fmt.fmt.pix.pixelformat) {
245 "The V4L2 driver changed the pixel format "
246 "from 0x%08X to 0x%08X\n",
247 pixelformat, fmt.fmt.pix.pixelformat);
251 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
253 "The V4L2 driver is using the interlaced mode\n");
268 if (std & V4L2_STD_NTSC)
274#if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
278 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
280 while(!
v4l2_ioctl(
s->fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
282 case V4L2_FRMSIZE_TYPE_DISCRETE:
284 vfse.discrete.width, vfse.discrete.height);
286 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
287 case V4L2_FRMSIZE_TYPE_STEPWISE:
289 vfse.stepwise.min_width,
290 vfse.stepwise.max_width,
291 vfse.stepwise.step_width,
292 vfse.stepwise.min_height,
293 vfse.stepwise.max_height,
294 vfse.stepwise.step_height);
304 struct v4l2_fmtdesc vfd = { .type =
s->buf_type };
312 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
316 fmt_name ? fmt_name :
"Unsupported",
318 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
328#ifdef V4L2_FMT_FLAG_EMULATED
329 if (vfd.flags & V4L2_FMT_FLAG_EMULATED)
332#if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
333 list_framesizes(
ctx, vfd.pixelformat);
343 struct v4l2_standard standard;
348 for (standard.index = 0; ; standard.index++) {
349 if (
v4l2_ioctl(
s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
359 standard.index, (uint64_t)standard.id, standard.name);
365 for (
int i = 0;
i < n;
i++)
374 struct v4l2_requestbuffers req = {
377 .memory = V4L2_MEMORY_MMAP
390 s->buffers = req.count;
397 for (
i = 0;
i < req.count;
i++) {
398 unsigned int buf_length, buf_offset;
399 struct v4l2_plane
planes[VIDEO_MAX_PLANES];
400 struct v4l2_buffer buf = {
403 .memory = V4L2_MEMORY_MMAP,
405 .length =
s->multiplanar ? VIDEO_MAX_PLANES : 0,
407 if (
v4l2_ioctl(
s->fd, VIDIOC_QUERYBUF, &buf) < 0) {
413 if (
s->multiplanar) {
414 if (buf.length != 1) {
419 buf_length = buf.m.planes[0].length;
420 buf_offset = buf.m.planes[0].m.mem_offset;
422 buf_length = buf.length;
423 buf_offset = buf.m.offset;
426 s->buf_data[
i].len = buf_length;
427 if (
s->frame_size > 0 &&
s->buf_data[
i].len <
s->frame_size) {
429 "buf_data[%d].len = %d < expected frame size %d\n",
430 i, buf_length,
s->frame_size);
435 PROT_READ | PROT_WRITE, MAP_SHARED,
438 if (
s->buf_data[
i].start == MAP_FAILED) {
468 struct v4l2_plane
planes[VIDEO_MAX_PLANES];
469 struct v4l2_buffer buf = { 0 };
470 struct buf_desc *buf_descriptor = opaque;
473 buf.type =
s->buf_type;
474 buf.memory = V4L2_MEMORY_MMAP;
475 buf.index = buf_descriptor->
index;
477 buf.length =
s->multiplanar ? VIDEO_MAX_PLANES : 0;
483#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
484static int64_t av_gettime_monotonic(
void)
497 ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE) {
502#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
503 if (
ctx->streams[0]->avg_frame_rate.num) {
504 now = av_gettime_monotonic();
506 (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE)) {
532#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
535 int64_t nowm = av_gettime_monotonic();
537 s->last_time_m = nowm;
547 struct v4l2_plane
planes[VIDEO_MAX_PLANES];
548 struct v4l2_buffer buf = {
550 .memory = V4L2_MEMORY_MMAP,
552 .length =
s->multiplanar ? VIDEO_MAX_PLANES : 0,
554 struct timeval buf_ts;
555 unsigned int bytesused;
561 while ((res =
v4l2_ioctl(
s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
572 buf_ts = buf.timestamp;
574 if (buf.index >=
s->buffers) {
582 bytesused =
s->multiplanar ? buf.m.planes[0].bytesused : buf.bytesused;
584#ifdef V4L2_BUF_FLAG_ERROR
585 if (buf.flags & V4L2_BUF_FLAG_ERROR) {
587 "Dequeued v4l2 buffer contains corrupted data (%d bytes).\n",
596 s->frame_size = bytesused;
598 if (
s->frame_size > 0 && bytesused !=
s->frame_size) {
600 "Dequeued v4l2 buffer contains %d bytes, but %d were expected. Flags: 0x%08X.\n",
601 bytesused,
s->frame_size, buf.flags);
615 memcpy(
pkt->data,
s->buf_data[buf.index].start, bytesused);
625 pkt->data =
s->buf_data[buf.index].start;
626 pkt->size = bytesused;
629 if (!buf_descriptor) {
638 buf_descriptor->
index = buf.index;
639 buf_descriptor->
s =
s;
650 pkt->pts = buf_ts.tv_sec * INT64_C(1000000) + buf_ts.tv_usec;
659 enum v4l2_buf_type
type;
662 for (
i = 0;
i <
s->buffers;
i++) {
663 struct v4l2_plane
planes[VIDEO_MAX_PLANES];
664 struct v4l2_buffer buf = {
667 .memory = V4L2_MEMORY_MMAP,
669 .length =
s->multiplanar ? VIDEO_MAX_PLANES : 0,
694 enum v4l2_buf_type
type;
707 struct v4l2_standard standard = { 0 };
708 struct v4l2_streamparm streamparm = { 0 };
709 struct v4l2_fract *tpf;
727 if (
v4l2_ioctl(
s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
739 if (
v4l2_ioctl(
s->fd, VIDIOC_S_STD, &standard.id) < 0) {
746 "This device does not support any standard\n");
752 tpf = &standard.frameperiod;
755 if (
v4l2_ioctl(
s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
762 tpf = &streamparm.parm.capture.timeperframe;
768 if (standard.id ==
s->std_id) {
770 "Current standard: %s, id: %"PRIx64
", frameperiod: %d/%d\n",
771 standard.name, (uint64_t)standard.id, tpf->numerator, tpf->denominator);
776 tpf = &streamparm.parm.capture.timeperframe;
779 streamparm.type =
s->buf_type;
780 if (
v4l2_ioctl(
s->fd, VIDIOC_G_PARM, &streamparm) < 0) {
783 }
else if (framerate_q.
num && framerate_q.
den) {
784 if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
785 tpf = &streamparm.parm.capture.timeperframe;
788 framerate_q.
den, framerate_q.
num);
789 tpf->numerator = framerate_q.
den;
790 tpf->denominator = framerate_q.
num;
792 if (
v4l2_ioctl(
s->fd, VIDIOC_S_PARM, &streamparm) < 0) {
799 if (framerate_q.
num != tpf->denominator ||
800 framerate_q.
den != tpf->numerator) {
802 "The driver changed the time per frame from "
804 framerate_q.
den, framerate_q.
num,
805 tpf->numerator, tpf->denominator);
809 "The driver does not permit changing the time per frame\n");
812 if (tpf->denominator > 0 && tpf->numerator > 0) {
813 ctx->streams[0]->avg_frame_rate.num = tpf->denominator;
814 ctx->streams[0]->avg_frame_rate.den = tpf->numerator;
815 ctx->streams[0]->r_frame_rate =
ctx->streams[0]->avg_frame_rate;
826 uint32_t *desired_format,
833 if (*desired_format) {
842 if (!*desired_format) {
854 else if (ret !=
AVERROR(EINVAL))
860 if (*desired_format == 0) {
862 "codec '%s' (id %d), pixel format '%s' (id %d)\n",
887 uint32_t desired_format;
890 struct v4l2_input input = { 0 };
900 v4l2_log_file = fopen(
"/dev/null",
"w");
907 if (
s->channel != -1) {
910 if (
v4l2_ioctl(
s->fd, VIDIOC_S_INPUT, &
s->channel) < 0) {
917 if (
v4l2_ioctl(
s->fd, VIDIOC_G_INPUT, &
s->channel) < 0) {
925 input.index =
s->channel;
926 if (
v4l2_ioctl(
s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
931 s->std_id = input.std;
933 s->channel, input.name, (uint64_t)input.std);
935 if (
s->list_format) {
941 if (
s->list_standard) {
949 if (
s->pixel_format) {
953 ctx->video_codec_id =
desc->id;
966 if (!
s->width && !
s->height) {
967 struct v4l2_format fmt = { .type =
s->buf_type };
970 "Querying the device for the current frame size\n");
978 s->width = fmt.fmt.pix.width;
979 s->height = fmt.fmt.pix.height;
981 "Setting frame size to %dx%d\n",
s->width,
s->height);
998 s->pixelformat = desired_format;
1006 s->width,
s->height, 1);
1022 if (desired_format == V4L2_PIX_FMT_YVU420)
1024 else if (desired_format == V4L2_PIX_FMT_YVU410)
1066 return !strncmp(
name,
"video", 5) ||
1067 !strncmp(
name,
"radio", 5) ||
1068 !strncmp(
name,
"vbi", 3) ||
1069 !strncmp(
name,
"v4l-subdev", 10);
1076 struct dirent *
entry;
1082 dir = opendir(
"/dev");
1088 while ((
entry = readdir(dir))) {
1090 struct v4l2_capability cap;
1092 char device_name[256];
1098 if (
size >=
sizeof(device_name)) {
1107 if (
v4l2_ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
1145#define OFFSET(x) offsetof(struct video_data, x)
1146#define DEC AV_OPT_FLAG_DECODING_PARAM
1156 {
"list_formats",
"list available formats and exit",
OFFSET(list_format),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX,
DEC, .unit =
"list_formats" },
1161 {
"list_standards",
"list supported standards and exit",
OFFSET(list_standard),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1,
DEC, .unit =
"list_standards" },
1162 {
"all",
"show all supported standards",
OFFSET(list_standard),
AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0,
DEC, .unit =
"list_standards" },
1164 {
"timestamps",
"set type of timestamps for grabbed frames",
OFFSET(ts_mode),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2,
DEC, .unit =
"timestamps" },
1165 {
"ts",
"set type of timestamps for grabbed frames",
OFFSET(ts_mode),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2,
DEC, .unit =
"timestamps" },
1169 {
"use_libv4l2",
"use libv4l2 (v4l-utils) conversion functions",
OFFSET(use_libv4l2),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
DEC },
1174 .class_name =
"V4L2 indev",
1182 .p.name =
"video4linux2,v4l2",
const FFInputFormat ff_v4l2_demuxer
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
Main libavdevice API header.
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
static int read_probe(const AVProbeData *p)
void avpriv_stream_set_need_parsing(AVStream *st, enum AVStreamParseType type)
static enum AVPixelFormat pix_fmt
#define atomic_store(object, desired)
#define atomic_load(object)
channel
Use these values when setting the channel map with ebur128_set_channel().
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
AVCodecID
Identify the syntax and semantics of the bitstream.
const AVCodecDescriptor * avcodec_descriptor_get_by_name(const char *name)
unsigned int avcodec_pix_fmt_to_codec_tag(enum AVPixelFormat pix_fmt)
Return a value representing the fourCC code associated to the pixel format pix_fmt,...
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters.
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
#define AV_TIME_BASE
Internal time base represented as integer.
#define LIBAVUTIL_VERSION_INT
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static const struct @257111027162314367033347246032313251342043035002 planes[]
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
#define MKTAG(a, b, c, d)
Memory handling functions.
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
AVPixelFormat
Pixel format.
Describe the class of an AVClass context structure.
This struct describes the properties of a single codec described by an AVCodecID.
int height
The height of the video frame in pixels.
int width
The width of the video frame in pixels.
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
enum AVMediaType codec_type
General type of the encoded data.
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int nb_devices
number of autodetected devices
AVDeviceInfo ** devices
list of autodetected devices
Structure describes basic parameters of the device.
char * device_description
human friendly name
char * device_name
device name, format depends on device
This structure stores compressed data.
This structure contains the data a format has to probe a file.
Rational number (pair of numerator and denominator).
AVCodecParameters * codecpar
Codec parameters associated with this stream.
AVRational avg_frame_rate
Average framerate.
Opaque type representing a time filter state.
int(* ioctl_f)(int fd, unsigned long int request,...)
char * framerate
Set by a private option.
int list_standard
Set by a private option.
enum v4l2_buf_type buf_type
char * pixel_format
Set by a private option.
int(* munmap_f)(void *_start, size_t length)
ssize_t(* read_f)(int fd, void *buffer, size_t n)
atomic_int buffers_queued
int(* open_f)(const char *file, int oflag,...)
struct buf_data * buf_data
int list_format
Set by a private option.
#define av_malloc_array(a, b)
static AVFormatContext * ctx
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
int64_t av_gettime(void)
Get the current time in microseconds.
double ff_timefilter_eval(TimeFilter *self, double delta)
Evaluate the filter at a specified time.
double ff_timefilter_update(TimeFilter *self, double system_time, double period)
Update the filter.
void ff_timefilter_destroy(TimeFilter *self)
Free all resources associated with the filter.
TimeFilter * ff_timefilter_new(double time_base, double period, double bandwidth)
Create a new Delay Locked Loop time filter.
const struct fmt_map ff_fmt_conversion_table[]
enum AVCodecID ff_fmt_v4l2codec(uint32_t v4l2_fmt)
enum AVPixelFormat ff_fmt_v4l2ff(uint32_t v4l2_fmt, enum AVCodecID codec_id)
uint32_t ff_fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
static int device_try_init(AVFormatContext *ctx, enum AVPixelFormat pix_fmt, int *width, int *height, uint32_t *desired_format, enum AVCodecID *codec_id)
static int mmap_start(AVFormatContext *ctx)
#define SET_WRAPPERS(prefix)
#define V4L_TS_MONO2ABS
Assume kernel timestamps are from the monotonic clock and convert to absolute timestamps.
static void mmap_release_buffer(void *opaque, uint8_t *data)
static const int desired_video_buffers
static int init_convert_timestamp(AVFormatContext *ctx, int64_t ts)
static int convert_timestamp(AVFormatContext *ctx, int64_t *ts)
static int v4l2_is_v4l_dev(const char *name)
static int v4l2_get_device_list(AVFormatContext *ctx, AVDeviceInfoList *device_list)
static const AVClass v4l2_class
static int enqueue_buffer(struct video_data *s, struct v4l2_buffer *buf)
static int mmap_init(AVFormatContext *ctx)
static void mmap_free(struct video_data *s, int n)
static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
static int v4l2_read_close(AVFormatContext *ctx)
static void list_standards(AVFormatContext *ctx)
static int v4l2_read_packet(AVFormatContext *ctx, AVPacket *pkt)
#define V4L_TS_ABS
Autodetect the kind of timestamps returned by the kernel and convert to absolute (wall clock) timesta...
static int first_field(const struct video_data *s)
static void list_formats(AVFormatContext *ctx, int type)
#define V4L_TS_DEFAULT
Return timestamps to the user exactly as returned by the kernel.
static int v4l2_read_probe(const AVProbeData *p)
#define V4L_TS_CONVERT_READY
Once the kind of timestamps returned by the kernel have been detected, the value of the timefilter (N...
static int v4l2_read_header(AVFormatContext *ctx)
static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pixelformat)
static int v4l2_set_parameters(AVFormatContext *ctx)
static void mmap_close(struct video_data *s)
static int device_open(AVFormatContext *ctx, const char *device_path)
#define atomic_fetch_add(object, operand)