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 commit2 changed files +83 -25
- crates/ec-av1/src/msac.rs +44 -11
- crates/ec-av1/src/tile.rs +39 -14
crates/ec-av1/src/msac.rsmodified+44 -11
| @@ -47,11 +47,22 @@ pub struct SymbolEncoder { | ||
| 47 | 47 | /// Flushed bytes before carry propagation: each entry can exceed `0xff` by |
| 48 | 48 | /// the carry it owes the byte before it, which [`Self::finish`] adds in. |
| 49 | 49 | 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, | |
| 55 | 66 | } |
| 56 | 67 | |
| 57 | 68 | impl Default for SymbolEncoder { |
| @@ -69,13 +80,25 @@ impl SymbolEncoder { | ||
| 69 | 80 | rng: 0x8000, |
| 70 | 81 | cnt: -9, |
| 71 | 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 | 99 | /// What the symbols written so far cost, in bits. |
| 77 | 100 | pub fn bits(&self) -> f64 { |
| 78 | - self.bits | |
| 101 | + self.bits_base + f64::from(self.shifts) - f64::from(self.rng).log2() | |
| 79 | 102 | } |
| 80 | 103 | |
| 81 | 104 | /// The coder's current interval width — libaom's `od_ec_enc_rng` / |
| @@ -91,13 +114,15 @@ impl SymbolEncoder { | ||
| 91 | 114 | /// holds one `u16` per byte already flushed. Pairs with the decoder's |
| 92 | 115 | /// `aom_reader_tell`, which counts the same bits consumed. |
| 93 | 116 | pub fn tell(&self) -> i32 { |
| 117 | + debug_assert!(!self.pricing, "a pricer keeps no byte position"); | |
| 94 | 118 | self.cnt + 10 + self.precarry.len() as i32 * 8 |
| 95 | 119 | } |
| 96 | 120 | |
| 97 | 121 | /// Forgets the cost accumulated so far, so that the next stretch of |
| 98 | 122 | /// symbols can be priced on its own. |
| 99 | 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 | 128 | /// Writes `symbol` against `cdf` and adapts `cdf` exactly as the decoder |
| @@ -145,9 +170,15 @@ impl SymbolEncoder { | ||
| 145 | 170 | // which is not quite its nominal probability: the coder works the |
| 146 | 171 | // range through an eight-bit multiply and hands every symbol above |
| 147 | 172 | // 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 | + } | |
| 151 | 182 | } |
| 152 | 183 | |
| 153 | 184 | /// Writes `bits` raw bits of `value`, most significant first — the `L(n)` |
| @@ -169,6 +200,7 @@ impl SymbolEncoder { | ||
| 169 | 200 | /// inside it however the decoder pads the string, which is what lets a tile |
| 170 | 201 | /// payload end on a byte boundary with no terminator. |
| 171 | 202 | pub fn finish(mut self) -> Vec<u8> { |
| 203 | + assert!(!self.pricing, "a pricer holds no bytes to flush"); | |
| 172 | 204 | // Round the interval's low end up to a value with 14 low zero bits, |
| 173 | 205 | // then set bit 14: the result is inside the interval (the interval is |
| 174 | 206 | // at least 2^15 wide before normalisation) and its tail bits are all |
| @@ -223,6 +255,7 @@ impl SymbolEncoder { | ||
| 223 | 255 | self.low = low << d; |
| 224 | 256 | self.rng = rng << d; |
| 225 | 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 | 2378 | TX16 => &SCANS[2], |
| 2379 | 2379 | _ => &SCANS[3], |
| 2380 | 2380 | }; |
| 2381 | - let mut enc = SymbolEncoder::new(); | |
| 2382 | - enc.reset_bits(); | |
| 2381 | + let mut enc = SymbolEncoder::pricer(); | |
| 2383 | 2382 | write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None); |
| 2384 | 2383 | enc.bits() |
| 2385 | 2384 | }) |
| @@ -2432,10 +2431,19 @@ fn write_coeffs( | ||
| 2432 | 2431 | ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell())); |
| 2433 | 2432 | } |
| 2434 | 2433 | 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 | + }; | |
| 2439 | 2447 | enc.symbol(usize::from(eob == 0), &mut coding.txb_skip[skip_ctx]); |
| 2440 | 2448 | if plane.is_some() { |
| 2441 | 2449 | ec_rng_trace(|| { |
| @@ -2639,10 +2647,22 @@ fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize { | ||
| 2639 | 2647 | if row == 0 && col == 0 { |
| 2640 | 2648 | return 0; |
| 2641 | 2649 | } |
| 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 | + }; | |
| 2646 | 2666 | let offset = cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize; |
| 2647 | 2667 | (((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 | 2671 | /// of its three closest neighbours below and to the right, uncapped, and a |
| 2652 | 2672 | /// term separating the DC, the corner of the transform, and the rest. |
| 2653 | 2673 | 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 | + }; | |
| 2658 | 2683 | let mag = (((mag + 1) >> 1).min(6)) as usize; |
| 2659 | 2684 | if row == 0 && col == 0 { |
| 2660 | 2685 | mag |