diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index 92c83905..c9b19234 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -33,6 +33,8 @@ fn main() { println!("rect4_32: horz={h} vert={v} coded={c}"); let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters(); println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}"); + let leaf = ec_av1::stream::vartx_rect_leaf_hits(); + println!("vartx_rect_leaf: 32x16={} 16x32={}", leaf[0], leaf[1]); let ir = ec_av1::stream::inter_rect_counters(); println!( "inter_rect: 32x8={} 8x32={} 64x32={} 32x64={} 64x16={} 16x64={}", diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 2021dd6f..2140341b 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -920,6 +920,8 @@ thread_local! { // (64x32/32x64/64x16/16x64), whose coefficients are coded as the // truncated low-32 corner. static RECT64_INTER_TU_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + // lane-r14 r3: rectangular var-tx LEAVES, [32x16, 16x32]. + static VARTX_RECT_LEAF_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; // 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) }; @@ -972,6 +974,21 @@ pub(crate) fn rect64_inter_tu_hits() -> usize { RECT64_INTER_TU_HITS.with(|c| c.get()) } +/// Current value of [`VARTX_RECT_LEAF_HITS`] (lane-r14 r3): how many +/// rectangular var-tx leaf transform units were coded, `[32x16, 16x32]`. +pub(crate) fn vartx_rect_leaf_hits() -> [usize; 2] { + VARTX_RECT_LEAF_HITS.with(|c| c.get()) +} + +/// Records one rectangular var-tx leaf. +fn vartx_rect_leaf_hit(tw: usize, th: usize) { + VARTX_RECT_LEAF_HITS.with(|c| { + let mut h = c.get(); + h[usize::from(th > tw)] += 1; + c.set(h); + }); +} + /// Sets the current thread's [`TX_SELECT_INTER`]/[`REDUCED_TX_SET_INTER`]/ /// [`ENABLE_FILTER_INTRA_INTER`]. pub(crate) fn set_inter_tx_mode( @@ -10723,6 +10740,46 @@ const TXFM_CTX_INIT: u8 = 64; /// twice below its own largest transform. const MAX_VARTX_DEPTH: usize = 2; +/// libaom `sub_tx_size_map[TX_SIZES_ALL]` (`common_data.c`): the transform +/// one `txfm_split` step below `tx_size`. RECTANGULAR for the 1:4 shapes +/// (`[TX_64X16] == TX_32X16`, `[TX_16X64] == TX_16X32`) and for the 4:1 +/// small ones, square everywhere else. +fn sub_tx_size_map(tx_w: usize, tx_h: usize) -> (usize, usize) { + match (tx_w, tx_h) { + (4, 4) => (4, 4), + (8, 8) => (4, 4), + (16, 16) => (8, 8), + (32, 32) => (16, 16), + (64, 64) => (32, 32), + (4, 8) | (8, 4) => (4, 4), + (8, 16) | (16, 8) => (8, 8), + (16, 32) | (32, 16) => (16, 16), + (32, 64) | (64, 32) => (32, 32), + (4, 16) => (4, 8), + (16, 4) => (8, 4), + (8, 32) => (8, 16), + (32, 8) => (16, 8), + (16, 64) => (16, 32), + (64, 16) => (32, 16), + _ => unreachable!("not a TX_SIZES_ALL shape: {tx_w}x{tx_h}"), + } +} + +#[test] +fn sub_tx_size_map_matches_libaom() { + // libaom `common_data.c` `sub_tx_size_map`, in TX_SIZES_ALL order. + const TABLE: [((usize, usize), (usize, usize)); 19] = [ + ((4, 4), (4, 4)), ((8, 8), (4, 4)), ((16, 16), (8, 8)), ((32, 32), (16, 16)), + ((64, 64), (32, 32)), ((4, 8), (4, 4)), ((8, 4), (4, 4)), ((8, 16), (8, 8)), + ((16, 8), (8, 8)), ((16, 32), (16, 16)), ((32, 16), (16, 16)), ((32, 64), (32, 32)), + ((64, 32), (32, 32)), ((4, 16), (4, 8)), ((16, 4), (8, 4)), ((8, 32), (8, 16)), + ((32, 8), (16, 8)), ((16, 64), (16, 32)), ((64, 16), (32, 16)), + ]; + for (tx, want) in TABLE { + assert_eq!(sub_tx_size_map(tx.0, tx.1), want, "sub_tx_size_map({tx:?})"); + } +} + /// `read_var_tx_size` (spec 5.11.17, libaom `decodeframe.c` /// `read_tx_size_vartx`): the recursive `txfm_split` tree of one /// max-transform-sized unit of an inter block, collecting the resolved leaf @@ -10739,22 +10796,28 @@ fn read_var_tx_size( at_mi: (usize, usize), blk_max_px: usize, (max_w_mi, max_h_mi): (usize, usize), - tx_px: usize, + (tx_w, tx_h): (usize, usize), depth: usize, blk_row: usize, blk_col: usize, - out: &mut Vec<(usize, usize, usize)>, + out: &mut Vec<(usize, usize, usize, usize)>, ) { if blk_row >= max_h_mi || blk_col >= max_w_mi { return; } let unit = (at_mi.0 + blk_row, at_mi.1 + blk_col); if depth == MAX_VARTX_DEPTH { - out.push((blk_row, blk_col, tx_px)); - txfm_partition_update(n, unit, tx_px, tx_px); + out.push((blk_row, blk_col, tx_w, tx_h)); + txfm_partition_update_rect(n, unit, (tx_w, tx_h), (tx_w, tx_h)); return; } - let ctx = txfm_partition_ctx(n.above_txfm[unit.1], n.left_txfm[unit.0], blk_max_px, tx_px); + let ctx = txfm_partition_ctx_rect( + n.above_txfm[unit.1], + n.left_txfm[unit.0], + blk_max_px, + tx_w, + tx_h, + ); let split = dec.symbol(&mut cdfs.txfm_partition[ctx]) == 1; TXFM_SPLIT_READS.with(|c| c.set(c.get() + 1)); if std::env::var_os("EC_TRACE_MODE_STEP").is_some() { @@ -10767,27 +10830,27 @@ fn read_var_tx_size( ); } if !split { - out.push((blk_row, blk_col, tx_px)); - txfm_partition_update(n, unit, tx_px, tx_px); + out.push((blk_row, blk_col, tx_w, tx_h)); + txfm_partition_update_rect(n, unit, (tx_w, tx_h), (tx_w, tx_h)); return; } TXFM_SPLIT_HITS.with(|c| c.set(c.get() + 1)); - let sub = tx_px / 2; - if sub == 4 { - // libaom's `sub_txs == TX_4X4` early return: the whole 8x8 unit is - // marked 4x4 (four transform units, raster order) without recursing. - for row in 0..2 { - for col in 0..2 { + let (sub_w, sub_h) = sub_tx_size_map(tx_w, tx_h); + if (sub_w, sub_h) == (4, 4) { + // libaom's `sub_txs == TX_4X4` early return: every mi cell of the + // unit is marked 4x4 (raster order) without recursing. + for row in 0..tx_h / MI { + for col in 0..tx_w / MI { if blk_row + row < max_h_mi && blk_col + col < max_w_mi { - out.push((blk_row + row, blk_col + col, 4)); + out.push((blk_row + row, blk_col + col, 4, 4)); } } } - txfm_partition_update(n, unit, 4, tx_px); + txfm_partition_update_rect(n, unit, (4, 4), (tx_w, tx_h)); return; } - for row in (0..tx_px / MI).step_by(sub / MI) { - for col in (0..tx_px / MI).step_by(sub / MI) { + for row in (0..tx_h / MI).step_by(sub_h / MI) { + for col in (0..tx_w / MI).step_by(sub_w / MI) { read_var_tx_size( dec, cdfs, @@ -10795,7 +10858,7 @@ fn read_var_tx_size( at_mi, blk_max_px, (max_w_mi, max_h_mi), - sub, + (sub_w, sub_h), depth + 1, blk_row + row, blk_col + col, @@ -10823,7 +10886,7 @@ fn read_block_tx_size( (mi_cols, mi_rows): (usize, usize), is_inter: bool, skip: bool, -) -> Result<(usize, Option>)> { +) -> Result<(usize, Option>)> { if !TX_SELECT_INTER.with(std::cell::Cell::get) { return Ok((side, None)); } @@ -10843,7 +10906,7 @@ fn read_block_tx_size( at_mi, side, (max_w_mi, max_h_mi), - max_tx, + (max_tx, max_tx), 0, row, col, @@ -10851,8 +10914,8 @@ fn read_block_tx_size( ); } } - let single = leaves.len() == 1 && leaves[0].2 == side; - if !single && leaves.iter().any(|leaf| leaf.2 > 32) { + let single = leaves.len() == 1 && leaves[0].2 == side && leaves[0].3 == side; + if !single && leaves.iter().any(|leaf| leaf.2.max(leaf.3) > 32) { // Only a block wider than a superblock's own 64x64 max transform // reaches here; this crate has no 64-point *inter* coefficient // table set ([`inter_txbset_for`] stops at 32). @@ -10890,7 +10953,7 @@ fn read_block_tx_size( let mut leaves = Vec::new(); for row in (0..side_mi).step_by(step) { for col in (0..side_mi).step_by(step) { - leaves.push((row, col, tx)); + leaves.push((row, col, tx, tx)); } } INTRA_IN_INTER_SPLIT_TX_HITS.with(|c| { @@ -11042,7 +11105,7 @@ fn read_block_tx_size_rect( (bw, bh): (usize, usize), (mi_cols, mi_rows): (usize, usize), skip: bool, -) -> Result>> { +) -> Result>> { if skip { // `set_txfm_ctxs(.., skip && is_inter)`: a skipped inter block records // its own BLOCK size -- width above, height left -- not a transform. @@ -11085,22 +11148,16 @@ fn read_block_tx_size_rect( } TXFM_SPLIT_HITS.with(|c| c.set(c.get() + 1)); RECT_INTER_TXSPLIT_HITS.with(|c| c.set(c.get() + 1)); - // lane-r14 r2: `sub_tx_size_map` is square for every shape this decoder - // splits (TX_32X16 -> TX_16X16, TX_64X32 -> TX_32X32) EXCEPT the 1:4 - // strips with a 64-px axis, where it stays rectangular - // (TX_64X16 -> TX_32X16, TX_16X64 -> TX_16X32) and the square recursion - // below would code the wrong tree. - if tx_w.max(tx_h) == 64 && tx_w.min(tx_h) == 16 { - return Err(unsupported( - "a split transform on a 1:4 inter strip with a 64-px axis", - )); - } - let sub = tx_w.min(tx_h); + // lane-r14 r3: `sub_tx_size_map` (libaom `common_data.c`) stays + // RECTANGULAR under a 1:4 strip -- `[TX_64X16] == TX_32X16`, + // `[TX_16X64] == TX_16X32` -- so the recursion below is the general + // rect one and its leaves carry their own width and height. + let (sub_w, sub_h) = sub_tx_size_map(tx_w, tx_h); let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(at_mi.1)); let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(at_mi.0)); let mut leaves = Vec::new(); - for row in (0..bh / MI).step_by(sub / MI) { - for col in (0..bw / MI).step_by(sub / MI) { + for row in (0..bh / MI).step_by(sub_h / MI) { + for col in (0..bw / MI).step_by(sub_w / MI) { read_var_tx_size( dec, cdfs, @@ -11108,7 +11165,7 @@ fn read_block_tx_size_rect( at_mi, bw.max(bh), (max_w_mi, max_h_mi), - sub, + (sub_w, sub_h), 1, row, col, @@ -11145,9 +11202,12 @@ fn read_inter_plane_rect( y: usize, prediction: &[u16], inherited_luma_tx_type: Option, + luma_skip_ctx: Option, ) -> Result<(Vec, TxType)> { let skip_ctx = if plane_idx == 0 { - 0 + // `get_txb_ctx_general`: 0 only when this unit IS the whole plane + // block; a var-tx leaf passes its own neighbour-table context. + luma_skip_ctx.unwrap_or(0) } else { usize::from(around.0) + usize::from(around.1) }; @@ -15510,7 +15570,7 @@ fn decode_inter_block( // 5.11.17), `Some` only when this block's var-tx tree resolved to more // than one luma transform -- read back by the residual loop, the // coefficient-context write-back and the loop-filter grid fill below. - let mut vartx_leaves: Option> = None; + let mut vartx_leaves: Option> = None; let at_mi = at; if is_inter { let above_nbr = has_above.then(|| NeighbourRef { @@ -16349,37 +16409,68 @@ fn decode_inter_block( // own slice of the block's motion-compensated predictor. let reduced = REDUCED_TX_SET_INTER.with(std::cell::Cell::get); let mut first_tx_type = TxType::DctDct; - for (idx, &(row, col, tx_px)) in leaves.iter().enumerate() { + for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() { let tu_mi = (at_mi.0 + row, at_mi.1 + col); let (tu_px, tu_py) = (px + col * MI, py + row * MI); - let mut tu_pred = Vec::with_capacity(tx_px * tx_px); - for rr in 0..tx_px { - let start = (row * MI + rr) * side + col * MI; - tu_pred.extend_from_slice(&pred_y[start..start + tx_px]); - } - let scan = default_scan(tx_px); - let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); - let (tu_grid, tu_tx_type) = read_inter_plane( - dec, - cdfs, - inter_txbset_for(tx_px, reduced), - &scan, - 0, - neighbours.around_mi(tu_mi, tx_px)[0], - mode_for_tx, - y, - tu_px, - tu_py, - tx_px, - base_q_idx, - &tu_pred, - None, - Some(tu_skip_ctx), - )?; + let (tu_grid, tu_tx_type) = if tw == th { + let tx_px = tw; + let mut tu_pred = Vec::with_capacity(tx_px * tx_px); + for rr in 0..tx_px { + let start = (row * MI + rr) * side + col * MI; + tu_pred.extend_from_slice(&pred_y[start..start + tx_px]); + } + let scan = default_scan(tx_px); + let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); + read_inter_plane( + dec, + cdfs, + inter_txbset_for(tx_px, reduced), + &scan, + 0, + neighbours.around_mi(tu_mi, tx_px)[0], + mode_for_tx, + y, + tu_px, + tu_py, + tx_px, + base_q_idx, + &tu_pred, + None, + Some(tu_skip_ctx), + )? + } else { + // lane-r14 r3: a 1:4 strip's `sub_tx_size_map` + // sub-transform is RECTANGULAR (TX_64X16 -> + // TX_32X16, TX_16X64 -> TX_16X32), so this leaf is + // read with the rect coefficient machinery, at its + // own `txb_skip_ctx`/neighbour spans -- the unit is + // smaller than its block, so `get_txb_ctx_general`'s + // neighbour table applies, not the lone-TU 0. + vartx_rect_leaf_hit(tw, th); + let tu_skip_ctx = + neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI); + let start = row * MI * side + col * MI; + read_inter_plane_rect( + dec, + cdfs, + rect_inter_luma_set(tw, th), + (tw, th), + side, + 0, + neighbours.around_mi_rect(tu_mi, tw, th)[0], + mode_for_tx, + y, + tu_px, + tu_py, + &pred_y[start..], + None, + Some(tu_skip_ctx), + )? + }; if idx == 0 { first_tx_type = tu_tx_type; } - neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid); + neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); } // `av1_get_tx_type`'s chroma lookup scales the chroma // position back to luma, so an inter block's chroma @@ -16403,6 +16494,7 @@ fn decode_inter_block( py, &pred_y, None, + None, )?; luma_tx_type = tx_type; luma_grid = embed_rect_grid(&grid, write_w, write_h, side); @@ -16446,6 +16538,7 @@ fn decode_inter_block( cpy, &pred_u, Some(luma_tx_type), + None, )? .0, uw, @@ -16467,6 +16560,7 @@ fn decode_inter_block( cpy, &pred_v, Some(luma_tx_type), + None, )? .0, uw, @@ -17327,37 +17421,68 @@ fn decode_inter_block( // own slice of the block's motion-compensated predictor. let reduced = REDUCED_TX_SET_INTER.with(std::cell::Cell::get); let mut first_tx_type = TxType::DctDct; - for (idx, &(row, col, tx_px)) in leaves.iter().enumerate() { + for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() { let tu_mi = (at_mi.0 + row, at_mi.1 + col); let (tu_px, tu_py) = (px + col * MI, py + row * MI); - let mut tu_pred = Vec::with_capacity(tx_px * tx_px); - for rr in 0..tx_px { - let start = (row * MI + rr) * side + col * MI; - tu_pred.extend_from_slice(&pred_y[start..start + tx_px]); - } - let scan = default_scan(tx_px); - let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); - let (tu_grid, tu_tx_type) = read_inter_plane( - dec, - cdfs, - inter_txbset_for(tx_px, reduced), - &scan, - 0, - neighbours.around_mi(tu_mi, tx_px)[0], - mode_for_tx, - y, - tu_px, - tu_py, - tx_px, - base_q_idx, - &tu_pred, - None, - Some(tu_skip_ctx), - )?; + let (tu_grid, tu_tx_type) = if tw == th { + let tx_px = tw; + let mut tu_pred = Vec::with_capacity(tx_px * tx_px); + for rr in 0..tx_px { + let start = (row * MI + rr) * side + col * MI; + tu_pred.extend_from_slice(&pred_y[start..start + tx_px]); + } + let scan = default_scan(tx_px); + let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); + read_inter_plane( + dec, + cdfs, + inter_txbset_for(tx_px, reduced), + &scan, + 0, + neighbours.around_mi(tu_mi, tx_px)[0], + mode_for_tx, + y, + tu_px, + tu_py, + tx_px, + base_q_idx, + &tu_pred, + None, + Some(tu_skip_ctx), + )? + } else { + // lane-r14 r3: a 1:4 strip's `sub_tx_size_map` + // sub-transform is RECTANGULAR (TX_64X16 -> + // TX_32X16, TX_16X64 -> TX_16X32), so this leaf is + // read with the rect coefficient machinery, at its + // own `txb_skip_ctx`/neighbour spans -- the unit is + // smaller than its block, so `get_txb_ctx_general`'s + // neighbour table applies, not the lone-TU 0. + vartx_rect_leaf_hit(tw, th); + let tu_skip_ctx = + neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI); + let start = row * MI * side + col * MI; + read_inter_plane_rect( + dec, + cdfs, + rect_inter_luma_set(tw, th), + (tw, th), + side, + 0, + neighbours.around_mi_rect(tu_mi, tw, th)[0], + mode_for_tx, + y, + tu_px, + tu_py, + &pred_y[start..], + None, + Some(tu_skip_ctx), + )? + }; if idx == 0 { first_tx_type = tu_tx_type; } - neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid); + neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); } // `av1_get_tx_type`'s chroma lookup scales the chroma // position back to luma, so an inter block's chroma @@ -17381,6 +17506,7 @@ fn decode_inter_block( py, &pred_y, None, + None, )?; luma_tx_type = tx_type; luma_grid = embed_rect_grid(&grid, write_w, write_h, side); @@ -17424,6 +17550,7 @@ fn decode_inter_block( cpy, &pred_u, Some(luma_tx_type), + None, )? .0, uw, @@ -17445,6 +17572,7 @@ fn decode_inter_block( cpy, &pred_v, Some(luma_tx_type), + None, )? .0, uw, @@ -17654,7 +17782,7 @@ fn decode_inter_block( let split_luma = vartx_leaves.is_some(); if let Some(leaves) = vartx_leaves.clone() { let reduced = REDUCED_TX_SET_INTER.with(std::cell::Cell::get); - for &(row, col, tx_px) in &leaves { + for &(row, col, tx_px, _) in &leaves { let tu_mi = (at_mi.0 + row, at_mi.1 + col); let (tu_px, tu_py) = (px + col * MI, py + row * MI); // A transform unit inside a block is not a block at that @@ -17896,13 +18024,13 @@ fn decode_inter_block( // units, not the block. Chroma still reads `tx_h_grid / 2` (the block's // uv transform is not split) -- a known corner-cut of this round. if let Some(leaves) = &vartx_leaves { - for &(row, col, tx_px) in leaves { + for &(row, col, tw, th) in leaves { neighbours.fill_lf_grid_rect( (at_mi.0 + row, at_mi.1 + col), - tx_px / MI, - tx_px / MI, - tx_px as u8, - tx_px as u8, + tw / MI, + th / MI, + tw as u8, + th as u8, if globalmv_for_lf { -ref_frame_for_lf } else { diff --git a/crates/ec-av1/src/gate_coverage.rs b/crates/ec-av1/src/gate_coverage.rs index cb8e4e4d..aa66be64 100644 --- a/crates/ec-av1/src/gate_coverage.rs +++ b/crates/ec-av1/src/gate_coverage.rs @@ -176,10 +176,6 @@ const NEVER_EXERCISED_8BIT: &[(&str, &str)] = &[ "enable-rect-tx", "never spelled; rect transforms reach the decoder only through partition shape, never through a gate that names the tool", ), - ( - "enable-tx64", - "never spelled; TX_64X64 appears only where aomenc's default picks it, which is unknown per stream", - ), ]; #[cfg(test)] @@ -222,7 +218,6 @@ const NEVER_EXERCISED_10BIT: &[(&str, &str)] = &[ // lane-cwarp's 10-bit comp // 1D tx class). ("enable-intrabc", "hole at both depths, see the 8-bit list"), ("enable-rect-tx", "hole at both depths, see the 8-bit list"), - ("enable-tx64", "hole at both depths, see the 8-bit list"), ]; /// [`DEFAULT_ON_TOOLS`] entries no 8-bit gate spells on, with the reason. diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 12c9c275..97d0067e 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -72,10 +72,6 @@ const REFUSALS: &[&str] = &[ "warp prediction with a scaled reference (superres, unimplemented)", "an 8x8 partition leaf under a scaled reference (superres, unimplemented)", "a motion_mode symbol for a block shape with no CDF row here", - // lane-r14 r2: the whole-transform case of every 64-axis inter strip is - // coded; only the SPLIT of a 1:4 strip with a 64-px axis is left, whose - // `sub_tx_size_map` entry is rectangular (TX_64X16 -> TX_32X16). - "a split transform on a 1:4 inter strip with a 64-px axis", ]; /// Gates whose `Err` arm turns a decode failure into a printed SKIP rather than diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 1eed0bf4..b74a7f9e 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -74,6 +74,13 @@ pub fn rect64_inter_tu_hits() -> usize { crate::decode::rect64_inter_tu_hits() } +/// lane-r14 r3: RECTANGULAR var-tx leaf transform units, `[32x16, 16x32]` -- +/// the leaves `sub_tx_size_map` produces when a 1:4 strip with a 64-px axis +/// splits its transform (`[TX_64X16] == TX_32X16`, `[TX_16X64] == TX_16X32`). +pub fn vartx_rect_leaf_hits() -> [usize; 2] { + crate::decode::vartx_rect_leaf_hits() +} + pub fn rect4_32_counters() -> (usize, usize, usize) { ( crate::decode::rect4_32_horz_hits(), @@ -6404,6 +6411,206 @@ mod tests { } } + /// lane-r14 r3: a 1:4 inter strip with a 64-px axis (64x16 / 16x64) whose + /// transform SPLITS -- the arm r2 left refused by name ("a split transform + /// on a 1:4 inter strip with a 64-px axis"). libaom's + /// `sub_tx_size_map[TX_64X16] == TX_32X16` (and `[TX_16X64] == TX_16X32`) + /// keeps the sub-transform RECTANGULAR, so the var-tx tree's leaves are + /// 32x16/16x32 units read through [`TxbSet::LumaRect32x16Inter`], each with + /// its own `get_txb_ctx_general` neighbour context (the unit is smaller + /// than its block, so `txb_skip_ctx` is NOT the lone-TU 0). + /// + /// `--enable-1to4-partitions=1 --min-partition-size=16 + /// --max-partition-size=64 --enable-tx64=1` (each spelled once, last + /// occurrence wins) is what lets aomenc pick a superblock-level HORZ_4 / + /// VERT_4; the textured translating source is what keeps the strips + /// non-skip so the residual is actually coded. 8-bit AND 10-bit, every + /// decoded frame compared to ffmpeg on Y/U/V, refusals counted (never + /// SKIPped), attempts that carried no rectangular var-tx leaf counted + /// separately and still required pixel-exact. + #[test] + fn a_real_aomenc_inter_1to4_recipe_decodes_pixel_exact_or_refuses_by_name() { + const NAME: &str = + "a_real_aomenc_inter_1to4_recipe_decodes_pixel_exact_or_refuses_by_name"; + 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); + for bit_depth in [8u32, 10u32] { + let (mut named_refusals, mut compared, mut out_of_scope, mut oos_mismatch) = + (0u32, 0u32, 0u32, 0u32); + let (mut leaf_32x16, mut leaf_16x32) = (0usize, 0usize); + for attempt in 0..16u32 { + // Two orientations (the transposed pair a one-sided gate could + // never separate) x four quantisers x two motion steps. + let vertical = attempt % 2 == 1; + let cq = [30, 36, 44, 52][(attempt / 2 % 4) as usize]; + let sp = 3 + (attempt / 8) * 3; + // 16-pixel bands, each band translating at its OWN speed + // across the other axis: that is what makes aomenc's RD split + // a 64x64 superblock into four 1:4 strips (`PARTITION_HORZ_4` + // / `VERT_4`) with distinct motion vectors, and the texture + // inside a band is what keeps the strip non-skip and its + // transform split. + let geq = if vertical { + format!( + "128+60*sin((Y+N*({sp}+9*mod(floor(X/16),2)))/7)+25*sin(X/11)" + ) + } else { + format!( + "128+60*sin((X+N*({sp}+9*mod(floor(Y/16),2)))/7)+25*sin(Y/11)" + ) + }; + let duration = frame_count as f64 / 25.0; + let source = format!( + "color=c=gray:s={width}x{height}:d={duration}:r=25,format=gray,\ + geq=lum='{geq}',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 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. + "--enable-tx64=1", "--enable-rect-partitions=1", + "--enable-ab-partitions=0", "--enable-1to4-partitions=1", + "--enable-dual-filter=0", "--enable-obmc=0", + "--min-partition-size=32", "--max-partition-size=64", + "--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 = crate::stream::vartx_rect_leaf_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 = crate::stream::vartx_rect_leaf_hits(); + let (h32, v32) = (after[0] - before[0], after[1] - before[1]); + 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 h32 + v32 == 0 { + // Counted per ATTEMPT, on a decoded+compared attempt only + // (class counter-from-refused-stream). + out_of_scope += 1; + if mismatched { + oos_mismatch += 1; + eprintln!( + "{NAME}: {bit_depth}-bit attempt {attempt} MISMATCHES with zero \ + rectangular var-tx leaves -- another shape's defect" + ); + } + continue; + } + 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)"); + assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (attempt {attempt}, {bit_depth}-bit)"); + assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (attempt {attempt}, {bit_depth}-bit)"); + } + leaf_32x16 += h32; + leaf_16x32 += v32; + compared += 1; + } + eprintln!( + "{NAME} ({bit_depth}-bit): {named_refusals} named refusals, {compared} \ + pixel-exact attempts carrying the arm, 32x16 var-tx leaves={leaf_32x16}, \ + 16x32 var-tx leaves={leaf_16x32}, {out_of_scope} attempts carried none \ + ({oos_mismatch} of them mismatched)" + ); + assert_eq!( + oos_mismatch, 0, + "{NAME}: {oos_mismatch} attempt(s) with no rectangular var-tx leaf decoded \ + but mismatched ffmpeg -- a defect of another shape, not a pass" + ); + // MEASURED, lane-r14 r3 (5 recipe sweeps, 60+ aomenc runs over + // lavfi banded-motion / testsrc2 / life / mandelbrot, 192x128 and + // 384x256, cq 26..58, both min-partition-size arms): aomenc picks a + // superblock-level 1:4 partition ONLY on content rich enough that + // the same stream also contains at least one of {an intra HORZ/VERT + // strip, a 16x16-level AB/1:4 partition, an SB-level AB partition, + // an inter partition below 8x8, a 32x8/8x32 rect residual}, each of + // which is ANOTHER lane's live refusal and stops the decode first. + // So this gate cannot yet assert the arm FIRED on a pixel-exact + // attempt; it asserts what it can (nothing mis-decodes silently), + // prints the leaf counters, and the firing proof for this round is + // the real film (lanes/r14-r3.report.md: 14 rectangular 16x32 + // var-tx leaves decoded out of the ReelB -ss 4500 segment). + // Restore the hard assert below the moment one of those refusals + // lifts -- do not weaken it further. + assert!( + compared + u32::from(named_refusals > 0) > 0, + "{NAME} ({bit_depth}-bit): no attempt produced anything at all" + ); + eprintln!( + "{NAME} ({bit_depth}-bit): arm-firing assert PARKED (see the comment at this \ + line): 32x16={leaf_32x16}, 16x32={leaf_16x32}" + ); + } + } + /// lane-inter4 r2: the 32x32-level inter `PARTITION_HORZ`/`PARTITION_VERT` /// strips (32x16 / 16x32) carrying a REAL residual -- the capability r1 /// found missing ("a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs diff --git a/lanes/r14-r3.report.md b/lanes/r14-r3.report.md new file mode 100644 index 00000000..3de28a67 --- /dev/null +++ b/lanes/r14-r3.report.md @@ -0,0 +1,101 @@ +# lane-r14 round 3 — the SPLIT transform of a 1:4 inter strip with a 64-px axis + +## What changed + +- `crates/ec-av1/src/decode.rs sub_tx_size_map(tx_w, tx_h)` — NEW, the full libaom + `sub_tx_size_map[TX_SIZES_ALL]` (`common_data.c`) transcribed, with a unit test + (`sub_tx_size_map_matches_libaom`) enumerating all 19 entries. The load-bearing rows are the + ones r2 refused on: `[TX_64X16] == TX_32X16`, `[TX_16X64] == TX_16X32` — RECTANGULAR. +- `crates/ec-av1/src/decode.rs read_var_tx_size` — now takes `(tx_w, tx_h)` and recurses exactly + as libaom `read_tx_size_vartx` (`decodeframe.c`) does: `ctx` from `txfm_partition_context` + (already rect-general here — above vs `tx_size_wide`, left vs `tx_size_high`, category from + `txsize_sqr_up_map`), `sub_txs = sub_tx_size_map[tx_size]`, loop + `for row in (0..tx_size_high_unit[tx_size]).step_by(bsh) { for col in (0..tx_size_wide_unit).step_by(bsw) }`, + and libaom's `sub_txs == TX_4X4` early return generalised to `tx_h/4 x tx_w/4` cells. + Leaves are now `(row, col, tw, th)` instead of the square `(row, col, tx_px)`. +- `crates/ec-av1/src/decode.rs read_block_tx_size_rect` — the refusal + `"a split transform on a 1:4 inter strip with a 64-px axis"` is GONE; the depth-1 recursion + starts from `sub_tx_size_map(bw, bh)` and can therefore hand back rectangular leaves. +- `crates/ec-av1/src/decode.rs` — both duplicated inter var-tx leaf loops (the two the r2 report + named, `decode_inter_block`'s compound and non-compound arms) now branch on `tw != th`: a + rectangular leaf reads through `read_inter_plane_rect` with `rect_inter_luma_set(tw, th)` + (`LumaRect32x16Inter`, the 2-symbol `EXT_TX_SET_DCT_IDTX` inter row — `av1_get_ext_tx_set_type` + at `tx_size_sqr_up == TX_32X32`), its own `luma_skip_ctx_rect` / `around_mi_rect` / + `record_mi_luma_rect` spans, and the block's own square prediction stride offset at the leaf. +- `crates/ec-av1/src/decode.rs read_inter_plane_rect` — gains `luma_skip_ctx: Option`: + `get_txb_ctx_general`'s `txb_skip_ctx` is 0 only when the unit IS the whole plane block, which + a var-tx leaf is not. The 6 whole-block call sites pass `None` (unchanged behaviour). +- `crates/ec-av1/src/decode.rs` + `stream.rs` — `vartx_rect_leaf_hits() -> [usize; 2]` + (`[32x16, 16x32]`), and `examples/decode_probe.rs` prints it. +- `crates/ec-av1/src/refusal_inventory.rs` — the refusal line dropped. +- `crates/ec-av1/src/gate_coverage.rs` — `enable-tx64` removed from `NEVER_EXERCISED_8BIT` and + `NEVER_EXERCISED_10BIT` (the new gate spells `--enable-tx64=1` at both depths); its own tests + are what dictated the edit. +- `crates/ec-av1/src/stream.rs` — new gate + `a_real_aomenc_inter_1to4_recipe_decodes_pixel_exact_or_refuses_by_name` (see the honest + scoping below). + +## Firing proof: his real film + +EVIDENCE: `decode_probe` on `seg4500.obu` (`ffmpeg -ss 4500 -t 2 -i -c:v copy -f obu`, 213 frame headers) | run under `systemd-run --user --scope -p MemoryMax=6G` on the r3 tip | `vartx_rect_leaf: 32x16=0 16x32=14`, `inter_rect: 64x32=24 32x64=24 64x16=0 16x64=16` — **14 rectangular var-tx leaves decoded**, and the stop moved from `a split transform on a 1:4 inter strip with a 64-px axis` to `an intra-coded HORZ/VERT strip needs rectangular intra prediction this decoder does not code yet` (lane-intrarect's wall) + +EVIDENCE: same command on `seg3000.obu` (288 frame headers) | before r3 it stopped at this round's refusal (r2 report) | now `REFUSED: an intra-coded HORZ/VERT strip needs rectangular intra prediction this decoder does not code yet` — this round's wall is off both mid-film segments + +EVIDENCE: same command on `seg300.obu` (206 frame headers) | unchanged wall (`intra-coded HORZ/VERT strip`) | but it now decodes 4 `16x64` strips and `vartx_rect_leaf 16x32=1` on the way there + +## Gate — and what it can NOT assert (measured, not assumed) + +`EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib -- --test-threads=1 1to4 superblock_level_rect_partition refusal_inventory gate_coverage sub_tx_size_map` → 17 passed / 0 failed (log `$HOME/.cache/r14-r3-g5.log`, plus `-g6` after the gate_coverage edit). + +The new gate sweeps 16 attempts x 2 bit depths of the chartered recipe +(`--enable-tx64=1 --enable-rect-partitions=1 --enable-ab-partitions=0 --enable-1to4-partitions=1 +--min-partition-size=16 --max-partition-size=64 --sb-size=64 --cpu-used=0`, each flag spelled +once, per-arm overrides last), compares EVERY decoded frame to ffmpeg on Y/U/V, counts named +refusals, and hard-asserts `oos_mismatch == 0`. It does **not** assert the arm fired, and that is +a measured limitation of this branch, written at the assert site: + +> 5 recipe sweeps, 60+ real aomenc runs (lavfi banded-motion at 16-px band granularity, +> `testsrc2`, `life`, `mandelbrot`, `smptebars`; 192x128 and 384x256; cq 26..58; both +> `--min-partition-size` arms; `--enable-dual-filter=0 --enable-obmc=0` to clear an unrelated +> `switchable_interp` desync panic): aomenc only picks a superblock-level 1:4 partition on +> content that ALSO contains at least one of {an intra HORZ/VERT strip, a 16x16-level AB or 1:4 +> partition, an SB-level AB partition, an inter partition below 8x8, a 32x8/8x32 rect residual}. +> Every one of those is another live refusal in this tree and stops the decode before the frame +> completes. With `--min-partition-size=32` aomenc emits no 1:4 partition at all (measured: the +> 64x16/16x64 counters read 0 across 12 recipes), so the 64-axis 1:4 shape is unreachable there. + +So the round's firing proof is the film, and the pixel-exactness proof is the sibling gates plus +this gate's `oos_mismatch == 0` sweep. That is a **deviation from the charter's gate clause**, +stated rather than papered over: the charter's "counters proving >=1 rectangular var-tx leaf of +each of 32x16/16x32 coded, decoded+compared" is not achievable on this branch's refusal set. + +## Sibling gates (COMMON's SIBLING GATES rule) + +`superblock_level_rect_partition`, `16_level_1to4`, `32x32_level_1to4`, `superblock_level_1to4`, +`filter_intra_on_a_32_level_1to4_strip`, `refusal_inventory`, `gate_coverage` — all green on the +r3 tip (`$HOME/.cache/r14-r3-g5.log` / `-g6.log`); they share `read_var_tx_size`, +`txfm_partition_*` and the leaf loops this round rewrote. + +## Residue + +- **deferred**(needs any one of lane-intrarect / SB-AB / 16-level-AB-1:4 / sub8 to merge — then + flip the parked assert back on): a pixel-exact aomenc gate that hard-asserts a rectangular + var-tx leaf. The assert and its restore instruction are in the gate body. +- **deferred**(needs grain synthesis in the dump path): the chartered per-frame 16-bit dump-before- + refusal diff on `seg3000`. `EC_AV1_FINAL_DUMP` already dumps every decoded frame (no new env + needed — reuse, not a new mode), but the ledger records the measurement it produces on this + film: the dump is the PRE-grain reference copy, so a compare against `ffmpeg -pix_fmt + yuv420p10le` reads as a whole-frame mismatch whose delta histogram is zero-mean +-5 film grain. + Dumping a 2 s 2160p 10-bit segment is also multi-GB. Not run. +- **flagged, not mine**: `--enable-ab-partitions=0` streams still hit + `an inter SB-level AB partition` in this decoder (memory class `refusal-from-own-desync`: a + refusal naming a DISABLED tool is usually our own desync). Seen on `mandelbrot` cq50 and on + several banded-motion recipes. Worth its own lane. +- **flagged, not mine**: a `switchable_interp's alphabet is exactly 3 symbols` PANIC (not a named + refusal) on an aomenc stream with dual filter + OBMC enabled, 192x128 cq30. Reproduced with the + new capability ABLATED, so it is pre-existing and unrelated to this round. + +## Suite + +`systemd-run --user --unit=r14-r3-suite-... -p MemoryMax=10G ... cargo test -p ec-av1 --lib -j3` +-> log `$HOME/.cache/r14-suite-r3.log`; totals appended below.