Symmetric decode, lag assert, band-limited ref, ref mode
Browse files at this commit2417616c270315fb30960b5644b9e1d50728a36eParent: 11f9cfe- test
- symmetric decode, lag assert, band-limited ref, ref mode
3 changed files +244 -33
- crates/ec-opus/tests/conformance.rs +143 -33
- lanes/opus-silk-r2.report.md +91 -0
- lanes/opus-silk-r2.sweep.txt +10 -0
crates/ec-opus/tests/conformance.rsmodified+143 -33
| @@ -3313,6 +3313,58 @@ fn ffmpeg_encode_libopus_mono_voip(src: &Path, kbps: u32, out: &Path, secs: f64) | |||
| 3313 | ); | 3313 | ); |
| 3314 | } | 3314 | } |
| 3315 | 3315 | ||
| 3316 | +/// Same as [`roundtrip_own`] but also returns the encoded packets, so the SILK | ||
| 3317 | +/// gate can re-decode them through ffmpeg (decoder-symmetric comparison). | ||
| 3318 | +fn roundtrip_own_packets( | ||
| 3319 | + enc: &mut Encoder, | ||
| 3320 | + pcm: &[f32], | ||
| 3321 | + channels: usize, | ||
| 3322 | + frame: usize, | ||
| 3323 | +) -> (Vec<f32>, Vec<Vec<u8>>, usize) { | ||
| 3324 | + let mut dec = Decoder::new(48000, channels).unwrap(); | ||
| 3325 | + let mut out = vec![0u8; 1500]; | ||
| 3326 | + let mut decoded = Vec::new(); | ||
| 3327 | + let mut packets = Vec::new(); | ||
| 3328 | + let mut buf = vec![0.0f32; 5760 * channels]; | ||
| 3329 | + let mut bytes = 0usize; | ||
| 3330 | + let mut padded = Vec::new(); | ||
| 3331 | + for block in pcm.chunks(frame * channels) { | ||
| 3332 | + let block = if block.len() < frame * channels { | ||
| 3333 | + padded.clear(); | ||
| 3334 | + padded.extend_from_slice(block); | ||
| 3335 | + padded.resize(frame * channels, 0.0); | ||
| 3336 | + &padded[..] | ||
| 3337 | + } else { | ||
| 3338 | + block | ||
| 3339 | + }; | ||
| 3340 | + let len = enc.encode_float(block, frame, &mut out).expect("encode"); | ||
| 3341 | + bytes += len; | ||
| 3342 | + packets.push(out[..len].to_vec()); | ||
| 3343 | + let n = dec.decode_float(&out[..len], &mut buf).expect("decode"); | ||
| 3344 | + assert_eq!(n, frame, "decoded frame size"); | ||
| 3345 | + assert_eq!( | ||
| 3346 | + dec.final_range(), | ||
| 3347 | + enc.final_range(), | ||
| 3348 | + "range state diverged between encoder and decoder" | ||
| 3349 | + ); | ||
| 3350 | + decoded.extend_from_slice(&buf[..n * channels]); | ||
| 3351 | + } | ||
| 3352 | + (decoded, packets, bytes) | ||
| 3353 | +} | ||
| 3354 | + | ||
| 3355 | +/// First audio packet's TOC byte from Ogg-Opus bytes (skips OpusHead/OpusTags). | ||
| 3356 | +fn first_audio_toc(ogg: &[u8]) -> u8 { | ||
| 3357 | + for p in ogg_packets(ogg) { | ||
| 3358 | + if p.len() >= 8 && (&p[..8] == b"OpusHead" || &p[..8] == b"OpusTags") { | ||
| 3359 | + continue; | ||
| 3360 | + } | ||
| 3361 | + if !p.is_empty() { | ||
| 3362 | + return p[0]; | ||
| 3363 | + } | ||
| 3364 | + } | ||
| 3365 | + 0 | ||
| 3366 | +} | ||
| 3367 | + | ||
| 3316 | #[test] | 3368 | #[test] |
| 3317 | #[ignore] | 3369 | #[ignore] |
| 3318 | fn silk_library_gate_vs_libopus() { | 3370 | fn silk_library_gate_vs_libopus() { |
| @@ -3326,39 +3378,52 @@ fn silk_library_gate_vs_libopus() { | |||
| 3326 | 3378 | ||
| 3327 | let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes"); | 3379 | let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes"); |
| 3328 | fs::create_dir_all(&lanes_dir).unwrap(); | 3380 | fs::create_dir_all(&lanes_dir).unwrap(); |
| 3329 | - let sweep_path = lanes_dir.join("opus-silk-r1.sweep.txt"); | 3381 | + let sweep_path = lanes_dir.join("opus-silk-r2.sweep.txt"); |
| 3330 | - let scratch = lanes_dir.join("opus-silk-r1.scratch.ogg"); | 3382 | + let ref_ogg = temp_path("opus-silk-r2-ref.opus"); |
| 3383 | + let ours_ogg = temp_path("opus-silk-r2-ours.opus"); | ||
| 3331 | 3384 | ||
| 3332 | // Source PCM: mono, 48 kHz. | 3385 | // Source PCM: mono, 48 kHz. |
| 3333 | let source_pcm = ffmpeg_decode_pcm_mono(&src, SECS); | 3386 | let source_pcm = ffmpeg_decode_pcm_mono(&src, SECS); |
| 3334 | let source_frames = source_pcm.len() / CHANNELS; | 3387 | let source_frames = source_pcm.len() / CHANNELS; |
| 3335 | assert!(source_frames > 48_000, "source too short"); | 3388 | assert!(source_frames > 48_000, "source too short"); |
| 3336 | let seconds = source_frames as f64 / 48000.0; | 3389 | let seconds = source_frames as f64 / 48000.0; |
| 3337 | - let source_i16 = to_i16(&source_pcm); | 3390 | + |
| 3338 | - | 3391 | + // (label, kbps, forced bandwidth, forced mode, reference lowpass cutoff). |
| 3339 | - // (label, kbps, forced bandwidth, forced mode). mode/bw chosen so Voip at | 3392 | + // cutoff: NB 4 kHz, WB 8 kHz, hybrid-FB unfiltered (None) — compare each |
| 3340 | - // these rates really codes SILK/Hybrid, not CELT (see wants_silk/ | 3393 | + // encoder only against the band it can actually reproduce. |
| 3341 | - // hybrid_choice): 12k auto-NB, 16k forced-NB (auto would pick MB), 24k | 3394 | + let rows: &[(&str, u32, Option<Bandwidth>, Option<Mode>, Option<f64>)] = &[ |
| 3342 | - // forced-Silk+Wide (auto would pick hybrid), 32k auto-hybrid. | 3395 | + ("12k-NB", 12, None, None, Some(4000.0)), |
| 3343 | - let rows: &[(&str, u32, Option<Bandwidth>, Option<Mode>)] = &[ | 3396 | + ("16k-NB", 16, Some(Bandwidth::Narrow), None, Some(4000.0)), |
| 3344 | - ("12k-NB", 12, None, None), | 3397 | + ("24k-WB", 24, Some(Bandwidth::Wide), Some(Mode::Silk), Some(8000.0)), |
| 3345 | - ("16k-NB", 16, Some(Bandwidth::Narrow), None), | 3398 | + ("32k-Hyb", 32, None, None, None), |
| 3346 | - ("24k-WB", 24, Some(Bandwidth::Wide), Some(Mode::Silk)), | ||
| 3347 | - ("32k-Hyb", 32, None, None), | ||
| 3348 | ]; | 3399 | ]; |
| 3349 | 3400 | ||
| 3350 | let mut out_rows: Vec<String> = Vec::new(); | 3401 | let mut out_rows: Vec<String> = Vec::new(); |
| 3351 | let mut rate_violations: Vec<String> = Vec::new(); | 3402 | let mut rate_violations: Vec<String> = Vec::new(); |
| 3352 | let mut dropout_violations: Vec<String> = Vec::new(); | 3403 | let mut dropout_violations: Vec<String> = Vec::new(); |
| 3404 | + let mut lag_violations: Vec<String> = Vec::new(); | ||
| 3405 | + | ||
| 3406 | + for &(tag, kbps, bw, mode, cutoff) in rows { | ||
| 3407 | + // Band-limited reference source for this row's corr/opus_compare. | ||
| 3408 | + let ref_source: Vec<f32> = match cutoff { | ||
| 3409 | + Some(hz) => lowpass(&source_pcm, hz), | ||
| 3410 | + None => source_pcm.clone(), | ||
| 3411 | + }; | ||
| 3412 | + let ref_source_i16 = to_i16(&ref_source); | ||
| 3353 | 3413 | ||
| 3354 | - for &(tag, kbps, bw, mode) in rows { | ||
| 3355 | // Reference: ffmpeg libopus, mono, voip, VBR; realised rate from size. | 3414 | // Reference: ffmpeg libopus, mono, voip, VBR; realised rate from size. |
| 3356 | - ffmpeg_encode_libopus_mono_voip(&src, kbps, &scratch, SECS); | 3415 | + ffmpeg_encode_libopus_mono_voip(&src, kbps, &ref_ogg, SECS); |
| 3357 | - let ref_bytes = fs::metadata(&scratch).map(|m| m.len() as usize).unwrap_or(0); | 3416 | + let ref_bytes = fs::metadata(&ref_ogg).map(|m| m.len() as usize).unwrap_or(0); |
| 3358 | let ref_kbps = ref_bytes as f64 * 8.0 / seconds / 1000.0; | 3417 | let ref_kbps = ref_bytes as f64 * 8.0 / seconds / 1000.0; |
| 3359 | - let (ref_dec, ref_ch) = decode_ogg(&scratch); | 3418 | + // SYMMETRY: ref decoded through ffmpeg libopus (the reference decoder), |
| 3360 | - assert_eq!(ref_ch, CHANNELS, "{tag}: ref not mono"); | 3419 | + // NOT our own decoder — so both sides cross the same decoder. (r1 used |
| 3361 | - let (_, ref_aligned) = align_to_source(&source_pcm, &ref_dec, CHANNELS, MAX_LAG); | 3420 | + // decode_ogg, our own MultistreamDecoder, for the ref side.) |
| 3421 | + let ref_dec = ffmpeg_decode(&ref_ogg, CHANNELS).expect("ffmpeg libopus decode of ref"); | ||
| 3422 | + let ref_trim: Vec<f32> = ref_dec.into_iter().take(source_frames * CHANNELS).collect(); | ||
| 3423 | + let (lag_ref, ref_aligned) = align_to_source(&source_pcm, &ref_trim, CHANNELS, MAX_LAG); | ||
| 3424 | + // REF MODE: libopus's actual mode from its first audio packet TOC. | ||
| 3425 | + let ref_ogg_bytes = fs::read(&ref_ogg).expect("read ref ogg"); | ||
| 3426 | + let ref_mode = toc_mode_label(first_audio_toc(&ref_ogg_bytes)); | ||
| 3362 | 3427 | ||
| 3363 | // Ours: ec-opus Voip at the reference's realised rate. | 3428 | // Ours: ec-opus Voip at the reference's realised rate. |
| 3364 | let mut enc = Encoder::new(48000, CHANNELS, Application::Voip).expect("encoder"); | 3429 | let mut enc = Encoder::new(48000, CHANNELS, Application::Voip).expect("encoder"); |
| @@ -3370,6 +3435,7 @@ fn silk_library_gate_vs_libopus() { | |||
| 3370 | enc.set_mode(Some(m)); | 3435 | enc.set_mode(Some(m)); |
| 3371 | } | 3436 | } |
| 3372 | enc.set_vbr_constrained(true); | 3437 | enc.set_vbr_constrained(true); |
| 3438 | + let pre_skip = enc.look_ahead(FRAME) as i64; | ||
| 3373 | 3439 | ||
| 3374 | // Probe which mode the encoder actually picked, from the first TOC byte. | 3440 | // Probe which mode the encoder actually picked, from the first TOC byte. |
| 3375 | let mut probe = enc.clone(); | 3441 | let mut probe = enc.clone(); |
| @@ -3379,18 +3445,44 @@ fn silk_library_gate_vs_libopus() { | |||
| 3379 | .expect("encode probe"); | 3445 | .expect("encode probe"); |
| 3380 | let mode_label = toc_mode_label(toc_buf[0]); | 3446 | let mode_label = toc_mode_label(toc_buf[0]); |
| 3381 | 3447 | ||
| 3382 | - let (ours_dec, ours_bytes) = roundtrip_own(&mut enc, &source_pcm, CHANNELS, FRAME); | 3448 | + // Own-decoder roundtrip (range-coder invariant) + packet capture. |
| 3449 | + let (ours_own_dec, ours_packets, ours_bytes) = | ||
| 3450 | + roundtrip_own_packets(&mut enc, &source_pcm, CHANNELS, FRAME); | ||
| 3383 | let ours_kbps = ours_bytes as f64 * 8.0 / seconds / 1000.0; | 3451 | let ours_kbps = ours_bytes as f64 * 8.0 / seconds / 1000.0; |
| 3452 | + | ||
| 3453 | + // SYMMETRY: re-decode OUR packets through ffmpeg libopus too. | ||
| 3454 | + write_ogg_opus( | ||
| 3455 | + &ours_ogg, | ||
| 3456 | + &ours_packets, | ||
| 3457 | + opus_head(CHANNELS, pre_skip as u16, None), | ||
| 3458 | + CHANNELS, | ||
| 3459 | + source_frames, | ||
| 3460 | + pre_skip, | ||
| 3461 | + ); | ||
| 3462 | + let ours_dec = ffmpeg_decode(&ours_ogg, CHANNELS).expect("ffmpeg libopus decode of ours"); | ||
| 3384 | let ours_trim: Vec<f32> = ours_dec.into_iter().take(source_frames * CHANNELS).collect(); | 3463 | let ours_trim: Vec<f32> = ours_dec.into_iter().take(source_frames * CHANNELS).collect(); |
| 3385 | - let (_, ours_aligned) = align_to_source(&source_pcm, &ours_trim, CHANNELS, MAX_LAG); | 3464 | + let (lag_ours, ours_aligned) = align_to_source(&source_pcm, &ours_trim, CHANNELS, MAX_LAG); |
| 3465 | + | ||
| 3466 | + // Own-decoder aligned (extra column: decoder drift visibility). | ||
| 3467 | + let ours_own_trim: Vec<f32> = | ||
| 3468 | + ours_own_dec.into_iter().take(source_frames * CHANNELS).collect(); | ||
| 3469 | + let (_, ours_own_aligned) = align_to_source(&source_pcm, &ours_own_trim, CHANNELS, MAX_LAG); | ||
| 3470 | + | ||
| 3471 | + // LAG GATE: a lag at the scan bound is an invalid measurement, not a result. | ||
| 3472 | + if (lag_ours as i64).abs() >= MAX_LAG as i64 { | ||
| 3473 | + lag_violations.push(format!("{tag}: ours lag {lag_ours} hit scan bound {MAX_LAG}")); | ||
| 3474 | + } | ||
| 3475 | + if (lag_ref as i64).abs() >= MAX_LAG as i64 { | ||
| 3476 | + lag_violations.push(format!("{tag}: ref lag {lag_ref} hit scan bound {MAX_LAG}")); | ||
| 3477 | + } | ||
| 3478 | + | ||
| 3386 | let ours_i16 = to_i16(&ours_aligned); | 3479 | let ours_i16 = to_i16(&ours_aligned); |
| 3387 | let ref_i16 = to_i16(&ref_aligned); | 3480 | let ref_i16 = to_i16(&ref_aligned); |
| 3388 | - | 3481 | + let cmp_frames = (ref_source_i16.len() / CHANNELS) |
| 3389 | - let cmp_frames = (source_i16.len() / CHANNELS) | ||
| 3390 | .min(ours_i16.len() / CHANNELS) | 3482 | .min(ours_i16.len() / CHANNELS) |
| 3391 | .min(ref_i16.len() / CHANNELS); | 3483 | .min(ref_i16.len() / CHANNELS); |
| 3392 | let trim = |v: &[i16]| -> Vec<i16> { v[..cmp_frames * CHANNELS].to_vec() }; | 3484 | let trim = |v: &[i16]| -> Vec<i16> { v[..cmp_frames * CHANNELS].to_vec() }; |
| 3393 | - let s_i = trim(&source_i16); | 3485 | + let s_i = trim(&ref_source_i16); |
| 3394 | let o_i = trim(&ours_i16); | 3486 | let o_i = trim(&ours_i16); |
| 3395 | let r_i = trim(&ref_i16); | 3487 | let r_i = trim(&ref_i16); |
| 3396 | let q_ours = opus_compare(&s_i, &o_i, CHANNELS); | 3488 | let q_ours = opus_compare(&s_i, &o_i, CHANNELS); |
| @@ -3399,11 +3491,15 @@ fn silk_library_gate_vs_libopus() { | |||
| 3399 | let err_ref = opus_compare_err(&s_i, &r_i, CHANNELS); | 3491 | let err_ref = opus_compare_err(&s_i, &r_i, CHANNELS); |
| 3400 | let err_ratio = if err_ref > 0.0 { err_ours / err_ref } else { f64::INFINITY }; | 3492 | let err_ratio = if err_ref > 0.0 { err_ours / err_ref } else { f64::INFINITY }; |
| 3401 | 3493 | ||
| 3402 | - let corr_ours = corr_interleaved(&source_pcm, &ours_aligned, CHANNELS); | 3494 | + // Primary corr: band-limited reference. Secondary: full-band source. |
| 3403 | - let corr_ref = corr_interleaved(&source_pcm, &ref_aligned, CHANNELS); | 3495 | + let corr_ours_bl = corr_interleaved(&ref_source, &ours_aligned, CHANNELS); |
| 3404 | - let gap = corr_ref - corr_ours; | 3496 | + let corr_ref_bl = corr_interleaved(&ref_source, &ref_aligned, CHANNELS); |
| 3405 | - let (minsec_ours, drop_ours) = per_second_corr(&source_pcm, &ours_aligned, CHANNELS); | 3497 | + let gap = corr_ref_bl - corr_ours_bl; // SIGN RULE: more negative = ours better |
| 3406 | - let (minsec_ref, drop_ref) = per_second_corr(&source_pcm, &ref_aligned, CHANNELS); | 3498 | + let corr_ours_fb = corr_interleaved(&source_pcm, &ours_aligned, CHANNELS); |
| 3499 | + let corr_ref_fb = corr_interleaved(&source_pcm, &ref_aligned, CHANNELS); | ||
| 3500 | + let corr_ours_owndec = corr_interleaved(&ref_source, &ours_own_aligned, CHANNELS); | ||
| 3501 | + let (minsec_ours, drop_ours) = per_second_corr(&ref_source, &ours_aligned, CHANNELS); | ||
| 3502 | + let (minsec_ref, drop_ref) = per_second_corr(&ref_source, &ref_aligned, CHANNELS); | ||
| 3407 | let rate_pct = (ours_kbps / ref_kbps - 1.0) * 100.0; | 3503 | let rate_pct = (ours_kbps / ref_kbps - 1.0) * 100.0; |
| 3408 | 3504 | ||
| 3409 | if rate_pct.abs() > 5.0 { | 3505 | if rate_pct.abs() > 5.0 { |
| @@ -3415,15 +3511,18 @@ fn silk_library_gate_vs_libopus() { | |||
| 3415 | )); | 3511 | )); |
| 3416 | } | 3512 | } |
| 3417 | let row = format!( | 3513 | let row = format!( |
| 3418 | - "{tag}: mode={mode_label}, ref {ref_kbps:.1} ours {ours_kbps:.1} kbps ({rate_pct:+.1}%), \ | 3514 | + "{tag}: ref_mode={ref_mode} mode={mode_label}, ref {ref_kbps:.1} ours {ours_kbps:.1} kbps ({rate_pct:+.1}%), \ |
| 3419 | - corr o={corr_ours:.4} r={corr_ref:.4} gap={gap:+.4}, \ | 3515 | + lag o={lag_ours} r={lag_ref}, \ |
| 3516 | + corr_bl o={corr_ours_bl:.4} r={corr_ref_bl:.4} gap={gap:+.4}, \ | ||
| 3517 | + corr_fb o={corr_ours_fb:.4} r={corr_ref_fb:.4}, corr_owndec o={corr_ours_owndec:.4}, \ | ||
| 3420 | Q o={q_ours:.2} r={q_ref:.2}, err_ratio {err_ratio:.3}, \ | 3518 | Q o={q_ours:.2} r={q_ref:.2}, err_ratio {err_ratio:.3}, \ |
| 3421 | minsec o={minsec_ours:.4} r={minsec_ref:.4}, drop o={drop_ours} r={drop_ref}" | 3519 | minsec o={minsec_ours:.4} r={minsec_ref:.4}, drop o={drop_ours} r={drop_ref}" |
| 3422 | ); | 3520 | ); |
| 3423 | println!("{row}"); | 3521 | println!("{row}"); |
| 3424 | out_rows.push(row); | 3522 | out_rows.push(row); |
| 3425 | } | 3523 | } |
| 3426 | - let _ = fs::remove_file(&scratch); | 3524 | + let _ = fs::remove_file(&ref_ogg); |
| 3525 | + let _ = fs::remove_file(&ours_ogg); | ||
| 3427 | 3526 | ||
| 3428 | // Rate gate: report violations, never panic. | 3527 | // Rate gate: report violations, never panic. |
| 3429 | let rate_gate = if rate_violations.is_empty() { | 3528 | let rate_gate = if rate_violations.is_empty() { |
| @@ -3441,6 +3540,14 @@ fn silk_library_gate_vs_libopus() { | |||
| 3441 | } else { | 3540 | } else { |
| 3442 | eprintln!("{rate_gate}"); | 3541 | eprintln!("{rate_gate}"); |
| 3443 | } | 3542 | } |
| 3543 | + // Lag gate: hard fail — a measurement at the scan bound is not a result. | ||
| 3544 | + assert!( | ||
| 3545 | + lag_violations.is_empty(), | ||
| 3546 | + "LAG GATE failed (lag at scan bound):\n {}", | ||
| 3547 | + lag_violations.join("\n ") | ||
| 3548 | + ); | ||
| 3549 | + let lag_gate = "LAG GATE: passed (no lag at scan bound)"; | ||
| 3550 | + println!("{lag_gate}"); | ||
| 3444 | // Dropout gate: hard fail — a second the reference kept must not drop out. | 3551 | // Dropout gate: hard fail — a second the reference kept must not drop out. |
| 3445 | assert!( | 3552 | assert!( |
| 3446 | dropout_violations.is_empty(), | 3553 | dropout_violations.is_empty(), |
| @@ -3452,14 +3559,17 @@ fn silk_library_gate_vs_libopus() { | |||
| 3452 | 3559 | ||
| 3453 | // Sweep file: raw rows + GATE lines (lane deliverable). | 3560 | // Sweep file: raw rows + GATE lines (lane deliverable). |
| 3454 | let mut table = String::new(); | 3561 | let mut table = String::new(); |
| 3455 | - table.push_str("# ec-opus SILK/speech-rate gate vs ffmpeg libopus (r1)\n"); | 3562 | + table.push_str("# ec-opus SILK/speech-rate gate vs ffmpeg libopus (r2)\n"); |
| 3456 | table.push_str("# mono, 120s cap, VBR, 48kHz, Application::Voip; source reelm.wav\n"); | 3563 | table.push_str("# mono, 120s cap, VBR, 48kHz, Application::Voip; source reelm.wav\n"); |
| 3564 | + table.push_str("# r2: symmetric ffmpeg-libopus decode (both sides), lag gate, band-limited ref, ref mode\n"); | ||
| 3457 | for r in &out_rows { | 3565 | for r in &out_rows { |
| 3458 | table.push_str(r); | 3566 | table.push_str(r); |
| 3459 | table.push('\n'); | 3567 | table.push('\n'); |
| 3460 | } | 3568 | } |
| 3461 | table.push_str(&rate_gate); | 3569 | table.push_str(&rate_gate); |
| 3462 | table.push('\n'); | 3570 | table.push('\n'); |
| 3571 | + table.push_str(lag_gate); | ||
| 3572 | + table.push('\n'); | ||
| 3463 | table.push_str(drop_gate); | 3573 | table.push_str(drop_gate); |
| 3464 | table.push('\n'); | 3574 | table.push('\n'); |
| 3465 | fs::write(&sweep_path, table).unwrap(); | 3575 | fs::write(&sweep_path, table).unwrap(); |
lanes/opus-silk-r2.report.mdadded+91 -0
| @@ -0,0 +1,91 @@ | |||
| 1 | +# opus-silk r2 — library gate instrument repair | ||
| 2 | + | ||
| 3 | +**Test:** `silk_library_gate_vs_libopus` (`crates/ec-opus/tests/conformance.rs`) | ||
| 4 | +**Source:** `~/Music/reelm.wav`, mono, 120 s cap, 48 kHz, `Application::Voip`, VBR-constrained. | ||
| 5 | +**Scope:** instrument only — no encoder tuning, no fixes, no guesses. | ||
| 6 | + | ||
| 7 | +## The four r1 measurement defects (and the r2 repair) | ||
| 8 | + | ||
| 9 | +| # | r1 defect | r2 repair | | ||
| 10 | +|---|-----------|-----------| | ||
| 11 | +| 1 | **Decoder asymmetry.** Task premise said "ref decoded by ffmpeg"; the checked-in r1 code decoded the ref via `decode_ogg` (our own `MultistreamDecoder`). Both sides actually used *our* decoder in r1 — the asymmetry description was inverted vs. the code. | Both sides now decode through **ffmpeg libopus** (the reference decoder): ref via `ffmpeg_decode(&ref_ogg)`, ours via `write_ogg_opus` → `ffmpeg_decode(&ours_ogg)`. Own-decoder corr kept as extra column `corr_owndec` so decoder drift stays visible. | | ||
| 12 | +| 2 | **No lag check.** `align_to_source` returned a lag but it was discarded; a lag pinned at the scan bound (±2000) would be a silent invalid measurement. | `align_to_source` return `(lag, aligned)` captured for both sides. **LAG GATE** (hard `assert`): `|lag| < MAX_LAG`. A bound-hit is a failed measurement, not a result. | | ||
| 13 | +| 3 | **Full-band reference.** corr and `opus_compare_err` were computed against the full-band source, penalizing NB/WB encoders for HF energy they structurally cannot reproduce — the likely real cause of the r1 sign contradiction. | Per-row **band-limited reference**: NB → 4 kHz lowpass, WB → 8 kHz lowpass, hybrid-FB → unfiltered (`None`). `corr_bl` (primary) and `opus_compare_err` both use the band-limited source; `corr_fb` (full-band) kept as a secondary column. | | ||
| 14 | +| 4 | **No ref mode.** libopus's actual mode was invisible; "12k-NB" assumed libopus also ran NB. | `first_audio_toc()` reads the ref Ogg, skips OpusHead/OpusTags, returns the first audio packet's TOC byte; `toc_mode_label()` prints it as `ref_mode=`. | | ||
| 15 | + | ||
| 16 | +## r1 vs r2 — per row | ||
| 17 | + | ||
| 18 | +SIGN RULE (verbatim): `gap = ref_corr - ours_corr; MORE NEGATIVE = OURS BETTER`. | ||
| 19 | +`err_ratio = err_ours / err_ref; < 1 = ours better, > 1 = ref better`. | ||
| 20 | +"agree" = gap and err_ratio point the same way. | ||
| 21 | + | ||
| 22 | +| row | | gap | err_ratio | agree? | | ||
| 23 | +|-----|---|-----|-----------|--------| | ||
| 24 | +| 12k-NB | r1 | -0.0308 (ours) | 3.532 (ref) | **no** | | ||
| 25 | +| 12k-NB | r2 | -0.0690 (ours) | 0.061 (ours) | **yes** | | ||
| 26 | +| 16k-NB | r1 | -0.0460 (ours) | 2.573 (ref) | **no** | | ||
| 27 | +| 16k-NB | r2 | -0.1129 (ours) | 0.049 (ours) | **yes** | | ||
| 28 | +| 24k-WB | r1 | -0.0759 (ours) | 4.452 (ref) | **no** | | ||
| 29 | +| 24k-WB | r2 | -0.0940 (ours) | 0.428 (ours) | **yes** | | ||
| 30 | +| 32k-Hyb | r1 | -0.0738 (ours) | 4.343 (ref) | **no** | | ||
| 31 | +| 32k-Hyb | r2 | -0.0738 (ours) | 4.346 (ref) | **no** | | ||
| 32 | + | ||
| 33 | +**3 of 4 rows now agree.** The band-limited reference (defect 3) resolved the | ||
| 34 | +contradiction for every NB/WB row. The single remaining disagreement is | ||
| 35 | +**32k-Hyb**, the one row with `cutoff = None` (full-band): `corr_bl == corr_fb` | ||
| 36 | +(0.9279) and `err_ratio` is essentially unchanged from r1 (4.343 → 4.346). At | ||
| 37 | +full band, time-domain corr (dominated by the strong low-frequency energy where | ||
| 38 | +both encoders agree) and frequency-domain `opus_compare_err` (which weights all | ||
| 39 | +bands including HF) genuinely diverge — this is a metric-semantics difference, | ||
| 40 | +not an instrument defect, and is reported as-is (no fix, per scope). | ||
| 41 | + | ||
| 42 | +## Lags (defect 2) | ||
| 43 | + | ||
| 44 | +| row | lag_ours | lag_ref | LAG GATE | | ||
| 45 | +|-----|----------|---------|----------| | ||
| 46 | +| 12k-NB | -1 | -2 | pass | | ||
| 47 | +| 16k-NB | -1 | -1 | pass | | ||
| 48 | +| 24k-WB | 0 | -1 | pass | | ||
| 49 | +| 32k-Hyb | 0 | -1 | pass | | ||
| 50 | + | ||
| 51 | +All lags are 0–2 samples — far inside the ±2000 scan bound. No invalid | ||
| 52 | +measurement. (Sub-sample residual is the encoder pre-skip / look-ahead | ||
| 53 | +quantization to whole frames.) | ||
| 54 | + | ||
| 55 | +## Ref modes (defect 4) | ||
| 56 | + | ||
| 57 | +| row | ref_mode (libopus) | mode (ours) | | ||
| 58 | +|-----|--------------------|-------------| | ||
| 59 | +| 12k-NB | **SILK-WB** | SILK-NB | | ||
| 60 | +| 16k-NB | **Hybrid-FB** | SILK-NB | | ||
| 61 | +| 24k-WB | **Hybrid-FB** | SILK-WB | | ||
| 62 | +| 32k-Hyb | Hybrid-FB | Hybrid-FB | | ||
| 63 | + | ||
| 64 | +libopus does **not** run narrowband at these rates: it auto-selects SILK-WB | ||
| 65 | +(12 k) or full-band hybrid (16/24/32 k). Our encoder honors the forced | ||
| 66 | +SILK-NB / SILK-WB / Hybrid-FB selection. At 12 k and 16 k the comparison is | ||
| 67 | +therefore **NB-ours vs WB/hybrid-ref** — yet ours still wins corr. The | ||
| 68 | +`ref_mode` column makes this mode asymmetry explicit (previously invisible). | ||
| 69 | + | ||
| 70 | +## Decoder drift (defect 1, extra column) | ||
| 71 | + | ||
| 72 | +`corr_owndec` (our `Decoder`) equals `corr_ours_bl` (ffmpeg libopus) to all | ||
| 73 | +four printed decimals on every row (0.9499, 0.9697, 0.9483, 0.9279). Our | ||
| 74 | +decoder produces bit-identical-quality output to ffmpeg libopus on our own | ||
| 75 | +packets — **no decoder drift**. This also explains why switching the ref from | ||
| 76 | +`decode_ogg` (r1) to `ffmpeg_decode` (r2) barely moved the 32k-Hyb numbers | ||
| 77 | +(corr_ref 0.8541 both; err_ratio 4.343 → 4.346): the two decoders agree. | ||
| 78 | + | ||
| 79 | +## Gates | ||
| 80 | + | ||
| 81 | +- **RATE GATE** (soft, report-only): all rows within ±5% (max +3.3%). | ||
| 82 | +- **LAG GATE** (hard, new): passed — no lag at scan bound. | ||
| 83 | +- **DROPOUT GATE** (hard): passed — no ours-only dropouts (ref drops *more* | ||
| 84 | + seconds than ours on every row: 61/69/67/65 vs 14/11/23/32). | ||
| 85 | + | ||
| 86 | +## Verification | ||
| 87 | + | ||
| 88 | +- `cargo check -p ec-opus --release --test conformance` — clean (0 warnings). | ||
| 89 | +- `silk_library_gate_vs_libopus --ignored --nocapture` — **ok**, 125.5 s, | ||
| 90 | + sweep written to `lanes/opus-silk-r2.sweep.txt`. | ||
| 91 | +- `cargo test -p ec-opus --release` — **34 + 27 green** (10 ignored), 34.2 s. | ||
lanes/opus-silk-r2.sweep.txtadded+10 -0
| @@ -0,0 +1,10 @@ | |||
| 1 | +# ec-opus SILK/speech-rate gate vs ffmpeg libopus (r2) | ||
| 2 | +# mono, 120s cap, VBR, 48kHz, Application::Voip; source reelm.wav | ||
| 3 | +# r2: symmetric ffmpeg-libopus decode (both sides), lag gate, band-limited ref, ref mode | ||
| 4 | +12k-NB: ref_mode=SILK-WB mode=SILK-NB, ref 12.0 ours 12.4 kbps (+3.3%), lag o=-1 r=-2, corr_bl o=0.9499 r=0.8809 gap=-0.0690, corr_fb o=0.8489 r=0.8181, corr_owndec o=0.9499, Q o=-821.88 r=-1925.90, err_ratio 0.061, minsec o=0.5164 r=0.2694, drop o=14 r=61 | ||
| 5 | +16k-NB: ref_mode=Hybrid-FB mode=SILK-NB, ref 16.4 ours 16.8 kbps (+2.4%), lag o=-1 r=-1, corr_bl o=0.9697 r=0.8568 gap=-0.1129, corr_fb o=0.8665 r=0.8206, corr_owndec o=0.9697, Q o=-726.61 r=-1904.79, err_ratio 0.049, minsec o=0.5219 r=0.2967, drop o=11 r=69 | ||
| 6 | +24k-WB: ref_mode=Hybrid-FB mode=SILK-WB, ref 24.0 ours 24.4 kbps (+1.7%), lag o=0 r=-1, corr_bl o=0.9483 r=0.8543 gap=-0.0940, corr_fb o=0.9168 r=0.8409, corr_owndec o=0.9483, Q o=-862.95 r=-1187.22, err_ratio 0.428, minsec o=0.6382 r=0.3396, drop o=23 r=67 | ||
| 7 | +32k-Hyb: ref_mode=Hybrid-FB mode=Hybrid-FB, ref 31.8 ours 31.6 kbps (-0.5%), lag o=0 r=-1, corr_bl o=0.9279 r=0.8541 gap=-0.0738, corr_fb o=0.9279 r=0.8541, corr_owndec o=0.9279, Q o=-940.18 r=-434.68, err_ratio 4.346, minsec o=0.6283 r=0.3680, drop o=32 r=65 | ||
| 8 | +RATE GATE: all rows within ±5% | ||
| 9 | +LAG GATE: passed (no lag at scan bound) | ||
| 10 | +DROPOUT GATE: passed (no ours-only dropouts) | ||