• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *                                                                            *
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 #include <string.h>
21 #include "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     err |= 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 WORD32 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   WORD16 err = 0;
220   WORD32 lr;
221   ia_sbr_qmf_filter_bank_struct *sbr_qmf_bank;
222 
223   for (lr = 0; lr < num_channels; lr++) {
224     ptr_header_data[lr]->pstr_freq_band_data->sub_band_start =
225         NO_ANALYSIS_CHANNELS;
226     ptr_header_data[lr]->pstr_freq_band_data->sub_band_end =
227         NO_SYNTHESIS_CHANNELS;
228 
229     sbr_qmf_bank = &pstr_sbr_channel[lr]->str_sbr_dec.str_synthesis_qmf_bank;
230     sbr_qmf_bank->lsb = NO_ANALYSIS_CHANNELS;
231     sbr_qmf_bank->usb = NO_SYNTHESIS_CHANNELS;
232 
233     sbr_qmf_bank = &pstr_sbr_channel[lr]->str_sbr_dec.str_codec_qmf_bank;
234     sbr_qmf_bank->lsb = 0;
235     sbr_qmf_bank->usb = NO_ANALYSIS_CHANNELS;
236     ptr_header_data[lr]->sync_state = UPSAMPLING;
237   }
238   return err;
239 }
240 
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)241 IA_ERRORCODE ixheaacd_applysbr(
242     ia_handle_sbr_dec_inst_struct self,
243     ia_aac_dec_sbr_bitstream_struct *p_sbr_bit_stream, WORD16 *core_sample_buf,
244     WORD16 *codec_num_channels, FLAG frame_status, FLAG down_samp_flag,
245     FLAG down_mix_flag, ia_sbr_scr_struct *sbr_scratch_struct, WORD32 ps_enable,
246     WORD32 ch_fac, WORD32 slot_element, ia_bit_buf_struct *it_bit_buff,
247     ia_drc_dec_struct *pstr_drc_dec, WORD eld_sbr_flag,
248     WORD32 audio_object_type) {
249   WORD32 k;
250   FLAG prev_ps_flag = 0;
251   FLAG ps_flag = 0;
252   FLAG stereo = 0;
253   FLAG low_pow_flag = 0;
254   FLAG header_flag = 1;
255   FLAG dual_mono = 0;
256   WORD32 err = 0;
257   WORD32 num_channels = *codec_num_channels;
258   FLAG prev_stereo;
259   WORD32 ele_channels = 0;
260   WORD32 num_elements = p_sbr_bit_stream->no_elements;
261   WORD32 usac_flag = self->aot_usac_flag;
262 
263   ia_sbr_channel_struct *pstr_sbr_channel[2];
264   ia_sbr_header_data_struct *ptr_header_data[MAXNRSBRCHANNELS];
265 
266   WORD32 initial_sync_state;
267 
268   ia_sbr_header_data_struct *ptr_sbr_dflt_header =
269       (ia_sbr_header_data_struct *)(&self->str_sbr_dflt_header);
270 
271   ia_sbr_frame_info_data_struct *ptr_frame_data[2];
272 
273   for (k = 0; k < 2; k++) {
274     ptr_frame_data[k] = (ia_sbr_frame_info_data_struct *)self->frame_buffer[k];
275 
276     pstr_sbr_channel[k] = self->pstr_sbr_channel[k];
277 
278     ptr_header_data[k] = self->pstr_sbr_header[k];
279 
280     if (audio_object_type == AOT_ER_AAC_ELD) {
281       ptr_frame_data[k]->eld_sbr_flag = eld_sbr_flag;
282       ptr_frame_data[k]->num_time_slots = ptr_header_data[0]->num_time_slots;
283     }
284 
285     ptr_frame_data[k]->usac_independency_flag = self->usac_independency_flag;
286     ptr_frame_data[k]->mps_sbr_flag = (self->stereo_config_idx == 3) ? 1 : 0;
287     ptr_frame_data[k]->stereo_config_idx = self->stereo_config_idx;
288     ptr_frame_data[k]->inter_tes_flag = self->inter_tes_flag;
289     ptr_frame_data[k]->sbr_mode = self->sbr_mode;
290 
291     if (!usac_flag) {
292       ptr_frame_data[k]->usac_independency_flag = 0;
293       ptr_frame_data[k]->mps_sbr_flag = 0;
294       ptr_frame_data[k]->stereo_config_idx = -1;
295       ptr_frame_data[k]->inter_tes_flag = 0;
296       ptr_frame_data[k]->sbr_mode = ORIG_SBR;
297     }
298   }
299 
300   for (k = 0; k < *codec_num_channels; k++) {
301     ptr_header_data[k]->usac_flag = self->aot_usac_flag;
302 
303     ptr_header_data[k]->usac_independency_flag = self->usac_independency_flag;
304     ptr_header_data[k]->hbe_flag = self->hbe_flag;
305     ptr_header_data[k]->pvc_flag = self->pvc_flag;
306 
307     if (!usac_flag) {
308       ptr_header_data[k]->usac_independency_flag = 0;
309       ptr_header_data[k]->hbe_flag = 0;
310       ptr_header_data[k]->pvc_flag = 0;
311     }
312   }
313 
314   initial_sync_state = ptr_header_data[0]->sync_state;
315 
316   low_pow_flag = !usac_flag;
317   self->pstr_sbr_tables->sbr_rand_ph =
318       self->pstr_sbr_tables->env_calc_tables_ptr->sbr_rand_ph;
319 
320   if (ps_enable) {
321     if (num_channels == 1) {
322       low_pow_flag = 0;
323     }
324   }
325 
326   if (audio_object_type == AOT_ER_AAC_ELD) {
327     low_pow_flag = 0;
328   }
329 
330   prev_stereo = (ptr_header_data[0]->channel_mode == SBR_STEREO);
331 
332   if (ps_enable) prev_ps_flag = (ptr_header_data[0]->channel_mode == PS_STEREO);
333 
334   ptr_header_data[0]->err_flag_prev = ptr_header_data[0]->err_flag;
335 
336   if (p_sbr_bit_stream->no_elements == 0) {
337     frame_status = 0;
338     ptr_header_data[0]->sync_state = UPSAMPLING;
339     if (num_channels == 2) ptr_header_data[1]->sync_state = UPSAMPLING;
340   }
341 
342   if ((usac_flag)) {
343     if ((p_sbr_bit_stream->no_elements) &&
344         (p_sbr_bit_stream->str_sbr_ele->size_payload > 0)) {
345       num_elements = p_sbr_bit_stream->no_elements;
346     } else {
347       num_elements = 0;
348     }
349   }
350 
351   for (k = 0; k < num_elements; k++) {
352     struct ia_bit_buf_struct local_bit_buf = {0};
353     ia_sbr_element_stream_struct *ptr_bit_str_ele =
354         &p_sbr_bit_stream->str_sbr_ele[k];
355     ele_channels =
356         (p_sbr_bit_stream->str_sbr_ele[0].sbr_ele_id == SBR_ID_CPE) ? 2 : 1;
357 
358     switch (ptr_bit_str_ele->sbr_ele_id) {
359       case SBR_ID_SCE:
360       case SBR_ID_CCE:
361         if (num_channels == 2) {
362           dual_mono = 1;
363         }
364         stereo = 0;
365         break;
366       case SBR_ID_CPE:
367         stereo = 1;
368         ptr_header_data[1] = ptr_header_data[0];
369 
370         memcpy(self->pstr_sbr_header[1], self->pstr_sbr_header[0],
371                sizeof(ia_sbr_header_data_struct));
372         break;
373       default:
374         frame_status = 0;
375     }
376 
377     if (frame_status) {
378       if (!usac_flag) {
379         ixheaacd_create_init_bit_buf(&local_bit_buf,
380                                      (UWORD8 *)ptr_bit_str_ele->ptr_sbr_data,
381                                      ptr_bit_str_ele->size_payload);
382 
383         it_bit_buff = &local_bit_buf;
384         it_bit_buff->xaac_jmp_buf = self->xaac_jmp_buf;
385         if (audio_object_type == AOT_ER_AAC_ELD) {
386           if (eld_sbr_flag != 1) {
387             ixheaacd_read_bits_buf(&local_bit_buf, LEN_NIBBLE);
388           }
389         } else {
390           ixheaacd_read_bits_buf(&local_bit_buf, LEN_NIBBLE);
391         }
392       }
393       if (ptr_bit_str_ele->extension_type == SBR_EXTENSION_CRC) {
394         WORD32 crc_bits = 0;
395         WORD32 crc_check_flag = 0;
396         crc_check_flag = 1;
397         crc_bits = (((ptr_bit_str_ele->size_payload - 1) << 3) +
398                     (4 - SBR_CYC_REDCY_CHK_BITS));
399 
400         if (crc_bits < 0) {
401           crc_check_flag = 0;
402           frame_status = 0;
403         }
404         if (crc_check_flag)
405           frame_status = ixheaacd_sbr_crccheck(it_bit_buff, crc_bits);
406       }
407 
408       if (!usac_flag) header_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
409 
410       if (audio_object_type != AOT_ER_AAC_ELD) {
411         if (header_flag) {
412           header_flag = ixheaacd_sbr_read_header_data(
413               ptr_header_data[k], it_bit_buff, stereo, ptr_sbr_dflt_header);
414           if (usac_flag) {
415             if ((self->ptr_pvc_data_str->prev_pvc_mode == 0) &&
416                 (ptr_header_data[k]->pvc_mode != 0)) {
417               self->ptr_pvc_data_str->prev_pvc_id = 0;
418             }
419             self->ptr_pvc_data_str->prev_pvc_mode =
420                 ptr_header_data[k]->pvc_mode;
421             if (ptr_header_data[k]->pvc_mode == 0) {
422               ptr_frame_data[k]->sbr_mode = ORIG_SBR;
423             } else {
424               ptr_frame_data[k]->sbr_mode = PVC_SBR;
425             }
426           }
427           if (header_flag == SBR_RESET) {
428             err = ixheaacd_calc_frq_bnd_tbls(ptr_header_data[k],
429 
430                                              self->pstr_common_tables);
431             if (!err) {
432               WORD32 lr;
433               WORD32 lr1 = ps_enable ? 2 : num_channels;
434               for (lr = 0; lr < lr1; lr++) {
435                 ptr_frame_data[lr]->reset_flag = 1;
436 
437                 err |= ixheaacd_sbr_dec_reset(
438                     &(pstr_sbr_channel[lr]->str_sbr_dec), ptr_header_data[k],
439                     low_pow_flag, self->pstr_common_tables,
440                     ptr_frame_data[k]->pitch_in_bins, audio_object_type);
441                 if (err < 0) return err;
442               }
443             }
444 
445             if (err == 0) {
446               ptr_header_data[k]->sync_state = SBR_ACTIVE;
447             }
448           }
449         }
450       } else {
451         if (header_flag) {
452           header_flag = ixheaacd_sbr_read_header_data(
453               ptr_header_data[k], it_bit_buff, stereo, ptr_sbr_dflt_header);
454           if (usac_flag) {
455             if ((self->ptr_pvc_data_str->prev_pvc_mode == 0) &&
456                 (ptr_header_data[k]->pvc_mode != 0)) {
457               self->ptr_pvc_data_str->prev_pvc_id = 0;
458             }
459             self->ptr_pvc_data_str->prev_pvc_mode =
460                 ptr_header_data[k]->pvc_mode;
461             if (ptr_header_data[k]->pvc_mode == 0) {
462               ptr_frame_data[k]->sbr_mode = ORIG_SBR;
463             } else {
464               ptr_frame_data[k]->sbr_mode = PVC_SBR;
465             }
466           }
467 
468           if (header_flag == SBR_RESET) {
469             err = ixheaacd_calc_frq_bnd_tbls(ptr_header_data[k],
470 
471                                              self->pstr_common_tables);
472             if (err) {
473               return err;
474             }
475           }
476         }
477 
478         {
479           WORD32 lr;
480           WORD32 lr1 = ps_enable ? 2 : num_channels;
481           for (lr = 0; lr < lr1; lr++) {
482             ptr_frame_data[lr]->reset_flag = 1;
483             if (ptr_header_data[k]->status) {
484               err |= ixheaacd_sbr_dec_reset(
485                   &(pstr_sbr_channel[lr]->str_sbr_dec), ptr_header_data[k],
486                   low_pow_flag, self->pstr_common_tables,
487                   ptr_frame_data[k]->pitch_in_bins, audio_object_type);
488               if (err < 0) return err;
489             }
490           }
491           ptr_header_data[k]->status = 0;
492         }
493 
494         if (err == 0) {
495           ptr_header_data[k]->sync_state = SBR_ACTIVE;
496         }
497       }
498     }
499 
500     if (err || (ptr_header_data[k]->sync_state == SBR_NOT_INITIALIZED)) {
501       WORD32 lr1 = ps_enable ? 2 : num_channels;
502       ixheaacd_prepare_upsamp(ptr_header_data, pstr_sbr_channel, lr1);
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         } else if (ptr_frame_data[k]->sbr_mode == PVC_SBR) {
525           frame_status = ixheaacd_sbr_read_pvc_sce(
526               ptr_frame_data[k], it_bit_buff, 0, self->ptr_pvc_data_str,
527               self->pstr_sbr_tables, ptr_header_data[k]);
528           if (frame_status < 0) return frame_status;
529         }
530       }
531       if (audio_object_type != AOT_ER_AAC_ELD) {
532         WORD32 total_bits_read;
533         total_bits_read = ixheaacd_no_bits_read(it_bit_buff);
534         if (total_bits_read > (ptr_bit_str_ele->size_payload << 3) ||
535             total_bits_read < ((ptr_bit_str_ele->size_payload << 3) - 8)) {
536           frame_status = 0;
537         }
538       }
539     }
540   }
541 
542   if (!usac_flag) {
543     if (!frame_status || (ptr_header_data[0]->sync_state != SBR_ACTIVE) ||
544         ptr_header_data[0]->err_flag) {
545       ptr_header_data[0]->err_flag = 1;
546       stereo = (num_channels == 2) ? 1 : 0;
547       if (ptr_header_data[0]->channel_mode == 0) {
548         ptr_header_data[0]->channel_mode = stereo ? SBR_STEREO : SBR_MONO;
549       }
550     }
551 
552     if (!(stereo || dual_mono)) {
553       ptr_frame_data[0]->coupling_mode = COUPLING_OFF;
554       ptr_frame_data[1]->coupling_mode = COUPLING_OFF;
555     }
556 
557     if (ptr_header_data[0]->sync_state == SBR_NOT_INITIALIZED) {
558       WORD32 lr1 = ps_enable ? 2 : num_channels;
559       ixheaacd_prepare_upsamp(ptr_header_data, pstr_sbr_channel, lr1);
560     }
561   }
562 
563   if (ptr_header_data[0]->sync_state == SBR_ACTIVE) {
564     if (ptr_frame_data[0]->sbr_mode == PVC_SBR) {
565       err = ixheaacd_dec_sbrdata_for_pvc(
566           ptr_header_data[0], ptr_frame_data[0],
567           pstr_sbr_channel[0]->pstr_prev_frame_data);
568       if (err) return err;
569     } else if (ptr_frame_data[0]->sbr_mode == ORIG_SBR) {
570       err = ixheaacd_dec_sbrdata(
571           ptr_header_data[0], ptr_header_data[1], ptr_frame_data[0],
572           pstr_sbr_channel[0]->pstr_prev_frame_data,
573           (stereo || dual_mono) ? ptr_frame_data[1] : NULL,
574           (stereo || dual_mono) ? pstr_sbr_channel[1]->pstr_prev_frame_data
575                                 : NULL,
576           self->pstr_common_tables);
577 
578       if (err) return err;
579     }
580 
581     if (ptr_header_data[0]->channel_mode == PS_STEREO &&
582         (audio_object_type != AOT_ER_AAC_ELD &&
583          audio_object_type != AOT_ER_AAC_LD)) {
584       ixheaacd_decode_ps_data(self->pstr_ps_stereo_dec);
585       ps_flag = 1;
586       self->ps_present = ps_flag;
587     }
588 
589     ptr_frame_data[0]->max_qmf_subband_aac =
590         ptr_header_data[0]->pstr_freq_band_data->sub_band_start;
591     if (stereo) {
592       ptr_frame_data[1]->max_qmf_subband_aac =
593           ptr_header_data[1]->pstr_freq_band_data->sub_band_start;
594     }
595   }
596   if (audio_object_type != AOT_ER_AAC_ELD) {
597     if ((initial_sync_state == SBR_NOT_INITIALIZED) &&
598         ptr_header_data[0]->err_flag) {
599       ptr_header_data[0]->sync_state = SBR_NOT_INITIALIZED;
600     }
601   } else {
602     ptr_header_data[0]->sync_state = SBR_ACTIVE;
603   }
604 
605   if ((num_channels == 2) && !(stereo || dual_mono)) {
606     ixheaacd_downmix_to_monosbr(&core_sample_buf[slot_element], ch_fac);
607   }
608 
609   if ((!prev_stereo && !prev_ps_flag) && (ps_flag)) {
610     WORD32 copy_size;
611     if (down_samp_flag)
612       copy_size = QMF_FILTER_STATE_SYN_SIZE_DOWN_SAMPLED;
613     else
614       copy_size = QMF_FILTER_STATE_SYN_SIZE;
615 
616     memcpy(
617         pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
618         pstr_sbr_channel[0]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
619         copy_size * sizeof(WORD16));
620 
621     pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale =
622         pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale;
623   }
624 
625   if ((!prev_stereo && stereo && (num_channels == 2)) &&
626       (audio_object_type != AOT_ER_AAC_ELD)) {
627     WORD32 copy_size;
628     if (down_samp_flag)
629       copy_size = QMF_FILTER_STATE_SYN_SIZE_DOWN_SAMPLED;
630     else
631       copy_size = QMF_FILTER_STATE_SYN_SIZE;
632 
633     memcpy(
634         pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
635         pstr_sbr_channel[0]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
636         copy_size * sizeof(WORD16));
637 
638     pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale =
639         pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale;
640 
641     memcpy(
642         pstr_sbr_channel[1]->str_sbr_dec.str_codec_qmf_bank.anal_filter_states,
643         pstr_sbr_channel[0]->str_sbr_dec.str_codec_qmf_bank.anal_filter_states,
644         QMF_FILTER_STATE_ANA_SIZE * sizeof(WORD16));
645 
646     pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_lb_scale =
647         pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_lb_scale;
648 
649     memcpy(pstr_sbr_channel[1]->str_sbr_dec.ptr_sbr_overlap_buf,
650            pstr_sbr_channel[0]->str_sbr_dec.ptr_sbr_overlap_buf,
651            MAX_OV_COLS * NO_SYNTHESIS_CHANNELS * sizeof(WORD32));
652 
653     pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.ov_lb_scale =
654         pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.ov_lb_scale;
655     pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.ov_hb_scale =
656         pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.ov_hb_scale;
657   }
658   pstr_sbr_channel[0]->str_sbr_dec.time_sample_buf = self->time_sample_buf[0];
659   if (pstr_drc_dec == NULL) {
660     WORD32 err_code = 0;
661     err_code = ixheaacd_sbr_dec(
662         &pstr_sbr_channel[0]->str_sbr_dec, core_sample_buf + slot_element,
663         ptr_header_data[0], ptr_frame_data[0],
664         pstr_sbr_channel[0]->pstr_prev_frame_data, self->pstr_ps_stereo_dec,
665         &pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank,
666         &pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact,
667         (ptr_header_data[0]->sync_state == SBR_ACTIVE), low_pow_flag,
668         sbr_scratch_struct->ptr_work_buf_core, self->pstr_sbr_tables,
669         self->pstr_common_tables, ch_fac, self->ptr_pvc_data_str, 0, NULL,
670         audio_object_type);
671     if (err_code) return err_code;
672   } else {
673     WORD32 err_code = 0;
674     err_code = ixheaacd_sbr_dec(
675         &pstr_sbr_channel[0]->str_sbr_dec, core_sample_buf + slot_element,
676         ptr_header_data[0], ptr_frame_data[0],
677         pstr_sbr_channel[0]->pstr_prev_frame_data, self->pstr_ps_stereo_dec,
678         &pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank,
679         &pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact,
680         (ptr_header_data[0]->sync_state == SBR_ACTIVE), low_pow_flag,
681         sbr_scratch_struct->ptr_work_buf_core, self->pstr_sbr_tables,
682         self->pstr_common_tables, ch_fac, self->ptr_pvc_data_str,
683         pstr_drc_dec->drc_on,
684         pstr_drc_dec->str_drc_channel_data[0].drc_factors_sbr,
685         audio_object_type);
686     if (err_code) return err_code;
687   }
688 
689   if (!down_mix_flag && (stereo || dual_mono) && (num_channels == 2)) {
690     pstr_sbr_channel[1]->str_sbr_dec.time_sample_buf = self->time_sample_buf[1];
691 
692     if (ele_channels == 1 && usac_flag) {
693       WORD32 err_code = ixheaacd_esbr_dec(
694           &pstr_sbr_channel[1]->str_sbr_dec, ptr_header_data[1],
695           ptr_frame_data[1], (ptr_header_data[1]->sync_state == SBR_ACTIVE),
696           low_pow_flag, self->pstr_sbr_tables, ch_fac);
697       if (err_code) return err_code;
698     } else {
699       if (pstr_drc_dec == NULL) {
700         WORD32 err_code = ixheaacd_sbr_dec(
701             &pstr_sbr_channel[1]->str_sbr_dec,
702             core_sample_buf + slot_element + 1, ptr_header_data[1],
703             ptr_frame_data[1], pstr_sbr_channel[1]->pstr_prev_frame_data, NULL,
704             NULL, NULL, (ptr_header_data[1]->sync_state == SBR_ACTIVE),
705             low_pow_flag, sbr_scratch_struct->ptr_work_buf_core,
706             self->pstr_sbr_tables, self->pstr_common_tables, ch_fac,
707             self->ptr_pvc_data_str, 0, NULL, audio_object_type);
708         if (err_code) return err_code;
709       } else {
710         WORD32 err_code = ixheaacd_sbr_dec(
711             &pstr_sbr_channel[1]->str_sbr_dec,
712             core_sample_buf + slot_element + 1, ptr_header_data[1],
713             ptr_frame_data[1], pstr_sbr_channel[1]->pstr_prev_frame_data, NULL,
714             NULL, NULL, (ptr_header_data[1]->sync_state == SBR_ACTIVE),
715             low_pow_flag, sbr_scratch_struct->ptr_work_buf_core,
716             self->pstr_sbr_tables, self->pstr_common_tables, ch_fac,
717             self->ptr_pvc_data_str, pstr_drc_dec->drc_on,
718             pstr_drc_dec->str_drc_channel_data[1].drc_factors_sbr,
719             audio_object_type);
720         if (err_code) return err_code;
721       }
722     }
723 
724   } else {
725     if (audio_object_type != AOT_ER_AAC_ELD &&
726         audio_object_type != AOT_ER_AAC_LD)
727 
728     {
729       if (sub_d((WORD16)ptr_header_data[0]->channel_mode, PS_STEREO) == 0) {
730         num_channels = 2;
731       }
732     }
733   }
734   *codec_num_channels = num_channels;
735   self->sbr_mode = ptr_frame_data[0]->sbr_mode;
736 
737   if (pstr_drc_dec != NULL) {
738     WORD32 i, j;
739     for (i = 0; i < *codec_num_channels; i++) {
740       for (j = 0; j < 32; j++) {
741         memcpy(pstr_drc_dec->str_drc_channel_data[i].drc_factors_sbr[j],
742                pstr_drc_dec->str_drc_channel_data[i].drc_factors_sbr[j + 32],
743                64 * sizeof(WORD32));
744       }
745     }
746   }
747 
748   return SBRDEC_OK;
749 }
750