forge
Explore
Log in

Don't you dare go Hollow

The RD pricer reads the real neighbour txb_skip/dc_sign contexts

Browse files at this commit

9afbc09b9d3759ba5b96fe39f65c3273a0db5237Ahmet Kaan GümüşParent: fbeac19
  • feat
    • the RD pricer reads the real neighbour txb_skip/dc_sign contexts
Show full message
feat(av1): the RD pricer reads the real neighbour txb_skip/dc_sign contexts

The search priced every transform block at txb_skip context 0 and dc_sign
context 0 -- exact for a luma transform covering its whole block, and the
largest remaining row of the pricer census everywhere else: an all-zero
chroma block was under-priced 41-81%, a one-coefficient Chroma8 over-priced
32%.

Each plane now carries a per-4x4-cell map of what the blocks committed
before it left behind (cul_level and DC sign, tile.rs neighbour_state),
written by Plane::commit and undone with the pixels by Plane::restore, so a
partition trial reads the parent's committed neighbours for its first child
and the just-committed sibling for the later ones -- coding order inside the
tree. Plane::coef_ctx mirrors the writer's own derivation: chroma reads
whether the above/left neighbours coded anything (write_block_planes), luma
context 0 for a whole-block transform and decode.rs SKIP_CONTEXTS for a
smaller unit (write_luma_tus / Neighbours::luma_skip_ctx), both planes the
neighbours' DC sign vote (dc_sign_ctx).

Census, 6 frames of each clip at 640x384 q=100, priced vs written:
  screen total +10.3% -> +9.5%, Chroma8 nz=0 -73.0% -> -1.9%,
  Chroma16 nz=0 -48% -> -23.4%, Chroma4 nz=0 -74% -> -6.7%,
  Chroma8 nz=1 +32.2% -> +4.9%, nz=2 +26.2% -> +7.9%,
  Luma4 nz=0 +464% -> +89.5%
  film total +5.3% -> +1.5%
