From 5281e57c62a13973c778c9296c98632de8bd5864 Mon Sep 17 00:00:00 2001
From: tue46wsdgxfjrt <jfbvxt@gmail.com>
Date: Tue, 9 Sep 2014 18:05:44 -0700
Subject: [PATCH] Avoid returning negative rectangles from cropdetect

---
 libavfilter/vf_cropdetect.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/libavfilter/vf_cropdetect.c b/libavfilter/vf_cropdetect.c
index 76aa7b2..70d7d66 100644
--- a/libavfilter/vf_cropdetect.c
+++ b/libavfilter/vf_cropdetect.c
@@ -120,7 +120,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     AVFilterContext *ctx = inlink->dst;
     CropDetectContext *s = ctx->priv;
     int bpp = s->max_pixsteps[0];
-    int w, h, x, y, shrink_by;
+    int w, h, x, y, x2, y2, shrink_by;
     AVDictionary **metadata;
 
     // ignore first 2 frames - they may be empty
@@ -168,9 +168,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
         // make sure they stay rounded!
         x = (s->x1+1) & ~1;
         y = (s->y1+1) & ~1;
+        x2 = FFMAX(x, (s->x2+1) & ~1);
+        y2 = FFMAX(y, (s->y2+1) & ~1);
 
-        w = s->x2 - x + 1;
-        h = s->y2 - y + 1;
+        w = x2 - x + 1;
+        h = y2 - y + 1;
 
         // w and h must be divisible by 2 as well because of yuv
         // colorspace problems.
-- 
1.8.5.2 (Apple Git-48)

