[PATCH 5/5] Implement v4l2 input size autodetection.

Stefano Sabatini stefano.sabatini-lala
Sat Apr 24 20:46:52 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 |   26 ++++++++++++++++++--------
 1 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index 4bf8d44..85b21a7 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -600,14 +600,6 @@ 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);
@@ -624,7 +616,25 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     }
     av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
 
+    if (!s->width && !s->height) {
+        struct v4l2_format fmt;
+
+        av_log(s1, AV_LOG_INFO, "Size value (%dx%d) unspecified, querying the device for the current settings\n",
+               s->width, s->height);
+        fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+        if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
+            log_errno(s1, "ioctl(VIDIOC_G_FMT)");
+            return AVERROR(errno);
+        }
+        s->width  = fmt.fmt.pix.width;
+        s->height = fmt.fmt.pix.height;
+        av_log(s1, AV_LOG_INFO, "Setting size to value %dx%d\n", s->width, s->height);
+    }
+
     desired_format = device_try_init(s1, ap, &s->width, &s->height, &codec_id);
+    if (avcodec_check_dimensions(s1, s->width, s->height) < 0)
+        return AVERROR(EINVAL);
+
     if (desired_format == 0) {
         av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
                "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, ap->pix_fmt);
-- 
1.7.0


--8GpibOaaTibBMecb--



More information about the ffmpeg-devel mailing list