use super::celt_log2; use crate::celt::{ BETA_COEF, BETA_INTRA, E_MEANS, E_PROB_MODEL, MAX_FINE_BITS, NB_BANDS, PRED_COEF, SMALL_ENERGY_ICDF, }; use crate::range::RangeEncoder; const LAPLACE_MINP: u32 = 1; const LAPLACE_NMIN: u32 = 16; const MAX_FRAME_BYTES: usize = 1275; fn laplace_get_freq1(fs0: u32, decay: i32) -> u32 { let ft = 32768 - LAPLACE_MINP * (2 * LAPLACE_NMIN) - fs0; ft.wrapping_mul((16384 - decay) as u32) >> 15 } fn laplace_encode(enc: &mut RangeEncoder, value: &mut i32, fs: u32, decay: i32) { let mut fs = fs; let mut fl: u32 = 0; let mut val = *value; if val != 0 { let s: i32 = -((val < 0) as i32); val = (val + s) ^ s; fl = fs; fs = laplace_get_freq1(fs, decay); let mut i: i32 = 1; while fs > 0 && i < val { fs *= 2; fl += fs + 2 * LAPLACE_MINP; fs = (fs * decay as u32) >> 15; i += 1; } if fs == 0 { let ndi_max = ((32768u32.wrapping_sub(fl).wrapping_add(LAPLACE_MINP - 1)) as i32 - s) >> 1; let di = (val - i).min(ndi_max - 1); fl = fl.wrapping_add((2 * di + 1 + s) as u32 * LAPLACE_MINP); fs = LAPLACE_MINP.min(32768u32.wrapping_sub(fl)); *value = (i + di + s) ^ s; } else { fs += LAPLACE_MINP; if s == 0 { fl += fs; } } } enc.encode_bin(fl, fl + fs, 15); } fn loss_distortion( e_bands: &[f32], old_e_bands: &[f32], start: usize, end: usize, c: usize, ) -> f32 { let mut dist = 0f32; for ch in 0..c { for i in start..end { let d = e_bands[i + ch * NB_BANDS] - old_e_bands[i + ch * NB_BANDS]; dist += d * d; } } if 200.0 < dist { 200.0 } else { dist } } fn quant_coarse_energy_impl( start: usize, end: usize, e_bands: &[f32], old_e_bands: &mut [f32], budget: i32, mut tell: i32, prob_model: &[u8; 42], error: &mut [f32], enc: &mut RangeEncoder, c: usize, lm: usize, intra: bool, max_decay: f32, lfe: bool, ) -> i32 { let mut badness = 0i32; let mut prev = [0f32; 2]; if tell + 3 <= budget { enc.enc_bit_logp(intra, 3); } let (coef, beta) = if intra { (0f32, BETA_INTRA) } else { (PRED_COEF[lm], BETA_COEF[lm]) }; for i in start..end { for ch in 0..c { let idx = i + ch * NB_BANDS; let x = e_bands[idx]; let old_e = if -9.0f32 > old_e_bands[idx] { -9.0f32 } else { old_e_bands[idx] }; let f = x - coef * old_e - prev[ch]; let mut qi = ((0.5f32 + f) as f64).floor() as i32; let raw_old = if -28.0f32 > old_e_bands[idx] { -28.0f32 } else { old_e_bands[idx] }; let decay_bound = raw_old - max_decay; if qi < 0 && x < decay_bound { qi += (decay_bound - x) as i32; if qi > 0 { qi = 0; } } let qi0 = qi; tell = enc.tell() as i32; let bits_left = budget - tell - 3 * c as i32 * (end - i) as i32; if i != start && bits_left < 30 { if bits_left < 24 { qi = qi.min(1); } if bits_left < 16 { qi = qi.max(-1); } } if lfe && i >= 2 { qi = qi.min(0); } if budget - tell >= 15 { let pi = 2 * i.min(20); laplace_encode( enc, &mut qi, (prob_model[pi] as u32) << 7, (prob_model[pi + 1] as i32) << 6, ); } else if budget - tell >= 2 { qi = qi.clamp(-1, 1); let k = (2 * qi) ^ if qi < 0 { -1 } else { 0 }; enc.enc_icdf(k as usize, &SMALL_ENERGY_ICDF, 2); } else if budget - tell >= 1 { qi = qi.min(0); enc.enc_bit_logp(qi != 0, 1); } else { qi = -1; } error[idx] = f - qi as f32; badness += (qi0 - qi).abs(); let q = qi as f32; old_e_bands[idx] = coef * old_e + prev[ch] + q; prev[ch] = prev[ch] + q - beta * q; } } if lfe { 0 } else { badness } } /// `quant_coarse_energy`. pub(crate) fn quant_coarse_energy( start: usize, end: usize, eff_end: usize, e_bands: &[f32], old_e_bands: &mut [f32], budget: u32, error: &mut [f32], enc: &mut RangeEncoder, c: usize, lm: usize, nb_available_bytes: i32, force_intra: bool, delayed_intra: &mut f32, two_pass: bool, loss_rate: i32, lfe: bool, ) { let n = c * NB_BANDS; let mut two_pass = two_pass; let mut intra = force_intra || (!two_pass && *delayed_intra > (2 * c * (end - start)) as f32 && nb_available_bytes > ((end - start) * c) as i32); let intra_bias = ((budget as f32 * *delayed_intra * loss_rate as f32) / (c * 512) as f32) as i32; let new_distortion = loss_distortion(e_bands, old_e_bands, start, eff_end, c); let tell = enc.tell(); if tell + 3 > budget { two_pass = false; intra = false; } let mut max_decay = 16f32; if end - start > 10 { let v = 0.125f32 * nb_available_bytes as f32; max_decay = if max_decay < v { max_decay } else { v }; } if lfe { max_decay = 3.0; } let start_state = enc.snapshot(); let nstart = enc.front_offs(); let mut old_intra = [0f32; 2 * NB_BANDS]; let mut err_intra = [0f32; 2 * NB_BANDS]; old_intra[..n].copy_from_slice(&old_e_bands[..n]); let mut badness1 = 0; if two_pass || intra { badness1 = quant_coarse_energy_impl( start, end, e_bands, &mut old_intra, budget as i32, tell as i32, &E_PROB_MODEL[lm][1], &mut err_intra, enc, c, lm, true, max_decay, lfe, ); } if !intra { let tell_intra = enc.tell_frac() as i32; let intra_state = enc.snapshot(); let nintra = enc.front_offs(); let intra_len = nintra - nstart; let mut intra_bits = [0u8; MAX_FRAME_BYTES]; intra_bits[..intra_len].copy_from_slice(&enc.front()[nstart..nintra]); enc.restore(&start_state); let badness2 = quant_coarse_energy_impl( start, end, e_bands, old_e_bands, budget as i32, tell as i32, &E_PROB_MODEL[lm][0], error, enc, c, lm, false, max_decay, lfe, ); if two_pass && (badness1 < badness2 || (badness1 == badness2 && enc.tell_frac() as i32 + intra_bias > tell_intra)) { enc.restore(&intra_state); enc.front_write(nstart, &intra_bits[..intra_len]); old_e_bands[..n].copy_from_slice(&old_intra[..n]); error[..n].copy_from_slice(&err_intra[..n]); intra = true; } } else { old_e_bands[..n].copy_from_slice(&old_intra[..n]); error[..n].copy_from_slice(&err_intra[..n]); } if intra { *delayed_intra = new_distortion; } else { *delayed_intra = PRED_COEF[lm] * PRED_COEF[lm] * *delayed_intra + new_distortion; } } /// `quant_fine_energy` as celt_encode_with_ec calls it without QEXT (`prev_quant` NULL). pub(crate) fn quant_fine_energy( start: usize, end: usize, old_e_bands: &mut [f32], error: &mut [f32], fine_quant: &[i32], enc: &mut RangeEncoder, c: usize, ) { for i in start..end { let fq = fine_quant[i]; if fq <= 0 { continue; } if enc.tell() as i32 + c as i32 * fq > enc.storage() as i32 * 8 { continue; } let extra = 1i32 << fq; for ch in 0..c { let idx = i + ch * NB_BANDS; let q2 = (((error[idx] + 0.5f32) * extra as f32) as f64).floor() as i32; let q2 = q2.min(extra - 1).max(0); enc.enc_bits(q2 as u32, fq as u32); let offset = (q2 as f32 + 0.5) * (1 << (14 - fq)) as f32 * (1.0f32 / 16384.0) - 0.5; old_e_bands[idx] += offset; error[idx] -= offset; } } } /// `quant_energy_finalise`. pub(crate) fn quant_energy_finalise( start: usize, end: usize, old_e_bands: &mut [f32], error: &mut [f32], fine_quant: &[i32], fine_priority: &[i32], bits_left: i32, enc: &mut RangeEncoder, c: usize, ) { let mut bits_left = bits_left; for prio in 0..2 { for i in start..end { if bits_left < c as i32 { break; } if fine_quant[i] >= MAX_FINE_BITS || fine_priority[i] != prio { continue; } for ch in 0..c { let idx = i + ch * NB_BANDS; let q2: u32 = if error[idx] < 0.0 { 0 } else { 1 }; enc.enc_bits(q2, 1); let offset = (q2 as f32 - 0.5) * (1 << (14 - fine_quant[i] - 1)) as f32 * (1.0f32 / 16384.0); old_e_bands[idx] += offset; error[idx] -= offset; bits_left -= 1; } } } } /// `amp2Log2`; `band_e`/`band_log_e` are `c * 21`. pub(crate) fn amp2log2( eff_end: usize, end: usize, band_e: &[f32], band_log_e: &mut [f32], c: usize, ) { for ch in 0..c { for i in 0..eff_end { band_log_e[i + ch * NB_BANDS] = celt_log2(band_e[i + ch * NB_BANDS]) - E_MEANS[i]; } for i in eff_end..end { band_log_e[ch * NB_BANDS + i] = -14.0; } } }