• 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 WORD16 ixheaacd_applysbr(ia_handle_sbr_dec_inst_struct self,
242                          ia_aac_dec_sbr_bitstream_struct *p_sbr_bit_stream,
243                          WORD16 *core_sample_buf, WORD16 *codec_num_channels,
244                          FLAG frame_status, FLAG down_samp_flag,
245                          FLAG down_mix_flag,
246                          ia_sbr_scr_struct *sbr_scratch_struct,
247                          WORD32 ps_enable, WORD32 ch_fac, WORD32 slot_element,
248                          ia_bit_buf_struct *it_bit_buff,
249                          ia_drc_dec_struct *pstr_drc_dec, WORD eld_sbr_flag,
250                          WORD32 audio_object_type) {
251   WORD32 k;
252   FLAG prev_ps_flag = 0;
253   FLAG ps_flag = 0;
254   FLAG stereo = 0;
255   FLAG low_pow_flag = 0;
256   FLAG header_flag = 1;
257   FLAG dual_mono = 0;
258   WORD32 err = 0;
259   WORD32 num_channels = *codec_num_channels;
260   FLAG prev_stereo;
261   WORD32 ele_channels = 0;
262   WORD32 num_elements = p_sbr_bit_stream->no_elements;
263   WORD32 usac_flag = self->aot_usac_flag;
264 
265   ia_sbr_channel_struct *pstr_sbr_channel[2];
266   ia_sbr_header_data_struct *ptr_header_data[MAXNRSBRCHANNELS];
267 
268   WORD32 initial_sync_state;
269 
270   ia_sbr_header_data_struct *ptr_sbr_dflt_header =
271       (ia_sbr_header_data_struct *)(&self->str_sbr_dflt_header);
272 
273   ia_sbr_frame_info_data_struct *ptr_frame_data[2];
274 
275   for (k = 0; k < 2; k++) {
276     ptr_frame_data[k] = (ia_sbr_frame_info_data_struct *)self->frame_buffer[k];
277 
278     pstr_sbr_channel[k] = self->pstr_sbr_channel[k];
279 
280     ptr_header_data[k] = self->pstr_sbr_header[k];
281 
282     if (audio_object_type == AOT_ER_AAC_ELD) {
283       ptr_frame_data[k]->eld_sbr_flag = eld_sbr_flag;
284       ptr_frame_data[k]->num_time_slots = ptr_header_data[0]->num_time_slots;
285     }
286 
287     ptr_frame_data[k]->usac_independency_flag = self->usac_independency_flag;
288     ptr_frame_data[k]->mps_sbr_flag = (self->stereo_config_idx == 3) ? 1 : 0;
289     ptr_frame_data[k]->stereo_config_idx = self->stereo_config_idx;
290     ptr_frame_data[k]->inter_tes_flag = self->inter_tes_flag;
291     ptr_frame_data[k]->sbr_mode = self->sbr_mode;
292 
293     if (!usac_flag) {
294       ptr_frame_data[k]->usac_independency_flag = 0;
295       ptr_frame_data[k]->mps_sbr_flag = 0;
296       ptr_frame_data[k]->stereo_config_idx = -1;
297       ptr_frame_data[k]->inter_tes_flag = 0;
298       ptr_frame_data[k]->sbr_mode = ORIG_SBR;
299     }
300   }
301 
302   for (k = 0; k < *codec_num_channels; k++) {
303     ptr_header_data[k]->usac_flag = self->aot_usac_flag;
304 
305     ptr_header_data[k]->usac_independency_flag = self->usac_independency_flag;
306     ptr_header_data[k]->hbe_flag = self->hbe_flag;
307     ptr_header_data[k]->pvc_flag = self->pvc_flag;
308 
309     if (!usac_flag) {
310       ptr_header_data[k]->usac_independency_flag = 0;
311       ptr_header_data[k]->hbe_flag = 0;
312       ptr_header_data[k]->pvc_flag = 0;
313     }
314   }
315 
316   initial_sync_state = ptr_header_data[0]->sync_state;
317 
318   low_pow_flag = !usac_flag;
319   self->pstr_sbr_tables->sbr_rand_ph =
320       self->pstr_sbr_tables->env_calc_tables_ptr->sbr_rand_ph;
321 
322   if (ps_enable) {
323     if (num_channels == 1) {
324       low_pow_flag = 0;
325     }
326   }
327 
328   if (audio_object_type == AOT_ER_AAC_ELD) {
329     low_pow_flag = 0;
330   }
331 
332   prev_stereo = (ptr_header_data[0]->channel_mode == SBR_STEREO);
333 
334   if (ps_enable) prev_ps_flag = (ptr_header_data[0]->channel_mode == PS_STEREO);
335 
336   ptr_header_data[0]->err_flag_prev = ptr_header_data[0]->err_flag;
337 
338   if (p_sbr_bit_stream->no_elements == 0) {
339     frame_status = 0;
340     ptr_header_data[0]->sync_state = UPSAMPLING;
341     if (num_channels == 2) ptr_header_data[1]->sync_state = UPSAMPLING;
342   }
343 
344   if ((usac_flag)) {
345     if ((p_sbr_bit_stream->no_elements) &&
346         (p_sbr_bit_stream->str_sbr_ele->size_payload > 0)) {
347       num_elements = p_sbr_bit_stream->no_elements;
348     } else {
349       num_elements = 0;
350     }
351   }
352 
353   for (k = 0; k < num_elements; k++) {
354     struct ia_bit_buf_struct local_bit_buf;
355     ia_sbr_element_stream_struct *ptr_bit_str_ele =
356         &p_sbr_bit_stream->str_sbr_ele[k];
357     ele_channels =
358         (p_sbr_bit_stream->str_sbr_ele[0].sbr_ele_id == SBR_ID_CPE) ? 2 : 1;
359 
360     switch (ptr_bit_str_ele->sbr_ele_id) {
361       case SBR_ID_SCE:
362       case SBR_ID_CCE:
363         if (num_channels == 2) {
364           dual_mono = 1;
365         }
366         stereo = 0;
367         break;
368       case SBR_ID_CPE:
369         stereo = 1;
370         ptr_header_data[1] = ptr_header_data[0];
371 
372         memcpy(self->pstr_sbr_header[1], self->pstr_sbr_header[0],
373                sizeof(ia_sbr_header_data_struct));
374         break;
375       default:
376         frame_status = 0;
377     }
378 
379     if (frame_status) {
380       if (!usac_flag) {
381         ixheaacd_create_init_bit_buf(&local_bit_buf,
382                                      (UWORD8 *)ptr_bit_str_ele->ptr_sbr_data,
383                                      ptr_bit_str_ele->size_payload);
384 
385         it_bit_buff = &local_bit_buf;
386         if (audio_object_type == AOT_ER_AAC_ELD) {
387           if (eld_sbr_flag != 1) {
388             ixheaacd_read_bits_buf(&local_bit_buf, LEN_NIBBLE);
389           }
390         } else {
391           ixheaacd_read_bits_buf(&local_bit_buf, LEN_NIBBLE);
392         }
393       }
394       if (ptr_bit_str_ele->extension_type == SBR_EXTENSION_CRC) {
395         WORD32 crc_bits = 0;
396         WORD32 crc_check_flag = 0;
397         crc_check_flag = 1;
398         crc_bits = (((ptr_bit_str_ele->size_payload - 1) << 3) +
399                     (4 - SBR_CYC_REDCY_CHK_BITS));
400 
401         if (crc_bits < 0) {
402           crc_check_flag = 0;
403           frame_status = 0;
404         }
405         if (crc_check_flag)
406           frame_status = ixheaacd_sbr_crccheck(it_bit_buff, crc_bits);
407       }
408 
409       if (!usac_flag) header_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
410 
411       if (audio_object_type != AOT_ER_AAC_ELD) {
412         if (header_flag) {
413           header_flag = ixheaacd_sbr_read_header_data(
414               ptr_header_data[k], it_bit_buff, stereo, ptr_sbr_dflt_header);
415           if (usac_flag) {
416             if ((self->ptr_pvc_data_str->prev_pvc_mode == 0) &&
417                 (ptr_header_data[k]->pvc_mode != 0)) {
418               self->ptr_pvc_data_str->prev_pvc_id = 0;
419             }
420             self->ptr_pvc_data_str->prev_pvc_mode =
421                 ptr_header_data[k]->pvc_mode;
422             if (ptr_header_data[k]->pvc_mode == 0) {
423               ptr_frame_data[k]->sbr_mode = ORIG_SBR;
424             } else {
425               ptr_frame_data[k]->sbr_mode = PVC_SBR;
426             }
427           }
428           if (header_flag == SBR_RESET) {
429             err = ixheaacd_calc_frq_bnd_tbls(ptr_header_data[k],
430 
431                                              self->pstr_common_tables);
432             if (!err) {
433               WORD32 lr;
434               WORD32 lr1 = ps_enable ? 2 : num_channels;
435               for (lr = 0; lr < lr1; lr++) {
436                 ptr_frame_data[lr]->reset_flag = 1;
437 
438                 err |= ixheaacd_sbr_dec_reset(
439                     &(pstr_sbr_channel[lr]->str_sbr_dec), ptr_header_data[k],
440                     low_pow_flag, self->pstr_common_tables,
441                     ptr_frame_data[k]->pitch_in_bins, audio_object_type);
442                 if (err < 0) return err;
443               }
444             }
445 
446             if (err == 0) {
447               ptr_header_data[k]->sync_state = SBR_ACTIVE;
448             }
449           }
450         }
451       } else {
452         if (header_flag) {
453           header_flag = ixheaacd_sbr_read_header_data(
454               ptr_header_data[k], it_bit_buff, stereo, ptr_sbr_dflt_header);
455           if (usac_flag) {
456             if ((self->ptr_pvc_data_str->prev_pvc_mode == 0) &&
457                 (ptr_header_data[k]->pvc_mode != 0)) {
458               self->ptr_pvc_data_str->prev_pvc_id = 0;
459             }
460             self->ptr_pvc_data_str->prev_pvc_mode =
461                 ptr_header_data[k]->pvc_mode;
462             if (ptr_header_data[k]->pvc_mode == 0) {
463               ptr_frame_data[k]->sbr_mode = ORIG_SBR;
464             } else {
465               ptr_frame_data[k]->sbr_mode = PVC_SBR;
466             }
467           }
468 
469           if (header_flag == SBR_RESET) {
470             err = ixheaacd_calc_frq_bnd_tbls(ptr_header_data[k],
471 
472                                              self->pstr_common_tables);
473             if (err) {
474               return err;
475             }
476           }
477         }
478 
479         {
480           WORD32 lr;
481           WORD32 lr1 = ps_enable ? 2 : num_channels;
482           for (lr = 0; lr < lr1; lr++) {
483             ptr_frame_data[lr]->reset_flag = 1;
484             if (ptr_header_data[k]->status) {
485               err |= ixheaacd_sbr_dec_reset(
486                   &(pstr_sbr_channel[lr]->str_sbr_dec), ptr_header_data[k],
487                   low_pow_flag, self->pstr_common_tables,
488                   ptr_frame_data[k]->pitch_in_bins, audio_object_type);
489               if (err < 0) return err;
490             }
491           }
492           ptr_header_data[k]->status = 0;
493         }
494 
495         if (err == 0) {
496           ptr_header_data[k]->sync_state = SBR_ACTIVE;
497         }
498       }
499     }
500 
501     if (err || (ptr_header_data[k]->sync_state == SBR_NOT_INITIALIZED)) {
502       WORD32 lr1 = ps_enable ? 2 : num_channels;
503       ixheaacd_prepare_upsamp(ptr_header_data, pstr_sbr_channel, lr1);
504     }
505 
506     if (frame_status && (ptr_header_data[k]->sync_state == SBR_ACTIVE)) {
507       if (stereo) {
508         frame_status = ixheaacd_sbr_read_cpe(ptr_header_data[0], ptr_frame_data,
509                                              it_bit_buff, self->pstr_sbr_tables,
510                                              audio_object_type);
511       } else {
512         if (ps_enable) {
513           if (down_mix_flag) {
514             self->pstr_ps_stereo_dec->force_mono = 1;
515           } else {
516             self->pstr_ps_stereo_dec->force_mono = 0;
517           }
518         } else {
519           self->pstr_ps_stereo_dec = 0;
520         }
521         if (ptr_frame_data[k]->sbr_mode == ORIG_SBR) {
522           frame_status = ixheaacd_sbr_read_sce(
523               ptr_header_data[k], ptr_frame_data[k], self->pstr_ps_stereo_dec,
524               it_bit_buff, self->pstr_sbr_tables, audio_object_type);
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       ixheaacd_dec_sbrdata_for_pvc(ptr_header_data[0], ptr_frame_data[0],
567                                    pstr_sbr_channel[0]->pstr_prev_frame_data);
568     } else if (ptr_frame_data[0]->sbr_mode == ORIG_SBR) {
569       ixheaacd_dec_sbrdata(
570           ptr_header_data[0], ptr_header_data[1], ptr_frame_data[0],
571           pstr_sbr_channel[0]->pstr_prev_frame_data,
572           (stereo || dual_mono) ? ptr_frame_data[1] : NULL,
573           (stereo || dual_mono) ? pstr_sbr_channel[1]->pstr_prev_frame_data
574                                 : NULL,
575           self->pstr_common_tables);
576     }
577 
578     if (ptr_header_data[0]->channel_mode == PS_STEREO &&
579         (audio_object_type != AOT_ER_AAC_ELD &&
580          audio_object_type != AOT_ER_AAC_LD)) {
581       ixheaacd_decode_ps_data(self->pstr_ps_stereo_dec);
582       ps_flag = 1;
583       self->ps_present = ps_flag;
584     }
585 
586     ptr_frame_data[0]->max_qmf_subband_aac =
587         ptr_header_data[0]->pstr_freq_band_data->sub_band_start;
588     if (stereo) {
589       ptr_frame_data[1]->max_qmf_subband_aac =
590           ptr_header_data[1]->pstr_freq_band_data->sub_band_start;
591     }
592   }
593   if (audio_object_type != AOT_ER_AAC_ELD) {
594     if ((initial_sync_state == SBR_NOT_INITIALIZED) &&
595         ptr_header_data[0]->err_flag) {
596       ptr_header_data[0]->sync_state = SBR_NOT_INITIALIZED;
597     }
598   } else {
599     ptr_header_data[0]->sync_state = SBR_ACTIVE;
600   }
601 
602   if ((num_channels == 2) && !(stereo || dual_mono)) {
603     ixheaacd_downmix_to_monosbr(&core_sample_buf[slot_element], ch_fac);
604   }
605 
606   if ((!prev_stereo && !prev_ps_flag) && (ps_flag)) {
607     WORD32 copy_size;
608     if (down_samp_flag)
609       copy_size = QMF_FILTER_STATE_SYN_SIZE_DOWN_SAMPLED;
610     else
611       copy_size = QMF_FILTER_STATE_SYN_SIZE;
612 
613     memcpy(
614         pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
615         pstr_sbr_channel[0]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
616         copy_size * sizeof(WORD16));
617 
618     pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale =
619         pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale;
620   }
621 
622   if ((!prev_stereo && stereo && (num_channels == 2)) &&
623       (audio_object_type != AOT_ER_AAC_ELD)) {
624     WORD32 copy_size;
625     if (down_samp_flag)
626       copy_size = QMF_FILTER_STATE_SYN_SIZE_DOWN_SAMPLED;
627     else
628       copy_size = QMF_FILTER_STATE_SYN_SIZE;
629 
630     memcpy(
631         pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
632         pstr_sbr_channel[0]->str_sbr_dec.str_synthesis_qmf_bank.filter_states,
633         copy_size * sizeof(WORD16));
634 
635     pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale =
636         pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_syn_scale;
637 
638     memcpy(
639         pstr_sbr_channel[1]->str_sbr_dec.str_codec_qmf_bank.anal_filter_states,
640         pstr_sbr_channel[0]->str_sbr_dec.str_codec_qmf_bank.anal_filter_states,
641         QMF_FILTER_STATE_ANA_SIZE * sizeof(WORD16));
642 
643     pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.st_lb_scale =
644         pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.st_lb_scale;
645 
646     memcpy(pstr_sbr_channel[1]->str_sbr_dec.ptr_sbr_overlap_buf,
647            pstr_sbr_channel[0]->str_sbr_dec.ptr_sbr_overlap_buf,
648            MAX_OV_COLS * NO_SYNTHESIS_CHANNELS * sizeof(WORD32));
649 
650     pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.ov_lb_scale =
651         pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.ov_lb_scale;
652     pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact.ov_hb_scale =
653         pstr_sbr_channel[0]->str_sbr_dec.str_sbr_scale_fact.ov_hb_scale;
654   }
655   pstr_sbr_channel[0]->str_sbr_dec.time_sample_buf = self->time_sample_buf[0];
656   if (pstr_drc_dec == NULL) {
657     WORD32 err_code = 0;
658     err_code = ixheaacd_sbr_dec(
659         &pstr_sbr_channel[0]->str_sbr_dec, core_sample_buf + slot_element,
660         ptr_header_data[0], ptr_frame_data[0],
661         pstr_sbr_channel[0]->pstr_prev_frame_data, self->pstr_ps_stereo_dec,
662         &pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank,
663         &pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact,
664         (ptr_header_data[0]->sync_state == SBR_ACTIVE), low_pow_flag,
665         sbr_scratch_struct->ptr_work_buf_core, self->pstr_sbr_tables,
666         self->pstr_common_tables, ch_fac, self->ptr_pvc_data_str, 0, NULL,
667         audio_object_type);
668     if (err_code) return err_code;
669   } else {
670     WORD32 err_code = 0;
671     err_code = ixheaacd_sbr_dec(
672         &pstr_sbr_channel[0]->str_sbr_dec, core_sample_buf + slot_element,
673         ptr_header_data[0], ptr_frame_data[0],
674         pstr_sbr_channel[0]->pstr_prev_frame_data, self->pstr_ps_stereo_dec,
675         &pstr_sbr_channel[1]->str_sbr_dec.str_synthesis_qmf_bank,
676         &pstr_sbr_channel[1]->str_sbr_dec.str_sbr_scale_fact,
677         (ptr_header_data[0]->sync_state == SBR_ACTIVE), low_pow_flag,
678         sbr_scratch_struct->ptr_work_buf_core, self->pstr_sbr_tables,
679         self->pstr_common_tables, ch_fac, self->ptr_pvc_data_str,
680         pstr_drc_dec->drc_on,
681         pstr_drc_dec->str_drc_channel_data[0].drc_factors_sbr,
682         audio_object_type);
683     if (err_code) return err_code;
684   }
685 
686   if (!down_mix_flag && (stereo || dual_mono) && (num_channels == 2)) {
687     pstr_sbr_channel[1]->str_sbr_dec.time_sample_buf = self->time_sample_buf[1];
688 
689     if (ele_channels == 1 && usac_flag) {
690       WORD32 err_code = ixheaacd_esbr_dec(
691           &pstr_sbr_channel[1]->str_sbr_dec, ptr_header_data[1],
692           ptr_frame_data[1], (ptr_header_data[1]->sync_state == SBR_ACTIVE),
693           low_pow_flag, self->pstr_sbr_tables, ch_fac);
694       if (err_code) return err_code;
695     } else {
696       if (pstr_drc_dec == NULL) {
697         WORD32 err_code = ixheaacd_sbr_dec(
698             &pstr_sbr_channel[1]->str_sbr_dec,
699             core_sample_buf + slot_element + 1, ptr_header_data[1],
700             ptr_frame_data[1], pstr_sbr_channel[1]->pstr_prev_frame_data, NULL,
701             NULL, NULL, (ptr_header_data[1]->sync_state == SBR_ACTIVE),
702             low_pow_flag, sbr_scratch_struct->ptr_work_buf_core,
703             self->pstr_sbr_tables, self->pstr_common_tables, ch_fac,
704             self->ptr_pvc_data_str, 0, NULL, audio_object_type);
705         if (err_code) return err_code;
706       } else {
707         WORD32 err_code = ixheaacd_sbr_dec(
708             &pstr_sbr_channel[1]->str_sbr_dec,
709             core_sample_buf + slot_element + 1, ptr_header_data[1],
710             ptr_frame_data[1], pstr_sbr_channel[1]->pstr_prev_frame_data, NULL,
711             NULL, NULL, (ptr_header_data[1]->sync_state == SBR_ACTIVE),
712             low_pow_flag, sbr_scratch_struct->ptr_work_buf_core,
713             self->pstr_sbr_tables, self->pstr_common_tables, ch_fac,
714             self->ptr_pvc_data_str, pstr_drc_dec->drc_on,
715             pstr_drc_dec->str_drc_channel_data[1].drc_factors_sbr,
716             audio_object_type);
717         if (err_code) return err_code;
718       }
719     }
720 
721   } else {
722     if (audio_object_type != AOT_ER_AAC_ELD &&
723         audio_object_type != AOT_ER_AAC_LD)
724 
725     {
726       if (sub_d((WORD16)ptr_header_data[0]->channel_mode, PS_STEREO) == 0) {
727         num_channels = 2;
728       }
729     }
730   }
731   *codec_num_channels = num_channels;
732   self->sbr_mode = ptr_frame_data[0]->sbr_mode;
733 
734   if (pstr_drc_dec != NULL) {
735     WORD32 i, j;
736     for (i = 0; i < *codec_num_channels; i++) {
737       for (j = 0; j < 32; j++) {
738         memcpy(pstr_drc_dec->str_drc_channel_data[i].drc_factors_sbr[j],
739                pstr_drc_dec->str_drc_channel_data[i].drc_factors_sbr[j + 32],
740                64 * sizeof(WORD32));
741       }
742     }
743   }
744 
745   return SBRDEC_OK;
746 }
747