From 97a7ed5dc7d79be3ad2330f0d1ae97d75e98bfb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Sep 2026 21:55:12 +0300 Subject: [PATCH] feat(av1): scaled-reference MC for compound, OBMC and interintra blocks Each compound tap now scales off its own stored reference's luma width (mc::horizontal_scaled_pass shared by predict_scaled and predict_compound_intermediate), OBMC neighbours are re-predicted through the scaled walk against their own reference's scale, and interintra blends over the scaled inter prediction. Warp under a scaled reference stays refused (libaom allow_warp's translational fallback is implemented but its stream still mismatches); the 8x8 leaf stays refused (its MC is threaded, but the only recipe reaching that path desyncs inside the leaf -- lane-sub8's open below-8x8 defect, not scaling). Also, found while probing 8x8 leaves and kept: obmc_mask(2) = {45,64} and one-sided chroma OBMC for an 8x8 luma block (av1_skip_u4x4_pred_in_obmc, reconinter.c:820) -- the first real 8x8 OBMC block hit unreachable!(). Gate: a_real_aomenc_superres_stream_with_compound_obmc_and_interintra_decodes_pixel_exact, 12/12 seeds pixel-exact vs ffmpeg incl. hidden alt-refs, hard-asserting predict_scaled/scaled_compound/scaled_obmc/scaled_interintra hit counters (3198/500/9/1). Refusals 47 -> 44. --- crates/ec-av1/src/decode.rs | 48 ++++++++++++++------ lanes/scaledref-r1.report.md | 85 ++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 lanes/scaledref-r1.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index aa8144ff..35511b44 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -8178,6 +8178,11 @@ fn num_proj_ref( /// decoder's own overlap sizes ever produce (luma 8/16/32, chroma 4/8/16) /// are transcribed. fn obmc_mask(len: usize) -> &'static [u8] { + // lane-scaledref r1: `obmc_mask_2` (libaom reconinter.c:753) is reached + // the moment an 8x8 leaf takes OBMC -- its chroma plane block is 4x4, so + // the LEFT pass blends a two-column strip. Missing it panicked + // `unreachable!` on the first real 8x8 OBMC stream this repo produced. + const M2: [u8; 2] = [45, 64]; const M4: [u8; 4] = [39, 50, 59, 64]; const M8: [u8; 8] = [36, 42, 48, 53, 57, 61, 64, 64]; const M16: [u8; 16] = [ @@ -8188,6 +8193,7 @@ fn obmc_mask(len: usize) -> &'static [u8] { 61, 62, 64, 64, 64, 64, 64, 64, 64, 64, ]; match len { + 2 => &M2, 4 => &M4, 8 => &M8, 16 => &M16, @@ -8449,6 +8455,13 @@ fn obmc_blend( }; let overlap_above = write_h / 2; let overlap_left = write_w / 2; + // lane-scaledref r1: `av1_skip_u4x4_pred_in_obmc` (libaom reconinter.c:820, + // `DISABLE_CHROMA_U8X8_OBMC == 0`) returns `dir == 0` when the PLANE block + // is 4x4/4x8/8x4 -- i.e. an 8x8 luma block in 4:2:0 gets ONE-SIDED chroma + // OBMC: the above pass skips U/V entirely, the left pass still blends its + // two-column chroma strip. Every larger block this decoder codes has a + // chroma plane of at least 8x8 and is unaffected. + let chroma_is_4x4 = bw4 == 2 && bh4 == 2; for (off4, span4, nb) in overlappable_above(grid, mi_row, mi_col, bw4, mi_cols, max_nb(bw4)) { @@ -8465,11 +8478,13 @@ fn obmc_blend( let (bw, bh, ox) = (span4 * 4, overlap_above, off4 * 4); let tmp_y = obmc_neighbour_pred(ny, px + ox, py, nb.mv, bw, bh, true, h_kind, v_kind, nb_scale); obmc_blend_v(pred_y, side, ox, 0, bw, bh, &tmp_y); - let (cbw, cbh, cox) = (bw / 2, overlap_above / 2, ox / 2); - let tmp_u = obmc_neighbour_pred(nu, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); - obmc_blend_v(pred_u, chroma_side, cox, 0, cbw, cbh, &tmp_u); - let tmp_v = obmc_neighbour_pred(nv, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); - obmc_blend_v(pred_v, chroma_side, cox, 0, cbw, cbh, &tmp_v); + if !chroma_is_4x4 { + let (cbw, cbh, cox) = (bw / 2, overlap_above / 2, ox / 2); + let tmp_u = obmc_neighbour_pred(nu, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); + obmc_blend_v(pred_u, chroma_side, cox, 0, cbw, cbh, &tmp_u); + let tmp_v = obmc_neighbour_pred(nv, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); + obmc_blend_v(pred_v, chroma_side, cox, 0, cbw, cbh, &tmp_v); + } } for (off4, span4, nb) in overlappable_left(grid, mi_row, mi_col, bh4, mi_rows, max_nb(bh4)) { @@ -12426,15 +12441,20 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // lane-scaledref r1: `decode_inter_block8` IS // threaded for a scaled reference now (its // single-ref, compound and OBMC MC all take - // spec 7.11.3.3's scaled walk), but no aomenc - // recipe this round produced a single 8x8 - // inter leaf -- gradients never splits below - // 16x16 (same wall as - // `a_real_aomenc_stream_with_obmc_8x8_decodes_pixel_exact`). - // An ungated lift is a capability claim - // nothing exercises, so the refusal stays - // until a firing fixture exists; deleting - // these six lines is then the whole change. + // spec 7.11.3.3's scaled walk), but the one + // recipe that reaches this path at all -- a + // 64x72 superres fixture whose bottom 16-row + // band straddles the true edge, so the + // gathered split bit lands on 8x8 leaves -- + // DESYNCS inside the leaf itself + // (`from_switchable_symbol` handed a 4th + // symbol, mc.rs:200), the same + // below-8x8 desync lane-sub8 r2 left open and + // unrelated to scaling. So the scaled MC here + // is unproven: refuse by name rather than + // ship a capability claim no gate exercises. + // Deleting these six lines is the whole lift + // once the leaf8 desync is fixed. if ref_y.width != frame_width as usize || ref_slots.iter().flatten().any(|(py, _, _)| { py.width != frame_width as usize diff --git a/lanes/scaledref-r1.report.md b/lanes/scaledref-r1.report.md new file mode 100644 index 00000000..fa4ad9a0 --- /dev/null +++ b/lanes/scaledref-r1.report.md @@ -0,0 +1,85 @@ +# lane-scaledref r1 — scaled-reference (superres) MC beyond single-ref, plus the Golomb tail + +Branch `lane-scaledref`, off main `3808cf8`. Round 1 continues the previous +builder's uncommitted state (preserved as `95b3e70`) — that snapshot is +verified, gated and kept; everything below r1 adds is on top of it. + +## What changed + +| path:line | why | +| --- | --- | +| `crates/ec-av1/src/mc.rs:474` `horizontal_scaled_pass` | spec 7.11.3.3's horizontal walk factored out of `predict_scaled` and given to `predict_compound_intermediate` (new `x_scale_fp` param), so each compound tap scales off its OWN reference's luma width | +| `crates/ec-av1/src/decode.rs` (compound branch, ~9470) | the "compound-reference block with a scaled reference" refusal replaced by two independent `mc::scale_factor` values threaded into all 6 compound MC calls (Y/U/V x 2 taps) | +| `crates/ec-av1/src/decode.rs` `obmc_neighbour_pred` / `obmc_blend` | each OBMC neighbour is re-predicted through the scaled walk against **its own** reference's scale (`frame_width` threaded in) | +| `crates/ec-av1/src/decode.rs` (~10170) | warp under a scaled reference: libaom `allow_warp` (reconinter.c:41) suppresses local AND global warp and predicts translationally; that suppression is implemented, but the case is still refused by name (see residue) | +| `crates/ec-av1/src/decode.rs` `decode_inter_block8` | single-ref/compound/OBMC MC of the 8x8 leaf threaded for a scaled reference (`frame_width` param) | +| `crates/ec-av1/src/decode.rs` `read_golomb` (~1349) | **r1**: reads up to 32 leading zeros instead of erroring at 20; refusal string deleted | +| `crates/ec-av1/src/decode.rs` `obmc_mask` + `obmc_blend` | **r1**: `obmc_mask_2 = {45,64}` added and one-sided chroma OBMC for an 8x8 luma block (`av1_skip_u4x4_pred_in_obmc`, reconinter.c:820, `DISABLE_CHROMA_U8X8_OBMC == 0` ⇒ chroma skipped in the ABOVE pass, kept in the LEFT pass). Before this, the first real 8x8 OBMC block hit `unreachable!` in `obmc_mask` | +| `crates/ec-av1/src/stream.rs` | the gate below | +| `crates/ec-av1/src/refusal_inventory.rs` | 3 refusal strings gone (compound-scaled, OBMC/interintra-scaled folded into a warp-only string, Golomb tail); 47 → 44 | + +## Gates + +1. `stream::tests::a_real_aomenc_superres_stream_with_compound_obmc_and_interintra_decodes_pixel_exact` + — real `aomenc --superres-mode=1 --superres-denominator=16 --superres-kf-denominator=16 + --auto-alt-ref=1 --lag-in-frames=25 --enable-obmc=1 --enable-interintra-comp=1 + --enable-smooth-interintra=1`, 12 seeds x 24 frames of 64x64 moving gradients, every frame + (hidden alt-refs included, `decode_stream` decodes the whole OBU sequence) compared to ffmpeg. + Hard-asserts `mc::predict_scaled_hits() > 0` and the per-case counters + `scaled_compound/scaled_obmc/scaled_interintra > 0`. + + `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib a_real_aomenc_superres_stream_with_compound -- --nocapture` + + EVIDENCE: /tmp/.../scratchpad/gate1.log | aomenc superres d=16 + compound/OBMC/interintra, 12 seeds x 24 frames decoded and compared to ffmpeg | 12/12 pixel-exact, 0 refusals, superres_hits=291 predict_scaled_hits=3198 scaled_compound=500 scaled_obmc=9 scaled_interintra=1 + +2. `decode::tests::golomb_tails_read_back_past_the_old_twenty_bit_cap` — every prefix length + 0..=31, smallest and largest value at each, written by `tile::write_golomb` (the syntax's + inverse) and read back by the reader real streams use. + + `cargo test -p ec-av1 --lib golomb` → 3 passed (this one plus the two pre-existing tile tests). + + EVIDENCE: cargo test output | write_golomb -> read_golomb round trip, 64 values across 32 prefix lengths | all equal, values ≥ 2^19 that used to return `unsupported` now decode + +## Refusals lifted (3) + +- "a compound-reference block with a scaled reference (superres, unimplemented)" — gate 1, 500 hits. +- "warp/OBMC/interintra prediction with a scaled reference (superres, unimplemented)" — narrowed to + "warp prediction with a scaled reference"; OBMC (9 hits) and interintra (1 hit) proven by gate 1. +- "a Golomb tail longer than this decoder reads" — gate 2. + +`refusal_inventory` and `gate_coverage` tests green. + +## Residue + +- fix-now → **not** done, deferred: **warp under a scaled reference** stays refused. The libaom + `allow_warp` fallback is implemented (warp suppressed, translational prediction used) but the + previous builder's `--enable-warped-motion=1 --superres-denominator=16` seed-47 stream still + mismatched ffmpeg at frame 2 luma. Shipping the lift would be wrong pixels behind a green name. + deferred: warp+superres — unblocked by bisecting that stream (set `EC_SCALEDREF_WARP=1` on gate 1 + to reproduce; `EC_AV1_GATE_DUMP=` pins the mismatching stream). +- deferred: **8x8 partition leaf under a scaled reference** — the leaf's MC *is* threaded for + scaling, but no aomenc recipe produces a firing, decodable fixture. r1 measured this instead of + assuming it: with `--min-partition-size=8` and a 64x**72** fixture (bottom 16-row band straddles + the true edge, which is the only way this decoder reaches `decode_inter_block8`), aomenc does emit + 8x8 leaves — and the decode **desyncs inside the leaf** (`from_switchable_symbol` handed a 4th + symbol, mc.rs:200), which is the same below-8x8 desync lane-sub8 r2 left open and has nothing to do + with scaling. Refusal restored with that fact in the comment; deleting those six lines is the whole + lift once the leaf8 desync is fixed. — unblocked by the lane-sub8 leaf8 desync. + That experiment did produce one real fix, kept: `obmc_mask(2)` + one-sided chroma OBMC (above). +- deferred: **"a reference picture whose height does not match this frame's own true size"** — not + attempted. Superres never scales height, so a height-mismatched reference can only come from + `--resize-mode` (reference scaling in BOTH axes). That needs a vertical counterpart to + `horizontal_scaled_pass` (per-row `y_step_qn`, `im_h` intermediate rows, libaom + `convolve_2d_scale`) *and* a gate whose frame size changes mid-stream. — unblocked by a lane sized + for resize-mode. +- accepted: **10-bit not covered.** `Picture` planes are `Vec`; 10-bit input is refused before + any of this code runs (see memory `his-av1-library-is-10-bit`, lane-hbd10). The 8/10-bit half of + the charter's gate line is unreachable from this lane. +- Reported conflict, not silently resolved: the charter states the spec's `read_golomb` caps at 32 + leading zeros. Locally verifiable sources disagree — libaom `read_golomb` (decodetxb.c:30) calls a + 21st prefix bit a CORRUPT FRAME, which is exactly what the old refusal mirrored; dav1d reads up to + 32. There is no AV1 spec copy on this box and the network is unreachable from the sandbox + (`curl https://aomediacodec.github.io/av1-spec/av1-spec.html` timed out), so the change follows the + permissive decoder (dav1d) and is bit-identical to the old reader for every tail any encoder writes. +- Off the film path: neither of the user's two films uses superres (lane-hbd10), so nothing here moves + their decode. The Golomb and OBMC-mask fixes are generic.