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 
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 
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 
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 
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       if (err) return err;
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       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