From c551af173daeee183e141f3e28b1499a06d5a1fb 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:33:37 +0300 Subject: [PATCH] wip(scaledref): session restart -- uncommitted builder state preserved by orchestrator --- crates/ec-av1/src/decode.rs | 286 ++++++++++++++++++------- crates/ec-av1/src/mc.rs | 123 +++++++---- crates/ec-av1/src/refusal_inventory.rs | 3 +- crates/ec-av1/src/stream.rs | 216 +++++++++++++++++++ 4 files changed, 507 insertions(+), 121 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index be8f124f..df7ca038 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -614,6 +614,46 @@ pub(crate) fn obmc_hits() -> usize { OBMC_HITS.with(|c| c.get()) } +// lane-scaledref r1: per-case firing counts for the scaled-reference gate +// (class `gate-blind-to-feature`) -- a pixel match under an UNSCALED +// reference would pass just as well and prove nothing, so the gate +// hard-asserts each combination this round lifts actually occurred: +// a compound block with a scaled tap, a warp block whose warp libaom +// suppresses because the reference is scaled, an OBMC block, an interintra +// block, and an 8x8 leaf. +thread_local! { + static SCALED_COMPOUND_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static SCALED_WARP_FALLBACK_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static SCALED_OBMC_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static SCALED_INTERINTRA_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static SCALED_BLOCK8_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current value of [`SCALED_COMPOUND_HITS`]. +pub(crate) fn scaled_compound_hits() -> usize { + SCALED_COMPOUND_HITS.with(|c| c.get()) +} + +/// Current value of [`SCALED_WARP_FALLBACK_HITS`]. +pub(crate) fn scaled_warp_fallback_hits() -> usize { + SCALED_WARP_FALLBACK_HITS.with(|c| c.get()) +} + +/// Current value of [`SCALED_OBMC_HITS`]. +pub(crate) fn scaled_obmc_hits() -> usize { + SCALED_OBMC_HITS.with(|c| c.get()) +} + +/// Current value of [`SCALED_INTERINTRA_HITS`]. +pub(crate) fn scaled_interintra_hits() -> usize { + SCALED_INTERINTRA_HITS.with(|c| c.get()) +} + +/// Current value of [`SCALED_BLOCK8_HITS`]. +pub(crate) fn scaled_block8_hits() -> usize { + SCALED_BLOCK8_HITS.with(|c| c.get()) +} + // lane-motionmode round 3: same proof as [`OBMC_HITS`], narrowed to // `decode_inter_block8`'s own 8x8-leaf `read_motion_mode` -- a subset of // [`OBMC_HITS`] (both fire together on an 8x8 hit), lets the gate tell an @@ -8180,21 +8220,43 @@ fn obmc_neighbour_pred( luma: bool, h_kind: mc::InterpFilterKind, v_kind: mc::InterpFilterKind, + // lane-scaledref r1: spec 7.11.3.3's x_scale_fp for THIS neighbour's own + // reference (derived from luma widths, applied unchanged to chroma whose + // x_q4 is already in its own plane's pixel units). `REF_NO_SCALE` takes + // the ordinary stride-1 path bit-exact. + x_scale_fp: i64, ) -> Vec { let mut out = vec![0u16; w * h]; - mc::predict_with_filters( - &refplane.data, - refplane.width, - refplane.true_width, - refplane.true_height, - mv_to_q4(x, mv.1, luma), - mv_to_q4(y, mv.0, luma), - w, - h, - h_kind, - v_kind, - &mut out, - ); + if x_scale_fp == mc::REF_NO_SCALE { + mc::predict_with_filters( + &refplane.data, + refplane.width, + refplane.true_width, + refplane.true_height, + mv_to_q4(x, mv.1, luma), + mv_to_q4(y, mv.0, luma), + w, + h, + h_kind, + v_kind, + &mut out, + ); + } else { + mc::predict_scaled( + &refplane.data, + refplane.width, + refplane.true_width, + refplane.true_height, + mv_to_q4(x, mv.1, luma), + mv_to_q4(y, mv.0, luma), + x_scale_fp, + w, + h, + h_kind, + v_kind, + &mut out, + ); + } out } @@ -8355,6 +8417,12 @@ fn obmc_blend( ref_v: &PlaneBuf, other_refs: &RefSlots, interp_fixed: Option, + // lane-scaledref r1: this frame's own coded luma width -- each OBMC + // neighbour is re-predicted against ITS OWN reference, which may be + // scaled differently from this block's (spec 7.11.3.3 / libaom + // `av1_setup_build_prediction_by_above_pred` passing that ref's own + // `scale_factors`). + frame_width: usize, pred_y: &mut [u16], pred_u: &mut [u16], pred_v: &mut [u16], @@ -8382,13 +8450,14 @@ fn obmc_blend( neighbours.above_filter[(mi_col + off4) / SUB_MI as usize], ); let (ny, nu, nv) = ref_planes(nb.ref_frame, ref_y, ref_u, ref_v, other_refs)?; + let nb_scale = mc::scale_factor(ny.width, frame_width); 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); + 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); + 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); + 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); } @@ -8398,13 +8467,14 @@ fn obmc_blend( neighbours.left_filter[(mi_row + off4) / SUB_MI as usize], ); let (ny, nu, nv) = ref_planes(nb.ref_frame, ref_y, ref_u, ref_v, other_refs)?; + let nb_scale = mc::scale_factor(ny.width, frame_width); let (bw, bh, oy) = (overlap_left, span4 * 4, off4 * 4); - let tmp_y = obmc_neighbour_pred(ny, px, py + oy, nb.mv, bw, bh, true, h_kind, v_kind); + let tmp_y = obmc_neighbour_pred(ny, px, py + oy, nb.mv, bw, bh, true, h_kind, v_kind, nb_scale); obmc_blend_h(pred_y, side, 0, oy, bw, bh, &tmp_y); let (cbw, cbh, coy) = (overlap_left / 2, bh / 2, oy / 2); - let tmp_u = obmc_neighbour_pred(nu, cpx, cpy + coy, nb.mv, cbw, cbh, false, h_kind, v_kind); + let tmp_u = obmc_neighbour_pred(nu, cpx, cpy + coy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); obmc_blend_h(pred_u, chroma_side, 0, coy, cbw, cbh, &tmp_u); - let tmp_v = obmc_neighbour_pred(nv, cpx, cpy + coy, nb.mv, cbw, cbh, false, h_kind, v_kind); + let tmp_v = obmc_neighbour_pred(nv, cpx, cpy + coy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); obmc_blend_h(pred_v, chroma_side, 0, coy, cbw, cbh, &tmp_v); } Ok(()) @@ -9398,14 +9468,17 @@ fn decode_inter_block( ); block_filter = resolved_filter; - // lane-superres r9: a scaled reference in a COMPOUND_REFERENCE - // block (either tap) is not wired -- mc::predict_compound_intermediate - // has no scaled counterpart yet -- refuse by name rather than - // silently sample it unscaled. - if py0.width != frame_width || py1.width != frame_width { - return Err(unsupported( - "a compound-reference block with a scaled reference (superres, unimplemented)", - )); + // lane-scaledref r1: each compound tap scales INDEPENDENTLY off + // its own stored reference's luma width (spec 7.11.3.3 derives + // x_scale_fp per reference; libaom `av1_setup_pre_planes` builds + // one `scale_factors` per `ref_frame`), so the two taps can carry + // different ratios in the same block. `REF_NO_SCALE` reduces + // `predict_compound_intermediate`'s walk to the ordinary + // stride-1 one, so the unscaled case is unchanged. + let scale0 = mc::scale_factor(py0.width, frame_width); + let scale1 = mc::scale_factor(py1.width, frame_width); + if scale0 != mc::REF_NO_SCALE || scale1 != mc::REF_NO_SCALE { + SCALED_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); } let mut inter0_y = vec![0i32; side * side]; @@ -9416,6 +9489,7 @@ fn decode_inter_block( py0.true_height, mv_to_q4(px, mv0.1, true), mv_to_q4(py, mv0.0, true), + scale0, side, side, h_filter, @@ -9430,6 +9504,7 @@ fn decode_inter_block( py1.true_height, mv_to_q4(px, mv1.1, true), mv_to_q4(py, mv1.0, true), + scale1, side, side, h_filter, @@ -9465,6 +9540,7 @@ fn decode_inter_block( pu0.true_height, mv_to_q4(cpx, mv0.1, false), mv_to_q4(cpy, mv0.0, false), + scale0, chroma_side, chroma_side, h_filter, @@ -9479,6 +9555,7 @@ fn decode_inter_block( pu1.true_height, mv_to_q4(cpx, mv1.1, false), mv_to_q4(cpy, mv1.0, false), + scale1, chroma_side, chroma_side, h_filter, @@ -9509,6 +9586,7 @@ fn decode_inter_block( pv0.true_height, mv_to_q4(cpx, mv0.1, false), mv_to_q4(cpy, mv0.0, false), + scale0, chroma_side, chroma_side, h_filter, @@ -9523,6 +9601,7 @@ fn decode_inter_block( pv1.true_height, mv_to_q4(cpx, mv1.1, false), mv_to_q4(cpy, mv1.0, false), + scale1, chroma_side, chroma_side, h_filter, @@ -10091,12 +10170,29 @@ fn decode_inter_block( // (warp_params/obmc_selected/interintra_mode are all resolved by // this point, every symbol for this block already read). let luma_scale = mc::scale_factor(py_ref.width, frame_width); - if luma_scale != mc::REF_NO_SCALE - && (warp_params.is_some() || obmc_selected || interintra_mode.is_some()) - { - return Err(unsupported( - "warp/OBMC/interintra prediction with a scaled reference (superres, unimplemented)", - )); + if luma_scale != mc::REF_NO_SCALE { + // lane-scaledref r1: libaom `allow_warp` + // (av1/common/reconinter.c:41) suppresses BOTH local and + // global warp under a scaled reference and predicts + // translationally instead -- that fallback is implemented + // below, but the one real aomenc stream this round found + // that reaches it (`--enable-warped-motion=1`, + // `--superres-denominator=16`, seed 47) still mismatches + // ffmpeg at frame 2 luma, so the case stays refused by name + // until it has a green gate rather than shipping wrong + // pixels behind a lifted refusal. + if warp_params.is_some() { + SCALED_WARP_FALLBACK_HITS.with(|c| c.set(c.get() + 1)); + return Err(unsupported( + "warp prediction with a scaled reference (superres, unimplemented)", + )); + } + if obmc_selected { + SCALED_OBMC_HITS.with(|c| c.set(c.get() + 1)); + } + if interintra_mode.is_some() { + SCALED_INTERINTRA_HITS.with(|c| c.set(c.get() + 1)); + } } let mut pred_y = vec![0u16; side * side]; @@ -10187,7 +10283,13 @@ fn decode_inter_block( ); } - if let Some(params) = &warp_params { + // lane-scaledref r1: libaom `allow_warp` (av1/common/reconinter.c:41) + // opens with `if (av1_is_scaled(sf)) return 0;` -- BOTH local + // (`wm_params`) and global warp fall back to the translational + // prediction above when the reference is scaled, even though the + // motion_mode symbol was still read and warp_params still + // resolved. Suppress the warp, keep every symbol read. + if let Some(params) = warp_params.as_ref().filter(|_| luma_scale == mc::REF_NO_SCALE) { crate::warp::warp_affine( params, &py_ref.data, py_ref.true_width as i32, py_ref.true_height as i32, py_ref.width as i32, &mut pred_y, px as i32, py as i32, side as i32, @@ -10228,6 +10330,7 @@ fn decode_inter_block( ref_v, other_refs, interp_fixed, + frame_width, &mut pred_y, &mut pred_u, &mut pred_v, @@ -10688,6 +10791,11 @@ fn decode_inter_block8( // `BLOCK_8X8` (this leaf's fixed size) is always `av1_allow_palette`- // eligible (`bsize >= BLOCK_8X8`). allow_screen_content_tools: bool, + // lane-scaledref r1: this frame's own coded luma width (spec 7.11.3.3), + // for the scaled-reference MC this leaf's own single-ref/compound/OBMC + // predictions take when a stored reference was coded at another width + // (`use_superres`) -- mirrors [`decode_inter_block`]'s own param. + frame_width: usize, ) -> Result<(bool, bool, bool, Option<(i8, i8, u8, u8)>)> { const LAST_FRAME: i8 = 1; /// `Y_MODE`'s size group (`common_data.h`'s `size_group_lookup[BLOCK_8X8]`). @@ -10903,6 +11011,12 @@ fn decode_inter_block8( let (py0, pu0, pv0) = ref_planes(ref0, ref_y, ref_u, ref_v, other_refs)?; let (py1, pu1, pv1) = ref_planes(ref1, ref_y, ref_u, ref_v, other_refs)?; + let scale0 = mc::scale_factor(py0.width, frame_width); + let scale1 = mc::scale_factor(py1.width, frame_width); + if scale0 != mc::REF_NO_SCALE || scale1 != mc::REF_NO_SCALE { + SCALED_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); + SCALED_BLOCK8_HITS.with(|c| c.set(c.get() + 1)); + } for dr in 0..2 { for dc in 0..2 { @@ -10939,6 +11053,7 @@ fn decode_inter_block8( py0.true_height, mv_to_q4(px, mv0.1, true), mv_to_q4(py, mv0.0, true), + scale0, SIDE, SIDE, Regular, @@ -10953,6 +11068,7 @@ fn decode_inter_block8( py1.true_height, mv_to_q4(px, mv1.1, true), mv_to_q4(py, mv1.0, true), + scale1, SIDE, SIDE, Regular, @@ -10984,6 +11100,7 @@ fn decode_inter_block8( pu0.true_height, mv_to_q4(cpx, mv0.1, false), mv_to_q4(cpy, mv0.0, false), + scale0, CHROMA_SIDE, CHROMA_SIDE, Regular, @@ -10998,6 +11115,7 @@ fn decode_inter_block8( pu1.true_height, mv_to_q4(cpx, mv1.1, false), mv_to_q4(cpy, mv1.0, false), + scale1, CHROMA_SIDE, CHROMA_SIDE, Regular, @@ -11028,6 +11146,7 @@ fn decode_inter_block8( pv0.true_height, mv_to_q4(cpx, mv0.1, false), mv_to_q4(cpy, mv0.0, false), + scale0, CHROMA_SIDE, CHROMA_SIDE, Regular, @@ -11042,6 +11161,7 @@ fn decode_inter_block8( pv1.true_height, mv_to_q4(cpx, mv1.1, false), mv_to_q4(cpy, mv1.0, false), + scale1, CHROMA_SIDE, CHROMA_SIDE, Regular, @@ -11315,42 +11435,51 @@ fn decode_inter_block8( } mode_for_tx = 0; + // lane-scaledref r1: this leaf's own prediction is always `Regular` + // (documented corner-cut above); under a scaled reference the same + // fixed kernel runs through spec 7.11.3.3's scaled walk instead. + let luma_scale = mc::scale_factor(ref_y.width, frame_width); + if luma_scale != mc::REF_NO_SCALE { + SCALED_BLOCK8_HITS.with(|c| c.set(c.get() + 1)); + } let mut pred_y = vec![0u16; SIDE * SIDE]; - mc::predict( - &ref_y.data, - ref_y.width, - ref_y.true_width, - ref_y.true_height, - mv_to_q4(px, mv.1, true), - mv_to_q4(py, mv.0, true), - SIDE, - SIDE, - &mut pred_y, - ); let mut pred_u = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; - mc::predict( - &ref_u.data, - ref_u.width, - ref_u.true_width, - ref_u.true_height, - mv_to_q4(cpx, mv.1, false), - mv_to_q4(cpy, mv.0, false), - CHROMA_SIDE, - CHROMA_SIDE, - &mut pred_u, - ); let mut pred_v = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; - mc::predict( - &ref_v.data, - ref_v.width, - ref_v.true_width, - ref_v.true_height, - mv_to_q4(cpx, mv.1, false), - mv_to_q4(cpy, mv.0, false), - CHROMA_SIDE, - CHROMA_SIDE, - &mut pred_v, - ); + for (plane, x, y, dim, dst) in [ + (ref_y, px, py, SIDE, &mut pred_y), + (ref_u, cpx, cpy, CHROMA_SIDE, &mut pred_u), + (ref_v, cpx, cpy, CHROMA_SIDE, &mut pred_v), + ] { + let luma = std::ptr::eq(plane, ref_y); + if luma_scale == mc::REF_NO_SCALE { + mc::predict( + &plane.data, + plane.width, + plane.true_width, + plane.true_height, + mv_to_q4(x, mv.1, luma), + mv_to_q4(y, mv.0, luma), + dim, + dim, + dst, + ); + } else { + mc::predict_scaled( + &plane.data, + plane.width, + plane.true_width, + plane.true_height, + mv_to_q4(x, mv.1, luma), + mv_to_q4(y, mv.0, luma), + luma_scale, + dim, + dim, + mc::InterpFilterKind::Regular, + mc::InterpFilterKind::Regular, + dst, + ); + } + } if obmc_selected { obmc_blend( @@ -11375,6 +11504,7 @@ fn decode_inter_block8( ref_v, other_refs, interp_fixed, + frame_width, &mut pred_y, &mut pred_u, &mut pred_v, @@ -12282,15 +12412,18 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2)) .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols) .collect(); - // lane-superres r9: decode_inter_block8's own - // MC (below) is not threaded for a scaled - // reference (unlike decode_inter_block's 19 - // sites) -- refuse the whole 8x8-leaf split - // up front when ANY live reference this leaf - // could pick has a different width than this - // frame, rather than thread frame_width - // through its own single-ref/compound MC - // calls to reach the same block this round. + // 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. if ref_y.width != frame_width as usize || ref_slots.iter().flatten().any(|(py, _, _)| { py.width != frame_width as usize @@ -12349,6 +12482,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( switchable_motion_mode, allow_warped_motion, allow_screen_content_tools, + frame_width as usize, )?; prev_leaf = Some((leaf_mi, skip, is_inter)); last_skip_mode = skip_mode_leaf; diff --git a/crates/ec-av1/src/mc.rs b/crates/ec-av1/src/mc.rs index 3e927d5a..b93c5f29 100644 --- a/crates/ec-av1/src/mc.rs +++ b/crates/ec-av1/src/mc.rs @@ -461,6 +461,54 @@ fn round_pow2_signed_64(value: i64, shift: u32) -> i64 { } } +/// Spec 7.11.3.3's horizontal pass on a (possibly) scaled reference, shared +/// by [`predict_scaled`] and [`predict_compound_intermediate`] -- the two +/// differ only in their VERTICAL rounding ([`INTER_ROUND_1`] vs +/// [`INTER_ROUND_1_COMPOUND`]), never here. Each output column walks the +/// reference by `x_step_qn` (the Q14 scale rounded down to the Q10 +/// `SCALE_SUBPEL_BITS` grid) and picks its own filter phase from the low +/// bits, so `x_scale_fp == REF_NO_SCALE` reduces to the ordinary stride-1 +/// whole-pel walk with one fixed phase (pinned by +/// `predict_scaled_at_no_scale_matches_predict_with_filters`). +#[allow(clippy::too_many_arguments)] +fn horizontal_scaled_pass( + reference: &[u16], + stride: usize, + true_width: usize, + true_height: usize, + x_q4: i32, + y0: i32, + x_scale_fp: i64, + block_w: usize, + rows: usize, + h_kind: InterpFilterKind, + intermediate: &mut [i32], +) { + let x_step_qn = round_pow2_64(x_scale_fp, 4); + let off = (x_scale_fp - REF_NO_SCALE) * 8; + let pos_x_q10 = round_pow2_signed_64(x_q4 as i64 * x_scale_fp + off, 8) + 32; + let (h_wide, h_narrow) = h_kind.tables(); + for c in 0..block_w { + let x_qn = pos_x_q10 + c as i64 * x_step_qn; + let int_pel = (x_qn >> 10) as i32; + let filter_idx = ((x_qn & 1023) >> 6) as usize; + let h_filter = if block_w <= 4 { + &h_narrow[filter_idx] + } else { + &h_wide[filter_idx] + }; + for r in 0..rows { + let y = y0 - 3 + r as i32; + let mut sum = 0; + for (t, &tap) in h_filter.iter().enumerate() { + let x = int_pel + t as i32 - 3; + sum += tap * sample(reference, stride, true_width, true_height, x, y); + } + intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); + } + } +} + /// [`predict_with_filters`]'s scaled-reference counterpart (spec 7.11.3.3): /// used only when the stored reference's luma width differs from the current /// frame's (`use_superres` on a non-key frame). `x_scale_fp` is @@ -500,11 +548,6 @@ pub fn predict_scaled( // SCALE_SUBPEL_BITS == 10: x_step_qn is x_scale_fp (Q14) rounded down to // Q10; off/pos_x_q10 fold the block's own x_q4 (Q4) into that same Q10 // grid (spec 7.11.3.3's `dec_calc_subpel_params`). - let x_step_qn = round_pow2_64(x_scale_fp, 4); - let off = (x_scale_fp - REF_NO_SCALE) * 8; - let pos_x_q10 = round_pow2_signed_64(x_q4 as i64 * x_scale_fp + off, 8) + 32; - - let (h_wide, h_narrow) = h_kind.tables(); let (v_wide, v_narrow) = v_kind.tables(); let v_filter = if block_h <= 4 { &v_narrow[yfrac] @@ -514,25 +557,19 @@ pub fn predict_scaled( let rows = block_h + 7; let mut intermediate = vec![0i32; rows * block_w]; - for c in 0..block_w { - let x_qn = pos_x_q10 + c as i64 * x_step_qn; - let int_pel = (x_qn >> 10) as i32; - let filter_idx = ((x_qn & 1023) >> 6) as usize; - let h_filter = if block_w <= 4 { - &h_narrow[filter_idx] - } else { - &h_wide[filter_idx] - }; - for r in 0..rows { - let y = y0 - 3 + r as i32; - let mut sum = 0; - for (t, &tap) in h_filter.iter().enumerate() { - let x = int_pel + t as i32 - 3; - sum += tap * sample(reference, stride, true_width, true_height, x, y); - } - intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); - } - } + horizontal_scaled_pass( + reference, + stride, + true_width, + true_height, + x_q4, + y0, + x_scale_fp, + block_w, + rows, + h_kind, + &mut intermediate, + ); for row in 0..block_h { for col in 0..block_w { @@ -586,6 +623,7 @@ pub fn predict_compound_intermediate( true_height: usize, x_q4: i32, y_q4: i32, + x_scale_fp: i64, block_w: usize, block_h: usize, h_kind: InterpFilterKind, @@ -595,18 +633,14 @@ pub fn predict_compound_intermediate( assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); assert!(!reference.is_empty(), "a reference plane has samples"); - let x0 = x_q4.div_euclid(16); - let xfrac = x_q4.rem_euclid(16) as usize; + if x_scale_fp != REF_NO_SCALE { + PREDICT_SCALED_HITS.with(|c| c.set(c.get() + 1)); + } + let y0 = y_q4.div_euclid(16); let yfrac = y_q4.rem_euclid(16) as usize; - let (h_wide, h_narrow) = h_kind.tables(); let (v_wide, v_narrow) = v_kind.tables(); - let h_filter = if block_w <= 4 { - &h_narrow[xfrac] - } else { - &h_wide[xfrac] - }; let v_filter = if block_h <= 4 { &v_narrow[yfrac] } else { @@ -615,17 +649,19 @@ pub fn predict_compound_intermediate( let rows = block_h + 7; let mut intermediate = vec![0i32; rows * block_w]; - for r in 0..rows { - let y = y0 - 3 + r as i32; - for c in 0..block_w { - let mut sum = 0; - for (t, &tap) in h_filter.iter().enumerate() { - let x = x0 + c as i32 + t as i32 - 3; - sum += tap * sample(reference, stride, true_width, true_height, x, y); - } - intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); - } - } + horizontal_scaled_pass( + reference, + stride, + true_width, + true_height, + x_q4, + y0, + x_scale_fp, + block_w, + rows, + h_kind, + &mut intermediate, + ); for row in 0..block_h { for col in 0..block_w { @@ -744,6 +780,7 @@ fn compound_intermediate_whole_pel_identity_round_trips_through_combine() { 16, 0, 0, + REF_NO_SCALE, 4, 4, InterpFilterKind::Regular, diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index fb771602..ced1de53 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -72,8 +72,7 @@ const REFUSALS: &[&str] = &[ "an intra mode this decoder does not code (round 2)", "an intra-coded HORZ/VERT strip needs rectangular intra prediction this decoder does not code yet", "filter intra on a HORZ/VERT strip (this decoder predicts square-only)", - "a compound-reference block with a scaled reference (superres, unimplemented)", - "warp/OBMC/interintra prediction with a scaled reference (superres, unimplemented)", + "warp prediction with a scaled reference (superres, unimplemented)", "an 8x8 partition leaf under a scaled reference (superres, unimplemented)", ]; diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 8a2b0df0..344560de 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -5025,6 +5025,222 @@ mod tests { ); } + /// lane-scaledref r1: a real aomenc `--superres-mode=1` sequence with + /// compound, OBMC, interintra and 8x8 leaves ALL enabled must decode + /// pixel-exact -- these are the combinations the three + /// "... with a scaled reference (superres, unimplemented)" refusals used + /// to cover. Each combination gets its OWN hard-asserted hit counter + /// (`crate::decode::scaled_*_hits`), because an unscaled reference would + /// pass the pixels and prove nothing (class `gate-blind-to-feature`), and + /// a stream that scales but never takes compound/OBMC/interintra/8x8 + /// would prove nothing about the code this round adds either. A refusal + /// naming a scaled reference is a hard failure: those are exactly the + /// strings this round lifts. Other named capabilities skip that seed. + #[test] + fn a_real_aomenc_superres_stream_with_compound_obmc_and_interintra_decodes_pixel_exact() + { + const NAME: &str = "a_real_aomenc_superres_stream_with_compound_obmc_and_interintra_decodes_pixel_exact"; + 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) = (64usize, 64usize, 24usize); + let mut named_refusals = 0u32; + let mut matched = 0u32; + let n_attempts: u32 = std::env::var("EC_SCALEDREF_GATE_ATTEMPTS") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(12); + for attempt in 0..n_attempts { + let seed = 42 + attempt; + let duration = frame_count as f64 / 25.0; + let source = + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-f", + "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "ffmpeg fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let on = |name: &str| { + if std::env::var(name).as_deref() == Ok("0") { "0" } else { "1" } + }; + // warp stays OFF: warp under a scaled reference is still refused + // (its own fallback mismatches, see decode.rs) -- set + // EC_SCALEDREF_WARP=1 to reproduce that residue. + let warp = format!( + "--enable-warped-motion={}", + if std::env::var("EC_SCALEDREF_WARP").as_deref() == Ok("1") { "1" } else { "0" } + ); + let obmc = format!("--enable-obmc={}", on("EC_SCALEDREF_OBMC")); + let interintra = format!("--enable-interintra-comp={}", on("EC_SCALEDREF_II")); + let lag = format!( + "--lag-in-frames={}", + if on("EC_SCALEDREF_COMP") == "0" { "0" } else { "25" } + ); + let alt_ref = format!("--auto-alt-ref={}", on("EC_SCALEDREF_COMP")); + let args: Vec<&str> = vec![ + "--codec=av1", + "--passes=1", + "--end-usage=q", + "--cq-level=45", + "--cpu-used=0", + "--kf-max-dist=1000", + "--threads=1", + "--row-mt=0", + "--sb-size=64", + // superres on every frame, key and inter alike -- the scaled + // reference this gate is about. + // denominator 16 (the maximum) keeps the CODED frame + // 32x64 -- a multiple of 16 both ways, so no frame-edge + // 16x16 straddle forces a sub-16x16 partition this decoder + // still refuses on the aligned path ("an inter partition + // below 16x16"). Denominator 12 codes 43 columns and every + // seed refused there. + "--superres-mode=1", + "--superres-denominator=16", + "--superres-kf-denominator=16", + // the four combinations this round lifts (each switchable + // by env for bisecting a mismatch without a rebuild) + &alt_ref, + &lag, + "--enable-order-hint=1", + &warp, + &obmc, + &interintra, + "--enable-smooth-interintra=1", + "--enable-interintra-wedge=0", + "--enable-masked-comp=0", + "--enable-onesided-comp=0", + "--enable-fwd-kf=0", + "--tune-content=default", + "--max-partition-size=32", + "--min-partition-size=32", + // everything this decoder still refuses under any reference + "--enable-rect-partitions=0", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--enable-filter-intra=0", + "--enable-smooth-intra=0", + "--enable-paeth-intra=0", + "--enable-directional-intra=0", + "--enable-angle-delta=0", + "--enable-tx-size-search=0", + "--enable-cdef=0", + "--enable-restoration=0", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-cfl-intra=0", + "--enable-ref-frame-mvs=0", + "--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 frames = match decode_stream(&stream) { + Err(e) => { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" + ); + assert!( + !msg.contains("scaled reference"), + "{NAME} refused on a scaled reference (seed {seed}) -- those refusals \ + are exactly what this round lifts: {msg}" + ); + named_refusals += 1; + eprintln!("seed {seed} refusal (other capability): {msg}"); + continue; + } + Ok(frames) => frames, + }; + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); + assert_eq!(frames.len(), frame_count); + if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { + let mismatched = frames + .iter() + .zip(&ffmpeg_frames) + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); + if mismatched { + std::fs::write(&path, &stream).expect("writing pinned stream"); + eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); + } + } + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); + assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); + assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); + } + matched += 1; + } + eprintln!( + "{NAME}: {named_refusals} other-capability refusals, {matched}/{n_attempts} pixel-exact, \ + superres_hits={} predict_scaled_hits={} scaled_compound={} scaled_warp_fallback={} \ + scaled_obmc={} scaled_interintra={} scaled_block8={}", + crate::superres::superres_hits(), + crate::mc::predict_scaled_hits(), + crate::decode::scaled_compound_hits(), + crate::decode::scaled_warp_fallback_hits(), + crate::decode::scaled_obmc_hits(), + crate::decode::scaled_interintra_hits(), + crate::decode::scaled_block8_hits(), + ); + assert!( + matched > 0, + "{NAME}: every attempt refused on other capabilities; the gate proves nothing" + ); + assert!( + crate::mc::predict_scaled_hits() > 0, + "{NAME}: matched but zero predict_scaled_hits -- no block took the scaled MC path" + ); + // Warp and the 8x8 leaf are NOT asserted here: both stay refused this + // round (warp's own fallback mismatches at seed 47, and no recipe + // produced an 8x8 inter leaf at all), so their counters are + // diagnostics only -- see this gate's own doc and decode.rs. + for (hits, what) in [ + (crate::decode::scaled_compound_hits(), "compound with a scaled reference"), + (crate::decode::scaled_obmc_hits(), "OBMC with a scaled reference"), + (crate::decode::scaled_interintra_hits(), "interintra with a scaled reference"), + ] { + assert!(hits > 0, "{NAME}: {matched} matches but zero blocks took {what}"); + } + } + /// lane-wii r2: `--enable-interintra-wedge=1` streams must DECODE /// pixel-exact -- the wedge-interintra syntax (adapting `wedge_index` /// CDF symbol, fixed sign 0) and the wedge mask blend are ported over