forge
Explore
Log in

Don't you dare go Hollow

V_DCT/H_DCT coefficient scan order + TX4 leaf8 scan table mixup

Browse files at this commit

700543bbda9fa5580b575506d58e73f10cb1aaa6Ahmet Kaan GümüşParent: 0c92312
  • fix
    • V_DCT/H_DCT coefficient scan order + TX4 leaf8 scan table mixup
Show full message
fix(av1): V_DCT/H_DCT coefficient scan order + TX4 leaf8 scan table mixup

Two real desync causes past r3's tx-class context-derivation fix:

1. read_coeffs always read coefficients in the default zigzag scan
   regardless of tx_type. libaom's V_DCT/H_DCT classes use Mrow_Scan/
   Mcol_Scan instead (scan.c av1_scan_orders index 10/11) -- decoded
   here as class_scan_table: V_DCT's class-scan is our grid's own
   natural row-major order (0..N), H_DCT's is that walk transposed.
   base_ctx/br_ctx's row/col-offset formulas (r3's fix) were already
   correct; only the scan order feeding position/row/col into them
   was wrong.

2. decode_leaf8's TX4 split loop (an 8x8 leaf's tx_depth=1 case) read
   its four 4x4 luma transform units with `scans.0` -- the leaf's own
   *8x8* scan table (64 entries) -- instead of `scans.1` (the 4x4
   table), so `pos` values up to 63 got written into a 16-cell grid.
   Pre-existing bug, only reachable/visible once (1) stopped eating
   the stream earlier.

