use crate::codec::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 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,
chroma_format_idc,
level_idc: 180,
full_range: false,
pcm_enabled: true,
qp: 26,
}
}
pub fn with_pcm(mut self, enabled: bool) -> Self {
self.pcm_enabled = enabled;
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 ctbs(&self) -> (u32, u32) {
(self.coded_width / CTB_SIZE, self.coded_height / CTB_SIZE)
}
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(CTB_LOG2 - 3); w.write_ue(0); w.write_ue(0); w.write_ue(2); w.write_ue(0); w.write_ue(0); 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(CTB_LOG2 - 3); w.write_ue(0); w.flag(true); }
w.write_ue(0); w.flag(false); w.flag(false); w.flag(false); if cfg.full_range {
w.flag(true); w.flag(false); w.flag(false); w.flag(true); w.write_bits(5, 3); w.flag(true); w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(false); } else {
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_ids(pps_id: u32, sps_id: 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(false); w.write_se(0); w.write_se(0); w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(false); w.flag(true); w.flag(false); w.flag(true); 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());
}
}