• 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 #include "ixheaacd_error_standards.h"
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_basic_op.h"
31 #include "ixheaacd_intrinsics.h"
32 #include "ixheaacd_bitbuffer.h"
33 #include "ixheaacd_sbrdecsettings.h"
34 #include "ixheaacd_sbr_scale.h"
35 #include "ixheaacd_lpp_tran.h"
36 #include "ixheaacd_env_extr_part.h"
37 #include "ixheaacd_sbr_rom.h"
38 #include "ixheaacd_hybrid.h"
39 #include "ixheaacd_ps_dec.h"
40 #include "ixheaacd_env_extr.h"
41 #include "ixheaacd_common_rom.h"
42 #include "ixheaacd_env_dec.h"
43 #include "ixheaacd_sbr_const.h"
44 #include "ixheaacd_basic_funcs.h"
45 
46 #define add16_m(a, b) ((a) + (b))
47 #define sub16_m(a, b) ((a) - (b))
48 
49 #define mult16x16_16(a, b) ixheaacd_mult16((a), (b))
50 
ixheaacd_dequant_esbr_env_data(FLOAT32 * ptr_env_sf,WORD32 num_env_sf,WORD32 num_noise_fac,WORD32 amp_res,FLOAT32 * ptr_noise_floor)51 static VOID ixheaacd_dequant_esbr_env_data(FLOAT32 *ptr_env_sf,
52                                            WORD32 num_env_sf,
53                                            WORD32 num_noise_fac, WORD32 amp_res,
54                                            FLOAT32 *ptr_noise_floor) {
55   WORD32 i;
56   static const FLOAT32 array[2] = {0.5f, 1.0f};
57   FLOAT32 a_flt = array[amp_res];
58 
59   for (i = 0; i < num_env_sf; i++) {
60     ptr_env_sf[i] = (FLOAT32)(pow(2, ptr_env_sf[i] * a_flt) * 64);
61   }
62 
63   for (i = 0; i < num_noise_fac; i++) {
64     FLOAT32 temp = ptr_noise_floor[i];
65 
66     temp = NOISE_FLOOR_OFFSET - temp;
67     temp = (FLOAT32)pow(2.0f, temp);
68 
69     ptr_noise_floor[i] = temp;
70   }
71 }
72 
ixheaacd_dequant_pvc_env_data(WORD32 num_noise_fac,FLOAT32 * ptr_noise_floor)73 static VOID ixheaacd_dequant_pvc_env_data(WORD32 num_noise_fac,
74                                           FLOAT32 *ptr_noise_floor) {
75   WORD32 i;
76 
77   for (i = 0; i < num_noise_fac; i++) {
78     FLOAT32 temp = ptr_noise_floor[i];
79 
80     temp = NOISE_FLOOR_OFFSET - temp;
81     temp = (FLOAT32)pow(2.0f, temp);
82 
83     ptr_noise_floor[i] = temp;
84   }
85 }
86 
ixheaacd_map_res_energy(WORD16 curr_val,WORD16 * prev_data,WORD32 ixheaacd_drc_offset,WORD32 index,WORD32 res)87 VOID ixheaacd_map_res_energy(WORD16 curr_val, WORD16 *prev_data,
88                              WORD32 ixheaacd_drc_offset, WORD32 index,
89                              WORD32 res) {
90   if (res == LOW) {
91     if (ixheaacd_drc_offset >= 0) {
92       if (index < ixheaacd_drc_offset) {
93         prev_data[index] = curr_val;
94       } else {
95         WORD32 index_2;
96         index_2 = ((index + index) - ixheaacd_drc_offset);
97         prev_data[index_2] = curr_val;
98         prev_data[index_2 + 1] = curr_val;
99       }
100     } else {
101       ixheaacd_drc_offset = -(ixheaacd_drc_offset);
102 
103       if (index < ixheaacd_drc_offset) {
104         WORD32 index_3;
105         index_3 = ((index + index) + index);
106         prev_data[index_3] = curr_val;
107         prev_data[index_3 + 1] = curr_val;
108         prev_data[index_3 + 2] = curr_val;
109       } else {
110         WORD32 index_2;
111         index_2 = ((index + index) + ixheaacd_drc_offset);
112         prev_data[index_2] = curr_val;
113         prev_data[index_2 + 1] = curr_val;
114       }
115     }
116   } else {
117     prev_data[index] = curr_val;
118   }
119 }
120 
ixheaacd_process_del_cod_env_data(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)121 VOID ixheaacd_process_del_cod_env_data(
122     ia_sbr_header_data_struct *ptr_header_data,
123     ia_sbr_frame_info_data_struct *ptr_sbr_data,
124     ia_sbr_prev_frame_data_struct *ptr_prev_data) {
125   WORD32 i, dtdf_dir, num_sf_bands, band, freq_res;
126   WORD16 temp_val;
127   WORD16 *ptr_prev_env_sf = ptr_prev_data->sfb_nrg_prev;
128   WORD16 *ptr_env_sf = ptr_sbr_data->int_env_sf_arr;
129 
130   FLOAT32 *ptr_env_sf_float = ptr_sbr_data->flt_env_sf_arr;
131 
132   WORD32 ixheaacd_drc_offset;
133   band = 0;
134 
135   ixheaacd_drc_offset =
136       ((ptr_header_data->pstr_freq_band_data->num_sf_bands[LOW] << 1) -
137        ptr_header_data->pstr_freq_band_data->num_sf_bands[HIGH]);
138 
139   for (i = 0; i < ptr_sbr_data->str_frame_info_details.num_env; i++) {
140     dtdf_dir = ptr_sbr_data->del_cod_dir_arr[i];
141     freq_res = ptr_sbr_data->str_frame_info_details.freq_res[i];
142 
143     num_sf_bands = ptr_header_data->pstr_freq_band_data->num_sf_bands[freq_res];
144 
145     if (dtdf_dir == DTDF_DIR_FREQ) {
146       ixheaacd_map_res_energy(*ptr_env_sf, ptr_prev_env_sf, ixheaacd_drc_offset,
147                               0, freq_res);
148       ptr_env_sf++;
149 
150       ptr_env_sf_float++;
151 
152       for (band = 1; band < num_sf_bands; band++) {
153         *ptr_env_sf = *ptr_env_sf + *(ptr_env_sf - 1);
154         ixheaacd_map_res_energy(*ptr_env_sf, ptr_prev_env_sf,
155                                 ixheaacd_drc_offset, band, freq_res);
156 
157         *ptr_env_sf_float = (FLOAT32)(*ptr_env_sf);
158         ptr_env_sf_float++;
159 
160         ptr_env_sf++;
161       }
162 
163     } else {
164       if (freq_res == LOW) {
165         if (ixheaacd_drc_offset < 0) {
166           WORD32 tar, index_3;
167           ixheaacd_drc_offset = -ixheaacd_drc_offset;
168           tar = ixheaacd_min32(ixheaacd_drc_offset, band);
169 
170           for (band = 0; band < tar; band++) {
171             index_3 = ((band + band) + band);
172             temp_val = add16_m(*ptr_env_sf, ptr_prev_env_sf[index_3]);
173 
174             ptr_prev_env_sf[index_3] = temp_val;
175             ptr_prev_env_sf[index_3 + 1] = temp_val;
176             ptr_prev_env_sf[index_3 + 2] = temp_val;
177             *ptr_env_sf++ = temp_val;
178 
179             *ptr_env_sf_float = (FLOAT32)temp_val;
180             ptr_env_sf_float++;
181           }
182 
183           for (; band < num_sf_bands; band++) {
184             index_3 = (band << 1) + ixheaacd_drc_offset;
185             temp_val = add16_m(*ptr_env_sf, ptr_prev_env_sf[index_3]);
186             ptr_prev_env_sf[index_3] = temp_val;
187             ptr_prev_env_sf[index_3 + 1] = temp_val;
188             *ptr_env_sf++ = temp_val;
189             *ptr_env_sf_float = (FLOAT32)temp_val;
190             ptr_env_sf_float++;
191           }
192         } else {
193           WORD32 tar, index_2;
194           WORD16 *ptr2 = ptr_prev_env_sf;
195           tar = ixheaacd_min32(ixheaacd_drc_offset, band);
196           for (band = 0; band < tar; band++) {
197             *ptr_env_sf = add16_m(*ptr_env_sf, *ptr2);
198             *ptr2 = *ptr_env_sf;
199 
200             *ptr_env_sf_float = (FLOAT32)(*ptr_env_sf);
201             ptr_env_sf_float++;
202 
203             ptr2++;
204             ptr_env_sf++;
205           }
206 
207           for (; band < num_sf_bands; band++) {
208             index_2 = (band < ixheaacd_drc_offset)
209                           ? band
210                           : ((band << 1) - ixheaacd_drc_offset);
211             temp_val = add16_m(*ptr_env_sf, ptr_prev_env_sf[index_2]);
212             ptr_prev_env_sf[index_2] = temp_val;
213             ptr_prev_env_sf[index_2 + 1] = temp_val;
214             *ptr_env_sf++ = temp_val;
215 
216             *ptr_env_sf_float = (FLOAT32)temp_val;
217             ptr_env_sf_float++;
218           }
219         }
220 
221       } else {
222         WORD16 *ptr2 = ptr_prev_env_sf;
223         for (band = num_sf_bands - 1; band >= 0; band--) {
224           *ptr_env_sf = add16_m(*ptr_env_sf, *ptr2);
225           *ptr2 = *ptr_env_sf;
226           *ptr_env_sf_float = (FLOAT32)(*ptr_env_sf);
227           ptr_env_sf_float++;
228           ptr2++;
229           ptr_env_sf++;
230         }
231         band = num_sf_bands;
232       }
233     }
234   }
235 }
236 
237 static PLATFORM_INLINE WORD32
ixheaacd_wrong_timing_compensate(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,ixheaacd_misc_tables * pstr_common_tables)238 ixheaacd_wrong_timing_compensate(ia_sbr_header_data_struct *ptr_header_data,
239                                  ia_sbr_frame_info_data_struct *ptr_sbr_data,
240                                  ia_sbr_prev_frame_data_struct *ptr_prev_data,
241                                  ixheaacd_misc_tables *pstr_common_tables) {
242   WORD32 i, num_env_sf;
243   ia_frame_info_struct *p_frame_info = &ptr_sbr_data->str_frame_info_details;
244   WORD16 *num_sf_bands = ptr_header_data->pstr_freq_band_data->num_sf_bands;
245   WORD32 start_pos_est;
246   WORD32 ref_len, new_len, shift;
247   WORD16 delta_exp;
248 
249   start_pos_est =
250       (ptr_prev_data->end_position - ptr_header_data->num_time_slots);
251 
252   ref_len = (p_frame_info->border_vec[1] - p_frame_info->border_vec[0]);
253 
254   new_len = (p_frame_info->border_vec[1] - start_pos_est);
255 
256   if (new_len <= 0) {
257     new_len = ref_len;
258     start_pos_est = p_frame_info->border_vec[0];
259   }
260 
261   delta_exp = pstr_common_tables->log_dual_is_table[ref_len];
262   delta_exp -= pstr_common_tables->log_dual_is_table[new_len];
263 
264   shift = (SHORT_BITS - ENV_EXP_FRACT - 3 - ptr_sbr_data->amp_res);
265   delta_exp = ixheaacd_shr16(delta_exp, (WORD16)shift);
266   p_frame_info->border_vec[0] = start_pos_est;
267   p_frame_info->noise_border_vec[0] = start_pos_est;
268 
269   if (start_pos_est < 0) return -1;
270 
271   if (ptr_sbr_data->coupling_mode != COUPLING_BAL) {
272     num_env_sf =
273         ((p_frame_info->freq_res[0]) ? num_sf_bands[HIGH] : num_sf_bands[LOW]);
274 
275     for (i = 0; i < num_env_sf; i++) {
276       ptr_sbr_data->int_env_sf_arr[i] =
277           add16_m(ptr_sbr_data->int_env_sf_arr[i], delta_exp);
278     }
279   }
280 
281   return 0;
282 }
283 
ixheaacd_check_env_data(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)284 WORD16 ixheaacd_check_env_data(ia_sbr_header_data_struct *ptr_header_data,
285                                ia_sbr_frame_info_data_struct *ptr_sbr_data,
286                                ia_sbr_prev_frame_data_struct *ptr_prev_data) {
287   WORD16 *ptr_evn_sf = ptr_sbr_data->int_env_sf_arr;
288   WORD16 *ptr_prev_evn_sf = ptr_prev_data->sfb_nrg_prev;
289   WORD32 i;
290   FLAG error_code = 0;
291   WORD16 sbr_max_env_sf;
292   WORD32 amp_res = ptr_sbr_data->amp_res;
293 
294   sbr_max_env_sf = (SBR_ENV_SF_MAX_VAL_1_5 >> amp_res);
295 
296   for (i = 0; i < ptr_sbr_data->num_env_sfac; i++) {
297     if (ptr_evn_sf[i] > sbr_max_env_sf) {
298       error_code = 1;
299     }
300     if (ptr_evn_sf[i] < 0) {
301       ptr_evn_sf[i] = 0;
302     }
303   }
304 
305   for (i = 0; i < ptr_header_data->pstr_freq_band_data->num_sf_bands[HIGH];
306        i++) {
307     if (ptr_prev_evn_sf[i] < 0) {
308       ptr_prev_evn_sf[i] = 0;
309     } else {
310       if (ptr_prev_evn_sf[i] > sbr_max_env_sf)
311         ptr_prev_evn_sf[i] = sbr_max_env_sf;
312     }
313   }
314   return (WORD16)(error_code);
315 }
316 
ixheaacd_dequant_env_data(ia_sbr_frame_info_data_struct * ptr_sbr_data,WORD32 amp_res)317 VOID ixheaacd_dequant_env_data(ia_sbr_frame_info_data_struct *ptr_sbr_data,
318                                WORD32 amp_res) {
319   WORD32 i;
320   WORD32 num_env_sf = ptr_sbr_data->num_env_sfac;
321   WORD32 mantissa;
322   WORD32 amp_res_1;
323   WORD32 exponent;
324   WORD32 exp_add = (7 + NRG_EXP_OFFSET);
325   WORD16 *ptr_env_sf = ptr_sbr_data->int_env_sf_arr;
326   static const WORD32 mant_arr[2] = {0x4000, 0x5a80};
327 
328   amp_res_1 = (1 - amp_res);
329 
330   for (i = num_env_sf - 1; i >= 0; i--) {
331     exponent = *ptr_env_sf;
332     mantissa = mant_arr[(exponent & amp_res_1)];
333     exponent = (exponent >> amp_res_1);
334     exponent = (exponent + exp_add);
335     *ptr_env_sf++ = (WORD16)(mantissa | (exponent & MASK_FOR_EXP));
336   }
337 }
338 
339 static PLATFORM_INLINE VOID
ixheaacd_limit_noise_floor_fac(ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct * ptr_sbr_data)340 ixheaacd_limit_noise_floor_fac(ia_sbr_header_data_struct *ptr_header_data,
341                                ia_sbr_frame_info_data_struct *ptr_sbr_data) {
342   WORD32 i, tot_nf_bands;
343   WORD32 value;
344   WORD32 num_nf_bands;
345   WORD16 *ptr_noise_floor;
346 
347   num_nf_bands = ptr_header_data->pstr_freq_band_data->num_nf_bands;
348 
349   tot_nf_bands =
350       ptr_sbr_data->str_frame_info_details.num_noise_env * num_nf_bands;
351 
352   ptr_noise_floor = ptr_sbr_data->int_noise_floor;
353 
354   for (i = tot_nf_bands - 1; i >= 0; i--) {
355     value = *ptr_noise_floor;
356     if (value > MAX_NOISE_FLOOR_FAC_VAL) {
357       *ptr_noise_floor = MAX_NOISE_FLOOR_FAC_VAL;
358     } else {
359       if (value < MIN_NOISE_FLOOR_FAC_VAL) {
360         *ptr_noise_floor = MIN_NOISE_FLOOR_FAC_VAL;
361       }
362     }
363     ptr_noise_floor++;
364   }
365 }
366 
ixheaacd_add_arr(WORD16 * ptr1,WORD16 * ptr2,WORD32 num)367 VOID ixheaacd_add_arr(WORD16 *ptr1, WORD16 *ptr2, WORD32 num) {
368   WORD32 i;
369   for (i = num - 1; i >= 0; i--) {
370     *ptr2 = (*ptr2 + *ptr1);
371     ptr2++;
372     ptr1++;
373   }
374 }
375 
ixheaacd_calc_noise_floor(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)376 IA_ERRORCODE ixheaacd_calc_noise_floor(
377     ia_sbr_header_data_struct *ptr_header_data,
378     ia_sbr_frame_info_data_struct *ptr_sbr_data,
379     ia_sbr_prev_frame_data_struct *ptr_prev_data) {
380   WORD32 i;
381   WORD32 num_nf_bands;
382   WORD32 num_noise_env;
383   WORD32 ixheaacd_drc_offset;
384   WORD16 *ptr_noise_floor = ptr_sbr_data->int_noise_floor;
385 
386   WORD16 *ptr_prev_noise_floor = ptr_prev_data->prev_noise_level;
387   WORD16 *ptr1, *ptr2;
388   WORD32 num;
389   FLOAT32 *ptr_noise_floor_float = ptr_sbr_data->flt_noise_floor;
390 
391   num_nf_bands = ptr_header_data->pstr_freq_band_data->num_nf_bands;
392   num_noise_env = ptr_sbr_data->str_frame_info_details.num_noise_env;
393 
394   if (ptr_sbr_data->del_cod_dir_noise_arr[0] == DTDF_DIR_FREQ) {
395     ptr1 = ptr_noise_floor++;
396     ptr2 = ptr_noise_floor;
397     num = num_nf_bands - 1;
398   } else {
399     ptr1 = ptr_prev_noise_floor;
400     ptr2 = ptr_sbr_data->int_noise_floor;
401     num = num_nf_bands;
402   }
403 
404   ixheaacd_add_arr(ptr1, ptr2, num);
405 
406   if (num_noise_env > 1) {
407     if (ptr_sbr_data->del_cod_dir_noise_arr[1] == DTDF_DIR_FREQ) {
408       ptr1 = &ptr_sbr_data->int_noise_floor[num_nf_bands];
409       ptr2 = &ptr_sbr_data->int_noise_floor[(num_nf_bands + 1)];
410 
411       num = num_nf_bands - 1;
412     } else {
413       ptr1 = &ptr_sbr_data->int_noise_floor[0];
414       ptr2 = &ptr_sbr_data->int_noise_floor[num_nf_bands];
415 
416       num = num_nf_bands;
417     }
418     ixheaacd_add_arr(ptr1, ptr2, num);
419   }
420 
421   ixheaacd_limit_noise_floor_fac(ptr_header_data, ptr_sbr_data);
422 
423   ixheaacd_drc_offset = num_nf_bands * (num_noise_env - 1);
424   if (ixheaacd_drc_offset < 0 || ixheaacd_drc_offset >= MAX_NUM_NOISE_VALUES)
425     return IA_FATAL_ERROR;
426   ptr1 = &ptr_sbr_data->int_noise_floor[ixheaacd_drc_offset];
427   ptr2 = ptr_prev_noise_floor;
428 
429   memcpy(ptr2, ptr1, sizeof(WORD16) * (num_nf_bands));
430 
431   if ((ptr_sbr_data->coupling_mode != COUPLING_BAL) ||
432       (ptr_header_data->usac_flag)) {
433     WORD32 noise_floor_exp, tot_nf_bands;
434 
435     tot_nf_bands = (num_nf_bands * num_noise_env);
436     ptr_noise_floor = &ptr_sbr_data->int_noise_floor[0];
437 
438     for (i = 0; i < tot_nf_bands; i++) {
439       noise_floor_exp =
440           (NOISE_FLOOR_OFFSET_INT + 1 + NOISE_EXP_OFFSET - *ptr_noise_floor);
441 
442       *ptr_noise_floor_float++ = *ptr_noise_floor;
443       *ptr_noise_floor++ = (WORD16)(0x4000 + (noise_floor_exp & MASK_FOR_EXP));
444     }
445   }
446   return IA_NO_ERROR;
447 }
448 
ixheaacd_dec_sbrdata_for_pvc(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)449 IA_ERRORCODE ixheaacd_dec_sbrdata_for_pvc(
450     ia_sbr_header_data_struct *ptr_header_data,
451     ia_sbr_frame_info_data_struct *ptr_sbr_data,
452     ia_sbr_prev_frame_data_struct *ptr_prev_data) {
453   WORD32 err = 0;
454   err = ixheaacd_calc_noise_floor(ptr_header_data, ptr_sbr_data, ptr_prev_data);
455   if (err) return err;
456 
457   if (!ptr_sbr_data->coupling_mode) {
458     ptr_sbr_data->num_noise_sfac =
459         ptr_header_data->pstr_freq_band_data->num_nf_bands *
460         ptr_sbr_data->str_frame_info_details.num_noise_env;
461     ixheaacd_dequant_pvc_env_data(ptr_sbr_data->num_noise_sfac,
462                                   ptr_sbr_data->flt_noise_floor);
463   }
464   return IA_NO_ERROR;
465 }
466 
ixheaacd_sbr_env_dequant_coup_fix(ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct * ptr_data_left,ia_sbr_frame_info_data_struct * ptr_data_right,ixheaacd_misc_tables * pstr_common_tables)467 VOID ixheaacd_sbr_env_dequant_coup_fix(
468     ia_sbr_header_data_struct *ptr_header_data,
469     ia_sbr_frame_info_data_struct *ptr_data_left,
470     ia_sbr_frame_info_data_struct *ptr_data_right,
471     ixheaacd_misc_tables *pstr_common_tables) {
472   WORD32 i;
473   WORD32 num_env_sf = ptr_data_left->num_env_sfac;
474   WORD16 temp_left_mant, temp_right_mant, temp_right_plus1_mant, new_left_mant,
475       new_right_mant;
476   WORD16 temp_left_exp, temp_right_exp, temp_rightplus1_exp, new_left_exp,
477       new_right_exp;
478   WORD32 i_end;
479   WORD16 *r_data = ptr_data_right->int_env_sf_arr;
480   WORD16 *l_data = ptr_data_left->int_env_sf_arr;
481 
482   for (i = 0; i < num_env_sf; i++) {
483     temp_right_mant = (WORD16)(*r_data & MASK_M);
484     temp_right_exp = (WORD16)(*r_data & MASK_FOR_EXP);
485 
486     temp_right_exp = sub16_m(temp_right_exp, add16_m(18, NRG_EXP_OFFSET));
487     temp_left_mant = (WORD16)(*l_data & MASK_M);
488     temp_left_exp = (WORD16)(*l_data & MASK_FOR_EXP);
489 
490     temp_left_exp = sub16_m(temp_left_exp, NRG_EXP_OFFSET);
491 
492     ixheaacd_fix_mant_exp_add(temp_right_mant, temp_right_exp, 0x4000, 1,
493                               &temp_right_plus1_mant, &temp_rightplus1_exp);
494 
495     new_right_exp = ixheaacd_fix_mant_div(temp_left_mant, temp_right_plus1_mant,
496                                           &new_right_mant, pstr_common_tables);
497 
498     new_right_exp += temp_left_exp - temp_rightplus1_exp + 2;
499 
500     new_left_mant = ixheaacd_mult16_shl(temp_right_mant, new_right_mant);
501 
502     new_left_exp = add16_m(temp_right_exp, new_right_exp);
503 
504     *r_data++ = (WORD16)(((new_right_mant + ROUNDING) & MASK_M) +
505                          ((new_right_exp + NRG_EXP_OFFSET) & MASK_FOR_EXP));
506     *l_data++ = (WORD16)(((new_left_mant + ROUNDING) & MASK_M) +
507                          ((new_left_exp + NRG_EXP_OFFSET) & MASK_FOR_EXP));
508   }
509 
510   i_end = ptr_header_data->pstr_freq_band_data->num_nf_bands *
511           ptr_data_left->str_frame_info_details.num_noise_env;
512   r_data = ptr_data_right->int_noise_floor;
513   l_data = ptr_data_left->int_noise_floor;
514 
515   for (i = i_end - 1; i >= 0; i--) {
516     temp_left_exp =
517         sub16_m((WORD16)(*l_data & (WORD16)MASK_FOR_EXP), NOISE_EXP_OFFSET);
518     temp_right_exp = sub16_m(*r_data, 12);
519 
520     ixheaacd_fix_mant_exp_add(0x4000, ixheaacd_add16(1, temp_right_exp), 0x4000,
521                               1, &temp_right_plus1_mant, &temp_rightplus1_exp);
522 
523     new_right_exp = ixheaacd_fix_mant_div(0x4000, temp_right_plus1_mant,
524                                           &new_right_mant, pstr_common_tables);
525 
526     new_right_exp += temp_left_exp - temp_rightplus1_exp + 2;
527 
528     new_left_mant = new_right_mant;
529     new_left_exp = add16_m(new_right_exp, temp_right_exp);
530     *r_data++ = (WORD16)(((new_right_mant + ROUNDING) & MASK_M) +
531                          ((new_right_exp + NOISE_EXP_OFFSET) & MASK_FOR_EXP));
532     *l_data++ = (WORD16)(((new_left_mant + ROUNDING) & MASK_M) +
533                          ((new_left_exp + NOISE_EXP_OFFSET) & MASK_FOR_EXP));
534   }
535 }
536 
ixheaacd_sbr_env_dequant_coup(ia_sbr_frame_info_data_struct * ptr_data_ch_0,ia_sbr_frame_info_data_struct * ptr_data_ch_1)537 VOID ixheaacd_sbr_env_dequant_coup(
538     ia_sbr_frame_info_data_struct *ptr_data_ch_0,
539     ia_sbr_frame_info_data_struct *ptr_data_ch_1) {
540   FLOAT32 *ptr_env_sf_left = ptr_data_ch_0->flt_env_sf_arr;
541   FLOAT32 *ptr_env_sf_right = ptr_data_ch_1->flt_env_sf_arr;
542   FLOAT32 *ptr_noise_floor_left = ptr_data_ch_0->flt_noise_floor;
543   FLOAT32 *ptr_noise_floor_right = ptr_data_ch_1->flt_noise_floor;
544   WORD32 num_env_sf = ptr_data_ch_0->num_env_sfac;
545   WORD32 num_noise_fac = ptr_data_ch_0->num_noise_sfac;
546   WORD32 amp_res = ptr_data_ch_0->amp_res;
547 
548   WORD32 i;
549   FLOAT32 temp_l, temp_r;
550   static const FLOAT32 pan_offset[2] = {24.0f, 12.0f};
551   static const FLOAT32 a_arr[2] = {0.5f, 1.0f};
552 
553   FLOAT32 a = a_arr[amp_res];
554 
555   for (i = 0; i < num_env_sf; i++) {
556     temp_l = ptr_env_sf_left[i];
557     temp_r = ptr_env_sf_right[i];
558 
559     ptr_env_sf_left[i] =
560         (FLOAT32)(64 * (pow(2, temp_l * a + 1) /
561                         (1 + pow(2, (pan_offset[amp_res] - temp_r) * a))));
562     ptr_env_sf_right[i] =
563         (FLOAT32)(64 * (pow(2, temp_l * a + 1) /
564                         (1 + pow(2, (temp_r - pan_offset[amp_res]) * a))));
565   }
566 
567   for (i = 0; i < num_noise_fac; i++) {
568     temp_l = ptr_noise_floor_left[i];
569     temp_r = ptr_noise_floor_right[i];
570 
571     ptr_noise_floor_left[i] =
572         (FLOAT32)(pow(2, NOISE_FLOOR_OFFSET - temp_l + 1) /
573                   (1 + pow(2, pan_offset[1] - temp_r)));
574     ptr_noise_floor_right[i] =
575         (FLOAT32)(pow(2, NOISE_FLOOR_OFFSET - temp_l + 1) /
576                   (1 + pow(2, temp_r - pan_offset[1])));
577   }
578 }
ixheaacd_dec_sbrdata(ia_sbr_header_data_struct * ptr_header_data_ch_0,ia_sbr_header_data_struct * ptr_header_data_ch_1,ia_sbr_frame_info_data_struct * ptr_sbr_data_ch_0,ia_sbr_prev_frame_data_struct * ptr_prev_data_ch_0,ia_sbr_frame_info_data_struct * ptr_sbr_data_ch_1,ia_sbr_prev_frame_data_struct * ptr_prev_data_ch_1,ixheaacd_misc_tables * ptr_common_tables)579 WORD32 ixheaacd_dec_sbrdata(ia_sbr_header_data_struct *ptr_header_data_ch_0,
580                             ia_sbr_header_data_struct *ptr_header_data_ch_1,
581                             ia_sbr_frame_info_data_struct *ptr_sbr_data_ch_0,
582                             ia_sbr_prev_frame_data_struct *ptr_prev_data_ch_0,
583                             ia_sbr_frame_info_data_struct *ptr_sbr_data_ch_1,
584                             ia_sbr_prev_frame_data_struct *ptr_prev_data_ch_1,
585                             ixheaacd_misc_tables *ptr_common_tables) {
586   FLAG error_code;
587   WORD32 err = 0;
588   WORD32 usac_flag = ptr_header_data_ch_0->usac_flag;
589 
590   err = ixheaacd_dec_envelope(ptr_header_data_ch_0, ptr_sbr_data_ch_0,
591                               ptr_prev_data_ch_0, ptr_prev_data_ch_1,
592                               ptr_common_tables);
593 
594   if (err) return err;
595 
596   err = ixheaacd_calc_noise_floor(ptr_header_data_ch_0, ptr_sbr_data_ch_0,
597                                   ptr_prev_data_ch_0);
598 
599   if (err == (WORD32)IA_FATAL_ERROR) return (WORD32)IA_FATAL_ERROR;
600 
601   if (!ptr_sbr_data_ch_0->coupling_mode && usac_flag) {
602     ptr_sbr_data_ch_0->num_noise_sfac =
603         ptr_header_data_ch_0->pstr_freq_band_data->num_nf_bands *
604         ptr_sbr_data_ch_0->str_frame_info_details.num_noise_env;
605 
606     ixheaacd_dequant_esbr_env_data(
607         ptr_sbr_data_ch_0->flt_env_sf_arr, ptr_sbr_data_ch_0->num_env_sfac,
608         ptr_sbr_data_ch_0->num_noise_sfac, ptr_sbr_data_ch_0->amp_res,
609         ptr_sbr_data_ch_0->flt_noise_floor);
610   }
611 
612   if (ptr_sbr_data_ch_1 != NULL) {
613     error_code = ptr_header_data_ch_0->err_flag;
614     err = ixheaacd_dec_envelope(ptr_header_data_ch_1, ptr_sbr_data_ch_1,
615                                 ptr_prev_data_ch_1, ptr_prev_data_ch_0,
616                                 ptr_common_tables);
617 
618     if (err) return err;
619 
620     err = ixheaacd_calc_noise_floor(ptr_header_data_ch_1, ptr_sbr_data_ch_1,
621                                     ptr_prev_data_ch_1);
622 
623     if (err) return err;
624 
625     if (!ptr_sbr_data_ch_1->coupling_mode && usac_flag) {
626       ptr_sbr_data_ch_1->num_noise_sfac =
627           ptr_header_data_ch_1->pstr_freq_band_data->num_nf_bands *
628           ptr_sbr_data_ch_1->str_frame_info_details.num_noise_env;
629 
630       ixheaacd_dequant_esbr_env_data(
631           ptr_sbr_data_ch_1->flt_env_sf_arr, ptr_sbr_data_ch_1->num_env_sfac,
632           ptr_sbr_data_ch_1->num_noise_sfac, ptr_sbr_data_ch_1->amp_res,
633           ptr_sbr_data_ch_1->flt_noise_floor);
634     }
635 
636     if (!usac_flag) {
637       if (!error_code && ptr_header_data_ch_0->err_flag) {
638         err = ixheaacd_dec_envelope(ptr_header_data_ch_0, ptr_sbr_data_ch_0,
639                                     ptr_prev_data_ch_0, ptr_prev_data_ch_1,
640                                     ptr_common_tables);
641 
642         if (err) return err;
643       }
644     }
645 
646     if (ptr_sbr_data_ch_0->coupling_mode) {
647       ptr_sbr_data_ch_0->num_noise_sfac =
648           ptr_header_data_ch_1->pstr_freq_band_data->num_nf_bands *
649           ptr_sbr_data_ch_1->str_frame_info_details.num_noise_env;
650       ixheaacd_sbr_env_dequant_coup_fix(ptr_header_data_ch_0, ptr_sbr_data_ch_0,
651                                         ptr_sbr_data_ch_1, ptr_common_tables);
652 
653       ixheaacd_sbr_env_dequant_coup(ptr_sbr_data_ch_0, ptr_sbr_data_ch_1);
654     }
655   }
656 
657   return 0;
658 }
ixheaacd_dec_envelope(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_ch_0,ia_sbr_prev_frame_data_struct * ptr_prev_data_ch_1,ixheaacd_misc_tables * pstr_common_tables)659 WORD32 ixheaacd_dec_envelope(ia_sbr_header_data_struct *ptr_header_data,
660                              ia_sbr_frame_info_data_struct *ptr_sbr_data,
661                              ia_sbr_prev_frame_data_struct *ptr_prev_data_ch_0,
662                              ia_sbr_prev_frame_data_struct *ptr_prev_data_ch_1,
663                              ixheaacd_misc_tables *pstr_common_tables) {
664   FLAG error_code;
665   WORD32 err;
666   WORD16 env_sf_local_arr[MAX_FREQ_COEFFS];
667   WORD32 usac_flag = ptr_header_data->usac_flag;
668   WORD32 temp_1 =
669       ptr_prev_data_ch_0->end_position - ptr_header_data->num_time_slots;
670   if (temp_1 < 0) return -1;
671   temp_1 = ptr_sbr_data->str_frame_info_details.border_vec[0] - temp_1;
672 
673   if ((!ptr_header_data->err_flag_prev) && (!ptr_header_data->err_flag) &&
674       (temp_1 != 0)) {
675     if (ptr_sbr_data->del_cod_dir_arr[0] == DTDF_DIR_TIME) {
676       ptr_header_data->err_flag = 1;
677     } else {
678       ptr_header_data->err_flag_prev = 1;
679     }
680   }
681 
682   if (ptr_header_data->err_flag && !usac_flag) {
683     ixheaacd_lean_sbrconcealment(ptr_header_data, ptr_sbr_data,
684                                  ptr_prev_data_ch_0);
685 
686     ixheaacd_process_del_cod_env_data(ptr_header_data, ptr_sbr_data,
687                                       ptr_prev_data_ch_0);
688   } else {
689     WORD32 num = ptr_header_data->pstr_freq_band_data->num_sf_bands[HIGH];
690     if (ptr_header_data->err_flag_prev && !usac_flag) {
691       WORD16 *ptr1, *ptr2;
692       WORD32 i;
693 
694       err = ixheaacd_wrong_timing_compensate(ptr_header_data, ptr_sbr_data,
695                                              ptr_prev_data_ch_0,
696                                              pstr_common_tables);
697 
698       if (err) return err;
699 
700       if (ptr_sbr_data->coupling_mode !=
701           (WORD16)ptr_prev_data_ch_0->coupling_mode) {
702         if (ptr_prev_data_ch_0->coupling_mode == COUPLING_BAL) {
703           memcpy(ptr_prev_data_ch_0->sfb_nrg_prev,
704                  ptr_prev_data_ch_1->sfb_nrg_prev, sizeof(WORD16) * num);
705         } else {
706           if (ptr_sbr_data->coupling_mode == COUPLING_LEVEL) {
707             ptr1 = ptr_prev_data_ch_0->sfb_nrg_prev;
708             ptr2 = ptr_prev_data_ch_1->sfb_nrg_prev;
709 
710             for (i = 0; i < num; i++) {
711               *ptr1 = (add16_m(*ptr1, *ptr2) >> 1);
712               ptr2++;
713               ptr1++;
714             }
715           } else {
716             if (ptr_sbr_data->coupling_mode == COUPLING_BAL) {
717               memset(ptr_prev_data_ch_0->sfb_nrg_prev, SBR_ENERGY_PAN_OFFSET,
718                      sizeof(WORD16) * num);
719             }
720           }
721         }
722       }
723     }
724 
725     memcpy(env_sf_local_arr, ptr_prev_data_ch_0->sfb_nrg_prev,
726            sizeof(WORD16) * MAX_FREQ_COEFFS);
727 
728     ixheaacd_process_del_cod_env_data(ptr_header_data, ptr_sbr_data,
729                                       ptr_prev_data_ch_0);
730 
731     if (!usac_flag) {
732       error_code = ixheaacd_check_env_data(ptr_header_data, ptr_sbr_data,
733                                            ptr_prev_data_ch_0);
734 
735       if (error_code) {
736         ptr_header_data->err_flag = 1;
737 
738         memcpy(ptr_prev_data_ch_0->sfb_nrg_prev, env_sf_local_arr,
739                sizeof(WORD16) * MAX_FREQ_COEFFS);
740 
741         err = ixheaacd_dec_envelope(ptr_header_data, ptr_sbr_data,
742                                     ptr_prev_data_ch_0, ptr_prev_data_ch_1,
743                                     pstr_common_tables);
744 
745         if (err) return err;
746         return 0;
747       }
748     }
749   }
750   if (!usac_flag)
751     ixheaacd_dequant_env_data(ptr_sbr_data, ptr_sbr_data->amp_res);
752 
753   return 0;
754 }
755 
ixheaacd_adj_timeslot(WORD32 * ptr_buf_real,WORD32 * ptr_buf_imag,WORD16 * ptr_filt_buf,WORD16 * ptr_filt_buf_noise,WORD16 * ptr_gain_buf,WORD16 * ptr_noise_floor,WORD16 * ptr_sine_lvl_buf,WORD16 noise_floor_exp,WORD16 * ptr_harm_index,WORD16 sub_band_start,WORD16 num_sub_bands,WORD16 scale_change,WORD16 smooth_ratio,FLAG num_noise_flg,WORD16 * ptr_phase_index,ia_sbr_tables_struct * ptr_sbr_tables)756 VOID ixheaacd_adj_timeslot(WORD32 *ptr_buf_real, WORD32 *ptr_buf_imag,
757                            WORD16 *ptr_filt_buf, WORD16 *ptr_filt_buf_noise,
758                            WORD16 *ptr_gain_buf, WORD16 *ptr_noise_floor,
759                            WORD16 *ptr_sine_lvl_buf, WORD16 noise_floor_exp,
760                            WORD16 *ptr_harm_index, WORD16 sub_band_start,
761                            WORD16 num_sub_bands, WORD16 scale_change,
762                            WORD16 smooth_ratio, FLAG num_noise_flg,
763                            WORD16 *ptr_phase_index,
764                            ia_sbr_tables_struct *ptr_sbr_tables) {
765   WORD16 k;
766   WORD16 *ptr_smoothed_gain, *ptr_smoothed_noise;
767   WORD16 direct_ratio;
768   WORD32 index = *ptr_phase_index;
769   WORD32 harm_idx = *ptr_harm_index;
770   WORD32 freq_inv_flag;
771   const WORD32 *ptr_rand_ph_buf;
772   WORD32 factor = 0;
773 
774   direct_ratio = ixheaacd_sub16_sat(0x7fff, smooth_ratio);
775   freq_inv_flag = (sub_band_start & 1);
776 
777   scale_change = scale_change - 1;
778 
779   ptr_rand_ph_buf = &ptr_sbr_tables->sbr_rand_ph[index];
780   *ptr_phase_index =
781       (WORD16)((index + num_sub_bands) & (SBR_NF_NO_RANDOM_VAL - 1));
782 
783   if (smooth_ratio) {
784     WORD16 *ptr_filt_buf_local = &ptr_filt_buf[0];
785     WORD16 *ptr_gain_buf_local = &ptr_gain_buf[0];
786     WORD16 *ptr_filt_noise_local = &ptr_filt_buf_noise[0];
787     WORD16 *ptr_noise_floor_local = &ptr_noise_floor[0];
788 
789     WORD16 tmp, tmp1;
790 
791     for (k = 0; k < num_sub_bands; k++) {
792       tmp = add16_m(mult16x16_16(smooth_ratio, *ptr_filt_buf_local),
793                     mult16x16_16(direct_ratio, *ptr_gain_buf_local++));
794 
795       ptr_gain_buf_local++;
796 
797       tmp1 = add16_m(mult16x16_16(smooth_ratio, *ptr_filt_noise_local),
798                      mult16x16_16(direct_ratio, *ptr_noise_floor_local++));
799 
800       ptr_noise_floor_local++;
801 
802       *ptr_filt_buf_local++ = tmp << 1;
803       ptr_filt_buf_local++;
804       *ptr_filt_noise_local++ = tmp1 << 1;
805     }
806     ptr_smoothed_gain = ptr_filt_buf;
807     ptr_smoothed_noise = ptr_filt_buf_noise;
808     factor = 1;
809   } else {
810     ptr_smoothed_gain = ptr_gain_buf;
811     ptr_smoothed_noise = ptr_noise_floor;
812     factor = 2;
813   }
814 
815   switch (harm_idx) {
816     case 0:
817     case 2:
818       ixheaacd_harm_idx_zerotwo(num_noise_flg, num_sub_bands, ptr_buf_real,
819                                 ptr_buf_imag, ptr_smoothed_gain,
820                                 ptr_smoothed_noise, factor, ptr_gain_buf,
821                                 scale_change, ptr_rand_ph_buf, ptr_sine_lvl_buf,
822                                 noise_floor_exp, harm_idx);
823       break;
824     case 1:
825     case 3:
826       ixheaacd_harm_idx_onethree(
827           num_noise_flg, num_sub_bands, ptr_buf_real, ptr_buf_imag,
828           ptr_smoothed_gain, ptr_smoothed_noise, factor, ptr_gain_buf,
829           scale_change, ptr_rand_ph_buf, ptr_sine_lvl_buf, noise_floor_exp,
830           freq_inv_flag, harm_idx);
831       break;
832   }
833   *ptr_harm_index = (WORD16)((harm_idx + 1) & 3);
834 }
835