PR #26040 opened by Jun Zhao (mypopydev)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26040
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26040.patch

This series eliminates the remaining library stack frames exceeding the 120 KiB
 `-Wstack-usage` / `-Wframe-larger-than` threshold (chosen for musl's 128 KiB 
default thread stacks) without changing bitstream or filter output: 

1. moves VVC's 320 KiB RPR scaled-MC scratch array into per-thread 
`VVCLocalContext`
2. moves `ffv1enc`'s ~152 KiB 32-bit float remap tables into an on-demand 
per-slice allocation
3.  moves `vf_median`'s per-row histograms (up to ~129 KiB at 16-bit) into 
per-thread context buffers
4. adds an endian-independent `fate-filter-median16` test to cover the 
previously untested 16-bit median path.


>From 22653a95ce9fe6f28d53d9a699188d2b50644c04 Mon Sep 17 00:00:00 2001
From: Jun Zhao <[email protected]>
Date: Sat, 14 Nov 2026 00:14:22 +0800
Subject: [PATCH 1/4] avcodec/vvc: move scaled MC scratch buffer off the stack

put_scaled()/put_uni_w_scaled() keep a 320 KiB int16_t tmp_array on the
stack; instantiated for 8/10/12-bit and six wrappers this gives 18
functions a ~320 KiB frame, overflowing musl's 128 KiB default thread
stack whenever a scaled reference picture (RPR) is decoded.

Pass the buffer through the DSP pointers from a new per-thread
VVCLocalContext member instead.  The executor allocates one local
context per thread, so this is race-free and allocation-free on the
hot path; the scaled DSP functions are C-only, so SIMD dispatch is
untouched.  The new buffer is exactly the original size (TMP_STRIDE ==
EDGE_EMU_BUFFER_STRIDE) and is fully rewritten on every call, so
sharing it is safe, and the interpolation itself is unchanged.  The
cost is one extra 320 KiB persistent heap buffer per decoder thread,
paid by all VVC streams rather than only RPR ones.

Signed-off-by: Jun Zhao <[email protected]>
---
 libavcodec/vvc/ctu.h            |  1 +
 libavcodec/vvc/dsp.h            |  6 +++---
 libavcodec/vvc/inter.c          |  6 +++---
 libavcodec/vvc/inter_template.c | 36 ++++++++++++++++-----------------
 4 files changed, 25 insertions(+), 24 deletions(-)

