diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index dddeb1d5..360b9aa7 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -737,6 +737,23 @@ thread_local! { const { std::cell::Cell::new(0) }; } +// How many plain SQUARE intra blocks (lane-sqchroma r1) resolved a luma +// transform smaller than their own side (`tx_depth != 0`) while coding a +// chroma prediction other than `DC_PRED` -- the pair the square path's chroma +// only exercises with aomenc's tx-size search left ON (its default), which +// every older recipe in this file pins off. Counted at the block, so a stream +// where RD happened to give every split-transform block a DC chroma mode +// reads as zero rather than silently passing for free. +thread_local! { + static SQ_CHROMA_TX_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`SQ_CHROMA_TX_HITS`]. +pub(crate) fn sq_chroma_tx_hits() -> usize { + SQ_CHROMA_TX_HITS.with(|c| c.get()) +} + /// Current value of [`TX_DEPTH_HITS`]. pub(crate) fn tx_depth_hits() -> usize { TX_DEPTH_HITS.with(|c| c.get()) @@ -5739,6 +5756,9 @@ fn decode_block( } else { (side, luma_tx) }; + if tx_select && logical_tx < side && uv_mode != DC_PRED { + SQ_CHROMA_TX_HITS.with(|c| c.set(c.get() + 1)); + } let luma_set = if tx_select { txbset_for(logical_tx, reduced_tx_set) } else { diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 646a201f..efe80e47 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -10627,6 +10627,196 @@ mod tests { ); } + /// lane-sqchroma r1 gate: a plain SQUARE-partition intra key frame with + /// REAL chroma (no `hue=s=0` guard) and aomenc's tx-size search left at + /// its own DEFAULT (on) -- the combination the user's films hit + /// everywhere and that no gate in this file covered: every older recipe + /// pins `--enable-tx-size-search=0`, and the one that does not + /// (`a_real_aomenc_stream_with_a_split_transform_horz_vert_strip_decodes_pixel_exact`) + /// desaturates its fixture. With the search on, RD gives a square block a + /// luma transform smaller than its own side while chroma keeps the + /// block's single `side/2` transform and its own `uv_mode`-derived + /// `tx_type` -- the pair [`crate::decode::sq_chroma_tx_hits`] counts and + /// this gate hard-asserts, so a stream whose split-transform blocks all + /// landed on `DC_PRED` chroma cannot pass for free. + /// + /// Sweeps seeds 8-bit and finishes with one 10-bit stream of the same + /// recipe (his films are `yuv420p10le`), whose sequence header's + /// `bit_depth` is asserted before any pixel is compared. Only a missing + /// tool SKIPs: a decode error that is not a named refusal, and every + /// pixel mismatch, FAIL. + #[test] + fn a_real_aomenc_intra_stream_with_tx_size_search_and_chroma_decodes_pixel_exact() { + const NAME: &str = + "a_real_aomenc_intra_stream_with_tx_size_search_and_chroma_decodes_pixel_exact"; + let _gate_lock = lock_gate_counters(); + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); + return; + } + let (width, height) = (192usize, 128usize); + // NOTE: no `--enable-tx-size-search=0` (the point of this gate), and + // no chroma filter on the fixture (the other point). Square + // partitions only: rect/AB/1to4 all off. + let base_args: Vec<&str> = vec![ + "--codec=av1", + "--passes=1", + "--end-usage=q", + "--cpu-used=0", + "--threads=1", + "--row-mt=0", + "--sb-size=64", + "--enable-rect-partitions=0", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--min-partition-size=16", + "--max-partition-size=32", + "--enable-restoration=0", + "--enable-palette=0", + "--deltaq-mode=0", + "--enable-intrabc=0", + "--limit=1", + ]; + let encode = |y4m: &[u8], extra: &[&str]| -> Vec { + let mut args = base_args.clone(); + args.extend_from_slice(extra); + args.extend_from_slice(&["--obu", "-o", "-", "-"]); + let mut child = Command::new(aomenc_path()) + .args(&args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(y4m) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + assert!(!out.stdout.is_empty(), "{NAME}: aomenc wrote an empty stream"); + out.stdout + }; + let render = |source: &str, pix_fmt: &str| -> Vec { + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", source, "-t", "0.04", "-pix_fmt", pix_fmt, + "-strict", "-1", "-f", "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "ffmpeg fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + y4m.stdout + }; + let mut named_refusals = 0u32; + let mut matched = 0u32; + // Hits counted ONLY over attempts that decoded AND pixel-compared: + // a stream that later refuses must not lend its firings to the + // assertion below (class refusal-hides-a-defect). + let mut firing = 0usize; + let n_attempts: u32 = std::env::var("EC_SQCHROMA_GATE_ATTEMPTS") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(10); + for attempt in 0..n_attempts { + let seed = 42 + attempt; + // Two quantisers per seed: the coarse one keeps most blocks on + // one transform, the fine one is what actually splits them. + let cq = if attempt % 2 == 0 { "--cq-level=20" } else { "--cq-level=40" }; + let y4m = render( + &gradients_source(seed, width, height, "duration=0.04:rate=25"), + "yuv420p", + ); + let stream = encode(&y4m, &[cq]); + let before = crate::decode::sq_chroma_tx_hits(); + let frames = match decode_stream(&stream) { + Err(e) => { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" + ); + named_refusals += 1; + continue; + } + Ok(frames) => frames, + }; + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); + assert_eq!(frames.len(), 1, "{NAME}: expected one frame (seed {seed})"); + assert_eq!(frames[0].y, ffmpeg_frames[0].y, "{NAME} luma vs ffmpeg (seed {seed})"); + assert_eq!(frames[0].u, ffmpeg_frames[0].u, "{NAME} U vs ffmpeg (seed {seed})"); + assert_eq!(frames[0].v, ffmpeg_frames[0].v, "{NAME} V vs ffmpeg (seed {seed})"); + firing += crate::decode::sq_chroma_tx_hits() - before; + matched += 1; + } + // Same recipe, 10-bit: his two films are both yuv420p10le, so the + // square+tx-search+chroma claim is only worth anything if it holds + // at their bit depth too. + let y4m10 = render( + &gradients_source(42, width, height, "duration=0.04:rate=25"), + "yuv420p10le", + ); + let stream10 = encode(&y4m10, &["--cq-level=20", "--input-bit-depth=10", "--bit-depth=10"]); + let mut probe = Av1Parser::new(); + let mut pos = 0usize; + while pos < stream10.len() && probe.sequence_header().is_none() { + let obu = probe.parse_obu(&stream10[pos..]).unwrap(); + pos += obu.total_size; + } + assert_eq!( + probe + .sequence_header() + .expect("stream has a sequence header OBU") + .color_config + .bit_depth, + 10, + "{NAME}: aomenc did not actually write a 10-bit sequence header" + ); + let before10 = crate::decode::sq_chroma_tx_hits(); + let frames10 = match decode_stream(&stream10) { + Ok(frames) => frames, + Err(e) => panic!("{NAME}: decode_stream refused the 10-bit stream: {e}"), + }; + let ffmpeg10 = ffmpeg_decode_sequence_10bit(&stream10, width, height, 1); + assert_eq!(frames10[0].y, ffmpeg10[0].y, "{NAME} luma vs ffmpeg (10-bit)"); + assert_eq!(frames10[0].u, ffmpeg10[0].u, "{NAME} U vs ffmpeg (10-bit)"); + assert_eq!(frames10[0].v, ffmpeg10[0].v, "{NAME} V vs ffmpeg (10-bit)"); + let firing10 = crate::decode::sq_chroma_tx_hits() - before10; + firing += firing10; + matched += 1; + assert!( + matched > 1, + "{NAME}: every 8-bit attempt refused; the gate decoded only the 10-bit stream" + ); + assert!( + firing > 0, + "{NAME}: zero square blocks split their luma transform under a non-DC chroma mode \ + ({matched} matches, {named_refusals} refusals) -- gate proved nothing this run" + ); + eprintln!( + "{NAME}: {matched} pixel-exact decodes ({named_refusals} named refusals out of \ + {n_attempts} 8-bit attempts), split-tx-with-chroma blocks: {firing} \ + (10-bit stream: {firing10})" + ); + } + /// lane-rectsplit r1 gate (a): a real `aomenc` key frame whose 32x32 /// quadrants split HORZ/VERT into true 32x16/16x32 strips, with the /// tx-size search left at aomenc's own default (ON) -- every sibling @@ -10670,17 +10860,14 @@ mod tests { let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, - // Desaturated (`hue=s=0`, the same guard - // `a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact` - // uses): with the tx-size search ON, a real chroma - // gradient hits a PRE-EXISTING chroma defect on the plain - // SQUARE path -- measured this round with - // `--enable-rect-partitions=0` on this very recipe, seed - // 42, U plane, i.e. with no rect strip in the stream at - // all. Flat chroma keeps that unrelated defect out of - // this gate (gate-recipe-confound class); it is open - // residue in lanes/rectsplit-r1.report.md, not fixed here. - "-vf", "hue=s=0", + // REAL chroma (lane-sqchroma r1 dropped the `hue=s=0` + // guard this recipe carried): the "PRE-EXISTING square-path + // chroma defect" that guard named does not reproduce at + // HEAD -- seeds 42..61, cq 12/20/32/45, plus testsrc2 and + // mandelbrot, all decode pixel-exact with + // `--enable-rect-partitions=0`, and this gate is green with + // the filter gone. Its own gate is + // `a_real_aomenc_intra_stream_with_tx_size_search_and_chroma_decodes_pixel_exact`. "-pix_fmt", "yuv420p", "-f", "yuv4mpegpipe", "-", ]) @@ -10842,10 +11029,9 @@ mod tests { let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, - // Same `hue=s=0` guard as gate (a): a real chroma gradient - // under the tx-size search hits a PRE-EXISTING square-path - // chroma defect that has nothing to do with this lane. - "-vf", "hue=s=0", + // Real chroma: the `hue=s=0` guard gate (a) carried is + // gone (lane-sqchroma r1 -- the square-path chroma defect + // it named does not reproduce at HEAD). "-pix_fmt", "yuv420p", "-f", "yuv4mpegpipe", "-", ]) .stdin(Stdio::null()) @@ -11042,17 +11228,14 @@ mod tests { let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, - // Desaturated (`hue=s=0`, the same guard - // `a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact` - // uses): with the tx-size search ON, a real chroma - // gradient hits a PRE-EXISTING chroma defect on the plain - // SQUARE path -- measured this round with - // `--enable-rect-partitions=0` on this very recipe, seed - // 42, U plane, i.e. with no rect strip in the stream at - // all. Flat chroma keeps that unrelated defect out of - // this gate (gate-recipe-confound class); it is open - // residue in lanes/rectsplit-r1.report.md, not fixed here. - "-vf", "hue=s=0", + // REAL chroma (lane-sqchroma r1 dropped the `hue=s=0` + // guard this recipe carried): the "PRE-EXISTING square-path + // chroma defect" that guard named does not reproduce at + // HEAD -- seeds 42..61, cq 12/20/32/45, plus testsrc2 and + // mandelbrot, all decode pixel-exact with + // `--enable-rect-partitions=0`, and this gate is green with + // the filter gone. Its own gate is + // `a_real_aomenc_intra_stream_with_tx_size_search_and_chroma_decodes_pixel_exact`. "-pix_fmt", "yuv420p", "-f", "yuv4mpegpipe", "-", ]) diff --git a/lanes/sqchroma-r1.report.md b/lanes/sqchroma-r1.report.md new file mode 100644 index 00000000..f6b6823c --- /dev/null +++ b/lanes/sqchroma-r1.report.md @@ -0,0 +1,90 @@ +# lane-sqchroma r1 — the chartered square-path chroma defect does not reproduce + +## Verdict + +The charter's premise is FALSE at HEAD (f31a2c5). A plain SQUARE-partition intra key +frame with real chroma and aomenc's tx-size search left ON decodes **pixel-exact**, in +every recipe I could build around the chartered one, 8-bit and 10-bit. Nothing was +fixed because nothing was broken; what shipped is the missing gate that pins the +capability, and the removal of the stale `hue=s=0` guard the (wrong) premise put into +two live rect gates. + +## Reproduction attempt (the chartered recipe, exactly) + +`gradients_source(seed, 192x128, duration=0.04:rate=25)` -> `yuv420p` (NO `hue=s=0`) -> +aomenc `--cpu-used=0 --threads=1 --row-mt=0 --sb-size=64 --enable-rect-partitions=0 +--enable-ab-partitions=0 --enable-1to4-partitions=0 --min-partition-size=16 +--max-partition-size=32 --enable-restoration=0 --enable-palette=0 --deltaq-mode=0 +--enable-filter-intra=0 --enable-cfl-intra=0 --enable-intrabc=0` (no +`--enable-tx-size-search=0`), decoded by `examples/decode_probe` and diffed plane by +plane against `ffmpeg -i s.obu -pix_fmt yuv420p -f rawvideo`. + +- seeds 42..61 at cq 45: 20/20 exact (Y=0 U=0 V=0 mismatching bytes). +- seeds 42..61 at cq 20: 20/20 exact. +- seed 42 (the charter's named seed) traced: `tx_depth != 0` fires 6 times, `uv_mode` + takes values 0/1/2/5/11 -- the exact "luma tx_depth>0 with a non-DC uv_mode" pairing + the defect was attributed to. Seed 55: 10 depth hits, uv_modes 0/2/7/12. Still exact. +- harder content, same square recipe, cq 12 and 32: `testsrc2`, `mandelbrot`, + `smptebars`, `rgbtestsrc` -- 8/8 exact (streams up to 4 KB, i.e. real chroma residual). +- with `--enable-cfl-intra=1 --enable-filter-intra=1` (the CFL-under-split-luma + candidate the charter names): testsrc2 + mandelbrot at cq 12/32, 4/4 exact. +- free partition tree (no min/max pin) + CFL: 5/6 exact, 1 named refusal + (`HORZ_A/HORZ_B/VERT_A below 16x16` -- an unrelated, already-named refusal). + +So no divergence to localise: no EC_AV1_PREFILT_DUMP split, no EC_TRACE_COEFF ladder, +no lane-sub8 cherry-pick needed (`decode_block` already computes `smooth_neighbor_uv` +from `above_uv_mode`/`left_uv_mode` at decode.rs:5696 -- the square >=16 path never +passed `false`; only the leaf8/split4 paths did, and lane-sub8 r5 fixed those). + +Most likely origin of the premise: it was measured on the pre-merge lane-rectsplit +branch, not on main-with-everything-merged (main f31a2c5 carries lane-tiny, lane-seg, +lane-intrabc and the rectsplit merge itself), and it was recorded as fact in a comment +rather than re-checked. Class: my-charters-shipped-stale-premises. + +## What changed + +- `crates/ec-av1/src/decode.rs:735` — new `SQ_CHROMA_TX_HITS` counter + `sq_chroma_tx_hits()`; + bumped at `decode.rs:5638` in `decode_block` when `tx_select && logical_tx < side && + uv_mode != DC_PRED` (square block whose luma transform split under a non-DC chroma mode). +- `crates/ec-av1/src/stream.rs:9924` — new gate + `a_real_aomenc_intra_stream_with_tx_size_search_and_chroma_decodes_pixel_exact`. +- `crates/ec-av1/src/stream.rs` (3 sites) — `-vf hue=s=0` dropped from the two live rect + gates and the ignored SB-strip gate; the stale "PRE-EXISTING square-path chroma defect" + comments replaced with what was actually measured. Both live gates stay green with real + chroma, so those gates now cover chroma too. + +## Gate + +`a_real_aomenc_intra_stream_with_tx_size_search_and_chroma_decodes_pixel_exact`: +square partitions only, tx-size search at aomenc's default (ON), real chroma, 10 8-bit +attempts (seeds 42..51, cq alternating 20/40) plus one 10-bit stream (`--bit-depth=10`, +sequence-header `bit_depth == 10` asserted before any pixel compare). Hits counted only +over attempts that decoded AND pixel-compared; `firing > 0` hard-asserted; a non-refusal +decode error or any pixel mismatch FAILS, only a missing tool SKIPs. + +``` +CARGO_TARGET_DIR=$HOME/.cache/cargo-target-sqchroma EC_AV1_REQUIRE_AOMENC=1 \ + cargo test -p ec-av1 --lib a_real_aomenc_intra_stream_with_tx_size_search_and_chroma -- --nocapture +``` + +EVIDENCE: gate stderr | 10 8-bit aomenc streams + 1 10-bit, decoded and diffed vs ffmpeg | `11 pixel-exact decodes (0 named refusals out of 10 8-bit attempts), split-tx-with-chroma blocks: 23 (10-bit stream: 2)` — test ok +EVIDENCE: scratchpad sweep.sh/src2.sh output | 40 gradient streams (seeds 42..61 at cq 45 and cq 20) + 12 testsrc2/mandelbrot/smptebars/rgbtestsrc streams, square-only recipe, decode_probe vs ffmpeg rawvideo | every plane Y=0 U=0 V=0 mismatching bytes +EVIDENCE: rectsplit gates with the hue filter removed | `cargo test -p ec-av1 --lib split_transform_horz_vert filter_intra_on_a_horz_vert_strip` | both ok (16 decoded seeds, split-tx delta 3; 30 matches, filter_intra_rect delta 9) + +## Suite + +`EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib` (worktree, target-sqchroma): **275 passed, 0 failed, 23 ignored** in 1647s. + +## Refusals + +None lifted (none were in the way -- the path already decodes). `refusal_inventory` and +`gate_coverage` untouched and green. + +## Residue + +- accepted: the charter's localisation plan (PREFILT dump, chroma tx_type derivation, + CFL-under-split-luma, chroma edge-filter type) was not executed -- there is no + divergence to localise. The tx_type/CFL rules were therefore NOT re-audited against + libaom this round; if a real square chroma defect turns up later it is unexamined ground. +- deferred(rectsplit r2): `a_real_aomenc_stream_with_a_split_transform_superblock_strip_decodes_pixel_exact` + stays `#[ignore]`d (measured RED on luma, seed 50) -- untouched by this lane beyond its comment.