forge
Explore
Log in

Don't you dare go Hollow

Frame-edge strips coded transform units outside the frame

Browse files at this commit

c850448d065043526750165dbd37fc5fecdcaac8Ahmet Kaan GümüşParent: e9e9d92
  • fix
    • frame-edge strips coded transform units outside the frame
Show full message
fix(av1): frame-edge strips coded transform units outside the frame

libaom clips av1_foreach_transformed_block_in_plane to max_blocks_wide/high
(mb_to_right_edge / mb_to_bottom_edge): a transform unit whose top-left sample
falls outside mi_cols*4 x mi_rows*4 is never coded. decode_rect_split walked
the full bh/tx rows, so a 64x32 bottom-edge HORZ strip on a 192x80 frame read
coefficients for four phantom 16x16 units at y=80..95 and desynced the stream
at the very next symbol -- the following superblock's gathered edge bit came
out SPLIT where aomenc wrote HORZ, and its 64 columns reconstructed wrong
(1081 Y pixels, rows 62..79). Ours now matches the oracle byte for byte on
that stream.

The r2 open defect is closed: its pin is un-ignored, and its flat-band assert
moves from the 32-level counter (only ever satisfied by our own desync) to the
new 64-level EDGE32_HITS slots 4..7 -- 96 edge bits, 48 bottom-HORZ and 48
right-VERT strips over 32 pixel-exact attempts. decode_block's square multi-TU
loop takes the same guard by inspection (odd-mi geometry only).

