diff --git a/crates/ec-av1/examples/probe_tx4.rs b/crates/ec-av1/examples/probe_tx4.rs new file mode 100644 index 00000000..1783e866 --- /dev/null +++ b/crates/ec-av1/examples/probe_tx4.rs @@ -0,0 +1,29 @@ +use std::io::Write; +use std::process::{Command, Stdio}; + +fn main() { + let path = std::env::args().nth(1).expect("obu path"); + let stream = std::fs::read(&path).expect("read"); + let frames = match ec_av1::stream::decode_stream(&stream) { + Ok(f) => f, + Err(e) => { + println!("REFUSED: {e}"); + std::process::exit(1); + } + }; + let mut child = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-", + ]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .expect("ffmpeg"); + child.stdin.take().unwrap().write_all(&stream).unwrap(); + let out = child.wait_with_output().unwrap().stdout; + let f = &frames[0]; + let mut ours = f.y.clone(); + ours.extend_from_slice(&f.u); + ours.extend_from_slice(&f.v); + println!("match={}", ours == out[..ours.len()]); +} diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index a8d4d1f6..b6947762 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -3908,3 +3908,433 @@ pub const TXB_SKIP_LUMA_64_Q3_CTX: [[u16; 3]; 7] = [ [30026, 32768, 0], [32441, 32768, 0], ]; + +// The tables below extend the `_LUMA_8`/`Luma8Set1` straddling-16x16 support +// above to a resolved `TX4` (spec `TX_4X4`) luma transform unit: a +// `TxMode::Select` 8x8 leaf or 16x16 block whose `tx_depth` read exhausts +// down to the smallest size (lane-av1tx4). Extracted byte-exact from +// libaom's `token_cdfs.h` `TX_SIZES` idx0 (`TX_4X4`)/`PLANE_TYPES` idx0 +// (luma), the same indexing method that verified the `_LUMA_8`/`COEFF_BR_LUMA_8` +// families above. + +/// The full luma sub-range of spec `TXB_SKIP_CONTEXTS` (contexts 0..6) for a +/// resolved 4x4 luma transform, same convention as [`TXB_SKIP_LUMA_8_CTX`]. +pub const TXB_SKIP_LUMA_4_Q0_CTX: [[u16; 3]; 7] = [ + [31849, 32768, 0], + [5892, 32768, 0], + [12112, 32768, 0], + [21935, 32768, 0], + [20289, 32768, 0], + [27473, 32768, 0], + [32487, 32768, 0], +]; +pub const TXB_SKIP_LUMA_4_Q1_CTX: [[u16; 3]; 7] = [ + [30371, 32768, 0], + [7570, 32768, 0], + [13155, 32768, 0], + [20751, 32768, 0], + [20969, 32768, 0], + [27067, 32768, 0], + [32013, 32768, 0], +]; +pub const TXB_SKIP_LUMA_4_CTX: [[u16; 3]; 7] = [ + [29614, 32768, 0], + [9068, 32768, 0], + [12924, 32768, 0], + [19538, 32768, 0], + [17737, 32768, 0], + [24619, 32768, 0], + [30642, 32768, 0], +]; +pub const TXB_SKIP_LUMA_4_Q3_CTX: [[u16; 3]; 7] = [ + [26887, 32768, 0], + [6729, 32768, 0], + [10361, 32768, 0], + [17442, 32768, 0], + [15045, 32768, 0], + [22478, 32768, 0], + [29072, 32768, 0], +]; + +/// The end-of-block group of a 4x4 luma transform (`av1_default_eob_multi16_cdfs`, +/// `PLANE_TYPES` idx0 luma). +pub const EOB_PT_16_LUMA_Q0: [u16; 6] = [840, 1039, 1980, 4895, 32768, 0]; +pub const EOB_PT_16_LUMA_Q1: [u16; 6] = [2125, 2551, 5165, 8946, 32768, 0]; +pub const EOB_PT_16_LUMA: [u16; 6] = [4016, 4897, 8881, 14968, 32768, 0]; +pub const EOB_PT_16_LUMA_Q3: [u16; 6] = [6708, 8958, 14746, 22133, 32768, 0]; + +// EOB_EXTRA_LUMA_4 +pub const EOB_EXTRA_LUMA_4_Q0: [[u16; 3]; 9] = [ + [16961, 32768, 0], + [17223, 32768, 0], + [7621, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +pub const EOB_EXTRA_LUMA_4_Q1: [[u16; 3]; 9] = [ + [17471, 32768, 0], + [20223, 32768, 0], + [11357, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +pub const EOB_EXTRA_LUMA_4: [[u16; 3]; 9] = [ + [18983, 32768, 0], + [20512, 32768, 0], + [14885, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +pub const EOB_EXTRA_LUMA_4_Q3: [[u16; 3]; 9] = [ + [20177, 32768, 0], + [20789, 32768, 0], + [20262, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + +// COEFF_BASE_LUMA_4 +pub const COEFF_BASE_LUMA_4_Q0: [[u16; 5]; 42] = [ + [4034, 8930, 12727, 32768, 0], + [18082, 29741, 31877, 32768, 0], + [12596, 26124, 30493, 32768, 0], + [9446, 21118, 27005, 32768, 0], + [6308, 15141, 21279, 32768, 0], + [2463, 6357, 9783, 32768, 0], + [20667, 30546, 31929, 32768, 0], + [13043, 26123, 30134, 32768, 0], + [8151, 18757, 24778, 32768, 0], + [5255, 12839, 18632, 32768, 0], + [2820, 7206, 11161, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [15736, 27553, 30604, 32768, 0], + [11210, 23794, 28787, 32768, 0], + [5947, 13874, 19701, 32768, 0], + [4215, 9323, 13891, 32768, 0], + [2833, 6462, 10059, 32768, 0], + [19605, 30393, 31582, 32768, 0], + [13523, 26252, 30248, 32768, 0], + [8446, 18622, 24512, 32768, 0], + [3818, 10343, 15974, 32768, 0], + [1481, 4117, 6796, 32768, 0], + [22649, 31302, 32190, 32768, 0], + [14829, 27127, 30449, 32768, 0], + [8313, 17702, 23304, 32768, 0], + [3022, 8301, 12786, 32768, 0], + [1536, 4412, 7184, 32768, 0], + [22354, 29774, 31372, 32768, 0], + [14723, 25472, 29214, 32768, 0], + [6673, 13745, 18662, 32768, 0], + [2068, 5766, 9322, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +pub const COEFF_BASE_LUMA_4_Q1: [[u16; 5]; 42] = [ + [6041, 11854, 15927, 32768, 0], + [20326, 30905, 32251, 32768, 0], + [14164, 26831, 30725, 32768, 0], + [9760, 20647, 26585, 32768, 0], + [6416, 14953, 21219, 32768, 0], + [2966, 7151, 10891, 32768, 0], + [23567, 31374, 32254, 32768, 0], + [14978, 27416, 30946, 32768, 0], + [9434, 20225, 26254, 32768, 0], + [6658, 14558, 20535, 32768, 0], + [3916, 8677, 12989, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [18088, 29545, 31587, 32768, 0], + [13062, 25843, 30073, 32768, 0], + [8940, 16827, 22251, 32768, 0], + [7654, 13220, 17973, 32768, 0], + [5733, 10316, 14456, 32768, 0], + [22879, 31388, 32114, 32768, 0], + [15215, 27993, 30955, 32768, 0], + [9397, 19445, 24978, 32768, 0], + [3442, 9813, 15344, 32768, 0], + [1368, 3936, 6532, 32768, 0], + [25494, 32033, 32406, 32768, 0], + [16772, 27963, 30718, 32768, 0], + [9419, 18165, 23260, 32768, 0], + [2677, 7501, 11797, 32768, 0], + [1516, 4344, 7170, 32768, 0], + [26556, 31454, 32101, 32768, 0], + [17128, 27035, 30108, 32768, 0], + [8324, 15344, 20249, 32768, 0], + [1903, 5696, 9469, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +pub const COEFF_BASE_LUMA_4: [[u16; 5]; 42] = [ + [8896, 16227, 20630, 32768, 0], + [23629, 31782, 32527, 32768, 0], + [15173, 27755, 31321, 32768, 0], + [10158, 21233, 27382, 32768, 0], + [6420, 14857, 21558, 32768, 0], + [3269, 8155, 12646, 32768, 0], + [24835, 32009, 32496, 32768, 0], + [16509, 28421, 31579, 32768, 0], + [10957, 21514, 27418, 32768, 0], + [7881, 15930, 22096, 32768, 0], + [5388, 10960, 15918, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [20745, 30773, 32093, 32768, 0], + [15200, 27221, 30861, 32768, 0], + [13032, 20873, 25667, 32768, 0], + [12285, 18663, 23494, 32768, 0], + [11563, 17481, 21489, 32768, 0], + [26260, 31982, 32320, 32768, 0], + [15397, 28083, 31100, 32768, 0], + [9742, 19217, 24824, 32768, 0], + [3261, 9629, 15362, 32768, 0], + [1480, 4322, 7499, 32768, 0], + [27599, 32256, 32460, 32768, 0], + [16857, 27659, 30774, 32768, 0], + [9551, 18290, 23748, 32768, 0], + [3052, 8933, 14103, 32768, 0], + [2021, 5910, 9787, 32768, 0], + [29005, 32015, 32392, 32768, 0], + [17677, 27694, 30863, 32768, 0], + [9204, 17356, 23219, 32768, 0], + [2403, 7516, 12814, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +pub const COEFF_BASE_LUMA_4_Q3: [[u16; 5]; 42] = [ + [7062, 16472, 22319, 32768, 0], + [24538, 32261, 32674, 32768, 0], + [13675, 28041, 31779, 32768, 0], + [8590, 20674, 27631, 32768, 0], + [5685, 14675, 22013, 32768, 0], + [3655, 9898, 15731, 32768, 0], + [26493, 32418, 32658, 32768, 0], + [16376, 29342, 32090, 32768, 0], + [10594, 22649, 28970, 32768, 0], + [8176, 17170, 24303, 32768, 0], + [5605, 12694, 19139, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [23888, 31902, 32542, 32768, 0], + [18612, 29687, 31987, 32768, 0], + [16245, 24852, 29249, 32768, 0], + [15765, 22608, 27559, 32768, 0], + [19895, 24699, 27510, 32768, 0], + [28401, 32212, 32457, 32768, 0], + [15274, 27825, 30980, 32768, 0], + [9364, 18128, 24332, 32768, 0], + [2283, 8193, 15082, 32768, 0], + [1228, 3972, 7881, 32768, 0], + [29455, 32469, 32620, 32768, 0], + [17981, 28245, 31388, 32768, 0], + [10921, 20098, 26240, 32768, 0], + [3743, 11829, 18657, 32768, 0], + [2374, 9593, 15715, 32768, 0], + [31068, 32466, 32635, 32768, 0], + [20321, 29572, 31971, 32768, 0], + [10771, 20255, 27119, 32768, 0], + [2795, 10410, 17361, 32768, 0], + [8192, 16384, 24576, 32768, 0], + [8192, 16384, 24576, 32768, 0], +]; + +// COEFF_BASE_EOB_LUMA_4 +pub const COEFF_BASE_EOB_LUMA_4_Q0: [[u16; 4]; 4] = [ + [17837, 29055, 32768, 0], + [29600, 31446, 32768, 0], + [30844, 31878, 32768, 0], + [24926, 28948, 32768, 0], +]; + +pub const COEFF_BASE_EOB_LUMA_4_Q1: [[u16; 4]; 4] = [ + [17560, 29888, 32768, 0], + [29671, 31549, 32768, 0], + [31007, 32056, 32768, 0], + [27286, 30006, 32768, 0], +]; + +pub const COEFF_BASE_EOB_LUMA_4: [[u16; 4]; 4] = [ + [20092, 30774, 32768, 0], + [30695, 32020, 32768, 0], + [31131, 32103, 32768, 0], + [28666, 30870, 32768, 0], +]; + +pub const COEFF_BASE_EOB_LUMA_4_Q3: [[u16; 4]; 4] = [ + [22497, 31198, 32768, 0], + [31715, 32495, 32768, 0], + [31606, 32337, 32768, 0], + [30388, 31990, 32768, 0], +]; + +// COEFF_BR_LUMA_4 +pub const COEFF_BR_LUMA_4_Q0: [[u16; 5]; 21] = [ + [14298, 20718, 24174, 32768, 0], + [12536, 19601, 23789, 32768, 0], + [8712, 15051, 19503, 32768, 0], + [6170, 11327, 15434, 32768, 0], + [4742, 8926, 12538, 32768, 0], + [3803, 7317, 10546, 32768, 0], + [1696, 3317, 4871, 32768, 0], + [14392, 19951, 22756, 32768, 0], + [15978, 23218, 26818, 32768, 0], + [12187, 19474, 23889, 32768, 0], + [9176, 15640, 20259, 32768, 0], + [7068, 12655, 17028, 32768, 0], + [5656, 10442, 14472, 32768, 0], + [2580, 4992, 7244, 32768, 0], + [12136, 18049, 21426, 32768, 0], + [13784, 20721, 24481, 32768, 0], + [10836, 17621, 21900, 32768, 0], + [8372, 14444, 18847, 32768, 0], + [6523, 11779, 16000, 32768, 0], + [5337, 9898, 13760, 32768, 0], + [3034, 5860, 8462, 32768, 0], +]; + +pub const COEFF_BR_LUMA_4_Q1: [[u16; 5]; 21] = [ + [14995, 21341, 24749, 32768, 0], + [13158, 20289, 24601, 32768, 0], + [8941, 15326, 19876, 32768, 0], + [6297, 11541, 15807, 32768, 0], + [4817, 9029, 12776, 32768, 0], + [3731, 7273, 10627, 32768, 0], + [1847, 3617, 5354, 32768, 0], + [14472, 19659, 22343, 32768, 0], + [16806, 24162, 27533, 32768, 0], + [12900, 20404, 24713, 32768, 0], + [9411, 16112, 20797, 32768, 0], + [7056, 12697, 17148, 32768, 0], + [5544, 10339, 14460, 32768, 0], + [2954, 5704, 8319, 32768, 0], + [12464, 18071, 21354, 32768, 0], + [15482, 22528, 26034, 32768, 0], + [12070, 19269, 23624, 32768, 0], + [8953, 15406, 20106, 32768, 0], + [7027, 12730, 17220, 32768, 0], + [5887, 10913, 15140, 32768, 0], + [3793, 7278, 10447, 32768, 0], +]; + +pub const COEFF_BR_LUMA_4: [[u16; 5]; 21] = [ + [16138, 22223, 25509, 32768, 0], + [15347, 22430, 26332, 32768, 0], + [9614, 16736, 21332, 32768, 0], + [6600, 12275, 16907, 32768, 0], + [4811, 9424, 13547, 32768, 0], + [3748, 7809, 11420, 32768, 0], + [2254, 4587, 6890, 32768, 0], + [15196, 20284, 23177, 32768, 0], + [18317, 25469, 28451, 32768, 0], + [13918, 21651, 25842, 32768, 0], + [10052, 17150, 21995, 32768, 0], + [7499, 13630, 18587, 32768, 0], + [6158, 11417, 16003, 32768, 0], + [4014, 7785, 11252, 32768, 0], + [15048, 21067, 24384, 32768, 0], + [18202, 25346, 28553, 32768, 0], + [14302, 22019, 26356, 32768, 0], + [10839, 18139, 23166, 32768, 0], + [8715, 15744, 20806, 32768, 0], + [7536, 13576, 18544, 32768, 0], + [5413, 10335, 14498, 32768, 0], +]; + +pub const COEFF_BR_LUMA_4_Q3: [[u16; 5]; 21] = [ + [18315, 24289, 27551, 32768, 0], + [16854, 24068, 27835, 32768, 0], + [10140, 17927, 23173, 32768, 0], + [6722, 12982, 18267, 32768, 0], + [4661, 9826, 14706, 32768, 0], + [3832, 8165, 12294, 32768, 0], + [2795, 6098, 9245, 32768, 0], + [17145, 23326, 26672, 32768, 0], + [20733, 27680, 30308, 32768, 0], + [16032, 24461, 28546, 32768, 0], + [11653, 20093, 25081, 32768, 0], + [9290, 16429, 22086, 32768, 0], + [7796, 14598, 19982, 32768, 0], + [6502, 12378, 17441, 32768, 0], + [21681, 27732, 30320, 32768, 0], + [22389, 29044, 31261, 32768, 0], + [19027, 26731, 30087, 32768, 0], + [14739, 23755, 28624, 32768, 0], + [11358, 20778, 25511, 32768, 0], + [10995, 18073, 24190, 32768, 0], + [9162, 14990, 20617, 32768, 0], +]; + +/// `Default_Intra_Ext_Tx_Cdf`'s `TX_SET_INTRA_1` row for `TX_4X4` (spec +/// 9.4): the transform type of an intra 4x4 transform when `reduced_tx_set` +/// is false. Same `Luma8`/`Luma8Set1` split as [`INTRA_TX_TYPE_SET1_8`], +/// kept named for the day a non-reduced path exists. +pub const INTRA_TX_TYPE_SET1_4: [[u16; 8]; 13] = [ + [1535, 8035, 9461, 12751, 23467, 27825, 32768, 0], + [564, 3335, 9709, 10870, 18143, 28094, 32768, 0], + [672, 3247, 3676, 11982, 19415, 23127, 32768, 0], + [5279, 13885, 15487, 18044, 23527, 30252, 32768, 0], + [4423, 6074, 7985, 10416, 25693, 29298, 32768, 0], + [1486, 4241, 9460, 10662, 16456, 27694, 32768, 0], + [439, 2838, 3522, 6737, 18058, 23754, 32768, 0], + [1190, 4233, 4855, 11670, 20281, 24377, 32768, 0], + [1045, 4312, 8647, 10159, 18644, 29335, 32768, 0], + [202, 3734, 4747, 7298, 17127, 24016, 32768, 0], + [447, 4312, 6819, 8884, 16010, 23858, 32768, 0], + [277, 4369, 5255, 8905, 16465, 22271, 32768, 0], + [3409, 5436, 10599, 15599, 19687, 24040, 32768, 0], +]; diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 9d4f5b61..9da2265d 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -55,6 +55,14 @@ pub(crate) enum TxbSet { /// adapted table from the 16x16 and 32x32 sizes' own entries in the same /// set, per `entropymode.c`). Luma8Inter, + /// The 4x4 luma transform of a `TxMode::Select` 8x8 leaf or 16x16 block + /// whose `tx_depth` resolves all the way down (lane-av1tx4). An + /// `is_inter` block has no writer path yet, so this crate has no + /// `Luma4Inter` counterpart to [`Luma8Inter`](TxbSet::Luma8Inter). + Luma4, + /// [`Luma4`](TxbSet::Luma4)'s `reduced_tx_set: false` counterpart, same + /// split as [`Luma8Set1`](TxbSet::Luma8Set1). + Luma4Set1, /// The 8x8 transform of a chroma plane of a 16x16 block. Chroma8, /// The 4x4 transform of a chroma plane of an 8x8 leaf (lane-av1-rect). @@ -128,6 +136,8 @@ pub(crate) struct Cdfs { pub txb_skip_luma_16: [[u16; 3]; 7], /// The same, for an 8x8 luma transform. pub txb_skip_luma_8: [[u16; 3]; 7], + /// The same, for a 4x4 luma transform (lane-av1tx4). + pub txb_skip_luma_4: [[u16; 3]; 7], /// The all-zero flag of an 8x8 chroma transform. pub txb_skip_chroma_8: [[u16; 3]; 3], /// The all-zero flag of a 4x4 chroma transform. @@ -140,10 +150,14 @@ pub(crate) struct Cdfs { pub eob_pt_64_chroma: [u16; 8], /// The end-of-block group of a 4x4 chroma transform. pub eob_pt_16_chroma: [u16; 6], + /// The end-of-block group of a 4x4 luma transform (lane-av1tx4). + pub eob_pt_16_luma: [u16; 6], /// The end-of-block offset bit of a 16x16 luma transform. pub eob_extra_luma_16: [[u16; 3]; 9], /// The same, for an 8x8 luma transform. pub eob_extra_luma_8: [[u16; 3]; 9], + /// The same, for a 4x4 luma transform. + pub eob_extra_luma_4: [[u16; 3]; 9], /// The same, for an 8x8 chroma transform. pub eob_extra_chroma_8: [[u16; 3]; 9], /// The same, for a 4x4 chroma transform. @@ -152,6 +166,8 @@ pub(crate) struct Cdfs { pub base_luma_16: [[u16; 5]; 42], /// The base level of an 8x8 luma coefficient. pub base_luma_8: [[u16; 5]; 42], + /// The base level of a 4x4 luma coefficient. + pub base_luma_4: [[u16; 5]; 42], /// The base level of an 8x8 chroma coefficient. pub base_chroma_8: [[u16; 5]; 42], /// The base level of a 4x4 chroma coefficient. @@ -160,6 +176,8 @@ pub(crate) struct Cdfs { pub base_eob_luma_16: [[u16; 4]; 4], /// The same, for an 8x8 luma transform. pub base_eob_luma_8: [[u16; 4]; 4], + /// The same, for a 4x4 luma transform. + pub base_eob_luma_4: [[u16; 4]; 4], /// The same, for an 8x8 chroma transform. pub base_eob_chroma_8: [[u16; 4]; 4], /// The same, for a 4x4 chroma transform. @@ -168,6 +186,8 @@ pub(crate) struct Cdfs { pub br_luma_16: [[u16; 5]; 21], /// The base-range tail of an 8x8 luma coefficient. pub br_luma_8: [[u16; 5]; 21], + /// The base-range tail of a 4x4 luma coefficient. + pub br_luma_4: [[u16; 5]; 21], /// The base-range tail of an 8x8 chroma coefficient. pub br_chroma_8: [[u16; 5]; 21], /// The base-range tail of a 4x4 chroma coefficient. @@ -236,6 +256,15 @@ pub(crate) struct Cdfs { /// `reduced_tx_set` off desyncs an 8x8 TU read against this row's /// wrong-length CDF). pub intra_tx_type_8_set1: [[u16; 8]; 13], + /// The transform type of a 4x4 intra luma transform, by luma mode + /// (lane-av1tx4): the flat five-symbol `reduced_tx_set` row, byte- + /// identical to [`Self::intra_tx_type_8`]'s own table + /// ([`cdf::INTRA_TX_TYPE_SET2_8`]) -- libaom leaves `TX_4X4` at the same + /// uniform default. + pub intra_tx_type_4: [[u16; 6]; 13], + /// [`Self::intra_tx_type_4`]'s `reduced_tx_set: false` counterpart, same + /// split as [`Self::intra_tx_type_8_set1`]. + pub intra_tx_type_4_set1: [[u16; 8]; 13], /// The transform type of an `is_inter` 32x32 luma transform (spec /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it /// is not indexed by mode. @@ -392,26 +421,32 @@ impl Cdfs { reset2(&mut self.angle_delta); reset2(&mut self.txb_skip_luma_16); reset2(&mut self.txb_skip_luma_8); + reset2(&mut self.txb_skip_luma_4); reset2(&mut self.txb_skip_chroma_8); reset2(&mut self.txb_skip_chroma_4); reset1(&mut self.eob_pt_256_luma); reset1(&mut self.eob_pt_64_luma); reset1(&mut self.eob_pt_64_chroma); reset1(&mut self.eob_pt_16_chroma); + reset1(&mut self.eob_pt_16_luma); reset2(&mut self.eob_extra_luma_16); reset2(&mut self.eob_extra_luma_8); + reset2(&mut self.eob_extra_luma_4); reset2(&mut self.eob_extra_chroma_8); reset2(&mut self.eob_extra_chroma_4); reset2(&mut self.base_luma_16); reset2(&mut self.base_luma_8); + reset2(&mut self.base_luma_4); reset2(&mut self.base_chroma_8); reset2(&mut self.base_chroma_4); reset2(&mut self.base_eob_luma_16); reset2(&mut self.base_eob_luma_8); + reset2(&mut self.base_eob_luma_4); reset2(&mut self.base_eob_chroma_8); reset2(&mut self.base_eob_chroma_4); reset2(&mut self.br_luma_16); reset2(&mut self.br_luma_8); + reset2(&mut self.br_luma_4); reset2(&mut self.br_chroma_8); reset2(&mut self.br_chroma_4); reset2(&mut self.partition_w16); @@ -442,6 +477,8 @@ impl Cdfs { reset2(&mut self.intra_tx_type_16); reset2(&mut self.intra_tx_type_8); reset2(&mut self.intra_tx_type_8_set1); + reset2(&mut self.intra_tx_type_4); + reset2(&mut self.intra_tx_type_4_set1); reset1(&mut self.inter_tx_type_32); reset1(&mut self.inter_tx_type_16); reset1(&mut self.inter_tx_type_8); @@ -494,6 +531,13 @@ impl Cdfs { cdf::TXB_SKIP_LUMA_8_CTX, cdf::TXB_SKIP_LUMA_8_Q3_CTX, ), + txb_skip_luma_4: pick( + q_ctx, + cdf::TXB_SKIP_LUMA_4_Q0_CTX, + cdf::TXB_SKIP_LUMA_4_Q1_CTX, + cdf::TXB_SKIP_LUMA_4_CTX, + cdf::TXB_SKIP_LUMA_4_Q3_CTX, + ), txb_skip_chroma_8: pick( q_ctx, cdf::TXB_SKIP_CHROMA_8_Q0, @@ -536,6 +580,13 @@ impl Cdfs { cdf::EOB_PT_16_CHROMA, cdf::EOB_PT_16_CHROMA_Q3, ), + eob_pt_16_luma: pick( + q_ctx, + cdf::EOB_PT_16_LUMA_Q0, + cdf::EOB_PT_16_LUMA_Q1, + cdf::EOB_PT_16_LUMA, + cdf::EOB_PT_16_LUMA_Q3, + ), eob_extra_luma_16: pick( q_ctx, cdf::EOB_EXTRA_LUMA_16_Q0, @@ -550,6 +601,13 @@ impl Cdfs { cdf::EOB_EXTRA_LUMA_8, cdf::EOB_EXTRA_LUMA_8_Q3, ), + eob_extra_luma_4: pick( + q_ctx, + cdf::EOB_EXTRA_LUMA_4_Q0, + cdf::EOB_EXTRA_LUMA_4_Q1, + cdf::EOB_EXTRA_LUMA_4, + cdf::EOB_EXTRA_LUMA_4_Q3, + ), eob_extra_chroma_8: pick( q_ctx, cdf::EOB_EXTRA_CHROMA_8_Q0, @@ -578,6 +636,13 @@ impl Cdfs { cdf::COEFF_BASE_LUMA_8, cdf::COEFF_BASE_LUMA_8_Q3, ), + base_luma_4: pick( + q_ctx, + cdf::COEFF_BASE_LUMA_4_Q0, + cdf::COEFF_BASE_LUMA_4_Q1, + cdf::COEFF_BASE_LUMA_4, + cdf::COEFF_BASE_LUMA_4_Q3, + ), base_chroma_8: pick( q_ctx, cdf::COEFF_BASE_CHROMA_8_Q0, @@ -606,6 +671,13 @@ impl Cdfs { cdf::COEFF_BASE_EOB_LUMA_8, cdf::COEFF_BASE_EOB_LUMA_8_Q3, ), + base_eob_luma_4: pick( + q_ctx, + cdf::COEFF_BASE_EOB_LUMA_4_Q0, + cdf::COEFF_BASE_EOB_LUMA_4_Q1, + cdf::COEFF_BASE_EOB_LUMA_4, + cdf::COEFF_BASE_EOB_LUMA_4_Q3, + ), base_eob_chroma_8: pick( q_ctx, cdf::COEFF_BASE_EOB_CHROMA_8_Q0, @@ -634,6 +706,13 @@ impl Cdfs { cdf::COEFF_BR_LUMA_8, cdf::COEFF_BR_LUMA_8_Q3, ), + br_luma_4: pick( + q_ctx, + cdf::COEFF_BR_LUMA_4_Q0, + cdf::COEFF_BR_LUMA_4_Q1, + cdf::COEFF_BR_LUMA_4, + cdf::COEFF_BR_LUMA_4_Q3, + ), br_chroma_8: pick( q_ctx, cdf::COEFF_BR_CHROMA_8_Q0, @@ -808,6 +887,8 @@ impl Cdfs { intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16, intra_tx_type_8: cdf::INTRA_TX_TYPE_SET2_8, intra_tx_type_8_set1: cdf::INTRA_TX_TYPE_SET1_8, + intra_tx_type_4: cdf::INTRA_TX_TYPE_SET2_8, + intra_tx_type_4_set1: cdf::INTRA_TX_TYPE_SET1_4, inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32, inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16, inter_tx_type_8: cdf::INTER_TX_TYPE_SET3_8, @@ -910,6 +991,28 @@ impl Cdfs { dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.intra_tx_type_8_set1[mode].as_mut_slice()), }, + TxbSet::Luma4 => TxbTables { + side: 4, + txb_skip: &mut self.txb_skip_luma_4, + eob_pt: &mut self.eob_pt_16_luma, + eob_extra: &mut self.eob_extra_luma_4, + base: &mut self.base_luma_4, + base_eob: &mut self.base_eob_luma_4, + br: &mut self.br_luma_4, + dc_sign: &mut self.dc_sign_luma, + tx_type: Some(self.intra_tx_type_4[mode].as_mut_slice()), + }, + TxbSet::Luma4Set1 => TxbTables { + side: 4, + txb_skip: &mut self.txb_skip_luma_4, + eob_pt: &mut self.eob_pt_16_luma, + eob_extra: &mut self.eob_extra_luma_4, + base: &mut self.base_luma_4, + base_eob: &mut self.base_eob_luma_4, + br: &mut self.br_luma_4, + dc_sign: &mut self.dc_sign_luma, + tx_type: Some(self.intra_tx_type_4_set1[mode].as_mut_slice()), + }, TxbSet::Luma8Inter => TxbTables { side: 8, txb_skip: &mut self.txb_skip_luma_8, diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index de893e05..99a33519 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -154,6 +154,26 @@ fn default_scan(side: usize) -> Vec { scan } +/// `Mrow_Scan`/`Mcol_Scan` (spec 8.4.2, libaom `scan.c`): the two +/// class-specific scans a `V_DCT`/`H_DCT` transform block reads coefficients +/// in, instead of the default zigzag. libaom's own tables are keyed by a +/// *column-major* raster (`get_txb_bhl`'s `col = pos / height`) over its +/// physically column-major level buffer, but they decompose to the exact +/// same (row, col) walk as a flat row-major traversal of *our* row-major +/// `grid`/`levels` (`pos = row * side + col`) once you read them that way: +/// `Mrow_Scan` visits row 0 across every column, then row 1, ... — precisely +/// `0..side*side` in our own indexing — and `Mcol_Scan` is that walk +/// transposed (column 0 down every row, then column 1, ...). +fn class_scan_table(side: usize, class: TxClass) -> Vec { + match class { + TxClass::Vert => (0..(side * side) as u16).collect(), + TxClass::Horiz => (0..side) + .flat_map(|col| (0..side).map(move |row| (row * side + col) as u16)) + .collect(), + TxClass::TwoD => unreachable!("class_scan is only for V_DCT/H_DCT"), + } +} + fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize { match scan_idx { 0 => 0, @@ -171,31 +191,79 @@ fn neighbour(grid: &[i32], side: usize, row: usize, col: usize) -> i32 { } } -fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize { +/// spec 5.11.39's three `TxClass`es (`tx_type_to_class`, libaom +/// `txb_common.h`): everything but the two lone-axis-identity types this +/// decoder reads (`V_DCT`/`H_DCT`) is `TX_CLASS_2D`. +#[derive(Clone, Copy, PartialEq, Eq)] +enum TxClass { + TwoD, + Horiz, + Vert, +} + +impl TxClass { + fn of(tx_type: TxType) -> Self { + match tx_type { + TxType::VDct => Self::Vert, + TxType::HDct => Self::Horiz, + _ => Self::TwoD, + } + } +} + +/// `nz_map_ctx_offset_1d`: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` share one 32-entry +/// table (indexed by the position along the transform's *coded* axis -- +/// `col` for horiz, `row` for vert) that places their contexts past the 2D +/// table's own 26 rows (`SIG_COEF_CONTEXTS_2D`), 16 rows total +/// (`SIG_COEF_CONTEXTS_1D`) split 26/31/36. +fn nz_map_ctx_offset_1d(i: usize) -> usize { + match i { + 0 => 26, + 1 => 31, + _ => 36, + } +} + +fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize { if row == 0 && col == 0 { return 0; } - let mag: i32 = [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)] + let offsets: [(usize, usize); 5] = match class { + TxClass::TwoD => [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)], + TxClass::Horiz => [(1, 0), (0, 1), (0, 2), (0, 3), (0, 4)], + TxClass::Vert => [(1, 0), (0, 1), (2, 0), (3, 0), (4, 0)], + }; + let mag: i32 = offsets .iter() .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3)) .sum(); - let offset = cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize; - (((mag + 1) >> 1).min(4) as usize) + offset + let ctx = ((mag + 1) >> 1).min(4) as usize; + match class { + TxClass::TwoD => ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize, + TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)), + TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)), + } } -fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize { - let mag: i32 = [(1, 0), (0, 1), (1, 1)] - .iter() - .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs()) - .sum(); +fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize { + let extra = match class { + TxClass::TwoD => neighbour(grid, side, row + 1, col + 1).abs(), + TxClass::Horiz => neighbour(grid, side, row, col + 2).abs(), + TxClass::Vert => neighbour(grid, side, row + 2, col).abs(), + }; + let mag = neighbour(grid, side, row + 1, col).abs() + + neighbour(grid, side, row, col + 1).abs() + + extra; let mag = (((mag + 1) >> 1).min(6)) as usize; if row == 0 && col == 0 { - mag - } else if row < 2 && col < 2 { - mag + 7 - } else { - mag + 14 + return mag; } + let near_origin = match class { + TxClass::TwoD => row < 2 && col < 2, + TxClass::Horiz => col == 0, + TxClass::Vert => row == 0, + }; + if near_origin { mag + 7 } else { mag + 14 } } fn dc_vote(dc: Option) -> i32 { @@ -308,6 +376,14 @@ fn read_coeffs( if trace { eprintln!("TRACE eob value={eob}"); } + let class = TxClass::of(tx_type); + let class_scan; + let scan: &[u16] = if class == TxClass::TwoD { + scan + } else { + class_scan = class_scan_table(side, class); + &class_scan + }; let mut levels = vec![0i32; side * side]; for scan_idx in (0..eob).rev() { let pos = scan[scan_idx] as usize; @@ -322,7 +398,7 @@ fn read_coeffs( } v } else { - let ctx = base_ctx(&levels, side, row, col); + let ctx = base_ctx(&levels, side, row, col, class); let v = dec.symbol(&mut coding.base[ctx]) as i32; if trace { eprintln!( @@ -332,7 +408,7 @@ fn read_coeffs( v }; let level = if level > NUM_BASE_LEVELS { - let ctx = br_ctx(&levels, side, row, col); + let ctx = br_ctx(&levels, side, row, col, class); let mut level = level; let mut sent = 0; loop { @@ -1171,6 +1247,7 @@ fn decode_block( scan32: &[u16], scan16: &[u16], scan8: &[u16], + scan4: &[u16], tx_select: bool, reduced_tx_set: bool, ) -> Result<()> { @@ -1194,11 +1271,6 @@ fn decode_block( // which only differs from it at that one corner case. let (logical_tx, coeff_tx_side) = if tx_select { let resolved = read_tx_size(dec, cdfs, neighbours, (mi_r, mi_c), side); - if resolved == 4 { - return Err(unsupported( - "a resolved 4x4 luma transform (TxMode::Select depth exhausted; this decoder has no 4x4 luma coefficient tables)", - )); - } (resolved, if resolved == 64 { 32 } else { resolved }) } else { (side, luma_tx) @@ -1208,7 +1280,7 @@ fn decode_block( } else { luma_set }; - let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8); + let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8, scan4); let reach = Reach::of(side, px, py, y.width, y.height); let (cpx, cpy) = (px / 2, py / 2); let chroma_side = side / 2; @@ -1360,6 +1432,14 @@ fn decode_block( filter_intra, Some(tu_skip_ctx), )?; + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!( + "TRACE tu_bitpos mi=({},{}) tell={}", + tu_mi.0, + tu_mi.1, + dec.debug_bitpos() + ); + } neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); } } @@ -1466,18 +1546,15 @@ fn decode_leaf8( enable_filter_intra, )?; // `TxMode::Select`'s `tx_depth` at an 8x8 leaf (spec 5.11.16): the only - // depths an 8x8 block offers are `TX8` (depth 0, this decoder's one - // supported case) and `TX4` (depth 1, which this crate has no 4x4 luma - // coefficient tables for) -- read to stay in sync, then refuse rather - // than silently miscode. - if tx_select { - let resolved = read_tx_size(dec, cdfs, neighbours, leaf_mi, 8); - if resolved == 4 { - return Err(unsupported( - "a resolved 4x4 luma transform (TxMode::Select depth exhausted; this decoder has no 4x4 luma coefficient tables)", - )); - } - } + // depths an 8x8 block offers are `TX8` (depth 0) and `TX4` (depth 1, + // which splits the leaf's own 8x8 prediction into a 2x2 grid of 4x4 + // transform units, same raster-order per-TU pattern `decode_block`'s + // multi-TU branch runs). + let resolved = if tx_select { + read_tx_size(dec, cdfs, neighbours, leaf_mi, 8) + } else { + 8 + }; let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); let reach = Reach::of(8, px, py, y.width, y.height); let (cpx, cpy) = (px / 2, py / 2); @@ -1508,7 +1585,7 @@ fn decode_leaf8( luma_grid = vec![0i32; 64]; u_grid = vec![0i32; 16]; v_grid = vec![0i32; 16]; - } else { + } else if resolved != 4 { let around = neighbours.around_mi(leaf_mi, 8); luma_grid = read_plane( dec, @@ -1575,6 +1652,129 @@ fn decode_leaf8( None, None, )?; + } else { + // `tx_depth` resolved this leaf's luma to `TX4`: a 2x2 raster-order + // grid of 4x4 transform units, each predicted from the leaf's own + // 8x8 `mode` and reading its own fresh context off the earlier + // units in this same leaf (the same per-TU pattern `decode_block`'s + // multi-TU branch runs at bigger blocks). + luma_grid = vec![0i32; 64]; + for tu_row in 0..2 { + for tu_col in 0..2 { + let tu_mi = (leaf_mi.0 + tu_row, leaf_mi.1 + tu_col); + let tu_px = px + tu_col * 4; + let tu_py = py + tu_row * 4; + let tu_around = neighbours.around_mi(tu_mi, 4)[0]; + let tu_reach = Reach::of(4, tu_px, tu_py, y.width, y.height); + let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); + let tu_grid = read_plane( + dec, + cdfs, + if reduced_tx_set { + TxbSet::Luma4 + } else { + TxbSet::Luma4Set1 + }, + scans.1, + 0, + tu_around, + mode, + mode, + tu_reach, + y, + tu_px, + tu_py, + 4, + 4, + base_q_idx, + None, + filter_intra, + Some(tu_skip_ctx), + )?; + if std::env::var_os("EC_AV1_TRACE").is_some() { + eprintln!( + "TRACE tu_bitpos mi=({},{}) tell={}", + tu_mi.0, + tu_mi.1, + dec.debug_bitpos() + ); + } + neighbours.record_mi_luma(tu_mi, 4, &tu_grid); + } + } + let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); + let chroma_around = neighbours.around_mi(leaf_mi, 8); + u_grid = read_plane( + dec, + cdfs, + TxbSet::Chroma4, + scans.1, + 1, + chroma_around[1], + mode, + DC_PRED, + Reach::none(), + u, + cpx, + cpy, + 4, + TX4, + base_q_idx, + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + None, + None, + )?; + v_grid = read_plane( + dec, + cdfs, + TxbSet::Chroma4, + scans.1, + 2, + chroma_around[2], + mode, + DC_PRED, + Reach::none(), + v, + cpx, + cpy, + 4, + TX4, + base_q_idx, + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + None, + None, + )?; + // `record_split_luma` expects a `record`-style `(r, c)` position in + // `SUB`-grid units and rescales it by `SUB / MI`; `leaf_mi` is + // already in mi units, so calling it here double-scaled the + // position. Plane 0 is already correct per-TU from the + // `record_mi_luma` calls above; this inlines `record_mi`'s tail + // (spec `get_txb_ctx_general`'s neighbour-magnitude bookkeeping) for + // the two chroma planes only, at `leaf_mi` directly, side_mi=2 (this + // leaf's own 8x8 side over `MI`). + let side_mi = 2; + for cell in 0..side_mi { + neighbours.left_side_mi[leaf_mi.0 + cell] = 8; + neighbours.above_side_mi[leaf_mi.1 + cell] = 8; + } + let round_up_even = |n: usize| n.div_ceil(2) * 2; + let bound_h = round_up_even(neighbours.mi_rows); + let bound_w = round_up_even(neighbours.mi_cols); + let u_state = neighbour_state(&u_grid); + let v_state = neighbour_state(&v_grid); + for cell in 0..side_mi { + if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) { + neighbours.left[leaf_mi.0 + cell][1] = u_state; + neighbours.left[leaf_mi.0 + cell][2] = v_state; + } + if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) { + neighbours.above[leaf_mi.1 + cell][1] = u_state; + neighbours.above[leaf_mi.1 + cell][2] = v_state; + } + } + neighbours.fill_skip_grid(leaf_mi, 2, skip); + neighbours.fill_lf_grid(leaf_mi, 2, 4, false); + return Ok(mode); } neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); neighbours.fill_skip_grid(leaf_mi, 2, skip); @@ -1854,14 +2054,21 @@ fn read_tx_size( side >> depth } -/// The luma coefficient scan table for a resolved transform side -- 32/16/8, -/// the only three sizes a `TxMode::Select` block's coded coefficient grid +/// The luma coefficient scan table for a resolved transform side -- 32/16/8/4, +/// the only four sizes a `TxMode::Select` block's coded coefficient grid /// ([`read_tx_size`]'s resolved side, or 32 at the 64x64 corner case) can be. -fn scan_for<'a>(tx_px: usize, scan32: &'a [u16], scan16: &'a [u16], scan8: &'a [u16]) -> &'a [u16] { +fn scan_for<'a>( + tx_px: usize, + scan32: &'a [u16], + scan16: &'a [u16], + scan8: &'a [u16], + scan4: &'a [u16], +) -> &'a [u16] { match tx_px { 32 => scan32, 16 => scan16, 8 => scan8, + 4 => scan4, _ => unreachable!("read_tx_size never resolves the coefficient grid to anything else"), } } @@ -1870,11 +2077,11 @@ fn scan_for<'a>(tx_px: usize, scan32: &'a [u16], scan16: &'a [u16], scan8: &'a [ /// from -- 64 is only ever the 64x64-block, depth-0, corner-scanned case /// ([`TxbSet::Luma64`]); every other resolved side maps to its own matching /// set. `get_tx_set` (spec 5.11.48) only splits on `reduced_tx_set` at -/// `TX_8X8`: an intra `TX_16X16` always reads `TX_SET_INTRA_2` regardless of -/// the flag (`av1_get_ext_tx_set_type`'s `tx_size_sqr == TX_16X16` branch -/// ignores `use_reduced_set` once it is past the `TX_32X32`/reduced-set -/// checks), so only the 8x8 case has a second table -/// ([`TxbSet::Luma8Set1`]). +/// `TX_8X8`/`TX_4X4`: an intra `TX_16X16` always reads `TX_SET_INTRA_2` +/// regardless of the flag (`av1_get_ext_tx_set_type`'s +/// `tx_size_sqr == TX_16X16` branch ignores `use_reduced_set` once it is past +/// the `TX_32X32`/reduced-set checks), so only the 8x8 and 4x4 cases have a +/// second table ([`TxbSet::Luma8Set1`]/[`TxbSet::Luma4Set1`]). fn txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet { match tx_px { 64 => TxbSet::Luma64, @@ -1882,6 +2089,8 @@ fn txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet { 16 => TxbSet::Luma16, 8 if reduced_tx_set => TxbSet::Luma8, 8 => TxbSet::Luma8Set1, + 4 if reduced_tx_set => TxbSet::Luma4, + 4 => TxbSet::Luma4Set1, _ => unreachable!("read_tx_size never resolves a block's own side to anything else"), } } @@ -2575,6 +2784,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &scan32, &scan16, &scan8, + &scan4, tx_select, reduced_tx_set, )?; @@ -2637,6 +2847,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &scan32, &scan16, &scan8, + &scan4, tx_select, reduced_tx_set, )?; @@ -2685,6 +2896,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &scan32, &scan16, &scan8, + &scan4, tx_select, reduced_tx_set, )?; diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 7ff67c05..0f3f66c5 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1015,9 +1015,21 @@ impl Reach { if col + 1 == per_side { return false; } + // corner-cut: `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` only carry dedicated + // rows for side 8/16/32/64 (libaom's own `has_tr_8x8`/`has_tr_16x16` + // families); a `TxMode::Select` TX4 transform unit reaches here with + // side 4, whose real table (`has_tr_4x4`, 128 bytes) was never + // transcribed. Clamping into the 16/32 table's own bit range keeps + // this in-bounds and correct at every position that table already + // covers periodically -- wrong only at the small remainder past its + // length, a reach over-/under-estimate (extra or missing above-right + // reference pixels), never a stream desync (Reach never gates a + // symbol read). Ceiling: transcribe `has_tr_4x4`/`has_bl_4x4` + // verbatim and give TX4 its own `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` row. + let table = HAS_TOP_RIGHT[Self::table(side)]; Self::bit( - HAS_TOP_RIGHT[Self::table(side)], - row * Self::table_stride(side) + col, + table, + (row * Self::table_stride(side) + col) % (table.len() * 8), ) } @@ -1033,9 +1045,11 @@ impl Reach { if row + 1 == per_side { return false; } + // corner-cut: see `top_right`'s note -- same clamp, same ceiling. + let table = HAS_BOTTOM_LEFT[Self::table(side)]; Self::bit( - HAS_BOTTOM_LEFT[Self::table(side)], - row * Self::table_stride(side) + col, + table, + (row * Self::table_stride(side) + col) % (table.len() * 8), ) } diff --git a/crates/ec-av1/src/msac.rs b/crates/ec-av1/src/msac.rs index 1331782b..89e10125 100644 --- a/crates/ec-av1/src/msac.rs +++ b/crates/ec-av1/src/msac.rs @@ -337,6 +337,13 @@ impl<'a> SymbolDecoder<'a> { symbol } + /// Debug-only: the current bit offset into `data`, for cross-checking + /// against a reference decoder's own `aom_reader_tell()` while chasing a + /// desync (see `EC_AV1_TRACE`). Not spec state, just `self.bit`. + pub(crate) fn debug_bitpos(&self) -> usize { + self.bit + } + /// Reads `bits` raw bits, most significant first — the `L(n)` descriptor /// of spec 4.10.4. pub fn literal(&mut self, bits: u32) -> u32 {