forge
Explore
Log in

Don't you dare go Hollow

A sub-8x8 rect leaf reads a tx_depth symbol -- TX_MODE_SELECT writes one for BLOCK_4X8/8X4 too

Browse files at this commit

88fd2ae1eaaf26bf02cd441f624661c273344b76Ahmet Kaan GümüşParent: d471e3e
  • fix
    • a sub-8x8 rect leaf reads a tx_depth symbol -- TX_MODE_SELECT writes one for BLOCK_4X8/8X4 too
Show full message
fix(av1): a sub-8x8 rect leaf reads a tx_depth symbol -- TX_MODE_SELECT writes one for BLOCK_4X8/8X4 too

`decode_leaf_rect8` never called `read_tx_size`, so with aomenc's default
`--enable-tx-size-search=1` (TX_MODE_SELECT) the decoder consumed one symbol
fewer than the encoder wrote and desynced the tile at the very first rect
leaf -- the whole frame came out near-flat from sample 0 (class
`symbol-consumption-gap`; the same defect the 16x16-level strip path already
carries a comment about).

Range ladder vs instrumented aomdec on the pinned seed-303 stream: ours
entered the first TU's `all_zero` with a range that had never taken the
`tx_depth` narrowing (38104 -> 46600 in the oracle, one unread symbol);
after the fix every element matches (45231 / 54240 / eob=30 / 62552 /
63488 / 59248 ...) and the frame is pixel-exact vs ffmpeg.

* `bsize_to_tx_size_cat(BLOCK_4X8) == 0` (`sub_tx_size_map[TX_4X8] == TX_4X4`
  is one step), so the symbol is the 2-symbol `tx_size_cat0` an 8x8 uses, NOT
  the 3-symbol `tx_size_cat2` the 32x16 strips read; context is
  `tx_size_context_rect(bw, bh)` (`get_tx_size_context`'s max_tx_wide/high).
* depth 1 splits the leaf into two 4x4 transform units along its long axis,
  each predicted from the previous unit's reconstruction -- ported (skipped
  leaves included, whose per-unit prediction also differs from one 4x8) and
  the tx size recorded as 4x4 for the next block's tx_size_context.
* new `rect8_split_tx_hits` counter; the gate now hard-asserts the split path
  fired, not just that rect leaves coded coefficients.
* `read_coeffs_rect` gained `read_coeffs`'s `EC_TRACE_COEFF` range ladder
  (all_zero/tx_type/eob/base/base_eob/br), in the oracle's own tag format.

