use crate::hevc::bits::BitWriter;
use crate::hevc::cabac::ctx::off;
use crate::hevc::cabac::encoder::Enc;
use alloc::vec::Vec;
pub fn to_nal(nal_type: u8, rbsp: &[u8]) -> Vec<u8> {
let mut out = alloc::vec![nal_type << 1, 1];
let mut zeros = 0usize;
for &b in rbsp {
if zeros >= 2 && b <= 3 {
out.push(3);
zeros = 0;
}
zeros = if b == 0 { zeros + 1 } else { 0 };
out.push(b);
}
out
}
pub fn build_vps() -> Vec<u8> {
let mut w = BitWriter::default();
w.put(0, 4); w.put(3, 2); w.put(0, 6); w.put(0, 3); w.put(1, 1); w.put(0xffff, 16);
profile_tier_level(&mut w);
w.rbsp_trailing();
w.bytes
}
fn profile_tier_level(w: &mut BitWriter) {
w.put(0, 2); w.put(0, 1); w.put(3, 5); for i in 0..32 {
w.put(u32::from(i == 3), 1);
}
w.put(0b1101, 4); w.put(0, 22); w.put(0, 21); w.put(0, 1); w.put(30, 8); }
pub fn build_sps(width: u32, height: u32, chroma_idc: u32, bit_depth: u32) -> Vec<u8> {
let mut w = BitWriter::default();
w.put(0, 4); w.put(0, 3); w.put(1, 1); profile_tier_level(&mut w);
w.ue(0); w.ue(chroma_idc); if chroma_idc == 3 {
w.put(0, 1); }
w.ue(width); w.ue(height); w.put(0, 1); w.ue(bit_depth - 8); w.ue(bit_depth - 8); w.ue(4); w.put(1, 1); w.ue(0);
w.ue(0);
w.ue(0);
w.ue(0); w.ue(3); w.ue(0); w.ue(3); w.ue(0); w.ue(0); w.put(0, 1); w.put(0, 1); w.put(0, 1); w.put(0, 1); w.ue(0); w.put(0, 1); w.put(0, 1); w.put(0, 1); w.put(0, 1); w.put(0, 1); w.rbsp_trailing();
w.bytes
}
pub fn build_pps_opt(tiles: bool) -> Vec<u8> {
let mut w = BitWriter::default();
w.ue(0); w.ue(0); w.put(0, 1); w.put(0, 1); w.put(0, 3); w.put(0, 1); w.put(0, 1); w.ue(0);
w.ue(0);
w.se(0); w.put(0, 1); w.put(0, 1); w.put(0, 1); w.se(0); w.se(0); w.put(0, 1); w.put(0, 2); w.put(0, 1); w.put(u32::from(tiles), 1); w.put(0, 1); if tiles {
w.ue(1); w.ue(1); w.put(1, 1); w.put(1, 1); }
w.put(0, 1); w.put(1, 1); w.put(0, 1); w.put(1, 1); w.put(0, 1); w.put(0, 1); w.ue(0); w.put(0, 1); w.put(0, 1); w.rbsp_trailing();
w.bytes
}
pub fn build_slice(ctbs: usize, chroma_idc: u32) -> Vec<u8> {
let mut w = BitWriter::default();
w.put(1, 1); w.put(0, 1); w.ue(0); w.ue(2); w.se(0); w.put(1, 1); while w.len_bits() % 8 != 0 {
w.put(0, 1);
}
let mut e = Enc::new(26);
for k in 0..ctbs {
encode_ctu(&mut e, chroma_idc);
e.terminate(u32::from(k + 1 == ctbs)); }
let mut out = w.bytes;
out.extend_from_slice(&e.finish());
out
}
pub fn picture_fmt(
width: u32,
height: u32,
chroma_idc: u32,
bit_depth: u32,
) -> (Vec<Vec<u8>>, Vec<u8>) {
let ctbs = (width as usize / 64) * (height as usize / 64);
let sets = alloc::vec![
to_nal(32, &build_vps()),
to_nal(33, &build_sps(width, height, chroma_idc, bit_depth)),
to_nal(34, &build_pps()),
];
(sets, to_nal(19, &build_slice(ctbs, chroma_idc)))
}
pub fn picture(width: u32, height: u32) -> (Vec<Vec<u8>>, Vec<u8>) {
picture_fmt(width, height, 1, 8)
}
pub fn build_pps() -> Vec<u8> {
build_pps_opt(false)
}
fn encode_ctu(e: &mut Enc, chroma_idc: u32) {
e.decision(off::SPLIT_CU, 0); e.decision(off::PREV_INTRA, 1); e.bypass(1); e.bypass(0);
if chroma_idc != 0 {
e.decision(off::INTRA_CHROMA, 0); e.decision(off::CBF_CHROMA, 0); e.decision(off::CBF_CHROMA, 0); }
for _ in 0..4 {
e.decision(off::CBF_LUMA, 0); }
}
pub fn picture_tiled() -> (Vec<Vec<u8>>, Vec<u8>) {
let mut w = BitWriter::default();
w.put(1, 1); w.put(0, 1); w.ue(0); w.ue(2); w.se(0); w.ue(0); w.put(1, 1); while w.len_bits() % 8 != 0 {
w.put(0, 1);
}
let mut out = w.bytes;
for tile in 0..4 {
let mut e = Enc::new(26);
for ctb in 0..4 {
encode_ctu(&mut e, 1);
let last = tile == 3 && ctb == 3;
e.terminate(u32::from(last)); }
if tile != 3 {
e.terminate(1); }
out.extend_from_slice(&e.finish());
}
let sets = alloc::vec![
to_nal(32, &build_vps()),
to_nal(33, &build_sps(256, 256, 1, 8)),
to_nal(34, &build_pps_opt(true)),
];
(sets, to_nal(19, &out))
}