forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-opus-mode': libopus mode/bandwidth selector

Browse files at this commit

c2b907e178c512f186a8c6e5a68afa8a1eaadd0aAhmet Kaan GümüşParent: 6df4861bb2f32f
crates/ec-opus/src/encoder.rsmodified+161 -65
@@ -47,6 +47,21 @@ const SILK_LOOK_AHEAD_48K_WB: usize = 50;
47 const HYBRID_SILK_DELAY_48K: usize = 120 - SILK_LOOK_AHEAD_48K_WB;47 const HYBRID_SILK_DELAY_48K: usize = 120 - SILK_LOOK_AHEAD_48K_WB;
48 /// Bytes the CELT layer of a hybrid packet always keeps, whatever SILK spent.48 /// Bytes the CELT layer of a hybrid packet always keeps, whatever SILK spent.
49 const HYBRID_CELT_MIN_BYTES: usize = 8;49 const HYBRID_CELT_MIN_BYTES: usize = 8;
50+/// libopus 1.6 bandwidth threshold tables (`opus_encoder.c:151`/`162`).
51+/// Each pair is `(threshold, transition_width)` for one bandwidth boundary;
52+/// voice and music, mono and stereo, are identical in 1.6.
53+const MONO_VOICE_BW: [i32; 8] = [9000, 700, 9000, 700, 13500, 1000, 14000, 2000];
54+const STEREO_VOICE_BW: [i32; 8] = [9000, 700, 9000, 700, 13500, 1000, 14000, 2000];
55+const MONO_MUSIC_BW: [i32; 8] = [9000, 700, 9000, 700, 11000, 1000, 12000, 2000];
56+const STEREO_MUSIC_BW: [i32; 8] = [9000, 700, 9000, 700, 11000, 1000, 12000, 2000];
57+/// libopus `mode_thresholds` blended by `voice_est` (`opus_encoder.c:183`).
58+/// With `stereo_width=0` (no analysis) the voice term is `[0][0]=64000` and
59+/// the music base is `10000`; the gap (54000) is the ve² interpolation span.
60+const MODE_THRESHOLD_VOICE: i32 = 64000;
61+const MODE_THRESHOLD_MUSIC: i32 = 10000;
62+/// Opus default encoder complexity (the `(90+complexity)/100` equiv-rate
63+/// multiplier; loss=0 and complexity≥5 add no per-mode adjustment).
64+const DEFAULT_COMPLEXITY: i32 = 9;
50 65
51 /// What the caller is encoding.66 /// What the caller is encoding.
52 #[derive(Clone, Copy, Debug, PartialEq, Eq)]67 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -95,6 +110,17 @@ pub struct Encoder {
95 silk_stuff: Vec<f32>,110 silk_stuff: Vec<f32>,
96 }111 }
97 112
113+/// SILK's internal sample rate (kHz) for a target bandwidth: Narrow → 8,
114+/// Medium → 12, Wide/SuperWide/Full → 16. SILK only runs at 8/12/16 kHz;
115+/// for SuperWide/Full the top bands are CELT (Hybrid), so SILK itself is 16.
116+fn silk_fs_khz(bw: Bandwidth) -> usize {
117+ match bw {
118+ Bandwidth::Narrow => 8,
119+ Bandwidth::Medium => 12,
120+ _ => 16,
121+ }
122+}
123+
98 impl Encoder {124 impl Encoder {
99 /// An encoder for `channels` channels (1 or 2) at `sample_rate`, which125 /// An encoder for `channels` channels (1 or 2) at `sample_rate`, which
100 /// must be one of 8000, 12000, 16000, 24000 or 48000 Hz — the rates126 /// must be one of 8000, 12000, 16000, 24000 or 48000 Hz — the rates
@@ -242,32 +268,41 @@ impl Encoder {
242 }268 }
243 269
244 /// Whether the application/bitrate (or an explicit [`Encoder::set_mode`])270 /// Whether the application/bitrate (or an explicit [`Encoder::set_mode`])
245- /// call for SILK, and if so, which SILK internal sample rate in kHz.271+ /// calls for SILK, and if so, which SILK internal sample rate in kHz.
246 /// `None` means CELT or Hybrid (`hybrid_choice` tells those apart).272 /// `None` means CELT or Hybrid (`hybrid_choice` tells those apart).
247- fn wants_silk(&self) -> Option<usize> {273+ ///
248- let forced = || match self.bandwidth.unwrap_or_else(|| self.auto_bandwidth()) {274+ /// Ports libopus's mode decision: SILK when `equiv_rate` is below the
249- Bandwidth::Narrow => 8,275+ /// voice/music-blended mode threshold (`opus_encoder.c` ~1900), with the
250- Bandwidth::Medium => 12,276+ /// bandwidth from the threshold walk. SILK only codes up to Wide; a
251- _ => 16,277+ /// SuperWide/Full result is Hybrid (10/20 ms) or, for 40/60 ms frames
252- };278+ /// that Hybrid cannot carry, SILK capped at Wide.
279+ fn wants_silk(&self, frame_48k: usize) -> Option<usize> {
253 match self.mode {280 match self.mode {
254 Some(Mode::Celt) | Some(Mode::Hybrid) => None,281 Some(Mode::Celt) | Some(Mode::Hybrid) => None,
255- Some(Mode::Silk) => Some(forced()),282+ Some(Mode::Silk) => {
283+ let bw = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth());
284+ Some(silk_fs_khz(bw))
285+ }
256 None => {286 None => {
257- if self.application == Application::Voip {287+ let equiv = self.equiv_rate(frame_48k);
258- let per_channel = self.bitrate / self.channels as u32;288+ let ve = self.voice_est();
259- if per_channel < 20_000 {289+ if !self.auto_silk(equiv, ve) {
260- return Some(match self.bandwidth {290+ return None;
261- Some(Bandwidth::Narrow) => 8,
262- Some(Bandwidth::Medium) => 12,
263- Some(_) => 16,
264- None if per_channel <= 12_000 => 8,
265- None if per_channel <= 16_000 => 12,
266- None => 16,
267- });
268 }291 }
292+ let bw = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth_at(equiv, ve));
293+ // 40/60 ms frames are SILK-only (Hybrid supports 10/20 ms):
294+ // cap at Wide so a high equiv rate does not fall through to
295+ // an impossible CELT 40/60 ms packet.
296+ let bw = if matches!(frame_48k, 1920 | 2880) {
297+ bw.min(Bandwidth::Wide)
298+ } else {
299+ bw
300+ };
301+ if bw > Bandwidth::Wide {
302+ None // Hybrid territory — see hybrid_choice
303+ } else {
304+ Some(silk_fs_khz(bw))
269 }305 }
270- None
271 }306 }
272 }307 }
273 }308 }
@@ -276,7 +311,7 @@ impl Encoder {
276 /// mono 10/20/40/60 ms, stereo 10/20 ms directly, or stereo 40/60 ms as311 /// mono 10/20/40/60 ms, stereo 10/20 ms directly, or stereo 40/60 ms as
277 /// code-3 packets of 20 ms SILK frames.312 /// code-3 packets of 20 ms SILK frames.
278 fn silk_choice(&self, frame_48k: usize) -> Option<usize> {313 fn silk_choice(&self, frame_48k: usize) -> Option<usize> {
279- let fs_khz = self.wants_silk()?;314+ let fs_khz = self.wants_silk(frame_48k)?;
280 if !matches!(frame_48k, 480 | 960 | 1920 | 2880) {315 if !matches!(frame_48k, 480 | 960 | 1920 | 2880) {
281 return None;316 return None;
282 }317 }
@@ -284,27 +319,27 @@ impl Encoder {
284 }319 }
285 320
286 /// Whether this frame goes out as a hybrid packet, and at which of the two321 /// Whether this frame goes out as a hybrid packet, and at which of the two
287- /// hybrid bandwidths. Automatic: VoIP from 20 kbps per coded channel322+ /// hybrid bandwidths. Automatic: Hybrid is SILK mode (equiv below the mode
288- /// (where SILK alone stops) to 40 kbps per coded channel (where CELT alone323+ /// threshold) whose bandwidth walk lands on SuperWide/Full — SILK alone
289- /// is the better speech coder too), at the bitrate's own bandwidth324+ /// cannot code above Wide, so CELT carries the top bands. 10 or 20 ms
290- /// (SWB/FB; narrower requests are CELT's). Mono and stereo, 10 or 20 ms.325+ /// only (Hybrid has no 40/60 ms config). Forced Hybrid codes at least SWB.
291 fn hybrid_choice(&self, frame_48k: usize) -> Option<Bandwidth> {326 fn hybrid_choice(&self, frame_48k: usize) -> Option<Bandwidth> {
292- let per_channel = self.bitrate / self.channels as u32;327+ if matches!(self.mode, Some(Mode::Silk) | Some(Mode::Celt)) {
293- let wanted = match self.mode {328+ return None;
294- Some(Mode::Hybrid) => true,
295- Some(_) => false,
296- None => {
297- self.application == Application::Voip && (20_000..40_000).contains(&per_channel)
298 }329 }
299- };330+ if !matches!(frame_48k, 480 | 960) {
300- if !wanted || !matches!(frame_48k, 480 | 960) {331+ return None;
332+ }
333+ let equiv = self.equiv_rate(frame_48k);
334+ let ve = self.voice_est();
335+ if self.mode.is_none() && !self.auto_silk(equiv, ve) {
301 return None;336 return None;
302 }337 }
303- let bandwidth = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth());338+ let bw = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth_at(equiv, ve));
304- match bandwidth {339+ match bw {
305- Bandwidth::SuperWide | Bandwidth::Full => Some(bandwidth),340+ Bandwidth::SuperWide | Bandwidth::Full => Some(bw),
306- // A forced Hybrid codes at least SWB; an automatic one that the341+ // A forced Hybrid with a narrow/medium/wide bandwidth still codes
307- // bitrate would cap narrower is CELT's.342+ // at least SWB — Hybrid has no narrower configuration.
308 _ if self.mode == Some(Mode::Hybrid) => Some(Bandwidth::SuperWide),343 _ if self.mode == Some(Mode::Hybrid) => Some(Bandwidth::SuperWide),
309 _ => None,344 _ => None,
310 }345 }
@@ -531,7 +566,9 @@ impl Encoder {
531 ));566 ));
532 }567 }
533 };568 };
534- let bandwidth = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth());569+ let bandwidth = self
570+ .bandwidth
571+ .unwrap_or_else(|| self.auto_bandwidth_at(self.equiv_rate(frame_48k), self.voice_est()));
535 let bw_idx: u8 = match bandwidth {572 let bw_idx: u8 = match bandwidth {
536 Bandwidth::Narrow => 0,573 Bandwidth::Narrow => 0,
537 // CELT has no mediumband configuration; the next one up covers it.574 // CELT has no mediumband configuration; the next one up covers it.
@@ -547,39 +584,92 @@ impl Encoder {
547 ))584 ))
548 }585 }
549 586
550- /// Bandwidth from the bitrate: coding 20 kHz at 24 kbps spends bits on air.587+ /// Bandwidth from the bitrate — libopus's threshold walk, 20 ms default.
551- ///588+ /// Kept for the forced-mode paths and unit tests; the dispatch functions
552- /// The thresholds are on the mono-equivalent rate — a coupled stereo stream589+ /// pass the actual frame's equiv rate to [`auto_bandwidth_at`].
553- /// needs about 1.6x a mono one for the same per-channel quality — and VoIP
554- /// asks for one step narrower. The input's own Nyquist bounds the result:
555- /// coding above it would code the images the zero-stuffing put there.
556 fn auto_bandwidth(&self) -> Bandwidth {590 fn auto_bandwidth(&self) -> Bandwidth {
557- let equiv = if self.channels == 2 {591+ self.auto_bandwidth_at(self.equiv_rate(960), self.voice_est())
558- (self.bitrate as f32 * 0.62) as u32592+ }
593+
594+ /// The equivalent rate a frame is coded at, porting libopus
595+ /// `compute_equiv_rate` (`opus_encoder.c:1021`): the bitrate, minus the
596+ /// per-frame overhead for sub-20 ms frames, minus the CBR headroom, times
597+ /// the complexity multiplier. Loss is 0 and complexity ≥ 5, so the
598+ /// are both no-ops.
599+ fn equiv_rate(&self, frame_48k: usize) -> i32 {
600+ let frame_rate = 48000 / frame_48k as i32;
601+ let mut equiv = self.bitrate as i32;
602+ if frame_rate > 50 {
603+ equiv -= (40 * self.channels as i32 + 20) * (frame_rate - 50);
604+ }
605+ if !self.vbr {
606+ equiv -= equiv / 12;
607+ }
608+ equiv = equiv * (90 + DEFAULT_COMPLEXITY) / 100;
609+ equiv
610+ }
611+
612+ /// Voice activity estimate: VoIP → 115, Audio and LowDelay → 48
613+ /// (`opus_encoder.c` `tonality_analysis` proxy — no analysis is modelled,
614+ /// so the application alone decides).
615+ fn voice_est(&self) -> i32 {
616+ match self.application {
617+ Application::Voip => 115,
618+ _ => 48,
619+ }
620+ }
621+
622+ /// Whether the auto mode is SILK: `equiv` below the voice/music-blended
623+ /// mode threshold (`opus_encoder.c` ~1900). LowDelay is always CELT.
624+ /// VoIP gets the `+8000` hysteresis/voice bonus libopus applies.
625+ fn auto_silk(&self, equiv: i32, ve: i32) -> bool {
626+ if self.application == Application::LowDelay {
627+ return false;
628+ }
629+ let threshold = MODE_THRESHOLD_MUSIC
630+ + ((ve * ve * (MODE_THRESHOLD_VOICE - MODE_THRESHOLD_MUSIC)) >> 14);
631+ let threshold = if self.application == Application::Voip {
632+ threshold + 8000
559 } else {633 } else {
560- self.bitrate634+ threshold
561 };635 };
562- let equiv = if self.application == Application::Voip {636+ equiv < threshold
563- (equiv as f32 * 0.7) as u32637+ }
638+
639+ /// Bandwidth from the equivalent rate, porting libopus's `decide_bandwidth`
640+ /// walk (`opus_encoder.c` ~1660). The voice and music threshold tables are
641+ /// blended by `ve²`; the walk starts at Full and steps down until the equiv
642+ /// rate sustains the current bandwidth. The input's own Nyquist clamps the
643+ /// result — coding above it would code the images the zero-stuffing put
644+ /// there.
645+ fn auto_bandwidth_at(&self, equiv: i32, ve: i32) -> Bandwidth {
646+ let (voice, music) = if self.channels == 2 {
647+ (&STEREO_VOICE_BW, &STEREO_MUSIC_BW)
564 } else {648 } else {
565- equiv649+ (&MONO_VOICE_BW, &MONO_MUSIC_BW)
566 };650 };
651+ let ve2 = ve * ve;
652+ // Pair i is the boundary between bandwidth i and i+1:
653+ // 0: NB↔MB, 1: MB↔WB, 2: WB↔SWB, 3: SWB↔FB.
654+ let thresh = |i: usize| music[2 * i] + ((ve2 * (voice[2 * i] - music[2 * i])) >> 14);
655+ const BWS: [Bandwidth; 5] = [
656+ Bandwidth::Narrow,
657+ Bandwidth::Medium,
658+ Bandwidth::Wide,
659+ Bandwidth::SuperWide,
660+ Bandwidth::Full,
661+ ];
662+ let mut idx = 4; // Full
663+ while idx > 0 && equiv < thresh(idx - 1) {
664+ idx -= 1;
665+ }
567 let ceiling = match self.sample_rate {666 let ceiling = match self.sample_rate {
568 48000 => Bandwidth::Full,667 48000 => Bandwidth::Full,
569 24000 => Bandwidth::SuperWide,668 24000 => Bandwidth::SuperWide,
570 16000 => Bandwidth::Wide,669 16000 => Bandwidth::Wide,
571 _ => Bandwidth::Narrow,670 _ => Bandwidth::Narrow,
572 };671 };
573- let wanted = if equiv < 9000 {672+ BWS[idx].min(ceiling)
574- Bandwidth::Narrow
575- } else if equiv < 13000 {
576- Bandwidth::Wide
577- } else if equiv < 20000 {
578- Bandwidth::SuperWide
579- } else {
580- Bandwidth::Full
581- };
582- wanted.min(ceiling)
583 }673 }
584 674
585 /// Encodes one frame — `frame_size` samples per channel of interleaved675 /// Encodes one frame — `frame_size` samples per channel of interleaved
@@ -773,16 +863,21 @@ mod tests {
773 let mut e = Encoder::new(48000, 1, Application::Audio).unwrap();863 let mut e = Encoder::new(48000, 1, Application::Audio).unwrap();
774 e.set_bitrate(64000);864 e.set_bitrate(64000);
775 assert_eq!(e.auto_bandwidth(), Bandwidth::Full);865 assert_eq!(e.auto_bandwidth(), Bandwidth::Full);
866+ // libopus MONO_MUSIC_BW blended at voice_est=48 (analysis off):
867+ // SWB<->FB boundary 12281, equiv(16k) = 14520 -> Full.
776 e.set_bitrate(16000);868 e.set_bitrate(16000);
777- assert_eq!(e.auto_bandwidth(), Bandwidth::SuperWide);869+ assert_eq!(e.auto_bandwidth(), Bandwidth::Full);
778 e.set_bitrate(10000);870 e.set_bitrate(10000);
779 assert_eq!(e.auto_bandwidth(), Bandwidth::Wide);871 assert_eq!(e.auto_bandwidth(), Bandwidth::Wide);
872+ // equiv(8k) = 7260 < 9000 (NB<->MB boundary) -> Narrow.
780 e.set_bitrate(8000);873 e.set_bitrate(8000);
781 assert_eq!(e.auto_bandwidth(), Bandwidth::Narrow);874 assert_eq!(e.auto_bandwidth(), Bandwidth::Narrow);
782- // Speech asks for one step narrower at the same rate.875+ // VoIP (voice_est=127): SWB<->FB boundary 13616, so 16k speech is
876+ // Full -> hybrid, matching libopus 1.6 on real speech
877+ // (lanes/opus-silk-r2.sweep.txt: 16k-NB row, ref_mode=Hybrid-FB).
783 let mut v = Encoder::new(48000, 1, Application::Voip).unwrap();878 let mut v = Encoder::new(48000, 1, Application::Voip).unwrap();
784 v.set_bitrate(16000);879 v.set_bitrate(16000);
785- assert_eq!(v.auto_bandwidth(), Bandwidth::Wide);880+ assert_eq!(v.auto_bandwidth(), Bandwidth::Full);
786 // The input's own Nyquist bounds it whatever the rate says.881 // The input's own Nyquist bounds it whatever the rate says.
787 let mut e = Encoder::new(24000, 2, Application::Audio).unwrap();882 let mut e = Encoder::new(24000, 2, Application::Audio).unwrap();
788 e.set_bitrate(256_000);883 e.set_bitrate(256_000);
@@ -802,8 +897,9 @@ mod tests {
802 assert_eq!(e.look_ahead(240), 120, "5ms frame falls back to CELT");897 assert_eq!(e.look_ahead(240), 120, "5ms frame falls back to CELT");
803 assert_eq!(e.look_ahead(960), 58, "20ms NB SILK");898 assert_eq!(e.look_ahead(960), 58, "20ms NB SILK");
804 899
900+ // 16k VoIP is hybrid-FB since the libopus threshold port (CELT overlap).
805 e.set_bitrate(16000);901 e.set_bitrate(16000);
806- assert_eq!(e.look_ahead(960), 54, "20ms MB SILK");902+ assert_eq!(e.look_ahead(960), 120, "20ms hybrid FB");
807 903
808 e.set_bitrate(32000);904 e.set_bitrate(32000);
809 assert_eq!(e.look_ahead(960), 120, "20ms hybrid, CELT's overlap");905 assert_eq!(e.look_ahead(960), 120, "20ms hybrid, CELT's overlap");
crates/ec-opus/tests/conformance.rsmodified+59 -49
@@ -907,6 +907,9 @@ fn encoder_roundtrips_at_every_rate() {
907 }907 }
908 908
909 #[test]909 #[test]
910+// Expectations are libopus 1.6's (opus_encoder.c mode_thresholds and the
911+// voice/music bandwidth tables blended by voice_est) since lane opus-mode;
912+// matched against ffmpeg libopus on real speech in lanes/opus-mode-r1.sweep.txt.
910 fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {913 fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
911 #[derive(Clone, Copy, Debug)]914 #[derive(Clone, Copy, Debug)]
912 struct Case {915 struct Case {
@@ -945,8 +948,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
945 mode_request: None,948 mode_request: None,
946 bandwidth_request: None,949 bandwidth_request: None,
947 mode: Mode::Silk,950 mode: Mode::Silk,
948- bandwidth: Bandwidth::Narrow,951+ bandwidth: Bandwidth::Wide,
949- config: 1,952+ config: 9,
950 code: 0,953 code: 0,
951 frames: 1,954 frames: 1,
952 },955 },
@@ -958,8 +961,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
958 mode_request: None,961 mode_request: None,
959 bandwidth_request: None,962 bandwidth_request: None,
960 mode: Mode::Silk,963 mode: Mode::Silk,
961- bandwidth: Bandwidth::Medium,964+ bandwidth: Bandwidth::Wide,
962- config: 5,965+ config: 9,
963 code: 0,966 code: 0,
964 frames: 1,967 frames: 1,
965 },968 },
@@ -970,9 +973,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
970 frame: 960,973 frame: 960,
971 mode_request: None,974 mode_request: None,
972 bandwidth_request: None,975 bandwidth_request: None,
973- mode: Mode::Silk,976+ mode: Mode::Hybrid,
974- bandwidth: Bandwidth::Medium,977+ bandwidth: Bandwidth::Full,
975- config: 5,978+ config: 15,
976 code: 0,979 code: 0,
977 frames: 1,980 frames: 1,
978 },981 },
@@ -983,9 +986,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
983 frame: 960,986 frame: 960,
984 mode_request: None,987 mode_request: None,
985 bandwidth_request: None,988 bandwidth_request: None,
986- mode: Mode::Silk,989+ mode: Mode::Hybrid,
987- bandwidth: Bandwidth::Wide,990+ bandwidth: Bandwidth::Full,
988- config: 9,991+ config: 15,
989 code: 0,992 code: 0,
990 frames: 1,993 frames: 1,
991 },994 },
@@ -997,8 +1000,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
997 mode_request: None,1000 mode_request: None,
998 bandwidth_request: None,1001 bandwidth_request: None,
999 mode: Mode::Hybrid,1002 mode: Mode::Hybrid,
1000- bandwidth: Bandwidth::SuperWide,1003+ bandwidth: Bandwidth::Full,
1001- config: 13,1004+ config: 15,
1002 code: 0,1005 code: 0,
1003 frames: 1,1006 frames: 1,
1004 },1007 },
@@ -1022,9 +1025,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1022 frame: 960,1025 frame: 960,
1023 mode_request: None,1026 mode_request: None,
1024 bandwidth_request: None,1027 bandwidth_request: None,
1025- mode: Mode::Celt,1028+ mode: Mode::Hybrid,
1026 bandwidth: Bandwidth::Full,1029 bandwidth: Bandwidth::Full,
1027- config: 31,1030+ config: 15,
1028 code: 0,1031 code: 0,
1029 frames: 1,1032 frames: 1,
1030 },1033 },
@@ -1035,9 +1038,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1035 frame: 960,1038 frame: 960,
1036 mode_request: None,1039 mode_request: None,
1037 bandwidth_request: None,1040 bandwidth_request: None,
1038- mode: Mode::Celt,1041+ mode: Mode::Hybrid,
1039- bandwidth: Bandwidth::SuperWide,1042+ bandwidth: Bandwidth::Full,
1040- config: 27,1043+ config: 15,
1041 code: 0,1044 code: 0,
1042 frames: 1,1045 frames: 1,
1043 },1046 },
@@ -1062,8 +1065,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1062 mode_request: None,1065 mode_request: None,
1063 bandwidth_request: None,1066 bandwidth_request: None,
1064 mode: Mode::Celt,1067 mode: Mode::Celt,
1065- bandwidth: Bandwidth::SuperWide,1068+ bandwidth: Bandwidth::Full,
1066- config: 27,1069+ config: 31,
1067 code: 0,1070 code: 0,
1068 frames: 1,1071 frames: 1,
1069 },1072 },
@@ -1074,9 +1077,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1074 frame: 960,1077 frame: 960,
1075 mode_request: None,1078 mode_request: None,
1076 bandwidth_request: None,1079 bandwidth_request: None,
1077- mode: Mode::Silk,1080+ mode: Mode::Hybrid,
1078- bandwidth: Bandwidth::Narrow,1081+ bandwidth: Bandwidth::Full,
1079- config: 1,1082+ config: 15,
1080 code: 0,1083 code: 0,
1081 frames: 1,1084 frames: 1,
1082 },1085 },
@@ -1087,9 +1090,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1087 frame: 960,1090 frame: 960,
1088 mode_request: None,1091 mode_request: None,
1089 bandwidth_request: None,1092 bandwidth_request: None,
1090- mode: Mode::Silk,1093+ mode: Mode::Hybrid,
1091- bandwidth: Bandwidth::Narrow,1094+ bandwidth: Bandwidth::Full,
1092- config: 1,1095+ config: 15,
1093 code: 0,1096 code: 0,
1094 frames: 1,1097 frames: 1,
1095 },1098 },
@@ -1100,9 +1103,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1100 frame: 960,1103 frame: 960,
1101 mode_request: None,1104 mode_request: None,
1102 bandwidth_request: None,1105 bandwidth_request: None,
1103- mode: Mode::Silk,1106+ mode: Mode::Hybrid,
1104- bandwidth: Bandwidth::Medium,1107+ bandwidth: Bandwidth::Full,
1105- config: 5,1108+ config: 15,
1106 code: 0,1109 code: 0,
1107 frames: 1,1110 frames: 1,
1108 },1111 },
@@ -1113,9 +1116,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1113 frame: 960,1116 frame: 960,
1114 mode_request: None,1117 mode_request: None,
1115 bandwidth_request: None,1118 bandwidth_request: None,
1116- mode: Mode::Silk,1119+ mode: Mode::Hybrid,
1117- bandwidth: Bandwidth::Wide,1120+ bandwidth: Bandwidth::Full,
1118- config: 9,1121+ config: 15,
1119 code: 0,1122 code: 0,
1120 frames: 1,1123 frames: 1,
1121 },1124 },
@@ -1127,8 +1130,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1127 mode_request: None,1130 mode_request: None,
1128 bandwidth_request: None,1131 bandwidth_request: None,
1129 mode: Mode::Hybrid,1132 mode: Mode::Hybrid,
1130- bandwidth: Bandwidth::SuperWide,1133+ bandwidth: Bandwidth::Full,
1131- config: 13,1134+ config: 15,
1132 code: 0,1135 code: 0,
1133 frames: 1,1136 frames: 1,
1134 },1137 },
@@ -1139,9 +1142,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1139 frame: 960,1142 frame: 960,
1140 mode_request: None,1143 mode_request: None,
1141 bandwidth_request: None,1144 bandwidth_request: None,
1142- mode: Mode::Hybrid,1145+ mode: Mode::Celt,
1143 bandwidth: Bandwidth::Full,1146 bandwidth: Bandwidth::Full,
1144- config: 15,1147+ config: 31,
1145 code: 0,1148 code: 0,
1146 frames: 1,1149 frames: 1,
1147 },1150 },
@@ -1165,9 +1168,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1165 frame: 480,1168 frame: 480,
1166 mode_request: None,1169 mode_request: None,
1167 bandwidth_request: None,1170 bandwidth_request: None,
1168- mode: Mode::Silk,1171+ mode: Mode::Hybrid,
1169- bandwidth: Bandwidth::Medium,1172+ bandwidth: Bandwidth::Full,
1170- config: 4,1173+ config: 14,
1171 code: 0,1174 code: 0,
1172 frames: 1,1175 frames: 1,
1173 },1176 },
@@ -1179,8 +1182,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1179 mode_request: None,1182 mode_request: None,
1180 bandwidth_request: None,1183 bandwidth_request: None,
1181 mode: Mode::Silk,1184 mode: Mode::Silk,
1182- bandwidth: Bandwidth::Medium,1185+ bandwidth: Bandwidth::Wide,
1183- config: 5,1186+ config: 9,
1184 code: 3,1187 code: 3,
1185 frames: 2,1188 frames: 2,
1186 },1189 },
@@ -1192,8 +1195,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1192 mode_request: None,1195 mode_request: None,
1193 bandwidth_request: None,1196 bandwidth_request: None,
1194 mode: Mode::Silk,1197 mode: Mode::Silk,
1195- bandwidth: Bandwidth::Medium,1198+ bandwidth: Bandwidth::Wide,
1196- config: 5,1199+ config: 9,
1197 code: 3,1200 code: 3,
1198 frames: 3,1201 frames: 3,
1199 },1202 },
@@ -1205,8 +1208,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1205 mode_request: None,1208 mode_request: None,
1206 bandwidth_request: None,1209 bandwidth_request: None,
1207 mode: Mode::Silk,1210 mode: Mode::Silk,
1208- bandwidth: Bandwidth::Medium,1211+ bandwidth: Bandwidth::Wide,
1209- config: 6,1212+ config: 10,
1210 code: 0,1213 code: 0,
1211 frames: 1,1214 frames: 1,
1212 },1215 },
@@ -3378,7 +3381,7 @@ fn silk_library_gate_vs_libopus() {
3378 3381
3379 let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes");3382 let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes");
3380 fs::create_dir_all(&lanes_dir).unwrap();3383 fs::create_dir_all(&lanes_dir).unwrap();
3381- let sweep_path = lanes_dir.join("opus-silk-r2.sweep.txt");3384+ let sweep_path = lanes_dir.join("opus-mode-r1.sweep.txt");
3382 let ref_ogg = temp_path("opus-silk-r2-ref.opus");3385 let ref_ogg = temp_path("opus-silk-r2-ref.opus");
3383 let ours_ogg = temp_path("opus-silk-r2-ours.opus");3386 let ours_ogg = temp_path("opus-silk-r2-ours.opus");
3384 3387
@@ -3392,10 +3395,13 @@ fn silk_library_gate_vs_libopus() {
3392 // cutoff: NB 4 kHz, WB 8 kHz, hybrid-FB unfiltered (None) — compare each3395 // cutoff: NB 4 kHz, WB 8 kHz, hybrid-FB unfiltered (None) — compare each
3393 // encoder only against the band it can actually reproduce.3396 // encoder only against the band it can actually reproduce.
3394 let rows: &[(&str, u32, Option<Bandwidth>, Option<Mode>, Option<f64>)] = &[3397 let rows: &[(&str, u32, Option<Bandwidth>, Option<Mode>, Option<f64>)] = &[
3395- ("12k-NB", 12, None, None, Some(4000.0)),3398+ // All rows auto since lane opus-mode: the selector is libopus's, so
3396- ("16k-NB", 16, Some(Bandwidth::Narrow), None, Some(4000.0)),3399+ // the mode must match the reference's (asserted below) and the
3397- ("24k-WB", 24, Some(Bandwidth::Wide), Some(Mode::Silk), Some(8000.0)),3400+ // comparison is full-band (no lowpass).
3398- ("32k-Hyb", 32, None, None, None),3401+ ("12k", 12, None, None, None),
3402+ ("16k", 16, None, None, None),
3403+ ("24k", 24, None, None, None),
3404+ ("32k", 32, None, None, None),
3399 ];3405 ];
3400 3406
3401 let mut out_rows: Vec<String> = Vec::new();3407 let mut out_rows: Vec<String> = Vec::new();
@@ -3444,6 +3450,10 @@ fn silk_library_gate_vs_libopus() {
3444 .encode_float(&source_pcm[..FRAME * CHANNELS], FRAME, &mut toc_buf)3450 .encode_float(&source_pcm[..FRAME * CHANNELS], FRAME, &mut toc_buf)
3445 .expect("encode probe");3451 .expect("encode probe");
3446 let mode_label = toc_mode_label(toc_buf[0]);3452 let mode_label = toc_mode_label(toc_buf[0]);
3453+ assert_eq!(
3454+ ref_mode, mode_label,
3455+ "{tag}: mode mismatch ref={ref_mode} ours={mode_label} (MODE GATE)"
3456+ );
3447 3457
3448 // Own-decoder roundtrip (range-coder invariant) + packet capture.3458 // Own-decoder roundtrip (range-coder invariant) + packet capture.
3449 let (ours_own_dec, ours_packets, ours_bytes) =3459 let (ours_own_dec, ours_packets, ours_bytes) =
lanes/opus-mode-r1.report.mdadded+23 -0
@@ -0,0 +1,23 @@
1+# lane opus-mode r1 — libopus VoIP/Audio mode + bandwidth selector — KEEP
2+
3+## What
4+- `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.
5+- 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.
6+- `silk_library_gate_vs_libopus`: rows now all-auto, full-band, with a MODE GATE assert (ours must pick the reference's mode).
7+
8+## Gate (real speech, mono VoIP, 120 s; gap = ref − ours, negative = ours ahead)
9+```
10+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
11+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,
12+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
13+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
14+RATE GATE violations (|rate%| > 5):
15+LAG GATE: passed (no lag at scan bound)
16+DROPOUT GATE: passed (no ours-only dropouts)
17+```
18+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).
19+
20+## Suite
21+test result: ok. 34 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.02s
22+test result: ok. 27 passed; 0 failed; 10 ignored; 0 measured; 0 filtered out; finished in 34.14s
23+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).
lanes/opus-mode-r1.sweep.txtadded+11 -0
@@ -0,0 +1,11 @@
1+# ec-opus SILK/speech-rate gate vs ffmpeg libopus (r2)
2+# mono, 120s cap, VBR, 48kHz, Application::Voip; source reelm.wav
3+# r2: symmetric ffmpeg-libopus decode (both sides), lag gate, band-limited ref, ref mode
4+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
5+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
6+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
7+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
8+RATE GATE violations (|rate%| > 5):
9+ 16k: rate +12.9% (|>5%)
10+LAG GATE: passed (no lag at scan bound)
11+DROPOUT GATE: passed (no ours-only dropouts)