wip(opus-reell): r1 handoff at tool cap
Browse files at this commitfc48a1c12b5ef22eeb5a067b399aeb2aaf6253c9Parent: 5a6fd273 changed files +397 -3
- crates/ec-opus/tests/conformance.rs +333 -3
- lanes/opus-reell-r1.bands.txt +54 -0
- lanes/opus-reell-r1.handoff.md +10 -0
crates/ec-opus/tests/conformance.rsmodified+333 -3
| @@ -242,12 +242,27 @@ fn band_energy( | |||
| 242 | /// to take an `err_ratio` of ours-vs-reference before the monotonic quality | 242 | /// to take an `err_ratio` of ours-vs-reference before the monotonic quality |
| 243 | /// transform flattens the high-error tail. | 243 | /// transform flattens the high-error tail. |
| 244 | fn opus_compare_err(reference: &[i16], test: &[i16], channels: usize) -> f64 { | 244 | fn opus_compare_err(reference: &[i16], test: &[i16], channels: usize) -> f64 { |
| 245 | + opus_compare_err_parts(reference, test, channels).0 | ||
| 246 | +} | ||
| 247 | + | ||
| 248 | +/// `opus_compare_err` with the intermediates the per-band divergence | ||
| 249 | +/// diagnostic needs: `(err, per-band mean eb², per-frame ef²)`. The per-band | ||
| 250 | +/// mean is each band's additive share of the pre-squared frame error | ||
| 251 | +/// `ef = mean(eb²)`; the numerics are the wrapped function's, unchanged | ||
| 252 | +/// (guarded by `opus_compare_err_pinned_against_c`). | ||
| 253 | +fn opus_compare_err_parts( | ||
| 254 | + reference: &[i16], | ||
| 255 | + test: &[i16], | ||
| 256 | + channels: usize, | ||
| 257 | +) -> (f64, Vec<f64>, Vec<f64>) { | ||
| 245 | assert_eq!(reference.len(), test.len(), "sample counts must match"); | 258 | assert_eq!(reference.len(), test.len(), "sample counts must match"); |
| 246 | let x: Vec<f64> = reference.iter().map(|&v| v as f64).collect(); | 259 | let x: Vec<f64> = reference.iter().map(|&v| v as f64).collect(); |
| 247 | let y: Vec<f64> = test.iter().map(|&v| v as f64).collect(); | 260 | let y: Vec<f64> = test.iter().map(|&v| v as f64).collect(); |
| 248 | let xlength = x.len() / channels; | 261 | let xlength = x.len() / channels; |
| 249 | assert!(xlength >= WIN_SIZE, "not enough samples to compare"); | 262 | assert!(xlength >= WIN_SIZE, "not enough samples to compare"); |
| 250 | let nframes = (xlength - WIN_SIZE + WIN_STEP) / WIN_STEP; | 263 | let nframes = (xlength - WIN_SIZE + WIN_STEP) / WIN_STEP; |
| 264 | + let mut band_eb2 = vec![0.0f64; NBANDS]; | ||
| 265 | + let mut frame_ef2 = Vec::with_capacity(nframes); | ||
| 251 | 266 | ||
| 252 | let mut xb = Vec::new(); | 267 | let mut xb = Vec::new(); |
| 253 | let mut big_x = Vec::new(); | 268 | let mut big_x = Vec::new(); |
| @@ -339,14 +354,21 @@ fn opus_compare_err(reference: &[i16], test: &[i16], channels: usize) -> f64 { | |||
| 339 | } | 354 | } |
| 340 | } | 355 | } |
| 341 | eb /= ((BANDS[bi + 1] - BANDS[bi]) * channels) as f64; | 356 | eb /= ((BANDS[bi + 1] - BANDS[bi]) * channels) as f64; |
| 342 | - ef += eb * eb; | 357 | + let eb2 = eb * eb; |
| 358 | + band_eb2[bi] += eb2; | ||
| 359 | + ef += eb2; | ||
| 343 | } | 360 | } |
| 344 | ef /= NBANDS as f64; | 361 | ef /= NBANDS as f64; |
| 345 | ef *= ef; | 362 | ef *= ef; |
| 346 | - err += ef * ef; | 363 | + let ef2 = ef * ef; |
| 364 | + frame_ef2.push(ef2); | ||
| 365 | + err += ef2; | ||
| 347 | } | 366 | } |
| 348 | err = (err / nframes as f64).powf(1.0 / 16.0); | 367 | err = (err / nframes as f64).powf(1.0 / 16.0); |
| 349 | - err | 368 | + for b in &mut band_eb2 { |
| 369 | + *b /= nframes as f64; | ||
| 370 | + } | ||
| 371 | + (err, band_eb2, frame_ef2) | ||
| 350 | } | 372 | } |
| 351 | 373 | ||
| 352 | /// The `opus_compare` quality metric: >= 0 passes, 100 is identical output. | 374 | /// The `opus_compare` quality metric: >= 0 passes, 100 is identical output. |
| @@ -3176,3 +3198,311 @@ fn opus_compare_err_pinned_against_c() { | |||
| 3176 | "pinned Q drift: got {q:.4}, expected -593.31 (C-validated)" | 3198 | "pinned Q drift: got {q:.4}, expected -593.31 (C-validated)" |
| 3177 | ); | 3199 | ); |
| 3178 | } | 3200 | } |
| 3201 | + | ||
| 3202 | +// --------------------------------------------------------------------------- | ||
| 3203 | +// Spectral-divergence diagnostic: WHERE the 96 kbps err_ratio outlier loses | ||
| 3204 | +// its error. Per opus_compare band: mean ln-energy error ours-vs-source and | ||
| 3205 | +// ref-vs-source, plus the band's share of the frame error; per source: the | ||
| 3206 | +// top-5 worst seconds with what the encoder did there (CeltFrameDiag). | ||
| 3207 | +// Run: SWEEP_ONLY=reell,dl8a cargo test -p ec-opus --release \ | ||
| 3208 | +// spectral_divergence_vs_libopus -- --ignored --nocapture | ||
| 3209 | +// Writes lanes/opus-reell-r1.bands.txt. | ||
| 3210 | +// --------------------------------------------------------------------------- | ||
| 3211 | + | ||
| 3212 | +/// libopus CELT band edges (copy of `celt.rs::E_BANDS`, which is pub(crate)): | ||
| 3213 | +/// band `i` ends at `CELT_EBANDS[i] * 240` Hz at 48 kHz. | ||
| 3214 | +const CELT_EBANDS: [usize; NBANDS + 1] = [ | ||
| 3215 | + 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100, | ||
| 3216 | +]; | ||
| 3217 | + | ||
| 3218 | +#[test] | ||
| 3219 | +#[ignore] | ||
| 3220 | +fn spectral_divergence_vs_libopus() { | ||
| 3221 | + const SECS: f64 = 120.0; | ||
| 3222 | + const FRAME: usize = 960; | ||
| 3223 | + const CHANNELS: usize = 2; | ||
| 3224 | + const MAX_LAG: usize = 2000; | ||
| 3225 | + const KBPS: u32 = 96; | ||
| 3226 | + | ||
| 3227 | + let all: &[(&str, &str)] = &[ | ||
| 3228 | + ("reell", "~/Music/reell.mp4"), | ||
| 3229 | + ("dl8a", "~/Downloads/8a3b6d1d19.mp3"), | ||
| 3230 | + ]; | ||
| 3231 | + let only: Vec<String> = std::env::var("SWEEP_ONLY") | ||
| 3232 | + .unwrap_or_else(|_| "reell,dl8a".to_owned()) | ||
| 3233 | + .split(',') | ||
| 3234 | + .filter(|s| !s.is_empty()) | ||
| 3235 | + .map(|s| s.trim().to_owned()) | ||
| 3236 | + .collect(); | ||
| 3237 | + let sources: Vec<(String, PathBuf)> = all | ||
| 3238 | + .iter() | ||
| 3239 | + .filter(|(tag, _)| only.iter().any(|o| o == tag)) | ||
| 3240 | + .map(|(tag, p)| (tag.to_string(), shellexpand(p))) | ||
| 3241 | + .collect(); | ||
| 3242 | + assert!(!sources.is_empty(), "SWEEP_ONLY matched no sources"); | ||
| 3243 | + | ||
| 3244 | + let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes"); | ||
| 3245 | + fs::create_dir_all(&lanes_dir).unwrap(); | ||
| 3246 | + let out_path = lanes_dir.join("opus-reell-r1.bands.txt"); | ||
| 3247 | + let scratch = lanes_dir.join("opus-reell-r1.scratch.ogg"); | ||
| 3248 | + let mut report = String::new(); | ||
| 3249 | + | ||
| 3250 | + for (tag, src) in &sources { | ||
| 3251 | + if !src.exists() { | ||
| 3252 | + eprintln!("SKIP {tag}: missing {}", src.display()); | ||
| 3253 | + continue; | ||
| 3254 | + } | ||
| 3255 | + let source_pcm = ffmpeg_decode_pcm(src, SECS); | ||
| 3256 | + let source_frames = source_pcm.len() / CHANNELS; | ||
| 3257 | + let seconds = source_frames as f64 / 48000.0; | ||
| 3258 | + let source_i16 = to_i16(&source_pcm); | ||
| 3259 | + | ||
| 3260 | + // Reference: ffmpeg libopus at 96k, VBR, decoded by our decoder. | ||
| 3261 | + ffmpeg_encode_libopus(src, KBPS, &scratch, SECS); | ||
| 3262 | + let ref_bytes = fs::metadata(&scratch).map(|m| m.len() as usize).unwrap_or(0); | ||
| 3263 | + let ref_kbps = ref_bytes as f64 * 8.0 / seconds / 1000.0; | ||
| 3264 | + let (ref_dec, ref_ch) = decode_ogg(&scratch); | ||
| 3265 | + assert_eq!(ref_ch, CHANNELS, "{tag}: ref not stereo"); | ||
| 3266 | + let (_, ref_aligned) = align_to_source(&source_pcm, &ref_dec, CHANNELS, MAX_LAG); | ||
| 3267 | + | ||
| 3268 | + // Ours at the reference's realised rate, per-frame diag captured. | ||
| 3269 | + let mut enc = Encoder::new(48000, CHANNELS, Application::Audio).expect("encoder"); | ||
| 3270 | + enc.set_bitrate((ref_kbps * 1000.0).round() as u32); | ||
| 3271 | + enc.set_vbr_constrained(true); | ||
| 3272 | + let mut dec = Decoder::new(48000, CHANNELS).unwrap(); | ||
| 3273 | + let mut packet = vec![0u8; 1500]; | ||
| 3274 | + let mut dbuf = vec![0.0f32; 5760 * CHANNELS]; | ||
| 3275 | + let mut ours_dec = Vec::new(); | ||
| 3276 | + let mut diags = Vec::new(); | ||
| 3277 | + let mut ours_bytes = 0usize; | ||
| 3278 | + for block in source_pcm.chunks(FRAME * CHANNELS) { | ||
| 3279 | + let mut padded = block.to_vec(); | ||
| 3280 | + padded.resize(FRAME * CHANNELS, 0.0); | ||
| 3281 | + let len = enc.encode_float(&padded, FRAME, &mut packet).expect("encode"); | ||
| 3282 | + ours_bytes += len; | ||
| 3283 | + let n = dec.decode_float(&packet[..len], &mut dbuf).expect("decode"); | ||
| 3284 | + ours_dec.extend_from_slice(&dbuf[..n * CHANNELS]); | ||
| 3285 | + diags.push(enc.last_celt_diag().clone()); | ||
| 3286 | + } | ||
| 3287 | + let ours_kbps = ours_bytes as f64 * 8.0 / seconds / 1000.0; | ||
| 3288 | + let ours_trim: Vec<f32> = ours_dec.into_iter().take(source_frames * CHANNELS).collect(); | ||
| 3289 | + let (_, ours_aligned) = align_to_source(&source_pcm, &ours_trim, CHANNELS, MAX_LAG); | ||
| 3290 | + let ours_i16 = to_i16(&ours_aligned); | ||
| 3291 | + let ref_i16 = to_i16(&ref_aligned); | ||
| 3292 | + | ||
| 3293 | + let cmp_frames = (source_i16.len() / CHANNELS) | ||
| 3294 | + .min(ours_i16.len() / CHANNELS) | ||
| 3295 | + .min(ref_i16.len() / CHANNELS); | ||
| 3296 | + let trim = |v: &[i16]| -> Vec<i16> { v[..cmp_frames * CHANNELS].to_vec() }; | ||
| 3297 | + let s_i = trim(&source_i16); | ||
| 3298 | + let o_i = trim(&ours_i16); | ||
| 3299 | + let r_i = trim(&ref_i16); | ||
| 3300 | + | ||
| 3301 | + // Metric totals, per-band shares, per-frame errors. | ||
| 3302 | + let (err_o, eb2_o, ef2_o) = opus_compare_err_parts(&s_i, &o_i, CHANNELS); | ||
| 3303 | + let (err_r, eb2_r, ef2_r) = opus_compare_err_parts(&s_i, &r_i, CHANNELS); | ||
| 3304 | + let corr_o = corr_interleaved(&source_pcm, &ours_aligned, CHANNELS); | ||
| 3305 | + let corr_r = corr_interleaved(&source_pcm, &ref_aligned, CHANNELS); | ||
| 3306 | + | ||
| 3307 | + // Raw per-band energies (no masking): spectral shape in the ln domain. | ||
| 3308 | + let nframes = (cmp_frames - WIN_SIZE + WIN_STEP) / WIN_STEP; | ||
| 3309 | + let as_f64 = |v: &[i16]| -> Vec<f64> { v.iter().map(|&x| x as f64).collect() }; | ||
| 3310 | + let mut bands_s = Vec::new(); | ||
| 3311 | + let mut bands_o = Vec::new(); | ||
| 3312 | + let mut bands_r = Vec::new(); | ||
| 3313 | + let mut scratch_ps = Vec::new(); | ||
| 3314 | + band_energy(&as_f64(&s_i), CHANNELS, nframes, &mut Some(&mut bands_s), &mut scratch_ps); | ||
| 3315 | + band_energy(&as_f64(&o_i), CHANNELS, nframes, &mut Some(&mut bands_o), &mut scratch_ps); | ||
| 3316 | + band_energy(&as_f64(&r_i), CHANNELS, nframes, &mut Some(&mut bands_r), &mut scratch_ps); | ||
| 3317 | + | ||
| 3318 | + report.push_str(&format!( | ||
| 3319 | + "\n# source {tag} @{KBPS}k: ours {ours_kbps:.1} kbps, ref {ref_kbps:.1} kbps; \ | ||
| 3320 | + corr o={corr_o:.4} r={corr_r:.4}; err o={err_o:.3} r={err_r:.3} ratio={:.2}\n", | ||
| 3321 | + err_o / err_r | ||
| 3322 | + )); | ||
| 3323 | + report.push_str( | ||
| 3324 | + "# dln = mean ln(E_test/E_src) per channel (neg = energy missing); \ | ||
| 3325 | + err = mean |dln|; eb2 = band's mean share of the pre-squared frame error\n", | ||
| 3326 | + ); | ||
| 3327 | + report.push_str( | ||
| 3328 | + "# lo_hz\thi_hz\tdlnL_o\tdlnR_o\tdlnL_r\tdlnR_r\terrL_o\terrR_o\terrL_r\terrR_r\teb2_o\teb2_r\n", | ||
| 3329 | + ); | ||
| 3330 | + for bi in 0..NBANDS { | ||
| 3331 | + let (mut dol, mut dor, mut drl, mut drr) = (0.0f64, 0.0, 0.0, 0.0); | ||
| 3332 | + let (mut aol, mut aor, mut arl, mut arr) = (0.0f64, 0.0, 0.0, 0.0); | ||
| 3333 | + for xi in 0..nframes { | ||
| 3334 | + let e = |b: &Vec<f64>, ci: usize| b[(xi * NBANDS + bi) * CHANNELS + ci].ln(); | ||
| 3335 | + let (sl, sr) = (e(&bands_s, 0), e(&bands_s, 1)); | ||
| 3336 | + let (o_l, o_r) = (e(&bands_o, 0) - sl, e(&bands_o, 1) - sr); | ||
| 3337 | + let (r_l, r_r) = (e(&bands_r, 0) - sl, e(&bands_r, 1) - sr); | ||
| 3338 | + dol += o_l; | ||
| 3339 | + dor += o_r; | ||
| 3340 | + drl += r_l; | ||
| 3341 | + drr += r_r; | ||
| 3342 | + aol += o_l.abs(); | ||
| 3343 | + aor += o_r.abs(); | ||
| 3344 | + arl += r_l.abs(); | ||
| 3345 | + arr += r_r.abs(); | ||
| 3346 | + } | ||
| 3347 | + let n = nframes as f64; | ||
| 3348 | + report.push_str(&format!( | ||
| 3349 | + "{}\t{}\t{:+.3}\t{:+.3}\t{:+.3}\t{:+.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3e}\t{:.3e}\n", | ||
| 3350 | + BANDS[bi] * 100, | ||
| 3351 | + BANDS[bi + 1] * 100, | ||
| 3352 | + dol / n, | ||
| 3353 | + dor / n, | ||
| 3354 | + drl / n, | ||
| 3355 | + drr / n, | ||
| 3356 | + aol / n, | ||
| 3357 | + aor / n, | ||
| 3358 | + arl / n, | ||
| 3359 | + arr / n, | ||
| 3360 | + eb2_o[bi], | ||
| 3361 | + eb2_r[bi], | ||
| 3362 | + )); | ||
| 3363 | + } | ||
| 3364 | + | ||
| 3365 | + // Top-5 worst seconds by ours per-frame error, with encoder behavior. | ||
| 3366 | + let mframes_per_sec = 48000 / WIN_STEP; // 400 metric frames per second | ||
| 3367 | + let eframes_per_sec = 48000 / FRAME; // 50 encoder frames per second | ||
| 3368 | + let nsecs = cmp_frames / 48000; | ||
| 3369 | + let mut sec_score: Vec<(usize, f64)> = (0..nsecs) | ||
| 3370 | + .map(|s| { | ||
| 3371 | + let (mut sum, mut cnt) = (0.0f64, 0usize); | ||
| 3372 | + for xi in s * mframes_per_sec..(s + 1) * mframes_per_sec { | ||
| 3373 | + if xi < ef2_o.len() { | ||
| 3374 | + sum += ef2_o[xi]; | ||
| 3375 | + cnt += 1; | ||
| 3376 | + } | ||
| 3377 | + } | ||
| 3378 | + (s, sum / cnt.max(1) as f64) | ||
| 3379 | + }) | ||
| 3380 | + .collect(); | ||
| 3381 | + sec_score.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); | ||
| 3382 | + report.push_str("# top-5 seconds by ours frame error (mean ef²) with encoder diag:\n"); | ||
| 3383 | + report.push_str( | ||
| 3384 | + "# sec\tscore\ttrans\tcbb_min_hz\tcbb_mean_hz\tint_mean_hz\tdual\tintra\tbits/fr\n", | ||
| 3385 | + ); | ||
| 3386 | + for &(s, score) in &sec_score[..nsecs.min(5)] { | ||
| 3387 | + let f0 = s * eframes_per_sec; | ||
| 3388 | + let f1 = ((s + 1) * eframes_per_sec).min(diags.len()); | ||
| 3389 | + let fr = &diags[f0.min(f1)..f1]; | ||
| 3390 | + let dn = fr.len(); | ||
| 3391 | + if dn == 0 { | ||
| 3392 | + continue; | ||
| 3393 | + } | ||
| 3394 | + let transient = fr.iter().filter(|d| d.is_transient).count(); | ||
| 3395 | + let dual = fr.iter().filter(|d| d.dual_stereo).count(); | ||
| 3396 | + let intra = fr.iter().filter(|d| d.intra).count(); | ||
| 3397 | + let cbb_min = fr.iter().map(|d| d.coded_bands).min().unwrap_or(0); | ||
| 3398 | + let cbb_mean = | ||
| 3399 | + fr.iter().map(|d| d.coded_bands).sum::<usize>() as f64 / dn as f64; | ||
| 3400 | + let int_mean = fr.iter().map(|d| d.intensity).sum::<usize>() as f64 / dn as f64; | ||
| 3401 | + let bits = | ||
| 3402 | + fr.iter().map(|d| d.nb_compressed).sum::<usize>() as f64 * 8.0 / dn as f64; | ||
| 3403 | + report.push_str(&format!( | ||
| 3404 | + "{}\t{:.3e}\t{}/{}\t{}\t{}\t{}\t{}/{}\t{}/{}\t{:.0}\n", | ||
| 3405 | + s, | ||
| 3406 | + score, | ||
| 3407 | + transient, | ||
| 3408 | + dn, | ||
| 3409 | + CELT_EBANDS[cbb_min] * 240, | ||
| 3410 | + CELT_EBANDS[cbb_mean as usize] * 240, | ||
| 3411 | + CELT_EBANDS[int_mean as usize] * 240, | ||
| 3412 | + dual, | ||
| 3413 | + dn, | ||
| 3414 | + intra, | ||
| 3415 | + dn, | ||
| 3416 | + bits, | ||
| 3417 | + )); | ||
| 3418 | + } | ||
| 3419 | + | ||
| 3420 | + // Whole-file encoder summary for ours. | ||
| 3421 | + let dn = diags.len(); | ||
| 3422 | + if dn > 0 { | ||
| 3423 | + let transient = diags.iter().filter(|d| d.is_transient).count(); | ||
| 3424 | + let dual = diags.iter().filter(|d| d.dual_stereo).count(); | ||
| 3425 | + let intra = diags.iter().filter(|d| d.intra).count(); | ||
| 3426 | + let cbb_min = diags.iter().map(|d| d.coded_bands).min().unwrap_or(0); | ||
| 3427 | + let cbb_mean = | ||
| 3428 | + diags.iter().map(|d| d.coded_bands).sum::<usize>() as f64 / dn as f64; | ||
| 3429 | + let int_mean = diags.iter().map(|d| d.intensity).sum::<usize>() as f64 / dn as f64; | ||
| 3430 | + let bits = | ||
| 3431 | + diags.iter().map(|d| d.nb_compressed).sum::<usize>() as f64 * 8.0 / dn as f64; | ||
| 3432 | + report.push_str(&format!( | ||
| 3433 | + "# whole file: transient {}/{} dual {}/{} intra {}/{}; coded_bands min {} mean {} Hz; \ | ||
| 3434 | + intensity mean {} Hz; bits/frame {:.0}\n", | ||
| 3435 | + transient, | ||
| 3436 | + dn, | ||
| 3437 | + dual, | ||
| 3438 | + dn, | ||
| 3439 | + intra, | ||
| 3440 | + dn, | ||
| 3441 | + CELT_EBANDS[cbb_min] * 240, | ||
| 3442 | + CELT_EBANDS[cbb_mean as usize] * 240, | ||
| 3443 | + CELT_EBANDS[int_mean as usize] * 240, | ||
| 3444 | + bits, | ||
| 3445 | + )); | ||
| 3446 | + } | ||
| 3447 | + | ||
| 3448 | + // Top-8 worst single metric windows (ours vs ref), with the encoder | ||
| 3449 | + // frame each window lands in. | ||
| 3450 | + let mut byf: Vec<(usize, f64)> = ef2_o.iter().cloned().enumerate().collect(); | ||
| 3451 | + byf.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); | ||
| 3452 | + report.push_str("# top-8 metric windows by ours ef²: offset_ms ours ref | enc frame diag\n"); | ||
| 3453 | + for &(xi, v) in &byf[..byf.len().min(8)] { | ||
| 3454 | + let rv = if xi < ef2_r.len() { ef2_r[xi] } else { f64::NAN }; | ||
| 3455 | + let ms = xi * WIN_STEP * 1000 / 48000; | ||
| 3456 | + let eidx = xi * WIN_STEP / FRAME; | ||
| 3457 | + let d = diags.get(eidx); | ||
| 3458 | + let ds = d.map(|d| { | ||
| 3459 | + format!( | ||
| 3460 | + "{}:trans{},intra{},sil{},cbb{}Hz,int{}Hz,bt{}", | ||
| 3461 | + eidx, | ||
| 3462 | + d.is_transient as u8, | ||
| 3463 | + d.intra as u8, | ||
| 3464 | + d.silence as u8, | ||
| 3465 | + CELT_EBANDS[d.coded_bands] * 240, | ||
| 3466 | + CELT_EBANDS[d.intensity] * 240, | ||
| 3467 | + d.nb_compressed | ||
| 3468 | + ) | ||
| 3469 | + }); | ||
| 3470 | + report.push_str(&format!( | ||
| 3471 | + "+{}ms\t{:.3e}\t{:.3e}\t{}\n", | ||
| 3472 | + ms, | ||
| 3473 | + v, | ||
| 3474 | + rv, | ||
| 3475 | + ds.as_deref().unwrap_or("-") | ||
| 3476 | + )); | ||
| 3477 | + } | ||
| 3478 | + | ||
| 3479 | + // Startup behavior: first 10 encoder frames. | ||
| 3480 | + report.push_str( | ||
| 3481 | + "# first encoder frames: idx trans intra silence cbb_hz int_hz dual bytes\n", | ||
| 3482 | + ); | ||
| 3483 | + for (i, d) in diags.iter().take(10).enumerate() { | ||
| 3484 | + report.push_str(&format!( | ||
| 3485 | + "{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n", | ||
| 3486 | + i, | ||
| 3487 | + d.is_transient as u8, | ||
| 3488 | + d.intra as u8, | ||
| 3489 | + d.silence as u8, | ||
| 3490 | + CELT_EBANDS[d.coded_bands] * 240, | ||
| 3491 | + CELT_EBANDS[d.intensity] * 240, | ||
| 3492 | + d.dual_stereo as u8, | ||
| 3493 | + d.nb_compressed | ||
| 3494 | + )); | ||
| 3495 | + } | ||
| 3496 | + println!( | ||
| 3497 | + "{tag}@{KBPS}k: err o={err_o:.3} r={err_r:.3} ratio={:.2}", | ||
| 3498 | + err_o / err_r | ||
| 3499 | + ); | ||
| 3500 | + } | ||
| 3501 | + let _ = fs::remove_file(&scratch); | ||
| 3502 | + let header = format!( | ||
| 3503 | + "# spectral divergence vs libopus @ {KBPS}k, {SECS:.0}s cap, sources {}\n", | ||
| 3504 | + only.join(",") | ||
| 3505 | + ); | ||
| 3506 | + fs::write(&out_path, header + &report).unwrap(); | ||
| 3507 | + println!("wrote {}", out_path.display()); | ||
| 3508 | +} | ||
lanes/opus-reell-r1.bands.txtadded+54 -0
| @@ -0,0 +1,54 @@ | |||
| 1 | +# spectral divergence vs libopus @ 96k, 120s cap, sources reell | ||
| 2 | + | ||
| 3 | +# source reell @96k: ours 105.3 kbps, ref 105.4 kbps; corr o=0.9954 r=0.9948; err o=8.863 r=0.371 ratio=23.87 | ||
| 4 | +# dln = mean ln(E_test/E_src) per channel (neg = energy missing); err = mean |dln|; eb2 = band's mean share of the pre-squared frame error | ||
| 5 | +# lo_hz hi_hz dlnL_o dlnR_o dlnL_r dlnR_r errL_o errR_o errL_r errR_r eb2_o eb2_r | ||
| 6 | +0 200 +0.001 +0.002 +0.002 +0.006 0.104 0.106 0.131 0.132 2.697e-1 3.016e-4 | ||
| 7 | +200 400 +0.001 -0.000 -0.001 +0.000 0.096 0.095 0.095 0.094 1.692e0 7.242e-5 | ||
| 8 | +400 600 -0.001 -0.001 -0.002 +0.002 0.104 0.107 0.115 0.119 1.168e1 8.595e-5 | ||
| 9 | +600 800 +0.005 +0.003 +0.002 +0.002 0.139 0.135 0.132 0.131 1.086e-1 7.575e-5 | ||
| 10 | +800 1000 +0.001 -0.002 +0.002 +0.000 0.119 0.114 0.124 0.119 1.732e0 9.527e-5 | ||
| 11 | +1000 1200 +0.003 +0.001 -0.001 +0.004 0.137 0.135 0.138 0.138 4.836e1 1.625e-4 | ||
| 12 | +1200 1400 +0.009 +0.003 +0.003 +0.001 0.163 0.157 0.160 0.155 1.245e0 2.724e-4 | ||
| 13 | +1400 1600 +0.005 +0.001 +0.001 +0.001 0.162 0.156 0.164 0.158 1.197e0 4.705e-4 | ||
| 14 | +1600 2000 +0.000 +0.001 +0.000 +0.005 0.122 0.124 0.126 0.128 2.832e-1 4.896e-4 | ||
| 15 | +2000 2400 +0.001 -0.002 -0.001 +0.003 0.147 0.145 0.154 0.153 1.005e0 6.703e-4 | ||
| 16 | +2400 2800 +0.004 -0.002 +0.004 +0.000 0.160 0.158 0.168 0.164 4.746e-1 9.076e-4 | ||
| 17 | +2800 3200 +0.001 +0.004 +0.004 +0.005 0.171 0.173 0.173 0.174 1.560e-3 1.308e-3 | ||
| 18 | +3200 4000 +0.002 +0.002 +0.004 +0.003 0.125 0.125 0.127 0.128 1.465e-2 1.280e-3 | ||
| 19 | +4000 4800 +0.001 +0.002 +0.006 +0.004 0.130 0.131 0.133 0.133 5.610e-3 1.706e-3 | ||
| 20 | +4800 5600 +0.004 +0.004 +0.006 +0.005 0.148 0.148 0.150 0.151 1.300e-2 2.727e-3 | ||
| 21 | +5600 6800 +0.017 +0.016 +0.007 +0.009 0.159 0.159 0.156 0.158 1.051e-2 3.473e-3 | ||
| 22 | +6800 8000 +0.011 +0.011 +0.009 +0.010 0.161 0.161 0.162 0.162 1.624e-2 5.401e-3 | ||
| 23 | +8000 9600 +0.009 +0.011 +0.007 +0.008 0.133 0.133 0.132 0.133 2.408e-2 5.434e-3 | ||
| 24 | +9600 12000 +0.012 +0.012 +0.008 +0.007 0.117 0.117 0.119 0.118 4.770e-2 7.134e-3 | ||
| 25 | +12000 15600 +0.008 +0.010 +0.007 +0.010 0.143 0.143 0.151 0.152 1.605e-2 1.123e-2 | ||
| 26 | +15600 20000 +0.023 +0.025 +0.034 +0.038 0.107 0.110 0.118 0.121 4.185e-3 4.877e-3 | ||
| 27 | +# top-5 seconds by ours frame error (mean ef²) with encoder diag: | ||
| 28 | +# sec score trans cbb_min_hz cbb_mean_hz int_mean_hz dual intra bits/fr | ||
| 29 | +0 1.739e17 2/50 240 14400 11520 5/50 5/50 2058 | ||
| 30 | +103 5.456e2 3/50 18720 18720 14400 23/50 5/50 2115 | ||
| 31 | +72 3.090e2 0/50 18720 18720 14400 7/50 0/50 2079 | ||
| 32 | +22 3.360e1 1/50 18720 18720 14400 18/50 3/50 2086 | ||
| 33 | +11 2.080e1 2/50 18720 18720 14400 26/50 4/50 2112 | ||
| 34 | +# whole file: transient 99/6000 dual 913/6000 intra 201/6000; coded_bands min 240 mean 18720 Hz; intensity mean 11520 Hz; bits/frame 2099 | ||
| 35 | +# top-8 metric windows by ours ef²: offset_ms ours ref | enc frame diag | ||
| 36 | ++42ms 5.184e19 2.281e-4 2:trans1,intra1,sil0,cbb24000Hz,int14400Hz,bt446 | ||
| 37 | ++40ms 1.770e19 1.209e-22 2:trans1,intra1,sil0,cbb24000Hz,int14400Hz,bt446 | ||
| 38 | ++37ms 5.262e14 7.660e-76 1:trans0,intra0,sil1,cbb240Hz,int0Hz,bt2 | ||
| 39 | ++35ms 1.124e12 0.000e0 1:trans0,intra0,sil1,cbb240Hz,int0Hz,bt2 | ||
| 40 | ++32ms 2.854e6 0.000e0 1:trans0,intra0,sil1,cbb240Hz,int0Hz,bt2 | ||
| 41 | ++103455ms 1.945e5 6.637e-8 5172:trans0,intra0,sil0,cbb18720Hz,int14400Hz,bt246 | ||
| 42 | ++72980ms 9.745e4 1.563e-11 3649:trans0,intra0,sil0,cbb18720Hz,int14400Hz,bt257 | ||
| 43 | ++72977ms 2.525e4 4.626e-12 3648:trans0,intra0,sil0,cbb18720Hz,int14400Hz,bt255 | ||
| 44 | +# first encoder frames: idx trans intra silence cbb_hz int_hz dual bytes | ||
| 45 | +0 0 0 1 240 0 0 2 | ||
| 46 | +1 0 0 1 240 0 0 2 | ||
| 47 | +2 1 1 0 24000 14400 0 446 | ||
| 48 | +3 0 1 0 18720 14400 1 257 | ||
| 49 | +4 0 1 0 18720 14400 1 264 | ||
| 50 | +5 0 0 0 18720 14400 1 266 | ||
| 51 | +6 0 0 0 18720 14400 0 258 | ||
| 52 | +7 0 0 0 18720 14400 0 259 | ||
| 53 | +8 0 0 0 18720 14400 1 260 | ||
| 54 | +9 0 0 0 18720 14400 1 260 | ||
lanes/opus-reell-r1.handoff.mdadded+10 -0
| @@ -0,0 +1,10 @@ | |||
| 1 | +## Exact next steps | ||
| 2 | + | ||
| 3 | +1. Add one dump loop after the top-8-windows block (~line 3488, before `# first encoder frames`): print `bands_s/bands_o/bands_r` ln-energies for metric windows xi 12–20 (index `(xi*NBANDS+bi)*CHANNELS+ci`) — shows WHICH bands carry the 1e19 (HF noise-fill vs broadband). `cargo check` first; the edit tool dropped braces twice this lane. | ||
| 4 | +2. If ours >> source in specific bands at +40ms: inspect decoder energy reconstruction after `old_band_e=-28` (fine/coarse energy prediction on intra frame 2, noise fill, anti-collapse) in `crates/ec-opus/src/celt.rs`; if silence windows: our silence decode path isn't emitting exact zeros. | ||
| 5 | +3. One lever max; acceptance = reell err_ratio drops AND corr/dropout ≤.001 regression; gate: `SWEEP_ONLY=reell,dl8a,reelm cargo test -p ec-opus --release --test conformance encoder_library_gate_vs_libopus -- --ignored --nocapture`; `git checkout -- lanes/opus-gate-r1.sweep.txt` before commit. | ||
| 6 | +4. Write `lanes/opus-reell-r1.report.md` verdict-first (cause: four startup windows — silence-decode leak + first-attack-frame error after digital silence; bands: TBD from step 1; lever tried/none; err_ratio 23.87 before → after). | ||
| 7 | +5. Commit all on `lane-opus-reell`; no merge, no push. | ||
| 8 | + | ||
| 9 | +HANDOFF: diagnostic narrowed to 4 startup metric windows (digital-silence intro + first attack frame of reell); test instrumentation added & verified (top-8 windows, first-10 frames dumps in lanes/opus-reell-r1.bands.txt); remaining: per-window per-band energy dump → pick single lever (silence-decode leak or attack-frame energy reconstruction after old_band_e=-28) → gate → report → commit on lane-opus-reell. | ||
| 10 | +DELTA: none | ||