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] = [
761 [21, 21, 21, 21, 21],761 [21, 21, 21, 21, 21],
762 ];762 ];
763 763
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+
764 /// `Default_Txb_Skip_Cdf[2][2][7..10]` (spec 9.4): the all-zero flag of a791 /// `Default_Txb_Skip_Cdf[2][2][7..10]` (spec 9.4): the all-zero flag of a
765 /// 16x16 chroma transform block that covers its whole plane block, for the792 /// 16x16 chroma transform block that covers its whole plane block, for the
766 /// three contexts its coded neighbours give.793 /// 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 {
970 }970 }
971 }971 }
972 972
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 {
974 // libaom's `get_nz_map_ctx_from_stats` only special-cases the DC981 // libaom's `get_nz_map_ctx_from_stats` only special-cases the DC
975 // coefficient to ctx 0 for `TX_CLASS_2D` (`(tx_class | coeff_idx) == 0`,982 // coefficient to ctx 0 for `TX_CLASS_2D` (`(tx_class | coeff_idx) == 0`,
976 // txb_common.h) -- `V_DCT`/`H_DCT` (class `Horiz`/`Vert`) still run the DC983 // 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) -
993 .sum();1000 .sum();
994 let ctx = ((mag + 1) >> 1).min(4) as usize;1001 let ctx = ((mag + 1) >> 1).min(4) as usize;
995 match class {1002 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+ }
997 TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)),1019 TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)),
998 TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)),1020 TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)),
999 }1021 }
@@ -1286,6 +1308,13 @@ fn read_coeffs(
1286 // a size the spec forces to `DCT_DCT` regardless of mode (32-point and1308 // a size the spec forces to `DCT_DCT` regardless of mode (32-point and
1287 // up, `EXT_TX_SET_DCTONLY`) -- the caller already folds that in.1309 // up, `EXT_TX_SET_DCTONLY`) -- the caller already folds that in.
1288 default_tx_type: TxType,1310 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)>,
1289 ) -> Result<(Vec<i32>, TxType)> {1318 ) -> Result<(Vec<i32>, TxType)> {
1290 let trace = std::env::var_os("EC_AV1_TRACE").is_some();1319 let trace = std::env::var_os("EC_AV1_TRACE").is_some();
1291 let side = coding.side;1320 let side = coding.side;
@@ -1371,7 +1400,7 @@ fn read_coeffs(
1371 }1400 }
1372 v1401 v
1373 } else {1402 } else {
1374- let ctx = base_ctx(&levels, side, row, col, class);1403+ let ctx = base_ctx(&levels, side, row, col, class, rect_shape);
1375 let v = dec.symbol(&mut coding.base[ctx]) as i32;1404 let v = dec.symbol(&mut coding.base[ctx]) as i32;
1376 if trace {1405 if trace {
1377 eprintln!(1406 eprintln!(
@@ -3434,6 +3463,7 @@ fn decode_block_rect64(
3434 0,3463 0,
3435 dc_sign_ctx(around[0].2),3464 dc_sign_ctx(around[0].2),
3436 TxType::DctDct,3465 TxType::DctDct,
3466+ Some((bw, bh)),
3437 )?;3467 )?;
3438 if std::env::var_os("EC_TRACE_COEFF").is_some() {3468 if std::env::var_os("EC_TRACE_COEFF").is_some() {
3439 let (rng, _) = dec.debug_state();3469 let (rng, _) = dec.debug_state();
@@ -4235,6 +4265,7 @@ fn read_plane(
4235 skip_ctx,4265 skip_ctx,
4236 dc_sign_ctx(around.2),4266 dc_sign_ctx(around.2),
4237 default_tx_type,4267 default_tx_type,
4268+ None,
4238 )?;4269 )?;
4239 // A 64x64 luma block's transform covers the whole 64x64 area, but only its4270 // A 64x64 luma block's transform covers the whole 64x64 area, but only its
4240 // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the4271 // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the
@@ -7115,6 +7146,7 @@ fn read_inter_plane(
7115 skip_ctx,7146 skip_ctx,
7116 dc_sign_ctx(around.2),7147 dc_sign_ctx(around.2),
7117 default_tx_type,7148 default_tx_type,
7149+ None,
7118 )?;7150 )?;
7119 let residual = dequant_and_inverse_typed(&grid, side, 8, CURRENT_Q_IDX.with(|c| c.get()), tx_type);7151 let residual = dequant_and_inverse_typed(&grid, side, 8, CURRENT_Q_IDX.with(|c| c.get()), tx_type);
7120 plane.reconstruct_mc(x, y, side, prediction, &residual);7152 plane.reconstruct_mc(x, y, side, prediction, &residual);