PR #26341 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26341
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26341.patch

The MS ADPCM predictor is `(s1 * c1 + s2 * c2) >> 8` in the reference decoders, 
rounding down; FFmpeg's `/ 64` (on quarter coefficients) rounds toward zero and 
drifts on negative values.

With the fix, FFmpeg matches Microsoft's msadp32 decoder and Apple's decoder 
sample for sample on `dxa/meetsquid.dxa`, 
`qt-surge-suite/surge-2-16-L-ms02.mov` and a Microsoft-encoded mono file. The 
encoder only uses block predictor 0, so its output does not change. Regenerated 
references for `adpcm-ms-mono` and `adpcm_ms-stereo`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From f5f1431a34edc9acdf6cb115c3405017c9507a2a Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 19 Nov 2026 18:09:43 -0800
Subject: [PATCH] avcodec/adpcm: floor the MS ADPCM predictor

The reference decoders shift the predictor right, rounding down;
the division rounded toward zero and drifted on negative values.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavcodec/adpcm.c             |  2 +-
 libavcodec/adpcmenc.c          |  4 ++--
 tests/ref/fate/adpcm-ms-mono   | 36 +++++++++++++++++-----------------
 tests/ref/fate/adpcm_ms-stereo |  2 +-
 4 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c
index 759a90aaf42..b48332ab11b 100644
--- a/libavcodec/adpcm.c
+++ b/libavcodec/adpcm.c
@@ -706,7 +706,7 @@ static inline int16_t 
adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble)
 {
     int predictor;
 
-    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) 
/ 64;
+    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) 
>> 6;
     predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
 
     c->sample2 = c->sample1;
diff --git a/libavcodec/adpcmenc.c b/libavcodec/adpcmenc.c
index fd8ce9e3670..26c6d446197 100644
--- a/libavcodec/adpcmenc.c
+++ b/libavcodec/adpcmenc.c
@@ -288,7 +288,7 @@ static inline uint8_t 
adpcm_ms_compress_sample(ADPCMChannelStatus *c,
     int predictor, nibble, bias;
 
     predictor = (((c->sample1) * (c->coeff1)) +
-                (( c->sample2) * (c->coeff2))) / 64;
+                (( c->sample2) * (c->coeff2))) >> 6;
 
     nibble = sample - predictor;
     if (nibble >= 0)
@@ -388,7 +388,7 @@ static void adpcm_compress_trellis(AVCodecContext *avctx,
             int nidx;
             if (version == AV_CODEC_ID_ADPCM_MS) {
                 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
-                                       (nodes[j]->sample2 * c->coeff2)) / 64;
+                                       (nodes[j]->sample2 * c->coeff2)) >> 6;
                 const int div  = (sample - predictor) / step;
                 const int nmin = av_clip(div-range, -8, 6);
                 const int nmax = av_clip(div+range, -7, 7);
diff --git a/tests/ref/fate/adpcm-ms-mono b/tests/ref/fate/adpcm-ms-mono
index 9823ba77b52..b60516cc66a 100644
--- a/tests/ref/fate/adpcm-ms-mono
+++ b/tests/ref/fate/adpcm-ms-mono
@@ -8,33 +8,33 @@
 0,       1000,       1000,      500,     1000, 0x75c19868
 0,       1500,       1500,      500,     1000, 0x93db6f79
 0,       2000,       2000,      500,     1000, 0x6835625d
