diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 819560b5..2d9bdbba 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -224,6 +224,48 @@ pub const EOB_PT_1024_LUMA: [u16; 12] = [ /// coefficient is the DC. pub const COEFF_BASE_EOB_LUMA_64_DC: [u16; 4] = [935, 3382, 32768, 0]; +/// `av1_default_eob_multi512_cdfs[2][0][0]` (`token_cdfs.h:829-906`, +/// lane-rectwire): the end-of-block group of a luma transform with 512 coded +/// positions -- what a true 32x16/16x32 rectangular transform has +/// (`txsize_log2_minus4` = 5, `common_data.h:346`), distinct from +/// [`EOB_PT_1024_LUMA`]'s 1024-position group a *square* 32x32 reads. +pub const EOB_PT_512_LUMA: [u16; 11] = [ + 2624, 3936, 6480, 9686, 13979, 17726, 23267, 28410, 31078, 32768, 0, +]; +/// [`EOB_PT_512_LUMA`], q-context 0 (`base_q_idx` 0..=20). +pub const EOB_PT_512_LUMA_Q0: [u16; 11] = [ + 641, 983, 3707, 5430, 10234, 14958, 18788, 23412, 26061, 32768, 0, +]; +/// [`EOB_PT_512_LUMA`], q-context 1 (`base_q_idx` 21..=60). +pub const EOB_PT_512_LUMA_Q1: [u16; 11] = [ + 1230, 2278, 5035, 7776, 11871, 15346, 19590, 24584, 28749, 32768, 0, +]; +/// [`EOB_PT_512_LUMA`], q-context 3 (`base_q_idx` 121..=255). +pub const EOB_PT_512_LUMA_Q3: [u16; 11] = [ + 5927, 7809, 10923, 14597, 19439, 24135, 28456, 31142, 32060, 32768, 0, +]; + +/// `av1_default_eob_multi128_cdfs[2][1][0]` (`token_cdfs.h:829-906`, +/// lane-rectwire): the end-of-block group of a chroma transform with 128 +/// coded positions -- what a true 16x8/8x16 rectangular transform has +/// (`txsize_log2_minus4` = 3), distinct from [`EOB_PT_256_CHROMA`]'s +/// 256-position group a *square* 16x16 reads. +pub const EOB_PT_128_CHROMA: [u16; 9] = [ + 13627, 16246, 20173, 24429, 27948, 30415, 31863, 32768, 0, +]; +/// [`EOB_PT_128_CHROMA`], q-context 0. +pub const EOB_PT_128_CHROMA_Q0: [u16; 9] = [ + 5245, 7456, 12880, 15852, 20033, 23932, 27608, 32768, 0, +]; +/// [`EOB_PT_128_CHROMA`], q-context 1. +pub const EOB_PT_128_CHROMA_Q1: [u16; 9] = [ + 8045, 11200, 15497, 19595, 23948, 27408, 30938, 32768, 0, +]; +/// [`EOB_PT_128_CHROMA`], q-context 3. +pub const EOB_PT_128_CHROMA_Q3: [u16; 9] = [ + 24313, 26062, 28385, 30107, 31217, 31898, 32345, 32768, 0, +]; + /// `Default_Dc_Sign_Cdf[2][0]` (spec 9.4): the sign of a luma DC coefficient, /// indexed by whether the neighbouring blocks' DC signs lean negative (1), /// positive (2) or neither (0). diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 7e06f28f..b8b0e0ca 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -90,6 +90,20 @@ pub(crate) enum TxbSet { Chroma16, /// The 32x32 transform of a chroma plane of a whole superblock. Chroma32, + /// The luma 32x16/16x32 transform of a `PARTITION_HORZ`/`VERT` intra + /// strip (lane-rectwire): `get_txsize_entropy_ctx` reduces any 2:1 rect + /// size to its square-up equivalent (`entropy.h:172`), so this shares + /// every table [`Luma32`](TxbSet::Luma32) does except `eob_pt` -- a true + /// 512-position transform reads [`crate::cdf::EOB_PT_512_LUMA`], not + /// `Luma32`'s 1024-position one. Never carries a `tx_type` symbol: both + /// orientations use one variant (`get_tx_set` is `EXT_TX_SET_DCTONLY` at + /// `tx_size_sqr_up == TX_32X32` regardless of which axis is longer). + LumaRect32x16, + /// The chroma 16x8/8x16 transform under the same strip (lane-rectwire): + /// [`Chroma16`](TxbSet::Chroma16)'s tables except `eob_pt`, which reads + /// the true 128-position [`crate::cdf::EOB_PT_128_CHROMA`] instead of + /// `Chroma16`'s 256-position one. + ChromaRect16x8, } /// The tables one transform block is coded with, borrowed from the state for @@ -233,6 +247,12 @@ pub(crate) struct Cdfs { pub eob_pt_1024_chroma: [u16; 12], /// The end-of-block group of a 16x16 chroma transform. pub eob_pt_256_chroma: [u16; 10], + /// The end-of-block group of a true 32x16/16x32 rect luma transform + /// (lane-rectwire), see [`TxbSet::LumaRect32x16`]. + pub eob_pt_512_luma: [u16; 11], + /// The end-of-block group of a true 16x8/8x16 rect chroma transform + /// (lane-rectwire), see [`TxbSet::ChromaRect16x8`]. + pub eob_pt_128_chroma: [u16; 9], /// The end-of-block offset bit, per table set. pub eob_extra_luma_32: [[u16; 3]; 9], /// The same, for a 64x64 luma transform. @@ -560,6 +580,8 @@ impl Cdfs { reset1(&mut self.eob_pt_1024_luma); reset1(&mut self.eob_pt_1024_chroma); reset1(&mut self.eob_pt_256_chroma); + reset1(&mut self.eob_pt_512_luma); + reset1(&mut self.eob_pt_128_chroma); reset2(&mut self.eob_extra_luma_32); reset2(&mut self.eob_extra_luma_64); reset2(&mut self.eob_extra_chroma_16); @@ -924,6 +946,20 @@ impl Cdfs { cdf::EOB_PT_256_CHROMA, cdf::EOB_PT_256_CHROMA_Q3, ), + eob_pt_512_luma: pick( + q_ctx, + cdf::EOB_PT_512_LUMA_Q0, + cdf::EOB_PT_512_LUMA_Q1, + cdf::EOB_PT_512_LUMA, + cdf::EOB_PT_512_LUMA_Q3, + ), + eob_pt_128_chroma: pick( + q_ctx, + cdf::EOB_PT_128_CHROMA_Q0, + cdf::EOB_PT_128_CHROMA_Q1, + cdf::EOB_PT_128_CHROMA, + cdf::EOB_PT_128_CHROMA_Q3, + ), eob_extra_luma_32: pick( q_ctx, cdf::EOB_EXTRA_LUMA_32_Q0, @@ -1288,6 +1324,30 @@ impl Cdfs { tx_type: None, eob_pt_class1: None, }, + TxbSet::LumaRect32x16 => TxbTables { + side: 32, + txb_skip: &mut self.txb_skip_luma_32, + eob_pt: &mut self.eob_pt_512_luma, + eob_extra: &mut self.eob_extra_luma_32, + base: &mut self.base_luma_32, + base_eob: &mut self.base_eob_luma_32, + br: &mut self.br_luma_32, + dc_sign: &mut self.dc_sign_luma, + tx_type: None, + eob_pt_class1: None, + }, + TxbSet::ChromaRect16x8 => TxbTables { + side: 16, + txb_skip: &mut self.txb_skip_chroma_16, + eob_pt: &mut self.eob_pt_128_chroma, + eob_extra: &mut self.eob_extra_chroma_16, + base: &mut self.base_chroma_16, + base_eob: &mut self.base_eob_chroma_16, + br: &mut self.br_chroma_16, + dc_sign: &mut self.dc_sign_chroma, + tx_type: None, + eob_pt_class1: None, + }, } } } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 62503f7a..db177dd9 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -38,7 +38,7 @@ use crate::mvstack::{ single_ref_p6_ctx, uni_comp_ref_p1_ctx, }; use crate::tile::{INTRA_MODE_CTX, block_grid, has_half}; -use crate::transform::{TxType, dequant_and_inverse_typed}; +use crate::transform::{TxType, dequant_and_inverse_typed, dequant_and_inverse_typed_wh}; const PARTITION_NONE: usize = 0; const PARTITION_HORZ: usize = 1; @@ -272,6 +272,17 @@ pub(crate) fn rect_partition_hits() -> usize { RECT_PARTITION_HITS.load(std::sync::atomic::Ordering::Relaxed) } +/// lane-rectwire r2: how many `PARTITION_HORZ`/`VERT` strips actually decoded +/// real (non-skip) coefficients through [`read_coeffs_rect`] -- proves the +/// rect coefficient reader itself fired, not just the strip-level partition +/// symbol [`RECT_PARTITION_HITS`] already counts. +static RECT_COEFF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// Current value of [`RECT_COEFF_HITS`]. +pub(crate) fn rect_coeff_hits() -> usize { + RECT_COEFF_HITS.load(std::sync::atomic::Ordering::Relaxed) +} + /// lane-partab r1: how many 32x32 quadrants decoded an AB partition /// (PARTITION_HORZ_A / VERT_A / VERT_B -- two 16x16 squares plus one /// 16x32/32x16 strip). Like [`RECT_PARTITION_HITS`], this is the @@ -616,6 +627,145 @@ fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> if near_origin { mag + 7 } else { mag + 14 } } +/// `Default_Scan_32x16` (spec 8.4.2, libaom `scan.c`'s `default_scan_32x16`): +/// transposed from libaom's column-major buffer into this decoder's row-major +/// `pos = row * 32 + col` (lane-rectwire, verified as a bijection of `0..512` +/// against the real table). Not the square zigzag formula generalized to +/// unequal `w`/`h` -- checked and that does NOT reproduce this table. +const SCAN_32X16: [u16; 512] = [ + 0, 32, 1, 64, 33, 2, 96, 65, 34, 3, 128, 97, 66, 35, 4, 160, 129, 98, 67, 36, 5, 192, 161, + 130, 99, 68, 37, 6, 224, 193, 162, 131, 100, 69, 38, 7, 256, 225, 194, 163, 132, 101, 70, 39, + 8, 288, 257, 226, 195, 164, 133, 102, 71, 40, 9, 320, 289, 258, 227, 196, 165, 134, 103, 72, + 41, 10, 352, 321, 290, 259, 228, 197, 166, 135, 104, 73, 42, 11, 384, 353, 322, 291, 260, 229, + 198, 167, 136, 105, 74, 43, 12, 416, 385, 354, 323, 292, 261, 230, 199, 168, 137, 106, 75, 44, + 13, 448, 417, 386, 355, 324, 293, 262, 231, 200, 169, 138, 107, 76, 45, 14, 480, 449, 418, + 387, 356, 325, 294, 263, 232, 201, 170, 139, 108, 77, 46, 15, 481, 450, 419, 388, 357, 326, + 295, 264, 233, 202, 171, 140, 109, 78, 47, 16, 482, 451, 420, 389, 358, 327, 296, 265, 234, + 203, 172, 141, 110, 79, 48, 17, 483, 452, 421, 390, 359, 328, 297, 266, 235, 204, 173, 142, + 111, 80, 49, 18, 484, 453, 422, 391, 360, 329, 298, 267, 236, 205, 174, 143, 112, 81, 50, 19, + 485, 454, 423, 392, 361, 330, 299, 268, 237, 206, 175, 144, 113, 82, 51, 20, 486, 455, 424, + 393, 362, 331, 300, 269, 238, 207, 176, 145, 114, 83, 52, 21, 487, 456, 425, 394, 363, 332, + 301, 270, 239, 208, 177, 146, 115, 84, 53, 22, 488, 457, 426, 395, 364, 333, 302, 271, 240, + 209, 178, 147, 116, 85, 54, 23, 489, 458, 427, 396, 365, 334, 303, 272, 241, 210, 179, 148, + 117, 86, 55, 24, 490, 459, 428, 397, 366, 335, 304, 273, 242, 211, 180, 149, 118, 87, 56, 25, + 491, 460, 429, 398, 367, 336, 305, 274, 243, 212, 181, 150, 119, 88, 57, 26, 492, 461, 430, + 399, 368, 337, 306, 275, 244, 213, 182, 151, 120, 89, 58, 27, 493, 462, 431, 400, 369, 338, + 307, 276, 245, 214, 183, 152, 121, 90, 59, 28, 494, 463, 432, 401, 370, 339, 308, 277, 246, + 215, 184, 153, 122, 91, 60, 29, 495, 464, 433, 402, 371, 340, 309, 278, 247, 216, 185, 154, + 123, 92, 61, 30, 496, 465, 434, 403, 372, 341, 310, 279, 248, 217, 186, 155, 124, 93, 62, 31, + 497, 466, 435, 404, 373, 342, 311, 280, 249, 218, 187, 156, 125, 94, 63, 498, 467, 436, 405, + 374, 343, 312, 281, 250, 219, 188, 157, 126, 95, 499, 468, 437, 406, 375, 344, 313, 282, 251, + 220, 189, 158, 127, 500, 469, 438, 407, 376, 345, 314, 283, 252, 221, 190, 159, 501, 470, 439, + 408, 377, 346, 315, 284, 253, 222, 191, 502, 471, 440, 409, 378, 347, 316, 285, 254, 223, 503, + 472, 441, 410, 379, 348, 317, 286, 255, 504, 473, 442, 411, 380, 349, 318, 287, 505, 474, 443, + 412, 381, 350, 319, 506, 475, 444, 413, 382, 351, 507, 476, 445, 414, 383, 508, 477, 446, 415, + 509, 478, 447, 510, 479, 511, +]; + +/// `Default_Scan_16x32`, this decoder's own row-major transcription (see +/// [`SCAN_32X16`]'s doc comment). +const SCAN_16X32: [u16; 512] = [ + 0, 1, 16, 2, 17, 32, 3, 18, 33, 48, 4, 19, 34, 49, 64, 5, 20, 35, 50, 65, 80, 6, 21, 36, 51, + 66, 81, 96, 7, 22, 37, 52, 67, 82, 97, 112, 8, 23, 38, 53, 68, 83, 98, 113, 128, 9, 24, 39, 54, + 69, 84, 99, 114, 129, 144, 10, 25, 40, 55, 70, 85, 100, 115, 130, 145, 160, 11, 26, 41, 56, 71, + 86, 101, 116, 131, 146, 161, 176, 12, 27, 42, 57, 72, 87, 102, 117, 132, 147, 162, 177, 192, + 13, 28, 43, 58, 73, 88, 103, 118, 133, 148, 163, 178, 193, 208, 14, 29, 44, 59, 74, 89, 104, + 119, 134, 149, 164, 179, 194, 209, 224, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, + 195, 210, 225, 240, 31, 46, 61, 76, 91, 106, 121, 136, 151, 166, 181, 196, 211, 226, 241, 256, + 47, 62, 77, 92, 107, 122, 137, 152, 167, 182, 197, 212, 227, 242, 257, 272, 63, 78, 93, 108, + 123, 138, 153, 168, 183, 198, 213, 228, 243, 258, 273, 288, 79, 94, 109, 124, 139, 154, 169, + 184, 199, 214, 229, 244, 259, 274, 289, 304, 95, 110, 125, 140, 155, 170, 185, 200, 215, 230, + 245, 260, 275, 290, 305, 320, 111, 126, 141, 156, 171, 186, 201, 216, 231, 246, 261, 276, 291, + 306, 321, 336, 127, 142, 157, 172, 187, 202, 217, 232, 247, 262, 277, 292, 307, 322, 337, 352, + 143, 158, 173, 188, 203, 218, 233, 248, 263, 278, 293, 308, 323, 338, 353, 368, 159, 174, 189, + 204, 219, 234, 249, 264, 279, 294, 309, 324, 339, 354, 369, 384, 175, 190, 205, 220, 235, 250, + 265, 280, 295, 310, 325, 340, 355, 370, 385, 400, 191, 206, 221, 236, 251, 266, 281, 296, 311, + 326, 341, 356, 371, 386, 401, 416, 207, 222, 237, 252, 267, 282, 297, 312, 327, 342, 357, 372, + 387, 402, 417, 432, 223, 238, 253, 268, 283, 298, 313, 328, 343, 358, 373, 388, 403, 418, 433, + 448, 239, 254, 269, 284, 299, 314, 329, 344, 359, 374, 389, 404, 419, 434, 449, 464, 255, 270, + 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 271, 286, 301, 316, 331, + 346, 361, 376, 391, 406, 421, 436, 451, 466, 481, 496, 287, 302, 317, 332, 347, 362, 377, 392, + 407, 422, 437, 452, 467, 482, 497, 303, 318, 333, 348, 363, 378, 393, 408, 423, 438, 453, 468, + 483, 498, 319, 334, 349, 364, 379, 394, 409, 424, 439, 454, 469, 484, 499, 335, 350, 365, 380, + 395, 410, 425, 440, 455, 470, 485, 500, 351, 366, 381, 396, 411, 426, 441, 456, 471, 486, 501, + 367, 382, 397, 412, 427, 442, 457, 472, 487, 502, 383, 398, 413, 428, 443, 458, 473, 488, 503, + 399, 414, 429, 444, 459, 474, 489, 504, 415, 430, 445, 460, 475, 490, 505, 431, 446, 461, 476, + 491, 506, 447, 462, 477, 492, 507, 463, 478, 493, 508, 479, 494, 509, 495, 510, 511, +]; + +/// `Default_Scan_16x8`, this decoder's own row-major transcription (see +/// [`SCAN_32X16`]'s doc comment). +const SCAN_16X8: [u16; 128] = [ + 0, 16, 1, 32, 17, 2, 48, 33, 18, 3, 64, 49, 34, 19, 4, 80, 65, 50, 35, 20, 5, 96, 81, 66, 51, + 36, 21, 6, 112, 97, 82, 67, 52, 37, 22, 7, 113, 98, 83, 68, 53, 38, 23, 8, 114, 99, 84, 69, 54, + 39, 24, 9, 115, 100, 85, 70, 55, 40, 25, 10, 116, 101, 86, 71, 56, 41, 26, 11, 117, 102, 87, + 72, 57, 42, 27, 12, 118, 103, 88, 73, 58, 43, 28, 13, 119, 104, 89, 74, 59, 44, 29, 14, 120, + 105, 90, 75, 60, 45, 30, 15, 121, 106, 91, 76, 61, 46, 31, 122, 107, 92, 77, 62, 47, 123, 108, + 93, 78, 63, 124, 109, 94, 79, 125, 110, 95, 126, 111, 127, +]; + +/// `Default_Scan_8x16`, this decoder's own row-major transcription (see +/// [`SCAN_32X16`]'s doc comment). +const SCAN_8X16: [u16; 128] = [ + 0, 1, 8, 2, 9, 16, 3, 10, 17, 24, 4, 11, 18, 25, 32, 5, 12, 19, 26, 33, 40, 6, 13, 20, 27, 34, + 41, 48, 7, 14, 21, 28, 35, 42, 49, 56, 15, 22, 29, 36, 43, 50, 57, 64, 23, 30, 37, 44, 51, 58, + 65, 72, 31, 38, 45, 52, 59, 66, 73, 80, 39, 46, 53, 60, 67, 74, 81, 88, 47, 54, 61, 68, 75, 82, + 89, 96, 55, 62, 69, 76, 83, 90, 97, 104, 63, 70, 77, 84, 91, 98, 105, 112, 71, 78, 85, 92, 99, + 106, 113, 120, 79, 86, 93, 100, 107, 114, 121, 87, 94, 101, 108, 115, 122, 95, 102, 109, 116, + 123, 103, 110, 117, 124, 111, 118, 125, 119, 126, 127, +]; + +/// [`neighbour`] with independent `w` (row stride)/`h` (row-bound) extents +/// (lane-rectwire, mirrors [`record_rect`](Neighbours::record_rect)'s +/// asymmetric-extent pattern). +fn neighbour_rect(grid: &[i32], w: usize, h: usize, row: usize, col: usize) -> i32 { + if row >= h || col >= w { + 0 + } else { + grid[row * w + col] + } +} + +/// [`base_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): +/// the rect generalization is libaom's own comment in +/// `get_nz_map_ctx_from_stats` (`txb_common.h:189-224`), reduces to the exact +/// same [`cdf::NZ_MAP_CTX_OFFSET_32`] table when `w == h`. Never called for +/// `V_DCT`/`H_DCT` -- geometrically impossible at the two size pairs +/// [`decode_block_rect`] codes (see that function's own doc comment). +fn base_ctx_rect(grid: &[i32], w: usize, h: usize, row: usize, col: usize) -> usize { + if row == 0 && col == 0 { + return 0; + } + const OFFSETS: [(usize, usize); 5] = [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)]; + let mag: i32 = OFFSETS + .iter() + .map(|&(dr, dc)| neighbour_rect(grid, w, h, row + dr, col + dc).abs().min(3)) + .sum(); + let ctx = ((mag + 1) >> 1).min(4) as usize; + if w < h && row < 2 { + return ctx + 11; + } + if w > h && col < 2 { + return ctx + 16; + } + ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize +} + +/// [`br_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): the +/// neighbour-offset math itself is shape-independent, only the boundary +/// clamp needs the real bound in each axis. +fn br_ctx_rect(grid: &[i32], w: usize, h: usize, row: usize, col: usize) -> usize { + let extra = neighbour_rect(grid, w, h, row + 1, col + 1).abs(); + let mag = neighbour_rect(grid, w, h, row + 1, col).abs() + + neighbour_rect(grid, w, h, row, col + 1).abs() + + extra; + let mag = (((mag + 1) >> 1).min(6)) as usize; + if row == 0 && col == 0 { + return mag; + } + if row < 2 && col < 2 { mag + 7 } else { mag + 14 } +} + fn dc_vote(dc: Option) -> i32 { match dc { None => 0, @@ -882,6 +1032,89 @@ fn read_coeffs( Ok((grid, tx_type)) } +/// [`read_coeffs`] widened to `(w, h)` (lane-rectwire), restricted to +/// `TxClass::TwoD` -- the only class either rect size pair this decoder codes +/// can ever produce (see [`decode_block_rect`]'s doc comment): refuses by +/// name rather than guess-decode if a `tx_type` symbol or non-2D class ever +/// shows up here, since neither `base_ctx_rect`/`br_ctx_rect` nor +/// [`class_scan_table`] have a rect form for those. +fn read_coeffs_rect( + dec: &mut SymbolDecoder, + coding: &mut TxbTables, + scan: &[u16], + w: usize, + h: usize, + skip_ctx: usize, + sign_ctx: usize, + default_tx_type: TxType, +) -> Result<(Vec, TxType)> { + let mut grid = vec![0i32; w * h]; + let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; + if all_zero { + return Ok((grid, TxType::DctDct)); + } + if coding.tx_type.is_some() { + return Err(unsupported( + "a tx_type symbol on a rectangular transform (never expected at this size)", + )); + } + let tx_type = default_tx_type; + if TxClass::of(tx_type) != TxClass::TwoD { + return Err(unsupported( + "a non-2D tx class on a rectangular transform (never expected at this size)", + )); + } + let eob = read_eob(dec, coding, TxClass::TwoD); + let mut levels = vec![0i32; w * h]; + for scan_idx in (0..eob).rev() { + let pos = scan[scan_idx] as usize; + let (row, col) = (pos / w, pos % w); + let level = if scan_idx == eob - 1 { + let ctx = eob_coeff_ctx(scan_idx, w * h); + dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1 + } else { + let ctx = base_ctx_rect(&levels, w, h, row, col); + dec.symbol(&mut coding.base[ctx]) as i32 + }; + let level = if level > NUM_BASE_LEVELS { + let ctx = br_ctx_rect(&levels, w, h, row, col); + let mut level = level; + let mut sent = 0; + loop { + let k = dec.symbol(&mut coding.br[ctx]) as i32; + level += k; + sent += BR_STEP; + if k < BR_STEP || sent >= COEFF_BASE_RANGE { + break; + } + } + level + } else { + level + }; + levels[pos] = level; + } + for &pos in &scan[..eob] { + let level = levels[pos as usize]; + if level == 0 { + continue; + } + let negative = if pos == 0 { + dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1 + } else { + dec.literal(1) == 1 + }; + let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { + let g = read_golomb(dec)?; + level + g as i32 + } else { + level + }; + grid[pos as usize] = if negative { -level } else { level }; + } + Ok((grid, tx_type)) +} + /// What one coded block leaves behind for the blocks that read it as a /// neighbour: whether it coded anything at all, and the sign of its DC — /// [`crate::tile`]'s own private `Neighbour`. @@ -1336,6 +1569,35 @@ impl Neighbours { }) } + /// [`Self::around`] with independent above (`w`)/left (`h`) extents, in + /// pixels -- lane-rectwire, mirrors [`Self::record_rect`]'s asymmetric + /// extent pattern for a true rectangular strip's own coefficient context. + fn around_rect(&self, (r, c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] { + self.around_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h) + } + + /// [`Self::around_rect`] taking the block's position directly in 4x4 + /// mode-info units, mirroring [`Self::around_mi`]. + fn around_mi_rect(&self, (mi_r, mi_c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] { + let (w_mi, h_mi) = (w / MI, h / MI); + std::array::from_fn(|plane| { + let mut above_coded = false; + let mut left_coded = false; + let mut vote = 0; + for cell in 0..w_mi { + let above = &self.above[mi_c + cell][plane]; + above_coded |= above.level != 0; + vote += dc_vote(above.dc); + } + for cell in 0..h_mi { + let left = &self.left[mi_r + cell][plane]; + left_coded |= left.level != 0; + vote += dc_vote(left.dc); + } + (above_coded, left_coded, vote) + }) + } + /// The luma `txb_skip_ctx` of a transform unit smaller than its own block /// (spec `get_txb_ctx_general`, plane 0, `plane_bsize != tx_size`): the /// above/left neighbours' magnitude tiers, OR-reduced over the unit's own @@ -1722,6 +1984,7 @@ fn decode_block_rect( v: &mut PlaneBuf, enable_filter_intra: bool, allow_screen_content_tools: bool, + base_q_idx: u8, ) -> Result<()> { let (r, c) = at; let (px, py) = (c * SUB, r * SUB); @@ -1769,64 +2032,195 @@ fn decode_block_rect( )); } let (tx_w, tx_h) = (bw, bh); - if !skip { - return Err(unsupported( - "a non-skip HORZ/VERT intra strip needs a rectangular transform \ - this decoder does not code yet", - )); - } let (cpx, cpy) = (px / 2, py / 2); let (chroma_w, chroma_h) = (bw / 2, bh / 2); let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); - y.reconstruct_rect( - px, - py, - bw, - bh, - mode, - angle_delta_y, - reach, - &vec![0i32; bw * bh], - None, - filter_intra, - ); - let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); - u.reconstruct_rect( - cpx, - cpy, - chroma_w, - chroma_h, - uv_predict_mode, - angle_delta_uv, - reach, - &vec![0i32; chroma_w * chroma_h], - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), - None, - ); - v.reconstruct_rect( - cpx, - cpy, - chroma_w, - chroma_h, - uv_predict_mode, - angle_delta_uv, - reach, - &vec![0i32; chroma_w * chroma_h], - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), - None, - ); - neighbours.record_rect( - at, - bw, - bh, - mode, - &[ - vec![0i32; bw * bh], - vec![0i32; chroma_w * chroma_h], - vec![0i32; chroma_w * chroma_h], - ], - ); - neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, true); + if skip { + y.reconstruct_rect( + px, + py, + bw, + bh, + mode, + angle_delta_y, + reach, + &vec![0i32; bw * bh], + None, + filter_intra, + ); + let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); + u.reconstruct_rect( + cpx, + cpy, + chroma_w, + chroma_h, + uv_predict_mode, + angle_delta_uv, + reach, + &vec![0i32; chroma_w * chroma_h], + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + None, + ); + v.reconstruct_rect( + cpx, + cpy, + chroma_w, + chroma_h, + uv_predict_mode, + angle_delta_uv, + reach, + &vec![0i32; chroma_w * chroma_h], + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + None, + ); + neighbours.record_rect( + at, + bw, + bh, + mode, + &[ + vec![0i32; bw * bh], + vec![0i32; chroma_w * chroma_h], + vec![0i32; chroma_w * chroma_h], + ], + ); + } else { + // lane-rectwire r2: real coefficients. `get_txsize_entropy_ctx` + // reduces both size pairs to their square-up CDF sets + // (`TxbSet::LumaRect32x16`/`ChromaRect16x8`, see those variants' own + // doc comments); the scan order and 2D context tables are the real + // rect ones (`SCAN_32X16`/etc, `base_ctx_rect`/`br_ctx_rect`). + let (luma_scan, chroma_scan): (&[u16], &[u16]) = if bw == 32 { + (&SCAN_32X16, &SCAN_16X8) + } else { + (&SCAN_16X32, &SCAN_8X16) + }; + // lane-rectwire r3: real coefficients desync a pixel-exact gate + // (rectwire-flake-1.obu, seed 55) -- both 32x16 strips of frame 0's + // mi(8,8) HORZ quadrant come out ~100% wrong from their very first + // pixel (not a subtle context-table off-by-one), while the sibling + // non-rect quadrants of the same frame match. Range-ladder bisection + // against a real aomdec (EC_TRACE_COEFF/EC_TRACE_MODE) was started + // but the intra key-frame path has no traced symbol between the + // partition read and the first coefficient block (mode/skip/tx_depth + // reads are all untraced in libaom's decodeframe.c intra path, unlike + // the inter EC_TRACE_MODE this round added), so a same-length range + // comparison wasn't reachable within budget -- see + // lanes/rectwire-r3.report.md for what was ruled out (base_ctx_rect's + // 5-neighbour offsets, u/v_skip_ctx's above+left formula, and + // dc_sign_ctx's around-index all cross-checked byte-for-byte against + // libaom and match). Refusing by name again (r1 behavior) rather than + // risk landing the desync. + return Err(unsupported( + "a HORZ/VERT intra strip with real (non-skip) coefficients \ + (lane-rectwire r3: confirmed desync, not yet isolated)", + )); + #[allow(unreachable_code)] + { + let around = neighbours.around_rect(at, bw, bh); + let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x16, mode); + let (luma_levels, luma_tx_type) = read_coeffs_rect( + dec, + &mut luma_coding, + luma_scan, + bw, + bh, + 0, + dc_sign_ctx(around[0].2), + TxType::DctDct, + )?; + let luma_residual = dequant_and_inverse_typed_wh( + &luma_levels, + bw, + bh, + 8, + i32::from(base_q_idx), + luma_tx_type, + ); + y.reconstruct_rect( + px, + py, + bw, + bh, + mode, + angle_delta_y, + reach, + &luma_residual, + None, + filter_intra, + ); + let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); + let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); + let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); + let mut u_coding = cdfs.txb(TxbSet::ChromaRect16x8, uv_predict_mode); + let (u_levels, u_tx_type) = read_coeffs_rect( + dec, + &mut u_coding, + chroma_scan, + chroma_w, + chroma_h, + u_skip_ctx, + dc_sign_ctx(around[1].2), + u_default_tx, + )?; + let u_residual = dequant_and_inverse_typed_wh( + &u_levels, + chroma_w, + chroma_h, + 8, + i32::from(base_q_idx), + u_tx_type, + ); + u.reconstruct_rect( + cpx, + cpy, + chroma_w, + chroma_h, + uv_predict_mode, + angle_delta_uv, + reach, + &u_residual, + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + None, + ); + let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); + let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); + let mut v_coding = cdfs.txb(TxbSet::ChromaRect16x8, uv_predict_mode); + let (v_levels, v_tx_type) = read_coeffs_rect( + dec, + &mut v_coding, + chroma_scan, + chroma_w, + chroma_h, + v_skip_ctx, + dc_sign_ctx(around[2].2), + v_default_tx, + )?; + let v_residual = dequant_and_inverse_typed_wh( + &v_levels, + chroma_w, + chroma_h, + 8, + i32::from(base_q_idx), + v_tx_type, + ); + v.reconstruct_rect( + cpx, + cpy, + chroma_w, + chroma_h, + uv_predict_mode, + angle_delta_uv, + reach, + &v_residual, + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + None, + ); + neighbours.record_rect(at, bw, bh, mode, &[luma_levels, u_levels, v_levels]); + RECT_COEFF_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } + } + neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0); RECT_PARTITION_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); Ok(()) @@ -4350,6 +4744,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, enable_filter_intra, allow_screen_content_tools, + base_q_idx, )?; decode_block_rect( &mut dec, @@ -4363,6 +4758,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, enable_filter_intra, allow_screen_content_tools, + base_q_idx, )?; } PARTITION_VERT => { @@ -4380,6 +4776,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, enable_filter_intra, allow_screen_content_tools, + base_q_idx, )?; decode_block_rect( &mut dec, @@ -4393,6 +4790,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, enable_filter_intra, allow_screen_content_tools, + base_q_idx, )?; } _ => { diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index c41eb80e..533bf9b7 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -4185,10 +4185,25 @@ mod tests { "{NAME}: SOFT-NOTE -- zero HORZ/VERT strips fired this run \ ({matched} matches, {named_refusals} refusals); sampling, not a regression" ); + } else if crate::decode::rect_coeff_hits() == 0 { + // lane-rectwire r3: r2's real (non-skip) coefficient decode + // desyncs (rectwire-flake-1.obu, seed 55 -- both 32x16 strips of + // a fired quadrant come out ~100% wrong from their first pixel; + // see lanes/rectwire-r3.report.md). Reverted to refusing a coded + // rect strip by name, same as r1, so `rect_coeff_hits` is always + // 0 now -- this used to be a hard assert (r2), downgraded back to + // a SOFT-NOTE until the desync is isolated and the real decode + // path is safe to re-enable. + eprintln!( + "{NAME}: SOFT-NOTE -- {} HORZ/VERT strips fired but rect_coeff_hits==0 \ + (r3: non-skip rect strips refuse by name pending the desync fix)", + crate::decode::rect_partition_hits() + ); } eprintln!( - "{NAME}: {named_refusals} named refusals, {matched} pixel-exact matches out of {n_attempts}, rect_partition_hits={} extended_partition_hits={} partab_hits={}", + "{NAME}: {named_refusals} named refusals, {matched} pixel-exact matches out of {n_attempts}, rect_partition_hits={} rect_coeff_hits={} extended_partition_hits={} partab_hits={}", crate::decode::rect_partition_hits(), + crate::decode::rect_coeff_hits(), crate::decode::extended_partition_hits(), crate::decode::partab_hits() ); diff --git a/lanes/rectwire-r1.charter.md b/lanes/rectwire-r1.charter.md new file mode 100644 index 00000000..d800cf16 --- /dev/null +++ b/lanes/rectwire-r1.charter.md @@ -0,0 +1,74 @@ +# lane-rectwire r1 charter — wire the rectangular transform into decode.rs + +## Where you are +Worktree /home/user/Documents/Code/Rust/edith_codecs-rectwire, branch +lane-rectwire @ main (6e54e47). +Build/test ONLY: + `env CARGO_TARGET_DIR=$HOME/.cache/cargo-target-rectwire CARGO_BUILD_JOBS=4 nice -n 19 cargo test -p ec-av1 --release --lib -- --nocapture` +FOREGROUND, EC_AV1_REQUIRE_AOMENC=1 on gates. Never push. Never touch the main +checkout or the sibling -gm worktree. WIP COMMIT after every green milestone. + +## The primitive already exists — do not rebuild it +lane-recttx landed the rectangular inverse transform this lane consumes: +`transform.rs::inverse_transform_2d_typed_wh(dequant, w, h, bit_depth, tx_type)` +and `dequant_and_inverse_typed_wh`, with `quant.rs::dequant_wh` beside them. +All 14 rect sizes are pinned by checksum against real libaom kernels +(`transform.rs::tests::rect_sizes_pinned_against_libaom`) and the square path +is proven bit-identical. Read `lanes/recttx-r1.report.md` first. The transform +MATH is done; this lane is the decode-side wiring it named as still owed. + +## What is missing (the report's own list) +1. **Rectangular scan order.** Only square zig-zag/diagonal tables are wired. + libaom's scan tables live in `~/.cache/aom-oracle/src/av1/common/scan.c` + (`av1_default_scan_x` and the class/`av1_scan_orders` table). +2. **Rectangular eob context.** The `get_eob_ctx` family is keyed on a square + tx size class today; libaom keys it on the real tx size + (`av1/common/txb_common.h`, `av1/decoder/decodetxb.c`). +3. **`max_txsize_rect_lookup` threading** through partition and tx-size + selection, so a rect block asks for its real tx size instead of a square + approximation. + +## Two refusals this is meant to lift, in priority order +- `decode.rs`'s refusal of every INTER partition below 16x16 (grep + "an inter partition below 16x16"). This is why the 8x8-OBMC gate has never + fired one 8x8 OBMC block in 80 attempts, despite the encoder writing them + constantly. +- The intra HORZ/VERT strips' skip-only restriction (grep "a non-skip HORZ/VERT + intra strip"), and the strip refusal in screen-content frames beside it. + +Lifting EITHER one with pixel-exact evidence is a good round. Do not attempt +both if the first is not clean — a partial landing that refuses precisely is +worth more than a wide one that decodes wrong. + +## Traps this repo has already paid for — read these before you start +- **Cross-axis scan weights.** A scan weight or step can use the CROSS axis + where libaom says so; square candidates hide every axis swap. Sweep a WxH + case together with its HxW twin in the SAME test, always. +- **A reference's layout is not the spec's.** libaom's coefficient buffer is + column-major; ours is row-major. Transpose at the boundary, never in our + code, or a real axis bug gets cancelled by a transposed harness. +- **A new tx size threads more surfaces than you expect** — scan, eob, context + derivation, the tx-set tables. Grep the reference for every indexed surface + and sweep them in ONE round rather than discovering them one desync at a time. +- **Adapting CDF four-site wiring**: struct field, defaults, save/restore, + per-frame counter reset. A table missing from the counter reset has the right + values and the wrong adaptation rate, and desyncs only after many reads. + +## Method +Refuse-by-name first, decode second: it is always acceptable to read every +symbol correctly and refuse the reconstruction by name. A desync is not +acceptable. On any pixel mismatch, self-pin with +`EC_AV1_GATE_DUMP=/tmp/claude-1000/rectwire-flake-N.obu`, then localize by +comparing the msac RANGE (never `tell()` — baselines differ by fixed constants +between decoders) from our `EC_AV1_TRACE` output against the instrumented +`~/.cache/aom-oracle/build/aomdec` under `EC_TRACE=1`. Ranges equal up to the +bad block means a prediction defect; ranges diverging means a consumption +defect. Do NOT guess-fix. + +## Done criteria +Full lib suite stays 226 passed / 0 failed. At least one of the two refusals +narrowed or lifted with pixel-exact evidence, and the gate that proves it +fires and HARD-ASSERTS its firing count (a gate that cannot fire the feature it +names measures nothing — the 8x8-OBMC gate is the cautionary example). +REPORT `lanes/rectwire-r1.report.md`, VERDICT on the FIRST line, with the +before/after refusal counts and the firing count. diff --git a/lanes/rectwire-r1.report.md b/lanes/rectwire-r1.report.md new file mode 100644 index 00000000..bf74478e --- /dev/null +++ b/lanes/rectwire-r1.report.md @@ -0,0 +1,168 @@ +# lane-rectwire r1 report + +VERDICT: NO CODE LANDED — design fully worked out and verified against real +libaom source, but not implemented, because the remaining turn budget did not +leave room to implement AND test-iterate a coefficient-decode surface where +"a desync is not acceptable" is the hard rule. Landing an unverified multi-site +edit at the end of a budget is exactly how a desync gets shipped; refusing by +inaction here is the charter's own method applied to the meta-level. Tree is +untouched (`git status --short` empty on HEAD), lib suite is unchanged at +226 passed / 0 failed, refusal counts are unchanged from before this round. + +## What actually narrowed: the scope, not the code + +`decode_block_rect` (`decode.rs:1713`) is called from exactly **four** call +sites, all at exactly **two** fixed size pairs: luma 32x16/16x32 +(`decode.rs:4341-4396`, only from a 32x32 quadrant's `PARTITION_HORZ`/`VERT`) +and its chroma half 16x8/8x16. This is much narrower than the charter's "every +INTER partition below 16x16" framing suggested for the intra strip -- the +non-skip refusal at `decode.rs:1772-1776` only ever needs to cover these two +size pairs, not a general rect-tx-size machine. The INTER-partition-below-16x16 +refusal (`decode.rs:8697`) was not investigated this round (charter: land one +cleanly, not both). + +## The three "missing surfaces" the charter named, resolved + +1. **Rectangular scan order.** libaom's `default_scan_WxH` tables are + precomputed data, not a general algorithm in source (no generator shipped + in `~/.cache/aom-oracle/src`). Extracted+transposed via a one-off script + (libaom's own buffer is column-major, `col = pos >> bhl; row = pos - + (col << bhl)` where `bhl = log2(height)`; our grid is row-major, so + `our_pos = row * w + col`). Verified as a genuine bijection of `0..w*h` for + all four needed tables (32x16, 16x32, 16x8, 8x16). Script + generated + consts are in `/tmp/claude-1000/gen_scan.py` and + `/tmp/claude-1000/scan_tables.rs` in this worktree's scratchpad (not + copied into the repo since nothing consumes them yet). + +2. **Rectangular eob context.** `eob_coeff_ctx(scan_idx, area)` already takes + `area`, not `side`, and libaom's own `get_lower_levels_ctx_eob` formula + (`(width << bhl)`, i.e. `width * height`) reduces to the exact same `area` + — **no change needed here**, only a caller passing `w * h` instead of + `side * side`. The genuine gap is `base_ctx`'s 2D-class position-offset + table (`NZ_MAP_CTX_OFFSET_32`, `cdf.rs:569`), which is square-only. + libaom's own comment in `txb_common.h:189-224` + (`get_nz_map_ctx_from_stats`) gives the exact rect generalization + verbatim: `if width < height && row < 2: offset 11; else if width > height + && col < 2: offset 16; else` fall through to the existing `row+col<2→1, + <4→6, else 21` table square case already implements. Confirmed this + reduces to the identical square table when `width == height` (the two new + branches never fire). `br_ctx`'s neighbour-offset math is shape-independent + for `TxClass::TwoD` (only the boundary clamp needs real `w`/`h` instead of + one `side`). + +3. **`max_txsize_rect_lookup` threading.** Turned out to be unnecessary for + this specific lift: `decode_block_rect` already resolves `tx_w = bw, tx_h + = bh` directly (no split, since `depth != 0` is refused above the skip + check) — the real tx size *is* the block size here, spec's rect-lookup + table is a non-issue at this call site. + +## The one genuinely new piece: two CDF table sets + +Coefficient CDFs are selected by `get_txsize_entropy_ctx(tx_size) = +txsize_sqr_up_map[tx_size]` (`entropy.h:172`, confirmed algebraically: for any +2:1 rect size this reduces to exactly `sqr_up`, never the average). For our +two size pairs that means: +- 32x16/16x32 luma → same square-equivalent (`TX_32X32`) as the already-wired + `TxbSet::Luma32` for **every** field except `eob_pt` — `txb_skip`, `base`, + `base_eob`, `br`, `dc_sign` are all directly reusable as-is (confirmed field + presence: `txb_skip_luma_32`, `base_luma_32`, `base_eob_luma_32`, + `br_luma_32`, `dc_sign_luma` all already exist in `cdf_state.rs`). +- 16x8/8x16 chroma → same square-equivalent (`TX_16X16`) as `TxbSet::Chroma16`, + same story (`txb_skip_chroma_16`, `base_chroma_16`, `base_eob_chroma_16`, + `br_chroma_16`, `dc_sign_chroma` all exist). + +Only `eob_pt` differs: the real-size eob group count is `log2(w*h) - 4` +(`txsize_log2_minus4`, confirmed exact match against the table in +`common_data.h:346`), which for 32x16/16x32 is 5 (the **512**-group, 10 +symbols) — not 6 (**1024**-group, `eob_pt_1024_luma`, what `Luma32` reads for +a true 32x32) — and for 16x8/8x16 is 3 (the **128**-group, 8 symbols) — not 4 +(**256**-group, `eob_pt_256_chroma`, what `Chroma16` reads for a true 16x16). +Neither `eob_pt_512_luma` nor `eob_pt_128_chroma` exist in `cdf_state.rs` +today. Their default values (4 q-context variants each, luma plane_type 0 +class-2D and chroma plane_type 1 class-2D) were extracted and transcribed +from `~/.cache/aom-oracle/src/av1/common/token_cdfs.h:829-906` +(`av1_default_eob_multi128_cdfs`, `av1_default_eob_multi512_cdfs`), format +confirmed against this codebase's own conversion convention (`AOM_CDFn`'s raw +arguments direct, no `AOM_ICDF` inversion — cross-checked `EOB_PT_1024_LUMA` +byte-for-byte against `av1_default_eob_multi1024_cdfs[2][0][0]`, an exact +match, q-index 2 = the unsuffixed default, 0/1/3 = `_Q0`/`_Q1`/`_Q3`): + +``` +EOB_PT_512_LUMA (q2, plane0, class2D): 2624,3936,6480,9686,13979,17726,23267,28410,31078,32768,0 +EOB_PT_512_LUMA_Q0: 641,983,3707,5430,10234,14958,18788,23412,26061,32768,0 +EOB_PT_512_LUMA_Q1: 1230,2278,5035,7776,11871,15346,19590,24584,28749,32768,0 +EOB_PT_512_LUMA_Q3: 5927,7809,10923,14597,19439,24135,28456,31142,32060,32768,0 + +EOB_PT_128_CHROMA (q2, plane1, class2D): 13627,16246,20173,24429,27948,30415,31863,32768,0 +EOB_PT_128_CHROMA_Q0: 5245,7456,12880,15852,20033,23932,27608,32768,0 +EOB_PT_128_CHROMA_Q1: 8045,11200,15497,19595,23948,27408,30938,32768,0 +EOB_PT_128_CHROMA_Q3: 24313,26062,28385,30107,31217,31898,32345,32768,0 +``` + +## The wiring plan (unexecuted, for the next round) + +1. `cdf.rs`: add the 8 arrays above. +2. `cdf_state.rs`: add `eob_pt_512_luma: [u16; 11]` / `eob_pt_128_chroma: + [u16; 9]` fields, `pick()`-initialize in `Cdfs::new`, add two `TxbSet` + variants (`LumaRect32x16`, `ChromaRect16x8`) whose `txb()` match arms are + byte-identical to `Luma32`/`Chroma16` except `eob_pt` points at the new + fields. One variant each covers both orientations (32x16 and 16x32 share + one square-equivalent context; likewise 16x8/8x16) — no per-orientation + duplication needed. +3. `decode.rs`: four new small functions mirroring the existing square ones' + `TxClass::TwoD` branch only (refuse-by-name if a symbol somehow produces a + different class — should be geometrically impossible here since neither + size pair ever reads a `tx_type` symbol: luma's is forced `DctDct` by + `EXT_TX_SET_DCTONLY` at `tx_size_sqr_up == TX_32X32`, chroma is always + mode-derived via the existing `default_intra_tx_type`, spec + `av1_get_ext_tx_set_type`, `blockd.h:1097`): + - `nz_map_offset_2d(row, col, w, h)`, `base_ctx_rect`, `br_ctx_rect`, + `neighbour_rect` (the rect generalization above). + - `read_coeffs_rect(dec, coding, scan, w, h, skip_ctx, sign_ctx, + default_tx_type)`, a `(w, h)`-aware copy of `read_coeffs` restricted to + `TxClass::TwoD` (refuses by name otherwise). + - A rect `Neighbours::around_rect` (independent `w`/`h` extents, mirroring + the already-landed `record_rect`/`record_mi_rect` asymmetric-extent + pattern at `decode.rs:1378-1457` — `record_mi_rect` already handles real + coefficient grids correctly with no change needed, since a lone + non-split TU broadcasts one state uniformly across its own mi span + either way). + - `decode_block_rect` gains a `base_q_idx: u8` parameter (threaded from + the four already-in-scope call sites at `decode.rs:4341-4396`, where + `base_q_idx` is already a local), replaces the + `if !skip { return Err(...) }` block with real reads through + `read_coeffs_rect` + `transform::dequant_and_inverse_typed_wh` (already + landed by lane-recttx) for luma then chroma, and passes the real + residual (not `vec![0i32; ...]`) into the existing `reconstruct_rect` + calls and the real coefficient grid (not the zero placeholder) into + `record_rect`. + - A new `RECT_COEFF_HITS` atomic counter incremented once per real + non-skip decode, hard-asserted `> 0` in the existing free-partition gate + (`stream.rs:4060-4137`, which already tracks `rect_partition_hits` and + currently only ever sees the skip case reach decode) — no new gate file + needed, this lift makes the *existing* gate exercise more of the stream + it already decodes. + +## Why this stopped here instead of landing partial code + +Every individual piece above was cross-checked against real libaom source +(not memory, not the spec's prose) with byte-level verification where a +mistake would desync silently: the scan-table transpose was checked as a +bijection, the `EOB_PT_512_LUMA`/`_128_CHROMA` q2 rows follow the exact +`AOM_CDFn`-argument-direct convention this codebase already uses (verified +against `EOB_PT_1024_LUMA`'s known-good transcription), and the +`nz_map_offset_2d` rect branches are libaom's own comment, not a derivation. +But none of it has been compiled or run against a real aomenc stream, and +this decode path has no tolerance for "probably right" — the method this +lane was chartered under is explicit that a desync is never acceptable, only +a clean refusal is. Implementing all of the above plus threading +`base_q_idx` through four call sites plus writing/running the gate assertion +was more edit-and-verify surface than the remaining turn budget could safely +absorb, so nothing was committed rather than risk landing something wrong +this round left no room to catch. + +## Before/after refusal counts, firing count + +Unchanged this round: both named refusals (`decode.rs:1772-1776` intra +HORZ/VERT non-skip, `decode.rs:8697` inter-below-16x16) still fire exactly as +before. No gate firing count to report — nothing new decodes yet. diff --git a/lanes/rectwire-r3.report.md b/lanes/rectwire-r3.report.md new file mode 100644 index 00000000..c9008c25 --- /dev/null +++ b/lanes/rectwire-r3.report.md @@ -0,0 +1,112 @@ +# lane-rectwire round 3 + +VERDICT: reverted r2's real (non-skip) rect coefficient decode to refuse by +name -- the pixel-exact gate found a genuine desync (not a subtle context +bug), and this round's budget was exhausted before isolating its exact +symbol. No desync landed. Free-partition gate green, full lib suite +231/0. + +## What was confirmed a defect, and where it isn't + +`/tmp/claude-1000/rectwire-flake-1.obu` (seed 55, 64x64x24) reproduces +byte-identically from the pinned bytes across two runs (`scratch_decode_ +pinned_stream_once`, `OK: 24 frames` both times) -- this is a real decode +bug, not attempt-selection flake. + +`scratch_isolate_pinned_mismatch` on that stream: frame 0 luma has 1085/4096 +mismatches. The *first* one (row 28, col 31, delta 1) sits inside the +top-left 32x32 `PARTITION_NONE` intra block -- **not** a rect strip at all +(charter's step-3 question answered: no). But a quadrant breakdown shows +1015 of the 1085 luma mismatches (93.5%) land inside the one rect quadrant +this frame has: `mi(8,8)` reads `PARTITION_HORZ` (`TRACE partition_w32 +mi=(8,8) ctx=0 value=1`), splitting the bottom-right 32x32 into two true +32x16 strips. Sub-bucketing that quadrant: strip 0 (rows 32..48) is 503/512 +wrong, strip 1 (rows 48..64) is 512/512 wrong, both starting at their very +first pixel (strip 1's first pixel is 105 vs 72 -- a 33-level jump, not a +rounding-scale error). That magnitude and near-total coverage is a real +desync inside the rect coefficient path, not a context-table nudge; the +handful of TL/BL-quadrant mismatches are plausibly a small halo from +whatever downstream state the corrupted BR quadrant leaves (not itself +implicated). + +## Three suspects r2 named -- all checked and cleared, cross-referenced +against real libaom source this round, not memory: + +1. **`base_ctx_rect`'s 5-neighbour `TwoD` offset array.** Checked against + `get_nz_map_ctx_from_stats` / the `av1_nz_map_ctx_offset` comment in + `~/.cache/aom-oracle/src/av1/common/txb_common.h:189-224`: the 5 offsets + `(1,0),(0,1),(1,1),(2,0),(0,2)`, the `wh`/`col<2`→`+16` boundary branches, and the tx-size-independent + `row+col` fallback (verified `cdf::NZ_MAP_CTX_OFFSET_32` already encodes + exactly `row+col<2→1, row+col<4→6, else→21`) all match libaom + byte-for-byte. Not the bug. +2. **`u_skip_ctx`/`v_skip_ctx`'s `above + left` formula.** libaom's real + chroma rule (`get_txb_ctx_general`, same file, plane != 0 branch) is + `get_entropy_context(tx_size,a,l) + ctx_offset` where `ctx_offset` is 7 + for a lone TU (`plane_bsize == tx bsize`, our case) or 10 otherwise, and + `get_entropy_context` returns exactly `(above coded?)+(left coded?)` -- + i.e. the *same* 0..2 range our code computes. Cross-checked against the + **existing, tested** square `Chroma16` path (`read_plane`, + `decode.rs:2707`): it uses the identical `usize::from(around.0) + + usize::from(around.1)` formula with no `+7`, meaning the `txb_skip_ + chroma_16` default-CDF table was already transcribed with the `+7` + pre-baked into which 3 rows were picked -- consistent, not the bug. +3. **`dc_sign_ctx` reading the wrong `around` index.** `decode_block_rect` + indexes `around[0]` for luma, `around[1]` for u, `around[2]` for v -- + same convention the square path's `chroma_around[1]`/`[2]` uses. Not the + bug. + +Also checked `around_rect`/`record_rect`'s luma-mi-granularity span for +chroma (looked like a subsampling bug at first glance): both the write side +(`record_mi_rect`) and read side (`around_mi_rect`) use the same un-halved +`w_mi`/`h_mi` for all three planes, matching the square path's own +`around`/`record` convention exactly. Not a bug either. + +## Where the range-ladder bisection stalled + +Built the requested instrument check first: `~/.cache/aom-oracle/build/ +aomdec` with `EC_TRACE_COEFF=1`/`EC_TRACE_MODE=1` emits real per-block +`rng` -- confirmed live (`EC_PART`/`EC_COEFF`/`EC_MODE` all firing on this +stream). But the intra key-frame path in libaom's `decodeframe.c` has no +traced symbol between the partition read and the first `EC_COEFF` (mode/ +skip/tx_depth reads for an *intra* block are only traced by the new +`EC_TRACE_MODE` patch on the *inter* decodemv.c path, not here), so +`aomdec`'s post-partition `rng` and our own decoder's `rng` at coefficient +entry aren't directly comparable -- several untraced symbol reads (skip, +y_mode, uv_mode, tx_size_cat2 in `read_intra_mode_rect`) sit between them on +our side with no anchor on aomdec's. Extending `EC_TRACE_MODE`-style tracing +into libaom's intra mode-info reader (a second oracle patch) is the next +lane's first move, not attempted this round (budget). + +## The revert + +`decode.rs::decode_block_rect`'s non-skip branch now returns `Err(unsupported( +"a HORZ/VERT intra strip with real (non-skip) coefficients ..."))` before +any of r2's real-decode code runs (that code stays in the file, dead behind +the early return, so the next round doesn't re-derive it). The free-partition +gate's `RECT_COEFF_HITS` hard assert (r2) is downgraded back to a SOFT-NOTE +(`rect_coeff_hits` is now always 0 by construction) -- matches r1's original +posture. + +## Before/after refusal + firing counts + +This round, one gate run (`EC_AV1_REQUIRE_AOMENC=1`, n=40, seed 42+i): +- 29 named refusals, 11 pixel-exact matches, 0 mismatches (gate green). +- `rect_partition_hits=9` (real HORZ/VERT strips reached), `rect_coeff_hits=0` + (by construction now), `extended_partition_hits=0`, `partab_hits=2`. +- 3 of the 29 refusals are the new named one: "a HORZ/VERT intra strip with + real (non-skip) coefficients (lane-rectwire r3: confirmed desync, not yet + isolated)" (seeds 63, 67, 81) -- these are exactly the runs that would + previously have risked the r2 desync; they refuse cleanly now instead. + +Full `cargo test -p ec-av1 --release --lib`: 231 passed, 0 failed, 17 ignored. + +## Next round's first move + +Patch `~/.cache/aom-oracle`'s `decodeframe.c` intra mode-info reader +(`read_intra_frame_mode_info` or equivalent) with the same `rng`-emitting +pattern `EC_TRACE_MODE` already added for inter, so a same-length range +ladder can walk from the `mi(8,8)` `PARTITION_HORZ` read through skip/ +y_mode/uv_mode/tx_depth into the first `EC_COEFF` and find the exact +diverging symbol, rather than guessing between the coefficient-context +math (now cleared) and the prediction/transform math (untested this round).