forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-rectwire': rect coefficient infrastructure, decode still refused

Browse files at this commit

7e312c05a0d3324c607dde9ad8f5a055ad19c5bdAhmet Kaan GümüşParent: dd732323bebc9d
Three rounds, landing on a refusal rather than a decode, which is the correct
outcome for what was proven.

r1 did the recon and deliberately did not land code with the budget it had left
-- the right call for a coefficient-decode edit, where the failure mode is a
silent desync. Its recon narrowed the job sharply: decode_block_rect is called
from only four sites at fixed size pairs, get_txsize_entropy_ctx reduces to
txsize_sqr_up_map so most TxbSet fields reuse the square ones, and the only
genuinely new pieces are two eob CDF table sets.

r2 landed the wiring: those two CDF sets at all four sites, the four literal
scan tables (verified byte-for-byte, and confirmed NOT reproducible from the
square zigzag formula generalized -- the transcription was necessary, not
lazy), the rect context helpers, and a real non-skip decode path.

r3 triaged the mismatch r2 reported honestly rather than shipped. It is a real
deterministic desync, not an attempt-selection flake: 93.5% of the frame's
luma mismatches concentrate in the one PARTITION_HORZ quadrant, and both 32x16
strips are wrong from their first pixel. All three of r2's own suspects were
cleared against libaom source. So the non-skip branch refuses by name again and
the decode logic stays in the file, unreachable, for the round that isolates
the divergence. The gate's RECT_COEFF_HITS assert is downgraded to a note,
since it is now zero by construction -- a hard assert that cannot fire is the
vacuous-gate trap this batch already fixed once.

