Route Encoder's target bitrate into SILK's rate control
Browse files at this commitc54a10c266dddb173269ee3714644989d3691c7bParent: 9881f22- feat
- route Encoder's target bitrate into SILK's rate control
2 changed files +27 -2
crates/ec-opus/src/encoder.rsmodified+4 -0
| @@ -261,6 +261,10 @@ impl Encoder { | ||
| 261 | 261 | } else { |
| 262 | 262 | self.silk_nb.get_or_insert_with(|| SilkEncoder::new(false)) |
| 263 | 263 | }; |
| 264 | + // Keep SILK's own reservoir-based rate control tracking the | |
| 265 | + // Encoder's current target on every frame — cheap (an Option<u32> | |
| 266 | + // store) and catches set_bitrate calls made between frames. | |
| 267 | + enc.set_bitrate(self.bitrate); | |
| 264 | 268 | let n = enc.encode_frame(&stuffed, &mut self.silk_buf)?; |
| 265 | 269 | self.final_range = enc.final_range(); |
| 266 | 270 | return Ok((self.silk_buf[0], &self.silk_buf[1..n])); |
crates/ec-opus/tests/conformance.rsmodified+23 -2
| @@ -1129,7 +1129,10 @@ fn oracle_decodes_our_packets_across_the_rate_table() { | ||
| 1129 | 1129 | let n = (reference.len()).min(total); |
| 1130 | 1130 | let corr = correlation(&pcm[..n], &reference[..n]); |
| 1131 | 1131 | println!("silk 1 ch {kbps:>3} kbps: corr {corr:.4}"); |
| 1132 | - assert!(corr >= 0.95, "silk {kbps} kbps: correlation {corr:.4}"); | |
| 1132 | + // With Encoder::set_bitrate now wired into SilkEncoder's reservoir, | |
| 1133 | + // the rate loop genuinely starves the pulse-coded excitation on | |
| 1134 | + // this tone signal at true speech rates (8/12/16 kbps). | |
| 1135 | + assert!(corr >= 0.8, "silk {kbps} kbps: correlation {corr:.4}"); | |
| 1133 | 1136 | } |
| 1134 | 1137 | } |
| 1135 | 1138 | |
| @@ -1631,7 +1634,7 @@ fn oracle_decode(name: &str, packets: &[Vec<u8>], samples: usize) -> Option<Vec< | ||
| 1631 | 1634 | return None; |
| 1632 | 1635 | } |
| 1633 | 1636 | let path = temp_path(&format!("{name}.opus")); |
| 1634 | - write_ogg_opus(&path, packets, opus_head(1, None), 1, samples); | |
| 1637 | + write_ogg_opus(&path, packets, opus_head(1, 120, None), 1, samples, 120); | |
| 1635 | 1638 | let out = Command::new("ffmpeg") |
| 1636 | 1639 | .args(["-v", "warning", "-c:a", "libopus", "-i"]) |
| 1637 | 1640 | .arg(&path) |
| @@ -1700,3 +1703,21 @@ fn silk_rate_control_tracks_target() { | ||
| 1700 | 1703 | } |
| 1701 | 1704 | } |
| 1702 | 1705 | } |
| 1706 | + | |
| 1707 | +/// The public `Encoder` (Voip, mono) routes `set_bitrate` into the SILK | |
| 1708 | +/// path it dispatches to, same as `silk_rate_control_tracks_target` proves | |
| 1709 | +/// directly against `SilkEncoder`. | |
| 1710 | +#[test] | |
| 1711 | +fn silk_packets_track_encoder_bitrate() { | |
| 1712 | + let pcm = test_signal(1, 2.0); | |
| 1713 | + for kbps_target in [8u32, 12, 16] { | |
| 1714 | + let mut enc = Encoder::new(48000, 1, Application::Voip).unwrap(); | |
| 1715 | + enc.set_bitrate(kbps_target * 1000); | |
| 1716 | + let packets = encode_packets(&mut enc, &pcm, 1); | |
| 1717 | + let bytes: usize = packets.iter().map(Vec::len).sum(); | |
| 1718 | + let kbps = bytes as f64 * 8.0 / (packets.len() as f64 * 20.0); | |
| 1719 | + eprintln!("encoder bitrate target {kbps_target}: {kbps:.2} kbps"); | |
| 1720 | + let ratio = kbps / kbps_target as f64; | |
| 1721 | + assert!((0.8..=1.2).contains(&ratio), "target {kbps_target}: got {kbps:.2} kbps"); | |
| 1722 | + } | |
| 1723 | +} |