PR #26164 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26164 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26164.patch
The decoder never set its initial state. The previous LSFs stayed at zero, so in 20 ms mode the first frame used an unstable synthesis filter and decoded to a full-scale burst. Short files sounded like noise. - Initialize the state the way the WebRTC reference does (mean LSF, PLC defaults). - Run the PLC for frames flagged empty or with an out-of-range start index. The start index is at most 3 in 20 ms mode. - Fix `norm_w32()`, which was off by one. The PLC periodicity overflowed, so concealment fell back to noise. With the enhancer off, the output is now bit-exact with WebRTC libilbc. That holds for an Apple-encoded file (40000/40000 samples) and for the same file with 25 corrupted frames. The enhancer is still unimplemented and accounts for the remaining ~20 dB difference from enhanced decoders. FATE: `fate-ilbc-caf` needs `caf/ilbc.caf` (4590 bytes, 2001 samples, Apple iLBC) uploaded to fate-suite. It also depends on the cafdec fix for CBR packet tables placed before the data chunk, which is not merged yet. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 40f4b877c6681fc9389a10328273294ed0fcdf7f Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Tue, 17 Nov 2026 22:26:30 -0800 Subject: [PATCH 1/3] avcodec/ilbcdec: initialize the decoder state Start from the mean LSF and the reference PLC defaults; zeroed LSFs made the first frame decode to a full-scale burst. Signed-off-by: Baptiste Coudurier <[email protected]> --- libavcodec/ilbcdata.h | 4 ++++ libavcodec/ilbcdec.c | 8 ++++++++ tests/Makefile | 1 + tests/fate/ilbc.mak | 5 +++++ tests/ref/fate/ilbc-caf | 18 ++++++++++++++++++ 5 files changed, 36 insertions(+) create mode 100644 tests/fate/ilbc.mak create mode 100644 tests/ref/fate/ilbc-caf diff --git a/libavcodec/ilbcdata.h b/libavcodec/ilbcdata.h index b17e24df5f4..15bc0fd42cf 100644 --- a/libavcodec/ilbcdata.h +++ b/libavcodec/ilbcdata.h @@ -37,6 +37,10 @@ static const uint8_t lsf_dim_codebook[] = { 3, 3, 4 }; static const uint8_t lsf_size_codebook[] = { 64, 128, 128 }; +static const int16_t lsf_mean[] = { + 2308, 3652, 5434, 7885, 10255, 12559, 15160, 17513, 20328, 22752 +}; + static const int16_t lsf_weight_20ms[] = { 12288, 8192, 4096, 0 }; static const int16_t lsf_weight_30ms[] = { 8192, 16384, 10923, 5461, 0, 0 }; diff --git a/libavcodec/ilbcdec.c b/libavcodec/ilbcdec.c index 8b495a2f8e8..b67aba4f12f 100644 --- a/libavcodec/ilbcdec.c +++ b/libavcodec/ilbcdec.c @@ -1471,6 +1471,14 @@ static av_cold int ilbc_decode_init(AVCodecContext *avctx) s->state_short_len = STATE_SHORT_LEN_20MS; } + memcpy(s->lsfdeqold, lsf_mean, sizeof(s->lsfdeqold)); + for (int i = 0; i < NSUB_MAX; i++) + s->old_syntdenum[i * (LPC_FILTERORDER + 1)] = 4096; + s->prev_lpc[0] = 4096; + s->last_lag = 20; + s->prevLag = 120; + s->seed = 777; + return 0; } diff --git a/tests/Makefile b/tests/Makefile index 0656946f11f..8a896c6c206 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -219,6 +219,7 @@ include $(SRC_PATH)/tests/fate/hlsenc.mak include $(SRC_PATH)/tests/fate/hw.mak include $(SRC_PATH)/tests/fate/iamf.mak include $(SRC_PATH)/tests/fate/id3v2.mak +include $(SRC_PATH)/tests/fate/ilbc.mak include $(SRC_PATH)/tests/fate/image.mak include $(SRC_PATH)/tests/fate/imf.mak include $(SRC_PATH)/tests/fate/indeo.mak diff --git a/tests/fate/ilbc.mak b/tests/fate/ilbc.mak new file mode 100644 index 00000000000..3dfa112e811 --- /dev/null +++ b/tests/fate/ilbc.mak @@ -0,0 +1,5 @@ +FATE_ILBC-$(call DEMDEC, CAF, ILBC) += fate-ilbc-caf +fate-ilbc-caf: CMD = framecrc -i $(TARGET_SAMPLES)/caf/ilbc.caf + +FATE_SAMPLES_FFMPEG += $(FATE_ILBC-yes) +fate-ilbc: $(FATE_ILBC-yes) diff --git a/tests/ref/fate/ilbc-caf b/tests/ref/fate/ilbc-caf new file mode 100644 index 00000000000..ba065a26a6a --- /dev/null +++ b/tests/ref/fate/ilbc-caf @@ -0,0 +1,18 @@ +#tb 0: 1/8000 +#media_type 0: audio +#codec_id 0: pcm_s16le +#sample_rate 0: 8000 +#channel_layout_name 0: mono +0, 0, 0, 120, 240, 0x8ac674f7 +0, 120, 120, 160, 320, 0xdfb5a6b4 +0, 280, 280, 160, 320, 0x5ccc9b03 +0, 440, 440, 160, 320, 0x6227a378 +0, 600, 600, 160, 320, 0x96c29bea +0, 760, 760, 160, 320, 0x8d159ada +0, 920, 920, 160, 320, 0x8ca892ca +0, 1080, 1080, 160, 320, 0x744ba03d +0, 1240, 1240, 160, 320, 0x69dba381 +0, 1400, 1400, 160, 320, 0x99d09bed +0, 1560, 1560, 160, 320, 0x8d159ada +0, 1720, 1720, 160, 320, 0x8ca892ca +0, 1880, 1880, 121, 242, 0x8edf77b3 -- 2.54.0 From 7230e199a2f47441c531be6d03fe5b485a270ec0 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Tue, 17 Nov 2026 22:29:12 -0800 Subject: [PATCH 2/3] avcodec/ilbcdec: conceal invalid frames Run the PLC for frames flagged empty or with an out of range start index, which is at most 3 in 20 ms mode. Signed-off-by: Baptiste Coudurier <[email protected]> --- libavcodec/ilbcdec.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libavcodec/ilbcdec.c b/libavcodec/ilbcdec.c index b67aba4f12f..246b16b1828 100644 --- a/libavcodec/ilbcdec.c +++ b/libavcodec/ilbcdec.c @@ -1370,7 +1370,7 @@ static int ilbc_decode_frame(AVCodecContext *avctx, AVFrame *frame, if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) return ret; - if (s->frame.start < 1 || s->frame.start > 5) + if (s->frame.start < 1 || s->frame.start > (s->mode == 20 ? 3 : 5)) mode = 0; if (mode) { @@ -1387,6 +1387,14 @@ static int ilbc_decode_frame(AVCodecContext *avctx, AVFrame *frame, s->last_lag, s); memcpy(s->decresidual, s->plc_residual, s->block_samples * 2); + } else { + do_plc(s->plc_residual, s->plc_lpc, 1, s->decresidual, s->syntdenum, + s->last_lag, s); + + memcpy(s->decresidual, s->plc_residual, s->block_samples * 2); + for (int i = 0; i < s->nsub; i++) + memcpy(s->syntdenum + i * (LPC_FILTERORDER + 1), s->plc_lpc, + (LPC_FILTERORDER + 1) * 2); } if (s->enhancer) { -- 2.54.0 From 3eeab80678ff895a3f93df1195cd3ea39b26abf8 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Tue, 17 Nov 2026 22:29:22 -0800 Subject: [PATCH 3/3] avcodec/ilbcdec: fix norm_w32() It must return the left shift that keeps the sign bit, as in the reference; the PLC periodicity overflowed and concealment fell back to noise. Signed-off-by: Baptiste Coudurier <[email protected]> --- libavcodec/ilbcdec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavcodec/ilbcdec.c b/libavcodec/ilbcdec.c index 246b16b1828..000d29d5d4a 100644 --- a/libavcodec/ilbcdec.c +++ b/libavcodec/ilbcdec.c @@ -940,7 +940,7 @@ static int16_t norm_w32(int32_t a) a = ~a; } - return ff_clz(a); + return ff_clz(a) - 1; } static int32_t div_w32_w16(int32_t num, int16_t den) -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
