• 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 <stdio.h>
22 #include <stdarg.h>
23 #include <assert.h>
24 
25 #include "ixheaacd_type_def.h"
26 #include "ixheaacd_constants.h"
27 #include "ixheaacd_bitbuffer.h"
28 
29 #include "ixheaacd_defines.h"
30 #include "ixheaacd_memory_standards.h"
31 #include "ixheaacd_sbrdecsettings.h"
32 #include "ixheaacd_sbr_scale.h"
33 #include "ixheaacd_env_extr_part.h"
34 #include "ixheaacd_aac_rom.h"
35 #include "ixheaacd_common_rom.h"
36 #include "ixheaacd_sbr_rom.h"
37 #include "ixheaacd_pulsedata.h"
38 #include "ixheaacd_pns.h"
39 
40 #include "ixheaacd_sbr_common.h"
41 #include "ixheaacd_drc_data_struct.h"
42 #include "ixheaacd_drc_dec.h"
43 
44 #include "ixheaacd_lt_predict.h"
45 #include "ixheaacd_cnst.h"
46 #include "ixheaacd_ec_defines.h"
47 #include "ixheaacd_ec_struct_def.h"
48 #include "ixheaacd_channelinfo.h"
49 #include "ixheaacd_channel.h"
50 #include "ixheaacd_sbrdecoder.h"
51 #include "ixheaacd_audioobjtypes.h"
52 #include "ixheaacd_latmdemux.h"
53 #include "ixheaacd_aacdec.h"
54 #include "ixheaacd_sbr_common.h"
55 
56 #include "ixheaacd_hybrid.h"
57 #include "ixheaacd_ps_dec.h"
58 #include "ixheaacd_qmf_dec.h"
59 #include "ixheaacd_mps_macro_def.h"
60 #include "ixheaacd_mps_struct_def.h"
61 #include "ixheaacd_mps_res_rom.h"
62 #include "ixheaacd_mps_aac_struct.h"
63 #include "ixheaacd_mps_polyphase.h"
64 #include "ixheaacd_config.h"
65 #include "ixheaacd_mps_dec.h"
66 #include "ixheaacd_struct_def.h"
67 
68 #include "ixheaacd_config.h"
69 #include "ixheaacd_interface.h"
70 #include "ixheaacd_info.h"
71 #include "ixheaacd_struct.h"
72 #include "ixheaacd_error_standards.h"
73 
74 #include "ixheaacd_error_codes.h"
75 
76 static const WORD32 sampling_rate_tbl[] = {
77     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025,
78     8000,  7350,  0,     0,     57600, 51200, 40000, 38400, 34150, 28800, 25600,
79     20000, 19200, 17075, 14400, 12800, 9600,  0,     0,     0};
80 
ixheaacd_sbr_ratio(UWORD32 core_sbr_framelength_idx)81 UWORD32 ixheaacd_sbr_ratio(UWORD32 core_sbr_framelength_idx) {
82   UWORD32 sbr_ratio_index = 0x0FF;
83 
84   switch (core_sbr_framelength_idx) {
85     case 0:
86     case 1:
87       sbr_ratio_index = USAC_SBR_RATIO_NO_SBR;
88       break;
89     case 2:
90       sbr_ratio_index = USAC_SBR_RATIO_INDEX_8_3;
91       break;
92     case 3:
93       sbr_ratio_index = USAC_SBR_RATIO_INDEX_2_1;
94       break;
95     case 4:
96       sbr_ratio_index = USAC_SBR_RATIO_INDEX_4_1;
97       break;
98   }
99 
100   return sbr_ratio_index;
101 }
102 
ixheaacd_get_sample_freq_indx(WORD32 sampling_freq)103 static WORD32 ixheaacd_get_sample_freq_indx(WORD32 sampling_freq) {
104   WORD32 index;
105   const WORD32 tbl_size = sizeof(sampling_rate_tbl) / sizeof(WORD32) - 1;
106 
107   for (index = 0; index < tbl_size; index++) {
108     if (sampling_rate_tbl[index] == sampling_freq) break;
109   }
110   return index;
111 }
ixheaacd_sbr_params(UWORD32 core_sbr_framelength_idx,WORD32 * output_framelength,WORD32 * block_size,WORD32 * output_samples,WORD32 * sample_rate_layer,UWORD32 * sample_freq_indx)112 UWORD32 ixheaacd_sbr_params(UWORD32 core_sbr_framelength_idx,
113                             WORD32 *output_framelength, WORD32 *block_size,
114                             WORD32 *output_samples, WORD32 *sample_rate_layer,
115                             UWORD32 *sample_freq_indx) {
116   UWORD32 sbr_ratio_index = 0x0FF;
117 
118   *output_framelength = -1;
119 
120   switch (core_sbr_framelength_idx) {
121     case 0:
122       sbr_ratio_index = USAC_SBR_RATIO_NO_SBR;
123       *output_framelength = USAC_OUT_FRAMELENGTH_768;
124       *block_size = 768;
125       *output_samples = *block_size;
126       break;
127     case 1:
128       sbr_ratio_index = USAC_SBR_RATIO_NO_SBR;
129       *output_framelength = USAC_OUT_FRAMELENGTH_1024;
130       *block_size = 1024;
131       *output_samples = *block_size;
132       break;
133     case 2:
134       sbr_ratio_index = USAC_SBR_RATIO_INDEX_8_3;
135       *output_framelength = USAC_OUT_FRAMELENGTH_2048;
136       *block_size = 768;
137       *output_samples = (*block_size * 8) / 3;
138       *sample_rate_layer = (*sample_rate_layer * 3) >> 3;
139       break;
140     case 3:
141       sbr_ratio_index = USAC_SBR_RATIO_INDEX_2_1;
142       *output_framelength = USAC_OUT_FRAMELENGTH_2048;
143       *block_size = 1024;
144       *output_samples = *block_size * 2;
145       *sample_rate_layer = *sample_rate_layer >> 1;
146       break;
147     case 4:
148       sbr_ratio_index = USAC_SBR_RATIO_INDEX_4_1;
149       *output_framelength = USAC_OUT_FRAMELENGTH_4096;
150       *block_size = 1024;
151       *output_samples = *block_size * 4;
152       *sample_rate_layer = *sample_rate_layer >> 2;
153       break;
154   }
155 
156   *sample_freq_indx = ixheaacd_get_sample_freq_indx(*sample_rate_layer);
157 
158   return sbr_ratio_index;
159 }
160 
ixheaacd_read_escape_value(ia_bit_buf_struct * it_bit_buff,UWORD32 * ext_ele_value,UWORD32 no_bits1,UWORD32 no_bits2,UWORD32 no_bits3)161 VOID ixheaacd_read_escape_value(ia_bit_buf_struct *it_bit_buff,
162                                 UWORD32 *ext_ele_value, UWORD32 no_bits1,
163                                 UWORD32 no_bits2, UWORD32 no_bits3) {
164   UWORD32 value = 0;
165   UWORD32 val_add = 0;
166   UWORD32 max_val1 = (1 << no_bits1) - 1;
167   UWORD32 max_val2 = (1 << no_bits2) - 1;
168 
169   value = ixheaacd_read_bits_buf(it_bit_buff, no_bits1);
170 
171   if (value == max_val1) {
172     val_add = ixheaacd_read_bits_buf(it_bit_buff, no_bits2);
173 
174     value += val_add;
175 
176     if (val_add == max_val2) {
177       val_add = ixheaacd_read_bits_buf(it_bit_buff, no_bits3);
178 
179       value += val_add;
180     }
181   }
182 
183   *ext_ele_value = value;
184 }
185 
ixheaacd_get_usac_chan_conf(ia_usac_config_struct * pstr_usac_config,UWORD32 ch_config_index,ia_bit_buf_struct * it_bit_buff,WORD32 ec_flag)186 static IA_ERRORCODE ixheaacd_get_usac_chan_conf(ia_usac_config_struct *pstr_usac_config,
187                         UWORD32 ch_config_index, ia_bit_buf_struct *it_bit_buff, WORD32 ec_flag)
188 {
189   switch (ch_config_index) {
190     case 1:
191       pstr_usac_config->num_out_channels = 1;
192       pstr_usac_config->output_channel_pos[0] = BS_OUTPUT_CHANNEL_POS_C;
193       break;
194     case 2:
195       pstr_usac_config->num_out_channels = 2;
196       pstr_usac_config->output_channel_pos[0] = BS_OUTPUT_CHANNEL_POS_L;
197       pstr_usac_config->output_channel_pos[1] = BS_OUTPUT_CHANNEL_POS_R;
198       break;
199     case 8:
200       pstr_usac_config->num_out_channels = 2;
201       pstr_usac_config->output_channel_pos[0] = BS_OUTPUT_CHANNEL_POS_NA;
202       pstr_usac_config->output_channel_pos[1] = BS_OUTPUT_CHANNEL_POS_NA;
203       break;
204 
205     default:
206       if (ec_flag)
207         longjmp(*(it_bit_buff->xaac_jmp_buf),
208             IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
209       else
210         return IA_FATAL_ERROR;
211       break;
212   }
213   return IA_NO_ERROR;
214 }
215 
ixheaacd_sbr_config(ia_bit_buf_struct * it_bit_buff,ia_usac_dec_sbr_config_struct * pstr_usac_sbr_config)216 VOID ixheaacd_sbr_config(ia_bit_buf_struct *it_bit_buff,
217                          ia_usac_dec_sbr_config_struct *pstr_usac_sbr_config) {
218   pstr_usac_sbr_config->harmonic_sbr = ixheaacd_read_bits_buf(it_bit_buff, 1);
219   pstr_usac_sbr_config->bs_inter_tes = ixheaacd_read_bits_buf(it_bit_buff, 1);
220   pstr_usac_sbr_config->bs_pvc = ixheaacd_read_bits_buf(it_bit_buff, 1);
221   pstr_usac_sbr_config->dflt_start_freq =
222       ixheaacd_read_bits_buf(it_bit_buff, 4);
223 
224   pstr_usac_sbr_config->dflt_stop_freq = ixheaacd_read_bits_buf(it_bit_buff, 4);
225   pstr_usac_sbr_config->dflt_header_extra1 =
226       ixheaacd_read_bits_buf(it_bit_buff, 1);
227   pstr_usac_sbr_config->dflt_header_extra2 =
228       ixheaacd_read_bits_buf(it_bit_buff, 1);
229 
230   if (pstr_usac_sbr_config->dflt_header_extra1) {
231     pstr_usac_sbr_config->dflt_freq_scale =
232         ixheaacd_read_bits_buf(it_bit_buff, 2);
233     pstr_usac_sbr_config->dflt_alter_scale =
234         ixheaacd_read_bits_buf(it_bit_buff, 1);
235     pstr_usac_sbr_config->dflt_noise_bands =
236         ixheaacd_read_bits_buf(it_bit_buff, 2);
237   }
238 
239   if (pstr_usac_sbr_config->dflt_header_extra2) {
240     pstr_usac_sbr_config->dflt_limiter_bands =
241         ixheaacd_read_bits_buf(it_bit_buff, 2);
242     pstr_usac_sbr_config->dflt_limiter_gains =
243         ixheaacd_read_bits_buf(it_bit_buff, 2);
244     pstr_usac_sbr_config->dflt_interpol_freq =
245         ixheaacd_read_bits_buf(it_bit_buff, 1);
246     pstr_usac_sbr_config->dflt_smoothing_mode =
247         ixheaacd_read_bits_buf(it_bit_buff, 1);
248   }
249 }
250 
ixheaacd_ext_element_config(ia_bit_buf_struct * it_bit_buff,ia_usac_dec_element_config_struct * pstr_usac_element_config,UWORD8 * ptr_usac_ext_ele_payload,WORD32 * ptr_usac_ext_ele_payload_len,WORD32 * preroll_flag)251 WORD32 ixheaacd_ext_element_config(
252     ia_bit_buf_struct *it_bit_buff,
253     ia_usac_dec_element_config_struct *pstr_usac_element_config,
254     UWORD8 *ptr_usac_ext_ele_payload, WORD32 *ptr_usac_ext_ele_payload_len,
255     WORD32 *preroll_flag) {
256   UWORD32 usac_ext_element_type, usac_ext_element_config_length, flag;
257 
258   UWORD32 i;
259 
260   ixheaacd_read_escape_value(it_bit_buff, &(usac_ext_element_type), 4, 8, 16);
261 
262   ixheaacd_read_escape_value(it_bit_buff, &(usac_ext_element_config_length), 4,
263                              8, 16);
264   if (usac_ext_element_config_length >= 768) return -1;
265 
266   flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
267 
268   *ptr_usac_ext_ele_payload_len = 0;
269 
270   if (flag) {
271     ixheaacd_read_escape_value(
272         it_bit_buff,
273         (UWORD32 *)(&(pstr_usac_element_config->usac_ext_eleme_def_len)), 8, 16,
274         0);
275     pstr_usac_element_config->usac_ext_eleme_def_len += 1;
276 
277   } else {
278     pstr_usac_element_config->usac_ext_eleme_def_len = 0;
279   }
280 
281   pstr_usac_element_config->usac_ext_elem_pld_frag =
282       ixheaacd_read_bits_buf(it_bit_buff, 1);
283 
284   switch (usac_ext_element_type) {
285     case ID_EXT_ELE_FILL:
286       break;
287     case ID_EXT_ELE_AUDIOPREROLL:
288       *preroll_flag = 1;
289       break;
290     case ID_EXT_ELE_UNI_DRC:
291       for (i = 0; i < usac_ext_element_config_length; i++) {
292         ptr_usac_ext_ele_payload[i] = ixheaacd_read_bits_buf(it_bit_buff, 8);
293       }
294       *ptr_usac_ext_ele_payload_len = usac_ext_element_config_length;
295       break;
296 
297     default:
298       if ((it_bit_buff->cnt_bits >> 3) < (WORD32)usac_ext_element_config_length)
299         return -1;
300       it_bit_buff->ptr_read_next += usac_ext_element_config_length;
301       it_bit_buff->cnt_bits -= (usac_ext_element_config_length << 3);
302 
303       break;
304   }
305 
306   return 0;
307 }
308 
ixheaacd_mps212_config(ia_bit_buf_struct * it_bit_buff,ia_usac_dec_mps_config_struct * pstr_usac_mps212_config,WORD32 stereo_config_index)309 IA_ERRORCODE ixheaacd_mps212_config(
310     ia_bit_buf_struct *it_bit_buff,
311     ia_usac_dec_mps_config_struct *pstr_usac_mps212_config,
312     WORD32 stereo_config_index) {
313   pstr_usac_mps212_config->bs_freq_res = ixheaacd_read_bits_buf(it_bit_buff, 3);
314 
315   pstr_usac_mps212_config->bs_fixed_gain_dmx =
316       ixheaacd_read_bits_buf(it_bit_buff, 3);
317 
318   pstr_usac_mps212_config->bs_temp_shape_config =
319       ixheaacd_read_bits_buf(it_bit_buff, 2);
320 
321   pstr_usac_mps212_config->bs_decorr_config =
322       ixheaacd_read_bits_buf(it_bit_buff, 2);
323   if (pstr_usac_mps212_config->bs_decorr_config > MAX_DECOR_CONFIG_IDX)
324     return IA_FATAL_ERROR;
325 
326   pstr_usac_mps212_config->bs_high_rate_mode =
327       ixheaacd_read_bits_buf(it_bit_buff, 1);
328 
329   pstr_usac_mps212_config->bs_phase_coding =
330       ixheaacd_read_bits_buf(it_bit_buff, 1);
331 
332   pstr_usac_mps212_config->bs_ott_bands_phase_present =
333       ixheaacd_read_bits_buf(it_bit_buff, 1);
334 
335   if (pstr_usac_mps212_config->bs_ott_bands_phase_present) {
336     pstr_usac_mps212_config->bs_ott_bands_phase =
337         ixheaacd_read_bits_buf(it_bit_buff, 5);
338     if (pstr_usac_mps212_config->bs_ott_bands_phase > MAX_PARAMETER_BANDS)
339       return IA_FATAL_ERROR;
340   }
341 
342   if (stereo_config_index > 1) {
343     pstr_usac_mps212_config->bs_residual_bands =
344         ixheaacd_read_bits_buf(it_bit_buff, 5);
345 
346     if (pstr_usac_mps212_config->bs_residual_bands > MAX_PARAMETER_BANDS)
347       return IA_FATAL_ERROR;
348 
349     pstr_usac_mps212_config->bs_ott_bands_phase =
350         max(pstr_usac_mps212_config->bs_ott_bands_phase,
351             pstr_usac_mps212_config->bs_residual_bands);
352 
353     pstr_usac_mps212_config->bs_pseudo_lr =
354         ixheaacd_read_bits_buf(it_bit_buff, 1);
355   }
356 
357   if (pstr_usac_mps212_config->bs_temp_shape_config == 2)
358     pstr_usac_mps212_config->bs_env_quant_mode =
359         ixheaacd_read_bits_buf(it_bit_buff, 1);
360 
361   return IA_NO_ERROR;
362 }
363 
ixheaacd_cpe_config(ia_bit_buf_struct * it_bit_buff,ia_usac_dec_element_config_struct * pstr_usac_element_config,WORD32 sbr_ratio_index)364 IA_ERRORCODE ixheaacd_cpe_config(
365     ia_bit_buf_struct *it_bit_buff,
366     ia_usac_dec_element_config_struct *pstr_usac_element_config,
367     WORD32 sbr_ratio_index) {
368   pstr_usac_element_config->tw_mdct = ixheaacd_read_bits_buf(it_bit_buff, 1);
369   pstr_usac_element_config->noise_filling =
370       ixheaacd_read_bits_buf(it_bit_buff, 1);
371 
372   if (sbr_ratio_index > 0) {
373     ixheaacd_sbr_config(it_bit_buff,
374                         &(pstr_usac_element_config->str_usac_sbr_config));
375     pstr_usac_element_config->stereo_config_index =
376         ixheaacd_read_bits_buf(it_bit_buff, 2);
377 
378   } else {
379     pstr_usac_element_config->stereo_config_index = 0;
380   }
381 
382   if (pstr_usac_element_config->stereo_config_index > 0)
383     return ixheaacd_mps212_config(
384         it_bit_buff, &(pstr_usac_element_config->str_usac_mps212_config),
385         pstr_usac_element_config->stereo_config_index);
386 
387   return IA_NO_ERROR;
388 }
389 
ixheaacd_decoder_config(ia_bit_buf_struct * it_bit_buff,ia_usac_decoder_config_struct * pstr_usac_decoder_config,WORD32 sbr_ratio_index,UINT32 * chan,WORD32 ec_flag)390 WORD32 ixheaacd_decoder_config(
391     ia_bit_buf_struct *it_bit_buff,
392     ia_usac_decoder_config_struct *pstr_usac_decoder_config,
393     WORD32 sbr_ratio_index, UINT32 *chan, WORD32 ec_flag) {
394   UWORD32 elem_idx = 0;
395   UWORD32 err = 0;
396   WORD32 num_channels = 0;
397 
398   ixheaacd_read_escape_value(
399       it_bit_buff, &(pstr_usac_decoder_config->num_elements), 4, 8, 16);
400   pstr_usac_decoder_config->num_elements += 1;
401   pstr_usac_decoder_config->preroll_flag = 0;
402 
403   if (pstr_usac_decoder_config->num_elements > USAC_MAX_ELEMENTS) {
404     if (ec_flag) {
405       pstr_usac_decoder_config->num_elements = USAC_MAX_ELEMENTS;
406       longjmp(*(it_bit_buff->xaac_jmp_buf), IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
407     } else {
408       return IA_FATAL_ERROR;
409     }
410   }
411 
412   for (elem_idx = 0; elem_idx < pstr_usac_decoder_config->num_elements;
413        elem_idx++) {
414     ia_usac_dec_element_config_struct *pstr_usac_element_config =
415         &(pstr_usac_decoder_config->str_usac_element_config[elem_idx]);
416 
417     pstr_usac_decoder_config->usac_element_type[elem_idx] =
418         ixheaacd_read_bits_buf(it_bit_buff, 2);
419 
420     switch (pstr_usac_decoder_config->usac_element_type[elem_idx]) {
421       case ID_USAC_SCE:
422         num_channels++;
423         pstr_usac_element_config->tw_mdct =
424             ixheaacd_read_bits_buf(it_bit_buff, 1);
425         pstr_usac_element_config->noise_filling =
426             ixheaacd_read_bits_buf(it_bit_buff, 1);
427         pstr_usac_element_config->stereo_config_index = 0;
428         if (sbr_ratio_index > 0)
429           ixheaacd_sbr_config(it_bit_buff,
430                               &(pstr_usac_element_config->str_usac_sbr_config));
431 
432         break;
433 
434       case ID_USAC_CPE:
435         num_channels += 2;
436         if (ixheaacd_cpe_config(it_bit_buff, pstr_usac_element_config, sbr_ratio_index) !=
437             IA_NO_ERROR) {
438           if (ec_flag) {
439             longjmp(*(it_bit_buff->xaac_jmp_buf),
440                     IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
441           } else {
442             return IA_FATAL_ERROR;
443           }
444         }
445         if (pstr_usac_element_config->stereo_config_index > 1 && *chan < 2) {
446           if (ec_flag) {
447             longjmp(*(it_bit_buff->xaac_jmp_buf),
448                     IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
449           } else {
450             return IA_FATAL_ERROR;
451           }
452         }
453         break;
454 
455       case ID_USAC_LFE:
456         num_channels++;
457         pstr_usac_element_config->tw_mdct = 0;
458         pstr_usac_element_config->noise_filling = 0;
459         pstr_usac_element_config->stereo_config_index = 0;
460         break;
461 
462       case ID_USAC_EXT:
463         err = ixheaacd_ext_element_config(
464             it_bit_buff, pstr_usac_element_config,
465             &pstr_usac_decoder_config->usac_ext_ele_payload_buf[elem_idx][0],
466             &pstr_usac_decoder_config->usac_ext_ele_payload_len[elem_idx],
467             &(pstr_usac_decoder_config->preroll_flag));
468 
469         if (pstr_usac_decoder_config->usac_ext_ele_payload_len[elem_idx] > 0) {
470           pstr_usac_decoder_config->usac_ext_ele_payload_present[elem_idx] = 1;
471         } else {
472           pstr_usac_decoder_config->usac_ext_ele_payload_present[elem_idx] = 0;
473         }
474         if (err != 0) {
475           if (ec_flag) {
476             longjmp(*(it_bit_buff->xaac_jmp_buf),
477                     IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
478           } else {
479             return IA_FATAL_ERROR;
480           }
481         }
482         break;
483       default:
484         if (ec_flag) {
485           longjmp(*(it_bit_buff->xaac_jmp_buf),
486                   IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
487         } else {
488           return IA_FATAL_ERROR;
489         }
490         break;
491     }
492     if (num_channels > 2) {
493       if (ec_flag) {
494         longjmp(*(it_bit_buff->xaac_jmp_buf),
495                 IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
496       } else {
497         return IA_FATAL_ERROR;
498       }
499     }
500   }
501   return err;
502 }
503 
ixheaacd_config_extension(ia_bit_buf_struct * it_bit_buff,ia_usac_decoder_config_struct * pstr_usac_decoder_config,WORD32 ec_flag)504 WORD32 ixheaacd_config_extension(ia_bit_buf_struct *it_bit_buff,
505     ia_usac_decoder_config_struct *pstr_usac_decoder_config, WORD32 ec_flag) {
506   UWORD32 i, j;
507   UWORD32 num_config_extensions;
508   UWORD32 usac_config_ext_type, usac_config_ext_len;
509 
510   ixheaacd_read_escape_value(it_bit_buff, &(num_config_extensions), 2, 4, 8);
511   num_config_extensions += 1;
512   if (USAC_MAX_CONFIG_EXTENSIONS < num_config_extensions) {
513     if (ec_flag) {
514       longjmp(*(it_bit_buff->xaac_jmp_buf), IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
515     } else {
516       return IA_FATAL_ERROR;
517     }
518   }
519 
520   pstr_usac_decoder_config->num_config_extensions = num_config_extensions;
521   memset(pstr_usac_decoder_config->usac_cfg_ext_info_len, 0,
522          USAC_MAX_CONFIG_EXTENSIONS * sizeof(WORD32));
523   memset(pstr_usac_decoder_config->usac_cfg_ext_info_present, 0,
524          USAC_MAX_CONFIG_EXTENSIONS * sizeof(WORD32));
525 
526   for (j = 0; j < num_config_extensions; j++) {
527     UWORD32 fill_byte_val = 0xa5;
528 
529     ixheaacd_read_escape_value(it_bit_buff, &(usac_config_ext_type), 4, 8, 16);
530     ixheaacd_read_escape_value(it_bit_buff, &(usac_config_ext_len), 4, 8, 16);
531 
532     if (usac_config_ext_len > 768) {
533       if (ec_flag) {
534         longjmp(*(it_bit_buff->xaac_jmp_buf),
535                 IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
536       } else {
537         return IA_FATAL_ERROR;
538       }
539     }
540 
541     switch (usac_config_ext_type) {
542       case ID_CONFIG_EXT_FILL:
543         for (i = 0; i < usac_config_ext_len; i++) {
544           fill_byte_val = ixheaacd_read_bits_buf(it_bit_buff, 8);
545           if (fill_byte_val != 0xa5) {
546             if (ec_flag) {
547               longjmp(*(it_bit_buff->xaac_jmp_buf),
548                       IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
549             } else {
550               return IA_FATAL_ERROR;
551             }
552           }
553         }
554         break;
555       default:
556         if ((WORD32)usac_config_ext_len > (it_bit_buff->cnt_bits >> 3)) {
557           if (ec_flag) {
558             longjmp(*(it_bit_buff->xaac_jmp_buf),
559                     IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
560           } else {
561             return IA_FATAL_ERROR;
562           }
563         }
564         if (ID_CONFIG_EXT_LOUDNESS_INFO == usac_config_ext_type) {
565           for (i = 0; i < usac_config_ext_len; i++) {
566             UWORD8 byte_val = ixheaacd_read_bits_buf(it_bit_buff, 8);
567             pstr_usac_decoder_config->usac_cfg_ext_info_buf[j][i] = byte_val;
568           }
569           pstr_usac_decoder_config->usac_cfg_ext_info_len[j] =
570               usac_config_ext_len;
571           pstr_usac_decoder_config->usac_cfg_ext_info_present[j] = 1;
572         } else {
573           for (i = 0; i < usac_config_ext_len; i++)
574             ixheaacd_read_bits_buf(it_bit_buff, 8);
575         }
576         break;
577     }
578   }
579 
580   return 0;
581 }
582 
ixheaacd_config(ia_bit_buf_struct * it_bit_buff,ia_usac_config_struct * pstr_usac_conf,UINT32 * chan,WORD32 ec_flag)583 WORD32 ixheaacd_config(ia_bit_buf_struct *it_bit_buff, ia_usac_config_struct *pstr_usac_conf,
584                        UINT32 *chan, WORD32 ec_flag) {
585   WORD32 tmp, err;
586   err = 0;
587 
588   pstr_usac_conf->usac_sampling_frequency_index =
589       ixheaacd_read_bits_buf(it_bit_buff, 5);
590 
591   if (pstr_usac_conf->usac_sampling_frequency_index == 0x1f) {
592     pstr_usac_conf->usac_sampling_frequency =
593         ixheaacd_read_bits_buf(it_bit_buff, 24);
594 
595     if (pstr_usac_conf->usac_sampling_frequency > USAC_MAX_SAMPLE_RATE) {
596       if (ec_flag) {
597         longjmp(*(it_bit_buff->xaac_jmp_buf),
598                 IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
599       } else {
600         return IA_FATAL_ERROR;
601       }
602     }
603 
604   } else {
605     pstr_usac_conf->usac_sampling_frequency =
606         sampling_rate_tbl[pstr_usac_conf->usac_sampling_frequency_index];
607   }
608 
609   if (pstr_usac_conf->usac_sampling_frequency == 0) {
610     if (ec_flag) {
611       longjmp(*(it_bit_buff->xaac_jmp_buf), IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
612     } else {
613       return IA_FATAL_ERROR;
614     }
615   }
616   pstr_usac_conf->core_sbr_framelength_index =
617       ixheaacd_read_bits_buf(it_bit_buff, 3);
618 
619   if (pstr_usac_conf->core_sbr_framelength_index > MAX_CORE_SBR_FRAME_LEN_IDX) {
620     if (ec_flag) {
621       longjmp(*(it_bit_buff->xaac_jmp_buf), IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
622     } else {
623       return IA_FATAL_ERROR;
624     }
625   }
626 
627   pstr_usac_conf->channel_configuration_index =
628       ixheaacd_read_bits_buf(it_bit_buff, 5);
629   if ((pstr_usac_conf->channel_configuration_index >= 3) &&
630       (pstr_usac_conf->channel_configuration_index != 8)) {
631     if (ec_flag) {
632       longjmp(*(it_bit_buff->xaac_jmp_buf), IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
633     } else {
634       return IA_FATAL_ERROR;
635     }
636   }
637 
638   if (pstr_usac_conf->channel_configuration_index == 0) {
639     UWORD32 i;
640 
641     ixheaacd_read_escape_value(it_bit_buff,
642                                (UWORD32 *)(&(pstr_usac_conf->num_out_channels)),
643                                5, 8, 16);
644     if (BS_MAX_NUM_OUT_CHANNELS < pstr_usac_conf->num_out_channels) {
645       return IA_XHEAAC_DEC_INIT_FATAL_STREAM_CHAN_GT_MAX;
646     }
647     for (i = 0; i < pstr_usac_conf->num_out_channels; i++)
648       pstr_usac_conf->output_channel_pos[i] =
649           ixheaacd_read_bits_buf(it_bit_buff, 5);
650 
651     if (ec_flag) {
652       longjmp(*(it_bit_buff->xaac_jmp_buf), IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
653     }
654   } else {
655     err = ixheaacd_get_usac_chan_conf(pstr_usac_conf, pstr_usac_conf->channel_configuration_index,
656                                       it_bit_buff, ec_flag);
657     if (err != 0)
658       return err;
659   }
660 
661   err = ixheaacd_decoder_config(
662       it_bit_buff, &(pstr_usac_conf->str_usac_dec_config),
663       ixheaacd_sbr_ratio(pstr_usac_conf->core_sbr_framelength_index), chan, ec_flag);
664   if (err != 0) return err;
665 
666   tmp = ixheaacd_read_bits_buf(it_bit_buff, 1);
667 
668   if (tmp) {
669     err = ixheaacd_config_extension(it_bit_buff,
670                                     &pstr_usac_conf->str_usac_dec_config, ec_flag);
671     if (err != 0) return -1;
672   }
673 
674   return err;
675 }
676 
ixheaacd_conf_default(ia_usac_config_struct * pstr_usac_conf)677 VOID ixheaacd_conf_default(ia_usac_config_struct *pstr_usac_conf) {
678   WORD32 i;
679 
680   pstr_usac_conf->num_out_channels = 0;
681 
682   for (i = 0; i < BS_MAX_NUM_OUT_CHANNELS; i++)
683     pstr_usac_conf->output_channel_pos[i] = BS_OUTPUT_CHANNEL_POS_NA;
684 
685   pstr_usac_conf->str_usac_dec_config.num_elements = 0;
686 
687   for (i = 0; i < USAC_MAX_ELEMENTS; i++)
688     pstr_usac_conf->str_usac_dec_config.usac_element_type[i] = ID_USAC_INVALID;
689 
690   return;
691 }
692