forge
Explore
Log in

Don't you dare go Hollow

Superblock HORZ/VERT strip's luma corner nz-map ctx table -- pixel exact

Browse files at this commit

db23f4bbfb3a70a23bde27543850a19f7433ccacAhmet Kaan GümüşParent: fbce155
  • fix
    • superblock HORZ/VERT strip's luma corner nz-map ctx table -- pixel exact
Show full message
fix(av1): superblock HORZ/VERT strip's luma corner nz-map ctx table -- pixel exact

r7 range-laddered block2's coefficient read to a single symbol: base_ctx at
(row=1, col=0) diverges (ours 3, libaom level 1) with identical entry range,
eob and base_eob. Root cause per libaom source: get_txsize_entropy_ctx
resolves TX_32X64/TX_64X32's CDF *set* to square TX_64X64 (confirmed correct,
ruling out the charter's first suspect), but av1_nz_map_ctx_offset (the
position term feeding base_ctx) is separately indexed by the raw, un-adjusted
tx_size in decodetxb.c -- TX_32X64 and TX_64X32 each own a genuinely
different offset table from the square one (av1_nz_map_ctx_offset_32x64/
_64x32 in txb_common.c), confirmed against libaom source and used already by
lane-rectwire's base_ctx_rect for the smaller (<=32) rectangular sizes.
decode_block_rect64's luma corner read went through the generic (square-only)
base_ctx via read_coeffs, never picking up the rect table.

Added NZ_MAP_CTX_OFFSET_32X64/_64X32 (verified against the real libaom
tables), threaded an Option<(w, h)> rect_shape hint through read_coeffs/
base_ctx (None at every square call site, Some((bw, bh)) only from
decode_block_rect64), and select the matching offset table.

