PR #26176 opened by Marcos Ashton (MarcosAsh) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26176 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26176.patch
Fixes #24268. 24-bit files from current Monkey's Audio versions can decode wrong and fail their CRC. ed0001482a7 made do_apply_filter() round in 64 bits for every file, but the reference decoder only does that in its interim mode, for 24-bit files from MAC 5.01 to 8.51. Everything else adds the rounding term in 32 bits and lets it wrap. This brings back the 32-bit line from before that commit for files that aren't in interim mode. A 24-bit stereo file keeps the 64-bit rounding until its mode is decided, as it does now. Mono has no interim detection, so it gets the 32-bit rounding. I have no 24-bit mono file to test that with. The sample from the issue now matches the source PCM and passes -err_detect crccheck. The other 22 APE files I have (FATE, samples.ffmpeg.org and the interim sample from the issue's tooling branch) decode the same as before, with and without asm, and fate-lossless-monkeysaudio passes. There is no FATE test because the sample isn't in fate-suite. I can add one once it is. >From 2b0e61547af98a500292374ed1f1a87dee48732a Mon Sep 17 00:00:00 2001 From: marcos ashton <[email protected]> Date: Wed, 18 Nov 2026 15:09:40 +0000 Subject: [PATCH] avcodec/apedec: round the filter output in 32 bits outside interim mode Only the interim versions round it in 64 bits, so since ed0001482a7 24-bit files from current versions decode wrong when the sum wraps around. Fixes #24268. --- libavcodec/apedec.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/libavcodec/apedec.c b/libavcodec/apedec.c index 466c3c2001c..5e1b727f331 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,8 @@ 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, + ctx->interim_mode > 0); while (count--) { /* Predictor Y */ @@ -1170,7 +1171,8 @@ 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, + ctx->interim_mode > 0); while (count--) { *decoded0 = predictor_update_3930(p, *decoded0, 0, YDELAYA); @@ -1249,7 +1251,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; num_passes ++; @@ -1320,7 +1324,8 @@ 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); + ape_apply_filters(ctx, ctx->decoded[0], NULL, count, + ctx->interim_mode > 0); currentA = p->lastA[0]; @@ -1380,9 +1385,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 +1402,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 +1457,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 +1476,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); } } -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
