diff --git a/crates/ec-mp4/src/config.rs b/crates/ec-mp4/src/config.rs index b1d08ad6..5e7278e1 100644 --- a/crates/ec-mp4/src/config.rs +++ b/crates/ec-mp4/src/config.rs @@ -1,45 +1,10 @@ //! Turns encoder output (Annex-B) into mp4 samples and sample-entry config records. -const NAL_IDR: u8 = 5; -const NAL_SEI: u8 = 6; -const NAL_SPS: u8 = 7; -const NAL_PPS: u8 = 8; -const NAL_AUD: u8 = 9; - const HEVC_VPS: u8 = 32; const HEVC_SPS: u8 = 33; const HEVC_PPS: u8 = 34; const HEVC_AUD: u8 = 35; -const AV1C_HEAD: [u8; 4] = [0x81, 0x1F, 0x0C, 0x00]; - -/// An H.264 sample, whether it is an IDR, and its first SPS and PPS when present. -pub type H264Split = (Vec, bool, Option<(Vec, Vec)>); - -/// Splits an H.264 Annex-B access unit into a length-prefixed sample. -/// -/// SPS, PPS, AUD and SEI are stripped from the sample. `None` when the unit holds no coded slice. -pub fn h264_split_annex_b(annex_b: &[u8]) -> Option { - let nals = split_annex_b(annex_b); - let first = |kind: u8| nals.iter().copied().find(|n| nal_type(n) == kind); - let params = match (first(NAL_SPS), first(NAL_PPS)) { - (Some(sps), Some(pps)) => Some((sps.to_vec(), pps.to_vec())), - _ => None, - }; - let mut out = Vec::with_capacity(annex_b.len()); - let mut is_idr = false; - for nal in nals { - match nal_type(nal) { - NAL_SPS | NAL_PPS | NAL_AUD | NAL_SEI => continue, - NAL_IDR => is_idr = true, - _ => {} - } - out.extend_from_slice(&(nal.len() as u32).to_be_bytes()); - out.extend_from_slice(nal); - } - (!out.is_empty()).then_some((out, is_idr, params)) -} - /// An H.264 `AVCDecoderConfigurationRecord` for one SPS and one PPS, with four-byte NAL length prefixes. pub fn avcc_record(sps: &[u8], pps: &[u8]) -> Vec { let profile = sps.get(1..4).unwrap_or(&[0, 0, 0]); @@ -112,17 +77,6 @@ pub fn annex_b_to_hvcc(annex_b: &[u8]) -> Option> { (!out.is_empty()).then_some(out) } -/// An `av1C` record: the four fixed bytes, then the sequence header OBU as the config OBUs. -pub fn av1c_record(ten_bit: bool, config_obus: &[u8]) -> Vec { - let mut head = AV1C_HEAD; - if ten_bit { - head[2] |= 0x40; - } - let mut out = head.to_vec(); - out.extend_from_slice(config_obus); - out -} - fn hevc_nal_type(nal: &[u8]) -> u8 { nal.first().map_or(0xFF, |b| (b >> 1) & 0x3F) } @@ -165,7 +119,3 @@ fn push_nal<'a>(nals: &mut Vec<&'a [u8]>, nal: &'a [u8]) { nals.push(&nal[..end]); } } - -fn nal_type(nal: &[u8]) -> u8 { - nal.first().map_or(0, |b| b & 0x1F) -} diff --git a/crates/ec-mp4/src/demux.rs b/crates/ec-mp4/src/demux.rs index 6a86a114..e7118f93 100644 --- a/crates/ec-mp4/src/demux.rs +++ b/crates/ec-mp4/src/demux.rs @@ -960,7 +960,7 @@ impl<'a> SampleTable<'a> { } b"stsc" => { let (_, _, rest) = full(payload)?; - let count = count_of(rest, 12, file_len)?; + let count = count_of(rest, 12)?; me.stsc = Vec::with_capacity(count); for i in 0..count { me.stsc @@ -987,7 +987,7 @@ impl<'a> SampleTable<'a> { } b"co64" => { let (_, _, rest) = full(payload)?; - let count = count_of(rest, 8, file_len)?; + let count = count_of(rest, 8)?; me.chunks = Vec::with_capacity(count); for i in 0..count { me.chunks.push(be64(rest, 4 + i * 8)?); @@ -1731,7 +1731,7 @@ fn unpack_language(packed: u16) -> Option { /// The `(count, value)` pairs of an `stts`/`ctts` table. fn pairs(payload: &[u8], make: impl Fn(u32, u32) -> T) -> Result> { let (_, _, rest) = full(payload)?; - let count = count_of(rest, 8, u64::MAX)?; + let count = count_of(rest, 8)?; let mut out = Vec::with_capacity(count); for i in 0..count { out.push(make(be32(rest, 4 + i * 8)?, be32(rest, 8 + i * 8)?)); @@ -1741,7 +1741,7 @@ fn pairs(payload: &[u8], make: impl Fn(u32, u32) -> T) -> Result> { /// The `u32` table of an `stss`/`stco`. fn words(rest: &[u8]) -> Result> { - let count = count_of(rest, 4, u64::MAX)?; + let count = count_of(rest, 4)?; let mut out = Vec::with_capacity(count); for i in 0..count { out.push(be32(rest, 4 + i * 4)?); @@ -1751,7 +1751,7 @@ fn words(rest: &[u8]) -> Result> { /// An entry count, refused when the entries it claims do not fit the box it is /// in — the one check that keeps a crafted count from being allocated. -fn count_of(rest: &[u8], width: usize, _file_len: u64) -> Result { +fn count_of(rest: &[u8], width: usize) -> Result { let count = be32(rest, 0)? as usize; let have = rest.len().saturating_sub(4) / width; if count > have { @@ -1798,7 +1798,7 @@ fn display_rotation(a: i32, b: i32, c: i32, d: i32) -> Rotation { fn elst(payload: &[u8]) -> Result> { let (version, _, rest) = full(payload)?; let width = if version == 0 { 12 } else { 20 }; - let count = count_of(rest, width, u64::MAX)?; + let count = count_of(rest, width)?; let mut out = Vec::with_capacity(count); for i in 0..count { let at = 4 + i * width;