5-scale probe (examples/probe_tx4.rs, /tmp/tx4work/s*.obu) moved
past every prior refusal point to new, later ones (scale 1.5 now
refuses on "a directional chroma mode (round 2)" instead of an
angle_delta/partition desync) -- real progress, not yet clean;
next round needs the same trace-correlation to chase the remaining
divergence(s). Suite green 172/172.
crates/ec-av1/examples/probe_tx4.rsadded+29 -0
@@ -0,0 +1,29 @@
1+use std::io::Write;
2+use std::process::{Command, Stdio};
3+
4+fn main() {
5+ let path = std::env::args().nth(1).expect("obu path");
6+ let stream = std::fs::read(&path).expect("read");
7+ let frames = match ec_av1::stream::decode_stream(&stream) {
8+ Ok(f) => f,
9+ Err(e) => {
10+ println!("REFUSED: {e}");
11+ std::process::exit(1);
12+ }
13+ };
14+ let mut child = Command::new("ffmpeg")
15+ .args([
16+ "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-",
17+ ])
18+ .stdin(Stdio::piped())
19+ .stdout(Stdio::piped())
20+ .spawn()
21+ .expect("ffmpeg");
22+ child.stdin.take().unwrap().write_all(&stream).unwrap();
23+ let out = child.wait_with_output().unwrap().stdout;
24+ let f = &frames[0];
25+ let mut ours = f.y.clone();
26+ ours.extend_from_slice(&f.u);
27+ ours.extend_from_slice(&f.v);
28+ println!("match={}", ours == out[..ours.len()]);
29+}
crates/ec-av1/src/cdf.rsmodified+430 -0
@@ -3908,3 +3908,433 @@ pub const TXB_SKIP_LUMA_64_Q3_CTX: [[u16; 3]; 7] = [
39083908 [30026, 32768, 0],
39093909 [32441, 32768, 0],
39103910 ];
3911+
3912+// The tables below extend the `_LUMA_8`/`Luma8Set1` straddling-16x16 support
3913+// above to a resolved `TX4` (spec `TX_4X4`) luma transform unit: a
3914+// `TxMode::Select` 8x8 leaf or 16x16 block whose `tx_depth` read exhausts
3915+// down to the smallest size (lane-av1tx4). Extracted byte-exact from
3916+// libaom's `token_cdfs.h` `TX_SIZES` idx0 (`TX_4X4`)/`PLANE_TYPES` idx0
3917+// (luma), the same indexing method that verified the `_LUMA_8`/`COEFF_BR_LUMA_8`
3918+// families above.
3919+
3920+/// The full luma sub-range of spec `TXB_SKIP_CONTEXTS` (contexts 0..6) for a
3921+/// resolved 4x4 luma transform, same convention as [`TXB_SKIP_LUMA_8_CTX`].
3922+pub const TXB_SKIP_LUMA_4_Q0_CTX: [[u16; 3]; 7] = [
3923+ [31849, 32768, 0],
3924+ [5892, 32768, 0],
3925+ [12112, 32768, 0],
3926+ [21935, 32768, 0],
3927+ [20289, 32768, 0],
3928+ [27473, 32768, 0],
3929+ [32487, 32768, 0],
3930+];
3931+pub const TXB_SKIP_LUMA_4_Q1_CTX: [[u16; 3]; 7] = [
3932+ [30371, 32768, 0],
3933+ [7570, 32768, 0],
3934+ [13155, 32768, 0],
3935+ [20751, 32768, 0],
3936+ [20969, 32768, 0],
3937+ [27067, 32768, 0],
3938+ [32013, 32768, 0],
3939+];
3940+pub const TXB_SKIP_LUMA_4_CTX: [[u16; 3]; 7] = [
3941+ [29614, 32768, 0],
3942+ [9068, 32768, 0],
3943+ [12924, 32768, 0],
3944+ [19538, 32768, 0],
3945+ [17737, 32768, 0],
3946+ [24619, 32768, 0],
3947+ [30642, 32768, 0],
3948+];
3949+pub const TXB_SKIP_LUMA_4_Q3_CTX: [[u16; 3]; 7] = [
3950+ [26887, 32768, 0],
3951+ [6729, 32768, 0],
3952+ [10361, 32768, 0],
3953+ [17442, 32768, 0],
3954+ [15045, 32768, 0],
3955+ [22478, 32768, 0],
3956+ [29072, 32768, 0],
3957+];
3958+
3959+/// The end-of-block group of a 4x4 luma transform (`av1_default_eob_multi16_cdfs`,
3960+/// `PLANE_TYPES` idx0 luma).
3961+pub const EOB_PT_16_LUMA_Q0: [u16; 6] = [840, 1039, 1980, 4895, 32768, 0];
3962+pub const EOB_PT_16_LUMA_Q1: [u16; 6] = [2125, 2551, 5165, 8946, 32768, 0];
3963+pub const EOB_PT_16_LUMA: [u16; 6] = [4016, 4897, 8881, 14968, 32768, 0];
3964+pub const EOB_PT_16_LUMA_Q3: [u16; 6] = [6708, 8958, 14746, 22133, 32768, 0];
3965+
3966+// EOB_EXTRA_LUMA_4
3967+pub const EOB_EXTRA_LUMA_4_Q0: [[u16; 3]; 9] = [
3968+ [16961, 32768, 0],
3969+ [17223, 32768, 0],
3970+ [7621, 32768, 0],
3971+ [16384, 32768, 0],
3972+ [16384, 32768, 0],
3973+ [16384, 32768, 0],
3974+ [16384, 32768, 0],
3975+ [16384, 32768, 0],
3976+ [16384, 32768, 0],
3977+];
3978+
3979+pub const EOB_EXTRA_LUMA_4_Q1: [[u16; 3]; 9] = [
3980+ [17471, 32768, 0],
3981+ [20223, 32768, 0],
3982+ [11357, 32768, 0],
3983+ [16384, 32768, 0],
3984+ [16384, 32768, 0],
3985+ [16384, 32768, 0],
3986+ [16384, 32768, 0],
3987+ [16384, 32768, 0],
3988+ [16384, 32768, 0],
3989+];
3990+
3991+pub const EOB_EXTRA_LUMA_4: [[u16; 3]; 9] = [
3992+ [18983, 32768, 0],
3993+ [20512, 32768, 0],
3994+ [14885, 32768, 0],
3995+ [16384, 32768, 0],
3996+ [16384, 32768, 0],
3997+ [16384, 32768, 0],
3998+ [16384, 32768, 0],
3999+ [16384, 32768, 0],
4000+ [16384, 32768, 0],
4001+];
4002+
4003+pub const EOB_EXTRA_LUMA_4_Q3: [[u16; 3]; 9] = [
4004+ [20177, 32768, 0],
4005+ [20789, 32768, 0],
4006+ [20262, 32768, 0],
4007+ [16384, 32768, 0],
4008+ [16384, 32768, 0],
4009+ [16384, 32768, 0],
4010+ [16384, 32768, 0],
4011+ [16384, 32768, 0],
4012+ [16384, 32768, 0],
4013+];
4014+
4015+// COEFF_BASE_LUMA_4
4016+pub const COEFF_BASE_LUMA_4_Q0: [[u16; 5]; 42] = [
4017+ [4034, 8930, 12727, 32768, 0],
4018+ [18082, 29741, 31877, 32768, 0],
4019+ [12596, 26124, 30493, 32768, 0],
4020+ [9446, 21118, 27005, 32768, 0],
4021+ [6308, 15141, 21279, 32768, 0],
4022+ [2463, 6357, 9783, 32768, 0],
4023+ [20667, 30546, 31929, 32768, 0],
4024+ [13043, 26123, 30134, 32768, 0],
4025+ [8151, 18757, 24778, 32768, 0],
4026+ [5255, 12839, 18632, 32768, 0],
4027+ [2820, 7206, 11161, 32768, 0],
4028+ [8192, 16384, 24576, 32768, 0],
4029+ [8192, 16384, 24576, 32768, 0],
4030+ [8192, 16384, 24576, 32768, 0],
4031+ [8192, 16384, 24576, 32768, 0],
4032+ [8192, 16384, 24576, 32768, 0],
4033+ [8192, 16384, 24576, 32768, 0],
4034+ [8192, 16384, 24576, 32768, 0],
4035+ [8192, 16384, 24576, 32768, 0],
4036+ [8192, 16384, 24576, 32768, 0],
4037+ [8192, 16384, 24576, 32768, 0],
4038+ [15736, 27553, 30604, 32768, 0],
4039+ [11210, 23794, 28787, 32768, 0],
4040+ [5947, 13874, 19701, 32768, 0],
4041+ [4215, 9323, 13891, 32768, 0],
4042+ [2833, 6462, 10059, 32768, 0],
4043+ [19605, 30393, 31582, 32768, 0],
4044+ [13523, 26252, 30248, 32768, 0],
4045+ [8446, 18622, 24512, 32768, 0],
4046+ [3818, 10343, 15974, 32768, 0],
4047+ [1481, 4117, 6796, 32768, 0],
4048+ [22649, 31302, 32190, 32768, 0],
4049+ [14829, 27127, 30449, 32768, 0],
4050+ [8313, 17702, 23304, 32768, 0],
4051+ [3022, 8301, 12786, 32768, 0],
4052+ [1536, 4412, 7184, 32768, 0],
4053+ [22354, 29774, 31372, 32768, 0],
4054+ [14723, 25472, 29214, 32768, 0],
4055+ [6673, 13745, 18662, 32768, 0],
4056+ [2068, 5766, 9322, 32768, 0],
4057+ [8192, 16384, 24576, 32768, 0],
4058+ [8192, 16384, 24576, 32768, 0],
4059+];
4060+
4061+pub const COEFF_BASE_LUMA_4_Q1: [[u16; 5]; 42] = [
4062+ [6041, 11854, 15927, 32768, 0],
4063+ [20326, 30905, 32251, 32768, 0],
4064+ [14164, 26831, 30725, 32768, 0],
4065+ [9760, 20647, 26585, 32768, 0],
4066+ [6416, 14953, 21219, 32768, 0],
4067+ [2966, 7151, 10891, 32768, 0],
4068+ [23567, 31374, 32254, 32768, 0],
4069+ [14978, 27416, 30946, 32768, 0],
4070+ [9434, 20225, 26254, 32768, 0],
4071+ [6658, 14558, 20535, 32768, 0],
4072+ [3916, 8677, 12989, 32768, 0],
4073+ [8192, 16384, 24576, 32768, 0],
4074+ [8192, 16384, 24576, 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+ [18088, 29545, 31587, 32768, 0],
4084+ [13062, 25843, 30073, 32768, 0],
4085+ [8940, 16827, 22251, 32768, 0],
4086+ [7654, 13220, 17973, 32768, 0],
4087+ [5733, 10316, 14456, 32768, 0],
4088+ [22879, 31388, 32114, 32768, 0],
4089+ [15215, 27993, 30955, 32768, 0],
4090+ [9397, 19445, 24978, 32768, 0],
4091+ [3442, 9813, 15344, 32768, 0],
4092+ [1368, 3936, 6532, 32768, 0],
4093+ [25494, 32033, 32406, 32768, 0],
4094+ [16772, 27963, 30718, 32768, 0],
4095+ [9419, 18165, 23260, 32768, 0],
4096+ [2677, 7501, 11797, 32768, 0],
4097+ [1516, 4344, 7170, 32768, 0],
4098+ [26556, 31454, 32101, 32768, 0],
4099+ [17128, 27035, 30108, 32768, 0],
4100+ [8324, 15344, 20249, 32768, 0],
4101+ [1903, 5696, 9469, 32768, 0],
4102+ [8192, 16384, 24576, 32768, 0],
4103+ [8192, 16384, 24576, 32768, 0],
4104+];
4105+
4106+pub const COEFF_BASE_LUMA_4: [[u16; 5]; 42] = [
4107+ [8896, 16227, 20630, 32768, 0],
4108+ [23629, 31782, 32527, 32768, 0],
4109+ [15173, 27755, 31321, 32768, 0],
4110+ [10158, 21233, 27382, 32768, 0],
4111+ [6420, 14857, 21558, 32768, 0],
4112+ [3269, 8155, 12646, 32768, 0],
4113+ [24835, 32009, 32496, 32768, 0],
4114+ [16509, 28421, 31579, 32768, 0],
4115+ [10957, 21514, 27418, 32768, 0],
4116+ [7881, 15930, 22096, 32768, 0],
4117+ [5388, 10960, 15918, 32768, 0],
4118+ [8192, 16384, 24576, 32768, 0],
4119+ [8192, 16384, 24576, 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+ [20745, 30773, 32093, 32768, 0],
4129+ [15200, 27221, 30861, 32768, 0],
4130+ [13032, 20873, 25667, 32768, 0],
4131+ [12285, 18663, 23494, 32768, 0],
4132+ [11563, 17481, 21489, 32768, 0],
4133+ [26260, 31982, 32320, 32768, 0],
4134+ [15397, 28083, 31100, 32768, 0],
4135+ [9742, 19217, 24824, 32768, 0],
4136+ [3261, 9629, 15362, 32768, 0],
4137+ [1480, 4322, 7499, 32768, 0],
4138+ [27599, 32256, 32460, 32768, 0],
4139+ [16857, 27659, 30774, 32768, 0],
4140+ [9551, 18290, 23748, 32768, 0],
4141+ [3052, 8933, 14103, 32768, 0],
4142+ [2021, 5910, 9787, 32768, 0],
4143+ [29005, 32015, 32392, 32768, 0],
4144+ [17677, 27694, 30863, 32768, 0],
4145+ [9204, 17356, 23219, 32768, 0],
4146+ [2403, 7516, 12814, 32768, 0],
4147+ [8192, 16384, 24576, 32768, 0],
4148+ [8192, 16384, 24576, 32768, 0],
4149+];
4150+
4151+pub const COEFF_BASE_LUMA_4_Q3: [[u16; 5]; 42] = [
4152+ [7062, 16472, 22319, 32768, 0],
4153+ [24538, 32261, 32674, 32768, 0],
4154+ [13675, 28041, 31779, 32768, 0],
4155+ [8590, 20674, 27631, 32768, 0],
4156+ [5685, 14675, 22013, 32768, 0],
4157+ [3655, 9898, 15731, 32768, 0],
4158+ [26493, 32418, 32658, 32768, 0],
4159+ [16376, 29342, 32090, 32768, 0],
4160+ [10594, 22649, 28970, 32768, 0],
4161+ [8176, 17170, 24303, 32768, 0],
4162+ [5605, 12694, 19139, 32768, 0],
4163+ [8192, 16384, 24576, 32768, 0],
4164+ [8192, 16384, 24576, 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+ [23888, 31902, 32542, 32768, 0],
4174+ [18612, 29687, 31987, 32768, 0],
4175+ [16245, 24852, 29249, 32768, 0],
4176+ [15765, 22608, 27559, 32768, 0],
4177+ [19895, 24699, 27510, 32768, 0],
4178+ [28401, 32212, 32457, 32768, 0],
4179+ [15274, 27825, 30980, 32768, 0],
4180+ [9364, 18128, 24332, 32768, 0],
4181+ [2283, 8193, 15082, 32768, 0],
4182+ [1228, 3972, 7881, 32768, 0],
4183+ [29455, 32469, 32620, 32768, 0],
4184+ [17981, 28245, 31388, 32768, 0],
4185+ [10921, 20098, 26240, 32768, 0],
4186+ [3743, 11829, 18657, 32768, 0],
4187+ [2374, 9593, 15715, 32768, 0],
4188+ [31068, 32466, 32635, 32768, 0],
4189+ [20321, 29572, 31971, 32768, 0],
4190+ [10771, 20255, 27119, 32768, 0],
4191+ [2795, 10410, 17361, 32768, 0],
4192+ [8192, 16384, 24576, 32768, 0],
4193+ [8192, 16384, 24576, 32768, 0],
4194+];
4195+
4196+// COEFF_BASE_EOB_LUMA_4
4197+pub const COEFF_BASE_EOB_LUMA_4_Q0: [[u16; 4]; 4] = [
4198+ [17837, 29055, 32768, 0],
4199+ [29600, 31446, 32768, 0],
4200+ [30844, 31878, 32768, 0],
4201+ [24926, 28948, 32768, 0],
4202+];
4203+
4204+pub const COEFF_BASE_EOB_LUMA_4_Q1: [[u16; 4]; 4] = [
4205+ [17560, 29888, 32768, 0],
4206+ [29671, 31549, 32768, 0],
4207+ [31007, 32056, 32768, 0],
4208+ [27286, 30006, 32768, 0],
4209+];
4210+
4211+pub const COEFF_BASE_EOB_LUMA_4: [[u16; 4]; 4] = [
4212+ [20092, 30774, 32768, 0],
4213+ [30695, 32020, 32768, 0],
4214+ [31131, 32103, 32768, 0],
4215+ [28666, 30870, 32768, 0],
4216+];
4217+
4218+pub const COEFF_BASE_EOB_LUMA_4_Q3: [[u16; 4]; 4] = [
4219+ [22497, 31198, 32768, 0],
4220+ [31715, 32495, 32768, 0],
4221+ [31606, 32337, 32768, 0],
4222+ [30388, 31990, 32768, 0],
4223+];
4224+
4225+// COEFF_BR_LUMA_4
4226+pub const COEFF_BR_LUMA_4_Q0: [[u16; 5]; 21] = [
4227+ [14298, 20718, 24174, 32768, 0],
4228+ [12536, 19601, 23789, 32768, 0],
4229+ [8712, 15051, 19503, 32768, 0],
4230+ [6170, 11327, 15434, 32768, 0],
4231+ [4742, 8926, 12538, 32768, 0],
4232+ [3803, 7317, 10546, 32768, 0],
4233+ [1696, 3317, 4871, 32768, 0],
4234+ [14392, 19951, 22756, 32768, 0],
4235+ [15978, 23218, 26818, 32768, 0],
4236+ [12187, 19474, 23889, 32768, 0],
4237+ [9176, 15640, 20259, 32768, 0],
4238+ [7068, 12655, 17028, 32768, 0],
4239+ [5656, 10442, 14472, 32768, 0],
4240+ [2580, 4992, 7244, 32768, 0],
4241+ [12136, 18049, 21426, 32768, 0],
4242+ [13784, 20721, 24481, 32768, 0],
4243+ [10836, 17621, 21900, 32768, 0],
4244+ [8372, 14444, 18847, 32768, 0],
4245+ [6523, 11779, 16000, 32768, 0],
4246+ [5337, 9898, 13760, 32768, 0],
4247+ [3034, 5860, 8462, 32768, 0],
4248+];
4249+
4250+pub const COEFF_BR_LUMA_4_Q1: [[u16; 5]; 21] = [
4251+ [14995, 21341, 24749, 32768, 0],
4252+ [13158, 20289, 24601, 32768, 0],
4253+ [8941, 15326, 19876, 32768, 0],
4254+ [6297, 11541, 15807, 32768, 0],
4255+ [4817, 9029, 12776, 32768, 0],
4256+ [3731, 7273, 10627, 32768, 0],
4257+ [1847, 3617, 5354, 32768, 0],
4258+ [14472, 19659, 22343, 32768, 0],
4259+ [16806, 24162, 27533, 32768, 0],
4260+ [12900, 20404, 24713, 32768, 0],
4261+ [9411, 16112, 20797, 32768, 0],
4262+ [7056, 12697, 17148, 32768, 0],
4263+ [5544, 10339, 14460, 32768, 0],
4264+ [2954, 5704, 8319, 32768, 0],
4265+ [12464, 18071, 21354, 32768, 0],
4266+ [15482, 22528, 26034, 32768, 0],
4267+ [12070, 19269, 23624, 32768, 0],
4268+ [8953, 15406, 20106, 32768, 0],
4269+ [7027, 12730, 17220, 32768, 0],
4270+ [5887, 10913, 15140, 32768, 0],
4271+ [3793, 7278, 10447, 32768, 0],
4272+];
4273+
4274+pub const COEFF_BR_LUMA_4: [[u16; 5]; 21] = [
4275+ [16138, 22223, 25509, 32768, 0],
4276+ [15347, 22430, 26332, 32768, 0],
4277+ [9614, 16736, 21332, 32768, 0],
4278+ [6600, 12275, 16907, 32768, 0],
4279+ [4811, 9424, 13547, 32768, 0],
4280+ [3748, 7809, 11420, 32768, 0],
4281+ [2254, 4587, 6890, 32768, 0],
4282+ [15196, 20284, 23177, 32768, 0],
4283+ [18317, 25469, 28451, 32768, 0],
4284+ [13918, 21651, 25842, 32768, 0],
4285+ [10052, 17150, 21995, 32768, 0],
4286+ [7499, 13630, 18587, 32768, 0],
4287+ [6158, 11417, 16003, 32768, 0],
4288+ [4014, 7785, 11252, 32768, 0],
4289+ [15048, 21067, 24384, 32768, 0],
4290+ [18202, 25346, 28553, 32768, 0],
4291+ [14302, 22019, 26356, 32768, 0],
4292+ [10839, 18139, 23166, 32768, 0],
4293+ [8715, 15744, 20806, 32768, 0],
4294+ [7536, 13576, 18544, 32768, 0],
4295+ [5413, 10335, 14498, 32768, 0],
4296+];
4297+
4298+pub const COEFF_BR_LUMA_4_Q3: [[u16; 5]; 21] = [
4299+ [18315, 24289, 27551, 32768, 0],
4300+ [16854, 24068, 27835, 32768, 0],
4301+ [10140, 17927, 23173, 32768, 0],
4302+ [6722, 12982, 18267, 32768, 0],
4303+ [4661, 9826, 14706, 32768, 0],
4304+ [3832, 8165, 12294, 32768, 0],
4305+ [2795, 6098, 9245, 32768, 0],
4306+ [17145, 23326, 26672, 32768, 0],
4307+ [20733, 27680, 30308, 32768, 0],
4308+ [16032, 24461, 28546, 32768, 0],
4309+ [11653, 20093, 25081, 32768, 0],
4310+ [9290, 16429, 22086, 32768, 0],
4311+ [7796, 14598, 19982, 32768, 0],
4312+ [6502, 12378, 17441, 32768, 0],
4313+ [21681, 27732, 30320, 32768, 0],
4314+ [22389, 29044, 31261, 32768, 0],
4315+ [19027, 26731, 30087, 32768, 0],
4316+ [14739, 23755, 28624, 32768, 0],
4317+ [11358, 20778, 25511, 32768, 0],
4318+ [10995, 18073, 24190, 32768, 0],
4319+ [9162, 14990, 20617, 32768, 0],
4320+];
4321+
4322+/// `Default_Intra_Ext_Tx_Cdf`'s `TX_SET_INTRA_1` row for `TX_4X4` (spec
4323+/// 9.4): the transform type of an intra 4x4 transform when `reduced_tx_set`
4324+/// is false. Same `Luma8`/`Luma8Set1` split as [`INTRA_TX_TYPE_SET1_8`],
4325+/// kept named for the day a non-reduced path exists.
4326+pub const INTRA_TX_TYPE_SET1_4: [[u16; 8]; 13] = [
4327+ [1535, 8035, 9461, 12751, 23467, 27825, 32768, 0],
4328+ [564, 3335, 9709, 10870, 18143, 28094, 32768, 0],
4329+ [672, 3247, 3676, 11982, 19415, 23127, 32768, 0],
4330+ [5279, 13885, 15487, 18044, 23527, 30252, 32768, 0],
4331+ [4423, 6074, 7985, 10416, 25693, 29298, 32768, 0],
4332+ [1486, 4241, 9460, 10662, 16456, 27694, 32768, 0],
4333+ [439, 2838, 3522, 6737, 18058, 23754, 32768, 0],
4334+ [1190, 4233, 4855, 11670, 20281, 24377, 32768, 0],
4335+ [1045, 4312, 8647, 10159, 18644, 29335, 32768, 0],
4336+ [202, 3734, 4747, 7298, 17127, 24016, 32768, 0],
4337+ [447, 4312, 6819, 8884, 16010, 23858, 32768, 0],
4338+ [277, 4369, 5255, 8905, 16465, 22271, 32768, 0],
4339+ [3409, 5436, 10599, 15599, 19687, 24040, 32768, 0],
4340+];
crates/ec-av1/src/cdf_state.rsmodified+103 -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).
@@ -128,6 +136,8 @@ pub(crate) struct Cdfs {
128136 pub txb_skip_luma_16: [[u16; 3]; 7],
129137 /// The same, for an 8x8 luma transform.
130138 pub txb_skip_luma_8: [[u16; 3]; 7],
139+ /// The same, for a 4x4 luma transform (lane-av1tx4).
140+ pub txb_skip_luma_4: [[u16; 3]; 7],
131141 /// The all-zero flag of an 8x8 chroma transform.
132142 pub txb_skip_chroma_8: [[u16; 3]; 3],
133143 /// The all-zero flag of a 4x4 chroma transform.
@@ -140,10 +150,14 @@ pub(crate) struct Cdfs {
140150 pub eob_pt_64_chroma: [u16; 8],
141151 /// The end-of-block group of a 4x4 chroma transform.
142152 pub eob_pt_16_chroma: [u16; 6],
153+ /// The end-of-block group of a 4x4 luma transform (lane-av1tx4).
154+ pub eob_pt_16_luma: [u16; 6],
143155 /// The end-of-block offset bit of a 16x16 luma transform.
144156 pub eob_extra_luma_16: [[u16; 3]; 9],
145157 /// The same, for an 8x8 luma transform.
146158 pub eob_extra_luma_8: [[u16; 3]; 9],
159+ /// The same, for a 4x4 luma transform.
160+ pub eob_extra_luma_4: [[u16; 3]; 9],
147161 /// The same, for an 8x8 chroma transform.
148162 pub eob_extra_chroma_8: [[u16; 3]; 9],
149163 /// The same, for a 4x4 chroma transform.
@@ -152,6 +166,8 @@ pub(crate) struct Cdfs {
152166 pub base_luma_16: [[u16; 5]; 42],
153167 /// The base level of an 8x8 luma coefficient.
154168 pub base_luma_8: [[u16; 5]; 42],
169+ /// The base level of a 4x4 luma coefficient.
170+ pub base_luma_4: [[u16; 5]; 42],
155171 /// The base level of an 8x8 chroma coefficient.
156172 pub base_chroma_8: [[u16; 5]; 42],
157173 /// The base level of a 4x4 chroma coefficient.
@@ -160,6 +176,8 @@ pub(crate) struct Cdfs {
160176 pub base_eob_luma_16: [[u16; 4]; 4],
161177 /// The same, for an 8x8 luma transform.
162178 pub base_eob_luma_8: [[u16; 4]; 4],
179+ /// The same, for a 4x4 luma transform.
180+ pub base_eob_luma_4: [[u16; 4]; 4],
163181 /// The same, for an 8x8 chroma transform.
164182 pub base_eob_chroma_8: [[u16; 4]; 4],
165183 /// The same, for a 4x4 chroma transform.
@@ -168,6 +186,8 @@ pub(crate) struct Cdfs {
168186 pub br_luma_16: [[u16; 5]; 21],
169187 /// The base-range tail of an 8x8 luma coefficient.
170188 pub br_luma_8: [[u16; 5]; 21],
189+ /// The base-range tail of a 4x4 luma coefficient.
190+ pub br_luma_4: [[u16; 5]; 21],
171191 /// The base-range tail of an 8x8 chroma coefficient.
172192 pub br_chroma_8: [[u16; 5]; 21],
173193 /// The base-range tail of a 4x4 chroma coefficient.
@@ -236,6 +256,15 @@ pub(crate) struct Cdfs {
236256 /// `reduced_tx_set` off desyncs an 8x8 TU read against this row's
237257 /// wrong-length CDF).
238258 pub intra_tx_type_8_set1: [[u16; 8]; 13],
259+ /// The transform type of a 4x4 intra luma transform, by luma mode
260+ /// (lane-av1tx4): the flat five-symbol `reduced_tx_set` row, byte-
261+ /// identical to [`Self::intra_tx_type_8`]'s own table
262+ /// ([`cdf::INTRA_TX_TYPE_SET2_8`]) -- libaom leaves `TX_4X4` at the same
263+ /// uniform default.
264+ pub intra_tx_type_4: [[u16; 6]; 13],
265+ /// [`Self::intra_tx_type_4`]'s `reduced_tx_set: false` counterpart, same
266+ /// split as [`Self::intra_tx_type_8_set1`].
267+ pub intra_tx_type_4_set1: [[u16; 8]; 13],
239268 /// The transform type of an `is_inter` 32x32 luma transform (spec
240269 /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it
241270 /// is not indexed by mode.
@@ -392,26 +421,32 @@ impl Cdfs {
392421 reset2(&mut self.angle_delta);
393422 reset2(&mut self.txb_skip_luma_16);
394423 reset2(&mut self.txb_skip_luma_8);
424+ reset2(&mut self.txb_skip_luma_4);
395425 reset2(&mut self.txb_skip_chroma_8);
396426 reset2(&mut self.txb_skip_chroma_4);
397427 reset1(&mut self.eob_pt_256_luma);
398428 reset1(&mut self.eob_pt_64_luma);
399429 reset1(&mut self.eob_pt_64_chroma);
400430 reset1(&mut self.eob_pt_16_chroma);
431+ reset1(&mut self.eob_pt_16_luma);
401432 reset2(&mut self.eob_extra_luma_16);
402433 reset2(&mut self.eob_extra_luma_8);
434+ reset2(&mut self.eob_extra_luma_4);
403435 reset2(&mut self.eob_extra_chroma_8);
404436 reset2(&mut self.eob_extra_chroma_4);
405437 reset2(&mut self.base_luma_16);
406438 reset2(&mut self.base_luma_8);
439+ reset2(&mut self.base_luma_4);
407440 reset2(&mut self.base_chroma_8);
408441 reset2(&mut self.base_chroma_4);
409442 reset2(&mut self.base_eob_luma_16);
410443 reset2(&mut self.base_eob_luma_8);
444+ reset2(&mut self.base_eob_luma_4);
411445 reset2(&mut self.base_eob_chroma_8);
412446 reset2(&mut self.base_eob_chroma_4);
413447 reset2(&mut self.br_luma_16);
414448 reset2(&mut self.br_luma_8);
449+ reset2(&mut self.br_luma_4);
415450 reset2(&mut self.br_chroma_8);
416451 reset2(&mut self.br_chroma_4);
417452 reset2(&mut self.partition_w16);
@@ -442,6 +477,8 @@ impl Cdfs {
442477 reset2(&mut self.intra_tx_type_16);
443478 reset2(&mut self.intra_tx_type_8);
444479 reset2(&mut self.intra_tx_type_8_set1);
480+ reset2(&mut self.intra_tx_type_4);
481+ reset2(&mut self.intra_tx_type_4_set1);
445482 reset1(&mut self.inter_tx_type_32);
446483 reset1(&mut self.inter_tx_type_16);
447484 reset1(&mut self.inter_tx_type_8);
@@ -494,6 +531,13 @@ impl Cdfs {
494531 cdf::TXB_SKIP_LUMA_8_CTX,
495532 cdf::TXB_SKIP_LUMA_8_Q3_CTX,
496533 ),
534+ txb_skip_luma_4: pick(
535+ q_ctx,
536+ cdf::TXB_SKIP_LUMA_4_Q0_CTX,
537+ cdf::TXB_SKIP_LUMA_4_Q1_CTX,
538+ cdf::TXB_SKIP_LUMA_4_CTX,
539+ cdf::TXB_SKIP_LUMA_4_Q3_CTX,
540+ ),
497541 txb_skip_chroma_8: pick(
498542 q_ctx,
499543 cdf::TXB_SKIP_CHROMA_8_Q0,
@@ -536,6 +580,13 @@ impl Cdfs {
536580 cdf::EOB_PT_16_CHROMA,
537581 cdf::EOB_PT_16_CHROMA_Q3,
538582 ),
583+ eob_pt_16_luma: pick(
584+ q_ctx,
585+ cdf::EOB_PT_16_LUMA_Q0,
586+ cdf::EOB_PT_16_LUMA_Q1,
587+ cdf::EOB_PT_16_LUMA,
588+ cdf::EOB_PT_16_LUMA_Q3,
589+ ),
539590 eob_extra_luma_16: pick(
540591 q_ctx,
541592 cdf::EOB_EXTRA_LUMA_16_Q0,
@@ -550,6 +601,13 @@ impl Cdfs {
550601 cdf::EOB_EXTRA_LUMA_8,
551602 cdf::EOB_EXTRA_LUMA_8_Q3,
552603 ),
604+ eob_extra_luma_4: pick(
605+ q_ctx,
606+ cdf::EOB_EXTRA_LUMA_4_Q0,
607+ cdf::EOB_EXTRA_LUMA_4_Q1,
608+ cdf::EOB_EXTRA_LUMA_4,
609+ cdf::EOB_EXTRA_LUMA_4_Q3,
610+ ),
553611 eob_extra_chroma_8: pick(
554612 q_ctx,
555613 cdf::EOB_EXTRA_CHROMA_8_Q0,
@@ -578,6 +636,13 @@ impl Cdfs {
578636 cdf::COEFF_BASE_LUMA_8,
579637 cdf::COEFF_BASE_LUMA_8_Q3,
580638 ),
639+ base_luma_4: pick(
640+ q_ctx,
641+ cdf::COEFF_BASE_LUMA_4_Q0,
642+ cdf::COEFF_BASE_LUMA_4_Q1,
643+ cdf::COEFF_BASE_LUMA_4,
644+ cdf::COEFF_BASE_LUMA_4_Q3,
645+ ),
581646 base_chroma_8: pick(
582647 q_ctx,
583648 cdf::COEFF_BASE_CHROMA_8_Q0,
@@ -606,6 +671,13 @@ impl Cdfs {
606671 cdf::COEFF_BASE_EOB_LUMA_8,
607672 cdf::COEFF_BASE_EOB_LUMA_8_Q3,
608673 ),
674+ base_eob_luma_4: pick(
675+ q_ctx,
676+ cdf::COEFF_BASE_EOB_LUMA_4_Q0,
677+ cdf::COEFF_BASE_EOB_LUMA_4_Q1,
678+ cdf::COEFF_BASE_EOB_LUMA_4,
679+ cdf::COEFF_BASE_EOB_LUMA_4_Q3,
680+ ),
609681 base_eob_chroma_8: pick(
610682 q_ctx,
611683 cdf::COEFF_BASE_EOB_CHROMA_8_Q0,
@@ -634,6 +706,13 @@ impl Cdfs {
634706 cdf::COEFF_BR_LUMA_8,
635707 cdf::COEFF_BR_LUMA_8_Q3,
636708 ),
709+ br_luma_4: pick(
710+ q_ctx,
711+ cdf::COEFF_BR_LUMA_4_Q0,
712+ cdf::COEFF_BR_LUMA_4_Q1,
713+ cdf::COEFF_BR_LUMA_4,
714+ cdf::COEFF_BR_LUMA_4_Q3,
715+ ),
637716 br_chroma_8: pick(
638717 q_ctx,
639718 cdf::COEFF_BR_CHROMA_8_Q0,
@@ -808,6 +887,8 @@ impl Cdfs {
808887 intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16,
809888 intra_tx_type_8: cdf::INTRA_TX_TYPE_SET2_8,
810889 intra_tx_type_8_set1: cdf::INTRA_TX_TYPE_SET1_8,
890+ intra_tx_type_4: cdf::INTRA_TX_TYPE_SET2_8,
891+ intra_tx_type_4_set1: cdf::INTRA_TX_TYPE_SET1_4,
811892 inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32,
812893 inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16,
813894 inter_tx_type_8: cdf::INTER_TX_TYPE_SET3_8,
@@ -910,6 +991,28 @@ impl Cdfs {
910991 dc_sign: &mut self.dc_sign_luma,
911992 tx_type: Some(self.intra_tx_type_8_set1[mode].as_mut_slice()),
912993 },
994+ TxbSet::Luma4 => TxbTables {
995+ side: 4,
996+ txb_skip: &mut self.txb_skip_luma_4,
997+ eob_pt: &mut self.eob_pt_16_luma,
998+ eob_extra: &mut self.eob_extra_luma_4,
999+ base: &mut self.base_luma_4,
1000+ base_eob: &mut self.base_eob_luma_4,
1001+ br: &mut self.br_luma_4,
1002+ dc_sign: &mut self.dc_sign_luma,
1003+ tx_type: Some(self.intra_tx_type_4[mode].as_mut_slice()),
1004+ },
1005+ TxbSet::Luma4Set1 => TxbTables {
1006+ side: 4,
1007+ txb_skip: &mut self.txb_skip_luma_4,
1008+ eob_pt: &mut self.eob_pt_16_luma,
1009+ eob_extra: &mut self.eob_extra_luma_4,
1010+ base: &mut self.base_luma_4,
1011+ base_eob: &mut self.base_eob_luma_4,
1012+ br: &mut self.br_luma_4,
1013+ dc_sign: &mut self.dc_sign_luma,
1014+ tx_type: Some(self.intra_tx_type_4_set1[mode].as_mut_slice()),
1015+ },
9131016 TxbSet::Luma8Inter => TxbTables {
9141017 side: 8,
9151018 txb_skip: &mut self.txb_skip_luma_8,
crates/ec-av1/src/decode.rsmodified+255 -43
@@ -154,6 +154,26 @@ fn default_scan(side: usize) -> Vec<u16> {
154154 scan
155155 }
156156
157+/// `Mrow_Scan`/`Mcol_Scan` (spec 8.4.2, libaom `scan.c`): the two
158+/// class-specific scans a `V_DCT`/`H_DCT` transform block reads coefficients
159+/// in, instead of the default zigzag. libaom's own tables are keyed by a
160+/// *column-major* raster (`get_txb_bhl`'s `col = pos / height`) over its
161+/// physically column-major level buffer, but they decompose to the exact
162+/// same (row, col) walk as a flat row-major traversal of *our* row-major
163+/// `grid`/`levels` (`pos = row * side + col`) once you read them that way:
164+/// `Mrow_Scan` visits row 0 across every column, then row 1, ... — precisely
165+/// `0..side*side` in our own indexing — and `Mcol_Scan` is that walk
166+/// transposed (column 0 down every row, then column 1, ...).
167+fn class_scan_table(side: usize, class: TxClass) -> Vec<u16> {
168+ match class {
169+ TxClass::Vert => (0..(side * side) as u16).collect(),
170+ TxClass::Horiz => (0..side)
171+ .flat_map(|col| (0..side).map(move |row| (row * side + col) as u16))
172+ .collect(),
173+ TxClass::TwoD => unreachable!("class_scan is only for V_DCT/H_DCT"),
174+ }
175+}
176+
157177 fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize {
158178 match scan_idx {
159179 0 => 0,
@@ -171,31 +191,79 @@ fn neighbour(grid: &[i32], side: usize, row: usize, col: usize) -> i32 {
171191 }
172192 }
173193
174-fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
194+/// spec 5.11.39's three `TxClass`es (`tx_type_to_class`, libaom
195+/// `txb_common.h`): everything but the two lone-axis-identity types this
196+/// decoder reads (`V_DCT`/`H_DCT`) is `TX_CLASS_2D`.
197+#[derive(Clone, Copy, PartialEq, Eq)]
198+enum TxClass {
199+ TwoD,
200+ Horiz,
201+ Vert,
202+}
203+
204+impl TxClass {
205+ fn of(tx_type: TxType) -> Self {
206+ match tx_type {
207+ TxType::VDct => Self::Vert,
208+ TxType::HDct => Self::Horiz,
209+ _ => Self::TwoD,
210+ }
211+ }
212+}
213+
214+/// `nz_map_ctx_offset_1d`: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` share one 32-entry
215+/// table (indexed by the position along the transform's *coded* axis --
216+/// `col` for horiz, `row` for vert) that places their contexts past the 2D
217+/// table's own 26 rows (`SIG_COEF_CONTEXTS_2D`), 16 rows total
218+/// (`SIG_COEF_CONTEXTS_1D`) split 26/31/36.
219+fn nz_map_ctx_offset_1d(i: usize) -> usize {
220+ match i {
221+ 0 => 26,
222+ 1 => 31,
223+ _ => 36,
224+ }
225+}
226+
227+fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize {
175228 if row == 0 && col == 0 {
176229 return 0;
177230 }
178- let mag: i32 = [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)]
231+ let offsets: [(usize, usize); 5] = match class {
232+ TxClass::TwoD => [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)],
233+ TxClass::Horiz => [(1, 0), (0, 1), (0, 2), (0, 3), (0, 4)],
234+ TxClass::Vert => [(1, 0), (0, 1), (2, 0), (3, 0), (4, 0)],
235+ };
236+ let mag: i32 = offsets
179237 .iter()
180238 .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3))
181239 .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
240+ let ctx = ((mag + 1) >> 1).min(4) as usize;
241+ match class {
242+ TxClass::TwoD => ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize,
243+ TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)),
244+ TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)),
245+ }
184246 }
185247
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();
248+fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize {
249+ let extra = match class {
250+ TxClass::TwoD => neighbour(grid, side, row + 1, col + 1).abs(),
251+ TxClass::Horiz => neighbour(grid, side, row, col + 2).abs(),
252+ TxClass::Vert => neighbour(grid, side, row + 2, col).abs(),
253+ };
254+ let mag = neighbour(grid, side, row + 1, col).abs()
255+ + neighbour(grid, side, row, col + 1).abs()
256+ + extra;
191257 let mag = (((mag + 1) >> 1).min(6)) as usize;
192258 if row == 0 && col == 0 {
193- mag
194- } else if row < 2 && col < 2 {
195- mag + 7
196- } else {
197- mag + 14
259+ return mag;
198260 }
261+ let near_origin = match class {
262+ TxClass::TwoD => row < 2 && col < 2,
263+ TxClass::Horiz => col == 0,
264+ TxClass::Vert => row == 0,
265+ };
266+ if near_origin { mag + 7 } else { mag + 14 }
199267 }
200268
201269 fn dc_vote(dc: Option<bool>) -> i32 {
@@ -308,6 +376,14 @@ fn read_coeffs(
308376 if trace {
309377 eprintln!("TRACE eob value={eob}");
310378 }
379+ let class = TxClass::of(tx_type);
380+ let class_scan;
381+ let scan: &[u16] = if class == TxClass::TwoD {
382+ scan
383+ } else {
384+ class_scan = class_scan_table(side, class);
385+ &class_scan
386+ };
311387 let mut levels = vec![0i32; side * side];
312388 for scan_idx in (0..eob).rev() {
313389 let pos = scan[scan_idx] as usize;
@@ -322,7 +398,7 @@ fn read_coeffs(
322398 }
323399 v
324400 } else {
325- let ctx = base_ctx(&levels, side, row, col);
401+ let ctx = base_ctx(&levels, side, row, col, class);
326402 let v = dec.symbol(&mut coding.base[ctx]) as i32;
327403 if trace {
328404 eprintln!(
@@ -332,7 +408,7 @@ fn read_coeffs(
332408 v
333409 };
334410 let level = if level > NUM_BASE_LEVELS {
335- let ctx = br_ctx(&levels, side, row, col);
411+ let ctx = br_ctx(&levels, side, row, col, class);
336412 let mut level = level;
337413 let mut sent = 0;
338414 loop {
@@ -1171,6 +1247,7 @@ fn decode_block(
11711247 scan32: &[u16],
11721248 scan16: &[u16],
11731249 scan8: &[u16],
1250+ scan4: &[u16],
11741251 tx_select: bool,
11751252 reduced_tx_set: bool,
11761253 ) -> Result<()> {
@@ -1194,11 +1271,6 @@ fn decode_block(
11941271 // which only differs from it at that one corner case.
11951272 let (logical_tx, coeff_tx_side) = if tx_select {
11961273 let resolved = read_tx_size(dec, cdfs, neighbours, (mi_r, mi_c), side);
1197- if resolved == 4 {
1198- return Err(unsupported(
1199- "a resolved 4x4 luma transform (TxMode::Select depth exhausted; this decoder has no 4x4 luma coefficient tables)",
1200- ));
1201- }
12021274 (resolved, if resolved == 64 { 32 } else { resolved })
12031275 } else {
12041276 (side, luma_tx)
@@ -1208,7 +1280,7 @@ fn decode_block(
12081280 } else {
12091281 luma_set
12101282 };
1211- let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8);
1283+ let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8, scan4);
12121284 let reach = Reach::of(side, px, py, y.width, y.height);
12131285 let (cpx, cpy) = (px / 2, py / 2);
12141286 let chroma_side = side / 2;
@@ -1360,6 +1432,14 @@ fn decode_block(
13601432 filter_intra,
13611433 Some(tu_skip_ctx),
13621434 )?;
1435+ if std::env::var_os("EC_AV1_TRACE").is_some() {
1436+ eprintln!(
1437+ "TRACE tu_bitpos mi=({},{}) tell={}",
1438+ tu_mi.0,
1439+ tu_mi.1,
1440+ dec.debug_bitpos()
1441+ );
1442+ }
13631443 neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid);
13641444 }
13651445 }
@@ -1466,18 +1546,15 @@ fn decode_leaf8(
14661546 enable_filter_intra,
14671547 )?;
14681548 // `TxMode::Select`'s `tx_depth` at an 8x8 leaf (spec 5.11.16): the only
1469- // depths an 8x8 block offers are `TX8` (depth 0, this decoder's one
1470- // supported case) and `TX4` (depth 1, which this crate has no 4x4 luma
1471- // coefficient tables for) -- read to stay in sync, then refuse rather
1472- // than silently miscode.
1473- if tx_select {
1474- let resolved = read_tx_size(dec, cdfs, neighbours, leaf_mi, 8);
1475- if resolved == 4 {
1476- return Err(unsupported(
1477- "a resolved 4x4 luma transform (TxMode::Select depth exhausted; this decoder has no 4x4 luma coefficient tables)",
1478- ));
1479- }
1480- }
1549+ // depths an 8x8 block offers are `TX8` (depth 0) and `TX4` (depth 1,
1550+ // which splits the leaf's own 8x8 prediction into a 2x2 grid of 4x4
1551+ // transform units, same raster-order per-TU pattern `decode_block`'s
1552+ // multi-TU branch runs).
1553+ let resolved = if tx_select {
1554+ read_tx_size(dec, cdfs, neighbours, leaf_mi, 8)
1555+ } else {
1556+ 8
1557+ };
14811558 let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
14821559 let reach = Reach::of(8, px, py, y.width, y.height);
14831560 let (cpx, cpy) = (px / 2, py / 2);
@@ -1508,7 +1585,7 @@ fn decode_leaf8(
15081585 luma_grid = vec![0i32; 64];
15091586 u_grid = vec![0i32; 16];
15101587 v_grid = vec![0i32; 16];
1511- } else {
1588+ } else if resolved != 4 {
15121589 let around = neighbours.around_mi(leaf_mi, 8);
15131590 luma_grid = read_plane(
15141591 dec,
@@ -1575,6 +1652,129 @@ fn decode_leaf8(
15751652 None,
15761653 None,
15771654 )?;
1655+ } else {
1656+ // `tx_depth` resolved this leaf's luma to `TX4`: a 2x2 raster-order
1657+ // grid of 4x4 transform units, each predicted from the leaf's own
1658+ // 8x8 `mode` and reading its own fresh context off the earlier
1659+ // units in this same leaf (the same per-TU pattern `decode_block`'s
1660+ // multi-TU branch runs at bigger blocks).
1661+ luma_grid = vec![0i32; 64];
1662+ for tu_row in 0..2 {
1663+ for tu_col in 0..2 {
1664+ let tu_mi = (leaf_mi.0 + tu_row, leaf_mi.1 + tu_col);
1665+ let tu_px = px + tu_col * 4;
1666+ let tu_py = py + tu_row * 4;
1667+ let tu_around = neighbours.around_mi(tu_mi, 4)[0];
1668+ let tu_reach = Reach::of(4, tu_px, tu_py, y.width, y.height);
1669+ let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1);
1670+ let tu_grid = read_plane(
1671+ dec,
1672+ cdfs,
1673+ if reduced_tx_set {
1674+ TxbSet::Luma4
1675+ } else {
1676+ TxbSet::Luma4Set1
1677+ },
1678+ scans.1,
1679+ 0,
1680+ tu_around,
1681+ mode,
1682+ mode,
1683+ tu_reach,
1684+ y,
1685+ tu_px,
1686+ tu_py,
1687+ 4,
1688+ 4,
1689+ base_q_idx,
1690+ None,
1691+ filter_intra,
1692+ Some(tu_skip_ctx),
1693+ )?;
1694+ if std::env::var_os("EC_AV1_TRACE").is_some() {
1695+ eprintln!(
1696+ "TRACE tu_bitpos mi=({},{}) tell={}",
1697+ tu_mi.0,
1698+ tu_mi.1,
1699+ dec.debug_bitpos()
1700+ );
1701+ }
1702+ neighbours.record_mi_luma(tu_mi, 4, &tu_grid);
1703+ }
1704+ }
1705+ let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8));
1706+ let chroma_around = neighbours.around_mi(leaf_mi, 8);
1707+ u_grid = read_plane(
1708+ dec,
1709+ cdfs,
1710+ TxbSet::Chroma4,
1711+ scans.1,
1712+ 1,
1713+ chroma_around[1],
1714+ mode,
1715+ DC_PRED,
1716+ Reach::none(),
1717+ u,
1718+ cpx,
1719+ cpy,
1720+ 4,
1721+ TX4,
1722+ base_q_idx,
1723+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
1724+ None,
1725+ None,
1726+ )?;
1727+ v_grid = read_plane(
1728+ dec,
1729+ cdfs,
1730+ TxbSet::Chroma4,
1731+ scans.1,
1732+ 2,
1733+ chroma_around[2],
1734+ mode,
1735+ DC_PRED,
1736+ Reach::none(),
1737+ v,
1738+ cpx,
1739+ cpy,
1740+ 4,
1741+ TX4,
1742+ base_q_idx,
1743+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
1744+ None,
1745+ None,
1746+ )?;
1747+ // `record_split_luma` expects a `record`-style `(r, c)` position in
1748+ // `SUB`-grid units and rescales it by `SUB / MI`; `leaf_mi` is
1749+ // already in mi units, so calling it here double-scaled the
1750+ // position. Plane 0 is already correct per-TU from the
1751+ // `record_mi_luma` calls above; this inlines `record_mi`'s tail
1752+ // (spec `get_txb_ctx_general`'s neighbour-magnitude bookkeeping) for
1753+ // the two chroma planes only, at `leaf_mi` directly, side_mi=2 (this
1754+ // leaf's own 8x8 side over `MI`).
1755+ let side_mi = 2;
1756+ for cell in 0..side_mi {
1757+ neighbours.left_side_mi[leaf_mi.0 + cell] = 8;
1758+ neighbours.above_side_mi[leaf_mi.1 + cell] = 8;
1759+ }
1760+ let round_up_even = |n: usize| n.div_ceil(2) * 2;
1761+ let bound_h = round_up_even(neighbours.mi_rows);
1762+ let bound_w = round_up_even(neighbours.mi_cols);
1763+ let u_state = neighbour_state(&u_grid);
1764+ let v_state = neighbour_state(&v_grid);
1765+ for cell in 0..side_mi {
1766+ if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) {
1767+ neighbours.left[leaf_mi.0 + cell][1] = u_state;
1768+ neighbours.left[leaf_mi.0 + cell][2] = v_state;
1769+ }
1770+ if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) {
1771+ neighbours.above[leaf_mi.1 + cell][1] = u_state;
1772+ neighbours.above[leaf_mi.1 + cell][2] = v_state;
1773+ }
1774+ }
1775+ neighbours.fill_skip_grid(leaf_mi, 2, skip);
1776+ neighbours.fill_lf_grid(leaf_mi, 2, 4, false);
1777+ return Ok(mode);
15781778 }
15791779 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
15801780 neighbours.fill_skip_grid(leaf_mi, 2, skip);
@@ -1854,14 +2054,21 @@ fn read_tx_size(
18542054 side >> depth
18552055 }
18562056
1857-/// The luma coefficient scan table for a resolved transform side -- 32/16/8,
1858-/// the only three sizes a `TxMode::Select` block's coded coefficient grid
2057+/// The luma coefficient scan table for a resolved transform side -- 32/16/8/4,
2058+/// the only four sizes a `TxMode::Select` block's coded coefficient grid
18592059 /// ([`read_tx_size`]'s resolved side, or 32 at the 64x64 corner case) can be.
1860-fn scan_for<'a>(tx_px: usize, scan32: &'a [u16], scan16: &'a [u16], scan8: &'a [u16]) -> &'a [u16] {
2060+fn scan_for<'a>(
2061+ tx_px: usize,
2062+ scan32: &'a [u16],
2063+ scan16: &'a [u16],
2064+ scan8: &'a [u16],
2065+ scan4: &'a [u16],
2066+) -> &'a [u16] {
18612067 match tx_px {
18622068 32 => scan32,
18632069 16 => scan16,
18642070 8 => scan8,
2071+ 4 => scan4,
18652072 _ => unreachable!("read_tx_size never resolves the coefficient grid to anything else"),
18662073 }
18672074 }
@@ -1870,11 +2077,11 @@ fn scan_for<'a>(tx_px: usize, scan32: &'a [u16], scan16: &'a [u16], scan8: &'a [
18702077 /// from -- 64 is only ever the 64x64-block, depth-0, corner-scanned case
18712078 /// ([`TxbSet::Luma64`]); every other resolved side maps to its own matching
18722079 /// set. `get_tx_set` (spec 5.11.48) only splits on `reduced_tx_set` at
1873-/// `TX_8X8`: an intra `TX_16X16` always reads `TX_SET_INTRA_2` regardless of
1874-/// the flag (`av1_get_ext_tx_set_type`'s `tx_size_sqr == TX_16X16` branch
1875-/// ignores `use_reduced_set` once it is past the `TX_32X32`/reduced-set
1876-/// checks), so only the 8x8 case has a second table
1877-/// ([`TxbSet::Luma8Set1`]).
2080+/// `TX_8X8`/`TX_4X4`: an intra `TX_16X16` always reads `TX_SET_INTRA_2`
2081+/// regardless of the flag (`av1_get_ext_tx_set_type`'s
2082+/// `tx_size_sqr == TX_16X16` branch ignores `use_reduced_set` once it is past
2083+/// the `TX_32X32`/reduced-set checks), so only the 8x8 and 4x4 cases have a
2084+/// second table ([`TxbSet::Luma8Set1`]/[`TxbSet::Luma4Set1`]).
18782085 fn txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet {
18792086 match tx_px {
18802087 64 => TxbSet::Luma64,
@@ -1882,6 +2089,8 @@ fn txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet {
18822089 16 => TxbSet::Luma16,
18832090 8 if reduced_tx_set => TxbSet::Luma8,
18842091 8 => TxbSet::Luma8Set1,
2092+ 4 if reduced_tx_set => TxbSet::Luma4,
2093+ 4 => TxbSet::Luma4Set1,
18852094 _ => unreachable!("read_tx_size never resolves a block's own side to anything else"),
18862095 }
18872096 }
@@ -2575,6 +2784,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
25752784 &scan32,
25762785 &scan16,
25772786 &scan8,
2787+ &scan4,
25782788 tx_select,
25792789 reduced_tx_set,
25802790 )?;
@@ -2637,6 +2847,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
26372847 &scan32,
26382848 &scan16,
26392849 &scan8,
2850+ &scan4,
26402851 tx_select,
26412852 reduced_tx_set,
26422853 )?;
@@ -2685,6 +2896,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
26852896 &scan32,
26862897 &scan16,
26872898 &scan8,
2899+ &scan4,
26882900 tx_select,
26892901 reduced_tx_set,
26902902 )?;
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 {