forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-corner' (930b0aa2): the whole-64 root guards on unaligned frames are lifted; the chartered chroma desync was a stale premise

Browse files at this commit

0915e534f98a4d8383b55daea07422c25a860e3bAhmet Kaan GümüşParent: c29f173d0faeca
At 248x152 the corner superblock fails has_rows, so the block the guard
forbade was never legal; four doubly-cut sizes are Y/U/V exact three
ways. 1920x792 film A -1.0% bytes. The reference-decode assertion is red
on the 1920x792 crop (libaom crf 5 Golomb tail, rav1e +-1): a decoder
lane follows.
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+107 -0
@@ -0,0 +1,107 @@
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+### What the lift buys: `EC_AV1_NATIVE_CROP=1920x792`, film A
51+
52+An unaligned crop is exactly the case the `aligned` guard switched the whole
53+key root off on, so `EC_AV1_I64=0` reproduces the old code on this arm to the
54+byte. `bd_rate_screen_native --ignored`, `EC_AV1_NATIVE_FILM=1`:
55+
56+| arm | bytes per ladder point | BD vs libaom | BD vs rav1e | wall ours | key 64 roots |
57+| --- | --- | --- | --- | --- | --- |
58+| before (`EC_AV1_I64=0`) | 69541 / 110325 / 180401 / 426318 | +23.2% | -1.6% | 170.7s | 0 of 0 |
59+| after (default) | 68847 / 109557 / 178345 / 426318 | +22.5% | -2.3% | 178.6s | 390 of 1440 (27.1%) |
60+
61+PSNR is equal to the hundredth at every point (44.03 / 45.60-45.61 / 46.92 /
62+48.20 dB), so the ~1.0% of bytes is the whole of it: **0.7 BD points on both
63+reference columns** for +4.6% encode wall.
64+
65+Zero of those roots sat on a cut superblock: 792 = 12*64 + 24, and a 24-px
66+straddle fails `has_rows`, so this crop cannot carry one. The edge half of
67+the lift is proven by the round-trip test, not by this row; the crop that
68+would measure it is 1920x1080 (a 56-px straddle) --
69+`deferred: the BD value of the EDGE root on a 1920x1080 crop -- 7 min/arm and
70+the box is shared -- unblocked by one `EC_AV1_NATIVE_CROP=1920x1080` pair`.
71+
72+Both arms end with the same 9 pre-existing REFERENCE-ladder decode failures
73+(libaom crf 5 "a Golomb tail longer than this decoder reads", a rav1e +-1) --
74+identical before and after, ours decode fine; that is lane-dgolomb's open
75+debt, not this lane's.
76+
77+### Invariants (release test binary, `$HOME/.cache/corner/inv.log`)
78+
79+All green, 15 runs: pins `the_encoders_own_streams_are_byte_identical_to_their_pins`
80+(8562 / 33357 -- the 640x384 pin clip is superblock-aligned and its bytes did
81+NOT move, which is the same reason the 12-frame gate rows stay byte-identical:
82+every gate clip is aligned and takes the identical path),
83+`the_facade_codes_the_same_bytes_as_encode_sequence`,
84+`predicted_coeff_bits_track_the_tile_the_writer_wrote`,
85+`every_speed_preset_decodes_sample_exact_through_both_decoders --ignored`,
86+the four thread-determinism tests `--include-ignored`, and
87+`EC_COMP_MISMATCH=1` at `EC_AV1_SPEED` 0 and 6 over the two straddling-size
88+tests and the 232x168 encoder test: zero mismatch lines.
89+
90+### Suite and check
91+
92+`cargo test -p ec-av1 --release`: **570 passed, 0 failed, 46 ignored** (569 +
93+this lane's new test), 1135s, RC=0.
94+`cargo check --workspace --all-targets -j4`: 0 errors, 0 `ec-av1` warnings
95+(the 22 that print are pre-existing `ec-opus` missing-doc and one `ec-vorbis`
96+dead-code warning).
97+
98+## Class
99+
100+`refusal-lifted-without-a-gate`'s mirror: **a guard written from a desync that
101+the guard's own repro cannot reach**. The `aligned` line named a 248x152
102+corner as its evidence, but 248x152 makes that corner's `PARTITION_NONE`
103+illegal in the first place, so no build of this encoder ever coded the block
104+the guard forbade -- while the guard cost every key root on every unaligned
105+frame. Sibling shape to `stale-premise lanes` and to
106+`fixture-proves-symbol-not-signal`: the ceiling comment quoted a measurement
107+whose fire count was never asserted.