From 04d785155a6d3c78d29588e7c22756e58f4a0bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 24 Sep 2026 12:18:03 +0300 Subject: [PATCH] lane-av112bitg: lift the 12-bit film grain refusal to the witnessed set aomenc emits film grain at 12 bits on the first try (--film-grain-test=N maps to film_grain_test_vectors[N-1] with bit_depth overwritten from the sequence, encoder_utils.c:792); all of vectors 1..8 emit apply_grain=1 on key and inter frames alike. - film_grain.rs: no arithmetic change -- every bit-depth-sensitive site (grain_range, gauss_sec_shift, scale_lut/LUT expansion, chroma index combine and offsets, legal-range clamps, overlap-blend clamp) re-diffed against the oracle grain_synthesis.c and already 12-bit exact. - stream.rs: grain_refusal and its four call sites deleted; the refusal gate becomes a_real_aomenc_12bit_film_grain_stream_decodes_pixel_exact (--film-grain-test=5, 2 luma + 9 cb + 9 cr points, overlap on, clip on, inter frame inheriting via update_grain=0) -- byte-exact vs ffmpeg on every plane of both frames, grain_hits asserted. Warp pinned off by name: this lane lifts GRAIN only, and the unpinned arm trips the sibling's intact 12-bit warp refusal. - refusal_inventory: 37 -> 36, every proven refusal still names a test. - Regressions: parent 12-bit key/inter/compound witnesses, five 10-bit film gates, warp + screen-content refusals, full inventory module -- 26/26 green, 0 warnings on --all-targets. --- crates/ec-av1/src/refusal_inventory.rs | 12 ++- crates/ec-av1/src/stream.rs | 140 ++++++++++++++++--------- lanes/av112bitg.report.md | 90 ++++++++++++++++ 3 files changed, 190 insertions(+), 52 deletions(-) create mode 100644 lanes/av112bitg.report.md diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index e5ef31e2..4d2d074d 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -80,8 +80,11 @@ const REFUSALS: &[&str] = &[ // lane-av112bitc: compound joined the witnessed set (six-frame 12-bit // compound stream byte-exact vs ffmpeg; the combines already absorbed // the CONV_BUF gain drop through `INTER_POST_ROUND - delta`). + // lane-av112bitg: film grain joined the witnessed set (two-frame + // `--film-grain-test=5` stream byte-exact vs ffmpeg; the gate that + // replaced the refusal is + // `a_real_aomenc_12bit_film_grain_stream_decodes_pixel_exact`). "warped motion at 12 bits (warp's reduce bits inherit the 12-bit round_0 and no 12-bit warp witness exists)", - "film grain synthesis at 12 bits (no byte-exact 12-bit grain witness exists: the 12-bit gates encode with grain off)", "a 12-bit frame with screen content tools (allow_screen_content_tools=1: neither palette nor intrabc has a 12-bit witness)", // lane-av1txr: the silent-garbage guard. A 4:4:4/4:2:2 stream used to // decode with no refusal and wrong pixels (every chroma extent is @@ -363,14 +366,13 @@ const PROVEN: &[(&str, &str)] = &[ // lane-av112bitc: the compound pair left with its refusal -- the // six-frame compound stream now DECODES byte-exact // (`a_real_aomenc_12bit_compound_inter_sequence_decodes_pixel_exact`). + // lane-av112bitg: the grain refusal left with its witness -- the + // two-frame 12-bit grain stream now DECODES byte-exact + // (`a_real_aomenc_12bit_film_grain_stream_decodes_pixel_exact`). ( "warped motion at 12 bits (warp's reduce bits inherit the 12-bit round_0 and no 12-bit warp witness exists)", "a_12bit_warped_motion_stream_is_refused_by_name", ), - ( - "film grain synthesis at 12 bits (no byte-exact 12-bit grain witness exists: the 12-bit gates encode with grain off)", - "a_12bit_film_grain_stream_is_refused_by_name", - ), ( "a 12-bit frame with screen content tools (allow_screen_content_tools=1: neither palette nor intrabc has a 12-bit witness)", "a_12bit_screen_content_stream_is_refused_by_name", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 99fb48d4..2a5bf42a 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -38,21 +38,6 @@ fn no_grain() -> bool { std::env::var_os("EC_AV1_NO_GRAIN").is_some() } -/// lane-av112bit: film grain synthesis has no 12-bit byte-exact witness (the -/// lane's witnesses encode with grain off), so a 12-bit frame that would -/// synthesise grain is refused by name at output rather than trusted -- -/// `film_grain.rs` is bit-depth generic since lane-hbd10, but generic is not -/// proven until a real 12-bit grain stream compares exact. -fn grain_refusal(bit_depth: u32) -> Result<()> { - if bit_depth == 12 { - return Err(Error::unsupported( - "AV1 decode_stream", - "film grain synthesis at 12 bits (no byte-exact 12-bit grain witness exists: the 12-bit gates encode with grain off)", - )); - } - Ok(()) -} - // lane-hidden r1: where `EC_AV1_FINAL_DUMP`'s per-frame dump goes for the // current thread, overriding the environment variable. Every ffmpeg/aomdec // pixel gate in this repo compares SHOWN frames only (class @@ -699,7 +684,6 @@ pub fn decode_stream_with_threads( pictures_shown += 1; let grain = header.film_grain; let seq = SeqFlags::read(&parser); - grain_refusal(u32::from(seq.bit_depth))?; let tx = tx.clone(); jobs.submit(move || { let reply = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { @@ -751,13 +735,6 @@ pub fn decode_stream_with_threads( } picture }; - if header.film_grain.apply_grain { - grain_refusal( - parser - .sequence_header() - .map_or(8, |seq| u32::from(seq.color_config.bit_depth)), - )?; - } let output = if header.film_grain.apply_grain && !no_grain() { // lane-hbd10 r1: grain synthesis is bit-depth generic // (`film_grain.rs`'s `grain_range`/`scale_lut`, spec 7.18.3). @@ -1097,9 +1074,6 @@ pub fn decode_stream_with_threads( // Grain is synthesised for OUTPUT only: a hidden frame is // reported to the sink clean, exactly as the inline path // below reports it (spec 7.18.3.1). - if header.film_grain.apply_grain { - grain_refusal(u32::from(seq.bit_depth))?; - } let output = if shown && header.film_grain.apply_grain && !no_grain() { std::sync::Arc::new(crate::film_grain::apply_grain( &picture, @@ -1234,9 +1208,6 @@ pub fn decode_stream_with_threads( decode::note_fwd_kf(); } if header.show_frame { - if header.film_grain.apply_grain { - grain_refusal(u32::from(seq.bit_depth))?; - } let output = if header.film_grain.apply_grain && !no_grain() { let mc_identity = seq.mc_identity; std::sync::Arc::new(crate::film_grain::apply_grain( @@ -1676,10 +1647,12 @@ fn decode_frame( // the CONV_BUF gain), and CDEF/deblock/quant tables/SGR/palette reads/ // superres upscale were already bit-depth generic (10-bit-proven). // Everything no 12-bit witness reached stays refused BY NAME at its own - // site: warp (decode.rs, warp's reduce bits inherit the 12-bit round_0), - // film grain (`grain_refusal`, the witnesses run with grain off), and - // any per-feature gap the witnesses' counters prove unreached -- see the - // 12-bit refusals in decode.rs and `refusal_inventory.rs`. + // site: warp (decode.rs, warp's reduce bits inherit the 12-bit round_0) + // and any per-feature gap the witnesses' counters prove unreached -- see + // the 12-bit refusals in decode.rs and `refusal_inventory.rs`. + // lane-av112bitg: film grain joined the witnessed set (`grain_refusal` + // deleted; the two-arm witness is + // `a_real_aomenc_12bit_film_grain_stream_decodes_pixel_exact`). // lane-av1txr: `color_config.subsampling_x/y` (spec 5.5.2) are parsed by // `ec-av1-syntax` -- profile 1 forces 4:4:4, profile 2 below 12-bit forces // 4:2:2, and the sRGB identity description forces full-range 4:4:4 -- but @@ -7794,9 +7767,12 @@ pub(crate) mod tests { // restoration (Wiener AND SGR), and translational subpel motion // compensation through the bit-depth-parameterised `round_0`/`round_1` // (libaom `convolve.h:81..86`). Everything else stays refused by name: - // warp, film grain, screen content tools (palette/intrabc) at the frame - // level, compound inter blocks at the mode read, each with its own - // real-stream refusal gate below. + // warp and screen content tools (palette/intrabc) at the frame level, + // each with its own real-stream refusal gate below. + // lane-av112bitc: compound joined the witnessed set (refusal gate became + // the six-frame compound witness). + // lane-av112bitg: film grain joined the witnessed set (refusal gate became + // `a_real_aomenc_12bit_film_grain_stream_decodes_pixel_exact`). // ------------------------------------------------------------------- /// The 12-bit witness source, byte-identical to the lane's committed @@ -8123,25 +8099,95 @@ pub(crate) mod tests { } } - /// The 12-bit film grain refusal: a real `--film-grain-test=1` stream - /// (grain params present AND applied) is refused by name at output. + /// lane-av112bitg: the 12-bit film grain witness. A real + /// `aomenc --bit-depth=12 --profile=2 --film-grain-test=5` stream -- grain + /// params present AND applied (2 luma + 9 cb + 9 cr scaling points, + /// overlap blending on, clip-to-restricted-range on; the second frame is + /// an INTER frame that inherits the params via `update_grain = 0`) -- + /// decoded byte-exact against ffmpeg's own 12-bit decode of the identical + /// bytes (ffmpeg synthesizes grain by default; no grain-disable flag is + /// passed anywhere in this file). Proves the bit-depth-generic grain path + /// at 12 bits: `grain_range`'s ±(128 << 4) AR clamp and overlap-blend + /// clamp, the `gauss_sec_shift = 12 - bd` Gaussian shift, the 4096-entry + /// expanded scaling-LUT interpolation, and the `<< (bit_depth - 8)` + /// chroma offsets and legal-range clamps. HARD-asserts the parsed headers + /// really carry that coverage and `film_grain::grain_hits()` fired, so + /// neither a params-without-apply stream nor a silently-skipped synthesis + /// can pass vacuously. #[test] - fn a_12bit_film_grain_stream_is_refused_by_name() { - const NAME: &str = "a_12bit_film_grain_stream_is_refused_by_name"; + fn a_real_aomenc_12bit_film_grain_stream_decodes_pixel_exact() { + const NAME: &str = "a_real_aomenc_12bit_film_grain_stream_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 y4m = y4m_12bit_subpel_source(1); - let stream = encode_12bit(&y4m, 1, &["--film-grain-test=1"]); - let err = decode_stream(&stream).unwrap_err().to_string(); + let y4m = y4m_12bit_subpel_source(2); + // Warp stays refused at 12 bits (lane-av112bitg lifts GRAIN only), so + // the arm pins both motion tools off exactly once each -- aomenc + // otherwise picks warped motion on this fixture's sub-pel motion. + let stream = encode_12bit( + &y4m, + 2, + &[ + "--film-grain-test=5", + "--enable-warped-motion=0", + "--enable-global-motion=0", + ], + ); + assert_12bit_sequence_header(&stream, NAME); + // The arm is only honest if the stream itself carries the coverage: + // grain applied on every frame, scaling points on all three planes, + // overlap blending and restricted-range clipping on. + let mut probe = Av1Parser::new(); + let mut pos = 0usize; + let mut grain_frames = 0usize; + while pos < stream.len() { + let obu = probe.parse_obu(&stream[pos..]).unwrap(); + pos += obu.total_size; + if let ObuKind::Frame(header, _) = obu.kind { + let fg = &header.film_grain; + assert!( + fg.apply_grain, + "{NAME}: frame without apply_grain -- aomenc emitted params it does not apply" + ); + assert!( + fg.num_y_points > 0 && fg.num_cb_points > 0 && fg.num_cr_points > 0, + "{NAME}: grain without all three planes' scaling points -- the bit-depth \ + LUT math would not be exercised" + ); + assert!( + fg.overlap_flag, + "{NAME}: overlap off -- the grain_min/grain_max blend clamp is not exercised" + ); + assert!( + fg.clip_to_restricted_range, + "{NAME}: clip_to_restricted_range off -- the << (bd - 8) legal-range clamps \ + are not exercised" + ); + grain_frames += 1; + } + } + assert_eq!(grain_frames, 2, "{NAME}: expected grain on both frames"); + let before = crate::film_grain::grain_hits(); + let frames = decode_stream(&stream) + .unwrap_or_else(|e| panic!("{NAME}: decode_stream refused a real 12-bit grain stream: {e}")); + assert_eq!(frames.len(), 2); assert!( - err.contains( - "film grain synthesis at 12 bits (no byte-exact 12-bit grain witness exists: the 12-bit gates encode with grain off)" - ), - "{NAME}: expected the 12-bit grain refusal, got: {err}" + crate::film_grain::grain_hits() > before, + "{NAME}: matched but zero grain_hits -- grain synthesis never ran" ); + let ffmpeg_frames = ffmpeg_decode_sequence_12bit(&stream, 160, 128, 2); + for (i, (ours, theirs)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!(ours.y, theirs.y, "{NAME}: luma vs ffmpeg (12-bit grain, frame {i})"); + assert_eq!(ours.u, theirs.u, "{NAME}: U vs ffmpeg (12-bit grain, frame {i})"); + assert_eq!(ours.v, theirs.v, "{NAME}: V vs ffmpeg (12-bit grain, frame {i})"); + } + eprintln!("{NAME}: byte-exact, grain_hits={}", crate::film_grain::grain_hits()); } /// The 12-bit screen-content refusal: a real `--tune-content=screen diff --git a/lanes/av112bitg.report.md b/lanes/av112bitg.report.md new file mode 100644 index 00000000..201423f1 --- /dev/null +++ b/lanes/av112bitg.report.md @@ -0,0 +1,90 @@ +# lane-av112bitg — 12-bit film grain joins the witnessed set + +Base 786c15b6 (lane-av112bitc), worktree `edith_codecs-av112bg`. The charter: +lift the `film grain synthesis at 12 bits` refusal exactly as far as a real +`aomenc --bit-depth=12` grain stream proves byte-exactness against ffmpeg, and +leave warp and screen content refused. Sibling lane av112bitw owns warp; +`film_grain.rs` needed no arithmetic change, so the two lanes share no code +except the refusal inventory rows (grain rows only here). + +## Emission (charter gate 1: passed on try 1) + +`--film-grain-test=N` maps to libaom's `film_grain_test_vectors[N-1]` +(`av1/encoder/grain_test_vectors.h`); `av1_update_film_grain_parameters` +overwrites the vector's static `bit_depth: 8` with the sequence's +(`encoder_utils.c:792`), and `av1_update_film_grain_parameters_seq` turns on +`film_grain_params_present`. Probed all of vectors 1..8 on the lane's 12-bit +sub-pel fixture: every one emits `apply_grain=1` on BOTH the key and the inter +frame header (vectors 1..8, 2-frame streams, all parsed and printed by a +scratch gate before the lift). No try failed, so the "8 tries" stop condition +never came close. + +## The bit-depth audit (no assumptions) + +Every bit-depth-sensitive site of `film_grain.rs` re-diffed against the oracle +`~/.cache/aom-oracle/src/av1/decoder/grain_synthesis.c` (v3.13.3 @ 92d4c37): + +| site | oracle | ours | +|---|---|---| +| `grain_min/max = ±(128 << (bd-8))` AR + blend clamp | `:1043-1045` | `grain_range` | +| `gauss_sec_shift = 12 - bd + grain_scale_shift` | `:468`, `:503` | both `generate_*_grain_block`s | +| `scale_LUT` shift-4 interpolation, `x == 255` short-circuit | `:616-626` | `scale_lut` | +| expanded LUT (4096 entries at 12-bit) | per-call `scale_LUT` | `expand_scaling_lut` (same function tabulated) | +| chroma index combine `((avg*luma_mult + mult*chroma) >> 6) + offset`, clamp `0..(256 << (bd-8)) - 1` | `:815-819`, `:830-834` | `chroma_noise_row_scalar` + ChromaNoise | +| `cb/cr_offset = (x << (bd-8)) - (1 << bd)` | `:752`, `:757` | `add_noise_to_block` | +| legal-range clamps `16/235/240 << (bd-8)`, `mc_identity` chroma quirk | `:781-795` | `add_noise_to_block` | +| 4:2:0 chroma luma average `(l0 + l1 + 1) >> 1` (horizontal PAIR, not a quad) | `:801-806` | `chroma_noise_row_scalar` | +| ver/hor overlap blends clamped to `grain_min/grain_max` | `:912-970` | `ver_overlap_inplace`/`hor_overlap_inplace` | + +The AVX2 kernels are parameter-shaped (`index_max`, min/max, LUT length all +inputs; the `vpmaddwd` luma pair sum stays exact at 12-bit since +`2 * 4095 < 2^15`) and this machine's decode path runs them (chroma blocks are +15 wide -> 8+4+3 tails; luma 30 wide -> 8+8+8+4+2), so the witness exercised +SIMD and scalar alike. `apply_grain` call sites already thread the parsed +sequence `bit_depth` (the `map_or(8, ...)` site included). + +## The witness + +`stream.rs`: `grain_refusal` and its four call sites deleted; the refusal gate +became `a_real_aomenc_12bit_film_grain_stream_decodes_pixel_exact`: + +- `aomenc --profile=2 --bit-depth=12 --film-grain-test=5 + --enable-warped-motion=0 --enable-global-motion=0`, 2 frames over the lane's + 160x128 C420p12 sub-pel fixture. Vector 5 carries 2 luma + 9 cb + 9 cr + scaling points, `overlap_flag=1`, `clip_to_restricted_range=1`; the inter + frame codes `update_grain=0` (params inherited from the ref slot, new seed + only), so the gate also exercises the spec 5.9.30 persistence path and grain + on a non-key output. +- Warp is pinned OFF by name: the unpinned first run tripped the (sibling's, + intact) `warped motion at 12 bits` refusal — this lane lifts GRAIN only, so + the arm stays inside the witnessed set. Each flag spelled exactly once + (aomenc LAST-occurrence precedence). +- Non-vacuity: parsed-header asserts demand `apply_grain`, all three planes' + scaling points, overlap and restricted-range clipping on BOTH frames; + `film_grain::grain_hits()` must advance; ffmpeg decodes with grain ON by + default and must produce exactly 2 yuv420p12le frames. +- Result: **byte-exact on every plane of both frames** (grain_hits=2). ffmpeg + synthesized the identical grain from the identical reconstruction. + +## Inventory + +`refusal_inventory.rs`: grain REFUSALS string and PROVEN row deleted (37 -> 36 +refusals), lane-av112bitg notes added next to the lane-av112bitc ones. +`the_decode_path_refuses_exactly_the_listed_cases` and +`every_proven_refusal_names_a_test_that_exists` both green. + +## Regression (all green, same run) + +- `a_real_aomenc_12bit_stream_decodes_pixel_exact` — parent 12-bit KEY fixture, grain off, byte-exact. +- `a_real_aomenc_12bit_inter_sequence_decodes_pixel_exact`, + `a_real_aomenc_12bit_compound_inter_sequence_decodes_pixel_exact` — 12-bit inter/compound witnesses. +- `a_real_aomenc_10bit_film_grain_stream_decodes_pixel_exact` + the other four `10bit_film` gates. +- `a_12bit_warped_motion_stream_is_refused_by_name`, + `a_12bit_screen_content_stream_is_refused_by_name` — sibling refusals intact. +- Full `refusal_inventory` module. 26/26 tests, 0 warnings on + `cargo check --all-targets -p ec-av1`. + +## Open + +None for grain. Warp (`a_12bit_warped_motion_stream_is_refused_by_name`, +sibling lane av112bitw) and screen content tools stay refused by name.