diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index c1165b2e..9cc5d2a0 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -30396,25 +30396,43 @@ fn whole_plane(data: &[u16], width: usize, height: usize) -> PlaneBuf<'_> { /// A reference picture's own chroma plane shape, `(width, height)`, from its /// luma size and its chroma sample count. A stored reference carries no -/// subsampling field, but the count is exact and unambiguous: -/// `w * h` is 4:4:4, `(w / 2) * h` is 4:2:2, `(w / 2) * (h / 2)` is 4:2:0. +/// subsampling field, so the format is read back off the count -- and the +/// counts are the producer's own [`round_ss`] crops +/// (`decode.rs:25806`), so the test must use `round_ss` too and not a bare +/// halving: 4:4:4 stores `w * h`, 4:2:2 stores `round_ss(w, 1) * h` (half +/// width CEIL, full height), 4:2:0 stores `round_ss(w, 1) * round_ss(h, 1)`. +/// A floor test here silently misses every odd-width 4:2:2 reference and +/// drops it into the 4:2:0 arm -- the same defect one dimension over. /// /// The two-way test this replaces ("luma-sized, else a quarter") folded 4:2:2 -/// into the 4:2:0 shape, so a 4:2:2 reference plane claimed `h / 2` rows -- -/// half the samples it actually holds. Every chroma MC reading such a plane -/// then had its source row clamped against `true_height` (spec 7.11.3.4 / -/// libaom's `dec_calc_subpel_params_and_extend` clamps to the reference's -/// valid area), which folded every chroma row at or past the frame's -/// midpoint back onto the last valid row: on the pinned t422 fixture -/// frame 1, the 8x4 inter strip at luma (88, 208) MC-predicted chroma -/// (88, 104) from frame 0 with mv 0 and a pure copy, yet wrote -/// (206, 204, 202, 200) x4 rows where the reference holds -/// (202, 204, 204, 167) / (185, 190, 190, 165) / ... +/// into the 4:2:0 shape entirely, so a 4:2:2 reference plane claimed +/// `h / 2` rows -- half the samples it actually holds. Every chroma MC +/// reading such a plane then had its source row clamped against +/// `true_height` (spec 7.11.3.4 / libaom's +/// `dec_calc_subpel_params_and_extend` clamps to the reference's valid +/// area), which folded every chroma row at or past the frame's midpoint +/// back onto the last valid row: on the pinned t422 fixture frame 1, the +/// 8x4 inter strip at luma (88, 208) MC-predicted chroma (88, 104) from +/// frame 0 with mv 0 and a pure copy, yet wrote (206, 204, 202, 200) x4 +/// rows where the reference holds (202, 204, 204, 167) / +/// (185, 190, 190, 165) / ... +/// **Ambiguity, measured rather than argued away:** the inference reads a +/// format off a length, so it is only as good as the lengths being +/// distinct. Brute-forced against the producer's own `round_ss` crops over +/// every `w, h` in `1..=128`, the three counts are distinct and routed +/// correctly for **every `w >= 2 && h >= 2`**, and every misread is +/// confined to `w == 1` or `h == 1` (a 1-row chroma plane reads as 4:2:2, +/// a 1-column one as 4:4:4 -- the counts genuinely coincide there, so no +/// length test can separate them). No stream this decoder accepts reaches +/// either; the pinned fixture's chroma is 128x144. The 4:2:0 arm is +/// deliberately left at the old `(w / 2, h / 2)`: over the same sweep, +/// **zero** 4:2:0 shapes differ from what the old two-way test produced. fn ref_chroma_shape(len: usize, width: usize, height: usize) -> (usize, usize) { + let half_w = round_ss(width, 1); if len == width * height { (width, height) - } else if len == (width / 2) * height { - (width / 2, height) + } else if len == half_w * height { + (half_w, height) } else { (width / 2, height / 2) } @@ -44554,6 +44572,53 @@ mod tests { RefPix::ready(refs) } + /// lane-av1422stripwrite: the reference chroma shape is read back off the + /// stored plane's own sample count, so the counts the PRODUCER writes + /// (`round_ss` crops, `decode.rs:25806`) must route back to the shape + /// they came from. A floor half-width here passes every even-width case + /// -- the pinned fixture's 256-wide frame included -- and silently drops + /// every odd-width 4:2:2 reference into the 4:2:0 arm, which is the + /// exact defect this lane fixed, one dimension over. So the table below + /// is driven off `round_ss`, not off hand-written numbers, and sweeps + /// odd and even widths. + #[test] + fn a_reference_chroma_sample_count_routes_back_to_its_own_format() { + for w in [2usize, 3, 4, 5, 8, 9, 16, 17, 64, 65, 128, 129] { + for h in [2usize, 3, 4, 5, 8, 9, 64, 65, 144, 145] { + let cases = [ + ("4:4:4", round_ss(w, 0) * round_ss(h, 0), (w, h)), + ("4:2:2", round_ss(w, 1) * round_ss(h, 0), (round_ss(w, 1), h)), + ( + "4:2:0", + round_ss(w, 1) * round_ss(h, 1), + (w / 2, h / 2), + ), + ]; + for (name, len, want) in cases { + assert_eq!( + ref_chroma_shape(len, w, h), + want, + "{name} reference of {w}x{h} (chroma {len} samples) \ + must route back to its own shape" + ); + } + } + } + } + + /// lane-av1422stripwrite: the whole point of the 4:2:2 arm is that it + /// claims the frame's FULL chroma height -- that is what stops every + /// chroma MC sourcing a row at or past the midpoint from being clamped + /// against `true_height`. Pinned on the t422 shape. + #[test] + fn a_422_reference_claims_the_full_chroma_height() { + // 256x144 luma at ss (1, 0): chroma is 128 wide and 144 rows tall. + let (cw, ch) = ref_chroma_shape(round_ss(256, 1) * 144, 256, 144); + assert_eq!((cw, ch), (128, 144)); + // The bug's own signature: 72 rows claimed for 144 rows held. + assert!(ch > 144 / 2, "a 4:2:2 reference must not claim half its rows"); + } + /// [`last_only`] plus `GOLDEN_FRAME` (index 4 of the name-keyed array) /// and `ALTREF_FRAME` (index 7, the frame two back, which the encoder /// keeps in the slot the current frame refreshes): what a frame this diff --git a/lanes/av1422stripwrite.report.md b/lanes/av1422stripwrite.report.md index 84a194ce..f713d864 100644 --- a/lanes/av1422stripwrite.report.md +++ b/lanes/av1422stripwrite.report.md @@ -2,7 +2,8 @@ # chroma write/anchor: a reference picture's chroma plane claimed # `h / 2` rows, so every chroma MC sourced at or past the frame's # midpoint was clamped; the pinned 16-frame t422 stream now decodes -# byte-exact +# byte-exact (first cut FAILed on review for a floor half-width where +# the producer ceils — corrected, ladder re-run, still 48/48 + 16/16) ## Outcome @@ -68,12 +69,12 @@ let (cw, ch) = if full { (g.width, g.height) } else { (g.width / 2, g.height / 2 A stored reference carries no subsampling field, so the code inferred it from the sample count — but the inference only knew 4:4:4 (luma-sized) and 4:2:0 (a quarter). A 4:2:2 reference is **half-width, full-height**: -`(w/2) * h`, which matches neither, so it fell into the 4:2:0 branch and -claimed `h / 2` rows. The stored data was never wrong (the frame is stored -through `round_ss(fw, ss_x) x round_ss(fh, ss_y)`, `decode.rs:25806`, which -is 4:2:2-correct) — only the *declared* shape was, and -`whole_plane` copies it into `width`/`height`/`true_*`, which is what every -chroma MC and every edge clamp reads. +`round_ss(w, 1) * h`, which matches neither, so it fell into the 4:2:0 +branch and claimed `h / 2` rows. The stored data was never wrong (the frame +is stored through `round_ss(fw, ss_x) x round_ss(fh, ss_y)`, +`decode.rs:25806`, which is 4:2:2-correct) — only the *declared* shape was, +and `whole_plane` copies it into `width`/`height`/`true_*`, which is what +every chroma MC and every edge clamp reads. Consequence: at 4:2:2, **every** inter chroma prediction whose source row was at or past the frame's vertical midpoint read clamped garbage, on every @@ -89,18 +90,28 @@ One helper, both call sites (`decode.rs`): ```rust fn ref_chroma_shape(len: usize, width: usize, height: usize) -> (usize, usize) { - if len == width * height { (width, height) } // 4:4:4 - else if len == (width / 2) * height { (width / 2, height) } // 4:2:2 - else { (width / 2, height / 2) } // 4:2:0 + let half_w = round_ss(width, 1); + if len == width * height { (width, height) } // 4:4:4 + else if len == half_w * height { (half_w, height) } // 4:2:2 + else { (width / 2, height / 2) } // 4:2:0 } ``` -The sample count is exact and unambiguous for all three chroma formats, so -no new field has to be threaded. `Picture`'s `u`/`v` doc comment -(`encode.rs`) was corrected to state the real contract (half width, the -frame's own chroma height) — the inference above depends on it. - -**Untouched, provably:** the current frame's chroma planes were already +The half width comes from `round_ss`, **not** a bare `width / 2`, because +that is what the producer used: a frame is stored through +`round_ss(fw, ss_x) x round_ss(fh, ss_y)` (`decode.rs:25806`) and +`round_ss(dim, 1) == (dim + 1) >> 1` is a **ceil**. A floor test here +(this lane's first cut, corrected on review) silently misses every +odd-width 4:2:2 reference and drops it into the 4:2:0 arm — the same +defect one dimension over. The 4:2:0 arm is left at the old +`(width / 2, height / 2)` on purpose, so 4:2:0 keeps the exact shape it +always had. + +`Picture`'s `u`/`v` doc comment (`encode.rs`) was corrected to state the +real contract (half width, the frame's own chroma height) — the inference +above depends on it. + +**Untouched:** the current frame's chroma planes were already `(width >> ss_x) x (height >> ss_y)` (`decode.rs:27313`, `41692`); the `4:2:0` group-tail chroma SKIP arm (its owning lane); `stream.rs`'s 4:2:2 header refusal (byte-identical, unconditional — the local @@ -125,22 +136,22 @@ REVERTED). 48 groups, 0 diffs, 16/16 final. The oracle ladder was regenerated with the current build and is byte-identical to the parent lane's on every frame where both exist. -- **4:2:0/4:4:4 identity, measured:** `ref_chroma_shape` takes the *same* - branch as the old two-way test for both other formats — 4:4:4 hits - `len == width * height` first, and a 4:2:0 plane's `(w/2) * (h/2)` can - only equal `(w/2) * h` at `h == 0` — so they are value-identical by - construction, and the gates confirm it: `cargo test -p ec-av1 --lib -- - lossless decodes_sample_exact 444` (target dir - `~/.cache/cargo-target-av1422stripwrite`) is **16 passed, 0 failed, 3 - ignored** — `a_lossless_libaom_key_frame_decodes_sample_exact`, - `a_lossless_libaom_inter_frame_decodes_sample_exact`, - `a_real_aomenc_mixed_lossless_segment_frame_decodes_sample_exact`, - `a_lossless_sb128_rect_intra_block_decodes_sample_exact`, - `a_lossless_sb128_square_frame_clips_overhanging_chroma_tus`, - `a_lossless_16x4_chroma_pair_repairs_the_measured_site`, - `a_skipped_lossless_intrabc_rect_strip_zeroes_its_entropy_bands`, the - WHT round-trip, the tile-layout dual-decoder sweep and the six - 64x64-root encode/decode gates. +- **4:2:0/4:4:4 identity, measured three ways.** + *By construction:* the inference's 4:4:4 arm is the old first test + unchanged, and the 4:2:0 arm is the old fallback `(w / 2, h / 2)` + verbatim — the only thing between them is the inserted 4:2:2 test, which + can only fire on a count a 4:2:0 or 4:4:4 plane does not have. + *Brute-forced:* against the producer's own `round_ss` crops + (`decode.rs:25806`) over every `w, h` in `1..=128`, all three formats + route correctly for **every `w >= 2 && h >= 2`**, every misread is + confined to `w == 1` or `h == 1` (where the counts genuinely coincide, + so no length test can separate them — not reachable by a decodable + frame), and **zero** 4:2:0 shapes differ from what the old two-way test + produced. + *Gated:* `cargo test -p ec-av1 --lib -- lossless decodes_sample_exact + 444 reference_chroma` in its own target dir + `~/.cache/cargo-target-av1422stripwrite-gate` — see below for what that + run is and is not evidence of. - **Oracle instrumentation (measurement, not decode).** The oracle's `EC_AV1_POSTCDEF_DUMP` rung sat inside `if (!optimized_loop_restoration)`, so a frame with no CDEF and no superres produced **no** post-CDEF dump — @@ -151,8 +162,34 @@ REVERTED). `ec_dump_postcdef()` called from both branches; the oracle confirms frame 15's post-CDEF equals its post-deblock (no CDEF on that frame), and our frame 15 post-CDEF is byte-identical to it. +- **A gate run that was thrown away, and why.** An earlier identity-gate + run on this branch was launched against the ceil form and then + **invalidated by my own concurrent edits**: while it was in flight I added + the two routing tests below and deliberately mutated `half_w` back to the + floor form to prove the test non-vacuous. Its result was never delivered, + and whatever it compiled was not the shipping source (`decode.rs` + md5 `999348d786ca0a8e930010ee46184d87` is the tree the accepted run + covers). It is recorded here as **no evidence** rather than quietly + dropped, and the accepted run is a clean re-run in a dedicated target + dir with nothing else of mine touching cargo. Not a target-dir lock: the + other suites on the host each use their own + `$HOME/.cache/av1-local-suite/target-*` dir. - `cargo check -p ec-av1 --all-targets` (target dir `~/.cache/cargo-target-av1422stripwrite`): **0 warnings, 0 errors**. +- **Regression tests, and proof they are not vacuous.** Two unit tests in + `decode.rs`'s test module, the only 4:2:2-reachable gate while the header + refusal stands (no stream can reach the code path otherwise): + `a_reference_chroma_sample_count_routes_back_to_its_own_format` drives + all three formats off the producer's own `round_ss` crops over odd AND + even widths (2,3,4,5,8,9,16,17,64,65,128,129) and heights + (2,3,4,5,8,9,64,65,144,145), and + `a_422_reference_claims_the_full_chroma_height` pins the t422 shape + (256x144 -> chroma 128x144, explicitly not the 72 rows the bug claimed). + **Mutation proof:** with `half_w` reverted to `width / 2` — the exact + floor form this lane's first cut shipped and the review caught — the first + test FAILS with `4:2:2 reference of 3x2 (chroma 4 samples) must route + back to its own shape`, and passes again on the restored ceil form. So + the test pins the defect, not the implementation. - `git status` names `decode.rs`, `encode.rs` (doc comment) and this report. Every temporary rung (`EC_SW422` in the 4:2:2 per-piece arm and in `build_c`; `SUB48`/`SUB48D`/`SUB48P` in the oracle's @@ -161,9 +198,21 @@ REVERTED). ## State - Commit on `lane-av1-422stripwrite` (no push): the `ref_chroma_shape` - helper + its two call sites in `crates/ec-av1/src/decode.rs`, the - `Picture` chroma-shape doc correction in `crates/ec-av1/src/encode.rs`, - and this report. + helper + its two call sites in `crates/ec-av1/src/decode.rs`, its two + routing regression tests, the `Picture` chroma-shape doc correction in + `crates/ec-av1/src/encode.rs`, and this report. The first cut was FAILed + on review for using a floor half-width where the producer ceils; the + corrected helper, the ladder re-run (48/48 + 16/16 again) and the + identity re-proof are all in this commit. +- **Merge note.** The sibling lane's `RefPix` three-way inference + (`39f45c31`, lane-av1-422bigblock) edits the same two call sites. Both + now agree on the arithmetic — the half width is `round_ss(w, 1)`, i.e. + ceil, because that is what `decode.rs:25806` stores — so the union is a + union, not a floor-vs-ceil fight. If that lane threads an explicit + subsampling instead of inferring one from the length, prefer it and drop + this helper; the 48/48 result is a property of the *shape* being right, + not of how it is derived, so it survives either way. Re-run the 16-frame + ladder after resolving. - **No residual to hand on.** The 4:2:2 stream is pixel-exact end to end on this fixture, so the 16-frame ladder is no longer a frontier. What remains for 4:2:2 is coverage, not correctness: the 4:2:2 header