forge
Explore
Log in

Don't you dare go Hollow

wip(av1): per-trial neighbour nz context for the pricer, snapshot at the agent cap (may not compile)

Browse files at this commit

b71a8507e67af2ce29524344d12bd22e4a230966Ahmet Kaan GümüşParent: 9afbc09
crates/ec-av1/src/encode.rsmodified+14 -4
@@ -1627,9 +1627,16 @@ impl Plane<'_> {
1627 },1627 },
1628 };1628 };
1629 let n = (side / 4).max(1);1629 let n = (side / 4).max(1);
1630- for row in 0..n {1630+ // Only up to the frame's TRUE extent: the writer's own
1631+ // `Neighbours::record_planes` clamps to `blocks_wide`/`blocks_high`
1632+ // (spec `av1_set_entropy_contexts`, off the true `mi_cols`/`mi_rows`)
1633+ // and leaves the cells past it uncoded even mid-block, so a
1634+ // straddling block publishes context for its inside part only.
1635+ let rows = n.min(self.true_height.saturating_sub(y).div_ceil(4));
1636+ let cols = n.min(self.true_width.saturating_sub(x).div_ceil(4));
1637+ for row in 0..rows {
1631 let start = (y / 4 + row) * self.ctx.stride + x / 4;1638 let start = (y / 4 + row) * self.ctx.stride + x / 4;
1632- let end = (start + n).min(self.ctx.cells.len());1639+ let end = (start + cols).min(self.ctx.cells.len());
1633 if start < end {1640 if start < end {
1634 self.ctx.cells[start..end].fill(state);1641 self.ctx.cells[start..end].fill(state);
1635 }1642 }
@@ -10721,7 +10728,10 @@ mod tests {
10721 /// above, not by this pin. Re-pinned on lane-av1price2: the search now10728 /// above, not by this pin. Re-pinned on lane-av1price2: the search now
10722 /// prices coefficients against the tables the frame's writer really10729 /// prices coefficients against the tables the frame's writer really
10723 /// starts from on every non-screen frame (`tile::arm_pricing_cdfs`), a10730 /// starts from on every non-screen frame (`tile::arm_pricing_cdfs`), a
10724- /// decision change the BD gate judges.10731+ /// decision change the BD gate judges. Re-pinned again on
10732+ /// lane-av1skipctx: the search now prices `txb_skip`/`dc_sign` at the
10733+ /// real neighbour contexts ([`Plane::coef_ctx`]) instead of zero, which
10734+ /// is the same kind of decision change.
10725 #[test]10735 #[test]
10726 fn the_encoders_own_streams_are_byte_identical_to_their_pins() {10736 fn the_encoders_own_streams_are_byte_identical_to_their_pins() {
10727 if !have_ffmpeg() {10737 if !have_ffmpeg() {
@@ -10743,7 +10753,7 @@ mod tests {
10743 })10753 })
10744 };10754 };
10745 let pins: [(u8, usize, u64); 2] =10755 let pins: [(u8, usize, u64); 2] =
10746- [(150, 7106, 0x1da4_9acd_a892_68e3), (60, 25963, 0x94df_8f46_174e_1a5e)];10756+ [(150, 7141, 0x3a6a_df46_9b16_d9a7), (60, 25906, 0xbf71_9320_9e50_d237)];
10747 for (q, bytes, hash) in pins {10757 for (q, bytes, hash) in pins {
10748 let encoded = encode_sequence(&source, q, 0.5).unwrap();10758 let encoded = encode_sequence(&source, q, 0.5).unwrap();
10749 assert_eq!(10759 assert_eq!(
crates/ec-av1/src/stream.rsmodified+29 -0
@@ -3033,6 +3033,35 @@ pub(crate) mod tests {
3033 let decoded = decode_stream(&encoded.stream).unwrap();3033 let decoded = decode_stream(&encoded.stream).unwrap();
3034 assert_eq!(decoded.len(), encoded.frames.len());3034 assert_eq!(decoded.len(), encoded.frames.len());
3035 for (i, (got, frame)) in decoded.iter().zip(&encoded.frames).enumerate() {3035 for (i, (got, frame)) in decoded.iter().zip(&encoded.frames).enumerate() {
3036+ for (name, a, b) in [
3037+ ("y", &got.y, &frame.reconstruction.y),
3038+ ("u", &got.u, &frame.reconstruction.u),
3039+ ("v", &got.v, &frame.reconstruction.v),
3040+ ] {
3041+ let w = if name == "y" { width } else { width.div_ceil(2) };
3042+ if let Some(k) = a.iter().zip(b.iter()).position(|(p, q)| p != q) {
3043+ eprintln!(
3044+ "DBG {width}x{height} frame {i} {name}: {} of {} differ, first at row {} col {} stride {w} ({}, {}), rows {:?} cols {:?}",
3045+ a.iter().zip(b.iter()).filter(|(p, q)| p != q).count(),
3046+ a.len(),
3047+ k / w,
3048+ k % w,
3049+ a[k],
3050+ b[k],
3051+ {
3052+ let mut v: Vec<usize> = a.iter().zip(b.iter()).enumerate().filter(|(_, (p, q))| p != q).map(|(j, _)| j / w).collect();
3053+ v.dedup();
3054+ v
3055+ },
3056+ {
3057+ let mut v: Vec<usize> = a.iter().zip(b.iter()).enumerate().filter(|(_, (p, q))| p != q).map(|(j, _)| j % w).collect();
3058+ v.sort_unstable();
3059+ v.dedup();
3060+ v
3061+ }
3062+ );
3063+ }
3064+ }
3036 assert_eq!(3065 assert_eq!(
3037 got.y, frame.reconstruction.y,3066 got.y, frame.reconstruction.y,
3038 "{width}x{height} frame {i} luma"3067 "{width}x{height} frame {i} luma"
crates/ec-av1/src/tile.rsmodified+26 -16
@@ -3981,20 +3981,28 @@ pub(crate) fn luma_32_coeff_bits(grid: &[i32]) -> f64 {
3981 /// tables), and against the contexts a block whose neighbours coded nothing3981 /// tables), and against the contexts a block whose neighbours coded nothing
3982 /// reads.3982 /// reads.
3983 ///3983 ///
3984-/// That last assumption -- `skip_ctx` 0, `sign_ctx` 0 -- is the largest3984+/// lane-av1skipctx CLOSED that row: `skip_ctx`/`sign_ctx` are the caller's
3985-/// remaining row of the census and lane-av1price2 did NOT close it: every3985+/// now, and [`crate::encode::CoefCtxMap`] -- a per-plane, per-4x4-cell map
3986-/// all-zero block is priced at the `txb_skip` cost of a block whose3986+/// committed in coding order and undone with a losing partition trial's
3987-/// neighbours coded nothing, which is the most expensive context there is, so3987+/// pixels -- is the per-trial neighbour state the previous lane said the
3988-/// an all-zero chroma block is UNDER-priced by 41-81% on film and 51-73% on3988+/// search did not have. The census (6 frames of each clip at 640x384, q=100,
3989-/// the screen capture (`pricer_error_census_on_clips`), and an all-zero3989+/// `pricer_error_census_on_clips`) moved:
3990-/// Luma4 is over-priced by 400%+. Arming the frame's real tables does not3990+///
3991-/// touch those rows (Chroma8 nz=0 reads -73.0% in both modes). Pricing them3991+/// | row | before | after |
3992-/// right needs the real neighbour context, which needs something the search3992+/// |---|---|---|
3993-/// does not have: a per-plane non-zero context map committed in coding order.3993+/// | screen total | +10.3% | +9.5% |
3994-/// The search ranks partition TREES, so it prices candidates whose neighbours3994+/// | screen Chroma8 nz=0 | -73.0% | -1.9% |
3995-/// are not yet decided; the map would have to be built per trial, not per3995+/// | screen Chroma16 nz=0 | -48% | -23.4% |
3996-/// block. Deferred with the numbers above rather than approximated by a3996+/// | screen Chroma4 nz=0 | -74% | -6.7% |
3997-/// fitted constant.3997+/// | screen Chroma8 nz=1 / nz=2 | +32.2% / +26.2% | +4.9% / +7.9% |
3998+/// | screen Luma4 nz=0 | +464% | +89.5% |
3999+/// | film total | +5.3% | +1.5% |
4000+///
4001+/// What is left in the all-zero rows is table ADAPTATION, not context: the
4002+/// price is taken against the frame's starting tables, and a frame whose
4003+/// blocks mostly code nothing narrows `txb_skip` far below where it started.
4004+/// A whole-block luma transform still reads context 0 -- that is what the
4005+/// writer codes (`write_luma_tus`, `n == 1`), not an approximation.
3998 pub(crate) fn coeff_bits(4006 pub(crate) fn coeff_bits(
3999 grid: &[i32],4007 grid: &[i32],
4000 set: TxbSet,4008 set: TxbSet,
@@ -4133,8 +4141,10 @@ thread_local! {
4133 /// luma price accurate while leaving the bigger chroma and all-zero errors4141 /// luma price accurate while leaving the bigger chroma and all-zero errors
4134 /// exactly where they were, which SKEWS the search's tradeoffs between them4142 /// exactly where they were, which SKEWS the search's tradeoffs between them
4135 /// instead of correcting them -- and the bytes get worse. The fix for that4143 /// instead of correcting them -- and the bytes get worse. The fix for that
4136-/// row is the neighbour skip context, not another table set (see4144+/// row was the neighbour skip context, not another table set, and
4137-/// [`coeff_bits`]).4145+/// lane-av1skipctx landed it (see [`coeff_bits`]): with the real contexts
4146+/// under every price the screen capture comes down +59.4 -> +58.3 vs libaom
4147+/// at native, still with every one of its frames armed with the defaults.
4138 pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>, screen: bool) {4148 pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>, screen: bool) {
4139 static MODE: std::sync::OnceLock<u8> = std::sync::OnceLock::new();4149 static MODE: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
4140 let mode = *MODE.get_or_init(|| match std::env::var("EC_AV1_PRICE_FRAME_CDFS").as_deref() {4150 let mode = *MODE.get_or_init(|| match std::env::var("EC_AV1_PRICE_FRAME_CDFS").as_deref() {