diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 50220905..481e6875 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -786,6 +786,46 @@ pub fn take_b128_compound_hits() -> usize { B128_COMPOUND_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) } +/// lane-b128hv: whether the 128 superblock root offers its `PARTITION_HORZ` +/// and `PARTITION_VERT` candidates -- two 128x64 / 64x128 inter blocks, the +/// same skip-only single-reference/compound machinery the NONE arm ships, +/// halved ([`search_root_128_rect`], `crate::tile::write_inter_block_128_rect`). +/// `EC_AV1_B128HV=0` is this arm's attribution control. +fn b128_rect() -> bool { + static ENV: std::sync::LazyLock> = std::sync::LazyLock::new(|| { + crate::envflags::var("EC_AV1_B128HV").ok().map(|v| v != "0") + }); + ENV.unwrap_or(true) && b128_root() +} + +/// How many 128 roots the search left as two 128x64 (HORZ) / 64x128 (VERT) +/// halves since the last take (class `gate-blind-to-feature`). +static B128_HORZ_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); +/// The VERT twin of [`B128_HORZ_HITS`]. +static B128_VERT_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// The HORZ-128-root count since the last call, and zero it. +pub fn take_b128_horz_hits() -> usize { + B128_HORZ_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) +} + +/// The VERT-128-root count since the last call, and zero it. +pub fn take_b128_vert_hits() -> usize { + B128_VERT_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) +} + +/// Test-only: force the 128 root's rect partition whatever it costs -- 0 off, +/// 1 `PARTITION_HORZ`, 2 `PARTITION_VERT`. The witness needs it because +/// synthetic content is where the shape is cheapest to decode-check and the +/// RD there prefers one whole block; the gate census is the unforced count. +static FORCE_B128_RECT: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0); + +/// Arms [`FORCE_B128_RECT`] for the rest of the process (0 / 1 HORZ / 2 VERT). +#[cfg(test)] +pub(crate) fn force_b128_rect(which: u8) { + FORCE_B128_RECT.store(which, std::sync::atomic::Ordering::Relaxed); +} + /// Test-only: take the 128 root's residual arm whatever it costs, so a /// witness clip codes the shape rather than waiting for content on which the /// RD picks it (the same role [`force_sb128`] plays for the superblock size). @@ -7379,6 +7419,31 @@ fn record_mi( } } +/// lane-b128hv: [`record_mi`] for a RECTANGULAR block -- the mi stamp the +/// writer's own `write_inter_block_128_rect` makes, so the second half of a +/// HORZ/VERT 128 root searches against the stack the writer will rebuild. +fn record_mi_rect( + grid: &mut MiGrid, + mi_row: usize, + mi_col: usize, + (w_mi, h_mi): (usize, usize), + inter: Option, + skip: bool, +) { + record_mi(grid, mi_row, mi_col, w_mi.min(h_mi) as u8, inter, skip); + // `record_mi` stamped the square corner; widen it to the true footprint, + // with the block's own (rect) size fields. + let mut info = grid.get_frame(mi_row, mi_col).expect("just recorded"); + info.size = w_mi as u8; + info.size_h = h_mi as u8; + for dr in 0..h_mi { + for dc in 0..w_mi { + grid.set(mi_row + dr, mi_col + dc, info); + grid.set_skip(mi_row + dr, mi_col + dc, skip); + } + } +} + /// The reconstructed samples one 32x32 square covers in all three planes, so /// that a partition trial can be undone. /// One square of a plane, row-major, the inverse of [`Plane::restore`]. @@ -9659,6 +9724,165 @@ fn search_root_128( )) } +/// lane-b128hv: the `PARTITION_HORZ` / `PARTITION_VERT` candidate for a whole +/// 128 superblock root -- two 128x64 (`horz`) or 64x128 inter blocks, the +/// shipped NONE machinery halved. Skip only, single reference or compound, +/// exactly what `crate::tile::write_inter_block_128_rect` codes and +/// `decode_inter_block` reads at `sb128_rect`. +/// +/// The FIRST half is published into `grid` before the second is searched -- +/// the writer publishes it too, so a second half searched against the stale +/// grid would be priced (and its mv predicted) off a stack the decoder never +/// derives (class `encoder-grid-drift-invisible-to-pixel-gate`). The grid and +/// both planes are put back before returning; the caller re-applies the +/// winner. +/// +/// Returns `(cost, lambda, the two halves, reconstruction)`, the cost already +/// carrying the root's own partition symbol so it ranks against +/// [`search_root_128`] and against the four superblocks below. +#[allow(clippy::too_many_arguments)] +fn search_root_128_rect( + luma: &mut Plane, + chroma: &mut [Plane; 2], + grid: &mut crate::mvstack::MiGrid, + (root_r, root_c): (usize, usize), + (rows, cols): (usize, usize), + (mi_cols, mi_rows): (u32, u32), + search: Search<'_>, + tpl_factors: &Option>, + sb_q: Option<&Vec>, + sb_cols: usize, + tpl_cells_x: usize, + base_q_idx: u8, + reference: &Picture, + // The `LAST` + extra-reference pairs offered at this root; only the + // reference and its picture are read here, since each HALF derives its + // own compound stack at its own rectangular window. + compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], + horz: bool, + fctx: &crate::decode::FrameCtx, +) -> Option<(f64, f64, [BlockCoeffs; 2], [(Vec, Vec); 3])> { + const SIDE: usize = SUPERBLOCK * 2; + const SIDE_MI: usize = SIDE / 4; + let (x128, y128) = (root_c * SUPERBLOCK, root_r * SUPERBLOCK); + let (mi_row, mi_col) = (root_r * 16, root_c * 16); + if root_r * 2 + 3 >= rows + || root_c * 2 + 3 >= cols + || (mi_row + SIDE_MI) as u32 > mi_rows + || (mi_col + SIDE_MI) as u32 > mi_cols + || x128 + SIDE > luma.true_width + || y128 + SIDE > luma.true_height + { + return None; + } + // The root's own lambda, the mean of its four superblocks' -- the same + // one [`search_root_128`] prices with, so the three root candidates and + // the four-superblock arm are all comparable. + let cell_lambda = |sb_r: usize, sb_c: usize| match (sb_q, tpl_factors) { + (Some(q), _) => sb_q_search(q, sb_cols, base_q_idx, (sb_r, sb_c), search).lambda, + (None, Some(f)) => { + let mean = (0..16) + .map(|i| f[(sb_r * 4 + i / 4) * tpl_cells_x + sb_c * 4 + i % 4]) + .sum::() + / 16.0; + search.lambda * mean + } + (None, None) => search.lambda, + }; + let lambda = [(0, 0), (0, 1), (1, 0), (1, 1)] + .iter() + .map(|&(dr, dc)| cell_lambda(root_r + dr, root_c + dc)) + .sum::() + / 4.0; + let root_search = Search { + lambda, + ..match sb_q { + Some(q) => sb_q_search(q, sb_cols, base_q_idx, (root_r, root_c), search), + None => search, + } + }; + let (w, h) = if horz { (SIDE, SUPERBLOCK) } else { (SUPERBLOCK, SIDE) }; + let (w_mi, h_mi) = (w / 4, h / 4); + let base = snapshot(luma, chroma, (x128, y128), SIDE); + let grid_base = grid.snapshot_rect(mi_row, mi_col, SIDE_MI, SIDE_MI); + let mut cost = 0.0f64; + let mut halves: Vec = Vec::with_capacity(2); + for i in 0..2usize { + let (mr, mc) = if horz { + (mi_row + i * 16, mi_col) + } else { + (mi_row, mi_col + i * 16) + }; + let stack = find_mv_stack( + grid, + mr, + mc, + w_mi, + h_mi, + LAST_FRAME, + mi_cols as usize, + mi_rows as usize, + ); + let half_compound: Vec<(i8, &Picture, crate::mvstack::CompoundMvStack)> = compound + .iter() + .map(|&(r, pic, _)| { + ( + r, + pic, + crate::mvstack::find_mv_stack_compound( + grid, + mr, + mc, + w_mi, + h_mi, + (LAST_FRAME, r), + mi_cols as usize, + mi_rows as usize, + grid.sign_bias_table(), + &[(0, 0); 7], + None, + ), + ) + }) + .collect(); + let found = search_skip_64( + luma, + chroma, + (mc * 4, mr * 4), + (w, h), + &root_search, + false, + reference, + &stack, + &half_compound, + fctx, + ); + match found { + Some((c, block)) => { + cost += c; + record_mi_rect(grid, mr, mc, (w_mi, h_mi), block.inter, block.skip); + halves.push(block); + } + None => { + restore(luma, chroma, (x128, y128), SIDE, &base); + grid.restore_rect(mi_row, mi_col, SIDE_MI, SIDE_MI, &grid_base); + return None; + } + } + } + let after = snapshot(luma, chroma, (x128, y128), SIDE); + restore(luma, chroma, (x128, y128), SIDE, &base); + grid.restore_rect(mi_row, mi_col, SIDE_MI, SIDE_MI, &grid_base); + let symbol = if horz { 1 } else { 2 }; + let [b0, b1] = <[BlockCoeffs; 2]>::try_from(halves).ok()?; + Some(( + cost + lambda * crate::tile::partition_bits_sym(SIDE, symbol), + lambda, + [b0, b1], + after, + )) +} + /// The 64x64 `PARTITION_NONE` candidate for a whole superblock ([`B64_ROOT`]): /// single-reference LAST at `NEARESTMV`/`GLOBALMV`/`NEARMV`/`NEWMV`, coded /// SKIP, priced through the same `symbol_bits` path every other candidate @@ -12389,6 +12613,44 @@ pub(crate) fn encode_inter_frame( fctx, )) .flatten(); + // lane-b128hv: the same root as two 128x64 / 64x128 halves. Both cuts + // are priced; the cheaper one competes with the NONE block and with + // the four superblocks below. + let forced_rect = FORCE_B128_RECT.load(std::sync::atomic::Ordering::Relaxed); + let root_rect = (sb128_search && b128_rect() && cells.len() == 4) + .then(|| { + let mut best: Option<(bool, _)> = None; + for horz in [true, false] { + if forced_rect != 0 && forced_rect != u8::from(horz) + u8::from(!horz) * 2 { + continue; + } + let Some(found) = search_root_128_rect( + &mut luma, + &mut chroma, + &mut grid, + (root_r, root_c), + (rows, cols), + (header.mi_cols, header.mi_rows), + search, + &tpl_factors, + sb_q.as_ref(), + sb_cols, + tpl_cells_x, + base_q_idx, + reference, + &root_compound, + horz, + fctx, + ) else { + continue; + }; + if best.as_ref().is_none_or(|b: &(bool, (f64, f64, _, _))| found.0 < b.1.0) { + best = Some((horz, found)); + } + } + best + }) + .flatten(); for (sb_r, sb_c) in cells { // lane-b64: the whole superblock as ONE 64x64 block, tried before // its quadrants are (`search_skip_64`) and compared against the @@ -12967,18 +13229,56 @@ pub(crate) fn encode_inter_frame( // `partition_w128` symbol) against what its four superblocks really // cost, the same weighing the 64 root does one size down. Both sides // carry their own partition symbol, so the comparison is complete. - if let Some((cost128, lambda128, block, after)) = root128 + // lane-b128hv: `None` is the NONE arm, `Some(horz)` a rect cut. + let mut cands: Vec<(f64, f64, Option, Vec, _)> = Vec::new(); + if let Some((c, l, b, a)) = root128 { + cands.push((c, l, None, vec![b], a)); + } + if let Some((horz, (c, l, halves, a))) = root_rect { + cands.push((c, l, Some(horz), halves.to_vec(), a)); + } + // The rect witness knob drops the NONE arm outright: on synthetic + // content the whole block always wins its own comparison. + if forced_rect != 0 { + cands.retain(|c| c.2.is_some()); + } + cands.sort_by(|a, b| a.0.total_cmp(&b.0)); + if let Some((cost128, lambda128, shape, halves, after)) = cands.into_iter().next() && (cost128 < root_cost + lambda128 * crate::tile::partition_bits(128, true) - // The witness knob takes the root whatever it costs, so the - // forced residual block really reaches the writer. - || FORCE_B128_ROOT.load(std::sync::atomic::Ordering::Relaxed)) + // The witness knobs take the root whatever it costs, so the + // forced block really reaches the writer. + || FORCE_B128_ROOT.load(std::sync::atomic::Ordering::Relaxed) + || forced_rect != 0) { - B128_NONE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); let (x128, y128) = (root_c * SUPERBLOCK, root_r * SUPERBLOCK); blocks.truncate(mark128); restore(&mut luma, &mut chroma, (x128, y128), SUPERBLOCK * 2, &after); - record_mi(&mut grid, root_r * 16, root_c * 16, 32, block.inter, block.skip); - blocks.push(((root_r * 2) * cols + root_c * 2, Quadrant::Whole128(block))); + let at = (root_r * 2) * cols + root_c * 2; + match shape { + None => { + B128_NONE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let block = halves.into_iter().next().expect("the NONE arm's block"); + record_mi(&mut grid, root_r * 16, root_c * 16, 32, block.inter, block.skip); + blocks.push((at, Quadrant::Whole128(block))); + } + Some(horz) => { + if horz { + B128_HORZ_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } else { + B128_VERT_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } + let (w_mi, h_mi) = if horz { (32usize, 16usize) } else { (16, 32) }; + for (i, half) in halves.iter().enumerate() { + let (mr, mc) = if horz { + (root_r * 16 + i * 16, root_c * 16) + } else { + (root_r * 16, root_c * 16 + i * 16) + }; + record_mi_rect(&mut grid, mr, mc, (w_mi, h_mi), half.inter, half.skip); + } + blocks.push((at, Quadrant::Rect128(horz, halves))); + } + } for q in 1..16 { let (r32, c32) = (root_r * 2 + q / 4, root_c * 2 + q % 4); blocks.push((r32 * cols + c32, Quadrant::Covered)); @@ -13036,10 +13336,28 @@ pub(crate) fn encode_inter_frame( matches!(q, Quadrant::Whole128(b) if b.inter.is_some_and(|i| i.ref1.is_some())) }) .count(); + // lane-b128hv: and how many roots took a rect cut instead, per shape + // -- the HORZ/VERT fire count the gate rows are read against. + let horz = blocks + .iter() + .filter(|q| matches!(q, Quadrant::Rect128(true, _))) + .count(); + let vert = blocks + .iter() + .filter(|q| matches!(q, Quadrant::Rect128(false, _))) + .count(); + let rect_compound = blocks + .iter() + .filter(|q| { + matches!(q, Quadrant::Rect128(_, hs) + if hs.iter().any(|b| b.inter.is_some_and(|i| i.ref1.is_some()))) + }) + .count(); eprintln!( "B128 census: inter frame, {whole} whole 128x128 roots ({residual} with a residual, \ - {compound} compound), {roots} coded blocks ({:.1}% of the frame's area at 128)", - 100.0 * (whole * 16) as f64 / blocks.len().max(1) as f64, + {compound} compound), {horz} HORZ + {vert} VERT roots ({rect_compound} with a \ + compound half), {roots} coded blocks ({:.1}% of the frame's area at 128)", + 100.0 * ((whole + horz + vert) * 16) as f64 / blocks.len().max(1) as f64, ); } let modes = blocks @@ -16469,6 +16787,102 @@ mod tests { } } + /// lane-b128hv: the 128 root's `PARTITION_HORZ` and `PARTITION_VERT` + /// arms -- two 128x64 / 64x128 inter blocks where the NONE arm codes one + /// square. Three passes over one `testsrc2` clip: + /// + /// * HORZ forced at every root ([`force_b128_rect`]), so every context + /// the 128x64 shape reads is exercised rather than the handful the RD + /// would pick; + /// * VERT forced the same way; + /// * UNFORCED, which is the count the gate rows are read against -- the + /// same clip with the RD free to choose among NONE / HORZ / VERT / + /// four superblocks. + /// + /// Each pass is sample-exact against the encoder's own reconstruction + /// through `decode_stream` AND through ffmpeg; a writer/reader mismatch + /// surfaces here as a refusal or a pixel miss, never in the BD gate. RED + /// before this lane by construction: `write_sb128_root` could only name + /// `PARTITION_NONE` or `PARTITION_SPLIT`, so the count was 0 on all three. + /// + /// cargo test -p ec-av1 --release --lib -- --ignored \ + /// a_128_root_horz_and_vert --nocapture + #[test] + #[ignore = "sets the process-global superblock size: run it alone"] + fn a_128_root_horz_and_vert_halves_decode_exact_through_both_decoders() { + let _knobs = crate::speed::knob_write(); + if !have_ffmpeg() { + eprintln!("SKIP: no ffmpeg"); + return; + } + force_sb128(true); + let dir = std::env::temp_dir().join(format!("ec-av1-b128hv-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let clip = dir.join("testsrc2-1280x768.y4m"); + let ok = Command::new("ffmpeg") + .args(["-v", "error", "-y", "-f", "lavfi"]) + .args(["-i", "testsrc2=size=1280x768:rate=24"]) + .args(["-frames:v", "12", "-pix_fmt", "yuv420p"]) + .arg(&clip) + .status() + .expect("ffmpeg failed to run") + .success(); + assert!(ok, "ffmpeg could not write the synthetic clip"); + let (width, height) = (1280usize, 768usize); + let pictures = + crate::probe::source(clip.to_str().unwrap(), "0", "null", width, height, 12); + let mut unforced = (0usize, 0usize); + for (label, force) in [("HORZ", 1u8), ("VERT", 2), ("unforced", 0)] { + force_b128_rect(force); + let _ = take_b128_horz_hits(); + let _ = take_b128_vert_hits(); + let _ = crate::tile::take_sb128_horz_hits(); + let _ = crate::tile::take_sb128_vert_hits(); + let encoded = encode_sequence(&pictures, 150, 0.5).unwrap(); + let (won_h, won_v) = (take_b128_horz_hits(), take_b128_vert_hits()); + let (coded_h, coded_v) = ( + crate::tile::take_sb128_horz_hits(), + crate::tile::take_sb128_vert_hits(), + ); + eprintln!( + "{label}: search won {won_h} HORZ + {won_v} VERT roots, writer coded \ + {coded_h} 128x64 + {coded_v} 64x128 halves" + ); + match force { + 1 => assert!(coded_h > 0, "no 128x64 half was coded with HORZ forced"), + 2 => assert!(coded_v > 0, "no 64x128 half was coded with VERT forced"), + // The unforced count is REPORTED, not asserted: it is the + // number the gate is read against, and a zero here would be + // an RD result, not a defect (the forced passes already prove + // the writer and the reader agree on the shape). + _ => unforced = (won_h, won_v), + } + let decoded = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); + assert_eq!(decoded.len(), encoded.frames.len(), "{label}: frame count"); + for (i, (frame, dec)) in encoded.frames.iter().zip(&decoded).enumerate() { + for (plane, got, want) in [ + ("luma", &dec.y, &frame.reconstruction.y), + ("U", &dec.u, &frame.reconstruction.u), + ("V", &dec.v, &frame.reconstruction.v), + ] { + let differ = got.iter().zip(want.iter()).filter(|(a, b)| a != b).count(); + assert_eq!(differ, 0, "{label} frame {i}: {plane} -- {differ} samples differ"); + } + } + let ff = ffmpeg_decode_sequence(&encoded.stream, width, height, pictures.len()); + for (i, (frame, dec)) in encoded.frames.iter().zip(&ff).enumerate() { + assert_eq!(dec.y, frame.reconstruction.y, "{label} frame {i}: ffmpeg luma"); + assert_eq!(dec.u, frame.reconstruction.u, "{label} frame {i}: ffmpeg U"); + assert_eq!(dec.v, frame.reconstruction.v, "{label} frame {i}: ffmpeg V"); + } + } + force_b128_rect(0); + eprintln!( + "unforced on testsrc2: {} HORZ + {} VERT roots over 12 frames", + unforced.0, unforced.1 + ); + } + #[test] #[ignore = "sets the process-global superblock size: run it alone"] fn a_128_superblock_clip_whose_drl_index_the_write_time_stack_cannot_carry_decodes_exact() { diff --git a/crates/ec-av1/src/mvstack.rs b/crates/ec-av1/src/mvstack.rs index 342c7935..c3421099 100644 --- a/crates/ec-av1/src/mvstack.rs +++ b/crates/ec-av1/src/mvstack.rs @@ -372,6 +372,49 @@ impl MiGrid { } } + /// lane-b128hv: the raw cells and skip flags of one rect region, and the + /// write-back that undoes a trial that published into it -- the 128 + /// root's HORZ/VERT search stamps its FIRST half so that the second + /// half's mv stack is the one the writer will rebuild, then puts the grid + /// back for the four-superblock arm it is weighed against. + pub fn snapshot_rect( + &self, + row: usize, + col: usize, + h: usize, + w: usize, + ) -> (Vec>, Vec) { + let mut cells = Vec::with_capacity(h * w); + let mut skips = Vec::with_capacity(h * w); + for r in row..(row + h).min(self.rows) { + let c1 = (col + w).min(self.cols); + cells.extend_from_slice(&self.cells[r * self.cols + col..r * self.cols + c1]); + skips.extend_from_slice(&self.skips[r * self.cols + col..r * self.cols + c1]); + } + (cells, skips) + } + + /// Puts a [`Self::snapshot_rect`] back. + pub fn restore_rect( + &mut self, + row: usize, + col: usize, + h: usize, + w: usize, + snap: &(Vec>, Vec), + ) { + let mut i = 0; + for r in row..(row + h).min(self.rows) { + let c1 = (col + w).min(self.cols); + let n = c1 - col; + self.cells[r * self.cols + col..r * self.cols + c1] + .copy_from_slice(&snap.0[i..i + n]); + self.skips[r * self.cols + col..r * self.cols + c1] + .copy_from_slice(&snap.1[i..i + n]); + i += n; + } + } + /// The unit at `(row, col)`, or `None` when it is outside the grid, hasn't /// been coded, or sits outside the current tile's own bounds ([`Self::set_tile_bounds`]). pub fn get(&self, row: usize, col: usize) -> Option {