Coverage Report

Created: 2026-01-17 06:53

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