From 4a881f9248abb4ee3be76d3c877ff7fdf3a9cb96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 10:15:02 +0300 Subject: [PATCH] ec-opus test: judge hybrid FB lag against pinned libopus, not look_ahead Root cause: hybrid_fb_roundtrip asserted the speech correlation lag within look_ahead(960)=312 +/-6. Pinned ffmpeg 9.0.2 libopus (32 kbps, 20 ms, fullband, voip, CBR) decoded untrimmed (-flags2 +skip_manual) peaks at lag 298 on wav16-mono-48000 (corr 0.9994; lag 312 corr 0.9167), and 308 on the synthetic speech (corr 0.928). Our 298 therefore matches libopus; the constant was wrong. Lag is now asserted within +/-6 of the libopus lag measured by the same aligned_corr. Library code unchanged. --- crates/ec-opus/tests/conformance.rs | 71 ++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/crates/ec-opus/tests/conformance.rs b/crates/ec-opus/tests/conformance.rs index c1b5856f..d37f45ae 100644 --- a/crates/ec-opus/tests/conformance.rs +++ b/crates/ec-opus/tests/conformance.rs @@ -2125,6 +2125,71 @@ fn oracle_decode(name: &str, packets: &[Vec], samples: usize, pre_skip: usiz decoded } +/// Pinned libopus (ffmpeg) encode of mono `pcm` at the same 32 kbps, 20 ms, +/// fullband VoIP setting, decoded with the pre-skip left in: the output starts +/// at the encoder's input sample 0, so its lag is libopus's real delay. +fn libopus_untrimmed(name: &str, pcm: &[f32]) -> Vec { + let raw = temp_path(&format!("{name}.f32")); + let opus = temp_path(&format!("{name}.opus")); + let bytes: Vec = pcm.iter().flat_map(|x| x.to_le_bytes()).collect(); + fs::write(&raw, bytes).unwrap(); + let encode = ec_testkit::ffmpeg() + .args([ + "-v", "error", "-y", "-f", "f32le", "-ar", "48000", "-ac", "1", "-i", + ]) + .arg(&raw) + .args([ + "-c:a", + "libopus", + "-application", + "voip", + "-b:a", + "32000", + "-vbr", + "off", + "-frame_duration", + "20", + "-fflags", + "+bitexact", + ]) + .arg(&opus) + .output() + .unwrap(); + assert!( + encode.status.success(), + "{name}: ffmpeg encode: {}", + String::from_utf8_lossy(&encode.stderr) + ); + let decode = ec_testkit::ffmpeg() + .args([ + "-v", + "error", + "-flags2", + "+skip_manual", + "-c:a", + "libopus", + "-i", + ]) + .arg(&opus) + .args(["-f", "f32le", "-ar", "48000", "-ac", "1", "-"]) + .output() + .unwrap(); + assert!( + decode.status.success(), + "{name}: ffmpeg decode: {}", + String::from_utf8_lossy(&decode.stderr) + ); + let _ = fs::remove_file(&raw); + let _ = fs::remove_file(&opus); + decode + .stdout + .as_chunks::<4>() + .0 + .iter() + .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])) + .collect() +} + fn silk_sources() -> Vec<(&'static str, Vec)> { let wav = fixture("wav16-mono-48000.wav"); let mut sources = vec![("synthetic", speech_like())]; @@ -2257,9 +2322,11 @@ fn hybrid_fb_roundtrip() { ); assert!(corr >= 0.9, "{name}: full-band corr {corr:.4}"); let delay = hybrid_delay(32_000); + let libopus = libopus_untrimmed(&format!("hybrid-lag-{name}"), &pcm); + let (_, libopus_lag) = aligned_corr(&pcm, &libopus, 400); assert!( - (delay - 6..=delay + 6).contains(&lag), - "{name}: aligned at lag {lag}, not the advertised {delay}" + lag.abs_diff(libopus_lag) <= 6, + "{name}: aligned at lag {lag}, libopus at {libopus_lag}" ); assert!(lb_corr >= 0.9, "{name}: low-band corr {lb_corr:.4}"); assert!(