33 #include <stdatomic.h> 44 #define V4L_ALLFORMATS 3 45 #define V4L_RAWFORMATS 1 46 #define V4L_COMPFORMATS 2 51 #define V4L_TS_DEFAULT 0 61 #define V4L_TS_MONO2ABS 2 68 #define V4L_TS_CONVERT_READY V4L_TS_DEFAULT 116 struct v4l2_capability cap;
121 #define SET_WRAPPERS(prefix) do { \ 122 s->open_f = prefix ## open; \ 123 s->close_f = prefix ## close; \ 124 s->dup_f = prefix ## dup; \ 125 s->ioctl_f = prefix ## ioctl; \ 126 s->read_f = prefix ## read; \ 127 s->mmap_f = prefix ## mmap; \ 128 s->munmap_f = prefix ## munmap; \ 142 #define v4l2_open s->open_f 143 #define v4l2_close s->close_f 144 #define v4l2_dup s->dup_f 145 #define v4l2_ioctl s->ioctl_f 146 #define v4l2_read s->read_f 147 #define v4l2_mmap s->mmap_f 148 #define v4l2_munmap s->munmap_f 162 if (
v4l2_ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
170 fd, cap.capabilities);
172 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
178 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
180 "The device does not support the streaming I/O method.\n");
196 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
199 fmt.fmt.pix.width = *
width;
200 fmt.fmt.pix.height = *
height;
202 fmt.fmt.pix.field = V4L2_FIELD_ANY;
209 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
211 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
212 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
213 *width = fmt.fmt.pix.width;
214 *height = fmt.fmt.pix.height;
217 if (pixelformat != fmt.fmt.pix.pixelformat) {
219 "The V4L2 driver changed the pixel format " 220 "from 0x%08X to 0x%08X\n",
221 pixelformat, fmt.fmt.pix.pixelformat);
225 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
227 "The V4L2 driver is using the interlaced mode\n");
242 if (std & V4L2_STD_NTSC)
248 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE 252 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
254 while(!
v4l2_ioctl(s->
fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
256 case V4L2_FRMSIZE_TYPE_DISCRETE:
258 vfse.discrete.width, vfse.discrete.height);
260 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
261 case V4L2_FRMSIZE_TYPE_STEPWISE:
263 vfse.stepwise.min_width,
264 vfse.stepwise.max_width,
265 vfse.stepwise.step_width,
266 vfse.stepwise.min_height,
267 vfse.stepwise.max_height,
268 vfse.stepwise.step_height);
278 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
286 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
290 fmt_name ? fmt_name :
"Unsupported",
292 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
296 desc ? desc->
name :
"Unsupported",
302 #ifdef V4L2_FMT_FLAG_EMULATED 303 if (vfd.flags & V4L2_FMT_FLAG_EMULATED)
306 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE 307 list_framesizes(ctx, vfd.pixelformat);
317 struct v4l2_standard standard;
322 for (standard.index = 0; ; standard.index++) {
333 standard.index, (uint64_t)standard.id, standard.name);
341 struct v4l2_requestbuffers req = {
342 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
344 .memory = V4L2_MEMORY_MMAP
370 for (i = 0; i < req.count; i++) {
371 struct v4l2_buffer buf = {
372 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
374 .memory = V4L2_MEMORY_MMAP
385 "buf_len[%d] = %d < expected frame size %d\n",
390 PROT_READ | PROT_WRITE, MAP_SHARED,
391 s->
fd, buf.m.offset);
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
499 struct timeval buf_ts;
505 while ((res =
v4l2_ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
516 buf_ts = buf.timestamp;
526 #ifdef V4L2_BUF_FLAG_ERROR 527 if (buf.flags & V4L2_BUF_FLAG_ERROR) {
529 "Dequeued v4l2 buffer contains corrupted data (%d bytes).\n",
542 "Dequeued v4l2 buffer contains %d bytes, but %d were expected. Flags: 0x%08X.\n",
568 pkt->
size = buf.bytesused;
571 if (!buf_descriptor) {
580 buf_descriptor->
index = buf.index;
581 buf_descriptor->
s =
s;
592 pkt->
pts = buf_ts.tv_sec * INT64_C(1000000) + buf_ts.tv_usec;
601 enum v4l2_buf_type
type;
604 for (i = 0; i < s->
buffers; i++) {
605 struct v4l2_buffer buf = {
606 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
608 .memory = V4L2_MEMORY_MMAP
620 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
633 enum v4l2_buf_type
type;
636 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
641 for (i = 0; i < s->
buffers; i++) {
651 struct v4l2_standard standard = { 0 };
652 struct v4l2_streamparm streamparm = { 0 };
653 struct v4l2_fract *tpf;
683 if (
v4l2_ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
690 "This device does not support any standard\n");
696 tpf = &standard.frameperiod;
706 tpf = &streamparm.parm.capture.timeperframe;
712 if (standard.id == s->
std_id) {
714 "Current standard: %s, id: %"PRIx64
", frameperiod: %d/%d\n",
715 standard.name, (uint64_t)standard.id, tpf->numerator, tpf->denominator);
720 tpf = &streamparm.parm.capture.timeperframe;
723 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
724 if (
v4l2_ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) < 0) {
727 }
else if (framerate_q.
num && framerate_q.
den) {
728 if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
729 tpf = &streamparm.parm.capture.timeperframe;
732 framerate_q.
den, framerate_q.
num);
733 tpf->numerator = framerate_q.
den;
734 tpf->denominator = framerate_q.
num;
736 if (
v4l2_ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) < 0) {
743 if (framerate_q.
num != tpf->denominator ||
744 framerate_q.
den != tpf->numerator) {
746 "The driver changed the time per frame from " 748 framerate_q.
den, framerate_q.
num,
749 tpf->numerator, tpf->denominator);
753 "The driver does not permit changing the time per frame\n");
756 if (tpf->denominator > 0 && tpf->numerator > 0) {
770 uint32_t *desired_format,
777 if (*desired_format) {
778 ret =
device_init(ctx, width, height, *desired_format);
786 if (!*desired_format) {
795 ret =
device_init(ctx, width, height, *desired_format);
798 else if (ret !=
AVERROR(EINVAL))
804 if (*desired_format == 0) {
806 "codec '%s' (id %d), pixel format '%s' (id %d)\n",
831 uint32_t desired_format;
834 struct v4l2_input input = { 0 };
844 v4l2_log_file = fopen(
"/dev/null",
"w");
876 av_log(ctx,
AV_LOG_DEBUG,
"Current input_channel: %d, input_name: %s, input_std: %"PRIx64
"\n",
877 s->
channel, input.name, (uint64_t)input.std);
911 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
914 "Querying the device for the current frame size\n");
922 s->
width = fmt.fmt.pix.width;
923 s->
height = fmt.fmt.pix.height;
925 "Setting frame size to %dx%d\n", s->
width, s->
height);
966 if (desired_format == V4L2_PIX_FMT_YVU420)
968 else if (desired_format == V4L2_PIX_FMT_YVU410)
984 #if FF_API_CODED_FRAME && FF_API_LAVF_AVCTX 996 #if FF_API_CODED_FRAME && FF_API_LAVF_AVCTX 1024 return !strncmp(name,
"video", 5) ||
1025 !strncmp(name,
"radio", 5) ||
1026 !strncmp(name,
"vbi", 3) ||
1027 !strncmp(name,
"v4l-subdev", 10);
1034 struct dirent *entry;
1036 struct v4l2_capability cap;
1042 dir = opendir(
"/dev");
1048 while ((entry = readdir(dir))) {
1049 char device_name[256];
1054 snprintf(device_name,
sizeof(device_name),
"/dev/%s", entry->d_name);
1099 #define OFFSET(x) offsetof(struct video_data, x) 1100 #define DEC AV_OPT_FLAG_DECODING_PARAM 1136 .
name =
"video4linux2,v4l2",
Structure describes basic parameters of the device.
int(* munmap_f)(void *_start, size_t length)
static enum AVPixelFormat pix_fmt
This structure describes decoded (raw) audio or video data.
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
#define atomic_store(object, desired)
char * device_description
human friendly name
static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pixelformat)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
char * pixel_format
Set by a private option.
char * device_name
device name, format depends on device
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
const char * av_default_item_name(void *ptr)
Return the context name.
static int enqueue_buffer(struct video_data *s, struct v4l2_buffer *buf)
static int device_try_init(AVFormatContext *ctx, enum AVPixelFormat pix_fmt, int *width, int *height, uint32_t *desired_format, enum AVCodecID *codec_id)
static void list_formats(AVFormatContext *ctx, int type)
uint32_t ff_fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
double ff_timefilter_eval(TimeFilter *self, double delta)
Evaluate the filter at a specified time.
const struct fmt_map ff_fmt_conversion_table[]
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define V4L_TS_ABS
Autodetect the kind of timestamps returned by the kernel and convert to absolute (wall clock) timesta...
Opaque type representing a time filter state.
static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
static int init_convert_timestamp(AVFormatContext *ctx, int64_t ts)
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, or 0 if no associated fourCC code can be found.
int(* ioctl_f)(int fd, unsigned long int request,...)
enum AVStreamParseType need_parsing
static int v4l2_read_header(AVFormatContext *ctx)
static int mmap_start(AVFormatContext *ctx)
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
A list of all streams in the file.
static const int desired_video_buffers
int flags
Flags modifying the (de)muxer behaviour.
static int v4l2_is_v4l_dev(const char *name)
static double av_q2d(AVRational a)
Convert an AVRational to a double.
#define AV_LOG_VERBOSE
Detailed information.
int interlaced_frame
The content of the picture is interlaced.
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
#define V4L_TS_DEFAULT
Return timestamps to the user exactly as returned by the kernel.
static int v4l2_set_parameters(AVFormatContext *ctx)
enum AVCodecID video_codec_id
Forced video codec_id.
int(* open_f)(const char *file, int oflag,...)
static int first_field(const struct video_data *s)
#define V4L_TS_CONVERT_READY
Once the kind of timestamps returned by the kernel have been detected, the value of the timefilter (N...
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
static int v4l2_get_device_list(AVFormatContext *ctx, AVDeviceInfoList *device_list)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
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...
#define i(width, name, range_min, range_max)
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int device_open(AVFormatContext *ctx, const char *device_path)
#define atomic_load(object)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
char * url
input or output URL.
static const AVOption options[]
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
enum AVMediaType codec_type
General type of the encoded data.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
AVDeviceInfo ** devices
list of autodetected devices
static const uint8_t offset[127][2]
AVRational avg_frame_rate
Average framerate.
#define V4L_TS_MONO2ABS
Assume kernel timestamps are from the monotonic clock and convert to absolute timestamps.
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...
#define AV_TIME_BASE
Internal time base represented as integer.
static void list_standards(AVFormatContext *ctx)
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
static int convert_timestamp(AVFormatContext *ctx, int64_t *ts)
static void mmap_close(struct video_data *s)
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
int list_format
Set by a private option.
int64_t av_gettime(void)
Get the current time in microseconds.
#define SET_WRAPPERS(prefix)
#define atomic_fetch_add(object, operand)
#define AV_LOG_INFO
Standard information.
TimeFilter * ff_timefilter_new(double time_base, double period, double bandwidth)
Create a new Delay Locked Loop time filter.
char * av_strdup(const char *s)
Duplicate a string.
atomic_int buffers_queued
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Describe the class of an AVClass context structure.
static int v4l2_read_close(AVFormatContext *ctx)
Rational number (pair of numerator and denominator).
static int v4l2_read_probe(const AVProbeData *p)
const char * name
Name of the codec described by this descriptor.
offset must point to two consecutive integers
This structure contains the data a format has to probe a file.
int list_standard
Set by a private option.
static int mmap_init(AVFormatContext *ctx)
This struct describes the properties of a single codec described by an AVCodecID. ...
static const AVClass v4l2_class
static int v4l2_read_packet(AVFormatContext *ctx, AVPacket *pkt)
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
#define flags(name, subs,...)
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
double ff_timefilter_update(TimeFilter *self, double system_time, double period)
Update the filter.
#define FF_DISABLE_DEPRECATION_WARNINGS
char * framerate
Set by a private option.
channel
Use these values when setting the channel map with ebur128_set_channel().
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
enum AVCodecID ff_fmt_v4l2codec(uint32_t v4l2_fmt)
#define FF_ENABLE_DEPRECATION_WARNINGS
int top_field_first
If the content is interlaced, is top field displayed first.
void * priv_data
Format private data.
enum AVPixelFormat ff_fmt_v4l2ff(uint32_t v4l2_fmt, enum AVCodecID codec_id)
ssize_t(* read_f)(int fd, void *buffer, size_t n)
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
AVInputFormat ff_v4l2_demuxer
static void mmap_release_buffer(void *opaque, uint8_t *data)
AVCodecParameters * codecpar
Codec parameters associated with this stream.
#define av_malloc_array(a, b)
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
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.
#define MKTAG(a, b, c, d)
AVRational r_frame_rate
Real base framerate of the stream.
int nb_devices
number of autodetected devices
AVPixelFormat
Pixel format.
This structure stores compressed data.
const AVCodecDescriptor * avcodec_descriptor_get_by_name(const char *name)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...