forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'simplify/c-opus'

Browse files at this commit

61e9e70bee623b1c3802b105efb527a63f11fa24Ahmet Kaan GümüşParent: 554f0fa08538a8
crates/ec-opus/tests/conformance.rsmodified+92 -9
@@ -2125,6 +2125,68 @@ fn oracle_decode(name: &str, packets: &[Vec<u8>], samples: usize, pre_skip: usiz
21252125 decoded
21262126 }
21272127
2128+fn libopus_untrimmed(name: &str, pcm: &[f32]) -> Vec<f32> {
2129+ let raw = temp_path(&format!("{name}.f32"));
2130+ let opus = temp_path(&format!("{name}.opus"));
2131+ let bytes: Vec<u8> = pcm.iter().flat_map(|x| x.to_le_bytes()).collect();
2132+ fs::write(&raw, bytes).unwrap();
2133+ let encode = ec_testkit::ffmpeg()
2134+ .args([
2135+ "-v", "error", "-y", "-f", "f32le", "-ar", "48000", "-ac", "1", "-i",
2136+ ])
2137+ .arg(&raw)
2138+ .args([
2139+ "-c:a",
2140+ "libopus",
2141+ "-application",
2142+ "voip",
2143+ "-b:a",
2144+ "32000",
2145+ "-vbr",
2146+ "off",
2147+ "-frame_duration",
2148+ "20",
2149+ "-fflags",
2150+ "+bitexact",
2151+ ])
2152+ .arg(&opus)
2153+ .output()
2154+ .unwrap();
2155+ assert!(
2156+ encode.status.success(),
2157+ "{name}: ffmpeg encode: {}",
2158+ String::from_utf8_lossy(&encode.stderr)
2159+ );
2160+ let decode = ec_testkit::ffmpeg()
2161+ .args([
2162+ "-v",
2163+ "error",
2164+ "-flags2",
2165+ "+skip_manual",
2166+ "-c:a",
2167+ "libopus",
2168+ "-i",
2169+ ])
2170+ .arg(&opus)
2171+ .args(["-f", "f32le", "-ar", "48000", "-ac", "1", "-"])
2172+ .output()
2173+ .unwrap();
2174+ assert!(
2175+ decode.status.success(),
2176+ "{name}: ffmpeg decode: {}",
2177+ String::from_utf8_lossy(&decode.stderr)
2178+ );
2179+ let _ = fs::remove_file(&raw);
2180+ let _ = fs::remove_file(&opus);
2181+ decode
2182+ .stdout
2183+ .as_chunks::<4>()
2184+ .0
2185+ .iter()
2186+ .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
2187+ .collect()
2188+}
2189+
21282190 fn silk_sources() -> Vec<(&'static str, Vec<f32>)> {
21292191 let wav = fixture("wav16-mono-48000.wav");
21302192 let mut sources = vec![("synthetic", speech_like())];
@@ -2163,11 +2225,13 @@ fn hybrid_roundtrip(
21632225 pcm: &[f32],
21642226 bps: u32,
21652227 bandwidth: Option<Bandwidth>,
2228+ mode: Option<Mode>,
21662229 mute: (bool, bool),
21672230 ) -> (Vec<f32>, Vec<Vec<u8>>) {
21682231 let mut enc = Encoder::new(48000, 1, Application::Voip).unwrap();
21692232 enc.set_bitrate(bps);
21702233 enc.set_bandwidth(bandwidth);
2234+ enc.set_mode(mode);
21712235 let mut dec = Decoder::new(48000, 1).unwrap();
21722236 dec.debug_mute_layers(mute.0, mute.1);
21732237 let mut out = vec![0u8; 1500];
@@ -2215,8 +2279,8 @@ fn hybrid_layers_align() {
22152279 .unwrap()
22162280 .0
22172281 };
2218- let (lb, _) = hybrid_roundtrip(&click, 32_000, None, (true, false));
2219- let (hb, _) = hybrid_roundtrip(&click, 32_000, None, (false, true));
2282+ let (lb, _) = hybrid_roundtrip(&click, 32_000, None, None, (true, false));
2283+ let (hb, _) = hybrid_roundtrip(&click, 32_000, None, None, (false, true));
22202284 let delay = hybrid_delay(32_000) as i64;
22212285 let (plb, phb) = (peak(&lb) as i64 - at as i64, peak(&hb) as i64 - at as i64);
22222286 eprintln!("hybrid click: LB peak +{plb}, HB peak +{phb}, look_ahead {delay}");
@@ -2243,11 +2307,23 @@ fn hybrid_layers_align() {
22432307 #[test]
22442308 fn hybrid_fb_roundtrip() {
22452309 for (name, pcm) in silk_sources() {
2246- let (decoded, packets) = hybrid_roundtrip(&pcm, 32_000, None, (false, false));
2310+ let (decoded, packets) = hybrid_roundtrip(&pcm, 32_000, None, None, (false, false));
22472311 let toc = ec_opus::Toc::new(packets[0][0]);
22482312 assert_eq!(toc.config, 15, "{name}: TOC config {}", toc.config);
22492313 let (corr, lag) = aligned_corr(&pcm, &decoded, 400);
2250- let (lb_only, _) = hybrid_roundtrip(&pcm, 32_000, None, (true, false));
2314+ let (lb_only, lb_packets) = hybrid_roundtrip(
2315+ &pcm,
2316+ 32_000,
2317+ Some(Bandwidth::Full),
2318+ Some(Mode::Hybrid),
2319+ (true, false),
2320+ );
2321+ assert!(
2322+ lb_packets
2323+ .iter()
2324+ .all(|p| ec_opus::Toc::new(p[0]).config == 15),
2325+ "{name}: forced-FB run left hybrid"
2326+ );
22512327 let lb_ref = lowpass(&pcm, 7000.0);
22522328 let (lb_corr, lb_lag) = aligned_corr(&lb_ref, &lb_only, 400);
22532329 let bytes: usize = packets.iter().map(Vec::len).sum();
@@ -2257,9 +2333,11 @@ fn hybrid_fb_roundtrip() {
22572333 );
22582334 assert!(corr >= 0.9, "{name}: full-band corr {corr:.4}");
22592335 let delay = hybrid_delay(32_000);
2336+ let libopus = libopus_untrimmed(&format!("hybrid-lag-{name}"), &pcm);
2337+ let (_, libopus_lag) = aligned_corr(&pcm, &libopus, 400);
22602338 assert!(
2261- (delay - 6..=delay + 6).contains(&lag),
2262- "{name}: aligned at lag {lag}, not the advertised {delay}"
2339+ lag.abs_diff(libopus_lag) <= 6,
2340+ "{name}: aligned at lag {lag}, libopus at {libopus_lag}"
22632341 );
22642342 assert!(lb_corr >= 0.9, "{name}: low-band corr {lb_corr:.4}");
22652343 assert!(
@@ -2275,11 +2353,16 @@ fn hybrid_fb_roundtrip() {
22752353 // SWB when asked for it, and the rest of the automatic hybrid range
22762354 // (SILK's share leaves CELT least at the bottom of it).
22772355 let speech = speech_like();
2278- let (_, packets) =
2279- hybrid_roundtrip(&speech, 24_000, Some(Bandwidth::SuperWide), (false, false));
2356+ let (_, packets) = hybrid_roundtrip(
2357+ &speech,
2358+ 24_000,
2359+ Some(Bandwidth::SuperWide),
2360+ None,
2361+ (false, false),
2362+ );
22802363 assert_eq!(ec_opus::Toc::new(packets[0][0]).config, 13);
22812364 for bps in [20_000u32, 24_000, 39_000] {
2282- let (decoded, packets) = hybrid_roundtrip(&speech, bps, None, (false, false));
2365+ let (decoded, packets) = hybrid_roundtrip(&speech, bps, None, None, (false, false));
22832366 let (corr, _) = aligned_corr(&speech, &decoded, 400);
22842367 let kbps = packets.iter().map(Vec::len).sum::<usize>() as f64 * 8.0
22852368 / (packets.len() as f64 * 20.0);