Symphonia, symphonia-core and symphonia-codec-aac over ec-probe
Browse files at this commit32195c9fdbbe6115e8fcae783b7a932bf6f3bea4Parent: 51de303- feat
- symphonia, symphonia-core and symphonia-codec-aac over ec-probe
14 changed files +1518 -0
- shims/symphonia-codec-aac/Cargo.toml +12 -0
- shims/symphonia-codec-aac/src/lib.rs +81 -0
- shims/symphonia-core/Cargo.toml +11 -0
- shims/symphonia-core/src/codecs.rs +373 -0
- shims/symphonia-core/src/formats.rs +136 -0
- shims/symphonia-core/src/io.rs +50 -0
- shims/symphonia-core/src/lib.rs +77 -0
- shims/symphonia-core/src/meta.rs +12 -0
- shims/symphonia-core/src/packet.rs +38 -0
- shims/symphonia-core/src/units.rs +134 -0
- shims/symphonia/Cargo.toml +16 -0
- shims/symphonia/src/lib.rs +137 -0
- shims/symphonia/src/reader.rs +159 -0
- shims/symphonia/tests/edith_surface.rs +282 -0
shims/symphonia-codec-aac/Cargo.tomladded+12 -0
| @@ -0,0 +1,12 @@ | ||
| 1 | +[package] | |
| 2 | +name = "symphonia-codec-aac" | |
| 3 | +description = "Compatibility shim: the symphonia-codec-aac 0.6 surface edith consumes, served by ec-aac." | |
| 4 | +version = "0.6.0" | |
| 5 | +edition = "2024" | |
| 6 | +license = "MIT OR Apache-2.0" | |
| 7 | +authors = ["Ahmet Kaan Gümüş"] | |
| 8 | +publish = false | |
| 9 | + | |
| 10 | +[dependencies] | |
| 11 | +ec-aac = { path = "../../crates/ec-aac", version = "0.1.0" } | |
| 12 | +symphonia-core = { path = "../symphonia-core", version = "0.6.0" } |
shims/symphonia-codec-aac/src/lib.rsadded+81 -0
| @@ -0,0 +1,81 @@ | ||
| 1 | +//! Compatibility shim: the `symphonia-codec-aac` 0.6 surface edith consumes, | |
| 2 | +//! served by [`ec_aac`]. | |
| 3 | +//! | |
| 4 | +//! One difference from the incumbent, and it is the reason this exists: | |
| 5 | +//! **multichannel decodes**. The incumbent refuses anything wider than stereo | |
| 6 | +//! outright (`aac/mod.rs:93`, "aac: aac too complex"), which is exactly the 5.1 | |
| 7 | +//! and 7.1 tracks of a film — edith routes those to a second AAC crate to get | |
| 8 | +//! around it. [`ec_aac`] decodes mono through 7.1 in film channel order, so | |
| 9 | +//! there is nothing to route around. | |
| 10 | +//! | |
| 11 | +//! Written from the signatures edith calls (`AacDecoder::try_new`, then the | |
| 12 | +//! `AudioDecoder` trait) and the crate's published documentation; no symphonia | |
| 13 | +//! source was read. | |
| 14 | + | |
| 15 | +#![forbid(unsafe_code)] | |
| 16 | + | |
| 17 | +use symphonia_core::codecs::audio::{ | |
| 18 | + AudioCodecParameters, AudioDecoder, AudioDecoderOptions, GenericAudioBufferRef, | |
| 19 | + well_known::CODEC_ID_AAC, | |
| 20 | +}; | |
| 21 | +use symphonia_core::packet::Packet; | |
| 22 | +use symphonia_core::{Error, Result}; | |
| 23 | + | |
| 24 | +/// An AAC-LC decoder for one track. | |
| 25 | +pub struct AacDecoder { | |
| 26 | + inner: ec_aac::AacDecoder, | |
| 27 | + params: AudioCodecParameters, | |
| 28 | + out: Vec<f32>, | |
| 29 | + channels: usize, | |
| 30 | +} | |
| 31 | + | |
| 32 | +impl AacDecoder { | |
| 33 | + /// A decoder for the track `params` describes. | |
| 34 | + /// | |
| 35 | + /// The `extra_data` is the `AudioSpecificConfig`; a track with none (raw | |
| 36 | + /// ADTS, which states its configuration in every frame) is accepted and | |
| 37 | + /// configures itself from the first frame. | |
| 38 | + pub fn try_new( | |
| 39 | + params: &AudioCodecParameters, | |
| 40 | + _options: &AudioDecoderOptions, | |
| 41 | + ) -> Result<AacDecoder> { | |
| 42 | + if params.codec != CODEC_ID_AAC { | |
| 43 | + return Err(Error::Unsupported(format!( | |
| 44 | + "codec id {} is not AAC", | |
| 45 | + params.codec.0 | |
| 46 | + ))); | |
| 47 | + } | |
| 48 | + let inner = match params.extra_data.as_deref() { | |
| 49 | + Some(asc) => ec_aac::AacDecoder::with_config_bytes(asc)?, | |
| 50 | + None => ec_aac::AacDecoder::new(), | |
| 51 | + }; | |
| 52 | + let channels = params.channels.map_or(2, |c| c.count()).max(1) as usize; | |
| 53 | + Ok(AacDecoder { | |
| 54 | + inner, | |
| 55 | + params: params.clone(), | |
| 56 | + out: Vec::new(), | |
| 57 | + channels, | |
| 58 | + }) | |
| 59 | + } | |
| 60 | +} | |
| 61 | + | |
| 62 | +impl AudioDecoder for AacDecoder { | |
| 63 | + fn decode(&mut self, packet: &Packet) -> Result<GenericAudioBufferRef<'_>> { | |
| 64 | + let audio = self.inner.decode(&packet.data, Some(packet.pts.value()))?; | |
| 65 | + self.channels = usize::from(audio.channels).max(1); | |
| 66 | + self.out.clear(); | |
| 67 | + self.out.extend_from_slice(&audio.samples); | |
| 68 | + Ok(GenericAudioBufferRef::new(&self.out, self.channels)) | |
| 69 | + } | |
| 70 | + | |
| 71 | + fn codec_params(&self) -> &AudioCodecParameters { | |
| 72 | + &self.params | |
| 73 | + } | |
| 74 | + | |
| 75 | + fn reset(&mut self) { | |
| 76 | + self.inner = match self.params.extra_data.as_deref() { | |
| 77 | + Some(asc) => ec_aac::AacDecoder::with_config_bytes(asc).unwrap_or_default(), | |
| 78 | + None => ec_aac::AacDecoder::new(), | |
| 79 | + }; | |
| 80 | + } | |
| 81 | +} |
shims/symphonia-core/Cargo.tomladded+11 -0
| @@ -0,0 +1,11 @@ | ||
| 1 | +[package] | |
| 2 | +name = "symphonia-core" | |
| 3 | +description = "Compatibility shim: the symphonia-core 0.6 surface edith consumes, over ec-core." | |
| 4 | +version = "0.6.0" | |
| 5 | +edition = "2024" | |
| 6 | +license = "MIT OR Apache-2.0" | |
| 7 | +authors = ["Ahmet Kaan Gümüş"] | |
| 8 | +publish = false | |
| 9 | + | |
| 10 | +[dependencies] | |
| 11 | +ec-core = { path = "../../crates/ec-core", version = "0.1.0" } |
shims/symphonia-core/src/codecs.rsadded+373 -0
| @@ -0,0 +1,373 @@ | ||
| 1 | +//! Codec identities, stream parameters and the decoder trait. | |
| 2 | + | |
| 3 | +use crate::Result; | |
| 4 | +use crate::packet::Packet; | |
| 5 | + | |
| 6 | +/// Per-media-type parameters, as a track carries them. | |
| 7 | +#[derive(Debug, Clone, PartialEq)] | |
| 8 | +pub enum CodecParameters { | |
| 9 | + /// An audio track. | |
| 10 | + Audio(audio::AudioCodecParameters), | |
| 11 | + /// A video track. Nothing in this family decodes picture through the | |
| 12 | + /// symphonia surface, so it carries only what a listing needs. | |
| 13 | + Video(VideoCodecParameters), | |
| 14 | + /// A subtitle track. | |
| 15 | + Subtitle(SubtitleCodecParameters), | |
| 16 | +} | |
| 17 | + | |
| 18 | +/// What a video track states about itself. | |
| 19 | +#[derive(Debug, Clone, Default, PartialEq)] | |
| 20 | +pub struct VideoCodecParameters { | |
| 21 | + /// Codec identity, in this shim's own numbering. | |
| 22 | + pub codec: u32, | |
| 23 | + /// Coded width. | |
| 24 | + pub width: Option<u16>, | |
| 25 | + /// Coded height. | |
| 26 | + pub height: Option<u16>, | |
| 27 | +} | |
| 28 | + | |
| 29 | +/// What a subtitle track states about itself. | |
| 30 | +#[derive(Debug, Clone, Default, PartialEq)] | |
| 31 | +pub struct SubtitleCodecParameters { | |
| 32 | + /// Codec identity, in this shim's own numbering. | |
| 33 | + pub codec: u32, | |
| 34 | + /// Setup bytes (the ASS header, for instance). | |
| 35 | + pub extra_data: Option<Box<[u8]>>, | |
| 36 | +} | |
| 37 | + | |
| 38 | +/// A codec's name, as a registry reports it. | |
| 39 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 40 | +pub struct CodecInfo { | |
| 41 | + /// Short lowercase name, e.g. `"aac"`. | |
| 42 | + pub short_name: &'static str, | |
| 43 | + /// Human-readable name. | |
| 44 | + pub long_name: &'static str, | |
| 45 | +} | |
| 46 | + | |
| 47 | +/// One entry of a codec registry. | |
| 48 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 49 | +pub struct CodecDescriptor { | |
| 50 | + /// The identity this entry serves. | |
| 51 | + pub id: audio::AudioCodecId, | |
| 52 | + /// Its names. | |
| 53 | + pub info: CodecInfo, | |
| 54 | +} | |
| 55 | + | |
| 56 | +/// A registry's answer for one audio codec. | |
| 57 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 58 | +pub struct AudioDecoderRegistration { | |
| 59 | + /// The codec this registration decodes. | |
| 60 | + pub codec: CodecDescriptor, | |
| 61 | +} | |
| 62 | + | |
| 63 | +/// Audio codecs, parameters and decoders. | |
| 64 | +pub mod audio { | |
| 65 | + use super::*; | |
| 66 | + | |
| 67 | + /// A codec identity. The numbering is this shim's own; only equality | |
| 68 | + /// against the [`well_known`] constants is meaningful. | |
| 69 | + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] | |
| 70 | + pub struct AudioCodecId(pub u32); | |
| 71 | + | |
| 72 | + /// The identities edith names. | |
| 73 | + pub mod well_known { | |
| 74 | + use super::AudioCodecId; | |
| 75 | + | |
| 76 | + /// MPEG-4 AAC. | |
| 77 | + pub const CODEC_ID_AAC: AudioCodecId = AudioCodecId(0x1000); | |
| 78 | + /// Xiph Opus. | |
| 79 | + pub const CODEC_ID_OPUS: AudioCodecId = AudioCodecId(0x1001); | |
| 80 | + /// Xiph Vorbis. | |
| 81 | + pub const CODEC_ID_VORBIS: AudioCodecId = AudioCodecId(0x1002); | |
| 82 | + /// Xiph FLAC. | |
| 83 | + pub const CODEC_ID_FLAC: AudioCodecId = AudioCodecId(0x1003); | |
| 84 | + /// MPEG-1/2 Layer III. | |
| 85 | + pub const CODEC_ID_MP3: AudioCodecId = AudioCodecId(0x1004); | |
| 86 | + /// Apple Lossless. | |
| 87 | + pub const CODEC_ID_ALAC: AudioCodecId = AudioCodecId(0x1005); | |
| 88 | + /// Dolby AC-3. | |
| 89 | + pub const CODEC_ID_AC3: AudioCodecId = AudioCodecId(0x1006); | |
| 90 | + /// Dolby Digital Plus. | |
| 91 | + pub const CODEC_ID_EAC3: AudioCodecId = AudioCodecId(0x1007); | |
| 92 | + /// Dolby TrueHD. No decoder is registered for it, which is how a | |
| 93 | + /// caller learns the track cannot be played. | |
| 94 | + pub const CODEC_ID_TRUEHD: AudioCodecId = AudioCodecId(0x1008); | |
| 95 | + /// DTS and its extensions. No decoder is registered for it either. | |
| 96 | + pub const CODEC_ID_DTS: AudioCodecId = AudioCodecId(0x1009); | |
| 97 | + /// Unsigned 8-bit PCM. | |
| 98 | + pub const CODEC_ID_PCM_U8: AudioCodecId = AudioCodecId(0x1010); | |
| 99 | + /// Signed 16-bit little-endian PCM. | |
| 100 | + pub const CODEC_ID_PCM_S16LE: AudioCodecId = AudioCodecId(0x1011); | |
| 101 | + /// Signed 16-bit big-endian PCM. | |
| 102 | + pub const CODEC_ID_PCM_S16BE: AudioCodecId = AudioCodecId(0x1012); | |
| 103 | + /// Signed 24-bit little-endian PCM. | |
| 104 | + pub const CODEC_ID_PCM_S24LE: AudioCodecId = AudioCodecId(0x1013); | |
| 105 | + /// Signed 32-bit little-endian PCM. | |
| 106 | + pub const CODEC_ID_PCM_S32LE: AudioCodecId = AudioCodecId(0x1014); | |
| 107 | + /// 32-bit float little-endian PCM. | |
| 108 | + pub const CODEC_ID_PCM_F32LE: AudioCodecId = AudioCodecId(0x1015); | |
| 109 | + /// Anything with no identity of its own here. | |
| 110 | + pub const CODEC_ID_NULL: AudioCodecId = AudioCodecId(0); | |
| 111 | + } | |
| 112 | + | |
| 113 | + /// A channel layout, as far as anything here asks: how many there are. | |
| 114 | + #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] | |
| 115 | + pub struct Channels(pub u32); | |
| 116 | + | |
| 117 | + impl Channels { | |
| 118 | + /// Channels in the layout. | |
| 119 | + pub fn count(&self) -> u32 { | |
| 120 | + self.0 | |
| 121 | + } | |
| 122 | + } | |
| 123 | + | |
| 124 | + /// Everything an audio decoder needs before its first packet. | |
| 125 | + #[derive(Debug, Clone, Default, PartialEq)] | |
| 126 | + pub struct AudioCodecParameters { | |
| 127 | + /// Which codec. | |
| 128 | + pub codec: AudioCodecId, | |
| 129 | + /// Sample rate in Hz. | |
| 130 | + pub sample_rate: Option<u32>, | |
| 131 | + /// Channel layout. | |
| 132 | + pub channels: Option<Channels>, | |
| 133 | + /// Coded bit depth, when the container states one. | |
| 134 | + pub bits_per_sample: Option<u32>, | |
| 135 | + /// Codec setup bytes: the AudioSpecificConfig, `OpusHead`, STREAMINFO. | |
| 136 | + pub extra_data: Option<Box<[u8]>>, | |
| 137 | + } | |
| 138 | + | |
| 139 | + impl AudioCodecParameters { | |
| 140 | + /// Empty parameters, filled in by the `with_*` builders. | |
| 141 | + pub fn new() -> AudioCodecParameters { | |
| 142 | + AudioCodecParameters::default() | |
| 143 | + } | |
| 144 | + | |
| 145 | + /// Name the codec. | |
| 146 | + pub fn for_codec(&mut self, codec: AudioCodecId) -> &mut AudioCodecParameters { | |
| 147 | + self.codec = codec; | |
| 148 | + self | |
| 149 | + } | |
| 150 | + | |
| 151 | + /// State the sample rate. | |
| 152 | + pub fn with_sample_rate(&mut self, rate: u32) -> &mut AudioCodecParameters { | |
| 153 | + self.sample_rate = Some(rate); | |
| 154 | + self | |
| 155 | + } | |
| 156 | + | |
| 157 | + /// State the channel layout. | |
| 158 | + pub fn with_channels(&mut self, channels: Channels) -> &mut AudioCodecParameters { | |
| 159 | + self.channels = Some(channels); | |
| 160 | + self | |
| 161 | + } | |
| 162 | + | |
| 163 | + /// State the bit depth. | |
| 164 | + pub fn with_bits_per_sample(&mut self, bits: u32) -> &mut AudioCodecParameters { | |
| 165 | + self.bits_per_sample = Some(bits); | |
| 166 | + self | |
| 167 | + } | |
| 168 | + | |
| 169 | + /// State the codec setup bytes. | |
| 170 | + pub fn with_extra_data(&mut self, data: Box<[u8]>) -> &mut AudioCodecParameters { | |
| 171 | + self.extra_data = Some(data); | |
| 172 | + self | |
| 173 | + } | |
| 174 | + } | |
| 175 | + | |
| 176 | + /// Options a decoder is built with. Nothing here has any, which is what | |
| 177 | + /// `Default` means for it. | |
| 178 | + #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] | |
| 179 | + pub struct AudioDecoderOptions { | |
| 180 | + /// Verify the decoded audio against a checksum where the format has | |
| 181 | + /// one. Not implemented by any decoder in this family. | |
| 182 | + pub verify: bool, | |
| 183 | + } | |
| 184 | + | |
| 185 | + /// A decoded block, borrowed from the decoder until its next call. | |
| 186 | + #[derive(Debug, Clone, Copy)] | |
| 187 | + pub struct GenericAudioBufferRef<'a> { | |
| 188 | + samples: &'a [f32], | |
| 189 | + channels: usize, | |
| 190 | + } | |
| 191 | + | |
| 192 | + impl<'a> GenericAudioBufferRef<'a> { | |
| 193 | + /// Wrap interleaved samples. | |
| 194 | + pub fn new(samples: &'a [f32], channels: usize) -> GenericAudioBufferRef<'a> { | |
| 195 | + GenericAudioBufferRef { samples, channels } | |
| 196 | + } | |
| 197 | + | |
| 198 | + /// Channels in the block. | |
| 199 | + pub fn spec_channels(&self) -> usize { | |
| 200 | + self.channels | |
| 201 | + } | |
| 202 | + | |
| 203 | + /// Sample frames in the block. | |
| 204 | + pub fn frames(&self) -> usize { | |
| 205 | + match self.channels { | |
| 206 | + 0 => 0, | |
| 207 | + n => self.samples.len() / n, | |
| 208 | + } | |
| 209 | + } | |
| 210 | + | |
| 211 | + /// Copy the block into `out`, interleaved, replacing what was there. | |
| 212 | + pub fn copy_to_vec_interleaved<S: FromSample>(&self, out: &mut Vec<S>) { | |
| 213 | + out.clear(); | |
| 214 | + out.extend(self.samples.iter().map(|&s| S::from_sample(s))); | |
| 215 | + } | |
| 216 | + } | |
| 217 | + | |
| 218 | + /// A sample type [`GenericAudioBufferRef::copy_to_vec_interleaved`] can | |
| 219 | + /// convert to. | |
| 220 | + pub trait FromSample { | |
| 221 | + /// Convert one normalised `f32` sample. | |
| 222 | + fn from_sample(s: f32) -> Self; | |
| 223 | + } | |
| 224 | + | |
| 225 | + impl FromSample for f32 { | |
| 226 | + fn from_sample(s: f32) -> f32 { | |
| 227 | + s | |
| 228 | + } | |
| 229 | + } | |
| 230 | + | |
| 231 | + impl FromSample for i16 { | |
| 232 | + fn from_sample(s: f32) -> i16 { | |
| 233 | + (s.clamp(-1.0, 1.0) * 32767.0) as i16 | |
| 234 | + } | |
| 235 | + } | |
| 236 | + | |
| 237 | + impl FromSample for i32 { | |
| 238 | + fn from_sample(s: f32) -> i32 { | |
| 239 | + (f64::from(s.clamp(-1.0, 1.0)) * 2147483647.0) as i32 | |
| 240 | + } | |
| 241 | + } | |
| 242 | + | |
| 243 | + /// A decoder: packets in, one block out per packet. | |
| 244 | + pub trait AudioDecoder: Send { | |
| 245 | + /// Decode one packet. | |
| 246 | + fn decode(&mut self, packet: &Packet) -> Result<GenericAudioBufferRef<'_>>; | |
| 247 | + | |
| 248 | + /// The parameters this decoder was built with. | |
| 249 | + fn codec_params(&self) -> &AudioCodecParameters; | |
| 250 | + | |
| 251 | + /// Drop state left over from before a seek. | |
| 252 | + fn reset(&mut self); | |
| 253 | + } | |
| 254 | +} | |
| 255 | + | |
| 256 | +impl CodecParameters { | |
| 257 | + /// The audio half, when this is an audio track. | |
| 258 | + pub fn audio(&self) -> Option<&audio::AudioCodecParameters> { | |
| 259 | + match self { | |
| 260 | + CodecParameters::Audio(p) => Some(p), | |
| 261 | + _ => None, | |
| 262 | + } | |
| 263 | + } | |
| 264 | +} | |
| 265 | + | |
| 266 | +/// The identity a family [`ec_core::CodecId`] answers to here. | |
| 267 | +pub fn audio_codec_id(codec: ec_core::CodecId) -> audio::AudioCodecId { | |
| 268 | + use audio::well_known::*; | |
| 269 | + use ec_core::CodecId; | |
| 270 | + match codec { | |
| 271 | + CodecId::Aac => CODEC_ID_AAC, | |
| 272 | + CodecId::Opus => CODEC_ID_OPUS, | |
| 273 | + CodecId::Vorbis => CODEC_ID_VORBIS, | |
| 274 | + CodecId::Flac => CODEC_ID_FLAC, | |
| 275 | + CodecId::Mp3 => CODEC_ID_MP3, | |
| 276 | + CodecId::Alac => CODEC_ID_ALAC, | |
| 277 | + CodecId::Ac3 => CODEC_ID_AC3, | |
| 278 | + CodecId::EAc3 => CODEC_ID_EAC3, | |
| 279 | + CodecId::TrueHd => CODEC_ID_TRUEHD, | |
| 280 | + CodecId::Dts => CODEC_ID_DTS, | |
| 281 | + // Each PCM width keeps its own identity: mapping them all to one and | |
| 282 | + // back would decode a 24-bit WAVE as 16-bit. | |
| 283 | + CodecId::PcmU8 => CODEC_ID_PCM_U8, | |
| 284 | + CodecId::PcmS16Le => CODEC_ID_PCM_S16LE, | |
| 285 | + CodecId::PcmS16Be => CODEC_ID_PCM_S16BE, | |
| 286 | + CodecId::PcmS24Le => CODEC_ID_PCM_S24LE, | |
| 287 | + CodecId::PcmS32Le => CODEC_ID_PCM_S32LE, | |
| 288 | + CodecId::PcmF32Le => CODEC_ID_PCM_F32LE, | |
| 289 | + _ => CODEC_ID_NULL, | |
| 290 | + } | |
| 291 | +} | |
| 292 | + | |
| 293 | +/// The family codec an identity names, or [`None`] for one nothing here | |
| 294 | +/// decodes. | |
| 295 | +pub fn ec_codec_id(id: audio::AudioCodecId) -> Option<ec_core::CodecId> { | |
| 296 | + use audio::well_known::*; | |
| 297 | + use ec_core::CodecId; | |
| 298 | + Some(match id { | |
| 299 | + CODEC_ID_AAC => CodecId::Aac, | |
| 300 | + CODEC_ID_OPUS => CodecId::Opus, | |
| 301 | + CODEC_ID_VORBIS => CodecId::Vorbis, | |
| 302 | + CODEC_ID_FLAC => CodecId::Flac, | |
| 303 | + CODEC_ID_MP3 => CodecId::Mp3, | |
| 304 | + CODEC_ID_ALAC => CodecId::Alac, | |
| 305 | + CODEC_ID_AC3 => CodecId::Ac3, | |
| 306 | + CODEC_ID_EAC3 => CodecId::EAc3, | |
| 307 | + CODEC_ID_TRUEHD => CodecId::TrueHd, | |
| 308 | + CODEC_ID_DTS => CodecId::Dts, | |
| 309 | + CODEC_ID_PCM_U8 => CodecId::PcmU8, | |
| 310 | + CODEC_ID_PCM_S16LE => CodecId::PcmS16Le, | |
| 311 | + CODEC_ID_PCM_S16BE => CodecId::PcmS16Be, | |
| 312 | + CODEC_ID_PCM_S24LE => CodecId::PcmS24Le, | |
| 313 | + CODEC_ID_PCM_S32LE => CodecId::PcmS32Le, | |
| 314 | + CODEC_ID_PCM_F32LE => CodecId::PcmF32Le, | |
| 315 | + _ => return None, | |
| 316 | + }) | |
| 317 | +} | |
| 318 | + | |
| 319 | +/// Family parameters for what a track states, for handing to a family decoder. | |
| 320 | +pub fn ec_parameters( | |
| 321 | + params: &audio::AudioCodecParameters, | |
| 322 | +) -> crate::Result<ec_core::CodecParameters> { | |
| 323 | + let codec = ec_codec_id(params.codec).ok_or_else(|| { | |
| 324 | + crate::Error::Unsupported(format!("no decoder for codec id {}", params.codec.0)) | |
| 325 | + })?; | |
| 326 | + let mut out = ec_core::CodecParameters::new(codec); | |
| 327 | + out.extradata = params | |
| 328 | + .extra_data | |
| 329 | + .as_deref() | |
| 330 | + .map(ec_core::packet::Buf::copy_from_slice); | |
| 331 | + out.media = ec_core::registry::MediaParameters::Audio(ec_core::registry::AudioParameters { | |
| 332 | + sample_rate: params.sample_rate.unwrap_or(48_000), | |
| 333 | + layout: ec_core::ChannelLayout::from_count( | |
| 334 | + params.channels.map_or(2, |c| c.count()).max(1) as usize | |
| 335 | + ), | |
| 336 | + format: None, | |
| 337 | + bits_per_sample: params.bits_per_sample, | |
| 338 | + }); | |
| 339 | + Ok(out) | |
| 340 | +} | |
| 341 | + | |
| 342 | +/// What a track states, from the family's own description of it. | |
| 343 | +pub fn from_ec_parameters(params: &ec_core::CodecParameters) -> CodecParameters { | |
| 344 | + match params.audio() { | |
| 345 | + Some(audio) => { | |
| 346 | + let mut out = audio::AudioCodecParameters::new(); | |
| 347 | + out.for_codec(audio_codec_id(params.codec)) | |
| 348 | + .with_sample_rate(audio.sample_rate) | |
| 349 | + .with_channels(audio::Channels(audio.layout.channel_count() as u32)); | |
| 350 | + if let Some(bits) = audio.bits_per_sample { | |
| 351 | + out.with_bits_per_sample(bits); | |
| 352 | + } | |
| 353 | + if let Some(extra) = ¶ms.extradata { | |
| 354 | + out.with_extra_data(extra.as_ref().to_vec().into_boxed_slice()); | |
| 355 | + } | |
| 356 | + CodecParameters::Audio(out) | |
| 357 | + } | |
| 358 | + None => match params.video() { | |
| 359 | + Some(video) => CodecParameters::Video(VideoCodecParameters { | |
| 360 | + codec: audio_codec_id(params.codec).0, | |
| 361 | + width: Some(video.width as u16), | |
| 362 | + height: Some(video.height as u16), | |
| 363 | + }), | |
| 364 | + None => CodecParameters::Subtitle(SubtitleCodecParameters { | |
| 365 | + codec: audio_codec_id(params.codec).0, | |
| 366 | + extra_data: params | |
| 367 | + .extradata | |
| 368 | + .as_ref() | |
| 369 | + .map(|b| b.as_ref().to_vec().into_boxed_slice()), | |
| 370 | + }), | |
| 371 | + }, | |
| 372 | + } | |
| 373 | +} |
shims/symphonia-core/src/formats.rsadded+136 -0
| @@ -0,0 +1,136 @@ | ||
| 1 | +//! Tracks, the reader trait, and seeking. | |
| 2 | + | |
| 3 | +use crate::Result; | |
| 4 | +use crate::codecs::CodecParameters; | |
| 5 | +use crate::packet::Packet; | |
| 6 | +use crate::units::{Time, TimeBase, Timestamp}; | |
| 7 | + | |
| 8 | +/// What kind of stream a track carries. | |
| 9 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | |
| 10 | +pub enum TrackType { | |
| 11 | + /// Sound. | |
| 12 | + Audio, | |
| 13 | + /// Pictures. | |
| 14 | + Video, | |
| 15 | + /// Timed text or bitmaps. | |
| 16 | + Subtitle, | |
| 17 | +} | |
| 18 | + | |
| 19 | +/// One track of a container. | |
| 20 | +#[derive(Debug, Clone, PartialEq)] | |
| 21 | +pub struct Track { | |
| 22 | + /// The container's own id for the track — a Matroska `TrackNumber`, which | |
| 23 | + /// is what names one language of a dual-audio file. | |
| 24 | + pub id: u32, | |
| 25 | + /// Which kind of stream it is. | |
| 26 | + pub track_type: TrackType, | |
| 27 | + /// Base of this track's timestamps. | |
| 28 | + pub time_base: Option<TimeBase>, | |
| 29 | + /// Sample frames the track holds, when the container states one. | |
| 30 | + pub num_frames: Option<u64>, | |
| 31 | + /// First timestamp. | |
| 32 | + pub start_ts: u64, | |
| 33 | + /// Codec and setup data. | |
| 34 | + pub codec_params: Option<CodecParameters>, | |
| 35 | + /// ISO 639 language tag, when the container carries one. | |
| 36 | + pub language: Option<String>, | |
| 37 | +} | |
| 38 | + | |
| 39 | +/// Options a reader is built with; nothing here has any. | |
| 40 | +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] | |
| 41 | +pub struct FormatOptions { | |
| 42 | + /// Build a seek index even when the container carries none. | |
| 43 | + pub enable_gapless: bool, | |
| 44 | +} | |
| 45 | + | |
| 46 | +/// How exactly a seek must land. | |
| 47 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] | |
| 48 | +pub enum SeekMode { | |
| 49 | + /// The nearest random access point at or before the target, from which a | |
| 50 | + /// decoder reaches the target exactly by decoding forward. | |
| 51 | + #[default] | |
| 52 | + Accurate, | |
| 53 | + /// Whatever is cheapest. | |
| 54 | + Coarse, | |
| 55 | +} | |
| 56 | + | |
| 57 | +/// Where to seek to. | |
| 58 | +#[derive(Debug, Clone, Copy, PartialEq)] | |
| 59 | +pub enum SeekTo { | |
| 60 | + /// An instant, on `track_id` or on the default track. | |
| 61 | + Time { | |
| 62 | + /// The instant. | |
| 63 | + time: Time, | |
| 64 | + /// Which track's timeline it is on. | |
| 65 | + track_id: Option<u32>, | |
| 66 | + }, | |
| 67 | + /// A timestamp in a track's own base. | |
| 68 | + TimeStamp { | |
| 69 | + /// The timestamp. | |
| 70 | + ts: Timestamp, | |
| 71 | + /// Which track it belongs to. | |
| 72 | + track_id: u32, | |
| 73 | + }, | |
| 74 | +} | |
| 75 | + | |
| 76 | +/// Where a seek actually landed. | |
| 77 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 78 | +pub struct SeekedTo { | |
| 79 | + /// Track the landing is on. | |
| 80 | + pub track_id: u32, | |
| 81 | + /// What was asked for. | |
| 82 | + pub required_ts: Timestamp, | |
| 83 | + /// What was reached — at or before `required_ts` in [`SeekMode::Accurate`]. | |
| 84 | + pub actual_ts: Timestamp, | |
| 85 | +} | |
| 86 | + | |
| 87 | +/// A container reader. | |
| 88 | +pub trait FormatReader: Send { | |
| 89 | + /// Every track the container declares. | |
| 90 | + fn tracks(&self) -> &[Track]; | |
| 91 | + | |
| 92 | + /// The first track of `kind`. | |
| 93 | + fn default_track(&self, kind: TrackType) -> Option<&Track>; | |
| 94 | + | |
| 95 | + /// Seek, answering where it landed. | |
| 96 | + fn seek(&mut self, mode: SeekMode, to: SeekTo) -> Result<SeekedTo>; | |
| 97 | + | |
| 98 | + /// The next packet, or `Ok(None)` at the end of the stream. | |
| 99 | + fn next_packet(&mut self) -> Result<Option<Packet>>; | |
| 100 | +} | |
| 101 | + | |
| 102 | +/// Format hints from outside the bytes. | |
| 103 | +pub mod probe { | |
| 104 | + /// What the caller knows that the content does not say: a file extension, | |
| 105 | + /// a MIME type. Advisory — every reader in this family sniffs the content | |
| 106 | + /// first and is free to disagree. | |
| 107 | + #[derive(Debug, Clone, Default, PartialEq, Eq)] | |
| 108 | + pub struct Hint { | |
| 109 | + extension: Option<String>, | |
| 110 | + mime_type: Option<String>, | |
| 111 | + } | |
| 112 | + | |
| 113 | + impl Hint { | |
| 114 | + /// A hint that says nothing. | |
| 115 | + pub fn new() -> Hint { | |
| 116 | + Hint::default() | |
| 117 | + } | |
| 118 | + | |
| 119 | + /// Name the file extension, without its dot. | |
| 120 | + pub fn with_extension(&mut self, extension: &str) -> &mut Hint { | |
| 121 | + self.extension = Some(extension.to_string()); | |
| 122 | + self | |
| 123 | + } | |
| 124 | + | |
| 125 | + /// Name the MIME type. | |
| 126 | + pub fn mime_type(&mut self, mime: &str) -> &mut Hint { | |
| 127 | + self.mime_type = Some(mime.to_string()); | |
| 128 | + self | |
| 129 | + } | |
| 130 | + | |
| 131 | + /// The extension, if one was given. | |
| 132 | + pub fn extension(&self) -> Option<&str> { | |
| 133 | + self.extension.as_deref() | |
| 134 | + } | |
| 135 | + } | |
| 136 | +} |
shims/symphonia-core/src/io.rsadded+50 -0
| @@ -0,0 +1,50 @@ | ||
| 1 | +//! The source a reader reads from. | |
| 2 | + | |
| 3 | +use std::io::{Read, Seek}; | |
| 4 | + | |
| 5 | +/// Anything a reader can be built over: seekable, readable, and movable to the | |
| 6 | +/// thread that will do the reading. | |
| 7 | +pub trait MediaSource: Read + Seek + Send {} | |
| 8 | + | |
| 9 | +impl<T: Read + Seek + Send> MediaSource for T {} | |
| 10 | + | |
| 11 | +/// Options a stream is built with; nothing here has any. | |
| 12 | +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] | |
| 13 | +pub struct MediaSourceStreamOptions { | |
| 14 | + /// Buffer size a caller would like. This shim buffers in the demuxers | |
| 15 | + /// themselves, so it is advisory. | |
| 16 | + pub buffer_len: usize, | |
| 17 | +} | |
| 18 | + | |
| 19 | +/// A source with a buffer in front of it. | |
| 20 | +pub struct MediaSourceStream { | |
| 21 | + inner: Box<dyn MediaSource>, | |
| 22 | +} | |
| 23 | + | |
| 24 | +impl MediaSourceStream { | |
| 25 | + /// Wrap a source. | |
| 26 | + pub fn new( | |
| 27 | + source: Box<dyn MediaSource>, | |
| 28 | + _options: MediaSourceStreamOptions, | |
| 29 | + ) -> MediaSourceStream { | |
| 30 | + MediaSourceStream { inner: source } | |
| 31 | + } | |
| 32 | + | |
| 33 | + /// Take the source back out, which is how a reader in this family is | |
| 34 | + /// handed the file it will read. | |
| 35 | + pub fn into_inner(self) -> Box<dyn MediaSource> { | |
| 36 | + self.inner | |
| 37 | + } | |
| 38 | +} | |
| 39 | + | |
| 40 | +impl Read for MediaSourceStream { | |
| 41 | + fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | |
| 42 | + self.inner.read(buf) | |
| 43 | + } | |
| 44 | +} | |
| 45 | + | |
| 46 | +impl Seek for MediaSourceStream { | |
| 47 | + fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> { | |
| 48 | + self.inner.seek(pos) | |
| 49 | + } | |
| 50 | +} |
shims/symphonia-core/src/lib.rsadded+77 -0
| @@ -0,0 +1,77 @@ | ||
| 1 | +//! Compatibility shim: the `symphonia-core` 0.6 surface edith consumes, over | |
| 2 | +//! [`ec_core`] and (through the `symphonia` shim) [`ec_probe`]. | |
| 3 | +//! | |
| 4 | +//! This is the vocabulary half — parameters, packets, units, the reader and | |
| 5 | +//! decoder traits. The registries that build them live in the `symphonia` | |
| 6 | +//! shim, and the AAC decoder seat in `symphonia-codec-aac`, exactly as the | |
| 7 | +//! incumbent splits them. | |
| 8 | +//! | |
| 9 | +//! Written from the signatures edith calls (`crates/engine/src/audio.rs` | |
| 10 | +//! lines 36-53 and their call sites) and the crate's published documentation. | |
| 11 | +//! No symphonia source was read: the incumbent is MPL-2.0 and this family is | |
| 12 | +//! MIT OR Apache-2.0. | |
| 13 | +//! | |
| 14 | +//! **Not the whole crate.** What edith does not call is not here: metadata | |
| 15 | +//! revisions, the sample-buffer zoo, `Signal`/`AudioBuffer` generics, the | |
| 16 | +//! `CodecRegistry` builder API. Each is a small addition when something needs | |
| 17 | +//! it; none is a silent omission. | |
| 18 | + | |
| 19 | +#![forbid(unsafe_code)] | |
| 20 | + | |
| 21 | +pub mod codecs; | |
| 22 | +pub mod formats; | |
| 23 | +pub mod io; | |
| 24 | +pub mod meta; | |
| 25 | +pub mod packet; | |
| 26 | +pub mod units; | |
| 27 | + | |
| 28 | +use std::fmt; | |
| 29 | + | |
| 30 | +/// The incumbent's error type, in the shape callers match on. | |
| 31 | +#[derive(Debug)] | |
| 32 | +pub enum Error { | |
| 33 | + /// Underlying I/O failure. | |
| 34 | + IoError(std::io::Error), | |
| 35 | + /// The bitstream is malformed. | |
| 36 | + DecodeError(String), | |
| 37 | + /// A capability this build does not have, naming what and why. | |
| 38 | + Unsupported(String), | |
| 39 | + /// A seek that could not be served. | |
| 40 | + SeekError(String), | |
| 41 | + /// End of stream. | |
| 42 | + ResetRequired, | |
| 43 | +} | |
| 44 | + | |
| 45 | +/// `Result` with this crate's [`Error`]. | |
| 46 | +pub type Result<T> = std::result::Result<T, Error>; | |
| 47 | + | |
| 48 | +impl fmt::Display for Error { | |
| 49 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| 50 | + match self { | |
| 51 | + Error::IoError(e) => write!(f, "io error: {e}"), | |
| 52 | + Error::DecodeError(e) => write!(f, "decode error: {e}"), | |
| 53 | + Error::Unsupported(e) => write!(f, "unsupported: {e}"), | |
| 54 | + Error::SeekError(e) => write!(f, "seek error: {e}"), | |
| 55 | + Error::ResetRequired => write!(f, "decoder reset required"), | |
| 56 | + } | |
| 57 | + } | |
| 58 | +} | |
| 59 | + | |
| 60 | +impl std::error::Error for Error {} | |
| 61 | + | |
| 62 | +impl From<std::io::Error> for Error { | |
| 63 | + fn from(e: std::io::Error) -> Error { | |
| 64 | + Error::IoError(e) | |
| 65 | + } | |
| 66 | +} | |
| 67 | + | |
| 68 | +impl From<ec_core::Error> for Error { | |
| 69 | + fn from(e: ec_core::Error) -> Error { | |
| 70 | + match e { | |
| 71 | + ec_core::Error::Io(io) => Error::IoError(io), | |
| 72 | + ec_core::Error::Eof | ec_core::Error::NeedMore => Error::ResetRequired, | |
| 73 | + ec_core::Error::Unsupported { .. } => Error::Unsupported(e.to_string()), | |
| 74 | + ec_core::Error::Corrupt { .. } => Error::DecodeError(e.to_string()), | |
| 75 | + } | |
| 76 | + } | |
| 77 | +} |
shims/symphonia-core/src/meta.rsadded+12 -0
| @@ -0,0 +1,12 @@ | ||
| 1 | +//! Metadata options. | |
| 2 | +//! | |
| 3 | +//! edith passes `MetadataOptions::default()` and never reads a revision back, | |
| 4 | +//! so the tags a file carries are read by `ec_probe::Tags` and surfaced there | |
| 5 | +//! rather than through this type. | |
| 6 | + | |
| 7 | +/// Options metadata is read with. | |
| 8 | +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] | |
| 9 | +pub struct MetadataOptions { | |
| 10 | + /// Read cover art as well as text. Nothing here does. | |
| 11 | + pub read_visuals: bool, | |
| 12 | +} |
shims/symphonia-core/src/packet.rsadded+38 -0
| @@ -0,0 +1,38 @@ | ||
| 1 | +//! One packet of coded bytes. | |
| 2 | + | |
| 3 | +use crate::units::{Duration, Timestamp}; | |
| 4 | + | |
| 5 | +/// A packet as a reader hands it out. | |
| 6 | +#[derive(Debug, Clone, PartialEq, Eq)] | |
| 7 | +pub struct Packet { | |
| 8 | + /// Track this packet belongs to. | |
| 9 | + pub track_id: u32, | |
| 10 | + /// Presentation timestamp, in the track's own time base. | |
| 11 | + pub pts: Timestamp, | |
| 12 | + /// How long it plays for. | |
| 13 | + pub dur: Duration, | |
| 14 | + /// The coded bytes. | |
| 15 | + pub data: Box<[u8]>, | |
| 16 | +} | |
| 17 | + | |
| 18 | +impl Packet { | |
| 19 | + /// A packet over `data`. | |
| 20 | + pub fn new(track_id: u32, pts: Timestamp, dur: Duration, data: &[u8]) -> Packet { | |
| 21 | + Packet { | |
| 22 | + track_id, | |
| 23 | + pts, | |
| 24 | + dur, | |
| 25 | + data: data.to_vec().into_boxed_slice(), | |
| 26 | + } | |
| 27 | + } | |
| 28 | + | |
| 29 | + /// The presentation timestamp. | |
| 30 | + pub fn pts(&self) -> Timestamp { | |
| 31 | + self.pts | |
| 32 | + } | |
| 33 | + | |
| 34 | + /// The coded bytes. | |
| 35 | + pub fn buf(&self) -> &[u8] { | |
| 36 | + &self.data | |
| 37 | + } | |
| 38 | +} |
shims/symphonia-core/src/units.rsadded+134 -0
| @@ -0,0 +1,134 @@ | ||
| 1 | +//! Time, timestamps and the rational base between them. | |
| 2 | + | |
| 3 | +/// A media timestamp, in [`TimeBase`] ticks. | |
| 4 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)] | |
| 5 | +pub struct Timestamp(pub i64); | |
| 6 | + | |
| 7 | +impl Timestamp { | |
| 8 | + /// A timestamp of `ticks`. | |
| 9 | + pub fn new(ticks: i64) -> Timestamp { | |
| 10 | + Timestamp(ticks) | |
| 11 | + } | |
| 12 | + | |
| 13 | + /// The tick count. | |
| 14 | + pub fn value(&self) -> i64 { | |
| 15 | + self.0 | |
| 16 | + } | |
| 17 | +} | |
| 18 | + | |
| 19 | +/// A duration in [`TimeBase`] ticks. | |
| 20 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)] | |
| 21 | +pub struct Duration(pub u64); | |
| 22 | + | |
| 23 | +impl Duration { | |
| 24 | + /// A duration of `ticks`. | |
| 25 | + pub fn new(ticks: u64) -> Duration { | |
| 26 | + Duration(ticks) | |
| 27 | + } | |
| 28 | + | |
| 29 | + /// The tick count. | |
| 30 | + pub fn value(&self) -> u64 { | |
| 31 | + self.0 | |
| 32 | + } | |
| 33 | +} | |
| 34 | + | |
| 35 | +/// Wall-clock time, split so that no fraction is lost to a float. | |
| 36 | +#[derive(Debug, Clone, Copy, PartialEq, Default)] | |
| 37 | +pub struct Time { | |
| 38 | + /// Whole seconds. | |
| 39 | + pub seconds: u64, | |
| 40 | + /// Fraction of a second, 0.0..1.0. | |
| 41 | + pub frac: f64, | |
| 42 | +} | |
| 43 | + | |
| 44 | +impl Time { | |
| 45 | + /// A time from whole seconds and a fraction. | |
| 46 | + pub fn new(seconds: u64, frac: f64) -> Time { | |
| 47 | + Time { seconds, frac } | |
| 48 | + } | |
| 49 | + | |
| 50 | + /// A time from seconds, or [`None`] for a negative or non-finite input — | |
| 51 | + /// which is what makes a caller check before seeking to it. | |
| 52 | + pub fn try_from_secs_f64(secs: f64) -> Option<Time> { | |
| 53 | + if !secs.is_finite() || secs < 0.0 { | |
| 54 | + return None; | |
| 55 | + } | |
| 56 | + let seconds = secs.trunc() as u64; | |
| 57 | + Some(Time { | |
| 58 | + seconds, | |
| 59 | + frac: secs - secs.trunc(), | |
| 60 | + }) | |
| 61 | + } | |
| 62 | + | |
| 63 | + /// This time in seconds. | |
| 64 | + pub fn as_secs_f64(&self) -> f64 { | |
| 65 | + self.seconds as f64 + self.frac | |
| 66 | + } | |
| 67 | +} | |
| 68 | + | |
| 69 | +/// A rational tick duration: `numer / denom` seconds. | |
| 70 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] | |
| 71 | +pub struct TimeBase { | |
| 72 | + /// Numerator, in seconds. | |
| 73 | + pub numer: u32, | |
| 74 | + /// Denominator. | |
| 75 | + pub denom: u32, | |
| 76 | +} | |
| 77 | + | |
| 78 | +impl TimeBase { | |
| 79 | + /// A time base of `numer / denom` seconds per tick. | |
| 80 | + pub fn new(numer: u32, denom: u32) -> TimeBase { | |
| 81 | + TimeBase { numer, denom } | |
| 82 | + } | |
| 83 | + | |
| 84 | + /// `ts` as wall-clock time; a base with a zero denominator answers zero | |
| 85 | + /// rather than dividing by it. | |
| 86 | + pub fn calc_time_saturating(&self, ts: Timestamp) -> Time { | |
| 87 | + if self.denom == 0 { | |
| 88 | + return Time::default(); | |
| 89 | + } | |
| 90 | + let secs = ts.0 as f64 * f64::from(self.numer) / f64::from(self.denom); | |
| 91 | + Time::try_from_secs_f64(secs).unwrap_or_default() | |
| 92 | + } | |
| 93 | + | |
| 94 | + /// The timestamp `time` falls on in this base. | |
| 95 | + pub fn calc_timestamp(&self, time: Time) -> Timestamp { | |
| 96 | + if self.numer == 0 { | |
| 97 | + return Timestamp::default(); | |
| 98 | + } | |
| 99 | + Timestamp((time.as_secs_f64() * f64::from(self.denom) / f64::from(self.numer)) as i64) | |
| 100 | + } | |
| 101 | +} | |
| 102 | + | |
| 103 | +impl From<ec_core::TimeBase> for TimeBase { | |
| 104 | + fn from(tb: ec_core::TimeBase) -> TimeBase { | |
| 105 | + TimeBase::new(tb.num().max(0) as u32, tb.den().max(1) as u32) | |
| 106 | + } | |
| 107 | +} | |
| 108 | + | |
| 109 | +impl From<TimeBase> for ec_core::TimeBase { | |
| 110 | + fn from(tb: TimeBase) -> ec_core::TimeBase { | |
| 111 | + ec_core::TimeBase::new(i64::from(tb.numer.max(1)), i64::from(tb.denom.max(1))) | |
| 112 | + } | |
| 113 | +} | |
| 114 | + | |
| 115 | +#[cfg(test)] | |
| 116 | +mod tests { | |
| 117 | + use super::*; | |
| 118 | + | |
| 119 | + #[test] | |
| 120 | + fn a_timestamp_round_trips_through_its_base() { | |
| 121 | + let base = TimeBase::new(1, 48_000); | |
| 122 | + let time = base.calc_time_saturating(Timestamp::new(96_000)); | |
| 123 | + assert_eq!(time.as_secs_f64(), 2.0); | |
| 124 | + assert_eq!(base.calc_timestamp(time), Timestamp::new(96_000)); | |
| 125 | + // A negative instant is not a time, and says so rather than wrapping. | |
| 126 | + assert_eq!(Time::try_from_secs_f64(-1.0), None); | |
| 127 | + assert_eq!( | |
| 128 | + TimeBase::new(1, 0) | |
| 129 | + .calc_time_saturating(Timestamp::new(5)) | |
| 130 | + .as_secs_f64(), | |
| 131 | + 0.0 | |
| 132 | + ); | |
| 133 | + } | |
| 134 | +} |
shims/symphonia/Cargo.tomladded+16 -0
| @@ -0,0 +1,16 @@ | ||
| 1 | +[package] | |
| 2 | +name = "symphonia" | |
| 3 | +description = "Compatibility shim: the symphonia 0.6 umbrella surface edith consumes, served by ec-probe." | |
| 4 | +version = "0.6.0" | |
| 5 | +edition = "2024" | |
| 6 | +license = "MIT OR Apache-2.0" | |
| 7 | +authors = ["Ahmet Kaan Gümüş"] | |
| 8 | +publish = false | |
| 9 | + | |
| 10 | +[dependencies] | |
| 11 | +ec-core = { path = "../../crates/ec-core", version = "0.1.0" } | |
| 12 | +ec-probe = { path = "../../crates/ec-probe", version = "0.1.0" } | |
| 13 | +symphonia-core = { path = "../symphonia-core", version = "0.6.0" } | |
| 14 | + | |
| 15 | +[dev-dependencies] | |
| 16 | +symphonia-codec-aac = { path = "../symphonia-codec-aac", version = "0.6.0" } |
shims/symphonia/src/lib.rsadded+137 -0
| @@ -0,0 +1,137 @@ | ||
| 1 | +//! Compatibility shim: the `symphonia` 0.6 umbrella surface edith consumes, | |
| 2 | +//! served by [`ec_probe`]. | |
| 3 | +//! | |
| 4 | +//! Two registries, exactly as the incumbent has them: | |
| 5 | +//! [`default::get_probe`] opens a file and [`default::get_codecs`] builds a | |
| 6 | +//! decoder for one of its tracks. Underneath, both are `ec_probe`: one sniffing | |
| 7 | +//! reader over WAV, MP3, FLAC, ADTS, Ogg, mp4 and Matroska, and one decoder | |
| 8 | +//! seat over the family's codecs. | |
| 9 | +//! | |
| 10 | +//! Two behaviours differ from the incumbent, deliberately, because they are | |
| 11 | +//! defects edith works around today: | |
| 12 | +//! | |
| 13 | +//! - **A seek is reliable more than once per reader.** The incumbent's mkv | |
| 14 | +//! seek neither clears its queued frames nor recovers its element iterator, | |
| 15 | +//! so edith opens a *new reader per seek attempt* | |
| 16 | +//! (`crates/engine/src/audio.rs`, `SymTrack::seek_to`). Here every seek is | |
| 17 | +//! served on the one reader the file was opened with, and the landing it | |
| 18 | +//! reports is the next packet's own timestamp. | |
| 19 | +//! - **A multichannel AAC track decodes.** The incumbent refuses anything | |
| 20 | +//! wider than stereo (`aac: aac too complex`), which is exactly a film's 5.1; | |
| 21 | +//! [`ec_aac`] decodes to 7.1, so `make_audio_decoder` does not refuse it. | |
| 22 | +//! | |
| 23 | +//! Written from the signatures edith calls and the crate's published | |
| 24 | +//! documentation; no symphonia source was read (it is MPL-2.0, this family is | |
| 25 | +//! MIT OR Apache-2.0). | |
| 26 | + | |
| 27 | +#![forbid(unsafe_code)] | |
| 28 | + | |
| 29 | +pub use symphonia_core as core; | |
| 30 | + | |
| 31 | +mod reader; | |
| 32 | + | |
| 33 | +use symphonia_core::codecs::audio::{ | |
| 34 | + AudioCodecId, AudioCodecParameters, AudioDecoder, AudioDecoderOptions, well_known::*, | |
| 35 | +}; | |
| 36 | +use symphonia_core::codecs::{ | |
| 37 | + AudioDecoderRegistration, CodecDescriptor, CodecInfo, ec_codec_id, ec_parameters, | |
| 38 | +}; | |
| 39 | +use symphonia_core::formats::{FormatOptions, FormatReader, probe::Hint}; | |
| 40 | +use symphonia_core::io::MediaSourceStream; | |
| 41 | +use symphonia_core::meta::MetadataOptions; | |
| 42 | +use symphonia_core::{Error, Result}; | |
| 43 | + | |
| 44 | +pub use reader::ProbeReader; | |
| 45 | + | |
| 46 | +/// The registries, as the incumbent hands them out. | |
| 47 | +pub mod default { | |
| 48 | + use super::{CodecRegistry, Probe}; | |
| 49 | + | |
| 50 | + /// The format probe: content-sniffing, extension-hinted. | |
| 51 | + pub fn get_probe() -> &'static Probe { | |
| 52 | + &Probe | |
| 53 | + } | |
| 54 | + | |
| 55 | + /// The codec registry. | |
| 56 | + pub fn get_codecs() -> &'static CodecRegistry { | |
| 57 | + &CodecRegistry | |
| 58 | + } | |
| 59 | +} | |
| 60 | + | |
| 61 | +/// Opens a media source as whatever it turns out to be. | |
| 62 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 63 | +pub struct Probe; | |
| 64 | + | |
| 65 | +impl Probe { | |
| 66 | + /// Open `source`, using `hint` only where the content says nothing. | |
| 67 | + pub fn probe( | |
| 68 | + &self, | |
| 69 | + hint: &Hint, | |
| 70 | + source: MediaSourceStream, | |
| 71 | + _format: FormatOptions, | |
| 72 | + _metadata: MetadataOptions, | |
| 73 | + ) -> Result<Box<dyn FormatReader>> { | |
| 74 | + let reader = ec_probe::Reader::new(source.into_inner(), hint.extension())?; | |
| 75 | + Ok(Box::new(ProbeReader::new(reader))) | |
| 76 | + } | |
| 77 | +} | |
| 78 | + | |
| 79 | +/// Which codecs have a decoder behind them. | |
| 80 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 81 | +pub struct CodecRegistry; | |
| 82 | + | |
| 83 | +/// Every codec this registry serves, with the name it reports. | |
| 84 | +const REGISTERED: &[(AudioCodecId, &str, &str)] = &[ | |
| 85 | + (CODEC_ID_AAC, "aac", "Advanced Audio Coding"), | |
| 86 | + (CODEC_ID_OPUS, "opus", "Opus"), | |
| 87 | + (CODEC_ID_VORBIS, "vorbis", "Vorbis"), | |
| 88 | + (CODEC_ID_FLAC, "flac", "Free Lossless Audio Codec"), | |
| 89 | + (CODEC_ID_MP3, "mp3", "MPEG-1/2 Audio Layer III"), | |
| 90 | + (CODEC_ID_ALAC, "alac", "Apple Lossless Audio Codec"), | |
| 91 | + (CODEC_ID_AC3, "ac3", "Dolby Digital"), | |
| 92 | + (CODEC_ID_EAC3, "eac3", "Dolby Digital Plus"), | |
| 93 | + (CODEC_ID_PCM_U8, "pcm_u8", "PCM unsigned 8-bit"), | |
| 94 | + (CODEC_ID_PCM_S16LE, "pcm_s16le", "PCM signed 16-bit LE"), | |
| 95 | + (CODEC_ID_PCM_S16BE, "pcm_s16be", "PCM signed 16-bit BE"), | |
| 96 | + (CODEC_ID_PCM_S24LE, "pcm_s24le", "PCM signed 24-bit LE"), | |
| 97 | + (CODEC_ID_PCM_S32LE, "pcm_s32le", "PCM signed 32-bit LE"), | |
| 98 | + (CODEC_ID_PCM_F32LE, "pcm_f32le", "PCM float 32-bit LE"), | |
| 99 | + // TrueHD and DTS are deliberately absent: nothing in this family decodes | |
| 100 | + // them, so a caller asking gets `None` and can say so. | |
| 101 | +]; | |
| 102 | + | |
| 103 | +impl CodecRegistry { | |
| 104 | + /// The registration for `codec`, or [`None`] when nothing decodes it. | |
| 105 | + pub fn get_audio_decoder(&self, codec: AudioCodecId) -> Option<AudioDecoderRegistration> { | |
| 106 | + REGISTERED | |
| 107 | + .iter() | |
| 108 | + .find(|(id, ..)| *id == codec) | |
| 109 | + .map(|&(id, short_name, long_name)| AudioDecoderRegistration { | |
| 110 | + codec: CodecDescriptor { | |
| 111 | + id, | |
| 112 | + info: CodecInfo { | |
| 113 | + short_name, | |
| 114 | + long_name, | |
| 115 | + }, | |
| 116 | + }, | |
| 117 | + }) | |
| 118 | + } | |
| 119 | + | |
| 120 | + /// Build a decoder for `params`. | |
| 121 | + pub fn make_audio_decoder( | |
| 122 | + &self, | |
| 123 | + params: &AudioCodecParameters, | |
| 124 | + _options: &AudioDecoderOptions, | |
| 125 | + ) -> Result<Box<dyn AudioDecoder>> { | |
| 126 | + if self.get_audio_decoder(params.codec).is_none() { | |
| 127 | + let named = ec_codec_id(params.codec).map_or("this codec", |c| c.name()); | |
| 128 | + return Err(Error::Unsupported(format!( | |
| 129 | + "{named}: no decoder for it exists in this family" | |
| 130 | + ))); | |
| 131 | + } | |
| 132 | + Ok(Box::new(reader::EcAudioDecoder::new( | |
| 133 | + ec_probe::AudioDecoder::new(&ec_parameters(params)?)?, | |
| 134 | + params.clone(), | |
| 135 | + ))) | |
| 136 | + } | |
| 137 | +} |
shims/symphonia/src/reader.rsadded+159 -0
| @@ -0,0 +1,159 @@ | ||
| 1 | +//! The reader and decoder seats: `ec_probe` behind the incumbent's traits. | |
| 2 | + | |
| 3 | +use symphonia_core::Result; | |
| 4 | +use symphonia_core::codecs::audio::{AudioCodecParameters, AudioDecoder, GenericAudioBufferRef}; | |
| 5 | +use symphonia_core::codecs::from_ec_parameters; | |
| 6 | +use symphonia_core::formats::{FormatReader, SeekMode, SeekTo, SeekedTo, Track, TrackType}; | |
| 7 | +use symphonia_core::packet::Packet; | |
| 8 | +use symphonia_core::units::{Duration, TimeBase, Timestamp}; | |
| 9 | + | |
| 10 | +use ec_core::registry::MediaType; | |
| 11 | + | |
| 12 | +/// One open file: the tracks it declares, and its packets. | |
| 13 | +/// | |
| 14 | +/// Track ids are the *container's* own — a Matroska `TrackNumber` — so a | |
| 15 | +/// caller that named one track by number gets that track back and not the one | |
| 16 | +/// that happened to be at the same index. | |
| 17 | +pub struct ProbeReader { | |
| 18 | + inner: ec_probe::Reader, | |
| 19 | + tracks: Vec<Track>, | |
| 20 | +} | |
| 21 | + | |
| 22 | +impl ProbeReader { | |
| 23 | + /// Wrap an open [`ec_probe::Reader`]. | |
| 24 | + pub fn new(inner: ec_probe::Reader) -> ProbeReader { | |
| 25 | + let tracks = inner | |
| 26 | + .streams() | |
| 27 | + .iter() | |
| 28 | + .map(|s| Track { | |
| 29 | + id: inner.native_id(s.index) as u32, | |
| 30 | + track_type: match s.params.codec.media_type() { | |
| 31 | + MediaType::Audio => TrackType::Audio, | |
| 32 | + MediaType::Video => TrackType::Video, | |
| 33 | + MediaType::Subtitle => TrackType::Subtitle, | |
| 34 | + }, | |
| 35 | + time_base: Some(TimeBase::from(s.time_base)), | |
| 36 | + num_frames: s.duration.map(|d| d.max(0) as u64), | |
| 37 | + start_ts: s.start_time.unwrap_or(0).max(0) as u64, | |
| 38 | + codec_params: Some(from_ec_parameters(&s.params)), | |
| 39 | + language: s.language.clone(), | |
| 40 | + }) | |
| 41 | + .collect(); | |
| 42 | + ProbeReader { inner, tracks } | |
| 43 | + } | |
| 44 | + | |
| 45 | + /// The stream index behind a track id. | |
| 46 | + fn stream_of(&self, id: u32) -> Option<u32> { | |
| 47 | + self.inner.stream_of_native(u64::from(id)) | |
| 48 | + } | |
| 49 | + | |
| 50 | + /// The time base of a track id. | |
| 51 | + fn base_of(&self, id: u32) -> ec_core::TimeBase { | |
| 52 | + self.stream_of(id) | |
| 53 | + .and_then(|s| self.inner.streams().iter().find(|i| i.index == s)) | |
| 54 | + .map(|s| s.time_base) | |
| 55 | + .unwrap_or(ec_core::TimeBase::MILLIS) | |
| 56 | + } | |
| 57 | +} | |
| 58 | + | |
| 59 | +impl FormatReader for ProbeReader { | |
| 60 | + fn tracks(&self) -> &[Track] { | |
| 61 | + &self.tracks | |
| 62 | + } | |
| 63 | + | |
| 64 | + fn default_track(&self, kind: TrackType) -> Option<&Track> { | |
| 65 | + self.tracks.iter().find(|t| t.track_type == kind) | |
| 66 | + } | |
| 67 | + | |
| 68 | + fn seek(&mut self, mode: SeekMode, to: SeekTo) -> Result<SeekedTo> { | |
| 69 | + let (time, track_id) = match to { | |
| 70 | + SeekTo::Time { time, track_id } => (time.as_secs_f64(), track_id), | |
| 71 | + SeekTo::TimeStamp { ts, track_id } => { | |
| 72 | + let base = self.base_of(track_id); | |
| 73 | + (ts.value() as f64 * base.as_secs_f64(), Some(track_id)) | |
| 74 | + } | |
| 75 | + }; | |
| 76 | + let id = track_id | |
| 77 | + .or_else(|| self.default_track(TrackType::Audio).map(|t| t.id)) | |
| 78 | + .unwrap_or(0); | |
| 79 | + let stream = self.stream_of(id).unwrap_or(0); | |
| 80 | + let base = self.base_of(id); | |
| 81 | + let ticks = (time / base.as_secs_f64()) as i64; | |
| 82 | + let landed = self.inner.seek( | |
| 83 | + stream, | |
| 84 | + ec_core::Timestamp::new(ticks, base), | |
| 85 | + match mode { | |
| 86 | + // Both modes land at or before the target: it is the only | |
| 87 | + // landing a decoder can reach the exact sample from. | |
| 88 | + SeekMode::Accurate | SeekMode::Coarse => ec_core::SeekMode::SyncBefore, | |
| 89 | + }, | |
| 90 | + )?; | |
| 91 | + Ok(SeekedTo { | |
| 92 | + track_id: id, | |
| 93 | + required_ts: Timestamp::new(ticks), | |
| 94 | + actual_ts: Timestamp::new(landed.ticks), | |
| 95 | + }) | |
| 96 | + } | |
| 97 | + | |
| 98 | + fn next_packet(&mut self) -> Result<Option<Packet>> { | |
| 99 | + match self.inner.next_packet() { | |
| 100 | + Ok(packet) => { | |
| 101 | + let id = self.inner.native_id(packet.stream) as u32; | |
| 102 | + Ok(Some(Packet::new( | |
| 103 | + id, | |
| 104 | + Timestamp::new(packet.pts.unwrap_or(0)), | |
| 105 | + Duration::new(packet.duration.unwrap_or(0).max(0) as u64), | |
| 106 | + &packet.data, | |
| 107 | + ))) | |
| 108 | + } | |
| 109 | + Err(e) if e.is_eof() => Ok(None), | |
| 110 | + Err(e) => Err(e.into()), | |
| 111 | + } | |
| 112 | + } | |
| 113 | +} | |
| 114 | + | |
| 115 | +/// One decoder seat over [`ec_probe::AudioDecoder`]. | |
| 116 | +pub(crate) struct EcAudioDecoder { | |
| 117 | + inner: ec_probe::AudioDecoder, | |
| 118 | + params: AudioCodecParameters, | |
| 119 | + out: Vec<f32>, | |
| 120 | +} | |
| 121 | + | |
| 122 | +impl EcAudioDecoder { | |
| 123 | + pub(crate) fn new( | |
| 124 | + inner: ec_probe::AudioDecoder, | |
| 125 | + params: AudioCodecParameters, | |
| 126 | + ) -> EcAudioDecoder { | |
| 127 | + EcAudioDecoder { | |
| 128 | + inner, | |
| 129 | + params, | |
| 130 | + out: Vec::new(), | |
| 131 | + } | |
| 132 | + } | |
| 133 | +} | |
| 134 | + | |
| 135 | +impl AudioDecoder for EcAudioDecoder { | |
| 136 | + fn decode(&mut self, packet: &Packet) -> Result<GenericAudioBufferRef<'_>> { | |
| 137 | + let rate = self.inner.sample_rate().max(1); | |
| 138 | + let mut ec = ec_core::Packet::new( | |
| 139 | + packet.track_id, | |
| 140 | + ec_core::TimeBase::from_rate(rate), | |
| 141 | + packet.data.as_ref(), | |
| 142 | + ); | |
| 143 | + ec.pts = Some(packet.pts.value()); | |
| 144 | + ec.duration = Some(packet.dur.value() as i64); | |
| 145 | + self.inner.decode(&ec, &mut self.out)?; | |
| 146 | + Ok(GenericAudioBufferRef::new( | |
| 147 | + &self.out, | |
| 148 | + self.inner.channels().max(1), | |
| 149 | + )) | |
| 150 | + } | |
| 151 | + | |
| 152 | + fn codec_params(&self) -> &AudioCodecParameters { | |
| 153 | + &self.params | |
| 154 | + } | |
| 155 | + | |
| 156 | + fn reset(&mut self) { | |
| 157 | + self.inner.reset(); | |
| 158 | + } | |
| 159 | +} |
shims/symphonia/tests/edith_surface.rsadded+282 -0
| @@ -0,0 +1,282 @@ | ||
| 1 | +//! The shim driven exactly as edith drives symphonia. | |
| 2 | +//! | |
| 3 | +//! Every call here is copied in shape from `edith/crates/engine/src/audio.rs` | |
| 4 | +//! (the `SymTrack` reader at 1461-1668 and the `SymDecoder` at 1692-1740), so a | |
| 5 | +//! green run means the replica's audio path compiles and *works* against this | |
| 6 | +//! shim rather than merely type-checking against it. | |
| 7 | + | |
| 8 | +use std::fs::File; | |
| 9 | +use std::path::{Path, PathBuf}; | |
| 10 | + | |
| 11 | +use symphonia_codec_aac::AacDecoder; | |
| 12 | +use symphonia_core::codecs::CodecParameters; | |
| 13 | +use symphonia_core::codecs::audio::{ | |
| 14 | + AudioCodecParameters, AudioDecoder, AudioDecoderOptions, | |
| 15 | + well_known::{CODEC_ID_AAC, CODEC_ID_OPUS}, | |
| 16 | +}; | |
| 17 | +use symphonia_core::formats::probe::Hint; | |
| 18 | +use symphonia_core::formats::{ | |
| 19 | + FormatOptions, FormatReader, SeekMode, SeekTo, TrackType as SymKind, | |
| 20 | +}; | |
| 21 | +use symphonia_core::io::MediaSourceStream; | |
| 22 | +use symphonia_core::meta::MetadataOptions; | |
| 23 | +use symphonia_core::units::{Time, TimeBase, Timestamp}; | |
| 24 | + | |
| 25 | +fn fixtures() -> PathBuf { | |
| 26 | + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/audio") | |
| 27 | +} | |
| 28 | + | |
| 29 | +/// edith's `sym_reader`, verbatim in shape. | |
| 30 | +fn sym_reader(path: &Path) -> Box<dyn FormatReader> { | |
| 31 | + let mss = MediaSourceStream::new( | |
| 32 | + Box::new(File::open(path).expect("open")), | |
| 33 | + Default::default(), | |
| 34 | + ); | |
| 35 | + let mut hint = Hint::new(); | |
| 36 | + if let Some(ext) = path.extension().and_then(|e| e.to_str()) { | |
| 37 | + hint.with_extension(ext); | |
| 38 | + } | |
| 39 | + symphonia::default::get_probe() | |
| 40 | + .probe( | |
| 41 | + &hint, | |
| 42 | + mss, | |
| 43 | + FormatOptions::default(), | |
| 44 | + MetadataOptions::default(), | |
| 45 | + ) | |
| 46 | + .expect("probe") | |
| 47 | +} | |
| 48 | + | |
| 49 | +/// edith's `SymTrack::open_inner`, verbatim in shape. | |
| 50 | +struct SymTrack { | |
| 51 | + reader: Box<dyn FormatReader>, | |
| 52 | + track_id: u32, | |
| 53 | + sample_rate: u32, | |
| 54 | + channels: u16, | |
| 55 | + total_samples: Option<u64>, | |
| 56 | + time_base: TimeBase, | |
| 57 | + params: AudioCodecParameters, | |
| 58 | + codec: &'static str, | |
| 59 | +} | |
| 60 | + | |
| 61 | +impl SymTrack { | |
| 62 | + fn open(path: &Path) -> Option<SymTrack> { | |
| 63 | + let reader = sym_reader(path); | |
| 64 | + let track = reader.default_track(SymKind::Audio)?; | |
| 65 | + let (track_id, num_frames) = (track.id, track.num_frames); | |
| 66 | + let time_base = track.time_base.expect("a time base"); | |
| 67 | + let Some(CodecParameters::Audio(params)) = track.codec_params.clone() else { | |
| 68 | + panic!("no audio codec parameters"); | |
| 69 | + }; | |
| 70 | + let sample_rate = params.sample_rate.expect("a sample rate"); | |
| 71 | + let channels = params | |
| 72 | + .channels | |
| 73 | + .as_ref() | |
| 74 | + .map(|c| c.count() as u16) | |
| 75 | + .expect("a layout"); | |
| 76 | + let codec = symphonia::default::get_codecs() | |
| 77 | + .get_audio_decoder(params.codec) | |
| 78 | + .map_or("an unsupported format", |d| d.codec.info.short_name); | |
| 79 | + Some(SymTrack { | |
| 80 | + reader, | |
| 81 | + track_id, | |
| 82 | + sample_rate, | |
| 83 | + channels, | |
| 84 | + total_samples: num_frames, | |
| 85 | + time_base, | |
| 86 | + params, | |
| 87 | + codec, | |
| 88 | + }) | |
| 89 | + } | |
| 90 | + | |
| 91 | + /// edith's `SymTrack::decoder`, minus the two routes it takes away from | |
| 92 | + /// symphonia (multichannel AAC and Opus) — which is the point: this shim | |
| 93 | + /// serves them itself. | |
| 94 | + fn decoder(&self) -> Box<dyn AudioDecoder> { | |
| 95 | + if self.params.codec == CODEC_ID_AAC { | |
| 96 | + return Box::new( | |
| 97 | + AacDecoder::try_new(&self.params, &AudioDecoderOptions::default()).expect("aac"), | |
| 98 | + ); | |
| 99 | + } | |
| 100 | + symphonia::default::get_codecs() | |
| 101 | + .make_audio_decoder(&self.params, &AudioDecoderOptions::default()) | |
| 102 | + .expect("decoder") | |
| 103 | + } | |
| 104 | + | |
| 105 | + /// edith's `samples_at`. | |
| 106 | + fn samples_at(&self, ts: Timestamp) -> u64 { | |
| 107 | + let secs = self.time_base.calc_time_saturating(ts).as_secs_f64(); | |
| 108 | + (secs.max(0.0) * f64::from(self.sample_rate)) as u64 | |
| 109 | + } | |
| 110 | + | |
| 111 | + /// edith's `seek_to`, on **one** reader — the loop that reopens the file | |
| 112 | + /// per attempt exists because the incumbent's mkv seek works once. | |
| 113 | + fn seek_to(&mut self, secs: f64) -> f64 { | |
| 114 | + let time = Time::try_from_secs_f64(secs).expect("a time"); | |
| 115 | + let to = SeekTo::Time { | |
| 116 | + time, | |
| 117 | + track_id: Some(self.track_id), | |
| 118 | + }; | |
| 119 | + let landed = self.reader.seek(SeekMode::Accurate, to).expect("seek"); | |
| 120 | + self.time_base | |
| 121 | + .calc_time_saturating(landed.actual_ts) | |
| 122 | + .as_secs_f64() | |
| 123 | + } | |
| 124 | +} | |
| 125 | + | |
| 126 | +/// Open, describe, decode: what edith does for every standalone audio file and | |
| 127 | +/// for the sound of a Matroska one. | |
| 128 | +#[test] | |
| 129 | +fn every_file_opens_describes_and_decodes_through_the_shim() { | |
| 130 | + let files = [ | |
| 131 | + ("wav16-stereo-48000.wav", "pcm_s16le", 2), | |
| 132 | + ("flac-5.1-44100.flac", "flac", 6), | |
| 133 | + ("mp3-stereo-48000.mp3", "mp3", 2), | |
| 134 | + ("aac-adts-stereo-48000.aac", "aac", 2), | |
| 135 | + ("alac-mp4-stereo-44100.m4a", "alac", 2), | |
| 136 | + ("vorbis-ogg-stereo-48000.ogg", "vorbis", 2), | |
| 137 | + ("opus-ogg-5.1-48000.opus", "opus", 6), | |
| 138 | + ("aac-mka-stereo-48000.mka", "aac", 2), | |
| 139 | + ("av-h264-aac-stereo-48000.mkv", "aac", 2), | |
| 140 | + ]; | |
| 141 | + let mut checked = 0; | |
| 142 | + for (name, codec, channels) in files { | |
| 143 | + let path = fixtures().join(name); | |
| 144 | + if !path.exists() { | |
| 145 | + continue; | |
| 146 | + } | |
| 147 | + let mut track = SymTrack::open(&path).unwrap_or_else(|| panic!("{name}: no audio track")); | |
| 148 | + assert_eq!(track.codec, codec, "{name}: codec name"); | |
| 149 | + assert_eq!(track.channels, channels, "{name}: channels"); | |
| 150 | + assert!(track.total_samples.unwrap_or(0) > 0, "{name}: no duration"); | |
| 151 | + | |
| 152 | + let mut decoder = track.decoder(); | |
| 153 | + let mut out = Vec::new(); | |
| 154 | + let mut frames = 0u64; | |
| 155 | + let mut first_pts = None; | |
| 156 | + while let Some(packet) = track.reader.next_packet().expect("demux") { | |
| 157 | + if packet.track_id != track.track_id { | |
| 158 | + continue; | |
| 159 | + } | |
| 160 | + first_pts.get_or_insert_with(|| track.samples_at(packet.pts)); | |
| 161 | + decoder | |
| 162 | + .decode(&packet) | |
| 163 | + .expect("decode") | |
| 164 | + .copy_to_vec_interleaved::<f32>(&mut out); | |
| 165 | + frames += (out.len() / usize::from(track.channels)) as u64; | |
| 166 | + } | |
| 167 | + assert_eq!(first_pts, Some(0), "{name}: first packet is not at zero"); | |
| 168 | + let want = track.total_samples.unwrap_or(0); | |
| 169 | + assert!( | |
| 170 | + frames * 20 > want * 19, | |
| 171 | + "{name}: decoded {frames} of {want} frames" | |
| 172 | + ); | |
| 173 | + checked += 1; | |
| 174 | + } | |
| 175 | + assert!(checked > 0, "no fixtures — run scripts/gen-fixtures.sh"); | |
| 176 | + eprintln!("edith's symphonia surface: {checked} files opened and decoded"); | |
| 177 | +} | |
| 178 | + | |
| 179 | +/// The defect this shim exists to fix: the incumbent's Matroska seek is | |
| 180 | +/// reliable exactly once per reader, so edith opens a new reader per attempt. | |
| 181 | +/// Here twenty seeks in a row are served on one reader, each landing at or | |
| 182 | +/// before its target, and the audio still decodes from every landing. | |
| 183 | +#[test] | |
| 184 | +fn twenty_seeks_on_one_reader_all_land() { | |
| 185 | + for name in ["av-h264-aac-stereo-48000.mkv", "aac-mka-stereo-48000.mka"] { | |
| 186 | + let path = fixtures().join(name); | |
| 187 | + if !path.exists() { | |
| 188 | + continue; | |
| 189 | + } | |
| 190 | + let mut track = SymTrack::open(&path).expect("track"); | |
| 191 | + let mut decoder = track.decoder(); | |
| 192 | + let mut out = Vec::new(); | |
| 193 | + for i in 0..20 { | |
| 194 | + let target = 0.1 + (i as f64 * 0.13) % 2.5; | |
| 195 | + let landed = track.seek_to(target); | |
| 196 | + assert!( | |
| 197 | + landed <= target + 0.001, | |
| 198 | + "{name}: seek {i} to {target:.3}s landed at {landed:.3}s" | |
| 199 | + ); | |
| 200 | + let mut decoded = 0; | |
| 201 | + for _ in 0..8 { | |
| 202 | + let Some(packet) = track.reader.next_packet().expect("demux") else { | |
| 203 | + break; | |
| 204 | + }; | |
| 205 | + if packet.track_id != track.track_id { | |
| 206 | + continue; | |
| 207 | + } | |
| 208 | + decoder | |
| 209 | + .decode(&packet) | |
| 210 | + .expect("decode after a seek") | |
| 211 | + .copy_to_vec_interleaved::<f32>(&mut out); | |
| 212 | + decoded += out.len(); | |
| 213 | + } | |
| 214 | + assert!(decoded > 0, "{name}: nothing decoded after seek {i}"); | |
| 215 | + } | |
| 216 | + eprintln!("{name}: 20 seeks, one reader, every landing at or before its target"); | |
| 217 | + } | |
| 218 | +} | |
| 219 | + | |
| 220 | +/// The other incumbent defect: `aac: aac too complex` for anything wider than | |
| 221 | +/// stereo. A 5.1 AAC track decodes here, through the AAC shim, as 6 channels. | |
| 222 | +#[test] | |
| 223 | +fn multichannel_aac_decodes_rather_than_being_refused() { | |
| 224 | + let path = fixtures().join("aac-mp4-5.1-44100.mp4"); | |
| 225 | + if !path.exists() { | |
| 226 | + return; | |
| 227 | + } | |
| 228 | + let mut track = SymTrack::open(&path).expect("track"); | |
| 229 | + assert_eq!(track.channels, 6); | |
| 230 | + let mut decoder = track.decoder(); | |
| 231 | + let mut out = Vec::new(); | |
| 232 | + let packet = loop { | |
| 233 | + let packet = track | |
| 234 | + .reader | |
| 235 | + .next_packet() | |
| 236 | + .expect("demux") | |
| 237 | + .expect("a packet"); | |
| 238 | + if packet.track_id == track.track_id { | |
| 239 | + break packet; | |
| 240 | + } | |
| 241 | + }; | |
| 242 | + decoder | |
| 243 | + .decode(&packet) | |
| 244 | + .expect("5.1 AAC decodes") | |
| 245 | + .copy_to_vec_interleaved::<f32>(&mut out); | |
| 246 | + assert_eq!(out.len() % 6, 0); | |
| 247 | + assert!(out.len() >= 6 * 1024, "a 5.1 frame is 1024 samples wide"); | |
| 248 | +} | |
| 249 | + | |
| 250 | +/// Opus: the incumbent has no decoder for it at any version, and edith carries | |
| 251 | +/// a second crate for that alone. Here the registry answers. | |
| 252 | +#[test] | |
| 253 | +fn opus_has_a_decoder_in_the_registry() { | |
| 254 | + let registration = symphonia::default::get_codecs().get_audio_decoder(CODEC_ID_OPUS); | |
| 255 | + assert_eq!( | |
| 256 | + registration.map(|r| r.codec.info.short_name), | |
| 257 | + Some("opus"), | |
| 258 | + "the registry must name an Opus decoder" | |
| 259 | + ); | |
| 260 | + // ...and a TrueHD track has none, which is how a caller learns to say so. | |
| 261 | + let truehd = symphonia_core::codecs::audio::well_known::CODEC_ID_TRUEHD; | |
| 262 | + assert!( | |
| 263 | + symphonia::default::get_codecs() | |
| 264 | + .get_audio_decoder(truehd) | |
| 265 | + .is_none() | |
| 266 | + ); | |
| 267 | + let params = { | |
| 268 | + let mut p = AudioCodecParameters::new(); | |
| 269 | + p.for_codec(truehd).with_sample_rate(48_000); | |
| 270 | + p | |
| 271 | + }; | |
| 272 | + let refusal = match symphonia::default::get_codecs() | |
| 273 | + .make_audio_decoder(¶ms, &AudioDecoderOptions::default()) | |
| 274 | + { | |
| 275 | + Ok(_) => panic!("a TrueHD decoder was built out of nothing"), | |
| 276 | + Err(e) => e.to_string(), | |
| 277 | + }; | |
| 278 | + assert!( | |
| 279 | + refusal.contains("truehd"), | |
| 280 | + "the refusal names the codec: {refusal}" | |
| 281 | + ); | |
| 282 | +} |