Merge branch 'simplify/c-opus'
Browse files at this commit1 changed file +92 -9
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 | ||
| 2125 | 2125 | decoded |
| 2126 | 2126 | } |
| 2127 | 2127 | |
| 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 | + | |
| 2128 | 2190 | fn silk_sources() -> Vec<(&'static str, Vec<f32>)> { |
| 2129 | 2191 | let wav = fixture("wav16-mono-48000.wav"); |
| 2130 | 2192 | let mut sources = vec![("synthetic", speech_like())]; |
| @@ -2163,11 +2225,13 @@ fn hybrid_roundtrip( | ||
| 2163 | 2225 | pcm: &[f32], |
| 2164 | 2226 | bps: u32, |
| 2165 | 2227 | bandwidth: Option<Bandwidth>, |
| 2228 | + mode: Option<Mode>, | |
| 2166 | 2229 | mute: (bool, bool), |
| 2167 | 2230 | ) -> (Vec<f32>, Vec<Vec<u8>>) { |
| 2168 | 2231 | let mut enc = Encoder::new(48000, 1, Application::Voip).unwrap(); |
| 2169 | 2232 | enc.set_bitrate(bps); |
| 2170 | 2233 | enc.set_bandwidth(bandwidth); |
| 2234 | + enc.set_mode(mode); | |
| 2171 | 2235 | let mut dec = Decoder::new(48000, 1).unwrap(); |
| 2172 | 2236 | dec.debug_mute_layers(mute.0, mute.1); |
| 2173 | 2237 | let mut out = vec![0u8; 1500]; |
| @@ -2215,8 +2279,8 @@ fn hybrid_layers_align() { | ||
| 2215 | 2279 | .unwrap() |
| 2216 | 2280 | .0 |
| 2217 | 2281 | }; |
| 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)); | |
| 2220 | 2284 | let delay = hybrid_delay(32_000) as i64; |
| 2221 | 2285 | let (plb, phb) = (peak(&lb) as i64 - at as i64, peak(&hb) as i64 - at as i64); |
| 2222 | 2286 | eprintln!("hybrid click: LB peak +{plb}, HB peak +{phb}, look_ahead {delay}"); |
| @@ -2243,11 +2307,23 @@ fn hybrid_layers_align() { | ||
| 2243 | 2307 | #[test] |
| 2244 | 2308 | fn hybrid_fb_roundtrip() { |
| 2245 | 2309 | 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)); | |
| 2247 | 2311 | let toc = ec_opus::Toc::new(packets[0][0]); |
| 2248 | 2312 | assert_eq!(toc.config, 15, "{name}: TOC config {}", toc.config); |
| 2249 | 2313 | 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 | + ); | |
| 2251 | 2327 | let lb_ref = lowpass(&pcm, 7000.0); |
| 2252 | 2328 | let (lb_corr, lb_lag) = aligned_corr(&lb_ref, &lb_only, 400); |
| 2253 | 2329 | let bytes: usize = packets.iter().map(Vec::len).sum(); |
| @@ -2257,9 +2333,11 @@ fn hybrid_fb_roundtrip() { | ||
| 2257 | 2333 | ); |
| 2258 | 2334 | assert!(corr >= 0.9, "{name}: full-band corr {corr:.4}"); |
| 2259 | 2335 | 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); | |
| 2260 | 2338 | 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}" | |
| 2263 | 2341 | ); |
| 2264 | 2342 | assert!(lb_corr >= 0.9, "{name}: low-band corr {lb_corr:.4}"); |
| 2265 | 2343 | assert!( |
| @@ -2275,11 +2353,16 @@ fn hybrid_fb_roundtrip() { | ||
| 2275 | 2353 | // SWB when asked for it, and the rest of the automatic hybrid range |
| 2276 | 2354 | // (SILK's share leaves CELT least at the bottom of it). |
| 2277 | 2355 | 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 | + ); | |
| 2280 | 2363 | assert_eq!(ec_opus::Toc::new(packets[0][0]).config, 13); |
| 2281 | 2364 | 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)); | |
| 2283 | 2366 | let (corr, _) = aligned_corr(&speech, &decoded, 400); |
| 2284 | 2367 | let kbps = packets.iter().map(Vec::len).sum::<usize>() as f64 * 8.0 |
| 2285 | 2368 | / (packets.len() as f64 * 20.0); |