The libopus input alignment ships behind a knob, off, with its gate tables
Browse files at this commit02f91a2469d1ce0a91ba581b805415a9d715725cParent: 0151578- feat
- the libopus input alignment ships behind a knob, off, with its gate tables
9 changed files +218 -40
- crates/ec-opus/src/celt_enc.rs +16 -3
- crates/ec-opus/src/encoder.rs +85 -25
- crates/ec-opus/tests/conformance.rs +24 -0
- lanes/opus-gate-r1.sweep.txt +12 -12
- lanes/opus-opusx2-r1.sweep.txt +17 -0
- lanes/opus-opusx2-r2.sweep.txt +16 -0
- lanes/opus-opusx2-r3.sweep.txt +16 -0
- lanes/opus-opusx2-r4.sweep.txt +16 -0
- lanes/opus-opusx2-r5.sweep.txt +16 -0
crates/ec-opus/src/celt_enc.rsmodified+16 -3
| @@ -391,6 +391,9 @@ pub struct CeltEncoder { | |||
| 391 | /// so its analysis window sits 4 ms behind the API frame. Interleaved, | 391 | /// so its analysis window sits 4 ms behind the API frame. Interleaved, |
| 392 | /// `CELT_DELAY_48K / upsample` samples per channel. | 392 | /// `CELT_DELAY_48K / upsample` samples per channel. |
| 393 | delay_buf: Vec<f32>, | 393 | delay_buf: Vec<f32>, |
| 394 | + /// Whether that delay is applied at all — see | ||
| 395 | + /// [`crate::Encoder::set_libopus_input_alignment`] for why it ships off. | ||
| 396 | + align_input: bool, | ||
| 394 | preemph_mem: [f32; 2], | 397 | preemph_mem: [f32; 2], |
| 395 | /// Coarse-energy prediction state, `2 * NB_BANDS`. | 398 | /// Coarse-energy prediction state, `2 * NB_BANDS`. |
| 396 | old_band_e: Vec<f32>, | 399 | old_band_e: Vec<f32>, |
| @@ -460,6 +463,7 @@ impl CeltEncoder { | |||
| 460 | plans: (0..4).map(|lm| MdctPlan::new(SHORT_MDCT << lm)).collect(), | 463 | plans: (0..4).map(|lm| MdctPlan::new(SHORT_MDCT << lm)).collect(), |
| 461 | in_mem: vec![0.0; channels * OVERLAP], | 464 | in_mem: vec![0.0; channels * OVERLAP], |
| 462 | delay_buf: vec![0.0; channels * CELT_DELAY_48K], | 465 | delay_buf: vec![0.0; channels * CELT_DELAY_48K], |
| 466 | + align_input: false, | ||
| 463 | preemph_mem: [0.0; 2], | 467 | preemph_mem: [0.0; 2], |
| 464 | old_band_e: vec![0.0; 2 * NB_BANDS], | 468 | old_band_e: vec![0.0; 2 * NB_BANDS], |
| 465 | delayed_intra: 1.0, | 469 | delayed_intra: 1.0, |
| @@ -516,6 +520,13 @@ impl CeltEncoder { | |||
| 516 | &self.last_diag | 520 | &self.last_diag |
| 517 | } | 521 | } |
| 518 | 522 | ||
| 523 | + /// Feeds the layer the input delayed by `CELT_DELAY_48K`, as libopus | ||
| 524 | + /// does; see [`crate::Encoder::set_libopus_input_alignment`]. | ||
| 525 | + pub fn set_align_input(&mut self, on: bool) { | ||
| 526 | + self.align_input = on; | ||
| 527 | + self.delay_buf.fill(0.0); | ||
| 528 | + } | ||
| 529 | + | ||
| 519 | /// Drops all inter-frame state. | 530 | /// Drops all inter-frame state. |
| 520 | pub fn reset(&mut self) { | 531 | pub fn reset(&mut self) { |
| 521 | self.in_mem.fill(0.0); | 532 | self.in_mem.fill(0.0); |
| @@ -621,7 +632,7 @@ impl CeltEncoder { | |||
| 621 | // `src(j)`: input sample `j` of the delayed stream libopus's CELT | 632 | // `src(j)`: input sample `j` of the delayed stream libopus's CELT |
| 622 | // layer sees -- the `d` samples held over from the previous call | 633 | // layer sees -- the `d` samples held over from the previous call |
| 623 | // first, then this frame's. | 634 | // first, then this frame's. |
| 624 | - let d = CELT_DELAY_48K / up; | 635 | + let d = if self.align_input { CELT_DELAY_48K / up } else { 0 }; |
| 625 | let src = |j: usize| { | 636 | let src = |j: usize| { |
| 626 | if j < d { delay[j * c + ch] } else { pcm[(j - d) * c + ch] } | 637 | if j < d { delay[j * c + ch] } else { pcm[(j - d) * c + ch] } |
| 627 | }; | 638 | }; |
| @@ -652,8 +663,10 @@ impl CeltEncoder { | |||
| 652 | self.in_mem[ch * OVERLAP..(ch + 1) * OVERLAP] | 663 | self.in_mem[ch * OVERLAP..(ch + 1) * OVERLAP] |
| 653 | .copy_from_slice(&self.in_buf[base + n..base + n + OVERLAP]); | 664 | .copy_from_slice(&self.in_buf[base + n..base + n + OVERLAP]); |
| 654 | } | 665 | } |
| 655 | - delay[..c * (CELT_DELAY_48K / self.upsample)] | 666 | + if self.align_input { |
| 656 | - .copy_from_slice(&next_delay[..c * (CELT_DELAY_48K / self.upsample)]); | 667 | + delay[..c * (CELT_DELAY_48K / self.upsample)] |
| 668 | + .copy_from_slice(&next_delay[..c * (CELT_DELAY_48K / self.upsample)]); | ||
| 669 | + } | ||
| 657 | self.delay_buf = delay; | 670 | self.delay_buf = delay; |
| 658 | 671 | ||
| 659 | // --- Silence flag (first symbol of the frame) ----------------------- | 672 | // --- Silence flag (first symbol of the frame) ----------------------- |
crates/ec-opus/src/encoder.rsmodified+85 -25
| @@ -40,16 +40,12 @@ const MAX_SILK_PACKET_BYTES: usize = 1 + 1 + 2 * 2 + 3 * MAX_FRAME_BYTES; | |||
| 40 | const SILK_LOOK_AHEAD_48K_NB: usize = 58; | 40 | const SILK_LOOK_AHEAD_48K_NB: usize = 58; |
| 41 | const SILK_LOOK_AHEAD_48K_MB: usize = 54; | 41 | const SILK_LOOK_AHEAD_48K_MB: usize = 54; |
| 42 | const SILK_LOOK_AHEAD_48K_WB: usize = 50; | 42 | const SILK_LOOK_AHEAD_48K_WB: usize = 50; |
| 43 | -/// The CELT layer's own delay: one MDCT overlap (120) plus libopus's | 43 | +/// One MDCT overlap: the CELT layer's delay without libopus's input |
| 44 | -/// `delay_compensation = Fs/250` (`CELT_DELAY_48K`, 192 at 48 kHz), which the | 44 | +/// alignment ([`Encoder::set_libopus_input_alignment`]), which adds |
| 45 | -/// CELT encoder applies to its input so its analysis window sits where | 45 | +/// `delay_compensation = Fs/250` (192 at 48 kHz) on top. |
| 46 | -/// libopus's does. | 46 | +const CELT_OVERLAP_48K: usize = 120; |
| 47 | -const CELT_LOOK_AHEAD_48K: usize = 120 + 192; | 47 | +/// libopus `opus_encoder.c`'s `delay_compensation = Fs/250`. |
| 48 | -/// How far the hybrid path delays the SILK layer's input so its output lines | 48 | +const CELT_DELAY_COMP_48K: usize = 192; |
| 49 | -/// up with the CELT layer's at the decoder (which sums them as-is): CELT's | ||
| 50 | -/// delay minus SILK's WB round trip, click-measured through the | ||
| 51 | -/// decoder with one layer muted (`hybrid_layers_align` in conformance.rs). | ||
| 52 | -const HYBRID_SILK_DELAY_48K: usize = CELT_LOOK_AHEAD_48K - SILK_LOOK_AHEAD_48K_WB; | ||
| 53 | /// Bytes the CELT layer of a hybrid packet always keeps, whatever SILK spent. | 49 | /// Bytes the CELT layer of a hybrid packet always keeps, whatever SILK spent. |
| 54 | /// Was 8: at 16 kbps SILK takes 37 of the 40-byte budget, so 8 expanded | 50 | /// Was 8: at 16 kbps SILK takes 37 of the 40-byte budget, so 8 expanded |
| 55 | /// 5133/6001 packets (+12.9% rate on the speech gate); libopus leaves CELT | 51 | /// 5133/6001 packets (+12.9% rate on the speech gate); libopus leaves CELT |
| @@ -111,11 +107,13 @@ pub struct Encoder { | |||
| 111 | /// Scratch space for the SILK packet payload, sized once so the steady-state | 107 | /// Scratch space for the SILK packet payload, sized once so the steady-state |
| 112 | /// loop (`steady_state_encode_loop_zero_alloc`) doesn't allocate. | 108 | /// loop (`steady_state_encode_loop_zero_alloc`) doesn't allocate. |
| 113 | silk_buf: [u8; MAX_SILK_PACKET_BYTES], | 109 | silk_buf: [u8; MAX_SILK_PACKET_BYTES], |
| 114 | - /// Hybrid: the last [`HYBRID_SILK_DELAY_48K`] samples of SILK-layer input. | 110 | + /// Hybrid: the last [`Encoder::hybrid_silk_delay_48k`] samples of SILK-layer input. |
| 115 | silk_delay: Vec<f32>, | 111 | silk_delay: Vec<f32>, |
| 116 | /// Scratch space for the zero-stuffed SILK/hybrid input, sized once so | 112 | /// Scratch space for the zero-stuffed SILK/hybrid input, sized once so |
| 117 | /// the steady-state loop doesn't allocate a `Vec` per frame. | 113 | /// the steady-state loop doesn't allocate a `Vec` per frame. |
| 118 | silk_stuff: Vec<f32>, | 114 | silk_stuff: Vec<f32>, |
| 115 | + /// [`Encoder::set_libopus_input_alignment`]: off by default. | ||
| 116 | + align_input: bool, | ||
| 119 | /// libopus `opus_encoder.c`'s `hp_mem`: the one-pole state of the | 117 | /// libopus `opus_encoder.c`'s `hp_mem`: the one-pole state of the |
| 120 | /// [`Encoder::dc_reject`] filter every frame of API input goes through, | 118 | /// [`Encoder::dc_reject`] filter every frame of API input goes through, |
| 121 | /// one per channel. | 119 | /// one per channel. |
| @@ -181,6 +179,7 @@ impl Encoder { | |||
| 181 | silk_buf: [0u8; MAX_SILK_PACKET_BYTES], | 179 | silk_buf: [0u8; MAX_SILK_PACKET_BYTES], |
| 182 | silk_delay: Vec::new(), | 180 | silk_delay: Vec::new(), |
| 183 | silk_stuff: Vec::new(), | 181 | silk_stuff: Vec::new(), |
| 182 | + align_input: false, | ||
| 184 | hp_mem: [0.0; 2], | 183 | hp_mem: [0.0; 2], |
| 185 | hp_buf: Vec::new(), | 184 | hp_buf: Vec::new(), |
| 186 | }) | 185 | }) |
| @@ -215,7 +214,7 @@ impl Encoder { | |||
| 215 | /// on, so this always matches which layer actually codes the frame): | 214 | /// on, so this always matches which layer actually codes the frame): |
| 216 | /// [`SILK_LOOK_AHEAD_48K_NB`], [`SILK_LOOK_AHEAD_48K_MB`] or | 215 | /// [`SILK_LOOK_AHEAD_48K_NB`], [`SILK_LOOK_AHEAD_48K_MB`] or |
| 217 | /// [`SILK_LOOK_AHEAD_48K_WB`]. Hybrid: CELT's 120, the SILK layer being | 216 | /// [`SILK_LOOK_AHEAD_48K_WB`]. Hybrid: CELT's 120, the SILK layer being |
| 218 | - /// delayed to meet it ([`HYBRID_SILK_DELAY_48K`]). Any other frame size | 217 | + /// delayed to meet it ([`Encoder::hybrid_silk_delay_48k`]). Any other frame size |
| 219 | /// falls back to CELT here exactly as `encode_toc_and_payload` does. | 218 | /// falls back to CELT here exactly as `encode_toc_and_payload` does. |
| 220 | pub fn look_ahead(&self, frame_size: usize) -> usize { | 219 | pub fn look_ahead(&self, frame_size: usize) -> usize { |
| 221 | let frame_48k = frame_size * self.upsample; | 220 | let frame_48k = frame_size * self.upsample; |
| @@ -227,7 +226,56 @@ impl Encoder { | |||
| 227 | }; | 226 | }; |
| 228 | return delay / self.upsample; | 227 | return delay / self.upsample; |
| 229 | } | 228 | } |
| 230 | - CELT_LOOK_AHEAD_48K / self.upsample | 229 | + self.celt_look_ahead_48k() / self.upsample |
| 230 | + } | ||
| 231 | + | ||
| 232 | + /// The CELT layer's delay in 48 kHz samples: one MDCT overlap, plus | ||
| 233 | + /// libopus's `delay_compensation` when the alignment is on. | ||
| 234 | + fn celt_look_ahead_48k(&self) -> usize { | ||
| 235 | + CELT_OVERLAP_48K + if self.align_input { CELT_DELAY_COMP_48K } else { 0 } | ||
| 236 | + } | ||
| 237 | + | ||
| 238 | + /// How far the hybrid path delays the SILK layer's input so its output | ||
| 239 | + /// lines up with the CELT layer's at the decoder (which sums them as-is): | ||
| 240 | + /// CELT's delay minus SILK's WB round trip, click-measured through the | ||
| 241 | + /// decoder with one layer muted (`hybrid_layers_align` in conformance.rs). | ||
| 242 | + fn hybrid_silk_delay_48k(&self) -> usize { | ||
| 243 | + self.celt_look_ahead_48k() - SILK_LOOK_AHEAD_48K_WB | ||
| 244 | + } | ||
| 245 | + | ||
| 246 | + /// Puts our CELT analysis window exactly where libopus's is: the layer is | ||
| 247 | + /// fed the input delayed by `delay_compensation = Fs/250` (192 samples at | ||
| 248 | + /// 48 kHz, `opus_encoder.c` ~:1955) and every frame first goes through | ||
| 249 | + /// libopus's 3 Hz `dc_reject`. The alignment is *exact* — on the reell | ||
| 250 | + /// attack frame (frame 2, 20 ms, LM=3, transient) our first eight band | ||
| 251 | + /// log-energies become | ||
| 252 | + /// 3.4386 4.0055 3.2603 4.5954 1.1790 4.2774 4.7584 3.7169 against | ||
| 253 | + /// libopus's 3.439 4.005 3.260 4.595 1.179 4.277 4.758 3.717, all eight | ||
| 254 | + /// within .0006 (the unaligned encoder is .06-.21 apart). | ||
| 255 | + /// | ||
| 256 | + /// It ships OFF because the 12-row library gate rejects it, and because | ||
| 257 | + /// re-tuning the constants that were fitted on the misaligned spectrum | ||
| 258 | + /// does not rescue it. Against the shipped rows | ||
| 259 | + /// (`lanes/opus-opustr2-r1.sweep.txt`), err_ratio: | ||
| 260 | + /// | ||
| 261 | + /// | arm | better | worse (blocking) | | ||
| 262 | + /// |---|---|---| | ||
| 263 | + /// | delay only (`opus-opusx-r1`) | 7 | nik@64 .708→.800, nik@96 .985→1.763, her@96 1.051→1.292, hein@64 .853→.980, hein@96 .528→1.602 | | ||
| 264 | + /// | delay + `dc_reject` (`opus-opusx2-r1`) | 5 | those, plus dl8a@64 .552→1.074, dl8a@96 .374→1.011 | | ||
| 265 | + /// | + two-pass coarse energy (`-r2`) | 5 | same 7 rows (nik@96 1.809, dl8a@64 1.200) | | ||
| 266 | + /// | + continuous 1.3 `alloc_trim` (`-r3`) | 5 | same 7, and corr worse by >.0005 on 9 rows | | ||
| 267 | + /// | + tf boost k=1.25 (`-r4`) | 5 | same 7 | | ||
| 268 | + /// | + tf boost k=1.5 (`-r5`) | 5 | same 7 | | ||
| 269 | + /// | ||
| 270 | + /// The blocking set (nik, her@96, dl8a, hein) is the same in every arm and | ||
| 271 | + /// no arm moves any of those rows back towards its shipped value, so the | ||
| 272 | + /// misfit is not one of the swept constants. Turning this on also costs | ||
| 273 | + /// 4 ms of algorithmic delay: [`Encoder::look_ahead`] for a CELT frame | ||
| 274 | + /// becomes 312, not 120, and the Ogg pre-skip must follow it. | ||
| 275 | + pub fn set_libopus_input_alignment(&mut self, on: bool) { | ||
| 276 | + self.align_input = on; | ||
| 277 | + self.celt.set_align_input(on); | ||
| 278 | + self.hp_mem = [0.0; 2]; | ||
| 231 | } | 279 | } |
| 232 | 280 | ||
| 233 | /// Forces the coded bandwidth; [`None`] (the default) picks it from the | 281 | /// Forces the coded bandwidth; [`None`] (the default) picks it from the |
| @@ -711,7 +759,12 @@ impl Encoder { | |||
| 711 | /// libopus takes the `hp_cutoff` branch instead for | 759 | /// libopus takes the `hp_cutoff` branch instead for |
| 712 | /// `OPUS_APPLICATION_VOIP` (a bandwidth-dependent second-order section); | 760 | /// `OPUS_APPLICATION_VOIP` (a bandwidth-dependent second-order section); |
| 713 | /// not ported — every VoIP frame still gets this DC reject. | 761 | /// not ported — every VoIP frame still gets this DC reject. |
| 714 | - fn dc_reject(&mut self, pcm: &[f32], frame_size: usize, out: &mut Vec<f32>) { | 762 | + /// Ships off with the alignment knob: `dc_reject` alone (without the |
| 763 | + /// input delay) has never been measured on the library gate. | ||
| 764 | + fn dc_reject<'a>(&mut self, pcm: &'a [f32], frame_size: usize, out: &'a mut Vec<f32>) -> &'a [f32] { | ||
| 765 | + if !self.align_input { | ||
| 766 | + return pcm; | ||
| 767 | + } | ||
| 715 | let c = self.channels; | 768 | let c = self.channels; |
| 716 | let coef = 6.3 * 3.0 / self.sample_rate as f32; | 769 | let coef = 6.3 * 3.0 / self.sample_rate as f32; |
| 717 | out.clear(); | 770 | out.clear(); |
| @@ -725,6 +778,7 @@ impl Encoder { | |||
| 725 | } | 778 | } |
| 726 | self.hp_mem[ch] = m; | 779 | self.hp_mem[ch] = m; |
| 727 | } | 780 | } |
| 781 | + out | ||
| 728 | } | 782 | } |
| 729 | 783 | ||
| 730 | /// Encodes one frame — `frame_size` samples per channel of interleaved | 784 | /// Encodes one frame — `frame_size` samples per channel of interleaved |
| @@ -737,8 +791,8 @@ impl Encoder { | |||
| 737 | out: &mut [u8], | 791 | out: &mut [u8], |
| 738 | ) -> Result<usize> { | 792 | ) -> Result<usize> { |
| 739 | let mut hp = std::mem::take(&mut self.hp_buf); | 793 | let mut hp = std::mem::take(&mut self.hp_buf); |
| 740 | - self.dc_reject(pcm, frame_size, &mut hp); | 794 | + let input = self.dc_reject(pcm, frame_size, &mut hp); |
| 741 | - let r = match self.encode_toc_and_payload(&hp, frame_size, out.len().saturating_sub(1)) { | 795 | + let r = match self.encode_toc_and_payload(input, frame_size, out.len().saturating_sub(1)) { |
| 742 | Err(e) => Err(e), | 796 | Err(e) => Err(e), |
| 743 | Ok((toc, payload)) => { | 797 | Ok((toc, payload)) => { |
| 744 | let n = payload.len(); | 798 | let n = payload.len(); |
| @@ -775,8 +829,8 @@ impl Encoder { | |||
| 775 | out: &mut [u8], | 829 | out: &mut [u8], |
| 776 | ) -> Result<usize> { | 830 | ) -> Result<usize> { |
| 777 | let mut hp = std::mem::take(&mut self.hp_buf); | 831 | let mut hp = std::mem::take(&mut self.hp_buf); |
| 778 | - self.dc_reject(pcm, frame_size, &mut hp); | 832 | + let input = self.dc_reject(pcm, frame_size, &mut hp); |
| 779 | - let r = match self.encode_toc_and_payload(&hp, frame_size, out.len().saturating_sub(3)) { | 833 | + let r = match self.encode_toc_and_payload(input, frame_size, out.len().saturating_sub(3)) { |
| 780 | Err(e) => Err(e), | 834 | Err(e) => Err(e), |
| 781 | Ok((toc, payload)) => { | 835 | Ok((toc, payload)) => { |
| 782 | let n = payload.len(); | 836 | let n = payload.len(); |
| @@ -808,7 +862,7 @@ impl Encoder { | |||
| 808 | /// One hybrid frame (10 or 20 ms, mono or stereo, `bandwidth` SWB or FB) | 862 | /// One hybrid frame (10 or 20 ms, mono or stereo, `bandwidth` SWB or FB) |
| 809 | /// into the internal range coder, returning its byte length: the SILK | 863 | /// into the internal range coder, returning its byte length: the SILK |
| 810 | /// layer's symbols (WB, 16 kHz internal, fed the input delayed by | 864 | /// layer's symbols (WB, 16 kHz internal, fed the input delayed by |
| 811 | - /// [`HYBRID_SILK_DELAY_48K`]), the no-redundancy flag under the decoder's | 865 | + /// [`Encoder::hybrid_silk_delay_48k`]), the no-redundancy flag under the decoder's |
| 812 | /// exact presence rule, then the CELT layer from band 17 in the same | 866 | /// exact presence rule, then the CELT layer from band 17 in the same |
| 813 | /// coder. CBR: the packet is the bitrate's size unless SILK alone needs | 867 | /// coder. CBR: the packet is the bitrate's size unless SILK alone needs |
| 814 | /// more, in which case CELT keeps [`HYBRID_CELT_MIN_BYTES`] on top. | 868 | /// more, in which case CELT keeps [`HYBRID_CELT_MIN_BYTES`] on top. |
| @@ -834,7 +888,7 @@ impl Encoder { | |||
| 834 | } else { | 888 | } else { |
| 835 | Self::zero_stuff_mono(pcm, frame_size, self.upsample, &mut self.silk_stuff); | 889 | Self::zero_stuff_mono(pcm, frame_size, self.upsample, &mut self.silk_stuff); |
| 836 | } | 890 | } |
| 837 | - let d = HYBRID_SILK_DELAY_48K * self.channels; | 891 | + let d = self.hybrid_silk_delay_48k() * self.channels; |
| 838 | if self.silk_delay.len() != d { | 892 | if self.silk_delay.len() != d { |
| 839 | self.silk_delay.clear(); | 893 | self.silk_delay.clear(); |
| 840 | self.silk_delay.resize(d, 0.0); | 894 | self.silk_delay.resize(d, 0.0); |
| @@ -953,7 +1007,7 @@ mod tests { | |||
| 953 | let mut e = Encoder::new(24000, 2, Application::Audio).unwrap(); | 1007 | let mut e = Encoder::new(24000, 2, Application::Audio).unwrap(); |
| 954 | e.set_bitrate(256_000); | 1008 | e.set_bitrate(256_000); |
| 955 | assert_eq!(e.auto_bandwidth(), Bandwidth::SuperWide); | 1009 | assert_eq!(e.auto_bandwidth(), Bandwidth::SuperWide); |
| 956 | - assert_eq!(e.look_ahead(960), CELT_LOOK_AHEAD_48K / 2); | 1010 | + assert_eq!(e.look_ahead(960), CELT_OVERLAP_48K / 2); |
| 957 | } | 1011 | } |
| 958 | 1012 | ||
| 959 | /// `look_ahead` must use the same 10-or-20 ms-frame predicate dispatch | 1013 | /// `look_ahead` must use the same 10-or-20 ms-frame predicate dispatch |
| @@ -965,21 +1019,27 @@ mod tests { | |||
| 965 | let mut e = Encoder::new(48000, 1, Application::Voip).unwrap(); | 1019 | let mut e = Encoder::new(48000, 1, Application::Voip).unwrap(); |
| 966 | e.set_bitrate(8000); | 1020 | e.set_bitrate(8000); |
| 967 | assert_eq!(e.look_ahead(480), 58, "10ms NB SILK"); | 1021 | assert_eq!(e.look_ahead(480), 58, "10ms NB SILK"); |
| 968 | - assert_eq!(e.look_ahead(240), CELT_LOOK_AHEAD_48K, "5ms frame falls back to CELT"); | 1022 | + assert_eq!(e.look_ahead(240), CELT_OVERLAP_48K, "5ms frame falls back to CELT"); |
| 969 | assert_eq!(e.look_ahead(960), 58, "20ms NB SILK"); | 1023 | assert_eq!(e.look_ahead(960), 58, "20ms NB SILK"); |
| 970 | 1024 | ||
| 971 | // 16k VoIP is hybrid-FB since the libopus threshold port (CELT overlap). | 1025 | // 16k VoIP is hybrid-FB since the libopus threshold port (CELT overlap). |
| 972 | e.set_bitrate(16000); | 1026 | e.set_bitrate(16000); |
| 973 | - assert_eq!(e.look_ahead(960), CELT_LOOK_AHEAD_48K, "20ms hybrid FB"); | 1027 | + assert_eq!(e.look_ahead(960), CELT_OVERLAP_48K, "20ms hybrid FB"); |
| 974 | 1028 | ||
| 975 | e.set_bitrate(32000); | 1029 | e.set_bitrate(32000); |
| 976 | - assert_eq!(e.look_ahead(960), CELT_LOOK_AHEAD_48K, "20ms hybrid, CELT's delay"); | 1030 | + assert_eq!(e.look_ahead(960), CELT_OVERLAP_48K, "20ms hybrid, CELT's delay"); |
| 977 | 1031 | ||
| 978 | let mut e = Encoder::new(48000, 2, Application::Voip).unwrap(); | 1032 | let mut e = Encoder::new(48000, 2, Application::Voip).unwrap(); |
| 979 | e.set_bitrate(8000); | 1033 | e.set_bitrate(8000); |
| 980 | assert_eq!(e.look_ahead(960), 58, "stereo 20ms NB SILK"); | 1034 | assert_eq!(e.look_ahead(960), 58, "stereo 20ms NB SILK"); |
| 981 | e.set_bitrate(64_000); | 1035 | e.set_bitrate(64_000); |
| 982 | - assert_eq!(e.look_ahead(960), CELT_LOOK_AHEAD_48K, "stereo 20ms hybrid"); | 1036 | + assert_eq!(e.look_ahead(960), CELT_OVERLAP_48K, "stereo 20ms hybrid"); |
| 1037 | + e.set_libopus_input_alignment(true); | ||
| 1038 | + assert_eq!( | ||
| 1039 | + e.look_ahead(960), | ||
| 1040 | + CELT_OVERLAP_48K + CELT_DELAY_COMP_48K, | ||
| 1041 | + "the alignment adds libopus's delay_compensation to the advertised delay" | ||
| 1042 | + ); | ||
| 983 | } | 1043 | } |
| 984 | 1044 | ||
| 985 | #[test] | 1045 | #[test] |
crates/ec-opus/tests/conformance.rsmodified+24 -0
| @@ -907,6 +907,30 @@ fn encoder_roundtrips_at_every_rate() { | |||
| 907 | } | 907 | } |
| 908 | } | 908 | } |
| 909 | 909 | ||
| 910 | +/// The libopus input alignment ([`Encoder::set_libopus_input_alignment`]): | ||
| 911 | +/// with it on the CELT layer's advertised delay grows by libopus's | ||
| 912 | +/// `delay_compensation`, and the decoded signal tracks the source at exactly | ||
| 913 | +/// that lag and not at the old one — the knob is usable, not just present. | ||
| 914 | +/// It ships off; the gate tables that rejected it are at the setter. | ||
| 915 | +#[test] | ||
| 916 | +fn libopus_input_alignment_delays_and_still_decodes() { | ||
| 917 | + let pcm = test_signal(2, 1.0); | ||
| 918 | + let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap(); | ||
| 919 | + enc.set_bitrate(128_000); | ||
| 920 | + assert_eq!(enc.look_ahead(960), 120, "default: one MDCT overlap"); | ||
| 921 | + enc.set_libopus_input_alignment(true); | ||
| 922 | + assert_eq!(enc.look_ahead(960), 312, "overlap + delay_compensation"); | ||
| 923 | + let (decoded, _) = roundtrip_own(&mut enc, &pcm, 2, 960); | ||
| 924 | + let aligned = delayed_corr(&pcm, &decoded, 2, enc.look_ahead(960)); | ||
| 925 | + let old = delayed_corr(&pcm, &decoded, 2, 120); | ||
| 926 | + println!("aligned: corr at 312 {aligned:.4}, at 120 {old:.4}"); | ||
| 927 | + assert!(aligned >= 0.85, "aligned round trip corr {aligned:.4}"); | ||
| 928 | + assert!( | ||
| 929 | + aligned > old, | ||
| 930 | + "advertised delay 312 correlates {aligned:.4}, worse than the old 120 ({old:.4})" | ||
| 931 | + ); | ||
| 932 | +} | ||
| 933 | + | ||
| 910 | #[test] | 934 | #[test] |
| 911 | // Expectations are libopus 1.6's (opus_encoder.c mode_thresholds and the | 935 | // Expectations are libopus 1.6's (opus_encoder.c mode_thresholds and the |
| 912 | // voice/music bandwidth tables blended by voice_est) since lane opus-mode; | 936 | // voice/music bandwidth tables blended by voice_est) since lane opus-mode; |
lanes/opus-gate-r1.sweep.txtmodified+12 -12
| @@ -1,15 +1,15 @@ | |||
| 1 | # ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | 1 | # ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) |
| 2 | # both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | 2 | # both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo |
| 3 | # source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref | 3 | # source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref |
| 4 | -nik 64 69.1 69.2 -0.1 0.9887 0.9864 -0.0023 -114.88 -154.78 0.800 0.9769 0.9745 0 0 | 4 | +nik 64 69.1 69.2 -0.2 0.9883 0.9864 -0.0019 -155.70 -154.78 1.005 0.9765 0.9745 0 0 |
| 5 | -nik 96 100.9 101.0 -0.1 0.9947 0.9940 -0.0007 -109.44 -31.55 1.763 0.9891 0.9888 0 0 | 5 | +nik 96 100.8 101.0 -0.2 0.9943 0.9940 -0.0003 -111.16 -31.55 1.781 0.9888 0.9888 0 0 |
| 6 | -reelo 64 63.6 63.6 -0.0 0.9894 0.9866 -0.0028 -170.07 -123.36 1.287 0.9709 0.9686 0 0 | 6 | +reelo 64 63.5 63.6 -0.2 0.9886 0.9866 -0.0020 -179.06 -123.36 1.347 0.9699 0.9686 0 0 |
| 7 | -reelo 96 93.4 93.9 -0.5 0.9950 0.9937 -0.0013 -100.39 -125.18 0.861 0.9875 0.9854 0 0 | 7 | +reelo 96 93.2 93.9 -0.7 0.9942 0.9937 -0.0004 -89.99 -125.18 0.805 0.9869 0.9854 0 0 |
| 8 | -her 64 67.6 67.9 -0.4 0.9837 0.9794 -0.0042 -333.52 -339.00 0.980 0.9481 0.9406 0 0 | 8 | +her 64 67.5 67.9 -0.7 0.9825 0.9794 -0.0030 -347.44 -339.00 1.032 0.9466 0.9406 0 0 |
| 9 | -her 96 101.0 101.5 -0.5 0.9925 0.9902 -0.0022 -248.22 -191.50 1.292 0.9774 0.9728 0 0 | 9 | +her 96 100.7 101.5 -0.8 0.9913 0.9902 -0.0011 -247.70 -191.50 1.289 0.9760 0.9728 0 0 |
| 10 | -reell 64 71.3 71.3 -0.0 0.9908 0.9880 -0.0028 -263.61 -128.39 1.916 0.9717 0.9644 0 0 | 10 | +reell 64 71.3 71.3 -0.0 0.9904 0.9880 -0.0024 -272.52 -128.39 1.987 0.9710 0.9644 0 0 |
| 11 | -reell 96 105.3 105.4 -0.0 0.9956 0.9948 -0.0008 -266.25 -29.16 3.900 0.9856 0.9804 0 0 | 11 | +reell 96 105.3 105.4 -0.0 0.9952 0.9948 -0.0004 -261.59 -29.16 3.825 0.9849 0.9804 0 0 |
| 12 | -dl8a 64 65.7 65.8 -0.1 0.9890 0.9865 -0.0025 -204.82 -378.84 0.498 0.9789 0.9723 0 0 | 12 | +dl8a 64 65.5 65.8 -0.4 0.9883 0.9865 -0.0019 -394.10 -378.84 1.055 0.9784 0.9723 0 0 |
| 13 | -dl8a 96 96.6 96.8 -0.3 0.9943 0.9934 -0.0009 -170.45 -423.47 0.361 0.9882 0.9864 0 0 | 13 | +dl8a 96 96.3 96.8 -0.5 0.9936 0.9934 -0.0002 -406.85 -423.47 0.945 0.9865 0.9864 0 0 |
| 14 | -hein 64 64.8 64.9 -0.2 0.9888 0.9874 -0.0014 -382.47 -388.31 0.980 0.9345 0.9148 0 0 | 14 | +hein 64 64.6 64.9 -0.4 0.9876 0.9874 -0.0003 -374.22 -388.31 0.951 0.9258 0.9148 0 0 |
| 15 | -hein 96 86.8 86.9 -0.2 0.9945 0.9930 -0.0015 -367.35 -246.46 1.602 0.9700 0.9499 0 0 | 15 | +hein 96 86.5 86.9 -0.4 0.9938 0.9930 -0.0007 -373.59 -246.46 1.638 0.9651 0.9499 0 0 |
lanes/opus-opusx2-r1.sweep.txtadded+17 -0
| @@ -0,0 +1,17 @@ | |||
| 1 | +# lane-opusx2 r1: CELT input delayed by Fs/250 + libopus dc_reject (3 Hz) on the API input | ||
| 2 | +# baseline lanes/opus-opustr2-r1.sweep.txt (shipped); delay-only arm = lanes/opus-opusx-r1.sweep.txt | ||
| 3 | +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | ||
| 4 | +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | ||
| 5 | +# source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref | ||
| 6 | +nik 64 69.1 69.2 -0.1 0.9883 0.9864 -0.0020 -116.02 -154.78 0.805 0.9766 0.9745 0 0 | ||
| 7 | +nik 96 100.9 101.0 -0.1 0.9944 0.9940 -0.0003 -111.04 -31.55 1.780 0.9888 0.9888 0 0 | ||
| 8 | +reelo 64 63.6 63.6 -0.0 0.9887 0.9866 -0.0021 -172.14 -123.36 1.301 0.9708 0.9686 0 0 | ||
| 9 | +reelo 96 93.4 93.9 -0.5 0.9943 0.9937 -0.0006 -101.22 -125.18 0.866 0.9875 0.9854 0 0 | ||
| 10 | +her 64 67.6 67.9 -0.4 0.9828 0.9794 -0.0033 -333.52 -339.00 0.980 0.9474 0.9406 0 0 | ||
| 11 | +her 96 101.0 101.5 -0.5 0.9916 0.9902 -0.0014 -248.38 -191.50 1.293 0.9770 0.9728 0 0 | ||
| 12 | +reell 64 71.3 71.3 -0.0 0.9904 0.9880 -0.0024 -264.15 -128.39 1.920 0.9714 0.9644 0 0 | ||
| 13 | +reell 96 105.3 105.4 -0.0 0.9952 0.9948 -0.0004 -271.16 -29.16 3.980 0.9853 0.9804 0 0 | ||
| 14 | +dl8a 64 65.7 65.8 -0.1 0.9885 0.9865 -0.0020 -399.15 -378.84 1.074 0.9787 0.9723 0 0 | ||
| 15 | +dl8a 96 96.6 96.8 -0.3 0.9938 0.9934 -0.0004 -426.78 -423.47 1.011 0.9876 0.9864 0 0 | ||
| 16 | +hein 64 64.8 64.9 -0.2 0.9887 0.9874 -0.0013 -383.53 -388.31 0.983 0.9345 0.9148 0 0 | ||
| 17 | +hein 96 86.8 86.9 -0.2 0.9944 0.9930 -0.0014 -351.66 -246.46 1.513 0.9701 0.9499 0 0 | ||
lanes/opus-opusx2-r2.sweep.txtadded+16 -0
| @@ -0,0 +1,16 @@ | |||
| 1 | +# lane-opusx2 r2: EC_OPUS_TWO_PASS=1 (libopus two-pass coarse energy) (on the aligned tree: Fs/250 delay + dc_reject) | ||
| 2 | +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | ||
| 3 | +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | ||
| 4 | +# source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref | ||
| 5 | +nik 64 69.1 69.2 -0.1 0.9884 0.9864 -0.0020 -114.86 -154.78 0.799 0.9767 0.9745 0 0 | ||
| 6 | +nik 96 100.9 101.0 -0.1 0.9944 0.9940 -0.0003 -113.73 -31.55 1.809 0.9889 0.9888 0 0 | ||
| 7 | +reelo 64 63.6 63.6 -0.0 0.9888 0.9866 -0.0021 -168.90 -123.36 1.280 0.9708 0.9686 0 0 | ||
| 8 | +reelo 96 93.4 93.9 -0.5 0.9943 0.9937 -0.0006 -98.26 -125.18 0.850 0.9874 0.9854 0 0 | ||
| 9 | +her 64 67.7 67.9 -0.4 0.9828 0.9794 -0.0034 -342.89 -339.00 1.015 0.9477 0.9406 0 0 | ||
| 10 | +her 96 101.0 101.5 -0.5 0.9916 0.9902 -0.0014 -245.44 -191.50 1.277 0.9773 0.9728 0 0 | ||
| 11 | +reell 64 71.3 71.3 -0.0 0.9904 0.9880 -0.0024 -270.85 -128.39 1.974 0.9715 0.9644 0 0 | ||
| 12 | +reell 96 105.3 105.4 -0.0 0.9952 0.9948 -0.0004 -271.17 -29.16 3.980 0.9853 0.9804 0 0 | ||
| 13 | +dl8a 64 65.7 65.8 -0.1 0.9885 0.9865 -0.0020 -431.62 -378.84 1.200 0.9784 0.9723 0 0 | ||
| 14 | +dl8a 96 96.6 96.8 -0.3 0.9938 0.9934 -0.0004 -429.71 -423.47 1.021 0.9877 0.9864 0 0 | ||
| 15 | +hein 64 64.8 64.9 -0.1 0.9889 0.9874 -0.0016 -398.79 -388.31 1.037 0.9355 0.9148 0 0 | ||
| 16 | +hein 96 86.8 86.9 -0.2 0.9945 0.9930 -0.0015 -348.20 -246.46 1.494 0.9705 0.9499 0 0 | ||
lanes/opus-opusx2-r3.sweep.txtadded+16 -0
| @@ -0,0 +1,16 @@ | |||
| 1 | +# lane-opusx2 r3: EC_OPUS_TRIM13=1 (continuous 1.3 alloc_trim) (on the aligned tree: Fs/250 delay + dc_reject) | ||
| 2 | +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | ||
| 3 | +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | ||
| 4 | +# source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref | ||
| 5 | +nik 64 69.1 69.2 -0.1 0.9875 0.9864 -0.0011 -117.01 -154.78 0.810 0.9760 0.9745 0 0 | ||
| 6 | +nik 96 100.9 101.0 -0.1 0.9938 0.9940 +0.0003 -128.36 -31.55 1.971 0.9880 0.9888 0 0 | ||
| 7 | +reelo 64 63.6 63.6 -0.0 0.9883 0.9866 -0.0017 -157.11 -123.36 1.204 0.9707 0.9686 0 0 | ||
| 8 | +reelo 96 93.4 93.9 -0.5 0.9940 0.9937 -0.0002 -74.67 -125.18 0.726 0.9866 0.9854 0 0 | ||
| 9 | +her 64 67.6 67.9 -0.4 0.9820 0.9794 -0.0026 -334.21 -339.00 0.982 0.9455 0.9406 0 0 | ||
| 10 | +her 96 101.0 101.5 -0.5 0.9911 0.9902 -0.0008 -251.84 -191.50 1.312 0.9757 0.9728 0 0 | ||
| 11 | +reell 64 71.3 71.3 -0.0 0.9896 0.9880 -0.0016 -268.76 -128.39 1.957 0.9704 0.9644 0 0 | ||
| 12 | +reell 96 105.3 105.4 -0.0 0.9947 0.9948 +0.0001 -265.83 -29.16 3.893 0.9845 0.9804 0 0 | ||
| 13 | +dl8a 64 65.7 65.8 -0.1 0.9874 0.9865 -0.0009 -387.57 -378.84 1.031 0.9762 0.9723 0 0 | ||
| 14 | +dl8a 96 96.6 96.8 -0.3 0.9931 0.9934 +0.0003 -413.74 -423.47 0.967 0.9858 0.9864 0 0 | ||
| 15 | +hein 64 64.8 64.9 -0.2 0.9886 0.9874 -0.0012 -383.53 -388.31 0.983 0.9345 0.9148 0 0 | ||
| 16 | +hein 96 86.8 86.9 -0.2 0.9944 0.9930 -0.0014 -351.67 -246.46 1.513 0.9699 0.9499 0 0 | ||
lanes/opus-opusx2-r4.sweep.txtadded+16 -0
| @@ -0,0 +1,16 @@ | |||
| 1 | +# lane-opusx2 r4: EC_OPUS_TF_K=1.25 (CVBR tf boost) (on the aligned tree: Fs/250 delay + dc_reject) | ||
| 2 | +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | ||
| 3 | +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | ||
| 4 | +# source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref | ||
| 5 | +nik 64 69.1 69.2 -0.1 0.9883 0.9864 -0.0020 -110.52 -154.78 0.779 0.9765 0.9745 0 0 | ||
| 6 | +nik 96 100.9 101.0 -0.1 0.9943 0.9940 -0.0003 -111.87 -31.55 1.789 0.9888 0.9888 0 0 | ||
| 7 | +reelo 64 63.5 63.6 -0.1 0.9887 0.9866 -0.0020 -180.92 -123.36 1.359 0.9704 0.9686 0 0 | ||
| 8 | +reelo 96 93.3 93.9 -0.6 0.9942 0.9937 -0.0005 -90.05 -125.18 0.806 0.9870 0.9854 0 0 | ||
| 9 | +her 64 67.5 67.9 -0.6 0.9826 0.9794 -0.0032 -311.93 -339.00 0.903 0.9471 0.9406 0 0 | ||
| 10 | +her 96 100.7 101.5 -0.7 0.9914 0.9902 -0.0012 -230.30 -191.50 1.195 0.9765 0.9728 0 0 | ||
| 11 | +reell 64 71.3 71.3 -0.0 0.9904 0.9880 -0.0024 -269.89 -128.39 1.966 0.9712 0.9644 0 0 | ||
| 12 | +reell 96 105.3 105.4 -0.0 0.9952 0.9948 -0.0004 -263.44 -29.16 3.855 0.9852 0.9804 0 0 | ||
| 13 | +dl8a 64 65.6 65.8 -0.2 0.9884 0.9865 -0.0020 -424.09 -378.84 1.170 0.9791 0.9723 0 0 | ||
| 14 | +dl8a 96 96.4 96.8 -0.4 0.9937 0.9934 -0.0003 -434.84 -423.47 1.039 0.9872 0.9864 0 0 | ||
| 15 | +hein 64 64.7 64.9 -0.3 0.9882 0.9874 -0.0008 -376.99 -388.31 0.961 0.9314 0.9148 0 0 | ||
| 16 | +hein 96 86.7 86.9 -0.3 0.9941 0.9930 -0.0011 -363.54 -246.46 1.580 0.9677 0.9499 0 0 | ||
lanes/opus-opusx2-r5.sweep.txtadded+16 -0
| @@ -0,0 +1,16 @@ | |||
| 1 | +# lane-opusx2 r5: EC_OPUS_TF_K=1.5 (CVBR tf boost) (on the aligned tree: Fs/250 delay + dc_reject) | ||
| 2 | +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | ||
| 3 | +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | ||
| 4 | +# source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref | ||
| 5 | +nik 64 69.1 69.2 -0.2 0.9883 0.9864 -0.0019 -155.70 -154.78 1.005 0.9765 0.9745 0 0 | ||
| 6 | +nik 96 100.8 101.0 -0.2 0.9943 0.9940 -0.0003 -111.16 -31.55 1.781 0.9888 0.9888 0 0 | ||
| 7 | +reelo 64 63.5 63.6 -0.2 0.9886 0.9866 -0.0020 -179.06 -123.36 1.347 0.9699 0.9686 0 0 | ||
| 8 | +reelo 96 93.2 93.9 -0.7 0.9942 0.9937 -0.0004 -89.99 -125.18 0.805 0.9869 0.9854 0 0 | ||
| 9 | +her 64 67.5 67.9 -0.7 0.9825 0.9794 -0.0030 -347.44 -339.00 1.032 0.9466 0.9406 0 0 | ||
| 10 | +her 96 100.7 101.5 -0.8 0.9913 0.9902 -0.0011 -247.70 -191.50 1.289 0.9760 0.9728 0 0 | ||
| 11 | +reell 64 71.3 71.3 -0.0 0.9904 0.9880 -0.0024 -272.52 -128.39 1.987 0.9710 0.9644 0 0 | ||
| 12 | +reell 96 105.3 105.4 -0.0 0.9952 0.9948 -0.0004 -261.59 -29.16 3.825 0.9849 0.9804 0 0 | ||
| 13 | +dl8a 64 65.5 65.8 -0.4 0.9883 0.9865 -0.0019 -394.10 -378.84 1.055 0.9784 0.9723 0 0 | ||
| 14 | +dl8a 96 96.3 96.8 -0.5 0.9936 0.9934 -0.0002 -406.85 -423.47 0.945 0.9865 0.9864 0 0 | ||
| 15 | +hein 64 64.6 64.9 -0.4 0.9876 0.9874 -0.0003 -374.22 -388.31 0.951 0.9258 0.9148 0 0 | ||
| 16 | +hein 96 86.5 86.9 -0.4 0.9938 0.9930 -0.0007 -373.59 -246.46 1.638 0.9651 0.9499 0 0 | ||