Coverage Report

Created: 2026-04-12 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/ixheaace_enc_init.c
Line
Count
Source
1
/******************************************************************************
2
 *                                                                            *
3
 * Copyright (C) 2023 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
21
#include <string.h>
22
#include "ixheaac_type_def.h"
23
#include "ixheaac_constants.h"
24
#include "ixheaace_aac_constants.h"
25
#include "impd_drc_common_enc.h"
26
#include "impd_drc_uni_drc.h"
27
#include "impd_drc_tables.h"
28
#include "impd_drc_api.h"
29
#include "ixheaace_api.h"
30
#include "ixheaac_error_standards.h"
31
#include "ixheaace_error_codes.h"
32
#include "ixheaace_psy_const.h"
33
#include "ixheaace_tns.h"
34
#include "ixheaace_tns_params.h"
35
#include "ixheaace_rom.h"
36
#include "ixheaace_common_rom.h"
37
#include "ixheaace_bitbuffer.h"
38
#include "ixheaac_basic_ops32.h"
39
#include "ixheaac_basic_ops40.h"
40
#include "ixheaac_basic_ops.h"
41
#include "ixheaace_adjust_threshold_data.h"
42
43
#include "ixheaace_dynamic_bits.h"
44
#include "ixheaace_qc_data.h"
45
#include "ixheaace_channel_map.h"
46
#include "ixheaace_block_switch.h"
47
#include "ixheaace_psy_data.h"
48
#include "ixheaace_interface.h"
49
#include "ixheaace_write_bitstream.h"
50
#include "ixheaace_psy_configuration.h"
51
#include "ixheaace_psy_mod.h"
52
#include "ixheaace_stereo_preproc.h"
53
#include "ixheaace_enc_main.h"
54
#include "ixheaace_qc_util.h"
55
#include "ixheaace_config_params.h"
56
#include "ixheaace_common_utils.h"
57
#define ALIGNMENT_DEFINE __attribute__((aligned(8)))
58
59
static WORD32 ixheaace_calculate_bandwidth(const WORD32 sample_rate,
60
                                           const WORD32 channel_bit_rate, const WORD32 num_ch,
61
1.12k
                                           WORD32 aot) {
62
1.12k
  WORD32 bandwidth = -1;
63
1.12k
  const ixheaace_bandwidth_table *pstr_bandwidth_table = NULL;
64
1.12k
  WORD32 bandwidth_table_size = 0;
65
1.12k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
66
730
    pstr_bandwidth_table = bandwidth_table_lc;
67
730
    bandwidth_table_size = sizeof(bandwidth_table_lc) / sizeof(ixheaace_bandwidth_table);
68
730
  } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
69
399
    switch (sample_rate) {
70
16
      case 48000:
71
60
      case 64000:
72
78
      case 88200:
73
103
      case 96000:
74
103
        pstr_bandwidth_table = bandwidth_table_ld_48000;
75
103
        bandwidth_table_size =
76
103
            sizeof(bandwidth_table_ld_48000) / sizeof(ixheaace_bandwidth_table);
77
103
        break;
78
22
      case 44100:
79
22
        pstr_bandwidth_table = bandwidth_table_ld_44100;
80
22
        bandwidth_table_size =
81
22
            sizeof(bandwidth_table_ld_44100) / sizeof(ixheaace_bandwidth_table);
82
22
        break;
83
23
      case 32000:
84
23
        pstr_bandwidth_table = bandwidth_table_ld_32000;
85
23
        bandwidth_table_size =
86
23
            sizeof(bandwidth_table_ld_32000) / sizeof(ixheaace_bandwidth_table);
87
23
        break;
88
113
      case 24000:
89
113
        pstr_bandwidth_table = bandwidth_table_ld_24000;
90
113
        bandwidth_table_size =
91
113
            sizeof(bandwidth_table_ld_24000) / sizeof(ixheaace_bandwidth_table);
92
113
        break;
93
45
      case 8000:
94
67
      case 11025:
95
80
      case 12000:
96
123
      case 16000:
97
138
      case 22050:
98
138
        pstr_bandwidth_table = bandwidth_table_ld_22050;
99
138
        bandwidth_table_size =
100
138
            sizeof(bandwidth_table_ld_22050) / sizeof(ixheaace_bandwidth_table);
101
138
        break;
102
399
    }
103
399
  }
104
2.63k
  for (WORD32 i = 0; i < bandwidth_table_size - 1; i++) {
105
2.63k
    if (channel_bit_rate >= 96000) {
106
160
      if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
107
98
        bandwidth = 20000;
108
98
      } else {
109
62
        if (num_ch == 1) {
110
35
          bandwidth = 19000;
111
35
        } else {
112
27
          bandwidth = 22000;
113
27
        }
114
62
      }
115
160
      break;
116
2.47k
    } else if (channel_bit_rate >= pstr_bandwidth_table[i].channel_bit_rate &&
117
2.47k
               channel_bit_rate < pstr_bandwidth_table[i + 1].channel_bit_rate) {
118
969
      if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
119
632
        bandwidth = (num_ch == 1) ? pstr_bandwidth_table[i].bandwidth_mono
120
632
                                  : pstr_bandwidth_table[i].bandwidth_stereo;
121
632
        bandwidth = bandwidth - (pstr_bandwidth_table[i].channel_bit_rate / 32);
122
632
        break;
123
632
      } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
124
337
        WORD32 start_bandwidth, end_bandwidth, start_bitrate, end_bitrate;
125
337
        FLOAT32 bandwidth_fac;
126
337
        start_bandwidth = (num_ch == 1) ? pstr_bandwidth_table[i].bandwidth_mono
127
337
                                        : pstr_bandwidth_table[i].bandwidth_stereo;
128
337
        start_bandwidth = start_bandwidth - (pstr_bandwidth_table[i].channel_bit_rate / 32);
129
337
        end_bandwidth = (num_ch == 1) ? pstr_bandwidth_table[i + 1].bandwidth_mono
130
337
                                      : pstr_bandwidth_table[i + 1].bandwidth_stereo;
131
337
        end_bandwidth = end_bandwidth - (pstr_bandwidth_table[i + 1].channel_bit_rate / 32);
132
337
        start_bitrate = pstr_bandwidth_table[i].channel_bit_rate;
133
337
        end_bitrate = pstr_bandwidth_table[i + 1].channel_bit_rate;
134
337
        bandwidth_fac =
135
337
            (FLOAT32)((channel_bit_rate - start_bitrate) / (end_bitrate - start_bitrate));
136
337
        bandwidth = (WORD32)(bandwidth_fac * (end_bandwidth - start_bandwidth) + start_bandwidth);
137
337
        break;
138
337
      }
139
969
    }
140
2.63k
  }
141
1.12k
  return bandwidth;
142
1.12k
}
143
144
static VOID ixheaace_determine_bandwidth(const WORD32 proposed_bandwidth, const WORD32 bitrate,
145
                                         const WORD32 sample_rate, const WORD32 channels,
146
2.96k
                                         WORD32 *const bandwidth, WORD32 aot) {
147
2.96k
  WORD32 channel_bit_rate = bitrate / channels;
148
2.96k
  if (proposed_bandwidth == 0) {
149
1.12k
    *bandwidth = ixheaace_calculate_bandwidth(sample_rate, channel_bit_rate, channels, aot);
150
1.83k
  } else {
151
1.83k
    *bandwidth = MIN(proposed_bandwidth, MIN(20000, sample_rate >> 1));
152
1.83k
  }
153
2.96k
  *bandwidth = MIN(*bandwidth, sample_rate / 2);
154
2.96k
}
155
156
10.3k
WORD32 ia_enhaacplus_enc_aac_enc_pers_size(WORD32 num_aac_chan, WORD32 aot) {
157
10.3k
  WORD32 num_bytes;
158
10.3k
  num_bytes = IXHEAAC_GET_SIZE_ALIGNED(sizeof(iexheaac_encoder_str), BYTE_ALIGN_8);
159
10.3k
  num_bytes += (num_aac_chan *
160
10.3k
    IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_out_channel), BYTE_ALIGN_8));
161
10.3k
  num_bytes += (num_aac_chan *
162
10.3k
    IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_data), BYTE_ALIGN_8));
163
10.3k
  num_bytes += (num_aac_chan *
164
10.3k
    IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_temporal_noise_shaping_data), BYTE_ALIGN_8));
165
10.3k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
166
6.54k
    num_bytes += (num_aac_chan *
167
6.54k
      IXHEAAC_GET_SIZE_ALIGNED(BLK_SWITCH_OFFSET_LC_128 * sizeof(FLOAT32), BYTE_ALIGN_8));
168
6.54k
  } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
169
3.77k
    num_bytes += (num_aac_chan *
170
3.77k
      IXHEAAC_GET_SIZE_ALIGNED(BLK_SWITCH_OFFSET_LD * sizeof(FLOAT32), BYTE_ALIGN_8));
171
3.77k
  }
172
173
10.3k
  num_bytes += (num_aac_chan *
174
10.3k
    IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_qc_out_channel), BYTE_ALIGN_8));
175
10.3k
  return num_bytes;
176
10.3k
}
177
178
338k
WORD32 ia_enhaacplus_enc_aac_enc_scr_size(VOID) {
179
338k
  return IXHEAAC_GET_SIZE_ALIGNED(sizeof(iaace_scratch), BYTE_ALIGN_8);
180
338k
}
181
182
VOID ia_enhaacplus_enc_set_shared_bufs(iaace_scratch *scr, WORD32 **shared_buf1,
183
                                       WORD32 **shared_buf2, WORD32 **shared_buf3,
184
5.11k
                                       WORD8 **shared_buf5) {
185
5.11k
  iaace_scratch *pstr_aac_enc_scratch = scr;
186
  /* Fill addresses of shared buffers */
