Commit 1f757d45fe for ffmpeg

commit 1f757d45fecc82c92770c5a397b9ce8d16c0f703
Author: marcos ashton <marcosashiglesias@gmail.com>
Date:   Wed Nov 18 15:09:40 2026 +0000

    avcodec/apedec: only round the filter output in 64 bits when interim mode is possible

    The reference decoder does that only in its interim versions, so since ed0001482a7 24-bit stereo files from current versions decode wrong when the sum wraps around; 24-bit mono can't tell the two apart and keeps the 64-bit rounding. Part of #24268.

diff --git a/libavcodec/apedec.c b/libavcodec/apedec.c
index fd6fa780d6..b7a0a120c5 100644
--- a/libavcodec/apedec.c
+++ b/libavcodec/apedec.c
@@ -201,7 +201,7 @@ typedef struct APEContext {
 } APEContext;

 static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
-                              int32_t *decoded1, int count);
+                              int32_t *decoded1, int count, int interim);

 static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode);
 static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode);
@@ -1143,7 +1143,7 @@ static void predictor_decode_stereo_3930(APEContext *ctx, int count)
     int32_t *decoded0 = ctx->decoded[0];
     int32_t *decoded1 = ctx->decoded[1];

-    ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
+    ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count, 0);

     while (count--) {
         /* Predictor Y */
@@ -1170,7 +1170,7 @@ static void predictor_decode_mono_3930(APEContext *ctx, int count)
     APEPredictor *p = &ctx->predictor;
     int32_t *decoded0 = ctx->decoded[0];

-    ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
+    ape_apply_filters(ctx, ctx->decoded[0], NULL, count, 0);

     while (count--) {
         *decoded0 = predictor_update_3930(p, *decoded0, 0, YDELAYA);
@@ -1249,7 +1249,9 @@ static void predictor_decode_stereo_3950(APEContext *ctx, int count)
     int lcount = count;
     int num_passes = 1;

-    ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
+    /* keep the 64-bit rounding while the mode is undecided */
+    ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count,
+                      ctx->interim_mode != 0);
     if (ctx->interim_mode == -1) {
         p_interim = *p_default;
         p_interim.buf = p_interim.historybuffer + (p_default->buf - p_default->historybuffer);
@@ -1320,7 +1322,9 @@ static void predictor_decode_mono_3950(APEContext *ctx, int count)
     int32_t *decoded0 = ctx->decoded[0];
     int32_t predictionA, currentA, A, sign;

-    ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
+    /* 24-bit mono never decides the mode, so keep the 64-bit rounding */
+    ape_apply_filters(ctx, ctx->decoded[0], NULL, count,
+                      ctx->interim_mode != 0);

     currentA = p->lastA[0];

@@ -1380,9 +1384,14 @@ static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
     do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
 }

+/**
+ * @param interim round in 64 bits, the default wraps around in 32 bits
+ */
 static void do_apply_filter(APEContext *ctx, int version, APEFilter *f,
-                            int32_t *data, int count, int order, int fracbits)
+                            int32_t *data, int count, int order, int fracbits,
+                            int interim)
 {
+    const int round = 1 << (fracbits - 1);
     int res;
     unsigned absres;

@@ -1392,7 +1401,10 @@ static void do_apply_filter(APEContext *ctx, int version, APEFilter *f,
                                                      f->delay - order,
                                                      f->adaptcoeffs - order,
                                                      order, APESIGN(*data));
-        res = (int64_t)(res + (1LL << (fracbits - 1))) >> fracbits;
+        if (interim)
+            res = ((int64_t)res + round) >> fracbits;
+        else
+            res = (int)(res + (unsigned)round) >> fracbits;
         res += (unsigned)*data;
         *data++ = res;

@@ -1444,15 +1456,17 @@ static void do_apply_filter(APEContext *ctx, int version, APEFilter *f,

 static void apply_filter(APEContext *ctx, APEFilter *f,
                          int32_t *data0, int32_t *data1,
-                         int count, int order, int fracbits)
+                         int count, int order, int fracbits, int interim)
 {
-    do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
+    do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order,
+                    fracbits, interim);
     if (data1)
-        do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
+        do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order,
+                        fracbits, interim);
 }

 static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
-                              int32_t *decoded1, int count)
+                              int32_t *decoded1, int count, int interim)
 {
     int i;

@@ -1461,7 +1475,7 @@ static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
             break;
         apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count,
                      ape_filter_orders[ctx->fset][i],
-                     ape_filter_fracbits[ctx->fset][i]);
+                     ape_filter_fracbits[ctx->fset][i], interim);
     }
 }