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