From b2eabdd9d876fa04f6b96b06804d697d83e697cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 05:19:17 +0300 Subject: [PATCH] feat(av1): 16x16-level rectangular INTER leaves -- PARTITION_HORZ/VERT into two 16x8/8x16 inter blocks Mode info, rectangular residual and prediction for the 16-level rect inter leaf, the shape lane-inter4 r2 measured as the real reach blocker (real motion content codes one before any 32-level 2:1 strip). - decode.rs: the 16x16-level PARTITION_HORZ/VERT arms (side=SUB keeps every CDF/prediction-buffer decision square, write_w/write_h carry the true footprint), `rect_inter_residual_supported` extended to 16x8/8x16, `rect_inter_luma_set`/`rect_inter_chroma_set` picking the TxbSet from the block's true shape, motion_mode/obmc CDF rows for BLOCK_8X16/BLOCK_16X8, and `size_group_wh` -- `y_mode` and `interintra` now read the block's own `size_group_lookup` row instead of its square `side`'s. - cdf_state.rs/cdf.rs: `TxbSet::LumaRect16x8Inter` (+`Set1`) -- LumaRect16x8's coefficient tables with the inter tx_type alphabet (EXT_TX_SET_DCT_IDTX reduced / EXT_TX_SET_ALL16 otherwise, both at the TX_8X8 CDF row); the missing `eob_pt_128_luma_class1` and `eob_pt_32_chroma_class1` tables, wired into the rect 16x8/8x16 luma and 8x4/4x8 chroma sets (a 1D V_DCT/H_DCT unit was reading the 2D eob alphabet) and into the per-frame counter reset. - decode.rs `obmc_blend`: libaom's real `max_neighbor_obmc` table (1 mi -> 0, 2 -> 1) instead of a row that was right only at 16 px and above. - REFUSALS: "an inter partition below 16x16 other than SPLIT" is gone; two named refusals take its place -- the 16-level AB/1:4 partitions, and OBMC on a 16x8/8x16 leaf (which mismatches the reference; see the report). Gate: a_real_aomenc_inter_sequence_with_a_16_level_rect_leaf_decodes_pixel_exact (8-bit: 3 attempts carrying a 16x8 leaf, 2 an 8x16, 2 coded rect inter TUs; 10-bit: 2/3 leaves, residual proven by the 8-bit arm), every frame Y/U/V vs ffmpeg, refusals counted and never SKIPped. --- crates/ec-av1/src/cdf.rs | 37 +++- crates/ec-av1/src/cdf_state.rs | 72 +++++++- crates/ec-av1/src/decode.rs | 204 +++++++++++++++++++-- crates/ec-av1/src/refusal_inventory.rs | 3 +- crates/ec-av1/src/stream.rs | 239 +++++++++++++++++++++++++ 5 files changed, 535 insertions(+), 20 deletions(-) diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index bcbb00c3..f8037f97 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -1610,7 +1610,7 @@ pub const SKIP_MODE: [[u16; 3]; 3] = [[32621, 32768, 0], [20708, 32768, 0], [812 /// `motion_mode_allowed` (spec 5.11.24's `read_motion_mode`) offers /// `OBMC_CAUSAL` rather than `WARPED_CAUSAL` -- lane-motionmode round 1 /// never reaches the three-way `motion_mode_cdf`, see `decode.rs`. -pub const OBMC: [[u16; 3]; 12] = [ +pub const OBMC: [[u16; 3]; 14] = [ [10437, 32768, 0], [17432, 32768, 0], [25817, 32768, 0], @@ -1630,6 +1630,10 @@ pub const OBMC: [[u16; 3]; 12] = [ [20901, 32768, 0], [24008, 32768, 0], [26879, 32768, 0], + // lane-inter4 r3: the 16x16-level rect inter leaves' own rows -- + // BLOCK_8X16 (4) and BLOCK_16X8 (5) of libaom's `default_obmc_cdf`. + [9371, 32768, 0], + [9301, 32768, 0], ]; /// `default_motion_mode_cdf` (spec 9.4, `entropymode.c`), the square-bsize @@ -1639,7 +1643,7 @@ pub const OBMC: [[u16; 3]; 12] = [ /// `read_motion_mode` reads instead of [`OBMC`] whenever `motion_mode_allowed` /// (spec 5.11.24) resolves to `WARPED_CAUSAL`-eligible (`num_proj_ref >= 1` /// under `allow_warped_motion`) -- lane-warp round 1. -pub const MOTION_MODE: [[u16; 4]; 12] = [ +pub const MOTION_MODE: [[u16; 4]; 14] = [ [7651, 24760, 32768, 0], [19419, 26810, 32768, 0], [26260, 29116, 32768, 0], @@ -1655,6 +1659,10 @@ pub const MOTION_MODE: [[u16; 4]; 12] = [ [26431, 30774, 32768, 0], [28973, 31594, 32768, 0], [29742, 31203, 32768, 0], + // lane-inter4 r3: BLOCK_8X16 / BLOCK_16X8 rows (libaom + // `default_motion_mode_cdf` indices 4 and 5). + [4738, 24765, 32768, 0], + [5391, 25528, 32768, 0], ]; /// `default_interintra_cdf` (spec 9.4): `interintra`, indexed by @@ -5214,6 +5222,31 @@ pub const EOB_PT_16_CHROMA_CLASS1: [u16; 6] = [3192, 5032, 10297, 19755, 32768, pub const EOB_PT_16_CHROMA_CLASS1_Q3: [u16; 6] = [7297, 10767, 19273, 28194, 32768, 0]; /// `EOB_PT_128_CHROMA`'s class-1 sibling (`av1_default_eob_multi128_cdfs[q][1][1]`). +/// `EOB_PT_128_LUMA`'s class-1 sibling (`av1_default_eob_multi128_cdfs[q][0][1]`, +/// token_cdfs.h:830-848) -- the `TX_CLASS_HORIZ`/`TX_CLASS_VERT` (`V_DCT`/ +/// `H_DCT`) eob group of a 128-position LUMA transform, i.e. a true 16x8/8x16 +/// one. lane-inter4 r3: an inter 16x8 leaf reads `EXT_TX_SET_ALL16`, so 1D +/// types are common there; the intra rect leaf could reach them too and was +/// falling back to the 2D table. +pub const EOB_PT_128_LUMA_CLASS1_Q0: [u16; 9] = [371, 699, 1254, 4830, 9479, 12562, 17497, 32768, 0]; +/// [`EOB_PT_128_LUMA_CLASS1_Q0`]'s q-context 1 sibling. +pub const EOB_PT_128_LUMA_CLASS1_Q1: [u16; 9] = [217, 352, 618, 2303, 5261, 9969, 17472, 32768, 0]; +/// [`EOB_PT_128_LUMA_CLASS1_Q0`]'s q-context 2 sibling. +pub const EOB_PT_128_LUMA_CLASS1: [u16; 9] = [354, 558, 944, 2760, 7287, 14037, 21779, 32768, 0]; +/// [`EOB_PT_128_LUMA_CLASS1_Q0`]'s q-context 3 sibling. +pub const EOB_PT_128_LUMA_CLASS1_Q3: [u16; 9] = [886, 1731, 3271, 8469, 15569, 22126, 28383, 32768, 0]; + +/// `EOB_PT_32_CHROMA`'s class-1 sibling (`av1_default_eob_multi32_cdfs[q][1][1]`, +/// token_cdfs.h:792-808) -- the chroma 8x4/4x8 transform under a rect leaf +/// inherits its luma unit's `tx_type`, so it reaches the 1D classes too. +pub const EOB_PT_32_CHROMA_CLASS1_Q0: [u16; 7] = [1786, 3179, 6902, 11357, 19054, 32768, 0]; +/// [`EOB_PT_32_CHROMA_CLASS1_Q0`]'s q-context 1 sibling. +pub const EOB_PT_32_CHROMA_CLASS1_Q1: [u16; 7] = [2578, 4124, 8181, 13670, 24234, 32768, 0]; +/// [`EOB_PT_32_CHROMA_CLASS1_Q0`]'s q-context 2 sibling. +pub const EOB_PT_32_CHROMA_CLASS1: [u16; 7] = [3542, 5502, 10415, 16760, 25644, 32768, 0]; +/// [`EOB_PT_32_CHROMA_CLASS1_Q0`]'s q-context 3 sibling. +pub const EOB_PT_32_CHROMA_CLASS1_Q3: [u16; 7] = [7699, 10897, 20891, 26926, 31628, 32768, 0]; + pub const EOB_PT_128_CHROMA_CLASS1_Q0: [u16; 9] = [2054, 3472, 5869, 14232, 18242, 20590, 26752, 32768, 0]; pub const EOB_PT_128_CHROMA_CLASS1_Q1: [u16; 9] = [2310, 4160, 7471, 14997, 17931, 20768, 30240, 32768, 0]; pub const EOB_PT_128_CHROMA_CLASS1: [u16; 9] = [6275, 9889, 14769, 23164, 27988, 30493, 32272, 32768, 0]; diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index c2c3efff..f50acab5 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -175,6 +175,24 @@ pub(crate) enum TxbSet { /// 16x16 row's five-symbol reduced CDF here and desynced on the very /// first coded rect leaf. LumaRect16x8Set1, + /// lane-inter4 r3: [`LumaRect16x8`](TxbSet::LumaRect16x8)'s INTER twin -- + /// the same coefficient tables (`get_txsize_entropy_ctx` reduces TX_16X8 + /// and TX_8X16 alike to the 16x16 set, 128-position `eob_pt`), with the + /// inter `tx_type` alphabet in place of the intra one. Under + /// `reduced_tx_set` `av1_get_ext_tx_set_type` (`tx_size_sqr_up == + /// TX_16X16`, `is_inter`) returns `EXT_TX_SET_DCT_IDTX` (2 symbols), + /// whose CDF row is `default_inter_ext_tx_cdf[1][txsize_sqr_map[TX_16X8] + /// == TX_8X8]`, i.e. [`INTER_TX_TYPE_SET3_8`](crate::cdf::INTER_TX_TYPE_SET3_8) + /// -- the same table the square `Luma8Inter` set reads. + LumaRect16x8Inter, + /// [`LumaRect16x8Inter`](TxbSet::LumaRect16x8Inter)'s `reduced_tx_set: + /// false` counterpart: `tx_size_sqr` is `TX_8X8` here (NOT the + /// `txsize_sqr_up_map` `TX_16X16` the coefficient tables use), so + /// `av1_ext_tx_set_lookup[is_inter=1][tx_size_sqr == TX_16X16 -> 0]` is + /// `EXT_TX_SET_ALL16` -- sixteen symbols, `default_inter_ext_tx_cdf[1] + /// [TX_8X8]`, i.e. [`INTER_TX_TYPE_SET1_8`](crate::cdf::INTER_TX_TYPE_SET1_8), + /// the table [`Luma8InterSet1`](TxbSet::Luma8InterSet1) also reads. + LumaRect16x8InterSet1, /// The chroma 8x4/4x8 transform under the same leaf (lane-rectx): /// [`Chroma8`](TxbSet::Chroma8)'s tables except `eob_pt`, which reads the /// true 32-position [`crate::cdf::EOB_PT_32_CHROMA`] instead of @@ -289,6 +307,10 @@ pub(crate) struct Cdfs { pub eob_pt_64_chroma_class1: [u16; 8], /// `eob_pt_128_chroma`'s class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling, see `eob_pt_16_luma_class1`. pub eob_pt_128_chroma_class1: [u16; 9], + /// `eob_pt_128_luma`'s class-1 sibling (lane-inter4 r3). + pub eob_pt_128_luma_class1: [u16; 9], + /// `eob_pt_32_chroma`'s class-1 sibling (lane-inter4 r3). + pub eob_pt_32_chroma_class1: [u16; 7], /// `eob_pt_256_luma`'s class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling, see `eob_pt_16_luma_class1`. pub eob_pt_256_luma_class1: [u16; 10], /// `eob_pt_256_chroma`'s class-1 (`TX_CLASS_HORIZ`/`VERT`) sibling, see `eob_pt_16_luma_class1`. @@ -479,13 +501,13 @@ pub(crate) struct Cdfs { /// `BLOCK_8X8`, 1 = `BLOCK_16X16`, 2 = `BLOCK_32X32`, 3 = `BLOCK_64X64` /// (`default_obmc_cdf`'s own `BLOCK_SIZES_ALL` indices 3/6/9/12 -- /// this decoder only ever codes those four square sizes). - pub obmc: [[u16; 3]; 12], + pub obmc: [[u16; 3]; 14], /// `motion_mode` (spec 5.11.24's `read_motion_mode`), the 3-symbol /// `SIMPLE_TRANSLATION`/`OBMC_CAUSAL`/`WARPED_CAUSAL` alphabet read /// instead of [`Self::obmc`] whenever `motion_mode_allowed` resolves to /// `WARPED_CAUSAL`-eligible -- same square-bsize indexing as `obmc`. /// lane-warp round 1. - pub motion_mode: [[u16; 4]; 12], + pub motion_mode: [[u16; 4]; 14], /// `interintra`, indexed by `size_group_lookup[bsize]` -- see /// `cdf::INTERINTRA`'s own doc. pub interintra: [[u16; 3]; 4], @@ -724,6 +746,8 @@ impl Cdfs { reset1(&mut self.eob_pt_16_chroma_class1); reset1(&mut self.eob_pt_64_chroma_class1); reset1(&mut self.eob_pt_128_chroma_class1); + reset1(&mut self.eob_pt_128_luma_class1); + reset1(&mut self.eob_pt_32_chroma_class1); reset1(&mut self.eob_pt_256_luma_class1); reset1(&mut self.eob_pt_256_chroma_class1); reset1(&mut self.eob_pt_512_luma_class1); @@ -990,6 +1014,20 @@ impl Cdfs { cdf::EOB_PT_128_CHROMA_CLASS1, cdf::EOB_PT_128_CHROMA_CLASS1_Q3, ), + eob_pt_128_luma_class1: pick( + q_ctx, + cdf::EOB_PT_128_LUMA_CLASS1_Q0, + cdf::EOB_PT_128_LUMA_CLASS1_Q1, + cdf::EOB_PT_128_LUMA_CLASS1, + cdf::EOB_PT_128_LUMA_CLASS1_Q3, + ), + eob_pt_32_chroma_class1: pick( + q_ctx, + cdf::EOB_PT_32_CHROMA_CLASS1_Q0, + cdf::EOB_PT_32_CHROMA_CLASS1_Q1, + cdf::EOB_PT_32_CHROMA_CLASS1, + cdf::EOB_PT_32_CHROMA_CLASS1_Q3, + ), eob_pt_256_luma_class1: pick( q_ctx, cdf::EOB_PT_256_LUMA_CLASS1_Q0, @@ -1755,7 +1793,7 @@ impl Cdfs { br: &mut self.br_luma_16, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()), - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_128_luma_class1), }, TxbSet::LumaRect16x8Set1 => TxbTables { side: 16, @@ -1767,7 +1805,31 @@ impl Cdfs { br: &mut self.br_luma_16, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.intra_tx_type_8_set1[mode].as_mut_slice()), - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_128_luma_class1), + }, + TxbSet::LumaRect16x8Inter => TxbTables { + side: 16, + txb_skip: &mut self.txb_skip_luma_16, + eob_pt: &mut self.eob_pt_128_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_8.as_mut_slice()), + eob_pt_class1: Some(&mut self.eob_pt_128_luma_class1), + }, + TxbSet::LumaRect16x8InterSet1 => TxbTables { + side: 16, + txb_skip: &mut self.txb_skip_luma_16, + eob_pt: &mut self.eob_pt_128_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_8_set1.as_mut_slice()), + eob_pt_class1: Some(&mut self.eob_pt_128_luma_class1), }, TxbSet::ChromaRect8x4 => TxbTables { side: 8, @@ -1779,7 +1841,7 @@ impl Cdfs { br: &mut self.br_chroma_8, dc_sign: &mut self.dc_sign_chroma, tx_type: None, - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_32_chroma_class1), }, } } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index a6025eb8..ba01e260 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -774,6 +774,10 @@ thread_local! { // their rect max transform into square sub-transforms. static RECT_INTER_TU_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; static RECT_INTER_TXSPLIT_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + // lane-inter4 r3: 16x16-level PARTITION_HORZ / PARTITION_VERT on an inter + // frame -- 16x8 and 8x16 inter leaves. + static INTER_LEAF16_HORZ_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static INTER_LEAF16_VERT_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; // This frame's own `tx_mode == TxMode::Select` and `reduced_tx_set` bits, // set once per inter tile by [`decode_inter_frame_tile_with_cdfs`] -- // `decode_inter_block` already carries forty parameters and both are @@ -792,6 +796,16 @@ pub(crate) fn txfm_split_hits() -> usize { TXFM_SPLIT_HITS.with(|c| c.get()) } +/// Current value of [`INTER_LEAF16_HORZ_HITS`] (lane-inter4 r3). +pub(crate) fn inter_leaf16_horz_hits() -> usize { + INTER_LEAF16_HORZ_HITS.with(|c| c.get()) +} + +/// Current value of [`INTER_LEAF16_VERT_HITS`] (lane-inter4 r3). +pub(crate) fn inter_leaf16_vert_hits() -> usize { + INTER_LEAF16_VERT_HITS.with(|c| c.get()) +} + /// Current value of [`RECT_INTER_TU_HITS`] (lane-inter4 r2). pub(crate) fn rect_inter_tu_hits() -> usize { RECT_INTER_TU_HITS.with(|c| c.get()) @@ -9018,7 +9032,53 @@ fn read_block_tx_size( /// `DTT9_IDTX_1DDCT` row at `tx_size_sqr == TX_8X8`, only reachable through a /// rect transform), neither of which exists yet -- both stay refused by name. fn rect_inter_residual_supported(w: usize, h: usize) -> bool { - matches!((w, h), (32, 16) | (16, 32)) + matches!((w, h), (32, 16) | (16, 32) | (16, 8) | (8, 16)) +} + +/// The luma coefficient/`tx_type` table set of a whole-block rectangular +/// INTER transform unit (lane-inter4 r3). `av1_get_ext_tx_set_type` splits +/// the two shapes: a 32x16/16x32 unit has `tx_size_sqr_up == TX_32X32`, so +/// it reads the 2-symbol `EXT_TX_SET_DCT_IDTX` set regardless of +/// `reduced_tx_set`; a 16x8/8x16 unit has `tx_size_sqr_up == TX_16X16` and +/// `tx_size_sqr == TX_8X8`, so it reads `EXT_TX_SET_DCT_IDTX` when reduced +/// and the 16-symbol `EXT_TX_SET_ALL16` otherwise -- both at the *8x8* CDF +/// row (`txsize_sqr_map`), never the 16x16 one. +fn rect_inter_luma_set(w: usize, h: usize) -> TxbSet { + match (w, h) { + (32, 16) | (16, 32) => TxbSet::LumaRect32x16Inter, + (16, 8) | (8, 16) => { + if REDUCED_TX_SET_INTER.with(std::cell::Cell::get) { + TxbSet::LumaRect16x8Inter + } else { + TxbSet::LumaRect16x8InterSet1 + } + } + _ => unreachable!("rect_inter_residual_supported gates every shape that reaches here"), + } +} + +/// The chroma table set of the same block's single rectangular chroma unit +/// (`av1_get_max_uv_txsize`): 16x8/8x16 under a 32x16/16x32 strip, 8x4/4x8 +/// under a 16x8/8x16 one. Neither codes a `tx_type` symbol -- both inherit +/// the luma unit's. +fn rect_inter_chroma_set(w: usize, h: usize) -> TxbSet { + match (w, h) { + (16, 8) | (8, 16) => TxbSet::ChromaRect16x8, + (8, 4) | (4, 8) => TxbSet::ChromaRect8x4, + _ => unreachable!("rect_inter_residual_supported gates every shape that reaches here"), + } +} + +/// libaom `size_group_lookup[bsize]` for the block shapes this decoder's +/// inter path reaches, from the block's TRUE footprint rather than the +/// square `side` corner-cut: 8x8/8x16/16x8 -> 1, 16x16/16x32/32x16 -> 2, +/// everything 32 and above -> 3. +fn size_group_wh(w: usize, h: usize) -> usize { + match w.min(h) { + 0..=8 => 1, + 16 => 2, + _ => 3, + } } /// The default (2D) coefficient scan of a rectangular transform. @@ -12715,11 +12775,34 @@ fn obmc_blend( // its OWN neighbour cap (width-log2 for above, height-log2 for left) and // overlap (bh/2 rows above, bw/2 cols left) -- one square `side` was // right only for square blocks. + // libaom `max_neighbor_obmc[6] = { 0, 1, 2, 3, 4, 4 }` indexed by + // `mi_size_wide_log2[bsize]` (above pass) / `mi_size_high_log2` (left): + // 1 mi -> 0, 2 -> 1, 4 -> 2, 8 -> 3, 16+ -> 4. lane-inter4 r3: the old + // `1..=4 => 2` row was right only for blocks at least 16 px on that axis + // -- a 16x8 leaf's LEFT pass (bh4 = 2) walked up to two neighbours where + // libaom walks one, blending an extra 8-px-tall neighbour into every + // rect leaf with two of them on its left edge (class table-narrowed-to- + // the-reachable-sizes; the square gates could never expose it). let max_nb = |n4: usize| match n4 { - 1..=4 => 2, + 0..=1 => 0, + 2 => 1, + 3..=4 => 2, 5..=8 => 3, _ => 4, }; + // lane-inter4 r3: OBMC on a 16-level rect inter leaf mismatches ffmpeg + // (192x128 testsrc-style motion, cq 22, `--enable-obmc=1`: 5302 luma + // pixels of frame 5 wrong, max |delta| 84, while the SAME recipe with + // `--enable-obmc=0` decodes 5 rect-leaf-carrying attempts pixel-exact). + // The blend geometry here matches libaom `build_obmc_inter_pred_*` for + // these shapes, so the cause is upstream (neighbour walk or mv stack for + // a 2:1 leaf) and unidentified -- refuse by name rather than ship wrong + // pixels. Deleting this block is the whole lift once it is found. + if matches!((write_w, write_h), (16, 8) | (8, 16)) { + return Err(unsupported( + "OBMC on a 16x8/8x16 inter leaf (blend mismatches the reference on this shape)", + )); + } let overlap_above = write_h / 2; let overlap_left = write_w / 2; // lane-inter8 r1 / lane-scaledref r1: `av1_skip_u4x4_pred_in_obmc` @@ -13143,7 +13226,11 @@ fn decode_inter_block( chroma_tx: usize, scan_luma: &[u16], scan_chroma: &[u16], - size_group: usize, + // lane-inter4 r3: superseded by `size_group_wh(write_w, write_h)` at the + // one site that read it (`y_mode`) -- a rect strip's group comes from its + // true footprint, not the caller's square `side`. Kept in the signature + // rather than editing twenty call sites. + _size_group: usize, allow_high_precision_mv: bool, force_integer_mv: bool, interp_fixed: Option, @@ -14205,7 +14292,7 @@ fn decode_inter_block( let (grid, tx_type) = read_inter_plane_rect( dec, cdfs, - TxbSet::LumaRect32x16Inter, + rect_inter_luma_set(write_w, write_h), (write_w, write_h), side, 0, @@ -14248,7 +14335,7 @@ fn decode_inter_block( &read_inter_plane_rect( dec, cdfs, - TxbSet::ChromaRect16x8, + rect_inter_chroma_set(write_chroma_w, write_chroma_h), (uw, uh), chroma_side, 1, @@ -14269,7 +14356,7 @@ fn decode_inter_block( &read_inter_plane_rect( dec, cdfs, - TxbSet::ChromaRect16x8, + rect_inter_chroma_set(write_chroma_w, write_chroma_h), (uw, uh), chroma_side, 2, @@ -14511,7 +14598,10 @@ fn decode_inter_block( && (side == 16 || side == 32) && interintra_shape { - let bsize_group = if side == 16 { 2 } else { 3 }; + // lane-inter4 r3: `size_group_lookup` reads the block's + // TRUE footprint -- a 16x8 strip is group 1, a 32x16 strip + // group 2, not their square `side`'s 2/3. + let bsize_group = size_group_wh(write_w, write_h); let interintra = dec.symbol(&mut cdfs.interintra[bsize_group]) == 1; if interintra { // lane-interintra r1 (decodemv.c 1540-1555): interintra_mode, @@ -14621,6 +14711,9 @@ fn decode_inter_block( (32, 8) => 9, (16, 64) => 10, (64, 16) => 11, + // lane-inter4 r3: the 16x16-level rect inter leaves. + (8, 16) => 12, + (16, 8) => 13, _ => { return Err(unsupported( "a motion_mode symbol for a block shape with no CDF row here", @@ -15137,7 +15230,7 @@ fn decode_inter_block( let (grid, tx_type) = read_inter_plane_rect( dec, cdfs, - TxbSet::LumaRect32x16Inter, + rect_inter_luma_set(write_w, write_h), (write_w, write_h), side, 0, @@ -15180,7 +15273,7 @@ fn decode_inter_block( &read_inter_plane_rect( dec, cdfs, - TxbSet::ChromaRect16x8, + rect_inter_chroma_set(write_chroma_w, write_chroma_h), (uw, uh), chroma_side, 1, @@ -15201,7 +15294,7 @@ fn decode_inter_block( &read_inter_plane_rect( dec, cdfs, - TxbSet::ChromaRect16x8, + rect_inter_chroma_set(write_chroma_w, write_chroma_h), (uw, uh), chroma_side, 2, @@ -15271,7 +15364,7 @@ fn decode_inter_block( this decoder does not code yet", )); } - let mode = dec.symbol(&mut cdfs.y_mode[size_group]); + let mode = dec.symbol(&mut cdfs.y_mode[size_group_wh(write_w, write_h)]); if mode >= 13 { return Err(unsupported( "an intra mode this decoder does not code (round 2)", @@ -17877,9 +17970,96 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( allow_screen_content_tools, frame_width as usize, )?; + } else if part16 == PARTITION_HORZ || part16 == PARTITION_VERT { + // lane-inter4 r3: the 16x16-level rect inter + // leaves -- two true 16x8 (HORZ) or 8x16 + // (VERT) inter blocks. Same shape as the + // 32-level HORZ/VERT arm above: `side = SUB` + // keeps every CDF/prediction-buffer decision + // square, `write_w`/`write_h` carry the true + // footprint into the mv stack, the motion-mode + // row, the neighbour stamps and the rectangular + // residual path (`rect_inter_residual_supported` + // covers 16x8/8x16 since this round). + let horz = part16 == PARTITION_HORZ; + if horz { + INTER_LEAF16_HORZ_HITS.with(|c| c.set(c.get() + 1)); + } else { + INTER_LEAF16_VERT_HITS.with(|c| c.set(c.get() + 1)); + } + let base_mi = sub16_to_mi(at16); + let (ww, wh) = if horz { (16, 8) } else { (8, 16) }; + for piece in 0..2 { + let piece_mi = if horz { + (base_mi.0 + piece * 2, base_mi.1) + } else { + (base_mi.0, base_mi.1 + piece * 2) + }; + // libaom `decode_partition`'s own second- + // half frame-edge break. + if piece > 0 + && (piece_mi.0 >= mi_rows as usize + || piece_mi.1 >= mi_cols as usize) + { + break; + } + decode_inter_block( + &mut dec, + &mut cdfs, + &mut neighbours, + &mut grid, + piece_mi, + SUB, + mi_cols, + mi_rows, + &mut y, + &mut u, + &mut v, + &ref_y, + &ref_u, + &ref_v, + &ref_slots, + &sign_bias_table, + &global_motion, + base_q_idx, + TxbSet::Luma16, + if reduced_tx_set { + TxbSet::Luma16Inter + } else { + TxbSet::Luma16InterSet1 + }, + TxbSet::Chroma8, + TX16, + TX8, + &scan16, + &scan8, + 1, + allow_high_precision_mv, + force_integer_mv, + interp_fixed, + enable_dual_filter, + tpl_frame.as_ref(), + reference_select, + enable_masked_compound, + enable_interintra_compound, + enable_jnt_comp, + order_hint_bits, + order_hint, + ref_order_hints, + skip_mode_present, + skip_mode_frame, + switchable_motion_mode, + allow_warped_motion, + true, + ww, + wh, + allow_screen_content_tools, + frame_width as usize, + )?; + } } else if part16 != PARTITION_SPLIT { return Err(unsupported( - "an inter partition below 16x16 other than SPLIT (16x8/8x16 rect inter leaves are not coded yet)", + "an inter 16x16-level AB or 1:4 partition (HORZ_A/HORZ_B/VERT_A/VERT_B/HORZ_4/VERT_4; this decoder's inter path codes a 16x16 as NONE, HORZ, VERT or SPLIT)", )); } else { if has_cols16 && has_rows16 { diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 92fa1b60..10d6ece0 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -64,7 +64,8 @@ const REFUSALS: &[&str] = &[ "an inter frame with no key frame before it", "an inter SB-level AB partition (HORZ_A/HORZ_B/VERT_A/VERT_B; this decoder's inter tile path codes a superblock as NONE, SPLIT, HORZ, VERT, HORZ_4 or VERT_4)", "a masked compound 64x64 inter block (compound_type is inferred, not coded, at this size)", - "an inter partition below 16x16 other than SPLIT (16x8/8x16 rect inter leaves are not coded yet)", + "an inter 16x16-level AB or 1:4 partition (HORZ_A/HORZ_B/VERT_A/VERT_B/HORZ_4/VERT_4; this decoder's inter path codes a 16x16 as NONE, HORZ, VERT or SPLIT)", + "OBMC on a 16x8/8x16 inter leaf (blend mismatches the reference on this shape)", "an intra mode this decoder does not code (round 2)", "an intra-coded HORZ/VERT strip needs rectangular intra prediction this decoder does not code yet", // Lifted for the 16x8/8x16 leaf by lane-fistrip; still live one level diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 2ea18d78..0c376e50 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -1945,6 +1945,10 @@ mod tests { // masked compound, palette, ...) are other lanes' work; // this gate is about hidden frames, not about them. "--enable-warped-motion=0", + // lane-inter4 r3: OBMC on a 16x8/8x16 leaf is a NAMED + // REFUSAL (see decode.rs `obmc_blend`) -- it mismatches + // the reference on this shape, so the gate spells the + // tool off rather than counting its refusals as passes. "--enable-obmc=0", "--enable-masked-comp=0", "--enable-interintra-comp=0", @@ -4915,6 +4919,241 @@ mod tests { } } + #[test] + // lane-inter4 r3: the 16x16-level rect INTER leaf -- PARTITION_HORZ / + // PARTITION_VERT at 16x16 on an inter frame, i.e. two 16x8 / 8x16 inter + // blocks, mode info + rectangular residual + prediction. Every decoded + // frame is compared Y/U/V against ffmpeg, refusals are counted and never + // SKIPped, and the gate asserts a pixel-exact attempt for BOTH axes that + // also coded a rectangular inter transform unit (i.e. was not `skip`). + fn a_real_aomenc_inter_sequence_with_a_16_level_rect_leaf_decodes_pixel_exact() { + const NAME: &str = + "a_real_aomenc_inter_sequence_with_a_16_level_rect_leaf_decodes_pixel_exact"; + let _gate_lock = lock_gate_counters(); + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); + return; + } + let (width, height, frame_count) = (192usize, 128usize, 6usize); + // Coded (non-skip) rect inter transform units, summed over both bit + // depths: at 10 bits this recipe's rect leaves all come out `skip` + // (measured at cq 34/22, 30/26 and 24/12 -- the lower pairs stop + // reaching a rect leaf at all), so the RESIDUAL half of the claim is + // proven by the 8-bit arm while the 10-bit arm proves the mode-info, + // prediction and neighbour halves on both axes. + let mut tu_total = 0usize; + for bit_depth in [8u32, 10u32] { + let (mut named_refusals, mut matched, mut out_of_scope, mut oos_mismatch) = + (0u32, 0u32, 0u32, 0u32); + let (mut tu_proved, mut split_proved) = (0usize, 0usize); + let (mut horz_proved, mut vert_proved, mut sub16_split) = (0u32, 0u32, 0usize); + for attempt in 0..16u32 { + // Two sources (a translating textured ramp, and testsrc2's + // natural motion) x two quantisers x two tx-size-search arms x + // two motion steps. + let tx_search = if attempt % 2 == 0 { "0" } else { "1" }; + // A 10-bit encode of the same source spends fewer bits on + // residual at a given cq, so it needs the lower pair to make + // a rect leaf non-skip at all (measured: cq 34/22 gives zero + // coded rect inter TUs in 16 10-bit attempts). + let cq = [34, 22][(attempt / 2 % 2) as usize]; + let natural = attempt / 4 % 2 == 1; + let sp = 3 + (attempt / 8) * 5; + let duration = frame_count as f64 / 25.0; + // Continuous-tone, mid-frequency, translating: a flat or + // banded source makes aomenc code the strips `skip` (r1's + // gate) and its screen-content detector fires on the + // synthetic ramps, stopping the decode at an unrelated intra + // refusal long before any inter strip. + // Structure ALONG one axis is what makes aomenc's RD pick a + // 2:1 rect partition on that axis: horizontal bands drifting + // vertically favour PARTITION_HORZ (32x16 strips), the + // transposed source PARTITION_VERT (16x32). + let lum = if natural { + format!("128+58*sin((Y+N*{sp})/6)+18*sin(X/23)") + } else { + format!("128+58*sin((X+N*{sp})/6)+18*sin(Y/23)") + }; + let source = format!( + "color=c=gray:s={width}x{height}:d={duration}:r=25,format=gray,\ + geq=lum='{lum}',format=yuv420p" + ); + let pix_fmt = if bit_depth == 10 { "yuv420p10le" } else { "yuv420p" }; + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", &source, "-t", &duration.to_string(), + "-pix_fmt", pix_fmt, "-strict", "-1", "-f", "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "{NAME}: ffmpeg refused the {bit_depth}-bit fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let cq_arg = format!("--cq-level={cq}"); + let depth_arg = format!("--bit-depth={bit_depth}"); + let input_depth_arg = format!("--input-bit-depth={bit_depth}"); + let tx_arg = format!("--enable-tx-size-search={tx_search}"); + let args: Vec<&str> = vec![ + "--codec=av1", "--passes=1", "--end-usage=q", &cq_arg, "--cpu-used=0", + "--threads=1", "--row-mt=0", "--sb-size=64", &depth_arg, &input_depth_arg, + "--enable-restoration=0", "--enable-palette=0", "--deltaq-mode=0", + "--enable-filter-intra=0", "--enable-cfl-intra=0", "--enable-intrabc=0", + "--lag-in-frames=0", + // Per-arm overrides go LAST: aomenc keeps the last + // occurrence of a repeated --enable-* flag. + &tx_arg, + "--enable-rect-partitions=1", "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", "--min-partition-size=8", + "--max-partition-size=16", + "--enable-obmc=0", + "--obu", "-o", "-", "-", + ]; + let mut child = Command::new(aomenc_path()) + .args(&args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + let before = (decode::rect_inter_tu_hits(), decode::rect_inter_txsplit_hits()); + let before_leaf = ( + decode::inter_leaf16_horz_hits(), + decode::inter_leaf16_vert_hits(), + decode::inter_sub16_split_hits(), + ); + let frames = match decode_stream(&stream) { + Err(e) => { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{NAME} failed outright, not a named refusal ({bit_depth}-bit, \ + attempt {attempt}): {msg}" + ); + named_refusals += 1; + eprintln!("{bit_depth}-bit attempt {attempt} refusal: {msg}"); + continue; + } + Ok(frames) => frames, + }; + let after = (decode::rect_inter_tu_hits(), decode::rect_inter_txsplit_hits()); + let (tu, split) = (after.0 - before.0, after.1 - before.1); + let (horz, vert, splits16) = ( + decode::inter_leaf16_horz_hits() - before_leaf.0, + decode::inter_leaf16_vert_hits() - before_leaf.1, + decode::inter_sub16_split_hits() - before_leaf.2, + ); + let ffmpeg_frames = if bit_depth == 10 { + ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) + } else { + ffmpeg_decode_sequence(&stream, width, height, frame_count) + }; + assert_eq!(frames.len(), frame_count); + let mismatched = frames + .iter() + .zip(&ffmpeg_frames) + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); + if horz + vert == 0 { + out_of_scope += 1; + if mismatched { + oos_mismatch += 1; + eprintln!( + "{NAME}: {bit_depth}-bit attempt {attempt} MISMATCHES with zero \ + 16-level rect inter leaf -- another shape's defect" + ); + } + continue; + } + if mismatched { + // lane-inter4 r3: where, not just that -- a handful of + // 4x4-aligned pixels is a prediction/reconstruction + // defect, a whole band from the first row down is a + // desync. + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + let bad: Vec = (0..got.y.len()) + .filter(|&k| got.y[k] != want.y[k]) + .collect(); + if !bad.is_empty() { + eprintln!( + "{NAME}: attempt {attempt} {bit_depth}-bit frame {i}: {} luma \ + pixels differ, first at ({}, {}), max |delta| {}", + bad.len(), + bad[0] % width, + bad[0] / width, + bad.iter() + .map(|&k| (got.y[k] as i32 - want.y[k] as i32).abs()) + .max() + .unwrap_or(0) + ); + } + } + } + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (attempt {attempt}, {bit_depth}-bit, tx_search={tx_search})"); + assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (attempt {attempt}, {bit_depth}-bit, tx_search={tx_search})"); + assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (attempt {attempt}, {bit_depth}-bit, tx_search={tx_search})"); + } + tu_proved += tu; + split_proved += split; + sub16_split += splits16; + if horz > 0 { + horz_proved += 1; + } + if vert > 0 { + vert_proved += 1; + } + matched += 1; + } + eprintln!( + "{NAME} ({bit_depth}-bit): {named_refusals} named refusals, {matched} pixel-exact \ + attempts carrying a 16-level rect inter leaf, 16x8-carrying={horz_proved}, \ + 8x16-carrying={vert_proved}, whole-block rect TUs={tu_proved}, rect trees that \ + split={split_proved}, 16x16 SPLITs={sub16_split}, {out_of_scope} attempts \ + carried none ({oos_mismatch} of them mismatched)" + ); + assert_eq!( + oos_mismatch, 0, + "{NAME}: {oos_mismatch} attempt(s) with no coded rect inter residual decoded but \ + mismatched ffmpeg -- a defect of another shape, not a pass" + ); + assert!( + horz_proved > 0 && vert_proved > 0, + "{NAME} ({bit_depth}-bit): the 16-level rect inter leaf is not proven on both \ + axes (16x8={horz_proved}, 8x16={vert_proved}, tu={tu_proved}, 16x16 \ + SPLITs={sub16_split}, {named_refusals} refusals, {out_of_scope} out of \ + scope) -- gate proved nothing" + ); + tu_total += tu_proved; + } + assert!( + tu_total > 0, + "{NAME}: no pixel-exact attempt at either bit depth coded a rectangular inter \ + residual on a 16-level leaf -- the residual half of the claim is unproven" + ); + } + + #[test] fn a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact() { if !have_ffmpeg() {