Coverage Report

Created: 2025-10-10 06:12

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
2.06k
                                           WORD32 aot) {
62
2.06k
  WORD32 bandwidth = -1;
63
2.06k
  const ixheaace_bandwidth_table *pstr_bandwidth_table = NULL;
64
2.06k
  WORD32 bandwidth_table_size = 0;
65
2.06k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
66
1.41k
    pstr_bandwidth_table = bandwidth_table_lc;
67
1.41k
    bandwidth_table_size = sizeof(bandwidth_table_lc) / sizeof(ixheaace_bandwidth_table);
68
1.41k
  } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
69
649
    switch (sample_rate) {
70
24
      case 48000:
71
63
      case 64000:
72
85
      case 88200:
73
113
      case 96000:
74
113
        pstr_bandwidth_table = bandwidth_table_ld_48000;
75
113
        bandwidth_table_size =
76
113
            sizeof(bandwidth_table_ld_48000) / sizeof(ixheaace_bandwidth_table);
77
113
        break;
78
35
      case 44100:
79
35
        pstr_bandwidth_table = bandwidth_table_ld_44100;
80
35
        bandwidth_table_size =
81
35
            sizeof(bandwidth_table_ld_44100) / sizeof(ixheaace_bandwidth_table);
82
35
        break;
83
34
      case 32000:
84
34
        pstr_bandwidth_table = bandwidth_table_ld_32000;
85
34
        bandwidth_table_size =
86
34
            sizeof(bandwidth_table_ld_32000) / sizeof(ixheaace_bandwidth_table);
87
34
        break;
88
158
      case 24000:
89
158
        pstr_bandwidth_table = bandwidth_table_ld_24000;
90
158
        bandwidth_table_size =
91
158
            sizeof(bandwidth_table_ld_24000) / sizeof(ixheaace_bandwidth_table);
92
158
        break;
93
56
      case 8000:
94
107
      case 11025:
95
154
      case 12000:
96
256
      case 16000:
97
309
      case 22050:
98
309
        pstr_bandwidth_table = bandwidth_table_ld_22050;
99
309
        bandwidth_table_size =
100
309
            sizeof(bandwidth_table_ld_22050) / sizeof(ixheaace_bandwidth_table);
101
309
        break;
102
649
    }
103
649
  }
104
4.49k
  for (WORD32 i = 0; i < bandwidth_table_size - 1; i++) {
105
4.49k
    if (channel_bit_rate >= 96000) {
106
314
      if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
107
206
        bandwidth = 20000;
108
206
      } else {
109
108
        if (num_ch == 1) {
110
40
          bandwidth = 19000;
111
68
        } else {
112
68
          bandwidth = 22000;
113
68
        }
114
108
      }
115
314
      break;
116
4.18k
    } else if (channel_bit_rate >= pstr_bandwidth_table[i].channel_bit_rate &&
117
4.18k
               channel_bit_rate < pstr_bandwidth_table[i + 1].channel_bit_rate) {
118
1.75k
      if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
119
1.21k
        bandwidth = (num_ch == 1) ? pstr_bandwidth_table[i].bandwidth_mono
120
1.21k
                                  : pstr_bandwidth_table[i].bandwidth_stereo;
121
1.21k
        bandwidth = bandwidth - (pstr_bandwidth_table[i].channel_bit_rate / 32);
122
1.21k
        break;
123
1.21k
      } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
124
541
        WORD32 start_bandwidth, end_bandwidth, start_bitrate, end_bitrate;
125
541
        FLOAT32 bandwidth_fac;
126
541
        start_bandwidth = (num_ch == 1) ? pstr_bandwidth_table[i].bandwidth_mono
127
541
                                        : pstr_bandwidth_table[i].bandwidth_stereo;
128
541
        start_bandwidth = start_bandwidth - (pstr_bandwidth_table[i].channel_bit_rate / 32);
129
541
        end_bandwidth = (num_ch == 1) ? pstr_bandwidth_table[i + 1].bandwidth_mono