187
5.11k
  pstr_aac_enc_scratch->shared_buffer1 = *shared_buf1;
188
5.11k
  pstr_aac_enc_scratch->shared_buffer_2 = *shared_buf2;
189
5.11k
  pstr_aac_enc_scratch->shared_buffer3 = *shared_buf3;
190
5.11k
  pstr_aac_enc_scratch->shared_buffer5 = (WORD8 *)*shared_buf5;
191
5.11k
}
192
193
57.7k
VOID ia_enhaacplus_enc_aac_init_default_config(iaace_config *config, WORD32 aot) {
194
57.7k
  memset(config, 0, sizeof(iaace_config));
195
196
  /* default configurations */
197
57.7k
  config->bit_rate = AAC_BITRATE_DEFAULT_VALUE;
198
57.7k
  config->band_width = 0;
199
57.7k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
200
13.3k
    config->inv_quant = 0;
201
44.4k
  } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
202
9.91k
    config->inv_quant = 2;
203
9.91k
  }
204
57.7k
  config->bitreservoir_size = BITRESERVOIR_SIZE_CONFIG_PARAM_DEFAULT_VALUE;
205
57.7k
  config->use_tns = 0;
206
57.7k
  config->flag_framelength_small =
207
57.7k
      USE_FRAMELENGTH_SMALL_PARAM_DEFAULT_VALUE;  // assume framelength large
