forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1txsel': TxMode::Select intra decode, proven firing

Browse files at this commit

0c923126f3a1350a9cac0916bacd1139063a178eAhmet Kaan GümüşParent: ce44f722411791
Nine rounds: tx_depth read (spec 5.11.16) with per-TU luma loops, the
7-context txb_skip rewire, the seven-type TX_SET_INTRA_1 tx_type row
with V_DCT/H_DCT, and a gate that provably splits transforms on real
aomenc bytes and decodes pixel-exact. Select stays refused on inter
frames; resolved 4x4 luma is a named refusal.
crates/ec-av1/src/cdf.rsmodified+175 -0
@@ -326,6 +326,31 @@ pub const FILTER_INTRA: [[u16; 3]; 4] = [
326326 /// `FILTER_INTRA_MODES` a `use_filter_intra` block picks.
327327 pub const FILTER_INTRA_MODE: [u16; 6] = [8949, 12776, 17211, 29558, 32768, 0];
328328
329+/// `default_tx_size_cdf[0]` (entropymode.c): an 8x8 block's `tx_depth` flag
330+/// (`TX8` vs `TX4`), indexed by `tx_size_context` (0..=2).
331+pub const TX_SIZE_CAT0: [[u16; 3]; 3] = [[19968, 32768, 0], [19968, 32768, 0], [24320, 32768, 0]];
332+/// `default_tx_size_cdf[1]` (entropymode.c): a 16x16 block's `tx_depth`
333+/// (`TX16`/`TX8`/`TX4`), indexed by `tx_size_context`.
334+pub const TX_SIZE_CAT1: [[u16; 4]; 3] = [
335+ [12272, 30172, 32768, 0],
336+ [12272, 30172, 32768, 0],
337+ [18677, 30848, 32768, 0],
338+];
339+/// `default_tx_size_cdf[2]` (entropymode.c): a 32x32 block's `tx_depth`
340+/// (`TX32`/`TX16`/`TX8`), indexed by `tx_size_context`.
341+pub const TX_SIZE_CAT2: [[u16; 4]; 3] = [
342+ [12986, 15180, 32768, 0],
343+ [12986, 15180, 32768, 0],
344+ [24302, 25602, 32768, 0],
345+];
346+/// `default_tx_size_cdf[3]` (entropymode.c): a 64x64 block's `tx_depth`
347+/// (`TX64`/`TX32`/`TX16`), indexed by `tx_size_context`.
348+pub const TX_SIZE_CAT3: [[u16; 4]; 3] = [
349+ [5782, 11475, 32768, 0],
350+ [5782, 11475, 32768, 0],
351+ [16803, 22759, 32768, 0],
352+];
353+
329354 /// `Default_Cfl_Alpha_Cdf` (spec 9.4): a `UV_CFL_PRED` block's per-plane
330355 /// alpha magnitude (spec 5.11.45's `cfl_alpha_u`/`cfl_alpha_v`), indexed by
331356 /// the joint sign's context (`CFL_CONTEXT_U`/`CFL_CONTEXT_V`,
@@ -3733,3 +3758,153 @@ pub const INTRA_TX_TYPE_SET1_8: [[u16; 8]; 13] = [
37333758 /// at the flat 5-way default (unlike its trained `TX_16X16` neighbour,
37343759 /// [`INTRA_TX_TYPE_SET2_16`]) -- every mode reads the same uniform CDF.
37353760 pub const INTRA_TX_TYPE_SET2_8: [[u16; 6]; 13] = [[6554, 13107, 19661, 26214, 32768, 0]; 13];
3761+/// The full luma sub-range of spec `TXB_SKIP_CONTEXTS` (contexts 0..6, the
3762+/// `plane_bsize != tx_size` `skip_contexts[top][left]` table plus its own
3763+/// single `plane_bsize == tx_size` context 0), for a `TxMode::Select` block
3764+/// whose luma transform is split into several transform units smaller than
3765+/// the block itself -- the single-context `TXB_SKIP_LUMA_*` constants above
3766+/// stay for every other caller, which only ever reads context 0.
3767+pub const TXB_SKIP_LUMA_8_Q0_CTX: [[u16; 3]; 7] = [
3768+ [31548, 32768, 0],
3769+ [1549, 32768, 0],
3770+ [10130, 32768, 0],
3771+ [16656, 32768, 0],
3772+ [18591, 32768, 0],
3773+ [26308, 32768, 0],
3774+ [32537, 32768, 0],
3775+];
3776+pub const TXB_SKIP_LUMA_8_Q1_CTX: [[u16; 3]; 7] = [
3777+ [31782, 32768, 0],
3778+ [1836, 32768, 0],
3779+ [10689, 32768, 0],
3780+ [17604, 32768, 0],
3781+ [21622, 32768, 0],
3782+ [27518, 32768, 0],
3783+ [32399, 32768, 0],
3784+];
3785+pub const TXB_SKIP_LUMA_8_CTX: [[u16; 3]; 7] = [
3786+ [31957, 32768, 0],
3787+ [3230, 32768, 0],
3788+ [11153, 32768, 0],
3789+ [18123, 32768, 0],
3790+ [20143, 32768, 0],
3791+ [26536, 32768, 0],
3792+ [31986, 32768, 0],
3793+];
3794+pub const TXB_SKIP_LUMA_8_Q3_CTX: [[u16; 3]; 7] = [
3795+ [31903, 32768, 0],
3796+ [2044, 32768, 0],
3797+ [7528, 32768, 0],
3798+ [14618, 32768, 0],
3799+ [16182, 32768, 0],
3800+ [24168, 32768, 0],
3801+ [31037, 32768, 0],
3802+];
3803+pub const TXB_SKIP_LUMA_16_Q0_CTX: [[u16; 3]; 7] = [
3804+ [29957, 32768, 0],
3805+ [5391, 32768, 0],
3806+ [18039, 32768, 0],
3807+ [23566, 32768, 0],
3808+ [22431, 32768, 0],
3809+ [25822, 32768, 0],
3810+ [32197, 32768, 0],
3811+];
3812+pub const TXB_SKIP_LUMA_16_Q1_CTX: [[u16; 3]; 7] = [
3813+ [31901, 32768, 0],
3814+ [10311, 32768, 0],
3815+ [18047, 32768, 0],
3816+ [24806, 32768, 0],
3817+ [23288, 32768, 0],
3818+ [27914, 32768, 0],
3819+ [32296, 32768, 0],
3820+];
3821+pub const TXB_SKIP_LUMA_16_CTX: [[u16; 3]; 7] = [
3822+ [32363, 32768, 0],
3823+ [10692, 32768, 0],
3824+ [19090, 32768, 0],
3825+ [24357, 32768, 0],
3826+ [24442, 32768, 0],
3827+ [28312, 32768, 0],
3828+ [32169, 32768, 0],
3829+];
3830+pub const TXB_SKIP_LUMA_16_Q3_CTX: [[u16; 3]; 7] = [
3831+ [32510, 32768, 0],
3832+ [8430, 32768, 0],
3833+ [17318, 32768, 0],
3834+ [24154, 32768, 0],
3835+ [23674, 32768, 0],
3836+ [28789, 32768, 0],
3837+ [32139, 32768, 0],
3838+];
3839+pub const TXB_SKIP_LUMA_32_Q0_CTX: [[u16; 3]; 7] = [
3840+ [17920, 32768, 0],
3841+ [1818, 32768, 0],
3842+ [7282, 32768, 0],
3843+ [25273, 32768, 0],
3844+ [10923, 32768, 0],
3845+ [31554, 32768, 0],
3846+ [32624, 32768, 0],
3847+];
3848+pub const TXB_SKIP_LUMA_32_Q1_CTX: [[u16; 3]; 7] = [
3849+ [26726, 32768, 0],
3850+ [1045, 32768, 0],
3851+ [11703, 32768, 0],
3852+ [20590, 32768, 0],
3853+ [18554, 32768, 0],
3854+ [25970, 32768, 0],
3855+ [31938, 32768, 0],
3856+];
3857+pub const TXB_SKIP_LUMA_32_CTX: [[u16; 3]; 7] = [
3858+ [30669, 32768, 0],
3859+ [3832, 32768, 0],
3860+ [11663, 32768, 0],
3861+ [18889, 32768, 0],
3862+ [19782, 32768, 0],
3863+ [23313, 32768, 0],
3864+ [31330, 32768, 0],
3865+];
3866+pub const TXB_SKIP_LUMA_32_Q3_CTX: [[u16; 3]; 7] = [
3867+ [31671, 32768, 0],
3868+ [2056, 32768, 0],
3869+ [11746, 32768, 0],
3870+ [16852, 32768, 0],
3871+ [18635, 32768, 0],
3872+ [24715, 32768, 0],
3873+ [31484, 32768, 0],
3874+];
3875+pub const TXB_SKIP_LUMA_64_Q0_CTX: [[u16; 3]; 7] = [
3876+ [6308, 32768, 0],
3877+ [117, 32768, 0],
3878+ [1638, 32768, 0],
3879+ [2161, 32768, 0],
3880+ [16384, 32768, 0],
3881+ [10923, 32768, 0],
3882+ [30247, 32768, 0],
3883+];
3884+pub const TXB_SKIP_LUMA_64_Q1_CTX: [[u16; 3]; 7] = [
3885+ [26584, 32768, 0],
3886+ [188, 32768, 0],
3887+ [8847, 32768, 0],
3888+ [24519, 32768, 0],
3889+ [22938, 32768, 0],
3890+ [30583, 32768, 0],
3891+ [32608, 32768, 0],
3892+];
3893+pub const TXB_SKIP_LUMA_64_CTX: [[u16; 3]; 7] = [
3894+ [28573, 32768, 0],
3895+ [3183, 32768, 0],
3896+ [17802, 32768, 0],
3897+ [25977, 32768, 0],
3898+ [26677, 32768, 0],
3899+ [27832, 32768, 0],
3900+ [32387, 32768, 0],
3901+];
3902+pub const TXB_SKIP_LUMA_64_Q3_CTX: [[u16; 3]; 7] = [
3903+ [31539, 32768, 0],
3904+ [8433, 32768, 0],
3905+ [20576, 32768, 0],
3906+ [27904, 32768, 0],
3907+ [27852, 32768, 0],
3908+ [30026, 32768, 0],
3909+ [32441, 32768, 0],
3910+];
crates/ec-av1/src/cdf_state.rsmodified+80 -35
@@ -41,6 +41,10 @@ pub(crate) enum TxbSet {
4141 /// block (lane-av1-rect). An `is_inter` leaf reads
4242 /// [`Luma8Inter`](TxbSet::Luma8Inter) instead.
4343 Luma8,
44+ /// [`Luma8`](TxbSet::Luma8)'s `reduced_tx_set: false` counterpart (round
45+ /// 8): the same coefficient tables, but the seven-symbol `TX_SET_INTRA_1`
46+ /// `tx_type` table rather than `Luma8`'s five-symbol reduced one.
47+ Luma8Set1,
4448 /// The 8x8 luma transform of an `is_inter` 8x8 leaf under a straddling
4549 /// 16x16 block (lane-av1inter8): the same coefficient tables
4650 /// [`Luma8`](TxbSet::Luma8) reads, but `get_tx_set` (spec 5.11.48)
@@ -116,10 +120,14 @@ pub(crate) struct Cdfs {
116120 pub cfl_alpha: [[u16; 17]; 6],
117121 /// The angle a directional mode is nudged by.
118122 pub angle_delta: [[u16; 8]; 8],
119- /// The one-context all-zero flag of a 16x16 luma transform.
120- pub txb_skip_luma_16: [[u16; 3]; 1],
121- /// The one-context all-zero flag of an 8x8 luma transform.
122- pub txb_skip_luma_8: [[u16; 3]; 1],
123+ /// The all-zero flag of a 16x16 luma transform, 7 contexts (index 0 for
124+ /// a lone TU whose own bsize equals the block's; 1..6 the
125+ /// `skip_contexts[top][left]` table when `TxMode::Select` splits the
126+ /// block's luma into several smaller transform units, spec
127+ /// `get_txb_ctx_general`).
128+ pub txb_skip_luma_16: [[u16; 3]; 7],
129+ /// The same, for an 8x8 luma transform.
130+ pub txb_skip_luma_8: [[u16; 3]; 7],
123131 /// The all-zero flag of an 8x8 chroma transform.
124132 pub txb_skip_chroma_8: [[u16; 3]; 3],
125133 /// The all-zero flag of a 4x4 chroma transform.
@@ -170,10 +178,10 @@ pub(crate) struct Cdfs {
170178 /// `PARTITION_NONE` is ever coded against it, but the alphabet still
171179 /// adapts on every read, same as every other partition table.
172180 pub partition_w8: [[u16; 5]; 4],
173- /// The one-context all-zero flag of a 32x32 luma transform.
174- pub txb_skip_luma_32: [[u16; 3]; 1],
181+ /// The same as `txb_skip_luma_16`'s 7 contexts, for a 32x32 luma transform.
182+ pub txb_skip_luma_32: [[u16; 3]; 7],
175183 /// The same, for a 64x64 luma transform.
176- pub txb_skip_luma_64: [[u16; 3]; 1],
184+ pub txb_skip_luma_64: [[u16; 3]; 7],
177185 /// The all-zero flag of a 16x16 chroma transform.
178186 pub txb_skip_chroma_16: [[u16; 3]; 3],
179187 /// The all-zero flag of a 32x32 chroma transform.
@@ -221,6 +229,13 @@ pub(crate) struct Cdfs {
221229 pub intra_tx_type_16: [[u16; 6]; 13],
222230 /// The transform type of an 8x8 intra luma transform, by luma mode.
223231 pub intra_tx_type_8: [[u16; 6]; 13],
232+ /// The transform type of an 8x8 intra luma transform, by luma mode, when
233+ /// `reduced_tx_set` is false (spec `TX_SET_INTRA_1`, seven types) --
234+ /// [`Self::intra_tx_type_8`]'s five-type table is only the
235+ /// `reduced_tx_set` row (round 8: a real `aomenc` stream with
236+ /// `reduced_tx_set` off desyncs an 8x8 TU read against this row's
237+ /// wrong-length CDF).
238+ pub intra_tx_type_8_set1: [[u16; 8]; 13],
224239 /// The transform type of an `is_inter` 32x32 luma transform (spec
225240 /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it
226241 /// is not indexed by mode.
@@ -260,6 +275,15 @@ pub(crate) struct Cdfs {
260275 pub filter_intra: [[u16; 3]; 4],
261276 /// Which `FILTER_INTRA_MODES` entry a `use_filter_intra` block picks.
262277 pub filter_intra_mode: [u16; 6],
278+ /// `TxMode::Select`'s `tx_depth` flag at an 8x8 block, by `tx_size_context`
279+ /// (0..=2) -- see [`cdf::TX_SIZE_CAT0`].
280+ pub tx_size_cat0: [[u16; 3]; 3],
281+ /// `tx_depth` at a 16x16 block -- see [`cdf::TX_SIZE_CAT1`].
282+ pub tx_size_cat1: [[u16; 4]; 3],
283+ /// `tx_depth` at a 32x32 block -- see [`cdf::TX_SIZE_CAT2`].
284+ pub tx_size_cat2: [[u16; 4]; 3],
285+ /// `tx_depth` at a 64x64 block -- see [`cdf::TX_SIZE_CAT3`].
286+ pub tx_size_cat3: [[u16; 4]; 3],
263287 }
264288
265289 /// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`,
@@ -417,6 +441,7 @@ impl Cdfs {
417441 reset2(&mut self.dc_sign_luma);
418442 reset2(&mut self.intra_tx_type_16);
419443 reset2(&mut self.intra_tx_type_8);
444+ reset2(&mut self.intra_tx_type_8_set1);
420445 reset1(&mut self.inter_tx_type_32);
421446 reset1(&mut self.inter_tx_type_16);
422447 reset1(&mut self.inter_tx_type_8);
@@ -434,6 +459,10 @@ impl Cdfs {
434459 .for_each(MvComponentCdfs::reset_counts);
435460 reset2(&mut self.filter_intra);
436461 reset1(&mut self.filter_intra_mode);
462+ reset2(&mut self.tx_size_cat0);
463+ reset2(&mut self.tx_size_cat1);
464+ reset2(&mut self.tx_size_cat2);
465+ reset2(&mut self.tx_size_cat3);
437466 }
438467
439468 /// The defaults a key frame starts from (spec 8.4, `init_coeff_cdfs` and
@@ -451,20 +480,20 @@ impl Cdfs {
451480 cfl_sign: cdf::CFL_SIGN,
452481 cfl_alpha: cdf::CFL_ALPHA,
453482 angle_delta: cdf::ANGLE_DELTA,
454- txb_skip_luma_16: [pick(
455- q_ctx,
456- cdf::TXB_SKIP_LUMA_16_Q0,
457- cdf::TXB_SKIP_LUMA_16_Q1,
458- cdf::TXB_SKIP_LUMA_16,
459- cdf::TXB_SKIP_LUMA_16_Q3,
460- )],
461- txb_skip_luma_8: [pick(
462- q_ctx,
463- cdf::TXB_SKIP_LUMA_8_Q0,
464- cdf::TXB_SKIP_LUMA_8_Q1,
465- cdf::TXB_SKIP_LUMA_8,
466- cdf::TXB_SKIP_LUMA_8_Q3,
467- )],
483+ txb_skip_luma_16: pick(
484+ q_ctx,
485+ cdf::TXB_SKIP_LUMA_16_Q0_CTX,
486+ cdf::TXB_SKIP_LUMA_16_Q1_CTX,
487+ cdf::TXB_SKIP_LUMA_16_CTX,
488+ cdf::TXB_SKIP_LUMA_16_Q3_CTX,
489+ ),
490+ txb_skip_luma_8: pick(
491+ q_ctx,
492+ cdf::TXB_SKIP_LUMA_8_Q0_CTX,
493+ cdf::TXB_SKIP_LUMA_8_Q1_CTX,
494+ cdf::TXB_SKIP_LUMA_8_CTX,
495+ cdf::TXB_SKIP_LUMA_8_Q3_CTX,
496+ ),
468497 txb_skip_chroma_8: pick(
469498 q_ctx,
470499 cdf::TXB_SKIP_CHROMA_8_Q0,
@@ -621,20 +650,20 @@ impl Cdfs {
621650 ),
622651 partition_w16: cdf::PARTITION_W16,
623652 partition_w8: cdf::PARTITION_W8,
624- txb_skip_luma_32: [pick(
625- q_ctx,
626- cdf::TXB_SKIP_LUMA_32_Q0,
627- cdf::TXB_SKIP_LUMA_32_Q1,
628- cdf::TXB_SKIP_LUMA_32,
629- cdf::TXB_SKIP_LUMA_32_Q3,
630- )],
631- txb_skip_luma_64: [pick(
632- q_ctx,
633- cdf::TXB_SKIP_LUMA_64_Q0,
634- cdf::TXB_SKIP_LUMA_64_Q1,
635- cdf::TXB_SKIP_LUMA_64,
636- cdf::TXB_SKIP_LUMA_64_Q3,
637- )],
653+ txb_skip_luma_32: pick(
654+ q_ctx,
655+ cdf::TXB_SKIP_LUMA_32_Q0_CTX,
656+ cdf::TXB_SKIP_LUMA_32_Q1_CTX,
657+ cdf::TXB_SKIP_LUMA_32_CTX,
658+ cdf::TXB_SKIP_LUMA_32_Q3_CTX,
659+ ),
660+ txb_skip_luma_64: pick(
661+ q_ctx,
662+ cdf::TXB_SKIP_LUMA_64_Q0_CTX,
663+ cdf::TXB_SKIP_LUMA_64_Q1_CTX,
664+ cdf::TXB_SKIP_LUMA_64_CTX,
665+ cdf::TXB_SKIP_LUMA_64_Q3_CTX,
666+ ),
638667 txb_skip_chroma_16: pick(
639668 q_ctx,
640669 cdf::TXB_SKIP_CHROMA_16_Q0,
@@ -778,6 +807,7 @@ impl Cdfs {
778807 dc_sign_luma: cdf::DC_SIGN_LUMA,
779808 intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16,
780809 intra_tx_type_8: cdf::INTRA_TX_TYPE_SET2_8,
810+ intra_tx_type_8_set1: cdf::INTRA_TX_TYPE_SET1_8,
781811 inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32,
782812 inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16,
783813 inter_tx_type_8: cdf::INTER_TX_TYPE_SET3_8,
@@ -793,6 +823,10 @@ impl Cdfs {
793823 mv_comp: [MvComponentCdfs::new(), MvComponentCdfs::new()],
794824 filter_intra: cdf::FILTER_INTRA,
795825 filter_intra_mode: cdf::FILTER_INTRA_MODE,
826+ tx_size_cat0: cdf::TX_SIZE_CAT0,
827+ tx_size_cat1: cdf::TX_SIZE_CAT1,
828+ tx_size_cat2: cdf::TX_SIZE_CAT2,
829+ tx_size_cat3: cdf::TX_SIZE_CAT3,
796830 }
797831 }
798832
@@ -865,6 +899,17 @@ impl Cdfs {
865899 dc_sign: &mut self.dc_sign_luma,
866900 tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()),
867901 },
902+ TxbSet::Luma8Set1 => TxbTables {
903+ side: 8,
904+ txb_skip: &mut self.txb_skip_luma_8,
905+ eob_pt: &mut self.eob_pt_64_luma,
906+ eob_extra: &mut self.eob_extra_luma_8,
907+ base: &mut self.base_luma_8,
908+ base_eob: &mut self.base_eob_luma_8,
909+ br: &mut self.br_luma_8,
910+ dc_sign: &mut self.dc_sign_luma,
911+ tx_type: Some(self.intra_tx_type_8_set1[mode].as_mut_slice()),
912+ },
868913 TxbSet::Luma8Inter => TxbTables {
869914 side: 8,
870915 txb_skip: &mut self.txb_skip_luma_8,
crates/ec-av1/src/decode.rsmodified+421 -23
@@ -61,6 +61,19 @@ static DEBLOCK_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicU
6161 pub(crate) fn deblock_hits() -> usize {
6262 DEBLOCK_HITS.load(std::sync::atomic::Ordering::Relaxed)
6363 }
64+
65+/// How many [`read_tx_size`] reads resolved a `tx_depth` strictly less than
66+/// the block's own side, across every call in the process -- the same
67+/// before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream
68+/// actually exercised a *split* transform under `TxMode::Select` rather than
69+/// every block coincidentally resolving `depth=0` (indistinguishable from
70+/// `TxMode::Largest` pixel-wise).
71+static TX_DEPTH_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
72+
73+/// Current value of [`TX_DEPTH_HITS`].
74+pub(crate) fn tx_depth_hits() -> usize {
75+ TX_DEPTH_HITS.load(std::sync::atomic::Ordering::Relaxed)
76+}
6477 const SUB_MI: u32 = 4;
6578 const MI: usize = 4;
6679 const SB: usize = 64;
@@ -275,13 +288,20 @@ fn read_coeffs(
275288 }
276289 let mut tx_type = TxType::DctDct;
277290 if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() {
291+ // The CDF row's own width names its set: 7 symbols (8 slots) is
292+ // `TX_SET_INTRA_1`, 5 (6 slots) the reduced `TX_SET_INTRA_2` (the
293+ // inter sets we read share set 2's symbol order).
294+ let set1 = tx_type_cdf.len() == 8;
278295 let t = dec.symbol(tx_type_cdf);
279296 if trace {
280- eprintln!("TRACE tx_type value={t}");
297+ eprintln!("TRACE tx_type value={t} set1={set1}");
298+ }
299+ tx_type = if set1 {
300+ TxType::from_symbol_set1(t)
301+ } else {
302+ TxType::from_symbol(t)
281303 }
282- tx_type = TxType::from_symbol(t).ok_or_else(|| {
283- unsupported(format!("a tx_type symbol outside its CDF's own set: {t}"))
284- })?;
304+ .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?;
285305 }
286306
287307 let eob = read_eob(dec, coding);
@@ -368,17 +388,37 @@ fn read_coeffs(
368388 /// [`crate::tile`]'s own private `Neighbour`.
369389 #[derive(Clone, Copy, Default)]
370390 struct Neighbour {
371- coded: bool,
391+ /// The transform unit's cumulative coefficient level (spec `cul_level`,
392+ /// `decodetxb.c`'s `read_coeffs_txb`): the sum of every coded level's
393+ /// magnitude, clamped to `COEFF_CONTEXT_MASK` (7). A plain "coded or not"
394+ /// boolean (`level != 0`) is enough for every context this decoder read
395+ /// until now, but the *luma* `txb_skip_ctx` of a transform unit smaller
396+ /// than its own block (`TxMode::Select`, spec `get_txb_ctx_general`)
397+ /// needs the actual magnitude tier of its above/left neighbours, not
398+ /// just whether they coded anything.
399+ level: u8,
372400 dc: Option<bool>,
373401 }
374402
375403 fn neighbour_state(grid: &[i32]) -> Neighbour {
404+ let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum();
376405 Neighbour {
377- coded: grid.iter().any(|&l| l != 0),
406+ level: cul.min(7) as u8,
378407 dc: (grid[0] != 0).then_some(grid[0] < 0),
379408 }
380409 }
381410
411+/// spec `get_txb_ctx_general`'s `skip_contexts[top][left]` table (plane 0,
412+/// transform unit smaller than the block it sits in): `top`/`left` are the
413+/// above/left neighbours' magnitude tiers, each already clamped to 4.
414+const SKIP_CONTEXTS: [[usize; 5]; 5] = [
415+ [1, 2, 2, 2, 3],
416+ [2, 4, 4, 4, 5],
417+ [2, 4, 4, 4, 5],
418+ [2, 4, 4, 4, 5],
419+ [3, 5, 5, 5, 6],
420+];
421+
382422 /// The neighbour state a block's contexts are read from — a duplicate of
383423 /// [`crate::tile`]'s own private `Neighbours` (that module is another lane's
384424 /// territory this round), kept at the same two granularities the real writer
@@ -588,14 +628,37 @@ impl Neighbours {
588628 for cell in 0..side_mi {
589629 let above = &self.above[mi_c + cell][plane];
590630 let left = &self.left[mi_r + cell][plane];
591- above_coded |= above.coded;
592- left_coded |= left.coded;
631+ above_coded |= above.level != 0;
632+ left_coded |= left.level != 0;
593633 vote += dc_vote(above.dc) + dc_vote(left.dc);
594634 }
595635 (above_coded, left_coded, vote)
596636 })
597637 }
598638
639+ /// The luma `txb_skip_ctx` of a transform unit smaller than its own block
640+ /// (spec `get_txb_ctx_general`, plane 0, `plane_bsize != tx_size`): the
641+ /// above/left neighbours' magnitude tiers, OR-reduced over the unit's own
642+ /// span and each clamped to 4, indexed into [`SKIP_CONTEXTS`]. Unlike
643+ /// [`Self::around_mi`]'s plain coded/uncoded bit, this needs the real
644+ /// cumulative level -- a lone-TU block never calls this (its `txb_skip_ctx`
645+ /// is always 0, spec's `plane_bsize == tx_size` branch).
646+ fn luma_skip_ctx(&self, (mi_r, mi_c): (usize, usize), side_mi: usize) -> usize {
647+ let mut top = 0u8;
648+ let mut left = 0u8;
649+ for cell in 0..side_mi {
650+ top |= self.above[mi_c + cell][0].level;
651+ left |= self.left[mi_r + cell][0].level;
652+ }
653+ let ctx = SKIP_CONTEXTS[(top as usize).min(4)][(left as usize).min(4)];
654+ if std::env::var_os("EC_AV1_TRACE").is_some() {
655+ eprintln!(
656+ "TRACE luma_skip_ctx mi=({mi_r},{mi_c}) side_mi={side_mi} top={top} left={left} ctx={ctx}"
657+ );
658+ }
659+ ctx
660+ }
661+
599662 /// Writes one coded block into every cell it covers, on both grids: the
600663 /// mode/side arrays up to `side`'s own width, and the coefficient-context
601664 /// arrays up to the true frame edge — the units past it are left at their
@@ -658,6 +721,69 @@ impl Neighbours {
658721 });
659722 }
660723 }
724+
725+ /// [`Self::record_mi`]'s plane-0 half only, for one luma transform unit
726+ /// inside a coded block whose luma transform is smaller than its own
727+ /// side (`TxMode::Select`): the real per-transform-unit coefficient
728+ /// context (spec `AboveLevelContext`/`LeftLevelContext`), unlike the
729+ /// coarse per-block `tx_grid`/`fill_lf_grid` state
730+ /// [`Self::record_split_luma`] still writes once for the whole block.
731+ /// Leaves `above_side_mi`/`left_side_mi` and the chroma planes alone --
732+ /// the caller's own [`Self::record_split_luma`] call sets those once,
733+ /// at the block's own true side, not this TU's.
734+ fn record_mi_luma(&mut self, (mi_r, mi_c): (usize, usize), tx_px: usize, grid: &[i32]) {
735+ let state = neighbour_state(grid);
736+ let side_mi = tx_px / MI;
737+ for cell in 0..side_mi {
738+ if mi_r + cell < self.mi_rows {
739+ self.left[mi_r + cell][0] = state;
740+ }
741+ if mi_c + cell < self.mi_cols {
742+ self.above[mi_c + cell][0] = state;
743+ }
744+ }
745+ }
746+
747+ /// [`Self::record`]'s mode/side and chroma-context bookkeeping, for a
748+ /// block whose luma was decoded as several transform units through
749+ /// [`Self::record_mi_luma`] rather than [`Self::record`]'s own single
750+ /// whole-block luma write -- everything [`Self::record`] does except
751+ /// plane 0, which is already correct per-TU by the time this runs.
752+ fn record_split_luma(
753+ &mut self,
754+ at: (usize, usize),
755+ side: usize,
756+ mode: usize,
757+ chroma_grids: [&[i32]; 2],
758+ ) {
759+ let (r, c) = at;
760+ for cell in 0..side / SUB {
761+ self.above_mode[c + cell] = mode;
762+ self.left_mode[r + cell] = mode;
763+ self.above_side[c + cell] = side;
764+ self.left_side[r + cell] = side;
765+ }
766+ let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));
767+ let side_mi = side / MI;
768+ for cell in 0..side_mi {
769+ self.left_side_mi[mi_r + cell] = side;
770+ self.above_side_mi[mi_c + cell] = side;
771+ }
772+ let round_up_even = |n: usize| n.div_ceil(2) * 2;
773+ let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols));
774+ for (plane_idx, grid) in chroma_grids.into_iter().enumerate() {
775+ let plane = plane_idx + 1;
776+ let state = neighbour_state(grid);
777+ for cell in 0..side_mi {
778+ if cell < side_mi.min(bound_h.saturating_sub(mi_r)) {
779+ self.left[mi_r + cell][plane] = state;
780+ }
781+ if cell < side_mi.min(bound_w.saturating_sub(mi_c)) {
782+ self.above[mi_c + cell][plane] = state;
783+ }
784+ }
785+ }
786+ }
661787 }
662788
663789 /// Reads one coded block's skip flag, luma mode, angle delta (if the mode
@@ -956,9 +1082,16 @@ fn read_plane(
9561082 base_q_idx: u8,
9571083 cfl: Option<(i32, &[i32])>,
9581084 filter_intra: Option<usize>,
1085+ // Only meaningful for `plane_idx == 0`: `Some` when this transform unit is
1086+ // smaller than its own block (spec `get_txb_ctx_general`'s
1087+ // `plane_bsize != tx_size` branch), carrying the already-looked-up
1088+ // `SKIP_CONTEXTS` value; `None` everywhere else (a lone luma TU, or any
1089+ // chroma plane), where `txb_skip_ctx` is 0 or the usual OR-of-neighbours
1090+ // formula below.
1091+ luma_skip_ctx: Option<usize>,
9591092 ) -> Result<Vec<i32>> {
9601093 let skip_ctx = if plane_idx == 0 {
961- 0
1094+ luma_skip_ctx.unwrap_or(0)
9621095 } else {
9631096 usize::from(around.0) + usize::from(around.1)
9641097 };
@@ -1035,6 +1168,11 @@ fn decode_block(
10351168 v: &mut PlaneBuf,
10361169 base_q_idx: u8,
10371170 enable_filter_intra: bool,
1171+ scan32: &[u16],
1172+ scan16: &[u16],
1173+ scan8: &[u16],
1174+ tx_select: bool,
1175+ reduced_tx_set: bool,
10381176 ) -> Result<()> {
10391177 let (r, c) = at;
10401178 let (px, py) = (c * SUB, r * SUB);
@@ -1047,8 +1185,31 @@ fn decode_block(
10471185 side,
10481186 enable_filter_intra,
10491187 )?;
1188+ let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));
1189+ // `TxMode::Select`'s `tx_depth` (spec 5.11.16): read for every intra
1190+ // block, skipped or not. `logical_tx` is what the loop filter's edge
1191+ // lookup and the next block's own `tx_size_context` want (the *nominal*
1192+ // transform size -- 64 even for the 64x64 corner-scanned case, spec
1193+ // 5.11.40); `coeff_tx_side` is the actual coded coefficient grid's side,
1194+ // which only differs from it at that one corner case.
1195+ let (logical_tx, coeff_tx_side) = if tx_select {
1196+ 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+ }
1202+ (resolved, if resolved == 64 { 32 } else { resolved })
1203+ } else {
1204+ (side, luma_tx)
1205+ };
1206+ let luma_set = if tx_select {
1207+ txbset_for(logical_tx, reduced_tx_set)
1208+ } else {
1209+ luma_set
1210+ };
1211+ let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8);
10501212 let reach = Reach::of(side, px, py, y.width, y.height);
1051- let (luma_grid, u_grid, v_grid);
10521213 let (cpx, cpy) = (px / 2, py / 2);
10531214 let chroma_side = side / 2;
10541215 if skip {
@@ -1085,16 +1246,20 @@ fn decode_block(
10851246 alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
10861247 None,
10871248 );
1088- luma_grid = vec![0i32; side * side];
1089- u_grid = vec![0i32; chroma_side * chroma_side];
1090- v_grid = vec![0i32; chroma_side * chroma_side];
1091- } else {
1249+ let luma_grid = vec![0i32; side * side];
1250+ let u_grid = vec![0i32; chroma_side * chroma_side];
1251+ let v_grid = vec![0i32; chroma_side * chroma_side];
1252+ neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]);
1253+ } else if !tx_select || logical_tx == side {
1254+ // A single luma transform unit -- the old path, unchanged (including
1255+ // the 64x64 corner case, `logical_tx == side == 64` with
1256+ // `coeff_tx_side == 32`).
10921257 let around = neighbours.around(at, side);
1093- luma_grid = read_plane(
1258+ let luma_grid = read_plane(
10941259 dec,
10951260 cdfs,
10961261 luma_set,
1097- scans.0,
1262+ luma_scan,
10981263 0,
10991264 around[0],
11001265 mode,
@@ -1104,13 +1269,14 @@ fn decode_block(
11041269 px,
11051270 py,
11061271 side,
1107- luma_tx,
1272+ coeff_tx_side,
11081273 base_q_idx,
11091274 None,
11101275 filter_intra,
1276+ None,
11111277 )?;
11121278 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
1113- u_grid = read_plane(
1279+ let u_grid = read_plane(
11141280 dec,
11151281 cdfs,
11161282 chroma_set,
@@ -1128,8 +1294,9 @@ fn decode_block(
11281294 base_q_idx,
11291295 alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
11301296 None,
1297+ None,
11311298 )?;
1132- v_grid = read_plane(
1299+ let v_grid = read_plane(
11331300 dec,
11341301 cdfs,
11351302 chroma_set,
@@ -1147,14 +1314,104 @@ fn decode_block(
11471314 base_q_idx,
11481315 alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
11491316 None,
1317+ None,
11501318 )?;
1319+ neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]);
1320+ } else {
1321+ // Several luma transform units, raster order (spec
1322+ // `transform_block`'s loop): the block's resolved transform is
1323+ // genuinely smaller than its own side, so each tile of it predicts
1324+ // and codes independently, its own context read fresh off whatever
1325+ // the earlier tiles in this same block already wrote -- the same
1326+ // per-TU pattern [`decode_leaf8`] already runs for an 8x8 leaf.
1327+ let n_axis = side / logical_tx;
1328+ for tu_row in 0..n_axis {
1329+ for tu_col in 0..n_axis {
1330+ let tu_mi = (
1331+ mi_r + tu_row * (logical_tx / MI),
1332+ mi_c + tu_col * (logical_tx / MI),
1333+ );
1334+ let tu_px = px + tu_col * logical_tx;
1335+ let tu_py = py + tu_row * logical_tx;
1336+ let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0];
1337+ let tu_reach = Reach::of(logical_tx, tu_px, tu_py, y.width, y.height);
1338+ // This transform unit's own bsize (`logical_tx`) is smaller
1339+ // than the block it sits in (`side`), so `txb_skip_ctx` is
1340+ // the neighbour-magnitude table, not the lone-TU 0 (spec
1341+ // `get_txb_ctx_general`'s `plane_bsize != tx_size` branch).
1342+ let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, logical_tx / MI);
1343+ let tu_grid = read_plane(
1344+ dec,
1345+ cdfs,
1346+ luma_set,
1347+ luma_scan,
1348+ 0,
1349+ tu_around,
1350+ mode,
1351+ mode,
1352+ tu_reach,
1353+ y,
1354+ tu_px,
1355+ tu_py,
1356+ logical_tx,
1357+ logical_tx,
1358+ base_q_idx,
1359+ None,
1360+ filter_intra,
1361+ Some(tu_skip_ctx),
1362+ )?;
1363+ neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid);
1364+ }
1365+ }
1366+ let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
1367+ let chroma_around = neighbours.around(at, side);
1368+ let u_grid = read_plane(
1369+ dec,
1370+ cdfs,
1371+ chroma_set,
1372+ scans.1,
1373+ 1,
1374+ chroma_around[1],
1375+ mode,
1376+ DC_PRED,
1377+ Reach::none(),
1378+ u,
1379+ cpx,
1380+ cpy,
1381+ chroma_side,
1382+ chroma_tx,
1383+ base_q_idx,
1384+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
1385+ None,
1386+ None,
1387+ )?;
1388+ let v_grid = read_plane(
1389+ dec,
1390+ cdfs,
1391+ chroma_set,
1392+ scans.1,
1393+ 2,
1394+ chroma_around[2],
1395+ mode,
1396+ DC_PRED,
1397+ Reach::none(),
1398+ v,
1399+ cpx,
1400+ cpy,
1401+ chroma_side,
1402+ chroma_tx,
1403+ base_q_idx,
1404+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
1405+ None,
1406+ None,
1407+ )?;
1408+ neighbours.record_split_luma(at, side, mode, [&u_grid, &v_grid]);
11511409 }
1152- neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]);
11531410 neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip);
11541411 neighbours.fill_lf_grid(
11551412 (r * (SUB / MI), c * (SUB / MI)),
11561413 side / MI,
1157- side as u8,
1414+ logical_tx as u8,
11581415 false,
11591416 );
11601417 Ok(())
@@ -1183,6 +1440,8 @@ fn decode_leaf8(
11831440 v: &mut PlaneBuf,
11841441 base_q_idx: u8,
11851442 enable_filter_intra: bool,
1443+ tx_select: bool,
1444+ reduced_tx_set: bool,
11861445 ) -> Result<usize> {
11871446 let (r, c) = outer_at;
11881447 let mut above_mode = neighbours.above_mode[c];
@@ -1206,6 +1465,19 @@ fn decode_leaf8(
12061465 8,
12071466 enable_filter_intra,
12081467 )?;
1468+ // `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+ }
12091481 let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
12101482 let reach = Reach::of(8, px, py, y.width, y.height);
12111483 let (cpx, cpy) = (px / 2, py / 2);
@@ -1241,7 +1513,11 @@ fn decode_leaf8(
12411513 luma_grid = read_plane(
12421514 dec,
12431515 cdfs,
1244- TxbSet::Luma8,
1516+ if reduced_tx_set {
1517+ TxbSet::Luma8
1518+ } else {
1519+ TxbSet::Luma8Set1
1520+ },
12451521 scans.0,
12461522 0,
12471523 around[0],
@@ -1256,6 +1532,7 @@ fn decode_leaf8(
12561532 base_q_idx,
12571533 None,
12581534 filter_intra,
1535+ None,
12591536 )?;
12601537 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8));
12611538 u_grid = read_plane(
@@ -1276,6 +1553,7 @@ fn decode_leaf8(
12761553 base_q_idx,
12771554 alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
12781555 None,
1556+ None,
12791557 )?;
12801558 v_grid = read_plane(
12811559 dec,
@@ -1295,6 +1573,7 @@ fn decode_leaf8(
12951573 base_q_idx,
12961574 alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
12971575 None,
1576+ None,
12981577 )?;
12991578 }
13001579 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
@@ -1537,6 +1816,76 @@ fn tx_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 {
15371816 if chroma { luma_tx / 2 } else { luma_tx }
15381817 }
15391818
1819+/// `get_tx_size_context` (spec 9.3's `TxDepthCtx`, libaom
1820+/// `TXFM_CONTEXT`/`entropymode.c`): the sum of two boolean terms, each
1821+/// present only when its neighbour exists inside the true frame -- whether
1822+/// the block immediately above (at this block's own leftmost mi column) or
1823+/// to the left (at its own topmost mi row) coded a transform at least as wide
1824+/// as `own_side`.
1825+fn tx_size_context(n: &Neighbours, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize {
1826+ let above = mi_r > 0 && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_side;
1827+ let left = mi_c > 0 && tx_px_at(n, false, mi_r, mi_c - 1) as usize >= own_side;
1828+ usize::from(above) + usize::from(left)
1829+}
1830+
1831+/// `read_tx_size`/`read_selected_tx_size` (spec 5.11.16): under
1832+/// `TxMode::Select`, an intra block's `tx_depth` symbol, resolved straight to
1833+/// the transform's own side in pixels (`side >> depth`) -- this decoder's
1834+/// four square categories (8/16/32/64) each halve at most twice, matching
1835+/// libaom's own per-size depth cap (`cdf::TX_SIZE_CAT0`..`TX_SIZE_CAT3`).
1836+fn read_tx_size(
1837+ dec: &mut SymbolDecoder,
1838+ cdfs: &mut Cdfs,
1839+ n: &Neighbours,
1840+ at_mi: (usize, usize),
1841+ side: usize,
1842+) -> usize {
1843+ let ctx = tx_size_context(n, at_mi, side);
1844+ let depth = match side {
1845+ 8 => dec.symbol(&mut cdfs.tx_size_cat0[ctx]),
1846+ 16 => dec.symbol(&mut cdfs.tx_size_cat1[ctx]),
1847+ 32 => dec.symbol(&mut cdfs.tx_size_cat2[ctx]),
1848+ 64 => dec.symbol(&mut cdfs.tx_size_cat3[ctx]),
1849+ _ => unreachable!("decode_block/decode_leaf8 only call this at 8/16/32/64"),
1850+ };
1851+ if depth != 0 {
1852+ TX_DEPTH_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1853+ }
1854+ side >> depth
1855+}
1856+
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
1859+/// ([`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] {
1861+ match tx_px {
1862+ 32 => scan32,
1863+ 16 => scan16,
1864+ 8 => scan8,
1865+ _ => unreachable!("read_tx_size never resolves the coefficient grid to anything else"),
1866+ }
1867+}
1868+
1869+/// The [`TxbSet`] a resolved luma transform side reads its coefficient tables
1870+/// from -- 64 is only ever the 64x64-block, depth-0, corner-scanned case
1871+/// ([`TxbSet::Luma64`]); every other resolved side maps to its own matching
1872+/// 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`]).
1878+fn txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet {
1879+ match tx_px {
1880+ 64 => TxbSet::Luma64,
1881+ 32 => TxbSet::Luma32,
1882+ 16 => TxbSet::Luma16,
1883+ 8 if reduced_tx_set => TxbSet::Luma8,
1884+ 8 => TxbSet::Luma8Set1,
1885+ _ => unreachable!("read_tx_size never resolves a block's own side to anything else"),
1886+ }
1887+}
1888+
15401889 /// `tx_size_wide_unit_log2`/`tx_size_high_unit_log2`: `log2(px / 4)`, the
15411890 /// index [`tx_dim_to_filter_length`] and the chroma 4-vs-6 choice key on.
15421891 fn unit_log2(px: u32) -> i32 {
@@ -2077,6 +2426,8 @@ pub fn decode_key_frame_tile(
20772426 enable_filter_intra: bool,
20782427 cdef: &CdefParams,
20792428 loop_filter: &LoopFilterParams,
2429+ tx_select: bool,
2430+ reduced_tx_set: bool,
20802431 ) -> Result<Picture> {
20812432 decode_key_frame_tile_with_cdfs(
20822433 data,
@@ -2089,6 +2440,8 @@ pub fn decode_key_frame_tile(
20892440 cdef,
20902441 loop_filter,
20912442 None,
2443+ tx_select,
2444+ reduced_tx_set,
20922445 )
20932446 .map(|(picture, _)| picture)
20942447 }
@@ -2112,6 +2465,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
21122465 cdef: &CdefParams,
21132466 loop_filter: &LoopFilterParams,
21142467 initial_cdfs: Option<Cdfs>,
2468+ tx_select: bool,
2469+ reduced_tx_set: bool,
21152470 ) -> Result<(Picture, Cdfs)> {
21162471 if mi_cols == 0 || mi_rows == 0 {
21172472 return Err(unsupported("a frame with no mode-info grid"));
@@ -2217,6 +2572,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
22172572 &mut v,
22182573 base_q_idx,
22192574 enable_filter_intra,
2575+ &scan32,
2576+ &scan16,
2577+ &scan8,
2578+ tx_select,
2579+ reduced_tx_set,
22202580 )?;
22212581 }
22222582 PARTITION_SPLIT => {
@@ -2274,6 +2634,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
22742634 &mut v,
22752635 base_q_idx,
22762636 enable_filter_intra,
2637+ &scan32,
2638+ &scan16,
2639+ &scan8,
2640+ tx_select,
2641+ reduced_tx_set,
22772642 )?;
22782643 }
22792644 PARTITION_SPLIT => {
@@ -2293,6 +2658,12 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
22932658 let ctx16 = neighbours.partition_ctx(at16, SUB);
22942659 if has_cols16 && has_rows16 {
22952660 let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]);
2661+ if std::env::var_os("EC_AV1_TRACE").is_some() {
2662+ eprintln!(
2663+ "TRACE partition_w16 mi=({},{}) ctx={ctx16} value={part16}",
2664+ at16.0, at16.1
2665+ );
2666+ }
22962667 if part16 == PARTITION_NONE {
22972668 decode_block(
22982669 &mut dec,
@@ -2311,6 +2682,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
23112682 &mut v,
23122683 base_q_idx,
23132684 enable_filter_intra,
2685+ &scan32,
2686+ &scan16,
2687+ &scan8,
2688+ tx_select,
2689+ reduced_tx_set,
23142690 )?;
23152691 continue;
23162692 }
@@ -2354,6 +2730,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
23542730 &mut v,
23552731 base_q_idx,
23562732 enable_filter_intra,
2733+ tx_select,
2734+ reduced_tx_set,
23572735 )?;
23582736 prev_leaf = Some((leaf_mi, leaf_mode));
23592737 }
@@ -2416,6 +2794,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
24162794 &mut v,
24172795 base_q_idx,
24182796 enable_filter_intra,
2797+ tx_select,
2798+ reduced_tx_set,
24192799 )?;
24202800 prev_leaf = Some((leaf_mi, leaf_mode));
24212801 }
@@ -2982,6 +3362,7 @@ fn decode_inter_block(
29823362 base_q_idx,
29833363 None,
29843364 None,
3365+ None,
29853366 )?;
29863367 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
29873368 u_grid = read_plane(
@@ -3002,6 +3383,7 @@ fn decode_inter_block(
30023383 base_q_idx,
30033384 alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)),
30043385 None,
3386+ None,
30053387 )?;
30063388 v_grid = read_plane(
30073389 dec,
@@ -3021,6 +3403,7 @@ fn decode_inter_block(
30213403 base_q_idx,
30223404 alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)),
30233405 None,
3406+ None,
30243407 )?;
30253408 }
30263409 }
@@ -3377,6 +3760,7 @@ fn decode_inter_block8(
33773760 base_q_idx,
33783761 None,
33793762 None,
3763+ None,
33803764 )?;
33813765 u_grid = read_plane(
33823766 dec,
@@ -3396,6 +3780,7 @@ fn decode_inter_block8(
33963780 base_q_idx,
33973781 None,
33983782 None,
3783+ None,
33993784 )?;
34003785 v_grid = read_plane(
34013786 dec,
@@ -3415,6 +3800,7 @@ fn decode_inter_block8(
34153800 base_q_idx,
34163801 None,
34173802 None,
3803+ None,
34183804 )?;
34193805 }
34203806 }
@@ -3808,7 +4194,9 @@ mod tests {
38084194 128,
38094195 false,
38104196 &CdefParams::default(),
3811- &LoopFilterParams::default()
4197+ &LoopFilterParams::default(),
4198+ false,
4199+ true,
38124200 )
38134201 .is_err()
38144202 );
@@ -3849,6 +4237,8 @@ mod tests {
38494237 false,
38504238 &CdefParams::default(),
38514239 &LoopFilterParams::default(),
4240+ false,
4241+ true,
38524242 )
38534243 .unwrap();
38544244 assert_eq!(decoded.width, w, "{w}x{h}: decoded width");
@@ -3930,6 +4320,8 @@ mod tests {
39304320 false,
39314321 &CdefParams::default(),
39324322 &LoopFilterParams::default(),
4323+ false,
4324+ true,
39334325 )
39344326 .unwrap();
39354327 assert!(
@@ -4113,6 +4505,8 @@ mod tests {
41134505 false,
41144506 &CdefParams::default(),
41154507 &LoopFilterParams::default(),
4508+ false,
4509+ true,
41164510 )
41174511 .unwrap();
41184512 assert_eq!(ours.y, ffmpeg_decoded.y, "{width}x{height}: luma vs ffmpeg");
@@ -4183,6 +4577,8 @@ mod tests {
41834577 false,
41844578 &CdefParams::default(),
41854579 &LoopFilterParams::default(),
4580+ false,
4581+ true,
41864582 )
41874583 .unwrap();
41884584 assert_eq!(
@@ -4456,6 +4852,8 @@ mod tests {
44564852 false,
44574853 &CdefParams::default(),
44584854 &LoopFilterParams::default(),
4855+ false,
4856+ true,
44594857 )
44604858 .unwrap();
44614859 assert_eq!(reference.y, ffmpeg_frames[0].y, "frame 0 luma vs ffmpeg");
crates/ec-av1/src/stream.rsmodified+184 -14
@@ -92,22 +92,22 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
9292 "a frame with cdef_bits > 0 (this decoder never reads the per-64x64 cdef_idx symbol)",
9393 ));
9494 }
95- // Every `decode_block` call below assumes the block's transform is
96- // its own full size (`TxMode::Largest`, which is all this crate's own
97- // encoder ever writes, spec `read_tx_size` inferring the max depth
98- // with no bits) -- it never reads a `tx_depth` symbol at all. A real
99- // encoder's `TxMode::Select` (spec 5.9.2's `tx_mode` bit) codes one
100- // per intra block, and skipping that read does not error -- the
101- // decoder just goes on consuming the next bits as if they were the
102- // uv_mode/coefficient symbols they are not, so the block "decodes"
103- // syntactically clean into low-energy garbage: lane-av1real r3's
104- // luma-near-flat-vs-ffmpeg-gradient bug. Refuse before that, the same
105- // way as the CDEF case above, rather than silently miscode every
106- // `TxMode::Select` stream.
107- if header.tx_mode == TxMode::Select {
95+ // An intra frame's `decode_key_frame_tile*` now reads `tx_depth`
96+ // under `TxMode::Select` (lane-av1txsel, spec 5.11.16): the key-frame
97+ // decode path threads `tx_select` through `decode_block`/
98+ // `decode_leaf8`, and refuses on its own (a named `unsupported`, not
99+ // a silent desync) the one case it still cannot code -- a resolved
100+ // 4x4 luma transform, which has no coefficient tables here. This
101+ // refusal is narrowed to what genuinely still has no reader at all:
102+ // `decode_inter_frame`'s inter path never threads `tx_select`
103+ // through its own block loop, so an inter frame under
104+ // `TxMode::Select` still desyncs the same way the comment above used
105+ // to describe for every frame -- lane-av1real r3's
106+ // luma-near-flat-vs-ffmpeg-gradient bug.
107+ if header.tx_mode == TxMode::Select && header.frame_type != FrameType::Key {
108108 return Err(Error::unsupported(
109109 "AV1 decode_stream",
110- "a frame using TxMode::Select (this decoder never reads a tx_depth symbol, so it desyncs after the first intra block's mode)",
110+ "an inter frame using TxMode::Select (this decoder's inter path never reads a tx_depth symbol, so it desyncs after the first block's mode)",
111111 ));
112112 }
113113 // lane-av1real r11's blindness-audit sweep (`read_intra_frame_mode_info`,
@@ -214,6 +214,8 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
214214 &header.cdef,
215215 &header.loop_filter,
216216 initial_cdfs,
217+ header.tx_mode == TxMode::Select,
218+ header.reduced_tx_set,
217219 )?
218220 } else {
219221 let last_slot = header.ref_frame_idx[0] as usize;
@@ -339,6 +341,9 @@ mod tests {
339341 false,
340342 &ec_av1_syntax::CdefParams::default(),
341343 &ec_av1_syntax::LoopFilterParams::default(),
344+ false,
345+ // Our own encoder always writes `reduced_tx_set: true`.
346+ true,
342347 )
343348 .unwrap();
344349 let via_stream = decode_stream(&encoded.stream).unwrap();
@@ -996,6 +1001,171 @@ mod tests {
9961001 assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg");
9971002 }
9981003
1004+ /// As [`a_real_aomenc_intra_stream_with_deblocking_decodes_pixel_exact`],
1005+ /// but WITHOUT `--enable-tx-size-search=0` -- the one flag every other
1006+ /// recipe in this file pins off specifically to keep the stream on
1007+ /// `TxMode::Largest`. Dropped, aomenc's RD is free to pick
1008+ /// `TxMode::Select` and split some blocks' luma transform below their own
1009+ /// side (spec 5.11.16), which is what this test's `tx_depth_hits()`
1010+ /// counter proves actually happened -- a stream that resolves every
1011+ /// `tx_depth` to 0 would pixel-match by construction (identical to
1012+ /// `TxMode::Largest`) without exercising the new split-TU decode path at
1013+ /// all. Same multi-seed retry shape as
1014+ /// [`a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact`]:
1015+ /// aomenc's own RD is free (with tx-size search on) to also occasionally
1016+ /// pick `allow_screen_content_tools` on this content, a separate,
1017+ /// already-named refusal -- try several seeds, and the first stream that
1018+ /// both decodes AND actually split a transform must be pixel-exact.
1019+ #[test]
1020+ fn a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact() {
1021+ if !have_ffmpeg() {
1022+ eprintln!(
1023+ "SKIP a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact: no ffmpeg"
1024+ );
1025+ return;
1026+ }
1027+ if !have_aomenc() {
1028+ eprintln!(
1029+ "SKIP a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact: no aomenc at {}",
1030+ aomenc_path().display()
1031+ );
1032+ return;
1033+ }
1034+ let (width, height) = (64usize, 64usize);
1035+ let mut refusals = Vec::new();
1036+ for attempt in 0..40u32 {
1037+ let seed = 42 + attempt;
1038+ // A flat gradient never has enough local detail for the RD
1039+ // search to prefer a split transform over the block's own full
1040+ // size -- `mandelbrot`'s fractal boundary gives every attempt
1041+ // (a different `start_scale`) sharp, varied-frequency edges.
1042+ // start_scale 5.0 is the proven first try (decodes pixel-exact
1043+ // AND reads the seven-symbol set-1 tx_type rows on TX8 TUs);
1044+ // later attempts walk down for variety when aomenc's RD flips
1045+ // elsewhere.
1046+ let source = format!(
1047+ "mandelbrot=size=64x64:start_scale={}:rate=25",
1048+ 5.0 - f64::from(attempt) * 0.06
1049+ );
1050+ let y4m = Command::new("ffmpeg")
1051+ .args([
1052+ "-v",
1053+ "error",
1054+ "-f",
1055+ "lavfi",
1056+ "-i",
1057+ &source,
1058+ // Desaturated: chroma stays flat (constant 128) so
1059+ // `uv_mode` always resolves to `DC_PRED` and this
1060+ // decoder's still-unsupported directional-chroma round-2
1061+ // gap never fires, leaving the luma detail alone to
1062+ // drive whether `TxMode::Select`'s RD splits a block.
1063+ "-vf",
1064+ "hue=s=0",
1065+ "-pix_fmt",
1066+ "yuv420p",
1067+ "-t",
1068+ "0.04",
1069+ "-f",
1070+ "yuv4mpegpipe",
1071+ "-",
1072+ ])
1073+ .stdin(Stdio::null())
1074+ .stdout(Stdio::piped())
1075+ .stderr(Stdio::piped())
1076+ .output()
1077+ .expect("ffmpeg failed to run");
1078+ assert!(
1079+ y4m.status.success(),
1080+ "ffmpeg fixture: {}",
1081+ String::from_utf8_lossy(&y4m.stderr)
1082+ );
1083+ let mut child = Command::new(aomenc_path())
1084+ .args([
1085+ "--codec=av1",
1086+ "--passes=1",
1087+ "--end-usage=q",
1088+ "--cq-level=45",
1089+ "--cpu-used=0",
1090+ "--enable-rect-partitions=0",
1091+ "--enable-ab-partitions=0",
1092+ "--enable-1to4-partitions=0",
1093+ "--enable-filter-intra=0",
1094+ // Every non-DC intra tool off: directional chroma and
1095+ // angle deltas are separate round-2 gaps, and DC-only
1096+ // residual is exactly what gives the RD search a reason
1097+ // to split a transform.
1098+ "--enable-smooth-intra=0",
1099+ "--enable-paeth-intra=0",
1100+ "--enable-directional-intra=0",
1101+ "--enable-angle-delta=0",
1102+ "--enable-cdef=0",
1103+ // Loop restoration is a named refusal (aomenc's RD picks
1104+ // Sgrproj on this kind of content) -- off in every recipe
1105+ // whose test targets a different surface.
1106+ "--enable-restoration=0",
1107+ "--max-partition-size=32",
1108+ // 16x16 blocks are what split into the TX8 TUs whose
1109+ // set-1 tx_type rows this gate exists to prove; some
1110+ // scales resolve depth 2 (TX4, a named refusal) and the
1111+ // attempt loop just moves on.
1112+ "--min-partition-size=16",
1113+ "--enable-palette=0",
1114+ "--enable-intrabc=0",
1115+ "--enable-cfl-intra=0",
1116+ "--obu",
1117+ "-o",
1118+ "-",
1119+ "-",
1120+ ])
1121+ .stdin(Stdio::piped())
1122+ .stdout(Stdio::piped())
1123+ .stderr(Stdio::piped())
1124+ .spawn()
1125+ .expect("aomenc failed to start");
1126+ child
1127+ .stdin
1128+ .take()
1129+ .expect("aomenc stdin")
1130+ .write_all(&y4m.stdout)
1131+ .expect("writing y4m to aomenc");
1132+ let out = child.wait_with_output().expect("aomenc failed to run");
1133+ assert!(
1134+ out.status.success(),
1135+ "aomenc refused the fixture: {}",
1136+ String::from_utf8_lossy(&out.stderr)
1137+ );
1138+ let stream = out.stdout;
1139+ let before = decode::tx_depth_hits();
1140+ let frames = match decode_stream(&stream) {
1141+ Ok(frames) => frames,
1142+ Err(e) => {
1143+ refusals.push(format!("seed {seed}: {e}"));
1144+ continue;
1145+ }
1146+ };
1147+ if decode::tx_depth_hits() == before {
1148+ refusals.push(format!(
1149+ "seed {seed}: decoded, but no block resolved a split tx_depth"
1150+ ));
1151+ continue;
1152+ }
1153+ let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1);
1154+ assert_eq!(
1155+ frames[0].y, ffmpeg_frames[0].y,
1156+ "luma vs ffmpeg (seed {seed})"
1157+ );
1158+ assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg (seed {seed})");
1159+ assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (seed {seed})");
1160+ return;
1161+ }
1162+ eprintln!(
1163+ "SKIP a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact: every attempt \
1164+ hit a named refusal or never split a transform:\n{}",
1165+ refusals.join("\n")
1166+ );
1167+ }
1168+
9991169 /// As above, but a multi-frame inter sequence -- `--lag-in-frames=0
10001170 /// --auto-alt-ref=0` keep every non-key frame a simple forward
10011171 /// single-`LAST_FRAME` P frame (no alt-ref/backward reference this
crates/ec-av1/src/transform.rsmodified+27 -0
@@ -314,6 +314,12 @@ pub enum TxType {
314314 AdstDct,
315315 /// DCT down the column axis, ADST across the row axis.
316316 DctAdst,
317+ /// DCT down the column axis, identity across the row axis
318+ /// (`V_DCT` -- `TX_SET_INTRA_1` only).
319+ VDct,
320+ /// Identity down the column axis, DCT across the row axis
321+ /// (`H_DCT` -- `TX_SET_INTRA_1` only).
322+ HDct,
317323 }
318324
319325 impl TxType {
@@ -329,6 +335,24 @@ impl TxType {
329335 }
330336 }
331337
338+ /// The inverse of `Tx_Type_Intra_Inv_Set1`'s CDF symbol order
339+ /// (`av1_ext_tx_inv[EXT_TX_SET_DTT4_IDTX_1DDCT]`, entropymode.h:
340+ /// `{9, 0, 10, 11, 3, 1, 2}` = IDTX, DCT_DCT, V_DCT, H_DCT, ADST_ADST,
341+ /// ADST_DCT, DCT_ADST) -- the seven-type set a `reduced_tx_set == 0`
342+ /// frame's small intra transforms read from.
343+ pub fn from_symbol_set1(t: usize) -> Option<Self> {
344+ match t {
345+ 0 => Some(Self::Idtx),
346+ 1 => Some(Self::DctDct),
347+ 2 => Some(Self::VDct),
348+ 3 => Some(Self::HDct),
349+ 4 => Some(Self::AdstAdst),
350+ 5 => Some(Self::AdstDct),
351+ 6 => Some(Self::DctAdst),
352+ _ => None,
353+ }
354+ }
355+
332356 /// `(row, col)`: the transform `inverse_transform_2d_typed`'s row pass
333357 /// (`htx_tab`, spec/libaom "horizontal") and column pass (`vtx_tab`,
334358 /// "vertical") each run. Read off libaom's `av1_inv_txfm2d.c` `vtx_tab`/
@@ -343,6 +367,9 @@ impl TxType {
343367 Self::AdstAdst => (Adst, Adst),
344368 Self::AdstDct => (Dct, Adst),
345369 Self::DctAdst => (Adst, Dct),
370+ // `av1_inv_txfm2d.c`: V_DCT vtx=DCT htx=IDTX, H_DCT the mirror.
371+ Self::VDct => (Identity, Dct),
372+ Self::HDct => (Dct, Identity),
346373 }
347374 }
348375 }