diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 533bf9b7..8e4d6748 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -903,6 +903,74 @@ mod tests { } } + /// Builds a `gradients` lavfi source string with colours derived from + /// `seed`, not left to the filter's own default. + /// + /// Measured (lane-fixdet r1): ffmpeg's `gradients` source IGNORES its + /// `seed=` parameter for colour selection -- five encodes of an + /// identical command line (same `seed=`, same everything) produced five + /// different fixtures, because the unset `c0..c9` stops are randomized + /// from something other than that seed. Every one of this file's ~20 + /// gate fixtures built a `gradients` source this way, so none of them + /// had a reproducible input: a pin captured from one run could not be + /// regenerated, and "attempt N failed" was as likely to be a different + /// fixture as a different decode outcome. + /// + /// Freezing the colours outright (as the earliest four gates below do, + /// hand-picking `c0=red:c1=blue:c2=green`) fixes reproducibility but + /// throws away the per-attempt content variety the sweep gates need to + /// make features (OBMC, warp, wedge, ...) fire at all -- this repo's own + /// `sampler-decorrelated-gate` class. So: derive `c0..c3` deterministically + /// FROM the seed with a plain integer hash, and keep passing `seed=` + /// itself too, so a future ffmpeg that actually honours it only adds + /// more variety, never removes the determinism this buys. + fn gradients_source(seed: u32, width: usize, height: usize, tail: &str) -> String { + fn hash_color(seed: u32, salt: u32) -> String { + let h = seed + .wrapping_mul(2_654_435_761) + .wrapping_add(salt.wrapping_mul(0x9E37_79B9)) + ^ (seed.rotate_left(13)); + format!("0x{:06x}", h & 0x00ff_ffff) + } + let (c0, c1, c2, c3) = ( + hash_color(seed, 0), + hash_color(seed, 1), + hash_color(seed, 2), + hash_color(seed, 3), + ); + format!("gradients=size={width}x{height}:c0={c0}:c1={c1}:c2={c2}:c3={c3}:seed={seed}:{tail}") + } + + /// The determinism guard: same seed twice must byte-match ffmpeg's own + /// output; different seeds must not. Regresses this fix if it ever + /// breaks. + #[test] + fn gradients_source_is_reproducible_per_seed() { + if !have_ffmpeg() { + eprintln!("SKIP gradients_source_is_reproducible_per_seed: no ffmpeg"); + return; + } + fn render_hash(lavfi: &str) -> Vec { + let out = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", lavfi, "-frames:v", "1", "-f", "rawvideo", + "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!(out.status.success(), "ffmpeg refused {lavfi}"); + out.stdout + } + let a1 = render_hash(&gradients_source(42, 64, 64, "duration=0.04:rate=25")); + let a2 = render_hash(&gradients_source(43, 64, 64, "duration=0.04:rate=25")); + let a3 = render_hash(&gradients_source(42, 64, 64, "duration=0.04:rate=25")); + assert_eq!(a1, a3, "same seed must reproduce byte-identical output"); + assert_ne!(a1, a2, "different seeds must not collide"); + } + /// Encodes a real libaom-av1 stream (via ffmpeg's own encoder, not this /// crate's) with the same reduced-partition flags lane-av1adst's r6 /// probe found still select `ADST_ADST`/`ADST_DCT`/`DCT_ADST` on @@ -988,17 +1056,13 @@ mod tests { eprintln!("SKIP a_real_libaom_stream_with_adst_decodes_end_to_end: no ffmpeg"); return; } - let configs: [(&str, usize, usize, u32); 2] = [ - ("testsrc2=size=64x64:rate=1", 64, 64, 15), - ( - "gradients=size=64x64:c0=red:c1=blue:c2=green:rate=1", - 64, - 64, - 15, - ), + let configs: [(String, usize, usize, u32); 2] = [ + ("testsrc2=size=64x64:rate=1".to_string(), 64, 64, 15), + (gradients_source(42, 64, 64, "rate=1"), 64, 64, 15), ]; let mut verdicts = Vec::new(); for (lavfi, width, height, crf) in configs { + let lavfi = lavfi.as_str(); let Some(stream) = libaom_encode(lavfi, width, height, crf) else { verdicts.push(format!("{lavfi}: ffmpeg itself refused to encode")); continue; @@ -1052,7 +1116,7 @@ mod tests { } let (width, height) = (64, 64); let stream = libaom_encode_with( - "gradients=size=64x64:c0=red:c1=blue:c2=green:rate=1:seed=42", + &gradients_source(42, width, height, "rate=1"), width, height, 15, @@ -1098,7 +1162,7 @@ mod tests { } let (width, height) = (32, 32); let stream = libaom_encode_with( - "gradients=size=32x32:c0=red:c1=blue:c2=green:rate=1:seed=42", + &gradients_source(42, width, height, "rate=1"), width, height, 15, @@ -1142,7 +1206,7 @@ mod tests { } let (width, height) = (64, 64); let stream = libaom_encode_with( - "gradients=size=64x64:c0=red:c1=blue:c2=green:rate=1:seed=42", + &gradients_source(42, width, height, "rate=1"), width, height, 30, @@ -1265,7 +1329,7 @@ mod tests { .ok() .and_then(|v| v.parse().ok()) .unwrap_or(42); - let fi_source = format!("gradients=size=64x64:seed={fi_seed}:duration=0.04:rate=25"); + let fi_source = gradients_source(fi_seed, 64, 64, "duration=0.04:rate=25"); let fi_cq = std::env::var("EC_FI_CQ").unwrap_or_else(|_| "40".to_string()); let fi_cq_arg = format!("--cq-level={fi_cq}"); let y4m = Command::new("ffmpeg") @@ -1391,20 +1455,11 @@ mod tests { return; } let (width, height) = (64usize, 64usize); + let source = gradients_source(42, width, height, "duration=0.04:rate=25"); let y4m = Command::new("ffmpeg") - .args([ - "-v", - "error", - "-f", - "lavfi", - "-i", - "gradients=size=64x64:seed=42:duration=0.04:rate=25", - "-pix_fmt", - "yuv420p", - "-f", - "yuv4mpegpipe", - "-", - ]) + .args(["-v", "error", "-f", "lavfi", "-i"]) + .arg(&source) + .args(["-pix_fmt", "yuv420p", "-f", "yuv4mpegpipe", "-"]) .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) @@ -1827,14 +1882,11 @@ mod tests { return; } let (width, height, frame_count) = (64usize, 64usize, 4usize); + let source = gradients_source(42, width, height, "duration=0.16:rate=25"); let y4m = Command::new("ffmpeg") + .args(["-v", "error", "-f", "lavfi", "-i"]) + .arg(&source) .args([ - "-v", - "error", - "-f", - "lavfi", - "-i", - "gradients=size=64x64:seed=42:duration=0.16:rate=25", "-pix_fmt", "yuv420p", "-f", @@ -1991,7 +2043,7 @@ mod tests { let mut refusals = Vec::new(); for attempt in 0..8u32 { let seed = 42 + attempt; - let source = format!("gradients=size=64x64:seed={seed}:duration=0.16:rate=25"); + let source = gradients_source(seed, 64, 64, "duration=0.16:rate=25"); let y4m = Command::new("ffmpeg") .args([ "-v", @@ -2349,7 +2401,7 @@ mod tests { // this recipe's own choosing, not a decoder bug. for attempt in 0..120u32 { let seed = 42 + attempt % 40; - let source = format!("gradients=size=64x64:seed={seed}:duration=0.32:rate=25"); + let source = gradients_source(seed, 64, 64, "duration=0.32:rate=25"); let y4m = Command::new("ffmpeg") .args([ "-v", @@ -2561,7 +2613,7 @@ mod tests { let seed = 42 + attempt % 40; let duration = frame_count as f64 / 25.0; let source = - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", @@ -2745,7 +2797,7 @@ mod tests { let seed = 42 + attempt % 40; let duration = frame_count as f64 / 25.0; let source = - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", @@ -2960,7 +3012,7 @@ mod tests { let seed = 42 + attempt % 40; let duration = frame_count as f64 / 25.0; let source = - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", @@ -3136,7 +3188,7 @@ mod tests { let seed = 42 + attempt % 40; let duration = frame_count as f64 / 25.0; let source = - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", @@ -3317,7 +3369,7 @@ mod tests { // EC_AV1_OBMC8_SOURCE overrides the lavfi source so a firing recipe // can be searched for without a rebuild. let source = std::env::var("EC_AV1_OBMC8_SOURCE").unwrap_or_else(|_| { - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25") + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")) }); // A source supplied through EC_AV1_OBMC8_SOURCE need not carry its // own `duration=`; `-t` is the bound that always holds. Without it @@ -3493,7 +3545,7 @@ mod tests { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; let source = - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", @@ -3662,7 +3714,7 @@ mod tests { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; let source = - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", @@ -3838,7 +3890,7 @@ mod tests { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; let source = if attempt % 2 == 0 { - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25") + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")) } else { // `end_pts` does NOT bound mandelbrot (it generated 300+ // frames and hung the gate for an hour); `-t` below is the @@ -4041,7 +4093,7 @@ mod tests { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; let source = - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", @@ -4242,7 +4294,7 @@ mod tests { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; let source = - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", @@ -4841,7 +4893,7 @@ mod tests { let seed = seed_base + attempt; let duration = frame_count as f64 / 25.0; let source = - format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", diff --git a/lanes/fixdet-r1.charter.md b/lanes/fixdet-r1.charter.md new file mode 100644 index 00000000..ff30d5a5 --- /dev/null +++ b/lanes/fixdet-r1.charter.md @@ -0,0 +1,72 @@ +# lane-fixdet r1 charter — the gate fixtures are not reproducible + +## Where you are +Worktree /home/user/Documents/Code/Rust/edith_codecs-fixdet, branch +lane-fixdet @ main. +Build/test ONLY: + `env CARGO_TARGET_DIR=$HOME/.cache/cargo-target-fixdet CARGO_BUILD_JOBS=4 nice -n 19 cargo test -p ec-av1 --release --lib -- --nocapture` +FOREGROUND, EC_AV1_REQUIRE_AOMENC=1 on gates. Never push. Never touch the main +checkout or the sibling -rectwire2 worktree. WIP COMMIT after every green step. + +## The measurement (already done — do not repeat it, build on it) +ffmpeg's `gradients` lavfi source IGNORES its `seed` parameter for colour +selection. Hashing its output five times with an identical command line: + + e981f6da916b400a 2451ed66cdf1c114 13c53f4382ab72a1 + f9ac6e84c77489ff dcffa3d8caa39e78 + +Five runs, five different fixtures. Twenty gate sites in +`crates/ec-av1/src/stream.rs` build their fixture this way, so those gates have +never had a reproducible input. + +Two consequences, both measured: +- `a_real_aomenc_filter_intra_stream_decodes_pixel_exact` fails about 1 run in + 15 even in isolation with the encoder pinned to one thread. The encoder is + NOT the source of that: hashing aomenc's output six times over a FIXED y4m + gives the same bytes every time. The fixture is the variable. +- Every "attempt-selection flake" attributed to aomenc's RD search deserves + re-examination. Some of those were the SOURCE changing under the gate. + +Naming a seed that does nothing is worse than having no seed: it advertises +reproducibility the gate does not have, and a pin captured from such a run +cannot be regenerated. + +## The fix +`gradients` IS deterministic once the colours are given explicitly — verified, +three identical hashes. But the gates need per-attempt VARIETY too: they sweep +`seed` across attempts precisely to sample different content, and freezing the +colours would make every attempt identical and quietly destroy the sampling +that makes features fire (this repo has a `sampler-decorrelated-gate` class +already). + +So derive the colours FROM the seed. Verified working: seeds 42, 43, 42 gave +hashes c25eb1d4d042, 5097b017faf7, c25eb1d4d042 — different seeds differ, the +same seed reproduces exactly. + +1. Write ONE helper in `stream.rs`, e.g. + `fn gradients_source(seed: u32, w: usize, h: usize, duration: f64) -> String`, + that maps the seed deterministically to `c0`..`c3` (and keeps passing + `seed` so any future ffmpeg that honours it still varies). Document WHY in + a comment: the seed alone does not make this source reproducible. +2. Route all 20 sites through it. Keep each site's existing width/height/ + duration/rate; this is a determinism fix, not a content change. +3. Prove it: a test that builds the same source string twice for one seed and + runs ffmpeg on both, asserting byte-identical output, and that two + different seeds differ. That test is the guard against this regressing. + +## Then re-measure what the fix changes +- Run `a_real_aomenc_filter_intra_stream_decodes_pixel_exact` 20 times. It was + 14/15 before. Report the new number honestly, whatever it is. +- Run the free-partition gate 3 times and report whether its refusal/match + counts are now stable run-to-run. If they are, say so; if they still move, + aomenc attempt selection is real on top of this and the report should say + that too, with the numbers. +- If a gate now fails CONSISTENTLY, that is a genuine decoder defect this + nondeterminism was hiding. Do NOT paper over it: pin it with + EC_AV1_GATE_DUMP, name it in the report, and refuse-by-name rather than + guess-fix. + +## Done criteria +Full lib suite 231 passed / 0 failed. All 20 sites through the helper. The +determinism test in place. REPORT `lanes/fixdet-r1.report.md`, VERDICT on the +FIRST line, with the before/after flake rates as measured counts. diff --git a/lanes/fixdet-r1.report.md b/lanes/fixdet-r1.report.md new file mode 100644 index 00000000..754c87a9 --- /dev/null +++ b/lanes/fixdet-r1.report.md @@ -0,0 +1,90 @@ +# lane-fixdet r1 report + +VERDICT: FIXED — all 20 `gradients` gate sites now route through a seed-derived-colour helper, the determinism guard test is green, filter_intra went from 14/15 to 20/20, the free-partition gate's counts are now byte-stable run-to-run, and the full lib suite stays 232/0 (231 pre-existing + the new guard test). No gate now fails consistently; no decoder defect was uncovered by removing this nondeterminism. + +## What changed +`crates/ec-av1/src/stream.rs`: +- New `gradients_source(seed, width, height, tail) -> String` (right before + `libaom_encode`), hashing `seed` into four `0xRRGGBB` colours for + `c0..c3` with a plain integer mix (`wrapping_mul`/`rotate_left`), and + still passing `seed=` itself through in case a future ffmpeg honours it. + Doc comment states why: `gradients` ignores `seed=` for its own colour + choice, so an unset `c0..c9` stop is randomized from something else, + and pinning colours to fixed literals (as the 4 earliest gates already + did) would kill the per-attempt content variety the sweep gates need to + make features fire. +- New guard test `gradients_source_is_reproducible_per_seed`: renders raw + video for seed 42 twice and seed 43 once, asserts the two seed-42 runs + are byte-identical and seed-43 differs. This is the regression fence. +- All 20 `gradients=size=...` call sites (4 that hand-pinned + `c0=red:c1=blue:c2=green`, plus 16 that built the string with only + `seed=` and no colours at all) rewritten to call the helper, keeping + each site's original width/height/duration/rate exactly. Two of them + built the `-i` argument inline in a `Command::new("ffmpeg").args([...])` + array of `&str` literals; those became `.args([...]).arg(&source)` + since the helper returns an owned `String`. One (`a_real_libaom_stream_with_adst_decodes_end_to_end`) + held both fixtures in a `[(&str, ...); 2]` array; retyped to + `[(String, ...); 2]`. + +## Re-measurement + +### filter_intra gate — was 14/15, isolated +20 runs of `a_real_aomenc_filter_intra_stream_decodes_pixel_exact` in +isolation (`cargo test ... a_real_aomenc_filter_intra_stream_decodes_pixel_exact`), +each a fresh process: + + 20 passed / 0 failed out of 20 + +Every one of the 20 runs printed `test result: ok. 1 passed`. The ~1-in-15 +flake measured in the charter is gone. + +### free-partition gate — 3 runs, isolated +`a_real_aomenc_stream_with_free_partitions_decodes_pixel_exact`, 3 fresh +processes: + + run 1: 33 named refusals, 7 pixel-exact matches out of 40, rect_partition_hits=9 rect_coeff_hits=0 extended_partition_hits=0 partab_hits=2 + run 2: 33 named refusals, 7 pixel-exact matches out of 40, rect_partition_hits=9 rect_coeff_hits=0 extended_partition_hits=0 partab_hits=2 + run 3: 33 named refusals, 7 pixel-exact matches out of 40, rect_partition_hits=9 rect_coeff_hits=0 extended_partition_hits=0 partab_hits=2 + +Every field is now byte-identical across all 3 runs: refusal count, +match count, and every named hit counter. The gate's counts ARE stable +now — the fixture-side nondeterminism this lane targeted is closed for +this gate. + +One honest caveat: the same test, run as part of the full 232-test suite +(reported below) printed different numbers that same session +(`rect_partition_hits=18 ... partab_hits=4`, exactly double the isolated +run's counts, with the same 7/40 match rate and 33 named refusals). +That is consistent with the gate accumulating into a process-global +counter shared with another in-suite test on the same fixture family +(cross-test interference), not with residual fixture nondeterminism — +the isolated, filtered runs above (which exclude every other test) are +the correct measurement of THIS gate's own stability, and they are flat +across all 3. Flagging it rather than papering over it: `deferred: +double-counted rect_partition_hits/partab_hits when this gate runs +alongside its siblings in the full suite — likely a shared static +counter not scoped per-test — unblocked by grepping RECT_PARTITION_HITS/ +PARTAB_HITS for a missing per-test reset; not a fixdet-r1 scope item, +filed here as found, not investigated further this round.` + +### Full lib suite +`cargo test -p ec-av1 --release --lib` with `EC_AV1_REQUIRE_AOMENC=1`: + + test result: ok. 232 passed; 0 failed; 17 ignored; 0 measured; 0 filtered out; finished in 68.15s + +231 pre-existing + 1 new (`gradients_source_is_reproducible_per_seed`). +0 failures. No gate failed consistently in this run, so this lane did not +uncover a genuine decoder defect that the fixture nondeterminism had been +hiding — the report's "pin it with EC_AV1_GATE_DUMP and refuse by name" +branch does not apply this round. + +## Deferred +- `deferred: rect_partition_hits/partab_hits double-counted when the + free-partition gate runs inside the full suite vs isolated — cross-test + counter-sharing, not a fixture-determinism defect — unblocked by + auditing the counter's storage (static vs per-call) — accepted as + out-of-scope for fixdet-r1, no fix attempted this round.` + +## Files touched +- `crates/ec-av1/src/stream.rs` — `gradients_source` helper, guard test, + 20 call sites rewired.