forge
Explore
Log in

Don't you dare go Hollow

12k per-band divergence

Browse files at this commit

76642daecc4c0c782a812bc7ea69a4d38bab629bAhmet Kaan GümüşParent: 6d12ee1
  • test
    • 12k per-band divergence
Show full message
test(opus-silkq/r2): 12k per-band divergence
crates/ec-opus/tests/conformance.rsmodified+366 -56
@@ -3846,62 +3846,6 @@ fn silk_silkq_oracle() {
3846 ours_packets.push(out[..len].to_vec());3846 ours_packets.push(out[..len].to_vec());
3847 }3847 }
3848 3848
3849- // Decode both streams through separate SilkDecoders, capturing indices.
3850- fn collect_indices(
3851- packets: &[Vec<u8>],
3852- ) -> Vec<(ec_opus::SilkDecIndices, usize)> {
3853- let mut silk = ec_opus::SilkDecoder::new(16000, 1);
3854- let mut results = Vec::new();
3855- let mut silk_pcm = vec![0i16; 320]; // 20ms at 16kHz
3856- for pkt in packets {
3857- if pkt.len() <= 1 {
3858- continue; // DTX / empty
3859- }
3860- let parsed = match ec_opus::Packet::parse(pkt, false) {
3861- Ok(p) => p,
3862- Err(_) => continue,
3863- };
3864- let toc = parsed.toc;
3865- let mode = toc.mode();
3866- if mode == ec_opus::Mode::Celt {
3867- continue;
3868- }
3869- let internal_rate = if mode == ec_opus::Mode::Hybrid {
3870- 16000
3871- } else {
3872- match toc.bandwidth() {
3873- ec_opus::Bandwidth::Narrow => 8000,
3874- ec_opus::Bandwidth::Medium => 12000,
3875- _ => 16000,
3876- }
3877- };
3878- let frame_48k = toc.frame_size_48k();
3879- let payload_ms = (frame_48k / 48).max(10);
3880- let frame_bytes = parsed.frames.len();
3881- let mut first = true;
3882- for frame_data in &parsed.frames {
3883- if frame_data.len() <= 1 {
3884- first = false;
3885- continue;
3886- }
3887- let mut dec = ec_opus::RangeDecoder::new(frame_data);
3888- let _ = silk.decode(
3889- &mut dec,
3890- &mut silk_pcm,
3891- payload_ms,
3892- internal_rate,
3893- 1,
3894- first,
3895- );
3896- let ix = silk.last_indices();
3897- let bytes = frame_data.len();
3898- results.push((ix, bytes));
3899- first = false;
3900- }
3901- let _ = frame_bytes; // suppress unused
3902- }
3903- results
3904- }
3905 3849
3906 let ours_ix = collect_indices(&ours_packets);3850 let ours_ix = collect_indices(&ours_packets);
3907 let ref_ix = collect_indices(&ref_packets);3851 let ref_ix = collect_indices(&ref_packets);
@@ -4021,6 +3965,372 @@ fn silk_silkq_oracle() {
4021 print!("{}", out_str);3965 print!("{}", out_str);
4022 }3966 }
4023 3967
3968+/// Decode a SILK stream packet-by-packet through a fresh `SilkDecoder`,
3969+/// snapshotting each frame's coded indices — the oracle diagnostic's view of
3970+/// what the bitstream actually coded (our encoder's packets and a reference
3971+/// encoder's alike).
3972+fn collect_indices(packets: &[Vec<u8>]) -> Vec<(ec_opus::SilkDecIndices, usize)> {
3973+ let mut silk = ec_opus::SilkDecoder::new(16000, 1);
3974+ let mut results = Vec::new();
3975+ let mut silk_pcm = vec![0i16; 320]; // 20ms at 16kHz
3976+ for pkt in packets {
3977+ if pkt.len() <= 1 {
3978+ continue; // DTX / empty
3979+ }
3980+ let parsed = match ec_opus::Packet::parse(pkt, false) {
3981+ Ok(p) => p,
3982+ Err(_) => continue,
3983+ };
3984+ let toc = parsed.toc;
3985+ let mode = toc.mode();
3986+ if mode == ec_opus::Mode::Celt {
3987+ continue;
3988+ }
3989+ let internal_rate = if mode == ec_opus::Mode::Hybrid {
3990+ 16000
3991+ } else {
3992+ match toc.bandwidth() {
3993+ ec_opus::Bandwidth::Narrow => 8000,
3994+ ec_opus::Bandwidth::Medium => 12000,
3995+ _ => 16000,
3996+ }
3997+ };
3998+ let payload_ms = (toc.frame_size_48k() / 48).max(10);
3999+ let mut first = true;
4000+ for frame_data in &parsed.frames {
4001+ if frame_data.len() <= 1 {
4002+ first = false;
4003+ continue;
4004+ }
4005+ let mut dec = ec_opus::RangeDecoder::new(frame_data);
4006+ let _ = silk.decode(
4007+ &mut dec,
4008+ &mut silk_pcm,
4009+ payload_ms,
4010+ internal_rate,
4011+ 1,
4012+ first,
4013+ );
4014+ let ix = silk.last_indices();
4015+ let bytes = frame_data.len();
4016+ results.push((ix, bytes));
4017+ first = false;
4018+ }
4019+ }
4020+ results
4021+}
4022+
4023+// ---------------------------------------------------------------------------
4024+// SILK 12k spectral divergence: WHERE the 12 kbps Voip err_ratio outlier
4025+// carries its error. Same machinery as spectral_divergence_vs_libopus, but
4026+// mono/Application::Voip/12 kbps on reelm.wav, both sides decoded through
4027+// ffmpeg libopus (gate symmetry), with the index oracle on the worst frames.
4028+// Run:
4029+// cargo test -p ec-opus --release --test conformance \
4030+// silk_spectral_divergence_12k -- --ignored --nocapture
4031+// Writes lanes/opus-silkq-r2.bands.txt.
4032+// ---------------------------------------------------------------------------
4033+
4034+#[test]
4035+#[ignore]
4036+fn silk_spectral_divergence_12k() {
4037+ const SECS: f64 = 120.0;
4038+ const FRAME: usize = 960; // 20 ms at 48 kHz
4039+ const CHANNELS: usize = 1;
4040+ const MAX_LAG: usize = 2000;
4041+ const KBPS: u32 = 12;
4042+
4043+ let src = shellexpand("~/Music/reelm.wav");
4044+ assert!(src.exists(), "source missing: {}", src.display());
4045+ let source_pcm = ffmpeg_decode_pcm_mono(&src, SECS);
4046+ let source_frames = source_pcm.len() / CHANNELS;
4047+ assert!(source_frames > 48_000, "source too short");
4048+ let seconds = source_frames as f64 / 48000.0;
4049+
4050+ let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes");
4051+ fs::create_dir_all(&lanes_dir).unwrap();
4052+ let ref_ogg = temp_path("opus-silkq-r2-ref.opus");
4053+ let ours_ogg = temp_path("opus-silkq-r2-ours.opus");
4054+
4055+ // Reference: ffmpeg libopus mono voip at 12k, decoded by ffmpeg libopus.
4056+ ffmpeg_encode_libopus_mono_voip(&src, KBPS, &ref_ogg, SECS);
4057+ let ref_bytes = fs::metadata(&ref_ogg).map(|m| m.len() as usize).unwrap_or(0);
4058+ let ref_kbps = ref_bytes as f64 * 8.0 / seconds / 1000.0;
4059+ let ref_dec = ffmpeg_decode(&ref_ogg, CHANNELS).expect("ffmpeg libopus decode of ref");
4060+ let ref_trim: Vec<f32> = ref_dec.into_iter().take(source_frames * CHANNELS).collect();
4061+ let (_, ref_aligned) = align_to_source(&source_pcm, &ref_trim, CHANNELS, MAX_LAG);
4062+ let ref_ogg_bytes = fs::read(&ref_ogg).expect("read ref ogg");
4063+ let ref_packets: Vec<Vec<u8>> = ogg_packets(&ref_ogg_bytes)
4064+ .into_iter()
4065+ .skip(2)
4066+ .filter(|p| !p.is_empty())
4067+ .collect();
4068+ let _ = fs::remove_file(&ref_ogg);
4069+
4070+ // Ours: ec-opus Voip at the reference's realised rate; our packets
4071+ // re-decoded through ffmpeg libopus so both sides cross the same decoder.
4072+ let mut enc = Encoder::new(48000, CHANNELS, Application::Voip).expect("encoder");
4073+ enc.set_bitrate((ref_kbps * 1000.0).round() as u32);
4074+ enc.set_vbr_constrained(true);
4075+ let pre_skip = enc.look_ahead(FRAME) as i64;
4076+ let mut out = vec![0u8; 1500];
4077+ let mut ours_packets: Vec<Vec<u8>> = Vec::new();
4078+ let mut padded = Vec::new();
4079+ for block in source_pcm.chunks(FRAME * CHANNELS) {
4080+ let block = if block.len() < FRAME * CHANNELS {
4081+ padded.clear();
4082+ padded.extend_from_slice(block);
4083+ padded.resize(FRAME * CHANNELS, 0.0);
4084+ &padded[..]
4085+ } else {
4086+ block
4087+ };
4088+ let len = enc.encode_float(block, FRAME, &mut out).expect("encode");
4089+ ours_packets.push(out[..len].to_vec());
4090+ }
4091+ write_ogg_opus(
4092+ &ours_ogg,
4093+ &ours_packets,
4094+ opus_head(CHANNELS, pre_skip as u16, None),
4095+ CHANNELS,
4096+ source_frames,
4097+ pre_skip,
4098+ );
4099+ let ours_bytes: usize = ours_packets.iter().map(|p| p.len()).sum();
4100+ let ours_kbps = ours_bytes as f64 * 8.0 / seconds / 1000.0;
4101+ let ours_dec = ffmpeg_decode(&ours_ogg, CHANNELS).expect("ffmpeg libopus decode of ours");
4102+ let _ = fs::remove_file(&ours_ogg);
4103+ let ours_trim: Vec<f32> = ours_dec.into_iter().take(source_frames * CHANNELS).collect();
4104+ let (_, ours_aligned) = align_to_source(&source_pcm, &ours_trim, CHANNELS, MAX_LAG);
4105+
4106+ // opus_compare totals, per-band eb², per-frame ef² — both vs the source.
4107+ let cmp_frames = source_frames
4108+ .min(ours_aligned.len() / CHANNELS)
4109+ .min(ref_aligned.len() / CHANNELS);
4110+ let take = |v: &[f32]| -> Vec<i16> { to_i16(&v[..cmp_frames * CHANNELS]) };
4111+ let s_i = take(&source_pcm);
4112+ let o_i = take(&ours_aligned);
4113+ let r_i = take(&ref_aligned);
4114+ let (err_o, eb2_o, ef2_o) = opus_compare_err_parts(&s_i, &o_i, CHANNELS);
4115+ let (err_r, eb2_r, ef2_r) = opus_compare_err_parts(&s_i, &r_i, CHANNELS);
4116+ let nframes = ef2_o.len();
4117+ assert_eq!(nframes, ef2_r.len(), "frame counts differ");
4118+
4119+ // Raw (unmasked) band energies: spectral shape + dominant-band lookup.
4120+ let as_f64 = |v: &[i16]| -> Vec<f64> { v.iter().map(|&x| x as f64).collect() };
4121+ let mut bands_s = Vec::new();
4122+ let mut bands_o = Vec::new();
4123+ let mut bands_r = Vec::new();
4124+ let mut scratch_ps = Vec::new();
4125+ band_energy(&as_f64(&s_i), CHANNELS, nframes, &mut Some(&mut bands_s), &mut scratch_ps);
4126+ band_energy(&as_f64(&o_i), CHANNELS, nframes, &mut Some(&mut bands_o), &mut scratch_ps);
4127+ band_energy(&as_f64(&r_i), CHANNELS, nframes, &mut Some(&mut bands_r), &mut scratch_ps);
4128+
4129+ // Oracle indices both sides; FFT frame xi -> 20 ms packet index.
4130+ let ours_ix = collect_indices(&ours_packets);
4131+ let ref_ix = collect_indices(&ref_packets);
4132+ let pkt_of = |xi: usize| xi * WIN_STEP / (FRAME * CHANNELS);
4133+
4134+ let mut report = String::new();
4135+ report.push_str("# reelm@12k SILK per-band divergence: ours vs ffmpeg-libopus (r2)\n");
4136+ report.push_str("# mono, 120s, 20ms frames, VBR constrained, 48kHz, Application::Voip\n");
4137+ report.push_str(&format!(
4138+ "# rates: ours {ours_kbps:.2} kbps, ref {ref_kbps:.2} kbps; \
4139+ err ours={err_o:.3} ref={err_r:.3} ratio={:.2}; nframes={nframes}\n",
4140+ err_o / err_r
4141+ ));
4142+
4143+ // (1) 21-band table: which bands carry the error.
4144+ let sum_o: f64 = eb2_o.iter().sum();
4145+ let sum_r: f64 = eb2_r.iter().sum();
4146+ report.push_str(
4147+ "# eb2 = band's mean share of the pre-squared frame error; \
4148+ dln = mean ln(E_test/E_src); band i ends at CELT_EBANDS[i+1]*240 Hz\n",
4149+ );
4150+ report.push_str("# band lo_hz hi_hz dln_o dln_r eb2_o eb2_r ratio share_o share_r\n");
4151+ for bi in 0..NBANDS {
4152+ let (mut dol, mut drl) = (0.0f64, 0.0f64);
4153+ for xi in 0..nframes {
4154+ let sl = bands_s[(xi * NBANDS + bi) * CHANNELS].ln();
4155+ dol += bands_o[(xi * NBANDS + bi) * CHANNELS].ln() - sl;
4156+ drl += bands_r[(xi * NBANDS + bi) * CHANNELS].ln() - sl;
4157+ }
4158+ dol /= nframes as f64;
4159+ drl /= nframes as f64;
4160+ let ratio = if eb2_r[bi] > 0.0 { eb2_o[bi] / eb2_r[bi] } else { f64::INFINITY };
4161+ report.push_str(&format!(
4162+ "{:5} {:5} {:6} {:+.3} {:+.3} {:.3e} {:.3e} {:>7} {:5.1}% {:5.1}%\n",
4163+ bi,
4164+ CELT_EBANDS[bi] * 240,
4165+ CELT_EBANDS[bi + 1] * 240,
4166+ dol,
4167+ drl,
4168+ eb2_o[bi],
4169+ eb2_r[bi],
4170+ ratio,
4171+ 100.0 * eb2_o[bi] / sum_o,
4172+ 100.0 * eb2_r[bi] / sum_r,
4173+ ));
4174+ }
4175+
4176+ // (2) top-15 frames by ef2(ours), with the dominant band and the oracle.
4177+ let mut by_ef2: Vec<usize> = (0..nframes).collect();
4178+ by_ef2.sort_by(|&a, &b| ef2_o[b].partial_cmp(&ef2_o[a]).unwrap());
4179+ report.push_str(
4180+ "\n# top-15 frames by ef2(ours) (ef2 = frame's squared opus_compare error); \
4181+ dom = argmax_b (lnE_o - lnE_s)^2, raw bands\n",
4182+ );
4183+ report.push_str(
4184+ "# t(s) fft pkt ef2_o ef2_r dom domband_hz | side sig qoff gains[4] nlsf_i lag cont per ltp[4] ltpscl seed bytes\n",
4185+ );
4186+ for &xi in by_ef2.iter().take(15) {
4187+ let pkt = pkt_of(xi);
4188+ let t = (xi * WIN_STEP) as f64 / 48000.0;
4189+ let (mut dom, mut dom_v) = (0usize, -1.0f64);
4190+ for bi in 0..NBANDS {
4191+ let sl = bands_s[(xi * NBANDS + bi) * CHANNELS].ln();
4192+ let d = bands_o[(xi * NBANDS + bi) * CHANNELS].ln() - sl;
4193+ if d * d > dom_v {
4194+ dom_v = d * d;
4195+ dom = bi;
4196+ }
4197+ }
4198+ report.push_str(&format!(
4199+ "{:7.3} {:5} {:4} {:.3e} {:.3e} {:2} {}-{}Hz\n",
4200+ t,
4201+ xi,
4202+ pkt,
4203+ ef2_o[xi],
4204+ ef2_r[xi],
4205+ dom,
4206+ CELT_EBANDS[dom] * 240,
4207+ CELT_EBANDS[dom + 1] * 240
4208+ ));
4209+ for (side, ixv) in [(" O", ours_ix.get(pkt)), (" R", ref_ix.get(pkt))] {
4210+ match ixv {
4211+ Some((ix, bytes)) => {
4212+ let voiced = ix.signal_type == 2;
4213+ let opt = |v: String, on: bool| if on { v } else { "-".to_owned() };
4214+ report.push_str(&format!(
4215+ " {side} sig={} qoff={} gains=[{},{},{},{}] nlsf_i={} lag={} cont={} per={} ltp=[{},{},{},{}] ltpscl={} seed={} bytes={}\n",
4216+ ix.signal_type,
4217+ ix.quant_offset_type,
4218+ ix.gains[0],
4219+ ix.gains[1],
4220+ ix.gains[2],
4221+ ix.gains[3],
4222+ ix.nlsf_interp_coef_q2,
4223+ opt(format!("{}", ix.lag_index), voiced),
4224+ opt(format!("{}", ix.contour_index), voiced),
4225+ opt(format!("{}", ix.per_index), voiced),
4226+ opt(format!("{}", ix.ltp_index[0]), voiced),
4227+ opt(format!("{}", ix.ltp_index[1]), voiced),
4228+ opt(format!("{}", ix.ltp_index[2]), voiced),
4229+ opt(format!("{}", ix.ltp_index[3]), voiced),
4230+ opt(format!("{}", ix.ltp_scale_index), voiced),
4231+ ix.seed,
4232+ bytes
4233+ ));
4234+ }
4235+ None => report.push_str(&format!(" {side} -- missing --\n")),
4236+ }
4237+ }
4238+ }
4239+
4240+ // (3) tail vs body: how much of the total error the worst frames carry,
4241+ // and whether the 11x ratio survives de-tailing (err = (Σef2/n)^(1/16)).
4242+ let tail = |ef2: &[f64]| -> (usize, f64, f64, usize, f64) {
4243+ let mut s = ef2.to_vec();
4244+ s.sort_by(|a, b| b.partial_cmp(a).unwrap());
4245+ let tot: f64 = s.iter().sum();
4246+ let k = (s.len() / 100).max(1);
4247+ let top1: f64 = s[..k].iter().sum();
4248+ let (mut half, mut acc) = (s.len(), 0.0);
4249+ for (i, v) in s.iter().enumerate() {
4250+ acc += v;
4251+ if acc >= 0.5 * tot {
4252+ half = i + 1;
4253+ break;
4254+ }
4255+ }
4256+ (k, top1 / tot, half as f64 / s.len() as f64, half, tot)
4257+ };
4258+ let root16 = |tot: f64, n: usize| tot.powf(1.0 / 16.0) / (n as f64).powf(1.0 / 16.0);
4259+ let (k, sh1_o, halff_o, half_o, _tot_o) = tail(&ef2_o);
4260+ let (_, sh1_r, halff_r, half_r, _tot_r) = tail(&ef2_r);
4261+ report.push_str(&format!(
4262+ "# tail: top-1% ({k} frames) carry {:.2}% of ours' Σef2 vs {:.2}% of ref's; \
4263+ 50% of ours' err sits in the worst {half_o} frames ({:.2}%) \
4264+ vs ref's worst {half_r} ({:.2}%)\n",
4265+ 100.0 * sh1_o, 100.0 * sh1_r, 100.0 * halff_o, 100.0 * halff_r
4266+ ));
4267+ // De-tailed: drop each side's worst 50 frames (of 24k FFT frames/pkt-mapped).
4268+ let clip50 = |ef2: &[f64]| -> f64 {
4269+ let mut s = ef2.to_vec();
4270+ s.sort_by(|a, b| b.partial_cmp(a).unwrap());
4271+ s[50..].iter().sum()
4272+ };
4273+ let err_o_clip = root16(clip50(&ef2_o), nframes);
4274+ let err_r_clip = root16(clip50(&ef2_r), nframes);
4275+ report.push_str(&format!(
4276+ "# de-tailed (worst 50 of {nframes} FFT frames dropped per side): \
4277+ err ours={err_o_clip:.3} ref={err_r_clip:.3} ratio={:.2} (was {err_o:.3}/{err_r:.3}/{:.2})\n",
4278+ err_o_clip / err_r_clip,
4279+ err_o / err_r
4280+ ));
4281+ // Burst signature: per 20 ms packet, does first-subframe gain >= 60 or a
4282+ // starved packet (<= 20 bytes) line up with the packet's mean ef2?
4283+ let mut pkt_ef2 = vec![0.0f64; ours_packets.len()];
4284+ let mut pkt_cnt = vec![0u32; ours_packets.len()];
4285+ for xi in 0..nframes {
4286+ let p = pkt_of(xi).min(pkt_ef2.len() - 1);
4287+ pkt_ef2[p] += ef2_o[xi];
4288+ pkt_cnt[p] += 1;
4289+ }
4290+ let mut g_hi = (0.0f64, 0usize); // (sum, count) packets with gains[0] >= 60
4291+ let mut g_lo = (0.0f64, 0usize);
4292+ let mut b_lo = (0.0f64, 0usize); // bytes <= 20
4293+ let mut b_hi = (0.0f64, 0usize);
4294+ for (i, (ix, bytes)) in ours_ix.iter().enumerate() {
4295+ if i >= pkt_ef2.len() {
4296+ break;
4297+ }
4298+ let mean = if pkt_cnt[i] > 0 { pkt_ef2[i] / pkt_cnt[i] as f64 } else { 0.0 };
4299+ if ix.gains[0] >= 60 {
4300+ g_hi.0 += mean;
4301+ g_hi.1 += 1;
4302+ } else {
4303+ g_lo.0 += mean;
4304+ g_lo.1 += 1;
4305+ }
4306+ if *bytes <= 20 {
4307+ b_lo.0 += mean;
4308+ b_lo.1 += 1;
4309+ } else {
4310+ b_hi.0 += mean;
4311+ b_hi.1 += 1;
4312+ }
4313+ }
4314+ report.push_str(&format!(
4315+ "# burst signature: packets with gains[0]>=60: {}/{}, mean ef2 {:.3e}; \
4316+ gains[0]<60: mean ef2 {:.3e} || packets <=20 bytes: {}/{}, mean ef2 {:.3e}; \
4317+ >20 bytes: mean ef2 {:.3e}\n",
4318+ g_hi.1,
4319+ g_hi.1 + g_lo.1,
4320+ if g_hi.1 > 0 { g_hi.0 / g_hi.1 as f64 } else { f64::NAN },
4321+ if g_lo.1 > 0 { g_lo.0 / g_lo.1 as f64 } else { f64::NAN },
4322+ b_lo.1,
4323+ b_lo.1 + b_hi.1,
4324+ if b_lo.1 > 0 { b_lo.0 / b_lo.1 as f64 } else { f64::NAN },
4325+ if b_hi.1 > 0 { b_hi.0 / b_hi.1 as f64 } else { f64::NAN },
4326+ ));
4327+
4328+ let out_path = lanes_dir.join("opus-silkq-r2.bands.txt");
4329+ fs::write(&out_path, &report).unwrap();
4330+ println!("wrote {}", out_path.display());
4331+ print!("{}", report);
4332+}
4333+
4024 // ---------------------------------------------------------------------------4334 // ---------------------------------------------------------------------------
4025 // Harness: feed identical raw PCM (.sw, s16le interleaved) to our opus_compare4335 // Harness: feed identical raw PCM (.sw, s16le interleaved) to our opus_compare
4026 // so it can be checked against the C `opus_compare` tool on the SAME bytes.4336 // so it can be checked against the C `opus_compare` tool on the SAME bytes.
lanes/opus-silkq-r2.bands.txtadded+77 -0
@@ -0,0 +1,77 @@
1+# reelm@12k SILK per-band divergence: ours vs ffmpeg-libopus (r2)
2+# mono, 120s, 20ms frames, VBR constrained, 48kHz, Application::Voip
3+# rates: ours 12.38 kbps, ref 11.98 kbps; err ours=27.421 ref=2.407 ratio=11.39; nframes=47997
4+# eb2 = band's mean share of the pre-squared frame error; dln = mean ln(E_test/E_src); band i ends at CELT_EBANDS[i+1]*240 Hz
5+# band lo_hz hi_hz dln_o dln_r eb2_o eb2_r ratio share_o share_r
6+ 0 0 240 -0.404 -1.015 9.001e2 8.256e-2 10901.483587521187 8.1% 4.5%
7+ 1 240 480 -0.555 -0.822 1.503e2 3.133e-2 4798.8320168752825 1.4% 1.7%
8+ 2 480 720 -0.321 -0.719 2.740e2 3.078e-2 8903.992616391502 2.5% 1.7%
9+ 3 720 960 -0.203 -0.727 1.979e1 6.301e-2 314.11641684740283 0.2% 3.4%
10+ 4 960 1200 +0.086 -0.653 7.565e2 1.271e-1 5949.7190716845535 6.8% 6.9%
11+ 5 1200 1440 -0.100 -0.722 2.464e2 6.096e-2 4041.469210559519 2.2% 3.3%
12+ 6 1440 1680 -0.053 -0.669 6.312e1 6.228e-2 1013.5273671435615 0.6% 3.4%
13+ 7 1680 1920 +0.090 -0.590 6.521e1 1.371e-1 475.68774048015473 0.6% 7.4%
14+ 8 1920 2400 -0.061 -0.686 6.777e2 1.671e-1 4056.5361856691347 6.1% 9.0%
15+ 9 2400 2880 -0.053 -0.685 3.503e3 7.006e-2 50002.415780327836 31.5% 3.8%
16+ 10 2880 3360 -0.159 -0.713 1.983e3 1.380e-1 14374.382059534017 17.9% 7.5%
17+ 11 3360 3840 -0.193 -0.711 1.443e3 1.083e-1 13319.398793662045 13.0% 5.9%
18+ 12 3840 4800 -0.090 -0.640 7.548e2 1.246e-1 6057.742411724968 6.8% 6.7%
19+ 13 4800 5760 -0.377 -0.734 1.358e2 1.598e-1 850.1916761988703 1.2% 8.6%
20+ 14 5760 6720 -0.345 -0.685 5.009e1 7.785e-2 643.3793387719822 0.5% 4.2%
21+ 15 6720 8160 -0.203 -0.594 3.432e1 7.665e-2 447.77759764424064 0.3% 4.1%
22+ 16 8160 9600 +0.074 -0.366 5.147e1 3.265e-2 1576.4684713915938 0.5% 1.8%
23+ 17 9600 11520 -1.565 -1.530 1.430e-1 1.325e-1 1.0791172707432997 0.0% 7.2%
24+ 18 11520 14400 -1.910 -1.910 1.345e-1 1.226e-1 1.0965271211155312 0.0% 6.6%
25+ 19 14400 18720 -1.103 -1.104 4.699e-2 4.612e-2 1.0188001325977802 0.0% 2.5%
26+ 20 18720 24000 -0.046 -0.046 3.853e-5 3.768e-5 1.0225011087163844 0.0% 0.0%
27+
28+# top-15 frames by ef2(ours) (ef2 = frame's squared opus_compare error); dom = argmax_b (lnE_o - lnE_s)^2, raw bands
29+# t(s) fft pkt ef2_o ef2_r dom domband_hz | side sig qoff gains[4] nlsf_i lag cont per ltp[4] ltpscl seed bytes
30+ 2.780 1112 139 4.051e27 4.494e-8 9 2400-2880Hz
31+ O sig=2 qoff=0 gains=[63,4,4,4] nlsf_i=4 lag=49 cont=3 per=0 ltp=[1,1,1,1] ltpscl=0 seed=0 bytes=14
32+ R sig=2 qoff=0 gains=[23,4,5,4] nlsf_i=4 lag=51 cont=0 per=2 ltp=[11,11,11,11] ltpscl=0 seed=0 bytes=37
33+ 2.777 1111 138 5.487e26 5.770e-8 9 2400-2880Hz
34+ O sig=2 qoff=0 gains=[40,2,3,5] nlsf_i=4 lag=48 cont=3 per=1 ltp=[0,3,3,3] ltpscl=0 seed=0 bytes=46
35+ R sig=2 qoff=0 gains=[21,6,4,4] nlsf_i=4 lag=50 cont=1 per=2 ltp=[15,0,10,10] ltpscl=0 seed=2 bytes=39
36+ 2.783 1113 139 2.959e26 8.289e-8 9 2400-2880Hz
37+ O sig=2 qoff=0 gains=[63,4,4,4] nlsf_i=4 lag=49 cont=3 per=0 ltp=[1,1,1,1] ltpscl=0 seed=0 bytes=14
38+ R sig=2 qoff=0 gains=[23,4,5,4] nlsf_i=4 lag=51 cont=0 per=2 ltp=[11,11,11,11] ltpscl=0 seed=0 bytes=37
39+ 51.998 20799 2599 2.708e24 3.126e-9 8 1920-2400Hz
40+ O sig=2 qoff=0 gains=[44,0,0,5] nlsf_i=4 lag=146 cont=3 per=0 ltp=[1,1,0,1] ltpscl=0 seed=0 bytes=44
41+ R sig=2 qoff=0 gains=[26,4,4,2] nlsf_i=4 lag=183 cont=5 per=1 ltp=[3,7,7,0] ltpscl=0 seed=2 bytes=30
42+ 52.005 20802 2600 2.003e24 1.938e-9 4 960-1200Hz
43+ O sig=2 qoff=0 gains=[63,4,4,4] nlsf_i=4 lag=125 cont=3 per=2 ltp=[7,7,7,7] ltpscl=0 seed=0 bytes=16
44+ R sig=2 qoff=0 gains=[24,3,4,4] nlsf_i=4 lag=127 cont=12 per=1 ltp=[3,3,0,3] ltpscl=0 seed=0 bytes=32
45+ 52.000 20800 2600 1.715e24 5.741e-8 4 960-1200Hz
46+ O sig=2 qoff=0 gains=[63,4,4,4] nlsf_i=4 lag=125 cont=3 per=2 ltp=[7,7,7,7] ltpscl=0 seed=0 bytes=16
47+ R sig=2 qoff=0 gains=[24,3,4,4] nlsf_i=4 lag=127 cont=12 per=1 ltp=[3,3,0,3] ltpscl=0 seed=0 bytes=32
48+ 52.002 20801 2600 5.132e23 2.970e-8 4 960-1200Hz
49+ O sig=2 qoff=0 gains=[63,4,4,4] nlsf_i=4 lag=125 cont=3 per=2 ltp=[7,7,7,7] ltpscl=0 seed=0 bytes=16
50+ R sig=2 qoff=0 gains=[24,3,4,4] nlsf_i=4 lag=127 cont=12 per=1 ltp=[3,3,0,3] ltpscl=0 seed=0 bytes=32
51+ 52.015 20806 2600 1.163e23 3.251e-9 4 960-1200Hz
52+ O sig=2 qoff=0 gains=[63,4,4,4] nlsf_i=4 lag=125 cont=3 per=2 ltp=[7,7,7,7] ltpscl=0 seed=0 bytes=16
53+ R sig=2 qoff=0 gains=[24,3,4,4] nlsf_i=4 lag=127 cont=12 per=1 ltp=[3,3,0,3] ltpscl=0 seed=0 bytes=32
54+ 2.775 1110 138 7.458e22 5.930e-8 9 2400-2880Hz
55+ O sig=2 qoff=0 gains=[40,2,3,5] nlsf_i=4 lag=48 cont=3 per=1 ltp=[0,3,3,3] ltpscl=0 seed=0 bytes=46
56+ R sig=2 qoff=0 gains=[21,6,4,4] nlsf_i=4 lag=50 cont=1 per=2 ltp=[15,0,10,10] ltpscl=0 seed=2 bytes=39
57+ 52.008 20803 2600 2.201e22 1.857e-8 7 1680-1920Hz
58+ O sig=2 qoff=0 gains=[63,4,4,4] nlsf_i=4 lag=125 cont=3 per=2 ltp=[7,7,7,7] ltpscl=0 seed=0 bytes=16
59+ R sig=2 qoff=0 gains=[24,3,4,4] nlsf_i=4 lag=127 cont=12 per=1 ltp=[3,3,0,3] ltpscl=0 seed=0 bytes=32
60+ 2.785 1114 139 8.835e21 4.607e-8 9 2400-2880Hz
61+ O sig=2 qoff=0 gains=[63,4,4,4] nlsf_i=4 lag=49 cont=3 per=0 ltp=[1,1,1,1] ltpscl=0 seed=0 bytes=14
62+ R sig=2 qoff=0 gains=[23,4,5,4] nlsf_i=4 lag=51 cont=0 per=2 ltp=[11,11,11,11] ltpscl=0 seed=0 bytes=37
63+ 52.013 20805 2600 1.187e21 8.529e-7 4 960-1200Hz
64+ O sig=2 qoff=0 gains=[63,4,4,4] nlsf_i=4 lag=125 cont=3 per=2 ltp=[7,7,7,7] ltpscl=0 seed=0 bytes=16
65+ R sig=2 qoff=0 gains=[24,3,4,4] nlsf_i=4 lag=127 cont=12 per=1 ltp=[3,3,0,3] ltpscl=0 seed=0 bytes=32
66+ 51.995 20798 2599 7.481e20 1.221e-9 5 1200-1440Hz
67+ O sig=2 qoff=0 gains=[44,0,0,5] nlsf_i=4 lag=146 cont=3 per=0 ltp=[1,1,0,1] ltpscl=0 seed=0 bytes=44
68+ R sig=2 qoff=0 gains=[26,4,4,2] nlsf_i=4 lag=183 cont=5 per=1 ltp=[3,7,7,0] ltpscl=0 seed=2 bytes=30
69+ 52.010 20804 2600 3.759e20 3.219e-6 4 960-1200Hz
70+ O sig=2 qoff=0 gains=[63,4,4,4] nlsf_i=4 lag=125 cont=3 per=2 ltp=[7,7,7,7] ltpscl=0 seed=0 bytes=16
71+ R sig=2 qoff=0 gains=[24,3,4,4] nlsf_i=4 lag=127 cont=12 per=1 ltp=[3,3,0,3] ltpscl=0 seed=0 bytes=32
72+ 51.943 20777 2597 2.894e20 2.264e-11 5 1200-1440Hz
73+ O sig=2 qoff=0 gains=[63,0,2,7] nlsf_i=4 lag=200 cont=3 per=0 ltp=[1,1,1,1] ltpscl=0 seed=0 bytes=16
74+ R sig=1 qoff=0 gains=[29,3,1,4] nlsf_i=4 lag=- cont=- per=- ltp=[-,-,-,-] ltpscl=- seed=2 bytes=24
75+# tail: top-1% (479 frames) carry 100.00% of ours' Σef2 vs 100.00% of ref's; 50% of ours' err sits in the worst 1 frames (0.00%) vs ref's worst 1 (0.00%)
76+# de-tailed (worst 50 of 47997 FFT frames dropped per side): err ours=5.624 ref=0.812 ratio=6.93 (was 27.421/2.407/11.39)
77+# burst signature: packets with gains[0]>=60: 24/6000, mean ef2 2.266e25; gains[0]<60: mean ef2 1.154e22 || packets <=20 bytes: 141/6000, mean ef2 3.857e24; >20 bytes: mean ef2 1.177e22
lanes/opus-silkq-r2.report.mdadded+107 -0
@@ -0,0 +1,107 @@
1+# opus-silkq r2 — where the 12 kbps SILK-WB error lives
2+
3+Setup: reelm.wav @ 12 kbps target, mono, `Application::Voip`, 120 s, 20 ms frames,
4+VBR-constrained, both sides decoded through ffmpeg libopus (gate symmetry).
5+Full tables: `lanes/opus-silkq-r2.bands.txt`.
6+Reproduce: `cargo test -p ec-opus --release --test conformance silk_spectral_divergence_12k -- --ignored --nocapture`
7+
8+Headline: err ours=27.421 ref=2.407 **ratio=11.39** (reproduces the gate's ~11×);
9+rates ours 12.38 kbps vs ref 11.98 kbps (±3%); nframes=47997.
10+
11+## Mechanism (named)
12+
13+**Burst-starved constrained-VBR frames + uniform body coarseness — not a
14+bandwidth/aliasing defect.**
15+
16+1. **Tail — bit-reservoir starvation bursts.** 141/6000 packets (2.35%) are
17+ starved to ≤20 bytes (worst: 14–16 B on voiced frames libopus codes at 24–39 B).
18+ On the worst 24 of these the first-subframe gain index codes **63 (max)** →
19+ decoder emits a loud mid-band burst (dominant band 2.4–3.8 kHz) where the
20+ source is quiet: ef2 up to 4.05e27 vs ref 4.5e-8 at the same instant.
21+ These ~24 packets carry ~100% of ours' Σef2. Worst clusters: t≈2.78 s
22+ (pkt 138/139: bytes 46→14, gains[0] 40→63 — spend-then-starve alternation)
23+ and t≈51.94–52.02 s (pkts 2597–2600: 16 B, gains[0]=63).
24+2. **Body — uniform elevation.** Every band 0–16 (0–9.6 kHz) shows ours' mean
25+ eb² at 314×–50 000× ref, tracking signal energy (bands 9–11 hold 62% of
26+ the share simply because speech energy lives there). dln shows ours' mean
27+ level *closer* to source than ref's (ref over-attenuates, dln ≈ −0.6…−1.0;
28+ ours ≈ −0.05…−0.55) — so the error is noise/structure, not level. Consistent
29+ with r1's systematic first-subframe gain-index offset (ours 33–47 vs ref 26–39):
30+ over-coarse quantization everywhere, no single band mechanism.
31+3. **Not aliasing/bandwidth:** bands 17–20 (>9.6 kHz) ratio 1.02–1.10, ours ≈ ref
32+ (both SILK-WB, nothing up there). Band 16 (8.16–9.6 kHz) ratio 1576× is
33+ SILK's 8 kHz edge leakage, same order as body bands.
34+
35+## Table 1 — per-band mean eb² (ours/ref), dln = mean ln(E_test/E_src)
36+
37+| band | Hz | dln_o | dln_r | eb2_o | eb2_r | ratio | share_o | share_r |
38+|---|---|---|---|---|---|---|---|---|
39+| 0 | 0–240 | −0.404 | −1.015 | 9.0e2 | 8.3e−2 | 1.1e4 | 8.1% | 4.5% |
40+| 1 | 240–480 | −0.555 | −0.822 | 1.5e2 | 3.1e−2 | 4.8e3 | 1.4% | 1.7% |
41+| 2 | 480–720 | −0.321 | −0.719 | 2.7e2 | 3.1e−2 | 8.9e3 | 2.5% | 1.7% |
42+| 3 | 720–960 | −0.203 | −0.727 | 2.0e1 | 6.3e−2 | 3.1e2 | 0.2% | 3.4% |
43+| 4 | 960–1200 | +0.086 | −0.653 | 7.6e2 | 1.3e−1 | 5.9e3 | 6.8% | 6.9% |
44+| 5 | 1200–1440 | −0.100 | −0.722 | 2.5e2 | 6.1e−2 | 4.0e3 | 2.2% | 3.3% |
45+| 6 | 1440–1680 | −0.053 | −0.669 | 6.3e1 | 6.2e−2 | 1.0e3 | 0.6% | 3.4% |
46+| 7 | 1680–1920 | +0.090 | −0.590 | 6.5e1 | 1.4e−1 | 4.8e2 | 0.6% | 7.4% |
47+| 8 | 1920–2400 | −0.061 | −0.686 | 6.8e2 | 1.7e−1 | 4.1e3 | 6.1% | 9.0% |
48+| **9** | **2400–2880** | −0.053 | −0.685 | 3.5e3 | 7.0e−2 | **5.0e4** | **31.5%** | 3.8% |
49+| **10** | **2880–3360** | −0.159 | −0.713 | 2.0e3 | 1.4e−1 | **1.4e4** | **17.9%** | 7.5% |
50+| **11** | **3360–3840** | −0.193 | −0.711 | 1.4e3 | 1.1e−1 | **1.3e4** | **13.0%** | 5.9% |
51+| 12 | 3840–4800 | −0.090 | −0.640 | 7.5e2 | 1.2e−1 | 6.1e3 | 6.8% | 6.7% |
52+| 13 | 4800–5760 | −0.377 | −0.734 | 1.4e2 | 1.6e−1 | 8.5e2 | 1.2% | 8.6% |
53+| 14 | 5760–6720 | −0.345 | −0.685 | 5.0e1 | 7.8e−2 | 6.4e2 | 0.5% | 4.2% |
54+| 15 | 6720–8160 | −0.203 | −0.594 | 3.4e1 | 7.7e−2 | 4.5e2 | 0.3% | 4.1% |
55+| 16 | 8160–9600 | +0.074 | −0.366 | 5.1e1 | 3.3e−2 | 1.6e3 | 0.5% | 1.8% |
56+| 17 | 9600–11520 | −1.565 | −1.530 | 1.4e−1 | 1.3e−1 | **1.08** | 0.0% | 7.2% |
57+| 18 | 11520–14400 | −1.910 | −1.910 | 1.3e−1 | 1.2e−1 | **1.10** | 0.0% | 6.6% |
58+| 19 | 14400–18720 | −1.103 | −1.104 | 4.7e−2 | 4.6e−2 | **1.02** | 0.0% | 2.5% |
59+| 20 | 18720–24000 | −0.046 | −0.046 | 3.9e−5 | 3.8e−5 | **1.02** | 0.0% | 0.0% |
60+
61+## Table 2 — worst frames (top of 15; full table in bands.txt)
62+
63+| t(s) | pkt | ef2_ours | ef2_ref | dom band | ours: B, g0 | ref: B, g0 |
64+|---|---|---|---|---|---|---|
65+| 2.780 | 139 | 4.05e27 | 4.5e−8 | 2.4–2.9k | **14, 63** | 37, 23 |
66+| 2.777 | 138 | 5.49e26 | 5.8e−8 | 2.4–2.9k | 46, 40 | 39, 21 |
67+| 2.783 | 139 | 2.96e26 | 8.3e−8 | 2.4–2.9k | **14, 63** | 37, 23 |
68+| 51.998 | 2599 | 2.71e24 | 3.1e−9 | 1.9–2.4k | 44, 44 | 30, 26 |
69+| 52.005 | 2600 | 2.00e24 | 1.9e−9 | 1.0–1.2k | **16, 63** | 32, 24 |
70+| 52.000–52.015 | 2600 | 1.2e23–1.7e24 | ~e−9 | 1.0–1.9k | **16, 63** | 32, 24 |
71+| 51.943 | 2597 | 2.89e20 | 2.3e−11 | 1.2–1.4k | **16, 63** (lag 200) | 24, 29 |
72+
73+Oracle rows (sig/qoff/nlsf_i/lag/cont/per/ltp) in bands.txt — both sides code
74+these frames voiced (sig=2, qoff=0, nlsf_i=4); ours differs in bytes, g0, and
75+contour (ours cont=3 everywhere vs ref 0/1/5/12) — contour bookkeeping is a
76+secondary suspect, but bytes+g0 is the load-bearing difference.
77+
78+## Table 3 — tail vs body
79+
80+- top-1% (479 frames) carry 100.00% of Σef2 **both sides** (ef² is squared
81+ twice before summing — the metric is tail-dominated by construction).
82+- de-tailed (worst 50 frames dropped per side): err ours=5.624 ref=0.812 →
83+ **ratio 6.93** (was 11.39). Bursts alone explain roughly half the log-scale gap.
84+- burst signature: packets with gains[0]≥60: **24/6000**, mean ef2 2.27e25
85+ (vs 1.15e22 for the rest, ≈2000×); packets ≤20 bytes: **141/6000**, mean
86+ ef2 3.86e24 (vs 1.18e22, ≈330×).
87+- [INFERENCE] If the ≤20 B starvation were eliminated (bursts → body quality):
88+ ours' err ≈ 5.624 vs unclipped ref 2.407 → **ratio ≈ 2.3**, a −80% drop —
89+ far past the gate KEEP threshold (−20%).
90+
91+## Fix decision
92+
93+**No encoder change this round.** The table names the mechanism but does not
94+identify a ≤10-line port: the candidate libopus logic (voiced-frame minimum-bit
95+floor / constrained-VBR reservoir clamp in silk VBR bit assignment) is not
96+present locally (only opus headers installed) and porting rate-control from
97+memory is off-limits. r3 pointer: fetch libopus `silk/VBR.c` +
98+`encode_frame` bit assignment, find the per-frame min/clamp, port behind
99+`const ON`, gate, SIGN RULE. Expected effect per the inference above.
100+
101+## Verification
102+
103+- `cargo check -p ec-opus --tests` — clean.
104+- `silk_spectral_divergence_12k` (release, --ignored): ok, 29.5 s, wrote
105+ `lanes/opus-silkq-r2.bands.txt` (75 lines).
106+- `cargo test -p ec-opus --release`: **34 + 27 passed, 0 failed** (13 ignored
107+ diagnostics, incl. the new one).