TX4/TX8 eob_pt CDF is class-split (V_DCT/H_DCT vs 2D)
Browse files at this commitea554628a66e3b27d72ca2b3f44d41ad3efa8421Parent: 700543b- fix
- TX4/TX8 eob_pt CDF is class-split (V_DCT/H_DCT vs 2D)
4 changed files +73 -33
- crates/ec-av1/examples/probe_tx4.rs +0 -29
- crates/ec-av1/src/cdf.rs +19 -0
- crates/ec-av1/src/cdf_state.rs +40 -0
- crates/ec-av1/src/decode.rs +14 -4
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 | 2980 | /// EOB_PT_64_LUMA_Q3, q-context 3: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 121..=255. |
| 2981 | 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 | 2991 | /// EOB_PT_16_CHROMA_Q0, q-context 0: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 0..=20. |
| 2984 | 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 | 3971 | pub const EOB_PT_16_LUMA: [u16; 6] = [4016, 4897, 8881, 14968, 32768, 0]; |
| 3964 | 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 | 3985 | // EOB_EXTRA_LUMA_4 |
| 3967 | 3986 | pub const EOB_EXTRA_LUMA_4_Q0: [[u16; 3]; 9] = [ |
| 3968 | 3987 | [16961, 32768, 0], |
crates/ec-av1/src/cdf_state.rsmodified+40 -0
| @@ -90,6 +90,11 @@ pub(crate) struct TxbTables<'a> { | ||
| 90 | 90 | pub txb_skip: &'a mut [[u16; 3]], |
| 91 | 91 | /// The end-of-block group, whose alphabet the transform size sets. |
| 92 | 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 | 98 | /// The top bit of the offset inside that group. |
| 94 | 99 | pub eob_extra: &'a mut [[u16; 3]; 9], |
| 95 | 100 | /// A coefficient's base level. |
| @@ -152,6 +157,11 @@ pub(crate) struct Cdfs { | ||
| 152 | 157 | pub eob_pt_16_chroma: [u16; 6], |
| 153 | 158 | /// The end-of-block group of a 4x4 luma transform (lane-av1tx4). |
| 154 | 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 | 165 | /// The end-of-block offset bit of a 16x16 luma transform. |
| 156 | 166 | pub eob_extra_luma_16: [[u16; 3]; 9], |
| 157 | 167 | /// The same, for an 8x8 luma transform. |
| @@ -429,6 +439,8 @@ impl Cdfs { | ||
| 429 | 439 | reset1(&mut self.eob_pt_64_chroma); |
| 430 | 440 | reset1(&mut self.eob_pt_16_chroma); |
| 431 | 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 | 444 | reset2(&mut self.eob_extra_luma_16); |
| 433 | 445 | reset2(&mut self.eob_extra_luma_8); |
| 434 | 446 | reset2(&mut self.eob_extra_luma_4); |
| @@ -587,6 +599,20 @@ impl Cdfs { | ||
| 587 | 599 | cdf::EOB_PT_16_LUMA, |
| 588 | 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 | 616 | eob_extra_luma_16: pick( |
| 591 | 617 | q_ctx, |
| 592 | 618 | cdf::EOB_EXTRA_LUMA_16_Q0, |
| @@ -924,6 +950,7 @@ impl Cdfs { | ||
| 924 | 950 | br: &mut self.br_luma_32, |
| 925 | 951 | dc_sign: &mut self.dc_sign_luma, |
| 926 | 952 | tx_type: None, |
| 953 | + eob_pt_class1: None, | |
| 927 | 954 | }, |
| 928 | 955 | TxbSet::Luma32Inter => TxbTables { |
| 929 | 956 | side: 32, |
| @@ -935,6 +962,7 @@ impl Cdfs { | ||
| 935 | 962 | br: &mut self.br_luma_32, |
| 936 | 963 | dc_sign: &mut self.dc_sign_luma, |
| 937 | 964 | tx_type: Some(self.inter_tx_type_32.as_mut_slice()), |
| 965 | + eob_pt_class1: None, | |
| 938 | 966 | }, |
| 939 | 967 | TxbSet::Luma64 => TxbTables { |
| 940 | 968 | side: 32, |
| @@ -946,6 +974,7 @@ impl Cdfs { | ||
| 946 | 974 | br: &mut self.br_luma_32, |
| 947 | 975 | dc_sign: &mut self.dc_sign_luma, |
| 948 | 976 | tx_type: None, |
| 977 | + eob_pt_class1: None, | |
| 949 | 978 | }, |
| 950 | 979 | TxbSet::Luma16 => TxbTables { |
| 951 | 980 | side: 16, |
| @@ -957,6 +986,7 @@ impl Cdfs { | ||
| 957 | 986 | br: &mut self.br_luma_16, |
| 958 | 987 | dc_sign: &mut self.dc_sign_luma, |
| 959 | 988 | tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()), |
| 989 | + eob_pt_class1: None, | |
| 960 | 990 | }, |
| 961 | 991 | TxbSet::Luma16Inter => TxbTables { |
| 962 | 992 | side: 16, |
| @@ -968,6 +998,7 @@ impl Cdfs { | ||
| 968 | 998 | br: &mut self.br_luma_16, |
| 969 | 999 | dc_sign: &mut self.dc_sign_luma, |
| 970 | 1000 | tx_type: Some(self.inter_tx_type_16.as_mut_slice()), |
| 1001 | + eob_pt_class1: None, | |
| 971 | 1002 | }, |
| 972 | 1003 | TxbSet::Luma8 => TxbTables { |
| 973 | 1004 | side: 8, |
| @@ -979,6 +1010,7 @@ impl Cdfs { | ||
| 979 | 1010 | br: &mut self.br_luma_8, |
| 980 | 1011 | dc_sign: &mut self.dc_sign_luma, |
| 981 | 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 | 1015 | TxbSet::Luma8Set1 => TxbTables { |
| 984 | 1016 | side: 8, |
| @@ -990,6 +1022,7 @@ impl Cdfs { | ||
| 990 | 1022 | br: &mut self.br_luma_8, |
| 991 | 1023 | dc_sign: &mut self.dc_sign_luma, |
| 992 | 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 | 1027 | TxbSet::Luma4 => TxbTables { |
| 995 | 1028 | side: 4, |
| @@ -1001,6 +1034,7 @@ impl Cdfs { | ||
| 1001 | 1034 | br: &mut self.br_luma_4, |
| 1002 | 1035 | dc_sign: &mut self.dc_sign_luma, |
| 1003 | 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 | 1039 | TxbSet::Luma4Set1 => TxbTables { |
| 1006 | 1040 | side: 4, |
| @@ -1012,6 +1046,7 @@ impl Cdfs { | ||
| 1012 | 1046 | br: &mut self.br_luma_4, |
| 1013 | 1047 | dc_sign: &mut self.dc_sign_luma, |
| 1014 | 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 | 1051 | TxbSet::Luma8Inter => TxbTables { |
| 1017 | 1052 | side: 8, |
| @@ -1023,6 +1058,7 @@ impl Cdfs { | ||
| 1023 | 1058 | br: &mut self.br_luma_8, |
| 1024 | 1059 | dc_sign: &mut self.dc_sign_luma, |
| 1025 | 1060 | tx_type: Some(self.inter_tx_type_8.as_mut_slice()), |
| 1061 | + eob_pt_class1: None, | |
| 1026 | 1062 | }, |
| 1027 | 1063 | TxbSet::Chroma8 => TxbTables { |
| 1028 | 1064 | side: 8, |
| @@ -1034,6 +1070,7 @@ impl Cdfs { | ||
| 1034 | 1070 | br: &mut self.br_chroma_8, |
| 1035 | 1071 | dc_sign: &mut self.dc_sign_chroma, |
| 1036 | 1072 | tx_type: None, |
| 1073 | + eob_pt_class1: None, | |
| 1037 | 1074 | }, |
| 1038 | 1075 | TxbSet::Chroma4 => TxbTables { |
| 1039 | 1076 | side: 4, |
| @@ -1045,6 +1082,7 @@ impl Cdfs { | ||
| 1045 | 1082 | br: &mut self.br_chroma_4, |
| 1046 | 1083 | dc_sign: &mut self.dc_sign_chroma, |
| 1047 | 1084 | tx_type: None, |
| 1085 | + eob_pt_class1: None, | |
| 1048 | 1086 | }, |
| 1049 | 1087 | TxbSet::Chroma16 => TxbTables { |
| 1050 | 1088 | side: 16, |
| @@ -1056,6 +1094,7 @@ impl Cdfs { | ||
| 1056 | 1094 | br: &mut self.br_chroma_16, |
| 1057 | 1095 | dc_sign: &mut self.dc_sign_chroma, |
| 1058 | 1096 | tx_type: None, |
| 1097 | + eob_pt_class1: None, | |
| 1059 | 1098 | }, |
| 1060 | 1099 | TxbSet::Chroma32 => TxbTables { |
| 1061 | 1100 | side: 32, |
| @@ -1067,6 +1106,7 @@ impl Cdfs { | ||
| 1067 | 1106 | br: &mut self.br_chroma_32, |
| 1068 | 1107 | dc_sign: &mut self.dc_sign_chroma, |
| 1069 | 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 | 303 | /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of |
| 304 | 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 | 306 | const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513]; |
| 307 | 307 | const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 308 | 308 | |
| 309 | 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 | 321 | if trace { |
| 312 | 322 | eprintln!("TRACE eob_pt value={group}"); |
| 313 | 323 | } |
| @@ -372,11 +382,11 @@ fn read_coeffs( | ||
| 372 | 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 | 387 | if trace { |
| 377 | 388 | eprintln!("TRACE eob value={eob}"); |
| 378 | 389 | } |
| 379 | - let class = TxClass::of(tx_type); | |
| 380 | 390 | let class_scan; |
| 381 | 391 | let scan: &[u16] = if class == TxClass::TwoD { |
| 382 | 392 | scan |