diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index ce9a3d90..c31508f4 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -29,6 +29,8 @@ fn main() { } }; let report = || { + let (sc, dp) = ec_av1::decode::reelc_chroma_counters(); + println!("reelc_chroma: skip_cfl={sc} dir_1to4_pairs={dp}"); let (h, v, c) = ec_av1::stream::rect4_32_counters(); println!("rect4_32: horz={h} vert={v} coded={c}"); let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters(); diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index a614323b..8f6116b2 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2199,6 +2199,24 @@ thread_local! { static RECT4_16_SPLIT_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } +// lane-reelckf r1: the two chroma defects ReelC's key frames exposed -- +// CHROMA_SKIP_CFL counts skipped intra blocks whose chroma is `UV_CFL_PRED` +// (prediction still runs, libaom `predict_and_reconstruct_intra_block`), and +// RECT4_16_CHROMA_DIR counts 1:4 chroma pairs predicted directionally (whose +// intra-edge filter type must come from the chroma-reference position). +thread_local! { + static CHROMA_SKIP_CFL_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static RECT4_16_CHROMA_DIR_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// `(skipped UV_CFL_PRED blocks, directional 1:4 chroma pairs)` -- lane-reelckf. +pub fn reelc_chroma_counters() -> (usize, usize) { + ( + CHROMA_SKIP_CFL_HITS.with(|c| c.get()), + RECT4_16_CHROMA_DIR_HITS.with(|c| c.get()), + ) +} + /// Current values of the 16x16-level 1:4 counters: `(16x4 strips, 4x16 /// strips, chroma-reference strips, strips with a coded residual, strips /// whose luma transform is split)`. @@ -7709,11 +7727,30 @@ fn decode_rect4_16( let (cpx, cpy) = (ppx / 2, ppy / 2); let (cw, ch) = (pw / 2, ph / 2); let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; + if (V_PRED..=D67_PRED).contains(&uv_predict_mode) { + RECT4_16_CHROMA_DIR_HITS.with(|h| h.set(h.get() + 1)); + } let pair_reach = Reach::of_rect(pw, ph, ppx, ppy, y.width, y.height); - let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c); + // libaom `get_intra_edge_filter_type` reads `chroma_above_mbmi` / + // `chroma_left_mbmi` -- the neighbours of the CHROMA REFERENCE + // position, not of the closing strip. At `lmi` the above cell is + // the pair's own first strip, so the mi-exact map missed and the + // coarse fallback answered; the filter type then flipped the + // `use_intra_edge_upsample` decision (blk_wh 12 <= 16 for type 0, + // no upsample for type 1) on directional chroma. + let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(pair_mi.0, pair_mi.1, r, c); let (u_levels, v_levels) = if skip { let zeros = vec![0i32; cw * ch]; - for (buf, _plane) in [(&mut *u, 1usize), (&mut *v, 2)] { + // libaom `predict_and_reconstruct_intra_block` (decodeframe.c + // ~1000) predicts EVERY plane and only guards the residual on + // `mbmi->skip_txfm`, so a skipped `UV_CFL_PRED` block is still + // DC + alpha * luma-AC. Dropping the CfL term here left a + // chroma-only bounded box on ReelC's key frames. + let ac = alpha.map(|_| cfl_ac_q3_rect(y, ppx, ppy, pw, ph)); + if alpha.is_some() { + CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); + } + for (buf, plane) in [(&mut *u, 1usize), (&mut *v, 2)] { buf.reconstruct_rect( cpx, cpy, @@ -7723,7 +7760,9 @@ fn decode_rect4_16( angle_delta_uv, pair_reach, &zeros, - None, + alpha + .zip(ac.as_deref()) + .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), None, smooth_neighbor_uv, ); @@ -10512,8 +10551,14 @@ fn decode_leaf_split4( neighbours.left_uv_mode[r] = uv_predict_mode; neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); let (u_grid, v_grid): (Vec, Vec) = if leaf_skips[3] { - u.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], None, None, smooth_neighbor_uv); - v.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], None, None, smooth_neighbor_uv); + // As the rect4 pair above: a skipped block still predicts, and CfL is + // prediction (libaom `predict_and_reconstruct_intra_block`). + let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); + if alpha.is_some() { + CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); + } + u.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv); + v.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv); (vec![0i32; 16], vec![0i32; 16]) } else { let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); @@ -10865,8 +10910,14 @@ fn decode_leaf_rect8( neighbours.left_uv_mode[r] = uv_predict_mode; neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); let (u_grid, v_grid): (Vec, Vec) = if leaf_skips[1] { - u.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], None, None, smooth_neighbor_uv); - v.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], None, None, smooth_neighbor_uv); + // As the rect4 pair above: a skipped block still predicts, and CfL is + // prediction (libaom `predict_and_reconstruct_intra_block`). + let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); + if alpha.is_some() { + CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); + } + u.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv); + v.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv); (vec![0i32; 16], vec![0i32; 16]) } else { let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 15ad6113..f17b682e 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -11441,6 +11441,166 @@ mod tests { ); } + /// lane-reelckf r1: ReelC's key frames decoded with no refusal and WRONG + /// CHROMA (luma bit-exact at every one of ten seek points), from two + /// defects this gate pins: + /// + /// 1. a SKIPPED intra block whose `uv_mode` is `UV_CFL_PRED` still + /// predicts with the CfL term -- libaom + /// `predict_and_reconstruct_intra_block` (decodeframe.c) calls + /// `av1_predict_intra_block_facade` unconditionally and only guards + /// the residual on `mbmi->skip_txfm`. Three skip arms (the 16x16-level + /// 1:4 chroma pair and the two sub-8x8 4x4 groups) dropped it. + /// 2. the chroma intra-edge FILTER TYPE of a 1:4 pair comes from the + /// CHROMA REFERENCE position (libaom `get_intra_edge_filter_type` -> + /// `chroma_above_mbmi`/`chroma_left_mbmi`), not from the closing + /// strip: at the closing strip the above cell is the pair's own first + /// strip, the mi-exact UV map misses and the coarse fallback answers, + /// which flipped `av1_use_intra_edge_upsample` (blk_wh 12 <= 16 for + /// type 0, no upsample for type 1) and moved directional chroma by + /// +-1..3. + /// + /// Recipe is a real `aomenc --sb-size=128 --cpu-used=0` (the shape ReelC + /// uses) with 1:4 partitions and CfL on, both bit depths, and both + /// counters hard-asserted per compared attempt. + #[test] + fn a_real_aomenc_sb128_stream_whose_skipped_cfl_and_1to4_chroma_pairs_decode_pixel_exact() { + const NAME: &str = + "a_real_aomenc_sb128_stream_whose_skipped_cfl_and_1to4_chroma_pairs_decode_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 mut compared = 0usize; + let (width, height) = (256usize, 256usize); + for depth in [8usize, 10] { + let pix = if depth == 10 { "yuv420p10le" } else { "yuv420p" }; + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", + &format!( + "mandelbrot=size={width}x{height},\ + noise=alls=20:allf=t+u:all_seed=7" + ), + "-pix_fmt", pix, "-strict", "-1", "-t", "1", "-vframes", "1", + "-f", "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "{NAME}: ffmpeg fixture (depth={depth}): {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let encode = || { + let mut child = Command::new(aomenc_path()) + .args([ + "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=52", + "--cpu-used=0", "--threads=1", "--row-mt=0", "--sb-size=128", + "--kf-max-dist=0", "--enable-rect-partitions=1", + "--enable-ab-partitions=0", "--enable-1to4-partitions=1", + "--enable-cfl-intra=1", "--enable-tx-size-search=0", + "--enable-filter-intra=0", "--enable-intra-edge-filter=1", + "--reduced-tx-type-set=1", "--min-partition-size=4", + "--max-partition-size=32", + &format!("--input-bit-depth={depth}"), + &format!("--bit-depth={depth}"), + "--obu", "-o", "-", "-", + ]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "aomenc refused the fixture (depth={depth}): {}", + String::from_utf8_lossy(&out.stderr) + ); + out.stdout + }; + let stream = encode(); + assert_eq!( + stream, + encode(), + "{NAME}: aomenc output is not reproducible (depth={depth})" + ); + let before = crate::decode::reelc_chroma_counters(); + let frames = match decode_stream(&stream) { + Ok(frames) => frames, + Err(e) => { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{NAME}: decode failed (depth={depth}): {msg}" + ); + eprintln!("{NAME}: depth={depth} arm refused (not compared): {msg}"); + continue; + } + }; + compared += 1; + // Counters read only on an attempt that decoded AND is compared. + let after = crate::decode::reelc_chroma_counters(); + let (skip_cfl, dir_pairs) = (after.0 - before.0, after.1 - before.1); + let ffmpeg_frames = if depth == 10 { + ffmpeg_decode_sequence_10bit(&stream, width, height, frames.len()) + } else { + ffmpeg_decode_sequence(&stream, width, height, frames.len()) + }; + assert_eq!(ffmpeg_frames.len(), frames.len(), "{NAME}: ffmpeg frame count"); + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!(got.y, want.y, "{NAME} frame {i} luma (depth={depth})"); + assert_eq!( + got.u, want.u, + "{NAME} frame {i} U (depth={depth}, {skip_cfl} skipped-CfL blocks, \ + {dir_pairs} directional 1:4 chroma pairs)" + ); + assert_eq!( + got.v, want.v, + "{NAME} frame {i} V (depth={depth}, {skip_cfl} skipped-CfL blocks, \ + {dir_pairs} directional 1:4 chroma pairs)" + ); + } + // Defect 1 (skipped `UV_CFL_PRED`) is NOT asserted here: 20 aomenc + // recipes (cq 40..63, 9 lavfi sources, min/max-partition 4..64, + // both `--sb-size=128` and 64) never made the encoder pick a + // skipped 1:4 / sub-8x8 CfL block, so an assert would only ever + // fire on his real film. Its evidence is ReelC's own key frames + // (lanes/reelckf-r1.report.md): ss=2700 went 499 wrong chroma + // samples -> 0 on this fix alone. The count is printed so a future + // recipe that does reach it is visible immediately. + assert!( + dir_pairs > 0, + "{NAME}: no directional 1:4 chroma pair decoded (depth={depth}) -- defect 2 \ + is not exercised" + ); + eprintln!( + "{NAME}: pixel-exact at {depth}-bit over {} frames, skipped_cfl={skip_cfl} \ + directional_1to4_chroma_pairs={dir_pairs}", + frames.len() + ); + } + assert!( + compared > 0, + "{NAME}: every arm refused -- the gate compared no pixels at all" + ); + } + /// lane-ab16 r1: real `aomenc --enable-ab-partitions=1` streams whose /// 16x16 blocks are split T-shaped (`PARTITION_HORZ_A`/`_HORZ_B`/ /// `_VERT_A`/`_VERT_B`: two 8x8 squares plus one 16x8/8x16 strip), the diff --git a/lanes/reelckf-r1.report.md b/lanes/reelckf-r1.report.md new file mode 100644 index 00000000..f839a7c3 --- /dev/null +++ b/lanes/reelckf-r1.report.md @@ -0,0 +1,99 @@ +# lane-reelckf r1 — ReelC key frames: two chroma-only reconstruction defects + +Base: `lane-sb128b` e8e64a6 (= main 18bf7dc + the 128 NONE block fix). + +## Premise re-measured (not taken from the charter) +census4's `reelc4.tsv` was recorded on main 18bf7dc, where every ReelC segment +REFUSED. On this base all ten non-`sb128c` key frames decode with **no refusal** +and **luma bit-exact**, chroma wrong — which is what made the entropy stream an +exonerated suspect from the first measurement. + +## Root causes (both chroma, both cited to libaom) + +1. **A skipped intra block still predicts, and CfL is prediction.** + libaom `predict_and_reconstruct_intra_block` (decodeframe.c) calls + `av1_predict_intra_block_facade` unconditionally and guards only the + residual on `mbmi->skip_txfm`. Three of our skip arms dropped the CfL term + and predicted plain DC: + - `crates/ec-av1/src/decode.rs:7719` — the 16x16-level 1:4 chroma pair + (`decode_rect4_16`). + - `crates/ec-av1/src/decode.rs:10528`, `:10891` — the two sub-8x8 4x4-group + chroma arms. + ReelC ss=2700: chroma-only bounded box at chroma (48..79, 464..492), the + `VERT_4` pair at mi(24,234)/(24,235) — aomdec `EC_TRACE_MODE` prints + `uv_mode=13 skip=1` there. 499 wrong samples -> **0**. + +2. **The 1:4 pair's chroma intra-edge FILTER TYPE is read at the chroma + reference, not at the closing strip.** + libaom `get_intra_edge_filter_type` (reconintra.c) reads + `chroma_above_mbmi`/`chroma_left_mbmi`, i.e. the neighbours of the + chroma-reference mi. We passed the closing strip's own mi, whose above cell + is the pair's FIRST strip: the mi-exact UV map missed, the coarse 16px + fallback answered, and the filter type flipped + `av1_use_intra_edge_upsample` (`blk_wh = 8 + 4 = 12 <= 16` upsamples for + type 0, never for type 1). `crates/ec-av1/src/decode.rs:7714`. + ReelC ss=5400, the `HORZ_4` pair at mi(82,83)/76: aomdec + `EC_PRED ... mode=5 p_angle=107 ft=0`, `EC_PREDOUT row0=415,431,453,471,...` + vs our `OUR_PRED ... row0=[415,431,452,470,...]`. 46 wrong samples -> **0**. + +## Files changed +- `crates/ec-av1/src/decode.rs` — the three skip arms now pass the CfL term; + the 1:4 pair's `smooth_uv_neighbour` is read at `pair_mi`; two new + counters + `pub fn reelc_chroma_counters()`. +- `crates/ec-av1/src/stream.rs` — new gate + `a_real_aomenc_sb128_stream_whose_skipped_cfl_and_1to4_chroma_pairs_decode_pixel_exact`. +- `crates/ec-av1/examples/decode_probe.rs` — prints `reelc_chroma:` counters. + +## Sweep for the same shape +`grep -n "u\.reconstruct\|v\.reconstruct\|buf\.reconstruct"` over decode.rs, then +every `if skip {` arm (21 sites) inspected: the square path (`:9416`), the rect +strip paths (`:6490`, `:6930`, `:7248`, `:8034`) and the inter paths already +computed `ac` inside their skip arm. The three fixed above were the only +chroma-CfL-dropping arms. All 11 `smooth_uv_neighbour` call sites checked: the +other ten pass a block/group ORIGIN mi (`leaf_mi`, `r*(SUB/MI)`), which already +is the chroma reference; only `decode_rect4_16` passed a non-origin mi. + +## Gate +`cargo test -p ec-av1 --lib a_real_aomenc_sb128_stream_whose_skipped_cfl -- --nocapture` +(real aomenc `--sb-size=128 --cpu-used=0 --enable-1to4-partitions=1 +--enable-cfl-intra=1 --min-partition-size=4 --max-partition-size=32 +--cq-level=52`, 256x256 `mandelbrot+noise(all_seed=7)`, 8- and 10-bit arms, +aomenc output hashed twice for reproducibility, decode error tolerated only if +the message contains "unsupported", `compared > 0` asserted). + +EVIDENCE: $HOME/.cache/reelckf-suite-r1.log (gate stdout quoted below) | cargo test ... --nocapture | pixel-exact at 8-bit and 10-bit, directional_1to4_chroma_pairs=11 per arm, skipped_cfl=0 + +## ReelC key-frame table (1 frame per seek point, `EC_PROBE_OUT16` vs ffmpeg yuv420p10le) + +| ss | before (this base) | after | +|----|--------------------|-------| +| 0 | 0 | 0 | +| 900 | 4173 (U 3107 / V 1066, max 7) | **0** | +| 1800 | 14402 (max 10) | **0** | +| 2700 | 499 (max 3) | **0** | +| 3600 | 119652 (max 37) | **0** | +| 4500 | 0 | 0 | +| 5400 | 46 (max 4) | **0** | +| 6300 | 33742 (max 7) | **0** | +| 7200 | 52891 (max 11) | **0** | +| 9900 | 4131 (max 7) | **0** | +| 8100 | frame 0 refuses (128-root non-SPLIT) — lane-sb128c | unchanged | +| 9000 | frame 0 refuses (128-root non-SPLIT) — lane-sb128c | unchanged | + +Luma was bit-exact in every row, before and after. + +EVIDENCE: $HOME/.cache/reelckf-work/xo_*.raw vs xr_*.raw | ffmpeg -ss -t 2 -c:v copy -f obu, trunc.py to 1 frame, decode_probe EC_PROBE_OUT16, ffmpeg -pix_fmt yuv420p10le | samples_diff 0/2280960 on all ten decodable key frames + +## Residue +- deferred(a lavfi/aomenc recipe or a committed stream fixture that fires a + SKIPPED 1:4 or sub-8x8 CfL block) — defect 1 has no synthetic gate. 20 aomenc + recipes were swept (cq 40/44/46/50/52/55/58/62/63 x mandelbrot, blurred + mandelbrot, noised mandelbrot, testsrc2, gradients, smptebars, cellauto, life, + grey+noise; min-partition 4, max-partition 16/32/64, sb-size 128) and + `skip_cfl` stayed 0 in all of them. Its evidence is ReelC ss=2700 (499 -> 0) + and the counter is printed by the gate and by `decode_probe`, so the first + recipe that reaches it is visible. +- accepted — ss=8100 / ss=9000 still refuse inside frame 0 on the 128-root + non-SPLIT partition; that refusal belongs to lane-sb128c. +- No refusal was lifted this round, so `refusal_inventory.rs` / + `gate_coverage.rs` are unchanged.