forge
Explore
Log in

Don't you dare go Hollow

diag(opus): per-frame ours-vs-libopus decision dump; alloc_trim port rejected a third time

Browse files at this commit

fddb8d42e379cef06cd445269f5d92d7185ed4f7Ahmet Kaan GümüşParent: 1dee32d
The 64k transient gap is an alloc_trim difference, not a tf or anti-collapse
one. `frame_decisions_vs_libopus` decodes both bitstreams with our own decoder
and prints, per frame or aggregated over transient/steady frames, the coded
decisions: bytes, silence, intra, transient, anti_collapse, alloc_trim, spread,
coded_bands, tf_res, pulses, fine_quant, band energies. `EC_OPUS_TF_DEBUG=1`
adds the tf Viterbi's own metric/importance/selcost.

Aggregated over 2000 frames at 64k, our alloc_trim is systematically higher
than libopus's on every source -- +2.0 on transient frames (reelo 4.02 vs 1.99,
reell 4.59 vs 2.68, her 5.83 vs 3.88) and +1.0 on steady ones -- which is exactly
the missing `trim -= 2*tf_estimate` term plus the coarse pre-1.1 tilt ladder.
Mean tf_res and anti_collapse rates agree within noise, so those are not it.

Porting the libopus 1.3 float `alloc_trim_analysis` whole moves the 12-row
library gate 5 rows better / 4 worse on err_ratio and costs corr on every row;
the KEEP rule rejects it, and this is its third rejection. Numbers are recorded
at the decision site and in lanes/opus-opustr-r1.sweep.txt.

