forge
Explore
Log in

Don't you dare go Hollow

wip(av1): lane-tiles r2 per-tile decode in progress, committed at the 75-turn cap

Browse files at this commit

6416aca4f7ffcb1e66405b55d2b64021bfa65947Ahmet Kaan GümüşParent: 34716e8
Staged by the agent, committed verbatim by the orchestrator so it survives the
cap; state unverified by me.
crates/ec-av1/src/decode.rsmodified+137 -22
@@ -21,7 +21,7 @@
2121 //! too). Anything outside that refuses with [`Error::unsupported`] rather than
2222 //! silently miscoding.
2323
24-use ec_av1_syntax::{CdefParams, LoopFilterParams};
24+use ec_av1_syntax::{CdefParams, LoopFilterParams, TileInfo};
2525 use ec_core::{Error, Result};
2626
2727 use crate::cdf;
@@ -66,6 +66,20 @@ pub(crate) fn filter_intra_hits() -> usize {
6666 FILTER_INTRA_HITS.with(|c| c.get())
6767 }
6868
69+// How many tiles [`decode_key_frame_tile_with_cdfs`] has actually decoded a
70+// full superblock walk over, across every call on the current thread --
71+// lane-tiles r2's own gate-blind-to-feature guard: a gate over a
72+// multi-tile stream must prove more than one tile actually fired, not just
73+// that the stream carried tile_info.cols > 1.
74+thread_local! {
75+ static TILE_HITS: std::cell::Cell<usize> =
76+ const { std::cell::Cell::new(0) };
77+}
78+/// Current value of [`TILE_HITS`].
79+pub(crate) fn tile_hits() -> usize {
80+ TILE_HITS.with(|c| c.get())
81+}
82+
6983 // How many 4-pixel deblocking edge groups [`edge_params`] has actually
7084 // selected for filtering, across every call on the current thread -- the same
7185 // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream
@@ -2661,6 +2675,16 @@ struct PlaneBuf {
26612675 /// real decoder's edge or reach reads.
26622676 true_width: usize,
26632677 true_height: usize,
2678+ /// This plane's own tile's top-left pixel origin (spec: intra prediction
2679+ /// never reaches across a tile boundary even though an earlier-decoded
2680+ /// tile's real pixels sit right there in the shared buffer) — set via
2681+ /// [`Self::set_tile_origin`] before each tile's own superblock walk,
2682+ /// `0` for the single-tile case. lane-tiles r2: only the left/top bound
2683+ /// is threaded (the two-tile column-split stage 1 fixture's tile always
2684+ /// ends at the frame's own right/bottom edge); a non-last tile's own
2685+ /// right/bottom reach bound is still open, tracked in the report.
2686+ tile_x0: usize,
2687+ tile_y0: usize,
26642688 }
26652689
26662690 /// One entry per `MV_REFERENCE_FRAME` (index 0/1 unused -- `NONE`/
@@ -2672,6 +2696,15 @@ struct PlaneBuf {
26722696 type RefSlots<'a> = [Option<(&'a PlaneBuf, &'a PlaneBuf, &'a PlaneBuf)>; 8];
26732697
26742698 impl PlaneBuf {
2699+ /// Sets this plane's own tile pixel origin ([`Self::tile_x0`]/
2700+ /// [`Self::tile_y0`]) before that tile's own superblock walk. `x0`/`y0`
2701+ /// are already in this plane's own units (luma pixels for `y`, chroma
2702+ /// pixels — halved — for `u`/`v`).
2703+ fn set_tile_origin(&mut self, x0: usize, y0: usize) {
2704+ self.tile_x0 = x0;
2705+ self.tile_y0 = y0;
2706+ }
2707+
26752708 fn edges(
26762709 &self,
26772710 x: usize,
@@ -2693,14 +2726,15 @@ impl PlaneBuf {
26932726 own_down
26942727 }
26952728 .min(self.height);
2696- let above = (y > 0 && across > x)
2729+ let above = (y > self.tile_y0 && across > x)
26972730 .then(|| self.data[(y - 1) * self.width + x..][..across - x].to_vec());
2698- let left = (x > 0 && down > y).then(|| {
2731+ let left = (x > self.tile_x0 && down > y).then(|| {
26992732 (y..down)
27002733 .map(|row| self.data[row * self.width + x - 1])
27012734 .collect::<Vec<_>>()
27022735 });
2703- let corner = (x > 0 && y > 0).then(|| self.data[(y - 1) * self.width + x - 1]);
2736+ let corner =
2737+ (x > self.tile_x0 && y > self.tile_y0).then(|| self.data[(y - 1) * self.width + x - 1]);
27042738 (above, left, corner)
27052739 }
27062740
@@ -2729,14 +2763,15 @@ impl PlaneBuf {
27292763 own_down
27302764 }
27312765 .min(self.height);
2732- let above = (y > 0 && across > x)
2766+ let above = (y > self.tile_y0 && across > x)
27332767 .then(|| self.data[(y - 1) * self.width + x..][..across - x].to_vec());
2734- let left = (x > 0 && down > y).then(|| {
2768+ let left = (x > self.tile_x0 && down > y).then(|| {
27352769 (y..down)
27362770 .map(|row| self.data[row * self.width + x - 1])
27372771 .collect::<Vec<_>>()
27382772 });
2739- let corner = (x > 0 && y > 0).then(|| self.data[(y - 1) * self.width + x - 1]);
2773+ let corner =
2774+ (x > self.tile_x0 && y > self.tile_y0).then(|| self.data[(y - 1) * self.width + x - 1]);
27402775 (above, left, corner)
27412776 }
27422777
@@ -4490,8 +4525,14 @@ pub fn decode_key_frame_tile(
44904525 allow_screen_content_tools: bool,
44914526 allow_intrabc: bool,
44924527 ) -> Result<Picture> {
4528+ let single_tile = TileInfo {
4529+ mi_col_starts: vec![0, mi_cols],
4530+ mi_row_starts: vec![0, mi_rows],
4531+ ..TileInfo::default()
4532+ };
44934533 decode_key_frame_tile_with_cdfs(
4494- data,
4534+ &[data],
4535+ &single_tile,
44954536 mi_cols,
44964537 mi_rows,
44974538 base_q_idx,
@@ -4519,7 +4560,8 @@ pub fn decode_key_frame_tile(
45194560 /// alongside the picture, for the caller to save into whichever reference
45204561 /// slots this frame's `refresh_frame_flags` names.
45214562 pub(crate) fn decode_key_frame_tile_with_cdfs(
4522- data: &[u8],
4563+ tiles: &[&[u8]],
4564+ tile_info: &TileInfo,
45234565 mi_cols: u32,
45244566 mi_rows: u32,
45254567 base_q_idx: u8,
@@ -4550,6 +4592,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
45504592 height,
45514593 true_width,
45524594 true_height,
4595+ tile_x0: 0,
4596+ tile_y0: 0,
45534597 };
45544598 let mut u = PlaneBuf {
45554599 data: vec![0u8; width * height / 4],
@@ -4557,6 +4601,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
45574601 height: height / 2,
45584602 true_width: true_width / 2,
45594603 true_height: true_height / 2,
4604+ tile_x0: 0,
4605+ tile_y0: 0,
45604606 };
45614607 let mut v = PlaneBuf {
45624608 data: vec![0u8; width * height / 4],
@@ -4564,6 +4610,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
45644610 height: height / 2,
45654611 true_width: true_width / 2,
45664612 true_height: true_height / 2,
4613+ tile_x0: 0,
4614+ tile_y0: 0,
45674615 };
45684616
45694617 let scan32 = default_scan(TX32);
@@ -4571,15 +4619,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
45714619 let scan8 = default_scan(TX8);
45724620 let scan4 = default_scan(TX4);
45734621
4574- let mut cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx)));
4575- if std::env::var_os("EC_AV1_TRACE").is_some() {
4576- eprintln!(
4577- "TRACE key_tile_bytes len={} first8={:02x?} base_q_idx={base_q_idx} mi_cols={mi_cols} mi_rows={mi_rows}",
4578- data.len(),
4579- &data[..data.len().min(8)]
4580- );
4581- }
4582- let mut dec = SymbolDecoder::new(data);
4622+ let base_cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx)));
4623+ let mut result_cdfs = base_cdfs.clone();
45834624 let mut neighbours = Neighbours::new(
45844625 cols32 as usize * 2,
45854626 rows32 as usize * 2,
@@ -4587,9 +4628,50 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
45874628 mi_rows as usize,
45884629 );
45894630
4590- for sb_r in 0..sb_rows {
4631+ // lane-tiles r2: each tile gets its own fresh `SymbolDecoder` over its
4632+ // own byte range and its own fresh copy of the frame's initial CDFs
4633+ // (spec 5.11.2 `decode_tile`'s `init_symbol`); only the tile named by
4634+ // `context_update_tile_id` has its end-of-tile adapted table kept as the
4635+ // frame's own output (spec `exit_symbol`) -- every other tile's
4636+ // adaptation is discarded, matching the refusal this replaces.
4637+ for (tile_idx, &tile_bytes) in tiles.iter().enumerate() {
4638+ let tile_num = tile_idx as u32;
4639+ let (trow, tcol) = (tile_num / tile_info.cols, tile_num % tile_info.cols);
4640+ let mi_row0 = tile_info.mi_row_starts[trow as usize];
4641+ let mi_row1 = tile_info.mi_row_starts[trow as usize + 1];
4642+ let mi_col0 = tile_info.mi_col_starts[tcol as usize];
4643+ let mi_col1 = tile_info.mi_col_starts[tcol as usize + 1];
4644+ // Tile boundaries are always superblock-aligned in mi units (spec
4645+ // 5.9.15's uniform/non-uniform derivation both only ever emit
4646+ // multiples of the superblock's own mi size), so plain division is
4647+ // exact here -- no rounding needed.
4648+ // `mi_row0`/`mi_col0` (this tile's own start, `0` or an interior
4649+ // tile boundary) are always superblock-aligned by spec 5.9.15; the
4650+ // frame's own true final edge (`mi_row_starts`/`mi_col_starts`'
4651+ // last entry) is not, so the upper bound needs `div_ceil` the same
4652+ // way the whole-frame `sb_rows`/`sb_cols` above does, to keep the
4653+ // trailing partial superblock row/column in scope.
4654+ let (sb_r0, sb_r1) = ((mi_row0 / SB_MI).min(sb_rows), mi_row1.div_ceil(SB_MI).min(sb_rows));
4655+ let (sb_c0, sb_c1) = ((mi_col0 / SB_MI).min(sb_cols), mi_col1.div_ceil(SB_MI).min(sb_cols));
4656+
4657+ let mut cdfs = base_cdfs.clone();
4658+ if std::env::var_os("EC_AV1_TRACE").is_some() {
4659+ eprintln!(
4660+ "TRACE key_tile_bytes tile={tile_num} len={} first8={:02x?} base_q_idx={base_q_idx} mi_cols={mi_cols} mi_rows={mi_rows}",
4661+ tile_bytes.len(),
4662+ &tile_bytes[..tile_bytes.len().min(8)]
4663+ );
4664+ }
4665+ let mut dec = SymbolDecoder::new(tile_bytes);
4666+ neighbours.start_tile(mi_row0 as usize, mi_col0 as usize, mi_col1 as usize);
4667+ y.set_tile_origin(mi_col0 as usize * 4, mi_row0 as usize * 4);
4668+ u.set_tile_origin(mi_col0 as usize * 2, mi_row0 as usize * 2);
4669+ v.set_tile_origin(mi_col0 as usize * 2, mi_row0 as usize * 2);
4670+ TILE_HITS.with(|c| c.set(c.get() + 1));
4671+
4672+ for sb_r in sb_r0..sb_r1 {
45914673 neighbours.start_row();
4592- for sb_c in 0..sb_cols {
4674+ for sb_c in sb_c0..sb_c1 {
45934675 let at = (sb_r as usize * 4, sb_c as usize * 4);
45944676 let ctx = neighbours.partition_ctx(at, SB);
45954677 let (has_cols, has_rows) = (
@@ -4989,6 +5071,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
49895071 }
49905072 }
49915073
5074+ if tile_num == tile_info.context_update_tile_id {
5075+ result_cdfs = cdfs;
5076+ }
5077+ }
5078+
49925079 // lane-comppin r4: pre-loop-filter decode-order dump, matching aomdec's
49935080 // own EC_AV1_PREFILT_DUMP shape (decodeframe.c ~5451) -- diffs against
49945081 // that isolate whether a decode-order frame's mismatch already exists
@@ -5018,7 +5105,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
50185105 u: u.data,
50195106 v: v.data,
50205107 },
5021- cdfs,
5108+ result_cdfs,
50225109 ));
50235110 }
50245111 let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u8> {
@@ -5036,7 +5123,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
50365123 u: crop(&u, fw / 2, fh / 2),
50375124 v: crop(&v, fw / 2, fh / 2),
50385125 },
5039- cdfs,
5126+ result_cdfs,
50405127 ))
50415128 }
50425129
@@ -9220,6 +9307,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
92209307 height: reference.height,
92219308 true_width: reference.width,
92229309 true_height: reference.height,
9310+ tile_x0: 0,
9311+ tile_y0: 0,
92239312 };
92249313 let ref_u = PlaneBuf {
92259314 data: reference.u.clone(),
@@ -9227,6 +9316,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
92279316 height: reference.height / 2,
92289317 true_width: reference.width / 2,
92299318 true_height: reference.height / 2,
9319+ tile_x0: 0,
9320+ tile_y0: 0,
92309321 };
92319322 let ref_v = PlaneBuf {
92329323 data: reference.v.clone(),
@@ -9234,6 +9325,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
92349325 height: reference.height / 2,
92359326 true_width: reference.width / 2,
92369327 true_height: reference.height / 2,
9328+ tile_x0: 0,
9329+ tile_y0: 0,
92379330 };
92389331 // Every non-`LAST_FRAME` reference this frame header's own `ref_frame_idx`
92399332 // names a live DPB slot for (lane-av1refs: generalised from the old
@@ -9253,6 +9346,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
92539346 height: g.height,
92549347 true_width: g.width,
92559348 true_height: g.height,
9349+ tile_x0: 0,
9350+ tile_y0: 0,
92569351 },
92579352 PlaneBuf {
92589353 data: g.u.clone(),
@@ -9260,6 +9355,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
92609355 height: g.height / 2,
92619356 true_width: g.width / 2,
92629357 true_height: g.height / 2,
9358+ tile_x0: 0,
9359+ tile_y0: 0,
92639360 },
92649361 PlaneBuf {
92659362 data: g.v.clone(),
@@ -9267,6 +9364,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
92679364 height: g.height / 2,
92689365 true_width: g.width / 2,
92699366 true_height: g.height / 2,
9367+ tile_x0: 0,
9368+ tile_y0: 0,
92709369 },
92719370 )
92729371 })
@@ -9280,6 +9379,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
92809379 height,
92819380 true_width,
92829381 true_height,
9382+ tile_x0: 0,
9383+ tile_y0: 0,
92839384 };
92849385 let mut u = PlaneBuf {
92859386 data: vec![0u8; width * height / 4],
@@ -9287,6 +9388,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
92879388 height: height / 2,
92889389 true_width: true_width / 2,
92899390 true_height: true_height / 2,
9391+ tile_x0: 0,
9392+ tile_y0: 0,
92909393 };
92919394 let mut v = PlaneBuf {
92929395 data: vec![0u8; width * height / 4],
@@ -9294,6 +9397,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
92949397 height: height / 2,
92959398 true_width: true_width / 2,
92969399 true_height: true_height / 2,
9400+ tile_x0: 0,
9401+ tile_y0: 0,
92979402 };
92989403
92999404 let scan32 = default_scan(TX32);
@@ -11180,6 +11285,8 @@ mod tests {
1118011285 height: height / scale,
1118111286 true_width: width / scale,
1118211287 true_height: height / scale,
11288+ tile_x0: 0,
11289+ tile_y0: 0,
1118311290 };
1118411291 let ref_y = PlaneBuf {
1118511292 data: ref_pattern.clone(),
@@ -11187,6 +11294,8 @@ mod tests {
1118711294 height,
1118811295 true_width: width,
1118911296 true_height: height,
11297+ tile_x0: 0,
11298+ tile_y0: 0,
1119011299 };
1119111300 let ref_u = ref_plane(2);
1119211301 let ref_v = ref_plane(2);
@@ -11197,6 +11306,8 @@ mod tests {
1119711306 height,
1119811307 true_width: width,
1119911308 true_height: height,
11309+ tile_x0: 0,
11310+ tile_y0: 0,
1120011311 };
1120111312 let mut u = PlaneBuf {
1120211313 data: vec![0u8; (width / 2) * (height / 2)],
@@ -11204,6 +11315,8 @@ mod tests {
1120411315 height: height / 2,
1120511316 true_width: width / 2,
1120611317 true_height: height / 2,
11318+ tile_x0: 0,
11319+ tile_y0: 0,
1120711320 };
1120811321 let mut v = PlaneBuf {
1120911322 data: vec![0u8; (width / 2) * (height / 2)],
@@ -11211,6 +11324,8 @@ mod tests {
1121111324 height: height / 2,
1121211325 true_width: width / 2,
1121311326 true_height: height / 2,
11327+ tile_x0: 0,
11328+ tile_y0: 0,
1121411329 };
1121511330
1121611331 let mut cdfs = Cdfs::new(q_ctx_of(100));
crates/ec-av1/src/stream.rsmodified+2 -1
@@ -360,7 +360,8 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
360360
361361 let (picture, end_cdfs, motion_field) = if header.frame_type == FrameType::Key {
362362 let (picture, end_cdfs) = decode_key_frame_tile_with_cdfs(
363- tile_bytes,
363+ &[tile_bytes],
364+ &header.tile_info,
364365 header.mi_cols,
365366 header.mi_rows,
366367 header.quantization.base_q_idx,