forge
Explore
Log in

Don't you dare go Hollow

B5: ec-matroska PCM from BitDepth, refuse non-32 float

Browse files at this commit

74c238bc3cdd02b52a36195e1bf40c8167c2814bAhmet Kaan GümüşParent: 22f0f3f
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.rsmodified+45 -28
@@ -1166,8 +1166,8 @@ fn content_encoding(buf: &[u8], range: Range<usize>) -> Unpack {
1166 1166
1167 /// Which [`CodecId`] a Matroska codec id names. `A_PCM/*` needs the track's own1167 /// Which [`CodecId`] a Matroska codec id names. `A_PCM/*` needs the track's own
1168 /// `BitDepth`, which is why the depth comes in.1168 /// `BitDepth`, which is why the depth comes in.
1169-pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Option<CodecId> {1169+pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Result<Option<CodecId>> {
1170- Some(match id {1170+ Ok(Some(match id {
1171 "V_MPEG4/ISO/AVC" => CodecId::H264,1171 "V_MPEG4/ISO/AVC" => CodecId::H264,
1172 "V_MPEGH/ISO/HEVC" => CodecId::H265,1172 "V_MPEGH/ISO/HEVC" => CodecId::H265,
1173 // MPEG-4 Part 2 Visual; CodecPrivate holds the VOL header.1173 // 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<CodecId> {
1190 "S_HDMV/PGS" => CodecId::Pgs,1190 "S_HDMV/PGS" => CodecId::Pgs,
1191 "S_VOBSUB" => CodecId::DvdSub,1191 "S_VOBSUB" => CodecId::DvdSub,
1192 "S_DVBSUB" => CodecId::DvbSub,1192 "S_DVBSUB" => CodecId::DvbSub,
1193- "A_PCM/FLOAT/IEEE" => CodecId::PcmF32Le,1193+ "A_PCM/FLOAT/IEEE" => pcm_codec(false, true, bit_depth)?,
1194- "A_PCM/INT/BIG" => CodecId::PcmS16Be,1194+ "A_PCM/INT/BIG" => pcm_codec(true, false, bit_depth)?,
1195- "A_PCM/INT/LIT" => match bit_depth {1195+ "A_PCM/INT/LIT" => pcm_codec(false, false, bit_depth)?,
1196- 8 => CodecId::PcmU8,
1197- 24 => CodecId::PcmS24Le,
1198- 32 => CodecId::PcmS32Le,
1199- _ => CodecId::PcmS16Le,
1200- },
1201 // `A_AAC/MPEG4/LC/SBR` and its family all decode as AAC, and1196 // `A_AAC/MPEG4/LC/SBR` and its family all decode as AAC, and
1202 // `A_MPEG/L3` is the only MPEG layer this carries.1197 // `A_MPEG/L3` is the only MPEG layer this carries.
1203 id if id.starts_with("A_AAC") => CodecId::Aac,1198 id if id.starts_with("A_AAC") => CodecId::Aac,
@@ -1205,7 +1200,28 @@ pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Option<CodecId> {
1205 // MPEG-1/2 Layer II (MP2) and Layer I (MP1): the same decoder as Layer III reads them.1200 // MPEG-1/2 Layer II (MP2) and Layer I (MP1): the same decoder as Layer III reads them.
1206 "A_MPEG/L2" => CodecId::Mp2,1201 "A_MPEG/L2" => CodecId::Mp2,
1207 "A_MPEG/L1" => CodecId::Mp1,1202 "A_MPEG/L1" => CodecId::Mp1,
1208- _ => return None,1203+ _ => return Ok(None),
1204+ }))
1205+}
1206+
1207+/// The PCM codec an `A_PCM/*` id and its `BitDepth` name. Matroska defines only
1208+/// 32-bit float, and it is little-endian; any other float depth is refused.
1209+pub(crate) fn pcm_codec(big: bool, float: bool, bits: u32) -> Result<CodecId> {
1210+ Ok(match (float, big, bits) {
1211+ (true, _, 0 | 32) => CodecId::PcmF32Le,
1212+ (true, _, _) => {
1213+ return Err(Error::unsupported(
1214+ format!("{bits}-bit float PCM in Matroska"),
1215+ "only 32-bit float is defined",
1216+ ));
1217+ }
1218+ (false, false, 8) => CodecId::PcmU8,
1219+ (false, false, 24) => CodecId::PcmS24Le,
1220+ (false, false, 32) => CodecId::PcmS32Le,
1221+ (false, false, _) => CodecId::PcmS16Le,
1222+ (false, true, 24) => CodecId::PcmS24Be,
1223+ (false, true, 32) => CodecId::PcmS32Be,
1224+ (false, true, _) => CodecId::PcmS16Be,
1209 })1225 })
1210 }1226 }
1211 1227
@@ -1333,11 +1349,12 @@ pub(crate) fn parse_tracks(
1333 // Ticks, not nanoseconds: everything downstream counts in the file's own1349 // Ticks, not nanoseconds: everything downstream counts in the file's own
1334 // time base and nothing here reintroduces a float clock.1350 // time base and nothing here reintroduces a float clock.
1335 let step = default_duration.map(|ns| (ns / scale_ns.max(1)).max(1) as i64);1351 let step = default_duration.map(|ns| (ns / scale_ns.max(1)).max(1) as i64);
1336- let codec_id = media1352+ let depth = u32::try_from(bits).unwrap_or(u32::MAX);
1337- .filter(|_| kind != 0)1353+ let codec_id = match media.filter(|_| kind != 0) {
1338- .and_then(|_| {1354+ Some(_) => codec_of(&codec, depth)?
1339- codec_of(&codec, bits as u32).or_else(|| video_codec_from_private(&codec, private.as_deref()))1355+ .or_else(|| video_codec_from_private(&codec, private.as_deref())),
1340- });1356+ None => None,
1357+ };
1341 let stream = match (codec_id, media) {1358 let stream = match (codec_id, media) {
1342 (Some(codec_id), Some(media)) => {1359 (Some(codec_id), Some(media)) => {
1343 if media == MediaType::Audio && channels > MAX_CHANNELS {1360 if media == MediaType::Audio && channels > MAX_CHANNELS {
@@ -1380,7 +1397,7 @@ pub(crate) fn parse_tracks(
1380 },1397 },
1381 layout: ChannelLayout::from_count(channels.max(1) as usize),1398 layout: ChannelLayout::from_count(channels.max(1) as usize),
1382 format: None,1399 format: None,
1383- bits_per_sample: (bits > 0).then_some(bits as u32),1400+ bits_per_sample: (bits > 0).then_some(depth),
1384 }),1401 }),
1385 MediaType::Subtitle => MediaParameters::Subtitle,1402 MediaType::Subtitle => MediaParameters::Subtitle,
1386 };1403 };
@@ -1908,17 +1925,17 @@ mod tests {
1908 1925
1909 #[test]1926 #[test]
1910 fn codec_ids_cover_the_families_the_family_carries() {1927 fn codec_ids_cover_the_families_the_family_carries() {
1911- assert_eq!(codec_of("V_MPEGH/ISO/HEVC", 0), Some(CodecId::H265));1928+ assert_eq!(codec_of("V_MPEGH/ISO/HEVC", 0).unwrap(), Some(CodecId::H265));
1912- assert_eq!(codec_of("A_AAC/MPEG4/LC/SBR", 0), Some(CodecId::Aac));1929+ assert_eq!(codec_of("A_AAC/MPEG4/LC/SBR", 0).unwrap(), Some(CodecId::Aac));
1913- assert_eq!(codec_of("A_PCM/INT/LIT", 24), Some(CodecId::PcmS24Le));1930+ assert_eq!(codec_of("A_PCM/INT/LIT", 24).unwrap(), Some(CodecId::PcmS24Le));
1914- assert_eq!(codec_of("A_PCM/INT/LIT", 16), Some(CodecId::PcmS16Le));1931+ assert_eq!(codec_of("A_PCM/INT/LIT", 16).unwrap(), Some(CodecId::PcmS16Le));
1915- assert_eq!(codec_of("S_HDMV/PGS", 0), Some(CodecId::Pgs));1932+ assert_eq!(codec_of("S_HDMV/PGS", 0).unwrap(), Some(CodecId::Pgs));
1916- assert_eq!(codec_of("S_VOBSUB", 0), Some(CodecId::DvdSub));1933+ assert_eq!(codec_of("S_VOBSUB", 0).unwrap(), Some(CodecId::DvdSub));
1917- assert_eq!(codec_of("S_DVBSUB", 0), Some(CodecId::DvbSub));1934+ assert_eq!(codec_of("S_DVBSUB", 0).unwrap(), Some(CodecId::DvbSub));
1918- assert_eq!(codec_of("V_MJPEG", 0), Some(CodecId::Mjpeg));1935+ assert_eq!(codec_of("V_MJPEG", 0).unwrap(), Some(CodecId::Mjpeg));
1919- assert_eq!(codec_of("V_MPEG4/ISO/ASP", 0), Some(CodecId::Mpeg4Part2));1936+ assert_eq!(codec_of("V_MPEG4/ISO/ASP", 0).unwrap(), Some(CodecId::Mpeg4Part2));
1920- assert_eq!(codec_of("V_MPEG4/ISO/SP", 0), Some(CodecId::Mpeg4Part2));1937+ assert_eq!(codec_of("V_MPEG4/ISO/SP", 0).unwrap(), Some(CodecId::Mpeg4Part2));
1921- assert_eq!(codec_of("V_MS/VFW/FOURCC", 0), None);1938+ assert_eq!(codec_of("V_MS/VFW/FOURCC", 0).unwrap(), None);
1922 }1939 }
1923 1940
1924 #[test]1941 #[test]