diff --git a/crates/ec-opus/tests/conformance.rs b/crates/ec-opus/tests/conformance.rs index c1b5856f..5765c1cb 100644 --- a/crates/ec-opus/tests/conformance.rs +++ b/crates/ec-opus/tests/conformance.rs @@ -2125,6 +2125,68 @@ fn oracle_decode(name: &str, packets: &[Vec], samples: usize, pre_skip: usiz decoded } +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())]; @@ -2163,11 +2225,13 @@ fn hybrid_roundtrip( pcm: &[f32], bps: u32, bandwidth: Option, + mode: Option, mute: (bool, bool), ) -> (Vec, Vec>) { let mut enc = Encoder::new(48000, 1, Application::Voip).unwrap(); enc.set_bitrate(bps); enc.set_bandwidth(bandwidth); + enc.set_mode(mode); let mut dec = Decoder::new(48000, 1).unwrap(); dec.debug_mute_layers(mute.0, mute.1); let mut out = vec![0u8; 1500]; @@ -2215,8 +2279,8 @@ fn hybrid_layers_align() { .unwrap() .0 }; - let (lb, _) = hybrid_roundtrip(&click, 32_000, None, (true, false)); - let (hb, _) = hybrid_roundtrip(&click, 32_000, None, (false, true)); + let (lb, _) = hybrid_roundtrip(&click, 32_000, None, None, (true, false)); + let (hb, _) = hybrid_roundtrip(&click, 32_000, None, None, (false, true)); let delay = hybrid_delay(32_000) as i64; let (plb, phb) = (peak(&lb) as i64 - at as i64, peak(&hb) as i64 - at as i64); eprintln!("hybrid click: LB peak +{plb}, HB peak +{phb}, look_ahead {delay}"); @@ -2243,11 +2307,23 @@ fn hybrid_layers_align() { #[test] fn hybrid_fb_roundtrip() { for (name, pcm) in silk_sources() { - let (decoded, packets) = hybrid_roundtrip(&pcm, 32_000, None, (false, false)); + let (decoded, packets) = hybrid_roundtrip(&pcm, 32_000, None, None, (false, false)); let toc = ec_opus::Toc::new(packets[0][0]); assert_eq!(toc.config, 15, "{name}: TOC config {}", toc.config); let (corr, lag) = aligned_corr(&pcm, &decoded, 400); - let (lb_only, _) = hybrid_roundtrip(&pcm, 32_000, None, (true, false)); + let (lb_only, lb_packets) = hybrid_roundtrip( + &pcm, + 32_000, + Some(Bandwidth::Full), + Some(Mode::Hybrid), + (true, false), + ); + assert!( + lb_packets + .iter() + .all(|p| ec_opus::Toc::new(p[0]).config == 15), + "{name}: forced-FB run left hybrid" + ); let lb_ref = lowpass(&pcm, 7000.0); let (lb_corr, lb_lag) = aligned_corr(&lb_ref, &lb_only, 400); let bytes: usize = packets.iter().map(Vec::len).sum(); @@ -2257,9 +2333,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!( @@ -2275,11 +2353,16 @@ fn hybrid_fb_roundtrip() { // SWB when asked for it, and the rest of the automatic hybrid range // (SILK's share leaves CELT least at the bottom of it). let speech = speech_like(); - let (_, packets) = - hybrid_roundtrip(&speech, 24_000, Some(Bandwidth::SuperWide), (false, false)); + let (_, packets) = hybrid_roundtrip( + &speech, + 24_000, + Some(Bandwidth::SuperWide), + None, + (false, false), + ); assert_eq!(ec_opus::Toc::new(packets[0][0]).config, 13); for bps in [20_000u32, 24_000, 39_000] { - let (decoded, packets) = hybrid_roundtrip(&speech, bps, None, (false, false)); + let (decoded, packets) = hybrid_roundtrip(&speech, bps, None, None, (false, false)); let (corr, _) = aligned_corr(&speech, &decoded, 400); let kbps = packets.iter().map(Vec::len).sum::() as f64 * 8.0 / (packets.len() as f64 * 20.0);