forge
Explore
Log in

Don't you dare go Hollow

TX4/TX8 eob_pt CDF is class-split (V_DCT/H_DCT vs 2D)

Browse files at this commit

ea554628a66e3b27d72ca2b3f44d41ad3efa8421Ahmet Kaan GümüşParent: 700543b
  • fix
    • TX4/TX8 eob_pt CDF is class-split (V_DCT/H_DCT vs 2D)
Show full message
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.rsdeleted+0 -29
@@ -1,29 +0,0 @@
1-use std::io::Write;
2-use std::process::{Command, Stdio};
3-
4-fn main() {
5- let path = std::env::args().nth(1).expect("obu path");
6- let stream = std::fs::read(&path).expect("read");
7- let frames = match ec_av1::stream::decode_stream(&stream) {
8- Ok(f) => f,
9- Err(e) => {
10- println!("REFUSED: {e}");
11- std::process::exit(1);
12- }
13- };
14- let mut child = Command::new("ffmpeg")
15- .args([
16- "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-",
17- ])
18- .stdin(Stdio::piped())
19- .stdout(Stdio::piped())
20- .spawn()
21- .expect("ffmpeg");
22- child.stdin.take().unwrap().write_all(&stream).unwrap();
23- let out = child.wait_with_output().unwrap().stdout;
24- let f = &frames[0];
25- let mut ours = f.y.clone();
26- ours.extend_from_slice(&f.u);
27- ours.extend_from_slice(&f.v);
28- println!("match={}", ours == out[..ours.len()]);
29-}
crates/ec-av1/src/cdf.rsmodified+19 -0
@@ -2980,6 +2980,14 @@ pub const EOB_PT_64_LUMA: [u16; 8] = [2374, 2772, 4583, 7276, 12288, 19706, 3276
29802980 /// EOB_PT_64_LUMA_Q3, q-context 3: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 121..=255.
29812981 pub const EOB_PT_64_LUMA_Q3: [u16; 8] = [6307, 7541, 12060, 16358, 22553, 27865, 32768, 0];
29822982
2983+/// The `EOB_PT_64_LUMA` sibling of [`EOB_PT_16_LUMA_CLASS1`]
2984+/// (`av1_default_eob_multi64_cdfs[q][0][1]`), for an 8x8 luma TU whose
2985+/// `tx_type` resolves `V_DCT`/`H_DCT`.
2986+pub const EOB_PT_64_LUMA_CLASS1_Q0: [u16; 8] = [335, 730, 1459, 5494, 8755, 12997, 32768, 0];
2987+pub const EOB_PT_64_LUMA_CLASS1_Q1: [u16; 8] = [401, 605, 1029, 2563, 5845, 12626, 32768, 0];
2988+pub const EOB_PT_64_LUMA_CLASS1: [u16; 8] = [497, 810, 1315, 3000, 7004, 15641, 32768, 0];
2989+pub const EOB_PT_64_LUMA_CLASS1_Q3: [u16; 8] = [1289, 2320, 3971, 7926, 14153, 24291, 32768, 0];
2990+
29832991 /// EOB_PT_16_CHROMA_Q0, q-context 0: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 0..=20.
29842992 pub const EOB_PT_16_CHROMA_Q0: [u16; 6] = [3247, 4950, 9688, 14563, 32768, 0];
29852993
@@ -3963,6 +3971,17 @@ pub const EOB_PT_16_LUMA_Q1: [u16; 6] = [2125, 2551, 5165, 8946, 32768, 0];
39633971 pub const EOB_PT_16_LUMA: [u16; 6] = [4016, 4897, 8881, 14968, 32768, 0];
39643972 pub const EOB_PT_16_LUMA_Q3: [u16; 6] = [6708, 8958, 14746, 22133, 32768, 0];
39653973
3974+/// The `TX_CLASS_HORIZ`/`TX_CLASS_VERT` (non-2D-scan) sibling of
3975+/// [`EOB_PT_16_LUMA`] (`av1_default_eob_multi16_cdfs[q][0][1]`) -- read
3976+/// instead of the 2D table whenever a 4x4 luma TU's `tx_type` resolves to
3977+/// `V_DCT`/`H_DCT` (lane-av1tx4 r5: the class dimension libaom always keeps,
3978+/// this crate never carried at all until a real aomdec trace showed the
3979+/// desync starting exactly at this read).
3980+pub const EOB_PT_16_LUMA_CLASS1_Q0: [u16; 6] = [370, 671, 1883, 4471, 32768, 0];
3981+pub const EOB_PT_16_LUMA_CLASS1_Q1: [u16; 6] = [513, 765, 1859, 6339, 32768, 0];
3982+pub const EOB_PT_16_LUMA_CLASS1: [u16; 6] = [716, 1105, 2646, 10056, 32768, 0];
3983+pub const EOB_PT_16_LUMA_CLASS1_Q3: [u16; 6] = [1222, 2074, 4783, 15410, 32768, 0];
3984+
39663985 // EOB_EXTRA_LUMA_4
39673986 pub const EOB_EXTRA_LUMA_4_Q0: [[u16; 3]; 9] = [
39683987 [16961, 32768, 0],
crates/ec-av1/src/cdf_state.rsmodified+40 -0
@@ -90,6 +90,11 @@ pub(crate) struct TxbTables<'a> {
9090 pub txb_skip: &'a mut [[u16; 3]],
9191 /// The end-of-block group, whose alphabet the transform size sets.
9292 pub eob_pt: &'a mut [u16],
93+ /// `eob_pt`'s `TX_CLASS_HORIZ`/`TX_CLASS_VERT` sibling table (r5): read
94+ /// instead of `eob_pt` once this TU's `tx_type` is known to be
95+ /// `V_DCT`/`H_DCT`. `None` for any size/set whose `tx_type` alphabet
96+ /// cannot produce those two symbols (chroma, and every inter set).
97+ pub eob_pt_class1: Option<&'a mut [u16]>,
9398 /// The top bit of the offset inside that group.
9499 pub eob_extra: &'a mut [[u16; 3]; 9],
95100 /// A coefficient's base level.
@@ -152,6 +157,11 @@ pub(crate) struct Cdfs {
152157 pub eob_pt_16_chroma: [u16; 6],
153158 /// The end-of-block group of a 4x4 luma transform (lane-av1tx4).
154159 pub eob_pt_16_luma: [u16; 6],
160+ /// `eob_pt_16_luma`'s `TX_CLASS_HORIZ`/`TX_CLASS_VERT` sibling, read
161+ /// instead whenever the TU's `tx_type` is `V_DCT`/`H_DCT` (r5).
162+ pub eob_pt_16_luma_class1: [u16; 6],
163+ /// `eob_pt_64_luma`'s class-1 sibling (r5), see `eob_pt_16_luma_class1`.
164+ pub eob_pt_64_luma_class1: [u16; 8],
155165 /// The end-of-block offset bit of a 16x16 luma transform.
156166 pub eob_extra_luma_16: [[u16; 3]; 9],
157167 /// The same, for an 8x8 luma transform.
@@ -429,6 +439,8 @@ impl Cdfs {
429439 reset1(&mut self.eob_pt_64_chroma);
430440 reset1(&mut self.eob_pt_16_chroma);
431441 reset1(&mut self.eob_pt_16_luma);
442+ reset1(&mut self.eob_pt_16_luma_class1);
443+ reset1(&mut self.eob_pt_64_luma_class1);
432444 reset2(&mut self.eob_extra_luma_16);
433445 reset2(&mut self.eob_extra_luma_8);
434446 reset2(&mut self.eob_extra_luma_4);
@@ -587,6 +599,20 @@ impl Cdfs {
587599 cdf::EOB_PT_16_LUMA,
588600 cdf::EOB_PT_16_LUMA_Q3,
589601 ),
602+ eob_pt_16_luma_class1: pick(
603+ q_ctx,
604+ cdf::EOB_PT_16_LUMA_CLASS1_Q0,
605+ cdf::EOB_PT_16_LUMA_CLASS1_Q1,
606+ cdf::EOB_PT_16_LUMA_CLASS1,
607+ cdf::EOB_PT_16_LUMA_CLASS1_Q3,
608+ ),
609+ eob_pt_64_luma_class1: pick(
610+ q_ctx,
611+ cdf::EOB_PT_64_LUMA_CLASS1_Q0,
612+ cdf::EOB_PT_64_LUMA_CLASS1_Q1,
613+ cdf::EOB_PT_64_LUMA_CLASS1,
614+ cdf::EOB_PT_64_LUMA_CLASS1_Q3,
615+ ),
590616 eob_extra_luma_16: pick(
591617 q_ctx,
592618 cdf::EOB_EXTRA_LUMA_16_Q0,
@@ -924,6 +950,7 @@ impl Cdfs {
924950 br: &mut self.br_luma_32,
925951 dc_sign: &mut self.dc_sign_luma,
926952 tx_type: None,
953+ eob_pt_class1: None,
927954 },
928955 TxbSet::Luma32Inter => TxbTables {
929956 side: 32,
@@ -935,6 +962,7 @@ impl Cdfs {
935962 br: &mut self.br_luma_32,
936963 dc_sign: &mut self.dc_sign_luma,
937964 tx_type: Some(self.inter_tx_type_32.as_mut_slice()),
965+ eob_pt_class1: None,
938966 },
939967 TxbSet::Luma64 => TxbTables {
940968 side: 32,
@@ -946,6 +974,7 @@ impl Cdfs {
946974 br: &mut self.br_luma_32,
947975 dc_sign: &mut self.dc_sign_luma,
948976 tx_type: None,
977+ eob_pt_class1: None,
949978 },
950979 TxbSet::Luma16 => TxbTables {
951980 side: 16,
@@ -957,6 +986,7 @@ impl Cdfs {
957986 br: &mut self.br_luma_16,
958987 dc_sign: &mut self.dc_sign_luma,
959988 tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()),
989+ eob_pt_class1: None,
960990 },
961991 TxbSet::Luma16Inter => TxbTables {
962992 side: 16,
@@ -968,6 +998,7 @@ impl Cdfs {
968998 br: &mut self.br_luma_16,
969999 dc_sign: &mut self.dc_sign_luma,
9701000 tx_type: Some(self.inter_tx_type_16.as_mut_slice()),
1001+ eob_pt_class1: None,
9711002 },
9721003 TxbSet::Luma8 => TxbTables {
9731004 side: 8,
@@ -979,6 +1010,7 @@ impl Cdfs {
9791010 br: &mut self.br_luma_8,
9801011 dc_sign: &mut self.dc_sign_luma,
9811012 tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()),
1013+ eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1),
9821014 },
9831015 TxbSet::Luma8Set1 => TxbTables {
9841016 side: 8,
@@ -990,6 +1022,7 @@ impl Cdfs {
9901022 br: &mut self.br_luma_8,
9911023 dc_sign: &mut self.dc_sign_luma,
9921024 tx_type: Some(self.intra_tx_type_8_set1[mode].as_mut_slice()),
1025+ eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1),
9931026 },
9941027 TxbSet::Luma4 => TxbTables {
9951028 side: 4,
@@ -1001,6 +1034,7 @@ impl Cdfs {
10011034 br: &mut self.br_luma_4,
10021035 dc_sign: &mut self.dc_sign_luma,
10031036 tx_type: Some(self.intra_tx_type_4[mode].as_mut_slice()),
1037+ eob_pt_class1: Some(&mut self.eob_pt_16_luma_class1),
10041038 },
10051039 TxbSet::Luma4Set1 => TxbTables {
10061040 side: 4,
@@ -1012,6 +1046,7 @@ impl Cdfs {
10121046 br: &mut self.br_luma_4,
10131047 dc_sign: &mut self.dc_sign_luma,
10141048 tx_type: Some(self.intra_tx_type_4_set1[mode].as_mut_slice()),
1049+ eob_pt_class1: Some(&mut self.eob_pt_16_luma_class1),
10151050 },
10161051 TxbSet::Luma8Inter => TxbTables {
10171052 side: 8,
@@ -1023,6 +1058,7 @@ impl Cdfs {
10231058 br: &mut self.br_luma_8,
10241059 dc_sign: &mut self.dc_sign_luma,
10251060 tx_type: Some(self.inter_tx_type_8.as_mut_slice()),
1061+ eob_pt_class1: None,
10261062 },
10271063 TxbSet::Chroma8 => TxbTables {
10281064 side: 8,
@@ -1034,6 +1070,7 @@ impl Cdfs {
10341070 br: &mut self.br_chroma_8,
10351071 dc_sign: &mut self.dc_sign_chroma,
10361072 tx_type: None,
1073+ eob_pt_class1: None,
10371074 },
10381075 TxbSet::Chroma4 => TxbTables {
10391076 side: 4,
@@ -1045,6 +1082,7 @@ impl Cdfs {
10451082 br: &mut self.br_chroma_4,
10461083 dc_sign: &mut self.dc_sign_chroma,
10471084 tx_type: None,
1085+ eob_pt_class1: None,
10481086 },
10491087 TxbSet::Chroma16 => TxbTables {
10501088 side: 16,
@@ -1056,6 +1094,7 @@ impl Cdfs {
10561094 br: &mut self.br_chroma_16,
10571095 dc_sign: &mut self.dc_sign_chroma,
10581096 tx_type: None,
1097+ eob_pt_class1: None,
10591098 },
10601099 TxbSet::Chroma32 => TxbTables {
10611100 side: 32,
@@ -1067,6 +1106,7 @@ impl Cdfs {
10671106 br: &mut self.br_chroma_32,
10681107 dc_sign: &mut self.dc_sign_chroma,
10691108 tx_type: None,
1109+ eob_pt_class1: None,
10701110 },
10711111 }
10721112 }
crates/ec-av1/src/decode.rsmodified+14 -4
@@ -302,12 +302,22 @@ fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> {
302302
303303 /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of
304304 /// [`crate::tile`]'s `write_eob`.
305-fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables) -> usize {
305+fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) -> usize {
306306 const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513];
307307 const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
308308
309309 let trace = std::env::var_os("EC_AV1_TRACE").is_some();
310- let group = dec.symbol(coding.eob_pt) + 1;
310+ // libaom's `eob_flag_cdf*` tables carry a second dimension the 2D scan
311+ // never touches: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` (`V_DCT`/`H_DCT`) adapt
312+ // a wholly separate CDF from every other tx_type (`decodetxb.c`'s
313+ // `eob_multi_ctx`). Missing that split desynced the very first
314+ // `V_DCT`/`H_DCT` TU this decoder ever produced (lane-av1tx4 r5, caught
315+ // against a real aomdec trace).
316+ let eob_pt: &mut [u16] = match (class, coding.eob_pt_class1.as_deref_mut()) {
317+ (TxClass::TwoD, _) | (_, None) => coding.eob_pt,
318+ (_, Some(class1)) => class1,
319+ };
320+ let group = dec.symbol(eob_pt) + 1;
311321 if trace {
312322 eprintln!("TRACE eob_pt value={group}");
313323 }
@@ -372,11 +382,11 @@ fn read_coeffs(
372382 .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?;
373383 }
374384
375- let eob = read_eob(dec, coding);
385+ let class = TxClass::of(tx_type);
386+ let eob = read_eob(dec, coding, class);
376387 if trace {
377388 eprintln!("TRACE eob value={eob}");
378389 }
379- let class = TxClass::of(tx_type);
380390 let class_scan;
381391 let scan: &[u16] = if class == TxClass::TwoD {
382392 scan