xref: /aosp_15_r20/external/libxaac/decoder/ixheaacd_tcx_fwd_alcnx.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
1 /******************************************************************************
2  *                                                                            *
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 #include <float.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include <string.h>
25 #include "ixheaac_type_def.h"
26 #include "ixheaacd_bitbuffer.h"
27 
28 #include "ixheaacd_interface.h"
29 
30 #include "ixheaacd_tns_usac.h"
31 #include "ixheaacd_cnst.h"
32 
33 #include "ixheaacd_acelp_info.h"
34 
35 #include "ixheaacd_td_mdct.h"
36 
37 #include "ixheaacd_sbrdecsettings.h"
38 #include "ixheaacd_info.h"
39 #include "ixheaacd_sbr_common.h"
40 #include "ixheaacd_drc_data_struct.h"
41 #include "ixheaacd_drc_dec.h"
42 #include "ixheaacd_sbrdecoder.h"
43 #include "ixheaacd_mps_polyphase.h"
44 #include "ixheaac_sbr_const.h"
45 
46 #include "ixheaacd_ec_defines.h"
47 #include "ixheaacd_ec_struct_def.h"
48 #include "ixheaacd_main.h"
49 #include "ixheaacd_arith_dec.h"
50 #include "ixheaacd_func_def.h"
51 #include "ixheaacd_windows.h"
52 #include "ixheaacd_acelp_com.h"
53 
54 #include "ixheaac_constants.h"
55 #include "ixheaac_basic_ops32.h"
56 #include "ixheaac_basic_ops40.h"
57 
58 #define ABS(A) ((A) < 0 ? (-A) : (A))
59 
ixheaacd_lpc_coeff_wt_apply(FLOAT32 * a,FLOAT32 * ap)60 VOID ixheaacd_lpc_coeff_wt_apply(FLOAT32 *a, FLOAT32 *ap) {
61   FLOAT32 f;
62   WORD32 i;
63   ap[0] = a[0];
64   f = 0.92f;
65   for (i = 1; i <= 16; i++) {
66     ap[i] = f * a[i];
67     f *= 0.92f;
68   }
69   return;
70 }
71 
ixheaacd_float2fix(FLOAT32 * x,WORD32 * int_x,WORD32 length)72 WORD8 ixheaacd_float2fix(FLOAT32 *x, WORD32 *int_x, WORD32 length) {
73   WORD32 k, itemp;
74   FLOAT32 ftemp = 0.0;
75   WORD8 shiftp;
76   for (k = 0; k < length; k++) {
77     if (ABS(x[k]) > ftemp) ftemp = ABS(x[k]);
78   }
79 
80   itemp = (WORD32)(ftemp);
81   shiftp = ixheaac_norm32(itemp);
82 
83   for (k = 0; k < length; k++) {
84     int_x[k] = (WORD32)(x[k] * (FLOAT32)((WORD64)1 << shiftp));
85   }
86 
87   return (shiftp);
88 }
89 
ixheaacd_fix2float(WORD32 * int_xn1,FLOAT32 * xn1,WORD32 length,WORD8 * shiftp,WORD32 * preshift)90 VOID ixheaacd_fix2float(WORD32 *int_xn1, FLOAT32 *xn1, WORD32 length,
91                         WORD8 *shiftp, WORD32 *preshift) {
92   WORD32 k;
93   FLOAT32 qfac;
94   if ((*shiftp - *preshift) > 0) {
95     qfac = 1.0f / (FLOAT32)((WORD64)1 << (*shiftp - *preshift));
96     for (k = 0; k < length; k++) {
97       xn1[k] = (FLOAT32)((FLOAT32)int_xn1[k] * qfac);
98     }
99   } else {
100     for (k = 0; k < length; k++) {
101       xn1[k] = (FLOAT32)((FLOAT32)int_xn1[k] *
102                          (FLOAT32)((WORD64)1 << (*preshift - *shiftp)));
103     }
104   }
105 }
106 
ixheaacd_low_fq_deemphasis(FLOAT32 x[],WORD32 lg,FLOAT32 gains[])107 static VOID ixheaacd_low_fq_deemphasis(FLOAT32 x[], WORD32 lg,
108                                        FLOAT32 gains[]) {
109   WORD32 i, j, k, i_max;
110   FLOAT32 max, factor, rm;
111 
112   k = 8;
113   i_max = lg / 4;
114 
115   max = 0.01f;
116   for (i = 0; i < i_max; i += k) {
117     rm = 0.01f;
118     for (j = i; j < i + k; j++) rm += x[j] * x[j];
119 
120     if (rm > max) max = rm;
121   }
122 
123   factor = 0.1f;
124   for (i = 0; i < i_max; i += k) {
125     rm = 0.01f;
126     for (j = i; j < i + k; j++) rm += x[j] * x[j];
127 
128     rm = (FLOAT32)sqrt(rm / max);
129     if (rm > factor) factor = rm;
130 
131     for (j = i; j < i + k; j++) x[j] *= factor;
132 
133     gains[i / k] = factor;
134   }
135 
136   return;
137 }
138 
ixheaacd_calc_max_pitch(FLOAT32 x[LEN_SUPERFRAME],WORD32 n)139 static WORD32 ixheaacd_calc_max_pitch(FLOAT32 x[LEN_SUPERFRAME], WORD32 n) {
140   FLOAT32 max_m;
141   FLOAT32 t_est;
142   WORD32 i, i_max, pitch_tcx;
143 
144   max_m = 0;
145   i_max = 1;
146 
147   for (i = 1; i < n; i++) {
148     FLOAT32 mag = (x[2 * i] * x[2 * i]) + (x[2 * i + 1] * x[2 * i + 1]);
149     if (mag > max_m) {
150       max_m = mag;
151       i_max = i;
152     }
153   }
154 
155   t_est = (n / (FLOAT32)i_max);
156 
157   if (t_est >= 256) {
158     pitch_tcx = 256;
159   } else {
160     FLOAT32 tmp_est = t_est;
161     while (tmp_est < 256) {
162       tmp_est += t_est;
163     }
164     pitch_tcx = (WORD32)(tmp_est - t_est);
165   }
166 
167   return (pitch_tcx);
168 }
169 
ixheaacd_tcx_mdct(ia_usac_data_struct * usac_data,ia_td_frame_data_struct * pstr_td_frame_data,WORD32 frame_index,FLOAT32 lp_flt_coff_a[],WORD32 lg,ia_usac_lpd_decoder_handle st)170 WORD32 ixheaacd_tcx_mdct(ia_usac_data_struct *usac_data,
171                          ia_td_frame_data_struct *pstr_td_frame_data,
172                          WORD32 frame_index, FLOAT32 lp_flt_coff_a[], WORD32 lg,
173                          ia_usac_lpd_decoder_handle st) {
174   WORD32 i, mode;
175   WORD32 *ptr_tcx_quant;
176   FLOAT32 tmp, gain_tcx = 0.0f, noise_level, energy, temp;
177   FLOAT32 *ptr_a, i_ap[ORDER + 1];
178   const FLOAT32 *sine_window_prev, *sine_window;
179   WORD32 fac_length_prev;
180   FLOAT32 alfd_gains[LEN_SUPERFRAME / (4 * 8)] = {0};
181   FLOAT32 x[LEN_SUPERFRAME], buf[ORDER + LEN_SUPERFRAME];
182   WORD32 int_x[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
183   WORD32 int_xn1[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
184   FLOAT32 gain1[LEN_SUPERFRAME], gain2[LEN_SUPERFRAME];
185   FLOAT32 xn_buf[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
186   FLOAT32 *xn;
187   FLOAT32 xn1[2 * FAC_LENGTH], facwindow[2 * FAC_LENGTH];
188   WORD32 TTT;
189   WORD8 shiftp = 0;
190   WORD32 preshift = 0;
191   WORD32 loop_count = 0;
192   FLOAT32 *exc = &usac_data->exc_buf[usac_data->len_subfrm * frame_index +
193                                      MAX_PITCH + INTER_LP_FIL_ORDER + 1];
194   FLOAT32 *synth =
195       &usac_data->synth_buf[usac_data->len_subfrm * frame_index + MAX_PITCH +
196                             (((NUM_FRAMES * usac_data->num_subfrm) / 2) - 1) *
197                                 LEN_SUBFR];
198 
199   WORD32 *ptr_scratch = &usac_data->scratch_buffer[0];
200 
201   WORD32 fac_length = (usac_data->len_subfrm) / 2;
202   WORD32 err = 0;
203 
204   mode = lg / (usac_data->len_subfrm);
205   if (mode > 2) mode = 3;
206 
207   if (st->mode_prev == -2)
208     fac_length_prev = (usac_data->ccfl) / 16;
209 
210   else
211     fac_length_prev = fac_length;
212 
213   if (fac_length == 96)
214     sine_window = ixheaacd_sine_window192;
215   else
216     sine_window = ixheaacd__sine_window256;
217 
218   if (fac_length_prev == 48)
219     sine_window_prev = ixheaacd_sine_window96;
220 
221   else if (fac_length_prev == 64)
222     sine_window_prev = ixheaacd_sine_window128;
223 
224   else if (fac_length_prev == 96)
225     sine_window_prev = ixheaacd_sine_window192;
226 
227   else
228     sine_window_prev = ixheaacd__sine_window256;
229 
230   xn = xn_buf + fac_length;
231 
232   if (st->mode_prev != 0) {
233     if (st->mode_prev > 0) {
234       for (i = 0; i < (2 * fac_length_prev); i++) {
235         st->exc_prev[i + fac_length - fac_length_prev + 1] *=
236             sine_window_prev[(2 * fac_length_prev) - 1 - i];
237       }
238     }
239     for (i = 0; i < fac_length - fac_length_prev; i++) {
240       st->exc_prev[i + fac_length + fac_length_prev + 1] = 0.0f;
241     }
242   }
243   if (usac_data->frame_ok == 1) {
244     noise_level = 0.0625f * (8.0f - ((FLOAT32)pstr_td_frame_data->noise_factor[frame_index]));
245 
246   ptr_tcx_quant = pstr_td_frame_data->x_tcx_invquant;
247   for (i = 0; i < frame_index; i++)
248     ptr_tcx_quant += pstr_td_frame_data->tcx_lg[i];
249 
250   for (i = 0; i < lg; i++) x[i] = (FLOAT32)ptr_tcx_quant[i];
251 
252     if (usac_data->ec_flag) {
253       st->last_tcx_pitch = ixheaacd_calc_max_pitch(x, (lg >> 5));
254     }
255 
256   for (i = lg / 6; i < lg; i += 8) {
257     WORD32 k, max_k = min(lg, i + 8);
258     FLOAT32 tmp = 0.0f;
259     for (k = i; k < max_k; k++) tmp += ptr_tcx_quant[k] * ptr_tcx_quant[k];
260 
261     if (tmp == 0.0f) {
262       for (k = i; k < max_k; k++)
263         x[k] = noise_level *
264                ixheaacd_randomsign(
265                    &(usac_data->seed_value[usac_data->present_chan]));
266     }
267   }
268 
269   ixheaacd_low_fq_deemphasis(x, lg, alfd_gains);
270 
271   ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (ORDER + 1), i_ap);
272   ixheaacd_lpc_to_td(i_ap, ORDER, gain1, usac_data->len_subfrm / 4);
273 
274   ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (2 * (ORDER + 1)), i_ap);
275   ixheaacd_lpc_to_td(i_ap, ORDER, gain2, usac_data->len_subfrm / 4);
276 
277   energy = 0.01f;
278   for (i = 0; i < lg; i++) energy += x[i] * x[i];
279 
280   temp = (FLOAT32)sqrt(energy) / lg;
281 
282   gain_tcx =
283       (FLOAT32)pow(
284           10.0f,
285           ((FLOAT32)pstr_td_frame_data->global_gain[frame_index]) / 28.0f) /
286       (temp * 2.0f);
287   }
288   if (usac_data->ec_flag) {
289     if (usac_data->frame_ok == 1) {
290       usac_data->past_gain_tcx[usac_data->present_chan] = gain_tcx;
291     } else {
292       gain_tcx = usac_data->past_gain_tcx[usac_data->present_chan];
293     }
294   }
295   if (usac_data->frame_ok == 1) {
296   ixheaacd_noise_shaping(x, lg, (usac_data->len_subfrm) / 4, gain1, gain2);
297   shiftp = ixheaacd_float2fix(x, int_x, lg);
298   }
299   if (usac_data->ec_flag == 1) {
300     if (st->mode_prev != 0) {
301       if (usac_data->frame_ok == 1) {
302         memcpy(usac_data->tcx_spec_coeffs[usac_data->present_chan], int_x, lg * sizeof(int_x[0]));
303         usac_data->last_shiftp = shiftp;
304       } else {
305         memcpy(int_x, usac_data->tcx_spec_coeffs[usac_data->present_chan], lg * sizeof(int_x[0]));
306         shiftp = usac_data->last_shiftp;
307       }
308     }
309   } else {
310     if (lg & (lg - 1)) {
311       if ((lg != 48) && (lg != 96) && (lg != 192) && (lg != 384) && (lg != 768)) {
312         return -1;
313       }
314     }
315   }
316 
317   ixheaacd_acelp_mdct_main(usac_data, int_x, int_xn1, (2 * fac_length), lg - (2 * fac_length),
318                            &preshift);
319 
320   ixheaacd_fix2float(int_xn1, xn_buf, (lg + (2 * fac_length)), &shiftp,
321                      &preshift);
322 
323   ixheaacd_vec_cnst_mul((2.0f / lg), xn_buf, xn_buf, lg + (2 * fac_length));
324 
325   st->fac_gain =
326       gain_tcx * 0.5f * (FLOAT32)sqrt(((FLOAT32)fac_length) / (FLOAT32)lg);
327 
328   for (i = 0; i < fac_length / 4; i++)
329     st->fac_fd_data[i] = alfd_gains[i * lg / (8 * fac_length)];
330 
331   if (st->mode_prev == 0) {
332     for (i = 0; i < fac_length_prev; i++) {
333       facwindow[i] =
334           sine_window_prev[i] * sine_window_prev[(2 * fac_length_prev) - 1 - i];
335       facwindow[fac_length_prev + i] =
336           1.0f - (sine_window_prev[fac_length_prev + i] *
337                   sine_window_prev[fac_length_prev + i]);
338     }
339 
340     for (i = 0; i < fac_length / 2; i++) {
341       x[i] = st->fac_gain *
342              (FLOAT32)pstr_td_frame_data->fac[frame_index * FAC_LENGTH + 2 * i];
343       x[fac_length / 2 + i] =
344           st->fac_gain *
345           (FLOAT32)pstr_td_frame_data
346               ->fac[frame_index * FAC_LENGTH + fac_length - 2 * i - 1];
347     }
348 
349     for (i = 0; i < fac_length / 8; i++) {
350       x[i] *= st->fac_fd_data[2 * i];
351       x[fac_length - i - 1] *= st->fac_fd_data[2 * i + 1];
352     }
353 
354     preshift = 0;
355     shiftp = ixheaacd_float2fix(x, int_x, fac_length);
356 
357     if (usac_data->ec_flag == 0) {
358       if (fac_length & (fac_length - 1)) {
359         if ((fac_length != 48) && (fac_length != 96) && (fac_length != 192) &&
360             (fac_length != 384) && (fac_length != 768)) {
361           return -1;
362         }
363       }
364     }
365 
366     ixheaacd_acelp_mdct(int_x, int_xn1, &preshift, fac_length, ptr_scratch);
367 
368     ixheaacd_fix2float(int_xn1, xn1, fac_length, &shiftp, &preshift);
369 
370     ixheaacd_vec_cnst_mul((2.0f / (FLOAT32)fac_length), xn1, xn1, fac_length);
371 
372     memset(xn1 + fac_length, 0, fac_length * sizeof(FLOAT32));
373 
374     ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (ORDER + 1), i_ap);
375     ixheaacd_synthesis_tool_float(i_ap, xn1, xn1, 2 * fac_length,
376                                   xn1 + fac_length);
377 
378     for (i = 0; i < fac_length; i++) {
379       temp = st->exc_prev[1 + fac_length + i] * facwindow[fac_length + i] +
380              st->exc_prev[fac_length - i] * facwindow[fac_length - 1 - i];
381       xn1[i] += temp;
382     }
383   }
384 
385   for (i = 0; i < lg + (2 * fac_length); i++) xn_buf[i] *= gain_tcx;
386 
387   for (i = 0; i < (2 * fac_length_prev); i++)
388     xn_buf[i + fac_length - fac_length_prev] *= sine_window_prev[i];
389 
390   for (i = 0; i < fac_length - fac_length_prev; i++) xn_buf[i] = 0.0f;
391 
392   if (st->mode_prev != 0) {
393     for (i = fac_length - fac_length_prev; i < (fac_length + fac_length_prev);
394          i++)
395       xn_buf[i] += st->exc_prev[1 + i];
396   } else {
397     for (i = fac_length - fac_length_prev; i < (fac_length + fac_length_prev);
398          i++)
399       xn_buf[i + fac_length] += xn1[i];
400   }
401 
402   ixheaacd_mem_cpy(xn_buf + lg - 1, st->exc_prev, 1 + (2 * fac_length));
403 
404   for (i = 0; i < (2 * fac_length); i++) {
405     xn_buf[i + lg] *= sine_window[(2 * fac_length) - 1 - i];
406   }
407 
408   if (st->mode_prev != 0) {
409     ixheaacd_mem_cpy(xn_buf + fac_length - fac_length_prev,
410                      synth - fac_length_prev, fac_length_prev);
411 
412     for (i = 0; i < ORDER + fac_length; i++)
413       buf[i] = synth[i - ORDER - fac_length] -
414                (PREEMPH_FILT_FAC * synth[i - ORDER - fac_length - 1]);
415 
416     ptr_a = st->lp_flt_coeff_a_prev;
417     TTT = fac_length % LEN_SUBFR;
418     if (TTT != 0)
419       ixheaacd_residual_tool_float(ptr_a, &buf[ORDER], &exc[-fac_length], TTT,
420                                    1);
421 
422     loop_count = (fac_length - TTT) / LEN_SUBFR;
423     ixheaacd_residual_tool_float(ptr_a, &buf[ORDER + TTT],
424                                  &exc[TTT - fac_length], LEN_SUBFR, loop_count);
425   }
426 
427   ixheaacd_mem_cpy(xn, synth, lg);
428 
429   ixheaacd_mem_cpy(synth - ORDER - 1, xn - ORDER - 1, ORDER + 1);
430   tmp = xn[-ORDER - 1];
431   ixheaacd_preemphsis_tool_float(xn - ORDER, PREEMPH_FILT_FAC, ORDER + lg, tmp);
432 
433   ptr_a = lp_flt_coff_a + (2 * (ORDER + 1));
434 
435   ixheaacd_residual_tool_float(ptr_a, xn, exc, lg, 1);
436 
437   ixheaacd_mem_cpy(ptr_a, st->lp_flt_coeff_a_prev, ORDER + 1);
438   ixheaacd_mem_cpy(ptr_a, st->lp_flt_coeff_a_prev + ORDER + 1, ORDER + 1);
439 
440   return err;
441 }
442 
ixheaacd_randomsign(UWORD32 * seed)443 FLOAT32 ixheaacd_randomsign(UWORD32 *seed) {
444   FLOAT32 sign = 0.0f;
445   *seed = (UWORD32)(((UWORD64)(*seed) * (UWORD64)69069) + 5);
446 
447   if (((*seed) & 0x10000) > 0)
448     sign = -1.f;
449   else
450     sign = +1.f;
451 
452   return sign;
453 }
454