130
541
                                      : pstr_bandwidth_table[i + 1].bandwidth_stereo;
131
541
        end_bandwidth = end_bandwidth - (pstr_bandwidth_table[i + 1].channel_bit_rate / 32);
132
541
        start_bitrate = pstr_bandwidth_table[i].channel_bit_rate;
133
541
        end_bitrate = pstr_bandwidth_table[i + 1].channel_bit_rate;
134
541
        bandwidth_fac =
135
541
            (FLOAT32)((channel_bit_rate - start_bitrate) / (end_bitrate - start_bitrate));
136
541
        bandwidth = (WORD32)(bandwidth_fac * (end_bandwidth - start_bandwidth) + start_bandwidth);
137
541
        break;
138
541
      }
139
1.75k
    }
140
4.49k
  }
141
2.06k
  return bandwidth;
142
2.06k
}
143
144
static VOID ixheaace_determine_bandwidth(const WORD32 proposed_bandwidth, const WORD32 bitrate,
145
                                         const WORD32 sample_rate, const WORD32 channels,
146
5.00k
                                         WORD32 *const bandwidth, WORD32 aot) {
147
5.00k
  WORD32 channel_bit_rate = bitrate / channels;
148
5.00k
  if (proposed_bandwidth == 0) {
149
2.06k
    *bandwidth = ixheaace_calculate_bandwidth(sample_rate, channel_bit_rate, channels, aot);
150
2.93k
  } else {
151
2.93k
    *bandwidth = MIN(proposed_bandwidth, MIN(20000, sample_rate >> 1));
152
2.93k
  }
153
5.00k
  *bandwidth = MIN(*bandwidth, sample_rate / 2);
154
5.00k
}
155
156
12.7k
WORD32 ia_enhaacplus_enc_aac_enc_pers_size(WORD32 num_aac_chan, WORD32 aot) {
157
12.7k
  WORD32 num_bytes;
158
12.7k
  num_bytes = IXHEAAC_GET_SIZE_ALIGNED(sizeof(iexheaac_encoder_str), BYTE_ALIGN_8);
159
12.7k
  num_bytes += (num_aac_chan *
160
12.7k
    IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_out_channel), BYTE_ALIGN_8));
161
12.7k
  num_bytes += (num_aac_chan *
162
12.7k
    IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_data), BYTE_ALIGN_8));
163
12.7k
  num_bytes += (num_aac_chan *
164
12.7k
    IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_temporal_noise_shaping_data), BYTE_ALIGN_8));
165
12.7k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
166
8.20k
    num_bytes += (num_aac_chan *
167
8.20k
      IXHEAAC_GET_SIZE_ALIGNED(BLK_SWITCH_OFFSET_LC_128 * sizeof(FLOAT32), BYTE_ALIGN_8));
168
8.20k
  } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
169
4.55k
    num_bytes += (num_aac_chan *
170
4.55k
      IXHEAAC_GET_SIZE_ALIGNED(BLK_SWITCH_OFFSET_LD * sizeof(FLOAT32), BYTE_ALIGN_8));
171
4.55k
  }
172
173
12.7k
  num_bytes += (num_aac_chan *
174
12.7k
    IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_qc_out_channel), BYTE_ALIGN_8));
175
12.7k
  return num_bytes;
