Merge branch 'simplify/s4' into simplify/w2
Browse files at this commit5 changed files +78 -47
- crates/ec-matroska/src/demux.rs +31 -8
- crates/ec-matroska/src/fuzz_api.rs +1 -1
- crates/ec-mp4/src/codec_table.rs +43 -0
- crates/ec-mp4/src/demux.rs +2 -38
- crates/ec-mp4/src/lib.rs +1 -0
crates/ec-matroska/src/demux.rsmodified+31 -8
| @@ -1008,9 +1008,9 @@ pub(crate) fn parse_block(buf: &[u8], range: Range<usize>) -> Result<BlockHeader | ||
| 1008 | 1008 | let flags = rest[2]; |
| 1009 | 1009 | let start = range.start + len + 3; |
| 1010 | 1010 | let end = range.end; |
| 1011 | - let frames = match (flags >> 1) & 0x03 { | |
| 1012 | - 0 => std::iter::once(start..end).collect(), | |
| 1013 | - lacing => lace(buf, start, end, lacing)?, | |
| 1011 | + let frames = match Lacing::of_flags((flags >> 1) & 0x03) { | |
| 1012 | + None => std::iter::once(start..end).collect(), | |
| 1013 | + Some(lacing) => lace(buf, start, end, lacing)?, | |
| 1014 | 1014 | }; |
| 1015 | 1015 | Ok(BlockHeader { |
| 1016 | 1016 | track, |
| @@ -1020,13 +1020,36 @@ pub(crate) fn parse_block(buf: &[u8], range: Range<usize>) -> Result<BlockHeader | ||
| 1020 | 1020 | }) |
| 1021 | 1021 | } |
| 1022 | 1022 | |
| 1023 | +#[derive(Clone, Copy)] | |
| 1024 | +pub(crate) enum Lacing { | |
| 1025 | + Xiph, | |
| 1026 | + Fixed, | |
| 1027 | + Ebml, | |
| 1028 | +} | |
| 1029 | + | |
| 1030 | +impl Lacing { | |
| 1031 | + pub(crate) fn of_flags(bits: u8) -> Option<Lacing> { | |
| 1032 | + match bits { | |
| 1033 | + 0 => None, | |
| 1034 | + 1 => Some(Lacing::Xiph), | |
| 1035 | + 2 => Some(Lacing::Fixed), | |
| 1036 | + _ => Some(Lacing::Ebml), | |
| 1037 | + } | |
| 1038 | + } | |
| 1039 | +} | |
| 1040 | + | |
| 1023 | 1041 | /// The frames of a laced block. Lacing packs several frames — always whole |
| 1024 | 1042 | /// ones, and only ones nothing else references — behind a header of sizes, in |
| 1025 | 1043 | /// one of the three shapes the spec defines. Video muxers write none, but a |
| 1026 | 1044 | /// streaming service's audio is laced by the thousand: every E-AC-3 block of a |
| 1027 | 1045 | /// WEB remux is fixed-laced, and a reader that refuses those is a film that |
| 1028 | 1046 | /// plays silent. |
| 1029 | -pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result<Vec<Range<usize>>> { | |
| 1047 | +pub(crate) fn lace( | |
| 1048 | + buf: &[u8], | |
| 1049 | + start: usize, | |
| 1050 | + end: usize, | |
| 1051 | + lacing: Lacing, | |
| 1052 | +) -> Result<Vec<Range<usize>>> { | |
| 1030 | 1053 | let head = buf |
| 1031 | 1054 | .get(start..end) |
| 1032 | 1055 | .ok_or_else(|| Error::corrupt("Matroska: lace header past its block"))?; |
| @@ -1040,7 +1063,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result<V | ||
| 1040 | 1063 | let mut sizes = Vec::with_capacity(count); |
| 1041 | 1064 | match lacing { |
| 1042 | 1065 | // Xiph: every size but the last as a run of 255s and a remainder. |
| 1043 | - 1 => { | |
| 1066 | + Lacing::Xiph => { | |
| 1044 | 1067 | for _ in 1..count { |
| 1045 | 1068 | let mut size = 0usize; |
| 1046 | 1069 | loop { |
| @@ -1055,7 +1078,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result<V | ||
| 1055 | 1078 | } |
| 1056 | 1079 | } |
| 1057 | 1080 | // Fixed: no sizes written at all, the frames divide the rest evenly. |
| 1058 | - 2 => { | |
| 1081 | + Lacing::Fixed => { | |
| 1059 | 1082 | let rest = head.len() - at; |
| 1060 | 1083 | if count == 0 || !rest.is_multiple_of(count) { |
| 1061 | 1084 | return Err(Error::corrupt( |
| @@ -1067,7 +1090,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result<V | ||
| 1067 | 1090 | // EBML: the first size outright, the rest as differences from the one |
| 1068 | 1091 | // before — signed vints, which is the same encoding with the middle of |
| 1069 | 1092 | // its range taken as zero. |
| 1070 | - _ => { | |
| 1093 | + Lacing::Ebml => { | |
| 1071 | 1094 | let mut size = 0i64; |
| 1072 | 1095 | for i in 1..count { |
| 1073 | 1096 | let (raw, len) = ebml::vint(head.get(at..).ok_or_else(short)?, true)?; |
| @@ -1362,7 +1385,7 @@ pub(crate) fn parse_tracks( | ||
| 1362 | 1385 | // time base and nothing here reintroduces a float clock. |
| 1363 | 1386 | let step = default_duration.map(|ns| (ns / scale_ns.max(1)).max(1) as i64); |
| 1364 | 1387 | let depth = u32::try_from(bits).unwrap_or(u32::MAX); |
| 1365 | - let codec_id = match media.filter(|_| kind != 0) { | |
| 1388 | + let codec_id = match media { | |
| 1366 | 1389 | Some(_) => codec_of(&codec, depth)? |
| 1367 | 1390 | .or_else(|| video_codec_from_private(&codec, private.as_deref())), |
| 1368 | 1391 | None => None, |
crates/ec-matroska/src/fuzz_api.rsmodified+1 -1
| @@ -42,7 +42,7 @@ fn walk(buf: &[u8], depth: u32) -> usize { | ||
| 42 | 42 | /// `buf` as the payload of a laced block with the given lacing (1 Xiph, 2 fixed, |
| 43 | 43 | /// 3 EBML). Returns the frame ranges. |
| 44 | 44 | pub fn lace(buf: &[u8], lacing: u8) -> Result<Vec<Range<usize>>> { |
| 45 | - demux::lace(buf, 0, buf.len(), lacing) | |
| 45 | + demux::lace(buf, 0, buf.len(), demux::Lacing::of_flags(lacing).unwrap_or(demux::Lacing::Ebml)) | |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /// `buf` as a whole `SimpleBlock` or `Block` body: track number, timecode, flags and |
crates/ec-mp4/src/codec_table.rsadded+43 -0
| @@ -0,0 +1,43 @@ | ||
| 1 | +use ec_core::CodecId; | |
| 2 | + | |
| 3 | +use crate::boxes::FourCc; | |
| 4 | + | |
| 5 | +pub(crate) fn video_codec( | |
| 6 | + kind: &FourCc, | |
| 7 | + video_track: bool, | |
| 8 | +) -> Option<(CodecId, Option<&'static FourCc>)> { | |
| 9 | + Some(match kind { | |
| 10 | + b"avc1" | b"avc3" => (CodecId::H264, Some(b"avcC")), | |
| 11 | + b"hvc1" | b"hev1" => (CodecId::H265, Some(b"hvcC")), | |
| 12 | + b"vp08" => (CodecId::Vp8, Some(b"vpcC")), | |
| 13 | + b"vp09" => (CodecId::Vp9, Some(b"vpcC")), | |
| 14 | + b"av01" => (CodecId::Av1, Some(b"av1C")), | |
| 15 | + // Motion JPEG: QuickTime `jpeg` (Photo JPEG) and `mjpa` (MJPEG-A) carry whole JFIF | |
| 16 | + // frames and no configuration box. `mjpb` (MJPEG-B) packets have a different layout | |
| 17 | + // (no SOI, marker-less blocks at header offsets); ec-mjpeg detects and rewraps them. | |
| 18 | + // Uncompressed video; `raw ` is also the 8-bit PCM audio entry, so it needs a `vide` track. | |
| 19 | + // The decoder (ec-riff `RawVideoDecoder`) reads the layout from the synthesized header. | |
| 20 | + b"raw " | b"yuvs" if !video_track => return None, | |
| 21 | + b"raw " | b"yuvs" | b"2vuy" | b"yuv2" | b"v210" | b"v410" | b"b16g" => { | |
| 22 | + (CodecId::RawVideo, None) | |
| 23 | + } | |
| 24 | + b"jpeg" | b"mjpa" | b"mjpb" => (CodecId::Mjpeg, None), | |
| 25 | + // ProRes entries carry no configuration box: the frame header is self-describing. | |
| 26 | + b"apco" | b"apcs" | b"apcn" | b"apch" | b"ap4h" | b"ap4x" => (CodecId::ProRes, None), | |
| 27 | + // Avid DNxHD (`AVdn`) and DNxHR (`AVdh`); the optional `DNxD` box is | |
| 28 | + // the only child worth keeping as extradata. | |
| 29 | + b"AVdn" | b"AVdh" => (CodecId::Dnxhd, Some(b"DNxD")), | |
| 30 | + // MPEG-4 Part 2 Visual: `mp4v` carries the VOL header in the `esds` | |
| 31 | + // DecoderSpecificInfo; the 3GPP/QuickTime H.263 entries (`s263`, `h263`) | |
| 32 | + // carry no configuration the baseline decoder needs (`d263` is ignored). | |
| 33 | + b"mp4v" => (CodecId::Mpeg4Part2, Some(b"esds")), | |
| 34 | + b"s263" | b"h263" => (CodecId::Mpeg4Part2, None), | |
| 35 | + // DV: QuickTime sample entries for DV25/DVCPRO25 (`dvc `/`dvcp`/`dvpp`), DVCPRO50 | |
| 36 | + // (`dv5n`/`dv5p`) and DVCPRO HD (`dvhq`/`dvh3`/`dvh5`/`dvh6`/`dvhp`...). The frame is | |
| 37 | + // self-describing: no configuration box. | |
| 38 | + b"dvc " | b"dvcp" | b"dvpp" | b"dvcs" | b"dvis" | b"dv5n" | b"dv5p" | b"dv5q" | b"dvhq" | |
| 39 | + | b"dvh1" | b"dvh2" | b"dvh3" | b"dvh4" | b"dvh5" | b"dvh6" | b"dvhp" | b"dvhs" | |
| 40 | + | b"dvhn" | b"dvhr" => (CodecId::DvVideo, None), | |
| 41 | + _ => return None, | |
| 42 | + }) | |
| 43 | +} |
crates/ec-mp4/src/demux.rsmodified+2 -38
| @@ -10,6 +10,7 @@ use ec_core::{ | ||
| 10 | 10 | |
| 11 | 11 | use crate::asc; |
| 12 | 12 | use crate::boxes::{Boxes, FourCc, Src, be16, be32, be64, full}; |
| 13 | +use crate::codec_table::video_codec; | |
| 13 | 14 | use crate::esds::{Esds, object_type}; |
| 14 | 15 | |
| 15 | 16 | /// The whole movie header is read into memory at once — it is the index, and a |
| @@ -1178,7 +1179,7 @@ fn sample_entry(kind: &FourCc, payload: &[u8], video_track: bool) -> Result<Opti | ||
| 1178 | 1179 | Err(_) if children.ends_with(&[0; 4]) => break, |
| 1179 | 1180 | Err(e) => return Err(e), |
| 1180 | 1181 | }; |
| 1181 | - if &kind == config { | |
| 1182 | + if config == Some(&kind) { | |
| 1182 | 1183 | extradata = Some(Buf::copy_from_slice(body)); |
| 1183 | 1184 | } else if &kind == b"colr" { |
| 1184 | 1185 | if let Some((info, tags)) = colr(body) { |
| @@ -1601,43 +1602,6 @@ pub(crate) fn opus_head(dops: &[u8]) -> Buf { | ||
| 1601 | 1602 | Buf::from_vec(out) |
| 1602 | 1603 | } |
| 1603 | 1604 | |
| 1604 | -fn video_codec(kind: &FourCc, video_track: bool) -> Option<(CodecId, &'static FourCc)> { | |
| 1605 | - Some(match kind { | |
| 1606 | - b"avc1" | b"avc3" => (CodecId::H264, b"avcC"), | |
| 1607 | - b"hvc1" | b"hev1" => (CodecId::H265, b"hvcC"), | |
| 1608 | - b"vp08" => (CodecId::Vp8, b"vpcC"), | |
| 1609 | - b"vp09" => (CodecId::Vp9, b"vpcC"), | |
| 1610 | - b"av01" => (CodecId::Av1, b"av1C"), | |
| 1611 | - // Motion JPEG: QuickTime `jpeg` (Photo JPEG) and `mjpa` (MJPEG-A) carry whole JFIF | |
| 1612 | - // frames and no configuration box. `mjpb` (MJPEG-B) packets have a different layout | |
| 1613 | - // (no SOI, marker-less blocks at header offsets); ec-mjpeg detects and rewraps them. | |
| 1614 | - // Uncompressed video; `raw ` is also the 8-bit PCM audio entry, so it needs a `vide` track. | |
| 1615 | - // The decoder (ec-riff `RawVideoDecoder`) reads the layout from the synthesized header. | |
| 1616 | - b"raw " | b"yuvs" if !video_track => return None, | |
| 1617 | - b"raw " | b"yuvs" | b"2vuy" | b"yuv2" | b"v210" | b"v410" | b"b16g" => { | |
| 1618 | - (CodecId::RawVideo, b"\0\0\0\0") | |
| 1619 | - } | |
| 1620 | - b"jpeg" | b"mjpa" | b"mjpb" => (CodecId::Mjpeg, b"\0\0\0\0"), | |
| 1621 | - // ProRes entries carry no configuration box: the frame header is self-describing. | |
| 1622 | - b"apco" | b"apcs" | b"apcn" | b"apch" | b"ap4h" | b"ap4x" => (CodecId::ProRes, b"\0\0\0\0"), | |
| 1623 | - // Avid DNxHD (`AVdn`) and DNxHR (`AVdh`); the optional `DNxD` box is | |
| 1624 | - // the only child worth keeping as extradata. | |
| 1625 | - b"AVdn" | b"AVdh" => (CodecId::Dnxhd, b"DNxD"), | |
| 1626 | - // MPEG-4 Part 2 Visual: `mp4v` carries the VOL header in the `esds` | |
| 1627 | - // DecoderSpecificInfo; the 3GPP/QuickTime H.263 entries (`s263`, `h263`) | |
| 1628 | - // carry no configuration the baseline decoder needs (`d263` is ignored). | |
| 1629 | - b"mp4v" => (CodecId::Mpeg4Part2, b"esds"), | |
| 1630 | - b"s263" | b"h263" => (CodecId::Mpeg4Part2, b"\0\0\0\0"), | |
| 1631 | - // DV: QuickTime sample entries for DV25/DVCPRO25 (`dvc `/`dvcp`/`dvpp`), DVCPRO50 | |
| 1632 | - // (`dv5n`/`dv5p`) and DVCPRO HD (`dvhq`/`dvh3`/`dvh5`/`dvh6`/`dvhp`...). The frame is | |
| 1633 | - // self-describing: no configuration box. | |
| 1634 | - b"dvc " | b"dvcp" | b"dvpp" | b"dvcs" | b"dvis" | b"dv5n" | b"dv5p" | b"dv5q" | b"dvhq" | |
| 1635 | - | b"dvh1" | b"dvh2" | b"dvh3" | b"dvh4" | b"dvh5" | b"dvh6" | b"dvhp" | b"dvhs" | |
| 1636 | - | b"dvhn" | b"dvhr" => (CodecId::DvVideo, b"\0\0\0\0"), | |
| 1637 | - _ => return None, | |
| 1638 | - }) | |
| 1639 | -} | |
| 1640 | - | |
| 1641 | 1605 | /// A `ColourInformationBox`: the H.273 triplet, when it is the `nclx`/`nclc` |
| 1642 | 1606 | /// kind rather than an ICC profile — as the raw triplet *and* as the tags that |
| 1643 | 1607 | /// triplet really states. |
crates/ec-mp4/src/lib.rsmodified+1 -0
| @@ -52,6 +52,7 @@ | ||
| 52 | 52 | |
| 53 | 53 | mod asc; |
| 54 | 54 | mod boxes; |
| 55 | +mod codec_table; | |
| 55 | 56 | mod demux; |
| 56 | 57 | mod esds; |
| 57 | 58 | mod mux; |