forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBdfeffbcTahinli2 months ago3180 commits

chroma-r1-attempt.diff254 lines, 10.7 KiBHistoryRawDownload
1diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs
2index 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
59diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs
60index 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