edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | use super::celt_log2; |
| 2 | use crate::celt::{ |
| 3 | BETA_COEF, BETA_INTRA, E_MEANS, E_PROB_MODEL, MAX_FINE_BITS, NB_BANDS, PRED_COEF, |
| 4 | SMALL_ENERGY_ICDF, |
| 5 | }; |
| 6 | use crate::range::RangeEncoder; |
| 7 | |
| 8 | const LAPLACE_MINP: u32 = 1; |
| 9 | const LAPLACE_NMIN: u32 = 16; |
| 10 | const MAX_FRAME_BYTES: usize = 1275; |
| 11 | |
| 12 | fn laplace_get_freq1(fs0: u32, decay: i32) -> u32 { |
| 13 | let ft = 32768 - LAPLACE_MINP * (2 * LAPLACE_NMIN) - fs0; |
| 14 | ft.wrapping_mul((16384 - decay) as u32) >> 15 |
| 15 | } |
| 16 | |
| 17 | fn laplace_encode(enc: &mut RangeEncoder, value: &mut i32, fs: u32, decay: i32) { |
| 18 | let mut fs = fs; |
| 19 | let mut fl: u32 = 0; |
| 20 | let mut val = *value; |
| 21 | if val != 0 { |
| 22 | let s: i32 = -((val < 0) as i32); |
| 23 | val = (val + s) ^ s; |
| 24 | fl = fs; |
| 25 | fs = laplace_get_freq1(fs, decay); |
| 26 | let mut i: i32 = 1; |
| 27 | while fs > 0 && i < val { |
| 28 | fs *= 2; |
| 29 | fl += fs + 2 * LAPLACE_MINP; |
| 30 | fs = (fs * decay as u32) >> 15; |
| 31 | i += 1; |
| 32 | } |
| 33 | if fs == 0 { |
| 34 | let ndi_max = |
| 35 | ((32768u32.wrapping_sub(fl).wrapping_add(LAPLACE_MINP - 1)) as i32 - s) >> 1; |
| 36 | let di = (val - i).min(ndi_max - 1); |
| 37 | fl = fl.wrapping_add((2 * di + 1 + s) as u32 * LAPLACE_MINP); |
| 38 | fs = LAPLACE_MINP.min(32768u32.wrapping_sub(fl)); |
| 39 | *value = (i + di + s) ^ s; |
| 40 | } else { |
| 41 | fs += LAPLACE_MINP; |
| 42 | if s == 0 { |
| 43 | fl += fs; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | enc.encode_bin(fl, fl + fs, 15); |
| 48 | } |
| 49 | |
| 50 | fn loss_distortion( |
| 51 | e_bands: &[f32], |
| 52 | old_e_bands: &[f32], |
| 53 | start: usize, |
| 54 | end: usize, |
| 55 | c: usize, |
| 56 | ) -> f32 { |
| 57 | let mut dist = 0f32; |
| 58 | for ch in 0..c { |
| 59 | for i in start..end { |
| 60 | let d = e_bands[i + ch * NB_BANDS] - old_e_bands[i + ch * NB_BANDS]; |
| 61 | dist += d * d; |
| 62 | } |
| 63 | } |
| 64 | if 200.0 < dist { 200.0 } else { dist } |
| 65 | } |
| 66 | |
| 67 | fn quant_coarse_energy_impl( |
| 68 | start: usize, |
| 69 | end: usize, |
| 70 | e_bands: &[f32], |
| 71 | old_e_bands: &mut [f32], |
| 72 | budget: i32, |
| 73 | mut tell: i32, |
| 74 | prob_model: &[u8; 42], |
| 75 | error: &mut [f32], |
| 76 | enc: &mut RangeEncoder, |
| 77 | c: usize, |
| 78 | lm: usize, |
| 79 | intra: bool, |
| 80 | max_decay: f32, |
| 81 | lfe: bool, |
| 82 | ) -> i32 { |
| 83 | let mut badness = 0i32; |
| 84 | let mut prev = [0f32; 2]; |
| 85 | if tell + 3 <= budget { |
| 86 | enc.enc_bit_logp(intra, 3); |
| 87 | } |
| 88 | let (coef, beta) = if intra { |
| 89 | (0f32, BETA_INTRA) |
| 90 | } else { |
| 91 | (PRED_COEF[lm], BETA_COEF[lm]) |
| 92 | }; |
| 93 | for i in start..end { |
| 94 | for ch in 0..c { |
| 95 | let idx = i + ch * NB_BANDS; |
| 96 | let x = e_bands[idx]; |
| 97 | let old_e = if -9.0f32 > old_e_bands[idx] { |
| 98 | -9.0f32 |
| 99 | } else { |
| 100 | old_e_bands[idx] |
| 101 | }; |
| 102 | let f = x - coef * old_e - prev[ch]; |
| 103 | let mut qi = ((0.5f32 + f) as f64).floor() as i32; |
| 104 | let raw_old = if -28.0f32 > old_e_bands[idx] { |
| 105 | -28.0f32 |
| 106 | } else { |
| 107 | old_e_bands[idx] |
| 108 | }; |
| 109 | let decay_bound = raw_old - max_decay; |
| 110 | if qi < 0 && x < decay_bound { |
| 111 | qi += (decay_bound - x) as i32; |
| 112 | if qi > 0 { |
| 113 | qi = 0; |
| 114 | } |
| 115 | } |
| 116 | let qi0 = qi; |
| 117 | tell = enc.tell() as i32; |
| 118 | let bits_left = budget - tell - 3 * c as i32 * (end - i) as i32; |
| 119 | if i != start && bits_left < 30 { |
| 120 | if bits_left < 24 { |
| 121 | qi = qi.min(1); |
| 122 | } |
| 123 | if bits_left < 16 { |
| 124 | qi = qi.max(-1); |
| 125 | } |
| 126 | } |
| 127 | if lfe && i >= 2 { |
| 128 | qi = qi.min(0); |
| 129 | } |
| 130 | if budget - tell >= 15 { |
| 131 | let pi = 2 * i.min(20); |
| 132 | laplace_encode( |
| 133 | enc, |
| 134 | &mut qi, |
| 135 | (prob_model[pi] as u32) << 7, |
| 136 | (prob_model[pi + 1] as i32) << 6, |
| 137 | ); |
| 138 | } else if budget - tell >= 2 { |
| 139 | qi = qi.clamp(-1, 1); |
| 140 | let k = (2 * qi) ^ if qi < 0 { -1 } else { 0 }; |
| 141 | enc.enc_icdf(k as usize, &SMALL_ENERGY_ICDF, 2); |
| 142 | } else if budget - tell >= 1 { |
| 143 | qi = qi.min(0); |
| 144 | enc.enc_bit_logp(qi != 0, 1); |
| 145 | } else { |
| 146 | qi = -1; |
| 147 | } |
| 148 | error[idx] = f - qi as f32; |
| 149 | badness += (qi0 - qi).abs(); |
| 150 | let q = qi as f32; |
| 151 | old_e_bands[idx] = coef * old_e + prev[ch] + q; |
| 152 | prev[ch] = prev[ch] + q - beta * q; |
| 153 | } |
| 154 | } |
| 155 | if lfe { 0 } else { badness } |
| 156 | } |
| 157 | |
| 158 | /// `quant_coarse_energy`. |
| 159 | pub(crate) fn quant_coarse_energy( |
| 160 | start: usize, |
| 161 | end: usize, |
| 162 | eff_end: usize, |
| 163 | e_bands: &[f32], |
| 164 | old_e_bands: &mut [f32], |
| 165 | budget: u32, |
| 166 | error: &mut [f32], |
| 167 | enc: &mut RangeEncoder, |
| 168 | c: usize, |
| 169 | lm: usize, |
| 170 | nb_available_bytes: i32, |
| 171 | force_intra: bool, |
| 172 | delayed_intra: &mut f32, |
| 173 | two_pass: bool, |
| 174 | loss_rate: i32, |
| 175 | lfe: bool, |
| 176 | ) { |
| 177 | let n = c * NB_BANDS; |
| 178 | let mut two_pass = two_pass; |
| 179 | let mut intra = force_intra |
| 180 | || (!two_pass |
| 181 | && *delayed_intra > (2 * c * (end - start)) as f32 |
| 182 | && nb_available_bytes > ((end - start) * c) as i32); |
| 183 | let intra_bias = |
| 184 | ((budget as f32 * *delayed_intra * loss_rate as f32) / (c * 512) as f32) as i32; |
| 185 | let new_distortion = loss_distortion(e_bands, old_e_bands, start, eff_end, c); |
| 186 | |
| 187 | let tell = enc.tell(); |
| 188 | if tell + 3 > budget { |
| 189 | two_pass = false; |
| 190 | intra = false; |
| 191 | } |
| 192 | |
| 193 | let mut max_decay = 16f32; |
| 194 | if end - start > 10 { |
| 195 | let v = 0.125f32 * nb_available_bytes as f32; |
| 196 | max_decay = if max_decay < v { max_decay } else { v }; |
| 197 | } |
| 198 | if lfe { |
| 199 | max_decay = 3.0; |
| 200 | } |
| 201 | |
| 202 | let start_state = enc.snapshot(); |
| 203 | let nstart = enc.front_offs(); |
| 204 | let mut old_intra = [0f32; 2 * NB_BANDS]; |
| 205 | let mut err_intra = [0f32; 2 * NB_BANDS]; |
| 206 | old_intra[..n].copy_from_slice(&old_e_bands[..n]); |
| 207 | |
| 208 | let mut badness1 = 0; |
| 209 | if two_pass || intra { |
| 210 | badness1 = quant_coarse_energy_impl( |
| 211 | start, |
| 212 | end, |
| 213 | e_bands, |
| 214 | &mut old_intra, |
| 215 | budget as i32, |
| 216 | tell as i32, |
| 217 | &E_PROB_MODEL[lm][1], |
| 218 | &mut err_intra, |
| 219 | enc, |
| 220 | c, |
| 221 | lm, |
| 222 | true, |
| 223 | max_decay, |
| 224 | lfe, |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | if !intra { |
| 229 | let tell_intra = enc.tell_frac() as i32; |
| 230 | let intra_state = enc.snapshot(); |
| 231 | let nintra = enc.front_offs(); |
| 232 | let intra_len = nintra - nstart; |
| 233 | let mut intra_bits = [0u8; MAX_FRAME_BYTES]; |
| 234 | intra_bits[..intra_len].copy_from_slice(&enc.front()[nstart..nintra]); |
| 235 | |
| 236 | enc.restore(&start_state); |
| 237 | let badness2 = quant_coarse_energy_impl( |
| 238 | start, |
| 239 | end, |
| 240 | e_bands, |
| 241 | old_e_bands, |
| 242 | budget as i32, |
| 243 | tell as i32, |
| 244 | &E_PROB_MODEL[lm][0], |
| 245 | error, |
| 246 | enc, |
| 247 | c, |
| 248 | lm, |
| 249 | false, |
| 250 | max_decay, |
| 251 | lfe, |
| 252 | ); |
| 253 | |
| 254 | if two_pass |
| 255 | && (badness1 < badness2 |
| 256 | || (badness1 == badness2 && enc.tell_frac() as i32 + intra_bias > tell_intra)) |
| 257 | { |
| 258 | enc.restore(&intra_state); |
| 259 | enc.front_write(nstart, &intra_bits[..intra_len]); |
| 260 | old_e_bands[..n].copy_from_slice(&old_intra[..n]); |
| 261 | error[..n].copy_from_slice(&err_intra[..n]); |
| 262 | intra = true; |
| 263 | } |
| 264 | } else { |
| 265 | old_e_bands[..n].copy_from_slice(&old_intra[..n]); |
| 266 | error[..n].copy_from_slice(&err_intra[..n]); |
| 267 | } |
| 268 | |
| 269 | if intra { |
| 270 | *delayed_intra = new_distortion; |
| 271 | } else { |
| 272 | *delayed_intra = PRED_COEF[lm] * PRED_COEF[lm] * *delayed_intra + new_distortion; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /// `quant_fine_energy` as celt_encode_with_ec calls it without QEXT (`prev_quant` NULL). |
| 277 | pub(crate) fn quant_fine_energy( |
| 278 | start: usize, |
| 279 | end: usize, |
| 280 | old_e_bands: &mut [f32], |
| 281 | error: &mut [f32], |
| 282 | fine_quant: &[i32], |
| 283 | enc: &mut RangeEncoder, |
| 284 | c: usize, |
| 285 | ) { |
| 286 | for i in start..end { |
| 287 | let fq = fine_quant[i]; |
| 288 | if fq <= 0 { |
| 289 | continue; |
| 290 | } |
| 291 | if enc.tell() as i32 + c as i32 * fq > enc.storage() as i32 * 8 { |
| 292 | continue; |
| 293 | } |
| 294 | let extra = 1i32 << fq; |
| 295 | for ch in 0..c { |
| 296 | let idx = i + ch * NB_BANDS; |
| 297 | let q2 = (((error[idx] + 0.5f32) * extra as f32) as f64).floor() as i32; |
| 298 | let q2 = q2.min(extra - 1).max(0); |
| 299 | enc.enc_bits(q2 as u32, fq as u32); |
| 300 | let offset = (q2 as f32 + 0.5) * (1 << (14 - fq)) as f32 * (1.0f32 / 16384.0) - 0.5; |
| 301 | old_e_bands[idx] += offset; |
| 302 | error[idx] -= offset; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /// `quant_energy_finalise`. |
| 308 | pub(crate) fn quant_energy_finalise( |
| 309 | start: usize, |
| 310 | end: usize, |
| 311 | old_e_bands: &mut [f32], |
| 312 | error: &mut [f32], |
| 313 | fine_quant: &[i32], |
| 314 | fine_priority: &[i32], |
| 315 | bits_left: i32, |
| 316 | enc: &mut RangeEncoder, |
| 317 | c: usize, |
| 318 | ) { |
| 319 | let mut bits_left = bits_left; |
| 320 | for prio in 0..2 { |
| 321 | for i in start..end { |
| 322 | if bits_left < c as i32 { |
| 323 | break; |
| 324 | } |
| 325 | if fine_quant[i] >= MAX_FINE_BITS || fine_priority[i] != prio { |
| 326 | continue; |
| 327 | } |
| 328 | for ch in 0..c { |
| 329 | let idx = i + ch * NB_BANDS; |
| 330 | let q2: u32 = if error[idx] < 0.0 { 0 } else { 1 }; |
| 331 | enc.enc_bits(q2, 1); |
| 332 | let offset = |
| 333 | (q2 as f32 - 0.5) * (1 << (14 - fine_quant[i] - 1)) as f32 * (1.0f32 / 16384.0); |
| 334 | old_e_bands[idx] += offset; |
| 335 | error[idx] -= offset; |
| 336 | bits_left -= 1; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /// `amp2Log2`; `band_e`/`band_log_e` are `c * 21`. |
| 343 | pub(crate) fn amp2log2( |
| 344 | eff_end: usize, |
| 345 | end: usize, |
| 346 | band_e: &[f32], |
| 347 | band_log_e: &mut [f32], |
| 348 | c: usize, |
| 349 | ) { |
| 350 | for ch in 0..c { |
| 351 | for i in 0..eff_end { |
| 352 | band_log_e[i + ch * NB_BANDS] = celt_log2(band_e[i + ch * NB_BANDS]) - E_MEANS[i]; |
| 353 | } |
| 354 | for i in eff_end..end { |
| 355 | band_log_e[ch * NB_BANDS + i] = -14.0; |
| 356 | } |
| 357 | } |
| 358 | } |