forge
Explore
Log in

Don't you dare go Hollow

ec-av1: 422 reference chroma shape read off the sample count (ceil)

Browse files at this commit

9b0298785bb1e1c748b9e98e9fd17c400b8743baAhmet Kaan GümüşParent: 35d5774
Review findings closed:

1. The first cut inferred a 4:2:2 reference's half width with a bare
   `width / 2`, but the producer stores chroma through `round_ss`
   (decode.rs:25806) and `round_ss(dim, 1) == (dim + 1) >> 1` is a CEIL.
   The floor test passed every even-width case -- the pinned fixture's
   256-wide frame included -- and dropped every odd-width 4:2:2 reference
   into the 4:2:0 arm, i.e. the same defect one dimension over.
   `ref_chroma_shape` now tests `round_ss(width, 1) * height`. The 4:2:0
   arm stays at the old `(width / 2, height / 2)` verbatim.

2. No gate covered that arithmetic. Two unit tests added:
   `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 and heights, and
   `a_422_reference_claims_the_full_chroma_height` pins the t422 shape
   (256x144 luma -> 128x144 chroma, not the 72 rows the bug claimed).
   Mutation proof: with `half_w` back at `width / 2` the first test fails
   ("4:2:2 reference of 3x2 (chroma 4 samples) must route back to its own
   shape") and passes again on the restored ceil form.

3. The doc comment still described the counts with floor halvings; it now
   states the `round_ss` crops, the odd-width failure mode, and the
   measured ambiguity bound: brute-forced over every w,h in 1..=128 the
   three counts route correctly for every w >= 2 && h >= 2, and every
   misread is confined to w == 1 / h == 1 where the counts genuinely
   coincide and no length test could separate them.

Evidence: 16-frame t422 ladder re-run on this tree -- 0 diffs on every
plane of every frame 0..15 at prefilt, post-deblock and post-CDEF (48
stage-frame-plane groups) and the final post-loop-restoration dump
byte-identical for all 16 frames. Identity gates (own target dir):
17 passed, 0 failed, 3 ignored, 665 filtered out.

Untouched: the 4:2:2 header refusal in stream.rs (byte-identical,
unconditional; the local probe bypass was applied and reverted), the
group-tail chroma SKIP arm, and every temporary debug rung.
crates/ec-av1/src/decode.rsmodified+79 -14
@@ -30396,25 +30396,43 @@ fn whole_plane(data: &[u16], width: usize, height: usize) -> PlaneBuf<'_> {
30396 30396
30397 /// A reference picture's own chroma plane shape, `(width, height)`, from its30397 /// A reference picture's own chroma plane shape, `(width, height)`, from its
30398 /// luma size and its chroma sample count. A stored reference carries no30398 /// luma size and its chroma sample count. A stored reference carries no
30399-/// subsampling field, but the count is exact and unambiguous:30399+/// subsampling field, so the format is read back off the count -- and the
30400-/// `w * h` is 4:4:4, `(w / 2) * h` is 4:2:2, `(w / 2) * (h / 2)` is 4:2:0.30400+/// counts are the producer's own [`round_ss`] crops
30401+/// (`decode.rs:25806`), so the test must use `round_ss` too and not a bare
30402+/// halving: 4:4:4 stores `w * h`, 4:2:2 stores `round_ss(w, 1) * h` (half
30403+/// width CEIL, full height), 4:2:0 stores `round_ss(w, 1) * round_ss(h, 1)`.
30404+/// A floor test here silently misses every odd-width 4:2:2 reference and
30405+/// drops it into the 4:2:0 arm -- the same defect one dimension over.
30401 ///30406 ///
30402 /// The two-way test this replaces ("luma-sized, else a quarter") folded 4:2:230407 /// 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 --30408+/// into the 4:2:0 shape entirely, so a 4:2:2 reference plane claimed
30404-/// half the samples it actually holds. Every chroma MC reading such a plane30409+/// `h / 2` rows -- half the samples it actually holds. Every chroma MC
30405-/// then had its source row clamped against `true_height` (spec 7.11.3.4 /30410+/// reading such a plane then had its source row clamped against
30406-/// libaom's `dec_calc_subpel_params_and_extend` clamps to the reference's30411+/// `true_height` (spec 7.11.3.4 / libaom's
30407-/// valid area), which folded every chroma row at or past the frame's30412+/// `dec_calc_subpel_params_and_extend` clamps to the reference's valid
30408-/// midpoint back onto the last valid row: on the pinned t422 fixture30413+/// area), which folded every chroma row at or past the frame's midpoint
30409-/// frame 1, the 8x4 inter strip at luma (88, 208) MC-predicted chroma30414+/// back onto the last valid row: on the pinned t422 fixture frame 1, the
30410-/// (88, 104) from frame 0 with mv 0 and a pure copy, yet wrote30415+/// 8x4 inter strip at luma (88, 208) MC-predicted chroma (88, 104) from
30411-/// (206, 204, 202, 200) x4 rows where the reference holds30416+/// frame 0 with mv 0 and a pure copy, yet wrote (206, 204, 202, 200) x4
30412-/// (202, 204, 204, 167) / (185, 190, 190, 165) / ...30417+/// rows where the reference holds (202, 204, 204, 167) /
30418+/// (185, 190, 190, 165) / ...
30419+/// **Ambiguity, measured rather than argued away:** the inference reads a
30420+/// format off a length, so it is only as good as the lengths being
30421+/// distinct. Brute-forced against the producer's own `round_ss` crops over
30422+/// every `w, h` in `1..=128`, the three counts are distinct and routed
30423+/// correctly for **every `w >= 2 && h >= 2`**, and every misread is
30424+/// confined to `w == 1` or `h == 1` (a 1-row chroma plane reads as 4:2:2,
30425+/// a 1-column one as 4:4:4 -- the counts genuinely coincide there, so no
30426+/// length test can separate them). No stream this decoder accepts reaches
30427+/// either; the pinned fixture's chroma is 128x144. The 4:2:0 arm is
30428+/// deliberately left at the old `(w / 2, h / 2)`: over the same sweep,
30429+/// **zero** 4:2:0 shapes differ from what the old two-way test produced.
30413 fn ref_chroma_shape(len: usize, width: usize, height: usize) -> (usize, usize) {30430 fn ref_chroma_shape(len: usize, width: usize, height: usize) -> (usize, usize) {
30431+ let half_w = round_ss(width, 1);
30414 if len == width * height {30432 if len == width * height {
30415 (width, height)30433 (width, height)
30416- } else if len == (width / 2) * height {30434+ } else if len == half_w * height {
30417- (width / 2, height)30435+ (half_w, height)
30418 } else {30436 } else {
30419 (width / 2, height / 2)30437 (width / 2, height / 2)
30420 }30438 }
@@ -44554,6 +44572,53 @@ mod tests {
44554 RefPix::ready(refs)44572 RefPix::ready(refs)
44555 }44573 }
44556 44574
44575+ /// lane-av1422stripwrite: the reference chroma shape is read back off the
44576+ /// stored plane's own sample count, so the counts the PRODUCER writes
44577+ /// (`round_ss` crops, `decode.rs:25806`) must route back to the shape
44578+ /// they came from. A floor half-width here passes every even-width case
44579+ /// -- the pinned fixture's 256-wide frame included -- and silently drops
44580+ /// every odd-width 4:2:2 reference into the 4:2:0 arm, which is the
44581+ /// exact defect this lane fixed, one dimension over. So the table below
44582+ /// is driven off `round_ss`, not off hand-written numbers, and sweeps
44583+ /// odd and even widths.
44584+ #[test]
44585+ fn a_reference_chroma_sample_count_routes_back_to_its_own_format() {
44586+ for w in [2usize, 3, 4, 5, 8, 9, 16, 17, 64, 65, 128, 129] {
44587+ for h in [2usize, 3, 4, 5, 8, 9, 64, 65, 144, 145] {
44588+ let cases = [
44589+ ("4:4:4", round_ss(w, 0) * round_ss(h, 0), (w, h)),
44590+ ("4:2:2", round_ss(w, 1) * round_ss(h, 0), (round_ss(w, 1), h)),
44591+ (
44592+ "4:2:0",
44593+ round_ss(w, 1) * round_ss(h, 1),
44594+ (w / 2, h / 2),
44595+ ),
44596+ ];
44597+ for (name, len, want) in cases {
44598+ assert_eq!(
44599+ ref_chroma_shape(len, w, h),
44600+ want,
44601+ "{name} reference of {w}x{h} (chroma {len} samples) \
44602+ must route back to its own shape"
44603+ );
44604+ }
44605+ }
44606+ }
44607+ }
44608+
44609+ /// lane-av1422stripwrite: the whole point of the 4:2:2 arm is that it
44610+ /// claims the frame's FULL chroma height -- that is what stops every
44611+ /// chroma MC sourcing a row at or past the midpoint from being clamped
44612+ /// against `true_height`. Pinned on the t422 shape.
44613+ #[test]
44614+ fn a_422_reference_claims_the_full_chroma_height() {
44615+ // 256x144 luma at ss (1, 0): chroma is 128 wide and 144 rows tall.
44616+ let (cw, ch) = ref_chroma_shape(round_ss(256, 1) * 144, 256, 144);
44617+ assert_eq!((cw, ch), (128, 144));
44618+ // The bug's own signature: 72 rows claimed for 144 rows held.
44619+ assert!(ch > 144 / 2, "a 4:2:2 reference must not claim half its rows");
44620+ }
44621+
44557 /// [`last_only`] plus `GOLDEN_FRAME` (index 4 of the name-keyed array)44622 /// [`last_only`] plus `GOLDEN_FRAME` (index 4 of the name-keyed array)
44558 /// and `ALTREF_FRAME` (index 7, the frame two back, which the encoder44623 /// and `ALTREF_FRAME` (index 7, the frame two back, which the encoder
44559 /// keeps in the slot the current frame refreshes): what a frame this44624 /// keeps in the slot the current frame refreshes): what a frame this
lanes/av1422stripwrite.report.mdmodified+84 -35
@@ -2,7 +2,8 @@
2 # chroma write/anchor: a reference picture's chroma plane claimed2 # chroma write/anchor: a reference picture's chroma plane claimed
3 # `h / 2` rows, so every chroma MC sourced at or past the frame's3 # `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 decodes4 # midpoint was clamped; the pinned 16-frame t422 stream now decodes
5-# byte-exact5+# byte-exact (first cut FAILed on review for a floor half-width where
6+# the producer ceils — corrected, ladder re-run, still 48/48 + 16/16)
6 7
7 ## Outcome8 ## Outcome
8 9
@@ -68,12 +69,12 @@ let (cw, ch) = if full { (g.width, g.height) } else { (g.width / 2, g.height / 2
68 A stored reference carries no subsampling field, so the code inferred it69 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) and70 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 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 and72+`round_ss(w, 1) * h`, which matches neither, so it fell into the 4:2:0
72-claimed `h / 2` rows. The stored data was never wrong (the frame is stored73+branch and claimed `h / 2` rows. The stored data was never wrong (the frame
73-through `round_ss(fw, ss_x) x round_ss(fh, ss_y)`, `decode.rs:25806`, which74+is stored through `round_ss(fw, ss_x) x round_ss(fh, ss_y)`,
74-is 4:2:2-correct) — only the *declared* shape was, and75+`decode.rs:25806`, which is 4:2:2-correct) — only the *declared* shape was,
75-`whole_plane` copies it into `width`/`height`/`true_*`, which is what every76+and `whole_plane` copies it into `width`/`height`/`true_*`, which is what
76-chroma MC and every edge clamp reads.77+every chroma MC and every edge clamp reads.
77 78
78 Consequence: at 4:2:2, **every** inter chroma prediction whose source row79 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 every80 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`):
89 90
90 ```rust91 ```rust
91 fn ref_chroma_shape(len: usize, width: usize, height: usize) -> (usize, usize) {92 fn ref_chroma_shape(len: usize, width: usize, height: usize) -> (usize, usize) {
92- if len == width * height { (width, height) } // 4:4:493+ let half_w = round_ss(width, 1);
93- else if len == (width / 2) * height { (width / 2, height) } // 4:2:294+ if len == width * height { (width, height) } // 4:4:4
94- else { (width / 2, height / 2) } // 4:2:095+ else if len == half_w * height { (half_w, height) } // 4:2:2
96+ else { (width / 2, height / 2) } // 4:2:0
95 }97 }
96 ```98 ```
97 99
98-The sample count is exact and unambiguous for all three chroma formats, so100+The half width comes from `round_ss`, **not** a bare `width / 2`, because
99-no new field has to be threaded. `Picture`'s `u`/`v` doc comment101+that is what the producer used: a frame is stored through
100-(`encode.rs`) was corrected to state the real contract (half width, the102+`round_ss(fw, ss_x) x round_ss(fh, ss_y)` (`decode.rs:25806`) and
101-frame's own chroma height) — the inference above depends on it.103+`round_ss(dim, 1) == (dim + 1) >> 1` is a **ceil**. A floor test here
102-104+(this lane's first cut, corrected on review) silently misses every
103-**Untouched, provably:** the current frame's chroma planes were already105+odd-width 4:2:2 reference and drops it into the 4:2:0 arm — the same
106+defect one dimension over. The 4:2:0 arm is left at the old
107+`(width / 2, height / 2)` on purpose, so 4:2:0 keeps the exact shape it
108+always had.
109+
110+`Picture`'s `u`/`v` doc comment (`encode.rs`) was corrected to state the
111+real contract (half width, the frame's own chroma height) — the inference
112+above depends on it.
113+
114+**Untouched:** the current frame's chroma planes were already
104 `(width >> ss_x) x (height >> ss_y)` (`decode.rs:27313`, `41692`); the115 `(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:2116 `4:2:0` group-tail chroma SKIP arm (its owning lane); `stream.rs`'s 4:2:2
106 header refusal (byte-identical, unconditional — the local117 header refusal (byte-identical, unconditional — the local
@@ -125,22 +136,22 @@ REVERTED).
125 48 groups, 0 diffs, 16/16 final. The oracle ladder was regenerated with136 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 every137 the current build and is byte-identical to the parent lane's on every
127 frame where both exist.138 frame where both exist.
128-- **4:2:0/4:4:4 identity, measured:** `ref_chroma_shape` takes the *same*139+- **4:2:0/4:4:4 identity, measured three ways.**
129- branch as the old two-way test for both other formats — 4:4:4 hits140+ *By construction:* the inference's 4:4:4 arm is the old first test
130- `len == width * height` first, and a 4:2:0 plane's `(w/2) * (h/2)` can141+ unchanged, and the 4:2:0 arm is the old fallback `(w / 2, h / 2)`
131- only equal `(w/2) * h` at `h == 0` — so they are value-identical by142+ verbatim — the only thing between them is the inserted 4:2:2 test, which
132- construction, and the gates confirm it: `cargo test -p ec-av1 --lib --143+ can only fire on a count a 4:2:0 or 4:4:4 plane does not have.
133- lossless decodes_sample_exact 444` (target dir144+ *Brute-forced:* against the producer's own `round_ss` crops
134- `~/.cache/cargo-target-av1422stripwrite`) is **16 passed, 0 failed, 3145+ (`decode.rs:25806`) over every `w, h` in `1..=128`, all three formats
135- ignored** — `a_lossless_libaom_key_frame_decodes_sample_exact`,146+ route correctly for **every `w >= 2 && h >= 2`**, every misread is
136- `a_lossless_libaom_inter_frame_decodes_sample_exact`,147+ confined to `w == 1` or `h == 1` (where the counts genuinely coincide,
137- `a_real_aomenc_mixed_lossless_segment_frame_decodes_sample_exact`,148+ so no length test can separate them — not reachable by a decodable
138- `a_lossless_sb128_rect_intra_block_decodes_sample_exact`,149+ frame), and **zero** 4:2:0 shapes differ from what the old two-way test
139- `a_lossless_sb128_square_frame_clips_overhanging_chroma_tus`,150+ produced.
140- `a_lossless_16x4_chroma_pair_repairs_the_measured_site`,151+ *Gated:* `cargo test -p ec-av1 --lib -- lossless decodes_sample_exact
141- `a_skipped_lossless_intrabc_rect_strip_zeroes_its_entropy_bands`, the152+ 444 reference_chroma` in its own target dir
142- WHT round-trip, the tile-layout dual-decoder sweep and the six153+ `~/.cache/cargo-target-av1422stripwrite-gate` — see below for what that
143- 64x64-root encode/decode gates.154+ run is and is not evidence of.
144 - **Oracle instrumentation (measurement, not decode).** The oracle's155 - **Oracle instrumentation (measurement, not decode).** The oracle's
145 `EC_AV1_POSTCDEF_DUMP` rung sat inside `if (!optimized_loop_restoration)`,156 `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 —157 so a frame with no CDEF and no superres produced **no** post-CDEF dump —
@@ -151,8 +162,34 @@ REVERTED).
151 `ec_dump_postcdef()` called from both branches; the oracle confirms162 `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),163 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.164 and our frame 15 post-CDEF is byte-identical to it.
165+- **A gate run that was thrown away, and why.** An earlier identity-gate
166+ run on this branch was launched against the ceil form and then
167+ **invalidated by my own concurrent edits**: while it was in flight I added
168+ the two routing tests below and deliberately mutated `half_w` back to the
169+ floor form to prove the test non-vacuous. Its result was never delivered,
170+ and whatever it compiled was not the shipping source (`decode.rs`
171+ md5 `999348d786ca0a8e930010ee46184d87` is the tree the accepted run
172+ covers). It is recorded here as **no evidence** rather than quietly
173+ dropped, and the accepted run is a clean re-run in a dedicated target
174+ dir with nothing else of mine touching cargo. Not a target-dir lock: the
175+ other suites on the host each use their own
176+ `$HOME/.cache/av1-local-suite/target-*` dir.
154 - `cargo check -p ec-av1 --all-targets` (target dir177 - `cargo check -p ec-av1 --all-targets` (target dir
155 `~/.cache/cargo-target-av1422stripwrite`): **0 warnings, 0 errors**.178 `~/.cache/cargo-target-av1422stripwrite`): **0 warnings, 0 errors**.
179+- **Regression tests, and proof they are not vacuous.** Two unit tests in
180+ `decode.rs`'s test module, the only 4:2:2-reachable gate while the header
181+ refusal stands (no stream can reach the code path otherwise):
182+ `a_reference_chroma_sample_count_routes_back_to_its_own_format` drives
183+ all three formats off the producer's own `round_ss` crops over odd AND
184+ even widths (2,3,4,5,8,9,16,17,64,65,128,129) and heights
185+ (2,3,4,5,8,9,64,65,144,145), and
186+ `a_422_reference_claims_the_full_chroma_height` pins the t422 shape
187+ (256x144 -> chroma 128x144, explicitly not the 72 rows the bug claimed).
188+ **Mutation proof:** with `half_w` reverted to `width / 2` — the exact
189+ floor form this lane's first cut shipped and the review caught — the first
190+ test FAILS with `4:2:2 reference of 3x2 (chroma 4 samples) must route
191+ back to its own shape`, and passes again on the restored ceil form. So
192+ the test pins the defect, not the implementation.
156 - `git status` names `decode.rs`, `encode.rs` (doc comment) and this193 - `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 and194 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's195 in `build_c`; `SUB48`/`SUB48D`/`SUB48P` in the oracle's
@@ -161,9 +198,21 @@ REVERTED).
161 ## State198 ## State
162 199
163 - Commit on `lane-av1-422stripwrite` (no push): the `ref_chroma_shape`200 - Commit on `lane-av1-422stripwrite` (no push): the `ref_chroma_shape`
164- helper + its two call sites in `crates/ec-av1/src/decode.rs`, the201+ helper + its two call sites in `crates/ec-av1/src/decode.rs`, its two
165- `Picture` chroma-shape doc correction in `crates/ec-av1/src/encode.rs`,202+ routing regression tests, the `Picture` chroma-shape doc correction in
166- and this report.203+ `crates/ec-av1/src/encode.rs`, and this report. The first cut was FAILed
204+ on review for using a floor half-width where the producer ceils; the
205+ corrected helper, the ladder re-run (48/48 + 16/16 again) and the
206+ identity re-proof are all in this commit.
207+- **Merge note.** The sibling lane's `RefPix` three-way inference
208+ (`39f45c31`, lane-av1-422bigblock) edits the same two call sites. Both
209+ now agree on the arithmetic — the half width is `round_ss(w, 1)`, i.e.
210+ ceil, because that is what `decode.rs:25806` stores — so the union is a
211+ union, not a floor-vs-ceil fight. If that lane threads an explicit
212+ subsampling instead of inferring one from the length, prefer it and drop
213+ this helper; the 48/48 result is a property of the *shape* being right,
214+ not of how it is derived, so it survives either way. Re-run the 16-frame
215+ ladder after resolving.
167 - **No residual to hand on.** The 4:2:2 stream is pixel-exact end to end216 - **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. What217 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 header218 remains for 4:2:2 is coverage, not correctness: the 4:2:2 header