diff --git a/crates/ec-opus/src/celt_enc.rs b/crates/ec-opus/src/celt_enc.rs index 5db6e3e1..a0edb8c8 100644 --- a/crates/ec-opus/src/celt_enc.rs +++ b/crates/ec-opus/src/celt_enc.rs @@ -393,6 +393,8 @@ pub struct CeltEncoder { band_log_e: Vec, /// Long-window log energies used by dynalloc (`bandLogE2`). band_log_e2: Vec, + /// Previous intensity decision (hysteresis state, libopus `st->intensity`). + intensity: usize, error: Vec, transient_tmp: Vec, hadamard_tmp: Vec, @@ -442,6 +444,7 @@ impl CeltEncoder { band_e: vec![0.0; 2 * NB_BANDS], band_log_e: vec![0.0; 2 * NB_BANDS], band_log_e2: vec![0.0; 2 * NB_BANDS], + intensity: 0, error: vec![0.0; 2 * NB_BANDS], transient_tmp: vec![0.0; max_n + OVERLAP], hadamard_tmp: vec![0.0; max_n], @@ -797,24 +800,8 @@ impl CeltEncoder { // terms that need tonality/tf analysis this encoder does not run. let coded_bands = end - 1; let intensity_est = if c == 2 { - // Same thresholds as the stereo section below (16 at 64k stereo). - let effective_rate = 2 * (((8 * effective_bytes - 80) >> lm) as i32) / 5; - let i = if effective_rate < 35 { - 8usize - } else if effective_rate < 50 { - 12 - } else if effective_rate < 68 { - 16 - } else if effective_rate < 84 { - 18 - } else if effective_rate < 102 { - 19 - } else if effective_rate < 130 { - 20 - } else { - 100 - }; - i.clamp(start, coded_bands) + self.intensity_decision(nb_compressed, c, lm, vbr_rate_bps); + self.intensity.clamp(start, coded_bands) } else { 0 }; @@ -921,23 +908,8 @@ impl CeltEncoder { if lm != 0 { dual_stereo = self.stereo_analysis(lm, n); } - let effective_rate = 2 * (((8 * effective_bytes - 80) >> lm) as i32) / 5; - intensity = if effective_rate < 35 { - 8 - } else if effective_rate < 50 { - 12 - } else if effective_rate < 68 { - 16 - } else if effective_rate < 84 { - 18 - } else if effective_rate < 102 { - 19 - } else if effective_rate < 130 { - 20 - } else { - 100 - }; - intensity = intensity.clamp(start, end); + self.intensity_decision(nb_compressed, c, lm, vbr_rate_bps); + intensity = self.intensity.clamp(start, end); } let _ = &mut effective_bytes; @@ -1460,6 +1432,23 @@ impl CeltEncoder { trim.clamp(0, 10) } + /// libopus `equiv_rate` + `hysteresis_decision()` for the intensity band: + /// the frame's bytes as a 20 ms-equivalent bitrate minus the per-frame + /// overhead, capped by the VBR target. Idempotent within a frame. + fn intensity_decision(&mut self, nb_compressed: usize, c: usize, lm: usize, vbr_rate_bps: u32) { + let overhead = (40 * c as i32 + 20) * ((400 >> lm) - 50); + let mut equiv_rate = ((nb_compressed as i32 * 8 * 50) << (3 - lm)) - overhead; + if vbr_rate_bps > 0 { + equiv_rate = equiv_rate.min(vbr_rate_bps as i32 - overhead); + } + self.intensity = hysteresis_decision( + equiv_rate as f32 / 1000.0, + &INTENSITY_THRESHOLDS, + &INTENSITY_HYSTERESIS, + self.intensity, + ); + } + /// `stereo_analysis()`: L1 entropy model of L/R against M/S. fn stereo_analysis(&self, lm: usize, n0: usize) -> bool { let mut sum_lr = 1e-15f64; @@ -2498,3 +2487,30 @@ mod tests { } } } + +const INTENSITY_THRESHOLDS: [f32; 21] = [ + 1., 2., 3., 4., 5., 6., 7., 8., 16., 24., 36., 44., 50., 56., 62., 67., 72., 79., 88., 106., + 134., +]; +const INTENSITY_HYSTERESIS: [f32; 21] = + [1., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 2., 3., 3., 4., 5., 6., 8., 8.]; + +/// libopus `hysteresis_decision()`: the largest `i` with `val >= thresholds[i]`, +/// sticky around the previous decision by `hysteresis`. +fn hysteresis_decision(val: f32, thresholds: &[f32], hysteresis: &[f32], prev: usize) -> usize { + let n = thresholds.len(); + let mut i = 0; + while i < n { + if val < thresholds[i] { + break; + } + i += 1; + } + if i > prev && val < thresholds[prev] + hysteresis[prev] { + i = prev; + } + if i < prev && val > thresholds[prev - 1] - hysteresis[prev - 1] { + i = prev; + } + i +} diff --git a/lanes/opus-int-r1.report.md b/lanes/opus-int-r1.report.md new file mode 100644 index 00000000..a0f2c672 --- /dev/null +++ b/lanes/opus-int-r1.report.md @@ -0,0 +1,10 @@ +# lane opus-int r1 — intensity band via libopus `equiv_rate` + `hysteresis_decision` + +## What +- `celt_enc.rs`: the 1.0-era `effective_rate` ladder (8/12/16/18/19/20/100) is replaced by libopus 1.3.1 `equiv_rate = (bytes*8*50 << (3-LM)) - (40C+20)((400>>LM)-50)`, capped by the VBR bitrate, through the 21-entry `intensity_thresholds` / `intensity_histeresis` tables with per-encoder hysteresis state (`CeltEncoder::intensity`). One method `intensity_decision()` is called at both consumers (VBR target `coded_bins` and the stereo decision) so CBR frames get the decision too. +- Defect caught by the suite on the first cut: the decision only ran inside the VBR-target branch, so CBR frames kept `intensity = 0` (all bands mono): `encoder_roundtrips_at_every_rate` 2 ch 32 kbps corr 0.7989. Fixed by the shared method. + +## Measured +- Intensity band (`short_block_bits_vs_libopus`, band start Hz): reelm@64 long ours 8160 → 5760 (ref 6720), short 8160 → 3840 (ref 5760); hein@64 long 6720 → 5760 (ref 5760), short 8160 → 5760 (ref 6720). Residual: the system ref (libopus 1.6 via ffmpeg) sits one band higher on long frames than the 1.3.1 formula yields at 64 kbps (needs bitrate ≥ 67k for band 15); the 1.5-era `celt_encoder.c` on disk has the same table/formula, so the offset is upstream of CELT (opus_encoder bitrate handed to CELT) and not verifiable without the 1.6 source. +- 14-row gate vs `lanes/opus-sp-r1.sweep.txt`: every row within ±.0001 corr (reelm@64 +.0039 → +.0038, reelo@64 −.0017 → −.0018); RATE ±5% pass (max −1.8%); DROPOUT 0. Intensity band position is corr-neutral at 64/96 kbps on this library. +- Suite: `cargo test -p ec-opus --release` 34 + 27 passed, 9 ignored. diff --git a/lanes/opus-int-r1.sweep.txt b/lanes/opus-int-r1.sweep.txt new file mode 100644 index 00000000..18258eed --- /dev/null +++ b/lanes/opus-int-r1.sweep.txt @@ -0,0 +1,16 @@ +nik@64k: ref 69.2 ours 68.7 kbps (-0.7%), corr o=0.9883 r=0.9864 gap=-0.0020, Q o=-126.20 r=-154.78, err_ratio 0.854, minsec o=0.9765 r=0.9745, drop o=0 r=0 +nik@96k: ref 101.0 ours 100.6 kbps (-0.4%), corr o=0.9947 r=0.9940 gap=-0.0006, Q o=-49.40 r=-31.55, err_ratio 1.162, minsec o=0.9888 r=0.9888, drop o=0 r=0 +reelo@64k: ref 63.6 ours 63.5 kbps (-0.1%), corr o=0.9884 r=0.9866 gap=-0.0018, Q o=-202.91 r=-123.36, err_ratio 1.510, minsec o=0.9629 r=0.9686, drop o=0 r=0 +reelo@96k: ref 93.9 ours 93.8 kbps (-0.1%), corr o=0.9945 r=0.9937 gap=-0.0008, Q o=-127.38 r=-125.18, err_ratio 1.013, minsec o=0.9855 r=0.9854, drop o=0 r=0 +her@64k: ref 67.9 ours 67.4 kbps (-0.8%), corr o=0.9826 r=0.9794 gap=-0.0031, Q o=-365.27 r=-339.00, err_ratio 1.101, minsec o=0.9450 r=0.9406, drop o=0 r=0 +her@96k: ref 101.5 ours 100.6 kbps (-0.9%), corr o=0.9911 r=0.9902 gap=-0.0008, Q o=-206.04 r=-191.50, err_ratio 1.071, minsec o=0.9715 r=0.9728, drop o=0 r=0 +reell@64k: ref 71.3 ours 71.3 kbps (-0.1%), corr o=0.9907 r=0.9880 gap=-0.0028, Q o=-323.58 r=-128.39, err_ratio 2.429, minsec o=0.9704 r=0.9644, drop o=0 r=0 +reell@96k: ref 105.4 ours 105.3 kbps (-0.0%), corr o=0.9956 r=0.9948 gap=-0.0008, Q o=-156.53 r=-29.16, err_ratio 2.349, minsec o=0.9858 r=0.9804, drop o=0 r=0 +reelm@64k: ref 63.3 ours 62.3 kbps (-1.5%), corr o=0.9821 r=0.9859 gap=+0.0038, Q o=-270.38 r=-392.46, err_ratio 0.631, minsec o=0.9123 r=0.9100, drop o=0 r=0 +reelm@96k: ref 84.8 ours 83.6 kbps (-1.5%), corr o=0.9920 r=0.9920 gap=-0.0000, Q o=-139.95 r=-231.16, err_ratio 0.640, minsec o=0.9617 r=0.9511, drop o=0 r=0 +dl8a@64k: ref 65.8 ours 65.6 kbps (-0.3%), corr o=0.9881 r=0.9865 gap=-0.0017, Q o=-210.79 r=-378.84, err_ratio 0.512, minsec o=0.9752 r=0.9723, drop o=0 r=0 +dl8a@96k: ref 96.8 ours 96.1 kbps (-0.8%), corr o=0.9940 r=0.9934 gap=-0.0006, Q o=-193.52 r=-423.47, err_ratio 0.404, minsec o=0.9860 r=0.9864, drop o=0 r=0 +hein@64k: ref 64.9 ours 63.8 kbps (-1.6%), corr o=0.9852 r=0.9874 gap=+0.0022, Q o=-286.22 r=-388.31, err_ratio 0.683, minsec o=0.9144 r=0.9148, drop o=0 r=0 +hein@96k: ref 86.9 ours 85.4 kbps (-1.8%), corr o=0.9933 r=0.9930 gap=-0.0002, Q o=-153.23 r=-246.46, err_ratio 0.643, minsec o=0.9618 r=0.9499, drop o=0 r=0 +RATE GATE: all rows within ±5% +DROPOUT GATE: passed (no ours-only dropouts)