diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 0ddb1234..a0071f8e 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -10429,6 +10429,20 @@ impl Neighbours { mode: usize, uv_mode: usize, grids: &[&[i32]; 3], + ) { + self.record_rect_mi_modes(at_mi, w, h, mode, uv_mode); + self.record_mi_rect(at_mi, w, h, grids); + } + + /// [`Self::record_rect_mi`]'s mode and transform-size half, without the + /// coefficient-context stamp. + fn record_rect_mi_modes( + &mut self, + at_mi: (usize, usize), + w: usize, + h: usize, + mode: usize, + uv_mode: usize, ) { let (r, c) = (at_mi.0 / (SUB / MI), at_mi.1 / (SUB / MI)); // lane-inter4 r2: a strip whose own footprint runs past the frame's @@ -10452,7 +10466,28 @@ impl Neighbours { } self.record_mode_mi(at_mi.0, at_mi.1, w / MI, h / MI, mode); self.record_uv_mode_mi(at_mi.0, at_mi.1, w / MI, h / MI, uv_mode); - self.record_mi_rect(at_mi, w, h, grids); + } + + /// lane-av1chromarect: [`Self::record_rect_mi`] for a block whose uv + /// plane block is SEVERAL transform units, each of which stamped its own + /// span through [`Self::record_mi_chroma`]. Writing one whole-block state + /// over them is the `override-slot-on-one-arm` class: libaom keeps the + /// per-unit above/left contexts distinct, and the next block's first + /// unit reads them. The luma plane has one unit here, so it is stamped + /// exactly as the whole-block record stamps it. + fn record_rect_mi_luma_only( + &mut self, + at_mi: (usize, usize), + w: usize, + h: usize, + mode: usize, + uv_mode: usize, + luma_grid: &[i32], + ) { + self.record_rect_mi_modes(at_mi, w, h, mode, uv_mode); + fill_span(&mut self.left_side_mi, at_mi.0, h / MI, h as u8); + fill_span(&mut self.above_side_mi, at_mi.1, w / MI, w as u8); + self.record_mi_luma_rect(at_mi, w, h, luma_grid); } /// The coefficient-context half of [`Self::record`], taking the block's @@ -10652,6 +10687,39 @@ impl Neighbours { mode: usize, uv_mode: usize, chroma_grids: [&[i32]; 2], + ) { + self.record_split_luma_rect_mi_modes(at_mi, w, h, mode, uv_mode); + let (mi_r, _) = at_mi; + let (w_mi, h_mi) = (w / MI, h / MI); + let round_up_even = |n: usize| n.div_ceil(2) * 2; + let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); + for (plane_idx, grid) in chroma_grids.into_iter().enumerate() { + let plane = plane_idx + 1; + let state = neighbour_state(grid); + for cell in 0..h_mi { + if cell < h_mi.min(bound_h.saturating_sub(mi_r)) { + self.left[mi_r + cell][plane] = state; + } + } + for cell in 0..w_mi { + if cell < w_mi.min(bound_w.saturating_sub(at_mi.1)) { + self.above[at_mi.1 + cell][plane] = state; + } + } + } + } + + /// [`Self::record_split_luma_rect_mi`]'s mode and transform-size half, + /// without the per-plane coefficient stamp -- see + /// [`Self::record_rect_mi_luma_only`] for why a multi-unit chroma block + /// must not take the whole-block stamp. + fn record_split_luma_rect_mi_modes( + &mut self, + at_mi: (usize, usize), + w: usize, + h: usize, + mode: usize, + uv_mode: usize, ) { let (mi_r, mi_c) = at_mi; let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); @@ -10677,22 +10745,6 @@ impl Neighbours { self.record_uv_mode_mi(mi_r, mi_c, w_mi, h_mi, uv_mode); fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8); fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8); - let round_up_even = |n: usize| n.div_ceil(2) * 2; - let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); - for (plane_idx, grid) in chroma_grids.into_iter().enumerate() { - let plane = plane_idx + 1; - let state = neighbour_state(grid); - for cell in 0..h_mi { - if cell < h_mi.min(bound_h.saturating_sub(mi_r)) { - self.left[mi_r + cell][plane] = state; - } - } - for cell in 0..w_mi { - if cell < w_mi.min(bound_w.saturating_sub(mi_c)) { - self.above[mi_c + cell][plane] = state; - } - } - } } } @@ -16982,6 +17034,8 @@ fn decode_intrabc_owned_rect( u_grid = Grid::Zero(cside * cside); v_grid = Grid::Zero(cside * cside); } else if let Some(ls) = leaves.as_ref() { + let mut leaf_types: Vec<(usize, usize, usize, usize, TxType)> = + Vec::with_capacity(ls.len()); for (idx, &(row, col, tw, th)) in ls.iter().enumerate() { let tu_mi = (mi_r + row, mi_c + col); let (tu_px, tu_py) = (px + col * MI, py + row * MI); @@ -17024,6 +17078,7 @@ fn decode_intrabc_owned_rect( fctx, )? }; + leaf_types.push((row, col, tw, th, tt)); if idx == 0 { luma_tx_type = tt; } @@ -17039,43 +17094,66 @@ fn decode_intrabc_owned_rect( } } luma_grid = Grid::Zero(side * side); - let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); - let (ug, _) = read_inter_plane_rect( - dec, - cdfs, - rect_inter_chroma_set(cw, ch)?, - (cw, ch), - cside, - 1, - around[1], - 0, - u, - cpx, - cpy, - Pred::Inline(&pred_u, cside), - Some(luma_tx_type), - None, - fctx, - )?; - let (vg, _) = read_inter_plane_rect( - dec, - cdfs, - rect_inter_chroma_set(cw, ch)?, - (cw, ch), - cside, - 2, - around[2], - 0, - v, - cpx, - cpy, - Pred::Inline(&pred_v, cside), - Some(luma_tx_type), - None, - fctx, - )?; - u_grid = ug; - v_grid = vg; + if cw.min(32) != cw || ch.min(32) != ch { + // lane-av1chromarect: more than one chroma transform unit (the + // 4:4:4 32x64/64x32 plane block). See the helper. + let (ug, vg) = read_intrabc_rect_chroma_split( + dec, + cdfs, + neighbours, + (mi_r, mi_c), + (cpx, cpy), + (cw, ch), + cside, + &pred_u, + &pred_v, + &leaf_types, + luma_tx_type, + u, + v, + fctx, + )?; + u_grid = ug; + v_grid = vg; + } else { + let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); + let (ug, _) = read_inter_plane_rect( + dec, + cdfs, + rect_inter_chroma_set(cw, ch)?, + (cw, ch), + cside, + 1, + around[1], + 0, + u, + cpx, + cpy, + Pred::Inline(&pred_u, cside), + Some(luma_tx_type), + None, + fctx, + )?; + let (vg, _) = read_inter_plane_rect( + dec, + cdfs, + rect_inter_chroma_set(cw, ch)?, + (cw, ch), + cside, + 2, + around[2], + 0, + v, + cpx, + cpy, + Pred::Inline(&pred_v, cside), + Some(luma_tx_type), + None, + fctx, + )?; + u_grid = ug; + v_grid = vg; + } } else { let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); let (g, tt) = read_inter_plane_rect( @@ -17097,53 +17175,93 @@ fn decode_intrabc_owned_rect( )?; luma_tx_type = tt; luma_grid = g; - let (ug, _) = read_inter_plane_rect( - dec, - cdfs, - rect_inter_chroma_set(cw, ch)?, - (cw, ch), - cside, - 1, - around[1], - 0, - u, - cpx, - cpy, - Pred::Inline(&pred_u, cside), - Some(luma_tx_type), - None, - fctx, - )?; - let (vg, _) = read_inter_plane_rect( - dec, - cdfs, - rect_inter_chroma_set(cw, ch)?, - (cw, ch), - cside, - 2, - around[2], - 0, - v, - cpx, - cpy, - Pred::Inline(&pred_v, cside), - Some(luma_tx_type), - None, - fctx, - )?; - u_grid = ug; - v_grid = vg; + if cw.min(32) != cw || ch.min(32) != ch { + // lane-av1chromarect: more than one chroma transform unit (the + // 4:4:4 32x64/64x32 plane block). See the helper. + let (ug, vg) = read_intrabc_rect_chroma_split( + dec, + cdfs, + neighbours, + (mi_r, mi_c), + (cpx, cpy), + (cw, ch), + cside, + &pred_u, + &pred_v, + &[], + luma_tx_type, + u, + v, + fctx, + )?; + u_grid = ug; + v_grid = vg; + } else { + let (ug, _) = read_inter_plane_rect( + dec, + cdfs, + rect_inter_chroma_set(cw, ch)?, + (cw, ch), + cside, + 1, + around[1], + 0, + u, + cpx, + cpy, + Pred::Inline(&pred_u, cside), + Some(luma_tx_type), + None, + fctx, + )?; + let (vg, _) = read_inter_plane_rect( + dec, + cdfs, + rect_inter_chroma_set(cw, ch)?, + (cw, ch), + cside, + 2, + around[2], + 0, + v, + cpx, + cpy, + Pred::Inline(&pred_v, cside), + Some(luma_tx_type), + None, + fctx, + )?; + u_grid = ug; + v_grid = vg; + } } drop(restore); + // lane-av1chromarect: when the chroma plane block was walked as SEVERAL + // transform units, each unit stamped its own span through + // `record_mi_chroma` and the whole-block stamp below would overwrite them + // with one state (class `override-slot-on-one-arm`). libaom keeps the + // per-unit above/left contexts distinct; the next block's first unit + // reads them. + // `!skip` matters: a SKIPPED strip reads no chroma unit at all (its whole + // chroma plane block is one `push_mc_rect` of `ZERO_RESIDUAL`), so no + // per-unit `record_mi_chroma` ran and the whole-block chroma stamp below + // is still the only one that happens. + let chroma_split_units = !skip && (cw.min(32) != cw || ch.min(32) != ch); if leaves.is_some() { - neighbours.record_split_luma_rect_mi( - (mi_r, mi_c), - bw, - bh, - DC_PRED, - DC_PRED, - [&u_grid, &v_grid], - ); + if chroma_split_units { + neighbours.record_split_luma_rect_mi_modes((mi_r, mi_c), bw, bh, DC_PRED, DC_PRED); + } else { + neighbours.record_split_luma_rect_mi( + (mi_r, mi_c), + bw, + bh, + DC_PRED, + DC_PRED, + [&u_grid, &v_grid], + ); + } + } else if chroma_split_units { + neighbours.record_rect_mi_luma_only((mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, &luma_grid); } else { neighbours.record_rect_mi( (mi_r, mi_c), @@ -17179,6 +17297,111 @@ fn decode_intrabc_owned_rect( Ok(()) } +/// The chroma plane block of a 128-root intra-BC HORZ/VERT strip when that +/// plane block needs MORE THAN ONE transform unit -- the 4:4:4 cell, and the +/// 4:2:0 cell of a strip with a 128-px axis. +/// +/// libaom never forms a 64-point CHROMA transform. `get_vartx_max_txsize` +/// (blockd.h:1447) returns `av1_get_adjusted_tx_size(max_txsize_rect_lookup +/// [plane_bsize])` and `av1_get_adjusted_tx_size` maps `TX_32X64`/`TX_64X32` +/// to `TX_32X32`; `decode_token_recon_block` (decodeframe.c:994-1004) then +/// steps the plane block by that size's mi units, and +/// `decode_reconstruct_tx`'s `tx_size == plane_tx_size || plane` arm never +/// recurses for chroma -- the same statement from the other side. So a 4:4:4 +/// 32x64 chroma plane block is TWO stacked `TX_32X32` units, each with its +/// own eob, its own scan and its own coefficient context, NOT one 32x64 unit +/// whose coded corner happens to be 32x32: `av1_get_max_eob`'s 64-axis +/// truncation is a LUMA rule, and a chroma plane block is never 64 points +/// long to begin with. +/// +/// That is exactly the tiling [`read_inter_rect_chroma`] implements +/// (`(write_chroma_w.min(32), write_chroma_h.min(32))` and the `nx * ny` +/// walk); this is the same walk for a caller that keeps its own grid +/// assembly and neighbour recording. The single-unit case is deliberately +/// NOT routed through here, so at 4:2:0 and at every 4:4:4 shape below 64 +/// the caller keeps its own one-unit read verbatim. +#[allow(clippy::too_many_arguments)] +fn read_intrabc_rect_chroma_split( + dec: &mut SymbolDecoder, + cdfs: &mut Cdfs, + neighbours: &mut Neighbours, + (mi_r, mi_c): (usize, usize), + (cpx, cpy): (usize, usize), + (cw, ch): (usize, usize), + cside: usize, + pred_u: &[u16], + pred_v: &[u16], + leaf_tx_types: &[(usize, usize, usize, usize, TxType)], + luma_tx_type: TxType, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + fctx: &crate::decode::FrameCtx, +) -> Result<(Grid, Grid)> { + let (uw, uh) = (cw.min(32), ch.min(32)); + let (nx, ny) = (cw / uw, ch / uh); + let set = rect_inter_chroma_set(uw, uh)?; + let assembled_h = ch.max(cside); + let mut assembled: [Vec; 2] = [ + vec![0i32; cside * assembled_h], + vec![0i32; cside * assembled_h], + ]; + for plane_idx in 1..=2 { + let (src, buf) = if plane_idx == 1 { + (pred_u, &mut *u) + } else { + (pred_v, &mut *v) + }; + for cu_row in 0..ny { + for cu_col in 0..nx { + let (span_x, span_y) = (uw << ss_x(fctx), uh << ss_y(fctx)); + let cu_mi = (mi_r + cu_row * (span_y / MI), mi_c + cu_col * (span_x / MI)); + let cu_around = neighbours.around_mi_rect(cu_mi, span_x, span_y); + let (cu_x, cu_y) = (cpx + cu_col * uw, cpy + cu_row * uh); + let cu_pred = Pred::Inline(src, cside).offset(cu_row * uh * cside + cu_col * uw); + // libaom's `av1_get_tx_type` (blockd.h:1287) reads + // `xd->tx_type_map` at the chroma unit's OWN position scaled + // back to luma, so a unit sitting over a luma leaf that coded + // nothing takes that leaf's stamp, not the block-level one. + let rel_mi = (cu_mi.0 - mi_r, cu_mi.1 - mi_c); + let inherit_tx_type = + covering_leaf_tx_type(leaf_tx_types, rel_mi).or(Some(luma_tx_type)); + let cu_grid = read_inter_plane_rect( + dec, + cdfs, + set, + (uw, uh), + cside, + plane_idx, + cu_around[plane_idx], + 0, + buf, + cu_x, + cu_y, + cu_pred, + inherit_tx_type, + // `get_txb_ctx`: plus 10 when the plane block is bigger + // than the transform -- the same offset-3 convention + // `read_inter_rect_chroma` passes on this arm. + Some(3), + fctx, + )? + .0; + // Immediately, so the next unit of this block reads this one's + // coefficient context (libaom updates the chroma above/left + // contexts per unit). + neighbours.record_mi_chroma(cu_mi, span_x, span_y, plane_idx, &cu_grid); + let dst = &mut assembled[plane_idx - 1]; + for rr in 0..uh { + let start = (cu_row * uh + rr) * cside + cu_col * uw; + dst[start..start + uw].copy_from_slice(&cu_grid[rr * uw..][..uw]); + } + } + } + } + let [ug, vg] = assembled; + Ok((Grid::Own(ug), Grid::Own(vg))) +} + fn predict_intrabc_window( plane: &PlaneBuf<'_>, px: usize, diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index e11e7b27..fec0bd51 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -48468,23 +48468,50 @@ pub(crate) mod tests { } /// lane-av1chromarect: the 128-root intra-BC HORZ/VERT strip's chroma - /// plane block, on the 4:4:4 LOSSY witness that reaches - /// `decode_intrabc_owned_rect`. + /// plane block, on the 4:4:4 LOSSY witness `fixtures/r512.obu` that + /// reaches `decode_intrabc_owned_rect` five times. /// - /// The extent is the ss-derived one (`bw >> ss_x` by `bh >> ss_y`, + /// The extent is the ss-derived one (`bw >> ss_x` by `bh >> ss_y`; /// `av1_get_max_uv_txsize` = `ss_size_lookup[bsize]`, blockd.h:1372), and - /// the witness is pinned at the size that shape forces -- a 4:4:4 32x64 - /// luma block's chroma plane block is 32x64. **This gate asserts the - /// refusal, on purpose.** It exists so the day the missing arm lands the - /// gate goes RED and the assertion has to be rewritten as the - /// pixel-exactness check -- a witness with no gate is a pin, and a pin - /// with no governing refusal is a defect nobody is looking at. - #[test] - fn a_444_intrabc_owned_rect_strip_refuses_the_missing_rect_chroma_coefficient_set() { + /// the plane block is walked by `get_vartx_max_txsize` steps -- a 4:4:4 + /// 32x64 luma block's chroma plane block is 32x64, which libaom codes as + /// TWO `TX_32X32` units, not as one 32x64 transform. + /// + /// This gate WAS the refusal assertion "a rectangular inter chroma + /// transform unit whose shape has no coefficient table set here", put + /// there one commit earlier so that the day the arm landed it would go + /// RED and force this rewrite. The refusal string is GONE from the + /// witness's decode because nothing reaches it any more, not because it + /// was deleted: `rect_inter_chroma_set` still refuses (32, 64) and the + /// string is still in the source, it is just not on this path any more. + /// + /// The assertion is a per-(frame, plane) EXACT PREFIX against the + /// instrumented aomdec, the same shape as the sibling + /// `a_444_intrabc_rect_chroma_plane_block_is_the_block_footprint`. The + /// floors are MEASURED, and each one only grows: + /// + /// * luma is byte-exact on ALL THREE frames (786432/786432 samples). + /// Before the fix frame 0 luma was 166229 samples wrong from 65728 -- + /// the halving's chroma misread desynced the tile. + /// * frame 2 is byte-exact on all three planes. Before the fix it was + /// 9216 U + 8192 V wrong. + /// * frames 0 and 1 carry an OPEN chroma fork at a DIFFERENT site (see + /// lanes/av1chromarect.report.md); the floors below are the exact + /// prefixes, not a claim that the tail is right. + #[test] + fn a_444_intrabc_owned_rect_strip_sizes_its_chroma_plane_block_and_decodes() { const NAME: &str = - "a_444_intrabc_owned_rect_strip_refuses_the_missing_rect_chroma_coefficient_set"; + "a_444_intrabc_owned_rect_strip_sizes_its_chroma_plane_block_and_decodes"; const FILE: &str = "r512.obu"; const LEN: usize = 6948; + /// 512x512 8-bit 4:4:4, one plane. + const PLANE: usize = 512 * 512; + /// First differing sample per (frame, plane), decode order. + const EXACT_PREFIX: [[usize; 3]; 3] = [ + [PLANE, 229728, 229728], + [PLANE, 65704, 65704], + [PLANE, PLANE, PLANE], + ]; let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(format!("fixtures/{FILE}")); let stream = std::fs::read(&path) @@ -48494,28 +48521,84 @@ pub(crate) mod tests { let _guard = lock_gate_counters(); reset_ibc_owned_rect_chroma_footprint_444_hits(); - let err = decode_stream(&stream).err().unwrap_or_else(|| { - panic!( - "{NAME}: the witness DECODED -- the extent is not the ss-derived one any \ - more, so this gate is no longer measuring what it names" - ) - }); - let msg = err.to_string(); + // The refusal this gate used to assert must NOT be what happens now. + let dir = std::env::temp_dir().join(format!("ec-av1-{NAME}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).expect("scratch dir"); + let obu = dir.join("in.obu"); + std::fs::write(&obu, &stream).expect("writing the stream"); + let aom_prefix = dir.join("aom"); + let out = Command::new(aomdec_path()) + .args(["--codec=av1", "-o"]) + .arg(dir.join("out.y4m")) + .arg(&obu) + .env("EC_AV1_FINAL_DUMP", &aom_prefix) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("aomdec failed to run"); assert!( - msg.contains( - "a rectangular inter chroma transform unit whose shape has no coefficient table \ - set here" - ), - "{NAME}: expected the missing-rect-chroma-set refusal, got: {msg}" + out.status.success(), + "{NAME}: the oracle aomdec refused the stream: {}", + String::from_utf8_lossy(&out.stderr) ); + let ours_prefix = dir.join("ours"); + set_final_dump_prefix(Some(ours_prefix.display().to_string())); + let decoded = decode_stream(&stream); + set_final_dump_prefix(None); + if let Err(e) = decoded { + panic!( + "{NAME}: this decoder refused the witness: {e} -- the chroma plane block is \ + sized ss-wide and walked in `get_vartx_max_txsize` units, so the 4:4:4 32x64 \ + plane block is two TX_32X32 units, NOT the rect unit the refusal named" + ); + } let hits = ibc_owned_rect_chroma_footprint_444_hits(); - // Exactly 1, not 5: the decode stops AT the first 4:4:4 strip, so this - // counts the strips that reached the corrected extent before the - // refusal ended the stream. The pixel-exactness gate that replaces - // this one sees all five. assert_eq!( - hits, 1, - "{NAME}: {hits} 128-root intra-BC strip(s) sized at 4:4:4 before the refusal, not 1" + hits, 5, + "{NAME}: {hits} 128-root intra-BC strip(s) sized at 4:4:4, not 5 -- the witness \ + stopped exercising the arm" ); + assert!( + !std::fs::read(format!("{}.f0", ours_prefix.display())) + .unwrap_or_default() + .is_empty(), + "{NAME}: our decoder wrote no frame" + ); + for f in 0..3 { + let aom_dump = std::fs::read(format!("{}.f{f}", aom_prefix.display())) + .unwrap_or_else(|e| panic!("{NAME}: oracle dump f{f} missing: {e}")); + let our_dump = std::fs::read(format!("{}.f{f}", ours_prefix.display())) + .unwrap_or_else(|e| panic!("{NAME}: our dump f{f} missing: {e}")); + assert_eq!( + our_dump.len(), + 3 * PLANE, + "{NAME}: frame {f} is not 8-bit 4:4:4 512x512" + ); + assert_eq!( + our_dump.len(), + aom_dump.len(), + "{NAME}: frame {f} length differs" + ); + for (plane, name) in [(0usize, "Y"), (1, "U"), (2, "V")] { + let at = plane * PLANE; + let floor = EXACT_PREFIX[f][plane]; + if our_dump[at..at + floor] == aom_dump[at..at + floor] { + continue; + } + let first = (0..floor) + .find(|&s| our_dump[at + s] != aom_dump[at + s]) + .unwrap_or(floor); + panic!( + "{NAME}: frame {f} plane {name} diverges from the oracle at sample {first} \ + of {PLANE} (x={}, y={}) -- the exact prefix this gate pins is {floor} and \ + it only grows", + first % 512, + first / 512 + ); + } + } + let _ = std::fs::remove_dir_all(&dir); } } diff --git a/lanes/av1chromarect.report.md b/lanes/av1chromarect.report.md new file mode 100644 index 00000000..6543ea61 --- /dev/null +++ b/lanes/av1chromarect.report.md @@ -0,0 +1,387 @@ +# lane-av1chromarect — 4:4:4 chroma extent + multi-unit walk at the 128-root intra-BC strip + +Worktree `~/.cache/wt/av1chromarect`, branch `lane-av1chromarect`, base +`edffad9e` ("Merge lane-chromahalvings-r5: EC_HALVSWEEP rungs"). All edits are +absolute-pathed inside this worktree; `git -C ~/Documents/Code/Rust/edith_codecs +status --porcelain` printed nothing after every edit batch. + +Target: `decode_intrabc_owned_rect` (`crates/ec-av1/src/decode.rs:16897`) — the +128-root intra-BC HORZ/VERT strip path (`BLOCK_128X64` / `BLOCK_64X128` and the +`decode_block_rect64` strips under them). + +## 0. Provenance of the witness + +`crates/ec-av1/fixtures/r512.obu`, 6948 B, git blob +`bff992e98d01fed6ec73ed35ddd73f9fa482d19f`. It is on **`lane-chromahalvings-r3`** +and on no other ref of that lane (`lane-chromahalvings` and +`lane-chromahalvings-r5` both lack it, and so does `main`): + + $ for r in lane-chromahalvings-r3 lane-chromahalvings lane-chromahalvings-r5 main; do + printf "%s " "$r"; git cat-file -e "$r:crates/ec-av1/fixtures/r512.obu" 2>/dev/null \ + && git rev-parse "$r:crates/ec-av1/fixtures/r512.obu" || echo MISSING; done + lane-chromahalvings-r3 bff992e98d01fed6ec73ed35ddd73f9fa482d19f + lane-chromahalvings MISSING + lane-chromahalvings-r5 MISSING + main MISSING + $ git hash-object crates/ec-av1/fixtures/r512.obu + bff992e98d01fed6ec73ed35ddd73f9fa482d19f + +Recipe (from `lanes/av1chromahalvings.report.md` r4, re-stated so the pin is +reproducible): testsrc2 512x512 yuv444p 8-bit, `--sb-size=128 --enable-intrabc=1 +--enable-palette=1 --cq-level=50 --enable-rect-partitions=1 +--enable-1to4-partitions=1 --min-partition-size=4 --max-partition-size=128 +--tune-content=screen --profile=1 --obu`, 3 frames. + +## 1. The defect, and the red-before MEASURED HERE + +`decode_intrabc_owned_rect` sized its chroma plane block `(bw / 2, bh / 2)` and +passed `px / 2, py / 2` at eight sites as the chroma MV origins. libaom does not +halve there: `av1_get_max_uv_txsize` (blockd.h:1372) is +`av1_get_adjusted_tx_size(max_txsize_rect_lookup[get_plane_block_size(bsize, ss_x, +ss_y)])` and `get_plane_block_size` is `ss_size_lookup[bsize]`, so at ss (0,0) +the chroma plane block **is** the luma footprint. At 4:2:0 (`ss_x == ss_y == 1`) +`>> 1` and `/ 2` are the same arithmetic, which is why the bug was invisible +until a 4:4:4 witness existed. + +Red-before, on the **unpatched** line in this worktree, per-plane sample counts +against the instrumented aomdec (`aomdec --codec=av1` with +`EC_AV1_FINAL_DUMP`, ours with `set_final_dump_prefix`; 512x512 8-bit 4:4:4, +786432 B per frame, three frames, both sides in decode order): + + f0 Y 166229 wrong from 65728 | U 155383 from 32848 | V 153498 from 32848 total 475110 + f1 Y 0 (byte-exact) | U 74708 from 49282 | V 73501 from 49280 total 148209 + f2 Y 0 (byte-exact) | U 9216 from 114880 | V 8192 from 229760 total 17408 + +Luma byte-exact on f1 and f2 while chroma is wrong is the signature that makes +this a chroma-plane sizing defect and not a block-geometry one. The numbers +reproduce the previous lane's r4 measurement exactly. + +## 2. What the blocker's own refusal was actually asking for + +The previous lane's r4/r5 finding was that the ss-derived extent REFUSES +`fixtures/r512.obu` with + + unsupported: AV1 tile (a rectangular inter chroma transform unit whose shape + has no coefficient table set here) + +and concluded the fix needs a new `TxbSet` — `ChromaRect32x64` / `ChromaRect64x32` +— with its own CDF set, `rect_scan` and rect `base_ctx_rect` / `br_ctx_rect`. +**That conclusion is wrong, and the wrongness is the finding.** + +The refusal names the shape the crate HANDED `rect_inter_chroma_set`, not the +shape libaom codes. With the ss extent a 4:4:4 32x64 luma block's chroma plane +block is 32x64, and this crate read it as one rect transform. libaom never forms +a 64-point CHROMA transform: + +* `get_vartx_max_txsize` (blockd.h:1447) — + `return av1_get_adjusted_tx_size(max_txsize_rect_lookup[bsize]);` for `plane != 0`. +* `av1_get_adjusted_tx_size` (blockd.h, just above `av1_get_max_uv_txsize`) maps + `TX_64X32 → TX_32X32` and `TX_32X64 → TX_32X32`, and returns everything else + unchanged. +* `max_txsize_rect_lookup[BLOCK_SIZES_ALL]` (common_data.h:126) has + `BLOCK_32X64 → TX_32X64`, `BLOCK_64X32 → TX_64X32`. +* `decode_token_recon_block` (decodeframe.c:994-1004) steps the plane block by + `tx_size_*_unit[max_tx_size]`, i.e. 4 mi per axis, and + `decode_reconstruct_tx`'s `if (tx_size == plane_tx_size || plane)` arm never + recurses for chroma — the same statement from the other side. + +So a 4:4:4 32x64 chroma plane block is **two stacked `TX_32X32` units**, each +with its own eob, its own scan and its own coefficient context. `TX_32X32` is +already in the crate: `rect_inter_chroma_set(32, 32) → TxbSet::Chroma32` +(decode.rs:28919, the arm `lane-av1-444` added). What was missing was not a +coefficient set but the **walk** that hands the crate `2 x 32x32` instead of +`1 x 32x64`. + +The 64-axis truncation `read_inter_plane_rect` does internally +(`let (cw, ch) = (w.min(32), h.min(32));` + `extend_corner`, decode.rs:29532) is +libaom's `av1_get_max_eob` rule, and that rule is a LUMA rule: a chroma plane +block is never 64 points long in the first place. Routing a 4:4:4 32x64 chroma +plane block through one 32x64 unit would have produced a new `TxbSet` that no +stream can ever exercise — a coefficient table with no bitstream, which is the +worst kind of green. + +### What shipped instead + +* `read_intrabc_rect_chroma_split` (decode.rs, after + `decode_intrabc_owned_rect`): the same `(cw.min(32), ch.min(32))` / `nx * ny` + tiling `read_inter_rect_chroma` already implements for the other rect paths, + for a caller that keeps its own grid assembly and neighbour recording. It is + entered ONLY when `cw.min(32) != cw || ch.min(32) != ch`, so at 4:2:0 and at + every 4:4:4 shape below 64 the caller's own one-unit read is untouched + byte-for-byte. +* Per unit: its own `around_mi_rect(cu_mi, span_x, span_y)` gather, its own + `(cpx + cu_col*uw, cpy + cu_row*uh)` origin, its own prediction slice, its own + `covering_leaf_tx_type(...)` inheritance (`av1_get_tx_type`, blockd.h:1287, + reads `xd->tx_type_map` at the unit's own position), the `get_txb_ctx` plus-10 + offset as `Some(3)`, and `record_mi_chroma` immediately so the next unit + reads this one's context. +* `record_split_luma_rect_mi_modes` / `record_rect_mi_luma_only` (new, extracted + from `record_split_luma_rect_mi` / `record_rect_mi`): when the chroma plane + block was walked as several units, the whole-block coefficient stamp would + overwrite the per-unit spans with one state (the `override-slot-on-one-arm` + class, the same reason the lossless arm sets `mu_chroma = true`). The flag is + `!skip && …`: a SKIPPED strip reads no chroma unit at all (its whole plane + block is one `push_mc_rect` of `ZERO_RESIDUAL`), so no per-unit stamp ran and + the whole-block stamp is still the only one that happens. + +### The two pieces the charter asked for, and why they are not needed + +* **CDF set** — `Chroma32` (`cdf_state.rs`), the exact + `get_txsize_entropy_ctx(TX_32X32)` tables. Nothing new. +* **`rect_scan`** — a `TX_32X32` unit is square, so + `read_inter_plane_rect` takes its `(cw, ch) == (32, 32)` branch and reads + `default_scan(TX32)` (decode.rs:29546); `rect_scan` is never called. Nothing + new. +* **rect `base_ctx_rect` / `br_ctx_rect`** — same reason: those are the rect + forms of the 2:1 and 4:1 nz-map offsets, and no 2:1 or 4:1 chroma unit exists + at 4:4:4 for this block shape. Nothing new. The call the new walk makes is + byte-identical to the one `read_inter_rect_chroma` already makes for the + 4:4:4 four-unit arms proven by `lane-av1chrtx`. + +## 3. Result on the witness, and the OPEN fork that remains + +Same oracle, same comparison, after the fix: + + f0 Y 0 (byte-exact) | U 7488 from 229728 | V 8128 from 229728 total 15616 + f1 Y 0 (byte-exact) | U 22571 from 65704 | V 41782 from 65704 total 64353 + f2 Y 0 (byte-exact) | U 0 | V 0 0 + +Wrong samples over the whole stream: **640727 → 79969** (−87.5 %). Luma is +byte-exact on all three frames (786432/786432 samples; it was 166229 wrong on +f0). Frame 2 is byte-exact on all three planes (it was 9216 U + 8192 V wrong). + +Reach proof for the site, `EC_HALVSWEEP=1` (the rung is main's, merge +`edffad9e`), five strips on this witness, `mi` in 4-px units: + + EC_HALV ibc_owned mi=(32,40) bw=32 bh=64 skip=false + EC_HALV ibc_owned mi=(48,64) bw=32 bh=64 skip=true + EC_HALV ibc_owned mi=(64,108) bw=16 bh=64 skip=true + EC_HALV ibc_owned mi=(112,96) bw=32 bh=64 skip=true + EC_HALV ibc_owned mi=(112,104) bw=32 bh=64 skip=true + +(The previous lane reported 6 and named `mi=(72,32) 64x32 skip=false`. On this +base the count is 5 and that block is not reached. The witness blob is +byte-identical, so the difference is the tree, not the stream — the same +stale-verdict hazard the previous lane's r4 recorded, re-measured rather than +inherited.) + +### The residual is a DIFFERENT site, and it is NOT this lane's + +Both remaining regions are chroma-only, bounded, and luma-clean, so they are +reconstruction defects rather than entropy desyncs: + +* f0: 128 dirty 8x8 cells, bbox x=352..=504, y=448..=504 — the last 64 rows of + the frame. Strip #4's own footprint (x=384..=416) is CLEAN; strip #5's + (x=416..=448) is dirty, and so is everything to its right. +* f1: 362 dirty 8x8 cells (U) / 667 (V), bbox x=128..=368 / 128..=408, + y=128..=504, first dirty cell (168,128) PARTIAL (56 of 64 samples). + +Ruled out by measurement, not by argument: + +* **CfL.** `cfl_ac_q3_at` (decode.rs:20179) is the other `bw / 2, bh / 2` site + in this wave and is out of scope (another agent owns it). A temporary probe at + both its callers (`cfl_ac_q3`, `cfl_ac_q3_rect`) over the whole witness: + + $ EC_ZZCFL=1 cargo test -p ec-av1 --lib -- --nocapture | grep -c ZZCFL + 0 + + The witness codes NO CfL block, so `cfl_ac_q3_at` cannot be the residual. (The + probe was removed before the commit; it is not in the diff.) +* **The skip arm's record.** Forcing `chroma_split_units` true for skipped + strips as well changes neither number — the f0 band and the f1 region are + identical with and without it. +* **This lane's helper.** The per-unit prediction slice's column term + (`cu_col * uw`) is dead on this witness (`nx == 1`), so mutating it leaves the + gate green; the row term does bite (see §5, M5). + +NOT DONE, explicitly: the f0 and f1 chroma residue above. It is not attributable +to `decode_intrabc_owned_rect` from the evidence I have, and I did not identify +its site. It is stated as a measured open fork, with numbers, rather than +absorbed into a "wrong by exactly N" expectation. + +## 4. Gates + +### 4.1 The refusal gate went in FIRST, and it went RED + +Commit 1 (`58629693`) pins the witness, lands the ss-derived extent and the eight +MV origin pairs, and adds +`a_444_intrabc_owned_rect_strip_refuses_the_missing_rect_chroma_coefficient_set` +— which asserts, on purpose, that the witness is REFUSED with the exact string +`a rectangular inter chroma transform unit whose shape has no coefficient table +set here`, and that exactly one strip reached the corrected extent before the +decode stopped. It cites this lane in the test. Without it the witness would be +a pin with nothing watching it. + +Commit 2 replaces that test with +`a_444_intrabc_owned_rect_strip_sizes_its_chroma_plane_block_and_decodes`, the +pixel-exactness assertion the refusal gate was there to force. + +The refusal string is gone from the witness's decode because nothing reaches it, +not because it was deleted: `rect_inter_chroma_set` still refuses `(32, 64)` and +`(64, 32)`, the string is still in `decode.rs:28938` and in +`refusal_inventory.rs:55`, and the gate +`every_rect_transform_shape_the_census_lists_has_a_coefficient_table_and_scan` +(which names all three rect refusals and asserts they are still in the source) +still passes. + +### 4.2 Non-vacuity: the route counter, and three mutations + +`IBC_OWNED_RECT_CHROMA_FOOTPRINT_444_HITS` counts 128-root intra-BC strips whose +chroma plane block was sized at ss (0,0). At 4:2:0 the corrected and halving +forms are identical, so `>= 1` cannot be satisfied by a 4:2:0 stream, and the +arm is exclusive to this function. The gate asserts `hits == 5`. + +Mutation proofs against the gate, each reverted after: + +| # | mutation | result | +| --- | --- | --- | +| M1 | `bw >> ss_x` / `bh >> ss_y` and `px >> ss_x` / `py >> ss_y` back to `/ 2` | **RED** — `assertion left == right failed: 6 128-root intra-BC strip(s) sized at 4:4:4, not 5` (the counter bites: with the halving a sixth strip reaches the arm) | +| M3 | `(nx, ny) = (cw / uw, ch / uh)` → `(ch / uh, cw / uw)` | **RED** — `frame 0 plane Y diverges from the oracle at sample 65728 of 262144 (x=192, y=128)` | +| M5 | `let (cu_x, cu_y) = (cpx + cu_col * uw, cpy + cu_row * uh)` → `(cpx, cpy)` | **RED** — `frame 0 plane U diverges from the oracle at sample 65696 of 262144 (x=160, y=128)` | + +A fourth mutation (dropping the `cu_col * uw` term from the prediction slice) +left the gate GREEN — correctly, because `nx == 1` on this witness makes that +term dead here. Recorded because a green mutation is a fact about coverage, not +about the gate. + +The gate's own shape, per (frame, plane) exact prefix against the oracle, with +the floors it pins: `[[262144, 229728, 229728], [262144, 65704, 65704], +[262144, 262144, 262144]]` — the same honest "exact prefix that only grows" form +as the sibling `a_444_intrabc_rect_chroma_plane_block_is_the_block_footprint`. +It is a floor, not an exactness claim: §3 says exactly what it does not cover. + +## 5. Gate families re-quoted + +`cargo check -p ec-av1 --all-targets` clean (no errors, no warnings) before every +run, on a private `CARGO_TARGET_DIR`; a green gate alongside a build error would +have been a stale binary. + + $ EC_AV1_REQUIRE_AOMDEC=1 EC_AV1_REQUIRE_AOMENC=1 \ + cargo test -p ec-av1 --lib -- a_444 lossless_444 intrabc_rect 444_lossless -- --nocapture + test result: ok. 33 passed; 0 failed; 0 ignored; 0 measured; 730 filtered out + +That set contains the 4:4:4-lossless family (`a_lossless_444_*`, +`a_444_lossless_sb64_intrabc_rect_chroma_walks_4x4_units`, +`a_real_aomenc_lossless_444_key_frame_decodes_sample_exact`), the intrabc_rect +family (`a_444_intrabc_rect4_reads_its_own_chroma_plane_block`, +`a_lossless_444_intrabc_rect_leaf_walks_per_4x4_units`, +`a_lossless_444_intrabc_rect_replay_steps_by_the_units_own_mi_footprint`, +`a_skipped_lossless_intrabc_rect_strip_zeroes_its_entropy_bands`, +`a_coded_rect_intrabc_block_reconstructs_in_both_orientations`) and — the 4:2:0 +identity arm — `a_444_intrabc_rect_chroma_plane_block_is_the_block_footprint`, +which decodes the pinned 4:2:0 twin (`420_intrabc_rect4_witness.obu`) through +`decode_all_frames_vs_oracle`. + +The wider 121-test `rect intrabc ibc chroma 444 420 subsampl` family, tip vs +the base commit, is in §6. + +## 6. 121-test family: tip vs base + +One filtered run per tree, same filter string, same env, separate private +`CARGO_TARGET_DIR` each (a shared-shape target dir is exactly how the previous +lane got a stale verdict): + + $ EC_AV1_REQUIRE_AOMDEC=1 EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib \ + -- rect intrabc ibc chroma 444 420 subsampl -- --nocapture + +| tree | result | +| --- | --- | +| base `edffad9e` (`~/.cache/wt/av1chromarect-base`, detached) | `ok. 137 passed; 0 failed; 5 ignored; 0 measured; 620 filtered out; finished in 288.38s` | +| tip `lane-av1chromarect` | `ok. 138 passed; 0 failed; 5 ignored; 0 measured; 620 filtered out; finished in 295.84s` | + +The set difference of the two passing-test lists is exactly one line: + + 34a35 + > test stream::tests::a_444_intrabc_owned_rect_strip_sizes_its_chroma_plane_block_and_decodes + +The same 5 tests are `#[ignore]`d in both trees (two superblock-size globals, a +directional sweep, an intrabc probe needing the real-library manifest, and the +rectx recipe sweep). Nothing regressed, nothing was silently dropped. + +Inside that family, the 4:2:0 identity is carried by +`a_444_intrabc_rect_chroma_plane_block_is_the_block_footprint`, which decodes +the pinned 4:2:0 twin `420_intrabc_rect4_witness.obu` through +`decode_all_frames_vs_oracle` (every frame byte-exact vs aomdec) and +`a_444_intrabc_rect4_reads_its_own_chroma_plane_block`; the 4:4:4-lossless arm +by the `a_lossless_444_*` set; the intrabc_rect arm by +`a_lossless_444_intrabc_rect_leaf_walks_per_4x4_units`, +`a_lossless_444_intrabc_rect_replay_steps_by_the_units_own_mi_footprint`, +`a_skipped_lossless_intrabc_rect_strip_zeroes_its_entropy_bands`, +`a_444_lossless_sb64_intrabc_rect_chroma_walks_4x4_units` and +`a_coded_rect_intrabc_block_reconstructs_in_both_orientations`. + +## 7. Gap row — `decode_intra_sub8_leaf` has no `chroma_422` arm + +`crates/ec-av1/src/decode.rs:45606`, body ends at `46434`. It carries a +`chroma_444` branch set and **zero** `chroma_422` mentions inside the body +(`awk 'NR>=45606 && NR<=46400' | grep -c chroma_422` → `0`; the two +`chroma_422` mentions at 45406/45418 belong to the enclosing +`decode_intra_split`, not to this function). + +The 4:4:4 branches it owns, and what each falls back to at 4:2:2: + +| line | what the `chroma_444` arm does | what 4:2:2 gets instead | +| --- | --- | --- | +| 46095 | `let chroma_444 = ss_x == 0 && ss_y == 0` | `false` at (1,0) | +| 46098 | `unit_x, unit_y = (px, py)` | `(cpx, cpy)` — the 4:2:0 double halving; at (1,0) the width halves and the height does not | +| 46100-46105 | `reach_c` = the piece's own rect reach at 4:4:4 | `group_reach` (a 8x8 group reach) | +| 46107 | `band_r, band_c` from `lmi` | from `r, c` (the group) | +| 46113-46114 | `smooth_uv_neighbour` sampled at `lmi` | sampled at `gr, gc` | +| 46127 | `record_uv_mode_mi(lmi, w_mi, h_mi)` | `record_uv_mode_mi(gr, gc, 2, 2)` — a 2x2 group stamp | +| 46134 | `cfl_src_rect(px, py, bw, bh)` | not taken | +| 46146, 46210 | the non-square 4:4:4 unit shape arms | not taken | +| 46352 | chroma per-unit gather at `lmi` with a 4-px step | at `group_mi` with an 8-px step | + +The shape the missing arm would have to be: at 4:2:2 `ss_size_lookup` maps an +8x4 block to `BLOCK_INVALID` (common_data.c:38) and no `TX_4X8` chroma unit +exists, so a 4:2:2 sub-8 leaf's chroma is coded CHUNKED, exactly the +`invalid_ss_plane` case `read_inter_rect_chroma` already special-cases +(decode.rs:29328, `(4, 32)` at ss (1,0) → `(4, 8)` units). + +**Caller set — exactly two sites, and BOTH are already 4:2:2-aware:** + +1. `decode.rs:44736` — the `BLOCK_4X4` leaf of the sub-8x8 intra-in-inter split + arm. It passes + `has_chroma = chroma_444 || (cmi & 1 == 1 && chroma_422) || i == 3` + (libaom's `is_chroma_reference`, `av1_common_int.h:1459`: BOTH odd-column + pieces at 4:2:2). +2. `decode.rs:46622` — the `(bw, bh)` 8x8/4x8/8x4 piece. It passes + `has_chroma = piece_is_chroma_ref(i)`, and `piece_is_chroma_ref` + (decode.rs:46546) has an explicit `else if chroma_422 { !vert || ((gc + i) & 1) == 1 }` + branch. + +So the callers compute the 4:2:2 chroma-reference rule correctly and the leaf +body has no geometry arm to honour it. This is a latent gap, not a live bug: the +crate refuses 4:2:2 by name at the sequence header +(`"a chroma format of 4:2:2 (subsampling_x != subsampling_y): this decoder +decodes 4:2:0 and 4:4:4; 4:2:2 is not ported"`, `refusal_inventory.rs:119`, gate +`a_non_420_subsampled_sequence_header_is_refused_by_name`, stream.rs:2258), so +`chroma_422` is always `false` when this function runs. Not implemented here, by +charter. + +## 8. Scope boundaries, stated + +* `cfl_ac_q3_at` (decode.rs:20179, `let (cw, ch) = (bw / 2, bh / 2);`, no + `fctx`) — **untouched, another lane owns it.** §3 measures that the witness + codes no CfL, so it is not this witness's residual either. +* `suppress_internal_lf_edges` — **not re-fixed.** C1 (wave 3d, on this base) + already threads `fctx` into it and computes `((w_mi * MI) >> ss_x(fctx)).max(4)`. + The earlier "format-blind" claim is retracted upstream; there is nothing here + to change. +* `decode_intrabc_rect` (the non-`owned` sibling, decode.rs:13646) reads its + chroma with the same single-unit `rect_inter_chroma_set(cw, ch)`. It is + ss-correct (lane-ibc444c) and its pinned 4:4:4 witness is 8x16, one unit, so + the multi-unit case is latent there too. I did not widen the fix: this lane's + witness and its gate do not reach it, and a change there would be unmeasured. + Flagged for whoever takes the next 4:4:4 rect cell. + +## 9. What I could not do + +1. The f0/f1 chroma residue of §3 is not identified to a site. 79969 wrong + samples remain out of 2359296 (3.4 %); luma is exact everywhere. +2. The 128-root multi-unit *record* difference in §2 is derived from libaom's + per-unit above/left context update and validated by the gate going from + 640727 wrong samples to 79969 with luma exact; I did not diff our per-unit + above/left contexts against the oracle's rung-by-rung. +3. No heavy encodes were run: the witness was taken from the pin, and the gate + families are fixture-driven. No server fleet run was requested or consumed.