diff --git a/libavcodec/vvc/ctu.h b/libavcodec/vvc/ctu.h
index 59366a17569..d089fa4d8b9 100644
--- a/libavcodec/vvc/ctu.h
+++ b/libavcodec/vvc/ctu.h
@@ -443,6 +443,7 @@ typedef struct VVCLocalContext {
         struct {
             /* *2 for high bit depths */
             DECLARE_ALIGNED(32, uint8_t, 
edge_emu_buffer)[EDGE_EMU_BUFFER_STRIDE * EDGE_EMU_BUFFER_STRIDE * 2];
+            DECLARE_ALIGNED(32, int16_t, scaled_tmp)[EDGE_EMU_BUFFER_STRIDE * 
MAX_PB_SIZE];
             DECLARE_ALIGNED(32, int16_t, tmp)[MAX_PB_SIZE * MAX_PB_SIZE];
             DECLARE_ALIGNED(32, int16_t, tmp1)[MAX_PB_SIZE * MAX_PB_SIZE];
             union {
diff --git a/libavcodec/vvc/dsp.h b/libavcodec/vvc/dsp.h
index 29cdd7e8f49..ddad6edbc2e 100644
--- a/libavcodec/vvc/dsp.h
+++ b/libavcodec/vvc/dsp.h
@@ -59,16 +59,16 @@ typedef struct VVCInterDSPContext {
 
     void (*put_scaled[2 /* luma, chroma */][7 /* log2(width) - 1 */])(
         int16_t *dst, const uint8_t *src, ptrdiff_t src_stride, int src_height,
-        int x, int y, int dx, int dy, int height, const int8_t *hf, const 
int8_t *vf, int width);
+        int x, int y, int dx, int dy, int height, const int8_t *hf, const 
int8_t *vf, int width, int16_t *tmp);
 
     void (*put_uni_scaled[2 /* luma, chroma */][7 /* log2(width) - 1 */])(
         uint8_t *dst, const ptrdiff_t dst_stride, const uint8_t *src, 
ptrdiff_t src_stride, int src_height,
-        int x, int y, int dx, int dy, int height, const int8_t *hf, const 
int8_t *vf, int width);
+        int x, int y, int dx, int dy, int height, const int8_t *hf, const 
int8_t *vf, int width, int16_t *tmp);
 
     void (*put_uni_w_scaled[2 /* luma, chroma */][7 /* log2(width) - 1 */])(
         uint8_t *dst, const ptrdiff_t dst_stride, const uint8_t *src, 
ptrdiff_t src_stride, int src_height,
         int x, int y, int dx, int dy, int height, int denom, int wx, int ox, 
const int8_t *hf, const int8_t *vf,
-        int width);
+        int width, int16_t *tmp);
 
     void (*avg)(uint8_t *dst, ptrdiff_t dst_stride,
         const int16_t *src0, const int16_t *src1, int width, int height);
diff --git a/libavcodec/vvc/inter.c b/libavcodec/vvc/inter.c
index fd2508dbb12..c2d28282974 100644
--- a/libavcodec/vvc/inter.c
+++ b/libavcodec/vvc/inter.c
@@ -435,7 +435,7 @@ static void mc_scaled(VVCLocalContext *lc, int16_t *dst, 
const VVCRefPic *refp,
 
     scaled_ref_pos_and_step(lc, refp, mv, x_off, y_off, c_idx, &x, &y, &dx, 
&dy);
     emulated_edge_scaled(lc, &src, &src_stride, &src_height, refp->ref, x, y, 
dx, dy, block_w, block_h, is_chroma);
-    fc->vvcdsp.inter.put_scaled[is_chroma][idx](dst, src, src_stride, 
src_height, x, y, dx, dy, block_h, hf, vf, block_w);
+    fc->vvcdsp.inter.put_scaled[is_chroma][idx](dst, src, src_stride, 
src_height, x, y, dx, dy, block_h, hf, vf, block_w, lc->pred.scaled_tmp);
 }
 
 static void mc_uni_scaled(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t 
dst_stride, const VVCRefPic *refp,
@@ -459,10 +459,10 @@ static void mc_uni_scaled(VVCLocalContext *lc, uint8_t 
*dst, const ptrdiff_t dst
 
     if (derive_weight_uni(&denom, &wx, &ox, lc, mvf, c_idx)) {
         fc->vvcdsp.inter.put_uni_w_scaled[is_chroma][idx](dst, dst_stride, 
src, src_stride, src_height,
-            x, y, dx, dy, block_h, denom, wx, ox, hf, vf, block_w);
+            x, y, dx, dy, block_h, denom, wx, ox, hf, vf, block_w, 
lc->pred.scaled_tmp);
     } else {
         fc->vvcdsp.inter.put_uni_scaled[is_chroma][idx](dst, dst_stride, src, 
src_stride, src_height,
-            x, y, dx, dy, block_h, hf, vf, block_w);
+            x, y, dx, dy, block_h, hf, vf, block_w, lc->pred.scaled_tmp);
     }
 }
 
diff --git a/libavcodec/vvc/inter_template.c b/libavcodec/vvc/inter_template.c
index f78a3946442..e6d822c56b3 100644
--- a/libavcodec/vvc/inter_template.c
+++ b/libavcodec/vvc/inter_template.c
@@ -24,12 +24,12 @@
 #include "libavutil/imgutils.h"
 
 #define TMP_STRIDE EDGE_EMU_BUFFER_STRIDE
-static void av_always_inline FUNC(put_scaled)(uint8_t *_dst, const ptrdiff_t 
_dst_stride,
+static void av_always_inline FUNC(put_scaled)(uint8_t *restrict _dst, const 
ptrdiff_t _dst_stride,
     const uint8_t *const _src, ptrdiff_t _src_stride, const int src_height,
     const int _x, const int _y, const int dx, const int dy,
-    const int height, const int8_t *hf, const int8_t *vf, const int width, 
const int is_uni, const int is_chroma)
+    const int height, const int8_t *hf, const int8_t *vf, const int width, 
const int is_uni,
+    const int is_chroma, int16_t *restrict tmp_array)
 {
-    int16_t tmp_array[TMP_STRIDE * MAX_PB_SIZE];
     int16_t *tmp                 = tmp_array;
     pixel *dst                   = (pixel*)_dst;
     int16_t *dst16               = (int16_t*)_dst;
@@ -84,41 +84,41 @@ static void av_always_inline FUNC(put_scaled)(uint8_t 
*_dst, const ptrdiff_t _ds
 static void FUNC(put_luma_scaled)(int16_t *_dst,
     const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
     const int x, const int y, const int dx, const int dy,
-    const int height, const int8_t *hf, const int8_t *vf, const int width)
+    const int height, const int8_t *hf, const int8_t *vf, const int width, 
int16_t *tmp)
 {
-    FUNC(put_scaled)((uint8_t *)_dst, MAX_PB_SIZE * sizeof(pixel), _src, 
_src_stride, src_height, x, y, dx, dy, height, hf, vf, width, 0, 0);
+    FUNC(put_scaled)((uint8_t *)_dst, MAX_PB_SIZE * sizeof(pixel), _src, 
_src_stride, src_height, x, y, dx, dy, height, hf, vf, width, 0, 0, tmp);
 }
 
 static void FUNC(put_chroma_scaled)(int16_t *_dst,
     const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
     const int x, const int y, const int dx, const int dy,
-    const int height, const int8_t *hf, const int8_t *vf, const int width)
+    const int height, const int8_t *hf, const int8_t *vf, const int width, 
int16_t *tmp)
 {
-    FUNC(put_scaled)((uint8_t *)_dst, MAX_PB_SIZE * sizeof(pixel), _src, 
_src_stride, src_height, x, y, dx, dy, height, hf, vf, width, 0, 1);
+    FUNC(put_scaled)((uint8_t *)_dst, MAX_PB_SIZE * sizeof(pixel), _src, 
_src_stride, src_height, x, y, dx, dy, height, hf, vf, width, 0, 1, tmp);
 }
 
 static void FUNC(put_uni_luma_scaled)(uint8_t *_dst, const ptrdiff_t 
_dst_stride,
     const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
     const int x, const int y, const int dx, const int dy,
-    const int height, const int8_t *hf, const int8_t *vf, const int width)
+    const int height, const int8_t *hf, const int8_t *vf, const int width, 
int16_t *tmp)
 {
-    FUNC(put_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, x, y, 
dx, dy, height, hf, vf, width, 1, 0);
+    FUNC(put_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, x, y, 
dx, dy, height, hf, vf, width, 1, 0, tmp);
 }
 
 static void FUNC(put_uni_chroma_scaled)(uint8_t *_dst, const ptrdiff_t 
_dst_stride,
     const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
     const int x, const int y, const int dx, const int dy,
-    const int height, const int8_t *hf, const int8_t *vf, const int width)
+    const int height, const int8_t *hf, const int8_t *vf, const int width, 
int16_t *tmp)
 {
-    FUNC(put_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, x, y, 
dx, dy, height, hf, vf, width, 1, 1);
+    FUNC(put_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, x, y, 
dx, dy, height, hf, vf, width, 1, 1, tmp);
 }
 
-static void av_always_inline FUNC(put_uni_w_scaled)(uint8_t *_dst, const 
ptrdiff_t _dst_stride,
+static void av_always_inline FUNC(put_uni_w_scaled)(uint8_t *restrict _dst, 
const ptrdiff_t _dst_stride,
     const uint8_t *const _src, ptrdiff_t _src_stride, const int src_height,
     const int _x, const int _y, const int dx, const int dy, const int denom, 
const int wx, const int _ox,
-    const int height, const int8_t *hf, const int8_t *vf, const int width, 
const int is_chroma)
+    const int height, const int8_t *hf, const int8_t *vf, const int width,
+    const int is_chroma, int16_t *restrict tmp_array)
 {
-    int16_t tmp_array[TMP_STRIDE * MAX_PB_SIZE];
     int16_t *tmp                 = tmp_array;
     pixel *dst                   = (pixel*)_dst;
     const ptrdiff_t dst_stride   = _dst_stride / sizeof(pixel);
@@ -167,17 +167,17 @@ static void av_always_inline 
FUNC(put_uni_w_scaled)(uint8_t *_dst, const ptrdiff
 static void FUNC(put_uni_luma_w_scaled)(uint8_t *_dst, const ptrdiff_t 
_dst_stride,
     const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
     const int x, const int y, const int dx, const int dy, const int height,
-    const int denom, const int wx, const int ox, const int8_t *hf, const 
int8_t *vf, const int width)
+    const int denom, const int wx, const int ox, const int8_t *hf, const 
int8_t *vf, const int width, int16_t *tmp)
 {
-    FUNC(put_uni_w_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, 
x, y, dx, dy, denom, wx, ox, height, hf, vf, width, 0);
+    FUNC(put_uni_w_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, 
x, y, dx, dy, denom, wx, ox, height, hf, vf, width, 0, tmp);
 }
 
 static void FUNC(put_uni_chroma_w_scaled)(uint8_t *_dst, const ptrdiff_t 
_dst_stride,
     const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
     const int x, const int y, const int dx, const int dy, const int height,
-    const int denom, const int wx, const int ox, const int8_t *hf, const 
int8_t *vf, const int width)
+    const int denom, const int wx, const int ox, const int8_t *hf, const 
int8_t *vf, const int width, int16_t *tmp)
 {
-    FUNC(put_uni_w_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, 
x, y, dx, dy,  denom, wx, ox, height, hf, vf, width, 1);
+    FUNC(put_uni_w_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, 
x, y, dx, dy,  denom, wx, ox, height, hf, vf, width, 1, tmp);
 }
 
 #undef TMP_STRIDE
-- 
2.54.0


>From a788aac3feb7ff0409e03c46c6d0ea25e3c833d4 Mon Sep 17 00:00:00 2001
From: Jun Zhao <[email protected]>
Date: Sat, 14 Nov 2026 00:34:23 +0800
Subject: [PATCH 2/4] avcodec/ffv1enc: move 32-bit remap scratch buffers off
 the stack

encode_float32_remap() keeps mul_all[11][1025] and score_tab_all[1025][23]
on the stack, and encode_float32_remap_segment() adds mul[4097].  Inlined
into encode_slice() the chain peaks at ~159 KiB, overflowing musl's 128 KiB
default thread stack for gbrpf32/gbrapf32 remap encodes.

Move them into a per-slice FFV1RemapBuffers allocation made only when a
32-bit remap is configured, freed in ff_ffv1_close().  Each slice is
encoded by its own thread, so per-slice is race-free; the decoder shares
the slice contexts but never enters remap and just carries NULL.  A
per-thread pool would be smaller but execute() exposes no thread index,
so per-slice is the safe choice, bounded by MAX_SLICES (1024) * 152 KiB
(~152 MiB worst case, that configuration only).  score_tab_all's
per-iteration stack re-zeroing becomes an explicit memset; no arithmetic
changes, so output stays bit-exact.

Signed-off-by: Jun Zhao <[email protected]>
---
 libavcodec/ffv1.c    |  1 +
 libavcodec/ffv1.h    |  4 ++++
 libavcodec/ffv1enc.c | 31 ++++++++++++++++++++++++++-----
 3 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/libavcodec/ffv1.c b/libavcodec/ffv1.c
index 1dcb7be28d9..1cbac01052a 100644
--- a/libavcodec/ffv1.c
+++ b/libavcodec/ffv1.c
@@ -274,6 +274,7 @@ av_cold void ff_ffv1_close(FFV1Context *s)
 
         av_freep(&sc->sample_buffer);
         av_freep(&sc->sample_buffer32);
+        av_freep(&sc->remap_buf);
         for(int p = 0; p < 4 ; p++) {
             av_freep(&sc->fltmap[p]);
             av_freep(&sc->fltmap32[p]);
diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h
index 012b92ec212..5ea56b4b98e 100644
--- a/libavcodec/ffv1.h
+++ b/libavcodec/ffv1.h
@@ -70,6 +70,8 @@ typedef struct PlaneContext {
 
 #define MAX_SLICES 1024
 
+struct FFV1RemapBuffers;
+
 typedef struct FFV1SliceContext {
     int16_t *sample_buffer;
     int32_t *sample_buffer32;
@@ -117,6 +119,8 @@ typedef struct FFV1SliceContext {
         uint32_t val; //this is unneeded if you accept a dereference on each 
access
         uint32_t ndx;
     } *unit[4];
+
+    struct FFV1RemapBuffers *remap_buf; ///< encoder-only, allocated for 
32-bit float remap
 } FFV1SliceContext;
 
 typedef struct FFV1Context {
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index e03154b415d..db45f4f40c3 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -42,6 +42,19 @@
 #include "ffv1.h"
 #include "ffv1enc.h"
 
+#define REMAP_MAX_LOG2_MUL_COUNT 10
+#define REMAP_MAX_MUL_COUNT      (1 << REMAP_MAX_LOG2_MUL_COUNT)
+#define REMAP_MAX_LOG2_MUL       22
+#define FFV1_MAX_MUL_COUNT       4096
+
+/* Scratch buffers for 32-bit float remap, kept off the stack (each ~152 KiB).
+ * Allocated per slice only when a 32-bit remap is actually used. */
+struct FFV1RemapBuffers {
+    int   mul_all[REMAP_MAX_LOG2_MUL_COUNT + 1][REMAP_MAX_MUL_COUNT + 1];
+    float score_tab_all[REMAP_MAX_MUL_COUNT + 1][REMAP_MAX_LOG2_MUL + 1];
+    int   mul[FFV1_MAX_MUL_COUNT + 1];
+};
+
 static const int8_t quant5_10bit[256] = {
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,
      1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
@@ -1084,6 +1097,11 @@ static av_cold int encode_init_internal(AVCodecContext 
*avctx)
         }
         av_assert0(s->remap_mode >= 0);
         if (s->remap_mode) {
+            if (s->bits_per_raw_sample == 32) {
+                sc->remap_buf = av_mallocz(sizeof(*sc->remap_buf));
+                if (!sc->remap_buf)
+                    return AVERROR(ENOMEM);
+            }
             for (int p = 0; p < 1 + 2*s->chroma_planes + s->transparency ; 
p++) {
                 if (s->bits_per_raw_sample == 32) {
                     sc->unit[p] = av_malloc_array(sc->slice_width, 
sc->slice_height * sizeof(**sc->unit));
@@ -1431,7 +1449,7 @@ static int encode_float32_remap_segment(FFV1SliceContext 
*sc,
 {
     const int pixel_num = sc->slice_width * sc->slice_height;
     uint8_t state[2][3][32];
-    int mul[4096+1];
+    int *mul = sc->remap_buf->mul;
     RangeCoder rc = sc->c;
     int lu = 0;
     int run = 0;
@@ -1553,13 +1571,16 @@ static void encode_float32_remap(FFV1Context *f, 
FFV1SliceContext *sc,
     const int stair_mode          = ((int[]){  0,  0,  0,  1,  0,   
0})[f->remap_optimizer];
     const int magic_log2          = ((int[]){  1,  1,  1,  1,  0,   
0})[f->remap_optimizer];
 
+    av_assert0(max_log2_mul_count <= REMAP_MAX_LOG2_MUL_COUNT);
+    av_assert0(max_log2_mul       <= REMAP_MAX_LOG2_MUL);
+
     for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
         int best_log2_mul_count = 0;
-        float score_sum[11] = {0};
-        int mul_all[11][1025];
-
+        float score_sum[REMAP_MAX_LOG2_MUL_COUNT + 1] = {0};
+        int (*mul_all)[REMAP_MAX_MUL_COUNT + 1] = sc->remap_buf->mul_all;
+        float (*score_tab_all)[REMAP_MAX_LOG2_MUL + 1] = 
sc->remap_buf->score_tab_all;
         for (int log2_mul_count= 0; log2_mul_count <= max_log2_mul_count; 
log2_mul_count += log2_mul_count_step) {
-            float score_tab_all[1025][23] = {0};
+            memset(score_tab_all, 0, sizeof(sc->remap_buf->score_tab_all));
             int64_t last_val = -1;
             int *mul_tab = mul_all[log2_mul_count];
             int last_mul_index = -1;
-- 
2.54.0


>From 343ad71d2a624f3051aafd5aed88ff39eeabff5a Mon Sep 17 00:00:00 2001
From: Jun Zhao <[email protected]>
Date: Sat, 14 Nov 2026 00:34:24 +0800
Subject: [PATCH 3/4] avfilter/vf_median: move 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.

Allocate the three row arrays once per thread in config_input(), indexed
by jobnr like the existing global histograms, and free them in uninit().
s->bins equals the template's BINS, so the sizes match; explicit memsets
preserve the per-row zeroing and the filter body is unchanged, so output
stays bit-exact.  The global cumulative histograms are a different buffer
and remain untouched.

Signed-off-by: Jun Zhao <[email protected]>
---
 libavfilter/median.h          |  4 ++++
 libavfilter/median_template.c |  9 ++++++---
 libavfilter/vf_median.c       | 30 +++++++++++++++++++++++++++---
 3 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/libavfilter/median.h b/libavfilter/median.h
index 672607caa83..4851402f7de 100644
--- a/libavfilter/median.h
+++ b/libavfilter/median.h
@@ -44,6 +44,10 @@ typedef struct MedianContext {
     int bins;
     int t;
 
+    /* Per-thread row histograms, replacing the large on-stack arrays in
+     * median_template.c (each up to bins*bins*sizeof(uint16_t) bytes). */
+    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 6f719df57f3..179f24fc598 100644
--- a/libavfilter/median_template.c
+++ b/libavfilter/median_template.c
@@ -61,6 +61,9 @@ static void fn(filter_plane)(AVFilterContext *ctx, const 
uint8_t *ssrc, int src_
     pixel *dst = (pixel *)ddst;
     const pixel *srcp;
     const pixel *p;
+    htype *coarse = s->row_coarse[jobnr];
+    htype (*fine)[BINS] = (htype (*)[BINS])s->row_fine[jobnr];
+    htype *luc = s->row_luc[jobnr];
 
     src_linesize /= sizeof(pixel);
     dst_linesize /= sizeof(pixel);
@@ -88,9 +91,9 @@ 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++) {
-        htype coarse[BINS] = { 0 };
-        htype fine[BINS][BINS] = { { 0 } };
-        htype luc[BINS] = { 0 };
+        memset(coarse, 0, BINS * sizeof(*coarse));
+        memset(fine, 0, BINS * BINS * sizeof(**fine));
+        memset(luc, 0, BINS * sizeof(*luc));
 
         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 53989324362..6adc4c15846 100644
--- a/libavfilter/vf_median.c
+++ b/libavfilter/vf_median.c
@@ -161,6 +161,19 @@ static int config_input(AVFilterLink *inlink)
             return AVERROR(ENOMEM);
     }
 
+    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;
@@ -240,13 +253,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,
-- 
2.54.0


>From 0218a69ece070650f6dffc82cfddae98aae34a14 Mon Sep 17 00:00:00 2001
From: Jun Zhao <[email protected]>
Date: Sat, 14 Nov 2026 00:45:48 +0800
Subject: [PATCH 4/4] tests/fate: add a 16-bit median regression test

fate-filter-median uses the 8-bit vsynth source and never instantiates
the DEPTH=16 template, which had the ~128 KiB on-stack histogram and no
coverage at all.

Add fate-filter-median16, a self-contained -lavfi framecrc test on a
gray16 source (BINS=256).  Anchor both ends to gray16le through scale so
testsrc2 (little-endian only via drawutils) and rawvideo framecrc
checksums stay identical on big-endian targets while median itself still
runs on native-endian gray16.  Gate it with FILTERFRAMECRC on MEDIAN,
TESTSRC2, FORMAT and SCALE so reduced configurations skip it cleanly.

Signed-off-by: Jun Zhao <[email protected]>
---
 tests/fate/filter-video.mak    |  5 +++++
 tests/ref/fate/filter-median16 | 10 ++++++++++
 2 files changed, 15 insertions(+)
 create mode 100644 tests/ref/fate/filter-median16

diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
index 82ab48dc68d..4ce8e06c37c 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -686,6 +686,11 @@ fate-filter-edgedetect-colormix: CMD = video_filter 
"scale,format=gbrp,perms=ran
 FATE_FILTER_VSYNTH_VIDEO_FILTER-$(CONFIG_MEDIAN_FILTER) += fate-filter-median
 fate-filter-median: CMD = video_filter "median=radius=5"
 
+# Exercise the 16-bit median template (DEPTH=16, BINS=256), whose
+# histogram scratch used to be a ~132 KiB on-stack array.
+FATE_FILTER-$(call FILTERFRAMECRC, MEDIAN TESTSRC2 FORMAT SCALE) += 
fate-filter-median16
+fate-filter-median16: CMD = framecrc -lavfi 
"testsrc2=size=64x64:rate=5:duration=1,format=gray16le,scale,format=gray16,median=radius=3,scale"
 -pix_fmt gray16le -frames:v 5
+
 FATE_FILTER_VSYNTH_VIDEO_FILTER-$(call ALLYES, PERMS_FILTER HUE_FILTER) += 
fate-filter-hue1 fate-filter-hue2 fate-filter-hue3
 fate-filter-hue1: CMD = video_filter "perms=random,hue=s=sin(2*PI*t)+1" 
-frames:v 20
 fate-filter-hue2: CMD = video_filter "perms=random,hue=h=18*n" -frames:v 20
diff --git a/tests/ref/fate/filter-median16 b/tests/ref/fate/filter-median16
new file mode 100644
index 00000000000..9fd1a2f7642
--- /dev/null
+++ b/tests/ref/fate/filter-median16
@@ -0,0 +1,10 @@
+#tb 0: 1/5
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 64x64
+#sar 0: 1/1
+0,          0,          0,        1,     8192, 0x2d933fab
+0,          1,          1,        1,     8192, 0x6ecdbbb2
+0,          2,          2,        1,     8192, 0x4c0a2b92
+0,          3,          3,        1,     8192, 0x1b34f327
+0,          4,          4,        1,     8192, 0xe2af3482
-- 
2.54.0

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

Reply via email to