The reell/her pre-attack window is an ENCODER-side hole: our decoder replays
libopus's own bitstream at the source's -110 dBFS ramp level (1.5e-12, 4.0e-10
per 120-sample hop) while our encoder's stream decodes to 1e-18 there.
crates/ec-opus/src/celt_enc.rsmodified+20 -0
@@ -1457,6 +1457,19 @@ impl CeltEncoder {
1457 }1457 }
1458 1458
1459 /// `alloc_trim_analysis()`: stereo correlation plus spectral tilt.1459 /// `alloc_trim_analysis()`: stereo correlation plus spectral tilt.
1460+ ///
1461+ /// This is the pre-1.1 libopus discrete ladder, not the continuous 1.3
1462+ /// form. MEASURED (lane opustr r1, `lanes/opus-opustr-r1.sweep.txt`): the
1463+ /// whole 1.3 float function -- `trim += max(-4, .75*log2(1.001-sum^2))`,
1464+ /// `trim -= clamp(-2, 2, (diff+1)/6)` with `diff /= C*(end-1)`, and the
1465+ /// transient term `trim -= 2*tf_estimate` -- moves the 12-row library gate
1466+ /// 5 rows better / 4 worse on err_ratio (reelo@64 1.510->1.049, reell@96
1467+ /// 4.499->2.458, nik@64 .708->.648, dl8a .552/.374->.487/.336; against
1468+ /// reell@64 2.697->2.872, her@96 1.051->1.085, nik@96 .985->1.006, her@64
1469+ /// 1.103->1.109) and costs corr on every row (nik@64 .9887->.9878,
1470+ /// dl8a@64 .9889->.9879). The KEEP rule rejects it; this is the third
1471+ /// rejection of the same port (see `lanes/opus-trim-r2.{A,B}.sweep.txt`).
1472+ /// Do not re-port it without a fix for the transient rows first.
1460 fn alloc_trim_analysis(&mut self, end: usize, lm: usize, c: usize, n0: usize) -> i32 {1473 fn alloc_trim_analysis(&mut self, end: usize, lm: usize, c: usize, n0: usize) -> i32 {
1461 let mut trim = 5i32;1474 let mut trim = 5i32;
1462 if c == 2 {1475 if c == 2 {
@@ -1823,6 +1836,13 @@ impl CeltEncoder {
1823 if selcost[1] < selcost[0] && is_transient {1836 if selcost[1] < selcost[0] && is_transient {
1824 tf_select = 1;1837 tf_select = 1;
1825 }1838 }
1839+ if std::env::var_os("EC_OPUS_TF_DEBUG").is_some() {
1840+ eprintln!(
1841+ "tf: trans {is_transient} lm {lm} tfe {tf_estimate:.4} bias {bias:.5} \
1842+ lambda {lambda} sel {tf_select} selcost {selcost:?} metric {:?} imp {:?}",
1843+ &metric[..len], &importance[..len]
1844+ );
1845+ }
1826 1846
1827 // Final Viterbi forward pass1847 // Final Viterbi forward pass
1828 let mut cost0 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select]).abs();1848 let mut cost0 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select]).abs();
crates/ec-opus/tests/conformance.rsmodified+119 -0
@@ -5540,3 +5540,122 @@ fn err_map_vs_libopus() {
5540 let _ = fs::remove_file(&scratch);5540 let _ = fs::remove_file(&scratch);
5541 println!("wrote {}", out_path.display());5541 println!("wrote {}", out_path.display());
5542 }5542 }
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+ let mut enc = Encoder::new(48000, CH, Application::Audio).unwrap();
5590+ enc.set_bitrate(kbps * 1000);
5591+ enc.set_vbr_constrained(true);
5592+ let mut dec = Decoder::new(48000, CH).unwrap();
5593+ let mut out = vec![0u8; 1500];
5594+ let mut ours_rows = Vec::new();
5595+ for block in pcm.chunks_exact(960 * CH).take(to) {
5596+ let n = enc.encode_float(block, 960, &mut out).unwrap();
5597+ let ok = ec_opus::Toc::new(out[0]).mode() != Mode::Silk
5598+ && dec.decode_float(&out[..n], &mut buf).is_ok();
5599+ ours_rows.push(if ok { Some((n, dec.last_celt_diag().clone())) } else { None });
5600+ }
5601+
5602+ if to - from <= 40 {
5603+ for f in from..to {
5604+ match ours_rows.get(f).and_then(|x| x.as_ref()) {
5605+ Some((n, d)) => show("ours", f, *n, d),
5606+ None => println!("f{f} ours: (silk/absent)"),
5607+ }
5608+ match ref_rows.get(f).and_then(|x| x.as_ref()) {
5609+ Some((n, d)) => show("ref ", f, *n, d),
5610+ None => println!("f{f} ref : (silk/absent)"),
5611+ }
5612+ }
5613+ }
5614+
5615+ // Aggregate over the frames BOTH sides coded as CELT, split by whether
5616+ // either side called the frame transient: which coded decision differs.
5617+ let mut agg = [(0usize, [0.0f64; 8]); 2];
5618+ let mut trans_diff = 0usize;
5619+ for f in from..to {
5620+ let (Some((no, o)), Some((nr, r))) =
5621+ (ours_rows.get(f).and_then(|x| x.as_ref()), ref_rows.get(f).and_then(|x| x.as_ref()))
5622+ else {
5623+ continue;
5624+ };
5625+ if o.transient != r.transient {
5626+ trans_diff += 1;
5627+ }
5628+ let slot = usize::from(o.transient || r.transient);
5629+ let tf = |d: &ec_opus::celt::CeltDecDiag| {
5630+ d.tf_res[..NB_BANDS_DIAG].iter().sum::<i32>() as f64 / NB_BANDS_DIAG as f64
5631+ };
5632+ let a = &mut agg[slot];
5633+ a.0 += 1;
5634+ for (k, v) in [
5635+ *no as f64, *nr as f64,
5636+ o.alloc_trim as f64, r.alloc_trim as f64,
5637+ tf(o), tf(r),
5638+ f64::from(u8::from(o.anti_collapse)), f64::from(u8::from(r.anti_collapse)),
5639+ ]
5640+ .into_iter()
5641+ .enumerate()
5642+ {
5643+ a.1[k] += v;
5644+ }
5645+ }
5646+ println!("frames {from}..{to}, transient-flag disagreements {trans_diff}");
5647+ for (slot, name) in [(1usize, "transient"), (0, "steady ")] {
5648+ let (n, v) = agg[slot];
5649+ if n == 0 {
5650+ continue;
5651+ }
5652+ let m = |k: usize| v[k] / n as f64;
5653+ println!(
5654+ "{name} n={n}: bytes o {:.1} r {:.1} | trim o {:.2} r {:.2} | mean tf_res o {:.3} \
5655+ r {:.3} | anti_collapse o {:.2} r {:.2}",
5656+ m(0), m(1), m(2), m(3), m(4), m(5), m(6), m(7)
5657+ );
5658+ }
5659+}
5660+
5661+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