forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-opustr' (ef346028): per-frame coded-decision census vs libopus; three transient ports measured and reverted

Browse files at this commit

673c26b2cd22ebf09838ed85b12f454d1eaf9f27Ahmet Kaan GümüşParent: fbeac1931589bd
Encoder byte-identical (EC_OPUS_TF_DEBUG print only). Findings at 64k:
alloc_trim +2 on transient frames / +1 steady vs libopus, transient frames
underspend 7-12% at the reference's realised rate; the pre-attack digital
zero is encoder-side (a tf_select near-tie). alloc_trim (third rejection),
the x2 CVBR tf boost and the reservoir release all fail the all-rows rule.
crates/ec-opus/src/celt_enc.rsmodified+35 -4
@@ -891,10 +891,16 @@ impl CeltEncoder {
891891 // dynalloc boost minus calibration average (C:1651)
892892 target += total_boost - (19 << lm);
893893 // TF boost (C:1653-1654): MULT16_32_Q15 is identity in float mode,
894- // SHL32(.., 1) is the x2: 2 * (tf_estimate - 0.044) * target.
895- // Measured deviation: C applies SHL32(.., 1) (x2). On the 14-row
896- // library gate x2 loses .003 corr on reelm/hein@64 vs x1 (lane
897- // opus-vbr r2 vs r3); our tf_estimate is not scaled like libopus's.
894+ // SHL32(.., 1) is the x2 and tf_calibration is .04 at a fixed frame
895+ // size. MEASURED TWICE (lane opus-vbr r2/r3 on the old 14-row gate,
896+ // lane opustr r2 on the 12-row one): the x2 costs .0030 corr on
897+ // hein@64 and .0014 on hein@96 and pushes 5 err_ratio rows the
898+ // wrong way, so the x1 stand-in stays. The reelm rows that carried
899+ // the first rejection are gone; hein reproduced it exactly.
900+ // It is nonetheless the only lever measured to move the reell rows:
901+ // with it, reell@64 goes 2.697 -> 1.764 and reell@96 4.499 -> 2.114
902+ // (`lanes/opus-opustr-r2.sweep.txt`). Upgrade path: gate the x2 on
903+ // content -- it pays on music transients and starves hein's speech.
898904 target += ((tf_estimate - 0.044) * target as f32) as i32;
899905 // floor depth cap (C:1683-1695): SHR32/MULT16_32_Q15 are identity.
900906 {
@@ -941,6 +947,11 @@ impl CeltEncoder {
941947 // Release banked credit slowly (8 bytes per frame) so it stays
942948 // available for transient boosts; libopus CVBR re-adds all of
943949 // it to the next frame, which would defeat banking entirely.
950+ // MEASURED (lane opustr r3, `lanes/opus-opustr-r3.sweep.txt`):
951+ // restoring the libopus uncapped one-frame release on its own
952+ // is neutral-to-worse -- 3 rows better, 5 worse (dl8a@96
953+ // .374->.415, hein@96 .528->.555, her@96 1.051->1.064) and
954+ // hein@64 corr -.0008. The drip stays.
944955 let adjust = ((-self.vbr_reservoir) / (8 << BITRES)).min(8);
945956 if !silence {
946957 nb_avail += adjust;
@@ -1457,6 +1468,19 @@ impl CeltEncoder {
14571468 }
14581469
14591470 /// `alloc_trim_analysis()`: stereo correlation plus spectral tilt.
1471+ ///
1472+ /// This is the pre-1.1 libopus discrete ladder, not the continuous 1.3
1473+ /// form. MEASURED (lane opustr r1, `lanes/opus-opustr-r1.sweep.txt`): the
1474+ /// whole 1.3 float function -- `trim += max(-4, .75*log2(1.001-sum^2))`,
1475+ /// `trim -= clamp(-2, 2, (diff+1)/6)` with `diff /= C*(end-1)`, and the
1476+ /// transient term `trim -= 2*tf_estimate` -- moves the 12-row library gate
1477+ /// 5 rows better / 4 worse on err_ratio (reelo@64 1.510->1.049, reell@96
1478+ /// 4.499->2.458, nik@64 .708->.648, dl8a .552/.374->.487/.336; against
1479+ /// reell@64 2.697->2.872, her@96 1.051->1.085, nik@96 .985->1.006, her@64
1480+ /// 1.103->1.109) and costs corr on every row (nik@64 .9887->.9878,
1481+ /// dl8a@64 .9889->.9879). The KEEP rule rejects it; this is the third
1482+ /// rejection of the same port (see `lanes/opus-trim-r2.{A,B}.sweep.txt`).
1483+ /// Do not re-port it without a fix for the transient rows first.
14601484 fn alloc_trim_analysis(&mut self, end: usize, lm: usize, c: usize, n0: usize) -> i32 {
14611485 let mut trim = 5i32;
14621486 if c == 2 {
@@ -1823,6 +1847,13 @@ impl CeltEncoder {
18231847 if selcost[1] < selcost[0] && is_transient {
18241848 tf_select = 1;
18251849 }
1850+ if std::env::var_os("EC_OPUS_TF_DEBUG").is_some() {
1851+ eprintln!(
1852+ "tf: trans {is_transient} lm {lm} tfe {tf_estimate:.4} bias {bias:.5} \
1853+ lambda {lambda} sel {tf_select} selcost {selcost:?} metric {:?} imp {:?}",
1854+ &metric[..len], &importance[..len]
1855+ );
1856+ }
18261857
18271858 // Final Viterbi forward pass
18281859 let mut cost0 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select]).abs();
crates/ec-opus/tests/conformance.rsmodified+125 -0
@@ -5540,3 +5540,128 @@ fn err_map_vs_libopus() {
55405540 let _ = fs::remove_file(&scratch);
55415541 println!("wrote {}", out_path.display());
55425542 }
5543+
5544+/// Per-frame ours-vs-libopus coded decisions over a window of frames, decoded
5545+/// with our own decoder on both sides. FRAME_SRC / FRAME_KBPS / FRAME_FROM /
5546+/// FRAME_TO pick the source, rate and frame range (diagnostic).
5547+#[test]
5548+#[ignore]
5549+fn frame_decisions_vs_libopus() {
5550+ const CH: usize = 2;
5551+ let src_s = std::env::var("FRAME_SRC")
5552+ .unwrap_or_else(|_| "~/Music/reell.mp4".into());
5553+ let src = shellexpand(&src_s);
5554+ if !src.exists() {
5555+ eprintln!("SKIP: missing {}", src.display());
5556+ return;
5557+ }
5558+ let kbps: u32 = std::env::var("FRAME_KBPS").ok().and_then(|v| v.parse().ok()).unwrap_or(64);
5559+ let from: usize = std::env::var("FRAME_FROM").ok().and_then(|v| v.parse().ok()).unwrap_or(0);
5560+ let to: usize = std::env::var("FRAME_TO").ok().and_then(|v| v.parse().ok()).unwrap_or(6);
5561+ let secs = (to as f64 * 0.02 + 1.0).max(2.0);
5562+ let pcm = ffmpeg_decode_pcm(&src, secs);
5563+ let scratch = std::env::temp_dir().join("ec-opus-frame-dec.opus");
5564+ ffmpeg_encode_libopus(&src, kbps, &scratch, secs);
5565+ let ref_pkts: Vec<Vec<u8>> =
5566+ ogg_packets(&fs::read(&scratch).unwrap()).into_iter().skip(2).collect();
5567+
5568+ let show = |tag: &str, f: usize, bytes: usize, d: &ec_opus::celt::CeltDecDiag| {
5569+ let e: Vec<String> = d.old_band_e[..NB_BANDS_DIAG].iter().map(|v| format!("{v:.1}")).collect();
5570+ println!(
5571+ "f{f} {tag}: bytes {bytes} sil {} intra {} trans {} ac {} trim {} spread {} cb {} \
5572+ tf {:?} pulses {:?} fine {:?} E {}",
5573+ d.silence, d.intra, d.transient, d.anti_collapse, d.alloc_trim, d.spread,
5574+ d.coded_bands, &d.tf_res[..NB_BANDS_DIAG], &d.pulses[..NB_BANDS_DIAG],
5575+ &d.fine_quant[..NB_BANDS_DIAG], e.join(",")
5576+ );
5577+ };
5578+
5579+ let mut d = Decoder::new(48000, CH).unwrap();
5580+ let mut buf = vec![0.0f32; 5760 * CH];
5581+ let mut ref_rows = Vec::new();
5582+ for p in ref_pkts.iter().take(to) {
5583+ let ok = !p.is_empty()
5584+ && ec_opus::Toc::new(p[0]).mode() != Mode::Silk
5585+ && d.decode_float(p, &mut buf).is_ok();
5586+ ref_rows.push(if ok { Some((p.len(), d.last_celt_diag().clone())) } else { None });
5587+ }
5588+
5589+ // Encode at the reference's REALISED rate, like the library gate does:
5590+ // ffmpeg's libopus overshoots its -b:a target by up to 12% on these
5591+ // sources, and a flat target makes every byte column meaningless.
5592+ let ref_bytes: usize = ref_pkts.iter().take(to).map(Vec::len).sum();
5593+ let ref_kbps = ref_bytes as f64 * 8.0 / (ref_pkts.len().min(to) as f64 * 0.02) / 1000.0;
5594+ println!("ffmpeg target {kbps} kbps, realised {ref_kbps:.1} kbps");
5595+ let mut enc = Encoder::new(48000, CH, Application::Audio).unwrap();
5596+ enc.set_bitrate((ref_kbps * 1000.0).round() as u32);
5597+ enc.set_vbr_constrained(true);
5598+ let mut dec = Decoder::new(48000, CH).unwrap();
5599+ let mut out = vec![0u8; 1500];
5600+ let mut ours_rows = Vec::new();
5601+ for block in pcm.chunks_exact(960 * CH).take(to) {
5602+ let n = enc.encode_float(block, 960, &mut out).unwrap();
5603+ let ok = ec_opus::Toc::new(out[0]).mode() != Mode::Silk
5604+ && dec.decode_float(&out[..n], &mut buf).is_ok();
5605+ ours_rows.push(if ok { Some((n, dec.last_celt_diag().clone())) } else { None });
5606+ }
5607+
5608+ if to - from <= 40 {
5609+ for f in from..to {
5610+ match ours_rows.get(f).and_then(|x| x.as_ref()) {
5611+ Some((n, d)) => show("ours", f, *n, d),
5612+ None => println!("f{f} ours: (silk/absent)"),
5613+ }
5614+ match ref_rows.get(f).and_then(|x| x.as_ref()) {
5615+ Some((n, d)) => show("ref ", f, *n, d),
5616+ None => println!("f{f} ref : (silk/absent)"),
5617+ }
5618+ }
5619+ }
5620+
5621+ // Aggregate over the frames BOTH sides coded as CELT, split by whether
5622+ // either side called the frame transient: which coded decision differs.
5623+ let mut agg = [(0usize, [0.0f64; 8]); 2];
5624+ let mut trans_diff = 0usize;
5625+ for f in from..to {
5626+ let (Some((no, o)), Some((nr, r))) =
5627+ (ours_rows.get(f).and_then(|x| x.as_ref()), ref_rows.get(f).and_then(|x| x.as_ref()))
5628+ else {
5629+ continue;
5630+ };
5631+ if o.transient != r.transient {
5632+ trans_diff += 1;
5633+ }
5634+ let slot = usize::from(o.transient || r.transient);
5635+ let tf = |d: &ec_opus::celt::CeltDecDiag| {
5636+ d.tf_res[..NB_BANDS_DIAG].iter().sum::<i32>() as f64 / NB_BANDS_DIAG as f64
5637+ };
5638+ let a = &mut agg[slot];
5639+ a.0 += 1;
5640+ for (k, v) in [
5641+ *no as f64, *nr as f64,
5642+ o.alloc_trim as f64, r.alloc_trim as f64,
5643+ tf(o), tf(r),
5644+ f64::from(u8::from(o.anti_collapse)), f64::from(u8::from(r.anti_collapse)),
5645+ ]
5646+ .into_iter()
5647+ .enumerate()
5648+ {
5649+ a.1[k] += v;
5650+ }
5651+ }
5652+ println!("frames {from}..{to}, transient-flag disagreements {trans_diff}");
5653+ for (slot, name) in [(1usize, "transient"), (0, "steady ")] {
5654+ let (n, v) = agg[slot];
5655+ if n == 0 {
5656+ continue;
5657+ }
5658+ let m = |k: usize| v[k] / n as f64;
5659+ println!(
5660+ "{name} n={n}: bytes o {:.1} r {:.1} | trim o {:.2} r {:.2} | mean tf_res o {:.3} \
5661+ r {:.3} | anti_collapse o {:.2} r {:.2}",
5662+ m(0), m(1), m(2), m(3), m(4), m(5), m(6), m(7)
5663+ );
5664+ }
5665+}
5666+
5667+const NB_BANDS_DIAG: usize = ec_opus::celt::NB_BANDS;
lanes/opus-opustr-r1.sweep.txtadded+14 -0
@@ -0,0 +1,14 @@
1+# lane-opustr r1 REVERTED: libopus-1.3 alloc_trim_analysis port (continuous tilt + 0.75*logXC stereo + 2*tf_estimate transient term)
2+# vs lanes/opus-opus64-r3.sweep.txt. 12 rows (reelm.wav gone, its 2 rows SKIP).
3+nik@64k: ref 69.2 ours 69.1 kbps (-0.1%), corr o=0.9878 r=0.9864 gap=-0.0015, Q o=-82.01 r=-154.78, err_ratio 0.648, minsec o=0.9760 r=0.9745, drop o=0 r=0
4+nik@96k: ref 101.0 ours 100.9 kbps (-0.1%), corr o=0.9942 r=0.9940 gap=-0.0001, Q o=-32.19 r=-31.55, err_ratio 1.006, minsec o=0.9880 r=0.9888, drop o=0 r=0
5+reelo@64k: ref 63.6 ours 63.6 kbps (-0.0%), corr o=0.9890 r=0.9866 gap=-0.0023, Q o=-131.74 r=-123.36, err_ratio 1.049, minsec o=0.9700 r=0.9686, drop o=0 r=0
6+reelo@96k: ref 93.9 ours 93.4 kbps (-0.5%), corr o=0.9944 r=0.9937 gap=-0.0007, Q o=-109.23 r=-125.18, err_ratio 0.910, minsec o=0.9859 r=0.9854, drop o=0 r=0
7+her@64k: ref 67.9 ours 67.7 kbps (-0.4%), corr o=0.9827 r=0.9794 gap=-0.0033, Q o=-367.34 r=-339.00, err_ratio 1.109, minsec o=0.9463 r=0.9406, drop o=0 r=0
8+her@96k: ref 101.5 ours 101.0 kbps (-0.5%), corr o=0.9910 r=0.9902 gap=-0.0007, Q o=-208.80 r=-191.50, err_ratio 1.085, minsec o=0.9719 r=0.9728, drop o=0 r=0
9+reell@64k: ref 71.3 ours 71.3 kbps (-0.0%), corr o=0.9900 r=0.9880 gap=-0.0020, Q o=-369.03 r=-128.39, err_ratio 2.872, minsec o=0.9711 r=0.9644, drop o=0 r=0
10+reell@96k: ref 105.4 ours 105.3 kbps (-0.0%), corr o=0.9951 r=0.9948 gap=-0.0003, Q o=-165.27 r=-29.16, err_ratio 2.458, minsec o=0.9847 r=0.9804, drop o=0 r=0
11+dl8a@64k: ref 65.8 ours 65.7 kbps (-0.1%), corr o=0.9879 r=0.9865 gap=-0.0014, Q o=-200.20 r=-378.84, err_ratio 0.487, minsec o=0.9773 r=0.9723, drop o=0 r=0
12+dl8a@96k: ref 96.8 ours 96.5 kbps (-0.3%), corr o=0.9936 r=0.9934 gap=-0.0002, Q o=-156.64 r=-423.47, err_ratio 0.336, minsec o=0.9870 r=0.9864, drop o=0 r=0
13+hein@64k: ref 64.9 ours 64.8 kbps (-0.2%), corr o=0.9886 r=0.9874 gap=-0.0013, Q o=-344.06 r=-388.31, err_ratio 0.853, minsec o=0.9318 r=0.9148, drop o=0 r=0
14+hein@96k: ref 86.9 ours 86.8 kbps (-0.2%), corr o=0.9945 r=0.9930 gap=-0.0015, Q o=-117.93 r=-246.46, err_ratio 0.528, minsec o=0.9689 r=0.9499, drop o=0 r=0
lanes/opus-opustr-r2.sweep.txtadded+14 -0
@@ -0,0 +1,14 @@
1+# lane-opustr r2 REVERTED: libopus CVBR restored whole -- tf boost 2*(tf_estimate-.04) AND
2+# the uncapped one-frame reservoir release. vs lanes/opus-opus64-r3.sweep.txt. 12 rows.
3+nik@64k: ref 69.2 ours 68.9 kbps (-0.3%), corr o=0.9885 r=0.9864 gap=-0.0022, Q o=-78.35 r=-154.78, err_ratio 0.632, minsec o=0.9764 r=0.9745, drop o=0 r=0
4+nik@96k: ref 101.0 ours 100.9 kbps (-0.1%), corr o=0.9947 r=0.9940 gap=-0.0006, Q o=-17.96 r=-31.55, err_ratio 0.881, minsec o=0.9883 r=0.9888, drop o=0 r=0
5+reelo@64k: ref 63.6 ours 63.4 kbps (-0.2%), corr o=0.9888 r=0.9866 gap=-0.0021, Q o=-202.91 r=-123.36, err_ratio 1.510, minsec o=0.9649 r=0.9686, drop o=0 r=0
6+reelo@96k: ref 93.9 ours 93.7 kbps (-0.1%), corr o=0.9945 r=0.9937 gap=-0.0008, Q o=-129.08 r=-125.18, err_ratio 1.023, minsec o=0.9847 r=0.9854, drop o=0 r=0
7+her@64k: ref 67.9 ours 67.8 kbps (-0.2%), corr o=0.9831 r=0.9794 gap=-0.0036, Q o=-367.21 r=-339.00, err_ratio 1.109, minsec o=0.9457 r=0.9406, drop o=0 r=0
8+her@96k: ref 101.5 ours 101.3 kbps (-0.2%), corr o=0.9912 r=0.9902 gap=-0.0010, Q o=-205.38 r=-191.50, err_ratio 1.068, minsec o=0.9715 r=0.9728, drop o=0 r=0
9+reell@64k: ref 71.3 ours 71.2 kbps (-0.1%), corr o=0.9907 r=0.9880 gap=-0.0027, Q o=-244.05 r=-128.39, err_ratio 1.764, minsec o=0.9709 r=0.9644, drop o=0 r=0
10+reell@96k: ref 105.4 ours 105.2 kbps (-0.1%), corr o=0.9956 r=0.9948 gap=-0.0008, Q o=-137.01 r=-29.16, err_ratio 2.114, minsec o=0.9853 r=0.9804, drop o=0 r=0
11+dl8a@64k: ref 65.8 ours 65.7 kbps (-0.1%), corr o=0.9884 r=0.9865 gap=-0.0019, Q o=-233.24 r=-378.84, err_ratio 0.566, minsec o=0.9778 r=0.9723, drop o=0 r=0
12+dl8a@96k: ref 96.8 ours 96.8 kbps (-0.0%), corr o=0.9941 r=0.9934 gap=-0.0007, Q o=-192.92 r=-423.47, err_ratio 0.403, minsec o=0.9877 r=0.9864, drop o=0 r=0
13+hein@64k: ref 64.9 ours 64.9 kbps (-0.0%), corr o=0.9858 r=0.9874 gap=+0.0016, Q o=-195.97 r=-388.31, err_ratio 0.462, minsec o=0.9139 r=0.9148, drop o=0 r=0
14+hein@96k: ref 86.9 ours 86.9 kbps (-0.0%), corr o=0.9932 r=0.9930 gap=-0.0002, Q o=-92.58 r=-246.46, err_ratio 0.451, minsec o=0.9620 r=0.9499, drop o=0 r=0
lanes/opus-opustr-r3.sweep.txtadded+14 -0
@@ -0,0 +1,14 @@
1+# lane-opustr r3 REVERTED: only the uncapped one-frame CVBR reservoir release (tf boost left at x1).
2+# vs lanes/opus-opus64-r3.sweep.txt. 12 rows (reelm.wav gone, its 2 rows SKIP).
3+nik@64k: ref 69.2 ours 69.0 kbps (-0.3%), corr o=0.9886 r=0.9864 gap=-0.0022, Q o=-81.38 r=-154.78, err_ratio 0.646, minsec o=0.9766 r=0.9745, drop o=0 r=0
4+nik@96k: ref 101.0 ours 100.9 kbps (-0.1%), corr o=0.9948 r=0.9940 gap=-0.0007, Q o=-30.21 r=-31.55, err_ratio 0.988, minsec o=0.9889 r=0.9888, drop o=0 r=0
5+reelo@64k: ref 63.6 ours 63.4 kbps (-0.2%), corr o=0.9891 r=0.9866 gap=-0.0024, Q o=-202.79 r=-123.36, err_ratio 1.509, minsec o=0.9673 r=0.9686, drop o=0 r=0
6+reelo@96k: ref 93.9 ours 93.7 kbps (-0.1%), corr o=0.9947 r=0.9937 gap=-0.0010, Q o=-108.03 r=-125.18, err_ratio 0.903, minsec o=0.9857 r=0.9854, drop o=0 r=0
7+her@64k: ref 67.9 ours 67.8 kbps (-0.2%), corr o=0.9834 r=0.9794 gap=-0.0039, Q o=-367.31 r=-339.00, err_ratio 1.109, minsec o=0.9475 r=0.9406, drop o=0 r=0
8+her@96k: ref 101.5 ours 101.3 kbps (-0.2%), corr o=0.9914 r=0.9902 gap=-0.0012, Q o=-204.58 r=-191.50, err_ratio 1.064, minsec o=0.9727 r=0.9728, drop o=0 r=0
9+reell@64k: ref 71.3 ours 71.3 kbps (-0.1%), corr o=0.9908 r=0.9880 gap=-0.0028, Q o=-351.70 r=-128.39, err_ratio 2.697, minsec o=0.9710 r=0.9644, drop o=0 r=0
10+reell@96k: ref 105.4 ours 105.2 kbps (-0.1%), corr o=0.9956 r=0.9948 gap=-0.0008, Q o=-301.83 r=-29.16, err_ratio 4.499, minsec o=0.9856 r=0.9804, drop o=0 r=0
11+dl8a@64k: ref 65.8 ours 65.7 kbps (-0.1%), corr o=0.9887 r=0.9865 gap=-0.0022, Q o=-227.27 r=-378.84, err_ratio 0.551, minsec o=0.9785 r=0.9723, drop o=0 r=0
12+dl8a@96k: ref 96.8 ours 96.8 kbps (-0.0%), corr o=0.9943 r=0.9934 gap=-0.0009, Q o=-199.03 r=-423.47, err_ratio 0.415, minsec o=0.9886 r=0.9864, drop o=0 r=0
13+hein@64k: ref 64.9 ours 64.9 kbps (-0.0%), corr o=0.9880 r=0.9874 gap=-0.0006, Q o=-344.06 r=-388.31, err_ratio 0.853, minsec o=0.9277 r=0.9148, drop o=0 r=0
14+hein@96k: ref 86.9 ours 86.9 kbps (-0.0%), corr o=0.9944 r=0.9930 gap=-0.0014, Q o=-126.54 r=-246.46, err_ratio 0.555, minsec o=0.9697 r=0.9499, drop o=0 r=0