📄 v4l2.c
字号:
pkt->size = 0;
}
static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
{
struct video_data *s = ctx->priv_data;
struct v4l2_buffer buf;
struct buff_data *buf_descriptor;
int res;
memset(&buf, 0, sizeof(struct v4l2_buffer));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
/* FIXME: Some special treatment might be needed in case of loss of signal... */
while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
if (res < 0) {
if (errno == EAGAIN) {
pkt->size = 0;
return AVERROR(EAGAIN);
}
av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
return -1;
}
assert (buf.index < s->buffers);
if (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;
}
/* Image is at s->buff_start[buf.index] */
pkt->data= s->buf_start[buf.index];
pkt->size = buf.bytesused;
pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
pkt->destruct = mmap_release_buffer;
buf_descriptor = av_malloc(sizeof(struct buff_data));
if (buf_descriptor == NULL) {
/* Something went wrong... Since av_malloc() failed, we cannot even
* allocate a buffer for memcopying into it
*/
av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
res = ioctl (s->fd, VIDIOC_QBUF, &buf);
return -1;
}
buf_descriptor->fd = s->fd;
buf_descriptor->index = buf.index;
pkt->priv = buf_descriptor;
return s->buf_len[buf.index];
}
static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
{
return -1;
}
static int mmap_start(AVFormatContext *ctx)
{
struct video_data *s = ctx->priv_data;
enum v4l2_buf_type type;
int i, res;
for (i = 0; i < s->buffers; i++) {
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(struct v4l2_buffer));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
res = ioctl (s->fd, VIDIOC_QBUF, &buf);
if (res < 0) {
av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
return -1;
}
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
res = ioctl (s->fd, VIDIOC_STREAMON, &type);
if (res < 0) {
av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
return -1;
}
return 0;
}
static void mmap_close(struct video_data *s)
{
enum v4l2_buf_type type;
int i;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
/* We do not check for the result, because we could
* not do anything about it anyway...
*/
ioctl(s->fd, VIDIOC_STREAMOFF, &type);
for (i = 0; i < s->buffers; i++) {
munmap(s->buf_start[i], s->buf_len[i]);
}
av_free(s->buf_start);
av_free(s->buf_len);
}
static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
{
struct video_data *s = s1->priv_data;
struct v4l2_input input;
struct v4l2_standard standard;
int i;
if(ap->channel>=0) {
/* set tv video input */
memset (&input, 0, sizeof (input));
input.index = ap->channel;
if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
return AVERROR(EIO);
}
av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
ap->channel, input.name);
if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
ap->channel);
return AVERROR(EIO);
}
}
if(ap->standard) {
av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
ap->standard );
/* set tv standard */
memset (&standard, 0, sizeof (standard));
for(i=0;;i++) {
standard.index = i;
if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
ap->standard);
return AVERROR(EIO);
}
if(!strcasecmp(standard.name, ap->standard)) {
break;
}
}
av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
ap->standard, 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 AVERROR(EIO);
}
}
return 0;
}
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;
uint32_t desired_format, capabilities;
if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
return -1;
}
width = ap->width;
height = ap->height;
frame_rate = ap->time_base.den;
frame_rate_base = ap->time_base.num;
if((unsigned)width > 32767 || (unsigned)height > 32767) {
av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);
return -1;
}
st = av_new_stream(s1, 0);
if (!st) {
return AVERROR(ENOMEM);
}
av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
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);
if (s->fd < 0) {
av_free(st);
return AVERROR(EIO);
}
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 == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {
done = 1;
}
}
}
if (desired_format == 0) {
av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");
close(s->fd);
av_free(st);
return AVERROR(EIO);
}
s->frame_format = desired_format;
if( v4l2_set_parameters( s1, ap ) < 0 )
return AVERROR(EIO);
st->codec->pix_fmt = fmt_v4l2ff(desired_format);
s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);
if (capabilities & V4L2_CAP_STREAMING) {
s->io_method = io_mmap;
res = mmap_init(s1);
if (res == 0) {
res = mmap_start(s1);
}
} else {
s->io_method = io_read;
res = read_init(s1);
}
if (res < 0) {
close(s->fd);
av_free(st);
return AVERROR(EIO);
}
s->top_field_first = first_field(s->fd);
st->codec->codec_type = CODEC_TYPE_VIDEO;
st->codec->codec_id = CODEC_ID_RAWVIDEO;
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->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
return 0;
}
static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
{
struct video_data *s = s1->priv_data;
int res;
if (s->io_method == io_mmap) {
av_init_packet(pkt);
res = mmap_read_frame(s1, pkt);
} else if (s->io_method == io_read) {
if (av_new_packet(pkt, s->frame_size) < 0)
return AVERROR(EIO);
res = read_frame(s1, pkt);
} else {
return AVERROR(EIO);
}
if (res < 0) {
return res;
}
if (s1->streams[0]->codec->coded_frame) {
s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
}
return s->frame_size;
}
static int v4l2_read_close(AVFormatContext *s1)
{
struct video_data *s = s1->priv_data;
if (s->io_method == io_mmap) {
mmap_close(s);
}
close(s->fd);
return 0;
}
AVInputFormat v4l2_demuxer = {
"video4linux2",
"video grab",
sizeof(struct video_data),
NULL,
v4l2_read_header,
v4l2_read_packet,
v4l2_read_close,
.flags = AVFMT_NOFILE,
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -