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 7c27751f5baa97e869f95c1be915d360c539e3de
Author:     Jun Zhao <[email protected]>
AuthorDate: Sat Nov 14 00:34:23 2026 +0800
Commit:     Michael Niedermayer <[email protected]>
CommitDate: Thu Nov 19 14:56:30 2026 +0100

    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]>
    
    (cherry picked from commit 7121f5686e57b9eeefdcbb66f4b54997501d4e11)
    Signed-off-by: Michael Niedermayer <[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 1dcb7be28d..1cbac01052 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 012b92ec21..5ea56b4b98 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 1d18f10f05..3b47609e1a 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,
@@ -1072,6 +1085,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));
@@ -1399,7 +1417,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;
@@ -1521,13 +1539,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;

-- 
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