forge
Explore
Log in

Don't you dare go Hollow

Intensity band from libopus equiv_rate + hysteresis_decision

Browse files at this commit

9c017b0eb46ad5232d7f140d45f5b20b6790d091Ahmet Kaan GümüşParent: 501c871
  • feat
    • intensity band from libopus equiv_rate + hysteresis_decision
Show full message
feat(opus-int/r1): intensity band from libopus equiv_rate + hysteresis_decision

Replaces the 1.0-era effective_rate ladder with the 1.3.1 formula and
threshold/hysteresis tables, shared by the VBR target and the stereo
decision so CBR frames get it too. 14-row gate corr-neutral (±.0001).
crates/ec-opus/src/celt_enc.rsmodified+51 -35
@@ -393,6 +393,8 @@ pub struct CeltEncoder {
393 band_log_e: Vec<f32>,393 band_log_e: Vec<f32>,
394 /// Long-window log energies used by dynalloc (`bandLogE2`).394 /// Long-window log energies used by dynalloc (`bandLogE2`).
395 band_log_e2: Vec<f32>,395 band_log_e2: Vec<f32>,
396+ /// Previous intensity decision (hysteresis state, libopus `st->intensity`).
397+ intensity: usize,
396 error: Vec<f32>,398 error: Vec<f32>,
397 transient_tmp: Vec<f32>,399 transient_tmp: Vec<f32>,
398 hadamard_tmp: Vec<f32>,400 hadamard_tmp: Vec<f32>,
@@ -442,6 +444,7 @@ impl CeltEncoder {
442 band_e: vec![0.0; 2 * NB_BANDS],444 band_e: vec![0.0; 2 * NB_BANDS],
443 band_log_e: vec![0.0; 2 * NB_BANDS],445 band_log_e: vec![0.0; 2 * NB_BANDS],
444 band_log_e2: vec![0.0; 2 * NB_BANDS],446 band_log_e2: vec![0.0; 2 * NB_BANDS],
447+ intensity: 0,
445 error: vec![0.0; 2 * NB_BANDS],448 error: vec![0.0; 2 * NB_BANDS],
446 transient_tmp: vec![0.0; max_n + OVERLAP],449 transient_tmp: vec![0.0; max_n + OVERLAP],
447 hadamard_tmp: vec![0.0; max_n],450 hadamard_tmp: vec![0.0; max_n],
@@ -797,24 +800,8 @@ impl CeltEncoder {
797 // terms that need tonality/tf analysis this encoder does not run.800 // terms that need tonality/tf analysis this encoder does not run.
798 let coded_bands = end - 1;801 let coded_bands = end - 1;
799 let intensity_est = if c == 2 {802 let intensity_est = if c == 2 {
800- // Same thresholds as the stereo section below (16 at 64k stereo).803+ self.intensity_decision(nb_compressed, c, lm, vbr_rate_bps);
801- let effective_rate = 2 * (((8 * effective_bytes - 80) >> lm) as i32) / 5;804+ self.intensity.clamp(start, coded_bands)
802- let i = if effective_rate < 35 {
803- 8usize
804- } else if effective_rate < 50 {
805- 12
806- } else if effective_rate < 68 {
807- 16
808- } else if effective_rate < 84 {
809- 18
810- } else if effective_rate < 102 {
811- 19
812- } else if effective_rate < 130 {
813- 20
814- } else {
815- 100
816- };
817- i.clamp(start, coded_bands)
818 } else {805 } else {
819 0806 0
820 };807 };
@@ -921,23 +908,8 @@ impl CeltEncoder {
921 if lm != 0 {908 if lm != 0 {
922 dual_stereo = self.stereo_analysis(lm, n);909 dual_stereo = self.stereo_analysis(lm, n);
923 }910 }
924- let effective_rate = 2 * (((8 * effective_bytes - 80) >> lm) as i32) / 5;911+ self.intensity_decision(nb_compressed, c, lm, vbr_rate_bps);
925- intensity = if effective_rate < 35 {912+ intensity = self.intensity.clamp(start, end);
926- 8
927- } else if effective_rate < 50 {
928- 12
929- } else if effective_rate < 68 {
930- 16
931- } else if effective_rate < 84 {
932- 18
933- } else if effective_rate < 102 {
934- 19
935- } else if effective_rate < 130 {
936- 20
937- } else {
938- 100
939- };
940- intensity = intensity.clamp(start, end);
941 }913 }
942 let _ = &mut effective_bytes;914 let _ = &mut effective_bytes;
943 915
@@ -1460,6 +1432,23 @@ impl CeltEncoder {
1460 trim.clamp(0, 10)1432 trim.clamp(0, 10)
1461 }1433 }
1462 1434
1435+ /// libopus `equiv_rate` + `hysteresis_decision()` for the intensity band:
1436+ /// the frame's bytes as a 20 ms-equivalent bitrate minus the per-frame
1437+ /// overhead, capped by the VBR target. Idempotent within a frame.
1438+ fn intensity_decision(&mut self, nb_compressed: usize, c: usize, lm: usize, vbr_rate_bps: u32) {
1439+ let overhead = (40 * c as i32 + 20) * ((400 >> lm) - 50);
1440+ let mut equiv_rate = ((nb_compressed as i32 * 8 * 50) << (3 - lm)) - overhead;
1441+ if vbr_rate_bps > 0 {
1442+ equiv_rate = equiv_rate.min(vbr_rate_bps as i32 - overhead);
1443+ }
1444+ self.intensity = hysteresis_decision(
1445+ equiv_rate as f32 / 1000.0,
1446+ &INTENSITY_THRESHOLDS,
1447+ &INTENSITY_HYSTERESIS,
1448+ self.intensity,
1449+ );
1450+ }
1451+
1463 /// `stereo_analysis()`: L1 entropy model of L/R against M/S.1452 /// `stereo_analysis()`: L1 entropy model of L/R against M/S.
1464 fn stereo_analysis(&self, lm: usize, n0: usize) -> bool {1453 fn stereo_analysis(&self, lm: usize, n0: usize) -> bool {
1465 let mut sum_lr = 1e-15f64;1454 let mut sum_lr = 1e-15f64;
@@ -2498,3 +2487,30 @@ mod tests {
2498 }2487 }
2499 }2488 }
2500 }2489 }
2490+
2491+const INTENSITY_THRESHOLDS: [f32; 21] = [
2492+ 1., 2., 3., 4., 5., 6., 7., 8., 16., 24., 36., 44., 50., 56., 62., 67., 72., 79., 88., 106.,
2493+ 134.,
2494+];
2495+const INTENSITY_HYSTERESIS: [f32; 21] =
2496+ [1., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 2., 3., 3., 4., 5., 6., 8., 8.];
2497+
2498+/// libopus `hysteresis_decision()`: the largest `i` with `val >= thresholds[i]`,
2499+/// sticky around the previous decision by `hysteresis`.
2500+fn hysteresis_decision(val: f32, thresholds: &[f32], hysteresis: &[f32], prev: usize) -> usize {
2501+ let n = thresholds.len();
2502+ let mut i = 0;
2503+ while i < n {
2504+ if val < thresholds[i] {
2505+ break;
2506+ }
2507+ i += 1;
2508+ }
2509+ if i > prev && val < thresholds[prev] + hysteresis[prev] {
2510+ i = prev;
2511+ }
2512+ if i < prev && val > thresholds[prev - 1] - hysteresis[prev - 1] {
2513+ i = prev;
2514+ }
2515+ i
2516+}
lanes/opus-int-r1.report.mdadded+10 -0
@@ -0,0 +1,10 @@
1+# lane opus-int r1 — intensity band via libopus `equiv_rate` + `hysteresis_decision`
2+
3+## What
4+- `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.
5+- 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.
6+
7+## Measured
8+- 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.
9+- 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.
10+- Suite: `cargo test -p ec-opus --release` 34 + 27 passed, 9 ignored.
lanes/opus-int-r1.sweep.txtadded+16 -0
@@ -0,0 +1,16 @@
1+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
2+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
3+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
4+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
5+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
6+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
7+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
8+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
9+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
10+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
11+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
12+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
13+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
14+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
15+RATE GATE: all rows within ±5%
16+DROPOUT GATE: passed (no ours-only dropouts)