diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b4a7ad28..b23b0066 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1964,6 +1964,19 @@ pub(crate) fn intra_rect_strip_in_inter_hits(class: usize) -> usize { INTRA_RECT_IN_INTER_HITS.with(|c| c.get()[class]) } +// lane-intrasplit r1: intra HORZ/VERT strips inside an INTER frame whose luma +// transform is SPLIT, bucketed by the read `tx_depth` (index 1 and 2; index 0 +// is never incremented) -- the gate's proof that the lifted path ran per depth. +thread_local! { + static INTRA_RECT_IN_INTER_SPLIT_TX_HITS: std::cell::Cell<[usize; 3]> = + const { std::cell::Cell::new([0; 3]) }; +} + +/// Current value of [`INTRA_RECT_IN_INTER_SPLIT_TX_HITS`], indexed by tx depth. +pub(crate) fn intra_rect_in_inter_split_tx_hits() -> [usize; 3] { + INTRA_RECT_IN_INTER_SPLIT_TX_HITS.with(std::cell::Cell::get) +} + // lane-rectx r5: how many times a NON-strip `kf_y_mode` reader // ([`decode_block`], [`decode_block_rect`], [`decode_block_rect64`]) took an // above/left mode from the mi-exact map that DIFFERS from its coarse 16x16 @@ -6212,7 +6225,9 @@ fn decode_intra_rect_in_inter( } let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c); let depth = if TX_SELECT_INTER.with(std::cell::Cell::get) { - let ctx = tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh); + // The INTER frame's own `TXFM_CONTEXT` bands, not the key frame's + // deblock-grid approximation -- [`tx_size_context_txfm_rect`]. + let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64)); match tx_cat { 1 => dec.symbol(&mut cdfs.tx_size_cat1[ctx]), 2 => dec.symbol(&mut cdfs.tx_size_cat2[ctx]), @@ -6223,15 +6238,14 @@ fn decode_intra_rect_in_inter( }; if depth != 0 { TX_DEPTH_HITS.with(|h| h.set(h.get() + 1)); - // merge b51719f+lane-intrarect (class merge-cross-product-defect): - // main's 10-bit `split_transform_intra_block` gate reaches this arm - // with a nonzero tx depth, and the split rect-TU walk mis-decodes it - // (2340 luma samples off at seed 51; refusing here restores that gate - // while the depth-0 strip the lane proved keeps decoding). The lane's - // own gate never exercised a split strip. - return Err(unsupported( - "a split (nonzero tx_depth) transform on an intra HORZ/VERT strip in an inter frame", - )); + // lane-intrasplit r1: a split intra strip in an inter frame walks the + // key frame's own per-TU path ([`decode_rect_split`] below); what was + // wrong when this refusal was written was the depth SYMBOL, not the walk. + INTRA_RECT_IN_INTER_SPLIT_TX_HITS.with(|h| { + let mut v = h.get(); + v[depth.min(2)] += 1; + h.set(v); + }); } let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth); let modes = RectStripModes { @@ -6265,6 +6279,22 @@ fn decode_intra_rect_in_inter( base_q_idx, REDUCED_TX_SET_INTER.with(std::cell::Cell::get), )?; + // `set_txfm_ctxs` (libaom `decodeframe.c:1078`, at the end of + // `decode_token_recon_block`): an INTRA block records its TRANSFORM size in + // both `TXFM_CONTEXT` bands -- the above row over the block's width, the + // left column over its height. This arm wrote neither before lane-intrasplit + // r1, so every later block in the frame read a stale `tx_size`/`txfm_split` + // context across the strip's footprint. + for i in 0..bw / MI { + if let Some(cell) = neighbours.above_txfm.get_mut(mi_c + i) { + *cell = tx_w as u8; + } + } + for i in 0..bh / MI { + if let Some(cell) = neighbours.left_txfm.get_mut(mi_r + i) { + *cell = tx_h as u8; + } + } let class = match bw.max(bh) { 64 => 0, 32 => 1, @@ -11068,6 +11098,38 @@ fn tx_size_context(n: &Neighbours, (mi_r, mi_c): (usize, usize), own_side: usize /// a neighbour's var-tx tree splits. [`tx_size_context`]'s deblock-grid /// approximation coincides with this only while every neighbour codes one /// whole-block transform, which is exactly what `TxMode::Select` ends. +/// [`tx_size_context_txfm`] with the block's own width and height (lane-intrasplit +/// r1): `get_tx_size_context` compares the neighbour ABOVE against +/// `tx_size_wide[max_txsize_rect_lookup[bsize]]` and the neighbour to the LEFT +/// against `tx_size_high[...]` (libaom `pred_common.h:342`), which for a 2:1 +/// strip are two different numbers. The intra strip inside an inter frame read +/// this through [`tx_size_context_rect`] -- the key frame's deblock-grid +/// approximation -- so once `TxMode::Select` made the depth symbol exist, it was +/// read off the wrong CDF row whenever a neighbour's var-tx tree had split. +fn tx_size_context_txfm_rect( + n: &Neighbours, + (mi_r, mi_c): (usize, usize), + own_w: usize, + own_h: usize, +) -> usize { + let has_above = mi_r > n.tile_row0_mi; + let has_left = mi_c > n.tile_col0_mi; + let mut above = usize::from(n.above_txfm[mi_c]) >= own_w; + let mut left = usize::from(n.left_txfm[mi_r]) >= own_h; + if has_above && n.above_inter[mi_c / (SUB / MI)] { + above = n.above_side_mi[mi_c] >= own_w; + } + if has_left && n.left_inter[mi_r / (SUB / MI)] { + left = n.left_side_mi[mi_r] >= own_h; + } + match (has_above, has_left) { + (true, true) => usize::from(above) + usize::from(left), + (true, false) => usize::from(above), + (false, true) => usize::from(left), + (false, false) => 0, + } +} + fn tx_size_context_txfm(n: &Neighbours, (mi_r, mi_c): (usize, usize), side: usize) -> usize { let has_above = mi_r > n.tile_row0_mi; let has_left = mi_c > n.tile_col0_mi; diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 9581323a..b9a19b2b 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -74,7 +74,6 @@ const REFUSALS: &[&str] = &[ // what is left is the 1:4 shape, whose `bsize_to_tx_size_cat` breaks the // size-group/category diagonal the 2:1 shapes share. "an intra-coded 1:4 (or other non-2:1) rect strip on the inter block path", - "a split (nonzero tx_depth) transform on an intra HORZ/VERT strip in an inter frame", // The same arm's screen-content gate: palette/intrabc syntax is consumed // for square blocks only, so a strip in such a frame would skip symbols. "a HORZ/VERT intra strip in a screen-content frame (palette syntax is consumed for square blocks only)", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index eab98345..8be8d9c0 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -4975,6 +4975,7 @@ mod tests { ); let stream = out.stdout; let before = decode::intra_in_inter_split_tx_hits(); + let strip_before = decode::intra_rect_in_inter_split_tx_hits(); let frames = match decode_stream(&stream) { Ok(frames) => frames, Err(e) => { @@ -4988,6 +4989,14 @@ mod tests { } }; let after = decode::intra_in_inter_split_tx_hits(); + // lane-intrasplit r1: this gate is what the split intra-STRIP + // refusal was protecting, so it prints that counter too. + let strip_after = decode::intra_rect_in_inter_split_tx_hits(); + eprintln!( + "{name} seed={seed}: split intra STRIPS depth1={} depth2={}", + strip_after[1] - strip_before[1], + strip_after[2] - strip_before[2] + ); let mut per_size = [0usize; 4]; for i in 0..4 { per_size[i] = after[i] - before[i]; @@ -5061,6 +5070,260 @@ mod tests { ); } + /// lane-intrasplit r1's decisive gate: a real aomenc INTER sequence in + /// which aomenc's RD picks an intra-coded HORZ/VERT *strip* (a 2:1 + /// rectangular partition) inside an inter frame AND splits its luma + /// transform (`tx_depth >= 1`). Until this round that block was the named + /// refusal "a split (nonzero tx_depth) transform on an intra HORZ/VERT + /// strip in an inter frame"; it now walks the key frame's own per-TU path + /// ([`decode::decode_rect_split`]). Hard-asserts + /// [`decode::intra_rect_in_inter_split_tx_hits`] per depth: an attempt + /// that never produced such a block proves nothing, and exhausting every + /// attempt is a FAILURE, not a SKIP. + #[test] + fn a_real_aomenc_inter_sequence_with_a_split_transform_intra_strip_decodes_pixel_exact() { + intra_rect_in_inter_split_tx_gate(8); + } + + /// The 10-bit arm (both of the user's films are `yuv420p10le`). + #[test] + fn a_real_aomenc_10bit_inter_sequence_with_a_split_transform_intra_strip_decodes_pixel_exact() { + intra_rect_in_inter_split_tx_gate(10); + } + + fn intra_rect_in_inter_split_tx_gate(bit_depth: u32) { + let name = format!("intra_rect_in_inter_split_tx_gate({bit_depth})"); + 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, frame_count) = (192usize, 128usize, 8usize); + let mut refusals = Vec::new(); + let mut uncounted_exact = 0u32; + let mut counted_exact = 0u32; + let mut seen = [0usize; 3]; + for attempt in 0..40u32 { + let seed = 42 + attempt; + // MEASURED (r1, 40 attempts): the same sweep the merged + // `intra_rect_in_inter_gate` had to make -- at `--cpu-used=0` + // aomenc emits AB partitions regardless of + // `--enable-ab-partitions=0` (ledger), so the preset is swept + // 1..4, and the quantiser with it (a hard-cut overlay source at + // cpu-used=0 stopped 38/40 attempts on other lanes' refusals). + let cpu = attempt % 5; + let cq = [30u32, 20, 12, 63][((attempt / 5) % 4) as usize]; + // A FAST ZOOM: consecutive frames share almost no content, so + // aomenc's RD codes blocks INTRA inside INTER frames + // (`--kf-min-dist` forbids a new key frame) -- the only way a + // real encoder reaches this decoder's intra-in-inter arm -- and + // the fractal's fine detail is what then splits their transforms. + let source = format!( + "mandelbrot=size={width}x{height}:start_scale={}:end_scale=0.004:end_pts=8:rate=25", + 5.0 - f64::from(attempt) * 0.06 + ); + let y4m = Command::new("ffmpeg") + .args([ + "-v", + "error", + "-f", + "lavfi", + "-i", + &source, + // Desaturated chroma keeps `uv_mode` on DC_PRED, so the + // still-refused directional-chroma gaps never fire and + // luma detail alone drives the transform split. + "-vf", + "hue=s=0", + "-pix_fmt", + if bit_depth == 10 { "yuv420p10le" } else { "yuv420p" }, + "-strict", + "-1", + "-t", + "0.32", + "-f", + "yuv4mpegpipe", + "-strict", + "-1", + "-", + ]) + .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) + ); + let depth_args: &[&str] = if bit_depth == 10 { + &["-b", "10", "--input-bit-depth=10"] + } else { + &[] + }; + let cq_arg = format!("--cq-level={cq}"); + let cpu_arg = format!("--cpu-used={cpu}"); + let mut child = Command::new(aomenc_path()) + .args(depth_args) + .args([ + "--codec=av1", + "--passes=1", + "--end-usage=q", + &cq_arg, + &cpu_arg, + "--lag-in-frames=0", + "--auto-alt-ref=0", + "--kf-max-dist=9999", + "--kf-min-dist=9999", + "--threads=1", + "--row-mt=0", + "--enable-order-hint=0", + "--enable-warped-motion=0", + "--enable-obmc=0", + "--enable-masked-comp=0", + "--enable-interintra-comp=0", + "--enable-dist-wtd-comp=0", + "--enable-diff-wtd-comp=0", + "--enable-onesided-comp=0", + "--enable-interintra-wedge=0", + "--enable-smooth-interintra=0", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--enable-filter-intra=0", + "--enable-smooth-intra=0", + "--enable-paeth-intra=0", + "--enable-directional-intra=0", + "--enable-angle-delta=0", + "--enable-cdef=0", + "--enable-restoration=0", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-cfl-intra=0", + "--enable-ref-frame-mvs=0", + "--min-partition-size=32", + "--max-partition-size=32", + // Per-arm overrides go LAST (aomenc keeps the last + // occurrence of a repeated --enable-* flag): this gate + // needs the rect partitions the strip lives in and the + // tx_depth symbol, which exists only under TX_MODE_SELECT. + "--enable-rect-partitions=1", + "--enable-tx-size-search=1", + "--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: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + let before = decode::intra_rect_in_inter_split_tx_hits(); + let frames = match decode_stream(&stream) { + Ok(frames) => frames, + Err(e) => { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{name} decode failed outright (seed {seed} cq {cq}): {msg}" + ); + refusals.push(format!("seed {seed} cq {cq} cpu {cpu}: {msg}")); + continue; + } + }; + let after = decode::intra_rect_in_inter_split_tx_hits(); + let mut per_depth = [0usize; 3]; + for i in 0..3 { + per_depth[i] = after[i] - before[i]; + } + // class [[gate-skips-on-its-own-failure]]: a decode that + // succeeded is ALWAYS pixel-compared below; the counter only + // decides whether the attempt counts as firing. + let counted = per_depth[1] + per_depth[2] != 0; + if !counted { + uncounted_exact += 1; + refusals.push(format!( + "seed {seed} cq {cq} cpu {cpu}: decoded and pixel-compared, but no intra rect strip \ + in an inter frame split its transform" + )); + } + let ffmpeg_frames = if bit_depth == 10 { + ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) + } else { + ffmpeg_decode_sequence(&stream, width, height, frame_count) + }; + assert_eq!(frames.len(), frame_count); + eprintln!( + "{name} seed={seed} cq={cq} cpu={cpu}: split intra strips depth1={} depth2={}", + per_depth[1], per_depth[2] + ); + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + for (plane, (g, w)) in [ + ("Y", (&got.y, &want.y)), + ("U", (&got.u, &want.u)), + ("V", (&got.v, &want.v)), + ] { + let stride = if plane == "Y" { width } else { width / 2 }; + if let Some(at) = g.iter().zip(w.iter()).position(|(a, b)| a != b) { + eprintln!( + "{name} seed={seed}: frame {i} {plane} first diff at ({}, {}) got {} \ + want {} ({} samples differ)", + at % stride, + at / stride, + g[at], + w[at], + g.iter().zip(w.iter()).filter(|(a, b)| a != b).count(), + ); + } + } + } + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (seed {seed} cq {cq})"); + assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (seed {seed} cq {cq})"); + assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (seed {seed} cq {cq})"); + } + if counted { + counted_exact += 1; + seen[1] += per_depth[1]; + seen[2] += per_depth[2]; + // Keep sweeping until BOTH reachable depths have been seen in + // a pixel-exact attempt; one firing attempt is the floor. + if seen[1] != 0 && seen[2] != 0 { + break; + } + } + } + gate_buckets( + &name, + counted_exact, + uncounted_exact, + refusals.len() - uncounted_exact as usize, + ); + eprintln!("{name}: split strips seen depth1={} depth2={}", seen[1], seen[2]); + assert!( + seen[1] + seen[2] != 0, + "{name} never observed a split-transform intra strip in an inter frame:\n{}", + refusals.join("\n") + ); + } + /// lane-fiinter r1 (class [[symbol-consumption-gap]] + /// [[tool-disabled-in-every-gate]]): a real aomenc inter sequence with /// `--enable-filter-intra=1` (aomenc's own default -- every inter gate diff --git a/lanes/intrasplit-r1.report.md b/lanes/intrasplit-r1.report.md new file mode 100644 index 00000000..3886e80c --- /dev/null +++ b/lanes/intrasplit-r1.report.md @@ -0,0 +1,69 @@ +# lane-intrasplit r1 — RED (refusal lifted, two root causes fixed, gate never fired) + +Base: 18bf7dc. Branch: lane-intrasplit. + +## What changed +- `crates/ec-av1/src/decode.rs:6225` — the refusal "a split (nonzero tx_depth) transform on + an intra HORZ/VERT strip in an inter frame" is gone; a nonzero depth now walks the key + frame's own per-TU path (`depth_to_tx_wh` + `decode_rect_split`, already wired) and bumps + the new per-depth counter. +- `crates/ec-av1/src/decode.rs` (`tx_size_context_txfm_rect`, new, above + `tx_size_context_txfm`) — ROOT CAUSE 1: the arm read its `tx_depth` symbol through + `tx_size_context_rect`, the KEY FRAME's deblock-grid approximation. libaom + `get_tx_size_context` (pred_common.h:342, called from `read_selected_tx_size`, + decodemv.c) reads the two `TXFM_CONTEXT` bands and, for an INTER neighbour, that + neighbour's BLOCK width/height instead. Class [[cdf-row-held-constant]]: wrong CDF row, + same value most of the time, different range. +- `crates/ec-av1/src/decode.rs` (tail of `decode_intra_rect_in_inter`) — ROOT CAUSE 2: + the strip never wrote its own `TXFM_CONTEXT` bands (`set_txfm_ctxs`, + decodeframe.c end of `decode_token_recon_block`), because the call site + (decode.rs:18210) returns early. Every later block in the frame read a stale + `tx_size`/`txfm_split` context across the strip's footprint. +- `crates/ec-av1/src/refusal_inventory.rs` — inventory line dropped. +- `crates/ec-av1/src/stream.rs` — new gate + `a_real_aomenc_{,10bit_}inter_sequence_with_a_split_transform_intra_strip_decodes_pixel_exact` + (192x128, 8 frames, real aomenc, `--enable-rect-partitions=1 --enable-tx-size-search=1`, + `--kf-max-dist/-min-dist=9999 --lag-in-frames=0`, cpu-used and cq (incl. 63) swept, + each flag spelled once with the rect/tx overrides LAST) hard-asserting + `decode::intra_rect_in_inter_split_tx_hits()` per depth. + +## Gate result: RED (never fired) +`cargo test -p ec-av1 --lib a_real_aomenc_inter_sequence_with_a_split_transform_intra_strip` +40/40 attempts stop at OTHER lanes' named refusals; 0 attempts decoded whole, so the lift is +UNGATED and this branch MUST NOT merge as-is. + +EVIDENCE: $HOME/.cache/intrasplit-suite-r1.log | 40-attempt sweep, min/max-partition 32/32, +cpu 0..4, cq 30/20/12/63, zoom-mandelbrot source | refusal histogram: +intra 1:4 strip 9, non-skip inter rect strip 7, inter 16x16 AB/1:4 7, inter SB AB 4, +1:4 inter strip split w/ 64 axis 3, inter sub-8 3, split intra strip w/ 64x32 unit 2, +32x64 unit 2, Golomb tail 2, 8x8 non-DC chroma 1. +Six recipe families were swept (192x128 and 96x96; min/max partition 16/32, 16/64, 32/64, +32/32; hard-cut overlay and fast-zoom sources; sb-size 64): every stream that makes aomenc +pick an intra 2:1 strip with a split transform also carries one of the refusals above. +Same shape as the recorded lane-intra14 r1 and lane-r14 r3 gate hunts. + +## Film probes (charter) +EVIDENCE: scratchpad/intrasplit/{reelb1200,reelc1800}.obu | `ffmpeg -ss 1200/-ss 1800 -t 2 -c:v copy -f obu` +then `cargo run -p ec-av1 --example decode_probe` | new stops: +- ReelB -ss 1200: `a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular + residual coding` (was the split-intra-strip refusal per census4) — 0 frames in + EC_AV1_FINAL_DUMP. +- ReelC -ss 1800: `an intra-coded 1:4 (or other non-2:1) rect strip on the inter block path` + (lane-intra14's surface) — 0 frames in EC_AV1_FINAL_DUMP. No 128-superblock refusal was + reached on this segment. + +## Suite +`cargo test -p ec-av1 --lib` (systemd unit, MemoryMax=10G) — see +`$HOME/.cache/intrasplit-suite-r1.log`; totals in the handoff line below. +The two new gate tests FAIL by construction (never fired). One further failure, +`a_real_aomenc_stream_with_film_grain_decodes_pixel_exact`, is being triaged against the +base commit (`$HOME/.cache/intrasplit-base-filmgrain.log`). + +## Residue +- fix-now (next round): make the gate fire. The only untried levers are a content family + that yields 32x16/16x32 intra strips with NO 1:4 or AB partition anywhere in the frame, + or waiting on lane-intra14 (1:4 intra strips) and lane-r14 (inter rect leaves) — both + would remove the two dominant blockers. +- deferred(lane-r14/lane-intra14): the two 64-axis strip refusals ("split intra strip whose + transform unit is 64x32/32x64") are this arm's neighbours but need rectangular 64-axis + luma coefficient tables.