Full lib suite 231 passed / 0 failed; the free-partition gate is green with
zero mismatches.
crates/ec-av1/src/cdf.rsmodified+42 -0
@@ -224,6 +224,48 @@ pub const EOB_PT_1024_LUMA: [u16; 12] = [
224 /// coefficient is the DC.224 /// coefficient is the DC.
225 pub const COEFF_BASE_EOB_LUMA_64_DC: [u16; 4] = [935, 3382, 32768, 0];225 pub const COEFF_BASE_EOB_LUMA_64_DC: [u16; 4] = [935, 3382, 32768, 0];
226 226
227+/// `av1_default_eob_multi512_cdfs[2][0][0]` (`token_cdfs.h:829-906`,
228+/// lane-rectwire): the end-of-block group of a luma transform with 512 coded
229+/// positions -- what a true 32x16/16x32 rectangular transform has
230+/// (`txsize_log2_minus4` = 5, `common_data.h:346`), distinct from
231+/// [`EOB_PT_1024_LUMA`]'s 1024-position group a *square* 32x32 reads.
232+pub const EOB_PT_512_LUMA: [u16; 11] = [
233+ 2624, 3936, 6480, 9686, 13979, 17726, 23267, 28410, 31078, 32768, 0,
234+];
235+/// [`EOB_PT_512_LUMA`], q-context 0 (`base_q_idx` 0..=20).
236+pub const EOB_PT_512_LUMA_Q0: [u16; 11] = [
237+ 641, 983, 3707, 5430, 10234, 14958, 18788, 23412, 26061, 32768, 0,
238+];
239+/// [`EOB_PT_512_LUMA`], q-context 1 (`base_q_idx` 21..=60).
240+pub const EOB_PT_512_LUMA_Q1: [u16; 11] = [
241+ 1230, 2278, 5035, 7776, 11871, 15346, 19590, 24584, 28749, 32768, 0,
242+];
243+/// [`EOB_PT_512_LUMA`], q-context 3 (`base_q_idx` 121..=255).
244+pub const EOB_PT_512_LUMA_Q3: [u16; 11] = [
245+ 5927, 7809, 10923, 14597, 19439, 24135, 28456, 31142, 32060, 32768, 0,
246+];
247+
248+/// `av1_default_eob_multi128_cdfs[2][1][0]` (`token_cdfs.h:829-906`,
249+/// lane-rectwire): the end-of-block group of a chroma transform with 128
250+/// coded positions -- what a true 16x8/8x16 rectangular transform has
251+/// (`txsize_log2_minus4` = 3), distinct from [`EOB_PT_256_CHROMA`]'s
252+/// 256-position group a *square* 16x16 reads.
253+pub const EOB_PT_128_CHROMA: [u16; 9] = [
254+ 13627, 16246, 20173, 24429, 27948, 30415, 31863, 32768, 0,
255+];
256+/// [`EOB_PT_128_CHROMA`], q-context 0.
257+pub const EOB_PT_128_CHROMA_Q0: [u16; 9] = [
258+ 5245, 7456, 12880, 15852, 20033, 23932, 27608, 32768, 0,
259+];
260+/// [`EOB_PT_128_CHROMA`], q-context 1.
261+pub const EOB_PT_128_CHROMA_Q1: [u16; 9] = [
262+ 8045, 11200, 15497, 19595, 23948, 27408, 30938, 32768, 0,
263+];
264+/// [`EOB_PT_128_CHROMA`], q-context 3.
265+pub const EOB_PT_128_CHROMA_Q3: [u16; 9] = [
266+ 24313, 26062, 28385, 30107, 31217, 31898, 32345, 32768, 0,
267+];
268+
227 /// `Default_Dc_Sign_Cdf[2][0]` (spec 9.4): the sign of a luma DC coefficient,269 /// `Default_Dc_Sign_Cdf[2][0]` (spec 9.4): the sign of a luma DC coefficient,
228 /// indexed by whether the neighbouring blocks' DC signs lean negative (1),270 /// indexed by whether the neighbouring blocks' DC signs lean negative (1),
229 /// positive (2) or neither (0).271 /// positive (2) or neither (0).
crates/ec-av1/src/cdf_state.rsmodified+60 -0
@@ -90,6 +90,20 @@ pub(crate) enum TxbSet {
90 Chroma16,90 Chroma16,
91 /// The 32x32 transform of a chroma plane of a whole superblock.91 /// The 32x32 transform of a chroma plane of a whole superblock.
92 Chroma32,92 Chroma32,
93+ /// The luma 32x16/16x32 transform of a `PARTITION_HORZ`/`VERT` intra
94+ /// strip (lane-rectwire): `get_txsize_entropy_ctx` reduces any 2:1 rect
95+ /// size to its square-up equivalent (`entropy.h:172`), so this shares
96+ /// every table [`Luma32`](TxbSet::Luma32) does except `eob_pt` -- a true
97+ /// 512-position transform reads [`crate::cdf::EOB_PT_512_LUMA`], not
98+ /// `Luma32`'s 1024-position one. Never carries a `tx_type` symbol: both
99+ /// orientations use one variant (`get_tx_set` is `EXT_TX_SET_DCTONLY` at
100+ /// `tx_size_sqr_up == TX_32X32` regardless of which axis is longer).
101+ LumaRect32x16,
102+ /// The chroma 16x8/8x16 transform under the same strip (lane-rectwire):
103+ /// [`Chroma16`](TxbSet::Chroma16)'s tables except `eob_pt`, which reads
104+ /// the true 128-position [`crate::cdf::EOB_PT_128_CHROMA`] instead of
105+ /// `Chroma16`'s 256-position one.
106+ ChromaRect16x8,
93 }107 }
94 108
95 /// The tables one transform block is coded with, borrowed from the state for109 /// The tables one transform block is coded with, borrowed from the state for
@@ -233,6 +247,12 @@ pub(crate) struct Cdfs {
233 pub eob_pt_1024_chroma: [u16; 12],247 pub eob_pt_1024_chroma: [u16; 12],
234 /// The end-of-block group of a 16x16 chroma transform.248 /// The end-of-block group of a 16x16 chroma transform.
235 pub eob_pt_256_chroma: [u16; 10],249 pub eob_pt_256_chroma: [u16; 10],
250+ /// The end-of-block group of a true 32x16/16x32 rect luma transform
251+ /// (lane-rectwire), see [`TxbSet::LumaRect32x16`].
252+ pub eob_pt_512_luma: [u16; 11],
253+ /// The end-of-block group of a true 16x8/8x16 rect chroma transform
254+ /// (lane-rectwire), see [`TxbSet::ChromaRect16x8`].
255+ pub eob_pt_128_chroma: [u16; 9],
236 /// The end-of-block offset bit, per table set.256 /// The end-of-block offset bit, per table set.
237 pub eob_extra_luma_32: [[u16; 3]; 9],257 pub eob_extra_luma_32: [[u16; 3]; 9],
238 /// The same, for a 64x64 luma transform.258 /// The same, for a 64x64 luma transform.
@@ -560,6 +580,8 @@ impl Cdfs {
560 reset1(&mut self.eob_pt_1024_luma);580 reset1(&mut self.eob_pt_1024_luma);
561 reset1(&mut self.eob_pt_1024_chroma);581 reset1(&mut self.eob_pt_1024_chroma);
562 reset1(&mut self.eob_pt_256_chroma);582 reset1(&mut self.eob_pt_256_chroma);
583+ reset1(&mut self.eob_pt_512_luma);
584+ reset1(&mut self.eob_pt_128_chroma);
563 reset2(&mut self.eob_extra_luma_32);585 reset2(&mut self.eob_extra_luma_32);
564 reset2(&mut self.eob_extra_luma_64);586 reset2(&mut self.eob_extra_luma_64);
565 reset2(&mut self.eob_extra_chroma_16);587 reset2(&mut self.eob_extra_chroma_16);
@@ -924,6 +946,20 @@ impl Cdfs {
924 cdf::EOB_PT_256_CHROMA,946 cdf::EOB_PT_256_CHROMA,
925 cdf::EOB_PT_256_CHROMA_Q3,947 cdf::EOB_PT_256_CHROMA_Q3,
926 ),948 ),
949+ eob_pt_512_luma: pick(
950+ q_ctx,
951+ cdf::EOB_PT_512_LUMA_Q0,
952+ cdf::EOB_PT_512_LUMA_Q1,
953+ cdf::EOB_PT_512_LUMA,
954+ cdf::EOB_PT_512_LUMA_Q3,
955+ ),
956+ eob_pt_128_chroma: pick(
957+ q_ctx,
958+ cdf::EOB_PT_128_CHROMA_Q0,
959+ cdf::EOB_PT_128_CHROMA_Q1,
960+ cdf::EOB_PT_128_CHROMA,
961+ cdf::EOB_PT_128_CHROMA_Q3,
962+ ),
927 eob_extra_luma_32: pick(963 eob_extra_luma_32: pick(
928 q_ctx,964 q_ctx,
929 cdf::EOB_EXTRA_LUMA_32_Q0,965 cdf::EOB_EXTRA_LUMA_32_Q0,
@@ -1288,6 +1324,30 @@ impl Cdfs {
1288 tx_type: None,1324 tx_type: None,
1289 eob_pt_class1: None,1325 eob_pt_class1: None,
1290 },1326 },
1327+ TxbSet::LumaRect32x16 => TxbTables {
1328+ side: 32,
1329+ txb_skip: &mut self.txb_skip_luma_32,
1330+ eob_pt: &mut self.eob_pt_512_luma,
1331+ eob_extra: &mut self.eob_extra_luma_32,
1332+ base: &mut self.base_luma_32,
1333+ base_eob: &mut self.base_eob_luma_32,
1334+ br: &mut self.br_luma_32,
1335+ dc_sign: &mut self.dc_sign_luma,
1336+ tx_type: None,
1337+ eob_pt_class1: None,
1338+ },
1339+ TxbSet::ChromaRect16x8 => TxbTables {
1340+ side: 16,
1341+ txb_skip: &mut self.txb_skip_chroma_16,
1342+ eob_pt: &mut self.eob_pt_128_chroma,
1343+ eob_extra: &mut self.eob_extra_chroma_16,
1344+ base: &mut self.base_chroma_16,
1345+ base_eob: &mut self.base_eob_chroma_16,
1346+ br: &mut self.br_chroma_16,
1347+ dc_sign: &mut self.dc_sign_chroma,
1348+ tx_type: None,
1349+ eob_pt_class1: None,
1350+ },
1291 }1351 }
1292 }1352 }
1293 }1353 }
crates/ec-av1/src/decode.rsmodified+454 -56
@@ -38,7 +38,7 @@ use crate::mvstack::{
38 single_ref_p6_ctx, uni_comp_ref_p1_ctx,38 single_ref_p6_ctx, uni_comp_ref_p1_ctx,
39 };39 };
40 use crate::tile::{INTRA_MODE_CTX, block_grid, has_half};40 use crate::tile::{INTRA_MODE_CTX, block_grid, has_half};
41-use crate::transform::{TxType, dequant_and_inverse_typed};41+use crate::transform::{TxType, dequant_and_inverse_typed, dequant_and_inverse_typed_wh};
42 42
43 const PARTITION_NONE: usize = 0;43 const PARTITION_NONE: usize = 0;
44 const PARTITION_HORZ: usize = 1;44 const PARTITION_HORZ: usize = 1;
@@ -272,6 +272,17 @@ pub(crate) fn rect_partition_hits() -> usize {
272 RECT_PARTITION_HITS.load(std::sync::atomic::Ordering::Relaxed)272 RECT_PARTITION_HITS.load(std::sync::atomic::Ordering::Relaxed)
273 }273 }
274 274
275+/// lane-rectwire r2: how many `PARTITION_HORZ`/`VERT` strips actually decoded
276+/// real (non-skip) coefficients through [`read_coeffs_rect`] -- proves the
277+/// rect coefficient reader itself fired, not just the strip-level partition
278+/// symbol [`RECT_PARTITION_HITS`] already counts.
279+static RECT_COEFF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
280+
281+/// Current value of [`RECT_COEFF_HITS`].
282+pub(crate) fn rect_coeff_hits() -> usize {
283+ RECT_COEFF_HITS.load(std::sync::atomic::Ordering::Relaxed)
284+}
285+
275 /// lane-partab r1: how many 32x32 quadrants decoded an AB partition286 /// lane-partab r1: how many 32x32 quadrants decoded an AB partition
276 /// (PARTITION_HORZ_A / VERT_A / VERT_B -- two 16x16 squares plus one287 /// (PARTITION_HORZ_A / VERT_A / VERT_B -- two 16x16 squares plus one
277 /// 16x32/32x16 strip). Like [`RECT_PARTITION_HITS`], this is the288 /// 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) ->
616 if near_origin { mag + 7 } else { mag + 14 }627 if near_origin { mag + 7 } else { mag + 14 }
617 }628 }
618 629
630+/// `Default_Scan_32x16` (spec 8.4.2, libaom `scan.c`'s `default_scan_32x16`):
631+/// transposed from libaom's column-major buffer into this decoder's row-major
632+/// `pos = row * 32 + col` (lane-rectwire, verified as a bijection of `0..512`
633+/// against the real table). Not the square zigzag formula generalized to
634+/// unequal `w`/`h` -- checked and that does NOT reproduce this table.
635+const SCAN_32X16: [u16; 512] = [
636+ 0, 32, 1, 64, 33, 2, 96, 65, 34, 3, 128, 97, 66, 35, 4, 160, 129, 98, 67, 36, 5, 192, 161,
637+ 130, 99, 68, 37, 6, 224, 193, 162, 131, 100, 69, 38, 7, 256, 225, 194, 163, 132, 101, 70, 39,
638+ 8, 288, 257, 226, 195, 164, 133, 102, 71, 40, 9, 320, 289, 258, 227, 196, 165, 134, 103, 72,
639+ 41, 10, 352, 321, 290, 259, 228, 197, 166, 135, 104, 73, 42, 11, 384, 353, 322, 291, 260, 229,
640+ 198, 167, 136, 105, 74, 43, 12, 416, 385, 354, 323, 292, 261, 230, 199, 168, 137, 106, 75, 44,
641+ 13, 448, 417, 386, 355, 324, 293, 262, 231, 200, 169, 138, 107, 76, 45, 14, 480, 449, 418,
642+ 387, 356, 325, 294, 263, 232, 201, 170, 139, 108, 77, 46, 15, 481, 450, 419, 388, 357, 326,
643+ 295, 264, 233, 202, 171, 140, 109, 78, 47, 16, 482, 451, 420, 389, 358, 327, 296, 265, 234,
644+ 203, 172, 141, 110, 79, 48, 17, 483, 452, 421, 390, 359, 328, 297, 266, 235, 204, 173, 142,
645+ 111, 80, 49, 18, 484, 453, 422, 391, 360, 329, 298, 267, 236, 205, 174, 143, 112, 81, 50, 19,
646+ 485, 454, 423, 392, 361, 330, 299, 268, 237, 206, 175, 144, 113, 82, 51, 20, 486, 455, 424,
647+ 393, 362, 331, 300, 269, 238, 207, 176, 145, 114, 83, 52, 21, 487, 456, 425, 394, 363, 332,
648+ 301, 270, 239, 208, 177, 146, 115, 84, 53, 22, 488, 457, 426, 395, 364, 333, 302, 271, 240,
649+ 209, 178, 147, 116, 85, 54, 23, 489, 458, 427, 396, 365, 334, 303, 272, 241, 210, 179, 148,
650+ 117, 86, 55, 24, 490, 459, 428, 397, 366, 335, 304, 273, 242, 211, 180, 149, 118, 87, 56, 25,
651+ 491, 460, 429, 398, 367, 336, 305, 274, 243, 212, 181, 150, 119, 88, 57, 26, 492, 461, 430,
652+ 399, 368, 337, 306, 275, 244, 213, 182, 151, 120, 89, 58, 27, 493, 462, 431, 400, 369, 338,
653+ 307, 276, 245, 214, 183, 152, 121, 90, 59, 28, 494, 463, 432, 401, 370, 339, 308, 277, 246,
654+ 215, 184, 153, 122, 91, 60, 29, 495, 464, 433, 402, 371, 340, 309, 278, 247, 216, 185, 154,
655+ 123, 92, 61, 30, 496, 465, 434, 403, 372, 341, 310, 279, 248, 217, 186, 155, 124, 93, 62, 31,
656+ 497, 466, 435, 404, 373, 342, 311, 280, 249, 218, 187, 156, 125, 94, 63, 498, 467, 436, 405,
657+ 374, 343, 312, 281, 250, 219, 188, 157, 126, 95, 499, 468, 437, 406, 375, 344, 313, 282, 251,
658+ 220, 189, 158, 127, 500, 469, 438, 407, 376, 345, 314, 283, 252, 221, 190, 159, 501, 470, 439,
659+ 408, 377, 346, 315, 284, 253, 222, 191, 502, 471, 440, 409, 378, 347, 316, 285, 254, 223, 503,
660+ 472, 441, 410, 379, 348, 317, 286, 255, 504, 473, 442, 411, 380, 349, 318, 287, 505, 474, 443,
661+ 412, 381, 350, 319, 506, 475, 444, 413, 382, 351, 507, 476, 445, 414, 383, 508, 477, 446, 415,
662+ 509, 478, 447, 510, 479, 511,
663+];
664+
665+/// `Default_Scan_16x32`, this decoder's own row-major transcription (see
666+/// [`SCAN_32X16`]'s doc comment).
667+const SCAN_16X32: [u16; 512] = [
668+ 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,
669+ 66, 81, 96, 7, 22, 37, 52, 67, 82, 97, 112, 8, 23, 38, 53, 68, 83, 98, 113, 128, 9, 24, 39, 54,
670+ 69, 84, 99, 114, 129, 144, 10, 25, 40, 55, 70, 85, 100, 115, 130, 145, 160, 11, 26, 41, 56, 71,
671+ 86, 101, 116, 131, 146, 161, 176, 12, 27, 42, 57, 72, 87, 102, 117, 132, 147, 162, 177, 192,
672+ 13, 28, 43, 58, 73, 88, 103, 118, 133, 148, 163, 178, 193, 208, 14, 29, 44, 59, 74, 89, 104,
673+ 119, 134, 149, 164, 179, 194, 209, 224, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180,
674+ 195, 210, 225, 240, 31, 46, 61, 76, 91, 106, 121, 136, 151, 166, 181, 196, 211, 226, 241, 256,
675+ 47, 62, 77, 92, 107, 122, 137, 152, 167, 182, 197, 212, 227, 242, 257, 272, 63, 78, 93, 108,
676+ 123, 138, 153, 168, 183, 198, 213, 228, 243, 258, 273, 288, 79, 94, 109, 124, 139, 154, 169,
677+ 184, 199, 214, 229, 244, 259, 274, 289, 304, 95, 110, 125, 140, 155, 170, 185, 200, 215, 230,
678+ 245, 260, 275, 290, 305, 320, 111, 126, 141, 156, 171, 186, 201, 216, 231, 246, 261, 276, 291,
679+ 306, 321, 336, 127, 142, 157, 172, 187, 202, 217, 232, 247, 262, 277, 292, 307, 322, 337, 352,
680+ 143, 158, 173, 188, 203, 218, 233, 248, 263, 278, 293, 308, 323, 338, 353, 368, 159, 174, 189,
681+ 204, 219, 234, 249, 264, 279, 294, 309, 324, 339, 354, 369, 384, 175, 190, 205, 220, 235, 250,
682+ 265, 280, 295, 310, 325, 340, 355, 370, 385, 400, 191, 206, 221, 236, 251, 266, 281, 296, 311,
683+ 326, 341, 356, 371, 386, 401, 416, 207, 222, 237, 252, 267, 282, 297, 312, 327, 342, 357, 372,
684+ 387, 402, 417, 432, 223, 238, 253, 268, 283, 298, 313, 328, 343, 358, 373, 388, 403, 418, 433,
685+ 448, 239, 254, 269, 284, 299, 314, 329, 344, 359, 374, 389, 404, 419, 434, 449, 464, 255, 270,
686+ 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 271, 286, 301, 316, 331,
687+ 346, 361, 376, 391, 406, 421, 436, 451, 466, 481, 496, 287, 302, 317, 332, 347, 362, 377, 392,
688+ 407, 422, 437, 452, 467, 482, 497, 303, 318, 333, 348, 363, 378, 393, 408, 423, 438, 453, 468,
689+ 483, 498, 319, 334, 349, 364, 379, 394, 409, 424, 439, 454, 469, 484, 499, 335, 350, 365, 380,
690+ 395, 410, 425, 440, 455, 470, 485, 500, 351, 366, 381, 396, 411, 426, 441, 456, 471, 486, 501,
691+ 367, 382, 397, 412, 427, 442, 457, 472, 487, 502, 383, 398, 413, 428, 443, 458, 473, 488, 503,
692+ 399, 414, 429, 444, 459, 474, 489, 504, 415, 430, 445, 460, 475, 490, 505, 431, 446, 461, 476,
693+ 491, 506, 447, 462, 477, 492, 507, 463, 478, 493, 508, 479, 494, 509, 495, 510, 511,
694+];
695+
696+/// `Default_Scan_16x8`, this decoder's own row-major transcription (see
697+/// [`SCAN_32X16`]'s doc comment).
698+const SCAN_16X8: [u16; 128] = [
699+ 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,
700+ 36, 21, 6, 112, 97, 82, 67, 52, 37, 22, 7, 113, 98, 83, 68, 53, 38, 23, 8, 114, 99, 84, 69, 54,
701+ 39, 24, 9, 115, 100, 85, 70, 55, 40, 25, 10, 116, 101, 86, 71, 56, 41, 26, 11, 117, 102, 87,
702+ 72, 57, 42, 27, 12, 118, 103, 88, 73, 58, 43, 28, 13, 119, 104, 89, 74, 59, 44, 29, 14, 120,
703+ 105, 90, 75, 60, 45, 30, 15, 121, 106, 91, 76, 61, 46, 31, 122, 107, 92, 77, 62, 47, 123, 108,
704+ 93, 78, 63, 124, 109, 94, 79, 125, 110, 95, 126, 111, 127,
705+];
706+
707+/// `Default_Scan_8x16`, this decoder's own row-major transcription (see
708+/// [`SCAN_32X16`]'s doc comment).
709+const SCAN_8X16: [u16; 128] = [
710+ 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,
711+ 41, 48, 7, 14, 21, 28, 35, 42, 49, 56, 15, 22, 29, 36, 43, 50, 57, 64, 23, 30, 37, 44, 51, 58,
712+ 65, 72, 31, 38, 45, 52, 59, 66, 73, 80, 39, 46, 53, 60, 67, 74, 81, 88, 47, 54, 61, 68, 75, 82,
713+ 89, 96, 55, 62, 69, 76, 83, 90, 97, 104, 63, 70, 77, 84, 91, 98, 105, 112, 71, 78, 85, 92, 99,
714+ 106, 113, 120, 79, 86, 93, 100, 107, 114, 121, 87, 94, 101, 108, 115, 122, 95, 102, 109, 116,
715+ 123, 103, 110, 117, 124, 111, 118, 125, 119, 126, 127,
716+];
717+
718+/// [`neighbour`] with independent `w` (row stride)/`h` (row-bound) extents
719+/// (lane-rectwire, mirrors [`record_rect`](Neighbours::record_rect)'s
720+/// asymmetric-extent pattern).
721+fn neighbour_rect(grid: &[i32], w: usize, h: usize, row: usize, col: usize) -> i32 {
722+ if row >= h || col >= w {
723+ 0
724+ } else {
725+ grid[row * w + col]
726+ }
727+}
728+
729+/// [`base_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire):
730+/// the rect generalization is libaom's own comment in
731+/// `get_nz_map_ctx_from_stats` (`txb_common.h:189-224`), reduces to the exact
732+/// same [`cdf::NZ_MAP_CTX_OFFSET_32`] table when `w == h`. Never called for
733+/// `V_DCT`/`H_DCT` -- geometrically impossible at the two size pairs
734+/// [`decode_block_rect`] codes (see that function's own doc comment).
735+fn base_ctx_rect(grid: &[i32], w: usize, h: usize, row: usize, col: usize) -> usize {
736+ if row == 0 && col == 0 {
737+ return 0;
738+ }
739+ const OFFSETS: [(usize, usize); 5] = [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)];
740+ let mag: i32 = OFFSETS
741+ .iter()
742+ .map(|&(dr, dc)| neighbour_rect(grid, w, h, row + dr, col + dc).abs().min(3))
743+ .sum();
744+ let ctx = ((mag + 1) >> 1).min(4) as usize;
745+ if w < h && row < 2 {
746+ return ctx + 11;
747+ }
748+ if w > h && col < 2 {
749+ return ctx + 16;
750+ }
751+ ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize
752+}
753+
754+/// [`br_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): the
755+/// neighbour-offset math itself is shape-independent, only the boundary
756+/// clamp needs the real bound in each axis.
757+fn br_ctx_rect(grid: &[i32], w: usize, h: usize, row: usize, col: usize) -> usize {
758+ let extra = neighbour_rect(grid, w, h, row + 1, col + 1).abs();
759+ let mag = neighbour_rect(grid, w, h, row + 1, col).abs()
760+ + neighbour_rect(grid, w, h, row, col + 1).abs()
761+ + extra;
762+ let mag = (((mag + 1) >> 1).min(6)) as usize;
763+ if row == 0 && col == 0 {
764+ return mag;
765+ }
766+ if row < 2 && col < 2 { mag + 7 } else { mag + 14 }
767+}
768+
619 fn dc_vote(dc: Option<bool>) -> i32 {769 fn dc_vote(dc: Option<bool>) -> i32 {
620 match dc {770 match dc {
621 None => 0,771 None => 0,
@@ -882,6 +1032,89 @@ fn read_coeffs(
882 Ok((grid, tx_type))1032 Ok((grid, tx_type))
883 }1033 }
884 1034
1035+/// [`read_coeffs`] widened to `(w, h)` (lane-rectwire), restricted to
1036+/// `TxClass::TwoD` -- the only class either rect size pair this decoder codes
1037+/// can ever produce (see [`decode_block_rect`]'s doc comment): refuses by
1038+/// name rather than guess-decode if a `tx_type` symbol or non-2D class ever
1039+/// shows up here, since neither `base_ctx_rect`/`br_ctx_rect` nor
1040+/// [`class_scan_table`] have a rect form for those.
1041+fn read_coeffs_rect(
1042+ dec: &mut SymbolDecoder,
1043+ coding: &mut TxbTables,
1044+ scan: &[u16],
1045+ w: usize,
1046+ h: usize,
1047+ skip_ctx: usize,
1048+ sign_ctx: usize,
1049+ default_tx_type: TxType,
1050+) -> Result<(Vec<i32>, TxType)> {
1051+ let mut grid = vec![0i32; w * h];
1052+ let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1;
1053+ if all_zero {
1054+ return Ok((grid, TxType::DctDct));
1055+ }
1056+ if coding.tx_type.is_some() {
1057+ return Err(unsupported(
1058+ "a tx_type symbol on a rectangular transform (never expected at this size)",
1059+ ));
1060+ }
1061+ let tx_type = default_tx_type;
1062+ if TxClass::of(tx_type) != TxClass::TwoD {
1063+ return Err(unsupported(
1064+ "a non-2D tx class on a rectangular transform (never expected at this size)",
1065+ ));
1066+ }
1067+ let eob = read_eob(dec, coding, TxClass::TwoD);
1068+ let mut levels = vec![0i32; w * h];
1069+ for scan_idx in (0..eob).rev() {
1070+ let pos = scan[scan_idx] as usize;
1071+ let (row, col) = (pos / w, pos % w);
1072+ let level = if scan_idx == eob - 1 {
1073+ let ctx = eob_coeff_ctx(scan_idx, w * h);
1074+ dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1
1075+ } else {
1076+ let ctx = base_ctx_rect(&levels, w, h, row, col);
1077+ dec.symbol(&mut coding.base[ctx]) as i32
1078+ };
1079+ let level = if level > NUM_BASE_LEVELS {
1080+ let ctx = br_ctx_rect(&levels, w, h, row, col);
1081+ let mut level = level;
1082+ let mut sent = 0;
1083+ loop {
1084+ let k = dec.symbol(&mut coding.br[ctx]) as i32;
1085+ level += k;
1086+ sent += BR_STEP;
1087+ if k < BR_STEP || sent >= COEFF_BASE_RANGE {
1088+ break;
1089+ }
1090+ }
1091+ level
1092+ } else {
1093+ level
1094+ };
1095+ levels[pos] = level;
1096+ }
1097+ for &pos in &scan[..eob] {
1098+ let level = levels[pos as usize];
1099+ if level == 0 {
1100+ continue;
1101+ }
1102+ let negative = if pos == 0 {
1103+ dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1
1104+ } else {
1105+ dec.literal(1) == 1
1106+ };
1107+ let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL {
1108+ let g = read_golomb(dec)?;
1109+ level + g as i32
1110+ } else {
1111+ level
1112+ };
1113+ grid[pos as usize] = if negative { -level } else { level };
1114+ }
1115+ Ok((grid, tx_type))
1116+}
1117+
885 /// What one coded block leaves behind for the blocks that read it as a1118 /// What one coded block leaves behind for the blocks that read it as a
886 /// neighbour: whether it coded anything at all, and the sign of its DC —1119 /// neighbour: whether it coded anything at all, and the sign of its DC —
887 /// [`crate::tile`]'s own private `Neighbour`.1120 /// [`crate::tile`]'s own private `Neighbour`.
@@ -1336,6 +1569,35 @@ impl Neighbours {
1336 })1569 })
1337 }1570 }
1338 1571
1572+ /// [`Self::around`] with independent above (`w`)/left (`h`) extents, in
1573+ /// pixels -- lane-rectwire, mirrors [`Self::record_rect`]'s asymmetric
1574+ /// extent pattern for a true rectangular strip's own coefficient context.
1575+ fn around_rect(&self, (r, c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] {
1576+ self.around_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h)
1577+ }
1578+
1579+ /// [`Self::around_rect`] taking the block's position directly in 4x4
1580+ /// mode-info units, mirroring [`Self::around_mi`].
1581+ fn around_mi_rect(&self, (mi_r, mi_c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] {
1582+ let (w_mi, h_mi) = (w / MI, h / MI);
1583+ std::array::from_fn(|plane| {
1584+ let mut above_coded = false;
1585+ let mut left_coded = false;
1586+ let mut vote = 0;
1587+ for cell in 0..w_mi {
1588+ let above = &self.above[mi_c + cell][plane];
1589+ above_coded |= above.level != 0;
1590+ vote += dc_vote(above.dc);
1591+ }
1592+ for cell in 0..h_mi {
1593+ let left = &self.left[mi_r + cell][plane];
1594+ left_coded |= left.level != 0;
1595+ vote += dc_vote(left.dc);
1596+ }
1597+ (above_coded, left_coded, vote)
1598+ })
1599+ }
1600+
1339 /// The luma `txb_skip_ctx` of a transform unit smaller than its own block1601 /// The luma `txb_skip_ctx` of a transform unit smaller than its own block
1340 /// (spec `get_txb_ctx_general`, plane 0, `plane_bsize != tx_size`): the1602 /// (spec `get_txb_ctx_general`, plane 0, `plane_bsize != tx_size`): the
1341 /// above/left neighbours' magnitude tiers, OR-reduced over the unit's own1603 /// above/left neighbours' magnitude tiers, OR-reduced over the unit's own
@@ -1722,6 +1984,7 @@ fn decode_block_rect(
1722 v: &mut PlaneBuf,1984 v: &mut PlaneBuf,
1723 enable_filter_intra: bool,1985 enable_filter_intra: bool,
1724 allow_screen_content_tools: bool,1986 allow_screen_content_tools: bool,
1987+ base_q_idx: u8,
1725 ) -> Result<()> {1988 ) -> Result<()> {
1726 let (r, c) = at;1989 let (r, c) = at;
1727 let (px, py) = (c * SUB, r * SUB);1990 let (px, py) = (c * SUB, r * SUB);
@@ -1769,64 +2032,195 @@ fn decode_block_rect(
1769 ));2032 ));
1770 }2033 }
1771 let (tx_w, tx_h) = (bw, bh);2034 let (tx_w, tx_h) = (bw, bh);
1772- if !skip {
1773- return Err(unsupported(
1774- "a non-skip HORZ/VERT intra strip needs a rectangular transform \
1775- this decoder does not code yet",
1776- ));
1777- }
1778 let (cpx, cpy) = (px / 2, py / 2);2035 let (cpx, cpy) = (px / 2, py / 2);
1779 let (chroma_w, chroma_h) = (bw / 2, bh / 2);2036 let (chroma_w, chroma_h) = (bw / 2, bh / 2);
1780 let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height);2037 let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height);
1781- y.reconstruct_rect(2038+ if skip {
1782- px,2039+ y.reconstruct_rect(
1783- py,2040+ px,
1784- bw,2041+ py,
1785- bh,2042+ bw,
1786- mode,2043+ bh,
1787- angle_delta_y,2044+ mode,
1788- reach,2045+ angle_delta_y,
1789- &vec![0i32; bw * bh],2046+ reach,
1790- None,2047+ &vec![0i32; bw * bh],
1791- filter_intra,2048+ None,
1792- );2049+ filter_intra,
1793- let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh));2050+ );
1794- u.reconstruct_rect(2051+ let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh));
1795- cpx,2052+ u.reconstruct_rect(
1796- cpy,2053+ cpx,
1797- chroma_w,2054+ cpy,
1798- chroma_h,2055+ chroma_w,
1799- uv_predict_mode,2056+ chroma_h,
1800- angle_delta_uv,2057+ uv_predict_mode,
1801- reach,2058+ angle_delta_uv,
1802- &vec![0i32; chroma_w * chroma_h],2059+ reach,
1803- alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),2060+ &vec![0i32; chroma_w * chroma_h],
1804- None,2061+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
1805- );2062+ None,
1806- v.reconstruct_rect(2063+ );
1807- cpx,2064+ v.reconstruct_rect(
1808- cpy,2065+ cpx,
1809- chroma_w,2066+ cpy,
1810- chroma_h,2067+ chroma_w,
1811- uv_predict_mode,2068+ chroma_h,
1812- angle_delta_uv,2069+ uv_predict_mode,
1813- reach,2070+ angle_delta_uv,
1814- &vec![0i32; chroma_w * chroma_h],2071+ reach,
1815- alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),2072+ &vec![0i32; chroma_w * chroma_h],
1816- None,2073+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
1817- );2074+ None,
1818- neighbours.record_rect(2075+ );
1819- at,2076+ neighbours.record_rect(
1820- bw,2077+ at,
1821- bh,2078+ bw,
1822- mode,2079+ bh,
1823- &[2080+ mode,
1824- vec![0i32; bw * bh],2081+ &[
1825- vec![0i32; chroma_w * chroma_h],2082+ vec![0i32; bw * bh],
1826- vec![0i32; chroma_w * chroma_h],2083+ vec![0i32; chroma_w * chroma_h],
1827- ],2084+ vec![0i32; chroma_w * chroma_h],
1828- );2085+ ],
1829- neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, true);2086+ );
2087+ } else {
2088+ // lane-rectwire r2: real coefficients. `get_txsize_entropy_ctx`
2089+ // reduces both size pairs to their square-up CDF sets
2090+ // (`TxbSet::LumaRect32x16`/`ChromaRect16x8`, see those variants' own
2091+ // doc comments); the scan order and 2D context tables are the real
2092+ // rect ones (`SCAN_32X16`/etc, `base_ctx_rect`/`br_ctx_rect`).
2093+ let (luma_scan, chroma_scan): (&[u16], &[u16]) = if bw == 32 {
2094+ (&SCAN_32X16, &SCAN_16X8)
2095+ } else {
2096+ (&SCAN_16X32, &SCAN_8X16)
2097+ };
2098+ // lane-rectwire r3: real coefficients desync a pixel-exact gate
2099+ // (rectwire-flake-1.obu, seed 55) -- both 32x16 strips of frame 0's
2100+ // mi(8,8) HORZ quadrant come out ~100% wrong from their very first
2101+ // pixel (not a subtle context-table off-by-one), while the sibling
2102+ // non-rect quadrants of the same frame match. Range-ladder bisection
2103+ // against a real aomdec (EC_TRACE_COEFF/EC_TRACE_MODE) was started
2104+ // but the intra key-frame path has no traced symbol between the
2105+ // partition read and the first coefficient block (mode/skip/tx_depth
2106+ // reads are all untraced in libaom's decodeframe.c intra path, unlike
2107+ // the inter EC_TRACE_MODE this round added), so a same-length range
2108+ // comparison wasn't reachable within budget -- see
2109+ // lanes/rectwire-r3.report.md for what was ruled out (base_ctx_rect's
2110+ // 5-neighbour offsets, u/v_skip_ctx's above+left formula, and
2111+ // dc_sign_ctx's around-index all cross-checked byte-for-byte against
2112+ // libaom and match). Refusing by name again (r1 behavior) rather than
2113+ // risk landing the desync.
2114+ return Err(unsupported(
2115+ "a HORZ/VERT intra strip with real (non-skip) coefficients \
2116+ (lane-rectwire r3: confirmed desync, not yet isolated)",
2117+ ));
2118+ #[allow(unreachable_code)]
2119+ {
2120+ let around = neighbours.around_rect(at, bw, bh);
2121+ let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x16, mode);
2122+ let (luma_levels, luma_tx_type) = read_coeffs_rect(
2123+ dec,
2124+ &mut luma_coding,
2125+ luma_scan,
2126+ bw,
2127+ bh,
2128+ 0,
2129+ dc_sign_ctx(around[0].2),
2130+ TxType::DctDct,
2131+ )?;
2132+ let luma_residual = dequant_and_inverse_typed_wh(
2133+ &luma_levels,
2134+ bw,
2135+ bh,
2136+ 8,
2137+ i32::from(base_q_idx),
2138+ luma_tx_type,
2139+ );
2140+ y.reconstruct_rect(
2141+ px,
2142+ py,
2143+ bw,
2144+ bh,
2145+ mode,
2146+ angle_delta_y,
2147+ reach,
2148+ &luma_residual,
2149+ None,
2150+ filter_intra,
2151+ );
2152+ let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh));
2153+ let u_default_tx = default_intra_tx_type(uv_predict_mode as u8);
2154+ let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1);
2155+ let mut u_coding = cdfs.txb(TxbSet::ChromaRect16x8, uv_predict_mode);
2156+ let (u_levels, u_tx_type) = read_coeffs_rect(
2157+ dec,
2158+ &mut u_coding,
2159+ chroma_scan,
2160+ chroma_w,
2161+ chroma_h,
2162+ u_skip_ctx,
2163+ dc_sign_ctx(around[1].2),
2164+ u_default_tx,
2165+ )?;
2166+ let u_residual = dequant_and_inverse_typed_wh(
2167+ &u_levels,
2168+ chroma_w,
2169+ chroma_h,
2170+ 8,
2171+ i32::from(base_q_idx),
2172+ u_tx_type,
2173+ );
2174+ u.reconstruct_rect(
2175+ cpx,
2176+ cpy,
2177+ chroma_w,
2178+ chroma_h,
2179+ uv_predict_mode,
2180+ angle_delta_uv,
2181+ reach,
2182+ &u_residual,
2183+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
2184+ None,
2185+ );
2186+ let v_default_tx = default_intra_tx_type(uv_predict_mode as u8);
2187+ let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1);
2188+ let mut v_coding = cdfs.txb(TxbSet::ChromaRect16x8, uv_predict_mode);
2189+ let (v_levels, v_tx_type) = read_coeffs_rect(
2190+ dec,
2191+ &mut v_coding,
2192+ chroma_scan,
2193+ chroma_w,
2194+ chroma_h,
2195+ v_skip_ctx,
2196+ dc_sign_ctx(around[2].2),
2197+ v_default_tx,
2198+ )?;
2199+ let v_residual = dequant_and_inverse_typed_wh(
2200+ &v_levels,
2201+ chroma_w,
2202+ chroma_h,
2203+ 8,
2204+ i32::from(base_q_idx),
2205+ v_tx_type,
2206+ );
2207+ v.reconstruct_rect(
2208+ cpx,
2209+ cpy,
2210+ chroma_w,
2211+ chroma_h,
2212+ uv_predict_mode,
2213+ angle_delta_uv,
2214+ reach,
2215+ &v_residual,
2216+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
2217+ None,
2218+ );
2219+ neighbours.record_rect(at, bw, bh, mode, &[luma_levels, u_levels, v_levels]);
2220+ RECT_COEFF_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2221+ }
2222+ }
2223+ neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip);
1830 neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0);2224 neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0);
1831 RECT_PARTITION_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);2225 RECT_PARTITION_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1832 Ok(())2226 Ok(())
@@ -4350,6 +4744,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
4350 &mut v,4744 &mut v,
4351 enable_filter_intra,4745 enable_filter_intra,
4352 allow_screen_content_tools,4746 allow_screen_content_tools,
4747+ base_q_idx,
4353 )?;4748 )?;
4354 decode_block_rect(4749 decode_block_rect(
4355 &mut dec,4750 &mut dec,
@@ -4363,6 +4758,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
4363 &mut v,4758 &mut v,
4364 enable_filter_intra,4759 enable_filter_intra,
4365 allow_screen_content_tools,4760 allow_screen_content_tools,
4761+ base_q_idx,
4366 )?;4762 )?;
4367 }4763 }
4368 PARTITION_VERT => {4764 PARTITION_VERT => {
@@ -4380,6 +4776,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
4380 &mut v,4776 &mut v,
4381 enable_filter_intra,4777 enable_filter_intra,
4382 allow_screen_content_tools,4778 allow_screen_content_tools,
4779+ base_q_idx,
4383 )?;4780 )?;
4384 decode_block_rect(4781 decode_block_rect(
4385 &mut dec,4782 &mut dec,
@@ -4393,6 +4790,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
4393 &mut v,4790 &mut v,
4394 enable_filter_intra,4791 enable_filter_intra,
4395 allow_screen_content_tools,4792 allow_screen_content_tools,
4793+ base_q_idx,
4396 )?;4794 )?;
4397 }4795 }
4398 _ => {4796 _ => {
crates/ec-av1/src/stream.rsmodified+16 -1
@@ -4185,10 +4185,25 @@ mod tests {
4185 "{NAME}: SOFT-NOTE -- zero HORZ/VERT strips fired this run \4185 "{NAME}: SOFT-NOTE -- zero HORZ/VERT strips fired this run \
4186 ({matched} matches, {named_refusals} refusals); sampling, not a regression"4186 ({matched} matches, {named_refusals} refusals); sampling, not a regression"
4187 );4187 );
4188+ } else if crate::decode::rect_coeff_hits() == 0 {
4189+ // lane-rectwire r3: r2's real (non-skip) coefficient decode
4190+ // desyncs (rectwire-flake-1.obu, seed 55 -- both 32x16 strips of
4191+ // a fired quadrant come out ~100% wrong from their first pixel;
4192+ // see lanes/rectwire-r3.report.md). Reverted to refusing a coded
4193+ // rect strip by name, same as r1, so `rect_coeff_hits` is always
4194+ // 0 now -- this used to be a hard assert (r2), downgraded back to
4195+ // a SOFT-NOTE until the desync is isolated and the real decode
4196+ // path is safe to re-enable.
4197+ eprintln!(
4198+ "{NAME}: SOFT-NOTE -- {} HORZ/VERT strips fired but rect_coeff_hits==0 \
4199+ (r3: non-skip rect strips refuse by name pending the desync fix)",
4200+ crate::decode::rect_partition_hits()
4201+ );
4188 }4202 }
4189 eprintln!(4203 eprintln!(
4190- "{NAME}: {named_refusals} named refusals, {matched} pixel-exact matches out of {n_attempts}, rect_partition_hits={} extended_partition_hits={} partab_hits={}",4204+ "{NAME}: {named_refusals} named refusals, {matched} pixel-exact matches out of {n_attempts}, rect_partition_hits={} rect_coeff_hits={} extended_partition_hits={} partab_hits={}",
4191 crate::decode::rect_partition_hits(),4205 crate::decode::rect_partition_hits(),
4206+ crate::decode::rect_coeff_hits(),
4192 crate::decode::extended_partition_hits(),4207 crate::decode::extended_partition_hits(),
4193 crate::decode::partab_hits()4208 crate::decode::partab_hits()
4194 );4209 );
lanes/rectwire-r1.charter.mdadded+74 -0
@@ -0,0 +1,74 @@
1+# lane-rectwire r1 charter — wire the rectangular transform into decode.rs
2+
3+## Where you are
4+Worktree /home/user/Documents/Code/Rust/edith_codecs-rectwire, branch
5+lane-rectwire @ main (6e54e47).
6+Build/test ONLY:
7+ `env CARGO_TARGET_DIR=$HOME/.cache/cargo-target-rectwire CARGO_BUILD_JOBS=4 nice -n 19 cargo test -p ec-av1 --release --lib <name> -- --nocapture`
8+FOREGROUND, EC_AV1_REQUIRE_AOMENC=1 on gates. Never push. Never touch the main
9+checkout or the sibling -gm worktree. WIP COMMIT after every green milestone.
10+
11+## The primitive already exists — do not rebuild it
12+lane-recttx landed the rectangular inverse transform this lane consumes:
13+`transform.rs::inverse_transform_2d_typed_wh(dequant, w, h, bit_depth, tx_type)`
14+and `dequant_and_inverse_typed_wh`, with `quant.rs::dequant_wh` beside them.
15+All 14 rect sizes are pinned by checksum against real libaom kernels
16+(`transform.rs::tests::rect_sizes_pinned_against_libaom`) and the square path
17+is proven bit-identical. Read `lanes/recttx-r1.report.md` first. The transform
18+MATH is done; this lane is the decode-side wiring it named as still owed.
19+
20+## What is missing (the report's own list)
21+1. **Rectangular scan order.** Only square zig-zag/diagonal tables are wired.
22+ libaom's scan tables live in `~/.cache/aom-oracle/src/av1/common/scan.c`
23+ (`av1_default_scan_<w>x<h>` and the class/`av1_scan_orders` table).
24+2. **Rectangular eob context.** The `get_eob_ctx` family is keyed on a square
25+ tx size class today; libaom keys it on the real tx size
26+ (`av1/common/txb_common.h`, `av1/decoder/decodetxb.c`).
27+3. **`max_txsize_rect_lookup` threading** through partition and tx-size
28+ selection, so a rect block asks for its real tx size instead of a square
29+ approximation.
30+
31+## Two refusals this is meant to lift, in priority order
32+- `decode.rs`'s refusal of every INTER partition below 16x16 (grep
33+ "an inter partition below 16x16"). This is why the 8x8-OBMC gate has never
34+ fired one 8x8 OBMC block in 80 attempts, despite the encoder writing them
35+ constantly.
36+- The intra HORZ/VERT strips' skip-only restriction (grep "a non-skip HORZ/VERT
37+ intra strip"), and the strip refusal in screen-content frames beside it.
38+
39+Lifting EITHER one with pixel-exact evidence is a good round. Do not attempt
40+both if the first is not clean — a partial landing that refuses precisely is
41+worth more than a wide one that decodes wrong.
42+
43+## Traps this repo has already paid for — read these before you start
44+- **Cross-axis scan weights.** A scan weight or step can use the CROSS axis
45+ where libaom says so; square candidates hide every axis swap. Sweep a WxH
46+ case together with its HxW twin in the SAME test, always.
47+- **A reference's layout is not the spec's.** libaom's coefficient buffer is
48+ column-major; ours is row-major. Transpose at the boundary, never in our
49+ code, or a real axis bug gets cancelled by a transposed harness.
50+- **A new tx size threads more surfaces than you expect** — scan, eob, context
51+ derivation, the tx-set tables. Grep the reference for every indexed surface
52+ and sweep them in ONE round rather than discovering them one desync at a time.
53+- **Adapting CDF four-site wiring**: struct field, defaults, save/restore,
54+ per-frame counter reset. A table missing from the counter reset has the right
55+ values and the wrong adaptation rate, and desyncs only after many reads.
56+
57+## Method
58+Refuse-by-name first, decode second: it is always acceptable to read every
59+symbol correctly and refuse the reconstruction by name. A desync is not
60+acceptable. On any pixel mismatch, self-pin with
61+`EC_AV1_GATE_DUMP=/tmp/claude-1000/rectwire-flake-N.obu`, then localize by
62+comparing the msac RANGE (never `tell()` — baselines differ by fixed constants
63+between decoders) from our `EC_AV1_TRACE` output against the instrumented
64+`~/.cache/aom-oracle/build/aomdec` under `EC_TRACE=1`. Ranges equal up to the
65+bad block means a prediction defect; ranges diverging means a consumption
66+defect. Do NOT guess-fix.
67+
68+## Done criteria
69+Full lib suite stays 226 passed / 0 failed. At least one of the two refusals
70+narrowed or lifted with pixel-exact evidence, and the gate that proves it
71+fires and HARD-ASSERTS its firing count (a gate that cannot fire the feature it
72+names measures nothing — the 8x8-OBMC gate is the cautionary example).
73+REPORT `lanes/rectwire-r1.report.md`, VERDICT on the FIRST line, with the
74+before/after refusal counts and the firing count.
lanes/rectwire-r1.report.mdadded+168 -0
@@ -0,0 +1,168 @@
1+# lane-rectwire r1 report
2+
3+VERDICT: NO CODE LANDED — design fully worked out and verified against real
4+libaom source, but not implemented, because the remaining turn budget did not
5+leave room to implement AND test-iterate a coefficient-decode surface where
6+"a desync is not acceptable" is the hard rule. Landing an unverified multi-site
7+edit at the end of a budget is exactly how a desync gets shipped; refusing by
8+inaction here is the charter's own method applied to the meta-level. Tree is
9+untouched (`git status --short` empty on HEAD), lib suite is unchanged at
10+226 passed / 0 failed, refusal counts are unchanged from before this round.
11+
12+## What actually narrowed: the scope, not the code
13+
14+`decode_block_rect` (`decode.rs:1713`) is called from exactly **four** call
15+sites, all at exactly **two** fixed size pairs: luma 32x16/16x32
16+(`decode.rs:4341-4396`, only from a 32x32 quadrant's `PARTITION_HORZ`/`VERT`)
17+and its chroma half 16x8/8x16. This is much narrower than the charter's "every
18+INTER partition below 16x16" framing suggested for the intra strip -- the
19+non-skip refusal at `decode.rs:1772-1776` only ever needs to cover these two
20+size pairs, not a general rect-tx-size machine. The INTER-partition-below-16x16
21+refusal (`decode.rs:8697`) was not investigated this round (charter: land one
22+cleanly, not both).
23+
24+## The three "missing surfaces" the charter named, resolved
25+
26+1. **Rectangular scan order.** libaom's `default_scan_WxH` tables are
27+ precomputed data, not a general algorithm in source (no generator shipped
28+ in `~/.cache/aom-oracle/src`). Extracted+transposed via a one-off script
29+ (libaom's own buffer is column-major, `col = pos >> bhl; row = pos -
30+ (col << bhl)` where `bhl = log2(height)`; our grid is row-major, so
31+ `our_pos = row * w + col`). Verified as a genuine bijection of `0..w*h` for
32+ all four needed tables (32x16, 16x32, 16x8, 8x16). Script + generated
33+ consts are in `/tmp/claude-1000/gen_scan.py` and
34+ `/tmp/claude-1000/scan_tables.rs` in this worktree's scratchpad (not
35+ copied into the repo since nothing consumes them yet).
36+
37+2. **Rectangular eob context.** `eob_coeff_ctx(scan_idx, area)` already takes
38+ `area`, not `side`, and libaom's own `get_lower_levels_ctx_eob` formula
39+ (`(width << bhl)`, i.e. `width * height`) reduces to the exact same `area`
40+ — **no change needed here**, only a caller passing `w * h` instead of
41+ `side * side`. The genuine gap is `base_ctx`'s 2D-class position-offset
42+ table (`NZ_MAP_CTX_OFFSET_32`, `cdf.rs:569`), which is square-only.
43+ libaom's own comment in `txb_common.h:189-224`
44+ (`get_nz_map_ctx_from_stats`) gives the exact rect generalization
45+ verbatim: `if width < height && row < 2: offset 11; else if width > height
46+ && col < 2: offset 16; else` fall through to the existing `row+col<2→1,
47+ <4→6, else 21` table square case already implements. Confirmed this
48+ reduces to the identical square table when `width == height` (the two new
49+ branches never fire). `br_ctx`'s neighbour-offset math is shape-independent
50+ for `TxClass::TwoD` (only the boundary clamp needs real `w`/`h` instead of
51+ one `side`).
52+
53+3. **`max_txsize_rect_lookup` threading.** Turned out to be unnecessary for
54+ this specific lift: `decode_block_rect` already resolves `tx_w = bw, tx_h
55+ = bh` directly (no split, since `depth != 0` is refused above the skip
56+ check) — the real tx size *is* the block size here, spec's rect-lookup
57+ table is a non-issue at this call site.
58+
59+## The one genuinely new piece: two CDF table sets
60+
61+Coefficient CDFs are selected by `get_txsize_entropy_ctx(tx_size) =
62+txsize_sqr_up_map[tx_size]` (`entropy.h:172`, confirmed algebraically: for any
63+2:1 rect size this reduces to exactly `sqr_up`, never the average). For our
64+two size pairs that means:
65+- 32x16/16x32 luma → same square-equivalent (`TX_32X32`) as the already-wired
66+ `TxbSet::Luma32` for **every** field except `eob_pt` — `txb_skip`, `base`,
67+ `base_eob`, `br`, `dc_sign` are all directly reusable as-is (confirmed field
68+ presence: `txb_skip_luma_32`, `base_luma_32`, `base_eob_luma_32`,
69+ `br_luma_32`, `dc_sign_luma` all already exist in `cdf_state.rs`).
70+- 16x8/8x16 chroma → same square-equivalent (`TX_16X16`) as `TxbSet::Chroma16`,
71+ same story (`txb_skip_chroma_16`, `base_chroma_16`, `base_eob_chroma_16`,
72+ `br_chroma_16`, `dc_sign_chroma` all exist).
73+
74+Only `eob_pt` differs: the real-size eob group count is `log2(w*h) - 4`
75+(`txsize_log2_minus4`, confirmed exact match against the table in
76+`common_data.h:346`), which for 32x16/16x32 is 5 (the **512**-group, 10
77+symbols) — not 6 (**1024**-group, `eob_pt_1024_luma`, what `Luma32` reads for
78+a true 32x32) — and for 16x8/8x16 is 3 (the **128**-group, 8 symbols) — not 4
79+(**256**-group, `eob_pt_256_chroma`, what `Chroma16` reads for a true 16x16).
80+Neither `eob_pt_512_luma` nor `eob_pt_128_chroma` exist in `cdf_state.rs`
81+today. Their default values (4 q-context variants each, luma plane_type 0
82+class-2D and chroma plane_type 1 class-2D) were extracted and transcribed
83+from `~/.cache/aom-oracle/src/av1/common/token_cdfs.h:829-906`
84+(`av1_default_eob_multi128_cdfs`, `av1_default_eob_multi512_cdfs`), format
85+confirmed against this codebase's own conversion convention (`AOM_CDFn`'s raw
86+arguments direct, no `AOM_ICDF` inversion — cross-checked `EOB_PT_1024_LUMA`
87+byte-for-byte against `av1_default_eob_multi1024_cdfs[2][0][0]`, an exact
88+match, q-index 2 = the unsuffixed default, 0/1/3 = `_Q0`/`_Q1`/`_Q3`):
89+
90+```
91+EOB_PT_512_LUMA (q2, plane0, class2D): 2624,3936,6480,9686,13979,17726,23267,28410,31078,32768,0
92+EOB_PT_512_LUMA_Q0: 641,983,3707,5430,10234,14958,18788,23412,26061,32768,0
93+EOB_PT_512_LUMA_Q1: 1230,2278,5035,7776,11871,15346,19590,24584,28749,32768,0
94+EOB_PT_512_LUMA_Q3: 5927,7809,10923,14597,19439,24135,28456,31142,32060,32768,0
95+
96+EOB_PT_128_CHROMA (q2, plane1, class2D): 13627,16246,20173,24429,27948,30415,31863,32768,0
97+EOB_PT_128_CHROMA_Q0: 5245,7456,12880,15852,20033,23932,27608,32768,0
98+EOB_PT_128_CHROMA_Q1: 8045,11200,15497,19595,23948,27408,30938,32768,0
99+EOB_PT_128_CHROMA_Q3: 24313,26062,28385,30107,31217,31898,32345,32768,0
100+```
101+
102+## The wiring plan (unexecuted, for the next round)
103+
104+1. `cdf.rs`: add the 8 arrays above.
105+2. `cdf_state.rs`: add `eob_pt_512_luma: [u16; 11]` / `eob_pt_128_chroma:
106+ [u16; 9]` fields, `pick()`-initialize in `Cdfs::new`, add two `TxbSet`
107+ variants (`LumaRect32x16`, `ChromaRect16x8`) whose `txb()` match arms are
108+ byte-identical to `Luma32`/`Chroma16` except `eob_pt` points at the new
109+ fields. One variant each covers both orientations (32x16 and 16x32 share
110+ one square-equivalent context; likewise 16x8/8x16) — no per-orientation
111+ duplication needed.
112+3. `decode.rs`: four new small functions mirroring the existing square ones'
113+ `TxClass::TwoD` branch only (refuse-by-name if a symbol somehow produces a
114+ different class — should be geometrically impossible here since neither
115+ size pair ever reads a `tx_type` symbol: luma's is forced `DctDct` by
116+ `EXT_TX_SET_DCTONLY` at `tx_size_sqr_up == TX_32X32`, chroma is always
117+ mode-derived via the existing `default_intra_tx_type`, spec
118+ `av1_get_ext_tx_set_type`, `blockd.h:1097`):
119+ - `nz_map_offset_2d(row, col, w, h)`, `base_ctx_rect`, `br_ctx_rect`,
120+ `neighbour_rect` (the rect generalization above).
121+ - `read_coeffs_rect(dec, coding, scan, w, h, skip_ctx, sign_ctx,
122+ default_tx_type)`, a `(w, h)`-aware copy of `read_coeffs` restricted to
123+ `TxClass::TwoD` (refuses by name otherwise).
124+ - A rect `Neighbours::around_rect` (independent `w`/`h` extents, mirroring
125+ the already-landed `record_rect`/`record_mi_rect` asymmetric-extent
126+ pattern at `decode.rs:1378-1457` — `record_mi_rect` already handles real
127+ coefficient grids correctly with no change needed, since a lone
128+ non-split TU broadcasts one state uniformly across its own mi span
129+ either way).
130+ - `decode_block_rect` gains a `base_q_idx: u8` parameter (threaded from
131+ the four already-in-scope call sites at `decode.rs:4341-4396`, where
132+ `base_q_idx` is already a local), replaces the
133+ `if !skip { return Err(...) }` block with real reads through
134+ `read_coeffs_rect` + `transform::dequant_and_inverse_typed_wh` (already
135+ landed by lane-recttx) for luma then chroma, and passes the real
136+ residual (not `vec![0i32; ...]`) into the existing `reconstruct_rect`
137+ calls and the real coefficient grid (not the zero placeholder) into
138+ `record_rect`.
139+ - A new `RECT_COEFF_HITS` atomic counter incremented once per real
140+ non-skip decode, hard-asserted `> 0` in the existing free-partition gate
141+ (`stream.rs:4060-4137`, which already tracks `rect_partition_hits` and
142+ currently only ever sees the skip case reach decode) — no new gate file
143+ needed, this lift makes the *existing* gate exercise more of the stream
144+ it already decodes.
145+
146+## Why this stopped here instead of landing partial code
147+
148+Every individual piece above was cross-checked against real libaom source
149+(not memory, not the spec's prose) with byte-level verification where a
150+mistake would desync silently: the scan-table transpose was checked as a
151+bijection, the `EOB_PT_512_LUMA`/`_128_CHROMA` q2 rows follow the exact
152+`AOM_CDFn`-argument-direct convention this codebase already uses (verified
153+against `EOB_PT_1024_LUMA`'s known-good transcription), and the
154+`nz_map_offset_2d` rect branches are libaom's own comment, not a derivation.
155+But none of it has been compiled or run against a real aomenc stream, and
156+this decode path has no tolerance for "probably right" — the method this
157+lane was chartered under is explicit that a desync is never acceptable, only
158+a clean refusal is. Implementing all of the above plus threading
159+`base_q_idx` through four call sites plus writing/running the gate assertion
160+was more edit-and-verify surface than the remaining turn budget could safely
161+absorb, so nothing was committed rather than risk landing something wrong
162+this round left no room to catch.
163+
164+## Before/after refusal counts, firing count
165+
166+Unchanged this round: both named refusals (`decode.rs:1772-1776` intra
167+HORZ/VERT non-skip, `decode.rs:8697` inter-below-16x16) still fire exactly as
168+before. No gate firing count to report — nothing new decodes yet.
lanes/rectwire-r3.report.mdadded+112 -0
@@ -0,0 +1,112 @@
1+# lane-rectwire round 3
2+
3+VERDICT: reverted r2's real (non-skip) rect coefficient decode to refuse by
4+name -- the pixel-exact gate found a genuine desync (not a subtle context
5+bug), and this round's budget was exhausted before isolating its exact
6+symbol. No desync landed. Free-partition gate green, full lib suite
7+231/0.
8+
9+## What was confirmed a defect, and where it isn't
10+
11+`/tmp/claude-1000/rectwire-flake-1.obu` (seed 55, 64x64x24) reproduces
12+byte-identically from the pinned bytes across two runs (`scratch_decode_
13+pinned_stream_once`, `OK: 24 frames` both times) -- this is a real decode
14+bug, not attempt-selection flake.
15+
16+`scratch_isolate_pinned_mismatch` on that stream: frame 0 luma has 1085/4096
17+mismatches. The *first* one (row 28, col 31, delta 1) sits inside the
18+top-left 32x32 `PARTITION_NONE` intra block -- **not** a rect strip at all
19+(charter's step-3 question answered: no). But a quadrant breakdown shows
20+1015 of the 1085 luma mismatches (93.5%) land inside the one rect quadrant
21+this frame has: `mi(8,8)` reads `PARTITION_HORZ` (`TRACE partition_w32
22+mi=(8,8) ctx=0 value=1`), splitting the bottom-right 32x32 into two true
23+32x16 strips. Sub-bucketing that quadrant: strip 0 (rows 32..48) is 503/512
24+wrong, strip 1 (rows 48..64) is 512/512 wrong, both starting at their very
25+first pixel (strip 1's first pixel is 105 vs 72 -- a 33-level jump, not a
26+rounding-scale error). That magnitude and near-total coverage is a real
27+desync inside the rect coefficient path, not a context-table nudge; the
28+handful of TL/BL-quadrant mismatches are plausibly a small halo from
29+whatever downstream state the corrupted BR quadrant leaves (not itself
30+implicated).
31+
32+## Three suspects r2 named -- all checked and cleared, cross-referenced
33+against real libaom source this round, not memory:
34+
35+1. **`base_ctx_rect`'s 5-neighbour `TwoD` offset array.** Checked against
36+ `get_nz_map_ctx_from_stats` / the `av1_nz_map_ctx_offset` comment in
37+ `~/.cache/aom-oracle/src/av1/common/txb_common.h:189-224`: the 5 offsets
38+ `(1,0),(0,1),(1,1),(2,0),(0,2)`, the `w<h`/`row<2`→`+11` and
39+ `w>h`/`col<2`→`+16` boundary branches, and the tx-size-independent
40+ `row+col` fallback (verified `cdf::NZ_MAP_CTX_OFFSET_32` already encodes
41+ exactly `row+col<2→1, row+col<4→6, else→21`) all match libaom
42+ byte-for-byte. Not the bug.
43+2. **`u_skip_ctx`/`v_skip_ctx`'s `above + left` formula.** libaom's real
44+ chroma rule (`get_txb_ctx_general`, same file, plane != 0 branch) is
45+ `get_entropy_context(tx_size,a,l) + ctx_offset` where `ctx_offset` is 7
46+ for a lone TU (`plane_bsize == tx bsize`, our case) or 10 otherwise, and
47+ `get_entropy_context` returns exactly `(above coded?)+(left coded?)` --
48+ i.e. the *same* 0..2 range our code computes. Cross-checked against the
49+ **existing, tested** square `Chroma16` path (`read_plane`,
50+ `decode.rs:2707`): it uses the identical `usize::from(around.0) +
51+ usize::from(around.1)` formula with no `+7`, meaning the `txb_skip_
52+ chroma_16` default-CDF table was already transcribed with the `+7`
53+ pre-baked into which 3 rows were picked -- consistent, not the bug.
54+3. **`dc_sign_ctx` reading the wrong `around` index.** `decode_block_rect`
55+ indexes `around[0]` for luma, `around[1]` for u, `around[2]` for v --
56+ same convention the square path's `chroma_around[1]`/`[2]` uses. Not the
57+ bug.
58+
59+Also checked `around_rect`/`record_rect`'s luma-mi-granularity span for
60+chroma (looked like a subsampling bug at first glance): both the write side
61+(`record_mi_rect`) and read side (`around_mi_rect`) use the same un-halved
62+`w_mi`/`h_mi` for all three planes, matching the square path's own
63+`around`/`record` convention exactly. Not a bug either.
64+
65+## Where the range-ladder bisection stalled
66+
67+Built the requested instrument check first: `~/.cache/aom-oracle/build/
68+aomdec` with `EC_TRACE_COEFF=1`/`EC_TRACE_MODE=1` emits real per-block
69+`rng` -- confirmed live (`EC_PART`/`EC_COEFF`/`EC_MODE` all firing on this
70+stream). But the intra key-frame path in libaom's `decodeframe.c` has no
71+traced symbol between the partition read and the first `EC_COEFF` (mode/
72+skip/tx_depth reads for an *intra* block are only traced by the new
73+`EC_TRACE_MODE` patch on the *inter* decodemv.c path, not here), so
74+`aomdec`'s post-partition `rng` and our own decoder's `rng` at coefficient
75+entry aren't directly comparable -- several untraced symbol reads (skip,
76+y_mode, uv_mode, tx_size_cat2 in `read_intra_mode_rect`) sit between them on
77+our side with no anchor on aomdec's. Extending `EC_TRACE_MODE`-style tracing
78+into libaom's intra mode-info reader (a second oracle patch) is the next
79+lane's first move, not attempted this round (budget).
80+
81+## The revert
82+
83+`decode.rs::decode_block_rect`'s non-skip branch now returns `Err(unsupported(
84+"a HORZ/VERT intra strip with real (non-skip) coefficients ..."))` before
85+any of r2's real-decode code runs (that code stays in the file, dead behind
86+the early return, so the next round doesn't re-derive it). The free-partition
87+gate's `RECT_COEFF_HITS` hard assert (r2) is downgraded back to a SOFT-NOTE
88+(`rect_coeff_hits` is now always 0 by construction) -- matches r1's original
89+posture.
90+
91+## Before/after refusal + firing counts
92+
93+This round, one gate run (`EC_AV1_REQUIRE_AOMENC=1`, n=40, seed 42+i):
94+- 29 named refusals, 11 pixel-exact matches, 0 mismatches (gate green).
95+- `rect_partition_hits=9` (real HORZ/VERT strips reached), `rect_coeff_hits=0`
96+ (by construction now), `extended_partition_hits=0`, `partab_hits=2`.
97+- 3 of the 29 refusals are the new named one: "a HORZ/VERT intra strip with
98+ real (non-skip) coefficients (lane-rectwire r3: confirmed desync, not yet
99+ isolated)" (seeds 63, 67, 81) -- these are exactly the runs that would
100+ previously have risked the r2 desync; they refuse cleanly now instead.
101+
102+Full `cargo test -p ec-av1 --release --lib`: 231 passed, 0 failed, 17 ignored.
103+
104+## Next round's first move
105+
106+Patch `~/.cache/aom-oracle`'s `decodeframe.c` intra mode-info reader
107+(`read_intra_frame_mode_info` or equivalent) with the same `rng`-emitting
108+pattern `EC_TRACE_MODE` already added for inter, so a same-length range
109+ladder can walk from the `mi(8,8)` `PARTITION_HORZ` read through skip/
110+y_mode/uv_mode/tx_depth into the first `EC_COEFF` and find the exact
111+diverging symbol, rather than guessing between the coefficient-context
112+math (now cleared) and the prediction/transform math (untested this round).