[FFmpeg-devel] [PATCH] avfilter/cropdetect: add option for initial skip

Gyan Doshi ffmpeg at gyani.pro
Tue Dec 8 15:28:47 EET 2020


The cropdetect filter, at present, skips the first two frames. This
behaviour is hardcoded.

New option 'skip' allows users to change this. Convenient for when
input is a single image or a trimmed video stream.

Default is kept at 2 to preserve current behaviour.
---
 doc/filters.texi            |  4 ++++
 libavfilter/vf_cropdetect.c | 10 ++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 62d6e26a02..d9f606604e 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -8899,6 +8899,10 @@ The value which the width/height should be divisible by. It defaults to
 get only even dimensions (needed for 4:2:2 video). 16 is best when
 encoding to most video codecs.
 
+ at item skip
+Set the number of initial frames for which evaluation is skipped.
+Default is 2. Range is 0 to INT_MAX.
+
 @item reset_count, reset
 Set the counter that determines after how many frames cropdetect will
 reset the previously detected largest video area and start over to
diff --git a/libavfilter/vf_cropdetect.c b/libavfilter/vf_cropdetect.c
index 7c7d0b953a..5ae87cad2d 100644
--- a/libavfilter/vf_cropdetect.c
+++ b/libavfilter/vf_cropdetect.c
@@ -37,6 +37,7 @@ typedef struct CropDetectContext {
     int x1, y1, x2, y2;
     float limit;
     int round;
+    int skip;
     int reset_count;
     int frame_nb;
     int max_pixsteps[4];
@@ -127,10 +128,10 @@ static av_cold int init(AVFilterContext *ctx)
 {
     CropDetectContext *s = ctx->priv;
 
-    s->frame_nb = -2;
+    s->frame_nb = -1 * s->skip;
 
-    av_log(ctx, AV_LOG_VERBOSE, "limit:%f round:%d reset_count:%d\n",
-           s->limit, s->round, s->reset_count);
+    av_log(ctx, AV_LOG_VERBOSE, "limit:%f round:%d skip:%d reset_count:%d\n",
+           s->limit, s->round, s->skip, s->reset_count);
 
     return 0;
 }
@@ -167,7 +168,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     int outliers, last_y;
     int limit = lrint(s->limit);
 
-    // ignore first 2 frames - they may be empty
+    // ignore first s->skip frames
     if (++s->frame_nb > 0) {
         metadata = &frame->metadata;
 
@@ -247,6 +248,7 @@ static const AVOption cropdetect_options[] = {
     { "limit", "Threshold below which the pixel is considered black", OFFSET(limit),       AV_OPT_TYPE_FLOAT, { .dbl = 24.0/255 }, 0, 65535, FLAGS },
     { "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 },
+    { "skip",  "Number of initial frames to skip",                    OFFSET(skip),        AV_OPT_TYPE_INT, { .i64 = 2 },  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 }
-- 
2.27.0



More information about the ffmpeg-devel mailing list