forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'simplify/s4' into simplify/w2

Browse files at this commit

2df2562698fb702ffc72a9ae5aec5998b0eb7bf2Ahmet Kaan GümüşParent: eaa1becadc0ddf
crates/ec-matroska/src/demux.rsmodified+31 -8
@@ -1008,9 +1008,9 @@ pub(crate) fn parse_block(buf: &[u8], range: Range<usize>) -> Result<BlockHeader
10081008 let flags = rest[2];
10091009 let start = range.start + len + 3;
10101010 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)?,
10141014 };
10151015 Ok(BlockHeader {
10161016 track,
@@ -1020,13 +1020,36 @@ pub(crate) fn parse_block(buf: &[u8], range: Range<usize>) -> Result<BlockHeader
10201020 })
10211021 }
10221022
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+
10231041 /// The frames of a laced block. Lacing packs several frames — always whole
10241042 /// ones, and only ones nothing else references — behind a header of sizes, in
10251043 /// one of the three shapes the spec defines. Video muxers write none, but a
10261044 /// streaming service's audio is laced by the thousand: every E-AC-3 block of a
10271045 /// WEB remux is fixed-laced, and a reader that refuses those is a film that
10281046 /// 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>>> {
10301053 let head = buf
10311054 .get(start..end)
10321055 .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
10401063 let mut sizes = Vec::with_capacity(count);
10411064 match lacing {
10421065 // Xiph: every size but the last as a run of 255s and a remainder.
1043- 1 => {
1066+ Lacing::Xiph => {
10441067 for _ in 1..count {
10451068 let mut size = 0usize;
10461069 loop {
@@ -1055,7 +1078,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result<V
10551078 }
10561079 }
10571080 // Fixed: no sizes written at all, the frames divide the rest evenly.
1058- 2 => {
1081+ Lacing::Fixed => {
10591082 let rest = head.len() - at;
10601083 if count == 0 || !rest.is_multiple_of(count) {
10611084 return Err(Error::corrupt(
@@ -1067,7 +1090,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result<V
10671090 // EBML: the first size outright, the rest as differences from the one
10681091 // before — signed vints, which is the same encoding with the middle of
10691092 // its range taken as zero.
1070- _ => {
1093+ Lacing::Ebml => {
10711094 let mut size = 0i64;
10721095 for i in 1..count {
10731096 let (raw, len) = ebml::vint(head.get(at..).ok_or_else(short)?, true)?;
@@ -1362,7 +1385,7 @@ pub(crate) fn parse_tracks(
13621385 // time base and nothing here reintroduces a float clock.
13631386 let step = default_duration.map(|ns| (ns / scale_ns.max(1)).max(1) as i64);
13641387 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 {
13661389 Some(_) => codec_of(&codec, depth)?
13671390 .or_else(|| video_codec_from_private(&codec, private.as_deref())),
13681391 None => None,
crates/ec-matroska/src/fuzz_api.rsmodified+1 -1
@@ -42,7 +42,7 @@ fn walk(buf: &[u8], depth: u32) -> usize {
4242 /// `buf` as the payload of a laced block with the given lacing (1 Xiph, 2 fixed,
4343 /// 3 EBML). Returns the frame ranges.
4444 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))
4646 }
4747
4848 /// `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::{
1010
1111 use crate::asc;
1212 use crate::boxes::{Boxes, FourCc, Src, be16, be32, be64, full};
13+use crate::codec_table::video_codec;
1314 use crate::esds::{Esds, object_type};
1415
1516 /// 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
11781179 Err(_) if children.ends_with(&[0; 4]) => break,
11791180 Err(e) => return Err(e),
11801181 };
1181- if &kind == config {
1182+ if config == Some(&kind) {
11821183 extradata = Some(Buf::copy_from_slice(body));
11831184 } else if &kind == b"colr" {
11841185 if let Some((info, tags)) = colr(body) {
@@ -1601,43 +1602,6 @@ pub(crate) fn opus_head(dops: &[u8]) -> Buf {
16011602 Buf::from_vec(out)
16021603 }
16031604
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-
16411605 /// A `ColourInformationBox`: the H.273 triplet, when it is the `nclx`/`nclc`
16421606 /// kind rather than an ICC profile — as the raw triplet *and* as the tags that
16431607 /// triplet really states.
crates/ec-mp4/src/lib.rsmodified+1 -0
@@ -52,6 +52,7 @@
5252
5353 mod asc;
5454 mod boxes;
55+mod codec_table;
5556 mod demux;
5657 mod esds;
5758 mod mux;