FFmpeg
Data Structures | Macros | Functions | Variables
v4l2.c File Reference
#include <stdatomic.h>
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/imgutils.h"
#include "libavutil/mem.h"
#include "libavutil/parseutils.h"
#include "libavutil/pixdesc.h"
#include "libavutil/time.h"
#include "libavcodec/avcodec.h"
#include "libavcodec/codec_desc.h"
#include "libavformat/demux.h"
#include "libavformat/internal.h"
#include "avdevice.h"
#include "timefilter.h"
#include "v4l2-common.h"
#include <dirent.h>

Go to the source code of this file.

Data Structures

struct  video_data
 
struct  buff_data
 

Macros

#define V4L_ALLFORMATS   3
 
#define V4L_RAWFORMATS   1
 
#define V4L_COMPFORMATS   2
 
#define V4L_TS_DEFAULT   0
 Return timestamps to the user exactly as returned by the kernel. More...
 
#define V4L_TS_ABS   1
 Autodetect the kind of timestamps returned by the kernel and convert to absolute (wall clock) timestamps. More...
 
#define V4L_TS_MONO2ABS   2
 Assume kernel timestamps are from the monotonic clock and convert to absolute timestamps. More...
 
#define V4L_TS_CONVERT_READY   V4L_TS_DEFAULT
 Once the kind of timestamps returned by the kernel have been detected, the value of the timefilter (NULL or not) determines whether a conversion takes place. More...
 
#define SET_WRAPPERS(prefix)
 
#define v4l2_open   s->open_f
 
#define v4l2_close   s->close_f
 
#define v4l2_dup   s->dup_f
 
#define v4l2_ioctl   s->ioctl_f
 
#define v4l2_read   s->read_f
 
#define v4l2_mmap   s->mmap_f
 
#define v4l2_munmap   s->munmap_f
 
#define OFFSET(x)   offsetof(struct video_data, x)
 
#define DEC   AV_OPT_FLAG_DECODING_PARAM
 

Functions

static int device_open (AVFormatContext *ctx, const char *device_path)
 
static int device_init (AVFormatContext *ctx, int *width, int *height, uint32_t pixelformat)
 
static int first_field (const struct video_data *s)
 
static void list_formats (AVFormatContext *ctx, int type)
 
static void list_standards (AVFormatContext *ctx)
 
static int mmap_init (AVFormatContext *ctx)
 
static int enqueue_buffer (struct video_data *s, struct v4l2_buffer *buf)
 
static void mmap_release_buffer (void *opaque, uint8_t *data)
 
static int init_convert_timestamp (AVFormatContext *ctx, int64_t ts)
 
static int convert_timestamp (AVFormatContext *ctx, int64_t *ts)
 
static int mmap_read_frame (AVFormatContext *ctx, AVPacket *pkt)
 
static int mmap_start (AVFormatContext *ctx)
 
static void mmap_close (struct video_data *s)
 
static int v4l2_set_parameters (AVFormatContext *ctx)
 
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 v4l2_read_probe (const AVProbeData *p)
 
static int v4l2_read_header (AVFormatContext *ctx)
 
static int v4l2_read_packet (AVFormatContext *ctx, AVPacket *pkt)
 
static int v4l2_read_close (AVFormatContext *ctx)
 
static int v4l2_is_v4l_dev (const char *name)
 
static int v4l2_get_device_list (AVFormatContext *ctx, AVDeviceInfoList *device_list)
 

Variables

static const int desired_video_buffers = 256
 
static const AVOption options []
 
static const AVClass v4l2_class
 
const FFInputFormat ff_v4l2_demuxer
 

Detailed Description

Video4Linux2 grab interface

