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 <stdio.h>
21 #include <string.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 #include "ixheaacd_bitbuffer.h"
31
32 #include "ixheaacd_basic_op.h"
33 #include "ixheaacd_intrinsics.h"
34
35 #include "ixheaacd_defines.h"
36
37 #include "ixheaacd_aac_rom.h"
38
39 #include "ixheaacd_definitions.h"
40
41 #include "ixheaacd_error_codes.h"
42
43 #include "ixheaacd_pulsedata.h"
44
45 #include "ixheaacd_pns.h"
46 #include "ixheaacd_drc_data_struct.h"
47
48 #include "ixheaacd_lt_predict.h"
49
50 #include "ixheaacd_channelinfo.h"
51 #include "ixheaacd_cnst.h"
52 #include "ixheaacd_drc_dec.h"
53 #include "ixheaacd_sbrdecoder.h"
54 #include "ixheaacd_block.h"
55 #include "ixheaacd_channel.h"
56
57 #include "ixheaacd_sbr_payload.h"
58 #include "ixheaacd_common_rom.h"
59 #include "ixheaacd_sbrdecsettings.h"
60 #include "ixheaacd_sbr_scale.h"
61 #include "ixheaacd_env_extr_part.h"
62 #include "ixheaacd_sbr_rom.h"
63 #include "ixheaacd_stereo.h"
64 #include "ixheaacd_lpp_tran.h"
65 #include "ixheaacd_hybrid.h"
66 #include "ixheaacd_ps_dec.h"
67 #include "ixheaacd_env_extr.h"
68 #include "ixheaacd_adts.h"
69 #include "ixheaacd_audioobjtypes.h"
70 #include "ixheaacd_memory_standards.h"
71 #include "ixheaacd_latmdemux.h"
72 #include "ixheaacd_aacdec.h"
73 #include "ixheaacd_mps_polyphase.h"
74 #include "ixheaacd_config.h"
75 #include "ixheaacd_mps_dec.h"
76 #include "ixheaacd_struct_def.h"
77 #include "ixheaacd_headerdecode.h"
78 #include "ixheaacd_multichannel.h"
79 #include "ixheaacd_adts_crc_check.h"
80
81 #include "ixheaacd_hcr.h"
82
83 #define SIZEOF_INT(x) ((sizeof(x) + sizeof(WORD32) - 1) / sizeof(WORD32))
84
85 #define EXT_FILL_DATA 1
86 #define EXT_FIL 0
87
ixheaacd_aacdec_decodeframe(ia_exhaacplus_dec_api_struct * p_obj_exhaacplus_dec,ia_aac_dec_scratch_struct * aac_scratch_ptrs,WORD16 * time_data,FLAG frame_status,WORD * type,WORD * ch_idx,WORD init_flag,WORD channel,WORD * element_index_order,WORD skip_full_decode,WORD ch_fac,WORD slot_element,WORD max_channels,WORD32 total_channels,WORD32 frame_length,WORD32 frame_size,ia_drc_dec_struct * pstr_drc_dec,WORD32 object_type,WORD32 ch_config,ia_eld_specific_config_struct eld_specific_config,WORD16 adtsheader,ia_drc_dec_struct * drc_dummy)88 WORD32 ixheaacd_aacdec_decodeframe(
89 ia_exhaacplus_dec_api_struct *p_obj_exhaacplus_dec,
90 ia_aac_dec_scratch_struct *aac_scratch_ptrs, WORD16 *time_data,
91 FLAG frame_status, WORD *type, WORD *ch_idx, WORD init_flag, WORD channel,
92 WORD *element_index_order, WORD skip_full_decode, WORD ch_fac,
93 WORD slot_element, WORD max_channels, WORD32 total_channels,
94 WORD32 frame_length, WORD32 frame_size, ia_drc_dec_struct *pstr_drc_dec,
95 WORD32 object_type, WORD32 ch_config,
96 ia_eld_specific_config_struct eld_specific_config, WORD16 adtsheader,
97 ia_drc_dec_struct *drc_dummy)
98
99 {
100 WORD ch, ele_type;
101 ia_aac_dec_state_struct *p_state_enhaacplus_dec;
102 ia_aac_decoder_struct *aac_dec_handle;
103 ia_bit_buf_struct *it_bit_buff;
104 ixheaacd_latm_struct *latm_element;
105
106 WORD error_code = (WORD)frame_status;
107 WORD previous_element;
108 WORD prev_data_ele_present = 0;
109 WORD new_element;
110 WORD32 num_ch = 0;
111
112 WORD32 crc_reg = 0;
113 ia_adts_crc_info_struct *ptr_adts_crc_info;
114
115 WORD32 cnt_bits = 0;
116
117 WORD32 eld_sbr_flag = eld_specific_config.ld_sbr_flag_present;
118 WORD32 ld_sbr_crc_flag = eld_specific_config.ld_sbr_crc_flag;
119 WORD32 aac_spect_data_resil_flag =
120 eld_specific_config.aac_spect_data_resil_flag;
121
122 WORD32 ele_ch = 0;
123
124 ia_aac_sfb_code_book_struct *ptr_aac_sfb_code_book_data[CHANNELS];
125 ia_pns_stereo_data_struct *ptr_pns_stereo_data;
126
127 WORD32 *work_buffer_core = aac_scratch_ptrs->base_scr_8k;
128 WORD32 *work_buffer_1 = aac_scratch_ptrs->extra_scr_4k[0];
129 WORD32 *work_buffer_2 = aac_scratch_ptrs->extra_scr_4k[2];
130 p_state_enhaacplus_dec = p_obj_exhaacplus_dec->p_state_aac;
131
132 aac_dec_handle = p_state_enhaacplus_dec->pstr_aac_dec_info[*ch_idx];
133 it_bit_buff = p_state_enhaacplus_dec->ptr_bit_stream;
134
135 ptr_adts_crc_info = it_bit_buff->pstr_adts_crc_info;
136
137 latm_element = &p_state_enhaacplus_dec->latm_struct_element;
138
139 ptr_pns_stereo_data =
140 (ia_pns_stereo_data_struct
141 *)&work_buffer_1[2 * SIZEOF_INT(ia_aac_dec_channel_info_struct) +
142 2 * SIZEOF_INT(ia_aac_sfb_code_book_struct)];
143
144 aac_dec_handle->frame_status = 1;
145
146 for (ch = 0; ch < channel; ch++) {
147 const ia_aac_dec_imdct_tables_struct *pstr_imdct_tables;
148 aac_dec_handle->pstr_aac_dec_ch_info[ch] =
149 (ia_aac_dec_channel_info_struct
150 *)&work_buffer_1[ch * SIZEOF_INT(ia_aac_dec_channel_info_struct)];
151 ptr_aac_sfb_code_book_data[ch] =
152 (ia_aac_sfb_code_book_struct
153 *)&work_buffer_1[2 * SIZEOF_INT(ia_aac_dec_channel_info_struct) +
154 (ch * SIZEOF_INT(ia_aac_sfb_code_book_struct))];
155
156 aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_scale_factor =
157 ptr_aac_sfb_code_book_data[ch]->scale_factor;
158 aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_code_book =
159 ptr_aac_sfb_code_book_data[ch]->code_book;
160
161 aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_spec_coeff =
162 &work_buffer_core[ch * MAX_BINS_LONG];
163
164 if (object_type == AOT_ER_AAC_ELD) {
165 aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_spec_coeff =
166 &work_buffer_core[2 * ch * MAX_BINS_LONG];
167 }
168
169 aac_dec_handle->pstr_aac_dec_ch_info[ch]->pstr_stereo_info =
170 &ptr_pns_stereo_data->str_stereo_info;
171 aac_dec_handle->pstr_aac_dec_ch_info[ch]->pstr_pns_corr_info =
172 &ptr_pns_stereo_data->str_pns_corr_info;
173 aac_dec_handle->pstr_aac_dec_ch_info[ch]->pstr_pns_rand_vec_data =
174 aac_dec_handle->pstr_pns_rand_vec_data;
175
176 pstr_imdct_tables = aac_dec_handle->pstr_aac_tables->pstr_imdct_tables;
177
178 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
179 pstr_imdct_tables->only_long_window_sine;
180 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_short_window[0] =
181 pstr_imdct_tables->only_short_window_sine;
182 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
183 pstr_imdct_tables->only_long_window_kbd;
184 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_short_window[1] =
185 pstr_imdct_tables->only_short_window_kbd;
186
187 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_long_window[0] =
188 pstr_imdct_tables->only_long_window_sine;
189 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_short_window[0] =
190 pstr_imdct_tables->only_short_window_sine;
191 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_long_window[1] =
192 pstr_imdct_tables->only_long_window_kbd;
193 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ptr_short_window[1] =
194 pstr_imdct_tables->only_short_window_kbd;
195
196 if (object_type == AOT_ER_AAC_ELD || object_type == AOT_ER_AAC_LD ||
197 object_type == AOT_AAC_LTP) {
198 aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.frame_length =
199 frame_length;
200
201 if (512 == aac_dec_handle->samples_per_frame) {
202 if (object_type != AOT_ER_AAC_ELD) {
203 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
204 (WORD16 *)pstr_imdct_tables->low_overlap_win;
205 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
206 (WORD16 *)pstr_imdct_tables->window_sine_512;
207
208 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
209 ->ptr_long_window[1] =
210 (WORD16 *)pstr_imdct_tables->low_overlap_win;
211 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
212 ->ptr_long_window[0] =
213 (WORD16 *)pstr_imdct_tables->window_sine_512;
214 } else {
215 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
216 (WORD16 *)pstr_imdct_tables->window_sine_512_eld;
217 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
218 (WORD16 *)pstr_imdct_tables->window_sine_512_eld;
219 }
220 } else if (480 == aac_dec_handle->samples_per_frame) {
221 if (object_type != AOT_ER_AAC_ELD) {
222 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
223 (WORD16 *)pstr_imdct_tables->low_overlap_win_480;
224 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
225 (WORD16 *)pstr_imdct_tables->window_sine_480;
226
227 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
228 ->ptr_long_window[1] =
229 (WORD16 *)pstr_imdct_tables->low_overlap_win_480;
230 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
231 ->ptr_long_window[0] =
232 (WORD16 *)pstr_imdct_tables->window_sine_480;
233
234 } else {
235 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[1] =
236 (WORD16 *)pstr_imdct_tables->window_sine_480_eld;
237 aac_dec_handle->pstr_aac_dec_overlap_info[ch]->ptr_long_window[0] =
238 (WORD16 *)pstr_imdct_tables->window_sine_480_eld;
239 }
240 }
241 }
242 if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
243 if (aac_dec_handle->samples_per_frame <= 512) {
244 aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp.lag =
245 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag;
246 }
247 aac_dec_handle->pstr_aac_dec_ch_info[ch]->ltp_buf =
248 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_buf;
249 aac_dec_handle->pstr_aac_dec_ch_info[ch]->ltp_lag =
250 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag;
251 }
252
253 aac_dec_handle->pstr_aac_dec_ch_info[ch]->scratch_buf_ptr = work_buffer_2;
254 if (object_type == AOT_ER_AAC_ELD) {
255 aac_dec_handle->pstr_aac_dec_ch_info[ch]->pulse_scratch =
256 aac_scratch_ptrs->extra_scr_4k[3];
257 }
258 }
259
260 if (channel == 2) {
261 if (aac_dec_handle->pstr_aac_dec_ch_info[1]->ptr_spec_coeff ==
262 aac_scratch_ptrs->extra_scr_4k[0]) {
263 aac_dec_handle->pstr_aac_dec_ch_info[1]->ptr_spec_coeff =
264 aac_dec_handle->pstr_aac_dec_ch_info[0]->ptr_spec_coeff;
265 }
266 }
267
268 for (ch = 0; ch < channel; ch++) {
269 ia_pns_info_struct *ptr_pns_info =
270 &aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_pns_info;
271 memset(ptr_pns_info, 0, sizeof(ia_pns_info_struct));
272 }
273
274 {
275 ia_pns_correlation_info_struct *ptr_corr_info =
276 aac_dec_handle->pstr_aac_dec_ch_info[0]->pstr_pns_corr_info;
277 memset(ptr_corr_info->correlated, 0, sizeof(UWORD8) * PNS_BAND_FLAGS_SIZE);
278 }
279
280 for (ch = 0; ch < channel; ch++) {
281 memset(&aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_hcr_info, 0,
282 sizeof(ia_hcr_info_struct));
283 ixheaacd_huff_code_reorder_tbl_init(
284 &aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_hcr_info);
285 }
286
287 for (ch = 0; ch < channel; ch++) {
288 aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp.data_present = 0;
289 aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp2.data_present =
290 0;
291 }
292
293 previous_element = ID_END;
294
295 aac_dec_handle->pstr_sbr_bitstream->no_elements = 0;
296 new_element = 0;
297 ele_type = *type;
298
299 cnt_bits = it_bit_buff->cnt_bits;
300
301 if (((object_type != AOT_ER_AAC_ELD) && (object_type != AOT_ER_AAC_LD)) ||
302 (object_type < ER_OBJECT_START)) {
303 while (ele_type != ID_END && aac_dec_handle->frame_status) {
304 ele_type = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 3);
305 ixheaacd_read_bidirection(it_bit_buff, -3);
306
307 if (it_bit_buff->cnt_bits < 3) {
308 it_bit_buff->cnt_bits = -1;
309 error_code = (WORD16)(
310 (WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
311 break;
312 }
313
314 if ((ele_type == ID_FIL) || (ele_type == ID_DSE) || (new_element == 0)) {
315 ele_type = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 3);
316 new_element = 1;
317 } else if ((ele_type != ID_END)) {
318 ele_type = -1;
319 break;
320 } else {
321 ele_type = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 3);
322 }
323
324 if (it_bit_buff->cnt_bits < 0) {
325 aac_dec_handle->frame_status = 0;
326 }
327
328 switch (ele_type) {
329 case ID_SCE:
330 case ID_CPE:
331 case ID_LFE:
332
333 if (aac_dec_handle->frame_status) {
334 ia_aac_dec_channel_info_struct *pstr_aac_dec_ch_info =
335 aac_dec_handle->pstr_aac_dec_ch_info[LEFT];
336 ia_ics_info_struct *ptr_ics_info =
337 &pstr_aac_dec_ch_info->str_ics_info;
338 ele_ch = 1;
339 if (ele_type == ID_CPE) {
340 ele_ch = 2;
341 } else {
342 ele_ch = 1;
343 }
344
345 prev_data_ele_present = 1;
346
347 if (ptr_adts_crc_info->crc_active == 1 &&
348 ptr_adts_crc_info->no_reg < 7) {
349 crc_reg = ixheaacd_adts_crc_start_reg(
350 ptr_adts_crc_info, it_bit_buff, CRC_ADTS_RAW_DATA_BLK_LEN);
351 }
352
353 pstr_aac_dec_ch_info->element_instance_tag =
354 (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
355
356 element_index_order[*ch_idx] =
357 pstr_aac_dec_ch_info->element_instance_tag;
358 pstr_aac_dec_ch_info->common_window = 0;
359
360 ptr_ics_info->num_swb_window = 0;
361 ptr_ics_info->sampling_rate_index =
362 aac_dec_handle->sampling_rate_index;
363 if ((object_type == AOT_ER_AAC_LD) ||
364 (object_type == AOT_AAC_LTP)) {
365 ptr_ics_info->ltp.data_present = 0;
366 ptr_ics_info->ltp2.data_present = 0;
367 ptr_ics_info->predictor_data_present = 0;
368 }
369
370 if (ele_ch > 1) {
371 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
372 ->str_ics_info.num_swb_window = 0;
373 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
374 ->str_ics_info.sampling_rate_index =
375 aac_dec_handle->sampling_rate_index;
376
377 pstr_aac_dec_ch_info->common_window =
378 (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 1);
379
380 if (pstr_aac_dec_ch_info->common_window) {
381 error_code = ixheaacd_ics_read(
382 it_bit_buff, ptr_ics_info, aac_dec_handle->num_swb_window,
383 object_type, pstr_aac_dec_ch_info->common_window,
384 aac_dec_handle->samples_per_frame);
385 if (error_code) {
386 if (it_bit_buff->cnt_bits < 0) {
387 error_code = (WORD16)(
388 (WORD32)
389 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
390 }
391
392 goto _ia_handle_error;
393 }
394
395 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info =
396 pstr_aac_dec_ch_info->str_ics_info;
397
398 ixheaacd_read_ms_data(it_bit_buff, pstr_aac_dec_ch_info);
399 }
400 }
401
402 error_code = ixheaacd_individual_ch_stream(
403 it_bit_buff, aac_dec_handle, ele_ch, frame_length,
404 total_channels, object_type, eld_specific_config, ele_type);
405
406 if (ptr_adts_crc_info->crc_active == 1) {
407 ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff,
408 crc_reg);
409 }
410
411 if (it_bit_buff->cnt_bits < 0) {
412 error_code = (WORD16)(
413 (WORD32)
414 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
415 }
416
417 if (error_code) {
418 goto _ia_handle_error;
419 }
420
421 _ia_handle_error:
422 if (error_code) {
423 aac_dec_handle->frame_status = 0;
424 if ((ele_type >= ID_SCE) && (ele_type <= ID_LFE))
425 num_ch = num_ch + ele_ch;
426 break;
427 } else {
428 ixheaacd_channel_pair_process(
429 aac_dec_handle->pstr_aac_dec_ch_info, ele_ch,
430 aac_dec_handle->pstr_aac_tables, total_channels, object_type,
431 aac_spect_data_resil_flag,
432 eld_specific_config.aac_sf_data_resil_flag,
433 aac_scratch_ptrs->in_data, aac_scratch_ptrs->out_data,
434 (void *)aac_dec_handle);
435 num_ch = num_ch + ele_ch;
436 }
437 }
438
439 break;
440 case ID_CCE:
441 if (max_channels > 2) {
442 prev_data_ele_present = 1;
443 error_code = ixheaacd_dec_coupling_channel_element(
444 it_bit_buff, aac_dec_handle,
445 aac_dec_handle->sampling_rate_index,
446 aac_dec_handle->pstr_aac_tables,
447 aac_dec_handle->pstr_common_tables,
448 &element_index_order[*ch_idx],
449 (ia_enhaacplus_dec_ind_cc *)aac_dec_handle->p_ind_channel_info,
450 total_channels, frame_length, object_type, eld_specific_config,
451 ele_type);
452
453 num_ch = num_ch + 1;
454
455 if (error_code) {
456 aac_dec_handle->frame_status = 0;
457
458 break;
459 } else {
460 ixheaacd_channel_pair_process(
461 aac_dec_handle->pstr_aac_dec_ch_info, 1,
462 aac_dec_handle->pstr_aac_tables, total_channels, object_type,
463 aac_spect_data_resil_flag,
464 eld_specific_config.aac_sf_data_resil_flag,
465 aac_scratch_ptrs->in_data, aac_scratch_ptrs->out_data,
466 (void *)aac_dec_handle);
467 }
468 } else {
469 error_code =
470 (WORD32)((WORD32)IA_ENHAACPLUS_DEC_EXE_FATAL_UNIMPLEMENTED_CCE);
471 }
472 if (it_bit_buff->cnt_bits < 0) {
473 error_code = (WORD16)((
474 WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
475 goto _ia_handle_error;
476 }
477 break;
478
479 case ID_DSE:
480 case ID_PCE:
481 case ID_FIL:
482
483 {
484 WORD32 flag = 1;
485
486 if ((ele_type != ID_FIL) && (ptr_adts_crc_info->crc_active == 1) &&
487 (ptr_adts_crc_info->no_reg < 7)) {
488 crc_reg =
489 ixheaacd_adts_crc_start_reg(ptr_adts_crc_info, it_bit_buff, 0);
490 }
491 if (ele_type == ID_DSE) {
492 ixheaacd_read_data_stream_element(
493 it_bit_buff, &aac_dec_handle->byte_align_bits,
494 p_obj_exhaacplus_dec->p_state_aac->pstr_drc_dec);
495 }
496
497 else if (ele_type == ID_PCE) {
498 error_code = ixheaacd_decode_pce(
499 it_bit_buff,
500 &p_obj_exhaacplus_dec->aac_config.ui_pce_found_in_hdr,
501 &p_obj_exhaacplus_dec->aac_config.str_prog_config);
502 if (error_code != 0) {
503 if (it_bit_buff->cnt_bits < 0) {
504 error_code = (WORD16)(
505 (WORD32)
506 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
507 goto _ia_handle_error;
508 }
509 aac_dec_handle->frame_status = 0;
510 if (error_code > 0) {
511 error_code = IA_ENHAACPLUS_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
512 return error_code;
513 } else {
514 return error_code;
515 }
516 }
517 }
518
519 else if (ele_type == ID_FIL) {
520 WORD32 bits_decoded = 0;
521 if (object_type == AOT_ER_AAC_ELD) {
522 bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
523 cnt_bits = (frame_size * 8 - bits_decoded);
524 if (adtsheader == 1) {
525 if (cnt_bits > it_bit_buff->cnt_bits)
526 return IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
527 }
528 }
529
530 if (ixheaacd_check_for_sbr_payload(
531 it_bit_buff, aac_dec_handle->pstr_sbr_bitstream,
532 (WORD16)previous_element, pstr_drc_dec, object_type,
533 adtsheader, cnt_bits, ld_sbr_crc_flag, drc_dummy)) {
534 flag = 0;
535 }
536 }
537
538 if (it_bit_buff->cnt_bits < 0) {
539 error_code = (WORD16)((
540 WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
541 goto _ia_handle_error;
542 }
543
544 if (flag) {
545 if (prev_data_ele_present == 0) {
546 new_element = 0;
547 }
548 }
549 if ((ele_type != ID_FIL) && (ptr_adts_crc_info->crc_active == 1)) {
550 ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff, crc_reg);
551 }
552
553 if (ele_type == ID_PCE) {
554 if (ptr_adts_crc_info->str_crc_reg_data[crc_reg].bit_buf_cnt) {
555 ptr_adts_crc_info->str_crc_reg_data[crc_reg].max_bits =
556 ptr_adts_crc_info->str_crc_reg_data[crc_reg].bit_buf_cnt;
557 }
558 }
559 }
560
561 break;
562
563 case ID_END:
564 error_code = 0;
565 break;
566 }
567
568 previous_element = ele_type;
569
570 if (init_flag) {
571 if ((ele_type >= ID_SCE) && (ele_type <= ID_LFE)) {
572 p_obj_exhaacplus_dec->aac_config.element_type[*ch_idx] = ele_type;
573 }
574 }
575 }
576 } else {
577 {
578 switch (ch_config) {
579 default:
580 if (aac_dec_handle->frame_status) {
581 ia_aac_dec_channel_info_struct *pstr_aac_dec_ch_info =
582 aac_dec_handle->pstr_aac_dec_ch_info[LEFT];
583 ia_ics_info_struct *ptr_ics_info =
584 &pstr_aac_dec_ch_info->str_ics_info;
585
586 if (ch_config == 2)
587 ele_ch = 2, ele_type = 1;
588 else
589 ele_ch = 1, ele_type = 0;
590
591 prev_data_ele_present = 1;
592
593 if ((ptr_adts_crc_info->crc_active == 1) &&
594 (ptr_adts_crc_info->no_reg < 7)) {
595 crc_reg = ixheaacd_adts_crc_start_reg(
596 ptr_adts_crc_info, it_bit_buff, CRC_ADTS_RAW_DATA_BLK_LEN);
597 }
598
599 if (object_type != AOT_ER_AAC_ELD)
600 pstr_aac_dec_ch_info->element_instance_tag =
601 (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
602
603 element_index_order[*ch_idx] =
604 pstr_aac_dec_ch_info->element_instance_tag;
605 pstr_aac_dec_ch_info->common_window = 0;
606
607 ptr_ics_info->num_swb_window = 0;
608 ptr_ics_info->sampling_rate_index =
609 aac_dec_handle->sampling_rate_index;
610
611 if (object_type == AOT_ER_AAC_LD) {
612 ptr_ics_info->ltp.data_present = 0;
613 ptr_ics_info->ltp2.data_present = 0;
614 ptr_ics_info->predictor_data_present = 0;
615 }
616 if (ele_ch > 1) {
617 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
618 ->str_ics_info.num_swb_window = 0;
619 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]
620 ->str_ics_info.sampling_rate_index =
621 aac_dec_handle->sampling_rate_index;
622
623 if (object_type != 39)
624 pstr_aac_dec_ch_info->common_window =
625 (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 1);
626 else
627 pstr_aac_dec_ch_info->common_window = 1;
628
629 if (pstr_aac_dec_ch_info->common_window) {
630 error_code = ixheaacd_ics_read(
631 it_bit_buff, ptr_ics_info, aac_dec_handle->num_swb_window,
632 object_type, pstr_aac_dec_ch_info->common_window,
633 aac_dec_handle->samples_per_frame);
634 if (error_code) {
635 if (it_bit_buff->cnt_bits < 0) {
636 error_code = (WORD16)(
637 (WORD32)
638 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
639 }
640
641 goto _ia_handle_error1;
642 }
643
644 aac_dec_handle->pstr_aac_dec_ch_info[RIGHT]->str_ics_info =
645 pstr_aac_dec_ch_info->str_ics_info;
646
647 ixheaacd_read_ms_data(it_bit_buff, pstr_aac_dec_ch_info);
648
649 {
650 if (object_type == AOT_ER_AAC_LD) {
651 WORD16 temp = ixheaacd_ltp_decode(
652 it_bit_buff, ptr_ics_info, object_type,
653 aac_dec_handle->samples_per_frame, LEFT);
654
655 if (temp != 0) {
656 return temp;
657 }
658 }
659 }
660 }
661 }
662
663 error_code = ixheaacd_individual_ch_stream(
664 it_bit_buff, aac_dec_handle, ele_ch, frame_length,
665 total_channels, object_type, eld_specific_config, ele_type);
666
667 if (ptr_adts_crc_info->crc_active == 1) {
668 ixheaacd_adts_crc_end_reg(ptr_adts_crc_info, it_bit_buff,
669 crc_reg);
670 }
671
672 if (it_bit_buff->cnt_bits < 0) {
673 error_code = (WORD16)(
674 (WORD32)
675 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
676 }
677
678 if (error_code) {
679 goto _ia_handle_error1;
680 }
681
682 _ia_handle_error1:
683 if (error_code) {
684 aac_dec_handle->frame_status = 0;
685 if ((ele_type >= ID_SCE) && (ele_type <= ID_LFE))
686 num_ch = num_ch + ele_ch;
687 break;
688 } else {
689 ixheaacd_channel_pair_process(
690 aac_dec_handle->pstr_aac_dec_ch_info, ele_ch,
691 aac_dec_handle->pstr_aac_tables, total_channels, object_type,
692 aac_spect_data_resil_flag,
693 eld_specific_config.aac_sf_data_resil_flag,
694 aac_scratch_ptrs->in_data, aac_scratch_ptrs->out_data,
695 (void *)aac_dec_handle);
696 num_ch = num_ch + ele_ch;
697 }
698 }
699
700 p_obj_exhaacplus_dec->aac_config.element_type[*ch_idx] = ele_ch - 1;
701 break;
702 }
703
704 if ((object_type != AOT_ER_AAC_ELD) || (!eld_sbr_flag)) {
705 WORD32 bits_decoded, cnt_bits;
706 bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
707
708 cnt_bits = (frame_size * 8 - bits_decoded);
709 if (cnt_bits >= 8) {
710 ixheaacd_extension_payload(it_bit_buff, cnt_bits);
711 }
712
713 if (((object_type == AOT_ER_AAC_ELD) ||
714 (object_type == AOT_ER_AAC_LD)) &&
715 (p_obj_exhaacplus_dec->aac_config.ld_decoder != 1)) {
716 if (it_bit_buff->cnt_bits) {
717 WORD32 alignment = it_bit_buff->bit_pos & 0x07;
718 it_bit_buff->cnt_bits = (it_bit_buff->cnt_bits + alignment) & 7;
719 it_bit_buff->bit_pos = 7;
720 it_bit_buff->ptr_read_next++;
721 }
722 } else {
723 if (it_bit_buff->bit_pos != 7) {
724 WORD32 alignment = it_bit_buff->bit_pos & 0x07;
725 it_bit_buff->cnt_bits -= alignment + 1;
726 it_bit_buff->bit_pos += 7 - alignment;
727 it_bit_buff->ptr_read_next++;
728 }
729 }
730 } else {
731 WORD32 bits_decoded, cnt_bits;
732 bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
733 cnt_bits = (frame_size * 8 - bits_decoded);
734 if (adtsheader == 1) {
735 if (cnt_bits > it_bit_buff->cnt_bits)
736 return IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
737 }
738 ixheaacd_check_for_sbr_payload(
739 it_bit_buff, aac_dec_handle->pstr_sbr_bitstream,
740 (WORD16)(ch_config - 1), pstr_drc_dec, object_type, adtsheader,
741 cnt_bits, ld_sbr_crc_flag, drc_dummy);
742 }
743 }
744 }
745
746 if (ele_type == ID_END &&
747 p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT) {
748 WORD32 tmp;
749 tmp = ((WORD32)latm_element->layer_info[0][0].frame_len_bits) -
750 (it_bit_buff->initial_cnt_bits - it_bit_buff->cnt_bits);
751
752 if (tmp > 0) ixheaacd_read_bidirection(it_bit_buff, tmp);
753
754 if (latm_element->other_data_present) {
755 WORD32 count_bits = (WORD32)latm_element->other_data_length;
756 ixheaacd_read_bidirection(it_bit_buff, count_bits);
757 }
758 }
759
760 if (object_type == AOT_ER_AAC_LD) {
761 for (ch = 0; ch < channel; ch++) {
762 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]->ltp_lag =
763 aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info.ltp.lag;
764 }
765 }
766 aac_dec_handle->frame_status = aac_dec_handle->frame_status && frame_status;
767
768 aac_dec_handle->channels = num_ch;
769
770 if (error_code == 0)
771 if ((skip_full_decode == 0) || ((skip_full_decode == 1) && error_code)) {
772 ia_ics_info_struct str_ics_info[2];
773 WORD32 *spec_coef[2];
774 WORD32 *scratch[2];
775
776 for (ch = 0; ch < channel; ch++) {
777 str_ics_info[ch] =
778 aac_dec_handle->pstr_aac_dec_ch_info[ch]->str_ics_info;
779 spec_coef[ch] =
780 aac_dec_handle->pstr_aac_dec_ch_info[ch]->ptr_spec_coeff;
781 }
782
783 scratch[0] = (WORD32 *)aac_scratch_ptrs->extra_scr_4k[2];
784 scratch[1] = (WORD32 *)aac_scratch_ptrs->extra_scr_4k[1];
785
786 ixheaacd_drc_map_channels(
787 pstr_drc_dec, aac_dec_handle->channels,
788 aac_dec_handle->pstr_aac_dec_ch_info[0]->str_ics_info.frame_length);
789
790 for (ch = 0; ch < aac_dec_handle->channels; ch++) {
791 WORD32 *overlap1 = aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
792 ->overlap_add_data.ptr_overlap_buf;
793 const WORD16 *ptr_long_window_next =
794 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
795 ->ptr_long_window[(int)str_ics_info[ch].window_shape];
796 const WORD16 *ptr_short_window_next =
797 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
798 ->ptr_short_window[(int)str_ics_info[ch].window_shape];
799 if (pstr_drc_dec->drc_on) {
800 ixheaacd_drc_apply(pstr_drc_dec, spec_coef[ch],
801 str_ics_info[ch].window_sequence, ch,
802 str_ics_info[ch].frame_length);
803 }
804 if (skip_full_decode == 0) {
805 ixheaacd_imdct_process(
806 aac_dec_handle->pstr_aac_dec_overlap_info[ch], spec_coef[ch],
807 &str_ics_info[ch], time_data + slot_element, (WORD16)ch_fac,
808 scratch[ch], aac_dec_handle->pstr_aac_tables, object_type);
809
810 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
811 ->overlap_add_data.win_shape = str_ics_info[ch].window_shape;
812 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
813 ->overlap_add_data.win_seq = str_ics_info[ch].window_sequence;
814 if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
815 {
816 if ((str_ics_info[ch].window_sequence == ONLY_LONG_SEQUENCE) ||
817 (str_ics_info[ch].window_sequence == LONG_STOP_SEQUENCE)) {
818 ixheaacd_lt_update_state(
819 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
820 ->ltp_buf,
821 time_data + slot_element, overlap1,
822 aac_dec_handle->samples_per_frame, object_type,
823 (WORD16)ch_fac, str_ics_info[ch].window_sequence,
824 (WORD16 *)ptr_long_window_next);
825 } else {
826 ixheaacd_lt_update_state(
827 aac_dec_handle->ptr_aac_dec_static_channel_info[ch]
828 ->ltp_buf,
829 time_data + slot_element, overlap1,
830 aac_dec_handle->samples_per_frame, object_type,
831 (WORD16)ch_fac, str_ics_info[ch].window_sequence,
832 (WORD16 *)ptr_short_window_next);
833 }
834 }
835 }
836 slot_element++;
837 }
838 }
839 }
840
841 if (ele_type == ID_END) {
842 ixheaacd_byte_align(it_bit_buff, &aac_dec_handle->byte_align_bits);
843 if (p_state_enhaacplus_dec->bs_format == LOAS_BSFORMAT) {
844 ixheaacd_byte_align(it_bit_buff, &it_bit_buff->audio_mux_align);
845 }
846 }
847 *type = ele_type;
848
849 aac_dec_handle->block_number =
850 ixheaacd_add32(aac_dec_handle->block_number, 1);
851 return error_code;
852 }
853
ixheaacd_extension_payload(ia_bit_buf_struct * it_bit_buff,WORD32 cnt)854 WORD32 ixheaacd_extension_payload(ia_bit_buf_struct *it_bit_buff, WORD32 cnt) {
855 WORD16 extension_type, discard;
856 WORD32 i;
857 WORD32 fill_nibble;
858
859 WORD32 err = 0;
860 extension_type = (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 4);
861 switch (extension_type) {
862 case EXT_FILL_DATA:
863
864 fill_nibble = ixheaacd_read_bits_buf(it_bit_buff, 4);
865
866 if (fill_nibble == 0) {
867 for (i = 0; i < (cnt >> 3) - 1; i++) {
868 ixheaacd_read_bits_buf(it_bit_buff, 8);
869 }
870
871 } else
872 err = -1;
873 break;
874 case EXT_FIL:
875 default:
876 for (i = 0; i < ((cnt)-8) + 4; i++) {
877 discard = (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 1);
878 }
879 }
880
881 return err;
882 }
883