Coverage Report

Created: 2024-06-21 06:45

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