From e1a08a19d7dd4a259a361f259fbab77a268f6236 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 06:42:29 +0300 Subject: [PATCH] feat(av1): split transform (TX_8X4/TX_4X8, TX_4X4) on a 16-level 1:4 strip The 4K 10-bit AV1 mkv's next stop after the partition itself: every 16x4/4x16 strip of its key frame reads tx_depth != 0. `decode_rect_split`'s own per-unit walk, inlined into `decode_rect4_16` because the chroma of this shape belongs to the strip's 4:2:0 PAIR, not to the strip (that function codes one chroma transform per block). depth 1 is the 2:1 rect unit TX_8X4/TX_4X8 (`sub_tx_size_map[TX_16X4]`, TxbSet::LumaRect4x8 -- the 8x8 class with 32 positions and the TX_4X4 tx_type row), depth 2 the square TX_4X4 through the ordinary `read_plane`; each unit predicts off the units before it and reads its own `txb_skip` context, and the block's lf grid now carries the UNIT's transform size. Gate: the same gate grew the `--enable-tx-size-search=1` half of its attempt window (the 32-level 1:4 gate's own shape). 8-bit: 15 compared attempts, 192 16x4 + 288 4x16 strips, 240 chroma pairs, 102 split-transform strips; 10-bit: 16 attempts, 148 + 280 strips, 214 pairs, 82 split strips. Every decode pixel-compared against ffmpeg, zero refusals. CfL stays `=0` on the recipe: no CfL hit counter exists, so `=1` would retire `enable-cfl-intra` from gate_coverage::NEVER_EXERCISED_* on a flag alone. Film check: the 4K 10-bit AV1 key frame at t=300s now walks past the 1:4 wall (104 16x4 + 172 4x16 strips at the 32 level alone) and stops at another lane's refusal, "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding". --- crates/ec-av1/src/decode.rs | 121 +++++++++++++++++++++++-- crates/ec-av1/src/refusal_inventory.rs | 1 - crates/ec-av1/src/stream.rs | 42 +++++++-- 3 files changed, 146 insertions(+), 18 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b7d95cff..d0c93c1a 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1960,16 +1960,19 @@ thread_local! { static RECT4_16_VERT_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; static RECT4_16_CHROMA_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; static RECT4_16_COEFF_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static RECT4_16_SPLIT_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// Current values of the 16x16-level 1:4 counters: `(16x4 strips, 4x16 -/// strips, chroma-reference strips, strips with a coded residual)`. -pub fn rect4_16_counters() -> (usize, usize, usize, usize) { +/// strips, chroma-reference strips, strips with a coded residual, strips +/// whose luma transform is split)`. +pub fn rect4_16_counters() -> (usize, usize, usize, usize, usize) { ( RECT4_16_HORZ_HITS.with(|c| c.get()), RECT4_16_VERT_HITS.with(|c| c.get()), RECT4_16_CHROMA_HITS.with(|c| c.get()), RECT4_16_COEFF_HITS.with(|c| c.get()), + RECT4_16_SPLIT_HITS.with(|c| c.get()), ) } @@ -6828,7 +6831,6 @@ fn decode_rect4_16( tx_select: bool, reduced_tx_set: bool, ) -> Result<()> { - let _ = base_q_idx; let (r, c) = at16; let (mi_row0, mi_col0) = (r * (SUB / MI), c * (SUB / MI)); let (bw, bh) = if horz { (16, 4) } else { (4, 16) }; @@ -6910,12 +6912,115 @@ fn decode_rect4_16( }; if depth != 0 { TX_DEPTH_HITS.with(|h| h.set(h.get() + 1)); - return Err(unsupported( - "a 16x4/4x16 strip whose transform is split (sub_tx_size_map[TX_16X4] == TX_8X4; the per-unit walk is not ported at this shape)", - )); + RECT4_16_SPLIT_HITS.with(|h| h.set(h.get() + 1)); } + let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth); let (px, py) = (lmi.1 * MI, lmi.0 * MI); let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); + neighbours.record_mode_mi(lmi.0, lmi.1, mi_w, mi_h, mode); + if depth != 0 { + // [`decode_rect_split`]'s per-unit walk, inlined because the + // CHROMA of this shape belongs to the strip's 4:2:0 pair, not to + // the strip (that function codes one chroma transform per block). + // depth 1 is the 2:1 rect unit TX_8X4/TX_4X8 + // (`sub_tx_size_map[TX_16X4]`), depth 2 the square TX_4X4; each + // unit predicts off the units before it in raster order. + let rect_unit = tx_w != tx_h; + let zero_tu = vec![0i32; tx_w * tx_h]; + for tu_row in 0..bh / tx_h { + for tu_col in 0..bw / tx_w { + let tu_mi = (lmi.0 + tu_row * (tx_h / MI), lmi.1 + tu_col * (tx_w / MI)); + let (tu_px, tu_py) = (px + tu_col * tx_w, py + tu_row * tx_h); + let tu_reach = Reach::of_tu( + bw, + bh, + tu_col * tx_w, + tu_row * tx_h, + tx_w, + tx_h, + reach, + ); + if skip { + y.reconstruct_rect( + tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &zero_tu, + None, filter_intra, smooth_neighbor, + ); + neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &zero_tu); + } else if rect_unit { + // `get_txsize_entropy_ctx(TX_8X4)` is the 8x8 class with + // 32 coded positions and the `TX_4X4` `tx_type` row: + // exactly [`TxbSet::LumaRect4x8`]. + let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; + let tu_skip_ctx = + neighbours.luma_skip_ctx_rect(tu_mi, tx_w / MI, tx_h / MI); + let set = if reduced_tx_set { + TxbSet::LumaRect4x8 + } else { + TxbSet::LumaRect4x8Set1 + }; + let tu_scan: &[u16] = if tx_w > tx_h { &SCAN_8X4 } else { &SCAN_4X8 }; + let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); + let (tu_grid, tu_tx_type) = read_coeffs_rect( + dec, + &mut coding, + tu_scan, + tx_w, + tx_h, + tu_skip_ctx, + dc_sign_ctx(tu_around.2), + TxType::DctDct, + )?; + let residual = dequant_and_inverse_typed_wh( + &tu_grid, + tx_w, + tx_h, + crate::decode::bit_depth(), + block_q_idx(), + plane_q_delta(0).0, + plane_q_delta(0).1, + tu_tx_type, + ); + y.reconstruct_rect( + tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &residual, + None, filter_intra, smooth_neighbor, + ); + if tu_grid.iter().any(|&l| l != 0) { + RECT4_16_COEFF_HITS.with(|h| h.set(h.get() + 1)); + } + neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &tu_grid); + } else { + let tu_around = neighbours.around_mi(tu_mi, tx_w)[0]; + let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_w / MI); + let tu_grid = read_plane( + dec, + cdfs, + txbset_for(tx_w, reduced_tx_set), + &default_scan(tx_w), + 0, + tu_around, + mode, + mode, + angle_delta_y, + tu_reach, + y, + tu_px, + tu_py, + tx_w, + tx_w, + base_q_idx, + None, + filter_intra, + Some(tu_skip_ctx), + smooth_neighbor, + )?; + if tu_grid.iter().any(|&l| l != 0) { + RECT4_16_COEFF_HITS.with(|h| h.set(h.get() + 1)); + } + neighbours.record_mi_luma(tu_mi, tx_w, &tu_grid); + } + } + } + } else { let levels = if skip { let zeros = vec![0i32; bw * bh]; y.reconstruct_rect( @@ -6961,10 +7066,10 @@ fn decode_rect4_16( RECT4_16_COEFF_HITS.with(|h| h.set(h.get() + 1)); levels }; - neighbours.record_mode_mi(lmi.0, lmi.1, mi_w, mi_h, mode); neighbours.record_mi_luma_rect(lmi, bw, bh, &levels); + } neighbours.fill_skip_grid_rect(lmi, mi_w, mi_h, skip); - neighbours.fill_lf_grid_rect(lmi, mi_w, mi_h, bw as u8, bh as u8, 0); + neighbours.fill_lf_grid_rect(lmi, mi_w, mi_h, tx_w as u8, tx_h as u8, 0); // `update_partition_context` writes `partition_context_lookup[ // BLOCK_16X4]` over the whole 16x16: full width above, a 4-px side on // the left (mirrored under VERT_4). diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index e88a3edf..67978c6e 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -35,7 +35,6 @@ const REFUSALS: &[&str] = &[ "a split intra strip whose transform unit is {tx_w}x{tx_h} (no luma coefficient tables for that shape here)", "an inter partition below 8x8 (this decoder codes no inter leaf smaller than 8x8; lane-sub8 scoped to intra)", "a 16x16-level partition value outside PARTITION_NONE..PARTITION_VERT_4", - "a 16x4/4x16 strip whose transform is split (sub_tx_size_map[TX_16X4] == TX_8X4; the per-unit walk is not ported at this shape)", "a 1:4 rect strip that actually uses a palette (reconstruction is not ported at this shape)", "a 16x16 block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet", "a 16x16 inter block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index e86ad583..e26dea86 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -18188,10 +18188,10 @@ mod tests { /// transposed scan or nz-map table cannot pass (class /// scan-weights-cross-axis), plus an explicit-seed noise layer that keeps /// aomenc out of screen-content mode. `--enable-filter-intra=1` and - /// `--enable-cfl-intra=1` are ON: this shape reads `use_filter_intra` off - /// its own CDF rows (classes 12/13) and its CfL averages the whole 16x8 - /// (8x16) luma PAIR, not the strip. `--enable-tx-size-search=0`: a split - /// transform at this shape is still refused by name. + /// is ON: this shape reads `use_filter_intra` off its own CDF rows + /// (classes 12/13). The `--enable-tx-size-search=1` half of the window + /// splits the strip's transform (depth 1 = TX_8X4/TX_4X8, depth 2 = + /// TX_4X4). /// /// Counters: 16x4 strips, 4x16 strips, chroma-reference strips (the odd /// strip of each 4:2:0 pair, the only one that codes chroma) and strips @@ -18221,8 +18221,14 @@ mod tests { let mut matched = 0u32; let (mut horz_proved, mut vert_proved) = (0usize, 0usize); let (mut chroma_proved, mut coeff_proved) = (0usize, 0usize); + let mut split_proved = 0usize; let (mut out_of_scope, mut out_of_scope_mismatch) = (0u32, 0u32); - for attempt in 0..n_attempts { + // The second half of the window turns `--enable-tx-size-search` on: + // that is what makes a 16x4 strip SPLIT its transform (depth 1 = + // TX_8X4, depth 2 = TX_4X4), the arm the first half never reaches. + for attempt in 0..n_attempts * 2 { + let tx_search = attempt >= n_attempts; + let attempt = attempt % n_attempts; let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; let axis = if attempt % 2 == 0 { "Y" } else { "X" }; @@ -18272,9 +18278,19 @@ mod tests { "--enable-palette=0", "--deltaq-mode=0", "--enable-filter-intra=1", - "--enable-cfl-intra=1", + // lane-r14: CfL stays OFF here. This shape's CfL averages + // the luma of the whole PAIR and is written, but no gate + // asserts a CfL hit counter (there is none), and passing + // `=1` would retire `enable-cfl-intra` from + // `gate_coverage::NEVER_EXERCISED_*` on a flag alone -- + // exactly the false coverage that list exists to prevent. + "--enable-cfl-intra=0", "--enable-intrabc=0", - "--enable-tx-size-search=0", + if tx_search { + "--enable-tx-size-search=1" + } else { + "--enable-tx-size-search=0" + }, "--obu", "-o", "-", @@ -18316,11 +18332,12 @@ mod tests { Ok(frames) => frames, }; let now = crate::decode::rect4_16_counters(); - let (horz, vert, chroma, coeff) = ( + let (horz, vert, chroma, coeff, split) = ( now.0 - before.0, now.1 - before.1, now.2 - before.2, now.3 - before.3, + now.4 - before.4, ); let ffmpeg_frames = if bit_depth == 10 { ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) @@ -18368,6 +18385,7 @@ mod tests { vert_proved += vert; chroma_proved += chroma; coeff_proved += coeff; + split_proved += split; matched += 1; } assert!( @@ -18394,10 +18412,16 @@ mod tests { coeff_proved > 0, "{NAME}: {bit_depth}-bit compared attempts coded no residual on any strip" ); + assert!( + split_proved > 0, + "{NAME}: {bit_depth}-bit compared attempts never split a strip's transform -- \ + the `--enable-tx-size-search=1` half of the window proved nothing" + ); eprintln!( "{NAME}: {bit_depth}-bit {matched} compared attempts, {horz_proved} 16x4 / \ {vert_proved} 4x16 strips, {chroma_proved} chroma pairs, {coeff_proved} coded \ - strips, {named_refusals} refusals, {out_of_scope} out of scope" + units, {split_proved} split-transform strips, {named_refusals} refusals, \ + {out_of_scope} out of scope" ); } }