use crate::hevc::error::{Error, Result};
use alloc::vec::Vec;
pub mod unit_type {
pub const BLA_W_LP: u8 = 16;
pub const IDR_W_RADL: u8 = 19;
pub const IDR_N_LP: u8 = 20;
pub const RSV_IRAP_VCL23: u8 = 23;
pub const VPS: u8 = 32;
pub const SPS: u8 = 33;
pub const PPS: u8 = 34;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NalHeader {
pub nal_unit_type: u8,
pub layer_id: u8,
pub temporal_id: u8,
}
impl NalHeader {
pub fn parse(nal: &[u8]) -> Result<NalHeader> {
if nal.len() < 2 {
return Err(Error::Truncated);
}
if nal[0] & 0x80 != 0 {
return Err(Error::InvalidData("forbidden_zero_bit set"));
}
let tid_plus1 = nal[1] & 0x07;
if tid_plus1 == 0 {
return Err(Error::InvalidData("nuh_temporal_id_plus1 is zero"));
}
Ok(NalHeader {
nal_unit_type: (nal[0] >> 1) & 0x3f,
layer_id: ((nal[0] & 1) << 5) | (nal[1] >> 3),
temporal_id: tid_plus1 - 1,
})
}
pub fn is_vcl(&self) -> bool {
self.nal_unit_type < 32
}
pub fn is_irap(&self) -> bool {
(unit_type::BLA_W_LP..=unit_type::RSV_IRAP_VCL23).contains(&self.nal_unit_type)
}
}
#[derive(Debug, Clone, Default)]
pub struct Rbsp {
pub data: Vec<u8>,
pub epb: Vec<u32>,
}
impl Rbsp {
pub fn from_nal(nal: &[u8]) -> Result<Rbsp> {
if nal.len() < 2 {
return Err(Error::Truncated);
}
let mut out = Vec::with_capacity(nal.len() - 2);
let mut epb = Vec::new();
let mut zeros = 0usize;
let mut i = 2usize;
while i < nal.len() {
let b = nal[i];
if zeros >= 2 && b == 0x03 {
epb.push(i as u32);
zeros = 0;
i += 1;
continue;
}
zeros = if b == 0 { zeros + 1 } else { 0 };
out.push(b);
i += 1;
}
Ok(Rbsp { data: out, epb })
}
pub fn rbsp_to_nal(&self, rbsp_off: usize) -> usize {
let mut nal_off = rbsp_off + 2;
for &p in &self.epb {
if (p as usize) <= nal_off {
nal_off += 1;
} else {
break;
}
}
nal_off
}
pub fn nal_to_rbsp(&self, nal_off: usize) -> usize {
let mut removed = 0usize;
for &p in &self.epb {
if (p as usize) < nal_off {
removed += 1;
} else {
break;
}
}
nal_off.saturating_sub(2 + removed)
}
}
#[cfg(test)]
pub fn split_annexb(stream: &[u8]) -> Vec<&[u8]> {
let mut out = Vec::new();
let n = stream.len();
let mut i = 0usize;
let mut start = None;
while i + 3 <= n {
if stream[i] == 0 && stream[i + 1] == 0 && stream[i + 2] == 1 {
start = Some(i + 3);
i += 3;
break;
}
i += 1;
}
let mut cur = match start {
Some(s) => s,
None => return out,
};
while i + 3 <= n {
if stream[i] == 0 && stream[i + 1] == 0 && stream[i + 2] == 1 {
let mut end = i;
if end > cur && stream[end - 1] == 0 {
end -= 1;
}
if end > cur {
out.push(&stream[cur..end]);
}
cur = i + 3;
i += 3;
} else {
i += 1;
}
}
if cur < n {
out.push(&stream[cur..n]);
}
out
}
#[cfg(test)]
#[path = "nal_tests.rs"]
mod tests;