• 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 
31 #include "ixheaacd_basic_op.h"
32 #include "ixheaacd_intrinsics.h"
33 #include "ixheaacd_common_rom.h"
34 #include "ixheaacd_basic_funcs.h"
35 #include "ixheaacd_bitbuffer.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_sbrdecsettings.h"
41 #include "ixheaacd_sbr_scale.h"
42 #include "ixheaacd_lpp_tran.h"
43 #include "ixheaacd_env_extr_part.h"
44 #include "ixheaacd_sbr_rom.h"
45 #include "ixheaacd_hybrid.h"
46 #include "ixheaacd_ps_dec.h"
47 #include "ixheaacd_env_extr.h"
48 
49 #include "ixheaacd_sbr_const.h"
50 #include "ixheaacd_intrinsics.h"
51 
52 #include "ixheaacd_pvc_dec.h"
53 
54 #include "ixheaacd_ps_bitdec.h"
55 
56 #include "ixheaacd_audioobjtypes.h"
57 
ixheaacd_cnt_leading_ones(WORD32 a)58 WORD32 ixheaacd_cnt_leading_ones(WORD32 a) {
59   WORD32 count = 0;
60 
61   while (a) {
62     if (a & 0x80000000)
63       count++;
64     else
65       break;
66     a = a << 1;
67   }
68   return count;
69 }
ixheaacd_huffman_decode(WORD32 it_bit_buff,WORD16 * h_index,WORD16 * len,const UWORD16 * input_table,const UWORD32 * idx_table)70 VOID ixheaacd_huffman_decode(WORD32 it_bit_buff, WORD16 *h_index, WORD16 *len,
71                              const UWORD16 *input_table,
72                              const UWORD32 *idx_table) {
73   UWORD32 temp = 0;
74   UWORD32 temp1 = 0;
75   WORD32 found = 0;
76   UWORD32 mask = 0x80000000;
77 
78   WORD32 clo;
79   WORD32 MAX_LEN;
80   WORD32 ixheaacd_drc_offset = 0;
81   WORD32 length;
82   UWORD32 cwrd;
83   WORD32 len_end;
84 
85   MAX_LEN = input_table[0];
86   mask = mask - (1 << (31 - MAX_LEN));
87   mask = mask << 1;
88   temp = (UWORD32)(it_bit_buff & mask);
89 
90   len_end = input_table[0];
91   clo = ixheaacd_cnt_leading_ones(temp);
92   do {
93     ixheaacd_drc_offset = (idx_table[clo] >> 20) & 0xff;
94     length = input_table[ixheaacd_drc_offset + 1] & 0x1f;
95     cwrd = idx_table[clo] & 0xfffff;
96     temp1 = temp >> (32 - length);
97     if (temp1 <= cwrd) {
98       ixheaacd_drc_offset = ixheaacd_drc_offset - (cwrd - temp1);
99       found = 1;
100     } else {
101       len_end = len_end + ((idx_table[clo] >> 28) & 0xf);
102       clo = len_end;
103     }
104   } while (!found);
105   *h_index = input_table[ixheaacd_drc_offset + 1] >> 5;
106   *len = length;
107 }
108 
ixheaacd_read_esbr_pvc_envelope(ia_pvc_data_struct * ptr_pvc_data,ia_bit_buf_struct * it_bit_buff,WORD32 indepFlag)109 static WORD32 ixheaacd_read_esbr_pvc_envelope(ia_pvc_data_struct *ptr_pvc_data,
110                                               ia_bit_buf_struct *it_bit_buff,
111                                               WORD32 indepFlag) {
112   WORD32 i, j, k;
113   WORD32 fixed_length = 0, num_grid_info = 0, grid_info;
114   UWORD8 div_mode, ns_mode;
115   UWORD16 pvc_id[PVC_NUM_TIME_SLOTS + 1];
116   UWORD8 num_length;
117   UWORD8 length;
118   UWORD8 reuse_pvc_id;
119   WORD32 sum_length = 0;
120   WORD32 length_bits = 4;
121   UWORD8 pvc_id_bits = PVC_ID_BITS;
122 
123   div_mode = (UWORD8)ixheaacd_read_bits_buf(it_bit_buff, PVC_DIV_MODE_BITS);
124   ns_mode = (UWORD8)ixheaacd_read_bits_buf(it_bit_buff, PVC_NS_MODE_BITS);
125 
126   if (ptr_pvc_data->pvc_mode == 3) {
127     pvc_id_bits = 0;
128   }
129 
130   if (div_mode <= 3) {
131     num_length = div_mode;
132     if (indepFlag) {
133       reuse_pvc_id = 0;
134     } else {
135       reuse_pvc_id =
136           (UWORD8)ixheaacd_read_bits_buf(it_bit_buff, PVC_REUSE_PVC_ID_BITS);
137     }
138     if (reuse_pvc_id == 1) {
139       pvc_id[0] = ptr_pvc_data->prev_pvc_id;
140     } else {
141       pvc_id[0] = (UWORD16)ixheaacd_read_bits_buf(it_bit_buff, pvc_id_bits);
142     }
143 
144     k = 1;
145     if (num_length) {
146       sum_length = 0;
147       for (i = 0; i < num_length; i++) {
148         if (sum_length >= 13) {
149           length_bits = 1;
150         } else if (sum_length >= 11) {
151           length_bits = 2;
152         } else if (sum_length >= 7) {
153           length_bits = 3;
154         } else {
155           length_bits = 4;
156         }
157         length = (UWORD8)ixheaacd_read_bits_buf(it_bit_buff, length_bits);
158         length += 1;
159         sum_length += length;
160         if ((k + length - 1) > PVC_NUM_TIME_SLOTS) {
161           return -1;
162         }
163         for (j = 1; j < length; j++, k++) {
164           pvc_id[k] = pvc_id[k - 1];
165         }
166         pvc_id[k++] = (UWORD16)ixheaacd_read_bits_buf(it_bit_buff, pvc_id_bits);
167       }
168     }
169 
170     for (; k < 16; k++) {
171       pvc_id[k] = pvc_id[k - 1];
172     }
173 
174   } else {
175     switch (div_mode) {
176       case 4:
177         num_grid_info = 2;
178         fixed_length = 8;
179         break;
180       case 5:
181         num_grid_info = 4;
182         fixed_length = 4;
183         break;
184       case 6:
185         num_grid_info = 8;
186         fixed_length = 2;
187         break;
188       case 7:
189         num_grid_info = 16;
190         fixed_length = 1;
191         break;
192       default:;
193     }
194     if (indepFlag) {
195       grid_info = 1;
196     } else {
197       grid_info = ixheaacd_read_bits_buf(it_bit_buff, PVC_GRID_INFO_BITS);
198     }
199     if (grid_info) {
200       pvc_id[0] = (UWORD16)ixheaacd_read_bits_buf(it_bit_buff, pvc_id_bits);
201     } else {
202       pvc_id[0] = ptr_pvc_data->prev_pvc_id;
203     }
204     for (j = 1, k = 1; j < fixed_length; j++, k++) {
205       pvc_id[k] = pvc_id[k - 1];
206     }
207 
208     for (i = 1; i < num_grid_info; i++) {
209       grid_info = ixheaacd_read_bits_buf(it_bit_buff, PVC_GRID_INFO_BITS);
210       if (grid_info == 1) {
211         pvc_id[k++] = (UWORD16)ixheaacd_read_bits_buf(it_bit_buff, pvc_id_bits);
212       } else {
213         pvc_id[k] = pvc_id[k - 1];
214         k++;
215       }
216       for (j = 1; j < fixed_length; j++, k++) {
217         pvc_id[k] = pvc_id[k - 1];
218       }
219     }
220   }
221   ptr_pvc_data->div_mode = div_mode;
222   ptr_pvc_data->ns_mode = ns_mode;
223   for (i = 0; i < PVC_NUM_TIME_SLOTS; i++) {
224     ptr_pvc_data->pvc_id[i] = pvc_id[i];
225   }
226   return 0;
227 }
228 
ixheaacd_pvc_env_dtdf_data(ia_sbr_frame_info_data_struct * ptr_frame_data,ia_bit_buf_struct * it_bit_buff)229 static VOID ixheaacd_pvc_env_dtdf_data(
230     ia_sbr_frame_info_data_struct *ptr_frame_data,
231     ia_bit_buf_struct *it_bit_buff) {
232   WORD32 i;
233   WORD32 usac_independency_flag = ptr_frame_data->usac_independency_flag;
234   WORD32 bs_num_noise = ptr_frame_data->str_frame_info_details.num_noise_env;
235 
236   if (usac_independency_flag) {
237     ptr_frame_data->del_cod_dir_noise_arr[0] = 0;
238   } else {
239     ptr_frame_data->del_cod_dir_noise_arr[0] =
240         ixheaacd_read_bits_buf(it_bit_buff, ESBR_DOMAIN_BITS);
241   }
242 
243   for (i = 1; i < bs_num_noise; i++) {
244     ptr_frame_data->del_cod_dir_noise_arr[i] =
245         ixheaacd_read_bits_buf(it_bit_buff, ESBR_DOMAIN_BITS);
246   }
247 }
248 
ixheaacd_read_sbr_addi_data(ia_sbr_frame_info_data_struct * ptr_frame_data,ia_sbr_header_data_struct * ptr_header_data,ia_bit_buf_struct * it_bit_buff)249 static VOID ixheaacd_read_sbr_addi_data(
250     ia_sbr_frame_info_data_struct *ptr_frame_data,
251     ia_sbr_header_data_struct *ptr_header_data,
252     ia_bit_buf_struct *it_bit_buff) {
253   WORD32 i;
254 
255   WORD32 flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
256 
257   ptr_frame_data->sin_start_for_cur_top =
258       ptr_frame_data->sin_start_for_next_top;
259   ptr_frame_data->sin_len_for_cur_top = ptr_frame_data->sin_len_for_next_top;
260   ptr_frame_data->sin_start_for_next_top = 0;
261   ptr_frame_data->sin_len_for_next_top = 0;
262 
263   if (flag) {
264     for (i = 0; i < ptr_header_data->pstr_freq_band_data->num_sf_bands[HIGH];
265          i++) {
266       ptr_frame_data->add_harmonics[i] = ixheaacd_read_bits_buf(it_bit_buff, 1);
267     }
268     if (ptr_frame_data->pvc_mode != 0) {
269       ptr_frame_data->sine_position = ESC_SIN_POS;
270 
271       ptr_frame_data->bs_sin_pos_present =
272           ixheaacd_read_bits_buf(it_bit_buff, 1);
273 
274       if (ptr_frame_data->bs_sin_pos_present == 1) {
275         ptr_frame_data->sine_position = ixheaacd_read_bits_buf(it_bit_buff, 5);
276       }
277       if (ptr_frame_data->var_len > 0) {
278         if (ptr_frame_data->sine_position > 16) {
279           if (ptr_frame_data->sine_position == 31) {
280             ptr_frame_data->sin_start_for_next_top = 0;
281             ptr_frame_data->sin_len_for_next_top = ptr_frame_data->var_len;
282           } else {
283             if ((ptr_frame_data->var_len + 16) ==
284                 ptr_frame_data->sine_position) {
285               ptr_frame_data->sin_start_for_next_top = 0;
286               ptr_frame_data->sin_len_for_next_top = ptr_frame_data->var_len;
287             } else {
288               ptr_frame_data->sin_start_for_next_top =
289                   ptr_frame_data->sine_position - 16;
290               ptr_frame_data->sin_len_for_next_top = ptr_frame_data->var_len;
291             }
292           }
293         } else {
294           ptr_frame_data->sin_start_for_next_top = 0;
295           ptr_frame_data->sin_len_for_next_top = ptr_frame_data->var_len;
296         }
297       } else {
298         ptr_frame_data->sin_start_for_next_top = 0;
299         ptr_frame_data->sin_len_for_next_top = 0;
300       }
301     }
302   }
303   return;
304 }
305 
ixheaacd_ssc_huff_dec(ia_huffman_data_type t_huff,ia_handle_bit_buf_struct it_bit_buff)306 WORD32 ixheaacd_ssc_huff_dec(ia_huffman_data_type t_huff,
307                              ia_handle_bit_buf_struct it_bit_buff) {
308   WORD32 index;
309   WORD32 value, bit;
310   WORD16 cw;
311   index = 0;
312 
313   while (index >= 0) {
314     cw = t_huff[index];
315 
316     bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
317 
318     if (bit) {
319       WORD sign = (cw & 0x0080);
320       if (sign) {
321         index = (cw | 0xffffff80);
322       } else {
323         index = (cw & 0x007f);
324       }
325     } else {
326       index = (cw >> 8);
327     }
328   }
329 
330   value = (index + 64);
331 
332   return (value);
333 }
334 
ixheaacd_sbr_read_header_data(ia_sbr_header_data_struct * pstr_sbr_header,ia_bit_buf_struct * it_bit_buff,FLAG stereo_flag,ia_sbr_header_data_struct * pstr_sbr_dflt_header)335 WORD32 ixheaacd_sbr_read_header_data(
336     ia_sbr_header_data_struct *pstr_sbr_header, ia_bit_buf_struct *it_bit_buff,
337     FLAG stereo_flag, ia_sbr_header_data_struct *pstr_sbr_dflt_header) {
338   ia_sbr_header_data_struct prev_header_info;
339   FLAG header_extra_1 = 0, header_extra_2 = 0;
340   WORD32 tmp;
341   WORD32 usac_independency_flag = pstr_sbr_header->usac_independency_flag;
342   WORD32 use_dflt_hdr = 0;
343   WORD32 header_present = 1;
344   WORD32 usac_flag = pstr_sbr_header->usac_flag;
345 
346   if (!usac_flag) {
347     memcpy(&prev_header_info, pstr_sbr_header,
348            sizeof(ia_sbr_header_data_struct));
349 
350     tmp = ixheaacd_read_bits_buf(
351         it_bit_buff, SBR_AMPLITUDE_RESOLUTION_BITS + SBR_BEGIN_SAMP_FREQ_BITS +
352                          SBR_END_SAMP_FREQ_BITS + SBR_CROSS_OVER_BND_BITS);
353 
354     pstr_sbr_header->amp_res = (WORD16)(
355         (tmp & 0x0800) >> (SBR_BEGIN_SAMP_FREQ_BITS + SBR_END_SAMP_FREQ_BITS +
356                            SBR_CROSS_OVER_BND_BITS));
357 
358     pstr_sbr_header->start_freq = (WORD16)(
359         (tmp & 0x0780) >> (SBR_END_SAMP_FREQ_BITS + SBR_CROSS_OVER_BND_BITS));
360 
361     pstr_sbr_header->stop_freq =
362         (WORD16)((tmp & 0x078) >> (SBR_CROSS_OVER_BND_BITS));
363 
364     pstr_sbr_header->xover_band = (WORD16)((tmp & 0x07));
365 
366     tmp = ixheaacd_read_bits_buf(
367         it_bit_buff,
368         SBR_HDR_RESERV_BITS + SBR_HDR_EXTR_1_BITS + SBR_HDR_EXTR_2_BITS);
369     header_extra_1 = (FLAG)((tmp & 0x02) >> (SBR_HDR_EXTR_2_BITS));
370     header_extra_2 = (FLAG)((tmp & 0x01));
371     if (stereo_flag) {
372       pstr_sbr_header->channel_mode = SBR_STEREO;
373     } else {
374       pstr_sbr_header->channel_mode = SBR_MONO;
375     }
376   } else {
377     WORD32 info_present = 0;
378     if (pstr_sbr_header->sync_state == SBR_ACTIVE) {
379       memcpy(&prev_header_info, pstr_sbr_header,
380              sizeof(ia_sbr_header_data_struct));
381     }
382     if (usac_independency_flag) {
383       header_present = 1;
384       info_present = 1;
385     } else {
386       info_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
387       if (info_present) {
388         header_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
389       } else {
390         header_present = 0;
391       }
392     }
393 
394     if (info_present) {
395       tmp = ixheaacd_read_bits_buf(it_bit_buff, SBR_AMPLITUDE_RESOLUTION_BITS +
396                                                     ESBR_CROSS_OVER_BND_BITS +
397                                                     ESBR_PRE_FLAT_BITS);
398       pstr_sbr_header->amp_res = (WORD16)(
399           (tmp & 0x0020) >> (ESBR_CROSS_OVER_BND_BITS + ESBR_PRE_FLAT_BITS));
400       pstr_sbr_header->xover_band =
401           (WORD16)((tmp & 0x001E) >> (ESBR_PRE_FLAT_BITS));
402       pstr_sbr_header->pre_proc_flag = (WORD16)((tmp & 0x001));
403       if (pstr_sbr_header->pvc_flag) {
404         pstr_sbr_header->pvc_mode =
405             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PVC_MODE_BITS);
406       } else {
407         pstr_sbr_header->pvc_mode = 0;
408       }
409     }
410 
411     if (header_present) {
412       use_dflt_hdr = ixheaacd_read_bits_buf(it_bit_buff, 1);
413       if (use_dflt_hdr) {
414         pstr_sbr_header->start_freq = pstr_sbr_dflt_header->start_freq;
415         pstr_sbr_header->stop_freq = pstr_sbr_dflt_header->stop_freq;
416         pstr_sbr_header->header_extra_1 = pstr_sbr_dflt_header->header_extra_1;
417         pstr_sbr_header->header_extra_2 = pstr_sbr_dflt_header->header_extra_2;
418         pstr_sbr_header->freq_scale = pstr_sbr_dflt_header->freq_scale;
419         pstr_sbr_header->alter_scale = pstr_sbr_dflt_header->alter_scale;
420         pstr_sbr_header->noise_bands = pstr_sbr_dflt_header->noise_bands;
421         pstr_sbr_header->limiter_bands = pstr_sbr_dflt_header->limiter_bands;
422         pstr_sbr_header->limiter_gains = pstr_sbr_dflt_header->limiter_gains;
423         pstr_sbr_header->interpol_freq = pstr_sbr_dflt_header->interpol_freq;
424         pstr_sbr_header->smoothing_mode = pstr_sbr_dflt_header->smoothing_mode;
425       } else {
426         tmp = ixheaacd_read_bits_buf(
427             it_bit_buff, SBR_BEGIN_SAMP_FREQ_BITS + SBR_END_SAMP_FREQ_BITS +
428                              SBR_HDR_EXTR_1_BITS + SBR_HDR_EXTR_2_BITS);
429         pstr_sbr_header->start_freq =
430             (tmp & 0x03C0) >> (SBR_END_SAMP_FREQ_BITS + SBR_HDR_EXTR_1_BITS +
431                                SBR_HDR_EXTR_2_BITS);
432         pstr_sbr_header->stop_freq =
433             (tmp & 0x003C) >> (SBR_HDR_EXTR_1_BITS + SBR_HDR_EXTR_2_BITS);
434         pstr_sbr_header->header_extra_1 =
435             (tmp & 0x0002) >> (SBR_HDR_EXTR_2_BITS);
436         pstr_sbr_header->header_extra_2 = (tmp & 0x0001);
437         header_extra_1 = pstr_sbr_header->header_extra_1;
438         header_extra_2 = pstr_sbr_header->header_extra_2;
439       }
440     }
441   }
442 
443   if (!use_dflt_hdr && header_present) {
444     if (header_extra_1) {
445       tmp = ixheaacd_read_bits_buf(
446           it_bit_buff,
447           SBR_SAMP_FREQ_LVL_BITS + SBR_CHANGE_LVL_BITS + SBR_NOISE_BND_BITS);
448       pstr_sbr_header->freq_scale =
449           (WORD16)((tmp & 0x018) >> (SBR_CHANGE_LVL_BITS + SBR_NOISE_BND_BITS));
450       pstr_sbr_header->alter_scale =
451           (WORD16)((tmp & 0x04) >> (SBR_NOISE_BND_BITS));
452       pstr_sbr_header->noise_bands = (WORD16)((tmp & 0x03));
453     } else {
454       pstr_sbr_header->freq_scale = SBR_SAMP_FEQ_LVL_DEF;
455       pstr_sbr_header->alter_scale = SBR_CHANGE_LVL_DEF;
456       pstr_sbr_header->noise_bands = SBR_NOISE_BND_DEF;
457     }
458 
459     if (header_extra_2) {
460       tmp = ixheaacd_read_bits_buf(
461           it_bit_buff, SBR_BND_LIMIT_BITS + SBR_GAIN_LIMIT_BITS +
462                            SBR_INTERPOL_SAMP_FREQ_BITS + SBR_SMOOTH_LEN_BITS);
463       pstr_sbr_header->limiter_bands = (WORD16)(
464           (tmp & 0x030) >> (SBR_GAIN_LIMIT_BITS + SBR_INTERPOL_SAMP_FREQ_BITS +
465                             SBR_SMOOTH_LEN_BITS));
466       pstr_sbr_header->limiter_gains = (WORD16)(
467           (tmp & 0x0c) >> (SBR_INTERPOL_SAMP_FREQ_BITS + SBR_SMOOTH_LEN_BITS));
468       pstr_sbr_header->interpol_freq =
469           (WORD16)((tmp & 0x02) >> (SBR_SMOOTH_LEN_BITS));
470       pstr_sbr_header->smoothing_mode = (WORD16)((tmp & 0x01));
471     } else {
472       pstr_sbr_header->limiter_bands = SBR_BND_LIMIT_DEF;
473       pstr_sbr_header->limiter_gains = SBR_GAIN_LIMIT_DEF;
474       pstr_sbr_header->interpol_freq = SBR_INTERPOL_SAMP_FEQ_DEF;
475       pstr_sbr_header->smoothing_mode = SBR_SMOOTH_LEN_DEF;
476     }
477   }
478 
479   if ((pstr_sbr_header->sync_state != SBR_ACTIVE) ||
480       (prev_header_info.start_freq != pstr_sbr_header->start_freq) ||
481       (prev_header_info.stop_freq != pstr_sbr_header->stop_freq) ||
482       (prev_header_info.xover_band != pstr_sbr_header->xover_band) ||
483       (prev_header_info.freq_scale != pstr_sbr_header->freq_scale) ||
484       (prev_header_info.alter_scale != pstr_sbr_header->alter_scale) ||
485       (prev_header_info.noise_bands != pstr_sbr_header->noise_bands)) {
486     return SBR_RESET;
487   }
488 
489   return 0;
490 }
491 
ixheaacd_sbr_sin_coding_data(ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct * ptr_frame_data,ia_bit_buf_struct * it_bit_buff)492 static VOID ixheaacd_sbr_sin_coding_data(
493     ia_sbr_header_data_struct *ptr_header_data,
494     ia_sbr_frame_info_data_struct *ptr_frame_data,
495     ia_bit_buf_struct *it_bit_buff) {
496   FLAG *p_add_harmonic = ptr_frame_data->add_harmonics;
497   WORD32 i;
498 
499   i = ptr_header_data->pstr_freq_band_data->num_sf_bands[HIGH];
500   do {
501     *p_add_harmonic++ =
502         (FLAG)ixheaacd_read_bits_buf(it_bit_buff, SBR_ADD_SINE_FLAG_BITS);
503     i--;
504   } while (i != 0);
505 
506   return;
507 }
508 
ixheaacd_validate_frame_info(ia_frame_info_struct * pstr_frame_info,WORD16 num_time_slots,WORD audio_object_type)509 static WORD16 ixheaacd_validate_frame_info(
510     ia_frame_info_struct *pstr_frame_info, WORD16 num_time_slots,
511     WORD audio_object_type) {
512   WORD32 i, j;
513 
514   WORD32 start_pos, end_pos, transient_env, start_pos_noise, end_pos_noise,
515       num_env_sf, num_noise_env;
516 
517   num_env_sf = pstr_frame_info->num_env;
518   num_noise_env = pstr_frame_info->num_noise_env;
519 
520   if ((num_env_sf < 1) || (num_env_sf > MAX_ENVELOPES)) return 0;
521 
522   if (num_noise_env > MAX_NOISE_ENVELOPES) return 0;
523 
524   start_pos = pstr_frame_info->border_vec[0];
525   end_pos = pstr_frame_info->border_vec[num_env_sf];
526   transient_env = pstr_frame_info->transient_env;
527 
528   if (transient_env > num_env_sf) return 0;
529 
530   start_pos_noise = pstr_frame_info->noise_border_vec[0];
531   end_pos_noise = pstr_frame_info->noise_border_vec[num_noise_env];
532 
533   if ((start_pos < 0) || (start_pos >= end_pos)) return 0;
534 
535   if (start_pos > SBR_OV_SLOTS) return 0;
536   if (audio_object_type != AOT_ER_AAC_ELD &&
537       audio_object_type != AOT_ER_AAC_LD) {
538     if (end_pos < SBR_TIME_SLOTS) return 0;
539   } else {
540     if (end_pos < num_time_slots) return 0;
541   }
542 
543   if (end_pos > add_d(SBR_TIME_SLOTS, SBR_OV_SLOTS)) return 0;
544 
545   for (i = 0; i < num_env_sf; i++) {
546     if (pstr_frame_info->border_vec[i] > pstr_frame_info->border_vec[i + 1])
547       return 0;
548   }
549 
550   if ((num_env_sf == 1) && (num_noise_env > 1)) return 0;
551 
552   if ((start_pos != start_pos_noise) || (end_pos != end_pos_noise)) return 0;
553 
554   for (i = 0; i < num_noise_env; i++) {
555     start_pos_noise = pstr_frame_info->noise_border_vec[i];
556 
557     for (j = 0; j < num_env_sf; j++) {
558       if (pstr_frame_info->border_vec[j] == start_pos_noise) break;
559     }
560     if (j == num_env_sf) return 0;
561   }
562 
563   return 1;
564 }
565 
ixheaacd_read_extn_data(ia_sbr_header_data_struct * ptr_header_data,ia_ps_dec_struct * ptr_ps_dec,ia_bit_buf_struct * it_bit_buff,ia_ps_tables_struct * ps_tables_ptr)566 static WORD16 ixheaacd_read_extn_data(
567     ia_sbr_header_data_struct *ptr_header_data, ia_ps_dec_struct *ptr_ps_dec,
568     ia_bit_buf_struct *it_bit_buff, ia_ps_tables_struct *ps_tables_ptr) {
569   WORD i;
570   WORD extended_data;
571   WORD no_bits_left;
572 
573   extended_data = ixheaacd_read_bits_buf(it_bit_buff, SBR_ENLARGED_DATA_BITS);
574 
575   if (extended_data) {
576     WORD cnt;
577     FLAG ps_read;
578 
579     ps_read = 0;
580 
581     cnt = ixheaacd_read_bits_buf(it_bit_buff, SBR_CONT_SIZE_BITS);
582 
583     if (cnt == ((1 << SBR_CONT_SIZE_BITS) - 1)) {
584       cnt = (cnt + ixheaacd_read_bits_buf(it_bit_buff, SBR_CONT_ESC_CNT_BITS));
585     }
586 
587     no_bits_left = (cnt << 3);
588 
589     while (no_bits_left > 7) {
590       WORD extension_id = ixheaacd_read_bits_buf(it_bit_buff, SBR_CONT_ID_BITS);
591 
592       no_bits_left = (no_bits_left - SBR_CONT_ID_BITS);
593 
594       switch (extension_id) {
595         case EXTENSION_ID_PS_CODING:
596 
597           if (ptr_ps_dec == NULL) {
598             return 0;
599           }
600 
601           if (!(ptr_ps_dec->force_mono || ps_read)) {
602             no_bits_left =
603                 (no_bits_left - ixheaacd_read_ps_data(ptr_ps_dec, it_bit_buff,
604                                                       (WORD16)no_bits_left,
605                                                       ps_tables_ptr));
606 
607             if (no_bits_left < 0) return 0;
608 
609             ptr_header_data->channel_mode = PS_STEREO;
610             ps_read = 1;
611             break;
612           }
613 
614         default:
615           cnt = (no_bits_left >> 3);
616           for (i = cnt - 1; i >= 0; i--) ixheaacd_read_bits_buf(it_bit_buff, 8);
617           no_bits_left = (no_bits_left - (cnt << 3));
618           break;
619       }
620     }
621 
622     if (no_bits_left < 0) return 0;
623 
624     ixheaacd_read_bits_buf(it_bit_buff, no_bits_left);
625   }
626   return 1;
627 }
628 
ixheaacd_sbr_read_pvc_sce(ia_sbr_frame_info_data_struct * ptr_frame_data,ia_bit_buf_struct * it_bit_buff,WORD32 hbe_flag,ia_pvc_data_struct * ptr_pvc_data,ia_sbr_tables_struct * ptr_sbr_tables,ia_sbr_header_data_struct * ptr_header_data)629 WORD32 ixheaacd_sbr_read_pvc_sce(ia_sbr_frame_info_data_struct *ptr_frame_data,
630                                  ia_bit_buf_struct *it_bit_buff,
631                                  WORD32 hbe_flag,
632                                  ia_pvc_data_struct *ptr_pvc_data,
633                                  ia_sbr_tables_struct *ptr_sbr_tables,
634                                  ia_sbr_header_data_struct *ptr_header_data) {
635   WORD32 i;
636   WORD32 err_code = 0;
637   ia_env_extr_tables_struct *env_extr_tables_ptr =
638       ptr_sbr_tables->env_extr_tables_ptr;
639   WORD32 usac_independency_flag = ptr_frame_data->usac_independency_flag;
640 
641   if (hbe_flag) {
642     ptr_frame_data->sbr_patching_mode =
643         ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
644 
645     if (ptr_frame_data->sbr_patching_mode == 0) {
646       ptr_frame_data->over_sampling_flag =
647           ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
648       if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
649         ptr_frame_data->pitch_in_bins =
650             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
651       else
652         ptr_frame_data->pitch_in_bins = 0;
653     } else {
654       ptr_frame_data->over_sampling_flag = ptr_frame_data->pitch_in_bins = 0;
655     }
656   }
657 
658   err_code = ixheaacd_pvc_time_freq_grid_info(it_bit_buff, ptr_frame_data);
659   if (err_code) return err_code;
660 
661   ptr_pvc_data->prev_sbr_mode = PVC_SBR;
662 
663   ixheaacd_pvc_env_dtdf_data(ptr_frame_data, it_bit_buff);
664 
665   for (i = 0; i < ptr_header_data->pstr_freq_band_data->num_nf_bands; i++) {
666     ptr_frame_data->sbr_invf_mode_prev[i] = ptr_frame_data->sbr_invf_mode[i];
667     ptr_frame_data->sbr_invf_mode[i] =
668         (WORD32)ixheaacd_read_bits_buf(it_bit_buff, ESBR_INVF_MODE_BITS);
669   }
670 
671   ptr_pvc_data->pvc_mode = ptr_header_data->pvc_mode;
672 
673   ixheaacd_read_esbr_pvc_envelope(ptr_pvc_data, it_bit_buff,
674                                   usac_independency_flag);
675 
676   ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data,
677                                      it_bit_buff, env_extr_tables_ptr);
678 
679   memset(ptr_frame_data->add_harmonics, 0,
680          ptr_header_data->pstr_freq_band_data->num_sf_bands[HIGH] *
681              sizeof(WORD32));
682   ptr_frame_data->pvc_mode = ptr_header_data->pvc_mode;
683 
684   ixheaacd_read_sbr_addi_data(ptr_frame_data, ptr_header_data, it_bit_buff);
685 
686   ptr_frame_data->coupling_mode = COUPLING_OFF;
687 
688   return 0;
689 }
690 
ixheaacd_sbr_read_sce(ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct * ptr_frame_data,ia_ps_dec_struct * ptr_ps_dec,ia_bit_buf_struct * it_bit_buff,ia_sbr_tables_struct * ptr_sbr_tables,WORD audio_object_type)691 WORD8 ixheaacd_sbr_read_sce(ia_sbr_header_data_struct *ptr_header_data,
692                             ia_sbr_frame_info_data_struct *ptr_frame_data,
693                             ia_ps_dec_struct *ptr_ps_dec,
694                             ia_bit_buf_struct *it_bit_buff,
695                             ia_sbr_tables_struct *ptr_sbr_tables,
696                             WORD audio_object_type) {
697   WORD32 bit;
698   WORD32 i;
699   WORD32 hbe_flag = ptr_header_data->hbe_flag;
700   WORD32 num_if_bands = ptr_header_data->pstr_freq_band_data->num_if_bands;
701   WORD32 usac_flag = ptr_header_data->usac_flag;
702   ia_env_extr_tables_struct *env_extr_tables_ptr =
703       ptr_sbr_tables->env_extr_tables_ptr;
704 
705   ptr_frame_data->coupling_mode = COUPLING_OFF;
706 
707   if (!usac_flag) {
708     bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
709 
710     if (bit) ixheaacd_read_bits_buf(it_bit_buff, SBR_SCE_RESERV_BITS);
711     if (audio_object_type == AOT_ER_AAC_ELD ||
712         audio_object_type == AOT_ER_AAC_LD) {
713       if (ptr_frame_data->eld_sbr_flag == 1) {
714         if (!ixheaacd_extract_frame_info_ld(it_bit_buff, ptr_frame_data))
715           return 0;
716       }
717     } else {
718       if (!ixheaacd_sbr_time_freq_grid_info(it_bit_buff, ptr_frame_data,
719                                             env_extr_tables_ptr))
720 
721         return 0;
722     }
723     if (!ixheaacd_validate_frame_info(&ptr_frame_data->str_frame_info_details,
724                                       ptr_header_data->num_time_slots,
725                                       audio_object_type))
726       return 0;
727 
728   } else {
729     if (hbe_flag) {
730       ptr_frame_data->sbr_patching_mode =
731           ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
732       if (ptr_frame_data->sbr_patching_mode == 0) {
733         ptr_frame_data->over_sampling_flag =
734             ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
735         if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
736           ptr_frame_data->pitch_in_bins =
737               ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
738         else
739           ptr_frame_data->pitch_in_bins = 0;
740       } else {
741         ptr_frame_data->over_sampling_flag = ptr_frame_data->pitch_in_bins = 0;
742       }
743     }
744     ptr_frame_data->num_time_slots = ptr_header_data->num_time_slots;
745     if (!ixheaacd_sbr_time_freq_grid_info(it_bit_buff, ptr_frame_data,
746                                           env_extr_tables_ptr))
747       return 0;
748 
749     if (!ixheaacd_validate_frame_info(&ptr_frame_data->str_frame_info_details,
750                                       ptr_header_data->num_time_slots,
751                                       audio_object_type))
752       return 0;
753 
754     ptr_frame_data->prev_sbr_mode = ORIG_SBR;
755   }
756 
757   ixheaacd_sbr_env_dtdf_data(ptr_frame_data, it_bit_buff,
758                              ptr_header_data->usac_flag);
759 
760   if (ptr_frame_data->del_cod_dir_arr[0] == DTDF_DIR_FREQ) {
761     ptr_header_data->err_flag = 0;
762   }
763 
764   for (i = 0; i < num_if_bands; i++) {
765     ptr_frame_data->sbr_invf_mode_prev[i] = ptr_frame_data->sbr_invf_mode[i];
766     ptr_frame_data->sbr_invf_mode[i] =
767         (WORD32)ixheaacd_read_bits_buf(it_bit_buff, SBR_INVERSE_FILT_MODE_BITS);
768   }
769 
770   if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data, it_bit_buff,
771                                   env_extr_tables_ptr, audio_object_type))
772     return 0;
773 
774   ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data,
775                                      it_bit_buff, env_extr_tables_ptr);
776 
777   if (usac_flag) {
778     memset(
779         ptr_frame_data->add_harmonics, 0,
780         ptr_header_data->pstr_freq_band_data->num_sf_bands[1] * sizeof(WORD32));
781     ptr_frame_data->coupling_mode = COUPLING_OFF;
782   }
783 
784   bit = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
785   if (bit) {
786     ixheaacd_sbr_sin_coding_data(ptr_header_data, ptr_frame_data, it_bit_buff);
787   } else {
788     memset(ptr_frame_data->add_harmonics, 0, sizeof(FLAG) * MAX_FREQ_COEFFS);
789   }
790 
791   if (!usac_flag) {
792     ixheaacd_read_extn_data(ptr_header_data, ptr_ps_dec, it_bit_buff,
793                             ptr_sbr_tables->ps_tables_ptr);
794   }
795 
796   return 1;
797 }
798 
ixheaacd_sbr_read_cpe(ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct ** ptr_frame_data,ia_bit_buf_struct * it_bit_buff,ia_sbr_tables_struct * ptr_sbr_tables,WORD audio_object_type)799 WORD8 ixheaacd_sbr_read_cpe(ia_sbr_header_data_struct *ptr_header_data,
800                             ia_sbr_frame_info_data_struct **ptr_frame_data,
801                             ia_bit_buf_struct *it_bit_buff,
802                             ia_sbr_tables_struct *ptr_sbr_tables,
803                             WORD audio_object_type) {
804   WORD32 i, k, bit, num_ch = 2;
805   WORD32 num_if_bands = ptr_header_data->pstr_freq_band_data->num_if_bands;
806   WORD32 hbe_flag = ptr_header_data->hbe_flag;
807   WORD32 usac_flag = ptr_header_data->usac_flag;
808 
809   ia_env_extr_tables_struct *env_extr_tables_ptr =
810       ptr_sbr_tables->env_extr_tables_ptr;
811   bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
812 
813   if (usac_flag) {
814     if (bit) {
815       if (hbe_flag) {
816         ptr_frame_data[0]->sbr_patching_mode =
817             ptr_frame_data[1]->sbr_patching_mode =
818                 ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
819         if (ptr_frame_data[0]->sbr_patching_mode == 0) {
820           ptr_frame_data[0]->over_sampling_flag =
821               ptr_frame_data[1]->over_sampling_flag = ixheaacd_read_bits_buf(
822                   it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
823           if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
824             ptr_frame_data[0]->pitch_in_bins =
825                 ptr_frame_data[1]->pitch_in_bins =
826                     ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
827           else
828             ptr_frame_data[0]->pitch_in_bins =
829                 ptr_frame_data[1]->pitch_in_bins = 0;
830         } else {
831           ptr_frame_data[0]->over_sampling_flag = 0;
832           ptr_frame_data[1]->over_sampling_flag = 0;
833           ptr_frame_data[0]->pitch_in_bins = 0;
834           ptr_frame_data[1]->pitch_in_bins = 0;
835         }
836       }
837       ptr_frame_data[0]->coupling_mode = COUPLING_LEVEL;
838       ptr_frame_data[1]->coupling_mode = COUPLING_BAL;
839     } else {
840       if (hbe_flag) {
841         ptr_frame_data[0]->sbr_patching_mode =
842             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
843         if (ptr_frame_data[0]->sbr_patching_mode == 0) {
844           ptr_frame_data[0]->over_sampling_flag =
845               ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
846           if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
847             ptr_frame_data[0]->pitch_in_bins =
848                 ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
849           else
850             ptr_frame_data[0]->pitch_in_bins = 0;
851         } else {
852           ptr_frame_data[0]->over_sampling_flag = 0;
853           ptr_frame_data[0]->pitch_in_bins = 0;
854         }
855         ptr_frame_data[1]->sbr_patching_mode =
856             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
857         if (ptr_frame_data[1]->sbr_patching_mode == 0) {
858           ptr_frame_data[1]->over_sampling_flag =
859               ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
860           if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
861             ptr_frame_data[1]->pitch_in_bins =
862                 ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
863           else
864             ptr_frame_data[1]->pitch_in_bins = 0;
865         } else {
866           ptr_frame_data[1]->over_sampling_flag =
867               ptr_frame_data[1]->pitch_in_bins = 0;
868         }
869       }
870 
871       ptr_frame_data[0]->coupling_mode = COUPLING_OFF;
872       ptr_frame_data[1]->coupling_mode = COUPLING_OFF;
873     }
874   } else {
875     if (bit) {
876       ixheaacd_read_bits_buf(it_bit_buff,
877                              SBR_SCE_RESERV_BITS + SBR_SCE_RESERV_BITS);
878     }
879     if ((audio_object_type != AOT_ER_AAC_ELD) &&
880         (ptr_header_data->channel_mode != SBR_STEREO)) {
881       ptr_header_data->sync_state = UPSAMPLING;
882       return 0;
883     }
884 
885     bit = ixheaacd_read_bits_buf(it_bit_buff, SBR_COUPLNG_MODE_BITS);
886 
887     if (bit) {
888       ptr_frame_data[0]->coupling_mode = COUPLING_LEVEL;
889       ptr_frame_data[1]->coupling_mode = COUPLING_BAL;
890     } else {
891       ptr_frame_data[0]->coupling_mode = COUPLING_OFF;
892       ptr_frame_data[1]->coupling_mode = COUPLING_OFF;
893     }
894   }
895 
896   for (i = 0; i < num_ch; i++) {
897     ptr_frame_data[i]->num_time_slots = ptr_header_data->num_time_slots;
898     if (audio_object_type == AOT_ER_AAC_ELD ||
899         audio_object_type == AOT_ER_AAC_LD) {
900       if (ptr_frame_data[i]->eld_sbr_flag == 1) {
901         if (!ixheaacd_extract_frame_info_ld(it_bit_buff, ptr_frame_data[i]))
902           return 0;
903       }
904     } else {
905       if (!ixheaacd_sbr_time_freq_grid_info(it_bit_buff, ptr_frame_data[i],
906                                             env_extr_tables_ptr))
907         return 0;
908     }
909 
910     if (!ixheaacd_validate_frame_info(
911             &ptr_frame_data[i]->str_frame_info_details,
912             ptr_header_data->num_time_slots, audio_object_type))
913       return 0;
914 
915     if (ptr_frame_data[0]->coupling_mode) {
916       memcpy(&ptr_frame_data[1]->str_frame_info_details,
917              &ptr_frame_data[0]->str_frame_info_details,
918              sizeof(ia_frame_info_struct));
919       if (audio_object_type == AOT_ER_AAC_ELD ||
920           audio_object_type == AOT_ER_AAC_LD) {
921         ptr_frame_data[1]->amp_res = ptr_frame_data[0]->amp_res;
922       }
923       num_ch = 1;
924     }
925   }
926 
927   if (ptr_frame_data[0]->coupling_mode && usac_flag) {
928     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[0], it_bit_buff,
929                                ptr_header_data->usac_flag);
930     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[1], it_bit_buff,
931                                ptr_header_data->usac_flag);
932 
933     for (i = 0; i < ptr_header_data->pstr_freq_band_data->num_if_bands; i++) {
934       ptr_frame_data[0]->sbr_invf_mode_prev[i] =
935           ptr_frame_data[0]->sbr_invf_mode[i];
936       ptr_frame_data[1]->sbr_invf_mode_prev[i] =
937           ptr_frame_data[1]->sbr_invf_mode[i];
938 
939       ptr_frame_data[0]->sbr_invf_mode[i] =
940           (WORD32)ixheaacd_read_bits_buf(it_bit_buff, ESBR_INVF_MODE_BITS);
941       ptr_frame_data[1]->sbr_invf_mode[i] = ptr_frame_data[0]->sbr_invf_mode[i];
942     }
943 
944     ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[0], it_bit_buff,
945                                env_extr_tables_ptr, audio_object_type);
946     ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[0],
947                                        it_bit_buff, env_extr_tables_ptr);
948 
949     ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[1], it_bit_buff,
950                                env_extr_tables_ptr, audio_object_type);
951     ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[1],
952                                        it_bit_buff, env_extr_tables_ptr);
953 
954     memset(
955         ptr_frame_data[0]->add_harmonics, 0,
956         ptr_header_data->pstr_freq_band_data->num_sf_bands[1] * sizeof(WORD32));
957     memset(
958         ptr_frame_data[1]->add_harmonics, 0,
959         ptr_header_data->pstr_freq_band_data->num_sf_bands[1] * sizeof(WORD32));
960 
961   } else {
962     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[0], it_bit_buff,
963                                ptr_header_data->usac_flag);
964     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[1], it_bit_buff,
965                                ptr_header_data->usac_flag);
966 
967     if ((ptr_frame_data[0]->del_cod_dir_arr[0] == DTDF_DIR_FREQ) &&
968         (ptr_frame_data[1]->del_cod_dir_arr[0] == DTDF_DIR_FREQ)) {
969       ptr_header_data->err_flag = 0;
970     }
971 
972     for (k = 0; k < num_ch; k++) {
973       for (i = 0; i < num_if_bands; i++) {
974         ptr_frame_data[k]->sbr_invf_mode_prev[i] =
975             ptr_frame_data[k]->sbr_invf_mode[i];
976         ptr_frame_data[k]->sbr_invf_mode[i] = (WORD32)ixheaacd_read_bits_buf(
977             it_bit_buff, SBR_INVERSE_FILT_MODE_BITS);
978       }
979     }
980 
981     if (ptr_frame_data[0]->coupling_mode) {
982       memcpy(ptr_frame_data[1]->sbr_invf_mode, ptr_frame_data[0]->sbr_invf_mode,
983              sizeof(WORD32) * num_if_bands);
984 
985       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[0],
986                                       it_bit_buff, env_extr_tables_ptr,
987                                       audio_object_type)) {
988         return 0;
989       }
990 
991       ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[0],
992                                          it_bit_buff, env_extr_tables_ptr);
993 
994       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[1],
995                                       it_bit_buff, env_extr_tables_ptr,
996                                       audio_object_type)) {
997         return 0;
998       }
999     } else {
1000       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[0],
1001                                       it_bit_buff, env_extr_tables_ptr,
1002                                       audio_object_type))
1003         return 0;
1004 
1005       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[1],
1006                                       it_bit_buff, env_extr_tables_ptr,
1007                                       audio_object_type))
1008         return 0;
1009 
1010       ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[0],
1011                                          it_bit_buff, env_extr_tables_ptr);
1012     }
1013     ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[1],
1014                                        it_bit_buff, env_extr_tables_ptr);
1015   }
1016 
1017   bit = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
1018   if (bit) {
1019     ixheaacd_sbr_sin_coding_data(ptr_header_data, ptr_frame_data[0],
1020                                  it_bit_buff);
1021   } else {
1022     memset(ptr_frame_data[0]->add_harmonics, 0, sizeof(FLAG) * MAX_FREQ_COEFFS);
1023   }
1024 
1025   bit = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
1026   if (bit) {
1027     ixheaacd_sbr_sin_coding_data(ptr_header_data, ptr_frame_data[1],
1028                                  it_bit_buff);
1029   } else {
1030     memset(ptr_frame_data[1]->add_harmonics, 0, sizeof(FLAG) * MAX_FREQ_COEFFS);
1031   }
1032 
1033   if (!usac_flag) {
1034     ixheaacd_read_extn_data(ptr_header_data, NULL, it_bit_buff,
1035                             ptr_sbr_tables->ps_tables_ptr);
1036   }
1037   return 1;
1038 }
1039 
ixheaacd_sbr_env_dtdf_data(ia_sbr_frame_info_data_struct * ptr_frame_data,ia_bit_buf_struct * it_bit_buff,WORD32 usac_flag)1040 VOID ixheaacd_sbr_env_dtdf_data(ia_sbr_frame_info_data_struct *ptr_frame_data,
1041                                 ia_bit_buf_struct *it_bit_buff,
1042                                 WORD32 usac_flag) {
1043   WORD32 i;
1044   WORD32 num_env = ptr_frame_data->str_frame_info_details.num_env;
1045   WORD32 num_noise_env = ptr_frame_data->str_frame_info_details.num_noise_env;
1046   WORD16 *p_coding_dir_vec = ptr_frame_data->del_cod_dir_arr;
1047   WORD16 *p_coding_dir_noise_vec = ptr_frame_data->del_cod_dir_noise_arr;
1048   WORD32 usac_independency_flag = ptr_frame_data->usac_independency_flag;
1049 
1050   if (usac_flag) {
1051     if (usac_independency_flag) {
1052       *p_coding_dir_vec = 0;
1053       p_coding_dir_vec++;
1054     } else {
1055       *p_coding_dir_vec =
1056           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1057       p_coding_dir_vec++;
1058     }
1059     for (i = num_env - 1; i >= 1; i--) {
1060       *p_coding_dir_vec++ =
1061           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1062     }
1063     if (usac_independency_flag) {
1064       *p_coding_dir_noise_vec = 0;
1065       p_coding_dir_noise_vec++;
1066     } else {
1067       *p_coding_dir_noise_vec =
1068           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1069       p_coding_dir_noise_vec++;
1070     }
1071     for (i = num_noise_env - 1; i >= 1; i--) {
1072       *p_coding_dir_noise_vec++ =
1073           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1074     }
1075   } else {
1076     for (i = num_env - 1; i >= 0; i--) {
1077       *p_coding_dir_vec++ =
1078           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1079     }
1080 
1081     for (i = num_noise_env - 1; i >= 0; i--) {
1082       *p_coding_dir_noise_vec++ =
1083           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1084     }
1085   }
1086 }
1087 
ixheaacd_read_env_data(ia_sbr_frame_info_data_struct * ptr_frame_data,ia_bit_buf_struct * it_bit_buff,ia_huffman_data_type hcb_t,ia_huffman_data_type hcb_f,WORD32 * idx_t,WORD32 * idx_f,WORD16 * no_band,WORD32 num_env,WORD32 env_data_tbl_comp_factor,WORD32 start_bits,WORD32 start_bits_balance,WORD32 num_noise_env,WORD32 lav,WORD32 usac_flag)1088 VOID ixheaacd_read_env_data(ia_sbr_frame_info_data_struct *ptr_frame_data,
1089                             ia_bit_buf_struct *it_bit_buff,
1090                             ia_huffman_data_type hcb_t,
1091                             ia_huffman_data_type hcb_f, WORD32 *idx_t,
1092                             WORD32 *idx_f, WORD16 *no_band, WORD32 num_env,
1093                             WORD32 env_data_tbl_comp_factor, WORD32 start_bits,
1094                             WORD32 start_bits_balance, WORD32 num_noise_env,
1095                             WORD32 lav, WORD32 usac_flag) {
1096   WORD32 j, i, ixheaacd_drc_offset = 0,
1097                coupling_mode = ptr_frame_data->coupling_mode, delta, bits,
1098                shift;
1099   WORD16 *p_coding_dir_vec, *p_sbr_sf;
1100   WORD16 index, length;
1101   WORD32 readword;
1102   FLOAT32 *p_sbr_sf_float;
1103 
1104   if (num_noise_env) {
1105     p_coding_dir_vec = ptr_frame_data->del_cod_dir_noise_arr;
1106     p_sbr_sf = ptr_frame_data->int_noise_floor;
1107     p_sbr_sf_float = ptr_frame_data->flt_noise_floor;
1108   } else {
1109     p_coding_dir_vec = ptr_frame_data->del_cod_dir_arr;
1110     p_sbr_sf = ptr_frame_data->int_env_sf_arr;
1111     p_sbr_sf_float = ptr_frame_data->flt_env_sf_arr;
1112   }
1113 
1114   if (coupling_mode == COUPLING_BAL) {
1115     bits = start_bits_balance;
1116     shift = env_data_tbl_comp_factor;
1117 
1118   } else {
1119     bits = start_bits;
1120     shift = 0;
1121   }
1122 
1123   for (j = 0; j < num_env; j++) {
1124     ia_huffman_data_type h;
1125     const WORD32 *idx_tab;
1126     WORD32 dtdf_dir_flag = p_coding_dir_vec[j];
1127 
1128     if (dtdf_dir_flag == DTDF_DIR_FREQ) {
1129       p_sbr_sf[ixheaacd_drc_offset] =
1130           (WORD16)(ixheaacd_read_bits_buf(it_bit_buff, bits) << shift);
1131       p_sbr_sf_float[ixheaacd_drc_offset] = p_sbr_sf[ixheaacd_drc_offset];
1132       h = hcb_f;
1133       idx_tab = idx_f;
1134     } else {
1135       h = hcb_t;
1136       idx_tab = idx_t;
1137     }
1138 
1139     for (i = (1 - dtdf_dir_flag); i < no_band[j]; i++) {
1140       if (it_bit_buff->cnt_bits < 20) {
1141         readword = ixheaacd_show_bits_buf(it_bit_buff, it_bit_buff->cnt_bits);
1142         readword = readword << (32 - it_bit_buff->cnt_bits);
1143       } else {
1144         readword = ixheaacd_show_bits_buf(it_bit_buff, 20);
1145         readword = readword << 12;
1146       }
1147       ixheaacd_huffman_decode(readword, &index, &length, (const UWORD16 *)h,
1148                               (const UWORD32 *)idx_tab);
1149       delta = index - lav;
1150       ixheaacd_read_bits_buf(it_bit_buff, length);
1151       p_sbr_sf[ixheaacd_drc_offset + i] =
1152           (WORD16)(delta << env_data_tbl_comp_factor);
1153       p_sbr_sf_float[ixheaacd_drc_offset + i] =
1154           p_sbr_sf[ixheaacd_drc_offset + i];
1155     }
1156     if (usac_flag && (num_noise_env == 0)) {
1157       ptr_frame_data->inter_temp_shape_mode[j] = 0;
1158       if (ptr_frame_data->inter_tes_flag) {
1159         WORD32 flag = (WORD32)ixheaacd_read_bits_buf(it_bit_buff, 1);
1160         if (flag) {
1161           ptr_frame_data->inter_temp_shape_mode[j] =
1162               (WORD32)ixheaacd_read_bits_buf(it_bit_buff, 2);
1163         }
1164       }
1165     }
1166     ixheaacd_drc_offset += (no_band[j]);
1167   }
1168 }
1169 
ixheaacd_read_sbr_noise_floor_data(ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct * ptr_frame_data,ia_bit_buf_struct * it_bit_buff,ia_env_extr_tables_struct * env_extr_tables_ptr)1170 VOID ixheaacd_read_sbr_noise_floor_data(
1171     ia_sbr_header_data_struct *ptr_header_data,
1172     ia_sbr_frame_info_data_struct *ptr_frame_data,
1173     ia_bit_buf_struct *it_bit_buff,
1174     ia_env_extr_tables_struct *env_extr_tables_ptr) {
1175   WORD32 i;
1176   WORD32 coupling_mode;
1177   WORD16 num_noise_bands[MAX_NOISE_ENVELOPES];
1178   ia_huffman_data_type hcb_noise_env;
1179   ia_huffman_data_type hcb_noise;
1180   WORD32 *idx_noise_env;
1181   WORD32 *idx_noise;
1182   WORD32 lav;
1183   WORD32 env_data_tbl_comp_factor;
1184 
1185   WORD32 start_bits;
1186   WORD32 start_bits_balance;
1187   WORD32 num_noise_env = ptr_frame_data->str_frame_info_details.num_noise_env;
1188 
1189   for (i = 0; i < num_noise_env; i++)
1190     num_noise_bands[i] = ptr_header_data->pstr_freq_band_data->num_nf_bands;
1191 
1192   start_bits = SBR_BEGIN_NOISE_BITS_AMPLITUDE_RESOLUTION_3_0;
1193   start_bits_balance = SBR_BEGIN_NOISE_BITS_BALNCE_AMPLITUDE_RESOLUTION_3_0;
1194 
1195   coupling_mode = ptr_frame_data->coupling_mode;
1196 
1197   if (coupling_mode == COUPLING_BAL) {
1198     lav = 12;
1199     hcb_noise = (ia_huffman_data_type)&env_extr_tables_ptr
1200                     ->ixheaacd_t_huffman_noise_bal_3_0db_inp_table;
1201     idx_noise =
1202         env_extr_tables_ptr->ixheaacd_t_huffman_noise_bal_3_0db_idx_table;
1203     hcb_noise_env = (ia_huffman_data_type)&env_extr_tables_ptr
1204                         ->ixheaacd_f_huffman_env_bal_3_0db_inp_table;
1205     idx_noise_env =
1206         env_extr_tables_ptr->ixheaacd_f_huffman_env_bal_3_0db_idx_table;
1207     env_data_tbl_comp_factor = 1;
1208   } else {
1209     lav = 31;
1210     hcb_noise = (ia_huffman_data_type)&env_extr_tables_ptr
1211                     ->ixheaacd_t_huffman_noise_3_0db_inp_table;
1212     idx_noise = env_extr_tables_ptr->ixheaacd_t_huffman_noise_3_0db_idx_table;
1213     hcb_noise_env = (ia_huffman_data_type)&env_extr_tables_ptr
1214                         ->ixheaacd_f_huffman_env_3_0db_inp_table;
1215     idx_noise_env = env_extr_tables_ptr->ixheaacd_f_huffman_env_3_0db_idx_table;
1216     env_data_tbl_comp_factor = 0;
1217   }
1218 
1219   ixheaacd_read_env_data(ptr_frame_data, it_bit_buff, hcb_noise, hcb_noise_env,
1220                          idx_noise, idx_noise_env, &num_noise_bands[0],
1221                          num_noise_env, env_data_tbl_comp_factor, start_bits,
1222                          start_bits_balance, 1, lav,
1223                          ptr_header_data->usac_flag);
1224 }
1225 
ixheaacd_read_sbr_env_data(ia_sbr_header_data_struct * ptr_header_data,ia_sbr_frame_info_data_struct * ptr_frame_data,ia_bit_buf_struct * it_bit_buff,ia_env_extr_tables_struct * env_extr_tables_ptr,WORD audio_object_type)1226 WORD16 ixheaacd_read_sbr_env_data(
1227     ia_sbr_header_data_struct *ptr_header_data,
1228     ia_sbr_frame_info_data_struct *ptr_frame_data,
1229     ia_bit_buf_struct *it_bit_buff,
1230     ia_env_extr_tables_struct *env_extr_tables_ptr, WORD audio_object_type) {
1231   WORD32 coupling_mode = ptr_frame_data->coupling_mode;
1232   WORD32 *idx_t, *idx_f;
1233   WORD32 lav;
1234   WORD32 i;
1235   WORD16 no_band[MAX_ENVELOPES];
1236   WORD32 delta;
1237   WORD32 amp_res, num_env, env_data_tbl_comp_factor, start_bits,
1238       start_bits_balance;
1239   WORD16 *p_freq_res = ptr_frame_data->str_frame_info_details.freq_res;
1240   WORD16 *p_num_sf_bands = ptr_header_data->pstr_freq_band_data->num_sf_bands;
1241   ia_huffman_data_type hcb_t, hcb_f;
1242 
1243   delta = 0;
1244   amp_res = ptr_header_data->amp_res;
1245   num_env = ptr_frame_data->str_frame_info_details.num_env;
1246 
1247   ptr_frame_data->num_env_sfac = 0;
1248 
1249   if ((ptr_frame_data->str_frame_info_details.frame_class == FIXFIX) &&
1250       (num_env == 1)) {
1251     if (audio_object_type != AOT_ER_AAC_ELD &&
1252         audio_object_type != AOT_ER_AAC_LD) {
1253       amp_res = SBR_AMPLITUDE_RESOLUTION_1_5;
1254     } else {
1255       amp_res = ptr_frame_data->amp_res;
1256     }
1257   }
1258   ptr_frame_data->amp_res = amp_res;
1259 
1260   if (amp_res == SBR_AMPLITUDE_RESOLUTION_3_0) {
1261     start_bits = SBR_BEGIN_ENVN_BITS_AMPLITUDE_RESOLUTION_3_0;
1262     start_bits_balance = SBR_BEGIN_ENVN_BITS_BALNCE_AMPLITUDE_RESOLUTION_3_0;
1263   } else {
1264     start_bits = SBR_BEGIN_ENVN_BITS_AMPLITUDE_RESOLUTION_1_5;
1265     start_bits_balance = SBR_BEGIN_ENVN_BITS_BALNCE_AMPLITUDE_RESOLUTION_1_5;
1266   }
1267 
1268   for (i = 0; i < num_env; i++) {
1269     no_band[i] = p_num_sf_bands[*p_freq_res++];
1270     ptr_frame_data->num_env_sfac =
1271         ixheaacd_add16(ptr_frame_data->num_env_sfac, no_band[i]);
1272   }
1273 
1274   if (ptr_frame_data->num_env_sfac > MAX_NUM_ENVELOPE_VALUES) return 0;
1275 
1276   if (coupling_mode == COUPLING_BAL) {
1277     env_data_tbl_comp_factor = 1;
1278 
1279     if (amp_res == SBR_AMPLITUDE_RESOLUTION_1_5) {
1280       lav = 24;
1281       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
1282                   ->ixheaacd_t_huffman_env_bal_1_5db_inp_table;
1283       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_bal_1_5db_idx_table;
1284       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
1285                   ->ixheaacd_f_huffman_env_bal_1_5db_inp_table;
1286       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_bal_1_5db_idx_table;
1287     } else {
1288       lav = 12;
1289       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
1290                   ->ixheaacd_t_huffman_env_bal_3_0db_inp_table;
1291       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_bal_3_0db_idx_table;
1292       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
1293                   ->ixheaacd_f_huffman_env_bal_3_0db_inp_table;
1294       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_bal_3_0db_idx_table;
1295     }
1296   } else {
1297     env_data_tbl_comp_factor = 0;
1298 
1299     if (amp_res == SBR_AMPLITUDE_RESOLUTION_1_5) {
1300       lav = 60;
1301       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
1302                   ->ixheaacd_t_huffman_env_1_5db_inp_table;
1303       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_1_5db_idx_table;
1304       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
1305                   ->ixheaacd_f_huffman_env_1_5db_inp_table;
1306       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_1_5db_idx_table;
1307     } else {
1308       lav = 31;
1309       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
1310                   ->ixheaacd_t_huffman_env_3_0db_inp_table;
1311       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_3_0db_idx_table;
1312       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
1313                   ->ixheaacd_f_huffman_env_3_0db_inp_table;
1314       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_3_0db_idx_table;
1315     }
1316   }
1317 
1318   ixheaacd_read_env_data(ptr_frame_data, it_bit_buff, hcb_t, hcb_f, idx_t,
1319                          idx_f, &no_band[0], num_env, env_data_tbl_comp_factor,
1320                          start_bits, start_bits_balance, 0, lav,
1321                          ptr_header_data->usac_flag);
1322 
1323   return 1;
1324 }
1325 
ixheaacd_extract_frame_info_ld(ia_bit_buf_struct * it_bit_buff,ia_sbr_frame_info_data_struct * h_frame_data)1326 int ixheaacd_extract_frame_info_ld(
1327     ia_bit_buf_struct *it_bit_buff,
1328     ia_sbr_frame_info_data_struct *h_frame_data) {
1329   int abs_bord_lead = 0, num_rel_lead = 0, num_rel_trail = 0, bs_num_env = 0,
1330       frame_class, temp, env, k, abs_bord_trail = 0, middle_bord = 0,
1331       bs_num_noise, transient_env_temp = 0, bs_transient_position = 0;
1332 
1333   WORD16 time_border[MAX_ENVELOPES + 1];
1334   WORD16 time_border_noise[2 + 1];
1335   WORD16 f[MAX_ENVELOPES + 1];
1336   int rel_bord_lead[7] = {0};
1337 
1338   ia_frame_info_struct *v_frame_info = &h_frame_data->str_frame_info_details;
1339 
1340   int numTimeSlots = h_frame_data->num_time_slots;
1341 
1342   v_frame_info->frame_class = frame_class =
1343       ixheaacd_read_bits_buf(it_bit_buff, SBRLD_CLA_BITS);
1344 
1345   switch (frame_class) {
1346     case FIXFIX:
1347       temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_ENV_BITS);
1348       bs_num_env = 1 << temp;
1349 
1350       if (bs_num_env == 1)
1351         h_frame_data->amp_res =
1352             ixheaacd_read_bits_buf(it_bit_buff, SBR_AMPLITUDE_RESOLUTION_BITS);
1353 
1354       f[0] = ixheaacd_read_bits_buf(it_bit_buff, SBR_RES_BITS);
1355 
1356       for (env = 1; env < bs_num_env; env++) f[env] = f[0];
1357       break;
1358     case LD_TRAN:
1359       bs_transient_position =
1360           ixheaacd_read_bits_buf(it_bit_buff, SBR_TRAN_BITS);
1361       v_frame_info->frame_class = 0;
1362       if ((numTimeSlots != 16) && (bs_transient_position >= LD_ENV_TBL_480)) {
1363         return -1;
1364       }
1365       bs_num_env = (numTimeSlots == 16)
1366                        ? ixheaacd_ld_env_table_512[bs_transient_position]
1367                                                   [SBR_ENVT_NUMENV]
1368                        : ixheaacd_ld_env_table_480[bs_transient_position]
1369                                                   [SBR_ENVT_NUMENV];
1370       for (env = 0; env < bs_num_env; env++)
1371         f[env] = ixheaacd_read_bits_buf(it_bit_buff, SBR_RES_BITS);
1372       break;
1373   }
1374 
1375   switch (frame_class) {
1376     case FIXFIX:
1377       abs_bord_lead = 0;
1378       abs_bord_trail = numTimeSlots;
1379       num_rel_lead = bs_num_env - 1;
1380       num_rel_trail = 0;
1381 
1382       for (k = 0; k < num_rel_lead; k++) {
1383         rel_bord_lead[k] = ixheaacd_ld_env_table_time_slot[num_rel_lead - 1];
1384       }
1385 
1386       time_border[0] = abs_bord_lead;
1387       time_border[bs_num_env] = abs_bord_trail;
1388       for (env = 1; env <= num_rel_lead; env++) {
1389         time_border[env] = abs_bord_lead;
1390         for (k = 0; k <= env - 1; k++) time_border[env] += rel_bord_lead[k];
1391       }
1392       break;
1393 
1394     case LD_TRAN:
1395       time_border[0] = 0;
1396       time_border[bs_num_env] = numTimeSlots;
1397       for (k = 1; k < bs_num_env; k++)
1398         time_border[k] =
1399             (numTimeSlots == 16)
1400                 ? ixheaacd_ld_env_table_512[bs_transient_position][k]
1401                 : ixheaacd_ld_env_table_480[bs_transient_position][k];
1402       break;
1403 
1404     default:
1405       time_border[0] = 0;
1406 
1407       break;
1408   };
1409 
1410   switch (frame_class) {
1411     case FIXFIX:
1412       middle_bord = bs_num_env / 2;
1413       break;
1414     case LD_TRAN:
1415       middle_bord = 1;
1416       break;
1417   };
1418 
1419   time_border_noise[0] = time_border[0];
1420   if (bs_num_env > 1) {
1421     time_border_noise[1] = time_border[middle_bord];
1422     time_border_noise[2] = time_border[bs_num_env];
1423     bs_num_noise = 2;
1424   } else {
1425     time_border_noise[1] = time_border[bs_num_env];
1426     bs_num_noise = 1;
1427   }
1428 
1429   switch (frame_class) {
1430     case FIXFIX:
1431       transient_env_temp = -1;
1432       break;
1433     case LD_TRAN:
1434       transient_env_temp =
1435           (numTimeSlots == 16)
1436               ? ixheaacd_ld_env_table_512[bs_transient_position]
1437                                          [SBR_ENVT_TRANIDX]
1438               : ixheaacd_ld_env_table_480[bs_transient_position]
1439                                          [SBR_ENVT_TRANIDX];
1440       break;
1441   };
1442 
1443   v_frame_info->num_env = bs_num_env;
1444   memcpy(v_frame_info->border_vec, time_border,
1445          (bs_num_env + 1) * sizeof(WORD16));
1446   memcpy(v_frame_info->freq_res, f, bs_num_env * sizeof(WORD16));
1447   v_frame_info->transient_env = transient_env_temp;
1448   v_frame_info->num_noise_env = bs_num_noise;
1449   memcpy(v_frame_info->noise_border_vec, time_border_noise,
1450          (bs_num_noise + 1) * sizeof(WORD16));
1451 
1452   return 1;
1453 }
1454 
ixheaacd_pvc_time_freq_grid_info(ia_bit_buf_struct * it_bit_buff,ia_sbr_frame_info_data_struct * ptr_frame_data)1455 WORD32 ixheaacd_pvc_time_freq_grid_info(
1456     ia_bit_buf_struct *it_bit_buff,
1457     ia_sbr_frame_info_data_struct *ptr_frame_data) {
1458   WORD32 bs_num_env = 0, bs_num_noise = 0;
1459   WORD32 time_border[MAX_ENVELOPES + 1];
1460   WORD32 time_border_noise[2 + 1];
1461   WORD32 pvc_time_border[MAX_ENVELOPES + 1];
1462   WORD32 pvc_time_border_noise[2 + 1];
1463   WORD32 bs_freq_res[MAX_ENVELOPES + 1];
1464   WORD32 var_len;
1465   ia_frame_info_struct *p_frame_info = &ptr_frame_data->str_frame_info_details;
1466   ia_frame_info_struct *pvc_frame_info = &ptr_frame_data->str_pvc_frame_info;
1467   WORD32 i;
1468   WORD32 prev_sbr_mode = ptr_frame_data->prev_sbr_mode;
1469 
1470   WORD32 tmp;
1471   WORD32 bs_noise_pos;
1472   bs_noise_pos = ixheaacd_read_bits_buf(it_bit_buff, 4);
1473 
1474   tmp = ixheaacd_read_bits_buf(it_bit_buff, 1);
1475   if (tmp == 0) {
1476     ptr_frame_data->var_len = 0;
1477   } else {
1478     tmp = ixheaacd_read_bits_buf(it_bit_buff, 2);
1479     ptr_frame_data->var_len = tmp + 1;
1480   }
1481   var_len = ptr_frame_data->var_len;
1482 
1483   if (p_frame_info->num_env > 0) {
1484     time_border[0] = p_frame_info->border_vec[p_frame_info->num_env] - 16;
1485   } else {
1486     time_border[0] = 0;
1487   }
1488   if (time_border[0] < 0) return -1;
1489   pvc_time_border[0] = 0;
1490   bs_freq_res[0] = 0;
1491 
1492   if (bs_noise_pos == 0) {
1493     time_border[1] = 16 + var_len;
1494     pvc_time_border[1] = 16;
1495     bs_num_noise = 1;
1496     bs_num_env = 1;
1497   } else {
1498     time_border[1] = bs_noise_pos;
1499     pvc_time_border[1] = bs_noise_pos;
1500     time_border[2] = 16 + var_len;
1501     pvc_time_border[2] = 16;
1502     bs_freq_res[1] = 0;
1503     bs_num_noise = 2;
1504     bs_num_env = 2;
1505   }
1506 
1507   for (i = 0; i < 3; i++) {
1508     time_border_noise[i] = time_border[i];
1509     pvc_time_border_noise[i] = pvc_time_border[i];
1510   }
1511 
1512   if (prev_sbr_mode == ORIG_SBR) {
1513     pvc_time_border[0] = time_border[0];
1514     pvc_time_border_noise[0] = time_border[0];
1515   }
1516 
1517   pvc_frame_info->num_env = bs_num_env;
1518   for (i = 0; i < (bs_num_env + 1); i++) {
1519     pvc_frame_info->border_vec[i] = pvc_time_border[i];
1520   }
1521   for (i = 0; i < (bs_num_env); i++) {
1522     pvc_frame_info->freq_res[i] = bs_freq_res[i];
1523   }
1524   pvc_frame_info->transient_env = -1;
1525   pvc_frame_info->num_noise_env = bs_num_noise;
1526   for (i = 0; i < (bs_num_noise + 1); i++) {
1527     pvc_frame_info->noise_border_vec[i] = pvc_time_border_noise[i];
1528   }
1529   p_frame_info->num_env = bs_num_env;
1530   for (i = 0; i < (bs_num_env + 1); i++) {
1531     p_frame_info->border_vec[i] = time_border[i];
1532   }
1533   for (i = 0; i < (bs_num_env); i++) {
1534     p_frame_info->freq_res[i] = bs_freq_res[i];
1535   }
1536   p_frame_info->transient_env = -1;
1537   p_frame_info->num_noise_env = bs_num_noise;
1538   for (i = 0; i < (bs_num_noise + 1); i++) {
1539     p_frame_info->noise_border_vec[i] = time_border_noise[i];
1540   }
1541   return 0;
1542 }
1543 
ixheaacd_sbr_time_freq_grid_info(ia_bit_buf_struct * it_bit_buff,ia_sbr_frame_info_data_struct * ptr_frame_data,ia_env_extr_tables_struct * env_extr_tables_ptr)1544 WORD16 ixheaacd_sbr_time_freq_grid_info(
1545     ia_bit_buf_struct *it_bit_buff,
1546     ia_sbr_frame_info_data_struct *ptr_frame_data,
1547     ia_env_extr_tables_struct *env_extr_tables_ptr) {
1548   WORD32 i, k, bs_num_rel = 0;
1549   WORD32 bs_pointer_bits = 0, bs_num_env = 0, border, bs_pointer,
1550          bs_var_bord = 0, temp = 0;
1551   WORD32 freq_res_0 = 0, frame_class;
1552   WORD32 abs_bord_lead, abs_bord_trail, num_rel_trail, num_rel_lead;
1553   static const WORD32 pointer_bits_array[7] = {1, 2, 2, 3, 3, 3, 3};
1554   ia_frame_info_struct *p_fixfix_tab;
1555   ia_frame_info_struct *p_frame_info = &ptr_frame_data->str_frame_info_details;
1556 
1557   frame_class = ixheaacd_read_bits_buf(it_bit_buff, SBR_FRAME_CLASS_BITS);
1558   p_frame_info->frame_class = frame_class;
1559 
1560   switch (frame_class) {
1561     case FIXFIX:
1562       temp =
1563           ixheaacd_read_bits_buf(it_bit_buff, SBR_ENV_BITS + SBR_FRQ_RES_BITS);
1564       bs_num_env = (temp & 0x6) >> SBR_FRQ_RES_BITS;
1565       p_fixfix_tab = &env_extr_tables_ptr->sbr_frame_info1_2_4_16[bs_num_env];
1566       memcpy(p_frame_info, p_fixfix_tab, sizeof(ia_frame_info_struct));
1567       bs_num_env = (1 << bs_num_env);
1568       freq_res_0 = temp & 0x1;
1569 
1570       if (!freq_res_0) {
1571         memset(&p_frame_info->freq_res[0], 0, sizeof(WORD16) * bs_num_env);
1572       }
1573       break;
1574     case FIXVAR:
1575       bs_var_bord =
1576           ixheaacd_read_bits_buf(it_bit_buff, SBR_VAR_BORD_BITS + SBR_NUM_BITS);
1577       bs_num_rel = bs_var_bord & 3;
1578       bs_var_bord = bs_var_bord >> SBR_NUM_BITS;
1579       bs_num_env = bs_num_rel + 1;
1580       p_frame_info->border_vec[0] = 0;
1581       border = bs_var_bord + SBR_TIME_SLOTS;
1582       p_frame_info->border_vec[bs_num_env] = border;
1583       for (k = bs_num_rel; k > 0; k--) {
1584         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
1585         border = border - ((temp << 1) + 2);
1586         if (border < 0) border = 0;
1587         p_frame_info->border_vec[k] = border;
1588       }
1589 
1590       bs_pointer_bits = pointer_bits_array[bs_num_rel];
1591       bs_pointer = ixheaacd_read_bits_buf(it_bit_buff, bs_pointer_bits);
1592 
1593       if ((bs_pointer - (bs_num_rel + 1)) > 0) return 0;
1594 
1595       for (k = bs_num_rel; k >= 0; k--) {
1596         p_frame_info->freq_res[k] =
1597             ixheaacd_read_bits_buf(it_bit_buff, SBR_FRQ_RES_BITS);
1598       }
1599       if (bs_pointer) {
1600         p_frame_info->transient_env = bs_num_env + 1 - bs_pointer;
1601       } else {
1602         p_frame_info->transient_env = -1;
1603       }
1604       if ((bs_pointer == 0) || (bs_pointer == 1))
1605         p_frame_info->noise_border_vec[1] =
1606             p_frame_info->border_vec[bs_num_rel];
1607       else
1608         p_frame_info->noise_border_vec[1] =
1609             p_frame_info->border_vec[p_frame_info->transient_env];
1610 
1611       break;
1612 
1613     case VARFIX:
1614       bs_var_bord =
1615           ixheaacd_read_bits_buf(it_bit_buff, SBR_VAR_BORD_BITS + SBR_NUM_BITS);
1616       bs_num_rel = bs_var_bord & 3;
1617       bs_var_bord = bs_var_bord >> SBR_NUM_BITS;
1618       bs_num_env = bs_num_rel + 1;
1619 
1620       border = bs_var_bord;
1621       p_frame_info->border_vec[0] = border;
1622       for (k = 1; k <= bs_num_rel; k++) {
1623         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
1624         border = border + ((temp << 1) + 2);
1625         if (border > SBR_TIME_SLOTS) border = SBR_TIME_SLOTS;
1626         p_frame_info->border_vec[k] = border;
1627       }
1628       p_frame_info->border_vec[k] = SBR_TIME_SLOTS;
1629 
1630       bs_pointer_bits = pointer_bits_array[bs_num_rel];
1631 
1632       bs_pointer = ixheaacd_read_bits_buf(it_bit_buff, bs_pointer_bits);
1633 
1634       if ((bs_pointer - (bs_num_rel + 1)) > 0) return 0;
1635 
1636       if (bs_pointer == 0 || (bs_pointer - 1) == 0) {
1637         p_frame_info->transient_env = -1;
1638       } else {
1639         p_frame_info->transient_env = bs_pointer - 1;
1640       }
1641 
1642       for (k = 0; k <= bs_num_rel; k++) {
1643         p_frame_info->freq_res[k] =
1644             ixheaacd_read_bits_buf(it_bit_buff, SBR_FRQ_RES_BITS);
1645       }
1646 
1647       switch (bs_pointer) {
1648         case 0:
1649           p_frame_info->noise_border_vec[1] = p_frame_info->border_vec[1];
1650           break;
1651         case 1:
1652           p_frame_info->noise_border_vec[1] =
1653               p_frame_info->border_vec[bs_num_rel];
1654           break;
1655         default:
1656           p_frame_info->noise_border_vec[1] =
1657               p_frame_info->border_vec[(WORD32)p_frame_info->transient_env];
1658           break;
1659       }
1660 
1661       break;
1662 
1663     case VARVAR:
1664       abs_bord_lead = ixheaacd_read_bits_buf(
1665           it_bit_buff, 2 * SBR_VAR_BORD_BITS + 2 * SBR_NUM_BITS);
1666       abs_bord_trail =
1667           (((abs_bord_lead & 0x30) >> (2 * SBR_NUM_BITS)) + SBR_TIME_SLOTS);
1668       num_rel_trail = ((abs_bord_lead & 0xc) >> SBR_NUM_BITS);
1669       num_rel_lead = (abs_bord_lead & 0x3);
1670       abs_bord_lead = abs_bord_lead >> (SBR_VAR_BORD_BITS + 2 * SBR_NUM_BITS);
1671       bs_num_env = ((num_rel_trail + num_rel_lead) + 1);
1672       border = abs_bord_lead;
1673       p_frame_info->border_vec[0] = border;
1674 
1675       for (k = 1; k <= num_rel_trail; k++) {
1676         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
1677         border = border + ((temp << 1) + 2);
1678         p_frame_info->border_vec[k] = border;
1679       }
1680 
1681       border = abs_bord_trail;
1682       i = bs_num_env;
1683 
1684       p_frame_info->border_vec[i] = border;
1685 
1686       for (k = 0; k < num_rel_lead; k++) {
1687         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
1688         border = border - ((temp << 1) + 2);
1689         i--;
1690         p_frame_info->border_vec[i] = border;
1691       }
1692       bs_pointer_bits = pointer_bits_array[num_rel_trail + num_rel_lead];
1693 
1694       bs_pointer = ixheaacd_read_bits_buf(it_bit_buff, bs_pointer_bits);
1695       if ((bs_pointer - ((num_rel_trail + num_rel_lead) + 1)) > 0) return 0;
1696 
1697       if (bs_pointer) {
1698         p_frame_info->transient_env = bs_num_env + 1 - bs_pointer;
1699       } else {
1700         p_frame_info->transient_env = -1;
1701       }
1702 
1703       for (k = 0; k < bs_num_env; k++) {
1704         p_frame_info->freq_res[k] =
1705             ixheaacd_read_bits_buf(it_bit_buff, SBR_FRQ_RES_BITS);
1706       }
1707       p_frame_info->noise_border_vec[0] = abs_bord_lead;
1708       if (bs_num_env == 1) {
1709         p_frame_info->noise_border_vec[1] = abs_bord_trail;
1710       } else {
1711         if (bs_pointer == 0 || (bs_pointer - 1) == 0)
1712           p_frame_info->noise_border_vec[1] =
1713               p_frame_info->border_vec[bs_num_env - 1];
1714         else
1715           p_frame_info->noise_border_vec[1] =
1716               p_frame_info->border_vec[(WORD32)p_frame_info->transient_env];
1717 
1718         p_frame_info->noise_border_vec[2] = abs_bord_trail;
1719       }
1720       break;
1721   }
1722   p_frame_info->num_env = bs_num_env;
1723 
1724   if (bs_num_env == 1)
1725     p_frame_info->num_noise_env = 1;
1726   else
1727     p_frame_info->num_noise_env = 2;
1728 
1729   if (frame_class == VARFIX || frame_class == FIXVAR) {
1730     p_frame_info->noise_border_vec[0] = p_frame_info->border_vec[0];
1731     p_frame_info->noise_border_vec[p_frame_info->num_noise_env] =
1732         p_frame_info->border_vec[bs_num_env];
1733   }
1734   return 1;
1735 }