Part of this file is based on the V4L2 video capture example (http://linuxtv.org/downloads/v4l-dvb-apis/capture-example.html)

Thanks to Michael Niedermayer for providing the mapping between V4L2_PIX_FMT_* and AV_PIX_FMT_*

Definition in file v4l2.c.

Macro Definition Documentation

◆ V4L_ALLFORMATS

#define V4L_ALLFORMATS   3

Definition at line 57 of file v4l2.c.

◆ V4L_RAWFORMATS

#define V4L_RAWFORMATS   1

Definition at line 58 of file v4l2.c.

◆ V4L_COMPFORMATS

#define V4L_COMPFORMATS   2

Definition at line 59 of file v4l2.c.

◆ V4L_TS_DEFAULT

#define V4L_TS_DEFAULT   0

Return timestamps to the user exactly as returned by the kernel.

Definition at line 64 of file v4l2.c.

◆ V4L_TS_ABS

#define V4L_TS_ABS   1

Autodetect the kind of timestamps returned by the kernel and convert to absolute (wall clock) timestamps.

Definition at line 69 of file v4l2.c.

◆ V4L_TS_MONO2ABS

#define V4L_TS_MONO2ABS   2

Assume kernel timestamps are from the monotonic clock and convert to absolute timestamps.

Definition at line 74 of file v4l2.c.

◆ V4L_TS_CONVERT_READY

#define V4L_TS_CONVERT_READY   V4L_TS_DEFAULT

Once the kind of timestamps returned by the kernel have been detected, the value of the timefilter (NULL or not) determines whether a conversion takes place.

Definition at line 81 of file v4l2.c.

◆ SET_WRAPPERS

#define SET_WRAPPERS (   prefix)
Value:
do { \
s->open_f = prefix ## open; \
s->close_f = prefix ## close; \
s->dup_f = prefix ## dup; \
s->ioctl_f = prefix ## ioctl; \
s->read_f = prefix ## read; \
s->mmap_f = prefix ## mmap; \
s->munmap_f = prefix ## munmap; \
} while (0)

◆ v4l2_open

#define v4l2_open   s->open_f

◆ v4l2_close

#define v4l2_close   s->close_f

◆ v4l2_dup

#define v4l2_dup   s->dup_f

◆ v4l2_ioctl

#define v4l2_ioctl   s->ioctl_f

◆ v4l2_read

#define v4l2_read   s->read_f

◆ v4l2_mmap

#define v4l2_mmap   s->mmap_f

◆ v4l2_munmap

#define v4l2_munmap   s->munmap_f

◆ OFFSET

#define OFFSET (   x)    offsetof(struct video_data, x)

Definition at line 1139 of file v4l2.c.

◆ DEC

#define DEC   AV_OPT_FLAG_DECODING_PARAM

Definition at line 1140 of file v4l2.c.

Function Documentation

◆ device_open()

static int device_open ( AVFormatContext ctx,
const char *  device_path 
)
static

Definition at line 129 of file v4l2.c.

Referenced by v4l2_get_device_list(), and v4l2_read_header().

◆ device_init()

static int device_init ( AVFormatContext ctx,
int *  width,
int *  height,
uint32_t  pixelformat 
)
static

Definition at line 214 of file v4l2.c.

Referenced by device_try_init().

◆ first_field()

static int first_field ( const struct video_data s)
static

◆ list_formats()

static void list_formats ( AVFormatContext ctx,
int  type 
)
static

Definition at line 297 of file v4l2.c.

Referenced by v4l2_read_header().

◆ list_standards()

static void list_standards ( AVFormatContext ctx)
static

Definition at line 335 of file v4l2.c.

Referenced by v4l2_read_header().

◆ mmap_init()

static int mmap_init ( AVFormatContext ctx)
static

Definition at line 359 of file v4l2.c.

Referenced by v4l2_read_header().

◆ enqueue_buffer()

static int enqueue_buffer ( struct video_data s,
struct v4l2_buffer *  buf 
)
static

Definition at line 441 of file v4l2.c.

Referenced by mmap_read_frame(), and mmap_release_buffer().

◆ mmap_release_buffer()

static void mmap_release_buffer ( void *  opaque,
uint8_t *  data 
)
static

Definition at line 455 of file v4l2.c.

Referenced by mmap_read_frame().

◆ init_convert_timestamp()

static int init_convert_timestamp ( AVFormatContext ctx,
int64_t  ts 
)
static

Definition at line 479 of file v4l2.c.

Referenced by convert_timestamp().

◆ convert_timestamp()

static int convert_timestamp ( AVFormatContext ctx,
int64_t ts 
)
static

Definition at line 512 of file v4l2.c.

Referenced by mmap_read_frame().

◆ mmap_read_frame()

static int mmap_read_frame ( AVFormatContext ctx,
AVPacket pkt 
)
static

Definition at line 533 of file v4l2.c.

Referenced by v4l2_read_packet().

◆ mmap_start()

static int mmap_start ( AVFormatContext ctx)
static

Definition at line 645 of file v4l2.c.

Referenced by v4l2_read_header().

◆ mmap_close()

static void mmap_close ( struct video_data s)
static

Definition at line 681 of file v4l2.c.

Referenced by v4l2_read_close().

◆ v4l2_set_parameters()

static int v4l2_set_parameters ( AVFormatContext ctx)
static

Definition at line 698 of file v4l2.c.

Referenced by v4l2_read_header().

◆ device_try_init()

static int device_try_init ( AVFormatContext ctx,
enum AVPixelFormat  pix_fmt,
int *  width,
int *  height,
uint32_t *  desired_format,
enum AVCodecID codec_id 
)
static

Definition at line 816 of file v4l2.c.

Referenced by v4l2_read_header().

◆ v4l2_read_probe()

static int v4l2_read_probe ( const AVProbeData p)
static

Definition at line 869 of file v4l2.c.

◆ v4l2_read_header()

static int v4l2_read_header ( AVFormatContext ctx)
static

Definition at line 876 of file v4l2.c.

◆ v4l2_read_packet()

static int v4l2_read_packet ( AVFormatContext ctx,
AVPacket pkt 
)
static

Definition at line 1032 of file v4l2.c.

◆ v4l2_read_close()

static int v4l2_read_close ( AVFormatContext ctx)
static

Definition at line 1043 of file v4l2.c.

◆ v4l2_is_v4l_dev()

static int v4l2_is_v4l_dev ( const char *  name)
static

Definition at line 1058 of file v4l2.c.

Referenced by v4l2_get_device_list().

◆ v4l2_get_device_list()

static int v4l2_get_device_list ( AVFormatContext ctx,
AVDeviceInfoList device_list 
)
static

Definition at line 1066 of file v4l2.c.

Variable Documentation

◆ desired_video_buffers

const int desired_video_buffers = 256
static

Definition at line 55 of file v4l2.c.

Referenced by mmap_init().

◆ options

const AVOption options[]
static
Initial value:
= {
{ "standard", "set TV standard, used only by analog frame grabber", OFFSET(standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
{ "channel", "set TV channel, used only by frame grabber", OFFSET(channel), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX, DEC },
{ "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
{ "pixel_format", "set preferred pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
{ "input_format", "set preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
{ "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
{ "list_formats", "list available formats and exit", OFFSET(list_format), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC, .unit = "list_formats" },
{ "all", "show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_ALLFORMATS }, 0, INT_MAX, DEC, .unit = "list_formats" },
{ "raw", "show only non-compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_RAWFORMATS }, 0, INT_MAX, DEC, .unit = "list_formats" },
{ "compressed", "show only compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_COMPFORMATS }, 0, INT_MAX, DEC, .unit = "list_formats" },
{ "list_standards", "list supported standards and exit", OFFSET(list_standard), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC, .unit = "list_standards" },
{ "all", "show all supported standards", OFFSET(list_standard), AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, DEC, .unit = "list_standards" },
{ "timestamps", "set type of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, .unit = "timestamps" },
{ "ts", "set type of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, .unit = "timestamps" },
{ "default", "use timestamps from the kernel", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_DEFAULT }, 0, 2, DEC, .unit = "timestamps" },
{ "abs", "use absolute timestamps (wall clock)", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_ABS }, 0, 2, DEC, .unit = "timestamps" },
{ "mono2abs", "force conversion from monotonic to absolute timestamps", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_MONO2ABS }, 0, 2, DEC, .unit = "timestamps" },
{ "use_libv4l2", "use libv4l2 (v4l-utils) conversion functions", OFFSET(use_libv4l2), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
{ NULL },
}

Definition at line 1142 of file v4l2.c.

◆ v4l2_class

const AVClass v4l2_class
static
Initial value:
= {
.class_name = "V4L2 indev",
.item_name = av_default_item_name,
.option = options,
}

Definition at line 1167 of file v4l2.c.

◆ ff_v4l2_demuxer

const FFInputFormat ff_v4l2_demuxer
Initial value:
= {
.p.name = "video4linux2,v4l2",
.p.long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
.p.flags = AVFMT_NOFILE,
.p.priv_class = &v4l2_class,
.priv_data_size = sizeof(struct video_data),
.read_header = v4l2_read_header,
.read_packet = v4l2_read_packet,
.read_close = v4l2_read_close,
.get_device_list = v4l2_get_device_list,
}

Definition at line 1175 of file v4l2.c.

v4l2_class
static const AVClass v4l2_class
Definition: v4l2.c:1167
framerate
float framerate
Definition: av1_levels.c:29
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
NULL
#define NULL
Definition: coverity.c:32
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition: opt.h:303
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
V4L_RAWFORMATS
#define V4L_RAWFORMATS
Definition: v4l2.c:58
AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
Definition: log.h:41
v4l2_read_close
static int v4l2_read_close(AVFormatContext *ctx)
Definition: v4l2.c:1043
v4l2_read_probe
static int v4l2_read_probe(const AVProbeData *p)
Definition: v4l2.c:869
V4L_COMPFORMATS
#define V4L_COMPFORMATS
Definition: v4l2.c:59
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:468
DEC
#define DEC
Definition: v4l2.c:1140
dc1394_data::pixel_format
char * pixel_format
Set by a private option.
Definition: libdc1394.c:45
options
static const AVOption options[]
Definition: v4l2.c:1142
OFFSET
#define OFFSET(x)
Definition: v4l2.c:1139
v4l2_get_device_list
static int v4l2_get_device_list(AVFormatContext *ctx, AVDeviceInfoList *device_list)
Definition: v4l2.c:1066
V4L_TS_MONO2ABS
#define V4L_TS_MONO2ABS
Assume kernel timestamps are from the monotonic clock and convert to absolute timestamps.
Definition: v4l2.c:74
video_data
Definition: v4l2.c:83
V4L_ALLFORMATS
#define V4L_ALLFORMATS
Definition: v4l2.c:57
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
v4l2_read_header
static int v4l2_read_header(AVFormatContext *ctx)
Definition: v4l2.c:876
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
v4l2_read_packet
static int v4l2_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: v4l2.c:1032
V4L_TS_DEFAULT
#define V4L_TS_DEFAULT
Return timestamps to the user exactly as returned by the kernel.
Definition: v4l2.c:64
AV_OPT_TYPE_STRING
@ 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...
Definition: opt.h:276
width
#define width
Definition: dsp.h:85
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
V4L_TS_ABS
#define V4L_TS_ABS
Autodetect the kind of timestamps returned by the kernel and convert to absolute (wall clock) timesta...
Definition: v4l2.c:69
channel
channel
Definition: ebur128.h:39
read
static uint32_t BS_FUNC() read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
Definition: bitstream_template.h:231