Price a symbol string off its final interval, not one log2 per symbol
Browse files at this commitfb3fa69b574c89c2530d5f0e5739474e63931cf9Parent: 0f15668- perf
- price a symbol string off its final interval, not one log2 per symbol
2 changed files +45 -13
- crates/ec-av1/src/msac.rs +44 -11
- crates/ec-av1/src/tile.rs +1 -2
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` by | 47 | /// 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 written | 50 | + /// `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 of | 51 | + /// width at [`Self::new`] or at the last [`Self::reset_bits`]. |
| 52 | - /// probability `p` to within the rounding of its interval, so this is what | 52 | + 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.bits | 101 | + 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's | 114 | /// 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 * 8 | 118 | 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 of | 121 | /// 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 decoder | 128 | /// 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 the | 170 | // which is not quite its nominal probability: the coder works the |
| 146 | // range through an eight-bit multiply and hands every symbol above | 171 | // range through an eight-bit multiply and hands every symbol above |
| 147 | // this one a floor of `EC_MIN_PROB`. Pricing the narrowing rather than | 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. | 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 tile | 200 | /// 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 is | 205 | // 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 all | 206 | // 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+1 -2
| @@ -2213,8 +2213,7 @@ pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet, q_ctx: usize) -> f64 { | |||
| 2213 | TX16 => &SCANS[2], | 2213 | TX16 => &SCANS[2], |
| 2214 | _ => &SCANS[3], | 2214 | _ => &SCANS[3], |
| 2215 | }; | 2215 | }; |
| 2216 | - let mut enc = SymbolEncoder::new(); | 2216 | + let mut enc = SymbolEncoder::pricer(); |
| 2217 | - enc.reset_bits(); | ||
| 2218 | write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None); | 2217 | write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None); |
| 2219 | enc.bits() | 2218 | enc.bits() |
| 2220 | }) | 2219 | }) |