176
12.7k
}
177
178
347k
WORD32 ia_enhaacplus_enc_aac_enc_scr_size(VOID) {
179
347k
  return IXHEAAC_GET_SIZE_ALIGNED(sizeof(iaace_scratch), BYTE_ALIGN_8);
180
347k
}
181
182
VOID ia_enhaacplus_enc_set_shared_bufs(iaace_scratch *scr, WORD32 **shared_buf1,
183
                                       WORD32 **shared_buf2, WORD32 **shared_buf3,
184
6.32k
                                       WORD8 **shared_buf5) {
185
6.32k
  iaace_scratch *pstr_aac_enc_scratch = scr;
186
  /* Fill addresses of shared buffers */
187
6.32k
  pstr_aac_enc_scratch->shared_buffer1 = *shared_buf1;
188
6.32k
  pstr_aac_enc_scratch->shared_buffer_2 = *shared_buf2;
189
6.32k
  pstr_aac_enc_scratch->shared_buffer3 = *shared_buf3;
190
6.32k
  pstr_aac_enc_scratch->shared_buffer5 = (WORD8 *)*shared_buf5;
191
6.32k
}
192
193
65.5k
VOID ia_enhaacplus_enc_aac_init_default_config(iaace_config *config, WORD32 aot) {
194
65.5k
  memset(config, 0, sizeof(iaace_config));
195
196
  /* default configurations */
197
65.5k
  config->bit_rate = AAC_BITRATE_DEFAULT_VALUE;
198
65.5k
  config->band_width = 0;
199
65.5k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
200
15.8k
    config->inv_quant = 0;
201
15.8k
    config->bitreservoir_size = BITRESERVOIR_SIZE_CONFIG_PARAM_DEFAULT_VALUE_LC;
202
49.6k
  } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
203
11.4k
    config->inv_quant = 2;
204
11.4k
    config->bitreservoir_size = BITRESERVOIR_SIZE_CONFIG_PARAM_DEFAULT_VALUE_LD;
205
11.4k
  }
206
65.5k
  config->use_tns = 0;
207
65.5k
  config->flag_framelength_small =
208
65.5k
      USE_FRAMELENGTH_SMALL_PARAM_DEFAULT_VALUE;  // assume framelength large
209
65.5k
}
210
211
static VOID ia_enhaacplus_enc_aac_set_scratch_ptr(iexheaac_encoder_str *pstr_exheaac_encoder,
212
6.32k
                                                  iaace_scratch *pstr_scr) {
213
6.32k
  pstr_exheaac_encoder->pstr_aac_scratch = pstr_scr;
214
6.32k
}
215
216
8.19k
VOID ia_enhaacplus_enc_init_aac_tabs(ixheaace_aac_tables *pstr_aac_tabs) {
217
8.19k
  pstr_aac_tabs->pstr_mdct_tab = (ixheaace_mdct_tables *)&ixheaace_enc_mdct_tab;
218
8.19k
  pstr_aac_tabs->pstr_huff_tab = (ixheaace_huffman_tables *)&ixheaace_enc_huff_tab;
219
8.19k
  pstr_aac_tabs->pstr_psycho_tab = (ixheaace_psycho_tables *)&ixheaace_enc_psycho_tab;
220
8.19k
  pstr_aac_tabs->pstr_quant_tab = (ixheaace_quant_tables *)&ixheaace_enc_quant_tab;
221
8.19k
  pstr_aac_tabs->pstr_tns_tab =
222
8.19k
      (ixheaace_temporal_noise_shaping_tables *)&ixheaace_enhaacplus_enc_tns_tab;
223
8.19k
}
224
225
6.32k
static VOID ia_enhaacplus_enc_aac_set_persist_buf(WORD8 *ptr_base, WORD32 num_chan, WORD32 aot) {
226
6.32k
  iexheaac_encoder_str *pstr_exheaac_encoder;
227
6.32k
  WORD8 *ptr_curr_mem = ptr_base +
228
6.32k
    IXHEAAC_GET_SIZE_ALIGNED(sizeof(iexheaac_encoder_str), BYTE_ALIGN_8);
229
6.32k
  WORD32 i;
230
231
6.32k
  pstr_exheaac_encoder = (iexheaac_encoder_str *)ptr_base;
232
233
15.5k
  for (i = 0; i < num_chan; i++) {
234
9.20k
    pstr_exheaac_encoder->psy_out.psy_out_ch[i] = (ixheaace_psy_out_channel *)(ptr_curr_mem);
235
9.20k
    ptr_curr_mem = ptr_curr_mem +
236
9.20k
      IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_out_channel), BYTE_ALIGN_8);
