Superblock HORZ/VERT strip's luma corner nz-map ctx table -- pixel exact
Browse files at this commitdb23f4bbfb3a70a23bde27543850a19f7433ccacParent: fbce155- fix
- superblock HORZ/VERT strip's luma corner nz-map ctx table -- pixel exact
2 changed files +62 -3
- crates/ec-av1/src/cdf.rs +27 -0
- crates/ec-av1/src/decode.rs +35 -3
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 a | 791 | /// `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 the | 792 | /// 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 DC | 981 | // 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 DC | 983 | // 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 and | 1308 | // 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 | v | 1401 | 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 its | 4270 | // 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 the | 4271 | // 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); |