diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 7d2245e3..f5e0a250 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -1105,6 +1105,26 @@ pub const INTER_TX_TYPE_SET3_16: [u16; 3] = [1998, 32768, 0]; /// `default_inter_ext_tx_cdf[EXT_TX_SET_DCT_IDTX][TX_8X8]`). pub const INTER_TX_TYPE_SET3_8: [u16; 3] = [4167, 32768, 0]; +/// `Default_Inter_Ext_Tx_Cdf`'s `TX_SET_INTER_2` (`EXT_TX_SET_DTT9_IDTX_1DDCT`) +/// row for `TX_16X16` (lane-cdffwd2, spec 9.4/5.11.48): the twelve-symbol +/// transform type of an `is_inter` 16x16 luma transform when `reduced_tx_set +/// == 0` -- `av1_get_ext_tx_set_type`'s `av1_ext_tx_set_lookup[1][1]`, not +/// [`INTER_TX_TYPE_SET3_16`]'s two-symbol reduced set (libaom +/// `entropymode.c` `default_inter_ext_tx_cdf[2][TX_16X16]`). +pub const INTER_TX_TYPE_SET2_16: [u16; 13] = [ + 770, 2421, 5225, 12907, 15819, 18927, 21561, 24089, 26595, 28526, 30529, 32768, 0, +]; + +/// `Default_Inter_Ext_Tx_Cdf`'s `TX_SET_INTER_1` (`EXT_TX_SET_ALL16`) row for +/// `TX_8X8` (lane-cdffwd2): the full sixteen-symbol transform type of an +/// `is_inter` 8x8 luma transform when `reduced_tx_set == 0` +/// (`av1_ext_tx_set_lookup[1][0]`, libaom `entropymode.c` +/// `default_inter_ext_tx_cdf[1][TX_8X8]`). +pub const INTER_TX_TYPE_SET1_8: [u16; 17] = [ + 1645, 2573, 4778, 5711, 7807, 8622, 10522, 15357, 17674, 20408, 22517, 25010, 27116, 28856, + 30749, 32768, 0, +]; + // The inter-frame tables below (spec 9.4) support the syntax an inter block // needs: whether it is coded as intra at all, which reference frame and // motion vector it takes when it is not. The compound-reference tables diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 6093bfbc..f04826cd 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -37,6 +37,13 @@ pub(crate) enum TxbSet { /// reads, but its own two-symbol `tx_type` table rather than `Luma16`'s /// five-symbol, mode-indexed one. Luma16Inter, + /// [`Luma16Inter`](TxbSet::Luma16Inter)'s `reduced_tx_set: false` + /// counterpart (lane-cdffwd2, root-caused off `cdfflake-stream-seed45`): + /// the same coefficient tables, but the twelve-symbol + /// `EXT_TX_SET_DTT9_IDTX_1DDCT` `tx_type` table + /// ([`crate::cdf::INTER_TX_TYPE_SET2_16`]) rather than `Luma16Inter`'s + /// two-symbol reduced one. + Luma16InterSet1, /// The 8x8 luma transform of an intra 8x8 leaf under a straddling 16x16 /// block (lane-av1-rect). An `is_inter` leaf reads /// [`Luma8Inter`](TxbSet::Luma8Inter) instead. @@ -55,6 +62,10 @@ pub(crate) enum TxbSet { /// adapted table from the 16x16 and 32x32 sizes' own entries in the same /// set, per `entropymode.c`). Luma8Inter, + /// [`Luma8Inter`](TxbSet::Luma8Inter)'s `reduced_tx_set: false` + /// counterpart (lane-cdffwd2): the sixteen-symbol `EXT_TX_SET_ALL16` + /// `tx_type` table ([`crate::cdf::INTER_TX_TYPE_SET1_8`]). + Luma8InterSet1, /// The 4x4 luma transform of a `TxMode::Select` 8x8 leaf or 16x16 block /// whose `tx_depth` resolves all the way down (lane-av1tx4). An /// `is_inter` block has no writer path yet, so this crate has no @@ -287,6 +298,14 @@ pub(crate) struct Cdfs { /// `reduced_tx_set` (spec `TX_SET_INTER_3`, `TX_8X8` row) -- not indexed /// by mode, same as [`Self::inter_tx_type_32`] (lane-av1inter8). pub inter_tx_type_8: [u16; 3], + /// [`Self::inter_tx_type_16`]'s `reduced_tx_set: false` counterpart + /// (lane-cdffwd2): the twelve-symbol `EXT_TX_SET_DTT9_IDTX_1DDCT` row, + /// not indexed by mode. + pub inter_tx_type_16_set2: [u16; 13], + /// [`Self::inter_tx_type_8`]'s `reduced_tx_set: false` counterpart + /// (lane-cdffwd2): the sixteen-symbol `EXT_TX_SET_ALL16` row, not + /// indexed by mode. + pub inter_tx_type_8_set1: [u16; 17], /// The DC sign of a chroma coefficient, shared by both chroma sets. pub dc_sign_chroma: [[u16; 3]; 3], /// Whether an inter frame's block is coded as intra. @@ -522,6 +541,8 @@ impl Cdfs { reset1(&mut self.inter_tx_type_32); reset1(&mut self.inter_tx_type_16); reset1(&mut self.inter_tx_type_8); + reset1(&mut self.inter_tx_type_16_set2); + reset1(&mut self.inter_tx_type_8_set1); reset2(&mut self.dc_sign_chroma); reset2(&mut self.intra_inter); reset3(&mut self.single_ref); @@ -955,6 +976,8 @@ impl Cdfs { inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32, inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16, inter_tx_type_8: cdf::INTER_TX_TYPE_SET3_8, + inter_tx_type_16_set2: cdf::INTER_TX_TYPE_SET2_16, + inter_tx_type_8_set1: cdf::INTER_TX_TYPE_SET1_8, dc_sign_chroma: cdf::DC_SIGN_CHROMA, intra_inter: cdf::INTRA_INTER, single_ref: cdf::SINGLE_REF, @@ -1047,6 +1070,18 @@ impl Cdfs { tx_type: Some(self.inter_tx_type_16.as_mut_slice()), eob_pt_class1: None, }, + TxbSet::Luma16InterSet1 => TxbTables { + side: 16, + txb_skip: &mut self.txb_skip_luma_16, + eob_pt: &mut self.eob_pt_256_luma, + eob_extra: &mut self.eob_extra_luma_16, + base: &mut self.base_luma_16, + base_eob: &mut self.base_eob_luma_16, + br: &mut self.br_luma_16, + dc_sign: &mut self.dc_sign_luma, + tx_type: Some(self.inter_tx_type_16_set2.as_mut_slice()), + eob_pt_class1: None, + }, TxbSet::Luma8 => TxbTables { side: 8, txb_skip: &mut self.txb_skip_luma_8, @@ -1107,6 +1142,18 @@ impl Cdfs { tx_type: Some(self.inter_tx_type_8.as_mut_slice()), eob_pt_class1: None, }, + TxbSet::Luma8InterSet1 => TxbTables { + side: 8, + txb_skip: &mut self.txb_skip_luma_8, + eob_pt: &mut self.eob_pt_64_luma, + eob_extra: &mut self.eob_extra_luma_8, + base: &mut self.base_luma_8, + base_eob: &mut self.base_eob_luma_8, + br: &mut self.br_luma_8, + dc_sign: &mut self.dc_sign_luma, + tx_type: Some(self.inter_tx_type_8_set1.as_mut_slice()), + eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1), + }, TxbSet::Chroma8 => TxbTables { side: 8, txb_skip: &mut self.txb_skip_chroma_8, diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 571adc37..49597772 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -400,8 +400,10 @@ enum TxClass { impl TxClass { fn of(tx_type: TxType) -> Self { match tx_type { - TxType::VDct => Self::Vert, - TxType::HDct => Self::Horiz, + // `TX_CLASS_VERT`/`TX_CLASS_HORIZ` (`txb_common.h`'s `tx_type_to_class`) + // key on axis alone -- FLIPADST is still ADST for this purpose. + TxType::VDct | TxType::VAdst | TxType::VFlipAdst => Self::Vert, + TxType::HDct | TxType::HAdst | TxType::HFlipAdst => Self::Horiz, _ => Self::TwoD, } } @@ -599,18 +601,24 @@ fn read_coeffs( } let mut tx_type = default_tx_type; if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { - // The CDF row's own width names its set: 7 symbols (8 slots) is - // `TX_SET_INTRA_1`, 5 (6 slots) the reduced `TX_SET_INTRA_2` (the - // inter sets we read share set 2's symbol order). - let set1 = tx_type_cdf.len() == 8; + // The CDF row's own width names its set (lane-cdffwd2: extended + // past the original two-way intra split once wider `reduced_tx_set + // == 0` inter sets joined the table): 17 slots (16 symbols) is + // `EXT_TX_SET_ALL16`, 13 (12 symbols) `EXT_TX_SET_DTT9_IDTX_1DDCT`, + // 8 (7 symbols) `TX_SET_INTRA_1`, everything else (6 slots, 5 + // symbols) the reduced `TX_SET_INTRA_2`/`TX_SET_INTER_3` two sets + // share their symbol order with (`Tx_Type_Inter_Inv_Set3`'s two + // members are a prefix of `Tx_Type_Intra_Inv_Set2`'s five). + let len = tx_type_cdf.len(); let t = dec.symbol(tx_type_cdf); if trace { - eprintln!("TRACE tx_type value={t} set1={set1}"); + eprintln!("TRACE tx_type value={t} len={len}"); } - tx_type = if set1 { - TxType::from_symbol_set1(t) - } else { - TxType::from_symbol(t) + tx_type = match len { + 17 => TxType::from_symbol_all16(t), + 13 => TxType::from_symbol_set2_12(t), + 8 => TxType::from_symbol_set1(t), + _ => TxType::from_symbol(t), } .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; } diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index 7ad050ec..7c764d35 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -320,6 +320,31 @@ pub enum TxType { /// Identity down the column axis, DCT across the row axis /// (`H_DCT` -- `TX_SET_INTRA_1` only). HDct, + /// FLIPADST down the column axis, DCT across the row axis (lane-cdffwd2: + /// reachable once `reduced_tx_set == false`'s wider inter sets are read + /// -- `EXT_TX_SET_DTT9_IDTX_1DDCT`/`EXT_TX_SET_ALL16`). FLIPADST is the + /// same 1D ADST kernel [`AdstDct`] uses (`vtx_tab`/`htx_tab`, + /// `common_data.h`), just read/written flipped -- see [`TxType::flip`]. + FlipAdstDct, + /// DCT down the column axis, FLIPADST across the row axis. + DctFlipAdst, + /// FLIPADST on both axes. + FlipAdstFlipAdst, + /// ADST down the column axis, FLIPADST across the row axis. + AdstFlipAdst, + /// FLIPADST down the column axis, ADST across the row axis. + FlipAdstAdst, + /// ADST down the column axis, identity across the row axis (`V_ADST` -- + /// `EXT_TX_SET_ALL16` only). + VAdst, + /// Identity down the column axis, ADST across the row axis (`H_ADST`). + HAdst, + /// FLIPADST down the column axis, identity across the row axis + /// (`V_FLIPADST`). + VFlipAdst, + /// Identity down the column axis, FLIPADST across the row axis + /// (`H_FLIPADST`). + HFlipAdst, } impl TxType { @@ -353,6 +378,57 @@ impl TxType { } } + /// The inverse of `Tx_Type_Inter_Inv_Set2`'s CDF symbol order + /// (`av1_ext_tx_inv[EXT_TX_SET_DTT9_IDTX_1DDCT]`, `entropymode.h`: + /// `{9, 10, 11, 0, 1, 2, 4, 5, 3, 6, 7, 8}`) -- the twelve-type set a + /// `reduced_tx_set == 0` frame's 16x16 *inter* transform reads from + /// (`av1_get_ext_tx_set_type`'s `tx_size_sqr == TX_16X16` branch of + /// `av1_ext_tx_set_lookup[1]`). + pub fn from_symbol_set2_12(t: usize) -> Option { + match t { + 0 => Some(Self::Idtx), + 1 => Some(Self::VDct), + 2 => Some(Self::HDct), + 3 => Some(Self::DctDct), + 4 => Some(Self::AdstDct), + 5 => Some(Self::DctAdst), + 6 => Some(Self::FlipAdstDct), + 7 => Some(Self::DctFlipAdst), + 8 => Some(Self::AdstAdst), + 9 => Some(Self::FlipAdstFlipAdst), + 10 => Some(Self::AdstFlipAdst), + 11 => Some(Self::FlipAdstAdst), + _ => None, + } + } + + /// The inverse of `Tx_Type_Inter_Inv_Set1`'s CDF symbol order + /// (`av1_ext_tx_inv[EXT_TX_SET_ALL16]`, `entropymode.h`: + /// `{9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 4, 5, 3, 6, 7, 8}`) -- the full + /// sixteen-type set a `reduced_tx_set == 0` frame's 8x8 (and 4x4) inter + /// transform reads from (`av1_ext_tx_set_lookup[1][0]`). + pub fn from_symbol_all16(t: usize) -> Option { + match t { + 0 => Some(Self::Idtx), + 1 => Some(Self::VDct), + 2 => Some(Self::HDct), + 3 => Some(Self::VAdst), + 4 => Some(Self::HAdst), + 5 => Some(Self::VFlipAdst), + 6 => Some(Self::HFlipAdst), + 7 => Some(Self::DctDct), + 8 => Some(Self::AdstDct), + 9 => Some(Self::DctAdst), + 10 => Some(Self::FlipAdstDct), + 11 => Some(Self::DctFlipAdst), + 12 => Some(Self::AdstAdst), + 13 => Some(Self::FlipAdstFlipAdst), + 14 => Some(Self::AdstFlipAdst), + 15 => Some(Self::FlipAdstAdst), + _ => None, + } + } + /// `(row, col)`: the transform `inverse_transform_2d_typed`'s row pass /// (`htx_tab`, spec/libaom "horizontal") and column pass (`vtx_tab`, /// "vertical") each run. Read off libaom's `av1_inv_txfm2d.c` `vtx_tab`/ @@ -370,6 +446,29 @@ impl TxType { // `av1_inv_txfm2d.c`: V_DCT vtx=DCT htx=IDTX, H_DCT the mirror. Self::VDct => (Identity, Dct), Self::HDct => (Dct, Identity), + // FLIPADST reads the same 1D ADST kernel as ADST -- `vtx_tab`/ + // `htx_tab` (`common_data.h`) map `FLIPADST_1D` and `ADST_1D` + // to the exact same butterfly network; only [`Self::flip`] + // differs. So every FLIPADST-bearing axis below mirrors its + // plain-ADST counterpart's kernel pairing. + Self::FlipAdstDct => (Dct, Adst), + Self::DctFlipAdst => (Adst, Dct), + Self::FlipAdstFlipAdst | Self::AdstFlipAdst | Self::FlipAdstAdst => (Adst, Adst), + Self::VAdst | Self::VFlipAdst => (Identity, Adst), + Self::HAdst | Self::HFlipAdst => (Adst, Identity), + } + } + + /// `(ud_flip, lr_flip)` (`get_flip_cfg`, `av1_txfm.h`): whether the + /// column pass reads its input right-to-left and/or the finished + /// residual is written bottom-to-top. Every FLIPADST-bearing type sets + /// one or both; every other type is `(false, false)`. + fn flip(self) -> (bool, bool) { + match self { + Self::FlipAdstDct | Self::FlipAdstAdst | Self::VFlipAdst => (true, false), + Self::DctFlipAdst | Self::AdstFlipAdst | Self::HFlipAdst => (false, true), + Self::FlipAdstFlipAdst => (true, true), + _ => (false, false), } } } @@ -698,6 +797,7 @@ pub fn inverse_transform_2d_typed( let col_clamp = (usize::from(bit_depth) + 6).max(16); let mut residual = vec![0i32; side * side]; let (row_kind, col_kind) = tx_type.axes(); + let (ud_flip, lr_flip) = tx_type.flip(); let mut t = [0i32; 64]; for i in 0..side { @@ -716,16 +816,25 @@ pub fn inverse_transform_2d_typed( } } + // A FLIPADST axis reads the row-pass output right-to-left and/or writes + // its own output bottom-to-top (`inv_txfm2d_add_c`, `av1_inv_txfm2d.c` + // lines 291-314): `out` is a second buffer, not `residual` reused in + // place, because a flipped read/write pair aliases a column this same + // loop has not visited yet (column `j`'s write target under `lr_flip` + // is a column an unflipped loop would still need to read from later). + let mut out = vec![0i32; side * side]; for j in 0..side { + let src_col = if lr_flip { side - 1 - j } else { j }; for i in 0..side { - t[i] = residual[i * side + j]; + t[i] = residual[i * side + src_col]; } inverse_1d(&mut t, log2, col_clamp, col_kind); for i in 0..side { - residual[i * side + j] = round2(t[i], 4); + let dst_row = if ud_flip { side - 1 - i } else { i }; + out[dst_row * side + j] = round2(t[i], 4); } } - residual + out } /// [`inverse_transform_2d_typed`] at `DCT_DCT`, this crate's original