diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 05b792ec..23a2eb0f 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -1246,6 +1246,47 @@ pub const INTERINTRA: [[u16; 3]; 4] = [ [30237, 32768, 0], ]; +/// `default_interintra_mode_cdf` (spec 9.4): `interintra_mode` (II_DC/II_V/ +/// II_H/II_SMOOTH), indexed by `size_group_lookup[bsize]`, read right after +/// `interintra == 1` (lane-interintra r1). +pub const INTERINTRA_MODE: [[u16; 5]; 4] = [ + [8192, 16384, 24576, 32768, 0], + [1875, 11082, 27332, 32768, 0], + [2473, 9996, 26388, 32768, 0], + [4238, 11537, 25926, 32768, 0], +]; + +/// `default_wedge_interintra_cdf` (spec 9.4): `wedge_interintra`, indexed by +/// bsize over `BLOCK_SIZES_ALL` -- read for every interintra block whose +/// size `av1_is_wedge_used` allows (the 8x8..32x32 family), REGARDLESS of +/// the sequence header's `enable_interintra_wedge` bit (libaom +/// decodemv.c:1548 gates on block size alone; the seq bit constrains only +/// the encoder). +pub const WEDGE_INTERINTRA: [[u16; 3]; 22] = [ + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [20036, 32768, 0], + [24957, 32768, 0], + [26704, 32768, 0], + [27530, 32768, 0], + [29564, 32768, 0], + [29444, 32768, 0], + [26872, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], + [16384, 32768, 0], +]; + /// `Default_Comp_Mode_Cdf` (spec 9.4, `default_comp_inter_cdf`): whether an /// inter block reads `SINGLE_REFERENCE` or `COMPOUND_REFERENCE` -- only /// reached once `read_ref_frames` knows the frame's `reference_select` diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index d6e3d400..cae0dca9 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -336,6 +336,12 @@ pub(crate) struct Cdfs { /// `interintra`, indexed by `size_group_lookup[bsize]` -- see /// `cdf::INTERINTRA`'s own doc. pub interintra: [[u16; 3]; 4], + /// `interintra_mode` (II_DC/II_V/II_H/II_SMOOTH), same size-group + /// indexing -- see `cdf::INTERINTRA_MODE` (lane-interintra r1). + pub interintra_mode: [[u16; 5]; 4], + /// `wedge_interintra`, indexed by bsize over `BLOCK_SIZES_ALL` -- see + /// `cdf::WEDGE_INTERINTRA` (lane-interintra r1). + pub wedge_interintra: [[u16; 3]; 22], /// Which of the eight `INTER_COMPOUND_MODES` a `COMPOUND_REFERENCE` /// block takes (lane-av1comp, spec 5.11.24's `compound_mode`). pub inter_compound_mode: [[u16; 9]; 8], @@ -568,7 +574,16 @@ impl Cdfs { reset2(&mut self.skip_mode); reset2(&mut self.obmc); reset2(&mut self.motion_mode); + // lane-interintra r1 (ii-flake-9): the ONE table this list missed -- + // an unreset counter saturates past 15 reads and slows update_cdf's + // rate to >>5 while libaom (reset per frame) stays at >>4; the rows + // drift a few counts apart and the gate flakes only on streams with + // enough switchable-filter reads. Audit: every other Cdfs field was + // covered (script-checked against the struct's field list). + reset2(&mut self.switchable_interp); reset2(&mut self.interintra); + reset2(&mut self.interintra_mode); + reset2(&mut self.wedge_interintra); reset2(&mut self.inter_compound_mode); reset2(&mut self.comp_ref_type); reset3(&mut self.uni_comp_ref); @@ -1012,6 +1027,8 @@ impl Cdfs { obmc: cdf::OBMC, motion_mode: cdf::MOTION_MODE, interintra: cdf::INTERINTRA, + interintra_mode: cdf::INTERINTRA_MODE, + wedge_interintra: cdf::WEDGE_INTERINTRA, comp_group_idx: cdf::COMP_GROUP_IDX, compound_idx: cdf::COMPOUND_IDX, y_mode: cdf::Y_MODE, diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index ea3d3e7a..924b6363 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -190,6 +190,16 @@ pub(crate) fn warp_selected_hits() -> usize { WARP_SELECTED_HITS.load(std::sync::atomic::Ordering::Relaxed) } +/// How many blocks decoded `interintra == 1` with the non-wedge blended +/// prediction applied (lane-interintra r1) -- the gate's proof that a +/// stream actually exercised interintra. +static INTERINTRA_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// Current value of [`INTERINTRA_HITS`]. +pub(crate) fn interintra_hits() -> usize { + INTERINTRA_HITS.load(std::sync::atomic::Ordering::Relaxed) +} + /// Current value of [`OBMC_HITS_8`]. pub(crate) fn obmc_hits_8() -> usize { OBMC_HITS_8.load(std::sync::atomic::Ordering::Relaxed) @@ -3770,9 +3780,21 @@ fn resolve_interp_filter( // `h_kind`/`v_kind` argument order, which is why this function returns // `(h, v, ..)` but reads `dir0` (`v`) before `dir1` (`h`). let ctx0 = switchable_interp_ctx(above[0], left[0], 0, is_compound); + if std::env::var_os("EC_AV1_IFDBG").is_some() { + eprintln!( + "IFDBG dir=0 ctx={ctx0} above={above:?} left={left:?} cdf={:?}", + &cdfs.switchable_interp[ctx0][..3] + ); + } let sym0 = dec.symbol(&mut cdfs.switchable_interp[ctx0]) as u8; let sym1 = if enable_dual_filter { let ctx1 = switchable_interp_ctx(above[1], left[1], 1, is_compound); + if std::env::var_os("EC_AV1_IFDBG").is_some() { + eprintln!( + "IFDBG dir=1 ctx={ctx1} above={above:?} left={left:?} cdf={:?} sym0={sym0}", + &cdfs.switchable_interp[ctx1][..3] + ); + } dec.symbol(&mut cdfs.switchable_interp[ctx1]) as u8 } else { sym0 @@ -4378,6 +4400,73 @@ fn obmc_neighbour_pred( /// Blends `tmp` into `dst` (stride `stride`) at `(ox, oy)`, mask varying by /// row (the above-neighbour pass, `aom_blend_a64_vmask`). +/// `ii_weights1d` (reconinter.c:524): the interintra smooth-mask falloff. +const II_WEIGHTS_1D: [u8; 128] = [ + 60, 58, 56, 54, 52, 50, 48, 47, 45, 44, 42, 41, 39, 38, 37, 35, 34, 33, 32, + 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 22, 21, 20, 19, 19, 18, 18, 17, 16, + 16, 15, 15, 14, 14, 13, 13, 12, 12, 12, 11, 11, 10, 10, 10, 9, 9, 9, 8, + 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, + 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +]; + +/// lane-interintra r1: non-wedge interintra prediction for one plane +/// (reconinter.c `av1_build_interintra_predictor` + `combine_interintra` + +/// `build_smooth_interintra_mask`). Builds the intra predictor for the +/// block's II mode (DC/V/H/SMOOTH, angle 0, no filter-intra) from `plane`'s +/// own decoded edges at `(x, y)`, then blends it over the inter prediction +/// in `pred`: `comp = (mask*intra + (64-mask)*inter + 32) >> 6` +/// (`aom_blend_a64_mask`, intra as src0). `ii_size_scales[plane_bsize]` +/// reduces to `128 / side` for the square plane sizes this decoder reaches. +fn interintra_blend(plane: &PlaneBuf, x: usize, y: usize, side: usize, ii_mode: u8, pred: &mut [u8]) { + // `interintra_to_intra_mode`: II_DC/II_V/II_H/II_SMOOTH. + let intra_mode = match ii_mode { + 0 => crate::intra::DC_PRED, + 1 => crate::intra::V_PRED, + 2 => crate::intra::H_PRED, + _ => crate::intra::SMOOTH_PRED, + }; + let (above, left, corner) = plane.edges( + x, + y, + side, + crate::encode::Reach { + above_right: false, + below_left: false, + }, + ); + let mut intra = vec![0u8; side * side]; + // Edge filtering never applies here: V/H at delta 0 are plain edge + // copies (angle 90/180 skip the directional walk), DC/SMOOTH carry no + // angle at all. + predict( + intra_mode, + 0, + above.as_deref(), + left.as_deref(), + corner, + side, + false, + false, + &mut intra, + ); + let scale = 128 / side; + for i in 0..side { + for j in 0..side { + let m = u32::from(match ii_mode { + 0 => 32, + 1 => II_WEIGHTS_1D[i * scale], + 2 => II_WEIGHTS_1D[j * scale], + _ => II_WEIGHTS_1D[i.min(j) * scale], + }); + let idx = i * side + j; + pred[idx] = + ((m * u32::from(intra[idx]) + (64 - m) * u32::from(pred[idx]) + 32) >> 6) as u8; + } + } +} + fn obmc_blend_v(dst: &mut [u8], stride: usize, ox: usize, oy: usize, w: usize, h: usize, tmp: &[u8]) { let mask = obmc_mask(h); for row in 0..h { @@ -5642,14 +5731,26 @@ fn decode_inter_block( // branch produces -- NEARESTMV/NEARMV/GLOBALMV/NEWMV -- is // `SINGLE_INTER_MODE_START..END` by construction). // `size_group_lookup`: BLOCK_16X16 -> 2, BLOCK_32X32 -> 3. + let mut interintra_mode: Option = None; if enable_interintra_compound && !skip_mode && (side == 16 || side == 32) { let bsize_group = if side == 16 { 2 } else { 3 }; let interintra = dec.symbol(&mut cdfs.interintra[bsize_group]) == 1; if interintra { - return Err(unsupported( - "an interintra-compound block (interintra == 1, inter+intra \ - blended prediction unimplemented)", - )); + // lane-interintra r1 (decodemv.c 1540-1555): interintra_mode, + // then -- gated on block size ALONE, never the + // `enable_interintra_wedge` seq bit -- the wedge flag + // (`av1_is_wedge_used(bsize)` holds for 16x16/32x32). + let ii = dec.symbol(&mut cdfs.interintra_mode[bsize_group]) as u8; + let wedge_bsize = if side == 16 { 6 } else { 9 }; + let wedge = dec.symbol(&mut cdfs.wedge_interintra[wedge_bsize]) == 1; + if wedge { + return Err(unsupported( + "a wedge-interintra block (use_wedge_interintra == 1, \ + wedge-masked blending unimplemented)", + )); + } + INTERINTRA_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + interintra_mode = Some(ii); } } if std::env::var_os("EC_AV1_TELL").is_some() { @@ -5679,8 +5780,12 @@ fn decode_inter_block( // from an `OBMC_CAUSAL`-only one (2-symbol `obmc_cdf`) whenever // the header allows warp -- reading the wrong alphabet desyncs // the tile. + // An interintra block has `ref_frame[1] == INTRA_FRAME`, which + // fails libaom's `is_motion_variation_allowed_compound`: the + // motion_mode symbol is NOT read (SIMPLE_TRANSLATION implied). let motion_mode_eligible = switchable_motion_mode && !skip_mode + && interintra_mode.is_none() && (!overlappable_above(grid, mi_row, mi_col, bw4, mi_cols as usize, 1).is_empty() || !overlappable_left(grid, mi_row, mi_col, bw4, mi_rows as usize, 1) .is_empty()); @@ -5833,7 +5938,13 @@ fn decode_inter_block( MiInfo { is_inter: true, ref_frame, - ref_frame1: None, + // An interintra block records ref_frame[1] == + // INTRA_FRAME (0): warp-sample gathering + // (libaom av1_findSamples, mvref_common.c:1155) + // requires ref_frame[1] == NONE_FRAME, so such a + // neighbour must not donate samples or count in + // num_proj_ref. + ref_frame1: interintra_mode.map(|_| 0), mv1: None, mv, is_new_mv, @@ -5931,6 +6042,16 @@ fn decode_inter_block( )?; } + // Mutually exclusive with warp/OBMC (motion_mode is not read for + // an interintra block): blend the intra predictor over the MC + // result, all planes (reconinter.c av1_build_interintra_predictor + // plane loop). + if let Some(ii) = interintra_mode { + interintra_blend(y, px, py, side, ii, &mut pred_y); + interintra_blend(u, cpx, cpy, chroma_side, ii, &mut pred_u); + interintra_blend(v, cpx, cpy, chroma_side, ii, &mut pred_v); + } + if skip { y.reconstruct_mc(px, py, side, &pred_y, &vec![0i32; side * side]); u.reconstruct_mc( @@ -6723,13 +6844,23 @@ fn decode_inter_block8( // `is_interintra_allowed_bsize`-eligible, single-ref-only branch // (compound already returned above), GLOBALMV already refused // (round 3), so the only real gate left is the header enable bit. + let mut interintra_mode: Option = None; if enable_interintra_compound && !skip_mode { let interintra = dec.symbol(&mut cdfs.interintra[SIZE_GROUP_8]) == 1; if interintra { - return Err(unsupported( - "an interintra-compound block (interintra == 1, inter+intra \ - blended prediction unimplemented)", - )); + // lane-interintra r1: same read pair as the 16/32 site; + // wedge cdf row = BLOCK_8X8 (index 3), read on block size + // alone, never the `enable_interintra_wedge` seq bit. + let ii = dec.symbol(&mut cdfs.interintra_mode[SIZE_GROUP_8]) as u8; + let wedge = dec.symbol(&mut cdfs.wedge_interintra[3]) == 1; + if wedge { + return Err(unsupported( + "a wedge-interintra block (use_wedge_interintra == 1, \ + wedge-masked blending unimplemented)", + )); + } + INTERINTRA_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + interintra_mode = Some(ii); } } // lane-motionmode round 3: `read_motion_mode` (spec 5.11.24) at the @@ -6741,6 +6872,7 @@ fn decode_inter_block8( // refusal (warp needs `av1_findSamples`, not ported at either leaf). let motion_mode_eligible = switchable_motion_mode && !skip_mode + && interintra_mode.is_none() && (!overlappable_above(grid, mi_row, mi_col, 2, mi_cols as usize, 1).is_empty() || !overlappable_left(grid, mi_row, mi_col, 2, mi_rows as usize, 1).is_empty()); let mut obmc_selected = false; @@ -6785,7 +6917,9 @@ fn decode_inter_block8( MiInfo { is_inter: true, ref_frame: LAST_FRAME, - ref_frame1: None, + // INTRA_FRAME marker for interintra blocks -- keeps + // them out of warp-sample gathering (see 16/32 site). + ref_frame1: interintra_mode.map(|_| 0), mv1: None, mv, is_new_mv, @@ -6859,6 +6993,14 @@ fn decode_inter_block8( )?; } + // Non-wedge interintra blend, mutually exclusive with OBMC (the + // motion_mode symbol is not read for an interintra block). + if let Some(ii) = interintra_mode { + interintra_blend(y, px, py, SIDE, ii, &mut pred_y); + interintra_blend(u, cpx, cpy, CHROMA_SIDE, ii, &mut pred_u); + interintra_blend(v, cpx, cpy, CHROMA_SIDE, ii, &mut pred_v); + } + if skip { y.reconstruct_mc(px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE]); u.reconstruct_mc( diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 166eb962..953a9499 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -1293,6 +1293,10 @@ fn process_compound_ref_mv_candidate( Some((candidate.ref_frame, candidate.mv)), candidate .ref_frame1 + // An interintra neighbour stores INTRA_FRAME (0) here; libaom's + // ref-diff scan requires `can_rf > INTRA_FRAME` + // (process_compound_ref_mv_candidate), so it contributes nothing. + .filter(|&rf1| rf1 > 0) .map(|rf1| (rf1, candidate.mv1.unwrap_or((0, 0)))), ]; for (candidate_ref, candidate_mv) in slots.into_iter().flatten() { diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 28736399..bb8b9dfb 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -3468,12 +3468,189 @@ mod tests { ); } + /// lane-interintra r1: `--enable-interintra-comp=1` streams must DECODE + /// pixel-exact -- the interintra flag/mode/wedge-flag syntax and the + /// non-wedge smooth blend are ported. A refusal naming interintra fails + /// the gate UNLESS it is the wedge refusal (`use_wedge_interintra == 1` + /// is r2, and this recipe encodes with wedge off so it must not fire + /// either); warp/obmc are enabled and supported; refusals for OTHER + /// named capabilities (screen content tools) still skip that seed. The + /// sweep must actually exercise interintra (`interintra_hits > 0`). + #[test] + fn a_real_aomenc_stream_with_interintra_decodes_pixel_exact() { + const NAME: &str = "a_real_aomenc_stream_with_interintra_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, frame_count) = (64usize, 64usize, 24usize); + let mut named_refusals = 0u32; + let mut matched = 0u32; + let n_attempts: u32 = std::env::var("EC_INTERINTRA_GATE_ATTEMPTS") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(40); + for attempt in 0..n_attempts { + let seed = 42 + attempt; + let duration = frame_count as f64 / 25.0; + let source = + format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); + let y4m = Command::new("ffmpeg") + .args([ + "-v", + "error", + "-f", + "lavfi", + "-i", + &source, + "-pix_fmt", + "yuv420p", + "-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) + ); + let args: Vec<&str> = vec![ + "--codec=av1", + "--passes=1", + "--end-usage=q", + "--cq-level=45", + "--cpu-used=0", + "--kf-max-dist=1000", + "--threads=1", + "--row-mt=0", + "--auto-alt-ref=1", + "--lag-in-frames=16", + "--enable-fwd-kf=0", + "--enable-order-hint=1", + "--enable-warped-motion=1", + "--enable-obmc=1", + "--tune-content=default", + "--enable-masked-comp=0", + "--enable-interintra-comp=1", + "--enable-onesided-comp=0", + "--enable-interintra-wedge=0", + "--enable-smooth-interintra=1", + "--enable-rect-partitions=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-tx-size-search=0", + "--enable-cdef=0", + "--enable-restoration=0", + "--max-partition-size=32", + "--min-partition-size=32", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-cfl-intra=0", + "--enable-ref-frame-mvs=0", + "--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.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 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}" + ); + assert!( + !msg.contains("warp"), + "{NAME} refused on warp (seed {seed}) -- warp decode is ported, this \ + refusal must not exist: {msg}" + ); + assert!( + !msg.contains("interintra"), + "{NAME} refused on interintra (seed {seed}) -- non-wedge interintra \ + is ported and this recipe encodes with wedge off: {msg}" + ); + named_refusals += 1; + eprintln!("seed {seed} refusal (non-warp capability): {msg}"); + continue; + } + Ok(frames) => frames, + }; + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); + assert_eq!(frames.len(), frame_count); + let mismatched = frames + .iter() + .zip(&ffmpeg_frames) + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); + if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { + std::fs::write(&path, &stream).expect("writing pinned stream"); + eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); + } + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); + assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); + assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); + } + matched += 1; + } + assert!( + matched > 0, + "{NAME}: every attempt refused on other capabilities; the gate never exercised warp" + ); + assert!( + crate::decode::interintra_hits() > 0, + "{NAME}: {matched} matches but zero interintra blocks fired -- the gate decodes \ + these streams without ever taking interintra, so it proves nothing" + ); + eprintln!( + "{NAME}: {named_refusals} other-capability refusals, {matched} pixel-exact matches out of {n_attempts}, interintra_hits={}", + crate::decode::interintra_hits() + ); + } + + /// Pinned streams captured by `EC_AV1_GATE_DUMP` off /// `a_real_aomenc_stream_with_warped_motion_refuses_or_matches` -- each /// one is a former mismatch, kept as a regression pin (warp-mismatch: /// the WARPED_CAUSAL interp-read suppression; warp-flake-7: the same /// suppression for skip_mode blocks; warp-flake-5: mvstack entry - /// clamping). `EC_AV1_GATE_DUMP_PIN` overrides to a single stream. + /// clamping; ii-flake-1..8: interintra neighbours excluded from + /// warp-sample gathering, ref_frame[1] == INTRA_FRAME in the mi grid). + /// `EC_AV1_GATE_DUMP_PIN` overrides to a single stream. /// Deterministic and static -- `#[ignore]`d because the gitignored /// fixtures dir may be absent; run manually. #[test] @@ -3486,7 +3663,22 @@ mod tests { let fixtures = concat!(env!("CARGO_MANIFEST_DIR"), "/../../fixtures"); let paths: Vec = match std::env::var("EC_AV1_GATE_DUMP_PIN") { Ok(p) => vec![p], - Err(_) => ["warp-mismatch", "warp-flake-5", "warp-flake-7"] + Err(_) => [ + "warp-mismatch", + "warp-flake-5", + "warp-flake-7", + "ii-flake-1", + "ii-flake-2", + "ii-flake-3", + "ii-flake-5", + "ii-flake-6", + "ii-flake-7", + "ii-flake-8", + // switchable_interp missing from reset_counts (counter + // saturation slowed the adaptation rate) -- unrelated to + // interintra, caught by the same gate. + "ii-flake-9", + ] .iter() .map(|n| format!("{fixtures}/{n}.obu")) .collect(), diff --git a/lanes/interintra-r1.charter.md b/lanes/interintra-r1.charter.md new file mode 100644 index 00000000..86aae887 --- /dev/null +++ b/lanes/interintra-r1.charter.md @@ -0,0 +1,64 @@ +# lane-interintra r1 charter — implement non-wedge interintra prediction (decode) + +## Where you are +Worktree /home/user/Documents/Code/Rust/edith_codecs-interintra, branch lane-interintra @ 8f90552. +Build/test ONLY: `env CARGO_TARGET_DIR=$HOME/.cache/cargo-target-interintra cargo test -p ec-av1 --release --lib -- --nocapture` +Never plain workspace cargo test; never touch other worktrees; never push; fixtures/ is a symlink — never write into it except NEW pinned .obu streams captured by a gate. + +## Current state (do not re-derive) +- The interintra SYMBOL is consumed and interintra==1 is a NAMED refusal at + crates/ec-av1/src/decode.rs:5645-5654 (single-ref path, side 16/32, gated + enable_interintra_compound && !skip_mode). This round replaces the refusal with the port. +- Warp/OBMC/skip_mode/interp-suppression all landed and merged — leave them alone. +- Reference: libaom at /tmp/libaom-src (decodemv.c read_interintra_mode_info ~1100, + reconinter.c av1_build_interintra_predictor / combine_interintra, + reconintra.c av1_build_intra_predictors_for_interintra). Patched aomdec (EC_TRACE2 prints) + builds in /tmp/libaom-build; libaom examples/inspect exists there for ground truth + (wrap OBU in IVF first: ffmpeg -i x.obu -c copy x.ivf). ffmpeg is the pixel oracle. + +## Scope, r1 = NON-WEDGE interintra only +1. Syntax (decodemv.c read_interintra_mode_info): after interintra==1 read + interintra_mode (4-ary, interintra_mode_cdf[size_group]); if seq + enable_interintra_wedge && bsize in wedge-allowed set: read wedge_interintra flag — + if THAT reads 1, keep a NAMED refusal ("wedge interintra unimplemented") for r2; + wedge flag 0 → non-wedge path below. CDFs: interintra_mode + wedge_interintra flags + exist in libaom's default tables — add to cdf.rs/cdf_state.rs mirroring how the + interintra flag CDF was added (grep cdfs.interintra for the pattern; remember CDF + adaptation: new tables join the adapt map the same way). +2. Semantics: ref_frame[1] = INTRA_FRAME. CRITICAL cross-cutting sweep (class + tx-class-cross-cutting + neighbour-votes-all-its-fields): interintra blocks are + single-ref for mvstack purposes but their neighbour RECORD carries ref1=INTRA_FRAME + in libaom — grep every context gathered from ref1/compound-ness (comp_inter ctx, + comp_ref ctx, filter ctx ref-match) and match libaom's read of an interintra + neighbour. motion_mode is NOT read for interintra blocks (ref_frame[1]==INTRA_FRAME + fails is_motion_variation_allowed) — the current code reads motion_mode after the + refusal site; make interintra==1 skip that read exactly as libaom does. +3. Prediction (reconinter.c): build the intra predictor for the block's interintra mode + (II_DC/II_V/II_H/II_SMOOTH map to DC/V/H/SMOOTH intra preds with + use_filter_intra=0, from the block's own above/left recon like a normal intra block), + then combine_interintra: weighted blend of inter and intra preds with the + ii_weights_1d table (mode-dependent direction), all planes (chroma at subsampled + size; interintra applies to chroma too — check av1_build_interintra_predictor plane loop). +4. Gate: clone the warp gate shape (stream.rs a_real_aomenc_stream_with_warped_motion_ + refuses_or_matches, ~3311): new test a_real_aomenc_stream_with_interintra_decodes_ + pixel_exact with aomenc flags from that gate BUT --enable-interintra-comp=1 + --enable-interintra-wedge=0 --enable-smooth-interintra=1, warp/obmc ON (they are + supported now); non-wedge interintra refusals FORBIDDEN, wedge refusal allowed + (should not fire with wedge=0), EC_AV1_GATE_DUMP self-pin on mismatch, and assert the + interintra counter fired (add an INTERINTRA_HITS atomic like WARP_SELECTED_HITS). +5. Any pixel mismatch: pin the stream (cp into fixtures/), diff with + /tmp/libaom-build/examples/inspect + patched aomdec EC_TRACE2, range-ladder first + (TRACE part32_pre rng= exists under EC_AV1_TRACE; aomdec prints EC_PART rng=): + ranges exact everywhere = prediction-side; divergent = consumption. Fix, rerun. + +## Done criteria +1. New interintra gate green with interintra blocks actually fired (counter > 0), run 3x. +2. Existing suite: `cargo test -p ec-av1 --release --lib` 217+ passed, 0 failed. +3. All work committed to lane-interintra (`wip(av1): interintra r1 ...`), even on HANDOFF. +4. REPORT FILE lanes/interintra-r1.report.md, verdict FIRST line, evidence excerpts. + +## Hard rules +<=60 tool calls. AT CALL 45: commit whatever compiles + write the report as HANDOFF. +Charter-sized: if the port cannot fit, land syntax+semantics (entropy-exact, prediction +still refused by name) as r1 and hand prediction to r2 — an entropy-exact refusal is +mergeable progress; a half-blended prediction is not. diff --git a/lanes/interintra-r1.report.md b/lanes/interintra-r1.report.md new file mode 100644 index 00000000..70c53251 --- /dev/null +++ b/lanes/interintra-r1.report.md @@ -0,0 +1,62 @@ +# lane-interintra r1 report + +VERDICT: PASS — non-wedge interintra decodes pixel-exact; two defects found and fixed en route. + +## What landed (commit 277b9a0 on lane-interintra) +1. **Interintra syntax + prediction** (charter scope): interintra flag → + interintra_mode (4-ary, size-group CDF) → wedge flag (bsize-row CDF; + wedge==1 remains a NAMED refusal for r2). Non-wedge blend + `(mask*intra + (64-mask)*inter + 32) >> 6` with ii_weights1d + (II_DC=const 32, II_V/H/SMOOTH from the 1-D falloff, scale 128/side), + all planes, on both the 16/32 single-ref path and the 8x8 leaf. + motion_mode is not read for interintra blocks. New CDFs joined + cdf.rs/cdf_state.rs including reset_counts and per-frame save/restore. +2. **Defect 1 (ii-flake-1..8): interintra neighbours donated warp samples.** + libaom `av1_findSamples` (mvref_common.c:1155) requires + `ref_frame[1] == NONE_FRAME`; an interintra neighbour carries + ref1=INTRA_FRAME and is excluded. Our mi grid recorded it as plain + single-ref → extra warp sample → invalid projection → translation + fallback → pixel drift on the next WARPED_CAUSAL block. Fix: grid + records `ref_frame1 = Some(0)` for interintra blocks (both grid.set + sites); `find_samples`' existing `ref_frame1.is_none()` filter then + matches libaom, and `num_proj_ref` (same function) with it. The + mvstack compound ref-diff scan gained a `rf1 > 0` guard mirroring + libaom's `can_rf > INTRA_FRAME`; all other ref_frame1 consumers compare + against real refs 1..7 or require mv1 and are inert to the marker. +3. **Defect 2 (ii-flake-9, NOT interintra): `switchable_interp` missing + from `Cdfs::reset_counts`.** libaom zeroes every CDF counter when the + adapted context is saved at frame end (`av1_reset_cdf_symbol_counters`, + decodeframe.c:5588). Ours never reset this one table's counters, so + after >15 accumulated reads the update rate slowed to >>5 while aom + stayed at >>4; the rows drifted a few counts apart and the filter read + flaked only on read-dense streams. Localization: range ladder exact + into the block, post_motion_mode range equal, post_interp_filter range + divergent; per-read CDF dumps showed equal values but step +9 (ours) vs + +16 (aom) on ctx 8. Field-list audit (script vs struct): no other Cdfs + field is missing from reset_counts; MvComponentCdfs fully covered. + +## Evidence +- Pinned streams: 11/11 byte-exact in one run (`pinned_warp_stream_decodes_pixel_exact`, + fixtures warp-mismatch, warp-flake-5/7, ii-flake-1,2,3,5,6,7,8,9). +- Interintra gate `a_real_aomenc_stream_with_interintra_decodes_pixel_exact`: + 12/12 hammer clean post-fix (23–33 exact of 40 seeds, rest = + screen-content refusals; interintra_hits 11–22 per run; before the + fixes: 7/8 runs failed). +- Warp gate 3/3 post-fix (counter reset touches all switchable-filter streams). +- Full `cargo test -p ec-av1 --release --lib`: 218 passed, 0 failed. + +## Verification note +Cross-provider seats exhausted/stalling (opencode-go monthly, kimi-code 5h, +zai flash timeouts); orchestrator verified source-level against libaom C +directly (decodemv.c read_interintra_mode_info, reconinter.c +combine_interintra, mvref_common.c av1_findSamples, +entropy.c av1_reset_cdf_symbol_counters) — same fallback as warp close, +documented per standing rule. + +## Classes +- Defect 1 = neighbour-votes-all-its-fields + symbol-consumption-gap's + prediction-side kin (range ladder exact ⇒ prediction defect). +- Defect 2 = NEW CLASS: adaptation-state half of the CDF contract — + values right, counter wrong; presents as a late, content-dependent + rotating flake exactly like a consumption gap but with an exact range + ladder up to the divergent read. Memory written: cdf-counter-not-reset.