FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Functions | Variables
v4l2.c File Reference

Video4Linux2 grab interface. More...

#include "config.h"
#include "libavformat/internal.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <linux/videodev2.h>
#include "libavutil/avassert.h"
#include "libavutil/imgutils.h"
#include "libavutil/log.h"
#include "libavutil/opt.h"
#include "avdevice.h"
#include "timefilter.h"
#include "libavutil/parseutils.h"
#include "libavutil/pixdesc.h"
#include "libavutil/avstring.h"

Go to the source code of this file.

Data Structures

struct  video_data
 
struct  buff_data
 
struct  fmt_map
 

Macros

#define v4l2_open   open
 
#define v4l2_close   close
 
#define v4l2_dup   dup
 
#define v4l2_ioctl   ioctl
 
#define v4l2_read   read
 
#define v4l2_mmap   mmap
 
#define v4l2_munmap   munmap
 
#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.
 
#define V4L_TS_ABS   1
 Autodetect the kind of timestamps returned by the kernel and convert to absolute (wall clock) timestamps.
 
#define V4L_TS_MONO2ABS   2
 Assume kernel timestamps are from the monotonic clock and convert to absolute timestamps.
 
#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.
 
#define OFFSET(x)   offsetof(struct video_data, x)
 
#define DEC   AV_OPT_FLAG_DECODING_PARAM
 

Functions

static int device_open (AVFormatContext *ctx)
 
