edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·03dc6e9·Tahinli·3 months ago·178 commits
| 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 | } |