Inter frame per-tile decode loop, MV stack bounded per tile
Browse files at this commitaaa4b1b330a3a7627d523a5829003df9a656ee5bParent: 1a6617f- feat
- inter frame per-tile decode loop, MV stack bounded per tile
4 changed files +124 -29
- crates/ec-av1/src/decode.rs +83 -18
- crates/ec-av1/src/mvstack.rs +33 -3
- crates/ec-av1/src/refusal_inventory.rs +0 -1
- crates/ec-av1/src/stream.rs +8 -7
crates/ec-av1/src/decode.rsmodified+83 -18
| @@ -9486,8 +9486,14 @@ pub fn decode_inter_frame_tile( | ||
| 9486 | 9486 | enable_dual_filter: bool, |
| 9487 | 9487 | reference_select: bool, |
| 9488 | 9488 | ) -> 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 | + }; | |
| 9489 | 9494 | decode_inter_frame_tile_with_cdfs( |
| 9490 | - data, | |
| 9495 | + &[data], | |
| 9496 | + &single_tile, | |
| 9491 | 9497 | mi_cols, |
| 9492 | 9498 | mi_rows, |
| 9493 | 9499 | base_q_idx, |
| @@ -9528,7 +9534,8 @@ pub fn decode_inter_frame_tile( | ||
| 9528 | 9534 | /// `primary_ref_frame == PRIMARY_REF_NONE` too (an error-resilient stream), |
| 9529 | 9535 | /// so `None` is a real case here, not only a convenience default. |
| 9530 | 9536 | pub(crate) fn decode_inter_frame_tile_with_cdfs( |
| 9531 | - data: &[u8], | |
| 9537 | + tiles: &[&[u8]], | |
| 9538 | + tile_info: &TileInfo, | |
| 9532 | 9539 | mi_cols: u32, |
| 9533 | 9540 | mi_rows: u32, |
| 9534 | 9541 | base_q_idx: u8, |
| @@ -9733,18 +9740,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 9733 | 9740 | let scan8 = default_scan(TX8); |
| 9734 | 9741 | let scan4 = default_scan(TX4); |
| 9735 | 9742 | |
| 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(); | |
| 9748 | 9745 | let mut neighbours = Neighbours::new( |
| 9749 | 9746 | cols as usize * 2, |
| 9750 | 9747 | rows as usize * 2, |
| @@ -9753,9 +9750,72 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 9753 | 9750 | ); |
| 9754 | 9751 | let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); |
| 9755 | 9752 | |
| 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 { | |
| 9757 | 9817 | neighbours.start_row(); |
| 9758 | - for sb_c in 0..sb_cols { | |
| 9818 | + for sb_c in sb_c0..sb_c1 { | |
| 9759 | 9819 | CDEF_TRANSMITTED.with(|c| c.set(false)); |
| 9760 | 9820 | let sb_at = (sb_r as usize * 4, sb_c as usize * 4); |
| 9761 | 9821 | let sb_ctx = neighbours.partition_ctx(sb_at, SB); |
| @@ -10945,6 +11005,11 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 10945 | 11005 | } |
| 10946 | 11006 | } |
| 10947 | 11007 | |
| 11008 | + if tile_num == tile_info.context_update_tile_id { | |
| 11009 | + result_cdfs = cdfs; | |
| 11010 | + } | |
| 11011 | + } | |
| 11012 | + | |
| 10948 | 11013 | // lane-comppin r4: pre-loop-filter decode-order dump, matching aomdec's |
| 10949 | 11014 | // own EC_AV1_PREFILT_DUMP shape (decodeframe.c ~5451) -- diffs against |
| 10950 | 11015 | // that isolate whether a decode-order frame's mismatch already exists |
| @@ -10982,7 +11047,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 10982 | 11047 | u: u.data, |
| 10983 | 11048 | v: v.data, |
| 10984 | 11049 | }, |
| 10985 | - cdfs, | |
| 11050 | + result_cdfs, | |
| 10986 | 11051 | motion_field, |
| 10987 | 11052 | )); |
| 10988 | 11053 | } |
| @@ -11001,7 +11066,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11001 | 11066 | u: crop(&u, fw / 2, fh / 2), |
| 11002 | 11067 | v: crop(&v, fw / 2, fh / 2), |
| 11003 | 11068 | }, |
| 11004 | - cdfs, | |
| 11069 | + result_cdfs, | |
| 11005 | 11070 | motion_field, |
| 11006 | 11071 | )) |
| 11007 | 11072 | } |
crates/ec-av1/src/mvstack.rsmodified+33 -3
| @@ -177,6 +177,15 @@ pub struct MiGrid { | ||
| 177 | 177 | cols: usize, |
| 178 | 178 | rows: usize, |
| 179 | 179 | 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, | |
| 180 | 189 | } |
| 181 | 190 | |
| 182 | 191 | impl MiGrid { |
| @@ -186,9 +195,24 @@ impl MiGrid { | ||
| 186 | 195 | cols, |
| 187 | 196 | rows, |
| 188 | 197 | cells: vec![None; cols * rows], |
| 198 | + tile_row0: 0, | |
| 199 | + tile_col0: 0, | |
| 200 | + tile_row1: rows, | |
| 201 | + tile_col1: cols, | |
| 189 | 202 | } |
| 190 | 203 | } |
| 191 | 204 | |
| 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 | + | |
| 192 | 216 | /// Records the unit at `(row, col)`. Out-of-range coordinates are a |
| 193 | 217 | /// no-op — the caller only ever addresses units inside the grid it built. |
| 194 | 218 | pub fn set(&mut self, row: usize, col: usize, info: MiInfo) { |
| @@ -197,10 +221,16 @@ impl MiGrid { | ||
| 197 | 221 | } |
| 198 | 222 | } |
| 199 | 223 | |
| 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`]). | |
| 202 | 226 | 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 | + { | |
| 204 | 234 | self.cells[row * self.cols + col].as_ref() |
| 205 | 235 | } else { |
| 206 | 236 | None |
crates/ec-av1/src/refusal_inventory.rsmodified+0 -1
| @@ -51,7 +51,6 @@ const REFUSALS: &[&str] = &[ | ||
| 51 | 51 | "a frame with more than one tile row (only tile columns are proven pixel-exact so far)", |
| 52 | 52 | "a frame with more than two tile columns (a non-last column's right-edge reach bound past column 0 is unimplemented)", |
| 53 | 53 | "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)", | |
| 55 | 54 | "a frame with no mode-info grid", |
| 56 | 55 | "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", |
| 57 | 56 | "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>> { | ||
| 308 | 308 | // is capped at 2. Tile rows are untested this round, so `rows` |
| 309 | 309 | // stays capped at 1. |
| 310 | 310 | 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). | |
| 317 | 317 | if header.tile_info.rows > 1 { |
| 318 | 318 | return Err(Error::unsupported( |
| 319 | 319 | "AV1 decode_stream", |
| @@ -472,7 +472,8 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 472 | 472 | ) |
| 473 | 473 | }); |
| 474 | 474 | decode_inter_frame_tile_with_cdfs( |
| 475 | - tile_bytes, | |
| 475 | + &tile_bufs, | |
| 476 | + &header.tile_info, | |
| 476 | 477 | header.mi_cols, |
| 477 | 478 | header.mi_rows, |
| 478 | 479 | header.quantization.base_q_idx, |