Port libaom's real MV-stack weight formula for variable block sizes
Browse files at this commit34a0447bfdf7431997b468c10801aef21f00e24fParent: caef4a8- fix
- port libaom's real MV-stack weight formula for variable block sizes
1 changed file +258 -177
- crates/ec-av1/src/mvstack.rs +258 -177
crates/ec-av1/src/mvstack.rsmodified+258 -177
| @@ -39,18 +39,19 @@ | |||
| 39 | //! | 39 | //! |
| 40 | //! The corner probe's own candidate is added to the stack too (matching | 40 | //! The corner probe's own candidate is added to the stack too (matching |
| 41 | //! libaom: a genuinely new MV there becomes a real, if low-weight, entry), | 41 | //! libaom: a genuinely new MV there becomes a real, if low-weight, entry), |
| 42 | -//! but — like the reduced row/col/top-right scans — at this module's own | 42 | +//! at libaom's real `2 * mi_size_wide[BLOCK_8X8]` (`CORNER_WEIGHT`, 4) — |
| 43 | -//! `IMMEDIATE_WEIGHT` scale rather than libaom's real `len * max(2, inc)` | 43 | +//! same as the top-right probe, which shares `scan_blk_mbmi`'s fixed weight |
| 44 | -//! (row/col) or `2 * mi_size_wide[BLOCK_8X8]` (corner/top-right) weights. | 44 | +//! in libaom too. The row/col scans (`scan_row`/`scan_col`) carry libaom's |
| 45 | -//! That scale mismatch is harmless for every caller this module has: the | 45 | +//! real per-block-run `len * weight` formula (`scan_row_mbmi`/ |
| 46 | -//! only real caller (`tile.rs`) always asks for a fixed `bw4 == bh4 == 8` | 46 | +//! `scan_col_mbmi`, mvref_common.c), not a flat per-cell weight: this |
| 47 | -//! block, at which size libaom's row and col weights are *always tied* | 47 | +//! module shipped a flat `IMMEDIATE_WEIGHT = 2` for several rounds, exact |
| 48 | -//! (both scan a full-width same-size neighbour) and its corner/top-right | 48 | +//! only for `tile.rs`'s own encoder (always a fixed `bw4 == bh4 == 8` query |
| 49 | -//! weight is *always* far below either — exactly the ordering | 49 | +//! against same-size-8 neighbours, where the real formula's `bw4 <= n4` |
| 50 | -//! `IMMEDIATE_WEIGHT` already reproduces, so sort order and the | 50 | +//! weight boost applies identically on every candidate and so never |
| 51 | -//! `REF_CAT_LEVEL`-vs-not DRL split (which only tests a threshold, not a | 51 | +//! reorders anything) — decode.rs's real variable partition sizes need the |
| 52 | -//! magnitude) come out identical either way. A caller with mixed block | 52 | +//! real formula, since a same-or-wider neighbour against a *smaller* query |
| 53 | -//! sizes would need the real weight formula; none exists today. | 53 | +//! block gets a real, non-flat weight boost there libaom's DRL-index |
| 54 | +//! selection depends on. | ||
| 54 | //! | 55 | //! |
| 55 | //! The corner probe's own match does *not* count toward the real | 56 | //! The corner probe's own match does *not* count toward the real |
| 56 | //! `newmv_count` that feeds `new_mv_ctx` (libaom passes it a `dummy_newmv_count` | 57 | //! `newmv_count` that feeds `new_mv_ctx` (libaom passes it a `dummy_newmv_count` |
| @@ -69,12 +70,8 @@ | |||
| 69 | //! | 70 | //! |
| 70 | //! Deliberately reduced away (see the crate report, not reproduced bit-exact | 71 | //! Deliberately reduced away (see the crate report, not reproduced bit-exact |
| 71 | //! here): compound/multi-reference candidates, the temporal MV projection | 72 | //! here): compound/multi-reference candidates, the temporal MV projection |
| 72 | -//! scan, the real (non-corner) `idx = 2..MVREF_ROW_COLS` extended row/col | 73 | +//! scan, and the single-reference "extension" pass that re-walks the same |
| 73 | -//! scan positions (libaom's own `processed_rows`/`processed_cols` bookkeeping | 74 | +//! row/col |
| 74 | -//! means those never fire for this module's fixed 8-mi block geometry — the | ||
| 75 | -//! immediate-offset scan's coverage window already reaches as far as the | ||
| 76 | -//! extended offsets can, so nothing there is left unscanned), the | ||
| 77 | -//! single-reference "extension" pass that re-walks the same row/col | ||
| 78 | //! neighbours through `process_single_ref_mv_candidate` (a no-op here: with | 75 | //! neighbours through `process_single_ref_mv_candidate` (a no-op here: with |
| 79 | //! only one reference frame ever in play, whatever it would add was already | 76 | //! only one reference frame ever in play, whatever it would add was already |
| 80 | //! added by the row/col scan), and the "extra search" that pads a short | 77 | //! added by the row/col scan), and the "extra search" that pads a short |
| @@ -195,13 +192,19 @@ const REF_CAT_LEVEL: u32 = 640; | |||
| 195 | /// The most candidates a stack keeps (spec `MAX_REF_MV_STACK_SIZE`). | 192 | /// The most candidates a stack keeps (spec `MAX_REF_MV_STACK_SIZE`). |
| 196 | const MAX_STACK_SIZE: usize = 8; | 193 | const MAX_STACK_SIZE: usize = 8; |
| 197 | 194 | ||
| 198 | -/// Weight one immediate (row `-1` / col `-1`) neighbour 4x4 unit's vote | 195 | +/// The floor libaom's `scan_row_mbmi`/`scan_col_mbmi` weight starts at |
| 199 | -/// counts for (spec: `len * 2` with `len = 1` per unit for the reduction | 196 | +/// before a same-or-wider candidate block can raise it (`uint16_t weight = |
| 200 | -/// this module makes — see the module doc). | 197 | +/// 2;`, mvref_common.c). |
| 201 | -const IMMEDIATE_WEIGHT: u32 = 2; | 198 | +const ROW_COL_WEIGHT_FLOOR: u32 = 2; |
| 199 | + | ||
| 200 | +/// Fixed weight libaom's `scan_blk_mbmi` uses for both single-cell probes | ||
| 201 | +/// (`2 * mi_size_wide[BLOCK_8X8]` = `2 * 2`): the diagonal corner and the | ||
| 202 | +/// top-right, neither of which ever runs the row/col scans' real per-block | ||
| 203 | +/// weight below. | ||
| 204 | +const CORNER_WEIGHT: u32 = 4; | ||
| 202 | 205 | ||
| 203 | /// Spec `MVREF_ROW_COLS`: the extended row/col scan runs at offsets `-3` and | 206 | /// Spec `MVREF_ROW_COLS`: the extended row/col scan runs at offsets `-3` and |
| 204 | -/// `-5` (`idx = 2..=3`), one past `IMMEDIATE_WEIGHT`'s own `-1`. | 207 | +/// `-5` (`idx = 2..=3`), one past the immediate scan's own `-1`. |
| 205 | const MVREF_ROW_COLS: usize = 3; | 208 | const MVREF_ROW_COLS: usize = 3; |
| 206 | 209 | ||
| 207 | /// Adds one candidate MV with `weight` to `candidates`, merging it into an | 210 | /// Adds one candidate MV with `weight` to `candidates`, merging it into an |
| @@ -215,148 +218,133 @@ fn add_candidate(candidates: &mut Vec<StackEntry>, mv: (i32, i32), weight: u32) | |||
| 215 | } | 218 | } |
| 216 | } | 219 | } |
| 217 | 220 | ||
| 218 | -/// The row-above scan (spec 7.10.2.2): every 4x4 unit directly above the | 221 | +/// The row scan (spec 7.10.2.2, libaom `scan_row_mbmi`): walks candidate |
| 219 | -/// block's span. Returns whether any unit voted, and `processed_rows` | 222 | +/// *blocks* — not individual 4x4 cells — along the row `row_offset` mi units |
| 220 | -/// (libaom `scan_row_mbmi`'s output): how many rows past the immediate one | 223 | +/// above the block's own row, in run-length steps sized by each candidate's |
| 221 | -/// this span already covers by *geometry* alone (0 unless a same-or-wider | 224 | +/// own block width. Handles both the immediate probe (`row_offset == -1`) |
| 222 | -/// neighbour occupies it, in which case that neighbour's own `size` is the | 225 | +/// and the extended ones (`-3`, `-5`) libaom threads through the same |
| 223 | -/// coverage, spec 7.10.2.2's `inc` term reduced to this crate's square, | 226 | +/// function: `max_row_offset` is the immediate scan's own reach (spec |
| 224 | -/// fixed-weight candidates) -- tracked for *every* coded cell, intra or | 227 | +/// 7.10.2.4's `find_valid_row_offset`, clamped to the tile's top edge), used |
| 225 | -/// inter, since libaom's `processed_rows` update sits outside the | 228 | +/// only to size the weight boost below, not to gate whether this call runs |
| 226 | -/// ref-frame-matching `add_ref_mv_candidate` call. | 229 | +/// at all (the caller does that via `processed_rows`/the tile edge). |
| 230 | +/// | ||
| 231 | +/// Each matching candidate's vote is weighted `len * weight`: `len` is how | ||
| 232 | +/// many mi columns the run covers (the candidate's own width, floored to | ||
| 233 | +/// the query block's `bw4` and — at the wide/extended-scan geometries | ||
| 234 | +/// libaom's own `use_step_16`/`abs(row_offset) > 1` branches cover — raised | ||
| 235 | +/// to a block-size floor); `weight` starts at 2 and, only when the candidate | ||
| 236 | +/// is at least as wide as the query block (`bw4 <= n4`), rises to how much | ||
| 237 | +/// of `max_row_offset`'s remaining reach the candidate's own height can | ||
| 238 | +/// still cover. That `bw4 <= n4` condition is why this module's old flat | ||
| 239 | +/// `IMMEDIATE_WEIGHT = 2` was exact for every real caller before decode.rs | ||
| 240 | +/// existed: `tile.rs`'s own encoder only ever asks with `bw4 == 8` against | ||
| 241 | +/// neighbours it also always wrote at `size == 8`, and every synthetic test | ||
| 242 | +/// neighbour here uses `size: 1` (which the `n4 < weight`-floor condition | ||
| 243 | +/// keeps at the `2` floor regardless of `bw4`) — this real formula only | ||
| 244 | +/// diverges from that flat weight when a *bigger* candidate meets a | ||
| 245 | +/// *smaller* query block, which only decode.rs's variable partition sizes | ||
| 246 | +/// ever produce. | ||
| 247 | +/// | ||
| 248 | +/// Threads `processed_rows` (`*mut` in libaom, `&mut` here) across every row | ||
| 249 | +/// call — immediate, then the two extended offsets — the way libaom's | ||
| 250 | +/// single stack `int` does: only the `bw4 <= n4` branch above updates it, | ||
| 251 | +/// same as libaom only updating `*processed_rows` there. | ||
| 252 | +#[allow(clippy::too_many_arguments)] | ||
| 227 | fn scan_row( | 253 | fn scan_row( |
| 228 | grid: &MiGrid, | 254 | grid: &MiGrid, |
| 229 | mi_row: usize, | 255 | mi_row: usize, |
| 230 | mi_col: usize, | 256 | mi_col: usize, |
| 231 | bw4: usize, | 257 | bw4: usize, |
| 258 | + row_offset: isize, | ||
| 259 | + max_row_offset: isize, | ||
| 232 | ref_frame: i8, | 260 | ref_frame: i8, |
| 233 | candidates: &mut Vec<StackEntry>, | 261 | candidates: &mut Vec<StackEntry>, |
| 234 | newmv_count: &mut u32, | 262 | newmv_count: &mut u32, |
| 235 | -) -> (bool, usize) { | 263 | + processed_rows: &mut usize, |
| 236 | - let Some(row) = mi_row.checked_sub(1) else { | 264 | +) -> bool { |
| 237 | - return (false, 0); | 265 | + let Some(row) = mi_row.checked_add_signed(row_offset) else { |
| 266 | + return false; | ||
| 238 | }; | 267 | }; |
| 268 | + let col_shift: usize = if row_offset.unsigned_abs() > 1 { 1 } else { 0 }; | ||
| 269 | + let use_step_16 = bw4 >= 16; | ||
| 270 | + let end_mi = bw4.min(16); | ||
| 239 | let mut found = false; | 271 | let mut found = false; |
| 240 | - let mut processed_rows = 0; | 272 | + let mut i = 0usize; |
| 241 | - for col in mi_col..mi_col + bw4 { | 273 | + while i < end_mi { |
| 242 | - let Some(info) = grid.get(row, col) else { | 274 | + let Some(info) = grid.get(row, mi_col + col_shift + i) else { |
| 275 | + i += 1; | ||
| 243 | continue; | 276 | continue; |
| 244 | }; | 277 | }; |
| 245 | - if bw4 <= info.size { | 278 | + let n4 = info.size; |
| 246 | - processed_rows = processed_rows.max(info.size); | 279 | + let mut len = bw4.min(n4); |
| 280 | + if use_step_16 { | ||
| 281 | + len = len.max(4); | ||
| 282 | + } else if row_offset.unsigned_abs() > 1 { | ||
| 283 | + len = len.max(2); | ||
| 284 | + } | ||
| 285 | + let mut weight = ROW_COL_WEIGHT_FLOOR; | ||
| 286 | + if bw4 <= n4 { | ||
| 287 | + let inc = ((-max_row_offset + row_offset + 1) as usize).min(n4); | ||
| 288 | + weight = weight.max(inc as u32); | ||
| 289 | + *processed_rows = (inc as isize - row_offset - 1).max(0) as usize; | ||
| 247 | } | 290 | } |
| 248 | if info.is_inter && info.ref_frame == ref_frame { | 291 | if info.is_inter && info.ref_frame == ref_frame { |
| 249 | found = true; | 292 | found = true; |
| 250 | - add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); | 293 | + add_candidate(candidates, info.mv, len as u32 * weight); |
| 251 | *newmv_count += u32::from(info.is_new_mv); | 294 | *newmv_count += u32::from(info.is_new_mv); |
| 252 | } | 295 | } |
| 296 | + i += len.max(1); | ||
| 253 | } | 297 | } |
| 254 | - (found, processed_rows) | 298 | + found |
| 255 | } | 299 | } |
| 256 | 300 | ||
| 257 | -/// The col-left scan (spec 7.10.2.3): every 4x4 unit directly left of the | 301 | +/// The col scan (spec 7.10.2.3, libaom `scan_col_mbmi`): `scan_row`, |
| 258 | -/// block's span. Returns whether any unit voted, and `processed_cols` | 302 | +/// transposed. |
| 259 | -/// (`scan_row`'s doc, transposed). | 303 | +#[allow(clippy::too_many_arguments)] |
| 260 | fn scan_col( | 304 | fn scan_col( |
| 261 | grid: &MiGrid, | 305 | grid: &MiGrid, |
| 262 | mi_row: usize, | 306 | mi_row: usize, |
| 263 | mi_col: usize, | 307 | mi_col: usize, |
| 264 | bh4: usize, | 308 | bh4: usize, |
| 309 | + col_offset: isize, | ||
| 310 | + max_col_offset: isize, | ||
| 265 | ref_frame: i8, | 311 | ref_frame: i8, |
| 266 | candidates: &mut Vec<StackEntry>, | 312 | candidates: &mut Vec<StackEntry>, |
| 267 | newmv_count: &mut u32, | 313 | newmv_count: &mut u32, |
| 268 | -) -> (bool, usize) { | 314 | + processed_cols: &mut usize, |
| 269 | - let Some(col) = mi_col.checked_sub(1) else { | 315 | +) -> bool { |
| 270 | - return (false, 0); | 316 | + let Some(col) = mi_col.checked_add_signed(col_offset) else { |
| 317 | + return false; | ||
| 271 | }; | 318 | }; |
| 319 | + let row_shift: usize = if col_offset.unsigned_abs() > 1 { 1 } else { 0 }; | ||
| 320 | + let use_step_16 = bh4 >= 16; | ||
| 321 | + let end_mi = bh4.min(16); | ||
| 272 | let mut found = false; | 322 | let mut found = false; |
| 273 | - let mut processed_cols = 0; | 323 | + let mut i = 0usize; |
| 274 | - for row in mi_row..mi_row + bh4 { | 324 | + while i < end_mi { |
| 275 | - let Some(info) = grid.get(row, col) else { | 325 | + let Some(info) = grid.get(mi_row + row_shift + i, col) else { |
| 326 | + i += 1; | ||
| 276 | continue; | 327 | continue; |
| 277 | }; | 328 | }; |
| 278 | - if bh4 <= info.size { | 329 | + let n4 = info.size; |
| 279 | - processed_cols = processed_cols.max(info.size); | 330 | + let mut len = bh4.min(n4); |
| 331 | + if use_step_16 { | ||
| 332 | + len = len.max(4); | ||
| 333 | + } else if col_offset.unsigned_abs() > 1 { | ||
| 334 | + len = len.max(2); | ||
| 335 | + } | ||
| 336 | + let mut weight = ROW_COL_WEIGHT_FLOOR; | ||
| 337 | + if bh4 <= n4 { | ||
| 338 | + let inc = ((-max_col_offset + col_offset + 1) as usize).min(n4); | ||
| 339 | + weight = weight.max(inc as u32); | ||
| 340 | + *processed_cols = (inc as isize - col_offset - 1).max(0) as usize; | ||
| 280 | } | 341 | } |
| 281 | if info.is_inter && info.ref_frame == ref_frame { | 342 | if info.is_inter && info.ref_frame == ref_frame { |
| 282 | found = true; | 343 | found = true; |
| 283 | - add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); | 344 | + add_candidate(candidates, info.mv, len as u32 * weight); |
| 284 | *newmv_count += u32::from(info.is_new_mv); | 345 | *newmv_count += u32::from(info.is_new_mv); |
| 285 | } | 346 | } |
| 286 | - } | 347 | + i += len.max(1); |
| 287 | - (found, processed_cols) | ||
| 288 | -} | ||
| 289 | - | ||
| 290 | -/// The extended row scan (spec 7.10.2.4's `idx = 2..MVREF_ROW_COLS` row | ||
| 291 | -/// positions, libaom `mvref_common.c`'s second loop): probes rows `-3` and | ||
| 292 | -/// `-5`, shifted one column right (`col_offset = 1`, libaom's `col_adj` | ||
| 293 | -/// collapses to that for every block size this crate ever queries), each | ||
| 294 | -/// only run when the immediate scan's own `processed_rows` hasn't already | ||
| 295 | -/// covered it and the tile's top edge doesn't clip it first. Its match folds | ||
| 296 | -/// into the row side (`ref_match_count`, never `nearest_match`), same as the | ||
| 297 | -/// corner probe. | ||
| 298 | -fn scan_row_extended( | ||
| 299 | - grid: &MiGrid, | ||
| 300 | - mi_row: usize, | ||
| 301 | - mi_col: usize, | ||
| 302 | - bw4: usize, | ||
| 303 | - ref_frame: i8, | ||
| 304 | - candidates: &mut Vec<StackEntry>, | ||
| 305 | - processed_rows: usize, | ||
| 306 | -) -> bool { | ||
| 307 | - let max_row_offset = mi_row.min(MVREF_ROW_COLS * 2); | ||
| 308 | - let mut found = false; | ||
| 309 | - for idx in 2..=MVREF_ROW_COLS { | ||
| 310 | - let offset = idx * 2 - 1; | ||
| 311 | - if offset > max_row_offset || offset <= processed_rows { | ||
| 312 | - continue; | ||
| 313 | - } | ||
| 314 | - let Some(row) = mi_row.checked_sub(offset) else { | ||
| 315 | - continue; | ||
| 316 | - }; | ||
| 317 | - for col in mi_col + 1..mi_col + 1 + bw4 { | ||
| 318 | - if let Some(info) = grid.get(row, col) | ||
| 319 | - && info.is_inter | ||
| 320 | - && info.ref_frame == ref_frame | ||
| 321 | - { | ||
| 322 | - found = true; | ||
| 323 | - add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); | ||
| 324 | - } | ||
| 325 | - } | ||
| 326 | - } | ||
| 327 | - found | ||
| 328 | -} | ||
| 329 | - | ||
| 330 | -/// The extended col scan: `scan_row_extended`, transposed (probes cols `-3` | ||
| 331 | -/// and `-5`, shifted one row down). | ||
| 332 | -fn scan_col_extended( | ||
| 333 | - grid: &MiGrid, | ||
| 334 | - mi_row: usize, | ||
| 335 | - mi_col: usize, | ||
| 336 | - bh4: usize, | ||
| 337 | - ref_frame: i8, | ||
| 338 | - candidates: &mut Vec<StackEntry>, | ||
| 339 | - processed_cols: usize, | ||
| 340 | -) -> bool { | ||
| 341 | - let max_col_offset = mi_col.min(MVREF_ROW_COLS * 2); | ||
| 342 | - let mut found = false; | ||
| 343 | - for idx in 2..=MVREF_ROW_COLS { | ||
| 344 | - let offset = idx * 2 - 1; | ||
| 345 | - if offset > max_col_offset || offset <= processed_cols { | ||
| 346 | - continue; | ||
| 347 | - } | ||
| 348 | - let Some(col) = mi_col.checked_sub(offset) else { | ||
| 349 | - continue; | ||
| 350 | - }; | ||
| 351 | - for row in mi_row + 1..mi_row + 1 + bh4 { | ||
| 352 | - if let Some(info) = grid.get(row, col) | ||
| 353 | - && info.is_inter | ||
| 354 | - && info.ref_frame == ref_frame | ||
| 355 | - { | ||
| 356 | - found = true; | ||
| 357 | - add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); | ||
| 358 | - } | ||
| 359 | - } | ||
| 360 | } | 348 | } |
| 361 | found | 349 | found |
| 362 | } | 350 | } |
| @@ -384,7 +372,7 @@ fn scan_top_right( | |||
| 384 | && info.is_inter | 372 | && info.is_inter |
| 385 | && info.ref_frame == ref_frame | 373 | && info.ref_frame == ref_frame |
| 386 | { | 374 | { |
| 387 | - add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); | 375 | + add_candidate(candidates, info.mv, CORNER_WEIGHT); |
| 388 | *newmv_count += u32::from(info.is_new_mv); | 376 | *newmv_count += u32::from(info.is_new_mv); |
| 389 | return true; | 377 | return true; |
| 390 | } | 378 | } |
| @@ -412,7 +400,7 @@ fn scan_corner( | |||
| 412 | && info.is_inter | 400 | && info.is_inter |
| 413 | && info.ref_frame == ref_frame | 401 | && info.ref_frame == ref_frame |
| 414 | { | 402 | { |
| 415 | - add_candidate(candidates, info.mv, IMMEDIATE_WEIGHT); | 403 | + add_candidate(candidates, info.mv, CORNER_WEIGHT); |
| 416 | *newmv_count += u32::from(info.is_new_mv); | 404 | *newmv_count += u32::from(info.is_new_mv); |
| 417 | return true; | 405 | return true; |
| 418 | } | 406 | } |
| @@ -469,24 +457,56 @@ pub fn find_mv_stack( | |||
| 469 | ) -> MvStack { | 457 | ) -> MvStack { |
| 470 | let mut candidates = Vec::new(); | 458 | let mut candidates = Vec::new(); |
| 471 | let mut newmv_count = 0u32; | 459 | let mut newmv_count = 0u32; |
| 472 | - let (found_above, processed_rows) = scan_row( | 460 | + |
| 473 | - grid, | 461 | + // libaom's `max_row_offset`/`max_col_offset` (`setup_ref_mv_list`): how |
| 474 | - mi_row, | 462 | + // far the row/col scans may reach, clamped to the tile's near edge |
| 475 | - mi_col, | 463 | + // (single-tile-per-frame here, so that's just the frame edge at 0) -- |
| 476 | - bw4, | 464 | + // `row_adj`/`col_adj` only ever fire for a sub-8x8 query block, which no |
| 477 | - ref_frame, | 465 | + // real caller of this module ever is, but are ported anyway since |
| 478 | - &mut candidates, | 466 | + // nothing here relies on that. |
| 479 | - &mut newmv_count, | 467 | + let row_adj = bh4 < 2 && mi_row % 2 == 1; |
| 480 | - ); | 468 | + let col_adj = bw4 < 2 && mi_col % 2 == 1; |
| 481 | - let (found_left, processed_cols) = scan_col( | 469 | + let max_row_offset: isize = if mi_row > 0 { |
| 482 | - grid, | 470 | + let reach = if bh4 < 2 { -4 } else { -6 } + isize::from(row_adj); |
| 483 | - mi_row, | 471 | + reach.max(-(mi_row as isize)) |
| 484 | - mi_col, | 472 | + } else { |
| 485 | - bh4, | 473 | + 0 |
| 486 | - ref_frame, | 474 | + }; |
| 487 | - &mut candidates, | 475 | + let max_col_offset: isize = if mi_col > 0 { |
| 488 | - &mut newmv_count, | 476 | + let reach = if bw4 < 2 { -4 } else { -6 } + isize::from(col_adj); |
| 489 | - ); | 477 | + reach.max(-(mi_col as isize)) |
| 478 | + } else { | ||
| 479 | + 0 | ||
| 480 | + }; | ||
| 481 | + | ||
| 482 | + let mut processed_rows = 0usize; | ||
| 483 | + let mut processed_cols = 0usize; | ||
| 484 | + let found_above = mi_row > 0 | ||
| 485 | + && scan_row( | ||
| 486 | + grid, | ||
| 487 | + mi_row, | ||
| 488 | + mi_col, | ||
| 489 | + bw4, | ||
| 490 | + -1, | ||
| 491 | + max_row_offset, | ||
| 492 | + ref_frame, | ||
| 493 | + &mut candidates, | ||
| 494 | + &mut newmv_count, | ||
| 495 | + &mut processed_rows, | ||
| 496 | + ); | ||
| 497 | + let found_left = mi_col > 0 | ||
| 498 | + && scan_col( | ||
| 499 | + grid, | ||
| 500 | + mi_row, | ||
| 501 | + mi_col, | ||
| 502 | + bh4, | ||
| 503 | + -1, | ||
| 504 | + max_col_offset, | ||
| 505 | + ref_frame, | ||
| 506 | + &mut candidates, | ||
| 507 | + &mut newmv_count, | ||
| 508 | + &mut processed_cols, | ||
| 509 | + ); | ||
| 490 | let found_top_right = scan_top_right( | 510 | let found_top_right = scan_top_right( |
| 491 | grid, | 511 | grid, |
| 492 | mi_row, | 512 | mi_row, |
| @@ -523,32 +543,58 @@ pub fn find_mv_stack( | |||
| 523 | &mut candidates, | 543 | &mut candidates, |
| 524 | &mut dummy_newmv_count, | 544 | &mut dummy_newmv_count, |
| 525 | ); | 545 | ); |
| 526 | - // The extended row/col scan (spec 7.10.2.4's `idx = 2..MVREF_ROW_COLS`): | 546 | + // The extended row/col scan (spec 7.10.2.4's `idx = 2..MVREF_ROW_COLS`, |
| 527 | - // only reachable once the immediate scan's own reach (`processed_rows`/ | 547 | + // libaom's second `scan_row_mbmi`/`scan_col_mbmi` loop): reachable only |
| 528 | - // `processed_cols`) leaves a gap the tile's edge doesn't already clip. | 548 | + // once the immediate scan's own reach (`processed_rows`/`processed_cols`, |
| 529 | - // At this module's fixed 8-mi block geometry with every coded cell | 549 | + // threaded across calls the way libaom's own `int`s are) leaves a gap |
| 530 | - // (intra or inter) now feeding coverage, that reach always hits | 550 | + // `max_row_offset`/`max_col_offset` (the tile edge) doesn't already |
| 551 | + // clip. At this module's fixed 8-mi `tile.rs` geometry with every coded | ||
| 552 | + // cell (intra or inter) feeding coverage, that reach always hits | ||
| 531 | // `MVREF_ROW_COLS`'s farthest offset first -- a true no-op there (see | 553 | // `MVREF_ROW_COLS`'s farthest offset first -- a true no-op there (see |
| 532 | - // module doc and `extended_scan_is_a_no_op_at_8mi_geometry` below). Folds | 554 | + // `extended_scan_is_a_no_op_at_8mi_geometry` below); decode.rs's smaller |
| 533 | - // into the row/col match booleans, never `nearest_match`. | 555 | + // query blocks can leave real gaps this now actually scans. Folds into |
| 534 | - let row_matched_ext = scan_row_extended( | 556 | + // the row/col match booleans, never `nearest_match`; shares the corner |
| 535 | - grid, | 557 | + // probe's `dummy_newmv_count` (libaom's `setup_ref_mv_list` does too). |
| 536 | - mi_row, | 558 | + let mut row_matched_ext = false; |
| 537 | - mi_col, | 559 | + let mut col_matched_ext = false; |
| 538 | - bw4, | 560 | + for idx in 2..=MVREF_ROW_COLS { |
| 539 | - ref_frame, | 561 | + let row_offset = -((idx as isize) * 2) + 1 + isize::from(row_adj); |
| 540 | - &mut candidates, | 562 | + let col_offset = -((idx as isize) * 2) + 1 + isize::from(col_adj); |
| 541 | - processed_rows, | 563 | + if row_offset.unsigned_abs() <= max_row_offset.unsigned_abs() |
| 542 | - ); | 564 | + && row_offset.unsigned_abs() > processed_rows |
| 543 | - let col_matched_ext = scan_col_extended( | 565 | + && scan_row( |
| 544 | - grid, | 566 | + grid, |
| 545 | - mi_row, | 567 | + mi_row, |
| 546 | - mi_col, | 568 | + mi_col, |
| 547 | - bh4, | 569 | + bw4, |
| 548 | - ref_frame, | 570 | + row_offset, |
| 549 | - &mut candidates, | 571 | + max_row_offset, |
| 550 | - processed_cols, | 572 | + ref_frame, |
| 551 | - ); | 573 | + &mut candidates, |
| 574 | + &mut dummy_newmv_count, | ||
| 575 | + &mut processed_rows, | ||
| 576 | + ) | ||
| 577 | + { | ||
| 578 | + row_matched_ext = true; | ||
| 579 | + } | ||
| 580 | + if col_offset.unsigned_abs() <= max_col_offset.unsigned_abs() | ||
| 581 | + && col_offset.unsigned_abs() > processed_cols | ||
| 582 | + && scan_col( | ||
| 583 | + grid, | ||
| 584 | + mi_row, | ||
| 585 | + mi_col, | ||
| 586 | + bh4, | ||
| 587 | + col_offset, | ||
| 588 | + max_col_offset, | ||
| 589 | + ref_frame, | ||
| 590 | + &mut candidates, | ||
| 591 | + &mut dummy_newmv_count, | ||
| 592 | + &mut processed_cols, | ||
| 593 | + ) | ||
| 594 | + { | ||
| 595 | + col_matched_ext = true; | ||
| 596 | + } | ||
| 597 | + } | ||
| 552 | let ref_match_count = usize::from(row_matched || corner_matched || row_matched_ext) | 598 | let ref_match_count = usize::from(row_matched || corner_matched || row_matched_ext) |
| 553 | + usize::from(found_left || col_matched_ext); | 599 | + usize::from(found_left || col_matched_ext); |
| 554 | 600 | ||
| @@ -693,10 +739,12 @@ mod tests { | |||
| 693 | 739 | ||
| 694 | assert_eq!(stack.entries.len(), 1); | 740 | assert_eq!(stack.entries.len(), 1); |
| 695 | assert_eq!(stack.entries[0].mv, mv); | 741 | assert_eq!(stack.entries[0].mv, mv); |
| 696 | - // 2 above cells + 2 left cells + 1 top-right, each weight 2, plus the | 742 | + // 2 above cells + 2 left cells (weight 2 each, `size: 1` neighbours |
| 697 | - // REF_CAT_LEVEL boost av1_find_mv_stack applies to every entry the | 743 | + // below the `bw4 <= n4` weight-boost floor) + 1 top-right (libaom's |
| 698 | - // immediate row/col/top-right scan found (module doc). | 744 | + // real `scan_blk_mbmi` weight, `CORNER_WEIGHT` = 4, not a flat 2), |
| 699 | - assert_eq!(stack.entries[0].weight, 10 + REF_CAT_LEVEL); | 745 | + // plus the REF_CAT_LEVEL boost av1_find_mv_stack applies to every |
| 746 | + // entry the immediate row/col/top-right scan found (module doc). | ||
| 747 | + assert_eq!(stack.entries[0].weight, 12 + REF_CAT_LEVEL); | ||
| 700 | assert_eq!(stack.nearest_mv, mv); | 748 | assert_eq!(stack.nearest_mv, mv); |
| 701 | // Exact ctx (see module doc): both sides matched (nearest_match=2) | 749 | // Exact ctx (see module doc): both sides matched (nearest_match=2) |
| 702 | // and no neighbour was coded NEWMV, which is libaom's | 750 | // and no neighbour was coded NEWMV, which is libaom's |
| @@ -875,6 +923,39 @@ mod tests { | |||
| 875 | assert_eq!(stack.ref_mv_ctx, 1); // nearest_match=0, ref_match_count=1 | 923 | assert_eq!(stack.ref_mv_ctx, 1); // nearest_match=0, ref_match_count=1 |
| 876 | } | 924 | } |
| 877 | 925 | ||
| 926 | + /// The class this round ports: a query block *smaller* than its | ||
| 927 | + /// same-row neighbour (decode.rs's real variable partition sizes, never | ||
| 928 | + /// `tile.rs`'s fixed `bw4 == 8`) gets libaom's real, non-flat weight — | ||
| 929 | + /// hand-computed against `scan_row_mbmi` (mvref_common.c): `bw4 = 2` | ||
| 930 | + /// against an `n4 = 8` neighbour 6 mi rows from the frame's top edge | ||
| 931 | + /// gives `len = min(bw4, n4) = 2`, `inc = min(-max_row_offset + | ||
| 932 | + /// row_offset + 1, n4) = min(6 - 1 + 1, 8) = 6`, `weight = max(2, inc) = | ||
| 933 | + /// 6`, total `len * weight = 12` -- not the flat `IMMEDIATE_WEIGHT = 2` | ||
| 934 | + /// this module used to report for every match regardless of block size. | ||
| 935 | + #[test] | ||
| 936 | + fn a_wider_neighbour_than_the_query_block_gets_libaoms_real_weight() { | ||
| 937 | + let mut grid = MiGrid::new(32, 32); | ||
| 938 | + let (mi_row, mi_col) = (8, 8); | ||
| 939 | + let mv = (4, 4); | ||
| 940 | + grid.set( | ||
| 941 | + mi_row - 1, | ||
| 942 | + mi_col, | ||
| 943 | + MiInfo { | ||
| 944 | + is_inter: true, | ||
| 945 | + ref_frame: 1, | ||
| 946 | + mv, | ||
| 947 | + is_new_mv: false, | ||
| 948 | + size: 8, | ||
| 949 | + }, | ||
| 950 | + ); | ||
| 951 | + | ||
| 952 | + let stack = find_mv_stack(&grid, mi_row, mi_col, 2, 2, 1, 32, 32); | ||
| 953 | + | ||
| 954 | + assert_eq!(stack.entries.len(), 1); | ||
| 955 | + assert_eq!(stack.entries[0].mv, mv); | ||
| 956 | + assert_eq!(stack.entries[0].weight, 12 + REF_CAT_LEVEL); | ||
| 957 | + } | ||
| 958 | + | ||
| 878 | #[test] | 959 | #[test] |
| 879 | fn single_ref_ctx_matches_libaoms_collapsed_forward_only_case() { | 960 | fn single_ref_ctx_matches_libaoms_collapsed_forward_only_case() { |
| 880 | assert_eq!(single_ref_ctx(false), 1); | 961 | assert_eq!(single_ref_ctx(false), 1); |