30 #undef __STRICT_ANSI__ //workaround due to broken kernel headers 36 #include <sys/ioctl.h> 40 #if HAVE_SYS_VIDEOIO_H 41 #include <sys/videoio.h> 43 #include <linux/videodev2.h> 58 #define V4L_ALLFORMATS 3 59 #define V4L_RAWFORMATS 1 60 #define V4L_COMPFORMATS 2 114 #ifdef V4L2_PIX_FMT_H264 121 struct v4l2_capability cap;
142 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
153 fd, cap.capabilities);
155 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
162 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
164 "The device does not support the streaming I/O method.\n");
182 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
183 struct v4l2_pix_format *pix = &fmt.fmt.pix;
190 pix->field = V4L2_FIELD_ANY;
192 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
194 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
196 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
197 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
198 *width = fmt.fmt.pix.width;
199 *height = fmt.fmt.pix.height;
202 if (pix_fmt != fmt.fmt.pix.pixelformat) {
204 "The V4L2 driver changed the pixel format " 205 "from 0x%08X to 0x%08X\n",
206 pix_fmt, fmt.fmt.pix.pixelformat);
210 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
223 res = ioctl(fd, VIDIOC_G_STD, &std);
227 if (std & V4L2_STD_NTSC) {
240 fmt_conversion_table[i].codec_id == codec_id) &&
243 return fmt_conversion_table[i].
v4l2_fmt;
255 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
256 fmt_conversion_table[i].codec_id == codec_id) {
257 return fmt_conversion_table[i].
ff_fmt;
269 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
270 return fmt_conversion_table[i].
codec_id;
277 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE 280 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
282 while(!ioctl(
fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
284 case V4L2_FRMSIZE_TYPE_DISCRETE:
286 vfse.discrete.width, vfse.discrete.height);
288 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
289 case V4L2_FRMSIZE_TYPE_STEPWISE:
291 vfse.stepwise.min_width,
292 vfse.stepwise.max_width,
293 vfse.stepwise.step_width,
294 vfse.stepwise.min_height,
295 vfse.stepwise.max_height,
296 vfse.stepwise.step_height);
305 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
307 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
313 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
317 fmt_name ? fmt_name :
"Unsupported",
319 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
323 desc ? desc->
name :
"Unsupported",
329 #ifdef V4L2_FMT_FLAG_EMULATED 330 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
335 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE 336 list_framesizes(ctx, fd, vfd.pixelformat);
346 struct v4l2_requestbuffers req = {
347 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
349 .memory = V4L2_MEMORY_MMAP
352 res = ioctl(s->
fd, VIDIOC_REQBUFS, &req);
384 for (i = 0; i < req.count; i++) {
385 struct v4l2_buffer buf = {
386 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
388 .memory = V4L2_MEMORY_MMAP
391 res = ioctl(s->
fd, VIDIOC_QUERYBUF, &buf);
402 "Buffer len [%d] = %d != %d\n",
408 PROT_READ | PROT_WRITE, MAP_SHARED,
409 s->
fd, buf.m.offset);
426 struct v4l2_buffer buf = { 0 };
428 struct buff_data *buf_descriptor = opaque;
432 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
433 buf.memory = V4L2_MEMORY_MMAP;
434 buf.index = buf_descriptor->
index;
435 fd = buf_descriptor->
fd;
438 res = ioctl(fd, VIDIOC_QBUF, &buf);
450 struct v4l2_buffer buf = {
451 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
452 .memory = V4L2_MEMORY_MMAP
454 struct pollfd p = { .fd = s->
fd, .events = POLLIN };
461 if (!(p.revents & (POLLIN | POLLERR | POLLHUP)))
465 while ((res = ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
468 if (errno == EAGAIN) {
491 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
507 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
519 pkt->
size = buf.bytesused;
522 if (!buf_descriptor) {
527 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
531 buf_descriptor->
fd = s->
fd;
532 buf_descriptor->
index = buf.index;
533 buf_descriptor->
s =
s;
542 pkt->
pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
550 enum v4l2_buf_type type;
554 for (i = 0; i < s->
buffers; i++) {
555 struct v4l2_buffer buf = {
556 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
558 .memory = V4L2_MEMORY_MMAP
561 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
573 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
574 res = ioctl(s->
fd, VIDIOC_STREAMON, &type);
589 enum v4l2_buf_type type;
592 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
596 ioctl(s->
fd, VIDIOC_STREAMOFF, &type);
597 for (i = 0; i < s->
buffers; i++) {
607 struct v4l2_input input = { 0 };
608 struct v4l2_standard standard = { 0 };
609 struct v4l2_streamparm streamparm = { 0 };
610 struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
614 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
625 if (ioctl(s->
fd, VIDIOC_ENUMINPUT, &input) < 0) {
632 if (ioctl(s->
fd, VIDIOC_S_INPUT, &input.index) < 0) {
634 "The V4L2 driver ioctl set input(%d) failed\n",
645 if (ioctl(s->
fd, VIDIOC_ENUMSTD, &standard) < 0) {
647 "The V4L2 driver ioctl set standard(%s) failed\n",
658 "The V4L2 driver set standard: %s, id: %"PRIu64
"\n",
659 s->
standard, (uint64_t)standard.id);
660 if (ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
662 "The V4L2 driver ioctl set standard(%s) failed\n",
668 if (framerate_q.
num && framerate_q.
den) {
670 framerate_q.
den, framerate_q.
num);
671 tpf->numerator = framerate_q.
den;
672 tpf->denominator = framerate_q.
num;
674 if (ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) != 0) {
676 "ioctl set time per frame(%d/%d) failed\n",
677 framerate_q.
den, framerate_q.
num);
681 if (framerate_q.
num != tpf->denominator ||
682 framerate_q.
den != tpf->numerator) {
684 "The driver changed the time per frame from " 686 framerate_q.
den, framerate_q.
num,
687 tpf->numerator, tpf->denominator);
690 if (ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) != 0) {
717 if (desired_format == 0 ||
718 device_init(s1, width, height, desired_format) < 0) {
725 desired_format = fmt_conversion_table[i].
v4l2_fmt;
726 if (
device_init(s1, width, height, desired_format) >= 0) {
734 if (desired_format != 0) {
739 return desired_format;
747 uint32_t desired_format;
792 struct v4l2_format fmt;
795 "Querying the device for the current frame size\n");
796 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
797 if (ioctl(s->
fd, VIDIOC_G_FMT, &fmt) < 0) {
806 s->
width = fmt.fmt.pix.width;
807 s->
height = fmt.fmt.pix.height;
809 "Setting frame size to %dx%d\n", s->
width, s->
height);
814 if (desired_format == 0) {
856 #if FF_API_CODED_FRAME && FF_API_LAVF_AVCTX 868 #if FF_API_CODED_FRAME && FF_API_LAVF_AVCTX 894 #define OFFSET(x) offsetof(struct video_data, x) 895 #define DEC AV_OPT_FLAG_DECODING_PARAM 918 .
name =
"video4linux2",
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define avpriv_atomic_int_add_and_fetch
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
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.
int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
Parse str and put in width_ptr and height_ptr the detected values.
#define AV_LOG_WARNING
Something somehow does not look correct.
packed RGB 8:8:8, 24bpp, RGBRGB...
char * pixel_format
Set by a private option.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define FF_ARRAY_ELEMS(a)
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
volatile int buffers_queued
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 uint32_t device_try_init(AVFormatContext *s1, enum AVPixelFormat pix_fmt, int *width, int *height, enum AVCodecID *codec_id)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
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.
enum AVStreamParseType need_parsing
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 void list_formats(AVFormatContext *ctx, int fd, int type)
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
static const int desired_video_buffers
static double av_q2d(AVRational a)
Convert rational to double.
int flags
Flags modifying the (de)muxer behaviour.
#define AV_LOG_VERBOSE
Detailed information.
int interlaced_frame
The content of the picture is interlaced.
static int device_open(AVFormatContext *ctx)
enum AVCodecID video_codec_id
Forced video codec_id.
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...
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(). ...
static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
#define avpriv_atomic_int_get
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
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.
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
simple assert() macros that are a bit more flexible than ISO C assert().
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.
AVRational avg_frame_rate
Average framerate.
static uint32_t fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
int bit_rate
The average bitrate of the encoded data (in bits per second).
common internal API header
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...
char filename[1024]
input or output filename
int av_strcasecmp(const char *a, const char *b)
static void mmap_close(struct video_data *s)
static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
packed RGB 8:8:8, 24bpp, BGRBGR...
enum AVPixelFormat pix_fmt
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
int list_format
Set by a private option.
static enum AVPixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum AVCodecID codec_id)
char * video_size
String describing video size, set by a private option.
#define AV_LOG_INFO
Standard information.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Describe the class of an AVClass context structure.
rational number numerator/denominator
const char * name
Name of the codec described by this descriptor.
static int mmap_init(AVFormatContext *ctx)
This struct describes the properties of a single codec described by an AVCodecID. ...
static const AVClass v4l2_class
AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FF_DISABLE_DEPRECATION_WARNINGS
static int v4l2_set_parameters(AVFormatContext *s1)
char * framerate
Set by a private option.
#define AV_PIX_FMT_RGB555
static enum AVCodecID fmt_v4l2codec(uint32_t v4l2_fmt)
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
#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)
void * priv_data
Format private data.
enum AVPixelFormat ff_fmt
#define AV_PIX_FMT_RGB565
AVInputFormat ff_v4l2_demuxer
static int v4l2_read_close(AVFormatContext *s1)
static void mmap_release_buffer(void *opaque, uint8_t *data)
AVCodecParameters * codecpar
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.
static int first_field(int fd)
static struct fmt_map fmt_conversion_table[]
AVPixelFormat
Pixel format.
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...