forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli4 days ago4021 commits

vq.rs250 lines, 6.2 KiBHistoryRawDownload
1use super::{EPSILON, celt_atan2p_norm, celt_cos_norm, celt_rcp, celt_rsqrt, celt_sqrt};
2use crate::celt::{pvq_u, pvq_v};
3use crate::range::RangeEncoder;
4
5const SPREAD_FACTOR: [i32; 3] = [15, 10, 5];
6const MAX_N: usize = 176;
7
8fn inner_prod(x: &[f32], y: &[f32], n: usize) -> f32 {
9 let mut xy: f32 = 0.0;
10 for i in 0..n {
11 xy += x[i] * y[i];
12 }
13 xy
14}
15
16fn exp_rotation1(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) {
17 let ms = -s;
18 for i in 0..len.saturating_sub(stride) {
19 let x1 = x[i];
20 let x2 = x[i + stride];
21 x[i + stride] = c * x2 + s * x1;
22 x[i] = c * x1 + ms * x2;
23 }
24 for i in (0..len.saturating_sub(2 * stride)).rev() {
25 let x1 = x[i];
26 let x2 = x[i + stride];
27 x[i + stride] = c * x2 + s * x1;
28 x[i] = c * x1 + ms * x2;
29 }
30}
31
32/// `exp_rotation`.
33pub(crate) fn exp_rotation(
34 x: &mut [f32],
35 len: usize,
36 dir: i32,
37 stride: usize,
38 k: i32,
39 spread: i32,
40) {
41 if 2 * k >= len as i32 || spread == 0 {
42 return;
43 }
44 let factor = SPREAD_FACTOR[(spread - 1) as usize];
45 let gain = len as f32 / (len as i32 + factor * k) as f32;
46 let theta = 0.5f32 * (gain * gain);
47 let c = celt_cos_norm(theta);
48 let s = celt_cos_norm(1.0 - theta);
49 let mut stride2 = 0usize;
50 if len >= 8 * stride {
51 stride2 = 1;
52 while (stride2 * stride2 + stride2) * stride + (stride >> 2) < len {
53 stride2 += 1;
54 }
55 }
56 let len = len / stride;
57 for i in 0..stride {
58 let seg = &mut x[i * len..];
59 if dir < 0 {
60 if stride2 != 0 {
61 exp_rotation1(seg, len, stride2, s, c);
62 }
63 exp_rotation1(seg, len, 1, c, s);
64 } else {
65 exp_rotation1(seg, len, 1, c, -s);
66 if stride2 != 0 {
67 exp_rotation1(seg, len, stride2, s, -c);
68 }
69 }
70 }
71}
72
73fn normalise_residual(iy: &[i32], x: &mut [f32], n: usize, ryy: f32, gain: f32) {
74 let g = celt_rsqrt(ryy) * gain;
75 for i in 0..n {
76 x[i] = iy[i] as f32 * g;
77 }
78}
79
80fn extract_collapse_mask(iy: &[i32], n: usize, b: usize) -> u32 {
81 if b <= 1 {
82 return 1;
83 }
84 let n0 = n / b;
85 let mut mask = 0u32;
86 for i in 0..b {
87 let mut tmp = 0u32;
88 for j in 0..n0 {
89 tmp |= iy[i * n0 + j] as u32;
90 }
91 mask |= ((tmp != 0) as u32) << i;
92 }
93 mask
94}
95
96fn op_pvq_search(x: &mut [f32], iy: &mut [i32], k: i32, n: usize) -> f32 {
97 let mut y_buf = [0f32; MAX_N];
98 let mut signx_buf = [0i32; MAX_N];
99 let y = &mut y_buf[..n];
100 let signx = &mut signx_buf[..n];
101 for j in 0..n {
102 signx[j] = (x[j] < 0.0) as i32;
103 x[j] = x[j].abs();
104 iy[j] = 0;
105 y[j] = 0.0;
106 }
107
108 let mut xy: f32 = 0.0;
109 let mut yy: f32 = 0.0;
110 let mut pulses_left = k;
111
112 if k > (n >> 1) as i32 {
113 let mut sum: f32 = 0.0;
114 for j in 0..n {
115 sum += x[j];
116 }
117 if !(sum > EPSILON && sum < 64.0) {
118 x[0] = 1.0;
119 for j in 1..n {
120 x[j] = 0.0;
121 }
122 sum = 1.0;
123 }
124 let rcp = (k as f32 + 0.8f32) * celt_rcp(sum);
125 for j in 0..n {
126 iy[j] = ((rcp * x[j]) as f64).floor() as i32;
127 y[j] = iy[j] as f32;
128 yy += y[j] * y[j];
129 xy += x[j] * y[j];
130 y[j] *= 2.0;
131 pulses_left -= iy[j];
132 }
133 }
134
135 if pulses_left > n as i32 + 3 {
136 let tmp = pulses_left as f32;
137 yy += tmp * tmp;
138 yy += tmp * y[0];
139 iy[0] += pulses_left;
140 pulses_left = 0;
141 }
142
143 for _ in 0..pulses_left {
144 let mut best_id = 0usize;
145 yy += 1.0;
146 let mut rxy = xy + x[0];
147 let mut ryy = yy + y[0];
148 rxy = rxy * rxy;
149 let mut best_den = ryy;
150 let mut best_num = rxy;
151 for j in 1..n {
152 rxy = xy + x[j];
153 ryy = yy + y[j];
154 rxy = rxy * rxy;
155 if best_den * rxy > ryy * best_num {
156 best_den = ryy;
157 best_num = rxy;
158 best_id = j;
159 }
160 }
161 xy += x[best_id];
162 yy += y[best_id];
163 y[best_id] += 2.0;
164 iy[best_id] += 1;
165 }
166
167 for j in 0..n {
168 if signx[j] != 0 {
169 iy[j] = -iy[j];
170 }
171 }
172 yy
173}
174
175fn icwrs(n: usize, y: &[i32]) -> u32 {
176 let mut j = n - 1;
177 let mut i = (y[j] < 0) as u32;
178 let mut k = y[j].unsigned_abs() as usize;
179 loop {
180 j -= 1;
181 i = i.wrapping_add(pvq_u(n - j, k));
182 k += y[j].unsigned_abs() as usize;
183 if y[j] < 0 {
184 i = i.wrapping_add(pvq_u(n - j, k + 1));
185 }
186 if j == 0 {
187 break;
188 }
189 }
190 i
191}
192
193fn encode_pulses(y: &[i32], n: usize, k: i32, enc: &mut RangeEncoder) {
194 enc.enc_uint(icwrs(n, y), pvq_v(n, k as usize));
195}
196
197/// `alg_quant` (encoder, no QEXT); returns the collapse mask.
198pub(crate) fn alg_quant(
199 x: &mut [f32],
200 n: usize,
201 k: i32,
202 spread: i32,
203 b: i32,
204 enc: &mut RangeEncoder,
205 gain: f32,
206 resynth: bool,
207) -> u32 {
208 let b = b as usize;
209 let mut iy_buf = [0i32; MAX_N + 3];
210 let iy = &mut iy_buf[..n + 3];
211 exp_rotation(x, n, 1, b, k, spread);
212 let yy = op_pvq_search(x, iy, k, n);
213 let collapse_mask = extract_collapse_mask(iy, n, b);
214 encode_pulses(iy, n, k, enc);
215 if resynth {
216 normalise_residual(iy, x, n, yy, gain);
217 exp_rotation(x, n, -1, b, k, spread);
218 }
219 collapse_mask
220}
221
222/// `stereo_itheta`.
223pub(crate) fn stereo_itheta(x: &[f32], y: &[f32], stereo: bool, n: usize) -> i32 {
224 let mut emid: f32 = 0.0;
225 let mut eside: f32 = 0.0;
226 if stereo {
227 for i in 0..n {
228 let m = x[i] + y[i];
229 let s = x[i] - y[i];
230 emid += m * m;
231 eside += s * s;
232 }
233 } else {
234 emid += inner_prod(x, x, n);
235 eside += inner_prod(y, y, n);
236 }
237 let mid = celt_sqrt(emid);
238 let side = celt_sqrt(eside);
239 let v = 0.5f32 + (65536.0f32 * 16384.0f32) * celt_atan2p_norm(side, mid);
240 (v as f64).floor() as i32
241}
242
243/// `renormalise_vector`.
244pub(crate) fn renormalise_vector(x: &mut [f32], n: usize, gain: f32) {
245 let e = EPSILON + inner_prod(x, x, n);
246 let g = celt_rsqrt(e) * gain;
247 for i in 0..n {
248 x[i] *= g;
249 }
250}