forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-mi' (7b15f443): mode-info grids filled per contiguous row span instead of per 4x4 cell, MiInfo size fields u8 (Option<MiInfo> 48 -> 28 bytes), record_mi_rect neighbour state as three contiguous runs; byte-exact, -1.0% instructions on the 4K segment

Browse files at this commit

46b0c495131bc55d76a3de2b0a209e389a2f7deeAhmet Kaan GümüşParent: 1e1c40e4bb9310
crates/ec-av1/src/decode.rsmodified+115 -168
@@ -3190,8 +3190,8 @@ fn record_intrabc_mi(mi_r: usize, mi_c: usize, n4: usize, dv: Option<(i32, i32)>
31903190 is_new_mv: dv.is_some(),
31913191 is_global_mv0: false,
31923192 is_global_mv1: false,
3193- size: n4,
3194- size_h: n4,
3193+ size: n4 as u8,
3194+ size_h: n4 as u8,
31953195 };
31963196 for r in mi_r..mi_r + n4 {
31973197 for c in mi_c..mi_c + n4 {
@@ -6028,6 +6028,21 @@ struct Neighbour {
60286028 dc: Option<bool>,
60296029 }
60306030
6031+/// `dst[start .. start + n] = v`, with the same per-cell bounds behaviour the
6032+/// per-4x4-cell `get_mut` loops it replaces had: a cell past the end of the
6033+/// grid is dropped, never a panic. One `memset` per grid row instead of a
6034+/// bounds check per cell (lane-mi) -- the index span a row of a mi rect covers
6035+/// is contiguous, so this writes exactly the cells the cell loop did,
6036+/// including the ones a `mi_c + cc` past `skip_grid_cols_mi` folded into the
6037+/// next row.
6038+#[inline]
6039+fn fill_span<T: Copy>(dst: &mut [T], start: usize, n: usize, v: T) {
6040+ let end = (start + n).min(dst.len());
6041+ if start < end {
6042+ dst[start..end].fill(v);
6043+ }
6044+}
6045+
60316046 fn neighbour_state(grid: &[i32]) -> Neighbour {
60326047 let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum();
60336048 Neighbour {
@@ -6651,15 +6666,9 @@ impl Neighbours {
66516666 let (mi_r, mi_c) = at_mi;
66526667 let (uv_w, uv_h) = ((w_mi * MI / 2).max(4) as u8, (h_mi * MI / 2).max(4) as u8);
66536668 for rr in 0..h_mi {
6654- for cc in 0..w_mi {
6655- let idx = (mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc);
6656- if let Some(cell) = self.uv_tx_grid.get_mut(idx) {
6657- *cell = uv_w;
6658- }
6659- if let Some(cell) = self.uv_tx_h_grid.get_mut(idx) {
6660- *cell = uv_h;
6661- }
6662- }
6669+ let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c;
6670+ fill_span(&mut self.uv_tx_grid, start, w_mi, uv_w);
6671+ fill_span(&mut self.uv_tx_h_grid, start, w_mi, uv_h);
66636672 }
66646673 }
66656674
@@ -6688,27 +6697,13 @@ impl Neighbours {
66886697 [cur[0].clamp(-63, 63) as i8; 4]
66896698 };
66906699 for rr in 0..h_mi {
6691- for cc in 0..w_mi {
6692- let idx = (mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc);
6693- if let Some(cell) = self.tx_grid.get_mut(idx) {
6694- *cell = tx_px;
6695- }
6696- if let Some(cell) = self.tx_h_grid.get_mut(idx) {
6697- *cell = tx_h_px;
6698- }
6699- if let Some(cell) = self.uv_tx_grid.get_mut(idx) {
6700- *cell = uv_tx_w;
6701- }
6702- if let Some(cell) = self.uv_tx_h_grid.get_mut(idx) {
6703- *cell = uv_tx_h;
6704- }
6705- if let Some(cell) = self.ref_grid.get_mut(idx) {
6706- *cell = ref_frame;
6707- }
6708- if let Some(cell) = self.delta_lf_grid.get_mut(idx) {
6709- *cell = snapshot;
6710- }
6711- }
6700+ let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c;
6701+ fill_span(&mut self.tx_grid, start, w_mi, tx_px);
6702+ fill_span(&mut self.tx_h_grid, start, w_mi, tx_h_px);
6703+ fill_span(&mut self.uv_tx_grid, start, w_mi, uv_tx_w);
6704+ fill_span(&mut self.uv_tx_h_grid, start, w_mi, uv_tx_h);
6705+ fill_span(&mut self.ref_grid, start, w_mi, ref_frame);
6706+ fill_span(&mut self.delta_lf_grid, start, w_mi, snapshot);
67126707 }
67136708 }
67146709
@@ -6733,15 +6728,9 @@ impl Neighbours {
67336728 eprintln!("EC_LFLEAF mi_row={mi_r} mi_col={mi_c} w_mi={w_mi} h_mi={h_mi} tx_px={tx_px} tx_h_px={tx_h_px}");
67346729 }
67356730 for rr in 0..h_mi {
6736- for cc in 0..w_mi {
6737- let idx = (mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc);
6738- if let Some(cell) = self.tx_grid.get_mut(idx) {
6739- *cell = tx_px;
6740- }
6741- if let Some(cell) = self.tx_h_grid.get_mut(idx) {
6742- *cell = tx_h_px;
6743- }
6744- }
6731+ let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c;
6732+ fill_span(&mut self.tx_grid, start, w_mi, tx_px);
6733+ fill_span(&mut self.tx_h_grid, start, w_mi, tx_h_px);
67456734 }
67466735 }
67476736
@@ -6760,21 +6749,11 @@ impl Neighbours {
67606749 RECT_SKIP_BAND_HITS.with(|c| c.set(c.get() + 1));
67616750 }
67626751 for rr in 0..h_mi {
6763- for cc in 0..w_mi {
6764- let idx = (mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc);
6765- if let Some(cell) = self.skip_grid.get_mut(idx) {
6766- *cell = skip;
6767- }
6768- if let Some(cell) = self.skip_written.get_mut(idx) {
6769- *cell = true;
6770- }
6771- if let Some(org) = self.blk_org_grid.get_mut(idx) {
6772- *org = [mi_r as u16, mi_c as u16];
6773- }
6774- if let Some(dim) = self.blk_dim_grid.get_mut(idx) {
6775- *dim = [w_mi as u8, h_mi as u8];
6776- }
6777- }
6752+ let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c;
6753+ fill_span(&mut self.skip_grid, start, w_mi, skip);
6754+ fill_span(&mut self.skip_written, start, w_mi, true);
6755+ fill_span(&mut self.blk_org_grid, start, w_mi, [mi_r as u16, mi_c as u16]);
6756+ fill_span(&mut self.blk_dim_grid, start, w_mi, [w_mi as u8, h_mi as u8]);
67786757 }
67796758 }
67806759
@@ -6936,34 +6915,30 @@ impl Neighbours {
69366915 self.record_uv_mode_mi(r, c, w_mi, h_mi, DC_PRED);
69376916 }
69386917 let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME;
6939- for cell in 0..w_mi {
6940- self.above_skip[c + cell] = skip;
6941- self.above_skip_mode[c + cell] = skip_mode;
6942- self.above_inter[c + cell] = is_inter;
6943- self.above_ref[c + cell] = ref_frame;
6944- // lane-av1comp: no caller of `record_inter` ever decodes a
6945- // compound block yet (see [`Self::above_ref1`]'s doc); the
6946- // parameter this becomes lands with `read_ref_frames`.
6947- self.above_ref1[c + cell] = None;
6948- // libaom `get_comp_group_idx_context`/`get_comp_index_context`'s
6949- // single-ref special case: `ref_frame[0] == ALTREF_FRAME` reads
6950- // as `3`/`1` even though this block has no second reference.
6951- // `record_compound_ctx` overwrites this with the real bit for an
6952- // actual compound block's cells, called right after this one.
6953- self.above_comp_group_idx[c + cell] = if altref { 3 } else { 0 };
6954- self.above_compound_idx[c + cell] = u8::from(altref);
6955- self.above_filter[c + cell] = filter;
6956- }
6957- for cell in 0..h_mi {
6958- self.left_skip[r + cell] = skip;
6959- self.left_skip_mode[r + cell] = skip_mode;
6960- self.left_inter[r + cell] = is_inter;
6961- self.left_ref[r + cell] = ref_frame;
6962- self.left_ref1[r + cell] = None;
6963- self.left_comp_group_idx[r + cell] = if altref { 3 } else { 0 };
6964- self.left_compound_idx[r + cell] = u8::from(altref);
6965- self.left_filter[r + cell] = filter;
6966- }
6918+ self.above_skip[c..c + w_mi].fill(skip);
6919+ self.above_skip_mode[c..c + w_mi].fill(skip_mode);
6920+ self.above_inter[c..c + w_mi].fill(is_inter);
6921+ self.above_ref[c..c + w_mi].fill(ref_frame);
6922+ // lane-av1comp: no caller of `record_inter` ever decodes a
6923+ // compound block yet (see [`Self::above_ref1`]'s doc); the
6924+ // parameter this becomes lands with `read_ref_frames`.
6925+ self.above_ref1[c..c + w_mi].fill(None);
6926+ // libaom `get_comp_group_idx_context`/`get_comp_index_context`'s
6927+ // single-ref special case: `ref_frame[0] == ALTREF_FRAME` reads
6928+ // as `3`/`1` even though this block has no second reference.
6929+ // `record_compound_ctx` overwrites this with the real bit for an
6930+ // actual compound block's cells, called right after this one.
6931+ self.above_comp_group_idx[c..c + w_mi].fill(if altref { 3 } else { 0 });
6932+ self.above_compound_idx[c..c + w_mi].fill(u8::from(altref));
6933+ self.above_filter[c..c + w_mi].fill(filter);
6934+ self.left_skip[r..r + h_mi].fill(skip);
6935+ self.left_skip_mode[r..r + h_mi].fill(skip_mode);
6936+ self.left_inter[r..r + h_mi].fill(is_inter);
6937+ self.left_ref[r..r + h_mi].fill(ref_frame);
6938+ self.left_ref1[r..r + h_mi].fill(None);
6939+ self.left_comp_group_idx[r..r + h_mi].fill(if altref { 3 } else { 0 });
6940+ self.left_compound_idx[r..r + h_mi].fill(u8::from(altref));
6941+ self.left_filter[r..r + h_mi].fill(filter);
69676942 }
69686943
69696944 /// Overwrites [`Self::record_inter`]'s ALTREF-special-case default with
@@ -7249,27 +7224,14 @@ impl Neighbours {
72497224 uv_mode: usize,
72507225 ) {
72517226 let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1);
7252- for cell in 0..mi_w {
7253- if let Some(slot) = self.uv_mode_col.get_mut(mi_c + cell) {
7254- *slot = (last_r, uv_mode);
7255- }
7256- }
7257- for cell in 0..mi_h {
7258- if let Some(slot) = self.uv_mode_row.get_mut(mi_r + cell) {
7259- *slot = (last_c, uv_mode);
7260- }
7261- }
7227+ fill_span(&mut self.uv_mode_col, mi_c, mi_w, (last_r, uv_mode));
7228+ fill_span(&mut self.uv_mode_row, mi_r, mi_h, (last_c, uv_mode));
72627229 // libaom writes one `MB_MODE_INFO` pointer into every mi cell a block
72637230 // covers, so the chroma neighbour read below is a plain grid read.
72647231 let stride = self.uv_grid_cols;
7232+ let n = stride.saturating_sub(mi_c).min(last_c + 1 - mi_c);
72657233 for row in mi_r..=last_r {
7266- for col in mi_c..=last_c {
7267- if col < stride
7268- && let Some(cell) = self.uv_mode_grid.get_mut(row * stride + col)
7269- {
7270- *cell = uv_mode as u8;
7271- }
7272- }
7234+ fill_span(&mut self.uv_mode_grid, row * stride + mi_c, n, uv_mode as u8);
72737235 }
72747236 }
72757237
@@ -7506,16 +7468,8 @@ impl Neighbours {
75067468 if crate::envflags::env_flag!("EC_ECPUB") {
75077469 eprintln!("EC_ECPUB blk mi=({mi_r},{mi_c}) wh=({w},{h}) lvl={}", states[0].level);
75087470 }
7509- for cell in 0..h_mi {
7510- if let Some(slot) = self.left_side_mi.get_mut(mi_r + cell) {
7511- *slot = h;
7512- }
7513- }
7514- for cell in 0..w_mi {
7515- if let Some(slot) = self.above_side_mi.get_mut(mi_c + cell) {
7516- *slot = w;
7517- }
7518- }
7471+ fill_span(&mut self.left_side_mi, mi_r, h_mi, h);
7472+ fill_span(&mut self.above_side_mi, mi_c, w_mi, w);
75197473 // A chroma 4x4 unit straddling the true luma edge is still whole in
75207474 // chroma's own halved grid, so libaom rounds the luma bound up to the
75217475 // plane's own 4x4 unit before clamping a subsampled plane
@@ -7531,34 +7485,35 @@ impl Neighbours {
75317485 round_up_even(self.mi_cols),
75327486 round_up_even(self.mi_cols),
75337487 ];
7534- for cell in 0..h_mi {
7535- // lane-sb128c r7: a 128x64 at the bottom frame edge names mi rows
7536- // past the padded side arrays (mi_rows 198, block rows 192..208);
7537- // libaom's own arrays are superblock-sized, so those cells simply
7538- // have no reader. Same guard the `left_side_mi` loop above uses.
7539- let Some(left_slot) = self.left.get_mut(mi_r + cell) else {
7540- break;
7541- };
7542- *left_slot = std::array::from_fn(|plane| {
7543- if cell < h_mi.min(bound_h[plane].saturating_sub(mi_r)) {
7544- states[plane]
7545- } else {
7546- Default::default()
7547- }
7548- });
7549- }
7550- for cell in 0..w_mi {
7551- let Some(above_slot) = self.above.get_mut(mi_c + cell) else {
7552- break;
7553- };
7554- *above_slot = std::array::from_fn(|plane| {
7555- if cell < w_mi.min(bound_w[plane].saturating_sub(mi_c)) {
7556- states[plane]
7557- } else {
7558- Default::default()
7559- }
7560- });
7561- }
7488+ // lane-sb128c r7: a 128x64 at the bottom frame edge names mi rows
7489+ // past the padded side arrays (mi_rows 198, block rows 192..208);
7490+ // libaom's own arrays are superblock-sized, so those cells simply
7491+ // have no reader -- [`fill_span`] drops them, as the `break` did.
7492+ // `bound_h[1] == bound_h[2] >= bound_h[0]`, so the per-cell plane
7493+ // predicate is three contiguous runs: all planes, chroma only, none
7494+ // (lane-mi).
7495+ let luma_h = h_mi.min(bound_h[0].saturating_sub(mi_r));
7496+ let chroma_h = h_mi.min(bound_h[1].saturating_sub(mi_r));
7497+ let uv_only = [Neighbour::default(), states[1], states[2]];
7498+ fill_span(&mut self.left, mi_r, luma_h, states);
7499+ fill_span(&mut self.left, mi_r + luma_h, chroma_h - luma_h, uv_only);
7500+ fill_span(
7501+ &mut self.left,
7502+ mi_r + chroma_h,
7503+ h_mi - chroma_h,
7504+ [Neighbour::default(); 3],
7505+ );
7506+ let luma_w = w_mi.min(bound_w[0].saturating_sub(mi_c));
7507+ let chroma_w = w_mi.min(bound_w[1].saturating_sub(mi_c));
7508+ let uv_only_w = [Neighbour::default(), states[1], states[2]];
7509+ fill_span(&mut self.above, mi_c, luma_w, states);
7510+ fill_span(&mut self.above, mi_c + luma_w, chroma_w - luma_w, uv_only_w);
7511+ fill_span(
7512+ &mut self.above,
7513+ mi_c + chroma_w,
7514+ w_mi - chroma_w,
7515+ [Neighbour::default(); 3],
7516+ );
75627517 }
75637518
75647519 /// [`Self::record_mi`]'s plane-0 half only, for one luma transform unit
@@ -7703,16 +7658,8 @@ impl Neighbours {
77037658 let (w_mi, h_mi) = (w / MI, h / MI);
77047659 self.record_mode_mi(mi_r, mi_c, w_mi, h_mi, mode);
77057660 self.record_uv_mode_mi(mi_r, mi_c, w_mi, h_mi, uv_mode);
7706- for cell in 0..h_mi {
7707- if let Some(slot) = self.left_side_mi.get_mut(mi_r + cell) {
7708- *slot = h;
7709- }
7710- }
7711- for cell in 0..w_mi {
7712- if let Some(slot) = self.above_side_mi.get_mut(mi_c + cell) {
7713- *slot = w;
7714- }
7715- }
7661+ fill_span(&mut self.left_side_mi, mi_r, h_mi, h);
7662+ fill_span(&mut self.above_side_mi, mi_c, w_mi, w);
77167663 let round_up_even = |n: usize| n.div_ceil(2) * 2;
77177664 let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols));
77187665 for (plane_idx, grid) in chroma_grids.into_iter().enumerate() {
@@ -22077,7 +22024,7 @@ fn overlappable_above(
2207722024 let mut col = mi_col;
2207822025 while col < end_col && out.len() < max_neighbors {
2207922026 let cell = grid.get(mi_row - 1, col);
22080- let mut step = cell.map_or(1, |c| c.size).max(1).min(SB_MI as usize);
22027+ let mut step = cell.map_or(1, |c| c.size as usize).max(1).min(SB_MI as usize);
2208122028 let mut nb = cell;
2208222029 // lane-obmcrec r1: the mi the neighbour's `MiInfo` is actually taken
2208322030 // from -- the pair merge below moves it, and libaom reads that same
@@ -22132,7 +22079,7 @@ fn overlappable_left(
2213222079 let cell = grid.get(row, mi_col - 1);
2213322080 // Vertical walk steps by the neighbour's HEIGHT -- a 32x16 strip's
2213422081 // width (8) would swallow the strip below it.
22135- let mut step = cell.map_or(1, |c| c.size_h).max(1).min(SB_MI as usize);
22082+ let mut step = cell.map_or(1, |c| c.size_h as usize).max(1).min(SB_MI as usize);
2213622083 let mut nb = cell;
2213722084 // lane-obmcrec r1: see `overlappable_above`'s `src`.
2213822085 let mut src = row;
@@ -22229,7 +22176,7 @@ fn find_samples(
2222922176 'outer: {
2223022177 if up_available {
2223122178 let above = grid.get(mi_row - 1, mi_col);
22232- let superblock_width = above.map_or(1, |c| c.size).max(1);
22179+ let superblock_width = above.map_or(1, |c| c.size as usize).max(1);
2223322180 if bw4 <= superblock_width {
2223422181 let col_offset = -((mi_col % superblock_width) as isize);
2223522182 if col_offset < 0 {
@@ -22251,7 +22198,7 @@ fn find_samples(
2225122198 let limit = bw4.min(mi_cols.saturating_sub(mi_col));
2225222199 while i < limit {
2225322200 let cell = grid.get(mi_row - 1, mi_col + i);
22254- let sw = cell.map_or(1, |c| c.size).max(1);
22201+ let sw = cell.map_or(1, |c| c.size as usize).max(1);
2225522202 if let Some(info) = cell {
2225622203 if single_ref_match(info) {
2225722204 samples.push(rec(info, 0, -1, i as i32, 1));
@@ -22267,7 +22214,7 @@ fn find_samples(
2226722214
2226822215 if left_available {
2226922216 let left = grid.get(mi_row, mi_col - 1);
22270- let superblock_height = left.map_or(1, |c| c.size_h).max(1);
22217+ let superblock_height = left.map_or(1, |c| c.size_h as usize).max(1);
2227122218 if bh4 <= superblock_height {
2227222219 let row_offset = -((mi_row % superblock_height) as isize);
2227322220 if row_offset < 0 {
@@ -22286,7 +22233,7 @@ fn find_samples(
2228622233 let limit = bh4.min(mi_rows.saturating_sub(mi_row));
2228722234 while i < limit {
2228822235 let cell = grid.get(mi_row + i, mi_col - 1);
22289- let sh = cell.map_or(1, |c| c.size_h).max(1);
22236+ let sh = cell.map_or(1, |c| c.size_h as usize).max(1);
2229022237 if let Some(info) = cell {
2229122238 if single_ref_match(info) {
2229222239 samples.push(rec(info, i as i32, 1, 0, -1));
@@ -22717,7 +22664,7 @@ nbmv=({},{}) nbref={} nbbsize={} filt={}",
2271722664 nb.mv.0,
2271822665 nb.mv.1,
2271922666 nb.ref_frame,
22720- ec_obmc_bsize(nb.size, nb.size_h),
22667+ ec_obmc_bsize(nb.size as usize, nb.size_h as usize),
2272122668 sym(v_kind) | (sym(h_kind) << 16),
2272222669 );
2272322670 }
@@ -23851,8 +23798,8 @@ fn decode_inter_block(
2385123798 mv1: Some(mv1),
2385223799 mv: mv0,
2385323800 is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7),
23854- size: bw4,
23855- size_h: bh4,
23801+ size: bw4 as u8,
23802+ size_h: bh4 as u8,
2385623803 // libaom `is_global_mv_block`: per-ref-slot, mode
2385723804 // GLOBAL_GLOBALMV, block >= 8x8, and THAT slot's
2385823805 // own gm model > TRANSLATION (IDENTITY excluded
@@ -25413,8 +25360,8 @@ fn decode_inter_block(
2541325360 mv1: None,
2541425361 mv,
2541525362 is_new_mv,
25416- size: bw4,
25417- size_h: bh4,
25363+ size: bw4 as u8,
25364+ size_h: bh4 as u8,
2541825365 is_global_mv0: is_global_mv_block,
2541925366 is_global_mv1: false,
2542025367 },
@@ -26239,8 +26186,8 @@ fn decode_inter_block(
2623926186 mv1: None,
2624026187 mv: (0, 0),
2624126188 is_new_mv: false,
26242- size: write_w / MI,
26243- size_h: write_h / MI,
26189+ size: (write_w / MI) as u8,
26190+ size_h: (write_h / MI) as u8,
2624426191 is_global_mv0: false,
2624526192 is_global_mv1: false,
2624626193 },
@@ -26463,8 +26410,8 @@ fn decode_inter_block(
2646326410 mv1: None,
2646426411 mv: (0, 0),
2646526412 is_new_mv: false,
26466- size: side / 4,
26467- size_h: side / 4,
26413+ size: (side / 4) as u8,
26414+ size_h: (side / 4) as u8,
2646826415 is_global_mv0: false,
2646926416 is_global_mv1: false,
2647026417 },
@@ -27914,8 +27861,8 @@ fn decode_intra_sub8_leaf(
2791427861 mv1: None,
2791527862 mv: (0, 0),
2791627863 is_new_mv: false,
27917- size: w_mi,
27918- size_h: h_mi,
27864+ size: w_mi as u8,
27865+ size_h: h_mi as u8,
2791927866 is_global_mv0: false,
2792027867 is_global_mv1: false,
2792127868 },
@@ -28619,8 +28566,8 @@ fn grid_stamp_rect(
2861928566 mv1: None,
2862028567 mv,
2862128568 is_new_mv,
28622- size: w_mi,
28623- size_h: h_mi,
28569+ size: w_mi as u8,
28570+ size_h: h_mi as u8,
2862428571 is_global_mv0: false,
2862528572 is_global_mv1: false,
2862628573 },
crates/ec-av1/src/mvstack.rsmodified+27 -14
@@ -196,11 +196,11 @@ pub struct MiInfo {
196196 /// inter -- libaom's `scan_row_mbmi` advances `processed_rows` from any
197197 /// candidate's `bsize` regardless of whether it also casts a vote (that
198198 /// requires a ref-frame match, checked separately in `add_ref_mv_candidate`).
199- pub size: usize,
199+ pub size: u8,
200200 /// The unit's height in 4x4 mi cells (== `size` for every square block;
201201 /// differs only for rect partition strips, lane-rect r2). Row scans step
202202 /// and weight by `size` (width), column scans by `size_h`.
203- pub size_h: usize,
203+ pub size_h: u8,
204204 }
205205
206206 /// The whole frame's `mi` grid, one [`MiInfo`] per 4x4 unit, in raster order.
@@ -549,7 +549,7 @@ fn scan_row(
549549 i += 1;
550550 continue;
551551 };
552- let n4 = info.size;
552+ let n4 = info.size as usize;
553553 let mut len = bw4.min(n4);
554554 if use_step_16 {
555555 len = len.max(4);
@@ -561,7 +561,7 @@ fn scan_row(
561561 // `xd->width >= mi_size_wide[BLOCK_8X8]` as well: a 4-wide block (a 1:4
562562 // strip, a sub-8x8 leaf) keeps weight 2 and leaves `processed_rows` alone.
563563 if bw4 >= 2 && bw4 <= n4 {
564- let inc = ((-max_row_offset + row_offset + 1) as usize).min(info.size_h);
564+ let inc = ((-max_row_offset + row_offset + 1) as usize).min(info.size_h as usize);
565565 weight = weight.max(inc as u32);
566566 *processed_rows = (inc as isize - row_offset - 1).max(0) as usize;
567567 }
@@ -637,7 +637,7 @@ fn scan_col(
637637 i += 1;
638638 continue;
639639 };
640- let n4 = info.size_h;
640+ let n4 = info.size_h as usize;
641641 let mut len = bh4.min(n4);
642642 if use_step_16 {
643643 len = len.max(4);
@@ -649,7 +649,7 @@ fn scan_col(
649649 // mi_size_high[BLOCK_8X8]` gates this branch, so a 4-high block keeps
650650 // weight 2 and leaves `processed_cols` alone.
651651 if bh4 >= 2 && bh4 <= n4 {
652- let inc = ((-max_col_offset + col_offset + 1) as usize).min(info.size);
652+ let inc = ((-max_col_offset + col_offset + 1) as usize).min(info.size as usize);
653653 weight = weight.max(inc as u32);
654654 *processed_cols = (inc as isize - col_offset - 1).max(0) as usize;
655655 }
@@ -1217,7 +1217,7 @@ pub fn find_mv_stack_with_sign_bias(
12171217 let mut idx = 0usize;
12181218 while idx < mi_size && candidates.len() < MAX_MV_REF_CANDIDATES {
12191219 let cand = grid.get(mi_row - 1, mi_col + idx);
1220- let step = cand.map_or(1, |c| c.size).max(1);
1220+ let step = cand.map_or(1, |c| c.size as usize).max(1);
12211221 if let Some(c) = cand {
12221222 process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut candidates);
12231223 }
@@ -1228,7 +1228,7 @@ pub fn find_mv_stack_with_sign_bias(
12281228 let mut idx = 0usize;
12291229 while idx < mi_size && candidates.len() < MAX_MV_REF_CANDIDATES {
12301230 let cand = grid.get(mi_row + idx, mi_col - 1);
1231- let step = cand.map_or(1, |c| c.size_h).max(1);
1231+ let step = cand.map_or(1, |c| c.size_h as usize).max(1);
12321232 if let Some(c) = cand {
12331233 process_single_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut candidates);
12341234 }
@@ -1441,7 +1441,7 @@ fn scan_row_compound(
14411441 i += 1;
14421442 continue;
14431443 };
1444- let n4 = info.size;
1444+ let n4 = info.size as usize;
14451445 let mut len = bw4.min(n4);
14461446 if use_step_16 {
14471447 len = len.max(4);
@@ -1453,7 +1453,7 @@ fn scan_row_compound(
14531453 // `xd->width >= mi_size_wide[BLOCK_8X8]` as well: a 4-wide block (a 1:4
14541454 // strip, a sub-8x8 leaf) keeps weight 2 and leaves `processed_rows` alone.
14551455 if bw4 >= 2 && bw4 <= n4 {
1456- let inc = ((-max_row_offset + row_offset + 1) as usize).min(info.size_h);
1456+ let inc = ((-max_row_offset + row_offset + 1) as usize).min(info.size_h as usize);
14571457 weight = weight.max(inc as u32);
14581458 *processed_rows = (inc as isize - row_offset - 1).max(0) as usize;
14591459 }
@@ -1504,7 +1504,7 @@ fn scan_col_compound(
15041504 i += 1;
15051505 continue;
15061506 };
1507- let n4 = info.size_h;
1507+ let n4 = info.size_h as usize;
15081508 let mut len = bh4.min(n4);
15091509 if use_step_16 {
15101510 len = len.max(4);
@@ -1516,7 +1516,7 @@ fn scan_col_compound(
15161516 // mi_size_high[BLOCK_8X8]` gates this branch, so a 4-high block keeps
15171517 // weight 2 and leaves `processed_cols` alone.
15181518 if bh4 >= 2 && bh4 <= n4 {
1519- let inc = ((-max_col_offset + col_offset + 1) as usize).min(info.size);
1519+ let inc = ((-max_col_offset + col_offset + 1) as usize).min(info.size as usize);
15201520 weight = weight.max(inc as u32);
15211521 *processed_cols = (inc as isize - col_offset - 1).max(0) as usize;
15221522 }
@@ -1995,7 +1995,7 @@ pub fn find_mv_stack_compound(
19951995 let mut idx = 0usize;
19961996 while idx < mi_size {
19971997 let cand = grid.get(mi_row - 1, mi_col + idx);
1998- let step = cand.map_or(1, |c| c.size).max(1);
1998+ let step = cand.map_or(1, |c| c.size as usize).max(1);
19991999 if let Some(c) = cand {
20002000 process_compound_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut lists);
20012001 }
@@ -2006,7 +2006,7 @@ pub fn find_mv_stack_compound(
20062006 let mut idx = 0usize;
20072007 while idx < mi_size {
20082008 let cand = grid.get(mi_row + idx, mi_col - 1);
2009- let step = cand.map_or(1, |c| c.size_h).max(1);
2009+ let step = cand.map_or(1, |c| c.size_h as usize).max(1);
20102010 if let Some(c) = cand {
20112011 process_compound_ref_mv_candidate(c, ref_frame, sign_bias_table, &mut lists);
20122012 }
@@ -2390,6 +2390,19 @@ pub(crate) fn comp_reference_type_ctx(
23902390
23912391 #[cfg(test)]
23922392 mod tests {
2393+ /// lane-mi: a 4K frame's grid is ~518k cells, refilled per frame, and
2394+ /// every mv-stack scan walks it -- `size`/`size_h` as `u8` keep the
2395+ /// option under 32 bytes. Widening a field here is a measurable
2396+ /// regression, not a free change.
2397+ #[test]
2398+ fn mi_info_stays_small_enough_for_a_4k_grid() {
2399+ assert!(
2400+ std::mem::size_of::<Option<MiInfo>>() <= 32,
2401+ "Option<MiInfo> grew to {} bytes",
2402+ std::mem::size_of::<Option<MiInfo>>()
2403+ );
2404+ }
2405+
23932406 use super::*;
23942407
23952408 fn inter(mv: (i32, i32)) -> MiInfo {
crates/ec-av1/src/tile.rsmodified+4 -4
@@ -2634,8 +2634,8 @@ fn write_inter_frame_leaf(
26342634 mv1: None,
26352635 mv,
26362636 is_new_mv,
2637- size: SUB_MI as usize,
2638- size_h: SUB_MI as usize,
2637+ size: SUB_MI as usize as u8,
2638+ size_h: SUB_MI as usize as u8,
26392639 is_global_mv0: false,
26402640 is_global_mv1: false,
26412641 },
@@ -2666,8 +2666,8 @@ fn write_inter_frame_leaf(
26662666 mv1: None,
26672667 mv: (0, 0),
26682668 is_new_mv: false,
2669- size: SUB_MI as usize,
2670- size_h: SUB_MI as usize,
2669+ size: SUB_MI as usize as u8,
2670+ size_h: SUB_MI as usize as u8,
26712671 is_global_mv0: false,
26722672 is_global_mv1: false,
26732673 },