• 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 <assert.h>
21 #include <float.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <math.h>
25 #include <string.h>
26 
27 #include "ixheaacd_type_def.h"
28 #include "ixheaacd_bitbuffer.h"
29 #include "ixheaacd_interface.h"
30 #include "ixheaacd_tns_usac.h"
31 #include "ixheaacd_cnst.h"
32 #include "ixheaacd_acelp_info.h"
33 
34 #include "ixheaacd_sbrdecsettings.h"
35 #include "ixheaacd_info.h"
36 #include "ixheaacd_sbr_common.h"
37 #include "ixheaacd_drc_data_struct.h"
38 #include "ixheaacd_drc_dec.h"
39 #include "ixheaacd_sbrdecoder.h"
40 #include "ixheaacd_mps_polyphase.h"
41 #include "ixheaacd_sbr_const.h"
42 #include "ixheaacd_main.h"
43 #include "ixheaacd_arith_dec.h"
44 
45 #include "ixheaacd_func_def.h"
46 #include "ixheaacd_windows.h"
47 #include "ixheaacd_acelp_com.h"
48 #include "ixheaacd_constants.h"
49 #include "ixheaacd_basic_ops32.h"
50 #include "ixheaacd_basic_ops40.h"
51 
52 #define LSF_GAP_F 50.0f
53 #define FREQ_MAX_F 6400.0f
54 #define FREQ_DIV_F 400.0f
55 
56 extern const FLOAT32 lsf_init[ORDER];
57 
58 extern const FLOAT32 ixheaacd_fir_lp_filt[1 + FILTER_DELAY];
59 
60 const WORD32 ixheaacd_pow_10_i_by_128[128] = {
61     16384,     17788,     19312,     20968,     22765,     24716,     26835,
62     29135,     31632,     34343,     37287,     40483,     43953,     47720,
63     51810,     56251,     61072,     66307,     71990,     78161,     84860,
64     92134,     100030,    108604,    117913,    128019,    138992,    150905,
65     163840,    177882,    193129,    209682,    227654,    247167,    268352,
66     291353,    316325,    343438,    372874,    404834,    439532,    477205,
67     518107,    562515,    610728,    663075,    719908,    781612,    848605,
68     921340,    1000309,   1086046,   1179133,   1280197,   1389925,   1509057,
69     1638400,   1778829,   1931294,   2096827,   2276549,   2471675,   2683525,
70     2913532,   3163255,   3434381,   3728745,   4048340,   4395328,   4772057,
71     5181075,   5625151,   6107289,   6630752,   7199081,   7816122,   8486051,
72     9213400,   10003091,  10860467,  11791330,  12801978,  13899250,  15090570,
73     16384000,  17788290,  19312945,  20968279,  22765494,  24716750,  26835250,
74     29135329,  31632551,  34343813,  37287459,  40483409,  43953287,  47720573,
75     51810757,  56251515,  61072895,  66307521,  71990813,  78161226,  84860513,
76     92134002,  100030911, 108604672, 117913300, 128019781, 138992500, 150905703,
77     163840000, 177882909, 193129453, 209682794, 227654941, 247167501, 268352504,
78     291353298, 316325515, 343438130, 372874596, 404834095, 439532879, 477205734,
79     518107571, 562515151};
80 
81 VOID ixheaacd_lsf_weight_2st_flt(float *lsfq, float *w, WORD32 mode);
82 
ixheaacd_mult32_m(WORD32 a,WORD32 b)83 static PLATFORM_INLINE WORD32 ixheaacd_mult32_m(WORD32 a, WORD32 b) {
84   WORD32 result;
85   WORD64 temp_result;
86 
87   temp_result = (WORD64)a * (WORD64)b;
88   result = (WORD32)(temp_result >> 31);
89 
90   return (result);
91 }
92 
ixheaacd_reset_acelp_data_fix(ia_usac_data_struct * usac_data,ia_usac_lpd_decoder_handle st,WORD32 * ptr_overlap_buf,WORD32 was_last_short,WORD32 tw_mdct)93 void ixheaacd_reset_acelp_data_fix(ia_usac_data_struct *usac_data,
94                                    ia_usac_lpd_decoder_handle st,
95                                    WORD32 *ptr_overlap_buf,
96                                    WORD32 was_last_short, WORD32 tw_mdct) {
97   WORD32 i;
98 
99   if (was_last_short == 1) {
100     st->mode_prev = -2;
101   } else {
102     st->mode_prev = -1;
103   }
104 
105   for (i = 0; i < NUM_SUBFR_SUPERFRAME_BY2 - 1; i++) {
106     st->pitch_prev[i] = 64;
107     st->gain_prev[i] = 0;
108   }
109 
110   st->bpf_active_prev = 0;
111 
112   if (ptr_overlap_buf != NULL && !tw_mdct) {
113     const WORD32 *ptr_window_coeff;
114     WORD32 fac_length;
115     if (was_last_short) {
116       fac_length = (usac_data->ccfl) / 16;
117     } else {
118       fac_length = (usac_data->len_subfrm) / 2;
119     }
120 
121     if (fac_length == 48) {
122       ptr_window_coeff = ixheaacd_sine_win_96;
123     } else if (fac_length == 64) {
124       ptr_window_coeff = ixheaacd_sine_win_128;
125     } else if (fac_length == 96) {
126       ptr_window_coeff = ixheaacd_sine_win_192;
127     } else {
128       ptr_window_coeff = ixheaacd_sine_win_256;
129     }
130 
131     for (i = 0; i < 2 * fac_length; i++) {
132       ptr_overlap_buf[(usac_data->ccfl) / 2 - fac_length + i] =
133           ixheaacd_mult32_m(
134               ptr_overlap_buf[(usac_data->ccfl) / 2 - fac_length + i],
135               ptr_window_coeff[2 * fac_length - 1 - i]);
136     }
137     for (i = 0; i < (usac_data->ccfl) / 2 - fac_length; i++) {
138       ptr_overlap_buf[(usac_data->ccfl) / 2 + fac_length + i] = 0;
139     }
140 
141     if (ptr_overlap_buf != NULL) {
142       for (i = 0; i < (usac_data->len_subfrm) / 2 - fac_length; i++) {
143         st->exc_prev[i] = 0.0f;
144       }
145       for (i = 0; i < 2 * fac_length + 1; i++) {
146         st->exc_prev[(usac_data->len_subfrm) / 2 - fac_length + i] =
147             ptr_overlap_buf[i + usac_data->ccfl / 2 - fac_length - 1] /
148             (float)(16384);
149       }
150     } else {
151       ixheaacd_memset(st->exc_prev, 1 + (2 * FAC_LENGTH));
152     }
153   }
154 
155   return;
156 }
157 
ixheaacd_fix2flt_data(ia_usac_data_struct * usac_data,ia_usac_lpd_decoder_handle st,WORD32 k)158 VOID ixheaacd_fix2flt_data(ia_usac_data_struct *usac_data,
159                            ia_usac_lpd_decoder_handle st, WORD32 k) {
160   WORD32 i;
161   WORD32 fac_length;
162   WORD32 window_sequence_last = usac_data->window_sequence_last[k];
163   WORD32 *p_ola_buffer = usac_data->overlap_data_ptr[k];
164   if (window_sequence_last == EIGHT_SHORT_SEQUENCE) {
165     fac_length = (usac_data->ccfl) / 16;
166   } else {
167     fac_length = (usac_data->len_subfrm) / 2;
168   }
169 
170   ixheaacd_memset(st->lp_flt_coeff_a_prev, 2 * (ORDER + 1));
171   ixheaacd_memset(st->xcitation_prev, MAX_PITCH + INTER_LP_FIL_ORDER + 1);
172   ixheaacd_memset(st->synth_prev, MAX_PITCH + SYNTH_DELAY_LMAX);
173   ixheaacd_memset(st->bpf_prev, FILTER_DELAY + LEN_SUBFR);
174 
175   st->gain_threshold = 0.0f;
176 
177   if (p_ola_buffer != NULL) {
178     for (i = 0; i < (usac_data->len_subfrm) / 2 - fac_length; i++) {
179       st->exc_prev[i] = 0;
180     }
181     for (i = 0; i < 2 * fac_length + 1; i++) {
182       st->exc_prev[(usac_data->len_subfrm) / 2 - fac_length + i] = (FLOAT32)(
183           p_ola_buffer[i + usac_data->ccfl / 2 - fac_length - 1] / 16384.0);
184     }
185   } else {
186     ixheaacd_memset(st->exc_prev, 1 + (2 * FAC_LENGTH));
187   }
188 
189   return;
190 }
191 
ixheaacd_init_acelp_data(ia_usac_data_struct * usac_data,ia_usac_lpd_decoder_handle st)192 void ixheaacd_init_acelp_data(ia_usac_data_struct *usac_data,
193                               ia_usac_lpd_decoder_handle st) {
194   ixheaacd_reset_acelp_data_fix(usac_data, st, NULL, 0, 0);
195 }
196 
197 #define PI_BY_6400 (PI / 6400.0)
198 #define SCALE1 (6400.0 / PI)
199 
ixheaacd_lsp_2_lsf_conversion(float lsp[],float lsf[],WORD32 m)200 void ixheaacd_lsp_2_lsf_conversion(float lsp[], float lsf[], WORD32 m) {
201   short i;
202   for (i = 0; i < m; i++) {
203     lsf[i] = (float)(acos(lsp[i]) * SCALE1);
204   }
205   return;
206 }
207 
ixheaacd_lsf_2_lsp_conversion_float(FLOAT32 lsf[],FLOAT32 lsp[],WORD32 m)208 static VOID ixheaacd_lsf_2_lsp_conversion_float(FLOAT32 lsf[], FLOAT32 lsp[],
209                                                 WORD32 m) {
210   WORD32 i;
211   for (i = 0; i < m; i++)
212     lsp[i] = (FLOAT32)cos((double)lsf[i] * (double)PI_BY_6400);
213 
214   return;
215 }
216 
ixheaacd_bass_post_filter(FLOAT32 * synth_sig,WORD32 * pitch,FLOAT32 * pitch_gain,FLOAT32 * synth_out,WORD32 len_fr,WORD32 len2,FLOAT32 bpf_prev[])217 static WORD32 ixheaacd_bass_post_filter(FLOAT32 *synth_sig, WORD32 *pitch,
218                                         FLOAT32 *pitch_gain, FLOAT32 *synth_out,
219                                         WORD32 len_fr, WORD32 len2,
220                                         FLOAT32 bpf_prev[]) {
221   WORD32 i, j, sf, num_subfr, pitch_lag, lg;
222   FLOAT32 x_energy, xy_corr, y_energy, norm_corr, energy, gain, tmp, alpha;
223   FLOAT32 noise_buf[FILTER_DELAY + (2 * LEN_SUBFR)], *noise_tmp1, *noise_tmp2,
224       *x, *y;
225 
226   noise_tmp1 = noise_buf + FILTER_DELAY;
227   noise_tmp2 = noise_buf + FILTER_DELAY + LEN_SUBFR;
228 
229   memcpy(synth_out, synth_sig - LEN_SUBFR, len_fr * sizeof(FLOAT32));
230 
231   if (len_fr % 64)
232     memset(synth_out + len_fr, 0, (LEN_SUBFR - len_fr % 64) * sizeof(FLOAT32));
233 
234   sf = 0;
235   for (num_subfr = 0; num_subfr < len_fr; num_subfr += LEN_SUBFR, sf++) {
236     pitch_lag = pitch[sf];
237     gain = pitch_gain[sf];
238     if (((pitch_lag >> 1) + 96 - num_subfr) > MAX_PITCH) return -1;
239     if (gain > 1.0f) gain = 1.0f;
240     if (gain < 0.0f) gain = 0.0f;
241 
242     x = &synth_sig[num_subfr - 96];
243     y = &synth_sig[num_subfr - pitch_lag / 2 - 96];
244 
245     x_energy = 0.01f;
246     xy_corr = 0.01f;
247     y_energy = 0.01f;
248     for (i = 0; i < LEN_SUBFR + 96; i++) {
249       x_energy += x[i] * x[i];
250       xy_corr += x[i] * y[i];
251       y_energy += y[i] * y[i];
252     }
253 
254     norm_corr = xy_corr / (FLOAT32)sqrt(x_energy * y_energy);
255 
256     if (norm_corr > 0.95f) pitch_lag >>= 1;
257 
258     lg = len_fr + len2 - pitch_lag - num_subfr;
259     if (lg < 0) lg = 0;
260     if (lg > LEN_SUBFR) lg = LEN_SUBFR;
261 
262     if (gain > 0) {
263       if (lg > 0) {
264         tmp = 0.01f;
265         for (i = 0; i < lg; i++) {
266           tmp += synth_sig[i + num_subfr] * synth_sig[i + num_subfr];
267         }
268         energy = 0.01f;
269         for (i = 0; i < lg; i++) {
270           energy += synth_sig[i + num_subfr + pitch_lag] *
271                     synth_sig[i + num_subfr + pitch_lag];
272         }
273         tmp = (FLOAT32)sqrt(tmp / energy);
274         if (tmp < gain) gain = tmp;
275       }
276 
277       alpha = 0.5f * gain;
278       for (i = 0; i < lg; i++) {
279         noise_tmp2[i] = alpha * (synth_sig[i + num_subfr] -
280                                  0.5f * synth_sig[i + num_subfr - pitch_lag] -
281                                  0.5f * synth_sig[i + num_subfr + pitch_lag]);
282       }
283       for (i = lg; i < LEN_SUBFR; i++) {
284         noise_tmp2[i] = alpha * (synth_sig[i + num_subfr] -
285                                  synth_sig[i + num_subfr - pitch_lag]);
286       }
287     } else {
288       memset(noise_tmp2, 0, LEN_SUBFR * sizeof(FLOAT32));
289     }
290 
291     memcpy(noise_buf, bpf_prev, (FILTER_DELAY + LEN_SUBFR) * sizeof(FLOAT32));
292     memcpy(bpf_prev, noise_buf + LEN_SUBFR,
293            (FILTER_DELAY + LEN_SUBFR) * sizeof(FLOAT32));
294 
295     for (i = 0; i < LEN_SUBFR; i++) {
296       tmp = ixheaacd_fir_lp_filt[0] * noise_tmp1[i];
297       for (j = 1; j <= FILTER_DELAY; j++) {
298         tmp +=
299             ixheaacd_fir_lp_filt[j] * (noise_tmp1[i - j] + noise_tmp1[i + j]);
300       }
301       synth_out[i + num_subfr] -= tmp;
302     }
303   }
304 
305   return 0;
306 }
307 
ixheaacd_reorder_lsf(float * lsf,float min_dist,int n)308 void ixheaacd_reorder_lsf(float *lsf, float min_dist, int n) {
309   int i;
310   float lsf_min;
311 
312   lsf_min = min_dist;
313   for (i = 0; i < n; i++) {
314     if (lsf[i] < lsf_min) lsf[i] = lsf_min;
315 
316     lsf_min = lsf[i] + min_dist;
317   }
318 
319   lsf_min = FREQ_MAX_F - min_dist;
320   for (i = n - 1; i >= 0; i--) {
321     if (lsf[i] > lsf_min) lsf[i] = lsf_min;
322 
323     lsf_min = lsf[i] - min_dist;
324   }
325 
326   return;
327 }
328 
ixheaacd_lpd_dec(ia_usac_data_struct * usac_data,ia_usac_lpd_decoder_handle st,ia_td_frame_data_struct * pstr_td_frame_data,FLOAT32 fsynth[],WORD32 first_lpd_flag,WORD32 short_fac_flag,WORD32 bpf_control_info)329 WORD32 ixheaacd_lpd_dec(ia_usac_data_struct *usac_data,
330                         ia_usac_lpd_decoder_handle st,
331                         ia_td_frame_data_struct *pstr_td_frame_data,
332                         FLOAT32 fsynth[], WORD32 first_lpd_flag,
333                         WORD32 short_fac_flag, WORD32 bpf_control_info) {
334   FLOAT32 *synth_buf = usac_data->synth_buf;
335   FLOAT32 *xcitation_buff = usac_data->exc_buf;
336   FLOAT32 lsp_curr[ORDER];
337   FLOAT32 lsf_curr[ORDER];
338   FLOAT32 *lp_flt_coff_a = usac_data->lp_flt_coff;
339   FLOAT32 *synth, *xcitation_curr;
340   WORD32 *pitch = usac_data->pitch;
341   FLOAT32 *pitch_gain = usac_data->pitch_gain;
342   FLOAT32 lsf_flt[(2 * NUM_FRAMES + 1) * ORDER];
343 
344   WORD32 i, k, tp, mode;
345   WORD32 *mod;
346   FLOAT32 gain, stability_factor;
347   FLOAT32 tmp, synth_corr, synth_energy;
348 
349   WORD32 len_fr;
350   WORD32 len_subfrm;
351   WORD32 num_subfr;
352   WORD32 num_subfr_in_superfr;
353   WORD32 num_subfr_by2;
354   WORD32 synth_delay;
355   WORD32 num_samples = 0;
356 
357   WORD32 *ptr_scratch = &usac_data->scratch_buffer[0];
358 
359   WORD32 subfr_len, n_subfr;
360   WORD32 err = 0;
361 
362   len_fr = usac_data->ccfl;
363   len_subfrm = usac_data->len_subfrm;
364   num_subfr = usac_data->num_subfrm;
365   num_subfr_in_superfr = NUM_FRAMES * num_subfr;
366   num_subfr_by2 = (num_subfr_in_superfr / 2) - 1;
367   synth_delay = num_subfr_by2 * LEN_SUBFR;
368 
369   synth = synth_buf + MAX_PITCH + synth_delay;
370   ixheaacd_mem_cpy(st->synth_prev, synth_buf, MAX_PITCH + synth_delay);
371   ixheaacd_memset(synth, SYNTH_DELAY_LMAX + LEN_SUPERFRAME - synth_delay);
372 
373   xcitation_curr = xcitation_buff + MAX_PITCH + INTER_LP_FIL_ORDER + 1;
374   ixheaacd_mem_cpy(st->xcitation_prev, xcitation_buff,
375                    MAX_PITCH + INTER_LP_FIL_ORDER + 1);
376   memset(xcitation_curr, 0, sizeof(FLOAT32) * (LEN_SUPERFRAME + 1));
377 
378   mod = pstr_td_frame_data->mod;
379 
380   for (i = 0; i < num_subfr_by2; i++) {
381     pitch[i] = st->pitch_prev[i];
382     pitch_gain[i] = st->gain_prev[i];
383   }
384   for (i = 0; i < num_subfr_in_superfr; i++) {
385     pitch[i + num_subfr_by2] = 64;
386     pitch_gain[i + num_subfr_by2] = 0.0f;
387   }
388   if (!first_lpd_flag) {
389     ixheaacd_lsp_2_lsf_conversion(st->lspold, lsf_flt, ORDER);
390   }
391 
392   ixheaacd_alg_vec_dequant(pstr_td_frame_data, first_lpd_flag, lsf_flt,
393                            pstr_td_frame_data->mod);
394 
395   if (first_lpd_flag) {
396     ixheaacd_mem_cpy(&lsf_flt[0], st->lsf_prev, ORDER);
397     ixheaacd_lsf_2_lsp_conversion_float(st->lsf_prev, st->lspold, ORDER);
398   }
399 
400   if ((first_lpd_flag && mod[0] == 0) || (first_lpd_flag && mod[1] == 0) ||
401       ((first_lpd_flag && mod[2] == 0 && len_subfrm != LEN_FRAME))) {
402     FLOAT32 lp_flt_coeff_a[9 * (ORDER + 1)];
403     FLOAT32 tmp_buf[3 * LEN_FRAME + ORDER];
404     FLOAT32 tmp_res_buf[3 * LEN_FRAME];
405     FLOAT32 *tmp = &(tmp_buf[LEN_FRAME]);
406     FLOAT32 *ptr_tmp = &(tmp_res_buf[LEN_FRAME]);
407     WORD32 tmp_start;
408     FLOAT32 mem = 0;
409     WORD32 gain;
410     WORD32 length;
411 
412     ixheaacd_interpolation_lsp_params(st->lspold, st->lspold, lp_flt_coeff_a,
413                                       8);
414 
415     memcpy(st->lp_flt_coeff_a_prev, lp_flt_coeff_a,
416            (ORDER + 1) * sizeof(FLOAT32));
417     memcpy(st->lp_flt_coeff_a_prev + ORDER + 1, lp_flt_coeff_a,
418            (ORDER + 1) * sizeof(FLOAT32));
419 
420     if (mod[0] == 0) {
421       WORD32 fac_length;
422       if (short_fac_flag) {
423         fac_length = (len_subfrm * NUM_FRAMES) / 16;
424       } else {
425         fac_length = len_subfrm / 2;
426       }
427       if ((pstr_td_frame_data->fac_data[0] < 0) ||
428           (pstr_td_frame_data->fac_data[0] > 128)) {
429         return -1;
430       }
431       gain = ixheaacd_pow_10_i_by_128[pstr_td_frame_data->fac_data[0]];
432 
433       memcpy(ptr_scratch, &pstr_td_frame_data->fac_data[0],
434              129 * sizeof(WORD32));
435 
436       for (i = 0; i < fac_length / 2; i++) {
437         pstr_td_frame_data->fac_data[i] = ptr_scratch[2 * i + 1] << 16;
438         pstr_td_frame_data->fac_data[fac_length / 2 + i] =
439             ptr_scratch[fac_length - 2 * i] << 16;
440       }
441 
442       err = ixheaacd_fwd_alias_cancel_tool(usac_data, pstr_td_frame_data,
443                                            fac_length, lp_flt_coeff_a, gain);
444       if (err == -1) return err;
445 
446       memset(
447           &usac_data->overlap_data_ptr[usac_data->present_chan][(len_fr / 2)],
448           0, fac_length * sizeof(WORD32));
449     }
450 
451     for (i = 0; i < 2 * len_subfrm; i++)
452       st->fd_synth[ORDER + i] = (FLOAT32)(
453           (FLOAT32)usac_data->overlap_data_ptr[usac_data->present_chan][i] /
454           16384.0);
455     num_samples = min(2 * len_subfrm, MAX_PITCH + synth_delay);
456 
457     ixheaacd_mem_cpy(st->fd_synth + ORDER, synth - 2 * len_subfrm,
458                      2 * len_subfrm);
459 
460     ixheaacd_preemphsis_tool_float(st->fd_synth + ORDER, PREEMPH_FILT_FAC,
461                                    2 * len_subfrm, mem);
462 
463     ixheaacd_memset(tmp, ORDER);
464     ixheaacd_mem_cpy(st->fd_synth + ORDER, tmp + ORDER, 2 * len_subfrm);
465     tmp_start = 0;
466 
467     ixheaacd_memset(ptr_tmp - len_subfrm, 3 * len_subfrm);
468     memset(st->fd_synth, 0, ORDER * sizeof(WORD32));
469     length = (2 * len_subfrm - tmp_start) / LEN_SUBFR;
470 
471     ixheaacd_residual_tool_float1(lp_flt_coeff_a,
472                                   &st->fd_synth[ORDER + tmp_start],
473                                   &ptr_tmp[tmp_start], LEN_SUBFR, length);
474 
475     if (mod[0] != 0 && (len_subfrm == LEN_FRAME || mod[1] != 0)) {
476       num_samples = min(len_subfrm, MAX_PITCH + INTER_LP_FIL_ORDER + 1);
477     } else {
478       num_samples = min(2 * len_subfrm, MAX_PITCH + INTER_LP_FIL_ORDER + 1);
479     }
480     ixheaacd_mem_cpy(ptr_tmp + 2 * len_subfrm - num_samples,
481                      xcitation_curr - num_samples, num_samples);
482   }
483 
484   k = 0;
485 
486   while (k < 4) {
487     mode = mod[k];
488     if ((st->mode_prev == 0) && (mode > 0) &&
489         (k != 0 || st->bpf_active_prev == 1)) {
490       i = (k * num_subfr) + num_subfr_by2;
491       pitch[i + 1] = pitch[i] = pitch[i - 1];
492       pitch_gain[i + 1] = pitch_gain[i] = pitch_gain[i - 1];
493     }
494 
495     if ((mode == 0) || (mode == 1))
496       memcpy(lsf_curr, &lsf_flt[(k + 1) * ORDER], ORDER * sizeof(FLOAT32));
497     else if (mode == 2)
498       memcpy(lsf_curr, &lsf_flt[(k + 2) * ORDER], ORDER * sizeof(FLOAT32));
499     else
500       memcpy(lsf_curr, &lsf_flt[(k + 4) * ORDER], ORDER * sizeof(FLOAT32));
501 
502     ixheaacd_lsf_2_lsp_conversion_float(lsf_curr, lsp_curr, ORDER);
503 
504     tmp = 0.0f;
505     for (i = 0; i < ORDER; i++) {
506       tmp += (lsf_curr[i] - st->lsf_prev[i]) * (lsf_curr[i] - st->lsf_prev[i]);
507     }
508     stability_factor = (FLOAT32)(1.25f - (tmp / 400000.0f));
509     if (stability_factor > 1.0f) {
510       stability_factor = 1.0f;
511     }
512     if (stability_factor < 0.0f) {
513       stability_factor = 0.0f;
514     }
515 
516     if (mode == 0) {
517       ixheaacd_interpolation_lsp_params(st->lspold, lsp_curr, lp_flt_coff_a,
518                                         num_subfr);
519 
520       err = ixheaacd_acelp_alias_cnx(usac_data, pstr_td_frame_data, k,
521                                      lp_flt_coff_a, stability_factor, st);
522       if (err) return err;
523 
524       if ((st->mode_prev != 0) && bpf_control_info) {
525         i = (k * num_subfr) + num_subfr_by2;
526         pitch[i - 1] = pitch[i];
527         pitch_gain[i - 1] = pitch_gain[i];
528         if (st->mode_prev != -2) {
529           pitch[i - 2] = pitch[i];
530           pitch_gain[i - 2] = pitch_gain[i];
531         }
532       }
533       k++;
534     } else {
535       if (mode == 1) {
536         subfr_len = len_subfrm;
537         n_subfr = num_subfr;
538       } else if (mode == 2) {
539         subfr_len = len_subfrm << 1;
540         n_subfr = num_subfr_in_superfr / 2;
541       } else if (mode == 3) {
542         subfr_len = len_subfrm << 2;
543         n_subfr = num_subfr_in_superfr;
544       }
545 
546       ixheaacd_lpc_coef_gen(st->lspold, lsp_curr, lp_flt_coff_a, n_subfr,
547                             ORDER);
548 
549       err = ixheaacd_tcx_mdct(usac_data, pstr_td_frame_data, k, lp_flt_coff_a,
550                               subfr_len, st);
551       if (err) return err;
552       k += (1 << (mode - 1));
553     }
554 
555     st->mode_prev = mode;
556 
557     ixheaacd_mem_cpy(lsp_curr, st->lspold, ORDER);
558     ixheaacd_mem_cpy(lsf_curr, st->lsf_prev, ORDER);
559   }
560 
561   ixheaacd_mem_cpy(xcitation_buff + len_fr, st->xcitation_prev,
562                    MAX_PITCH + INTER_LP_FIL_ORDER + 1);
563 
564   ixheaacd_mem_cpy(synth_buf + len_fr, st->synth_prev, MAX_PITCH + synth_delay);
565 
566   if (!bpf_control_info) {
567     if (mod[0] != 0 && st->bpf_active_prev) {
568       for (i = 2; i < num_subfr_in_superfr; i++)
569         pitch_gain[num_subfr_by2 + i] = 0.0;
570     } else {
571       for (i = 0; i < num_subfr_in_superfr; i++)
572         pitch_gain[num_subfr_by2 + i] = 0.0;
573     }
574   }
575   st->bpf_active_prev = bpf_control_info;
576 
577   for (i = 0; i < num_subfr_by2; i++) {
578     st->pitch_prev[i] = pitch[num_subfr_in_superfr + i];
579     st->gain_prev[i] = pitch_gain[num_subfr_in_superfr + i];
580   }
581 
582   synth = synth_buf + MAX_PITCH;
583 
584   for (i = 0; i < num_subfr_in_superfr; i++) {
585     tp = pitch[i];
586     gain = pitch_gain[i];
587     if (gain > 0.0f) {
588       synth_corr = 0.0f, synth_energy = 1e-6f;
589       if ((((i * LEN_SUBFR) + LEN_SUBFR) > LEN_SUPERFRAME) ||
590           ((((i * LEN_SUBFR) + LEN_SUBFR) - tp) > LEN_SUPERFRAME))
591         return -1;
592       for (k = 0; k < LEN_SUBFR; k++) {
593         synth_corr +=
594             synth[i * LEN_SUBFR + k] * synth[(i * LEN_SUBFR) - tp + k];
595         synth_energy +=
596             synth[(i * LEN_SUBFR) - tp + k] * synth[(i * LEN_SUBFR) - tp + k];
597       }
598       pitch_gain[i] = synth_corr / synth_energy;
599     }
600   }
601 
602   if (mod[3] == 0) {
603     err = ixheaacd_bass_post_filter(synth, pitch, pitch_gain, fsynth, len_fr,
604                                     synth_delay, st->bpf_prev);
605   } else {
606     err =
607         ixheaacd_bass_post_filter(synth, pitch, pitch_gain, fsynth, len_fr,
608                                   synth_delay - (len_subfrm / 2), st->bpf_prev);
609   }
610   return err;
611 }
612 
ixheaacd_lpd_dec_update(ia_usac_lpd_decoder_handle tddec,ia_usac_data_struct * usac_data,WORD32 i_ch)613 VOID ixheaacd_lpd_dec_update(ia_usac_lpd_decoder_handle tddec,
614                              ia_usac_data_struct *usac_data, WORD32 i_ch) {
615   WORD32 i, k;
616 
617   WORD32 *ptr_overlap = &usac_data->overlap_data_ptr[i_ch][0];
618   WORD32 len_fr, lpd_sbf_len, lpd_delay, num_subfr_by2, synth_delay, fac_length;
619 
620   if (usac_data->tw_mdct[0])
621     ptr_overlap = &usac_data->overlap_data_ptr[i_ch][usac_data->ccfl / 2];
622 
623   len_fr = usac_data->ccfl;
624   lpd_sbf_len = (NUM_FRAMES * usac_data->num_subfrm) / 2;
625   lpd_delay = lpd_sbf_len * LEN_SUBFR;
626   num_subfr_by2 = lpd_sbf_len - 1;
627   synth_delay = num_subfr_by2 * LEN_SUBFR;
628   fac_length = (usac_data->len_subfrm) / 2;
629 
630   for (i = 0; i < LEN_SUBFR + synth_delay; i++)
631     ptr_overlap[i] = (WORD32)(
632         (FLOAT32)tddec->synth_prev[MAX_PITCH - (LEN_SUBFR) + i] * 16384.0);
633 
634   ptr_overlap += LEN_SUBFR + synth_delay - fac_length;
635 
636   for (k = 0; k < 2 * fac_length; k++)
637     ptr_overlap[k] = (WORD32)((FLOAT32)tddec->exc_prev[k + 1] * 16384.0);
638 
639   ptr_overlap = &usac_data->overlap_data_ptr[i_ch][lpd_delay + fac_length];
640 
641   for (i = 0; i < len_fr - lpd_delay - fac_length; i++) ptr_overlap[i] = 0;
642 
643   usac_data->window_shape[i_ch] = WIN_SEL_0;
644   usac_data->window_sequence_last[i_ch] = EIGHT_SHORT_SEQUENCE;
645   usac_data->td_frame_prev[i_ch] = 1;
646 
647   if (tddec->mode_prev == 0) {
648     memmove(usac_data->lpc_prev[i_ch], &tddec->lp_flt_coeff_a_prev[ORDER + 1],
649             (ORDER + 1) * sizeof(FLOAT32));
650     memmove(usac_data->acelp_in[i_ch], tddec->exc_prev,
651             (1 + (2 * FAC_LENGTH)) * sizeof(FLOAT32));
652   }
653 
654   return;
655 }
656 
ixheaacd_lpd_bpf_fix(ia_usac_data_struct * usac_data,WORD32 is_short_flag,FLOAT32 out_buffer[],ia_usac_lpd_decoder_handle st)657 WORD32 ixheaacd_lpd_bpf_fix(ia_usac_data_struct *usac_data,
658                             WORD32 is_short_flag, FLOAT32 out_buffer[],
659                             ia_usac_lpd_decoder_handle st) {
660   WORD32 i, tp, k;
661   float synth_buf[MAX_PITCH + SYNTH_DELAY_LMAX + LEN_SUPERFRAME];
662   float signal_out[LEN_SUPERFRAME];
663   float *synth, synth_corr, synth_energy;
664   WORD32 pitch[NUM_SUBFR_SUPERFRAME_BY2 + 3];
665   float pitch_gain[NUM_SUBFR_SUPERFRAME_BY2 + 3];
666   WORD32 len_fr, lpd_sbf_len, lpd_delay, num_subfr_by2, synth_delay, fac_length;
667   WORD32 err = 0;
668 
669   len_fr = usac_data->ccfl;
670   lpd_sbf_len = (NUM_FRAMES * usac_data->num_subfrm) / 2;
671   lpd_delay = lpd_sbf_len * LEN_SUBFR;
672   num_subfr_by2 = lpd_sbf_len - 1;
673   synth_delay = num_subfr_by2 * LEN_SUBFR;
674   fac_length = (usac_data->len_subfrm) / 2;
675 
676   ixheaacd_memset(synth_buf, MAX_PITCH + synth_delay + len_fr);
677   ixheaacd_mem_cpy(st->synth_prev, synth_buf, MAX_PITCH + synth_delay);
678   ixheaacd_mem_cpy(out_buffer, synth_buf + MAX_PITCH - (LEN_SUBFR),
679                    synth_delay + len_fr + (LEN_SUBFR));
680 
681   for (i = 0; i < num_subfr_by2; i++) {
682     pitch[i] = st->pitch_prev[i];
683     pitch_gain[i] = st->gain_prev[i];
684   }
685   for (i = num_subfr_by2; i < lpd_sbf_len + 3; i++) {
686     pitch[i] = 64;
687     pitch_gain[i] = 0.0f;
688   }
689   if (st->mode_prev == 0) {
690     pitch[num_subfr_by2] = pitch[num_subfr_by2 - 1];
691     pitch_gain[num_subfr_by2] = pitch_gain[num_subfr_by2 - 1];
692     if (!is_short_flag) {
693       pitch[num_subfr_by2 + 1] = pitch[num_subfr_by2];
694       pitch_gain[num_subfr_by2 + 1] = pitch_gain[num_subfr_by2];
695     }
696   }
697 
698   synth = synth_buf + MAX_PITCH;
699 
700   for (i = 0; i < num_subfr_by2 + 2; i++) {
701     tp = pitch[i];
702     if ((i * LEN_SUBFR + MAX_PITCH) < tp) {
703       return -1;
704     } else if (((i * LEN_SUBFR + MAX_PITCH - tp) >= 1883) ||
705                (((i * LEN_SUBFR) + LEN_SUBFR) > LEN_SUPERFRAME) ||
706                ((((i * LEN_SUBFR) + LEN_SUBFR) - tp) > LEN_SUPERFRAME)) {
707       return -1;
708     }
709 
710     if (pitch_gain[i] > 0.0f) {
711       synth_corr = 0.0f, synth_energy = 1e-6f;
712       for (k = 0; k < LEN_SUBFR; k++) {
713         synth_corr +=
714             synth[i * LEN_SUBFR + k] * synth[(i * LEN_SUBFR) - tp + k];
715         synth_energy +=
716             synth[(i * LEN_SUBFR) - tp + k] * synth[(i * LEN_SUBFR) - tp + k];
717       }
718       pitch_gain[i] = synth_corr / synth_energy;
719     }
720   }
721 
722   err = ixheaacd_bass_post_filter(synth, pitch, pitch_gain, signal_out,
723                                   (lpd_sbf_len + 2) * LEN_SUBFR + LEN_SUBFR,
724                                   len_fr - (lpd_sbf_len + 4) * LEN_SUBFR,
725                                   st->bpf_prev);
726   if (err != 0) return err;
727 
728   ixheaacd_mem_cpy(signal_out, out_buffer,
729                    (lpd_sbf_len + 2) * LEN_SUBFR + LEN_SUBFR);
730   return err;
731 }
732