PR #26236 opened by Marcos Ashton (MarcosAsh)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26236
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26236.patch

Stereo APE files of version 3.95 and later decode wrong when the decoder's 
max_samples option isn't a multiple of 512, for example `ffmpeg -max_samples 
1000 -i luckynight-partial.ape`. Since 86c092a0edc 
predictor_decode_stereo_3950() sets the history pointer back to the start of 
the buffer on every call. The reset was there because `p_interim = *p_default` 
copies a pointer into the other struct's array. This drops the reset and points 
the copy at its own buffer in the two places where the struct is copied.

The default of 4608 is a multiple of 512, so the default output doesn't change, 
and the 52 APE files I have decode the same as before. I encoded 30 files with 
MAC 13.26 (8, 16 and 24 bit, mono and stereo, c1000 to c5000) and decoded them 
with max_samples 4608, 1, 7, 511, 512, 513, 1000, 1024, 4607 and all. 270 of 
the 300 runs match MAC's decoder, against 192 before. The other 30 are three 
24-bit files that are wrong at every value, which is the rounding bug in #26176.

The two tests decode samples that are already in fate-suite with -max_samples 
1000 and expect the same output as the default. Both fail without the fix. 
86c092a0edc is in every release branch from 6.1 on. This doesn't depend on 
#26176 but it touches lines next to it, so whichever goes in second needs a 
rebase.


>From 854c79a648a79df7372345b8ce2f24e7a8a295a9 Mon Sep 17 00:00:00 2001
From: marcos ashton <[email protected]>
Date: Fri, 20 Nov 2026 19:21:49 +0000
Subject: [PATCH 1/2] avcodec/apedec: keep the predictor history position
 between calls

Since 86c092a0edc predictor_decode_stereo_3950() restarted at the beginning of 
the history buffer on every call, so stereo files of version 3.95 and later 
decoded wrong when max_samples was not a multiple of 512.
---
 libavcodec/apedec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/apedec.c b/libavcodec/apedec.c
index 466c3c2001c..fd6fa780d6c 100644
--- a/libavcodec/apedec.c
+++ b/libavcodec/apedec.c
@@ -1252,6 +1252,7 @@ static void predictor_decode_stereo_3950(APEContext *ctx, 
int count)
     ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
     if (ctx->interim_mode == -1) {
         p_interim = *p_default;
+        p_interim.buf = p_interim.historybuffer + (p_default->buf - 
p_default->historybuffer);
         num_passes ++;
         memcpy(ctx->interim[0], ctx->decoded[0], 
sizeof(*ctx->interim[0])*count);
         memcpy(ctx->interim[1], ctx->decoded[1], 
sizeof(*ctx->interim[1])*count);
@@ -1271,7 +1272,6 @@ static void predictor_decode_stereo_3950(APEContext *ctx, 
int count)
             decoded0 = ctx->decoded[0];
             decoded1 = ctx->decoded[1];
         }
-        p->buf = p->historybuffer;
 
         count = lcount;
         while (count--) {
@@ -1310,7 +1310,7 @@ static void predictor_decode_stereo_3950(APEContext *ctx, 
int count)
         memcpy(ctx->decoded[0], ctx->interim[0], 
sizeof(*ctx->interim[0])*lcount);
         memcpy(ctx->decoded[1], ctx->interim[1], 
sizeof(*ctx->interim[1])*lcount);
         *p_default = p_interim;
-        p_default->buf = p_default->historybuffer;
+        p_default->buf = p_default->historybuffer + (p_interim.buf - 
p_interim.historybuffer);
     }
 }
 
-- 
2.54.1


>From af4d7acd87e6ead3f3e2e9bae5db43568be8e85a Mon Sep 17 00:00:00 2001
From: marcos ashton <[email protected]>
Date: Fri, 20 Nov 2026 19:21:49 +0000
Subject: [PATCH 2/2] tests/fate/monkeysaudio: add tests with a non-default
 max_samples

They decode two existing samples with -max_samples 1000 and fail without the 
commit before this one.
---
 tests/fate/monkeysaudio.mak                             | 6 ++++++
 tests/ref/fate/lossless-monkeysaudio-399-max-samples    | 1 +
 tests/ref/fate/lossless-monkeysaudio-legacy-max-samples | 1 +
 3 files changed, 8 insertions(+)
 create mode 100644 tests/ref/fate/lossless-monkeysaudio-399-max-samples
 create mode 100644 tests/ref/fate/lossless-monkeysaudio-legacy-max-samples

diff --git a/tests/fate/monkeysaudio.mak b/tests/fate/monkeysaudio.mak
index eaca9005c89..0eb24527720 100644
--- a/tests/fate/monkeysaudio.mak
+++ b/tests/fate/monkeysaudio.mak
@@ -19,6 +19,12 @@ fate-lossless-monkeysaudio-399: CMD = md5 -i 
$(TARGET_SAMPLES)/lossless-audio/lu
 FATE_APE += fate-lossless-monkeysaudio-legacy
 fate-lossless-monkeysaudio-legacy: CMD = md5 -i 
$(TARGET_SAMPLES)/lossless-audio/NoLegacy-cut.ape -f s32le -af aresample
 
+FATE_APE += fate-lossless-monkeysaudio-399-max-samples
+fate-lossless-monkeysaudio-399-max-samples: CMD = md5 -max_samples 1000 -i 
$(TARGET_SAMPLES)/lossless-audio/luckynight-partial.ape -f s16le -af 
atrim=end_sample=73728,aresample
+
+FATE_APE += fate-lossless-monkeysaudio-legacy-max-samples
+fate-lossless-monkeysaudio-legacy-max-samples: CMD = md5 -max_samples 1000 -i 
$(TARGET_SAMPLES)/lossless-audio/NoLegacy-cut.ape -f s32le -af 
atrim=end_sample=13824,aresample
+
 FATE_APE := $(if $(call CRC, APE, APE, ARESAMPLE_FILTER), $(FATE_APE))
 FATE_SAMPLES_FFMPEG += $(FATE_APE)
 fate-lossless-monkeysaudio: $(FATE_APE)
diff --git a/tests/ref/fate/lossless-monkeysaudio-399-max-samples 
b/tests/ref/fate/lossless-monkeysaudio-399-max-samples
new file mode 100644
index 00000000000..9b68328ed3d
--- /dev/null
+++ b/tests/ref/fate/lossless-monkeysaudio-399-max-samples
@@ -0,0 +1 @@
+5815b76ffecd805219727575fe6da623
diff --git a/tests/ref/fate/lossless-monkeysaudio-legacy-max-samples 
b/tests/ref/fate/lossless-monkeysaudio-legacy-max-samples
new file mode 100644
index 00000000000..33d4b562047
--- /dev/null
+++ b/tests/ref/fate/lossless-monkeysaudio-legacy-max-samples
@@ -0,0 +1 @@
+2cd8a60478c77382dfed8b8bdf1f70e4
-- 
2.54.1

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

Reply via email to