diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 531e1066..f8d03dbd 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -181,6 +181,16 @@ thread_local! { static INTRA_IN_INTER8_UV_CFL_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } +thread_local! { + static INTRA_IN_INTER8_ANGLE_DELTA_Y_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Nonzero luma angle deltas coded by an intra 8x8 leaf inside an inter frame +/// (lane-leaf8tx r1) -- counted only in [`decode_inter_block8`]'s intra arm. +pub(crate) fn intra_in_inter8_angle_delta_y_hits() -> usize { + INTRA_IN_INTER8_ANGLE_DELTA_Y_HITS.with(|c| c.get()) +} + /// `(directional, smooth/paeth, CfL)` chroma modes coded by an intra 8x8 leaf /// inside an inter frame -- see [`INTRA_IN_INTER8_UV_DIR_HITS`]. pub(crate) fn intra_in_inter8_uv_hits() -> (usize, usize, usize) { @@ -21819,18 +21829,16 @@ fn decode_inter_block8( }; if angle_delta_y != 0 { INTRA_IN_INTER_ANGLE_DELTA_Y_HITS.with(|c| c.set(c.get() + 1)); - // lane-angleinter r1: the >=16x16 arm's lift is gated by a real - // aomenc stream; this leaf's is NOT -- 80 attempts across two - // sizes and five quantisers never produced an 8x8 intra leaf with - // a nonzero delta (the leaf's own sibling refusals -- non-DC - // chroma, the TX_4X4 split, sub-16 rect inter leaves -- fire - // first). The prediction below would handle it; refusing keeps - // the rule that no lift ships ungated. Note the claim is about - // THIS DECODER's gate coverage, not about what an encoder writes. - return Err(unsupported( - "a nonzero angle delta on an 8x8 intra leaf in an inter frame (no gate reaches \ - this leaf with one; the >=16x16 arm decodes deltas)", - )); + // lane-leaf8tx r1: `av1_use_angle_delta(BLOCK_8X8)` is true + // (`bsize >= BLOCK_8X8`), so this leaf codes `angle_delta_y` off + // `angle_delta_cdf[mode - V_PRED]` exactly as a key-frame block + // does (libaom `read_intra_angle_info` inside + // `read_intra_block_mode_info`, decodemv.c), and the delta feeds + // the directional predictor + the edge filter/upsample decision + // through the same `reconstruct` argument below. Counted + // separately from the >=16x16 arm so the gate cannot be satisfied + // by a bigger block. + INTRA_IN_INTER8_ANGLE_DELTA_Y_HITS.with(|c| c.set(c.get() + 1)); } // lane-uv8 r1: the delta'd angle decides the intra edge filter, and a // directional luma mode is already legal on this leaf -- the arm @@ -21974,11 +21982,15 @@ fn decode_inter_block8( } } - // lane-intrainter r1: the 8x8 leaf's own intra `tx_depth` can only - // resolve to TX_4X4, whose 2x2 unit grid this leaf path has no - // prediction loop for (the >=16x16 square path above does). Refuse by - // name rather than mis-reconstruct. - if read_block_tx_size( + // lane-leaf8tx r1: the 8x8 leaf's own intra `tx_depth` (libaom + // `read_tx_size` -> `read_selected_tx_size`, max depth 1 at + // BLOCK_8X8) resolves to TX_4X4, and `decode_block` + // (`decodeframe.c`) then codes one intra prediction + one + // coefficient set PER 4x4 transform unit in raster order, each unit + // predicting off the units reconstructed before it -- the same loop + // the >=16x16 intra-in-inter arm runs. Chroma stays one 4x4 unit + // (`av1_get_max_uv_txsize`). + let intra_leaves = read_block_tx_size( dec, cdfs, neighbours, @@ -21988,28 +22000,81 @@ fn decode_inter_block8( false, skip, )? - .1 - .is_some() - { - return Err(unsupported( - "an 8x8 intra leaf in an inter frame whose tx_depth splits it into 4x4 \ - transform units", - )); - } + .1; + split8 = intra_leaves.is_some(); let reach = Reach::of(SIDE, px, py, y.width, y.height); + if let Some(leaves) = intra_leaves { + for &(row, col, tx_px, _) in &leaves { + let tu_mi = (leaf_mi.0 + row, leaf_mi.1 + col); + let (tu_px, tu_py) = (px + col * MI, py + row * MI); + // A transform unit inside a block is not a block at that + // position: top-right / bottom-left availability is the + // block's, narrowed by the unit's offset inside it. + let tu_reach = Reach::of_tu(SIDE, SIDE, col * MI, row * MI, tx_px, tx_px, reach); + let tu_grid = if skip { + y.reconstruct( + tu_px, + tu_py, + tx_px, + mode, + angle_delta_y, + tu_reach, + &vec![0i32; tx_px * tx_px], + None, + filter_intra, + smooth_neighbor, + ); + vec![0i32; tx_px * tx_px] + } else { + // The unit is smaller than the block it sits in, so + // `txb_skip_ctx` is the neighbour-magnitude table, not + // the lone-TU 0 (`get_txb_ctx_general`). + let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); + read_plane( + dec, + cdfs, + // Intra 4x4 luma: seven-type + // `EXT_TX_SET_DTT4_IDTX_1DDCT` unless reduced, whose + // CDF row is the intra dir (`fi_tx_row` inside + // `read_plane` maps a filter-intra block's). + txbset_for(tx_px, reduced_tx_set), + &default_scan(tx_px), + 0, + neighbours.around_mi(tu_mi, tx_px)[0], + mode, + mode, + angle_delta_y, + tu_reach, + y, + tu_px, + tu_py, + tx_px, + tx_px, + base_q_idx, + None, + filter_intra, + Some(tu_skip_ctx), + smooth_neighbor, + )? + }; + neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid); + } + } if skip { - y.reconstruct( - px, - py, - SIDE, - mode, - angle_delta_y, - reach, - &vec![0i32; SIDE * SIDE], - None, - filter_intra, - smooth_neighbor, - ); + if !split8 { + y.reconstruct( + px, + py, + SIDE, + mode, + angle_delta_y, + reach, + &vec![0i32; SIDE * SIDE], + None, + filter_intra, + smooth_neighbor, + ); + } // CfL's AC contribution is the luma this leaf just reconstructed, // averaged over the 4x4 chroma block (`cfl_ac_q3`, spec 7.11.5). let ac = alpha.map(|_| cfl_ac_q3(y, px, py, SIDE)); @@ -22042,7 +22107,13 @@ fn decode_inter_block8( v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; } else { let around = neighbours.around_mi(leaf_mi, 8); - luma_grid = read_plane( + luma_grid = if split8 { + // Already coded and reconstructed unit by unit above; the + // block-level grid only feeds the neighbour write-back, + // which the `saved_luma_ctx` restore below keeps per-TU. + vec![0i32; SIDE * SIDE] + } else { + read_plane( dec, cdfs, // lane-uv8 r3: an intra leaf inside an inter frame is @@ -22071,7 +22142,8 @@ fn decode_inter_block8( filter_intra, None, smooth_neighbor, - )?; + )? + }; let ac = alpha.map(|_| cfl_ac_q3(y, px, py, SIDE)); u_grid = read_plane( dec, diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index cf7376e1..70692fe8 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -29,7 +29,6 @@ const CAPABILITY_CLAIMS: &[&str] = &[ #[cfg(test)] const REFUSALS: &[&str] = &[ - "a nonzero angle delta on an 8x8 intra leaf in an inter frame (no gate reaches this leaf with one; the >=16x16 arm decodes deltas)", "a coded HORZ/VERT strip whose chroma transform has no rect coefficient tables here", "a split intra strip whose transform unit is {tx_w}x{tx_h} (no luma coefficient tables for that shape here)", "an OBMC neighbour whose interp filter was never recorded (no switchable symbol for that block)", @@ -69,7 +68,6 @@ const REFUSALS: &[&str] = &[ // prediction + coefficients, gate // `a_real_aomenc_inter_sequence_with_a_split_transform_intra_block_decodes_pixel_exact`); // the 8x8 leaf's TX_4X4 2x2 grid is still refused here. - "an 8x8 intra leaf in an inter frame whose tx_depth splits it into 4x4 transform units", "an inter frame with no key frame before it", "an inter SB-level AB partition (HORZ_A/HORZ_B/VERT_A/VERT_B; this decoder's inter tile path codes a superblock as NONE, SPLIT, HORZ, VERT, HORZ_4 or VERT_4)", // lane-inter16ab r1 lifted the AB half (HORZ_A/HORZ_B/VERT_A/VERT_B at diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index e719d3c9..db01453e 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -5962,6 +5962,247 @@ mod tests { } + /// lane-leaf8tx r1: the two remaining refusals on an INTRA 8x8 leaf + /// inside an INTER frame -- its `tx_depth` splitting the luma transform + /// into four TX_4X4 units (`--enable-tx-size-search=1`), and a nonzero + /// luma `angle_delta_y` on it (`--enable-angle-delta=1`, + /// `av1_use_angle_delta(BLOCK_8X8)`). Both counters are per-attempt + /// deltas on an attempt that decoded to the end and is pixel-compared + /// (class [[counter-from-refused-stream]]); a decode error or a mismatch + /// is a FAILURE, never a SKIP. + #[test] + fn a_real_aomenc_inter_sequence_with_a_split_tx_8x8_intra_leaf_decodes_pixel_exact() { + tx_split_angle_in_inter8_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_tx_8x8_intra_leaf_decodes_pixel_exact() { + tx_split_angle_in_inter8_gate(10); + } + + fn tx_split_angle_in_inter8_gate(bit_depth: u32) { + let name = format!("tx_split_angle_in_inter8_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, 6usize); + let mut refusals = Vec::new(); + let mut uncounted_exact = 0u32; + let mut totals = (0usize, 0usize); + for attempt in 0..30u32 { + let seed = 42 + attempt; + // Same zoom + hard-cut fractal source the sibling uv_mode gate + // uses (a cut is what makes aomenc code INTRA blocks inside an + // inter frame); `noise` carries an explicit seed (class + // [[seeded-fixture-not-reproducible]]). + let source = format!( + "mandelbrot=size={width}x{height}:start_scale={}:rate=25[a];\ + mandelbrot=size={width}x{height}:start_scale={}:rate=25[b];\ + [a][b]overlay=enable='gte(n,3)',noise=alls=12:allf=t+u:all_seed={seed}", + 5.0 - f64::from(attempt) * 0.06, + 0.9 + f64::from(attempt) * 0.11 + ); + let y4m = Command::new("ffmpeg") + .args([ + "-v", + "error", + "-f", + "lavfi", + "-i", + &source, + "-pix_fmt", + if bit_depth == 10 { "yuv420p10le" } else { "yuv420p" }, + "-strict", + "-1", + "-t", + "0.24", + "-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 cq = format!("--cq-level={}", 12 + (attempt % 5) * 7); + let depth_args: &[&str] = if bit_depth == 10 { + &["-b", "10", "--input-bit-depth=10"] + } else { + &[] + }; + // The y4m goes through a PID-keyed FILE, never aomenc's stdin + // (pipe deadlock, class [[gate-loader-slurps-whole-file]]). + let y4m_path = std::env::temp_dir().join(format!( + "ec-av1-leaf8tx-{}-{bit_depth}-{seed}.y4m", + std::process::id() + )); + std::fs::write(&y4m_path, &y4m.stdout).expect("writing the y4m fixture"); + let child = Command::new(aomenc_path()) + .args(depth_args) + .args([ + "--codec=av1", + "--passes=1", + "--end-usage=q", + &cq, + "--cpu-used=0", + "--lag-in-frames=0", + "--auto-alt-ref=0", + "--kf-max-dist=9999", + "--kf-min-dist=9999", + "--threads=1", + "--row-mt=0", + "--tile-columns=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-cdef=0", + "--enable-restoration=0", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-ref-frame-mvs=0", + // Per-arm overrides LAST (aomenc keeps the last + // occurrence of a repeated --enable-* flag). Rect + // partitions stay off so the leaf's unrelated sub-16 rect + // inter refusal never fires. + "--enable-rect-partitions=0", + "--enable-tx-size-search=1", + "--enable-angle-delta=1", + "--enable-directional-intra=1", + "--min-partition-size=8", + "--max-partition-size=32", + "--obu", + "-o", + "-", + ]) + .arg(&y4m_path) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + let out = child.wait_with_output().expect("aomenc failed to run"); + let _ = std::fs::remove_file(&y4m_path); + assert!( + out.status.success(), + "aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + let before = ( + decode::intra_in_inter_split_tx_hits()[0], + decode::intra_in_inter8_angle_delta_y_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}): {msg}" + ); + refusals.push(format!("seed {seed}: {msg}")); + continue; + } + }; + let now = ( + decode::intra_in_inter_split_tx_hits()[0], + decode::intra_in_inter8_angle_delta_y_hits(), + ); + let fired = (now.0 - before.0, now.1 - before.1); + totals = (totals.0 + fired.0, totals.1 + fired.1); + // Both lifted capabilities must fire on the SAME pixel-compared + // attempt. + let counted = fired.0 > 0 && fired.1 > 0; + if !counted { + uncounted_exact += 1; + refusals.push(format!( + "seed {seed}: decoded and pixel-compared, but split-tx {} / angle-delta {} \ + 8x8 intra-in-inter leaves", + fired.0, fired.1 + )); + } + 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}: 8x8 intra-in-inter leaves with tx_depth=1 {}, with a \ + nonzero angle_delta_y {}", + fired.0, fired.1 + ); + 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})"); + assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (seed {seed})"); + assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (seed {seed})"); + } + if counted { + gate_buckets( + &name, + 1, + uncounted_exact, + refusals.len() - uncounted_exact as usize, + ); + return; + } + } + gate_buckets( + &name, + 0, + uncounted_exact, + refusals.len() - uncounted_exact as usize, + ); + panic!( + "{name} never observed BOTH a tx_depth split and a nonzero angle delta on an 8x8 \ + intra leaf in an inter frame (totals split/angle {totals:?}):\n{}", + refusals.join("\n") + ); + } + /// lane-chroma r2 stage 1: a real aomenc key frame with /// `--enable-smooth-intra=1` (opposite of every sibling recipe's `=0`) /// and `--enable-paeth-intra=0` -- paeth chroma stays off so the still-