//! One decoder seat for every audio codec the family carries: packets in, //! interleaved `f32` out, in film channel order. //! //! This is the registry half of the probe. A caller that has a stream from //! [`crate::Reader`] asks for [`AudioDecoder::new`] and gets either a decoder //! or an [`ec_core::Error::Unsupported`] that names the codec *and* why nothing //! here decodes it — a refusal string with no capability behind it is a bug, //! not a message. use ec_core::error::{Error, Result}; use ec_core::frame::{Frame, SampleFormat}; use ec_core::packet::Packet; use ec_core::registry::{CodecId, CodecParameters, Decoder}; /// Which family decoder is doing the work. enum Inner { Flac(Box), Mp3(Box), Vorbis(Box), Ac3(Box), Aac(Box), Alac(Box), Opus(Box), /// PCM needs no decoder, only a reading of the bytes. Pcm(CodecId), } /// A decoder for one audio stream. pub struct AudioDecoder { inner: Inner, codec: CodecId, channels: usize, sample_rate: u32, } impl std::fmt::Debug for AudioDecoder { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("AudioDecoder") .field("codec", &self.codec.name()) .field("channels", &self.channels) .field("sample_rate", &self.sample_rate) .finish() } } impl AudioDecoder { /// A decoder for the stream `params` describes. pub fn new(params: &CodecParameters) -> Result { let audio = params .audio() .ok_or_else(|| Error::corrupt("a decoder was asked for on a non-audio stream"))?; let channels = audio.layout.channel_count().max(1); let sample_rate = audio.sample_rate; let extradata = params.extradata.as_deref(); let inner = match params.codec { CodecId::Flac => { let mut params = params.clone(); params.extradata = flac_stream_info(extradata.unwrap_or_default()) .map(ec_core::packet::Buf::copy_from_slice); Inner::Flac(Box::new(ec_flac::FlacDecoder::new(params)?)) } CodecId::Mp3 => Inner::Mp3(Box::new(ec_mp3::Mp3Decoder::new(params.clone())?)), CodecId::Vorbis => { let data = extradata.ok_or_else(|| { Error::corrupt("a Vorbis stream with no header triplet in its extradata") })?; let headers = ec_ogg::xiph_unlace(data)?; Inner::Vorbis(Box::new(ec_vorbis::VorbisDecoder::new(&headers)?)) } CodecId::Ac3 | CodecId::EAc3 => Inner::Ac3(Box::new(ec_ac3::Ac3Decoder::new())), CodecId::Aac => { let decoder = match extradata { Some(asc) => ec_aac::AacDecoder::with_config_bytes(asc)?, // ADTS states its own configuration in every frame. None => ec_aac::AacDecoder::new(), }; Inner::Aac(Box::new(decoder)) } CodecId::Alac => Inner::Alac(Box::new(ec_alac::AlacDecoder::from_parameters( params.clone(), )?)), CodecId::Opus => { let head = extradata.unwrap_or_default(); let (streams, coupled, mapping) = opus_layout(head, channels)?; // 48 kHz whatever the container says: it is the only rate Opus // decodes at, and the rate its timestamps are in. Inner::Opus(Box::new(ec_opus::MultistreamDecoder::try_with_rate( 48_000, streams, coupled, &mapping, )?)) } codec if is_pcm(codec) => Inner::Pcm(codec), codec => { return Err(Error::unsupported( format!("the {} track", codec.name()), match codec.media_type() { ec_core::MediaType::Audio => { "no decoder for it exists in this family (yet)" } _ => "this is an audio probe; picture and subtitles decode elsewhere", }, )); } }; Ok(AudioDecoder { inner, codec: params.codec, channels, sample_rate: match params.codec { CodecId::Opus => 48_000, _ => sample_rate, }, }) } /// Which codec this is decoding. pub fn codec(&self) -> CodecId { self.codec } /// Channels the decoder emits. pub fn channels(&self) -> usize { self.channels } /// Rate the decoder emits at. pub fn sample_rate(&self) -> u32 { self.sample_rate } /// One packet in, interleaved `f32` out, normalised to -1.0..=1.0. /// /// `out` is cleared first: a packet that decodes to nothing (a header /// packet, a frame the decoder swallowed) leaves it empty rather than /// leaving the caller with the previous packet's audio. pub fn decode(&mut self, packet: &Packet, out: &mut Vec) -> Result<()> { out.clear(); match &mut self.inner { Inner::Flac(d) => drain(d.as_mut(), packet, out)?, Inner::Mp3(d) => drain(d.as_mut(), packet, out)?, Inner::Vorbis(d) => drain(d.as_mut(), packet, out)?, Inner::Ac3(d) => drain(d.as_mut(), packet, out)?, Inner::Aac(d) => { let audio = d.decode(&packet.data, packet.pts)?; self.channels = usize::from(audio.channels).max(1); if audio.sample_rate > 0 { self.sample_rate = audio.sample_rate; } out.extend_from_slice(&audio.samples); } Inner::Alac(d) => { let scale = match d.cookie().sample_format() { SampleFormat::S16 => 1.0 / 32768.0, _ => 1.0 / 2147483648.0, }; out.extend(d.decode(&packet.data)?.iter().map(|&s| s as f32 * scale)); } Inner::Opus(d) => { let samples = d.decode_packet(&packet.data)?; out.extend_from_slice(&samples); } Inner::Pcm(codec) => pcm(*codec, &packet.data, out), } Ok(()) } /// Drop everything buffered from before a seek. pub fn reset(&mut self) { match &mut self.inner { Inner::Flac(d) => d.reset(), Inner::Mp3(d) => d.reset(), Inner::Vorbis(d) => d.reset(), Inner::Ac3(d) => d.reset(), Inner::Aac(d) => **d = ec_aac::AacDecoder::new(), Inner::Alac(d) => d.reset(), Inner::Opus(d) => d.reset(), Inner::Pcm(_) => {} } } } /// The bare 34-byte `STREAMINFO` inside whatever a container calls FLAC /// extradata. /// /// Three shapes carry the same block: Matroska's `CodecPrivate` is the whole /// `fLaC` header chain, mp4's `dfLa` is a version word and then the blocks, /// and Ogg's first packet has a `\x7fFLAC` wrapper in front of that. Rather /// than encode three offsets, this looks for the one block header that can /// only be `STREAMINFO`: type 0, length 34. fn flac_stream_info(data: &[u8]) -> Option<&[u8]> { if data.len() == 34 { return Some(data); } for at in 0..data.len().saturating_sub(38).min(16) { let head = &data[at..at + 4]; let len = u32::from_be_bytes([0, head[1], head[2], head[3]]); if head[0] & 0x7f == 0 && len == 34 { return data.get(at + 4..at + 38); } } // Nothing recognisable: hand the bytes over as they came and let the // decoder read the frames' own headers instead. None } /// True for the PCM codec ids, which are read rather than decoded. fn is_pcm(codec: CodecId) -> bool { matches!( codec, CodecId::PcmU8 | CodecId::PcmS16Le | CodecId::PcmS16Be | CodecId::PcmS24Le | CodecId::PcmS32Le | CodecId::PcmF32Le ) } /// Push one packet through an [`ec_core::Decoder`] and take every frame it /// yields, converted to interleaved `f32`. fn drain(decoder: &mut dyn Decoder, packet: &Packet, out: &mut Vec) -> Result<()> { decoder.send_packet(packet)?; loop { match decoder.receive_frame() { Ok(Frame::Audio(frame)) => interleave(&frame, out), Ok(Frame::Video(_)) => { return Err(Error::corrupt("an audio decoder produced a picture")); } Err(e) if e.is_need_more() || e.is_eof() => return Ok(()), Err(e) => return Err(e), } } } /// One decoded frame appended to `out` as interleaved, normalised `f32`. fn interleave(frame: &ec_core::AudioFrame, out: &mut Vec) { let channels = frame.channels().max(1); let width = frame.format.bytes_per_sample(); let read = |plane: &[u8], i: usize| -> f32 { let at = i * width; let b = &plane[at..at + width]; match frame.format { SampleFormat::U8 => (f32::from(b[0]) - 128.0) / 128.0, SampleFormat::S16 => f32::from(i16::from_ne_bytes([b[0], b[1]])) / 32768.0, SampleFormat::S32 => i32::from_ne_bytes([b[0], b[1], b[2], b[3]]) as f32 / 2147483648.0, SampleFormat::F32 => f32::from_ne_bytes([b[0], b[1], b[2], b[3]]), SampleFormat::F64 => { f64::from_ne_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]) as f32 } } }; match frame.planar { false => { let Some(plane) = frame.data.first() else { return; }; let n = (plane.len() / width).min(frame.samples * channels); out.extend((0..n).map(|i| read(plane, i))); } true => { for i in 0..frame.samples { for c in 0..channels { let Some(plane) = frame.data.get(c) else { continue; }; if (i + 1) * width <= plane.len() { out.push(read(plane, i)); } } } } } } /// PCM bytes as normalised `f32`. fn pcm(codec: CodecId, data: &[u8], out: &mut Vec) { match codec { CodecId::PcmU8 => out.extend(data.iter().map(|&b| (f32::from(b) - 128.0) / 128.0)), CodecId::PcmS16Le => out.extend( data.chunks_exact(2) .map(|c| f32::from(i16::from_le_bytes([c[0], c[1]])) / 32768.0), ), CodecId::PcmS16Be => out.extend( data.chunks_exact(2) .map(|c| f32::from(i16::from_be_bytes([c[0], c[1]])) / 32768.0), ), // 24-bit is stored packed, little-endian, and sign-extends from bit 23. CodecId::PcmS24Le => out.extend(data.chunks_exact(3).map(|c| { let v = i32::from_le_bytes([0, c[0], c[1], c[2]]); v as f32 / 2147483648.0 })), CodecId::PcmS32Le => out.extend( data.chunks_exact(4) .map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]]) as f32 / 2147483648.0), ), CodecId::PcmF32Le => out.extend( data.chunks_exact(4) .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])), ), _ => {} } } /// The stream layout of an Opus track, read off its `OpusHead`. /// /// Mapping family 0 is the implied mono/stereo layout the header does not /// spell out; families 1 and 255 carry the table. A header too short to hold /// one is treated as family 0, which is what a `.opus` file with a truncated /// head still plays as. pub fn opus_layout(head: &[u8], channels: usize) -> Result<(usize, usize, Vec)> { let channels = channels.max(1); let family = head.get(18).copied().unwrap_or(0); if family == 0 || head.len() < 21 { let coupled = usize::from(channels >= 2); let mapping: Vec = (0..channels.min(2) as u8).collect(); return Ok((channels.min(2) - coupled, coupled, mapping)); } let streams = usize::from(head[19]); let coupled = usize::from(head[20]); let mapping = head .get(21..21 + channels) .ok_or_else(|| { Error::corrupt(format!( "an OpusHead claiming {channels} channels with a {}-byte mapping table", head.len().saturating_sub(21) )) })? .to_vec(); Ok((streams, coupled, mapping)) } /// The encoder delay an Opus track states in its `OpusHead`, in 48 kHz samples. pub fn opus_pre_skip(head: &[u8]) -> u64 { match head.get(10..12) { Some(b) => u64::from(u16::from_le_bytes([b[0], b[1]])), None => 0, } } #[cfg(test)] mod tests { use super::*; #[test] fn opus_layouts_come_off_the_head_or_are_implied() { // Family 0 stereo: one coupled stream, no table in the header. let head = b"OpusHead\x01\x02\x38\x01\x80\xbb\x00\x00\x00\x00\x00"; assert_eq!(opus_layout(head, 2).unwrap(), (1, 1, vec![0, 1])); assert_eq!(opus_layout(head, 1).unwrap(), (1, 0, vec![0])); assert_eq!(opus_pre_skip(head), 312); // Family 1, 5.1: four streams of which two are coupled. let mut six = head[..18].to_vec(); six.extend_from_slice(&[1, 4, 2, 0, 4, 1, 2, 3, 5]); assert_eq!( opus_layout(&six, 6).unwrap(), (4, 2, vec![0, 4, 1, 2, 3, 5]) ); // A table too short for the channel count is named, not guessed at. assert!(opus_layout(&six[..23], 6).is_err()); } #[test] fn pcm_reads_every_width_at_full_scale() { let mut out = Vec::new(); pcm(CodecId::PcmS16Le, &[0x00, 0x80, 0xff, 0x7f], &mut out); assert_eq!(out, vec![-1.0, 32767.0 / 32768.0]); out.clear(); pcm(CodecId::PcmU8, &[0, 128, 255], &mut out); assert_eq!(out[0], -1.0); assert_eq!(out[1], 0.0); out.clear(); // 24-bit sign-extends: 0x800000 is full-scale negative. pcm(CodecId::PcmS24Le, &[0x00, 0x00, 0x80], &mut out); assert_eq!(out, vec![-1.0]); } }