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>
55 #define V4L_ALLFORMATS 3
56 #define V4L_RAWFORMATS 1
57 #define V4L_COMPFORMATS 2
113 struct v4l2_capability cap;
132 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
142 fd, cap.capabilities);
144 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
151 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
153 "The device does not support the streaming I/O method.\n");
171 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
172 struct v4l2_pix_format *pix = &fmt.fmt.pix;
179 pix->field = V4L2_FIELD_ANY;
181 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
183 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
185 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
186 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
187 *width = fmt.fmt.pix.width;
188 *height = fmt.fmt.pix.height;
191 if (pix_fmt != fmt.fmt.pix.pixelformat) {
193 "The V4L2 driver changed the pixel format "
194 "from 0x%08X to 0x%08X\n",
195 pix_fmt, fmt.fmt.pix.pixelformat);
199 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
212 res = ioctl(fd, VIDIOC_G_STD, &std);
216 if (std & V4L2_STD_NTSC) {
229 fmt_conversion_table[i].codec_id == codec_id) &&
232 return fmt_conversion_table[i].
v4l2_fmt;
244 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
245 fmt_conversion_table[i].codec_id == codec_id) {
246 return fmt_conversion_table[i].
ff_fmt;
258 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
259 return fmt_conversion_table[i].
codec_id;
266 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
267 static void list_framesizes(
AVFormatContext *ctx,
int fd, uint32_t pixelformat)
269 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
271 while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
273 case V4L2_FRMSIZE_TYPE_DISCRETE:
275 vfse.discrete.width, vfse.discrete.height);
277 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
278 case V4L2_FRMSIZE_TYPE_STEPWISE:
280 vfse.stepwise.min_width,
281 vfse.stepwise.max_width,
282 vfse.stepwise.step_width,
283 vfse.stepwise.min_height,
284 vfse.stepwise.max_height,
285 vfse.stepwise.step_height);
294 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
296 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
302 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
306 fmt_name ? fmt_name :
"Unsupported",
308 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
312 codec ? codec->
name :
"Unsupported",
318 #ifdef V4L2_FMT_FLAG_EMULATED
319 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
324 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
325 list_framesizes(ctx, fd, vfd.pixelformat);
335 struct v4l2_requestbuffers req = {
336 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
338 .memory = V4L2_MEMORY_MMAP
341 res = ioctl(s->
fd, VIDIOC_REQBUFS, &req);
343 if (errno == EINVAL) {
372 for (i = 0; i < req.count; i++) {
373 struct v4l2_buffer buf = {
374 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
376 .memory = V4L2_MEMORY_MMAP
379 res = ioctl(s->
fd, VIDIOC_QUERYBUF, &buf);
389 "Buffer len [%d] = %d != %d\n",
395 PROT_READ | PROT_WRITE, MAP_SHARED,
396 s->
fd, buf.m.offset);
410 struct v4l2_buffer buf = { 0 };
417 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
418 buf.memory = V4L2_MEMORY_MMAP;
419 buf.index = buf_descriptor->
index;
420 fd = buf_descriptor->
fd;
423 res = ioctl(fd, VIDIOC_QBUF, &buf);
435 struct v4l2_buffer buf = {
436 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
437 .memory = V4L2_MEMORY_MMAP
440 struct pollfd p = { .
fd = s->
fd, .events = POLLIN };
447 if (!(p.revents & (POLLIN | POLLERR | POLLHUP)))
451 while ((res = ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
453 if (errno == EAGAIN) {
463 assert (buf.index < s->
buffers);
466 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
474 pkt->
size = buf.bytesused;
475 pkt->
pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
478 if (buf_descriptor ==
NULL) {
483 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
487 buf_descriptor->
fd = s->
fd;
488 buf_descriptor->
index = buf.index;
489 pkt->
priv = buf_descriptor;
497 enum v4l2_buf_type type;
500 for (i = 0; i < s->
buffers; i++) {
501 struct v4l2_buffer buf = {
502 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
504 .memory = V4L2_MEMORY_MMAP
507 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
516 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
517 res = ioctl(s->
fd, VIDIOC_STREAMON, &type);
530 enum v4l2_buf_type type;
533 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
537 ioctl(s->
fd, VIDIOC_STREAMOFF, &type);
538 for (i = 0; i < s->
buffers; i++) {
548 struct v4l2_input input = { 0 };
549 struct v4l2_standard standard = { 0 };
550 struct v4l2_streamparm streamparm = { 0 };
551 struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
555 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
566 if (ioctl(s->
fd, VIDIOC_ENUMINPUT, &input) < 0) {
573 if (ioctl(s->
fd, VIDIOC_S_INPUT, &input.index) < 0) {
575 "The V4L2 driver ioctl set input(%d) failed\n",
586 if (ioctl(s->
fd, VIDIOC_ENUMSTD, &standard) < 0) {
588 "The V4L2 driver ioctl set standard(%s) failed\n",
599 "The V4L2 driver set standard: %s, id: %"PRIu64
"\n",
600 s->
standard, (uint64_t)standard.id);
601 if (ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
603 "The V4L2 driver ioctl set standard(%s) failed\n",
609 if (framerate_q.
num && framerate_q.
den) {
611 framerate_q.
den, framerate_q.
num);
612 tpf->numerator = framerate_q.
den;
613 tpf->denominator = framerate_q.
num;
615 if (ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) != 0) {
617 "ioctl set time per frame(%d/%d) failed\n",
618 framerate_q.
den, framerate_q.
num);
622 if (framerate_q.
num != tpf->denominator ||
623 framerate_q.
den != tpf->numerator) {
625 "The driver changed the time per frame from "
627 framerate_q.
den, framerate_q.
num,
628 tpf->numerator, tpf->denominator);
631 if (ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) != 0) {
655 if (desired_format == 0 ||
656 device_init(s1, width, height, desired_format) < 0) {
663 desired_format = fmt_conversion_table[i].
v4l2_fmt;
664 if (
device_init(s1, width, height, desired_format) >= 0) {
672 if (desired_format != 0) {
677 return desired_format;
685 uint32_t desired_format;
734 struct v4l2_format fmt;
737 "Querying the device for the current frame size\n");
738 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
739 if (ioctl(s->
fd, VIDIOC_G_FMT, &fmt) < 0) {
746 s->
width = fmt.fmt.pix.width;
747 s->
height = fmt.fmt.pix.height;
749 "Setting frame size to %dx%d\n", s->
width, s->
height);
754 if (desired_format == 0) {
825 #define OFFSET(x) offsetof(struct video_data, x)
826 #define DEC AV_OPT_FLAG_DECODING_PARAM
849 .
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 ...
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.
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
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.
packed RGB 8:8:8, 24bpp, RGBRGB...
AVFrame * coded_frame
the picture in the bitstream
char * pixel_format
Set by a private option.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
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)
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.
static int mmap_start(AVFormatContext *ctx)
#define AV_PIX_FMT_RGB555
static void list_formats(AVFormatContext *ctx, int fd, int type)
static const int desired_video_buffers
static double av_q2d(AVRational a)
Convert rational to double.
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.
#define AV_PIX_FMT_RGB565
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.
AVCodecID
Identify the syntax and semantics of the bitstream.
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. ...
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
static const AVOption options[]
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
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.
static uint32_t fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
AVCodecContext * codec
Codec context associated with this stream.
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
int av_strcasecmp(const char *a, const char *b)
int width
picture width / height.
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
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.
enum AVMediaType codec_type
static void close(AVCodecParserContext *s)
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
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Describe the class of an AVClass context structure.
#define FF_ARRAY_ELEMS(a)
rational number numerator/denominator
static int mmap_init(AVFormatContext *ctx)
static const AVClass v4l2_class
AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
static int v4l2_set_parameters(AVFormatContext *s1)
char * framerate
Set by a private option.
#define AVERROR_INVALIDDATA
static void mmap_release_buffer(AVPacket *pkt)
static enum AVCodecID fmt_v4l2codec(uint32_t v4l2_fmt)
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
int top_field_first
If the content is interlaced, is top field displayed first.
static int64_t video_size
static int v4l2_read_header(AVFormatContext *s1)
void * priv_data
Format private data.
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...
#define AVFMT_FLAG_NONBLOCK
enum AVPixelFormat ff_fmt
AVInputFormat ff_v4l2_demuxer
static int v4l2_read_close(AVFormatContext *s1)
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...