Index: libmpcodecs/vf_cropdetect.c
===================================================================
--- libmpcodecs/vf_cropdetect.c	(revision 30684)
+++ libmpcodecs/vf_cropdetect.c	(working copy)
@@ -75,7 +75,7 @@
 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
     mp_image_t *dmpi;
     int bpp=mpi->bpp/8;
-    int w,h,x,y,shrink_by;
+    int w,h,x,y,x2,y2,shrink_by;
 
     // hope we'll get DR buffer:
     dmpi=vf_get_image(vf->next,mpi->imgfmt,
@@ -92,6 +92,7 @@
     dmpi->height=mpi->height;
 
 if(++vf->priv->fno>0){	// ignore first 2 frames - they may be empty
+    int round_sig = 1;
 
     // Reset the crop area every reset_count frames, if reset_count is > 0
     if(vf->priv->reset_count > 0 && vf->priv->fno > vf->priv->reset_count){
@@ -102,8 +103,11 @@
 	vf->priv->fno=1;
     }
 
-    for(y=0;y<vf->priv->y1;y++){
-	if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
+    // Do not check line 0 - CC information might confuse the result
+    for(y=1;y<vf->priv->y1;y++){
+       if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
+            if(y == 1) // If line 1 is not empty assume line 0 is used as well
+                y = 0;
 	    vf->priv->y1=y;
 	    break;
 	}
@@ -130,29 +134,82 @@
 	}
     }
 
-    // round x and y (up), important for yuv colorspaces
-    // make sure they stay rounded!
-    x=(vf->priv->x1+1)&(~1);
-    y=(vf->priv->y1+1)&(~1);
+    if (vf->priv->round < 0)
+	round_sig = -1;
 
-    w = vf->priv->x2 - x + 1;
-    h = vf->priv->y2 - y + 1;
+    x = vf->priv->x1;
+    y = vf->priv->y1;
+    x2 = vf->priv->x2;
+    y2 = vf->priv->y2;
 
-    // w and h must be divisible by 2 as well because of yuv
-    // colorspace problems.
-    if (vf->priv->round <= 1)
-      vf->priv->round = 16;
-    if (vf->priv->round % 2)
-      vf->priv->round *= 2;
+    // round x and y, important for yuv colorspaces
+    if (x%2)
+        x += round_sig;
+    if (y%2)
+        y += round_sig;
+    // and x2 and y2 are rounded to an odd number (this ensures
+    // w and h being even numbers
+    if (x2%2 == 0)
+        x += round_sig;
+    if (y2%2 == 0)
+        y += round_sig;
 
-    shrink_by = w % vf->priv->round;
+    w = x2 - x + 1;
+    h = y2 - y + 1;
+
+    // w and h must be divisible by 2 because of yuv colorspace problems.
+    // This is always true as round is even number != 0
+
+    // Modulus with negative divisor is implementation-defined, avoid
+    // problems.
+    shrink_by = w % abs(vf->priv->round);
+    if (shrink_by && vf->priv->round < 0)
+	shrink_by += vf->priv->round;
+
     w -= shrink_by;
-    x += (shrink_by / 2 + 1) & ~1;
+    if (w>mpi->w) {
+        printf("\nBUG w: %d  w0: %d\n",w,mpi->w);
+        w = mpi->w;
+    }
+    if (w<0) {
+        printf("\nBUG w: %d  w0: %d\n",w,mpi->w);
+        w = 0;
+    }
+    x += shrink_by / 2;
+    // Necessary, x can go negative for shrink_by < 0
+    if (x<0)
+        x = 0;
+    // Sanity check
+    if (x+w>mpi->w) {
+        x = mpi->w-w;
+    }
 
-    shrink_by = h % vf->priv->round;
+    shrink_by = h % abs(vf->priv->round);
+    if (shrink_by && vf->priv->round < 0)
+	shrink_by += vf->priv->round;
+
     h -= shrink_by;
-    y += (shrink_by / 2 + 1) & ~1;
+    if (h>mpi->h) {
+        printf("\nBUG h: %d  h0: %d\n",h,mpi->h);
+        h = mpi->h;
+    }
+    if (h<0) {
+        printf("\nBUG h: %d  h0: %d\n",h,mpi->h);
+        h = 0;
+    }
+    y += shrink_by / 2;
+    // Necessary, y can go negative for shrink_by < 0
+    if (y<0)
+        y = 0;
+    // Sanity check
+    if (y+h>mpi->h) {
+        y = mpi->h-h;
+    }
 
+    // make sure x and y stay rounded to even number
+    x = (x%2 ? x+round_sig : x);
+    y = (y%2 ? y+round_sig : y);
+
     mp_msg(MSGT_VFILTER, MSGL_INFO, MSGTR_MPCODECS_CropArea,
 	vf->priv->x1,vf->priv->x2,
 	vf->priv->y1,vf->priv->y2,
@@ -175,6 +232,7 @@
 //===========================================================================//
 
 static int vf_open(vf_instance_t *vf, char *args){
+    int round_sig = 1;
     vf->config=config;
     vf->put_image=put_image;
     vf->query_format=query_format;
@@ -186,6 +244,15 @@
     &vf->priv->limit,
     &vf->priv->round,
     &vf->priv->reset_count);
+
+    if (vf->priv->round < 0)
+	round_sig = -1;
+
+    if (abs(vf->priv->round) <= 1)
+      vf->priv->round = 16*round_sig;
+    if (vf->priv->round % 2)
+	vf->priv->round += round_sig; // Increase by one, keep sign
+
     return 1;
 }
 
