use crate::bitstream::{rbsp_trailing_bits, BitWriter};
#[derive(Clone, Copy, Debug)]
pub struct HevcConfig {
pub width: u32,
pub height: u32,
pub coded_width: u32,
pub coded_height: u32,
pub chroma_format_idc: u8,
pub level_idc: u8,
pub full_range: bool,
pub pcm_enabled: bool,
pub qp: i32,
pub max_transform_hierarchy_depth_intra: u32,
pub ctb_log2: u32,
pub min_cb_log2: u32,
pub max_tb_log2: u32,
pub tile_grid: (u32, u32),
}
pub fn auto_tile_grid(width: u32, height: u32) -> (u32, u32) {
const TARGET: u64 = 4_200_000; let (mut c, mut r) = (1u32, 1u32);
while u64::from(width / c) * u64::from(height / r) > TARGET && c <= 8 && r <= 8 {
if width / c >= height / r {
c *= 2;
} else {
r *= 2;
}
}
(c, r)
}
pub const CTB_SIZE: u32 = 16;
pub const CTB_LOG2: u32 = 4;
impl HevcConfig {
pub fn new(width: u32, height: u32) -> Self {
Self::with_chroma(width, height, 1)
}
pub fn new_monochrome(width: u32, height: u32) -> Self {
Self::with_chroma(width, height, 0)
}
fn with_chroma(width: u32, height: u32, chroma_format_idc: u8) -> Self {
let coded_width = width.div_ceil(CTB_SIZE) * CTB_SIZE;
let coded_height = height.div_ceil(CTB_SIZE) * CTB_SIZE;
Self {
width,
height,
coded_width,
coded_height,
ctb_log2: CTB_LOG2,
min_cb_log2: CTB_LOG2,
max_tb_log2: CTB_LOG2,
tile_grid: (1, 1),
chroma_format_idc,
level_idc: 180,
full_range: false,
pcm_enabled: true,
qp: 26,
max_transform_hierarchy_depth_intra: 0,
}
}
pub fn with_pcm(mut self, enabled: bool) -> Self {
self.pcm_enabled = enabled;
self
}
pub fn with_transform_depth(mut self, depth: u32) -> Self {
self.max_transform_hierarchy_depth_intra = depth;
self
}
pub fn with_level(mut self, level_idc: u8) -> Self {
self.level_idc = level_idc;
self
}
pub fn with_full_range(mut self, full_range: bool) -> Self {
self.full_range = full_range;
self
}
pub fn has_chroma(&self) -> bool {
self.chroma_format_idc != 0
}
pub fn sub_wh(&self) -> (u32, u32) {
if self.has_chroma() {
(2, 2)
} else {
(1, 1)
}
}
pub fn tiles(&self) -> u32 {
self.tile_grid.0 * self.tile_grid.1
}
pub fn with_tiles(mut self, cols: u32, rows: u32) -> Self {
self.tile_grid = (cols.max(1), rows.max(1));
self.clamp_tiles();
self
}
fn clamp_tiles(&mut self) {
let ctb = 1u32 << self.ctb_log2;
let (nx, ny) = self.ctbs();
let max_cols = (nx / 256u32.div_ceil(ctb)).max(1);
let max_rows = (ny / 64u32.div_ceil(ctb)).max(1);
self.tile_grid = (self.tile_grid.0.min(max_cols), self.tile_grid.1.min(max_rows));
}
pub fn with_auto_tiles(self) -> Self {
let (c, r) = auto_tile_grid(self.coded_width, self.coded_height);
self.with_tiles(c, r)
}
pub fn ctbs(&self) -> (u32, u32) {
let ctb = 1 << self.ctb_log2;
(self.coded_width.div_ceil(ctb), self.coded_height.div_ceil(ctb))
}
pub fn with_coding_tree(mut self, ctb_log2: u32, min_cb_log2: u32, max_tb_log2: u32) -> Self {
assert!(min_cb_log2 >= 3 && min_cb_log2 <= ctb_log2, "MinCb in [8, CTB]");
assert!((2..=5).contains(&max_tb_log2), "max TB in [4, 32]");
self.ctb_log2 = ctb_log2;
self.min_cb_log2 = min_cb_log2;
self.max_tb_log2 = max_tb_log2;
let mcb = 1 << min_cb_log2;
self.coded_width = self.width.div_ceil(mcb) * mcb;
self.coded_height = self.height.div_ceil(mcb) * mcb;
self.clamp_tiles();
self
}
fn needs_conf_window(&self) -> bool {
self.coded_width != self.width || self.coded_height != self.height
}
}
fn profile_tier_level(w: &mut BitWriter, monochrome: bool, level_idc: u8) {
w.write_bits(0, 2); w.flag(false); let profile_idc: u32 = if monochrome { 4 } else { 1 };
w.write_bits(profile_idc, 5);
for j in 0..32u32 {
w.flag(j == profile_idc);
}
w.flag(true); w.flag(false); w.flag(false); w.flag(true);
if monochrome {
w.flag(true); w.flag(true); w.flag(true); w.flag(true); w.flag(true); w.flag(true); w.flag(false); w.flag(false); w.flag(true); w.write_bits(0, 34 - 32); w.write_bits(0, 32); w.flag(false); } else {
w.write_bits(0, 43 - 32);
w.write_bits(0, 32);
w.flag(false); }
w.write_bits(level_idc as u32, 8); }
fn sub_layer_ordering(w: &mut BitWriter) {
w.flag(false); w.write_ue(1); w.write_ue(0); w.write_ue(0); }
pub fn write_vps(monochrome: bool) -> Vec<u8> {
let mut w = BitWriter::new();
w.write_bits(0, 4); w.flag(true); w.flag(true); w.write_bits(0, 6); w.write_bits(0, 3); w.flag(true); w.write_bits(0xFFFF, 16); profile_tier_level(&mut w, monochrome, 180);
sub_layer_ordering(&mut w);
w.write_bits(0, 6); w.write_ue(0); w.flag(false); w.flag(false); rbsp_trailing_bits(&mut w);
w.finish()
}
pub fn write_sps(cfg: &HevcConfig) -> Vec<u8> {
write_sps_id(cfg, 0)
}
pub fn write_sps_id(cfg: &HevcConfig, sps_id: u32) -> Vec<u8> {
let mut w = BitWriter::new();
w.write_bits(0, 4); w.write_bits(0, 3); w.flag(true); profile_tier_level(&mut w, !cfg.has_chroma(), cfg.level_idc);
w.write_ue(sps_id); w.write_ue(cfg.chroma_format_idc as u32); w.write_ue(cfg.coded_width); w.write_ue(cfg.coded_height);
if cfg.needs_conf_window() {
w.flag(true); let (subw, subh) = cfg.sub_wh();
w.write_ue(0); w.write_ue((cfg.coded_width - cfg.width) / subw); w.write_ue(0); w.write_ue((cfg.coded_height - cfg.height) / subh); } else {
w.flag(false); }
w.write_ue(0); w.write_ue(0); w.write_ue(0); sub_layer_ordering(&mut w);
w.write_ue(cfg.min_cb_log2 - 3); w.write_ue(cfg.ctb_log2 - cfg.min_cb_log2); w.write_ue(0); w.write_ue(cfg.max_tb_log2 - 2); w.write_ue(0); w.write_ue(cfg.max_transform_hierarchy_depth_intra); w.flag(false); w.flag(false); w.flag(false);
w.flag(cfg.pcm_enabled); if cfg.pcm_enabled {
w.write_bits(7, 4); w.write_bits(7, 4); w.write_ue(cfg.min_cb_log2 - 3); w.write_ue(0); w.flag(true); }
w.write_ue(0); w.flag(false); w.flag(false); w.flag(false); w.flag(true); w.flag(false); w.flag(false); w.flag(true); w.write_bits(5, 3); w.flag(cfg.full_range); w.flag(!cfg.full_range); if !cfg.full_range {
w.write_byte(2); w.write_byte(2); w.write_byte(6); }
w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(false); rbsp_trailing_bits(&mut w);
w.finish()
}
pub fn write_pps() -> Vec<u8> {
write_pps_ids(0, 0)
}
pub fn write_pps_cu_qp_delta() -> Vec<u8> {
write_pps_inner(0, 0, true, true)
}
pub fn write_pps_cu_qp_delta_no_deblock() -> Vec<u8> {
write_pps_inner(0, 0, true, false)
}
pub fn write_pps_ids(pps_id: u32, sps_id: u32) -> Vec<u8> {
write_pps_inner(pps_id, sps_id, false, false)
}
fn write_pps_inner(pps_id: u32, sps_id: u32, cu_qp_delta: bool, deblock: bool) -> Vec<u8> {
write_pps_full(pps_id, sps_id, cu_qp_delta, deblock, (1, 1))
}
pub fn write_pps_tiled(tile_grid: (u32, u32), deblock: bool) -> Vec<u8> {
write_pps_full(0, 0, true, deblock, tile_grid)
}
fn write_pps_full(
pps_id: u32,
sps_id: u32,
cu_qp_delta: bool,
deblock: bool,
tile_grid: (u32, u32),
) -> Vec<u8> {
let mut w = BitWriter::new();
w.write_ue(pps_id); w.write_ue(sps_id); w.flag(false); w.flag(false); w.write_bits(0, 3); w.flag(false); w.flag(false); w.write_ue(0); w.write_ue(0); w.write_se(0); w.flag(false); w.flag(false); w.flag(cu_qp_delta); if cu_qp_delta {
w.write_ue(0); }
w.write_se(0); w.write_se(0); w.flag(false); w.flag(false); w.flag(false); w.flag(false); let tiled = tile_grid.0 * tile_grid.1 > 1;
w.flag(tiled); w.flag(false); if tiled {
w.write_ue(tile_grid.0 - 1); w.write_ue(tile_grid.1 - 1); w.flag(true); w.flag(true); }
w.flag(false); w.flag(true); w.flag(false); w.flag(!deblock); if deblock {
w.write_se(0); w.write_se(0); }
w.flag(false); w.flag(false); w.write_ue(0); w.flag(false); w.flag(false); rbsp_trailing_bits(&mut w);
w.finish()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn config_pads_to_ctb() {
let c = HevcConfig::new(1920, 1080);
assert_eq!(c.coded_width, 1920); assert_eq!(c.coded_height, 1088); assert_eq!(c.ctbs(), (120, 68));
assert!(c.needs_conf_window());
let a = HevcConfig::new(64, 64);
assert_eq!((a.coded_width, a.coded_height), (64, 64));
assert!(!a.needs_conf_window());
}
#[test]
fn param_sets_are_byte_aligned_and_nonempty() {
assert!(!write_vps(false).is_empty());
assert!(!write_sps(&HevcConfig::new(64, 64)).is_empty());
assert!(!write_pps().is_empty());
}
#[test]
fn monochrome_config_and_sps() {
let c = HevcConfig::new_monochrome(64, 64);
assert_eq!(c.chroma_format_idc, 0);
assert!(!c.has_chroma());
assert_eq!(c.sub_wh(), (1, 1));
assert!(!write_sps(&c).is_empty());
assert!(!write_vps(true).is_empty());
}
}