208
57.7k
}
209
210
static VOID ia_enhaacplus_enc_aac_set_scratch_ptr(iexheaac_encoder_str *pstr_exheaac_encoder,
211
5.11k
                                                  iaace_scratch *pstr_scr) {
212
5.11k
  pstr_exheaac_encoder->pstr_aac_scratch = pstr_scr;
213
5.11k
}
214
215
7.22k
VOID ia_enhaacplus_enc_init_aac_tabs(ixheaace_aac_tables *pstr_aac_tabs) {
216
7.22k
  pstr_aac_tabs->pstr_mdct_tab = (ixheaace_mdct_tables *)&ixheaace_enc_mdct_tab;
217
7.22k
  pstr_aac_tabs->pstr_huff_tab = (ixheaace_huffman_tables *)&ixheaace_enc_huff_tab;
218
7.22k
  pstr_aac_tabs->pstr_psycho_tab = (ixheaace_psycho_tables *)&ixheaace_enc_psycho_tab;
219
7.22k
  pstr_aac_tabs->pstr_quant_tab = (ixheaace_quant_tables *)&ixheaace_enc_quant_tab;
220
7.22k
  pstr_aac_tabs->pstr_tns_tab =
221
7.22k
      (ixheaace_temporal_noise_shaping_tables *)&ixheaace_enhaacplus_enc_tns_tab;
222
7.22k
}
223
224
5.11k
static VOID ia_enhaacplus_enc_aac_set_persist_buf(WORD8 *ptr_base, WORD32 num_chan, WORD32 aot) {
225
5.11k
  iexheaac_encoder_str *pstr_exheaac_encoder;
226
5.11k
  WORD8 *ptr_curr_mem = ptr_base +
227
5.11k
    IXHEAAC_GET_SIZE_ALIGNED(sizeof(iexheaac_encoder_str), BYTE_ALIGN_8);
228
5.11k
  WORD32 i;
229
230
5.11k
  pstr_exheaac_encoder = (iexheaac_encoder_str *)ptr_base;
231
232
12.5k
  for (i = 0; i < num_chan; i++) {
233
7.47k
    pstr_exheaac_encoder->psy_out.psy_out_ch[i] = (ixheaace_psy_out_channel *)(ptr_curr_mem);
234
7.47k
    ptr_curr_mem = ptr_curr_mem +
235
7.47k
      IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_out_channel), BYTE_ALIGN_8);
