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
31 #include "ixheaacd_intrinsics.h"
32 #include "ixheaacd_sbr_const.h"
33 #include "ixheaacd_basic_op.h"
34 #include "ixheaacd_defines.h"
35 #include "ixheaacd_bitbuffer.h"
36 #include "ixheaacd_pns.h"
37
38 #include "ixheaacd_aac_rom.h"
39 #include "ixheaacd_pulsedata.h"
40
41 #include "ixheaacd_drc_data_struct.h"
42 #include "ixheaacd_lt_predict.h"
43 #include "ixheaacd_channelinfo.h"
44 #include "ixheaacd_drc_dec.h"
45
46 #include "ixheaacd_sbrdecoder.h"
47
48 #include "ixheaacd_sbrdecsettings.h"
49 #include "ixheaacd_sbr_scale.h"
50 #include "ixheaacd_lpp_tran.h"
51 #include "ixheaacd_env_extr_part.h"
52 #include "ixheaacd_sbr_rom.h"
53 #include "ixheaacd_hybrid.h"
54 #include "ixheaacd_ps_dec.h"
55 #include "ixheaacd_ps_bitdec.h"
56 #include "ixheaacd_env_extr.h"
57 #include "ixheaacd_common_rom.h"
58 #include "ixheaacd_freq_sca.h"
59
60 #include "ixheaacd_qmf_dec.h"
61
62 #include "ixheaacd_env_calc.h"
63
64 #include "ixheaacd_pvc_dec.h"
65 #include "ixheaacd_sbr_dec.h"
66 #include "ixheaacd_env_dec.h"
67 #include "ixheaacd_basic_funcs.h"
68 #include "ixheaacd_sbr_crc.h"
69
70 #include "ixheaacd_audioobjtypes.h"
71
ixheaacd_downmix_to_monosbr(WORD16 * core_sample_buf,WORD32 ch_fac)72 VOID ixheaacd_downmix_to_monosbr(WORD16 *core_sample_buf, WORD32 ch_fac) {
73 WORD32 i;
74 WORD16 *ptr1 = &core_sample_buf[0];
75 WORD16 *ptr2 = &core_sample_buf[1];
76
77 for (i = MAX_FRAME_SIZE - 1; i >= 0; i--) {
78 *ptr1 = ((*ptr1 >> 1) + (*ptr2 >> 1));
79
80 ptr1 += ch_fac;
81 ptr2 += ch_fac;
82 }
83 }
84
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)85 static WORD32 ixheaacd_sbr_dec_reset(ia_sbr_dec_struct *ptr_sbr_dec,
86 ia_sbr_header_data_struct *ptr_header_data,
87 FLAG low_pow_flag,
88 ixheaacd_misc_tables *pstr_common_tables,
89 WORD32 pitch_in_bins,
90 WORD32 audio_object_type) {
91 WORD32 old_lsb, new_lsb;
92 WORD32 l;
93 WORD32 err = 0;
94 WORD32 num_time_slots = ptr_sbr_dec->str_codec_qmf_bank.num_time_slots;
95 WORD32 upsample_ratio_idx = ptr_header_data->sbr_ratio_idx;
96 WORD32 op_delay = 6 + SBR_HF_ADJ_OFFSET;
97 WORD32 hbe_flag = ptr_header_data->hbe_flag;
98 WORD32 usac_flag = ptr_header_data->usac_flag;
99
100 if (ptr_header_data->is_usf_4) {
101 op_delay = op_delay + 6;
102 }
103
104 ixheaacd_reset_sbrenvelope_calc(&ptr_sbr_dec->str_sbr_calc_env);
105
106 new_lsb = ptr_header_data->pstr_freq_band_data->sub_band_start;
107 ptr_sbr_dec->str_synthesis_qmf_bank.lsb = new_lsb;
108 ptr_sbr_dec->str_synthesis_qmf_bank.usb =
109 ptr_header_data->pstr_freq_band_data->sub_band_end;
110
111 old_lsb = ptr_sbr_dec->str_synthesis_qmf_bank.lsb;
112 ptr_sbr_dec->str_codec_qmf_bank.lsb = 0;
113 ptr_sbr_dec->str_codec_qmf_bank.usb = old_lsb;
114
115 {
116 WORD32 *plpc_filt_states_real =
117 &ptr_sbr_dec->str_hf_generator.lpc_filt_states_real[0][old_lsb];
118 WORD32 *plpc_filt_states_real_1 =
119 &ptr_sbr_dec->str_hf_generator.lpc_filt_states_real[1][old_lsb];
120
121 WORD32 *plpc_filt_states_imag =
122 &ptr_sbr_dec->str_hf_generator.lpc_filt_states_imag[0][old_lsb];
123 WORD32 *plpc_filt_states_imag_1 =
124 &ptr_sbr_dec->str_hf_generator.lpc_filt_states_imag[1][old_lsb];
125
126 for (l = new_lsb - old_lsb - 1; l >= 0; l--) {
127 *plpc_filt_states_real++ = *plpc_filt_states_real_1++ = 0L;
128
129 if (!low_pow_flag) {
130 *plpc_filt_states_imag++ = *plpc_filt_states_imag_1++ = 0L;
131 }
132 }
133 }
134
135 if (usac_flag) {
136 WORD32 start_band;
137 WORD32 stop_band;
138 WORD32 start_slot = SBR_HF_ADJ_OFFSET;
139 WORD32 k;
140
141 start_band = ptr_header_data->pstr_freq_band_data->qmf_sb_prev;
142 stop_band = ptr_header_data->pstr_freq_band_data->sub_band_start;
143 if (!hbe_flag) {
144 for (l = 0; l < SBR_HF_ADJ_OFFSET; l++) {
145 for (k = start_band; k < stop_band; k++) {
146 ptr_sbr_dec->qmf_buf_real[l][k] = 0.0;
147 ptr_sbr_dec->qmf_buf_imag[l][k] = 0.0;
148 }
149 }
150 for (l = start_slot; l < op_delay; l++) {
151 for (k = start_band; k < stop_band; k++) {
152 ptr_sbr_dec->qmf_buf_real[l][k] = 0.0;
153 ptr_sbr_dec->qmf_buf_imag[l][k] = 0.0;
154 }
155 }
156 }
157 if (hbe_flag && ptr_sbr_dec->p_hbe_txposer != NULL) {
158 WORD32 k, i;
159 WORD32 err = ixheaacd_qmf_hbe_data_reinit(
160 ptr_sbr_dec->p_hbe_txposer,
161 ptr_header_data->pstr_freq_band_data->freq_band_table,
162 ptr_header_data->pstr_freq_band_data->num_sf_bands,
163 ptr_header_data->is_usf_4);
164 if (err) return err;
165
166 for (k = 0; k < 2; k++) {
167 if (!((upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) && (k == 0))) {
168 WORD32 xpos_delay = num_time_slots * k;
169 if (upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
170 xpos_delay = num_time_slots * k - 32;
171 }
172
173 for (i = 0; i < 8; i++) {
174 memmove(ptr_sbr_dec->ph_vocod_qmf_real[i],
175 ptr_sbr_dec->ph_vocod_qmf_real[num_time_slots + i],
176 64 * sizeof(FLOAT32));
177 memmove(ptr_sbr_dec->ph_vocod_qmf_imag[i],
178 ptr_sbr_dec->ph_vocod_qmf_imag[num_time_slots + i],
179 64 * sizeof(FLOAT32));
180 }
181
182 err = ixheaacd_qmf_hbe_apply(
183 ptr_sbr_dec->p_hbe_txposer,
184 ptr_sbr_dec->qmf_buf_real + op_delay + xpos_delay,
185 ptr_sbr_dec->qmf_buf_imag + op_delay + xpos_delay, num_time_slots,
186 ptr_sbr_dec->ph_vocod_qmf_real + op_delay,
187 ptr_sbr_dec->ph_vocod_qmf_imag + op_delay, pitch_in_bins);
188 if (err) return err;
189
190 if (upsample_ratio_idx == SBR_UPSAMPLE_IDX_4_1) {
191 ixheaacd_hbe_repl_spec(&ptr_sbr_dec->p_hbe_txposer->x_over_qmf[0],
192 ptr_sbr_dec->ph_vocod_qmf_real + op_delay,
193 ptr_sbr_dec->ph_vocod_qmf_imag + op_delay,
194 num_time_slots,
195 ptr_sbr_dec->p_hbe_txposer->max_stretch);
196 }
197 }
198 }
199 }
200 }
201
202 if (!usac_flag) {
203 err |= ixheaacd_reset_hf_generator(&ptr_sbr_dec->str_hf_generator,
204 ptr_header_data, audio_object_type);
205
206 ixheaacd_derive_lim_band_tbl(
207 ptr_header_data,
208 ptr_sbr_dec->str_hf_generator.pstr_settings->str_patch_param,
209 ptr_sbr_dec->str_hf_generator.pstr_settings->num_patches,
210 pstr_common_tables);
211 }
212
213 return err;
214 }
215
ixheaacd_prepare_upsamp(ia_sbr_header_data_struct ** ptr_header_data,ia_sbr_channel_struct * pstr_sbr_channel[2],WORD32 num_channels)216 VOID ixheaacd_prepare_upsamp(ia_sbr_header_data_struct **ptr_header_data,
217 ia_sbr_channel_struct *pstr_sbr_channel[2],
218 WORD32 num_channels) {
219 WORD32 lr;
220 ia_sbr_qmf_filter_bank_struct *sbr_qmf_bank;
221
222 for (lr = 0; lr < num_channels; lr++) {
223 ptr_header_data[lr]->pstr_freq_band_data->sub_band_start =
224 NO_ANALYSIS_CHANNELS;
225 ptr_header_data[lr]->pstr_freq_band_data->sub_band_end =
226 NO_SYNTHESIS_CHANNELS;
227
228 sbr_qmf_bank = &pstr_sbr_channel[lr]->str_sbr_dec.str_synthesis_qmf_bank;
229 sbr_qmf_bank->lsb = NO_ANALYSIS_CHANNELS;
230 sbr_qmf_bank->usb = NO_SYNTHESIS_CHANNELS;
231
232 sbr_qmf_bank = &pstr_sbr_channel[lr]->str_sbr_dec.str_codec_qmf_bank;
233 sbr_qmf_bank->lsb = 0;
234 sbr_qmf_bank->usb = NO_ANALYSIS_CHANNELS;
235 ptr_header_data[lr]->sync_state = UPSAMPLING;
236 }
237 return;
238 }
239
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)240 IA_ERRORCODE ixheaacd_applysbr(
241 ia_handle_sbr_dec_inst_struct self,
242 ia_aac_dec_sbr_bitstream_struct *p_sbr_bit_stream, WORD16 *core_sample_buf,
243 WORD16 *codec_num_channels, FLAG frame_status, FLAG down_samp_flag,
244 FLAG down_mix_flag, ia_sbr_scr_struct *sbr_scratch_struct, WORD32 ps_enable,
245 WORD32 ch_fac, WORD32 slot_element, ia_bit_buf_struct *it_bit_buff,
246 ia_drc_dec_struct *pstr_drc_dec, WORD eld_sbr_flag,
247 WORD32 audio_object_type) {
248 WORD32 k;
249 FLAG prev_ps_flag = 0;
250 FLAG ps_flag = 0;
251 FLAG stereo = 0;
252 FLAG low_pow_flag = 0;
253 FLAG header_flag = 1;
254 FLAG dual_mono = 0;
255 WORD32 err = 0;
256 WORD32 num_channels = *codec_num_channels;
257 FLAG prev_stereo;
258 WORD32 ele_channels = 0;
259 WORD32 num_elements = p_sbr_bit_stream->no_elements;
260 WORD32 usac_flag = self->aot_usac_flag;
261
262 ia_sbr_channel_struct *pstr_sbr_channel[2];
263 ia_sbr_header_data_struct *ptr_header_data[MAXNRSBRCHANNELS];
264
265 WORD32 initial_sync_state;
266
267 ia_sbr_header_data_struct *ptr_sbr_dflt_header =
268 (ia_sbr_header_data_struct *)(&self->str_sbr_dflt_header);
269
270 ia_sbr_frame_info_data_struct *ptr_frame_data[2];
271
272 for (k = 0; k < 2; k++) {
273 ptr_frame_data[k] = (ia_sbr_frame_info_data_struct *)self->frame_buffer[k];
274
275 pstr_sbr_channel[k] = self->pstr_sbr_channel[k];
276
277 ptr_header_data[k] = self->pstr_sbr_header[k];
278
279 if (audio_object_type == AOT_ER_AAC_ELD) {
280 ptr_frame_data[k]->eld_sbr_flag = eld_sbr_flag;
281 ptr_frame_data[k]->num_time_slots = ptr_header_data[0]->num_time_slots;
282 }
283
284 ptr_frame_data[k]->usac_independency_flag = self->usac_independency_flag;
285 ptr_frame_data[k]->mps_sbr_flag = (self->stereo_config_idx == 3) ? 1 : 0;
286 ptr_frame_data[k]->stereo_config_idx = self->stereo_config_idx;
287 ptr_frame_data[k]->inter_tes_flag = self->inter_tes_flag;
288 ptr_frame_data[k]->sbr_mode = self->sbr_mode;
289
290 if (!usac_flag) {
291 ptr_frame_data[k]->usac_independency_flag = 0;
292 ptr_frame_data[k]->mps_sbr_flag = 0;
293 ptr_frame_data[k]->stereo_config_idx = -1;
294 ptr_frame_data[k]->inter_tes_flag = 0;
295 ptr_frame_data[k]->sbr_mode = ORIG_SBR;
296 }
297 }
298
299 for (k = 0; k < *codec_num_channels; k++) {
300 ptr_header_data[k]->usac_flag = self->aot_usac_flag;
301
302 ptr_header_data[k]->usac_independency_flag = self->usac_independency_flag;
303 ptr_header_data[k]->hbe_flag = self->hbe_flag;
304 ptr_header_data[k]->pvc_flag = self->pvc_flag;
305
306 if (!usac_flag) {
307 ptr_header_data[k]->usac_independency_flag = 0;
308 ptr_header_data[k]->hbe_flag = 0;
309 ptr_header_data[k]->pvc_flag = 0;
310 }
311 }
312
313 initial_sync_state = ptr_header_data[0]->sync_state;
314
315 low_pow_flag = !usac_flag;
316 self->pstr_sbr_tables->sbr_rand_ph =
317 self->pstr_sbr_tables->env_calc_tables_ptr->sbr_rand_ph;
318
319 if (ps_enable) {
320 if (num_channels == 1) {
321 low_pow_flag = 0;
322 }
323 }
324
325 if (audio_object_type == AOT_ER_AAC_ELD) {
326 low_pow_flag = 0;
327 }
328
329 prev_stereo = (ptr_header_data[0]->channel_mode == SBR_STEREO);
330
331 if (ps_enable) prev_ps_flag = (ptr_header_data[0]->channel_mode == PS_STEREO);
332
333 ptr_header_data[0]->err_flag_prev = ptr_header_data[0]->err_flag;
334
335 if (p_sbr_bit_stream->no_elements == 0) {
336 frame_status = 0;
337 ptr_header_data[0]->sync_state = UPSAMPLING;
338 if (num_channels == 2) ptr_header_data[1]->sync_state = UPSAMPLING;
339 }
340
341 if ((usac_flag)) {
342 if ((p_sbr_bit_stream->no_elements) &&
343 (p_sbr_bit_stream->str_sbr_ele->size_payload > 0)) {
344 num_elements = p_sbr_bit_stream->no_elements;
345 } else {
346 num_elements = 0;
347 }
348 }
349
350 for (k = 0; k < num_elements; k++) {
351 struct ia_bit_buf_struct local_bit_buf = {0};
352 ia_sbr_element_stream_struct *ptr_bit_str_ele =
353 &p_sbr_bit_stream->str_sbr_ele[k];
354 ele_channels =
355 (p_sbr_bit_stream->str_sbr_ele[0].sbr_ele_id == SBR_ID_CPE) ? 2 : 1;
356
357 switch (ptr_bit_str_ele->sbr_ele_id) {
358 case SBR_ID_SCE:
359 case SBR_ID_CCE:
360 if (num_channels == 2) {
361 dual_mono = 1;
362 }
363 stereo = 0;
364 break;
365 case SBR_ID_CPE:
366 stereo = 1;
367 ptr_header_data[1] = ptr_header_data[0];
368
369 memcpy(self->pstr_sbr_header[1], self->pstr_sbr_header[0],
370 sizeof(ia_sbr_header_data_struct));
371 break;
372 default:
373 frame_status = 0;
374 }
375
376 if (frame_status) {
377 if (!usac_flag) {
378 ixheaacd_create_init_bit_buf(&local_bit_buf,
379 (UWORD8 *)ptr_bit_str_ele->ptr_sbr_data,
380 ptr_bit_str_ele->size_payload);
381
382 it_bit_buff = &local_bit_buf;
383 it_bit_buff->xaac_jmp_buf = self->xaac_jmp_buf;
384 if (audio_object_type == AOT_ER_AAC_ELD) {
385 if (eld_sbr_flag != 1) {
386 ixheaacd_read_bits_buf(&local_bit_buf, LEN_NIBBLE);
387 }
388 } else {
389 ixheaacd_read_bits_buf(&local_bit_buf, LEN_NIBBLE);
390 }
391 }
392 if (ptr_bit_str_ele->extension_type == SBR_EXTENSION_CRC) {
393 WORD32 crc_bits = 0;
394 WORD32 crc_check_flag = 0;
395 crc_check_flag = 1;
396 crc_bits = (((ptr_bit_str_ele->size_payload - 1) << 3) +
397 (4 - SBR_CYC_REDCY_CHK_BITS));
398
399 if (crc_bits < 0) {
400 crc_check_flag = 0;
401 frame_status = 0;
402 }
403 if (crc_check_flag)
404 frame_status = ixheaacd_sbr_crccheck(it_bit_buff, crc_bits);
405 }
406
407 if (!usac_flag) header_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
408
409 if (audio_object_type != AOT_ER_AAC_ELD) {
410 if (header_flag) {
411 header_flag = ixheaacd_sbr_read_header_data(
412 ptr_header_data[k], it_bit_buff, stereo, ptr_sbr_dflt_header);
413 if (usac_flag) {
414 if ((self->ptr_pvc_data_str->prev_pvc_mode == 0) &&
415 (ptr_header_data[k]->pvc_mode != 0)) {
416 self->ptr_pvc_data_str->prev_pvc_id = 0;
417 }
418 self->ptr_pvc_data_str->prev_pvc_mode =
419 ptr_header_data[k]->pvc_mode;
420 if (ptr_header_data[k]->pvc_mode == 0) {
421 ptr_frame_data[k]->sbr_mode = ORIG_SBR;
422 } else {
423 ptr_frame_data[k]->sbr_mode = PVC_SBR;
424 }
425 }
426 if (header_flag == SBR_RESET) {
427 err = ixheaacd_calc_frq_bnd_tbls(ptr_header_data[k],
428
429 self->pstr_common_tables);
430 if (!err) {
431 WORD32 lr;
432 WORD32 lr1 = ps_enable ? 2 : num_channels;
433 for (lr = 0; lr < lr1; lr++) {
434 ptr_frame_data[lr]->reset_flag = 1;
435
436 err |= ixheaacd_sbr_dec_reset(
437 &(pstr_sbr_channel[lr]->str_sbr_dec), ptr_header_data[k],
438 low_pow_flag, self->pstr_common_tables,
439 ptr_frame_data[k]->pitch_in_bins, audio_object_type);
440 if (err < 0) return err;
441 }
442 }
443
444 if (err == 0) {
445 ptr_header_data[k]->sync_state = SBR_ACTIVE;
446 }
447 }
448 }
449 } else {
450 if (header_flag) {
451 header_flag = ixheaacd_sbr_read_header_data(
452 ptr_header_data[k], it_bit_buff, stereo, ptr_sbr_dflt_header);
453 if (usac_flag) {
454 if ((self->ptr_pvc_data_str->prev_pvc_mode == 0) &&
455 (ptr_header_data[k]->pvc_mode != 0)) {
456 self->ptr_pvc_data_str->prev_pvc_id = 0;
457 }
458 self->ptr_pvc_data_str->prev_pvc_mode =
459 ptr_header_data[k]->pvc_mode;
460 if (ptr_header_data[k]->pvc_mode == 0) {
461 ptr_frame_data[k]->sbr_mode = ORIG_SBR;
462 } else {
463 ptr_frame_data[k]->sbr_mode = PVC_SBR;
464 }
465 }
466
467 if (header_flag == SBR_RESET) {
468 err = ixheaacd_calc_frq_bnd_tbls(ptr_header_data[k],
469
470 self->pstr_common_tables);
471 if (err) {
472 return err;
473 }
474 }
475 }
476
477 {
478 WORD32 lr;
479 WORD32 lr1 = ps_enable ? 2 : num_channels;
480 for (lr = 0; lr < lr1; lr++) {
481 ptr_frame_data[lr]->reset_flag = 1;
482 if (ptr_header_data[k]->status) {
483 err |= ixheaacd_sbr_dec_reset(
484 &(pstr_sbr_channel[lr]->str_sbr_dec), ptr_header_data[k],
485 low_pow_flag, self->pstr_common_tables,
486 ptr_frame_data[k]->pitch_in_bins, audio_object_type);
487 if (err < 0) return err;
488 }
489 }
490 ptr_header_data[k]->status = 0;
491 }
492
493 if (err == 0) {
494 ptr_header_data[k]->sync_state = SBR_ACTIVE;
495 }
496 }
497 }
498
499 if (err || (ptr_header_data[k]->sync_state == SBR_NOT_INITIALIZED)) {
500 WORD32 lr1 = ps_enable ? 2 : num_channels;
501 ixheaacd_prepare_upsamp(ptr_header_data, pstr_sbr_channel, lr1);
502 if (err) return err;
503 }
504
505 if (frame_status && (ptr_header_data[k]->sync_state == SBR_ACTIVE)) {
506 if (stereo) {
507 frame_status = ixheaacd_sbr_read_cpe(ptr_header_data[0], ptr_frame_data,
508 it_bit_buff, self->pstr_sbr_tables,
509 audio_object_type);
510 } else {
511 if (ps_enable) {
512 if (down_mix_flag) {
513 self->pstr_ps_stereo_dec->force_mono = 1;
514 } else {
515 self->pstr_ps_stereo_dec->force_mono = 0;
516 }
517 } else {
518 self->pstr_ps_stereo_dec = 0;
519 }
520 if (ptr_frame_data[k]->sbr_mode == ORIG_SBR) {
521 frame_status = ixheaacd_sbr_read_sce(
522 ptr_header_data[k], ptr_frame_data[k], self->pstr_ps_stereo_dec,
523 it_bit_buff, self->pstr_sbr_tables, audio_object_type);
524 if (frame_status < 0) return frame_status;
525 } else if (ptr_frame_data[k]->sbr_mode == PVC_SBR) {
526 frame_status = ixheaacd_sbr_read_pvc_sce(
527 ptr_frame_data[k], it_bit_buff, 0, self->ptr_pvc_data_str,
528 self->pstr_sbr_tables, ptr_header_data[k]);
529 if (frame_status < 0) return frame_status;
530 }
531 }
532 if (audio_object_type != AOT_ER_AAC_ELD) {
533 WORD32 total_bits_read;
534 total_bits_read = ixheaacd_no_bits_read(it_bit_buff);
535 if (total_bits_read > (ptr_bit_str_ele->size_payload << 3) ||
536 total_bits_read < ((ptr_bit_str_ele->size_payload << 3) - 8)) {
537 frame_status = 0;
538 }
539 }
540 }
541 }
542
543 if (!usac_flag) {
544 if (!frame_status || (ptr_header_data[0]->sync_state != SBR_ACTIVE) ||
545 ptr_header_data[0]->err_flag) {
546 ptr_header_data[0]->err_flag = 1;
547 stereo = (num_channels == 2) ? 1 : 0;
548 if (ptr_header_data[0]->channel_mode == 0) {
549 ptr_header_data[0]->channel_mode = stereo ? SBR_STEREO : SBR_MONO;
550 }
551 }
552
553 if (!(stereo || dual_mono)) {
554 ptr_frame_data[0]->coupling_mode = COUPLING_OFF;
555 ptr_frame_data[1]->coupling_mode = COUPLING_OFF;
556 }
557
558 if (ptr_header_data[0]->sync_state == SBR_NOT_INITIALIZED) {
559 WORD32 lr1 = ps_enable ? 2 : num_channels;
560 ixheaacd_prepare_upsamp(ptr_header_data, pstr_sbr_channel, lr1);
561 }
562 }
563
564 if (ptr_header_data[0]->sync_state == SBR_ACTIVE) {
565 if (ptr_frame_data[0]->sbr_mode == PVC_SBR) {
566 err = ixheaacd_dec_sbrdata_for_pvc(
567 ptr_header_data[0], ptr_frame_data[0],
568 pstr_sbr_channel[0]->pstr_prev_frame_data);
569 if (err) return err;
570 } else if (ptr_frame_data[0]->sbr_mode == ORIG_SBR) {
571 err = ixheaacd_dec_sbrdata(
572 ptr_header_data[0], ptr_header_data[1], ptr_frame_data[0],
573 pstr_sbr_channel[0]->pstr_prev_frame_data,
574 (stereo || dual_mono) ? ptr_frame_data[1] : NULL,
575 (stereo || dual_mono) ? pstr_sbr_channel[1]->pstr_prev_frame_data
576 : NULL,
577 self->pstr_common_tables);
578
579 if (err) return err;
580 }
581
582 if (ptr_header_data[0]->channel_mode == PS_STEREO &&
583 (audio_object_type != AOT_ER_AAC_ELD &&
584 audio_object_type != AOT_ER_AAC_LD)) {
585 ixheaacd_decode_ps_data(self->pstr_ps_stereo_dec);
586 ps_flag = 1;
587 self->ps_present = ps_flag;
588 }
589
590 ptr_frame_data[0]->max_qmf_subband_aac =
591 ptr_header_data[0]->pstr_freq_band_data->sub_band_start;
592 if (stereo) {
593 ptr_frame_data[1]->max_qmf_subband_aac =
594 ptr_header_data[1]->pstr_freq_band_data->sub_band_start;
595 }
596 }
597 if (audio_object_type != AOT_ER_AAC_ELD) {
598 if ((initial_sync_state == SBR_NOT_INITIALIZED) &&
599 ptr_header_data[0]->err_flag) {
600 ptr_header_data[0]->sync_state = SBR_NOT_INITIALIZED;
601 }
602 } else {
603 ptr_header_data[0]->sync_state = SBR_ACTIVE;
604 }
605
606 if ((num_channels == 2) && !(stereo || dual_mono)) {
607 ixheaacd_downmix_to_monosbr(&core_sample_buf[slot_element], ch_fac);
608 }
609
610 if ((!prev_stereo && !prev_ps_flag) && (ps_flag)) {
611 WORD32 copy_size;
612 if (down_samp_flag)
613 copy_size = QMF_FILTER_STATE_SYN_SIZE_DOWN_SAMPLED;
614 else
615 copy_size = QMF_FILTER_STATE_SYN_SIZE;
616
617 memcpy(
618 pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
619 pstr_sbr_channel[0]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
620 copy_size * sizeof(WORD16));
621
622 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale =
623 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale;
624 }
625
626 if ((!prev_stereo && stereo && (num_channels == 2)) &&
627 (audio_object_type != AOT_ER_AAC_ELD)) {
628 WORD32 copy_size;
629 if (down_samp_flag)
630 copy_size = QMF_FILTER_STATE_SYN_SIZE_DOWN_SAMPLED;
631 else
632 copy_size = QMF_FILTER_STATE_SYN_SIZE;
633
634 memcpy(
635 pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
636 pstr_sbr_channel[0]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
637 copy_size * sizeof(WORD16));
638
639 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale =
640 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale;
641
642 memcpy(
643 pstr_sbr_channel[1]->str_sbr_dec.str_codec_qmf_bank.anal_filter_states,
644 pstr_sbr_channel[0]->str_sbr_dec.str_codec_qmf_bank.anal_filter_states,
645 QMF_FILTER_STATE_ANA_SIZE * sizeof(WORD16));
646
647 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_lb_scale =
648 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_lb_scale;
649
650 memcpy(pstr_sbr_channel[1]->str_sbr_dec.ptr_sbr_overlap_buf,
651 pstr_sbr_channel[0]->str_sbr_dec.ptr_sbr_overlap_buf,
652 MAX_OV_COLS * NO_SYNTHESIS_CHANNELS * sizeof(WORD32));
653
654 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.ov_lb_scale =
655 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.ov_lb_scale;
656 pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.ov_hb_scale =
657 pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.ov_hb_scale;
658 }
659 pstr_sbr_channel[0]->str_sbr_dec.time_sample_buf = self->time_sample_buf[0];
660 if (pstr_drc_dec == NULL) {
661 WORD32 err_code = 0;
662 err_code = ixheaacd_sbr_dec(
663 &pstr_sbr_channel[0]->str_sbr_dec, core_sample_buf + slot_element,
664 ptr_header_data[0], ptr_frame_data[0],
665 pstr_sbr_channel[0]->pstr_prev_frame_data, self->pstr_ps_stereo_dec,
666 &pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank,
667 &pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact,
668 (ptr_header_data[0]->sync_state == SBR_ACTIVE), low_pow_flag,
669 sbr_scratch_struct->ptr_work_buf_core, self->pstr_sbr_tables,
670 self->pstr_common_tables, ch_fac, self->ptr_pvc_data_str, 0, NULL,
671 audio_object_type);
672 if (err_code) return err_code;
673 } else {
674 WORD32 err_code = 0;
675 err_code = ixheaacd_sbr_dec(
676 &pstr_sbr_channel[0]->str_sbr_dec, core_sample_buf + slot_element,
677 ptr_header_data[0], ptr_frame_data[0],
678 pstr_sbr_channel[0]->pstr_prev_frame_data, self->pstr_ps_stereo_dec,
679 &pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank,
680 &pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact,
681 (ptr_header_data[0]->sync_state == SBR_ACTIVE), low_pow_flag,
682 sbr_scratch_struct->ptr_work_buf_core, self->pstr_sbr_tables,
683 self->pstr_common_tables, ch_fac, self->ptr_pvc_data_str,
684 pstr_drc_dec->drc_on,
685 pstr_drc_dec->str_drc_channel_data[0].drc_factors_sbr,
686 audio_object_type);
687 if (err_code) return err_code;
688 }
689
690 if (!down_mix_flag && (stereo || dual_mono) && (num_channels == 2)) {
691 pstr_sbr_channel[1]->str_sbr_dec.time_sample_buf = self->time_sample_buf[1];
692
693 if (ele_channels == 1 && usac_flag) {
694 WORD32 err_code = ixheaacd_esbr_dec(
695 &pstr_sbr_channel[1]->str_sbr_dec, ptr_header_data[1],
696 ptr_frame_data[1], (ptr_header_data[1]->sync_state == SBR_ACTIVE),
697 low_pow_flag, self->pstr_sbr_tables, ch_fac);
698 if (err_code) return err_code;
699 } else {
700 if (pstr_drc_dec == NULL) {
701 WORD32 err_code = ixheaacd_sbr_dec(
702 &pstr_sbr_channel[1]->str_sbr_dec,
703 core_sample_buf + slot_element + 1, ptr_header_data[1],
704 ptr_frame_data[1], pstr_sbr_channel[1]->pstr_prev_frame_data, NULL,
705 NULL, NULL, (ptr_header_data[1]->sync_state == SBR_ACTIVE),
706 low_pow_flag, sbr_scratch_struct->ptr_work_buf_core,
707 self->pstr_sbr_tables, self->pstr_common_tables, ch_fac,
708 self->ptr_pvc_data_str, 0, NULL, audio_object_type);
709 if (err_code) return err_code;
710 } else {
711 WORD32 err_code = ixheaacd_sbr_dec(
712 &pstr_sbr_channel[1]->str_sbr_dec,
713 core_sample_buf + slot_element + 1, ptr_header_data[1],
714 ptr_frame_data[1], pstr_sbr_channel[1]->pstr_prev_frame_data, NULL,
715 NULL, NULL, (ptr_header_data[1]->sync_state == SBR_ACTIVE),
716 low_pow_flag, sbr_scratch_struct->ptr_work_buf_core,
717 self->pstr_sbr_tables, self->pstr_common_tables, ch_fac,
718 self->ptr_pvc_data_str, pstr_drc_dec->drc_on,
719 pstr_drc_dec->str_drc_channel_data[1].drc_factors_sbr,
720 audio_object_type);
721 if (err_code) return err_code;
722 }
723 }
724
725 } else {
726 if (audio_object_type != AOT_ER_AAC_ELD &&
727 audio_object_type != AOT_ER_AAC_LD)
728
729 {
730 if (sub_d((WORD16)ptr_header_data[0]->channel_mode, PS_STEREO) == 0) {
731 num_channels = 2;
732 }
733 }
734 }
735 *codec_num_channels = num_channels;
736 self->sbr_mode = ptr_frame_data[0]->sbr_mode;
737
738 if (pstr_drc_dec != NULL) {
739 WORD32 i, j;
740 for (i = 0; i < *codec_num_channels; i++) {
741 for (j = 0; j < 32; j++) {
742 memcpy(pstr_drc_dec->str_drc_channel_data[i].drc_factors_sbr[j],
743 pstr_drc_dec->str_drc_channel_data[i].drc_factors_sbr[j + 32],
744 64 * sizeof(WORD32));
745 }
746 }
747 }
748
749 return SBRDEC_OK;
750 }
751