• 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   IA_ERRORCODE err = IA_NO_ERROR;
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 err;
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_bit_buf_struct * it_bit_buff)307 WORD32 ixheaacd_ssc_huff_dec(ia_huffman_data_type t_huff,
308                              ia_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   prev_header_info.start_freq = 0;
341   prev_header_info.noise_bands = 0;
342   FLAG header_extra_1 = 0, header_extra_2 = 0;
343   WORD32 tmp;
344   WORD32 usac_independency_flag = pstr_sbr_header->usac_independency_flag;
345   WORD32 use_dflt_hdr = 0;
346   WORD32 header_present = 1;
347   WORD32 usac_flag = pstr_sbr_header->usac_flag;
348 
349   if (!usac_flag) {
350     memcpy(&prev_header_info, pstr_sbr_header,
351            sizeof(ia_sbr_header_data_struct));
352 
353     tmp = ixheaacd_read_bits_buf(
354         it_bit_buff, SBR_AMPLITUDE_RESOLUTION_BITS + SBR_BEGIN_SAMP_FREQ_BITS +
355                          SBR_END_SAMP_FREQ_BITS + SBR_CROSS_OVER_BND_BITS);
356 
357     pstr_sbr_header->amp_res = (WORD16)(
358         (tmp & 0x0800) >> (SBR_BEGIN_SAMP_FREQ_BITS + SBR_END_SAMP_FREQ_BITS +
359                            SBR_CROSS_OVER_BND_BITS));
360 
361     pstr_sbr_header->start_freq = (WORD16)(
362         (tmp & 0x0780) >> (SBR_END_SAMP_FREQ_BITS + SBR_CROSS_OVER_BND_BITS));
363 
364     pstr_sbr_header->stop_freq =
365         (WORD16)((tmp & 0x078) >> (SBR_CROSS_OVER_BND_BITS));
366 
367     pstr_sbr_header->xover_band = (WORD16)((tmp & 0x07));
368 
369     tmp = ixheaacd_read_bits_buf(
370         it_bit_buff,
371         SBR_HDR_RESERV_BITS + SBR_HDR_EXTR_1_BITS + SBR_HDR_EXTR_2_BITS);
372     header_extra_1 = (FLAG)((tmp & 0x02) >> (SBR_HDR_EXTR_2_BITS));
373     header_extra_2 = (FLAG)((tmp & 0x01));
374     if (stereo_flag) {
375       pstr_sbr_header->channel_mode = SBR_STEREO;
376     } else {
377       pstr_sbr_header->channel_mode = SBR_MONO;
378     }
379   } else {
380     WORD32 info_present = 0;
381     if (pstr_sbr_header->sync_state == SBR_ACTIVE) {
382       memcpy(&prev_header_info, pstr_sbr_header,
383              sizeof(ia_sbr_header_data_struct));
384     }
385     if (usac_independency_flag) {
386       header_present = 1;
387       info_present = 1;
388     } else {
389       info_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
390       if (info_present) {
391         header_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
392       } else {
393         header_present = 0;
394       }
395     }
396 
397     if (info_present) {
398       tmp = ixheaacd_read_bits_buf(it_bit_buff, SBR_AMPLITUDE_RESOLUTION_BITS +
399                                                     ESBR_CROSS_OVER_BND_BITS +
400                                                     ESBR_PRE_FLAT_BITS);
401       pstr_sbr_header->amp_res = (WORD16)(
402           (tmp & 0x0020) >> (ESBR_CROSS_OVER_BND_BITS + ESBR_PRE_FLAT_BITS));
403       pstr_sbr_header->xover_band =
404           (WORD16)((tmp & 0x001E) >> (ESBR_PRE_FLAT_BITS));
405       pstr_sbr_header->pre_proc_flag = (WORD16)((tmp & 0x001));
406       if (pstr_sbr_header->pvc_flag) {
407         pstr_sbr_header->pvc_mode =
408             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PVC_MODE_BITS);
409       } else {
410         pstr_sbr_header->pvc_mode = 0;
411       }
412     }
413 
414     if (header_present) {
415       use_dflt_hdr = ixheaacd_read_bits_buf(it_bit_buff, 1);
416       if (use_dflt_hdr) {
417         pstr_sbr_header->start_freq = pstr_sbr_dflt_header->start_freq;
418         pstr_sbr_header->stop_freq = pstr_sbr_dflt_header->stop_freq;
419         pstr_sbr_header->header_extra_1 = pstr_sbr_dflt_header->header_extra_1;
420         pstr_sbr_header->header_extra_2 = pstr_sbr_dflt_header->header_extra_2;
421         pstr_sbr_header->freq_scale = pstr_sbr_dflt_header->freq_scale;
422         pstr_sbr_header->alter_scale = pstr_sbr_dflt_header->alter_scale;
423         pstr_sbr_header->noise_bands = pstr_sbr_dflt_header->noise_bands;
424         pstr_sbr_header->limiter_bands = pstr_sbr_dflt_header->limiter_bands;
425         pstr_sbr_header->limiter_gains = pstr_sbr_dflt_header->limiter_gains;
426         pstr_sbr_header->interpol_freq = pstr_sbr_dflt_header->interpol_freq;
427         pstr_sbr_header->smoothing_mode = pstr_sbr_dflt_header->smoothing_mode;
428       } else {
429         tmp = ixheaacd_read_bits_buf(
430             it_bit_buff, SBR_BEGIN_SAMP_FREQ_BITS + SBR_END_SAMP_FREQ_BITS +
431                              SBR_HDR_EXTR_1_BITS + SBR_HDR_EXTR_2_BITS);
432         pstr_sbr_header->start_freq =
433             (tmp & 0x03C0) >> (SBR_END_SAMP_FREQ_BITS + SBR_HDR_EXTR_1_BITS +
434                                SBR_HDR_EXTR_2_BITS);
435         pstr_sbr_header->stop_freq =
436             (tmp & 0x003C) >> (SBR_HDR_EXTR_1_BITS + SBR_HDR_EXTR_2_BITS);
437         pstr_sbr_header->header_extra_1 =
438             (tmp & 0x0002) >> (SBR_HDR_EXTR_2_BITS);
439         pstr_sbr_header->header_extra_2 = (tmp & 0x0001);
440         header_extra_1 = pstr_sbr_header->header_extra_1;
441         header_extra_2 = pstr_sbr_header->header_extra_2;
442       }
443     }
444   }
445 
446   if (!use_dflt_hdr && header_present) {
447     if (header_extra_1) {
448       tmp = ixheaacd_read_bits_buf(
449           it_bit_buff,
450           SBR_SAMP_FREQ_LVL_BITS + SBR_CHANGE_LVL_BITS + SBR_NOISE_BND_BITS);
451       pstr_sbr_header->freq_scale =
452           (WORD16)((tmp & 0x018) >> (SBR_CHANGE_LVL_BITS + SBR_NOISE_BND_BITS));
453       pstr_sbr_header->alter_scale =
454           (WORD16)((tmp & 0x04) >> (SBR_NOISE_BND_BITS));
455       pstr_sbr_header->noise_bands = (WORD16)((tmp & 0x03));
456     } else {
457       pstr_sbr_header->freq_scale = SBR_SAMP_FEQ_LVL_DEF;
458       pstr_sbr_header->alter_scale = SBR_CHANGE_LVL_DEF;
459       pstr_sbr_header->noise_bands = SBR_NOISE_BND_DEF;
460     }
461 
462     if (header_extra_2) {
463       tmp = ixheaacd_read_bits_buf(
464           it_bit_buff, SBR_BND_LIMIT_BITS + SBR_GAIN_LIMIT_BITS +
465                            SBR_INTERPOL_SAMP_FREQ_BITS + SBR_SMOOTH_LEN_BITS);
466       pstr_sbr_header->limiter_bands = (WORD16)(
467           (tmp & 0x030) >> (SBR_GAIN_LIMIT_BITS + SBR_INTERPOL_SAMP_FREQ_BITS +
468                             SBR_SMOOTH_LEN_BITS));
469       pstr_sbr_header->limiter_gains = (WORD16)(
470           (tmp & 0x0c) >> (SBR_INTERPOL_SAMP_FREQ_BITS + SBR_SMOOTH_LEN_BITS));
471       pstr_sbr_header->interpol_freq =
472           (WORD16)((tmp & 0x02) >> (SBR_SMOOTH_LEN_BITS));
473       pstr_sbr_header->smoothing_mode = (WORD16)((tmp & 0x01));
474     } else {
475       pstr_sbr_header->limiter_bands = SBR_BND_LIMIT_DEF;
476       pstr_sbr_header->limiter_gains = SBR_GAIN_LIMIT_DEF;
477       pstr_sbr_header->interpol_freq = SBR_INTERPOL_SAMP_FEQ_DEF;
478       pstr_sbr_header->smoothing_mode = SBR_SMOOTH_LEN_DEF;
479     }
480   }
481 
482   if ((pstr_sbr_header->sync_state != SBR_ACTIVE) ||
483       (prev_header_info.start_freq != pstr_sbr_header->start_freq) ||
484       (prev_header_info.stop_freq != pstr_sbr_header->stop_freq) ||
485       (prev_header_info.xover_band != pstr_sbr_header->xover_band) ||
486       (prev_header_info.freq_scale != pstr_sbr_header->freq_scale) ||
487       (prev_header_info.alter_scale != pstr_sbr_header->alter_scale) ||
488       (prev_header_info.noise_bands != pstr_sbr_header->noise_bands)) {
489     return SBR_RESET;
490   }
491 
492   return 0;
493 }
494 
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)495 static VOID ixheaacd_sbr_sin_coding_data(
496     ia_sbr_header_data_struct *ptr_header_data,
497     ia_sbr_frame_info_data_struct *ptr_frame_data,
498     ia_bit_buf_struct *it_bit_buff) {
499   FLAG *p_add_harmonic = ptr_frame_data->add_harmonics;
500   WORD32 i;
501 
502   i = ptr_header_data->pstr_freq_band_data->num_sf_bands[HIGH];
503   do {
504     *p_add_harmonic++ =
505         (FLAG)ixheaacd_read_bits_buf(it_bit_buff, SBR_ADD_SINE_FLAG_BITS);
506     i--;
507   } while (i != 0);
508 
509   return;
510 }
511 
ixheaacd_validate_frame_info(ia_frame_info_struct * pstr_frame_info,WORD16 num_time_slots,WORD audio_object_type)512 static WORD16 ixheaacd_validate_frame_info(
513     ia_frame_info_struct *pstr_frame_info, WORD16 num_time_slots,
514     WORD audio_object_type) {
515   WORD32 i, j;
516 
517   WORD32 start_pos, end_pos, transient_env, start_pos_noise, end_pos_noise,
518       num_env_sf, num_noise_env;
519 
520   num_env_sf = pstr_frame_info->num_env;
521   num_noise_env = pstr_frame_info->num_noise_env;
522 
523   if ((num_env_sf < 1) || (num_env_sf > MAX_ENVELOPES)) return 0;
524 
525   if (num_noise_env > MAX_NOISE_ENVELOPES) return 0;
526 
527   start_pos = pstr_frame_info->border_vec[0];
528   end_pos = pstr_frame_info->border_vec[num_env_sf];
529   transient_env = pstr_frame_info->transient_env;
530 
531   if (transient_env > num_env_sf) return 0;
532 
533   start_pos_noise = pstr_frame_info->noise_border_vec[0];
534   end_pos_noise = pstr_frame_info->noise_border_vec[num_noise_env];
535 
536   if ((start_pos < 0) || (start_pos >= end_pos)) return 0;
537 
538   if (start_pos > SBR_OV_SLOTS) return 0;
539   if (audio_object_type != AOT_ER_AAC_ELD &&
540       audio_object_type != AOT_ER_AAC_LD) {
541     if (num_time_slots != 15) {
542       if (end_pos < SBR_TIME_SLOTS) return 0;
543     } else {
544       if (end_pos < num_time_slots) return 0;
545     }
546   } else {
547     if (end_pos < num_time_slots) return 0;
548   }
549 
550   if (num_time_slots != 15) {
551     if (end_pos > add_d(SBR_TIME_SLOTS, SBR_OV_SLOTS)) return 0;
552   } else {
553     if (end_pos > add_d(num_time_slots, SBR_OV_SLOTS)) return 0;
554   }
555 
556   for (i = 0; i < num_env_sf; i++) {
557     if (pstr_frame_info->border_vec[i] > pstr_frame_info->border_vec[i + 1])
558       return 0;
559   }
560 
561   if ((num_env_sf == 1) && (num_noise_env > 1)) return 0;
562 
563   if ((start_pos != start_pos_noise) || (end_pos != end_pos_noise)) return 0;
564 
565   for (i = 0; i < num_noise_env; i++) {
566     start_pos_noise = pstr_frame_info->noise_border_vec[i];
567 
568     for (j = 0; j < num_env_sf; j++) {
569       if (pstr_frame_info->border_vec[j] == start_pos_noise) break;
570     }
571     if (j == num_env_sf) return 0;
572   }
573 
574   return 1;
575 }
576 
ixheaacd_read_enh_sbr_data(ia_sbr_header_data_struct * ptr_header_data,ia_bit_buf_struct * it_bit_buff,VOID * p_frame_data,WORD32 ele_id)577 static WORD16 ixheaacd_read_enh_sbr_data(
578     ia_sbr_header_data_struct *ptr_header_data,
579     ia_bit_buf_struct *it_bit_buff,
580     VOID *p_frame_data,
581     WORD32 ele_id) {
582   WORD32 tmp = 0;
583   WORD16 num_bits_read = 0;
584   tmp = ixheaacd_read_bits_buf(it_bit_buff, ESBR_PRE_FLAT_BITS);
585   ptr_header_data->pre_proc_flag = tmp;
586   num_bits_read += ESBR_PRE_FLAT_BITS;
587 
588   if (ele_id == SBR_ID_SCE) {
589     ia_sbr_frame_info_data_struct *ptr_frame_data =
590         (ia_sbr_frame_info_data_struct *)p_frame_data;
591 
592     tmp = ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
593     ptr_frame_data->sbr_patching_mode = tmp;
594     num_bits_read += ESBR_PATCHING_MODE_BITS;
595 
596     if (tmp == 0) {
597       tmp = ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
598       ptr_frame_data->over_sampling_flag = tmp;
599       num_bits_read += ESBR_OVERSAMPLING_FLAG_BITS;
600 
601       tmp = ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS);
602       num_bits_read += ESBR_PITCHIN_FLAG_BITS;
603 
604       if (tmp) {
605         tmp =
606            ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
607         ptr_frame_data->pitch_in_bins = tmp;
608         num_bits_read += ESBR_PITCHIN_BINS_BITS;
609       } else {
610         ptr_frame_data->pitch_in_bins = 0;
611       }
612     } else {
613       ptr_frame_data->over_sampling_flag = 0;
614       ptr_frame_data->pitch_in_bins = 0;
615     }
616   } else if (ele_id == SBR_ID_CPE) {
617     ia_sbr_frame_info_data_struct **ptr_frame_data =
618         (ia_sbr_frame_info_data_struct **)p_frame_data;
619     if (ptr_frame_data[0]->coupling_mode) {
620       ptr_frame_data[0]->sbr_patching_mode =
621           ptr_frame_data[1]->sbr_patching_mode =
622           ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
623       num_bits_read += ESBR_PATCHING_MODE_BITS;
624 
625       if (ptr_frame_data[0]->sbr_patching_mode == 0) {
626         ptr_frame_data[0]->over_sampling_flag =
627             ptr_frame_data[1]->over_sampling_flag =
628             ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
629         num_bits_read += ESBR_OVERSAMPLING_FLAG_BITS;
630         num_bits_read += ESBR_PITCHIN_FLAG_BITS;
631         if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS)) {
632           ptr_frame_data[0]->pitch_in_bins =
633           ptr_frame_data[1]->pitch_in_bins =
634               ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
635           num_bits_read += ESBR_PITCHIN_BINS_BITS;
636         } else {
637           ptr_frame_data[0]->pitch_in_bins = 0;
638           ptr_frame_data[1]->pitch_in_bins = 0;
639         }
640       } else {
641         ptr_frame_data[0]->over_sampling_flag = 0;
642         ptr_frame_data[0]->pitch_in_bins = 0;
643 
644         ptr_frame_data[1]->over_sampling_flag = 0;
645         ptr_frame_data[1]->pitch_in_bins = 0;
646       }
647     } else {
648       ptr_frame_data[0]->sbr_patching_mode =
649           ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
650       num_bits_read += ESBR_PATCHING_MODE_BITS;
651 
652       if (ptr_frame_data[0]->sbr_patching_mode == 0) {
653         ptr_frame_data[0]->over_sampling_flag =
654             ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
655         num_bits_read += ESBR_OVERSAMPLING_FLAG_BITS;
656         num_bits_read += ESBR_PITCHIN_FLAG_BITS;
657         if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS)) {
658           ptr_frame_data[0]->pitch_in_bins =
659               ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
660           num_bits_read += ESBR_PITCHIN_BINS_BITS;
661         } else {
662           ptr_frame_data[0]->pitch_in_bins = 0;
663         }
664       } else {
665         ptr_frame_data[0]->over_sampling_flag = 0;
666         ptr_frame_data[0]->pitch_in_bins = 0;
667       }
668 
669       ptr_frame_data[1]->sbr_patching_mode =
670           ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
671       num_bits_read += ESBR_PATCHING_MODE_BITS;
672 
673       if (ptr_frame_data[1]->sbr_patching_mode == 0) {
674         ptr_frame_data[1]->over_sampling_flag =
675             ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
676         num_bits_read += ESBR_OVERSAMPLING_FLAG_BITS;
677         num_bits_read += ESBR_PITCHIN_FLAG_BITS;
678         if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS)) {
679           ptr_frame_data[1]->pitch_in_bins =
680               ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
681           num_bits_read += ESBR_PITCHIN_BINS_BITS;
682         } else {
683           ptr_frame_data[1]->pitch_in_bins = 0;
684         }
685       } else {
686         ptr_frame_data[1]->over_sampling_flag =
687             ptr_frame_data[1]->pitch_in_bins = 0;
688       }
689     }
690   }
691   if (num_bits_read < 6) {
692     ixheaacd_read_bits_buf(it_bit_buff, 6 - num_bits_read);
693     num_bits_read = 6;
694   }
695   return num_bits_read;
696 }
697 
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,VOID * p_frame_data,WORD32 ele_id)698 static IA_ERRORCODE ixheaacd_read_extn_data(ia_sbr_header_data_struct *ptr_header_data,
699                                             ia_ps_dec_struct *ptr_ps_dec,
700                                             ia_bit_buf_struct *it_bit_buff,
701                                             ia_ps_tables_struct *ps_tables_ptr,
702                                             VOID *p_frame_data, WORD32 ele_id) {
703   WORD i;
704   WORD extended_data;
705   WORD no_bits_left;
706 
707   extended_data = ixheaacd_read_bits_buf(it_bit_buff, SBR_ENLARGED_DATA_BITS);
708 
709   if (extended_data) {
710     WORD cnt;
711     FLAG ps_read;
712 
713     ps_read = 0;
714 
715     cnt = ixheaacd_read_bits_buf(it_bit_buff, SBR_CONT_SIZE_BITS);
716 
717     if (cnt == ((1 << SBR_CONT_SIZE_BITS) - 1)) {
718       cnt = (cnt + ixheaacd_read_bits_buf(it_bit_buff, SBR_CONT_ESC_CNT_BITS));
719     }
720 
721     no_bits_left = (cnt << 3);
722 
723     ptr_header_data->hbe_flag = !ptr_header_data->usac_flag;
724     ptr_header_data->sbr_ratio_idx = SBR_UPSAMPLE_IDX_2_1;
725 
726     while (no_bits_left > 7) {
727       WORD extension_id = ixheaacd_read_bits_buf(it_bit_buff, SBR_CONT_ID_BITS);
728 
729       no_bits_left = (no_bits_left - SBR_CONT_ID_BITS);
730 
731       switch (extension_id) {
732         case EXTENSION_ID_PS_CODING:
733 
734           if (ptr_ps_dec == NULL) {
735             return 0;
736           }
737 
738           if (!(ptr_ps_dec->force_mono || ps_read)) {
739             IA_ERRORCODE ret_val = ixheaacd_read_ps_data(ptr_ps_dec, it_bit_buff,
740                                                          (WORD16)no_bits_left, ps_tables_ptr);
741             if (ret_val == IA_FATAL_ERROR) {
742               return ret_val;
743             } else {
744               no_bits_left = no_bits_left - ret_val;
745             }
746 
747             if (no_bits_left < 0) return 0;
748             ptr_header_data->channel_mode = PS_STEREO;
749             ps_read = 1;
750             break;
751           }
752         case EXTENSION_ID_ENHSBR_CODING: {
753           ptr_header_data->enh_sbr = 1;
754           no_bits_left =
755               (no_bits_left - ixheaacd_read_enh_sbr_data(ptr_header_data, it_bit_buff,
756                   p_frame_data, ele_id));
757 
758           ptr_header_data->hbe_flag = 1;
759           ptr_header_data->sbr_ratio_idx = SBR_UPSAMPLE_IDX_2_1;
760           break;
761         }
762         default:
763           cnt = (no_bits_left >> 3);
764           for (i = cnt - 1; i >= 0; i--) ixheaacd_read_bits_buf(it_bit_buff, 8);
765           no_bits_left = (no_bits_left - (cnt << 3));
766           break;
767       }
768     }
769 
770     if (no_bits_left < 0) return 0;
771     ixheaacd_read_bits_buf(it_bit_buff, no_bits_left);
772   }
773   return 0;
774 }
775 
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)776 WORD32 ixheaacd_sbr_read_pvc_sce(ia_sbr_frame_info_data_struct *ptr_frame_data,
777                                  ia_bit_buf_struct *it_bit_buff,
778                                  WORD32 hbe_flag,
779                                  ia_pvc_data_struct *ptr_pvc_data,
780                                  ia_sbr_tables_struct *ptr_sbr_tables,
781                                  ia_sbr_header_data_struct *ptr_header_data) {
782   WORD32 i;
783   WORD32 err_code = 0;
784   ia_env_extr_tables_struct *env_extr_tables_ptr =
785       ptr_sbr_tables->env_extr_tables_ptr;
786   WORD32 usac_independency_flag = ptr_frame_data->usac_independency_flag;
787 
788   if (hbe_flag) {
789     ptr_frame_data->sbr_patching_mode =
790         ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
791 
792     if (ptr_frame_data->sbr_patching_mode == 0) {
793       ptr_frame_data->over_sampling_flag =
794           ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
795       if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
796         ptr_frame_data->pitch_in_bins =
797             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
798       else
799         ptr_frame_data->pitch_in_bins = 0;
800     } else {
801       ptr_frame_data->over_sampling_flag = ptr_frame_data->pitch_in_bins = 0;
802     }
803   }
804 
805   err_code = ixheaacd_pvc_time_freq_grid_info(it_bit_buff, ptr_frame_data);
806   if (err_code) return err_code;
807 
808   ptr_pvc_data->prev_sbr_mode = PVC_SBR;
809 
810   ixheaacd_pvc_env_dtdf_data(ptr_frame_data, it_bit_buff);
811 
812   for (i = 0; i < ptr_header_data->pstr_freq_band_data->num_nf_bands; i++) {
813     ptr_frame_data->sbr_invf_mode_prev[i] = ptr_frame_data->sbr_invf_mode[i];
814     ptr_frame_data->sbr_invf_mode[i] =
815         ixheaacd_read_bits_buf(it_bit_buff, ESBR_INVF_MODE_BITS);
816   }
817 
818   ptr_pvc_data->pvc_mode = ptr_header_data->pvc_mode;
819 
820   err_code = ixheaacd_read_esbr_pvc_envelope(ptr_pvc_data, it_bit_buff,
821                                              usac_independency_flag);
822   if (err_code) return err_code;
823 
824   ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data,
825                                      it_bit_buff, env_extr_tables_ptr);
826 
827   memset(ptr_frame_data->add_harmonics, 0,
828          ptr_header_data->pstr_freq_band_data->num_sf_bands[HIGH] *
829              sizeof(WORD32));
830   ptr_frame_data->pvc_mode = ptr_header_data->pvc_mode;
831 
832   ixheaacd_read_sbr_addi_data(ptr_frame_data, ptr_header_data, it_bit_buff);
833 
834   ptr_frame_data->coupling_mode = COUPLING_OFF;
835 
836   return err_code;
837 }
838 
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,WORD32 ec_flag)839 IA_ERRORCODE ixheaacd_sbr_read_sce(ia_sbr_header_data_struct *ptr_header_data,
840                                    ia_sbr_frame_info_data_struct *ptr_frame_data,
841                                    ia_ps_dec_struct *ptr_ps_dec, ia_bit_buf_struct *it_bit_buff,
842                                    ia_sbr_tables_struct *ptr_sbr_tables, WORD audio_object_type,
843                                    WORD32 ec_flag) {
844   WORD32 bit;
845   WORD32 i;
846   WORD32 hbe_flag = ptr_header_data->hbe_flag;
847   WORD32 num_if_bands = ptr_header_data->pstr_freq_band_data->num_if_bands;
848   WORD32 usac_flag = ptr_header_data->usac_flag;
849   ia_env_extr_tables_struct *env_extr_tables_ptr =
850       ptr_sbr_tables->env_extr_tables_ptr;
851   IA_ERRORCODE err = IA_NO_ERROR;
852 
853   ptr_frame_data->coupling_mode = COUPLING_OFF;
854 
855   if (!usac_flag) {
856     bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
857 
858     if (bit) ixheaacd_read_bits_buf(it_bit_buff, SBR_SCE_RESERV_BITS);
859     if (audio_object_type == AOT_ER_AAC_ELD ||
860         audio_object_type == AOT_ER_AAC_LD) {
861       if (ptr_frame_data->eld_sbr_flag == 1) {
862         err = ixheaacd_extract_frame_info_ld(it_bit_buff, ptr_frame_data);
863         if (err) return err;
864       }
865     } else {
866       if (!ixheaacd_sbr_time_freq_grid_info(it_bit_buff, ptr_frame_data,
867                                             env_extr_tables_ptr,
868                                             ptr_header_data->num_time_slots))
869 
870         return 0;
871     }
872     if (!ixheaacd_validate_frame_info(&ptr_frame_data->str_frame_info_details,
873                                       ptr_header_data->num_time_slots,
874                                       audio_object_type))
875       return 0;
876 
877   } else {
878     if (hbe_flag) {
879       ptr_frame_data->sbr_patching_mode =
880           ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
881       if (ptr_frame_data->sbr_patching_mode == 0) {
882         ptr_frame_data->over_sampling_flag =
883             ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
884         if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
885           ptr_frame_data->pitch_in_bins =
886               ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
887         else
888           ptr_frame_data->pitch_in_bins = 0;
889       } else {
890         ptr_frame_data->over_sampling_flag = ptr_frame_data->pitch_in_bins = 0;
891       }
892     }
893     ptr_frame_data->num_time_slots = ptr_header_data->num_time_slots;
894     if (!ixheaacd_sbr_time_freq_grid_info(it_bit_buff, ptr_frame_data,
895                                           env_extr_tables_ptr,
896                                           ptr_header_data->num_time_slots))
897       return 0;
898 
899     if (!ixheaacd_validate_frame_info(&ptr_frame_data->str_frame_info_details,
900                                       ptr_header_data->num_time_slots,
901                                       audio_object_type))
902       return 0;
903 
904     ptr_frame_data->prev_sbr_mode = ORIG_SBR;
905   }
906 
907   ixheaacd_sbr_env_dtdf_data(ptr_frame_data, it_bit_buff,
908                              ptr_header_data->usac_flag);
909 
910   if (ptr_frame_data->del_cod_dir_arr[0] == DTDF_DIR_FREQ) {
911     ptr_header_data->err_flag = 0;
912   }
913 
914   for (i = 0; i < num_if_bands; i++) {
915     ptr_frame_data->sbr_invf_mode_prev[i] = ptr_frame_data->sbr_invf_mode[i];
916     ptr_frame_data->sbr_invf_mode[i] =
917         ixheaacd_read_bits_buf(it_bit_buff, SBR_INVERSE_FILT_MODE_BITS);
918   }
919 
920   if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data, it_bit_buff,
921                                   env_extr_tables_ptr, audio_object_type))
922     return 0;
923 
924   ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data,
925                                      it_bit_buff, env_extr_tables_ptr);
926 
927   if (usac_flag) {
928     memset(
929         ptr_frame_data->add_harmonics, 0,
930         ptr_header_data->pstr_freq_band_data->num_sf_bands[1] * sizeof(WORD32));
931     ptr_frame_data->coupling_mode = COUPLING_OFF;
932   }
933 
934   bit = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
935   if (bit) {
936     ixheaacd_sbr_sin_coding_data(ptr_header_data, ptr_frame_data, it_bit_buff);
937   } else {
938     memset(ptr_frame_data->add_harmonics, 0, sizeof(FLAG) * MAX_FREQ_COEFFS);
939   }
940 
941   if (!usac_flag) {
942     IA_ERRORCODE err =
943         ixheaacd_read_extn_data(ptr_header_data, ptr_ps_dec, it_bit_buff,
944                                 ptr_sbr_tables->ps_tables_ptr, ptr_frame_data, SBR_ID_SCE);
945     if (err == IA_FATAL_ERROR) {
946       if (ec_flag)
947         return 0;
948       else
949         return err;
950     }
951   }
952 
953   return 1;
954 }
955 
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)956 IA_ERRORCODE ixheaacd_sbr_read_cpe(
957     ia_sbr_header_data_struct *ptr_header_data,
958     ia_sbr_frame_info_data_struct **ptr_frame_data,
959     ia_bit_buf_struct *it_bit_buff, ia_sbr_tables_struct *ptr_sbr_tables,
960     WORD audio_object_type) {
961   WORD32 i, k, bit, num_ch = 2;
962   WORD32 num_if_bands = ptr_header_data->pstr_freq_band_data->num_if_bands;
963   WORD32 hbe_flag = ptr_header_data->hbe_flag;
964   WORD32 usac_flag = ptr_header_data->usac_flag;
965   IA_ERRORCODE err = IA_NO_ERROR;
966   ia_env_extr_tables_struct *env_extr_tables_ptr =
967       ptr_sbr_tables->env_extr_tables_ptr;
968   bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
969 
970   if (usac_flag) {
971     if (bit) {
972       if (hbe_flag) {
973         ptr_frame_data[0]->sbr_patching_mode =
974             ptr_frame_data[1]->sbr_patching_mode =
975                 ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
976         if (ptr_frame_data[0]->sbr_patching_mode == 0) {
977           ptr_frame_data[0]->over_sampling_flag =
978               ptr_frame_data[1]->over_sampling_flag = ixheaacd_read_bits_buf(
979                   it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
980           if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
981             ptr_frame_data[0]->pitch_in_bins =
982                 ptr_frame_data[1]->pitch_in_bins =
983                     ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
984           else
985             ptr_frame_data[0]->pitch_in_bins =
986                 ptr_frame_data[1]->pitch_in_bins = 0;
987         } else {
988           ptr_frame_data[0]->over_sampling_flag = 0;
989           ptr_frame_data[1]->over_sampling_flag = 0;
990           ptr_frame_data[0]->pitch_in_bins = 0;
991           ptr_frame_data[1]->pitch_in_bins = 0;
992         }
993       }
994       ptr_frame_data[0]->coupling_mode = COUPLING_LEVEL;
995       ptr_frame_data[1]->coupling_mode = COUPLING_BAL;
996     } else {
997       if (hbe_flag) {
998         ptr_frame_data[0]->sbr_patching_mode =
999             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
1000         if (ptr_frame_data[0]->sbr_patching_mode == 0) {
1001           ptr_frame_data[0]->over_sampling_flag =
1002               ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
1003           if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
1004             ptr_frame_data[0]->pitch_in_bins =
1005                 ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
1006           else
1007             ptr_frame_data[0]->pitch_in_bins = 0;
1008         } else {
1009           ptr_frame_data[0]->over_sampling_flag = 0;
1010           ptr_frame_data[0]->pitch_in_bins = 0;
1011         }
1012         ptr_frame_data[1]->sbr_patching_mode =
1013             ixheaacd_read_bits_buf(it_bit_buff, ESBR_PATCHING_MODE_BITS);
1014         if (ptr_frame_data[1]->sbr_patching_mode == 0) {
1015           ptr_frame_data[1]->over_sampling_flag =
1016               ixheaacd_read_bits_buf(it_bit_buff, ESBR_OVERSAMPLING_FLAG_BITS);
1017           if (ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_FLAG_BITS))
1018             ptr_frame_data[1]->pitch_in_bins =
1019                 ixheaacd_read_bits_buf(it_bit_buff, ESBR_PITCHIN_BINS_BITS);
1020           else
1021             ptr_frame_data[1]->pitch_in_bins = 0;
1022         } else {
1023           ptr_frame_data[1]->over_sampling_flag =
1024               ptr_frame_data[1]->pitch_in_bins = 0;
1025         }
1026       }
1027 
1028       ptr_frame_data[0]->coupling_mode = COUPLING_OFF;
1029       ptr_frame_data[1]->coupling_mode = COUPLING_OFF;
1030     }
1031   } else {
1032     if (bit) {
1033       ixheaacd_read_bits_buf(it_bit_buff,
1034                              SBR_SCE_RESERV_BITS + SBR_SCE_RESERV_BITS);
1035     }
1036     if ((audio_object_type != AOT_ER_AAC_ELD) &&
1037         (ptr_header_data->channel_mode != SBR_STEREO)) {
1038       ptr_header_data->sync_state = UPSAMPLING;
1039       return 0;
1040     }
1041 
1042     bit = ixheaacd_read_bits_buf(it_bit_buff, SBR_COUPLNG_MODE_BITS);
1043 
1044     if (bit) {
1045       ptr_frame_data[0]->coupling_mode = COUPLING_LEVEL;
1046       ptr_frame_data[1]->coupling_mode = COUPLING_BAL;
1047     } else {
1048       ptr_frame_data[0]->coupling_mode = COUPLING_OFF;
1049       ptr_frame_data[1]->coupling_mode = COUPLING_OFF;
1050     }
1051   }
1052 
1053   for (i = 0; i < num_ch; i++) {
1054     ptr_frame_data[i]->num_time_slots = ptr_header_data->num_time_slots;
1055     if (audio_object_type == AOT_ER_AAC_ELD ||
1056         audio_object_type == AOT_ER_AAC_LD) {
1057       if (ptr_frame_data[i]->eld_sbr_flag == 1) {
1058         err = ixheaacd_extract_frame_info_ld(it_bit_buff, ptr_frame_data[i]);
1059         if (err) return err;
1060       }
1061     } else {
1062       if (!ixheaacd_sbr_time_freq_grid_info(it_bit_buff, ptr_frame_data[i],
1063                                             env_extr_tables_ptr,
1064                                             ptr_header_data->num_time_slots))
1065         return 0;
1066     }
1067 
1068     if (!ixheaacd_validate_frame_info(
1069             &ptr_frame_data[i]->str_frame_info_details,
1070             ptr_header_data->num_time_slots, audio_object_type))
1071       return 0;
1072 
1073     if (ptr_frame_data[0]->coupling_mode) {
1074       memcpy(&ptr_frame_data[1]->str_frame_info_details,
1075              &ptr_frame_data[0]->str_frame_info_details,
1076              sizeof(ia_frame_info_struct));
1077       if (audio_object_type == AOT_ER_AAC_ELD ||
1078           audio_object_type == AOT_ER_AAC_LD) {
1079         ptr_frame_data[1]->amp_res = ptr_frame_data[0]->amp_res;
1080       }
1081       num_ch = 1;
1082     }
1083   }
1084 
1085   if (ptr_frame_data[0]->coupling_mode && usac_flag) {
1086     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[0], it_bit_buff,
1087                                ptr_header_data->usac_flag);
1088     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[1], it_bit_buff,
1089                                ptr_header_data->usac_flag);
1090 
1091     for (i = 0; i < ptr_header_data->pstr_freq_band_data->num_if_bands; i++) {
1092       ptr_frame_data[0]->sbr_invf_mode_prev[i] =
1093           ptr_frame_data[0]->sbr_invf_mode[i];
1094       ptr_frame_data[1]->sbr_invf_mode_prev[i] =
1095           ptr_frame_data[1]->sbr_invf_mode[i];
1096 
1097       ptr_frame_data[0]->sbr_invf_mode[i] =
1098           ixheaacd_read_bits_buf(it_bit_buff, ESBR_INVF_MODE_BITS);
1099       ptr_frame_data[1]->sbr_invf_mode[i] = ptr_frame_data[0]->sbr_invf_mode[i];
1100     }
1101 
1102     if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[0],
1103                                     it_bit_buff, env_extr_tables_ptr,
1104                                     audio_object_type)) {
1105       return 0;
1106     }
1107     ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[0],
1108                                        it_bit_buff, env_extr_tables_ptr);
1109 
1110     if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[1],
1111                                     it_bit_buff, env_extr_tables_ptr,
1112                                     audio_object_type)) {
1113       return 0;
1114     }
1115     ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[1],
1116                                        it_bit_buff, env_extr_tables_ptr);
1117 
1118     memset(
1119         ptr_frame_data[0]->add_harmonics, 0,
1120         ptr_header_data->pstr_freq_band_data->num_sf_bands[1] * sizeof(WORD32));
1121     memset(
1122         ptr_frame_data[1]->add_harmonics, 0,
1123         ptr_header_data->pstr_freq_band_data->num_sf_bands[1] * sizeof(WORD32));
1124 
1125   } else {
1126     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[0], it_bit_buff,
1127                                ptr_header_data->usac_flag);
1128     ixheaacd_sbr_env_dtdf_data(ptr_frame_data[1], it_bit_buff,
1129                                ptr_header_data->usac_flag);
1130 
1131     if ((ptr_frame_data[0]->del_cod_dir_arr[0] == DTDF_DIR_FREQ) &&
1132         (ptr_frame_data[1]->del_cod_dir_arr[0] == DTDF_DIR_FREQ)) {
1133       ptr_header_data->err_flag = 0;
1134     }
1135 
1136     for (k = 0; k < num_ch; k++) {
1137       for (i = 0; i < num_if_bands; i++) {
1138         ptr_frame_data[k]->sbr_invf_mode_prev[i] =
1139             ptr_frame_data[k]->sbr_invf_mode[i];
1140         ptr_frame_data[k]->sbr_invf_mode[i] = ixheaacd_read_bits_buf(
1141             it_bit_buff, SBR_INVERSE_FILT_MODE_BITS);
1142       }
1143     }
1144 
1145     if (ptr_frame_data[0]->coupling_mode) {
1146       memcpy(ptr_frame_data[1]->sbr_invf_mode_prev, ptr_frame_data[1]->sbr_invf_mode,
1147              sizeof(ptr_frame_data[1]->sbr_invf_mode_prev[0]) * num_if_bands);
1148       memcpy(ptr_frame_data[1]->sbr_invf_mode, ptr_frame_data[0]->sbr_invf_mode,
1149              sizeof(WORD32) * num_if_bands);
1150 
1151       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[0],
1152                                       it_bit_buff, env_extr_tables_ptr,
1153                                       audio_object_type)) {
1154         return 0;
1155       }
1156 
1157       ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[0],
1158                                          it_bit_buff, env_extr_tables_ptr);
1159 
1160       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[1],
1161                                       it_bit_buff, env_extr_tables_ptr,
1162                                       audio_object_type)) {
1163         return 0;
1164       }
1165     } else {
1166       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[0],
1167                                       it_bit_buff, env_extr_tables_ptr,
1168                                       audio_object_type))
1169         return 0;
1170 
1171       if (!ixheaacd_read_sbr_env_data(ptr_header_data, ptr_frame_data[1],
1172                                       it_bit_buff, env_extr_tables_ptr,
1173                                       audio_object_type))
1174         return 0;
1175 
1176       ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[0],
1177                                          it_bit_buff, env_extr_tables_ptr);
1178     }
1179     ixheaacd_read_sbr_noise_floor_data(ptr_header_data, ptr_frame_data[1],
1180                                        it_bit_buff, env_extr_tables_ptr);
1181   }
1182 
1183   bit = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
1184   if (bit) {
1185     ixheaacd_sbr_sin_coding_data(ptr_header_data, ptr_frame_data[0],
1186                                  it_bit_buff);
1187   } else {
1188     memset(ptr_frame_data[0]->add_harmonics, 0, sizeof(FLAG) * MAX_FREQ_COEFFS);
1189   }
1190 
1191   bit = (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
1192   if (bit) {
1193     ixheaacd_sbr_sin_coding_data(ptr_header_data, ptr_frame_data[1],
1194                                  it_bit_buff);
1195   } else {
1196     memset(ptr_frame_data[1]->add_harmonics, 0, sizeof(FLAG) * MAX_FREQ_COEFFS);
1197   }
1198 
1199   if (!usac_flag) {
1200     IA_ERRORCODE err =
1201         ixheaacd_read_extn_data(ptr_header_data, NULL, it_bit_buff, ptr_sbr_tables->ps_tables_ptr,
1202                                 (VOID *)ptr_frame_data, SBR_ID_CPE);
1203     if (err == IA_FATAL_ERROR) {
1204       return err;
1205     }
1206   }
1207   return 1;
1208 }
1209 
ixheaacd_sbr_env_dtdf_data(ia_sbr_frame_info_data_struct * ptr_frame_data,ia_bit_buf_struct * it_bit_buff,WORD32 usac_flag)1210 VOID ixheaacd_sbr_env_dtdf_data(ia_sbr_frame_info_data_struct *ptr_frame_data,
1211                                 ia_bit_buf_struct *it_bit_buff,
1212                                 WORD32 usac_flag) {
1213   WORD32 i;
1214   WORD32 num_env = ptr_frame_data->str_frame_info_details.num_env;
1215   WORD32 num_noise_env = ptr_frame_data->str_frame_info_details.num_noise_env;
1216   WORD16 *p_coding_dir_vec = ptr_frame_data->del_cod_dir_arr;
1217   WORD16 *p_coding_dir_noise_vec = ptr_frame_data->del_cod_dir_noise_arr;
1218   WORD32 usac_independency_flag = ptr_frame_data->usac_independency_flag;
1219 
1220   if (usac_flag) {
1221     if (usac_independency_flag) {
1222       *p_coding_dir_vec = 0;
1223       p_coding_dir_vec++;
1224     } else {
1225       *p_coding_dir_vec =
1226           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1227       p_coding_dir_vec++;
1228     }
1229     for (i = num_env - 1; i >= 1; i--) {
1230       *p_coding_dir_vec++ =
1231           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1232     }
1233     if (usac_independency_flag) {
1234       *p_coding_dir_noise_vec = 0;
1235       p_coding_dir_noise_vec++;
1236     } else {
1237       *p_coding_dir_noise_vec =
1238           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1239       p_coding_dir_noise_vec++;
1240     }
1241     for (i = num_noise_env - 1; i >= 1; i--) {
1242       *p_coding_dir_noise_vec++ =
1243           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1244     }
1245   } else {
1246     for (i = num_env - 1; i >= 0; i--) {
1247       *p_coding_dir_vec++ =
1248           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1249     }
1250 
1251     for (i = num_noise_env - 1; i >= 0; i--) {
1252       *p_coding_dir_noise_vec++ =
1253           (WORD16)ixheaacd_read_bits_buf(it_bit_buff, SBR_DEL_COD_DIR_BITS);
1254     }
1255   }
1256 }
1257 
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)1258 VOID ixheaacd_read_env_data(ia_sbr_frame_info_data_struct *ptr_frame_data,
1259                             ia_bit_buf_struct *it_bit_buff,
1260                             ia_huffman_data_type hcb_t,
1261                             ia_huffman_data_type hcb_f, WORD32 *idx_t,
1262                             WORD32 *idx_f, WORD16 *no_band, WORD32 num_env,
1263                             WORD32 env_data_tbl_comp_factor, WORD32 start_bits,
1264                             WORD32 start_bits_balance, WORD32 num_noise_env,
1265                             WORD32 lav, WORD32 usac_flag) {
1266   WORD32 j, i, ixheaacd_drc_offset = 0,
1267                coupling_mode = ptr_frame_data->coupling_mode, delta, bits,
1268                shift;
1269   WORD16 *p_coding_dir_vec, *p_sbr_sf;
1270   WORD16 index, length;
1271   WORD32 readword;
1272   FLOAT32 *p_sbr_sf_float;
1273 
1274   if (num_noise_env) {
1275     p_coding_dir_vec = ptr_frame_data->del_cod_dir_noise_arr;
1276     p_sbr_sf = ptr_frame_data->int_noise_floor;
1277     p_sbr_sf_float = ptr_frame_data->flt_noise_floor;
1278   } else {
1279     p_coding_dir_vec = ptr_frame_data->del_cod_dir_arr;
1280     p_sbr_sf = ptr_frame_data->int_env_sf_arr;
1281     p_sbr_sf_float = ptr_frame_data->flt_env_sf_arr;
1282   }
1283 
1284   if (coupling_mode == COUPLING_BAL) {
1285     bits = start_bits_balance;
1286     shift = env_data_tbl_comp_factor;
1287 
1288   } else {
1289     bits = start_bits;
1290     shift = 0;
1291   }
1292 
1293   for (j = 0; j < num_env; j++) {
1294     ia_huffman_data_type h;
1295     const WORD32 *idx_tab;
1296     WORD32 dtdf_dir_flag = p_coding_dir_vec[j];
1297 
1298     if (dtdf_dir_flag == DTDF_DIR_FREQ) {
1299       p_sbr_sf[ixheaacd_drc_offset] =
1300           (WORD16)(ixheaacd_read_bits_buf(it_bit_buff, bits) << shift);
1301       p_sbr_sf_float[ixheaacd_drc_offset] = p_sbr_sf[ixheaacd_drc_offset];
1302       h = hcb_f;
1303       idx_tab = idx_f;
1304     } else {
1305       h = hcb_t;
1306       idx_tab = idx_t;
1307     }
1308 
1309     for (i = (1 - dtdf_dir_flag); i < no_band[j]; i++) {
1310       if (it_bit_buff->cnt_bits < 20) {
1311         readword = ixheaacd_show_bits_buf(it_bit_buff, it_bit_buff->cnt_bits);
1312         readword = readword << (32 - it_bit_buff->cnt_bits);
1313       } else {
1314         readword = ixheaacd_show_bits_buf(it_bit_buff, 20);
1315         readword = readword << 12;
1316       }
1317       ixheaacd_huffman_decode(readword, &index, &length, (const UWORD16 *)h,
1318                               (const UWORD32 *)idx_tab);
1319       delta = index - lav;
1320       ixheaacd_read_bits_buf(it_bit_buff, length);
1321       p_sbr_sf[ixheaacd_drc_offset + i] =
1322           (WORD16)(delta << env_data_tbl_comp_factor);
1323       p_sbr_sf_float[ixheaacd_drc_offset + i] =
1324           p_sbr_sf[ixheaacd_drc_offset + i];
1325     }
1326     if (usac_flag && (num_noise_env == 0)) {
1327       ptr_frame_data->inter_temp_shape_mode[j] = 0;
1328       if (ptr_frame_data->inter_tes_flag) {
1329         WORD32 flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
1330         if (flag) {
1331           ptr_frame_data->inter_temp_shape_mode[j] =
1332               ixheaacd_read_bits_buf(it_bit_buff, 2);
1333         }
1334       }
1335     }
1336     ixheaacd_drc_offset += (no_band[j]);
1337   }
1338 }
1339 
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)1340 VOID ixheaacd_read_sbr_noise_floor_data(
1341     ia_sbr_header_data_struct *ptr_header_data,
1342     ia_sbr_frame_info_data_struct *ptr_frame_data,
1343     ia_bit_buf_struct *it_bit_buff,
1344     ia_env_extr_tables_struct *env_extr_tables_ptr) {
1345   WORD32 i;
1346   WORD32 coupling_mode;
1347   WORD16 num_noise_bands[MAX_NOISE_ENVELOPES];
1348   ia_huffman_data_type hcb_noise_env;
1349   ia_huffman_data_type hcb_noise;
1350   WORD32 *idx_noise_env;
1351   WORD32 *idx_noise;
1352   WORD32 lav;
1353   WORD32 env_data_tbl_comp_factor;
1354 
1355   WORD32 start_bits;
1356   WORD32 start_bits_balance;
1357   WORD32 num_noise_env = ptr_frame_data->str_frame_info_details.num_noise_env;
1358 
1359   for (i = 0; i < num_noise_env; i++)
1360     num_noise_bands[i] = ptr_header_data->pstr_freq_band_data->num_nf_bands;
1361 
1362   start_bits = SBR_BEGIN_NOISE_BITS_AMPLITUDE_RESOLUTION_3_0;
1363   start_bits_balance = SBR_BEGIN_NOISE_BITS_BALNCE_AMPLITUDE_RESOLUTION_3_0;
1364 
1365   coupling_mode = ptr_frame_data->coupling_mode;
1366 
1367   if (coupling_mode == COUPLING_BAL) {
1368     lav = 12;
1369     hcb_noise = (ia_huffman_data_type)&env_extr_tables_ptr
1370                     ->ixheaacd_t_huffman_noise_bal_3_0db_inp_table;
1371     idx_noise =
1372         env_extr_tables_ptr->ixheaacd_t_huffman_noise_bal_3_0db_idx_table;
1373     hcb_noise_env = (ia_huffman_data_type)&env_extr_tables_ptr
1374                         ->ixheaacd_f_huffman_env_bal_3_0db_inp_table;
1375     idx_noise_env =
1376         env_extr_tables_ptr->ixheaacd_f_huffman_env_bal_3_0db_idx_table;
1377     env_data_tbl_comp_factor = 1;
1378   } else {
1379     lav = 31;
1380     hcb_noise = (ia_huffman_data_type)&env_extr_tables_ptr
1381                     ->ixheaacd_t_huffman_noise_3_0db_inp_table;
1382     idx_noise = env_extr_tables_ptr->ixheaacd_t_huffman_noise_3_0db_idx_table;
1383     hcb_noise_env = (ia_huffman_data_type)&env_extr_tables_ptr
1384                         ->ixheaacd_f_huffman_env_3_0db_inp_table;
1385     idx_noise_env = env_extr_tables_ptr->ixheaacd_f_huffman_env_3_0db_idx_table;
1386     env_data_tbl_comp_factor = 0;
1387   }
1388 
1389   ixheaacd_read_env_data(ptr_frame_data, it_bit_buff, hcb_noise, hcb_noise_env,
1390                          idx_noise, idx_noise_env, &num_noise_bands[0],
1391                          num_noise_env, env_data_tbl_comp_factor, start_bits,
1392                          start_bits_balance, 1, lav,
1393                          ptr_header_data->usac_flag);
1394 }
1395 
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)1396 WORD16 ixheaacd_read_sbr_env_data(
1397     ia_sbr_header_data_struct *ptr_header_data,
1398     ia_sbr_frame_info_data_struct *ptr_frame_data,
1399     ia_bit_buf_struct *it_bit_buff,
1400     ia_env_extr_tables_struct *env_extr_tables_ptr, WORD audio_object_type) {
1401   WORD32 coupling_mode = ptr_frame_data->coupling_mode;
1402   WORD32 *idx_t, *idx_f;
1403   WORD32 lav;
1404   WORD32 i;
1405   WORD16 no_band[MAX_ENVELOPES];
1406   WORD32 amp_res, num_env, env_data_tbl_comp_factor, start_bits,
1407       start_bits_balance;
1408   WORD16 *p_freq_res = ptr_frame_data->str_frame_info_details.freq_res;
1409   WORD16 *p_num_sf_bands = ptr_header_data->pstr_freq_band_data->num_sf_bands;
1410   ia_huffman_data_type hcb_t, hcb_f;
1411 
1412   amp_res = ptr_header_data->amp_res;
1413   num_env = ptr_frame_data->str_frame_info_details.num_env;
1414 
1415   ptr_frame_data->num_env_sfac = 0;
1416 
1417   if ((ptr_frame_data->str_frame_info_details.frame_class == FIXFIX) &&
1418       (num_env == 1)) {
1419     if (audio_object_type != AOT_ER_AAC_ELD &&
1420         audio_object_type != AOT_ER_AAC_LD) {
1421       amp_res = SBR_AMPLITUDE_RESOLUTION_1_5;
1422     } else {
1423       amp_res = ptr_frame_data->amp_res;
1424     }
1425   }
1426   ptr_frame_data->amp_res = amp_res;
1427 
1428   if (amp_res == SBR_AMPLITUDE_RESOLUTION_3_0) {
1429     start_bits = SBR_BEGIN_ENVN_BITS_AMPLITUDE_RESOLUTION_3_0;
1430     start_bits_balance = SBR_BEGIN_ENVN_BITS_BALNCE_AMPLITUDE_RESOLUTION_3_0;
1431   } else {
1432     start_bits = SBR_BEGIN_ENVN_BITS_AMPLITUDE_RESOLUTION_1_5;
1433     start_bits_balance = SBR_BEGIN_ENVN_BITS_BALNCE_AMPLITUDE_RESOLUTION_1_5;
1434   }
1435 
1436   for (i = 0; i < num_env; i++) {
1437     no_band[i] = p_num_sf_bands[*p_freq_res++];
1438     ptr_frame_data->num_env_sfac =
1439         ixheaacd_add16(ptr_frame_data->num_env_sfac, no_band[i]);
1440   }
1441 
1442   if (ptr_frame_data->num_env_sfac > MAX_NUM_ENVELOPE_VALUES) return 0;
1443 
1444   if (coupling_mode == COUPLING_BAL) {
1445     env_data_tbl_comp_factor = 1;
1446 
1447     if (amp_res == SBR_AMPLITUDE_RESOLUTION_1_5) {
1448       lav = 24;
1449       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
1450                   ->ixheaacd_t_huffman_env_bal_1_5db_inp_table;
1451       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_bal_1_5db_idx_table;
1452       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
1453                   ->ixheaacd_f_huffman_env_bal_1_5db_inp_table;
1454       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_bal_1_5db_idx_table;
1455     } else {
1456       lav = 12;
1457       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
1458                   ->ixheaacd_t_huffman_env_bal_3_0db_inp_table;
1459       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_bal_3_0db_idx_table;
1460       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
1461                   ->ixheaacd_f_huffman_env_bal_3_0db_inp_table;
1462       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_bal_3_0db_idx_table;
1463     }
1464   } else {
1465     env_data_tbl_comp_factor = 0;
1466 
1467     if (amp_res == SBR_AMPLITUDE_RESOLUTION_1_5) {
1468       lav = 60;
1469       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
1470                   ->ixheaacd_t_huffman_env_1_5db_inp_table;
1471       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_1_5db_idx_table;
1472       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
1473                   ->ixheaacd_f_huffman_env_1_5db_inp_table;
1474       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_1_5db_idx_table;
1475     } else {
1476       lav = 31;
1477       hcb_t = (ia_huffman_data_type)&env_extr_tables_ptr
1478                   ->ixheaacd_t_huffman_env_3_0db_inp_table;
1479       idx_t = env_extr_tables_ptr->ixheaacd_t_huffman_env_3_0db_idx_table;
1480       hcb_f = (ia_huffman_data_type)&env_extr_tables_ptr
1481                   ->ixheaacd_f_huffman_env_3_0db_inp_table;
1482       idx_f = env_extr_tables_ptr->ixheaacd_f_huffman_env_3_0db_idx_table;
1483     }
1484   }
1485 
1486   ixheaacd_read_env_data(ptr_frame_data, it_bit_buff, hcb_t, hcb_f, idx_t,
1487                          idx_f, &no_band[0], num_env, env_data_tbl_comp_factor,
1488                          start_bits, start_bits_balance, 0, lav,
1489                          ptr_header_data->usac_flag);
1490 
1491   return 1;
1492 }
1493 
ixheaacd_extract_frame_info_ld(ia_bit_buf_struct * it_bit_buff,ia_sbr_frame_info_data_struct * h_frame_data)1494 IA_ERRORCODE ixheaacd_extract_frame_info_ld(
1495     ia_bit_buf_struct *it_bit_buff,
1496     ia_sbr_frame_info_data_struct *h_frame_data) {
1497   int abs_bord_lead = 0, num_rel_lead = 0, bs_num_env = 0,
1498       frame_class, temp, env, k, abs_bord_trail = 0, middle_bord = 0,
1499       bs_num_noise, transient_env_temp = 0, bs_transient_position = 0;
1500 
1501   IA_ERRORCODE err = IA_NO_ERROR;
1502   WORD16 time_border[MAX_ENVELOPES + 1];
1503   WORD16 time_border_noise[2 + 1];
1504   WORD16 f[MAX_ENVELOPES + 1];
1505   int rel_bord_lead[7] = {0};
1506 
1507   ia_frame_info_struct *v_frame_info = &h_frame_data->str_frame_info_details;
1508 
1509   int numTimeSlots = h_frame_data->num_time_slots;
1510 
1511   v_frame_info->frame_class = frame_class =
1512       ixheaacd_read_bits_buf(it_bit_buff, SBRLD_CLA_BITS);
1513 
1514   switch (frame_class) {
1515     case FIXFIX:
1516       temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_ENV_BITS);
1517       bs_num_env = 1 << temp;
1518 
1519       if (bs_num_env == 1)
1520         h_frame_data->amp_res =
1521             ixheaacd_read_bits_buf(it_bit_buff, SBR_AMPLITUDE_RESOLUTION_BITS);
1522 
1523       f[0] = ixheaacd_read_bits_buf(it_bit_buff, SBR_RES_BITS);
1524 
1525       for (env = 1; env < bs_num_env; env++) f[env] = f[0];
1526       break;
1527     case LD_TRAN:
1528       bs_transient_position =
1529           ixheaacd_read_bits_buf(it_bit_buff, SBR_TRAN_BITS);
1530       v_frame_info->frame_class = 0;
1531       if ((numTimeSlots != 16) && (bs_transient_position >= LD_ENV_TBL_480)) {
1532         return -1;
1533       }
1534       bs_num_env = (numTimeSlots == 16)
1535                        ? ixheaacd_ld_env_table_512[bs_transient_position]
1536                                                   [SBR_ENVT_NUMENV]
1537                        : ixheaacd_ld_env_table_480[bs_transient_position]
1538                                                   [SBR_ENVT_NUMENV];
1539       for (env = 0; env < bs_num_env; env++)
1540         f[env] = ixheaacd_read_bits_buf(it_bit_buff, SBR_RES_BITS);
1541       break;
1542   }
1543 
1544   switch (frame_class) {
1545     case FIXFIX:
1546       abs_bord_lead = 0;
1547       abs_bord_trail = numTimeSlots;
1548       num_rel_lead = bs_num_env - 1;
1549 
1550       for (k = 0; k < num_rel_lead; k++) {
1551         rel_bord_lead[k] = ixheaacd_ld_env_table_time_slot[num_rel_lead - 1];
1552       }
1553 
1554       time_border[0] = abs_bord_lead;
1555       time_border[bs_num_env] = abs_bord_trail;
1556       for (env = 1; env <= num_rel_lead; env++) {
1557         time_border[env] = abs_bord_lead;
1558         for (k = 0; k <= env - 1; k++) time_border[env] += rel_bord_lead[k];
1559       }
1560       break;
1561 
1562     case LD_TRAN:
1563       time_border[0] = 0;
1564       time_border[bs_num_env] = numTimeSlots;
1565       for (k = 1; k < bs_num_env; k++)
1566         time_border[k] =
1567             (numTimeSlots == 16)
1568                 ? ixheaacd_ld_env_table_512[bs_transient_position][k]
1569                 : ixheaacd_ld_env_table_480[bs_transient_position][k];
1570       break;
1571 
1572     default:
1573       time_border[0] = 0;
1574 
1575       break;
1576   };
1577 
1578   switch (frame_class) {
1579     case FIXFIX:
1580       middle_bord = bs_num_env / 2;
1581       break;
1582     case LD_TRAN:
1583       middle_bord = 1;
1584       break;
1585   };
1586 
1587   time_border_noise[0] = time_border[0];
1588   if (bs_num_env > 1) {
1589     time_border_noise[1] = time_border[middle_bord];
1590     time_border_noise[2] = time_border[bs_num_env];
1591     bs_num_noise = 2;
1592   } else {
1593     time_border_noise[1] = time_border[bs_num_env];
1594     bs_num_noise = 1;
1595   }
1596 
1597   switch (frame_class) {
1598     case FIXFIX:
1599       transient_env_temp = -1;
1600       break;
1601     case LD_TRAN:
1602       transient_env_temp =
1603           (numTimeSlots == 16)
1604               ? ixheaacd_ld_env_table_512[bs_transient_position]
1605                                          [SBR_ENVT_TRANIDX]
1606               : ixheaacd_ld_env_table_480[bs_transient_position]
1607                                          [SBR_ENVT_TRANIDX];
1608       break;
1609   };
1610 
1611   v_frame_info->num_env = bs_num_env;
1612   memcpy(v_frame_info->border_vec, time_border,
1613          (bs_num_env + 1) * sizeof(WORD16));
1614   memcpy(v_frame_info->freq_res, f, bs_num_env * sizeof(WORD16));
1615   v_frame_info->transient_env = transient_env_temp;
1616   v_frame_info->num_noise_env = bs_num_noise;
1617   memcpy(v_frame_info->noise_border_vec, time_border_noise,
1618          (bs_num_noise + 1) * sizeof(WORD16));
1619 
1620   return err;
1621 }
1622 
ixheaacd_pvc_time_freq_grid_info(ia_bit_buf_struct * it_bit_buff,ia_sbr_frame_info_data_struct * ptr_frame_data)1623 WORD32 ixheaacd_pvc_time_freq_grid_info(
1624     ia_bit_buf_struct *it_bit_buff,
1625     ia_sbr_frame_info_data_struct *ptr_frame_data) {
1626   WORD32 bs_num_env = 0, bs_num_noise = 0;
1627   WORD32 time_border[MAX_ENVELOPES + 1];
1628   WORD32 time_border_noise[2 + 1];
1629   WORD32 pvc_time_border[MAX_ENVELOPES + 1];
1630   WORD32 pvc_time_border_noise[2 + 1];
1631   WORD32 bs_freq_res[MAX_ENVELOPES + 1];
1632   WORD32 var_len;
1633   ia_frame_info_struct *p_frame_info = &ptr_frame_data->str_frame_info_details;
1634   ia_frame_info_struct *pvc_frame_info = &ptr_frame_data->str_pvc_frame_info;
1635   WORD32 i;
1636   WORD32 prev_sbr_mode = ptr_frame_data->prev_sbr_mode;
1637 
1638   WORD32 tmp;
1639   WORD32 bs_noise_pos;
1640   bs_noise_pos = ixheaacd_read_bits_buf(it_bit_buff, 4);
1641 
1642   tmp = ixheaacd_read_bits_buf(it_bit_buff, 1);
1643   if (tmp == 0) {
1644     ptr_frame_data->var_len = 0;
1645   } else {
1646     tmp = ixheaacd_read_bits_buf(it_bit_buff, 2);
1647     ptr_frame_data->var_len = tmp + 1;
1648   }
1649   var_len = ptr_frame_data->var_len;
1650 
1651   if (p_frame_info->num_env > 0) {
1652     time_border[0] = p_frame_info->border_vec[p_frame_info->num_env] - 16;
1653   } else {
1654     time_border[0] = 0;
1655   }
1656   if (time_border[0] < 0) return -1;
1657   pvc_time_border[0] = 0;
1658   bs_freq_res[0] = 0;
1659 
1660   if (bs_noise_pos == 0) {
1661     time_border[1] = 16 + var_len;
1662     pvc_time_border[1] = 16;
1663     bs_num_noise = 1;
1664     bs_num_env = 1;
1665   } else {
1666     time_border[1] = bs_noise_pos;
1667     pvc_time_border[1] = bs_noise_pos;
1668     time_border[2] = 16 + var_len;
1669     pvc_time_border[2] = 16;
1670     bs_freq_res[1] = 0;
1671     bs_num_noise = 2;
1672     bs_num_env = 2;
1673   }
1674 
1675   for (i = 0; i < 3; i++) {
1676     time_border_noise[i] = time_border[i];
1677     pvc_time_border_noise[i] = pvc_time_border[i];
1678   }
1679 
1680   if (prev_sbr_mode == ORIG_SBR) {
1681     pvc_time_border[0] = time_border[0];
1682     pvc_time_border_noise[0] = time_border[0];
1683   }
1684 
1685   pvc_frame_info->num_env = bs_num_env;
1686   for (i = 0; i < (bs_num_env + 1); i++) {
1687     pvc_frame_info->border_vec[i] = pvc_time_border[i];
1688   }
1689   for (i = 0; i < (bs_num_env); i++) {
1690     pvc_frame_info->freq_res[i] = bs_freq_res[i];
1691   }
1692   pvc_frame_info->transient_env = -1;
1693   pvc_frame_info->num_noise_env = bs_num_noise;
1694   for (i = 0; i < (bs_num_noise + 1); i++) {
1695     pvc_frame_info->noise_border_vec[i] = pvc_time_border_noise[i];
1696   }
1697   p_frame_info->num_env = bs_num_env;
1698   for (i = 0; i < (bs_num_env + 1); i++) {
1699     p_frame_info->border_vec[i] = time_border[i];
1700   }
1701   for (i = 0; i < (bs_num_env); i++) {
1702     p_frame_info->freq_res[i] = bs_freq_res[i];
1703   }
1704   p_frame_info->transient_env = -1;
1705   p_frame_info->num_noise_env = bs_num_noise;
1706   for (i = 0; i < (bs_num_noise + 1); i++) {
1707     p_frame_info->noise_border_vec[i] = time_border_noise[i];
1708   }
1709   return 0;
1710 }
1711 
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,WORD16 number_of_time_slots)1712 WORD16 ixheaacd_sbr_time_freq_grid_info(
1713     ia_bit_buf_struct *it_bit_buff,
1714     ia_sbr_frame_info_data_struct *ptr_frame_data,
1715     ia_env_extr_tables_struct *env_extr_tables_ptr, WORD16 number_of_time_slots) {
1716   WORD32 i, k, bs_num_rel = 0;
1717   WORD32 bs_pointer_bits = 0, bs_num_env = 0, border, bs_pointer,
1718          bs_var_bord = 0, temp = 0;
1719   WORD32 freq_res_0 = 0, frame_class;
1720   WORD32 abs_bord_lead, abs_bord_trail, num_rel_trail, num_rel_lead;
1721   static const WORD32 pointer_bits_array[7] = {1, 2, 2, 3, 3, 3, 3};
1722   ia_frame_info_struct *p_fixfix_tab;
1723   ia_frame_info_struct *p_frame_info = &ptr_frame_data->str_frame_info_details;
1724 
1725   frame_class = ixheaacd_read_bits_buf(it_bit_buff, SBR_FRAME_CLASS_BITS);
1726   p_frame_info->frame_class = frame_class;
1727 
1728   switch (frame_class) {
1729     case FIXFIX:
1730       temp =
1731           ixheaacd_read_bits_buf(it_bit_buff, SBR_ENV_BITS + SBR_FRQ_RES_BITS);
1732       bs_num_env = (temp & 0x6) >> SBR_FRQ_RES_BITS;
1733 
1734       if (number_of_time_slots != 15) {
1735         p_fixfix_tab = &env_extr_tables_ptr->sbr_frame_info1_2_4_16[bs_num_env];
1736       } else {
1737         if (bs_num_env > 2) return 0;
1738         p_fixfix_tab = &env_extr_tables_ptr->sbr_frame_info1_2_4_16[bs_num_env + 4];
1739       }
1740 
1741       memcpy(p_frame_info, p_fixfix_tab, sizeof(ia_frame_info_struct));
1742       bs_num_env = (1 << bs_num_env);
1743       freq_res_0 = temp & 0x1;
1744 
1745       if (!freq_res_0) {
1746         memset(&p_frame_info->freq_res[0], 0, sizeof(WORD16) * bs_num_env);
1747       }
1748       break;
1749     case FIXVAR:
1750       bs_var_bord =
1751           ixheaacd_read_bits_buf(it_bit_buff, SBR_VAR_BORD_BITS + SBR_NUM_BITS);
1752       bs_num_rel = bs_var_bord & 3;
1753       bs_var_bord = bs_var_bord >> SBR_NUM_BITS;
1754       bs_num_env = bs_num_rel + 1;
1755       p_frame_info->border_vec[0] = 0;
1756 
1757       if (number_of_time_slots != 15) {
1758         border = bs_var_bord + SBR_TIME_SLOTS;
1759       } else {
1760         border = bs_var_bord + number_of_time_slots;
1761       }
1762 
1763       p_frame_info->border_vec[bs_num_env] = border;
1764       for (k = bs_num_rel; k > 0; k--) {
1765         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
1766         border = border - ((temp << 1) + 2);
1767         if (border < 0) border = 0;
1768         p_frame_info->border_vec[k] = border;
1769       }
1770 
1771       bs_pointer_bits = pointer_bits_array[bs_num_rel];
1772       bs_pointer = ixheaacd_read_bits_buf(it_bit_buff, bs_pointer_bits);
1773 
1774       if ((bs_pointer - (bs_num_rel + 1)) > 0) return 0;
1775 
1776       for (k = bs_num_rel; k >= 0; k--) {
1777         p_frame_info->freq_res[k] =
1778             ixheaacd_read_bits_buf(it_bit_buff, SBR_FRQ_RES_BITS);
1779       }
1780       if (bs_pointer) {
1781         p_frame_info->transient_env = bs_num_env + 1 - bs_pointer;
1782       } else {
1783         p_frame_info->transient_env = -1;
1784       }
1785       if ((bs_pointer == 0) || (bs_pointer == 1))
1786         p_frame_info->noise_border_vec[1] =
1787             p_frame_info->border_vec[bs_num_rel];
1788       else
1789         p_frame_info->noise_border_vec[1] =
1790             p_frame_info->border_vec[p_frame_info->transient_env];
1791 
1792       break;
1793 
1794     case VARFIX:
1795       bs_var_bord =
1796           ixheaacd_read_bits_buf(it_bit_buff, SBR_VAR_BORD_BITS + SBR_NUM_BITS);
1797       bs_num_rel = bs_var_bord & 3;
1798       bs_var_bord = bs_var_bord >> SBR_NUM_BITS;
1799       bs_num_env = bs_num_rel + 1;
1800 
1801       border = bs_var_bord;
1802       p_frame_info->border_vec[0] = border;
1803       for (k = 1; k <= bs_num_rel; k++) {
1804         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
1805         border = border + ((temp << 1) + 2);
1806 
1807         if (number_of_time_slots != 15) {
1808           if (border > SBR_TIME_SLOTS) border = SBR_TIME_SLOTS;
1809         } else {
1810           if (border > number_of_time_slots) border = number_of_time_slots;
1811         }
1812 
1813         p_frame_info->border_vec[k] = border;
1814       }
1815 
1816       if (number_of_time_slots != 15) {
1817         p_frame_info->border_vec[k] = SBR_TIME_SLOTS;
1818       } else {
1819         p_frame_info->border_vec[k] = number_of_time_slots;
1820       }
1821 
1822       bs_pointer_bits = pointer_bits_array[bs_num_rel];
1823 
1824       bs_pointer = ixheaacd_read_bits_buf(it_bit_buff, bs_pointer_bits);
1825 
1826       if ((bs_pointer - (bs_num_rel + 1)) > 0) return 0;
1827 
1828       if (bs_pointer == 0 || (bs_pointer - 1) == 0) {
1829         p_frame_info->transient_env = -1;
1830       } else {
1831         p_frame_info->transient_env = bs_pointer - 1;
1832       }
1833 
1834       for (k = 0; k <= bs_num_rel; k++) {
1835         p_frame_info->freq_res[k] =
1836             ixheaacd_read_bits_buf(it_bit_buff, SBR_FRQ_RES_BITS);
1837       }
1838 
1839       switch (bs_pointer) {
1840         case 0:
1841           p_frame_info->noise_border_vec[1] = p_frame_info->border_vec[1];
1842           break;
1843         case 1:
1844           p_frame_info->noise_border_vec[1] =
1845               p_frame_info->border_vec[bs_num_rel];
1846           break;
1847         default:
1848           p_frame_info->noise_border_vec[1] =
1849               p_frame_info->border_vec[(WORD32)p_frame_info->transient_env];
1850           break;
1851       }
1852 
1853       break;
1854 
1855     case VARVAR:
1856       abs_bord_lead = ixheaacd_read_bits_buf(
1857           it_bit_buff, 2 * SBR_VAR_BORD_BITS + 2 * SBR_NUM_BITS);
1858 
1859       if (number_of_time_slots != 15) {
1860         abs_bord_trail =
1861           (((abs_bord_lead & 0x30) >> (2 * SBR_NUM_BITS)) + SBR_TIME_SLOTS);
1862       } else {
1863         abs_bord_trail =
1864           (((abs_bord_lead & 0x30) >> (2 * SBR_NUM_BITS)) + number_of_time_slots);
1865       }
1866 
1867       num_rel_trail = ((abs_bord_lead & 0xc) >> SBR_NUM_BITS);
1868       num_rel_lead = (abs_bord_lead & 0x3);
1869       abs_bord_lead = abs_bord_lead >> (SBR_VAR_BORD_BITS + 2 * SBR_NUM_BITS);
1870       bs_num_env = ((num_rel_trail + num_rel_lead) + 1);
1871       border = abs_bord_lead;
1872       p_frame_info->border_vec[0] = border;
1873 
1874       for (k = 1; k <= num_rel_trail; k++) {
1875         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
1876         border = border + ((temp << 1) + 2);
1877         p_frame_info->border_vec[k] = border;
1878       }
1879 
1880       border = abs_bord_trail;
1881       i = bs_num_env;
1882 
1883       p_frame_info->border_vec[i] = border;
1884 
1885       for (k = 0; k < num_rel_lead; k++) {
1886         temp = ixheaacd_read_bits_buf(it_bit_buff, SBR_REL_BITS);
1887         border = border - ((temp << 1) + 2);
1888         i--;
1889         p_frame_info->border_vec[i] = border;
1890       }
1891       bs_pointer_bits = pointer_bits_array[num_rel_trail + num_rel_lead];
1892 
1893       bs_pointer = ixheaacd_read_bits_buf(it_bit_buff, bs_pointer_bits);
1894       if ((bs_pointer - ((num_rel_trail + num_rel_lead) + 1)) > 0) return 0;
1895 
1896       if (bs_pointer) {
1897         p_frame_info->transient_env = bs_num_env + 1 - bs_pointer;
1898       } else {
1899         p_frame_info->transient_env = -1;
1900       }
1901 
1902       for (k = 0; k < bs_num_env; k++) {
1903         p_frame_info->freq_res[k] =
1904             ixheaacd_read_bits_buf(it_bit_buff, SBR_FRQ_RES_BITS);
1905       }
1906       p_frame_info->noise_border_vec[0] = abs_bord_lead;
1907       if (bs_num_env == 1) {
1908         p_frame_info->noise_border_vec[1] = abs_bord_trail;
1909       } else {
1910         if (bs_pointer == 0 || (bs_pointer - 1) == 0)
1911           p_frame_info->noise_border_vec[1] =
1912               p_frame_info->border_vec[bs_num_env - 1];
1913         else
1914           p_frame_info->noise_border_vec[1] =
1915               p_frame_info->border_vec[(WORD32)p_frame_info->transient_env];
1916 
1917         p_frame_info->noise_border_vec[2] = abs_bord_trail;
1918       }
1919       break;
1920   }
1921   p_frame_info->num_env = bs_num_env;
1922 
1923   if (bs_num_env == 1)
1924     p_frame_info->num_noise_env = 1;
1925   else
1926     p_frame_info->num_noise_env = 2;
1927 
1928   if (frame_class == VARFIX || frame_class == FIXVAR) {
1929     p_frame_info->noise_border_vec[0] = p_frame_info->border_vec[0];
1930     p_frame_info->noise_border_vec[p_frame_info->num_noise_env] =
1931         p_frame_info->border_vec[bs_num_env];
1932   }
1933   return 1;
1934 }