Merge branch 'simplify/b5' into simplify/w1a
Browse files at this commit3 changed files +77 -32
- crates/ec-matroska/src/demux.rs +56 -27
- crates/ec-matroska/src/mux.rs +11 -5
- crates/ec-probe/src/decoder.rs +10 -0
crates/ec-matroska/src/demux.rsmodified+56 -27
| @@ -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 own | 1167 | /// 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, and | 1196 | // `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,6 +1200,39 @@ 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, |
| 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), | ||
| 1208 | _ => return None, | 1236 | _ => return None, |
| 1209 | }) | 1237 | }) |
| 1210 | } | 1238 | } |
| @@ -1333,11 +1361,12 @@ pub(crate) fn parse_tracks( | |||
| 1333 | // Ticks, not nanoseconds: everything downstream counts in the file's own | 1361 | // Ticks, not nanoseconds: everything downstream counts in the file's own |
| 1334 | // time base and nothing here reintroduces a float clock. | 1362 | // 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); | 1363 | let step = default_duration.map(|ns| (ns / scale_ns.max(1)).max(1) as i64); |
| 1336 | - let codec_id = media | 1364 | + let depth = u32::try_from(bits).unwrap_or(u32::MAX); |
| 1337 | - .filter(|_| kind != 0) | 1365 | + let codec_id = match media.filter(|_| kind != 0) { |
| 1338 | - .and_then(|_| { | 1366 | + Some(_) => codec_of(&codec, depth)? |
| 1339 | - codec_of(&codec, bits as u32).or_else(|| video_codec_from_private(&codec, private.as_deref())) | 1367 | + .or_else(|| video_codec_from_private(&codec, private.as_deref())), |
| 1340 | - }); | 1368 | + None => None, |
| 1369 | + }; | ||
| 1341 | let stream = match (codec_id, media) { | 1370 | let stream = match (codec_id, media) { |
| 1342 | (Some(codec_id), Some(media)) => { | 1371 | (Some(codec_id), Some(media)) => { |
| 1343 | if media == MediaType::Audio && channels > MAX_CHANNELS { | 1372 | if media == MediaType::Audio && channels > MAX_CHANNELS { |
| @@ -1380,7 +1409,7 @@ pub(crate) fn parse_tracks( | |||
| 1380 | }, | 1409 | }, |
| 1381 | layout: ChannelLayout::from_count(channels.max(1) as usize), | 1410 | layout: ChannelLayout::from_count(channels.max(1) as usize), |
| 1382 | format: None, | 1411 | format: None, |
| 1383 | - bits_per_sample: (bits > 0).then_some(bits as u32), | 1412 | + bits_per_sample: (bits > 0).then_some(depth), |
| 1384 | }), | 1413 | }), |
| 1385 | MediaType::Subtitle => MediaParameters::Subtitle, | 1414 | MediaType::Subtitle => MediaParameters::Subtitle, |
| 1386 | }; | 1415 | }; |
| @@ -1908,17 +1937,17 @@ mod tests { | |||
| 1908 | 1937 | ||
| 1909 | #[test] | 1938 | #[test] |
| 1910 | fn codec_ids_cover_the_families_the_family_carries() { | 1939 | fn codec_ids_cover_the_families_the_family_carries() { |
| 1911 | - assert_eq!(codec_of("V_MPEGH/ISO/HEVC", 0), Some(CodecId::H265)); | 1940 | + 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)); | 1941 | + 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)); | 1942 | + 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)); | 1943 | + 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)); | 1944 | + assert_eq!(codec_of("S_HDMV/PGS", 0).unwrap(), Some(CodecId::Pgs)); |
| 1916 | - assert_eq!(codec_of("S_VOBSUB", 0), Some(CodecId::DvdSub)); | 1945 | + assert_eq!(codec_of("S_VOBSUB", 0).unwrap(), Some(CodecId::DvdSub)); |
| 1917 | - assert_eq!(codec_of("S_DVBSUB", 0), Some(CodecId::DvbSub)); | 1946 | + assert_eq!(codec_of("S_DVBSUB", 0).unwrap(), Some(CodecId::DvbSub)); |
| 1918 | - assert_eq!(codec_of("V_MJPEG", 0), Some(CodecId::Mjpeg)); | 1947 | + assert_eq!(codec_of("V_MJPEG", 0).unwrap(), Some(CodecId::Mjpeg)); |
| 1919 | - assert_eq!(codec_of("V_MPEG4/ISO/ASP", 0), Some(CodecId::Mpeg4Part2)); | 1948 | + 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)); | 1949 | + assert_eq!(codec_of("V_MPEG4/ISO/SP", 0).unwrap(), Some(CodecId::Mpeg4Part2)); |
| 1921 | - assert_eq!(codec_of("V_MS/VFW/FOURCC", 0), None); | 1950 | + assert_eq!(codec_of("V_MS/VFW/FOURCC", 0).unwrap(), None); |
| 1922 | } | 1951 | } |
| 1923 | 1952 | ||
| 1924 | #[test] | 1953 | #[test] |
crates/ec-matroska/src/mux.rsmodified+11 -5
| @@ -12,6 +12,7 @@ use ec_core::{ | |||
| 12 | TimeBase, | 12 | TimeBase, |
| 13 | }; | 13 | }; |
| 14 | 14 | ||
| 15 | +use crate::demux::pcm_bits; | ||
| 15 | use crate::ebml::{self, elem, elem_head_len, float, put_id, put_size, uint}; | 16 | use crate::ebml::{self, elem, elem_head_len, float, put_id, put_size, uint}; |
| 16 | 17 | ||
| 17 | /// One millisecond, the tick every Matroska muxer writes and this one's block | 18 | /// 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> { | |||
| 767 | CodecId::Mp3 => "A_MPEG/L3", | 768 | CodecId::Mp3 => "A_MPEG/L3", |
| 768 | CodecId::Opus => "A_OPUS", | 769 | CodecId::Opus => "A_OPUS", |
| 769 | CodecId::Vorbis => "A_VORBIS", | 770 | CodecId::Vorbis => "A_VORBIS", |
| 770 | - CodecId::PcmU8 | CodecId::PcmS16Le | CodecId::PcmS24Le | CodecId::PcmS32Le => { | 771 | + CodecId::PcmU8 |
| 771 | - "A_PCM/INT/LIT" | 772 | + | CodecId::PcmS16Le |
| 772 | - } | 773 | + | CodecId::PcmS24Le |
| 773 | - CodecId::PcmS16Be => "A_PCM/INT/BIG", | 774 | + | CodecId::PcmS32Le |
| 774 | - CodecId::PcmF32Le => "A_PCM/FLOAT/IEEE", | 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 | + }, | ||
| 775 | CodecId::Srt => "S_TEXT/UTF8", | 781 | CodecId::Srt => "S_TEXT/UTF8", |
| 776 | CodecId::WebVtt => "S_TEXT/WEBVTT", | 782 | CodecId::WebVtt => "S_TEXT/WEBVTT", |
| 777 | CodecId::Ass => "S_TEXT/ASS", | 783 | CodecId::Ass => "S_TEXT/ASS", |
crates/ec-probe/src/decoder.rsmodified+10 -0
| @@ -374,7 +374,9 @@ fn is_pcm(codec: CodecId) -> bool { | |||
| 374 | | CodecId::PcmS16Le | 374 | | CodecId::PcmS16Le |
| 375 | | CodecId::PcmS16Be | 375 | | CodecId::PcmS16Be |
| 376 | | CodecId::PcmS24Le | 376 | | CodecId::PcmS24Le |
| 377 | + | CodecId::PcmS24Be | ||
| 377 | | CodecId::PcmS32Le | 378 | | CodecId::PcmS32Le |
| 379 | + | CodecId::PcmS32Be | ||
| 378 | | CodecId::PcmF32Le | 380 | | CodecId::PcmF32Le |
| 379 | ) | 381 | ) |
| 380 | } | 382 | } |
| @@ -463,10 +465,18 @@ fn pcm(codec: CodecId, data: &[u8], out: &mut Vec<f32>) { | |||
| 463 | let v = i32::from_le_bytes([0, c[0], c[1], c[2]]); | 465 | let v = i32::from_le_bytes([0, c[0], c[1], c[2]]); |
| 464 | v as f32 / 2147483648.0 | 466 | v as f32 / 2147483648.0 |
| 465 | })), | 467 | })), |
| 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 | + })), | ||
| 466 | CodecId::PcmS32Le => out.extend( | 472 | CodecId::PcmS32Le => out.extend( |
| 467 | data.as_chunks::<4>().0.iter() | 473 | data.as_chunks::<4>().0.iter() |
| 468 | .map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]]) as f32 / 2147483648.0), | 474 | .map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]]) as f32 / 2147483648.0), |
| 469 | ), | 475 | ), |
| 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 | + ), | ||
| 470 | CodecId::PcmF32Le => out.extend( | 480 | CodecId::PcmF32Le => out.extend( |
| 471 | data.as_chunks::<4>().0.iter() | 481 | data.as_chunks::<4>().0.iter() |
| 472 | .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])), | 482 | .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])), |