• 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 <stdlib.h>
21 #include "ixheaacd_type_def.h"
22 #include "ixheaacd_error_standards.h"
23 #include "ixheaacd_constants.h"
24 #include "ixheaacd_basic_ops32.h"
25 #include "ixheaacd_basic_ops16.h"
26 #include "ixheaacd_basic_ops40.h"
27 #include "ixheaacd_sbr_common.h"
28 
29 #include "ixheaacd_bitbuffer.h"
30 #include "ixheaacd_defines.h"
31 #include "ixheaacd_aac_rom.h"
32 
33 #include "ixheaacd_sbrdecsettings.h"
34 #include "ixheaacd_sbr_scale.h"
35 #include "ixheaacd_env_extr_part.h"
36 #include "ixheaacd_sbr_rom.h"
37 
38 #include "ixheaacd_lpp_tran.h"
39 #include "ixheaacd_hybrid.h"
40 #include "ixheaacd_ps_dec.h"
41 
42 #include "ixheaacd_env_extr.h"
43 #include "ixheaacd_common_rom.h"
44 
45 #include "ixheaacd_pulsedata.h"
46 
47 #include "ixheaacd_pns.h"
48 #include "ixheaacd_drc_data_struct.h"
49 
50 #include "ixheaacd_lt_predict.h"
51 
52 #include "ixheaacd_cnst.h"
53 #include "ixheaacd_ec_defines.h"
54 #include "ixheaacd_ec_struct_def.h"
55 #include "ixheaacd_channelinfo.h"
56 #include "ixheaacd_drc_dec.h"
57 #include "ixheaacd_sbrdecoder.h"
58 #include "ixheaacd_block.h"
59 #include "ixheaacd_channel.h"
60 
61 #include "ixheaacd_adts.h"
62 #include "ixheaacd_audioobjtypes.h"
63 #include "ixheaacd_sbrdecoder.h"
64 #include "ixheaacd_memory_standards.h"
65 
66 #include "ixheaacd_latmdemux.h"
67 
68 #include "ixheaacd_aacdec.h"
69 #include "ixheaacd_mps_polyphase.h"
70 #include "ixheaacd_config.h"
71 #include "ixheaacd_qmf_dec.h"
72 #include "ixheaacd_mps_macro_def.h"
73 #include "ixheaacd_mps_struct_def.h"
74 #include "ixheaacd_mps_res_rom.h"
75 #include "ixheaacd_mps_aac_struct.h"
76 #include "ixheaacd_mps_dec.h"
77 #include "ixheaacd_struct_def.h"
78 #include "ixheaacd_error_codes.h"
79 #include "ixheaacd_definitions.h"
80 #include "ixheaacd_adts_crc_check.h"
81 
82 #include "ixheaacd_headerdecode.h"
83 
84 #include "ixheaacd_interface.h"
85 #include "ixheaacd_info.h"
86 #include "ixheaacd_mps_interface.h"
87 #include "ixheaacd_config.h"
88 
89 #include "ixheaacd_struct.h"
90 #include "ixheaacd_function_selector.h"
91 #include "ixheaacd_ld_mps_dec.h"
92 
93 #include "ixheaacd_error_standards.h"
94 
95 #define ELDEXT_SAOC 1
96 #define ELDEXT_TERM 0
97 #define ELDEXT_LDSAC 2
98 
99 extern const WORD32 ixheaacd_sampl_freq_idx_table[17];
100 
101 #define AAC_LC_PROFILE (2)
102 
103 #define ADTS_HEADER_LENGTH 7
104 
105 static PLATFORM_INLINE VOID
ixheaacd_aac_bytealign(struct ia_bit_buf_struct * it_bit_buff)106 ixheaacd_aac_bytealign(struct ia_bit_buf_struct *it_bit_buff) {
107   WORD16 num_bit;
108   num_bit = (it_bit_buff->bit_pos + 1);
109   if (num_bit != 8) {
110     it_bit_buff->bit_pos = 7;
111     it_bit_buff->cnt_bits -= num_bit;
112     it_bit_buff->ptr_read_next += 1;
113   }
114 }
115 
ixheaacd_read_pce_channel_info(WORD32 ch,WORD8 * ptr_is_cpe,WORD8 * ptr_tag_select,struct ia_bit_buf_struct * it_bit_buff)116 WORD32 ixheaacd_read_pce_channel_info(WORD32 ch, WORD8 *ptr_is_cpe,
117                                       WORD8 *ptr_tag_select,
118                                       struct ia_bit_buf_struct *it_bit_buff) {
119   WORD32 num_ch = 0, i, tmp;
120   for (i = 0; i < ch; i++) {
121     tmp = ixheaacd_read_bits_buf(it_bit_buff, 5);
122     ptr_is_cpe[i] = (tmp & 0x10) >> 4;
123 
124     if (ptr_is_cpe[i]) {
125       num_ch += 2;
126     } else {
127       num_ch++;
128     }
129 
130     ptr_tag_select[i] = (tmp & 0xF);
131   }
132   return num_ch;
133 }
134 
ixheaacd_read_pce_mixdown_data(struct ia_bit_buf_struct * it_bit_buff,WORD32 mix_down_present,WORD32 mix_down_element_no)135 VOID ixheaacd_read_pce_mixdown_data(struct ia_bit_buf_struct *it_bit_buff,
136                                     WORD32 mix_down_present,
137                                     WORD32 mix_down_element_no) {
138   WORD32 mix_down_flag = ixheaacd_read_bits_buf(it_bit_buff, mix_down_present);
139   if (mix_down_flag == 1) {
140     ixheaacd_read_bits_buf(it_bit_buff, mix_down_element_no);
141   }
142 }
143 
ixheaacd_skip_bits(struct ia_bit_buf_struct * it_bit_buff,WORD32 bits,WORD32 num_element)144 VOID ixheaacd_skip_bits(struct ia_bit_buf_struct *it_bit_buff, WORD32 bits,
145                         WORD32 num_element) {
146   WORD32 i;
147   for (i = 0; i < num_element; i++) {
148     ixheaacd_read_bits_buf(it_bit_buff, bits);
149   }
150 }
151 
ixheaacd_read_prog_config_element(ia_program_config_struct * ptr_config_element,struct ia_bit_buf_struct * it_bit_buff)152 WORD32 ixheaacd_read_prog_config_element(
153     ia_program_config_struct *ptr_config_element,
154     struct ia_bit_buf_struct *it_bit_buff) {
155   WORD32 i, tmp;
156   WORD count = 0, num_ch = 0;
157 
158   tmp = ixheaacd_read_bits_buf(it_bit_buff, 6);
159 
160   ptr_config_element->element_instance_tag = (tmp >> 2);
161   ptr_config_element->object_type = tmp & 0x3;
162 
163   if ((ptr_config_element->object_type + 1) != 2
164 
165       && (ptr_config_element->object_type + 1) != 4
166 
167       ) {
168     return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
169   }
170 
171   ptr_config_element->samp_freq_index = ixheaacd_read_bits_buf(it_bit_buff, 4);
172   if (ptr_config_element->samp_freq_index > 11) {
173     return IA_XHEAAC_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
174   }
175 
176   tmp = ixheaacd_read_bits_buf(it_bit_buff, 21);
177 
178   count += ptr_config_element->num_front_channel_elements = (tmp >> 17);
179   count += ptr_config_element->num_side_channel_elements =
180       (tmp & 0x1E000) >> 13;
181   count += ptr_config_element->num_back_channel_elements = (tmp & 0x1E00) >> 9;
182   count += ptr_config_element->num_lfe_channel_elements = (tmp & 0x180) >> 7;
183   ptr_config_element->num_assoc_data_elements = (tmp & 0x70) >> 4;
184   count += ptr_config_element->num_valid_cc_elements = tmp & 0xF;
185 
186   if (count > MAX_BS_ELEMENT) {
187     return IA_XHEAAC_DEC_INIT_FATAL_STREAM_CHAN_GT_MAX;
188   }
189 
190   ixheaacd_read_pce_mixdown_data(it_bit_buff, 1, 4);
191   ixheaacd_read_pce_mixdown_data(it_bit_buff, 1, 4);
192   ixheaacd_read_pce_mixdown_data(it_bit_buff, 1, 3);
193 
194   num_ch += ixheaacd_read_pce_channel_info(
195       ptr_config_element->num_front_channel_elements,
196       ptr_config_element->front_element_is_cpe,
197       ptr_config_element->front_element_tag_select, it_bit_buff);
198 
199   num_ch += ixheaacd_read_pce_channel_info(
200       ptr_config_element->num_side_channel_elements,
201       ptr_config_element->side_element_is_cpe,
202       ptr_config_element->side_element_tag_select, it_bit_buff);
203 
204   num_ch += ixheaacd_read_pce_channel_info(
205       ptr_config_element->num_back_channel_elements,
206       ptr_config_element->back_element_is_cpe,
207       ptr_config_element->back_element_tag_select, it_bit_buff);
208 
209   num_ch += ptr_config_element->num_lfe_channel_elements;
210 
211   for (i = 0; i < (ptr_config_element->num_lfe_channel_elements); i++) {
212     ptr_config_element->lfe_element_tag_select[i] =
213         ixheaacd_read_bits_buf(it_bit_buff, 4);
214   }
215 
216   ptr_config_element->channels = num_ch;
217 
218   for (i = 0; i < (ptr_config_element->num_assoc_data_elements); i++) {
219     ixheaacd_read_bits_buf(it_bit_buff, 4);
220   }
221 
222   ixheaacd_skip_bits(it_bit_buff, 5, ptr_config_element->num_valid_cc_elements);
223 
224   {
225     WORD32 bits_to_read = ptr_config_element->alignment_bits;
226     if (bits_to_read <= it_bit_buff->bit_pos) {
227       bits_to_read = it_bit_buff->bit_pos - bits_to_read;
228     } else {
229       bits_to_read = 8 - (bits_to_read) + it_bit_buff->bit_pos;
230     }
231     tmp = ixheaacd_read_bits_buf(it_bit_buff, bits_to_read);
232   }
233   tmp = ixheaacd_read_bits_buf(it_bit_buff, 8);
234 
235   ixheaacd_skip_bits(it_bit_buff, 8, tmp);
236 
237   return 0;
238 }
239 
ixheaacd_decode_pce(struct ia_bit_buf_struct * it_bit_buff,UWORD32 * ui_pce_found_in_hdr,ia_program_config_struct * ptr_prog_config)240 WORD ixheaacd_decode_pce(struct ia_bit_buf_struct *it_bit_buff,
241                          UWORD32 *ui_pce_found_in_hdr,
242                          ia_program_config_struct *ptr_prog_config) {
243   WORD32 error_code = 0;
244 
245   if (*ui_pce_found_in_hdr == 1 || *ui_pce_found_in_hdr == 3) {
246     ia_program_config_struct ptr_config_element = {0};
247     ptr_config_element.alignment_bits = ptr_prog_config->alignment_bits;
248     error_code =
249         ixheaacd_read_prog_config_element(&ptr_config_element, it_bit_buff);
250     *ui_pce_found_in_hdr = 3;
251   } else {
252     error_code =
253         ixheaacd_read_prog_config_element(ptr_prog_config, it_bit_buff);
254     *ui_pce_found_in_hdr = 2;
255   }
256   return error_code;
257 }
258 
ixheaacd_get_adif_header(ia_adif_header_struct * adif,struct ia_bit_buf_struct * it_bit_buff)259 static PLATFORM_INLINE WORD32 ixheaacd_get_adif_header(
260     ia_adif_header_struct *adif, struct ia_bit_buf_struct *it_bit_buff) {
261   WORD32 i;
262   WORD32 ret_val = 0, tmp;
263 
264   ixheaacd_read_bits_buf(it_bit_buff, 16);
265   tmp = ixheaacd_read_bits_buf(it_bit_buff, 17);
266 
267   if (tmp & 0x1) {
268     ixheaacd_skip_bits(it_bit_buff, 8, 9);
269   }
270 
271   tmp = ixheaacd_read_bits_buf(it_bit_buff, 3);
272 
273   adif->bit_stream_type = (tmp & 0x1);
274 
275   ixheaacd_read_bits_buf(it_bit_buff, 23);
276 
277   tmp = ixheaacd_read_bits_buf(it_bit_buff, 4);
278 
279   for (i = 0; i <= tmp; i++) {
280     if (adif->bit_stream_type == 0) {
281       ixheaacd_read_bits_buf(it_bit_buff, 20);
282     }
283 
284     adif->prog_config_present = 1;
285     adif->str_prog_config.alignment_bits = 7;
286     ret_val =
287         ixheaacd_read_prog_config_element(&adif->str_prog_config, it_bit_buff);
288     if (ret_val) {
289       return ret_val;
290     }
291   }
292 
293   return 0;
294 }
295 
ixheaacd_find_syncword(ia_adts_header_struct * adts,struct ia_bit_buf_struct * it_bit_buff)296 WORD32 ixheaacd_find_syncword(ia_adts_header_struct *adts,
297                               struct ia_bit_buf_struct *it_bit_buff) {
298   adts->sync_word = (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 12);
299   if (adts->sync_word == 0xFFF) {
300     return 0;
301   }
302 
303   while (1) {
304     ixheaacd_read_bidirection(it_bit_buff, -4);
305     if (it_bit_buff->cnt_bits < 12) {
306       return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
307     }
308     adts->sync_word = (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 12);
309     if (adts->sync_word == 0xFFF) {
310       ixheaacd_read_bidirection(it_bit_buff, -12);
311       return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
312     }
313   }
314 }
315 
ixheaacd_adtsframe(ia_adts_header_struct * adts,struct ia_bit_buf_struct * it_bit_buff)316 WORD32 ixheaacd_adtsframe(ia_adts_header_struct *adts,
317                           struct ia_bit_buf_struct *it_bit_buff) {
318   WORD32 tmp;
319   IA_ERRORCODE err = IA_NO_ERROR;
320   WORD32 crc_reg;
321   ia_adts_crc_info_struct *ptr_adts_crc_info = it_bit_buff->pstr_adts_crc_info;
322   ptr_adts_crc_info->crc_active = 1;
323   ptr_adts_crc_info->no_reg = 0;
324   ixheaacd_read_bidirection(it_bit_buff, -12);
325   crc_reg = ixheaacd_adts_crc_start_reg(ptr_adts_crc_info, it_bit_buff,
326                                         CRC_ADTS_HEADER_LEN);
327   err = ixheaacd_find_syncword(adts, it_bit_buff);
328   if (err) return err;
329 
330   tmp = ixheaacd_read_bits_buf(it_bit_buff, 10);
331 
332   adts->id = (tmp & 0x200) >> 9;
333   adts->layer = (tmp & 0x180) >> 7;
334   adts->protection_absent = (tmp & 0x40) >> 6;
335   adts->profile = (tmp & 0x30) >> 4;
336   { adts->profile++; }
337   adts->samp_freq_index = (tmp & 0xF);
338 
339   if (((adts->profile != AAC_LC_PROFILE)) || (adts->samp_freq_index > 11))
340 
341   {
342     return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
343   }
344 
345   tmp = ixheaacd_read_bits_buf(it_bit_buff, 21);
346 
347   adts->channel_configuration = (WORD32)(tmp & 0xE0000) >> 17;
348 
349   adts->aac_frame_length = (tmp & 0x1FFF);
350 
351   tmp = ixheaacd_read_bits_buf(it_bit_buff, 13);
352 
353   adts->no_raw_data_blocks = (tmp & 0x3);
354 
355   ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff, crc_reg);
356 
357   if (adts->protection_absent == 0) {
358     ixheaacd_skip_bits(it_bit_buff, 16, adts->no_raw_data_blocks);
359     adts->crc_check = ixheaacd_read_bits_buf(it_bit_buff, 16);
360 
361     ptr_adts_crc_info->crc_active = 1;
362     ptr_adts_crc_info->file_value = adts->crc_check;
363   } else
364     ptr_adts_crc_info->crc_active = 0;
365 
366   ixheaacd_aac_bytealign(it_bit_buff);
367   return 0;
368 }
369 
ixheaacd_get_samp_rate(struct ia_bit_buf_struct * it_bit_buff,ia_sampling_rate_info_struct * pstr_samp_rate_info,ia_audio_specific_config_struct * pstr_audio_specific_config)370 WORD32 ixheaacd_get_samp_rate(
371     struct ia_bit_buf_struct *it_bit_buff,
372     ia_sampling_rate_info_struct *pstr_samp_rate_info,
373     ia_audio_specific_config_struct *pstr_audio_specific_config) {
374   WORD32 index;
375   WORD32 sampling_rate;
376   index = ixheaacd_read_bits_buf(it_bit_buff, 4);
377   pstr_audio_specific_config->samp_frequency_index = index;
378 
379   if (index == 0x0F) {
380     sampling_rate = ixheaacd_read_bits_buf(it_bit_buff, 24);
381 
382     if (pstr_audio_specific_config->audio_object_type != AOT_USAC) {
383       if (sampling_rate < 9391)
384         sampling_rate = 8000;
385       else if ((sampling_rate >= 9391) && (sampling_rate < 11502))
386         sampling_rate = 11025;
387       else if ((sampling_rate >= 11502) && (sampling_rate < 13856))
388         sampling_rate = 12000;
389       else if ((sampling_rate >= 13856) && (sampling_rate < 18783))
390         sampling_rate = 16000;
391       else if ((sampling_rate >= 18783) && (sampling_rate < 23004))
392         sampling_rate = 22050;
393       else if ((sampling_rate >= 23004) && (sampling_rate < 27713))
394         sampling_rate = 24000;
395       else if ((sampling_rate >= 27713) && (sampling_rate < 37566))
396         sampling_rate = 32000;
397       else if ((sampling_rate >= 37566) && (sampling_rate < 46009))
398         sampling_rate = 44100;
399       else if ((sampling_rate >= 46009) && (sampling_rate < 55426))
400         sampling_rate = 48000;
401       else if ((sampling_rate >= 55426) && (sampling_rate < 75132))
402         sampling_rate = 64000;
403       else if ((sampling_rate >= 75132) && (sampling_rate < 92017))
404         sampling_rate = 88200;
405       else if (sampling_rate >= 92017)
406         sampling_rate = 96000;
407     }
408     return sampling_rate;
409   } else if ((index > 12) && (index < 15)) {
410     return -1;
411   } else {
412     return ((pstr_samp_rate_info[index].sampling_frequency));
413   }
414 }
ixheaacd_get_ld_sbr_header(ia_bit_buf_struct * it_bit_buff,ia_sbr_header_data_struct * sbr_header_data)415 static int ixheaacd_get_ld_sbr_header(
416     ia_bit_buf_struct *it_bit_buff,
417     ia_sbr_header_data_struct *sbr_header_data) {
418   WORD32 header_extra_1, header_extra_2;
419   UWORD32 tmp, bit_cnt = 0;
420 
421   tmp = ixheaacd_read_bits_buf(it_bit_buff, 16);
422   bit_cnt += 16;
423 
424   sbr_header_data->amp_res = (tmp & 0x8000) >> 15;
425   sbr_header_data->start_freq = (tmp & 0x7800) >> 11;
426   sbr_header_data->stop_freq = (tmp & 0x780) >> 7;
427   sbr_header_data->xover_band = (tmp & 0x70) >> 4;
428   header_extra_1 = (tmp & 0x0002) >> 1;
429   header_extra_2 = (tmp & 0x0001);
430 
431   if (header_extra_1) {
432     sbr_header_data->freq_scale = ixheaacd_read_bits_buf(it_bit_buff, 2);
433     sbr_header_data->alter_scale = ixheaacd_read_bits_buf(it_bit_buff, 1);
434     sbr_header_data->noise_bands = ixheaacd_read_bits_buf(it_bit_buff, 2);
435   } else {
436     sbr_header_data->freq_scale = 2;
437     sbr_header_data->alter_scale = 1;
438     sbr_header_data->noise_bands = 2;
439   }
440 
441   if (header_extra_2) {
442     sbr_header_data->limiter_bands = ixheaacd_read_bits_buf(it_bit_buff, 2);
443     sbr_header_data->limiter_gains = ixheaacd_read_bits_buf(it_bit_buff, 2);
444     sbr_header_data->interpol_freq = ixheaacd_read_bits_buf(it_bit_buff, 1);
445     sbr_header_data->smoothing_mode = ixheaacd_read_bits_buf(it_bit_buff, 1);
446   } else {
447     sbr_header_data->limiter_bands = 2;
448     sbr_header_data->limiter_gains = 2;
449     sbr_header_data->interpol_freq = 1;
450     sbr_header_data->smoothing_mode = 1;
451   }
452 
453   return (bit_cnt);
454 }
455 
ixheaacd_eld_sbr_header(ia_bit_buf_struct * it_bit_buff,WORD32 channels,ia_sbr_header_data_struct * pstr_sbr_config)456 WORD32 ixheaacd_eld_sbr_header(ia_bit_buf_struct *it_bit_buff, WORD32 channels,
457                                ia_sbr_header_data_struct *pstr_sbr_config) {
458   int num_sbr_header, el, bit_cnt = 0;
459   switch (channels) {
460     default:
461       num_sbr_header = 0;
462       break;
463     case 1:
464     case 2:
465       num_sbr_header = 1;
466       break;
467     case 3:
468       num_sbr_header = 2;
469       break;
470     case 4:
471     case 5:
472     case 6:
473       num_sbr_header = 3;
474       break;
475     case 7:
476       num_sbr_header = 4;
477       break;
478   }
479   for (el = 0; el < num_sbr_header; el++) {
480     bit_cnt = ixheaacd_get_ld_sbr_header(it_bit_buff, pstr_sbr_config);
481   }
482   return (bit_cnt);
483 }
484 
ixheaacd_ga_hdr_dec(ia_aac_dec_state_struct * aac_state_struct,WORD32 header_len,WORD32 * bytes_consumed,ia_sampling_rate_info_struct * pstr_samp_rate_info,struct ia_bit_buf_struct * it_bit_buff)485 WORD32 ixheaacd_ga_hdr_dec(ia_aac_dec_state_struct *aac_state_struct,
486                            WORD32 header_len, WORD32 *bytes_consumed,
487                            ia_sampling_rate_info_struct *pstr_samp_rate_info,
488                            struct ia_bit_buf_struct *it_bit_buff) {
489   WORD32 tmp;
490   WORD32 cnt_bits = it_bit_buff->cnt_bits;
491   UWORD32 aot_init;
492   UWORD32 tmp_aot;
493 
494   ia_audio_specific_config_struct *pstr_audio_specific_config;
495 
496   memset(aac_state_struct->ia_audio_specific_config, 0,
497          sizeof(ia_audio_specific_config_struct));
498 
499   memset(&(aac_state_struct->eld_specific_config), 0,
500          sizeof(ia_eld_specific_config_struct));
501 
502   pstr_audio_specific_config = aac_state_struct->ia_audio_specific_config;
503 
504   aac_state_struct->p_config->str_prog_config.alignment_bits =
505       it_bit_buff->bit_pos;
506 
507   aot_init = aac_state_struct->audio_object_type;
508   aac_state_struct->frame_length = FRAME_SIZE;
509 
510   tmp_aot = ixheaacd_read_bits_buf(it_bit_buff, 5);
511 
512   if (tmp_aot == 31) {
513     tmp = ixheaacd_read_bits_buf(it_bit_buff, 6);
514     tmp_aot = 32 + tmp;
515   }
516 
517   if (aac_state_struct->header_dec_done || aac_state_struct->ui_init_done) {
518     if (tmp_aot != aot_init && tmp_aot != AOT_SBR && tmp_aot != AOT_PS)
519       return IA_FATAL_ERROR;
520   }
521 
522   pstr_audio_specific_config->audio_object_type =
523       aac_state_struct->audio_object_type = tmp_aot;
524 
525   tmp = ixheaacd_get_samp_rate(it_bit_buff, pstr_samp_rate_info,
526                                pstr_audio_specific_config);
527   pstr_audio_specific_config->sampling_frequency = tmp;
528 
529   if (tmp == -1) {
530     *bytes_consumed = 1;
531     return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
532   } else
533     aac_state_struct->sampling_rate = tmp;
534   aac_state_struct->p_config->ui_samp_freq = tmp;
535 
536   aac_state_struct->ch_config = ixheaacd_read_bits_buf(it_bit_buff, 4);
537 
538   pstr_audio_specific_config->channel_configuration =
539       aac_state_struct->ch_config;
540 
541   if (aac_state_struct->audio_object_type == AOT_SBR ||
542       aac_state_struct->audio_object_type == AOT_PS) {
543     tmp = ixheaacd_get_samp_rate(it_bit_buff, pstr_samp_rate_info,
544                                  pstr_audio_specific_config);
545     aac_state_struct->sbr_present_flag = 1;
546     if (tmp == -1) {
547       *bytes_consumed = 1;
548       return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
549     } else
550       aac_state_struct->extension_samp_rate = tmp;
551 
552     aac_state_struct->audio_object_type =
553         ixheaacd_read_bits_buf(it_bit_buff, 5);
554   }
555 
556   if (aac_state_struct->header_dec_done || aac_state_struct->ui_init_done) {
557     if (aac_state_struct->audio_object_type != aot_init) return IA_FATAL_ERROR;
558   }
559 
560   if (((aac_state_struct->audio_object_type >= AOT_AAC_MAIN &&
561         aac_state_struct->audio_object_type <= AOT_AAC_LTP) ||
562        aac_state_struct->audio_object_type == AOT_AAC_SCAL ||
563        aac_state_struct->audio_object_type == AOT_TWIN_VQ ||
564        aac_state_struct->audio_object_type == AOT_ER_AAC_LD ||
565        aac_state_struct->audio_object_type == AOT_ER_AAC_ELD ||
566        aac_state_struct->audio_object_type == AOT_ER_AAC_SCAL ||
567        aac_state_struct->audio_object_type == AOT_ER_AAC_LC) &&
568       aac_state_struct->audio_object_type != AOT_USAC)
569 
570   {
571     aac_state_struct->usac_flag = 0;
572 
573     aac_state_struct->frame_len_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
574     if (aac_state_struct->audio_object_type != AOT_ER_AAC_ELD) {
575       aac_state_struct->depends_on_core_coder =
576           ixheaacd_read_bits_buf(it_bit_buff, 1);
577       aac_state_struct->extension_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
578 
579       if (aac_state_struct->ch_config == 0) {
580         WORD32 error_code;
581         error_code = ixheaacd_read_prog_config_element(
582             &aac_state_struct->p_config->str_prog_config, it_bit_buff);
583         if (error_code != 0) {
584           *bytes_consumed = 1;
585           return error_code;
586         }
587         aac_state_struct->p_config->ui_pce_found_in_hdr = 1;
588       }
589     }
590     if (aac_state_struct->audio_object_type == AOT_ER_AAC_ELD ||
591         aac_state_struct->audio_object_type == AOT_ER_AAC_LD ||
592         aac_state_struct->audio_object_type == AOT_ER_AAC_LC ||
593         aac_state_struct->audio_object_type == AOT_ER_AAC_SCAL) {
594       aac_state_struct->eld_specific_config.aac_sect_data_resil_flag = 0;
595       aac_state_struct->eld_specific_config.aac_sf_data_resil_flag = 0;
596       aac_state_struct->eld_specific_config.aac_spect_data_resil_flag = 0;
597       aac_state_struct->eld_specific_config.ep_config = 0;
598       if ((aac_state_struct->extension_flag == 1) ||
599           aac_state_struct->audio_object_type == AOT_ER_AAC_ELD) {
600         if (aac_state_struct->audio_object_type >= ER_OBJECT_START) {
601           aac_state_struct->eld_specific_config.aac_sect_data_resil_flag =
602               ixheaacd_read_bits_buf(it_bit_buff, 1);
603           aac_state_struct->eld_specific_config.aac_sf_data_resil_flag =
604               ixheaacd_read_bits_buf(it_bit_buff, 1);
605           aac_state_struct->eld_specific_config.aac_spect_data_resil_flag =
606               ixheaacd_read_bits_buf(it_bit_buff, 1);
607           if (aac_state_struct->audio_object_type == AOT_ER_AAC_LD)
608             aac_state_struct->eld_specific_config.ep_config =
609                 ixheaacd_read_bits_buf(it_bit_buff, 2);
610           if (aac_state_struct->audio_object_type == AOT_ER_AAC_ELD)
611             aac_state_struct->eld_specific_config.ld_sbr_flag_present =
612                 ixheaacd_read_bits_buf(it_bit_buff, 1);
613         }
614       }
615     }
616   }
617   if (pstr_audio_specific_config->audio_object_type == AOT_USAC) {
618     {
619       pstr_audio_specific_config->sbr_present_flag = 0;
620       pstr_audio_specific_config->ext_audio_object_type = 0;
621     }
622 
623     {
624       {
625         SIZE_T tmp = 0xf;
626         UWORD32 i;
627         WORD32 err = 0;
628 
629         aac_state_struct->usac_flag = 1;
630 
631         ixheaacd_conf_default(&(pstr_audio_specific_config->str_usac_config));
632         err = ixheaacd_config(it_bit_buff, &(pstr_audio_specific_config->str_usac_config),
633                               &(pstr_audio_specific_config->channel_configuration),
634                               aac_state_struct->ec_enable);
635         if (err != 0) return err;
636 
637         pstr_audio_specific_config->sampling_frequency =
638             pstr_audio_specific_config->str_usac_config.usac_sampling_frequency;
639 
640         if (pstr_audio_specific_config->audio_object_type == AOT_USAC) {
641           pstr_audio_specific_config->sbr_present_flag = 1;
642           pstr_audio_specific_config->ext_audio_object_type = AOT_SBR;
643           pstr_audio_specific_config->ext_sampling_frequency =
644               pstr_audio_specific_config->sampling_frequency;
645           pstr_audio_specific_config->ext_samp_frequency_index =
646               pstr_audio_specific_config->samp_frequency_index;
647 
648           for (i = 0; i < sizeof(ixheaacd_sampl_freq_idx_table) /
649                               sizeof(ixheaacd_sampl_freq_idx_table[0]);
650                i++) {
651             if (ixheaacd_sampl_freq_idx_table[i] ==
652                 (int)(pstr_audio_specific_config->sampling_frequency)) {
653               tmp = i;
654               break;
655             }
656           }
657           pstr_audio_specific_config->samp_frequency_index = (UINT32)tmp;
658         } else {
659           pstr_audio_specific_config->sbr_present_flag = 0;
660         }
661       }
662     }
663 
664     if (aac_state_struct->bs_format != LOAS_BSFORMAT) {
665       ixheaacd_skip_bits_buf(it_bit_buff, it_bit_buff->cnt_bits);
666 
667       if ((SIZE_T)it_bit_buff->ptr_read_next ==
668           (SIZE_T)it_bit_buff->ptr_bit_buf_base) {
669         *bytes_consumed = ((WORD32)it_bit_buff->size) >> 3;
670       } else {
671         *bytes_consumed = (WORD32)((((((SIZE_T)it_bit_buff->ptr_read_next -
672                               (SIZE_T)it_bit_buff->ptr_bit_buf_base))
673                             << 3) +
674                             7 - it_bit_buff->bit_pos + 7) >>
675                             3);
676       }
677     }
678     return 0;
679   }
680 
681   aac_state_struct->frame_length = FRAME_SIZE;
682   if (aac_state_struct->frame_len_flag)
683     aac_state_struct->frame_length = FRAME_SIZE_SMALL;
684 
685   if (aac_state_struct->extension_flag)
686     aac_state_struct->extension_flag_3 = ixheaacd_read_bits_buf(it_bit_buff, 1);
687 
688   if (aac_state_struct->audio_object_type == AOT_ER_AAC_LD)
689     aac_state_struct->frame_length >>= 1;
690 
691   if (aac_state_struct->audio_object_type == AOT_ER_AAC_ELD) {
692     aac_state_struct->frame_length >>= 1;
693     if (aac_state_struct->eld_specific_config.ld_sbr_flag_present) {
694       aac_state_struct->eld_specific_config.ld_sbr_samp_rate =
695           ixheaacd_read_bits_buf(it_bit_buff, 1);
696       aac_state_struct->eld_specific_config.ld_sbr_crc_flag =
697           ixheaacd_read_bits_buf(it_bit_buff, 1);
698 
699       ixheaacd_eld_sbr_header(it_bit_buff, aac_state_struct->ch_config,
700                               &aac_state_struct->str_sbr_config);
701 
702       aac_state_struct->dwnsmp_signal =
703           !aac_state_struct->eld_specific_config.ld_sbr_samp_rate;
704     }
705 
706     {
707       UWORD16 len, eld_ext_type;
708       WORD32 err;
709       UWORD16 cnt;
710 
711       if (ixheaacd_show_bits_buf(it_bit_buff, 4) != ELDEXT_TERM) {
712         while ((eld_ext_type = ixheaacd_read_bits_buf(it_bit_buff, 4)) !=
713                ELDEXT_TERM) {
714           len = ixheaacd_read_bits_buf(it_bit_buff, 4);
715           if (len == 15) {
716             len += ixheaacd_read_bits_buf(it_bit_buff, 8);
717           }
718           if (len == 15 + 255) {
719             len += ixheaacd_read_bits_buf(it_bit_buff, 16);
720           }
721 
722           switch (eld_ext_type) {
723             case ELDEXT_SAOC:
724               break;
725             case ELDEXT_LDSAC:
726               aac_state_struct->ldmps_present = 1;
727               aac_state_struct->mps_dec_handle.ldmps_config.ldmps_present_flag =
728                   1;
729               aac_state_struct->mps_dec_handle.object_type =
730                   aac_state_struct->audio_object_type;
731               aac_state_struct->mps_dec_handle.ec_flag = aac_state_struct->ec_enable;
732               err = ixheaacd_ld_spatial_specific_config(
733                   &(aac_state_struct->mps_dec_handle.ldmps_config), it_bit_buff);
734               if (err) return err;
735 
736               if (ixheaacd_mps_create(
737                       &aac_state_struct->mps_dec_handle,
738                       aac_state_struct->mps_dec_handle.ldmps_config
739                           .bs_frame_length,
740                       0, &(aac_state_struct->mps_dec_handle.ldmps_config))) {
741                 return -1;
742               }
743               break;
744             default:
745               for (cnt = 0; cnt < len; cnt++) {
746                 ixheaacd_read_bits_buf(it_bit_buff, 8);
747               }
748 
749               break;
750           }
751           break;
752         }
753       }
754     }
755     if (!((aac_state_struct->ldmps_present == 1) &&
756           !(it_bit_buff->cnt_bits >= 1)))
757     ixheaacd_read_bits_buf(it_bit_buff, 1);
758   }
759   if (!((aac_state_struct->ldmps_present == 1) && !(it_bit_buff->cnt_bits > 0))) {
760     if (aac_state_struct->audio_object_type == AOT_ER_AAC_ELD ||
761         aac_state_struct->audio_object_type == AOT_ER_AAC_LC ||
762         aac_state_struct->audio_object_type == AOT_ER_AAC_SCAL) {
763       WORD32 ep_config = ixheaacd_read_bits_buf(it_bit_buff, 2);
764       if (ep_config == 2 || ep_config == 3) {
765       }
766       if (ep_config == 3) {
767         WORD32 direct_map = ixheaacd_read_bits_buf(it_bit_buff, 1);
768         if (!direct_map) {
769         }
770       }
771     }
772 
773     tmp = (header_len * 8) - it_bit_buff->cnt_bits;
774 
775     if (aac_state_struct->audio_object_type != AOT_SBR &&
776         (it_bit_buff->cnt_bits >= 16)) {
777       tmp = ixheaacd_read_bits_buf(it_bit_buff, 11);
778 
779       if (tmp == 0x2b7) {
780         tmp = ixheaacd_read_bits_buf(it_bit_buff, 5);
781 
782         if (tmp == AOT_SBR) {
783           WORD32 sbr_present_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
784           if (sbr_present_flag) {
785             tmp = ixheaacd_get_samp_rate(it_bit_buff, pstr_samp_rate_info,
786                                          pstr_audio_specific_config);
787             if (tmp == -1) {
788               *bytes_consumed = 1;
789               return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
790             } else
791               aac_state_struct->extension_samp_rate = tmp;
792 
793             if (it_bit_buff->cnt_bits >= 12) {
794               tmp = ixheaacd_read_bits_buf(it_bit_buff, 11);
795               if (tmp == 0x548) {
796                 tmp = ixheaacd_read_bits_buf(it_bit_buff, 1);
797               }
798             }
799           }
800         }
801       } else if (aac_state_struct->bs_format == LOAS_BSFORMAT) {
802         ixheaacd_read_bidirection(it_bit_buff, -11);
803       }
804     }
805   }
806 
807   if (aac_state_struct->audio_object_type != AOT_AAC_LC &&
808       aac_state_struct->audio_object_type != AOT_SBR &&
809       aac_state_struct->audio_object_type != AOT_PS &&
810       aac_state_struct->audio_object_type != AOT_ER_AAC_LC &&
811       aac_state_struct->audio_object_type != AOT_ER_AAC_LD &&
812       aac_state_struct->audio_object_type != AOT_ER_AAC_ELD &&
813       aac_state_struct->audio_object_type != AOT_AAC_LTP) {
814     *bytes_consumed = 1;
815     return IA_XHEAAC_DEC_INIT_FATAL_AUDIOOBJECTTYPE_NOT_SUPPORTED;
816   } else {
817     if (aac_state_struct->bs_format == LOAS_BSFORMAT) {
818       *bytes_consumed = (cnt_bits - it_bit_buff->cnt_bits) >> 3;
819       if (it_bit_buff->bit_pos < 7) *bytes_consumed += 1;
820 
821     } else
822       *bytes_consumed = header_len;
823 
824     return 0;
825   }
826 }
827 
ixheaacd_check_if_adts(ia_adts_header_struct * adts,struct ia_bit_buf_struct * it_bit_buff,WORD32 usr_max_ch)828 WORD32 ixheaacd_check_if_adts(ia_adts_header_struct *adts,
829                               struct ia_bit_buf_struct *it_bit_buff,
830                               WORD32 usr_max_ch) {
831   WORD32 max_frm_len_per_ch, result = 0;
832 
833   result = ixheaacd_adtsframe(adts, it_bit_buff);
834 
835   max_frm_len_per_ch = ixheaacd_mult32(768, (adts->no_raw_data_blocks + 1));
836 
837   if (adts->channel_configuration != 0)
838     max_frm_len_per_ch =
839         ixheaacd_mult32(max_frm_len_per_ch, adts->channel_configuration);
840   else
841     max_frm_len_per_ch = max_frm_len_per_ch * usr_max_ch;
842 
843   return ((result != 0) || (adts->aac_frame_length < 8) || (adts->layer != 0) ||
844           (adts->profile != AAC_LC_PROFILE));
845 }
846 
ixheaacd_latm_header_decode(ia_aac_dec_state_struct * aac_state_struct,struct ia_bit_buf_struct * it_bit_buff,WORD32 * bytes_consumed,ia_sampling_rate_info_struct * pstr_samp_rate_info)847 IA_ERRORCODE ixheaacd_latm_header_decode(
848     ia_aac_dec_state_struct *aac_state_struct,
849     struct ia_bit_buf_struct *it_bit_buff, WORD32 *bytes_consumed,
850     ia_sampling_rate_info_struct *pstr_samp_rate_info) {
851   WORD32 sync;
852   IA_ERRORCODE result;
853   WORD32 next_sync, audio_mux_len_bytes_last;
854   WORD32 audio_mux_len_bits_last;
855   WORD32 sync_status = aac_state_struct->sync_status;
856   WORD32 bit_count = aac_state_struct->bit_count;
857   WORD32 cnt_bits = it_bit_buff->cnt_bits;
858 
859   *bytes_consumed = 0;
860 
861   aac_state_struct->bs_format = LOAS_BSFORMAT;
862 
863   if (sync_status == 0) {
864     do {
865       sync = ixheaacd_read_bits_buf(it_bit_buff, 11);
866       bit_count += 11;
867       while (sync != 0x2B7) {
868         sync = ((sync & 0x3ff) << 1) | ixheaacd_read_bits_buf(it_bit_buff, 1);
869         bit_count += 1;
870         if (it_bit_buff->cnt_bits < 13) {
871           ixheaacd_read_bidirection(it_bit_buff, -11);
872           *bytes_consumed = (cnt_bits - it_bit_buff->cnt_bits) / 8;
873           return (IA_XHEAAC_DEC_INIT_NONFATAL_HEADER_NOT_AT_START);
874         }
875       }
876 
877       audio_mux_len_bytes_last = ixheaacd_read_bits_buf(it_bit_buff, 13);
878       bit_count += 13;
879       audio_mux_len_bits_last = audio_mux_len_bytes_last << 3;
880       if (it_bit_buff->cnt_bits >= (audio_mux_len_bits_last + 11)) {
881         ixheaacd_read_bidirection(it_bit_buff, audio_mux_len_bits_last);
882         bit_count += audio_mux_len_bits_last;
883 
884         next_sync = ixheaacd_read_bits_buf(it_bit_buff, 11);
885         bit_count += 11;
886 
887         if (next_sync == 0x2B7) {
888           ixheaacd_read_bidirection(it_bit_buff,
889                                     -(11 + audio_mux_len_bits_last + 13 + 11));
890           bit_count -= 11 + audio_mux_len_bits_last + 13 + 11;
891           break;
892         } else {
893           ixheaacd_read_bidirection(it_bit_buff,
894                                     -(audio_mux_len_bits_last + 24 + 11 - 1));
895           bit_count -= audio_mux_len_bits_last + 24 + 11 - 1;
896         }
897 
898       } else {
899         if (it_bit_buff->cnt_bits != audio_mux_len_bits_last) {
900           ixheaacd_read_bidirection(it_bit_buff, -(13 + 11));
901           bit_count -= (13 + 11);
902           *bytes_consumed = (cnt_bits - it_bit_buff->cnt_bits) / 8;
903           return IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
904         } else {
905           ixheaacd_read_bidirection(it_bit_buff, -(13 + 11));
906           bit_count -= (13 + 11);
907           break;
908         }
909       }
910     } while (1);
911 
912     do {
913       WORD32 audio_mux_len_bytes_last;
914       WORD32 use_same_stream_mux;
915 
916       sync = ixheaacd_read_bits_buf(it_bit_buff, 11);
917       bit_count += 11;
918 
919       if (sync != 0x2b7) {
920         ixheaacd_read_bidirection(it_bit_buff, -25);
921         bit_count -= 11;
922         *bytes_consumed = (cnt_bits - it_bit_buff->cnt_bits) / 8;
923         return IA_XHEAAC_DEC_INIT_NONFATAL_HEADER_NOT_AT_START;
924       }
925 
926       audio_mux_len_bytes_last = ixheaacd_read_bits_buf(it_bit_buff, 13);
927       bit_count += 13;
928 
929       use_same_stream_mux = ixheaacd_read_bits_buf(it_bit_buff, 1);
930       bit_count += 1;
931 
932       if (!use_same_stream_mux) {
933         ixheaacd_read_bidirection(it_bit_buff, -25);
934         bit_count -= 25;
935         sync_status = 1;
936         aac_state_struct->sync_status = sync_status;
937         break;
938       } else {
939         ixheaacd_read_bidirection(it_bit_buff, -25);
940         bit_count -= 25;
941         aac_state_struct->bit_count = bit_count;
942         *bytes_consumed = 1;
943         return IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
944       }
945 
946 
947       ixheaacd_read_bidirection(it_bit_buff, audio_mux_len_bytes_last * 8 - 1);
948       bit_count += audio_mux_len_bytes_last * 8 - 1;
949 
950     } while (sync_status == 0);
951 
952     *bytes_consumed = (cnt_bits - it_bit_buff->cnt_bits) / 8;
953     {
954       ixheaacd_latm_struct latm_struct_element;
955       WORD32 sync;
956       memset(&latm_struct_element, 0, sizeof(ixheaacd_latm_struct));
957 
958       sync = ixheaacd_read_bits_buf(it_bit_buff, 11);
959       if (sync == 0x2b7) {
960         result = ixheaacd_latm_audio_mux_element(
961             it_bit_buff, &latm_struct_element, aac_state_struct,
962             pstr_samp_rate_info);
963         if (result != 0) {
964           sync_status = 0;
965           aac_state_struct->sync_status = sync_status;
966 
967           *bytes_consumed += 1;
968           return result;
969         }
970       }
971     }
972   }
973   return IA_NO_ERROR;
974 }
975 
ixheaacd_aac_headerdecode(ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec,UWORD8 * buffer,WORD32 * bytes_consumed,const ia_aac_dec_huffman_tables_struct * pstr_huffmann_tables)976 WORD32 ixheaacd_aac_headerdecode(
977     ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec, UWORD8 *buffer,
978     WORD32 *bytes_consumed,
979     const ia_aac_dec_huffman_tables_struct *pstr_huffmann_tables) {
980   struct ia_bit_buf_struct it_bit_buff = {0}, *handle_bit_buff;
981   ia_adif_header_struct adif = {0};
982   ia_adts_header_struct adts = {0};
983   WORD32 result;
984   WORD32 header_len;
985   WORD32 sync = 0;
986 
987   WORD32 disable_sync = p_obj_exhaacplus_dec->aac_config.ui_disable_sync;
988   WORD32 is_ga_header = p_obj_exhaacplus_dec->aac_config.ui_mp4_flag;
989 
990   WORD32 loas_present = p_obj_exhaacplus_dec->aac_config.loas_present;
991 
992   ia_aac_dec_state_struct *aac_state_struct =
993       p_obj_exhaacplus_dec->pp_mem_aac[IA_ENHAACPLUS_DEC_PERSIST_IDX];
994   WORD32 usr_max_ch = aac_state_struct->p_config->ui_max_channels;
995 
996   ia_sampling_rate_info_struct *pstr_samp_rate_info =
997       (ia_sampling_rate_info_struct *)&pstr_huffmann_tables
998           ->str_sample_rate_info[0];
999 
1000   if (buffer == 0) {
1001     return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL;
1002   }
1003 
1004   header_len = aac_state_struct->ui_in_bytes;
1005 
1006   handle_bit_buff = ixheaacd_create_bit_buf(&it_bit_buff, (UWORD8 *)buffer,
1007                                             (WORD16)header_len);
1008   handle_bit_buff->cnt_bits += (header_len << 3);
1009   handle_bit_buff->xaac_jmp_buf = &aac_state_struct->xaac_jmp_buf;
1010 
1011   if (is_ga_header == 1) {
1012     return ixheaacd_ga_hdr_dec(aac_state_struct, header_len, bytes_consumed,
1013                                pstr_samp_rate_info, handle_bit_buff);
1014   } else if (loas_present) {
1015     return ixheaacd_latm_header_decode(aac_state_struct, &it_bit_buff,
1016                                        bytes_consumed, pstr_samp_rate_info);
1017   }
1018 
1019   else {
1020     WORD32 header_found = 0;
1021     WORD32 bytes_taken = -1;
1022     WORD32 prev_offset = 0;
1023     WORD32 run_once = 1;
1024     if (disable_sync == 0) run_once = 0;
1025 
1026     do {
1027       bytes_taken++;
1028       buffer += (bytes_taken - prev_offset);
1029 
1030       prev_offset = bytes_taken;
1031 
1032       handle_bit_buff = ixheaacd_create_bit_buf(
1033           &it_bit_buff, (UWORD8 *)buffer, (WORD16)(header_len - bytes_taken));
1034       handle_bit_buff->cnt_bits += (8 * (header_len - bytes_taken));
1035 
1036       handle_bit_buff->pstr_adts_crc_info = &handle_bit_buff->str_adts_crc_info;
1037       ixheaacd_adts_crc_open(handle_bit_buff->pstr_adts_crc_info);
1038 
1039       aac_state_struct->ldmps_present = 0;
1040       aac_state_struct->latm_initialized = 0;
1041       memset(&aac_state_struct->mps_dec_handle, 0,
1042              sizeof(aac_state_struct->mps_dec_handle));
1043       memset(&aac_state_struct->eld_specific_config, 0,
1044              sizeof(aac_state_struct->eld_specific_config));
1045 
1046       if ((buffer[0] == 'A') && (buffer[1] == 'D') && (buffer[2] == 'I') &&
1047           (buffer[3] == 'F')) {
1048         adif.prog_config_present = 0;
1049         result = ixheaacd_get_adif_header(&adif, handle_bit_buff);
1050         if (result == 0) {
1051           if (adif.prog_config_present == 1) {
1052             aac_state_struct->p_config->ui_pce_found_in_hdr = 1;
1053             aac_state_struct->p_config->str_prog_config = adif.str_prog_config;
1054           }
1055           aac_state_struct->s_adif_hdr_present = 1;
1056           aac_state_struct->audio_object_type =
1057               adif.str_prog_config.object_type;
1058           aac_state_struct->sampling_rate =
1059               pstr_samp_rate_info[adif.str_prog_config.samp_freq_index]
1060                   .sampling_frequency;
1061           aac_state_struct->ch_config = adif.str_prog_config.channels;
1062           bytes_taken +=
1063               ((handle_bit_buff->size - handle_bit_buff->cnt_bits) >> 3);
1064 
1065           header_found = 1;
1066           aac_state_struct->frame_length = FRAME_SIZE;
1067           if (aac_state_struct->audio_object_type == AOT_ER_AAC_LD)
1068             aac_state_struct->frame_length >>= 1;
1069         }
1070       }
1071 
1072       else if ((sync = ixheaacd_read_bits_buf(&it_bit_buff, 12)) == 0xfff) {
1073         result = ixheaacd_check_if_adts(&adts, handle_bit_buff, usr_max_ch);
1074         if (result != 0) {
1075           continue;
1076         }
1077 
1078         if ((adts.aac_frame_length + ADTS_HEADER_LENGTH) <
1079             (header_len - bytes_taken)) {
1080           ia_adts_header_struct adts_loc = {0};
1081 
1082           ixheaacd_create_init_bit_buf(
1083               &it_bit_buff, (UWORD8 *)(buffer + adts.aac_frame_length),
1084               (WORD16)(header_len - bytes_taken - adts.aac_frame_length));
1085 
1086           handle_bit_buff = &it_bit_buff;
1087 
1088           adts_loc.sync_word =
1089               (WORD16)ixheaacd_read_bits_buf(handle_bit_buff, 12);
1090 
1091           if (adts_loc.sync_word != 0xFFF) {
1092             continue;
1093           }
1094 
1095           result =
1096               ixheaacd_check_if_adts(&adts_loc, handle_bit_buff, usr_max_ch);
1097           if ((result != 0) ||
1098               (adts.samp_freq_index != adts_loc.samp_freq_index) ||
1099               (adts.channel_configuration != adts_loc.channel_configuration)) {
1100             continue;
1101           }
1102         }
1103 
1104         {
1105           WORD32 obj_type;
1106           obj_type = adts.profile;
1107 
1108           aac_state_struct->audio_object_type = obj_type;
1109           aac_state_struct->sampling_rate =
1110               ((pstr_samp_rate_info[adts.samp_freq_index].sampling_frequency));
1111           aac_state_struct->ch_config = adts.channel_configuration;
1112           aac_state_struct->s_adts_hdr_present = 1;
1113           header_found = 1;
1114           aac_state_struct->bs_format = ADTS_BSFORMAT;
1115           aac_state_struct->frame_length = FRAME_SIZE;
1116           if (aac_state_struct->audio_object_type == AOT_ER_AAC_LD)
1117             aac_state_struct->frame_length >>= 1;
1118         }
1119       } else if (0x2b7 == (sync >> 1)) {
1120         ixheaacd_read_bidirection(&it_bit_buff, -12);
1121         result =
1122             ixheaacd_latm_header_decode(aac_state_struct, &it_bit_buff,
1123                                         bytes_consumed, pstr_samp_rate_info);
1124         if (result != 0) {
1125           if ((result ==
1126                (WORD32)
1127                    IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES) ||
1128               (result ==
1129                (WORD32)IA_XHEAAC_DEC_INIT_FATAL_STREAM_CHAN_GT_MAX)) {
1130             bytes_taken += *bytes_consumed;
1131             *bytes_consumed = bytes_taken;
1132             return result;
1133           } else if (result == -1)
1134             return -1;
1135           else if (result == (WORD32)IA_FATAL_ERROR)
1136             return IA_FATAL_ERROR;
1137           else
1138             bytes_taken += *bytes_consumed - 1;
1139           continue;
1140         }
1141         header_found = 1;
1142         aac_state_struct->bs_format = LOAS_BSFORMAT;
1143         bytes_taken += *bytes_consumed;
1144       }
1145 
1146     } while ((header_found == 0 && ((bytes_taken + 1) < (header_len - 68))) &&
1147              run_once != 1);
1148 
1149     if (header_found == 0 && disable_sync == 1) {
1150       WORD32 err_code;
1151       ixheaacd_read_bidirection(&it_bit_buff, -12);
1152       err_code =
1153           ixheaacd_ga_hdr_dec(aac_state_struct, header_len, bytes_consumed,
1154                               pstr_samp_rate_info, handle_bit_buff);
1155 
1156       if (err_code == 0) p_obj_exhaacplus_dec->aac_config.ui_mp4_flag = 1;
1157       return err_code;
1158     }
1159 
1160     switch (aac_state_struct->audio_object_type) {
1161         case AOT_AAC_MAIN:
1162         case AOT_AAC_LC:
1163         case AOT_AAC_SSR:
1164         case AOT_AAC_LTP:
1165         case AOT_AAC_SCAL:
1166         case AOT_TWIN_VQ:
1167         case AOT_ER_AAC_LD:
1168         case AOT_ER_AAC_ELD:
1169         case AOT_ER_AAC_LC:
1170         case AOT_USAC:
1171           break;
1172         default:
1173           return IA_XHEAAC_DEC_INIT_FATAL_AUDIOOBJECTTYPE_NOT_SUPPORTED;
1174       }
1175 
1176     if (aac_state_struct->audio_object_type != AOT_USAC)
1177       aac_state_struct->usac_flag = 0;
1178     *bytes_consumed = bytes_taken;
1179 
1180     if ((handle_bit_buff->cnt_bits < 0) &&
1181         (handle_bit_buff->size <
1182          (usr_max_ch * (IA_ENHAACPLUS_DEC_INP_BUF_SIZE << 3)))) {
1183       return (WORD16)(
1184           (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
1185     }
1186 
1187     if (header_found == 0) {
1188       *bytes_consumed = bytes_taken + 1;
1189       return IA_XHEAAC_DEC_INIT_NONFATAL_HEADER_NOT_AT_START;
1190     } else {
1191       return 0;
1192     }
1193   }
1194 }
1195