diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 549b7cbb..1e2965f2 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -3161,6 +3161,16 @@ pub const TXB_SKIP_CHROMA_16_Q3: [[u16; 3]; 3] = /// TXB_SKIP_CHROMA_32, q-context 3: the same table as [`TXB_SKIP_CHROMA_32`], for `base_q_idx` 121..=255. pub const TXB_SKIP_CHROMA_32_Q3: [[u16; 3]; 3] = [[4656, 32768, 0], [16074, 32768, 0], [24704, 32768, 0]]; + +/// `av1_default_txb_skip_cdfs[q][TX_32X32][10..13]` (token_cdfs.h): libaom's +/// chroma `txb_skip_ctx` offset is 10 instead of 7 when the plane block is +/// LARGER than the transform (`get_txb_ctx`), which in AV1 happens only for a +/// 128x128 block's four TX_32X32 chroma units -- the offset-7 rows above are +/// every other case (lane-sb128b r3). +pub const TXB_SKIP_CHROMA_32_BIG_Q0: [[u16; 3]; 3] = [[146, 32768, 0], [5132, 32768, 0], [31657, 32768, 0]]; +pub const TXB_SKIP_CHROMA_32_BIG_Q1: [[u16; 3]; 3] = [[641, 32768, 0], [22265, 32768, 0], [31452, 32768, 0]]; +pub const TXB_SKIP_CHROMA_32_BIG: [[u16; 3]; 3] = [[3082, 32768, 0], [20982, 32768, 0], [29443, 32768, 0]]; +pub const TXB_SKIP_CHROMA_32_BIG_Q3: [[u16; 3]; 3] = [[1806, 32768, 0], [14645, 32768, 0], [25336, 32768, 0]]; /// EOB_EXTRA_LUMA_16, q-context 3: the same table as [`EOB_EXTRA_LUMA_16`], for `base_q_idx` 121..=255. pub const EOB_EXTRA_LUMA_16_Q3: [[u16; 3]; 9] = [ [19941, 32768, 0], diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 47035982..a4d2fb07 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -383,7 +383,10 @@ pub(crate) struct Cdfs { /// The all-zero flag of a 16x16 chroma transform. pub txb_skip_chroma_16: [[u16; 3]; 3], /// The all-zero flag of a 32x32 chroma transform. - pub txb_skip_chroma_32: [[u16; 3]; 3], + /// SIX contexts, not three: rows 0..3 are libaom's `txb_skip_ctx` 7..9 + /// (chroma transform == its plane block) and rows 3..6 its 10..12 (plane + /// block larger, i.e. a 128x128 block's four TX_32X32 chroma units). + pub txb_skip_chroma_32: [[u16; 3]; 6], /// The end-of-block group of a luma transform of 1024 positions. pub eob_pt_1024_luma: [u16; 12], /// The same, for chroma. @@ -1237,13 +1240,23 @@ impl Cdfs { cdf::TXB_SKIP_CHROMA_16, cdf::TXB_SKIP_CHROMA_16_Q3, ), - txb_skip_chroma_32: pick( - q_ctx, - cdf::TXB_SKIP_CHROMA_32_Q0, - cdf::TXB_SKIP_CHROMA_32_Q1, - cdf::TXB_SKIP_CHROMA_32, - cdf::TXB_SKIP_CHROMA_32_Q3, - ), + txb_skip_chroma_32: { + let small = pick( + q_ctx, + cdf::TXB_SKIP_CHROMA_32_Q0, + cdf::TXB_SKIP_CHROMA_32_Q1, + cdf::TXB_SKIP_CHROMA_32, + cdf::TXB_SKIP_CHROMA_32_Q3, + ); + let big = pick( + q_ctx, + cdf::TXB_SKIP_CHROMA_32_BIG_Q0, + cdf::TXB_SKIP_CHROMA_32_BIG_Q1, + cdf::TXB_SKIP_CHROMA_32_BIG, + cdf::TXB_SKIP_CHROMA_32_BIG_Q3, + ); + [small[0], small[1], small[2], big[0], big[1], big[2]] + }, eob_pt_1024_luma: pick( q_ctx, cdf::EOB_PT_1024_LUMA_Q0, diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 8104ebfc..842eb8ec 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -8633,7 +8633,12 @@ fn read_plane( let skip_ctx = if plane_idx == 0 { luma_skip_ctx.unwrap_or(0) } else { - usize::from(around.0) + usize::from(around.1) + // libaom `get_txb_ctx`: a chroma unit's context is + // `combine_entropy_contexts(above, left)` plus 7, or plus 10 when the + // plane block is bigger than the transform. Our chroma tables hold the + // offset-7 rows first, so the caller adds 3 for the bigger-block case + // (only a 128x128 block's TX_32X32 chroma units, lane-sb128b r3). + usize::from(around.0) + usize::from(around.1) + luma_skip_ctx.unwrap_or(0) }; // spec 5.11.47/libaom `av1_read_tx_type`: a filter-intra luma block's // `intra_ext_tx_cdf` row is indexed by `fimode_to_intradir[filter_intra_mode]`, @@ -9135,8 +9140,27 @@ fn decode_block( // the earlier tiles in this same block already wrote -- the same // per-TU pattern [`decode_leaf8`] already runs for an 8x8 leaf. let n_axis = side / logical_tx; - for tu_row in 0..n_axis { - for tu_col in 0..n_axis { + let cn = (chroma_side / chroma_tx).max(1); + // libaom's transform-block loop is over 64x64 luma "mu" chunks + // (`decode_token_recon_block`: `mu_blocks_wide/high` = + // `mi_size_wide[BLOCK_64X64]`), and it codes EVERY PLANE of a chunk + // before moving to the next one. Below 128 a block is a single chunk, + // so this is byte-for-byte the all-luma-then-all-chroma order that has + // always run; at 128 the four quadrants interleave Y,U,V per quadrant + // and reading all four luma units first desyncs the tile at the second + // one (lane-sb128b r3, measured against aomdec's `EC_TRACE_COEFF`). + let nchunk = (side / 64).max(1); + // `cn > 1` means the uv plane block is larger than the uv transform, + // which moves libaom's chroma `txb_skip_ctx` offset from 7 to 10. + let chroma_ctx_offset = if cn > 1 { Some(3) } else { None }; + let tpc = (n_axis / nchunk).max(1); + let cpc = (cn / nchunk).max(1); + let mut last_grids = [Vec::new(), Vec::new()]; + let mut units: Vec<((usize, usize), usize, Vec, Vec)> = Vec::new(); + for ch_row in 0..nchunk { + for ch_col in 0..nchunk { + for tu_row in ch_row * tpc..(ch_row + 1) * tpc { + for tu_col in ch_col * tpc..(ch_col + 1) * tpc { let tu_mi = ( mi_r + tu_row * (logical_tx / MI), mi_c + tu_col * (logical_tx / MI), @@ -9219,19 +9243,17 @@ fn decode_block( neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); } } + // CfL is only allowed up to 32x32, i.e. only at `nchunk == 1`, where + // this still runs after every luma unit of the block. let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); // The block's uv plane block is `chroma_side` across but its uv // transform is `chroma_tx` (`av1_get_max_uv_txsize` = // `av1_get_adjusted_tx_size(max_txsize_rect_lookup[uv_plane_bsize])`): // identical at every size up to 64x64, but a 128x128 block's 64x64 uv // plane block adjusts TX_64X64 down to TX_32X32, so each plane is - // FOUR units (lane-sb128b r2). `cn == 1` is byte-for-byte the single - // whole-plane read this always did. - let cn = (chroma_side / chroma_tx).max(1); - let mut last_grids = [Vec::new(), Vec::new()]; - let mut units: Vec<((usize, usize), usize, Vec, Vec)> = Vec::new(); - for cu_row in 0..cn { - for cu_col in 0..cn { + // FOUR units (lane-sb128b r2) -- one per mu chunk. + for cu_row in ch_row * cpc..(ch_row + 1) * cpc { + for cu_col in ch_col * cpc..(ch_col + 1) * cpc { // The chroma context arrays are indexed in LUMA mi, so this // unit's anchor is its own luma quadrant: `chroma_tx` chroma // pixels span `2 * chroma_tx` luma ones. @@ -9259,7 +9281,7 @@ fn decode_block( uv_predict_mode, angle_delta_uv, cu_reach, u, cu_x, cu_y, chroma_tx, chroma_tx, base_q_idx, alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), - None, None, smooth_neighbor_uv, + None, chroma_ctx_offset, smooth_neighbor_uv, )?; if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone()); @@ -9269,15 +9291,25 @@ fn decode_block( uv_predict_mode, angle_delta_uv, cu_reach, v, cu_x, cu_y, chroma_tx, chroma_tx, base_q_idx, alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), - None, None, smooth_neighbor_uv, + None, chroma_ctx_offset, smooth_neighbor_uv, )?; if cn > 1 { CHROMA_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); + // Immediately, so the NEXT unit of this same block reads + // this one's coefficient context (libaom's chroma + // `txb_skip_ctx` for the second quadrant is 11, i.e. base + // 1 from this left neighbour -- measured against aomdec). + // The same writes are replayed after `record_split_luma` + // below, which would otherwise clobber them whole-block. + neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 1, &u_grid); + neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 2, &v_grid); units.push((cu_mi, luma_span, u_grid.clone(), v_grid.clone())); } last_grids = [u_grid, v_grid]; } } + } + } // Mode/side bookkeeping plus, at `cn == 1`, the whole-plane chroma // context write; at `cn > 1` the per-unit writes above are what the // next block reads, so they run after this (last writer wins). @@ -12342,18 +12374,6 @@ fn read_sb128_root( PART128_SPLIT_HITS.with(|c| c.set(c.get() + 1)); } else { PART128_NONE_HITS.with(|c| c.set(c.get() + 1)); - // lane-sb128b r2 RESIDUAL, measured (gate - // `a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact`, - // 384x384 cq63 seed 42, a 76-byte near-flat frame): the chroma TU loop - // landed and the tile decodes end to end, but frame 0 luma differs from - // ffmpeg at 139162/147456 samples -- ffmpeg's row 0 is a constant 115 - // where ours ramps 115 -> 112 across the first TX_64X64 unit, i.e. our - // first luma unit reads AC coefficients libaom never wrote. The DC and - // the mode agree; the divergence is inside that unit's coefficient - // read. Refuse rather than emit wrong pixels. - return Err(unsupported( - "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)", - )); } Ok(part128) } diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 76aa54ad..55afe670 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -43,7 +43,6 @@ const REFUSALS: &[&str] = &[ "a tx_type symbol outside its CDF's own set: {t}", "an INTER 32x32 partition type this decoder does not code (value={part32})", "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4", - "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)", "a 128x128 superblock HORZ/VERT or AB partition (only SPLIT and NONE are decoded at the 128 root)", "a 128x128 superblock PARTITION_NONE root on an inter frame (only the key-frame path codes a whole 128x128 block)", "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)", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index d459f54c..c9d0ed82 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -18014,9 +18014,6 @@ mod tests { /// Smooth gradients at a high `--cq-level` are what make the RD search /// keep a whole 128x128 block; `part128_none_hits` HARD-asserts it did. #[test] - #[ignore = "lane-sb128b r2 reproducer: the 128 NONE luma AND chroma units all \ - decode, the first luma unit still reads AC libaom never wrote, so the \ - product path refuses the arm by name"] fn a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_key_frame_with_a_128x128_none_partition_decodes_pixel_exact";