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
2980 /// EOB_PT_64_LUMA_Q3, q-context 3: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 121..=255.2980 /// EOB_PT_64_LUMA_Q3, q-context 3: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 121..=255.
2981 pub const EOB_PT_64_LUMA_Q3: [u16; 8] = [6307, 7541, 12060, 16358, 22553, 27865, 32768, 0];2981 pub const EOB_PT_64_LUMA_Q3: [u16; 8] = [6307, 7541, 12060, 16358, 22553, 27865, 32768, 0];
2982 2982
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+
2983 /// EOB_PT_16_CHROMA_Q0, q-context 0: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 0..=20.2991 /// EOB_PT_16_CHROMA_Q0, q-context 0: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 0..=20.
2984 pub const EOB_PT_16_CHROMA_Q0: [u16; 6] = [3247, 4950, 9688, 14563, 32768, 0];2992 pub const EOB_PT_16_CHROMA_Q0: [u16; 6] = [3247, 4950, 9688, 14563, 32768, 0];
2985 2993
@@ -3963,6 +3971,17 @@ pub const EOB_PT_16_LUMA_Q1: [u16; 6] = [2125, 2551, 5165, 8946, 32768, 0];
3963 pub const EOB_PT_16_LUMA: [u16; 6] = [4016, 4897, 8881, 14968, 32768, 0];3971 pub const EOB_PT_16_LUMA: [u16; 6] = [4016, 4897, 8881, 14968, 32768, 0];
3964 pub const EOB_PT_16_LUMA_Q3: [u16; 6] = [6708, 8958, 14746, 22133, 32768, 0];3972 pub const EOB_PT_16_LUMA_Q3: [u16; 6] = [6708, 8958, 14746, 22133, 32768, 0];
3965 3973
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+
3966 // EOB_EXTRA_LUMA_43985 // EOB_EXTRA_LUMA_4
3967 pub const EOB_EXTRA_LUMA_4_Q0: [[u16; 3]; 9] = [3986 pub const EOB_EXTRA_LUMA_4_Q0: [[u16; 3]; 9] = [
3968 [16961, 32768, 0],3987 [16961, 32768, 0],
crates/ec-av1/src/cdf_state.rsmodified+40 -0
@@ -90,6 +90,11 @@ pub(crate) struct TxbTables<'a> {
90 pub txb_skip: &'a mut [[u16; 3]],90 pub txb_skip: &'a mut [[u16; 3]],
91 /// The end-of-block group, whose alphabet the transform size sets.91 /// The end-of-block group, whose alphabet the transform size sets.
92 pub eob_pt: &'a mut [u16],92 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]>,
93 /// The top bit of the offset inside that group.98 /// The top bit of the offset inside that group.
94 pub eob_extra: &'a mut [[u16; 3]; 9],99 pub eob_extra: &'a mut [[u16; 3]; 9],
95 /// A coefficient's base level.100 /// A coefficient's base level.
@@ -152,6 +157,11 @@ pub(crate) struct Cdfs {
152 pub eob_pt_16_chroma: [u16; 6],157 pub eob_pt_16_chroma: [u16; 6],
153 /// The end-of-block group of a 4x4 luma transform (lane-av1tx4).158 /// The end-of-block group of a 4x4 luma transform (lane-av1tx4).
154 pub eob_pt_16_luma: [u16; 6],159 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],
155 /// The end-of-block offset bit of a 16x16 luma transform.165 /// The end-of-block offset bit of a 16x16 luma transform.
156 pub eob_extra_luma_16: [[u16; 3]; 9],166 pub eob_extra_luma_16: [[u16; 3]; 9],
157 /// The same, for an 8x8 luma transform.167 /// The same, for an 8x8 luma transform.
@@ -429,6 +439,8 @@ impl Cdfs {
429 reset1(&mut self.eob_pt_64_chroma);439 reset1(&mut self.eob_pt_64_chroma);
430 reset1(&mut self.eob_pt_16_chroma);440 reset1(&mut self.eob_pt_16_chroma);
431 reset1(&mut self.eob_pt_16_luma);441 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);
432 reset2(&mut self.eob_extra_luma_16);444 reset2(&mut self.eob_extra_luma_16);
433 reset2(&mut self.eob_extra_luma_8);445 reset2(&mut self.eob_extra_luma_8);
434 reset2(&mut self.eob_extra_luma_4);446 reset2(&mut self.eob_extra_luma_4);
@@ -587,6 +599,20 @@ impl Cdfs {
587 cdf::EOB_PT_16_LUMA,599 cdf::EOB_PT_16_LUMA,
588 cdf::EOB_PT_16_LUMA_Q3,600 cdf::EOB_PT_16_LUMA_Q3,
589 ),601 ),
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+ ),
590 eob_extra_luma_16: pick(616 eob_extra_luma_16: pick(
591 q_ctx,617 q_ctx,
592 cdf::EOB_EXTRA_LUMA_16_Q0,618 cdf::EOB_EXTRA_LUMA_16_Q0,
@@ -924,6 +950,7 @@ impl Cdfs {
924 br: &mut self.br_luma_32,950 br: &mut self.br_luma_32,
925 dc_sign: &mut self.dc_sign_luma,951 dc_sign: &mut self.dc_sign_luma,
926 tx_type: None,952 tx_type: None,
953+ eob_pt_class1: None,
927 },954 },
928 TxbSet::Luma32Inter => TxbTables {955 TxbSet::Luma32Inter => TxbTables {
929 side: 32,956 side: 32,
@@ -935,6 +962,7 @@ impl Cdfs {
935 br: &mut self.br_luma_32,962 br: &mut self.br_luma_32,
936 dc_sign: &mut self.dc_sign_luma,963 dc_sign: &mut self.dc_sign_luma,
937 tx_type: Some(self.inter_tx_type_32.as_mut_slice()),964 tx_type: Some(self.inter_tx_type_32.as_mut_slice()),
965+ eob_pt_class1: None,
938 },966 },
939 TxbSet::Luma64 => TxbTables {967 TxbSet::Luma64 => TxbTables {
940 side: 32,968 side: 32,
@@ -946,6 +974,7 @@ impl Cdfs {
946 br: &mut self.br_luma_32,974 br: &mut self.br_luma_32,
947 dc_sign: &mut self.dc_sign_luma,975 dc_sign: &mut self.dc_sign_luma,
948 tx_type: None,976 tx_type: None,
977+ eob_pt_class1: None,
949 },978 },
950 TxbSet::Luma16 => TxbTables {979 TxbSet::Luma16 => TxbTables {
951 side: 16,980 side: 16,
@@ -957,6 +986,7 @@ impl Cdfs {
957 br: &mut self.br_luma_16,986 br: &mut self.br_luma_16,
958 dc_sign: &mut self.dc_sign_luma,987 dc_sign: &mut self.dc_sign_luma,
959 tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()),988 tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()),
989+ eob_pt_class1: None,
960 },990 },
961 TxbSet::Luma16Inter => TxbTables {991 TxbSet::Luma16Inter => TxbTables {
962 side: 16,992 side: 16,
@@ -968,6 +998,7 @@ impl Cdfs {
968 br: &mut self.br_luma_16,998 br: &mut self.br_luma_16,
969 dc_sign: &mut self.dc_sign_luma,999 dc_sign: &mut self.dc_sign_luma,
970 tx_type: Some(self.inter_tx_type_16.as_mut_slice()),1000 tx_type: Some(self.inter_tx_type_16.as_mut_slice()),
1001+ eob_pt_class1: None,
971 },1002 },
972 TxbSet::Luma8 => TxbTables {1003 TxbSet::Luma8 => TxbTables {
973 side: 8,1004 side: 8,
@@ -979,6 +1010,7 @@ impl Cdfs {
979 br: &mut self.br_luma_8,1010 br: &mut self.br_luma_8,
980 dc_sign: &mut self.dc_sign_luma,1011 dc_sign: &mut self.dc_sign_luma,
981 tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()),1012 tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()),
1013+ eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1),
982 },1014 },
983 TxbSet::Luma8Set1 => TxbTables {1015 TxbSet::Luma8Set1 => TxbTables {
984 side: 8,1016 side: 8,
@@ -990,6 +1022,7 @@ impl Cdfs {
990 br: &mut self.br_luma_8,1022 br: &mut self.br_luma_8,
991 dc_sign: &mut self.dc_sign_luma,1023 dc_sign: &mut self.dc_sign_luma,
992 tx_type: Some(self.intra_tx_type_8_set1[mode].as_mut_slice()),1024 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),
993 },1026 },
994 TxbSet::Luma4 => TxbTables {1027 TxbSet::Luma4 => TxbTables {
995 side: 4,1028 side: 4,
@@ -1001,6 +1034,7 @@ impl Cdfs {
1001 br: &mut self.br_luma_4,1034 br: &mut self.br_luma_4,
1002 dc_sign: &mut self.dc_sign_luma,1035 dc_sign: &mut self.dc_sign_luma,
1003 tx_type: Some(self.intra_tx_type_4[mode].as_mut_slice()),1036 tx_type: Some(self.intra_tx_type_4[mode].as_mut_slice()),
1037+ eob_pt_class1: Some(&mut self.eob_pt_16_luma_class1),
1004 },1038 },
1005 TxbSet::Luma4Set1 => TxbTables {1039 TxbSet::Luma4Set1 => TxbTables {
1006 side: 4,1040 side: 4,
@@ -1012,6 +1046,7 @@ impl Cdfs {
1012 br: &mut self.br_luma_4,1046 br: &mut self.br_luma_4,
1013 dc_sign: &mut self.dc_sign_luma,1047 dc_sign: &mut self.dc_sign_luma,
1014 tx_type: Some(self.intra_tx_type_4_set1[mode].as_mut_slice()),1048 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),
1015 },1050 },
1016 TxbSet::Luma8Inter => TxbTables {1051 TxbSet::Luma8Inter => TxbTables {
1017 side: 8,1052 side: 8,
@@ -1023,6 +1058,7 @@ impl Cdfs {
1023 br: &mut self.br_luma_8,1058 br: &mut self.br_luma_8,
1024 dc_sign: &mut self.dc_sign_luma,1059 dc_sign: &mut self.dc_sign_luma,
1025 tx_type: Some(self.inter_tx_type_8.as_mut_slice()),1060 tx_type: Some(self.inter_tx_type_8.as_mut_slice()),
1061+ eob_pt_class1: None,
1026 },1062 },
1027 TxbSet::Chroma8 => TxbTables {1063 TxbSet::Chroma8 => TxbTables {
1028 side: 8,1064 side: 8,
@@ -1034,6 +1070,7 @@ impl Cdfs {
1034 br: &mut self.br_chroma_8,1070 br: &mut self.br_chroma_8,
1035 dc_sign: &mut self.dc_sign_chroma,1071 dc_sign: &mut self.dc_sign_chroma,
1036 tx_type: None,1072 tx_type: None,
1073+ eob_pt_class1: None,
1037 },1074 },
1038 TxbSet::Chroma4 => TxbTables {1075 TxbSet::Chroma4 => TxbTables {
1039 side: 4,1076 side: 4,
@@ -1045,6 +1082,7 @@ impl Cdfs {
1045 br: &mut self.br_chroma_4,1082 br: &mut self.br_chroma_4,
1046 dc_sign: &mut self.dc_sign_chroma,1083 dc_sign: &mut self.dc_sign_chroma,
1047 tx_type: None,1084 tx_type: None,
1085+ eob_pt_class1: None,
1048 },1086 },
1049 TxbSet::Chroma16 => TxbTables {1087 TxbSet::Chroma16 => TxbTables {
1050 side: 16,1088 side: 16,
@@ -1056,6 +1094,7 @@ impl Cdfs {
1056 br: &mut self.br_chroma_16,1094 br: &mut self.br_chroma_16,
1057 dc_sign: &mut self.dc_sign_chroma,1095 dc_sign: &mut self.dc_sign_chroma,
1058 tx_type: None,1096 tx_type: None,
1097+ eob_pt_class1: None,
1059 },1098 },
1060 TxbSet::Chroma32 => TxbTables {1099 TxbSet::Chroma32 => TxbTables {
1061 side: 32,1100 side: 32,
@@ -1067,6 +1106,7 @@ impl Cdfs {
1067 br: &mut self.br_chroma_32,1106 br: &mut self.br_chroma_32,
1068 dc_sign: &mut self.dc_sign_chroma,1107 dc_sign: &mut self.dc_sign_chroma,
1069 tx_type: None,1108 tx_type: None,
1109+ eob_pt_class1: None,
1070 },1110 },
1071 }1111 }
1072 }1112 }
crates/ec-av1/src/decode.rsmodified+14 -4
@@ -302,12 +302,22 @@ fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> {
302 302
303 /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of303 /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of
304 /// [`crate::tile`]'s `write_eob`.304 /// [`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 {
306 const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513];306 const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513];
307 const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];307 const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
308 308
309 let trace = std::env::var_os("EC_AV1_TRACE").is_some();309 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;
311 if trace {321 if trace {
312 eprintln!("TRACE eob_pt value={group}");322 eprintln!("TRACE eob_pt value={group}");
313 }323 }
@@ -372,11 +382,11 @@ fn read_coeffs(
372 .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?;382 .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?;
373 }383 }
374 384
375- let eob = read_eob(dec, coding);385+ let class = TxClass::of(tx_type);
386+ let eob = read_eob(dec, coding, class);
376 if trace {387 if trace {
377 eprintln!("TRACE eob value={eob}");388 eprintln!("TRACE eob value={eob}");
378 }389 }
379- let class = TxClass::of(tx_type);
380 let class_scan;390 let class_scan;
381 let scan: &[u16] = if class == TxClass::TwoD {391 let scan: &[u16] = if class == TxClass::TwoD {
382 scan392 scan