diff --git a/crates/ec-opus/tests/conformance.rs b/crates/ec-opus/tests/conformance.rs index d29576ce..55c95abd 100644 --- a/crates/ec-opus/tests/conformance.rs +++ b/crates/ec-opus/tests/conformance.rs @@ -3846,62 +3846,6 @@ fn silk_silkq_oracle() { ours_packets.push(out[..len].to_vec()); } - // Decode both streams through separate SilkDecoders, capturing indices. - fn collect_indices( - packets: &[Vec], - ) -> Vec<(ec_opus::SilkDecIndices, usize)> { - let mut silk = ec_opus::SilkDecoder::new(16000, 1); - let mut results = Vec::new(); - let mut silk_pcm = vec![0i16; 320]; // 20ms at 16kHz - for pkt in packets { - if pkt.len() <= 1 { - continue; // DTX / empty - } - let parsed = match ec_opus::Packet::parse(pkt, false) { - Ok(p) => p, - Err(_) => continue, - }; - let toc = parsed.toc; - let mode = toc.mode(); - if mode == ec_opus::Mode::Celt { - continue; - } - let internal_rate = if mode == ec_opus::Mode::Hybrid { - 16000 - } else { - match toc.bandwidth() { - ec_opus::Bandwidth::Narrow => 8000, - ec_opus::Bandwidth::Medium => 12000, - _ => 16000, - } - }; - let frame_48k = toc.frame_size_48k(); - let payload_ms = (frame_48k / 48).max(10); - let frame_bytes = parsed.frames.len(); - let mut first = true; - for frame_data in &parsed.frames { - if frame_data.len() <= 1 { - first = false; - continue; - } - let mut dec = ec_opus::RangeDecoder::new(frame_data); - let _ = silk.decode( - &mut dec, - &mut silk_pcm, - payload_ms, - internal_rate, - 1, - first, - ); - let ix = silk.last_indices(); - let bytes = frame_data.len(); - results.push((ix, bytes)); - first = false; - } - let _ = frame_bytes; // suppress unused - } - results - } let ours_ix = collect_indices(&ours_packets); let ref_ix = collect_indices(&ref_packets); @@ -4021,6 +3965,372 @@ fn silk_silkq_oracle() { print!("{}", out_str); } +/// Decode a SILK stream packet-by-packet through a fresh `SilkDecoder`, +/// snapshotting each frame's coded indices — the oracle diagnostic's view of +/// what the bitstream actually coded (our encoder's packets and a reference +/// encoder's alike). +fn collect_indices(packets: &[Vec]) -> Vec<(ec_opus::SilkDecIndices, usize)> { + let mut silk = ec_opus::SilkDecoder::new(16000, 1); + let mut results = Vec::new(); + let mut silk_pcm = vec![0i16; 320]; // 20ms at 16kHz + for pkt in packets { + if pkt.len() <= 1 { + continue; // DTX / empty + } + let parsed = match ec_opus::Packet::parse(pkt, false) { + Ok(p) => p, + Err(_) => continue, + }; + let toc = parsed.toc; + let mode = toc.mode(); + if mode == ec_opus::Mode::Celt { + continue; + } + let internal_rate = if mode == ec_opus::Mode::Hybrid { + 16000 + } else { + match toc.bandwidth() { + ec_opus::Bandwidth::Narrow => 8000, + ec_opus::Bandwidth::Medium => 12000, + _ => 16000, + } + }; + let payload_ms = (toc.frame_size_48k() / 48).max(10); + let mut first = true; + for frame_data in &parsed.frames { + if frame_data.len() <= 1 { + first = false; + continue; + } + let mut dec = ec_opus::RangeDecoder::new(frame_data); + let _ = silk.decode( + &mut dec, + &mut silk_pcm, + payload_ms, + internal_rate, + 1, + first, + ); + let ix = silk.last_indices(); + let bytes = frame_data.len(); + results.push((ix, bytes)); + first = false; + } + } + results +} + +// --------------------------------------------------------------------------- +// SILK 12k spectral divergence: WHERE the 12 kbps Voip err_ratio outlier +// carries its error. Same machinery as spectral_divergence_vs_libopus, but +// mono/Application::Voip/12 kbps on reelm.wav, both sides decoded through +// ffmpeg libopus (gate symmetry), with the index oracle on the worst frames. +// Run: +// cargo test -p ec-opus --release --test conformance \ +// silk_spectral_divergence_12k -- --ignored --nocapture +// Writes lanes/opus-silkq-r2.bands.txt. +// --------------------------------------------------------------------------- + +#[test] +#[ignore] +fn silk_spectral_divergence_12k() { + const SECS: f64 = 120.0; + const FRAME: usize = 960; // 20 ms at 48 kHz + const CHANNELS: usize = 1; + const MAX_LAG: usize = 2000; + const KBPS: u32 = 12; + + let src = shellexpand("~/Music/reelm.wav"); + assert!(src.exists(), "source missing: {}", src.display()); + let source_pcm = ffmpeg_decode_pcm_mono(&src, SECS); + let source_frames = source_pcm.len() / CHANNELS; + assert!(source_frames > 48_000, "source too short"); + let seconds = source_frames as f64 / 48000.0; + + let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes"); + fs::create_dir_all(&lanes_dir).unwrap(); + let ref_ogg = temp_path("opus-silkq-r2-ref.opus"); + let ours_ogg = temp_path("opus-silkq-r2-ours.opus"); + + // Reference: ffmpeg libopus mono voip at 12k, decoded by ffmpeg libopus. + ffmpeg_encode_libopus_mono_voip(&src, KBPS, &ref_ogg, SECS); + let ref_bytes = fs::metadata(&ref_ogg).map(|m| m.len() as usize).unwrap_or(0); + let ref_kbps = ref_bytes as f64 * 8.0 / seconds / 1000.0; + let ref_dec = ffmpeg_decode(&ref_ogg, CHANNELS).expect("ffmpeg libopus decode of ref"); + let ref_trim: Vec = ref_dec.into_iter().take(source_frames * CHANNELS).collect(); + let (_, ref_aligned) = align_to_source(&source_pcm, &ref_trim, CHANNELS, MAX_LAG); + let ref_ogg_bytes = fs::read(&ref_ogg).expect("read ref ogg"); + let ref_packets: Vec> = ogg_packets(&ref_ogg_bytes) + .into_iter() + .skip(2) + .filter(|p| !p.is_empty()) + .collect(); + let _ = fs::remove_file(&ref_ogg); + + // Ours: ec-opus Voip at the reference's realised rate; our packets + // re-decoded through ffmpeg libopus so both sides cross the same decoder. + let mut enc = Encoder::new(48000, CHANNELS, Application::Voip).expect("encoder"); + enc.set_bitrate((ref_kbps * 1000.0).round() as u32); + enc.set_vbr_constrained(true); + let pre_skip = enc.look_ahead(FRAME) as i64; + let mut out = vec![0u8; 1500]; + let mut ours_packets: Vec> = Vec::new(); + let mut padded = Vec::new(); + for block in source_pcm.chunks(FRAME * CHANNELS) { + let block = if block.len() < FRAME * CHANNELS { + padded.clear(); + padded.extend_from_slice(block); + padded.resize(FRAME * CHANNELS, 0.0); + &padded[..] + } else { + block + }; + let len = enc.encode_float(block, FRAME, &mut out).expect("encode"); + ours_packets.push(out[..len].to_vec()); + } + write_ogg_opus( + &ours_ogg, + &ours_packets, + opus_head(CHANNELS, pre_skip as u16, None), + CHANNELS, + source_frames, + pre_skip, + ); + let ours_bytes: usize = ours_packets.iter().map(|p| p.len()).sum(); + let ours_kbps = ours_bytes as f64 * 8.0 / seconds / 1000.0; + let ours_dec = ffmpeg_decode(&ours_ogg, CHANNELS).expect("ffmpeg libopus decode of ours"); + let _ = fs::remove_file(&ours_ogg); + let ours_trim: Vec = ours_dec.into_iter().take(source_frames * CHANNELS).collect(); + let (_, ours_aligned) = align_to_source(&source_pcm, &ours_trim, CHANNELS, MAX_LAG); + + // opus_compare totals, per-band eb², per-frame ef² — both vs the source. + let cmp_frames = source_frames + .min(ours_aligned.len() / CHANNELS) + .min(ref_aligned.len() / CHANNELS); + let take = |v: &[f32]| -> Vec { to_i16(&v[..cmp_frames * CHANNELS]) }; + let s_i = take(&source_pcm); + let o_i = take(&ours_aligned); + let r_i = take(&ref_aligned); + let (err_o, eb2_o, ef2_o) = opus_compare_err_parts(&s_i, &o_i, CHANNELS); + let (err_r, eb2_r, ef2_r) = opus_compare_err_parts(&s_i, &r_i, CHANNELS); + let nframes = ef2_o.len(); + assert_eq!(nframes, ef2_r.len(), "frame counts differ"); + + // Raw (unmasked) band energies: spectral shape + dominant-band lookup. + let as_f64 = |v: &[i16]| -> Vec { v.iter().map(|&x| x as f64).collect() }; + let mut bands_s = Vec::new(); + let mut bands_o = Vec::new(); + let mut bands_r = Vec::new(); + let mut scratch_ps = Vec::new(); + band_energy(&as_f64(&s_i), CHANNELS, nframes, &mut Some(&mut bands_s), &mut scratch_ps); + band_energy(&as_f64(&o_i), CHANNELS, nframes, &mut Some(&mut bands_o), &mut scratch_ps); + band_energy(&as_f64(&r_i), CHANNELS, nframes, &mut Some(&mut bands_r), &mut scratch_ps); + + // Oracle indices both sides; FFT frame xi -> 20 ms packet index. + let ours_ix = collect_indices(&ours_packets); + let ref_ix = collect_indices(&ref_packets); + let pkt_of = |xi: usize| xi * WIN_STEP / (FRAME * CHANNELS); + + let mut report = String::new(); + report.push_str("# reelm@12k SILK per-band divergence: ours vs ffmpeg-libopus (r2)\n"); + report.push_str("# mono, 120s, 20ms frames, VBR constrained, 48kHz, Application::Voip\n"); + report.push_str(&format!( + "# rates: ours {ours_kbps:.2} kbps, ref {ref_kbps:.2} kbps; \ + err ours={err_o:.3} ref={err_r:.3} ratio={:.2}; nframes={nframes}\n", + err_o / err_r + )); + + // (1) 21-band table: which bands carry the error. + let sum_o: f64 = eb2_o.iter().sum(); + let sum_r: f64 = eb2_r.iter().sum(); + report.push_str( + "# 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\n", + ); + report.push_str("# band lo_hz hi_hz dln_o dln_r eb2_o eb2_r ratio share_o share_r\n"); + for bi in 0..NBANDS { + let (mut dol, mut drl) = (0.0f64, 0.0f64); + for xi in 0..nframes { + let sl = bands_s[(xi * NBANDS + bi) * CHANNELS].ln(); + dol += bands_o[(xi * NBANDS + bi) * CHANNELS].ln() - sl; + drl += bands_r[(xi * NBANDS + bi) * CHANNELS].ln() - sl; + } + dol /= nframes as f64; + drl /= nframes as f64; + let ratio = if eb2_r[bi] > 0.0 { eb2_o[bi] / eb2_r[bi] } else { f64::INFINITY }; + report.push_str(&format!( + "{:5} {:5} {:6} {:+.3} {:+.3} {:.3e} {:.3e} {:>7} {:5.1}% {:5.1}%\n", + bi, + CELT_EBANDS[bi] * 240, + CELT_EBANDS[bi + 1] * 240, + dol, + drl, + eb2_o[bi], + eb2_r[bi], + ratio, + 100.0 * eb2_o[bi] / sum_o, + 100.0 * eb2_r[bi] / sum_r, + )); + } + + // (2) top-15 frames by ef2(ours), with the dominant band and the oracle. + let mut by_ef2: Vec = (0..nframes).collect(); + by_ef2.sort_by(|&a, &b| ef2_o[b].partial_cmp(&ef2_o[a]).unwrap()); + report.push_str( + "\n# top-15 frames by ef2(ours) (ef2 = frame's squared opus_compare error); \ + dom = argmax_b (lnE_o - lnE_s)^2, raw bands\n", + ); + report.push_str( + "# 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", + ); + for &xi in by_ef2.iter().take(15) { + let pkt = pkt_of(xi); + let t = (xi * WIN_STEP) as f64 / 48000.0; + let (mut dom, mut dom_v) = (0usize, -1.0f64); + for bi in 0..NBANDS { + let sl = bands_s[(xi * NBANDS + bi) * CHANNELS].ln(); + let d = bands_o[(xi * NBANDS + bi) * CHANNELS].ln() - sl; + if d * d > dom_v { + dom_v = d * d; + dom = bi; + } + } + report.push_str(&format!( + "{:7.3} {:5} {:4} {:.3e} {:.3e} {:2} {}-{}Hz\n", + t, + xi, + pkt, + ef2_o[xi], + ef2_r[xi], + dom, + CELT_EBANDS[dom] * 240, + CELT_EBANDS[dom + 1] * 240 + )); + for (side, ixv) in [(" O", ours_ix.get(pkt)), (" R", ref_ix.get(pkt))] { + match ixv { + Some((ix, bytes)) => { + let voiced = ix.signal_type == 2; + let opt = |v: String, on: bool| if on { v } else { "-".to_owned() }; + report.push_str(&format!( + " {side} sig={} qoff={} gains=[{},{},{},{}] nlsf_i={} lag={} cont={} per={} ltp=[{},{},{},{}] ltpscl={} seed={} bytes={}\n", + ix.signal_type, + ix.quant_offset_type, + ix.gains[0], + ix.gains[1], + ix.gains[2], + ix.gains[3], + ix.nlsf_interp_coef_q2, + opt(format!("{}", ix.lag_index), voiced), + opt(format!("{}", ix.contour_index), voiced), + opt(format!("{}", ix.per_index), voiced), + opt(format!("{}", ix.ltp_index[0]), voiced), + opt(format!("{}", ix.ltp_index[1]), voiced), + opt(format!("{}", ix.ltp_index[2]), voiced), + opt(format!("{}", ix.ltp_index[3]), voiced), + opt(format!("{}", ix.ltp_scale_index), voiced), + ix.seed, + bytes + )); + } + None => report.push_str(&format!(" {side} -- missing --\n")), + } + } + } + + // (3) tail vs body: how much of the total error the worst frames carry, + // and whether the 11x ratio survives de-tailing (err = (Σef2/n)^(1/16)). + let tail = |ef2: &[f64]| -> (usize, f64, f64, usize, f64) { + let mut s = ef2.to_vec(); + s.sort_by(|a, b| b.partial_cmp(a).unwrap()); + let tot: f64 = s.iter().sum(); + let k = (s.len() / 100).max(1); + let top1: f64 = s[..k].iter().sum(); + let (mut half, mut acc) = (s.len(), 0.0); + for (i, v) in s.iter().enumerate() { + acc += v; + if acc >= 0.5 * tot { + half = i + 1; + break; + } + } + (k, top1 / tot, half as f64 / s.len() as f64, half, tot) + }; + let root16 = |tot: f64, n: usize| tot.powf(1.0 / 16.0) / (n as f64).powf(1.0 / 16.0); + let (k, sh1_o, halff_o, half_o, _tot_o) = tail(&ef2_o); + let (_, sh1_r, halff_r, half_r, _tot_r) = tail(&ef2_r); + report.push_str(&format!( + "# tail: top-1% ({k} frames) carry {:.2}% of ours' Σef2 vs {:.2}% of ref's; \ + 50% of ours' err sits in the worst {half_o} frames ({:.2}%) \ + vs ref's worst {half_r} ({:.2}%)\n", + 100.0 * sh1_o, 100.0 * sh1_r, 100.0 * halff_o, 100.0 * halff_r + )); + // De-tailed: drop each side's worst 50 frames (of 24k FFT frames/pkt-mapped). + let clip50 = |ef2: &[f64]| -> f64 { + let mut s = ef2.to_vec(); + s.sort_by(|a, b| b.partial_cmp(a).unwrap()); + s[50..].iter().sum() + }; + let err_o_clip = root16(clip50(&ef2_o), nframes); + let err_r_clip = root16(clip50(&ef2_r), nframes); + report.push_str(&format!( + "# de-tailed (worst 50 of {nframes} FFT frames dropped per side): \ + err ours={err_o_clip:.3} ref={err_r_clip:.3} ratio={:.2} (was {err_o:.3}/{err_r:.3}/{:.2})\n", + err_o_clip / err_r_clip, + err_o / err_r + )); + // Burst signature: per 20 ms packet, does first-subframe gain >= 60 or a + // starved packet (<= 20 bytes) line up with the packet's mean ef2? + let mut pkt_ef2 = vec![0.0f64; ours_packets.len()]; + let mut pkt_cnt = vec![0u32; ours_packets.len()]; + for xi in 0..nframes { + let p = pkt_of(xi).min(pkt_ef2.len() - 1); + pkt_ef2[p] += ef2_o[xi]; + pkt_cnt[p] += 1; + } + let mut g_hi = (0.0f64, 0usize); // (sum, count) packets with gains[0] >= 60 + let mut g_lo = (0.0f64, 0usize); + let mut b_lo = (0.0f64, 0usize); // bytes <= 20 + let mut b_hi = (0.0f64, 0usize); + for (i, (ix, bytes)) in ours_ix.iter().enumerate() { + if i >= pkt_ef2.len() { + break; + } + let mean = if pkt_cnt[i] > 0 { pkt_ef2[i] / pkt_cnt[i] as f64 } else { 0.0 }; + if ix.gains[0] >= 60 { + g_hi.0 += mean; + g_hi.1 += 1; + } else { + g_lo.0 += mean; + g_lo.1 += 1; + } + if *bytes <= 20 { + b_lo.0 += mean; + b_lo.1 += 1; + } else { + b_hi.0 += mean; + b_hi.1 += 1; + } + } + report.push_str(&format!( + "# burst signature: packets with gains[0]>=60: {}/{}, mean ef2 {:.3e}; \ + gains[0]<60: mean ef2 {:.3e} || packets <=20 bytes: {}/{}, mean ef2 {:.3e}; \ + >20 bytes: mean ef2 {:.3e}\n", + g_hi.1, + g_hi.1 + g_lo.1, + if g_hi.1 > 0 { g_hi.0 / g_hi.1 as f64 } else { f64::NAN }, + if g_lo.1 > 0 { g_lo.0 / g_lo.1 as f64 } else { f64::NAN }, + b_lo.1, + b_lo.1 + b_hi.1, + if b_lo.1 > 0 { b_lo.0 / b_lo.1 as f64 } else { f64::NAN }, + if b_hi.1 > 0 { b_hi.0 / b_hi.1 as f64 } else { f64::NAN }, + )); + + let out_path = lanes_dir.join("opus-silkq-r2.bands.txt"); + fs::write(&out_path, &report).unwrap(); + println!("wrote {}", out_path.display()); + print!("{}", report); +} + // --------------------------------------------------------------------------- // Harness: feed identical raw PCM (.sw, s16le interleaved) to our opus_compare // so it can be checked against the C `opus_compare` tool on the SAME bytes. diff --git a/lanes/opus-silkq-r2.bands.txt b/lanes/opus-silkq-r2.bands.txt new file mode 100644 index 00000000..56e30ad9 --- /dev/null +++ b/lanes/opus-silkq-r2.bands.txt @@ -0,0 +1,77 @@ +# reelm@12k SILK per-band divergence: ours vs ffmpeg-libopus (r2) +# mono, 120s, 20ms frames, VBR constrained, 48kHz, Application::Voip +# rates: ours 12.38 kbps, ref 11.98 kbps; err ours=27.421 ref=2.407 ratio=11.39; nframes=47997 +# 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 +# band lo_hz hi_hz dln_o dln_r eb2_o eb2_r ratio share_o share_r + 0 0 240 -0.404 -1.015 9.001e2 8.256e-2 10901.483587521187 8.1% 4.5% + 1 240 480 -0.555 -0.822 1.503e2 3.133e-2 4798.8320168752825 1.4% 1.7% + 2 480 720 -0.321 -0.719 2.740e2 3.078e-2 8903.992616391502 2.5% 1.7% + 3 720 960 -0.203 -0.727 1.979e1 6.301e-2 314.11641684740283 0.2% 3.4% + 4 960 1200 +0.086 -0.653 7.565e2 1.271e-1 5949.7190716845535 6.8% 6.9% + 5 1200 1440 -0.100 -0.722 2.464e2 6.096e-2 4041.469210559519 2.2% 3.3% + 6 1440 1680 -0.053 -0.669 6.312e1 6.228e-2 1013.5273671435615 0.6% 3.4% + 7 1680 1920 +0.090 -0.590 6.521e1 1.371e-1 475.68774048015473 0.6% 7.4% + 8 1920 2400 -0.061 -0.686 6.777e2 1.671e-1 4056.5361856691347 6.1% 9.0% + 9 2400 2880 -0.053 -0.685 3.503e3 7.006e-2 50002.415780327836 31.5% 3.8% + 10 2880 3360 -0.159 -0.713 1.983e3 1.380e-1 14374.382059534017 17.9% 7.5% + 11 3360 3840 -0.193 -0.711 1.443e3 1.083e-1 13319.398793662045 13.0% 5.9% + 12 3840 4800 -0.090 -0.640 7.548e2 1.246e-1 6057.742411724968 6.8% 6.7% + 13 4800 5760 -0.377 -0.734 1.358e2 1.598e-1 850.1916761988703 1.2% 8.6% + 14 5760 6720 -0.345 -0.685 5.009e1 7.785e-2 643.3793387719822 0.5% 4.2% + 15 6720 8160 -0.203 -0.594 3.432e1 7.665e-2 447.77759764424064 0.3% 4.1% + 16 8160 9600 +0.074 -0.366 5.147e1 3.265e-2 1576.4684713915938 0.5% 1.8% + 17 9600 11520 -1.565 -1.530 1.430e-1 1.325e-1 1.0791172707432997 0.0% 7.2% + 18 11520 14400 -1.910 -1.910 1.345e-1 1.226e-1 1.0965271211155312 0.0% 6.6% + 19 14400 18720 -1.103 -1.104 4.699e-2 4.612e-2 1.0188001325977802 0.0% 2.5% + 20 18720 24000 -0.046 -0.046 3.853e-5 3.768e-5 1.0225011087163844 0.0% 0.0% + +# top-15 frames by ef2(ours) (ef2 = frame's squared opus_compare error); dom = argmax_b (lnE_o - lnE_s)^2, raw bands +# 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 + 2.780 1112 139 4.051e27 4.494e-8 9 2400-2880Hz + 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 + 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 + 2.777 1111 138 5.487e26 5.770e-8 9 2400-2880Hz + 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 + 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 + 2.783 1113 139 2.959e26 8.289e-8 9 2400-2880Hz + 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 + 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 + 51.998 20799 2599 2.708e24 3.126e-9 8 1920-2400Hz + 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 + 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 + 52.005 20802 2600 2.003e24 1.938e-9 4 960-1200Hz + 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 + 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 + 52.000 20800 2600 1.715e24 5.741e-8 4 960-1200Hz + 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 + 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 + 52.002 20801 2600 5.132e23 2.970e-8 4 960-1200Hz + 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 + 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 + 52.015 20806 2600 1.163e23 3.251e-9 4 960-1200Hz + 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 + 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 + 2.775 1110 138 7.458e22 5.930e-8 9 2400-2880Hz + 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 + 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 + 52.008 20803 2600 2.201e22 1.857e-8 7 1680-1920Hz + 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 + 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 + 2.785 1114 139 8.835e21 4.607e-8 9 2400-2880Hz + 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 + 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 + 52.013 20805 2600 1.187e21 8.529e-7 4 960-1200Hz + 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 + 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.995 20798 2599 7.481e20 1.221e-9 5 1200-1440Hz + 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 + 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 + 52.010 20804 2600 3.759e20 3.219e-6 4 960-1200Hz + 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 + 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.943 20777 2597 2.894e20 2.264e-11 5 1200-1440Hz + 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 + R sig=1 qoff=0 gains=[29,3,1,4] nlsf_i=4 lag=- cont=- per=- ltp=[-,-,-,-] ltpscl=- seed=2 bytes=24 +# 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%) +# 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) +# 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 diff --git a/lanes/opus-silkq-r2.report.md b/lanes/opus-silkq-r2.report.md new file mode 100644 index 00000000..2f6bb6cc --- /dev/null +++ b/lanes/opus-silkq-r2.report.md @@ -0,0 +1,107 @@ +# opus-silkq r2 — where the 12 kbps SILK-WB error lives + +Setup: reelm.wav @ 12 kbps target, mono, `Application::Voip`, 120 s, 20 ms frames, +VBR-constrained, both sides decoded through ffmpeg libopus (gate symmetry). +Full tables: `lanes/opus-silkq-r2.bands.txt`. +Reproduce: `cargo test -p ec-opus --release --test conformance silk_spectral_divergence_12k -- --ignored --nocapture` + +Headline: err ours=27.421 ref=2.407 **ratio=11.39** (reproduces the gate's ~11×); +rates ours 12.38 kbps vs ref 11.98 kbps (±3%); nframes=47997. + +## Mechanism (named) + +**Burst-starved constrained-VBR frames + uniform body coarseness — not a +bandwidth/aliasing defect.** + +1. **Tail — bit-reservoir starvation bursts.** 141/6000 packets (2.35%) are + starved to ≤20 bytes (worst: 14–16 B on voiced frames libopus codes at 24–39 B). + On the worst 24 of these the first-subframe gain index codes **63 (max)** → + decoder emits a loud mid-band burst (dominant band 2.4–3.8 kHz) where the + source is quiet: ef2 up to 4.05e27 vs ref 4.5e-8 at the same instant. + These ~24 packets carry ~100% of ours' Σef2. Worst clusters: t≈2.78 s + (pkt 138/139: bytes 46→14, gains[0] 40→63 — spend-then-starve alternation) + and t≈51.94–52.02 s (pkts 2597–2600: 16 B, gains[0]=63). +2. **Body — uniform elevation.** Every band 0–16 (0–9.6 kHz) shows ours' mean + eb² at 314×–50 000× ref, tracking signal energy (bands 9–11 hold 62% of + the share simply because speech energy lives there). dln shows ours' mean + level *closer* to source than ref's (ref over-attenuates, dln ≈ −0.6…−1.0; + ours ≈ −0.05…−0.55) — so the error is noise/structure, not level. Consistent + with r1's systematic first-subframe gain-index offset (ours 33–47 vs ref 26–39): + over-coarse quantization everywhere, no single band mechanism. +3. **Not aliasing/bandwidth:** bands 17–20 (>9.6 kHz) ratio 1.02–1.10, ours ≈ ref + (both SILK-WB, nothing up there). Band 16 (8.16–9.6 kHz) ratio 1576× is + SILK's 8 kHz edge leakage, same order as body bands. + +## Table 1 — per-band mean eb² (ours/ref), dln = mean ln(E_test/E_src) + +| band | Hz | dln_o | dln_r | eb2_o | eb2_r | ratio | share_o | share_r | +|---|---|---|---|---|---|---|---|---| +| 0 | 0–240 | −0.404 | −1.015 | 9.0e2 | 8.3e−2 | 1.1e4 | 8.1% | 4.5% | +| 1 | 240–480 | −0.555 | −0.822 | 1.5e2 | 3.1e−2 | 4.8e3 | 1.4% | 1.7% | +| 2 | 480–720 | −0.321 | −0.719 | 2.7e2 | 3.1e−2 | 8.9e3 | 2.5% | 1.7% | +| 3 | 720–960 | −0.203 | −0.727 | 2.0e1 | 6.3e−2 | 3.1e2 | 0.2% | 3.4% | +| 4 | 960–1200 | +0.086 | −0.653 | 7.6e2 | 1.3e−1 | 5.9e3 | 6.8% | 6.9% | +| 5 | 1200–1440 | −0.100 | −0.722 | 2.5e2 | 6.1e−2 | 4.0e3 | 2.2% | 3.3% | +| 6 | 1440–1680 | −0.053 | −0.669 | 6.3e1 | 6.2e−2 | 1.0e3 | 0.6% | 3.4% | +| 7 | 1680–1920 | +0.090 | −0.590 | 6.5e1 | 1.4e−1 | 4.8e2 | 0.6% | 7.4% | +| 8 | 1920–2400 | −0.061 | −0.686 | 6.8e2 | 1.7e−1 | 4.1e3 | 6.1% | 9.0% | +| **9** | **2400–2880** | −0.053 | −0.685 | 3.5e3 | 7.0e−2 | **5.0e4** | **31.5%** | 3.8% | +| **10** | **2880–3360** | −0.159 | −0.713 | 2.0e3 | 1.4e−1 | **1.4e4** | **17.9%** | 7.5% | +| **11** | **3360–3840** | −0.193 | −0.711 | 1.4e3 | 1.1e−1 | **1.3e4** | **13.0%** | 5.9% | +| 12 | 3840–4800 | −0.090 | −0.640 | 7.5e2 | 1.2e−1 | 6.1e3 | 6.8% | 6.7% | +| 13 | 4800–5760 | −0.377 | −0.734 | 1.4e2 | 1.6e−1 | 8.5e2 | 1.2% | 8.6% | +| 14 | 5760–6720 | −0.345 | −0.685 | 5.0e1 | 7.8e−2 | 6.4e2 | 0.5% | 4.2% | +| 15 | 6720–8160 | −0.203 | −0.594 | 3.4e1 | 7.7e−2 | 4.5e2 | 0.3% | 4.1% | +| 16 | 8160–9600 | +0.074 | −0.366 | 5.1e1 | 3.3e−2 | 1.6e3 | 0.5% | 1.8% | +| 17 | 9600–11520 | −1.565 | −1.530 | 1.4e−1 | 1.3e−1 | **1.08** | 0.0% | 7.2% | +| 18 | 11520–14400 | −1.910 | −1.910 | 1.3e−1 | 1.2e−1 | **1.10** | 0.0% | 6.6% | +| 19 | 14400–18720 | −1.103 | −1.104 | 4.7e−2 | 4.6e−2 | **1.02** | 0.0% | 2.5% | +| 20 | 18720–24000 | −0.046 | −0.046 | 3.9e−5 | 3.8e−5 | **1.02** | 0.0% | 0.0% | + +## Table 2 — worst frames (top of 15; full table in bands.txt) + +| t(s) | pkt | ef2_ours | ef2_ref | dom band | ours: B, g0 | ref: B, g0 | +|---|---|---|---|---|---|---| +| 2.780 | 139 | 4.05e27 | 4.5e−8 | 2.4–2.9k | **14, 63** | 37, 23 | +| 2.777 | 138 | 5.49e26 | 5.8e−8 | 2.4–2.9k | 46, 40 | 39, 21 | +| 2.783 | 139 | 2.96e26 | 8.3e−8 | 2.4–2.9k | **14, 63** | 37, 23 | +| 51.998 | 2599 | 2.71e24 | 3.1e−9 | 1.9–2.4k | 44, 44 | 30, 26 | +| 52.005 | 2600 | 2.00e24 | 1.9e−9 | 1.0–1.2k | **16, 63** | 32, 24 | +| 52.000–52.015 | 2600 | 1.2e23–1.7e24 | ~e−9 | 1.0–1.9k | **16, 63** | 32, 24 | +| 51.943 | 2597 | 2.89e20 | 2.3e−11 | 1.2–1.4k | **16, 63** (lag 200) | 24, 29 | + +Oracle rows (sig/qoff/nlsf_i/lag/cont/per/ltp) in bands.txt — both sides code +these frames voiced (sig=2, qoff=0, nlsf_i=4); ours differs in bytes, g0, and +contour (ours cont=3 everywhere vs ref 0/1/5/12) — contour bookkeeping is a +secondary suspect, but bytes+g0 is the load-bearing difference. + +## Table 3 — tail vs body + +- top-1% (479 frames) carry 100.00% of Σef2 **both sides** (ef² is squared + twice before summing — the metric is tail-dominated by construction). +- de-tailed (worst 50 frames dropped per side): err ours=5.624 ref=0.812 → + **ratio 6.93** (was 11.39). Bursts alone explain roughly half the log-scale gap. +- burst signature: packets with gains[0]≥60: **24/6000**, mean ef2 2.27e25 + (vs 1.15e22 for the rest, ≈2000×); packets ≤20 bytes: **141/6000**, mean + ef2 3.86e24 (vs 1.18e22, ≈330×). +- [INFERENCE] If the ≤20 B starvation were eliminated (bursts → body quality): + ours' err ≈ 5.624 vs unclipped ref 2.407 → **ratio ≈ 2.3**, a −80% drop — + far past the gate KEEP threshold (−20%). + +## Fix decision + +**No encoder change this round.** The table names the mechanism but does not +identify a ≤10-line port: the candidate libopus logic (voiced-frame minimum-bit +floor / constrained-VBR reservoir clamp in silk VBR bit assignment) is not +present locally (only opus headers installed) and porting rate-control from +memory is off-limits. r3 pointer: fetch libopus `silk/VBR.c` + +`encode_frame` bit assignment, find the per-frame min/clamp, port behind +`const ON`, gate, SIGN RULE. Expected effect per the inference above. + +## Verification + +- `cargo check -p ec-opus --tests` — clean. +- `silk_spectral_divergence_12k` (release, --ignored): ok, 29.5 s, wrote + `lanes/opus-silkq-r2.bands.txt` (75 lines). +- `cargo test -p ec-opus --release`: **34 + 27 passed, 0 failed** (13 ignored + diagnostics, incl. the new one).