]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/v4l2.c
nut needs crc.o
[ffmpeg] / libavformat / v4l2.c
index 4cdfc26e5ec6f449a196e3a4a969efab76aa874c..009ca9fc95b6214c709611f8630940818f5626e1 100644 (file)
@@ -30,8 +30,8 @@
 #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;
@@ -126,17 +126,20 @@ static int device_open(const char *devname, uint32_t *capabilities)
     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;
     }
@@ -145,17 +148,25 @@ static int device_open(const char *devname, uint32_t *capabilities)
     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)
@@ -373,7 +384,7 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     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;
@@ -412,16 +423,16 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
 
         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 {
@@ -446,7 +457,9 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     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);
@@ -509,7 +522,7 @@ static int v4l2_read_close(AVFormatContext *s1)
     return 0;
 }
 
-static AVInputFormat v4l2_format = {
+AVInputFormat v4l2_demuxer = {
     "video4linux2",
     "video grab",
     sizeof(struct video_data),
@@ -519,9 +532,3 @@ static AVInputFormat v4l2_format = {
     v4l2_read_close,
     .flags = AVFMT_NOFILE,
 };
-
-int v4l2_init(void)
-{
-    av_register_input_format(&v4l2_format);
-    return 0;
-}