forge
Explore
Log in

Don't you dare go Hollow

The mv-stack frame-edge clamp window is signed -- a block overhanging the bottom/right edge was never clamped

Browse files at this commit

3bd289466b3e922fab83b5d24552384377917ef8Ahmet Kaan GümüşParent: 173b937
  • fix
    • the mv-stack frame-edge clamp window is signed -- a block overhanging the bottom/right edge was never clamped
Show full message
fix(av1): the mv-stack frame-edge clamp window is signed -- a block overhanging the bottom/right edge was never clamped

libaom's `mb_to_right_edge` / `mb_to_bottom_edge` (`set_mi_row_col`,
av1_common_int.h) are signed: a block that overhangs the frame's right or
bottom edge keeps its full `bw4`/`bh4`, so `mi_cols - bw4 - mi_col` and
`mi_rows - bh4 - mi_row` go NEGATIVE and `clamp_mv_ref` (mvref_common.h)
narrows the predictor window by exactly that much. Our port computed both
terms with `usize::saturating_sub`, which clipped them to 0 and left the
window up to two block sizes too wide, so an edge block's stack entry was
never clamped.

On a 10-bit 3840x1608 stream (`mi_rows == 402`) every bottom-row 64x64 block
overhangs by 14 mi units: the 64x64 NEWMV block at mi(400,368) of decode
frame 88 took `mv0=(224,568)` where libaom takes `(32,568)` -- the entropy
stream is identical (same range at EC_MODE and EC_MODE_MV), only the
reconstructed value differed -- and its MC then read the reference's
replicated last row instead of real rows 1604..1606. That frame was the only
non-exact one in the 100-frame 4K head prefix (221 bytes); it is now exact.

New counter `mvstack::mv_clamp_edge_overhang_hits` (also printed by
decode_probe) counts stack entries the negative-edge window actually moves.

