forge
Explore
Log in

Don't you dare go Hollow

A 128x128 block codes its planes per 64x64 mu chunk, and its chroma units use libaom's offset-10 txb_skip rows

Browse files at this commit

69826a6db3a6da68f1df51606a48d993f65d46ceAhmet Kaan GümüşParent: 4bf07db
  • fix
    • a 128x128 block codes its planes per 64x64 mu chunk, and its chroma units use libaom's offset-10 txb_skip rows
Show full message
fix(av1): a 128x128 block codes its planes per 64x64 mu chunk, and its chroma units use libaom's offset-10 txb_skip rows

Three root causes behind lane-sb128b r2's "the first TX_64X64 luma unit reads
AC libaom never wrote" residual; the entropy ladder (aomdec EC_TRACE_COEFF vs
ours, 48 txb_skip symbols) is now identical range for range and all four gate
arms decode pixel-exact.

1. decode.rs `decode_block`'s multi-TU arm read every luma unit and then every
   chroma unit. libaom's `decode_token_recon_block` loops 64x64 luma "mu"
   chunks (`mu_blocks_wide/high` = `mi_size_wide[BLOCK_64X64]`) and codes ALL
   PLANES of a chunk before the next chunk, so at 128 the order is Y,U,V per
   quadrant. Below 128 a block is one chunk and the order is unchanged.
2. The chroma `txb_skip_ctx` offset is 10, not 7, when the uv plane block is
   larger than the uv transform (`get_txb_ctx`) -- the 128 block's four
   TX_32X32 chroma units are AV1's only such case, and our chroma table only
   carried the offset-7 rows. Added `TXB_SKIP_CHROMA_32_BIG*` (token_cdfs.h
   `av1_default_txb_skip_cdfs[q][TX_32X32][10..13]`, all four q contexts),
   `txb_skip_chroma_32` is now six rows, and the caller adds 3 when `cn > 1`.
3. Each chroma unit's coefficient context was recorded only after the whole
   block, so the second quadrant read ctx 10 where libaom reads 11. Recorded
   per unit inside the loop as well (the post-`record_split_luma` replay
   stays: that call would otherwise clobber them whole-block).

Refusal lifted with its gate: `a 128x128 PARTITION_NONE block (...)` deleted
from decode.rs + refusal_inventory.rs, and
`a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact`
un-ignored -- 4/4 arms pixel-exact (384x384 seeds 42/43, 256x256 seed 44,
10-bit seed 45), part128_none_hits=31.
crates/ec-av1/src/cdf.rsmodified+10 -0
@@ -3161,6 +3161,16 @@ pub const TXB_SKIP_CHROMA_16_Q3: [[u16; 3]; 3] =
31613161 /// TXB_SKIP_CHROMA_32, q-context 3: the same table as [`TXB_SKIP_CHROMA_32`], for `base_q_idx` 121..=255.
31623162 pub const TXB_SKIP_CHROMA_32_Q3: [[u16; 3]; 3] =
31633163 [[4656, 32768, 0], [16074, 32768, 0], [24704, 32768, 0]];
3164+
3165+/// `av1_default_txb_skip_cdfs[q][TX_32X32][10..13]` (token_cdfs.h): libaom's
3166+/// chroma `txb_skip_ctx` offset is 10 instead of 7 when the plane block is
3167+/// LARGER than the transform (`get_txb_ctx`), which in AV1 happens only for a
3168+/// 128x128 block's four TX_32X32 chroma units -- the offset-7 rows above are
3169+/// every other case (lane-sb128b r3).
3170+pub const TXB_SKIP_CHROMA_32_BIG_Q0: [[u16; 3]; 3] = [[146, 32768, 0], [5132, 32768, 0], [31657, 32768, 0]];
3171+pub const TXB_SKIP_CHROMA_32_BIG_Q1: [[u16; 3]; 3] = [[641, 32768, 0], [22265, 32768, 0], [31452, 32768, 0]];
3172+pub const TXB_SKIP_CHROMA_32_BIG: [[u16; 3]; 3] = [[3082, 32768, 0], [20982, 32768, 0], [29443, 32768, 0]];
3173+pub const TXB_SKIP_CHROMA_32_BIG_Q3: [[u16; 3]; 3] = [[1806, 32768, 0], [14645, 32768, 0], [25336, 32768, 0]];
31643174 /// EOB_EXTRA_LUMA_16, q-context 3: the same table as [`EOB_EXTRA_LUMA_16`], for `base_q_idx` 121..=255.
31653175 pub const EOB_EXTRA_LUMA_16_Q3: [[u16; 3]; 9] = [
31663176 [19941, 32768, 0],
crates/ec-av1/src/cdf_state.rsmodified+21 -8
@@ -383,7 +383,10 @@ pub(crate) struct Cdfs {
383383 /// The all-zero flag of a 16x16 chroma transform.
384384 pub txb_skip_chroma_16: [[u16; 3]; 3],
385385 /// The all-zero flag of a 32x32 chroma transform.
386- pub txb_skip_chroma_32: [[u16; 3]; 3],
386+ /// SIX contexts, not three: rows 0..3 are libaom's `txb_skip_ctx` 7..9
387+ /// (chroma transform == its plane block) and rows 3..6 its 10..12 (plane
388+ /// block larger, i.e. a 128x128 block's four TX_32X32 chroma units).
389+ pub txb_skip_chroma_32: [[u16; 3]; 6],
387390 /// The end-of-block group of a luma transform of 1024 positions.
388391 pub eob_pt_1024_luma: [u16; 12],
389392 /// The same, for chroma.
@@ -1237,13 +1240,23 @@ impl Cdfs {
12371240 cdf::TXB_SKIP_CHROMA_16,
12381241 cdf::TXB_SKIP_CHROMA_16_Q3,
12391242 ),
1240- txb_skip_chroma_32: pick(
1241- q_ctx,
1242- cdf::TXB_SKIP_CHROMA_32_Q0,
1243- cdf::TXB_SKIP_CHROMA_32_Q1,
1244- cdf::TXB_SKIP_CHROMA_32,
1245- cdf::TXB_SKIP_CHROMA_32_Q3,
1246- ),
1243+ txb_skip_chroma_32: {
1244+ let small = pick(
1245+ q_ctx,
1246+ cdf::TXB_SKIP_CHROMA_32_Q0,
1247+ cdf::TXB_SKIP_CHROMA_32_Q1,
1248+ cdf::TXB_SKIP_CHROMA_32,
1249+ cdf::TXB_SKIP_CHROMA_32_Q3,
1250+ );
1251+ let big = pick(
1252+ q_ctx,
1253+ cdf::TXB_SKIP_CHROMA_32_BIG_Q0,
1254+ cdf::TXB_SKIP_CHROMA_32_BIG_Q1,
1255+ cdf::TXB_SKIP_CHROMA_32_BIG,
1256+ cdf::TXB_SKIP_CHROMA_32_BIG_Q3,
1257+ );
1258+ [small[0], small[1], small[2], big[0], big[1], big[2]]
1259+ },
12471260 eob_pt_1024_luma: pick(
12481261 q_ctx,
12491262 cdf::EOB_PT_1024_LUMA_Q0,
crates/ec-av1/src/decode.rsmodified+44 -24
@@ -8633,7 +8633,12 @@ fn read_plane(
86338633 let skip_ctx = if plane_idx == 0 {
86348634 luma_skip_ctx.unwrap_or(0)
86358635 } else {
8636- usize::from(around.0) + usize::from(around.1)
8636+ // libaom `get_txb_ctx`: a chroma unit's context is
8637+ // `combine_entropy_contexts(above, left)` plus 7, or plus 10 when the
8638+ // plane block is bigger than the transform. Our chroma tables hold the
8639+ // offset-7 rows first, so the caller adds 3 for the bigger-block case
8640+ // (only a 128x128 block's TX_32X32 chroma units, lane-sb128b r3).
8641+ usize::from(around.0) + usize::from(around.1) + luma_skip_ctx.unwrap_or(0)
86378642 };
86388643 // spec 5.11.47/libaom `av1_read_tx_type`: a filter-intra luma block's
86398644 // `intra_ext_tx_cdf` row is indexed by `fimode_to_intradir[filter_intra_mode]`,
@@ -9135,8 +9140,27 @@ fn decode_block(
91359140 // the earlier tiles in this same block already wrote -- the same
91369141 // per-TU pattern [`decode_leaf8`] already runs for an 8x8 leaf.
91379142 let n_axis = side / logical_tx;
9138- for tu_row in 0..n_axis {
9139- for tu_col in 0..n_axis {
9143+ let cn = (chroma_side / chroma_tx).max(1);
9144+ // libaom's transform-block loop is over 64x64 luma "mu" chunks
9145+ // (`decode_token_recon_block`: `mu_blocks_wide/high` =
9146+ // `mi_size_wide[BLOCK_64X64]`), and it codes EVERY PLANE of a chunk
9147+ // before moving to the next one. Below 128 a block is a single chunk,
9148+ // so this is byte-for-byte the all-luma-then-all-chroma order that has
9149+ // always run; at 128 the four quadrants interleave Y,U,V per quadrant
9150+ // and reading all four luma units first desyncs the tile at the second
9151+ // one (lane-sb128b r3, measured against aomdec's `EC_TRACE_COEFF`).
9152+ let nchunk = (side / 64).max(1);
9153+ // `cn > 1` means the uv plane block is larger than the uv transform,
9154+ // which moves libaom's chroma `txb_skip_ctx` offset from 7 to 10.
9155+ let chroma_ctx_offset = if cn > 1 { Some(3) } else { None };
9156+ let tpc = (n_axis / nchunk).max(1);
9157+ let cpc = (cn / nchunk).max(1);
9158+ let mut last_grids = [Vec::new(), Vec::new()];
9159+ let mut units: Vec<((usize, usize), usize, Vec<i32>, Vec<i32>)> = Vec::new();
9160+ for ch_row in 0..nchunk {
9161+ for ch_col in 0..nchunk {
9162+ for tu_row in ch_row * tpc..(ch_row + 1) * tpc {
9163+ for tu_col in ch_col * tpc..(ch_col + 1) * tpc {
91409164 let tu_mi = (
91419165 mi_r + tu_row * (logical_tx / MI),
91429166 mi_c + tu_col * (logical_tx / MI),
@@ -9219,19 +9243,17 @@ fn decode_block(
92199243 neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid);
92209244 }
92219245 }
9246+ // CfL is only allowed up to 32x32, i.e. only at `nchunk == 1`, where
9247+ // this still runs after every luma unit of the block.
92229248 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
92239249 // The block's uv plane block is `chroma_side` across but its uv
92249250 // transform is `chroma_tx` (`av1_get_max_uv_txsize` =
92259251 // `av1_get_adjusted_tx_size(max_txsize_rect_lookup[uv_plane_bsize])`):
92269252 // identical at every size up to 64x64, but a 128x128 block's 64x64 uv
92279253 // plane block adjusts TX_64X64 down to TX_32X32, so each plane is
9228- // FOUR units (lane-sb128b r2). `cn == 1` is byte-for-byte the single
9229- // whole-plane read this always did.
9230- let cn = (chroma_side / chroma_tx).max(1);
9231- let mut last_grids = [Vec::new(), Vec::new()];
9232- let mut units: Vec<((usize, usize), usize, Vec<i32>, Vec<i32>)> = Vec::new();
9233- for cu_row in 0..cn {
9234- for cu_col in 0..cn {
9254+ // FOUR units (lane-sb128b r2) -- one per mu chunk.
9255+ for cu_row in ch_row * cpc..(ch_row + 1) * cpc {
9256+ for cu_col in ch_col * cpc..(ch_col + 1) * cpc {
92359257 // The chroma context arrays are indexed in LUMA mi, so this
92369258 // unit's anchor is its own luma quadrant: `chroma_tx` chroma
92379259 // pixels span `2 * chroma_tx` luma ones.
@@ -9259,7 +9281,7 @@ fn decode_block(
92599281 uv_predict_mode, angle_delta_uv, cu_reach, u, cu_x, cu_y,
92609282 chroma_tx, chroma_tx, base_q_idx,
92619283 alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
9262- None, None, smooth_neighbor_uv,
9284+ None, chroma_ctx_offset, smooth_neighbor_uv,
92639285 )?;
92649286 if let Some((_, vb)) = &palette_uv_bufs {
92659287 set_palette_pred(vb.clone());
@@ -9269,15 +9291,25 @@ fn decode_block(
92699291 uv_predict_mode, angle_delta_uv, cu_reach, v, cu_x, cu_y,
92709292 chroma_tx, chroma_tx, base_q_idx,
92719293 alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
9272- None, None, smooth_neighbor_uv,
9294+ None, chroma_ctx_offset, smooth_neighbor_uv,
92739295 )?;
92749296 if cn > 1 {
92759297 CHROMA_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1));
9298+ // Immediately, so the NEXT unit of this same block reads
9299+ // this one's coefficient context (libaom's chroma
9300+ // `txb_skip_ctx` for the second quadrant is 11, i.e. base
9301+ // 1 from this left neighbour -- measured against aomdec).
9302+ // The same writes are replayed after `record_split_luma`
9303+ // below, which would otherwise clobber them whole-block.
9304+ neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 1, &u_grid);
9305+ neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 2, &v_grid);
92769306 units.push((cu_mi, luma_span, u_grid.clone(), v_grid.clone()));
92779307 }
92789308 last_grids = [u_grid, v_grid];
92799309 }
92809310 }
9311+ }
9312+ }
92819313 // Mode/side bookkeeping plus, at `cn == 1`, the whole-plane chroma
92829314 // context write; at `cn > 1` the per-unit writes above are what the
92839315 // next block reads, so they run after this (last writer wins).
@@ -12342,18 +12374,6 @@ fn read_sb128_root(
1234212374 PART128_SPLIT_HITS.with(|c| c.set(c.get() + 1));
1234312375 } else {
1234412376 PART128_NONE_HITS.with(|c| c.set(c.get() + 1));
12345- // lane-sb128b r2 RESIDUAL, measured (gate
12346- // `a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact`,
12347- // 384x384 cq63 seed 42, a 76-byte near-flat frame): the chroma TU loop
12348- // landed and the tile decodes end to end, but frame 0 luma differs from
12349- // ffmpeg at 139162/147456 samples -- ffmpeg's row 0 is a constant 115
12350- // where ours ramps 115 -> 112 across the first TX_64X64 unit, i.e. our
12351- // first luma unit reads AC coefficients libaom never wrote. The DC and
12352- // the mode agree; the divergence is inside that unit's coefficient
12353- // read. Refuse rather than emit wrong pixels.
12354- return Err(unsupported(
12355- "a 128x128 PARTITION_NONE block (four TX_64X64 luma and four TX_32X32 chroma units all decode, but the first unit's coefficients already carry AC where libaom has DC only -- lane-sb128b r2's open entropy residual)",
12356- ));
1235712377 }
1235812378 Ok(part128)
1235912379 }
crates/ec-av1/src/refusal_inventory.rsmodified+0 -1
@@ -43,7 +43,6 @@ const REFUSALS: &[&str] = &[
4343 "a tx_type symbol outside its CDF's own set: {t}",
4444 "an INTER 32x32 partition type this decoder does not code (value={part32})",
4545 "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4",
46- "a 128x128 PARTITION_NONE block (four TX_64X64 luma and four TX_32X32 chroma units all decode, but the first unit's coefficients already carry AC where libaom has DC only -- lane-sb128b r2's open entropy residual)",
4746 "a 128x128 superblock HORZ/VERT or AB partition (only SPLIT and NONE are decoded at the 128 root)",
4847 "a 128x128 superblock PARTITION_NONE root on an inter frame (only the key-frame path codes a whole 128x128 block)",
4948 "intrabc under a 128x128 superblock (libaom's av1_is_dv_valid derives the block-vector delay from sb_size, which this decoder hardcodes to 64)",
crates/ec-av1/src/stream.rsmodified+0 -3
@@ -18014,9 +18014,6 @@ mod tests {
1801418014 /// Smooth gradients at a high `--cq-level` are what make the RD search
1801518015 /// keep a whole 128x128 block; `part128_none_hits` HARD-asserts it did.
1801618016 #[test]
18017- #[ignore = "lane-sb128b r2 reproducer: the 128 NONE luma AND chroma units all \
18018- decode, the first luma unit still reads AC libaom never wrote, so the \
18019- product path refuses the arm by name"]
1802018017 fn a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact() {
1802118018 const NAME: &str =
1802218019 "a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact";