forge
Explore
Log in

Don't you dare go Hollow

Lane-chroma r2 charter + r1's attempt diff, out of the reapable scratchpad

Browse files at this commit

04170b5f37e6c88ce518a1d979eb775b95f76f9fAhmet Kaan GümüşParent: a869d13
  • docs
    • lane-chroma r2 charter + r1's attempt diff, out of the reapable scratchpad
Show full message
docs(av1): lane-chroma r2 charter + r1's attempt diff, out of the reapable scratchpad
lanes/chroma-r1-attempt.diffadded+254 -0
@@ -0,0 +1,254 @@
1+diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs
2+index fc53c53..9abc050 100644
3+--- a/crates/ec-av1/src/decode.rs
4++++ b/crates/ec-av1/src/decode.rs
5+@@ -126,6 +126,20 @@ pub(crate) fn directional_uv_hits() -> usize {
6+ DIRECTIONAL_UV_HITS.with(|c| c.get())
7+ }
8+
9++/// How many `uv_mode` reads resolved to `SMOOTH_PRED..=PAETH_PRED` (9..=12),
10++/// across every call in the process -- lane-chroma r1's own before/after
11++/// counter, proving a stream actually exercised chroma's smooth/paeth
12++/// predictor rather than every block landing on DC/CFL/directional.
13++thread_local! {
14++ static SMOOTH_UV_HITS: std::cell::Cell<usize> =
15++ const { std::cell::Cell::new(0) };
16++}
17++
18++/// Current value of [`SMOOTH_UV_HITS`].
19++pub(crate) fn smooth_uv_hits() -> usize {
20++ SMOOTH_UV_HITS.with(|c| c.get())
21++}
22++
23+ /// How many `angle_delta`/`angle_delta_uv` symbols this decoder has read as
24+ /// something other than [`ANGLE_DELTA_ZERO`], across every call in the
25+ /// process -- the same before/after counter pattern as [`FILTER_INTRA_HITS`],
26+@@ -1967,7 +1981,7 @@ fn read_intra_mode_rect(
27+ None
28+ };
29+ if (9..=12).contains(&uv_mode) {
30+- return Err(unsupported("a smooth or paeth chroma mode (round 2)"));
31++ SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1));
32+ }
33+ let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) {
34+ DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1));
35+@@ -2447,15 +2461,15 @@ fn read_intra_mode(
36+ } else {
37+ None
38+ };
39+- // `SMOOTH_PRED..PAETH_PRED` (9..=12) chroma is a separate round-2 gap
40+- // from this lane's directional chroma: a corner block with neither
41+- // `above` nor `left` (both `Edges::build`'s no-neighbour fallback,
42+- // 127/129) fed `SMOOTH_PRED` produces the wrong pixel there (worst delta
43+- // 78 against ffmpeg, traced 2026-08-27) -- an existing, un-lane-touched
44+- // bug in that fallback this lane's own scope does not cover. Keep it
45+- // refused here rather than ship a silently-wrong decode.
46++ // `SMOOTH_PRED..PAETH_PRED` (9..=12) chroma: lane-chroma r1 lands this --
47++ // the predictor itself (`intra::predict`) is already shared with luma and
48++ // already spec-correct for the no-neighbour corner case (127/129/128 is
49++ // the spec's own default, `Edges::build`). The earlier round's "worst
50++ // delta 78" trace was `smooth_neighbor` hardcoded `false` interacting
51++ // with a DIFFERENT bug that has since been fixed elsewhere; re-verified
52++ // clean under `a_real_aomenc_stream_with_smooth_paeth_chroma_decodes_pixel_exact`.
53+ if (9..=12).contains(&uv_mode) {
54+- return Err(unsupported("a smooth or paeth chroma mode (round 2)"));
55++ SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1));
56+ }
57+ // `get_uv_mode` (spec 9.3): `UV_CFL_PRED` predicts as `DC_PRED` for the
58+ // angle-delta question -- libaom's own `read_intra_frame_mode_info` reads
59+diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs
60+index 20ff033..31479d2 100644
61+--- a/crates/ec-av1/src/stream.rs
62++++ b/crates/ec-av1/src/stream.rs
63+@@ -1861,6 +1861,191 @@ mod tests {
64+ );
65+ }
66+
67++ /// The smooth/paeth-chroma gate (lane-chroma r1): a real aomenc key frame
68++ /// with `--enable-smooth-intra=1 --enable-paeth-intra=1` (the opposite of
69++ /// the directional-chroma gate above, which turned these OFF to keep this
70++ /// round-2 gap from firing while directional chroma was under test) and
71++ /// directional intra left on aomenc's own default too, so the RD search
72++ /// is free to land on any `uv_mode`. Some seeds won't pick smooth/paeth
73++ /// for chroma at all -- retry like every other real-encoder gate here,
74++ /// but the first attempt that actually fires [`decode::smooth_uv_hits`]
75++ /// must decode pixel-exact.
76++ #[test]
77++ fn a_real_aomenc_stream_with_smooth_paeth_chroma_decodes_pixel_exact() {
78++ if !have_ffmpeg() {
79++ eprintln!(
80++ "SKIP a_real_aomenc_stream_with_smooth_paeth_chroma_decodes_pixel_exact: no ffmpeg"
81++ );
82++ return;
83++ }
84++ if !have_aomenc() {
85++ eprintln!(
86++ "SKIP a_real_aomenc_stream_with_smooth_paeth_chroma_decodes_pixel_exact: no aomenc at {}",
87++ aomenc_path().display()
88++ );
89++ return;
90++ }
91++ let (width, height) = (64usize, 64usize);
92++ let mut refusals = Vec::new();
93++ let mut fired_runs = 0u32;
94++ for attempt in 0..40u32 {
95++ let seed = 42 + attempt;
96++ let source = format!(
97++ "mandelbrot=size=64x64:start_scale={}:rate=25",
98++ 5.0 - f64::from(attempt) * 0.06
99++ );
100++ let y4m = Command::new("ffmpeg")
101++ .args([
102++ "-v",
103++ "error",
104++ "-f",
105++ "lavfi",
106++ "-i",
107++ &source,
108++ "-pix_fmt",
109++ "yuv420p",
110++ "-t",
111++ "0.04",
112++ "-f",
113++ "yuv4mpegpipe",
114++ "-",
115++ ])
116++ .stdin(Stdio::null())
117++ .stdout(Stdio::piped())
118++ .stderr(Stdio::piped())
119++ .output()
120++ .expect("ffmpeg failed to run");
121++ assert!(
122++ y4m.status.success(),
123++ "ffmpeg fixture: {}",
124++ String::from_utf8_lossy(&y4m.stderr)
125++ );
126++ let mut child = Command::new(aomenc_path())
127++ .args([
128++ "--codec=av1",
129++ "--passes=1",
130++ "--end-usage=q",
131++ "--cq-level=45",
132++ "--cpu-used=0",
133++ "--enable-rect-partitions=0",
134++ "--enable-ab-partitions=0",
135++ "--enable-1to4-partitions=0",
136++ "--enable-filter-intra=0",
137++ "--enable-smooth-intra=1",
138++ "--enable-paeth-intra=1",
139++ "--enable-tx-size-search=0",
140++ "--enable-cdef=0",
141++ "--enable-restoration=0",
142++ "--enable-intra-edge-filter=1",
143++ "--max-partition-size=32",
144++ // Same reason as the tx-select gate above: 8x8 and other
145++ // sub-16x16 splits are a separate, already-named refusal.
146++ "--min-partition-size=16",
147++ "--enable-palette=0",
148++ "--enable-intrabc=0",
149++ "--enable-cfl-intra=0",
150++ // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack;
151++ // leaving it on silently desyncs symbols on inter frames (grainfix lesson).
152++ "--enable-ref-frame-mvs=0",
153++ "--obu",
154++ "-o",
155++ "-",
156++ "-",
157++ ])
158++ .stdin(Stdio::piped())
159++ .stdout(Stdio::piped())
160++ .stderr(Stdio::piped())
161++ .spawn()
162++ .expect("aomenc failed to start");
163++ child
164++ .stdin
165++ .take()
166++ .expect("aomenc stdin")
167++ .write_all(&y4m.stdout)
168++ .expect("writing y4m to aomenc");
169++ let out = child.wait_with_output().expect("aomenc failed to run");
170++ assert!(
171++ out.status.success(),
172++ "aomenc refused the fixture: {}",
173++ String::from_utf8_lossy(&out.stderr)
174++ );
175++ let stream = out.stdout;
176++ let smooth_before = decode::smooth_uv_hits();
177++ let frames = match decode_stream(&stream) {
178++ Ok(frames) => frames,
179++ Err(e) => {
180++ refusals.push(format!("seed {seed}: {e}"));
181++ continue;
182++ }
183++ };
184++ if decode::smooth_uv_hits() == smooth_before {
185++ refusals.push(format!(
186++ "seed {seed}: decoded, but no smooth/paeth uv_mode fired"
187++ ));
188++ continue;
189++ }
190++ fired_runs += 1;
191++ let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1);
192++ let mismatched = frames[0].y != ffmpeg_frames[0].y
193++ || frames[0].u != ffmpeg_frames[0].u
194++ || frames[0].v != ffmpeg_frames[0].v;
195++ if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") {
196++ std::fs::write(&path, &stream).expect("writing pinned stream");
197++ eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}");
198++ }
199++ assert_eq!(
200++ frames[0].y, ffmpeg_frames[0].y,
201++ "luma vs ffmpeg (seed {seed})"
202++ );
203++ assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg (seed {seed})");
204++ assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (seed {seed})");
205++ if fired_runs >= 4 {
206++ return;
207++ }
208++ }
209++ assert!(
210++ fired_runs > 0,
211++ "every attempt hit a named refusal or never exercised smooth/paeth chroma:\n{}",
212++ refusals.join("\n")
213++ );
214++ }
215++
216++ #[test]
217++ #[ignore]
218++ fn zzz_smooth_pin_diag() {
219++ let path = std::env::var("EC_ZZZ_PATH").unwrap_or_else(|_| {
220++ "/tmp/claude-1000/-home-user-Documents-Code-Rust-edith-codecs/baaa03f8-c4ff-4469-8ebb-83100429b150/scratchpad/smooth-pin.obu"
221++ .to_string()
222++ });
223++ let stream = std::fs::read(&path).unwrap();
224++ let frames = decode_stream(&stream).unwrap();
225++ let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 1);
226++ for (row, (a, b)) in frames[0]
227++ .y
228++ .chunks(64)
229++ .zip(ffmpeg_frames[0].y.chunks(64))
230++ .enumerate()
231++ {
232++ for (col, (x, y)) in a.iter().zip(b).enumerate() {
233++ if x != y {
234++ eprintln!("Y mismatch row={row} col={col} ours={x} ffmpeg={y}");
235++ }
236++ }
237++ }
238++ for (row, (a, b)) in frames[0]
239++ .u
240++ .chunks(32)
241++ .zip(ffmpeg_frames[0].u.chunks(32))
242++ .enumerate()
243++ {
244++ for (col, (x, y)) in a.iter().zip(b).enumerate() {
245++ if x != y {
246++ eprintln!("U mismatch row={row} col={col} ours={x} ffmpeg={y}");
247++ }
248++ }
249++ }
250++ }
251++
252+ /// As above, but a multi-frame inter sequence -- `--lag-in-frames=0
253+ /// --auto-alt-ref=0` keep every non-key frame a simple forward
254+ /// single-`LAST_FRAME` P frame (no alt-ref/backward reference this
lanes/chroma-r2.charter.mdadded+80 -0
@@ -0,0 +1,80 @@
1+# lane-chroma r2 — smooth-neighbour edge filter, then smooth/paeth chroma
2+
3+Worktree `/home/user/Documents/Code/Rust/edith_codecs-chroma`, branch
4+`lane-chroma`, at 3ca2455 (r1's report; r1 landed NO code — it reverted).
5+
6+## What r1 established (read `lanes/chroma-r1.report.md` first — it is short)
7+- The `SMOOTH_PRED`/`PAETH_PRED` predictor math in `intra.rs` is already
8+ spec-correct and shared between luma and chroma. Removing the chroma refusal
9+ (`decode.rs` uv_mode 9..=12, around lines 1970 and 2458) is NOT the work.
10+- The real defect is on LUMA: `reconstruct()` hardcodes `smooth_neighbor = false`
11+ — its own comment calls this "a corner-cut for luma" and names the upgrade
12+ path. `intra_edge_filter_strength` therefore picks the wrong bucket whenever a
13+ directional block's neighbour used a SMOOTH mode. r1 reproduced it exactly:
14+ seed 42, directional block px=48 py=16 mode=5, neighbour px=48 py=0 with
15+ SMOOTH_H_PRED, off by 1-2 in the filtered edge.
16+- **Nothing in this repo had ever exercised it**: all 23 real-aomenc gates in
17+ `stream.rs` pass `--enable-smooth-intra=0 --enable-paeth-intra=0`. That is a
18+ fleet-wide gate-recipe blindness, not a one-gate miss.
19+- r1's attempted diff (refusal removal + the new gate + the counter) is
20+ preserved at `lanes/chroma-r1-attempt.diff` in this worktree. Start from it.
21+
22+## Goal, in order
23+1. **Fix the luma bug first, on its own, with its own gate.** Thread the
24+ neighbour's intra mode into `reconstruct()` / `reconstruct_rect()` so
25+ `smooth_neighbor` is `above_mode` (or `left_mode`, per the spec's
26+ directionality) being in `SMOOTH_PRED..=SMOOTH_H_PRED`. r1's report lists
27+ the ~9 call sites. `Neighbours` already tracks per-block state — add the
28+ luma mode alongside whatever it already stores; follow the shape of the
29+ existing `skip_at` / `skip_txfm_ctx` accessors in `decode.rs`.
30+ Spec anchor: 7.11.2.9 `intra edge filter strength` — the `filt_type`
31+ argument is 1 when the relevant neighbour used a smooth mode. libaom:
32+ `av1/common/reconintra.c` `get_filt_type()` / `av1_filter_intra_edge` and
33+ `intra_edge_filter_strength`; `get_filt_type` reads
34+ `av1_is_directional_mode` and `is_smooth(above_mbmi)`/`is_smooth(left_mbmi)`.
35+ Gate it with a real-aomenc stream that enables smooth intra for LUMA while
36+ the chroma refusal is still in place (chroma can still refuse at this
37+ stage — that keeps the two changes separable). COMMIT.
38+2. **Then** add chroma UV-mode neighbour tracking and remove the smooth/paeth
39+ chroma refusal, with the gate from the attempt diff
40+ (`a_real_aomenc_stream_with_smooth_paeth_chroma_decodes_pixel_exact`)
41+ passing pixel-exact on luma AND chroma. COMMIT.
42+3. **Then** the directional-chroma sibling if turns remain: `decode.rs:7786`,
43+ the inter-frame intra-block reader, which refuses any non-DC/CFL uv_mode
44+ unconditionally and never computes `angle_delta_uv`. r1 found no bug there,
45+ only unwritten plumbing. COMMIT.
46+
47+## Gate rules (mandatory)
48+- Run tests with `EC_AV1_REQUIRE_AOMENC=1` — a missing oracle must FAIL, not SKIP.
49+- Bound every ffmpeg `generate` with `-t <seconds>`.
50+- Build fixtures through the existing `gradients_source(seed, w, h, tail)`
51+ helper; ffmpeg's `gradients` source ignores its own seed.
52+- aomenc `--threads=1 --row-mt=0`, and of course `--enable-smooth-intra=1`
53+ (and `--enable-paeth-intra=1` for stage 2).
54+- Hard-assert a firing count with a thread-local `Cell<usize>` counter in
55+ decode.rs matching the existing `*_HITS` (thread-local now, NOT atomics):
56+ for stage 1, count the blocks where `smooth_neighbor` came out TRUE — a gate
57+ that runs with smooth intra enabled but never hits the new branch proves
58+ nothing (class `gate-blind-to-feature`).
59+- Refusals inside a gate are FORBIDDEN once the stage removing them lands.
60+
61+## Method
62+CLASS `compare-range-not-tell`: compare the msac RANGE against the oracle, never
63+`tell()`. Oracle at `~/.cache/aom-oracle`, rungs `EC_TRACE=1` (partitions),
64+`EC_TRACE_COEFF=1`, `EC_TRACE_MODE=1` (inter + intra mode info),
65+`EC_AV1_PREFILT_DUMP=<prefix>` (per-frame pre-filter recon — the right rung for
66+a predictor bug, since it isolates prediction from the loop filters).
67+Add a rung via `scripts/instrument-aom-oracle.sh` + `scripts/build-aom-oracle.sh`
68+in the existing shape (env-gated, silent when unset, idempotent) if you need one.
69+
70+## Hard rules
71+- `export CARGO_TARGET_DIR=$HOME/.cache/cargo-target-chroma`. Foreground builds,
72+ `nice -n 19 cargo ... -j4`. Sibling worktrees (edith_codecs, -realworld, -lr,
73+ -superres, -tiles) have live agents — never build in or edit them.
74+- Suite: `nice -n 19 cargo test -p ec-av1 --lib`, baseline 232 passed / 0 failed.
75+- NEVER push, never merge, never touch `main`. Commit on `lane-chroma` only.
76+- 75-turn cap: COMMIT AFTER EVERY GREEN MILESTONE. r1 spent its whole budget and
77+ landed zero code because it held everything in the working tree; a commit that
78+ compiles beats a perfect uncommitted tree. Do not re-derive what r1's report
79+ already tells you (class `worker-cap-spent-reading`).
80+- End with `lanes/chroma-r2.report.md`, VERDICT on the first line.