diff --git a/crates/ec-probe/src/decoder.rs b/crates/ec-probe/src/decoder.rs index fc06b628..931b10aa 100644 --- a/crates/ec-probe/src/decoder.rs +++ b/crates/ec-probe/src/decoder.rs @@ -26,7 +26,36 @@ enum Inner { AmrWb(Box), Opus(Box), /// PCM needs no decoder, only a reading of the bytes. - Pcm(CodecId), + Pcm(PcmFormat), +} + +/// The PCM layouts read straight from the packet bytes. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum PcmFormat { + U8, + S16Le, + S16Be, + S24Le, + S24Be, + S32Le, + S32Be, + F32Le, +} + +impl PcmFormat { + fn of(codec: CodecId) -> Option { + Some(match codec { + CodecId::PcmU8 => PcmFormat::U8, + CodecId::PcmS16Le => PcmFormat::S16Le, + CodecId::PcmS16Be => PcmFormat::S16Be, + CodecId::PcmS24Le => PcmFormat::S24Le, + CodecId::PcmS24Be => PcmFormat::S24Be, + CodecId::PcmS32Le => PcmFormat::S32Le, + CodecId::PcmS32Be => PcmFormat::S32Be, + CodecId::PcmF32Le => PcmFormat::F32Le, + _ => return None, + }) + } } /// A decoder for one audio stream. @@ -129,18 +158,20 @@ impl AudioDecoder { 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", - }, - )); - } + codec => match PcmFormat::of(codec) { + Some(format) => Inner::Pcm(format), + None => { + 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, @@ -213,7 +244,7 @@ impl AudioDecoder { let samples = d.decode_packet(&packet.data)?; out.extend_from_slice(&samples); } - Inner::Pcm(codec) => pcm(*codec, &packet.data, out), + Inner::Pcm(format) => pcm(*format, &packet.data, out), } // Vorbis trims to its own page granules inside `ec-vorbis`, so its // output is final as it arrives. Every other codec here is trimmed @@ -366,21 +397,6 @@ fn flac_stream_info(data: &[u8]) -> Option<&[u8]> { 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::PcmS24Be - | CodecId::PcmS32Le - | CodecId::PcmS32Be - | 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<()> { @@ -449,39 +465,38 @@ fn interleave(frame: &ec_core::AudioFrame, out: &mut Vec) { } /// 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( +fn pcm(format: PcmFormat, data: &[u8], out: &mut Vec) { + match format { + PcmFormat::U8 => out.extend(data.iter().map(|&b| (f32::from(b) - 128.0) / 128.0)), + PcmFormat::S16Le => out.extend( data.as_chunks::<2>().0.iter() .map(|c| f32::from(i16::from_le_bytes([c[0], c[1]])) / 32768.0), ), - CodecId::PcmS16Be => out.extend( + PcmFormat::S16Be => out.extend( data.as_chunks::<2>().0.iter() .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.as_chunks::<3>().0.iter().map(|c| { + PcmFormat::S24Le => out.extend(data.as_chunks::<3>().0.iter().map(|c| { let v = i32::from_le_bytes([0, c[0], c[1], c[2]]); v as f32 / 2147483648.0 })), - CodecId::PcmS24Be => out.extend(data.as_chunks::<3>().0.iter().map(|c| { + PcmFormat::S24Be => out.extend(data.as_chunks::<3>().0.iter().map(|c| { let v = i32::from_be_bytes([c[0], c[1], c[2], 0]); v as f32 / 2147483648.0 })), - CodecId::PcmS32Le => out.extend( + PcmFormat::S32Le => out.extend( data.as_chunks::<4>().0.iter() .map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]]) as f32 / 2147483648.0), ), - CodecId::PcmS32Be => out.extend( + PcmFormat::S32Be => out.extend( data.as_chunks::<4>().0.iter() .map(|c| i32::from_be_bytes([c[0], c[1], c[2], c[3]]) as f32 / 2147483648.0), ), - CodecId::PcmF32Le => out.extend( + PcmFormat::F32Le => out.extend( data.as_chunks::<4>().0.iter() .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])), ), - _ => {} } } @@ -579,15 +594,15 @@ mod tests { #[test] fn pcm_reads_every_width_at_full_scale() { let mut out = Vec::new(); - pcm(CodecId::PcmS16Le, &[0x00, 0x80, 0xff, 0x7f], &mut out); + pcm(PcmFormat::S16Le, &[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); + pcm(PcmFormat::U8, &[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); + pcm(PcmFormat::S24Le, &[0x00, 0x00, 0x80], &mut out); assert_eq!(out, vec![-1.0]); } }