EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
Merge upstream/main into av1enc-scratch-buffers
vangel76bcc21b939c06
diff --cc crates/av1enc/src/decide.rsindex c9fe6f268,db18c61c3..9bbd56823--- a/crates/av1enc/src/decide.rs+++ b/crates/av1enc/src/decide.rs@@@ -107,31 -122,6 +122,38 @@@ struct TxResultbits: f64,}++/// The output and scratch buffers of one reconstruction, each `n * n` entries long.++struct ReconBuffers<'a> {++ deq: &'a mut [i32],++ resid: &'a mut [i32],++ recon: &'a mut [u16],++}+++/// Outcome of trying one transform block; the levels and the reconstruction stay in `TxScratch`.+#[derive(Clone, Copy)]+struct TxEval {+ nonzero: bool,+ dist: f64,+ bits: f64,+}++/// Buffers of the transform path, sized for the largest coded transform block (32x32). Only the+/// first `n * n` entries are used; they are reused across candidates instead of being allocated.+struct TxScratch {+ res: [i32; 1024],+ coef: [f32; 1024],+ levels: [i32; 1024],+ deq: [i32; 1024],+ resid: [i32; 1024],+ recon: [u16; 1024],+}++impl Default for TxScratch {+ fn default() -> Self {+ TxScratch { res: [0; 1024], coef: [0.0; 1024], levels: [0; 1024], deq: [0; 1024], resid: [0; 1024], recon: [0; 1024] }+ }+}+/// Saved encoder state of a block area (reconstruction, BlockDecoded flags).struct Snapshot {planes: [Vec<u16>; 3],@@@ -373,26 -358,26 +395,26 @@@ impl<'a> Decider<'a>let ed = edges(&self.rec[0], &e);// modes ranked by SATD, then a rate-distortion choice of mode and transform type among// the best three- let mut ranked: Vec<(f64, usize)> = INTRA_MODES_USED- .iter()- .map(|&mode| {- predict_intra(&ed, &e, mode, &mut pred);- let sad = self.satd(0, c * 4, r * 4, n, &pred[..n * n]) as f64;- (sad + self.lambda_sad * if mode == DC_PRED { 1.0 } else { MODE_BITS_INTRA }, mode)- })- .collect();+ let mut ranked: [(f64, usize); INTRA_MODES_USED.len()] = INTRA_MODES_USED.map(|mode| {+ predict_intra(&ed, &e, mode, &mut pred);+ let sad = self.satd(0, c * 4, r * 4, n, &pred[..n * n]) as f64;+ (sad + self.lambda_sad * if mode == DC_PRED { 1.0 } else { MODE_BITS_INTRA }, mode)+ });ranked.sort_by(|a, b| a.0.total_cmp(&b.0));let tx_y = tx_size_for(log2);- let mut best: Option<(f64, usize, usize, TxResult, Option<Vec<u16>>)> = None;+ let mut best: Option<(f64, usize, usize, TxEval, bool)> = None;+ let mut sc = self.scratch.take().unwrap_or_default();for &(_, mode) in ranked.iter().take(3) {predict_intra(&ed, &e, mode, &mut pred);- let mut types = vec![DCT_DCT];+ let mut types = [DCT_DCT; 2];+ let mut n_types = 1;let alt = MODE_TO_TXFM[mode] as usize;if alt != DCT_DCT && tx_in_set(tx_set(tx_y, false), false, alt) {- types.push(alt);+ types[1] = alt;+ n_types = 2;}- for &t in &types {- let (res, recon) = self.try_tx(TransformBlock { plane: 0, x: c * 4, y: r * 4, log2, tx_type: t, intra: true }, &pred[..n * n]);+ for &t in &types[..n_types] {- let (res, has_recon) = self.try_tx(&mut sc, 0, c * 4, r * 4, log2, t, &pred[..n * n], true);++ let (res, has_recon) = self.try_tx(&mut sc, TransformBlock { plane: 0, x: c * 4, y: r * 4, log2, tx_type: t, intra: true }, &pred[..n * n]);let type_bits = if !res.nonzero || tx_set(tx_y, false) == TX_SET_DCTONLY {0.0} else if t == DCT_DCT {@@@ -807,21 -790,19 +833,23 @@@/// Transform, quantise and reconstruct one square transform block of `plane` at (x, y)/// over the prediction `pred`; the reconstruction is written to `rec`.- fn code_tx(&mut self, p: usize, x: usize, y: usize, log2: u32, tx_type: usize, pred: &[u16], intra: bool) -> TxResult {+ fn code_tx(&mut self, block: TransformBlock, pred: &[u16]) -> TxResult {+ let TransformBlock { plane: p, x, y, log2, .. } = block;- let (res, recon) = self.try_tx(block, pred);+ let mut sc = self.scratch.take().unwrap_or_default();- let (res, has_recon) = self.try_tx(&mut sc, p, x, y, log2, tx_type, pred, intra);++ let (res, has_recon) = self.try_tx(&mut sc, block, pred);let n = 1usize << log2;- self.put(p, x, y, n, recon.as_deref().unwrap_or(pred));- res+ self.put(p, x, y, n, if has_recon { &sc.recon[..n * n] } else { pred });+ let levels = sc.levels[..n * n].to_vec();+ self.scratch = Some(sc);+ TxResult { levels, nonzero: res.nonzero, dist: res.dist, bits: res.bits }}- /// Transform and quantise one block; returns the result and its reconstruction (None: the- /// prediction) without writing it.- fn try_tx(&self, block: TransformBlock, pred: &[u16]) -> (TxResult, Option<Vec<u16>>) {+ /// Transform and quantise one block without writing it. The levels are left in `sc.levels`;+ /// the flag tells whether `sc.recon` holds the reconstruction (false: it is the prediction).- fn try_tx(&self, sc: &mut TxScratch, p: usize, x: usize, y: usize, log2: u32, tx_type: usize, pred: &[u16], intra: bool) -> (TxEval, bool) {++ fn try_tx(&self, sc: &mut TxScratch, block: TransformBlock, pred: &[u16]) -> (TxEval, bool) {+ let TransformBlock { plane: p, x, y, log2, tx_type, intra } = block;let n = 1usize << log2;+ let nn = n * n;let tx_sz = tx_size_for(log2);let bd = self.bd;let src = &self.src[p];@@@ -848,36 -829,23 +876,26 @@@}let zero_dist = self.sse(p, x, y, n, pred);if !nonzero {- return (TxResult { levels, nonzero, dist: zero_dist, bits: 1.0 }, None);+ return (TxEval { nonzero, dist: zero_dist, bits: 1.0 }, false);}- let recon = self.reconstruct(&levels, tx_sz, tx_type, pred, n, bd);- let dist = self.sse(p, x, y, n, &recon);- let bits = estimate_bits(&levels, tx_sz);+ let recon = &mut recon[..nn];- self.reconstruct(levels, &mut deq[..nn], &mut resid[..nn], recon, tx_sz, tx_type, pred, n, bd);++ self.reconstruct(levels, ReconBuffers { deq: &mut deq[..nn], resid: &mut resid[..nn], recon }, tx_sz, tx_type, pred, bd);+ let dist = self.sse(p, x, y, n, recon);+ let bits = estimate_bits(levels, tx_sz);if dist + self.lambda * bits >= zero_dist + self.lambda {- levels.iter_mut().for_each(|l| *l = 0);- return (TxResult { levels, nonzero: false, dist: zero_dist, bits: 1.0 }, None);+ levels.fill(0);+ return (TxEval { nonzero: false, dist: zero_dist, bits: 1.0 }, false);}- (TxResult { levels, nonzero: true, dist, bits }, Some(recon))+ (TxEval { nonzero: true, dist, bits }, true)}- /// Dequantisation (7.12.3) and the inverse transform, added to the prediction.- fn reconstruct(&self, levels: &[i32], tx_sz: usize, tx_type: usize, pred: &[u16], n: usize, bd: u32) -> Vec<u16> {+ /// Dequantisation (7.12.3) and the inverse transform, added to the prediction; the result is+ /// written to `recon`. `deq` and `resid` are scratch buffers of `n * n` entries.- fn reconstruct(- &self,- levels: &[i32],- deq: &mut [i32],- resid: &mut [i32],- recon: &mut [u16],- tx_sz: usize,- tx_type: usize,- pred: &[u16],- n: usize,- bd: u32,- ) {++ fn reconstruct(&self, levels: &[i32], bufs: ReconBuffers, tx_sz: usize, tx_type: usize, pred: &[u16], bd: u32) {++ let ReconBuffers { deq, resid, recon } = bufs;let denom: i64 = if tx_sz == TX_32X32 { 2 } else { 1 };let lim = 1i64 << (7 + bd);- let mut deq = vec![0i32; n * n];+ deq.fill(0);for (k, (&l, d)) in levels.iter().zip(deq.iter_mut()).enumerate() {if l == 0 {continue;@@@ -887,12 -855,10 +905,11 @@@let mag = (dq.abs() & 0xFF_FFFF) / denom;*d = (if dq < 0 { -mag } else { mag }).clamp(-lim, lim - 1) as i32;}- let mut resid = vec![0i32; n * n];- inverse_2d(&deq, &mut resid, tx_sz, tx_type, bd);+ inverse_2d(deq, resid, tx_sz, tx_type, bd);let max = (1i32 << bd) - 1;- pred.iter().zip(&resid).map(|(&p, &r)| (p as i32 + r).clamp(0, max) as u16).collect()+ for ((o, &p), &r) in recon.iter_mut().zip(pred).zip(resid.iter()) {+ *o = (p as i32 + r).clamp(0, max) as u16;+ }- debug_assert_eq!(recon.len(), n * n);}}