forge
Explore
Log in

Don't you dare go Hollow

Inter frame per-tile decode loop, MV stack bounded per tile

Browse files at this commit

aaa4b1b330a3a7627d523a5829003df9a656ee5bAhmet Kaan GümüşParent: 1a6617f
  • feat
    • inter frame per-tile decode loop, MV stack bounded per tile
Show full message
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.rsmodified+83 -18
@@ -9486,8 +9486,14 @@ pub fn decode_inter_frame_tile(
94869486 enable_dual_filter: bool,
94879487 reference_select: bool,
94889488 ) -> Result<Picture> {
9489+ let single_tile = TileInfo {
9490+ mi_col_starts: vec![0, mi_cols],
9491+ mi_row_starts: vec![0, mi_rows],
9492+ ..TileInfo::default()
9493+ };
94899494 decode_inter_frame_tile_with_cdfs(
9490- data,
9495+ &[data],
9496+ &single_tile,
94919497 mi_cols,
94929498 mi_rows,
94939499 base_q_idx,
@@ -9528,7 +9534,8 @@ pub fn decode_inter_frame_tile(
95289534 /// `primary_ref_frame == PRIMARY_REF_NONE` too (an error-resilient stream),
95299535 /// so `None` is a real case here, not only a convenience default.
95309536 pub(crate) fn decode_inter_frame_tile_with_cdfs(
9531- data: &[u8],
9537+ tiles: &[&[u8]],
9538+ tile_info: &TileInfo,
95329539 mi_cols: u32,
95339540 mi_rows: u32,
95349541 base_q_idx: u8,
@@ -9733,18 +9740,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
97339740 let scan8 = default_scan(TX8);
97349741 let scan4 = default_scan(TX4);
97359742
9736- let mut cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx)));
9737- let mut dec = SymbolDecoder::new(data);
9738- // lane-comppin r9: tile-entry range, the earliest point comparable
9739- // against aomdec's own `r->ec.rng` right after `aom_reader_init` -- the
9740- // first ladder rung before any symbol (partition or otherwise) is read.
9741- if std::env::var_os("EC_AV1_TELL").is_some() {
9742- eprintln!(
9743- "TELL label=tile_init tell={} range={}",
9744- dec.debug_bitpos(),
9745- dec.debug_state().0
9746- );
9747- }
9743+ let base_cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx)));
9744+ let mut result_cdfs = base_cdfs.clone();
97489745 let mut neighbours = Neighbours::new(
97499746 cols as usize * 2,
97509747 rows as usize * 2,
@@ -9753,9 +9750,72 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
97539750 );
97549751 let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize);
97559752
9756- for sb_r in 0..sb_rows {
9753+ // lane-tiles r6: mirrors decode_key_frame_tile_with_cdfs's own per-tile
9754+ // loop (spec 5.11.2 decode_tile / 7.20 exit_symbol) -- each tile gets
9755+ // its own fresh SymbolDecoder over its own byte range and its own fresh
9756+ // copy of the frame's initial CDFs; only context_update_tile_id's own
9757+ // end-of-tile adapted table becomes the frame's own output.
9758+ for (tile_idx, &data) in tiles.iter().enumerate() {
9759+ let tile_num = tile_idx as u32;
9760+ let (trow, tcol) = (tile_num / tile_info.cols, tile_num % tile_info.cols);
9761+ let mi_row0 = tile_info.mi_row_starts[trow as usize];
9762+ let mi_row1 = tile_info.mi_row_starts[trow as usize + 1];
9763+ let mi_col0 = tile_info.mi_col_starts[tcol as usize];
9764+ let mi_col1 = tile_info.mi_col_starts[tcol as usize + 1];
9765+ let (sb_r0, sb_r1) = (
9766+ (mi_row0 / SB_MI).min(sb_rows),
9767+ mi_row1.div_ceil(SB_MI).min(sb_rows),
9768+ );
9769+ let (sb_c0, sb_c1) = (
9770+ (mi_col0 / SB_MI).min(sb_cols),
9771+ mi_col1.div_ceil(SB_MI).min(sb_cols),
9772+ );
9773+ let mut cdfs = base_cdfs.clone();
9774+ let mut dec = SymbolDecoder::new(data);
9775+ // lane-comppin r9: tile-entry range, the earliest point comparable
9776+ // against aomdec's own `r->ec.rng` right after `aom_reader_init` -- the
9777+ // first ladder rung before any symbol (partition or otherwise) is read.
9778+ if std::env::var_os("EC_AV1_TELL").is_some() {
9779+ eprintln!(
9780+ "TELL label=tile_init tell={} range={}",
9781+ dec.debug_bitpos(),
9782+ dec.debug_state().0
9783+ );
9784+ }
9785+ neighbours.start_tile(mi_row0 as usize, mi_col0 as usize, mi_col1 as usize);
9786+ y.set_tile_origin(
9787+ mi_col0 as usize * 4,
9788+ mi_row0 as usize * 4,
9789+ (mi_col1 as usize * 4).min(y.width),
9790+ (mi_row1 as usize * 4).min(y.height),
9791+ );
9792+ u.set_tile_origin(
9793+ mi_col0 as usize * 2,
9794+ mi_row0 as usize * 2,
9795+ (mi_col1 as usize * 2).min(u.width),
9796+ (mi_row1 as usize * 2).min(u.height),
9797+ );
9798+ v.set_tile_origin(
9799+ mi_col0 as usize * 2,
9800+ mi_row0 as usize * 2,
9801+ (mi_col1 as usize * 2).min(v.width),
9802+ (mi_row1 as usize * 2).min(v.height),
9803+ );
9804+ // lane-tiles r6: an MV candidate scan (mvstack.rs's grid.get) must
9805+ // not reach across a tile boundary any more than intra prediction's
9806+ // PlaneBuf reach does -- bounding the shared grid's own read window
9807+ // per tile is equivalent to threading tile bounds through every
9808+ // find_mv_stack* call site (all four read through this one grid).
9809+ grid.set_tile_bounds(
9810+ mi_row0 as usize,
9811+ mi_col0 as usize,
9812+ mi_row1 as usize,
9813+ mi_col1 as usize,
9814+ );
9815+
9816+ for sb_r in sb_r0..sb_r1 {
97579817 neighbours.start_row();
9758- for sb_c in 0..sb_cols {
9818+ for sb_c in sb_c0..sb_c1 {
97599819 CDEF_TRANSMITTED.with(|c| c.set(false));
97609820 let sb_at = (sb_r as usize * 4, sb_c as usize * 4);
97619821 let sb_ctx = neighbours.partition_ctx(sb_at, SB);
@@ -10945,6 +11005,11 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1094511005 }
1094611006 }
1094711007
11008+ if tile_num == tile_info.context_update_tile_id {
11009+ result_cdfs = cdfs;
11010+ }
11011+ }
11012+
1094811013 // lane-comppin r4: pre-loop-filter decode-order dump, matching aomdec's
1094911014 // own EC_AV1_PREFILT_DUMP shape (decodeframe.c ~5451) -- diffs against
1095011015 // that isolate whether a decode-order frame's mismatch already exists
@@ -10982,7 +11047,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1098211047 u: u.data,
1098311048 v: v.data,
1098411049 },
10985- cdfs,
11050+ result_cdfs,
1098611051 motion_field,
1098711052 ));
1098811053 }
@@ -11001,7 +11066,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1100111066 u: crop(&u, fw / 2, fh / 2),
1100211067 v: crop(&v, fw / 2, fh / 2),
1100311068 },
11004- cdfs,
11069+ result_cdfs,
1100511070 motion_field,
1100611071 ))
1100711072 }
crates/ec-av1/src/mvstack.rsmodified+33 -3
@@ -177,6 +177,15 @@ pub struct MiGrid {
177177 cols: usize,
178178 rows: usize,
179179 cells: Vec<Option<MiInfo>>,
180+ /// lane-tiles r6: the current tile's own mi-unit bounds (spec: an MV
181+ /// candidate scan never reaches across a tile boundary, mirroring
182+ /// `PlaneBuf`'s `tile_x0`/`tile_x1` reach clamp for intra prediction).
183+ /// Defaults to the whole grid (the single-tile case, unchanged old
184+ /// behaviour) and is narrowed per tile via [`Self::set_tile_bounds`].
185+ tile_row0: usize,
186+ tile_col0: usize,
187+ tile_row1: usize,
188+ tile_col1: usize,
180189 }
181190
182191 impl MiGrid {
@@ -186,9 +195,24 @@ impl MiGrid {
186195 cols,
187196 rows,
188197 cells: vec![None; cols * rows],
198+ tile_row0: 0,
199+ tile_col0: 0,
200+ tile_row1: rows,
201+ tile_col1: cols,
189202 }
190203 }
191204
205+ /// Narrows [`Self::get`]'s own read window to one tile's mi-unit span
206+ /// (`row1`/`col1` exclusive) -- called once per tile, before that
207+ /// tile's own superblock walk, the same way `Neighbours::start_tile`
208+ /// and `PlaneBuf::set_tile_origin` are.
209+ pub fn set_tile_bounds(&mut self, row0: usize, col0: usize, row1: usize, col1: usize) {
210+ self.tile_row0 = row0;
211+ self.tile_col0 = col0;
212+ self.tile_row1 = row1;
213+ self.tile_col1 = col1;
214+ }
215+
192216 /// Records the unit at `(row, col)`. Out-of-range coordinates are a
193217 /// no-op — the caller only ever addresses units inside the grid it built.
194218 pub fn set(&mut self, row: usize, col: usize, info: MiInfo) {
@@ -197,10 +221,16 @@ impl MiGrid {
197221 }
198222 }
199223
200- /// The unit at `(row, col)`, or `None` when it is outside the grid (the
201- /// tile) or hasn't been coded.
224+ /// The unit at `(row, col)`, or `None` when it is outside the grid, hasn't
225+ /// been coded, or sits outside the current tile's own bounds ([`Self::set_tile_bounds`]).
202226 pub fn get(&self, row: usize, col: usize) -> Option<&MiInfo> {
203- if row < self.rows && col < self.cols {
227+ if row < self.rows
228+ && col < self.cols
229+ && row >= self.tile_row0
230+ && row < self.tile_row1
231+ && col >= self.tile_col0
232+ && col < self.tile_col1
233+ {
204234 self.cells[row * self.cols + col].as_ref()
205235 } else {
206236 None
crates/ec-av1/src/refusal_inventory.rsmodified+0 -1
@@ -51,7 +51,6 @@ const REFUSALS: &[&str] = &[
5151 "a frame with more than one tile row (only tile columns are proven pixel-exact so far)",
5252 "a frame with more than two tile columns (a non-last column's right-edge reach bound past column 0 is unimplemented)",
5353 "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)",
54- "an inter frame with more than one tile (the inter tile-decode path has no per-tile loop, only key frames do)",
5554 "a frame with no mode-info grid",
5655 "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)",
5756 "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding",
crates/ec-av1/src/stream.rsmodified+8 -7
@@ -308,12 +308,12 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
308308 // is capped at 2. Tile rows are untested this round, so `rows`
309309 // stays capped at 1.
310310 let loop_filter_on = header.loop_filter.level.iter().any(|&l| l != 0);
311- if header.frame_type != FrameType::Key && header.tile_info.cols > 1 {
312- return Err(Error::unsupported(
313- "AV1 decode_stream",
314- "an inter frame with more than one tile (the inter tile-decode path has no per-tile loop, only key frames do)",
315- ));
316- }
311+ // lane-tiles r6: decode_inter_frame_tile_with_cdfs now takes
312+ // `tiles: &[&[u8]]` and grows the same per-tile loop
313+ // decode_key_frame_tile_with_cdfs has (mvstack.rs's MiGrid bounded
314+ // per tile too, see MiGrid::set_tile_bounds), so an inter frame with
315+ // >1 tile column is no longer refused by name -- the >2-columns and
316+ // loop-filter refusals below stay (unproven this round).
317317 if header.tile_info.rows > 1 {
318318 return Err(Error::unsupported(
319319 "AV1 decode_stream",
@@ -472,7 +472,8 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
472472 )
473473 });
474474 decode_inter_frame_tile_with_cdfs(
475- tile_bytes,
475+ &tile_bufs,
476+ &header.tile_info,
476477 header.mi_cols,
477478 header.mi_rows,
478479 header.quantization.base_q_idx,