static int device_init (AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
 
static int first_field (int fd)
 
static uint32_t fmt_ff2v4l (enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
 
static enum AVPixelFormat fmt_v4l2ff (uint32_t v4l2_fmt, enum AVCodecID codec_id)
 
static enum AVCodecID fmt_v4l2codec (uint32_t v4l2_fmt)
 
static void list_formats (AVFormatContext *ctx, int fd, int type)
 
static int mmap_init (AVFormatContext *ctx)
 
static void mmap_release_buffer (AVPacket *pkt)
 
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 *s1)
 
static uint32_t device_try_init (AVFormatContext *s1, enum AVPixelFormat pix_fmt, int *width, int *height, enum AVCodecID *codec_id)
 
static int v4l2_read_header (AVFormatContext *s1)
 
static int v4l2_read_packet (AVFormatContext *s1, AVPacket *pkt)
 
static int v4l2_read_close (AVFormatContext *s1)
 

Variables

static const int desired_video_buffers = 256
 
static struct fmt_map fmt_conversion_table []
 
static const AVOption options []
 
static const AVClass v4l2_class
 
AVInputFormat ff_v4l2_demuxer
 

Detailed Description

Video4Linux2 grab interface.

Part of this file is based on the V4L2 video capture example (http://v4l2spec.bytesex.org/v4l2spec/capture.c)

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

Definition in file v4l2.c.

Macro Definition Documentation

#define v4l2_open   open

Definition at line 62 of file v4l2.c.

Referenced by device_open().

#define v4l2_close   close

Definition at line 63 of file v4l2.c.

Referenced by device_open(), v4l2_read_close(), and v4l2_read_header().

#define v4l2_dup   dup

Definition at line 64 of file v4l2.c.

#define v4l2_ioctl   ioctl
#define v4l2_read   read

Definition at line 66 of file v4l2.c.

#define v4l2_mmap   mmap

Definition at line 67 of file v4l2.c.

Referenced by mmap_init().

#define v4l2_munmap   munmap

Definition at line 68 of file v4l2.c.

Referenced by mmap_close().

#define V4L_ALLFORMATS   3

Definition at line 73 of file v4l2.c.

#define V4L_RAWFORMATS   1

Definition at line 74 of file v4l2.c.

Referenced by list_formats().

#define V4L_COMPFORMATS   2

Definition at line 75 of file v4l2.c.

Referenced by list_formats().

#define V4L_TS_DEFAULT   0

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

Definition at line 80 of file v4l2.c.

#define V4L_TS_ABS   1

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

Definition at line 85 of file v4l2.c.

Referenced by init_convert_timestamp().

#define V4L_TS_MONO2ABS   2

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

Definition at line 90 of file v4l2.c.

Referenced by init_convert_timestamp().

#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 97 of file v4l2.c.

Referenced by init_convert_timestamp().

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

Definition at line 924 of file v4l2.c.

#define DEC   AV_OPT_FLAG_DECODING_PARAM

Definition at line 925 of file v4l2.c.

Function Documentation

static int device_open ( AVFormatContext ctx)
static

Definition at line 161 of file v4l2.c.

Referenced by v4l2_read_header().

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

Definition at line 216 of file v4l2.c.

Referenced by device_try_init().

static int first_field ( int  fd)
static

Definition at line 257 of file v4l2.c.

Referenced by dnxhd_decode_frame(), dnxhd_encode_picture(), and v4l2_read_header().

static uint32_t fmt_ff2v4l ( enum AVPixelFormat  pix_fmt,
enum AVCodecID  codec_id 
)
static

Definition at line 273 of file v4l2.c.

Referenced by device_try_init().

static enum AVPixelFormat fmt_v4l2ff ( uint32_t  v4l2_fmt,
enum AVCodecID  codec_id 
)
static

Definition at line 289 of file v4l2.c.

Referenced by list_formats(), and v4l2_read_header().

static enum AVCodecID fmt_v4l2codec ( uint32_t  v4l2_fmt)
static

Definition at line 303 of file v4l2.c.

Referenced by device_try_init(), and list_formats().

static void list_formats ( AVFormatContext ctx,
int  fd,
int  type 
)
static

Definition at line 342 of file v4l2.c.

Referenced by v4l2_read_header().

static int mmap_init ( AVFormatContext ctx)
static

Definition at line 381 of file v4l2.c.

Referenced by v4l2_read_header().

static void mmap_release_buffer ( AVPacket pkt)
static

Definition at line 451 of file v4l2.c.

Referenced by mmap_read_frame().

static int init_convert_timestamp ( AVFormatContext ctx,
int64_t  ts 
)
static

Definition at line 485 of file v4l2.c.

Referenced by convert_timestamp().

static int convert_timestamp ( AVFormatContext ctx,
int64_t *  ts 
)
static

Definition at line 514 of file v4l2.c.

Referenced by mmap_read_frame().

static int mmap_read_frame ( AVFormatContext ctx,
AVPacket pkt 
)
static

Definition at line 535 of file v4l2.c.

Referenced by v4l2_read_packet().

static int mmap_start ( AVFormatContext ctx)
static

Definition at line 598 of file v4l2.c.

Referenced by v4l2_read_header().

static void mmap_close ( struct video_data s)
static

Definition at line 632 of file v4l2.c.

Referenced by v4l2_read_close().

static int v4l2_set_parameters ( AVFormatContext s1)
static

Definition at line 649 of file v4l2.c.

Referenced by v4l2_read_header().

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

Definition at line 744 of file v4l2.c.

Referenced by v4l2_read_header().

static int v4l2_read_header ( AVFormatContext s1)
static

Definition at line 777 of file v4l2.c.

static int v4l2_read_packet ( AVFormatContext s1,
AVPacket pkt 
)
static

Definition at line 895 of file v4l2.c.

static int v4l2_read_close ( AVFormatContext s1)
static

Definition at line 914 of file v4l2.c.

Variable Documentation

const int desired_video_buffers = 256
static

Definition at line 71 of file v4l2.c.

Referenced by mmap_init().

struct fmt_map fmt_conversion_table[]
static
Initial value:
= {
{ AV_PIX_FMT_YUV420P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV420 },
{ AV_PIX_FMT_YUV420P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YVU420 },
{ AV_PIX_FMT_YUV422P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV422P },
{ AV_PIX_FMT_YUYV422, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUYV },
{ AV_PIX_FMT_UYVY422, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_UYVY },
{ AV_PIX_FMT_YUV411P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV411P },
{ AV_PIX_FMT_YUV410P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV410 },
{ AV_PIX_FMT_RGB555LE,AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB555 },
{ AV_PIX_FMT_RGB555BE,AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB555X },
{ AV_PIX_FMT_RGB565LE,AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB565 },
{ AV_PIX_FMT_RGB565BE,AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB565X },
{ AV_PIX_FMT_BGR24, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR24 },
{ AV_PIX_FMT_RGB24, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB24 },
{ AV_PIX_FMT_BGR0, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR32 },
{ AV_PIX_FMT_0RGB, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB32 },
{ AV_PIX_FMT_GRAY8, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_GREY },
{ AV_PIX_FMT_NV12, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12 },
{ AV_PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_MJPEG },
{ AV_PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_JPEG },
}

Definition at line 132 of file v4l2.c.

const AVOption options[]
static
Initial value:
= {
{ "standard", "TV standard, used only by analog frame grabber", OFFSET(standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
{ "channel", "TV channel, used only by frame grabber", OFFSET(channel), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
{ "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
{ "pixel_format", "Preferred pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
{ "input_format", "Preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
{ "framerate", "", 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, "list_formats" },
{ "all", "Show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_ALLFORMATS }, 0, INT_MAX, DEC, "list_formats" },
{ "raw", "Show only non-compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_RAWFORMATS }, 0, INT_MAX, DEC, "list_formats" },
{ "compressed", "Show only compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_COMPFORMATS }, 0, INT_MAX, DEC, "list_formats" },
{ "timestamps", "Kind of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "timestamps" },
{ "default", "Use timestamps from the kernel", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_DEFAULT }, 0, 2, DEC, "timestamps" },
{ "abs", "Use absolute timestamps (wall clock)", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_ABS }, 0, 2, DEC, "timestamps" },
{ "mono2abs", "Force conversion from monotonic to absolute timestamps", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.i64 = V4L_TS_MONO2ABS }, 0, 2, DEC, "timestamps" },
{ "ts", "Kind of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "timestamps" },
{ NULL },
}

Definition at line 927 of file v4l2.c.

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

Definition at line 946 of file v4l2.c.

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

Definition at line 953 of file v4l2.c.