diff --git a/crates/ec-opus/src/celt_enc.rs b/crates/ec-opus/src/celt_enc.rs index c65b2249..8f6b01c4 100644 --- a/crates/ec-opus/src/celt_enc.rs +++ b/crates/ec-opus/src/celt_enc.rs @@ -1457,6 +1457,19 @@ impl CeltEncoder { } /// `alloc_trim_analysis()`: stereo correlation plus spectral tilt. + /// + /// This is the pre-1.1 libopus discrete ladder, not the continuous 1.3 + /// form. MEASURED (lane opustr r1, `lanes/opus-opustr-r1.sweep.txt`): the + /// whole 1.3 float function -- `trim += max(-4, .75*log2(1.001-sum^2))`, + /// `trim -= clamp(-2, 2, (diff+1)/6)` with `diff /= C*(end-1)`, and the + /// transient term `trim -= 2*tf_estimate` -- moves the 12-row library gate + /// 5 rows better / 4 worse on err_ratio (reelo@64 1.510->1.049, reell@96 + /// 4.499->2.458, nik@64 .708->.648, dl8a .552/.374->.487/.336; against + /// reell@64 2.697->2.872, her@96 1.051->1.085, nik@96 .985->1.006, her@64 + /// 1.103->1.109) and costs corr on every row (nik@64 .9887->.9878, + /// dl8a@64 .9889->.9879). The KEEP rule rejects it; this is the third + /// rejection of the same port (see `lanes/opus-trim-r2.{A,B}.sweep.txt`). + /// Do not re-port it without a fix for the transient rows first. fn alloc_trim_analysis(&mut self, end: usize, lm: usize, c: usize, n0: usize) -> i32 { let mut trim = 5i32; if c == 2 { @@ -1823,6 +1836,13 @@ impl CeltEncoder { if selcost[1] < selcost[0] && is_transient { tf_select = 1; } + if std::env::var_os("EC_OPUS_TF_DEBUG").is_some() { + eprintln!( + "tf: trans {is_transient} lm {lm} tfe {tf_estimate:.4} bias {bias:.5} \ + lambda {lambda} sel {tf_select} selcost {selcost:?} metric {:?} imp {:?}", + &metric[..len], &importance[..len] + ); + } // Final Viterbi forward pass let mut cost0 = importance[0] * (metric[0] - 2 * TF_SELECT[lm][4 * t + 2 * tf_select]).abs(); diff --git a/crates/ec-opus/tests/conformance.rs b/crates/ec-opus/tests/conformance.rs index ec21e0ae..e5d59d32 100644 --- a/crates/ec-opus/tests/conformance.rs +++ b/crates/ec-opus/tests/conformance.rs @@ -5540,3 +5540,122 @@ fn err_map_vs_libopus() { let _ = fs::remove_file(&scratch); println!("wrote {}", out_path.display()); } + +/// Per-frame ours-vs-libopus coded decisions over a window of frames, decoded +/// with our own decoder on both sides. FRAME_SRC / FRAME_KBPS / FRAME_FROM / +/// FRAME_TO pick the source, rate and frame range (diagnostic). +#[test] +#[ignore] +fn frame_decisions_vs_libopus() { + const CH: usize = 2; + let src_s = std::env::var("FRAME_SRC") + .unwrap_or_else(|_| "~/Music/reell.mp4".into()); + let src = shellexpand(&src_s); + if !src.exists() { + eprintln!("SKIP: missing {}", src.display()); + return; + } + let kbps: u32 = std::env::var("FRAME_KBPS").ok().and_then(|v| v.parse().ok()).unwrap_or(64); + let from: usize = std::env::var("FRAME_FROM").ok().and_then(|v| v.parse().ok()).unwrap_or(0); + let to: usize = std::env::var("FRAME_TO").ok().and_then(|v| v.parse().ok()).unwrap_or(6); + let secs = (to as f64 * 0.02 + 1.0).max(2.0); + let pcm = ffmpeg_decode_pcm(&src, secs); + let scratch = std::env::temp_dir().join("ec-opus-frame-dec.opus"); + ffmpeg_encode_libopus(&src, kbps, &scratch, secs); + let ref_pkts: Vec> = + ogg_packets(&fs::read(&scratch).unwrap()).into_iter().skip(2).collect(); + + let show = |tag: &str, f: usize, bytes: usize, d: &ec_opus::celt::CeltDecDiag| { + let e: Vec = d.old_band_e[..NB_BANDS_DIAG].iter().map(|v| format!("{v:.1}")).collect(); + println!( + "f{f} {tag}: bytes {bytes} sil {} intra {} trans {} ac {} trim {} spread {} cb {} \ + tf {:?} pulses {:?} fine {:?} E {}", + d.silence, d.intra, d.transient, d.anti_collapse, d.alloc_trim, d.spread, + d.coded_bands, &d.tf_res[..NB_BANDS_DIAG], &d.pulses[..NB_BANDS_DIAG], + &d.fine_quant[..NB_BANDS_DIAG], e.join(",") + ); + }; + + let mut d = Decoder::new(48000, CH).unwrap(); + let mut buf = vec![0.0f32; 5760 * CH]; + let mut ref_rows = Vec::new(); + for p in ref_pkts.iter().take(to) { + let ok = !p.is_empty() + && ec_opus::Toc::new(p[0]).mode() != Mode::Silk + && d.decode_float(p, &mut buf).is_ok(); + ref_rows.push(if ok { Some((p.len(), d.last_celt_diag().clone())) } else { None }); + } + + let mut enc = Encoder::new(48000, CH, Application::Audio).unwrap(); + enc.set_bitrate(kbps * 1000); + enc.set_vbr_constrained(true); + let mut dec = Decoder::new(48000, CH).unwrap(); + let mut out = vec![0u8; 1500]; + let mut ours_rows = Vec::new(); + for block in pcm.chunks_exact(960 * CH).take(to) { + let n = enc.encode_float(block, 960, &mut out).unwrap(); + let ok = ec_opus::Toc::new(out[0]).mode() != Mode::Silk + && dec.decode_float(&out[..n], &mut buf).is_ok(); + ours_rows.push(if ok { Some((n, dec.last_celt_diag().clone())) } else { None }); + } + + if to - from <= 40 { + for f in from..to { + match ours_rows.get(f).and_then(|x| x.as_ref()) { + Some((n, d)) => show("ours", f, *n, d), + None => println!("f{f} ours: (silk/absent)"), + } + match ref_rows.get(f).and_then(|x| x.as_ref()) { + Some((n, d)) => show("ref ", f, *n, d), + None => println!("f{f} ref : (silk/absent)"), + } + } + } + + // Aggregate over the frames BOTH sides coded as CELT, split by whether + // either side called the frame transient: which coded decision differs. + let mut agg = [(0usize, [0.0f64; 8]); 2]; + let mut trans_diff = 0usize; + for f in from..to { + let (Some((no, o)), Some((nr, r))) = + (ours_rows.get(f).and_then(|x| x.as_ref()), ref_rows.get(f).and_then(|x| x.as_ref())) + else { + continue; + }; + if o.transient != r.transient { + trans_diff += 1; + } + let slot = usize::from(o.transient || r.transient); + let tf = |d: &ec_opus::celt::CeltDecDiag| { + d.tf_res[..NB_BANDS_DIAG].iter().sum::() as f64 / NB_BANDS_DIAG as f64 + }; + let a = &mut agg[slot]; + a.0 += 1; + for (k, v) in [ + *no as f64, *nr as f64, + o.alloc_trim as f64, r.alloc_trim as f64, + tf(o), tf(r), + f64::from(u8::from(o.anti_collapse)), f64::from(u8::from(r.anti_collapse)), + ] + .into_iter() + .enumerate() + { + a.1[k] += v; + } + } + println!("frames {from}..{to}, transient-flag disagreements {trans_diff}"); + for (slot, name) in [(1usize, "transient"), (0, "steady ")] { + let (n, v) = agg[slot]; + if n == 0 { + continue; + } + let m = |k: usize| v[k] / n as f64; + println!( + "{name} n={n}: bytes o {:.1} r {:.1} | trim o {:.2} r {:.2} | mean tf_res o {:.3} \ + r {:.3} | anti_collapse o {:.2} r {:.2}", + m(0), m(1), m(2), m(3), m(4), m(5), m(6), m(7) + ); + } +} + +const NB_BANDS_DIAG: usize = ec_opus::celt::NB_BANDS; diff --git a/lanes/opus-opustr-r1.sweep.txt b/lanes/opus-opustr-r1.sweep.txt new file mode 100644 index 00000000..4339687b --- /dev/null +++ b/lanes/opus-opustr-r1.sweep.txt @@ -0,0 +1,14 @@ +# lane-opustr r1 REVERTED: libopus-1.3 alloc_trim_analysis port (continuous tilt + 0.75*logXC stereo + 2*tf_estimate transient term) +# vs lanes/opus-opus64-r3.sweep.txt. 12 rows (reelm.wav gone, its 2 rows SKIP). +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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