30 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
32 #include "libavformat/avformat.h"
36 #include <sys/ioctl.h>
40 #if HAVE_SYS_VIDEOIO_H
41 #include <sys/videoio.h>
43 #include <linux/videodev2.h>
46 #include "libavutil/avassert.h"
47 #include "libavutil/imgutils.h"
49 #include "libavutil/log.h"
50 #include "libavutil/opt.h"
51 #include "libavutil/parseutils.h"
52 #include "libavutil/pixdesc.h"
53 #include "libavutil/avstring.h"
54 #include "libavutil/mathematics.h"
58 #define V4L_ALLFORMATS 3
59 #define V4L_RAWFORMATS 1
60 #define V4L_COMPFORMATS 2
118 struct v4l2_capability cap;
137 res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
147 fd, cap.capabilities);
149 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
156 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
158 "The device does not support the streaming I/O method.\n");
176 struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
177 struct v4l2_pix_format *pix = &fmt.fmt.pix;
184 pix->field = V4L2_FIELD_ANY;
186 res = ioctl(fd, VIDIOC_S_FMT, &fmt);
188 if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
190 "The V4L2 driver changed the video from %dx%d to %dx%d\n",
191 *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
192 *width = fmt.fmt.pix.width;
193 *height = fmt.fmt.pix.height;
196 if (pix_fmt != fmt.fmt.pix.pixelformat) {
198 "The V4L2 driver changed the pixel format "
199 "from 0x%08X to 0x%08X\n",
200 pix_fmt, fmt.fmt.pix.pixelformat);
204 if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
217 res = ioctl(fd, VIDIOC_G_STD, &std);
221 if (std & V4L2_STD_NTSC) {
234 fmt_conversion_table[i].codec_id == codec_id) &&
237 return fmt_conversion_table[i].
v4l2_fmt;
249 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
250 fmt_conversion_table[i].codec_id == codec_id) {
251 return fmt_conversion_table[i].
ff_fmt;
263 if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
264 return fmt_conversion_table[i].
codec_id;
271 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
272 static void list_framesizes(
AVFormatContext *ctx,
int fd, uint32_t pixelformat)
274 struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
276 while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
278 case V4L2_FRMSIZE_TYPE_DISCRETE:
280 vfse.discrete.width, vfse.discrete.height);
282 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
283 case V4L2_FRMSIZE_TYPE_STEPWISE:
285 vfse.stepwise.min_width,
286 vfse.stepwise.max_width,
287 vfse.stepwise.step_width,
288 vfse.stepwise.min_height,
289 vfse.stepwise.max_height,
290 vfse.stepwise.step_height);
299 struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
301 while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
307 if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
311 fmt_name ? fmt_name :
"Unsupported",
313 }
else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
317 codec ? codec->
name :
"Unsupported",
323 #ifdef V4L2_FMT_FLAG_EMULATED
324 if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
329 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
330 list_framesizes(ctx, fd, vfd.pixelformat);
340 struct v4l2_requestbuffers req = {
341 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
343 .memory = V4L2_MEMORY_MMAP
346 res = ioctl(s->
fd, VIDIOC_REQBUFS, &req);
348 if (errno == EINVAL) {
377 for (i = 0; i < req.count; i++) {
378 struct v4l2_buffer buf = {
379 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
381 .memory = V4L2_MEMORY_MMAP
384 res = ioctl(s->
fd, VIDIOC_QUERYBUF, &buf);
394 "Buffer len [%d] = %d != %d\n",
400 PROT_READ | PROT_WRITE, MAP_SHARED,
401 s->
fd, buf.m.offset);
413 #if FF_API_DESTRUCT_PACKET
414 static void dummy_release_buffer(
AVPacket *pkt)
422 struct v4l2_buffer buf = { 0 };
424 struct buff_data *buf_descriptor = opaque;
427 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
428 buf.memory = V4L2_MEMORY_MMAP;
429 buf.index = buf_descriptor->
index;
430 fd = buf_descriptor->
fd;
433 res = ioctl(fd, VIDIOC_QBUF, &buf);
443 struct v4l2_buffer buf = {
444 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
445 .memory = V4L2_MEMORY_MMAP
447 struct pollfd p = { .fd = s->
fd, .events = POLLIN };
454 if (!(p.revents & (POLLIN | POLLERR | POLLHUP)))
458 while ((res = ioctl(s->
fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
460 if (errno == EAGAIN) {
481 "The v4l2 frame is %d bytes, but %d bytes are expected\n",
497 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
508 pkt->
size = buf.bytesused;
509 #if FF_API_DESTRUCT_PACKET
511 pkt->
destruct = dummy_release_buffer;
516 if (!buf_descriptor) {
521 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
525 buf_descriptor->fd = s->
fd;
526 buf_descriptor->index = buf.index;
527 buf_descriptor->s =
s;
536 pkt->
pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
544 enum v4l2_buf_type type;
547 for (i = 0; i < s->
buffers; i++) {
548 struct v4l2_buffer buf = {
549 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
551 .memory = V4L2_MEMORY_MMAP
554 res = ioctl(s->
fd, VIDIOC_QBUF, &buf);
564 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
565 res = ioctl(s->
fd, VIDIOC_STREAMON, &type);
578 enum v4l2_buf_type type;
581 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
585 ioctl(s->
fd, VIDIOC_STREAMOFF, &type);
586 for (i = 0; i < s->
buffers; i++) {
596 struct v4l2_input input = { 0 };
597 struct v4l2_standard standard = { 0 };
598 struct v4l2_streamparm streamparm = { 0 };
599 struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
603 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
614 if (ioctl(s->
fd, VIDIOC_ENUMINPUT, &input) < 0) {
621 if (ioctl(s->
fd, VIDIOC_S_INPUT, &input.index) < 0) {
623 "The V4L2 driver ioctl set input(%d) failed\n",
634 if (ioctl(s->
fd, VIDIOC_ENUMSTD, &standard) < 0) {
636 "The V4L2 driver ioctl set standard(%s) failed\n",
647 "The V4L2 driver set standard: %s, id: %"PRIu64
"\n",
648 s->
standard, (uint64_t)standard.id);
649 if (ioctl(s->
fd, VIDIOC_S_STD, &standard.id) < 0) {
651 "The V4L2 driver ioctl set standard(%s) failed\n",
657 if (framerate_q.
num && framerate_q.
den) {
659 framerate_q.
den, framerate_q.
num);
660 tpf->numerator = framerate_q.
den;
661 tpf->denominator = framerate_q.
num;
663 if (ioctl(s->
fd, VIDIOC_S_PARM, &streamparm) != 0) {
665 "ioctl set time per frame(%d/%d) failed\n",
666 framerate_q.
den, framerate_q.
num);
670 if (framerate_q.
num != tpf->denominator ||
671 framerate_q.
den != tpf->numerator) {
673 "The driver changed the time per frame from "
675 framerate_q.
den, framerate_q.
num,
676 tpf->numerator, tpf->denominator);
679 if (ioctl(s->
fd, VIDIOC_G_PARM, &streamparm) != 0) {
703 if (desired_format == 0 ||
704 device_init(s1, width, height, desired_format) < 0) {
711 desired_format = fmt_conversion_table[i].
v4l2_fmt;
712 if (
device_init(s1, width, height, desired_format) >= 0) {
720 if (desired_format != 0) {
725 return desired_format;
733 uint32_t desired_format;
776 struct v4l2_format fmt;
779 "Querying the device for the current frame size\n");
780 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
781 if (ioctl(s->
fd, VIDIOC_G_FMT, &fmt) < 0) {
787 s->
width = fmt.fmt.pix.width;
788 s->
height = fmt.fmt.pix.height;
790 "Setting frame size to %dx%d\n", s->
width, s->
height);
795 if (desired_format == 0) {
868 #define OFFSET(x) offsetof(struct video_data, x)
869 #define DEC AV_OPT_FLAG_DECODING_PARAM
892 .
name =
"video4linux2",
#define avpriv_atomic_int_add_and_fetch
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
void av_free_packet(AVPacket *pkt)
Free a packet.
This structure describes decoded (raw) audio or video data.
packed RGB 8:8:8, 24bpp, RGBRGB...
#define AV_LOG_WARNING
Something somehow does not look correct.
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
char * pixel_format
Set by a private option.
AVCodecContext * codec
Codec context associated with this stream.
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.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
volatile int buffers_queued
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)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
attribute_deprecated void(* destruct)(struct AVPacket *)
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.
AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
static int mmap_start(AVFormatContext *ctx)
const char * name
Name of the codec implementation.
static void list_formats(AVFormatContext *ctx, int fd, int type)
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.
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.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
static int device_open(AVFormatContext *ctx)
enum AVCodecID video_codec_id
Forced video codec_id.
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
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.
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(). ...
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
void * priv_data
Format private data.
static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
char filename[1024]
input or output filename
#define avpriv_atomic_int_get
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static const AVOption options[]
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
AVRational avg_frame_rate
Average framerate.
static uint32_t fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
AVPixelFormat
Pixel format.
int av_parse_video_rate(AVRational *rate, const char *str)
Parse str and store the detected values in *rate.
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...
int bit_rate
the average bitrate
int av_strcasecmp(const char *a, const char *b)
int width
picture width / height.
AVStream ** streams
A list of all streams in the file.
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static void mmap_close(struct video_data *s)
#define FF_ARRAY_ELEMS(a)
static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
enum AVPixelFormat pix_fmt
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
int list_format
Set by a private option.
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.
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.
enum AVMediaType codec_type
#define AV_PIX_FMT_RGB555
static void close(AVCodecParserContext *s)
packed RGB 8:8:8, 24bpp, BGRBGR...
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Describe the class of an AVClass context structure.
rational number numerator/denominator
AVFrame * coded_frame
the picture in the bitstream
static int mmap_init(AVFormatContext *ctx)
static const AVClass v4l2_class
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
#define AV_PIX_FMT_RGB565
#define FF_DISABLE_DEPRECATION_WARNINGS
static int v4l2_set_parameters(AVFormatContext *s1)
char * framerate
Set by a private option.
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
static enum AVCodecID fmt_v4l2codec(uint32_t v4l2_fmt)
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
#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)
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...
enum AVPixelFormat ff_fmt
AVInputFormat ff_v4l2_demuxer
static int v4l2_read_close(AVFormatContext *s1)
static void mmap_release_buffer(void *opaque, uint8_t *data)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static int first_field(int fd)
av_default_item_name
Return the context name.
static struct fmt_map fmt_conversion_table[]
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...