forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1price' (128ba78b): byte-exact pricer speed -- telescoped symbol cost, pricing coder without the carry state, interior context fast path, all-zero eob test

Browse files at this commit

e6e02af666d3fc45f0be30840a76791b8dd5d6dcAhmet Kaan GümüşParent: 3e400dccd32144
-5.3% instructions on the 1080p ladder, streams byte-identical (12/12 FNV
points on the three gate clips against its base; the byte pins from
lane-av1mv hold on the merged tree).
crates/ec-av1/src/msac.rsmodified+44 -11
@@ -47,11 +47,22 @@ pub struct SymbolEncoder {
4747 /// Flushed bytes before carry propagation: each entry can exceed `0xff` by
4848 /// the carry it owes the byte before it, which [`Self::finish`] adds in.
4949 precarry: Vec<u16>,
50- /// What every symbol written so far cost against the CDF it was written
51- /// with, in bits. The arithmetic coder spends `-log2(p)` on a symbol of
52- /// probability `p` to within the rounding of its interval, so this is what
53- /// a search may price a decision with without coding it twice.
54- bits: f64,
50+ /// `log2` of the interval width the bit account started from — the coder's
51+ /// width at [`Self::new`] or at the last [`Self::reset_bits`].
52+ bits_base: f64,
53+ /// How many bits the renormalisation has shifted `rng` up by since the
54+ /// account started. A symbol costs `log2(r) - log2(rng)` for the widths it
55+ /// narrows between, and the next symbol's `r` is this symbol's `rng`
56+ /// shifted left by the renormalisation, so the whole string's cost
57+ /// telescopes to `bits_base + shifts - log2(rng)` — one `log2` per price
58+ /// asked for, instead of one per symbol (libaom's `od_ec_enc_tell_frac`
59+ /// reads its account the same way).
60+ shifts: u32,
61+ /// Whether this coder only prices: the bit account depends on nothing but
62+ /// how far each symbol narrows `rng`, so a pricer keeps the interval width
63+ /// and drops the value bookkeeping (`low`, `cnt`, `precarry`) whose only
64+ /// consumer is [`Self::finish`].
65+ pricing: bool,
5566 }
5667
5768 impl Default for SymbolEncoder {
@@ -69,13 +80,25 @@ impl SymbolEncoder {
6980 rng: 0x8000,
7081 cnt: -9,
7182 precarry: Vec::new(),
72- bits: 0.0,
83+ bits_base: 15.0,
84+ shifts: 0,
85+ pricing: false,
86+ }
87+ }
88+
89+ /// A coder that only prices, for a search that wants [`Self::bits`] and
90+ /// will never ask for the bytes. Every symbol narrows `rng` exactly as it
91+ /// would in a writing coder, so the account is the same to the bit.
92+ pub fn pricer() -> SymbolEncoder {
93+ SymbolEncoder {
94+ pricing: true,
95+ ..SymbolEncoder::new()
7396 }
7497 }
7598
7699 /// What the symbols written so far cost, in bits.
77100 pub fn bits(&self) -> f64 {
78- self.bits
101+ self.bits_base + f64::from(self.shifts) - f64::from(self.rng).log2()
79102 }
80103
81104 /// The coder's current interval width — libaom's `od_ec_enc_rng` /
@@ -91,13 +114,15 @@ impl SymbolEncoder {
91114 /// holds one `u16` per byte already flushed. Pairs with the decoder's
92115 /// `aom_reader_tell`, which counts the same bits consumed.
93116 pub fn tell(&self) -> i32 {
117+ debug_assert!(!self.pricing, "a pricer keeps no byte position");
94118 self.cnt + 10 + self.precarry.len() as i32 * 8
95119 }
96120
97121 /// Forgets the cost accumulated so far, so that the next stretch of
98122 /// symbols can be priced on its own.
99123 pub fn reset_bits(&mut self) {
100- self.bits = 0.0;
124+ self.bits_base = f64::from(self.rng).log2();
125+ self.shifts = 0;
101126 }
102127
103128 /// Writes `symbol` against `cdf` and adapts `cdf` exactly as the decoder
@@ -145,9 +170,15 @@ impl SymbolEncoder {
145170 // which is not quite its nominal probability: the coder works the
146171 // range through an eight-bit multiply and hands every symbol above
147172 // this one a floor of `EC_MIN_PROB`. Pricing the narrowing rather than
148- // the table entry is what makes the account match the bytes.
149- self.bits -= (f64::from(rng) / f64::from(r)).log2();
150- self.normalize(low, rng);
173+ // the table entry is what makes the account match the bytes; the
174+ // narrowing itself is read off `rng` and `shifts` by [`Self::bits`].
175+ if self.pricing {
176+ let d = 16 - (32 - rng.leading_zeros());
177+ self.rng = rng << d;
178+ self.shifts += d;
179+ } else {
180+ self.normalize(low, rng);
181+ }
151182 }
152183
153184 /// Writes `bits` raw bits of `value`, most significant first — the `L(n)`
@@ -169,6 +200,7 @@ impl SymbolEncoder {
169200 /// inside it however the decoder pads the string, which is what lets a tile
170201 /// payload end on a byte boundary with no terminator.
171202 pub fn finish(mut self) -> Vec<u8> {
203+ assert!(!self.pricing, "a pricer holds no bytes to flush");
172204 // Round the interval's low end up to a value with 14 low zero bits,
173205 // then set bit 14: the result is inside the interval (the interval is
174206 // at least 2^15 wide before normalisation) and its tail bits are all
@@ -223,6 +255,7 @@ impl SymbolEncoder {
223255 self.low = low << d;
224256 self.rng = rng << d;
225257 self.cnt = s;
258+ self.shifts += d as u32;
226259 }
227260 }
228261
crates/ec-av1/src/tile.rsmodified+39 -14
@@ -2378,8 +2378,7 @@ pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet, q_ctx: usize) -> f64 {
23782378 TX16 => &SCANS[2],
23792379 _ => &SCANS[3],
23802380 };
2381- let mut enc = SymbolEncoder::new();
2382- enc.reset_bits();
2381+ let mut enc = SymbolEncoder::pricer();
23832382 write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None);
23842383 enc.bits()
23852384 })
@@ -2432,10 +2431,19 @@ fn write_coeffs(
24322431 ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell()));
24332432 }
24342433 let side = coding.side;
2435- let eob = scan
2436- .iter()
2437- .rposition(|&pos| grid[pos as usize] != 0)
2438- .map_or(0, |i| i + 1);
2434+ // The scan-order search reads the grid in scan order, one dependent load
2435+ // per position, and an all-zero block makes it walk every one of them --
2436+ // which the pricer does for candidate after candidate. A straight-line
2437+ // pass settles that case first: it vectorises, and a block that codes
2438+ // anything at all almost always codes its DC, so it stops at the first
2439+ // element.
2440+ let eob = if grid.iter().all(|&level| level == 0) {
2441+ 0
2442+ } else {
2443+ scan.iter()
2444+ .rposition(|&pos| grid[pos as usize] != 0)
2445+ .map_or(0, |i| i + 1)
2446+ };
24392447 enc.symbol(usize::from(eob == 0), &mut coding.txb_skip[skip_ctx]);
24402448 if plane.is_some() {
24412449 ec_rng_trace(|| {
@@ -2639,10 +2647,22 @@ fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
26392647 if row == 0 && col == 0 {
26402648 return 0;
26412649 }
2642- let mag: i32 = [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)]
2643- .iter()
2644- .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3))
2645- .sum();
2650+ // A coefficient two rows and two columns clear of the far edges reaches
2651+ // all five neighbours, so the gather is five loads off one index with no
2652+ // per-neighbour edge test -- which is where this writer spends a third of
2653+ // its coefficient loop.
2654+ let p = row * side + col;
2655+ let mag: i32 = if row + 2 < side && col + 2 < side {
2656+ [p + side, p + 1, p + side + 1, p + 2 * side, p + 2]
2657+ .iter()
2658+ .map(|&i| grid[i].abs().min(3))
2659+ .sum()
2660+ } else {
2661+ [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)]
2662+ .iter()
2663+ .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3))
2664+ .sum()
2665+ };
26462666 let offset = cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize;
26472667 (((mag + 1) >> 1).min(4) as usize) + offset
26482668 }
@@ -2651,10 +2671,15 @@ fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
26512671 /// of its three closest neighbours below and to the right, uncapped, and a
26522672 /// term separating the DC, the corner of the transform, and the rest.
26532673 fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
2654- let mag: i32 = [(1, 0), (0, 1), (1, 1)]
2655- .iter()
2656- .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs())
2657- .sum();
2674+ let p = row * side + col;
2675+ let mag: i32 = if row + 1 < side && col + 1 < side {
2676+ grid[p + side].abs() + grid[p + 1].abs() + grid[p + side + 1].abs()
2677+ } else {
2678+ [(1, 0), (0, 1), (1, 1)]
2679+ .iter()
2680+ .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs())
2681+ .sum()
2682+ };
26582683 let mag = (((mag + 1) >> 1).min(6)) as usize;
26592684 if row == 0 && col == 0 {
26602685 mag