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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 
25 #include "ixheaacd_type_def.h"
26 
27 #include "ixheaacd_cnst.h"
28 
29 #include "ixheaacd_bitbuffer.h"
30 #include "ixheaacd_config.h"
31 #include "ixheaacd_interface.h"
32 #include "ixheaacd_acelp_info.h"
33 
34 #include "ixheaacd_tns_usac.h"
35 #include "ixheaacd_sbrdecsettings.h"
36 #include "ixheaacd_info.h"
37 #include "ixheaacd_struct.h"
38 #include "ixheaacd_sbr_common.h"
39 #include "ixheaacd_drc_data_struct.h"
40 #include "ixheaacd_drc_dec.h"
41 
42 #include "ixheaacd_sbr_scale.h"
43 #include "ixheaacd_env_extr_part.h"
44 #include "ixheaacd_sbr_rom.h"
45 #include "ixheaacd_hybrid.h"
46 #include "ixheaacd_ps_dec.h"
47 #include "ixheaacd_common_rom.h"
48 #include "ixheaacd_qmf_dec.h"
49 #include "ixheaacd_sbr_const.h"
50 #include "ixheaacd_lpp_tran.h"
51 #include "ixheaacd_sbrdecoder.h"
52 #include "ixheaacd_env_extr.h"
53 #include "ixheaacd_env_calc.h"
54 #include "ixheaacd_pvc_dec.h"
55 #include "ixheaacd_sbr_dec.h"
56 #include "ixheaacd_mps_polyphase.h"
57 #include "ixheaacd_sbr_const.h"
58 
59 #include "ixheaacd_main.h"
60 
61 #include "ixheaacd_arith_dec.h"
62 
63 #include "ixheaacd_memory_standards.h"
64 #include "ixheaacd_sbrdecsettings.h"
65 #include "ixheaacd_defines.h"
66 #include "ixheaacd_aac_rom.h"
67 #include "ixheaacd_common_rom.h"
68 #include "ixheaacd_sbr_rom.h"
69 #include "ixheaacd_bitbuffer.h"
70 #include "ixheaacd_pulsedata.h"
71 #include "ixheaacd_pns.h"
72 
73 #include "ixheaacd_lt_predict.h"
74 
75 #include "ixheaacd_channelinfo.h"
76 #include "ixheaacd_channel.h"
77 #include "ixheaacd_channelinfo.h"
78 #include "ixheaacd_sbrdecoder.h"
79 #include "ixheaacd_audioobjtypes.h"
80 #include "ixheaacd_latmdemux.h"
81 #include "ixheaacd_aacdec.h"
82 #include "ixheaacd_sbr_common.h"
83 
84 #include "ixheaacd_mps_polyphase.h"
85 #include "ixheaacd_config.h"
86 #include "ixheaacd_mps_dec.h"
87 #include "ixheaacd_struct_def.h"
88 
89 #include "ixheaacd_create.h"
90 
91 #include "ixheaacd_process.h"
92 
93 #include "ixheaacd_sbrdecoder.h"
94 
95 #include "ixheaacd_mps_interface.h"
96 
97 #include "ixheaacd_bit_extract.h"
98 #include "ixheaacd_func_def.h"
99 #include "ixheaacd_interface.h"
100 
101 extern const ia_huff_code_word_struct ixheaacd_huff_book_scl[];
102 
103 extern const WORD32 ixheaacd_book_scl_index[];
104 extern const WORD16 ixheaacd_book_scl_code_book[];
105 
106 extern const ia_usac_samp_rate_info ixheaacd_samp_rate_info[];
107 extern const WORD32 ixheaacd_sampling_boundaries[(1 << LEN_SAMP_IDX)];
108 
109 const WORD32 ixheaacd_sampl_freq_idx_table[17] = {
110     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000,
111     12000, 11025, 8000,  7350,  -1,    -1,    -1,    -1};
112 
ixheaacd_info_init(const ia_usac_samp_rate_info * ptr_samp_info,WORD32 block_size_samples,ia_sfb_info_struct * pstr_sfb_info_long,ia_sfb_info_struct * pstr_sfb_info_short,WORD16 * sfb_width_short,WORD16 * sfb_width_long)113 static VOID ixheaacd_info_init(const ia_usac_samp_rate_info *ptr_samp_info,
114                                WORD32 block_size_samples,
115                                ia_sfb_info_struct *pstr_sfb_info_long,
116                                ia_sfb_info_struct *pstr_sfb_info_short,
117                                WORD16 *sfb_width_short,
118                                WORD16 *sfb_width_long) {
119   WORD32 i, j, k, n, ws;
120   const WORD16 *sfbands;
121   ia_sfb_info_struct *pstr_sfb_info_ip;
122 
123   pstr_sfb_info_long->islong = 1;
124   pstr_sfb_info_long->max_win_len = 1;
125   pstr_sfb_info_long->samp_per_bk = block_size_samples;
126 
127   switch (block_size_samples) {
128     case 480:
129       pstr_sfb_info_long->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_480;
130       pstr_sfb_info_long->sfb_per_sbk = ptr_samp_info->num_sfb_480;
131       break;
132     case 512:
133       pstr_sfb_info_long->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_512;
134       pstr_sfb_info_long->sfb_per_sbk = ptr_samp_info->num_sfb_512;
135       break;
136     case 768:
137       pstr_sfb_info_long->sfb_per_sbk = ptr_samp_info->num_sfb_768;
138       pstr_sfb_info_long->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_768;
139       break;
140     case 960:
141       pstr_sfb_info_long->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_960;
142       pstr_sfb_info_long->sfb_per_sbk = ptr_samp_info->num_sfb_960;
143       break;
144     case 1024:
145       pstr_sfb_info_long->sfb_per_sbk = ptr_samp_info->num_sfb_1024;
146       pstr_sfb_info_long->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_1024;
147       break;
148     default:
149       assert(0);
150       break;
151   }
152 
153   pstr_sfb_info_long->sfb_width = sfb_width_long;
154   pstr_sfb_info_long->num_groups = 1;
155   pstr_sfb_info_long->group_len[0] = 1;
156 
157   for (i = 0, j = 0, n = pstr_sfb_info_long->sfb_per_sbk; i < n; i++) {
158     k = pstr_sfb_info_long->ptr_sfb_tbl[i];
159     pstr_sfb_info_long->sfb_width[i] = k - j;
160     j = k;
161   }
162 
163   pstr_sfb_info_short->islong = 0;
164   pstr_sfb_info_short->max_win_len = NSHORT;
165   pstr_sfb_info_short->samp_per_bk = block_size_samples;
166 
167   for (i = 0; i < pstr_sfb_info_short->max_win_len; i++) {
168     switch (block_size_samples) {
169       case 768:
170         pstr_sfb_info_short->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_96;
171         pstr_sfb_info_short->sfb_per_sbk = ptr_samp_info->num_sfb_96;
172         break;
173       case 960:
174         pstr_sfb_info_short->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_120;
175         pstr_sfb_info_short->sfb_per_sbk = ptr_samp_info->num_sfb_120;
176         break;
177       case 1024:
178         pstr_sfb_info_short->ptr_sfb_tbl = ptr_samp_info->ptr_sfb_128;
179         pstr_sfb_info_short->sfb_per_sbk = ptr_samp_info->num_sfb_128;
180         break;
181       default:
182         assert(0);
183         break;
184     }
185   }
186 
187   pstr_sfb_info_short->sfb_width = sfb_width_short;
188   for (i = 0, j = 0, n = pstr_sfb_info_short->sfb_per_sbk; i < n; i++) {
189     k = pstr_sfb_info_short->ptr_sfb_tbl[i];
190     pstr_sfb_info_short->sfb_width[i] = k - j;
191     j = k;
192   }
193 
194   pstr_sfb_info_ip = pstr_sfb_info_long;
195   for (ws = 0; ws < 2; ws++) {
196     pstr_sfb_info_ip->sfb_per_bk = 0;
197     k = 0;
198     n = 0;
199     for (i = 0; i < pstr_sfb_info_ip->max_win_len; i++) {
200       pstr_sfb_info_ip->bins_per_sbk =
201           pstr_sfb_info_ip->samp_per_bk / pstr_sfb_info_ip->max_win_len;
202 
203       pstr_sfb_info_ip->sfb_per_bk += pstr_sfb_info_ip->sfb_per_sbk;
204 
205       sfbands = pstr_sfb_info_ip->ptr_sfb_tbl;
206       for (j = 0; j < pstr_sfb_info_ip->sfb_per_sbk; j++)
207         pstr_sfb_info_ip->sfb_idx_tbl[j + k] = sfbands[j] + n;
208 
209       n += pstr_sfb_info_ip->bins_per_sbk;
210       k += pstr_sfb_info_ip->sfb_per_sbk;
211     }
212     pstr_sfb_info_ip = pstr_sfb_info_short;
213   }
214 }
215 
ixheaacd_decode_init(VOID * handle,WORD32 sample_rate,ia_usac_data_struct * usac_data,ia_audio_specific_config_struct * pstr_stream_config)216 WORD32 ixheaacd_decode_init(
217     VOID *handle, WORD32 sample_rate, ia_usac_data_struct *usac_data,
218     ia_audio_specific_config_struct *pstr_stream_config) {
219   WORD32 i;
220   ia_exhaacplus_dec_api_struct *codec_handle =
221       (ia_exhaacplus_dec_api_struct *)handle;
222   ia_aac_dec_state_struct *aac_dec_handle = codec_handle->p_state_aac;
223   WORD32 fscale;
224 
225   WORD32 ele_id = 0;
226 
227   ia_usac_config_struct *ptr_usac_config =
228       &(pstr_stream_config->str_usac_config);
229   ia_usac_decoder_config_struct *ptr_usac_dec_config =
230       &(pstr_stream_config->str_usac_config.str_usac_dec_config);
231   WORD32 num_elements = ptr_usac_dec_config->num_elements;
232   WORD32 chan = 0;
233 
234   usac_data->huffman_code_book_scl = aac_dec_handle->huffman_code_book_scl;
235   usac_data->huffman_code_book_scl_index =
236       aac_dec_handle->huffman_code_book_scl_index;
237 
238   usac_data->tns_coeff3_32 =
239       aac_dec_handle->pstr_aac_tables->pstr_block_tables->tns_coeff3_32;
240   usac_data->tns_coeff4_32 =
241       aac_dec_handle->pstr_aac_tables->pstr_block_tables->tns_coeff4_32;
242   usac_data->tns_max_bands_tbl_usac =
243       &aac_dec_handle->pstr_aac_tables->pstr_block_tables
244            ->tns_max_bands_tbl_usac;
245 
246   for (i = 0; i < 11; i++) {
247     if (ixheaacd_sampling_boundaries[i] <= sample_rate) break;
248   }
249 
250   if (i == (1 << LEN_SAMP_IDX)) return -1;
251   usac_data->sampling_rate_idx = i;
252 
253   fscale = (WORD32)((double)sample_rate * (double)FSCALE_DENOM / 12800.0f);
254 
255   for (i = 0; i < MAX_NUM_CHANNELS; i++) {
256     usac_data->window_shape_prev[i] = 0;
257     usac_data->window_shape[i] = 0;
258   }
259 
260   ixheaacd_hufftab(&ixheaacd_book, ixheaacd_huff_book_scl,
261                    ixheaacd_book_scl_code_book, ixheaacd_book_scl_index, 1, 60,
262                    60, 1, 19);
263 
264   usac_data->pstr_usac_winmap[0] = &usac_data->str_only_long_info;
265   usac_data->pstr_usac_winmap[1] = &usac_data->str_only_long_info;
266   usac_data->pstr_usac_winmap[2] = &usac_data->str_eight_short_info;
267   usac_data->pstr_usac_winmap[3] = &usac_data->str_only_long_info;
268   usac_data->pstr_usac_winmap[4] = &usac_data->str_only_long_info;
269 
270   if ((usac_data->ccfl != 480) && (usac_data->ccfl != 512) &&
271       (usac_data->ccfl != 768) && (usac_data->ccfl != 960) &&
272       (usac_data->ccfl != 1024))
273     return -1;
274   ixheaacd_info_init(&ixheaacd_samp_rate_info[usac_data->sampling_rate_idx],
275                      usac_data->ccfl, usac_data->pstr_usac_winmap[0],
276                      usac_data->pstr_usac_winmap[2], usac_data->sfb_width_short,
277                      usac_data->sfb_width_long);
278 
279   for (i = 0; i < MAX_NUM_CHANNELS; i++) {
280     usac_data->str_tddec[i] = &usac_data->arr_str_tddec[i];
281     if (usac_data->ccfl == 768)
282       usac_data->str_tddec[i]->fscale = pstr_stream_config->sampling_frequency;
283     else
284       usac_data->str_tddec[i]->fscale =
285           ((fscale)*usac_data->ccfl) / LEN_SUPERFRAME;
286     usac_data->len_subfrm = usac_data->ccfl / 4;
287     usac_data->num_subfrm = (MAX_NUM_SUBFR * usac_data->ccfl) / LEN_SUPERFRAME;
288 
289     ixheaacd_init_acelp_data(usac_data, usac_data->str_tddec[i]);
290 
291     usac_data->str_tddec[i]->fd_synth =
292         &usac_data->str_tddec[i]->fd_synth_buf[LEN_FRAME];
293   }
294 
295   for (ele_id = 0; ele_id < num_elements; ele_id++) {
296     UWORD32 ele_type;
297     WORD32 stereo_config_index;
298 
299     ia_usac_dec_element_config_struct *ptr_usac_ele_config =
300         &ptr_usac_config->str_usac_dec_config.str_usac_element_config[ele_id];
301 
302     if (ptr_usac_ele_config) {
303       if (usac_data->tw_mdct[ele_id]) {
304         return -1;
305       }
306 
307       usac_data->noise_filling_config[ele_id] =
308           ptr_usac_ele_config->noise_filling;
309     }
310 
311     ele_type = ptr_usac_config->str_usac_dec_config.usac_element_type[ele_id];
312 
313     stereo_config_index = ptr_usac_ele_config->stereo_config_index;
314 
315     switch (ele_type) {
316       case ID_USAC_SCE:
317       case ID_USAC_LFE:
318 
319         if ((chan + 1) > MAX_NUM_CHANNELS_USAC_LVL2) return -1;
320         usac_data->seed_value[chan] = 0x3039;
321         chan++;
322 
323         break;
324 
325       case ID_USAC_CPE: {
326         static const WORD32 frame_len_tbl[MAX_CORE_SBR_FRAME_LEN_IDX + 1] = {
327             -1, -1, 32, 32, 64};
328 
329         if ((chan + 2) > MAX_NUM_CHANNELS_USAC_LVL2) return -1;
330         usac_data->seed_value[chan] = 0x3039;
331         chan++;
332 
333         usac_data->seed_value[chan] = 0x10932;
334         chan++;
335 
336         if (stereo_config_index > 0) {
337           WORD32 bs_frame_length =
338               frame_len_tbl[ptr_usac_config->core_sbr_framelength_index];
339           WORD32 bs_residual_coding = (stereo_config_index > 1) ? 1 : 0;
340 
341           ia_usac_dec_mps_config_struct *ptr_usac_mps212_config =
342               &(ptr_usac_config->str_usac_dec_config
343                     .str_usac_element_config[ele_id]
344                     .str_usac_mps212_config);
345 
346           if (ixheaacd_mps_create(&aac_dec_handle->mps_dec_handle,
347                                   bs_frame_length, bs_residual_coding,
348                                   ptr_usac_mps212_config)) {
349             return -1;
350           }
351         }
352         break;
353       }
354 
355       break;
356       case ID_USAC_EXT:
357         break;
358       default:
359         return -1;
360         break;
361     }
362   }
363 
364   return 0;
365 }
366 
ixheaacd_dec_data_init(VOID * handle,ia_frame_data_struct * pstr_frame_data,ia_usac_data_struct * usac_data)367 WORD32 ixheaacd_dec_data_init(VOID *handle,
368                               ia_frame_data_struct *pstr_frame_data,
369                               ia_usac_data_struct *usac_data) {
370   ia_audio_specific_config_struct *pstr_stream_config, *layer_config;
371   WORD32 err_code = 0;
372 
373   WORD32 num_out_chan = 0;
374 
375   WORD32 i_ch, i, ele_id;
376   WORD32 num_elements;
377 
378   WORD32 out_frame_len, sbr_ratio_idx;
379 
380   ia_usac_config_struct *ptr_usac_config =
381       &(pstr_frame_data->str_audio_specific_config.str_usac_config);
382 
383   usac_data->window_shape_prev[0] = WIN_SEL_0;
384   usac_data->window_shape_prev[1] = WIN_SEL_0;
385 
386   pstr_frame_data->str_layer.bit_rate =
387       pstr_frame_data->str_audio_specific_config.avg_bit_rate;
388   pstr_stream_config = &pstr_frame_data->str_audio_specific_config;
389   layer_config = &pstr_frame_data->str_audio_specific_config;
390 
391   sbr_ratio_idx = ixheaacd_sbr_params(
392       ptr_usac_config->core_sbr_framelength_index, &out_frame_len,
393       &usac_data->ccfl, &usac_data->output_samples,
394       &pstr_frame_data->str_layer.sample_rate_layer,
395       &layer_config->samp_frequency_index);
396 
397   pstr_stream_config->sampling_frequency =
398       pstr_frame_data->str_layer.sample_rate_layer;
399   pstr_stream_config->samp_frequency_index = layer_config->samp_frequency_index;
400 
401   num_elements = ptr_usac_config->str_usac_dec_config.num_elements;
402 
403   for (ele_id = 0; ele_id < num_elements; ele_id++) {
404     ia_usac_dec_element_config_struct *ptr_usac_ele_config =
405         &(ptr_usac_config->str_usac_dec_config.str_usac_element_config[ele_id]);
406 
407     if (ptr_usac_ele_config) {
408       usac_data->tw_mdct[ele_id] = ptr_usac_ele_config->tw_mdct;
409     }
410 
411     {
412       ia_usac_dec_mps_config_struct *ptr_usac_mps212_config =
413           &ptr_usac_ele_config->str_usac_mps212_config;
414       WORD32 stereo_config_index = ptr_usac_ele_config->stereo_config_index;
415 
416       usac_data->mps_pseudo_lr[ele_id] =
417           (stereo_config_index > 1) ? ptr_usac_mps212_config->bs_pseudo_lr : 0;
418     }
419   }
420 
421   usac_data->sbr_ratio_idx = sbr_ratio_idx;
422   usac_data->esbr_bit_str[0].no_elements = 0;
423   usac_data->esbr_bit_str[1].no_elements = 0;
424 
425   num_out_chan = ptr_usac_config->num_out_channels;
426 
427   if (usac_data->ccfl == 768)
428     pstr_frame_data->str_layer.sample_rate_layer =
429         4 * pstr_frame_data->str_layer.sample_rate_layer / 3;
430 
431   for (i = 0; i < MAX_NUM_CHANNELS; i++) {
432     usac_data->coef_fix[i] = &usac_data->arr_coef_fix[i][0];
433     usac_data->coef[i] = &usac_data->arr_coef[i][0];
434     usac_data->coef_save[i] = &usac_data->arr_coef_save[i][0];
435     usac_data->factors[i] = &usac_data->arr_factors[i][0];
436     usac_data->group_dis[i] = &usac_data->arr_group_dis[i][0];
437     usac_data->pstr_tns[i] = &usac_data->arr_str_tns[i];
438     usac_data->tw_ratio[i] = &usac_data->arr_tw_ratio[i][0];
439     usac_data->ms_used[i] = &usac_data->arr_ms_used[i][0];
440     usac_data->window_shape_prev[i] = WIN_SEL_0;
441 
442     usac_data->seed_value[i] = 0x0;
443 
444     usac_data->fac_data_present[i] = 0;
445   }
446 
447   err_code =
448       ixheaacd_decode_init(handle, pstr_frame_data->str_layer.sample_rate_layer,
449                            usac_data, pstr_stream_config);
450   if (err_code != 0) return err_code;
451 
452   for (i_ch = 0; i_ch < MAX_NUM_CHANNELS; i_ch++) {
453     if (usac_data->tw_mdct[0] == 1) {
454       WORD32 i;
455       for (i = 0; i < 2 * usac_data->ccfl; i++) {
456         usac_data->warp_cont_mem[i_ch][i] = 1.0;
457       }
458       usac_data->warp_sum[i_ch][0] = usac_data->warp_sum[i_ch][1] =
459           (FLOAT32)usac_data->ccfl;
460     }
461   }
462   return err_code;
463 }
464 
ixheaacd_count_tracks_per_layer(int * max_layer,int * stream_count,int * tracks_in_layer)465 static VOID ixheaacd_count_tracks_per_layer(int *max_layer, int *stream_count,
466                                             int *tracks_in_layer) {
467   WORD32 stream;
468   WORD32 num_layer;
469   WORD32 num_streams;
470   WORD32 layer = 0;
471 
472   if (stream_count == NULL)
473     num_streams = 0;
474   else
475     num_streams = *stream_count;
476   if (max_layer == NULL)
477     num_layer = num_streams;
478   else
479     num_layer = *max_layer;
480   if (num_layer < 0) num_layer = num_streams;
481 
482   for (stream = 0; (layer <= num_layer) && (stream < num_streams);) {
483     *tracks_in_layer = 1;
484     stream += 1;
485     layer++;
486     if (layer <= num_layer) *tracks_in_layer = 0;
487   }
488 
489   if (max_layer) *max_layer = (layer - 1);
490   if (stream_count) *stream_count = stream;
491 }
492 
ixheaacd_frm_data_init(ia_audio_specific_config_struct * pstr_audio_conf,ia_dec_data_struct * pstr_dec_data)493 WORD32 ixheaacd_frm_data_init(ia_audio_specific_config_struct *pstr_audio_conf,
494                               ia_dec_data_struct *pstr_dec_data)
495 
496 {
497   WORD32 layer;
498   WORD32 track;
499   WORD32 num_dec_streams;
500   ia_frame_data_struct *pstr_frame_data;
501 
502   WORD32 stream_count = 1;
503   WORD32 max_layer = -1;
504 
505   memset(pstr_dec_data, 0, sizeof(ia_dec_data_struct));
506   memset(&(pstr_dec_data->str_frame_data), 0,
507          sizeof(pstr_dec_data->str_frame_data));
508 
509   pstr_frame_data = &(pstr_dec_data->str_frame_data);
510 
511   if (max_layer < 0) max_layer = stream_count - 1;
512 
513   ixheaacd_count_tracks_per_layer(&max_layer, &stream_count,
514                                   &pstr_frame_data->tracks_in_layer);
515 
516   pstr_frame_data->scal_out_select = max_layer;
517 
518   pstr_frame_data->stream_count = 0;
519 
520   num_dec_streams = track = 0;
521   for (layer = 0; layer < (signed)pstr_frame_data->scal_out_select + 1;
522        layer++) {
523     WORD32 j;
524     for (j = 0; j < 1; j++, num_dec_streams++) {
525       pstr_frame_data->str_audio_specific_config = *pstr_audio_conf;
526       pstr_frame_data->str_layer.sample_rate_layer =
527           pstr_frame_data->str_audio_specific_config.sampling_frequency;
528       pstr_frame_data->str_layer.bit_rate =
529           pstr_frame_data->str_audio_specific_config.avg_bit_rate;
530     }
531 
532     track += pstr_frame_data->tracks_in_layer;
533   }
534 
535   pstr_frame_data->stream_count = num_dec_streams;
536 
537   return num_dec_streams;
538 }
539 
ixheaacd_decode_create(ia_exhaacplus_dec_api_struct * handle,ia_dec_data_struct * pstr_dec_data,WORD32 tracks_for_decoder)540 WORD32 ixheaacd_decode_create(ia_exhaacplus_dec_api_struct *handle,
541                               ia_dec_data_struct *pstr_dec_data,
542                               WORD32 tracks_for_decoder) {
543   WORD32 stream;
544 
545   WORD32 err = 0;
546   ia_frame_data_struct *pstr_frame_data;
547   WORD32 stream_count;
548   ia_aac_dec_state_struct *aac_dec_handle = handle->p_state_aac;
549   pstr_frame_data = &(pstr_dec_data->str_frame_data);
550   stream_count = pstr_frame_data->stream_count;
551   pstr_frame_data->stream_count = tracks_for_decoder;
552 
553   for (stream = 0; stream < stream_count; stream++) {
554     UWORD32 aot = pstr_frame_data->str_audio_specific_config.audio_object_type;
555 
556     switch (aot) {
557       case AOT_USAC:
558 
559         err = ixheaacd_dec_data_init(handle, pstr_frame_data,
560                                      &(pstr_dec_data->str_usac_data));
561 
562         if (err != 0) return err;
563 
564         switch (pstr_dec_data->str_usac_data.sbr_ratio_idx) {
565           case 0:
566             handle->aac_config.ui_sbr_mode = 0;
567             break;
568           case 1:
569             handle->aac_config.ui_sbr_mode = 1;
570             break;
571           case 2:
572             handle->aac_config.ui_sbr_mode = 1;
573             break;
574           case 3:
575             handle->aac_config.ui_sbr_mode = 3;
576             break;
577 
578           default:
579             handle->aac_config.ui_sbr_mode = 0;
580         }
581 
582 
583         break;
584 
585       default:
586 
587         break;
588     }
589   }
590 
591   pstr_frame_data->scal_out_object_type =
592       pstr_frame_data->str_audio_specific_config.audio_object_type;
593   pstr_frame_data->scal_out_num_channels =
594       pstr_frame_data->str_audio_specific_config.channel_configuration;
595   pstr_frame_data->scal_out_sampling_frequency =
596       pstr_frame_data->str_audio_specific_config.sampling_frequency;
597 
598   if (&(pstr_dec_data->str_usac_data) != NULL) {
599     ia_sbr_header_data_struct usac_def_header;
600     ia_audio_specific_config_struct *pstr_aud_spec_config =
601         &pstr_frame_data->str_audio_specific_config;
602     ia_usac_config_struct *ptr_usac_config =
603         &(pstr_frame_data->str_audio_specific_config.str_usac_config);
604 
605     WORD32 inter_tes[MAX_NUM_ELEMENTS] = {0};
606     WORD32 bs_pvc[MAX_NUM_ELEMENTS] = {0};
607     WORD32 harmonic_sbr[MAX_NUM_ELEMENTS] = {0};
608     WORD32 inter_test_flag = 0;
609     WORD32 bs_pvc_flag = 0;
610     WORD32 harmonic_Sbr_flag = 0;
611 
612     ia_usac_decoder_config_struct *ptr_usac_dec_config =
613         &ptr_usac_config->str_usac_dec_config;
614     WORD32 const num_ele = ptr_usac_dec_config->num_elements;
615     WORD32 elem_idx = 0;
616 
617     memset(&usac_def_header, 0, sizeof(ia_sbr_header_data_struct));
618 
619     for (elem_idx = 0; elem_idx < num_ele; elem_idx++) {
620       UWORD32 usac_ele_type =
621           ptr_usac_config->str_usac_dec_config.usac_element_type[elem_idx];
622       ia_usac_dec_element_config_struct *ptr_usac_ele_config =
623           &ptr_usac_config->str_usac_dec_config
624                .str_usac_element_config[elem_idx];
625 
626       ia_usac_dec_sbr_config_struct *ptr_usac_sbr_config =
627           &(ptr_usac_dec_config->str_usac_element_config[elem_idx]
628                 .str_usac_sbr_config);
629       inter_tes[elem_idx] =
630           (ptr_usac_sbr_config != NULL) ? ptr_usac_sbr_config->bs_inter_tes : 0;
631       bs_pvc[elem_idx] =
632           (ptr_usac_sbr_config != NULL) ? ptr_usac_sbr_config->bs_pvc : 0;
633       harmonic_sbr[elem_idx] =
634           (ptr_usac_sbr_config != NULL) ? ptr_usac_sbr_config->harmonic_sbr : 0;
635 
636       if (ptr_usac_sbr_config->bs_inter_tes) inter_test_flag = 1;
637       if (ptr_usac_sbr_config->bs_pvc) bs_pvc_flag = 1;
638       if (ptr_usac_sbr_config->harmonic_sbr) harmonic_Sbr_flag = 1;
639 
640       if ((usac_ele_type != ID_USAC_LFE) && (usac_ele_type != ID_USAC_EXT)) {
641         ia_usac_dec_sbr_config_struct *ptr_usac_sbr_config =
642             &(ptr_usac_ele_config->str_usac_sbr_config);
643 
644         usac_def_header.start_freq = ptr_usac_sbr_config->dflt_start_freq;
645         usac_def_header.stop_freq = ptr_usac_sbr_config->dflt_stop_freq;
646         usac_def_header.header_extra_1 =
647             ptr_usac_sbr_config->dflt_header_extra1;
648         usac_def_header.header_extra_2 =
649             ptr_usac_sbr_config->dflt_header_extra2;
650         usac_def_header.freq_scale = ptr_usac_sbr_config->dflt_freq_scale;
651         usac_def_header.alter_scale = ptr_usac_sbr_config->dflt_alter_scale;
652         usac_def_header.noise_bands = ptr_usac_sbr_config->dflt_noise_bands;
653         usac_def_header.limiter_bands = ptr_usac_sbr_config->dflt_limiter_bands;
654         usac_def_header.limiter_gains = ptr_usac_sbr_config->dflt_limiter_gains;
655         usac_def_header.interpol_freq = ptr_usac_sbr_config->dflt_interpol_freq;
656         usac_def_header.smoothing_mode =
657             ptr_usac_sbr_config->dflt_smoothing_mode;
658       }
659     }
660 
661     pstr_dec_data->str_usac_data.down_samp_sbr = 0;
662 
663     if (pstr_dec_data->str_usac_data.sbr_ratio_idx > 0) {
664       if (pstr_aud_spec_config->ext_sampling_frequency ==
665           pstr_aud_spec_config->sampling_frequency) {
666         pstr_dec_data->str_usac_data.down_samp_sbr = 1;
667       }
668       if (pstr_dec_data->str_usac_data.down_samp_sbr == 0) {
669         if (pstr_dec_data->str_usac_data.sbr_ratio_idx == 3) {
670           pstr_frame_data->scal_out_sampling_frequency =
671               4 * pstr_frame_data->scal_out_sampling_frequency;
672         } else {
673           pstr_frame_data->scal_out_sampling_frequency =
674               2 * pstr_frame_data->scal_out_sampling_frequency;
675         }
676       }
677 
678       {
679         void *sbr_persistent_mem_v = aac_dec_handle->sbr_persistent_mem_u;
680 
681         pstr_dec_data->str_usac_data.pstr_esbr_dec = ixheaacd_init_sbr(
682             pstr_frame_data->str_layer.sample_rate_layer,
683             pstr_dec_data->str_usac_data.ccfl,
684             &pstr_dec_data->str_usac_data.down_samp_sbr, sbr_persistent_mem_v,
685             NULL, pstr_frame_data->scal_out_num_channels, 0,
686             pstr_dec_data->str_usac_data.sbr_ratio_idx,
687             pstr_dec_data->str_usac_data.output_samples, &harmonic_Sbr_flag,
688             (void *)&usac_def_header, aac_dec_handle->str_sbr_config,
689             pstr_dec_data->str_usac_data.audio_object_type);
690         pstr_dec_data->str_usac_data.sbr_scratch_mem_base =
691             aac_dec_handle->sbr_scratch_mem_u;
692         if (num_ele)
693           ixheaacd_setesbr_flags(sbr_persistent_mem_v, bs_pvc_flag,
694                                  harmonic_Sbr_flag, inter_test_flag);
695       }
696 
697       if (pstr_dec_data->str_usac_data.pstr_esbr_dec == NULL) {
698         return -1;
699       } else {
700         pstr_dec_data->str_usac_data.pstr_esbr_dec->xaac_jmp_buf =
701             &(aac_dec_handle->xaac_jmp_buf);
702       }
703     }
704   }
705   aac_dec_handle->decode_create_done = 1;
706   return 0;
707 }
708