• 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_type_def.h>
54 #include <ixheaacd_basic_ops32.h>
55 #include <ixheaacd_basic_ops40.h>
56 
57 static FLOAT32 ixheaacd_randomsign(UWORD32 *seed);
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 = ixheaacd_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_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)139 WORD32 ixheaacd_tcx_mdct(ia_usac_data_struct *usac_data,
140                          ia_td_frame_data_struct *pstr_td_frame_data,
141                          WORD32 frame_index, FLOAT32 lp_flt_coff_a[], WORD32 lg,
142                          ia_usac_lpd_decoder_handle st) {
143   WORD32 i, mode;
144   WORD32 *ptr_tcx_quant;
145   FLOAT32 tmp, gain_tcx, noise_level, energy, temp;
146   FLOAT32 *ptr_a, i_ap[ORDER + 1];
147   const FLOAT32 *sine_window_prev, *sine_window;
148   WORD32 fac_length_prev;
149   FLOAT32 alfd_gains[LEN_SUPERFRAME / (4 * 8)];
150   FLOAT32 x[LEN_SUPERFRAME], buf[ORDER + LEN_SUPERFRAME];
151   WORD32 int_x[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
152   WORD32 int_xn1[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
153   FLOAT32 gain1[LEN_SUPERFRAME], gain2[LEN_SUPERFRAME];
154   FLOAT32 xn_buf[LEN_SUPERFRAME + (2 * FAC_LENGTH)];
155   FLOAT32 *xn;
156   FLOAT32 xn1[2 * FAC_LENGTH], facwindow[2 * FAC_LENGTH];
157   WORD32 TTT;
158   WORD8 shiftp;
159   WORD32 preshift = 0;
160   WORD32 loop_count = 0;
161   FLOAT32 *exc = &usac_data->exc_buf[usac_data->len_subfrm * frame_index +
162                                      MAX_PITCH + INTER_LP_FIL_ORDER + 1];
163   FLOAT32 *synth =
164       &usac_data->synth_buf[usac_data->len_subfrm * frame_index + MAX_PITCH +
165                             (((NUM_FRAMES * usac_data->num_subfrm) / 2) - 1) *
166                                 LEN_SUBFR];
167 
168   WORD32 *ptr_scratch = &usac_data->scratch_buffer[0];
169 
170   WORD32 fac_length = (usac_data->len_subfrm) / 2;
171   WORD32 err = 0;
172 
173   mode = lg / (usac_data->len_subfrm);
174   if (mode > 2) mode = 3;
175 
176   if (st->mode_prev == -2)
177     fac_length_prev = (usac_data->ccfl) / 16;
178 
179   else
180     fac_length_prev = fac_length;
181 
182   if (fac_length == 96)
183     sine_window = ixheaacd_sine_window192;
184   else
185     sine_window = ixheaacd__sine_window256;
186 
187   if (fac_length_prev == 48)
188     sine_window_prev = ixheaacd_sine_window96;
189 
190   else if (fac_length_prev == 64)
191     sine_window_prev = ixheaacd_sine_window128;
192 
193   else if (fac_length_prev == 96)
194     sine_window_prev = ixheaacd_sine_window192;
195 
196   else
197     sine_window_prev = ixheaacd__sine_window256;
198 
199   xn = xn_buf + fac_length;
200 
201   if (st->mode_prev != 0) {
202     if (st->mode_prev > 0) {
203       for (i = 0; i < (2 * fac_length_prev); i++) {
204         st->exc_prev[i + fac_length - fac_length_prev + 1] *=
205             sine_window_prev[(2 * fac_length_prev) - 1 - i];
206       }
207     }
208     for (i = 0; i < fac_length - fac_length_prev; i++) {
209       st->exc_prev[i + fac_length + fac_length_prev + 1] = 0.0f;
210     }
211   }
212 
213   noise_level =
214       0.0625f *
215       (8.0f - ((FLOAT32)pstr_td_frame_data->noise_factor[frame_index]));
216 
217   ptr_tcx_quant = pstr_td_frame_data->x_tcx_invquant;
218   for (i = 0; i < frame_index; i++)
219     ptr_tcx_quant += pstr_td_frame_data->tcx_lg[i];
220 
221   for (i = 0; i < lg; i++) x[i] = (FLOAT32)ptr_tcx_quant[i];
222 
223   for (i = lg / 6; i < lg; i += 8) {
224     WORD32 k, max_k = min(lg, i + 8);
225     FLOAT32 tmp = 0.0f;
226     for (k = i; k < max_k; k++) tmp += ptr_tcx_quant[k] * ptr_tcx_quant[k];
227 
228     if (tmp == 0.0f) {
229       for (k = i; k < max_k; k++)
230         x[k] = noise_level *
231                ixheaacd_randomsign(
232                    &(usac_data->seed_value[usac_data->present_chan]));
233     }
234   }
235 
236   ixheaacd_low_fq_deemphasis(x, lg, alfd_gains);
237 
238   ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (ORDER + 1), i_ap);
239   ixheaacd_lpc_to_td(i_ap, ORDER, gain1, usac_data->len_subfrm / 4);
240 
241   ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (2 * (ORDER + 1)), i_ap);
242   ixheaacd_lpc_to_td(i_ap, ORDER, gain2, usac_data->len_subfrm / 4);
243 
244   energy = 0.01f;
245   for (i = 0; i < lg; i++) energy += x[i] * x[i];
246 
247   temp = (FLOAT32)sqrt(energy) / lg;
248 
249   gain_tcx =
250       (FLOAT32)pow(
251           10.0f,
252           ((FLOAT32)pstr_td_frame_data->global_gain[frame_index]) / 28.0f) /
253       (temp * 2.0f);
254 
255   ixheaacd_noise_shaping(x, lg, (usac_data->len_subfrm) / 4, gain1, gain2);
256 
257   shiftp = ixheaacd_float2fix(x, int_x, lg);
258 
259   err = ixheaacd_acelp_mdct_main(usac_data, int_x, int_xn1, (2 * fac_length),
260                                  lg - (2 * fac_length), &preshift);
261   if (err == -1) return err;
262 
263   ixheaacd_fix2float(int_xn1, xn_buf, (lg + (2 * fac_length)), &shiftp,
264                      &preshift);
265 
266   ixheaacd_vec_cnst_mul((2.0f / lg), xn_buf, xn_buf, lg + (2 * fac_length));
267 
268   st->fac_gain =
269       gain_tcx * 0.5f * (FLOAT32)sqrt(((FLOAT32)fac_length) / (FLOAT32)lg);
270 
271   for (i = 0; i < fac_length / 4; i++)
272     st->fac_fd_data[i] = alfd_gains[i * lg / (8 * fac_length)];
273 
274   if (st->mode_prev == 0) {
275     for (i = 0; i < fac_length_prev; i++) {
276       facwindow[i] =
277           sine_window_prev[i] * sine_window_prev[(2 * fac_length_prev) - 1 - i];
278       facwindow[fac_length_prev + i] =
279           1.0f - (sine_window_prev[fac_length_prev + i] *
280                   sine_window_prev[fac_length_prev + i]);
281     }
282 
283     for (i = 0; i < fac_length / 2; i++) {
284       x[i] = st->fac_gain *
285              (FLOAT32)pstr_td_frame_data->fac[frame_index * FAC_LENGTH + 2 * i];
286       x[fac_length / 2 + i] =
287           st->fac_gain *
288           (FLOAT32)pstr_td_frame_data
289               ->fac[frame_index * FAC_LENGTH + fac_length - 2 * i - 1];
290     }
291 
292     for (i = 0; i < fac_length / 8; i++) {
293       x[i] *= st->fac_fd_data[2 * i];
294       x[fac_length - i - 1] *= st->fac_fd_data[2 * i + 1];
295     }
296 
297     preshift = 0;
298     shiftp = ixheaacd_float2fix(x, int_x, fac_length);
299 
300     err =
301         ixheaacd_acelp_mdct(int_x, int_xn1, &preshift, fac_length, ptr_scratch);
302     if (err == -1) return err;
303 
304     ixheaacd_fix2float(int_xn1, xn1, fac_length, &shiftp, &preshift);
305 
306     ixheaacd_vec_cnst_mul((2.0f / (FLOAT32)fac_length), xn1, xn1, fac_length);
307 
308     memset(xn1 + fac_length, 0, fac_length * sizeof(FLOAT32));
309 
310     ixheaacd_lpc_coeff_wt_apply(lp_flt_coff_a + (ORDER + 1), i_ap);
311     ixheaacd_synthesis_tool_float(i_ap, xn1, xn1, 2 * fac_length,
312                                   xn1 + fac_length);
313 
314     for (i = 0; i < fac_length; i++) {
315       temp = st->exc_prev[1 + fac_length + i] * facwindow[fac_length + i] +
316              st->exc_prev[fac_length - i] * facwindow[fac_length - 1 - i];
317       xn1[i] += temp;
318     }
319   }
320 
321   for (i = 0; i < lg + (2 * fac_length); i++) xn_buf[i] *= gain_tcx;
322 
323   for (i = 0; i < (2 * fac_length_prev); i++)
324     xn_buf[i + fac_length - fac_length_prev] *= sine_window_prev[i];
325 
326   for (i = 0; i < fac_length - fac_length_prev; i++) xn_buf[i] = 0.0f;
327 
328   if (st->mode_prev != 0) {
329     for (i = fac_length - fac_length_prev; i < (fac_length + fac_length_prev);
330          i++)
331       xn_buf[i] += st->exc_prev[1 + i];
332   } else {
333     for (i = fac_length - fac_length_prev; i < (fac_length + fac_length_prev);
334          i++)
335       xn_buf[i + fac_length] += xn1[i];
336   }
337 
338   ixheaacd_mem_cpy(xn_buf + lg - 1, st->exc_prev, 1 + (2 * fac_length));
339 
340   for (i = 0; i < (2 * fac_length); i++) {
341     xn_buf[i + lg] *= sine_window[(2 * fac_length) - 1 - i];
342   }
343 
344   if (st->mode_prev != 0) {
345     ixheaacd_mem_cpy(xn_buf + fac_length - fac_length_prev,
346                      synth - fac_length_prev, fac_length_prev);
347 
348     for (i = 0; i < ORDER + fac_length; i++)
349       buf[i] = synth[i - ORDER - fac_length] -
350                (PREEMPH_FILT_FAC * synth[i - ORDER - fac_length - 1]);
351 
352     ptr_a = st->lp_flt_coeff_a_prev;
353     TTT = fac_length % LEN_SUBFR;
354     if (TTT != 0)
355       ixheaacd_residual_tool_float(ptr_a, &buf[ORDER], &exc[-fac_length], TTT,
356                                    1);
357 
358     loop_count = (fac_length - TTT) / LEN_SUBFR;
359     ixheaacd_residual_tool_float(ptr_a, &buf[ORDER + TTT],
360                                  &exc[TTT - fac_length], LEN_SUBFR, loop_count);
361   }
362 
363   ixheaacd_mem_cpy(xn, synth, lg);
364 
365   ixheaacd_mem_cpy(synth - ORDER - 1, xn - ORDER - 1, ORDER + 1);
366   tmp = xn[-ORDER - 1];
367   ixheaacd_preemphsis_tool_float(xn - ORDER, PREEMPH_FILT_FAC, ORDER + lg, tmp);
368 
369   ptr_a = lp_flt_coff_a + (2 * (ORDER + 1));
370 
371   ixheaacd_residual_tool_float(ptr_a, xn, exc, lg, 1);
372 
373   ixheaacd_mem_cpy(ptr_a, st->lp_flt_coeff_a_prev, ORDER + 1);
374   ixheaacd_mem_cpy(ptr_a, st->lp_flt_coeff_a_prev + ORDER + 1, ORDER + 1);
375 
376   return err;
377 }
378 
ixheaacd_randomsign(UWORD32 * seed)379 static FLOAT32 ixheaacd_randomsign(UWORD32 *seed) {
380   FLOAT32 sign = 0.0f;
381   *seed = (UWORD32)(((UWORD64)(*seed) * (UWORD64)69069) + 5);
382 
383   if (((*seed) & 0x10000) > 0)
384     sign = -1.f;
385   else
386     sign = +1.f;
387 
388   return sign;
389 }
390