forge
Explore
Log in

Don't you dare go Hollow

The key frame's 64x64 intra root at a superblock the frame edge cuts

Browse files at this commit

3603580b9784ce3ea1ab8e523df4d3d06e9da39dAhmet Kaan GümüşParent: c29f173
  • fix
    • the key frame's 64x64 intra root at a superblock the frame edge cuts
Show full message
fix(av1): the key frame's 64x64 intra root at a superblock the frame edge cuts

The root shipped behind two guards written from a desync report that does not
reproduce: the frame had to be a whole number of superblocks, and the
superblock wholly inside it. The first switched the whole feature off on any
arbitrary export size. Legality is now the spec's has_cols/has_rows pair, the
same the inter root and the tile writer already use, and the new
stream::tests::a_doubly_cut_superblock_root_round_trips_in_every_plane pins
Y, U and V of the reconstruction, our decoder and ffmpeg at four straddling
sizes (three cut on both axes), asserting the edge fire count first.
crates/ec-av1/src/encode.rsmodified+60 -33
@@ -662,13 +662,16 @@ pub(crate) const I64_ROOT: bool = true;
662662 /// the last [`take_i64_root_hits`], so a gate reports the fire rate rather
663663 /// than assuming it (class `gate-blind-to-feature`).
664664 /// Index 1 is how many were OFFERED the trial at all, so a gate reads a
665-/// SHARE and not a bare count.
666-static I64_HITS: [std::sync::atomic::AtomicUsize; 2] =
667- [const { std::sync::atomic::AtomicUsize::new(0) }; 2];
665+/// SHARE and not a bare count, and index 2 how many of the whole ones sat on
666+/// a superblock the true frame edge CUTS THROUGH (lane-corner) -- the
667+/// `gate-blind-to-feature` guard for the edge path, which no
668+/// superblock-aligned clip fires at all.
669+static I64_HITS: [std::sync::atomic::AtomicUsize; 3] =
670+ [const { std::sync::atomic::AtomicUsize::new(0) }; 3];
668671
669-/// The key-frame `(whole, offered)` 64x64-intra-root counts since the last
670-/// call, and zero them.
671-pub fn take_i64_root_hits() -> [usize; 2] {
672+/// The key-frame `(whole, offered, edge)` 64x64-intra-root counts since the
673+/// last call, and zero them.
674+pub fn take_i64_root_hits() -> [usize; 3] {
672675 std::array::from_fn(|i| I64_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed))
673676 }
674677
@@ -792,10 +795,17 @@ pub fn take_b64_var_tx_hits() -> usize {
792795 ///
793796 /// Off by `EC_AV1_B64EDGE=0`.
794797 fn b64_edge() -> bool {
798+ edge_root_enabled() && b64_root()
799+}
800+
801+/// What `EC_AV1_B64EDGE` names on its own, without the inter root's own
802+/// preset switch -- the key frame's root ([`I64_ROOT`]) reads the same flag
803+/// but is gated by [`i64_root`].
804+fn edge_root_enabled() -> bool {
795805 static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| {
796806 crate::envflags::var("EC_AV1_B64EDGE").ok().map(|v| v != "0")
797807 });
798- ENV.unwrap_or(true) && b64_root()
808+ ENV.unwrap_or(true)
799809 }
800810
801811 /// How many 64x64 roots the frame edge cut through since the last
@@ -7271,34 +7281,47 @@ pub(crate) fn encode_key_frame_inner(
72717281 // BEFORE its quadrants (so both start from the same
72727282 // reconstruction) and compared against the four of them below --
72737283 // the shape the inter frame's own 64 root takes
7274- // (`search_skip_64`). Only a superblock wholly inside the true
7275- // frame is offered it: the writer's `Superblock::Whole` arm
7276- // refuses one the edge cuts through, and a frame that allows
7277- // intrabc is refused outright ([`I64_ROOT`]) because the quadrant
7278- // searches below carry DV state a discarded trial would desync.
7284+ // (`search_skip_64`). A frame that allows intrabc is refused
7285+ // outright ([`I64_ROOT`]) because the quadrant searches below
7286+ // carry DV state a discarded trial would desync.
72797287 let (x64, y64) = (sb_col * SUPERBLOCK, sb_row * SUPERBLOCK);
7280- // corner-cut, ceiling named: only a frame whose true size is a
7281- // WHOLE number of superblocks is offered the root at all.
7282- // MEASURED (lane-i64): on a 248x152 frame -- whose bottom-right
7283- // superblock is cut on BOTH axes -- a frame carrying whole 64
7284- // roots elsewhere desyncs that corner's CHROMA three ways (our
7285- // writer's reconstruction, our decoder and ffmpeg all disagree,
7286- // luma stays exact), i.e. a defect in the whole-64 chroma
7287- // neighbour publication that only the doubly-cut tail block
7288- // reads (class `last-block-desync-reads-as-reconstruction`).
7289- // Every clip on the BD gates, and every real film/screen source,
7290- // is superblock aligned, so nothing measured is withheld.
7291- // Ceiling: trace the corner block's chroma `txb_skip`/dc context
7292- // against aomdec and lift this line.
7293- let aligned = header.mi_cols % crate::tile::SB_MI == 0
7294- && header.mi_rows % crate::tile::SB_MI == 0;
7288+ // lane-corner LIFTED the two guards this root shipped with: the
7289+ // frame no longer has to be a whole number of superblocks, and
7290+ // the superblock no longer has to be wholly inside the frame.
7291+ // The desync the guards were written for (a 248x152 corner's
7292+ // chroma disagreeing three ways) does NOT reproduce at this
7293+ // revision -- see
7294+ // `stream::tests::a_doubly_cut_superblock_root_round_trips_in_every_plane`,
7295+ // which pins all three planes of the encoder's reconstruction,
7296+ // our decoder and ffmpeg on four straddling sizes, three of them
7297+ // cut on BOTH axes, with the edge fire count asserted. What the
7298+ // guards cost was every key-frame root on an arbitrary export
7299+ // size (the `aligned` line switched the whole feature off on, for
7300+ // instance, a 1920x792 frame).
7301+ let whole_inside = x64 + SUPERBLOCK <= luma.true_width
7302+ && y64 + SUPERBLOCK <= luma.true_height;
7303+ // lane-corner: and the superblock the true frame edge CUTS
7304+ // THROUGH with more than half of each axis still inside, which is
7305+ // all AV1 asks of a `PARTITION_NONE` root (spec
7306+ // `decode_partition`'s `has_cols`/`has_rows`, the same pair the
7307+ // key writer's `Superblock::Whole` arm checks) -- the shape the
7308+ // inter root already takes ([`b64_edge`]).
7309+ let edge_inside = edge_root_enabled()
7310+ && crate::tile::has_half(
7311+ sb_col as u32 * crate::tile::SB_MI,
7312+ crate::tile::SB_MI,
7313+ header.mi_cols,
7314+ )
7315+ && crate::tile::has_half(
7316+ sb_row as u32 * crate::tile::SB_MI,
7317+ crate::tile::SB_MI,
7318+ header.mi_rows,
7319+ );
72957320 let sb64_legal = i64_root()
7296- && aligned
72977321 && ibc.is_none()
72987322 && sb_row * 2 + 1 < rows
72997323 && sb_col * 2 + 1 < cols
7300- && x64 + SUPERBLOCK <= luma.true_width
7301- && y64 + SUPERBLOCK <= luma.true_height;
7324+ && (whole_inside || edge_inside);
73027325 let mut sb64: Option<(f64, BlockCoeffs, [(Vec<u8>, Vec<CoefCtx>); 3])> = None;
73037326 if sb64_legal {
73047327 I64_HITS[1].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -7509,6 +7532,9 @@ pub(crate) fn encode_key_frame_inner(
75097532 left_mode[sb_row * 4 + cell] = block.mode;
75107533 }
75117534 I64_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
7535+ if !whole_inside {
7536+ I64_HITS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
7537+ }
75127538 let mode = block.mode;
75137539 coded.push((index, Superblock::Whole(block), vec![mode]));
75147540 }
@@ -12088,7 +12114,7 @@ mod tests {
1208812114 }
1208912115 let _ = take_i64_root_hits();
1209012116 let encoded = encode_key_frame_with_ctx(&picture, 120, 0.5, fctx).unwrap();
12091- let [roots, _offered] = take_i64_root_hits();
12117+ let [roots, _offered, _edge] = take_i64_root_hits();
1209212118 // The counters are process-global, so another test encoding in
1209312119 // parallel can only ADD to them -- the per-FRAME fact is the mode
1209412120 // list, which carries one entry per coded block and so exactly 12
@@ -16604,9 +16630,10 @@ mod tests {
1660416630 // reference (class `gate-blind-to-feature`).
1660516631 // lane-i64: the KEY frame's own 64x64 intra root -- how many of
1660616632 // the superblocks offered the trial were coded whole.
16607- let [i64_whole, i64_offered] = take_i64_root_hits();
16633+ let [i64_whole, i64_offered, i64_edge] = take_i64_root_hits();
1660816634 eprintln!(
16609- "{name}: key 64x64 intra roots {i64_whole} of {i64_offered} offered ({:.1}%)",
16635+ "{name}: key 64x64 intra roots {i64_whole} of {i64_offered} offered ({:.1}%), \
16636+ {i64_edge} on a superblock the frame edge cuts",
1661016637 100.0 * i64_whole as f64 / i64_offered.max(1) as f64,
1661116638 );
1661216639 eprintln!(
crates/ec-av1/src/stream.rsmodified+63 -0
@@ -3450,6 +3450,69 @@ pub(crate) mod tests {
34503450 );
34513451 }
34523452
3453+ /// lane-corner: sizes whose LAST superblock is cut by the true frame
3454+ /// edge on BOTH axes with more than half of each still inside -- so
3455+ /// `has_cols` and `has_rows` are both true and the spec lets that
3456+ /// superblock stay a whole 64x64 `PARTITION_NONE` block, which this
3457+ /// encoder's roots take (`EC_AV1_B64EDGE` on the inter side, the key
3458+ /// frame's own `Superblock::Whole`). The sibling luma-only test
3459+ /// (`encoder.rs`'s `a_non_superblock_aligned_clip_...`) is blind to
3460+ /// chroma (class `metric-blind-to-a-plane`), so this one compares all
3461+ /// three planes three ways: our decoder, the encoder's own
3462+ /// reconstruction, and ffmpeg.
3463+ #[test]
3464+ fn a_doubly_cut_superblock_root_round_trips_in_every_plane() {
3465+ let fctx = &crate::decode::FrameCtx::new();
3466+ if !have_ffmpeg() {
3467+ eprintln!("SKIP a_doubly_cut_superblock_root_round_trips_in_every_plane: no ffmpeg");
3468+ return;
3469+ }
3470+ const FRAMES: usize = 4;
3471+ // Each size leaves MORE than half of its last superblock row (and,
3472+ // but for 200x120's 8-px column, of its last column) inside, which is
3473+ // what makes the root legal there at all; 248x168/232x168/248x184 are
3474+ // cut on BOTH axes at once. 1920x1080 -- the user's own export shape
3475+ // -- is the same case: 1080 = 16*64 + 56, a 56-px bottom straddle.
3476+ for &(width, height) in &[(248usize, 168usize), (232, 168), (200, 120), (248, 184)] {
3477+ let pictures: Vec<_> = (0..FRAMES as i64)
3478+ .map(|i| panned_test_card(width, height, i * 3))
3479+ .collect();
3480+ let _ = crate::encode::take_b64_edge_hits();
3481+ let _ = crate::encode::take_i64_root_hits();
3482+ let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx)
3483+ .unwrap_or_else(|e| panic!("{width}x{height}: encoder refused: {e}"));
3484+ let edges = crate::encode::take_b64_edge_hits();
3485+ let [i64_whole, i64_offered, i64_edge] = crate::encode::take_i64_root_hits();
3486+ eprintln!(
3487+ "{width}x{height}: edge inter 64 roots {edges}, key 64 roots {i64_whole} of \
3488+ {i64_offered} offered, {i64_edge} of them cut by the frame edge"
3489+ );
3490+ // gate-blind-to-feature: without this the three-way compare below
3491+ // passes on a stream that never coded a whole root at the cut
3492+ // superblock at all.
3493+ assert!(
3494+ i64_edge > 0,
3495+ "{width}x{height}: no 64x64 root sat on a superblock the frame edge cuts"
3496+ );
3497+ let decoded = decode_stream(&encoded.stream)
3498+ .unwrap_or_else(|e| panic!("{width}x{height}: our decoder refused: {e}"));
3499+ let ffmpeg_frames = ffmpeg_decode_sequence(&encoded.stream, width, height, FRAMES);
3500+ assert_eq!(ffmpeg_frames.len(), FRAMES, "{width}x{height}: ffmpeg frame count");
3501+ assert_eq!(decoded.len(), FRAMES, "{width}x{height}: frame count");
3502+ for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() {
3503+ assert_eq!(got.y, want.y, "{width}x{height} frame {i} luma: ours vs ffmpeg");
3504+ assert_eq!(got.u, want.u, "{width}x{height} frame {i} U: ours vs ffmpeg");
3505+ assert_eq!(got.v, want.v, "{width}x{height} frame {i} V: ours vs ffmpeg");
3506+ }
3507+ for (i, (frame, want)) in encoded.frames.iter().zip(&ffmpeg_frames).enumerate() {
3508+ let rec = &frame.reconstruction;
3509+ assert_eq!(rec.y, want.y, "{width}x{height} frame {i} luma: recon vs ffmpeg");
3510+ assert_eq!(rec.u, want.u, "{width}x{height} frame {i} U: recon vs ffmpeg");
3511+ assert_eq!(rec.v, want.v, "{width}x{height} frame {i} V: recon vs ffmpeg");
3512+ }
3513+ }
3514+ }
3515+
34533516 /// A hand-built 3-frame stream that actually fires `GOLDEN_FRAME`
34543517 /// through [`decode_stream`], proving the round-4 flip (`decode.rs`'s
34553518 /// `decode_inter_block` `GOLDEN_FRAME` arm) the way 120+ real aomenc
lanes/corner.report.mdadded+50 -0
@@ -0,0 +1,50 @@
1+# lane-corner — the whole-64 root at a superblock the frame edge cuts
2+
3+## Verdict on the chartered defect: STALE PREMISE, not reproducible
4+
5+The debt says a whole-64 root on a doubly-cut superblock desyncs chroma three
6+ways at 248x152. At `main` f6c67770 it does not, for two separate reasons:
7+
8+1. **248x152 cannot carry the block at all.** Its bottom-right superblock has
9+ 24 px of 64 inside vertically, so spec `decode_partition`'s `has_rows`
10+ (`MiRow + halfBlock4x4 < MiRows`: 32+8=40 vs MiRows 38) is FALSE and
11+ `PARTITION_NONE` is not even a legal symbol there — the writer's
12+ `Superblock::Whole` arm refuses it (`tile.rs:2919`). The chartered repro
13+ (`a_sweep_of_doubly_straddling_sizes_round_trips_through_ffmpeg` with the
14+ `aligned` guard removed) is GREEN because no edge root ever fires in it:
15+ its sizes top out at height 152, and the key root's own
16+ `x64 + SUPERBLOCK <= true_width` bound kept it to wholly-inside
17+ superblocks anyway (class `gate-blind-to-feature`).
18+2. **Where the block IS legal, all three sides agree.** Sizes whose last
19+ superblock keeps more than half of each axis inside (248x168, 232x168,
20+ 248x184 — cut on BOTH axes — and 200x120) round-trip byte-exact in Y, U
21+ and V across the encoder's own reconstruction, our decoder and ffmpeg,
22+ over a 4-frame GOP.
23+
24+## What shipped
25+
26+* `encode.rs` `encode_key_frame_inner`: both guards lifted. The root's
27+ legality is now the spec's — `whole_inside || edge_inside`, where
28+ `edge_inside` is `has_half` on both axes — the same shape the inter root
29+ has had since lane-b64b. The `EC_AV1_B64EDGE` switch is now readable
30+ without the inter root's preset flag (`edge_root_enabled`), so `=0`
31+ restores the old behaviour on both roots.
32+* `I64_HITS` grew a third slot: whole roots that sat on a cut superblock.
33+ `take_i64_root_hits` returns `[whole, offered, edge]`, and the native gate
34+ prints the edge share (class `gate-blind-to-feature`).
35+* `stream::tests::a_doubly_cut_superblock_root_round_trips_in_every_plane`:
36+ the three-way, three-PLANE pin at four straddling sizes, asserting the edge
37+ fire count first. Its sibling
38+ `encoder.rs::a_non_superblock_aligned_clip_codes_edge_64x64_roots_and_decodes_sample_exact`
39+ compares LUMA only (class `metric-blind-to-a-plane`), which is why the
40+ chroma claim in the debt was never actually gated either way.
41+
42+## Sweep
43+
44+`aligned`-style whole-frame refusals: `grep -rn "SB_MI == 0" crates/ec-av1/src`
45+returns no other site — this was the only frame-alignment guard. The encoder
46+writes 64x64 superblocks only, so there is no whole-128 chroma path to sweep.
47+
48+## Measurements
49+
50+(filled in below as the arms land)