//! Entropy seat: the slice-data syntax reader the macroblock loop talks to. //! //! H.264 codes the same slice-data syntax two ways — CAVLC (clause 9.2) and //! CABAC (clause 9.3) — and the difference reaches every syntax element, not //! just the residual blocks. This enum is the single seam between them, so //! [`crate::decoder::decode_macroblock`] stays one fused parse-and-reconstruct //! loop instead of growing a second copy per entropy coder. //! //! Enum dispatch rather than a generic parameter: the variant is fixed for a //! whole slice, so every branch here is perfectly predicted, and one //! monomorphisation of the macroblock loop keeps the instruction cache (and the //! source) half the size. Measured: no change on the CAVLC ns/MB test. //! //! The seat deliberately takes *neighbour values*, never the picture: context //! derivation needs the neighbouring macroblocks' coefficient counts and coded //! block patterns, and passing those in as small copied values keeps the //! flat-array picture state private to the decoder module. use ec_core::error::{Error, Result}; use crate::bits::BitCursor; use crate::cabac::Cabac; use crate::tables::{CBP_INTER_420, CBP_INTER_MONO, CBP_INTRA_420, CBP_INTRA_MONO}; /// Macroblock state bits (`Picture::mb_flags`, and `MbInfo::flags` for a /// neighbour). CABAC context selection reads all of them. /// The macroblock has been decoded in this picture. pub(crate) const FLAG_DECODED: u8 = 1; /// mb_type is I_PCM. pub(crate) const FLAG_PCM: u8 = 2; /// mb_type is one of the Intra_16x16 types. pub(crate) const FLAG_I16: u8 = 4; /// intra_chroma_pred_mode is not 0 (DC). pub(crate) const FLAG_CHROMA_PRED: u8 = 8; /// The macroblock is inter coded. pub(crate) const FLAG_INTER: u8 = 16; /// mb_type is B_Skip or B_Direct_16x16 (9.3.3.1.1.3 at ctxIdxOffset 27). pub(crate) const FLAG_DIRECT: u8 = 32; /// mb_type is P_Skip or B_Skip (9.3.3.1.1.1). pub(crate) const FLAG_SKIP: u8 = 64; /// transform_size_8x8_flag is 1: the luma residual is four 8x8 transform /// blocks, and an Intra_NxN macroblock predicts in 8x8 blocks (8.3.2). pub(crate) const FLAG_TRANS8X8: u8 = 128; /// Residual block class (spec Table 9-42, the 4:2:0 subset this decoder codes). #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub(crate) enum BlockCat { /// Intra16x16DCLevel, `ctxBlockCat` 0. LumaDc, /// Intra16x16ACLevel, `ctxBlockCat` 1. LumaAc, /// LumaLevel4x4, `ctxBlockCat` 2. Luma4x4, /// ChromaDCLevel of component `iCbCr`, `ctxBlockCat` 3. ChromaDc(u8), /// ChromaDCLevel of component `iCbCr` for 4:2:2 (2x4 DC, eight levels), /// `ctxBlockCat` 3. ChromaDc422(u8), /// ChromaACLevel, `ctxBlockCat` 4. ChromaAc, /// LumaLevel8x8, `ctxBlockCat` 5: one 64-coefficient block under /// transform_size_8x8_flag. CABAC codes it whole (no coded_block_flag for /// 4:2:0); CAVLC never sees this category, it codes the block as four /// interleaved `Luma4x4` blocks (7.3.5.3.1). Luma8x8, } impl BlockCat { /// `maxNumCoeff` (Table 9-42). pub(crate) const fn max_num_coeff(self) -> usize { match self { BlockCat::LumaDc | BlockCat::Luma4x4 => 16, BlockCat::LumaAc | BlockCat::ChromaAc => 15, BlockCat::ChromaDc(_) => 4, BlockCat::ChromaDc422(_) => 8, BlockCat::Luma8x8 => 64, } } /// `ctxBlockCat` (Table 9-42). pub(crate) const fn ctx_block_cat(self) -> usize { match self { BlockCat::LumaDc => 0, BlockCat::LumaAc => 1, BlockCat::Luma4x4 => 2, BlockCat::ChromaDc(_) | BlockCat::ChromaDc422(_) => 3, BlockCat::ChromaAc => 4, BlockCat::Luma8x8 => 5, } } } /// What a context-adaptive reader needs to know about one neighbouring /// macroblock. #[derive(Clone, Copy)] pub(crate) struct MbInfo { /// The `FLAG_*` bits above. pub flags: u8, /// `CodedBlockPatternLuma | CodedBlockPatternChroma << 4`. pub cbp: u8, /// coded_block_flag of the DC blocks: bit 0 luma, bit 1 Cb, bit 2 Cr. pub dc_cbf: u8, } /// Neighbourhood of the macroblock about to be parsed: left (A) and above (B) /// per clause 6.4.11.1, `None` when outside the picture or in another slice. #[derive(Clone, Copy, Default)] pub(crate) struct MbCtx { pub a: Option, pub b: Option, /// ctxIdxInc for the first mb_qp_delta bin (clause 9.3.3.1.1.5), derived by /// the caller from the previous macroblock in decoding order. pub qp_delta_inc: u8, /// The macroblock is a field macroblock (a field picture, or a field /// macroblock of an MBAFF frame): field-coded significance contexts. pub field: bool, /// MBAFF frames only: the left neighbour of the lower 8x8 luma blocks can /// be a different macroblock from the one beside the upper blocks, and the /// 8x8 block of the neighbour that is adjacent differs with it. pub mbaff_a: Option, } /// See [`MbCtx::mbaff_a`]. #[derive(Clone, Copy)] pub(crate) struct MbaffA { /// The macroblock left of luma row 8. pub lower: Option, /// The 8x8 block index inside the neighbour that bins 0 (upper) and 2 /// (lower) of coded_block_pattern read. pub blk: [u8; 2], } /// The slice-data syntax reader. /// /// The CABAC variant is much the larger of the two (it carries 436 context /// variables), and stays inline on purpose: one of these lives per slice on the /// stack, so boxing it would trade a compile-time size warning for a heap /// allocation in a decode loop that is otherwise allocation free. #[allow(clippy::large_enum_variant)] pub(crate) enum Entropy<'a> { Cavlc(BitCursor<'a>), Cabac(Cabac<'a>), } impl<'a> Entropy<'a> { /// A CAVLC reader over `rbsp`, positioned just past the slice header. pub(crate) fn cavlc(rbsp: &'a [u8], header_bits: u64) -> Entropy<'a> { Entropy::Cavlc(BitCursor::new(rbsp, header_bits)) } /// A CABAC reader: consumes the `cabac_alignment_one_bit`s and initialises /// the context variables and the arithmetic decoding engine (clause 9.3.1). /// `init_column` is 0 for I and SI slices, `cabac_init_idc + 1` otherwise. pub(crate) fn cabac( rbsp: &'a [u8], header_bits: u64, slice_qp: i32, init_column: usize, ) -> Result> { Ok(Entropy::Cabac(Cabac::new( rbsp, header_bits, slice_qp, init_column, )?)) } /// True for the CABAC variant. #[inline] pub(crate) fn is_cabac(&self) -> bool { matches!(self, Entropy::Cabac(_)) } /// Declare whether the macroblock being parsed is intra coded, which /// CABAC needs for the coded_block_flag contexts of 9.3.3.1.1.9. #[inline] pub(crate) fn set_intra(&mut self, intra: bool) { if let Entropy::Cabac(c) = self { c.set_intra(intra); } } /// `mb_skip_run` (7.3.4), CAVLC only. pub(crate) fn mb_skip_run(&mut self) -> Result { match self { Entropy::Cavlc(r) => r.read_ue(), Entropy::Cabac(_) => Err(Error::corrupt("mb_skip_run in a CABAC slice")), } } /// `mb_skip_flag` (7.3.4), CABAC only; `inc` is condTermFlagA + /// condTermFlagB per 9.3.3.1.1.1. pub(crate) fn mb_skip_flag(&mut self, b_slice: bool, inc: usize) -> Result { match self { Entropy::Cavlc(_) => Err(Error::corrupt("mb_skip_flag in a CAVLC slice")), Entropy::Cabac(c) => Ok(c.mb_skip_flag(b_slice, inc)), } } /// `mb_field_decoding_flag` (7.3.4); `inc` is the CABAC context increment. pub(crate) fn mb_field_flag(&mut self, inc: usize) -> Result { match self { Entropy::Cavlc(r) => r.read_bit(), Entropy::Cabac(c) => Ok(c.mb_field_decoding_flag(inc)), } } /// The next CAVLC bit without consuming it (the field flag of the bottom /// macroblock when the top one is skipped); `false` for CABAC. pub(crate) fn peek_bit(&mut self) -> bool { match self { Entropy::Cavlc(r) => r.peek16() & 0x8000 != 0, Entropy::Cabac(_) => false, } } /// `mb_type` of a P or SP slice: 0..4, or 5 + the I-slice mb_type. pub(crate) fn mb_type_p(&mut self) -> Result { match self { Entropy::Cavlc(r) => { let t = r.read_ue()?; if t > 30 { return Err(Error::corrupt("P-slice mb_type > 30")); } Ok(t) } Entropy::Cabac(c) => c.mb_type_p(), } } /// `mb_type` of a B slice: 0..22, or 23 + the I-slice mb_type. pub(crate) fn mb_type_b(&mut self) -> Result { match self { Entropy::Cavlc(r) => { let t = r.read_ue()?; if t > 48 { return Err(Error::corrupt("B-slice mb_type > 48")); } Ok(t) } Entropy::Cabac(c) => c.mb_type_b(), } } /// `sub_mb_type[ ]` of a P (`b_slice` false) or B macroblock. pub(crate) fn sub_mb_type(&mut self, b_slice: bool) -> Result { let max = if b_slice { 12 } else { 3 }; match self { Entropy::Cavlc(r) => { let t = r.read_ue()?; if t > max { return Err(Error::corrupt("sub_mb_type out of range")); } Ok(t) } Entropy::Cabac(c) => Ok(if b_slice { c.sub_mb_type_b() } else { c.sub_mb_type_p() }), } } /// `ref_idx_lX` (7.3.5.1): te(v) over `0..=cmax` for CAVLC, unary with the /// neighbour context of 9.3.3.1.1.6 for CABAC. pub(crate) fn ref_idx(&mut self, cmax: u32, inc: usize) -> Result { let v = match self { // te(v), clause 9.1.1: a one-valued range is a single inverted bit. Entropy::Cavlc(r) if cmax == 1 => u32::from(!r.read_bit()?), Entropy::Cavlc(r) => r.read_ue()?, Entropy::Cabac(c) => c.ref_idx(inc)?, }; if v > cmax { return Err(Error::corrupt("ref_idx beyond num_ref_idx_active")); } Ok(v) } /// One `mvd_lX` component (7.3.5.1), in quarter luma samples. pub(crate) fn mvd(&mut self, comp: usize, inc: usize) -> Result { match self { Entropy::Cavlc(r) => r.read_se(), Entropy::Cabac(c) => c.mvd(comp, inc), } } /// `coded_block_pattern` of an inter macroblock, as /// `(CodedBlockPatternLuma, CodedBlockPatternChroma)`. /// /// `has_chroma` is ChromaArrayType 1 or 2: the pattern then has a chroma /// part; for 0 and 3 (Table 9-4 b) it is the luma nibble alone. pub(crate) fn coded_block_pattern_inter(&mut self, has_chroma: bool) -> Result<(u8, u8)> { match self { Entropy::Cavlc(r) => { let code = r.read_ue()? as usize; if !has_chroma { let cbp = *CBP_INTER_MONO .get(code) .ok_or_else(|| Error::corrupt("coded_block_pattern codeNum > 15"))?; return Ok((cbp, 0)); } let cbp = *CBP_INTER_420 .get(code) .ok_or_else(|| Error::corrupt("coded_block_pattern codeNum > 47"))?; Ok((cbp & 15, cbp >> 4)) } Entropy::Cabac(c) => c.coded_block_pattern(has_chroma), } } /// Publish the neighbourhood of the macroblock about to be parsed. #[inline] pub(crate) fn begin_mb(&mut self, ctx: &MbCtx) { if let Entropy::Cabac(c) = self { c.begin_mb(ctx); } } /// `mb_type` of an I slice: 0 (I_NxN), 1..24 (Intra_16x16), 25 (I_PCM). pub(crate) fn mb_type_i(&mut self) -> Result { match self { Entropy::Cavlc(r) => { let t = r.read_ue()?; if t > 25 { return Err(Error::corrupt("I-slice mb_type > 25")); } Ok(t) } Entropy::Cabac(c) => c.mb_type_i(), } } /// `transform_size_8x8_flag` (7.3.5). pub(crate) fn transform_size_8x8_flag(&mut self) -> Result { match self { Entropy::Cavlc(r) => r.read_bit(), Entropy::Cabac(c) => c.transform_size_8x8_flag(), } } /// One 4x4 intra prediction mode: `None` selects the predicted mode, /// `Some(rem)` the `rem_intra4x4_pred_mode` value (7.3.5.1). #[inline] pub(crate) fn intra4x4_pred_mode(&mut self) -> Result> { match self { Entropy::Cavlc(r) => { if r.read_bit()? { Ok(None) } else { Ok(Some(r.read_bits(3)? as u8)) } } Entropy::Cabac(c) => c.intra4x4_pred_mode(), } } /// `intra_chroma_pred_mode` (7.3.5.1), 0..3. pub(crate) fn intra_chroma_pred_mode(&mut self) -> Result { match self { Entropy::Cavlc(r) => { let m = r.read_ue()?; if m > 3 { return Err(Error::corrupt("intra_chroma_pred_mode > 3")); } Ok(m as u8) } Entropy::Cabac(c) => c.intra_chroma_pred_mode(), } } /// `coded_block_pattern` of an Intra_4x4 macroblock, as /// `(CodedBlockPatternLuma, CodedBlockPatternChroma)`. pub(crate) fn coded_block_pattern_intra(&mut self, has_chroma: bool) -> Result<(u8, u8)> { match self { Entropy::Cavlc(r) => { // me(v), clause 9.1.2 Table 9-4. let code = r.read_ue()? as usize; if !has_chroma { let cbp = *CBP_INTRA_MONO .get(code) .ok_or_else(|| Error::corrupt("coded_block_pattern codeNum > 15"))?; return Ok((cbp, 0)); } let cbp = *CBP_INTRA_420 .get(code) .ok_or_else(|| Error::corrupt("coded_block_pattern codeNum > 47"))?; Ok((cbp & 15, cbp >> 4)) } Entropy::Cabac(c) => c.coded_block_pattern(has_chroma), } } /// `mb_qp_delta` (7.4.5), already range checked. pub(crate) fn mb_qp_delta(&mut self) -> Result { let delta = match self { Entropy::Cavlc(r) => r.read_se()?, Entropy::Cabac(c) => c.mb_qp_delta()?, }; // The widest range any supported bit depth allows; the caller narrows it // to the picture's QpBdOffset. if !(-32..=31).contains(&delta) { return Err(Error::corrupt("mb_qp_delta outside [-26, 25]")); } Ok(delta) } /// Decode one residual block into `coeff[0..cat.max_num_coeff()]` in scan /// order, returning the number of non-zero coefficients. /// /// `na`/`nb` are the same counts for the left and above neighbouring blocks /// of the same class, `None` when that neighbour is unavailable. CAVLC /// averages them into nC (9.2.1); CABAC turns them into the /// coded_block_flag context increment (9.3.3.1.1.9). #[inline] pub(crate) fn residual_block( &mut self, coeff: &mut [i32; 16], cat: BlockCat, na: Option, nb: Option, ) -> Result { self.residual_block_c(coeff, cat, 0, na, nb) } /// [`Entropy::residual_block`] for colour component `comp` of a 4:4:4 /// macroblock (0 Y, 1 Cb, 2 Cr): the Cb and Cr residual is coded like luma /// but with context sets of its own. #[inline] pub(crate) fn residual_block_c( &mut self, coeff: &mut [i32; 16], cat: BlockCat, comp: u8, na: Option, nb: Option, ) -> Result { match self { Entropy::Cavlc(r) => { let nc = if matches!(cat, BlockCat::ChromaDc(_)) { -1 } else if matches!(cat, BlockCat::ChromaDc422(_)) { -2 } 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, } }; crate::cavlc::residual_block(r, coeff, cat.max_num_coeff(), nc) } Entropy::Cabac(c) => c.residual_block(coeff, cat, comp, na, nb), } } /// Decode one 8x8 luma residual block into `coeff[0..64]` in scan order, /// returning the number of non-zero coefficients. CABAC only: a CAVLC 8x8 /// block is coded as four interleaved 4x4 blocks (7.3.5.3.1), which the /// caller reads through [`Entropy::residual_block`]. /// /// `comp` is the colour component, and `cbf` the neighbouring blocks' counts /// when coded_block_flag is sent for the 8x8 class (ChromaArrayType 3). pub(crate) fn residual_block_8x8( &mut self, coeff: &mut [i32; 64], comp: u8, cbf: Option<(Option, Option)>, ) -> Result { match self { Entropy::Cavlc(_) => Err(Error::corrupt("8x8 residual block read from a CAVLC slice")), Entropy::Cabac(c) => c.residual_block_8x8(coeff, comp, cbf), } } /// The raw bytes of an I_PCM macroblock (256 luma plus the chroma samples), after /// alignment. A CABAC reader restarts its arithmetic engine afterwards /// (clause 9.3.1.2). pub(crate) fn pcm_block(&mut self, bd: u32, samples: usize) -> Result<&'a [u8]> { // `samples` samples of `bd` bits: 256 luma plus the chroma ones (384 // for 4:2:0, 256 for monochrome, 768 for 4:4:4). let n = samples / 8 * bd as usize; match self { Entropy::Cavlc(r) => { r.align_to_byte(); r.read_bytes(n) } Entropy::Cabac(c) => c.pcm_block(bd, samples), } } /// True while the slice has another macroblock: `more_rbsp_data()` for /// CAVLC, `!end_of_slice_flag` for CABAC. pub(crate) fn more_macroblocks(&mut self) -> Result { match self { Entropy::Cavlc(r) => Ok(r.more_rbsp_data()), Entropy::Cabac(c) => c.more_macroblocks(), } } }