diff --git a/crates/ec-opus/src/celt_enc.rs b/crates/ec-opus/src/celt_enc.rs index c65b2249..ebaad243 100644 --- a/crates/ec-opus/src/celt_enc.rs +++ b/crates/ec-opus/src/celt_enc.rs @@ -891,10 +891,16 @@ impl CeltEncoder { // dynalloc boost minus calibration average (C:1651) target += total_boost - (19 << lm); // TF boost (C:1653-1654): MULT16_32_Q15 is identity in float mode, - // SHL32(.., 1) is the x2: 2 * (tf_estimate - 0.044) * target. - // Measured deviation: C applies SHL32(.., 1) (x2). On the 14-row - // library gate x2 loses .003 corr on reelm/hein@64 vs x1 (lane - // opus-vbr r2 vs r3); our tf_estimate is not scaled like libopus's. + // SHL32(.., 1) is the x2 and tf_calibration is .04 at a fixed frame + // size. MEASURED TWICE (lane opus-vbr r2/r3 on the old 14-row gate, + // lane opustr r2 on the 12-row one): the x2 costs .0030 corr on + // hein@64 and .0014 on hein@96 and pushes 5 err_ratio rows the + // wrong way, so the x1 stand-in stays. The reelm rows that carried + // the first rejection are gone; hein reproduced it exactly. + // It is nonetheless the only lever measured to move the reell rows: + // with it, reell@64 goes 2.697 -> 1.764 and reell@96 4.499 -> 2.114 + // (`lanes/opus-opustr-r2.sweep.txt`). Upgrade path: gate the x2 on + // content -- it pays on music transients and starves hein's speech. target += ((tf_estimate - 0.044) * target as f32) as i32; // floor depth cap (C:1683-1695): SHR32/MULT16_32_Q15 are identity. { @@ -941,6 +947,11 @@ impl CeltEncoder { // Release banked credit slowly (8 bytes per frame) so it stays // available for transient boosts; libopus CVBR re-adds all of // it to the next frame, which would defeat banking entirely. + // MEASURED (lane opustr r3, `lanes/opus-opustr-r3.sweep.txt`): + // restoring the libopus uncapped one-frame release on its own + // is neutral-to-worse -- 3 rows better, 5 worse (dl8a@96 + // .374->.415, hein@96 .528->.555, her@96 1.051->1.064) and + // hein@64 corr -.0008. The drip stays. let adjust = ((-self.vbr_reservoir) / (8 << BITRES)).min(8); if !silence { nb_avail += adjust; @@ -1457,6 +1468,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 +1847,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..24b83794 100644 --- a/crates/ec-opus/tests/conformance.rs +++ b/crates/ec-opus/tests/conformance.rs @@ -5540,3 +5540,128 @@ 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 }); + } + + // Encode at the reference's REALISED rate, like the library gate does: + // ffmpeg's libopus overshoots its -b:a target by up to 12% on these + // sources, and a flat target makes every byte column meaningless. + let ref_bytes: usize = ref_pkts.iter().take(to).map(Vec::len).sum(); + let ref_kbps = ref_bytes as f64 * 8.0 / (ref_pkts.len().min(to) as f64 * 0.02) / 1000.0; + println!("ffmpeg target {kbps} kbps, realised {ref_kbps:.1} kbps"); + let mut enc = Encoder::new(48000, CH, Application::Audio).unwrap(); + enc.set_bitrate((ref_kbps * 1000.0).round() as u32); + 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 diff --git a/lanes/opus-opustr-r2.sweep.txt b/lanes/opus-opustr-r2.sweep.txt new file mode 100644 index 00000000..ceace060 --- /dev/null +++ b/lanes/opus-opustr-r2.sweep.txt @@ -0,0 +1,14 @@ +# lane-opustr r2 REVERTED: libopus CVBR restored whole -- tf boost 2*(tf_estimate-.04) AND +# the uncapped one-frame reservoir release. vs lanes/opus-opus64-r3.sweep.txt. 12 rows. +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 diff --git a/lanes/opus-opustr-r3.sweep.txt b/lanes/opus-opustr-r3.sweep.txt new file mode 100644 index 00000000..837d64c7 --- /dev/null +++ b/lanes/opus-opustr-r3.sweep.txt @@ -0,0 +1,14 @@ +# lane-opustr r3 REVERTED: only the uncapped one-frame CVBR reservoir release (tf boost left at x1). +# vs lanes/opus-opus64-r3.sweep.txt. 12 rows (reelm.wav gone, its 2 rows SKIP). +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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 +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