#include "libavutil/imgutils.h"
#include "libavutil/log.h"
#include "libavutil/opt.h"
+#include "libavutil/parseutils.h"
+#include "libavutil/pixdesc.h"
static const int desired_video_buffers = 256;
unsigned int *buf_len;
char *standard;
int channel;
+ char *video_size; /**< String describing video size, set by a private option. */
+ char *pixel_format; /**< Set by a private option. */
+ char *framerate; /**< Set by a private option. */
};
struct buff_data {
struct v4l2_standard standard;
struct v4l2_streamparm streamparm = { 0 };
struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
- int i;
+ int i, ret;
+ AVRational fps;
streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ if (s->framerate && (ret = av_parse_video_rate(&fps, s->framerate)) < 0) {
+ av_log(s1, AV_LOG_ERROR, "Couldn't parse framerate.\n");
+ return ret;
+ }
#if FF_API_FORMAT_PARAMETERS
if (ap->channel > 0)
s->channel = ap->channel;
+ if (ap->time_base.num)
+ fps = (AVRational){ap->time_base.den, ap->time_base.num};
#endif
/* set tv video input */
return AVERROR(EIO);
}
}
- av_freep(&s->standard);
- if (ap->time_base.num && ap->time_base.den) {
+ if (fps.num && fps.den) {
av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
- ap->time_base.num, ap->time_base.den);
- tpf->numerator = ap->time_base.num;
- tpf->denominator = ap->time_base.den;
+ fps.den, fps.num);
+ tpf->numerator = fps.den;
+ tpf->denominator = fps.num;
if (ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) {
av_log(s1, AV_LOG_ERROR,
"ioctl set time per frame(%d/%d) failed\n",
- ap->time_base.num, ap->time_base.den);
+ fps.den, fps.num);
return AVERROR(EIO);
}
- if (ap->time_base.den != tpf->denominator ||
- ap->time_base.num != tpf->numerator) {
+ if (fps.num != tpf->denominator ||
+ fps.den != tpf->numerator) {
av_log(s1, AV_LOG_INFO,
"The driver changed the time per frame from %d/%d to %d/%d\n",
- ap->time_base.num, ap->time_base.den,
+ fps.den, fps.num,
tpf->numerator, tpf->denominator);
}
} else {
- /* if timebase value is not set in ap, read the timebase value from the driver */
+ /* if timebase value is not set, read the timebase value from the driver */
if (ioctl(s->fd, VIDIOC_G_PARM, &streamparm) != 0) {
av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n", strerror(errno));
return AVERROR(errno);
}
}
- ap->time_base.num = tpf->numerator;
- ap->time_base.den = tpf->denominator;
+ s1->streams[0]->codec->time_base.den = tpf->denominator;
+ s1->streams[0]->codec->time_base.num = tpf->numerator;
return 0;
}
static uint32_t device_try_init(AVFormatContext *s1,
- const AVFormatParameters *ap,
+ enum PixelFormat pix_fmt,
int *width,
int *height,
enum CodecID *codec_id)
{
- uint32_t desired_format = fmt_ff2v4l(ap->pix_fmt, s1->video_codec_id);
+ uint32_t desired_format = fmt_ff2v4l(pix_fmt, s1->video_codec_id);
if (desired_format == 0 ||
device_init(s1, width, height, desired_format) < 0) {
{
struct video_data *s = s1->priv_data;
AVStream *st;
- int res;
+ int res = 0;
uint32_t desired_format, capabilities;
enum CodecID codec_id;
+ enum PixelFormat pix_fmt = PIX_FMT_NONE;
st = av_new_stream(s1, 0);
if (!st) {
- return AVERROR(ENOMEM);
+ res = AVERROR(ENOMEM);
+ goto out;
}
av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
- s->width = ap->width;
- s->height = ap->height;
+ if (s->video_size && (res = av_parse_video_size(&s->width, &s->height, s->video_size)) < 0) {
+ av_log(s1, AV_LOG_ERROR, "Couldn't parse video size.\n");
+ goto out;
+ }
+ if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
+ av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
+ res = AVERROR(EINVAL);
+ goto out;
+ }
+#if FF_API_FORMAT_PARAMETERS
+ if (ap->width > 0)
+ s->width = ap->width;
+ if (ap->height > 0)
+ s->height = ap->height;
+ if (ap->pix_fmt)
+ pix_fmt = ap->pix_fmt;
+#endif
capabilities = 0;
s->fd = device_open(s1, &capabilities);
if (s->fd < 0) {
- return AVERROR(EIO);
+ res = AVERROR(EIO);
+ goto out;
}
av_log(s1, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n", s->fd, capabilities);
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", strerror(errno));
- return AVERROR(errno);
+ res = AVERROR(errno);
+ goto out;
}
s->width = fmt.fmt.pix.width;
s->height = fmt.fmt.pix.height;
av_log(s1, AV_LOG_VERBOSE, "Setting frame size to %dx%d\n", s->width, s->height);
}
- desired_format = device_try_init(s1, ap, &s->width, &s->height, &codec_id);
+ desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height, &codec_id);
if (desired_format == 0) {
av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
- "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, ap->pix_fmt);
+ "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, pix_fmt);
close(s->fd);
- return AVERROR(EIO);
+ res = AVERROR(EIO);
+ goto out;
}
- if (av_image_check_size(s->width, s->height, 0, s1) < 0)
- return AVERROR(EINVAL);
+ if ((res = av_image_check_size(s->width, s->height, 0, s1) < 0))
+ goto out;
s->frame_format = desired_format;
- if (v4l2_set_parameters(s1, ap) < 0)
- return AVERROR(EIO);
+ if ((res = v4l2_set_parameters(s1, ap) < 0))
+ goto out;
st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
s->frame_size = avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
if (res < 0) {
close(s->fd);
- return AVERROR(EIO);
+ res = AVERROR(EIO);
+ goto out;
}
s->top_field_first = first_field(s->fd);
st->codec->codec_id = codec_id;
st->codec->width = s->width;
st->codec->height = s->height;
- 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;
+out:
+ av_freep(&s->video_size);
+ av_freep(&s->pixel_format);
+ av_freep(&s->standard);
+ av_freep(&s->framerate);
+ return res;
}
static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
return 0;
}
+#define OFFSET(x) offsetof(struct video_data, x)
+#define DEC AV_OPT_FLAG_DECODING_PARAM
static const AVOption options[] = {
{ "standard", "", offsetof(struct video_data, standard), FF_OPT_TYPE_STRING, {.str = "NTSC" }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
{ "channel", "", offsetof(struct video_data, channel), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
+ { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
+ { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
+ { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
{ NULL },
};