diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 3012099d..116942bb 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -221,7 +221,7 @@ use crate::msac::SymbolDecoder; use crate::mvstack::{ ALTREF_FRAME, ALTREF2_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST_FRAME, LAST2_FRAME, LAST3_FRAME, MiGrid, MiInfo, NO_REF1, NO_SIGN_BIAS, NeighbourRef, SignBiasTable, comp_reference_type_ctx, - find_mv_stack_with_sign_bias, reference_mode_ctx, + find_mv_stack_with_sign_bias, mv16, mv32, reference_mode_ctx, single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, uni_comp_ref_p1_ctx, }; @@ -3380,7 +3380,7 @@ fn record_intrabc_mi(mi_r: usize, mi_c: usize, n4: usize, dv: Option<(i32, i32)> is_inter: dv.is_some(), ref_frame: 0, ref_frame1: NO_REF1, - mv: dv.unwrap_or((0, 0)), + mv: mv16(dv.unwrap_or((0, 0))), mv1: (0, 0), is_new_mv: dv.is_some(), is_global_mv0: false, @@ -4854,8 +4854,8 @@ pub(crate) fn build_motion_field( // projected with the wrong reference distance. let mut saved: Option = None; for (rf, mv) in [ - (info.ref_frame, Some(info.mv)), - (info.ref_frame1.max(0), Some(info.mv1)), + (info.ref_frame, Some(mv32(info.mv))), + (info.ref_frame1.max(0), Some(mv32(info.mv1))), ] { let (Some(mv), true) = (mv, rf > 0) else { continue; @@ -22667,7 +22667,7 @@ fn overlappable_above( // neighbour PREDICTION at a frame edge, which picks the // 4-tap kernel below 8 px (class narrow-block-sharp-kernel); // the destination clip stays in `obmc_blend`. - out.push((col - mi_col, step.min(bw4), *info, src - mi_col)); + out.push((col - mi_col, step.min(bw4), info, src - mi_col)); } } col += step; @@ -22716,7 +22716,7 @@ fn overlappable_left( } // lane-t900 r14: `AOMMIN(xd->height, mi_step)` -- see // `overlappable_above`. - out.push((row - mi_row, step.min(bh4), *info, src - mi_row)); + out.push((row - mi_row, step.min(bh4), info, src - mi_row)); } } row += step; @@ -22787,7 +22787,7 @@ fn find_samples( // neighbour donates its sample at ITS center (aom pts y=-72 for a // 32x16 above strip; the square assumption put it at -136). let nb_bh = (info.size_h * 4) as i32; - crate::warp::record_sample(nb_bw, nb_bh, info.mv, row_offset, sign_r, col_offset, sign_c) + crate::warp::record_sample(nb_bw, nb_bh, mv32(info.mv), row_offset, sign_r, col_offset, sign_c) }; 'outer: { @@ -22803,8 +22803,8 @@ fn find_samples( do_tr = false; } if let Some(info) = above { - if single_ref_match(info) { - samples.push(rec(info, 0, -1, col_offset as i32, 1)); + if single_ref_match(&info) { + samples.push(rec(&info, 0, -1, col_offset as i32, 1)); if samples.len() >= MAX { break 'outer; } @@ -22817,8 +22817,8 @@ fn find_samples( let cell = grid.get(mi_row - 1, mi_col + i); let sw = cell.map_or(1, |c| c.size as usize).max(1); if let Some(info) = cell { - if single_ref_match(info) { - samples.push(rec(info, 0, -1, i as i32, 1)); + if single_ref_match(&info) { + samples.push(rec(&info, 0, -1, i as i32, 1)); if samples.len() >= MAX { break 'outer; } @@ -22838,8 +22838,8 @@ fn find_samples( do_tl = false; } if let Some(info) = left { - if single_ref_match(info) { - samples.push(rec(info, row_offset as i32, 1, 0, -1)); + if single_ref_match(&info) { + samples.push(rec(&info, row_offset as i32, 1, 0, -1)); if samples.len() >= MAX { break 'outer; } @@ -22852,8 +22852,8 @@ fn find_samples( let cell = grid.get(mi_row + i, mi_col - 1); let sh = cell.map_or(1, |c| c.size_h as usize).max(1); if let Some(info) = cell { - if single_ref_match(info) { - samples.push(rec(info, i as i32, 1, 0, -1)); + if single_ref_match(&info) { + samples.push(rec(&info, i as i32, 1, 0, -1)); if samples.len() >= MAX { break 'outer; } @@ -22866,8 +22866,8 @@ fn find_samples( if do_tl && left_available && up_available { if let Some(info) = grid.get(mi_row - 1, mi_col - 1) { - if single_ref_match(info) { - samples.push(rec(info, 0, -1, 0, -1)); + if single_ref_match(&info) { + samples.push(rec(&info, 0, -1, 0, -1)); if samples.len() >= MAX { break 'outer; } @@ -22890,8 +22890,8 @@ fn find_samples( let tr_col = mi_col + bw4; if mi_row > 0 && tr_col < mi_cols { if let Some(info) = grid.get(mi_row - 1, tr_col) { - if single_ref_match(info) { - samples.push(rec(info, 0, -1, bw4 as i32, 1)); + if single_ref_match(&info) { + samples.push(rec(&info, 0, -1, bw4 as i32, 1)); } } } @@ -23425,7 +23425,7 @@ fn obmc_blend( let above_syms = neighbours.above_filter[mi_col + src4]; obmcrec_probe( "above", mi_row, mi_col, write_w, write_h, off4, span4, &nb, above_syms, - grid.get(mi_row - 1, mi_col + src4).copied(), interp_fixed.is_some(), + grid.get(mi_row - 1, mi_col + src4), interp_fixed.is_some(), ); let (h_kind, v_kind) = neighbour_filter(interp_fixed, above_syms)?; ec_obmc_trace("above", mi_row, mi_col, write_w, write_h, off4, span4, &nb, h_kind, v_kind); @@ -23437,7 +23437,7 @@ fn obmc_blend( let (bw, bh, ox) = (span4 * 4, overlap_above, off4 * 4); // lane-t900 r14: the prediction is `op_mi_size` wide (kernel choice // included); only the WRITE is clipped to this block's own buffer. - obmc_neighbour_pred(ny, px + ox, py, nb.mv, bw, bh, true, h_kind, v_kind, nb_scale, &mut tmp_y, fctx); + obmc_neighbour_pred(ny, px + ox, py, mv32(nb.mv), bw, bh, true, h_kind, v_kind, nb_scale, &mut tmp_y, fctx); obmc_blend_v(pred_y, side, ox, 0, bw.min(write_w - ox), bh, bw, &tmp_y); if !skip_chroma_above { // lane-t900 r13 (measured, NOT a defect): libaom's min-4 clamp @@ -23449,9 +23449,9 @@ fn obmc_blend( // 13 B -> 11007 B. let (cbw, cbh, cox) = (bw / 2, overlap_above / 2, ox / 2); let cw = cbw.min(write_w / 2 - cox); - obmc_neighbour_pred(nu, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale, &mut tmp_u, fctx); + obmc_neighbour_pred(nu, cpx + cox, cpy, mv32(nb.mv), cbw, cbh, false, h_kind, v_kind, nb_scale, &mut tmp_u, fctx); obmc_blend_v(pred_u, chroma_side, cox, 0, cw, cbh, cbw, &tmp_u); - obmc_neighbour_pred(nv, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale, &mut tmp_v, fctx); + obmc_neighbour_pred(nv, cpx + cox, cpy, mv32(nb.mv), cbw, cbh, false, h_kind, v_kind, nb_scale, &mut tmp_v, fctx); obmc_blend_v(pred_v, chroma_side, cox, 0, cw, cbh, cbw, &tmp_v); } } @@ -23461,7 +23461,7 @@ fn obmc_blend( let left_syms = neighbours.left_filter[mi_row + src4]; obmcrec_probe( "left", mi_row, mi_col, write_w, write_h, off4, span4, &nb, left_syms, - grid.get(mi_row + src4, mi_col - 1).copied(), interp_fixed.is_some(), + grid.get(mi_row + src4, mi_col - 1), interp_fixed.is_some(), ); let (h_kind, v_kind) = neighbour_filter(interp_fixed, left_syms)?; ec_obmc_trace("left", mi_row, mi_col, write_w, write_h, off4, span4, &nb, h_kind, v_kind); @@ -23471,10 +23471,10 @@ fn obmc_blend( OBMC_EDGE_SPAN_HITS.with(|c| c.set(c.get() + 1)); } let (bw, bh, oy) = (overlap_left, span4 * 4, off4 * 4); - obmc_neighbour_pred(ny, px, py + oy, nb.mv, bw, bh, true, h_kind, v_kind, nb_scale, &mut tmp_y, fctx); + obmc_neighbour_pred(ny, px, py + oy, mv32(nb.mv), bw, bh, true, h_kind, v_kind, nb_scale, &mut tmp_y, fctx); obmc_blend_h(pred_y, side, 0, oy, bw, bh.min(write_h - oy), &tmp_y); let (cbw, cbh, coy) = (overlap_left / 2, bh / 2, oy / 2); - obmc_neighbour_pred(nu, cpx, cpy + coy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale, &mut tmp_u, fctx); + obmc_neighbour_pred(nu, cpx, cpy + coy, mv32(nb.mv), cbw, cbh, false, h_kind, v_kind, nb_scale, &mut tmp_u, fctx); if let Some((_, _, idx)) = ec_mcb { eprintln!( "OUR_MCB left f={idx} off4={off4} span4={span4} cbw={cbw} cbh={cbh} coy={coy} mv=({},{}) ref0={}", @@ -23487,7 +23487,7 @@ fn obmc_blend( } let ch = cbh.min(write_h / 2 - coy); obmc_blend_h(pred_u, chroma_side, 0, coy, cbw, ch, &tmp_u); - obmc_neighbour_pred(nv, cpx, cpy + coy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale, &mut tmp_v, fctx); + obmc_neighbour_pred(nv, cpx, cpy + coy, mv32(nb.mv), cbw, cbh, false, h_kind, v_kind, nb_scale, &mut tmp_v, fctx); obmc_blend_h(pred_v, chroma_side, 0, coy, cbw, ch, &tmp_v); } if let Some((_, _, idx)) = ec_mcb { @@ -24412,8 +24412,8 @@ fn decode_inter_block( is_inter: true, ref_frame: ref0, ref_frame1: ref1, - mv1, - mv: mv0, + mv1: mv16(mv1), + mv: mv16(mv0), is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), size: bw4 as u8, size_h: bh4 as u8, @@ -25986,7 +25986,7 @@ fn decode_inter_block( // num_proj_ref. ref_frame1: if interintra_mode.is_some() { 0 } else { NO_REF1 }, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv, size: bw4 as u8, size_h: bh4 as u8, @@ -27965,7 +27965,7 @@ fn decode_inter_sub8_split4( ref_frame, ref_frame1: NO_REF1, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv, size: 1, size_h: 1, @@ -29208,7 +29208,7 @@ fn grid_stamp_rect( ref_frame, ref_frame1: NO_REF1, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv, size: w_mi as u8, size_h: h_mi as u8, @@ -29786,8 +29786,8 @@ fn decode_inter_block8( is_inter: true, ref_frame: ref0, ref_frame1: ref1, - mv1, - mv: mv0, + mv1: mv16(mv1), + mv: mv16(mv0), is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), size: 2, size_h: 2, @@ -30464,7 +30464,7 @@ fn decode_inter_block8( // them out of warp-sample gathering (see 16/32 site). ref_frame1: if interintra_mode.is_some() { 0 } else { NO_REF1 }, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv, size: 2, size_h: 2, @@ -35742,12 +35742,12 @@ mod tests { let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); let (above_mv, left_mv) = ((4, 4), (8, 8)); - let neighbour = |mv| MiInfo { + let neighbour = |mv: (i32, i32)| MiInfo { is_inter: true, ref_frame: LAST_FRAME, ref_frame1: NO_REF1, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv: false, size: 1, size_h: 1, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index d645f3d5..6d0f2c08 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -28,7 +28,7 @@ use crate::frame::frame_obu; use crate::intra::{D67_PRED, DC_PRED, KEY_FRAME_MODES, V_PRED}; use crate::mc; use crate::motion; -use crate::mvstack::{MiGrid, MiInfo, MvStack, NO_REF1, find_mv_stack}; +use crate::mvstack::{MiGrid, MiInfo, MvStack, NO_REF1, find_mv_stack, mv16}; use crate::obu::temporal_delimiter; use crate::quant::ac_q; use crate::sequence::sequence_header_obu; @@ -3099,7 +3099,7 @@ pub(crate) fn encode_inter_frame( ref_frame: LAST_FRAME, ref_frame1: NO_REF1, mv1: (0, 0), - mv: info.mv, + mv: mv16(info.mv), is_new_mv: matches!(info.mode, InterMode::NewMv), size: 8, size_h: 8, @@ -3197,7 +3197,7 @@ pub(crate) fn encode_inter_frame( ref_frame: LAST_FRAME, ref_frame1: NO_REF1, mv1: (0, 0), - mv: info.mv, + mv: mv16(info.mv), is_new_mv: matches!(info.mode, InterMode::NewMv), size: 4, size_h: 4, @@ -3257,7 +3257,7 @@ pub(crate) fn encode_inter_frame( ref_frame: LAST_FRAME, ref_frame1: NO_REF1, mv1: (0, 0), - mv: info.mv, + mv: mv16(info.mv), is_new_mv: matches!( info.mode, InterMode::NewMv diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index a6ce6f80..9ab02e5d 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -161,6 +161,23 @@ pub struct TplArgs<'a> { /// -- as absent without an `Option` tag word per grid cell. pub const NO_REF1: i8 = -1; +/// lane-migrid: widen a stored [`MiInfo`] motion vector to the `(i32, i32)` +/// every consumer computes in. +pub fn mv32(mv: (i16, i16)) -> (i32, i32) { + (i32::from(mv.0), i32::from(mv.1)) +} + +/// lane-migrid: narrow a computed motion vector to [`MiInfo`]'s stored +/// width -- libaom's own `MV` (`int16_t row, col`), which is the spec's MV +/// range, so this is not a narrowing of anything the spec can produce. +pub fn mv16(mv: (i32, i32)) -> (i16, i16) { + debug_assert!( + i16::try_from(mv.0).is_ok() && i16::try_from(mv.1).is_ok(), + "mv outside the spec's i16 range: {mv:?}" + ); + (mv.0 as i16, mv.1 as i16) +} + /// One 4x4 `mi` unit's motion state, as the encode loop will have filled it /// in by the time it asks for a block's MV stack. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -181,10 +198,13 @@ pub struct MiInfo { /// in this slot). pub ref_frame1: i8, /// The unit's motion vector, `(row, col)`, in the spec's 1/8-pel units. - pub mv: (i32, i32), + /// lane-migrid: `i16`, which is libaom's own `MV` (`int16_t row, col`) + /// and the spec's MV range -- two of these plus the byte-sized fields + /// make the grid cell 16 bytes instead of 24. + pub mv: (i16, i16), /// The unit's second motion vector, matching `ref_frame1` (spec /// `Mvs[1]`) — `Some` only for a compound-coded unit, `None` otherwise. - pub mv1: (i32, i32), + pub mv1: (i16, i16), /// Whether this unit's own mode was `NEWMV` (spec's /// `have_newmv_in_inter_mode`, compound modes excluded since this module /// has no compound candidates). Feeds `NewMvContext` (7.10.2.8): a @@ -297,7 +317,7 @@ impl MiGrid { /// The unit at `(row, col)`, or `None` when it is outside the grid, hasn't /// been coded, or sits outside the current tile's own bounds ([`Self::set_tile_bounds`]). - pub fn get(&self, row: usize, col: usize) -> Option<&MiInfo> { + pub fn get(&self, row: usize, col: usize) -> Option { if row < self.rows && col < self.cols && row >= self.tile_row0 @@ -305,7 +325,7 @@ impl MiGrid { && col >= self.tile_col0 && col < self.tile_col1 { - self.cells[row * self.cols + col].as_ref() + self.cells[row * self.cols + col] } else { None } @@ -319,9 +339,9 @@ impl MiGrid { /// only the last tile's columns into the motion field and every other /// tile's temporal candidates silently vanished from the next frame's /// MV stack. - pub fn get_frame(&self, row: usize, col: usize) -> Option<&MiInfo> { + pub fn get_frame(&self, row: usize, col: usize) -> Option { if row < self.rows && col < self.cols { - self.cells[row * self.cols + col].as_ref() + self.cells[row * self.cols + col] } else { None } @@ -579,13 +599,13 @@ fn process_single_ref_mv_candidate( // 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 slots = [(candidate.ref_frame, mv32(candidate.mv)); 2]; let mut n_slots = 1; // `mv1` is meaningful only for a compound neighbour (`ref_frame1 > 0`); // an interintra one stores `INTRA_FRAME` (0) with no second MV, and used // to be excluded here by its `mv1` being `None`. if candidate.ref_frame1 > 0 { - slots[1] = (candidate.ref_frame1, candidate.mv1); + slots[1] = (candidate.ref_frame1, mv32(candidate.mv1)); n_slots = 2; } for &(rf, mv) in &slots[..n_slots] { @@ -679,7 +699,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, gm) { + 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); @@ -706,13 +726,13 @@ fn single_ref_match(info: &MiInfo, ref_frame: i8, gm: &GmMvTable) -> Option<(i32 Some(if info.is_global_mv0 { gm_mv(gm, ref_frame) } else { - info.mv + mv32(info.mv) }) } else if info.ref_frame1 > 0 && info.ref_frame1 == ref_frame { if info.is_global_mv1 { Some(gm_mv(gm, ref_frame)) } else { - Some(info.mv1) + Some(mv32(info.mv1)) } } else { None @@ -767,7 +787,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, gm) { + 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); @@ -875,7 +895,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, gm) + && let Some(mv) = single_ref_match(&info, ref_frame, gm) { add_candidate(candidates, mv, CORNER_WEIGHT); *newmv_count += u32::from(info.is_new_mv); @@ -903,7 +923,7 @@ fn scan_corner( return false; }; if let Some(info) = grid.get(row, col) - && let Some(mv) = single_ref_match(info, ref_frame, gm) + && let Some(mv) = single_ref_match(&info, ref_frame, gm) { add_candidate(candidates, mv, CORNER_WEIGHT); *newmv_count += u32::from(info.is_new_mv); @@ -1346,7 +1366,7 @@ pub fn find_mv_stack_with_sign_bias( let cand = grid.get(mi_row - 1, mi_col + idx); let step = cand.map_or(1, |c| c.size as usize).max(1); if let Some(c) = cand { - process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut candidates); + process_single_ref_mv_candidate(&c, ref_frame, sign_bias_table, &mut candidates); } idx += step; } @@ -1357,7 +1377,7 @@ pub fn find_mv_stack_with_sign_bias( let cand = grid.get(mi_row + idx, mi_col - 1); let step = cand.map_or(1, |c| c.size_h as usize).max(1); if let Some(c) = cand { - process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut candidates); + process_single_ref_mv_candidate(&c, ref_frame, sign_bias_table, &mut candidates); } idx += step; } @@ -1588,8 +1608,8 @@ fn scan_row_compound( found = true; add_compound_candidate( candidates, - 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 }, + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { mv32(info.mv) }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { mv32(info.mv1) }, len as u32 * weight, ); *newmv_count += u32::from(info.is_new_mv); @@ -1651,8 +1671,8 @@ fn scan_col_compound( found = true; add_compound_candidate( candidates, - 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 }, + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { mv32(info.mv) }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { mv32(info.mv1) }, len as u32 * weight, ); *newmv_count += u32::from(info.is_new_mv); @@ -1685,8 +1705,8 @@ fn scan_top_right_compound( { add_compound_candidate( candidates, - 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 }, + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { mv32(info.mv) }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { mv32(info.mv1) }, CORNER_WEIGHT, ); *newmv_count += u32::from(info.is_new_mv); @@ -1715,8 +1735,8 @@ fn scan_corner_compound( { add_compound_candidate( candidates, - 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 }, + if info.is_global_mv0 { gm_mv(gm, ref_frame.0) } else { mv32(info.mv) }, + if info.is_global_mv1 { gm_mv(gm, ref_frame.1) } else { mv32(info.mv1) }, CORNER_WEIGHT, ); *newmv_count += u32::from(info.is_new_mv); @@ -1757,11 +1777,11 @@ fn process_compound_ref_mv_candidate( return; } let slots = [ - Some((candidate.ref_frame, candidate.mv)), + Some((candidate.ref_frame, mv32(candidate.mv))), // An interintra neighbour stores INTRA_FRAME (0) here; libaom's // ref-diff scan requires `can_rf > INTRA_FRAME` // (process_compound_ref_mv_candidate), so it contributes nothing. - (candidate.ref_frame1 > 0).then_some((candidate.ref_frame1, candidate.mv1)), + (candidate.ref_frame1 > 0).then_some((candidate.ref_frame1, mv32(candidate.mv1))), ]; for (candidate_ref, candidate_mv) in slots.into_iter().flatten() { for (i, side_ref) in [ref_frame.0, ref_frame.1].into_iter().enumerate() { @@ -2126,7 +2146,7 @@ pub fn find_mv_stack_compound( let cand = grid.get(mi_row - 1, mi_col + idx); let step = cand.map_or(1, |c| c.size as usize).max(1); if let Some(c) = cand { - process_compound_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut lists); + process_compound_ref_mv_candidate(&c, ref_frame, sign_bias_table, &mut lists); } idx += step; } @@ -2137,7 +2157,7 @@ pub fn find_mv_stack_compound( let cand = grid.get(mi_row + idx, mi_col - 1); let step = cand.map_or(1, |c| c.size_h as usize).max(1); if let Some(c) = cand { - process_compound_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut lists); + process_compound_ref_mv_candidate(&c, ref_frame, sign_bias_table, &mut lists); } idx += step; } @@ -2526,7 +2546,7 @@ mod tests { #[test] fn mi_info_stays_small_enough_for_a_4k_grid() { assert!( - std::mem::size_of::>() <= 32, + std::mem::size_of::>() <= 16, "Option grew to {} bytes", std::mem::size_of::>() ); @@ -2540,10 +2560,13 @@ mod tests { /// the scan's memory traffic. `ref_frame1`/`mv1` are validity-tagged by /// `ref_frame1 == NO_REF1` rather than by two `Option` tags (32 bytes -> /// 24), and `is_inter` keeps a niche so the grid's `Option` costs - /// no extra word. + /// no extra word. lane-migrid: the two motion vectors ride as `i16` + /// pairs -- libaom's own `MV` (`int16_t row, col`), i.e. the spec's MV + /// range -- which takes the cell from 24 bytes to 16 and the grid's + /// index arithmetic from a multiply to a shift. #[test] fn mi_info_cell_stays_compact() { - assert_eq!(std::mem::size_of::(), 24); + assert_eq!(std::mem::size_of::(), 16); assert_eq!( std::mem::size_of::>(), std::mem::size_of::() @@ -2556,7 +2579,7 @@ mod tests { ref_frame: 1, ref_frame1: NO_REF1, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv: false, // 1: smaller than every `bw4`/`bh4` these small-grid tests use, // so it never trips the extended-scan coverage boost -- these @@ -2689,11 +2712,13 @@ mod tests { // A 4x4-mi frame (16x16 px), block 1x1 mi at (1, 1): room for one // neighbour above and one to the left, tight edges on every side. let mut grid = MiGrid::new(4, 4); - grid.set(0, 1, inter((100_000, 100_000))); + // lane-migrid: far outside this 16x16-px frame, but inside the + // spec's own MV width (`MiInfo` stores libaom's `int16_t` mv). + grid.set(0, 1, inter((30_000, 30_000))); let stack = find_mv_stack(&grid, 1, 1, 1, 1, 1, 4, 4); - // Unclamped this candidate is (100000, 100000); spec 7.10.2.14 caps + // Unclamped this candidate is (30000, 30000); spec 7.10.2.14 caps // it at the frame edge (2 mi below/right) plus the block's own 4x4 // span plus MV_BORDER (128), all in 1/8-pel: (2*4)*8 + 1*4*8 + 128 = // 64 + 32 + 128 = 224. @@ -2810,7 +2835,7 @@ mod tests { let stack = find_mv_stack(&grid, mi_row, mi_col, 2, 2, 1, 32, 32); assert_eq!(stack.entries.len(), 1); - assert_eq!(stack.entries[0].mv, mv); + assert_eq!(stack.entries[0].mv, mv32(mv)); assert_eq!(stack.entries[0].weight, 12 + REF_CAT_LEVEL); } @@ -2849,7 +2874,7 @@ mod tests { ref_frame: 1, ref_frame1: NO_REF1, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv: false, size: 8, size_h: 8, @@ -3088,8 +3113,8 @@ mod tests { is_inter: true, ref_frame: COMP_PAIR.0, ref_frame1: COMP_PAIR.1, - mv: mv0, - mv1, + mv: mv16(mv0), + mv1: mv16(mv1), is_new_mv: false, size: 1, size_h: 1, diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 7b6a05fa..d79cef34 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -16,7 +16,7 @@ use ec_core::{Error, Result}; use crate::cdf; use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; use crate::msac::SymbolEncoder; -use crate::mvstack::{MiGrid, MiInfo, NO_REF1, find_mv_stack, single_ref_ctx}; +use crate::mvstack::{MiGrid, MiInfo, NO_REF1, find_mv_stack, mv16, single_ref_ctx}; /// round-4 av1-truesize debugging aid: prints `msg()` to stderr when the /// `EC_RNG` environment variable is set, mirroring the `EC_PART`/`EC_TOK` @@ -2436,7 +2436,7 @@ pub fn sb_coeff_inter_frame_tile( ref_frame: LAST_FRAME, ref_frame1: NO_REF1, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv, size: 8, size_h: 8, @@ -2457,7 +2457,7 @@ pub fn sb_coeff_inter_frame_tile( ref_frame: LAST_FRAME, ref_frame1: NO_REF1, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv, size: 8, size_h: 8, @@ -2632,7 +2632,7 @@ fn write_inter_frame_leaf( ref_frame: LAST_FRAME, ref_frame1: NO_REF1, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv, size: SUB_MI as usize as u8, size_h: SUB_MI as usize as u8, @@ -2830,7 +2830,7 @@ fn write_inter_frame_leaf8( ref_frame: LAST_FRAME, ref_frame1: NO_REF1, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv, size: 2, size_h: 2, @@ -5140,7 +5140,7 @@ mod tests { ref_frame: 1, ref_frame1: NO_REF1, mv1: (0, 0), - mv, + mv: mv16(mv), is_new_mv, size: 8, size_h: 8,