diff --git a/crates/ec-opus/src/encoder.rs b/crates/ec-opus/src/encoder.rs index 1b63e5a9..578274e8 100644 --- a/crates/ec-opus/src/encoder.rs +++ b/crates/ec-opus/src/encoder.rs @@ -47,6 +47,21 @@ const SILK_LOOK_AHEAD_48K_WB: usize = 50; const HYBRID_SILK_DELAY_48K: usize = 120 - SILK_LOOK_AHEAD_48K_WB; /// Bytes the CELT layer of a hybrid packet always keeps, whatever SILK spent. const HYBRID_CELT_MIN_BYTES: usize = 8; +/// libopus 1.6 bandwidth threshold tables (`opus_encoder.c:151`/`162`). +/// Each pair is `(threshold, transition_width)` for one bandwidth boundary; +/// voice and music, mono and stereo, are identical in 1.6. +const MONO_VOICE_BW: [i32; 8] = [9000, 700, 9000, 700, 13500, 1000, 14000, 2000]; +const STEREO_VOICE_BW: [i32; 8] = [9000, 700, 9000, 700, 13500, 1000, 14000, 2000]; +const MONO_MUSIC_BW: [i32; 8] = [9000, 700, 9000, 700, 11000, 1000, 12000, 2000]; +const STEREO_MUSIC_BW: [i32; 8] = [9000, 700, 9000, 700, 11000, 1000, 12000, 2000]; +/// libopus `mode_thresholds` blended by `voice_est` (`opus_encoder.c:183`). +/// With `stereo_width=0` (no analysis) the voice term is `[0][0]=64000` and +/// the music base is `10000`; the gap (54000) is the ve² interpolation span. +const MODE_THRESHOLD_VOICE: i32 = 64000; +const MODE_THRESHOLD_MUSIC: i32 = 10000; +/// Opus default encoder complexity (the `(90+complexity)/100` equiv-rate +/// multiplier; loss=0 and complexity≥5 add no per-mode adjustment). +const DEFAULT_COMPLEXITY: i32 = 9; /// What the caller is encoding. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -95,6 +110,17 @@ pub struct Encoder { silk_stuff: Vec, } +/// SILK's internal sample rate (kHz) for a target bandwidth: Narrow → 8, +/// Medium → 12, Wide/SuperWide/Full → 16. SILK only runs at 8/12/16 kHz; +/// for SuperWide/Full the top bands are CELT (Hybrid), so SILK itself is 16. +fn silk_fs_khz(bw: Bandwidth) -> usize { + match bw { + Bandwidth::Narrow => 8, + Bandwidth::Medium => 12, + _ => 16, + } +} + impl Encoder { /// An encoder for `channels` channels (1 or 2) at `sample_rate`, which /// must be one of 8000, 12000, 16000, 24000 or 48000 Hz — the rates @@ -242,32 +268,41 @@ impl Encoder { } /// Whether the application/bitrate (or an explicit [`Encoder::set_mode`]) - /// call for SILK, and if so, which SILK internal sample rate in kHz. + /// calls for SILK, and if so, which SILK internal sample rate in kHz. /// `None` means CELT or Hybrid (`hybrid_choice` tells those apart). - fn wants_silk(&self) -> Option { - let forced = || match self.bandwidth.unwrap_or_else(|| self.auto_bandwidth()) { - Bandwidth::Narrow => 8, - Bandwidth::Medium => 12, - _ => 16, - }; + /// + /// Ports libopus's mode decision: SILK when `equiv_rate` is below the + /// voice/music-blended mode threshold (`opus_encoder.c` ~1900), with the + /// bandwidth from the threshold walk. SILK only codes up to Wide; a + /// SuperWide/Full result is Hybrid (10/20 ms) or, for 40/60 ms frames + /// that Hybrid cannot carry, SILK capped at Wide. + fn wants_silk(&self, frame_48k: usize) -> Option { match self.mode { Some(Mode::Celt) | Some(Mode::Hybrid) => None, - Some(Mode::Silk) => Some(forced()), + Some(Mode::Silk) => { + let bw = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth()); + Some(silk_fs_khz(bw)) + } None => { - if self.application == Application::Voip { - let per_channel = self.bitrate / self.channels as u32; - if per_channel < 20_000 { - return Some(match self.bandwidth { - Some(Bandwidth::Narrow) => 8, - Some(Bandwidth::Medium) => 12, - Some(_) => 16, - None if per_channel <= 12_000 => 8, - None if per_channel <= 16_000 => 12, - None => 16, - }); - } + let equiv = self.equiv_rate(frame_48k); + let ve = self.voice_est(); + if !self.auto_silk(equiv, ve) { + return None; + } + let bw = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth_at(equiv, ve)); + // 40/60 ms frames are SILK-only (Hybrid supports 10/20 ms): + // cap at Wide so a high equiv rate does not fall through to + // an impossible CELT 40/60 ms packet. + let bw = if matches!(frame_48k, 1920 | 2880) { + bw.min(Bandwidth::Wide) + } else { + bw + }; + if bw > Bandwidth::Wide { + None // Hybrid territory — see hybrid_choice + } else { + Some(silk_fs_khz(bw)) } - None } } } @@ -276,7 +311,7 @@ impl Encoder { /// mono 10/20/40/60 ms, stereo 10/20 ms directly, or stereo 40/60 ms as /// code-3 packets of 20 ms SILK frames. fn silk_choice(&self, frame_48k: usize) -> Option { - let fs_khz = self.wants_silk()?; + let fs_khz = self.wants_silk(frame_48k)?; if !matches!(frame_48k, 480 | 960 | 1920 | 2880) { return None; } @@ -284,27 +319,27 @@ impl Encoder { } /// Whether this frame goes out as a hybrid packet, and at which of the two - /// hybrid bandwidths. Automatic: VoIP from 20 kbps per coded channel - /// (where SILK alone stops) to 40 kbps per coded channel (where CELT alone - /// is the better speech coder too), at the bitrate's own bandwidth - /// (SWB/FB; narrower requests are CELT's). Mono and stereo, 10 or 20 ms. + /// hybrid bandwidths. Automatic: Hybrid is SILK mode (equiv below the mode + /// threshold) whose bandwidth walk lands on SuperWide/Full — SILK alone + /// cannot code above Wide, so CELT carries the top bands. 10 or 20 ms + /// only (Hybrid has no 40/60 ms config). Forced Hybrid codes at least SWB. fn hybrid_choice(&self, frame_48k: usize) -> Option { - let per_channel = self.bitrate / self.channels as u32; - let wanted = match self.mode { - Some(Mode::Hybrid) => true, - Some(_) => false, - None => { - self.application == Application::Voip && (20_000..40_000).contains(&per_channel) - } - }; - if !wanted || !matches!(frame_48k, 480 | 960) { + if matches!(self.mode, Some(Mode::Silk) | Some(Mode::Celt)) { return None; } - let bandwidth = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth()); - match bandwidth { - Bandwidth::SuperWide | Bandwidth::Full => Some(bandwidth), - // A forced Hybrid codes at least SWB; an automatic one that the - // bitrate would cap narrower is CELT's. + if !matches!(frame_48k, 480 | 960) { + return None; + } + let equiv = self.equiv_rate(frame_48k); + let ve = self.voice_est(); + if self.mode.is_none() && !self.auto_silk(equiv, ve) { + return None; + } + let bw = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth_at(equiv, ve)); + match bw { + Bandwidth::SuperWide | Bandwidth::Full => Some(bw), + // A forced Hybrid with a narrow/medium/wide bandwidth still codes + // at least SWB — Hybrid has no narrower configuration. _ if self.mode == Some(Mode::Hybrid) => Some(Bandwidth::SuperWide), _ => None, } @@ -531,7 +566,9 @@ impl Encoder { )); } }; - let bandwidth = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth()); + let bandwidth = self + .bandwidth + .unwrap_or_else(|| self.auto_bandwidth_at(self.equiv_rate(frame_48k), self.voice_est())); let bw_idx: u8 = match bandwidth { Bandwidth::Narrow => 0, // CELT has no mediumband configuration; the next one up covers it. @@ -547,39 +584,92 @@ impl Encoder { )) } - /// Bandwidth from the bitrate: coding 20 kHz at 24 kbps spends bits on air. - /// - /// The thresholds are on the mono-equivalent rate — a coupled stereo stream - /// needs about 1.6x a mono one for the same per-channel quality — and VoIP - /// asks for one step narrower. The input's own Nyquist bounds the result: - /// coding above it would code the images the zero-stuffing put there. + /// Bandwidth from the bitrate — libopus's threshold walk, 20 ms default. + /// Kept for the forced-mode paths and unit tests; the dispatch functions + /// pass the actual frame's equiv rate to [`auto_bandwidth_at`]. fn auto_bandwidth(&self) -> Bandwidth { - let equiv = if self.channels == 2 { - (self.bitrate as f32 * 0.62) as u32 + self.auto_bandwidth_at(self.equiv_rate(960), self.voice_est()) + } + + /// The equivalent rate a frame is coded at, porting libopus + /// `compute_equiv_rate` (`opus_encoder.c:1021`): the bitrate, minus the + /// per-frame overhead for sub-20 ms frames, minus the CBR headroom, times + /// the complexity multiplier. Loss is 0 and complexity ≥ 5, so the + /// are both no-ops. + fn equiv_rate(&self, frame_48k: usize) -> i32 { + let frame_rate = 48000 / frame_48k as i32; + let mut equiv = self.bitrate as i32; + if frame_rate > 50 { + equiv -= (40 * self.channels as i32 + 20) * (frame_rate - 50); + } + if !self.vbr { + equiv -= equiv / 12; + } + equiv = equiv * (90 + DEFAULT_COMPLEXITY) / 100; + equiv + } + + /// Voice activity estimate: VoIP → 115, Audio and LowDelay → 48 + /// (`opus_encoder.c` `tonality_analysis` proxy — no analysis is modelled, + /// so the application alone decides). + fn voice_est(&self) -> i32 { + match self.application { + Application::Voip => 115, + _ => 48, + } + } + + /// Whether the auto mode is SILK: `equiv` below the voice/music-blended + /// mode threshold (`opus_encoder.c` ~1900). LowDelay is always CELT. + /// VoIP gets the `+8000` hysteresis/voice bonus libopus applies. + fn auto_silk(&self, equiv: i32, ve: i32) -> bool { + if self.application == Application::LowDelay { + return false; + } + let threshold = MODE_THRESHOLD_MUSIC + + ((ve * ve * (MODE_THRESHOLD_VOICE - MODE_THRESHOLD_MUSIC)) >> 14); + let threshold = if self.application == Application::Voip { + threshold + 8000 } else { - self.bitrate + threshold }; - let equiv = if self.application == Application::Voip { - (equiv as f32 * 0.7) as u32 + equiv < threshold + } + + /// Bandwidth from the equivalent rate, porting libopus's `decide_bandwidth` + /// walk (`opus_encoder.c` ~1660). The voice and music threshold tables are + /// blended by `ve²`; the walk starts at Full and steps down until the equiv + /// rate sustains the current bandwidth. The input's own Nyquist clamps the + /// result — coding above it would code the images the zero-stuffing put + /// there. + fn auto_bandwidth_at(&self, equiv: i32, ve: i32) -> Bandwidth { + let (voice, music) = if self.channels == 2 { + (&STEREO_VOICE_BW, &STEREO_MUSIC_BW) } else { - equiv + (&MONO_VOICE_BW, &MONO_MUSIC_BW) }; + let ve2 = ve * ve; + // Pair i is the boundary between bandwidth i and i+1: + // 0: NB↔MB, 1: MB↔WB, 2: WB↔SWB, 3: SWB↔FB. + let thresh = |i: usize| music[2 * i] + ((ve2 * (voice[2 * i] - music[2 * i])) >> 14); + const BWS: [Bandwidth; 5] = [ + Bandwidth::Narrow, + Bandwidth::Medium, + Bandwidth::Wide, + Bandwidth::SuperWide, + Bandwidth::Full, + ]; + let mut idx = 4; // Full + while idx > 0 && equiv < thresh(idx - 1) { + idx -= 1; + } let ceiling = match self.sample_rate { 48000 => Bandwidth::Full, 24000 => Bandwidth::SuperWide, 16000 => Bandwidth::Wide, _ => Bandwidth::Narrow, }; - let wanted = if equiv < 9000 { - Bandwidth::Narrow - } else if equiv < 13000 { - Bandwidth::Wide - } else if equiv < 20000 { - Bandwidth::SuperWide - } else { - Bandwidth::Full - }; - wanted.min(ceiling) + BWS[idx].min(ceiling) } /// Encodes one frame — `frame_size` samples per channel of interleaved @@ -773,16 +863,21 @@ mod tests { let mut e = Encoder::new(48000, 1, Application::Audio).unwrap(); e.set_bitrate(64000); assert_eq!(e.auto_bandwidth(), Bandwidth::Full); + // libopus MONO_MUSIC_BW blended at voice_est=48 (analysis off): + // SWB<->FB boundary 12281, equiv(16k) = 14520 -> Full. e.set_bitrate(16000); - assert_eq!(e.auto_bandwidth(), Bandwidth::SuperWide); + assert_eq!(e.auto_bandwidth(), Bandwidth::Full); e.set_bitrate(10000); assert_eq!(e.auto_bandwidth(), Bandwidth::Wide); + // equiv(8k) = 7260 < 9000 (NB<->MB boundary) -> Narrow. e.set_bitrate(8000); assert_eq!(e.auto_bandwidth(), Bandwidth::Narrow); - // Speech asks for one step narrower at the same rate. + // VoIP (voice_est=127): SWB<->FB boundary 13616, so 16k speech is + // Full -> hybrid, matching libopus 1.6 on real speech + // (lanes/opus-silk-r2.sweep.txt: 16k-NB row, ref_mode=Hybrid-FB). let mut v = Encoder::new(48000, 1, Application::Voip).unwrap(); v.set_bitrate(16000); - assert_eq!(v.auto_bandwidth(), Bandwidth::Wide); + assert_eq!(v.auto_bandwidth(), Bandwidth::Full); // The input's own Nyquist bounds it whatever the rate says. let mut e = Encoder::new(24000, 2, Application::Audio).unwrap(); e.set_bitrate(256_000); @@ -802,8 +897,9 @@ mod tests { assert_eq!(e.look_ahead(240), 120, "5ms frame falls back to CELT"); assert_eq!(e.look_ahead(960), 58, "20ms NB SILK"); + // 16k VoIP is hybrid-FB since the libopus threshold port (CELT overlap). e.set_bitrate(16000); - assert_eq!(e.look_ahead(960), 54, "20ms MB SILK"); + assert_eq!(e.look_ahead(960), 120, "20ms hybrid FB"); e.set_bitrate(32000); assert_eq!(e.look_ahead(960), 120, "20ms hybrid, CELT's overlap"); diff --git a/crates/ec-opus/tests/conformance.rs b/crates/ec-opus/tests/conformance.rs index 4d9f051a..b6513d73 100644 --- a/crates/ec-opus/tests/conformance.rs +++ b/crates/ec-opus/tests/conformance.rs @@ -907,6 +907,9 @@ fn encoder_roundtrips_at_every_rate() { } #[test] +// Expectations are libopus 1.6's (opus_encoder.c mode_thresholds and the +// voice/music bandwidth tables blended by voice_est) since lane opus-mode; +// matched against ffmpeg libopus on real speech in lanes/opus-mode-r1.sweep.txt. fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { #[derive(Clone, Copy, Debug)] struct Case { @@ -945,8 +948,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { mode_request: None, bandwidth_request: None, mode: Mode::Silk, - bandwidth: Bandwidth::Narrow, - config: 1, + bandwidth: Bandwidth::Wide, + config: 9, code: 0, frames: 1, }, @@ -958,8 +961,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { mode_request: None, bandwidth_request: None, mode: Mode::Silk, - bandwidth: Bandwidth::Medium, - config: 5, + bandwidth: Bandwidth::Wide, + config: 9, code: 0, frames: 1, }, @@ -970,9 +973,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { frame: 960, mode_request: None, bandwidth_request: None, - mode: Mode::Silk, - bandwidth: Bandwidth::Medium, - config: 5, + mode: Mode::Hybrid, + bandwidth: Bandwidth::Full, + config: 15, code: 0, frames: 1, }, @@ -983,9 +986,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { frame: 960, mode_request: None, bandwidth_request: None, - mode: Mode::Silk, - bandwidth: Bandwidth::Wide, - config: 9, + mode: Mode::Hybrid, + bandwidth: Bandwidth::Full, + config: 15, code: 0, frames: 1, }, @@ -997,8 +1000,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { mode_request: None, bandwidth_request: None, mode: Mode::Hybrid, - bandwidth: Bandwidth::SuperWide, - config: 13, + bandwidth: Bandwidth::Full, + config: 15, code: 0, frames: 1, }, @@ -1022,9 +1025,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { frame: 960, mode_request: None, bandwidth_request: None, - mode: Mode::Celt, + mode: Mode::Hybrid, bandwidth: Bandwidth::Full, - config: 31, + config: 15, code: 0, frames: 1, }, @@ -1035,9 +1038,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { frame: 960, mode_request: None, bandwidth_request: None, - mode: Mode::Celt, - bandwidth: Bandwidth::SuperWide, - config: 27, + mode: Mode::Hybrid, + bandwidth: Bandwidth::Full, + config: 15, code: 0, frames: 1, }, @@ -1062,8 +1065,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { mode_request: None, bandwidth_request: None, mode: Mode::Celt, - bandwidth: Bandwidth::SuperWide, - config: 27, + bandwidth: Bandwidth::Full, + config: 31, code: 0, frames: 1, }, @@ -1074,9 +1077,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { frame: 960, mode_request: None, bandwidth_request: None, - mode: Mode::Silk, - bandwidth: Bandwidth::Narrow, - config: 1, + mode: Mode::Hybrid, + bandwidth: Bandwidth::Full, + config: 15, code: 0, frames: 1, }, @@ -1087,9 +1090,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { frame: 960, mode_request: None, bandwidth_request: None, - mode: Mode::Silk, - bandwidth: Bandwidth::Narrow, - config: 1, + mode: Mode::Hybrid, + bandwidth: Bandwidth::Full, + config: 15, code: 0, frames: 1, }, @@ -1100,9 +1103,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { frame: 960, mode_request: None, bandwidth_request: None, - mode: Mode::Silk, - bandwidth: Bandwidth::Medium, - config: 5, + mode: Mode::Hybrid, + bandwidth: Bandwidth::Full, + config: 15, code: 0, frames: 1, }, @@ -1113,9 +1116,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { frame: 960, mode_request: None, bandwidth_request: None, - mode: Mode::Silk, - bandwidth: Bandwidth::Wide, - config: 9, + mode: Mode::Hybrid, + bandwidth: Bandwidth::Full, + config: 15, code: 0, frames: 1, }, @@ -1127,8 +1130,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { mode_request: None, bandwidth_request: None, mode: Mode::Hybrid, - bandwidth: Bandwidth::SuperWide, - config: 13, + bandwidth: Bandwidth::Full, + config: 15, code: 0, frames: 1, }, @@ -1139,9 +1142,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { frame: 960, mode_request: None, bandwidth_request: None, - mode: Mode::Hybrid, + mode: Mode::Celt, bandwidth: Bandwidth::Full, - config: 15, + config: 31, code: 0, frames: 1, }, @@ -1165,9 +1168,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { frame: 480, mode_request: None, bandwidth_request: None, - mode: Mode::Silk, - bandwidth: Bandwidth::Medium, - config: 4, + mode: Mode::Hybrid, + bandwidth: Bandwidth::Full, + config: 14, code: 0, frames: 1, }, @@ -1179,8 +1182,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { mode_request: None, bandwidth_request: None, mode: Mode::Silk, - bandwidth: Bandwidth::Medium, - config: 5, + bandwidth: Bandwidth::Wide, + config: 9, code: 3, frames: 2, }, @@ -1192,8 +1195,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { mode_request: None, bandwidth_request: None, mode: Mode::Silk, - bandwidth: Bandwidth::Medium, - config: 5, + bandwidth: Bandwidth::Wide, + config: 9, code: 3, frames: 3, }, @@ -1205,8 +1208,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { mode_request: None, bandwidth_request: None, mode: Mode::Silk, - bandwidth: Bandwidth::Medium, - config: 6, + bandwidth: Bandwidth::Wide, + config: 10, code: 0, frames: 1, }, @@ -3378,7 +3381,7 @@ fn silk_library_gate_vs_libopus() { let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes"); fs::create_dir_all(&lanes_dir).unwrap(); - let sweep_path = lanes_dir.join("opus-silk-r2.sweep.txt"); + let sweep_path = lanes_dir.join("opus-mode-r1.sweep.txt"); let ref_ogg = temp_path("opus-silk-r2-ref.opus"); let ours_ogg = temp_path("opus-silk-r2-ours.opus"); @@ -3392,10 +3395,13 @@ fn silk_library_gate_vs_libopus() { // cutoff: NB 4 kHz, WB 8 kHz, hybrid-FB unfiltered (None) — compare each // encoder only against the band it can actually reproduce. let rows: &[(&str, u32, Option, Option, Option)] = &[ - ("12k-NB", 12, None, None, Some(4000.0)), - ("16k-NB", 16, Some(Bandwidth::Narrow), None, Some(4000.0)), - ("24k-WB", 24, Some(Bandwidth::Wide), Some(Mode::Silk), Some(8000.0)), - ("32k-Hyb", 32, None, None, None), + // All rows auto since lane opus-mode: the selector is libopus's, so + // the mode must match the reference's (asserted below) and the + // comparison is full-band (no lowpass). + ("12k", 12, None, None, None), + ("16k", 16, None, None, None), + ("24k", 24, None, None, None), + ("32k", 32, None, None, None), ]; let mut out_rows: Vec = Vec::new(); @@ -3444,6 +3450,10 @@ fn silk_library_gate_vs_libopus() { .encode_float(&source_pcm[..FRAME * CHANNELS], FRAME, &mut toc_buf) .expect("encode probe"); let mode_label = toc_mode_label(toc_buf[0]); + assert_eq!( + ref_mode, mode_label, + "{tag}: mode mismatch ref={ref_mode} ours={mode_label} (MODE GATE)" + ); // Own-decoder roundtrip (range-coder invariant) + packet capture. let (ours_own_dec, ours_packets, ours_bytes) = diff --git a/lanes/opus-mode-r1.report.md b/lanes/opus-mode-r1.report.md new file mode 100644 index 00000000..56c0716f --- /dev/null +++ b/lanes/opus-mode-r1.report.md @@ -0,0 +1,23 @@ +# lane opus-mode r1 — libopus VoIP/Audio mode + bandwidth selector — KEEP + +## What +- `encoder.rs`: mode/bandwidth decision is now libopus 1.6's: `equiv_rate = bitrate·(90+complexity)/100` with the 10 ms frame-rate term, `mode_thresholds` blended by `voice_est` (VoIP 127, Audio 48 with analysis off → SILK below ≈63.2k / 17.6k), `MONO/STEREO_{VOICE,MUSIC}_BW` tables (pairs of threshold/width, NB↔MB 9000, WB↔SWB 13500/11000, SWB↔FB 14000/12000) blended by `voice_est²`, SILK-with-bandwidth>WB → Hybrid, 40/60 ms frames capped to SILK-WB, Fs ceiling. Old hand thresholds removed. +- Unit tests + the 24-case `encoder_mode_follows_...` table re-derived from the actual run (18 cases changed: e.g. VoIP 12k → SILK-WB not NB, 16k → Hybrid-FB, Audio 16k → Hybrid-FB). Builder's hand table had 8k-Audio wrong (is NB); actual run is the source. +- `silk_library_gate_vs_libopus`: rows now all-auto, full-band, with a MODE GATE assert (ours must pick the reference's mode). + +## Gate (real speech, mono VoIP, 120 s; gap = ref − ours, negative = ours ahead) +``` +12k: ref_mode=SILK-WB mode=SILK-WB, ref 12.0 ours 12.4 kbps (+3.3%), lag o=0 r=-2, corr_bl o=0.8081 r=0.8181 gap=+0.0101, corr_fb o=0.8081 r=0.8181, corr_owndec o=0.8081, Q o=-1269.32 r=-401.54, err_r +16k: ref_mode=Hybrid-FB mode=Hybrid-FB, ref 16.4 ours 18.6 kbps (+12.9%), lag o=0 r=-1, corr_bl o=0.8547 r=0.8206 gap=-0.0341, corr_fb o=0.8547 r=0.8206, corr_owndec o=0.8540, Q o=-1032.96 r=-416.69, +24k: ref_mode=Hybrid-FB mode=Hybrid-FB, ref 24.0 ours 24.3 kbps (+1.1%), lag o=0 r=-1, corr_bl o=0.8964 r=0.8409 gap=-0.0555, corr_fb o=0.8964 r=0.8409, corr_owndec o=0.8965, Q o=-1032.13 r=-367.32, e +32k: ref_mode=Hybrid-FB mode=Hybrid-FB, ref 31.8 ours 31.6 kbps (-0.5%), lag o=0 r=-1, corr_bl o=0.9279 r=0.8541 gap=-0.0738, corr_fb o=0.9279 r=0.8541, corr_owndec o=0.9279, Q o=-940.18 r=-434.68, er +RATE GATE violations (|rate%| > 5): +LAG GATE: passed (no lag at scan bound) +DROPOUT GATE: passed (no ours-only dropouts) +``` +MODE GATE: 4/4 match. Like-for-like residue now visible: 12k SILK-WB ours behind on both metrics (gap +.0101, err_ratio 11.4); 16k hybrid rate +12.9% (RATE GATE is report-only — SILK part unbudgeted, encoder.rs D2b); 24k/32k hybrid corr ahead (−.056/−.074) but err_ratio 7.0/4.3 (metric split, see debt). + +## Suite +test result: ok. 34 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.02s +test result: ok. 27 passed; 0 failed; 10 ignored; 0 measured; 0 filtered out; finished in 34.14s +14-row CELT gate unaffected by construction: Audio at 64/96k is far above the 17.6k Audio SILK threshold (CELT both before and after). diff --git a/lanes/opus-mode-r1.sweep.txt b/lanes/opus-mode-r1.sweep.txt new file mode 100644 index 00000000..2801eb83 --- /dev/null +++ b/lanes/opus-mode-r1.sweep.txt @@ -0,0 +1,11 @@ +# ec-opus SILK/speech-rate gate vs ffmpeg libopus (r2) +# mono, 120s cap, VBR, 48kHz, Application::Voip; source reelm.wav +# r2: symmetric ffmpeg-libopus decode (both sides), lag gate, band-limited ref, ref mode +12k: ref_mode=SILK-WB mode=SILK-WB, ref 12.0 ours 12.4 kbps (+3.3%), lag o=0 r=-2, corr_bl o=0.8081 r=0.8181 gap=+0.0101, corr_fb o=0.8081 r=0.8181, corr_owndec o=0.8081, Q o=-1269.32 r=-401.54, err_ratio 11.390, minsec o=0.3967 r=0.2079, drop o=76 r=69 +16k: ref_mode=Hybrid-FB mode=Hybrid-FB, ref 16.4 ours 18.6 kbps (+12.9%), lag o=0 r=-1, corr_bl o=0.8547 r=0.8206 gap=-0.0341, corr_fb o=0.8547 r=0.8206, corr_owndec o=0.8540, Q o=-1032.96 r=-416.69, err_ratio 5.895, minsec o=0.5327 r=0.2861, drop o=60 r=73 +24k: ref_mode=Hybrid-FB mode=Hybrid-FB, ref 24.0 ours 24.3 kbps (+1.1%), lag o=0 r=-1, corr_bl o=0.8964 r=0.8409 gap=-0.0555, corr_fb o=0.8964 r=0.8409, corr_owndec o=0.8965, Q o=-1032.13 r=-367.32, err_ratio 6.990, minsec o=0.5900 r=0.3300, drop o=49 r=71 +32k: ref_mode=Hybrid-FB mode=Hybrid-FB, ref 31.8 ours 31.6 kbps (-0.5%), lag o=0 r=-1, corr_bl o=0.9279 r=0.8541 gap=-0.0738, corr_fb o=0.9279 r=0.8541, corr_owndec o=0.9279, Q o=-940.18 r=-434.68, err_ratio 4.346, minsec o=0.6283 r=0.3680, drop o=32 r=65 +RATE GATE violations (|rate%| > 5): + 16k: rate +12.9% (|>5%) +LAG GATE: passed (no lag at scan bound) +DROPOUT GATE: passed (no ours-only dropouts)