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 {
47 /// Flushed bytes before carry propagation: each entry can exceed `0xff` by47 /// Flushed bytes before carry propagation: each entry can exceed `0xff` by
48 /// the carry it owes the byte before it, which [`Self::finish`] adds in.48 /// the carry it owes the byte before it, which [`Self::finish`] adds in.
49 precarry: Vec<u16>,49 precarry: Vec<u16>,
50- /// What every symbol written so far cost against the CDF it was written50+ /// `log2` of the interval width the bit account started from — the coder's
51- /// with, in bits. The arithmetic coder spends `-log2(p)` on a symbol of51+ /// width at [`Self::new`] or at the last [`Self::reset_bits`].
52- /// probability `p` to within the rounding of its interval, so this is what52+ bits_base: f64,
53- /// a search may price a decision with without coding it twice.53+ /// How many bits the renormalisation has shifted `rng` up by since the
54- bits: f64,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,
55 }66 }
56 67
57 impl Default for SymbolEncoder {68 impl Default for SymbolEncoder {
@@ -69,13 +80,25 @@ impl SymbolEncoder {
69 rng: 0x8000,80 rng: 0x8000,
70 cnt: -9,81 cnt: -9,
71 precarry: Vec::new(),82 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()
73 }96 }
74 }97 }
75 98
76 /// What the symbols written so far cost, in bits.99 /// What the symbols written so far cost, in bits.
77 pub fn bits(&self) -> f64 {100 pub fn bits(&self) -> f64 {
78- self.bits101+ self.bits_base + f64::from(self.shifts) - f64::from(self.rng).log2()
79 }102 }
80 103
81 /// The coder's current interval width — libaom's `od_ec_enc_rng` /104 /// The coder's current interval width — libaom's `od_ec_enc_rng` /
@@ -91,13 +114,15 @@ impl SymbolEncoder {
91 /// holds one `u16` per byte already flushed. Pairs with the decoder's114 /// holds one `u16` per byte already flushed. Pairs with the decoder's
92 /// `aom_reader_tell`, which counts the same bits consumed.115 /// `aom_reader_tell`, which counts the same bits consumed.
93 pub fn tell(&self) -> i32 {116 pub fn tell(&self) -> i32 {
117+ debug_assert!(!self.pricing, "a pricer keeps no byte position");
94 self.cnt + 10 + self.precarry.len() as i32 * 8118 self.cnt + 10 + self.precarry.len() as i32 * 8
95 }119 }
96 120
97 /// Forgets the cost accumulated so far, so that the next stretch of121 /// Forgets the cost accumulated so far, so that the next stretch of
98 /// symbols can be priced on its own.122 /// symbols can be priced on its own.
99 pub fn reset_bits(&mut self) {123 pub fn reset_bits(&mut self) {
100- self.bits = 0.0;124+ self.bits_base = f64::from(self.rng).log2();
125+ self.shifts = 0;
101 }126 }
102 127
103 /// Writes `symbol` against `cdf` and adapts `cdf` exactly as the decoder128 /// Writes `symbol` against `cdf` and adapts `cdf` exactly as the decoder
@@ -145,9 +170,15 @@ impl SymbolEncoder {
145 // which is not quite its nominal probability: the coder works the170 // which is not quite its nominal probability: the coder works the
146 // range through an eight-bit multiply and hands every symbol above171 // range through an eight-bit multiply and hands every symbol above
147 // this one a floor of `EC_MIN_PROB`. Pricing the narrowing rather than172 // 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.173+ // the table entry is what makes the account match the bytes; the
149- self.bits -= (f64::from(rng) / f64::from(r)).log2();174+ // narrowing itself is read off `rng` and `shifts` by [`Self::bits`].
150- self.normalize(low, rng);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+ }
151 }182 }
152 183
153 /// Writes `bits` raw bits of `value`, most significant first — the `L(n)`184 /// Writes `bits` raw bits of `value`, most significant first — the `L(n)`
@@ -169,6 +200,7 @@ impl SymbolEncoder {
169 /// inside it however the decoder pads the string, which is what lets a tile200 /// inside it however the decoder pads the string, which is what lets a tile
170 /// payload end on a byte boundary with no terminator.201 /// payload end on a byte boundary with no terminator.
171 pub fn finish(mut self) -> Vec<u8> {202 pub fn finish(mut self) -> Vec<u8> {
203+ assert!(!self.pricing, "a pricer holds no bytes to flush");
172 // Round the interval's low end up to a value with 14 low zero bits,204 // Round the interval's low end up to a value with 14 low zero bits,
173 // then set bit 14: the result is inside the interval (the interval is205 // then set bit 14: the result is inside the interval (the interval is
174 // at least 2^15 wide before normalisation) and its tail bits are all206 // at least 2^15 wide before normalisation) and its tail bits are all
@@ -223,6 +255,7 @@ impl SymbolEncoder {
223 self.low = low << d;255 self.low = low << d;
224 self.rng = rng << d;256 self.rng = rng << d;
225 self.cnt = s;257 self.cnt = s;
258+ self.shifts += d as u32;
226 }259 }
227 }260 }
228 261
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 {
2378 TX16 => &SCANS[2],2378 TX16 => &SCANS[2],
2379 _ => &SCANS[3],2379 _ => &SCANS[3],
2380 };2380 };
2381- let mut enc = SymbolEncoder::new();2381+ let mut enc = SymbolEncoder::pricer();
2382- enc.reset_bits();
2383 write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None);2382 write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None);
2384 enc.bits()2383 enc.bits()
2385 })2384 })
@@ -2432,10 +2431,19 @@ fn write_coeffs(
2432 ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell()));2431 ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell()));
2433 }2432 }
2434 let side = coding.side;2433 let side = coding.side;
2435- let eob = scan2434+ // The scan-order search reads the grid in scan order, one dependent load
2436- .iter()2435+ // per position, and an all-zero block makes it walk every one of them --
2437- .rposition(|&pos| grid[pos as usize] != 0)2436+ // which the pricer does for candidate after candidate. A straight-line
2438- .map_or(0, |i| i + 1);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+ };
2439 enc.symbol(usize::from(eob == 0), &mut coding.txb_skip[skip_ctx]);2447 enc.symbol(usize::from(eob == 0), &mut coding.txb_skip[skip_ctx]);
2440 if plane.is_some() {2448 if plane.is_some() {
2441 ec_rng_trace(|| {2449 ec_rng_trace(|| {
@@ -2639,10 +2647,22 @@ fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
2639 if row == 0 && col == 0 {2647 if row == 0 && col == 0 {
2640 return 0;2648 return 0;
2641 }2649 }
2642- let mag: i32 = [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)]2650+ // A coefficient two rows and two columns clear of the far edges reaches
2643- .iter()2651+ // all five neighbours, so the gather is five loads off one index with no
2644- .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3))2652+ // per-neighbour edge test -- which is where this writer spends a third of
2645- .sum();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+ };
2646 let offset = cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize;2666 let offset = cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize;
2647 (((mag + 1) >> 1).min(4) as usize) + offset2667 (((mag + 1) >> 1).min(4) as usize) + offset
2648 }2668 }
@@ -2651,10 +2671,15 @@ fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
2651 /// of its three closest neighbours below and to the right, uncapped, and a2671 /// of its three closest neighbours below and to the right, uncapped, and a
2652 /// term separating the DC, the corner of the transform, and the rest.2672 /// term separating the DC, the corner of the transform, and the rest.
2653 fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {2673 fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize {
2654- let mag: i32 = [(1, 0), (0, 1), (1, 1)]2674+ let p = row * side + col;
2655- .iter()2675+ let mag: i32 = if row + 1 < side && col + 1 < side {
2656- .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs())2676+ grid[p + side].abs() + grid[p + 1].abs() + grid[p + side + 1].abs()
2657- .sum();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+ };
2658 let mag = (((mag + 1) >> 1).min(6)) as usize;2683 let mag = (((mag + 1) >> 1).min(6)) as usize;
2659 if row == 0 && col == 0 {2684 if row == 0 && col == 0 {
2660 mag2685 mag