Heap-free MV stack and row-fill mi publication
Browse files at this commitfc982dba9c966e5570f227a90fd6d8d1879d35f9Parent: 5b55d04- perf
- heap-free MV stack and row-fill mi publication
2 changed files +292 -214
- crates/ec-av1/src/decode.rs +175 -193
- crates/ec-av1/src/mvstack.rs +117 -21
crates/ec-av1/src/decode.rsmodified+175 -193
| @@ -24231,31 +24231,29 @@ fn decode_inter_block( | ||
| 24231 | 24231 | // too -- a square `bw4`x`bw4` stamp here claims a rect strip's |
| 24232 | 24232 | // NEXT strip's own rows/cols before it decodes, corrupting |
| 24233 | 24233 | // 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 | + ); | |
| 24259 | 24257 | // spec 5.11.25's `comp_group_idx`/`compound_idx`: only read when |
| 24260 | 24258 | // masked compound / distance-weighted compound are actually |
| 24261 | 24259 | // enabled for this stream -- `is_any_masked_compound_used_here` |
| @@ -25787,32 +25785,30 @@ fn decode_inter_block( | ||
| 25787 | 25785 | // lane-rect r2: see the compound path's matching comment above -- |
| 25788 | 25786 | // `grid` must be stamped with the block's true bh4/bw4 span, not |
| 25789 | 25787 | // 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 | + ); | |
| 25816 | 25812 | mode_for_tx = 0; |
| 25817 | 25813 | uv_predict_mode = DC_PRED; |
| 25818 | 25814 | |
| @@ -26619,26 +26615,24 @@ fn decode_inter_block( | ||
| 26619 | 26615 | // lane's `r * SUB_MI` re-derivation would snap a sub-SUB strip to |
| 26620 | 26616 | // its enclosing 16-px cell. |
| 26621 | 26617 | 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 | + ); | |
| 26642 | 26636 | neighbours.record_inter_rect_mi( |
| 26643 | 26637 | at, |
| 26644 | 26638 | write_w / MI, |
| @@ -26843,26 +26837,24 @@ fn decode_inter_block( | ||
| 26843 | 26837 | ref_frame_for_lf = 0; |
| 26844 | 26838 | globalmv_for_lf = false; |
| 26845 | 26839 | 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 | + ); | |
| 26866 | 26858 | |
| 26867 | 26859 | // lane-intrainter r1: an intra block in an inter frame reads the same |
| 26868 | 26860 | // `tx_depth` symbol a key-frame block does, and when it splits, the |
| @@ -28294,26 +28286,24 @@ fn decode_intra_sub8_leaf( | ||
| 28294 | 28286 | // intra-in-inter block writes them: `is_inter = 0`, `INTRA_FRAME` ref, no |
| 28295 | 28287 | // interpolation filter, no skip mode. |
| 28296 | 28288 | 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 | + ); | |
| 28317 | 28307 | SUB8_INTRA_RECT_HITS.with(|h| { |
| 28318 | 28308 | let mut s = h.get(); |
| 28319 | 28309 | s[usize::from(vert)] += 1; |
| @@ -28999,26 +28989,24 @@ fn grid_stamp_rect( | ||
| 28999 | 28989 | mv: (i32, i32), |
| 29000 | 28990 | is_new_mv: bool, |
| 29001 | 28991 | ) { |
| 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 | + ); | |
| 29022 | 29010 | } |
| 29023 | 29011 | |
| 29024 | 29012 | /// Decodes one 8x8 leaf of a straddling 16x16 inter-frame block |
| @@ -29580,26 +29568,24 @@ fn decode_inter_block8( | ||
| 29580 | 29568 | COMPOUND_WARP_HITS.with(|c| c.set(c.get() + 1)); |
| 29581 | 29569 | COMPOUND_WARP_HITS_8.with(|c| c.set(c.get() + 1)); |
| 29582 | 29570 | } |
| 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 | + ); | |
| 29603 | 29589 | mode_for_tx = 0; |
| 29604 | 29590 | |
| 29605 | 29591 | let mut inter0_y = vec![0i32; SIDE * SIDE]; |
| @@ -30257,28 +30243,26 @@ fn decode_inter_block8( | ||
| 30257 | 30243 | warp_params.is_some(), |
| 30258 | 30244 | ); |
| 30259 | 30245 | } |
| 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 | + ); | |
| 30282 | 30266 | mode_for_tx = 0; |
| 30283 | 30267 | |
| 30284 | 30268 | // lane-scaledref r1: this leaf's own prediction is always `Regular` |
| @@ -30721,26 +30705,24 @@ fn decode_inter_block8( | ||
| 30721 | 30705 | }); |
| 30722 | 30706 | mode_for_tx = mode; |
| 30723 | 30707 | 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 | + ); | |
| 30744 | 30726 | |
| 30745 | 30727 | // lane-leaf8tx r1: the 8x8 leaf's own intra `tx_depth` (libaom |
| 30746 | 30728 | // `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 { | ||
| 272 | 272 | } |
| 273 | 273 | } |
| 274 | 274 | |
| 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 | + | |
| 275 | 292 | /// The unit at `(row, col)`, or `None` when it is outside the grid, hasn't |
| 276 | 293 | /// been coded, or sits outside the current tile's own bounds ([`Self::set_tile_bounds`]). |
| 277 | 294 | pub fn get(&self, row: usize, col: usize) -> Option<&MiInfo> { |
| @@ -307,7 +324,7 @@ impl MiGrid { | ||
| 307 | 324 | |
| 308 | 325 | /// One entry of the reference MV stack: a candidate motion vector and the |
| 309 | 326 | /// total weight the neighbours that agreed on it contributed. |
| 310 | -#[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 327 | +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] | |
| 311 | 328 | pub struct StackEntry { |
| 312 | 329 | /// The candidate `(row, col)` motion vector. |
| 313 | 330 | pub mv: (i32, i32), |
| @@ -315,6 +332,85 @@ pub struct StackEntry { | ||
| 315 | 332 | pub weight: u32, |
| 316 | 333 | } |
| 317 | 334 | |
| 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 | + | |
| 318 | 414 | /// The result of [`find_mv_stack`]: the sorted candidate list, the two |
| 319 | 415 | /// predictors a `NEARESTMV`/`NEARMV` block reads from it, the predictor a |
| 320 | 416 | /// `NEWMV` residual is coded against, and the symbol contexts the block's |
| @@ -323,7 +419,7 @@ pub struct StackEntry { | ||
| 323 | 419 | pub struct MvStack { |
| 324 | 420 | /// The candidates, highest weight first (ties keep scan order: above, |
| 325 | 421 | /// then left, then top-right). |
| 326 | - pub entries: Vec<StackEntry>, | |
| 422 | + pub entries: StackVec, | |
| 327 | 423 | /// `RefStackMv[0]`, or `(0, 0)` when the stack is empty. |
| 328 | 424 | pub nearest_mv: (i32, i32), |
| 329 | 425 | /// `RefStackMv[1]`, or `(0, 0)` when the stack has fewer than two |
| @@ -340,7 +436,7 @@ pub struct MvStack { | ||
| 340 | 436 | pub zero_mv_ctx: usize, |
| 341 | 437 | /// Context for each `drl_mode` symbol between consecutive stack |
| 342 | 438 | /// entries, `entries.len().saturating_sub(1)` long. |
| 343 | - pub drl_ctx: Vec<usize>, | |
| 439 | + pub drl_ctx: DrlVec, | |
| 344 | 440 | } |
| 345 | 441 | |
| 346 | 442 | /// 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; | ||
| 377 | 473 | /// Adds one candidate MV with `weight` to `candidates`, merging it into an |
| 378 | 474 | /// existing entry with the same MV rather than duplicating it (spec |
| 379 | 475 | /// `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) { | |
| 381 | 477 | if let Some(entry) = candidates.iter_mut().find(|e| e.mv == mv) { |
| 382 | 478 | entry.weight += weight; |
| 383 | 479 | } else if candidates.len() >= MAX_STACK_SIZE { |
| @@ -468,7 +564,7 @@ fn process_single_ref_mv_candidate( | ||
| 468 | 564 | candidate: &MiInfo, |
| 469 | 565 | ref_frame: i8, |
| 470 | 566 | sign_bias_table: &SignBiasTable, |
| 471 | - candidates: &mut Vec<StackEntry>, | |
| 567 | + candidates: &mut StackVec, | |
| 472 | 568 | ) { |
| 473 | 569 | if !candidate.is_inter { |
| 474 | 570 | return; |
| @@ -538,7 +634,7 @@ fn scan_row( | ||
| 538 | 634 | max_row_offset: isize, |
| 539 | 635 | ref_frame: i8, |
| 540 | 636 | gm: &GmMvTable, |
| 541 | - candidates: &mut Vec<StackEntry>, | |
| 637 | + candidates: &mut StackVec, | |
| 542 | 638 | newmv_count: &mut u32, |
| 543 | 639 | processed_rows: &mut usize, |
| 544 | 640 | ) -> bool { |
| @@ -626,7 +722,7 @@ fn scan_col( | ||
| 626 | 722 | max_col_offset: isize, |
| 627 | 723 | ref_frame: i8, |
| 628 | 724 | gm: &GmMvTable, |
| 629 | - candidates: &mut Vec<StackEntry>, | |
| 725 | + candidates: &mut StackVec, | |
| 630 | 726 | newmv_count: &mut u32, |
| 631 | 727 | processed_cols: &mut usize, |
| 632 | 728 | ) -> bool { |
| @@ -762,7 +858,7 @@ fn scan_top_right( | ||
| 762 | 858 | bw4: usize, |
| 763 | 859 | ref_frame: i8, |
| 764 | 860 | gm: &GmMvTable, |
| 765 | - candidates: &mut Vec<StackEntry>, | |
| 861 | + candidates: &mut StackVec, | |
| 766 | 862 | newmv_count: &mut u32, |
| 767 | 863 | ) -> bool { |
| 768 | 864 | let Some(row) = mi_row.checked_sub(1) else { |
| @@ -791,7 +887,7 @@ fn scan_corner( | ||
| 791 | 887 | mi_col: usize, |
| 792 | 888 | ref_frame: i8, |
| 793 | 889 | gm: &GmMvTable, |
| 794 | - candidates: &mut Vec<StackEntry>, | |
| 890 | + candidates: &mut StackVec, | |
| 795 | 891 | newmv_count: &mut u32, |
| 796 | 892 | ) -> bool { |
| 797 | 893 | 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( | ||
| 919 | 1015 | ) -> MvStack { |
| 920 | 1016 | // lane-alloc: the stack is capped at MAX_STACK_SIZE entries, so the pushes |
| 921 | 1017 | // 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(); | |
| 923 | 1019 | let mut newmv_count = 0u32; |
| 924 | 1020 | |
| 925 | 1021 | // 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( | ||
| 1011 | 1107 | // av1_find_mv_stack boosts only the entries the immediate scan found — |
| 1012 | 1108 | // exactly `candidates` at this point, before the corner probe (below) |
| 1013 | 1109 | // can add anything else (mvref_common.c ~line 544-545). |
| 1014 | - for entry in &mut candidates { | |
| 1110 | + for entry in candidates.iter_mut() { | |
| 1015 | 1111 | entry.weight += REF_CAT_LEVEL; |
| 1016 | 1112 | } |
| 1017 | 1113 | |
| @@ -1371,7 +1467,7 @@ pub fn find_mv_stack_with_sign_bias( | ||
| 1371 | 1467 | /// combined weight — spec 7.10.2's compound half of `add_ref_mv_candidate` |
| 1372 | 1468 | /// (libaom `mvref_common.c`, the `is_compound` branch of `scan_row_mbmi`/ |
| 1373 | 1469 | /// `scan_col_mbmi`/`scan_blk_mbmi`). |
| 1374 | -#[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 1470 | +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] | |
| 1375 | 1471 | pub struct CompoundStackEntry { |
| 1376 | 1472 | /// The candidate's vote for `RefFrame[0]`'s motion vector. |
| 1377 | 1473 | pub mv0: (i32, i32), |
| @@ -1386,7 +1482,7 @@ pub struct CompoundStackEntry { | ||
| 1386 | 1482 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 1387 | 1483 | pub struct CompoundMvStack { |
| 1388 | 1484 | /// The candidates, highest weight first. |
| 1389 | - pub entries: Vec<CompoundStackEntry>, | |
| 1485 | + pub entries: CompoundStackVec, | |
| 1390 | 1486 | /// `RefStackMv[0]` for both refs, or `((0,0),(0,0))` when the stack is |
| 1391 | 1487 | /// empty. |
| 1392 | 1488 | pub nearest_mv: ((i32, i32), (i32, i32)), |
| @@ -1403,12 +1499,12 @@ pub struct CompoundMvStack { | ||
| 1403 | 1499 | /// Context for the compound `zero_mv` symbol. |
| 1404 | 1500 | pub zero_mv_ctx: usize, |
| 1405 | 1501 | /// Context for each `drl_mode` symbol between consecutive stack entries. |
| 1406 | - pub drl_ctx: Vec<usize>, | |
| 1502 | + pub drl_ctx: DrlVec, | |
| 1407 | 1503 | } |
| 1408 | 1504 | |
| 1409 | 1505 | /// [`add_candidate`], doubled: dedupes on the whole `(mv0, mv1)` pair. |
| 1410 | 1506 | fn add_compound_candidate( |
| 1411 | - candidates: &mut Vec<CompoundStackEntry>, | |
| 1507 | + candidates: &mut CompoundStackVec, | |
| 1412 | 1508 | mv0: (i32, i32), |
| 1413 | 1509 | mv1: (i32, i32), |
| 1414 | 1510 | weight: u32, |
| @@ -1443,7 +1539,7 @@ fn scan_row_compound( | ||
| 1443 | 1539 | max_row_offset: isize, |
| 1444 | 1540 | ref_frame: (i8, i8), |
| 1445 | 1541 | gm: &GmMvTable, |
| 1446 | - candidates: &mut Vec<CompoundStackEntry>, | |
| 1542 | + candidates: &mut CompoundStackVec, | |
| 1447 | 1543 | newmv_count: &mut u32, |
| 1448 | 1544 | processed_rows: &mut usize, |
| 1449 | 1545 | ) -> bool { |
| @@ -1506,7 +1602,7 @@ fn scan_col_compound( | ||
| 1506 | 1602 | max_col_offset: isize, |
| 1507 | 1603 | ref_frame: (i8, i8), |
| 1508 | 1604 | gm: &GmMvTable, |
| 1509 | - candidates: &mut Vec<CompoundStackEntry>, | |
| 1605 | + candidates: &mut CompoundStackVec, | |
| 1510 | 1606 | newmv_count: &mut u32, |
| 1511 | 1607 | processed_cols: &mut usize, |
| 1512 | 1608 | ) -> bool { |
| @@ -1566,7 +1662,7 @@ fn scan_top_right_compound( | ||
| 1566 | 1662 | bw4: usize, |
| 1567 | 1663 | ref_frame: (i8, i8), |
| 1568 | 1664 | gm: &GmMvTable, |
| 1569 | - candidates: &mut Vec<CompoundStackEntry>, | |
| 1665 | + candidates: &mut CompoundStackVec, | |
| 1570 | 1666 | newmv_count: &mut u32, |
| 1571 | 1667 | ) -> bool { |
| 1572 | 1668 | let Some(row) = mi_row.checked_sub(1) else { |
| @@ -1597,7 +1693,7 @@ fn scan_corner_compound( | ||
| 1597 | 1693 | mi_col: usize, |
| 1598 | 1694 | ref_frame: (i8, i8), |
| 1599 | 1695 | gm: &GmMvTable, |
| 1600 | - candidates: &mut Vec<CompoundStackEntry>, | |
| 1696 | + candidates: &mut CompoundStackVec, | |
| 1601 | 1697 | newmv_count: &mut u32, |
| 1602 | 1698 | ) -> bool { |
| 1603 | 1699 | 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( | ||
| 1748 | 1844 | gm: &GmMvTable, |
| 1749 | 1845 | tpl: Option<CompoundTplArgs>, |
| 1750 | 1846 | ) -> CompoundMvStack { |
| 1751 | - let mut candidates: Vec<CompoundStackEntry> = Vec::with_capacity(MAX_STACK_SIZE); | |
| 1847 | + let mut candidates = CompoundStackVec::default(); | |
| 1752 | 1848 | let mut newmv_count = 0u32; |
| 1753 | 1849 | |
| 1754 | 1850 | let row_adj = bh4 < 2 && mi_row % 2 == 1; |
| @@ -1828,7 +1924,7 @@ pub fn find_mv_stack_compound( | ||
| 1828 | 1924 | let row_matched = found_above || found_top_right; |
| 1829 | 1925 | let nearest_match = usize::from(row_matched) + usize::from(found_left); |
| 1830 | 1926 | |
| 1831 | - for entry in &mut candidates { | |
| 1927 | + for entry in candidates.iter_mut() { | |
| 1832 | 1928 | entry.weight += REF_CAT_LEVEL; |
| 1833 | 1929 | } |
| 1834 | 1930 |