237
9.20k
  }
238
239
15.5k
  for (i = 0; i < num_chan; i++) {
240
9.20k
    pstr_exheaac_encoder->psy_kernel.psy_data[i] = (ixheaace_psy_data *)(ptr_curr_mem);
241
9.20k
    ptr_curr_mem = ptr_curr_mem +
242
9.20k
      IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_data), BYTE_ALIGN_8);
243
9.20k
  }
244
245
15.5k
  for (i = 0; i < num_chan; i++) {
246
9.20k
    pstr_exheaac_encoder->psy_kernel.temporal_noise_shaping_data[i] =
247
9.20k
        (ixheaace_temporal_noise_shaping_data *)(ptr_curr_mem);
248
9.20k
    ptr_curr_mem = ptr_curr_mem +
249
9.20k
      IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_temporal_noise_shaping_data), BYTE_ALIGN_8);
250
9.20k
  }
251
252
15.5k
  for (i = 0; i < num_chan; i++) {
253
9.20k
    switch (aot) {
254
2.15k
      case AOT_AAC_LC:
255
5.65k
      case AOT_SBR:
256
5.95k
      case AOT_PS:
257
5.95k
        pstr_exheaac_encoder->psy_kernel.psy_data[i]->ptr_mdct_delay_buf =
258
5.95k
            (FLOAT32 *)(ptr_curr_mem);
259
5.95k
        ptr_curr_mem = ptr_curr_mem +
260
5.95k
          IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * BLK_SWITCH_OFFSET_LC_128, BYTE_ALIGN_8);
261
5.95k
        break;
262
263
886
      case AOT_AAC_LD:
264
3.24k
      case AOT_AAC_ELD:
265
3.24k
        pstr_exheaac_encoder->psy_kernel.psy_data[i]->ptr_mdct_delay_buf =
266
3.24k
            (FLOAT32 *)(ptr_curr_mem);
267
3.24k
        ptr_curr_mem = ptr_curr_mem +
268
3.24k
          IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * BLK_SWITCH_OFFSET_LD, BYTE_ALIGN_8);
269
3.24k
        break;
270
9.20k
    }
271
9.20k
  }
272
273
15.5k
  for (i = 0; i < num_chan; i++) {
274
9.20k
    pstr_exheaac_encoder->qc_out.qc_channel[i] = (ixheaace_qc_out_channel *)(ptr_curr_mem);
275
9.20k
    ptr_curr_mem = ptr_curr_mem +
276
9.20k
      IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_qc_out_channel), BYTE_ALIGN_8);
277
9.20k
  }
