Lock ogg timing through shim
Browse files at this commite356edd070d8bbfbb17aa190dec37289c1a335d6Parent: d687174- test
- lock ogg timing through shim
3 changed files +217 -0
- Cargo.lock +1 -0
- shims/symphonia/Cargo.toml +1 -0
- shims/symphonia/tests/vorbis_timing.rs +215 -0
Cargo.lockmodified+1 -0
| @@ -1253,6 +1253,7 @@ dependencies = [ | |||
| 1253 | "ec-ogg", | 1253 | "ec-ogg", |
| 1254 | "ec-probe", | 1254 | "ec-probe", |
| 1255 | "ec-vorbis", | 1255 | "ec-vorbis", |
| 1256 | + "rusty_vorbis", | ||
| 1256 | "symphonia-codec-aac", | 1257 | "symphonia-codec-aac", |
| 1257 | "symphonia-core", | 1258 | "symphonia-core", |
| 1258 | ] | 1259 | ] |
shims/symphonia/Cargo.tomlmodified+1 -0
| @@ -16,6 +16,7 @@ symphonia-core = { path = "../symphonia-core", version = "0.6.0" } | |||
| 16 | [dev-dependencies] | 16 | [dev-dependencies] |
| 17 | symphonia-codec-aac = { path = "../symphonia-codec-aac", version = "0.6.0" } | 17 | symphonia-codec-aac = { path = "../symphonia-codec-aac", version = "0.6.0" } |
| 18 | ec-vorbis = { path = "../../crates/ec-vorbis", version = "0.1.0" } | 18 | ec-vorbis = { path = "../../crates/ec-vorbis", version = "0.1.0" } |
| 19 | +rusty_vorbis = { path = "../rusty_vorbis", version = "0.1.1" } | ||
| 19 | 20 | ||
| 20 | # The incumbent's feature names, every one of them a no-op: a caller selects | 21 | # The incumbent's feature names, every one of them a no-op: a caller selects |
| 21 | # codecs and containers there, and here `ec_probe` serves all of them | 22 | # codecs and containers there, and here `ec_probe` serves all of them |
shims/symphonia/tests/vorbis_timing.rsadded+215 -0
| @@ -0,0 +1,215 @@ | |||
| 1 | +use std::fs::File; | ||
| 2 | +use std::path::{Path, PathBuf}; | ||
| 3 | +use std::process::Command; | ||
| 4 | + | ||
| 5 | +use ec_core::{ | ||
| 6 | + AudioParameters, Buf, ChannelLayout, CodecId, CodecParameters, Demuxer, MediaParameters, Muxer, | ||
| 7 | + Packet, StreamInfo, TimeBase, | ||
| 8 | +}; | ||
| 9 | +use symphonia_core::codecs::audio::AudioDecoderOptions; | ||
| 10 | +use symphonia_core::formats::probe::Hint; | ||
| 11 | +use symphonia_core::formats::{FormatOptions, FormatReader, TrackType}; | ||
| 12 | +use symphonia_core::io::MediaSourceStream; | ||
| 13 | +use symphonia_core::meta::MetadataOptions; | ||
| 14 | + | ||
| 15 | +const CHANNELS: u16 = 2; | ||
| 16 | +const FIRST_PACKET_FRAMES: usize = 1024; | ||
| 17 | + | ||
| 18 | +fn scratch(name: &str) -> PathBuf { | ||
| 19 | + let dir = std::env::temp_dir().join(format!("symphonia-timing-{}", std::process::id())); | ||
| 20 | + std::fs::create_dir_all(&dir).expect("scratch dir"); | ||
| 21 | + dir.join(name) | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +fn samples(rate: u32) -> Vec<i16> { | ||
| 25 | + let tone = (rate / 2) as usize; | ||
| 26 | + let gap = rate as usize; | ||
| 27 | + let frames = tone + gap + tone; | ||
| 28 | + let mut out = Vec::with_capacity(frames * usize::from(CHANNELS)); | ||
| 29 | + for frame in 0..frames { | ||
| 30 | + let active = frame < tone || frame >= tone + gap; | ||
| 31 | + for ch in 0..CHANNELS { | ||
| 32 | + let hz = if ch == 0 { 440.0 } else { 660.0 }; | ||
| 33 | + let v = if active { | ||
| 34 | + (2.0 * std::f64::consts::PI * hz * frame as f64 / f64::from(rate)).sin() * 0.45 | ||
| 35 | + } else { | ||
| 36 | + 0.0 | ||
| 37 | + }; | ||
| 38 | + out.push((v * f64::from(i16::MAX)) as i16); | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | + out | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +fn write_ogg(out: &Path, samples: &[i16], rate: u32) { | ||
| 45 | + let mut encoder = rusty_vorbis::VorbisEncoder::new(rusty_vorbis::VorbisEncoderConfig { | ||
| 46 | + bitrate_bps: rusty_vorbis::BITRATE_NOMINAL, | ||
| 47 | + quality: 0.85, | ||
| 48 | + }); | ||
| 49 | + encoder | ||
| 50 | + .push_pcm_s16(samples, CHANNELS, rate) | ||
| 51 | + .expect("encode"); | ||
| 52 | + encoder.finish(); | ||
| 53 | + let mut packets = Vec::new(); | ||
| 54 | + loop { | ||
| 55 | + match encoder.next_packet() { | ||
| 56 | + Ok(packet) => packets.push(packet), | ||
| 57 | + Err(rusty_vorbis::Error::Eof) => break, | ||
| 58 | + Err(e) => panic!("encode: {e}"), | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + let extradata = ec_ogg::xiph_lace(&[ | ||
| 63 | + &packets[0].data[..], | ||
| 64 | + &packets[1].data[..], | ||
| 65 | + &packets[2].data[..], | ||
| 66 | + ]) | ||
| 67 | + .expect("headers lace"); | ||
| 68 | + let time_base = TimeBase::new(1, i64::from(rate)); | ||
| 69 | + let mut params = CodecParameters::new(CodecId::Vorbis); | ||
| 70 | + params.media = MediaParameters::Audio(AudioParameters { | ||
| 71 | + sample_rate: rate, | ||
| 72 | + layout: ChannelLayout::from_count(usize::from(CHANNELS)), | ||
| 73 | + format: None, | ||
| 74 | + bits_per_sample: None, | ||
| 75 | + }); | ||
| 76 | + params.extradata = Some(Buf::from_vec(extradata)); | ||
| 77 | + let mut muxer = ec_ogg::OggMuxer::new(File::create(out).expect("create")); | ||
| 78 | + muxer | ||
| 79 | + .add_stream(StreamInfo::new(0, time_base, params)) | ||
| 80 | + .expect("stream"); | ||
| 81 | + muxer.write_headers().expect("headers"); | ||
| 82 | + for packet in &packets[3..] { | ||
| 83 | + let mut out = Packet::new(0, time_base, packet.data.clone()); | ||
| 84 | + out.side_data.push(ec_ogg::granule_side_data(packet.pts)); | ||
| 85 | + out.duration = Some(packet.duration); | ||
| 86 | + muxer.write_packet(&out).expect("packet"); | ||
| 87 | + } | ||
| 88 | + muxer.finish().expect("trailer"); | ||
| 89 | +} | ||
| 90 | + | ||
| 91 | +fn ffmpeg_decode(path: &Path) -> Vec<f32> { | ||
| 92 | + let out = Command::new("ffmpeg") | ||
| 93 | + .args(["-nostdin", "-v", "error", "-i"]) | ||
| 94 | + .arg(path) | ||
| 95 | + .args(["-f", "f32le", "-acodec", "pcm_f32le", "-"]) | ||
| 96 | + .output() | ||
| 97 | + .expect("ffmpeg"); | ||
| 98 | + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); | ||
| 99 | + out.stdout | ||
| 100 | + .chunks_exact(4) | ||
| 101 | + .map(|c| f32::from_le_bytes(c.try_into().unwrap())) | ||
| 102 | + .collect() | ||
| 103 | +} | ||
| 104 | + | ||
| 105 | +fn probe_decode(path: &Path) -> Vec<f32> { | ||
| 106 | + let mut demuxer = ec_ogg::OggDemuxer::open(File::open(path).expect("open")).expect("demux"); | ||
| 107 | + let params = demuxer.streams()[0].params.clone(); | ||
| 108 | + let mut decoder = ec_probe::AudioDecoder::new(¶ms).expect("decoder"); | ||
| 109 | + let mut out = Vec::new(); | ||
| 110 | + let mut chunk = Vec::new(); | ||
| 111 | + loop { | ||
| 112 | + match demuxer.next_packet() { | ||
| 113 | + Ok(packet) => { | ||
| 114 | + decoder.decode(&packet, &mut chunk).expect("decode"); | ||
| 115 | + out.extend_from_slice(&chunk); | ||
| 116 | + } | ||
| 117 | + Err(e) if e.is_eof() => break, | ||
| 118 | + Err(e) => panic!("demux: {e}"), | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + decoder.flush(&mut chunk).expect("flush"); | ||
| 122 | + out.extend_from_slice(&chunk); | ||
| 123 | + out | ||
| 124 | +} | ||
| 125 | + | ||
| 126 | +fn shim_reader(path: &Path) -> Box<dyn FormatReader> { | ||
| 127 | + let mss = MediaSourceStream::new(Box::new(File::open(path).expect("open")), Default::default()); | ||
| 128 | + let mut hint = Hint::new(); | ||
| 129 | + hint.with_extension("ogg"); | ||
| 130 | + symphonia::default::get_probe() | ||
| 131 | + .probe( | ||
| 132 | + &hint, | ||
| 133 | + mss, | ||
| 134 | + FormatOptions::default(), | ||
| 135 | + MetadataOptions::default(), | ||
| 136 | + ) | ||
| 137 | + .expect("probe") | ||
| 138 | +} | ||
| 139 | + | ||
| 140 | +fn shim_decode(path: &Path) -> Vec<f32> { | ||
| 141 | + let mut reader = shim_reader(path); | ||
| 142 | + let track = reader.default_track(TrackType::Audio).expect("track"); | ||
| 143 | + let track_id = track.id; | ||
| 144 | + let params = match track.codec_params.clone().expect("params") { | ||
| 145 | + symphonia_core::codecs::CodecParameters::Audio(params) => params, | ||
| 146 | + _ => panic!("audio params"), | ||
| 147 | + }; | ||
| 148 | + let mut decoder = symphonia::default::get_codecs() | ||
| 149 | + .make_audio_decoder(¶ms, &AudioDecoderOptions::default()) | ||
| 150 | + .expect("decoder"); | ||
| 151 | + let mut out = Vec::new(); | ||
| 152 | + let mut chunk = Vec::new(); | ||
| 153 | + while let Some(packet) = reader.next_packet().expect("packet") { | ||
| 154 | + if packet.track_id != track_id { | ||
| 155 | + continue; | ||
| 156 | + } | ||
| 157 | + decoder | ||
| 158 | + .decode(&packet) | ||
| 159 | + .expect("decode") | ||
| 160 | + .copy_to_vec_interleaved::<f32>(&mut chunk); | ||
| 161 | + out.extend_from_slice(&chunk); | ||
| 162 | + } | ||
| 163 | + out | ||
| 164 | +} | ||
| 165 | + | ||
| 166 | +fn gap_rms(samples: &[f32], rate: u32) -> f64 { | ||
| 167 | + let channels = usize::from(CHANNELS); | ||
| 168 | + let onset = ((rate / 2 + rate) as usize) * channels; | ||
| 169 | + let start = onset - ((rate / 20) as usize) * channels; | ||
| 170 | + let window = &samples[start..onset.min(samples.len())]; | ||
| 171 | + (window.iter().map(|&v| f64::from(v) * f64::from(v)).sum::<f64>() / window.len() as f64).sqrt() | ||
| 172 | +} | ||
| 173 | + | ||
| 174 | +fn max_abs_after_first_packet(a: &[f32], b: &[f32]) -> f32 { | ||
| 175 | + let skip = FIRST_PACKET_FRAMES * usize::from(CHANNELS); | ||
| 176 | + a[skip..] | ||
| 177 | + .iter() | ||
| 178 | + .zip(&b[skip..]) | ||
| 179 | + .map(|(x, y)| (x - y).abs()) | ||
| 180 | + .fold(0.0, f32::max) | ||
| 181 | +} | ||
| 182 | + | ||
| 183 | +#[test] | ||
| 184 | +fn ogg_timing_matches_reference_decoder() { | ||
| 185 | + if Command::new("ffmpeg").arg("-version").output().is_err() { | ||
| 186 | + eprintln!("skip: ffmpeg not on PATH"); | ||
| 187 | + return; | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + for rate in [44_100, 48_000] { | ||
| 191 | + let path = scratch(&format!("timing-{rate}.ogg")); | ||
| 192 | + write_ogg(&path, &samples(rate), rate); | ||
| 193 | + let shim = shim_decode(&path); | ||
| 194 | + let probe = probe_decode(&path); | ||
| 195 | + let reference = ffmpeg_decode(&path); | ||
| 196 | + | ||
| 197 | + let shim_gap = gap_rms(&shim, rate); | ||
| 198 | + let probe_gap = gap_rms(&probe, rate); | ||
| 199 | + let reference_gap = gap_rms(&reference, rate); | ||
| 200 | + let shim_max = max_abs_after_first_packet(&shim, &reference); | ||
| 201 | + let probe_max = max_abs_after_first_packet(&probe, &reference); | ||
| 202 | + eprintln!( | ||
| 203 | + "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}", | ||
| 204 | + shim.len() / usize::from(CHANNELS), | ||
| 205 | + probe.len() / usize::from(CHANNELS), | ||
| 206 | + reference.len() / usize::from(CHANNELS), | ||
| 207 | + ); | ||
| 208 | + | ||
| 209 | + assert_eq!(shim.len(), reference.len(), "{rate}: shim length"); | ||
| 210 | + assert_eq!(probe.len(), reference.len(), "{rate}: probe length"); | ||
| 211 | + assert!(shim_max <= 1e-6, "{rate}: shim differs by {shim_max}"); | ||
| 212 | + assert!(probe_max <= 1e-6, "{rate}: probe differs by {probe_max}"); | ||
| 213 | + assert!(shim_gap < 1e-4, "{rate}: shim gap rms {shim_gap}"); | ||
| 214 | + } | ||
| 215 | +} | ||