/*
* Video4Linux2 grab interface
- * Copyright (c) 2000,2001 Fabrice Bellard.
- * Copyright (c) 2006 Luca Abeni.
+ * Copyright (c) 2000,2001 Fabrice Bellard
+ * Copyright (c) 2006 Luca Abeni
*
* Part of this file is based on the V4L2 video capture example
* (http://v4l2spec.bytesex.org/v4l2spec/capture.c)
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
-#ifdef HAVE_SYS_VIDEOIO_H
+#if HAVE_SYS_VIDEOIO_H
#include <sys/videoio.h>
#else
#include <asm/types.h>
int frame_format; /* V4L2_PIX_FMT_* */
enum io_method io_method;
int width, height;
- int frame_rate;
- int frame_rate_base;
int frame_size;
int top_field_first;
struct fmt_map {
enum PixelFormat ff_fmt;
- int32_t v4l2_fmt;
+ enum CodecID codec_id;
+ uint32_t v4l2_fmt;
};
static struct fmt_map fmt_conversion_table[] = {
{
.ff_fmt = PIX_FMT_YUV420P,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_YUV420,
},
{
.ff_fmt = PIX_FMT_YUV422P,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_YUV422P,
},
{
.ff_fmt = PIX_FMT_YUYV422,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_YUYV,
},
{
.ff_fmt = PIX_FMT_UYVY422,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_UYVY,
},
{
.ff_fmt = PIX_FMT_YUV411P,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_YUV411P,
},
{
.ff_fmt = PIX_FMT_YUV410P,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_YUV410,
},
{
.ff_fmt = PIX_FMT_RGB555,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_RGB555,
},
{
.ff_fmt = PIX_FMT_RGB565,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_RGB565,
},
{
.ff_fmt = PIX_FMT_BGR24,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_BGR24,
},
{
.ff_fmt = PIX_FMT_RGB24,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_RGB24,
},
{
.ff_fmt = PIX_FMT_BGRA,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_BGR32,
},
{
.ff_fmt = PIX_FMT_GRAY8,
+ .codec_id = CODEC_ID_RAWVIDEO,
.v4l2_fmt = V4L2_PIX_FMT_GREY,
},
+ {
+ .ff_fmt = PIX_FMT_NONE,
+ .codec_id = CODEC_ID_MJPEG,
+ .v4l2_fmt = V4L2_PIX_FMT_MJPEG,
+ },
+ {
+ .ff_fmt = PIX_FMT_NONE,
+ .codec_id = CODEC_ID_MJPEG,
+ .v4l2_fmt = V4L2_PIX_FMT_JPEG,
+ },
};
static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
return fd;
}
-static int device_init(AVFormatContext *ctx, int *width, int *height, int pix_fmt)
+static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
{
struct video_data *s = ctx->priv_data;
int fd = s->fd;
return 1;
}
-static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt)
+static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt, enum CodecID codec_id)
{
int i;
for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
- if (fmt_conversion_table[i].ff_fmt == pix_fmt) {
+ if ((codec_id == CODEC_ID_NONE ||
+ fmt_conversion_table[i].codec_id == codec_id) &&
+ (pix_fmt == PIX_FMT_NONE ||
+ fmt_conversion_table[i].ff_fmt == pix_fmt)) {
return fmt_conversion_table[i].v4l2_fmt;
}
}
return 0;
}
-static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)
+static enum PixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum CodecID codec_id)
{
int i;
for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
- if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {
+ if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
+ fmt_conversion_table[i].codec_id == codec_id) {
return fmt_conversion_table[i].ff_fmt;
}
}
return PIX_FMT_NONE;
}
+static enum CodecID fmt_v4l2codec(uint32_t v4l2_fmt)
+{
+ int i;
+
+ for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
+ if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
+ return fmt_conversion_table[i].codec_id;
+ }
+ }
+
+ return CODEC_ID_NONE;
+}
+
static int mmap_init(AVFormatContext *ctx)
{
struct video_data *s = ctx->priv_data;
}
s->buf_len[i] = buf.length;
- if (s->buf_len[i] < s->frame_size) {
+ if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
return -1;
int res, fd;
struct buff_data *buf_descriptor = pkt->priv;
+ if (pkt->data == NULL) {
+ return;
+ }
+
memset(&buf, 0, sizeof(struct v4l2_buffer));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
return -1;
}
assert (buf.index < s->buffers);
- if (buf.bytesused != s->frame_size) {
+ if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
return -1;
}
av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
- ap->standard, standard.id);
+ ap->standard, (uint64_t)standard.id);
if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
ap->standard);
return 0;
}
+static uint32_t device_try_init(AVFormatContext *s1,
+ const AVFormatParameters *ap,
+ int *width,
+ int *height,
+ enum CodecID *codec_id)
+{
+ uint32_t desired_format = fmt_ff2v4l(ap->pix_fmt, ap->video_codec_id);
+
+ if (desired_format == 0 ||
+ device_init(s1, width, height, desired_format) < 0) {
+ int i;
+
+ desired_format = 0;
+ for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
+ if (ap->video_codec_id == CODEC_ID_NONE ||
+ fmt_conversion_table[i].codec_id == ap->video_codec_id) {
+ desired_format = fmt_conversion_table[i].v4l2_fmt;
+ if (device_init(s1, width, height, desired_format) >= 0) {
+ break;
+ }
+ desired_format = 0;
+ }
+ }
+ }
+ if (desired_format != 0) {
+ *codec_id = fmt_v4l2codec(desired_format);
+ assert(*codec_id != CODEC_ID_NONE);
+ }
+
+ return desired_format;
+}
+
static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
{
struct video_data *s = s1->priv_data;
AVStream *st;
int width, height;
- int res, frame_rate, frame_rate_base;
+ int res;
uint32_t desired_format, capabilities;
+ enum CodecID codec_id;
if (ap->width <= 0 || ap->height <= 0) {
av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
return -1;
}
- if (ap->time_base.den <= 0) {
- av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
- return -1;
- }
width = ap->width;
height = ap->height;
- frame_rate = ap->time_base.den;
- frame_rate_base = ap->time_base.num;
if(avcodec_check_dimensions(s1, ap->width, ap->height) < 0)
return -1;
s->width = width;
s->height = height;
- s->frame_rate = frame_rate;
- s->frame_rate_base = frame_rate_base;
capabilities = 0;
s->fd = device_open(s1, &capabilities);
}
av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
- desired_format = fmt_ff2v4l(ap->pix_fmt);
- if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {
- int i, done;
-
- done = 0; i = 0;
- while (!done) {
- desired_format = fmt_conversion_table[i].v4l2_fmt;
- if (device_init(s1, &width, &height, desired_format) < 0) {
- desired_format = 0;
- i++;
- } else {
- done = 1;
- }
- if (i == FF_ARRAY_ELEMS(fmt_conversion_table)) {
- done = 1;
- }
- }
- }
+ desired_format = device_try_init(s1, ap, &width, &height, &codec_id);
if (desired_format == 0) {
- av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
+ av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
+ "codec_id %d, pix_fmt %d.\n", ap->video_codec_id, ap->pix_fmt);
close(s->fd);
return AVERROR(EIO);
if( v4l2_set_parameters( s1, ap ) < 0 )
return AVERROR(EIO);
- st->codec->pix_fmt = fmt_v4l2ff(desired_format);
+ st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
if (capabilities & V4L2_CAP_STREAMING) {
s->io_method = io_mmap;
s->top_field_first = first_field(s->fd);
st->codec->codec_type = CODEC_TYPE_VIDEO;
- st->codec->codec_id = CODEC_ID_RAWVIDEO;
+ st->codec->codec_id = codec_id;
st->codec->width = width;
st->codec->height = height;
- st->codec->time_base.den = frame_rate;
- st->codec->time_base.num = frame_rate_base;
+ st->codec->time_base.den = ap->time_base.den;
+ st->codec->time_base.num = ap->time_base.num;
st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
return 0;
s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
}
- return s->frame_size;
+ return pkt->size;
}
static int v4l2_read_close(AVFormatContext *s1)