236
7.47k
  }
237
238
12.5k
  for (i = 0; i < num_chan; i++) {
239
7.47k
    pstr_exheaac_encoder->psy_kernel.psy_data[i] = (ixheaace_psy_data *)(ptr_curr_mem);
240
7.47k
    ptr_curr_mem = ptr_curr_mem +
241
7.47k
      IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_data), BYTE_ALIGN_8);
242
7.47k
  }
243
244
12.5k
  for (i = 0; i < num_chan; i++) {
245
7.47k
    pstr_exheaac_encoder->psy_kernel.temporal_noise_shaping_data[i] =
246
7.47k
        (ixheaace_temporal_noise_shaping_data *)(ptr_curr_mem);
247
7.47k
    ptr_curr_mem = ptr_curr_mem +
248
7.47k
      IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_temporal_noise_shaping_data), BYTE_ALIGN_8);
249
7.47k
  }
250
251
12.5k
  for (i = 0; i < num_chan; i++) {
252
7.47k
    switch (aot) {
253
1.73k
      case AOT_AAC_LC:
254
4.49k
      case AOT_SBR:
255
4.76k
      case AOT_PS:
256
4.76k
        pstr_exheaac_encoder->psy_kernel.psy_data[i]->ptr_mdct_delay_buf =
257
4.76k
            (FLOAT32 *)(ptr_curr_mem);
258
4.76k
        ptr_curr_mem = ptr_curr_mem +
259
4.76k
          IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * BLK_SWITCH_OFFSET_LC_128, BYTE_ALIGN_8);
260
4.76k
        break;
261
262
727
      case AOT_AAC_LD:
263
2.71k
      case AOT_AAC_ELD:
264
2.71k
        pstr_exheaac_encoder->psy_kernel.psy_data[i]->ptr_mdct_delay_buf =
265
2.71k
            (FLOAT32 *)(ptr_curr_mem);
266
2.71k
        ptr_curr_mem = ptr_curr_mem +
267
2.71k
          IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * BLK_SWITCH_OFFSET_LD, BYTE_ALIGN_8);
268
2.71k
        break;
269
7.47k
    }
270
7.47k
  }
271
272
12.5k
  for (i = 0; i < num_chan; i++) {
273
7.47k
    pstr_exheaac_encoder->qc_out.qc_channel[i] = (ixheaace_qc_out_channel *)(ptr_curr_mem);
274
7.47k
    ptr_curr_mem = ptr_curr_mem +
275
7.47k
      IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_qc_out_channel), BYTE_ALIGN_8);
276
7.47k
  }
