• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ixheaacd_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 "ixheaacd_sbr_const.h"
45 
46 #include "ixheaacd_main.h"
47 #include "ixheaacd_arith_dec.h"
48 #include "ixheaacd_func_def.h"
49 #include "ixheaacd_windows.h"
50 #include "ixheaacd_acelp_com.h"
51 
52 #include "ixheaacd_constants.h"
53 #include "ixheaacd_basic_ops32.h"
54 #include "ixheaacd_basic_ops40.h"
55 
56 static FLOAT32 ixheaacd_randomsign(UWORD32 *seed);
57 #define ABS(A) ((A) < 0 ? (-A) : (A))
58 
ixheaacd_lpc_coeff_wt_apply(FLOAT32 * a,FLOAT32 * ap)59 VOID ixheaacd_lpc_coeff_wt_apply(FLOAT32 *a, FLOAT32 *ap) {
60   FLOAT32 f;
61   WORD32 i;
62   ap[0] = a[0];
63   f = 0.92f;
64   for (i = 1; i <= 16; i++) {
65     ap[i] = f * a[i];
66     f *= 0.92f;
67   }
68   return;
69 }
70 
ixheaacd_float2fix(FLOAT32 * x,WORD32 * int_x,WORD32 length)71 WORD8 ixheaacd_float2fix(FLOAT32 *x, WORD32 *int_x, WORD32 length) {
72   WORD32 k, itemp;
73   FLOAT32 ftemp = 0.0;
74   WORD8 shiftp;
75   for (k = 0; k < length; k++) {
76     if (ABS(x[k]) > ftemp) ftemp = ABS(x[k]);
77   }
78 
79   itemp = (WORD32)(ftemp);
80   shiftp = ixheaacd_norm32(itemp);
81 
82   for (k = 0; k < length; k++) {
83     int_x[k] = (WORD32)(x[k] * (FLOAT32)((WORD64)1 << shiftp));
84   }
85 
86   return (shiftp);
87 }
88 
ixheaacd_fix2float(WORD32 * int_xn1,FLOAT32 * xn1,WORD32 length,WORD8 * shiftp,WORD32 * preshift)89 VOID ixheaacd_fix2float(WORD32 *int_xn1, FLOAT32 *xn1, WORD32 length,
90                         WORD8 *shiftp, WORD32 *preshift) {
91   WORD32 k;
92   FLOAT32 qfac;
93   if ((*shiftp - *preshift) > 0) {
94     qfac = 1.0f / (FLOAT32)((WORD64)1 << (*shiftp - *preshift));
95     for (k = 0; k < length; k++) {
96       xn1[k] = (FLOAT32)((FLOAT32)int_xn1[k] * qfac);
97     }
98   } else {
99     for (k = 0; k < length; k++) {
100       xn1[k] = (FLOAT32)((FLOAT32)int_xn1[k] *
101                          (FLOAT32)((WORD64)1 << (*preshift - *shiftp)));
102     }
103   }
104 }
105 
ixheaacd_low_fq_deemphasis(FLOAT32 x[],WORD32 lg,FLOAT32 gains[])106 static VOID ixheaacd_low_fq_deemphasis(FLOAT32 x[], WORD32 lg,
107                                        FLOAT32 gains[]) {
108   WORD32 i, j, k, i_max;
109   FLOAT32 max, factor, rm;
110 
111   k = 8;
112   i_max = lg / 4;
113 
114   max = 0.01f;
115   for (i = 0; i < i_max; i += k) {
116     rm = 0.01f;
117     for (j = i; j < i + k; j++) rm += x[j] * x[j];
118 
119     if (rm > max) max = rm;
120   }
121 
122   factor = 0.1f;
123   for (i = 0; i < i_max; i += k) {
124     rm = 0.01f;
125     for (j = i; j < i + k; j++) rm += x[j] * x[j];
126 
127     rm = (FLOAT32)sqrt(rm / max);
128     if (rm > factor) factor = rm;
129 
130     for (j = i; j < i + k; j++) x[j] *= factor;
131 
132     gains[i / k] = factor;
133   }
134 
135   return;
136 }
137 
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)138 WORD32 ixheaacd_tcx_mdct(ia_usac_data_struct *usac_data,
139                          ia_td_frame_data_struct *pstr_td_frame_data,
140                          WORD32 frame_index, FLOAT32 lp_flt_coff_a[], WORD32 lg,
141                          ia_usac_lpd_decoder_handle st) {
142   WORD32 i, mode;
143   WORD32 *ptr_tcx_quant;
144   FLOAT32 tmp, gain_tcx, noise_level, energy, temp;
145   FLOAT32 *ptr_a, i_ap[ORDER + 1];
146   const FLOAT32 *sine_window_prev, *sine_window;
147   WORD32 fac_length_prev;
148   FLOAT32 alfd_gains[LEN_SUPERFRAME / (4 * 8)];
149   FLOAT32 x[LEN_SUPERFRAME], buf[ORDER + LEN_SUPERFRAME];
150   WORD32 int_x[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
151   WORD32 int_xn1[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
152   FLOAT32 gain1[LEN_SUPERFRAME], gain2[LEN_SUPERFRAME];
153   FLOAT32 xn_buf[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
154   FLOAT32 *xn;
155   FLOAT32 xn1[2 * FAC_LENGTH], facwindow[2 * FAC_LENGTH];
156   WORD32 TTT;
157   WORD8 shiftp;
158   WORD32 preshift = 0;
159   WORD32 loop_count = 0;
160   FLOAT32 *exc = &usac_data->exc_buf[usac_data->len_subfrm * frame_index +
161                                      MAX_PITCH + INTER_LP_FIL_ORDER + 1];
162   FLOAT32 *synth =
163       &usac_data->synth_buf[usac_data->len_subfrm * frame_index + MAX_PITCH +
164                             (((NUM_FRAMES * usac_data->num_subfrm) / 2) - 1) *
165                                 LEN_SUBFR];
166 
167   WORD32 *ptr_scratch = &usac_data->scratch_buffer[0];
168 
169   WORD32 fac_length = (usac_data->len_subfrm) / 2;
170   WORD32 err = 0;
171 
172   mode = lg / (usac_data->len_subfrm);
173   if (mode > 2) mode = 3;
174 
175   if (st->mode_prev == -2)
176     fac_length_prev = (usac_data->ccfl) / 16;
177 
178   else
179     fac_length_prev = fac_length;
180 
181   if (fac_length == 96)
182     sine_window = ixheaacd_sine_window192;
183   else
184     sine_window = ixheaacd__sine_window256;
185 
186   if (fac_length_prev == 48)
187     sine_window_prev = ixheaacd_sine_window96;
188 
189   else if (fac_length_prev == 64)
190     sine_window_prev = ixheaacd_sine_window128;
191 
192   else if (fac_length_prev == 96)
193     sine_window_prev = ixheaacd_sine_window192;
194 
195   else
196     sine_window_prev = ixheaacd__sine_window256;
197 
198   xn = xn_buf + fac_length;
199 
200   if (st->mode_prev != 0) {
201     if (st->mode_prev > 0) {
202       for (i = 0; i < (2 * fac_length_prev); i++) {
203         st->exc_prev[i + fac_length - fac_length_prev + 1] *=
204             sine_window_prev[(2 * fac_length_prev) - 1 - i];
205       }
206     }
207     for (i = 0; i < fac_length - fac_length_prev; i++) {
208       st->exc_prev[i + fac_length + fac_length_prev + 1] = 0.0f;
209     }
210   }
211 
212   noise_level =
213       0.0625f *
214       (8.0f - ((FLOAT32)pstr_td_frame_data->noise_factor[frame_index]));
215 
216   ptr_tcx_quant = pstr_td_frame_data->x_tcx_invquant;
217   for (i = 0; i < frame_index; i++)
218     ptr_tcx_quant += pstr_td_frame_data->tcx_lg[i];
219 
220   for (i = 0; i < lg; i++) x[i] = (FLOAT32)ptr_tcx_quant[i];
221 
222   for (i = lg / 6; i < lg; i += 8) {
223     WORD32 k, max_k = min(lg, i + 8);
224     FLOAT32 tmp = 0.0f;
225     for (k = i; k < max_k; k++) tmp += ptr_tcx_quant[k] * ptr_tcx_quant[k];
226 
227     if (tmp == 0.0f) {
228       for (k = i; k < max_k; k++)
229         x[k] = noise_level *
230                ixheaacd_randomsign(
231                    &(usac_data->seed_value[usac_data->present_chan]));
232     }
233   }
234 
235   ixheaacd_low_fq_deemphasis(x, lg, alfd_gains);
236 
237   ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (ORDER + 1), i_ap);
238   err = ixheaacd_lpc_to_td(i_ap, ORDER, gain1, usac_data->len_subfrm / 4);
239   if (err) return err;
240 
241   ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (2 * (ORDER + 1)), i_ap);
242   err = ixheaacd_lpc_to_td(i_ap, ORDER, gain2, usac_data->len_subfrm / 4);
243   if (err) return err;
244 
245   energy = 0.01f;
246   for (i = 0; i < lg; i++) energy += x[i] * x[i];
247 
248   temp = (FLOAT32)sqrt(energy) / lg;
249 
250   gain_tcx =
251       (FLOAT32)pow(
252           10.0f,
253           ((FLOAT32)pstr_td_frame_data->global_gain[frame_index]) / 28.0f) /
254       (temp * 2.0f);
255 
256   ixheaacd_noise_shaping(x, lg, (usac_data->len_subfrm) / 4, gain1, gain2);
257 
258   shiftp = ixheaacd_float2fix(x, int_x, lg);
259 
260   err = ixheaacd_acelp_mdct_main(usac_data, int_x, int_xn1, (2 * fac_length),
261                                  lg - (2 * fac_length), &preshift);
262   if (err == -1) return err;
263 
264   ixheaacd_fix2float(int_xn1, xn_buf, (lg + (2 * fac_length)), &shiftp,
265                      &preshift);
266 
267   ixheaacd_vec_cnst_mul((2.0f / lg), xn_buf, xn_buf, lg + (2 * fac_length));
268 
269   st->fac_gain =
270       gain_tcx * 0.5f * (FLOAT32)sqrt(((FLOAT32)fac_length) / (FLOAT32)lg);
271 
272   for (i = 0; i < fac_length / 4; i++)
273     st->fac_fd_data[i] = alfd_gains[i * lg / (8 * fac_length)];
274 
275   if (st->mode_prev == 0) {
276     for (i = 0; i < fac_length_prev; i++) {
277       facwindow[i] =
278           sine_window_prev[i] * sine_window_prev[(2 * fac_length_prev) - 1 - i];
279       facwindow[fac_length_prev + i] =
280           1.0f - (sine_window_prev[fac_length_prev + i] *
281                   sine_window_prev[fac_length_prev + i]);
282     }
283 
284     for (i = 0; i < fac_length / 2; i++) {
285       x[i] = st->fac_gain *
286              (FLOAT32)pstr_td_frame_data->fac[frame_index * FAC_LENGTH + 2 * i];
287       x[fac_length / 2 + i] =
288           st->fac_gain *
289           (FLOAT32)pstr_td_frame_data
290               ->fac[frame_index * FAC_LENGTH + fac_length - 2 * i - 1];
291     }
292 
293     for (i = 0; i < fac_length / 8; i++) {
294       x[i] *= st->fac_fd_data[2 * i];
295       x[fac_length - i - 1] *= st->fac_fd_data[2 * i + 1];
296     }
297 
298     preshift = 0;
299     shiftp = ixheaacd_float2fix(x, int_x, fac_length);
300 
301     err =
302         ixheaacd_acelp_mdct(int_x, int_xn1, &preshift, fac_length, ptr_scratch);
303     if (err == -1) return err;
304 
305     ixheaacd_fix2float(int_xn1, xn1, fac_length, &shiftp, &preshift);
306 
307     ixheaacd_vec_cnst_mul((2.0f / (FLOAT32)fac_length), xn1, xn1, fac_length);
308 
309     memset(xn1 + fac_length, 0, fac_length * sizeof(FLOAT32));
310 
311     ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (ORDER + 1), i_ap);
312     ixheaacd_synthesis_tool_float(i_ap, xn1, xn1, 2 * fac_length,
313                                   xn1 + fac_length);
314 
315     for (i = 0; i < fac_length; i++) {
316       temp = st->exc_prev[1 + fac_length + i] * facwindow[fac_length + i] +
317              st->exc_prev[fac_length - i] * facwindow[fac_length - 1 - i];
318       xn1[i] += temp;
319     }
320   }
321 
322   for (i = 0; i < lg + (2 * fac_length); i++) xn_buf[i] *= gain_tcx;
323 
324   for (i = 0; i < (2 * fac_length_prev); i++)
325     xn_buf[i + fac_length - fac_length_prev] *= sine_window_prev[i];
326 
327   for (i = 0; i < fac_length - fac_length_prev; i++) xn_buf[i] = 0.0f;
328 
329   if (st->mode_prev != 0) {
330     for (i = fac_length - fac_length_prev; i < (fac_length + fac_length_prev);
331          i++)
332       xn_buf[i] += st->exc_prev[1 + i];
333   } else {
334     for (i = fac_length - fac_length_prev; i < (fac_length + fac_length_prev);
335          i++)
336       xn_buf[i + fac_length] += xn1[i];
337   }
338 
339   ixheaacd_mem_cpy(xn_buf + lg - 1, st->exc_prev, 1 + (2 * fac_length));
340 
341   for (i = 0; i < (2 * fac_length); i++) {
342     xn_buf[i + lg] *= sine_window[(2 * fac_length) - 1 - i];
343   }
344 
345   if (st->mode_prev != 0) {
346     ixheaacd_mem_cpy(xn_buf + fac_length - fac_length_prev,
347                      synth - fac_length_prev, fac_length_prev);
348 
349     for (i = 0; i < ORDER + fac_length; i++)
350       buf[i] = synth[i - ORDER - fac_length] -
351                (PREEMPH_FILT_FAC * synth[i - ORDER - fac_length - 1]);
352 
353     ptr_a = st->lp_flt_coeff_a_prev;
354     TTT = fac_length % LEN_SUBFR;
355     if (TTT != 0)
356       ixheaacd_residual_tool_float(ptr_a, &buf[ORDER], &exc[-fac_length], TTT,
357                                    1);
358 
359     loop_count = (fac_length - TTT) / LEN_SUBFR;
360     ixheaacd_residual_tool_float(ptr_a, &buf[ORDER + TTT],
361                                  &exc[TTT - fac_length], LEN_SUBFR, loop_count);
362   }
363 
364   ixheaacd_mem_cpy(xn, synth, lg);
365 
366   ixheaacd_mem_cpy(synth - ORDER - 1, xn - ORDER - 1, ORDER + 1);
367   tmp = xn[-ORDER - 1];
368   ixheaacd_preemphsis_tool_float(xn - ORDER, PREEMPH_FILT_FAC, ORDER + lg, tmp);
369 
370   ptr_a = lp_flt_coff_a + (2 * (ORDER + 1));
371 
372   ixheaacd_residual_tool_float(ptr_a, xn, exc, lg, 1);
373 
374   ixheaacd_mem_cpy(ptr_a, st->lp_flt_coeff_a_prev, ORDER + 1);
375   ixheaacd_mem_cpy(ptr_a, st->lp_flt_coeff_a_prev + ORDER + 1, ORDER + 1);
376 
377   return err;
378 }
379 
ixheaacd_randomsign(UWORD32 * seed)380 static FLOAT32 ixheaacd_randomsign(UWORD32 *seed) {
381   FLOAT32 sign = 0.0f;
382   *seed = (UWORD32)(((UWORD64)(*seed) * (UWORD64)69069) + 5);
383 
384   if (((*seed) & 0x10000) > 0)
385     sign = -1.f;
386   else
387     sign = +1.f;
388 
389   return sign;
390 }
391