diff --git a/crates/ec-av1/src/msac.rs b/crates/ec-av1/src/msac.rs index 695bc965..61b9cc17 100644 --- a/crates/ec-av1/src/msac.rs +++ b/crates/ec-av1/src/msac.rs @@ -47,11 +47,22 @@ pub struct SymbolEncoder { /// Flushed bytes before carry propagation: each entry can exceed `0xff` by /// the carry it owes the byte before it, which [`Self::finish`] adds in. precarry: Vec, - /// What every symbol written so far cost against the CDF it was written - /// with, in bits. The arithmetic coder spends `-log2(p)` on a symbol of - /// probability `p` to within the rounding of its interval, so this is what - /// a search may price a decision with without coding it twice. - bits: f64, + /// `log2` of the interval width the bit account started from — the coder's + /// width at [`Self::new`] or at the last [`Self::reset_bits`]. + bits_base: f64, + /// How many bits the renormalisation has shifted `rng` up by since the + /// account started. A symbol costs `log2(r) - log2(rng)` for the widths it + /// narrows between, and the next symbol's `r` is this symbol's `rng` + /// shifted left by the renormalisation, so the whole string's cost + /// telescopes to `bits_base + shifts - log2(rng)` — one `log2` per price + /// asked for, instead of one per symbol (libaom's `od_ec_enc_tell_frac` + /// reads its account the same way). + shifts: u32, + /// Whether this coder only prices: the bit account depends on nothing but + /// how far each symbol narrows `rng`, so a pricer keeps the interval width + /// and drops the value bookkeeping (`low`, `cnt`, `precarry`) whose only + /// consumer is [`Self::finish`]. + pricing: bool, } impl Default for SymbolEncoder { @@ -69,13 +80,25 @@ impl SymbolEncoder { rng: 0x8000, cnt: -9, precarry: Vec::new(), - bits: 0.0, + bits_base: 15.0, + shifts: 0, + pricing: false, + } + } + + /// A coder that only prices, for a search that wants [`Self::bits`] and + /// will never ask for the bytes. Every symbol narrows `rng` exactly as it + /// would in a writing coder, so the account is the same to the bit. + pub fn pricer() -> SymbolEncoder { + SymbolEncoder { + pricing: true, + ..SymbolEncoder::new() } } /// What the symbols written so far cost, in bits. pub fn bits(&self) -> f64 { - self.bits + self.bits_base + f64::from(self.shifts) - f64::from(self.rng).log2() } /// The coder's current interval width — libaom's `od_ec_enc_rng` / @@ -91,13 +114,15 @@ impl SymbolEncoder { /// holds one `u16` per byte already flushed. Pairs with the decoder's /// `aom_reader_tell`, which counts the same bits consumed. pub fn tell(&self) -> i32 { + debug_assert!(!self.pricing, "a pricer keeps no byte position"); self.cnt + 10 + self.precarry.len() as i32 * 8 } /// Forgets the cost accumulated so far, so that the next stretch of /// symbols can be priced on its own. pub fn reset_bits(&mut self) { - self.bits = 0.0; + self.bits_base = f64::from(self.rng).log2(); + self.shifts = 0; } /// Writes `symbol` against `cdf` and adapts `cdf` exactly as the decoder @@ -145,9 +170,15 @@ impl SymbolEncoder { // which is not quite its nominal probability: the coder works the // range through an eight-bit multiply and hands every symbol above // this one a floor of `EC_MIN_PROB`. Pricing the narrowing rather than - // the table entry is what makes the account match the bytes. - self.bits -= (f64::from(rng) / f64::from(r)).log2(); - self.normalize(low, rng); + // the table entry is what makes the account match the bytes; the + // narrowing itself is read off `rng` and `shifts` by [`Self::bits`]. + if self.pricing { + let d = 16 - (32 - rng.leading_zeros()); + self.rng = rng << d; + self.shifts += d; + } else { + self.normalize(low, rng); + } } /// Writes `bits` raw bits of `value`, most significant first — the `L(n)` @@ -169,6 +200,7 @@ impl SymbolEncoder { /// inside it however the decoder pads the string, which is what lets a tile /// payload end on a byte boundary with no terminator. pub fn finish(mut self) -> Vec { + assert!(!self.pricing, "a pricer holds no bytes to flush"); // Round the interval's low end up to a value with 14 low zero bits, // then set bit 14: the result is inside the interval (the interval is // at least 2^15 wide before normalisation) and its tail bits are all @@ -223,6 +255,7 @@ impl SymbolEncoder { self.low = low << d; self.rng = rng << d; self.cnt = s; + self.shifts += d as u32; } } diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 8d5613fe..2f5eebf9 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -2378,8 +2378,7 @@ pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet, q_ctx: usize) -> f64 { TX16 => &SCANS[2], _ => &SCANS[3], }; - let mut enc = SymbolEncoder::new(); - enc.reset_bits(); + let mut enc = SymbolEncoder::pricer(); write_coeffs(&mut enc, &mut coding, grid, scan, 0, 0, None); enc.bits() }) @@ -2432,10 +2431,19 @@ fn write_coeffs( ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell())); } let side = coding.side; - let eob = scan - .iter() - .rposition(|&pos| grid[pos as usize] != 0) - .map_or(0, |i| i + 1); + // The scan-order search reads the grid in scan order, one dependent load + // per position, and an all-zero block makes it walk every one of them -- + // which the pricer does for candidate after candidate. A straight-line + // pass settles that case first: it vectorises, and a block that codes + // anything at all almost always codes its DC, so it stops at the first + // element. + let eob = if grid.iter().all(|&level| level == 0) { + 0 + } else { + scan.iter() + .rposition(|&pos| grid[pos as usize] != 0) + .map_or(0, |i| i + 1) + }; enc.symbol(usize::from(eob == 0), &mut coding.txb_skip[skip_ctx]); if plane.is_some() { ec_rng_trace(|| { @@ -2639,10 +2647,22 @@ fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize { if row == 0 && col == 0 { return 0; } - let mag: i32 = [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)] - .iter() - .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3)) - .sum(); + // A coefficient two rows and two columns clear of the far edges reaches + // all five neighbours, so the gather is five loads off one index with no + // per-neighbour edge test -- which is where this writer spends a third of + // its coefficient loop. + let p = row * side + col; + let mag: i32 = if row + 2 < side && col + 2 < side { + [p + side, p + 1, p + side + 1, p + 2 * side, p + 2] + .iter() + .map(|&i| grid[i].abs().min(3)) + .sum() + } else { + [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)] + .iter() + .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3)) + .sum() + }; let offset = cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize; (((mag + 1) >> 1).min(4) as usize) + offset } @@ -2651,10 +2671,15 @@ fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize { /// of its three closest neighbours below and to the right, uncapped, and a /// term separating the DC, the corner of the transform, and the rest. fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize) -> usize { - let mag: i32 = [(1, 0), (0, 1), (1, 1)] - .iter() - .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs()) - .sum(); + let p = row * side + col; + let mag: i32 = if row + 1 < side && col + 1 < side { + grid[p + side].abs() + grid[p + 1].abs() + grid[p + side + 1].abs() + } else { + [(1, 0), (0, 1), (1, 1)] + .iter() + .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs()) + .sum() + }; let mag = (((mag + 1) >> 1).min(6)) as usize; if row == 0 && col == 0 { mag