278
6.32k
}
279
280
IA_ERRORCODE ia_enhaacplus_enc_aac_enc_open(iexheaac_encoder_str **ppstr_exheaac_encoder,
281
                                            const iaace_config config,
282
                                            iaace_scratch *pstr_aac_scratch,
283
                                            ixheaace_aac_tables *pstr_aac_tabs, WORD32 ele_type,
284
6.32k
                                            WORD32 element_instance_tag, WORD32 aot) {
285
6.32k
  IA_ERRORCODE error = IA_NO_ERROR;
286
6.32k
  WORD32 profile = 1;
287
6.32k
  ixheaace_element_info *pstr_element_info = NULL;
288
6.32k
  iexheaac_encoder_str *pstr_exheaac_encoder;
289
6.32k
  WORD32 frame_len_long = FRAME_LEN_1024;
290
6.32k
  switch (aot) {
291
1.44k
    case AOT_AAC_LC:
292
3.76k
    case AOT_SBR:
293
4.07k
    case AOT_PS:
294
4.07k
      if (config.flag_framelength_small) {
295
1.28k
        frame_len_long = FRAME_LEN_960;
296
2.79k
      } else {
297
2.79k
        frame_len_long = FRAME_LEN_1024;
298
2.79k
      }
299
4.07k
      break;
300
301
612
    case AOT_AAC_LD:
302
2.25k
    case AOT_AAC_ELD:
303
2.25k
      if (config.flag_framelength_small) {
304
1.08k
        frame_len_long = FRAME_LEN_480;
305
1.16k
      } else {
306
1.16k
        frame_len_long = FRAME_LEN_512;
307
1.16k
      }
308
2.25k
      break;
309
6.32k
  }
310
311
6.32k
  if ((config.num_in_channels < 1) || (config.num_out_channels > IXHEAACE_MAX_CH_IN_BS_ELE) ||
312
6.32k
    (config.num_out_channels < 1) || (config.num_in_channels < config.num_out_channels)) {
313
0
    return IA_EXHEAACE_INIT_FATAL_INVALID_NUM_CHANNELS_IN_ELE;
314
0
  }
315
6.32k
  if ((config.bit_rate != 0) && ((config.bit_rate / config.num_out_channels < 8000) ||
316
6.32k
    (config.bit_rate / config.num_out_channels > 576000))) {
317
0
    error = IA_EXHEAACE_INIT_FATAL_BITRATE_NOT_SUPPORTED;
318
0
  }
319
6.32k
  if (error != IA_NO_ERROR) {
320
0
    return error;
321
0
  }
322
323
6.32k
  pstr_exheaac_encoder = *ppstr_exheaac_encoder;
324
325
6.32k
  memset(pstr_exheaac_encoder, 0, sizeof(iexheaac_encoder_str));
326
327
6.32k
  ia_enhaacplus_enc_aac_set_scratch_ptr(pstr_exheaac_encoder, pstr_aac_scratch);
328
329
6.32k
  ia_enhaacplus_enc_aac_set_persist_buf((WORD8 *)pstr_exheaac_encoder, config.num_out_channels,
330
6.32k
                                        aot);
331
332
  /* check sample rate */
333
334
6.32k
  switch (config.core_sample_rate) {
335
234
    case 8000:
336
427
    case 11025:
337
703
    case 12000:
338
2.29k
    case 16000:
339
3.65k
    case 22050:
340
5.40k
    case 24000:
341
5.52k
    case 32000:
342
5.66k
    case 44100:
343
5.79k
    case 48000:
344
5.90k
    case 64000:
345
6.19k
    case 88200:
346
6.32k
    case 96000:
347
6.32k
      break;
348
349
0
    default:
350
0
      return IA_EXHEAACE_INIT_FATAL_INVALID_CORE_SAMPLE_RATE;
351
0
      break;
352
6.32k
  }
353
354
6.32k
  pstr_exheaac_encoder->config = config;
355
356
6.32k
  error = ia_enhaacplus_enc_init_element_info(config.num_out_channels,
357
6.32k
                                              &pstr_exheaac_encoder->element_info, ele_type,
358
6.32k
                                              element_instance_tag);
359
6.32k
  if (error != IA_NO_ERROR) {
360
0
    return error;
361
0
  }
362
363
6.32k
  pstr_element_info = &pstr_exheaac_encoder->element_info;
364
365
  /* allocate the Psy aud Psy Out structure */
366
367
6.32k
  error = (ia_enhaacplus_enc_psy_new(
368
6.32k
      &pstr_exheaac_encoder->psy_kernel, pstr_element_info->n_channels_in_el,
369
6.32k
      pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer_2, frame_len_long));
370
371
6.32k
  if (error != IA_NO_ERROR) {
372
0
    return error;
373
0
  }
374
375
6.32k
  WORD32 tns_mask = config.use_tns;
376
6.32k
  if (config.full_bandwidth) {
377
1.32k
    pstr_exheaac_encoder->config.band_width = config.core_sample_rate >> 2;
378
5.00k
  } else {
379
5.00k
    ixheaace_determine_bandwidth(pstr_exheaac_encoder->config.band_width, config.bit_rate,
380
5.00k
                                 config.core_sample_rate, pstr_element_info->n_channels_in_el,
381
5.00k
                                 &pstr_exheaac_encoder->config.band_width, aot);
382
5.00k
  }
383
6.32k
  pstr_exheaac_encoder->bandwidth_90_dB = (WORD32)pstr_exheaac_encoder->config.band_width;
384
6.32k
  if (ele_type == ID_LFE) {
385
603
    tns_mask = 0;
386
603
  }
387
388
6.32k
  error = ia_enhaacplus_enc_psy_main_init(
389
6.32k
      &pstr_exheaac_encoder->psy_kernel, config.core_sample_rate, config.bit_rate,
390
6.32k
      pstr_element_info->n_channels_in_el, tns_mask, pstr_exheaac_encoder->bandwidth_90_dB, aot,
391
6.32k
      pstr_aac_tabs, frame_len_long);
392
6.32k
  if (error != IA_NO_ERROR) {
393
0
    return error;
394
0
  }
395
396
  /* allocate the Q&C Out structure */
397
6.32k
  error = ia_enhaacplus_enc_qc_out_new(
398
6.32k
      &pstr_exheaac_encoder->qc_out, pstr_element_info->n_channels_in_el,
399
6.32k
      pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer1,
400
6.32k
      pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer3, frame_len_long);
401
402
6.32k
  if (error != IA_NO_ERROR) {
403
0
    return error;
404
0
  }
405
406
  /* allocate the Q&C kernel */
407
6.32k
  error = ia_enhaacplus_enc_qc_new(&pstr_exheaac_encoder->qc_kernel,
408
6.32k
                                   pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer_2,
409
6.32k
                                   frame_len_long);
410
6.32k
  if (error != IA_NO_ERROR) {
411
0
    return error;
412
0
  }
413
414
6.32k
  ixheaace_qc_init qc_init;
415
416
6.32k
  qc_init.pstr_element_info = &pstr_exheaac_encoder->element_info;
417
418
6.32k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
419
4.07k
    if (config.flag_framelength_small) {
420
1.28k
      qc_init.max_bits = MAXIMUM_CHANNEL_BITS_960 * pstr_element_info->n_channels_in_el;
421
2.79k
    } else {
422
2.79k
      qc_init.max_bits = MAXIMUM_CHANNEL_BITS_1024 * pstr_element_info->n_channels_in_el;
423
2.79k
    }
424
425
4.07k
    qc_init.bit_res = qc_init.max_bits;
426
4.07k
  }
