Pin shim packet timing
Browse files at this commit8a93fe8983bdd2dfdc87359a48b9f03e7c45a871Parent: 3506a6c- test
- pin shim packet timing
1 changed file +111 -18
shims/symphonia/tests/vorbis_timing.rsmodified+111 -18
| @@ -177,11 +177,14 @@ fn decode_from_reader( | ||
| 177 | 177 | out |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | -fn packet_prefix( | |
| 181 | - reader: &mut dyn FormatReader, | |
| 182 | - track_id: u32, | |
| 183 | - n: usize, | |
| 184 | -) -> Vec<(Vec<u8>, Option<i64>)> { | |
| 180 | +#[derive(Debug, Clone, PartialEq, Eq)] | |
| 181 | +struct PacketTiming { | |
| 182 | + data: Vec<u8>, | |
| 183 | + ts: i64, | |
| 184 | + granule: Option<i64>, | |
| 185 | +} | |
| 186 | + | |
| 187 | +fn packet_prefix(reader: &mut dyn FormatReader, track_id: u32, n: usize) -> Vec<PacketTiming> { | |
| 185 | 188 | let mut out = Vec::new(); |
| 186 | 189 | while out.len() < n { |
| 187 | 190 | let packet = reader |
| @@ -189,12 +192,49 @@ fn packet_prefix( | ||
| 189 | 192 | .expect("packet") |
| 190 | 193 | .expect("enough packets"); |
| 191 | 194 | if packet.track_id == track_id && !packet.data.is_empty() { |
| 192 | - out.push((packet.data.as_ref().to_vec(), packet.granule)); | |
| 195 | + out.push(PacketTiming { | |
| 196 | + data: packet.data.as_ref().to_vec(), | |
| 197 | + ts: packet.pts.value() as i64, | |
| 198 | + granule: packet.granule, | |
| 199 | + }); | |
| 200 | + } | |
| 201 | + } | |
| 202 | + out | |
| 203 | +} | |
| 204 | + | |
| 205 | +fn probe_packet_prefix(path: &Path, n: usize) -> Vec<PacketTiming> { | |
| 206 | + let mut demuxer = ec_ogg::OggDemuxer::open(File::open(path).expect("open")).expect("demux"); | |
| 207 | + let mut out = Vec::new(); | |
| 208 | + while out.len() < n { | |
| 209 | + match demuxer.next_packet() { | |
| 210 | + Ok(packet) => { | |
| 211 | + if packet.stream == 0 { | |
| 212 | + out.push(PacketTiming { | |
| 213 | + data: packet.data.as_ref().to_vec(), | |
| 214 | + ts: packet.pts.unwrap_or(0), | |
| 215 | + granule: ec_ogg::granule_of(&packet), | |
| 216 | + }); | |
| 217 | + } | |
| 218 | + } | |
| 219 | + Err(e) if e.is_eof() => panic!("enough packets"), | |
| 220 | + Err(e) => panic!("demux: {e}"), | |
| 193 | 221 | } |
| 194 | 222 | } |
| 195 | 223 | out |
| 196 | 224 | } |
| 197 | 225 | |
| 226 | +fn assert_packet_timing(rate: u32, label: &str, left: &[PacketTiming], right: &[PacketTiming]) { | |
| 227 | + assert_eq!(left.len(), right.len(), "{rate}: {label} packet count"); | |
| 228 | + for (i, (left, right)) in left.iter().zip(right).enumerate() { | |
| 229 | + assert_eq!(left.ts, right.ts, "{rate}: {label} packet {i} ts"); | |
| 230 | + assert_eq!( | |
| 231 | + left.granule, right.granule, | |
| 232 | + "{rate}: {label} packet {i} granule" | |
| 233 | + ); | |
| 234 | + assert_eq!(left.data, right.data, "{rate}: {label} packet {i} payload"); | |
| 235 | + } | |
| 236 | +} | |
| 237 | + | |
| 198 | 238 | fn seek_to_start(reader: &mut dyn FormatReader, track_id: u32) { |
| 199 | 239 | reader |
| 200 | 240 | .seek( |
| @@ -213,11 +253,7 @@ fn shim_decode(path: &Path) -> Vec<f32> { | ||
| 213 | 253 | decode_from_reader(&mut *reader, track_id, ¶ms) |
| 214 | 254 | } |
| 215 | 255 | |
| 216 | -fn gap_rms(samples: &[f32], rate: u32) -> f64 { | |
| 217 | - let channels = usize::from(CHANNELS); | |
| 218 | - let onset = ((rate / 2 + rate) as usize) * channels; | |
| 219 | - let start = onset - ((rate / 20) as usize) * channels; | |
| 220 | - let window = &samples[start..onset.min(samples.len())]; | |
| 256 | +fn rms(window: &[f32]) -> f64 { | |
| 221 | 257 | (window |
| 222 | 258 | .iter() |
| 223 | 259 | .map(|&v| f64::from(v) * f64::from(v)) |
| @@ -226,6 +262,31 @@ fn gap_rms(samples: &[f32], rate: u32) -> f64 { | ||
| 226 | 262 | .sqrt() |
| 227 | 263 | } |
| 228 | 264 | |
| 265 | +fn tone_rms(samples: &[f32], rate: u32) -> f64 { | |
| 266 | + let channels = usize::from(CHANNELS); | |
| 267 | + let start = FIRST_PACKET_FRAMES * channels; | |
| 268 | + let end = (rate / 2) as usize * channels; | |
| 269 | + rms(&samples[start..end.min(samples.len())]) | |
| 270 | +} | |
| 271 | + | |
| 272 | +fn gap_rms(samples: &[f32], rate: u32) -> f64 { | |
| 273 | + let channels = usize::from(CHANNELS); | |
| 274 | + let onset = ((rate / 2 + rate) as usize) * channels; | |
| 275 | + let start = onset - ((rate / 20) as usize) * channels; | |
| 276 | + rms(&samples[start..onset.min(samples.len())]) | |
| 277 | +} | |
| 278 | + | |
| 279 | +fn first_tone_onset(samples: &[f32], rate: u32) -> usize { | |
| 280 | + let channels = usize::from(CHANNELS); | |
| 281 | + let threshold = (tone_rms(samples, rate) * 0.25) as f32; | |
| 282 | + let search = rate as usize * channels; | |
| 283 | + samples[search..] | |
| 284 | + .chunks_exact(channels) | |
| 285 | + .position(|frame| frame.iter().any(|v| v.abs() >= threshold)) | |
| 286 | + .map(|frame| frame + search / channels) | |
| 287 | + .expect("tone onset") | |
| 288 | +} | |
| 289 | + | |
| 229 | 290 | fn max_abs_after_first_packet(a: &[f32], b: &[f32]) -> f32 { |
| 230 | 291 | let skip = FIRST_PACKET_FRAMES * usize::from(CHANNELS); |
| 231 | 292 | a[skip..] |
| @@ -252,10 +313,17 @@ fn ogg_timing_matches_reference_decoder() { | ||
| 252 | 313 | let shim_gap = gap_rms(&shim, rate); |
| 253 | 314 | let probe_gap = gap_rms(&probe, rate); |
| 254 | 315 | let reference_gap = gap_rms(&reference, rate); |
| 316 | + let shim_tone = tone_rms(&shim, rate); | |
| 317 | + let probe_tone = tone_rms(&probe, rate); | |
| 318 | + let reference_tone = tone_rms(&reference, rate); | |
| 319 | + let gap_bound = reference_tone * 0.01; | |
| 320 | + let shim_onset = first_tone_onset(&shim, rate); | |
| 321 | + let probe_onset = first_tone_onset(&probe, rate); | |
| 322 | + let reference_onset = first_tone_onset(&reference, rate); | |
| 255 | 323 | let shim_max = max_abs_after_first_packet(&shim, &reference); |
| 256 | 324 | let probe_max = max_abs_after_first_packet(&probe, &reference); |
| 257 | 325 | eprintln!( |
| 258 | - "rate={rate} frames shim={} probe={} ref={} gap shim={shim_gap:.8} probe={probe_gap:.8} ref={reference_gap:.8} max shim={shim_max:.8} probe={probe_max:.8}", | |
| 326 | + "rate={rate} frames shim={} probe={} ref={} tone shim={shim_tone:.8} probe={probe_tone:.8} ref={reference_tone:.8} gap_bound={gap_bound:.8} gap shim={shim_gap:.8} probe={probe_gap:.8} ref={reference_gap:.8} onset shim={shim_onset} probe={probe_onset} ref={reference_onset} max shim={shim_max:.8} probe={probe_max:.8}", | |
| 259 | 327 | shim.len() / usize::from(CHANNELS), |
| 260 | 328 | probe.len() / usize::from(CHANNELS), |
| 261 | 329 | reference.len() / usize::from(CHANNELS), |
| @@ -263,9 +331,28 @@ fn ogg_timing_matches_reference_decoder() { | ||
| 263 | 331 | |
| 264 | 332 | assert_eq!(shim.len(), reference.len(), "{rate}: shim length"); |
| 265 | 333 | assert_eq!(probe.len(), reference.len(), "{rate}: probe length"); |
| 334 | + assert!( | |
| 335 | + shim_gap <= gap_bound, | |
| 336 | + "{rate}: shim gap RMS {shim_gap:.8} exceeds 40 dB-down absolute bound {gap_bound:.8}; tone RMS {shim_tone:.8}" | |
| 337 | + ); | |
| 338 | + assert!( | |
| 339 | + probe_gap <= gap_bound, | |
| 340 | + "{rate}: probe gap RMS {probe_gap:.8} exceeds 40 dB-down absolute bound {gap_bound:.8}; tone RMS {probe_tone:.8}" | |
| 341 | + ); | |
| 342 | + assert!( | |
| 343 | + reference_gap <= gap_bound, | |
| 344 | + "{rate}: reference gap RMS {reference_gap:.8} exceeds 40 dB-down absolute bound {gap_bound:.8}; tone RMS {reference_tone:.8}" | |
| 345 | + ); | |
| 346 | + assert!( | |
| 347 | + shim_onset.abs_diff(probe_onset) <= 1, | |
| 348 | + "{rate}: shim onset {shim_onset} vs probe onset {probe_onset}" | |
| 349 | + ); | |
| 350 | + assert!( | |
| 351 | + shim_onset.abs_diff(reference_onset) <= 1, | |
| 352 | + "{rate}: shim onset {shim_onset} vs reference onset {reference_onset}" | |
| 353 | + ); | |
| 266 | 354 | assert!(shim_max <= 1e-6, "{rate}: shim differs by {shim_max}"); |
| 267 | 355 | assert!(probe_max <= 1e-6, "{rate}: probe differs by {probe_max}"); |
| 268 | - assert!(shim_gap < 1e-4, "{rate}: shim gap rms {shim_gap}"); | |
| 269 | 356 | } |
| 270 | 357 | } |
| 271 | 358 | |
| @@ -278,6 +365,8 @@ fn seek_to_start_replays_fresh_packet_timing() { | ||
| 278 | 365 | let mut fresh_reader = shim_reader(&path); |
| 279 | 366 | let (track_id, params) = audio_track(&*fresh_reader); |
| 280 | 367 | let fresh_packets = packet_prefix(&mut *fresh_reader, track_id, 8); |
| 368 | + let probe_packets = probe_packet_prefix(&path, 8); | |
| 369 | + assert_packet_timing(rate, "shim vs probe", &fresh_packets, &probe_packets); | |
| 281 | 370 | |
| 282 | 371 | let mut seeked_reader = shim_reader(&path); |
| 283 | 372 | let (seeked_track, seeked_params) = audio_track(&*seeked_reader); |
| @@ -286,15 +375,13 @@ fn seek_to_start_replays_fresh_packet_timing() { | ||
| 286 | 375 | let _ = packet_prefix(&mut *seeked_reader, seeked_track, 5); |
| 287 | 376 | seek_to_start(&mut *seeked_reader, seeked_track); |
| 288 | 377 | let seeked_packets = packet_prefix(&mut *seeked_reader, seeked_track, 8); |
| 378 | + assert_packet_timing(rate, "seek(0) vs probe", &seeked_packets, &probe_packets); | |
| 289 | 379 | |
| 290 | 380 | eprintln!( |
| 291 | 381 | "rate={rate} fresh_first_granule={:?} seek_first_granule={:?}", |
| 292 | - fresh_packets[0].1, seeked_packets[0].1 | |
| 293 | - ); | |
| 294 | - assert_eq!( | |
| 295 | - seeked_packets, fresh_packets, | |
| 296 | - "{rate}: packets after seek(0)" | |
| 382 | + fresh_packets[0].granule, seeked_packets[0].granule | |
| 297 | 383 | ); |
| 384 | + assert_packet_timing(rate, "seek(0) vs fresh", &seeked_packets, &fresh_packets); | |
| 298 | 385 | |
| 299 | 386 | let fresh_pcm = shim_decode(&path); |
| 300 | 387 | let mut decode_reader = shim_reader(&path); |
| @@ -303,6 +390,12 @@ fn seek_to_start_replays_fresh_packet_timing() { | ||
| 303 | 390 | seek_to_start(&mut *decode_reader, decode_track); |
| 304 | 391 | let seeked_pcm = decode_from_reader(&mut *decode_reader, decode_track, &decode_params); |
| 305 | 392 | assert_eq!(seeked_pcm, fresh_pcm, "{rate}: decoded PCM after seek(0)"); |
| 393 | + let fresh_onset = first_tone_onset(&fresh_pcm, rate); | |
| 394 | + let seeked_onset = first_tone_onset(&seeked_pcm, rate); | |
| 395 | + assert!( | |
| 396 | + seeked_onset.abs_diff(fresh_onset) <= 1, | |
| 397 | + "{rate}: seek(0) onset {seeked_onset} vs fresh onset {fresh_onset}" | |
| 398 | + ); | |
| 306 | 399 | |
| 307 | 400 | let mut mid_reader = shim_reader(&path); |
| 308 | 401 | let (mid_track, mid_params) = audio_track(&*mid_reader); |