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>
56 #define V4L_ALLFORMATS 3
57 #define V4L_RAWFORMATS 1
58 #define V4L_COMPFORMATS 2
114 struct v4l2_capability cap;
133 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
143 fd, cap.capabilities);
145 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
152 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
154 "The device does not support the streaming I/O method.\n");
172 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
173 struct v4l2_pix_format *pix = &fmt.fmt.pix;
180 pix->field = V4L2_FIELD_ANY;
182 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
184 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
186 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
187 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
188 *width = fmt.fmt.pix.width;
189 *height = fmt.fmt.pix.height;
192 if (pix_fmt != fmt.fmt.pix.pixelformat) {
194 "The V4L2 driver changed the pixel format "
195 "from 0x%08X to 0x%08X\n",
196 pix_fmt, fmt.fmt.pix.pixelformat);
200 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
213 res = ioctl(fd, VIDIOC_G_STD, &std);
217 if (std & V4L2_STD_NTSC) {
230 fmt_conversion_table[i].codec_id == codec_id) &&
233 return fmt_conversion_table[i].
v4l2_fmt;
245 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
246 fmt_conversion_table[i].codec_id == codec_id) {
247 return fmt_conversion_table[i].
ff_fmt;
259 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
260 return fmt_conversion_table[i].
codec_id;
267 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
268 static void list_framesizes(
AVFormatContext *ctx,
int fd, uint32_t pixelformat)
270 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
272 while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
274 case V4L2_FRMSIZE_TYPE_DISCRETE:
276 vfse.discrete.width, vfse.discrete.height);
278 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
279 case V4L2_FRMSIZE_TYPE_STEPWISE:
281 vfse.stepwise.min_width,
282 vfse.stepwise.max_width,
283 vfse.stepwise.step_width,
284 vfse.stepwise.min_height,
285 vfse.stepwise.max_height,
286 vfse.stepwise.step_height);
295 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
297 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
303 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
307 fmt_name ? fmt_name :
"Unsupported",
309 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
313 codec ? codec->
name :
"Unsupported",
319 #ifdef V4L2_FMT_FLAG_EMULATED
320 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
325 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
326 list_framesizes(ctx, fd, vfd.pixelformat);
336 struct v4l2_requestbuffers req = {
337 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
339 .memory = V4L2_MEMORY_MMAP
342 res = ioctl(s->
fd, VIDIOC_REQBUFS, &req);
344 if (errno == EINVAL) {
373 for (i = 0; i < req.count; i++) {
374 struct v4l2_buffer buf = {
375 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
377 .memory = V4L2_MEMORY_MMAP
380 res = ioctl(s->
fd, VIDIOC_QUERYBUF, &buf);
390 "Buffer len [%d] = %d != %d\n",
396 PROT_READ | PROT_WRITE, MAP_SHARED,
397 s->
fd, buf.m.offset);
411 struct v4l2_buffer buf = { 0 };
418 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
419 buf.memory = V4L2_MEMORY_MMAP;
420 buf.index = buf_descriptor->
index;
421 fd = buf_descriptor->
fd;
424 res = ioctl(fd, VIDIOC_QBUF, &buf);
436 struct v4l2_buffer buf = {
437 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
438 .memory = V4L2_MEMORY_MMAP
441 struct pollfd p = { .
fd = s->
fd, .events = POLLIN };
448 if (!(p.revents & (POLLIN | POLLERR | POLLHUP)))
452 while ((res = ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
454 if (errno == EAGAIN) {
464 assert (buf.index < s->
buffers);
467 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
475 pkt->
size = buf.bytesused;
476 pkt->
pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
479 if (buf_descriptor ==
NULL) {
484 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
488 buf_descriptor->
fd = s->
fd;
489 buf_descriptor->
index = buf.index;
490 pkt->
priv = buf_descriptor;
498 enum v4l2_buf_type type;
501 for (i = 0; i < s->
buffers; i++) {
502 struct v4l2_buffer buf = {
503 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
505 .memory = V4L2_MEMORY_MMAP
508 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
517 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
518 res = ioctl(s->
fd, VIDIOC_STREAMON, &type);
531 enum v4l2_buf_type type;
534 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
538 ioctl(s->
fd, VIDIOC_STREAMOFF, &type);
539 for (i = 0; i < s->
buffers; i++) {
549 struct v4l2_input input = { 0 };
550 struct v4l2_standard standard = { 0 };
551 struct v4l2_streamparm streamparm = { 0 };
552 struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
556 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
567 if (ioctl(s->
fd, VIDIOC_ENUMINPUT, &input) < 0) {
574 if (ioctl(s->
fd, VIDIOC_S_INPUT, &input.index) < 0) {
576 "The V4L2 driver ioctl set input(%d) failed\n",
587 if (ioctl(s->
fd, VIDIOC_ENUMSTD, &standard) < 0) {
589 "The V4L2 driver ioctl set standard(%s) failed\n",
600 "The V4L2 driver set standard: %s, id: %"PRIu64
"\n",
601 s->
standard, (uint64_t)standard.id);
602 if (ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
604 "The V4L2 driver ioctl set standard(%s) failed\n",
610 if (framerate_q.
num && framerate_q.
den) {
612 framerate_q.
den, framerate_q.
num);
613 tpf->numerator = framerate_q.
den;
614 tpf->denominator = framerate_q.
num;
616 if (ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) != 0) {
618 "ioctl set time per frame(%d/%d) failed\n",
619 framerate_q.
den, framerate_q.
num);
623 if (framerate_q.
num != tpf->denominator ||
624 framerate_q.
den != tpf->numerator) {
626 "The driver changed the time per frame from "
628 framerate_q.
den, framerate_q.
num,
629 tpf->numerator, tpf->denominator);
632 if (ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) != 0) {
656 if (desired_format == 0 ||
657 device_init(s1, width, height, desired_format) < 0) {
664 desired_format = fmt_conversion_table[i].
v4l2_fmt;
665 if (
device_init(s1, width, height, desired_format) >= 0) {
673 if (desired_format != 0) {
678 return desired_format;
686 uint32_t desired_format;
735 struct v4l2_format fmt;
738 "Querying the device for the current frame size\n");
739 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
740 if (ioctl(s->
fd, VIDIOC_G_FMT, &fmt) < 0) {
747 s->
width = fmt.fmt.pix.width;
748 s->
height = fmt.fmt.pix.height;
750 "Setting frame size to %dx%d\n", s->
width, s->
height);
755 if (desired_format == 0) {
826 #define OFFSET(x) offsetof(struct video_data, x)
827 #define DEC AV_OPT_FLAG_DECODING_PARAM
850 .
name =
"video4linux2",
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
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.
AVCodec * avcodec_find_encoder(enum CodecID id)
Find a registered encoder with a matching codec ID.
AVFrame * coded_frame
the picture in the bitstream
#define AV_LOG_WARNING
Something somehow does not look correct.
char * pixel_format
Set by a private option.
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
#define FF_ARRAY_ELEMS(a)
enum CodecID video_codec_id
Forced video codec_id.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
static int mmap_start(AVFormatContext *ctx)
static void list_formats(AVFormatContext *ctx, int fd, int type)
static const int desired_video_buffers
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
static double av_q2d(AVRational a)
Convert rational to double.
int interlaced_frame
The content of the picture is interlaced.
int avpicture_get_size(enum PixelFormat 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...
static int device_open(AVFormatContext *ctx)
void(* destruct)(struct AVPacket *)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
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 NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static const AVOption options[]
void av_log(void *avcl, int level, const char *fmt,...)
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
const char * name
Name of the codec implementation.
AVCodecContext * codec
codec context
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...
int bit_rate
the average bitrate
char filename[1024]
input or output filename
static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt, enum CodecID codec_id)
int av_strcasecmp(const char *a, const char *b)
int width
picture width / height.
static void mmap_close(struct video_data *s)
const char * av_get_pix_fmt_name(enum PixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
int list_format
Set by a private option.
char * video_size
String describing video size, set by a private option.
AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
static enum PixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum CodecID codec_id)
enum AVMediaType codec_type
static void close(AVCodecParserContext *s)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Describe the class of an AVClass context structure.
rational number numerator/denominator
static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap)
packed RGB 8:8:8, 24bpp, BGRBGR...
static int mmap_init(AVFormatContext *ctx)
static const AVClass v4l2_class
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
char * framerate
Set by a private option.
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
static void mmap_release_buffer(AVPacket *pkt)
static enum CodecID fmt_v4l2codec(uint32_t v4l2_fmt)
CodecID
Identify the syntax and semantics of the bitstream.
unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat 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.
packed RGB 8:8:8, 24bpp, RGBRGB...
int top_field_first
If the content is interlaced, is top field displayed first.
static int64_t video_size
void * priv_data
Format private data.
AVInputFormat ff_v4l2_demuxer
static int v4l2_read_close(AVFormatContext *s1)
static int first_field(int fd)
static uint32_t device_try_init(AVFormatContext *s1, enum PixelFormat pix_fmt, int *width, int *height, enum CodecID *codec_id)
static struct fmt_map fmt_conversion_table[]
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
enum PixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)