• 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 <string.h>
21 #include <math.h>
22 #include "ixheaacd_sbr_common.h"
23 #include "ixheaacd_type_def.h"
24 
25 #include "ixheaacd_constants.h"
26 #include "ixheaacd_basic_ops32.h"
27 #include "ixheaacd_basic_ops16.h"
28 #include "ixheaacd_basic_ops40.h"
29 #include "ixheaacd_basic_ops.h"
30 #include "ixheaacd_defines.h"
31 
32 #include "ixheaacd_intrinsics.h"
33 #include "ixheaacd_sbr_const.h"
34 #include "ixheaacd_basic_op.h"
35 #include "ixheaacd_defines.h"
36 #include "ixheaacd_bitbuffer.h"
37 #include "ixheaacd_pns.h"
38 
39 #include "ixheaacd_aac_rom.h"
40 #include "ixheaacd_pulsedata.h"
41 
42 #include "ixheaacd_drc_data_struct.h"
43 #include "ixheaacd_lt_predict.h"
44 #include "ixheaacd_cnst.h"
45 #include "ixheaacd_ec_defines.h"
46 #include "ixheaacd_ec_struct_def.h"
47 #include "ixheaacd_channelinfo.h"
48 #include "ixheaacd_drc_dec.h"
49 
50 #include "ixheaacd_sbrdecoder.h"
51 
52 #include "ixheaacd_sbrdecsettings.h"
53 #include "ixheaacd_sbr_scale.h"
54 #include "ixheaacd_lpp_tran.h"
55 #include "ixheaacd_env_extr_part.h"
56 #include "ixheaacd_sbr_rom.h"
57 #include "ixheaacd_hybrid.h"
58 #include "ixheaacd_ps_dec.h"
59 #include "ixheaacd_ps_bitdec.h"
60 #include "ixheaacd_env_extr.h"
61 #include "ixheaacd_common_rom.h"
62 #include "ixheaacd_freq_sca.h"
63 
64 #include "ixheaacd_qmf_dec.h"
65 
66 #include "ixheaacd_env_calc.h"
67 
68 #include "ixheaacd_pvc_dec.h"
69 #include "ixheaacd_sbr_dec.h"
70 #include "ixheaacd_env_dec.h"
71 #include "ixheaacd_basic_funcs.h"
72 #include "ixheaacd_sbr_crc.h"
73 #include "ixheaacd_function_selector.h"
74 
75 #include "ixheaacd_audioobjtypes.h"
76 #include "ixheaacd_error_codes.h"
77 
78 #define ALIGN_SIZE64(x) ((((x) + 7) >> 3) << 3)
79 
80 static const FLOAT32 ixheaacd_new_bw_table[4][4] = {
81     {0.00f, 0.60f, 0.90f, 0.98f},
82     {0.60f, 0.75f, 0.90f, 0.98f},
83     {0.00f, 0.75f, 0.90f, 0.98f},
84     {0.00f, 0.75f, 0.90f, 0.98f}};
85 static const WORD32 ixheaacd_inew_bw_table[4][4] = {
86     {0x00000000, 0x4ccccccd, 0x73333333, 0x7d70a3d7},
87     {0x4ccccccd, 0x60000000, 0x73333333, 0x7d70a3d7},
88     {0x00000000, 0x60000000, 0x73333333, 0x7d70a3d7},
89     {0x00000000, 0x60000000, 0x73333333, 0x7d70a3d7}};
90 
ixheaacd_reset_sbrenvelope_calc(ia_sbr_calc_env_struct * h_cal_env)91 VOID ixheaacd_reset_sbrenvelope_calc(ia_sbr_calc_env_struct *h_cal_env) {
92   h_cal_env->ph_index = 0;
93   h_cal_env->filt_buf_noise_e = 0;
94   h_cal_env->start_up = 1;
95 }
96 
ixheaacd_derive_lim_band_tbl(ia_sbr_header_data_struct * ptr_header_data,const ia_patch_param_struct * p_str_patch_param,WORD16 num_patches,ixheaacd_misc_tables * pstr_common_tables)97 VOID ixheaacd_derive_lim_band_tbl(
98     ia_sbr_header_data_struct *ptr_header_data,
99     const ia_patch_param_struct *p_str_patch_param, WORD16 num_patches,
100     ixheaacd_misc_tables *pstr_common_tables) {
101   WORD32 i, k, k_1;
102   WORD32 nr_lim, patch_border_k, patch_border_k_1, temp_nr_lim;
103 
104   WORD16 lim_table[MAX_FREQ_COEFFS / 2 + MAX_NUM_PATCHES + 1];
105   WORD16 patch_borders[MAX_NUM_PATCHES + 1];
106   WORD16 kx, k2;
107   WORD16 temp, lim_bands, num_octaves;
108 
109   WORD16 *f_lim_tbl = ptr_header_data->pstr_freq_band_data->freq_band_tbl_lim;
110   WORD16 *num_lf_bands = &ptr_header_data->pstr_freq_band_data->num_lf_bands;
111   WORD16 *f_low_tbl =
112       ptr_header_data->pstr_freq_band_data->freq_band_table[LOW];
113   WORD16 num_low_bnd = ptr_header_data->pstr_freq_band_data->num_sf_bands[LOW];
114   WORD16 limiter_bands = ptr_header_data->limiter_bands;
115 
116   WORD16 sub_band_start = f_low_tbl[0];
117   WORD16 sub_band_end = f_low_tbl[num_low_bnd];
118   const WORD16 limbnd_per_oct[4] = {(WORD16)0x2000, (WORD16)0x2666, (WORD16)0x4000,
119                                     (WORD16)0x6000};
120 
121   if (limiter_bands == 0) {
122     f_lim_tbl[0] = 0;
123     f_lim_tbl[1] = sub_band_end - sub_band_start;
124     nr_lim = 1;
125   } else {
126     for (k = 0; k < num_patches; k++) {
127       patch_borders[k] = p_str_patch_param[k].guard_start_band - sub_band_start;
128     }
129     patch_borders[k] = sub_band_end - sub_band_start;
130 
131     for (k = 0; k <= num_low_bnd; k++) {
132       lim_table[k] = f_low_tbl[k] - sub_band_start;
133     }
134     for (k = 1; k < num_patches; k++) {
135       lim_table[num_low_bnd + k] = patch_borders[k];
136     }
137 
138     temp_nr_lim = nr_lim = (num_low_bnd + num_patches) - 1;
139     ixheaacd_aac_shellsort(lim_table, (temp_nr_lim + 1));
140 
141     k = 1;
142     k_1 = 0;
143 
144     lim_bands = limbnd_per_oct[limiter_bands];
145 
146     while ((k - temp_nr_lim) <= 0) {
147       k2 = lim_table[k] + sub_band_start;
148       kx = lim_table[k_1] + sub_band_start;
149 
150       num_octaves = pstr_common_tables->log_dual_is_table[k2];
151       num_octaves -= pstr_common_tables->log_dual_is_table[kx];
152 
153       temp = (WORD16)(((WORD32)lim_bands * (WORD32)num_octaves) >> 15);
154 
155       if (temp < 0x01f6) {
156         if (lim_table[k_1] == lim_table[k]) {
157           lim_table[k] = sub_band_end;
158           nr_lim = nr_lim - 1;
159           k = (k + 1);
160           continue;
161         }
162         patch_border_k_1 = patch_border_k = 0;
163 
164         for (i = 0; i <= num_patches; i++) {
165           if (lim_table[k] == patch_borders[i]) {
166             patch_border_k = 1;
167           }
168           if (lim_table[k_1] == patch_borders[i]) {
169             patch_border_k_1 = 1;
170           }
171         }
172         if (!patch_border_k) {
173           lim_table[k] = sub_band_end;
174           nr_lim = nr_lim - 1;
175           k = (k + 1);
176           continue;
177         }
178 
179         if (!patch_border_k_1) {
180           lim_table[k_1] = sub_band_end;
181           nr_lim = nr_lim - 1;
182         }
183       }
184       k_1 = k;
185       k = (k + 1);
186     }
187     ixheaacd_aac_shellsort(lim_table, (temp_nr_lim + 1));
188 
189     memcpy(f_lim_tbl, lim_table, sizeof(WORD16) * (nr_lim + 1));
190   }
191   *num_lf_bands = nr_lim;
192 
193   return;
194 }
195 
ixheaacd_lean_sbrconcealment(ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct * ptr_sbr_data,ia_sbr_prev_frame_data_struct * ptr_prev_data)196 VOID ixheaacd_lean_sbrconcealment(
197     ia_sbr_header_data_struct *ptr_header_data,
198     ia_sbr_frame_info_data_struct *ptr_sbr_data,
199     ia_sbr_prev_frame_data_struct *ptr_prev_data) {
200   WORD32 target;
201   WORD32 step;
202   WORD32 i;
203 
204   WORD16 cur_start_pos;
205   WORD16 cur_stop_pos;
206 
207   ptr_sbr_data->amp_res = ptr_prev_data->amp_res;
208   ptr_sbr_data->coupling_mode = ptr_prev_data->coupling_mode;
209   ptr_sbr_data->max_qmf_subband_aac = ptr_prev_data->max_qmf_subband_aac;
210 
211   memcpy(ptr_sbr_data->sbr_invf_mode, ptr_prev_data->sbr_invf_mode,
212          sizeof(WORD32) * MAX_INVF_BANDS);
213 
214   ptr_sbr_data->str_frame_info_details.num_env = 1;
215 
216   cur_start_pos = ptr_prev_data->end_position - ptr_header_data->num_time_slots;
217   cur_stop_pos = ptr_header_data->num_time_slots;
218 
219   ptr_sbr_data->str_frame_info_details.border_vec[0] = cur_start_pos;
220   ptr_sbr_data->str_frame_info_details.border_vec[1] = cur_stop_pos;
221 
222   ptr_sbr_data->str_frame_info_details.noise_border_vec[0] = cur_start_pos;
223   ptr_sbr_data->str_frame_info_details.noise_border_vec[1] = cur_stop_pos;
224   ;
225 
226   ptr_sbr_data->str_frame_info_details.freq_res[0] = 1;
227   ptr_sbr_data->str_frame_info_details.transient_env = -1;
228   ptr_sbr_data->str_frame_info_details.num_noise_env = 1;
229 
230   ptr_sbr_data->num_env_sfac =
231       ptr_header_data->pstr_freq_band_data->num_sf_bands[1];
232 
233   ptr_sbr_data->del_cod_dir_arr[0] = DTDF_DIR_TIME;
234 
235   if (ptr_sbr_data->coupling_mode == COUPLING_BAL) {
236     target = SBR_ENERGY_PAN_OFFSET;
237   } else {
238     target = 0;
239   }
240 
241   step = 1;
242 
243   if (ptr_header_data->amp_res - SBR_AMPLITUDE_RESOLUTION_1_5 == 0) {
244     target = (target << 1);
245     step = (step << 1);
246   }
247 
248   for (i = 0; i < ptr_sbr_data->num_env_sfac; i++) {
249     if (ptr_prev_data->sfb_nrg_prev[i] > target)
250       ptr_sbr_data->int_env_sf_arr[i] = -(step);
251     else
252       ptr_sbr_data->int_env_sf_arr[i] = step;
253   }
254 
255   ptr_sbr_data->del_cod_dir_noise_arr[0] = DTDF_DIR_TIME;
256 
257   memset(ptr_sbr_data->int_noise_floor, 0,
258          sizeof(ptr_sbr_data->int_noise_floor));
259 
260   memset(ptr_sbr_data->add_harmonics, 0, sizeof(FLAG) * MAX_FREQ_COEFFS);
261 }
262 
ixheaacd_find_closest_entry(WORD32 goal_sb,WORD16 * f_master_tbl,WORD16 num_mf_bands,WORD16 direction)263 static WORD16 ixheaacd_find_closest_entry(WORD32 goal_sb, WORD16 *f_master_tbl,
264                                           WORD16 num_mf_bands,
265                                           WORD16 direction) {
266   WORD32 index;
267 
268   if (goal_sb <= f_master_tbl[0]) return f_master_tbl[0];
269 
270   if (goal_sb >= f_master_tbl[num_mf_bands]) return f_master_tbl[num_mf_bands];
271 
272   if (direction) {
273     index = 0;
274     while (f_master_tbl[index] < goal_sb) {
275       index++;
276     }
277   } else {
278     index = num_mf_bands;
279     while (f_master_tbl[index] > goal_sb) {
280       index--;
281     }
282   }
283 
284   return f_master_tbl[index];
285 }
286 
ixheaacd_reset_hf_generator(ia_sbr_hf_generator_struct * ptr_hf_gen_str,ia_sbr_header_data_struct * ptr_header_data,WORD audio_object_type)287 WORD32 ixheaacd_reset_hf_generator(ia_sbr_hf_generator_struct *ptr_hf_gen_str,
288                                    ia_sbr_header_data_struct *ptr_header_data,
289                                    WORD audio_object_type) {
290   WORD32 patch, sb;
291   WORD32 temp;
292   WORD16 *ptr_noise_freq_tbl;
293   WORD32 num_nf_bands;
294 
295   ia_transposer_settings_struct *pstr_transposer_settings =
296       ptr_hf_gen_str->pstr_settings;
297   ia_patch_param_struct *p_str_patch_param =
298       pstr_transposer_settings->str_patch_param;
299 
300   WORD32 sub_band_start = ptr_header_data->pstr_freq_band_data->sub_band_start;
301   WORD16 *f_master_tbl = ptr_header_data->pstr_freq_band_data->f_master_tbl;
302   WORD16 num_mf_bands = ptr_header_data->pstr_freq_band_data->num_mf_bands;
303   WORD16 usb = ptr_header_data->pstr_freq_band_data->sub_band_end;
304 
305   WORD32 src_start_band;
306   WORD32 patch_stride;
307   WORD32 num_bands_in_patch;
308 
309   WORD32 lsb = f_master_tbl[0];
310   WORD16 xover_offset = sub_band_start - lsb;
311 
312   WORD16 goal_sb;
313   WORD32 fs = ptr_header_data->out_sampling_freq;
314 
315   if (lsb < (SHIFT_START_SB + 4)) {
316     return (1);
317   }
318   switch (fs) {
319     case 16000:
320     case 22050:
321     case 24000:
322     case 32000:
323       goal_sb = 64;
324       break;
325     case 44100:
326       goal_sb = 46;
327       break;
328     case 48000:
329       goal_sb = 43;
330       break;
331     case 64000:
332       goal_sb = 32;
333       break;
334     case 88200:
335       goal_sb = 23;
336       break;
337     case 96000:
338       goal_sb = 21;
339       break;
340     default:
341       return (0);
342   }
343 
344   goal_sb = ixheaacd_find_closest_entry(goal_sb, f_master_tbl, num_mf_bands, 1);
345   if (audio_object_type != AOT_ER_AAC_ELD &&
346       audio_object_type != AOT_ER_AAC_LD) {
347     if (ixheaacd_abs16_sat((WORD16)(goal_sb - usb)) < 4) {
348       goal_sb = usb;
349     }
350   }
351 
352   src_start_band = SHIFT_START_SB + xover_offset;
353   sb = (lsb + xover_offset);
354 
355   patch = 0;
356 
357   if ((goal_sb < sb) && (lsb > src_start_band)) {
358     return -1;
359   }
360 
361   while (((sb - usb) < 0) && (patch < MAX_NUM_PATCHES)) {
362     ia_patch_param_struct *ptr_loc_patch_param = &p_str_patch_param[patch];
363     WORD16 abs_sb, flag_break = 0;
364     ptr_loc_patch_param->guard_start_band = sb;
365     sb = (sb + GUARDBANDS);
366     ptr_loc_patch_param->dst_start_band = sb;
367 
368     num_bands_in_patch = (goal_sb - sb);
369     if ((num_bands_in_patch <= 0) &&
370         ((num_bands_in_patch - (lsb - src_start_band)) < 0)) {
371       flag_break = 1;
372     }
373     if ((num_bands_in_patch - (lsb - src_start_band)) >= 0) {
374       patch_stride = sb - src_start_band;
375       patch_stride = (WORD16)(patch_stride & ~1);
376       num_bands_in_patch = (lsb - (sb - patch_stride));
377       num_bands_in_patch = ixheaacd_find_closest_entry(
378           sb + num_bands_in_patch, f_master_tbl, num_mf_bands, 0);
379       num_bands_in_patch -= sb;
380     }
381 
382     patch_stride = ((num_bands_in_patch + sb) - lsb);
383     patch_stride = (WORD16)((patch_stride + 1) & ~1);
384 
385     if (num_bands_in_patch > 0) {
386       ptr_loc_patch_param->src_start_band = (sb - patch_stride);
387       ptr_loc_patch_param->dst_end_band = patch_stride;
388       ptr_loc_patch_param->num_bands_in_patch = num_bands_in_patch;
389       ptr_loc_patch_param->src_end_band =
390           (ptr_loc_patch_param->src_start_band + num_bands_in_patch);
391 
392       sb = (sb + ptr_loc_patch_param->num_bands_in_patch);
393       patch++;
394     }
395 
396     src_start_band = SHIFT_START_SB;
397     abs_sb = ixheaacd_abs16_sat((WORD16)((sb - goal_sb))) - 3;
398 
399     if (abs_sb < 0) {
400       goal_sb = usb;
401     } else {
402       if (flag_break == 1) break;
403     }
404   }
405 
406   patch--;
407 
408   if ((patch > 0) && (p_str_patch_param[patch].num_bands_in_patch < 3)) {
409     patch--;
410     sb = p_str_patch_param[patch].dst_start_band +
411          p_str_patch_param[patch].num_bands_in_patch;
412   }
413 
414   if (patch >= MAX_NUM_PATCHES) {
415     return -1;
416   }
417 
418   pstr_transposer_settings->num_patches = patch + 1;
419 
420   temp = 0;
421 
422   for (patch = 0; patch < pstr_transposer_settings->num_patches; patch++) {
423     sb = ixheaacd_min32(sb, p_str_patch_param[patch].src_start_band);
424     temp = ixheaacd_max32(temp, p_str_patch_param[patch].src_end_band);
425   }
426 
427   if (sb > temp) return IA_FATAL_ERROR;
428 
429   pstr_transposer_settings->start_patch = sb;
430   pstr_transposer_settings->stop_patch = temp;
431 
432   ptr_noise_freq_tbl =
433       ptr_header_data->pstr_freq_band_data->freq_band_tbl_noise;
434   num_nf_bands = ptr_header_data->pstr_freq_band_data->num_nf_bands;
435 
436   memcpy(&pstr_transposer_settings->bw_borders[0], &ptr_noise_freq_tbl[1],
437          sizeof(WORD16) * num_nf_bands);
438 
439   memset(ptr_hf_gen_str->bw_array_prev, 0, sizeof(WORD32) * MAX_NUM_PATCHES);
440 
441   return 0;
442 }
ixheaacd_rescale_x_overlap(ia_sbr_dec_struct * ptr_sbr_dec,ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct * ptr_frame_data,ia_sbr_prev_frame_data_struct * ptr_frame_data_prev,WORD32 ** pp_overlap_buffer_real,WORD32 ** pp_overlap_buffer_imag,FLAG low_pow_flag)443 VOID ixheaacd_rescale_x_overlap(
444     ia_sbr_dec_struct *ptr_sbr_dec, ia_sbr_header_data_struct *ptr_header_data,
445     ia_sbr_frame_info_data_struct *ptr_frame_data,
446     ia_sbr_prev_frame_data_struct *ptr_frame_data_prev,
447     WORD32 **pp_overlap_buffer_real, WORD32 **pp_overlap_buffer_imag,
448     FLAG low_pow_flag) {
449   WORD32 k, l;
450   WORD32 start_band, end_band;
451   WORD32 target_lsb, target_usb;
452   WORD32 source_scale, target_scale, delta_scale, reserve;
453 
454   WORD32 old_lsb = ptr_frame_data_prev->max_qmf_subband_aac;
455   WORD32 start_slot =
456       (ptr_header_data->time_step *
457        (ptr_frame_data_prev->end_position - ptr_header_data->num_time_slots));
458   WORD32 new_lsb = ptr_frame_data->max_qmf_subband_aac;
459 
460   ptr_sbr_dec->str_codec_qmf_bank.usb = new_lsb;
461   ptr_sbr_dec->str_synthesis_qmf_bank.lsb = new_lsb;
462 
463   start_band = ixheaacd_min32(old_lsb, new_lsb);
464   end_band = ixheaacd_max32(old_lsb, new_lsb);
465 
466   if (new_lsb != old_lsb && old_lsb > 0) {
467     for (l = start_slot; l < 6; l++) {
468       for (k = old_lsb; k < new_lsb; k++) {
469         pp_overlap_buffer_real[l][k] = 0L;
470 
471         if (!low_pow_flag) {
472           pp_overlap_buffer_imag[l][k] = 0L;
473         }
474       }
475     }
476 
477     if (new_lsb > old_lsb) {
478       source_scale = ptr_sbr_dec->str_sbr_scale_fact.ov_hb_scale;
479       target_scale = ptr_sbr_dec->str_sbr_scale_fact.ov_lb_scale;
480       target_lsb = 0;
481       target_usb = old_lsb;
482     } else {
483       source_scale = ptr_sbr_dec->str_sbr_scale_fact.ov_lb_scale;
484       target_scale = ptr_sbr_dec->str_sbr_scale_fact.ov_hb_scale;
485       target_lsb = old_lsb;
486       target_usb = ptr_sbr_dec->str_synthesis_qmf_bank.usb;
487     }
488 
489     reserve = (*ixheaacd_ixheaacd_expsubbandsamples)(
490         pp_overlap_buffer_real, pp_overlap_buffer_imag, start_band, end_band, 0,
491         start_slot, low_pow_flag);
492 
493     (*ixheaacd_adjust_scale)(pp_overlap_buffer_real, pp_overlap_buffer_imag,
494                              start_band, end_band, 0, start_slot, reserve,
495                              low_pow_flag);
496 
497     source_scale += reserve;
498 
499     delta_scale = (target_scale - source_scale);
500 
501     if (delta_scale > 0) {
502       delta_scale = -(delta_scale);
503       start_band = target_lsb;
504       end_band = target_usb;
505 
506       if (new_lsb > old_lsb) {
507         ptr_sbr_dec->str_sbr_scale_fact.ov_lb_scale = source_scale;
508       } else {
509         ptr_sbr_dec->str_sbr_scale_fact.ov_hb_scale = source_scale;
510       }
511     }
512 
513     (*ixheaacd_adjust_scale)(pp_overlap_buffer_real, pp_overlap_buffer_imag,
514                              start_band, end_band, 0, start_slot, delta_scale,
515                              low_pow_flag);
516   }
517 }
518 
ixheaacd_map_sineflags(WORD16 * freq_band_table,WORD16 num_sf_bands,FLAG * add_harmonics,WORD8 * harm_flags_prev,WORD16 transient_env,WORD8 * sine_mapped)519 VOID ixheaacd_map_sineflags(WORD16 *freq_band_table, WORD16 num_sf_bands,
520                             FLAG *add_harmonics, WORD8 *harm_flags_prev,
521                             WORD16 transient_env, WORD8 *sine_mapped)
522 
523 {
524   WORD32 qmfband2, li, ui, i;
525   WORD32 low_subband_sec;
526   WORD32 oldflags;
527 
528   low_subband_sec = (freq_band_table[0] << 1);
529 
530   memset(sine_mapped, MAX_ENVELOPES, sizeof(WORD8) * MAX_FREQ_COEFFS);
531 
532   for (i = (num_sf_bands - 1); i >= 0; i--) {
533     oldflags = *harm_flags_prev;
534     *harm_flags_prev++ = add_harmonics[i];
535 
536     if (add_harmonics[i]) {
537       li = freq_band_table[i];
538 
539       ui = freq_band_table[i + 1];
540 
541       qmfband2 = ((ui + li) - low_subband_sec) >> 1;
542 
543       if (oldflags)
544         sine_mapped[qmfband2] = 0;
545       else
546         sine_mapped[qmfband2] = (WORD8)transient_env;
547     }
548   }
549 }
550 
ixheaacd_map_34_params_to_20(WORD16 * params)551 VOID ixheaacd_map_34_params_to_20(WORD16 *params) {
552   params[0] = ixheaacd_divideby3(params[0] + params[0] + params[1]);
553   params[1] = ixheaacd_divideby3(params[1] + params[2] + params[2]);
554   params[2] = ixheaacd_divideby3(params[3] + params[3] + params[4]);
555   params[3] = ixheaacd_divideby3(params[4] + params[5] + params[5]);
556   params[4] = ixheaacd_divideby2(params[6] + params[7]);
557   params[5] = ixheaacd_divideby2(params[8] + params[9]);
558   params[6] = params[10];
559   params[7] = params[11];
560   params[8] = ixheaacd_divideby2(params[12] + params[13]);
561   params[9] = ixheaacd_divideby2(params[14] + params[15]);
562   params[10] = params[16];
563   params[11] = params[17];
564   params[12] = params[18];
565   params[13] = params[19];
566   params[14] = ixheaacd_divideby2(params[20] + params[21]);
567   params[15] = ixheaacd_divideby2(params[22] + params[23]);
568   params[16] = ixheaacd_divideby2(params[24] + params[25]);
569   params[17] = ixheaacd_divideby2(params[26] + params[27]);
570   params[18] = ixheaacd_divideby2(
571       ixheaacd_divideby2(params[28] + params[29] + params[30] + params[31]));
572   params[19] = ixheaacd_divideby2(params[32] + params[33]);
573 }
574 
575 extern const WORD16 ixheaacd_num_bands[3];
576 
ixheaacd_read_ps_data(ia_ps_dec_struct * ptr_ps_dec,ia_bit_buf_struct * it_bit_buff,WORD16 num_bits_left,ia_ps_tables_struct * ps_tables_ptr)577 IA_ERRORCODE ixheaacd_read_ps_data(ia_ps_dec_struct *ptr_ps_dec, ia_bit_buf_struct *it_bit_buff,
578                                    WORD16 num_bits_left, ia_ps_tables_struct *ps_tables_ptr)
579 {
580   WORD b, e, temp;
581   const WORD16 num_env_tab[4] = {0, 1, 2, 4};
582   WORD cnt_bits;
583   ia_huffman_data_type huffman_table, huffman_df_table, huffman_dt_table;
584   FLAG enable_ps_header;
585 
586   if (!ptr_ps_dec) {
587     return 0;
588   }
589 
590   cnt_bits = it_bit_buff->cnt_bits;
591 
592   enable_ps_header = ixheaacd_read_bits_buf(it_bit_buff, 1);
593 
594   if (enable_ps_header) {
595     ptr_ps_dec->enable_iid = ixheaacd_read_bits_buf(it_bit_buff, 1);
596     if (ptr_ps_dec->enable_iid) {
597       ptr_ps_dec->iid_mode = ixheaacd_read_bits_buf(it_bit_buff, 3);
598     }
599 
600     if (ptr_ps_dec->iid_mode > 2) {
601       ptr_ps_dec->iid_quant = 1;
602       ptr_ps_dec->iid_mode -= 3;
603     } else {
604       ptr_ps_dec->iid_quant = 0;
605     }
606 
607     ptr_ps_dec->enable_icc = ixheaacd_read_bits_buf(it_bit_buff, 1);
608     if (ptr_ps_dec->enable_icc) {
609       ptr_ps_dec->icc_mode = ixheaacd_read_bits_buf(it_bit_buff, 3);
610     }
611 
612     ptr_ps_dec->enable_ext = ixheaacd_read_bits_buf(it_bit_buff, 1);
613 
614     if (ptr_ps_dec->icc_mode > 2) {
615       ptr_ps_dec->icc_mode -= 3;
616       ptr_ps_dec->use_pca_rot_flg = 1;
617     } else {
618       ptr_ps_dec->use_pca_rot_flg = 0;
619     }
620     ptr_ps_dec->freq_res_ipd = ptr_ps_dec->iid_mode;
621     if (ptr_ps_dec->freq_res_ipd > 2) {
622       return IA_FATAL_ERROR;
623     }
624   }
625 
626   ptr_ps_dec->use_34_st_bands = 0;
627   ptr_ps_dec->use_pca_rot_flg = 0;
628 
629   if ((ptr_ps_dec->enable_iid && ptr_ps_dec->iid_mode > 2) ||
630       (ptr_ps_dec->enable_icc && ptr_ps_dec->icc_mode > 2)) {
631     ptr_ps_dec->ps_data_present = 0;
632 
633     num_bits_left -= (cnt_bits - it_bit_buff->cnt_bits);
634 
635     while (num_bits_left > 8) {
636       ixheaacd_read_bits_buf(it_bit_buff, 8);
637       num_bits_left -= 8;
638     }
639     if (num_bits_left >= 0) {
640       ixheaacd_read_bits_buf(it_bit_buff, num_bits_left);
641     }
642 
643     return (cnt_bits - it_bit_buff->cnt_bits);
644   }
645 
646   ptr_ps_dec->frame_class = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
647 
648   temp = ixheaacd_read_bits_buf(it_bit_buff, 2);
649 
650   if (ptr_ps_dec->frame_class == 0) {
651     ptr_ps_dec->num_env = num_env_tab[temp];
652   } else {
653     ptr_ps_dec->num_env = (((1 + temp) << 8) >> 8);
654 
655     for (e = 1; e < ptr_ps_dec->num_env + 1; e++) {
656       ptr_ps_dec->border_position[e] =
657           (((ixheaacd_read_bits_buf(it_bit_buff, 5) + 1) << 8) >> 8);
658     }
659   }
660 
661   if (ptr_ps_dec->enable_iid) {
662     if (ptr_ps_dec->iid_quant) {
663       huffman_df_table = (ia_huffman_data_type)&ps_tables_ptr->huff_iid_df_fine;
664       huffman_dt_table = (ia_huffman_data_type)&ps_tables_ptr->huff_iid_dt_fine;
665     } else {
666       huffman_df_table = (ia_huffman_data_type)&ps_tables_ptr->huff_iid_df;
667       huffman_dt_table = (ia_huffman_data_type)&ps_tables_ptr->huff_iid_dt;
668     }
669 
670     for (e = 0; e < ptr_ps_dec->num_env; e++) {
671       ptr_ps_dec->iid_dt[e] = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
672 
673       if (ptr_ps_dec->iid_dt[e]) {
674         huffman_table = huffman_dt_table;
675       } else {
676         huffman_table = huffman_df_table;
677       }
678 
679       for (b = 0; b < ixheaacd_num_bands[ptr_ps_dec->iid_mode]; b++) {
680         ptr_ps_dec->iid_par_table[e][b] =
681             ixheaacd_ssc_huff_dec(huffman_table, it_bit_buff);
682       }
683     }
684   }
685 
686   if (ptr_ps_dec->enable_icc) {
687     huffman_df_table = (ia_huffman_data_type)&ps_tables_ptr->huff_icc_df;
688     huffman_dt_table = (ia_huffman_data_type)&ps_tables_ptr->huff_icc_dt;
689 
690     for (e = 0; e < ptr_ps_dec->num_env; e++) {
691       ptr_ps_dec->icc_dt[e] = ixheaacd_read_bits_buf(it_bit_buff, 1);
692 
693       if (ptr_ps_dec->icc_dt[e]) {
694         huffman_table = huffman_dt_table;
695       } else {
696         huffman_table = huffman_df_table;
697       }
698 
699       for (b = 0; b < ixheaacd_num_bands[ptr_ps_dec->icc_mode]; b++) {
700         ptr_ps_dec->icc_par_table[e][b] = ixheaacd_ssc_huff_dec(huffman_table, it_bit_buff);
701       }
702     }
703   }
704 
705   if (ptr_ps_dec->enable_ext) {
706     WORD32 cnt;
707     if (it_bit_buff->cnt_bits < 4)
708       cnt = ixheaacd_read_bits_buf(it_bit_buff, it_bit_buff->cnt_bits);
709     else
710       cnt = ixheaacd_read_bits_buf(it_bit_buff, 4);
711 
712     if (cnt == 15) {
713       cnt += ixheaacd_read_bits_buf(it_bit_buff, 8);
714     }
715     while (cnt--) {
716       ixheaacd_read_bits_buf(it_bit_buff, 8);
717     }
718   }
719 
720   ptr_ps_dec->ps_data_present = 1;
721 
722   return (cnt_bits - it_bit_buff->cnt_bits);
723 }
724 
ixheaacd_invfilt_level_emphasis(ia_sbr_hf_generator_struct * ptr_hf_gen_str,WORD32 num_if_bands,WORD32 * inv_filt_mode,WORD32 * inv_filt_mode_prev,WORD32 * bw_array)725 VOID ixheaacd_invfilt_level_emphasis(ia_sbr_hf_generator_struct *ptr_hf_gen_str,
726                                      WORD32 num_if_bands, WORD32 *inv_filt_mode,
727                                      WORD32 *inv_filt_mode_prev,
728                                      WORD32 *bw_array) {
729   WORD32 i;
730   WORD32 accu;
731   WORD16 w1, w2;
732 
733   for (i = 0; i < num_if_bands; i++) {
734     bw_array[i] =
735         ixheaacd_inew_bw_table[inv_filt_mode_prev[i]][inv_filt_mode[i]];
736 
737     if (bw_array[i] < ptr_hf_gen_str->bw_array_prev[i]) {
738       w1 = 0x6000;
739       w2 = 0x2000;
740     } else {
741       w1 = 0x7400;
742       w2 = 0x0c00;
743     }
744     accu = ixheaacd_add32(
745         ixheaacd_mult32x16in32_shl(bw_array[i], w1),
746         ixheaacd_mult32x16in32_shl(ptr_hf_gen_str->bw_array_prev[i], w2));
747 
748     if (accu < 0x02000000) {
749       accu = 0;
750     }
751 
752     if (accu >= 0x7f800000) {
753       accu = 0x7f800000;
754     }
755     bw_array[i] = accu;
756   }
757 }
758 
759 typedef struct {
760   FLOAT32 phi_0_1_real;
761   FLOAT32 phi_0_1_imag;
762   FLOAT32 phi_0_2_real;
763   FLOAT32 phi_0_2_imag;
764   FLOAT32 phi_1_1;
765   FLOAT32 phi_1_2_real;
766   FLOAT32 phi_1_2_imag;
767   FLOAT32 phi_2_2;
768   FLOAT32 det;
769 } ia_auto_corr_ele_struct;
770 
ixheaacd_esbr_calc_co_variance(ia_auto_corr_ele_struct * pstr_auto_corr,FLOAT32 vec_x_real[][64],FLOAT32 vec_x_imag[][64],WORD32 bd,WORD32 len)771 static VOID ixheaacd_esbr_calc_co_variance(
772     ia_auto_corr_ele_struct *pstr_auto_corr, FLOAT32 vec_x_real[][64],
773     FLOAT32 vec_x_imag[][64], WORD32 bd, WORD32 len) {
774   WORD32 j, jminus1, jminus2;
775 
776   memset(pstr_auto_corr, 0, sizeof(ia_auto_corr_ele_struct));
777 
778   for (j = 0; j < len; j++) {
779     jminus1 = j - 1;
780     jminus2 = jminus1 - 1;
781 
782     pstr_auto_corr->phi_0_1_real +=
783         vec_x_real[j][bd] * vec_x_real[jminus1][bd] +
784         vec_x_imag[j][bd] * vec_x_imag[jminus1][bd];
785 
786     pstr_auto_corr->phi_0_1_imag +=
787         vec_x_imag[j][bd] * vec_x_real[jminus1][bd] -
788         vec_x_real[j][bd] * vec_x_imag[jminus1][bd];
789 
790     pstr_auto_corr->phi_0_2_real +=
791         vec_x_real[j][bd] * vec_x_real[jminus2][bd] +
792         vec_x_imag[j][bd] * vec_x_imag[jminus2][bd];
793 
794     pstr_auto_corr->phi_0_2_imag +=
795         vec_x_imag[j][bd] * vec_x_real[jminus2][bd] -
796         vec_x_real[j][bd] * vec_x_imag[jminus2][bd];
797 
798     pstr_auto_corr->phi_1_1 +=
799         vec_x_real[jminus1][bd] * vec_x_real[jminus1][bd] +
800         vec_x_imag[jminus1][bd] * vec_x_imag[jminus1][bd];
801 
802     pstr_auto_corr->phi_1_2_real +=
803         vec_x_real[jminus1][bd] * vec_x_real[jminus2][bd] +
804         vec_x_imag[jminus1][bd] * vec_x_imag[jminus2][bd];
805 
806     pstr_auto_corr->phi_1_2_imag +=
807         vec_x_imag[jminus1][bd] * vec_x_real[jminus2][bd] -
808         vec_x_real[jminus1][bd] * vec_x_imag[jminus2][bd];
809 
810     pstr_auto_corr->phi_2_2 +=
811         vec_x_real[jminus2][bd] * vec_x_real[jminus2][bd] +
812         vec_x_imag[jminus2][bd] * vec_x_imag[jminus2][bd];
813   }
814 
815   pstr_auto_corr->det =
816       pstr_auto_corr->phi_1_1 * pstr_auto_corr->phi_2_2 -
817       (pstr_auto_corr->phi_1_2_real * pstr_auto_corr->phi_1_2_real +
818        pstr_auto_corr->phi_1_2_imag * pstr_auto_corr->phi_1_2_imag) *
819           SBR_HF_RELAXATION_PARAM;
820 }
821 
ixheaacd_esbr_chirp_fac_calc(WORD32 * inv_filt_mode,WORD32 * inv_filt_mode_prev,WORD32 num_if_bands,FLOAT32 * bw_array,FLOAT32 * bw_array_prev)822 static void ixheaacd_esbr_chirp_fac_calc(WORD32 *inv_filt_mode,
823                                          WORD32 *inv_filt_mode_prev,
824                                          WORD32 num_if_bands, FLOAT32 *bw_array,
825                                          FLOAT32 *bw_array_prev) {
826   WORD32 i;
827 
828   for (i = 0; i < num_if_bands; i++) {
829     bw_array[i] =
830         ixheaacd_new_bw_table[inv_filt_mode_prev[i]][inv_filt_mode[i]];
831 
832     if (bw_array[i] < bw_array_prev[i])
833       bw_array[i] = 0.75000f * bw_array[i] + 0.25000f * bw_array_prev[i];
834     else
835       bw_array[i] = 0.90625f * bw_array[i] + 0.09375f * bw_array_prev[i];
836 
837     if (bw_array[i] < 0.015625) bw_array[i] = 0;
838   }
839 }
840 
ixheaacd_gausssolve(WORD32 n,FLOAT32 a[][MAXDEG+1],FLOAT32 b[],FLOAT32 y[])841 static void ixheaacd_gausssolve(WORD32 n, FLOAT32 a[][MAXDEG + 1], FLOAT32 b[],
842                                 FLOAT32 y[]) {
843   WORD32 i, j, k, imax;
844   FLOAT32 v;
845 
846   for (i = 0; i < n; i++) {
847     imax = i;
848     for (k = i + 1; k < n; k++) {
849       if (fabs(a[k][i]) > fabs(a[imax][i])) {
850         imax = k;
851       }
852     }
853     if (imax != i) {
854       v = b[imax];
855       b[imax] = b[i];
856       b[i] = v;
857       for (j = i; j < n; j++) {
858         v = a[imax][j];
859         a[imax][j] = a[i][j];
860         a[i][j] = v;
861       }
862     }
863 
864     v = a[i][i];
865 
866     b[i] /= v;
867     for (j = i; j < n; j++) {
868       a[i][j] /= v;
869     }
870 
871     for (k = i + 1; k < n; k++) {
872       v = a[k][i];
873       b[k] -= v * b[i];
874       for (j = i + 1; j < n; j++) {
875         a[k][j] -= v * a[i][j];
876       }
877     }
878   }
879 
880   for (i = n - 1; i >= 0; i--) {
881     y[i] = b[i];
882     for (j = i + 1; j < n; j++) {
883       y[i] -= a[i][j] * y[j];
884     }
885   }
886 }
887 
ixheaacd_polyfit(WORD32 n,FLOAT32 y[],FLOAT32 p[])888 void ixheaacd_polyfit(WORD32 n, FLOAT32 y[], FLOAT32 p[]) {
889   WORD32 i, j, k;
890   FLOAT32 a[MAXDEG + 1][MAXDEG + 1];
891   FLOAT32 b[MAXDEG + 1];
892   FLOAT32 v[2 * MAXDEG + 1];
893 
894   for (i = 0; i <= MAXDEG; i++) {
895     b[i] = 0.0f;
896     for (j = 0; j <= MAXDEG; j++) {
897       a[i][j] = 0.0f;
898     }
899   }
900 
901   for (k = 0; k < n; k++) {
902     v[0] = 1.0;
903     for (i = 1; i <= 2 * MAXDEG; i++) {
904       v[i] = k * v[i - 1];
905     }
906 
907     for (i = 0; i <= MAXDEG; i++) {
908       b[i] += v[MAXDEG - i] * y[k];
909       for (j = 0; j <= MAXDEG; j++) {
910         a[i][j] += v[2 * MAXDEG - i - j];
911       }
912     }
913   }
914 
915   ixheaacd_gausssolve(MAXDEG + 1, a, b, p);
916 }
917 
ixheaacd_pre_processing(FLOAT32 ptr_src_buf_real[][64],FLOAT32 ptr_src_buf_imag[][64],FLOAT32 gain_vector[],WORD32 num_bands,WORD32 start_sample,WORD32 end_sample)918 VOID ixheaacd_pre_processing(FLOAT32 ptr_src_buf_real[][64],
919                              FLOAT32 ptr_src_buf_imag[][64],
920                              FLOAT32 gain_vector[], WORD32 num_bands,
921                              WORD32 start_sample, WORD32 end_sample) {
922   WORD32 k, i;
923   FLOAT32 poly_coeff[4];
924   FLOAT32 mean_enrg = 0;
925   FLOAT32 low_env_slope[64];
926   FLOAT32 low_env[64];
927   FLOAT32 a0;
928   FLOAT32 a1;
929   FLOAT32 a2;
930   FLOAT32 a3;
931 
932   for (k = 0; k < num_bands; k++) {
933     FLOAT32 temp = 0;
934     for (i = start_sample; i < end_sample; i++) {
935       temp += ptr_src_buf_real[i][k] * ptr_src_buf_real[i][k] +
936               ptr_src_buf_imag[i][k] * ptr_src_buf_imag[i][k];
937     }
938     temp /= (end_sample - start_sample);
939     low_env[k] = (FLOAT32)(10 * log10(temp + 1));
940     mean_enrg = mean_enrg + low_env[k];
941   }
942   mean_enrg /= num_bands;
943 
944   ixheaacd_polyfit(num_bands, low_env, poly_coeff);
945 
946   a0 = poly_coeff[0];
947   a1 = poly_coeff[1];
948   a2 = poly_coeff[2];
949   a3 = poly_coeff[3];
950   for (k = 0; k < num_bands; k++) {
951     FLOAT32 x_low_l = (FLOAT32)k;
952     FLOAT32 low_env_slope_l = a3;
953     low_env_slope_l = low_env_slope_l + a2 * x_low_l;
954 
955     x_low_l = x_low_l * x_low_l;
956     low_env_slope_l = low_env_slope_l + a1 * x_low_l;
957 
958     x_low_l = x_low_l * (FLOAT32)k;
959     low_env_slope_l = low_env_slope_l + a0 * x_low_l;
960 
961     low_env_slope[k] = low_env_slope_l;
962   }
963 
964   for (i = 0; i < num_bands; i++) {
965     gain_vector[i] = (FLOAT32)pow(10, (mean_enrg - low_env_slope[i]) / 20.0f);
966   }
967 }
968 
ixheaacd_generate_hf(FLOAT32 ptr_src_buf_real[][64],FLOAT32 ptr_src_buf_imag[][64],FLOAT32 ptr_ph_vocod_buf_real[][64],FLOAT32 ptr_ph_vocod_buf_imag[][64],FLOAT32 ptr_dst_buf_real[][64],FLOAT32 ptr_dst_buf_imag[][64],ia_sbr_frame_info_data_struct * ptr_frame_data,ia_sbr_header_data_struct * ptr_header_data,WORD32 ldmps_present,WORD32 time_slots,WORD32 ec_flag)969 WORD32 ixheaacd_generate_hf(FLOAT32 ptr_src_buf_real[][64], FLOAT32 ptr_src_buf_imag[][64],
970                             FLOAT32 ptr_ph_vocod_buf_real[][64],
971                             FLOAT32 ptr_ph_vocod_buf_imag[][64], FLOAT32 ptr_dst_buf_real[][64],
972                             FLOAT32 ptr_dst_buf_imag[][64],
973                             ia_sbr_frame_info_data_struct *ptr_frame_data,
974                             ia_sbr_header_data_struct *ptr_header_data, WORD32 ldmps_present,
975                             WORD32 time_slots, WORD32 ec_flag) {
976   WORD32 bw_index, i, k, k2, patch = 0;
977   WORD32 co_var_len;
978   WORD32 start_sample, end_sample, goal_sb;
979   WORD32 sb, source_start_band, patch_stride, num_bands_in_patch;
980   WORD32 hbe_flag = ptr_header_data->hbe_flag;
981   FLOAT32 a0r, a0i, a1r, a1i;
982   FLOAT32 bw_array[MAX_NUM_PATCHES] = {0};
983 
984   ia_auto_corr_ele_struct str_auto_corr;
985 
986   WORD16 *ptr_invf_band_tbl =
987       &ptr_header_data->pstr_freq_band_data
988            ->freq_band_tbl_noise[1];  // offest 1 used as base address of
989                                       // ptr_invf_band_tbl
990   WORD32 num_if_bands = ptr_header_data->pstr_freq_band_data->num_nf_bands;
991   WORD32 sub_band_start = ptr_header_data->pstr_freq_band_data->sub_band_start;
992   WORD16 *f_master_tbl = ptr_header_data->pstr_freq_band_data->f_master_tbl;
993   WORD32 num_mf_bands = ptr_header_data->pstr_freq_band_data->num_mf_bands;
994   WORD32 *inv_filt_mode = ptr_frame_data->sbr_invf_mode;
995   WORD32 *inv_filt_mode_prev = ptr_frame_data->sbr_invf_mode_prev;
996   WORD32 sbr_patching_mode = ptr_frame_data->sbr_patching_mode;
997   ia_frame_info_struct *p_frame_info = &ptr_frame_data->str_frame_info_details;
998   WORD32 pre_proc_flag = ptr_header_data->pre_proc_flag;
999   WORD32 is_usf_4 = ptr_header_data->is_usf_4;
1000   WORD32 fs = ptr_header_data->out_sampling_freq;
1001   WORD32 cov_count;
1002   WORD32 lsb = f_master_tbl[0];
1003   WORD32 usb = f_master_tbl[num_mf_bands];
1004   WORD32 xover_offset = sub_band_start - f_master_tbl[0];
1005 
1006   FLOAT32 bw = 0.0f;
1007   FLOAT32 fac = 0.0f;
1008 
1009   FLOAT32 gain;
1010   FLOAT32 gain_vector[64];
1011 
1012   WORD32 slope_length = 0;
1013   WORD32 first_slot_offset = p_frame_info->border_vec[0];
1014   WORD32 end_slot_offs = 0;
1015 
1016   FLOAT32 *bw_array_prev = ptr_frame_data->bw_array_prev;
1017 
1018   end_slot_offs = p_frame_info->border_vec[p_frame_info->num_env] - 16;
1019 
1020   if (ldmps_present == 1)
1021     end_slot_offs =
1022         p_frame_info->border_vec[p_frame_info->num_env] - time_slots;
1023 
1024   if (is_usf_4) {
1025     start_sample = first_slot_offset * 4;
1026     end_sample = 64 + end_slot_offs * 4;
1027     co_var_len = 76;
1028   } else {
1029     start_sample = first_slot_offset * 2;
1030     end_sample = 32 + end_slot_offs * 2;
1031     co_var_len = 38;
1032   }
1033 
1034   if (ldmps_present == 1) {
1035     start_sample = 0;
1036     end_sample = time_slots;
1037     co_var_len = time_slots;
1038   }
1039 
1040   if (pre_proc_flag) {
1041     ixheaacd_pre_processing(ptr_src_buf_real, ptr_src_buf_imag, gain_vector,
1042                             f_master_tbl[0], start_sample, end_sample);
1043   }
1044 
1045   ixheaacd_esbr_chirp_fac_calc(inv_filt_mode, inv_filt_mode_prev, num_if_bands,
1046                                bw_array, bw_array_prev);
1047 
1048   for (i = start_sample; i < end_sample; i++) {
1049     memset(ptr_dst_buf_real[i] + usb, 0, (64 - usb) * sizeof(FLOAT32));
1050     memset(ptr_dst_buf_imag[i] + usb, 0, (64 - usb) * sizeof(FLOAT32));
1051   }
1052 
1053   if (sbr_patching_mode || !hbe_flag) {
1054     WORD32 flag_break = 0;
1055     FLOAT32 alpha_real[64][2] = {{0}}, alpha_imag[64][2] = {{0}};
1056     if (ptr_frame_data->mps_sbr_flag) {
1057       cov_count = (f_master_tbl[0] < ptr_frame_data->cov_count)
1058                       ? f_master_tbl[0]
1059                       : ptr_frame_data->cov_count;
1060     } else {
1061       cov_count = f_master_tbl[0];
1062     }
1063 
1064     for (k = 1; k < cov_count; k++) {
1065       ixheaacd_esbr_calc_co_variance(&str_auto_corr, &ptr_src_buf_real[0],
1066                                      &ptr_src_buf_imag[0], k, co_var_len);
1067       if (str_auto_corr.det == 0.0f) {
1068         alpha_real[k][1] = alpha_imag[k][1] = 0;
1069       } else {
1070         fac = 1.0f / str_auto_corr.det;
1071         alpha_real[k][1] =
1072             (str_auto_corr.phi_0_1_real * str_auto_corr.phi_1_2_real -
1073              str_auto_corr.phi_0_1_imag * str_auto_corr.phi_1_2_imag -
1074              str_auto_corr.phi_0_2_real * str_auto_corr.phi_1_1) *
1075             fac;
1076         alpha_imag[k][1] =
1077             (str_auto_corr.phi_0_1_imag * str_auto_corr.phi_1_2_real +
1078              str_auto_corr.phi_0_1_real * str_auto_corr.phi_1_2_imag -
1079              str_auto_corr.phi_0_2_imag * str_auto_corr.phi_1_1) *
1080             fac;
1081       }
1082 
1083       if (str_auto_corr.phi_1_1 == 0) {
1084         alpha_real[k][0] = alpha_imag[k][0] = 0;
1085       } else {
1086         fac = 1.0f / str_auto_corr.phi_1_1;
1087         alpha_real[k][0] = -(str_auto_corr.phi_0_1_real +
1088                              alpha_real[k][1] * str_auto_corr.phi_1_2_real +
1089                              alpha_imag[k][1] * str_auto_corr.phi_1_2_imag) *
1090                            fac;
1091         alpha_imag[k][0] = -(str_auto_corr.phi_0_1_imag +
1092                              alpha_imag[k][1] * str_auto_corr.phi_1_2_real -
1093                              alpha_real[k][1] * str_auto_corr.phi_1_2_imag) *
1094                            fac;
1095       }
1096 
1097       if ((alpha_real[k][0] * alpha_real[k][0] +
1098                alpha_imag[k][0] * alpha_imag[k][0] >=
1099            16.0f) ||
1100           (alpha_real[k][1] * alpha_real[k][1] +
1101                alpha_imag[k][1] * alpha_imag[k][1] >=
1102            16.0f)) {
1103         alpha_real[k][0] = 0.0f;
1104         alpha_imag[k][0] = 0.0f;
1105         alpha_real[k][1] = 0.0f;
1106         alpha_imag[k][1] = 0.0f;
1107       }
1108     }
1109 
1110     goal_sb = (WORD32)(2.048e6f / fs + 0.5f);
1111     {
1112       WORD32 index;
1113       if (goal_sb < f_master_tbl[num_mf_bands]) {
1114         for (index = 0; (f_master_tbl[index] < goal_sb); index++)
1115           ;
1116         goal_sb = f_master_tbl[index];
1117       } else {
1118         goal_sb = f_master_tbl[num_mf_bands];
1119       }
1120     }
1121 
1122     source_start_band = xover_offset + 1;
1123     sb = lsb + xover_offset;
1124 
1125     patch = 0;
1126     while (sb < usb) {
1127       if (MAX_NUM_PATCHES <= patch) {
1128         if (ec_flag) {
1129           break;
1130         } else {
1131           return -1;
1132         }
1133       }
1134       ptr_frame_data->patch_param.start_subband[patch] = sb;
1135       num_bands_in_patch = goal_sb - sb;
1136 
1137       if (num_bands_in_patch >= lsb - source_start_band) {
1138         patch_stride = sb - source_start_band;
1139         patch_stride = patch_stride & ~1;
1140         num_bands_in_patch = lsb - (sb - patch_stride);
1141         num_bands_in_patch =
1142             ixheaacd_find_closest_entry(sb + num_bands_in_patch, f_master_tbl,
1143                                         (WORD16)(num_mf_bands), 0) -
1144             (WORD32)(sb);
1145       }
1146 
1147       patch_stride = num_bands_in_patch + sb - lsb;
1148       patch_stride = (patch_stride + 1) & ~1;
1149 
1150       source_start_band = 1;
1151 
1152       if (goal_sb - (sb + num_bands_in_patch) < 3) {
1153         goal_sb = usb;
1154       }
1155 
1156       if ((num_bands_in_patch < 3) && (patch > 0) &&
1157           (sb + num_bands_in_patch == usb)) {
1158         for (i = start_sample + slope_length; i < end_sample + slope_length;
1159              i++) {
1160           for (k2 = sb; k2 < sb + num_bands_in_patch; k2++) {
1161             ptr_dst_buf_real[i][k2] = 0.0f;
1162             ptr_dst_buf_imag[i][k2] = 0.0f;
1163           }
1164         }
1165 
1166         break;
1167       }
1168 
1169       if (num_bands_in_patch < 0 && flag_break == 1) {
1170         break;
1171       }
1172 
1173       if (num_bands_in_patch < 0) {
1174         flag_break = 1;
1175         continue;
1176       } else {
1177         flag_break = 0;
1178       }
1179 
1180       for (k2 = sb; k2 < sb + num_bands_in_patch; k2++) {
1181         k = k2 - patch_stride;
1182         bw_index = 0;
1183         while (k2 >= ptr_invf_band_tbl[bw_index]) {
1184           bw_index++;
1185           if (bw_index >= MAX_NOISE_COEFFS) {
1186             if (ec_flag) {
1187               bw_index = MAX_NOISE_COEFFS - 1;
1188               break;
1189             } else
1190               return -1;
1191           }
1192         }
1193 
1194         if (bw_index >= MAX_NUM_PATCHES) {
1195           if (ec_flag)
1196             bw_index = MAX_NUM_PATCHES - 1;
1197           else
1198             return -1;
1199         }
1200         bw = bw_array[bw_index];
1201 
1202         a0r = bw * alpha_real[k][0];
1203         a0i = bw * alpha_imag[k][0];
1204         bw *= bw;
1205         a1r = bw * alpha_real[k][1];
1206         a1i = bw * alpha_imag[k][1];
1207 
1208         if (pre_proc_flag) {
1209           gain = gain_vector[k];
1210         } else {
1211           gain = 1.0f;
1212         }
1213 
1214         for (i = start_sample + slope_length; i < end_sample + slope_length;
1215              i++) {
1216           ptr_dst_buf_real[i][k2] = ptr_src_buf_real[i][k] * gain;
1217 
1218           ptr_dst_buf_imag[i][k2] = ptr_src_buf_imag[i][k] * gain;
1219 
1220           if (bw > 0.0f) {
1221             ptr_dst_buf_real[i][k2] += (a0r * ptr_src_buf_real[i - 1][k] -
1222                                         a0i * ptr_src_buf_imag[i - 1][k] +
1223                                         a1r * ptr_src_buf_real[i - 2][k] -
1224                                         a1i * ptr_src_buf_imag[i - 2][k]) *
1225                                        gain;
1226             ptr_dst_buf_imag[i][k2] += (a0i * ptr_src_buf_real[i - 1][k] +
1227                                         a0r * ptr_src_buf_imag[i - 1][k] +
1228                                         a1i * ptr_src_buf_real[i - 2][k] +
1229                                         a1r * ptr_src_buf_imag[i - 2][k]) *
1230                                        gain;
1231           }
1232         }
1233       }
1234       sb += num_bands_in_patch;
1235       patch++;
1236     }
1237   }
1238 
1239   if (NULL != ptr_ph_vocod_buf_real && NULL != ptr_ph_vocod_buf_imag) {
1240     if (hbe_flag && !sbr_patching_mode) {
1241       FLOAT32 alpha_real[2], alpha_imag[2];
1242 
1243       bw_index = 0, patch = 1;
1244 
1245       for (k2 = sub_band_start; k2 < f_master_tbl[num_mf_bands]; k2++) {
1246         ixheaacd_esbr_calc_co_variance(&str_auto_corr, &ptr_ph_vocod_buf_real[0],
1247                                        &ptr_ph_vocod_buf_imag[0], k2, co_var_len);
1248 
1249         if (str_auto_corr.det == 0.0f) {
1250           alpha_real[1] = alpha_imag[1] = 0;
1251         } else {
1252           fac = 1.0f / str_auto_corr.det;
1253           alpha_real[1] = (str_auto_corr.phi_0_1_real * str_auto_corr.phi_1_2_real -
1254                            str_auto_corr.phi_0_1_imag * str_auto_corr.phi_1_2_imag -
1255                            str_auto_corr.phi_0_2_real * str_auto_corr.phi_1_1) *
1256                           fac;
1257           alpha_imag[1] = (str_auto_corr.phi_0_1_imag * str_auto_corr.phi_1_2_real +
1258                            str_auto_corr.phi_0_1_real * str_auto_corr.phi_1_2_imag -
1259                            str_auto_corr.phi_0_2_imag * str_auto_corr.phi_1_1) *
1260                           fac;
1261         }
1262 
1263         if (str_auto_corr.phi_1_1 == 0) {
1264           alpha_real[0] = alpha_imag[0] = 0;
1265         } else {
1266           fac = 1.0f / str_auto_corr.phi_1_1;
1267           alpha_real[0] =
1268               -(str_auto_corr.phi_0_1_real + alpha_real[1] * str_auto_corr.phi_1_2_real +
1269                 alpha_imag[1] * str_auto_corr.phi_1_2_imag) *
1270               fac;
1271           alpha_imag[0] =
1272               -(str_auto_corr.phi_0_1_imag + alpha_imag[1] * str_auto_corr.phi_1_2_real -
1273                 alpha_real[1] * str_auto_corr.phi_1_2_imag) *
1274               fac;
1275         }
1276 
1277         if (alpha_real[0] * alpha_real[0] + alpha_imag[0] * alpha_imag[0] >= 16.0f ||
1278             alpha_real[1] * alpha_real[1] + alpha_imag[1] * alpha_imag[1] >= 16.0f) {
1279           alpha_real[0] = 0.0f;
1280           alpha_imag[0] = 0.0f;
1281           alpha_real[1] = 0.0f;
1282           alpha_imag[1] = 0.0f;
1283         }
1284 
1285         while (k2 >= ptr_invf_band_tbl[bw_index]) {
1286           bw_index++;
1287           if (bw_index >= MAX_NOISE_COEFFS) {
1288             if (ec_flag) {
1289               bw_index = MAX_NOISE_COEFFS - 1;
1290               break;
1291             } else
1292               return -1;
1293           }
1294         }
1295 
1296         if (bw_index >= MAX_NUM_PATCHES) {
1297           if (ec_flag)
1298             bw_index = MAX_NUM_PATCHES - 1;
1299           else
1300             return -1;
1301         }
1302         bw = bw_array[bw_index];
1303 
1304         a0r = bw * alpha_real[0];
1305         a0i = bw * alpha_imag[0];
1306         bw *= bw;
1307         a1r = bw * alpha_real[1];
1308         a1i = bw * alpha_imag[1];
1309 
1310         if (bw > 0.0f) {
1311           for (i = start_sample; i < end_sample; i++) {
1312             FLOAT32 real1, imag1, real2, imag2, realTarget, imag_target;
1313 
1314             realTarget = ptr_ph_vocod_buf_real[i][k2];
1315             imag_target = ptr_ph_vocod_buf_imag[i][k2];
1316             real1 = ptr_ph_vocod_buf_real[i - 1][k2];
1317             imag1 = ptr_ph_vocod_buf_imag[i - 1][k2];
1318             real2 = ptr_ph_vocod_buf_real[i - 2][k2];
1319             imag2 = ptr_ph_vocod_buf_imag[i - 2][k2];
1320             realTarget += ((a0r * real1 - a0i * imag1) + (a1r * real2 - a1i * imag2));
1321             imag_target += ((a0i * real1 + a0r * imag1) + (a1i * real2 + a1r * imag2));
1322 
1323             ptr_dst_buf_real[i][k2] = realTarget;
1324             ptr_dst_buf_imag[i][k2] = imag_target;
1325           }
1326         } else {
1327           for (i = start_sample; i < end_sample; i++) {
1328             ptr_dst_buf_real[i][k2] = ptr_ph_vocod_buf_real[i][k2];
1329             ptr_dst_buf_imag[i][k2] = ptr_ph_vocod_buf_imag[i][k2];
1330           }
1331         }
1332       }
1333     }
1334   }
1335   if ((MAX_NUM_PATCHES + 1) <= patch) {
1336     if (ec_flag) {
1337       patch = MAX_NUM_PATCHES;
1338     } else {
1339       return -1;
1340     }
1341   }
1342   ptr_frame_data->patch_param.num_patches = patch;
1343   for (i = 0; i < num_if_bands; i++) {
1344     bw_array_prev[i] = bw_array[i];
1345   }
1346   return 0;
1347 }
1348