1 /******************************************************************************
2 * *
3 * Copyright (C) 2018 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 #include <string.h>
21 #include "ixheaacd_sbr_common.h"
22 #include "ixheaacd_type_def.h"
23
24 #include "ixheaacd_constants.h"
25 #include "ixheaacd_basic_ops32.h"
26 #include "ixheaacd_basic_ops16.h"
27 #include "ixheaacd_basic_ops40.h"
28 #include "ixheaacd_basic_ops.h"
29 #include "ixheaacd_defines.h"
30 #include "ixheaacd_error_codes.h"
31
32 #include "ixheaacd_intrinsics.h"
33 #include "ixheaacd_sbr_const.h"
34 #include "ixheaacd_basic_op.h"
35 #include "ixheaacd_defines.h"
36 #include "ixheaacd_bitbuffer.h"
37 #include "ixheaacd_pns.h"
38
39 #include "ixheaacd_aac_rom.h"
40 #include "ixheaacd_pulsedata.h"
41
42 #include "ixheaacd_drc_data_struct.h"
43 #include "ixheaacd_lt_predict.h"
44 #include "ixheaacd_cnst.h"
45 #include "ixheaacd_ec_defines.h"
46 #include "ixheaacd_ec_struct_def.h"
47 #include "ixheaacd_channelinfo.h"
48 #include "ixheaacd_drc_dec.h"
49
50 #include "ixheaacd_sbrdecoder.h"
51
52 #include "ixheaacd_sbrdecsettings.h"
53 #include "ixheaacd_sbr_scale.h"
54 #include "ixheaacd_lpp_tran.h"
55 #include "ixheaacd_env_extr_part.h"
56 #include "ixheaacd_sbr_rom.h"
57 #include "ixheaacd_hybrid.h"
58 #include "ixheaacd_ps_dec.h"
59 #include "ixheaacd_ps_bitdec.h"
60 #include "ixheaacd_env_extr.h"
61 #include "ixheaacd_common_rom.h"
62 #include "ixheaacd_freq_sca.h"
63
64 #include "ixheaacd_qmf_dec.h"
65
66 #include "ixheaacd_env_calc.h"
67
68 #include "ixheaacd_pvc_dec.h"
69 #include "ixheaacd_sbr_dec.h"
70 #include "ixheaacd_env_dec.h"
71 #include "ixheaacd_basic_funcs.h"
72 #include "ixheaacd_sbr_crc.h"
73
74 #include "ixheaacd_audioobjtypes.h"
75
ixheaacd_ec_set_frame_error_flag(ia_sbr_element_stream_struct * pstr_sbr_element,WORD32 value)76 static VOID ixheaacd_ec_set_frame_error_flag(ia_sbr_element_stream_struct *pstr_sbr_element,
77 WORD32 value) {
78 if (pstr_sbr_element != NULL) {
79 switch (value) {
80 case FRAME_ERROR_ALLSLOTS:
81 pstr_sbr_element->frame_error_flag[0] = FRAME_ERROR;
82 pstr_sbr_element->frame_error_flag[1] = FRAME_ERROR;
83 break;
84 default:
85 pstr_sbr_element->frame_error_flag[pstr_sbr_element->use_frame_slot] = value;
86 }
87 }
88 }
89
ixheaacd_downmix_to_monosbr(WORD16 * core_sample_buf,WORD32 ch_fac)90 VOID ixheaacd_downmix_to_monosbr(WORD16 *core_sample_buf, WORD32 ch_fac) {
91 WORD32 i;
92 WORD16 *ptr1 = &core_sample_buf[0];
93 WORD16 *ptr2 = &core_sample_buf[1];
94
95 for (i = MAX_FRAME_SIZE - 1; i >= 0; i--) {
96 *ptr1 = ((*ptr1 >> 1) + (*ptr2 >> 1));
97
98 ptr1 += ch_fac;
99 ptr2 += ch_fac;
100 }
101 }
102
ixheaacd_sbr_dec_reset(ia_sbr_dec_struct * ptr_sbr_dec,ia_sbr_header_data_struct * ptr_header_data,FLAG low_pow_flag,ixheaacd_misc_tables * pstr_common_tables,WORD32 pitch_in_bins,WORD32 audio_object_type,WORD32 * ptr_work_buf_core)103 static WORD32 ixheaacd_sbr_dec_reset(ia_sbr_dec_struct *ptr_sbr_dec,
104 ia_sbr_header_data_struct *ptr_header_data,
105 FLAG low_pow_flag,
106 ixheaacd_misc_tables *pstr_common_tables,
107 WORD32 pitch_in_bins,
108 WORD32 audio_object_type,
109 WORD32 *ptr_work_buf_core) {
110 WORD32 old_lsb, new_lsb;
111 WORD32 l;
112 WORD32 err = 0;
113 WORD32 num_time_slots = ptr_sbr_dec->str_codec_qmf_bank.num_time_slots;
114 WORD32 upsample_ratio_idx = ptr_header_data->sbr_ratio_idx;
115 WORD32 op_delay = 6 + SBR_HF_ADJ_OFFSET;
116 WORD32 hbe_flag = ptr_header_data->hbe_flag;
117 WORD32 usac_flag = ptr_header_data->usac_flag;
118
119 if (ptr_header_data->is_usf_4) {
120 op_delay = op_delay + 6;
121 }
122
123 ixheaacd_reset_sbrenvelope_calc(&ptr_sbr_dec->str_sbr_calc_env);
124
125 new_lsb = ptr_header_data->pstr_freq_band_data->sub_band_start;
126 ptr_sbr_dec->str_synthesis_qmf_bank.lsb = new_lsb;
127 ptr_sbr_dec->str_synthesis_qmf_bank.usb =
128 ptr_header_data->pstr_freq_band_data->sub_band_end;
129
130 old_lsb = ptr_sbr_dec->str_synthesis_qmf_bank.lsb;
131 ptr_sbr_dec->str_codec_qmf_bank.lsb = 0;
132 ptr_sbr_dec->str_codec_qmf_bank.usb = old_lsb;
133
134 {
135 WORD32 *plpc_filt_states_real =
136 &ptr_sbr_dec->str_hf_generator.lpc_filt_states_real[0][old_lsb];
137 WORD32 *plpc_filt_states_real_1 =
138 &ptr_sbr_dec->str_hf_generator.lpc_filt_states_real[1][old_lsb];
139
140 WORD32 *plpc_filt_states_imag =
141 &ptr_sbr_dec->str_hf_generator.lpc_filt_states_imag[0][old_lsb];
142 WORD32 *plpc_filt_states_imag_1 =
143 &ptr_sbr_dec->str_hf_generator.lpc_filt_states_imag[1][old_lsb];
144
145 for (l = new_lsb - old_lsb - 1; l >= 0; l--) {
146 *plpc_filt_states_real++ = *plpc_filt_states_real_1++ = 0L;
147
148 if (!low_pow_flag) {
149 *plpc_filt_states_imag++ = *plpc_filt_states_imag_1++ = 0L;
150 }
151 }
152 }
153 if (1) {
154 WORD32 start_band;
155 WORD32 stop_band;
156 WORD32 start_slot = SBR_HF_ADJ_OFFSET;
157 WORD32 k;
158
159 start_band = ptr_header_data->pstr_freq_band_data->qmf_sb_prev;
160 stop_band = ptr_header_data->pstr_freq_band_data->sub_band_start;
161 if (usac_flag && !hbe_flag) {
162 for (l = 0; l < SBR_HF_ADJ_OFFSET; l++) {
163 for (k = start_band; k < stop_band; k++) {
164 ptr_sbr_dec->qmf_buf_real[l][k] = 0.0;
165 ptr_sbr_dec->qmf_buf_imag[l][k] = 0.0;
166 }
167 }
168 for (l = start_slot; l < op_delay; l++) {
169 for (k = start_band; k < stop_band; k++) {
170 ptr_sbr_dec->qmf_buf_real[l][k] = 0.0;
171 ptr_sbr_dec->qmf_buf_imag[l][k] = 0.0;
172 }
173 }
174 }
175 if (ptr_sbr_dec->p_hbe_txposer != NULL && (usac_flag || hbe_flag)) {
176 WORD32 k, i;
177 WORD32 dft_hbe_flag = ptr_header_data->esbr_hq;
178 if (dft_hbe_flag == 1) {
179 err = ixheaacd_dft_hbe_data_reinit(
180 ptr_sbr_dec->p_hbe_txposer,
181 ptr_header_data->pstr_freq_band_data->freq_band_table,
182 ptr_header_data->pstr_freq_band_data->num_sf_bands);
183 } else {
184 err = ixheaacd_qmf_hbe_data_reinit(
185 ptr_sbr_dec->p_hbe_txposer,
186 ptr_header_data->pstr_freq_band_data->freq_band_table,
187 ptr_header_data->pstr_freq_band_data->num_sf_bands,
188 ptr_header_data->is_usf_4);
189 }
190 if (err) return err;
191
192 for (k = 0; k < 2; k++) {
193 if (!((upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) && (k == 0))) {
194 WORD32 xpos_delay = num_time_slots * k;
195 if (upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
196 xpos_delay = num_time_slots * k - 32;
197 }
198
199 for (i = 0; i < 8; i++) {
200 memmove(ptr_sbr_dec->ph_vocod_qmf_real[i],
201 ptr_sbr_dec->ph_vocod_qmf_real[num_time_slots + i],
202 64 * sizeof(FLOAT32));
203 memmove(ptr_sbr_dec->ph_vocod_qmf_imag[i],
204 ptr_sbr_dec->ph_vocod_qmf_imag[num_time_slots + i],
205 64 * sizeof(FLOAT32));
206 }
207 if (dft_hbe_flag == 1) {
208 err = ixheaacd_dft_hbe_apply(
209 ptr_sbr_dec->p_hbe_txposer,
210 ptr_sbr_dec->qmf_buf_real + op_delay + xpos_delay,
211 ptr_sbr_dec->qmf_buf_imag + op_delay + xpos_delay, num_time_slots,
212 ptr_sbr_dec->ph_vocod_qmf_real + op_delay,
213 ptr_sbr_dec->ph_vocod_qmf_imag + op_delay, pitch_in_bins,
214 (FLOAT32 *)ptr_work_buf_core);
215 if (err)
216 return err;
217 } else {
218 err = ixheaacd_qmf_hbe_apply(
219 ptr_sbr_dec->p_hbe_txposer,
220 ptr_sbr_dec->qmf_buf_real + op_delay + xpos_delay,
221 ptr_sbr_dec->qmf_buf_imag + op_delay + xpos_delay, num_time_slots,
222 ptr_sbr_dec->ph_vocod_qmf_real + op_delay,
223 ptr_sbr_dec->ph_vocod_qmf_imag + op_delay, pitch_in_bins,
224 ptr_header_data);
225 if (err) return err;
226 }
227
228 if (upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
229 ixheaacd_hbe_repl_spec(&ptr_sbr_dec->p_hbe_txposer->x_over_qmf[0],
230 ptr_sbr_dec->ph_vocod_qmf_real + op_delay,
231 ptr_sbr_dec->ph_vocod_qmf_imag + op_delay,
232 num_time_slots,
233 ptr_sbr_dec->p_hbe_txposer->max_stretch);
234 }
235 }
236 }
237 }
238 }
239
240 if (!usac_flag) {
241 err |= ixheaacd_reset_hf_generator(&ptr_sbr_dec->str_hf_generator,
242 ptr_header_data, audio_object_type);
243
244 ixheaacd_derive_lim_band_tbl(
245 ptr_header_data,
246 ptr_sbr_dec->str_hf_generator.pstr_settings->str_patch_param,
247 ptr_sbr_dec->str_hf_generator.pstr_settings->num_patches,
248 pstr_common_tables);
249 }
250
251 return err;
252 }
253
ixheaacd_prepare_upsamp(ia_sbr_header_data_struct ** ptr_header_data,ia_sbr_channel_struct * pstr_sbr_channel[2],WORD32 num_channels)254 VOID ixheaacd_prepare_upsamp(ia_sbr_header_data_struct **ptr_header_data,
255 ia_sbr_channel_struct *pstr_sbr_channel[2],
256 WORD32 num_channels) {
257 WORD32 lr;
258 ia_sbr_qmf_filter_bank_struct *sbr_qmf_bank;
259
260 for (lr = 0; lr < num_channels; lr++) {
261 ptr_header_data[lr]->pstr_freq_band_data->sub_band_start =
262 NO_ANALYSIS_CHANNELS;
263 ptr_header_data[lr]->pstr_freq_band_data->sub_band_end =
264 NO_SYNTHESIS_CHANNELS;
265
266 sbr_qmf_bank = &pstr_sbr_channel[lr]->str_sbr_dec.str_synthesis_qmf_bank;
267 sbr_qmf_bank->lsb = NO_ANALYSIS_CHANNELS;
268 sbr_qmf_bank->usb = NO_SYNTHESIS_CHANNELS;
269
270 sbr_qmf_bank = &pstr_sbr_channel[lr]->str_sbr_dec.str_codec_qmf_bank;
271 sbr_qmf_bank->lsb = 0;
272 sbr_qmf_bank->usb = NO_ANALYSIS_CHANNELS;
273 ptr_header_data[lr]->sync_state = UPSAMPLING;
274 }
275 return;
276 }
277
ixheaacd_copy_prev_ps_params(ia_ps_dec_struct * ps_config_curr,ia_ps_dec_config_struct * ps_config_prev,WORD32 frame_status)278 static VOID ixheaacd_copy_prev_ps_params(ia_ps_dec_struct *ps_config_curr,
279 ia_ps_dec_config_struct *ps_config_prev,
280 WORD32 frame_status) {
281 if (frame_status == 0) {
282 ps_config_curr->enable_iid = ps_config_prev->enable_iid;
283 ps_config_curr->iid_mode = ps_config_prev->iid_mode;
284 ps_config_curr->enable_icc = ps_config_prev->enable_icc;
285 ps_config_curr->icc_mode = ps_config_prev->icc_mode;
286 ps_config_curr->frame_class = ps_config_prev->frame_class;
287 ps_config_curr->freq_res_ipd = ps_config_prev->freq_res_ipd;
288 memcpy(ps_config_curr->border_position, ps_config_prev->border_position,
289 sizeof(ps_config_curr->border_position));
290 memcpy(ps_config_curr->iid_dt, ps_config_prev->iid_dt, sizeof(ps_config_curr->iid_dt));
291 memcpy(ps_config_curr->iid_par_table, ps_config_prev->iid_par_table,
292 sizeof(ps_config_curr->iid_par_table));
293 memcpy(ps_config_curr->icc_dt, ps_config_prev->icc_dt, sizeof(ps_config_curr->icc_dt));
294 memcpy(ps_config_curr->icc_par_table, ps_config_prev->icc_par_table,
295 sizeof(ps_config_curr->icc_par_table));
296 } else {
297 ps_config_prev->enable_iid = ps_config_curr->enable_iid;
298 ps_config_prev->iid_mode = ps_config_curr->iid_mode;
299 ps_config_prev->enable_icc = ps_config_curr->enable_icc;
300 ps_config_prev->icc_mode = ps_config_curr->icc_mode;
301 ps_config_prev->frame_class = ps_config_curr->frame_class;
302 ps_config_prev->freq_res_ipd = ps_config_curr->freq_res_ipd;
303 memcpy(ps_config_prev->border_position, ps_config_curr->border_position,
304 sizeof(ps_config_prev->border_position));
305 memcpy(ps_config_prev->iid_dt, ps_config_curr->iid_dt, sizeof(ps_config_prev->iid_dt));
306 memcpy(ps_config_prev->iid_par_table, ps_config_curr->iid_par_table,
307 sizeof(ps_config_prev->iid_par_table));
308 memcpy(ps_config_prev->icc_dt, ps_config_curr->icc_dt, sizeof(ps_config_prev->icc_dt));
309 memcpy(ps_config_prev->icc_par_table, ps_config_curr->icc_par_table,
310 sizeof(ps_config_prev->icc_par_table));
311 }
312 }
ixheaacd_applysbr(ia_handle_sbr_dec_inst_struct self,ia_aac_dec_sbr_bitstream_struct * p_sbr_bit_stream,WORD16 * core_sample_buf,WORD16 * codec_num_channels,FLAG frame_status,FLAG down_samp_flag,FLAG down_mix_flag,ia_sbr_scr_struct * sbr_scratch_struct,WORD32 ps_enable,WORD32 ch_fac,WORD32 slot_element,ia_bit_buf_struct * it_bit_buff,ia_drc_dec_struct * pstr_drc_dec,WORD eld_sbr_flag,WORD32 audio_object_type,WORD32 init_flag,WORD32 ldmps_present,WORD32 frame_size,WORD32 heaac_mps_present,WORD32 ec_flag,FLAG first_frame)313 IA_ERRORCODE ixheaacd_applysbr(
314 ia_handle_sbr_dec_inst_struct self, ia_aac_dec_sbr_bitstream_struct *p_sbr_bit_stream,
315 WORD16 *core_sample_buf, WORD16 *codec_num_channels, FLAG frame_status, FLAG down_samp_flag,
316 FLAG down_mix_flag, ia_sbr_scr_struct *sbr_scratch_struct, WORD32 ps_enable, WORD32 ch_fac,
317 WORD32 slot_element, ia_bit_buf_struct *it_bit_buff, ia_drc_dec_struct *pstr_drc_dec,
318 WORD eld_sbr_flag, WORD32 audio_object_type, WORD32 init_flag, WORD32 ldmps_present,
319 WORD32 frame_size, WORD32 heaac_mps_present, WORD32 ec_flag, FLAG first_frame) {
320 WORD32 k;
321 FLAG prev_ps_flag = 0;
322 FLAG ps_flag = 0;
323 FLAG stereo = 0;
324 FLAG low_pow_flag = 0;
325 FLAG header_flag = 1;
326 FLAG dual_mono = 0;
327 WORD32 err = 0;
328 WORD32 num_channels = *codec_num_channels;
329 FLAG prev_stereo;
330 WORD32 ele_channels = 0;
331 WORD32 num_elements = p_sbr_bit_stream->no_elements;
332 WORD32 usac_flag = self->aot_usac_flag;
333 WORD32 total_bits_left = 0;
334
335 ia_sbr_channel_struct *pstr_sbr_channel[2];
336 ia_sbr_header_data_struct *ptr_header_data[MAXNRSBRCHANNELS];
337
338 WORD32 initial_sync_state;
339
340 ia_sbr_header_data_struct *ptr_sbr_dflt_header =
341 (ia_sbr_header_data_struct *)(&self->str_sbr_dflt_header);
342
343 ia_sbr_frame_info_data_struct *ptr_frame_data[2];
344 self->num_delay_frames = 1;
345 self->ptr_mps_data = NULL;
346
347 if (!ec_flag || !usac_flag) {
348 for (k = 0; k < 2; k++) {
349 ptr_frame_data[k] = (ia_sbr_frame_info_data_struct *)self->frame_buffer[k];
350
351 pstr_sbr_channel[k] = self->pstr_sbr_channel[k];
352
353 ptr_header_data[k] = self->pstr_sbr_header[k];
354
355 if (audio_object_type == AOT_ER_AAC_ELD) {
356 ptr_frame_data[k]->eld_sbr_flag = eld_sbr_flag;
357 ptr_frame_data[k]->num_time_slots = ptr_header_data[0]->num_time_slots;
358 }
359
360 ptr_frame_data[k]->usac_independency_flag = self->usac_independency_flag;
361 ptr_frame_data[k]->mps_sbr_flag = (self->stereo_config_idx == 3) ? 1 : 0;
362 ptr_frame_data[k]->stereo_config_idx = self->stereo_config_idx;
363 ptr_frame_data[k]->inter_tes_flag = self->inter_tes_flag;
364 ptr_frame_data[k]->sbr_mode = self->sbr_mode;
365
366 if (!usac_flag) {
367 ptr_frame_data[k]->usac_independency_flag = 0;
368 ptr_frame_data[k]->mps_sbr_flag = 0;
369 ptr_frame_data[k]->stereo_config_idx = -1;
370 ptr_frame_data[k]->inter_tes_flag = 0;
371 ptr_frame_data[k]->sbr_mode = ORIG_SBR;
372 }
373 }
374
375 if (init_flag) {
376 ptr_frame_data[1]->reset_flag = 1;
377 ptr_frame_data[0]->reset_flag = 1;
378 }
379
380 for (k = 0; k < *codec_num_channels; k++) {
381 ptr_header_data[k]->usac_flag = self->aot_usac_flag;
382 ptr_header_data[k]->enh_sbr = self->enh_sbr;
383 ptr_header_data[k]->enh_sbr_ps =
384 ((self->enh_sbr_ps) | (ptr_header_data[k]->channel_mode == PS_STEREO));
385
386 ptr_header_data[k]->usac_independency_flag = self->usac_independency_flag;
387 ptr_header_data[k]->hbe_flag = self->hbe_flag;
388 ptr_header_data[k]->pvc_flag = self->pvc_flag;
389
390 if (!usac_flag) {
391 ptr_header_data[k]->usac_independency_flag = 0;
392 ptr_header_data[k]->hbe_flag = 0;
393 ptr_header_data[k]->pvc_flag = 0;
394 }
395 ptr_header_data[k]->esbr_hq = self->esbr_hq;
396 if (!usac_flag &&
397 (!(audio_object_type == AOT_ER_AAC_ELD || audio_object_type == AOT_ER_AAC_LD))) {
398 ptr_header_data[k]->hbe_flag = 1;
399 }
400 }
401
402 initial_sync_state = ptr_header_data[0]->sync_state;
403
404 low_pow_flag = !usac_flag;
405 self->pstr_sbr_tables->sbr_rand_ph = self->pstr_sbr_tables->env_calc_tables_ptr->sbr_rand_ph;
406
407 if (ps_enable) {
408 if (num_channels == 1) {
409 low_pow_flag = 0;
410 }
411 }
412
413 if (audio_object_type == AOT_ER_AAC_ELD) {
414 low_pow_flag = 0;
415 }
416
417 prev_stereo = (ptr_header_data[0]->channel_mode == SBR_STEREO);
418
419 if (ps_enable) prev_ps_flag = (ptr_header_data[0]->channel_mode == PS_STEREO);
420
421 ptr_header_data[0]->err_flag_prev = ptr_header_data[0]->err_flag;
422
423 if (p_sbr_bit_stream->no_elements == 0) {
424 frame_status = 0;
425 ptr_header_data[0]->sync_state = UPSAMPLING;
426 if (num_channels == 2) ptr_header_data[1]->sync_state = UPSAMPLING;
427 }
428
429 if ((usac_flag)) {
430 if ((p_sbr_bit_stream->no_elements) && (p_sbr_bit_stream->str_sbr_ele->size_payload > 0)) {
431 num_elements = p_sbr_bit_stream->no_elements;
432 } else {
433 num_elements = 0;
434 }
435 }
436
437 for (k = 0; k < num_elements; k++) {
438 struct ia_bit_buf_struct local_bit_buf = {0};
439 ia_sbr_element_stream_struct *ptr_bit_str_ele = &p_sbr_bit_stream->str_sbr_ele[k];
440 ele_channels = (p_sbr_bit_stream->str_sbr_ele[0].sbr_ele_id == SBR_ID_CPE) ? 2 : 1;
441 if (!frame_status && ptr_header_data[k]->sync_state == SBR_ACTIVE && ec_flag) {
442 ixheaacd_ec_set_frame_error_flag(ptr_bit_str_ele, FRAME_ERROR_ALLSLOTS);
443 }
444
445 switch (ptr_bit_str_ele->sbr_ele_id) {
446 case SBR_ID_SCE:
447 case SBR_ID_CCE:
448 if (num_channels == 2) {
449 dual_mono = 1;
450 }
451 stereo = 0;
452 break;
453 case SBR_ID_CPE:
454 stereo = 1;
455 ptr_header_data[1] = ptr_header_data[0];
456
457 memcpy(self->pstr_sbr_header[1], self->pstr_sbr_header[0],
458 sizeof(ia_sbr_header_data_struct));
459 break;
460 default:
461 frame_status = 0;
462 }
463
464 if (frame_status) {
465 if (!usac_flag) {
466 if (!(audio_object_type == AOT_ER_AAC_LD || audio_object_type == AOT_ER_AAC_ELD)) {
467 WORD8 tmp[1024];
468 WORD32 tmp_payload;
469 memcpy(&tmp[0], ptr_bit_str_ele->ptr_sbr_data, ptr_bit_str_ele->size_payload);
470 memcpy(ptr_bit_str_ele->ptr_sbr_data, ptr_bit_str_ele->ptr_prev_sbr_data,
471 ptr_bit_str_ele->prev_size_payload);
472 memcpy(ptr_bit_str_ele->ptr_prev_sbr_data, &tmp[0], ptr_bit_str_ele->size_payload);
473 tmp_payload = ptr_bit_str_ele->size_payload;
474 ptr_bit_str_ele->size_payload = ptr_bit_str_ele->prev_size_payload;
475 ptr_bit_str_ele->prev_size_payload = tmp_payload;
476 }
477 if (!ptr_bit_str_ele->size_payload) {
478 continue;
479 }
480
481 ixheaacd_create_init_bit_buf(&local_bit_buf, (UWORD8 *)ptr_bit_str_ele->ptr_sbr_data,
482 ptr_bit_str_ele->size_payload);
483 it_bit_buff = &local_bit_buf;
484 it_bit_buff->xaac_jmp_buf = self->xaac_jmp_buf;
485 total_bits_left = it_bit_buff->cnt_bits;
486
487 if (audio_object_type == AOT_ER_AAC_ELD) {
488 if (eld_sbr_flag != 1) {
489 ixheaacd_read_bits_buf(&local_bit_buf, LEN_NIBBLE);
490 }
491 } else {
492 ixheaacd_read_bits_buf(&local_bit_buf, LEN_NIBBLE);
493 }
494 }
495 if (ptr_bit_str_ele->extension_type == SBR_EXTENSION_CRC) {
496 WORD32 crc_bits = 0;
497 WORD32 crc_check_flag = 0;
498 crc_check_flag = 1;
499 crc_bits = (((ptr_bit_str_ele->size_payload - 1) << 3) + (4 - SBR_CYC_REDCY_CHK_BITS));
500
501 if (crc_bits < 0) {
502 crc_check_flag = 0;
503 frame_status = 0;
504 }
505 if (crc_check_flag) frame_status = ixheaacd_sbr_crccheck(it_bit_buff, crc_bits);
506 }
507
508 if (!usac_flag) header_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
509
510 if (audio_object_type != AOT_ER_AAC_ELD) {
511 if (header_flag) {
512 header_flag = ixheaacd_sbr_read_header_data(ptr_header_data[k], it_bit_buff, stereo,
513 ptr_sbr_dflt_header);
514 if (usac_flag) {
515 if ((self->ptr_pvc_data_str->prev_pvc_mode == 0) &&
516 (ptr_header_data[k]->pvc_mode != 0)) {
517 self->ptr_pvc_data_str->prev_pvc_id = 0;
518 }
519 self->ptr_pvc_data_str->prev_pvc_mode = ptr_header_data[k]->pvc_mode;
520 if (ptr_header_data[k]->pvc_mode == 0) {
521 ptr_frame_data[k]->sbr_mode = ORIG_SBR;
522 } else {
523 ptr_frame_data[k]->sbr_mode = PVC_SBR;
524 }
525 }
526 if (header_flag == SBR_RESET) {
527 err = ixheaacd_calc_frq_bnd_tbls(ptr_header_data[k],
528
529 self->pstr_common_tables);
530 if (!err) {
531 WORD32 lr;
532 WORD32 lr1 = ps_enable ? 2 : num_channels;
533 for (lr = 0; lr < lr1; lr++) {
534 if (ldmps_present != 1) ptr_frame_data[lr]->reset_flag = 1;
535 if ((SBR_NOT_INITIALIZED == ptr_header_data[lr]->sync_state) && !usac_flag) {
536 ptr_frame_data[lr]->sbr_patching_mode = 1;
537 ptr_frame_data[lr]->over_sampling_flag = 0;
538 ptr_frame_data[lr]->pitch_in_bins = 0;
539 ptr_header_data[lr]->pre_proc_flag = 0;
540 }
541 err |= ixheaacd_sbr_dec_reset(
542 &(pstr_sbr_channel[lr]->str_sbr_dec), ptr_header_data[k], low_pow_flag,
543 self->pstr_common_tables, ptr_frame_data[k]->pitch_in_bins,
544 audio_object_type, sbr_scratch_struct->ptr_work_buf_core);
545 if (err < 0) return err;
546 }
547 }
548
549 if (err == 0) {
550 ptr_header_data[k]->sync_state = SBR_ACTIVE;
551 }
552 }
553 }
554 } else {
555 if (header_flag) {
556 header_flag = ixheaacd_sbr_read_header_data(ptr_header_data[k], it_bit_buff, stereo,
557 ptr_sbr_dflt_header);
558 if (usac_flag) {
559 if ((self->ptr_pvc_data_str->prev_pvc_mode == 0) &&
560 (ptr_header_data[k]->pvc_mode != 0)) {
561 self->ptr_pvc_data_str->prev_pvc_id = 0;
562 }
563 self->ptr_pvc_data_str->prev_pvc_mode = ptr_header_data[k]->pvc_mode;
564 if (ptr_header_data[k]->pvc_mode == 0) {
565 ptr_frame_data[k]->sbr_mode = ORIG_SBR;
566 } else {
567 ptr_frame_data[k]->sbr_mode = PVC_SBR;
568 }
569 }
570
571 if (header_flag == SBR_RESET) {
572 err = ixheaacd_calc_frq_bnd_tbls(ptr_header_data[k],
573
574 self->pstr_common_tables);
575 if (err) {
576 return err;
577 }
578 }
579 }
580
581 {
582 WORD32 lr;
583 WORD32 lr1 = ps_enable ? 2 : num_channels;
584 for (lr = 0; lr < lr1; lr++) {
585 if (ldmps_present != 1) ptr_frame_data[lr]->reset_flag = 1;
586 if (ptr_header_data[k]->status) {
587 err |= ixheaacd_sbr_dec_reset(
588 &(pstr_sbr_channel[lr]->str_sbr_dec), ptr_header_data[k], low_pow_flag,
589 self->pstr_common_tables, ptr_frame_data[k]->pitch_in_bins, audio_object_type,
590 sbr_scratch_struct->ptr_work_buf_core);
591 if (err < 0) return err;
592 }
593 }
594 ptr_header_data[k]->status = 0;
595 }
596
597 if (err == 0) {
598 ptr_header_data[k]->sync_state = SBR_ACTIVE;
599 }
600 }
601 }
602
603 if (err || (ptr_header_data[k]->sync_state == SBR_NOT_INITIALIZED)) {
604 WORD32 lr1 = ps_enable ? 2 : num_channels;
605 ixheaacd_prepare_upsamp(ptr_header_data, pstr_sbr_channel, lr1);
606 if (err) return err;
607 }
608
609 if (frame_status && (ptr_header_data[k]->sync_state == SBR_ACTIVE)) {
610 if (stereo) {
611 frame_status = ixheaacd_sbr_read_cpe(ptr_header_data[0], ptr_frame_data, it_bit_buff,
612 self->pstr_sbr_tables, audio_object_type);
613 if (frame_status < 0) return frame_status;
614 } else {
615 if (ps_enable) {
616 if (down_mix_flag) {
617 self->pstr_ps_stereo_dec->force_mono = 1;
618 } else {
619 self->pstr_ps_stereo_dec->force_mono = 0;
620 }
621 } else {
622 self->pstr_ps_stereo_dec = 0;
623 }
624 if (ptr_frame_data[k]->sbr_mode == ORIG_SBR) {
625 frame_status = ixheaacd_sbr_read_sce(
626 ptr_header_data[k], ptr_frame_data[k], self->pstr_ps_stereo_dec, it_bit_buff,
627 self->pstr_sbr_tables, audio_object_type, ec_flag);
628 if (frame_status < 0) return frame_status;
629 if (ec_flag && self->pstr_ps_stereo_dec != NULL) {
630 ixheaacd_copy_prev_ps_params(self->pstr_ps_stereo_dec, &self->str_ps_config_prev,
631 frame_status);
632 }
633 } else if (ptr_frame_data[k]->sbr_mode == PVC_SBR) {
634 frame_status = ixheaacd_sbr_read_pvc_sce(ptr_frame_data[k], it_bit_buff, 0,
635 self->ptr_pvc_data_str,
636 self->pstr_sbr_tables, ptr_header_data[k]);
637 if (frame_status < 0) return frame_status;
638 }
639 }
640 if (audio_object_type != AOT_ER_AAC_ELD) {
641 WORD32 total_bits_read;
642 total_bits_read = ixheaacd_no_bits_read(it_bit_buff);
643 if (total_bits_read > (ptr_bit_str_ele->size_payload << 3) ||
644 total_bits_read < ((ptr_bit_str_ele->size_payload << 3) - 8)) {
645 frame_status = 0;
646 }
647 if (ec_flag) {
648 if (!frame_status) {
649 ixheaacd_ec_set_frame_error_flag(ptr_bit_str_ele, FRAME_ERROR);
650 } else {
651 ixheaacd_ec_set_frame_error_flag(ptr_bit_str_ele, FRAME_OK);
652 }
653 }
654 }
655 }
656 if ((ldmps_present == 1) && (it_bit_buff)) {
657 WORD32 bits_decoded = (it_bit_buff->size - it_bit_buff->cnt_bits);
658 self->ptr_mps_data = (WORD8 *)it_bit_buff->ptr_read_next;
659 self->left_mps_bits = (total_bits_left - bits_decoded);
660 self->mps_bits_pos = it_bit_buff->bit_pos;
661 }
662 if (ec_flag) {
663 if (frame_status && !init_flag) {
664 ptr_bit_str_ele->use_frame_slot =
665 (ptr_bit_str_ele->use_frame_slot + 1) % (self->num_delay_frames + 1);
666 }
667 if (ptr_header_data[0]->sync_state == SBR_ACTIVE) {
668 ptr_header_data[k]->err_flag =
669 ptr_bit_str_ele->frame_error_flag[ptr_bit_str_ele->use_frame_slot];
670 }
671 }
672 }
673
674 if (!usac_flag) {
675 if (!frame_status || (ptr_header_data[0]->sync_state != SBR_ACTIVE) ||
676 ptr_header_data[0]->err_flag) {
677 ptr_header_data[0]->err_flag = 1;
678 stereo = (num_channels == 2) ? 1 : 0;
679 if (ptr_header_data[0]->channel_mode == 0) {
680 ptr_header_data[0]->channel_mode = stereo ? SBR_STEREO : SBR_MONO;
681 }
682 }
683
684 if (!(stereo || dual_mono)) {
685 ptr_frame_data[0]->coupling_mode = COUPLING_OFF;
686 ptr_frame_data[1]->coupling_mode = COUPLING_OFF;
687 }
688
689 if (ptr_header_data[0]->sync_state == SBR_NOT_INITIALIZED && !ec_flag) {
690 WORD32 lr1 = ps_enable ? 2 : num_channels;
691 ixheaacd_prepare_upsamp(ptr_header_data, pstr_sbr_channel, lr1);
692 }
693 }
694
695 if (ptr_header_data[0]->sync_state == SBR_ACTIVE) {
696 if (ptr_frame_data[0]->sbr_mode == PVC_SBR) {
697 err = ixheaacd_dec_sbrdata_for_pvc(ptr_header_data[0], ptr_frame_data[0],
698 pstr_sbr_channel[0]->pstr_prev_frame_data,
699 audio_object_type);
700 if (err) return err;
701 } else if (ptr_frame_data[0]->sbr_mode == ORIG_SBR) {
702 err = ixheaacd_dec_sbrdata(
703 ptr_header_data[0], ptr_header_data[1], ptr_frame_data[0],
704 pstr_sbr_channel[0]->pstr_prev_frame_data,
705 (stereo || dual_mono) ? ptr_frame_data[1] : NULL,
706 (stereo || dual_mono) ? pstr_sbr_channel[1]->pstr_prev_frame_data : NULL,
707 self->pstr_common_tables, ldmps_present, audio_object_type, ec_flag);
708
709 if (err) return err;
710 }
711
712 if (ptr_header_data[0]->channel_mode == PS_STEREO &&
713 (audio_object_type != AOT_ER_AAC_ELD && audio_object_type != AOT_ER_AAC_LD)) {
714 ixheaacd_decode_ps_data(self->pstr_ps_stereo_dec, frame_size);
715 ps_flag = 1;
716 self->ps_present = ps_flag;
717 }
718 if (ptr_header_data[0]->enh_sbr_ps) {
719 ps_flag = 1;
720 self->ps_present = ps_flag;
721 }
722 ptr_frame_data[0]->max_qmf_subband_aac =
723 ptr_header_data[0]->pstr_freq_band_data->sub_band_start;
724 if (stereo) {
725 ptr_frame_data[1]->max_qmf_subband_aac =
726 ptr_header_data[1]->pstr_freq_band_data->sub_band_start;
727 }
728 if (ldmps_present == 1) {
729 ptr_frame_data[0]->rate = 1;
730 if (stereo) {
731 ptr_frame_data[1]->rate = 1;
732 }
733 }
734 }
735 if (audio_object_type == AOT_ER_AAC_ELD && ec_flag) {
736 if ((initial_sync_state == SBR_NOT_INITIALIZED) && ptr_header_data[0]->err_flag) {
737 ptr_header_data[0]->sync_state = SBR_NOT_INITIALIZED;
738 }
739 } else {
740 if (audio_object_type != AOT_ER_AAC_ELD) {
741 if ((initial_sync_state == SBR_NOT_INITIALIZED) && ptr_header_data[0]->err_flag) {
742 ptr_header_data[0]->sync_state = SBR_NOT_INITIALIZED;
743 }
744 } else {
745 ptr_header_data[0]->sync_state = SBR_ACTIVE;
746 }
747 }
748
749 if ((num_channels == 2) && !(stereo || dual_mono)) {
750 ixheaacd_downmix_to_monosbr(&core_sample_buf[slot_element], ch_fac);
751 }
752
753 if ((!prev_stereo && !prev_ps_flag) && (ps_flag)) {
754 WORD32 copy_size;
755 if (down_samp_flag)
756 copy_size = QMF_FILTER_STATE_SYN_SIZE_DOWN_SAMPLED;
757 else
758 copy_size = QMF_FILTER_STATE_SYN_SIZE;
759
760 memcpy(pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
761 pstr_sbr_channel[0]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
762 copy_size * sizeof(WORD16));
763
764 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale =
765 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale;
766 }
767
768 if ((!prev_stereo && stereo && (num_channels == 2)) &&
769 (audio_object_type != AOT_ER_AAC_ELD)) {
770 WORD32 copy_size;
771 if (down_samp_flag)
772 copy_size = QMF_FILTER_STATE_SYN_SIZE_DOWN_SAMPLED;
773 else
774 copy_size = QMF_FILTER_STATE_SYN_SIZE;
775
776 memcpy(pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
777 pstr_sbr_channel[0]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
778 copy_size * sizeof(WORD16));
779
780 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale =
781 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale;
782
783 memcpy(pstr_sbr_channel[1]->str_sbr_dec.str_codec_qmf_bank.anal_filter_states,
784 pstr_sbr_channel[0]->str_sbr_dec.str_codec_qmf_bank.anal_filter_states,
785 QMF_FILTER_STATE_ANA_SIZE * sizeof(WORD16));
786
787 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_lb_scale =
788 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_lb_scale;
789
790 memcpy(pstr_sbr_channel[1]->str_sbr_dec.ptr_sbr_overlap_buf,
791 pstr_sbr_channel[0]->str_sbr_dec.ptr_sbr_overlap_buf,
792 MAX_OV_COLS * NO_SYNTHESIS_CHANNELS * sizeof(WORD32));
793
794 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.ov_lb_scale =
795 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.ov_lb_scale;
796 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.ov_hb_scale =
797 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.ov_hb_scale;
798 }
799 pstr_sbr_channel[0]->str_sbr_dec.time_sample_buf = self->time_sample_buf[0];
800 if (self->pstr_ps_stereo_dec != NULL && (ps_enable || self->enh_sbr_ps)) {
801 self->pstr_ps_stereo_dec->pp_qmf_buf_real[0] =
802 pstr_sbr_channel[0]->str_sbr_dec.pp_qmf_buf_real;
803 self->pstr_ps_stereo_dec->pp_qmf_buf_imag[0] =
804 pstr_sbr_channel[0]->str_sbr_dec.pp_qmf_buf_imag;
805 self->pstr_ps_stereo_dec->pp_qmf_buf_real[1] =
806 pstr_sbr_channel[1]->str_sbr_dec.pp_qmf_buf_real;
807 self->pstr_ps_stereo_dec->pp_qmf_buf_imag[1] =
808 pstr_sbr_channel[1]->str_sbr_dec.pp_qmf_buf_imag;
809 self->pstr_ps_stereo_dec->time_sample_buf[0] = self->time_sample_buf[0];
810 self->pstr_ps_stereo_dec->time_sample_buf[1] = self->time_sample_buf[1];
811 }
812 } else {
813 for (k = 0; k < 2; k++) {
814 ptr_frame_data[k] = (ia_sbr_frame_info_data_struct *)self->frame_buffer[k];
815
816 pstr_sbr_channel[k] = self->pstr_sbr_channel[k];
817
818 ptr_header_data[k] = self->pstr_sbr_header[k];
819 ptr_header_data[k]->usac_flag = self->aot_usac_flag;
820 }
821
822 if ((p_sbr_bit_stream->no_elements) && (p_sbr_bit_stream->str_sbr_ele->size_payload > 0)) {
823 num_elements = p_sbr_bit_stream->no_elements;
824 } else {
825 num_elements = 0;
826 }
827
828 for (k = 0; k < num_elements; k++) {
829 ia_sbr_element_stream_struct *ptr_bit_str_ele = &p_sbr_bit_stream->str_sbr_ele[k];
830 ele_channels = (p_sbr_bit_stream->str_sbr_ele[0].sbr_ele_id == SBR_ID_CPE) ? 2 : 1;
831 {
832 switch (ptr_bit_str_ele->sbr_ele_id) {
833 case SBR_ID_SCE:
834 case SBR_ID_CCE:
835 if (num_channels == 2) {
836 dual_mono = 1;
837 }
838 stereo = 0;
839 break;
840 case SBR_ID_CPE:
841 stereo = 1;
842 ptr_header_data[1] = ptr_header_data[0];
843
844 memcpy(self->pstr_sbr_header[1], self->pstr_sbr_header[0],
845 sizeof(ia_sbr_header_data_struct));
846 break;
847 default:
848 frame_status = 0;
849 }
850 }
851 }
852 }
853 if (ec_flag) {
854 for (k = 0; k < 2; k++) {
855 if (pstr_sbr_channel[k]->str_sbr_dec.band_count == 0) {
856 pstr_sbr_channel[k]->str_sbr_dec.band_count =
857 pstr_sbr_channel[k]->str_sbr_dec.str_codec_qmf_bank.no_channels;
858 }
859 }
860 }
861 if (ec_flag && usac_flag && !first_frame && !self->sbr_parse_complete) {
862 return IA_FATAL_ERROR;
863 }
864 if (!ec_flag || !first_frame || init_flag) {
865 if (pstr_drc_dec == NULL) {
866 WORD32 err_code = 0;
867 err_code = ixheaacd_sbr_dec(
868 &pstr_sbr_channel[0]->str_sbr_dec, core_sample_buf + slot_element, ptr_header_data[0],
869 ptr_frame_data[0], pstr_sbr_channel[0]->pstr_prev_frame_data, self->pstr_ps_stereo_dec,
870 &pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank,
871 &pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact,
872 (ptr_header_data[0]->sync_state == SBR_ACTIVE), low_pow_flag,
873 sbr_scratch_struct->ptr_work_buf_core, self->pstr_sbr_tables, self->pstr_common_tables,
874 ch_fac, self->ptr_pvc_data_str, 0, NULL, audio_object_type, ldmps_present, self,
875 heaac_mps_present, ec_flag);
876 if (err_code) return err_code;
877 if (!self->enh_sbr_ps) {
878 if ((ptr_header_data[0]->sync_state == SBR_ACTIVE) && !ptr_frame_data[0]->mps_sbr_flag &&
879 ch_fac != 2) {
880 ptr_header_data[0]->pstr_freq_band_data[0].qmf_sb_prev =
881 ptr_header_data[0]->pstr_freq_band_data->sub_band_start;
882 }
883 } else {
884 if (ptr_header_data[0]->sync_state == SBR_ACTIVE) {
885 ptr_header_data[0]->pstr_freq_band_data[0].qmf_sb_prev =
886 ptr_header_data[0]->pstr_freq_band_data->sub_band_start;
887 }
888 }
889 } else {
890 WORD32 err_code = 0;
891 err_code = ixheaacd_sbr_dec(
892 &pstr_sbr_channel[0]->str_sbr_dec, core_sample_buf + slot_element, ptr_header_data[0],
893 ptr_frame_data[0], pstr_sbr_channel[0]->pstr_prev_frame_data, self->pstr_ps_stereo_dec,
894 &pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank,
895 &pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact,
896 (ptr_header_data[0]->sync_state == SBR_ACTIVE), low_pow_flag,
897 sbr_scratch_struct->ptr_work_buf_core, self->pstr_sbr_tables, self->pstr_common_tables,
898 ch_fac, self->ptr_pvc_data_str, pstr_drc_dec->drc_on,
899 pstr_drc_dec->str_drc_channel_data[0].drc_factors_sbr, audio_object_type, ldmps_present,
900 self, heaac_mps_present, ec_flag);
901 if (err_code) return err_code;
902 if (!self->enh_sbr_ps) {
903 if ((ptr_header_data[0]->sync_state == SBR_ACTIVE) && !ptr_frame_data[0]->mps_sbr_flag &&
904 num_channels != 2) {
905 ptr_header_data[0]->pstr_freq_band_data[0].qmf_sb_prev =
906 ptr_header_data[0]->pstr_freq_band_data->sub_band_start;
907 }
908 } else {
909 if (ptr_header_data[0]->sync_state == SBR_ACTIVE) {
910 ptr_header_data[0]->pstr_freq_band_data[0].qmf_sb_prev =
911 ptr_header_data[0]->pstr_freq_band_data->sub_band_start;
912 }
913 }
914 }
915
916 if (!down_mix_flag && (stereo || dual_mono) && (num_channels == 2)) {
917 pstr_sbr_channel[1]->str_sbr_dec.time_sample_buf = self->time_sample_buf[1];
918
919 if (ele_channels == 1 && usac_flag) {
920 WORD32 err_code =
921 ixheaacd_esbr_dec(&pstr_sbr_channel[1]->str_sbr_dec, ptr_header_data[1],
922 ptr_frame_data[1], (ptr_header_data[1]->sync_state == SBR_ACTIVE),
923 low_pow_flag, self->pstr_sbr_tables, ch_fac);
924 if (err_code) return err_code;
925 } else {
926 if (pstr_drc_dec == NULL) {
927 WORD32 err_code = ixheaacd_sbr_dec(
928 &pstr_sbr_channel[1]->str_sbr_dec, core_sample_buf + slot_element + 1,
929 ptr_header_data[1], ptr_frame_data[1], pstr_sbr_channel[1]->pstr_prev_frame_data,
930 NULL, NULL, NULL, (ptr_header_data[1]->sync_state == SBR_ACTIVE), low_pow_flag,
931 sbr_scratch_struct->ptr_work_buf_core, self->pstr_sbr_tables,
932 self->pstr_common_tables, ch_fac, self->ptr_pvc_data_str, 0, NULL,
933 audio_object_type, ldmps_present, self, heaac_mps_present, ec_flag);
934 if (err_code) return err_code;
935 if (!self->enh_sbr_ps) {
936 if ((ptr_header_data[1]->sync_state == SBR_ACTIVE) &&
937 !ptr_frame_data[0]->mps_sbr_flag) {
938 ptr_header_data[1]->pstr_freq_band_data[0].qmf_sb_prev =
939 ptr_header_data[1]->pstr_freq_band_data->sub_band_start;
940 }
941 } else {
942 if (ptr_header_data[1]->sync_state == SBR_ACTIVE) {
943 ptr_header_data[1]->pstr_freq_band_data[0].qmf_sb_prev =
944 ptr_header_data[1]->pstr_freq_band_data->sub_band_start;
945 }
946 }
947 } else {
948 WORD32 err_code = ixheaacd_sbr_dec(
949 &pstr_sbr_channel[1]->str_sbr_dec, core_sample_buf + slot_element + 1,
950 ptr_header_data[1], ptr_frame_data[1], pstr_sbr_channel[1]->pstr_prev_frame_data,
951 NULL, NULL, NULL, (ptr_header_data[1]->sync_state == SBR_ACTIVE), low_pow_flag,
952 sbr_scratch_struct->ptr_work_buf_core, self->pstr_sbr_tables,
953 self->pstr_common_tables, ch_fac, self->ptr_pvc_data_str, pstr_drc_dec->drc_on,
954 pstr_drc_dec->str_drc_channel_data[1].drc_factors_sbr, audio_object_type,
955 ldmps_present, self, heaac_mps_present, ec_flag);
956 if (err_code) return err_code;
957 if (!self->enh_sbr_ps) {
958 if ((ptr_header_data[1]->sync_state == SBR_ACTIVE) &&
959 !ptr_frame_data[0]->mps_sbr_flag) {
960 ptr_header_data[1]->pstr_freq_band_data[0].qmf_sb_prev =
961 ptr_header_data[1]->pstr_freq_band_data->sub_band_start;
962 }
963 } else {
964 if (ptr_header_data[1]->sync_state == SBR_ACTIVE) {
965 ptr_header_data[1]->pstr_freq_band_data[0].qmf_sb_prev =
966 ptr_header_data[1]->pstr_freq_band_data->sub_band_start;
967 }
968 }
969 }
970 }
971
972 } else {
973 if (audio_object_type != AOT_ER_AAC_ELD && audio_object_type != AOT_ER_AAC_LD)
974
975 {
976 if (sub_d((WORD16)ptr_header_data[0]->channel_mode, PS_STEREO) == 0) {
977 num_channels = 2;
978 }
979 if (ptr_header_data[0]->enh_sbr_ps) {
980 num_channels = 2;
981 }
982 }
983 }
984 *codec_num_channels = num_channels;
985 self->sbr_mode = ptr_frame_data[0]->sbr_mode;
986
987 if ((audio_object_type == AOT_ER_AAC_ELD) || (audio_object_type == AOT_ER_AAC_LD)) {
988 if (pstr_drc_dec != NULL) {
989 WORD32 i, j;
990 for (i = 0; i < *codec_num_channels; i++) {
991 for (j = 0; j < 32; j++) {
992 memcpy(pstr_drc_dec->str_drc_channel_data[i].drc_factors_sbr[j],
993 pstr_drc_dec->str_drc_channel_data[i].drc_factors_sbr[j + 32],
994 64 * sizeof(WORD32));
995 }
996 }
997 }
998 }
999 if (ec_flag) {
1000 self->band_count[0] = pstr_sbr_channel[0]->str_sbr_dec.band_count;
1001 self->band_count[1] = pstr_sbr_channel[1]->str_sbr_dec.band_count;
1002 }
1003 }
1004
1005 return SBRDEC_OK;
1006 }
1007
ixheaacd_parse_sbr(ia_handle_sbr_dec_inst_struct self,ia_aac_dec_sbr_bitstream_struct * p_sbr_bit_stream,WORD16 * codec_num_channels,FLAG frame_status,ia_sbr_scr_struct * sbr_scratch_struct,ia_bit_buf_struct * it_bit_buff,WORD32 audio_object_type)1008 IA_ERRORCODE ixheaacd_parse_sbr(ia_handle_sbr_dec_inst_struct self,
1009 ia_aac_dec_sbr_bitstream_struct *p_sbr_bit_stream,
1010 WORD16 *codec_num_channels, FLAG frame_status,
1011 ia_sbr_scr_struct *sbr_scratch_struct,
1012 ia_bit_buf_struct *it_bit_buff, WORD32 audio_object_type) {
1013 WORD32 k;
1014 FLAG ps_flag = 0;
1015 FLAG stereo = 0;
1016 FLAG low_pow_flag = 0;
1017 FLAG header_flag = 1;
1018 FLAG dual_mono = 0;
1019 WORD32 err = 0;
1020 WORD32 num_channels = *codec_num_channels;
1021 FLAG prev_stereo;
1022 WORD32 num_elements = p_sbr_bit_stream->no_elements;
1023 WORD32 usac_flag = self->aot_usac_flag;
1024
1025 ia_sbr_channel_struct *pstr_sbr_channel[2];
1026 ia_sbr_header_data_struct *ptr_header_data[MAXNRSBRCHANNELS];
1027
1028 WORD32 initial_sync_state;
1029
1030 ia_sbr_header_data_struct *ptr_sbr_dflt_header =
1031 (ia_sbr_header_data_struct *)(&self->str_sbr_dflt_header);
1032
1033 ia_sbr_frame_info_data_struct *ptr_frame_data[2];
1034
1035 self->num_delay_frames = 1;
1036
1037 self->ptr_mps_data = NULL;
1038
1039 if (usac_flag && self->ec_flag) {
1040 for (k = 0; k < 2; k++) {
1041 ptr_frame_data[k] = (ia_sbr_frame_info_data_struct *)self->frame_buffer[k];
1042
1043 pstr_sbr_channel[k] = self->pstr_sbr_channel[k];
1044
1045 ptr_header_data[k] = self->pstr_sbr_header[k];
1046
1047 ptr_frame_data[k]->usac_independency_flag = self->usac_independency_flag;
1048 ptr_frame_data[k]->mps_sbr_flag = (self->stereo_config_idx == 3) ? 1 : 0;
1049 ptr_frame_data[k]->stereo_config_idx = self->stereo_config_idx;
1050 ptr_frame_data[k]->inter_tes_flag = self->inter_tes_flag;
1051 ptr_frame_data[k]->sbr_mode = self->sbr_mode;
1052 }
1053
1054 for (k = 0; k < *codec_num_channels; k++) {
1055 ptr_header_data[k]->usac_flag = self->aot_usac_flag;
1056
1057 ptr_header_data[k]->enh_sbr = self->enh_sbr;
1058 ptr_header_data[k]->enh_sbr_ps =
1059 ((self->enh_sbr_ps) | (ptr_header_data[k]->channel_mode == PS_STEREO));
1060 ptr_header_data[k]->usac_independency_flag = self->usac_independency_flag;
1061 ptr_header_data[k]->hbe_flag = self->hbe_flag;
1062 ptr_header_data[k]->pvc_flag = self->pvc_flag;
1063
1064 ptr_header_data[k]->esbr_hq = self->esbr_hq;
1065 }
1066
1067 initial_sync_state = ptr_header_data[0]->sync_state;
1068
1069 low_pow_flag = 0;
1070 self->pstr_sbr_tables->sbr_rand_ph = self->pstr_sbr_tables->env_calc_tables_ptr->sbr_rand_ph;
1071
1072 prev_stereo = (ptr_header_data[0]->channel_mode == SBR_STEREO);
1073
1074 ptr_header_data[0]->err_flag_prev = ptr_header_data[0]->err_flag;
1075
1076 if (p_sbr_bit_stream->no_elements == 0) {
1077 frame_status = 0;
1078 ptr_header_data[0]->sync_state = UPSAMPLING;
1079 if (num_channels == 2) ptr_header_data[1]->sync_state = UPSAMPLING;
1080 }
1081
1082 if ((p_sbr_bit_stream->no_elements) && (p_sbr_bit_stream->str_sbr_ele->size_payload > 0)) {
1083 num_elements = p_sbr_bit_stream->no_elements;
1084 } else {
1085 num_elements = 0;
1086 }
1087
1088 for (k = 0; k < num_elements; k++) {
1089 ia_sbr_element_stream_struct *ptr_bit_str_ele = &p_sbr_bit_stream->str_sbr_ele[k];
1090
1091 switch (ptr_bit_str_ele->sbr_ele_id) {
1092 case SBR_ID_SCE:
1093 case SBR_ID_CCE:
1094 if (num_channels == 2) {
1095 dual_mono = 1;
1096 }
1097 stereo = 0;
1098 break;
1099 case SBR_ID_CPE:
1100 stereo = 1;
1101 ptr_header_data[1] = ptr_header_data[0];
1102
1103 memcpy(self->pstr_sbr_header[1], self->pstr_sbr_header[0],
1104 sizeof(*(self->pstr_sbr_header[1])));
1105 break;
1106 default:
1107 frame_status = 0;
1108 }
1109
1110 if (frame_status) {
1111 if (ptr_bit_str_ele->extension_type == SBR_EXTENSION_CRC) {
1112 WORD32 crc_bits = 0;
1113 WORD32 crc_check_flag = 0;
1114 crc_check_flag = 1;
1115 crc_bits = (((ptr_bit_str_ele->size_payload - 1) << 3) + (4 - SBR_CYC_REDCY_CHK_BITS));
1116
1117 if (crc_bits < 0) {
1118 crc_check_flag = 0;
1119 frame_status = 0;
1120 }
1121 if (crc_check_flag && frame_status == 1)
1122 frame_status = ixheaacd_sbr_crccheck(it_bit_buff, crc_bits);
1123 }
1124
1125 if (frame_status == 1) {
1126 header_flag = ixheaacd_sbr_read_header_data(ptr_header_data[k], it_bit_buff, stereo,
1127 ptr_sbr_dflt_header);
1128 }
1129 if (usac_flag) {
1130 if ((self->ptr_pvc_data_str->prev_pvc_mode == 0) &&
1131 (ptr_header_data[k]->pvc_mode != 0)) {
1132 self->ptr_pvc_data_str->prev_pvc_id = 0;
1133 }
1134 self->ptr_pvc_data_str->prev_pvc_mode = ptr_header_data[k]->pvc_mode;
1135 if (ptr_header_data[k]->pvc_mode == 0) {
1136 ptr_frame_data[k]->sbr_mode = ORIG_SBR;
1137 } else {
1138 ptr_frame_data[k]->sbr_mode = PVC_SBR;
1139 }
1140 }
1141 if (header_flag == SBR_RESET) {
1142 err = ixheaacd_calc_frq_bnd_tbls(ptr_header_data[k],
1143
1144 self->pstr_common_tables);
1145 if (!err) {
1146 WORD32 lr;
1147 WORD32 lr1 = num_channels;
1148 for (lr = 0; lr < lr1; lr++) {
1149 ptr_frame_data[lr]->reset_flag = 1;
1150 if ((SBR_NOT_INITIALIZED == ptr_header_data[lr]->sync_state) && !usac_flag) {
1151 ptr_frame_data[lr]->sbr_patching_mode = 1;
1152 ptr_frame_data[lr]->over_sampling_flag = 0;
1153 ptr_frame_data[lr]->pitch_in_bins = 0;
1154 ptr_header_data[lr]->pre_proc_flag = 0;
1155 }
1156
1157 err |= ixheaacd_sbr_dec_reset(
1158 &(pstr_sbr_channel[lr]->str_sbr_dec), ptr_header_data[k], low_pow_flag,
1159 self->pstr_common_tables, ptr_frame_data[k]->pitch_in_bins, audio_object_type,
1160 sbr_scratch_struct->ptr_work_buf_core);
1161 if (err < 0) {
1162 if (self->ec_flag) {
1163 self->frame_ok = 0;
1164 } else {
1165 return err;
1166 }
1167 }
1168 }
1169 }
1170
1171 if (err == 0) {
1172 ptr_header_data[k]->sync_state = SBR_ACTIVE;
1173 }
1174 }
1175 }
1176
1177 if (err || (ptr_header_data[k]->sync_state == SBR_NOT_INITIALIZED)) {
1178 WORD32 lr1 = num_channels;
1179 ixheaacd_prepare_upsamp(ptr_header_data, pstr_sbr_channel, lr1);
1180 if (err && !self->ec_flag) {
1181 return err;
1182 }
1183 }
1184
1185 if (frame_status && (ptr_header_data[k]->sync_state == SBR_ACTIVE)) {
1186 if (stereo) {
1187 frame_status = ixheaacd_sbr_read_cpe(ptr_header_data[0], ptr_frame_data, it_bit_buff,
1188 self->pstr_sbr_tables, audio_object_type);
1189 if (frame_status < 0) return frame_status;
1190 } else {
1191 self->pstr_ps_stereo_dec = 0;
1192
1193 if (ptr_frame_data[k]->sbr_mode == ORIG_SBR) {
1194 frame_status = ixheaacd_sbr_read_sce(ptr_header_data[k], ptr_frame_data[k],
1195 self->pstr_ps_stereo_dec, it_bit_buff,
1196 self->pstr_sbr_tables, audio_object_type, 0);
1197 if (frame_status < 0) return frame_status;
1198 } else if (ptr_frame_data[k]->sbr_mode == PVC_SBR) {
1199 frame_status = ixheaacd_sbr_read_pvc_sce(ptr_frame_data[k], it_bit_buff, 0,
1200 self->ptr_pvc_data_str,
1201 self->pstr_sbr_tables, ptr_header_data[k]);
1202 if (frame_status < 0) return frame_status;
1203 }
1204 }
1205 {
1206 WORD32 total_bits_read;
1207 total_bits_read = ixheaacd_no_bits_read(it_bit_buff);
1208 if (total_bits_read > (ptr_bit_str_ele->size_payload << 3) ||
1209 total_bits_read < ((ptr_bit_str_ele->size_payload << 3) - 8)) {
1210 frame_status = 0;
1211 }
1212 }
1213 } else {
1214 if (frame_status && self->ec_flag) {
1215 err = IA_XHEAAC_DEC_EXE_NONFATAL_SBR_PARSE_ERROR;
1216 self->sbr_parse_err_flag = 1;
1217 }
1218 if (!frame_status) {
1219 ixheaacd_ec_set_frame_error_flag(ptr_bit_str_ele, FRAME_ERROR);
1220 } else {
1221 ixheaacd_ec_set_frame_error_flag(ptr_bit_str_ele, FRAME_OK);
1222 }
1223 }
1224 }
1225 if (ptr_header_data[0]->sync_state == SBR_ACTIVE) {
1226 if (ptr_frame_data[0]->sbr_mode == PVC_SBR) {
1227 err = ixheaacd_dec_sbrdata_for_pvc(ptr_header_data[0], ptr_frame_data[0],
1228 pstr_sbr_channel[0]->pstr_prev_frame_data,
1229 audio_object_type);
1230 if (err) {
1231 if (self->ec_flag) {
1232 self->frame_ok = 0;
1233 } else {
1234 return err;
1235 }
1236 }
1237 } else if (ptr_frame_data[0]->sbr_mode == ORIG_SBR) {
1238 err = ixheaacd_dec_sbrdata(
1239 ptr_header_data[0], ptr_header_data[1], ptr_frame_data[0],
1240 pstr_sbr_channel[0]->pstr_prev_frame_data,
1241 (stereo || dual_mono) ? ptr_frame_data[1] : NULL,
1242 (stereo || dual_mono) ? pstr_sbr_channel[1]->pstr_prev_frame_data : NULL,
1243 self->pstr_common_tables, 0, audio_object_type, self->ec_flag);
1244
1245 if (err) {
1246 if (self->ec_flag) {
1247 self->frame_ok = 0;
1248 } else {
1249 return err;
1250 }
1251 }
1252 }
1253
1254 if (ptr_header_data[0]->channel_mode == PS_STEREO &&
1255 (audio_object_type != AOT_ER_AAC_ELD && audio_object_type != AOT_ER_AAC_LD)) {
1256 ixheaacd_decode_ps_data(self->pstr_ps_stereo_dec, 1024);
1257 ps_flag = 1;
1258 self->ps_present = ps_flag;
1259 }
1260
1261 if (ptr_header_data[0]->enh_sbr_ps) {
1262 ps_flag = 1;
1263 self->ps_present = ps_flag;
1264 }
1265
1266 ptr_frame_data[0]->max_qmf_subband_aac =
1267 ptr_header_data[0]->pstr_freq_band_data->sub_band_start;
1268 if (stereo) {
1269 ptr_frame_data[1]->max_qmf_subband_aac =
1270 ptr_header_data[1]->pstr_freq_band_data->sub_band_start;
1271 }
1272 }
1273 if (audio_object_type != AOT_ER_AAC_ELD) {
1274 if ((initial_sync_state == SBR_NOT_INITIALIZED) && ptr_header_data[0]->err_flag) {
1275 ptr_header_data[0]->sync_state = SBR_NOT_INITIALIZED;
1276 }
1277 } else {
1278 ptr_header_data[0]->sync_state = SBR_ACTIVE;
1279 }
1280
1281 if ((!prev_stereo && stereo && (num_channels == 2)) &&
1282 (audio_object_type != AOT_ER_AAC_ELD)) {
1283 memcpy(pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
1284 pstr_sbr_channel[0]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
1285 QMF_FILTER_STATE_SYN_SIZE * sizeof(WORD16));
1286
1287 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale =
1288 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale;
1289
1290 memcpy(pstr_sbr_channel[1]->str_sbr_dec.str_codec_qmf_bank.anal_filter_states,
1291 pstr_sbr_channel[0]->str_sbr_dec.str_codec_qmf_bank.anal_filter_states,
1292 QMF_FILTER_STATE_ANA_SIZE * sizeof(WORD16));
1293
1294 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_lb_scale =
1295 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_lb_scale;
1296
1297 memcpy(pstr_sbr_channel[1]->str_sbr_dec.ptr_sbr_overlap_buf,
1298 pstr_sbr_channel[0]->str_sbr_dec.ptr_sbr_overlap_buf,
1299 MAX_OV_COLS * NO_SYNTHESIS_CHANNELS * sizeof(WORD32));
1300
1301 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.ov_lb_scale =
1302 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.ov_lb_scale;
1303 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.ov_hb_scale =
1304 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.ov_hb_scale;
1305 }
1306
1307 pstr_sbr_channel[0]->str_sbr_dec.time_sample_buf = self->time_sample_buf[0];
1308 self->sbr_parse_complete = 1;
1309 }
1310
1311 return err;
1312 }