277
5.11k
}
278
279
IA_ERRORCODE ia_enhaacplus_enc_aac_enc_open(iexheaac_encoder_str **ppstr_exheaac_encoder,
280
                                            const iaace_config config,
281
                                            iaace_scratch *pstr_aac_scratch,
282
                                            ixheaace_aac_tables *pstr_aac_tabs, WORD32 ele_type,
283
5.11k
                                            WORD32 element_instance_tag, WORD32 aot) {
284
5.11k
  IA_ERRORCODE error = IA_NO_ERROR;
285
5.11k
  WORD32 profile = 1;
286
5.11k
  ixheaace_element_info *pstr_element_info = NULL;
287
5.11k
  iexheaac_encoder_str *pstr_exheaac_encoder;
288
5.11k
  WORD32 frame_len_long = FRAME_LEN_1024;
289
5.11k
  switch (aot) {
290
1.15k
    case AOT_AAC_LC:
291
2.98k
    case AOT_SBR:
292
3.25k
    case AOT_PS:
293
3.25k
      if (config.flag_framelength_small) {
294
1.13k
        frame_len_long = FRAME_LEN_960;
295
2.11k
      } else {
296
2.11k
        frame_len_long = FRAME_LEN_1024;
297
2.11k
      }
298
3.25k
      break;
299
300
504
    case AOT_AAC_LD:
301
1.86k
    case AOT_AAC_ELD:
302
1.86k
      if (config.flag_framelength_small) {
303
866
        frame_len_long = FRAME_LEN_480;
304
1.00k
      } else {
305
1.00k
        frame_len_long = FRAME_LEN_512;
306
1.00k
      }
307
1.86k
      break;
308
5.11k
  }
309
310
5.11k
  if ((config.num_in_channels < 1) || (config.num_out_channels > IXHEAACE_MAX_CH_IN_BS_ELE) ||
311
5.11k
    (config.num_out_channels < 1) || (config.num_in_channels < config.num_out_channels)) {
312
0
    return IA_EXHEAACE_INIT_FATAL_INVALID_NUM_CHANNELS_IN_ELE;
313
0
  }
314
5.11k
  if ((config.bit_rate != 0) && ((config.bit_rate / config.num_out_channels < 8000) ||
315
5.11k
    (config.bit_rate / config.num_out_channels > 576000))) {
316
0
    error = IA_EXHEAACE_INIT_FATAL_BITRATE_NOT_SUPPORTED;
317
0
  }
318
5.11k
  if (error != IA_NO_ERROR) {
319
0
    return error;
320
0
  }
321
322
5.11k
  pstr_exheaac_encoder = *ppstr_exheaac_encoder;
323
324
5.11k
  memset(pstr_exheaac_encoder, 0, sizeof(iexheaac_encoder_str));
325
326
5.11k
  ia_enhaacplus_enc_aac_set_scratch_ptr(pstr_exheaac_encoder, pstr_aac_scratch);
327
328
5.11k
  ia_enhaacplus_enc_aac_set_persist_buf((WORD8 *)pstr_exheaac_encoder, config.num_out_channels,
329
5.11k
                                        aot);
330
331
  /* check sample rate */
332
333
5.11k
  switch (config.core_sample_rate) {
334
181
    case 8000:
335
293
    case 11025:
336
498
    case 12000:
337
1.79k
    case 16000:
338
2.95k
    case 22050:
339
4.27k
    case 24000:
340
4.37k
    case 32000:
341
4.46k
    case 44100:
342
4.60k
    case 48000:
343
4.72k
    case 64000:
344
4.98k
    case 88200:
345
5.11k
    case 96000:
346
5.11k
      break;
347
348
0
    default:
349
0
      return IA_EXHEAACE_INIT_FATAL_INVALID_CORE_SAMPLE_RATE;
350
0
      break;
351
5.11k
  }
352
353
5.11k
  pstr_exheaac_encoder->config = config;
354
355
5.11k
  error = ia_enhaacplus_enc_init_element_info(config.num_out_channels,
356
5.11k
                                              &pstr_exheaac_encoder->element_info, ele_type,
357
5.11k
                                              element_instance_tag);
358
5.11k
  if (error != IA_NO_ERROR) {
359
0
    return error;
360
0
  }
361
362
5.11k
  pstr_element_info = &pstr_exheaac_encoder->element_info;
363
364
  /* allocate the Psy aud Psy Out structure */
365
366
5.11k
  error = (ia_enhaacplus_enc_psy_new(
367
5.11k
      &pstr_exheaac_encoder->psy_kernel, pstr_element_info->n_channels_in_el,
368
5.11k
      pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer_2, frame_len_long));
369
370
5.11k
  if (error != IA_NO_ERROR) {
371
0
    return error;
372
0
  }
373
374
5.11k
  WORD32 tns_mask = config.use_tns;
375
5.11k
  if (config.full_bandwidth) {
376
2.15k
    pstr_exheaac_encoder->config.band_width = config.core_sample_rate >> 2;
377
2.96k
  } else {
378
2.96k
    ixheaace_determine_bandwidth(pstr_exheaac_encoder->config.band_width, config.bit_rate,
379
2.96k
                                 config.core_sample_rate, pstr_element_info->n_channels_in_el,
380
2.96k
                                 &pstr_exheaac_encoder->config.band_width, aot);
381
2.96k
  }
382
5.11k
  pstr_exheaac_encoder->bandwidth_90_dB = (WORD32)pstr_exheaac_encoder->config.band_width;
383
5.11k
  if (ele_type == ID_LFE) {
384
455
    tns_mask = 0;
385
455
  }
386
387
5.11k
  error = ia_enhaacplus_enc_psy_main_init(
388
5.11k
      &pstr_exheaac_encoder->psy_kernel, config.core_sample_rate, config.bit_rate,
389
5.11k
      pstr_element_info->n_channels_in_el, tns_mask, pstr_exheaac_encoder->bandwidth_90_dB, aot,
390
5.11k
      pstr_aac_tabs, frame_len_long);
391
5.11k
  if (error != IA_NO_ERROR) {
392
0
    return error;
393
0
  }
394
395
  /* allocate the Q&C Out structure */
396
5.11k
  error = ia_enhaacplus_enc_qc_out_new(
397
5.11k
      &pstr_exheaac_encoder->qc_out, pstr_element_info->n_channels_in_el,
398
5.11k
      pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer1,
399
5.11k
      pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer3, frame_len_long);
400
401
5.11k
  if (error != IA_NO_ERROR) {
402
0
    return error;
403
0
  }
404
405
  /* allocate the Q&C kernel */
406
5.11k
  error = ia_enhaacplus_enc_qc_new(&pstr_exheaac_encoder->qc_kernel,
407
5.11k
                                   pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer_2,
408
5.11k
                                   frame_len_long);
409
5.11k
  if (error != IA_NO_ERROR) {
410
0
    return error;
411
0
  }
412
413
5.11k
  ixheaace_qc_init qc_init;
414
415
5.11k
  qc_init.pstr_element_info = &pstr_exheaac_encoder->element_info;
416
417
5.11k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
418
3.25k
    if (config.flag_framelength_small) {
419
1.13k
      qc_init.max_bits = MAXIMUM_CHANNEL_BITS_960 * pstr_element_info->n_channels_in_el;
420
2.11k
    } else {
421
2.11k
      qc_init.max_bits = MAXIMUM_CHANNEL_BITS_1024 * pstr_element_info->n_channels_in_el;
422
2.11k
    }
423
424
3.25k
    qc_init.bit_res = qc_init.max_bits;
425
3.25k
  }
