• 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 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 
142   return;
143 }
144 
ixheaacd_fix2flt_data(ia_usac_data_struct * usac_data,ia_usac_lpd_decoder_handle st,WORD32 k)145 VOID ixheaacd_fix2flt_data(ia_usac_data_struct *usac_data,
146                            ia_usac_lpd_decoder_handle st, WORD32 k) {
147   WORD32 i;
148   WORD32 fac_length;
149   WORD32 window_sequence_last = usac_data->window_sequence_last[k];
150   WORD32 *p_ola_buffer = usac_data->overlap_data_ptr[k];
151   if (window_sequence_last == EIGHT_SHORT_SEQUENCE) {
152     fac_length = (usac_data->ccfl) / 16;
153   } else {
154     fac_length = (usac_data->len_subfrm) / 2;
155   }
156 
157   ixheaacd_memset(st->lp_flt_coeff_a_prev, 2 * (ORDER + 1));
158   ixheaacd_memset(st->exc_prev, 1 + (2 * FAC_LENGTH));
159   ixheaacd_memset(st->xcitation_prev, MAX_PITCH + INTER_LP_FIL_ORDER + 1);
160   ixheaacd_memset(st->synth_prev, MAX_PITCH + SYNTH_DELAY_LMAX);
161   ixheaacd_memset(st->bpf_prev, FILTER_DELAY + LEN_SUBFR);
162 
163   st->gain_threshold = 0.0f;
164 
165   if (p_ola_buffer != NULL) {
166     for (i = 0; i < (usac_data->len_subfrm) / 2 - fac_length; i++) {
167       st->exc_prev[i] = 0;
168     }
169     for (i = 0; i < 2 * fac_length + 1; i++) {
170       st->exc_prev[(usac_data->len_subfrm) / 2 - fac_length + i] = (FLOAT32)(
171           p_ola_buffer[i + usac_data->ccfl / 2 - fac_length - 1] / 16384.0);
172     }
173   } else {
174     ixheaacd_memset(st->exc_prev, 1 + (2 * FAC_LENGTH));
175   }
176 
177   return;
178 }
179 
ixheaacd_init_acelp_data(ia_usac_data_struct * usac_data,ia_usac_lpd_decoder_handle st)180 void ixheaacd_init_acelp_data(ia_usac_data_struct *usac_data,
181                               ia_usac_lpd_decoder_handle st) {
182   ixheaacd_reset_acelp_data_fix(usac_data, st, NULL, 0, 0);
183 }
184 
185 #define PI_BY_6400 (PI / 6400.0)
186 #define SCALE1 (6400.0 / PI)
187 
ixheaacd_lsp_2_lsf_conversion(float lsp[],float lsf[],WORD32 m)188 void ixheaacd_lsp_2_lsf_conversion(float lsp[], float lsf[], WORD32 m) {
189   short i;
190   for (i = 0; i < m; i++) {
191     lsf[i] = (float)(acos(lsp[i]) * SCALE1);
192   }
193   return;
194 }
195 
ixheaacd_lsf_2_lsp_conversion_float(FLOAT32 lsf[],FLOAT32 lsp[],WORD32 m)196 static VOID ixheaacd_lsf_2_lsp_conversion_float(FLOAT32 lsf[], FLOAT32 lsp[],
197                                                 WORD32 m) {
198   WORD32 i;
199   for (i = 0; i < m; i++)
200     lsp[i] = (FLOAT32)cos((double)lsf[i] * (double)PI_BY_6400);
201 
202   return;
203 }
204 
ixheaacd_bass_post_filter(FLOAT32 * synth_sig,WORD32 * pitch,FLOAT32 * pitch_gain,FLOAT32 * synth_out,WORD32 len_fr,WORD32 len2,FLOAT32 bpf_prev[])205 static WORD32 ixheaacd_bass_post_filter(FLOAT32 *synth_sig, WORD32 *pitch,
206                                         FLOAT32 *pitch_gain, FLOAT32 *synth_out,
207                                         WORD32 len_fr, WORD32 len2,
208                                         FLOAT32 bpf_prev[]) {
209   WORD32 i, j, sf, num_subfr, pitch_lag, lg;
210   FLOAT32 x_energy, xy_corr, y_energy, norm_corr, energy, gain, tmp, alpha;
211   FLOAT32 noise_buf[FILTER_DELAY + (2 * LEN_SUBFR)], *noise_tmp1, *noise_tmp2,
212       *x, *y;
213 
214   noise_tmp1 = noise_buf + FILTER_DELAY;
215   noise_tmp2 = noise_buf + FILTER_DELAY + LEN_SUBFR;
216 
217   memcpy(synth_out, synth_sig - LEN_SUBFR, len_fr * sizeof(FLOAT32));
218 
219   if (len_fr % 64)
220     memset(synth_out + len_fr, 0, (LEN_SUBFR - len_fr % 64) * sizeof(FLOAT32));
221 
222   sf = 0;
223   for (num_subfr = 0; num_subfr < len_fr; num_subfr += LEN_SUBFR, sf++) {
224     pitch_lag = pitch[sf];
225     gain = pitch_gain[sf];
226     if (((pitch_lag >> 1) + 96 - num_subfr) > MAX_PITCH) return -1;
227     if (gain > 1.0f) gain = 1.0f;
228     if (gain < 0.0f) gain = 0.0f;
229 
230     x = &synth_sig[num_subfr - 96];
231     y = &synth_sig[num_subfr - pitch_lag / 2 - 96];
232 
233     x_energy = 0.01f;
234     xy_corr = 0.01f;
235     y_energy = 0.01f;
236     for (i = 0; i < LEN_SUBFR + 96; i++) {
237       x_energy += x[i] * x[i];
238       xy_corr += x[i] * y[i];
239       y_energy += y[i] * y[i];
240     }
241 
242     norm_corr = xy_corr / (FLOAT32)sqrt(x_energy * y_energy);
243 
244     if (norm_corr > 0.95f) pitch_lag >>= 1;
245 
246     lg = len_fr + len2 - pitch_lag - num_subfr;
247     if (lg < 0) lg = 0;
248     if (lg > LEN_SUBFR) lg = LEN_SUBFR;
249 
250     if (gain > 0) {
251       if (lg > 0) {
252         tmp = 0.01f;
253         for (i = 0; i < lg; i++) {
254           tmp += synth_sig[i + num_subfr] * synth_sig[i + num_subfr];
255         }
256         energy = 0.01f;
257         for (i = 0; i < lg; i++) {
258           energy += synth_sig[i + num_subfr + pitch_lag] *
259                     synth_sig[i + num_subfr + pitch_lag];
260         }
261         tmp = (FLOAT32)sqrt(tmp / energy);
262         if (tmp < gain) gain = tmp;
263       }
264 
265       alpha = 0.5f * gain;
266       for (i = 0; i < lg; i++) {
267         noise_tmp2[i] = alpha * (synth_sig[i + num_subfr] -
268                                  0.5f * synth_sig[i + num_subfr - pitch_lag] -
269                                  0.5f * synth_sig[i + num_subfr + pitch_lag]);
270       }
271       for (i = lg; i < LEN_SUBFR; i++) {
272         noise_tmp2[i] = alpha * (synth_sig[i + num_subfr] -
273                                  synth_sig[i + num_subfr - pitch_lag]);
274       }
275     } else {
276       memset(noise_tmp2, 0, LEN_SUBFR * sizeof(FLOAT32));
277     }
278 
279     memcpy(noise_buf, bpf_prev, (FILTER_DELAY + LEN_SUBFR) * sizeof(FLOAT32));
280     memcpy(bpf_prev, noise_buf + LEN_SUBFR,
281            (FILTER_DELAY + LEN_SUBFR) * sizeof(FLOAT32));
282 
283     for (i = 0; i < LEN_SUBFR; i++) {
284       tmp = ixheaacd_fir_lp_filt[0] * noise_tmp1[i];
285       for (j = 1; j <= FILTER_DELAY; j++) {
286         tmp +=
287             ixheaacd_fir_lp_filt[j] * (noise_tmp1[i - j] + noise_tmp1[i + j]);
288       }
289       synth_out[i + num_subfr] -= tmp;
290     }
291   }
292 
293   return 0;
294 }
295 
ixheaacd_reorder_lsf(float * lsf,float min_dist,int n)296 void ixheaacd_reorder_lsf(float *lsf, float min_dist, int n) {
297   int i;
298   float lsf_min;
299 
300   lsf_min = min_dist;
301   for (i = 0; i < n; i++) {
302     if (lsf[i] < lsf_min) lsf[i] = lsf_min;
303 
304     lsf_min = lsf[i] + min_dist;
305   }
306 
307   lsf_min = FREQ_MAX_F - min_dist;
308   for (i = n - 1; i >= 0; i--) {
309     if (lsf[i] > lsf_min) lsf[i] = lsf_min;
310 
311     lsf_min = lsf[i] - min_dist;
312   }
313 
314   return;
315 }
316 
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)317 WORD32 ixheaacd_lpd_dec(ia_usac_data_struct *usac_data,
318                         ia_usac_lpd_decoder_handle st,
319                         ia_td_frame_data_struct *pstr_td_frame_data,
320                         FLOAT32 fsynth[], WORD32 first_lpd_flag,
321                         WORD32 short_fac_flag, WORD32 bpf_control_info) {
322   FLOAT32 *synth_buf = usac_data->synth_buf;
323   FLOAT32 *xcitation_buff = usac_data->exc_buf;
324   FLOAT32 lsp_curr[ORDER];
325   FLOAT32 lsf_curr[ORDER];
326   FLOAT32 *lp_flt_coff_a = usac_data->lp_flt_coff;
327   FLOAT32 *synth, *xcitation_curr;
328   WORD32 *pitch = usac_data->pitch;
329   FLOAT32 *pitch_gain = usac_data->pitch_gain;
330   FLOAT32 lsf_flt[(2 * NUM_FRAMES + 1) * ORDER];
331 
332   WORD32 i, k, tp, mode;
333   WORD32 *mod;
334   FLOAT32 gain, stability_factor;
335   FLOAT32 tmp, synth_corr, synth_energy;
336 
337   WORD32 len_fr;
338   WORD32 len_subfrm;
339   WORD32 num_subfr;
340   WORD32 num_subfr_in_superfr;
341   WORD32 num_subfr_by2;
342   WORD32 synth_delay;
343   WORD32 num_samples = 0;
344 
345   WORD32 *ptr_scratch = &usac_data->scratch_buffer[0];
346 
347   WORD32 subfr_len, n_subfr;
348   WORD32 err = 0;
349 
350   len_fr = usac_data->ccfl;
351   len_subfrm = usac_data->len_subfrm;
352   num_subfr = usac_data->num_subfrm;
353   num_subfr_in_superfr = NUM_FRAMES * num_subfr;
354   num_subfr_by2 = (num_subfr_in_superfr / 2) - 1;
355   synth_delay = num_subfr_by2 * LEN_SUBFR;
356 
357   synth = synth_buf + MAX_PITCH + synth_delay;
358   ixheaacd_mem_cpy(st->synth_prev, synth_buf, MAX_PITCH + synth_delay);
359   ixheaacd_memset(synth, SYNTH_DELAY_LMAX + LEN_SUPERFRAME - synth_delay);
360 
361   xcitation_curr = xcitation_buff + MAX_PITCH + INTER_LP_FIL_ORDER + 1;
362   ixheaacd_mem_cpy(st->xcitation_prev, xcitation_buff,
363                    MAX_PITCH + INTER_LP_FIL_ORDER + 1);
364   memset(xcitation_curr, 0, sizeof(FLOAT32) * (LEN_SUPERFRAME + 1));
365 
366   mod = pstr_td_frame_data->mod;
367 
368   for (i = 0; i < num_subfr_by2; i++) {
369     pitch[i] = st->pitch_prev[i];
370     pitch_gain[i] = st->gain_prev[i];
371   }
372   for (i = 0; i < num_subfr_in_superfr; i++) {
373     pitch[i + num_subfr_by2] = 64;
374     pitch_gain[i + num_subfr_by2] = 0.0f;
375   }
376   if (!first_lpd_flag) {
377     ixheaacd_lsp_2_lsf_conversion(st->lspold, lsf_flt, ORDER);
378   }
379 
380   ixheaacd_alg_vec_dequant(pstr_td_frame_data, first_lpd_flag, lsf_flt,
381                            pstr_td_frame_data->mod);
382 
383   if (first_lpd_flag) {
384     ixheaacd_mem_cpy(&lsf_flt[0], st->lsf_prev, ORDER);
385     ixheaacd_lsf_2_lsp_conversion_float(st->lsf_prev, st->lspold, ORDER);
386   }
387 
388   if ((first_lpd_flag && mod[0] == 0) || (first_lpd_flag && mod[1] == 0) ||
389       ((first_lpd_flag && mod[2] == 0 && len_subfrm != LEN_FRAME))) {
390     FLOAT32 lp_flt_coeff_a[9 * (ORDER + 1)];
391     FLOAT32 tmp_buf[3 * LEN_FRAME + ORDER];
392     FLOAT32 tmp_res_buf[3 * LEN_FRAME];
393     FLOAT32 *tmp = &(tmp_buf[LEN_FRAME]);
394     FLOAT32 *ptr_tmp = &(tmp_res_buf[LEN_FRAME]);
395     WORD32 tmp_start;
396     FLOAT32 mem = 0;
397     WORD32 gain;
398     WORD32 length;
399 
400     ixheaacd_interpolation_lsp_params(st->lspold, st->lspold, lp_flt_coeff_a,
401                                       8);
402 
403     memcpy(st->lp_flt_coeff_a_prev, lp_flt_coeff_a,
404            (ORDER + 1) * sizeof(FLOAT32));
405     memcpy(st->lp_flt_coeff_a_prev + ORDER + 1, lp_flt_coeff_a,
406            (ORDER + 1) * sizeof(FLOAT32));
407 
408     if (mod[0] == 0) {
409       WORD32 fac_length;
410       if (short_fac_flag) {
411         fac_length = (len_subfrm * NUM_FRAMES) / 16;
412       } else {
413         fac_length = len_subfrm / 2;
414       }
415       if ((pstr_td_frame_data->fac_data[0] < 0) ||
416           (pstr_td_frame_data->fac_data[0] > 128)) {
417         return -1;
418       }
419       gain = ixheaacd_pow_10_i_by_128[pstr_td_frame_data->fac_data[0]];
420 
421       memcpy(ptr_scratch, &pstr_td_frame_data->fac_data[0],
422              129 * sizeof(WORD32));
423 
424       for (i = 0; i < 64; i++) {
425         pstr_td_frame_data->fac_data[i] = ptr_scratch[2 * i + 1] << 16;
426         pstr_td_frame_data->fac_data[64 + i] = ptr_scratch[fac_length - 2 * i]
427                                                << 16;
428       }
429 
430       err = ixheaacd_fwd_alias_cancel_tool(usac_data, pstr_td_frame_data,
431                                            fac_length, lp_flt_coeff_a, gain);
432       if (err == -1) return err;
433 
434       memset(
435           &usac_data->overlap_data_ptr[usac_data->present_chan][(len_fr / 2)],
436           0, fac_length * sizeof(WORD32));
437     }
438 
439     for (i = 0; i < 2 * len_subfrm; i++)
440       st->fd_synth[ORDER + i] = (FLOAT32)(
441           (FLOAT32)usac_data->overlap_data_ptr[usac_data->present_chan][i] /
442           16384.0);
443     num_samples = min(2 * len_subfrm, MAX_PITCH + synth_delay);
444 
445     ixheaacd_mem_cpy(st->fd_synth + ORDER, synth - 2 * len_subfrm,
446                      2 * len_subfrm);
447 
448     ixheaacd_preemphsis_tool_float(st->fd_synth + ORDER, PREEMPH_FILT_FAC,
449                                    2 * len_subfrm, mem);
450 
451     ixheaacd_memset(tmp, ORDER);
452     ixheaacd_mem_cpy(st->fd_synth + ORDER, tmp + ORDER, 2 * len_subfrm);
453     tmp_start = 0;
454 
455     ixheaacd_memset(ptr_tmp - len_subfrm, 3 * len_subfrm);
456     memset(st->fd_synth, 0, ORDER * sizeof(WORD32));
457     length = (2 * len_subfrm - tmp_start) / LEN_SUBFR;
458 
459     ixheaacd_residual_tool_float1(lp_flt_coeff_a,
460                                   &st->fd_synth[ORDER + tmp_start],
461                                   &ptr_tmp[tmp_start], LEN_SUBFR, length);
462 
463     if (mod[0] != 0 && (len_subfrm == LEN_FRAME || mod[1] != 0)) {
464       num_samples = min(len_subfrm, MAX_PITCH + INTER_LP_FIL_ORDER + 1);
465     } else {
466       num_samples = min(2 * len_subfrm, MAX_PITCH + INTER_LP_FIL_ORDER + 1);
467     }
468     ixheaacd_mem_cpy(ptr_tmp + 2 * len_subfrm - num_samples,
469                      xcitation_curr - num_samples, num_samples);
470   }
471 
472   k = 0;
473 
474   while (k < 4) {
475     mode = mod[k];
476     if ((st->mode_prev == 0) && (mode > 0) &&
477         (k != 0 || st->bpf_active_prev == 1)) {
478       i = (k * num_subfr) + num_subfr_by2;
479       pitch[i + 1] = pitch[i] = pitch[i - 1];
480       pitch_gain[i + 1] = pitch_gain[i] = pitch_gain[i - 1];
481     }
482 
483     if ((mode == 0) || (mode == 1))
484       memcpy(lsf_curr, &lsf_flt[(k + 1) * ORDER], ORDER * sizeof(FLOAT32));
485     else if (mode == 2)
486       memcpy(lsf_curr, &lsf_flt[(k + 2) * ORDER], ORDER * sizeof(FLOAT32));
487     else
488       memcpy(lsf_curr, &lsf_flt[(k + 4) * ORDER], ORDER * sizeof(FLOAT32));
489 
490     ixheaacd_lsf_2_lsp_conversion_float(lsf_curr, lsp_curr, ORDER);
491 
492     tmp = 0.0f;
493     for (i = 0; i < ORDER; i++) {
494       tmp += (lsf_curr[i] - st->lsf_prev[i]) * (lsf_curr[i] - st->lsf_prev[i]);
495     }
496     stability_factor = (FLOAT32)(1.25f - (tmp / 400000.0f));
497     if (stability_factor > 1.0f) {
498       stability_factor = 1.0f;
499     }
500     if (stability_factor < 0.0f) {
501       stability_factor = 0.0f;
502     }
503 
504     if (mode == 0) {
505       ixheaacd_interpolation_lsp_params(st->lspold, lsp_curr, lp_flt_coff_a,
506                                         num_subfr);
507 
508       ixheaacd_acelp_alias_cnx(usac_data, pstr_td_frame_data, k, lp_flt_coff_a,
509                                stability_factor, st);
510 
511       if ((st->mode_prev != 0) && bpf_control_info) {
512         i = (k * num_subfr) + num_subfr_by2;
513         pitch[i - 1] = pitch[i];
514         pitch_gain[i - 1] = pitch_gain[i];
515         if (st->mode_prev != -2) {
516           pitch[i - 2] = pitch[i];
517           pitch_gain[i - 2] = pitch_gain[i];
518         }
519       }
520       k++;
521     } else {
522       if (mode == 1) {
523         subfr_len = len_subfrm;
524         n_subfr = num_subfr;
525       } else if (mode == 2) {
526         subfr_len = len_subfrm << 1;
527         n_subfr = num_subfr_in_superfr / 2;
528       } else if (mode == 3) {
529         subfr_len = len_subfrm << 2;
530         n_subfr = num_subfr_in_superfr;
531       }
532 
533       ixheaacd_lpc_coef_gen(st->lspold, lsp_curr, lp_flt_coff_a, n_subfr,
534                             ORDER);
535 
536       ixheaacd_tcx_mdct(usac_data, pstr_td_frame_data, k, lp_flt_coff_a,
537                         subfr_len, st);
538       k += (1 << (mode - 1));
539     }
540 
541     st->mode_prev = mode;
542 
543     ixheaacd_mem_cpy(lsp_curr, st->lspold, ORDER);
544     ixheaacd_mem_cpy(lsf_curr, st->lsf_prev, ORDER);
545   }
546 
547   ixheaacd_mem_cpy(xcitation_buff + len_fr, st->xcitation_prev,
548                    MAX_PITCH + INTER_LP_FIL_ORDER + 1);
549 
550   ixheaacd_mem_cpy(synth_buf + len_fr, st->synth_prev, MAX_PITCH + synth_delay);
551 
552   if (!bpf_control_info) {
553     if (mod[0] != 0 && st->bpf_active_prev) {
554       for (i = 2; i < num_subfr_in_superfr; i++)
555         pitch_gain[num_subfr_by2 + i] = 0.0;
556     } else {
557       for (i = 0; i < num_subfr_in_superfr; i++)
558         pitch_gain[num_subfr_by2 + i] = 0.0;
559     }
560   }
561   st->bpf_active_prev = bpf_control_info;
562 
563   for (i = 0; i < num_subfr_by2; i++) {
564     st->pitch_prev[i] = pitch[num_subfr_in_superfr + i];
565     st->gain_prev[i] = pitch_gain[num_subfr_in_superfr + i];
566   }
567 
568   synth = synth_buf + MAX_PITCH;
569 
570   for (i = 0; i < num_subfr_in_superfr; i++) {
571     tp = pitch[i];
572     gain = pitch_gain[i];
573     if (gain > 0.0f) {
574       synth_corr = 0.0f, synth_energy = 1e-6f;
575       if ((((i * LEN_SUBFR) + LEN_SUBFR) > LEN_SUPERFRAME) ||
576           ((((i * LEN_SUBFR) + LEN_SUBFR) - tp) > LEN_SUPERFRAME))
577         return -1;
578       for (k = 0; k < LEN_SUBFR; k++) {
579         synth_corr +=
580             synth[i * LEN_SUBFR + k] * synth[(i * LEN_SUBFR) - tp + k];
581         synth_energy +=
582             synth[(i * LEN_SUBFR) - tp + k] * synth[(i * LEN_SUBFR) - tp + k];
583       }
584       pitch_gain[i] = synth_corr / synth_energy;
585     }
586   }
587 
588   if (mod[3] == 0) {
589     err = ixheaacd_bass_post_filter(synth, pitch, pitch_gain, fsynth, len_fr,
590                                     synth_delay, st->bpf_prev);
591   } else {
592     err =
593         ixheaacd_bass_post_filter(synth, pitch, pitch_gain, fsynth, len_fr,
594                                   synth_delay - (len_subfrm / 2), st->bpf_prev);
595   }
596   return err;
597 }
598 
ixheaacd_lpd_dec_update(ia_usac_lpd_decoder_handle tddec,ia_usac_data_struct * usac_data,WORD32 i_ch)599 WORD32 ixheaacd_lpd_dec_update(ia_usac_lpd_decoder_handle tddec,
600                                ia_usac_data_struct *usac_data, WORD32 i_ch) {
601   WORD32 err = 0, i, k;
602 
603   WORD32 *ptr_overlap = &usac_data->overlap_data_ptr[i_ch][0];
604   WORD32 len_fr, lpd_sbf_len, lpd_delay, num_subfr_by2, synth_delay, fac_length;
605 
606   if (usac_data->tw_mdct[0])
607     ptr_overlap = &usac_data->overlap_data_ptr[i_ch][usac_data->ccfl / 2];
608 
609   len_fr = usac_data->ccfl;
610   lpd_sbf_len = (NUM_FRAMES * usac_data->num_subfrm) / 2;
611   lpd_delay = lpd_sbf_len * LEN_SUBFR;
612   num_subfr_by2 = lpd_sbf_len - 1;
613   synth_delay = num_subfr_by2 * LEN_SUBFR;
614   fac_length = (usac_data->len_subfrm) / 2;
615 
616   for (i = 0; i < LEN_SUBFR + synth_delay; i++)
617     ptr_overlap[i] = (WORD32)(
618         (FLOAT32)tddec->synth_prev[MAX_PITCH - (LEN_SUBFR) + i] * 16384.0);
619 
620   ptr_overlap += LEN_SUBFR + synth_delay - fac_length;
621 
622   for (k = 0; k < 2 * fac_length; k++)
623     ptr_overlap[k] = (WORD32)((FLOAT32)tddec->exc_prev[k + 1] * 16384.0);
624 
625   ptr_overlap = &usac_data->overlap_data_ptr[i_ch][lpd_delay + fac_length];
626 
627   for (i = 0; i < len_fr - lpd_delay - fac_length; i++) ptr_overlap[i] = 0;
628 
629   usac_data->window_shape[i_ch] = WIN_SEL_0;
630   usac_data->window_sequence_last[i_ch] = EIGHT_SHORT_SEQUENCE;
631   usac_data->td_frame_prev[i_ch] = 1;
632 
633   if (tddec->mode_prev == 0) {
634     memmove(usac_data->lpc_prev[i_ch], &tddec->lp_flt_coeff_a_prev[ORDER + 1],
635             (ORDER + 1) * sizeof(FLOAT32));
636     memmove(usac_data->acelp_in[i_ch], tddec->exc_prev,
637             (1 + (2 * FAC_LENGTH)) * sizeof(FLOAT32));
638   }
639 
640   return err;
641 }
642 
ixheaacd_lpd_bpf_fix(ia_usac_data_struct * usac_data,WORD32 is_short_flag,FLOAT32 out_buffer[],ia_usac_lpd_decoder_handle st)643 WORD32 ixheaacd_lpd_bpf_fix(ia_usac_data_struct *usac_data,
644                             WORD32 is_short_flag, FLOAT32 out_buffer[],
645                             ia_usac_lpd_decoder_handle st) {
646   WORD32 i, tp, k;
647   float synth_buf[MAX_PITCH + SYNTH_DELAY_LMAX + LEN_SUPERFRAME];
648   float signal_out[LEN_SUPERFRAME];
649   float *synth, synth_corr, synth_energy;
650   WORD32 pitch[NUM_SUBFR_SUPERFRAME_BY2 + 3];
651   float pitch_gain[NUM_SUBFR_SUPERFRAME_BY2 + 3];
652   WORD32 len_fr, lpd_sbf_len, lpd_delay, num_subfr_by2, synth_delay, fac_length;
653   WORD32 err = 0;
654 
655   len_fr = usac_data->ccfl;
656   lpd_sbf_len = (NUM_FRAMES * usac_data->num_subfrm) / 2;
657   lpd_delay = lpd_sbf_len * LEN_SUBFR;
658   num_subfr_by2 = lpd_sbf_len - 1;
659   synth_delay = num_subfr_by2 * LEN_SUBFR;
660   fac_length = (usac_data->len_subfrm) / 2;
661 
662   ixheaacd_memset(synth_buf, MAX_PITCH + synth_delay + len_fr);
663   ixheaacd_mem_cpy(st->synth_prev, synth_buf, MAX_PITCH + synth_delay);
664   ixheaacd_mem_cpy(out_buffer, synth_buf + MAX_PITCH - (LEN_SUBFR),
665                    synth_delay + len_fr + (LEN_SUBFR));
666 
667   for (i = 0; i < num_subfr_by2; i++) {
668     pitch[i] = st->pitch_prev[i];
669     pitch_gain[i] = st->gain_prev[i];
670   }
671   for (i = num_subfr_by2; i < lpd_sbf_len + 3; i++) {
672     pitch[i] = 64;
673     pitch_gain[i] = 0.0f;
674   }
675   if (st->mode_prev == 0) {
676     pitch[num_subfr_by2] = pitch[num_subfr_by2 - 1];
677     pitch_gain[num_subfr_by2] = pitch_gain[num_subfr_by2 - 1];
678     if (!is_short_flag) {
679       pitch[num_subfr_by2 + 1] = pitch[num_subfr_by2];
680       pitch_gain[num_subfr_by2 + 1] = pitch_gain[num_subfr_by2];
681     }
682   }
683 
684   synth = synth_buf + MAX_PITCH;
685 
686   for (i = 0; i < num_subfr_by2 + 2; i++) {
687     tp = pitch[i];
688     if ((i * LEN_SUBFR + MAX_PITCH) < tp) {
689       return -1;
690     } else if (((i * LEN_SUBFR + MAX_PITCH - tp) >= 1883) ||
691                (((i * LEN_SUBFR) + LEN_SUBFR) > LEN_SUPERFRAME) ||
692                ((((i * LEN_SUBFR) + LEN_SUBFR) - tp) > LEN_SUPERFRAME)) {
693       return -1;
694     }
695 
696     if (pitch_gain[i] > 0.0f) {
697       synth_corr = 0.0f, synth_energy = 1e-6f;
698       for (k = 0; k < LEN_SUBFR; k++) {
699         synth_corr +=
700             synth[i * LEN_SUBFR + k] * synth[(i * LEN_SUBFR) - tp + k];
701         synth_energy +=
702             synth[(i * LEN_SUBFR) - tp + k] * synth[(i * LEN_SUBFR) - tp + k];
703       }
704       pitch_gain[i] = synth_corr / synth_energy;
705     }
706   }
707 
708   err = ixheaacd_bass_post_filter(synth, pitch, pitch_gain, signal_out,
709                                   (lpd_sbf_len + 2) * LEN_SUBFR + LEN_SUBFR,
710                                   len_fr - (lpd_sbf_len + 2) * LEN_SUBFR,
711                                   st->bpf_prev);
712   if (err != 0) return err;
713 
714   ixheaacd_mem_cpy(signal_out, out_buffer,
715                    (lpd_sbf_len + 2) * LEN_SUBFR + LEN_SUBFR);
716   return err;
717 }
718