#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
-#define _LINUX_TIME_H 1
-#include <linux/videodev.h>
+#include <asm/types.h>
+#include <linux/videodev2.h>
#include <time.h>
static const int desired_video_buffers = 256;
if (res < 0 && errno == 515)
{
av_log(NULL, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
+ close(fd);
return -1;
}
if (res < 0) {
av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
strerror(errno));
+ close(fd);
return -1;
}
if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
av_log(NULL, AV_LOG_ERROR, "Not a video capture device\n");
+ close(fd);
return -1;
}
return fd;
}
-static int device_init(int fd, int width, int height, int pix_fmt)
+static int device_init(int fd, int *width, int *height, int pix_fmt)
{
struct v4l2_format fmt;
+ int res;
memset(&fmt, 0, sizeof(struct v4l2_format));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- fmt.fmt.pix.width = width;
- fmt.fmt.pix.height = height;
+ fmt.fmt.pix.width = *width;
+ fmt.fmt.pix.height = *height;
fmt.fmt.pix.pixelformat = pix_fmt;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
- return ioctl (fd, VIDIOC_S_FMT, &fmt);
+ res = ioctl(fd, VIDIOC_S_FMT, &fmt);
+ if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
+ av_log(NULL, 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);
+ *width = fmt.fmt.pix.width;
+ *height = fmt.fmt.pix.height;
+ }
+
+ return res;
}
static int first_field(int fd)
uint32_t desired_format, capabilities;
const char *video_device;
- if (!ap || ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
+ if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
av_log(s1, AV_LOG_ERROR, "Missing/Wrong parameters\n");
return -1;
return AVERROR_IO;
}
- av_log(s1, AV_LOG_ERROR, "[%d]Capabilities: %x\n", s->fd, 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(s->fd, width, height, desired_format) < 0)) {
+ if (desired_format == 0 || (device_init(s->fd, &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(s->fd, width, height, desired_format) < 0) {
+ if (device_init(s->fd, &width, &height, desired_format) < 0) {
desired_format = 0;
i++;
} else {
if (capabilities & V4L2_CAP_STREAMING) {
s->io_method = io_mmap;
res = mmap_init(s);
- res = mmap_start(s);
+ if (res == 0) {
+ res = mmap_start(s);
+ }
} else {
s->io_method = io_read;
res = read_init(s);
return 0;
}
-static AVInputFormat v4l2_format = {
+AVInputFormat v4l2_demuxer = {
"video4linux2",
"video grab",
sizeof(struct video_data),
v4l2_read_close,
.flags = AVFMT_NOFILE,
};
-
-int v4l2_init(void)
-{
- av_register_input_format(&v4l2_format);
- return 0;
-}