PR #25924 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25924
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25924.patch

Split the per-row sums in `filter()` across slice jobs. Each job sums its share 
of the rows of every plane into per-job storage, and the caller adds up the 
partial sums. The sums are integers, so the totals, and therefore the output, 
do not depend on the number of jobs. The type decision, history, repeated field 
detection and metadata stay sequential.

**Output is unchanged.** `idet,metadata=mode=print` followed by `showinfo` was 
compared against master for `-filter_threads` 1, auto, 3 and 16, in default 
mode, with `half_life=3` and with `analyze_interlaced_flag=20`: 8-bit and 
10-bit input in 420/422/444, yuva420p and gray16, progressive, TFF and BFF 
interlaced, telecined, odd heights, and frames only 3 and 5 lines high. 
Per-frame metadata, frame flags and the summary lines are identical.

**Timing**, wall time for 120 s of 4096x2160 ProRes 422 (yuv422p10le, 23.976 
fps) on a c7g.4xlarge (Graviton3, 16 vCPU), `-vf idet -f null -`:

| | wall time |
|---|---|
| decode only | 15.8 s |
| idet, master | 47.2 s |
| idet, 1 filter thread | 47.5 s |
| idet, 4 filter threads | 18.9 s |
| idet, 8 filter threads | 19.5 s |
| idet, 16 filter threads | 18.3 s |

With 4 or more filter threads the decoder is the limit. On an Apple M3 Max, 
idet alone on 4096x2160 yuv422p10le (300 frames) goes from 1.21 s to 0.39 s 
with 4 threads and 0.32 s with 8.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From 9aa98fb12ad00fe198841209eea242fc63a7a994 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Tue, 10 Nov 2026 21:42:21 -0800
Subject: [PATCH] avfilter/vf_idet: add slice threading

Split the per-row sums in filter() across slice jobs. Each job sums
its share of the rows of every plane into per-job storage, and the
caller adds up the partial sums. The sums are integers, so the totals,
and therefore the output, do not depend on the number of jobs.

The type decision, history, repeated field detection and metadata
stay sequential.

4096x2160 yuv422p10le, 300 frames, Apple M3 Max, wall time:
 1 thread:  1.21 s
 4 threads: 0.39 s
 8 threads: 0.32 s
16 threads: 0.33 s

Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 libavfilter/vf_idet.c | 84 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 72 insertions(+), 12 deletions(-)

diff --git a/libavfilter/vf_idet.c b/libavfilter/vf_idet.c
index 02dc14fb7a1..1e4db2ed7aa 100644
--- a/libavfilter/vf_idet.c
+++ b/libavfilter/vf_idet.c
@@ -21,6 +21,7 @@
 #include <float.h> /* FLT_MAX */
 
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
@@ -40,6 +41,12 @@ typedef enum {
     REPEAT_BOTTOM,
 } RepeatedField;
 
+typedef struct IDETSums {
+    int64_t alpha[2];
+    int64_t delta;
+    int64_t gamma[2];
+} IDETSums;
+
 typedef struct IDETContext {
     const AVClass *class;
     IDETDSPContext dsp;
@@ -72,6 +79,9 @@ typedef struct IDETContext {
 
     const AVPixFmtDescriptor *csp;
     int eof;
+
+    int nb_threads;
+    IDETSums *sums;
 } IDETContext;
 
 #define OFFSET(x) offsetof(IDETContext, x)
@@ -132,30 +142,32 @@ static const char *rep2str(RepeatedField repeated_field)
     return NULL;
 }
 