427
428
6.32k
  qc_init.average_bits = (config.bit_rate * frame_len_long) / config.core_sample_rate;
429
430
6.32k
  if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
431
2.25k
    if (pstr_exheaac_encoder->config.bitreservoir_size != -1) {
432
2.20k
      qc_init.max_bits = (pstr_exheaac_encoder->config.bitreservoir_size * 8) *
433
2.20k
                         pstr_element_info->n_channels_in_el;
434
2.20k
      if (qc_init.max_bits > qc_init.average_bits) {
435
1.71k
        qc_init.bit_res = (pstr_exheaac_encoder->config.bitreservoir_size * 8) *
436
1.71k
                          pstr_element_info->n_channels_in_el;
437
1.71k
      } else {
438
488
        qc_init.max_bits = qc_init.average_bits;
439
488
        qc_init.bit_res = 0;
440
488
      }
441
2.20k
    } else {
442
56
      qc_init.max_bits = qc_init.average_bits;
443
56
      qc_init.bit_res = 0;
444
56
    }
445
2.25k
  }
446
447
6.32k
  qc_init.padding.padding_rest = config.core_sample_rate;
448
449
6.32k
  qc_init.mean_pe = ((FLOAT32)10 * frame_len_long * pstr_exheaac_encoder->bandwidth_90_dB * 2) /
450
6.32k
                    config.core_sample_rate;
