diff --git a/crates/ec-matroska/src/demux.rs b/crates/ec-matroska/src/demux.rs index 2f82b8af..bce4e5a9 100644 --- a/crates/ec-matroska/src/demux.rs +++ b/crates/ec-matroska/src/demux.rs @@ -1008,9 +1008,9 @@ pub(crate) fn parse_block(buf: &[u8], range: Range) -> Result> 1) & 0x03 { - 0 => std::iter::once(start..end).collect(), - lacing => lace(buf, start, end, lacing)?, + let frames = match Lacing::of_flags((flags >> 1) & 0x03) { + None => std::iter::once(start..end).collect(), + Some(lacing) => lace(buf, start, end, lacing)?, }; Ok(BlockHeader { track, @@ -1020,13 +1020,36 @@ pub(crate) fn parse_block(buf: &[u8], range: Range) -> Result Option { + match bits { + 0 => None, + 1 => Some(Lacing::Xiph), + 2 => Some(Lacing::Fixed), + _ => Some(Lacing::Ebml), + } + } +} + /// The frames of a laced block. Lacing packs several frames — always whole /// ones, and only ones nothing else references — behind a header of sizes, in /// one of the three shapes the spec defines. Video muxers write none, but a /// streaming service's audio is laced by the thousand: every E-AC-3 block of a /// WEB remux is fixed-laced, and a reader that refuses those is a film that /// plays silent. -pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result>> { +pub(crate) fn lace( + buf: &[u8], + start: usize, + end: usize, + lacing: Lacing, +) -> Result>> { let head = buf .get(start..end) .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 { + Lacing::Xiph => { for _ in 1..count { let mut size = 0usize; loop { @@ -1055,7 +1078,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result { + Lacing::Fixed => { let rest = head.len() - at; if count == 0 || !rest.is_multiple_of(count) { return Err(Error::corrupt( @@ -1067,7 +1090,7 @@ pub(crate) fn lace(buf: &[u8], start: usize, end: usize, lacing: u8) -> Result { + Lacing::Ebml => { let mut size = 0i64; for i in 1..count { let (raw, len) = ebml::vint(head.get(at..).ok_or_else(short)?, true)?; @@ -1362,7 +1385,7 @@ pub(crate) fn parse_tracks( // time base and nothing here reintroduces a float clock. let step = default_duration.map(|ns| (ns / scale_ns.max(1)).max(1) as i64); let depth = u32::try_from(bits).unwrap_or(u32::MAX); - let codec_id = match media.filter(|_| kind != 0) { + let codec_id = match media { Some(_) => codec_of(&codec, depth)? .or_else(|| video_codec_from_private(&codec, private.as_deref())), None => None, diff --git a/crates/ec-matroska/src/fuzz_api.rs b/crates/ec-matroska/src/fuzz_api.rs index 855ee317..7cdb7b10 100644 --- a/crates/ec-matroska/src/fuzz_api.rs +++ b/crates/ec-matroska/src/fuzz_api.rs @@ -42,7 +42,7 @@ fn walk(buf: &[u8], depth: u32) -> usize { /// `buf` as the payload of a laced block with the given lacing (1 Xiph, 2 fixed, /// 3 EBML). Returns the frame ranges. pub fn lace(buf: &[u8], lacing: u8) -> Result>> { - demux::lace(buf, 0, buf.len(), lacing) + demux::lace(buf, 0, buf.len(), demux::Lacing::of_flags(lacing).unwrap_or(demux::Lacing::Ebml)) } /// `buf` as a whole `SimpleBlock` or `Block` body: track number, timecode, flags and diff --git a/crates/ec-mp4/src/codec_table.rs b/crates/ec-mp4/src/codec_table.rs new file mode 100644 index 00000000..f7cd002c --- /dev/null +++ b/crates/ec-mp4/src/codec_table.rs @@ -0,0 +1,43 @@ +use ec_core::CodecId; + +use crate::boxes::FourCc; + +pub(crate) fn video_codec( + kind: &FourCc, + video_track: bool, +) -> Option<(CodecId, Option<&'static FourCc>)> { + Some(match kind { + b"avc1" | b"avc3" => (CodecId::H264, Some(b"avcC")), + b"hvc1" | b"hev1" => (CodecId::H265, Some(b"hvcC")), + b"vp08" => (CodecId::Vp8, Some(b"vpcC")), + b"vp09" => (CodecId::Vp9, Some(b"vpcC")), + b"av01" => (CodecId::Av1, Some(b"av1C")), + // Motion JPEG: QuickTime `jpeg` (Photo JPEG) and `mjpa` (MJPEG-A) carry whole JFIF + // frames and no configuration box. `mjpb` (MJPEG-B) packets have a different layout + // (no SOI, marker-less blocks at header offsets); ec-mjpeg detects and rewraps them. + // Uncompressed video; `raw ` is also the 8-bit PCM audio entry, so it needs a `vide` track. + // The decoder (ec-riff `RawVideoDecoder`) reads the layout from the synthesized header. + b"raw " | b"yuvs" if !video_track => return None, + b"raw " | b"yuvs" | b"2vuy" | b"yuv2" | b"v210" | b"v410" | b"b16g" => { + (CodecId::RawVideo, None) + } + b"jpeg" | b"mjpa" | b"mjpb" => (CodecId::Mjpeg, None), + // ProRes entries carry no configuration box: the frame header is self-describing. + b"apco" | b"apcs" | b"apcn" | b"apch" | b"ap4h" | b"ap4x" => (CodecId::ProRes, None), + // Avid DNxHD (`AVdn`) and DNxHR (`AVdh`); the optional `DNxD` box is + // the only child worth keeping as extradata. + b"AVdn" | b"AVdh" => (CodecId::Dnxhd, Some(b"DNxD")), + // MPEG-4 Part 2 Visual: `mp4v` carries the VOL header in the `esds` + // DecoderSpecificInfo; the 3GPP/QuickTime H.263 entries (`s263`, `h263`) + // carry no configuration the baseline decoder needs (`d263` is ignored). + b"mp4v" => (CodecId::Mpeg4Part2, Some(b"esds")), + b"s263" | b"h263" => (CodecId::Mpeg4Part2, None), + // DV: QuickTime sample entries for DV25/DVCPRO25 (`dvc `/`dvcp`/`dvpp`), DVCPRO50 + // (`dv5n`/`dv5p`) and DVCPRO HD (`dvhq`/`dvh3`/`dvh5`/`dvh6`/`dvhp`...). The frame is + // self-describing: no configuration box. + b"dvc " | b"dvcp" | b"dvpp" | b"dvcs" | b"dvis" | b"dv5n" | b"dv5p" | b"dv5q" | b"dvhq" + | b"dvh1" | b"dvh2" | b"dvh3" | b"dvh4" | b"dvh5" | b"dvh6" | b"dvhp" | b"dvhs" + | b"dvhn" | b"dvhr" => (CodecId::DvVideo, None), + _ => return None, + }) +} diff --git a/crates/ec-mp4/src/demux.rs b/crates/ec-mp4/src/demux.rs index e7118f93..3e957822 100644 --- a/crates/ec-mp4/src/demux.rs +++ b/crates/ec-mp4/src/demux.rs @@ -10,6 +10,7 @@ use ec_core::{ use crate::asc; use crate::boxes::{Boxes, FourCc, Src, be16, be32, be64, full}; +use crate::codec_table::video_codec; use crate::esds::{Esds, object_type}; /// 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 break, Err(e) => return Err(e), }; - if &kind == config { + if config == Some(&kind) { extradata = Some(Buf::copy_from_slice(body)); } else if &kind == b"colr" { if let Some((info, tags)) = colr(body) { @@ -1601,43 +1602,6 @@ pub(crate) fn opus_head(dops: &[u8]) -> Buf { Buf::from_vec(out) } -fn video_codec(kind: &FourCc, video_track: bool) -> Option<(CodecId, &'static FourCc)> { - Some(match kind { - b"avc1" | b"avc3" => (CodecId::H264, b"avcC"), - b"hvc1" | b"hev1" => (CodecId::H265, b"hvcC"), - b"vp08" => (CodecId::Vp8, b"vpcC"), - b"vp09" => (CodecId::Vp9, b"vpcC"), - b"av01" => (CodecId::Av1, b"av1C"), - // Motion JPEG: QuickTime `jpeg` (Photo JPEG) and `mjpa` (MJPEG-A) carry whole JFIF - // frames and no configuration box. `mjpb` (MJPEG-B) packets have a different layout - // (no SOI, marker-less blocks at header offsets); ec-mjpeg detects and rewraps them. - // Uncompressed video; `raw ` is also the 8-bit PCM audio entry, so it needs a `vide` track. - // The decoder (ec-riff `RawVideoDecoder`) reads the layout from the synthesized header. - b"raw " | b"yuvs" if !video_track => return None, - b"raw " | b"yuvs" | b"2vuy" | b"yuv2" | b"v210" | b"v410" | b"b16g" => { - (CodecId::RawVideo, b"\0\0\0\0") - } - b"jpeg" | b"mjpa" | b"mjpb" => (CodecId::Mjpeg, b"\0\0\0\0"), - // ProRes entries carry no configuration box: the frame header is self-describing. - b"apco" | b"apcs" | b"apcn" | b"apch" | b"ap4h" | b"ap4x" => (CodecId::ProRes, b"\0\0\0\0"), - // Avid DNxHD (`AVdn`) and DNxHR (`AVdh`); the optional `DNxD` box is - // the only child worth keeping as extradata. - b"AVdn" | b"AVdh" => (CodecId::Dnxhd, b"DNxD"), - // MPEG-4 Part 2 Visual: `mp4v` carries the VOL header in the `esds` - // DecoderSpecificInfo; the 3GPP/QuickTime H.263 entries (`s263`, `h263`) - // carry no configuration the baseline decoder needs (`d263` is ignored). - b"mp4v" => (CodecId::Mpeg4Part2, b"esds"), - b"s263" | b"h263" => (CodecId::Mpeg4Part2, b"\0\0\0\0"), - // DV: QuickTime sample entries for DV25/DVCPRO25 (`dvc `/`dvcp`/`dvpp`), DVCPRO50 - // (`dv5n`/`dv5p`) and DVCPRO HD (`dvhq`/`dvh3`/`dvh5`/`dvh6`/`dvhp`...). The frame is - // self-describing: no configuration box. - b"dvc " | b"dvcp" | b"dvpp" | b"dvcs" | b"dvis" | b"dv5n" | b"dv5p" | b"dv5q" | b"dvhq" - | b"dvh1" | b"dvh2" | b"dvh3" | b"dvh4" | b"dvh5" | b"dvh6" | b"dvhp" | b"dvhs" - | b"dvhn" | b"dvhr" => (CodecId::DvVideo, b"\0\0\0\0"), - _ => return None, - }) -} - /// A `ColourInformationBox`: the H.273 triplet, when it is the `nclx`/`nclc` /// kind rather than an ICC profile — as the raw triplet *and* as the tags that /// triplet really states. diff --git a/crates/ec-mp4/src/lib.rs b/crates/ec-mp4/src/lib.rs index e1fadf23..692993bf 100644 --- a/crates/ec-mp4/src/lib.rs +++ b/crates/ec-mp4/src/lib.rs @@ -52,6 +52,7 @@ mod asc; mod boxes; +mod codec_table; mod demux; mod esds; mod mux;