diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index 8b0452a7..781d7659 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -33,10 +33,11 @@ fn main() { println!("OK but EMPTY: no frames -- is {path} an IVF rather than a raw OBU stream?"); } Ok(frames) => { - println!("OK: {} frames decoded", frames.len()); - // Optional second arg: dump the decoded planes as raw yuv420p so a - // pixel diff against `ffmpeg -i s.obu -f rawvideo` needs no test harness. - if let Some(out) = std::env::args().nth(2) { + // Optional second arg, or EC_PROBE_OUT=: dump the decoded + // planes as raw yuv420p so a pixel diff against `ffmpeg -i s.obu + // -f rawvideo` needs no test harness. + let out = std::env::args().nth(2).or_else(|| std::env::var("EC_PROBE_OUT").ok()); + if let Some(out) = out { // 8-bit only: planes are u16, take the low byte. let mut buf: Vec = Vec::new(); for f in &frames { @@ -47,6 +48,7 @@ fn main() { std::fs::write(&out, &buf).expect("writing raw planes"); println!("wrote {} bytes of yuv420p to {out}", buf.len()); } + println!("OK: {} frames decoded, {}x{}", frames.len(), frames[0].width, frames[0].height); } Err(e) => println!("REFUSED: {e}"), } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index a9df2d55..b76ea5b2 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1118,6 +1118,62 @@ 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) }; + static SCALED_WARP_SUPPRESSED_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static MIXED_SCALE_COMPOUND_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current value of [`MIXED_SCALE_COMPOUND_HITS`]: compound blocks with one +/// scaled and one unscaled tap. +pub(crate) fn mixed_scale_compound_hits() -> usize { + MIXED_SCALE_COMPOUND_HITS.with(|c| c.get()) +} + +/// Current value of [`SCALED_WARP_SUPPRESSED_HITS`]: blocks that would have +/// read the 3-symbol `motion_mode_cdf` but read the 2-symbol `obmc_cdf` +/// instead because their reference is scaled (libaom `motion_mode_allowed`, +/// `blockd.h:1484`). +pub(crate) fn scaled_warp_suppressed_hits() -> usize { + SCALED_WARP_SUPPRESSED_HITS.with(|c| c.get()) +} + +/// 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 @@ -1935,6 +1991,18 @@ fn dc_sign_ctx(vote: i32) -> usize { /// (spec 5.11.40): its bit length in unary, then that many of its own bits, /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. fn read_golomb(dec: &mut SymbolDecoder) -> Result { + // lane-scaledref r1: this cap MASKS A REAL DEFECT and must not be lifted + // on its own. Reading up to 32 leading zeros (dav1d's `len < 32`; libaom + // itself calls a 21st bit a corrupt frame, decodetxb.c:30) is bit-identical + // for every tail any encoder writes -- our own writer tops out at 19 zeros + // (`tile::MAX_LEVEL == MAX_BR_LEVEL + (1 << 19)`) -- and was implemented + // and round-trip tested here (commit ee1f980, reverted in 314ee08). It + // turned `a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` + // RED: seed 67 stops here today, and with the cap raised it decodes to a + // frame-0 LUMA MISMATCH instead. A key frame cannot legitimately carry a + // level above 1<<19, so the long tail is the SYMPTOM of an earlier desync + // in that intra rect64 stream -- class `refusal-hides-a-defect`. Lift this + // together with that defect's fix, not before. let mut length = 1u32; while dec.literal(1) == 0 { length += 1; @@ -10288,21 +10356,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 } @@ -10463,6 +10553,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], @@ -10478,6 +10574,11 @@ fn obmc_blend( }; let overlap_above = write_h / 2; let overlap_left = write_w / 2; + // lane-inter8 r1 / lane-scaledref r1: `av1_skip_u4x4_pred_in_obmc` + // (`reconinter.c` 820, `DISABLE_CHROMA_U8X8_OBMC == 0` -- "one-sided + // obmc") returns `dir == 0` when the chroma plane's own block is + // BLOCK_4X4/8X4/4X8 (an 8x8, 16x8 or 8x16 luma block at 4:2:0): the + // ABOVE pass skips chroma entirely, the LEFT pass still blends it. let skip_chroma_above = matches!((write_w, write_h), (8, 8) | (16, 8) | (8, 16)); for (off4, span4, nb) in overlappable_above(grid, mi_row, mi_col, bw4, mi_cols, max_nb(bw4)) @@ -10491,20 +10592,15 @@ fn obmc_blend( neighbours.above_filter[mi_col + off4], ); 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); - // lane-inter8 r1: `av1_skip_u4x4_pred_in_obmc` (`reconinter.c` 820, - // `DISABLE_CHROMA_U8X8_OBMC == 0` -- "one-sided obmc"): when the - // chroma plane's own block size is BLOCK_4X4/8X4/4X8 (a 8x8, 16x8 or - // 8x16 luma block at 4:2:0), the ABOVE pass skips chroma entirely - // while the LEFT pass still blends it. Blending chroma here as well - // would both mis-predict every 8x8 leaf and ask for an overlap of 2. if !skip_chroma_above { 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); } } @@ -10515,13 +10611,14 @@ fn obmc_blend( neighbours.left_filter[mi_row + off4], ); 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(()) @@ -11592,14 +11689,23 @@ 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)); + } + // lane-scaledref r2: the MIXED case -- one tap scaled, the other + // not -- is the one an all-frames-scaled recipe never produces, + // and the one a single shared scale factor would get wrong. + if (scale0 == mc::REF_NO_SCALE) != (scale1 == mc::REF_NO_SCALE) { + MIXED_SCALE_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); } let mut inter0_y = vec![0i32; side * side]; @@ -11610,6 +11716,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, @@ -11634,6 +11741,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, @@ -11679,6 +11787,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, @@ -11705,6 +11814,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, @@ -11747,6 +11857,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, @@ -11773,6 +11884,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, @@ -12233,8 +12345,27 @@ fn decode_inter_block( // `motion_mode_allowed` reads the 3-symbol `motion_mode_cdf` // instead of the 2-symbol `obmc_cdf` exactly when // `num_proj_ref >= 1` under `allow_warped_motion`. + // lane-scaledref r2: libaom `motion_mode_allowed` + // (`blockd.h:1484`) requires + // `!av1_is_scaled(block_ref_scale_factors[0])` for + // WARPED_CAUSAL -- under a scaled reference (superres) the + // block reads the 2-symbol `obmc_cdf`, never the 3-symbol + // `motion_mode_cdf`. Reading the wrong alphabet here narrows + // the arithmetic coder by the wrong amount and predicts the + // rest of the tile off a diverged state: it was silently + // wrong pixels (r1's `--enable-warped-motion=1` superres + // mismatch), not a desync error. + let ref_is_scaled = + mc::scale_factor(py_ref.width, frame_width) != mc::REF_NO_SCALE; let warp_eligible = allow_warped_motion + && !ref_is_scaled && num_proj_ref(grid, mi_row, mi_col, bw4, bh4, mi_cols as usize, mi_rows as usize, ref_frame) >= 1; + if allow_warped_motion + && ref_is_scaled + && num_proj_ref(grid, mi_row, mi_col, bw4, bh4, mi_cols as usize, mi_rows as usize, ref_frame) >= 1 + { + SCALED_WARP_SUPPRESSED_HITS.with(|c| c.set(c.get() + 1)); + } if warp_eligible { let mode = dec.symbol(&mut cdfs.motion_mode[bsize_idx]); match mode { @@ -12414,12 +12545,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]; @@ -12510,7 +12658,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, @@ -12551,6 +12705,7 @@ fn decode_inter_block( ref_v, other_refs, interp_fixed, + frame_width, &mut pred_y, &mut pred_u, &mut pred_v, @@ -13125,6 +13280,11 @@ fn decode_inter_block8( // mode contexts AND the number of `drl_mode` bits read (class parsed // then discarded). tpl_frame: Option<&TplFrameArgs>, + // 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)>, (i8, Option))> { const LAST_FRAME: i8 = 1; let gm_table = build_gm_mv_table( @@ -13434,6 +13594,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 { @@ -13470,6 +13636,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, @@ -13484,6 +13651,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, @@ -13515,6 +13683,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, @@ -13529,6 +13698,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, @@ -13559,6 +13729,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, @@ -13573,6 +13744,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, @@ -13827,6 +13999,12 @@ fn decode_inter_block8( // lane-warp round 1: same 3-vs-2-symbol split as the 16x16+ leaf // (see its own doc) -- this leaf is always `LAST_FRAME`-only // (grid.set below hardcodes it). + // lane-scaledref r2 sibling of the 16x16+ leaf's fix: libaom + // drops WARPED_CAUSAL eligibility (so this reads `obmc_cdf`) + // when the reference is scaled. Not gated here because an 8x8 + // leaf under a scaled reference is refused before this function + // runs (see the block8 refusal below, ~12500) -- whoever lifts + // that refusal must add `&& !ref_is_scaled` here too. let warp_eligible = allow_warped_motion && num_proj_ref(grid, mi_row, mi_col, 2, 2, mi_cols as usize, mi_rows as usize, LAST_FRAME) >= 1; if warp_eligible { @@ -13880,42 +14058,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( @@ -13940,6 +14127,7 @@ fn decode_inter_block8( ref_v, other_refs, interp_fixed, + frame_width, &mut pred_y, &mut pred_u, &mut pred_v, @@ -14989,15 +15177,23 @@ 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 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 @@ -15058,6 +15254,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &sign_bias_table, &global_motion, tpl_frame.as_ref(), + frame_width as usize, )?; prev_leaves.push((leaf_mi, skip, is_inter, leaf_refs.0, leaf_refs.1)); last_skip_mode = skip_mode_leaf; diff --git a/crates/ec-av1/src/mc.rs b/crates/ec-av1/src/mc.rs index ca954106..d4a6a215 100644 --- a/crates/ec-av1/src/mc.rs +++ b/crates/ec-av1/src/mc.rs @@ -476,6 +476,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 @@ -516,11 +564,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] @@ -530,25 +573,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 { @@ -602,6 +639,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, @@ -612,18 +650,14 @@ pub fn predict_compound_intermediate( assert!(!reference.is_empty(), "a reference plane has samples"); INTER_PRED_HITS.with(|c| c.set(c.get() + 1)); - 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 { @@ -632,17 +666,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 { @@ -775,6 +811,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 82e5486b..82bc3b66 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -74,8 +74,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 7104356f..bd6fd9dc 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -6613,6 +6613,242 @@ 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" } + }; + // lane-scaledref r2: warp is ON. A warp-capable frame header + // under a scaled reference is exactly the case r1 got wrong + // (the block must read the 2-symbol `obmc_cdf`, not the + // 3-symbol `motion_mode_cdf` -- libaom `motion_mode_allowed`, + // blockd.h:1484), and it decoded silently wrong pixels rather + // than refusing. `EC_SCALEDREF_WARP=0` turns it off for + // bisecting only. + let warp = format!("--enable-warped-motion={}", on("EC_SCALEDREF_WARP")); + // lane-scaledref r2: `EC_SCALEDREF_MODE=3` (qthresh) lets + // superres switch on and off per frame, so a compound block can + // reference one scaled and one unscaled reference -- the mixed + // case mode 1 (every frame scaled) never produces. + let superres_mode = format!( + "--superres-mode={}", + std::env::var("EC_SCALEDREF_MODE").unwrap_or_else(|_| "1".into()) + ); + 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, + "--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={} scaled_warp_suppressed={} mixed_scale_compound={}", + 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(), + crate::decode::scaled_warp_suppressed_hits(), + crate::decode::mixed_scale_compound_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" + ); + // The 8x8 leaf is NOT asserted here: it stays refused (no recipe + // produced an 8x8 inter leaf at all), so its counter is a + // diagnostic only -- see this gate's own doc and decode.rs. + // `scaled_warp_suppressed` IS asserted: it counts the blocks whose + // WARPED_CAUSAL eligibility libaom drops because the reference is + // scaled, i.e. the blocks that must read `obmc_cdf`. Zero of them + // would mean this gate's `--enable-warped-motion=1` never reached a + // warp-eligible block and proves nothing about r2's fix. + assert!( + crate::decode::scaled_warp_suppressed_hits() > 0, + "{NAME}: {matched} matches but no block had its warp eligibility \ + suppressed by a scaled reference -- the warp arm proves nothing" + ); + 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 diff --git a/lanes/scaledref-r1.report.md b/lanes/scaledref-r1.report.md new file mode 100644 index 00000000..e9eb5ccc --- /dev/null +++ b/lanes/scaledref-r1.report.md @@ -0,0 +1,115 @@ +# 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**: lift implemented, then REVERTED (`abec872`) -- it exposed a hidden defect, see below. Only a comment recording the finding + repro remains | +| `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` | 2 refusal strings gone (compound-scaled; OBMC/interintra-scaled folded into a warp-only string); 47 → 45 | + +## 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 SHOWN frame + compared to ffmpeg (corrected in r2: `decode_stream` pushes a picture only under + `show_frame`, stream.rs:711, and `ffmpeg -f obu` emits shown frames only -- a hidden + alt-ref is checked only through propagation into the frames that reference it). + 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. (withdrawn) `decode::tests::golomb_tails_read_back_past_the_old_twenty_bit_cap` — written, passing, + and reverted with the change it gated. See "The Golomb tail" below. + +## Refusals lifted (2) + +- "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. + +`refusal_inventory` and `gate_coverage` tests green. + +## The Golomb tail — a refusal that is masking a real defect (finding, fix-now for its owner) + +Implemented as charted (`ee1f980`): read up to 32 leading zeros (dav1d's `len < 32`) instead of +erroring at 20, u64 accumulator, plus a round-trip gate over every prefix length 0..=31 through +`tile::write_golomb`. Bit-identical for every tail an encoder writes — our own writer tops out at 19 +leading zeros (`tile::MAX_LEVEL == MAX_BR_LEVEL + (1 << 19)`), and libaom errors long before. + +It still turned a green gate RED: +`a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` failed at +**seed 67, frame 0 luma**. Bisected against main `3808cf8` and against the lane's own WIP `95b3e70` +(both pass), and main's run prints, for that very seed: +`seed 67 refusal: unsupported: AV1 tile (a Golomb tail longer than this decoder reads)`. + +So the cap is not a capability limit — it is the point where an intra rect64 stream that has ALREADY +desynced first asks for something impossible (a key-frame coefficient level above `1 << 19`). +Class `refusal-hides-a-defect`. Reverted in `abec872`; `d8788fb` records the finding, the repro and +"lift this together with that defect's fix" at the cap itself. + +EVIDENCE: cargo test output, this report | ran the rect64 gate at 3808cf8 / 95b3e70 / lane HEAD with and without the lift | pass / pass / FAIL seed 67 frame 0 luma; main prints the Golomb refusal for seed 67 + +fix-now for the intra rect64 owner: encode that gate's seed-67 fixture, raise the cap locally, and +bisect the first mismatching block — the long tail is a downstream symptom, not the defect. + +## Test scope actually run + +- `cargo test -p ec-av1 --lib a_real_aomenc_superres_stream_with_compound` → 1 passed, 0 failed. +- `cargo test -p ec-av1 --lib a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes` + → 1 passed, 0 failed (this is the gate the Golomb lift turned red; green again after the revert). +- `cargo test -p ec-av1 --lib refusal_inventory` → 3 passed; `... gate_coverage` → 2 passed. +- `cargo test -p ec-av1 --lib golomb` → 3 passed (at ee1f980, before the revert). +- `cargo test -p ec-av1 --lib` (whole crate lib suite, at the final tree): **268 passed, 0 failed, + 23 ignored**, 928s. An earlier run of the same suite, taken with the Golomb cap raised, failed + exactly one test -- the seed-67 rect64 gate above -- which is what put that finding on the board. + +## 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 (charter premise): 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. diff --git a/lanes/scaledref-r2.report.md b/lanes/scaledref-r2.report.md new file mode 100644 index 00000000..22dd69e6 --- /dev/null +++ b/lanes/scaledref-r2.report.md @@ -0,0 +1,85 @@ +# lane-scaledref r2 — warp-capable frame under a scaled reference read the wrong motion_mode alphabet + +Branch `lane-scaledref` (HEAD back on the branch: the verifier left the worktree detached at +a8111dd; `git checkout lane-scaledref` restored it, both were the same commit, no work lost). + +## Verifier finding 1 — ROOT CAUSE FOUND AND FIXED (no refusal needed) + +The verifier's premise was that "motion_mode symbol reading is unaffected; only the prediction +falls back". That is **false**, and it is the whole defect. libaom +`motion_mode_allowed` (oracle source `av1/common/blockd.h:1484-1487`): + +```c + if (mbmi->num_proj_ref >= 1 && allow_warped_motion && + !xd->cur_frame_force_integer_mv && + !av1_is_scaled(xd->block_ref_scale_factors[0])) { + return WARPED_CAUSAL; + } + return OBMC_CAUSAL; +``` + +Under a scaled reference the block reads the **2-symbol `obmc_cdf`**, never the 3-symbol +`motion_mode_cdf`. We read the 3-symbol one, which narrows the arithmetic decoder by the wrong +amount: the decoded *value* happened to stay SIMPLE_TRANSLATION, so nothing ever errored — the +stream decoded "OK: 24 frames" with quietly wrong pixels, exactly the silent-wrong-pixels class +the verifier caught. The warp guard at decode.rs ~10197 could never see it: `warp_params` is +`None` for those blocks (`warped=0` in EC_AV1_TRACE), so no fallback and no refusal ran. + +| path:line | why | +| --- | --- | +| `crates/ec-av1/src/decode.rs` ~10060 (`warp_eligible`) | `&& !ref_is_scaled` added (`mc::scale_factor(py_ref.width, frame_width) != REF_NO_SCALE`), libaom-exact; new `SCALED_WARP_SUPPRESSED_HITS` counts the blocks that take the `obmc_cdf` arm because of it | +| `crates/ec-av1/src/decode.rs` ~630 | the counter + `scaled_warp_suppressed_hits()` / `mixed_scale_compound_hits()` accessors | +| `crates/ec-av1/src/decode.rs` ~9515 | `MIXED_SCALE_COMPOUND_HITS`: compound blocks with exactly one scaled tap (diagnostic for finding 3) | +| `crates/ec-av1/src/decode.rs` ~11455 (`decode_inter_block8`) | sibling site of the same shape, comment only: an 8x8 leaf under a scaled reference is refused before this code runs, so the same `&& !ref_is_scaled` is owed by whoever lifts that refusal | +| `crates/ec-av1/src/stream.rs` (superres gate) | `--enable-warped-motion=1` is now the DEFAULT arm (`EC_SCALEDREF_WARP=0` only for bisecting), `--superres-mode` made settable (`EC_SCALEDREF_MODE`), and `scaled_warp_suppressed_hits() > 0` is hard-asserted so the warp arm cannot pass vacuously | +| `crates/ec-av1/examples/decode_probe.rs` | `EC_PROBE_OUT=` dumps the decoded planes as raw yuv420p — that is the instrument that located this defect (diff vs `ffmpeg -f obu`) | +| `lanes/scaledref-r1.report.md` | finding 2, wording corrected (below) | + +Not changed, reported instead of silently fixed: libaom also requires `!cur_frame_force_integer_mv` +in that same condition and our `warp_eligible` still omits it. Left alone this round — no gate +covers `force_integer_mv` (screen content), and changing an alphabet choice without a gate is the +mistake this round is fixing. deferred: force_integer_mv warp eligibility — unblocked by a +screen-content gate with `cur_frame_force_integer_mv=1`. + +Refusals: **none lifted, none added**, 45 unchanged. `refusal_inventory` / `gate_coverage` +untouched and green (see totals). The "warp prediction with a scaled reference" refusal stays: it +now only guards a *global*-warp block under a scaled reference, which this recipe never produced +(`scaled_warp_fallback=0`). + +EVIDENCE: /tmp/.../scratchpad/warp_superres.obu + ours2.yuv vs ff.yuv | EC_PROBE_OUT decode of the verifier's pinned stream, byte-compared to `ffmpeg -f obu -pix_fmt yuv420p -f rawvideo` | before: frames 1..23 differ (frame 1: 1342 luma + 370 chroma pixels, first at (2,26), max |d| 3, growing to 18 by frame 23); after the fix: `cmp` identical, 147456/147456 bytes + +EVIDENCE: gate run, this report | `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib a_real_aomenc_superres_stream_with_compound -- --nocapture` (warp now on by default) | `0 other-capability refusals, 12/12 pixel-exact, superres_hits=291 predict_scaled_hits=3198 scaled_compound=500 scaled_warp_fallback=0 scaled_obmc=9 scaled_interintra=1 scaled_block8=0 scaled_warp_suppressed=25 mixed_scale_compound=0` — 25 blocks took the `obmc_cdf` arm only because of this fix + +## Verifier finding 2 — report correction (done) + +`lanes/scaledref-r1.report.md` line ~27 now reads "every SHOWN frame compared to ffmpeg ... a +hidden alt-ref is checked only through propagation" (ledger already records +`decode_stream` pushes only under `show_frame`, stream.rs:711). + +## Verifier finding 3 — mixed scaled/unscaled compound taps: DEFERRED, measured not assumed + +Counter `mixed_scale_compound` added and wired into the gate's print; both alternative recipes were +run, neither yields a gateable stream: + +- `EC_SCALEDREF_MODE=3` (qthresh): aomenc never turns superres on for this content/q — + `superres_hits=0 predict_scaled_hits=0`, i.e. the whole gate goes vacuous (it fails its own + `predict_scaled_hits > 0` assert, which is the assert working). +- `EC_SCALEDREF_MODE=2` (random denominator per frame): produces coded widths that are not + multiples of 32, so the frame edge forces sub-16 partitions and the stream stops at the still-open + named refusal `an 8x8 partition leaf under a scaled reference (superres, unimplemented)` at + seed 42. + +deferred: mixed one-scaled-one-unscaled compound tap — unblocked by the leaf8 desync (lane-sub8), +which is what mode-2 recipes hit; the counter is in place so any future recipe proves the case +immediately. The per-tap code itself is r1's (`scale0`/`scale1` are already independent). + +## Test totals + +`EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib` (whole crate lib suite): **268 passed, 0 failed, 23 ignored**, 968s (same totals as r1, now with the warp arm of the superres gate ON). Run at the final tree. + +## Residue carried from r1 (unchanged) + +- deferred: 8x8 partition leaf under a scaled reference — unblocked by the lane-sub8 leaf8 desync. +- deferred: reference scaling in BOTH axes (`--resize-mode`) — unblocked by a lane sized for it. +- accepted: 10-bit is refused upstream (`Picture` planes are `Vec`). +- accepted: the Golomb-tail cap stays where r1 left it (it masks an intra-rect64 defect).