Gate: a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact
now 15/40 pixel-exact matches (0 mismatches, 25 named refusals, sb_rect_hits=36) --
was 0/40 pixel-exact before this round.
crates/ec-av1/src/cdf.rsmodified+27 -0
@@ -761,6 +761,33 @@ pub const NZ_MAP_CTX_OFFSET_32: [[u8; 5]; 5] = [
761761 [21, 21, 21, 21, 21],
762762 ];
763763
764+/// `Av1_Nz_Map_Ctx_Offset[TX_32X64]` (libaom `txb_common.c`
765+/// `av1_nz_map_ctx_offset_32x64`, row and column clamped to four): the real
766+/// 32-wide/64-tall transform's own table, used when a superblock-level
767+/// `PARTITION_VERT` strip's luma corner is read through [`NZ_MAP_CTX_OFFSET_32`]'s
768+/// scan/CDF-set but the *un-adjusted* block shape (spec `get_txsize_entropy_ctx`
769+/// resolves the CDF *set* to `TX_64X64`, but `get_nz_map_ctx_from_stats` still
770+/// indexes this position table by the raw, un-adjusted `tx_size` -- lane-sbpart
771+/// r8 root cause) is genuinely rectangular, not square. Distinct from
772+/// [`NZ_MAP_CTX_OFFSET_32`] starting at `(row=1, col=0)`.
773+pub const NZ_MAP_CTX_OFFSET_32X64: [[u8; 5]; 5] = [
774+ [0, 11, 6, 6, 21],
775+ [11, 11, 6, 21, 21],
776+ [11, 11, 21, 21, 21],
777+ [11, 11, 21, 21, 21],
778+ [11, 11, 21, 21, 21],
779+];
780+
781+/// [`NZ_MAP_CTX_OFFSET_32X64`]'s `TX_64X32` counterpart (libaom
782+/// `av1_nz_map_ctx_offset_64x32`): a `PARTITION_HORZ` strip's luma corner.
783+pub const NZ_MAP_CTX_OFFSET_64X32: [[u8; 5]; 5] = [
784+ [0, 16, 16, 16, 16],
785+ [16, 16, 16, 16, 16],
786+ [6, 6, 21, 21, 21],
787+ [6, 21, 21, 21, 21],
788+ [21, 21, 21, 21, 21],
789+];
790+
764791 /// `Default_Txb_Skip_Cdf[2][2][7..10]` (spec 9.4): the all-zero flag of a
765792 /// 16x16 chroma transform block that covers its whole plane block, for the
766793 /// three contexts its coded neighbours give.
crates/ec-av1/src/decode.rsmodified+35 -3
@@ -970,7 +970,14 @@ fn nz_map_ctx_offset_1d(i: usize) -> usize {
970970 }
971971 }
972972
973-fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize {
973+fn base_ctx(
974+ grid: &[i32],
975+ side: usize,
976+ row: usize,
977+ col: usize,
978+ class: TxClass,
979+ rect_shape: Option<(usize, usize)>,
980+) -> usize {
974981 // libaom's `get_nz_map_ctx_from_stats` only special-cases the DC
975982 // coefficient to ctx 0 for `TX_CLASS_2D` (`(tx_class | coeff_idx) == 0`,
976983 // txb_common.h) -- `V_DCT`/`H_DCT` (class `Horiz`/`Vert`) still run the DC
@@ -993,7 +1000,22 @@ fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -
9931000 .sum();
9941001 let ctx = ((mag + 1) >> 1).min(4) as usize;
9951002 match class {
996- TxClass::TwoD => ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize,
1003+ // libaom's CDF *set* for a truncated 64-wide/tall corner resolves
1004+ // through `get_txsize_entropy_ctx` to the square `TX_64X64` (this
1005+ // decoder's `TxbSet::Luma64`), but `get_nz_map_ctx_from_stats`
1006+ // separately indexes `av1_nz_map_ctx_offset` by the raw, un-adjusted
1007+ // `tx_size` -- `TX_32X64`/`TX_64X32` for a superblock-level HORZ/VERT
1008+ // strip, which own a genuinely different table from the square one
1009+ // (lane-sbpart r8 root cause: `(row=1, col=0)` reads ctx 2 under the
1010+ // square table vs the real ctx 13 the rect table gives).
1011+ TxClass::TwoD => {
1012+ let table = match rect_shape {
1013+ Some((w, h)) if w < h => &cdf::NZ_MAP_CTX_OFFSET_32X64,
1014+ Some((w, h)) if w > h => &cdf::NZ_MAP_CTX_OFFSET_64X32,
1015+ _ => &cdf::NZ_MAP_CTX_OFFSET_32,
1016+ };
1017+ ctx + table[row.min(4)][col.min(4)] as usize
1018+ }
9971019 TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)),
9981020 TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)),
9991021 }
@@ -1286,6 +1308,13 @@ fn read_coeffs(
12861308 // a size the spec forces to `DCT_DCT` regardless of mode (32-point and
12871309 // up, `EXT_TX_SET_DCTONLY`) -- the caller already folds that in.
12881310 default_tx_type: TxType,
1311+ // `Some((w, h))` with `w != h` when this read is a superblock-level
1312+ // HORZ/VERT strip's truncated luma corner: the CDF *set* resolves square
1313+ // (`side`/`coding` above), but `av1_nz_map_ctx_offset`'s position table
1314+ // is indexed by the real, un-adjusted rectangular shape (lane-sbpart r8
1315+ // root cause, see [`base_ctx`]). `None` everywhere else -- every other
1316+ // caller's block genuinely is `side` x `side`.
1317+ rect_shape: Option<(usize, usize)>,
12891318 ) -> Result<(Vec<i32>, TxType)> {
12901319 let trace = std::env::var_os("EC_AV1_TRACE").is_some();
12911320 let side = coding.side;
@@ -1371,7 +1400,7 @@ fn read_coeffs(
13711400 }
13721401 v
13731402 } else {
1374- let ctx = base_ctx(&levels, side, row, col, class);
1403+ let ctx = base_ctx(&levels, side, row, col, class, rect_shape);
13751404 let v = dec.symbol(&mut coding.base[ctx]) as i32;
13761405 if trace {
13771406 eprintln!(
@@ -3434,6 +3463,7 @@ fn decode_block_rect64(
34343463 0,
34353464 dc_sign_ctx(around[0].2),
34363465 TxType::DctDct,
3466+ Some((bw, bh)),
34373467 )?;
34383468 if std::env::var_os("EC_TRACE_COEFF").is_some() {
34393469 let (rng, _) = dec.debug_state();
@@ -4235,6 +4265,7 @@ fn read_plane(
42354265 skip_ctx,
42364266 dc_sign_ctx(around.2),
42374267 default_tx_type,
4268+ None,
42384269 )?;
42394270 // A 64x64 luma block's transform covers the whole 64x64 area, but only its
42404271 // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the
@@ -7115,6 +7146,7 @@ fn read_inter_plane(
71157146 skip_ctx,
71167147 dc_sign_ctx(around.2),
71177148 default_tx_type,
7149+ None,
71187150 )?;
71197151 let residual = dequant_and_inverse_typed(&grid, side, 8, CURRENT_Q_IDX.with(|c| c.get()), tx_type);
71207152 plane.reconstruct_mc(x, y, side, prediction, &residual);