[FFmpeg-devel] [PATCH 6/6] Implement v4l2 input size autodetection.

Stefano Sabatini stefano.sabatini-lala
Sat Apr 24 20:53:25 CEST 2010


Move checks on size after the device is opened, and perform a
VIDIOC_G_FMT ioctl() on the device if WxH is set to 0x0.
---
 libavdevice/v4l2.c |   30 +++++++++++++++++++-----------
 1 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index e66ae91..f583d0e 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -591,23 +591,12 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     uint32_t desired_format, capabilities;
     enum CodecID codec_id;
 
-    if (ap->width <= 0 || ap->height <= 0) {
-        av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", ap->width, ap->height);
-        return AVERROR(EINVAL);
-    }
-
-    if(avcodec_check_dimensions(s1, ap->width, ap->height) < 0)
-        return AVERROR(EINVAL);
-
     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  = ap->width;
-    s->height = ap->height;
-
     capabilities = 0;
     s->fd = device_open(s1, &capabilities);
     if (s->fd < 0) {
@@ -615,6 +604,25 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     }
     av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
 
+    if (!ap->width && !ap->height) {
+        struct v4l2_format fmt;
+
+        av_log(s1, AV_LOG_INFO, "Size value unspecified, querying the device\n");
+        fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+        if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
+            IOCTL_ERROR(s1, VIDIOC_G_FMT);
+            return AVERROR(errno);
+        }
+        ap->width  = fmt.fmt.pix.width;
+        ap->height = fmt.fmt.pix.height;
+    }
+
+    if (avcodec_check_dimensions(s1, ap->width, ap->height) < 0)
+        return AVERROR(EINVAL);
+
+    s->width  = ap->width;
+    s->height = ap->height;
+
     desired_format = device_try_init(s1, ap, &s->width, &s->height, &codec_id);
     if (desired_format == 0) {
         av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
-- 
1.7.0




More information about the ffmpeg-devel mailing list