426
427
5.11k
  qc_init.average_bits = (config.bit_rate * frame_len_long) / config.core_sample_rate;
428
429
5.11k
  if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
430
1.86k
    if (pstr_exheaac_encoder->config.bitreservoir_size != -1) {
431
1.76k
      qc_init.max_bits = (pstr_exheaac_encoder->config.bitreservoir_size * 8) *
432
1.76k
                         pstr_element_info->n_channels_in_el;
433
1.76k
      if (qc_init.max_bits > qc_init.average_bits) {
434
1.27k
        qc_init.bit_res = (pstr_exheaac_encoder->config.bitreservoir_size * 8) *
435
1.27k
                          pstr_element_info->n_channels_in_el;
436
1.27k
      } else {
437
486
        qc_init.max_bits = qc_init.average_bits;
438
486
        qc_init.bit_res = 0;
439
486
      }
440
1.76k
    } else {
441
107
      qc_init.max_bits = qc_init.average_bits;
442
107
      qc_init.bit_res = 0;
443
107
    }
444
1.86k
  }
445
446
5.11k
  qc_init.padding.padding_rest = config.core_sample_rate;
447
448
5.11k
  qc_init.mean_pe = ((FLOAT32)10 * frame_len_long * pstr_exheaac_encoder->bandwidth_90_dB * 2) /
