forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1tx4': TX4 luma decode and the 1D transform classes

Browse files at this commit

843a5a862aa70e358f874104c085a32d285fe6b6Ahmet Kaan GümüşParent: f581c0d5147651
Seven rounds: TX4 coefficient tables (transcription cross-verified),
V_DCT/H_DCT scan orders and the class-split eob_pt CDF, the leaf8
scan-table fix, and a gate that fires the class-1 path on real aomenc
bytes and decodes pixel-exact. The tx-class lesson: a transform class
is a cross-cutting dimension -- scan, eob table, and context each
carry it.
crates/ec-av1/src/cdf.rsmodified+449 -0
@@ -3008,6 +3008,14 @@ pub const EOB_PT_64_LUMA: [u16; 8] = [2374, 2772, 4583, 7276, 12288, 19706, 3276
30083008 /// EOB_PT_64_LUMA_Q3, q-context 3: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 121..=255.
30093009 pub const EOB_PT_64_LUMA_Q3: [u16; 8] = [6307, 7541, 12060, 16358, 22553, 27865, 32768, 0];
30103010
3011+/// The `EOB_PT_64_LUMA` sibling of [`EOB_PT_16_LUMA_CLASS1`]
3012+/// (`av1_default_eob_multi64_cdfs[q][0][1]`), for an 8x8 luma TU whose
3013+/// `tx_type` resolves `V_DCT`/`H_DCT`.
3014+pub const EOB_PT_64_LUMA_CLASS1_Q0: [u16; 8] = [335, 730, 1459, 5494, 8755, 12997, 32768, 0];
3015+pub const EOB_PT_64_LUMA_CLASS1_Q1: [u16; 8] = [401, 605, 1029, 2563, 5845, 12626, 32768, 0];
3016+pub const EOB_PT_64_LUMA_CLASS1: [u16; 8] = [497, 810, 1315, 3000, 7004, 15641, 32768, 0];
3017+pub const EOB_PT_64_LUMA_CLASS1_Q3: [u16; 8] = [1289, 2320, 3971, 7926, 14153, 24291, 32768, 0];
3018+
30113019 /// EOB_PT_16_CHROMA_Q0, q-context 0: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 0..=20.
30123020 pub const EOB_PT_16_CHROMA_Q0: [u16; 6] = [3247, 4950, 9688, 14563, 32768, 0];
30133021
@@ -3936,3 +3944,444 @@ pub const TXB_SKIP_LUMA_64_Q3_CTX: [[u16; 3]; 7] = [
39363944 [30026, 32768, 0],
39373945 [32441, 32768, 0],
39383946 ];
3947+
3948+// The tables below extend the `_LUMA_8`/`Luma8Set1` straddling-16x16 support
3949+// above to a resolved `TX4` (spec `TX_4X4`) luma transform unit: a
3950+// `TxMode::Select` 8x8 leaf or 16x16 block whose `tx_depth` read exhausts
3951+// down to the smallest size (lane-av1tx4). Extracted byte-exact from
3952+// libaom's `token_cdfs.h` `TX_SIZES` idx0 (`TX_4X4`)/`PLANE_TYPES` idx0
3953+// (luma), the same indexing method that verified the `_LUMA_8`/`COEFF_BR_LUMA_8`
3954+// families above.
3955+
3956+/// The full luma sub-range of spec `TXB_SKIP_CONTEXTS` (contexts 0..6) for a
3957+/// resolved 4x4 luma transform, same convention as [`TXB_SKIP_LUMA_8_CTX`].
3958+pub const TXB_SKIP_LUMA_4_Q0_CTX: [[u16; 3]; 7] = [
3959+ [31849, 32768, 0],
3960+ [5892, 32768, 0],
3961+ [12112, 32768, 0],
3962+ [21935, 32768, 0],
3963+ [20289, 32768, 0],
3964+ [27473, 32768, 0],
3965+ [32487, 32768, 0],
3966+];
3967+pub const TXB_SKIP_LUMA_4_Q1_CTX: [[u16; 3]; 7] = [
3968+ [30371, 32768, 0],
3969+ [7570, 32768, 0],
3970+ [13155, 32768, 0],
3971+ [20751, 32768, 0],
3972+ [20969, 32768, 0],
3973+ [27067, 32768, 0],
3974+ [32013, 32768, 0],
3975+];
3976+pub const TXB_SKIP_LUMA_4_CTX: [[u16; 3]; 7] = [
3977+ [29614, 32768, 0],
3978+ [9068, 32768, 0],
3979+ [12924, 32768, 0],
3980+ [19538, 32768, 0],
3981+ [17737, 32768, 0],
3982+ [24619, 32768, 0],
3983+ [30642, 32768, 0],
3984+];
3985+pub const TXB_SKIP_LUMA_4_Q3_CTX: [[u16; 3]; 7] = [
3986+ [26887, 32768, 0],
3987+ [6729, 32768, 0],
3988+ [10361, 32768, 0],
3989+ [17442, 32768, 0],
3990+ [15045, 32768, 0],
3991+ [22478, 32768, 0],
3992+ [29072, 32768, 0],
3993+];
3994+
3995+/// The end-of-block group of a 4x4 luma transform (`av1_default_eob_multi16_cdfs`,
3996+/// `PLANE_TYPES` idx0 luma).
3997+pub const EOB_PT_16_LUMA_Q0: [u16; 6] = [840, 1039, 1980, 4895, 32768, 0];
3998+pub const EOB_PT_16_LUMA_Q1: [u16; 6] = [2125, 2551, 5165, 8946, 32768, 0];
3999+pub const EOB_PT_16_LUMA: [u16; 6] = [4016, 4897, 8881, 14968, 32768, 0];
4000+pub const EOB_PT_16_LUMA_Q3: [u16; 6] = [6708, 8958, 14746, 22133, 32768, 0];
4001+
4002+/// The `TX_CLASS_HORIZ`/`TX_CLASS_VERT` (non-2D-scan) sibling of
4003+/// [`EOB_PT_16_LUMA`] (`av1_default_eob_multi16_cdfs[q][0][1]`) -- read
4004+/// instead of the 2D table whenever a 4x4 luma TU's `tx_type` resolves to
4005+/// `V_DCT`/`H_DCT` (lane-av1tx4 r5: the class dimension libaom always keeps,
4006+/// this crate never carried at all until a real aomdec trace showed the
4007+/// desync starting exactly at this read).
4008+pub const EOB_PT_16_LUMA_CLASS1_Q0: [u16; 6] = [370, 671, 1883, 4471, 32768, 0];
4009+pub const EOB_PT_16_LUMA_CLASS1_Q1: [u16; 6] = [513, 765, 1859, 6339, 32768, 0];
4010+pub const EOB_PT_16_LUMA_CLASS1: [u16; 6] = [716, 1105, 2646, 10056, 32768, 0];
4011+pub const EOB_PT_16_LUMA_CLASS1_Q3: [u16; 6] = [1222, 2074, 4783, 15410, 32768, 0];
4012+
4013+// EOB_EXTRA_LUMA_4
4014+pub const EOB_EXTRA_LUMA_4_Q0: [[u16; 3]; 9] = [
4015+ [16961, 32768, 0],
4016+ [17223, 32768, 0],
4017+ [7621, 32768, 0],
4018+ [16384, 32768, 0],
4019+ [16384, 32768, 0],
4020+ [16384, 32768, 0],
4021+ [16384, 32768, 0],
4022+ [16384, 32768, 0],
4023+ [16384, 32768, 0],
4024+];
4025+
4026+pub const EOB_EXTRA_LUMA_4_Q1: [[u16; 3]; 9] = [
4027+ [17471, 32768, 0],
4028+ [20223, 32768, 0],
4029+ [11357, 32768, 0],
4030+ [16384, 32768, 0],
4031+ [16384, 32768, 0],
4032+ [16384, 32768, 0],
4033+ [16384, 32768, 0],
4034+ [16384, 32768, 0],
4035+ [16384, 32768, 0],
4036+];
4037+
4038+pub const EOB_EXTRA_LUMA_4: [[u16; 3]; 9] = [
4039+ [18983, 32768, 0],
4040+ [20512, 32768, 0],
4041+ [14885, 32768, 0],
4042+ [16384, 32768, 0],
4043+ [16384, 32768, 0],
4044+ [16384, 32768, 0],
4045+ [16384, 32768, 0],
4046+ [16384, 32768, 0],
4047+ [16384, 32768, 0],
4048+];
4049+
4050+pub const EOB_EXTRA_LUMA_4_Q3: [[u16; 3]; 9] = [
4051+ [20177, 32768, 0],
4052+ [20789, 32768, 0],
4053+ [20262, 32768, 0],
4054+ [16384, 32768, 0],
4055+ [16384, 32768, 0],
4056+ [16384, 32768, 0],
4057+ [16384, 32768, 0],
4058+ [16384, 32768, 0],
4059+ [16384, 32768, 0],
4060+];
4061+
4062+// COEFF_BASE_LUMA_4
4063+pub const COEFF_BASE_LUMA_4_Q0: [[u16; 5]; 42] = [
4064+ [4034, 8930, 12727, 32768, 0],
4065+ [18082, 29741, 31877, 32768, 0],
4066+ [12596, 26124, 30493, 32768, 0],
4067+ [9446, 21118, 27005, 32768, 0],
4068+ [6308, 15141, 21279, 32768, 0],
4069+ [2463, 6357, 9783, 32768, 0],
4070+ [20667, 30546, 31929, 32768, 0],
4071+ [13043, 26123, 30134, 32768, 0],
4072+ [8151, 18757, 24778, 32768, 0],
4073+ [5255, 12839, 18632, 32768, 0],
4074+ [2820, 7206, 11161, 32768, 0],
4075+ [8192, 16384, 24576, 32768, 0],
4076+ [8192, 16384, 24576, 32768, 0],
4077+ [8192, 16384, 24576, 32768, 0],
4078+ [8192, 16384, 24576, 32768, 0],
4079+ [8192, 16384, 24576, 32768, 0],
4080+ [8192, 16384, 24576, 32768, 0],
4081+ [8192, 16384, 24576, 32768, 0],
4082+ [8192, 16384, 24576, 32768, 0],
4083+ [8192, 16384, 24576, 32768, 0],
4084+ [8192, 16384, 24576, 32768, 0],
4085+ [15736, 27553, 30604, 32768, 0],
4086+ [11210, 23794, 28787, 32768, 0],
4087+ [5947, 13874, 19701, 32768, 0],
4088+ [4215, 9323, 13891, 32768, 0],
4089+ [2833, 6462, 10059, 32768, 0],
4090+ [19605, 30393, 31582, 32768, 0],
4091+ [13523, 26252, 30248, 32768, 0],
4092+ [8446, 18622, 24512, 32768, 0],
4093+ [3818, 10343, 15974, 32768, 0],
4094+ [1481, 4117, 6796, 32768, 0],
4095+ [22649, 31302, 32190, 32768, 0],
4096+ [14829, 27127, 30449, 32768, 0],
4097+ [8313, 17702, 23304, 32768, 0],
4098+ [3022, 8301, 12786, 32768, 0],
4099+ [1536, 4412, 7184, 32768, 0],
4100+ [22354, 29774, 31372, 32768, 0],
4101+ [14723, 25472, 29214, 32768, 0],
4102+ [6673, 13745, 18662, 32768, 0],
4103+ [2068, 5766, 9322, 32768, 0],
4104+ [8192, 16384, 24576, 32768, 0],
4105+ [8192, 16384, 24576, 32768, 0],
4106+];
4107+
4108+pub const COEFF_BASE_LUMA_4_Q1: [[u16; 5]; 42] = [
4109+ [6041, 11854, 15927, 32768, 0],
4110+ [20326, 30905, 32251, 32768, 0],
4111+ [14164, 26831, 30725, 32768, 0],
4112+ [9760, 20647, 26585, 32768, 0],
4113+ [6416, 14953, 21219, 32768, 0],
4114+ [2966, 7151, 10891, 32768, 0],
4115+ [23567, 31374, 32254, 32768, 0],
4116+ [14978, 27416, 30946, 32768, 0],
4117+ [9434, 20225, 26254, 32768, 0],
4118+ [6658, 14558, 20535, 32768, 0],
4119+ [3916, 8677, 12989, 32768, 0],
4120+ [8192, 16384, 24576, 32768, 0],
4121+ [8192, 16384, 24576, 32768, 0],
4122+ [8192, 16384, 24576, 32768, 0],
4123+ [8192, 16384, 24576, 32768, 0],
4124+ [8192, 16384, 24576, 32768, 0],
4125+ [8192, 16384, 24576, 32768, 0],
4126+ [8192, 16384, 24576, 32768, 0],
4127+ [8192, 16384, 24576, 32768, 0],
4128+ [8192, 16384, 24576, 32768, 0],
4129+ [8192, 16384, 24576, 32768, 0],
4130+ [18088, 29545, 31587, 32768, 0],
4131+ [13062, 25843, 30073, 32768, 0],
4132+ [8940, 16827, 22251, 32768, 0],
4133+ [7654, 13220, 17973, 32768, 0],
4134+ [5733, 10316, 14456, 32768, 0],
4135+ [22879, 31388, 32114, 32768, 0],
4136+ [15215, 27993, 30955, 32768, 0],
4137+ [9397, 19445, 24978, 32768, 0],
4138+ [3442, 9813, 15344, 32768, 0],
4139+ [1368, 3936, 6532, 32768, 0],
4140+ [25494, 32033, 32406, 32768, 0],
4141+ [16772, 27963, 30718, 32768, 0],
4142+ [9419, 18165, 23260, 32768, 0],
4143+ [2677, 7501, 11797, 32768, 0],
4144+ [1516, 4344, 7170, 32768, 0],
4145+ [26556, 31454, 32101, 32768, 0],
4146+ [17128, 27035, 30108, 32768, 0],
4147+ [8324, 15344, 20249, 32768, 0],
4148+ [1903, 5696, 9469, 32768, 0],
4149+ [8192, 16384, 24576, 32768, 0],
4150+ [8192, 16384, 24576, 32768, 0],
4151+];
4152+
4153+pub const COEFF_BASE_LUMA_4: [[u16; 5]; 42] = [
4154+ [8896, 16227, 20630, 32768, 0],
4155+ [23629, 31782, 32527, 32768, 0],
4156+ [15173, 27755, 31321, 32768, 0],
4157+ [10158, 21233, 27382, 32768, 0],
4158+ [6420, 14857, 21558, 32768, 0],
4159+ [3269, 8155, 12646, 32768, 0],
4160+ [24835, 32009, 32496, 32768, 0],
4161+ [16509, 28421, 31579, 32768, 0],
4162+ [10957, 21514, 27418, 32768, 0],
4163+ [7881, 15930, 22096, 32768, 0],
4164+ [5388, 10960, 15918, 32768, 0],
4165+ [8192, 16384, 24576, 32768, 0],
4166+ [8192, 16384, 24576, 32768, 0],
4167+ [8192, 16384, 24576, 32768, 0],
4168+ [8192, 16384, 24576, 32768, 0],
4169+ [8192, 16384, 24576, 32768, 0],
4170+ [8192, 16384, 24576, 32768, 0],
4171+ [8192, 16384, 24576, 32768, 0],
4172+ [8192, 16384, 24576, 32768, 0],
4173+ [8192, 16384, 24576, 32768, 0],
4174+ [8192, 16384, 24576, 32768, 0],
4175+ [20745, 30773, 32093, 32768, 0],
4176+ [15200, 27221, 30861, 32768, 0],
4177+ [13032, 20873, 25667, 32768, 0],
4178+ [12285, 18663, 23494, 32768, 0],
4179+ [11563, 17481, 21489, 32768, 0],
4180+ [26260, 31982, 32320, 32768, 0],
4181+ [15397, 28083, 31100, 32768, 0],
4182+ [9742, 19217, 24824, 32768, 0],
4183+ [3261, 9629, 15362, 32768, 0],
4184+ [1480, 4322, 7499, 32768, 0],
4185+ [27599, 32256, 32460, 32768, 0],
4186+ [16857, 27659, 30774, 32768, 0],
4187+ [9551, 18290, 23748, 32768, 0],
4188+ [3052, 8933, 14103, 32768, 0],
4189+ [2021, 5910, 9787, 32768, 0],
4190+ [29005, 32015, 32392, 32768, 0],
4191+ [17677, 27694, 30863, 32768, 0],
4192+ [9204, 17356, 23219, 32768, 0],
4193+ [2403, 7516, 12814, 32768, 0],
4194+ [8192, 16384, 24576, 32768, 0],
4195+ [8192, 16384, 24576, 32768, 0],
4196+];
4197+
4198+pub const COEFF_BASE_LUMA_4_Q3: [[u16; 5]; 42] = [
4199+ [7062, 16472, 22319, 32768, 0],
4200+ [24538, 32261, 32674, 32768, 0],
4201+ [13675, 28041, 31779, 32768, 0],
4202+ [8590, 20674, 27631, 32768, 0],
4203+ [5685, 14675, 22013, 32768, 0],
4204+ [3655, 9898, 15731, 32768, 0],
4205+ [26493, 32418, 32658, 32768, 0],
4206+ [16376, 29342, 32090, 32768, 0],
4207+ [10594, 22649, 28970, 32768, 0],
4208+ [8176, 17170, 24303, 32768, 0],
4209+ [5605, 12694, 19139, 32768, 0],
4210+ [8192, 16384, 24576, 32768, 0],
4211+ [8192, 16384, 24576, 32768, 0],
4212+ [8192, 16384, 24576, 32768, 0],
4213+ [8192, 16384, 24576, 32768, 0],
4214+ [8192, 16384, 24576, 32768, 0],
4215+ [8192, 16384, 24576, 32768, 0],
4216+ [8192, 16384, 24576, 32768, 0],
4217+ [8192, 16384, 24576, 32768, 0],
4218+ [8192, 16384, 24576, 32768, 0],
4219+ [8192, 16384, 24576, 32768, 0],
4220+ [23888, 31902, 32542, 32768, 0],
4221+ [18612, 29687, 31987, 32768, 0],
4222+ [16245, 24852, 29249, 32768, 0],
4223+ [15765, 22608, 27559, 32768, 0],
4224+ [19895, 24699, 27510, 32768, 0],
4225+ [28401, 32212, 32457, 32768, 0],
4226+ [15274, 27825, 30980, 32768, 0],
4227+ [9364, 18128, 24332, 32768, 0],
4228+ [2283, 8193, 15082, 32768, 0],
4229+ [1228, 3972, 7881, 32768, 0],
4230+ [29455, 32469, 32620, 32768, 0],
4231+ [17981, 28245, 31388, 32768, 0],
4232+ [10921, 20098, 26240, 32768, 0],
4233+ [3743, 11829, 18657, 32768, 0],
4234+ [2374, 9593, 15715, 32768, 0],
4235+ [31068, 32466, 32635, 32768, 0],
4236+ [20321, 29572, 31971, 32768, 0],
4237+ [10771, 20255, 27119, 32768, 0],
4238+ [2795, 10410, 17361, 32768, 0],
4239+ [8192, 16384, 24576, 32768, 0],
4240+ [8192, 16384, 24576, 32768, 0],
4241+];
4242+
4243+// COEFF_BASE_EOB_LUMA_4
4244+pub const COEFF_BASE_EOB_LUMA_4_Q0: [[u16; 4]; 4] = [
4245+ [17837, 29055, 32768, 0],
4246+ [29600, 31446, 32768, 0],
4247+ [30844, 31878, 32768, 0],
4248+ [24926, 28948, 32768, 0],
4249+];
4250+
4251+pub const COEFF_BASE_EOB_LUMA_4_Q1: [[u16; 4]; 4] = [
4252+ [17560, 29888, 32768, 0],
4253+ [29671, 31549, 32768, 0],
4254+ [31007, 32056, 32768, 0],
4255+ [27286, 30006, 32768, 0],
4256+];
4257+
4258+pub const COEFF_BASE_EOB_LUMA_4: [[u16; 4]; 4] = [
4259+ [20092, 30774, 32768, 0],
4260+ [30695, 32020, 32768, 0],
4261+ [31131, 32103, 32768, 0],
4262+ [28666, 30870, 32768, 0],
4263+];
4264+
4265+pub const COEFF_BASE_EOB_LUMA_4_Q3: [[u16; 4]; 4] = [
4266+ [22497, 31198, 32768, 0],
4267+ [31715, 32495, 32768, 0],
4268+ [31606, 32337, 32768, 0],
4269+ [30388, 31990, 32768, 0],
4270+];
4271+
4272+// COEFF_BR_LUMA_4
4273+pub const COEFF_BR_LUMA_4_Q0: [[u16; 5]; 21] = [
4274+ [14298, 20718, 24174, 32768, 0],
4275+ [12536, 19601, 23789, 32768, 0],
4276+ [8712, 15051, 19503, 32768, 0],
4277+ [6170, 11327, 15434, 32768, 0],
4278+ [4742, 8926, 12538, 32768, 0],
4279+ [3803, 7317, 10546, 32768, 0],
4280+ [1696, 3317, 4871, 32768, 0],
4281+ [14392, 19951, 22756, 32768, 0],
4282+ [15978, 23218, 26818, 32768, 0],
4283+ [12187, 19474, 23889, 32768, 0],
4284+ [9176, 15640, 20259, 32768, 0],
4285+ [7068, 12655, 17028, 32768, 0],
4286+ [5656, 10442, 14472, 32768, 0],
4287+ [2580, 4992, 7244, 32768, 0],
4288+ [12136, 18049, 21426, 32768, 0],
4289+ [13784, 20721, 24481, 32768, 0],
4290+ [10836, 17621, 21900, 32768, 0],
4291+ [8372, 14444, 18847, 32768, 0],
4292+ [6523, 11779, 16000, 32768, 0],
4293+ [5337, 9898, 13760, 32768, 0],
4294+ [3034, 5860, 8462, 32768, 0],
4295+];
4296+
4297+pub const COEFF_BR_LUMA_4_Q1: [[u16; 5]; 21] = [
4298+ [14995, 21341, 24749, 32768, 0],
4299+ [13158, 20289, 24601, 32768, 0],
4300+ [8941, 15326, 19876, 32768, 0],
4301+ [6297, 11541, 15807, 32768, 0],
4302+ [4817, 9029, 12776, 32768, 0],
4303+ [3731, 7273, 10627, 32768, 0],
4304+ [1847, 3617, 5354, 32768, 0],
4305+ [14472, 19659, 22343, 32768, 0],
4306+ [16806, 24162, 27533, 32768, 0],
4307+ [12900, 20404, 24713, 32768, 0],
4308+ [9411, 16112, 20797, 32768, 0],
4309+ [7056, 12697, 17148, 32768, 0],
4310+ [5544, 10339, 14460, 32768, 0],
4311+ [2954, 5704, 8319, 32768, 0],
4312+ [12464, 18071, 21354, 32768, 0],
4313+ [15482, 22528, 26034, 32768, 0],
4314+ [12070, 19269, 23624, 32768, 0],
4315+ [8953, 15406, 20106, 32768, 0],
4316+ [7027, 12730, 17220, 32768, 0],
4317+ [5887, 10913, 15140, 32768, 0],
4318+ [3793, 7278, 10447, 32768, 0],
4319+];
4320+
4321+pub const COEFF_BR_LUMA_4: [[u16; 5]; 21] = [
4322+ [16138, 22223, 25509, 32768, 0],
4323+ [15347, 22430, 26332, 32768, 0],
4324+ [9614, 16736, 21332, 32768, 0],
4325+ [6600, 12275, 16907, 32768, 0],
4326+ [4811, 9424, 13547, 32768, 0],
4327+ [3748, 7809, 11420, 32768, 0],
4328+ [2254, 4587, 6890, 32768, 0],
4329+ [15196, 20284, 23177, 32768, 0],
4330+ [18317, 25469, 28451, 32768, 0],
4331+ [13918, 21651, 25842, 32768, 0],
4332+ [10052, 17150, 21995, 32768, 0],
4333+ [7499, 13630, 18587, 32768, 0],
4334+ [6158, 11417, 16003, 32768, 0],
4335+ [4014, 7785, 11252, 32768, 0],
4336+ [15048, 21067, 24384, 32768, 0],
4337+ [18202, 25346, 28553, 32768, 0],
4338+ [14302, 22019, 26356, 32768, 0],
4339+ [10839, 18139, 23166, 32768, 0],
4340+ [8715, 15744, 20806, 32768, 0],
4341+ [7536, 13576, 18544, 32768, 0],
4342+ [5413, 10335, 14498, 32768, 0],
4343+];
4344+
4345+pub const COEFF_BR_LUMA_4_Q3: [[u16; 5]; 21] = [
4346+ [18315, 24289, 27551, 32768, 0],
4347+ [16854, 24068, 27835, 32768, 0],
4348+ [10140, 17927, 23173, 32768, 0],
4349+ [6722, 12982, 18267, 32768, 0],
4350+ [4661, 9826, 14706, 32768, 0],
4351+ [3832, 8165, 12294, 32768, 0],
4352+ [2795, 6098, 9245, 32768, 0],
4353+ [17145, 23326, 26672, 32768, 0],
4354+ [20733, 27680, 30308, 32768, 0],
4355+ [16032, 24461, 28546, 32768, 0],
4356+ [11653, 20093, 25081, 32768, 0],
4357+ [9290, 16429, 22086, 32768, 0],
4358+ [7796, 14598, 19982, 32768, 0],
4359+ [6502, 12378, 17441, 32768, 0],
4360+ [21681, 27732, 30320, 32768, 0],
4361+ [22389, 29044, 31261, 32768, 0],
4362+ [19027, 26731, 30087, 32768, 0],
4363+ [14739, 23755, 28624, 32768, 0],
4364+ [11358, 20778, 25511, 32768, 0],
4365+ [10995, 18073, 24190, 32768, 0],
4366+ [9162, 14990, 20617, 32768, 0],
4367+];
4368+
4369+/// `Default_Intra_Ext_Tx_Cdf`'s `TX_SET_INTRA_1` row for `TX_4X4` (spec
4370+/// 9.4): the transform type of an intra 4x4 transform when `reduced_tx_set`
4371+/// is false. Same `Luma8`/`Luma8Set1` split as [`INTRA_TX_TYPE_SET1_8`],
4372+/// kept named for the day a non-reduced path exists.
4373+pub const INTRA_TX_TYPE_SET1_4: [[u16; 8]; 13] = [
4374+ [1535, 8035, 9461, 12751, 23467, 27825, 32768, 0],
4375+ [564, 3335, 9709, 10870, 18143, 28094, 32768, 0],
4376+ [672, 3247, 3676, 11982, 19415, 23127, 32768, 0],
4377+ [5279, 13885, 15487, 18044, 23527, 30252, 32768, 0],
4378+ [4423, 6074, 7985, 10416, 25693, 29298, 32768, 0],
4379+ [1486, 4241, 9460, 10662, 16456, 27694, 32768, 0],
4380+ [439, 2838, 3522, 6737, 18058, 23754, 32768, 0],
4381+ [1190, 4233, 4855, 11670, 20281, 24377, 32768, 0],
4382+ [1045, 4312, 8647, 10159, 18644, 29335, 32768, 0],
4383+ [202, 3734, 4747, 7298, 17127, 24016, 32768, 0],
4384+ [447, 4312, 6819, 8884, 16010, 23858, 32768, 0],
4385+ [277, 4369, 5255, 8905, 16465, 22271, 32768, 0],
4386+ [3409, 5436, 10599, 15599, 19687, 24040, 32768, 0],
4387+];
crates/ec-av1/src/cdf_state.rsmodified+143 -0
@@ -55,6 +55,14 @@ pub(crate) enum TxbSet {
5555 /// adapted table from the 16x16 and 32x32 sizes' own entries in the same
5656 /// set, per `entropymode.c`).
5757 Luma8Inter,
58+ /// The 4x4 luma transform of a `TxMode::Select` 8x8 leaf or 16x16 block
59+ /// whose `tx_depth` resolves all the way down (lane-av1tx4). An
60+ /// `is_inter` block has no writer path yet, so this crate has no
61+ /// `Luma4Inter` counterpart to [`Luma8Inter`](TxbSet::Luma8Inter).
62+ Luma4,
63+ /// [`Luma4`](TxbSet::Luma4)'s `reduced_tx_set: false` counterpart, same
64+ /// split as [`Luma8Set1`](TxbSet::Luma8Set1).
65+ Luma4Set1,
5866 /// The 8x8 transform of a chroma plane of a 16x16 block.
5967 Chroma8,
6068 /// The 4x4 transform of a chroma plane of an 8x8 leaf (lane-av1-rect).
@@ -82,6 +90,11 @@ pub(crate) struct TxbTables<'a> {
8290 pub txb_skip: &'a mut [[u16; 3]],
8391 /// The end-of-block group, whose alphabet the transform size sets.
8492 pub eob_pt: &'a mut [u16],
93+ /// `eob_pt`'s `TX_CLASS_HORIZ`/`TX_CLASS_VERT` sibling table (r5): read
94+ /// instead of `eob_pt` once this TU's `tx_type` is known to be
95+ /// `V_DCT`/`H_DCT`. `None` for any size/set whose `tx_type` alphabet
96+ /// cannot produce those two symbols (chroma, and every inter set).
97+ pub eob_pt_class1: Option<&'a mut [u16]>,
8598 /// The top bit of the offset inside that group.
8699 pub eob_extra: &'a mut [[u16; 3]; 9],
87100 /// A coefficient's base level.
@@ -128,6 +141,8 @@ pub(crate) struct Cdfs {
128141 pub txb_skip_luma_16: [[u16; 3]; 7],
129142 /// The same, for an 8x8 luma transform.
130143 pub txb_skip_luma_8: [[u16; 3]; 7],
144+ /// The same, for a 4x4 luma transform (lane-av1tx4).
145+ pub txb_skip_luma_4: [[u16; 3]; 7],
131146 /// The all-zero flag of an 8x8 chroma transform.
132147 pub txb_skip_chroma_8: [[u16; 3]; 3],
133148 /// The all-zero flag of a 4x4 chroma transform.
@@ -140,10 +155,19 @@ pub(crate) struct Cdfs {
140155 pub eob_pt_64_chroma: [u16; 8],
141156 /// The end-of-block group of a 4x4 chroma transform.
142157 pub eob_pt_16_chroma: [u16; 6],
158+ /// The end-of-block group of a 4x4 luma transform (lane-av1tx4).
159+ pub eob_pt_16_luma: [u16; 6],
160+ /// `eob_pt_16_luma`'s `TX_CLASS_HORIZ`/`TX_CLASS_VERT` sibling, read
161+ /// instead whenever the TU's `tx_type` is `V_DCT`/`H_DCT` (r5).
162+ pub eob_pt_16_luma_class1: [u16; 6],
163+ /// `eob_pt_64_luma`'s class-1 sibling (r5), see `eob_pt_16_luma_class1`.
164+ pub eob_pt_64_luma_class1: [u16; 8],
143165 /// The end-of-block offset bit of a 16x16 luma transform.
144166 pub eob_extra_luma_16: [[u16; 3]; 9],
145167 /// The same, for an 8x8 luma transform.
146168 pub eob_extra_luma_8: [[u16; 3]; 9],
169+ /// The same, for a 4x4 luma transform.
170+ pub eob_extra_luma_4: [[u16; 3]; 9],
147171 /// The same, for an 8x8 chroma transform.
148172 pub eob_extra_chroma_8: [[u16; 3]; 9],
149173 /// The same, for a 4x4 chroma transform.
@@ -152,6 +176,8 @@ pub(crate) struct Cdfs {
152176 pub base_luma_16: [[u16; 5]; 42],
153177 /// The base level of an 8x8 luma coefficient.
154178 pub base_luma_8: [[u16; 5]; 42],
179+ /// The base level of a 4x4 luma coefficient.
180+ pub base_luma_4: [[u16; 5]; 42],
155181 /// The base level of an 8x8 chroma coefficient.
156182 pub base_chroma_8: [[u16; 5]; 42],
157183 /// The base level of a 4x4 chroma coefficient.
@@ -160,6 +186,8 @@ pub(crate) struct Cdfs {
160186 pub base_eob_luma_16: [[u16; 4]; 4],
161187 /// The same, for an 8x8 luma transform.
162188 pub base_eob_luma_8: [[u16; 4]; 4],
189+ /// The same, for a 4x4 luma transform.
190+ pub base_eob_luma_4: [[u16; 4]; 4],
163191 /// The same, for an 8x8 chroma transform.
164192 pub base_eob_chroma_8: [[u16; 4]; 4],
165193 /// The same, for a 4x4 chroma transform.
@@ -168,6 +196,8 @@ pub(crate) struct Cdfs {
168196 pub br_luma_16: [[u16; 5]; 21],
169197 /// The base-range tail of an 8x8 luma coefficient.
170198 pub br_luma_8: [[u16; 5]; 21],
199+ /// The base-range tail of a 4x4 luma coefficient.
200+ pub br_luma_4: [[u16; 5]; 21],
171201 /// The base-range tail of an 8x8 chroma coefficient.
172202 pub br_chroma_8: [[u16; 5]; 21],
173203 /// The base-range tail of a 4x4 chroma coefficient.
@@ -236,6 +266,15 @@ pub(crate) struct Cdfs {
236266 /// `reduced_tx_set` off desyncs an 8x8 TU read against this row's
237267 /// wrong-length CDF).
238268 pub intra_tx_type_8_set1: [[u16; 8]; 13],
269+ /// The transform type of a 4x4 intra luma transform, by luma mode
270+ /// (lane-av1tx4): the flat five-symbol `reduced_tx_set` row, byte-
271+ /// identical to [`Self::intra_tx_type_8`]'s own table
272+ /// ([`cdf::INTRA_TX_TYPE_SET2_8`]) -- libaom leaves `TX_4X4` at the same
273+ /// uniform default.
274+ pub intra_tx_type_4: [[u16; 6]; 13],
275+ /// [`Self::intra_tx_type_4`]'s `reduced_tx_set: false` counterpart, same
276+ /// split as [`Self::intra_tx_type_8_set1`].
277+ pub intra_tx_type_4_set1: [[u16; 8]; 13],
239278 /// The transform type of an `is_inter` 32x32 luma transform (spec
240279 /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it
241280 /// is not indexed by mode.
@@ -395,26 +434,34 @@ impl Cdfs {
395434 reset2(&mut self.angle_delta);
396435 reset2(&mut self.txb_skip_luma_16);
397436 reset2(&mut self.txb_skip_luma_8);
437+ reset2(&mut self.txb_skip_luma_4);
398438 reset2(&mut self.txb_skip_chroma_8);
399439 reset2(&mut self.txb_skip_chroma_4);
400440 reset1(&mut self.eob_pt_256_luma);
401441 reset1(&mut self.eob_pt_64_luma);
402442 reset1(&mut self.eob_pt_64_chroma);
403443 reset1(&mut self.eob_pt_16_chroma);
444+ reset1(&mut self.eob_pt_16_luma);
445+ reset1(&mut self.eob_pt_16_luma_class1);
446+ reset1(&mut self.eob_pt_64_luma_class1);
404447 reset2(&mut self.eob_extra_luma_16);
405448 reset2(&mut self.eob_extra_luma_8);
449+ reset2(&mut self.eob_extra_luma_4);
406450 reset2(&mut self.eob_extra_chroma_8);
407451 reset2(&mut self.eob_extra_chroma_4);
408452 reset2(&mut self.base_luma_16);
409453 reset2(&mut self.base_luma_8);
454+ reset2(&mut self.base_luma_4);
410455 reset2(&mut self.base_chroma_8);
411456 reset2(&mut self.base_chroma_4);
412457 reset2(&mut self.base_eob_luma_16);
413458 reset2(&mut self.base_eob_luma_8);
459+ reset2(&mut self.base_eob_luma_4);
414460 reset2(&mut self.base_eob_chroma_8);
415461 reset2(&mut self.base_eob_chroma_4);
416462 reset2(&mut self.br_luma_16);
417463 reset2(&mut self.br_luma_8);
464+ reset2(&mut self.br_luma_4);
418465 reset2(&mut self.br_chroma_8);
419466 reset2(&mut self.br_chroma_4);
420467 reset2(&mut self.partition_w16);
@@ -445,6 +492,8 @@ impl Cdfs {
445492 reset2(&mut self.intra_tx_type_16);
446493 reset2(&mut self.intra_tx_type_8);
447494 reset2(&mut self.intra_tx_type_8_set1);
495+ reset2(&mut self.intra_tx_type_4);
496+ reset2(&mut self.intra_tx_type_4_set1);
448497 reset1(&mut self.inter_tx_type_32);
449498 reset1(&mut self.inter_tx_type_16);
450499 reset1(&mut self.inter_tx_type_8);
@@ -497,6 +546,13 @@ impl Cdfs {
497546 cdf::TXB_SKIP_LUMA_8_CTX,
498547 cdf::TXB_SKIP_LUMA_8_Q3_CTX,
499548 ),
549+ txb_skip_luma_4: pick(
550+ q_ctx,
551+ cdf::TXB_SKIP_LUMA_4_Q0_CTX,
552+ cdf::TXB_SKIP_LUMA_4_Q1_CTX,
553+ cdf::TXB_SKIP_LUMA_4_CTX,
554+ cdf::TXB_SKIP_LUMA_4_Q3_CTX,
555+ ),
500556 txb_skip_chroma_8: pick(
501557 q_ctx,
502558 cdf::TXB_SKIP_CHROMA_8_Q0,
@@ -539,6 +595,27 @@ impl Cdfs {
539595 cdf::EOB_PT_16_CHROMA,
540596 cdf::EOB_PT_16_CHROMA_Q3,
541597 ),
598+ eob_pt_16_luma: pick(
599+ q_ctx,
600+ cdf::EOB_PT_16_LUMA_Q0,
601+ cdf::EOB_PT_16_LUMA_Q1,
602+ cdf::EOB_PT_16_LUMA,
603+ cdf::EOB_PT_16_LUMA_Q3,
604+ ),
605+ eob_pt_16_luma_class1: pick(
606+ q_ctx,
607+ cdf::EOB_PT_16_LUMA_CLASS1_Q0,
608+ cdf::EOB_PT_16_LUMA_CLASS1_Q1,
609+ cdf::EOB_PT_16_LUMA_CLASS1,
610+ cdf::EOB_PT_16_LUMA_CLASS1_Q3,
611+ ),
612+ eob_pt_64_luma_class1: pick(
613+ q_ctx,
614+ cdf::EOB_PT_64_LUMA_CLASS1_Q0,
615+ cdf::EOB_PT_64_LUMA_CLASS1_Q1,
616+ cdf::EOB_PT_64_LUMA_CLASS1,
617+ cdf::EOB_PT_64_LUMA_CLASS1_Q3,
618+ ),
542619 eob_extra_luma_16: pick(
543620 q_ctx,
544621 cdf::EOB_EXTRA_LUMA_16_Q0,
@@ -553,6 +630,13 @@ impl Cdfs {
553630 cdf::EOB_EXTRA_LUMA_8,
554631 cdf::EOB_EXTRA_LUMA_8_Q3,
555632 ),
633+ eob_extra_luma_4: pick(
634+ q_ctx,
635+ cdf::EOB_EXTRA_LUMA_4_Q0,
636+ cdf::EOB_EXTRA_LUMA_4_Q1,
637+ cdf::EOB_EXTRA_LUMA_4,
638+ cdf::EOB_EXTRA_LUMA_4_Q3,
639+ ),
556640 eob_extra_chroma_8: pick(
557641 q_ctx,
558642 cdf::EOB_EXTRA_CHROMA_8_Q0,
@@ -581,6 +665,13 @@ impl Cdfs {
581665 cdf::COEFF_BASE_LUMA_8,
582666 cdf::COEFF_BASE_LUMA_8_Q3,
583667 ),
668+ base_luma_4: pick(
669+ q_ctx,
670+ cdf::COEFF_BASE_LUMA_4_Q0,
671+ cdf::COEFF_BASE_LUMA_4_Q1,
672+ cdf::COEFF_BASE_LUMA_4,
673+ cdf::COEFF_BASE_LUMA_4_Q3,
674+ ),
584675 base_chroma_8: pick(
585676 q_ctx,
586677 cdf::COEFF_BASE_CHROMA_8_Q0,
@@ -609,6 +700,13 @@ impl Cdfs {
609700 cdf::COEFF_BASE_EOB_LUMA_8,
610701 cdf::COEFF_BASE_EOB_LUMA_8_Q3,
611702 ),
703+ base_eob_luma_4: pick(
704+ q_ctx,
705+ cdf::COEFF_BASE_EOB_LUMA_4_Q0,
706+ cdf::COEFF_BASE_EOB_LUMA_4_Q1,
707+ cdf::COEFF_BASE_EOB_LUMA_4,
708+ cdf::COEFF_BASE_EOB_LUMA_4_Q3,
709+ ),
612710 base_eob_chroma_8: pick(
613711 q_ctx,
614712 cdf::COEFF_BASE_EOB_CHROMA_8_Q0,
@@ -637,6 +735,13 @@ impl Cdfs {
637735 cdf::COEFF_BR_LUMA_8,
638736 cdf::COEFF_BR_LUMA_8_Q3,
639737 ),
738+ br_luma_4: pick(
739+ q_ctx,
740+ cdf::COEFF_BR_LUMA_4_Q0,
741+ cdf::COEFF_BR_LUMA_4_Q1,
742+ cdf::COEFF_BR_LUMA_4,
743+ cdf::COEFF_BR_LUMA_4_Q3,
744+ ),
640745 br_chroma_8: pick(
641746 q_ctx,
642747 cdf::COEFF_BR_CHROMA_8_Q0,
@@ -811,6 +916,8 @@ impl Cdfs {
811916 intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16,
812917 intra_tx_type_8: cdf::INTRA_TX_TYPE_SET2_8,
813918 intra_tx_type_8_set1: cdf::INTRA_TX_TYPE_SET1_8,
919+ intra_tx_type_4: cdf::INTRA_TX_TYPE_SET2_8,
920+ intra_tx_type_4_set1: cdf::INTRA_TX_TYPE_SET1_4,
814921 inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32,
815922 inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16,
816923 inter_tx_type_8: cdf::INTER_TX_TYPE_SET3_8,
@@ -847,6 +954,7 @@ impl Cdfs {
847954 br: &mut self.br_luma_32,
848955 dc_sign: &mut self.dc_sign_luma,
849956 tx_type: None,
957+ eob_pt_class1: None,
850958 },
851959 TxbSet::Luma32Inter => TxbTables {
852960 side: 32,
@@ -858,6 +966,7 @@ impl Cdfs {
858966 br: &mut self.br_luma_32,
859967 dc_sign: &mut self.dc_sign_luma,
860968 tx_type: Some(self.inter_tx_type_32.as_mut_slice()),
969+ eob_pt_class1: None,
861970 },
862971 TxbSet::Luma64 => TxbTables {
863972 side: 32,
@@ -869,6 +978,7 @@ impl Cdfs {
869978 br: &mut self.br_luma_32,
870979 dc_sign: &mut self.dc_sign_luma,
871980 tx_type: None,
981+ eob_pt_class1: None,
872982 },
873983 TxbSet::Luma16 => TxbTables {
874984 side: 16,
@@ -880,6 +990,7 @@ impl Cdfs {
880990 br: &mut self.br_luma_16,
881991 dc_sign: &mut self.dc_sign_luma,
882992 tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()),
993+ eob_pt_class1: None,
883994 },
884995 TxbSet::Luma16Inter => TxbTables {
885996 side: 16,
@@ -891,6 +1002,7 @@ impl Cdfs {
8911002 br: &mut self.br_luma_16,
8921003 dc_sign: &mut self.dc_sign_luma,
8931004 tx_type: Some(self.inter_tx_type_16.as_mut_slice()),
1005+ eob_pt_class1: None,
8941006 },
8951007 TxbSet::Luma8 => TxbTables {
8961008 side: 8,
@@ -902,6 +1014,7 @@ impl Cdfs {
9021014 br: &mut self.br_luma_8,
9031015 dc_sign: &mut self.dc_sign_luma,
9041016 tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()),
1017+ eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1),
9051018 },
9061019 TxbSet::Luma8Set1 => TxbTables {
9071020 side: 8,
@@ -913,6 +1026,31 @@ impl Cdfs {
9131026 br: &mut self.br_luma_8,
9141027 dc_sign: &mut self.dc_sign_luma,
9151028 tx_type: Some(self.intra_tx_type_8_set1[mode].as_mut_slice()),
1029+ eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1),
1030+ },
1031+ TxbSet::Luma4 => TxbTables {
1032+ side: 4,
1033+ txb_skip: &mut self.txb_skip_luma_4,
1034+ eob_pt: &mut self.eob_pt_16_luma,
1035+ eob_extra: &mut self.eob_extra_luma_4,
1036+ base: &mut self.base_luma_4,
1037+ base_eob: &mut self.base_eob_luma_4,
1038+ br: &mut self.br_luma_4,
1039+ dc_sign: &mut self.dc_sign_luma,
1040+ tx_type: Some(self.intra_tx_type_4[mode].as_mut_slice()),
1041+ eob_pt_class1: Some(&mut self.eob_pt_16_luma_class1),
1042+ },
1043+ TxbSet::Luma4Set1 => TxbTables {
1044+ side: 4,
1045+ txb_skip: &mut self.txb_skip_luma_4,
1046+ eob_pt: &mut self.eob_pt_16_luma,
1047+ eob_extra: &mut self.eob_extra_luma_4,
1048+ base: &mut self.base_luma_4,
1049+ base_eob: &mut self.base_eob_luma_4,
1050+ br: &mut self.br_luma_4,
1051+ dc_sign: &mut self.dc_sign_luma,
1052+ tx_type: Some(self.intra_tx_type_4_set1[mode].as_mut_slice()),
1053+ eob_pt_class1: Some(&mut self.eob_pt_16_luma_class1),
9161054 },
9171055 TxbSet::Luma8Inter => TxbTables {
9181056 side: 8,
@@ -924,6 +1062,7 @@ impl Cdfs {
9241062 br: &mut self.br_luma_8,
9251063 dc_sign: &mut self.dc_sign_luma,
9261064 tx_type: Some(self.inter_tx_type_8.as_mut_slice()),
1065+ eob_pt_class1: None,
9271066 },
9281067 TxbSet::Chroma8 => TxbTables {
9291068 side: 8,
@@ -935,6 +1074,7 @@ impl Cdfs {
9351074 br: &mut self.br_chroma_8,
9361075 dc_sign: &mut self.dc_sign_chroma,
9371076 tx_type: None,
1077+ eob_pt_class1: None,
9381078 },
9391079 TxbSet::Chroma4 => TxbTables {
9401080 side: 4,
@@ -946,6 +1086,7 @@ impl Cdfs {
9461086 br: &mut self.br_chroma_4,
9471087 dc_sign: &mut self.dc_sign_chroma,
9481088 tx_type: None,
1089+ eob_pt_class1: None,
9491090 },
9501091 TxbSet::Chroma16 => TxbTables {
9511092 side: 16,
@@ -957,6 +1098,7 @@ impl Cdfs {
9571098 br: &mut self.br_chroma_16,
9581099 dc_sign: &mut self.dc_sign_chroma,
9591100 tx_type: None,
1101+ eob_pt_class1: None,
9601102 },
9611103 TxbSet::Chroma32 => TxbTables {
9621104 side: 32,
@@ -968,6 +1110,7 @@ impl Cdfs {
9681110 br: &mut self.br_chroma_32,
9691111 dc_sign: &mut self.dc_sign_chroma,
9701112 tx_type: None,
1113+ eob_pt_class1: None,
9711114 },
9721115 }
9731116 }
crates/ec-av1/src/decode.rsmodified+308 -48
@@ -74,6 +74,19 @@ static TX_DEPTH_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::Atomic
7474 pub(crate) fn tx_depth_hits() -> usize {
7575 TX_DEPTH_HITS.load(std::sync::atomic::Ordering::Relaxed)
7676 }
77+
78+/// How many [`read_coeffs`] reads resolved a `tx_type` outside `TX_CLASS_2D`
79+/// (`V_DCT`/`H_DCT`, spec 5.11.39's `TxClass::Horiz`/`Vert`), across every
80+/// call in the process -- the same before/after counter pattern as
81+/// [`TX_DEPTH_HITS`], proving a stream actually exercised the class-split
82+/// `eob_pt`/1D-neighbour context path rather than every block coincidentally
83+/// landing on `DCT_DCT`.
84+static TX_CLASS1_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
85+
86+/// Current value of [`TX_CLASS1_HITS`].
87+pub(crate) fn tx_class1_hits() -> usize {
88+ TX_CLASS1_HITS.load(std::sync::atomic::Ordering::Relaxed)
89+}
7790 const SUB_MI: u32 = 4;
7891 const MI: usize = 4;
7992 const SB: usize = 64;
@@ -154,6 +167,26 @@ fn default_scan(side: usize) -> Vec<u16> {
154167 scan
155168 }
156169
170+/// `Mrow_Scan`/`Mcol_Scan` (spec 8.4.2, libaom `scan.c`): the two
171+/// class-specific scans a `V_DCT`/`H_DCT` transform block reads coefficients
172+/// in, instead of the default zigzag. libaom's own tables are keyed by a
173+/// *column-major* raster (`get_txb_bhl`'s `col = pos / height`) over its
174+/// physically column-major level buffer, but they decompose to the exact
175+/// same (row, col) walk as a flat row-major traversal of *our* row-major
176+/// `grid`/`levels` (`pos = row * side + col`) once you read them that way:
177+/// `Mrow_Scan` visits row 0 across every column, then row 1, ... — precisely
178+/// `0..side*side` in our own indexing — and `Mcol_Scan` is that walk
179+/// transposed (column 0 down every row, then column 1, ...).
180+fn class_scan_table(side: usize, class: TxClass) -> Vec<u16> {
181+ match class {
182+ TxClass::Vert => (0..(side * side) as u16).collect(),
183+ TxClass::Horiz => (0..side)
184+ .flat_map(|col| (0..side).map(move |row| (row * side + col) as u16))
185+ .collect(),
186+ TxClass::TwoD => unreachable!("class_scan is only for V_DCT/H_DCT"),
187+ }
188+}
189+
157190 fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize {
158191 match scan_idx {
159192 0 => 0,
@@ -171,31 +204,87 @@ fn neighbour(grid: &[i32], side: usize, row: usize, col: usize) -> i32 {
171204 }
172205 }
173206
174-fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
175- if row == 0 && col == 0 {
207+/// spec 5.11.39's three `TxClass`es (`tx_type_to_class`, libaom
208+/// `txb_common.h`): everything but the two lone-axis-identity types this
209+/// decoder reads (`V_DCT`/`H_DCT`) is `TX_CLASS_2D`.
210+#[derive(Clone, Copy, PartialEq, Eq)]
211+enum TxClass {
212+ TwoD,
213+ Horiz,
214+ Vert,
215+}
216+
217+impl TxClass {
218+ fn of(tx_type: TxType) -> Self {
219+ match tx_type {
220+ TxType::VDct => Self::Vert,
221+ TxType::HDct => Self::Horiz,
222+ _ => Self::TwoD,
223+ }
224+ }
225+}
226+
227+/// `nz_map_ctx_offset_1d`: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` share one 32-entry
228+/// table (indexed by the position along the transform's *coded* axis --
229+/// `col` for horiz, `row` for vert) that places their contexts past the 2D
230+/// table's own 26 rows (`SIG_COEF_CONTEXTS_2D`), 16 rows total
231+/// (`SIG_COEF_CONTEXTS_1D`) split 26/31/36.
232+fn nz_map_ctx_offset_1d(i: usize) -> usize {
233+ match i {
234+ 0 => 26,
235+ 1 => 31,
236+ _ => 36,
237+ }
238+}
239+
240+fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize {
241+ // libaom's `get_nz_map_ctx_from_stats` only special-cases the DC
242+ // coefficient to ctx 0 for `TX_CLASS_2D` (`(tx_class | coeff_idx) == 0`,
243+ // txb_common.h) -- `V_DCT`/`H_DCT` (class `Horiz`/`Vert`) still run the DC
244+ // position through the full 1D neighbour sum and offset table. Skipping
245+ // that for every class desynced the last (DC) coefficient of a
246+ // `TX_CLASS_HORIZ` block (lane-av1tx4 final round: real ctx 28 read as
247+ // 0, which then read one extra `BR` symbol at the max and forced a
248+ // spurious third Golomb call).
249+ if class == TxClass::TwoD && row == 0 && col == 0 {
176250 return 0;
177251 }
178- let mag: i32 = [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)]
252+ let offsets: [(usize, usize); 5] = match class {
253+ TxClass::TwoD => [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)],
254+ TxClass::Horiz => [(1, 0), (0, 1), (0, 2), (0, 3), (0, 4)],
255+ TxClass::Vert => [(1, 0), (0, 1), (2, 0), (3, 0), (4, 0)],
256+ };
257+ let mag: i32 = offsets
179258 .iter()
180259 .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3))
181260 .sum();
182- let offset = cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize;
183- (((mag + 1) >> 1).min(4) as usize) + offset
261+ let ctx = ((mag + 1) >> 1).min(4) as usize;
262+ match class {
263+ TxClass::TwoD => ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize,
264+ TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)),
265+ TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)),
266+ }
184267 }
185268
186-fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
187- let mag: i32 = [(1, 0), (0, 1), (1, 1)]
188- .iter()
189- .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs())
190- .sum();
269+fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize {
270+ let extra = match class {
271+ TxClass::TwoD => neighbour(grid, side, row + 1, col + 1).abs(),
272+ TxClass::Horiz => neighbour(grid, side, row, col + 2).abs(),
273+ TxClass::Vert => neighbour(grid, side, row + 2, col).abs(),
274+ };
275+ let mag = neighbour(grid, side, row + 1, col).abs()
276+ + neighbour(grid, side, row, col + 1).abs()
277+ + extra;
191278 let mag = (((mag + 1) >> 1).min(6)) as usize;
192279 if row == 0 && col == 0 {
193- mag
194- } else if row < 2 && col < 2 {
195- mag + 7
196- } else {
197- mag + 14
280+ return mag;
198281 }
282+ let near_origin = match class {
283+ TxClass::TwoD => row < 2 && col < 2,
284+ TxClass::Horiz => col == 0,
285+ TxClass::Vert => row == 0,
286+ };
287+ if near_origin { mag + 7 } else { mag + 14 }
199288 }
200289
201290 fn dc_vote(dc: Option<bool>) -> i32 {
@@ -234,12 +323,22 @@ fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> {
234323
235324 /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of
236325 /// [`crate::tile`]'s `write_eob`.
237-fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables) -> usize {
326+fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) -> usize {
238327 const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513];
239328 const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
240329
241330 let trace = std::env::var_os("EC_AV1_TRACE").is_some();
242- let group = dec.symbol(coding.eob_pt) + 1;
331+ // libaom's `eob_flag_cdf*` tables carry a second dimension the 2D scan
332+ // never touches: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` (`V_DCT`/`H_DCT`) adapt
333+ // a wholly separate CDF from every other tx_type (`decodetxb.c`'s
334+ // `eob_multi_ctx`). Missing that split desynced the very first
335+ // `V_DCT`/`H_DCT` TU this decoder ever produced (lane-av1tx4 r5, caught
336+ // against a real aomdec trace).
337+ let eob_pt: &mut [u16] = match (class, coding.eob_pt_class1.as_deref_mut()) {
338+ (TxClass::TwoD, _) | (_, None) => coding.eob_pt,
339+ (_, Some(class1)) => class1,
340+ };
341+ let group = dec.symbol(eob_pt) + 1;
243342 if trace {
244343 eprintln!("TRACE eob_pt value={group}");
245344 }
@@ -304,10 +403,21 @@ fn read_coeffs(
304403 .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?;
305404 }
306405
307- let eob = read_eob(dec, coding);
406+ let class = TxClass::of(tx_type);
407+ if class != TxClass::TwoD {
408+ TX_CLASS1_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
409+ }
410+ let eob = read_eob(dec, coding, class);
308411 if trace {
309412 eprintln!("TRACE eob value={eob}");
310413 }
414+ let class_scan;
415+ let scan: &[u16] = if class == TxClass::TwoD {
416+ scan
417+ } else {
418+ class_scan = class_scan_table(side, class);
419+ &class_scan
420+ };
311421 let mut levels = vec![0i32; side * side];
312422 for scan_idx in (0..eob).rev() {
313423 let pos = scan[scan_idx] as usize;
@@ -322,7 +432,7 @@ fn read_coeffs(
322432 }
323433 v
324434 } else {
325- let ctx = base_ctx(&levels, side, row, col);
435+ let ctx = base_ctx(&levels, side, row, col, class);
326436 let v = dec.symbol(&mut coding.base[ctx]) as i32;
327437 if trace {
328438 eprintln!(
@@ -332,7 +442,7 @@ fn read_coeffs(
332442 v
333443 };
334444 let level = if level > NUM_BASE_LEVELS {
335- let ctx = br_ctx(&levels, side, row, col);
445+ let ctx = br_ctx(&levels, side, row, col, class);
336446 let mut level = level;
337447 let mut sent = 0;
338448 loop {
@@ -1192,6 +1302,7 @@ fn decode_block(
11921302 scan32: &[u16],
11931303 scan16: &[u16],
11941304 scan8: &[u16],
1305+ scan4: &[u16],
11951306 tx_select: bool,
11961307 reduced_tx_set: bool,
11971308 ) -> Result<()> {
@@ -1215,11 +1326,6 @@ fn decode_block(
12151326 // which only differs from it at that one corner case.
12161327 let (logical_tx, coeff_tx_side) = if tx_select {
12171328 let resolved = read_tx_size(dec, cdfs, neighbours, (mi_r, mi_c), side);
1218- if resolved == 4 {
1219- return Err(unsupported(
1220- "a resolved 4x4 luma transform (TxMode::Select depth exhausted; this decoder has no 4x4 luma coefficient tables)",
1221- ));
1222- }
12231329 (resolved, if resolved == 64 { 32 } else { resolved })
12241330 } else {
12251331 (side, luma_tx)
@@ -1229,7 +1335,7 @@ fn decode_block(
12291335 } else {
12301336 luma_set
12311337 };
1232- let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8);
1338+ let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8, scan4);
12331339 let reach = Reach::of(side, px, py, y.width, y.height);
12341340 let (cpx, cpy) = (px / 2, py / 2);
12351341 let chroma_side = side / 2;
@@ -1381,6 +1487,14 @@ fn decode_block(
13811487 filter_intra,
13821488 Some(tu_skip_ctx),
13831489 )?;
1490+ if std::env::var_os("EC_AV1_TRACE").is_some() {
1491+ eprintln!(
1492+ "TRACE tu_bitpos mi=({},{}) tell={}",
1493+ tu_mi.0,
1494+ tu_mi.1,
1495+ dec.debug_bitpos()
1496+ );
1497+ }
13841498 neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid);
13851499 }
13861500 }
@@ -1487,18 +1601,15 @@ fn decode_leaf8(
14871601 enable_filter_intra,
14881602 )?;
14891603 // `TxMode::Select`'s `tx_depth` at an 8x8 leaf (spec 5.11.16): the only
1490- // depths an 8x8 block offers are `TX8` (depth 0, this decoder's one
1491- // supported case) and `TX4` (depth 1, which this crate has no 4x4 luma
1492- // coefficient tables for) -- read to stay in sync, then refuse rather
1493- // than silently miscode.
1494- if tx_select {
1495- let resolved = read_tx_size(dec, cdfs, neighbours, leaf_mi, 8);
1496- if resolved == 4 {
1497- return Err(unsupported(
1498- "a resolved 4x4 luma transform (TxMode::Select depth exhausted; this decoder has no 4x4 luma coefficient tables)",
1499- ));
1500- }
1501- }
1604+ // depths an 8x8 block offers are `TX8` (depth 0) and `TX4` (depth 1,
1605+ // which splits the leaf's own 8x8 prediction into a 2x2 grid of 4x4
1606+ // transform units, same raster-order per-TU pattern `decode_block`'s
1607+ // multi-TU branch runs).
1608+ let resolved = if tx_select {
1609+ read_tx_size(dec, cdfs, neighbours, leaf_mi, 8)
1610+ } else {
1611+ 8
1612+ };
15021613 let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
15031614 let reach = Reach::of(8, px, py, y.width, y.height);
15041615 let (cpx, cpy) = (px / 2, py / 2);
@@ -1529,7 +1640,7 @@ fn decode_leaf8(
15291640 luma_grid = vec![0i32; 64];
15301641 u_grid = vec![0i32; 16];
15311642 v_grid = vec![0i32; 16];
1532- } else {
1643+ } else if resolved != 4 {
15331644 let around = neighbours.around_mi(leaf_mi, 8);
15341645 luma_grid = read_plane(
15351646 dec,
@@ -1596,6 +1707,129 @@ fn decode_leaf8(
15961707 None,
15971708 None,
15981709 )?;
1710+ } else {
1711+ // `tx_depth` resolved this leaf's luma to `TX4`: a 2x2 raster-order
1712+ // grid of 4x4 transform units, each predicted from the leaf's own
1713+ // 8x8 `mode` and reading its own fresh context off the earlier
1714+ // units in this same leaf (the same per-TU pattern `decode_block`'s
1715+ // multi-TU branch runs at bigger blocks).
1716+ luma_grid = vec![0i32; 64];
1717+ for tu_row in 0..2 {
1718+ for tu_col in 0..2 {
1719+ let tu_mi = (leaf_mi.0 + tu_row, leaf_mi.1 + tu_col);
1720+ let tu_px = px + tu_col * 4;
1721+ let tu_py = py + tu_row * 4;
1722+ let tu_around = neighbours.around_mi(tu_mi, 4)[0];
1723+ let tu_reach = Reach::of(4, tu_px, tu_py, y.width, y.height);
1724+ let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1);
1725+ let tu_grid = read_plane(
1726+ dec,
1727+ cdfs,
1728+ if reduced_tx_set {
1729+ TxbSet::Luma4
1730+ } else {
1731+ TxbSet::Luma4Set1
1732+ },
1733+ scans.1,
1734+ 0,
1735+ tu_around,
1736+ mode,
1737+ mode,
1738+ tu_reach,
1739+ y,
1740+ tu_px,
1741+ tu_py,
1742+ 4,
1743+ 4,
1744+ base_q_idx,
1745+ None,
1746+ filter_intra,
1747+ Some(tu_skip_ctx),
1748+ )?;
1749+ if std::env::var_os("EC_AV1_TRACE").is_some() {
1750+ eprintln!(
1751+ "TRACE tu_bitpos mi=({},{}) tell={}",
1752+ tu_mi.0,
1753+ tu_mi.1,
1754+ dec.debug_bitpos()
1755+ );
1756+ }
1757+ neighbours.record_mi_luma(tu_mi, 4, &tu_grid);
1758+ }
1759+ }
1760+ let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8));
1761+ let chroma_around = neighbours.around_mi(leaf_mi, 8);
1762+ u_grid = read_plane(
1763+ dec,
1764+ cdfs,
1765+ TxbSet::Chroma4,
1766+ scans.1,
1767+ 1,
1768+ chroma_around[1],
1769+ mode,
1770+ DC_PRED,
1771+ Reach::none(),
1772+ u,
1773+ cpx,
1774+ cpy,
1775+ 4,
1776+ TX4,
1777+ base_q_idx,
1778+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
1779+ None,
1780+ None,
1781+ )?;
1782+ v_grid = read_plane(
1783+ dec,
1784+ cdfs,
1785+ TxbSet::Chroma4,
1786+ scans.1,
1787+ 2,
1788+ chroma_around[2],
1789+ mode,
1790+ DC_PRED,
1791+ Reach::none(),
1792+ v,
1793+ cpx,
1794+ cpy,
1795+ 4,
1796+ TX4,
1797+ base_q_idx,
1798+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
1799+ None,
1800+ None,
1801+ )?;
1802+ // `record_split_luma` expects a `record`-style `(r, c)` position in
1803+ // `SUB`-grid units and rescales it by `SUB / MI`; `leaf_mi` is
1804+ // already in mi units, so calling it here double-scaled the
1805+ // position. Plane 0 is already correct per-TU from the
1806+ // `record_mi_luma` calls above; this inlines `record_mi`'s tail
1807+ // (spec `get_txb_ctx_general`'s neighbour-magnitude bookkeeping) for
1808+ // the two chroma planes only, at `leaf_mi` directly, side_mi=2 (this
1809+ // leaf's own 8x8 side over `MI`).
1810+ let side_mi = 2;
1811+ for cell in 0..side_mi {
1812+ neighbours.left_side_mi[leaf_mi.0 + cell] = 8;
1813+ neighbours.above_side_mi[leaf_mi.1 + cell] = 8;
1814+ }
1815+ let round_up_even = |n: usize| n.div_ceil(2) * 2;
1816+ let bound_h = round_up_even(neighbours.mi_rows);
1817+ let bound_w = round_up_even(neighbours.mi_cols);
1818+ let u_state = neighbour_state(&u_grid);
1819+ let v_state = neighbour_state(&v_grid);
1820+ for cell in 0..side_mi {
1821+ if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) {
1822+ neighbours.left[leaf_mi.0 + cell][1] = u_state;
1823+ neighbours.left[leaf_mi.0 + cell][2] = v_state;
1824+ }
1825+ if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) {
1826+ neighbours.above[leaf_mi.1 + cell][1] = u_state;
1827+ neighbours.above[leaf_mi.1 + cell][2] = v_state;
1828+ }
1829+ }
1830+ neighbours.fill_skip_grid(leaf_mi, 2, skip);
1831+ neighbours.fill_lf_grid(leaf_mi, 2, 4, false);
1832+ return Ok(mode);
15991833 }
16001834 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
16011835 neighbours.fill_skip_grid(leaf_mi, 2, skip);
@@ -1875,14 +2109,21 @@ fn read_tx_size(
18752109 side >> depth
18762110 }
18772111
1878-/// The luma coefficient scan table for a resolved transform side -- 32/16/8,
1879-/// the only three sizes a `TxMode::Select` block's coded coefficient grid
2112+/// The luma coefficient scan table for a resolved transform side -- 32/16/8/4,
2113+/// the only four sizes a `TxMode::Select` block's coded coefficient grid
18802114 /// ([`read_tx_size`]'s resolved side, or 32 at the 64x64 corner case) can be.
1881-fn scan_for<'a>(tx_px: usize, scan32: &'a [u16], scan16: &'a [u16], scan8: &'a [u16]) -> &'a [u16] {
2115+fn scan_for<'a>(
2116+ tx_px: usize,
2117+ scan32: &'a [u16],
2118+ scan16: &'a [u16],
2119+ scan8: &'a [u16],
2120+ scan4: &'a [u16],
2121+) -> &'a [u16] {
18822122 match tx_px {
18832123 32 => scan32,
18842124 16 => scan16,
18852125 8 => scan8,
2126+ 4 => scan4,
18862127 _ => unreachable!("read_tx_size never resolves the coefficient grid to anything else"),
18872128 }
18882129 }
@@ -1891,11 +2132,11 @@ fn scan_for<'a>(tx_px: usize, scan32: &'a [u16], scan16: &'a [u16], scan8: &'a [
18912132 /// from -- 64 is only ever the 64x64-block, depth-0, corner-scanned case
18922133 /// ([`TxbSet::Luma64`]); every other resolved side maps to its own matching
18932134 /// set. `get_tx_set` (spec 5.11.48) only splits on `reduced_tx_set` at
1894-/// `TX_8X8`: an intra `TX_16X16` always reads `TX_SET_INTRA_2` regardless of
1895-/// the flag (`av1_get_ext_tx_set_type`'s `tx_size_sqr == TX_16X16` branch
1896-/// ignores `use_reduced_set` once it is past the `TX_32X32`/reduced-set
1897-/// checks), so only the 8x8 case has a second table
1898-/// ([`TxbSet::Luma8Set1`]).
2135+/// `TX_8X8`/`TX_4X4`: an intra `TX_16X16` always reads `TX_SET_INTRA_2`
2136+/// regardless of the flag (`av1_get_ext_tx_set_type`'s
2137+/// `tx_size_sqr == TX_16X16` branch ignores `use_reduced_set` once it is past
2138+/// the `TX_32X32`/reduced-set checks), so only the 8x8 and 4x4 cases have a
2139+/// second table ([`TxbSet::Luma8Set1`]/[`TxbSet::Luma4Set1`]).
18992140 fn txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet {
19002141 match tx_px {
19012142 64 => TxbSet::Luma64,
@@ -1903,6 +2144,8 @@ fn txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet {
19032144 16 => TxbSet::Luma16,
19042145 8 if reduced_tx_set => TxbSet::Luma8,
19052146 8 => TxbSet::Luma8Set1,
2147+ 4 if reduced_tx_set => TxbSet::Luma4,
2148+ 4 => TxbSet::Luma4Set1,
19062149 _ => unreachable!("read_tx_size never resolves a block's own side to anything else"),
19072150 }
19082151 }
@@ -2596,6 +2839,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
25962839 &scan32,
25972840 &scan16,
25982841 &scan8,
2842+ &scan4,
25992843 tx_select,
26002844 reduced_tx_set,
26012845 )?;
@@ -2615,7 +2859,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
26152859 let part32 = if has_cols32 && has_rows32 {
26162860 let p = dec.symbol(&mut cdfs.partition_w32[ctx32]);
26172861 if std::env::var_os("EC_AV1_TRACE").is_some() {
2618- eprintln!("TRACE partition_w32 ctx={ctx32} value={p}");
2862+ eprintln!(
2863+ "TRACE partition_w32 mi=({},{}) ctx={ctx32} value={p}",
2864+ r32 * BLOCK_MI,
2865+ c32 * BLOCK_MI
2866+ );
26192867 }
26202868 p
26212869 } else {
@@ -2658,6 +2906,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
26582906 &scan32,
26592907 &scan16,
26602908 &scan8,
2909+ &scan4,
26612910 tx_select,
26622911 reduced_tx_set,
26632912 )?;
@@ -2706,6 +2955,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
27062955 &scan32,
27072956 &scan16,
27082957 &scan8,
2958+ &scan4,
27092959 tx_select,
27102960 reduced_tx_set,
27112961 )?;
@@ -2733,6 +2983,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
27332983 let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
27342984 let part8 =
27352985 dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);
2986+ if std::env::var_os("EC_AV1_TRACE").is_some() {
2987+ eprintln!(
2988+ "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}"
2989+ );
2990+ }
27362991 if part8 != PARTITION_NONE {
27372992 return Err(unsupported(
27382993 "a partition below 8x8 (this encoder never writes one)",
@@ -2797,6 +3052,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
27973052 let leaf_mi = (mr as usize, mc as usize);
27983053 let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
27993054 let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);
3055+ if std::env::var_os("EC_AV1_TRACE").is_some() {
3056+ eprintln!(
3057+ "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}"
3058+ );
3059+ }
28003060 if part8 != PARTITION_NONE {
28013061 return Err(unsupported(
28023062 "a partition below 8x8 (this encoder never writes one)",
crates/ec-av1/src/encode.rsmodified+18 -4
@@ -1015,9 +1015,21 @@ impl Reach {
10151015 if col + 1 == per_side {
10161016 return false;
10171017 }
1018+ // corner-cut: `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` only carry dedicated
1019+ // rows for side 8/16/32/64 (libaom's own `has_tr_8x8`/`has_tr_16x16`
1020+ // families); a `TxMode::Select` TX4 transform unit reaches here with
1021+ // side 4, whose real table (`has_tr_4x4`, 128 bytes) was never
1022+ // transcribed. Clamping into the 16/32 table's own bit range keeps
1023+ // this in-bounds and correct at every position that table already
1024+ // covers periodically -- wrong only at the small remainder past its
1025+ // length, a reach over-/under-estimate (extra or missing above-right
1026+ // reference pixels), never a stream desync (Reach never gates a
1027+ // symbol read). Ceiling: transcribe `has_tr_4x4`/`has_bl_4x4`
1028+ // verbatim and give TX4 its own `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` row.
1029+ let table = HAS_TOP_RIGHT[Self::table(side)];
10181030 Self::bit(
1019- HAS_TOP_RIGHT[Self::table(side)],
1020- row * Self::table_stride(side) + col,
1031+ table,
1032+ (row * Self::table_stride(side) + col) % (table.len() * 8),
10211033 )
10221034 }
10231035
@@ -1033,9 +1045,11 @@ impl Reach {
10331045 if row + 1 == per_side {
10341046 return false;
10351047 }
1048+ // corner-cut: see `top_right`'s note -- same clamp, same ceiling.
1049+ let table = HAS_BOTTOM_LEFT[Self::table(side)];
10361050 Self::bit(
1037- HAS_BOTTOM_LEFT[Self::table(side)],
1038- row * Self::table_stride(side) + col,
1051+ table,
1052+ (row * Self::table_stride(side) + col) % (table.len() * 8),
10391053 )
10401054 }
10411055
crates/ec-av1/src/msac.rsmodified+7 -0
@@ -337,6 +337,13 @@ impl<'a> SymbolDecoder<'a> {
337337 symbol
338338 }
339339
340+ /// Debug-only: the current bit offset into `data`, for cross-checking
341+ /// against a reference decoder's own `aom_reader_tell()` while chasing a
342+ /// desync (see `EC_AV1_TRACE`). Not spec state, just `self.bit`.
343+ pub(crate) fn debug_bitpos(&self) -> usize {
344+ self.bit
345+ }
346+
340347 /// Reads `bits` raw bits, most significant first — the `L(n)` descriptor
341348 /// of spec 4.10.4.
342349 pub fn literal(&mut self, bits: u32) -> u32 {
crates/ec-av1/src/stream.rsmodified+156 -0
@@ -1563,4 +1563,160 @@ mod tests {
15631563 }
15641564 }
15651565 }
1566+
1567+ /// Final round of the TX4/tx-class lane's own gate: `--min-partition-size=8
1568+ /// --max-partition-size=8` pins every leaf to exactly 8x8 (spec
1569+ /// 5.11.4's own boundary carve-out aside, which a multiple-of-8 frame
1570+ /// never triggers), so a real `aomenc` stream from this recipe can only
1571+ /// legitimately read `PARTITION_NONE` at every `bsize=3` node -- any
1572+ /// other value this decoder sees there is a genuine desync, not a
1573+ /// "partition below 8x8" the encoder actually wrote (traced live against
1574+ /// a debug `aomdec` 2026-08-27: the true bitstream's own 8x8 partition
1575+ /// reads are all `NONE`; the mismatch was `base_ctx`'s DC-position
1576+ /// short-circuit firing for `TX_CLASS_HORIZ`/`VERT` too, off by one
1577+ /// `BR` read into a spurious extra Golomb call). The sinusoidal-stripe
1578+ /// `geq` source (a smooth luma ramp, not the sharp two-tone stripes that
1579+ /// tripped `aomenc`'s screen-content-tools auto-detect) is what actually
1580+ /// resolves `V_DCT`/`H_DCT` (`TxClass::of`) on some 8x8 TUs at this
1581+ /// content's frequency; the attempt loop moves on from named refusals
1582+ /// (`allow_screen_content_tools`, angle-delta) same as every other
1583+ /// multi-seed recipe in this file, and only counts a run as a genuine
1584+ /// firing when [`decode::tx_class1_hits`] actually moved.
1585+ #[test]
1586+ fn a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact() {
1587+ if !have_ffmpeg() {
1588+ eprintln!(
1589+ "SKIP a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact: no ffmpeg"
1590+ );
1591+ return;
1592+ }
1593+ if !have_aomenc() {
1594+ eprintln!(
1595+ "SKIP a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact: no aomenc at {}",
1596+ aomenc_path().display()
1597+ );
1598+ return;
1599+ }
1600+ let (width, height) = (64usize, 64usize);
1601+ let mut refusals = Vec::new();
1602+ let mut fired_runs = 0u32;
1603+ for attempt in 0..60u32 {
1604+ let cq = 20 + (attempt % 5) * 10;
1605+ let period = [4u32, 6, 8, 12, 16][(attempt as usize / 5) % 5];
1606+ let y4m = Command::new("ffmpeg")
1607+ .args([
1608+ "-v",
1609+ "error",
1610+ "-f",
1611+ "lavfi",
1612+ "-i",
1613+ "testsrc2=size=64x64:rate=25",
1614+ "-vf",
1615+ &format!(
1616+ "geq=lum='128+80*sin(2*PI*Y/{period})':cb=128:cr=128,noise=alls=6:allf=t"
1617+ ),
1618+ "-pix_fmt",
1619+ "yuv420p",
1620+ "-t",
1621+ "0.04",
1622+ "-f",
1623+ "yuv4mpegpipe",
1624+ "-",
1625+ ])
1626+ .stdin(Stdio::null())
1627+ .stdout(Stdio::piped())
1628+ .stderr(Stdio::piped())
1629+ .output()
1630+ .expect("ffmpeg failed to run");
1631+ assert!(
1632+ y4m.status.success(),
1633+ "ffmpeg fixture: {}",
1634+ String::from_utf8_lossy(&y4m.stderr)
1635+ );
1636+ let mut child = Command::new(aomenc_path())
1637+ .args([
1638+ "--codec=av1",
1639+ "--passes=1",
1640+ "--end-usage=q",
1641+ &format!("--cq-level={cq}"),
1642+ "--cpu-used=0",
1643+ "--enable-directional-intra=0",
1644+ "--enable-smooth-intra=0",
1645+ "--enable-paeth-intra=0",
1646+ "--enable-rect-partitions=0",
1647+ "--enable-angle-delta=0",
1648+ "--enable-palette=0",
1649+ "--reduced-tx-type-set=0",
1650+ "--enable-restoration=0",
1651+ "--enable-cdef=0",
1652+ "--loopfilter-control=0",
1653+ "--min-partition-size=8",
1654+ "--max-partition-size=8",
1655+ "--enable-ab-partitions=0",
1656+ "--enable-1to4-partitions=0",
1657+ "--enable-filter-intra=0",
1658+ "--enable-intrabc=0",
1659+ "--enable-cfl-intra=0",
1660+ "--tune-content=default",
1661+ "--obu",
1662+ "-o",
1663+ "-",
1664+ "-",
1665+ ])
1666+ .stdin(Stdio::piped())
1667+ .stdout(Stdio::piped())
1668+ .stderr(Stdio::piped())
1669+ .spawn()
1670+ .expect("aomenc failed to start");
1671+ child
1672+ .stdin
1673+ .take()
1674+ .expect("aomenc stdin")
1675+ .write_all(&y4m.stdout)
1676+ .expect("writing y4m to aomenc");
1677+ let out = child.wait_with_output().expect("aomenc failed to run");
1678+ if !out.status.success() {
1679+ refusals.push(format!(
1680+ "cq={cq} period={period}: aomenc itself refused the fixture"
1681+ ));
1682+ continue;
1683+ }
1684+ let stream = out.stdout;
1685+ let before = decode::tx_class1_hits();
1686+ let frames = match decode_stream(&stream) {
1687+ Ok(frames) => frames,
1688+ Err(e) => {
1689+ refusals.push(format!("cq={cq} period={period}: {e}"));
1690+ continue;
1691+ }
1692+ };
1693+ if decode::tx_class1_hits() == before {
1694+ refusals.push(format!(
1695+ "cq={cq} period={period}: decoded, but no block read a V_DCT/H_DCT tx_type"
1696+ ));
1697+ continue;
1698+ }
1699+ let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1);
1700+ assert_eq!(
1701+ frames[0].y, ffmpeg_frames[0].y,
1702+ "luma vs ffmpeg (cq={cq} period={period})"
1703+ );
1704+ assert_eq!(
1705+ frames[0].u, ffmpeg_frames[0].u,
1706+ "U vs ffmpeg (cq={cq} period={period})"
1707+ );
1708+ assert_eq!(
1709+ frames[0].v, ffmpeg_frames[0].v,
1710+ "V vs ffmpeg (cq={cq} period={period})"
1711+ );
1712+ fired_runs += 1;
1713+ if fired_runs >= 4 {
1714+ return;
1715+ }
1716+ }
1717+ panic!(
1718+ "fewer than 4 firing+pixel-exact runs out of 60 attempts:\n{}",
1719+ refusals.join("\n")
1720+ );
1721+ }
15661722 }