diff --git a/crates/ec-opus/src/encoder.rs b/crates/ec-opus/src/encoder.rs index 193b4877..e5e316fc 100644 --- a/crates/ec-opus/src/encoder.rs +++ b/crates/ec-opus/src/encoder.rs @@ -54,8 +54,7 @@ enum Prefill { } /// One MDCT overlap: the CELT layer's delay without libopus's input -/// alignment ([`Encoder::set_libopus_input_alignment`]), which adds -/// `delay_compensation = Fs/250` (192 at 48 kHz) on top. +/// alignment, which adds `delay_compensation = Fs/250` (192 at 48 kHz) on top. const CELT_OVERLAP_48K: usize = 120; /// libopus `opus_encoder.c`'s `delay_compensation = Fs/250`. const CELT_DELAY_COMP_48K: usize = 192; @@ -294,8 +293,6 @@ pub struct Encoder { /// 4088 bytes, and an inline array that size is a third of the whole /// budget on the `Encoder` that a caller holds by value. silk_buf: Box<[u8; MAX_SILK_PACKET_BYTES]>, - /// [`Encoder::set_libopus_input_alignment`]. - align_input: bool, filter: InputFilter, /// Scratch space for the filter's output, sized once. hp_buf: Vec, @@ -441,7 +438,6 @@ impl Encoder { mode: None, silk: None, silk_buf: Box::new([0u8; MAX_SILK_PACKET_BYTES]), - align_input: application != Application::LowDelay, filter: InputFilter::new(), hp_buf: Vec::new(), delay_buffer: vec![0.0; encoder_buffer(sample_rate) * channels], @@ -516,65 +512,13 @@ impl Encoder { /// libopus's `delay_compensation` when the alignment is on. fn celt_look_ahead_48k(&self) -> usize { CELT_OVERLAP_48K - + if self.align_input { + + if self.application != Application::LowDelay { CELT_DELAY_COMP_48K } else { 0 } } - /// On by default except for [`Application::LowDelay`], as in libopus: the - /// SILK layer's own delay is 312 samples, and only an aligned CELT layer - /// keeps CELT, SILK and hybrid frames of one stream at the same delay. - /// - /// Puts our CELT analysis window exactly where libopus's is: the layer is - /// fed the input delayed by `delay_compensation = Fs/250` (192 samples at - /// 48 kHz, `opus_encoder.c` ~:1955) and every frame first goes through - /// libopus's 3 Hz `dc_reject`. The alignment is *exact* — on the reell - /// attack frame (frame 2, 20 ms, LM=3, transient) our first eight band - /// log-energies become - /// 3.4386 4.0055 3.2603 4.5954 1.1790 4.2774 4.7584 3.7169 against - /// libopus's 3.439 4.005 3.260 4.595 1.179 4.277 4.758 3.717, all eight - /// within .0006 (the unaligned encoder is .06-.21 apart). - /// - /// It ships OFF because the 12-row library gate rejects it, and because - /// re-tuning the constants that were fitted on the misaligned spectrum - /// does not rescue it. Against the shipped rows - /// (`lanes/opus-opustr2-r1.sweep.txt`), err_ratio: - /// - /// | arm | better | worse (blocking) | - /// |---|---|---| - /// | 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 | - /// | delay + `dc_reject` (`opus-opusx2-r1`) | 5 | those, plus dl8a@64 .552→1.074, dl8a@96 .374→1.011 | - /// | + two-pass coarse energy (`-r2`) | 5 | same 7 rows (nik@96 1.809, dl8a@64 1.200) | - /// | + continuous 1.3 `alloc_trim` (`-r3`) | 5 | same 7, and corr worse by >.0005 on 9 rows | - /// | + tf boost k=1.25 (`-r4`) | 5 | same 7 | - /// | + tf boost k=1.5 (`-r5`) | 5 | same 7 | - /// | + two-pass *and* k=1.25, the combined best (`-r7`) | 5 | same 7: nik@64 .773, nik@96 1.801, her@96 1.114, dl8a@64 1.181, dl8a@96 1.014, hein@64 .977, hein@96 1.398 | - /// | + the whole libopus tonality analysis and its `alloc_trim`/`compute_vbr` consumers (`opus-opustonal-r4`) | 5 | same 7: nik@64 1.014, nik@96 1.842, her@96 1.217, dl8a@64 1.162, dl8a@96 .988, hein@64 .971, hein@96 1.514 | - /// - /// The best aligned arm on the KEEP rule is the delay alone without - /// `dc_reject` (5 rows worse); every arm that adds `dc_reject` puts dl8a - /// in the blocking set too. - /// - /// The last row is the one that was expected to rescue this: libopus's - /// actual operating point is the aligned spectrum *with* `analysis->valid` - /// true, and running the real analysis (`crates/ec-opus/src/analysis.rs`, - /// `music_prob` matching a patched-libopus trace to .001) moves reell@64 - /// 2.697→1.865, reell@96 4.499→3.986, reelo@64 1.510→1.123 and her@64 - /// 1.103→.952 — the largest movement any arm has produced on those rows — - /// and still leaves the same seven rows worse. - /// - /// The blocking set (nik, her@96, dl8a, hein) is the same in every arm and - /// no arm moves any of those rows back towards its shipped value, so the - /// misfit is not one of the swept constants. Turning this on also costs - /// 4 ms of algorithmic delay: [`Encoder::look_ahead`] for a CELT frame - /// becomes 312, not 120, and the Ogg pre-skip must follow it. - pub fn set_libopus_input_alignment(&mut self, on: bool) { - self.align_input = on; - self.filter = InputFilter::new(); - } - /// Forces the coded bandwidth; [`None`] (the default) picks it from the /// bitrate, the application and the input's Nyquist. pub fn set_bandwidth(&mut self, bandwidth: Option) { @@ -1929,7 +1873,7 @@ impl Encoder { } fn total_buffer(&self) -> usize { - if self.align_input { + if self.application != Application::LowDelay { self.sample_rate as usize / 250 } else { 0