use crate::bitstream::emulation_prevention;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum NalUnitType {
Idr = 5,
Sps = 7,
Pps = 8,
}
impl NalUnitType {
pub fn code(self) -> u8 {
self as u8
}
}
pub const START_CODE: [u8; 4] = [0x00, 0x00, 0x00, 0x01];
pub fn nal_unit(nal_type: NalUnitType, nal_ref_idc: u8, rbsp: &[u8]) -> Vec<u8> {
let t = nal_type.code() & 0x1F;
let ref_idc = nal_ref_idc & 0x03;
let header = (ref_idc << 5) | t;
let ebsp = emulation_prevention(rbsp);
let mut out = Vec::with_capacity(1 + ebsp.len());
out.push(header);
out.extend_from_slice(&ebsp);
out
}
pub fn nal_unit_base(nal_type: NalUnitType, rbsp: &[u8]) -> Vec<u8> {
nal_unit(nal_type, 3, rbsp)
}
pub fn push_annexb(out: &mut Vec<u8>, nal: &[u8]) {
out.extend_from_slice(&START_CODE);
out.extend_from_slice(nal);
}
pub fn split_annexb(stream: &[u8]) -> Vec<Vec<u8>> {
let mut starts = Vec::new();
let mut i = 0;
while i + 3 <= stream.len() {
if stream[i] == 0 && stream[i + 1] == 0 && stream[i + 2] == 1 {
starts.push((i, 3));
i += 3;
} else if i + 4 <= stream.len()
&& stream[i] == 0
&& stream[i + 1] == 0
&& stream[i + 2] == 0
&& stream[i + 3] == 1
{
starts.push((i, 4));
i += 4;
} else {
i += 1;
}
}
let mut nals = Vec::new();
for (n, &(off, len)) in starts.iter().enumerate() {
let end = starts.get(n + 1).map(|&(o, _)| o).unwrap_or(stream.len());
nals.push(stream[off + len..end].to_vec());
}
nals
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn header_bytes() {
assert_eq!(nal_unit_base(NalUnitType::Sps, &[])[0], 0x67);
assert_eq!(nal_unit_base(NalUnitType::Pps, &[])[0], 0x68);
assert_eq!(nal_unit_base(NalUnitType::Idr, &[])[0], 0x65);
}
#[test]
fn payload_gets_emulation_prevention() {
let nal = nal_unit_base(NalUnitType::Pps, &[0x00, 0x00, 0x01, 0xFF]);
assert_eq!(&nal[1..], &[0x00, 0x00, 0x03, 0x01, 0xFF]);
}
#[test]
fn annexb_and_split_roundtrip() {
let nals = [
nal_unit_base(NalUnitType::Sps, &[0xAA]),
nal_unit_base(NalUnitType::Pps, &[0xBB]),
];
let mut stream = Vec::new();
for nal in &nals {
push_annexb(&mut stream, nal);
}
assert_eq!(split_annexb(&stream), nals);
}
}