diff --git a/libavdevice/v4l.c b/libavdevice/v4l.c
index 39844a913cf8867cfa8b73470deef7097e86bab3..c2c67d0516aaece52bc0c5e2ce3a566f8e1628c3 100644
--- a/libavdevice/v4l.c
+++ b/libavdevice/v4l.c
@@ -84,11 +84,6 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     }
     s->time_base = ap->time_base;
 
-    if((unsigned)ap->width > 32767 || (unsigned)ap->height > 32767) {
-        av_log(s1, AV_LOG_ERROR, "Capture size is out of range: %dx%d\n",
-            ap->width, ap->height);
-        return -1;
-    }
     s->video_win.width = ap->width;
     s->video_win.height = ap->height;
 
@@ -121,6 +116,9 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
         }
     }
 
+    if(avcodec_check_dimensions(s1, s->video_win.width, s->video_win.height) < 0)
+        return -1;
+
     desired_palette = -1;
     desired_depth = -1;
     for (j = 0; j < vformat_num; j++) {
diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index 6819d9cc9f7420f94729bc1103fe1b41b82d70f3..d4eb7c874acb00c4d3b66f32328f799b4e49b2a1 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -526,11 +526,8 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     frame_rate = ap->time_base.den;
     frame_rate_base = ap->time_base.num;
 
-    if((unsigned)width > 32767 || (unsigned)height > 32767) {
-        av_log(s1, AV_LOG_ERROR, "Wrong size (%dx%d)\n", width, height);
-
+    if(avcodec_check_dimensions(s1, ap->width, ap->height) < 0)
         return -1;
-    }
 
     st = av_new_stream(s1, 0);
     if (!st) {