forge
Explore
Log in

Don't you dare go Hollow

Port libopus dynalloc_analysis

Browse files at this commit

698943dd9fbe0bcf4721a76ce288f7b4a7ea162eAhmet Kaan GümüşParent: 06a72f9
  • feat
    • port libopus dynalloc_analysis
    • decoder diag exposes allocation
Show full message
feat(opus-sb/r1): port libopus dynalloc_analysis; decoder diag exposes allocation

The old second-difference spike heuristic boosted the top coded band
(14.4-18.7 kHz) by ~220-260 1/8-bit per frame on reelm/hein@64k where
libopus gives 3, and starved 240-3000 Hz on short-block frames. The port
(follower/median masking over long-window energies, stereo cross-talk,
width-scaled boost units, CVBR halving, 2/3 cap) brings the per-band boost
table to within a few 1/8-bit of libopus.

Gate (reelm,hein,her,reell): reelm@64 gap +.0046 -> +.0037, hein@64 +.0031 ->
+.0022, no row loses more than .0003 corr, drops 0, rate within 5%.

CeltDecDiag gains alloc_trim/pulses/fine_quant/intensity/dual_stereo/
coded_bands/balance/offsets; new ignored test short_block_bits_vs_libopus
prints the per-band allocation ours vs libopus (lanes/opus-sb-r1.bits.txt).
crates/ec-opus/src/celt.rsmodified+33 -0
@@ -1110,6 +1110,7 @@ pub struct CeltDecDiag {
1110 pub silence: bool,1110 pub silence: bool,
1111 /// Coarse energies used the intra-frame reset.1111 /// Coarse energies used the intra-frame reset.
1112 pub intra: bool,1112 pub intra: bool,
1113+ /// Short-block (transient) frame as coded.
1113 pub transient: bool,1114 pub transient: bool,
1114 /// Packet bits the range decoder saw for this frame.1115 /// Packet bits the range decoder saw for this frame.
1115 pub total_bits: i32,1116 pub total_bits: i32,
@@ -1121,6 +1122,22 @@ pub struct CeltDecDiag {
1121 pub spread: usize,1122 pub spread: usize,
1122 /// Anti-collapse flag as coded.1123 /// Anti-collapse flag as coded.
1123 pub anti_collapse: bool,1124 pub anti_collapse: bool,
1125+ /// Allocation trim index as coded (0..10).
1126+ pub alloc_trim: i32,
1127+ /// Per-band allocated pulses after `compute_allocation`.
1128+ pub pulses: Vec<i32>,
1129+ /// Per-band fine-energy precision bits.
1130+ pub fine_quant: Vec<i32>,
1131+ /// Stereo intensity band (bands >= this collapse to mono).
1132+ pub intensity: usize,
1133+ /// Dual-stereo flag as coded.
1134+ pub dual_stereo: bool,
1135+ /// Highest band actually allocated pulses (`codedBands`).
1136+ pub coded_bands: usize,
1137+ /// Bit balance left after allocation, in 1/8-bit units.
1138+ pub balance: i32,
1139+ /// Decoded dynalloc band boosts (1/8-bit units).
1140+ pub offsets: Vec<i32>,
1124 }1141 }
1125 1142
1126 /// A CELT layer decoder, one per Opus stream.1143 /// A CELT layer decoder, one per Opus stream.
@@ -1218,6 +1235,14 @@ impl CeltDecoder {
1218 tf_res: Vec::new(),1235 tf_res: Vec::new(),
1219 spread: 0,1236 spread: 0,
1220 anti_collapse: false,1237 anti_collapse: false,
1238+ alloc_trim: 0,
1239+ pulses: vec![0; NB_BANDS],
1240+ fine_quant: vec![0; NB_BANDS],
1241+ intensity: 0,
1242+ dual_stereo: false,
1243+ coded_bands: 0,
1244+ balance: 0,
1245+ offsets: vec![0; NB_BANDS],
1221 },1246 },
1222 }1247 }
1223 }1248 }
@@ -1566,6 +1591,14 @@ impl CeltDecoder {
1566 tf_res: self.tf_res.to_vec(),1591 tf_res: self.tf_res.to_vec(),
1567 spread,1592 spread,
1568 anti_collapse: anti_collapse_on,1593 anti_collapse: anti_collapse_on,
1594+ alloc_trim,
1595+ pulses: self.pulses.to_vec(),
1596+ fine_quant: self.fine_quant.to_vec(),
1597+ intensity,
1598+ dual_stereo,
1599+ coded_bands,
1600+ balance,
1601+ offsets: self.offsets.to_vec(),
1569 };1602 };
1570 self.rng = dec.range();1603 self.rng = dec.range();
1571 1604
crates/ec-opus/src/celt_enc.rsmodified+135 -21
@@ -385,6 +385,8 @@ pub struct CeltEncoder {
385 x: Vec<f32>,385 x: Vec<f32>,
386 band_e: Vec<f32>,386 band_e: Vec<f32>,
387 band_log_e: Vec<f32>,387 band_log_e: Vec<f32>,
388+ /// Long-window log energies used by dynalloc (`bandLogE2`).
389+ band_log_e2: Vec<f32>,
388 error: Vec<f32>,390 error: Vec<f32>,
389 transient_tmp: Vec<f32>,391 transient_tmp: Vec<f32>,
390 hadamard_tmp: Vec<f32>,392 hadamard_tmp: Vec<f32>,
@@ -429,6 +431,7 @@ impl CeltEncoder {
429 x: vec![0.0; channels * max_n],431 x: vec![0.0; channels * max_n],
430 band_e: vec![0.0; 2 * NB_BANDS],432 band_e: vec![0.0; 2 * NB_BANDS],
431 band_log_e: vec![0.0; 2 * NB_BANDS],433 band_log_e: vec![0.0; 2 * NB_BANDS],
434+ band_log_e2: vec![0.0; 2 * NB_BANDS],
432 error: vec![0.0; 2 * NB_BANDS],435 error: vec![0.0; 2 * NB_BANDS],
433 transient_tmp: vec![0.0; max_n + OVERLAP],436 transient_tmp: vec![0.0; max_n + OVERLAP],
434 hadamard_tmp: vec![0.0; max_n],437 hadamard_tmp: vec![0.0; max_n],
@@ -617,6 +620,22 @@ impl CeltEncoder {
617 }620 }
618 621
619 // --- Forward MDCTs, energies, normalisation -------------------------622 // --- Forward MDCTs, energies, normalisation -------------------------
623+ // dynalloc looks at long-window energies (`bandLogE2`): on transient
624+ // frames that is a second MDCT pass, offset by LM/2 for the block size.
625+ if short_blocks != 0 {
626+ self.compute_mdcts(0, lm, n, c);
627+ for ch in 0..c {
628+ for i in start..end {
629+ let mut sum = 1e-27f32;
630+ for j in m * E_BANDS[i]..m * E_BANDS[i + 1] {
631+ let v = self.freq[ch * n + j];
632+ sum += v * v;
633+ }
634+ self.band_log_e2[i + ch * NB_BANDS] =
635+ (sum.sqrt() as f64).log2() as f32 - E_MEANS[i] + 0.5 * lm as f32;
636+ }
637+ }
638+ }
620 self.compute_mdcts(short_blocks, lm, n, c);639 self.compute_mdcts(short_blocks, lm, n, c);
621 for ch in 0..c {640 for ch in 0..c {
622 self.x[ch * n..ch * n + m * E_BANDS[start]].fill(0.0);641 self.x[ch * n..ch * n + m * E_BANDS[start]].fill(0.0);
@@ -629,6 +648,9 @@ impl CeltEncoder {
629 self.band_e[i + ch * NB_BANDS] = sum.sqrt();648 self.band_e[i + ch * NB_BANDS] = sum.sqrt();
630 self.band_log_e[i + ch * NB_BANDS] =649 self.band_log_e[i + ch * NB_BANDS] =
631 (self.band_e[i + ch * NB_BANDS] as f64).log2() as f32 - E_MEANS[i];650 (self.band_e[i + ch * NB_BANDS] as f64).log2() as f32 - E_MEANS[i];
651+ if short_blocks == 0 {
652+ self.band_log_e2[i + ch * NB_BANDS] = self.band_log_e[i + ch * NB_BANDS];
653+ }
632 }654 }
633 for i in start..end {655 for i in start..end {
634 let g = 1.0 / (1e-27 + self.band_e[i + ch * NB_BANDS]);656 let g = 1.0 / (1e-27 + self.band_e[i + ch * NB_BANDS]);
@@ -700,27 +722,7 @@ impl CeltEncoder {
700 }722 }
701 self.offsets = [0; NB_BANDS];723 self.offsets = [0; NB_BANDS];
702 if effective_bytes > 50 && lm >= 1 {724 if effective_bytes > 50 && lm >= 1 {
703- let (t1, t2) = if lm <= 1 {725+ self.dynalloc_analysis(start, end, c, lm, is_transient, effective_bytes);
704- (3.0f32, 5.0f32)
705- } else {
706- (2.0, 4.0)
707- };
708- for i in start + 1..end - 1 {
709- let mut d2 =
710- 2.0 * self.band_log_e[i] - self.band_log_e[i - 1] - self.band_log_e[i + 1];
711- if c == 2 {
712- d2 = 0.5
713- * (d2 + 2.0 * self.band_log_e[i + NB_BANDS]
714- - self.band_log_e[i - 1 + NB_BANDS]
715- - self.band_log_e[i + 1 + NB_BANDS]);
716- }
717- if d2 > t1 {
718- self.offsets[i] += 1;
719- }
720- if d2 > t2 {
721- self.offsets[i] += 1;
722- }
723- }
724 }726 }
725 let mut dynalloc_logp = 6u32;727 let mut dynalloc_logp = 6u32;
726 let total_bits_frac = total_bits << BITRES;728 let total_bits_frac = total_bits << BITRES;
@@ -1132,6 +1134,118 @@ impl CeltEncoder {
1132 (is_transient, tf_estimate, tf_chan)1134 (is_transient, tf_estimate, tf_chan)
1133 }1135 }
1134 1136
1137+ /// libopus `dynalloc_analysis`: per-band boost requests from a follower /
1138+ /// median masking model over the long-window energies. Writes
1139+ /// `self.offsets[i]` in boost units (the loop that codes them converts to
1140+ /// bits via the band's quanta).
1141+ fn dynalloc_analysis(
1142+ &mut self,
1143+ start: usize,
1144+ end: usize,
1145+ c: usize,
1146+ lm: usize,
1147+ is_transient: bool,
1148+ effective_bytes: i32,
1149+ ) {
1150+ const LSB_DEPTH: f32 = 24.0;
1151+ let mut noise_floor = [0.0f32; NB_BANDS];
1152+ for i in 0..end {
1153+ noise_floor[i] = 0.0625 * LOG_N[i] as f32 + 0.5 + (9.0 - LSB_DEPTH) - E_MEANS[i]
1154+ + 0.0062 * ((i + 5) * (i + 5)) as f32;
1155+ }
1156+ let median3 = |a: &[f32]| -> f32 {
1157+ let (x, y, z) = (a[0], a[1], a[2]);
1158+ x.max(y).min(z).max(x.min(y))
1159+ };
1160+ let median5 = |a: &[f32]| -> f32 {
1161+ let mut v = [a[0], a[1], a[2], a[3], a[4]];
1162+ v.sort_by(|p, q| p.partial_cmp(q).unwrap());
1163+ v[2]
1164+ };
1165+ let mut follower = [0.0f32; 2 * NB_BANDS];
1166+ let mut last = 0usize;
1167+ for ch in 0..c {
1168+ let e2 = &self.band_log_e2[ch * NB_BANDS..ch * NB_BANDS + NB_BANDS];
1169+ let f = &mut follower[ch * NB_BANDS..ch * NB_BANDS + NB_BANDS];
1170+ f[0] = e2[0];
1171+ for i in 1..end {
1172+ // The last band at least .5 dB above its predecessor bounds
1173+ // the backward pass (band-limited signals).
1174+ if e2[i] > e2[i - 1] + 0.5 {
1175+ last = i;
1176+ }
1177+ f[i] = (f[i - 1] + 1.5).min(e2[i]);
1178+ }
1179+ for i in (0..last).rev() {
1180+ f[i] = f[i].min((f[i + 1] + 2.0).min(e2[i]));
1181+ }
1182+ let offset = 1.0f32;
1183+ for i in 2..end.saturating_sub(2) {
1184+ f[i] = f[i].max(median5(&e2[i - 2..i + 3]) - offset);
1185+ }
1186+ let tmp = median3(&e2[0..3]) - offset;
1187+ f[0] = f[0].max(tmp);
1188+ f[1] = f[1].max(tmp);
1189+ let tmp = median3(&e2[end - 3..end]) - offset;
1190+ f[end - 2] = f[end - 2].max(tmp);
1191+ f[end - 1] = f[end - 1].max(tmp);
1192+ for i in 0..end {
1193+ f[i] = f[i].max(noise_floor[i]);
1194+ }
1195+ }
1196+ if c == 2 {
1197+ for i in start..end {
1198+ // 24 dB cross-talk between channels.
1199+ follower[NB_BANDS + i] = follower[NB_BANDS + i].max(follower[i] - 4.0);
1200+ follower[i] = follower[i].max(follower[NB_BANDS + i] - 4.0);
1201+ follower[i] = 0.5
1202+ * ((self.band_log_e[i] - follower[i]).max(0.0)
1203+ + (self.band_log_e[NB_BANDS + i] - follower[NB_BANDS + i]).max(0.0));
1204+ }
1205+ } else {
1206+ for i in start..end {
1207+ follower[i] = (self.band_log_e[i] - follower[i]).max(0.0);
1208+ }
1209+ }
1210+ // Our VBR is always constrained: halve dynalloc on steady frames.
1211+ if !is_transient {
1212+ for f in &mut follower[start..end] {
1213+ *f *= 0.5;
1214+ }
1215+ }
1216+ for i in start..end {
1217+ if i < 8 {
1218+ follower[i] *= 2.0;
1219+ }
1220+ if i >= 12 {
1221+ follower[i] *= 0.5;
1222+ }
1223+ }
1224+ let mut tot_boost = 0i32;
1225+ for i in start..end {
1226+ let f = follower[i].min(4.0);
1227+ let width = ((c * (E_BANDS[i + 1] - E_BANDS[i])) << lm) as i32;
1228+ let (boost, boost_bits) = if width < 6 {
1229+ let b = f as i32;
1230+ (b, (b * width) << BITRES)
1231+ } else if width > 48 {
1232+ let b = (f * 8.0) as i32;
1233+ (b, ((b * width) << BITRES) / 8)
1234+ } else {
1235+ let b = (f * width as f32 / 6.0) as i32;
1236+ (b, (b * 6) << BITRES)
1237+ };
1238+ // Constrained VBR, steady frame: dynalloc may take 2/3 of the bits.
1239+ if !is_transient && ((tot_boost + boost_bits) >> BITRES >> 3) > 2 * effective_bytes / 3 {
1240+ let cap = ((2 * effective_bytes / 3) << BITRES) << 3;
1241+ self.offsets[i] = cap - tot_boost;
1242+ break;
1243+ }
1244+ self.offsets[i] = boost;
1245+ tot_boost += boost_bits;
1246+ }
1247+ }
1248+
1135 fn compute_mdcts(&mut self, short_blocks: usize, lm: usize, n: usize, c: usize) {1249 fn compute_mdcts(&mut self, short_blocks: usize, lm: usize, n: usize, c: usize) {
1136 let stride = n + OVERLAP;1250 let stride = n + OVERLAP;
1137 let (n2, blocks, plan_lm) = if short_blocks != 0 {1251 let (n2, blocks, plan_lm) = if short_blocks != 0 {
crates/ec-opus/tests/conformance.rsmodified+255 -0
@@ -3920,3 +3920,258 @@ fn celt_click_peak_offset() {
3920 println!("celt click {bps}: peak +{} (lm {})", p as i64 - at as i64, d.lm);3920 println!("celt click {bps}: peak +{} (lm {})", p as i64 - at as i64, d.lm);
3921 }3921 }
3922 }3922 }
3923+
3924+/// Per-frame allocation diagnostics for SHORT-block (transient) frames at a
3925+/// fixed bitrate: decodes both our stream and libopus's with our decoder, then
3926+/// prints per-band mean pulses, fine_quant, alloc_trim, coded_bands, intensity,
3927+/// dual_stereo share, spread/tf histograms, total_bits — ours vs ref, split by
3928+/// the decoder's transient flag. Output: `lanes/opus-sb-r1.bits.txt`.
3929+#[test]
3930+#[ignore]
3931+fn short_block_bits_vs_libopus() {
3932+ const SECS: f64 = 120.0;
3933+ const FRAME: usize = 960;
3934+ const CHANNELS: usize = 2;
3935+ let kbps_env: u32 = std::env::var("SWEEP_KBPS")
3936+ .ok()
3937+ .and_then(|v| v.parse().ok())
3938+ .unwrap_or(64);
3939+ #[allow(non_snake_case)]
3940+ let KBPS: u32 = kbps_env;
3941+
3942+ let all: &[(&str, &str)] = &[
3943+ ("reell", "~/Music/reell.mp4"),
3944+ ("dl8a", "~/Downloads/8a3b6d1d19.mp3"),
3945+ ("her", "~/Music/reeli.mp3"),
3946+ ("reelm", "~/Music/reelm.wav"),
3947+ (
3948+ "hein",
3949+ "~/Downloads/reelm.mp3",
3950+ ),
3951+ ];
3952+ let only: Vec<String> = std::env::var("SWEEP_ONLY")
3953+ .unwrap_or_else(|_| "reelm,hein".to_owned())
3954+ .split(',')
3955+ .filter(|s| !s.is_empty())
3956+ .map(|s| s.trim().to_owned())
3957+ .collect();
3958+ let sources: Vec<(String, PathBuf)> = all
3959+ .iter()
3960+ .filter(|(tag, _)| only.iter().any(|o| o == tag))
3961+ .map(|(tag, p)| (tag.to_string(), shellexpand(p)))
3962+ .collect();
3963+ assert!(!sources.is_empty(), "SWEEP_ONLY matched no sources");
3964+
3965+ let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes");
3966+ fs::create_dir_all(&lanes_dir).unwrap();
3967+ let out_path = lanes_dir.join("opus-sb-r1.bits.txt");
3968+ let scratch = lanes_dir.join("opus-sb-r1.scratch.ogg");
3969+ let nb = ec_opus::celt::NB_BANDS;
3970+ let mut report = String::new();
3971+
3972+ for (tag, src) in &sources {
3973+ if !src.exists() {
3974+ eprintln!("SKIP {tag}: missing {}", src.display());
3975+ continue;
3976+ }
3977+ let source_pcm = ffmpeg_decode_pcm(src, SECS);
3978+ let seconds = source_pcm.len() as f64 / CHANNELS as f64 / 48000.0;
3979+
3980+ // Reference: ffmpeg libopus at KBPS, VBR.
3981+ ffmpeg_encode_libopus(src, KBPS, &scratch, SECS);
3982+ let ref_bytes = fs::metadata(&scratch).map(|m| m.len() as usize).unwrap_or(0);
3983+ let ref_kbps = ref_bytes as f64 * 8.0 / seconds / 1000.0;
3984+ let ref_pkts: Vec<Vec<u8>> = ogg_packets(&fs::read(&scratch).unwrap())
3985+ .into_iter()
3986+ .skip(2)
3987+ .collect();
3988+
3989+ // Decode ref packets with our decoder, capture per-frame CELT diag.
3990+ let mut ref_diags: Vec<ec_opus::celt::CeltDecDiag> = Vec::new();
3991+ {
3992+ let mut d = Decoder::new(48000, CHANNELS).unwrap();
3993+ let mut buf = vec![0.0f32; 5760 * CHANNELS];
3994+ for p in &ref_pkts {
3995+ if p.is_empty() {
3996+ continue;
3997+ }
3998+ let toc = ec_opus::Toc::new(p[0]);
3999+ if d.decode_float(p, &mut buf).is_ok() && toc.mode() != Mode::Silk {
4000+ ref_diags.push(d.last_celt_diag().clone());
4001+ }
4002+ }
4003+ }
4004+
4005+ // Ours at the reference's realised rate.
4006+ let mut enc = Encoder::new(48000, CHANNELS, Application::Audio).expect("encoder");
4007+ enc.set_bitrate((ref_kbps * 1000.0).round() as u32);
4008+ enc.set_vbr_constrained(true);
4009+ let mut dec = Decoder::new(48000, CHANNELS).unwrap();
4010+ let mut packet = vec![0u8; 1500];
4011+ let mut dbuf = vec![0.0f32; 5760 * CHANNELS];
4012+ let mut ours_diags: Vec<ec_opus::celt::CeltDecDiag> = Vec::new();
4013+ let mut ours_bytes = 0usize;
4014+ for block in source_pcm.chunks(FRAME * CHANNELS) {
4015+ let mut padded = block.to_vec();
4016+ padded.resize(FRAME * CHANNELS, 0.0);
4017+ let len = enc.encode_float(&padded, FRAME, &mut packet).expect("encode");
4018+ ours_bytes += len;
4019+ let toc = ec_opus::Toc::new(packet[0]);
4020+ if dec.decode_float(&packet[..len], &mut dbuf).is_ok() && toc.mode() != Mode::Silk {
4021+ ours_diags.push(dec.last_celt_diag().clone());
4022+ }
4023+ }
4024+ let ours_kbps = ours_bytes as f64 * 8.0 / seconds / 1000.0;
4025+
4026+ report.push_str(&format!(
4027+ "\n# source {tag} @{KBPS}k: ours {ours_kbps:.1} kbps, ref {ref_kbps:.1} kbps; \
4028+ ours_celt {} frames, ref_celt {} frames\n",
4029+ ours_diags.len(),
4030+ ref_diags.len()
4031+ ));
4032+
4033+ for (label, is_trans) in [("LONG", false), ("SHORT", true)] {
4034+ let ours_t: Vec<&ec_opus::celt::CeltDecDiag> =
4035+ ours_diags.iter().filter(|d| d.transient == is_trans).collect();
4036+ let ref_t: Vec<&ec_opus::celt::CeltDecDiag> =
4037+ ref_diags.iter().filter(|d| d.transient == is_trans).collect();
4038+ let on = ours_t.len();
4039+ let rn = ref_t.len();
4040+ if on == 0 && rn == 0 {
4041+ continue;
4042+ }
4043+ report.push_str(&format!("## {label}: ours {on} frames, ref {rn} frames\n"));
4044+
4045+ // Per-band mean pulses.
4046+ report.push_str("# mean pulses per band (ours / ref)\n");
4047+ report.push_str("# band_hz\tours\tref\tdiff\n");
4048+ for i in 0..nb {
4049+ let om: f64 =
4050+ ours_t.iter().map(|d| d.pulses[i] as f64).sum::<f64>() / on.max(1) as f64;
4051+ let rm: f64 =
4052+ ref_t.iter().map(|d| d.pulses[i] as f64).sum::<f64>() / rn.max(1) as f64;
4053+ report.push_str(&format!(
4054+ "{}\t{:.1}\t{:.1}\t{:.1}\n",
4055+ CELT_EBANDS[i] * 240,
4056+ om,
4057+ rm,
4058+ om - rm
4059+ ));
4060+ }
4061+
4062+ // Per-band mean fine_quant.
4063+ report.push_str("# mean fine_quant per band (ours / ref)\n");
4064+ report.push_str("# band_hz\tours\tref\n");
4065+ for i in 0..nb {
4066+ let om: f64 =
4067+ ours_t.iter().map(|d| d.fine_quant[i] as f64).sum::<f64>() / on.max(1) as f64;
4068+ let rm: f64 =
4069+ ref_t.iter().map(|d| d.fine_quant[i] as f64).sum::<f64>() / rn.max(1) as f64;
4070+ report.push_str(&format!(
4071+ "{}\t{:.2}\t{:.2}\n",
4072+ CELT_EBANDS[i] * 240,
4073+ om,
4074+ rm
4075+ ));
4076+ }
4077+
4078+ // Aggregate stats.
4079+ let mean = |v: &[&ec_opus::celt::CeltDecDiag], f: &dyn Fn(&ec_opus::celt::CeltDecDiag) -> f64| -> f64 {
4080+ v.iter().map(|d| f(d)).sum::<f64>() / v.len().max(1) as f64
4081+ };
4082+ let mean_band_hz = |v: &[&ec_opus::celt::CeltDecDiag], f: &dyn Fn(&ec_opus::celt::CeltDecDiag) -> usize| -> f64 {
4083+ let m = v.iter().map(|d| f(d)).sum::<usize>() / v.len().max(1);
4084+ CELT_EBANDS[m] as f64 * 240.0
4085+ };
4086+ let dual_share = |v: &[&ec_opus::celt::CeltDecDiag]| -> f64 {
4087+ v.iter().filter(|d| d.dual_stereo).count() as f64 / v.len().max(1) as f64
4088+ };
4089+ let fmt_hist = |h: [usize; 4]| -> String {
4090+ h.iter()
4091+ .map(|x| x.to_string())
4092+ .collect::<Vec<_>>()
4093+ .join(", ")
4094+ };
4095+ let spread_hist = |v: &[&ec_opus::celt::CeltDecDiag]| -> [usize; 4] {
4096+ let mut h = [0usize; 4];
4097+ for d in v {
4098+ h[d.spread.min(3)] += 1;
4099+ }
4100+ h
4101+ };
4102+ let tf_hist = |v: &[&ec_opus::celt::CeltDecDiag]| -> [usize; 4] {
4103+ let mut h = [0usize; 4];
4104+ for d in v {
4105+ for &t in &d.tf_res {
4106+ h[(t + 1).max(0).min(3) as usize] += 1;
4107+ }
4108+ }
4109+ h
4110+ };
4111+
4112+ let trim_hist = |v: &[&ec_opus::celt::CeltDecDiag]| -> [usize; 11] {
4113+ let mut h = [0usize; 11];
4114+ for d in v {
4115+ h[d.alloc_trim.clamp(0, 10) as usize] += 1;
4116+ }
4117+ h
4118+ };
4119+ report.push_str(&format!(
4120+ "# alloc_trim hist [0..10]: ours {:?} ref {:?}\n",
4121+ trim_hist(&ours_t),
4122+ trim_hist(&ref_t)
4123+ ));
4124+ report.push_str("# mean dynalloc boost per band (1/8 bit) ours / ref\n");
4125+ for b in 0..nb {
4126+ let mo = mean(&ours_t, &|d: &ec_opus::celt::CeltDecDiag| d.offsets[b] as f64);
4127+ let mr = mean(&ref_t, &|d: &ec_opus::celt::CeltDecDiag| d.offsets[b] as f64);
4128+ if mo != 0.0 || mr != 0.0 {
4129+ report.push_str(&format!("{}\t{:.1}\t{:.1}\n", CELT_EBANDS[b] * 240, mo, mr));
4130+ }
4131+ }
4132+ report.push_str(&format!(
4133+ "# alloc_trim mean: ours {:.2} ref {:.2}\n",
4134+ mean(&ours_t, &|d: &ec_opus::celt::CeltDecDiag| d.alloc_trim as f64),
4135+ mean(&ref_t, &|d: &ec_opus::celt::CeltDecDiag| d.alloc_trim as f64),
4136+ ));
4137+ report.push_str(&format!(
4138+ "# coded_bands mean Hz: ours {:.0} ref {:.0}\n",
4139+ mean_band_hz(&ours_t, &|d: &ec_opus::celt::CeltDecDiag| d.coded_bands),
4140+ mean_band_hz(&ref_t, &|d: &ec_opus::celt::CeltDecDiag| d.coded_bands),
4141+ ));
4142+ report.push_str(&format!(
4143+ "# intensity mean Hz: ours {:.0} ref {:.0}\n",
4144+ mean_band_hz(&ours_t, &|d: &ec_opus::celt::CeltDecDiag| d.intensity),
4145+ mean_band_hz(&ref_t, &|d: &ec_opus::celt::CeltDecDiag| d.intensity),
4146+ ));
4147+ report.push_str(&format!(
4148+ "# dual_stereo share: ours {:.3} ref {:.3}\n",
4149+ dual_share(&ours_t),
4150+ dual_share(&ref_t),
4151+ ));
4152+ report.push_str(&format!(
4153+ "# total_bits mean: ours {:.0} ref {:.0}\n",
4154+ mean(&ours_t, &|d: &ec_opus::celt::CeltDecDiag| d.total_bits as f64),
4155+ mean(&ref_t, &|d: &ec_opus::celt::CeltDecDiag| d.total_bits as f64),
4156+ ));
4157+ report.push_str(&format!(
4158+ "# balance mean: ours {:.0} ref {:.0}\n",
4159+ mean(&ours_t, &|d: &ec_opus::celt::CeltDecDiag| d.balance as f64),
4160+ mean(&ref_t, &|d: &ec_opus::celt::CeltDecDiag| d.balance as f64),
4161+ ));
4162+ report.push_str(&format!(
4163+ "# spread hist [0..3]: ours [{}] ref [{}]\n",
4164+ fmt_hist(spread_hist(&ours_t)),
4165+ fmt_hist(spread_hist(&ref_t)),
4166+ ));
4167+ report.push_str(&format!(
4168+ "# tf_res hist [-1..2]: ours [{}] ref [{}]\n",
4169+ fmt_hist(tf_hist(&ours_t)),
4170+ fmt_hist(tf_hist(&ref_t)),
4171+ ));
4172+ }
4173+ }
4174+
4175+ fs::write(&out_path, &report).unwrap();
4176+ eprintln!("wrote {}", out_path.display());
4177+}
lanes/opus-sb-r1.bits.txtadded+314 -0
@@ -0,0 +1,314 @@
1+
2+# source reelm @64k: ours 62.3 kbps, ref 63.3 kbps; ours_celt 6000 frames, ref_celt 6001 frames
3+## LONG: ours 4193 frames, ref 4213 frames
4+# mean pulses per band (ours / ref)
5+# band_hz ours ref diff
6+0 241.9 233.8 8.1
7+240 243.3 249.9 -6.6
8+480 232.5 234.1 -1.6
9+720 215.6 208.5 7.1
10+960 188.0 179.4 8.6
11+1200 179.1 171.9 7.2
12+1440 169.9 164.7 5.2
13+1680 162.0 158.0 3.9
14+1920 303.4 307.5 -4.1
15+2400 285.2 292.1 -6.8
16+2880 270.8 275.9 -5.1
17+3360 252.9 250.7 2.2
18+3840 463.4 456.6 6.8
19+4800 418.4 426.7 -8.2
20+5760 378.4 385.8 -7.4
21+6720 520.2 529.6 -9.5
22+8160 453.8 458.1 -4.3
23+9600 561.9 618.1 -56.3
24+11520 640.5 644.7 -4.2
25+14400 528.1 488.6 39.5
26+18720 0.0 1.0 -1.0
27+# mean fine_quant per band (ours / ref)
28+# band_hz ours ref
29+0 1.70 1.48
30+240 1.60 1.51
31+480 1.44 1.37
32+720 1.20 1.19
33+960 1.02 1.06
34+1200 1.02 1.05
35+1440 1.02 1.05
36+1680 1.03 1.06
37+1920 1.80 1.65
38+2400 1.71 1.52
39+2880 1.49 1.41
40+3360 1.15 1.28
41+3840 2.00 2.04
42+4800 2.00 2.05
43+5760 2.00 2.04
44+6720 2.04 2.21
45+8160 2.01 2.11
46+9600 2.35 2.40
47+11520 2.92 2.67
48+14400 2.60 2.28
49+18720 1.00 1.00
50+# alloc_trim hist [0..10]: ours [1742, 1860, 591, 0, 0, 0, 0, 0, 0, 0, 0] ref [3224, 832, 153, 4, 0, 0, 0, 0, 0, 0, 0]
51+# mean dynalloc boost per band (1/8 bit) ours / ref
52+0 0.4 3.3
53+240 13.9 32.6
54+480 14.0 27.1
55+720 2.8 7.7
56+960 1.5 3.6
57+1200 2.1 4.6
58+1440 2.3 5.9
59+1680 4.0 7.7
60+1920 9.6 23.6
61+2400 9.0 23.1
62+2880 10.0 22.5
63+3360 5.7 12.4
64+3840 2.1 6.5
65+4800 11.7 28.2
66+5760 10.1 22.7
67+6720 10.2 24.7
68+8160 1.6 6.5
69+9600 29.0 78.2
70+11520 9.2 26.1
71+14400 0.8 3.0
72+# alloc_trim mean: ours 0.73 ref 0.27
73+# coded_bands mean Hz: ours 14400 ref 14400
74+# intensity mean Hz: ours 8160 ref 6720
75+# dual_stereo share: ours 0.000 ref 0.000
76+# total_bits mean: ours 1028 ref 1037
77+# balance mean: ours 0 ref 0
78+# spread hist [0..3]: ours [0, 0, 4193, 0] ref [0, 0, 3303, 910]
79+# tf_res hist [-1..2]: ours [0, 88053, 0, 0] ref [19170, 69303, 0, 0]
80+## SHORT: ours 1807 frames, ref 1788 frames
81+# mean pulses per band (ours / ref)
82+# band_hz ours ref diff
83+0 308.6 292.4 16.3
84+240 325.4 306.7 18.7
85+480 323.1 305.6 17.4
86+720 350.2 335.5 14.7
87+960 290.0 276.9 13.1
88+1200 256.9 250.8 6.1
89+1440 256.4 247.9 8.5
90+1680 252.3 244.7 7.6
91+1920 459.8 446.2 13.6
92+2400 443.5 433.4 10.1
93+2880 409.1 402.9 6.2
94+3360 379.8 374.2 5.7
95+3840 714.2 710.2 4.1
96+4800 689.7 692.6 -2.9
97+5760 643.3 649.3 -6.0
98+6720 907.8 920.8 -13.0
99+8160 821.2 843.3 -22.1
100+9600 1096.9 1135.0 -38.1
101+11520 1423.1 1498.8 -75.6
102+14400 1349.5 1441.0 -91.5
103+18720 4.3 25.9 -21.7
104+# mean fine_quant per band (ours / ref)
105+# band_hz ours ref
106+0 2.11 2.07
107+240 2.19 2.16
108+480 2.19 2.14
109+720 2.33 2.26
110+960 1.98 1.84
111+1200 1.78 1.62
112+1440 1.74 1.58
113+1680 1.66 1.55
114+1920 2.18 2.16
115+2400 2.15 2.14
116+2880 2.05 2.06
117+3360 1.92 2.01
118+3840 2.68 2.60
119+4800 2.65 2.51
120+5760 2.55 2.39
121+6720 2.80 2.94
122+8160 2.75 2.87
123+9600 2.94 2.99
124+11520 3.17 3.21
125+14400 3.45 3.34
126+18720 1.01 1.06
127+# alloc_trim hist [0..10]: ours [762, 896, 148, 1, 0, 0, 0, 0, 0, 0, 0] ref [1723, 63, 2, 0, 0, 0, 0, 0, 0, 0, 0]
128+# mean dynalloc boost per band (1/8 bit) ours / ref
129+0 18.9 19.1
130+240 50.3 47.0
131+480 61.4 57.6
132+720 100.1 97.6
133+960 57.6 53.4
134+1200 30.7 31.9
135+1440 39.0 36.5
136+1680 43.0 40.9
137+1920 49.0 47.2
138+2400 50.8 50.2
139+2880 33.6 34.7
140+3360 21.1 21.4
141+3840 15.1 14.0
142+4800 43.8 43.5
143+5760 33.4 31.0
144+6720 33.2 32.1
145+8160 2.5 2.5
146+9600 68.8 65.5
147+11520 22.0 20.4
148+14400 4.6 3.4
149+18720 0.6 0.6
150+# alloc_trim mean: ours 0.66 ref 0.04
151+# coded_bands mean Hz: ours 14400 ref 18720
152+# intensity mean Hz: ours 8160 ref 6720
153+# dual_stereo share: ours 0.000 ref 0.000
154+# total_bits mean: ours 1725 ref 1735
155+# balance mean: ours 0 ref 0
156+# spread hist [0..3]: ours [0, 0, 1807, 0] ref [0, 0, 1788, 0]
157+# tf_res hist [-1..2]: ours [0, 37947, 0, 0] ref [7561, 14583, 11085, 4319]
158+
159+# source hein @64k: ours 63.8 kbps, ref 64.9 kbps; ours_celt 6000 frames, ref_celt 6001 frames
160+## LONG: ours 3972 frames, ref 3947 frames
161+# mean pulses per band (ours / ref)
162+# band_hz ours ref diff
163+0 240.1 233.1 7.0
164+240 241.3 247.2 -5.9
165+480 229.1 229.9 -0.7
166+720 213.9 206.8 7.0
167+960 185.5 177.3 8.1
168+1200 176.7 170.0 6.7
169+1440 171.9 172.3 -0.5
170+1680 164.2 167.5 -3.2
171+1920 304.1 315.1 -10.9
172+2400 283.0 289.1 -6.2
173+2880 265.9 268.5 -2.6
174+3360 255.5 259.9 -4.5
175+3840 462.1 460.3 1.8
176+4800 411.6 419.0 -7.4
177+5760 372.6 380.9 -8.3
178+6720 513.8 528.3 -14.5
179+8160 448.8 459.2 -10.4
180+9600 546.0 594.1 -48.1
181+11520 628.4 644.4 -16.1
182+14400 518.4 498.6 19.7
183+18720 0.0 1.8 -1.8
184+# mean fine_quant per band (ours / ref)
185+# band_hz ours ref
186+0 1.64 1.46
187+240 1.56 1.47
188+480 1.37 1.33
189+720 1.15 1.16
190+960 1.02 1.05
191+1200 1.02 1.04
192+1440 1.05 1.11
193+1680 1.06 1.13
194+1920 1.79 1.67
195+2400 1.67 1.50
196+2880 1.40 1.38
197+3360 1.16 1.31
198+3840 2.01 2.05
199+4800 2.00 2.04
200+5760 2.00 2.03
201+6720 2.04 2.19
202+8160 2.01 2.10
203+9600 2.32 2.39
204+11520 2.89 2.69
205+14400 2.57 2.32
206+18720 1.00 1.00
207+# alloc_trim hist [0..10]: ours [1776, 1790, 405, 0, 0, 1, 0, 0, 0, 0, 0] ref [3266, 588, 89, 2, 0, 2, 0, 0, 0, 0, 0]
208+# mean dynalloc boost per band (1/8 bit) ours / ref
209+0 1.0 5.2
210+240 14.1 31.7
211+480 12.3 24.7
212+720 3.0 7.7
213+960 1.6 3.6
214+1200 2.2 4.5
215+1440 7.3 16.4
216+1680 8.9 20.0
217+1920 14.2 34.5
218+2400 9.8 22.9
219+2880 7.3 17.1
220+3360 11.6 24.3
221+3840 6.8 14.0
222+4800 10.2 23.3
223+5760 9.3 19.9
224+6720 10.6 25.4
225+8160 3.1 8.4
226+9600 20.8 53.7
227+11520 7.2 19.9
228+14400 0.7 3.1
229+# alloc_trim mean: ours 0.66 ref 0.20
230+# coded_bands mean Hz: ours 14400 ref 14400
231+# intensity mean Hz: ours 6720 ref 5760
232+# dual_stereo share: ours 0.000 ref 0.000
233+# total_bits mean: ours 1026 ref 1043
234+# balance mean: ours 0 ref 0
235+# spread hist [0..3]: ours [0, 0, 3972, 0] ref [0, 0, 3809, 138]
236+# tf_res hist [-1..2]: ours [0, 83412, 0, 0] ref [20256, 62631, 0, 0]
237+## SHORT: ours 2028 frames, ref 2054 frames
238+# mean pulses per band (ours / ref)
239+# band_hz ours ref diff
240+0 303.3 285.9 17.5
241+240 325.9 306.4 19.4
242+480 326.1 309.3 16.8
243+720 358.0 336.8 21.2
244+960 288.5 277.1 11.4
245+1200 269.7 260.3 9.4
246+1440 293.4 283.8 9.6
247+1680 277.9 269.2 8.6
248+1920 467.7 450.2 17.5
249+2400 434.7 417.3 17.4
250+2880 406.4 391.3 15.1
251+3360 401.3 387.1 14.2
252+3840 733.0 716.6 16.4
253+4800 671.4 666.7 4.6
254+5760 636.5 634.3 2.2
255+6720 900.1 898.0 2.1
256+8160 831.5 837.4 -5.9
257+9600 1078.0 1094.1 -16.1
258+11520 1407.6 1452.8 -45.2
259+14400 1343.5 1388.4 -44.9
260+18720 2.3 22.5 -20.2
261+# mean fine_quant per band (ours / ref)
262+# band_hz ours ref
263+0 2.09 2.05
264+240 2.21 2.17
265+480 2.22 2.15
266+720 2.39 2.27
267+960 1.98 1.81
268+1200 1.86 1.67
269+1440 1.97 1.83
270+1680 1.80 1.69
271+1920 2.21 2.18
272+2400 2.11 2.09
273+2880 2.03 2.03
274+3360 2.01 2.06
275+3840 2.72 2.57
276+4800 2.64 2.41
277+5760 2.51 2.34
278+6720 2.79 2.96
279+8160 2.76 2.86
280+9600 2.93 2.99
281+11520 3.17 3.16
282+14400 3.43 3.29
283+18720 1.01 1.05
284+# alloc_trim hist [0..10]: ours [891, 1036, 100, 1, 0, 0, 0, 0, 0, 0, 0] ref [2019, 34, 1, 0, 0, 0, 0, 0, 0, 0, 0]
285+# mean dynalloc boost per band (1/8 bit) ours / ref
286+0 15.9 16.4
287+240 53.6 50.8
288+480 67.3 65.4
289+720 111.1 102.9
290+960 58.4 57.0
291+1200 46.9 46.0
292+1440 81.6 80.1
293+1680 72.6 71.5
294+1920 60.7 59.0
295+2400 44.8 40.7
296+2880 33.6 30.0
297+3360 46.7 42.3
298+3840 39.7 34.1
299+4800 30.1 30.3
300+5760 30.5 29.1
301+6720 31.4 30.1
302+8160 18.7 17.1
303+9600 56.4 51.7
304+11520 15.3 13.6
305+14400 9.1 9.3
306+18720 0.5 0.3
307+# alloc_trim mean: ours 0.61 ref 0.02
308+# coded_bands mean Hz: ours 14400 ref 18720
309+# intensity mean Hz: ours 8160 ref 6720
310+# dual_stereo share: ours 0.000 ref 0.000
311+# total_bits mean: ours 1741 ref 1724
312+# balance mean: ours 0 ref 0
313+# spread hist [0..3]: ours [0, 0, 2028, 0] ref [0, 0, 2054, 0]
314+# tf_res hist [-1..2]: ours [0, 42588, 0, 0] ref [6563, 17762, 13303, 5506]
lanes/opus-sb-r1.gate4.txtadded+10 -0
@@ -0,0 +1,10 @@
1+her@64k: ref 67.9 ours 67.4 kbps (-0.8%), corr o=0.9826 r=0.9794 gap=-0.0031, Q o=-375.10 r=-339.00, err_ratio 1.140, minsec o=0.9452 r=0.9406, drop o=0 r=0
2+her@96k: ref 101.5 ours 100.6 kbps (-0.9%), corr o=0.9911 r=0.9902 gap=-0.0008, Q o=-233.83 r=-191.50, err_ratio 1.214, minsec o=0.9715 r=0.9728, drop o=0 r=0
3+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.9703 r=0.9644, drop o=0 r=0
4+reell@96k: ref 105.4 ours 105.3 kbps (-0.0%), corr o=0.9956 r=0.9948 gap=-0.0008, Q o=-156.54 r=-29.16, err_ratio 2.349, minsec o=0.9859 r=0.9804, drop o=0 r=0
5+reelm@64k: ref 63.3 ours 62.3 kbps (-1.6%), corr o=0.9822 r=0.9859 gap=+0.0037, Q o=-292.53 r=-392.46, err_ratio 0.690, minsec o=0.9098 r=0.9100, drop o=0 r=0
6+reelm@96k: ref 84.8 ours 83.6 kbps (-1.5%), corr o=0.9920 r=0.9920 gap=-0.0001, Q o=-152.68 r=-231.16, err_ratio 0.685, minsec o=0.9618 r=0.9511, drop o=0 r=0
7+hein@64k: ref 64.9 ours 63.8 kbps (-1.7%), corr o=0.9851 r=0.9874 gap=+0.0022, Q o=-296.64 r=-388.31, err_ratio 0.712, minsec o=0.9144 r=0.9148, drop o=0 r=0
8+hein@96k: ref 86.9 ours 85.4 kbps (-1.8%), corr o=0.9934 r=0.9930 gap=-0.0003, Q o=-120.45 r=-246.46, err_ratio 0.536, minsec o=0.9639 r=0.9499, drop o=0 r=0
9+RATE GATE: all rows within ±5%
10+DROPOUT GATE: passed (no ours-only dropouts)