-static void filter(AVFilterContext *ctx)
+static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int 
nb_jobs)
 {
     IDETContext *idet = ctx->priv;
-    int y, i;
-    int64_t alpha[2]={0};
-    int64_t delta=0;
-    int64_t gamma[2]={0};
-    Type type, best_type;
-    RepeatedField repeat;
-    int match = 0;
-    AVDictionary **metadata = &idet->cur->metadata;
+    IDETSums *sums = &idet->sums[jobnr];
+    int64_t alpha[2] = { 0 };
+    int64_t delta = 0;
+    int64_t gamma[2] = { 0 };
     ff_idet_filter_func filter_line = idet->dsp.filter_line;
 
-    for (i = 0; i < idet->csp->nb_components; i++) {
+    for (int i = 0; i < idet->csp->nb_components; i++) {
         int w = idet->cur->width;
         int h = idet->cur->height;
         int refs = idet->cur->linesize[i];
+        int rows, start, end;
 
         if (i && i<3) {
             w = AV_CEIL_RSHIFT(w, idet->csp->log2_chroma_w);
             h = AV_CEIL_RSHIFT(h, idet->csp->log2_chroma_h);
         }
 
-        for (y = 2; y < h - 2; y++) {
+        /* rows 2 to h - 3 are analyzed, split across the jobs */
+        rows  = FFMAX(h - 4, 0);
+        start = 2 + ff_slice_pos(rows, jobnr,     nb_jobs);
+        end   = 2 + ff_slice_pos(rows, jobnr + 1, nb_jobs);
+
+        for (int y = start; y < end; y++) {
             uint8_t *prev = &idet->prev->data[i][y*idet->prev->linesize[i]];
             uint8_t *cur  = &idet->cur ->data[i][y*refs];
             uint8_t *next = &idet->next->data[i][y*idet->next->linesize[i]];
@@ -166,6 +178,38 @@ static void filter(AVFilterContext *ctx)
         }
     }
 
+    sums->alpha[0] = alpha[0];
+    sums->alpha[1] = alpha[1];
+    sums->delta    = delta;
+    sums->gamma[0] = gamma[0];
+    sums->gamma[1] = gamma[1];
+
+    return 0;
+}
+
+static void filter(AVFilterContext *ctx)
+{
+    IDETContext *idet = ctx->priv;
+    int i;
+    int64_t alpha[2]={0};
+    int64_t delta=0;
+    int64_t gamma[2]={0};
+    Type type, best_type;
+    RepeatedField repeat;
+    int match = 0;
+    AVDictionary **metadata = &idet->cur->metadata;
+    const int nb_jobs = FFMIN(idet->cur->height, idet->nb_threads);
+
+    ff_filter_execute(ctx, filter_slice, NULL, NULL, nb_jobs);
+
+    for (i = 0; i < nb_jobs; i++) {
+        alpha[0] += idet->sums[i].alpha[0];
+        alpha[1] += idet->sums[i].alpha[1];
+        delta    += idet->sums[i].delta;
+        gamma[0] += idet->sums[i].gamma[0];
+        gamma[1] += idet->sums[i].gamma[1];
+    }
+
     if      (alpha[0] > idet->interlace_threshold * alpha[1]){
         type = TFF;
     }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
@@ -380,6 +424,7 @@ static av_cold void uninit(AVFilterContext *ctx)
     av_frame_free(&idet->prev);
     av_frame_free(&idet->cur );
     av_frame_free(&idet->next);
+    av_freep(&idet->sums);
 }
 
 static const enum AVPixelFormat pix_fmts[] = {
@@ -434,11 +479,26 @@ static av_cold int init(AVFilterContext *ctx)
     return 0;
 }
 
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    IDETContext *idet = ctx->priv;
+
+    idet->nb_threads = FFMAX(1, ff_filter_get_nb_threads(ctx));
+    av_freep(&idet->sums);
+    idet->sums = av_calloc(idet->nb_threads, sizeof(*idet->sums));
+    if (!idet->sums)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
 static const AVFilterPad idet_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
+        .config_props = config_input,
     },
 };
 
@@ -453,7 +513,7 @@ static const AVFilterPad idet_outputs[] = {
 const FFFilter ff_vf_idet = {
     .p.name        = "idet",
     .p.description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
-    .p.flags       = AVFILTER_FLAG_METADATA_ONLY,
+    .p.flags       = AVFILTER_FLAG_METADATA_ONLY | AVFILTER_FLAG_SLICE_THREADS,
     .p.priv_class  = &idet_class,
     .priv_size     = sizeof(IDETContext),
     .init          = init,
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to