forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

post.rs370 lines, 11.3 KiBHistoryRawDownload
1//! AMR-NB post-processing chain: syn_filt, residu, weight_ai, a_refl, preemphasis,
2//! agc, post_filter, post_process (bit-exact port of opencore-amr).
3#![allow(clippy::all)]
4#![allow(dead_code)]
5
6use super::basic::*;
7use super::tables::Mode;
8
9const M: usize = 10;
10const MP1: usize = 11;
11const L_SUBFR: usize = 40;
12const L_FRAME: usize = 160;
13const L_H: usize = 22;
14const MU: i32 = 26214;
15const AGC_FAC: i16 = 29491;
16
17static GAMMA3_MR122: [i16; M] = [22938, 16057, 11240, 7868, 5508, 3856, 2699, 1889, 1322, 925];
18static GAMMA3: [i16; M] = [18022, 9912, 5451, 2998, 1649, 907, 499, 274, 151, 83];
19static GAMMA4_MR122: [i16; M] = [24576, 18432, 13824, 10368, 7776, 5832, 4374, 3281, 2461, 1846];
20static GAMMA4: [i16; M] = [22938, 16057, 11240, 7868, 5508, 3856, 2699, 1889, 1322, 925];
21
22#[inline]
23fn sat12(s: i32) -> i16 {
24 if (s.wrapping_add(134217728) as u32) < 0x0fff_ffff {
25 (s >> 12) as i16
26 } else if s > 0x07ff_ffff {
27 32767
28 } else {
29 -32768
30 }
31}
32
33/// C Syn_filt: y[i] = x[i] - sum a[j]*y[i-j] (a has M+1 coefs, lg <= 80).
34/// x and y are separate slices (len >= lg); mem is the M-sample filter memory,
35/// updated with the last M outputs when `update` is true.
36pub fn syn_filt(a: &[i16], x: &[i16], y: &mut [i16], lg: usize, mem: &mut [i16], update: bool) {
37 let mut tmp = [0i16; M + 80];
38 tmp[..M].copy_from_slice(&mem[..M]);
39 for i in 0..lg {
40 let mut s: i32 = (x[i] as i32).wrapping_mul(a[0] as i32).wrapping_add(0x800);
41 for j in 1..=M {
42 s = s.wrapping_sub((a[j] as i32).wrapping_mul(tmp[M + i - j] as i32));
43 }
44 let v = sat12(s);
45 tmp[M + i] = v;
46 y[i] = v;
47 }
48 if update {
49 mem[..M].copy_from_slice(&y[lg - M..lg]);
50 }
51}
52
53/// C Residu: y[i] = sum_{j=0..M} a[j]*x[xoff+i-j] (>>12, truncating). x[xoff-M..xoff]
54/// holds the history; y is a separate slice of length >= lg.
55pub fn residu(a: &[i16], x: &[i16], xoff: usize, y: &mut [i16], lg: usize) {
56 for i in 0..lg {
57 let mut s: i32 = 0x800;
58 for j in 0..=M {
59 s = s.wrapping_add((a[j] as i32).wrapping_mul(x[xoff + i - j] as i32));
60 }
61 y[i] = (s >> 12) as i16;
62 }
63}
64
65/// C Weight_Ai: a_exp[0]=a[0]; a_exp[i] = (a[i]*fac[i-1] + 0x4000) >> 15, i=1..=M.
66pub fn weight_ai(a: &[i16], fac: &[i16], a_exp: &mut [i16]) {
67 a_exp[0] = a[0];
68 for i in 1..=M {
69 a_exp[i] = (((a[i] as i32) * (fac[i - 1] as i32) + 0x4000) >> 15) as i16;
70 }
71}
72
73/// C A_Refl: direct form a[0..M] (note: M coefficients, without a[0]=1 term) to
74/// reflection coefficients refl[0..M]. Reproduces opencore's control flow.
75pub fn a_refl(a: &[i16], refl: &mut [i16]) {
76 let mut a_state = [0i16; M];
77 let mut b_state = [0i16; M];
78 a_state.copy_from_slice(&a[..M]);
79 let mut i = M as i32 - 1;
80 while i >= 0 {
81 let iu = i as usize;
82 if abs_s(a_state[iu]) >= 4096 {
83 for k in 0..M {
84 refl[k] = 0;
85 }
86 break;
87 }
88 refl[iu] = shl(a_state[iu], 3);
89 let l_temp = l_mult(refl[iu], refl[iu]);
90 let mut l_acc = l_sub(i32::MAX, l_temp);
91 let norm_shift = norm_l(l_acc);
92 let scale = 15 - norm_shift;
93 l_acc = l_shl(l_acc, norm_shift);
94 // opencore's pv_round(L_add) loses its saturation check here when built with optimisation
95 // (signed-overflow UB), so the reference output wraps for l_acc close to i32::MAX.
96 let norm_prod = (l_acc.wrapping_add(0x8000) >> 16) as i16;
97 let mult_ = div_s(16384, norm_prod);
98 for j in 0..iu {
99 let mut l_acc = (a_state[j] as i32).wrapping_shl(16);
100 l_acc = l_msu(l_acc, refl[iu], a_state[iu - j - 1]);
101 let temp = round_(l_acc);
102 let mut l_temp = l_mult(mult_, temp);
103 l_temp = l_shr_r(l_temp, scale);
104 let t = l_temp.wrapping_sub((l_temp < 0) as i32);
105 let t_abs = t ^ (t >> 31);
106 if t_abs > 32767 {
107 for k in 0..M {
108 refl[k] = 0;
109 }
110 break;
111 }
112 b_state[j] = l_temp as i16;
113 }
114 for j in 0..iu {
115 a_state[j] = b_state[j];
116 }
117 i -= 1;
118 }
119}
120
121/// C preemphasisState.
122#[derive(Clone, Debug, Default)]
123pub struct PreemphasisState {
124 pub mem_pre: i16,
125}
126
127impl PreemphasisState {
128 pub fn new() -> Self {
129 Self { mem_pre: 0 }
130 }
131 pub fn reset(&mut self) {
132 self.mem_pre = 0;
133 }
134}
135
136/// C preemphasis: in-place signal[0..l] -= g*signal[i-1] (mem for i=0).
137pub fn preemphasis(st: &mut PreemphasisState, signal: &mut [i16], g: i16, l: usize) {
138 let temp = signal[l - 1];
139 for i in (1..l).rev() {
140 let t2 = mult(g, signal[i - 1]);
141 signal[i] = sub(signal[i], t2);
142 }
143 let t2 = mult(g, st.mem_pre);
144 signal[0] = sub(signal[0], t2);
145 st.mem_pre = temp;
146}
147
148/// C agcState.
149#[derive(Clone, Debug)]
150pub struct AgcState {
151 pub past_gain: i16,
152}
153
154impl AgcState {
155 pub fn new() -> Self {
156 Self { past_gain: 4096 }
157 }
158 pub fn reset(&mut self) {
159 self.past_gain = 4096;
160 }
161}
162
163fn energy_new(inp: &[i16], l: usize) -> i32 {
164 let mut s: i32 = 0;
165 for i in 0..l {
166 s = l_mac(s, inp[i], inp[i]);
167 }
168 if s != i32::MAX {
169 s >>= 4;
170 } else {
171 // C energy_old
172 s = 0;
173 for &x in &inp[..l] {
174 let t = x >> 2;
175 s = l_mac(s, t, t);
176 }
177 }
178 s
179}
180
181/// C agc: scale sig_out (in place, len l) to the energy of sig_in.
182pub fn agc(st: &mut AgcState, sig_in: &[i16], sig_out: &mut [i16], agc_fac: i16, l: usize) {
183 let mut s = energy_new(&sig_out[..l], l);
184 if s == 0 {
185 st.past_gain = 0;
186 return;
187 }
188 let mut exp = norm_l(s) - 1;
189 let l_temp = l_shl(s, exp);
190 let gain_out = round_(l_temp);
191 s = energy_new(&sig_in[..l], l);
192 let g0: i16;
193 if s == 0 {
194 g0 = 0;
195 } else {
196 let i = norm_l(s);
197 let l_temp = s.wrapping_shl(i as u32);
198 let gain_in = round_(l_temp);
199 exp -= i;
200 let temp = div_s(gain_out, gain_in);
201 s = (temp as i32).wrapping_shl(7);
202 s = l_shr(s, exp);
203 s = inv_sqrt(s);
204 let l_temp = s.wrapping_shl(9);
205 let i = (l_temp.wrapping_add(0x8000) >> 16) as i16;
206 let temp = 32767 - agc_fac;
207 g0 = (((i as i32) * (temp as i32)) >> 15) as i16;
208 }
209 let mut gain = st.past_gain;
210 for i in 0..l {
211 gain = (((gain as i32) * (agc_fac as i32)) >> 15) as i16;
212 gain = gain.wrapping_add(g0);
213 let l_temp = ((sig_out[i] as i32) * (gain as i32)).wrapping_shl(1);
214 sig_out[i] = (l_temp >> 13) as i16;
215 }
216 st.past_gain = gain;
217}
218
219/// C Post_FilterState. synth_buf[0..M] is history, current frame at synth_buf[M..].
220#[derive(Clone, Debug)]
221pub struct PostFilterState {
222 pub res2: [i16; L_SUBFR],
223 pub mem_syn_pst: [i16; M],
224 pub preemph_state: PreemphasisState,
225 pub agc_state: AgcState,
226 pub synth_buf: [i16; M + L_FRAME],
227}
228
229impl PostFilterState {
230 pub fn new() -> Self {
231 Self {
232 res2: [0; L_SUBFR],
233 mem_syn_pst: [0; M],
234 preemph_state: PreemphasisState::new(),
235 agc_state: AgcState::new(),
236 synth_buf: [0; M + L_FRAME],
237 }
238 }
239 pub fn reset(&mut self) {
240 *self = Self::new();
241 }
242}
243
244/// C Post_Filter: syn[0..160] is the synthesis speech, replaced by the postfiltered
245/// output; az_4 holds 4 subframes of MP1(11) LPC coefs each (44 values).
246pub fn post_filter(st: &mut PostFilterState, mode: Mode, syn: &mut [i16], az_4: &[i16]) {
247 st.synth_buf[M..M + L_FRAME].copy_from_slice(&syn[..L_FRAME]);
248 let mut ap3 = [0i16; MP1];
249 let mut ap4 = [0i16; MP1];
250 let mut i_subfr = 0;
251 let mut az_off = 0;
252 while i_subfr < L_FRAME {
253 let az = &az_4[az_off..az_off + MP1];
254 if mode == Mode::Mr122 || mode == Mode::Mr102 {
255 weight_ai(az, &GAMMA3_MR122, &mut ap3);
256 weight_ai(az, &GAMMA4_MR122, &mut ap4);
257 } else {
258 weight_ai(az, &GAMMA3, &mut ap3);
259 weight_ai(az, &GAMMA4, &mut ap4);
260 }
261 residu(&ap3, &st.synth_buf, M + i_subfr, &mut st.res2, L_SUBFR);
262
263 // impulse response of A(z/0.7)/A(z/0.75)
264 let mut h = [0i16; L_H];
265 h[..MP1].copy_from_slice(&ap3);
266 let hx = h;
267 let mut zmem = [0i16; M];
268 syn_filt(&ap4, &hx, &mut h, L_H, &mut zmem, false);
269
270 // 1st correlation
271 let mut l_tmp: i32 = 0;
272 for i in (0..L_H).rev() {
273 let l2 = (h[i] as i32) * (h[i] as i32);
274 if l2 != 0x4000_0000 {
275 l_tmp = l_add(l_tmp, l2 << 1);
276 } else {
277 break;
278 }
279 }
280 let temp1 = (l_tmp >> 16) as i16;
281 l_tmp = 0;
282 for i in (0..L_H - 1).rev() {
283 let l2 = (h[i] as i32) * (h[i + 1] as i32);
284 if l2 != 0x4000_0000 {
285 l_tmp = l_add(l_tmp, l2 << 1);
286 } else {
287 break;
288 }
289 }
290 let mut temp2 = (l_tmp >> 16) as i16;
291 if temp2 <= 0 {
292 temp2 = 0;
293 } else {
294 let mut t = ((temp2 as i32) * MU) >> 15;
295 if t & 0x0001_0000 != 0 {
296 t |= 0xffff_0000u32 as i32;
297 }
298 temp2 = t as i16;
299 temp2 = div_s(temp2, temp1);
300 }
301 preemphasis(&mut st.preemph_state, &mut st.res2, temp2, L_SUBFR);
302
303 let res2 = st.res2;
304 syn_filt(
305 &ap4,
306 &res2,
307 &mut syn[i_subfr..i_subfr + L_SUBFR],
308 L_SUBFR,
309 &mut st.mem_syn_pst,
310 true,
311 );
312 agc(
313 &mut st.agc_state,
314 &st.synth_buf[M + i_subfr..M + i_subfr + L_SUBFR],
315 &mut syn[i_subfr..i_subfr + L_SUBFR],
316 AGC_FAC,
317 L_SUBFR,
318 );
319 az_off += MP1;
320 i_subfr += L_SUBFR;
321 }
322 st.synth_buf.copy_within(L_FRAME..L_FRAME + M, 0);
323}
324
325/// C Post_ProcessState (2nd order high-pass + x2 upscaling).
326#[derive(Clone, Debug, Default)]
327pub struct PostProcessState {
328 pub y2_hi: i16,
329 pub y2_lo: i16,
330 pub y1_hi: i16,
331 pub y1_lo: i16,
332 pub x0: i16,
333 pub x1: i16,
334}
335
336impl PostProcessState {
337 pub fn new() -> Self {
338 Self::default()
339 }
340 pub fn reset(&mut self) {
341 *self = Self::default();
342 }
343}
344
345/// C Post_Process: in-place high-pass filter and upscale of signal[0..lg].
346pub fn post_process(st: &mut PostProcessState, signal: &mut [i16], lg: usize) {
347 const C_A1: i32 = 15836;
348 const C_A2: i32 = -7667;
349 const C_B0: i32 = 7699;
350 const C_B1: i32 = -15398;
351 const C_B2: i32 = 7699;
352 for p in signal[..lg].iter_mut() {
353 let x2 = st.x1;
354 st.x1 = st.x0;
355 st.x0 = *p;
356 let mut l_tmp: i32 = (st.y1_hi as i32).wrapping_mul(C_A1);
357 l_tmp = l_tmp.wrapping_add(((st.y1_lo as i32).wrapping_mul(C_A1)) >> 15);
358 l_tmp = l_tmp.wrapping_add((st.y2_hi as i32).wrapping_mul(C_A2));
359 l_tmp = l_tmp.wrapping_add(((st.y2_lo as i32).wrapping_mul(C_A2)) >> 15);
360 l_tmp = l_tmp.wrapping_add((st.x0 as i32).wrapping_mul(C_B0));
361 l_tmp = l_tmp.wrapping_add((st.x1 as i32).wrapping_mul(C_B1));
362 l_tmp = l_tmp.wrapping_add((x2 as i32).wrapping_mul(C_B2));
363 l_tmp = l_shl(l_tmp, 3);
364 *p = round_(l_shl(l_tmp, 1));
365 st.y2_hi = st.y1_hi;
366 st.y2_lo = st.y1_lo;
367 st.y1_hi = (l_tmp >> 16) as i16;
368 st.y1_lo = ((l_tmp >> 1).wrapping_sub((st.y1_hi as i32) << 15)) as i16;
369 }
370}