-0,       2500,       2500,      500,     1000, 0xb3affa8f
-0,       3000,       3000,      500,     1000, 0x159fdcc8
-0,       3500,       3500,      500,     1000, 0x79f9f7f1
-0,       4000,       4000,      500,     1000, 0xd7d1131e
-0,       4500,       4500,      500,     1000, 0x52a6f797
-0,       5000,       5000,      500,     1000, 0x748202ca
-0,       5500,       5500,      500,     1000, 0x0ef92449
+0,       2500,       2500,      500,     1000, 0x531ff916
+0,       3000,       3000,      500,     1000, 0xc9e2e406
+0,       3500,       3500,      500,     1000, 0x2d26f817
+0,       4000,       4000,      500,     1000, 0xbce41636
+0,       4500,       4500,      500,     1000, 0xf535f6de
+0,       5000,       5000,      500,     1000, 0x655b0409
+0,       5500,       5500,      500,     1000, 0xce86227b
 0,       6000,       6000,      500,     1000, 0x6a3760ab
 0,       6500,       6500,      500,     1000, 0xce5c5abf
 0,       7000,       7000,      500,     1000, 0x23396792
 0,       7500,       7500,      500,     1000, 0xa5276238
-0,       8000,       8000,      500,     1000, 0x288adf1b
+0,       8000,       8000,      500,     1000, 0x7402db33
 0,       8500,       8500,      500,     1000, 0xe7de6fb2
 0,       9000,       9000,      500,     1000, 0x2c2c707f
 0,       9500,       9500,      500,     1000, 0xd66d6daf
 0,      10000,      10000,      500,     1000, 0xbcea7d64
-0,      10500,      10500,      500,     1000, 0x766feea5
-0,      11000,      11000,      500,     1000, 0xd2e1d63a
-0,      11500,      11500,      500,     1000, 0x2f7ef4ed
-0,      12000,      12000,      500,     1000, 0xb655cba4
-0,      12500,      12500,      500,     1000, 0x4507d37b
+0,      10500,      10500,      500,     1000, 0xec36f549
+0,      11000,      11000,      500,     1000, 0x77eadc97
+0,      11500,      11500,      500,     1000, 0xe912ef58
+0,      12000,      12000,      500,     1000, 0xf2dae691
+0,      12500,      12500,      500,     1000, 0xa632e2d9
 0,      13000,      13000,      500,     1000, 0x0c57f794
-0,      13500,      13500,      500,     1000, 0x0ecbe5cc
-0,      14000,      14000,      500,     1000, 0x9bf6e345
+0,      13500,      13500,      500,     1000, 0x8515e809
+0,      14000,      14000,      500,     1000, 0xfbf3fc65
 0,      14500,      14500,      500,     1000, 0xc461443c
 0,      15000,      15000,      500,     1000, 0xad9657bf
-0,      15500,      15500,      500,     1000, 0x466fe91c
+0,      15500,      15500,      500,     1000, 0x5c89e720
 0,      16000,      16000,      500,     1000, 0x9ee377fe
 0,      16500,      16500,      500,     1000, 0x09956428
 0,      17000,      17000,      500,     1000, 0x9b285f0a
@@ -43,8 +43,8 @@
 0,      18500,      18500,      500,     1000, 0x377be319
 0,      19000,      19000,      500,     1000, 0xe4890504
 0,      19500,      19500,      500,     1000, 0xe90a6497
-0,      20000,      20000,      500,     1000, 0xd00fe950
-0,      20500,      20500,      500,     1000, 0xf195eb44
+0,      20000,      20000,      500,     1000, 0x6ffaed67
+0,      20500,      20500,      500,     1000, 0xfef9e726
 0,      21000,      21000,      500,     1000, 0xa491f3ef
 0,      21500,      21500,      500,     1000, 0x2c036e18
 0,      22000,      22000,       50,      100, 0x0bd81f05
diff --git a/tests/ref/fate/adpcm_ms-stereo b/tests/ref/fate/adpcm_ms-stereo
index 6b131c1f970..76ba9ca3cbe 100644
--- a/tests/ref/fate/adpcm_ms-stereo
+++ b/tests/ref/fate/adpcm_ms-stereo
@@ -1 +1 @@
-3b7554e9a186c30adfd2a86bae950f4a
+98caebf5f36b81a7a01dce4c43d29139
-- 
2.54.1

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

Reply via email to