pub struct HvccArray<'a> {
pub nal_type: u8,
pub complete: bool,
pub nals: &'a [Vec<u8>],
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HvccProfile {
pub profile_idc: u8,
pub level_idc: u8,
pub chroma_format_idc: u8,
pub bit_depth_luma_minus8: u8,
pub bit_depth_chroma_minus8: u8,
pub constraint_flags: [u8; 6],
pub compat_flags: [u8; 4],
}
impl HvccProfile {
pub fn main_420(level_idc: u8) -> Self {
Self {
profile_idc: 1,
level_idc,
chroma_format_idc: 1,
bit_depth_luma_minus8: 0,
bit_depth_chroma_minus8: 0,
constraint_flags: [0x90, 0x00, 0x00, 0x00, 0x00, 0x00],
compat_flags: [0x60, 0x00, 0x00, 0x00],
}
}
pub fn from_sps(sps: &[u8]) -> Option<Self> {
let rbsp = crate::decode::bits::unescape(sps.get(2..)?);
let ptl = rbsp.get(1..13)?;
let parsed = crate::decode::hevc::params::Sps::parse(&rbsp).ok()?;
Some(Self {
profile_idc: ptl[0] & 0x1F,
level_idc: ptl[11],
chroma_format_idc: parsed.chroma_format_idc as u8,
bit_depth_luma_minus8: parsed.bit_depth_y.saturating_sub(8) as u8,
bit_depth_chroma_minus8: parsed.bit_depth_c.saturating_sub(8) as u8,
constraint_flags: ptl[5..11].try_into().ok()?,
compat_flags: ptl[1..5].try_into().ok()?,
})
}
}
pub fn build_hvcc(profile: &HvccProfile, arrays: &[HvccArray]) -> Vec<u8> {
let mut v = Vec::new();
v.push(1); v.push(profile.profile_idc & 0x1F); v.extend_from_slice(&profile.compat_flags);
v.extend_from_slice(&profile.constraint_flags);
v.push(profile.level_idc);
v.extend_from_slice(&[0xF0, 0x00]); v.push(0xFC); v.push(0xFC | (profile.chroma_format_idc & 3)); v.push(0xF8 | (profile.bit_depth_luma_minus8 & 7)); v.push(0xF8 | (profile.bit_depth_chroma_minus8 & 7)); v.extend_from_slice(&[0x00, 0x00]); v.push(0x0B);
v.push(arrays.len() as u8); for a in arrays {
v.push(((a.complete as u8) << 7) | (a.nal_type & 0x3F)); v.extend_from_slice(&(a.nals.len() as u16).to_be_bytes()); for nal in a.nals {
v.extend_from_slice(&(nal.len() as u16).to_be_bytes());
v.extend_from_slice(nal);
}
}
v
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_from_our_own_sps_matches_what_we_declare() {
let enc = crate::hevc::CompressedEncoder::new(320, 240, 26);
let (_, sps, _) = enc.parameter_sets();
let p = HvccProfile::from_sps(&sps).unwrap();
let coded = HvccProfile { compat_flags: [0x40, 0, 0, 0], ..HvccProfile::main_420(enc.config().level_idc) };
assert_eq!(p, coded);
}
}