From 74c238bc3cdd02b52a36195e1bf40c8167c2814b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 09:35:42 +0300 Subject: [PATCH] B5: ec-matroska PCM from BitDepth, refuse non-32 float A_PCM/INT/BIG always decoded as S16Be, so 24/32-bit BE tracks were read at 16 bits. A_PCM/FLOAT/IEEE ignored BitDepth, so 64-bit float decoded as f32. One pcm_codec(big, float, bits) now maps both; INT/BIG maps to S16Be, S24Be or S32Be, and float other than 32 bits is refused by name. BitDepth is read with u32::try_from instead of a truncating cast. Only BE and 64-bit float PCM tracks change output (toward the ffmpeg depth). Tests: cargo check and clippy ec-matroska only; orchestrator runs mkv tests and goldens. --- crates/ec-matroska/src/demux.rs | 73 ++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/crates/ec-matroska/src/demux.rs b/crates/ec-matroska/src/demux.rs index f420ba47..07b244dd 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,7 +1200,28 @@ 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 None, + _ => return Ok(None), + })) +} + +/// The PCM codec an `A_PCM/*` id and its `BitDepth` name. Matroska defines only +/// 32-bit float, and it is little-endian; any other float depth is refused. +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, }) } @@ -1333,11 +1349,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 +1397,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 +1925,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]