diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b86a542f..1c84ab7a 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -5580,6 +5580,9 @@ fn assign_compound_mv( mode: u8, allow_high_precision_mv: bool, force_integer_mv: bool, + // lane-gm r2: `gm_get_motion_vector(ref0)`/`(ref1)` at this block's own + // position, spec 7.10.2.10's `GLOBAL_GLOBALMV` assignment. + global_mv: ((i32, i32), (i32, i32)), ) -> ((i32, i32), (i32, i32)) { const NEAREST_NEARESTMV: u8 = 0; const NEAR_NEARMV: u8 = 1; @@ -5643,7 +5646,7 @@ fn assign_compound_mv( NEW_NEARESTMV => (read_new(dec, cdfs, new_pred.0), comp_stack.nearest_mv.1), NEAR_NEWMV => (near.0, read_new(dec, cdfs, new_pred.1)), NEW_NEARMV => (read_new(dec, cdfs, new_pred.0), near.1), - GLOBAL_GLOBALMV => ((0, 0), (0, 0)), + GLOBAL_GLOBALMV => global_mv, _ => ( read_new(dec, cdfs, new_pred.0), read_new(dec, cdfs, new_pred.1), @@ -5651,6 +5654,35 @@ fn assign_compound_mv( } } +/// Derives this query block's own [`crate::mvstack::GmMvTable`] -- one +/// `gm_get_motion_vector` (spec 7.10.2.1) result per reference frame, at +/// THIS block's own `(mi_row, mi_col, bw4, bh4)` (mvstack.rs's own doc: the +/// table is always computed at the *querying* block's position, never a +/// donating neighbour's). +fn build_gm_mv_table( + global_motion: &[ec_av1_syntax::WarpParams; 7], + mi_row: usize, + mi_col: usize, + bw4: usize, + bh4: usize, + allow_high_precision_mv: bool, + force_integer_mv: bool, +) -> crate::mvstack::GmMvTable { + std::array::from_fn(|i| { + let gm = &global_motion[i]; + crate::warp::gm_get_motion_vector( + gm.model, + &gm.params, + mi_row, + mi_col, + bw4, + bh4, + allow_high_precision_mv, + force_integer_mv, + ) + }) +} + /// Decodes one square inter-frame block (32x32 whole or 16x16 leaf): skip, /// `is_inter`, then either the single-reference/MV-stack/motion-vector chain /// and motion-compensated reconstruction, or the inter frame's own intra @@ -5690,6 +5722,11 @@ fn decode_inter_block( // GOP), matching `GOLDEN_FRAME`'s existing empty-slot refusal below. other_refs: &RefSlots, sign_bias_table: &SignBiasTable, + // lane-gm r2: this frame header's own `global_motion` table (spec + // 5.9.24), indexed `[ref_frame - LAST_FRAME]` same as `sign_bias_table` + // -- feeds `warp::gm_get_motion_vector` for `GLOBALMV`/`GLOBAL_GLOBALMV` + // assignment and the mv-stack's neighbour substitution. + global_motion: &[ec_av1_syntax::WarpParams; 7], base_q_idx: u8, luma_set_intra: TxbSet, luma_set_inter: TxbSet, @@ -5891,6 +5928,15 @@ fn decode_inter_block( ), allow_high_precision_mv, }); + let gm_table = build_gm_mv_table( + global_motion, + mi_row, + mi_col, + bw4, + bh4, + allow_high_precision_mv, + force_integer_mv, + ); let comp_stack = crate::mvstack::find_mv_stack_compound( grid, mi_row, @@ -5901,6 +5947,7 @@ fn decode_inter_block( mi_cols as usize, mi_rows as usize, sign_bias_table, + &gm_table, comp_tpl, ); let compound_mode = if skip_mode { @@ -5915,8 +5962,15 @@ fn decode_inter_block( compound_mode, allow_high_precision_mv, force_integer_mv, + (gm_table[(ref0 - LAST_FRAME) as usize], gm_table[(ref1 - LAST_FRAME) as usize]), ); let is_globalmv = compound_mode == 6; // GLOBAL_GLOBALMV + // spec `is_nontrans_global_motion`: ALL active refs' models must + // be non-TRANSLATION (IDENTITY counts) for the compound block's + // interp-filter read to be suppressed. + let gm_nontrans = is_globalmv + && global_motion[(ref0 - LAST_FRAME) as usize].model != ec_av1_syntax::WarpModel::Translation + && global_motion[(ref1 - LAST_FRAME) as usize].model != ec_av1_syntax::WarpModel::Translation; 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)?; // spec `get_ref_filter_type`: matches when EITHER of the @@ -5963,6 +6017,17 @@ fn decode_inter_block( is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), size: bw4, size_h: bh4, + // libaom `is_global_mv_block`: per-ref-slot, mode + // GLOBAL_GLOBALMV, block >= 8x8, and THAT slot's + // own gm model > TRANSLATION (IDENTITY excluded + // too -- the two-predicate trap, distinct from + // `gm_nontrans` above which allows IDENTITY). + is_global_mv0: is_globalmv + && bw4.min(bh4) >= 2 + && global_motion[(ref0 - LAST_FRAME) as usize].model as u8 > 1, + is_global_mv1: is_globalmv + && bw4.min(bh4) >= 2 + && global_motion[(ref1 - LAST_FRAME) as usize].model as u8 > 1, }, ); } @@ -6274,7 +6339,7 @@ fn decode_inter_block( cdfs, interp_fixed, enable_dual_filter, - is_globalmv || skip_mode, + gm_nontrans || skip_mode, above_filter_ctx, left_filter_ctx, true, @@ -6566,6 +6631,15 @@ fn decode_inter_block( ), allow_high_precision_mv, }); + let gm_table = build_gm_mv_table( + global_motion, + mi_row, + mi_col, + bw4, + bh4, + allow_high_precision_mv, + force_integer_mv, + ); let stack = find_mv_stack_with_sign_bias( grid, mi_row, @@ -6576,6 +6650,7 @@ fn decode_inter_block( mi_cols as usize, mi_rows as usize, sign_bias_table, + &gm_table, tpl, ); let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; @@ -6610,11 +6685,9 @@ fn decode_inter_block( let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1; is_globalmv = !not_zero; let mv = if !not_zero { - // GLOBALMV: `gm_get_motion_vector` (spec 7.10.2.1) under the - // `GmType::IDENTITY` this crate's `decode_stream` requires of - // every inter frame's `LAST_FRAME` entry (any other warp - // model is refused before the tile decoder is ever called). - (0, 0) + // GLOBALMV: `gm_get_motion_vector` (spec 7.10.2.1), already + // computed at this block's own position in `gm_table`. + gm_table[(ref_frame - LAST_FRAME) as usize] } else { let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; if nearest { @@ -6638,6 +6711,18 @@ fn decode_inter_block( }; (mv, false) }; + // lane-gm r2: the two libaom predicates single-ref GLOBALMV + // blocks need -- `is_global_mv_block` (blockd.h:421-429, mode + // GLOBAL* AND model > TRANSLATION AND min(bw,bh) >= 8px, gating + // `motion_mode_eligible`/`MiInfo::is_global_mv0` below) and + // `is_nontrans_global_motion` (reconinter.h:420-425, model != + // TRANSLATION -- IDENTITY counts here -- gating the interp + // filter suppress below). Do NOT unify these. + let gm_model = global_motion[(ref_frame - LAST_FRAME) as usize].model; + let min_bw_bh4 = bw4.min(bh4); + let is_global_mv_block = is_globalmv && gm_model as u8 > 1 && min_bw_bh4 >= 2; + let gm_nontrans = + is_globalmv && gm_model != ec_av1_syntax::WarpModel::Translation && min_bw_bh4 >= 2; if std::env::var_os("EC_AV1_TELL").is_some() { eprintln!( "TELL mi_row={mi_row} mi_col={mi_col} label=post_assign_mv tell={} range={}", @@ -6722,7 +6807,12 @@ fn decode_inter_block( && interintra_mode.is_none() && (!overlappable_above(grid, mi_row, mi_col, bw4, mi_cols as usize, 1).is_empty() || !overlappable_left(grid, mi_row, mi_col, bh4, mi_rows as usize, 1) - .is_empty()); + .is_empty()) + // libaom `motion_mode_allowed`: a GLOBALMV block whose model + // is non-IDENTITY-non-TRANSLATION under free (non-integer) + // mv reads no motion_mode/obmc symbol at all -- implicit + // SIMPLE_TRANSLATION. + && !(is_global_mv_block && !force_integer_mv); let mut obmc_selected = false; // lane-warp round 2: `Some` when motion_mode == WARPED_CAUSAL // *and* the local warp estimate is valid (`!wm_params.invalid`, @@ -6823,6 +6913,26 @@ fn decode_inter_block( } } } + // lane-gm r4: `allow_warp`'s `global_warp_allowed` branch + // (`reconinter.c:33-55`), gated INDEPENDENTLY of `motion_mode` -- + // libaom's `av1_init_warp_params` runs for every inter + // predictor build, not just a `WARPED_CAUSAL`-selected one. Only + // reachable when `warp_params` is still `None` here: a block + // that read (and got) local `WARPED_CAUSAL` above already has + // `local_warp_allowed && !wm_params.invalid`, which `allow_warp` + // checks FIRST and short-circuits on -- the global branch is an + // `else if`, never layered on top. `is_global_mv_block` already + // encodes `allow_warp`'s size bound (`min(bw4,bh4) >= 2`, i.e. + // >=8px both dims, matching `av1_init_warp_params`'s + // `block_height < 8 || block_width < 8` early return) and, via + // `motion_mode_eligible`'s own suppression above, the + // `force_integer_mv` case never reads a motion_mode symbol at + // all -- checked again here directly since this branch fires + // independent of `motion_mode_eligible`. + let gm_ref = &global_motion[(ref_frame - LAST_FRAME) as usize]; + if warp_params.is_none() && is_global_mv_block && !force_integer_mv && !gm_ref.invalid { + warp_params = crate::warp::global_warp_params(gm_ref.params); + } if std::env::var_os("EC_AV1_TELL").is_some() { eprintln!( "TELL mi_row={mi_row} mi_col={mi_col} label=post_motion_mode eligible={} tell={} range={}", @@ -6859,7 +6969,7 @@ fn decode_inter_block( cdfs, interp_fixed, enable_dual_filter, - is_globalmv || warped_selected, + gm_nontrans || warped_selected, above_filter_ctx, left_filter_ctx, false, @@ -6901,6 +7011,8 @@ fn decode_inter_block( is_new_mv, size: bw4, size_h: bh4, + is_global_mv0: is_global_mv_block, + is_global_mv1: false, }, ); } @@ -7147,6 +7259,8 @@ fn decode_inter_block( is_new_mv: false, size: side / 4, size_h: side / 4, + is_global_mv0: false, + is_global_mv1: false, }, ); } @@ -7482,6 +7596,7 @@ fn decode_inter_block8( mi_cols as usize, mi_rows as usize, &NO_SIGN_BIAS, + &crate::mvstack::NO_GM_MV, None, ); let compound_mode = if skip_mode { @@ -7501,6 +7616,9 @@ fn decode_inter_block8( compound_mode, allow_high_precision_mv, force_integer_mv, + // leaf8 stays gm-inert (sanctioned partial landing, see + // this fn's own GLOBALMV refusal below). + ((0, 0), (0, 0)), ); // spec 5.11.25: same gating [`decode_inter_block`]'s own @@ -7608,6 +7726,8 @@ fn decode_inter_block8( is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), size: 2, size_h: 2, + is_global_mv0: false, + is_global_mv1: false, }, ); } @@ -7995,6 +8115,8 @@ fn decode_inter_block8( is_new_mv, size: 2, size_h: 2, + is_global_mv0: false, + is_global_mv1: false, }, ); } @@ -8189,6 +8311,8 @@ fn decode_inter_block8( is_new_mv: false, size: 2, size_h: 2, + is_global_mv0: false, + is_global_mv1: false, }, ); } @@ -8349,6 +8473,7 @@ pub fn decode_inter_frame_tile( allow_high_precision_mv, force_integer_mv, NO_SIGN_BIAS, + [ec_av1_syntax::WarpParams::default(); 7], interp_fixed, enable_dual_filter, 0, @@ -8388,6 +8513,9 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( allow_high_precision_mv: bool, force_integer_mv: bool, sign_bias_table: SignBiasTable, + // lane-gm r2: this frame header's own `global_motion` table (spec + // 5.9.24), threaded to every `decode_inter_block` call below. + global_motion: [ec_av1_syntax::WarpParams; 7], interp_fixed: Option, enable_dual_filter: bool, // lane-av1tmvp: `order_hint`/`order_hint_bits`/`ref_order_hints` are @@ -8641,6 +8769,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma32, TxbSet::Luma32Inter, @@ -8714,6 +8843,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma16, if reduced_tx_set { @@ -8890,6 +9020,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma32, TxbSet::Luma32Inter, @@ -8940,6 +9071,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma16, if reduced_tx_set { @@ -8991,6 +9123,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma16, if reduced_tx_set { @@ -9052,6 +9185,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma32, TxbSet::Luma32Inter, @@ -9098,6 +9232,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma32, TxbSet::Luma32Inter, @@ -9149,6 +9284,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma32, TxbSet::Luma32Inter, @@ -9195,6 +9331,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma32, TxbSet::Luma32Inter, @@ -9247,6 +9384,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma16, if reduced_tx_set { @@ -9297,6 +9435,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma16, if reduced_tx_set { @@ -9347,6 +9486,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma32, TxbSet::Luma32Inter, @@ -9398,6 +9538,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma16, if reduced_tx_set { @@ -9448,6 +9589,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma16, if reduced_tx_set { @@ -9498,6 +9640,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma32, TxbSet::Luma32Inter, @@ -9550,6 +9693,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma32, TxbSet::Luma32Inter, @@ -9596,6 +9740,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma16, if reduced_tx_set { @@ -9646,6 +9791,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( &ref_v, &ref_slots, &sign_bias_table, + &global_motion, base_q_idx, TxbSet::Luma16, if reduced_tx_set { @@ -10316,6 +10462,8 @@ mod tests { is_new_mv: false, size: 1, size_h: 1, + is_global_mv0: false, + is_global_mv1: false, }; for col in mi_col..mi_col + bw4 { grid.set(mi_row - 1, col, neighbour(above_mv)); @@ -10424,6 +10572,7 @@ mod tests { &ref_v, &[None; 8], &NO_SIGN_BIAS, + &[ec_av1_syntax::WarpParams::default(); 7], 100, TxbSet::Luma16, TxbSet::Luma16Inter, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index d8ed33b4..efb73802 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -2681,6 +2681,8 @@ pub(crate) fn encode_inter_frame( is_new_mv: matches!(info.mode, InterMode::NewMv), size: 8, size_h: 8, + is_global_mv0: false, + is_global_mv1: false, }, // Intra: no vote, but still a coded cell // -- mvstack's extended-scan coverage @@ -2695,6 +2697,8 @@ pub(crate) fn encode_inter_frame( is_new_mv: false, size: 8, size_h: 8, + is_global_mv0: false, + is_global_mv1: false, }, }, ); @@ -2775,6 +2779,8 @@ pub(crate) fn encode_inter_frame( is_new_mv: matches!(info.mode, InterMode::NewMv), size: 4, size_h: 4, + is_global_mv0: false, + is_global_mv1: false, }, None => MiInfo { is_inter: false, @@ -2785,6 +2791,8 @@ pub(crate) fn encode_inter_frame( is_new_mv: false, size: 4, size_h: 4, + is_global_mv0: false, + is_global_mv1: false, }, }, ); @@ -2834,6 +2842,8 @@ pub(crate) fn encode_inter_frame( ), size: 2, size_h: 2, + is_global_mv0: false, + is_global_mv1: false, }, None => MiInfo { is_inter: false, @@ -2844,6 +2854,8 @@ pub(crate) fn encode_inter_frame( is_new_mv: false, size: 2, size_h: 2, + is_global_mv0: false, + is_global_mv1: false, }, }, ); diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 81c51cc5..2ac8f6e2 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -141,6 +141,16 @@ pub struct MiInfo { /// has no compound candidates). Feeds `NewMvContext` (7.10.2.8): a /// neighbour's own coded-with-NEWMV state, not this block's. pub is_new_mv: bool, + /// Whether this unit's own coded mode was `GLOBALMV`/`GLOBAL_GLOBALMV` + /// *and* the frame's global-motion model for its `ref_frame` is more + /// than `TRANSLATION` (libaom's `is_global_mv_block`, checked per + /// reference slot at donation time, mvref_common.c:88-92): when true, + /// a scan that matches this unit on its first ref slot donates the + /// *querying* block's own `gm_mv` there, not `mv`. + pub is_global_mv0: bool, + /// `is_global_mv0`, for the second ref slot (`ref_frame1`/`mv1`, + /// mvref_common.c:116-117). + pub is_global_mv1: bool, /// The side, in 4x4 units, of the square block this unit belongs to (8 /// for a whole 32x32 block, 4 for a 16x16 leaf -- every block this crate /// ever codes is square, so one dimension is enough). Feeds the extended @@ -302,6 +312,26 @@ pub type SignBiasTable = [bool; 7]; /// hardcoded `sign_bias` stub. pub const NO_SIGN_BIAS: SignBiasTable = [false; 7]; +/// One derived global-motion `(row, col)` mv per reference frame +/// (`table[ref_frame - LAST_FRAME]`, same shape/indexing as +/// [`SignBiasTable`]), all computed at the *querying* block's own position +/// -- lane-gm: a neighbour donating a candidate under `MiInfo::is_global_mv0`/ +/// `MiInfo::is_global_mv1` contributes this table's entry for the matched +/// ref frame, not its own stored `mv`/`mv1` (mvref_common.c:88-92/116-117). +pub type GmMvTable = [(i32, i32); 7]; + +/// The all-zero table every IDENTITY/no-gm caller passes: exact whenever no +/// unit in the grid is ever `is_global_mv0`/`is_global_mv1` (the 14-pin +/// list's own streams), since a substitution that never triggers reads as +/// this reduction's old hardcoded-zero behaviour. +pub const NO_GM_MV: GmMvTable = [(0, 0); 7]; + +/// `table[ref_frame - LAST_FRAME]`, [`sign_bias`]'s shape for a +/// [`GmMvTable`]. +fn gm_mv(table: &GmMvTable, ref_frame: i8) -> (i32, i32) { + table[(ref_frame - LAST_FRAME) as usize] +} + /// libaom's `process_single_ref_mv_candidate` (mvref_common.c): folds one /// already-coded neighbour into the stack as a low-weight (`2`, unboosted) /// filler entry, used only by the single-reference extension pass below. @@ -326,6 +356,10 @@ fn process_single_ref_mv_candidate( if !candidate.is_inter { return; } + // libaom's `process_single_ref_mv_candidate` (mvref_common.c ~440-475) + // reads `candidate->mv[rf_idx]` unconditionally here -- unlike + // `add_ref_mv_candidate`'s row/col/corner scans, this extension pass + // never checks `is_global_mv_block`/substitutes `gm_mv_candidates`. let mut slots = [(candidate.ref_frame, candidate.mv); 2]; let mut n_slots = 1; if let (Some(rf1), Some(mv1)) = (candidate.ref_frame1, candidate.mv1) { @@ -386,6 +420,7 @@ fn scan_row( row_offset: isize, max_row_offset: isize, ref_frame: i8, + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, processed_rows: &mut usize, @@ -416,7 +451,7 @@ fn scan_row( weight = weight.max(inc as u32); *processed_rows = (inc as isize - row_offset - 1).max(0) as usize; } - if let Some(mv) = single_ref_match(info, ref_frame) { + if let Some(mv) = single_ref_match(info, ref_frame, gm) { found = true; add_candidate(candidates, mv, len as u32 * weight); *newmv_count += u32::from(info.is_new_mv); @@ -435,14 +470,22 @@ fn scan_row( /// compound neighbour's vote whenever the match sat in its *second* slot /// (the class this crate's own compound-stack scan already had to close for /// [`find_mv_stack_compound`] -- see the module doc). -fn single_ref_match(info: &MiInfo, ref_frame: i8) -> Option<(i32, i32)> { +fn single_ref_match(info: &MiInfo, ref_frame: i8, gm: &GmMvTable) -> Option<(i32, i32)> { if !info.is_inter { return None; } if info.ref_frame == ref_frame { - Some(info.mv) + Some(if info.is_global_mv0 { + gm_mv(gm, ref_frame) + } else { + info.mv + }) } else if info.ref_frame1 == Some(ref_frame) { - info.mv1 + if info.is_global_mv1 { + Some(gm_mv(gm, ref_frame)) + } else { + info.mv1 + } } else { None } @@ -459,6 +502,7 @@ fn scan_col( col_offset: isize, max_col_offset: isize, ref_frame: i8, + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, processed_cols: &mut usize, @@ -489,7 +533,7 @@ fn scan_col( weight = weight.max(inc as u32); *processed_cols = (inc as isize - col_offset - 1).max(0) as usize; } - if let Some(mv) = single_ref_match(info, ref_frame) { + if let Some(mv) = single_ref_match(info, ref_frame, gm) { found = true; add_candidate(candidates, mv, len as u32 * weight); *newmv_count += u32::from(info.is_new_mv); @@ -511,6 +555,7 @@ fn scan_top_right( mi_col: usize, bw4: usize, ref_frame: i8, + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, ) -> bool { @@ -519,7 +564,7 @@ fn scan_top_right( }; let col = mi_col + bw4; if let Some(info) = grid.get(row, col) - && let Some(mv) = single_ref_match(info, ref_frame) + && let Some(mv) = single_ref_match(info, ref_frame, gm) { add_candidate(candidates, mv, CORNER_WEIGHT); *newmv_count += u32::from(info.is_new_mv); @@ -539,6 +584,7 @@ fn scan_corner( mi_row: usize, mi_col: usize, ref_frame: i8, + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, ) -> bool { @@ -546,7 +592,7 @@ fn scan_corner( return false; }; if let Some(info) = grid.get(row, col) - && let Some(mv) = single_ref_match(info, ref_frame) + && let Some(mv) = single_ref_match(info, ref_frame, gm) { add_candidate(candidates, mv, CORNER_WEIGHT); *newmv_count += u32::from(info.is_new_mv); @@ -613,6 +659,7 @@ pub fn find_mv_stack( mi_cols, mi_rows, &NO_SIGN_BIAS, + &NO_GM_MV, None, ) } @@ -633,6 +680,7 @@ pub fn find_mv_stack_with_sign_bias( mi_cols: usize, mi_rows: usize, sign_bias_table: &SignBiasTable, + gm: &GmMvTable, tpl: Option, ) -> MvStack { let mut candidates = Vec::new(); @@ -670,6 +718,7 @@ pub fn find_mv_stack_with_sign_bias( -1, max_row_offset, ref_frame, + gm, &mut candidates, &mut newmv_count, &mut processed_rows, @@ -683,6 +732,7 @@ pub fn find_mv_stack_with_sign_bias( -1, max_col_offset, ref_frame, + gm, &mut candidates, &mut newmv_count, &mut processed_cols, @@ -693,6 +743,7 @@ pub fn find_mv_stack_with_sign_bias( mi_col, bw4, ref_frame, + gm, &mut candidates, &mut newmv_count, ); @@ -756,7 +807,9 @@ pub fn find_mv_stack_with_sign_bias( any_hit = true; if blk_row == 0 && blk_col == 0 { first_sample_missing = false; - first_sample_far = cand.mv.0.abs() >= 16 || cand.mv.1.abs() >= 16; + let this_gm_mv = gm_mv(gm, ref_frame); + first_sample_far = (cand.mv.0 - this_gm_mv.0).abs() >= 16 + || (cand.mv.1 - this_gm_mv.1).abs() >= 16; } add_candidate(&mut candidates, cand.mv, 2); } @@ -811,6 +864,7 @@ pub fn find_mv_stack_with_sign_bias( mi_row, mi_col, ref_frame, + gm, &mut candidates, &mut dummy_newmv_count, ); @@ -841,6 +895,7 @@ pub fn find_mv_stack_with_sign_bias( row_offset, max_row_offset, ref_frame, + gm, &mut candidates, &mut dummy_newmv_count, &mut processed_rows, @@ -858,6 +913,7 @@ pub fn find_mv_stack_with_sign_bias( col_offset, max_col_offset, ref_frame, + gm, &mut candidates, &mut dummy_newmv_count, &mut processed_cols, @@ -1101,6 +1157,7 @@ fn scan_row_compound( row_offset: isize, max_row_offset: isize, ref_frame: (i8, i8), + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, processed_rows: &mut usize, @@ -1135,8 +1192,8 @@ fn scan_row_compound( found = true; add_compound_candidate( candidates, - info.mv, - info.mv1.unwrap_or((0, 0)), + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, len as u32 * weight, ); *newmv_count += u32::from(info.is_new_mv); @@ -1157,6 +1214,7 @@ fn scan_col_compound( col_offset: isize, max_col_offset: isize, ref_frame: (i8, i8), + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, processed_cols: &mut usize, @@ -1191,8 +1249,8 @@ fn scan_col_compound( found = true; add_compound_candidate( candidates, - info.mv, - info.mv1.unwrap_or((0, 0)), + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, len as u32 * weight, ); *newmv_count += u32::from(info.is_new_mv); @@ -1203,12 +1261,14 @@ fn scan_col_compound( } /// The compound top-right probe ([`scan_top_right`]'s pair-matched twin). +#[allow(clippy::too_many_arguments)] fn scan_top_right_compound( grid: &MiGrid, mi_row: usize, mi_col: usize, bw4: usize, ref_frame: (i8, i8), + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, ) -> bool { @@ -1223,8 +1283,8 @@ fn scan_top_right_compound( { add_compound_candidate( candidates, - info.mv, - info.mv1.unwrap_or((0, 0)), + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, CORNER_WEIGHT, ); *newmv_count += u32::from(info.is_new_mv); @@ -1239,6 +1299,7 @@ fn scan_corner_compound( mi_row: usize, mi_col: usize, ref_frame: (i8, i8), + gm: &GmMvTable, candidates: &mut Vec, newmv_count: &mut u32, ) -> bool { @@ -1252,8 +1313,8 @@ fn scan_corner_compound( { add_compound_candidate( candidates, - info.mv, - info.mv1.unwrap_or((0, 0)), + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { info.mv }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { info.mv1.unwrap_or((0, 0)) }, CORNER_WEIGHT, ); *newmv_count += u32::from(info.is_new_mv); @@ -1360,6 +1421,7 @@ fn combine_compound_candidates(lists: &CompoundRefLists) -> [((i32, i32), (i32, /// ports of libaom's real behaviour as of r5 — see the inline comments at /// each site. #[allow(clippy::too_many_arguments)] +#[allow(clippy::too_many_arguments)] pub fn find_mv_stack_compound( grid: &MiGrid, mi_row: usize, @@ -1370,6 +1432,7 @@ pub fn find_mv_stack_compound( mi_cols: usize, mi_rows: usize, sign_bias_table: &SignBiasTable, + gm: &GmMvTable, tpl: Option, ) -> CompoundMvStack { let mut candidates: Vec = Vec::new(); @@ -1401,6 +1464,7 @@ pub fn find_mv_stack_compound( -1, max_row_offset, ref_frame, + gm, &mut candidates, &mut newmv_count, &mut processed_rows, @@ -1414,6 +1478,7 @@ pub fn find_mv_stack_compound( -1, max_col_offset, ref_frame, + gm, &mut candidates, &mut newmv_count, &mut processed_cols, @@ -1424,6 +1489,7 @@ pub fn find_mv_stack_compound( mi_col, bw4, ref_frame, + gm, &mut candidates, &mut newmv_count, ); @@ -1489,10 +1555,12 @@ pub fn find_mv_stack_compound( let mv1 = cand1.map_or((0, 0), |c| c.mv); if blk_row == 0 && blk_col == 0 { first_sample_missing = false; - first_sample_far = mv0.0.abs() >= 16 - || mv0.1.abs() >= 16 - || mv1.0.abs() >= 16 - || mv1.1.abs() >= 16; + let this_gm0 = gm_mv(gm, ref_frame.0); + let this_gm1 = gm_mv(gm, ref_frame.1); + first_sample_far = (mv0.0 - this_gm0.0).abs() >= 16 + || (mv0.1 - this_gm0.1).abs() >= 16 + || (mv1.0 - this_gm1.0).abs() >= 16 + || (mv1.1 - this_gm1.1).abs() >= 16; } add_compound_candidate(&mut candidates, mv0, mv1, 2); } @@ -1512,6 +1580,7 @@ pub fn find_mv_stack_compound( mi_row, mi_col, ref_frame, + gm, &mut candidates, &mut dummy_newmv_count, ); @@ -1530,6 +1599,7 @@ pub fn find_mv_stack_compound( row_offset, max_row_offset, ref_frame, + gm, &mut candidates, &mut dummy_newmv_count, &mut processed_rows, @@ -1547,6 +1617,7 @@ pub fn find_mv_stack_compound( col_offset, max_col_offset, ref_frame, + gm, &mut candidates, &mut dummy_newmv_count, &mut processed_cols, @@ -1983,6 +2054,8 @@ mod tests { // tests only exercise the immediate/corner/top-right scans. size: 1, size_h: 1, + is_global_mv0: false, + is_global_mv1: false, } } @@ -2137,6 +2210,8 @@ mod tests { is_new_mv: true, size: 1, size_h: 1, + is_global_mv0: false, + is_global_mv1: false, }, ); // Only the row matches (nearest_match == 1): newmv_count > 0 picks @@ -2218,6 +2293,8 @@ mod tests { is_new_mv: false, size: 8, size_h: 8, + is_global_mv0: false, + is_global_mv1: false, }, ); @@ -2248,6 +2325,8 @@ mod tests { is_new_mv: false, size: 8, size_h: 8, + is_global_mv0: false, + is_global_mv1: false, } } @@ -2265,6 +2344,8 @@ mod tests { is_new_mv: false, size: 8, size_h: 8, + is_global_mv0: false, + is_global_mv1: false, } } @@ -2503,6 +2584,8 @@ mod tests { is_new_mv: false, size: 1, size_h: 1, + is_global_mv0: false, + is_global_mv1: false, } } @@ -2524,13 +2607,15 @@ mod tests { is_new_mv: false, size: 1, size_h: 1, + is_global_mv0: false, + is_global_mv1: false, }, ); // Exact pair match. let (mv0, mv1) = ((2, 2), (-6, 3)); grid.set(2, 1, comp_inter(mv0, mv1)); - let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, None); + let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, &NO_GM_MV, None); // The exact-pair immediate scan lands one boosted entry; the // compound extension pass (short stack, len < MAX_MV_REF_CANDIDATES) @@ -2554,7 +2639,7 @@ mod tests { grid.set(2, 1, comp_inter(mv0, mv1)); grid.set(3, 1, comp_inter(mv0, mv1)); - let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, None); + let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, &NO_GM_MV, None); // Exact libaom `is_dup` port: with exactly one entry already in the // stack (the four identical-pair neighbours deduped by the @@ -2586,7 +2671,7 @@ mod tests { grid.set(4, 2, comp_inter(left.0, left.1)); grid.set(5, 2, comp_inter(left.0, left.1)); - let stack = find_mv_stack_compound(&grid, 3, 3, 2, 3, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, None); + let stack = find_mv_stack_compound(&grid, 3, 3, 2, 3, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, &NO_GM_MV, None); assert_eq!(stack.entries.len(), 2); assert_eq!(stack.entries[0].mv0, left.0); @@ -2625,6 +2710,8 @@ mod tests { is_new_mv: false, size: 1, size_h: 1, + is_global_mv0: false, + is_global_mv1: false, }; process_compound_ref_mv_candidate(&candidate, COMP_PAIR, &NO_SIGN_BIAS, &mut lists); assert_eq!(lists.ref_id[0], vec![(5, 5)]); @@ -2652,6 +2739,8 @@ mod tests { is_new_mv: false, size: 8, size_h: 8, + is_global_mv0: false, + is_global_mv1: false, }, ); grid.set( @@ -2666,10 +2755,12 @@ mod tests { is_new_mv: false, size: 8, size_h: 8, + is_global_mv0: false, + is_global_mv1: false, }, ); - let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, None); + let stack = find_mv_stack_compound(&grid, 2, 2, 2, 2, COMP_PAIR, 8, 8, &NO_SIGN_BIAS, &NO_GM_MV, None); // The combine step (libaom's real "Handle compound reference frame // extension") zips each side's ref_id-then-ref_diff lists diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index ec1eed39..57a2d289 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -211,14 +211,76 @@ pub fn decode_stream(data: &[u8]) -> Result> { // lane-av1refs widens this from `[0, 3]` (LAST_FRAME/GOLDEN_FRAME) // to every one of the 7 single-reference slots `decode_inter_block` // can now select. + // + // lane-gm r2 STEP-8 ATTEMPT (reverted): removing this refusal + // exposed a real pixel mismatch -- + // a_real_aomenc_stream_with_interintra_wedge_decodes_pixel_exact + // seed 43, frame 1 luma, with EC_WEDGE_GATE_ATTEMPTS=20 + // EC_AV1_REQUIRE_AOMENC=1 -- so the single-ref/compound MV wiring + // is not yet provably correct end to end; keeping the refusal + // until that seed is captured (EC_AV1_GATE_DUMP), self-pinned, and + // root-caused rather than shipping a wrong decode. + // + // lane-gm r3 ROOT CAUSE (pinned, range-ladder-confirmed, still not + // fixed): the mismatch is NOT an mv/mvstack/entropy bug -- msac + // RANGE at the first divergent block (mi=(8,0), frame 1) matches + // aomdec's own `EC_PART` trace bit-for-bit (rng=38664 both sides), + // and this decoder's own `gm_get_motion_vector` for that block + // (ROTZOOM, seed 43's own header) computes the spec-correct + // mv=(-8,8) (hand-verified against mv.h's `block_center_x`/`_y`, + // which found and fixed a real `- 1` omission in `warp.rs` -- + // harmless for THIS block's rounding but a genuine spec bug fixed + // regardless). The actual gap: `reconinter.c`'s `allow_warp` has a + // `global_warp_allowed` branch (`warp_types.global_warp_allowed = + // is_global_mv_block(...)`, `reconinter_enc.c:281`) that predicts + // an `is_global_mv_block` block (ROTZOOM/AFFINE GLOBALMV, >=8x8) + // with the FULL per-pixel affine global-motion warp, independent + // of `motion_mode` -- this decoder only ever builds a warp + // prediction for local `WARPED_CAUSAL` motion mode and falls back + // to plain translational MC with the block-centre mv for every + // other GLOBALMV block, which is only a centre-point approximation + // of the true per-pixel warp. That is the entire remaining gap. + // lane-gm r4: `decode.rs` now feeds `global_motion[ref]` into + // `crate::warp::global_warp_params` for every single-ref + // `is_global_mv_block` (ROTZOOM/AFFINE), matching `allow_warp`'s + // `global_warp_allowed` branch -- ROTZOOM is pin-verified + // (`gm-seed43.obu`, frame 1 luma MATCH) and TRANSLATION already + // worked (`gm_get_motion_vector`'s translation-only mv, no warp + // needed). AFFINE is untested this round (no aomenc fixture in the + // wedge gate reached a 6-parameter model) -- keep refusing it by + // name rather than shipping an unverified decode. if header.frame_type != FrameType::Key && header.global_motion[..7] .iter() - .any(|gm| gm.model != WarpModel::Identity) + .any(|gm| gm.model == WarpModel::Affine) { return Err(Error::unsupported( "AV1 decode_stream", - "an inter frame whose global motion for a single-reference frame is not IDENTITY (GLOBALMV needs the full warp derivation this decoder does not carry)", + "an inter frame whose global motion for a single-reference frame is AFFINE (unverified this round; ROTZOOM/TRANSLATION/IDENTITY are proven)", + )); + } + // lane-gm r4: a NEW mismatch, distinct from the one this round + // fixed and NOT explained by `reference_select`/compound (traced + // live: the mismatched frame of the seed-43 pin has + // `reference_select == false`) -- localized instead to the one + // property that DOES distinguish it from every frame that already + // matches: TWO different ref slots (`GOLDEN_FRAME`, `BWDREF_FRAME`) + // carry a non-translation model AT THE SAME TIME. The pin-verified + // fix (frame 1) only ever exercised a single active non-identity + // slot (`LAST_FRAME` alone). Root cause not yet found within this + // round's budget (do not guess-fix): refuse the untested shape by + // name -- more than one concurrently active ROTZOOM/AFFINE ref -- + // rather than ship it silently wrong. + if header.frame_type != FrameType::Key + && header.global_motion[..7] + .iter() + .filter(|gm| gm.model != WarpModel::Identity && gm.model != WarpModel::Translation) + .count() + > 1 + { + return Err(Error::unsupported( + "AV1 decode_stream", + "an inter frame with more than one concurrently active ROTZOOM/AFFINE global-motion ref slot (single-active-slot ROTZOOM is pin-verified; this shape is not)", )); } // lane-av1comp: `decode_inter_block`/`decode_inter_block8` now read @@ -386,6 +448,7 @@ pub fn decode_stream(data: &[u8]) -> Result> { header.allow_high_precision_mv, header.force_integer_mv, header.ref_frame_sign_bias, + header.global_motion, interp_fixed, enable_dual_filter, order_hint_bits, diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 886d5b71..ab0b3b7c 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -2440,6 +2440,8 @@ pub fn sb_coeff_inter_frame_tile( is_new_mv, size: 8, size_h: 8, + is_global_mv0: false, + is_global_mv1: false, }, ); for dr in 0..8 { @@ -2459,6 +2461,8 @@ pub fn sb_coeff_inter_frame_tile( is_new_mv, size: 8, size_h: 8, + is_global_mv0: false, + is_global_mv1: false, }, ); } @@ -2489,6 +2493,8 @@ pub fn sb_coeff_inter_frame_tile( is_new_mv: false, size: 8, size_h: 8, + is_global_mv0: false, + is_global_mv1: false, }, ); } @@ -2630,6 +2636,8 @@ fn write_inter_frame_leaf( is_new_mv, size: SUB_MI as usize, size_h: SUB_MI as usize, + is_global_mv0: false, + is_global_mv1: false, }, ); } @@ -2660,6 +2668,8 @@ fn write_inter_frame_leaf( is_new_mv: false, size: SUB_MI as usize, size_h: SUB_MI as usize, + is_global_mv0: false, + is_global_mv1: false, }, ); } @@ -2824,6 +2834,8 @@ fn write_inter_frame_leaf8( is_new_mv, size: 2, size_h: 2, + is_global_mv0: false, + is_global_mv1: false, }, ); } @@ -2854,6 +2866,8 @@ fn write_inter_frame_leaf8( is_new_mv: false, size: 2, size_h: 2, + is_global_mv0: false, + is_global_mv1: false, }, ); } @@ -5109,6 +5123,8 @@ mod tests { is_new_mv, size: 8, size_h: 8, + is_global_mv0: false, + is_global_mv1: false, }, ); } diff --git a/crates/ec-av1/src/warp.rs b/crates/ec-av1/src/warp.rs index 8ae4eaf4..e3228046 100644 --- a/crates/ec-av1/src/warp.rs +++ b/crates/ec-av1/src/warp.rs @@ -46,6 +46,90 @@ fn round2(value: i64, n: u32) -> i64 { (value + (1i64 << n >> 1)) >> n } +/// `GM_TRANS_ONLY_PREC_DIFF` (`av1/common/mv.h`): `WARPEDMODEL_PREC_BITS +/// (16) - 3`, the extra precision a `TRANSLATION` global-motion param has +/// over a 1/8-pel mv. +const GM_TRANS_ONLY_PREC_DIFF: u32 = WARPEDMODEL_PREC_BITS - 3; + +/// `convert_to_trans_prec` (`av1/common/mv.h`): projects a +/// `WARPEDMODEL_PREC_BITS`-fixed-point coordinate down to 1/8-pel +/// (`allow_high_precision_mv`) or 1/4-pel-then-doubled (not high precision). +fn convert_to_trans_prec(allow_high_precision_mv: bool, x: i64) -> i32 { + if allow_high_precision_mv { + round2_signed(x, WARPEDMODEL_PREC_BITS - 3) as i32 + } else { + (round2_signed(x, WARPEDMODEL_PREC_BITS - 2) as i32) * 2 + } +} + +/// `lower_mv_precision`'s `force_integer_mv` half, applied per component +/// (spec 7.10.2.1's `integer_mv_precision`): rounds a 1/8-pel value to the +/// nearest full pel (multiple of 8), ties biased away from zero. +fn integer_mv_precision(v: i32) -> i32 { + let md = v % 8; + if md != 0 { + let mut v = v - md; + if md.abs() > 4 { + v += if md > 0 { 8 } else { -8 }; + } + v + } else { + v + } +} + +/// `gm_get_motion_vector` (`av1/common/mv.h:231`): the motion vector a +/// `GLOBALMV`/`GLOBAL_GLOBALMV` block predicts with, derived from the +/// frame's own global-motion model for `ref_frame` at this block's centre +/// -- spec 7.10.2.1. `bw4`/`bh4` are the block's size in 4x4 units. +/// `IDENTITY` returns `(0, 0)`; `TRANSLATION` keeps the spec's own +/// swapped-axis bug (`row` reads `wmmat[0]`, `col` reads `wmmat[1]`) rather +/// than "fixing" it -- that is what real encoders/decoders exchange. +#[allow(clippy::too_many_arguments)] +pub fn gm_get_motion_vector( + model: ec_av1_syntax::WarpModel, + params: &[i32; 6], + mi_row: usize, + mi_col: usize, + bw4: usize, + bh4: usize, + allow_high_precision_mv: bool, + force_integer_mv: bool, +) -> (i32, i32) { + use ec_av1_syntax::WarpModel; + let (row, col) = match model { + WarpModel::Identity => (0, 0), + WarpModel::Translation => ( + params[0] >> GM_TRANS_ONLY_PREC_DIFF, + params[1] >> GM_TRANS_ONLY_PREC_DIFF, + ), + WarpModel::Rotzoom | WarpModel::Affine => { + // `block_center_x`/`block_center_y` (mv.h:183-191): `- 1`, not + // just the halved block size -- lane-gm r3's root cause, missing + // here made every ROTZOOM/AFFINE GLOBALMV prediction off by a + // sub-pel fraction (small luma delta growing across P-frames as + // it propagates through the reference chain). + let x = (mi_col * MI_SIZE as usize + (bw4 * MI_SIZE as usize) / 2) as i64 - 1; + let y = (mi_row * MI_SIZE as usize + (bh4 * MI_SIZE as usize) / 2) as i64 - 1; + let xc = (params[2] as i64 - (1i64 << WARPEDMODEL_PREC_BITS)) * x + + params[3] as i64 * y + + params[0] as i64; + let yc = params[4] as i64 * x + + (params[5] as i64 - (1i64 << WARPEDMODEL_PREC_BITS)) * y + + params[1] as i64; + ( + convert_to_trans_prec(allow_high_precision_mv, yc), + convert_to_trans_prec(allow_high_precision_mv, xc), + ) + } + }; + if force_integer_mv { + (integer_mv_precision(row), integer_mv_precision(col)) + } else { + (row, col) + } +} + /// `get_msb` (`aom_ports/bitops.h`): index of the highest set bit. fn get_msb64(n: u64) -> i32 { debug_assert!(n != 0); @@ -316,6 +400,21 @@ pub fn find_projection( }) } +/// lane-gm r4: `allow_warp`'s `global_warp_allowed` branch +/// (`reconinter.c:50`) -- reuses the SAME shear decomposition +/// [`find_projection`]'s local-warp path calls (`get_shear_params`), just +/// keyed on the frame's already-coded `global_motion[ref]` model instead of +/// a per-block least-squares fit. Caller gates this on `is_global_mv_block +/// && !global_motion[ref].invalid` (matching `allow_warp` exactly); `None` +/// here means the model's own shear is not filter-representable even though +/// the frame header's `warpValid` bit said otherwise for a `mat[2] <= 0` +/// edge `get_shear_params` additionally rejects -- caller keeps its +/// translational fallback in that rare case. +pub fn global_warp_params(wmmat: [i32; 6]) -> Option { + let (alpha, beta, gamma, delta) = get_shear_params(wmmat)?; + Some(WarpParams { wmmat, alpha, beta, gamma, delta }) +} + fn clip_pixel(v: i32) -> u8 { v.clamp(0, 255) as u8 } @@ -653,3 +752,69 @@ const DIV_LUT: [u16; 257] = [ 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257, 8240, 8224, 8208, 8192, ]; + +#[cfg(test)] +mod gm_mv_tests { + use super::gm_get_motion_vector; + use ec_av1_syntax::WarpModel; + + /// `TRANSLATION`: `row = wmmat[0] >> 13`, `col = wmmat[1] >> 13` (spec's + /// own swapped-axis bug, kept). `100000 >> 13 = 12` (`100000 / 8192 = + /// 12.207`, floor 12); `-50000 >> 13 = -7` (`-50000 / 8192 = -6.1035`, + /// floor -7, arithmetic shift rounds toward -inf). + #[test] + fn translation_shifts_params_by_13() { + let params = [100_000, -50_000, 1 << 16, 0, 0, 1 << 16]; + let mv = gm_get_motion_vector(WarpModel::Translation, ¶ms, 0, 0, 2, 2, true, false); + assert_eq!(mv, (12, -7)); + } + + /// `ROTZOOM`, block centre `x = mi_col*4 + bw4*4/2 = 2*4 + 4*4/2 = 16`, + /// `y = mi_row*4 + bh4*4/2 = 1*4 + 4*4/2 = 12`. + /// `xc = (params[2]-2^16)*x + params[3]*y + params[0]` + /// `= 500*16 + 100*12 + 1000 = 8000 + 1200 + 1000 = 10200`. + /// `yc = params[4]*x + (params[5]-2^16)*y + params[1]` + /// `= -50*16 + (-300)*12 + 2000 = -800 - 3600 + 2000 = -2400`. + /// `hp`: `row = Round2Signed(yc,13) = -Round2(2400,13) = -((2400+4096)>>13) + /// = -(6496>>13) = -0 = 0`; `col = Round2Signed(xc,13) = + /// (10200+4096)>>13 = 14296>>13 = 1`. + #[test] + fn rotzoom_projects_block_centre_hp() { + let params = [1000, 2000, (1 << 16) + 500, 100, -50, (1 << 16) - 300]; + let mv = gm_get_motion_vector(WarpModel::Rotzoom, ¶ms, 1, 2, 4, 4, true, false); + assert_eq!(mv, (0, 1)); + } + + /// Same model/geometry as [`rotzoom_projects_block_centre_hp`], not high + /// precision: `convert_to_trans_prec` shifts by 14 instead of 13 then + /// doubles. `row = -Round2(2400,14)*2 = -((2400+8192)>>14)*2 = + /// -(10592>>14)*2 = 0`; `col = Round2(10200,14)*2 = ((10200+8192)>>14)*2 + /// = (18392>>14)*2 = 1*2 = 2`. + #[test] + fn rotzoom_projects_block_centre_no_hp() { + let params = [1000, 2000, (1 << 16) + 500, 100, -50, (1 << 16) - 300]; + let mv = gm_get_motion_vector(WarpModel::Rotzoom, ¶ms, 1, 2, 4, 4, false, false); + assert_eq!(mv, (0, 2)); + } + + /// `force_integer_mv`'s `integer_mv_precision`, applied to + /// [`translation_shifts_params_by_13`]'s raw `(12, -7)`: `12 % 8 = 4`, + /// `abs(4) > 4` false, so `12 -> 12-4 = 8`. `-7 % 8 = -7` (Rust's + /// truncating `%`), `abs(-7) > 4` true and `-7 < 0`, so + /// `-7 -> (-7 - (-7)) - 8 = -8`. + #[test] + fn translation_force_integer_mv_rounds_to_full_pel() { + let params = [100_000, -50_000, 1 << 16, 0, 0, 1 << 16]; + let mv = gm_get_motion_vector(WarpModel::Translation, ¶ms, 0, 0, 2, 2, true, true); + assert_eq!(mv, (8, -8)); + } + + #[test] + fn identity_is_always_zero() { + let params = [999, -999, (1 << 16) + 1, 5, 5, (1 << 16) - 1]; + assert_eq!( + gm_get_motion_vector(WarpModel::Identity, ¶ms, 3, 4, 4, 4, true, false), + (0, 0) + ); + } +} diff --git a/lanes/gm-r1.charter.md b/lanes/gm-r1.charter.md new file mode 100644 index 00000000..9538ff37 --- /dev/null +++ b/lanes/gm-r1.charter.md @@ -0,0 +1,82 @@ +# lane-gm r1 charter — non-IDENTITY global motion decodes (GLOBALMV + global warp MC) + +## Where you are +Worktree /home/user/Documents/Code/Rust/edith_codecs-gm, branch lane-gm @ 2143b92. +Build/test ONLY: `env CARGO_TARGET_DIR=$HOME/.cache/cargo-target-gm cargo test -p ec-av1 --release --lib -- --nocapture` +FOREGROUND, `nice -n 19`, `CARGO_BUILD_JOBS=4`. Never push; never touch other worktrees; fixtures/ is a symlink. +BUDGET ~60 calls: WIP COMMIT after every green milestone; commit compiling state + report by call 45. Recon is pre-done below — do not re-derive it. + +## Measured motivation +The wedge-interintra gate refuses 25-29/40 attempts with "global motion ... +not IDENTITY" — mandelbrot's pan/zoom makes aomenc code ROTZOOM/TRANSLATION +global motion. Killing this refusal converts those attempts to decodes. + +## Recon already done (anchors, do not re-search) +- Frame-header params ALREADY PARSED: ec-av1-syntax frame.rs + read_global_motion_params (~1709) fills header.global_motion[7] with + WarpParams { model: WarpModel, params/wmmat }. Read its struct shape first. +- The whole-frame refusal to remove: crates/ec-av1/src/stream.rs ~204-218 + (`any(|gm| gm.model != WarpModel::Identity)`). +- Single-ref GLOBALMV site: decode.rs ~6041-6070 (`is_globalmv = !not_zero`; + the GLOBALMV arm currently assigns mv (0,0)). +- Compound GLOBAL_GLOBALMV: decode.rs ~5378 (`compound_mode == 6`), also + assigns zeros today. +- mvstack pad/default candidates: crates/ec-av1/src/mvstack.rs — the stack + pads short lists with (0,0) entries; libaom pads with + gm_get_motion_vector results for the block's ref (setup_ref_mv_list's + gm_mv_candidates). grep `0, 0` pad sites + the module doc. +- Our warp machinery (from WARPED_CAUSAL): crates/ec-av1/src/warp.rs + `warp_affine` (~329) + shear derivation. Global warp MC reuses it with the + GLOBAL wmmat instead of the locally-derived one. +- Interp-filter read suppression: decode.rs ~6166-6200 comments + (`is_nontrans_global_motion`, warp-verify caveat: current suppression + assumes IDENTITY globals). libaom av1_is_interp_needed + (av1/common/reconinter.c) returns false when the block uses a + NON-TRANSLATION global model spanning >=8x8 — with non-IDENTITY globals + live this read-suppression condition MUST be ported exactly or every + GLOBALMV block bit-drifts. + +## libaom ground truth (read these, transcribe exactly) +- /tmp/libaom-src/av1/common/mv.h:231 gm_get_motion_vector: block-center + derivation, TRANSLATION/ROTZOOM/AFFINE cases, allow_hp/force_integer + rounding (is_integer), the >>3 / OFFSET semantics. This feeds BOTH + assign_mv and the mvstack gm candidates. +- /tmp/libaom-src/av1/common/reconinter.c av1_allow_warp + the + global-warp branch of av1_build_inter_predictor / warp path selection + (when is_global fires warp_plane vs plain MC with the derived mv): + block >=8x8, no scaling here. +- /tmp/libaom-src/av1/common/warped_motion.c av1_get_shear_params — global + params must pass shear validation or fall back (av1_warp checks + wm->invalid? read how decode handles invalid global params). + +## Scope (in order, WIP commit each) +1. Port gm_get_motion_vector into decode (or warp.rs) with unit tests + pinning a few (wmmat, center, hp/int) -> mv values TRANSCRIBED from a + hand-run of the C formula (show the arithmetic in the test comments). +2. Thread header.global_motion into decode_inter_block/decode_inter_block8: + GLOBALMV single-ref + GLOBAL_GLOBALMV compound assign the derived mv; + mvstack gm candidates (pass the gm params into find_mv_stack pad sites + per libaom setup_ref_mv_list). +3. MC: blocks whose mode is GLOBALMV (or whose selected mv comes from a + non-translation global model per av1_allow_warp) go through warp_affine + with the global wmmat + shear params; TRANSLATION model = plain MC with + the derived mv. Port av1_is_interp_needed's global condition at the + interp-filter read. +4. Remove the stream.rs whole-frame refusal. +5. GATE LADDER: (a) 14-pin default list; (b) wedge-interintra gate + a_real_aomenc_stream_with_interintra_wedge (its 25-29/40 global-motion + refusals must DROP — print before/after refusal counts in the report; + any new mismatch = EC_AV1_GATE_DUMP self-pin /tmp/claude-1000/gm-flake-N.obu, + report the pin + recon-diff location, do NOT guess-fix); + (c) free-partition + AB gates; (d) full lib suite EXCEPT run the wedge + gate separately (it is slow): `-- --skip a_real_aomenc_stream_with_interintra_wedge`. +6. If mismatches appear (likely — this is a value-heavy port): pin, localize + with EC_AV1_PREFILT_DUMP both sides + the EC_AV1_TELL ladder, fix the + earliest divergence only, iterate. Landing with SOME streams still + mismatching = report the pins and leave the stream.rs refusal for exactly + the model kinds still broken (e.g. keep refusing AFFINE, decode + TRANSLATION/ROTZOOM) rather than shipping a wrong decode. + +## Done criteria +Refusal narrowed or gone with pixel-exact evidence; gates green; wip commits; +REPORT lanes/gm-r1.report.md, verdict FIRST line, refusal-count before/after. diff --git a/lanes/gm-r1.handoff.md b/lanes/gm-r1.handoff.md new file mode 100644 index 00000000..743ffd18 --- /dev/null +++ b/lanes/gm-r1.handoff.md @@ -0,0 +1,29 @@ +HANDOFF: Recon phase complete, zero edits, zero commits, tree clean at 2143b92. Tool-call cap (60) hit mid-recon — everything below is verified from source reads; nothing speculative. + +**What the change actually is (libaom semantics nailed down):** + +1. **No new warp MC path exists.** `motion_mode_allowed` (blockd.h:1472) returns SIMPLE_TRANSLATION for `is_global_mv_block` blocks (mode GLOBALMV/GLOBAL_GLOBALMV, bsize≥8x8, gm type > TRANSLATION) when `cur_frame_force_integer_mv == 0`. GLOBAL blocks' gm-derived mv goes through the **existing** `inter_predict`. warp.rs untouched. +2. **gm_get_motion_vector** (mv.h:231): IDENTITY→(0,0); TRANSLATION→ row=`wmmat[0]>>13`, col=`wmmat[1]>>13` (**spec bug: swapped axes, keep it**; GM_TRANS_ONLY_PREC_DIFF=16−3=13); ROTZOOM/AFFINE→ center point x=`mi_col*4+w/2`, y=`mi_row*4+h/2`; xc=`(mat[2]−2^16)·x+mat[3]·y+mat[0]`, yc=`mat[4]·x+(mat[5]−2^16)·y+mat[1]`; then `convert_to_trans_prec(allow_hp, ·)` = sign-rounded >>3 (hp) / >>4. `is_integer` (force_integer_mv) → full-pel rounding. +3. **Two distinct predicates — the key trap:** `is_global_mv_block` needs wmtype **> TRANSLATION** (IDENTITY and TRANSLATION both false); `is_nontrans_global_motion` (filter suppression, reconinter.h:420) excludes only wmtype **== TRANSLATION**, so **IDENTITY counts as nontrans**. Current decode.rs `resolve_interp_filter` suppress arg `is_globalmv || warped_selected` is correct under all-IDENTITY and must become `(is_globalmv && gm.model != Translation) || warped_selected`. +4. **motion_mode gate:** add `&& !(is_globalmv && gm.model > Translation)` when `!force_integer_mv` (site ~6180 `motion_mode_eligible`, plus decode_inter_block8's 8x8 leaf ~6965). GLOBALMV under TRANSLATION gm still reads OBMC/WARPED(local fit). +5. **mvstack:** neighbours in GLOBAL mode contribute the **current block's** gm_mv (not their own mv) — needs `MiInfo.is_global_mv: bool` (3 ctor sites decode.rs ~5416 compound, ~6348 single, ~6600) + `gm_mv`/`gm_active` threaded through `find_mv_stack_with_sign_bias` and `find_mv_stack_compound` into the scan/add-candidate + empty-stack pad (mvref_common.c add_ref_mv_candidate ~700-780, setup_ref_mv_list pad ~790). +6. **Compound GLOBAL_GLOBALMV:** mv=gm_mv[0], mv1=gm_mv[1] from gm of each ref (decode.rs ~5378, `is_globalmv = compound_mode == 6` already computed). +7. **Refusal:** stream.rs ~203-218 (never actually read — grep pattern failed); narrow or delete per charter step 6. + +**Exact next steps (order):** (a) read stream.rs:190-230; (b) read mvref_common.c 700-800 full (artifact://5's middle was elided — add_ref_mv_candidate + pad need exact bodies, incl. whether gm candidates get clamp_mv'd); (c) decodemv.c `read_inter_compound_mode` + `av1_init_warp_params` bodies; (d) mv.h integer_mv_precision/convert_to_trans_prec + confirm WarpParams field names (frame.rs:368 `model` + params array); (e) implement in order mvstack.rs → decode.rs single-ref → compound → gates → stream.rs; (f) unit tests for gm mv (hand-computed TRANSLATION shift + ROTZOOM projection) + mvstack gm-candidate test; (g) gate ladder per charter, `--skip a_real_aomenc_stream_with_interintra_wedge`; (h) mismatch ladder EC_AV1_PREFILT_DUMP/EC_AV1_TELL. Build: `env CARGO_TARGET_DIR=$HOME/.cache/cargo-target-gm CARGO_BUILD_JOBS=4 nice -n 19 cargo test -p ec-av1 --release --lib -- --nocapture`. WIP commit per green milestone; report lanes/gm-r1.report.md, verdict first line. ~46 calls remain fresh. + +DELTA: +fact|decode.rs:6063 — GLOBALMV mv hardcoded (0,0), comment names stream.rs IDENTITY refusal +fact|mv.h:231 gm_get_motion_vector — IDENTITY→0; TRANSLATION row=wmmat[0]>>13 col=wmmat[1]>>13 (spec bug, keep); ROTZOOM/AFFINE center projection, convert_to_trans_prec sign-round >>3(hp)/>>4 +fact|blockd.h:1472 motion_mode_allowed — is_global_mv_block→SIMPLE only when cur_frame_force_integer_mv==0; TRANSLATION-gm GLOBAL blocks still get OBMC/WARPED(local) +fact|blockd.h is_nontrans_global_motion — false only when wmtype==TRANSLATION; IDENTITY counts as nontrans; is_global_mv_block needs >TRANSLATION — two predicates, never unify +fact|reconinter.h:420 av1_is_interp_needed — skip_mode→0; nontrans-global→0; motion_mode==WARPED_CAUSAL→0 +fact|mvstack port needs MiInfo.is_global_mv (ctor sites decode.rs ~5416/~6348/~6600) + gm_mv/gm_active through find_mv_stack_with_sign_bias + find_mv_stack_compound; candidates use CURRENT block's gm_mv; pad tail uses gm too (bodies NOT yet read — artifact://5 middle elided) +fact|compound GLOBAL_GLOBALMV mvs from gm_mv[0]/gm_mv[1]; read_inter_compound_mode body NOT yet read +fact|decode.rs:5615,6198 — resolve_interp_filter already takes suppress flag; narrow to (is_globalmv && model!=Translation)||warped_selected +decision|thread gm as one more decode_inter_block/decode_inter_block8 arg (house style, ~40-arg fns), no new params struct +dead-end|grep "MiInfo {" finds nothing — struct literals don't match; ctor sites are 5416/6348/6600 (sed those spans) +dead-end|shell $(grep -n …) substitution in my first two batch commands silently ran empty greps — use direct grep -n +open|gm candidates clamped in add_ref_mv_candidate? — read exact body +open|av1_init_warp_params + read_inter_compound_mode exact bodies; integer_mv_precision body; GM_*_PREC constants +open|WarpParams field names frame.rs:368 — parser fills params[idx] = (value << prec_diff) + round; wmmat[0..5] ↔ params[0..5] same order (confirm) diff --git a/lanes/gm-r2.charter.md b/lanes/gm-r2.charter.md new file mode 100644 index 00000000..d3f44d7b --- /dev/null +++ b/lanes/gm-r2.charter.md @@ -0,0 +1,81 @@ +# lane-gm r2 charter — CONTINUATION. r1 was pure recon; its HANDOFF is committed at lanes/gm-r1.handoff.md (d9890e3). READ THAT FIRST, then this. + +Worktree /home/user/Documents/Code/Rust/edith_codecs-gm, branch lane-gm (main merged: the aomenc oracle now lives at ~/.cache/aom-oracle, gates work). +Build/test ONLY: `env CARGO_TARGET_DIR=$HOME/.cache/cargo-target-gm CARGO_BUILD_JOBS=4 nice -n 19 cargo test -p ec-av1 --release --lib -- --nocapture` +FOREGROUND. Never push. BUDGET ~55 calls: WIP COMMIT after every green milestone; commit + report by call 45. +libaom source oracle: ~/.cache/aom-oracle/src (NOT /tmp any more). + +## r1's open items — ALL RESOLVED by the orchestrator, do NOT re-read these +1. `gm_get_motion_vector` (mv.h:231) confirmed verbatim, incl. the spec's + swapped-axis TRANSLATION bug (`row = wmmat[0] >> 13`, `col = wmmat[1] >> 13`, + GM_TRANS_ONLY_PREC_DIFF = 16-3). ROTZOOM/AFFINE: + `x = block_center_x(mi_col,bsize)`, `y = block_center_y(mi_row,bsize)`, + `xc = (mat[2] - (1<<16))*x + mat[3]*y + mat[0]`, + `yc = mat[4]*x + (mat[5] - (1<<16))*y + mat[1]`, + `row = convert_to_trans_prec(hp, yc)`, `col = convert_to_trans_prec(hp, xc)`. + `convert_to_trans_prec(hp,c)` = `ROUND_POWER_OF_TWO_SIGNED(c, 13)` when hp + else `ROUND_POWER_OF_TWO_SIGNED(c, 14) * 2`. + `is_integer` (force_integer_mv) -> `integer_mv_precision`: per component, + `mod = v % 8; if mod != 0 { v -= mod; if abs(mod) > 4 { v += mod > 0 ? 8 : -8 } }`. +2. WarpParams (crates/ec-av1-syntax/src/frame.rs:366): `params[0..6]` IS + `wmmat[0..6]`, same order, same WARPEDMODEL_PREC_BITS fixed point; `model` + is GmType; `invalid` is already computed (`warp_valid`). Nothing to add + to the parser. +3. gm candidates are NOT clamped in add_ref_mv_candidate; they are passed in + as `gm_mv_candidates[2]` computed ONCE per block (mvref_common.c:795-830) + from the CURRENT block's centre, and every scan receives them. + +## One site r1 MISSED (orchestrator found it) — must be in scope +`setup_ref_mv_list`'s temporal path compares candidates against the gm mv, not +against zero: mvref_common.c:369-370 (single) and :397-400 (compound) use +`abs(this_refmv - gm_mv_candidates[0]) >= 16` for the `zero_mv_ctx` decision. +Ours hardcodes the zero comparison in mvstack.rs (`first_sample_far = +cand.mv.0.abs() >= 16 || cand.mv.1.abs() >= 16`). That is a MODE CONTEXT input, +so getting it wrong is an ENTROPY desync, not a pixel error. Fix it with the +rest. + +## Implementation order (WIP commit each) +1. `gm_get_motion_vector` port + unit tests: one TRANSLATION case and one + ROTZOOM case, expected values hand-computed IN THE TEST COMMENT showing the + arithmetic (both hp and !hp), plus a force_integer_mv case. +2. mvstack: thread `gm_mv: [(i32,i32); 2]` + a per-neighbour `is_global_mv` + flag (new `MiInfo` field; ctor sites decode.rs ~5416/~6348/~6600 per r1) + into `find_mv_stack_with_sign_bias` and `find_mv_stack_compound`: + - a neighbour that `is_global_mv_block` contributes the CURRENT block's + `gm_mv[ref]`, not its own mv (mvref_common.c:88-92, 116-117); + - `is_global_mv_block` = mode is GLOBALMV/GLOBAL_GLOBALMV AND bsize >= 8x8 + AND `gm.wmtype > TRANSLATION` (blockd.h; IDENTITY and TRANSLATION are + both FALSE here); + - the `zero_mv_ctx` comparison above. + Square/IDENTITY callers must be unchanged — the 14-pin list proves it. +3. decode.rs: GLOBALMV single-ref (~6041-6070, currently hardcodes (0,0)) and + GLOBAL_GLOBALMV compound (~5378) assign the derived mv(s). +4. Filter suppression — THE TRAP, two DIFFERENT predicates, never unify: + `is_nontrans_global_motion` (reconinter.h ~420, used by + `av1_is_interp_needed`) is false ONLY when `wmtype == TRANSLATION`, so + IDENTITY COUNTS AS NONTRANS. Our `resolve_interp_filter` suppress arg is + `is_globalmv || warped_selected` today, which is right only because every + model is IDENTITY; it must become + `(is_globalmv && gm.model != Translation) || warped_selected`. +5. motion_mode: `motion_mode_allowed` returns SIMPLE_TRANSLATION for an + `is_global_mv_block` block when `!cur_frame_force_integer_mv` — so those + blocks read NO motion_mode symbol (sites ~6180 and the 8x8 leaf ~6965). + GLOBALMV under a TRANSLATION model still reads OBMC/WARPED normally. +6. MC: no new warp path (r1 verified) — global blocks predict through the + existing `inter_predict` with the derived mv. +7. stream.rs: narrow/remove the whole-frame refusal (~203-218). + +## Gate ladder (in order) +(a) 14-pin default list (`pinned_warp_stream_decodes_pixel_exact -- --ignored`); +(b) `a_real_aomenc_stream_with_interintra_wedge` — its 25-29/40 global-motion + refusals MUST drop; print before/after counts in the report; +(c) free-partition + AB gates; (d) full lib + `-- --skip a_real_aomenc_stream_with_interintra_wedge`. +Any mismatch: `EC_AV1_GATE_DUMP=/tmp/claude-1000/gm-flake-N.obu` self-pin, then +report the pin + the recon-diff location (EC_AV1_PREFILT_DUMP both sides). +Do NOT guess-fix. Landing with AFFINE still refused while TRANSLATION/ROTZOOM +decode pixel-exact is a GOOD outcome; shipping a wrong decode is not. + +## Done criteria +Refusal narrowed or gone with pixel-exact evidence + refusal-count delta; +gates green; wip commits; report lanes/gm-r2.report.md, verdict FIRST line. diff --git a/lanes/gm-r2.report.md b/lanes/gm-r2.report.md new file mode 100644 index 00000000..e4a78007 --- /dev/null +++ b/lanes/gm-r2.report.md @@ -0,0 +1,34 @@ +VERDICT: PARTIAL -- compiles, all inertness/regression gates green (227/0 full lib, AB/free-partition gates green, wedge gate green on retry after a confirmed flake), but the whole-frame IDENTITY-only refusal in stream.rs was NOT narrowed: doing so exposed a real pixel mismatch (seed 43, frame 1 luma) on the interintra-wedge gate, so it stays in place. Non-IDENTITY global motion still refuses by name at the stream level; the internal decode/mvstack wiring (steps 1-6) is complete and compiles, but is unproven end to end. + +## What changed +- `crates/ec-av1/src/decode.rs`: single-ref path now computes `is_global_mv_block` (blockd.h:421-429 -- mode GLOBAL* AND model > TRANSLATION AND min(bw,bh)>=8px) and `gm_nontrans` (reconinter.h:420-425 -- model != TRANSLATION, IDENTITY counts) as two separate locals, right after `is_globalmv`/`mv` are resolved. `gm_nontrans` now gates the single-ref `resolve_interp_filter` suppress arg (was `is_globalmv`); `is_global_mv_block` now gates `motion_mode_eligible` (`&& !(is_global_mv_block && !force_integer_mv)`, implicit SIMPLE_TRANSLATION) and `MiInfo::is_global_mv0`. +- `decode_inter_block` gained a `global_motion: &[WarpParams; 7]` param, threaded to all 18 call sites in `decode_inter_frame_tile_with_cdfs` plus the standalone unit-test call site and the `decode_inter_frame_tile` (no-cdfs) wrapper (`[WarpParams::default(); 7]`). +- `decode_inter_frame_tile_with_cdfs` gained the same param, threaded from `stream.rs`'s `header.global_motion`. +- `decode_inter_block8` (leaf8) stays gm-inert by design: its `find_mv_stack_compound` call got `&crate::mvstack::NO_GM_MV`, its `assign_compound_mv` call got `((0,0),(0,0))` -- compile-only fixes, its own GLOBALMV refusal (decode.rs ~7466) is untouched and still guards this leaf. +- `mvstack.rs`'s own unit tests (`find_mv_stack_compound` call sites) got `&NO_GM_MV` too (compile fix, same shape). +- `stream.rs`'s whole-frame `global_motion != IDENTITY` refusal: attempted removal (step 8), reverted same round -- see Dead-end below. Net diff there is a doc-comment only. + +## Gate ladder (in order, per charter) +a. `cargo test -p ec-av1 --release --lib pinned -- --ignored --nocapture`: 2 passed (golden7, warp), 4 failed -- all 4 are pre-existing env-only gaps unrelated to gm (golden3/golden4 read a hardcoded path into a DIFFERENT prior session's scratchpad UUID that doesn't exist on this box; `scratch_decode_pinned_stream_once`/`scratch_isolate_pinned_mismatch` require `EC_AV1_PIN` set manually, by design). No IDENTITY-gm pin moved. +b. `EC_AV1_REQUIRE_AOMENC=1 cargo test ... a_real_aomenc_stream_with_interintra_wedge -- --nocapture`: first run FAILED on `wii_hits==0` (12/40 matched, 0 wedge fires) -- reproduced the documented parallel-flake-is-attempt-selection class, NOT a gm regression (all 12 matched streams are IDENTITY-gm since the whole-frame refusal was still in place at that point). Rerun: `ok`, 30 other-capability refusals / 10 matches / wii_hits=7. Refusal count both runs: 27/40 and (unlogged, similar) named "global motion...not IDENTITY" -- within the charter's 25-29/40 baseline, i.e. unchanged (expected, since step 8 wasn't landed yet at that point). +c. AB + free-partition gates: both `ok` (`a_real_aomenc_stream_with_free_partitions_decodes_pixel_exact`, `a_real_aomenc_stream_with_ab_partitions_decodes_pixel_exact`), rect_partition_hits=18, extended_partition_hits=1, partab_hits=7/2 across the two runs. +d. `cargo test -p ec-av1 --release --lib -- --skip a_real_aomenc_stream_with_interintra_wedge`: 227 passed, 0 failed, 17 ignored, 1 filtered. + +## Step 8 (narrowing the whole-frame refusal) -- attempted, reverted +Removed the `header.global_motion != IDENTITY` whole-frame refusal in `stream.rs` and reran the AB/free-partition/wedge gates with `EC_AV1_REQUIRE_AOMENC=1 EC_WEDGE_GATE_ATTEMPTS=20 EC_RECTGATE_ATTEMPTS=20`: +- AB partitions: `ok`, 17 named refusals / 3 matches, **zero** "global motion" refusals logged (confirms the narrowing itself worked mechanically). +- Free partitions: `ok`, similarly zero gm refusals. +- Interintra-wedge: **FAILED** -- `a_real_aomenc_stream_with_interintra_wedge_decodes_pixel_exact frame 1 luma vs ffmpeg (seed 43)` pixel mismatch, decode.rs:3821 assert. + +This is a real defect somewhere in the single-ref/compound GLOBALMV wiring (steps 1-6), not proven safe to ship. Reverted the stream.rs removal (restored the refusal, kept everything else) rather than land a wrong decode per the charter's own instruction. Compiled clean after revert, committed (2e23ae3). + +## Dead-ends / open +dead-end: lane-gm r2 step-8 -- narrowing the whole-frame `global_motion != IDENTITY` refusal in stream.rs decodes AB/free-partition gates clean (0 gm refusals, both green) but mismatches pixel-exact on `a_real_aomenc_stream_with_interintra_wedge_decodes_pixel_exact` seed 43 frame 1 luma (`EC_WEDGE_GATE_ATTEMPTS=20 EC_AV1_REQUIRE_AOMENC=1`) -- a real defect in the single-ref/compound GLOBALMV MV/interp/motion-mode wiring, not yet root-caused. Refusal reverted/restored. +open: next round should reproduce seed 43 with `EC_AV1_GATE_DUMP=/gm-flake-1.obu` set on that same recipe (48/64px gradients+mandelbrot, `--cq-level=45 --cpu-used=0 --auto-alt-ref=1 --lag-in-frames=16 --enable-fwd-kf=0 --enable-order-hint=1 --enable-warped-motion=1 --enable-obmc=1 --enable-interintra-comp=1 --enable-interintra-wedge=1 ...` -- the full args list is in `stream.rs`'s `a_real_aomenc_stream_with_interintra_wedge_decodes_pixel_exact`), self-pin it, and range-ladder against aomdec `EC_TRACE` on the GLOBALMV blocks in frame 1 to find whether the desync is in `gm_get_motion_vector`'s per-model math, the mv-stack neighbour substitution, `is_global_mv_block`/`gm_nontrans` predicate boundary, or `build_gm_mv_table`'s per-block-position recompute. + +## Files touched +- `crates/ec-av1/src/decode.rs` -- gm predicates, threading, leaf8 compile stubs +- `crates/ec-av1/src/stream.rs` -- `header.global_motion` threaded to the call; whole-frame refusal doc-comment updated (refusal itself unchanged, net) +- `crates/ec-av1/src/mvstack.rs` -- own test call sites' `NO_GM_MV` compile fix + +Commits (branch lane-gm, worktree edith_codecs-gm): a53b294 (compiles), 208e89e (test compile fix), 2e23ae3 (step-8 attempt + revert, documented). diff --git a/lanes/gm-r3.report.md b/lanes/gm-r3.report.md new file mode 100644 index 00000000..a2aec625 --- /dev/null +++ b/lanes/gm-r3.report.md @@ -0,0 +1,39 @@ +VERDICT: ROOT-CAUSED, NOT FIXED -- the interintra-wedge gate mismatch (seed 43, frame 1 luma) is not an mv, mvstack, or entropy bug; it is a missing decoder capability. This decoder never implements AV1's GLOBAL WARP prediction path (`reconinter.c`'s `allow_warp` -> `global_warp_allowed` branch): libaom predicts every `is_global_mv_block` block (ROTZOOM/AFFINE `GLOBALMV`/`GLOBAL_GLOBALMV`, >=8x8, non-force-integer-mv) with the FULL per-pixel affine global-motion warp, independent of `motion_mode` -- our decoder only ever builds a warp prediction for local `WARPED_CAUSAL` and falls back to plain translational MC using the block-centre mv for every other GLOBALMV block. The whole-frame `global_motion != IDENTITY` refusal in `stream.rs` stays in place; it is still correct to refuse. + +## Method (charter ladder, steps 1-3) + +**1. Reproduce and pin.** Gated the whole-frame refusal behind a temporary `EC_GM_ALLOW_NONIDENTITY` env var (reverted before commit), ran `EC_WEDGE_GATE_ATTEMPTS=20 EC_AV1_REQUIRE_AOMENC=1 EC_AV1_GATE_DUMP=/gm-seed43.obu`. Reproduced deterministically first attempt: seed 43, frame 1 luma mismatch, pinned to `gm-seed43.obu`. Decoded the pinned bytes twice via `scratch_isolate_pinned_mismatch` (`EC_AV1_PIN=gm-seed43.obu EC_AV1_PIN_W=64 EC_AV1_PIN_H=64 EC_AV1_PIN_N=24`) -- byte-identical output both runs (only the timing line differed), confirming this is a real deterministic defect, not the documented parallel-flake-is-attempt-selection class. + +**2. Localize.** `scratch_isolate_pinned_mismatch`'s per-plane diff: frame 0 (key) MATCH on all 3 planes; frame 1 first divergence at luma offset 2048 = **row 32, col 0** (416 mismatches, worst delta 6) -- exactly the top-left corner of the bottom-left 32x32 partition leaf (frame is forced to 32x32 leaves via `--min/max-partition-size=32`), i.e. block `mi=(8,0)`. Frame 1's `global_motion` table (dumped via a temporary debug `eprintln!`, reverted): ref slot 0 (LAST_FRAME) = `Rotzoom { params: [137216, 144384, 61182, -64, 64, 61182] }`, all other refs Identity. `allow_high_precision_mv=false force_integer_mv=false` for this frame. + +**3. Range ladder (prediction vs entropy).** `EC_AV1_TRACE=1` on our decode: `TRACE part32_pre mi=(8,0) rng=38664` immediately before `EC_TRACE mi_row=8 mi_col=0 skip=0 mv=(-8,8) is_new_mv=false bsize=32 ref=1 motion_mode_eligible=0`. `EC_TRACE=1 aomdec` on the identical pinned bytes: `EC_PART mi_row=8 mi_col=0 bsize=9 ctx=8 tell=1001 rng=38664` (frame 1's own entry, matched by tell-count ordering). **RANGE IDENTICAL: 38664 == 38664** -- entropy decode is in sync through this block's partition/mode symbols. Verdict: PREDICTION-side defect, not a symbol-consumption/CDF bug. + +## Root cause + +Hand-verified our `gm_get_motion_vector` (`warp.rs`) against the real oracle's `mv.h:183-286` (`~/.cache/aom-oracle/src/av1/common/mv.h`, foreign implementation, not our own transcription): found and fixed a genuine bug -- `block_center_x`/`block_center_y` (mv.h:183-191) subtract 1 from the halved-block-size offset (`mi_col*MI_SIZE + bw/2 - 1`); our `warp.rs` was missing the `- 1` on both axes. Fixed in `warp.rs`. This is a real spec-conformance bug (kept fixed) but hand-computing both versions for this exact block showed the `-1` shifts `xc`/`yc` by only ~4354 out of a ~16384 rounding threshold in `convert_to_trans_prec` -- **it does not flip the rounded mv for this block** (confirmed empirically: 417 -> 416 mismatches, same first offset, same magnitude, before/after the fix). So it is real but not sufficient, and not the seed-43 root cause. + +Re-derived `gm_get_motion_vector`'s output for block `mi=(8,0)`, `bw4=bh4=8` by hand with the fixed formula: `x=15, y=47`, giving `mv=(-8,8)` -- **exactly what the trace shows** (`mv=(-8,8)`). The mv-stack/mvstack.rs wiring was also independently re-audited against the oracle's `mvref_common.c`: `add_ref_mv_candidate` (75-135, gm-substitutes) maps correctly to `single_ref_match`/`scan_row_compound` etc.; `process_single_ref_mv_candidate`/`process_compound_ref_mv_candidate` (350-472, must NOT substitute) map correctly to our functions of the same name, which correctly use raw `candidate.mv`/`.mv1` with no gm substitution. This wiring is provably correct against the charter's own named concern. + +So: entropy in sync, mv computation matches the (now spec-correct) oracle formula exactly, mvstack substitution correctly scoped, motion_mode correctly suppressed (`motion_mode_eligible=0` since `is_global_mv_block && !force_integer_mv`) -- yet the pixel is still off by a small, block-localized, frame-to-frame-growing delta (417 mismatches in frame 1/2, ballooning to ~3900 by frame 23 as the error propagates through the reference chain -- consistent with a systematic per-pixel prediction error, not entropy garbage). Traced this to `reconinter.c`'s `allow_warp` (`~/.cache/aom-oracle/src/av1/common/reconinter.c:33-55`): its `global_warp_allowed` branch is set by the real encoder as `is_global_mv_block(mi, wm->wmtype)` (`reconinter_enc.c:281`) -- the SAME predicate our decoder already computes and correctly uses to suppress `motion_mode`, but which we never feed into the actual per-pixel prediction. `av1_init_warp_params` is called unconditionally for every inter block's predictor build (not gated on `motion_mode`); when `global_warp_allowed` and the global model is valid, it uses the block's FULL affine model (not the block-centre-derived integer mv) to warp-predict every pixel individually. Our decoder's `warp_params: Option` is populated ONLY when `motion_mode == WARPED_CAUSAL` (local warp) -- every `GLOBALMV`/`GLOBAL_GLOBALMV` block with a non-translation model falls through to plain translational MC using the block-centre mv, which is only a centre-point approximation of the true per-pixel warp and diverges more the further a pixel sits from the block centre -- matching the observed error shape exactly (small at the block's near corner, growing, and compounding across P-frames). + +## Refusal count (wedge gate, `EC_AV1_REQUIRE_AOMENC=1`) + +- Before this round (refusal in place): 27 other-capability refusals / 13 matches / wii_hits=6-7 out of 40 (r2 baseline). +- With the whole-frame refusal REMOVED (diagnostic only, reverted): 0 gm refusals, real pixel mismatch at seed 43 frame 1 (reproduced this round). +- After this round (refusal restored, `warp.rs` fix landed): 27 other-capability refusals / 13 matches / wii_hits=3 out of 40 -- unchanged shape, still `ok` (the global-motion refusal is still one of the 27 named refusals; it is correct to keep it). + +## What is NOT fixed + +Implementing the global-warp prediction path itself: this needs `is_global_mv_block`'s already-computed value fed into a new "predict with `global_motion[ref]` as the affine model" branch in the block reconstruction pipeline (reuse `crate::warp::warp_affine`, the same 8x8-tiled warped filter the local `WARPED_CAUSAL` path already has, just keyed off `global_motion[ref]` instead of the locally-estimated `wm_params`), gated exactly on `is_global_mv_block && !global_motion[ref].invalid` (matching `allow_warp`'s `global_warp_allowed` branch, independent of `motion_mode`). That is real, scoped, next-round work -- not guessed at this round per the charter's "do not guess-fix" instruction. + +## Files touched +- `crates/ec-av1/src/warp.rs` -- `gm_get_motion_vector`'s ROTZOOM/AFFINE branch: added the missing `- 1` to both `x`/`y` block-centre offsets (`block_center_x`/`block_center_y`, mv.h:183-191), a genuine spec-conformance fix found via the oracle diff, kept even though it did not resolve seed 43. +- `crates/ec-av1/src/stream.rs` -- doc comment on the whole-frame refusal updated with this round's root-cause finding (refusal itself unchanged, still in place). Diagnostic env-var gate and debug `eprintln!` added and reverted before commit (net: comment-only + the one real `warp.rs` fix; no functional change to `stream.rs`'s refusal behaviour). + +## Gate ladder +- `scratch_isolate_pinned_mismatch` (pin, twice): deterministic, byte-identical. +- `cargo test -p ec-av1 --release --lib -- --skip a_real_aomenc_stream_with_interintra_wedge`: 227 passed, 0 failed, 17 ignored, 1 filtered. +- `EC_AV1_REQUIRE_AOMENC=1 cargo test ... a_real_aomenc_stream_with_interintra_wedge`: `ok`, 27/40 refusals, 13/40 matches, wii_hits=3 (refusal restored, unchanged shape from r2). + +## Next round +Wire `is_global_mv_block && !global_motion[ref].invalid` into the block predictor to select global-affine-warp prediction (reuse `warp::warp_affine` keyed on `global_motion[ref]` instead of the locally-fit `wm_params`), independent of `motion_mode`. Re-run this exact pin (`gm-seed43.obu`, kept in the scratchpad this round -- not committed to the repo, re-capture with the same `EC_AV1_GATE_DUMP` recipe if needed) to confirm frame 1 luma goes to MATCH before attempting to narrow/remove the whole-frame refusal again. diff --git a/lanes/gm-r4.report.md b/lanes/gm-r4.report.md new file mode 100644 index 00000000..8a43e67d --- /dev/null +++ b/lanes/gm-r4.report.md @@ -0,0 +1,41 @@ +VERDICT: FIXED (single-ref global-warp path landed and pin-verified) -- a distinct, narrower, NOT-yet-fixed shape found and safely refused by name, not guess-fixed. + +## What was implemented + +`allow_warp`'s `global_warp_allowed` branch (`reconinter.c:33-55`), gated INDEPENDENTLY of `motion_mode`, exactly as r3 scoped it: + +- `crates/ec-av1/src/warp.rs`: added `pub fn global_warp_params(wmmat: [i32; 6]) -> Option` -- calls the SAME `get_shear_params` `find_projection` already uses for local `WARPED_CAUSAL`, just keyed on the frame's coded `global_motion[ref]` model instead of a per-block least-squares fit. No second warp implementation. +- `crates/ec-av1/src/decode.rs` (single-ref inter block path, ~line 6391): `if warp_params.is_none() && is_global_mv_block && !force_integer_mv && !gm_ref.invalid { warp_params = crate::warp::global_warp_params(gm_ref.params); }`, placed right after the local-`WARPED_CAUSAL` motion_mode block so `warp_params.is_none()` naturally encodes `allow_warp`'s "local first, else-if global" precedence. `is_global_mv_block` already carries the `min(bw4,bh4) >= 2` (>=8px) size bound `av1_init_warp_params` checks separately. +- `crates/ec-av1/src/stream.rs`: the whole-frame refusal ("any non-IDENTITY global motion for a single-ref frame -> refuse") is GONE. Replaced with two narrower, named refusals for what remains genuinely unverified (see below). + +## Pin before/after (`gm-seed43.obu`, `EC_WEDGE_GATE_ATTEMPTS=20 EC_AV1_REQUIRE_AOMENC=1`) + +- **Before (r3 baseline):** frame 1 luma MISMATCH, 416 mismatches, first at row 32 col 0 (block `mi=(8,0)`, ROTZOOM on `LAST_FRAME` alone). +- **After:** frame 1 luma **MATCH**. Frames 0-13 all MATCH (all 3 planes, re-confirmed via `scratch_isolate_pinned_mismatch` with `EC_AV1_PIN_N=24`). AB-tested with a temporary `EC_GM_DISABLE` env gate (reverted before commit): disabling the new code reproduces the exact r3 baseline mismatch (416 @ row32/col0) and shows it never resolves across all 24 frames -- confirms the fix, not a coincidence. +- Frame 14 onward hits a **NEW, smaller, distinct** mismatch (441 Y mismatches, worst delta 7, first at row 0 col 18) that the r3 investigation never reached (the whole-frame refusal used to block this frame entirely). Traced (not guessed): the mismatched frame has `reference_select=false` (ruling out the compound-`GLOBAL_GLOBALMV` hypothesis I checked first) and `global_motion = [Identity,Identity,Identity,Rotzoom,Rotzoom,Identity,Identity]` -- i.e. **two different ref slots** (`GOLDEN_FRAME`, `BWDREF_FRAME`) carry an active ROTZOOM model **at the same time**, a shape frame 1's single-active-slot (`LAST_FRAME` alone) case never exercised. Root cause not found within this round's budget; per charter instruction, **not guess-fixed** -- refused by name instead (see below), verified via `scratch_decode_pinned_stream_once` that the refusal now fires cleanly (`Err`, no panic) rather than shipping the wrong pixels. + +## Refusal narrowing (stream.rs, `header.frame_type != Key` gate) + +1. `gm.model == WarpModel::Affine` (any ref slot) -- AFFINE (6-parameter) models are untested this round; no aomenc fixture in the wedge gate reached one. +2. More than one ref slot concurrently has a ROTZOOM/AFFINE model -- the exact shape frame 14 exposed; only the single-active-slot case is pin-verified. + +Proven decodable this round: **ROTZOOM as the sole active non-IDENTITY/non-TRANSLATION global-motion ref slot** (single-ref path). TRANSLATION was already correct (no warp needed, plain `gm_get_motion_vector` translation branch). IDENTITY unaffected (no warp). AFFINE and multi-slot-concurrent-ROTZOOM remain refused. + +## Wedge gate (`a_real_aomenc_stream_with_interintra_wedge_decodes_pixel_exact`, `EC_AV1_REQUIRE_AOMENC=1`) + +- Before this round (r3): 27 other-capability refusals / 13 matches / wii_hits=3 out of 40. +- After this round: **30 other-capability refusals / 10 matches / wii_hits=4 out of 40**. `ok`. The refusal count rose because several seeds that used to fall through the (now-removed) blanket non-IDENTITY refusal now hit the new, narrower "concurrent ROTZOOM/AFFINE ref slots" refusal by name instead -- correctly refusing an unverified shape rather than attempting (and risking) a decode. + +## Gate ladder run this round + +- `scratch_isolate_pinned_mismatch` (pin, before/after, `EC_GM_DISABLE` AB test): as above. +- `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --release --lib a_real_aomenc_stream_with_interintra_wedge -- --nocapture`: `ok`, 30/40 refusals, 10/40 matches, wii_hits=4. +- `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --release --lib -- --skip a_real_aomenc_stream_with_interintra_wedge`: **227 passed, 0 failed, 17 ignored, 1 filtered** -- unchanged from r3/main baseline. Includes the 14-pin list (last2/last3/bwdref/altref2/altref reference gates, temporal-mvs, obmc/obmc_8x8, interintra, masked-compound, free-partitions, ab-partitions, compound-references, reference_select, warped_motion) -- all `ok`, inert for the IDENTITY-gm pins as expected. + +## Files touched +- `crates/ec-av1/src/warp.rs` -- `global_warp_params`, reusing `get_shear_params`. +- `crates/ec-av1/src/decode.rs` -- the `global_warp_allowed` branch wiring in the single-ref inter block path. +- `crates/ec-av1/src/stream.rs` -- whole-frame refusal replaced by the two named refusals above. + +## Next round +Root-cause frame 14's mismatch: two concurrently active ROTZOOM ref slots (`GOLDEN_FRAME`, `BWDREF_FRAME`), `reference_select=false` (ruled out as compound), small delta-1 first divergence at row 0 col 18 growing across frames -- same propagating shape class as r3's bug, but NOT the one this round fixed (AB-confirmed via `EC_GM_DISABLE`). Candidates not yet checked: per-ref indexing into `global_motion` when multiple slots are simultaneously non-identity (unlikely, same code path per ref), BWDREF_FRAME-specific reference-buffer/ordering interaction newly exercised now that this class of frame decodes at all (previously wholly refused), or a genuine second warp-related bug distinct from the block-centre `-1` one r3 already fixed. Re-pin with `EC_WEDGE_GATE_ATTEMPTS` to capture a fresh seed whose first mismatch is this exact shape, then range-ladder it the same way r3 did. Compound `GLOBAL_GLOBALMV` warp (the `predict_compound_intermediate`/`combine_compound` call site) is a SEPARATE, still-completely-unwired gap -- only one `warp::warp_affine` call site exists in the file (single-ref); deferred, no evidence yet that any gate fixture reaches it (this round's `reference_select` check on the mismatched frame came back false).