Gate `a_real_aomenc_stream_with_a_sub8_rect_leaf_decodes_pixel_exact`:
RED (mismatch at sample 0) -> GREEN, all three arms.
crates/ec-av1/src/decode.rsmodified+148 -3
@@ -944,6 +944,16 @@ pub(crate) fn sub8_split_hits() -> usize {
944944 thread_local! {
945945 static TX4X8_CODED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
946946 static TX8X4_CODED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
947+ /// Sub-8x8 rect leaves whose `tx_depth` symbol resolved to 1 -- the leaf's
948+ /// two 4x4 transform units, predicted one after the other. A separate
949+ /// counter from [`TX_DEPTH_HITS`] (which every square path also bumps) so
950+ /// the gate can prove THIS path fired.
951+ static RECT8_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
952+}
953+
954+/// Current value of [`RECT8_SPLIT_TX_HITS`].
955+pub(crate) fn rect8_split_tx_hits() -> usize {
956+ RECT8_SPLIT_TX_HITS.with(|c| c.get())
947957 }
948958
949959 /// Current value of [`TX4X8_CODED_HITS`].
@@ -1713,8 +1723,16 @@ fn read_coeffs_rect(
17131723 sign_ctx: usize,
17141724 default_tx_type: TxType,
17151725 ) -> Result<(Vec<i32>, TxType)> {
1726+ let rect_trace = std::env::var_os("EC_TRACE_COEFF").is_some();
17161727 let mut grid = vec![0i32; w * h];
17171728 let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1;
1729+ if rect_trace {
1730+ eprintln!(
1731+ "EC_COEFF_STEP tag=all_zero plane=0 ctx={skip_ctx} all_zero={} rng={}",
1732+ all_zero as i32,
1733+ dec.debug_state().0
1734+ );
1735+ }
17181736 if all_zero {
17191737 return Ok((grid, TxType::DctDct));
17201738 }
@@ -1735,12 +1753,18 @@ fn read_coeffs_rect(
17351753 _ => TxType::from_symbol(t),
17361754 }
17371755 .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?;
1756+ if rect_trace {
1757+ eprintln!("EC_COEFF_STEP tag=tx_type plane=0 rng={}", dec.debug_state().0);
1758+ }
17381759 }
17391760 let class = TxClass::of(tx_type);
17401761 if class != TxClass::TwoD {
17411762 TX_CLASS1_HITS.with(|c| c.set(c.get() + 1));
17421763 }
17431764 let eob = read_eob(dec, coding, class);
1765+ if rect_trace {
1766+ eprintln!("EC_COEFF_STEP tag=eob eob={eob} rng={}", dec.debug_state().0);
1767+ }
17441768 let class_scan;
17451769 let scan: &[u16] = if class == TxClass::TwoD {
17461770 scan
@@ -1754,10 +1778,24 @@ fn read_coeffs_rect(
17541778 let (row, col) = (pos / w, pos % w);
17551779 let level = if scan_idx == eob - 1 {
17561780 let ctx = eob_coeff_ctx(scan_idx, w * h);
1757- dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1
1781+ let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1;
1782+ if rect_trace {
1783+ eprintln!(
1784+ "EC_COEFF_STEP tag=base_eob c={scan_idx} pos={pos} ctx={ctx} level={v} rng={}",
1785+ dec.debug_state().0
1786+ );
1787+ }
1788+ v
17581789 } else {
17591790 let ctx = base_ctx_rect(&levels, w, h, row, col, class);
1760- dec.symbol(&mut coding.base[ctx]) as i32
1791+ let v = dec.symbol(&mut coding.base[ctx]) as i32;
1792+ if rect_trace {
1793+ eprintln!(
1794+ "EC_COEFF_STEP tag=base c={scan_idx} pos={pos} ctx={ctx} level={v} rng={}",
1795+ dec.debug_state().0
1796+ );
1797+ }
1798+ v
17611799 };
17621800 let level = if level > NUM_BASE_LEVELS {
17631801 let ctx = br_ctx_rect(&levels, w, h, row, col, class);
@@ -1765,6 +1803,12 @@ fn read_coeffs_rect(
17651803 let mut sent = 0;
17661804 loop {
17671805 let k = dec.symbol(&mut coding.br[ctx]) as i32;
1806+ if rect_trace {
1807+ eprintln!(
1808+ "EC_COEFF_STEP tag=br c={scan_idx} pos={pos} ctx={ctx} k={k} rng={}",
1809+ dec.debug_state().0
1810+ );
1811+ }
17681812 level += k;
17691813 sent += BR_STEP;
17701814 if k < BR_STEP || sent >= COEFF_BASE_RANGE {
@@ -6058,6 +6102,7 @@ fn decode_leaf_rect8(
60586102 base_q_idx: u8,
60596103 enable_filter_intra: bool,
60606104 allow_intrabc: bool,
6105+ tx_select: bool,
60616106 reduced_tx_set: bool,
60626107 ) -> Result<(usize, usize)> {
60636108 let (r, c) = outer_at;
@@ -6099,6 +6144,26 @@ fn decode_leaf_rect8(
60996144 "filter intra on a HORZ/VERT strip (this decoder predicts square-only)",
61006145 ));
61016146 }
6147+ // `TxMode::Select`'s `tx_depth` (spec 5.11.16) exists at a 4x8/8x4
6148+ // leaf too, and is read for every intra block, skipped or not. Its
6149+ // category is libaom's `bsize_to_tx_size_cat(BLOCK_4X8) == 0` --
6150+ // `sub_tx_size_map[TX_4X8] == TX_4X4` is a single step, so it is the
6151+ // same 2-symbol `tx_size_cat0` an 8x8 uses, not the 3-symbol
6152+ // `tx_size_cat2` the 32x16 strips read -- with the rect context
6153+ // (`get_tx_size_context`'s `max_tx_wide`/`max_tx_high` = bw/bh).
6154+ // Missing this read consumed one symbol fewer than the encoder wrote
6155+ // and desynced the tile at the very first rect leaf (class
6156+ // `symbol-consumption-gap`).
6157+ let depth = if tx_select {
6158+ let ctx = tx_size_context_rect(neighbours, lmi, bw, bh);
6159+ dec.symbol(&mut cdfs.tx_size_cat0[ctx])
6160+ } else {
6161+ 0
6162+ };
6163+ if depth != 0 {
6164+ TX_DEPTH_HITS.with(|c| c.set(c.get() + 1));
6165+ RECT8_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1));
6166+ }
61026167 leaf_modes[i] = mode;
61036168 leaf_skips[i] = skip;
61046169 neighbours.record_mode_mi(lmi.0, lmi.1, w_mi, h_mi, mode);
@@ -6108,6 +6173,83 @@ fn decode_leaf_rect8(
61086173 }
61096174 let (px, py) = (lmi.1 * MI, lmi.0 * MI);
61106175 let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height);
6176+ // `sub_tx_size_map[TX_4X8] == TX_4X4`: depth 1 splits the leaf into
6177+ // two 4x4 transform units along its long axis, each predicted from
6178+ // the previous unit's reconstruction and reading its own `txb_skip`
6179+ // context -- `decode_leaf8`'s own depth-1 loop, two units instead of
6180+ // four. The per-TU path writes its own neighbour state, so the
6181+ // block-level write below is skipped for it.
6182+ let split = depth != 0;
6183+ if split {
6184+ let mut done = 0usize;
6185+ for tu in 0..2usize {
6186+ let tu_mi = if vert {
6187+ (lmi.0 + tu, lmi.1)
6188+ } else {
6189+ (lmi.0, lmi.1 + tu)
6190+ };
6191+ let (tu_px, tu_py) = (tu_mi.1 * MI, tu_mi.0 * MI);
6192+ let tu_around = neighbours.around_mi(tu_mi, 4)[0];
6193+ let tu_reach = Reach::of(4, tu_px, tu_py, y.width, y.height);
6194+ let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1);
6195+ if skip {
6196+ // A skipped leaf still predicts per transform unit: the
6197+ // second 4x4 takes its above/left edge from the first
6198+ // one's reconstruction, which is NOT what a single 4x8
6199+ // prediction of the same mode produces.
6200+ let zeros = vec![0i32; 16];
6201+ y.reconstruct(
6202+ tu_px, tu_py, 4, mode, angle_delta_y, tu_reach, &zeros, None, None,
6203+ smooth_neighbor,
6204+ );
6205+ neighbours.record_mi_luma(tu_mi, 4, &zeros);
6206+ continue;
6207+ }
6208+ let tu_grid = read_plane(
6209+ dec,
6210+ cdfs,
6211+ if reduced_tx_set {
6212+ TxbSet::Luma4
6213+ } else {
6214+ TxbSet::Luma4Set1
6215+ },
6216+ scan4,
6217+ 0,
6218+ tu_around,
6219+ mode,
6220+ mode,
6221+ angle_delta_y,
6222+ tu_reach,
6223+ y,
6224+ tu_px,
6225+ tu_py,
6226+ 4,
6227+ TX4,
6228+ base_q_idx,
6229+ None,
6230+ None,
6231+ Some(tu_skip_ctx),
6232+ smooth_neighbor,
6233+ )?;
6234+ if tu_grid.iter().any(|&l| l != 0) {
6235+ done += 1;
6236+ }
6237+ neighbours.record_mi_luma(tu_mi, 4, &tu_grid);
6238+ }
6239+ if done > 0 {
6240+ if vert {
6241+ TX4X8_CODED_HITS.with(|h| h.set(h.get() + 1));
6242+ } else {
6243+ TX8X4_CODED_HITS.with(|h| h.set(h.get() + 1));
6244+ }
6245+ }
6246+ neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip);
6247+ neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, 4, 4, 0);
6248+ if has_chroma {
6249+ last_uv = chroma;
6250+ }
6251+ continue;
6252+ }
61116253 let grid = if skip {
61126254 let zeros = vec![0i32; bw * bh];
61136255 y.reconstruct_rect(
@@ -6172,7 +6314,8 @@ fn decode_leaf_rect8(
61726314 }
61736315 }
61746316 neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip);
6175- neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, bw as u8, bh as u8, 0);
6317+ let (tx_w, tx_h) = if split { (4, 4) } else { (bw as u8, bh as u8) };
6318+ neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, tx_w, tx_h, 0);
61766319 if has_chroma {
61776320 last_uv = chroma;
61786321 }
@@ -7969,6 +8112,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
79698112 base_q_idx,
79708113 enable_filter_intra,
79718114 allow_intrabc,
8115+ tx_select,
79728116 reduced_tx_set,
79738117 )?
79748118 } else {
@@ -8095,6 +8239,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
80958239 base_q_idx,
80968240 enable_filter_intra,
80978241 allow_intrabc,
8242+ tx_select,
80988243 reduced_tx_set,
80998244 )?
81008245 } else {
crates/ec-av1/src/stream.rsmodified+13 -0
@@ -7458,6 +7458,7 @@ mod tests {
74587458 ] {
74597459 let mut refusals = Vec::new();
74607460 let mut fired_runs = 0u32;
7461+ let mut split_tx = 0usize;
74617462 for attempt in 0..40u32 {
74627463 let seed = 300 + attempt;
74637464 let cq = 6 + (attempt % 4) * 2;
@@ -7556,6 +7557,7 @@ mod tests {
75567557 let stream = out.stdout;
75577558 let vert_before = decode::tx4x8_coded_hits();
75587559 let horz_before = decode::tx8x4_coded_hits();
7560+ let split_before = decode::rect8_split_tx_hits();
75597561 let tiles_before = decode::tile_hits();
75607562 let frames = match decode_stream(&stream) {
75617563 Ok(frames) => frames,
@@ -7601,6 +7603,7 @@ mod tests {
76017603 "V vs ffmpeg (seed={seed} cq={cq} depth={bit_depth})"
76027604 );
76037605 fired_runs += 1;
7606+ split_tx += decode::rect8_split_tx_hits() - split_before;
76047607 if fired_runs >= 4 {
76057608 break;
76067609 }
@@ -7611,6 +7614,16 @@ mod tests {
76117614 ({width}x{height} depth={bit_depth} tiles={tile_args:?}):\n{}",
76127615 refusals.join("\n")
76137616 );
7617+ // aomenc's default `--enable-tx-size-search=1` makes TX_MODE_SELECT
7618+ // the frame's tx mode, so every sub-8x8 rect leaf carries a
7619+ // `tx_depth` symbol; depth 1 (two 4x4 transform units predicted one
7620+ // after the other) is the half of that path a whole-leaf transform
7621+ // would silently get wrong, so the gate proves it fired.
7622+ assert!(
7623+ split_tx > 0,
7624+ "no sub-8x8 rect leaf used a split (depth-1) transform in the \
7625+ {fired_runs} pixel-exact runs ({width}x{height} depth={bit_depth})"
7626+ );
76147627 }
76157628 }
76167629