Coverage Report

Created: 2026-01-09 06:51

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