From ea554628a66e3b27d72ca2b3f44d41ad3efa8421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 19:44:01 +0300 Subject: [PATCH] fix(av1): TX4/TX8 eob_pt CDF is class-split (V_DCT/H_DCT vs 2D) libaom's av1_default_eob_multi{16,64}_cdfs carry a [2] dimension this decoder never read: TX_CLASS_2D vs TX_CLASS_HORIZ/VERT. Read order (all_zero -> tx_type -> eob) verified against decodetxb.c's read_coeffs_txb; the class-1 tables transcribed from token_cdfs.h and checked value-for-value. Wired only where TX_SET_INTRA_1 (the 7-type set carrying V_DCT/H_DCT) is reachable: Luma4/Luma4Set1/Luma8/Luma8Set1. Verified: full ec-av1 suite green (172 passed), and a synthetic 8x8-partition intra stream now exercises H_DCT (tx_type=3) through read_eob's class1 branch without desyncing before an unrelated, pre-existing refusal later in the same frame (small-partition support is a separate gap, not this one). Removed the scratch probe_tx4.rs example used to drive this check. --- crates/ec-av1/examples/probe_tx4.rs | 29 --------------------- crates/ec-av1/src/cdf.rs | 19 ++++++++++++++ crates/ec-av1/src/cdf_state.rs | 40 +++++++++++++++++++++++++++++ crates/ec-av1/src/decode.rs | 18 ++++++++++--- 4 files changed, 73 insertions(+), 33 deletions(-) delete mode 100644 crates/ec-av1/examples/probe_tx4.rs diff --git a/crates/ec-av1/examples/probe_tx4.rs b/crates/ec-av1/examples/probe_tx4.rs deleted file mode 100644 index 1783e866..00000000 --- a/crates/ec-av1/examples/probe_tx4.rs +++ /dev/null @@ -1,29 +0,0 @@ -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 b6947762..e617a413 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -2980,6 +2980,14 @@ pub const EOB_PT_64_LUMA: [u16; 8] = [2374, 2772, 4583, 7276, 12288, 19706, 3276 /// EOB_PT_64_LUMA_Q3, q-context 3: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 121..=255. pub const EOB_PT_64_LUMA_Q3: [u16; 8] = [6307, 7541, 12060, 16358, 22553, 27865, 32768, 0]; +/// The `EOB_PT_64_LUMA` sibling of [`EOB_PT_16_LUMA_CLASS1`] +/// (`av1_default_eob_multi64_cdfs[q][0][1]`), for an 8x8 luma TU whose +/// `tx_type` resolves `V_DCT`/`H_DCT`. +pub const EOB_PT_64_LUMA_CLASS1_Q0: [u16; 8] = [335, 730, 1459, 5494, 8755, 12997, 32768, 0]; +pub const EOB_PT_64_LUMA_CLASS1_Q1: [u16; 8] = [401, 605, 1029, 2563, 5845, 12626, 32768, 0]; +pub const EOB_PT_64_LUMA_CLASS1: [u16; 8] = [497, 810, 1315, 3000, 7004, 15641, 32768, 0]; +pub const EOB_PT_64_LUMA_CLASS1_Q3: [u16; 8] = [1289, 2320, 3971, 7926, 14153, 24291, 32768, 0]; + /// EOB_PT_16_CHROMA_Q0, q-context 0: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 0..=20. pub const EOB_PT_16_CHROMA_Q0: [u16; 6] = [3247, 4950, 9688, 14563, 32768, 0]; @@ -3963,6 +3971,17 @@ 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]; +/// The `TX_CLASS_HORIZ`/`TX_CLASS_VERT` (non-2D-scan) sibling of +/// [`EOB_PT_16_LUMA`] (`av1_default_eob_multi16_cdfs[q][0][1]`) -- read +/// instead of the 2D table whenever a 4x4 luma TU's `tx_type` resolves to +/// `V_DCT`/`H_DCT` (lane-av1tx4 r5: the class dimension libaom always keeps, +/// this crate never carried at all until a real aomdec trace showed the +/// desync starting exactly at this read). +pub const EOB_PT_16_LUMA_CLASS1_Q0: [u16; 6] = [370, 671, 1883, 4471, 32768, 0]; +pub const EOB_PT_16_LUMA_CLASS1_Q1: [u16; 6] = [513, 765, 1859, 6339, 32768, 0]; +pub const EOB_PT_16_LUMA_CLASS1: [u16; 6] = [716, 1105, 2646, 10056, 32768, 0]; +pub const EOB_PT_16_LUMA_CLASS1_Q3: [u16; 6] = [1222, 2074, 4783, 15410, 32768, 0]; + // EOB_EXTRA_LUMA_4 pub const EOB_EXTRA_LUMA_4_Q0: [[u16; 3]; 9] = [ [16961, 32768, 0], diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 9da2265d..2b6d332e 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -90,6 +90,11 @@ pub(crate) struct TxbTables<'a> { pub txb_skip: &'a mut [[u16; 3]], /// The end-of-block group, whose alphabet the transform size sets. pub eob_pt: &'a mut [u16], + /// `eob_pt`'s `TX_CLASS_HORIZ`/`TX_CLASS_VERT` sibling table (r5): read + /// instead of `eob_pt` once this TU's `tx_type` is known to be + /// `V_DCT`/`H_DCT`. `None` for any size/set whose `tx_type` alphabet + /// cannot produce those two symbols (chroma, and every inter set). + pub eob_pt_class1: Option<&'a mut [u16]>, /// The top bit of the offset inside that group. pub eob_extra: &'a mut [[u16; 3]; 9], /// A coefficient's base level. @@ -152,6 +157,11 @@ pub(crate) struct Cdfs { 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], + /// `eob_pt_16_luma`'s `TX_CLASS_HORIZ`/`TX_CLASS_VERT` sibling, read + /// instead whenever the TU's `tx_type` is `V_DCT`/`H_DCT` (r5). + pub eob_pt_16_luma_class1: [u16; 6], + /// `eob_pt_64_luma`'s class-1 sibling (r5), see `eob_pt_16_luma_class1`. + pub eob_pt_64_luma_class1: [u16; 8], /// 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. @@ -429,6 +439,8 @@ impl Cdfs { reset1(&mut self.eob_pt_64_chroma); reset1(&mut self.eob_pt_16_chroma); reset1(&mut self.eob_pt_16_luma); + reset1(&mut self.eob_pt_16_luma_class1); + reset1(&mut self.eob_pt_64_luma_class1); reset2(&mut self.eob_extra_luma_16); reset2(&mut self.eob_extra_luma_8); reset2(&mut self.eob_extra_luma_4); @@ -587,6 +599,20 @@ impl Cdfs { cdf::EOB_PT_16_LUMA, cdf::EOB_PT_16_LUMA_Q3, ), + eob_pt_16_luma_class1: pick( + q_ctx, + cdf::EOB_PT_16_LUMA_CLASS1_Q0, + cdf::EOB_PT_16_LUMA_CLASS1_Q1, + cdf::EOB_PT_16_LUMA_CLASS1, + cdf::EOB_PT_16_LUMA_CLASS1_Q3, + ), + eob_pt_64_luma_class1: pick( + q_ctx, + cdf::EOB_PT_64_LUMA_CLASS1_Q0, + cdf::EOB_PT_64_LUMA_CLASS1_Q1, + cdf::EOB_PT_64_LUMA_CLASS1, + cdf::EOB_PT_64_LUMA_CLASS1_Q3, + ), eob_extra_luma_16: pick( q_ctx, cdf::EOB_EXTRA_LUMA_16_Q0, @@ -924,6 +950,7 @@ impl Cdfs { br: &mut self.br_luma_32, dc_sign: &mut self.dc_sign_luma, tx_type: None, + eob_pt_class1: None, }, TxbSet::Luma32Inter => TxbTables { side: 32, @@ -935,6 +962,7 @@ impl Cdfs { br: &mut self.br_luma_32, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.inter_tx_type_32.as_mut_slice()), + eob_pt_class1: None, }, TxbSet::Luma64 => TxbTables { side: 32, @@ -946,6 +974,7 @@ impl Cdfs { br: &mut self.br_luma_32, dc_sign: &mut self.dc_sign_luma, tx_type: None, + eob_pt_class1: None, }, TxbSet::Luma16 => TxbTables { side: 16, @@ -957,6 +986,7 @@ impl Cdfs { br: &mut self.br_luma_16, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()), + eob_pt_class1: None, }, TxbSet::Luma16Inter => TxbTables { side: 16, @@ -968,6 +998,7 @@ impl Cdfs { br: &mut self.br_luma_16, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.inter_tx_type_16.as_mut_slice()), + eob_pt_class1: None, }, TxbSet::Luma8 => TxbTables { side: 8, @@ -979,6 +1010,7 @@ impl Cdfs { br: &mut self.br_luma_8, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()), + eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1), }, TxbSet::Luma8Set1 => TxbTables { side: 8, @@ -990,6 +1022,7 @@ impl Cdfs { br: &mut self.br_luma_8, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.intra_tx_type_8_set1[mode].as_mut_slice()), + eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1), }, TxbSet::Luma4 => TxbTables { side: 4, @@ -1001,6 +1034,7 @@ impl Cdfs { 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()), + eob_pt_class1: Some(&mut self.eob_pt_16_luma_class1), }, TxbSet::Luma4Set1 => TxbTables { side: 4, @@ -1012,6 +1046,7 @@ impl Cdfs { 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()), + eob_pt_class1: Some(&mut self.eob_pt_16_luma_class1), }, TxbSet::Luma8Inter => TxbTables { side: 8, @@ -1023,6 +1058,7 @@ impl Cdfs { br: &mut self.br_luma_8, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.inter_tx_type_8.as_mut_slice()), + eob_pt_class1: None, }, TxbSet::Chroma8 => TxbTables { side: 8, @@ -1034,6 +1070,7 @@ impl Cdfs { br: &mut self.br_chroma_8, dc_sign: &mut self.dc_sign_chroma, tx_type: None, + eob_pt_class1: None, }, TxbSet::Chroma4 => TxbTables { side: 4, @@ -1045,6 +1082,7 @@ impl Cdfs { br: &mut self.br_chroma_4, dc_sign: &mut self.dc_sign_chroma, tx_type: None, + eob_pt_class1: None, }, TxbSet::Chroma16 => TxbTables { side: 16, @@ -1056,6 +1094,7 @@ impl Cdfs { br: &mut self.br_chroma_16, dc_sign: &mut self.dc_sign_chroma, tx_type: None, + eob_pt_class1: None, }, TxbSet::Chroma32 => TxbTables { side: 32, @@ -1067,6 +1106,7 @@ impl Cdfs { br: &mut self.br_chroma_32, dc_sign: &mut self.dc_sign_chroma, tx_type: None, + eob_pt_class1: None, }, } } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 99a33519..d8048f81 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -302,12 +302,22 @@ fn read_golomb(dec: &mut SymbolDecoder) -> Result { /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of /// [`crate::tile`]'s `write_eob`. -fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables) -> usize { +fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) -> usize { const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513]; const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; let trace = std::env::var_os("EC_AV1_TRACE").is_some(); - let group = dec.symbol(coding.eob_pt) + 1; + // libaom's `eob_flag_cdf*` tables carry a second dimension the 2D scan + // never touches: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` (`V_DCT`/`H_DCT`) adapt + // a wholly separate CDF from every other tx_type (`decodetxb.c`'s + // `eob_multi_ctx`). Missing that split desynced the very first + // `V_DCT`/`H_DCT` TU this decoder ever produced (lane-av1tx4 r5, caught + // against a real aomdec trace). + let eob_pt: &mut [u16] = match (class, coding.eob_pt_class1.as_deref_mut()) { + (TxClass::TwoD, _) | (_, None) => coding.eob_pt, + (_, Some(class1)) => class1, + }; + let group = dec.symbol(eob_pt) + 1; if trace { eprintln!("TRACE eob_pt value={group}"); } @@ -372,11 +382,11 @@ fn read_coeffs( .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; } - let eob = read_eob(dec, coding); + let class = TxClass::of(tx_type); + let eob = read_eob(dec, coding, class); if trace { eprintln!("TRACE eob value={eob}"); } - let class = TxClass::of(tx_type); let class_scan; let scan: &[u16] = if class == TxClass::TwoD { scan