Suite: 339 passed / 0 failed / 31 ignored.
crates/ec-av1/src/decode.rsmodified+59 -15
@@ -1604,16 +1604,31 @@ pub(crate) fn sb_rect_hits() -> usize {
16041604 // PARTITION_HORZ decoded with `has_rows32 == false` (only the top 32x16 half
16051605 // is coded), slot 3 = a PARTITION_VERT with `has_cols32 == false`. Both tile
16061606 // paths (intra and inter) feed the same counter.
1607+//
1608+// lane-golomb r3: slots 4..7 are the same four facts one level up, at the
1609+// SUPERBLOCK (64x64) edge -- 4/5 = the gathered 64-level bit read as
1610+// HORZ/VERT / SPLIT, 6 = a 64x32 bottom-edge strip decoded with
1611+// `has_rows == false`, 7 = a 32x64 right-edge strip. Real aomenc answers a
1612+// bottom-edge superblock with the 64-level HORZ (measured: 192x80 flat-band,
1613+// every SB of the last row), so the 32-level slots stay 0 on that content and
1614+// only these prove the arm fired.
16071615 thread_local! {
1608- static EDGE32_HITS: std::cell::Cell<[usize; 4]> =
1609- const { std::cell::Cell::new([0; 4]) };
1616+ static EDGE32_HITS: std::cell::Cell<[usize; 8]> =
1617+ const { std::cell::Cell::new([0; 8]) };
16101618 }
16111619
16121620 /// Current value of [`EDGE32_HITS`].
1613-pub(crate) fn edge32_hits() -> [usize; 4] {
1621+pub(crate) fn edge32_hits() -> [usize; 8] {
16141622 EDGE32_HITS.with(|c| c.get())
16151623 }
16161624
1625+/// [`bump_edge32`] of `base + bit`, returning the bit -- the gathered edge
1626+/// reads are `if`-conditions, so the counter has to ride the expression.
1627+fn bump_edge32_bit(base: usize, bit: usize) -> usize {
1628+ bump_edge32(base + bit.min(1));
1629+ bit
1630+}
1631+
16171632 fn bump_edge32(slot: usize) {
16181633 EDGE32_HITS.with(|c| {
16191634 let mut h = c.get();
@@ -4810,6 +4825,18 @@ fn decode_rect_split(
48104825 let tu_mi = (mi_r + tu_row * (tx / MI), mi_c + tu_col * (tx / MI));
48114826 let (tu_px, tu_py) = (px + tu_col * tx, py + tu_row * tx);
48124827 let (col_off, row_off) = (tu_col * tx, tu_row * tx);
4828+ // lane-golomb r3: a frame-edge HORZ/VERT strip is the one block
4829+ // shape that hangs off the bottom (or right) of the frame, and
4830+ // libaom's `av1_foreach_transformed_block_in_plane` clips its
4831+ // loop bounds to `max_blocks_high/wide` (`mb_to_bottom_edge` /
4832+ // `mb_to_right_edge`, i.e. `mi_rows * 4` / `mi_cols * 4`): a unit
4833+ // whose TOP-LEFT sample is outside the frame is never coded. We
4834+ // read coefficients for those phantom units and desynced the
4835+ // stream right after -- the next superblock's partition symbol
4836+ // came out SPLIT instead of HORZ (192x80, mi_col=32).
4837+ if tu_px >= y.true_width || tu_py >= y.true_height {
4838+ continue;
4839+ }
48134840 // `has_top_right`/`has_bottom_left` (libaom `reconintra.c`) for a
48144841 // transform unit INSIDE a block, which is a different rule from
48154842 // the standalone-block table [`Reach::of`] applies -- and the
@@ -7429,6 +7456,15 @@ fn decode_block(
74297456 );
74307457 let tu_px = px + tu_col * logical_tx;
74317458 let tu_py = py + tu_row * logical_tx;
7459+ // lane-golomb r3, same-shape sweep of the strip fix below:
7460+ // libaom clips this loop to `max_blocks_wide/high` too. A
7461+ // square block only hangs off the frame when the frame's mi
7462+ // dimensions are odd at the block's own level (lane-oddh
7463+ // territory), so no gate here reaches it -- it is the
7464+ // identical libaom rule, not a measured fix.
7465+ if tu_px >= y.true_width || tu_py >= y.true_height {
7466+ continue;
7467+ }
74327468 let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0];
74337469 let tu_reach = Reach::of(logical_tx, tu_px, tu_py, y.width, y.height);
74347470 // This transform unit's own bsize (`logical_tx`) is smaller
@@ -10246,18 +10282,14 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
1024610282 // mi_row=400 mi_col=0, the first block of that row.
1024710283 match (has_cols, has_rows) {
1024810284 (true, false) => {
10249- if dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], VERT_ALIKE)) == 1 {
10250- PARTITION_SPLIT
10251- } else {
10252- PARTITION_HORZ
10253- }
10285+ let b = dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], VERT_ALIKE));
10286+ bump_edge32(4 + b.min(1) as usize);
10287+ if b == 1 { PARTITION_SPLIT } else { PARTITION_HORZ }
1025410288 }
1025510289 (false, true) => {
10256- if dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], HORZ_ALIKE)) == 1 {
10257- PARTITION_SPLIT
10258- } else {
10259- PARTITION_VERT
10260- }
10290+ let b = dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], HORZ_ALIKE));
10291+ bump_edge32(4 + b.min(1) as usize);
10292+ if b == 1 { PARTITION_SPLIT } else { PARTITION_VERT }
1026110293 }
1026210294 _ => PARTITION_SPLIT,
1026310295 }
@@ -11352,6 +11384,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
1135211384 tx_select,
1135311385 reduced_tx_set,
1135411386 )?;
11387+ if !has_rows {
11388+ bump_edge32(6);
11389+ }
1135511390 // lane-golomb r1: at the frame edge only the first half of the
1135611391 // strip is coded (libaom `decode_partition` guards the second
1135711392 // with the same has_rows test that made this partition non-square).
@@ -11393,6 +11428,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
1139311428 tx_select,
1139411429 reduced_tx_set,
1139511430 )?;
11431+ if !has_cols {
11432+ bump_edge32(7);
11433+ }
1139611434 // lane-golomb r1: at the frame edge only the first half of the
1139711435 // strip is coded (libaom `decode_partition` guards the second
1139811436 // with the same has_cols test that made this partition non-square).
@@ -17257,7 +17295,10 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1725717295 // lane-golomb r1 (class `parsed-then-discarded`): the gathered
1725817296 // bit IS the partition -- 0 means PARTITION_HORZ, only 1 means SPLIT
1725917297 // (libaom `ec_read_partition_impl`).
17260- if dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], VERT_ALIKE)) == 1 {
17298+ if bump_edge32_bit(
17299+ 4,
17300+ dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], VERT_ALIKE)),
17301+ ) == 1 {
1726117302 PARTITION_SPLIT
1726217303 } else {
1726317304 PARTITION_HORZ
@@ -17267,7 +17308,10 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1726717308 // lane-golomb r1 (class `parsed-then-discarded`): the gathered
1726817309 // bit IS the partition -- 0 means PARTITION_VERT, only 1 means SPLIT
1726917310 // (libaom `ec_read_partition_impl`).
17270- if dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], HORZ_ALIKE)) == 1 {
17311+ if bump_edge32_bit(
17312+ 4,
17313+ dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], HORZ_ALIKE)),
17314+ ) == 1 {
1727117315 PARTITION_SPLIT
1727217316 } else {
1727317317 PARTITION_VERT
crates/ec-av1/src/stream.rsmodified+23 -12
@@ -14807,7 +14807,6 @@ mod tests {
1480714807 /// the partition guard. `#[ignore]` so the suite stays green; run it with
1480814808 /// `cargo test -p ec-av1 --lib -- --ignored a_32x32_frame_edge_rect_partition_with_a_flat`.
1480914809 #[test]
14810- #[ignore = "open r2 defect: the 32-level edge HORZ strip reconstructs wrong pixels"]
1481114810 fn a_32x32_frame_edge_rect_partition_with_a_flat_band_decodes_pixel_exact() {
1481214811 edge32_gate(
1481314812 "a_32x32_frame_edge_rect_partition_with_a_flat_band_decodes_pixel_exact",
@@ -14836,7 +14835,7 @@ mod tests {
1483614835 // tile walk is exercised too.
1483714836 arms.push((192, 80, 5, false));
1483814837 let cqs: [u32; 8] = [35, 40, 45, 50, 55, 57, 59, 61];
14839- let mut totals = [0usize; 4];
14838+ let mut totals = [0usize; 8];
1484014839 let mut compared = 0usize;
1484114840 let mut refusals: Vec<String> = Vec::new();
1484214841 for &(width, height, frame_count, ten_bit) in &arms {
@@ -14958,7 +14957,7 @@ mod tests {
1495814957 };
1495914958 let after = crate::decode::edge32_hits();
1496014959 let delta: Vec<usize> =
14961- (0..4).map(|i| after[i] - before[i]).collect();
14960+ (0..8).map(|i| after[i] - before[i]).collect();
1496214961 let reference = if ten_bit {
1496314962 ffmpeg_decode_sequence_10bit(&stream, width, height, decoded.len())
1496414963 } else {
@@ -14999,7 +14998,7 @@ mod tests {
1499914998 }
1500014999 }
1500115000 compared += 1;
15002- for i in 0..4 {
15001+ for i in 0..8 {
1500315002 totals[i] += delta[i];
1500415003 }
1500515004 }
@@ -15010,27 +15009,39 @@ mod tests {
1501015009 refusals.join("\n")
1501115010 );
1501215011 assert!(
15013- totals[0] + totals[1] > 0,
15014- "{NAME}: no 32x32-level frame-edge partition bit was read at all over \
15012+ totals[0] + totals[1] + totals[4] + totals[5] > 0,
15013+ "{NAME}: no frame-edge partition bit was read at all over \
1501515014 {compared} compared attempts -- the gate proved nothing"
1501615015 );
15016+ // lane-golomb r3: the flat-band arm's ASSERT moved one level up. Until
15017+ // r3 the last superblock row was reached through a 32-level edge bit
15018+ // only because our own out-of-frame transform units had desynced the
15019+ // stream; with that fixed, real aomenc answers a bottom-edge
15020+ // superblock with the 64-level `PARTITION_HORZ` (and a right-edge one
15021+ // with `PARTITION_VERT`) and the 32-level slots stay 0 on this
15022+ // content. Slots 6/7 are the strips actually decoded at the edge.
1501715023 assert!(
15018- !flat_band || totals[2] > 0,
15019- "{NAME}: the 32-level edge bit never named PARTITION_HORZ at the bottom edge \
15024+ !flat_band || totals[6] > 0,
15025+ "{NAME}: no 64x32 bottom-edge HORZ strip was decoded \
1502015026 (edge32={totals:?}, {compared} compared attempts)"
1502115027 );
1502215028 assert!(
15023- !flat_band || totals[3] > 0,
15024- "{NAME}: the 32-level edge bit never named PARTITION_VERT at the right edge \
15029+ !flat_band || totals[7] > 0,
15030+ "{NAME}: no 32x64 right-edge VERT strip was decoded \
1502515031 (edge32={totals:?}, {compared} compared attempts)"
1502615032 );
1502715033 eprintln!(
15028- "{NAME}: {compared} pixel-exact attempts, edge32 bits [horz_or_vert={} split={}] \
15029- bottom-HORZ={} right-VERT={}, {} named refusals",
15034+ "{NAME}: {compared} pixel-exact attempts, 32-level edge bits \
15035+ [horz_or_vert={} split={}] bottom-HORZ={} right-VERT={}, 64-level edge bits \
15036+ [horz_or_vert={} split={}] bottom-HORZ={} right-VERT={}, {} named refusals",
1503015037 totals[0],
1503115038 totals[1],
1503215039 totals[2],
1503315040 totals[3],
15041+ totals[4],
15042+ totals[5],
15043+ totals[6],
15044+ totals[7],
1503415045 refusals.len()
1503515046 );
1503615047 for r in &refusals {
lanes/golomb-r3.report.mdadded+106 -0
@@ -0,0 +1,106 @@
1+# lane-golomb r3 — the suite red was my own gate recipe; the edge strip was out-of-frame transform units
2+
3+## TASK 1 — bisect table (the r2 suite failure)
4+
5+`stream::tests::a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16_decodes_pixel_exact`,
6+run in a detached scratch worktree (`scratchpad/verify-golomb`, `CARGO_TARGET_DIR=~/.cache/cargo-target-verify`):
7+
8+| commit | what it is | result |
9+|---|---|---|
10+| a762fff | mcomp64 base | ok, 1 passed |
11+| 909064f | + fi8 cherry-pick 2d5c425 | ok, 1 passed |
12+| eacd7fd | r1 edge-bit fix | ok, 1 passed |
13+| fd54bcf | r2 guards | **FAILED** — "the stream decoded but no coded (non-skip) rect leaf fired" |
14+
15+EVIDENCE: bisect stdout above | `git worktree add --detach` at each sha, `cargo test -p ec-av1 --lib -- a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16` | 3 ok / 1 FAILED, first red = fd54bcf
16+
17+Root cause: **not** fi8, not the r1 edge read. r2's per-arm recipe override
18+(`--min-partition-size=32 --tune-content=film`) was pasted into the WRONG gate — hunk
19+`stream.rs@8435`, inside `a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16`, whose whole
20+point is sub-16x16 rect leaves. At `--min-partition-size=32` that 64x64 mandelbrot frame cannot
21+contain a 16x8 leaf, so `rect_leaf_coeff_hits` went to 0 and the gate's own
22+"the gate proves nothing" assert fired — the assert did its job. Fixed in ebec75d by restoring
23+`--min-partition-size=8`; the edge32 gate has always carried its own copy of the override.
24+No warning is owed to the fi8 merge agent: fi8 is already on main (b1d8457) and is exonerated by
25+row 2 of the table.
26+
27+EVIDENCE: gate stdout | `cargo test -p ec-av1 --lib -- --nocapture a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16` after ebec75d | `pixel-exact, rect_leaf_coeff_hits=8`, 1 passed
28+
29+## TASK 2 — the frame-edge strip's wrong pixels: out-of-frame transform units
30+
31+Class: `parsed-then-discarded`'s mirror — *read* something the reference never wrote
32+(`equal-range-means-unread`).
33+
34+Reproduced standalone (192x80 cq40 8-bit flat band, md5 `4bd18b25…`, scratchpad `e32.obu`):
35+1081 Y pixels differ, rows 62..79, **cols 124..191** — the LAST superblock only.
36+
37+The oracle's own view of that superblock row (instrumented aomdec):
38+`EC_PRED mi_row=16 mi_col=16 … bsize=11 part=1 txw=16 txh=16` with **row_off=0 only** (four TUs),
39+and `mi_row=16 mi_col=32 … bsize=11 part=1 txw=32 txh=32` (two TUs). `bsize=11` is
40+`BLOCK_64X32`: aomenc answers the bottom-edge superblock with the **64-level** `PARTITION_HORZ`,
41+never the 32-level one, and codes only the transform-unit ROW that is inside the frame.
42+
43+Ours decoded `mi_col=16` identically (`TRACE_RECT_SPLIT bw=64 bh=32 tx=16`) but then walked
44+`bh/tx = 2` TU rows, reading coefficients for four phantom 16x16 units at y=80..95 — outside a
45+frame whose `mi_rows*4 == 80`. That desynced the stream at exactly the next symbol: the SB2
46+partition bit came out SPLIT (two 32x16 strips, `edge32=[2,0,2,0]`) where the oracle read HORZ.
47+The r2 report's reading ("prediction/availability inside the strip") was wrong; the strips
48+were the *symptom* of a desync, and the "32-level edge HORZ fired" counter was a
49+`counter-from-refused-stream`-shaped artefact of our own desync.
50+
51+Fix — `crates/ec-av1/src/decode.rs:4818` (`decode_rect_split`, the only place a block hangs off
52+the frame): skip a transform unit whose top-left sample is outside `true_width`/`true_height`
53+(`mi_cols*4`/`mi_rows*4`), mirroring libaom `av1_foreach_transformed_block_in_plane`'s
54+`max_blocks_wide/high` clip (`mb_to_right_edge`/`mb_to_bottom_edge`). The chroma unit of a strip
55+always has its top-left inside, so it is unaffected.
56+
57+EVIDENCE: scratchpad/{e32.obu, ours.yuv, ao.yuv, ao-pred.txt, ours-step.txt} | instrumented
58+aomdec `EC_PRED`/`EC_TRACE_MODE_STEP` vs `decode_probe`, byte diff of the two raw yuv420p dumps |
59+before: 1081 Y pixels differ (first diff at our `EC_ISTEP mi_row=16 mi_col=32 name=skip rng=51413`
60+vs oracle `rng=32901`); after: **0 differing bytes over all three planes**
61+
62+## Counters + the pin, un-ignored
63+
64+`EDGE32_HITS` widened 4 → 8 slots (`decode.rs:1602`): slots 4/5 = the **64**-level gathered edge
65+bit read as HORZ/VERT / SPLIT (both tile paths), 6 = a 64x32 bottom-edge strip decoded with
66+`has_rows == false`, 7 = a 32x64 right-edge strip. Necessary because the flat-band arm's r2
67+assert (`totals[2] > 0`, a 32-level bottom-edge HORZ) was only ever satisfied by our own desync;
68+on a correct decode of this content the 32-level slots are 0 by construction and the 64-level
69+slots carry the proof. `#[ignore]` removed from
70+`a_32x32_frame_edge_rect_partition_with_a_flat_band_decodes_pixel_exact`.
71+
72+EVIDENCE: gate stdout | `cargo test -p ec-av1 --lib -- --nocapture a_32x32_frame_edge` |
73+2 passed; flat arm: 32 pixel-exact attempts, 64-level edge bits [horz_or_vert=96 split=0]
74+bottom-HORZ=48 right-VERT=48, 32-level slots all 0; detail arm: 32 pixel-exact attempts,
75+64-level [horz_or_vert=7 split=89] right-VERT=7, 32-level split=178
76+
77+## Suite
78+`$HOME/.cache/golomb-suite-r3.log` — **339 passed / 0 failed / 31 ignored** (277 s), run as a
79+user systemd unit on the final tree. r2 was 337/1 FAILED/32 ignored: +1 fixed (task 1), +1
80+un-ignored (task 2).
81+
82+Siblings re-run inside that suite, all green: `a_real_aomenc_stream_with_a_coded_rect_strip_below_16x16`,
83+`the_reelb_head_key_frame`, both `a_*_32x32_frame_edge_*`, `tiny_frame_size_sweep`,
84+`superblock_level_*`, `filter_intra_*`, `refusal_inventory`, `gate_coverage`.
85+
86+## Same-shape sweep
87+`decode.rs:7458` (`decode_block`'s square multi-TU loop) has the identical unclipped
88+`0..n_axis` shape and now carries the identical guard. A square block only hangs off the frame
89+when the frame's mi dimensions are odd at that block's level (lane-oddh's territory), so no gate
90+in this suite reaches it: the guard is libaom's rule applied by inspection, proven only inert
91+here (suite unchanged). `decode_block_rect64` delegates its split case to `decode_rect_split`,
92+so it is covered by the measured fix. Flagged, not claimed.
93+
94+## Film
95+`hg-head.obu` (18 frames) still stops at
96+"an inter SB-level partition type other than NONE or SPLIT (this decoder's inter tile path
97+recurses a superblock only as SPLIT)" — unchanged by this round (same string as r1's residue;
98+the inter tile path never reaches a 64-level HORZ).
99+EVIDENCE: probe stdout | `cargo run -p ec-av1 --example decode_probe -- hg-head.obu` | REFUSED at
100+the inter SB-level partition, 18 frame headers parsed, tiling cols=1 rows=1
101+
102+## Residue
103+- deferred: the inter tile path's 64-level `PARTITION_HORZ`/`VERT` (the film's next blocker) —
104+ unblocked by rect inter residual coding, which is lane-inter*/interbis territory, not this one.
105+- accepted: the square-block guard above is unexercised by any gate on this lane (odd-mi
106+ geometry is lane-oddh's gate).