forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'simplify/b5' into simplify/w1a

Browse files at this commit

afa57b34f3f323f895246043f93ced3c61f4c823Ahmet Kaan GümüşParent: 9dcaf0722a6626
crates/ec-matroska/src/demux.rsmodified+56 -27
@@ -1166,8 +1166,8 @@ fn content_encoding(buf: &[u8], range: Range<usize>) -> Unpack {
11661166
11671167 /// Which [`CodecId`] a Matroska codec id names. `A_PCM/*` needs the track's own
11681168 /// `BitDepth`, which is why the depth comes in.
1169-pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Option<CodecId> {
1170- Some(match id {
1169+pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Result<Option<CodecId>> {
1170+ Ok(Some(match id {
11711171 "V_MPEG4/ISO/AVC" => CodecId::H264,
11721172 "V_MPEGH/ISO/HEVC" => CodecId::H265,
11731173 // 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> {
11901190 "S_HDMV/PGS" => CodecId::Pgs,
11911191 "S_VOBSUB" => CodecId::DvdSub,
11921192 "S_DVBSUB" => CodecId::DvbSub,
1193- "A_PCM/FLOAT/IEEE" => CodecId::PcmF32Le,
1194- "A_PCM/INT/BIG" => CodecId::PcmS16Be,
1195- "A_PCM/INT/LIT" => match bit_depth {
1196- 8 => CodecId::PcmU8,
1197- 24 => CodecId::PcmS24Le,
1198- 32 => CodecId::PcmS32Le,
1199- _ => CodecId::PcmS16Le,
1200- },
1193+ "A_PCM/FLOAT/IEEE" => pcm_codec(false, true, bit_depth)?,
1194+ "A_PCM/INT/BIG" => pcm_codec(true, false, bit_depth)?,
1195+ "A_PCM/INT/LIT" => pcm_codec(false, false, bit_depth)?,
12011196 // `A_AAC/MPEG4/LC/SBR` and its family all decode as AAC, and
12021197 // `A_MPEG/L3` is the only MPEG layer this carries.
12031198 id if id.starts_with("A_AAC") => CodecId::Aac,
@@ -1205,6 +1200,39 @@ pub(crate) fn codec_of(id: &str, bit_depth: u32) -> Option<CodecId> {
12051200 // MPEG-1/2 Layer II (MP2) and Layer I (MP1): the same decoder as Layer III reads them.
12061201 "A_MPEG/L2" => CodecId::Mp2,
12071202 "A_MPEG/L1" => CodecId::Mp1,
1203+ _ => return Ok(None),
1204+ }))
1205+}
1206+
1207+pub(crate) fn pcm_codec(big: bool, float: bool, bits: u32) -> Result<CodecId> {
1208+ Ok(match (float, big, bits) {
1209+ (true, _, 0 | 32) => CodecId::PcmF32Le,
1210+ (true, _, _) => {
1211+ return Err(Error::unsupported(
1212+ format!("{bits}-bit float PCM in Matroska"),
1213+ "only 32-bit float is defined",
1214+ ));
1215+ }
1216+ (false, false, 8) => CodecId::PcmU8,
1217+ (false, false, 24) => CodecId::PcmS24Le,
1218+ (false, false, 32) => CodecId::PcmS32Le,
1219+ (false, false, _) => CodecId::PcmS16Le,
1220+ (false, true, 24) => CodecId::PcmS24Be,
1221+ (false, true, 32) => CodecId::PcmS32Be,
1222+ (false, true, _) => CodecId::PcmS16Be,
1223+ })
1224+}
1225+
1226+pub(crate) fn pcm_bits(codec: CodecId) -> Option<(bool, bool, u32)> {
1227+ Some(match codec {
1228+ CodecId::PcmU8 => (false, false, 8),
1229+ CodecId::PcmS16Le => (false, false, 16),
1230+ CodecId::PcmS24Le => (false, false, 24),
1231+ CodecId::PcmS32Le => (false, false, 32),
1232+ CodecId::PcmS16Be => (true, false, 16),
1233+ CodecId::PcmS24Be => (true, false, 24),
1234+ CodecId::PcmS32Be => (true, false, 32),
1235+ CodecId::PcmF32Le => (false, true, 32),
12081236 _ => return None,
12091237 })
12101238 }
@@ -1333,11 +1361,12 @@ pub(crate) fn parse_tracks(
13331361 // Ticks, not nanoseconds: everything downstream counts in the file's own
13341362 // time base and nothing here reintroduces a float clock.
13351363 let step = default_duration.map(|ns| (ns / scale_ns.max(1)).max(1) as i64);
1336- let codec_id = media
1337- .filter(|_| kind != 0)
1338- .and_then(|_| {
1339- codec_of(&codec, bits as u32).or_else(|| video_codec_from_private(&codec, private.as_deref()))
1340- });
1364+ let depth = u32::try_from(bits).unwrap_or(u32::MAX);
1365+ let codec_id = match media.filter(|_| kind != 0) {
1366+ Some(_) => codec_of(&codec, depth)?
1367+ .or_else(|| video_codec_from_private(&codec, private.as_deref())),
1368+ None => None,
1369+ };
13411370 let stream = match (codec_id, media) {
13421371 (Some(codec_id), Some(media)) => {
13431372 if media == MediaType::Audio && channels > MAX_CHANNELS {
@@ -1380,7 +1409,7 @@ pub(crate) fn parse_tracks(
13801409 },
13811410 layout: ChannelLayout::from_count(channels.max(1) as usize),
13821411 format: None,
1383- bits_per_sample: (bits > 0).then_some(bits as u32),
1412+ bits_per_sample: (bits > 0).then_some(depth),
13841413 }),
13851414 MediaType::Subtitle => MediaParameters::Subtitle,
13861415 };
@@ -1908,17 +1937,17 @@ mod tests {
19081937
19091938 #[test]
19101939 fn codec_ids_cover_the_families_the_family_carries() {
1911- assert_eq!(codec_of("V_MPEGH/ISO/HEVC", 0), Some(CodecId::H265));
1912- assert_eq!(codec_of("A_AAC/MPEG4/LC/SBR", 0), Some(CodecId::Aac));
1913- assert_eq!(codec_of("A_PCM/INT/LIT", 24), Some(CodecId::PcmS24Le));
1914- assert_eq!(codec_of("A_PCM/INT/LIT", 16), Some(CodecId::PcmS16Le));
1915- assert_eq!(codec_of("S_HDMV/PGS", 0), Some(CodecId::Pgs));
1916- assert_eq!(codec_of("S_VOBSUB", 0), Some(CodecId::DvdSub));
1917- assert_eq!(codec_of("S_DVBSUB", 0), Some(CodecId::DvbSub));
1918- assert_eq!(codec_of("V_MJPEG", 0), Some(CodecId::Mjpeg));
1919- assert_eq!(codec_of("V_MPEG4/ISO/ASP", 0), Some(CodecId::Mpeg4Part2));
1920- assert_eq!(codec_of("V_MPEG4/ISO/SP", 0), Some(CodecId::Mpeg4Part2));
1921- assert_eq!(codec_of("V_MS/VFW/FOURCC", 0), None);
1940+ assert_eq!(codec_of("V_MPEGH/ISO/HEVC", 0).unwrap(), Some(CodecId::H265));
1941+ assert_eq!(codec_of("A_AAC/MPEG4/LC/SBR", 0).unwrap(), Some(CodecId::Aac));
1942+ assert_eq!(codec_of("A_PCM/INT/LIT", 24).unwrap(), Some(CodecId::PcmS24Le));
1943+ assert_eq!(codec_of("A_PCM/INT/LIT", 16).unwrap(), Some(CodecId::PcmS16Le));
1944+ assert_eq!(codec_of("S_HDMV/PGS", 0).unwrap(), Some(CodecId::Pgs));
1945+ assert_eq!(codec_of("S_VOBSUB", 0).unwrap(), Some(CodecId::DvdSub));
1946+ assert_eq!(codec_of("S_DVBSUB", 0).unwrap(), Some(CodecId::DvbSub));
1947+ assert_eq!(codec_of("V_MJPEG", 0).unwrap(), Some(CodecId::Mjpeg));
1948+ assert_eq!(codec_of("V_MPEG4/ISO/ASP", 0).unwrap(), Some(CodecId::Mpeg4Part2));
1949+ assert_eq!(codec_of("V_MPEG4/ISO/SP", 0).unwrap(), Some(CodecId::Mpeg4Part2));
1950+ assert_eq!(codec_of("V_MS/VFW/FOURCC", 0).unwrap(), None);
19221951 }
19231952
19241953 #[test]
crates/ec-matroska/src/mux.rsmodified+11 -5
@@ -12,6 +12,7 @@ use ec_core::{
1212 TimeBase,
1313 };
1414
15+use crate::demux::pcm_bits;
1516 use crate::ebml::{self, elem, elem_head_len, float, put_id, put_size, uint};
1617
1718 /// 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> {
767768 CodecId::Mp3 => "A_MPEG/L3",
768769 CodecId::Opus => "A_OPUS",
769770 CodecId::Vorbis => "A_VORBIS",
770- CodecId::PcmU8 | CodecId::PcmS16Le | CodecId::PcmS24Le | CodecId::PcmS32Le => {
771- "A_PCM/INT/LIT"
772- }
773- CodecId::PcmS16Be => "A_PCM/INT/BIG",
774- CodecId::PcmF32Le => "A_PCM/FLOAT/IEEE",
771+ CodecId::PcmU8
772+ | CodecId::PcmS16Le
773+ | CodecId::PcmS24Le
774+ | CodecId::PcmS32Le
775+ | CodecId::PcmS16Be
776+ | CodecId::PcmF32Le => match pcm_bits(codec) {
777+ Some((_, true, _)) => "A_PCM/FLOAT/IEEE",
778+ Some((true, _, _)) => "A_PCM/INT/BIG",
779+ _ => "A_PCM/INT/LIT",
780+ },
775781 CodecId::Srt => "S_TEXT/UTF8",
776782 CodecId::WebVtt => "S_TEXT/WEBVTT",
777783 CodecId::Ass => "S_TEXT/ASS",
crates/ec-probe/src/decoder.rsmodified+10 -0
@@ -374,7 +374,9 @@ fn is_pcm(codec: CodecId) -> bool {
374374 | CodecId::PcmS16Le
375375 | CodecId::PcmS16Be
376376 | CodecId::PcmS24Le
377+ | CodecId::PcmS24Be
377378 | CodecId::PcmS32Le
379+ | CodecId::PcmS32Be
378380 | CodecId::PcmF32Le
379381 )
380382 }
@@ -463,10 +465,18 @@ fn pcm(codec: CodecId, data: &[u8], out: &mut Vec<f32>) {
463465 let v = i32::from_le_bytes([0, c[0], c[1], c[2]]);
464466 v as f32 / 2147483648.0
465467 })),
468+ CodecId::PcmS24Be => out.extend(data.as_chunks::<3>().0.iter().map(|c| {
469+ let v = i32::from_be_bytes([c[0], c[1], c[2], 0]);
470+ v as f32 / 2147483648.0
471+ })),
466472 CodecId::PcmS32Le => out.extend(
467473 data.as_chunks::<4>().0.iter()
468474 .map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]]) as f32 / 2147483648.0),
469475 ),
476+ CodecId::PcmS32Be => out.extend(
477+ data.as_chunks::<4>().0.iter()
478+ .map(|c| i32::from_be_bytes([c[0], c[1], c[2], c[3]]) as f32 / 2147483648.0),
479+ ),
470480 CodecId::PcmF32Le => out.extend(
471481 data.as_chunks::<4>().0.iter()
472482 .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])),