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+163 -67
@@ -47,6 +47,21 @@ const SILK_LOOK_AHEAD_48K_WB: usize = 50;
4747 const HYBRID_SILK_DELAY_48K: usize = 120 - SILK_LOOK_AHEAD_48K_WB;
4848 /// Bytes the CELT layer of a hybrid packet always keeps, whatever SILK spent.
4949 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;
5065
5166 /// What the caller is encoding.
5267 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -95,6 +110,17 @@ pub struct Encoder {
95110 silk_stuff: Vec<f32>,
96111 }
97112
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+
98124 impl Encoder {
99125 /// An encoder for `channels` channels (1 or 2) at `sample_rate`, which
100126 /// must be one of 8000, 12000, 16000, 24000 or 48000 Hz — the rates
@@ -242,32 +268,41 @@ impl Encoder {
242268 }
243269
244270 /// 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.
246272 /// `None` means CELT or Hybrid (`hybrid_choice` tells those apart).
247- fn wants_silk(&self) -> Option<usize> {
248- let forced = || match self.bandwidth.unwrap_or_else(|| self.auto_bandwidth()) {
249- Bandwidth::Narrow => 8,
250- Bandwidth::Medium => 12,
251- _ => 16,
252- };
273+ ///
274+ /// Ports libopus's mode decision: SILK when `equiv_rate` is below the
275+ /// voice/music-blended mode threshold (`opus_encoder.c` ~1900), with the
276+ /// bandwidth from the threshold walk. SILK only codes up to Wide; a
277+ /// SuperWide/Full result is Hybrid (10/20 ms) or, for 40/60 ms frames
278+ /// that Hybrid cannot carry, SILK capped at Wide.
279+ fn wants_silk(&self, frame_48k: usize) -> Option<usize> {
253280 match self.mode {
254281 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+ }
256286 None => {
257- if self.application == Application::Voip {
258- let per_channel = self.bitrate / self.channels as u32;
259- if per_channel < 20_000 {
260- return Some(match self.bandwidth {
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- }
287+ let equiv = self.equiv_rate(frame_48k);
288+ let ve = self.voice_est();
289+ if !self.auto_silk(equiv, ve) {
290+ return None;
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))
269305 }
270- None
271306 }
272307 }
273308 }
@@ -276,7 +311,7 @@ impl Encoder {
276311 /// mono 10/20/40/60 ms, stereo 10/20 ms directly, or stereo 40/60 ms as
277312 /// code-3 packets of 20 ms SILK frames.
278313 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)?;
280315 if !matches!(frame_48k, 480 | 960 | 1920 | 2880) {
281316 return None;
282317 }
@@ -284,27 +319,27 @@ impl Encoder {
284319 }
285320
286321 /// 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 channel
288- /// (where SILK alone stops) to 40 kbps per coded channel (where CELT alone
289- /// is the better speech coder too), at the bitrate's own bandwidth
290- /// (SWB/FB; narrower requests are CELT's). Mono and stereo, 10 or 20 ms.
322+ /// hybrid bandwidths. Automatic: Hybrid is SILK mode (equiv below the mode
323+ /// threshold) whose bandwidth walk lands on SuperWide/Full — SILK alone
324+ /// cannot code above Wide, so CELT carries the top bands. 10 or 20 ms
325+ /// only (Hybrid has no 40/60 ms config). Forced Hybrid codes at least SWB.
291326 fn hybrid_choice(&self, frame_48k: usize) -> Option<Bandwidth> {
292- let per_channel = self.bitrate / self.channels as u32;
293- let wanted = match self.mode {
294- Some(Mode::Hybrid) => true,
295- Some(_) => false,
296- None => {
297- self.application == Application::Voip && (20_000..40_000).contains(&per_channel)
298- }
299- };
300- if !wanted || !matches!(frame_48k, 480 | 960) {
327+ if matches!(self.mode, Some(Mode::Silk) | Some(Mode::Celt)) {
301328 return None;
302329 }
303- let bandwidth = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth());
304- match bandwidth {
305- Bandwidth::SuperWide | Bandwidth::Full => Some(bandwidth),
306- // A forced Hybrid codes at least SWB; an automatic one that the
307- // bitrate would cap narrower is CELT's.
330+ if !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) {
336+ return None;
337+ }
338+ let bw = self.bandwidth.unwrap_or_else(|| self.auto_bandwidth_at(equiv, ve));
339+ match bw {
340+ Bandwidth::SuperWide | Bandwidth::Full => Some(bw),
341+ // A forced Hybrid with a narrow/medium/wide bandwidth still codes
342+ // at least SWB — Hybrid has no narrower configuration.
308343 _ if self.mode == Some(Mode::Hybrid) => Some(Bandwidth::SuperWide),
309344 _ => None,
310345 }
@@ -531,7 +566,9 @@ impl Encoder {
531566 ));
532567 }
533568 };
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()));
535572 let bw_idx: u8 = match bandwidth {
536573 Bandwidth::Narrow => 0,
537574 // CELT has no mediumband configuration; the next one up covers it.
@@ -547,39 +584,92 @@ impl Encoder {
547584 ))
548585 }
549586
550- /// Bandwidth from the bitrate: coding 20 kHz at 24 kbps spends bits on air.
551- ///
552- /// The thresholds are on the mono-equivalent rate — a coupled stereo stream
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.
587+ /// Bandwidth from the bitrate — libopus's threshold walk, 20 ms default.
588+ /// Kept for the forced-mode paths and unit tests; the dispatch functions
589+ /// pass the actual frame's equiv rate to [`auto_bandwidth_at`].
556590 fn auto_bandwidth(&self) -> Bandwidth {
557- let equiv = if self.channels == 2 {
558- (self.bitrate as f32 * 0.62) as u32
591+ self.auto_bandwidth_at(self.equiv_rate(960), self.voice_est())
592+ }
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
559633 } else {
560- self.bitrate
634+ threshold
561635 };
562- let equiv = if self.application == Application::Voip {
563- (equiv as f32 * 0.7) as u32
636+ equiv < threshold
637+ }
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)
564648 } else {
565- equiv
649+ (&MONO_VOICE_BW, &MONO_MUSIC_BW)
566650 };
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+ }
567666 let ceiling = match self.sample_rate {
568667 48000 => Bandwidth::Full,
569668 24000 => Bandwidth::SuperWide,
570669 16000 => Bandwidth::Wide,
571670 _ => Bandwidth::Narrow,
572671 };
573- let wanted = if equiv < 9000 {
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)
672+ BWS[idx].min(ceiling)
583673 }
584674
585675 /// Encodes one frame — `frame_size` samples per channel of interleaved
@@ -773,16 +863,21 @@ mod tests {
773863 let mut e = Encoder::new(48000, 1, Application::Audio).unwrap();
774864 e.set_bitrate(64000);
775865 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.
776868 e.set_bitrate(16000);
777- assert_eq!(e.auto_bandwidth(), Bandwidth::SuperWide);
869+ assert_eq!(e.auto_bandwidth(), Bandwidth::Full);
778870 e.set_bitrate(10000);
779871 assert_eq!(e.auto_bandwidth(), Bandwidth::Wide);
872+ // equiv(8k) = 7260 < 9000 (NB<->MB boundary) -> Narrow.
780873 e.set_bitrate(8000);
781874 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).
783878 let mut v = Encoder::new(48000, 1, Application::Voip).unwrap();
784879 v.set_bitrate(16000);
785- assert_eq!(v.auto_bandwidth(), Bandwidth::Wide);
880+ assert_eq!(v.auto_bandwidth(), Bandwidth::Full);
786881 // The input's own Nyquist bounds it whatever the rate says.
787882 let mut e = Encoder::new(24000, 2, Application::Audio).unwrap();
788883 e.set_bitrate(256_000);
@@ -802,8 +897,9 @@ mod tests {
802897 assert_eq!(e.look_ahead(240), 120, "5ms frame falls back to CELT");
803898 assert_eq!(e.look_ahead(960), 58, "20ms NB SILK");
804899
900+ // 16k VoIP is hybrid-FB since the libopus threshold port (CELT overlap).
805901 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");
807903
808904 e.set_bitrate(32000);
809905 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() {
907907 }
908908
909909 #[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.
910913 fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
911914 #[derive(Clone, Copy, Debug)]
912915 struct Case {
@@ -945,8 +948,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
945948 mode_request: None,
946949 bandwidth_request: None,
947950 mode: Mode::Silk,
948- bandwidth: Bandwidth::Narrow,
949- config: 1,
951+ bandwidth: Bandwidth::Wide,
952+ config: 9,
950953 code: 0,
951954 frames: 1,
952955 },
@@ -958,8 +961,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
958961 mode_request: None,
959962 bandwidth_request: None,
960963 mode: Mode::Silk,
961- bandwidth: Bandwidth::Medium,
962- config: 5,
964+ bandwidth: Bandwidth::Wide,
965+ config: 9,
963966 code: 0,
964967 frames: 1,
965968 },
@@ -970,9 +973,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
970973 frame: 960,
971974 mode_request: None,
972975 bandwidth_request: None,
973- mode: Mode::Silk,
974- bandwidth: Bandwidth::Medium,
975- config: 5,
976+ mode: Mode::Hybrid,
977+ bandwidth: Bandwidth::Full,
978+ config: 15,
976979 code: 0,
977980 frames: 1,
978981 },
@@ -983,9 +986,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
983986 frame: 960,
984987 mode_request: None,
985988 bandwidth_request: None,
986- mode: Mode::Silk,
987- bandwidth: Bandwidth::Wide,
988- config: 9,
989+ mode: Mode::Hybrid,
990+ bandwidth: Bandwidth::Full,
991+ config: 15,
989992 code: 0,
990993 frames: 1,
991994 },
@@ -997,8 +1000,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
9971000 mode_request: None,
9981001 bandwidth_request: None,
9991002 mode: Mode::Hybrid,
1000- bandwidth: Bandwidth::SuperWide,
1001- config: 13,
1003+ bandwidth: Bandwidth::Full,
1004+ config: 15,
10021005 code: 0,
10031006 frames: 1,
10041007 },
@@ -1022,9 +1025,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
10221025 frame: 960,
10231026 mode_request: None,
10241027 bandwidth_request: None,
1025- mode: Mode::Celt,
1028+ mode: Mode::Hybrid,
10261029 bandwidth: Bandwidth::Full,
1027- config: 31,
1030+ config: 15,
10281031 code: 0,
10291032 frames: 1,
10301033 },
@@ -1035,9 +1038,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
10351038 frame: 960,
10361039 mode_request: None,
10371040 bandwidth_request: None,
1038- mode: Mode::Celt,
1039- bandwidth: Bandwidth::SuperWide,
1040- config: 27,
1041+ mode: Mode::Hybrid,
1042+ bandwidth: Bandwidth::Full,
1043+ config: 15,
10411044 code: 0,
10421045 frames: 1,
10431046 },
@@ -1062,8 +1065,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
10621065 mode_request: None,
10631066 bandwidth_request: None,
10641067 mode: Mode::Celt,
1065- bandwidth: Bandwidth::SuperWide,
1066- config: 27,
1068+ bandwidth: Bandwidth::Full,
1069+ config: 31,
10671070 code: 0,
10681071 frames: 1,
10691072 },
@@ -1074,9 +1077,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
10741077 frame: 960,
10751078 mode_request: None,
10761079 bandwidth_request: None,
1077- mode: Mode::Silk,
1078- bandwidth: Bandwidth::Narrow,
1079- config: 1,
1080+ mode: Mode::Hybrid,
1081+ bandwidth: Bandwidth::Full,
1082+ config: 15,
10801083 code: 0,
10811084 frames: 1,
10821085 },
@@ -1087,9 +1090,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
10871090 frame: 960,
10881091 mode_request: None,
10891092 bandwidth_request: None,
1090- mode: Mode::Silk,
1091- bandwidth: Bandwidth::Narrow,
1092- config: 1,
1093+ mode: Mode::Hybrid,
1094+ bandwidth: Bandwidth::Full,
1095+ config: 15,
10931096 code: 0,
10941097 frames: 1,
10951098 },
@@ -1100,9 +1103,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
11001103 frame: 960,
11011104 mode_request: None,
11021105 bandwidth_request: None,
1103- mode: Mode::Silk,
1104- bandwidth: Bandwidth::Medium,
1105- config: 5,
1106+ mode: Mode::Hybrid,
1107+ bandwidth: Bandwidth::Full,
1108+ config: 15,
11061109 code: 0,
11071110 frames: 1,
11081111 },
@@ -1113,9 +1116,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
11131116 frame: 960,
11141117 mode_request: None,
11151118 bandwidth_request: None,
1116- mode: Mode::Silk,
1117- bandwidth: Bandwidth::Wide,
1118- config: 9,
1119+ mode: Mode::Hybrid,
1120+ bandwidth: Bandwidth::Full,
1121+ config: 15,
11191122 code: 0,
11201123 frames: 1,
11211124 },
@@ -1127,8 +1130,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
11271130 mode_request: None,
11281131 bandwidth_request: None,
11291132 mode: Mode::Hybrid,
1130- bandwidth: Bandwidth::SuperWide,
1131- config: 13,
1133+ bandwidth: Bandwidth::Full,
1134+ config: 15,
11321135 code: 0,
11331136 frames: 1,
11341137 },
@@ -1139,9 +1142,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
11391142 frame: 960,
11401143 mode_request: None,
11411144 bandwidth_request: None,
1142- mode: Mode::Hybrid,
1145+ mode: Mode::Celt,
11431146 bandwidth: Bandwidth::Full,
1144- config: 15,
1147+ config: 31,
11451148 code: 0,
11461149 frames: 1,
11471150 },
@@ -1165,9 +1168,9 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
11651168 frame: 480,
11661169 mode_request: None,
11671170 bandwidth_request: None,
1168- mode: Mode::Silk,
1169- bandwidth: Bandwidth::Medium,
1170- config: 4,
1171+ mode: Mode::Hybrid,
1172+ bandwidth: Bandwidth::Full,
1173+ config: 14,
11711174 code: 0,
11721175 frames: 1,
11731176 },
@@ -1179,8 +1182,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
11791182 mode_request: None,
11801183 bandwidth_request: None,
11811184 mode: Mode::Silk,
1182- bandwidth: Bandwidth::Medium,
1183- config: 5,
1185+ bandwidth: Bandwidth::Wide,
1186+ config: 9,
11841187 code: 3,
11851188 frames: 2,
11861189 },
@@ -1192,8 +1195,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
11921195 mode_request: None,
11931196 bandwidth_request: None,
11941197 mode: Mode::Silk,
1195- bandwidth: Bandwidth::Medium,
1196- config: 5,
1198+ bandwidth: Bandwidth::Wide,
1199+ config: 9,
11971200 code: 3,
11981201 frames: 3,
11991202 },
@@ -1205,8 +1208,8 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
12051208 mode_request: None,
12061209 bandwidth_request: None,
12071210 mode: Mode::Silk,
1208- bandwidth: Bandwidth::Medium,
1209- config: 6,
1211+ bandwidth: Bandwidth::Wide,
1212+ config: 10,
12101213 code: 0,
12111214 frames: 1,
12121215 },
@@ -3378,7 +3381,7 @@ fn silk_library_gate_vs_libopus() {
33783381
33793382 let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes");
33803383 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");
33823385 let ref_ogg = temp_path("opus-silk-r2-ref.opus");
33833386 let ours_ogg = temp_path("opus-silk-r2-ours.opus");
33843387
@@ -3392,10 +3395,13 @@ fn silk_library_gate_vs_libopus() {
33923395 // cutoff: NB 4 kHz, WB 8 kHz, hybrid-FB unfiltered (None) — compare each
33933396 // encoder only against the band it can actually reproduce.
33943397 let rows: &[(&str, u32, Option<Bandwidth>, Option<Mode>, Option<f64>)] = &[
3395- ("12k-NB", 12, None, None, Some(4000.0)),
3396- ("16k-NB", 16, Some(Bandwidth::Narrow), None, Some(4000.0)),
3397- ("24k-WB", 24, Some(Bandwidth::Wide), Some(Mode::Silk), Some(8000.0)),
3398- ("32k-Hyb", 32, None, None, None),
3398+ // All rows auto since lane opus-mode: the selector is libopus's, so
3399+ // the mode must match the reference's (asserted below) and the
3400+ // comparison is full-band (no lowpass).
3401+ ("12k", 12, None, None, None),
3402+ ("16k", 16, None, None, None),
3403+ ("24k", 24, None, None, None),
3404+ ("32k", 32, None, None, None),
33993405 ];
34003406
34013407 let mut out_rows: Vec<String> = Vec::new();
@@ -3444,6 +3450,10 @@ fn silk_library_gate_vs_libopus() {
34443450 .encode_float(&source_pcm[..FRAME * CHANNELS], FRAME, &mut toc_buf)
34453451 .expect("encode probe");
34463452 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+ );
34473457
34483458 // Own-decoder roundtrip (range-coder invariant) + packet capture.
34493459 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)