diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 16e092fe..d752bf28 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1569,6 +1569,21 @@ thread_local! { static INTRABC_DV: std::cell::Cell> = const { std::cell::Cell::new(None) }; /// How many blocks decoded `use_intrabc == 1` -- the gate's hit counter. static INTRABC_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// The `tx_type` the last plane-0 [`read_plane`] actually coded, kept so + /// an intrabc block's chroma reads can inherit it (see + /// [`INTRABC_CHROMA_TX`]). Written on every luma read; only ever read + /// through that one arming site. + static LUMA_TX_TYPE: std::cell::Cell = const { std::cell::Cell::new(TxType::DctDct) }; + /// libaom `av1_get_tx_type` (`blockd.h:1350`): for `PLANE_TYPE_UV` it + /// branches on `is_inter_block(mbmi)` -- which `is_intrabc_block` also + /// satisfies (`blockd.h:373`) -- and takes the *colocated luma + /// transform's coded* `tx_type` (`mbmi->txk_type[...]`), never + /// `intra_mode_to_tx_type` of the chroma mode. `Some` only across an + /// intrabc block's two chroma [`read_plane`] calls; the type decides the + /// `TX_CLASS`, hence the scan order, `eob_multi_ctx` and every nz context + /// -- getting it wrong desyncs the chroma coefficients, it is not a + /// reconstruction-only detail. + static INTRABC_CHROMA_TX: std::cell::Cell> = const { std::cell::Cell::new(None) }; } /// Whether this frame's header set `allow_intrabc` -- [`INTRABC_MI_GRID`] is @@ -9515,6 +9530,13 @@ fn read_plane( // narrows there — only `Chroma32` and up do). let default_tx_type = if plane_idx == 0 || side >= 32 { TxType::DctDct + } else if let Some(inherited) = INTRABC_CHROMA_TX.with(std::cell::Cell::get) { + // See [`INTRABC_CHROMA_TX`]: an intrabc block is an inter block for + // `av1_get_tx_type`, so chroma inherits luma's coded type (the + // `side >= 32` arm above is already libaom's `EXT_TX_SET_DCTONLY` + // clamp at `tx_size_sqr_up >= TX_32X32`, matching + // [`read_inter_plane`]'s own `side < 32` guard). + inherited } else { default_intra_tx_type(predict_mode as u8) }; @@ -9528,6 +9550,9 @@ fn read_plane( None, )?; note_chroma_class1(plane_idx, tx_type); + if plane_idx == 0 { + LUMA_TX_TYPE.with(|c| c.set(tx_type)); + } // A 64x64 luma block's transform covers the whole 64x64 area, but only its // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the // dequantized grid stays zero, which `inverse_transform_2d`'s own `< 32` @@ -9928,6 +9953,12 @@ fn decode_block( smooth_neighbor, )?; let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); + // Armed for exactly this block's two chroma reads (see + // [`INTRABC_CHROMA_TX`]); the luma call above just set + // [`LUMA_TX_TYPE`] to the type it coded. + if intrabc_dv.is_some() { + INTRABC_CHROMA_TX.with(|c| c.set(Some(LUMA_TX_TYPE.with(std::cell::Cell::get)))); + } if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone()); } @@ -9984,6 +10015,7 @@ fn decode_block( None, smooth_neighbor_uv, )?; + INTRABC_CHROMA_TX.with(|c| c.set(None)); neighbours.record(at, side, mode, uv_predict_mode, &[luma_grid, u_grid, v_grid]); } else { // Several luma transform units, raster order (spec diff --git a/crates/ec-av1/src/gate_coverage.rs b/crates/ec-av1/src/gate_coverage.rs index 6ed4c60d..8c98892f 100644 --- a/crates/ec-av1/src/gate_coverage.rs +++ b/crates/ec-av1/src/gate_coverage.rs @@ -42,16 +42,13 @@ /// its tool is now covered by a real stream. #[cfg(test)] const NEVER_EXERCISED: &[(&str, &str)] = &[ - // lane-intrabc r1: the DV itself (mv stack against INTRA_FRAME, `ndvc` - // full-pel read, `av1_find_ref_dv` fallback) and the block-copy - // prediction are now decoded, but every real aomenc intrabc block this - // lane could produce sits under TX_MODE_SELECT, whose inter var-tx - // partition tree is unread -- so no stream reconstructs one end to end - // yet and this entry stays. - ( - "enable-intrabc", - "the block vector is decoded and predicted from, but every real stream's intrabc block needs the unread inter var-tx transform tree", - ), + // `enable-intrabc` LEFT this list on 2026-09-02 (lane-kf900 r6). The + // premise it carried -- "every real stream's intrabc block sits under + // TX_MODE_SELECT" -- was false: with `--enable-tx-size-search=0` aomenc + // codes intrabc blocks under TX_MODE_LARGEST, and + // `a_real_aomenc_screen_key_frame_reads_use_intrabc_on_rect_strips` + // spells `--enable-intrabc=1` and decodes three such 8-bit streams whole- + // frame pixel-exact against ffmpeg, with the block counter asserted > 0. ]; /// The `--enable-*` tools this decoder cares about, whether or not any gate @@ -167,10 +164,6 @@ const NEVER_EXERCISED_8BIT: &[(&str, &str)] = &[ "enable-global-motion", "off in 5 gates, on in none at 8 bits; lane-gm owns the global-warp prediction that is still missing", ), - ( - "enable-intrabc", - "the block vector is decoded and predicted from, but every real stream's intrabc block needs the unread inter var-tx transform tree", - ), ( "enable-rect-tx", "never spelled; rect transforms reach the decoder only through partition shape, never through a gate that names the tool", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 0f4ad695..e415d663 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -2759,30 +2759,22 @@ mod tests { let (width, height) = (256usize, 192usize); // (arm, source, per-arm overrides AFTER the base recipe, expect_refusal, // must_read_use_intrabc) - let arms: [(&str, &str, &[&str], bool, bool); 3] = [ - ( - "testsrc2-screen-nopalette-cq50", - "testsrc2=s=256x192:r=25", - &["--cq-level=50", "--enable-palette=0", "--enable-tx-size-search=0"], - true, - true, - ), - ( - "smptebars-screen-nopalette-cq45", - "smptebars=size=256x192:rate=25", - &["--cq-level=45", "--enable-palette=0", "--enable-tx-size-search=0"], - false, - true, - ), - ( - "testsrc2-screen-nopalette-cq63", - "testsrc2=s=256x192:r=25", - &["--cq-level=63", "--enable-palette=0", "--enable-tx-size-search=1"], - false, - false, - ), + const TS: &str = "testsrc2=s=256x192:r=25"; + 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] = [ + ("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), + ("smptebars-cq45-txs1", SB, &["--cq-level=45", "--enable-palette=0", "--enable-tx-size-search=1"], false, false), + ("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), ]; 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); for (arm, src, extra, expect_refusal, must_read) in &arms { let render = || { let out = Command::new("ffmpeg") @@ -2823,6 +2815,9 @@ mod tests { // `allow_screen_content_tools` (and with it the frame // header's `allow_intrabc` bit) comes from here. "--tune-content=screen", + // Spelled so `gate_coverage` counts the tool as exercised + // rather than defaulted (class `tool-disabled-in-every-gate`). + "--enable-intrabc=1", ]) // aomenc keeps the LAST occurrence of a repeated flag. .args(*extra) @@ -2850,9 +2845,15 @@ mod tests { ); let stream = out.stdout; decode::reset_rect_intrabc_reads(); + crate::decode::reset_intrabc_hits(); let decoded = decode_stream(&stream); let reads = decode::rect_intrabc_reads(); + // Blocks that decoded `use_intrabc == 1` (square path), i.e. that + // read a DV and predicted from the current frame -- distinct from + // `reads`, which counts rect strips that merely READ the flag. + let blocks = crate::decode::intrabc_hits(); reads_total += reads; + blocks_total += blocks; if *must_read { assert!( reads > 0, @@ -2868,19 +2869,38 @@ mod tests { reconstruction is not ported, it must refuse by name" ); assert!(!frames.is_empty(), "{NAME}: {arm} decoded no frame"); - if *must_read { - // Arm 1: entropy coverage only (see PIXEL RESIDUE above). - eprintln!("{NAME}: {arm} decoded {} frame(s), {reads} rect use_intrabc reads", frames.len()); - continue; - } let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frames.len()); assert_eq!(frames.len(), ffmpeg_frames.len(), "{NAME}: {arm} frame count"); + // EVERY frame is compared; an arm whose decode never hit an + // intrabc block proves nothing about the DV/prediction path, + // so it is counted OUT OF SCOPE -- but a mismatch there is + // still a failure (`out_of_scope_mismatch == 0`), never a skip. + let mut same = true; for (i, (ours, theirs)) in frames.iter().zip(ffmpeg_frames.iter()).enumerate() { - assert_eq!(ours.y, theirs.y, "{NAME}: {arm} luma, frame {i}"); - assert_eq!(ours.u, theirs.u, "{NAME}: {arm} U, frame {i}"); - assert_eq!(ours.v, theirs.v, "{NAME}: {arm} V, frame {i}"); + let ok = ours.y == theirs.y && ours.u == theirs.u && ours.v == theirs.v; + if !ok { + eprintln!("{NAME}: {arm} MISMATCH at frame {i}"); + } + same &= ok; frames_compared += 1; } + if blocks > 0 { + fired_arms += 1; + assert!( + same, + "{NAME}: {arm} decoded {blocks} intrabc block(s) and did not match ffmpeg" + ); + } else { + out_of_scope += 1; + if !same { + out_of_scope_mismatch += 1; + } + } + eprintln!( + "{NAME}: {arm} {} frame(s), {reads} rect use_intrabc reads, \ + {blocks} intrabc block(s), exact={same}", + frames.len() + ); } Err(e) => { let msg = e.to_string(); @@ -2900,11 +2920,21 @@ mod tests { } eprintln!( "{NAME}: {reads_total} rect-strip use_intrabc symbol(s) read, \ - {refused} refused by name, {frames_compared} frames compared pixel-exact" + {blocks_total} intrabc block(s) decoded across {fired_arms} arm(s), \ + {refused} refused by name, {frames_compared} frames compared, \ + {out_of_scope} out of scope ({out_of_scope_mismatch} mismatched)" ); assert!(reads_total > 0, "{NAME}: gate is vacuous -- no arm read the symbol"); + assert!( + blocks_total > 0 && fired_arms > 0, + "{NAME}: gate is vacuous -- no compared arm actually decoded an intrabc block" + ); assert_eq!(refused, 1, "{NAME}: expected exactly one named intrabc refusal"); assert!(frames_compared > 0, "{NAME}: no frame was compared pixel-exact"); + assert_eq!( + out_of_scope_mismatch, 0, + "{NAME}: an arm without an intrabc block still mismatched ffmpeg" + ); } /// lane-kf900 r4, the gate for the two lifted palette refusals ("a 1:4 diff --git a/lanes/kf900-r6.report.md b/lanes/kf900-r6.report.md new file mode 100644 index 00000000..3608958a --- /dev/null +++ b/lanes/kf900-r6.report.md @@ -0,0 +1,118 @@ +# lane-kf900 r6 report -- intrabc chroma tx_type was derived as intra, not inherited from luma + +Tip on `lane-kf900` (parent `cbfe32d`). GREEN for defect A. Defect B root-caused, NOT fixed +(deferred with the exact cause below). + +## The charter's premise for defect A was stale -- measured, not assumed + +The charter said `read_intrabc_dv` desyncs at the square 16x16 intrabc block mi(32,44) of +`b.obu`. It does not. The oracle carries an `EC_TRACE_IBC` rung inside `read_intrabc_info` +(`~/.cache/aom-oracle/src/av1/decoder/decodemv.c:752`) and prints, for the ONE intrabc block +in that stream: + +``` +EC_IBC mi_row=32 mi_col=44 bsize=6 skip=0 nstack=0 nearest=(0,0) near=(0,0) dv_ref=(-512,0) dv=(-512,-608) +``` + +Ours (`EC_AV1_TRACE=1`) printed `TRACE intrabc dv=(-512, -608)` -- bit-identical, including the +`av1_find_ref_dv` fallback branch (`mvref_common.h:267`: `mi_row - mib_size < tile->mi_row_start` +picks the `(0, -(MI_SIZE*mib_size + 256))` column form, else the `(-MI_SIZE*mib_size, 0)` row +form). Note also that a wrong DV predictor CANNOT desync anything: `read_mv` reads joint + +components before adding `ref`, so the predictor only moves the reconstructed vector. + +## Defect A -- root cause (cited) + +Cross-decoder coefficient ladder (`EC_TRACE_COEFF` both sides, compare `rng` not `tell`): + +``` +plane=0 all_zero ctx=0 rng=40876 both (the intrabc block's luma TU) +plane=0 eob=16 rng=40712 both ours: tx=VDct class=vert +plane=0 after_bases rng=58543 both +plane=1 all_zero ctx=7 rng=63400 both (chroma U) +plane=1 eob ref eob=8 rng=65032 ours eob=5 rng=37532 <-- first divergence +``` + +Luma coded `V_DCT`. libaom `av1_get_tx_type` (`av1/common/blockd.h`) branches `PLANE_TYPE_UV` +on `is_inter_block(mbmi)`, and `is_inter_block` counts `is_intrabc_block` (`blockd.h:373`), so +an intrabc block's CHROMA `tx_type` is the colocated LUMA transform's coded type +(`mbmi->txk_type[...]`), never `intra_mode_to_tx_type` of the chroma mode. We took the intra +rule and got `DCT_DCT`, i.e. `TX_CLASS_2D` where libaom had `TX_CLASS_VERT` -- which changes +`eob_multi_ctx` (0 vs 1), the scan order and every nz context. The chroma coefficients desynced +at the first `eob` symbol of the first intrabc block and the rest of the tile was garbage. + +This is the same rule `read_inter_plane` already implements for ordinary inter blocks +(`inherited_luma_tx_type`, `decode.rs:15475`); the intrabc path is an intra-side reader, so it +never went through it. Class sweep: `read_inter_plane` (inter) and this site (intrabc) are the +only two inter-typed chroma reads -- rect-strip and sub-8x8 intrabc still refuse by name, so no +third site exists. + +## Changes (defect A) + +| path | why | +| --- | --- | +| `crates/ec-av1/src/decode.rs` thread-locals `LUMA_TX_TYPE` / `INTRABC_CHROMA_TX` (next to `INTRABC_HITS`) | the luma `read_plane` does not return its coded `tx_type`; same off-stack idiom as `INTRABC_DV`/`PALETTE_PRED` | +| `crates/ec-av1/src/decode.rs` `read_plane` `default_tx_type` | chroma inherits the luma type while `INTRABC_CHROMA_TX` is armed; the pre-existing `side >= 32` arm is already libaom's `EXT_TX_SET_DCTONLY` clamp, matching `read_inter_plane`'s own `side < 32` guard | +| `crates/ec-av1/src/decode.rs` `read_plane` after `read_coeffs` | records the plane-0 coded `tx_type` | +| `crates/ec-av1/src/decode.rs` `decode_block`, `logical_tx == side` branch | arms `INTRABC_CHROMA_TX` for exactly this block's two chroma reads and clears it after V (no leak into the next block) | +| `crates/ec-av1/src/stream.rs` `a_real_aomenc_screen_key_frame_reads_use_intrabc_on_rect_strips` | 3 arms -> 7, `--enable-intrabc=1` spelled in the base recipe, every frame of every decoding arm compared, per-arm `intrabc_hits` counter, out-of-scope accounting | +| `crates/ec-av1/src/gate_coverage.rs` | `enable-intrabc` removed from the 8-bit `NEVER_EXERCISED` and never-on lists (its stated premise is disproven); the 10-bit hole entry stays | + +## Gate + +``` +cargo test -p ec-av1 --lib a_real_aomenc_screen_key_frame_reads_use_intrabc -- --nocapture +``` +``` +testsrc2-cq50-txs0 REFUSED (expected): intra block copy on a HORZ/VERT/1:4 rect intra strip +smptebars-cq40-txs0 1 frame(s), 41 rect use_intrabc reads, 1 intrabc block(s), exact=true +smptebars-cq45-txs0 1 frame(s), 35 rect use_intrabc reads, 1 intrabc block(s), exact=true +smptebars-cq45-txs1 1 frame(s), 0 reads, 0 intrabc block(s), exact=true +smptebars-cq50-txs1 1 frame(s), 0 reads, 0 intrabc block(s), exact=true +smptebars-cq55-txs0 1 frame(s), 31 rect use_intrabc reads, 1 intrabc block(s), exact=true +testsrc2-cq63-txs1 1 frame(s), 0 reads, 0 intrabc block(s), exact=true +185 rect-strip use_intrabc symbol(s) read, 4 intrabc block(s) decoded across 3 arm(s), +1 refused by name, 6 frames compared, 3 out of scope (0 mismatched) +-> 1 passed +``` +`gate_coverage` (9 tests) green. No refusal string was lifted this round, so +`refusal_inventory.rs` is unchanged (47 entries): rect-strip intrabc reconstruction and +"intrabc under TxMode::Select" both still stand, and the gate proves the first one still fires. + +## Defect B -- root-caused, NOT fixed + +Stream: `testsrc2=s=256x192:r=25`, cq45 cpu-used 0, `--tune-content=screen --enable-palette=1 +--enable-tx-size-search=1 --enable-intrabc=1` (saved `$HOME/.cache/kf900-r6/DIFF_45_1_1_test.obu`). + +Order question in the charter, answered from the source: `parse_decode_block` +(`av1/decoder/decodeframe.c:1158` then `:1176`) calls `av1_visit_palette(..., av1_decode_palette_tokens)` +BEFORE `read_tx_size`. Our rect reader already has that order -- it is not the defect. + +The ladder is exact through mi(0,8) `angle_uv` (rng 42372 both). The block is a 32x8 rect strip +with a Y palette. The first colour-index-map symbol is then read at rng **34824** (oracle) vs +**51354** (ours), i.e. the divergence is inside `read_palette_mode_info`. Our +`read_palette_colors_y`/`_uv` match `decodemv.c` line for line (delta `+1` on Y only, telescoped +`range`, the `AOMMIN(bits, ceil_log2(range))` adaptation, the V delta/raw branch) -- so the +differing quantity is the COLOUR CACHE size, i.e. the neighbour palette. + +Root cause: `Neighbours::{above,left}_palette_{size,colors}` are indexed on the **16-px SUB +grid** (`decode.rs:4029-4034`), while libaom keeps `palette_size` per 4-px mi cell. The +already-documented corner-cut in `record_palette_y_rect` ("safe today because every sub-16 leaf +path refuses palette outright") is no longer true: this frame codes 16x8 rect strips WITH +palettes. For mi(0,8) libaom's `xd->left_mbmi` is mi(0,7) = the 16x8 block at mi(0,4), palette +size 3; our `left_palette_size[0]` had already been overwritten by mi(2,4) (palette size 2, +same 16-px cell), so we read 2 cache bits where libaom read 3 and the arithmetic decoder +desynced there. + +`deferred: defect B (palette colour cache at sub-16 granularity) -- the fix is the corner-cut's +own stated upgrade path, widening the four Y and four UV palette neighbour arrays from SUB to mi +granularity and passing the block's real mi position from all ~8 record/lookup call sites -- +unblocked by nothing but budget; a fresh round can start straight at decode.rs:3863-3943, +4010-4045, 4118-4175, 4348-4351, 5806-5817.` + +## EVIDENCE + +* `EVIDENCE: $HOME/.cache/kf900-r5/{b.obu,b2.yuv,b2_ref.yuv} | decode_probe vs ffmpeg -i on the same bytes, before/after the chroma-inheritance fix | before: OK + wrong pixels; after: cmp reports the 73728-byte frame identical (PIXEL_EXACT)` +* `EVIDENCE: $HOME/.cache/kf900-r5/{co.txt,cr.txt} | EC_TRACE_COEFF ladder both decoders on b.obu | luma exact through after_bases rng=58543, first divergence at the chroma eob: ref 65032 (eob 8) vs ours 37532 (eob 5)` +* `EVIDENCE: oracle EC_TRACE_IBC on $HOME/.cache/kf900-r5/b.obu vs our EC_AV1_TRACE | one intrabc block, mi(32,44) | dv=(-512,-608) identical -- the chartered DV defect does not exist` +* `EVIDENCE: $HOME/.cache/kf900-r6/{all_o.txt,all_r.txt} | EC_TRACE_MODE_STEP + EC_TRACE_PALETTE both decoders on DIFF_45_1_1_test.obu | exact to mi(0,8) angle_uv rng=42372, palette map starts at 34824 (ref) vs 51354 (ours)` +* `EVIDENCE: $HOME/.cache/kf900-r6/sweep.log | 32-point aomenc sweep (2 sources x cq40/45/50/55 x txs x palette, --enable-intrabc=1) decoded and cmp'd against ffmpeg | every pal=0 arm that decodes is EXACT; the 5 remaining DIFF arms are all pal=1`