diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index 4d534d2d..e105f7b0 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -34,6 +34,7 @@ fn main() { let (h, v, c) = ec_av1::stream::rect4_32_counters(); println!("rect4_32: horz={h} vert={v} coded={c}"); println!("rect_intrabc_reads: {}", ec_av1::stream::rect_intrabc_reads()); + println!("leaf8_intrabc_hits: {}", ec_av1::stream::leaf8_intrabc_hits()); 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(); diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index ca1d09ac..004e454d 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1657,6 +1657,22 @@ pub(crate) fn reset_rect_intrabc_reads() { RECT_INTRABC_READS.with(|c| c.set(0)); } +thread_local! { + /// Blocks that USED intrabc at an 8x8 LEAF (`decode_leaf8`) specifically + /// -- the shape lane-kf900 r7 fixed; [`INTRABC_HITS`] counts every shape. + static LEAF8_INTRABC_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current value of [`LEAF8_INTRABC_HITS`]. +pub(crate) fn leaf8_intrabc_hits() -> usize { + LEAF8_INTRABC_HITS.with(|c| c.get()) +} + +/// Zeroes [`LEAF8_INTRABC_HITS`] (gate tests call this before a decode). +pub(crate) fn reset_leaf8_intrabc_hits() { + LEAF8_INTRABC_HITS.with(|c| c.set(0)); +} + /// Current value of [`INTRABC_HITS`]. pub(crate) fn intrabc_hits() -> usize { INTRABC_HITS.with(|c| c.get()) @@ -3591,7 +3607,7 @@ fn read_coeffs( let t = dec.symbol(tx_type_cdf); if std::env::var_os("EC_TRACE_COEFF").is_some() { let (rng, _) = dec.debug_state(); - eprintln!("EC_COEFF_STEP tag=tx_type rng={rng}"); + eprintln!("EC_COEFF_STEP tag=tx_type len={len} rng={rng}"); } if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { let (range, value) = dec.debug_state(); @@ -10652,6 +10668,24 @@ fn decode_leaf8( leaf_mi.0, leaf_mi.1, )?; + // lane-kf900 r7: an 8x8 leaf consumed `use_intrabc` (and its DV) through + // `read_intra_mode` and then decoded the block as if it were ordinary + // intra. libaom's `is_inter_block` counts an intrabc block + // (`blockd.h:373`), so its luma `tx_type` comes off the INTER ext-tx set + // (`av1_get_ext_tx_set_type(TX_8X8, is_inter=1, 0)` = `EXT_TX_SET_ALL16`, + // a 16-symbol CDF) while we read the 7-symbol `TX_SET_INTRA_1` row: the + // same value with a different interval, so the tile desynced at the very + // next symbol (class `wrong-alphabet-same-value`). Everything below + // mirrors `decode_block`'s own intrabc handling at 16x16 and up. + let intrabc_dv = INTRABC_DV.with(|c| c.take()); + if intrabc_dv.is_some() { + LEAF8_INTRABC_HITS.with(|c| c.set(c.get() + 1)); + } + if intrabc_dv.is_some() && tx_select && !skip { + return Err(unsupported( + "an intrabc block under TxMode::Select (its transform size is coded by the inter var-tx partition tree, which this decoder never reads)", + )); + } let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { @@ -10665,7 +10699,11 @@ fn decode_leaf8( // which splits the leaf's own 8x8 prediction into a 2x2 grid of 4x4 // transform units, same raster-order per-TU pattern `decode_block`'s // multi-TU branch runs). - let resolved = if tx_select { + // ... and an intrabc leaf reads no intra `tx_depth` symbol at all: under + // `TX_MODE_SELECT` a skipped inter block takes the largest transform + // (`read_tx_size(.., is_inter=1, ..)`), and the unskipped case is refused + // above. + let resolved = if tx_select && intrabc_dv.is_none() { read_tx_size(dec, cdfs, neighbours, leaf_mi, 8, None) } else { 8 @@ -10673,6 +10711,37 @@ fn decode_leaf8( let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); let reach = Reach::of(8, px, py, y.width, y.height); let (cpx, cpy) = (px / 2, py / 2); + // spec 7.11.3.4 with `use_intrabc`, same as `decode_block`: predict from + // the CURRENT frame's own pre-loop-filter reconstruction (every loop + // filter is off whenever `allow_intrabc`, frame.rs:194/210/252/272) at + // the full-pel block vector, with the BILINEAR kernel libaom forces. + let intrabc_bufs = intrabc_dv.map(|(dv_row, dv_col)| { + let mut yb = vec![0u16; 8 * 8]; + mc::predict_with_filter( + &y.data, y.width, y.true_width, y.true_height, + mv_to_q4(px, dv_col, true), mv_to_q4(py, dv_row, true), + 8, 8, mc::InterpFilterKind::Bilinear, &mut yb, + ); + let mut ub = vec![0u16; 4 * 4]; + mc::predict_with_filter( + &u.data, u.width, u.true_width, u.true_height, + mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), + 4, 4, mc::InterpFilterKind::Bilinear, &mut ub, + ); + let mut vb = vec![0u16; 4 * 4]; + mc::predict_with_filter( + &v.data, v.width, v.true_width, v.true_height, + mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), + 4, 4, mc::InterpFilterKind::Bilinear, &mut vb, + ); + (yb, ub, vb) + }); + // The per-plane prediction override rides the same [`PALETTE_PRED`] slot + // `decode_block` uses: set fresh right before each plane's own + // `reconstruct`/`read_plane` call (the slot is taken, not copied). + if let Some((yb, _, _)) = &intrabc_bufs { + set_palette_pred(yb.clone()); + } let (luma_grid, u_grid, v_grid); if skip { // lane-hgkf r2 (same shape as `decode_block`'s own skip branch): an @@ -10729,6 +10798,9 @@ fn decode_leaf8( ); } let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); + if let Some((_, ub, _)) = &intrabc_bufs { + set_palette_pred(ub.clone()); + } u.reconstruct( cpx, cpy, @@ -10741,6 +10813,9 @@ fn decode_leaf8( None, smooth_neighbor_uv, ); + if let Some((_, _, vb)) = &intrabc_bufs { + set_palette_pred(vb.clone()); + } v.reconstruct( cpx, cpy, @@ -10761,7 +10836,9 @@ fn decode_leaf8( luma_grid = read_plane( dec, cdfs, - if reduced_tx_set { + if intrabc_dv.is_some() { + txbset_for_inter(8, reduced_tx_set) + } else if reduced_tx_set { TxbSet::Luma8 } else { TxbSet::Luma8Set1 @@ -10784,7 +10861,16 @@ fn decode_leaf8( None, smooth_neighbor, )?; + // `av1_get_tx_type` branches `PLANE_TYPE_UV` on `is_inter_block`, so + // an intrabc block's chroma tx_type is the colocated LUMA transform's + // coded type (lane-kf900 r6's own fix, `decode_block`'s line). + if intrabc_dv.is_some() { + INTRABC_CHROMA_TX.with(|c| c.set(Some(LUMA_TX_TYPE.with(std::cell::Cell::get)))); + } let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); + if let Some((_, ub, _)) = &intrabc_bufs { + set_palette_pred(ub.clone()); + } u_grid = read_plane( dec, cdfs, @@ -10807,6 +10893,9 @@ fn decode_leaf8( None, smooth_neighbor_uv, )?; + if let Some((_, _, vb)) = &intrabc_bufs { + set_palette_pred(vb.clone()); + } v_grid = read_plane( dec, cdfs, @@ -10829,6 +10918,11 @@ fn decode_leaf8( None, smooth_neighbor_uv, )?; + // Disarm: `INTRABC_CHROMA_TX` is `Some` across exactly this block's + // two chroma reads (`decode_block`'s own tail does the same) -- left + // set, every later chroma block would inherit this luma tx_type, + // which picks the scan and the inverse transform. + INTRABC_CHROMA_TX.with(|c| c.set(None)); } else { // `tx_depth` resolved this leaf's luma to `TX4`: a 2x2 raster-order // grid of 4x4 transform units, each predicted from the leaf's own @@ -10979,6 +11073,7 @@ fn decode_leaf8( neighbours.fill_skip_grid(leaf_mi, 2, skip); neighbours.fill_lf_grid(leaf_mi, 2, 4, 0); neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); + record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv); return Ok(mode); } neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); @@ -11003,6 +11098,10 @@ fn decode_leaf8( // -ss 900, first bad luma sample row 186 col 1803). neighbours.fill_lf_grid(leaf_mi, 2, resolved as u8, 0); neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); + // The DV predictor of a LATER intrabc block is the ordinary `INTRA_FRAME` + // MV stack over this grid (`av1_find_mv_refs`), so an 8x8 leaf has to + // publish its own cells exactly like `decode_block` does. + record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv); Ok(mode) } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index cdb4c1b3..0639d0d9 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -86,6 +86,12 @@ pub fn rect_intrabc_reads() -> usize { crate::decode::rect_intrabc_reads() } +/// Blocks reconstructed with intra block copy at an 8x8 LEAF specifically +/// (lane-kf900 r7's shape); [`crate::decode::intrabc_hits`] counts every shape. +pub fn leaf8_intrabc_hits() -> usize { + crate::decode::leaf8_intrabc_hits() +} + pub fn rect4_32_counters() -> (usize, usize, usize) { ( crate::decode::rect4_32_horz_hits(), @@ -2845,7 +2851,7 @@ mod tests { const SB: &str = "smptebars=size=256x192:rate=25"; // (arm, source, per-arm overrides AFTER the base recipe, expect_refusal, // must_read_use_intrabc_on_a_rect_strip) - let arms: [(&str, &str, &[&str], bool, bool); 7] = [ + let arms: [(&str, &str, &[&str], bool, bool); 8] = [ ("testsrc2-cq50-txs0", TS, &["--cq-level=50", "--enable-palette=0", "--enable-tx-size-search=0"], true, true), ("smptebars-cq40-txs0", SB, &["--cq-level=40", "--enable-palette=0", "--enable-tx-size-search=0"], false, false), ("smptebars-cq45-txs0", SB, &["--cq-level=45", "--enable-palette=0", "--enable-tx-size-search=0"], false, true), @@ -2853,10 +2859,17 @@ mod tests { ("smptebars-cq50-txs1", SB, &["--cq-level=50", "--enable-palette=0", "--enable-tx-size-search=1"], false, false), ("smptebars-cq55-txs0", SB, &["--cq-level=55", "--enable-palette=0", "--enable-tx-size-search=0"], false, false), ("testsrc2-cq63-txs1", TS, &["--cq-level=63", "--enable-palette=0", "--enable-tx-size-search=1"], false, false), + // lane-kf900 r7: the 8x8-LEAF intrabc arm. Rect and 1:4 + // partitions off (so no rect strip refuses first), palette on, + // `TxMode::Largest`: aomenc puts exactly one `use_intrabc` block + // on a `BLOCK_8X8` leaf here, the shape `decode_leaf8` decoded as + // ordinary intra before this round. + ("testsrc2-cq55-leaf8", TS, &["--cq-level=55", "--enable-palette=1", "--enable-tx-size-search=0", "--enable-rect-partitions=0", "--enable-1to4-partitions=0"], false, false), ]; let (mut reads_total, mut refused, mut frames_compared) = (0usize, 0u32, 0usize); let (mut blocks_total, mut fired_arms, mut out_of_scope, mut out_of_scope_mismatch) = (0usize, 0u32, 0u32, 0u32); + let mut leaf8_total = 0usize; for (arm, src, extra, expect_refusal, must_read) in &arms { let render = || { let out = Command::new("ffmpeg") @@ -2928,6 +2941,7 @@ mod tests { let stream = out.stdout; decode::reset_rect_intrabc_reads(); crate::decode::reset_intrabc_hits(); + crate::decode::reset_leaf8_intrabc_hits(); let decoded = decode_stream(&stream); let reads = decode::rect_intrabc_reads(); // Blocks that decoded `use_intrabc == 1` (square path), i.e. that @@ -2936,6 +2950,18 @@ mod tests { let blocks = crate::decode::intrabc_hits(); reads_total += reads; blocks_total += blocks; + // The 8x8-leaf shape has its own counter: without it this gate's + // other arms (all rect/32x32 intrabc) would stay green while the + // leaf path decoded an intrabc block as ordinary intra (class + // `gate-blind-to-feature`). + let leaf8 = crate::decode::leaf8_intrabc_hits(); + leaf8_total += leaf8; + if *arm == "testsrc2-cq55-leaf8" { + assert!( + leaf8 > 0, + "{NAME}: {arm} decoded no intrabc block at an 8x8 leaf -- the arm no longer exercises `decode_leaf8`'s intrabc path" + ); + } if *must_read { assert!( reads > 0, @@ -3006,7 +3032,12 @@ mod tests { {refused} refused by name, {frames_compared} frames compared, \ {out_of_scope} out of scope ({out_of_scope_mismatch} mismatched)" ); + eprintln!("{NAME}: {leaf8_total} intrabc block(s) at an 8x8 leaf"); assert!(reads_total > 0, "{NAME}: gate is vacuous -- no arm read the symbol"); + assert!( + leaf8_total > 0, + "{NAME}: no arm decoded an intrabc block at an 8x8 leaf -- lane-kf900 r7's fix is untested" + ); assert!( blocks_total > 0 && fired_arms > 0, "{NAME}: gate is vacuous -- no compared arm actually decoded an intrabc block" diff --git a/lanes/kf900-r7.handoff.md b/lanes/kf900-r7.handoff.md new file mode 100644 index 00000000..de7a71c6 --- /dev/null +++ b/lanes/kf900-r7.handoff.md @@ -0,0 +1,67 @@ +# lane-kf900 r7 HANDOFF -- 8x8-leaf intrabc decoded as ordinary intra + +## Merge state +`git merge --no-commit main` (main `3566501`) auto-merged clean (decode.rs, stream.rs, +gate_coverage.rs), committed as `6c22a5f`. The r6 palette-at-mi-granularity hunks and the +intrabc chroma tx_type fix survive untouched (Neighbours palette fns re-read after the merge). + +## Root cause of the r6 "last superblock" residue (found, fixed) +Stream `$HOME/.cache/kf900-r6/DIFF_55_0_1_test.obu` (testsrc2 256x192 cq55 txs=0 palette=1 +screen 8-bit). The r6 report's "entropy stays in sync, reconstruction defect, deltas ~4" was +WRONG on both counts: deltas run to +-171 and it is an ENTROPY desync -- invisible because the +bad block sits in the LAST superblock, so nothing after it could disagree. + +* Stage: mode info -> first coefficient TU. Cross-decoder `EC_TRACE_MODE_STEP` ladder diverges + at mi(34,52) (luma 136,208): both decoders read skip/cdef/dq at rng=42705, then the next + block starts at oracle rng=52073 vs ours 33144. +* Block: an 8x8 **leaf** (`decode_leaf8`) that decoded `use_intrabc=1`, DV `(-512,-1360)` + (identical on both sides -- `EC_TRACE_IBC`, so the DV read is exonerated for the 3rd time). +* Element: the luma `tx_type`. `EC_TRACE_COEFF` (our trace now prints the CDF width, `len=`): + ours read an 8-slot row (`TX_SET_INTRA_1`, 7 symbols), libaom reads + `av1_get_ext_tx_set_type(TX_8X8, is_inter=1, 0)` = `EXT_TX_SET_ALL16` (16 symbols), because + `is_inter_block(mbmi)` counts `is_intrabc_block` (`av1/common/blockd.h:373`, set chooser + `av1/common/blockd.h` `av1_get_tx_type`). Same value, different interval -- class + `wrong-alphabet-same-value`. +* Why only there: `decode_leaf8` consumed `use_intrabc` + the DV through `read_intra_mode` and + then ignored the result entirely -- no inter ext-tx set, no chroma tx_type inheritance, no + prediction from the current frame, no `record_intrabc_mi`. `decode_block` (16x16 and up) had + all four since r6. + +## Fix (crates/ec-av1/src/decode.rs, `decode_leaf8`) +1. take `INTRABC_DV`; refuse under `TxMode::Select && !skip` with `decode_block`'s EXISTING + refusal string (no new inventory entry); a skipped intrabc leaf reads no intra `tx_depth`. +2. luma set `txbset_for_inter(8, reduced_tx_set)`. +3. BILINEAR prediction from the current frame at the DV (8x8 luma, 4x4 chroma) through the + `PALETTE_PRED` override slot, in both the skip and non-skip branches. +4. `INTRABC_CHROMA_TX` armed after the luma read and **disarmed** after the V read (leaving it + armed leaked the luma tx_type into every later chroma block: 320 chroma samples wrong, + luma exact -- second bug, found by re-measuring). +5. `record_intrabc_mi(leaf_mi, 2, intrabc_dv)` on both exits (a later DV's MV stack). +Plus counter `LEAF8_INTRABC_HITS` + `stream::leaf8_intrabc_hits()` + a `decode_probe` line. + +Measured, 8-bit `$HOME/.cache/kf900-r7/S_DIFF_test_55_r0_d8.obu` (same source/cq, rect+1:4 off +so no rect-strip refusal fires first): before 320 chroma samples wrong / entropy desync at the +leaf; after **0 bytes differ vs ffmpeg** (`cmp -l` = 0), `leaf8_intrabc_hits=1`, and the +`all_zero`/`eob` range ladder is identical for all 499 steps. +The ORIGINAL r6 stream now stops earlier at the pre-existing named refusal +"intra block copy on a HORZ/VERT/1:4 rect intra strip" instead of decoding wrong pixels. + +## Gate state +`a_real_aomenc_screen_key_frame_reads_use_intrabc_on_rect_strips` gained arm 8 +`testsrc2-cq55-leaf8` (`--cq-level=55 --enable-palette=1 --enable-tx-size-search=0 +--enable-rect-partitions=0 --enable-1to4-partitions=0` after the base recipe), a hard per-arm +`leaf8 > 0` assert and a gate-wide `leaf8_total > 0` vacuity assert. +`cargo test -p ec-av1 --lib -j3 -- intrabc palette --nocapture` = **8 passed, 0 failed** (45s). + +## Exact next steps +1. FULL SUITE NOT RUN this round (turn cap). Run: + `systemd-run --user --unit=kf900-suite-r7-$(date +%s) -p MemoryMax=10G --same-dir bash -lc + 'EC_NOMEMGUARD=1 EC_AV1_REQUIRE_AOMENC=1 CARGO_TARGET_DIR=$HOME/.cache/cargo-target-kf900 + nice -n 10 cargo test -p ec-av1 --lib -j3 > $HOME/.cache/kf900-suite-r7.log 2>&1'` + then `grep -E "^test result|FAILED" $HOME/.cache/kf900-suite-r7.log`. r6 baseline 385/0/33. +2. 10-bit leaf8 arm: the r7 sweep (`$HOME/.cache/kf900-r7/sweep.sh`, 3 sources x 5 cq x rect + on/off x 8/10 bit) produced NO decoding 10-bit stream at all (every 10-bit arm refuses on + another lane's gap), so the 8x8-leaf intrabc fix is 8-bit-proven only. Widen the sweep + (other sources / `--max-partition-size=16`) or wait for the sibling lanes. +3. Still open (unchanged from r6): rect-strip intrabc reconstruction refusal -- now the FIRST + thing `DIFF_55_0_1_test.obu` hits; porting BILINEAR prediction at rect shapes closes it.