use ec_core::BitWriter; use crate::entropy::{BlockCat, MbCtx}; use super::cabac_enc::CabacEnc; use super::vlc::{write_cbp, write_residual_block}; pub(crate) fn sub_block_4x4(c8: &[i32; 64], j: usize) -> [i32; 16] { std::array::from_fn(|i| c8[4 * i + j]) } #[derive(Clone)] pub(crate) enum EncEntropy { Cavlc { w: BitWriter, skip_run: u32, }, Cabac(Box), } pub(crate) fn append_bits(dst: &mut BitWriter, src: &BitWriter, from: u64) { let bytes = src.as_bytes(); for pos in from..src.bit_len() { let byte = bytes[(pos / 8) as usize]; dst.write_bit(byte >> (7 - pos % 8) & 1 == 1); } } pub(crate) fn aligned_empty(w: &BitWriter) -> BitWriter { let mut fresh = BitWriter::new(); for _ in 0..w.bit_len() % 8 { fresh.write_bit(false); } fresh } impl EncEntropy { pub(crate) fn probe_copy(&self) -> EncEntropy { match self { EncEntropy::Cavlc { w, skip_run } => EncEntropy::Cavlc { w: aligned_empty(w), skip_run: *skip_run, }, EncEntropy::Cabac(c) => EncEntropy::Cabac(Box::new(c.probe_copy())), } } pub(crate) fn adopt(&mut self, trial: EncEntropy) { match (self, trial) { ( EncEntropy::Cavlc { w, skip_run }, EncEntropy::Cavlc { w: tw, skip_run: tskip, }, ) => { let from = w.bit_len() % 8; append_bits(w, &tw, from); *skip_run = tskip; } (EncEntropy::Cabac(c), EncEntropy::Cabac(t)) => c.adopt(*t), _ => unreachable!("adopt across entropy coders"), } } pub(crate) fn cavlc(w: BitWriter) -> EncEntropy { EncEntropy::Cavlc { w, skip_run: 0 } } pub(crate) fn cabac(mut w: BitWriter, slice_qp: i32, init_column: usize) -> EncEntropy { while !w.is_byte_aligned() { w.write_bit(true); } EncEntropy::Cabac(Box::new(CabacEnc::new(w, slice_qp, init_column))) } pub(crate) fn bit_len(&self) -> u64 { match self { EncEntropy::Cavlc { w, .. } => w.bit_len(), EncEntropy::Cabac(c) => c.bit_len(), } } pub(crate) fn begin_mb(&mut self, ctx: &MbCtx) { if let EncEntropy::Cabac(c) = self { c.begin_mb(ctx); } } pub(crate) fn set_intra(&mut self, intra: bool) { if let EncEntropy::Cabac(c) = self { c.set_intra(intra); } } pub(crate) fn skipped_mb(&mut self, inc: usize) { match self { EncEntropy::Cavlc { skip_run, .. } => *skip_run += 1, EncEntropy::Cabac(c) => c.mb_skip_flag(true, inc), } } pub(crate) fn coded_mb(&mut self, p_slice: bool, inc: usize) { if !p_slice { return; } match self { EncEntropy::Cavlc { w, skip_run } => { w.write_ue(*skip_run); *skip_run = 0; } EncEntropy::Cabac(c) => c.mb_skip_flag(false, inc), } } pub(crate) fn mb_type(&mut self, p_slice: bool, value: u32) { match self { EncEntropy::Cavlc { w, .. } => w.write_ue(value), EncEntropy::Cabac(c) => { if p_slice { c.mb_type_p(value); } else { c.mb_type_i(value); } } } } pub(crate) fn sub_mb_type_p(&mut self, value: u32) { match self { EncEntropy::Cavlc { w, .. } => w.write_ue(value), EncEntropy::Cabac(c) => c.sub_mb_type_p(value), } } pub(crate) fn intra4x4_pred_mode(&mut self, rem: Option) { match self { EncEntropy::Cavlc { w, .. } => match rem { None => w.write_bit(true), Some(r) => { w.write_bit(false); w.write_bits(u32::from(r), 3); } }, EncEntropy::Cabac(c) => c.intra4x4_pred_mode(rem), } } pub(crate) fn transform_size_8x8_flag(&mut self, flag: bool) { match self { EncEntropy::Cavlc { w, .. } => w.write_bit(flag), EncEntropy::Cabac(c) => c.transform_size_8x8_flag(flag), } } pub(crate) fn intra_chroma_pred_mode(&mut self, mode: u8) { match self { EncEntropy::Cavlc { w, .. } => w.write_ue(u32::from(mode)), EncEntropy::Cabac(c) => c.intra_chroma_pred_mode(mode), } } pub(crate) fn coded_block_pattern(&mut self, luma: u8, chroma: u8, intra: bool) { match self { EncEntropy::Cavlc { w, .. } => write_cbp(w, luma, chroma, intra), EncEntropy::Cabac(c) => c.coded_block_pattern(luma, chroma), } } pub(crate) fn mb_qp_delta(&mut self, delta: i32) { match self { EncEntropy::Cavlc { w, .. } => w.write_se(delta), EncEntropy::Cabac(c) => c.mb_qp_delta(delta), } } pub(crate) fn mvd(&mut self, comp: usize, inc: usize, value: i32) { match self { EncEntropy::Cavlc { w, .. } => w.write_se(value), EncEntropy::Cabac(c) => c.mvd(comp, inc, value), } } pub(crate) fn residual_block( &mut self, coeff: &[i32], cat: BlockCat, na: Option, nb: Option, ) -> u8 { if cat == BlockCat::Luma8x8 { let EncEntropy::Cabac(c) = self else { unreachable!("CAVLC codes an 8x8 block as four interleaved 4x4 blocks"); }; return c.residual_block_8x8(coeff.try_into().expect("64 coefficients")); } match self { EncEntropy::Cavlc { w, .. } => { let nc = if matches!(cat, BlockCat::ChromaDc(_)) { -1 } else { match (na, nb) { (Some(a), Some(b)) => (i32::from(a) + i32::from(b) + 1) >> 1, (Some(a), None) => i32::from(a), (None, Some(b)) => i32::from(b), (None, None) => 0, } }; write_residual_block(w, coeff, cat.max_num_coeff(), nc) } EncEntropy::Cabac(c) => c.residual_block(coeff, cat, na, nb), } } pub(crate) fn residual_block_8x8_cost(&self, coeff: &[i32; 64]) -> Option { match self { EncEntropy::Cavlc { .. } => None, EncEntropy::Cabac(c) => Some(c.residual_block_8x8_cost(coeff)), } } pub(crate) fn residual_block_cost( &self, coeff: &[i32], cat: BlockCat, na: Option, nb: Option, ) -> Option { match self { EncEntropy::Cavlc { .. } => None, EncEntropy::Cabac(c) => Some(c.residual_block_cost(coeff, cat, na, nb)), } } pub(crate) fn is_cabac(&self) -> bool { matches!(self, EncEntropy::Cabac(_)) } pub(crate) fn end_of_slice(&mut self, last: bool) { if let (EncEntropy::Cabac(c), false) = (self, last) { c.not_end_of_slice(); } } pub(crate) fn finish(self, p_slice: bool) -> Vec { match self { EncEntropy::Cavlc { mut w, skip_run } => { if p_slice && skip_run > 0 { w.write_ue(skip_run); } w.write_bit(true); w.align_to_byte(); w.into_bytes() } EncEntropy::Cabac(c) => c.finish().into_bytes(), } } }