From ba5351f32be9ced7d53bb7ce6ce285f4a351e62e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 17:54:16 +0300 Subject: [PATCH] fix(av1): the saved motion field kept only the LAST tile's columns -- av1_copy_frame_mvs read the mi grid through the per-tile reach clamp `build_motion_field` (this crate's `av1_copy_frame_mvs`) walks the whole frame's mi grid AFTER the tile loop has finished, when `MiGrid`'s tile window is still narrowed to the last tile. `MiGrid::get` returns `None` outside that window, so every tile but the last saved no motion vectors at all: on the pinned 192x68 2-tile-column stream the saved field held only mi_col 32..47, and the next frame's temporal (MFMV) candidates over the whole left two thirds silently vanished from the MV stack. The entropy stream still matched symbol for symbol -- a missing weight-2 temporal vote only reorders the stack -- so a NEW_NEWMV block at mi(0,24) took stack[1] = (36,0) (the compound extension's zero-fill) instead of libaom's (0,0), predicting 32 px off and leaving a 32-px-wide wrong column band. New `MiGrid::get_frame` is the unclamped frame-level read; only `build_motion_field` uses it. Rungs added with it: `EC_TPL` (per frame: use_ref_frame_mvs + filled projected cells) and `EC_TRACE_TPL`'s new `EC_TPL_SAVED` occupancy map of the saved field, which is what showed the 16-column hole. Pre-filter reconstruction of all 6 decode-order frames of the pinned stream goes 1306/3358/5027/6622 wrong luma px (frames 2..5) -> 0. --- crates/ec-av1/src/decode.rs | 8 +++++++- crates/ec-av1/src/motion_field.rs | 19 +++++++++++++++++++ crates/ec-av1/src/mvstack.rs | 16 ++++++++++++++++ crates/ec-av1/src/stream.rs | 8 ++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 5f0a0812..59978722 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2997,7 +2997,7 @@ pub(crate) fn build_motion_field( const REFMVS_LIMIT: i32 = (1 << 12) - 1; for row in 0..mi_rows { for col in 0..mi_cols { - let Some(info) = grid.get(row, col) else { + let Some(info) = grid.get_frame(row, col) else { continue; }; // `av1_copy_frame_mvs` runs for EVERY block of an inter frame and @@ -3036,6 +3036,12 @@ pub(crate) fn build_motion_field( field.set(row, col, saved); } } + if std::env::var_os("EC_TRACE_TPL").is_some() { + eprintln!("EC_TPL_SAVED oh={order_hint}"); + for (r, row) in field.occupancy().iter().enumerate() { + eprintln!("EC_TPL_SAVED r{r} {row}"); + } + } field } diff --git a/crates/ec-av1/src/motion_field.rs b/crates/ec-av1/src/motion_field.rs index ac29d6fa..5768e499 100644 --- a/crates/ec-av1/src/motion_field.rs +++ b/crates/ec-av1/src/motion_field.rs @@ -75,6 +75,18 @@ impl MotionField { } } + /// lane-mergefix r5 rung (`EC_TRACE_TPL`): occupancy map of the SAVED + /// field, the input side of [`setup_motion_field`]'s projection. + pub fn occupancy(&self) -> Vec { + (0..self.rows) + .map(|r| { + (0..self.cols) + .map(|c| if self.cells[r * self.cols + c].is_some() { '#' } else { '.' }) + .collect() + }) + .collect() + } + fn get(&self, row8: usize, col8: usize) -> Option { if row8 < self.rows && col8 < self.cols { self.cells[row8 * self.cols + col8] @@ -95,6 +107,13 @@ pub struct TplField { } impl TplField { + /// lane-mergefix r5 rung (`EC_TPL`): how many 8x8 cells the projection + /// actually filled -- an empty field means every temporal candidate is + /// silently absent while the entropy stream still matches. + pub fn filled_cells(&self) -> usize { + self.cells.iter().filter(|c| c.is_some()).count() + } + fn get(&self, row8: usize, col8: usize) -> Option<((i32, i32), i32)> { if row8 < self.rows && col8 < self.cols { self.cells[row8 * self.cols + col8] diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 4755c69c..ad6ccc21 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -274,6 +274,22 @@ impl MiGrid { None } } + + /// The unit at `(row, col)` ignoring the tile window -- the frame-level + /// read `av1_copy_frame_mvs` (this crate's + /// `crate::decode::build_motion_field`) needs: it stores EVERY block of + /// the frame, and it runs after the last tile has already narrowed + /// [`Self::set_tile_bounds`], so reading it through [`Self::get`] saved + /// 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> { + if row < self.rows && col < self.cols { + self.cells[row * self.cols + col].as_ref() + } else { + None + } + } } /// One entry of the reference MV stack: a candidate motion vector and the diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 9aebbe31..5f7d9e88 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -766,6 +766,14 @@ pub fn decode_stream(data: &[u8]) -> Result> { header.mi_cols as usize, ) }); + if std::env::var_os("EC_TPL").is_some() { + eprintln!( + "EC_TPL order_hint={} use_ref_frame_mvs={} cells={}", + header.order_hint, + header.use_ref_frame_mvs, + tpl_field.as_ref().map_or(0, crate::motion_field::TplField::filled_cells) + ); + } let (picture, end_cdfs, motion_field) = decode_inter_frame_tile_with_cdfs( &tile_bufs, &header.tile_info,