[FFmpeg-devel] [PATCH 2/2] avfilter/vf_cropdetect: add max_outliers parameter

Michael Niedermayer michaelni at gmx.at
Sat Dec 27 04:35:22 CET 2014


Fixes Ticket3030

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
---
 libavfilter/vf_cropdetect.c |   23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/libavfilter/vf_cropdetect.c b/libavfilter/vf_cropdetect.c
index fd2286d..8d45491 100644
--- a/libavfilter/vf_cropdetect.c
+++ b/libavfilter/vf_cropdetect.c
@@ -40,6 +40,7 @@ typedef struct CropDetectContext {
     int reset_count;
     int frame_nb;
     int max_pixsteps[4];
+    int max_outliers;
 } CropDetectContext;
 
 static int query_formats(AVFilterContext *ctx)
@@ -122,6 +123,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     int bpp = s->max_pixsteps[0];
     int w, h, x, y, shrink_by;
     AVDictionary **metadata;
+    int outliers, last_y;
 
     // ignore first 2 frames - they may be empty
     if (++s->frame_nb > 0) {
@@ -137,17 +139,21 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
         }
 
 #define CHECK(DST, FROM, NOEND, INC, STEP0, STEP1, LEN) \
-        for (y = FROM; NOEND; INC) {\
+        outliers = 0;\
+        for (last_y = y = FROM; NOEND; y = y INC) {\
             if (checkline(ctx, frame->data[0] + STEP0 * y, STEP1, LEN, bpp) > s->limit) {\
-                DST = y;\
-                break;\
-            }\
+                if (++outliers > s->max_outliers) { \
+                    DST = last_y;\
+                    break;\
+                }\
+            } else\
+                last_y = y INC;\
         }
 
-        CHECK(s->y1,                 0,               y < s->y1, y++, frame->linesize[0], bpp, frame->width);
-        CHECK(s->y2, frame->height - 1, y > FFMAX(s->y2, s->y1), y--, frame->linesize[0], bpp, frame->width);
-        CHECK(s->x1,                 0,               y < s->x1, y++, bpp, frame->linesize[0], frame->height);
-        CHECK(s->x2,  frame->width - 1, y > FFMAX(s->x2, s->x1), y--, bpp, frame->linesize[0], frame->height);
+        CHECK(s->y1,                 0,               y < s->y1, +1, frame->linesize[0], bpp, frame->width);
+        CHECK(s->y2, frame->height - 1, y > FFMAX(s->y2, s->y1), -1, frame->linesize[0], bpp, frame->width);
+        CHECK(s->x1,                 0,               y < s->x1, +1, bpp, frame->linesize[0], frame->height);
+        CHECK(s->x2,  frame->width - 1, y > FFMAX(s->x2, s->x1), -1, bpp, frame->linesize[0], frame->height);
 
 
         // round x and y (up), important for yuv colorspaces
@@ -200,6 +206,7 @@ static const AVOption cropdetect_options[] = {
     { "round", "Value by which the width/height should be divisible", OFFSET(round),       AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
     { "reset", "Recalculate the crop area after this many frames",    OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 },  0, INT_MAX, FLAGS },
     { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 },  0, INT_MAX, FLAGS },
+    { "max_outliers", "Threshold count of outliers",                  OFFSET(max_outliers),AV_OPT_TYPE_INT, { .i64 = 0 },  0, INT_MAX, FLAGS },
     { NULL }
 };
 
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list