diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index de755ba4..6fa8d74e 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -920,6 +920,18 @@ pub const COEFF_BR_CHROMA_8: [[u16; 5]; 21] = [ [7625, 13801, 19144, 32768, 0], ]; +/// `Default_Partition_Cdf[0..4]`, `PARTITION_W16` format (spec 5.11.4 codes an +/// 8x8 block's partition as a 4-symbol read that only ever terminates +/// `PARTITION_NONE` for this writer -- `PARTITION_SPLIT` is illegal at 8x8, so +/// row 4 of libaom's real (8-symbol) 8x8 table is dropped and only the shared +/// leading four symbols, common to every partition-symbol alphabet, are kept. +pub const PARTITION_W8: [[u16; 5]; 4] = [ + [19132, 25510, 30392, 32768, 0], + [13928, 19855, 28540, 32768, 0], + [12522, 23679, 28629, 32768, 0], + [9896, 18783, 25853, 32768, 0], +]; + /// `Default_Partition_Cdf[0..4]`: the partition symbol of a 16x16 block. pub const PARTITION_W16: [[u16; 11]; 4] = [ [ @@ -2837,3 +2849,826 @@ mod tests { assert_eq!(MV_SIGN.len(), 2 + 1); } } + +// TX_8X8/TX_4X4 tables (lane-av1-rect): the 8x8 luma and 4x4 chroma coefficient +// coding CDFs, extracted byte-exact from libaom's Default_*_Cdf tables, needed +// to let a key frame's straddling 16x16 quadrant split down to an 8x8 leaf. + +/// TXB_SKIP_LUMA_8_Q0, q-context 0: the same table as [`TXB_SKIP_LUMA_8`], for `base_q_idx` 0..=20. +pub const TXB_SKIP_LUMA_8_Q0: [u16; 3] = [31548, 32768, 0]; + +/// TXB_SKIP_LUMA_8_Q1, q-context 1: the same table as [`TXB_SKIP_LUMA_8`], for `base_q_idx` 21..=60. +pub const TXB_SKIP_LUMA_8_Q1: [u16; 3] = [31782, 32768, 0]; + +/// the all-zero flag of an 8x8 luma transform, whose only context is zero because the transform covers its whole block. +pub const TXB_SKIP_LUMA_8: [u16; 3] = [31957, 32768, 0]; + +/// TXB_SKIP_LUMA_8_Q3, q-context 3: the same table as [`TXB_SKIP_LUMA_8`], for `base_q_idx` 121..=255. +pub const TXB_SKIP_LUMA_8_Q3: [u16; 3] = [31903, 32768, 0]; + +/// TXB_SKIP_CHROMA_4_Q0, q-context 0: the same table as [`TXB_SKIP_CHROMA_4`], for `base_q_idx` 0..=20. +pub const TXB_SKIP_CHROMA_4_Q0: [[u16; 3]; 3] = + [[7654, 32768, 0], [19473, 32768, 0], [29984, 32768, 0]]; + +/// TXB_SKIP_CHROMA_4_Q1, q-context 1: the same table as [`TXB_SKIP_CHROMA_4`], for `base_q_idx` 21..=60. +pub const TXB_SKIP_CHROMA_4_Q1: [[u16; 3]; 3] = + [[5495, 32768, 0], [17942, 32768, 0], [28280, 32768, 0]]; + +/// the all-zero flag of a 4x4 chroma transform. +pub const TXB_SKIP_CHROMA_4: [[u16; 3]; 3] = + [[4119, 32768, 0], [16026, 32768, 0], [25657, 32768, 0]]; + +/// TXB_SKIP_CHROMA_4_Q3, q-context 3: the same table as [`TXB_SKIP_CHROMA_4`], for `base_q_idx` 121..=255. +pub const TXB_SKIP_CHROMA_4_Q3: [[u16; 3]; 3] = + [[2713, 32768, 0], [11861, 32768, 0], [20773, 32768, 0]]; + +/// EOB_PT_64_LUMA_Q0, q-context 0: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 0..=20. +pub const EOB_PT_64_LUMA_Q0: [u16; 8] = [329, 498, 1101, 1784, 3265, 7758, 32768, 0]; + +/// EOB_PT_64_LUMA_Q1, q-context 1: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 21..=60. +pub const EOB_PT_64_LUMA_Q1: [u16; 8] = [1260, 1446, 2253, 3712, 6652, 13369, 32768, 0]; + +/// the end-of-block group of an 8x8 luma transform. +pub const EOB_PT_64_LUMA: [u16; 8] = [2374, 2772, 4583, 7276, 12288, 19706, 32768, 0]; + +/// EOB_PT_64_LUMA_Q3, q-context 3: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 121..=255. +pub const EOB_PT_64_LUMA_Q3: [u16; 8] = [6307, 7541, 12060, 16358, 22553, 27865, 32768, 0]; + +/// EOB_PT_16_CHROMA_Q0, q-context 0: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 0..=20. +pub const EOB_PT_16_CHROMA_Q0: [u16; 6] = [3247, 4950, 9688, 14563, 32768, 0]; + +/// EOB_PT_16_CHROMA_Q1, q-context 1: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 21..=60. +pub const EOB_PT_16_CHROMA_Q1: [u16; 6] = [7637, 9498, 14259, 19108, 32768, 0]; + +/// the end-of-block group of a 4x4 chroma transform. +pub const EOB_PT_16_CHROMA: [u16; 6] = [11139, 13270, 18241, 23566, 32768, 0]; + +/// EOB_PT_16_CHROMA_Q3, q-context 3: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 121..=255. +pub const EOB_PT_16_CHROMA_Q3: [u16; 6] = [19575, 21766, 26044, 29709, 32768, 0]; + +/// EOB_EXTRA_LUMA_8_Q0, q-context 0: the same table as [`EOB_EXTRA_LUMA_8`], for `base_q_idx` 0..=20. +pub const EOB_EXTRA_LUMA_8_Q0: [[u16; 3]; 9] = [ + [20401, 32768, 0], + [17025, 32768, 0], + [12845, 32768, 0], + [12873, 32768, 0], + [14094, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +/// EOB_EXTRA_LUMA_8_Q1, q-context 1: the same table as [`EOB_EXTRA_LUMA_8`], for `base_q_idx` 21..=60. +pub const EOB_EXTRA_LUMA_8_Q1: [[u16; 3]; 9] = [ + [20430, 32768, 0], + [20662, 32768, 0], + [15367, 32768, 0], + [16970, 32768, 0], + [14657, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +/// the top offset bit inside an 8x8 luma transform's end-of-block group. +pub const EOB_EXTRA_LUMA_8: [[u16; 3]; 9] = [ + [19139, 32768, 0], + [21487, 32768, 0], + [18959, 32768, 0], + [20910, 32768, 0], + [19089, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +/// EOB_EXTRA_LUMA_8_Q3, q-context 3: the same table as [`EOB_EXTRA_LUMA_8`], for `base_q_idx` 121..=255. +pub const EOB_EXTRA_LUMA_8_Q3: [[u16; 3]; 9] = [ + [20238, 32768, 0], + [21057, 32768, 0], + [19159, 32768, 0], + [22337, 32768, 0], + [20159, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +/// EOB_EXTRA_CHROMA_4_Q0, q-context 0: the same table as [`EOB_EXTRA_CHROMA_4`], for `base_q_idx` 0..=20. +pub const EOB_EXTRA_CHROMA_4_Q0: [[u16; 3]; 9] = [ + [19069, 32768, 0], + [22525, 32768, 0], + [13377, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +/// EOB_EXTRA_CHROMA_4_Q1, q-context 1: the same table as [`EOB_EXTRA_CHROMA_4`], for `base_q_idx` 21..=60. +pub const EOB_EXTRA_CHROMA_4_Q1: [[u16; 3]; 9] = [ + [20335, 32768, 0], + [21667, 32768, 0], + [14818, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +/// the same, for a 4x4 chroma transform. +pub const EOB_EXTRA_CHROMA_4: [[u16; 3]; 9] = [ + [20090, 32768, 0], + [19444, 32768, 0], + [17286, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +/// EOB_EXTRA_CHROMA_4_Q3, q-context 3: the same table as [`EOB_EXTRA_CHROMA_4`], for `base_q_idx` 121..=255. +pub const EOB_EXTRA_CHROMA_4_Q3: [[u16; 3]; 9] = [ + [21416, 32768, 0], + [20855, 32768, 0], + [23410, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +/// COEFF_BASE_LUMA_8_Q0, q-context 0: the same table as [`COEFF_BASE_LUMA_8`], for `base_q_idx` 0..=20. +pub const COEFF_BASE_LUMA_8_Q0: [[u16; 5]; 42] = [ + [4536, 10072, 14001, 32768, 0], + [25459, 31416, 32206, 32768, 0], + [16605, 28048, 30818, 32768, 0], + [11008, 22857, 27719, 32768, 0], + [6915, 16268, 22315, 32768, 0], + [2625, 6812, 10537, 32768, 0], + [24257, 31788, 32499, 32768, 0], + [16880, 29454, 31879, 32768, 0], + [11958, 25054, 29778, 32768, 0], + [7916, 18718, 25084, 32768, 0], + [3383, 8777, 13446, 32768, 0], + [22720, 31603, 32393, 32768, 0], + [14960, 28125, 31335, 32768, 0], + [9731, 22210, 27928, 32768, 0], + [6304, 15832, 22277, 32768, 0], + [2910, 7818, 12166, 32768, 0], + [20375, 30627, 32131, 32768, 0], + [13904, 27284, 30887, 32768, 0], + [9368, 21558, 27144, 32768, 0], + [5937, 14966, 21119, 32768, 0], + [2667, 7225, 11319, 32768, 0], + [23970, 31470, 32378, 32768, 0], + [17173, 29734, 32018, 32768, 0], + [12795, 25441, 29965, 32768, 0], + [8981, 19680, 25893, 32768, 0], + [4728, 11372, 16902, 32768, 0], + [24287, 31797, 32439, 32768, 0], + [16703, 29145, 31696, 32768, 0], + [10833, 23554, 28725, 32768, 0], + [6468, 16566, 23057, 32768, 0], + [2415, 6562, 10278, 32768, 0], + [26610, 32395, 32659, 32768, 0], + [18590, 30498, 32117, 32768, 0], + [12420, 25756, 29950, 32768, 0], + [7639, 18746, 24710, 32768, 0], + [3001, 8086, 12347, 32768, 0], + [25076, 32064, 32580, 32768, 0], + [17946, 30128, 32028, 32768, 0], + [12024, 24985, 29378, 32768, 0], + [7517, 18390, 24304, 32768, 0], + [3243, 8781, 13331, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +/// COEFF_BASE_LUMA_8_Q1, q-context 1: the same table as [`COEFF_BASE_LUMA_8`], for `base_q_idx` 21..=60. +pub const COEFF_BASE_LUMA_8_Q1: [[u16; 5]; 42] = [ + [6779, 13743, 17678, 32768, 0], + [24806, 31797, 32457, 32768, 0], + [17616, 29047, 31372, 32768, 0], + [11063, 23175, 28003, 32768, 0], + [6521, 16110, 22324, 32768, 0], + [2764, 7504, 11654, 32768, 0], + [25266, 32367, 32637, 32768, 0], + [19054, 30553, 32175, 32768, 0], + [12139, 25212, 29807, 32768, 0], + [7311, 18162, 24704, 32768, 0], + [3397, 9164, 14074, 32768, 0], + [25988, 32208, 32522, 32768, 0], + [16253, 28912, 31526, 32768, 0], + [9151, 21387, 27372, 32768, 0], + [5688, 14915, 21496, 32768, 0], + [2717, 7627, 12004, 32768, 0], + [23144, 31855, 32443, 32768, 0], + [16070, 28491, 31325, 32768, 0], + [8702, 20467, 26517, 32768, 0], + [5243, 13956, 20367, 32768, 0], + [2621, 7335, 11567, 32768, 0], + [26636, 32340, 32630, 32768, 0], + [19990, 31050, 32341, 32768, 0], + [13243, 26105, 30315, 32768, 0], + [8588, 19521, 25918, 32768, 0], + [4717, 11585, 17304, 32768, 0], + [25844, 32292, 32582, 32768, 0], + [19090, 30635, 32097, 32768, 0], + [11963, 24546, 28939, 32768, 0], + [6218, 16087, 22354, 32768, 0], + [2340, 6608, 10426, 32768, 0], + [28046, 32576, 32694, 32768, 0], + [21178, 31313, 32296, 32768, 0], + [13486, 26184, 29870, 32768, 0], + [7149, 17871, 23723, 32768, 0], + [2833, 7958, 12259, 32768, 0], + [27710, 32528, 32686, 32768, 0], + [20674, 31076, 32268, 32768, 0], + [12413, 24955, 29243, 32768, 0], + [6676, 16927, 23097, 32768, 0], + [2966, 8333, 12919, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +/// an 8x8 luma coefficient's base level. +pub const COEFF_BASE_LUMA_8: [[u16; 5]; 42] = [ + [9704, 17294, 21132, 32768, 0], + [26762, 32278, 32633, 32768, 0], + [18382, 29620, 31819, 32768, 0], + [10891, 23475, 28723, 32768, 0], + [6358, 16583, 23309, 32768, 0], + [3248, 9118, 14141, 32768, 0], + [27204, 32573, 32699, 32768, 0], + [19818, 30824, 32329, 32768, 0], + [11772, 25120, 30041, 32768, 0], + [6995, 18033, 25039, 32768, 0], + [3752, 10442, 16098, 32768, 0], + [27222, 32256, 32559, 32768, 0], + [15356, 28399, 31475, 32768, 0], + [8821, 20635, 27057, 32768, 0], + [5511, 14404, 21239, 32768, 0], + [2935, 8222, 13051, 32768, 0], + [24875, 32120, 32529, 32768, 0], + [15233, 28265, 31445, 32768, 0], + [8605, 20570, 26932, 32768, 0], + [5431, 14413, 21196, 32768, 0], + [2994, 8341, 13223, 32768, 0], + [28201, 32604, 32700, 32768, 0], + [21041, 31446, 32456, 32768, 0], + [13221, 26213, 30475, 32768, 0], + [8255, 19385, 26037, 32768, 0], + [4930, 12585, 18830, 32768, 0], + [28768, 32448, 32627, 32768, 0], + [19705, 30561, 32021, 32768, 0], + [11572, 23589, 28220, 32768, 0], + [5532, 15034, 21446, 32768, 0], + [2460, 7150, 11456, 32768, 0], + [29874, 32619, 32699, 32768, 0], + [21621, 31071, 32201, 32768, 0], + [12511, 24747, 28992, 32768, 0], + [6281, 16395, 22748, 32768, 0], + [3246, 9278, 14497, 32768, 0], + [29715, 32625, 32712, 32768, 0], + [20958, 31011, 32283, 32768, 0], + [11233, 23671, 28806, 32768, 0], + [6012, 16128, 22868, 32768, 0], + [3427, 9851, 15414, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +/// COEFF_BASE_LUMA_8_Q3, q-context 3: the same table as [`COEFF_BASE_LUMA_8`], for `base_q_idx` 121..=255. +pub const COEFF_BASE_LUMA_8_Q3: [[u16; 5]; 42] = [ + [7754, 16948, 22142, 32768, 0], + [25670, 32330, 32691, 32768, 0], + [15663, 29225, 31994, 32768, 0], + [9878, 23288, 29158, 32768, 0], + [6419, 17088, 24336, 32768, 0], + [3859, 11003, 17039, 32768, 0], + [27562, 32595, 32725, 32768, 0], + [17575, 30588, 32399, 32768, 0], + [10819, 24838, 30309, 32768, 0], + [7124, 18686, 25916, 32768, 0], + [4479, 12688, 19340, 32768, 0], + [28385, 32476, 32673, 32768, 0], + [15306, 29005, 31938, 32768, 0], + [8937, 21615, 28322, 32768, 0], + [5982, 15603, 22786, 32768, 0], + [3620, 10267, 16136, 32768, 0], + [27280, 32464, 32667, 32768, 0], + [15607, 29160, 32004, 32768, 0], + [9091, 22135, 28740, 32768, 0], + [6232, 16632, 24020, 32768, 0], + [4047, 11377, 17672, 32768, 0], + [29220, 32630, 32718, 32768, 0], + [19650, 31220, 32462, 32768, 0], + [13050, 26312, 30827, 32768, 0], + [9228, 20870, 27468, 32768, 0], + [6146, 15149, 21971, 32768, 0], + [30169, 32481, 32623, 32768, 0], + [17212, 29311, 31554, 32768, 0], + [9911, 21311, 26882, 32768, 0], + [4487, 13314, 20372, 32768, 0], + [2570, 7772, 12889, 32768, 0], + [30924, 32613, 32708, 32768, 0], + [19490, 30206, 32107, 32768, 0], + [11232, 23998, 29276, 32768, 0], + [6769, 17955, 25035, 32768, 0], + [4398, 12623, 19214, 32768, 0], + [30609, 32627, 32722, 32768, 0], + [19370, 30582, 32287, 32768, 0], + [10457, 23619, 29409, 32768, 0], + [6443, 17637, 24834, 32768, 0], + [4645, 13236, 20106, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +/// COEFF_BASE_CHROMA_4_Q0, q-context 0: the same table as [`COEFF_BASE_CHROMA_4`], for `base_q_idx` 0..=20. +pub const COEFF_BASE_CHROMA_4_Q0: [[u16; 5]; 42] = [ + [6302, 16444, 21761, 32768, 0], + [23040, 31538, 32475, 32768, 0], + [15196, 28452, 31496, 32768, 0], + [10020, 22946, 28514, 32768, 0], + [6533, 16862, 23501, 32768, 0], + [3538, 9816, 15076, 32768, 0], + [24444, 31875, 32525, 32768, 0], + [15881, 28924, 31635, 32768, 0], + [9922, 22873, 28466, 32768, 0], + [6527, 16966, 23691, 32768, 0], + [4114, 11303, 17220, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [20201, 30770, 32209, 32768, 0], + [14754, 28071, 31258, 32768, 0], + [8378, 20186, 26517, 32768, 0], + [5916, 15299, 21978, 32768, 0], + [4268, 11583, 17901, 32768, 0], + [24361, 32025, 32581, 32768, 0], + [18673, 30105, 31943, 32768, 0], + [10196, 22244, 27576, 32768, 0], + [5495, 14349, 20417, 32768, 0], + [2676, 7415, 11498, 32768, 0], + [24678, 31958, 32585, 32768, 0], + [18629, 29906, 31831, 32768, 0], + [9364, 20724, 26315, 32768, 0], + [4641, 12318, 18094, 32768, 0], + [2758, 7387, 11579, 32768, 0], + [25433, 31842, 32469, 32768, 0], + [18795, 29289, 31411, 32768, 0], + [7644, 17584, 23592, 32768, 0], + [3408, 9014, 15047, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +/// COEFF_BASE_CHROMA_4_Q1, q-context 1: the same table as [`COEFF_BASE_CHROMA_4`], for `base_q_idx` 21..=60. +pub const COEFF_BASE_CHROMA_4_Q1: [[u16; 5]; 42] = [ + [8455, 19003, 24368, 32768, 0], + [23563, 32021, 32604, 32768, 0], + [16237, 29446, 31935, 32768, 0], + [10724, 23999, 29358, 32768, 0], + [6725, 17528, 24416, 32768, 0], + [3927, 10927, 16825, 32768, 0], + [26313, 32288, 32634, 32768, 0], + [17430, 30095, 32095, 32768, 0], + [11116, 24606, 29679, 32768, 0], + [7195, 18384, 25269, 32768, 0], + [4726, 12852, 19315, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [22822, 31648, 32483, 32768, 0], + [16724, 29633, 31929, 32768, 0], + [10261, 23033, 28725, 32768, 0], + [7029, 17840, 24528, 32768, 0], + [4867, 13886, 21502, 32768, 0], + [25298, 31892, 32491, 32768, 0], + [17809, 29330, 31512, 32768, 0], + [9668, 21329, 26579, 32768, 0], + [4774, 12956, 18976, 32768, 0], + [2322, 7030, 11540, 32768, 0], + [25472, 31920, 32543, 32768, 0], + [17957, 29387, 31632, 32768, 0], + [9196, 20593, 26400, 32768, 0], + [4680, 12705, 19202, 32768, 0], + [2917, 8456, 13436, 32768, 0], + [26471, 32059, 32574, 32768, 0], + [18458, 29783, 31909, 32768, 0], + [8400, 19464, 25956, 32768, 0], + [3812, 10973, 17206, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +/// a 4x4 chroma coefficient's base level. +pub const COEFF_BASE_CHROMA_4: [[u16; 5]; 42] = [ + [10808, 22056, 26896, 32768, 0], + [25739, 32313, 32676, 32768, 0], + [17288, 30203, 32221, 32768, 0], + [11359, 24878, 29896, 32768, 0], + [6949, 17767, 24893, 32768, 0], + [4287, 11796, 18071, 32768, 0], + [27880, 32521, 32705, 32768, 0], + [19038, 31004, 32414, 32768, 0], + [12564, 26345, 30768, 32768, 0], + [8269, 19947, 26779, 32768, 0], + [5674, 14657, 21674, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [25742, 32319, 32671, 32768, 0], + [19557, 31164, 32454, 32768, 0], + [13381, 26381, 30755, 32768, 0], + [10101, 21466, 26722, 32768, 0], + [9209, 19650, 26825, 32768, 0], + [27107, 31917, 32432, 32768, 0], + [18056, 28893, 31203, 32768, 0], + [10200, 21434, 26764, 32768, 0], + [4660, 12913, 19502, 32768, 0], + [2368, 6930, 12504, 32768, 0], + [26960, 32158, 32613, 32768, 0], + [18628, 30005, 32031, 32768, 0], + [10233, 22442, 28232, 32768, 0], + [5471, 14630, 21516, 32768, 0], + [3235, 10767, 17109, 32768, 0], + [27696, 32440, 32692, 32768, 0], + [20032, 31167, 32438, 32768, 0], + [8700, 21341, 28442, 32768, 0], + [5662, 14831, 21795, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +/// COEFF_BASE_CHROMA_4_Q3, q-context 3: the same table as [`COEFF_BASE_CHROMA_4`], for `base_q_idx` 121..=255. +pub const COEFF_BASE_CHROMA_4_Q3: [[u16; 5]; 42] = [ + [9320, 22102, 27840, 32768, 0], + [27057, 32464, 32724, 32768, 0], + [16331, 30268, 32309, 32768, 0], + [10319, 23935, 29720, 32768, 0], + [6189, 16448, 24106, 32768, 0], + [3589, 10884, 18808, 32768, 0], + [29026, 32624, 32748, 32768, 0], + [19226, 31507, 32587, 32768, 0], + [12692, 26921, 31203, 32768, 0], + [7049, 19532, 27635, 32768, 0], + [7727, 15669, 23252, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [28056, 32625, 32748, 32768, 0], + [22383, 32075, 32669, 32768, 0], + [15417, 27098, 31749, 32768, 0], + [18127, 26493, 27190, 32768, 0], + [5461, 16384, 21845, 32768, 0], + [27982, 32091, 32584, 32768, 0], + [19045, 29868, 31972, 32768, 0], + [10397, 22266, 27932, 32768, 0], + [5990, 13697, 21500, 32768, 0], + [1792, 6912, 15104, 32768, 0], + [28198, 32501, 32718, 32768, 0], + [21534, 31521, 32569, 32768, 0], + [11109, 25217, 30017, 32768, 0], + [5671, 15124, 26151, 32768, 0], + [4681, 14043, 18725, 32768, 0], + [28688, 32580, 32741, 32768, 0], + [22576, 32079, 32661, 32768, 0], + [10627, 22141, 28340, 32768, 0], + [9362, 14043, 28087, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +/// COEFF_BASE_EOB_LUMA_8_Q0, q-context 0: the same table as [`COEFF_BASE_EOB_LUMA_8`], for `base_q_idx` 0..=20. +pub const COEFF_BASE_EOB_LUMA_8_Q0: [[u16; 4]; 4] = [ + [5717, 26477, 32768, 0], + [30491, 31703, 32768, 0], + [31550, 32158, 32768, 0], + [29648, 31491, 32768, 0], +]; + +/// COEFF_BASE_EOB_LUMA_8_Q1, q-context 1: the same table as [`COEFF_BASE_EOB_LUMA_8`], for `base_q_idx` 21..=60. +pub const COEFF_BASE_EOB_LUMA_8_Q1: [[u16; 4]; 4] = [ + [15239, 29932, 32768, 0], + [31315, 32095, 32768, 0], + [32130, 32434, 32768, 0], + [30864, 31996, 32768, 0], +]; + +/// the last coefficient's base level, for an 8x8 luma transform. +pub const COEFF_BASE_EOB_LUMA_8: [[u16; 4]; 4] = [ + [18049, 30489, 32768, 0], + [31706, 32286, 32768, 0], + [32163, 32473, 32768, 0], + [31550, 32184, 32768, 0], +]; + +/// COEFF_BASE_EOB_LUMA_8_Q3, q-context 3: the same table as [`COEFF_BASE_EOB_LUMA_8`], for `base_q_idx` 121..=255. +pub const COEFF_BASE_EOB_LUMA_8_Q3: [[u16; 4]; 4] = [ + [21457, 31043, 32768, 0], + [31951, 32483, 32768, 0], + [32153, 32562, 32768, 0], + [31473, 32215, 32768, 0], +]; + +/// COEFF_BASE_EOB_CHROMA_4_Q0, q-context 0: the same table as [`COEFF_BASE_EOB_CHROMA_4`], for `base_q_idx` 0..=20. +pub const COEFF_BASE_EOB_CHROMA_4_Q0: [[u16; 4]; 4] = [ + [21365, 30026, 32768, 0], + [30512, 32423, 32768, 0], + [31658, 32621, 32768, 0], + [29630, 31881, 32768, 0], +]; + +/// COEFF_BASE_EOB_CHROMA_4_Q1, q-context 1: the same table as [`COEFF_BASE_EOB_CHROMA_4`], for `base_q_idx` 21..=60. +pub const COEFF_BASE_EOB_CHROMA_4_Q1: [[u16; 4]; 4] = [ + [26594, 31212, 32768, 0], + [31208, 32582, 32768, 0], + [31835, 32637, 32768, 0], + [30595, 32206, 32768, 0], +]; + +/// the same, for a 4x4 chroma transform. +pub const COEFF_BASE_EOB_CHROMA_4: [[u16; 4]; 4] = [ + [27258, 31095, 32768, 0], + [31804, 32623, 32768, 0], + [31763, 32528, 32768, 0], + [31438, 32506, 32768, 0], +]; + +/// COEFF_BASE_EOB_CHROMA_4_Q3, q-context 3: the same table as [`COEFF_BASE_EOB_CHROMA_4`], for `base_q_idx` 121..=255. +pub const COEFF_BASE_EOB_CHROMA_4_Q3: [[u16; 4]; 4] = [ + [27877, 31584, 32768, 0], + [32170, 32728, 32768, 0], + [32155, 32688, 32768, 0], + [32219, 32702, 32768, 0], +]; + +/// COEFF_BR_LUMA_8_Q0, q-context 0: the same table as [`COEFF_BR_LUMA_8`], for `base_q_idx` 0..=20. +pub const COEFF_BR_LUMA_8_Q0: [[u16; 5]; 21] = [ + [14406, 20862, 24414, 32768, 0], + [11824, 18907, 23109, 32768, 0], + [8257, 14393, 18803, 32768, 0], + [5860, 10747, 14778, 32768, 0], + [4475, 8486, 11984, 32768, 0], + [3606, 6954, 10043, 32768, 0], + [1736, 3410, 5048, 32768, 0], + [14430, 20046, 22882, 32768, 0], + [15593, 22899, 26709, 32768, 0], + [12102, 19368, 23811, 32768, 0], + [9059, 15584, 20262, 32768, 0], + [6999, 12603, 17048, 32768, 0], + [5684, 10497, 14553, 32768, 0], + [2822, 5438, 7862, 32768, 0], + [15785, 21585, 24359, 32768, 0], + [18347, 25229, 28266, 32768, 0], + [14974, 22487, 26389, 32768, 0], + [11423, 18681, 23271, 32768, 0], + [8863, 15350, 20008, 32768, 0], + [7153, 12852, 17278, 32768, 0], + [3707, 7036, 9982, 32768, 0], +]; + +/// COEFF_BR_LUMA_8_Q1, q-context 1: the same table as [`COEFF_BR_LUMA_8`], for `base_q_idx` 21..=60. +pub const COEFF_BR_LUMA_8_Q1: [[u16; 5]; 21] = [ + [15999, 22208, 25449, 32768, 0], + [13050, 19988, 24122, 32768, 0], + [8594, 14864, 19378, 32768, 0], + [6033, 11079, 15238, 32768, 0], + [4554, 8683, 12347, 32768, 0], + [3672, 7139, 10337, 32768, 0], + [1900, 3771, 5576, 32768, 0], + [15788, 21340, 23949, 32768, 0], + [16825, 24235, 27758, 32768, 0], + [12873, 20402, 24810, 32768, 0], + [9590, 16363, 21094, 32768, 0], + [7352, 13209, 17733, 32768, 0], + [5960, 10989, 15184, 32768, 0], + [3232, 6234, 9007, 32768, 0], + [15761, 20716, 23224, 32768, 0], + [19318, 25989, 28759, 32768, 0], + [15529, 23094, 26929, 32768, 0], + [11662, 18989, 23641, 32768, 0], + [8955, 15568, 20366, 32768, 0], + [7281, 13106, 17708, 32768, 0], + [4248, 8059, 11440, 32768, 0], +]; + +/// the base-range tail of an 8x8 luma coefficient. +pub const COEFF_BR_LUMA_8: [[u16; 5]; 21] = [ + [16214, 22380, 25770, 32768, 0], + [14213, 21304, 25295, 32768, 0], + [9213, 15823, 20455, 32768, 0], + [6395, 11758, 16139, 32768, 0], + [4779, 9187, 13066, 32768, 0], + [3821, 7501, 10953, 32768, 0], + [2293, 4567, 6795, 32768, 0], + [15859, 21283, 23820, 32768, 0], + [18404, 25602, 28726, 32768, 0], + [14325, 21980, 26206, 32768, 0], + [10669, 17937, 22720, 32768, 0], + [8297, 14642, 19447, 32768, 0], + [6746, 12389, 16893, 32768, 0], + [4324, 8251, 11770, 32768, 0], + [16532, 21631, 24475, 32768, 0], + [20667, 27150, 29668, 32768, 0], + [16728, 24510, 28175, 32768, 0], + [12861, 20645, 25332, 32768, 0], + [10076, 17361, 22417, 32768, 0], + [8395, 14940, 19963, 32768, 0], + [5731, 10683, 14912, 32768, 0], +]; + +/// COEFF_BR_LUMA_8_Q3, q-context 3: the same table as [`COEFF_BR_LUMA_8`], for `base_q_idx` 121..=255. +pub const COEFF_BR_LUMA_8_Q3: [[u16; 5]; 21] = [ + [18274, 24813, 27890, 32768, 0], + [15537, 23149, 27003, 32768, 0], + [9449, 16740, 21827, 32768, 0], + [6700, 12498, 17261, 32768, 0], + [4988, 9866, 14198, 32768, 0], + [4236, 8147, 11902, 32768, 0], + [2867, 5860, 8654, 32768, 0], + [17124, 23171, 26101, 32768, 0], + [20396, 27477, 30148, 32768, 0], + [16573, 24629, 28492, 32768, 0], + [12749, 20846, 25674, 32768, 0], + [10233, 17878, 22818, 32768, 0], + [8525, 15332, 20363, 32768, 0], + [6283, 11632, 16255, 32768, 0], + [20466, 26511, 29286, 32768, 0], + [23059, 29174, 31191, 32768, 0], + [19481, 27263, 30241, 32768, 0], + [15458, 23631, 28137, 32768, 0], + [12416, 20608, 25693, 32768, 0], + [10261, 18011, 23261, 32768, 0], + [8016, 14655, 19666, 32768, 0], +]; + +/// COEFF_BR_CHROMA_4_Q0, q-context 0: the same table as [`COEFF_BR_CHROMA_4`], for `base_q_idx` 0..=20. +pub const COEFF_BR_CHROMA_4_Q0: [[u16; 5]; 21] = [ + [15967, 22905, 26286, 32768, 0], + [13534, 20654, 24579, 32768, 0], + [9504, 16092, 20535, 32768, 0], + [6975, 12568, 16903, 32768, 0], + [5364, 10091, 14020, 32768, 0], + [4357, 8370, 11857, 32768, 0], + [2506, 4934, 7218, 32768, 0], + [23032, 28815, 30936, 32768, 0], + [19540, 26704, 29719, 32768, 0], + [15158, 22969, 27097, 32768, 0], + [11408, 18865, 23650, 32768, 0], + [8885, 15448, 20250, 32768, 0], + [7108, 12853, 17416, 32768, 0], + [4231, 8041, 11480, 32768, 0], + [19823, 26490, 29156, 32768, 0], + [18890, 25929, 28932, 32768, 0], + [15660, 23491, 27433, 32768, 0], + [12147, 19776, 24488, 32768, 0], + [9728, 16774, 21649, 32768, 0], + [7919, 14277, 19066, 32768, 0], + [5440, 10170, 14185, 32768, 0], +]; + +/// COEFF_BR_CHROMA_4_Q1, q-context 1: the same table as [`COEFF_BR_CHROMA_4`], for `base_q_idx` 21..=60. +pub const COEFF_BR_CHROMA_4_Q1: [[u16; 5]; 21] = [ + [15571, 22232, 25749, 32768, 0], + [14506, 21575, 25374, 32768, 0], + [10189, 17089, 21569, 32768, 0], + [7316, 13301, 17915, 32768, 0], + [5783, 10912, 15190, 32768, 0], + [4760, 9155, 13088, 32768, 0], + [2993, 5966, 8774, 32768, 0], + [23424, 28903, 30778, 32768, 0], + [20775, 27666, 30290, 32768, 0], + [16474, 24410, 28299, 32768, 0], + [12471, 20180, 24987, 32768, 0], + [9410, 16487, 21439, 32768, 0], + [7536, 13614, 18529, 32768, 0], + [5048, 9586, 13549, 32768, 0], + [21090, 27290, 29756, 32768, 0], + [20796, 27402, 30026, 32768, 0], + [17819, 25485, 28969, 32768, 0], + [13860, 21909, 26462, 32768, 0], + [11002, 18494, 23529, 32768, 0], + [8953, 15929, 20897, 32768, 0], + [6448, 11918, 16454, 32768, 0], +]; + +/// the base-range tail of a 4x4 chroma coefficient. +pub const COEFF_BR_CHROMA_4: [[u16; 5]; 21] = [ + [17394, 24501, 27895, 32768, 0], + [15889, 23420, 27185, 32768, 0], + [11561, 19133, 23870, 32768, 0], + [8285, 14812, 19844, 32768, 0], + [6496, 12043, 16550, 32768, 0], + [4771, 9574, 13677, 32768, 0], + [3603, 6830, 10144, 32768, 0], + [21656, 27704, 30200, 32768, 0], + [21324, 27915, 30511, 32768, 0], + [17327, 25336, 28997, 32768, 0], + [13417, 21381, 26033, 32768, 0], + [10132, 17425, 22338, 32768, 0], + [8580, 15016, 19633, 32768, 0], + [5694, 11477, 16411, 32768, 0], + [24116, 29780, 31450, 32768, 0], + [23853, 29695, 31591, 32768, 0], + [20085, 27614, 30428, 32768, 0], + [15326, 24335, 28575, 32768, 0], + [11814, 19472, 24810, 32768, 0], + [10221, 18611, 24767, 32768, 0], + [7689, 14558, 20321, 32768, 0], +]; + +/// COEFF_BR_CHROMA_4_Q3, q-context 3: the same table as [`COEFF_BR_CHROMA_4`], for `base_q_idx` 121..=255. +pub const COEFF_BR_CHROMA_4_Q3: [[u16; 5]; 21] = [ + [21425, 27952, 30388, 32768, 0], + [18062, 25838, 29034, 32768, 0], + [11956, 19881, 24808, 32768, 0], + [7718, 15000, 20980, 32768, 0], + [5702, 11254, 16143, 32768, 0], + [4898, 9088, 16864, 32768, 0], + [3679, 6776, 11907, 32768, 0], + [23294, 30160, 31663, 32768, 0], + [24397, 29896, 31836, 32768, 0], + [19245, 27128, 30593, 32768, 0], + [13202, 19825, 26404, 32768, 0], + [11578, 19297, 23957, 32768, 0], + [8073, 13297, 21370, 32768, 0], + [5461, 10923, 19745, 32768, 0], + [27367, 30521, 31934, 32768, 0], + [24904, 30671, 31940, 32768, 0], + [23075, 28460, 31299, 32768, 0], + [14400, 23658, 30417, 32768, 0], + [13885, 23882, 28325, 32768, 0], + [14746, 22938, 27853, 32768, 0], + [5461, 16384, 27307, 32768, 0], +]; + +/// `Default_Intra_Ext_Tx_Cdf`'s `TX_SET_INTRA_1` (`EXT_TX_SET_DTT4_IDTX_1DDCT`) +/// row for `TX_8X8` (spec 9.4): the transform type of an intra 8x8 transform +/// when `reduced_tx_set` is false. Unused while this crate's key frames set +/// `reduced_tx_set: true` (`get_tx_set`'s intra branch then returns +/// `TX_SET_INTRA_2` -- [`INTRA_TX_TYPE_SET2_8`] -- at every size up to 16x16, +/// not only 16x16; r11 lane-av1-rect). Kept named for the day a non-reduced +/// path exists. +pub const INTRA_TX_TYPE_SET1_8: [[u16; 8]; 13] = [ + [1870, 13742, 14530, 16498, 23770, 27698, 32768, 0], + [326, 8796, 14632, 15079, 19272, 27486, 32768, 0], + [484, 7576, 7712, 14443, 19159, 22591, 32768, 0], + [1126, 15340, 15895, 17023, 20896, 30279, 32768, 0], + [655, 4854, 5249, 5913, 22099, 27138, 32768, 0], + [1299, 6458, 8885, 9290, 14851, 25497, 32768, 0], + [311, 5295, 5552, 6885, 16107, 22672, 32768, 0], + [883, 8059, 8270, 11258, 17289, 21549, 32768, 0], + [741, 7580, 9318, 10345, 16688, 29046, 32768, 0], + [110, 7406, 7915, 9195, 16041, 23329, 32768, 0], + [363, 7974, 9357, 10673, 15629, 24474, 32768, 0], + [153, 7647, 8112, 9936, 15307, 19996, 32768, 0], + [3511, 6332, 11165, 15335, 19323, 23594, 32768, 0], +]; + +/// `Default_Intra_Ext_Tx_Cdf`'s `TX_SET_INTRA_2` (`EXT_TX_SET_DTT4_IDTX`) row +/// for `TX_8X8` (spec 9.4, entropymode.c's `eset==2`/`TX_8X8` slot): the +/// transform type of an intra 8x8 transform when `reduced_tx_set` is true, +/// which this crate's key frames always set. libaom leaves this exact slot +/// at the flat 5-way default (unlike its trained `TX_16X16` neighbour, +/// [`INTRA_TX_TYPE_SET2_16`]) -- every mode reads the same uniform CDF. +pub const INTRA_TX_TYPE_SET2_8: [[u16; 6]; 13] = [[6554, 13107, 19661, 26214, 32768, 0]; 13]; diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 0aedb134..feaaa976 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -37,8 +37,21 @@ pub(crate) enum TxbSet { /// reads, but its own two-symbol `tx_type` table rather than `Luma16`'s /// five-symbol, mode-indexed one. Luma16Inter, + /// The 8x8 luma transform of an 8x8 leaf under a straddling 16x16 block + /// (lane-av1-rect): intra-only, so no `Luma8Inter` counterpart exists yet. + Luma8, /// The 8x8 transform of a chroma plane of a 16x16 block. Chroma8, + /// The 4x4 transform of a chroma plane of an 8x8 leaf (lane-av1-rect). + /// r5 correction: libaom's `is_chroma_reference` (`av1_common_int.h`) is + /// unconditionally true for `bsize >= BLOCK_8X8`, so chroma *does* follow + /// luma all the way down to 8x8 — it is coded once per 8x8 leaf, not once + /// per parent 16x16 (that "stays at the parent's Chroma8 granularity" + /// framing this doc carried through r4 was never checked against a real + /// decoder and is wrong; only `BLOCK_4X4`/`4X8`/`8X4`, one mi-unit wide or + /// high, get the "last sub-block only" chroma-merge treatment). A real + /// writer for the straddling-16x16 case must use this variant per leaf. + Chroma4, /// The 16x16 transform of a chroma plane of a 32x32 block. Chroma16, /// The 32x32 transform of a chroma plane of a whole superblock. @@ -89,30 +102,58 @@ pub(crate) struct Cdfs { pub angle_delta: [[u16; 8]; 8], /// The one-context all-zero flag of a 16x16 luma transform. pub txb_skip_luma_16: [[u16; 3]; 1], + /// The one-context all-zero flag of an 8x8 luma transform. + pub txb_skip_luma_8: [[u16; 3]; 1], /// The all-zero flag of an 8x8 chroma transform. pub txb_skip_chroma_8: [[u16; 3]; 3], + /// The all-zero flag of a 4x4 chroma transform. + pub txb_skip_chroma_4: [[u16; 3]; 3], /// The end-of-block group of a 16x16 luma transform. pub eob_pt_256_luma: [u16; 10], + /// The end-of-block group of an 8x8 luma transform. + pub eob_pt_64_luma: [u16; 8], /// The end-of-block group of an 8x8 chroma transform. pub eob_pt_64_chroma: [u16; 8], + /// The end-of-block group of a 4x4 chroma transform. + pub eob_pt_16_chroma: [u16; 6], /// The end-of-block offset bit of a 16x16 luma transform. pub eob_extra_luma_16: [[u16; 3]; 9], + /// The same, for an 8x8 luma transform. + pub eob_extra_luma_8: [[u16; 3]; 9], /// The same, for an 8x8 chroma transform. pub eob_extra_chroma_8: [[u16; 3]; 9], + /// The same, for a 4x4 chroma transform. + pub eob_extra_chroma_4: [[u16; 3]; 9], /// The base level of a 16x16 luma coefficient. pub base_luma_16: [[u16; 5]; 42], + /// The base level of an 8x8 luma coefficient. + pub base_luma_8: [[u16; 5]; 42], /// The base level of an 8x8 chroma coefficient. pub base_chroma_8: [[u16; 5]; 42], + /// The base level of a 4x4 chroma coefficient. + pub base_chroma_4: [[u16; 5]; 42], /// The last coefficient's base level, for a 16x16 luma transform. pub base_eob_luma_16: [[u16; 4]; 4], + /// The same, for an 8x8 luma transform. + pub base_eob_luma_8: [[u16; 4]; 4], /// The same, for an 8x8 chroma transform. pub base_eob_chroma_8: [[u16; 4]; 4], + /// The same, for a 4x4 chroma transform. + pub base_eob_chroma_4: [[u16; 4]; 4], /// The base-range tail of a 16x16 luma coefficient. pub br_luma_16: [[u16; 5]; 21], + /// The base-range tail of an 8x8 luma coefficient. + pub br_luma_8: [[u16; 5]; 21], /// The base-range tail of an 8x8 chroma coefficient. pub br_chroma_8: [[u16; 5]; 21], + /// The base-range tail of a 4x4 chroma coefficient. + pub br_chroma_4: [[u16; 5]; 21], /// The partition symbol of a 16x16 block. pub partition_w16: [[u16; 11]; 4], + /// The partition symbol of an 8x8 block (lane-av1-rect): only + /// `PARTITION_NONE` is ever coded against it, but the alphabet still + /// adapts on every read, same as every other partition table. + pub partition_w8: [[u16; 5]; 4], /// The one-context all-zero flag of a 32x32 luma transform. pub txb_skip_luma_32: [[u16; 3]; 1], /// The same, for a 64x64 luma transform. @@ -162,6 +203,8 @@ pub(crate) struct Cdfs { pub dc_sign_luma: [[u16; 3]; 3], /// The transform type of a 16x16 intra luma transform, by luma mode. pub intra_tx_type_16: [[u16; 6]; 13], + /// The transform type of an 8x8 intra luma transform, by luma mode. + pub intra_tx_type_8: [[u16; 6]; 13], /// The transform type of an `is_inter` 32x32 luma transform (spec /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it /// is not indexed by mode. @@ -262,6 +305,13 @@ impl Cdfs { cdf::TXB_SKIP_LUMA_16, cdf::TXB_SKIP_LUMA_16_Q3, )], + txb_skip_luma_8: [pick( + q_ctx, + cdf::TXB_SKIP_LUMA_8_Q0, + cdf::TXB_SKIP_LUMA_8_Q1, + cdf::TXB_SKIP_LUMA_8, + cdf::TXB_SKIP_LUMA_8_Q3, + )], txb_skip_chroma_8: pick( q_ctx, cdf::TXB_SKIP_CHROMA_8_Q0, @@ -269,6 +319,13 @@ impl Cdfs { cdf::TXB_SKIP_CHROMA_8, cdf::TXB_SKIP_CHROMA_8_Q3, ), + txb_skip_chroma_4: pick( + q_ctx, + cdf::TXB_SKIP_CHROMA_4_Q0, + cdf::TXB_SKIP_CHROMA_4_Q1, + cdf::TXB_SKIP_CHROMA_4, + cdf::TXB_SKIP_CHROMA_4_Q3, + ), eob_pt_256_luma: pick( q_ctx, cdf::EOB_PT_256_LUMA_Q0, @@ -276,6 +333,13 @@ impl Cdfs { cdf::EOB_PT_256_LUMA, cdf::EOB_PT_256_LUMA_Q3, ), + eob_pt_64_luma: pick( + q_ctx, + cdf::EOB_PT_64_LUMA_Q0, + cdf::EOB_PT_64_LUMA_Q1, + cdf::EOB_PT_64_LUMA, + cdf::EOB_PT_64_LUMA_Q3, + ), eob_pt_64_chroma: pick( q_ctx, cdf::EOB_PT_64_CHROMA_Q0, @@ -283,6 +347,13 @@ impl Cdfs { cdf::EOB_PT_64_CHROMA, cdf::EOB_PT_64_CHROMA_Q3, ), + eob_pt_16_chroma: pick( + q_ctx, + cdf::EOB_PT_16_CHROMA_Q0, + cdf::EOB_PT_16_CHROMA_Q1, + cdf::EOB_PT_16_CHROMA, + cdf::EOB_PT_16_CHROMA_Q3, + ), eob_extra_luma_16: pick( q_ctx, cdf::EOB_EXTRA_LUMA_16_Q0, @@ -290,6 +361,13 @@ impl Cdfs { cdf::EOB_EXTRA_LUMA_16, cdf::EOB_EXTRA_LUMA_16_Q3, ), + eob_extra_luma_8: pick( + q_ctx, + cdf::EOB_EXTRA_LUMA_8_Q0, + cdf::EOB_EXTRA_LUMA_8_Q1, + cdf::EOB_EXTRA_LUMA_8, + cdf::EOB_EXTRA_LUMA_8_Q3, + ), eob_extra_chroma_8: pick( q_ctx, cdf::EOB_EXTRA_CHROMA_8_Q0, @@ -297,6 +375,13 @@ impl Cdfs { cdf::EOB_EXTRA_CHROMA_8, cdf::EOB_EXTRA_CHROMA_8_Q3, ), + eob_extra_chroma_4: pick( + q_ctx, + cdf::EOB_EXTRA_CHROMA_4_Q0, + cdf::EOB_EXTRA_CHROMA_4_Q1, + cdf::EOB_EXTRA_CHROMA_4, + cdf::EOB_EXTRA_CHROMA_4_Q3, + ), base_luma_16: pick( q_ctx, cdf::COEFF_BASE_LUMA_16_Q0, @@ -304,6 +389,13 @@ impl Cdfs { cdf::COEFF_BASE_LUMA_16, cdf::COEFF_BASE_LUMA_16_Q3, ), + base_luma_8: pick( + q_ctx, + cdf::COEFF_BASE_LUMA_8_Q0, + cdf::COEFF_BASE_LUMA_8_Q1, + cdf::COEFF_BASE_LUMA_8, + cdf::COEFF_BASE_LUMA_8_Q3, + ), base_chroma_8: pick( q_ctx, cdf::COEFF_BASE_CHROMA_8_Q0, @@ -311,6 +403,13 @@ impl Cdfs { cdf::COEFF_BASE_CHROMA_8, cdf::COEFF_BASE_CHROMA_8_Q3, ), + base_chroma_4: pick( + q_ctx, + cdf::COEFF_BASE_CHROMA_4_Q0, + cdf::COEFF_BASE_CHROMA_4_Q1, + cdf::COEFF_BASE_CHROMA_4, + cdf::COEFF_BASE_CHROMA_4_Q3, + ), base_eob_luma_16: pick( q_ctx, cdf::COEFF_BASE_EOB_LUMA_16_Q0, @@ -318,6 +417,13 @@ impl Cdfs { cdf::COEFF_BASE_EOB_LUMA_16, cdf::COEFF_BASE_EOB_LUMA_16_Q3, ), + base_eob_luma_8: pick( + q_ctx, + cdf::COEFF_BASE_EOB_LUMA_8_Q0, + cdf::COEFF_BASE_EOB_LUMA_8_Q1, + cdf::COEFF_BASE_EOB_LUMA_8, + cdf::COEFF_BASE_EOB_LUMA_8_Q3, + ), base_eob_chroma_8: pick( q_ctx, cdf::COEFF_BASE_EOB_CHROMA_8_Q0, @@ -325,6 +431,13 @@ impl Cdfs { cdf::COEFF_BASE_EOB_CHROMA_8, cdf::COEFF_BASE_EOB_CHROMA_8_Q3, ), + base_eob_chroma_4: pick( + q_ctx, + cdf::COEFF_BASE_EOB_CHROMA_4_Q0, + cdf::COEFF_BASE_EOB_CHROMA_4_Q1, + cdf::COEFF_BASE_EOB_CHROMA_4, + cdf::COEFF_BASE_EOB_CHROMA_4_Q3, + ), br_luma_16: pick( q_ctx, cdf::COEFF_BR_LUMA_16_Q0, @@ -332,6 +445,13 @@ impl Cdfs { cdf::COEFF_BR_LUMA_16, cdf::COEFF_BR_LUMA_16_Q3, ), + br_luma_8: pick( + q_ctx, + cdf::COEFF_BR_LUMA_8_Q0, + cdf::COEFF_BR_LUMA_8_Q1, + cdf::COEFF_BR_LUMA_8, + cdf::COEFF_BR_LUMA_8_Q3, + ), br_chroma_8: pick( q_ctx, cdf::COEFF_BR_CHROMA_8_Q0, @@ -339,7 +459,15 @@ impl Cdfs { cdf::COEFF_BR_CHROMA_8, cdf::COEFF_BR_CHROMA_8_Q3, ), + br_chroma_4: pick( + q_ctx, + cdf::COEFF_BR_CHROMA_4_Q0, + cdf::COEFF_BR_CHROMA_4_Q1, + cdf::COEFF_BR_CHROMA_4, + cdf::COEFF_BR_CHROMA_4_Q3, + ), partition_w16: cdf::PARTITION_W16, + partition_w8: cdf::PARTITION_W8, txb_skip_luma_32: [pick( q_ctx, cdf::TXB_SKIP_LUMA_32_Q0, @@ -496,6 +624,7 @@ impl Cdfs { ), dc_sign_luma: cdf::DC_SIGN_LUMA, intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, + intra_tx_type_8: cdf::INTRA_TX_TYPE_SET2_8, inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32, inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16, dc_sign_chroma: cdf::DC_SIGN_CHROMA, @@ -569,6 +698,17 @@ impl Cdfs { dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.inter_tx_type_16.as_mut_slice()), }, + TxbSet::Luma8 => TxbTables { + side: 8, + txb_skip: &mut self.txb_skip_luma_8, + eob_pt: &mut self.eob_pt_64_luma, + eob_extra: &mut self.eob_extra_luma_8, + base: &mut self.base_luma_8, + base_eob: &mut self.base_eob_luma_8, + br: &mut self.br_luma_8, + dc_sign: &mut self.dc_sign_luma, + tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()), + }, TxbSet::Chroma8 => TxbTables { side: 8, txb_skip: &mut self.txb_skip_chroma_8, @@ -580,6 +720,17 @@ impl Cdfs { dc_sign: &mut self.dc_sign_chroma, tx_type: None, }, + TxbSet::Chroma4 => TxbTables { + side: 4, + txb_skip: &mut self.txb_skip_chroma_4, + eob_pt: &mut self.eob_pt_16_chroma, + eob_extra: &mut self.eob_extra_chroma_4, + base: &mut self.base_chroma_4, + base_eob: &mut self.base_eob_chroma_4, + br: &mut self.br_chroma_4, + dc_sign: &mut self.dc_sign_chroma, + tx_type: None, + }, TxbSet::Chroma16 => TxbTables { side: 16, txb_skip: &mut self.txb_skip_chroma_16, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 4d0cfb07..676567eb 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -431,6 +431,19 @@ pub(crate) fn key_frame_headers_colour( ..QuantizationParams::default() }, tx_mode: TxMode::Largest, + // Forces `get_tx_set` (spec 5.11.48) to `TX_SET_INTRA_2` for every + // intra transform below 32x32, not only 16x16 -- the set this crate + // carries a table for at 8x8/16x16 (`INTRA_TX_TYPE_SET2_8`/`_16`). + // This bit is part of the bitstream (spec 5.9.2's `reduced_tx_set`), + // so a decoder honouring the header picks the same CDF the writer + // used only if this is set here too, not only on the inter variant + // below -- an r14 lane-av1-rect fix: a key frame's own header used to + // default this to `false`, so `av1_get_ext_tx_set_type` picked the + // 7-symbol `TX_SET_INTRA_1` table on the decoder side while the + // writer coded against the 5-symbol `TX_SET_INTRA_2` one, diverging + // the arithmetic coder's `rng` register from the very first 8x8-leaf + // luma transform onward with no symbol value ever differing. + reduced_tx_set: true, ..FrameHeader::default() }; Ok((seq, header)) @@ -472,10 +485,12 @@ pub fn inter_frame_headers( // set this crate carries a table for at 16x16 (see // `crate::cdf::INTER_TX_TYPE_SET3_16`'s doc comment). A 32x32 inter // transform is unaffected (its `txSzSqrUp == TX_32X32` branch already - // reads `TX_SET_INTER_3` regardless of this flag), and neither is any - // intra transform at any size (`get_tx_set`'s intra branch returns - // the same `TX_SET_INTRA_2` at 16x16 with or without it), so this - // only narrows the 16x16-and-smaller inter case this crate is adding. + // reads `TX_SET_INTER_3` regardless of this flag). An intra transform + // IS affected below 16x16 too, not only at 16x16 (r11 lane-av1-rect: + // the earlier claim here was wrong and desynced the 8x8-leaf straddle + // path) -- with this flag set, `get_tx_set`'s intra branch returns + // `TX_SET_INTRA_2` at every size up to 16x16, so `TX_8X8` reads + // [`crate::cdf::INTRA_TX_TYPE_SET2_8`], not `SET1_8`. reduced_tx_set: true, ..key }; @@ -946,11 +961,28 @@ pub(crate) struct Reach { /// it. Indexed by the block's position in the superblock, in blocks of its own /// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's /// 128, not this crate's 64 superblock -- bit by bit from the low end. -const HAS_TOP_RIGHT: [&[u8]; 2] = [&[255, 85, 119, 85, 127, 85, 119, 85], &[95, 87]]; +const HAS_TOP_RIGHT: [&[u8]; 3] = [ + &[255, 85, 119, 85, 127, 85, 119, 85], + &[95, 87], + // has_tr_8x8 (libaom av1/common/reconintra.c) -- table_stride(8) == 16 + // (128 / 8), so this row is 16 wide by 16 rows, 32 bytes. + &[ + 255, 255, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, 255, 127, 85, 85, + 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, + ], +]; /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the /// left. -const HAS_BOTTOM_LEFT: [&[u8]; 2] = [&[84, 16, 84, 0, 84, 16, 84, 0], &[4, 4]]; +const HAS_BOTTOM_LEFT: [&[u8]; 3] = [ + &[84, 16, 84, 0, 84, 16, 84, 0], + &[4, 4], + // has_bl_8x8 (libaom av1/common/reconintra.c) + &[ + 84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0, 84, 85, 16, 17, 84, 85, 0, 1, + 84, 85, 16, 17, 84, 85, 0, 0, + ], +]; impl Reach { /// What a block of `side` samples at `(x, y)` may read past its own edges, @@ -1029,9 +1061,15 @@ impl Reach { 128 / side } - /// Which of the two block sizes the encoder codes a table row is for. + /// Which of the three block sizes the encoder codes a table row is for. fn table(side: usize) -> usize { - usize::from(side == BLOCK) + if side == BLOCK { + 1 + } else if side == 8 { + 2 + } else { + 0 + } } /// One bit of a table, counting from the low end of its first byte. @@ -1069,6 +1107,15 @@ fn code_square( ) -> (BlockCoeffs, f64) { let (luma_set, chroma_set) = if side == BLOCK { (TxbSet::Luma32, TxbSet::Chroma16) + } else if side == 8 { + // An 8x8 leaf under a straddling 16x16 (lane-av1-rect). r5 correction: + // `TxbSet::Chroma4`'s doc now has the checked spec fact -- chroma is + // `is_chroma_reference` at every BLOCK_8X8, so it is coded per leaf at + // 4x4, not once at the parent's 8x8. No caller passes `side == 8` yet + // (this branch is still unreached), so leaving `Chroma8` here would + // silently wire the wrong table the moment one does; `Chroma4` is + // what a real caller needs. + (TxbSet::Luma8, TxbSet::Chroma4) } else { (TxbSet::Luma16, TxbSet::Chroma8) }; @@ -1253,6 +1300,7 @@ fn code_square_inter( v: coeffs(&v.levels, SUB / 2), mode: DC_PRED as u8, skip, + eight: None, inter: Some(InterInfo { mode: InterMode::NearestMv, mv, @@ -1656,24 +1704,37 @@ pub(crate) fn encode_key_frame_inner( ), ); let whole_legal = has_cols32 && has_rows32; - let split_legal = (0..4) - .map(|i| (r0 + i / 2, c0 + i % 2)) - .filter(|&(sr, sc)| { - (sr as u32) * crate::tile::SUB_MI < header.mi_rows - && (sc as u32) * crate::tile::SUB_MI < header.mi_cols - }) - .all(|(sr, sc)| { + // A 16x16 sub-block's own hasCols/hasRows (spec + // `decode_partition`, recomputed at this leaf's own half): one + // axis false is a straddling leaf this writer codes as two + // 8x8 leaves (`crate::tile::write_leaf8`, lane-av1-rect r7); + // both axes false needs a rectangular transform this writer + // has no size for at all, whether whole or split. + let sub_half = |sr: usize, sc: usize| { + ( crate::tile::has_half( sc as u32 * crate::tile::SUB_MI, crate::tile::SUB_MI, header.mi_cols, - ) && crate::tile::has_half( + ), + crate::tile::has_half( sr as u32 * crate::tile::SUB_MI, crate::tile::SUB_MI, header.mi_rows, - ) + ), + ) + }; + let both_axes_cut = (0..4) + .map(|i| (r0 + i / 2, c0 + i % 2)) + .filter(|&(sr, sc)| { + (sr as u32) * crate::tile::SUB_MI < header.mi_rows + && (sc as u32) * crate::tile::SUB_MI < header.mi_cols + }) + .any(|(sr, sc)| { + let (has_cols16, has_rows16) = sub_half(sr, sc); + !has_cols16 && !has_rows16 }); - if !whole_legal && !split_legal { + if !whole_legal && both_axes_cut { return Err(Error::unsupported( "AV1 encode", format!( @@ -1717,22 +1778,67 @@ pub(crate) fn encode_key_frame_inner( { continue; } - let (block, cost) = code_square( - &mut luma, - &mut chroma, - (x + (sub % 2) * SUB, y + (sub / 2) * SUB), - SUB, - &search, - &mode_bits(above_mode[sc], left_mode[sr]), - ); - cost_split += cost; - split_modes.push(block.mode); - above_mode[sc] = block.mode; - left_mode[sr] = block.mode; - split.push(block); + let (has_cols16, has_rows16) = sub_half(sr, sc); + if has_cols16 && has_rows16 { + let (block, cost) = code_square( + &mut luma, + &mut chroma, + (x + (sub % 2) * SUB, y + (sub / 2) * SUB), + SUB, + &search, + &mode_bits(above_mode[sc], left_mode[sr]), + ); + cost_split += cost; + split_modes.push(block.mode); + above_mode[sc] = block.mode; + left_mode[sr] = block.mode; + split.push(block); + } else { + // A straddling 16x16: two (or, at a true corner, one) + // 8x8 leaves, in the raster order `write_leaf8`'s + // caller expects. Both leaves cost against the SAME + // enclosing-slot mode context (see + // `crate::tile::write_leaf8`'s doc), and neither + // updates `above_mode`/`left_mode` at this SUB slot -- + // the writer never does either, so the search must + // not diverge from what it will actually read next. + let leaf_mode_bits = mode_bits(above_mode[sc], left_mode[sr]); + let (x_sub, y_sub) = (x + (sub % 2) * SUB, y + (sub / 2) * SUB); + let mut leaves = Vec::with_capacity(2); + for i in 0..4 { + let leaf_x = x_sub + (i % 2) * 8; + let leaf_y = y_sub + (i / 2) * 8; + if leaf_x >= luma.true_width || leaf_y >= luma.true_height { + continue; + } + let (leaf, cost) = code_square( + &mut luma, + &mut chroma, + (leaf_x, leaf_y), + 8, + &search, + &leaf_mode_bits, + ); + cost_split += cost; + split_modes.push(leaf.mode); + leaves.push(leaf); + } + split.push(BlockCoeffs { + eight: Some(leaves), + ..BlockCoeffs::default() + }); + } } - if split_legal && (!whole_legal || (split_blocks && cost_split < cost_whole)) { + // A split whose subs are all whole 16x16 is a real quality + // alternative to `whole`, exactly as before; a split + // carrying an 8x8-leaf sub was, through r14, only ever + // forced (`!whole_legal`), never chosen on cost, because the + // leaf path was not yet proven against a real decoder. r15 + // closed that gap (`a_single_axis_straddle_round_trips_ + // through_ffmpeg` decodes clean), so the guard is lifted: + // leaf8 splits now compete on cost like any other split. + if !whole_legal || (split_blocks && cost_split < cost_whole) { modes.extend_from_slice(&split_modes); blocks.push(Quadrant::Split(split)); } else { @@ -2319,6 +2425,7 @@ fn search_inter_block( mode: best.mode, skip: best.skip, inter: best.inter, + eight: None, } } @@ -2858,6 +2965,7 @@ mod tests { v: Vec::new(), mode: 0, skip: false, + eight: None, inter: Some(InterInfo { mode: InterMode::NearestMv, mv: (0, 0), @@ -3207,23 +3315,25 @@ mod tests { } } - /// 640x360 is the class the charter asks be kept consistent between the - /// key frame and inter frame writers: 360 mod 32 == 8, so a 16x16 leaf's - /// own half (`has_half` at the SUB level) itself straddles the true edge - /// -- the genuine "needs an 8x8/rectangular transform" case neither - /// writer codes. Both must refuse it the same way. + /// 640x360 is the class the charter names: 360 mod 32 == 8, so a 16x16 + /// leaf's own half (`has_half` at the SUB level) itself straddles the + /// true edge on one axis only. lane-av1-rect r7 wires the key frame + /// writer's 8x8-leaf path (`crate::tile::write_leaf8`) live for exactly + /// this case, so the key frame path no longer *refuses* it; the inter + /// frame path (`sb_coeff_inter_frame_tile`) was not part of this round's + /// wiring and still has no leaf-8 generation, so it must still refuse. + /// This only proves the encoder does not error out -- the leaf-8 stream + /// it produces is NOT yet proven decoder-clean (see + /// `a_single_axis_straddle_round_trips_through_ffmpeg`, currently + /// `#[ignore]`d on a genuine dav1d desync at the smaller 40x32 case). #[test] - fn both_writers_refuse_the_same_strictly_before_half_straddle_size() { + fn key_frame_encodes_the_inter_frame_still_refuses_at_half_straddle_size() { let (width, height) = (640usize, 360usize); let picture = Picture::grey(width, height); - let key_err = encode_key_frame(&picture, 100, 0.5).unwrap_err(); - assert!( - key_err.to_string().contains("rectangular"), - "key frame refusal: {key_err}" - ); + encode_key_frame(&picture, 100, 0.5).expect("key frame now codes the mod-32==8 straddle"); // `encode_sequence`'s second frame is the inter path: same size, - // same class of refusal expected. + // still refused -- that machinery was not wired this round. let pictures = vec![picture.clone(), picture]; let inter_err = encode_sequence(&pictures, 100, 0.5).unwrap_err(); assert!( @@ -3232,6 +3342,31 @@ mod tests { ); } + /// The r15 fix at production size: three 640x360 key frames (inter is + /// still refused at this size, per the test above, so this stands in for + /// the charter's "3-frame sequence" using three independent key frames of + /// shifted content), each proven decoder-clean through ffmpeg rather than + /// only encoder-clean. + #[test] + fn a_640x360_half_straddle_round_trips_through_ffmpeg_across_three_frames() { + if !have_ffmpeg() { + eprintln!( + "SKIP a_640x360_half_straddle_round_trips_through_ffmpeg_across_three_frames: \ + no ffmpeg" + ); + return; + } + let (width, height) = (640usize, 360usize); + for shift in [0i64, 7, 19] { + let picture = panned_test_card(width, height, shift); + let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); + let decoded = ffmpeg_decode(&encoded.stream, width, height); + assert_eq!(decoded.y, encoded.reconstruction.y, "shift {shift}: luma"); + assert_eq!(decoded.u, encoded.reconstruction.u, "shift {shift}: U"); + assert_eq!(decoded.v, encoded.reconstruction.v, "shift {shift}: V"); + } + } + /// One frame of a real clip, scaled to a whole number of 32x32 blocks. fn clip_frame(clip: &str, skip: &str, width: usize, height: usize) -> Picture { let out = Command::new("ffmpeg") @@ -3762,32 +3897,117 @@ mod tests { /// fine now (see `a_frame_round_trips_at_its_own_size`) -- the true frame /// edge lands past the halfway point of whichever block it falls in, so /// `PARTITION_NONE` or a single gathered split flag still says everything - /// the spec needs. 40x32 and 32x48 are the sizes where the edge instead - /// falls *at or before* the halfway point of a 16x16 leaf: the spec's - /// `decode_partition` then requires that leaf to split again, to 8x8, - /// which this writer has no transform/coefficient path for yet, so it is - /// still refused, by name rather than by corrupting the stream. + /// the spec needs. lane-av1-rect r7 wires the 8x8-leaf path live, so a + /// 16x16 leaf straddling on exactly one axis (e.g. 40x32, cols mod 32 == + /// 8) now encodes too (see `a_frame_round_trips_at_its_own_size`'s + /// sibling below). Only a leaf whose true edge cuts *both* axes (e.g. + /// 40x40, both dims mod 32 == 8) needs the rectangular transform neither + /// writer has, so that class is still refused, by name rather than by + /// corrupting the stream. #[test] fn a_picture_off_the_block_grid_is_refused() { - let refused = |w, h| { - let msg = encode_key_frame(&Picture::grey(w, h), 100, 0.5) - .expect_err(&format!( - "{w}x{h} needs an 8x8 split this writer does not code" - )) - .to_string(); - msg.contains("true frame") - }; - assert!(refused(40, 32)); - // 32x48 was refused here through round 9: its split quadrant only - // ever has two legal 16x16 subs (the other two sit past the true - // 48-row edge, block-aligned, no 8x8 split needed), but the writer - // demanded four. See `ffmpeg_decodes_exactly_what_the_encoder_ - // reconstructed`, which now round-trips it pixel-exact instead. + let msg = encode_key_frame(&Picture::grey(40, 40), 100, 0.5) + .expect_err("40x40 straddles both axes of a 16x16 leaf, needs a rectangular transform") + .to_string(); + assert!(msg.contains("rectangular"), "refusal: {msg}"); + let mut short = Picture::grey(64, 64); short.u.truncate(10); assert!(encode_key_frame(&short, 100, 0.5).is_err()); } + /// 40x32: cols mod 32 == 8, so the true edge cuts a 16x16 leaf's own + /// column half only (`has_half` false on cols, true on rows) -- exactly + /// the single-axis straddle lane-av1-rect r7 wires through + /// `crate::tile::write_leaf8`'s 8x8 leaves. Not yet decoder-clean: + /// dav1d rejects the stream ("Invalid data found"). r7's falsification + /// candidate (`partition_w8`'s ctx reading the enclosing 16x16 slot) is + /// FALSIFIED by r8's rng trace against a debug `aomdec`: libaom's own + /// `partition_plane_context` masks at `bsl = 0` for `BLOCK_8X8`, and every + /// `partition_context_lookup` entry this writer ever produces (no 4x4 + /// splits exist) has bit 0 clear, so the ctx is provably 0 for every + /// leaf regardless of neighbour state -- our writer already agreed with + /// the decoder here (`ctx=0` both sides). r8 still ported the mi-precise + /// tracking (`Neighbours::{above,left}_side_mi`, `partition_ctx_mi`) as a + /// spec-accurate fix in its own right (it matters once 4x4 exists), and + /// it does not regress the gate. r11 tell-for-tell traced two further, + /// real bugs and fixed both: (1) `write_leaf8`'s intra-mode context read + /// the *enclosing* 16x16 slot's stale `above_mode`/`left_mode` for both + /// leaves, when the second leaf's true above (or left) neighbour is the + /// first leaf itself -- fixed by threading the first leaf's mode into the + /// second leaf's context, mi-precise, like `record_mi` already does for + /// coefficients; (2) `TxbSet::Luma8`'s transform-type table was + /// `INTRA_TX_TYPE_SET1_8` (`TX_SET_INTRA_1`, 7-way, correct only when + /// `reduced_tx_set` is false), but this crate's key frames set + /// `reduced_tx_set: true`, which per `get_tx_set` (spec 5.11.48) puts + /// *every* intra size up to 16x16 -- not only 16x16 -- on `TX_SET_INTRA_2` + /// (5-way); the wrong cardinality read enough of the stream as `TX_TYPE` + /// symbol bits to desync everything after it. Fixed with a new + /// `INTRA_TX_TYPE_SET2_8` (libaom's flat default for that exact + /// eset/size slot). Both fixes are provably correct against libaom + /// source and each closed part of the gap (leaf0's `eob` went from a + /// desynced 36 to a near-miss 3, writer's real eob is 7), but a residual + /// divergence remained in the `EOB_PT` symbol's decoded value despite an + /// aligned bit cost. r12 traced that residual with a fresh dual-trace + /// (do not reuse r11's committed logs, which predate its own fixes) and + /// found two more real bugs, both now fixed: (3) `write_leaf8` hardcoded + /// `cfl: false` for `write_intra_mode`'s chroma-mode CDF choice, but + /// `is_cfl_allowed` (spec 5.11.5) is true for any block `<= 32x32` -- + /// every other `write_block` caller at 16x16 and up already passes + /// `true`; the narrower `uv_mode_no_cfl` alphabet is one symbol short of + /// `uv_mode_cfl`, so even an identical `DC_PRED` decision consumed a + /// different amount of arithmetic range and derailed everything after + /// it. (4) `record()`'s `above_mode`/`left_mode` write is a no-op at an + /// 8x8 leaf's own side (`side / SUB == 0` for `SUB` = 16), and + /// `write_leaf8` called only `record_mi` (the coefficient-context half), + /// never updating those coarse arrays itself -- so a *second* straddling + /// 16x16 quadrant stacked above or left of the first (never covered by + /// r11's same-call `prev_leaf` override, which only threads state + /// between the two leaves of one shared quadrant) read whatever stale + /// mode sat in that slot before either leaf ran. Fixed by writing the + /// leaf's own mode into `above_mode[c]`/`left_mode[r]` directly. Fixing + /// (3) alone shrank the tell drift after leaf(0,8) from a decoder + /// `eob_pt=3` (wrong) vs the writer's real `eob_pt=4` down to a `tell` + /// offset of a single unit; fixing (3)+(4) together still leaves a + /// residual divergence one leaf further in -- the decoder now reads + /// leaf(2,8)'s own luma coefficient block as `txs_ctx=1` (a 32-sample + /// transform) where the writer intends `TX_8X8` (`txs_ctx=2`), meaning + /// something *before* this leaf's own `EC_TXBSKIP`/`EC_TXTYPE` still + /// desyncs the stream by exactly the width of one earlier symbol -- not + /// yet isolated to a single named symbol. r15 found the last bug: the + /// header fix from r14 explained the rng skew fully, and the remaining + /// desync (still past `mi_row=4`) was `write_leaf8`'s (4) fix itself -- + /// its `above_mode[c]`/`left_mode[r]` forced write ran *inside* the leaf + /// loop, once per leaf, so the first leaf's write clobbered the true + /// external neighbour mode the second leaf of the *same* straddling + /// quadrant still needed for its non-adjacency axis (a column-straddle + /// case: two leaves share one `outer_at` column index, so leaf 1's + /// write to `left_mode[r]` overwrote the real western neighbour before + /// leaf 2 read it). Fixed by moving the write out of `write_leaf8` and + /// doing it once, after the whole leaf loop, from the last (bottom/ + /// right-most) leaf's mode. The stream now round-trips through ffmpeg. + #[test] + fn a_single_axis_straddle_round_trips_through_ffmpeg() { + if !have_ffmpeg() { + eprintln!("SKIP a_single_axis_straddle_round_trips_through_ffmpeg: no ffmpeg"); + return; + } + let (width, height) = (40usize, 32usize); + let picture = test_card(width, height); + let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); + assert_eq!( + (encoded.reconstruction.width, encoded.reconstruction.height), + (width, height) + ); + if let Ok(path) = std::env::var("EC_AV1_DUMP") { + std::fs::write(&path, &encoded.stream).expect("dump the raw stream"); + } + let decoded = ffmpeg_decode(&encoded.stream, width, height); + assert_eq!(decoded.y, encoded.reconstruction.y, "luma"); + assert_eq!(decoded.u, encoded.reconstruction.u, "U"); + assert_eq!(decoded.v, encoded.reconstruction.v, "V"); + } + /// A frame of real video, rather than a picture built to be easy. /// /// `EC_AV1_CLIP` names the clip and `EC_AV1_CLIP_SKIP` how far into it to diff --git a/crates/ec-av1/src/msac.rs b/crates/ec-av1/src/msac.rs index c320f1bb..2311a4ac 100644 --- a/crates/ec-av1/src/msac.rs +++ b/crates/ec-av1/src/msac.rs @@ -77,6 +77,13 @@ impl SymbolEncoder { self.bits } + /// The coder's current interval width — libaom's `od_ec_enc_rng` / + /// decoder's `rng` field. Exposed only for the rng-register bisect trace + /// (`EC_RNG=1`); not part of the bitstream contract. + pub(crate) fn rng(&self) -> u32 { + self.rng + } + /// The exact bit position the stream has committed to so far (libaom's /// `od_ec_enc_tell`, `entenc.c`): `cnt + 10` undoes the `-9` `cnt` starts /// at and reserves the one bit `finish` always spends, and `precarry` diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 454107a1..4ac159ab 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -465,6 +465,13 @@ pub struct BlockCoeffs { /// block. Only [`sb_coeff_inter_frame_tile`] codes `is_inter`; the key /// frame writers never read this field. pub inter: Option, + /// The 8x8 leaves a straddling 16x16 block is split into (lane-av1-rect), + /// each with its own 8x8 luma transform and 4x4 chroma transforms, in + /// raster order among the leaves that are inside the true frame. `Some` + /// overrides this `BlockCoeffs`'s own `luma`/`u`/`v`/`mode`, which are left + /// at their defaults and unused, the same way a `Whole` [`Superblock`]'s + /// per-quadrant fields are unused. + pub eight: Option>, } /// One inter mode [`sb_coeff_inter_frame_tile`]'s blocks may take (spec @@ -509,8 +516,11 @@ const BLOCK: usize = 32; /// Side of the chroma transform beside it at 4:2:0, and of the luma transform /// of a 16x16 block. const TX16: usize = 16; -/// Side of the chroma transform of a 16x16 block at 4:2:0. +/// Side of the chroma transform of a 16x16 block at 4:2:0, and of the luma +/// transform of an 8x8 leaf under a straddling 16x16 (lane-av1-rect). const TX8: usize = 8; +/// Side of the chroma transform of an 8x8 leaf at 4:2:0 (lane-av1-rect). +const TX4: usize = 4; /// Side of a superblock, in samples, which is the size a block outside the /// tile reads as. const SB: usize = 64; @@ -606,6 +616,17 @@ struct Neighbours { above_side: Vec, /// The same, down the left edge. left_side: Vec, + /// The same as `above_side`/`left_side`, but kept at the finer mi (4x4) + /// granularity [`Self::above`]/[`Self::left`] are, rather than [`SUB`]: + /// two 8x8 leaves of one straddling 16x16 block (lane-av1-rect) share a + /// single [`SUB`]-grid cell, so a coarse array cannot tell the second + /// leaf's partition symbol that the first one was coded at 8x8 (finer + /// than the 16x16 it sits in) -- exactly what its `above`/`left` context + /// needs to read (spec 9.3's `AbovePartitionContext`/`LeftPartitionContext`, + /// which libaom keeps per mi unit for this reason). + above_side_mi: Vec, + /// The same, down the left edge. + left_side_mi: Vec, /// Whether the neighbour carried no residual, for [`sb_coeff_inter_frame_tile`]'s /// skip context. Unused (and left at its default) by the key frame writers. above_skip: Vec, @@ -653,6 +674,8 @@ impl Neighbours { left_mode: vec![DC_PRED; rows], above_side: vec![SB; cols], left_side: vec![SB; rows], + above_side_mi: vec![SB; cols * (SUB / MI)], + left_side_mi: vec![SB; rows * (SUB / MI)], above_skip: vec![false; cols], left_skip: vec![false; rows], above_inter: vec![false; cols], @@ -667,6 +690,7 @@ impl Neighbours { self.left.iter_mut().for_each(|l| *l = Default::default()); self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); self.left_side.iter_mut().for_each(|s| *s = SB); + self.left_side_mi.iter_mut().for_each(|s| *s = SB); self.left_skip.iter_mut().for_each(|s| *s = false); self.left_inter.iter_mut().for_each(|i| *i = false); } @@ -680,14 +704,30 @@ impl Neighbours { /// side). fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec; 3]) { let (r, c) = at; - let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); for cell in 0..side / SUB { self.above_mode[c + cell] = mode; self.left_mode[r + cell] = mode; self.above_side[c + cell] = side; self.left_side[r + cell] = side; } - let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI); + self.record_mi((r * (SUB / MI), c * (SUB / MI)), side, grids); + } + + /// The coefficient-context half of [`Self::record`], taking the block's + /// position directly in 4x4 mode-info units rather than [`SUB`]-grid + /// (r, c): an 8x8 leaf under a straddling 16x16 (lane-av1-rect) sits at a + /// mi offset [`Self::record`]'s SUB-unit `at` cannot name, but the + /// `above`/`left` arrays it writes into are already sized to the full mi + /// grid, so no resizing is needed to write into them at this + /// finer-than-SUB granularity. + fn record_mi(&mut self, at_mi: (usize, usize), side: usize, grids: &[Vec; 3]) { + let (mi_r, mi_c) = at_mi; + let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); + let side_mi = side / MI; + for cell in 0..side_mi { + self.left_side_mi[mi_r + cell] = side; + self.above_side_mi[mi_c + cell] = side; + } // libaom rounds the luma edge up to the plane's own 4x4 unit before // clamping a subsampled plane (`ROUND_POWER_OF_TWO(max_blocks_high, // subsampling_y)` in av1_write_intra_coeffs_mb, encodetxb.c:456-459): @@ -741,7 +781,13 @@ impl Neighbours { /// The gathered state of the blocks above and to the left of one block, /// per plane. fn around(&self, (r, c): (usize, usize), side: usize) -> [Around; 3] { - let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI); + self.around_mi((r * (SUB / MI), c * (SUB / MI)), side) + } + + /// [`Self::around`] taking the block's position directly in 4x4 mode-info + /// units, for the same reason [`Self::record_mi`] does. + fn around_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> [Around; 3] { + let side_mi = side / MI; std::array::from_fn(|plane| { let mut around = Around::default(); for cell in 0..side_mi { @@ -758,8 +804,23 @@ impl Neighbours { } fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize { + // Delegates to the mi-precise reader (same pattern as `around` / + // `around_mi`): `above_side`/`left_side` are only ever advanced in + // whole-[`SUB`] steps by [`Self::record`], so a leaf8's `record_mi` + // (lane-av1-rect) -- which only touches the finer mi arrays -- leaves + // them stale for the *next* sibling's own partition symbol, reading a + // 16x16-slot side that already split into 8x8s underneath it. let (r, c) = at; - 2 * usize::from(self.left_side[r] * 2 <= side) + usize::from(self.above_side[c] * 2 <= side) + self.partition_ctx_mi((r * (SUB / MI), c * (SUB / MI)), side) + } + + /// [`Self::partition_ctx`] at mi granularity, for an 8x8 leaf of a + /// straddling 16x16 block: reads the finer `above_side_mi`/`left_side_mi` + /// arrays so the second leaf sees the first leaf's own 8x8 side rather + /// than the enclosing 16x16 slot's stale, shared state. + fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize { + 2 * usize::from(self.left_side_mi[mi_r] * 2 <= side) + + usize::from(self.above_side_mi[mi_c] * 2 <= side) } } @@ -800,7 +861,12 @@ pub fn sb_coeff_key_frame_tile( match superblock { Superblock::Whole(block) => coded.push(block), Superblock::Split(quadrants) => { - coded.extend(quadrants.iter().flat_map(Quadrant::blocks)); + for block in quadrants.iter().flat_map(Quadrant::blocks) { + match &block.eight { + Some(leaves) => coded.extend(leaves.iter()), + None => coded.push(block), + } + } } } } @@ -820,7 +886,12 @@ pub fn sb_coeff_key_frame_tile( let sub_planes = [TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8]; let split_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16]; let whole_planes = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; - let scans = [default_scan(TX32), default_scan(TX16), default_scan(TX8)]; + let scans = [ + default_scan(TX32), + default_scan(TX16), + default_scan(TX8), + default_scan(TX4), + ]; // The blocks above and to the left, on the 16x16 grid. The left edge is // reset at every superblock row because a tile starts each row with no // left neighbour. @@ -1027,26 +1098,14 @@ pub fn sb_coeff_key_frame_tile( (false, false) => {} } for (block, (sr, sc)) in blocks.iter().zip(sub_positions) { - // A 16x16 leaf's own hasRows/hasCols: this - // writer has no smaller partition than - // 16x16 (no 8x8 or rectangular transform), - // so a leaf the true edge itself cuts - // through -- legal in the spec only via a - // `PARTITION_HORZ`/`VERT` this writer does - // not code -- is refused rather than - // silently miscoded. + // A 16x16 leaf's own hasRows/hasCols, + // recomputed at this leaf's own half + // (same three-way signaling as the 32x32 + // and 64x64 levels above). let (has_cols16, has_rows16) = ( has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), ); - if !has_cols16 || !has_rows16 { - return Err(Error::unsupported( - "AV1 tile", - "a 16x16 block the true frame edge cuts through \ - needs a rectangular transform this writer does \ - not code yet", - )); - } let at = (sr, sc); let ctx = neighbours.partition_ctx(at, SUB); ec_rng_trace(|| { @@ -1058,32 +1117,156 @@ pub fn sb_coeff_key_frame_tile( enc.tell() ) }); - enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx]); - let grids = [ - level_grid(&block.luma, TX16)?, - level_grid(&block.u, TX8)?, - level_grid(&block.v, TX8)?, - ]; - write_block( - &mut enc, - &mut cdfs, - &mut neighbours, - block, - at, - SUB, - &sub_planes, - &grids, - [&scans[1], &scans[2], &scans[2]], - true, - ); - ec_rng_trace(|| { - format!( - "EC_TOK mi_row={} mi_col={} tell={}", - at.0 * 4, - at.1 * 4, - enc.tell() + if has_cols16 && has_rows16 { + enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx]); + let grids = [ + level_grid(&block.luma, TX16)?, + level_grid(&block.u, TX8)?, + level_grid(&block.v, TX8)?, + ]; + write_block( + &mut enc, + &mut cdfs, + &mut neighbours, + block, + at, + SUB, + &sub_planes, + &grids, + [&scans[1], &scans[2], &scans[2]], + true, + ); + ec_rng_trace(|| { + format!( + "EC_TOK mi_row={} mi_col={} tell={}", + at.0 * 4, + at.1 * 4, + enc.tell() + ) + }); + continue; + } + // The true edge falls inside this 16x16 + // leaf itself (mod-32==8 target sizes): + // one axis only, per r6's charter -- an + // 8x8 leaf never itself straddles, so the + // block splits cleanly along whichever + // axis is short and only the in-frame + // 8x8s are coded, each its own leaf. + if !has_cols16 && !has_rows16 { + return Err(Error::unsupported( + "AV1 tile", + "a 16x16 block whose true edge cuts through both \ + axes needs a rectangular transform this writer \ + does not code yet", + )); + } + if has_cols16 { + enc.symbol_fixed( + 1, + &gather(&cdfs.partition_w16[ctx], VERT_ALIKE), + ); + } else { + enc.symbol_fixed( + 1, + &gather(&cdfs.partition_w16[ctx], HORZ_ALIKE), + ); + } + let leaves = block.eight.as_ref().ok_or_else(|| { + Error::unsupported( + "AV1 tile", + "a 16x16 block the true frame edge cuts through \ + needs its `eight` leaves populated", ) - }); + })?; + let (mi_row0, mi_col0) = + (sr as u32 * SUB_MI, sc as u32 * SUB_MI); + let leaf_positions: Vec<(u32, u32)> = (0..4) + .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2)) + .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols) + .collect(); + if leaves.len() != leaf_positions.len() { + return Err(Error::unsupported( + "AV1 tile", + "a straddling 16x16 block needs one `eight` entry \ + per 8x8 leaf inside the true frame", + )); + } + // r11: the enclosing 16x16 slot's + // above_mode/left_mode arrays are too + // coarse for a second leaf whose true + // above (or left) neighbour is the FIRST + // leaf -- track it here and hand it to + // write_leaf8 as a context override. + let mut prev_leaf: Option<((usize, usize), usize)> = None; + for (leaf, (mr, mc)) in leaves.iter().zip(leaf_positions) { + let leaf_mi = (mr as usize, mc as usize); + // r8: read at mi granularity, not the + // enclosing 16x16 slot -- the first + // leaf's `record_mi` call below + // updates `above_side_mi`/ + // `left_side_mi` at this leaf's own + // mi position, which the second + // leaf's ctx lookup then sees. + let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); + ec_rng_trace(|| { + format!( + "EC_PART mi_row={} mi_col={} bsize=3 ctx={} tell={}", + leaf_mi.0, + leaf_mi.1, + leaf_ctx, + enc.tell() + ) + }); + enc.symbol( + PARTITION_NONE, + &mut cdfs.partition_w8[leaf_ctx], + ); + let grids = [ + level_grid(&leaf.luma, TX8)?, + level_grid(&leaf.u, TX4)?, + level_grid(&leaf.v, TX4)?, + ]; + let leaf_mode = write_leaf8( + &mut enc, + &mut cdfs, + &mut neighbours, + leaf, + at, + leaf_mi, + &grids, + [&scans[2], &scans[3], &scans[3]], + prev_leaf, + ); + prev_leaf = Some((leaf_mi, leaf_mode)); + ec_rng_trace(|| { + format!( + "EC_TOK mi_row={} mi_col={} tell={}", + leaf_mi.0, + leaf_mi.1, + enc.tell() + ) + }); + } + // `record()`'s `above_mode`/`left_mode` + // write is a no-op at an 8x8 leaf's own + // side (`side / SUB == 0`), so the next + // 16x16 quadrant beyond this straddling + // one would otherwise see whatever stale + // mode sat here before it: force the + // write once the whole quadrant's leaves + // are done, from the last (bottom/right- + // most) leaf -- lane-av1-rect r15: doing + // this *inside* `write_leaf8`, once per + // leaf, let the first leaf's write + // clobber the true external neighbour a + // second leaf of the *same* quadrant + // still needed to read on its + // non-adjacency axis. + if let Some((_, mode)) = prev_leaf { + neighbours.above_mode[at.1] = mode; + neighbours.left_mode[at.0] = mode; + } } } } @@ -1113,6 +1296,7 @@ fn write_intra_mode( mode, &mut cdfs.kf_y_mode[INTRA_MODE_CTX[above_mode]][INTRA_MODE_CTX[left_mode]], ); + ec_rng_trace(|| format!("EC_YMODE mode={mode} tell={} rng={}", enc.tell(), enc.rng())); if (V_PRED..=D67_PRED).contains(&mode) { enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]); } @@ -1163,6 +1347,62 @@ fn write_block( neighbours.record(at, side, mode, grids); } +/// Writes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect): its own +/// luma transform and 4x4 chroma transforms, coded exactly like +/// [`write_block`] but reading its intra-mode context from the *enclosing* +/// 16x16 slot -- `outer_at`, in [`SUB`]-grid units -- rather than from its own +/// finer position, since [`Neighbours`]'s `above_mode`/`left_mode` arrays stay +/// at [`SUB`] (16-sample) granularity. `leaf_mi` is this leaf's own position +/// in 4x4 mode-info units, which is what its coefficient context (finer than +/// [`SUB`]) is kept and read at. +#[allow(clippy::too_many_arguments)] +fn write_leaf8( + enc: &mut SymbolEncoder, + cdfs: &mut Cdfs, + neighbours: &mut Neighbours, + block: &BlockCoeffs, + outer_at: (usize, usize), + leaf_mi: (usize, usize), + grids: &[Vec; 3], + scans: [&Vec; 3], + prev_leaf: Option<((usize, usize), usize)>, +) -> usize { + let (r, c) = outer_at; + let mut above_mode = neighbours.above_mode[c]; + let mut left_mode = neighbours.left_mode[r]; + // The previous leaf sits directly above (same column, two mi rows up) + // or directly to the left (same row, two mi cols over) of this one -- + // in either case its just-written mode, not the enclosing 16x16 slot's + // stale neighbour, is what a decoder reads as this leaf's context. + if let Some(((pr, pc), pmode)) = prev_leaf { + if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { + above_mode = pmode; + } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 { + left_mode = pmode; + } + } + // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec + // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF -- like every + // other `write_block` caller at 16x16 and up -- not the narrower + // no-CFL one: r12 lane-av1-rect, this leaf's own `cfl: false` was the + // true first divergence (a differently-sized alphabet under the same + // DC_PRED decision desyncs the coder even though the decoded mode is + // unchanged). + let mode = write_intra_mode(enc, cdfs, block, above_mode, left_mode, true); + let planes = [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4]; + write_block_planes( + enc, + cdfs, + &planes, + grids, + &scans, + &neighbours.around_mi(leaf_mi, 8), + mode, + ); + neighbours.record_mi(leaf_mi, 8, grids); + mode +} + /// Writes the three transform blocks of one coded block, in the order a /// decoder reads them. #[allow(clippy::too_many_arguments)] @@ -1331,14 +1571,21 @@ pub(crate) fn luma_32_coeff_bits(grid: &[i32]) -> f64 { pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet) -> f64 { /// Built once per size: the search prices thirteen modes for every block, /// and the scan is the same table every time. - static SCANS: LazyLock<[Vec; 3]> = - LazyLock::new(|| [default_scan(TX8), default_scan(TX16), default_scan(TX32)]); + static SCANS: LazyLock<[Vec; 4]> = LazyLock::new(|| { + [ + default_scan(TX4), + default_scan(TX8), + default_scan(TX16), + default_scan(TX32), + ] + }); let mut cdfs = Cdfs::new(2); let mut coding = cdfs.txb(set, DC_PRED); let scan = match coding.side { - TX8 => &SCANS[0], - TX16 => &SCANS[1], - _ => &SCANS[2], + TX4 => &SCANS[0], + TX8 => &SCANS[1], + TX16 => &SCANS[2], + _ => &SCANS[3], }; let mut enc = SymbolEncoder::new(); enc.reset_bits(); @@ -1397,9 +1644,10 @@ fn write_coeffs( if plane.is_some() { ec_rng_trace(|| { format!( - "EC_TXBSKIP ctx={skip_ctx} eob0={} tell={}", + "EC_TXBSKIP ctx={skip_ctx} eob0={} tell={} rng={}", usize::from(eob == 0), - enc.tell() + enc.tell(), + enc.rng() ) }); } @@ -1412,7 +1660,7 @@ fn write_coeffs( if let Some(tx_type) = coding.tx_type.as_deref_mut() { enc.symbol(TX_TYPE_DCT_DCT_SET2, tx_type); if plane.is_some() { - ec_rng_trace(|| format!("EC_TXTYPE tell={}", enc.tell())); + ec_rng_trace(|| format!("EC_TXTYPE tell={} rng={}", enc.tell(), enc.rng())); } } @@ -1525,7 +1773,13 @@ fn write_eob(enc: &mut SymbolEncoder, coding: &mut TxbTables, eob: usize, plane: // reaches is the size of its own end-of-block alphabet. enc.symbol(group - 1, coding.eob_pt); if let Some(plane) = plane { - ec_rng_trace(|| format!("EC_EOBPT plane={plane} eob_pt={group} tell={}", enc.tell())); + ec_rng_trace(|| { + format!( + "EC_EOBPT plane={plane} eob_pt={group} tell={} rng={}", + enc.tell(), + enc.rng() + ) + }); } let bits = OFFSET_BITS[group];