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 | // `frame_is_intra_only`), but it still calls `read_palette_mode_info` | 7513 | // `frame_is_intra_only`), but it still calls `read_palette_mode_info` |
| 7514 | // under the same `av1_allow_palette` gate as the key-frame path. | 7514 | // under the same `av1_allow_palette` gate as the key-frame path. |
| 7515 | allow_screen_content_tools: bool, | 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 | ) -> Result<()> { | 7523 | ) -> Result<()> { |
| 7517 | let (r, c) = at; | 7524 | let (r, c) = at; |
| 7518 | let (px, py) = (c * SUB, r * SUB); | 7525 | let (px, py) = (c * SUB, r * SUB); |
| @@ -10772,6 +10779,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 10772 | BLOCK, | 10779 | BLOCK, |
| 10773 | BLOCK, | 10780 | BLOCK, |
| 10774 | allow_screen_content_tools, | 10781 | allow_screen_content_tools, |
| 10782 | + frame_width as usize, | ||
| 10775 | )?; | 10783 | )?; |
| 10776 | } | 10784 | } |
| 10777 | PARTITION_SPLIT => { | 10785 | PARTITION_SPLIT => { |
| @@ -10851,6 +10859,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 10851 | SUB, | 10859 | SUB, |
| 10852 | SUB, | 10860 | SUB, |
| 10853 | allow_screen_content_tools, | 10861 | allow_screen_content_tools, |
| 10862 | + frame_width as usize, | ||
| 10854 | )?; | 10863 | )?; |
| 10855 | } else { | 10864 | } else { |
| 10856 | let ctx16 = neighbours.partition_ctx(at16, SUB); | 10865 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| @@ -11029,6 +11038,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11029 | BLOCK, | 11038 | BLOCK, |
| 11030 | SUB, | 11039 | SUB, |
| 11031 | allow_screen_content_tools, | 11040 | allow_screen_content_tools, |
| 11041 | + frame_width as usize, | ||
| 11032 | )?; | 11042 | )?; |
| 11033 | if (r32 * 2 + 1) as u32 * SUB_MI < mi_rows { | 11043 | if (r32 * 2 + 1) as u32 * SUB_MI < mi_rows { |
| 11034 | decode_inter_block( | 11044 | decode_inter_block( |
| @@ -11082,6 +11092,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11082 | SUB, | 11092 | SUB, |
| 11083 | SUB, | 11093 | SUB, |
| 11084 | allow_screen_content_tools, | 11094 | allow_screen_content_tools, |
| 11095 | + frame_width as usize, | ||
| 11085 | )?; | 11096 | )?; |
| 11086 | if (c32 * 2 + 1) as u32 * SUB_MI < mi_cols { | 11097 | if (c32 * 2 + 1) as u32 * SUB_MI < mi_cols { |
| 11087 | decode_inter_block( | 11098 | decode_inter_block( |
| @@ -11135,6 +11146,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11135 | SUB, | 11146 | SUB, |
| 11136 | SUB, | 11147 | SUB, |
| 11137 | allow_screen_content_tools, | 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 | BLOCK, | 11206 | BLOCK, |
| 11195 | SUB, | 11207 | SUB, |
| 11196 | allow_screen_content_tools, | 11208 | allow_screen_content_tools, |
| 11209 | + frame_width as usize, | ||
| 11197 | )?; | 11210 | )?; |
| 11198 | decode_inter_block( | 11211 | decode_inter_block( |
| 11199 | &mut dec, | 11212 | &mut dec, |
| @@ -11242,6 +11255,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11242 | BLOCK, | 11255 | BLOCK, |
| 11243 | SUB, | 11256 | SUB, |
| 11244 | allow_screen_content_tools, | 11257 | allow_screen_content_tools, |
| 11258 | + frame_width as usize, | ||
| 11245 | )?; | 11259 | )?; |
| 11246 | } | 11260 | } |
| 11247 | PARTITION_VERT => { | 11261 | PARTITION_VERT => { |
| @@ -11295,6 +11309,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11295 | SUB, | 11309 | SUB, |
| 11296 | BLOCK, | 11310 | BLOCK, |
| 11297 | allow_screen_content_tools, | 11311 | allow_screen_content_tools, |
| 11312 | + frame_width as usize, | ||
| 11298 | )?; | 11313 | )?; |
| 11299 | decode_inter_block( | 11314 | decode_inter_block( |
| 11300 | &mut dec, | 11315 | &mut dec, |
| @@ -11343,6 +11358,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11343 | SUB, | 11358 | SUB, |
| 11344 | BLOCK, | 11359 | BLOCK, |
| 11345 | allow_screen_content_tools, | 11360 | allow_screen_content_tools, |
| 11361 | + frame_width as usize, | ||
| 11346 | )?; | 11362 | )?; |
| 11347 | } | 11363 | } |
| 11348 | PARTITION_HORZ_A => { | 11364 | PARTITION_HORZ_A => { |
| @@ -11401,6 +11417,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11401 | SUB, | 11417 | SUB, |
| 11402 | SUB, | 11418 | SUB, |
| 11403 | allow_screen_content_tools, | 11419 | allow_screen_content_tools, |
| 11420 | + frame_width as usize, | ||
| 11404 | )?; | 11421 | )?; |
| 11405 | decode_inter_block( | 11422 | decode_inter_block( |
| 11406 | &mut dec, | 11423 | &mut dec, |
| @@ -11453,6 +11470,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11453 | SUB, | 11470 | SUB, |
| 11454 | SUB, | 11471 | SUB, |
| 11455 | allow_screen_content_tools, | 11472 | allow_screen_content_tools, |
| 11473 | + frame_width as usize, | ||
| 11456 | )?; | 11474 | )?; |
| 11457 | decode_inter_block( | 11475 | decode_inter_block( |
| 11458 | &mut dec, | 11476 | &mut dec, |
| @@ -11501,6 +11519,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11501 | BLOCK, | 11519 | BLOCK, |
| 11502 | SUB, | 11520 | SUB, |
| 11503 | allow_screen_content_tools, | 11521 | allow_screen_content_tools, |
| 11522 | + frame_width as usize, | ||
| 11504 | )?; | 11523 | )?; |
| 11505 | } | 11524 | } |
| 11506 | PARTITION_VERT_A => { | 11525 | PARTITION_VERT_A => { |
| @@ -11558,6 +11577,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11558 | SUB, | 11577 | SUB, |
| 11559 | SUB, | 11578 | SUB, |
| 11560 | allow_screen_content_tools, | 11579 | allow_screen_content_tools, |
| 11580 | + frame_width as usize, | ||
| 11561 | )?; | 11581 | )?; |
| 11562 | decode_inter_block( | 11582 | decode_inter_block( |
| 11563 | &mut dec, | 11583 | &mut dec, |
| @@ -11610,6 +11630,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11610 | SUB, | 11630 | SUB, |
| 11611 | SUB, | 11631 | SUB, |
| 11612 | allow_screen_content_tools, | 11632 | allow_screen_content_tools, |
| 11633 | + frame_width as usize, | ||
| 11613 | )?; | 11634 | )?; |
| 11614 | decode_inter_block( | 11635 | decode_inter_block( |
| 11615 | &mut dec, | 11636 | &mut dec, |
| @@ -11658,6 +11679,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11658 | SUB, | 11679 | SUB, |
| 11659 | BLOCK, | 11680 | BLOCK, |
| 11660 | allow_screen_content_tools, | 11681 | allow_screen_content_tools, |
| 11682 | + frame_width as usize, | ||
| 11661 | )?; | 11683 | )?; |
| 11662 | } | 11684 | } |
| 11663 | PARTITION_VERT_B => { | 11685 | PARTITION_VERT_B => { |
| @@ -11712,6 +11734,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11712 | SUB, | 11734 | SUB, |
| 11713 | BLOCK, | 11735 | BLOCK, |
| 11714 | allow_screen_content_tools, | 11736 | allow_screen_content_tools, |
| 11737 | + frame_width as usize, | ||
| 11715 | )?; | 11738 | )?; |
| 11716 | decode_inter_block( | 11739 | decode_inter_block( |
| 11717 | &mut dec, | 11740 | &mut dec, |
| @@ -11764,6 +11787,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11764 | SUB, | 11787 | SUB, |
| 11765 | SUB, | 11788 | SUB, |
| 11766 | allow_screen_content_tools, | 11789 | allow_screen_content_tools, |
| 11790 | + frame_width as usize, | ||
| 11767 | )?; | 11791 | )?; |
| 11768 | decode_inter_block( | 11792 | decode_inter_block( |
| 11769 | &mut dec, | 11793 | &mut dec, |
| @@ -11816,6 +11840,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 11816 | SUB, | 11840 | SUB, |
| 11817 | SUB, | 11841 | SUB, |
| 11818 | allow_screen_content_tools, | 11842 | allow_screen_content_tools, |
| 11843 | + frame_width as usize, | ||
| 11819 | )?; | 11844 | )?; |
| 11820 | } | 11845 | } |
| 11821 | _ => { | 11846 | _ => { |
| @@ -12669,6 +12694,7 @@ mod tests { | |||
| 12669 | side, | 12694 | side, |
| 12670 | side, | 12695 | side, |
| 12671 | false, | 12696 | false, |
| 12697 | + width, | ||
| 12672 | ) | 12698 | ) |
| 12673 | .unwrap(); | 12699 | .unwrap(); |
| 12674 | 12700 | ||
crates/ec-av1/src/mc.rsmodified+173 -1
| @@ -411,6 +411,125 @@ pub fn predict_with_filters( | |||
| 411 | crate::encode::stage_add(1, stage_t.elapsed()); | 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 | /// `InterRound1` for a compound ref (spec 7.11.3.2's `isCompound` branch, | 533 | /// `InterRound1` for a compound ref (spec 7.11.3.2's `isCompound` branch, |
| 415 | /// `COMPOUND_ROUND1_BITS` in libaom's `convolve.h`): 4 bits shallower than | 534 | /// `COMPOUND_ROUND1_BITS` in libaom's `convolve.h`): 4 bits shallower than |
| 416 | /// [`INTER_ROUND_1`] so the vertical pass lands in the `CONV_BUF` domain | 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 | #[cfg(test)] | 745 | #[cfg(test)] |
| 627 | mod tests { | 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 | /// Regression pin for the lane-av1flake ±1 defect: a real aomenc chroma | 752 | /// Regression pin for the lane-av1flake ±1 defect: a real aomenc chroma |
| 631 | /// inter block at the frame's top-left corner (mv q4 = (row 7, col 15), | 753 | /// inter block at the frame's top-left corner (mv q4 = (row 7, col 15), |
| @@ -904,4 +1026,54 @@ mod tests { | |||
| 904 | "an MV past the bottom-right edge must clamp to the corner sample, got {dst2:?}" | 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 | } |