//! Turns encoder output (Annex-B) into mp4 samples and sample-entry config records. const HEVC_VPS: u8 = 32; const HEVC_SPS: u8 = 33; const HEVC_PPS: u8 = 34; const HEVC_AUD: u8 = 35; /// 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]); let mut out = vec![1, profile[0], profile[1], profile[2], 0xFF, 0xE1]; out.extend_from_slice(&(sps.len() as u16).to_be_bytes()); out.extend_from_slice(sps); out.push(1); out.extend_from_slice(&(pps.len() as u16).to_be_bytes()); out.extend_from_slice(pps); out } /// An HEVC `HEVCDecoderConfigurationRecord` built from the VPS, SPS and PPS of an Annex-B access unit. /// /// `None` when the unit lacks a VPS, SPS or PPS. pub fn hvcc_record(annex_b: &[u8], ten_bit: bool) -> Option> { let nals = split_annex_b(annex_b); let of = |kind: u8| nals.iter().copied().find(|n| hevc_nal_type(n) == kind); let (vps, sps) = (of(HEVC_VPS)?, of(HEVC_SPS)?); let ppss: Vec<&[u8]> = nals .iter() .copied() .filter(|n| hevc_nal_type(n) == HEVC_PPS) .collect(); if ppss.is_empty() { return None; } let rbsp = unescape(sps.get(2..)?); let mut rec = vec![1u8]; rec.extend_from_slice(rbsp.get(1..13)?); rec.extend_from_slice(&[0xF0, 0x00]); rec.push(0xFC); rec.push(0xFC | 1); let depth = if ten_bit { 0xFA } else { 0xF8 }; rec.push(depth); rec.push(depth); rec.extend_from_slice(&0u16.to_be_bytes()); rec.push(0b0000_1111); rec.push(3); for (kind, units) in [ (HEVC_VPS, vec![vps]), (HEVC_SPS, vec![sps]), (HEVC_PPS, ppss), ] { rec.push(0x80 | kind); rec.extend_from_slice(&(units.len() as u16).to_be_bytes()); for nal in units { rec.extend_from_slice(&(nal.len() as u16).to_be_bytes()); rec.extend_from_slice(nal); } } Some(rec) } /// Splits an HEVC Annex-B access unit into a length-prefixed sample, dropping VPS, SPS, PPS and AUD. /// /// `None` when the unit holds no NAL left to write. pub fn annex_b_to_hvcc(annex_b: &[u8]) -> Option> { let mut out = Vec::with_capacity(annex_b.len()); for nal in split_annex_b(annex_b) { if matches!( hevc_nal_type(nal), HEVC_VPS | HEVC_SPS | HEVC_PPS | HEVC_AUD ) { continue; } out.extend_from_slice(&(nal.len() as u32).to_be_bytes()); out.extend_from_slice(nal); } (!out.is_empty()).then_some(out) } fn hevc_nal_type(nal: &[u8]) -> u8 { nal.first().map_or(0xFF, |b| (b >> 1) & 0x3F) } fn unescape(nal: &[u8]) -> Vec { let mut out = Vec::with_capacity(nal.len()); for (i, &byte) in nal.iter().enumerate() { if byte == 3 && i >= 2 && nal[i - 1] == 0 && nal[i - 2] == 0 { continue; } out.push(byte); } out } fn split_annex_b(src: &[u8]) -> Vec<&[u8]> { let mut nals = Vec::new(); let mut start = None; let mut i = 0; while i + 2 < src.len() { if src[i] == 0 && src[i + 1] == 0 && src[i + 2] == 1 { if let Some(s) = start { push_nal(&mut nals, &src[s..i]); } i += 3; start = Some(i); } else { i += 1; } } if let Some(s) = start { push_nal(&mut nals, &src[s..]); } nals } fn push_nal<'a>(nals: &mut Vec<&'a [u8]>, nal: &'a [u8]) { let end = nal.iter().rposition(|&b| b != 0).map_or(0, |p| p + 1); if end > 0 { nals.push(&nal[..end]); } }