diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs
index 240a9e42..7ac01af4 100644
--- a/crates/ec-av1/src/encode.rs
+++ b/crates/ec-av1/src/encode.rs
@@ -752,9 +752,7 @@ fn b128_root() -> bool {
/// the decoder derives, and on this HEAD the same row re-measures three-way
/// EXACT (encoder reconstruction == ffmpeg == `decode_stream`, every frame,
/// every plane, both gates). The knob stays as the arm's gate, not as a
-/// defect flag; the residual arm is also what
-/// [`crate::tile::write_inter_block_128_rect`]'s skip-only halves would
-/// share if they ever carry one.
+/// defect flag.
fn b128_residual() -> bool {
static ENV: std::sync::LazyLock> = std::sync::LazyLock::new(|| {
crate::envflags::var("EC_AV1_B128RES").ok().map(|v| v != "0")
@@ -762,6 +760,49 @@ fn b128_residual() -> bool {
(ENV.unwrap_or(false) || FORCE_B128_RESIDUAL.load(std::sync::atomic::Ordering::Relaxed))
&& b128_root()
}
+/// lane-rectres: whether the 128 root's RECTANGULAR pieces -- the two
+/// HORZ/VERT halves and the four AB shapes' three pieces (two 64x64 squares
+/// plus one half) -- price a REAL residual in the RD as well as the skip arm
+/// ([`search_root_128_rect`] / [`search_root_128_ab`], written through
+/// [`crate::tile::write_inter_block_128_rect`]). **Off by default**
+/// (`EC_AV1_RECTRES=1` turns it on): OFF is today's behavior bit for bit --
+/// no rect/AB piece can come out non-skip, so the writer's refusal is never
+/// reached. The residual shape is the whole 128 block's, halved or quartered
+/// per piece: one TX_64X64 luma unit plus a TX_32X32 chroma pair per 64x64
+/// mu chunk, var-tx depth 0 (exactly what the decoder's mu-chunk loop reads
+/// for any inter partition shape).
+fn rectres() -> bool {
+ static ENV: std::sync::LazyLock > = std::sync::LazyLock::new(|| {
+ crate::envflags::var("EC_AV1_RECTRES")
+ .ok()
+ .map(|v| v != "0")
+ });
+ ENV.unwrap_or(false) || FORCE_B128_RECTRES.load(std::sync::atomic::Ordering::Relaxed)
+}
+
+/// Test-only: take the rect/AB pieces' 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_B128_RESIDUAL`] plays at the NONE
+/// root). Never read outside `#[cfg(test)]` builds.
+static FORCE_B128_RECTRES: std::sync::atomic::AtomicBool =
+ std::sync::atomic::AtomicBool::new(false);
+
+/// Arms [`FORCE_B128_RECTRES`] for the rest of the process.
+#[cfg(test)]
+pub(crate) fn force_b128_rectres(on: bool) {
+ FORCE_B128_RECTRES.store(on, std::sync::atomic::Ordering::Relaxed);
+}
+
+/// How many rect/AB pieces the search left WITH a residual rather than
+/// skipped, since the last [`take_b128_rectres_hits`] (class
+/// `gate-blind-to-feature`).
+static B128_RECTRES_HITS: std::sync::atomic::AtomicUsize =
+ std::sync::atomic::AtomicUsize::new(0);
+
+/// The rect/AB-piece-residual count since the last call, and zero it.
+pub fn take_b128_rectres_hits() -> usize {
+ B128_RECTRES_HITS.swap(0, std::sync::atomic::Ordering::Relaxed)
+}
/// lane-b128r: whether the 128 root offers the COMPOUND candidates the 64
/// root already does (`NEAREST_NEARESTMV`, `GLOBAL_GLOBALMV`,
@@ -9905,11 +9946,124 @@ fn search_root_128(
))
}
+/// lane-rectres: one rect/AB piece's REAL residual, priced against the skip
+/// arm the caller's [`search_skip_64`] just committed to the planes (which
+/// IS the winner's prediction, sample for sample -- the same rule
+/// [`search_root_128`]'s residual arm runs at the NONE root). The piece's
+/// residual is one TX_64X64 luma unit plus a TX_32X32 chroma pair per 64x64
+/// mu chunk (two chunks for a 128x64 / 64x128 half, one for a 64x64 square),
+/// each trial at sides the forward network already reaches, never a
+/// rectangular transform; var-tx depth 0 only -- the units ARE the tree's
+/// leaves, exactly what `decode_inter_block`'s mu-chunk loop reads.
+///
+/// The depth-0 `txfm_partition` symbols the writer codes for the units are
+/// not priced, exactly as [`search_root_128`] does not price its four (their
+/// contexts are only known at write time); at two symbols a half this is
+/// under a bit against residuals of hundreds. On a win (or under the witness
+/// force) the trials are committed to the planes, `block` is flipped to
+/// carry the packed level grids [`crate::tile::write_inter_block_128_rect`]
+/// slices back out, and the new cost is returned; otherwise `cost` passes
+/// through untouched. Counts [`take_b128_rectres_hits`] on a win.
+#[allow(clippy::too_many_arguments)]
+fn search_rect_residual(
+ luma: &mut Plane,
+ chroma: &mut [Plane; 2],
+ // The piece's own top-left, in luma samples.
+ (px, py): (usize, usize),
+ // Its true footprint: (128, 64) HORZ, (64, 128) VERT, (64, 64) square.
+ (w, h): (usize, usize),
+ cost: f64,
+ lambda: f64,
+ search: &Search<'_>,
+ // The piece's own mv stack, for the skip symbol's cost either way.
+ stack: &crate::mvstack::MvStack,
+ block: &mut BlockCoeffs,
+) -> f64 {
+ /// The 64x64 "mu" chunk side, luma; chroma halves it.
+ const MU: usize = SUPERBLOCK;
+ let chunks: [(usize, usize); 2] = match (w, h) {
+ (128, 64) => [(0, 0), (0, 1)],
+ (64, 128) => [(0, 0), (1, 0)],
+ _ => [(0, 0), (0, 0)],
+ };
+ let n = if w.max(h) > MU { 2 } else { 1 };
+ // 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 the writer 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 sse_skip = 0.0f64;
+ let mut sse_res = 0.0f64;
+ let mut coeff_bits = 0.0f64;
+ let mut trials_per_chunk: Vec<[Trial; 3]> = Vec::with_capacity(n);
+ for &(cr, cc) in &chunks[..n] {
+ let (cxx, cyy) = (px + cc * MU, py + cr * MU);
+ let (cx, cy) = (cxx / 2, cyy / 2);
+ let half = MU / 2;
+ let pred_y = rows_of(&luma.reconstruction, luma.width, cxx, cyy, MU);
+ 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(cxx, cyy, MU, &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(
+ cxx, cyy, MU, &pred_y, false, search.base_q_idx, search.deadzone,
+ TxbSet::Luma64,
+ ),
+ chroma[0].code_from_prediction(
+ cx, cy, half, &pred_u, false, search.base_q_idx, search.deadzone,
+ TxbSet::Chroma32,
+ ),
+ chroma[1].code_from_prediction(
+ cx, cy, half, &pred_v, false, search.base_q_idx, 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]);
+ }
+ }
+ trials_per_chunk.push(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_RECTRES.load(std::sync::atomic::Ordering::Relaxed) {
+ for (trials, &(cr, cc)) in trials_per_chunk.iter().zip(&chunks[..n]) {
+ let (cxx, cyy) = (px + cc * MU, py + cr * MU);
+ let (cx, cy) = (cxx / 2, cyy / 2);
+ let half = MU / 2;
+ luma.commit(cxx, cyy, MU, &trials[0]);
+ chroma[0].commit(cx, cy, half, &trials[1]);
+ chroma[1].commit(cx, cy, half, &trials[2]);
+ }
+ 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);
+ B128_RECTRES_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ res_cost
+ } else {
+ cost
+ }
+}
+
/// 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`.
+/// shipped NONE machinery halved. Skip-only by default, single reference or
+/// compound; under `EC_AV1_RECTRES` each half also prices a REAL residual
+/// ([`search_rect_residual`]) -- 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
@@ -10041,7 +10195,23 @@ fn search_root_128_rect(
fctx,
);
match found {
- Some((c, block)) => {
+ Some((mut c, mut block)) => {
+ // lane-rectres: the same prediction with a REAL residual,
+ // priced against the skip arm the search just committed to
+ // the planes -- two 64x64 mu chunks for a half.
+ if rectres() && block.skip {
+ c = search_rect_residual(
+ luma,
+ chroma,
+ (mc * 4, mr * 4),
+ (w, h),
+ c,
+ root_search.lambda,
+ &root_search,
+ &stack,
+ &mut block,
+ );
+ }
cost += c;
record_mi_rect(grid, mr, mc, (w_mi, h_mi), block.inter, block.skip);
halves.push(block);
@@ -10081,8 +10251,9 @@ fn search_root_128_rect(
/// `encoder-grid-drift-invisible-to-pixel-gate`). The grid and both planes
/// are put back before returning; the caller re-applies the winner.
///
-/// SKIP only, for the reason [`search_root_128_rect`] gives: the 128 residual
-/// arm the pieces would share is itself parked behind `EC_AV1_B128RES`.
+/// Skip-only by default; under `EC_AV1_RECTRES` each piece also prices a
+/// REAL residual ([`search_rect_residual`]) -- the 64x64 squares are where
+/// libaom's own AB shapes earn their keep.
///
/// Returns `(cost, lambda, the three pieces, reconstruction)`, the cost
/// already carrying the root's own partition symbol so it ranks against the
@@ -10197,7 +10368,25 @@ fn search_root_128_ab(
fctx,
);
match found {
- Some((c, block)) => {
+ Some((mut c, mut block)) => {
+ // lane-rectres: the same prediction with a REAL residual,
+ // priced against the skip arm the search just committed to
+ // the planes -- one mu chunk for a 64x64 square, two for a
+ // half. The 64x64 squares are where libaom's own AB shapes
+ // earn their keep.
+ if rectres() && block.skip {
+ c = search_rect_residual(
+ luma,
+ chroma,
+ (mc * 4, mr * 4),
+ (w, h),
+ c,
+ root_search.lambda,
+ &root_search,
+ &stack,
+ &mut block,
+ );
+ }
cost += c;
record_mi_rect(grid, mr, mc, (w_mi, h_mi), block.inter, block.skip);
pieces.push(block);
@@ -17253,6 +17442,227 @@ mod tests {
}
}
+ /// lane-rectres: a `PARTITION_HORZ` 128 root whose two 128x64 halves
+ /// CARRY A RESIDUAL -- the mu-chunk shape (one TX_64X64 luma unit plus a
+ /// TX_32X32 chroma pair per chunk) `write_inter_block_128_rect` now
+ /// codes under `EC_AV1_RECTRES`, at the footprint the decoder's own
+ /// mu-chunk loop reads. The arm is FORCED ([`force_b128_rectres`], the
+ /// rect partition with [`force_b128_rect`]) so every inter root of the
+ /// clip goes through it; the `coded > 0` assert is the guard that catches
+ /// the next fixture drift (the whole-128 twin of this witness went RED
+ /// twice that way).
+ ///
+ /// cargo test -p ec-av1 --release --lib -- --ignored --exact \
+ /// encode::tests::a_rect128_half_with_a_real_residual --nocapture
+ #[test]
+ #[ignore = "sets the process-global superblock size: run it alone"]
+ fn a_rect128_half_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-rectres-{}", 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);
+ force_b128_rect(1);
+ force_b128_rectres(true);
+ let _ = take_b128_rectres_hits();
+ let _ = crate::tile::take_sb128_rectres_hits();
+ let encoded = encode_sequence(&pictures, 60, 0.5).unwrap();
+ let (won, coded) =
+ (take_b128_rectres_hits(), crate::tile::take_sb128_rectres_hits());
+ eprintln!("rect halves WITH a residual: search won {won}, writer coded {coded}");
+ assert!(
+ coded > 0,
+ "no 128x64 half 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");
+ }
+ }
+
+ /// lane-rectres: an AB shape (`PARTITION_HORZ_A` forced) whose THREE
+ /// pieces carry residuals -- the 128x64 half through the two-chunk arm,
+ /// the two 64x64 squares through the one-chunk arm (the shape libaom's
+ /// own AB partitions earn their keep with). testsrc2 at 12 frames, the
+ /// shipped AB witness's own recipe: an 8-frame clip parks an ARF whose
+ /// reference picture the GOP has not produced yet, which refuses before
+ /// any AB root is coded.
+ ///
+ /// cargo test -p ec-av1 --release --lib -- --ignored --exact \
+ /// encode::tests::an_ab128_piece_with_a_real_residual --nocapture
+ #[test]
+ #[ignore = "sets the process-global superblock size: run it alone"]
+ fn an_ab128_piece_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-rectres-ab-{}", 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);
+ force_b128_ab(4);
+ force_b128_rectres(true);
+ let _ = take_b128_rectres_hits();
+ let _ = crate::tile::take_sb128_rectres_hits();
+ let encoded = encode_sequence(&pictures, 60, 0.5).unwrap();
+ let (won, coded) =
+ (take_b128_rectres_hits(), crate::tile::take_sb128_rectres_hits());
+ eprintln!("AB pieces WITH a residual: search won {won}, writer coded {coded}");
+ assert!(
+ coded > 0,
+ "no AB piece 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");
+ }
+ }
+
+ /// lane-rectres, lane-edge128's interaction on the rect shapes: a HORZ
+ /// half WITH a residual under a clip whose CDEF search codes a
+ /// per-64x64 `cdef_idx` list. The half codes ONE literal at its origin
+ /// and the decoder stamps BOTH units it spans with it, so the owner map
+ /// ([`crate::tile::cdef_unit_owner`], now extended past Whole128) has to
+ /// collapse the pair -- exactly the bug class that broke bars 2160p at
+ /// the whole-128 root. RED before this lane by construction: no rect
+ /// piece could carry a residual, so no such literal existed.
+ ///
+ /// cargo test -p ec-av1 --release --lib -- --ignored --exact \
+ /// encode::tests::a_rect128_residual_half_under_a_per_unit_cdef --nocapture
+ #[test]
+ #[ignore = "sets the process-global superblock size: run it alone"]
+ fn a_rect128_residual_half_under_a_per_unit_cdef_list_decodes_exact() {
+ 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-rectres-cdef-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let clip = dir.join("testsrc2-1280x768.y4m");
+ // Detailed synthetic content: its CDEF search spends more than one
+ // strength pair, which is what puts a `cdef_idx` literal in the tile.
+ 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);
+ force_b128_rect(1);
+ force_b128_rectres(true);
+ let _ = crate::tile::take_sb128_rectres_hits();
+ let _ = crate::filter_search::take_cdef_covered_units();
+ let encoded = encode_sequence(&pictures, 90, 0.5).unwrap();
+ let coded = crate::tile::take_sb128_rectres_hits();
+ let covered = crate::filter_search::take_cdef_covered_units();
+ eprintln!(
+ "rect halves WITH a residual: writer coded {coded}; 64x64 CDEF units taking \
+ another unit's literal: {covered}"
+ );
+ assert!(coded > 0, "no 128x64 half carried a residual on this clip");
+ assert!(
+ covered > 0,
+ "no 64x64 unit took a non-zero preset from another unit's literal, so this clip \
+ cannot witness the rule",
+ );
+ 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() {
+ 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, "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, "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");
+ }
+ }
+
/// lane-b128r: the COMPOUND arm at the 128 root -- one 128x128 block
/// naming `LAST` plus `GOLDEN`/`ALTREF`, through the same
/// `write_compound_block` chain every smaller compound block takes, at a
@@ -20248,13 +20658,18 @@ mod tests {
{i64_edge} on a superblock the frame edge cuts",
100.0 * i64_whole as f64 / i64_offered.max(1) as f64,
);
+ let _ = take_b128_rectres_hits();
+ let _ = crate::tile::take_sb128_rectres_hits();
eprintln!(
"{name}: 128x128 roots {} (search) / {} (writer), with a residual \
+ {} (search) / {} (writer); rect/AB pieces with a residual \
{} (search) / {} (writer)",
take_b128_none_hits(),
crate::tile::take_sb128_none_hits(),
take_b128_residual_hits(),
crate::tile::take_sb128_residual_hits(),
+ take_b128_rectres_hits(),
+ crate::tile::take_sb128_rectres_hits(),
);
eprintln!(
"{name}: 64x64 roots {} (with a residual {}, split luma {}, compound {})",
diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs
index 2a357973..65064a49 100644
--- a/crates/ec-av1/src/tile.rs
+++ b/crates/ec-av1/src/tile.rs
@@ -473,13 +473,18 @@ fn write_inter_block_128(
/// block's true footprint governs only its mv-stack window, its mi/grid stamp
/// and its neighbour bands (decode.rs' own `write_w`/`write_h` split).
///
-/// SKIP only: the decoder reads a rect half's residual through the mu-chunk
-/// loop, but the 128 NONE residual arm it would share is itself parked
-/// (`EC_AV1_B128RES`), so offering one here would buy the same open filter
-/// mismatch for a candidate the RD takes 0-2 times a frame.
+/// lane-ab128: also the two 64x64 squares an AB shape codes (the `w == h`
+/// arm below) and its 128x64 / 64x128 half.
+///
+/// lane-rectres: a piece may CARRY A RESIDUAL -- the search only ever hands
+/// one over under `EC_AV1_RECTRES` ([`crate::encode::rectres`]), and then it
+/// is coded here exactly as `decode_inter_block` reads it: one TX_64X64 luma
+/// unit plus a TX_32X32 chroma pair per 64x64 mu chunk (two chunks for a
+/// half, one for a square), each depth-0 in the var-tx tree (a half codes
+/// two `txfm_partition` flags under `TxMode::Select`, a square one).
///
/// # Errors
-/// A block that is intra or that is not skipped.
+/// A block that is intra.
#[allow(clippy::too_many_arguments)]
fn write_inter_block_128_rect(
enc: &mut SymbolEncoder,
@@ -498,12 +503,6 @@ fn write_inter_block_128_rect(
let info = block.inter.ok_or_else(|| {
Error::unsupported("AV1 tile", "a 128-root rectangular half is coded inter")
})?;
- if !block.skip {
- return Err(Error::unsupported(
- "AV1 tile",
- "a 128-root rectangular half carries a residual",
- ));
- }
let (mi_r, mi_c) = at_mi;
let (w_mi, h_mi) = (w / MI, h / MI);
let has_above = neighbours.has_above(mi_r);
@@ -598,10 +597,46 @@ fn write_inter_block_128_rect(
block.motion_mode,
)?;
}
+ // The 64x64 mu chunks the residual is coded in, raster order -- the
+ // decoder's own walk: two for a half, one for a square piece.
+ let chunks: &[(usize, usize)] = match (w, h) {
+ (128, 64) => &[(0, 0), (0, 1)],
+ (64, 128) => &[(0, 0), (1, 0)],
+ _ => &[(0, 0)],
+ };
+ // A half's uv plane block (64x32 / 32x64) is wider or taller than its
+ // 32x32 unit, so the unit's `txb_skip_ctx` takes `get_txb_ctx`'s
+ // offset-10 rows (+3 here); a 64x64 piece's uv block IS the unit and
+ // takes none. The half is also the only shape whose chroma is re-stamped
+ // per mu chunk at the end (decode.rs' `mu_chroma`).
+ let is_half = w.max(h) > 64;
if tx_select {
- // `read_block_tx_size_rect`'s skip arm: `txfm_partition_update_rect`
- // with the BLOCK's own size in both bands, no symbol.
- neighbours.record_txfm_rect((mi_r, mi_c), (w, h), (w, h));
+ if block.skip {
+ // `read_block_tx_size_rect`'s skip arm: `txfm_partition_update_rect`
+ // with the BLOCK's own size in both bands, no symbol.
+ neighbours.record_txfm_rect((mi_r, mi_c), (w, h), (w, h));
+ } else if is_half {
+ // The var-tx tree of a 128x64 / 64x128 block enters at TX_64X64
+ // once per mu chunk (`read_block_tx_size_rect`'s `max_tx = 64`
+ // loop), so it is two 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],
+ w.max(h),
+ 64,
+ 64,
+ );
+ enc.symbol(0, &mut cdfs.txfm_partition[ctx]);
+ neighbours.record_txfm(unit, 64, 64, 64);
+ }
+ } else {
+ // A 64x64 piece is one whole TX_64X64: the single depth-0 flag
+ // the 64 root's own writer codes (`write_tx_syntax_inter`).
+ write_tx_syntax_inter(enc, cdfs, neighbours, (mi_r, mi_c), SB, true, false, 0);
+ }
}
let zero_grids = [
vec![0i32; TX32 * TX32],
@@ -612,7 +647,94 @@ fn write_inter_block_128_rect(
// side bands are named by the same SUB-grid origin every square block
// uses.
let at_sub = (mi_r / (SUB / MI), mi_c / (SUB / MI));
- neighbours.record_planes_rect(at_sub, (w, h), 0, &zero_grids, true);
+ if !block.skip {
+ // lane-rectres: the packed 64-wide level grids the search hands over,
+ // one plane each (the same layout `write_inter_block_128` slices for
+ // the whole block, here with the piece's own chunk list).
+ 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, and the context its all-zero flag reads: a
+ // HALF's unit is smaller than its block (neighbour magnitude
+ // table), a square's IS the whole plane block (fixed zero --
+ // `read_inter_plane`'s `luma_skip_ctx.unwrap_or(0)`, the 64
+ // root's own writer rule). It publishes its context before the
+ // next chunk reads it.
+ let luma = unit_of(0, cr, cc);
+ let skip_ctx = if is_half {
+ neighbours.luma_skip_ctx(unit_mi, 64 / MI)
+ } else {
+ 0
+ };
+ 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.
+ 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 * usize::from(is_half),
+ 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, on a half,
+ // the per-chunk chroma 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_rect(at_sub, (w, h), 0, &block_grids, false);
+ if is_half {
+ 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]);
+ }
+ }
+ SB128_RECTRES_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ } else {
+ neighbours.record_planes_rect(at_sub, (w, h), 0, &zero_grids, true);
+ }
neighbours.record_inter_rect_mi((mi_r, mi_c), (w, h), block.skip, true, block_ref(block));
if let Some(r1) = info.ref1 {
neighbours.record_compound_rect_mi((mi_r, mi_c), (w, h), r1, 0, 1);
@@ -666,6 +788,17 @@ static SB128_RESIDUAL_HITS: std::sync::atomic::AtomicUsize =
pub fn take_sb128_residual_hits() -> usize {
SB128_RESIDUAL_HITS.swap(0, std::sync::atomic::Ordering::Relaxed)
}
+/// lane-rectres: how many RECT/AB pieces (the halves and 64x64 squares
+/// [`write_inter_block_128_rect`] codes) carried a REAL residual rather than
+/// being skipped, since the last [`take_sb128_rectres_hits`] -- the writer
+/// side of [`crate::encode::take_b128_rectres_hits`]'s search count.
+static SB128_RECTRES_HITS: std::sync::atomic::AtomicUsize =
+ std::sync::atomic::AtomicUsize::new(0);
+
+/// The rect/AB-piece-with-residual count since the last call, and zero it.
+pub fn take_sb128_rectres_hits() -> usize {
+ SB128_RECTRES_HITS.swap(0, std::sync::atomic::Ordering::Relaxed)
+}
/// lane-b128r: how many of those blocks named a SECOND reference (the
/// compound arm at the 128 root), since the last
@@ -874,25 +1007,80 @@ impl TileLayout {
/// That is the shape this map exists to collapse
/// ([`crate::filter_search::pick_filters`]).
///
-/// Identity everywhere else: every block this writer codes at 64x64 or below
-/// covers exactly one unit.
+/// lane-rectres: the same holds for a Rect128 half or an Ab128 half that
+/// carries a residual (`EC_AV1_RECTRES`) -- it codes one literal at its own
+/// origin and the decoder stamps BOTH units it spans with it, so its pair is
+/// collapsed onto the origin unit too. Its 64x64 squares cover exactly one
+/// unit each, and a SKIP-ONLY rect/AB piece codes no literal at all (the
+/// decoder's per-piece `cdef_transmitted` reset never fires), so neither
+/// touches this map -- only pieces the writer really codes a residual for.
pub(crate) fn cdef_unit_owner(blocks: &[Quadrant], mi_cols: u32, mi_rows: u32) -> Vec {
let (cols, rows) = block_grid(mi_cols, mi_rows);
let (sb_cols, sb_rows) = (cols.div_ceil(2) as usize, rows.div_ceil(2) as usize);
let mut owner: Vec = (0..(sb_cols * sb_rows) as u32).collect();
+ // Collapses every 64x64 unit `(ur, uc) ..` a residual piece at unit
+ // `(ur, uc)` with footprint `(w, h)` spans onto the piece's origin --
+ // [`crate::encode::write_inter_block_128_rect`]'s one `write_cdef_idx`
+ // is what the decoder copies over all of them.
+ let collapse = |owner: &mut Vec, (ur, uc): (usize, usize), (w, h): (usize, usize)| {
+ let origin = (ur * sb_cols + uc) as u32;
+ for r in ur..(ur + (h / 64).max(1)).min(sb_rows) {
+ for c in uc..(uc + (w / 64).max(1)).min(sb_cols) {
+ owner[r * sb_cols + c] = origin;
+ }
+ }
+ };
for sb_r in (0..sb_rows).step_by(2) {
for sb_c in (0..sb_cols).step_by(2) {
- let root = ((sb_r * 2) < rows as usize && (sb_c * 2) < cols as usize)
+ let Some(root) = ((sb_r * 2) < rows as usize && (sb_c * 2) < cols as usize)
.then(|| &blocks[(sb_r * 2) * cols as usize + sb_c * 2])
- .is_some_and(|q| matches!(q, Quadrant::Whole128(_)));
- if !root {
+ else {
continue;
- }
- let origin = (sb_r * sb_cols + sb_c) as u32;
- for r in sb_r..(sb_r + 2).min(sb_rows) {
- for c in sb_c..(sb_c + 2).min(sb_cols) {
- owner[r * sb_cols + c] = origin;
+ };
+ match root {
+ Quadrant::Whole128(_) => {
+ let origin = (sb_r * sb_cols + sb_c) as u32;
+ for r in sb_r..(sb_r + 2).min(sb_rows) {
+ for c in sb_c..(sb_c + 2).min(sb_cols) {
+ owner[r * sb_cols + c] = origin;
+ }
+ }
+ }
+ // lane-rectres: the two halves of a HORZ/VERT root sit in
+ // different unit rows (HORZ) / columns (VERT); each
+ // non-skip half collapses its own pair.
+ Quadrant::Rect128(horz, halves) => {
+ for (i, half) in halves.iter().enumerate() {
+ if half.skip {
+ continue;
+ }
+ let (ur, uc) = if *horz {
+ (sb_r + i, sb_c)
+ } else {
+ (sb_r, sb_c + i)
+ };
+ let (w, h) = if *horz { (128, 64) } else { (64, 128) };
+ collapse(&mut owner, (ur, uc), (w, h));
+ }
+ }
+ // lane-rectres: the AB half is the only multi-unit piece
+ // (its two squares are one unit each); only a non-skip one
+ // codes a literal, so only it fires.
+ Quadrant::Ab128(symbol, pieces) => {
+ for (piece, ((mi_r, mi_c), (w, h))) in
+ pieces.iter().zip(crate::encode::ab128_pieces(
+ usize::from(*symbol),
+ (0, 0),
+ ))
+ {
+ if piece.skip {
+ continue;
+ }
+ let (ur, uc) = (sb_r + mi_r / 16, sb_c + mi_c / 16);
+ collapse(&mut owner, (ur, uc), (w, h));
+ }
}
+ _ => {}
}
}
}
@@ -2605,8 +2793,9 @@ pub enum Quadrant {
/// Carried by the top-left 32x32 entry of the root's sixteen, exactly as
/// [`Whole128`](Quadrant::Whole128) is.
///
- /// SKIP only ([`crate::tile::write_inter_block_128_rect`] refuses a
- /// residual), single reference or compound.
+ /// Skip-only by default; a piece carries a real residual under
+ /// `EC_AV1_RECTRES` (`crate::encode::rectres`), single reference or
+ /// compound either way.
Rect128(bool, Vec),
/// lane-ab128: the THREE inter blocks an AB shape at a 128 superblock
/// root carries -- two 64x64 pieces and one 128x64 / 64x128 half -- in
@@ -2616,8 +2805,8 @@ pub enum Quadrant {
/// top-left 32x32 entry of the root's sixteen, exactly as
/// [`Rect128`](Quadrant::Rect128) is.
///
- /// SKIP only, same writer ([`crate::tile::write_inter_block_128_rect`])
- /// and same reason.
+ /// Skip-only by default, same writer; under `EC_AV1_RECTRES` any of the
+ /// three pieces may carry a real residual.
Ab128(u8, Vec),
/// A quadrant a [`Whole64`](Quadrant::Whole64) at its superblock's
/// top-left already coded; it carries no block of its own.