From 6efce8183d4bd381ff75e969e5a25315c99e5b77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 13:09:11 +0300 Subject: [PATCH] fix(opus): 12 kHz input caps bandwidth at Medium, not Narrow opus_encode_native's input-rate ceiling has four tiers (<=24k SWB, <=16k WB, <=12k MB, <=8k NB); our match folded 12 kHz into the Narrow arm, coding 12 kHz VoIP a band narrower than the reference would. The opus-rs shim's slower-rates test surfaced it, stale in the other direction: it still expected CELT's 120-sample overlap at every rate, but since the libopus threshold port 16 kHz and below dispatch to SILK at 32 kbps, whose resampler look-ahead is what the encoder reports. Both expectations now follow the dispatch. --- crates/ec-opus/src/encoder.rs | 1 + shims/opus-rs/src/lib.rs | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/ec-opus/src/encoder.rs b/crates/ec-opus/src/encoder.rs index 3dd9e4f1..3530f265 100644 --- a/crates/ec-opus/src/encoder.rs +++ b/crates/ec-opus/src/encoder.rs @@ -680,6 +680,7 @@ impl Encoder { 48000 => Bandwidth::Full, 24000 => Bandwidth::SuperWide, 16000 => Bandwidth::Wide, + 12000 => Bandwidth::Medium, _ => Bandwidth::Narrow, }; BWS[idx].min(ceiling) diff --git a/shims/opus-rs/src/lib.rs b/shims/opus-rs/src/lib.rs index baff178c..64c587c3 100644 --- a/shims/opus-rs/src/lib.rs +++ b/shims/opus-rs/src/lib.rs @@ -374,15 +374,19 @@ mod tests { } /// Input below 48 kHz, which the shim used to refuse outright, and the - /// `look_ahead` that goes with it (120 samples at 48 kHz, scaled). + /// `look_ahead` that goes with it. 48/24 kHz VoIP at 32 kbps run + /// hybrid (SWB/FB), so the delay is CELT's 120-sample overlap, scaled; + /// 16 kHz and below cap the bandwidth at WB or narrower, where hybrid + /// does not exist, so dispatch takes SILK and the delay is the SILK + /// resampler look-ahead (WB 50 / MB 54 / NB 58 at 48 kHz), scaled. #[test] fn slower_input_rates_encode_and_report_their_own_delay() { for &(rate, frame, look) in &[ (48_000i32, 960usize, 120usize), (24_000, 480, 60), - (16_000, 320, 40), - (12_000, 240, 30), - (8_000, 160, 20), + (16_000, 320, 50 / 3), + (12_000, 240, 54 / 4), + (8_000, 160, 58 / 6), ] { let mut enc = OpusEncoder::new(rate, 1, Application::Voip).unwrap(); enc.bitrate_bps = 32_000;