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