forge
Explore
Log in

Don't you dare go Hollow

Heap-free MV stack and row-fill mi publication

Browse files at this commit

fc982dba9c966e5570f227a90fd6d8d1879d35f9Ahmet Kaan GümüşParent: 5b55d04
  • perf
    • heap-free MV stack and row-fill mi publication
Show full message
perf(av1): heap-free MV stack and row-fill mi publication

The MV stack was two heap allocations per inter block (`entries` plus
`drl_ctx`); both lists are bounded by a spec constant, so they move into a
fixed-capacity inline `SmallVec` that derefs to the same slice API. Mode-info
publication stamped its block footprint cell by cell through `MiGrid::set`;
rows are contiguous, so `MiGrid::fill_rect` fills each row with one
`slice::fill` of the `Copy` cell instead.

Byte-exact: all four gate streams hash unchanged at 1 thread.
instructions:u on the 4K segment 412.376 -> 410.629 G at 1 thread,
421.419 -> 419.746 G at (16,4,2).
crates/ec-av1/src/decode.rsmodified+175 -193
@@ -24231,31 +24231,29 @@ fn decode_inter_block(
2423124231 // too -- a square `bw4`x`bw4` stamp here claims a rect strip's
2423224232 // NEXT strip's own rows/cols before it decodes, corrupting
2423324233 // every later mvstack scan that reads this cell back.
24234- for dr in 0..bh4 {
24235- for dc in 0..bw4 {
24236- grid.set(
24237- mi_row + dr,
24238- mi_col + dc,
24239- MiInfo {
24240- is_inter: true,
24241- ref_frame: ref0,
24242- ref_frame1: Some(ref1),
24243- mv1: Some(mv1),
24244- mv: mv0,
24245- is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7),
24246- size: bw4 as u8,
24247- size_h: bh4 as u8,
24248- // libaom `is_global_mv_block`: per-ref-slot, mode
24249- // GLOBAL_GLOBALMV, block >= 8x8, and THAT slot's
24250- // own gm model > TRANSLATION (IDENTITY excluded
24251- // too -- the two-predicate trap, distinct from
24252- // `gm_nontrans` above which allows IDENTITY).
24253- is_global_mv0,
24254- is_global_mv1,
24255- },
24256- );
24257- }
24258- }
24234+ grid.fill_rect(
24235+ mi_row,
24236+ mi_col,
24237+ bh4,
24238+ bw4,
24239+ MiInfo {
24240+ is_inter: true,
24241+ ref_frame: ref0,
24242+ ref_frame1: Some(ref1),
24243+ mv1: Some(mv1),
24244+ mv: mv0,
24245+ is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7),
24246+ size: bw4 as u8,
24247+ size_h: bh4 as u8,
24248+ // libaom `is_global_mv_block`: per-ref-slot, mode
24249+ // GLOBAL_GLOBALMV, block >= 8x8, and THAT slot's
24250+ // own gm model > TRANSLATION (IDENTITY excluded
24251+ // too -- the two-predicate trap, distinct from
24252+ // `gm_nontrans` above which allows IDENTITY).
24253+ is_global_mv0,
24254+ is_global_mv1,
24255+ },
24256+ );
2425924257 // spec 5.11.25's `comp_group_idx`/`compound_idx`: only read when
2426024258 // masked compound / distance-weighted compound are actually
2426124259 // enabled for this stream -- `is_any_masked_compound_used_here`
@@ -25787,32 +25785,30 @@ fn decode_inter_block(
2578725785 // lane-rect r2: see the compound path's matching comment above --
2578825786 // `grid` must be stamped with the block's true bh4/bw4 span, not
2578925787 // a square `bw4`x`bw4` guess.
25790- for dr in 0..bh4 {
25791- for dc in 0..bw4 {
25792- grid.set(
25793- mi_row + dr,
25794- mi_col + dc,
25795- MiInfo {
25796- is_inter: true,
25797- ref_frame,
25798- // An interintra block records ref_frame[1] ==
25799- // INTRA_FRAME (0): warp-sample gathering
25800- // (libaom av1_findSamples, mvref_common.c:1155)
25801- // requires ref_frame[1] == NONE_FRAME, so such a
25802- // neighbour must not donate samples or count in
25803- // num_proj_ref.
25804- ref_frame1: interintra_mode.map(|_| 0),
25805- mv1: None,
25806- mv,
25807- is_new_mv,
25808- size: bw4 as u8,
25809- size_h: bh4 as u8,
25810- is_global_mv0: is_global_mv_block,
25811- is_global_mv1: false,
25812- },
25813- );
25814- }
25815- }
25788+ grid.fill_rect(
25789+ mi_row,
25790+ mi_col,
25791+ bh4,
25792+ bw4,
25793+ MiInfo {
25794+ is_inter: true,
25795+ ref_frame,
25796+ // An interintra block records ref_frame[1] ==
25797+ // INTRA_FRAME (0): warp-sample gathering
25798+ // (libaom av1_findSamples, mvref_common.c:1155)
25799+ // requires ref_frame[1] == NONE_FRAME, so such a
25800+ // neighbour must not donate samples or count in
25801+ // num_proj_ref.
25802+ ref_frame1: interintra_mode.map(|_| 0),
25803+ mv1: None,
25804+ mv,
25805+ is_new_mv,
25806+ size: bw4 as u8,
25807+ size_h: bh4 as u8,
25808+ is_global_mv0: is_global_mv_block,
25809+ is_global_mv1: false,
25810+ },
25811+ );
2581625812 mode_for_tx = 0;
2581725813 uv_predict_mode = DC_PRED;
2581825814
@@ -26619,26 +26615,24 @@ fn decode_inter_block(
2661926615 // lane's `r * SUB_MI` re-derivation would snap a sub-SUB strip to
2662026616 // its enclosing 16-px cell.
2662126617 let (mi_r0, mi_c0) = (rmi, cmi);
26622- for dr in 0..write_h / MI {
26623- for dc in 0..write_w / MI {
26624- grid.set(
26625- mi_r0 + dr,
26626- mi_c0 + dc,
26627- MiInfo {
26628- is_inter: false,
26629- ref_frame: -1,
26630- ref_frame1: None,
26631- mv1: None,
26632- mv: (0, 0),
26633- is_new_mv: false,
26634- size: (write_w / MI) as u8,
26635- size_h: (write_h / MI) as u8,
26636- is_global_mv0: false,
26637- is_global_mv1: false,
26638- },
26639- );
26640- }
26641- }
26618+ grid.fill_rect(
26619+ mi_r0,
26620+ mi_c0,
26621+ write_h / MI,
26622+ write_w / MI,
26623+ MiInfo {
26624+ is_inter: false,
26625+ ref_frame: -1,
26626+ ref_frame1: None,
26627+ mv1: None,
26628+ mv: (0, 0),
26629+ is_new_mv: false,
26630+ size: (write_w / MI) as u8,
26631+ size_h: (write_h / MI) as u8,
26632+ is_global_mv0: false,
26633+ is_global_mv1: false,
26634+ },
26635+ );
2664226636 neighbours.record_inter_rect_mi(
2664326637 at,
2664426638 write_w / MI,
@@ -26843,26 +26837,24 @@ fn decode_inter_block(
2684326837 ref_frame_for_lf = 0;
2684426838 globalmv_for_lf = false;
2684526839 let (mi_row, mi_col) = (rmi, cmi);
26846- for dr in 0..side / 4 {
26847- for dc in 0..side / 4 {
26848- grid.set(
26849- mi_row + dr,
26850- mi_col + dc,
26851- MiInfo {
26852- is_inter: false,
26853- ref_frame: -1,
26854- ref_frame1: None,
26855- mv1: None,
26856- mv: (0, 0),
26857- is_new_mv: false,
26858- size: (side / 4) as u8,
26859- size_h: (side / 4) as u8,
26860- is_global_mv0: false,
26861- is_global_mv1: false,
26862- },
26863- );
26864- }
26865- }
26840+ grid.fill_rect(
26841+ mi_row,
26842+ mi_col,
26843+ side / 4,
26844+ side / 4,
26845+ MiInfo {
26846+ is_inter: false,
26847+ ref_frame: -1,
26848+ ref_frame1: None,
26849+ mv1: None,
26850+ mv: (0, 0),
26851+ is_new_mv: false,
26852+ size: (side / 4) as u8,
26853+ size_h: (side / 4) as u8,
26854+ is_global_mv0: false,
26855+ is_global_mv1: false,
26856+ },
26857+ );
2686626858
2686726859 // lane-intrainter r1: an intra block in an inter frame reads the same
2686826860 // `tx_depth` symbol a key-frame block does, and when it splits, the
@@ -28294,26 +28286,24 @@ fn decode_intra_sub8_leaf(
2829428286 // intra-in-inter block writes them: `is_inter = 0`, `INTRA_FRAME` ref, no
2829528287 // interpolation filter, no skip mode.
2829628288 neighbours.record_inter_rect_mi(lmi, w_mi, h_mi, skip, false, 0, [3, 3], false);
28297- for dr in 0..h_mi {
28298- for dc in 0..w_mi {
28299- grid.set(
28300- lmi.0 + dr,
28301- lmi.1 + dc,
28302- MiInfo {
28303- is_inter: false,
28304- ref_frame: -1,
28305- ref_frame1: None,
28306- mv1: None,
28307- mv: (0, 0),
28308- is_new_mv: false,
28309- size: w_mi as u8,
28310- size_h: h_mi as u8,
28311- is_global_mv0: false,
28312- is_global_mv1: false,
28313- },
28314- );
28315- }
28316- }
28289+ grid.fill_rect(
28290+ lmi.0,
28291+ lmi.1,
28292+ h_mi,
28293+ w_mi,
28294+ MiInfo {
28295+ is_inter: false,
28296+ ref_frame: -1,
28297+ ref_frame1: None,
28298+ mv1: None,
28299+ mv: (0, 0),
28300+ is_new_mv: false,
28301+ size: w_mi as u8,
28302+ size_h: h_mi as u8,
28303+ is_global_mv0: false,
28304+ is_global_mv1: false,
28305+ },
28306+ );
2831728307 SUB8_INTRA_RECT_HITS.with(|h| {
2831828308 let mut s = h.get();
2831928309 s[usize::from(vert)] += 1;
@@ -28999,26 +28989,24 @@ fn grid_stamp_rect(
2899928989 mv: (i32, i32),
2900028990 is_new_mv: bool,
2900128991 ) {
29002- for dr in 0..h_mi {
29003- for dc in 0..w_mi {
29004- grid.set(
29005- mi_row + dr,
29006- mi_col + dc,
29007- MiInfo {
29008- is_inter: true,
29009- ref_frame,
29010- ref_frame1: None,
29011- mv1: None,
29012- mv,
29013- is_new_mv,
29014- size: w_mi as u8,
29015- size_h: h_mi as u8,
29016- is_global_mv0: false,
29017- is_global_mv1: false,
29018- },
29019- );
29020- }
29021- }
28992+ grid.fill_rect(
28993+ mi_row,
28994+ mi_col,
28995+ h_mi,
28996+ w_mi,
28997+ MiInfo {
28998+ is_inter: true,
28999+ ref_frame,
29000+ ref_frame1: None,
29001+ mv1: None,
29002+ mv,
29003+ is_new_mv,
29004+ size: w_mi as u8,
29005+ size_h: h_mi as u8,
29006+ is_global_mv0: false,
29007+ is_global_mv1: false,
29008+ },
29009+ );
2902229010 }
2902329011
2902429012 /// Decodes one 8x8 leaf of a straddling 16x16 inter-frame block
@@ -29580,26 +29568,24 @@ fn decode_inter_block8(
2958029568 COMPOUND_WARP_HITS.with(|c| c.set(c.get() + 1));
2958129569 COMPOUND_WARP_HITS_8.with(|c| c.set(c.get() + 1));
2958229570 }
29583- for dr in 0..2 {
29584- for dc in 0..2 {
29585- grid.set(
29586- mi_row + dr,
29587- mi_col + dc,
29588- MiInfo {
29589- is_inter: true,
29590- ref_frame: ref0,
29591- ref_frame1: Some(ref1),
29592- mv1: Some(mv1),
29593- mv: mv0,
29594- is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7),
29595- size: 2,
29596- size_h: 2,
29597- is_global_mv0: is_global_mv0_c,
29598- is_global_mv1: is_global_mv1_c,
29599- },
29600- );
29601- }
29602- }
29571+ grid.fill_rect(
29572+ mi_row,
29573+ mi_col,
29574+ 2,
29575+ 2,
29576+ MiInfo {
29577+ is_inter: true,
29578+ ref_frame: ref0,
29579+ ref_frame1: Some(ref1),
29580+ mv1: Some(mv1),
29581+ mv: mv0,
29582+ is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7),
29583+ size: 2,
29584+ size_h: 2,
29585+ is_global_mv0: is_global_mv0_c,
29586+ is_global_mv1: is_global_mv1_c,
29587+ },
29588+ );
2960329589 mode_for_tx = 0;
2960429590
2960529591 let mut inter0_y = vec![0i32; SIDE * SIDE];
@@ -30257,28 +30243,26 @@ fn decode_inter_block8(
3025730243 warp_params.is_some(),
3025830244 );
3025930245 }
30260- for dr in 0..2 {
30261- for dc in 0..2 {
30262- grid.set(
30263- mi_row + dr,
30264- mi_col + dc,
30265- MiInfo {
30266- is_inter: true,
30267- ref_frame,
30268- // INTRA_FRAME marker for interintra blocks -- keeps
30269- // them out of warp-sample gathering (see 16/32 site).
30270- ref_frame1: interintra_mode.map(|_| 0),
30271- mv1: None,
30272- mv,
30273- is_new_mv,
30274- size: 2,
30275- size_h: 2,
30276- is_global_mv0: is_global_mv_block,
30277- is_global_mv1: false,
30278- },
30279- );
30280- }
30281- }
30246+ grid.fill_rect(
30247+ mi_row,
30248+ mi_col,
30249+ 2,
30250+ 2,
30251+ MiInfo {
30252+ is_inter: true,
30253+ ref_frame,
30254+ // INTRA_FRAME marker for interintra blocks -- keeps
30255+ // them out of warp-sample gathering (see 16/32 site).
30256+ ref_frame1: interintra_mode.map(|_| 0),
30257+ mv1: None,
30258+ mv,
30259+ is_new_mv,
30260+ size: 2,
30261+ size_h: 2,
30262+ is_global_mv0: is_global_mv_block,
30263+ is_global_mv1: false,
30264+ },
30265+ );
3028230266 mode_for_tx = 0;
3028330267
3028430268 // lane-scaledref r1: this leaf's own prediction is always `Regular`
@@ -30721,26 +30705,24 @@ fn decode_inter_block8(
3072130705 });
3072230706 mode_for_tx = mode;
3072330707 let (mi_row, mi_col) = leaf_mi;
30724- for dr in 0..2 {
30725- for dc in 0..2 {
30726- grid.set(
30727- mi_row + dr,
30728- mi_col + dc,
30729- MiInfo {
30730- is_inter: false,
30731- ref_frame: -1,
30732- ref_frame1: None,
30733- mv1: None,
30734- mv: (0, 0),
30735- is_new_mv: false,
30736- size: 2,
30737- size_h: 2,
30738- is_global_mv0: false,
30739- is_global_mv1: false,
30740- },
30741- );
30742- }
30743- }
30708+ grid.fill_rect(
30709+ mi_row,
30710+ mi_col,
30711+ 2,
30712+ 2,
30713+ MiInfo {
30714+ is_inter: false,
30715+ ref_frame: -1,
30716+ ref_frame1: None,
30717+ mv1: None,
30718+ mv: (0, 0),
30719+ is_new_mv: false,
30720+ size: 2,
30721+ size_h: 2,
30722+ is_global_mv0: false,
30723+ is_global_mv1: false,
30724+ },
30725+ );
3074430726
3074530727 // lane-leaf8tx r1: the 8x8 leaf's own intra `tx_depth` (libaom
3074630728 // `read_tx_size` -> `read_selected_tx_size`, max depth 1 at
crates/ec-av1/src/mvstack.rsmodified+117 -21
@@ -272,6 +272,23 @@ impl MiGrid {
272272 }
273273 }
274274
275+ /// Records one block's whole `bh4` x `bw4` footprint at once. Every
276+ /// caller published a block cell-by-cell through [`Self::set`]; the rows
277+ /// are contiguous, so this fills each of them with one `slice::fill` of
278+ /// the (`Copy`) cell instead, the way the neighbour grids' own span fills
279+ /// do.
280+ pub fn fill_rect(&mut self, row: usize, col: usize, h: usize, w: usize, info: MiInfo) {
281+ if col >= self.cols || row >= self.rows {
282+ return;
283+ }
284+ let w = w.min(self.cols - col);
285+ let cell = Some(info);
286+ for r in row..(row + h).min(self.rows) {
287+ let base = r * self.cols + col;
288+ self.cells[base..base + w].fill(cell);
289+ }
290+ }
291+
275292 /// The unit at `(row, col)`, or `None` when it is outside the grid, hasn't
276293 /// been coded, or sits outside the current tile's own bounds ([`Self::set_tile_bounds`]).
277294 pub fn get(&self, row: usize, col: usize) -> Option<&MiInfo> {
@@ -307,7 +324,7 @@ impl MiGrid {
307324
308325 /// One entry of the reference MV stack: a candidate motion vector and the
309326 /// total weight the neighbours that agreed on it contributed.
310-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
327+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
311328 pub struct StackEntry {
312329 /// The candidate `(row, col)` motion vector.
313330 pub mv: (i32, i32),
@@ -315,6 +332,85 @@ pub struct StackEntry {
315332 pub weight: u32,
316333 }
317334
335+/// A fixed-capacity, heap-free candidate list. The MV stack is rebuilt for
336+/// EVERY inter block, so the `Vec` this replaces was one malloc/free pair per
337+/// block (plus one for `drl_ctx`); capacity is a spec constant
338+/// ([`MAX_STACK_SIZE`]), so the storage is inline and `Deref` hands every
339+/// consumer the same slice API a `Vec` did.
340+#[derive(Clone, Copy, Debug)]
341+pub struct SmallVec<T: Copy, const N: usize> {
342+ len: u8,
343+ buf: [T; N],
344+}
345+
346+impl<T: Copy + Default, const N: usize> Default for SmallVec<T, N> {
347+ fn default() -> Self {
348+ Self {
349+ len: 0,
350+ buf: [T::default(); N],
351+ }
352+ }
353+}
354+
355+impl<T: Copy, const N: usize> SmallVec<T, N> {
356+ /// Appends `v`. Callers gate on [`MAX_STACK_SIZE`] before pushing (the
357+ /// spec drops the overflowing candidate at insertion), so a full list
358+ /// here is a bug, not a stream property.
359+ pub fn push(&mut self, v: T) {
360+ debug_assert!((self.len as usize) < N);
361+ self.buf[self.len as usize] = v;
362+ self.len += 1;
363+ }
364+
365+ /// `Vec::truncate`.
366+ pub fn truncate(&mut self, n: usize) {
367+ if n < self.len as usize {
368+ self.len = n as u8;
369+ }
370+ }
371+}
372+
373+impl<T: Copy + Default, const N: usize> FromIterator<T> for SmallVec<T, N> {
374+ fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
375+ let mut out = Self::default();
376+ for v in iter {
377+ out.push(v);
378+ }
379+ out
380+ }
381+}
382+
383+impl<T: Copy, const N: usize> std::ops::Deref for SmallVec<T, N> {
384+ type Target = [T];
385+ fn deref(&self) -> &[T] {
386+ &self.buf[..self.len as usize]
387+ }
388+}
389+
390+impl<T: Copy, const N: usize> std::ops::DerefMut for SmallVec<T, N> {
391+ fn deref_mut(&mut self) -> &mut [T] {
392+ &mut self.buf[..self.len as usize]
393+ }
394+}
395+
396+impl<T: Copy + PartialEq, const N: usize> PartialEq for SmallVec<T, N> {
397+ fn eq(&self, other: &Self) -> bool {
398+ **self == **other
399+ }
400+}
401+
402+impl<T: Copy + Eq, const N: usize> Eq for SmallVec<T, N> {}
403+
404+/// The single-reference MV stack's own list type.
405+pub type StackVec = SmallVec<StackEntry, MAX_STACK_SIZE>;
406+
407+/// The compound MV stack's own list type.
408+pub type CompoundStackVec = SmallVec<CompoundStackEntry, MAX_STACK_SIZE>;
409+
410+/// `drl_ctx`: one context per adjacent stack pair, so at most
411+/// `MAX_STACK_SIZE - 1` of them.
412+pub type DrlVec = SmallVec<usize, MAX_STACK_SIZE>;
413+
318414 /// The result of [`find_mv_stack`]: the sorted candidate list, the two
319415 /// predictors a `NEARESTMV`/`NEARMV` block reads from it, the predictor a
320416 /// `NEWMV` residual is coded against, and the symbol contexts the block's
@@ -323,7 +419,7 @@ pub struct StackEntry {
323419 pub struct MvStack {
324420 /// The candidates, highest weight first (ties keep scan order: above,
325421 /// then left, then top-right).
326- pub entries: Vec<StackEntry>,
422+ pub entries: StackVec,
327423 /// `RefStackMv[0]`, or `(0, 0)` when the stack is empty.
328424 pub nearest_mv: (i32, i32),
329425 /// `RefStackMv[1]`, or `(0, 0)` when the stack has fewer than two
@@ -340,7 +436,7 @@ pub struct MvStack {
340436 pub zero_mv_ctx: usize,
341437 /// Context for each `drl_mode` symbol between consecutive stack
342438 /// entries, `entries.len().saturating_sub(1)` long.
343- pub drl_ctx: Vec<usize>,
439+ pub drl_ctx: DrlVec,
344440 }
345441
346442 /// Spec 7.10.2.8's threshold a candidate's weight is compared against to
@@ -377,7 +473,7 @@ const MVREF_ROW_COLS: usize = 3;
377473 /// Adds one candidate MV with `weight` to `candidates`, merging it into an
378474 /// existing entry with the same MV rather than duplicating it (spec
379475 /// `add_ref_mv_candidate`, 7.10.2.5).
380-fn add_candidate(candidates: &mut Vec<StackEntry>, mv: (i32, i32), weight: u32) {
476+fn add_candidate(candidates: &mut StackVec, mv: (i32, i32), weight: u32) {
381477 if let Some(entry) = candidates.iter_mut().find(|e| e.mv == mv) {
382478 entry.weight += weight;
383479 } else if candidates.len() >= MAX_STACK_SIZE {
@@ -468,7 +564,7 @@ fn process_single_ref_mv_candidate(
468564 candidate: &MiInfo,
469565 ref_frame: i8,
470566 sign_bias_table: &SignBiasTable,
471- candidates: &mut Vec<StackEntry>,
567+ candidates: &mut StackVec,
472568 ) {
473569 if !candidate.is_inter {
474570 return;
@@ -538,7 +634,7 @@ fn scan_row(
538634 max_row_offset: isize,
539635 ref_frame: i8,
540636 gm: &GmMvTable,
541- candidates: &mut Vec<StackEntry>,
637+ candidates: &mut StackVec,
542638 newmv_count: &mut u32,
543639 processed_rows: &mut usize,
544640 ) -> bool {
@@ -626,7 +722,7 @@ fn scan_col(
626722 max_col_offset: isize,
627723 ref_frame: i8,
628724 gm: &GmMvTable,
629- candidates: &mut Vec<StackEntry>,
725+ candidates: &mut StackVec,
630726 newmv_count: &mut u32,
631727 processed_cols: &mut usize,
632728 ) -> bool {
@@ -762,7 +858,7 @@ fn scan_top_right(
762858 bw4: usize,
763859 ref_frame: i8,
764860 gm: &GmMvTable,
765- candidates: &mut Vec<StackEntry>,
861+ candidates: &mut StackVec,
766862 newmv_count: &mut u32,
767863 ) -> bool {
768864 let Some(row) = mi_row.checked_sub(1) else {
@@ -791,7 +887,7 @@ fn scan_corner(
791887 mi_col: usize,
792888 ref_frame: i8,
793889 gm: &GmMvTable,
794- candidates: &mut Vec<StackEntry>,
890+ candidates: &mut StackVec,
795891 newmv_count: &mut u32,
796892 ) -> bool {
797893 let (Some(row), Some(col)) = (mi_row.checked_sub(1), mi_col.checked_sub(1)) else {
@@ -919,7 +1015,7 @@ pub fn find_mv_stack_with_sign_bias(
9191015 ) -> MvStack {
9201016 // lane-alloc: the stack is capped at MAX_STACK_SIZE entries, so the pushes
9211017 // below grew (and memcpy'd) a fresh allocation up to 4 times per block.
922- let mut candidates = Vec::with_capacity(MAX_STACK_SIZE);
1018+ let mut candidates = StackVec::default();
9231019 let mut newmv_count = 0u32;
9241020
9251021 // libaom's `max_row_offset`/`max_col_offset` (`setup_ref_mv_list`): how
@@ -1011,7 +1107,7 @@ pub fn find_mv_stack_with_sign_bias(
10111107 // av1_find_mv_stack boosts only the entries the immediate scan found —
10121108 // exactly `candidates` at this point, before the corner probe (below)
10131109 // can add anything else (mvref_common.c ~line 544-545).
1014- for entry in &mut candidates {
1110+ for entry in candidates.iter_mut() {
10151111 entry.weight += REF_CAT_LEVEL;
10161112 }
10171113
@@ -1371,7 +1467,7 @@ pub fn find_mv_stack_with_sign_bias(
13711467 /// combined weight — spec 7.10.2's compound half of `add_ref_mv_candidate`
13721468 /// (libaom `mvref_common.c`, the `is_compound` branch of `scan_row_mbmi`/
13731469 /// `scan_col_mbmi`/`scan_blk_mbmi`).
1374-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1470+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
13751471 pub struct CompoundStackEntry {
13761472 /// The candidate's vote for `RefFrame[0]`'s motion vector.
13771473 pub mv0: (i32, i32),
@@ -1386,7 +1482,7 @@ pub struct CompoundStackEntry {
13861482 #[derive(Clone, Debug, PartialEq, Eq)]
13871483 pub struct CompoundMvStack {
13881484 /// The candidates, highest weight first.
1389- pub entries: Vec<CompoundStackEntry>,
1485+ pub entries: CompoundStackVec,
13901486 /// `RefStackMv[0]` for both refs, or `((0,0),(0,0))` when the stack is
13911487 /// empty.
13921488 pub nearest_mv: ((i32, i32), (i32, i32)),
@@ -1403,12 +1499,12 @@ pub struct CompoundMvStack {
14031499 /// Context for the compound `zero_mv` symbol.
14041500 pub zero_mv_ctx: usize,
14051501 /// Context for each `drl_mode` symbol between consecutive stack entries.
1406- pub drl_ctx: Vec<usize>,
1502+ pub drl_ctx: DrlVec,
14071503 }
14081504
14091505 /// [`add_candidate`], doubled: dedupes on the whole `(mv0, mv1)` pair.
14101506 fn add_compound_candidate(
1411- candidates: &mut Vec<CompoundStackEntry>,
1507+ candidates: &mut CompoundStackVec,
14121508 mv0: (i32, i32),
14131509 mv1: (i32, i32),
14141510 weight: u32,
@@ -1443,7 +1539,7 @@ fn scan_row_compound(
14431539 max_row_offset: isize,
14441540 ref_frame: (i8, i8),
14451541 gm: &GmMvTable,
1446- candidates: &mut Vec<CompoundStackEntry>,
1542+ candidates: &mut CompoundStackVec,
14471543 newmv_count: &mut u32,
14481544 processed_rows: &mut usize,
14491545 ) -> bool {
@@ -1506,7 +1602,7 @@ fn scan_col_compound(
15061602 max_col_offset: isize,
15071603 ref_frame: (i8, i8),
15081604 gm: &GmMvTable,
1509- candidates: &mut Vec<CompoundStackEntry>,
1605+ candidates: &mut CompoundStackVec,
15101606 newmv_count: &mut u32,
15111607 processed_cols: &mut usize,
15121608 ) -> bool {
@@ -1566,7 +1662,7 @@ fn scan_top_right_compound(
15661662 bw4: usize,
15671663 ref_frame: (i8, i8),
15681664 gm: &GmMvTable,
1569- candidates: &mut Vec<CompoundStackEntry>,
1665+ candidates: &mut CompoundStackVec,
15701666 newmv_count: &mut u32,
15711667 ) -> bool {
15721668 let Some(row) = mi_row.checked_sub(1) else {
@@ -1597,7 +1693,7 @@ fn scan_corner_compound(
15971693 mi_col: usize,
15981694 ref_frame: (i8, i8),
15991695 gm: &GmMvTable,
1600- candidates: &mut Vec<CompoundStackEntry>,
1696+ candidates: &mut CompoundStackVec,
16011697 newmv_count: &mut u32,
16021698 ) -> bool {
16031699 let (Some(row), Some(col)) = (mi_row.checked_sub(1), mi_col.checked_sub(1)) else {
@@ -1748,7 +1844,7 @@ pub fn find_mv_stack_compound(
17481844 gm: &GmMvTable,
17491845 tpl: Option<CompoundTplArgs>,
17501846 ) -> CompoundMvStack {
1751- let mut candidates: Vec<CompoundStackEntry> = Vec::with_capacity(MAX_STACK_SIZE);
1847+ let mut candidates = CompoundStackVec::default();
17521848 let mut newmv_count = 0u32;
17531849
17541850 let row_adj = bh4 < 2 && mi_row % 2 == 1;
@@ -1828,7 +1924,7 @@ pub fn find_mv_stack_compound(
18281924 let row_matched = found_above || found_top_right;
18291925 let nearest_match = usize::from(row_matched) + usize::from(found_left);
18301926
1831- for entry in &mut candidates {
1927+ for entry in candidates.iter_mut() {
18321928 entry.weight += REF_CAT_LEVEL;
18331929 }
18341930