From 6480b8e3ba538ef489aadc81f806c062a9296c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 14 Aug 2026 09:45:10 +0300 Subject: [PATCH] refactor(h264): entropy seat between the macroblock loop and CAVLC The slice-data syntax reader becomes an enum seam (`Entropy`) covering every element the macroblock loop reads: mb_type, the intra prediction modes, coded_block_pattern, mb_qp_delta, the residual blocks, the I_PCM samples and the end-of-slice test. CABAC (clause 9.3) codes all of those differently, so the seam has to sit there rather than around residual_block alone; the parse-and-reconstruct loop stays fused. Enum dispatch, not a generic: the variant is constant for a whole slice, so the branches predict perfectly and the macroblock loop is monomorphised once. The neighbour derivations now hand out the raw (left, above) pair instead of a CAVLC nC, because CABAC reduces the same pair a different way. CAVLC ns/MB, median of three runs, before -> after: BA1_Sony_D 1331 -> 1328, CI_MW_D 1137 -> 1126, NL1_Sony_D 948 -> 933. --- crates/ec-h264/src/decoder.rs | 134 +++++++++--------------- crates/ec-h264/src/entropy.rs | 186 ++++++++++++++++++++++++++++++++++ crates/ec-h264/src/lib.rs | 1 + 3 files changed, 237 insertions(+), 84 deletions(-) create mode 100644 crates/ec-h264/src/entropy.rs diff --git a/crates/ec-h264/src/decoder.rs b/crates/ec-h264/src/decoder.rs index 58334bdb..a2bef463 100644 --- a/crates/ec-h264/src/decoder.rs +++ b/crates/ec-h264/src/decoder.rs @@ -19,13 +19,12 @@ use ec_h264_syntax::{ AnnexBIter, NalHeader, NalUnitType, Pps, SliceHeader, SliceType, Sps, unescape_rbsp, }; -use crate::bits::BitCursor; -use crate::cavlc::residual_block; use crate::deblock::{ chroma_qp, edge_params, filter_chroma_line, filter_luma_h_edge16, filter_luma_line, }; +use crate::entropy::{BlockCat, Entropy}; use crate::pred::{Nbr4, PlaneWindow, add_residual_4x4, pred_4x4, pred_16x16, pred_chroma_8x8}; -use crate::tables::{BLK4_POS, CBP_INTRA_420, CHROMA_QP}; +use crate::tables::{BLK4_POS, CHROMA_QP}; use crate::transform::{ LevelScale4x4, chroma_dc_transform_420, dequant_4x4, inverse_transform_4x4, luma_dc_transform, unzigzag, unzigzag_ac15, @@ -408,7 +407,7 @@ impl Decoder { cr_qp_offset: pps.second_chroma_qp_index_offset, }); - let mut r = BitCursor::new(rbsp, sh.header_bits); + let mut r = Entropy::cavlc(rbsp, sh.header_bits); let mut ctx = SliceCtx { slice_id, @@ -424,7 +423,7 @@ impl Decoder { return Err(Error::corrupt("macroblock address beyond the picture")); } decode_macroblock(&mut self.pic, &self.ls, &mut r, &mut ctx, mb_addr)?; - if !r.more_rbsp_data() { + if !r.more_macroblocks()? { break; } mb_addr += 1; @@ -531,53 +530,37 @@ fn mb_neighbors(pic: &Picture, mb_x: usize, mb_y: usize, slice_id: u16) -> MbNei } } -/// nC for a luma 4x4 block at global block coords `(bx, by)` (spec 9.2.1). +/// Non-zero counts of the left and above neighbours of the luma 4x4 block at +/// global block coords `(bx, by)`, `None` when that neighbour is unavailable +/// (clause 6.4.11.4 plus the availability rules of 6.4.8). #[inline] -fn luma_nc(pic: &Picture, n: &MbNeighbors, bx: usize, by: usize) -> i32 { +fn luma_nz_pair(pic: &Picture, n: &MbNeighbors, bx: usize, by: usize) -> (Option, Option) { let w4 = pic.mb_w * 4; let left_avail = if bx.is_multiple_of(4) { n.a } else { true }; let top_avail = if by.is_multiple_of(4) { n.b } else { true }; - let na = if left_avail && bx > 0 { - i32::from(pic.nz_y[by * w4 + bx - 1]) - } else { - -1 - }; - let nb = if top_avail && by > 0 { - i32::from(pic.nz_y[(by - 1) * w4 + bx]) - } else { - -1 - }; - match (na >= 0, nb >= 0) { - (true, true) => (na + nb + 1) >> 1, - (true, false) => na, - (false, true) => nb, - (false, false) => 0, - } + ( + (left_avail && bx > 0).then(|| pic.nz_y[by * w4 + bx - 1]), + (top_avail && by > 0).then(|| pic.nz_y[(by - 1) * w4 + bx]), + ) } -/// nC for a chroma AC 4x4 block at global chroma block coords. +/// The same for a chroma AC 4x4 block at global chroma block coords. #[inline] -fn chroma_nc(pic: &Picture, n: &MbNeighbors, comp: usize, cx: usize, cy: usize) -> i32 { +fn chroma_nz_pair( + pic: &Picture, + n: &MbNeighbors, + comp: usize, + cx: usize, + cy: usize, +) -> (Option, Option) { let w2 = pic.mb_w * 2; let grid = &pic.nz_c[comp]; let left_avail = if cx.is_multiple_of(2) { n.a } else { true }; let top_avail = if cy.is_multiple_of(2) { n.b } else { true }; - let na = if left_avail && cx > 0 { - i32::from(grid[cy * w2 + cx - 1]) - } else { - -1 - }; - let nb = if top_avail && cy > 0 { - i32::from(grid[(cy - 1) * w2 + cx]) - } else { - -1 - }; - match (na >= 0, nb >= 0) { - (true, true) => (na + nb + 1) >> 1, - (true, false) => na, - (false, true) => nb, - (false, false) => 0, - } + ( + (left_avail && cx > 0).then(|| grid[cy * w2 + cx - 1]), + (top_avail && cy > 0).then(|| grid[(cy - 1) * w2 + cx]), + ) } /// Decode one macroblock of an I slice (spec 7.3.5, 7.4.5) and reconstruct @@ -588,7 +571,7 @@ fn chroma_nc(pic: &Picture, n: &MbNeighbors, comp: usize, cx: usize, cy: usize) fn decode_macroblock( pic: &mut Picture, ls: &[LevelScale4x4; 3], - r: &mut BitCursor<'_>, + r: &mut Entropy<'_>, ctx: &mut SliceCtx, mb_addr: usize, ) -> Result<()> { @@ -603,24 +586,20 @@ fn decode_macroblock( let (bx0, by0) = (mb_x * 4, mb_y * 4); let (cx0, cy0) = (mb_x * 2, mb_y * 2); - let mb_type = r.read_ue()?; - if mb_type > 25 { - return Err(Error::corrupt("I-slice mb_type > 25")); - } + let mb_type = r.mb_type_i()?; if mb_type == 25 { // I_PCM (7.3.5, 8.3.5): raw samples, byte aligned. - r.align_to_byte(); + let pcm = r.pcm_block()?; for y in 0..16 { - let row = r.read_bytes(16)?; let dst = pic.y.at(mb_x * 16, mb_y * 16 + y); - pic.y.data[dst..dst + 16].copy_from_slice(row); + pic.y.data[dst..dst + 16].copy_from_slice(&pcm[y * 16..y * 16 + 16]); } - for plane in [&mut pic.cb, &mut pic.cr] { + for (c, plane) in [&mut pic.cb, &mut pic.cr].into_iter().enumerate() { + let base = 256 + c * 64; for y in 0..8 { - let row = r.read_bytes(8)?; let dst = plane.at(mb_x * 8, mb_y * 8 + y); - plane.data[dst..dst + 8].copy_from_slice(row); + plane.data[dst..dst + 8].copy_from_slice(&pcm[base + y * 8..base + y * 8 + 8]); } } for dy in 0..4 { @@ -656,7 +635,7 @@ fn decode_macroblock( }; // I_NxN transform size flag (High profile only). - if !is_i16 && ctx.transform_8x8_mode && r.read_bit()? { + if !is_i16 && ctx.transform_8x8_mode && r.transform_size_8x8_flag()? { return Err(Error::unsupported( "8x8 transform", "transform_size_8x8_flag 1 selects the 8x8 intra prediction of \ @@ -681,11 +660,10 @@ fn decode_macroblock( } else { 2 }; - let mode = if r.read_bit()? { - pred - } else { - let rem = r.read_bits(3)? as u8; - if rem < pred { rem } else { rem + 1 } + let mode = match r.intra4x4_pred_mode()? { + None => pred, + Some(rem) if rem < pred => rem, + Some(rem) => rem + 1, }; pic.i4_modes[by * w4 + bx] = mode; modes[blk] = mode; @@ -696,30 +674,18 @@ fn decode_macroblock( pic.i4_modes[base..base + 4].fill(2); } } - let chroma_mode = r.read_ue()?; - if chroma_mode > 3 { - return Err(Error::corrupt("intra_chroma_pred_mode > 3")); - } + let chroma_mode = r.intra_chroma_pred_mode()?; - // coded_block_pattern me(v) for I_NxN (9.1.2, Table 9-4). + // coded_block_pattern for I_NxN; Intra_16x16 derives it from mb_type. let (cbp_luma, cbp_chroma) = if is_i16 { (cbp_luma, cbp_chroma) } else { - let code = r.read_ue()? as usize; - let cbp = if code < 48 { - CBP_INTRA_420[code] - } else { - return Err(Error::corrupt("coded_block_pattern codeNum > 47")); - }; - (cbp & 15, cbp >> 4) + r.coded_block_pattern_intra()? }; // mb_qp_delta (7.4.5). if cbp_luma != 0 || cbp_chroma != 0 || is_i16 { - let delta = r.read_se()?; - if !(-26..=25).contains(&delta) { - return Err(Error::corrupt("mb_qp_delta outside [-26, 25]")); - } + let delta = r.mb_qp_delta()?; ctx.qp = (ctx.qp + delta + 52) % 52; } pic.mb_qp[mb_addr] = ctx.qp as u8; @@ -747,8 +713,8 @@ fn decode_macroblock( }; pred_16x16(i16_mode, &mut w, nbr.b, nbr.a); // Luma DC: un-zigzag over the 4x4 DC array, Hadamard + scale. - let nc = luma_nc(pic, &nbr, bx0, by0); - residual_block(r, &mut scan, 16, nc)?; + let (na, nb) = luma_nz_pair(pic, &nbr, bx0, by0); + r.residual_block(&mut scan, BlockCat::LumaDc, na, nb)?; unzigzag(&scan, &mut raster); luma_dc_transform(&mut raster, &ls[0], qp_y); let dc = raster; @@ -756,8 +722,8 @@ fn decode_macroblock( let (dx, dy) = BLK4_POS[blk]; let (bx, by) = (bx0 + dx as usize, by0 + dy as usize); let tc = if cbp_luma != 0 { - let nc = luma_nc(pic, &nbr, bx, by); - residual_block(r, &mut scan, 15, nc)? + let (na, nb) = luma_nz_pair(pic, &nbr, bx, by); + r.residual_block(&mut scan, BlockCat::LumaAc, na, nb)? } else { 0 }; @@ -784,8 +750,8 @@ fn decode_macroblock( let (dx, dy) = BLK4_POS[blk]; let (bx, by) = (bx0 + dx as usize, by0 + dy as usize); let tc = if cbp_luma & (1 << (blk >> 2)) != 0 { - let nc = luma_nc(pic, &nbr, bx, by); - residual_block(r, &mut scan, 16, nc)? + let (na, nb) = luma_nz_pair(pic, &nbr, bx, by); + r.residual_block(&mut scan, BlockCat::Luma4x4, na, nb)? } else { 0 }; @@ -827,12 +793,12 @@ fn decode_macroblock( stride: plane.stride, origin: plane.origin + (mb_y * 8) * plane.stride + mb_x * 8, }; - pred_chroma_8x8(chroma_mode as u8, &mut w, nbr.b, nbr.a); + pred_chroma_8x8(chroma_mode, &mut w, nbr.b, nbr.a); } let mut chroma_dc = [[0i32; 4]; 2]; if cbp_chroma != 0 { for (comp, c) in chroma_dc.iter_mut().enumerate() { - residual_block(r, &mut scan, 4, -1)?; + r.residual_block(&mut scan, BlockCat::ChromaDc, None, None)?; c.copy_from_slice(&scan[..4]); let (ls_c, qp_c) = if comp == 0 { (&ls[1], qp_cb) @@ -846,8 +812,8 @@ fn decode_macroblock( for blk in 0..4 { let (cx, cy) = (cx0 + (blk & 1), cy0 + (blk >> 1)); let tc = if cbp_chroma == 2 { - let nc = chroma_nc(pic, &nbr, comp, cx, cy); - residual_block(r, &mut scan, 15, nc)? + let (na, nb) = chroma_nz_pair(pic, &nbr, comp, cx, cy); + r.residual_block(&mut scan, BlockCat::ChromaAc, na, nb)? } else { 0 }; diff --git a/crates/ec-h264/src/entropy.rs b/crates/ec-h264/src/entropy.rs new file mode 100644 index 00000000..c2cc2bb8 --- /dev/null +++ b/crates/ec-h264/src/entropy.rs @@ -0,0 +1,186 @@ +//! 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::tables::CBP_INTRA_420; + +/// 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, `ctxBlockCat` 3. + ChromaDc, + /// ChromaACLevel, `ctxBlockCat` 4. + ChromaAc, +} + +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, + } + } +} + +/// The slice-data syntax reader. +pub(crate) enum Entropy<'a> { + Cavlc(BitCursor<'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)) + } + + /// `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) + } + } + } + + /// `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(), + } + } + + /// 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)) + } + } + } + } + + /// `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) + } + } + } + + /// `coded_block_pattern` of an Intra_4x4 macroblock, as + /// `(CodedBlockPatternLuma, CodedBlockPatternChroma)`. + pub(crate) fn coded_block_pattern_intra(&mut self) -> Result<(u8, u8)> { + match self { + Entropy::Cavlc(r) => { + // me(v), clause 9.1.2 Table 9-4. + let code = r.read_ue()? as usize; + let cbp = *CBP_INTRA_420 + .get(code) + .ok_or_else(|| Error::corrupt("coded_block_pattern codeNum > 47"))?; + Ok((cbp & 15, cbp >> 4)) + } + } + } + + /// `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()?, + }; + if !(-26..=25).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 { + match self { + Entropy::Cavlc(r) => { + let nc = if 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, + } + }; + crate::cavlc::residual_block(r, coeff, cat.max_num_coeff(), nc) + } + } + } + + /// The 384 raw bytes of an I_PCM macroblock (256 luma, 2x64 chroma), after + /// alignment. A CABAC reader restarts its arithmetic engine afterwards + /// (clause 9.3.1.2). + pub(crate) fn pcm_block(&mut self) -> Result<&'a [u8]> { + match self { + Entropy::Cavlc(r) => { + r.align_to_byte(); + r.read_bytes(384) + } + } + } + + /// 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()), + } + } +} diff --git a/crates/ec-h264/src/lib.rs b/crates/ec-h264/src/lib.rs index 17812c94..079fe39b 100644 --- a/crates/ec-h264/src/lib.rs +++ b/crates/ec-h264/src/lib.rs @@ -35,6 +35,7 @@ mod cavlc; mod codec; mod deblock; mod decoder; +mod entropy; mod pred; mod tables; mod transform;