crates/ec-av1/fixtures/reelb_head_mvclamp_witness.obu (72158 bytes, sha256
451a9b138995e636f22308847a123b5c8621d4908cd93772cf5bbf2b37df39d7, truncated
twice to identical hashes) is the smallest prefix of that head firing it
(N=79 -> 0, N=80 -> 5); new gate
`a_10bit_film_frames_with_frame_edge_mv_clamping_decode_pixel_exact` decodes
its 52 shown frames pixel-exact against ffmpeg on every plane and hard-asserts
the counter. `clamp_mv_ref_window_is_signed_at_the_frame_bottom_edge` pins the
ladder's exact numbers.
crates/ec-av1/examples/decode_probe.rsmodified+4 -0
@@ -46,6 +46,10 @@ fn main() {
46 println!("interintra_rect: {}", ec_av1::decode::interintra_rect_hits());46 println!("interintra_rect: {}", ec_av1::decode::interintra_rect_hits());
47 println!("gm_nontrans_small_side: {}", ec_av1::decode::gm_nontrans_small_side_hits());47 println!("gm_nontrans_small_side: {}", ec_av1::decode::gm_nontrans_small_side_hits());
48 println!("tr_reach_longer_side: {}", ec_av1::decode::tr_reach_longer_side_hits());48 println!("tr_reach_longer_side: {}", ec_av1::decode::tr_reach_longer_side_hits());
49+ println!(
50+ "mv_clamp_edge_overhang: {}",
51+ ec_av1::mvstack::mv_clamp_edge_overhang_hits()
52+ );
49 let (h84, h48) = ec_av1::decode::sub8_inter_rect_hits();53 let (h84, h48) = ec_av1::decode::sub8_inter_rect_hits();
50 println!("sub8_inter_rect: horz8x4={h84} vert4x8={h48}");54 println!("sub8_inter_rect: horz8x4={h84} vert4x8={h48}");
51 let si = ec_av1::stream::sub8_intra_rect_hits();55 let si = ec_av1::stream::sub8_intra_rect_hits();
crates/ec-av1/fixtures/reelb_head_mvclamp_witness.obuadded+0 -0

Binary file changed

crates/ec-av1/src/mvstack.rsmodified+49 -2
@@ -804,6 +804,19 @@ const MV_BORDER: i32 = 16 << 3;
804 /// a `mi_cols`-by-`mi_rows` frame: the distance from the block's own edge to804 /// a `mi_cols`-by-`mi_rows` frame: the distance from the block's own edge to
805 /// the matching frame edge, plus the block's own span and [`MV_BORDER`] of805 /// the matching frame edge, plus the block's own span and [`MV_BORDER`] of
806 /// slack, in every direction.806 /// slack, in every direction.
807+// lane-t900 r11: stack entries whose clamp window is narrowed by a NEGATIVE
808+// `mb_to_right_edge`/`mb_to_bottom_edge` -- i.e. blocks overhanging the frame's
809+// right/bottom edge whose predictor the frame-edge clamp actually moves. Zero
810+// of these before the r11 fix, because the window was computed in `usize`.
811+thread_local! {
812+ static MV_CLAMP_EDGE_OVERHANG_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
813+}
814+
815+/// Current value of [`MV_CLAMP_EDGE_OVERHANG_HITS`].
816+pub fn mv_clamp_edge_overhang_hits() -> usize {
817+ MV_CLAMP_EDGE_OVERHANG_HITS.with(std::cell::Cell::get)
818+}
819+
807 fn clamp_mv_ref(820 fn clamp_mv_ref(
808 mv: (i32, i32),821 mv: (i32, i32),
809 mi_row: usize,822 mi_row: usize,
@@ -817,9 +830,24 @@ fn clamp_mv_ref(
817 let to_subpel = |mi_units: usize| (mi_units as i32) * 4 * 8;830 let to_subpel = |mi_units: usize| (mi_units as i32) * 4 * 8;
818 let (bw8, bh8) = (to_subpel(bw4), to_subpel(bh4));831 let (bw8, bh8) = (to_subpel(bw4), to_subpel(bh4));
819 let to_left = -to_subpel(mi_col);832 let to_left = -to_subpel(mi_col);
820- let to_right = to_subpel(mi_cols.saturating_sub(bw4).saturating_sub(mi_col));833+ // lane-t900 r11: libaom's `mb_to_right_edge`/`mb_to_bottom_edge`
834+ // (`set_mi_row_col`, av1_common_int.h) are SIGNED and go NEGATIVE for a
835+ // block that overhangs the frame's right/bottom edge -- the block keeps
836+ // its full `bw4`/`bh4` there, so `mi_cols - bw4 - mi_col < 0`. Saturating
837+ // in `usize` clipped that to 0 and left the clamp window ~2 blocks too
838+ // wide, so an edge block's stack entry was never clamped.
839+ let signed = |a: usize, b: usize, c: usize| (a as i32 - b as i32 - c as i32) * 4 * 8;
840+ let to_right = signed(mi_cols, bw4, mi_col);
821 let to_top = -to_subpel(mi_row);841 let to_top = -to_subpel(mi_row);
822- let to_bottom = to_subpel(mi_rows.saturating_sub(bh4).saturating_sub(mi_row));842+ let to_bottom = signed(mi_rows, bh4, mi_row);
843+ if to_right < 0 || to_bottom < 0 {
844+ let (row, col) = mv;
845+ let clamped_row = row.clamp(to_top - bh8 - MV_BORDER, to_bottom + bh8 + MV_BORDER);
846+ let clamped_col = col.clamp(to_left - bw8 - MV_BORDER, to_right + bw8 + MV_BORDER);
847+ if clamped_row != row || clamped_col != col {
848+ MV_CLAMP_EDGE_OVERHANG_HITS.with(|c| c.set(c.get() + 1));
849+ }
850+ }
823 851
824 let (row, col) = mv;852 let (row, col) = mv;
825 (853 (
@@ -3182,4 +3210,23 @@ mod tests {
3182 assert_eq!(whole_frame, REF_CAT_LEVEL + 2 * 4 + CORNER_WEIGHT);3210 assert_eq!(whole_frame, REF_CAT_LEVEL + 2 * 4 + CORNER_WEIGHT);
3183 assert_ne!(tiled, whole_frame);3211 assert_ne!(tiled, whole_frame);
3184 }3212 }
3213+
3214+ /// lane-t900 r11 regression: the frame-edge clamp window is SIGNED.
3215+ ///
3216+ /// Numbers taken straight off the cross-decoder ladder on a 10-bit
3217+ /// 3840x1608 stream (`mi_cols=960`, `mi_rows=402`): the 64x64 NEWMV block
3218+ /// at mi(400,368) overhangs the bottom edge by 14 mi units, so libaom's
3219+ /// `mb_to_bottom_edge = (402 - 16 - 400) * 4 * 8 = -448` and the stack
3220+ /// entry's row is clamped to `-448 + 512 + 128 = 192`. Computing that in
3221+ /// `usize` saturated the term to 0 and let a row of 384 through, which is
3222+ /// 24 px of extra downward motion -- the block's MC then read the
3223+ /// replicated last row of the reference.
3224+ #[test]
3225+ fn clamp_mv_ref_window_is_signed_at_the_frame_bottom_edge() {
3226+ let clamped = clamp_mv_ref((384, -320), 400, 368, 16, 16, 960, 402);
3227+ assert_eq!(clamped, (192, -320));
3228+ // A block fully inside the frame keeps the wide window.
3229+ assert_eq!(clamp_mv_ref((384, -320), 100, 368, 16, 16, 960, 402), (384, -320));
3230+ }
3231+
3185 }3232 }
crates/ec-av1/src/stream.rsmodified+65 -0
@@ -30615,6 +30615,71 @@ mod tests {
30615 }30615 }
30616 30616
30617 30617
30618+ /// lane-t900 r11: the frame-edge MV clamp witness.
30619+ ///
30620+ /// `crates/ec-av1/fixtures/reelb_head_mvclamp_witness.obu` (72158 bytes,
30621+ /// sha256 451a9b138995e636f22308847a123b5c8621d4908cd93772cf5bbf2b37df39d7,
30622+ /// truncated twice independently to identical hashes) is the first 80
30623+ /// frame-carrying OBUs of a 10-bit 3840x1608 HDR film head -- the SMALLEST
30624+ /// such prefix that fires the clamp (N=79 fires 0, N=80 fires 5). 55
30625+ /// decode-order frames, 52 shown, every decode-order frame byte-identical
30626+ /// to instrumented aomdec (`EC_AV1_FINAL_DUMP`, see the r11 report).
30627+ ///
30628+ /// The path it pins: libaom's `mb_to_right_edge` / `mb_to_bottom_edge`
30629+ /// (`set_mi_row_col`, av1_common_int.h) are SIGNED, and a block that
30630+ /// overhangs the frame's right/bottom edge keeps its full `bw4`/`bh4`, so
30631+ /// `mi_cols - bw4 - mi_col` and `mi_rows - bh4 - mi_row` go NEGATIVE there
30632+ /// and `clamp_mv_ref` (mvref_common.h) narrows the stack window by that
30633+ /// much. This frame height is 1608 -> `mi_rows == 402`, so every 64x64
30634+ /// block on the bottom row overhangs by 14 mi units. Computing that
30635+ /// difference in `usize` saturated it to 0 and left the window 448 subpel
30636+ /// too wide, so an edge block's predictor was never clamped -- one 64x64
30637+ /// NEWMV block at mi(400,368) took `mv0=(224,568)` where libaom takes
30638+ /// `(32,568)`, its MC then read the replicated bottom row instead of real
30639+ /// reference rows, and the frame missed by 221 bytes.
30640+ #[test]
30641+ fn a_10bit_film_frames_with_frame_edge_mv_clamping_decode_pixel_exact() {
30642+ const NAME: &str = "a_10bit_film_frames_with_frame_edge_mv_clamping_decode_pixel_exact";
30643+ if !have_ffmpeg() {
30644+ eprintln!("SKIP {NAME}: no ffmpeg");
30645+ return;
30646+ }
30647+ let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
30648+ .join("fixtures/reelb_head_mvclamp_witness.obu");
30649+ let stream = std::fs::read(&path)
30650+ .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display()));
30651+ let (width, height) = (3840usize, 1608usize);
30652+ let clamp_before = crate::mvstack::mv_clamp_edge_overhang_hits();
30653+ let frames = match decode_stream(&stream) {
30654+ Ok(frames) => frames,
30655+ Err(e) => panic!("{NAME}: decode_stream refused: {e}"),
30656+ };
30657+ let clamped = crate::mvstack::mv_clamp_edge_overhang_hits() - clamp_before;
30658+ assert!(
30659+ clamped > 0,
30660+ "{NAME}: mv_clamp_edge_overhang never fired -- the gate would be vacuous for it"
30661+ );
30662+ assert_eq!(frames.len(), 52, "{NAME}: 52 shown frames");
30663+ assert_eq!((frames[0].width, frames[0].height), (width, height));
30664+ let ffmpeg_frames = ffmpeg_decode_sequence_10bit(&stream, width, height, frames.len());
30665+ assert_eq!(
30666+ ffmpeg_frames.len(),
30667+ frames.len(),
30668+ "{NAME}: ffmpeg returned {} frames, we decoded {}",
30669+ ffmpeg_frames.len(),
30670+ frames.len()
30671+ );
30672+ for (i, (ours, theirs)) in frames.iter().zip(&ffmpeg_frames).enumerate() {
30673+ assert_eq!(ours.y, theirs.y, "{NAME}: frame {i} luma vs ffmpeg");
30674+ assert_eq!(ours.u, theirs.u, "{NAME}: frame {i} U vs ffmpeg");
30675+ assert_eq!(ours.v, theirs.v, "{NAME}: frame {i} V vs ffmpeg");
30676+ }
30677+ eprintln!(
30678+ "{NAME}: 52 shown frames pixel-exact on every plane; \
30679+ mv_clamp_edge_overhang={clamped}"
30680+ );
30681+ }
30682+
30618 /// lane-sub8x4 r2, the gate that lifts BOTH sub-8x8 intra refusals ("an30683 /// lane-sub8x4 r2, the gate that lifts BOTH sub-8x8 intra refusals ("an
30619 /// intra 4x4 block inside an inter frame's sub-8x8 split" and "an intra30684 /// intra 4x4 block inside an inter frame's sub-8x8 split" and "an intra
30620 /// 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition").30685 /// 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition").