0a7b514f |
/* |
406792e7 |
* Copyright (c) 2000,2001 Fabrice Bellard
* Copyright (c) 2006 Luca Abeni |
0a7b514f |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
0a7b514f |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
0a7b514f |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
0a7b514f |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
0a7b514f |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ |
b0067549 |
|
895e4de8 |
/**
* @file
* Video4Linux2 grab interface
*
* Part of this file is based on the V4L2 video capture example
* (http://v4l2spec.bytesex.org/v4l2spec/capture.c)
*
* Thanks to Michael Niedermayer for providing the mapping between
* V4L2_PIX_FMT_* and PIX_FMT_*
*/
|
e9d4b8a5 |
#undef __STRICT_ANSI__ //workaround due to broken kernel headers |
b0067549 |
#include "config.h" |
0a7b514f |
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h> |
b250f9c6 |
#if HAVE_SYS_VIDEOIO_H |
1b6e8b73 |
#include <sys/videoio.h>
#else |
f743a062 |
#include <asm/types.h>
#include <linux/videodev2.h> |
1b6e8b73 |
#endif |
0a7b514f |
#include <time.h> |
ea452b54 |
#include <strings.h> |
7ffe76e5 |
#include "libavutil/imgutils.h" |
b3da2692 |
#include "libavutil/log.h"
#include "libavutil/opt.h" |
6b899e16 |
#include "avdevice.h" |
8fe7b644 |
#include "libavutil/parseutils.h" |
d576bbf3 |
#include "libavutil/pixdesc.h" |
0a7b514f |
static const int desired_video_buffers = 256;
enum io_method {
io_read,
io_mmap,
io_userptr
};
struct video_data { |
b3da2692 |
AVClass *class; |
0a7b514f |
int fd;
int frame_format; /* V4L2_PIX_FMT_* */
enum io_method io_method;
int width, height;
int frame_size;
int top_field_first;
int buffers;
void **buf_start;
unsigned int *buf_len; |
b3da2692 |
char *standard; |
a02fd06a |
int channel; |
8fe7b644 |
char *video_size; /**< String describing video size, set by a private option. */ |
d576bbf3 |
char *pixel_format; /**< Set by a private option. */ |
c21324ee |
char *framerate; /**< Set by a private option. */ |
0a7b514f |
};
|
41536a60 |
struct buff_data {
int index;
int fd;
};
|
0a7b514f |
struct fmt_map {
enum PixelFormat ff_fmt; |
9eb6e9d9 |
enum CodecID codec_id; |
9202218e |
uint32_t v4l2_fmt; |
0a7b514f |
};
static struct fmt_map fmt_conversion_table[] = { |
01058893 |
//ff_fmt codec_id v4l2_fmt
{ PIX_FMT_YUV420P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV420 },
{ PIX_FMT_YUV422P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV422P },
{ PIX_FMT_YUYV422, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUYV },
{ PIX_FMT_UYVY422, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_UYVY },
{ PIX_FMT_YUV411P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV411P },
{ PIX_FMT_YUV410P, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV410 },
{ PIX_FMT_RGB555, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB555 },
{ PIX_FMT_RGB565, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB565 },
{ PIX_FMT_BGR24, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR24 },
{ PIX_FMT_RGB24, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB24 },
{ PIX_FMT_BGRA, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR32 },
{ PIX_FMT_GRAY8, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_GREY },
{ PIX_FMT_NV12, CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12 },
{ PIX_FMT_NONE, CODEC_ID_MJPEG, V4L2_PIX_FMT_MJPEG },
{ PIX_FMT_NONE, CODEC_ID_MJPEG, V4L2_PIX_FMT_JPEG }, |
0a7b514f |
};
|
cc58300e |
static int device_open(AVFormatContext *ctx, uint32_t *capabilities) |
0a7b514f |
{
struct v4l2_capability cap;
int fd; |
50f85218 |
int res, err; |
653387d8 |
int flags = O_RDWR; |
0a7b514f |
|
653387d8 |
if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
flags |= O_NONBLOCK;
}
fd = open(ctx->filename, flags, 0); |
0a7b514f |
if (fd < 0) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n", |
cc58300e |
ctx->filename, strerror(errno)); |
c57a8fef |
return AVERROR(errno); |
0a7b514f |
}
res = ioctl(fd, VIDIOC_QUERYCAP, &cap); |
c16601bb |
// ENOIOCTLCMD definition only availble on __KERNEL__ |
50f85218 |
if (res < 0 && ((err = errno) == 515)) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n"); |
2b63c222 |
close(fd); |
c16601bb |
|
50f85218 |
return AVERROR(515); |
c16601bb |
} |
0a7b514f |
if (res < 0) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n", |
0a7b514f |
strerror(errno)); |
2b63c222 |
close(fd); |
50f85218 |
return AVERROR(err); |
0a7b514f |
}
if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n"); |
2b63c222 |
close(fd); |
c57a8fef |
return AVERROR(ENODEV); |
0a7b514f |
}
*capabilities = cap.capabilities;
return fd;
}
|
9202218e |
static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt) |
0a7b514f |
{ |
c7238c72 |
struct video_data *s = ctx->priv_data;
int fd = s->fd; |
7b017086 |
struct v4l2_format fmt = {0}; |
24300af4 |
int res; |
0a7b514f |
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
24300af4 |
fmt.fmt.pix.width = *width;
fmt.fmt.pix.height = *height; |
0a7b514f |
fmt.fmt.pix.pixelformat = pix_fmt;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; |
24300af4 |
res = ioctl(fd, VIDIOC_S_FMT, &fmt);
if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) { |
c7238c72 |
av_log(ctx, AV_LOG_INFO, "The V4L2 driver changed the video from %dx%d to %dx%d\n", *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height); |
24300af4 |
*width = fmt.fmt.pix.width;
*height = fmt.fmt.pix.height;
}
|
3d0d9a5e |
if (pix_fmt != fmt.fmt.pix.pixelformat) {
av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver changed the pixel format from 0x%08X to 0x%08X\n", pix_fmt, fmt.fmt.pix.pixelformat);
res = -1;
}
|
24300af4 |
return res; |
0a7b514f |
}
static int first_field(int fd)
{
int res;
v4l2_std_id std;
res = ioctl(fd, VIDIOC_G_STD, &std);
if (res < 0) {
return 0;
}
if (std & V4L2_STD_NTSC) {
return 0;
}
return 1;
}
|
3db77ccf |
static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt, enum CodecID codec_id) |
0a7b514f |
{
int i;
|
37d3e066 |
for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) { |
3db77ccf |
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)) { |
0a7b514f |
return fmt_conversion_table[i].v4l2_fmt;
}
}
return 0;
}
|
3db77ccf |
static enum PixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum CodecID codec_id) |
0a7b514f |
{
int i;
|
37d3e066 |
for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) { |
3db77ccf |
if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
fmt_conversion_table[i].codec_id == codec_id) { |
0a7b514f |
return fmt_conversion_table[i].ff_fmt;
}
}
|
eacced45 |
return PIX_FMT_NONE; |
0a7b514f |
}
|
3db77ccf |
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;
}
|
c7238c72 |
static int mmap_init(AVFormatContext *ctx) |
0a7b514f |
{ |
c7238c72 |
struct video_data *s = ctx->priv_data; |
7b017086 |
struct v4l2_requestbuffers req = {0}; |
0a7b514f |
int i, res;
req.count = desired_video_buffers;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP; |
680861ca |
res = ioctl(s->fd, VIDIOC_REQBUFS, &req); |
0a7b514f |
if (res < 0) {
if (errno == EINVAL) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n"); |
0a7b514f |
} else { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n"); |
0a7b514f |
} |
c57a8fef |
return AVERROR(errno); |
0a7b514f |
}
if (req.count < 2) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n"); |
c57a8fef |
return AVERROR(ENOMEM); |
0a7b514f |
}
s->buffers = req.count;
s->buf_start = av_malloc(sizeof(void *) * s->buffers);
if (s->buf_start == NULL) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n"); |
c57a8fef |
return AVERROR(ENOMEM); |
0a7b514f |
}
s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
if (s->buf_len == NULL) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n"); |
0a7b514f |
av_free(s->buf_start); |
c57a8fef |
return AVERROR(ENOMEM); |
0a7b514f |
}
for (i = 0; i < req.count; i++) { |
7b017086 |
struct v4l2_buffer buf = {0}; |
0a7b514f |
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i; |
680861ca |
res = ioctl(s->fd, VIDIOC_QUERYBUF, &buf); |
0a7b514f |
if (res < 0) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n"); |
c57a8fef |
return AVERROR(errno); |
0a7b514f |
}
s->buf_len[i] = buf.length; |
3db77ccf |
if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size); |
0a7b514f |
return -1;
}
s->buf_start[i] = mmap (NULL, buf.length,
PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
if (s->buf_start[i] == MAP_FAILED) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno)); |
c57a8fef |
return AVERROR(errno); |
0a7b514f |
}
}
return 0;
}
|
c7238c72 |
static int read_init(AVFormatContext *ctx) |
0a7b514f |
{
return -1;
}
|
41536a60 |
static void mmap_release_buffer(AVPacket *pkt)
{ |
7b017086 |
struct v4l2_buffer buf = {0}; |
41536a60 |
int res, fd;
struct buff_data *buf_descriptor = pkt->priv;
|
5449a787 |
if (pkt->data == NULL) {
return;
}
|
41536a60 |
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = buf_descriptor->index;
fd = buf_descriptor->fd;
av_free(buf_descriptor);
|
680861ca |
res = ioctl(fd, VIDIOC_QBUF, &buf); |
41536a60 |
if (res < 0) { |
bd7d9e45 |
av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno)); |
41536a60 |
}
pkt->data = NULL;
pkt->size = 0;
}
static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) |
0a7b514f |
{ |
c7238c72 |
struct video_data *s = ctx->priv_data; |
7b017086 |
struct v4l2_buffer buf = {0}; |
41536a60 |
struct buff_data *buf_descriptor; |
0a7b514f |
int res;
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... */ |
653387d8 |
while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR)); |
0a7b514f |
if (res < 0) { |
653387d8 |
if (errno == EAGAIN) {
pkt->size = 0;
return AVERROR(EAGAIN);
} |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno)); |
0a7b514f |
|
c57a8fef |
return AVERROR(errno); |
0a7b514f |
} |
2d48515e |
assert(buf.index < s->buffers); |
3db77ccf |
if (s->frame_size > 0 && buf.bytesused != s->frame_size) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size); |
c57a8fef |
return AVERROR_INVALIDDATA; |
7c7e7464 |
}
|
0a7b514f |
/* Image is at s->buff_start[buf.index] */ |
41536a60 |
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"); |
680861ca |
res = ioctl(s->fd, VIDIOC_QBUF, &buf); |
0a7b514f |
|
c57a8fef |
return AVERROR(ENOMEM); |
0a7b514f |
} |
41536a60 |
buf_descriptor->fd = s->fd;
buf_descriptor->index = buf.index;
pkt->priv = buf_descriptor; |
0a7b514f |
return s->buf_len[buf.index];
}
|
41536a60 |
static int read_frame(AVFormatContext *ctx, AVPacket *pkt) |
0a7b514f |
{
return -1;
}
|
c7238c72 |
static int mmap_start(AVFormatContext *ctx) |
0a7b514f |
{ |
c7238c72 |
struct video_data *s = ctx->priv_data; |
0a7b514f |
enum v4l2_buf_type type;
int i, res;
for (i = 0; i < s->buffers; i++) { |
7b017086 |
struct v4l2_buffer buf = {0}; |
0a7b514f |
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
|
680861ca |
res = ioctl(s->fd, VIDIOC_QBUF, &buf); |
0a7b514f |
if (res < 0) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno)); |
c57a8fef |
return AVERROR(errno); |
0a7b514f |
}
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
680861ca |
res = ioctl(s->fd, VIDIOC_STREAMON, &type); |
0a7b514f |
if (res < 0) { |
c7238c72 |
av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno)); |
c57a8fef |
return AVERROR(errno); |
0a7b514f |
}
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);
}
|
680861ca |
static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap) |
f5ad81f5 |
{
struct video_data *s = s1->priv_data; |
7b017086 |
struct v4l2_input input = {0};
struct v4l2_standard standard = {0}; |
2d48515e |
struct v4l2_streamparm streamparm = {0}; |
5d3d238f |
struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe; |
7533a727 |
int i, ret; |
e5578ad3 |
AVRational fps={0}; |
f5ad81f5 |
|
5d3d238f |
streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
c21324ee |
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;
} |
d20576d0 |
#if FF_API_FORMAT_PARAMETERS |
a02fd06a |
if (ap->channel > 0)
s->channel = ap->channel; |
c21324ee |
if (ap->time_base.num)
fps = (AVRational){ap->time_base.den, ap->time_base.num}; |
d20576d0 |
#endif |
f5ad81f5 |
|
3d2a4186 |
/* set tv video input */
input.index = s->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);
} |
f5ad81f5 |
|
3d2a4186 |
av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
s->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",
s->channel);
return AVERROR(EIO); |
0d6c0732 |
} |
f5ad81f5 |
|
fc68a8f7 |
#if FF_API_FORMAT_PARAMETERS |
680861ca |
if (ap->standard) { |
b3da2692 |
av_freep(&s->standard);
s->standard = av_strdup(ap->standard);
} |
fc68a8f7 |
#endif |
b3da2692 |
if (s->standard) { |
e4dd03f3 |
av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n", |
b3da2692 |
s->standard); |
e4dd03f3 |
/* set tv standard */ |
2d48515e |
for (i = 0;; i++) { |
e4dd03f3 |
standard.index = i; |
7533a727 |
ret = ioctl(s->fd, VIDIOC_ENUMSTD, &standard);
if (ret < 0 || !strcasecmp(standard.name, s->standard)) |
e4dd03f3 |
break; |
7533a727 |
}
if (ret < 0) {
av_log(s1, AV_LOG_ERROR, "Unknown standard '%s'\n", s->standard);
return ret; |
e4dd03f3 |
}
av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n", |
b3da2692 |
s->standard, (uint64_t)standard.id); |
e4dd03f3 |
if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) { |
f5ad81f5 |
av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n", |
b3da2692 |
s->standard); |
6f3e0b21 |
return AVERROR(EIO); |
f5ad81f5 |
} |
0d6c0732 |
} |
f5ad81f5 |
|
c21324ee |
if (fps.num && fps.den) { |
70f77361 |
av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n", |
c21324ee |
fps.den, fps.num);
tpf->numerator = fps.den;
tpf->denominator = fps.num; |
70f77361 |
if (ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) {
av_log(s1, AV_LOG_ERROR,
"ioctl set time per frame(%d/%d) failed\n", |
c21324ee |
fps.den, fps.num); |
70f77361 |
return AVERROR(EIO);
}
|
c21324ee |
if (fps.num != tpf->denominator ||
fps.den != tpf->numerator) { |
70f77361 |
av_log(s1, AV_LOG_INFO,
"The driver changed the time per frame from %d/%d to %d/%d\n", |
c21324ee |
fps.den, fps.num, |
70f77361 |
tpf->numerator, tpf->denominator);
} |
8621a37d |
} else { |
c21324ee |
/* if timebase value is not set, read the timebase value from the driver */ |
8621a37d |
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);
} |
70f77361 |
} |
a3b15e41 |
s1->streams[0]->codec->time_base.den = tpf->denominator;
s1->streams[0]->codec->time_base.num = tpf->numerator; |
70f77361 |
|
f5ad81f5 |
return 0;
}
|
8040c3b2 |
static uint32_t device_try_init(AVFormatContext *s1, |
d576bbf3 |
enum PixelFormat pix_fmt, |
8040c3b2 |
int *width,
int *height,
enum CodecID *codec_id) |
3db77ccf |
{ |
d576bbf3 |
uint32_t desired_format = fmt_ff2v4l(pix_fmt, s1->video_codec_id); |
3db77ccf |
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++) { |
90d0379f |
if (s1->video_codec_id == CODEC_ID_NONE ||
fmt_conversion_table[i].codec_id == s1->video_codec_id) { |
3db77ccf |
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;
}
|
0a7b514f |
static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
{
struct video_data *s = s1->priv_data;
AVStream *st; |
82b5aa0a |
int res = 0; |
0a7b514f |
uint32_t desired_format, capabilities; |
3db77ccf |
enum CodecID codec_id; |
d576bbf3 |
enum PixelFormat pix_fmt = PIX_FMT_NONE; |
0a7b514f |
st = av_new_stream(s1, 0);
if (!st) { |
82b5aa0a |
res = AVERROR(ENOMEM);
goto out; |
0a7b514f |
}
av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
|
8fe7b644 |
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;
} |
d576bbf3 |
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;
} |
8fe7b644 |
#if FF_API_FORMAT_PARAMETERS
if (ap->width > 0)
s->width = ap->width;
if (ap->height > 0)
s->height = ap->height; |
d576bbf3 |
if (ap->pix_fmt)
pix_fmt = ap->pix_fmt; |
8fe7b644 |
#endif |
0a7b514f |
capabilities = 0; |
cc58300e |
s->fd = device_open(s1, &capabilities); |
0a7b514f |
if (s->fd < 0) { |
82b5aa0a |
res = AVERROR(EIO);
goto out; |
0a7b514f |
} |
1ace15f5 |
av_log(s1, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n", s->fd, capabilities); |
0a7b514f |
|
932d775f |
if (!s->width && !s->height) {
struct v4l2_format fmt;
|
1ace15f5 |
av_log(s1, AV_LOG_VERBOSE, "Querying the device for the current frame size\n"); |
932d775f |
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)); |
82b5aa0a |
res = AVERROR(errno);
goto out; |
932d775f |
}
s->width = fmt.fmt.pix.width;
s->height = fmt.fmt.pix.height; |
1ace15f5 |
av_log(s1, AV_LOG_VERBOSE, "Setting frame size to %dx%d\n", s->width, s->height); |
932d775f |
}
|
d576bbf3 |
desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height, &codec_id); |
0a7b514f |
if (desired_format == 0) { |
3db77ccf |
av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for " |
fefa67d5 |
"codec_id %d, pix_fmt %d.\n", s1->video_codec_id, pix_fmt); |
0a7b514f |
close(s->fd);
|
82b5aa0a |
res = AVERROR(EIO);
goto out; |
0a7b514f |
} |
82b5aa0a |
if ((res = av_image_check_size(s->width, s->height, 0, s1) < 0))
goto out; |
0a7b514f |
s->frame_format = desired_format;
|
82b5aa0a |
if ((res = v4l2_set_parameters(s1, ap) < 0))
goto out; |
f5ad81f5 |
|
3db77ccf |
st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id); |
18225025 |
s->frame_size = avpicture_get_size(st->codec->pix_fmt, s->width, s->height); |
0a7b514f |
if (capabilities & V4L2_CAP_STREAMING) {
s->io_method = io_mmap; |
c7238c72 |
res = mmap_init(s1); |
eaddc471 |
if (res == 0) { |
c7238c72 |
res = mmap_start(s1); |
eaddc471 |
} |
0a7b514f |
} else {
s->io_method = io_read; |
c7238c72 |
res = read_init(s1); |
0a7b514f |
}
if (res < 0) {
close(s->fd); |
82b5aa0a |
res = AVERROR(EIO);
goto out; |
0a7b514f |
}
s->top_field_first = first_field(s->fd);
|
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
3db77ccf |
st->codec->codec_id = codec_id; |
18225025 |
st->codec->width = s->width;
st->codec->height = s->height; |
0a7b514f |
st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
|
82b5aa0a |
out:
return res; |
0a7b514f |
}
static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
{
struct video_data *s = s1->priv_data;
int res;
if (s->io_method == io_mmap) { |
41536a60 |
av_init_packet(pkt);
res = mmap_read_frame(s1, pkt); |
0a7b514f |
} else if (s->io_method == io_read) { |
41536a60 |
if (av_new_packet(pkt, s->frame_size) < 0) |
6f3e0b21 |
return AVERROR(EIO); |
41536a60 |
res = read_frame(s1, pkt); |
0a7b514f |
} else { |
6f3e0b21 |
return AVERROR(EIO); |
0a7b514f |
}
if (res < 0) { |
411f5c6a |
return res; |
0a7b514f |
}
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;
}
|
158aa9f2 |
return pkt->size; |
0a7b514f |
}
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;
}
|
8fe7b644 |
#define OFFSET(x) offsetof(struct video_data, x)
#define DEC AV_OPT_FLAG_DECODING_PARAM |
f8f3f6c4 |
|
b3da2692 |
static const AVOption options[] = { |
3251af90 |
{ "standard", "", OFFSET(standard), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM }, |
f8f3f6c4 |
{ "channel", "", OFFSET(channel), FF_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, |
8fe7b644 |
{ "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, |
d576bbf3 |
{ "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, |
c21324ee |
{ "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, |
b3da2692 |
{ NULL },
};
static const AVClass v4l2_class = {
.class_name = "V4L2 indev",
.item_name = av_default_item_name,
.option = options,
.version = LIBAVUTIL_VERSION_INT,
};
|
66355be3 |
AVInputFormat ff_v4l2_demuxer = { |
0a7b514f |
"video4linux2", |
df2a3f27 |
NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"), |
0a7b514f |
sizeof(struct video_data),
NULL,
v4l2_read_header,
v4l2_read_packet,
v4l2_read_close,
.flags = AVFMT_NOFILE, |
b3da2692 |
.priv_class = &v4l2_class, |
0a7b514f |
}; |