From 5fd3825b8b0959ea8eb38a0603c4ab5b4533b687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 8 Sep 2026 16:28:14 +0300 Subject: [PATCH] wip(av1): b128r lane snapshot at the turn cap (orchestrator) --- crates/ec-av1/src/encode.rs | 212 +++++++++++++++++++++++++++++++++++- crates/ec-av1/src/tile.rs | 168 +++++++++++++++++++++++++--- 2 files changed, 363 insertions(+), 17 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 0b03c6d4..e4bdf8db 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -733,6 +733,42 @@ fn b128_root() -> bool { *ENV } +/// lane-b128r: whether the 128 root prices a REAL residual as well as the +/// skip arm -- four TX_64X64 luma units interleaved with a TX_32X32 chroma +/// pair per 64x64 mu chunk, var-tx depth 0 (the shape +/// [`crate::tile::write_inter_block_128`] codes and `decode_inter_block` +/// reads). On by default; `EC_AV1_B128RES=0` restores lane-b128's skip-only +/// root, which is this arm's own attribution control. +fn b128_residual() -> bool { + static ENV: std::sync::LazyLock> = std::sync::LazyLock::new(|| { + crate::envflags::var("EC_AV1_B128RES").ok().map(|v| v != "0") + }); + ENV.unwrap_or(true) && b128_root() +} + +/// 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). +/// Never read outside `#[cfg(test)]` builds. +static FORCE_B128_RESIDUAL: std::sync::atomic::AtomicBool = + std::sync::atomic::AtomicBool::new(false); + +/// Arms [`FORCE_B128_RESIDUAL`] for the rest of the process. +#[cfg(test)] +pub(crate) fn force_b128_residual(on: bool) { + FORCE_B128_RESIDUAL.store(on, std::sync::atomic::Ordering::Relaxed); +} + +/// How many 128 roots came out cheaper WITH a residual than skipped, since +/// the last [`take_b128_residual_hits`] (class `gate-blind-to-feature`). +static B128_RESIDUAL_HITS: std::sync::atomic::AtomicUsize = + std::sync::atomic::AtomicUsize::new(0); + +/// The 128-residual count since the last call, and zero it. +pub fn take_b128_residual_hits() -> usize { + B128_RESIDUAL_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) +} + /// How many 128x128 `PARTITION_NONE` roots the search took since the last /// [`take_b128_none_hits`] (class `gate-blind-to-feature`): a flat BD result /// is only attributable once the fire count is known. @@ -9428,7 +9464,16 @@ fn search_root_128( .map(|&(dr, dc)| cell_lambda(root_r + dr, root_c + dc)) .sum::() / 4.0; - let root_search = Search { lambda, ..search }; + // The qindex this root is really quantized at: the delta_q unit is the + // 128 superblock, so the root's own top-left cell is the one coded + // (lane-b128m), and the residual arm below has to quantize at exactly it. + 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 stack = find_mv_stack( grid, mi_row, @@ -9452,7 +9497,84 @@ fn search_root_128( &[], fctx, ); - let (cost, block) = found?; + let (mut cost, mut block) = found?; + // lane-b128r: the same prediction with a REAL residual, priced against + // the skip arm the search just committed to the planes (which IS the + // winner's prediction, sample for sample). The block's residual is four + // TX_64X64 luma units, one per 64x64 mu chunk, each interleaved with its + // own TX_32X32 chroma pair -- so it is coded here as twelve trials at + // sides the forward network already reaches, never as one 128-point + // transform. Var-tx depth 0 only: the four units ARE the tree's leaves. + // + // The four `txfm_partition` symbols the writer codes for them are not + // priced, exactly as the 64 root does not price its own (their contexts + // are only known at write time); at four symbols a root this is under a + // bit against residuals of hundreds. + if b128_residual() && block.skip { + // Every unit's coded level grid is 32x32 (a 64-point transform codes + // only its top-left corner), packed per plane into one 64-wide grid + // with chunk (cr, cc) at (cr * 32, cc * 32) -- the layout + // `write_inter_block_128` slices back out in the same chunk order. + let mut packed = [vec![0i32; 64 * 64], vec![0i32; 64 * 64], vec![0i32; 64 * 64]]; + let mut chunks: Vec<((usize, usize), [Trial; 3])> = Vec::new(); + let (mut sse_skip, mut sse_res, mut coeff_bits) = (0.0f64, 0.0f64, 0.0f64); + for (cr, cc) in [(0usize, 0usize), (0, 1), (1, 0), (1, 1)] { + let (px, py) = (x128 + cc * SUPERBLOCK, y128 + cr * SUPERBLOCK); + let (cx, cy) = (px / 2, py / 2); + let half = SUPERBLOCK / 2; + let pred_y = rows_of(&luma.reconstruction, luma.width, px, py, SUPERBLOCK); + let pred_u = rows_of(&chroma[0].reconstruction, chroma[0].width, cx, cy, half); + let pred_v = rows_of(&chroma[1].reconstruction, chroma[1].width, cx, cy, half); + sse_skip += luma.block_sse(px, py, SUPERBLOCK, &pred_y) + + chroma[0].block_sse(cx, cy, half, &pred_u) + + chroma[1].block_sse(cx, cy, half, &pred_v); + let trials = [ + luma.code_from_prediction( + px, py, SUPERBLOCK, &pred_y, false, root_search.base_q_idx, + root_search.deadzone, TxbSet::Luma64, + ), + chroma[0].code_from_prediction( + cx, cy, half, &pred_u, false, root_search.base_q_idx, + root_search.deadzone, TxbSet::Chroma32, + ), + chroma[1].code_from_prediction( + cx, cy, half, &pred_v, false, root_search.base_q_idx, + root_search.deadzone, TxbSet::Chroma32, + ), + ]; + for (plane, trial) in trials.iter().enumerate() { + sse_res += trial.sse; + coeff_bits += trial.bits; + for row in 0..32 { + packed[plane][(cr * 32 + row) * 64 + cc * 32..][..32] + .copy_from_slice(&trial.levels[row * 32..][..32]); + } + } + chunks.push(((cr, cc), trials)); + } + // `cost` is the skip arm's own `sse + lambda * syntax`; the residual + // arm pays the same syntax with `skip` coded 0 instead of 1, plus its + // coefficients. + let unskip = skip_bits_at(&stack, false) - skip_bits_at(&stack, true); + let res_cost = cost - sse_skip + sse_res + lambda * (unskip + coeff_bits); + if res_cost < cost || FORCE_B128_RESIDUAL.load(std::sync::atomic::Ordering::Relaxed) { + B128_RESIDUAL_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + for ((cr, cc), trials) in &chunks { + let (px, py) = (x128 + cc * SUPERBLOCK, y128 + cr * SUPERBLOCK); + let half = SUPERBLOCK / 2; + luma.commit(px, py, SUPERBLOCK, &trials[0]); + chroma[0].commit(px / 2, py / 2, half, &trials[1]); + chroma[1].commit(px / 2, py / 2, half, &trials[2]); + } + cost = res_cost; + block.skip = false; + block.tx_depth = 0; + block.luma_tx_types = Vec::new(); + block.luma = coeffs(&packed[0], 64); + block.u = coeffs(&packed[1], 64); + block.v = coeffs(&packed[2], 64); + } + } let after = snapshot(luma, chroma, (x128, y128), SIDE); restore(luma, chroma, (x128, y128), SIDE, &base); Some(( @@ -12722,7 +12844,10 @@ pub(crate) fn encode_inter_frame( // 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 - && cost128 < root_cost + lambda128 * crate::tile::partition_bits(128, true) + && (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_RESIDUAL.load(std::sync::atomic::Ordering::Relaxed)) { B128_NONE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); let (x128, y128) = (root_c * SUPERBLOCK, root_r * SUPERBLOCK); @@ -12772,8 +12897,15 @@ pub(crate) fn encode_inter_frame( .iter() .filter(|q| !matches!(q, Quadrant::Covered)) .count(); + // lane-b128r: of those roots, how many code a REAL residual rather + // than the skip-only block lane-b128 shipped -- the fire count this + // arm's own gate row is read against. + let residual = blocks + .iter() + .filter(|q| matches!(q, Quadrant::Whole128(b) if !b.skip)) + .count(); eprintln!( - "B128 census: inter frame, {whole} whole 128x128 roots, {roots} coded blocks ({:.1}% of the frame's area at 128)", + "B128 census: inter frame, {whole} whole 128x128 roots ({residual} with a residual), {roots} coded blocks ({:.1}% of the frame's area at 128)", 100.0 * (whole * 16) as f64 / blocks.len().max(1) as f64, ); } @@ -16061,6 +16193,78 @@ mod tests { } } + /// lane-b128r: the 128 root's own RESIDUAL arm -- four TX_64X64 luma + /// units interleaved with a TX_32X32 chroma pair per 64x64 mu chunk. The + /// same pan the skip-only witness above uses, coded at a quantizer where + /// the residual is worth its bits, so the counters below are non-zero; + /// RED before this lane by construction (`write_inter_block_128` refused + /// every non-skip 128 block, so the count was 0 and no such block + /// existed to decode). + /// + /// cargo test -p ec-av1 --release --lib -- --ignored \ + /// a_128_root_block_with_a_real_residual --nocapture + #[test] + #[ignore = "sets the process-global superblock size: run it alone"] + fn a_128_root_block_with_a_real_residual_decodes_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-b128r-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let clip = dir.join("smpte-pan-1280x768.y4m"); + let ok = Command::new("ffmpeg") + .args(["-v", "error", "-y", "-f", "lavfi"]) + .args(["-i", "smptebars=size=1600x768:rate=24"]) + .args(["-vf", "crop=1280:768:'min(n*2,300)':0", "-frames:v", "8"]) + .args(["-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, 8); + let _ = take_b128_residual_hits(); + let _ = crate::tile::take_sb128_residual_hits(); + force_b128_residual(true); + let encoded = encode_sequence(&pictures, 150, 0.5).unwrap(); + let (won, coded) = + (take_b128_residual_hits(), crate::tile::take_sb128_residual_hits()); + eprintln!("128x128 blocks WITH a residual: search won {won}, writer coded {coded}"); + assert!( + coded > 0, + "no 128x128 block carried a residual on this clip, so it cannot witness the arm", + ); + let decoded = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); + assert_eq!(decoded.len(), encoded.frames.len(), "frame count"); + for (i, (frame, dec)) in encoded.frames.iter().zip(&decoded).enumerate() { + let recon = &frame.reconstruction; + for (plane, got, want) in [ + ("luma", &dec.y, &recon.y), + ("U", &dec.u, &recon.u), + ("V", &dec.v, &recon.v), + ] { + let differ = got.iter().zip(want.iter()).filter(|(a, b)| a != b).count(); + assert_eq!( + differ, 0, + "frame {i}: {plane} -- {differ} of {} samples differ from the encoder's \ + own reconstruction", + got.len() + ); + } + } + 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, "frame {i}: ffmpeg luma"); + assert_eq!(dec.u, frame.reconstruction.u, "frame {i}: ffmpeg U"); + assert_eq!(dec.v, frame.reconstruction.v, "frame {i}: ffmpeg V"); + } + } + #[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/tile.rs b/crates/ec-av1/src/tile.rs index d8f600d0..6832a335 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -215,14 +215,20 @@ fn write_sb128_root( /// 32x32-mi window, the motion mode, and the transform syntax, with the mi /// grid and every neighbour band published over all thirty-two mi cells. /// -/// SKIP only, and refused otherwise: a 128x128 block's residual is four -/// TX_64X64 luma units interleaved with a TX_32X32 chroma pair per 64x64 mu -/// chunk (the chroma units reading the offset-10 `txb_skip` rows), which -/// this writer has no path for. A skipped block writes no `cdef_idx` and no -/// `delta_q` either, exactly as the 64 root's skip arm does. +/// lane-b128r: a block that carries a RESIDUAL codes it as the four 64x64 mu +/// chunks libaom's `decode_token_recon_block` walks -- per chunk one +/// TX_64X64 luma unit (its coded 32x32 corner, `TxbSet::Luma64`, DCT-only at +/// this size) then one TX_32X32 unit per chroma plane on the offset-10 +/// `txb_skip` rows, each publishing its own coefficient context before the +/// next unit reads it. Its transform syntax is the var-tx tree's four +/// depth-0 `txfm_partition` symbols, one per chunk. The levels arrive packed +/// per plane into a 64-wide grid, chunk (cr, cc) at (cr * 32, cc * 32) +/// ([`crate::encode::search_root_128`]). A skipped block writes no +/// `cdef_idx` and no `delta_q`, exactly as the 64 root's skip arm does. /// /// # Errors -/// A block that is intra, or carries a residual, at this size. +/// A block that is intra, that is compound with a residual, or whose var-tx +/// depth is not 0, at this size. #[allow(clippy::too_many_arguments)] fn write_inter_block_128( enc: &mut SymbolEncoder, @@ -239,13 +245,16 @@ fn write_inter_block_128( /// The block's side in samples and in 4x4 mode-info units. const SIDE: usize = 128; const SIDE_MI: usize = SIDE / MI; + /// The four 64x64 "mu" chunks, in the order libaom's + /// `decode_token_recon_block` walks them (raster). + const CHUNKS: [(usize, usize); 4] = [(0, 0), (0, 1), (1, 0), (1, 1)]; let info = block.inter.ok_or_else(|| { Error::unsupported("AV1 tile", "a 128x128 superblock root block is coded inter") })?; - if !block.skip { + if !block.skip && block.tx_depth != 0 { return Err(Error::unsupported( "AV1 tile", - "a 128x128 superblock root block carries a residual", + "a 128x128 superblock root block splits its var-tx tree below TX_64X64", )); } let (mi_r, mi_c) = (sb_at.0 * (SUB / MI), sb_at.1 * (SUB / MI)); @@ -253,9 +262,9 @@ fn write_inter_block_128( let has_left = neighbours.has_left(mi_c); let skip_ctx = usize::from(neighbours.above_skip[mi_c]) + usize::from(neighbours.left_skip[mi_r]); - enc.symbol(1, &mut cdfs.skip[skip_ctx]); - write_cdef_idx(enc, (mi_r, mi_c), true); - write_delta_q(enc, cdfs, (mi_r, mi_c), true, true); + enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); + write_cdef_idx(enc, (mi_r, mi_c), block.skip); + write_delta_q(enc, cdfs, (mi_r, mi_c), true, block.skip); let ii_ctx = intra_inter_ctx( has_above, has_left, @@ -340,20 +349,129 @@ fn write_inter_block_128( )?; } if tx_select { - write_tx_syntax_inter(enc, cdfs, neighbours, (mi_r, mi_c), SIDE, true, true, 0); + if block.skip { + write_tx_syntax_inter(enc, cdfs, neighbours, (mi_r, mi_c), SIDE, true, true, 0); + } else { + // The var-tx tree of a 128x128 block enters at TX_64X64 once per + // mu chunk (`read_block_tx_size`'s `max_tx = side.min(64)` loop), + // so it is four depth-0 `txfm_split` flags of zero, each read + // against the bands the chunks before it published. + for (cr, cc) in CHUNKS { + let unit = (mi_r + cr * 16, mi_c + cc * 16); + let ctx = txfm_partition_ctx_rect( + neighbours.above_txfm[unit.1], + neighbours.left_txfm[unit.0], + SIDE, + 64, + 64, + ); + enc.symbol(0, &mut cdfs.txfm_partition[ctx]); + neighbours.record_txfm(unit, 64, 64, 64); + } + } } let zero_grids = [ vec![0i32; TX32 * TX32], vec![0i32; TX32 * TX32], vec![0i32; TX32 * TX32], ]; - neighbours.record(sb_at, SIDE, 0, &zero_grids); + if !block.skip { + // The packed 64-wide level grids the search hands over, one plane + // each (see this function's doc). + let packed = [ + level_grid(&block.luma, 64)?, + level_grid(&block.u, 64)?, + level_grid(&block.v, 64)?, + ]; + let scan32 = default_scan(TX32); + let unit_of = |plane: usize, cr: usize, cc: usize| -> Vec { + (0..32) + .flat_map(|row| { + packed[plane][(cr * 32 + row) * 64 + cc * 32..][..32].to_vec() + }) + .collect() + }; + let mut chroma_units: Vec<((usize, usize), [Vec; 2])> = Vec::new(); + for (cr, cc) in CHUNKS { + let unit_mi = (mi_r + cr * 16, mi_c + cc * 16); + // The luma unit: its own `txb_skip_ctx` is the neighbour + // magnitude table (the unit is smaller than its block), and it + // publishes its context before the next chunk reads it. + let luma = unit_of(0, cr, cc); + let skip_ctx = neighbours.luma_skip_ctx(unit_mi, 64 / MI); + let around = neighbours.around_mi(unit_mi, 64); + write_coeffs( + enc, + &mut cdfs.txb(TxbSet::Luma64, 0), + &luma, + &scan32, + skip_ctx, + dc_sign_ctx(around[0].dc_vote), + Some(0), + TxType::DctDct, + ); + neighbours.record_mi_luma(unit_mi, 64, &luma); + // Then this chunk's chroma pair, before the next chunk's luma: + // libaom codes every plane of a mu chunk before moving on. The + // chroma `txb_skip_ctx` takes `get_txb_ctx`'s offset-10 rows + // (+3 here) because the uv plane block is bigger than the unit. + let mut pair = [Vec::new(), Vec::new()]; + for plane in 1..3usize { + let grid = unit_of(plane, cr, cc); + let around = neighbours.around_mi(unit_mi, 64)[plane]; + write_coeffs( + enc, + &mut cdfs.txb(TxbSet::Chroma32, 0), + &grid, + &scan32, + usize::from(around.above_coded) + usize::from(around.left_coded) + 3, + dc_sign_ctx(around.dc_vote), + Some(plane), + TxType::DctDct, + ); + neighbours.record_mi_chroma(unit_mi, 64, plane, &grid); + pair[plane - 1] = grid; + } + chroma_units.push((unit_mi, pair)); + } + // decode.rs' own tail (`record_split_luma_rect_mi` then the + // `mu_chroma_units` re-stamp): the block-level write off the WHOLE + // assembled chroma plane grid, luma kept because each unit already + // published its own, and then the per-unit chroma writes replayed + // over it -- last writer wins, so the next block reads the units. + let block_grids = [ + vec![0i32; TX32 * TX32], + packed[1].clone(), + packed[2].clone(), + ]; + neighbours.record_planes(sb_at, SIDE, 0, &block_grids, false); + for (unit_mi, pair) in &chroma_units { + neighbours.record_mi_chroma(*unit_mi, 64, 1, &pair[0]); + neighbours.record_mi_chroma(*unit_mi, 64, 2, &pair[1]); + } + } else { + neighbours.record(sb_at, SIDE, 0, &zero_grids); + } neighbours.record_inter(sb_at, SIDE, true, true, block_ref(block)); record_block_compound(neighbours, (mi_r, mi_c), SIDE, block); SB128_NONE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + if !block.skip { + SB128_RESIDUAL_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } Ok(()) } +/// lane-b128r: how many of those blocks carried a REAL residual (four +/// TX_64X64 luma units plus their per-chunk chroma pair) rather than being +/// skipped, since the last [`take_sb128_residual_hits`]. +static SB128_RESIDUAL_HITS: std::sync::atomic::AtomicUsize = + std::sync::atomic::AtomicUsize::new(0); + +/// The 128x128-with-residual count since the last call, and zero it. +pub fn take_sb128_residual_hits() -> usize { + SB128_RESIDUAL_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) +} + /// How many 128x128 `PARTITION_NONE` blocks the inter writer has coded since /// the last [`take_sb128_none_hits`] (class `gate-blind-to-feature`). static SB128_NONE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); @@ -2718,6 +2836,30 @@ impl Neighbours { } } + /// lane-b128r: publishes ONE chroma transform unit's coefficient context, + /// the decoder's own `record_mi_chroma` -- a 128x128 block's chroma is one + /// TX_32X32 unit per plane per 64x64 mu chunk, and the next chunk reads + /// the previous one's state (its `txb_skip_ctx` base 1 from the left + /// neighbour). `span_px` is the unit's LUMA span, which is what the + /// luma-mi-indexed bands are addressed in. + fn record_mi_chroma( + &mut self, + (mi_r, mi_c): (usize, usize), + span_px: usize, + plane: usize, + grid: &[i32], + ) { + let round_up_even = |n: usize| n.div_ceil(2) * 2; + let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); + let state = neighbour_state(grid); + for cell in 0..(span_px / MI).min(bound_h.saturating_sub(mi_r)) { + self.left[mi_r + cell][plane] = state; + } + for cell in 0..(span_px / MI).min(bound_w.saturating_sub(mi_c)) { + self.above[mi_c + cell][plane] = state; + } + } + /// Writes one coded block into every 16x16 column and row it covers, and, /// on the finer 4x4 grid libaom's entropy context arrays are actually /// kept on, into every unit up to the true frame edge -- the units past