crates/ec-av1/src/cdf_state.rsmodified+20 -0
@@ -300,6 +300,26 @@ pub(crate) struct TxbTables<'a> {
300300 pub tx_type: Option<&'a mut [u16]>,
301301 }
302302
303+impl TxbSet {
304+ /// Whether this set codes a CHROMA plane's transform, which is what
305+ /// picks the `txb_skip` context rule the writer uses: chroma reads
306+ /// "did the above/left neighbour code anything" (`write_block_planes`),
307+ /// luma reads context 0 for a transform covering its whole block and
308+ /// `SKIP_CONTEXTS` for a smaller unit (`write_luma_tus`).
309+ pub(crate) fn is_chroma(self) -> bool {
310+ matches!(
311+ self,
312+ Self::Chroma4
313+ | Self::Chroma8
314+ | Self::Chroma16
315+ | Self::Chroma32
316+ | Self::ChromaRect8x4
317+ | Self::ChromaRect16x8
318+ | Self::ChromaRect32x16
319+ )
320+ }
321+}
322+
303323 /// Every table a key frame's tile writer adapts.
304324 #[derive(Clone)]
305325 pub(crate) struct Cdfs {
crates/ec-av1/src/encode.rsmodified+205 -10
@@ -1180,6 +1180,45 @@ fn too_large() -> Error {
11801180
11811181 /// One plane of the picture being coded, and the reconstruction being built
11821182 /// beside it.
1183+/// What one committed 4x4 cell left behind for the coefficient contexts of
1184+/// the blocks that read it as a neighbour -- tile.rs `neighbour_state`, which
1185+/// is the same state decode.rs records.
1186+#[derive(Clone, Copy, Default, PartialEq, Eq)]
1187+struct CoefCtx {
1188+ /// spec `cul_level`: the sum of the unit's coded magnitudes, clamped to 7.
1189+ level: u8,
1190+ /// The DC sign this cell votes with: `-1` negative, `1` positive, `0` when
1191+ /// the DC itself was zero (tile.rs `dc_vote`).
1192+ dc: i8,
1193+}
1194+
1195+/// One plane's coefficient contexts, committed in CODING ORDER: the search
1196+/// prices partition TREES, so the neighbour state a candidate reads has to be
1197+/// whatever the blocks coded before it left -- the parent's committed
1198+/// neighbours for a tree's first child, the just-committed sibling for the
1199+/// later ones. Keeping the map as a full per-4x4-cell grid rather than the
1200+/// writer's two running bands is what makes that fall out: a trial commits
1201+/// over its own rectangle ([`Plane::commit`]) and a losing trial is undone
1202+/// with the pixels ([`Plane::restore`]), so what a candidate reads above and
1203+/// to its left is exactly what the tile writer's `Neighbours` bands will hold
1204+/// when it gets there.
1205+///
1206+/// Before this map the pricer took both contexts as zero, which is exact for
1207+/// a luma transform covering its whole block and the largest remaining row of
1208+/// the pricer census everywhere else (all-zero chroma under-priced 41-81%).
1209+#[derive(Default)]
1210+struct CoefCtxMap {
1211+ cells: Vec<CoefCtx>,
1212+ stride: usize,
1213+ /// Whether the transforms being priced right now are SMALLER than the
1214+ /// block they sit in -- a `TxMode::Select` depth split
1215+ /// ([`Plane::code_tx_depth`]) or an inter var-tx split
1216+ /// ([`commit_inter_luma`]). That is the one thing the (x, y, side) of a
1217+ /// trial cannot tell, and it is what decides whether luma reads context 0
1218+ /// or `SKIP_CONTEXTS` (tile.rs `write_luma_tus`).
1219+ tu_split: bool,
1220+}
1221+
11831222 struct Plane<'a> {
11841223 source: &'a [u8],
11851224 reconstruction: Vec<u8>,
@@ -1207,6 +1246,11 @@ struct Plane<'a> {
12071246 tile_y0: usize,
12081247 tile_x1: usize,
12091248 tile_y1: usize,
1249+ /// [`CoefCtxMap`]: what the blocks already committed left for the
1250+ /// coefficient contexts of the ones still being priced. Sized on the
1251+ /// first commit, so a plane literal does not have to know its own extent
1252+ /// twice.
1253+ ctx: CoefCtxMap,
12101254 }
12111255
12121256 impl Plane<'_> {
@@ -1402,7 +1446,16 @@ impl Plane<'_> {
14021446 .map(|&level| 2.0 + 2.0 * f64::from(level.unsigned_abs() + 1).log2())
14031447 .sum()
14041448 } else {
1405- crate::tile::coeff_bits(&levels, at.set, crate::decode::q_ctx_of(base_q_idx))
1449+ {
1450+ let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, at.set);
1451+ crate::tile::coeff_bits(
1452+ &levels,
1453+ at.set,
1454+ crate::decode::q_ctx_of(base_q_idx),
1455+ skip_ctx,
1456+ sign_ctx,
1457+ )
1458+ }
14061459 };
14071460 #[cfg(test)]
14081461 stage_add(3, t.elapsed());
@@ -1471,7 +1524,16 @@ impl Plane<'_> {
14711524 .map(|&level| 2.0 + 2.0 * f64::from(level.unsigned_abs() + 1).log2())
14721525 .sum()
14731526 } else {
1474- crate::tile::coeff_bits(&levels, set, crate::decode::q_ctx_of(base_q_idx))
1527+ {
1528+ let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, set);
1529+ crate::tile::coeff_bits(
1530+ &levels,
1531+ set,
1532+ crate::decode::q_ctx_of(base_q_idx),
1533+ skip_ctx,
1534+ sign_ctx,
1535+ )
1536+ }
14751537 };
14761538 #[cfg(test)]
14771539 stage_add(3, t.elapsed());
@@ -1509,6 +1571,9 @@ impl Plane<'_> {
15091571 };
15101572 let mut levels = vec![0i32; side * side];
15111573 census_add(4, n * n);
1574+ // A unit smaller than its block reads the neighbour magnitude table
1575+ // for its `txb_skip` context, not context 0 ([`Plane::coef_ctx`]).
1576+ self.ctx.tu_split = n > 1;
15121577 let (mut sse, mut bits) = (0.0, 0.0);
15131578 for tu_row in 0..n {
15141579 for tu_col in 0..n {
@@ -1530,15 +1595,100 @@ impl Plane<'_> {
15301595 }
15311596 }
15321597 }
1598+ self.ctx.tu_split = false;
15331599 (levels, sse, bits)
15341600 }
15351601
1536- /// Writes a trial's reconstruction back into the plane.
1602+ /// Writes a trial's reconstruction back into the plane, and the
1603+ /// coefficient context it leaves for its neighbours ([`CoefCtxMap`]).
15371604 fn commit(&mut self, x: usize, y: usize, side: usize, trial: &Trial) {
15381605 for row in 0..side {
15391606 self.reconstruction[(y + row) * self.width + x..][..side]
15401607 .copy_from_slice(&trial.reconstruction[row * side..][..side]);
15411608 }
1609+ self.commit_ctx(x, y, side, &trial.levels);
1610+ }
1611+
1612+ /// Publishes one committed transform's own context over every 4x4 cell it
1613+ /// covers -- tile.rs `neighbour_state` + `Neighbours::record_planes`,
1614+ /// which is what the writer will read when it reaches the next block.
1615+ fn commit_ctx(&mut self, x: usize, y: usize, side: usize, levels: &[i32]) {
1616+ if self.ctx.cells.is_empty() {
1617+ self.ctx.stride = self.width.div_ceil(4);
1618+ self.ctx.cells = vec![CoefCtx::default(); self.ctx.stride * self.height.div_ceil(4)];
1619+ }
1620+ let cul = levels.iter().map(|l| l.unsigned_abs()).sum::<u32>().min(7) as u8;
1621+ let state = CoefCtx {
1622+ level: cul,
1623+ dc: match levels[0].signum() {
1624+ 0 => 0,
1625+ -1 => -1,
1626+ _ => 1,
1627+ },
1628+ };
1629+ let n = (side / 4).max(1);
1630+ for row in 0..n {
1631+ let start = (y / 4 + row) * self.ctx.stride + x / 4;
1632+ let end = (start + n).min(self.ctx.cells.len());
1633+ if start < end {
1634+ self.ctx.cells[start..end].fill(state);
1635+ }
1636+ }
1637+ }
1638+
1639+ /// A block committed without any residual at all ([`ibc_commit`]): the
1640+ /// cells it covers coded nothing, and leaving an earlier trial's state
1641+ /// there would price its neighbours off a block that was never coded.
1642+ fn commit_ctx_zero(&mut self, x: usize, y: usize, side: usize) {
1643+ self.commit_ctx(x, y, side, &[0i32]);
1644+ }
1645+
1646+ /// The `txb_skip` and `dc_sign` contexts a transform at `(x, y)` of this
1647+ /// plane reads, off what the blocks before it in coding order committed
1648+ /// ([`CoefCtxMap`]) -- the writer's own derivation: chroma reads whether
1649+ /// the above/left neighbours coded anything (tile.rs
1650+ /// `write_block_planes`), luma reads context 0 when its transform covers
1651+ /// its whole block and decode.rs `SKIP_CONTEXTS` off the neighbour
1652+ /// magnitude tiers when it does not (tile.rs `write_luma_tus` /
1653+ /// `Neighbours::luma_skip_ctx`), and both planes read the DC sign vote of
1654+ /// the same neighbours (tile.rs `dc_sign_ctx`).
1655+ ///
1656+ /// A neighbour outside this plane's own TILE does not exist, exactly as
1657+ /// the writer's per-tile `Neighbours` bands start blank.
1658+ fn coef_ctx(&self, x: usize, y: usize, side: usize, set: TxbSet) -> (usize, usize) {
1659+ if self.ctx.cells.is_empty() {
1660+ return (0, 0);
1661+ }
1662+ let n = (side / 4).max(1);
1663+ let (cx, cy) = (x / 4, y / 4);
1664+ let (mut top, mut left) = (0u8, 0u8);
1665+ let mut vote = 0i32;
1666+ if y > self.tile_y0 && cy > 0 {
1667+ for cell in &self.ctx.cells[(cy - 1) * self.ctx.stride + cx..][..n] {
1668+ top |= cell.level;
1669+ vote += i32::from(cell.dc);
1670+ }
1671+ }
1672+ if x > self.tile_x0 && cx > 0 {
1673+ for row in 0..n {
1674+ let cell = self.ctx.cells[(cy + row) * self.ctx.stride + cx - 1];
1675+ left |= cell.level;
1676+ vote += i32::from(cell.dc);
1677+ }
1678+ }
1679+ let skip_ctx = if set.is_chroma() {
1680+ usize::from(top != 0) + usize::from(left != 0)
1681+ } else if self.ctx.tu_split {
1682+ crate::decode::SKIP_CONTEXTS[usize::from(top).min(4)][usize::from(left).min(4)]
1683+ } else {
1684+ 0
1685+ };
1686+ let sign_ctx = match vote.signum() {
1687+ 0 => 0,
1688+ -1 => 1,
1689+ _ => 2,
1690+ };
1691+ (skip_ctx, sign_ctx)
15421692 }
15431693
15441694 /// The source samples of one square, contiguous — what a motion search
@@ -1551,15 +1701,46 @@ impl Plane<'_> {
15511701
15521702 /// The reconstructed samples of one square, so that a partition trial can
15531703 /// be undone.
1554- fn snapshot(&self, x: usize, y: usize, side: usize) -> Vec<u8> {
1555- rows_of(&self.reconstruction, self.width, x, y, side)
1704+ /// The reconstructed samples of one square AND the coefficient contexts
1705+ /// its cells hold, so that a partition trial can be undone whole: a
1706+ /// losing trial that left its own nz/DC state behind would price the
1707+ /// blocks after it off a block the tile writer never codes.
1708+ fn snapshot(&self, x: usize, y: usize, side: usize) -> (Vec<u8>, Vec<CoefCtx>) {
1709+ (
1710+ rows_of(&self.reconstruction, self.width, x, y, side),
1711+ self.ctx_rows(x, y, side),
1712+ )
1713+ }
1714+
1715+ fn ctx_rows(&self, x: usize, y: usize, side: usize) -> Vec<CoefCtx> {
1716+ if self.ctx.cells.is_empty() {
1717+ return Vec::new();
1718+ }
1719+ let n = (side / 4).max(1);
1720+ (0..n)
1721+ .flat_map(|row| {
1722+ let start = (y / 4 + row) * self.ctx.stride + x / 4;
1723+ self.ctx.cells[start..(start + n).min(self.ctx.cells.len())].to_vec()
1724+ })
1725+ .collect()
15561726 }
15571727
15581728 /// Puts a snapshot back.
1559- fn restore(&mut self, x: usize, y: usize, side: usize, samples: &[u8]) {
1729+ fn restore(&mut self, x: usize, y: usize, side: usize, saved: &(Vec<u8>, Vec<CoefCtx>)) {
15601730 for row in 0..side {
15611731 self.reconstruction[(y + row) * self.width + x..][..side]
1562- .copy_from_slice(&samples[row * side..][..side]);
1732+ .copy_from_slice(&saved.0[row * side..][..side]);
1733+ }
1734+ if saved.1.is_empty() {
1735+ return;
1736+ }
1737+ let n = (side / 4).max(1);
1738+ for row in 0..n {
1739+ let start = (y / 4 + row) * self.ctx.stride + x / 4;
1740+ let end = (start + n).min(self.ctx.cells.len());
1741+ if start < end {
1742+ self.ctx.cells[start..end].copy_from_slice(&saved.1[row * n..][..end - start]);
1743+ }
15631744 }
15641745 }
15651746
@@ -2381,6 +2562,10 @@ fn commit_inter_luma(
23812562 let mut levels = vec![0i32; side * side];
23822563 let (mut sse, mut bits) = (0.0, 0.0);
23832564 let mut units = Vec::with_capacity(4);
2565+ // The split units are smaller than their block, so their `txb_skip`
2566+ // context is the neighbour magnitude table ([`Plane::coef_ctx`]) -- and
2567+ // each unit's own commit publishes what the next one reads, coding order.
2568+ luma.ctx.tu_split = true;
23842569 for tu_row in 0..2 {
23852570 for tu_col in 0..2 {
23862571 let (tu_x, tu_y) = (x + tu_col * tx, y + tu_row * tx);
@@ -2397,6 +2582,7 @@ fn commit_inter_luma(
23972582 units.push((tu_x, tu_y, trial));
23982583 }
23992584 }
2585+ luma.ctx.tu_split = false;
24002586 let split_cost = sse + search.lambda * (bits + txfm_split_bits(side, 1));
24012587 let flat_cost = flat.sse + search.lambda * (flat.bits + txfm_split_bits(side, 0));
24022588 if split_cost < flat_cost {
@@ -2874,7 +3060,9 @@ fn ibc_commit(
28743060 let copied: Vec<u8> = plane.reconstruction[from..from + half].to_vec();
28753061 plane.reconstruction[to..to + half].copy_from_slice(&copied);
28763062 }
3063+ plane.commit_ctx_zero(x / 2, y / 2, half);
28773064 }
3065+ luma.commit_ctx_zero(x, y, side);
28783066 }
28793067
28803068 /// Searches this block's own DV: every hash-table position whose 16x16 luma
@@ -3972,7 +4160,7 @@ fn snapshot(
39724160 chroma: &[Plane; 2],
39734161 (x, y): (usize, usize),
39744162 side: usize,
3975-) -> [Vec<u8>; 3] {
4163+) -> [(Vec<u8>, Vec<CoefCtx>); 3] {
39764164 [
39774165 luma.snapshot(x, y, side),
39784166 chroma[0].snapshot(x / 2, y / 2, side / 2),
@@ -3986,7 +4174,7 @@ fn restore(
39864174 chroma: &mut [Plane; 2],
39874175 (x, y): (usize, usize),
39884176 side: usize,
3989- saved: &[Vec<u8>; 3],
4177+ saved: &[(Vec<u8>, Vec<CoefCtx>); 3],
39904178 ) {
39914179 luma.restore(x, y, side, &saved[0]);
39924180 chroma[0].restore(x / 2, y / 2, side / 2, &saved[1]);
@@ -4333,6 +4521,7 @@ fn fresh_plane(source: &[u8], width: usize, height: usize, true_width: usize, tr
43334521 tile_y0: 0,
43344522 tile_x1: width,
43354523 tile_y1: height,
4524+ ctx: CoefCtxMap::default(),
43364525 }
43374526 }
43384527
@@ -4604,6 +4793,7 @@ pub(crate) fn encode_key_frame_inner(
46044793 tile_y0: 0,
46054794 tile_x1: picture.width,
46064795 tile_y1: picture.height,
4796+ ctx: CoefCtxMap::default(),
46074797 };
46084798 let mut chroma = [
46094799 Plane {
@@ -4617,6 +4807,7 @@ pub(crate) fn encode_key_frame_inner(
46174807 tile_y0: 0,
46184808 tile_x1: picture.width / 2,
46194809 tile_y1: picture.height / 2,
4810+ ctx: CoefCtxMap::default(),
46204811 },
46214812 Plane {
46224813 source: &picture_v8,
@@ -4629,6 +4820,7 @@ pub(crate) fn encode_key_frame_inner(
46294820 tile_y0: 0,
46304821 tile_x1: picture.width / 2,
46314822 tile_y1: picture.height / 2,
4823+ ctx: CoefCtxMap::default(),
46324824 },
46334825 ];
46344826
@@ -6447,6 +6639,7 @@ pub(crate) fn encode_inter_frame(
64476639 tile_y0: 0,
64486640 tile_x1: picture.width,
64496641 tile_y1: picture.height,
6642+ ctx: CoefCtxMap::default(),
64506643 };
64516644 let mut chroma = [
64526645 Plane {
@@ -6460,6 +6653,7 @@ pub(crate) fn encode_inter_frame(
64606653 tile_y0: 0,
64616654 tile_x1: picture.width / 2,
64626655 tile_y1: picture.height / 2,
6656+ ctx: CoefCtxMap::default(),
64636657 },
64646658 Plane {
64656659 source: &picture_v8,
@@ -6472,6 +6666,7 @@ pub(crate) fn encode_inter_frame(
64726666 tile_y0: 0,
64736667 tile_x1: picture.width / 2,
64746668 tile_y1: picture.height / 2,
6669+ ctx: CoefCtxMap::default(),
64756670 },
64766671 ];
64776672
@@ -9739,7 +9934,7 @@ mod tests {
97399934 };
97409935 let t = Instant::now();
97419936 for _ in 0..N {
9742- std::hint::black_box(crate::tile::coeff_bits(&levels, set, 2));
9937+ std::hint::black_box(crate::tile::coeff_bits(&levels, set, 2, 0, 0));
97439938 }
97449939 let entropy_t = t.elapsed() / N;
97459940
crates/ec-av1/src/tile.rsmodified+50 -13
@@ -3530,7 +3530,14 @@ fn write_block_planes(
35303530 Some(plane),
35313531 );
35323532 #[cfg(test)]
3533- census_record(planes[plane], q_ctx, grid, f64::from(enc.tell() - before));
3533+ census_record(
3534+ planes[plane],
3535+ q_ctx,
3536+ grid,
3537+ f64::from(enc.tell() - before),
3538+ skip_ctx,
3539+ dc_sign_ctx(around[plane].dc_vote),
3540+ );
35343541 ec_rng_trace(|| {
35353542 format!(
35363543 "EC_PLANE plane={plane} nz={} skip_ctx={skip_ctx} tell={}",
@@ -3625,7 +3632,14 @@ fn write_luma_tus(
36253632 Some(0),
36263633 );
36273634 #[cfg(test)]
3628- census_record(set, q_ctx, &unit, f64::from(enc.tell() - before));
3635+ census_record(
3636+ set,
3637+ q_ctx,
3638+ &unit,
3639+ f64::from(enc.tell() - before),
3640+ skip_ctx,
3641+ dc_sign_ctx(around.dc_vote),
3642+ );
36293643 if n > 1 {
36303644 neighbours.record_mi_luma(tu_mi, tx, &unit);
36313645 }
@@ -3906,9 +3920,9 @@ pub(crate) fn predicted_coeff_bits_sb(superblocks: &[Superblock], base_q_idx: u8
39063920 // top-left 32x32 carries coefficients) and each chroma plane at
39073921 // 32x32; `predicted_coeff_bits` has no 64 arm, so price it here.
39083922 Superblock::Whole(block) if !block.skip => {
3909- coeff_bits(&dense(&block.luma, 32), TxbSet::Luma64, q_ctx)
3910- + coeff_bits(&dense(&block.u, 32), TxbSet::Chroma32, q_ctx)
3911- + coeff_bits(&dense(&block.v, 32), TxbSet::Chroma32, q_ctx)
3923+ coeff_bits(&dense(&block.luma, 32), TxbSet::Luma64, q_ctx, 0, 0)
3924+ + coeff_bits(&dense(&block.u, 32), TxbSet::Chroma32, q_ctx, 0, 0)
3925+ + coeff_bits(&dense(&block.v, 32), TxbSet::Chroma32, q_ctx, 0, 0)
39123926 }
39133927 Superblock::Whole(_) => 0.0,
39143928 Superblock::Split(quadrants) => predicted_coeff_bits(quadrants, base_q_idx),
@@ -3935,9 +3949,9 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 {
39353949 (_, false) => (TxbSet::Luma8, TxbSet::Chroma4),
39363950 (_, true) => (TxbSet::Luma8Inter, TxbSet::Chroma4),
39373951 };
3938- coeff_bits(&dense(&block.luma, side), luma, q_ctx)
3939- + coeff_bits(&dense(&block.u, side / 2), chroma, q_ctx)
3940- + coeff_bits(&dense(&block.v, side / 2), chroma, q_ctx)
3952+ coeff_bits(&dense(&block.luma, side), luma, q_ctx, 0, 0)
3953+ + coeff_bits(&dense(&block.u, side / 2), chroma, q_ctx, 0, 0)
3954+ + coeff_bits(&dense(&block.v, side / 2), chroma, q_ctx, 0, 0)
39413955 }
39423956 blocks
39433957 .iter()
@@ -3953,7 +3967,7 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 {
39533967
39543968 #[cfg(test)]
39553969 pub(crate) fn luma_32_coeff_bits(grid: &[i32]) -> f64 {
3956- coeff_bits(grid, TxbSet::Luma32, 2)
3970+ coeff_bits(grid, TxbSet::Luma32, 2, 0, 0)
39573971 }
39583972
39593973 /// What one transform block's levels cost through the CDFs of `set`, in bits.
@@ -3981,7 +3995,19 @@ pub(crate) fn luma_32_coeff_bits(grid: &[i32]) -> f64 {
39813995 /// are not yet decided; the map would have to be built per trial, not per
39823996 /// block. Deferred with the numbers above rather than approximated by a
39833997 /// fitted constant.
3984-pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet, q_ctx: usize) -> f64 {
3998+pub(crate) fn coeff_bits(
3999+ grid: &[i32],
4000+ set: TxbSet,
4001+ q_ctx: usize,
4002+ // lane-av1skipctx: the two neighbour-derived contexts, mirrored from the
4003+ // writer's own (`write_block_planes`/`write_luma_tus`) off the search's
4004+ // coding-order-committed map ([`crate::encode::CoefCtxMap`]). `(0, 0)` is
4005+ // what every caller passed before the map existed -- exact for a luma
4006+ // transform covering its whole block, and the census row that was the
4007+ // largest remaining pricer error for every other block.
4008+ skip_ctx: usize,
4009+ sign_ctx: usize,
4010+) -> f64 {
39854011 /// Built once per size: the search prices thirteen modes for every block,
39864012 /// and the scan is the same table every time.
39874013 static SCANS: LazyLock<[Vec<u16>; 4]> = LazyLock::new(|| {
@@ -4031,7 +4057,7 @@ pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet, q_ctx: usize) -> f64 {
40314057 _ => &SCANS[3],
40324058 };
40334059 let mut enc = SymbolEncoder::pricer();
4034- write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None);
4060+ write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None);
40354061 enc.bits()
40364062 };
40374063 PRICING_BASE.with_borrow_mut(|base| {
@@ -4182,7 +4208,18 @@ pub(crate) fn census_bucket_label(bucket: usize) -> &'static str {
41824208 }
41834209
41844210 #[cfg(test)]
4185-fn census_record(set: TxbSet, q_ctx: usize, grid: &[i32], written: f64) {
4211+fn census_record(
4212+ set: TxbSet,
4213+ q_ctx: usize,
4214+ grid: &[i32],
4215+ written: f64,
4216+ // The writer's own neighbour contexts for this very transform block: the
4217+ // census prices the search's estimate the way the search now takes it
4218+ // (lane-av1skipctx), so a row's error is the table/adaptation drift that
4219+ // is left once the context is right.
4220+ skip_ctx: usize,
4221+ sign_ctx: usize,
4222+) {
41864223 if CENSUS.with_borrow(|c| c.is_none()) {
41874224 return;
41884225 }
@@ -4196,7 +4233,7 @@ fn census_record(set: TxbSet, q_ctx: usize, grid: &[i32], written: f64) {
41964233 9..=16 => 5,
41974234 _ => 6,
41984235 };
4199- let priced = coeff_bits(grid, set, q_ctx);
4236+ let priced = coeff_bits(grid, set, q_ctx, skip_ctx, sign_ctx);
42004237 CENSUS.with_borrow_mut(|rows| {
42014238 let rows = rows.as_mut().expect("the census is on");
42024239 let row = rows.entry((format!("{set:?}"), bucket)).or_default();