451
452
6.32k
  switch (aot) {
453
1.44k
    case AOT_AAC_LC:
454
3.76k
    case AOT_SBR:
455
4.07k
    case AOT_PS:
456
4.07k
      if (config.flag_framelength_small) {
457
1.28k
        qc_init.max_bit_fac =
458
1.28k
            (float)(MAXIMUM_CHANNEL_BITS_960 * pstr_element_info->n_channels_in_el) /
459
1.28k
            (float)(qc_init.average_bits ? qc_init.average_bits : 1);
460
2.79k
      } else {
461
2.79k
        qc_init.max_bit_fac =
462
2.79k
            (float)(MAXIMUM_CHANNEL_BITS_1024 * pstr_element_info->n_channels_in_el) /
463
2.79k
            (float)(qc_init.average_bits ? qc_init.average_bits : 1);
464
2.79k
      }
465
4.07k
      break;
466
467
612
    case AOT_AAC_LD:
468
2.25k
    case AOT_AAC_ELD:
469
2.25k
      if (config.flag_framelength_small) {
470
1.08k
        qc_init.max_bit_fac = (FLOAT32)((MAXIMUM_CHANNEL_BITS_480)*pstr_element_info
471
1.08k
                                            ->n_channels_in_el);  // no anc data in aacld
472
1.16k
      } else {
473
1.16k
        qc_init.max_bit_fac = (FLOAT32)((MAXIMUM_CHANNEL_BITS_512)*pstr_element_info
474
1.16k
                                            ->n_channels_in_el);  // no anc data in aacld
475
1.16k
      }
476
2.25k
      qc_init.max_bit_fac =
477
2.25k
          qc_init.max_bit_fac / (qc_init.average_bits ? qc_init.average_bits : 1);
478
2.25k
      break;
479
6.32k
  }
480
481
6.32k
  qc_init.bitrate = config.bit_rate;
482
6.32k
  qc_init.inv_quant = config.inv_quant;
483
484
6.32k
  error = ia_enhaacplus_enc_qc_init(&pstr_exheaac_encoder->qc_kernel, aot, &qc_init,
485
6.32k
                                    config.flag_framelength_small);
486
6.32k
  if (error != IA_NO_ERROR) {
487
0
    return error;
488
0
  }
489
490
  /* init bitstream encoder */
491
6.32k
  pstr_exheaac_encoder->bse_init.num_channels = pstr_element_info->n_channels_in_el;
492
6.32k
  pstr_exheaac_encoder->bse_init.bitrate = config.bit_rate;
493
6.32k
  pstr_exheaac_encoder->bse_init.sample_rate = config.core_sample_rate;
494
6.32k
  pstr_exheaac_encoder->bse_init.profile = profile;
495
496
6.32k
  if (config.num_in_channels > config.num_out_channels) {
497
971
    pstr_exheaac_encoder->downmix = 1;
498
971
    pstr_exheaac_encoder->downmix_fac = config.num_in_channels / config.num_out_channels;
499
971
  }
500
501
6.32k
  if (pstr_element_info->el_type == ID_CPE &&
502
2.87k
      (config.core_sample_rate <= 24000 &&
503
2.42k
       (config.bit_rate / pstr_element_info->n_channels_in_el * 2) < 60000)) {
504
1.71k
    FLOAT32 scf_used_ratio = (FLOAT32)pstr_exheaac_encoder->psy_kernel.psy_conf_long.sfb_active /
505
1.71k
                             pstr_exheaac_encoder->psy_kernel.psy_conf_long.sfb_cnt;
506
507
1.71k
    error = iaace_init_stereo_pre_processing(&(pstr_exheaac_encoder->str_stereo_pre_pro),
508
1.71k
                                             pstr_element_info->n_channels_in_el, config.bit_rate,
509
1.71k
                                             config.core_sample_rate, scf_used_ratio);
510
1.71k
  }
511
512
6.32k
  if (error != IA_NO_ERROR) {
513
0
    return error;
514
0
  }
515
516
6.32k
  *ppstr_exheaac_encoder = pstr_exheaac_encoder;
517
518
6.32k
  return IA_NO_ERROR;
519
6.32k
}