From aaa4b1b330a3a7627d523a5829003df9a656ee5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 30 Aug 2026 16:43:37 +0300 Subject: [PATCH] feat(av1): inter frame per-tile decode loop, MV stack bounded per tile decode_inter_frame_tile_with_cdfs now takes tiles: &[&[u8]] + &TileInfo and grows the same per-tile loop decode_key_frame_tile_with_cdfs already has: fresh SymbolDecoder + CDF copy per tile, PlaneBuf::set_tile_origin threaded with real x1/y1 (right/bottom reach bound), only context_update_tile_id's own adapted table kept as the frame's own output. mvstack.rs: MiGrid gets tile_row0/col0/row1/col1 bounds (set_tile_bounds, called once per tile) so scan_row/scan_col/scan_top_right's grid.get() cannot see a cell outside the current tile -- bounds all four find_mv_stack*/find_mv_stack_compound call sites through the one grid they all read, without touching their own signatures. Lifts the "an inter frame with more than one tile" refusal (verbatim, removed from stream.rs and refusal_inventory.rs's pinned list). The >2-columns and multi-tile-with-loop-filter refusals stay -- not gate-proven safe to lift this round (deferred, see report). cargo check clean; scoped tests green: tile/frame/mvstack/stream tile tests (44), refusal_inventory (2), gate_coverage (2), including the existing mi_units_outside_the_tile_contribute_nothing tile-bound test and both real-aomenc two-tile-column gates. --- crates/ec-av1/src/decode.rs | 101 ++++++++++++++++++++----- crates/ec-av1/src/mvstack.rs | 36 ++++++++- crates/ec-av1/src/refusal_inventory.rs | 1 - crates/ec-av1/src/stream.rs | 15 ++-- 4 files changed, 124 insertions(+), 29 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 3cbe7599..67393524 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -9486,8 +9486,14 @@ pub fn decode_inter_frame_tile( enable_dual_filter: bool, reference_select: bool, ) -> Result { + let single_tile = TileInfo { + mi_col_starts: vec![0, mi_cols], + mi_row_starts: vec![0, mi_rows], + ..TileInfo::default() + }; decode_inter_frame_tile_with_cdfs( - data, + &[data], + &single_tile, mi_cols, mi_rows, base_q_idx, @@ -9528,7 +9534,8 @@ pub fn decode_inter_frame_tile( /// `primary_ref_frame == PRIMARY_REF_NONE` too (an error-resilient stream), /// so `None` is a real case here, not only a convenience default. pub(crate) fn decode_inter_frame_tile_with_cdfs( - data: &[u8], + tiles: &[&[u8]], + tile_info: &TileInfo, mi_cols: u32, mi_rows: u32, base_q_idx: u8, @@ -9733,18 +9740,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( let scan8 = default_scan(TX8); let scan4 = default_scan(TX4); - let mut cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx))); - let mut dec = SymbolDecoder::new(data); - // lane-comppin r9: tile-entry range, the earliest point comparable - // against aomdec's own `r->ec.rng` right after `aom_reader_init` -- the - // first ladder rung before any symbol (partition or otherwise) is read. - if std::env::var_os("EC_AV1_TELL").is_some() { - eprintln!( - "TELL label=tile_init tell={} range={}", - dec.debug_bitpos(), - dec.debug_state().0 - ); - } + let base_cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx))); + let mut result_cdfs = base_cdfs.clone(); let mut neighbours = Neighbours::new( cols as usize * 2, rows as usize * 2, @@ -9753,9 +9750,72 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( ); let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); - for sb_r in 0..sb_rows { + // lane-tiles r6: mirrors decode_key_frame_tile_with_cdfs's own per-tile + // loop (spec 5.11.2 decode_tile / 7.20 exit_symbol) -- each tile gets + // its own fresh SymbolDecoder over its own byte range and its own fresh + // copy of the frame's initial CDFs; only context_update_tile_id's own + // end-of-tile adapted table becomes the frame's own output. + for (tile_idx, &data) in tiles.iter().enumerate() { + let tile_num = tile_idx as u32; + let (trow, tcol) = (tile_num / tile_info.cols, tile_num % tile_info.cols); + let mi_row0 = tile_info.mi_row_starts[trow as usize]; + let mi_row1 = tile_info.mi_row_starts[trow as usize + 1]; + let mi_col0 = tile_info.mi_col_starts[tcol as usize]; + let mi_col1 = tile_info.mi_col_starts[tcol as usize + 1]; + let (sb_r0, sb_r1) = ( + (mi_row0 / SB_MI).min(sb_rows), + mi_row1.div_ceil(SB_MI).min(sb_rows), + ); + let (sb_c0, sb_c1) = ( + (mi_col0 / SB_MI).min(sb_cols), + mi_col1.div_ceil(SB_MI).min(sb_cols), + ); + let mut cdfs = base_cdfs.clone(); + let mut dec = SymbolDecoder::new(data); + // lane-comppin r9: tile-entry range, the earliest point comparable + // against aomdec's own `r->ec.rng` right after `aom_reader_init` -- the + // first ladder rung before any symbol (partition or otherwise) is read. + if std::env::var_os("EC_AV1_TELL").is_some() { + eprintln!( + "TELL label=tile_init tell={} range={}", + dec.debug_bitpos(), + dec.debug_state().0 + ); + } + neighbours.start_tile(mi_row0 as usize, mi_col0 as usize, mi_col1 as usize); + y.set_tile_origin( + mi_col0 as usize * 4, + mi_row0 as usize * 4, + (mi_col1 as usize * 4).min(y.width), + (mi_row1 as usize * 4).min(y.height), + ); + u.set_tile_origin( + mi_col0 as usize * 2, + mi_row0 as usize * 2, + (mi_col1 as usize * 2).min(u.width), + (mi_row1 as usize * 2).min(u.height), + ); + v.set_tile_origin( + mi_col0 as usize * 2, + mi_row0 as usize * 2, + (mi_col1 as usize * 2).min(v.width), + (mi_row1 as usize * 2).min(v.height), + ); + // lane-tiles r6: an MV candidate scan (mvstack.rs's grid.get) must + // not reach across a tile boundary any more than intra prediction's + // PlaneBuf reach does -- bounding the shared grid's own read window + // per tile is equivalent to threading tile bounds through every + // find_mv_stack* call site (all four read through this one grid). + grid.set_tile_bounds( + mi_row0 as usize, + mi_col0 as usize, + mi_row1 as usize, + mi_col1 as usize, + ); + + for sb_r in sb_r0..sb_r1 { neighbours.start_row(); - for sb_c in 0..sb_cols { + for sb_c in sb_c0..sb_c1 { CDEF_TRANSMITTED.with(|c| c.set(false)); let sb_at = (sb_r as usize * 4, sb_c as usize * 4); let sb_ctx = neighbours.partition_ctx(sb_at, SB); @@ -10945,6 +11005,11 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( } } + if tile_num == tile_info.context_update_tile_id { + result_cdfs = cdfs; + } + } + // lane-comppin r4: pre-loop-filter decode-order dump, matching aomdec's // own EC_AV1_PREFILT_DUMP shape (decodeframe.c ~5451) -- diffs against // that isolate whether a decode-order frame's mismatch already exists @@ -10982,7 +11047,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( u: u.data, v: v.data, }, - cdfs, + result_cdfs, motion_field, )); } @@ -11001,7 +11066,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( u: crop(&u, fw / 2, fh / 2), v: crop(&v, fw / 2, fh / 2), }, - cdfs, + result_cdfs, motion_field, )) } diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 8aa29d96..96284d7c 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -177,6 +177,15 @@ pub struct MiGrid { cols: usize, rows: usize, cells: Vec>, + /// lane-tiles r6: the current tile's own mi-unit bounds (spec: an MV + /// candidate scan never reaches across a tile boundary, mirroring + /// `PlaneBuf`'s `tile_x0`/`tile_x1` reach clamp for intra prediction). + /// Defaults to the whole grid (the single-tile case, unchanged old + /// behaviour) and is narrowed per tile via [`Self::set_tile_bounds`]. + tile_row0: usize, + tile_col0: usize, + tile_row1: usize, + tile_col1: usize, } impl MiGrid { @@ -186,9 +195,24 @@ impl MiGrid { cols, rows, cells: vec![None; cols * rows], + tile_row0: 0, + tile_col0: 0, + tile_row1: rows, + tile_col1: cols, } } + /// Narrows [`Self::get`]'s own read window to one tile's mi-unit span + /// (`row1`/`col1` exclusive) -- called once per tile, before that + /// tile's own superblock walk, the same way `Neighbours::start_tile` + /// and `PlaneBuf::set_tile_origin` are. + pub fn set_tile_bounds(&mut self, row0: usize, col0: usize, row1: usize, col1: usize) { + self.tile_row0 = row0; + self.tile_col0 = col0; + self.tile_row1 = row1; + self.tile_col1 = col1; + } + /// Records the unit at `(row, col)`. Out-of-range coordinates are a /// no-op — the caller only ever addresses units inside the grid it built. pub fn set(&mut self, row: usize, col: usize, info: MiInfo) { @@ -197,10 +221,16 @@ impl MiGrid { } } - /// The unit at `(row, col)`, or `None` when it is outside the grid (the - /// tile) or hasn't been coded. + /// 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> { - if row < self.rows && col < self.cols { + if row < self.rows + && col < self.cols + && row >= self.tile_row0 + && row < self.tile_row1 + && col >= self.tile_col0 + && col < self.tile_col1 + { self.cells[row * self.cols + col].as_ref() } else { None diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 42b4bed6..a6acac24 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -51,7 +51,6 @@ const REFUSALS: &[&str] = &[ "a frame with more than one tile row (only tile columns are proven pixel-exact so far)", "a frame with more than two tile columns (a non-last column's right-edge reach bound past column 0 is unimplemented)", "a multi-tile frame with loop filtering enabled (deblocking crosses tile boundaries by spec default; PlaneBuf's tile origin does not clip a non-last column's right-edge reach)", - "an inter frame with more than one tile (the inter tile-decode path has no per-tile loop, only key frames do)", "a frame with no mode-info grid", "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 2f814320..0e20344d 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -308,12 +308,12 @@ pub fn decode_stream(data: &[u8]) -> Result> { // is capped at 2. Tile rows are untested this round, so `rows` // stays capped at 1. let loop_filter_on = header.loop_filter.level.iter().any(|&l| l != 0); - if header.frame_type != FrameType::Key && header.tile_info.cols > 1 { - return Err(Error::unsupported( - "AV1 decode_stream", - "an inter frame with more than one tile (the inter tile-decode path has no per-tile loop, only key frames do)", - )); - } + // lane-tiles r6: decode_inter_frame_tile_with_cdfs now takes + // `tiles: &[&[u8]]` and grows the same per-tile loop + // decode_key_frame_tile_with_cdfs has (mvstack.rs's MiGrid bounded + // per tile too, see MiGrid::set_tile_bounds), so an inter frame with + // >1 tile column is no longer refused by name -- the >2-columns and + // loop-filter refusals below stay (unproven this round). if header.tile_info.rows > 1 { return Err(Error::unsupported( "AV1 decode_stream", @@ -472,7 +472,8 @@ pub fn decode_stream(data: &[u8]) -> Result> { ) }); decode_inter_frame_tile_with_cdfs( - tile_bytes, + &tile_bufs, + &header.tile_info, header.mi_cols, header.mi_rows, header.quantization.base_q_idx,