diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 70412e2f..c4861cc7 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1627,9 +1627,16 @@ impl Plane<'_> { }, }; let n = (side / 4).max(1); - for row in 0..n { + // Only up to the frame's TRUE extent: the writer's own + // `Neighbours::record_planes` clamps to `blocks_wide`/`blocks_high` + // (spec `av1_set_entropy_contexts`, off the true `mi_cols`/`mi_rows`) + // and leaves the cells past it uncoded even mid-block, so a + // straddling block publishes context for its inside part only. + let rows = n.min(self.true_height.saturating_sub(y).div_ceil(4)); + let cols = n.min(self.true_width.saturating_sub(x).div_ceil(4)); + for row in 0..rows { let start = (y / 4 + row) * self.ctx.stride + x / 4; - let end = (start + n).min(self.ctx.cells.len()); + let end = (start + cols).min(self.ctx.cells.len()); if start < end { self.ctx.cells[start..end].fill(state); } @@ -10721,7 +10728,10 @@ mod tests { /// above, not by this pin. Re-pinned on lane-av1price2: the search now /// prices coefficients against the tables the frame's writer really /// starts from on every non-screen frame (`tile::arm_pricing_cdfs`), a - /// decision change the BD gate judges. + /// decision change the BD gate judges. Re-pinned again on + /// lane-av1skipctx: the search now prices `txb_skip`/`dc_sign` at the + /// real neighbour contexts ([`Plane::coef_ctx`]) instead of zero, which + /// is the same kind of decision change. #[test] fn the_encoders_own_streams_are_byte_identical_to_their_pins() { if !have_ffmpeg() { @@ -10743,7 +10753,7 @@ mod tests { }) }; let pins: [(u8, usize, u64); 2] = - [(150, 7106, 0x1da4_9acd_a892_68e3), (60, 25963, 0x94df_8f46_174e_1a5e)]; + [(150, 7141, 0x3a6a_df46_9b16_d9a7), (60, 25906, 0xbf71_9320_9e50_d237)]; for (q, bytes, hash) in pins { let encoded = encode_sequence(&source, q, 0.5).unwrap(); assert_eq!( diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 4cc6869d..db66f549 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -3033,6 +3033,35 @@ pub(crate) mod tests { let decoded = decode_stream(&encoded.stream).unwrap(); assert_eq!(decoded.len(), encoded.frames.len()); for (i, (got, frame)) in decoded.iter().zip(&encoded.frames).enumerate() { + for (name, a, b) in [ + ("y", &got.y, &frame.reconstruction.y), + ("u", &got.u, &frame.reconstruction.u), + ("v", &got.v, &frame.reconstruction.v), + ] { + let w = if name == "y" { width } else { width.div_ceil(2) }; + if let Some(k) = a.iter().zip(b.iter()).position(|(p, q)| p != q) { + eprintln!( + "DBG {width}x{height} frame {i} {name}: {} of {} differ, first at row {} col {} stride {w} ({}, {}), rows {:?} cols {:?}", + a.iter().zip(b.iter()).filter(|(p, q)| p != q).count(), + a.len(), + k / w, + k % w, + a[k], + b[k], + { + let mut v: Vec = a.iter().zip(b.iter()).enumerate().filter(|(_, (p, q))| p != q).map(|(j, _)| j / w).collect(); + v.dedup(); + v + }, + { + let mut v: Vec = a.iter().zip(b.iter()).enumerate().filter(|(_, (p, q))| p != q).map(|(j, _)| j % w).collect(); + v.sort_unstable(); + v.dedup(); + v + } + ); + } + } assert_eq!( got.y, frame.reconstruction.y, "{width}x{height} frame {i} luma" diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index f53b7d2a..765edab5 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -3981,20 +3981,28 @@ pub(crate) fn luma_32_coeff_bits(grid: &[i32]) -> f64 { /// tables), and against the contexts a block whose neighbours coded nothing /// reads. /// -/// That last assumption -- `skip_ctx` 0, `sign_ctx` 0 -- is the largest -/// remaining row of the census and lane-av1price2 did NOT close it: every -/// all-zero block is priced at the `txb_skip` cost of a block whose -/// neighbours coded nothing, which is the most expensive context there is, so -/// an all-zero chroma block is UNDER-priced by 41-81% on film and 51-73% on -/// the screen capture (`pricer_error_census_on_clips`), and an all-zero -/// Luma4 is over-priced by 400%+. Arming the frame's real tables does not -/// touch those rows (Chroma8 nz=0 reads -73.0% in both modes). Pricing them -/// right needs the real neighbour context, which needs something the search -/// does not have: a per-plane non-zero context map committed in coding order. -/// The search ranks partition TREES, so it prices candidates whose neighbours -/// are not yet decided; the map would have to be built per trial, not per -/// block. Deferred with the numbers above rather than approximated by a -/// fitted constant. +/// lane-av1skipctx CLOSED that row: `skip_ctx`/`sign_ctx` are the caller's +/// now, and [`crate::encode::CoefCtxMap`] -- a per-plane, per-4x4-cell map +/// committed in coding order and undone with a losing partition trial's +/// pixels -- is the per-trial neighbour state the previous lane said the +/// search did not have. The census (6 frames of each clip at 640x384, q=100, +/// `pricer_error_census_on_clips`) moved: +/// +/// | row | before | after | +/// |---|---|---| +/// | screen total | +10.3% | +9.5% | +/// | screen Chroma8 nz=0 | -73.0% | -1.9% | +/// | screen Chroma16 nz=0 | -48% | -23.4% | +/// | screen Chroma4 nz=0 | -74% | -6.7% | +/// | screen Chroma8 nz=1 / nz=2 | +32.2% / +26.2% | +4.9% / +7.9% | +/// | screen Luma4 nz=0 | +464% | +89.5% | +/// | film total | +5.3% | +1.5% | +/// +/// What is left in the all-zero rows is table ADAPTATION, not context: the +/// price is taken against the frame's starting tables, and a frame whose +/// blocks mostly code nothing narrows `txb_skip` far below where it started. +/// A whole-block luma transform still reads context 0 -- that is what the +/// writer codes (`write_luma_tus`, `n == 1`), not an approximation. pub(crate) fn coeff_bits( grid: &[i32], set: TxbSet, @@ -4133,8 +4141,10 @@ thread_local! { /// luma price accurate while leaving the bigger chroma and all-zero errors /// exactly where they were, which SKEWS the search's tradeoffs between them /// instead of correcting them -- and the bytes get worse. The fix for that -/// row is the neighbour skip context, not another table set (see -/// [`coeff_bits`]). +/// row was the neighbour skip context, not another table set, and +/// lane-av1skipctx landed it (see [`coeff_bits`]): with the real contexts +/// under every price the screen capture comes down +59.4 -> +58.3 vs libaom +/// at native, still with every one of its frames armed with the defaults. pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>, screen: bool) { static MODE: std::sync::OnceLock = std::sync::OnceLock::new(); let mode = *MODE.get_or_init(|| match std::env::var("EC_AV1_PRICE_FRAME_CDFS").as_deref() {