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)
432 clock_gettime(CLOCK_MONOTONIC, &tv);
433 return (int64_t)tv.tv_sec * 1000000 + tv.tv_nsec / 1000;
444 ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE) {
449 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
450 now = av_gettime_monotonic();
452 (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 *
AV_TIME_BASE)) {
477 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
480 int64_t nowm = av_gettime_monotonic();
492 struct v4l2_buffer buf = {
493 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
494 .memory = V4L2_MEMORY_MMAP
499 while ((res =
v4l2_ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
501 if (errno == EAGAIN) {
526 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
554 pkt->
size = buf.bytesused;
555 #if FF_API_DESTRUCT_PACKET
557 pkt->
destruct = dummy_release_buffer;
562 if (buf_descriptor == NULL) {
572 buf_descriptor->index = buf.index;
573 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
612 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
624 enum v4l2_buf_type
type;
627 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
632 for (i = 0; i < s->
buffers; i++) {
642 struct v4l2_standard standard = { 0 };
643 struct v4l2_streamparm streamparm = { 0 };
644 struct v4l2_fract *tpf;
674 if (
v4l2_ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
681 "This device does not support any standard\n");
687 tpf = &standard.frameperiod;
693 tpf = &streamparm.parm.capture.timeperframe;
699 if (standard.id == s->
std_id) {
701 "Current standard: %s, id: %"PRIx64
", frameperiod: %d/%d\n",
702 standard.name, (uint64_t)standard.id, tpf->numerator, tpf->denominator);
707 tpf = &streamparm.parm.capture.timeperframe;
710 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
711 if (
v4l2_ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) < 0) {
717 if (framerate_q.
num && framerate_q.
den) {
718 if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
719 tpf = &streamparm.parm.capture.timeperframe;
722 framerate_q.
den, framerate_q.
num);
723 tpf->numerator = framerate_q.
den;
724 tpf->denominator = framerate_q.
num;
726 if (
v4l2_ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) < 0) {
732 if (framerate_q.
num != tpf->denominator ||
733 framerate_q.
den != tpf->numerator) {
735 "The driver changed the time per frame from "
737 framerate_q.
den, framerate_q.
num,
738 tpf->numerator, tpf->denominator);
742 "The driver does not allow to change time per frame\n");
756 uint32_t *desired_format,
763 if (*desired_format) {
764 ret =
device_init(s1, width, height, *desired_format);
772 if (!*desired_format) {
781 ret =
device_init(s1, width, height, *desired_format);
784 else if (ret !=
AVERROR(EINVAL))
790 if (*desired_format == 0) {
792 "codec '%s' (id %d), pixel format '%s' (id %d)\n",
809 uint32_t desired_format;
812 struct v4l2_input input = { 0 };
822 v4l2_log_file = fopen(
"/dev/null",
"w");
854 av_log(s1,
AV_LOG_DEBUG,
"Current input_channel: %d, input_name: %s, input_std: %"PRIx64
"\n",
855 s->
channel, input.name, (uint64_t)input.std);
886 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
889 "Querying the device for the current frame size\n");
896 s->
width = fmt.fmt.pix.width;
897 s->
height = fmt.fmt.pix.height;
899 "Setting frame size to %dx%d\n", s->
width, s->
height);
943 if (desired_format == V4L2_PIX_FMT_YVU420)
945 else if (desired_format == V4L2_PIX_FMT_YVU410)
987 #define OFFSET(x) offsetof(struct video_data, x)
988 #define DEC AV_OPT_FLAG_DECODING_PARAM
1023 .
name =
"video4linux2,v4l2",
int(* open_f)(const char *file, int oflag,...)
#define avpriv_atomic_int_add_and_fetch
void av_free_packet(AVPacket *pkt)
Free a packet.
This structure describes decoded (raw) audio or video data.
struct fmt_map avpriv_fmt_conversion_table[]
ssize_t(* read_f)(int fd, void *buffer, size_t n)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
char * pixel_format
Set by a private option.
AVCodecContext * codec
Codec context associated with this stream.
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.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
double ff_timefilter_eval(TimeFilter *self, double delta)
Evaluate the filter at a specified time.
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
volatile int buffers_queued
#define V4L_TS_ABS
Autodetect the kind of timestamps returned by the kernel and convert to absolute (wall clock) timesta...
const char * av_default_item_name(void *ctx)
Return the context name.
Opaque type representing a time filter state.
static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static int init_convert_timestamp(AVFormatContext *ctx, int64_t ts)
attribute_deprecated void(* destruct)(struct AVPacket *)
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.
static int first_field(const struct video_data *s, int fd)
static const uint8_t offset[511][2]
enum AVStreamParseType need_parsing
AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
static int mmap_start(AVFormatContext *ctx)
const char * name
Name of the codec implementation.
static void list_formats(AVFormatContext *ctx, int fd, int type)
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.
static const int desired_video_buffers
static double av_q2d(AVRational a)
Convert rational to double.
#define AV_LOG_VERBOSE
Detailed information.
enum AVPixelFormat pix_fmt
int interlaced_frame
The content of the picture is interlaced.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
#define V4L_TS_DEFAULT
Return timestamps to the user exactly as returned by the kernel.
const OptionDef options[]
static int device_open(AVFormatContext *ctx)
enum AVCodecID video_codec_id
Forced video codec_id.
#define V4L_TS_CONVERT_READY
Once the kind of timestamps returned by the kernel have been detected, the value of the timefilter (N...
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
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.
#define av_err2str(errnum)
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
enum AVCodecID avpriv_fmt_v4l2codec(uint32_t v4l2_fmt)
void * priv_data
Format private data.
static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
char filename[1024]
input or output filename
#define avpriv_atomic_int_get
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
AVRational avg_frame_rate
Average framerate.
AVPixelFormat
Pixel format.
int av_parse_video_rate(AVRational *rate, const char *str)
Parse str and store the detected values in *rate.
#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...
int64_t av_gettime(void)
Get the current time in microseconds.
int bit_rate
the average bitrate
#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.
int width
picture width / height.
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static int convert_timestamp(AVFormatContext *ctx, int64_t *ts)
static void mmap_close(struct video_data *s)
static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
static int device_try_init(AVFormatContext *s1, enum AVPixelFormat pix_fmt, int *width, int *height, uint32_t *desired_format, enum AVCodecID *codec_id)
int list_format
Set by a private option.
#define SET_WRAPPERS(prefix)
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
#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.
enum AVPixelFormat avpriv_fmt_v4l2ff(uint32_t v4l2_fmt, enum AVCodecID codec_id)
enum AVMediaType codec_type
uint32_t avpriv_fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
BYTE int const BYTE int int int height
Describe the class of an AVClass context structure.
#define MKTAG(a, b, c, d)
rational number numerator/denominator
AVFrame * coded_frame
the picture in the bitstream
offset must point to two consecutive integers
int list_standard
Set by a private option.
static int mmap_init(AVFormatContext *ctx)
static const AVClass v4l2_class
double ff_timefilter_update(TimeFilter *self, double system_time, double period)
Update the filter.
#define FF_DISABLE_DEPRECATION_WARNINGS
static int v4l2_set_parameters(AVFormatContext *s1)
char * framerate
Set by a private option.
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
#define AVERROR_INVALIDDATA
#define FF_ENABLE_DEPRECATION_WARNINGS
int top_field_first
If the content is interlaced, is top field displayed first.
static int v4l2_read_header(AVFormatContext *s1)
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
AVInputFormat ff_v4l2_demuxer
static int v4l2_read_close(AVFormatContext *s1)
static void mmap_release_buffer(void *opaque, uint8_t *data)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int(* munmap_f)(void *_start, size_t length)
AVRational r_frame_rate
Real base framerate of the stream.
This structure stores compressed data.
int(* ioctl_f)(int fd, unsigned long int request,...)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...