This is an automated email from the git hooks/post-receive script.

Git pushed a commit to branch release/9.1
in repository ffmpeg.

commit 54e7afd15ca6dbad239148b53bafbd790f2f2d32
Author:     Jun Zhao <[email protected]>
AuthorDate: Sat Nov 14 00:34:24 2026 +0800
Commit:     Michael Niedermayer <[email protected]>
CommitDate: Thu Nov 19 14:56:30 2026 +0100

    avfilter/vf_median: move 16-bit per-row histograms off the stack
    
    filter_plane() declares coarse[BINS], fine[BINS][BINS] and luc[BINS] as
    on-stack arrays reset on each row.  At DEPTH=16 (BINS=256) fine alone is
    128 KiB, so a 16-bit median run overflows musl's 128 KiB default thread
    stack; the 8-bit FATE test does not exercise this instantiation.
    
    For 16-bit input, allocate the three row arrays once per thread in
    config_input(), indexed by jobnr like the existing global histograms,
    and free them in uninit().  Retain the on-stack arrays at lower bit
    depths (at most ~33 KiB at DEPTH=14) so the common 8-bit path avoids
    pointer-aliasing overhead and stays unchanged.  Explicit memsets
    preserve the per-row zeroing for DEPTH=16 and the filter body is
    unchanged, keeping output bit-exact across all depths.
    
    Suggested-by: Forgejo_Fairy
    Signed-off-by: Jun Zhao <[email protected]>
    
    (cherry picked from commit 466465f9c48edbd6c8acb034b92b7b6df972bde7)
    Signed-off-by: Michael Niedermayer <[email protected]>
---
 libavfilter/median.h          |  4 ++++
 libavfilter/median_template.c | 11 +++++++++++
 libavfilter/vf_median.c       | 32 +++++++++++++++++++++++++++++---
 3 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/libavfilter/median.h b/libavfilter/median.h
index 672607caa8..f5c6eee46f 100644
--- a/libavfilter/median.h
+++ b/libavfilter/median.h
@@ -44,6 +44,10 @@ typedef struct MedianContext {
     int bins;
     int t;
 
+    /* Per-thread row histograms for 16-bit input, replacing the ~129 KiB
+     * on-stack arrays in median_template.c. */
+    uint16_t **row_coarse, **row_fine, **row_luc;
+
     void (*hadd)(uint16_t *dst, const uint16_t *src, int bins);
     void (*hsub)(uint16_t *dst, const uint16_t *src, int bins);
     void (*hmuladd)(uint16_t *dst, const uint16_t *src, int f, int bins);
diff --git a/libavfilter/median_template.c b/libavfilter/median_template.c
index 82c843a924..e18dd12698 100644
--- a/libavfilter/median_template.c
+++ b/libavfilter/median_template.c
@@ -61,6 +61,11 @@ static void fn(filter_plane)(AVFilterContext *ctx, const 
uint8_t *ssrc, int src_
     pixel *dst = (pixel *)ddst;
     const pixel *srcp;
     const pixel *p;
+#if DEPTH == 16
+    htype *coarse = s->row_coarse[jobnr];
+    htype (*fine)[BINS] = (htype (*)[BINS])s->row_fine[jobnr];
+    htype *luc = s->row_luc[jobnr];
+#endif
 
     src_linesize /= sizeof(pixel);
     dst_linesize /= sizeof(pixel);
@@ -88,9 +93,15 @@ static void fn(filter_plane)(AVFilterContext *ctx, const 
uint8_t *ssrc, int src_
     srcp = src;
 
     for (int i = slice_h_start; i < slice_h_end; i++) {
+#if DEPTH == 16
+        memset(coarse, 0, BINS * sizeof(*coarse));
+        memset(fine, 0, BINS * BINS * sizeof(**fine));
+        memset(luc, 0, BINS * sizeof(*luc));
+#else
         htype coarse[BINS] = { 0 };
         htype fine[BINS][BINS] = { { 0 } };
         htype luc[BINS] = { 0 };
+#endif
 
         p = srcp + src_linesize * FFMAX(0, i - radiusV - 1);
         for (int j = 0; j < width; j++) {
diff --git a/libavfilter/vf_median.c b/libavfilter/vf_median.c
index aeb5d09e3a..62535b7b0f 100644
--- a/libavfilter/vf_median.c
+++ b/libavfilter/vf_median.c
@@ -161,6 +161,21 @@ static int config_input(AVFilterLink *inlink)
             return AVERROR(ENOMEM);
     }
 
+    if (s->depth == 16) {
+        s->row_coarse = av_calloc(s->nb_threads, sizeof(*s->row_coarse));
+        s->row_fine   = av_calloc(s->nb_threads, sizeof(*s->row_fine));
+        s->row_luc    = av_calloc(s->nb_threads, sizeof(*s->row_luc));
+        if (!s->row_coarse || !s->row_fine || !s->row_luc)
+            return AVERROR(ENOMEM);
+        for (int i = 0; i < s->nb_threads; i++) {
+            s->row_coarse[i] = av_malloc_array(s->bins, 
sizeof(**s->row_coarse));
+            s->row_fine[i]   = av_malloc_array(s->bins * s->bins, 
sizeof(**s->row_fine));
+            s->row_luc[i]    = av_malloc_array(s->bins, sizeof(**s->row_luc));
+            if (!s->row_coarse[i] || !s->row_fine[i] || !s->row_luc[i])
+                return AVERROR(ENOMEM);
+        }
+    }
+
     s->hadd = hadd;
     s->hsub = hsub;
     s->hmuladd = hmuladd;
@@ -241,13 +256,24 @@ static av_cold void uninit(AVFilterContext *ctx)
 {
     MedianContext *s = ctx->priv;
 
-    for (int i = 0; i < s->nb_threads && s->coarse && s->fine; i++) {
-        av_freep(&s->coarse[i]);
-        av_freep(&s->fine[i]);
+    for (int i = 0; i < s->nb_threads; i++) {
+        if (s->coarse)
+            av_freep(&s->coarse[i]);
+        if (s->fine)
+            av_freep(&s->fine[i]);
+        if (s->row_coarse)
+            av_freep(&s->row_coarse[i]);
+        if (s->row_fine)
+            av_freep(&s->row_fine[i]);
+        if (s->row_luc)
+            av_freep(&s->row_luc[i]);
     }
 
     av_freep(&s->coarse);
     av_freep(&s->fine);
+    av_freep(&s->row_coarse);
+    av_freep(&s->row_fine);
+    av_freep(&s->row_luc);
 }
 
 static int process_command(AVFilterContext *ctx, const char *cmd, const char 
*args,

-- 
To stop receiving notification emails like this one, please contact
[email protected].
_______________________________________________
ffmpeg-cvslog mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to