Mc::predict_scaled + frame_width threaded through decode_inter_block
Browse files at this commitbc2ef3da00f54537069f7a4ba2339040deead135Parent: 849c804- feat
- mc::predict_scaled + frame_width threaded through decode_inter_block
2 changed files +199 -1
- crates/ec-av1/src/decode.rs +26 -0
- crates/ec-av1/src/mc.rs +173 -1
crates/ec-av1/src/decode.rsmodified+26 -0
| @@ -7513,6 +7513,13 @@ fn decode_inter_block( | ||
| 7513 | 7513 | // `frame_is_intra_only`), but it still calls `read_palette_mode_info` |
| 7514 | 7514 | // under the same `av1_allow_palette` gate as the key-frame path. |
| 7515 | 7515 | allow_screen_content_tools: bool, |
| 7516 | + // lane-superres r9: this frame's own coded luma width (spec 7.11.3.3) -- | |
| 7517 | + // compared against each reference's stored picture width to decide | |
| 7518 | + // whether a scaled-MC path (`mc::predict_scaled`) or the ordinary | |
| 7519 | + // stride-1 one (`mc::predict_with_filters`) applies per reference, and to | |
| 7520 | + // refuse (rather than mis-decode) the warp/OBMC/interintra/compound | |
| 7521 | + // combinations that don't implement scaled MC yet. | |
| 7522 | + frame_width: usize, | |
| 7516 | 7523 | ) -> Result<()> { |
| 7517 | 7524 | let (r, c) = at; |
| 7518 | 7525 | let (px, py) = (c * SUB, r * SUB); |
| @@ -10772,6 +10779,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 10772 | 10779 | BLOCK, |
| 10773 | 10780 | BLOCK, |
| 10774 | 10781 | allow_screen_content_tools, |
| 10782 | + frame_width as usize, | |
| 10775 | 10783 | )?; |
| 10776 | 10784 | } |
| 10777 | 10785 | PARTITION_SPLIT => { |
| @@ -10851,6 +10859,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 10851 | 10859 | SUB, |
| 10852 | 10860 | SUB, |
| 10853 | 10861 | allow_screen_content_tools, |
| 10862 | + frame_width as usize, | |
| 10854 | 10863 | )?; |
| 10855 | 10864 | } else { |
| 10856 | 10865 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| @@ -11029,6 +11038,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11029 | 11038 | BLOCK, |
| 11030 | 11039 | SUB, |
| 11031 | 11040 | allow_screen_content_tools, |
| 11041 | + frame_width as usize, | |
| 11032 | 11042 | )?; |
| 11033 | 11043 | if (r32 * 2 + 1) as u32 * SUB_MI < mi_rows { |
| 11034 | 11044 | decode_inter_block( |
| @@ -11082,6 +11092,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11082 | 11092 | SUB, |
| 11083 | 11093 | SUB, |
| 11084 | 11094 | allow_screen_content_tools, |
| 11095 | + frame_width as usize, | |
| 11085 | 11096 | )?; |
| 11086 | 11097 | if (c32 * 2 + 1) as u32 * SUB_MI < mi_cols { |
| 11087 | 11098 | decode_inter_block( |
| @@ -11135,6 +11146,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11135 | 11146 | SUB, |
| 11136 | 11147 | SUB, |
| 11137 | 11148 | allow_screen_content_tools, |
| 11149 | + frame_width as usize, | |
| 11138 | 11150 | )?; |
| 11139 | 11151 | } |
| 11140 | 11152 | } |
| @@ -11194,6 +11206,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11194 | 11206 | BLOCK, |
| 11195 | 11207 | SUB, |
| 11196 | 11208 | allow_screen_content_tools, |
| 11209 | + frame_width as usize, | |
| 11197 | 11210 | )?; |
| 11198 | 11211 | decode_inter_block( |
| 11199 | 11212 | &mut dec, |
| @@ -11242,6 +11255,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11242 | 11255 | BLOCK, |
| 11243 | 11256 | SUB, |
| 11244 | 11257 | allow_screen_content_tools, |
| 11258 | + frame_width as usize, | |
| 11245 | 11259 | )?; |
| 11246 | 11260 | } |
| 11247 | 11261 | PARTITION_VERT => { |
| @@ -11295,6 +11309,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11295 | 11309 | SUB, |
| 11296 | 11310 | BLOCK, |
| 11297 | 11311 | allow_screen_content_tools, |
| 11312 | + frame_width as usize, | |
| 11298 | 11313 | )?; |
| 11299 | 11314 | decode_inter_block( |
| 11300 | 11315 | &mut dec, |
| @@ -11343,6 +11358,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11343 | 11358 | SUB, |
| 11344 | 11359 | BLOCK, |
| 11345 | 11360 | allow_screen_content_tools, |
| 11361 | + frame_width as usize, | |
| 11346 | 11362 | )?; |
| 11347 | 11363 | } |
| 11348 | 11364 | PARTITION_HORZ_A => { |
| @@ -11401,6 +11417,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11401 | 11417 | SUB, |
| 11402 | 11418 | SUB, |
| 11403 | 11419 | allow_screen_content_tools, |
| 11420 | + frame_width as usize, | |
| 11404 | 11421 | )?; |
| 11405 | 11422 | decode_inter_block( |
| 11406 | 11423 | &mut dec, |
| @@ -11453,6 +11470,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11453 | 11470 | SUB, |
| 11454 | 11471 | SUB, |
| 11455 | 11472 | allow_screen_content_tools, |
| 11473 | + frame_width as usize, | |
| 11456 | 11474 | )?; |
| 11457 | 11475 | decode_inter_block( |
| 11458 | 11476 | &mut dec, |
| @@ -11501,6 +11519,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11501 | 11519 | BLOCK, |
| 11502 | 11520 | SUB, |
| 11503 | 11521 | allow_screen_content_tools, |
| 11522 | + frame_width as usize, | |
| 11504 | 11523 | )?; |
| 11505 | 11524 | } |
| 11506 | 11525 | PARTITION_VERT_A => { |
| @@ -11558,6 +11577,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11558 | 11577 | SUB, |
| 11559 | 11578 | SUB, |
| 11560 | 11579 | allow_screen_content_tools, |
| 11580 | + frame_width as usize, | |
| 11561 | 11581 | )?; |
| 11562 | 11582 | decode_inter_block( |
| 11563 | 11583 | &mut dec, |
| @@ -11610,6 +11630,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11610 | 11630 | SUB, |
| 11611 | 11631 | SUB, |
| 11612 | 11632 | allow_screen_content_tools, |
| 11633 | + frame_width as usize, | |
| 11613 | 11634 | )?; |
| 11614 | 11635 | decode_inter_block( |
| 11615 | 11636 | &mut dec, |
| @@ -11658,6 +11679,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11658 | 11679 | SUB, |
| 11659 | 11680 | BLOCK, |
| 11660 | 11681 | allow_screen_content_tools, |
| 11682 | + frame_width as usize, | |
| 11661 | 11683 | )?; |
| 11662 | 11684 | } |
| 11663 | 11685 | PARTITION_VERT_B => { |
| @@ -11712,6 +11734,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11712 | 11734 | SUB, |
| 11713 | 11735 | BLOCK, |
| 11714 | 11736 | allow_screen_content_tools, |
| 11737 | + frame_width as usize, | |
| 11715 | 11738 | )?; |
| 11716 | 11739 | decode_inter_block( |
| 11717 | 11740 | &mut dec, |
| @@ -11764,6 +11787,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11764 | 11787 | SUB, |
| 11765 | 11788 | SUB, |
| 11766 | 11789 | allow_screen_content_tools, |
| 11790 | + frame_width as usize, | |
| 11767 | 11791 | )?; |
| 11768 | 11792 | decode_inter_block( |
| 11769 | 11793 | &mut dec, |
| @@ -11816,6 +11840,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 11816 | 11840 | SUB, |
| 11817 | 11841 | SUB, |
| 11818 | 11842 | allow_screen_content_tools, |
| 11843 | + frame_width as usize, | |
| 11819 | 11844 | )?; |
| 11820 | 11845 | } |
| 11821 | 11846 | _ => { |
| @@ -12669,6 +12694,7 @@ mod tests { | ||
| 12669 | 12694 | side, |
| 12670 | 12695 | side, |
| 12671 | 12696 | false, |
| 12697 | + width, | |
| 12672 | 12698 | ) |
| 12673 | 12699 | .unwrap(); |
| 12674 | 12700 |
crates/ec-av1/src/mc.rsmodified+173 -1
| @@ -411,6 +411,125 @@ pub fn predict_with_filters( | ||
| 411 | 411 | crate::encode::stage_add(1, stage_t.elapsed()); |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | +/// `REF_SCALE_SHIFT`'s "no scaling" value (spec 7.11.3.3, libaom `scale.h` | |
| 415 | +/// `REF_NO_SCALE`): `x_scale_fp == REF_NO_SCALE` means the stored reference's | |
| 416 | +/// luma width equals the current frame's, so [`predict_scaled`] reduces | |
| 417 | +/// exactly to [`predict_with_filters`] (verified in | |
| 418 | +/// `predict_scaled_at_no_scale_matches_predict_with_filters` below). | |
| 419 | +pub const REF_NO_SCALE: i64 = 1 << 14; | |
| 420 | + | |
| 421 | +/// The horizontal scale ratio spec 7.11.3.3 derives from luma widths only | |
| 422 | +/// (libaom `av1_setup_scale_factors_for_frame`, luma widths, `REF_SCALE_SHIFT | |
| 423 | +/// == 14`): `other_width` is the stored reference picture's own luma width, | |
| 424 | +/// `this_width` the current frame's coded luma width. AV1 superres never | |
| 425 | +/// scales height (r8's derivation), so there is no vertical counterpart. | |
| 426 | +pub fn scale_factor(other_width: usize, this_width: usize) -> i64 { | |
| 427 | + (((other_width as i64) << 14) + this_width as i64 / 2) / this_width as i64 | |
| 428 | +} | |
| 429 | + | |
| 430 | +fn round_pow2_64(value: i64, shift: u32) -> i64 { | |
| 431 | + if shift == 0 { | |
| 432 | + value | |
| 433 | + } else { | |
| 434 | + (value + (1i64 << (shift - 1))) >> shift | |
| 435 | + } | |
| 436 | +} | |
| 437 | + | |
| 438 | +/// `ROUND_POWER_OF_TWO_SIGNED` (libaom `common/common.h`): halves round away | |
| 439 | +/// from zero's *magnitude* -- negative inputs are rounded like their | |
| 440 | +/// negation, not truncated toward zero the way a plain arithmetic shift | |
| 441 | +/// would. | |
| 442 | +fn round_pow2_signed_64(value: i64, shift: u32) -> i64 { | |
| 443 | + if value < 0 { | |
| 444 | + -round_pow2_64(-value, shift) | |
| 445 | + } else { | |
| 446 | + round_pow2_64(value, shift) | |
| 447 | + } | |
| 448 | +} | |
| 449 | + | |
| 450 | +/// [`predict_with_filters`]'s scaled-reference counterpart (spec 7.11.3.3): | |
| 451 | +/// used only when the stored reference's luma width differs from the current | |
| 452 | +/// frame's (`use_superres` on a non-key frame). `x_scale_fp` is | |
| 453 | +/// [`scale_factor`]'s ratio ([`REF_NO_SCALE`] reproduces | |
| 454 | +/// `predict_with_filters` bit-exact, pinned by | |
| 455 | +/// `predict_scaled_at_no_scale_matches_predict_with_filters`). Per r8's | |
| 456 | +/// derivation AV1 superres never scales height, so `y_q4`'s whole-sample / | |
| 457 | +/// fraction split and the vertical pass are untouched -- only the horizontal | |
| 458 | +/// pass's per-column integer position and filter phase come from a scaled | |
| 459 | +/// walk (`x_qn`) instead of a fixed stride-1 one. | |
| 460 | +/// | |
| 461 | +/// # Panics | |
| 462 | +/// Panics when `dst` is not `block_w * block_h` long, or the reference is | |
| 463 | +/// empty. | |
| 464 | +#[allow(clippy::too_many_arguments)] | |
| 465 | +pub fn predict_scaled( | |
| 466 | + reference: &[u8], | |
| 467 | + stride: usize, | |
| 468 | + true_width: usize, | |
| 469 | + true_height: usize, | |
| 470 | + x_q4: i32, | |
| 471 | + y_q4: i32, | |
| 472 | + x_scale_fp: i64, | |
| 473 | + block_w: usize, | |
| 474 | + block_h: usize, | |
| 475 | + h_kind: InterpFilterKind, | |
| 476 | + v_kind: InterpFilterKind, | |
| 477 | + dst: &mut [u8], | |
| 478 | +) { | |
| 479 | + assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); | |
| 480 | + assert!(!reference.is_empty(), "a reference plane has samples"); | |
| 481 | + | |
| 482 | + let y0 = y_q4.div_euclid(16); | |
| 483 | + let yfrac = y_q4.rem_euclid(16) as usize; | |
| 484 | + | |
| 485 | + // SCALE_SUBPEL_BITS == 10: x_step_qn is x_scale_fp (Q14) rounded down to | |
| 486 | + // Q10; off/pos_x_q10 fold the block's own x_q4 (Q4) into that same Q10 | |
| 487 | + // grid (spec 7.11.3.3's `dec_calc_subpel_params`). | |
| 488 | + let x_step_qn = round_pow2_64(x_scale_fp, 4); | |
| 489 | + let off = (x_scale_fp - REF_NO_SCALE) * 8; | |
| 490 | + let pos_x_q10 = round_pow2_signed_64(x_q4 as i64 * x_scale_fp + off, 8) + 32; | |
| 491 | + | |
| 492 | + let (h_wide, h_narrow) = h_kind.tables(); | |
| 493 | + let (v_wide, v_narrow) = v_kind.tables(); | |
| 494 | + let v_filter = if block_h <= 4 { | |
| 495 | + &v_narrow[yfrac] | |
| 496 | + } else { | |
| 497 | + &v_wide[yfrac] | |
| 498 | + }; | |
| 499 | + | |
| 500 | + let rows = block_h + 7; | |
| 501 | + let mut intermediate = vec![0i32; rows * block_w]; | |
| 502 | + for c in 0..block_w { | |
| 503 | + let x_qn = pos_x_q10 + c as i64 * x_step_qn; | |
| 504 | + let int_pel = (x_qn >> 10) as i32; | |
| 505 | + let filter_idx = ((x_qn & 1023) >> 6) as usize; | |
| 506 | + let h_filter = if block_w <= 4 { | |
| 507 | + &h_narrow[filter_idx] | |
| 508 | + } else { | |
| 509 | + &h_wide[filter_idx] | |
| 510 | + }; | |
| 511 | + for r in 0..rows { | |
| 512 | + let y = y0 - 3 + r as i32; | |
| 513 | + let mut sum = 0; | |
| 514 | + for (t, &tap) in h_filter.iter().enumerate() { | |
| 515 | + let x = int_pel + t as i32 - 3; | |
| 516 | + sum += tap * sample(reference, stride, true_width, true_height, x, y); | |
| 517 | + } | |
| 518 | + intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0); | |
| 519 | + } | |
| 520 | + } | |
| 521 | + | |
| 522 | + for row in 0..block_h { | |
| 523 | + for col in 0..block_w { | |
| 524 | + let mut sum = 0; | |
| 525 | + for (t, &tap) in v_filter.iter().enumerate() { | |
| 526 | + sum += tap * intermediate[(row + t) * block_w + col]; | |
| 527 | + } | |
| 528 | + dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, 255) as u8; | |
| 529 | + } | |
| 530 | + } | |
| 531 | +} | |
| 532 | + | |
| 414 | 533 | /// `InterRound1` for a compound ref (spec 7.11.3.2's `isCompound` branch, |
| 415 | 534 | /// `COMPOUND_ROUND1_BITS` in libaom's `convolve.h`): 4 bits shallower than |
| 416 | 535 | /// [`INTER_ROUND_1`] so the vertical pass lands in the `CONV_BUF` domain |
| @@ -625,7 +744,10 @@ fn compound_intermediate_whole_pel_identity_round_trips_through_combine() { | ||
| 625 | 744 | |
| 626 | 745 | #[cfg(test)] |
| 627 | 746 | mod tests { |
| 628 | - use super::{InterpFilterKind, predict, predict_with_filter}; | |
| 747 | + use super::{ | |
| 748 | + InterpFilterKind, REF_NO_SCALE, predict, predict_scaled, predict_with_filter, | |
| 749 | + predict_with_filters, | |
| 750 | + }; | |
| 629 | 751 | |
| 630 | 752 | /// Regression pin for the lane-av1flake ±1 defect: a real aomenc chroma |
| 631 | 753 | /// inter block at the frame's top-left corner (mv q4 = (row 7, col 15), |
| @@ -904,4 +1026,54 @@ mod tests { | ||
| 904 | 1026 | "an MV past the bottom-right edge must clamp to the corner sample, got {dst2:?}" |
| 905 | 1027 | ); |
| 906 | 1028 | } |
| 1029 | + | |
| 1030 | + #[test] | |
| 1031 | + fn predict_scaled_at_no_scale_matches_predict_with_filters() { | |
| 1032 | + // Algebraic pin from the r8/r9 derivation: x_scale_fp == REF_NO_SCALE | |
| 1033 | + // must reduce predict_scaled's per-column scaled walk to the exact | |
| 1034 | + // same int_pel/filter_idx sequence predict_with_filters computes from | |
| 1035 | + // a fixed stride-1 walk -- every subpel fraction, every block width. | |
| 1036 | + let width = 24; | |
| 1037 | + let height = 24; | |
| 1038 | + let reference: Vec<u8> = (0..width * height).map(|i| (i * 7 % 251) as u8).collect(); | |
| 1039 | + for block_w in [4usize, 8, 16] { | |
| 1040 | + for x_frac in 0..16i32 { | |
| 1041 | + let x_q4 = 3 * 16 + x_frac; | |
| 1042 | + let y_q4 = 2 * 16 + 5; | |
| 1043 | + let mut expected = vec![0u8; block_w * 8]; | |
| 1044 | + predict_with_filters( | |
| 1045 | + &reference, | |
| 1046 | + width, | |
| 1047 | + width, | |
| 1048 | + height, | |
| 1049 | + x_q4, | |
| 1050 | + y_q4, | |
| 1051 | + block_w, | |
| 1052 | + 8, | |
| 1053 | + InterpFilterKind::Regular, | |
| 1054 | + InterpFilterKind::Regular, | |
| 1055 | + &mut expected, | |
| 1056 | + ); | |
| 1057 | + let mut got = vec![0u8; block_w * 8]; | |
| 1058 | + predict_scaled( | |
| 1059 | + &reference, | |
| 1060 | + width, | |
| 1061 | + width, | |
| 1062 | + height, | |
| 1063 | + x_q4, | |
| 1064 | + y_q4, | |
| 1065 | + REF_NO_SCALE, | |
| 1066 | + block_w, | |
| 1067 | + 8, | |
| 1068 | + InterpFilterKind::Regular, | |
| 1069 | + InterpFilterKind::Regular, | |
| 1070 | + &mut got, | |
| 1071 | + ); | |
| 1072 | + assert_eq!( | |
| 1073 | + got, expected, | |
| 1074 | + "block_w={block_w} x_frac={x_frac}: REF_NO_SCALE must reproduce predict_with_filters exactly" | |
| 1075 | + ); | |
| 1076 | + } | |
| 1077 | + } | |
| 1078 | + } | |
| 907 | 1079 | } |