449
5.11k
                    config.core_sample_rate;
450
451
5.11k
  switch (aot) {
452
1.15k
    case AOT_AAC_LC:
453
2.98k
    case AOT_SBR:
454
3.25k
    case AOT_PS:
455
3.25k
      if (config.flag_framelength_small) {
456
1.13k
        qc_init.max_bit_fac =
457
1.13k
            (float)(MAXIMUM_CHANNEL_BITS_960 * pstr_element_info->n_channels_in_el) /
458
1.13k
            (float)(qc_init.average_bits ? qc_init.average_bits : 1);
459
2.11k
      } else {
460
2.11k
        qc_init.max_bit_fac =
461
2.11k
            (float)(MAXIMUM_CHANNEL_BITS_1024 * pstr_element_info->n_channels_in_el) /
462
2.11k
            (float)(qc_init.average_bits ? qc_init.average_bits : 1);
463
2.11k
      }
464
3.25k
      break;
465
466
504
    case AOT_AAC_LD:
467
1.86k
    case AOT_AAC_ELD:
468
1.86k
      if (config.flag_framelength_small) {
469
866
        qc_init.max_bit_fac = (FLOAT32)((MAXIMUM_CHANNEL_BITS_480)*pstr_element_info
470
866
                                            ->n_channels_in_el);  // no anc data in aacld
471
1.00k
      } else {
472
1.00k
        qc_init.max_bit_fac = (FLOAT32)((MAXIMUM_CHANNEL_BITS_512)*pstr_element_info
473
1.00k
                                            ->n_channels_in_el);  // no anc data in aacld
474
1.00k
      }
475
1.86k
      qc_init.max_bit_fac =
476
1.86k
          qc_init.max_bit_fac / (qc_init.average_bits ? qc_init.average_bits : 1);
477
1.86k
      break;
478
5.11k
  }
479
480
5.11k
  qc_init.bitrate = config.bit_rate;
481
5.11k
  qc_init.inv_quant = config.inv_quant;
482
483
5.11k
  error = ia_enhaacplus_enc_qc_init(&pstr_exheaac_encoder->qc_kernel, aot, &qc_init,
484
5.11k
                                    config.flag_framelength_small);
485
5.11k
  if (error != IA_NO_ERROR) {
486
0
    return error;
487
0
  }
488
489
  /* init bitstream encoder */
490
5.11k
  pstr_exheaac_encoder->bse_init.num_channels = pstr_element_info->n_channels_in_el;
491
5.11k
  pstr_exheaac_encoder->bse_init.bitrate = config.bit_rate;
492
5.11k
  pstr_exheaac_encoder->bse_init.sample_rate = config.core_sample_rate;
493
5.11k
  pstr_exheaac_encoder->bse_init.profile = profile;
494
495
5.11k
  if (config.num_in_channels > config.num_out_channels) {
496
860
    pstr_exheaac_encoder->downmix = 1;
497
860
    pstr_exheaac_encoder->downmix_fac = config.num_in_channels / config.num_out_channels;
498
860
  }
499
500
5.11k
  if (pstr_element_info->el_type == ID_CPE &&
501
2.36k
      (config.core_sample_rate <= 24000 &&
502
1.93k
       (config.bit_rate / pstr_element_info->n_channels_in_el * 2) < 60000)) {
503
1.36k
    FLOAT32 scf_used_ratio = (FLOAT32)pstr_exheaac_encoder->psy_kernel.psy_conf_long.sfb_active /
504
1.36k
                             pstr_exheaac_encoder->psy_kernel.psy_conf_long.sfb_cnt;
505
506
1.36k
    error = iaace_init_stereo_pre_processing(&(pstr_exheaac_encoder->str_stereo_pre_pro),
507
1.36k
                                             pstr_element_info->n_channels_in_el, config.bit_rate,
508
1.36k
                                             config.core_sample_rate, scf_used_ratio);
509
1.36k
  }
510
511
5.11k
  if (error != IA_NO_ERROR) {
512
0
    return error;
513
0
  }
514
515
5.11k
  *ppstr_exheaac_encoder = pstr_exheaac_encoder;
516
517
5.11k
  return IA_NO_ERROR;
518
5.11k
}