ec-av1: lane-av1422stripwrite -- the 4:2:2 residual was not the rect-strip chroma write/anchor: a stored reference's chroma plane was SHAPED from a two-way subsampling guess (luma-sized = 4:4:4, else a quarter = 4:2:0) that folds 4:2:2 into 4:2:0, so every 4:2:2 reference claimed h/2 rows while holding h; every chroma MC sourcing a row at or past the frame's midpoint was then clamped against true_height
Browse files at this commit35d5774cf6ba7f24178d44818c66a39e8ea36409Parent: 6bb8ccf3 changed files +208 -7
- crates/ec-av1/src/decode.rs +30 -6
- crates/ec-av1/src/encode.rs +4 -1
- lanes/av1422stripwrite.report.md +174 -0
crates/ec-av1/src/decode.rsmodified+30 -6
| @@ -30321,15 +30321,13 @@ impl<'p> RefPix<'p> { | |||
| 30321 | ( | 30321 | ( |
| 30322 | whole_plane(&g.y, g.width, g.height), | 30322 | whole_plane(&g.y, g.width, g.height), |
| 30323 | { | 30323 | { |
| 30324 | - // Reference pictures carry no subsampling field. A | 30324 | + // No subsampling field on a stored reference: the |
| 30325 | - // 4:4:4 chroma plane is luma-sized; 4:2:0 is the quarter. | 30325 | + // chroma format comes off its own sample count. |
| 30326 | - let full = g.u.len() == g.width * g.height; | 30326 | + let (cw, ch) = ref_chroma_shape(g.u.len(), g.width, g.height); |
| 30327 | - let (cw, ch) = if full { (g.width, g.height) } else { (g.width / 2, g.height / 2) }; | ||
| 30328 | whole_plane(&g.u, cw, ch) | 30327 | whole_plane(&g.u, cw, ch) |
| 30329 | }, | 30328 | }, |
| 30330 | { | 30329 | { |
| 30331 | - let full = g.v.len() == g.width * g.height; | 30330 | + let (cw, ch) = ref_chroma_shape(g.v.len(), g.width, g.height); |
| 30332 | - let (cw, ch) = if full { (g.width, g.height) } else { (g.width / 2, g.height / 2) }; | ||
| 30333 | whole_plane(&g.v, cw, ch) | 30331 | whole_plane(&g.v, cw, ch) |
| 30334 | }, | 30332 | }, |
| 30335 | ) | 30333 | ) |
| @@ -30396,6 +30394,32 @@ fn whole_plane(data: &[u16], width: usize, height: usize) -> PlaneBuf<'_> { | |||
| 30396 | } | 30394 | } |
| 30397 | } | 30395 | } |
| 30398 | 30396 | ||
| 30397 | +/// A reference picture's own chroma plane shape, `(width, height)`, from its | ||
| 30398 | +/// luma size and its chroma sample count. A stored reference carries no | ||
| 30399 | +/// subsampling field, but the count is exact and unambiguous: | ||
| 30400 | +/// `w * h` is 4:4:4, `(w / 2) * h` is 4:2:2, `(w / 2) * (h / 2)` is 4:2:0. | ||
| 30401 | +/// | ||
| 30402 | +/// The two-way test this replaces ("luma-sized, else a quarter") folded 4:2:2 | ||
| 30403 | +/// into the 4:2:0 shape, so a 4:2:2 reference plane claimed `h / 2` rows -- | ||
| 30404 | +/// half the samples it actually holds. Every chroma MC reading such a plane | ||
| 30405 | +/// then had its source row clamped against `true_height` (spec 7.11.3.4 / | ||
| 30406 | +/// libaom's `dec_calc_subpel_params_and_extend` clamps to the reference's | ||
| 30407 | +/// valid area), which folded every chroma row at or past the frame's | ||
| 30408 | +/// midpoint back onto the last valid row: on the pinned t422 fixture | ||
| 30409 | +/// frame 1, the 8x4 inter strip at luma (88, 208) MC-predicted chroma | ||
| 30410 | +/// (88, 104) from frame 0 with mv 0 and a pure copy, yet wrote | ||
| 30411 | +/// (206, 204, 202, 200) x4 rows where the reference holds | ||
| 30412 | +/// (202, 204, 204, 167) / (185, 190, 190, 165) / ... | ||
| 30413 | +fn ref_chroma_shape(len: usize, width: usize, height: usize) -> (usize, usize) { | ||
| 30414 | + if len == width * height { | ||
| 30415 | + (width, height) | ||
| 30416 | + } else if len == (width / 2) * height { | ||
| 30417 | + (width / 2, height) | ||
| 30418 | + } else { | ||
| 30419 | + (width / 2, height / 2) | ||
| 30420 | + } | ||
| 30421 | +} | ||
| 30422 | + | ||
| 30399 | /// Resolves a reference's own planes -- the `ref_frame -> picture` lookup | 30423 | /// Resolves a reference's own planes -- the `ref_frame -> picture` lookup |
| 30400 | /// [`decode_inter_block`]'s single-ref path does, factored out so the | 30424 | /// [`decode_inter_block`]'s single-ref path does, factored out so the |
| 30401 | /// compound path can call it twice (lane-av1comp), and the point where | 30425 | /// compound path can call it twice (lane-av1comp), and the point where |
crates/ec-av1/src/encode.rsmodified+4 -1
| @@ -1548,7 +1548,10 @@ pub struct Picture { | |||
| 1548 | pub height: usize, | 1548 | pub height: usize, |
| 1549 | /// The luma plane, `width * height` samples in raster order. | 1549 | /// The luma plane, `width * height` samples in raster order. |
| 1550 | pub y: Vec<u16>, | 1550 | pub y: Vec<u16>, |
| 1551 | - /// The U plane, at half the width and half the height. | 1551 | + /// The U plane: half the width, and the frame's own chroma height -- a |
| 1552 | + /// quarter of the luma samples at 4:2:0, half at 4:2:2, all of them at | ||
| 1553 | + /// 4:4:4. A consumer that only knows the luma size reads the chroma | ||
| 1554 | + /// format off this sample count (see `decode::ref_chroma_shape`). | ||
| 1552 | pub u: Vec<u16>, | 1555 | pub u: Vec<u16>, |
| 1553 | /// The V plane, the same shape as U. | 1556 | /// The V plane, the same shape as U. |
| 1554 | pub v: Vec<u16>, | 1557 | pub v: Vec<u16>, |
lanes/av1422stripwrite.report.mdadded+174 -0
| @@ -0,0 +1,174 @@ | |||
| 1 | +# lane-av1-422stripwrite — the 4:2:2 residual was NOT the rect-strip | ||
| 2 | +# chroma write/anchor: a reference picture's chroma plane claimed | ||
| 3 | +# `h / 2` rows, so every chroma MC sourced at or past the frame's | ||
| 4 | +# midpoint was clamped; the pinned 16-frame t422 stream now decodes | ||
| 5 | +# byte-exact | ||
| 6 | + | ||
| 7 | +## Outcome | ||
| 8 | + | ||
| 9 | +The parent lane's named site is **fixed, and it was not where the parent | ||
| 10 | +pointed**. Frame-1 prefilt chroma no longer first-diffs at U/V (88, 104): | ||
| 11 | +after the fix **all 16 frames of the pinned t422 stream are byte-exact on | ||
| 12 | +every plane at prefilt, post-deblock AND post-CDEF, and the final | ||
| 13 | +(post-loop-restoration) output is byte-identical to the oracle for all 16 | ||
| 14 | +frames** — 0 diffs, measured. The 4:2:2 header refusal stays. | ||
| 15 | + | ||
| 16 | +## Localization: the write and the anchor were both already right | ||
| 17 | + | ||
| 18 | +The parent read the site as "our written rows 88..91 are not the oracle's | ||
| 19 | +MC output" and suspected the strip write/anchor. Measured, in order: | ||
| 20 | + | ||
| 21 | +1. **The block is not what the parent said.** The oracle's own dispatch | ||
| 22 | + trace (`SUB48D`, added to `build_inter_predictors` in | ||
| 23 | + `reconinter_template.inc`) puts the site on **two `BLOCK_8X4` blocks at | ||
| 24 | + luma (88, 208) and (88, 216)** — `bsize=2`, `mi=(208,88)` / `mi=(216,88)`, | ||
| 25 | + one per mi column, i.e. the top strips of the 8x8 groups at mi (11,26) | ||
| 26 | + and (11,27), each coded its own chroma `TX_4x4` at chroma (88,104) and | ||
| 27 | + (88,108) (`is_chroma_reference` at ss (1,0): `mi_size_wide[BLOCK_8X4] == 2` | ||
| 28 | + in libaom's 4x4-mi units, so the column clause `!(bw & 1)` is true and | ||
| 29 | + BOTH are chroma references — `av1_common_int.h:1454`). The parent's | ||
| 30 | + "mi(22,52)" is this decoder's own 4x4-mi coordinate for the same block | ||
| 31 | + (`const MI: usize = 4`, `decode.rs:6052`), not libaom's 8x8 mi, and its | ||
| 32 | + "chroma cols 104..111, rows 88..95" spans TWO mi columns and two 8x8 | ||
| 33 | + groups. There is no strip-2 chroma MC because the strips below | ||
| 34 | + (luma rows 92..95) are INTRA, not because the pair merged. | ||
| 35 | +2. **The oracle's prediction is a pure reference copy** (`AOMIN48`/ | ||
| 36 | + `AOMOUT48`, both inert without the env var): `row=88 col=104 bw=4 bh=4 | ||
| 37 | + sx=0 sy=0 srcoff=17000 stride=192` → source = frame 0's final U at | ||
| 38 | + (88, 104) exactly, output `r0 = 202 204 204 167` = the source bytes. | ||
| 39 | + The oracle's final chroma there is the same value, so the strip is a | ||
| 40 | + zero-residual copy. | ||
| 41 | +3. **Our MC inputs were identical and correct.** A temporary rung in | ||
| 42 | + `decode_inter_sub8_rect2`'s 4:2:2 per-piece arm dumped the raw | ||
| 43 | + reference samples at the unit before the MC: | ||
| 44 | + `unit=(104,88) stride=128 tw=128 th=72 xq4=1664 yq4=1408 | ||
| 45 | + raw=[202, 204, 204, 167, 185, 190, 190, 165, 158, 164, 163, 148, | ||
| 46 | + 145, 150, 148, 138]` — byte-identical to the oracle's `src0`. So the | ||
| 47 | + reference *contents*, the position, the mv, the filters and the scale | ||
| 48 | + were all right, and the 4x4 write rect `(unit_x, unit_y, 4, 4)` was | ||
| 49 | + right. What was wrong was that the MC turned those correct inputs into | ||
| 50 | + `206 204 202 200` x4 rows. | ||
| 51 | +4. **The MC output was the tell.** `th=72` on a 144-tall frame. The | ||
| 52 | + reference plane the MC read declared **72 chroma rows** while holding | ||
| 53 | + 144. Rows at/past 72 are out of the plane's valid area, so | ||
| 54 | + `predict_with_filters` clamped them — the four output rows came from a | ||
| 55 | + clamped row, which is why they were identical to each other and why | ||
| 56 | + they matched nothing in the reference. | ||
| 57 | + | ||
| 58 | +## Root cause, class `ref-chroma-shape-420-folds-422` | ||
| 59 | + | ||
| 60 | +`RefPix::planes` (`decode.rs:30311`) builds each reference's chroma | ||
| 61 | +`PlaneBuf` from a **two-way** shape test: | ||
| 62 | + | ||
| 63 | +```rust | ||
| 64 | +let full = g.u.len() == g.width * g.height; | ||
| 65 | +let (cw, ch) = if full { (g.width, g.height) } else { (g.width / 2, g.height / 2) }; | ||
| 66 | +``` | ||
| 67 | + | ||
| 68 | +A stored reference carries no subsampling field, so the code inferred it | ||
| 69 | +from the sample count — but the inference only knew 4:4:4 (luma-sized) and | ||
| 70 | +4:2:0 (a quarter). A 4:2:2 reference is **half-width, full-height**: | ||
| 71 | +`(w/2) * h`, which matches neither, so it fell into the 4:2:0 branch and | ||
| 72 | +claimed `h / 2` rows. The stored data was never wrong (the frame is stored | ||
| 73 | +through `round_ss(fw, ss_x) x round_ss(fh, ss_y)`, `decode.rs:25806`, which | ||
| 74 | +is 4:2:2-correct) — only the *declared* shape was, and | ||
| 75 | +`whole_plane` copies it into `width`/`height`/`true_*`, which is what every | ||
| 76 | +chroma MC and every edge clamp reads. | ||
| 77 | + | ||
| 78 | +Consequence: at 4:2:2, **every** inter chroma prediction whose source row | ||
| 79 | +was at or past the frame's vertical midpoint read clamped garbage, on every | ||
| 80 | +subsampling-independent path (square blocks, rect strips, sub-8 pieces, | ||
| 81 | +compound, OBMC). On the pinned fixture the same rung sweep found four | ||
| 82 | +`mv = 0` per-piece units wrong, all with `unit_y >= 72`: (104,88), | ||
| 83 | +(108,88) [the named site] and (80,120), (80,124) — the parent's residual | ||
| 84 | +count was one symptom, not the site. | ||
| 85 | + | ||
| 86 | +## Fix | ||
| 87 | + | ||
| 88 | +One helper, both call sites (`decode.rs`): | ||
| 89 | + | ||
| 90 | +```rust | ||
| 91 | +fn ref_chroma_shape(len: usize, width: usize, height: usize) -> (usize, usize) { | ||
| 92 | + if len == width * height { (width, height) } // 4:4:4 | ||
| 93 | + else if len == (width / 2) * height { (width / 2, height) } // 4:2:2 | ||
| 94 | + else { (width / 2, height / 2) } // 4:2:0 | ||
| 95 | +} | ||
| 96 | +``` | ||
| 97 | + | ||
| 98 | +The sample count is exact and unambiguous for all three chroma formats, so | ||
| 99 | +no new field has to be threaded. `Picture`'s `u`/`v` doc comment | ||
| 100 | +(`encode.rs`) was corrected to state the real contract (half width, the | ||
| 101 | +frame's own chroma height) — the inference above depends on it. | ||
| 102 | + | ||
| 103 | +**Untouched, provably:** the current frame's chroma planes were already | ||
| 104 | +`(width >> ss_x) x (height >> ss_y)` (`decode.rs:27313`, `41692`); the | ||
| 105 | +`4:2:0` group-tail chroma SKIP arm (its owning lane); `stream.rs`'s 4:2:2 | ||
| 106 | +header refusal (byte-identical, unconditional — the local | ||
| 107 | +`EC_AV1_ALLOW_422_PROBE` bypass was applied for the probe runs and | ||
| 108 | +REVERTED). | ||
| 109 | + | ||
| 110 | +## Verification | ||
| 111 | + | ||
| 112 | +- **Before/after on the named fixture** (`/tmp/i422/t422_2f.obu`, sha256 | ||
| 113 | + `d166ebf3c1bde8c32152cd9d077a317b4c7205b6538f0a7481e09fb6333ffdf4`; | ||
| 114 | + decode_probe + `EC_AV1_{PREFILT,POSTDEBLOCK,POSTCDEF}_DUMP` vs the | ||
| 115 | + oracle's): frame 0 exact before and after; frame 1 before: Y 0, U 2231 | ||
| 116 | + (first (88,104) ours 206 / oracle 202), V 2197 (first (88,104) ours 212 / | ||
| 117 | + oracle 231) — byte-identical to the parent lane's reported counts, so | ||
| 118 | + the reproduction is sound; frame 1 after: **0 / 0 / 0**. | ||
| 119 | +- **16-frame ladder** (`/tmp/i422/t422_16f.obu`, sha256 | ||
| 120 | + `dda96300d2d36ba9dc958febb033e2334a2caee137bcb286c0276f259e57d5b6`): | ||
| 121 | + **0 diffs on every plane of every frame 0..15 at prefilt, post-deblock | ||
| 122 | + and post-CDEF** (48 stage-frame-plane groups), and the final | ||
| 123 | + post-loop-restoration dump is **byte-identical (73728 B) for all 16 | ||
| 124 | + frames**. Re-measured after the probe-bypass revert/re-apply round trip: | ||
| 125 | + 48 groups, 0 diffs, 16/16 final. The oracle ladder was regenerated with | ||
| 126 | + the current build and is byte-identical to the parent lane's on every | ||
| 127 | + frame where both exist. | ||
| 128 | +- **4:2:0/4:4:4 identity, measured:** `ref_chroma_shape` takes the *same* | ||
| 129 | + branch as the old two-way test for both other formats — 4:4:4 hits | ||
| 130 | + `len == width * height` first, and a 4:2:0 plane's `(w/2) * (h/2)` can | ||
| 131 | + only equal `(w/2) * h` at `h == 0` — so they are value-identical by | ||
| 132 | + construction, and the gates confirm it: `cargo test -p ec-av1 --lib -- | ||
| 133 | + lossless decodes_sample_exact 444` (target dir | ||
| 134 | + `~/.cache/cargo-target-av1422stripwrite`) is **16 passed, 0 failed, 3 | ||
| 135 | + ignored** — `a_lossless_libaom_key_frame_decodes_sample_exact`, | ||
| 136 | + `a_lossless_libaom_inter_frame_decodes_sample_exact`, | ||
| 137 | + `a_real_aomenc_mixed_lossless_segment_frame_decodes_sample_exact`, | ||
| 138 | + `a_lossless_sb128_rect_intra_block_decodes_sample_exact`, | ||
| 139 | + `a_lossless_sb128_square_frame_clips_overhanging_chroma_tus`, | ||
| 140 | + `a_lossless_16x4_chroma_pair_repairs_the_measured_site`, | ||
| 141 | + `a_skipped_lossless_intrabc_rect_strip_zeroes_its_entropy_bands`, the | ||
| 142 | + WHT round-trip, the tile-layout dual-decoder sweep and the six | ||
| 143 | + 64x64-root encode/decode gates. | ||
| 144 | +- **Oracle instrumentation (measurement, not decode).** The oracle's | ||
| 145 | + `EC_AV1_POSTCDEF_DUMP` rung sat inside `if (!optimized_loop_restoration)`, | ||
| 146 | + so a frame with no CDEF and no superres produced **no** post-CDEF dump — | ||
| 147 | + frame 15 got none, which is why the parent lane's "f15 postcdef Y=5059" | ||
| 148 | + had no oracle counterpart to compare against (the file it names, | ||
| 149 | + `/tmp/i422/av1422mi24/postcdef.f15`, is 81920 B — this decoder's own | ||
| 150 | + padded dump, not an oracle dump). The rung is now a shared | ||
| 151 | + `ec_dump_postcdef()` called from both branches; the oracle confirms | ||
| 152 | + frame 15's post-CDEF equals its post-deblock (no CDEF on that frame), | ||
| 153 | + and our frame 15 post-CDEF is byte-identical to it. | ||
| 154 | +- `cargo check -p ec-av1 --all-targets` (target dir | ||
| 155 | + `~/.cache/cargo-target-av1422stripwrite`): **0 warnings, 0 errors**. | ||
| 156 | +- `git status` names `decode.rs`, `encode.rs` (doc comment) and this | ||
| 157 | + report. Every temporary rung (`EC_SW422` in the 4:2:2 per-piece arm and | ||
| 158 | + in `build_c`; `SUB48`/`SUB48D`/`SUB48P` in the oracle's | ||
| 159 | + `reconinter_template.inc`) was removed before the commit. | ||
| 160 | + | ||
| 161 | +## State | ||
| 162 | + | ||
| 163 | +- Commit on `lane-av1-422stripwrite` (no push): the `ref_chroma_shape` | ||
| 164 | + helper + its two call sites in `crates/ec-av1/src/decode.rs`, the | ||
| 165 | + `Picture` chroma-shape doc correction in `crates/ec-av1/src/encode.rs`, | ||
| 166 | + and this report. | ||
| 167 | +- **No residual to hand on.** The 4:2:2 stream is pixel-exact end to end | ||
| 168 | + on this fixture, so the 16-frame ladder is no longer a frontier. What | ||
| 169 | + remains for 4:2:2 is coverage, not correctness: the 4:2:2 header | ||
| 170 | + refusal is still unconditional, and every claim here rests on one | ||
| 171 | + 256x144 fixture (`aomenc` 4:2:2, `--limit` cut) whose inter content is | ||
| 172 | + skip-heavy. A second 4:2:2 fixture with real residuals, compound and | ||
| 173 | + warped motion above the vertical midpoint is what would justify lifting | ||
| 174 | + the refusal — not more work on this stream. | ||