lane-av1422grain: the report -- measured, ported, byte-exact, negative-controlled
Browse files at this commita8377a2928fa7b9c8e3c9a86cc735418e1bd8335Parent: 55a71871 changed file +314 -0
- lanes/av1422grain.report.md +314 -0
lanes/av1422grain.report.mdadded+314 -0
| @@ -0,0 +1,314 @@ | ||
| 1 | +# lane-av1422grain — `film_grain.rs` is per-axis; 4:2:2 and 4:4:4 grain are byte-exact vs ffmpeg | |
| 2 | + | |
| 3 | +**Target:** `crates/ec-av1/src/film_grain.rs`, the one module the lift study | |
| 4 | +(`lanes/av1422liftrisk.report.md` §1b/§2 item 1) found still 4:2:0-shaped. | |
| 5 | +**Branch:** `lane/av1422grain`, commit `64549b09`. Base `228a55f5`. | |
| 6 | +**Changed file:** `crates/ec-av1/src/film_grain.rs` only. Nothing else touched. | |
| 7 | + | |
| 8 | +--- | |
| 9 | + | |
| 10 | +## 1. THE MEASUREMENT — aomenc DOES emit 4:2:2 film grain | |
| 11 | + | |
| 12 | +The assignment's branch 3 (assertion + "cannot be produced") does **not** apply. | |
| 13 | +The cell is codable, so this is branch 2: port and prove. | |
| 14 | + | |
| 15 | +**Encoder:** `/home/user/.cache/aom-oracle/build/aomenc` (libaom, oracle | |
| 16 | +snapshot `9bb526a`). | |
| 17 | + | |
| 18 | +```sh | |
| 19 | +# 4:2:2 source | |
| 20 | +ffmpeg -f lavfi -i testsrc2=size=320x240:rate=1:duration=1 \ | |
| 21 | + -pix_fmt yuv422p -f yuv4mpegpipe src422.y4m | |
| 22 | + | |
| 23 | +# the encode under test | |
| 24 | +aomenc src422.y4m --profile=2 --film-grain-test=5 --obu -o g422_t5.obu | |
| 25 | +``` | |
| 26 | + | |
| 27 | +**Result: exit 0, 10124 bytes, no diagnostic of any kind.** The encoder said | |
| 28 | +nothing because nothing was wrong — it simply emitted the cell. | |
| 29 | + | |
| 30 | +The proof that the frame header really carries 4:2:2 grain: | |
| 31 | + | |
| 32 | +```sh | |
| 33 | +ffprobe -v error -export_side_data +film_grain -show_frames -of json g422_t5.obu | |
| 34 | +``` | |
| 35 | +```json | |
| 36 | +"side_data_type": "Film grain parameters", | |
| 37 | +"seed": "7825", "width": 320, "height": 240, | |
| 38 | +"subsampling_x": 1, "subsampling_y": 0, | |
| 39 | +"chroma_scaling_from_luma": 0, "scaling_shift": 11, | |
| 40 | +"ar_coeff_lag": 3, "ar_coeff_shift": 7 | |
| 41 | +``` | |
| 42 | + | |
| 43 | +`subsampling_x: 1, subsampling_y: 0` is 4:2:2, with a **full** point set | |
| 44 | +(`ar_coeff_lag 3` is the maximum, so every AR tap and both `avgLuma` paths are | |
| 45 | +exercised — the params are not a degenerate no-op). The identical encode | |
| 46 | +without `--film-grain-test` produces 9998 bytes and ffprobe reports **zero** | |
| 47 | +`Film grain parameters` side-data blocks, so the flag is what puts grain in. | |
| 48 | + | |
| 49 | +**4:4:4 is also live**, which the assignment did not anticipate and which | |
| 50 | +changes the shape of the fix (see §3): | |
| 51 | + | |
| 52 | +```sh | |
| 53 | +aomenc src444.y4m --profile=1 --film-grain-test=5 --obu -o g444_t5.obu | |
| 54 | +# 15541 bytes; ffprobe: profile "High", pix_fmt yuv444p, 1x Film grain parameters | |
| 55 | +``` | |
| 56 | + | |
| 57 | +So this module had **two** silently-wrong arms, not one, and a port that | |
| 58 | +special-cased only `ss_y` would have fixed 4:2:2 and left 4:4:4 broken. The | |
| 59 | +port is per-axis on both. | |
| 60 | + | |
| 61 | +**Cells built** (all in `/home/user/.cache/av1422grain/`): | |
| 62 | + | |
| 63 | +| cell | encode | size | | |
| 64 | +|---|---|---| | |
| 65 | +| `g422_t5.obu` | `--profile=2 --film-grain-test=5` (8-bit 4:2:2) | 10124 | | |
| 66 | +| `g422_t11.obu` | `--profile=2 --film-grain-test=11` (8-bit 4:2:2) | 10124 | | |
| 67 | +| `g422_10_t5.obu` | `--profile=2 --input-bit-depth=10 --bit-depth=10 --film-grain-test=5` | 13792 | | |
| 68 | +| `g444_t5.obu` | `--profile=1 --film-grain-test=5` (8-bit 4:4:4) | 15541 | | |
| 69 | + | |
| 70 | +Two grain vectors at 8-bit so the result is not one lucky seed; one at 10-bit so | |
| 71 | +the bit-depth-generic path (`grain_range`, `scale_lut`, the `<< (bit_depth - 8)` | |
| 72 | +offsets) is covered at a second depth. | |
| 73 | + | |
| 74 | +--- | |
| 75 | + | |
| 76 | +## 2. WHAT WAS ACTUALLY WRONG (the study's list, confirmed and corrected) | |
| 77 | + | |
| 78 | +The study listed 9 sites. All 9 were real. Two of its characterisations needed | |
| 79 | +correcting, and both corrections are load-bearing: | |
| 80 | + | |
| 81 | +* **`:577 n = min(..., luma.len()/2)`** — the study calls this "halves the luma | |
| 82 | + operand". True, and it is right at 4:2:2 (where the operand IS a horizontal | |
| 83 | + pair) but wrong at **4:4:4**, where the operand is the bare luma sample. So | |
| 84 | + the fix is not "divide by 2", it is a `pair` flag. | |
| 85 | +* **`:351 c_stride = width/2`** — the study says "right for WIDTH at 4:2:2". It | |
| 86 | + is right at 4:2:2 and wrong at 4:4:4, where it must be `width`. | |
| 87 | + | |
| 88 | +The study also says the 4:2:2 failure is "the top half of the chroma plane | |
| 89 | +grained with a 4:2:0 row map and a 2x2 `avgLuma` quad". Confirmed, and §5 | |
| 90 | +quantifies it. | |
| 91 | + | |
| 92 | +## 3. THE PORT — libaom source lines | |
| 93 | + | |
| 94 | +This libaom snapshot has **no** `generate_grain_uv_422` and **no** | |
| 95 | +`av1_apply_grain_422`; the assignment's premise about those function names is | |
| 96 | +from an older libaom. What this version does is thread `chroma_subsamp_y` / | |
| 97 | +`chroma_subsamp_x` through one unified `add_film_grain_run` | |
| 98 | +(`av1/decoder/grain_synthesis.c:987`), set per pixel format in | |
| 99 | +`av1_add_film_grain` (`:1386-1420`, which lists `AOM_IMG_FMT_I422` and | |
| 100 | +`I42216` explicitly). **Read the C, not the function names** — every site below | |
| 101 | +is transcribed from this snapshot. | |
| 102 | + | |
| 103 | +| ours | C | what | | |
| 104 | +|---|---|---| | |
| 105 | +| `Ss::csub_x/y` | `:1018-1019` | `chroma_subblock_size_x/y = 32 >> chroma_subsamp` | | |
| 106 | +| `Ss::cblock_w/h` | `:1031-1035` | template size, `(2 >> ss) * ar_padding` terms | | |
| 107 | +| `c_stride` | `:1461` | `width >> chroma_subsamp_x` (right for 4:4:4 only here) | | |
| 108 | +| `chroma_row(y)` | `:1129` | chroma row of block row `y` is `y*2 >> ss_y` | | |
| 109 | +| `chroma_off_y/x` | `:1091-1094` | `top_pad + (2>>ss_y)*ar_pad + offset_y*(2>>ss_y)` | | |
| 110 | +| `cy0/cx0` | `:1129-1130` | `py0 >> ss_y`, `px0 >> ss_x` (not `/2`) | | |
| 111 | +| `ch/chw` block | `:680-681` | `half_h << (1-ss_y)` by `half_w << (1-ss_x)` | | |
| 112 | +| `avgLuma` in template | `:554-565` | `(ss_y+1) x (ss_x+1)` samples, `((1<<(ss_y+ss_x))>>1)` rounding | | |
| 113 | +| `average_luma` in apply | `:683-691` | horizontal pair at `ss_x==1`, bare sample at `ss_x==0` | | |
| 114 | +| line/col buffers | `:392-414` | `chroma_stride*(2>>ss_y)`, `(csub_y+(2>>ss_y))*(2>>ss_x)` | | |
| 115 | +| `ver_boundary_overlap` chroma | `:1102-1120` | width `2>>ss_x`, height `AOMMIN(csub_y+(2>>ss_y), (height-2y)>>ss_y)` | | |
| 116 | +| `hor_boundary_overlap` chroma | `:1157-1200` | height `2>>ss_y` | | |
| 117 | +| four chroma `copy_area`s | `:1289-1296`, `:1309-1320`, `:1345-1352` | per-axis extents | | |
| 118 | + | |
| 119 | +### Three traps the port walks into | |
| 120 | + | |
| 121 | +Each of these cost a real debugging cycle and is recorded at the site. | |
| 122 | + | |
| 123 | +**(a) The chroma BLOCK size is `16 << (1 - ss)`, not `chroma_subblock_size << (1 - ss)`.** | |
| 124 | +`add_noise_to_block` is handed `half_luma_height = AOMMIN(32 >> 1, ...)` and | |
| 125 | +`half_luma_width = AOMMIN(32 >> 1, ...)` (`:1254-1255`) — **luma** half-extents | |
| 126 | +— and scales them per axis inside (`:680-681`). So the block is | |
| 127 | +`16 << (1-ss)`: 16x16 at 4:2:0, **32x16 at 4:2:2**, **32x32 at 4:4:4**. The | |
| 128 | +tempting `chroma_subblock_size << (1 - ss)` agrees at 4:2:0 and diverges | |
| 129 | +wherever `chroma_subsamp == 0` (4:2:2 rows, 4:4:4 both axes) — the two differ | |
| 130 | +by a factor of 2. Getting this wrong is an out-of-bounds read, not a wrong | |
| 131 | +pixel. | |
| 132 | + | |
| 133 | +**(b) The block-column walk is in LUMA units at every subsampling.** The C is | |
| 134 | +`for (x = 0; x < width / 2; x += luma_subblock_size_x >> 1)` (`:1075`) and | |
| 135 | +`for (y = 0; y < height / 2; y += luma_subblock_size_y >> 1)` (`:1069`). | |
| 136 | +`width/2` and `height/2` are correct as written and must NOT become | |
| 137 | +`c_stride`; only the chroma ORIGIN and EXTENT derived from them are per-axis. | |
| 138 | +Making the walk itself per-axis is the single easiest way to break 4:2:0. | |
| 139 | + | |
| 140 | +**(c) `copy_area(cb_col_buf + (chroma_subblock_size_y << (1 - ss_x)), ...)` | |
| 141 | +(`:1289`) is a FLAT element offset, not a row index.** `copy_area` here | |
| 142 | +addresses `src_row0 * src_stride + src_col0`. Passing the C's flat value as the | |
| 143 | +row index multiplies it by the stride a second time and reads past the buffer | |
| 144 | +(observed: `len is 68 but the index is 128` at 4:4:4). The correct row index is | |
| 145 | +`csub_y`, because `src_stride == 2 >> ss_x` and the flat offset is | |
| 146 | +`csub_y << (1 - ss_x)`, which are the same number for every `ss_x`. Recorded | |
| 147 | +in a comment at the call site. | |
| 148 | + | |
| 149 | +### Two blend formulas, not one | |
| 150 | + | |
| 151 | +`ver_boundary_overlap` (`:912-939`) and `hor_boundary_overlap` (`:941-970`) | |
| 152 | +each have a `width/height == 1` arm and a `== 2` arm with **different | |
| 153 | +coefficients**: 23/22 for one, 27/17 and 17/27 for two. At 4:2:0 the `2 >> ss` | |
| 154 | +terms are both 1, so only the first arm ever ran and the second was dead code | |
| 155 | +that had never been exercised. It is live at 4:2:2 (rows) and 4:4:4 (both | |
| 156 | +axes), and the 17/27 second row is a genuinely different value. `film_grain.rs` | |
| 157 | +already had both arms (`ver_overlap_inplace`, `hor_overlap_inplace`); the port | |
| 158 | +is what makes them reachable. | |
| 159 | + | |
| 160 | +--- | |
| 161 | + | |
| 162 | +## 4. NON-VACUITY — the comparator was proven to bite | |
| 163 | + | |
| 164 | +`stream.rs:16626-16629` warns that a params-without-apply stream is the known | |
| 165 | +false green. Three independent guards, all live: | |
| 166 | + | |
| 167 | +1. **`grain_hits() > 0`** — asserted per cell, delta printed: | |
| 168 | + `0->1`, `1->2`, `2->3`, `3->4`. A stream carrying `apply_grain` that never | |
| 169 | + ran synthesis fails here. | |
| 170 | +2. **The header really carries grain** — the test re-parses the OBUs and | |
| 171 | + asserts some frame header has `film_grain.apply_grain`. | |
| 172 | +3. **The comparator bites** — one flipped oracle sample per plane, then the | |
| 173 | + expected plane-difference tuple is asserted to be exactly `(1,1,1)`: | |
| 174 | + | |
| 175 | +``` | |
| 176 | +TAMPER g422_t5.obu: plane diffs must be exactly (1,1,1), got (1,1,1) | |
| 177 | +TAMPER g422_t11.obu: plane diffs must be exactly (1,1,1), got (1,1,1) | |
| 178 | +TAMPER g422_10_t5.obu: plane diffs must be exactly (1,1,1), got (1,1,1) | |
| 179 | +``` | |
| 180 | + | |
| 181 | +A zero here would have meant the plane comparison was not reading what it | |
| 182 | +claims to. | |
| 183 | + | |
| 184 | +## 5. NEGATIVE CONTROL — the verdict is produced by this port | |
| 185 | + | |
| 186 | +Forcing `Ss::read` back to the old hardcoded `Ss { x: 1, y: 1 }` (the pre-lane | |
| 187 | +geometry, everything else identical) makes the cell **fail** against ffmpeg: | |
| 188 | + | |
| 189 | +``` | |
| 190 | +g422_t5.obu frame 0: luma/u/v samples differing from ffmpeg | |
| 191 | + left: (0, 23370, 22631) right: (0, 0, 0) | |
| 192 | +``` | |
| 193 | + | |
| 194 | +Read that shape carefully, it is the whole defect in one line: **luma exact, | |
| 195 | +~30% of each chroma plane wrong**. No panic, no counter, no refusal — exactly | |
| 196 | +the silent-wrong-picture failure the study predicted. (30% rather than ~50% | |
| 197 | +because a wrong grain delta is frequently zero, so many mis-grained samples | |
| 198 | +coincide with the oracle.) | |
| 199 | + | |
| 200 | +## 6. THE RESULT | |
| 201 | + | |
| 202 | +Every plane of every frame, `decode_stream` vs ffmpeg's own decode of the same | |
| 203 | +bytes, 4:2:2 chroma planes split by sequence-header geometry | |
| 204 | +(`yuv422p` / `yuv422p10le`; 10-bit read as u16 little-endian, never | |
| 205 | +u8-narrowed): | |
| 206 | + | |
| 207 | +``` | |
| 208 | +g422_t5.obu byte-exact, 1 frames, grain_hits 0->1 | |
| 209 | +g422_t11.obu byte-exact, 1 frames, grain_hits 1->2 | |
| 210 | +g422_10_t5.obu byte-exact, 1 frames, grain_hits 2->3 | |
| 211 | +g444_t5.obu byte-exact, 1 frames, grain_hits 3->4 | |
| 212 | +``` | |
| 213 | + | |
| 214 | +## 7. CONTROLS UNCHANGED | |
| 215 | + | |
| 216 | +The existing 4:2:0 coverage was run and is untouched and green — 8-bit, 10-bit | |
| 217 | +and 12-bit, all byte-exact against ffmpeg: | |
| 218 | + | |
| 219 | +| test | result | | |
| 220 | +|---|---| | |
| 221 | +| `a_real_aomenc_stream_with_film_grain_decodes_pixel_exact` (8-bit) | ok | | |
| 222 | +| `a_real_aomenc_10bit_film_grain_stream_decodes_pixel_exact` | ok | | |
| 223 | +| `a_real_aomenc_12bit_film_grain_stream_decodes_pixel_exact` | ok | | |
| 224 | +| `real_aomenc_film_grain_streams_decode_pixel_exact` (3 live aomenc draws) | ok | | |
| 225 | +| `untouched_planes_and_ragged_tail_come_back_clean` | ok | | |
| 226 | +| `simd_matches_scalar_luma_noise_row` | ok | | |
| 227 | +| `simd_matches_scalar_chroma_noise_row` | ok | | |
| 228 | +| `ragged_overlap_tail_matches_spec_reference` | ok (now 3 formats) | | |
| 229 | + | |
| 230 | +`cargo test -p ec-av1 --lib film_grain` → **8 passed, 0 failed**. | |
| 231 | + | |
| 232 | +`cargo check -p ec-av1 --lib --tests` → clean, no warnings. | |
| 233 | + | |
| 234 | +**Coverage that did not exist before this lane:** 4:2:2 and 4:4:4 film grain | |
| 235 | +had **no** test of any kind — not a byte-exactness gate, not a reference | |
| 236 | +comparison. `ragged_overlap_tail_matches_spec_reference` was a 4:2:0-only | |
| 237 | +transcription; it is now driven over all three formats, because a second | |
| 238 | +4:2:0-shaped copy of the reference would have been a new way to be wrong. It | |
| 239 | +also gained the assertion the old geometry **could not make**: | |
| 240 | + | |
| 241 | +```rust | |
| 242 | +assert_ne!(&got.u[last_chroma_row], &src.u[last_chroma_row], | |
| 243 | + "the LAST chroma row came back clean -- the walk did not reach it"); | |
| 244 | +``` | |
| 245 | + | |
| 246 | +At 4:2:2 the chroma plane is `height` rows tall and the old `nrows = height/2` | |
| 247 | +walk left the bottom half raw, so this is the assertion that would have caught | |
| 248 | +the original bug directly. | |
| 249 | + | |
| 250 | +## 8. PROCEDURE — probe bypass, patch-run-restore | |
| 251 | + | |
| 252 | +`EC_AV1_ALLOW_422_PROBE` does nothing; the bypass is patch-run-restore. | |
| 253 | + | |
| 254 | +1. `git worktree add --detach ~/.cache/wt/av1422grain-probe HEAD` | |
| 255 | +2. copy this lane's `film_grain.rs` into it | |
| 256 | +3. neutralise the guard at `stream.rs:1803` with | |
| 257 | + `if false && seq.subsampling_x != seq.subsampling_y`, with a | |
| 258 | + `// TEMP-PROBE-BYPASS lane-av1422grain: patch-run-restore, never committed.` | |
| 259 | + marker on the line above | |
| 260 | +4. run, then `git checkout -- <both files>`, verify `git status --porcelain` | |
| 261 | + is **empty**, verify `grep -c 'TEMP-PROBE-BYPASS\|if false &&'` is **0** | |
| 262 | +5. `git worktree remove --force` | |
| 263 | + | |
| 264 | +Verified: the probe worktree is removed, and the guard is intact and | |
| 265 | +un-bypassed. **The primary checkout is clean** (`git status --porcelain` | |
| 266 | +empty) with no bypass markers. | |
| 267 | + | |
| 268 | +**Incident, reported rather than buried:** my *first* edit of this lane used a | |
| 269 | +RELATIVE path (`crates/ec-av1/src/film_grain.rs`) while the shell CWD was the | |
| 270 | +primary checkout, so it landed in `/home/user/Documents/Code/Rust/edith_codecs` | |
| 271 | +instead of the lane worktree. The tool reported success and I read the | |
| 272 | +worktree, saw no change, and wrongly concluded the edit had been rolled back — | |
| 273 | +so I reissued everything against absolute paths and the worktree ended up | |
| 274 | +complete. The leak sat unnoticed in the primary until the post-probe | |
| 275 | +`git status` check caught it. I verified all 61 added lines were mine (the `Ss` | |
| 276 | +fields and brace lines; every other line carries a `lane-av1422grain` or | |
| 277 | +`chroma`/`luma`/`subsamp` marker) and reverted with | |
| 278 | +`git checkout -- crates/ec-av1/src/film_grain.rs`. No peer work was lost. The | |
| 279 | +lesson is the one already in the skill library: **absolute paths only, and | |
| 280 | +check `git status` in the primary before yielding** — the check is what found | |
| 281 | +it, and it should have been run after the first edit, not at the end. | |
| 282 | + | |
| 283 | +## 9. STATUS AND WHAT IS NOT DONE | |
| 284 | + | |
| 285 | +Delivered: branch 2 in full — the cell exists (measured), the geometry is | |
| 286 | +per-axis, `grain_hits` fires, all four cells are byte-exact per plane per | |
| 287 | +frame, the comparator is proven to bite, the negative control fails without the | |
| 288 | +port, and the 4:2:0 controls are unchanged. | |
| 289 | + | |
| 290 | +**The end-to-end gate is NOT committed, and cannot be until the guard is | |
| 291 | +lifted.** `decode_stream` still returns | |
| 292 | +`"a chroma format of 4:2:2 (subsampling_x != subsampling_y)"` on `main` | |
| 293 | +(`697b1308`) — the guard at `stream.rs:1803` is untouched by this lane, by | |
| 294 | +design; lifting it is not this arm. A committed 4:2:2 decode gate would be red | |
| 295 | +today for a reason that has nothing to do with film grain. | |
| 296 | + | |
| 297 | +**Deferred, with its unblock:** commit the four-cell byte-exactness gate | |
| 298 | +immediately after the lift lands. It needs no further film-grain work — the | |
| 299 | +bytes and the assertions are settled. Handing it over as a follow-up is the one | |
| 300 | +piece of scope I am explicitly not closing, and it is blocked on a decision | |
| 301 | +that is not mine. | |
| 302 | + | |
| 303 | +## 10. A NOTE FOR THE LIFT | |
| 304 | + | |
| 305 | +Two things this lane learned that the lift should not have to rediscover: | |
| 306 | + | |
| 307 | +* **The 4:2:2 film-grain cell is real and now byte-exact.** Whoever lifts the | |
| 308 | + guard should expect it to pass, and should not read a film-grain failure at | |
| 309 | + 4:2:2 as a lift regression — it would be a film-grain regression, and this | |
| 310 | + lane's commit is the fix. | |
| 311 | +* **`grain_hits()` is the counter to gate on** for the film-grain arm of a | |
| 312 | + lifted 4:2:2 stream, and the four cells in §1 are ready-made fixtures for | |
| 313 | + it. The study's §2 item 3 ("no 4:2:2 unit census exists") is a separate | |
| 314 | + matter and does not affect film grain. |