diff --git a/crates/ec-matroska/src/demux.rs b/crates/ec-matroska/src/demux.rs index f420ba47..2f82b8af 100644 --- a/crates/ec-matroska/src/demux.rs +++ b/crates/ec-matroska/src/demux.rs @@ -1166,8 +1166,8 @@ fn content_encoding(buf: &[u8], range: Range) -> Unpack { /// Which [`CodecId`] a Matroska codec id names. `A_PCM/*` needs the track's own /// `BitDepth`, which is why the depth comes in. -pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Option { - Some(match id { +pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Result> { + Ok(Some(match id { "V_MPEG4/ISO/AVC" => CodecId::H264, "V_MPEGH/ISO/HEVC" => CodecId::H265, // MPEG-4 Part 2 Visual; CodecPrivate holds the VOL header. @@ -1190,14 +1190,9 @@ pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Option { "S_HDMV/PGS" => CodecId::Pgs, "S_VOBSUB" => CodecId::DvdSub, "S_DVBSUB" => CodecId::DvbSub, - "A_PCM/FLOAT/IEEE" => CodecId::PcmF32Le, - "A_PCM/INT/BIG" => CodecId::PcmS16Be, - "A_PCM/INT/LIT" => match bit_depth { - 8 => CodecId::PcmU8, - 24 => CodecId::PcmS24Le, - 32 => CodecId::PcmS32Le, - _ => CodecId::PcmS16Le, - }, + "A_PCM/FLOAT/IEEE" => pcm_codec(false, true, bit_depth)?, + "A_PCM/INT/BIG" => pcm_codec(true, false, bit_depth)?, + "A_PCM/INT/LIT" => pcm_codec(false, false, bit_depth)?, // `A_AAC/MPEG4/LC/SBR` and its family all decode as AAC, and // `A_MPEG/L3` is the only MPEG layer this carries. id if id.starts_with("A_AAC") => CodecId::Aac, @@ -1205,6 +1200,39 @@ pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Option { // MPEG-1/2 Layer II (MP2) and Layer I (MP1): the same decoder as Layer III reads them. "A_MPEG/L2" => CodecId::Mp2, "A_MPEG/L1" => CodecId::Mp1, + _ => return Ok(None), + })) +} + +pub(crate) fn pcm_codec(big: bool, float: bool, bits: u32) -> Result { + Ok(match (float, big, bits) { + (true, _, 0 | 32) => CodecId::PcmF32Le, + (true, _, _) => { + return Err(Error::unsupported( + format!("{bits}-bit float PCM in Matroska"), + "only 32-bit float is defined", + )); + } + (false, false, 8) => CodecId::PcmU8, + (false, false, 24) => CodecId::PcmS24Le, + (false, false, 32) => CodecId::PcmS32Le, + (false, false, _) => CodecId::PcmS16Le, + (false, true, 24) => CodecId::PcmS24Be, + (false, true, 32) => CodecId::PcmS32Be, + (false, true, _) => CodecId::PcmS16Be, + }) +} + +pub(crate) fn pcm_bits(codec: CodecId) -> Option<(bool, bool, u32)> { + Some(match codec { + CodecId::PcmU8 => (false, false, 8), + CodecId::PcmS16Le => (false, false, 16), + CodecId::PcmS24Le => (false, false, 24), + CodecId::PcmS32Le => (false, false, 32), + CodecId::PcmS16Be => (true, false, 16), + CodecId::PcmS24Be => (true, false, 24), + CodecId::PcmS32Be => (true, false, 32), + CodecId::PcmF32Le => (false, true, 32), _ => return None, }) } @@ -1333,11 +1361,12 @@ pub(crate) fn parse_tracks( // Ticks, not nanoseconds: everything downstream counts in the file's own // time base and nothing here reintroduces a float clock. let step = default_duration.map(|ns| (ns / scale_ns.max(1)).max(1) as i64); - let codec_id = media - .filter(|_| kind != 0) - .and_then(|_| { - codec_of(&codec, bits as u32).or_else(|| video_codec_from_private(&codec, private.as_deref())) - }); + let depth = u32::try_from(bits).unwrap_or(u32::MAX); + let codec_id = match media.filter(|_| kind != 0) { + Some(_) => codec_of(&codec, depth)? + .or_else(|| video_codec_from_private(&codec, private.as_deref())), + None => None, + }; let stream = match (codec_id, media) { (Some(codec_id), Some(media)) => { if media == MediaType::Audio && channels > MAX_CHANNELS { @@ -1380,7 +1409,7 @@ pub(crate) fn parse_tracks( }, layout: ChannelLayout::from_count(channels.max(1) as usize), format: None, - bits_per_sample: (bits > 0).then_some(bits as u32), + bits_per_sample: (bits > 0).then_some(depth), }), MediaType::Subtitle => MediaParameters::Subtitle, }; @@ -1908,17 +1937,17 @@ mod tests { #[test] fn codec_ids_cover_the_families_the_family_carries() { - assert_eq!(codec_of("V_MPEGH/ISO/HEVC", 0), Some(CodecId::H265)); - assert_eq!(codec_of("A_AAC/MPEG4/LC/SBR", 0), Some(CodecId::Aac)); - assert_eq!(codec_of("A_PCM/INT/LIT", 24), Some(CodecId::PcmS24Le)); - assert_eq!(codec_of("A_PCM/INT/LIT", 16), Some(CodecId::PcmS16Le)); - assert_eq!(codec_of("S_HDMV/PGS", 0), Some(CodecId::Pgs)); - assert_eq!(codec_of("S_VOBSUB", 0), Some(CodecId::DvdSub)); - assert_eq!(codec_of("S_DVBSUB", 0), Some(CodecId::DvbSub)); - assert_eq!(codec_of("V_MJPEG", 0), Some(CodecId::Mjpeg)); - assert_eq!(codec_of("V_MPEG4/ISO/ASP", 0), Some(CodecId::Mpeg4Part2)); - assert_eq!(codec_of("V_MPEG4/ISO/SP", 0), Some(CodecId::Mpeg4Part2)); - assert_eq!(codec_of("V_MS/VFW/FOURCC", 0), None); + assert_eq!(codec_of("V_MPEGH/ISO/HEVC", 0).unwrap(), Some(CodecId::H265)); + assert_eq!(codec_of("A_AAC/MPEG4/LC/SBR", 0).unwrap(), Some(CodecId::Aac)); + assert_eq!(codec_of("A_PCM/INT/LIT", 24).unwrap(), Some(CodecId::PcmS24Le)); + assert_eq!(codec_of("A_PCM/INT/LIT", 16).unwrap(), Some(CodecId::PcmS16Le)); + assert_eq!(codec_of("S_HDMV/PGS", 0).unwrap(), Some(CodecId::Pgs)); + assert_eq!(codec_of("S_VOBSUB", 0).unwrap(), Some(CodecId::DvdSub)); + assert_eq!(codec_of("S_DVBSUB", 0).unwrap(), Some(CodecId::DvbSub)); + assert_eq!(codec_of("V_MJPEG", 0).unwrap(), Some(CodecId::Mjpeg)); + assert_eq!(codec_of("V_MPEG4/ISO/ASP", 0).unwrap(), Some(CodecId::Mpeg4Part2)); + assert_eq!(codec_of("V_MPEG4/ISO/SP", 0).unwrap(), Some(CodecId::Mpeg4Part2)); + assert_eq!(codec_of("V_MS/VFW/FOURCC", 0).unwrap(), None); } #[test] diff --git a/crates/ec-matroska/src/mux.rs b/crates/ec-matroska/src/mux.rs index 18a877d1..202a1d40 100644 --- a/crates/ec-matroska/src/mux.rs +++ b/crates/ec-matroska/src/mux.rs @@ -12,6 +12,7 @@ use ec_core::{ TimeBase, }; +use crate::demux::pcm_bits; use crate::ebml::{self, elem, elem_head_len, float, put_id, put_size, uint}; /// One millisecond, the tick every Matroska muxer writes and this one's block @@ -767,11 +768,16 @@ pub(crate) fn matroska_codec_id(codec: CodecId) -> Result<&'static str> { CodecId::Mp3 => "A_MPEG/L3", CodecId::Opus => "A_OPUS", CodecId::Vorbis => "A_VORBIS", - CodecId::PcmU8 | CodecId::PcmS16Le | CodecId::PcmS24Le | CodecId::PcmS32Le => { - "A_PCM/INT/LIT" - } - CodecId::PcmS16Be => "A_PCM/INT/BIG", - CodecId::PcmF32Le => "A_PCM/FLOAT/IEEE", + CodecId::PcmU8 + | CodecId::PcmS16Le + | CodecId::PcmS24Le + | CodecId::PcmS32Le + | CodecId::PcmS16Be + | CodecId::PcmF32Le => match pcm_bits(codec) { + Some((_, true, _)) => "A_PCM/FLOAT/IEEE", + Some((true, _, _)) => "A_PCM/INT/BIG", + _ => "A_PCM/INT/LIT", + }, CodecId::Srt => "S_TEXT/UTF8", CodecId::WebVtt => "S_TEXT/WEBVTT", CodecId::Ass => "S_TEXT/ASS", diff --git a/crates/ec-probe/src/decoder.rs b/crates/ec-probe/src/decoder.rs index 231d74a3..fc06b628 100644 --- a/crates/ec-probe/src/decoder.rs +++ b/crates/ec-probe/src/decoder.rs @@ -374,7 +374,9 @@ fn is_pcm(codec: CodecId) -> bool { | CodecId::PcmS16Le | CodecId::PcmS16Be | CodecId::PcmS24Le + | CodecId::PcmS24Be | CodecId::PcmS32Le + | CodecId::PcmS32Be | CodecId::PcmF32Le ) } @@ -463,10 +465,18 @@ fn pcm(codec: CodecId, data: &[u8], out: &mut Vec) { 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| { + let v = i32::from_be_bytes([c[0], c[1], c[2], 0]); + v as f32 / 2147483648.0 + })), CodecId::PcmS32Le => 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( + 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( data.as_chunks::<4>().0.iter() .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])),