Coverage Report

Created: 2026-06-13 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/ixheaace_api.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 <stdlib.h>
22
#include <string.h>
23
#include <stdlib.h>
24
#include "ixheaac_type_def.h"
25
#include "ixheaac_constants.h"
26
#include "ixheaace_aac_constants.h"
27
#include "ixheaac_basic_ops32.h"
28
#include "ixheaac_basic_ops16.h"
29
#include "ixheaac_basic_ops40.h"
30
#include "ixheaac_basic_ops.h"
31
/* standard */
32
#include "ixheaac_error_standards.h"
33
34
#include "ixheaace_config_params.h"
35
36
/* library */
37
#include "ixheaace_definitions.h"
38
#include "ixheaace_error_codes.h"
39
40
#include "iusace_bitbuffer.h"
41
42
/* DRC */
43
#include "impd_drc_common_enc.h"
44
#include "impd_drc_uni_drc.h"
45
#include "impd_drc_tables.h"
46
#include "impd_drc_api.h"
47
#include "impd_drc_uni_drc_eq.h"
48
#include "impd_drc_uni_drc_filter_bank.h"
49
#include "impd_drc_gain_enc.h"
50
#include "impd_drc_struct_def.h"
51
#include "impd_drc_enc.h"
52
53
#include "ixheaace_sbr_header.h"
54
#include "ixheaace_sbr_def.h"
55
#include "ixheaace_resampler.h"
56
57
#include "ixheaace_psy_const.h"
58
#include "ixheaace_tns.h"
59
#include "ixheaace_tns_params.h"
60
#include "ixheaace_rom.h"
61
#include "ixheaace_common_rom.h"
62
#include "ixheaace_bitbuffer.h"
63
64
#include "ixheaace_sbr_rom.h"
65
#include "ixheaace_common_rom.h"
66
#include "ixheaace_sbr_main.h"
67
#include "ixheaace_definitions.h"
68
#include "ixheaace_api.h"
69
#include "ixheaace_memory_standards.h"
70
#include "iusace_block_switch_const.h"
71
#include "iusace_block_switch_struct_def.h"
72
#include "iusace_cnst.h"
73
#include "iusace_tns_usac.h"
74
#include "iusace_psy_mod.h"
75
#include "iusace_config.h"
76
#include "iusace_arith_enc.h"
77
#include "ixheaace_version_number.h"
78
79
#include "ixheaace_adjust_threshold_data.h"
80
#include "ixheaace_dynamic_bits.h"
81
#include "ixheaace_qc_data.h"
82
#include "ixheaace_channel_map.h"
83
#include "ixheaace_block_switch.h"
84
#include "ixheaace_psy_data.h"
85
#include "ixheaace_interface.h"
86
#include "ixheaace_write_bitstream.h"
87
#include "ixheaace_psy_configuration.h"
88
#include "ixheaace_common_rom.h"
89
90
#include "ixheaace_psy_mod.h"
91
#include "iusace_fd_qc_util.h"
92
#include "iusace_fd_quant.h"
93
#include "iusace_ms.h"
94
#include "iusace_signal_classifier.h"
95
#include "ixheaace_sbr_header.h"
96
97
#include "ixheaace_config.h"
98
#include "ixheaace_asc_write.h"
99
#include "iusace_main.h"
100
#include "ixheaace_stereo_preproc.h"
101
#include "ixheaace_enc_main.h"
102
#include "ixheaace_qc_util.h"
103
104
// MPS header
105
#include "ixheaace_mps_common_fix.h"
106
#include "ixheaace_mps_defines.h"
107
#include "ixheaace_mps_common_define.h"
108
109
#include "ixheaace_mps_struct_def.h"
110
#include "ixheaace_mps_sac_polyphase.h"
111
#include "ixheaace_mps_sac_hybfilter.h"
112
#include "ixheaace_mps_bitstream.h"
113
#include "ixheaace_mps_spatial_bitstream.h"
114
115
#include "ixheaace_mps_buf.h"
116
#include "ixheaace_mps_lib.h"
117
#include "ixheaace_mps_main_structure.h"
118
#include "ixheaace_mps_onset_detect.h"
119
120
#include "ixheaace_mps_param_extract.h"
121
122
#include "ixheaace_mps_static_gain.h"
123
#include "ixheaace_mps_filter.h"
124
#include "ixheaace_mps_delay.h"
125
#include "ixheaace_mps_dmx_tdom_enh.h"
126
#include "ixheaace_mps_main_structure.h"
127
#include "ixheaace_mps_tools_rom.h"
128
#include "ixheaace_mps_qmf.h"
129
#include "ixheaace_mps_tree.h"
130
#include "ixheaace_mps_frame_windowing.h"
131
132
#include "ixheaace_mps_structure.h"
133
#include "ixheaace_mps_memory.h"
134
#include "ixheaace_mps_enc.h"
135
#include "ixheaace_struct_def.h"
136
#include "ixheaace_api_defs.h"
137
138
#include "ixheaace_write_adts_adif.h"
139
#include "ixheaace_loudness_measurement.h"
140
#include "iusace_psy_utils.h"
141
142
4.07k
static WORD32 iusace_scratch_size(VOID) {
143
4.07k
  WORD32 scr_size;
144
4.07k
  scr_size = IXHEAAC_GET_SIZE_ALIGNED(USACE_MAX_SCR_SIZE, BYTE_ALIGN_8);
145
4.07k
  return scr_size;
146
4.07k
}
147
148
4.07k
static WORD32 iusace_calc_pers_buf_sizes(ixheaace_api_struct *pstr_api_struct) {
149
4.07k
  WORD32 pers_size = 0;
150
4.07k
  ia_usac_encoder_config_struct *pstr_config = &pstr_api_struct->config[0].usac_config;
151
152
4.07k
  pers_size += IXHEAAC_GET_SIZE_ALIGNED(pstr_config->channels * sizeof(FLOAT32 *), BYTE_ALIGN_8);
153
4.07k
  pers_size += IXHEAAC_GET_SIZE_ALIGNED(pstr_config->channels * sizeof(FLOAT32 *), BYTE_ALIGN_8);
154
4.07k
  pers_size += IXHEAAC_GET_SIZE_ALIGNED(pstr_config->channels * sizeof(FLOAT32 *), BYTE_ALIGN_8);
155
4.07k
  pers_size += IXHEAAC_GET_SIZE_ALIGNED(pstr_config->channels * sizeof(FLOAT32 *), BYTE_ALIGN_8);
156
157
4.07k
  pers_size +=
158
4.07k
      (IXHEAAC_GET_SIZE_ALIGNED((2 * pstr_config->ccfl * sizeof(FLOAT32)), BYTE_ALIGN_8) *
159
4.07k
       pstr_config->channels);
160
4.07k
  pers_size += (IXHEAAC_GET_SIZE_ALIGNED((2 * pstr_config->drc_frame_size * sizeof(FLOAT32)),
161
4.07k
                                         BYTE_ALIGN_8) *
162
4.07k
                pstr_config->channels);
163
4.07k
  if (pstr_config->use_delay_adjustment == 1) {
164
3.92k
    pers_size +=
165
3.92k
        (IXHEAAC_GET_SIZE_ALIGNED(
166
3.92k
             ((CC_DELAY_ADJUSTMENT * pstr_config->ccfl) / FRAME_LEN_1024) * sizeof(FLOAT32),
167
3.92k
             BYTE_ALIGN_8) *
168
3.92k
         pstr_config->channels);
169
3.92k
    pers_size += (IXHEAAC_GET_SIZE_ALIGNED(
170
3.92k
                      ((CC_DELAY_ADJUSTMENT * pstr_config->drc_frame_size) / FRAME_LEN_1024) *
171
3.92k
                          sizeof(FLOAT32),
172
3.92k
                      BYTE_ALIGN_8) *
173
3.92k
                  pstr_config->channels);
174
3.92k
  }
175
176
4.07k
  pers_size +=
177
4.07k
      (IXHEAAC_GET_SIZE_ALIGNED((2 * pstr_config->ccfl * sizeof(FLOAT64)), BYTE_ALIGN_8) *
178
4.07k
       pstr_config->channels);
179
180
4.07k
  pers_size += (IXHEAAC_GET_SIZE_ALIGNED((pstr_config->ccfl * sizeof(FLOAT64)), BYTE_ALIGN_8) *
181
4.07k
                pstr_config->channels);
182
183
4.07k
  pers_size +=
184
4.07k
      (IXHEAAC_GET_SIZE_ALIGNED((2 * pstr_config->ccfl * sizeof(FLOAT64)), BYTE_ALIGN_8) *
185
4.07k
       pstr_config->channels);
186
187
4.07k
  pers_size +=
188
4.07k
      (IXHEAAC_GET_SIZE_ALIGNED((3 * pstr_config->ccfl * sizeof(FLOAT64)), BYTE_ALIGN_8) *
189
4.07k
       pstr_config->channels);
190
191
4.07k
  if (pstr_config->tns_select != 0) {
192
1.85k
    pers_size +=
193
1.85k
        (IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_tns_info), BYTE_ALIGN_8) * pstr_config->channels);
194
1.85k
  }
195
196
4.07k
  pers_size += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_usac_td_encoder_struct), BYTE_ALIGN_8) *
197
4.07k
                pstr_config->channels);
198
4.07k
  return pers_size;
199
4.07k
}
200
201
static WORD32 ia_enhaacplus_enc_sizeof_delay_buffer(FLAG flag_framelength_small, WORD32 aot,
202
                                                    WORD32 resamp_idx, WORD32 delay_buf_size,
203
12.9k
                                                    FLAG mps_enable) {
204
12.9k
  WORD32 downsample_fac;
205
12.9k
  if (resamp_idx > 1) {
206
10.5k
    downsample_fac = resamp_idx / 2;
207
10.5k
  } else {
208
2.37k
    downsample_fac = 1;
209
2.37k
  }
210
  // Set the downsampler delay
211
12.9k
  WORD32 max_downsamp_delay, max_upsamp_delay;
212
12.9k
  if (resamp_idx == 2)  // 8:3
213
666
  {
214
666
    max_downsamp_delay = MAXIMUM_DS_8_1_FILTER_DELAY;
215
666
    max_upsamp_delay = MAXIMUM_DS_1_3_FILTER_DELAY;
216
12.2k
  } else if (resamp_idx == 3)  // 2:1
217
8.78k
  {
218
8.78k
    max_downsamp_delay = MAXIMUM_DS_2_1_FILTER_DELAY;
219
8.78k
    max_upsamp_delay = 0;
220
8.78k
  } else if (resamp_idx == 4)  // 4:1
221
1.14k
  {
222
1.14k
    max_downsamp_delay = MAXIMUM_DS_4_1_FILTER_DELAY;
223
1.14k
    max_upsamp_delay = 0;
224
2.37k
  } else {
225
2.37k
    max_downsamp_delay = MAXIMUM_DS_2_1_FILTER_DELAY;
226
2.37k
    max_upsamp_delay = 0;
227
2.37k
  }
228
229
12.9k
  if (aot == AOT_SBR || aot == AOT_PS) {
230
1.94k
    if (flag_framelength_small)
231
620
      return (FRAME_LEN_960 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
232
620
              INPUT_DELAY_LC) *
233
620
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
234
1.32k
    else
235
1.32k
      return (FRAME_LEN_1024 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
236
1.32k
              INPUT_DELAY_LC) *
237
1.32k
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
238
11.0k
  } else if (aot == AOT_AAC_LC) {
239
732
    if (flag_framelength_small)
240
322
      return (FRAME_LEN_960 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LC) *
241
322
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
242
410
    else
243
410
      return (FRAME_LEN_1024 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LC) *
244
410
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
245
10.2k
  } else if (aot == AOT_USAC) {
246
8.15k
    return (FRAME_LEN_1024 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
247
8.15k
            INPUT_DELAY_LC) *
248
8.15k
           IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
249
8.15k
  } else if (aot == AOT_AAC_LD) {
250
395
    if (flag_framelength_small)
251
161
      return (FRAME_LEN_480 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LD_480) *
252
161
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
253
234
    else
254
234
      return (FRAME_LEN_512 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LD_512) *
255
234
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
256
1.74k
  } else if (aot == AOT_AAC_ELD) {
257
1.74k
    if (flag_framelength_small) {
258
829
      if (mps_enable) {
259
579
        return (FRAME_LEN_480 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
260
579
                INPUT_DELAY_ELDV2_480) *
261
579
               IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
262
579
      } else {
263
250
        return (FRAME_LEN_480 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
264
250
                INPUT_DELAY_ELD_480) *
265
250
               IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
266
250
      }
267
912
    } else {
268
912
      if (mps_enable) {
269
556
        return (FRAME_LEN_512 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
270
556
                INPUT_DELAY_ELDV2_512) *
271
556
               IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
272
556
      } else {
273
356
        return (FRAME_LEN_512 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
274
356
                INPUT_DELAY_ELD_512) *
275
356
               IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
276
356
      }
277
912
    }
278
1.74k
  } else {
279
    // return LC Delay buffer size by default
280
0
    return (FRAME_LEN_1024 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LC) *
281
0
           IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
282
0
  }
283
12.9k
}
284
285
static VOID ia_enhaacplus_enc_find_slots_for_elements(WORD32 i_channel_mask,
286
                                                      WORD32 *slots_for_elements,
287
1.84k
                                                      WORD32 i_num_coupling_chan) {
288
1.84k
  WORD32 slot = 0, i;
289
290
1.84k
  if ((i_channel_mask & 0x3)) {
291
1.51k
    slots_for_elements[FRONT_LEFT_RIGHT] = slot;
292
1.51k
    slot += 2;
293
1.51k
  }
294
295
1.84k
  if ((i_channel_mask & 0x4)) {
296
1.45k
    slots_for_elements[FRONT_CENTER] = slot;
297
1.45k
    slot += 1;
298
1.45k
  }
299
300
1.84k
  if ((i_channel_mask & 0x8)) {
301
444
    slots_for_elements[LFE_CHANNEL] = slot;
302
444
    slot += 1;
303
444
  }
304
305
1.84k
  if ((i_channel_mask & 0x30)) {
306
780
    slots_for_elements[BACK_LEFT_RIGHT] = slot;
307
780
    slot += 2;
308
780
  }
309
310
1.84k
  if ((i_channel_mask & 0x100)) {
311
210
    slots_for_elements[REAR_CENTER] = slot;
312
210
    slot += 1;
313
210
  }
314
315
1.84k
  if (i_num_coupling_chan != 0) {
316
0
    for (i = 0; i < i_num_coupling_chan; i++) {
317
0
      slots_for_elements[COUPLING_CH + i] = slot;
318
0
      slot += 1;
319
0
    }
320
0
  }
321
322
1.84k
  return;
323
1.84k
}
324
325
static VOID ia_enhaacplus_enc_find_channel_config(WORD32 *num_bs_elements, WORD32 *chan_config,
326
                                                  WORD32 *element_type, WORD32 *element_slot,
327
                                                  WORD32 *element_instance_tag,
328
                                                  WORD32 i_num_coupling_chan,
329
1.84k
                                                  WORD32 i_channel_mask) {
330
1.84k
  WORD32 i;
331
1.84k
  WORD32 slots_for_elements[2 * MAXIMUM_BS_ELE];
332
1.84k
  *num_bs_elements = 0;
333
334
1.84k
  ia_enhaacplus_enc_find_slots_for_elements(i_channel_mask, slots_for_elements,
335
1.84k
                                            i_num_coupling_chan);
336
337
1.84k
  if ((i_channel_mask & 0x4)) {
338
    /*Front Center Present*/
339
1.45k
    chan_config[*num_bs_elements] = 1;
340
1.45k
    element_type[*num_bs_elements] = ID_SCE;
341
1.45k
    element_slot[*num_bs_elements] = slots_for_elements[FRONT_CENTER];
342
1.45k
    element_instance_tag[*num_bs_elements] = 0;
343
1.45k
    (*num_bs_elements)++;
344
1.45k
  }
345
346
1.84k
  if ((i_channel_mask & 0x3)) {
347
    /*Front Left and Right Present*/
348
1.51k
    chan_config[*num_bs_elements] = 2;
349
1.51k
    element_type[*num_bs_elements] = ID_CPE;
350
1.51k
    element_slot[*num_bs_elements] = slots_for_elements[FRONT_LEFT_RIGHT];
351
1.51k
    element_instance_tag[*num_bs_elements] = 0;
352
1.51k
    (*num_bs_elements)++;
353
1.51k
  }
354
355
1.84k
  if ((i_channel_mask & 0x30)) {
356
    /*Back Left and Right Present*/
357
780
    chan_config[*num_bs_elements] = 2;
358
780
    element_type[*num_bs_elements] = ID_CPE;
359
780
    element_slot[*num_bs_elements] = slots_for_elements[BACK_LEFT_RIGHT];
360
780
    element_instance_tag[*num_bs_elements] = 1;
361
780
    (*num_bs_elements)++;
362
780
  }
363
364
1.84k
  if ((i_channel_mask & 0x100)) {
365
    /* Rear Center Present*/
366
210
    chan_config[*num_bs_elements] = 1;
367
210
    element_type[*num_bs_elements] = ID_SCE;
368
210
    element_slot[*num_bs_elements] = slots_for_elements[REAR_CENTER];
369
210
    element_instance_tag[*num_bs_elements] = 1;
370
210
    (*num_bs_elements)++;
371
210
  }
372
373
1.84k
  if ((i_channel_mask & 0x8)) {
374
    /*LFE channel Present*/
375
444
    chan_config[*num_bs_elements] = 1;
376
444
    element_type[*num_bs_elements] = ID_LFE;
377
444
    element_slot[*num_bs_elements] = slots_for_elements[LFE_CHANNEL];
378
444
    element_instance_tag[*num_bs_elements] = 0;
379
444
    (*num_bs_elements)++;
380
444
  }
381
382
1.84k
  if (i_num_coupling_chan != 0) {
383
0
    for (i = 0; i < i_num_coupling_chan; i++) {
384
      /*Coupling Channel Present*/
385
0
      chan_config[*num_bs_elements] = 1;
386
0
      element_type[*num_bs_elements] = ID_CCE;
387
0
      element_slot[*num_bs_elements] = slots_for_elements[COUPLING_CH + i];
388
0
      element_instance_tag[*num_bs_elements] = i_num_coupling_chan - i - 1;
389
0
      (*num_bs_elements)++;
390
0
    }
391
0
  }
392
1.84k
}
393
394
static VOID ia_enhaacplus_enc_allocate_bitrate_between_channels(
395
1.12k
    ixheaace_api_struct *pstr_api_struct, WORD32 *bitrate, WORD32 inp_bitrate) {
396
1.12k
  WORD32 ele_idx;
397
1.12k
  WORD32 num_lfe = 0, num_mono = 0, num_stereo = 0;
398
1.12k
  WORD32 bitrate_per_stereo, bitrate_per_mono;
399
4.82k
  for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
400
3.69k
    switch (pstr_api_struct->config[ele_idx].element_type) {
401
1.33k
      case ID_SCE:
402
1.33k
      case ID_CCE:
403
1.33k
        num_mono++;
404
1.33k
        break;
405
1.90k
      case ID_CPE:
406
1.90k
        num_stereo++;
407
1.90k
        break;
408
444
      case ID_LFE:
409
444
        num_lfe++;
410
444
        break;
411
0
      default:
412
0
        return;
413
3.69k
    }
414
3.69k
  }
415
1.12k
  bitrate_per_stereo = (WORD32)((inp_bitrate - (num_lfe)*8000) / (num_mono * 0.625 + num_stereo));
416
1.12k
  bitrate_per_mono = (WORD32)(0.625 * bitrate_per_stereo);
417
4.82k
  for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
418
3.69k
    switch (pstr_api_struct->config[ele_idx].element_type) {
419
1.33k
      case ID_SCE:
420
1.33k
      case ID_CCE:
421
1.33k
        bitrate[ele_idx] = bitrate_per_mono;
422
1.33k
        break;
423
1.90k
      case ID_CPE:
424
1.90k
        bitrate[ele_idx] = bitrate_per_stereo;
425
1.90k
        break;
426
444
      case ID_LFE:
427
444
        bitrate[ele_idx] = 8000;
428
444
        break;
429
0
      default:
430
0
        return;
431
3.69k
    }
432
3.69k
  }
433
1.12k
}
434
435
1.88k
static IA_ERRORCODE ixheaace_validate_channel_mask(WORD32 ch_mask, WORD32 num_ch) {
436
1.88k
  IA_ERRORCODE err_code = IA_NO_ERROR;
437
  // If ch_mask not supported, return error
438
1.88k
  WORD32 temp_mask;
439
1.88k
  switch (num_ch) {
440
358
    case 1:
441
358
      temp_mask = CH_MASK_CENTER_FRONT;
442
358
      break;
443
393
    case 2:
444
393
      temp_mask = CH_MASK_LEFT_RIGHT_FRONT;
445
393
      break;
446
140
    case 3:
447
140
      temp_mask = CH_MASK_CENTER_FRONT | CH_MASK_LEFT_RIGHT_FRONT;
448
140
      break;
449
211
    case 4:
450
211
      temp_mask = CH_MASK_CENTER_FRONT | CH_MASK_LEFT_RIGHT_FRONT | CH_MASK_REAR_CENTER;
451
211
      break;
452
338
    case 5:
453
338
      temp_mask = CH_MASK_CENTER_FRONT | CH_MASK_LEFT_RIGHT_FRONT | CH_MASK_LEFT_RIGHT_BACK;
454
338
      break;
455
445
    case 6:
456
445
      temp_mask =
457
445
          CH_MASK_CENTER_FRONT | CH_MASK_LEFT_RIGHT_FRONT | CH_MASK_LEFT_RIGHT_BACK | CH_MASK_LFE;
458
445
      break;
459
0
    default:
460
0
      temp_mask = 0;
461
0
      break;
462
1.88k
  }
463
1.88k
  if (ch_mask != temp_mask) {
464
40
    err_code =  IA_EXHEAACE_CONFIG_FATAL_CHANNELS_MASK;
465
40
  }
466
1.88k
  return err_code;
467
1.88k
}
468
469
1.78k
static VOID ixheaace_set_default_channel_mask(WORD32 *ch_mask, WORD32 num_ch) {
470
1.78k
  switch (num_ch) {
471
279
  case 1:
472
279
    *ch_mask = CH_MASK_CENTER_FRONT;
473
279
    break;
474
404
  case 2:
475
404
    *ch_mask = CH_MASK_LEFT_RIGHT_FRONT;
476
404
    break;
477
139
  case 3:
478
139
    *ch_mask = (CH_MASK_CENTER_FRONT | CH_MASK_LEFT_RIGHT_FRONT);
479
139
    break;
480
210
  case 4:
481
210
    *ch_mask = (CH_MASK_CENTER_FRONT | CH_MASK_LEFT_RIGHT_FRONT | CH_MASK_REAR_CENTER);
482
210
    break;
483
332
  case 5:
484
332
    *ch_mask = (CH_MASK_CENTER_FRONT | CH_MASK_LEFT_RIGHT_FRONT | CH_MASK_LEFT_RIGHT_BACK);
485
332
    break;
486
423
  case 6:
487
423
    *ch_mask =
488
423
      (CH_MASK_CENTER_FRONT | CH_MASK_LEFT_RIGHT_FRONT | CH_MASK_LEFT_RIGHT_BACK | CH_MASK_LFE);
489
423
    break;
490
1.78k
  }
491
1.78k
}
492
493
static VOID ixheaace_set_default_config(ixheaace_api_struct *pstr_api_struct,
494
6.88k
                                        ixheaace_input_config *pstr_input_config) {
495
6.88k
  ia_usac_encoder_config_struct *pstr_usac_config = &pstr_api_struct->config[0].usac_config;
496
6.88k
  WORD32 i;
497
498
61.9k
  for (i = 0; i < MAXIMUM_BS_ELE; i++) {
499
55.0k
    ia_enhaacplus_enc_aac_init_default_config(&pstr_api_struct->config[i].aac_config,
500
55.0k
                                              pstr_input_config->aot);
501
502
55.0k
    pstr_api_struct->config[i].i_channels = NUM_CHANNELS_CONFIG_PARAM_DEFAULT_VALUE;
503
55.0k
    pstr_api_struct->config[i].sample_rate = AAC_SAMP_FREQ_CONFIG_PARAM_DEFAULT_VALUE;
504
55.0k
    pstr_api_struct->config[i].native_sample_rate = AAC_SAMP_FREQ_CONFIG_PARAM_DEFAULT_VALUE;
505
55.0k
    pstr_api_struct->config[i].i_n_memtabs = NUM_MEMTABS_CONFIG_PARAM_DEFAULT_VALUE;
506
55.0k
    pstr_api_struct->config[i].aac_classic = AAC_CLASSIC_CONFIG_PARAM_DEFAULT_VALUE;
507
55.0k
    pstr_api_struct->config[i].use_parametric_stereo = USE_PS_CONFIG_PARAM_DEFAULT_VALUE;
508
55.0k
    pstr_api_struct->config[i].chmode_nchannels = CHMODE_NUM_CHANNELS_CONFIG_PARAM_DEFAULT_VALUE;
509
55.0k
    pstr_api_struct->config[i].chmode = CHMODE_CONFIG_PARAM_DEFAULT_VALUE; /*stereo*/
510
55.0k
    pstr_api_struct->config[i].adts_flag = ADTS_FLAG_CONFIG_PARAM_DEFAULT_VALUE;
511
55.0k
    pstr_api_struct->config[i].num_bs_elements = NUM_BS_ELEMENTS_CONFIG_PARAM_DEFAULT_VALUE;
512
55.0k
    pstr_api_struct->config[i].i_channels_mask = CHANNEL_MASK_CONFIG_PARAM_DEFAULT_VALUE;
513
55.0k
    pstr_api_struct->config[i].i_num_coupling_chan =
514
55.0k
        NUM_COUPLING_CHANNEL_CONFIG_PARAM_DEFAULT_VALUE;
515
55.0k
    pstr_api_struct->config[i].element_type = ELEMENT_TYPE_CONFIG_PARAM_DEFAULT_VALUE;
516
55.0k
    pstr_api_struct->config[i].element_slot = ELEMENT_SLOT_CONFIG_PARAM_DEFAULT_VALUE;
517
55.0k
    pstr_api_struct->config[i].num_bs_elements = NUM_BS_ELEMENTS_CONFIG_PARAM_DEFAULT_VALUE;
518
55.0k
    pstr_api_struct->config[i].element_instance_tag =
519
55.0k
        ELEMENT_INSTANCE_TAG_CONFIG_PARAM_DEFAULT_VALUE;
520
55.0k
    pstr_api_struct->config[i].aac_config.calc_crc = AAC_CFG_CALC_CRC_CONFIG_PARAM_DEFAULT_VALUE;
521
55.0k
    pstr_api_struct->config[i].aac_config.full_bandwidth =
522
55.0k
        AAC_CFG_FULL_BW_CONFIG_PARAM_DEFAULT_VALUE;
523
55.0k
    pstr_api_struct->config[i].eldsbr_found = ELDSBR_FOUND_CONFIG_PARAM_DEFAULT_VALUE;
524
55.0k
    pstr_api_struct->config[i].use_mps = USE_MPS_PARAM_DEFAULT_VALUE;
525
55.0k
    pstr_api_struct->config[i].mps_tree_config = USE_MPS_TREE_CONFIG_PARAM_DEFAULT_VALUE;
526
55.0k
  }
527
6.88k
  if (pstr_input_config->aot == AOT_USAC) {
528
4.16k
    memset(pstr_usac_config, 0, sizeof(*pstr_usac_config));
529
4.16k
    pstr_usac_config->channels = NUM_CHANNELS_CONFIG_PARAM_DEFAULT_VALUE;
530
4.16k
    pstr_usac_config->sample_rate = USAC_SAMP_FREQ_CONFIG_PARAM_DEFAULT_VALUE;
531
4.16k
    pstr_usac_config->core_sample_rate = USAC_SAMP_FREQ_CONFIG_PARAM_DEFAULT_VALUE;
532
4.16k
    pstr_usac_config->native_sample_rate = USAC_SAMP_FREQ_CONFIG_PARAM_DEFAULT_VALUE;
533
4.16k
    pstr_usac_config->sbr_pvc_active = USAC_SBR_PVC_DEFAULT_VALUE;
534
4.16k
    pstr_usac_config->sbr_inter_tes_active = USAC_SBR_INTER_TES_DEFAULT_VALUE;
535
4.16k
    pstr_usac_config->sbr_harmonic = USAC_SBR_HARMONIC_DEFAULT_VALUE;
536
4.16k
    pstr_usac_config->bit_rate = USAC_BITRATE_DEFAULT_VALUE;
537
4.16k
    pstr_usac_config->use_fill_element = USAC_FILL_ELEMENT_DEFAULT_VALUE;
538
4.16k
    pstr_usac_config->use_drc_element = USAC_DRC_DEFAULT_VALUE;
539
4.16k
    pstr_usac_config->cmplx_pred_flag = USAC_COMPLEX_PREDECTION_DEFAULT_VALUE;
540
4.16k
    pstr_usac_config->tns_select = USAC_TNS_DEFAULT_VALUE;
541
4.16k
    pstr_usac_config->flag_noiseFilling = USAC_FLAG_NOISE_FILLING_DEFAULT_VALUE;
542
4.16k
    pstr_usac_config->use_acelp_only = USAC_DEFAULT_ACELP_FLAG_VALUE;
543
4.16k
    pstr_usac_config->is_first_frame = USAC_FIRST_FRAME_FLAG_DEFAULT_VALUE;
544
4.16k
    pstr_usac_config->num_preroll_frames = CC_NUM_PREROLL_FRAMES;
545
4.16k
    pstr_usac_config->stream_id = USAC_DEFAULT_STREAM_ID_VALUE;
546
4.16k
    pstr_usac_config->use_delay_adjustment = USAC_DEFAULT_DELAY_ADJUSTMENT_VALUE;
547
4.16k
    pstr_usac_config->is_loudness_configured = USAC_DEFAULT_MEASURED_LOUDNESS_FLAG_VALUE;
548
4.16k
  }
549
  /* Initialize table pointers */
550
6.88k
  ia_enhaacplus_enc_init_aac_tabs(&(pstr_api_struct->pstr_aac_tabs));
551
6.88k
  ia_enhaacplus_enc_init_sbr_tabs(&(pstr_api_struct->spectral_band_replication_tabs));
552
6.88k
  pstr_api_struct->common_tabs.pstr_common_tab =
553
6.88k
      (ixheaace_common_tables *)&ia_enhaacplus_enc_common_tab;
554
6.88k
}
555
556
6.88k
static IA_ERRORCODE ixheaace_validate_config_params(ixheaace_input_config *pstr_input_config) {
557
6.88k
  IA_ERRORCODE err_code = IA_NO_ERROR;
558
6.88k
  if (pstr_input_config->aot != AOT_AAC_ELD && pstr_input_config->aot != AOT_AAC_LC &&
559
5.58k
      pstr_input_config->aot != AOT_AAC_LD && pstr_input_config->aot != AOT_PS &&
560
4.81k
      pstr_input_config->aot != AOT_SBR && pstr_input_config->aot != AOT_USAC) {
561
0
    pstr_input_config->aot = AOT_AAC_LC;
562
0
  }
563
6.88k
  pstr_input_config->i_native_samp_freq = pstr_input_config->i_samp_freq;
564
6.88k
  if (pstr_input_config->aot != AOT_USAC) {
565
2.71k
    pstr_input_config->i_samp_freq = iusace_map_sample_rate(pstr_input_config->i_samp_freq);
566
4.16k
  } else {
567
4.16k
    err_code = iusace_validate_baseline_profile_sample_rate(pstr_input_config->i_samp_freq);
568
4.16k
    if (err_code) {
569
93
      return err_code;
570
93
    }
571
4.16k
  }
572
573
6.78k
  if ((pstr_input_config->i_channels < MIN_NUM_CORE_CODER_CHANNELS) ||
574
6.51k
      (pstr_input_config->i_channels > MAX_NUM_CORE_CODER_CHANNELS)) {
575
594
    pstr_input_config->i_channels = 1;
576
594
  }
577
6.78k
  if (pstr_input_config->esbr_flag != 1 && pstr_input_config->esbr_flag != 0) {
578
0
    pstr_input_config->esbr_flag = 0;
579
0
  }
580
6.78k
  if ((pstr_input_config->esbr_flag == 1) &&
581
4.21k
      ((pstr_input_config->aot != AOT_SBR) && (pstr_input_config->aot != AOT_PS) &&
582
3.33k
       (pstr_input_config->aot != AOT_USAC))) {
583
1.02k
    pstr_input_config->esbr_flag = 0;
584
1.02k
  }
585
6.78k
  if (pstr_input_config->i_use_mps != 1 && pstr_input_config->i_use_mps != 0) {
586
0
    pstr_input_config->i_use_mps = 0;
587
0
  }
588
589
6.78k
  if ((pstr_input_config->i_channels != 2) && (pstr_input_config->i_channels != 6)) {
590
2.56k
    pstr_input_config->i_use_mps = 0;
591
2.56k
  }
592
6.78k
  if (pstr_input_config->aot != AOT_AAC_ELD && pstr_input_config->aot != AOT_USAC) {
593
1.73k
    pstr_input_config->i_use_mps = 0;
594
1.73k
  }
595
6.78k
  if (pstr_input_config->aot == AOT_USAC && pstr_input_config->i_use_mps == 1) {
596
1.51k
    if (pstr_input_config->ccfl_idx < SBR_8_3) {
597
866
      pstr_input_config->ccfl_idx = SBR_2_1;
598
866
    }
599
1.51k
  }
600
6.78k
  if (AOT_USAC == pstr_input_config->aot) {
601
4.07k
    if ((pstr_input_config->i_channels != 2) || (pstr_input_config->i_samp_freq > 48000)) {
602
      // Num qmf bands is mapped only till 48000. Hence, disable mps if fs > 48000 or if input
603
      // channels is not 2
604
2.03k
      pstr_input_config->i_use_mps = 0;
605
2.03k
    }
606
4.07k
  }
607
6.78k
  if (pstr_input_config->i_use_mps == 1) {
608
1.78k
    if (pstr_input_config->i_channels == 2) {
609
1.37k
      if (pstr_input_config->i_mps_tree_config != TREE_212) {
610
1.34k
        pstr_input_config->i_mps_tree_config = TREE_212;
611
1.34k
      }
612
1.37k
    } else {
613
410
      if (pstr_input_config->i_mps_tree_config != TREE_5151 &&
614
409
          pstr_input_config->i_mps_tree_config != TREE_5152 &&
615
370
          pstr_input_config->i_mps_tree_config != TREE_525) {
616
132
        pstr_input_config->i_mps_tree_config = TREE_5151;
617
132
      }
618
410
    }
619
5.00k
  } else {
620
5.00k
    pstr_input_config->i_mps_tree_config = INVALID_TREE_CONFIG;
621
5.00k
  }
622
6.78k
  if (pstr_input_config->aot == AOT_USAC || pstr_input_config->aot == AOT_AAC_ELD ||
623
5.26k
      pstr_input_config->aot == AOT_AAC_LD) {
624
5.26k
    pstr_input_config->i_use_adts = 0;
625
5.26k
    pstr_input_config->i_use_es = 1;
626
5.26k
  }
627
6.78k
  if (pstr_input_config->aot == AOT_USAC) {
628
4.07k
    if (pstr_input_config->codec_mode != USAC_SWITCHED &&
629
2.73k
        pstr_input_config->codec_mode != USAC_ONLY_FD &&
630
2.71k
        pstr_input_config->codec_mode != USAC_ONLY_TD) {
631
2.61k
      pstr_input_config->codec_mode = USAC_ONLY_FD;
632
2.61k
    }
633
4.07k
    if (pstr_input_config->ccfl_idx < NO_SBR_CCFL_768 || pstr_input_config->ccfl_idx > SBR_4_1) {
634
1.44k
      pstr_input_config->ccfl_idx = NO_SBR_CCFL_1024;  // default value
635
1.44k
    }
636
4.07k
    if ((pstr_input_config->ccfl_idx == SBR_4_1) && (pstr_input_config->i_samp_freq < 32000))
637
218
    {
638
218
      if (pstr_input_config->i_samp_freq >= 16000)
639
163
      {
640
163
        pstr_input_config->ccfl_idx = SBR_2_1;
641
163
      }
642
55
      else
643
55
      {
644
55
        pstr_input_config->ccfl_idx = NO_SBR_CCFL_1024;
645
55
      }
646
218
    }
647
4.07k
    if (pstr_input_config->cplx_pred != 1 && pstr_input_config->cplx_pred != 0) {
648
0
      pstr_input_config->cplx_pred = 0;
649
0
    }
650
4.07k
    if (pstr_input_config->use_drc_element != 0 && pstr_input_config->use_drc_element != 1) {
651
0
      pstr_input_config->use_drc_element = 0;
652
0
    }
653
654
4.07k
    if (pstr_input_config->hq_esbr != 0 && pstr_input_config->hq_esbr != 1) {
655
0
      pstr_input_config->hq_esbr = 0;
656
0
    }
657
4.07k
    if (pstr_input_config->harmonic_sbr != 0 && pstr_input_config->harmonic_sbr != 1) {
658
0
      pstr_input_config->harmonic_sbr = 0;
659
0
    }
660
4.07k
    if (pstr_input_config->pvc_active != 0 && pstr_input_config->pvc_active != 1) {
661
0
      pstr_input_config->pvc_active = 0;
662
0
    }
663
4.07k
    if (pstr_input_config->inter_tes_active != 0 && pstr_input_config->inter_tes_active != 1) {
664
0
      pstr_input_config->inter_tes_active = 0;
665
0
    }
666
4.07k
    if ((pstr_input_config->ccfl_idx != 3 && pstr_input_config->ccfl_idx != 4)) {
667
2.29k
      pstr_input_config->harmonic_sbr = 0;
668
2.29k
    }
669
4.07k
    if (pstr_input_config->harmonic_sbr != 1) {
670
2.65k
      pstr_input_config->hq_esbr = 0;
671
2.65k
    }
672
4.07k
    if (pstr_input_config->i_bitrate != 64000 && pstr_input_config->i_bitrate != 96000) {
673
3.72k
      pstr_input_config->i_bitrate = USAC_BITRATE_DEFAULT_VALUE;
674
3.72k
    }
675
4.07k
    {
676
4.07k
      if (pstr_input_config->i_bitrate < MINIMUM_BITRATE * pstr_input_config->i_channels) {
677
0
        pstr_input_config->i_bitrate = MINIMUM_BITRATE * pstr_input_config->i_channels;
678
0
      }
679
4.07k
      if (pstr_input_config->ccfl_idx == NO_SBR_CCFL_768 ||
680
3.44k
          pstr_input_config->ccfl_idx == NO_SBR_CCFL_1024) {
681
2.14k
        if (pstr_input_config->i_bitrate >
682
2.14k
            (WORD32)(6 * pstr_input_config->i_samp_freq * pstr_input_config->i_channels)) {
683
286
          pstr_input_config->i_bitrate =
684
286
              (6 * pstr_input_config->i_samp_freq * pstr_input_config->i_channels);
685
286
        }
686
2.14k
      } else if (pstr_input_config->ccfl_idx == SBR_8_3) {
687
151
        if (pstr_input_config->i_bitrate >
688
151
            (WORD32)(6 * ((pstr_input_config->i_samp_freq * 3) / 8) *
689
151
                     pstr_input_config->i_channels)) {
690
112
          pstr_input_config->i_bitrate =
691
112
              (6 * ((pstr_input_config->i_samp_freq * 3) / 8) * pstr_input_config->i_channels);
692
112
        }
693
1.78k
      } else if (pstr_input_config->ccfl_idx == SBR_2_1) {
694
1.20k
        if (pstr_input_config->i_bitrate >
695
1.20k
            (WORD32)(6 * (pstr_input_config->i_samp_freq / 2) * pstr_input_config->i_channels)) {
696
102
          pstr_input_config->i_bitrate =
697
102
              (6 * (pstr_input_config->i_samp_freq / 2) * pstr_input_config->i_channels);
698
102
        }
699
1.20k
      } else if (pstr_input_config->ccfl_idx == SBR_4_1) {
700
572
        if (pstr_input_config->i_bitrate >
701
572
            (WORD32)(6 * (pstr_input_config->i_samp_freq / 4) * pstr_input_config->i_channels)) {
702
173
          pstr_input_config->i_bitrate =
703
173
              (6 * (pstr_input_config->i_samp_freq / 4) * pstr_input_config->i_channels);
704
173
        }
705
572
      }
706
4.07k
    }
707
708
4.07k
    {
709
4.07k
      if ((pstr_input_config->codec_mode == USAC_SWITCHED ||
710
2.73k
           pstr_input_config->codec_mode == USAC_ONLY_TD) && pstr_input_config->esbr_flag &&
711
880
          pstr_input_config->i_samp_freq > 24000) {
712
739
        if (pstr_input_config->ccfl_idx == NO_SBR_CCFL_768) {
713
154
          pstr_input_config->ccfl_idx = SBR_8_3;  // Use 8:3 eSBR
714
154
        }
715
739
        if (pstr_input_config->ccfl_idx == NO_SBR_CCFL_1024) {
716
145
          pstr_input_config->ccfl_idx = SBR_2_1;  // Use 2:1 eSBR
717
145
        }
718
739
      }
719
720
4.07k
      if (pstr_input_config->codec_mode == USAC_ONLY_FD &&
721
2.63k
          pstr_input_config->i_samp_freq > 24000 && pstr_input_config->esbr_flag &&
722
1.06k
          pstr_input_config->i_bitrate <= MAX_USAC_ESBR_BITRATE) {
723
1.06k
        if (pstr_input_config->ccfl_idx == NO_SBR_CCFL_768) {
724
28
          pstr_input_config->ccfl_idx = SBR_8_3;  // Use 8:3 eSBR
725
28
        }
726
1.06k
        if (pstr_input_config->ccfl_idx == NO_SBR_CCFL_1024) {
727
632
          pstr_input_config->ccfl_idx = SBR_2_1;  // Use 2:1 eSBR
728
632
        }
729
1.06k
      }
730
731
4.07k
      if (pstr_input_config->ccfl_idx == NO_SBR_CCFL_768 ||
732
3.62k
          pstr_input_config->ccfl_idx == SBR_8_3) {
733
784
        pstr_input_config->frame_length = LEN_SUPERFRAME_768;
734
3.29k
      } else {
735
3.29k
        pstr_input_config->frame_length = LEN_SUPERFRAME;
736
3.29k
      }
737
4.07k
    }
738
4.07k
    if (pstr_input_config->random_access_interval < MIN_RAP_INTERVAL_IN_MS) {
739
1.75k
      pstr_input_config->random_access_interval = DEFAULT_RAP_INTERVAL_IN_MS;
740
1.75k
    }
741
4.07k
    if (pstr_input_config->method_def > MAX_METHOD_DEFINITION_TYPE) {
742
3.00k
      pstr_input_config->method_def = METHOD_DEFINITION_PROGRAM_LOUDNESS;
743
3.00k
    }
744
4.07k
    if (pstr_input_config->measurement_system != MEASUREMENT_SYSTEM_BS_1770_3) {
745
4.07k
      pstr_input_config->measurement_system = MEASUREMENT_SYSTEM_BS_1770_3;
746
4.07k
    }
747
4.07k
    if (pstr_input_config->measured_loudness > MAX_METHOD_VALUE ||
748
4.07k
        pstr_input_config->measured_loudness < MIN_METHOD_VALUE) {
749
120
      pstr_input_config->measured_loudness = DEFAULT_METHOD_VALUE;
750
120
    }
751
4.07k
    if (pstr_input_config->sample_peak_level > MAX_SAMPLE_PEAK_LEVEL ||
752
4.07k
        pstr_input_config->sample_peak_level < MIN_SAMPLE_PEAK_LEVEL) {
753
0
      pstr_input_config->sample_peak_level = DEFAULT_SAMPLE_PEAK_VALUE;
754
0
    }
755
4.07k
    if (pstr_input_config->use_delay_adjustment != 0 &&
756
3.92k
        pstr_input_config->use_delay_adjustment != 1) {
757
3.92k
      pstr_input_config->use_delay_adjustment = USAC_DEFAULT_DELAY_ADJUSTMENT_VALUE;
758
3.92k
    }
759
4.07k
    if (pstr_input_config->use_drc_element) {
760
2.14k
      ia_drc_input_config *pstr_drc_cfg = (ia_drc_input_config *)pstr_input_config->pv_drc_cfg;
761
2.14k
      err_code = impd_drc_validate_config_params(pstr_drc_cfg);
762
2.14k
      if (err_code & IA_FATAL_ERROR) {
763
0
        return err_code;
764
0
      }
765
2.14k
      if (err_code) {
766
696
        pstr_input_config->use_drc_element = 0;
767
696
        err_code = IA_NO_ERROR;
768
696
      }
769
2.14k
    }
770
4.07k
  } else {
771
2.71k
    pstr_input_config->cplx_pred = 0;
772
2.71k
    pstr_input_config->harmonic_sbr = 0;
773
2.71k
    pstr_input_config->pvc_active = 0;
774
2.71k
    pstr_input_config->inter_tes_active = 0;
775
2.71k
    pstr_input_config->use_drc_element = 0;
776
2.71k
    pstr_input_config->hq_esbr = 0;
777
2.71k
    pstr_input_config->use_delay_adjustment = 0;
778
2.71k
    if (pstr_input_config->i_channels != 2 && pstr_input_config->aot == AOT_PS) {
779
299
      pstr_input_config->aot = AOT_SBR;
780
299
    }
781
2.71k
    if (pstr_input_config->aac_config.use_tns != 1 &&
782
913
        pstr_input_config->aac_config.use_tns != 0) {
783
0
      pstr_input_config->aac_config.use_tns = 0;
784
0
    }
785
2.71k
    if (pstr_input_config->aac_config.noise_filling != 1 &&
786
1.05k
        pstr_input_config->aac_config.noise_filling != 0) {
787
0
      pstr_input_config->aac_config.noise_filling = 0;
788
0
    }
789
2.71k
    if (pstr_input_config->i_use_adts != 1 && pstr_input_config->i_use_adts != 0) {
790
0
      pstr_input_config->i_use_adts = 0;
791
0
      pstr_input_config->i_use_es = 1;
792
0
    }
793
2.71k
    if (pstr_input_config->aac_config.full_bandwidth != 1 &&
794
1.65k
        pstr_input_config->aac_config.full_bandwidth != 0) {
795
0
      pstr_input_config->aac_config.full_bandwidth = 0;
796
0
    }
797
2.71k
    {
798
2.71k
      if (pstr_input_config->i_bitrate < MINIMUM_BITRATE * pstr_input_config->i_channels) {
799
1.35k
        pstr_input_config->i_bitrate = MINIMUM_BITRATE * pstr_input_config->i_channels;
800
1.35k
      }
801
2.71k
      if (pstr_input_config->i_bitrate >
802
2.71k
          (WORD32)(6 * pstr_input_config->i_samp_freq * pstr_input_config->i_channels)) {
803
839
        pstr_input_config->i_bitrate =
804
839
            (6 * pstr_input_config->i_samp_freq * pstr_input_config->i_channels);
805
839
      }
806
2.71k
    }
807
808
2.71k
    {
809
2.71k
      if (pstr_input_config->aot == AOT_AAC_LC || pstr_input_config->aot == AOT_SBR ||
810
1.52k
          pstr_input_config->aot == AOT_PS) {
811
1.52k
        if (pstr_input_config->frame_length != LEN_SUPERFRAME &&
812
1.31k
            pstr_input_config->frame_length != FRAME_LEN_960) {
813
818
          pstr_input_config->frame_length = LEN_SUPERFRAME;
814
818
        }
815
1.52k
      } else if (pstr_input_config->aot == AOT_AAC_LD || pstr_input_config->aot == AOT_AAC_ELD) {
816
1.18k
        if (pstr_input_config->frame_length != FRAME_LEN_512 &&
817
1.06k
            pstr_input_config->frame_length != FRAME_LEN_480) {
818
557
          pstr_input_config->frame_length = FRAME_LEN_512;
819
557
        }
820
1.18k
      } else {
821
0
        pstr_input_config->frame_length = LEN_SUPERFRAME;
822
0
      }
823
2.71k
    }
824
2.71k
    if ((pstr_input_config->frame_length == FRAME_LEN_960) &&
825
495
        (pstr_input_config->esbr_flag == 1)) {
826
289
      pstr_input_config->esbr_flag = 0;
827
289
    }
828
2.71k
  }
829
6.78k
  return err_code;
830
6.78k
}
831
832
static IA_ERRORCODE ixheaace_set_config_params(ixheaace_api_struct *pstr_api_struct,
833
6.88k
                                               ixheaace_input_config *pstr_input_config) {
834
6.88k
  WORD32 ele_idx;
835
6.88k
  IA_ERRORCODE err_code = IA_NO_ERROR;
836
6.88k
  ia_usac_encoder_config_struct *pstr_usac_config = &pstr_api_struct->config[0].usac_config;
837
6.88k
  err_code = ixheaace_validate_config_params(pstr_input_config);
838
6.88k
  if (err_code) {
839
93
    return err_code;
840
93
  }
841
842
6.78k
  if (pstr_input_config->ui_pcm_wd_sz != 16) {
843
205
    return (IA_EXHEAACE_CONFIG_FATAL_PCM_WDSZ);
844
205
  }
845
6.58k
  if ((pstr_input_config->aac_config.inv_quant != 0) &&
846
0
      (pstr_input_config->aac_config.inv_quant != 1) &&
847
0
      (pstr_input_config->aac_config.inv_quant != 2)) {
848
0
    return (IA_EXHEAACE_CONFIG_FATAL_QUALITY_LEVEL);
849
0
  }
850
6.58k
  if ((pstr_input_config->write_program_config_element != 0) &&
851
0
      (pstr_input_config->write_program_config_element != 1)) {
852
0
    return (IA_EXHEAACE_CONFIG_FATAL_WRITE_PCE);
853
0
  }
854
6.58k
  if ((pstr_input_config->aac_config.f_no_stereo_preprocessing != 0) &&
855
0
      (pstr_input_config->aac_config.f_no_stereo_preprocessing != 1)) {
856
0
    return (IA_EXHEAACE_CONFIG_FATAL_USE_STEREO_PRE_PROC);
857
0
  }
858
6.58k
  if ((pstr_input_config->aac_config.use_tns != 0) &&
859
3.56k
      (pstr_input_config->aac_config.use_tns != 1)) {
860
0
    return (IA_EXHEAACE_CONFIG_FATAL_USE_TNS);
861
0
  }
862
6.58k
  if ((pstr_input_config->aac_config.full_bandwidth != 0) &&
863
2.78k
      (pstr_input_config->aac_config.full_bandwidth != 1)) {
864
0
    return (IA_EXHEAACE_CONFIG_FATAL_USE_FULL_BANDWIDTH);
865
0
  }
866
867
59.2k
  for (ele_idx = 0; ele_idx < MAXIMUM_BS_ELE; ele_idx++) {
868
52.6k
    pstr_api_struct->config[ele_idx].aac_classic = 1;
869
52.6k
    pstr_api_struct->config[ele_idx].firstframe = 1;
870
52.6k
    pstr_api_struct->config[ele_idx].aot = pstr_input_config->aot;
871
52.6k
    pstr_api_struct->config[ele_idx].adts_flag = pstr_input_config->i_use_adts;
872
52.6k
    pstr_api_struct->config[ele_idx].sample_rate = pstr_input_config->i_samp_freq;
873
52.6k
    pstr_api_struct->config[ele_idx].native_sample_rate = pstr_input_config->i_native_samp_freq;
874
875
52.6k
    pstr_api_struct->config[ele_idx].i_channels = pstr_input_config->i_channels;
876
52.6k
    pstr_api_struct->config[ele_idx].i_native_channels = pstr_input_config->i_channels;
877
52.6k
    pstr_api_struct->config[ele_idx].i_channels_mode = pstr_input_config->i_channels;
878
52.6k
    pstr_api_struct->config[ele_idx].aac_config.bit_rate = pstr_input_config->i_bitrate;
879
52.6k
    pstr_api_struct->config[ele_idx].esbr_flag = pstr_input_config->esbr_flag;
880
52.6k
    pstr_api_struct->config[ele_idx].use_mps = pstr_input_config->i_use_mps;
881
52.6k
    pstr_api_struct->config[ele_idx].mps_tree_config = pstr_input_config->i_mps_tree_config;
882
52.6k
    pstr_api_struct->config[ele_idx].i_num_coupling_chan = pstr_input_config->i_num_coupling_chan;
883
52.6k
    pstr_api_struct->config[ele_idx].ui_pcm_wd_sz = pstr_input_config->ui_pcm_wd_sz;
884
52.6k
    pstr_api_struct->config[ele_idx].write_program_config_element =
885
52.6k
        pstr_input_config->write_program_config_element;
886
52.6k
    pstr_api_struct->config[ele_idx].frame_length = pstr_input_config->frame_length;
887
52.6k
    pstr_api_struct->config[ele_idx].aac_config.num_stereo_preprocessing =
888
52.6k
        pstr_input_config->aac_config.f_no_stereo_preprocessing;
889
890
52.6k
    pstr_api_struct->config[ele_idx].aac_config.use_tns = pstr_input_config->aac_config.use_tns;
891
52.6k
    if (pstr_input_config->aot == AOT_AAC_LD || pstr_input_config->aot == AOT_AAC_ELD) {
892
8.81k
      pstr_api_struct->config[ele_idx].aac_config.inv_quant =
893
8.81k
          pstr_input_config->aac_config.inv_quant;
894
8.81k
      if (pstr_input_config->frame_length == FRAME_LEN_512) {
895
4.80k
        pstr_api_struct->config[ele_idx].aac_config.flag_framelength_small = 0;
896
4.80k
      } else if (pstr_input_config->frame_length == FRAME_LEN_480) {
897
4.00k
        pstr_api_struct->config[ele_idx].aac_config.flag_framelength_small = 1;
898
4.00k
      }
899
43.8k
    } else if (pstr_input_config->aot == AOT_AAC_LC || pstr_input_config->aot == AOT_SBR ||
900
34.6k
               pstr_input_config->aot == AOT_PS) {
901
11.2k
      pstr_api_struct->config[ele_idx].aac_config.inv_quant = 0;
902
11.2k
      if (pstr_input_config->frame_length == FRAME_LEN_1024) {
903
7.44k
        pstr_api_struct->config[ele_idx].aac_config.flag_framelength_small = 0;
904
7.44k
      } else if (pstr_input_config->frame_length == FRAME_LEN_960) {
905
3.79k
        pstr_api_struct->config[ele_idx].aac_config.flag_framelength_small = 1;
906
3.79k
      }
907
11.2k
    }
908
52.6k
    if ((AOT_SBR == pstr_input_config->aot) || (AOT_PS == pstr_input_config->aot) ||
909
44.9k
        (AOT_AAC_ELD == pstr_input_config->aot)) {
910
14.5k
      pstr_api_struct->config[ele_idx].aac_classic = 0;
911
14.5k
    }
912
52.6k
    if (pstr_api_struct->config[ele_idx].sample_rate < 32000) {
913
11.1k
      if (pstr_api_struct->config[ele_idx].aac_classic == 0) {
914
240
        pstr_api_struct->config[ele_idx].aac_classic = 1;
915
240
      }
916
11.1k
      if (pstr_input_config->aot == AOT_SBR || pstr_input_config->aot == AOT_PS) {
917
159
        pstr_input_config->aot = AOT_AAC_LC;
918
159
      }
919
11.1k
      if (pstr_input_config->aot == AOT_AAC_ELD) {
920
81
        pstr_input_config->aot = AOT_AAC_LD;
921
81
      }
922
11.1k
    }
923
52.6k
    pstr_api_struct->config[ele_idx].eldsbr_found =
924
52.6k
        !(pstr_api_struct->config[ele_idx].aac_classic);
925
52.6k
  }
926
6.58k
  if (pstr_input_config->aot == AOT_USAC) {
927
4.07k
    if (pstr_input_config->i_channels > 2) {
928
0
      return IA_EXHEAACE_CONFIG_FATAL_NUM_CHANNELS;
929
0
    }
930
4.07k
    if ((pstr_input_config->i_samp_freq < 6000) || (pstr_input_config->i_samp_freq > 96000)) {
931
0
      return (IA_EXHEAACE_CONFIG_FATAL_SAMP_FREQ);
932
0
    }
933
4.07k
    pstr_api_struct->usac_en = 1;
934
935
4.07k
    pstr_usac_config->codec_mode = pstr_input_config->codec_mode;
936
4.07k
    pstr_usac_config->channels = pstr_input_config->i_channels;
937
938
4.07k
    pstr_usac_config->core_sample_rate = pstr_input_config->i_samp_freq;
939
4.07k
    pstr_usac_config->sample_rate = pstr_input_config->i_samp_freq;
940
4.07k
    pstr_usac_config->native_sample_rate = pstr_input_config->i_native_samp_freq;
941
942
4.07k
    pstr_usac_config->ui_pcm_wd_sz = pstr_input_config->ui_pcm_wd_sz;
943
4.07k
    pstr_usac_config->ccfl_idx = pstr_input_config->ccfl_idx;
944
4.07k
    pstr_usac_config->bit_rate = pstr_input_config->i_bitrate;
945
4.07k
    pstr_usac_config->basic_bitrate = pstr_input_config->i_bitrate;
946
4.07k
    pstr_usac_config->tns_select = pstr_input_config->aac_config.use_tns;
947
4.07k
    pstr_usac_config->cmplx_pred_flag = pstr_input_config->cplx_pred;
948
4.07k
    pstr_usac_config->flag_noiseFilling = pstr_input_config->aac_config.noise_filling;
949
4.07k
    pstr_usac_config->sbr_pvc_active = pstr_input_config->pvc_active;
950
4.07k
    pstr_usac_config->sbr_harmonic = pstr_input_config->harmonic_sbr;
951
4.07k
    pstr_usac_config->hq_esbr = pstr_input_config->hq_esbr;
952
4.07k
    pstr_usac_config->sbr_inter_tes_active = pstr_input_config->inter_tes_active;
953
4.07k
    pstr_api_struct->config[0].chmode_nchannels = pstr_api_struct->config[0].i_channels;
954
955
4.07k
    switch (pstr_input_config->ccfl_idx) {
956
451
      case NO_SBR_CCFL_768:
957
451
        pstr_usac_config->ccfl = LEN_SUPERFRAME_768;
958
451
        pstr_usac_config->in_frame_length = LEN_SUPERFRAME_768;
959
451
        pstr_usac_config->sbr_enable = 0;
960
451
        pstr_usac_config->drc_frame_size = LEN_SUPERFRAME_768;
961
451
        break;
962
735
      case NO_SBR_CCFL_1024:
963
735
        pstr_usac_config->ccfl = LEN_SUPERFRAME;
964
735
        pstr_usac_config->in_frame_length = LEN_SUPERFRAME;
965
735
        pstr_usac_config->sbr_enable = 0;
966
735
        pstr_usac_config->drc_frame_size = LEN_SUPERFRAME;
967
735
        break;
968
333
      case SBR_8_3:
969
333
        pstr_usac_config->ccfl = LEN_SUPERFRAME_768;
970
333
        pstr_usac_config->in_frame_length = (LEN_SUPERFRAME_768 * 8) / 3;
971
333
        pstr_usac_config->sbr_enable = 1;
972
333
        pstr_usac_config->drc_frame_size = (LEN_SUPERFRAME_768 * 8) / 3;
973
333
        break;
974
1.98k
      case SBR_2_1:
975
1.98k
        pstr_usac_config->ccfl = LEN_SUPERFRAME;
976
1.98k
        pstr_usac_config->in_frame_length = (LEN_SUPERFRAME * 2);
977
1.98k
        pstr_usac_config->sbr_enable = 1;
978
1.98k
        pstr_usac_config->drc_frame_size = (LEN_SUPERFRAME * 2);
979
1.98k
        break;
980
572
      case SBR_4_1:
981
572
        pstr_usac_config->ccfl = LEN_SUPERFRAME;
982
572
        pstr_usac_config->in_frame_length = (LEN_SUPERFRAME * 4);
983
572
        pstr_usac_config->sbr_enable = 1;
984
572
        pstr_usac_config->drc_frame_size = (LEN_SUPERFRAME * 4);
985
572
        break;
986
0
      default:
987
0
        pstr_usac_config->ccfl = LEN_SUPERFRAME;
988
0
        pstr_usac_config->in_frame_length = LEN_SUPERFRAME;
989
0
        pstr_usac_config->sbr_enable = 0;
990
0
        pstr_usac_config->drc_frame_size = LEN_SUPERFRAME;
991
0
        break;
992
4.07k
    }
993
4.07k
    pstr_usac_config->random_access_interval = pstr_input_config->random_access_interval;
994
4.07k
    if (pstr_usac_config->random_access_interval > 0) {
995
2.31k
      pstr_usac_config->random_access_interval =
996
2.31k
          (WORD32)((((WORD64)pstr_usac_config->random_access_interval *
997
2.31k
                     pstr_input_config->i_native_samp_freq) +
998
2.31k
                    (pstr_usac_config->ccfl * 1000 - 1)) /
999
2.31k
                   (pstr_usac_config->ccfl * 1000));
1000
2.31k
    }
1001
4.07k
    pstr_usac_config->use_delay_adjustment = pstr_input_config->use_delay_adjustment;
1002
4.07k
    if (pstr_usac_config->random_access_interval) {
1003
4.07k
      pstr_usac_config->preroll_flag = 1;
1004
4.07k
    }
1005
4.07k
    if (pstr_usac_config->sbr_enable == 1) {
1006
2.89k
      pstr_usac_config->num_preroll_frames++;
1007
2.89k
      if (pstr_usac_config->sbr_harmonic == 1) {
1008
1.42k
        pstr_usac_config->num_preroll_frames++;
1009
1.42k
      }
1010
2.89k
    } else {
1011
1.18k
      if (pstr_usac_config->use_delay_adjustment == 1) {
1012
1.09k
        pstr_usac_config->num_preroll_frames++;
1013
1.09k
      }
1014
1.18k
    }
1015
4.07k
    pstr_usac_config->stream_id = pstr_input_config->stream_id;
1016
4.07k
    if (pstr_input_config->ccfl_idx < NO_SBR_CCFL_768 || pstr_input_config->ccfl_idx > SBR_4_1) {
1017
0
      pstr_api_struct->config[0].ccfl_idx = NO_SBR_CCFL_1024;  // default value
1018
4.07k
    } else {
1019
4.07k
      pstr_api_struct->config[0].ccfl_idx = pstr_input_config->ccfl_idx;
1020
4.07k
    }
1021
4.07k
    if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
1022
333
      pstr_api_struct->spectral_band_replication_tabs.ptr_sos_upsamp_tab =
1023
333
          (ixheaace_resampler_sos_table *)&iixheaace_resamp_1_to_3_filt_params;
1024
1025
333
      pstr_api_struct->spectral_band_replication_tabs.ptr_sos_downsamp_tab =
1026
333
          (ixheaace_resampler_sos_table *)&iixheaace_resamp_8_to_1_filt_params;
1027
3.74k
    } else if (pstr_api_struct->config[0].ccfl_idx == SBR_2_1) {
1028
1.98k
      pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab =
1029
1.98k
          (ixheaace_resampler_table *)&ixheaace_resamp_2_to_1_iir_filt_params;
1030
1.98k
    } else if (pstr_api_struct->config[0].ccfl_idx == SBR_4_1) {
1031
572
      pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab =
1032
572
          (ixheaace_resampler_table *)&ixheaace_resamp_4_to_1_iir_filt_params;
1033
572
    }
1034
4.07k
    pstr_usac_config->use_drc_element = pstr_input_config->use_drc_element;
1035
4.07k
    {
1036
4.07k
      ia_drc_input_config *pstr_drc_cfg = (ia_drc_input_config *)pstr_input_config->pv_drc_cfg;
1037
4.07k
      pstr_drc_cfg->str_uni_drc_config.str_channel_layout.base_ch_count =
1038
4.07k
          pstr_input_config->i_channels;
1039
4.07k
      pstr_drc_cfg->str_enc_params.sample_rate = pstr_input_config->i_samp_freq;
1040
4.07k
      pstr_drc_cfg->str_enc_params.domain = TIME_DOMAIN;
1041
4.07k
      pstr_drc_cfg->str_uni_drc_config.sample_rate = pstr_drc_cfg->str_enc_params.sample_rate;
1042
4.07k
      if (pstr_usac_config->use_drc_element) {
1043
2.99k
        for (WORD32 i = 0; i < pstr_drc_cfg->str_uni_drc_config.drc_coefficients_uni_drc_count;
1044
1.55k
             i++) {
1045
1.55k
          for (WORD32 j = 0;
1046
4.18k
               j <
1047
4.18k
               pstr_drc_cfg->str_uni_drc_config.str_drc_coefficients_uni_drc[i].gain_set_count;
1048
2.63k
               j++) {
1049
2.63k
            pstr_drc_cfg->str_uni_drc_config.str_drc_coefficients_uni_drc[i]
1050
2.63k
                .str_gain_set_params[j]
1051
2.63k
                .delta_tmin =
1052
2.63k
                impd_drc_get_delta_t_min(pstr_drc_cfg->str_uni_drc_config.sample_rate);
1053
2.63k
          }
1054
1.55k
        }
1055
2.17k
        for (WORD32 i = 0; i < pstr_drc_cfg->str_uni_drc_config.str_uni_drc_config_ext
1056
2.17k
                                   .drc_coefficients_uni_drc_v1_count;
1057
1.44k
             i++) {
1058
2.32k
          for (WORD32 j = 0; j < pstr_drc_cfg->str_uni_drc_config.str_uni_drc_config_ext
1059
2.32k
                                     .str_drc_coefficients_uni_drc_v1[i]
1060
2.32k
                                     .gain_set_count;
1061
1.59k
               j++) {
1062
1.59k
            pstr_drc_cfg->str_uni_drc_config.str_uni_drc_config_ext
1063
1.59k
                .str_drc_coefficients_uni_drc_v1[i]
1064
1.59k
                .str_gain_set_params[j]
1065
1.59k
                .delta_tmin =
1066
1.59k
                impd_drc_get_delta_t_min(pstr_drc_cfg->str_uni_drc_config.sample_rate);
1067
1.59k
          }
1068
724
        }
1069
1.44k
      }
1070
4.07k
      pstr_usac_config->str_drc_cfg = *pstr_drc_cfg;
1071
4.07k
      pstr_usac_config->str_drc_cfg.str_enc_params.frame_size = pstr_usac_config->drc_frame_size;
1072
4.07k
      pstr_usac_config->str_drc_cfg.str_uni_drc_config.str_drc_coefficients_uni_drc
1073
4.07k
          ->drc_frame_size = pstr_usac_config->drc_frame_size;
1074
4.07k
      pstr_input_config->drc_frame_size = pstr_usac_config->drc_frame_size;
1075
1076
4.07k
      ia_drc_loudness_info_set_struct *pstr_enc_loudness_info_set =
1077
4.07k
          &pstr_usac_config->str_drc_cfg.str_enc_loudness_info_set;
1078
1079
4.07k
      if ((pstr_usac_config->use_drc_element &&
1080
1.44k
           ((pstr_enc_loudness_info_set->loudness_info_count != 0) ||
1081
1.25k
            (pstr_enc_loudness_info_set->loudness_info_album_count != 0) ||
1082
1.14k
            (pstr_enc_loudness_info_set->str_loudness_info_set_extension
1083
1.14k
                 .str_loudness_info_set_ext_eq.loudness_info_v1_count != 0) ||
1084
1.00k
            (pstr_enc_loudness_info_set->str_loudness_info_set_extension
1085
1.00k
                 .str_loudness_info_set_ext_eq.loudness_info_v1_album_count != 0)))) {
1086
540
        pstr_usac_config->is_loudness_configured = 1;
1087
3.53k
      } else {
1088
3.53k
        pstr_usac_config->is_loudness_configured = 0;
1089
3.53k
      }
1090
4.07k
    }
1091
4.07k
  } else {
1092
2.50k
    WORD32 max_bitreservoir_size;
1093
2.50k
    if ((pstr_input_config->i_channels > MAX_NUM_CORE_CODER_CHANNELS)) {
1094
0
      return (IA_EXHEAACE_CONFIG_FATAL_NUM_CHANNELS);
1095
0
    }
1096
2.50k
    if (!((pstr_input_config->i_native_samp_freq == 7350) ||
1097
2.45k
          (pstr_input_config->i_native_samp_freq == 8000) ||
1098
2.42k
          (pstr_input_config->i_native_samp_freq == 11025) ||
1099
2.37k
          (pstr_input_config->i_native_samp_freq == 12000) ||
1100
2.29k
          (pstr_input_config->i_native_samp_freq == 16000) ||
1101
2.26k
          (pstr_input_config->i_native_samp_freq == 22050) ||
1102
2.24k
          (pstr_input_config->i_native_samp_freq == 24000) ||
1103
2.16k
          (pstr_input_config->i_native_samp_freq == 32000) ||
1104
1.57k
          (pstr_input_config->i_native_samp_freq == 44100) ||
1105
1.00k
          (pstr_input_config->i_native_samp_freq == 48000) ||
1106
301
          (pstr_input_config->i_native_samp_freq == 64000) ||
1107
245
          (pstr_input_config->i_native_samp_freq == 88200) ||
1108
136
          (pstr_input_config->i_native_samp_freq == 96000))) {
1109
62
      return (IA_EXHEAACE_CONFIG_FATAL_SAMP_FREQ);
1110
62
    }
1111
1112
2.44k
    if ((pstr_input_config->aot == AOT_AAC_ELD) && (pstr_input_config->i_use_mps == 1) &&
1113
560
        (pstr_input_config->i_channels > 2)) {
1114
399
      pstr_api_struct->config[0].num_bs_elements = 1;
1115
399
      pstr_api_struct->config[0].i_channels = pstr_input_config->i_channels;
1116
399
      pstr_api_struct->config[0].i_native_channels = pstr_input_config->i_channels;
1117
399
      pstr_api_struct->config[0].chmode_nchannels = pstr_input_config->i_channels;
1118
399
      pstr_api_struct->config[0].element_type = ID_SCE;
1119
399
      if (pstr_api_struct->config[0].mps_tree_config == TREE_525) {
1120
235
        pstr_api_struct->config[0].element_type = ID_CPE;
1121
235
      }
1122
399
      pstr_api_struct->config[0].element_slot = 0;
1123
399
      pstr_api_struct->config[0].element_instance_tag = 0;
1124
399
      pstr_api_struct->config[0].aac_config.bit_rate = pstr_input_config->i_bitrate;
1125
399
      pstr_api_struct->config[0].use_parametric_stereo = 0;
1126
399
    }
1127
2.44k
    if (pstr_input_config->i_channels_mask == 0) {
1128
1.78k
      ixheaace_set_default_channel_mask(&pstr_input_config->i_channels_mask,
1129
1.78k
                                        pstr_input_config->i_channels);
1130
1.78k
    }
1131
2.44k
    if (pstr_api_struct->config[0].aac_config.dual_mono != 1) {
1132
2.44k
      if (pstr_input_config->aot != AOT_AAC_ELD || (pstr_input_config->i_use_mps != 1)) {
1133
1.88k
        WORD32 num_bs_elements, chan_config[MAXIMUM_BS_ELE], element_type[MAXIMUM_BS_ELE],
1134
1.88k
            element_slot[MAXIMUM_BS_ELE], element_instance_tag[MAXIMUM_BS_ELE],
1135
1.88k
            bitrate[MAXIMUM_BS_ELE];
1136
1.88k
        if ((pstr_input_config->i_channels_mask > 0x3FFFF)) {
1137
0
          return (IA_EXHEAACE_CONFIG_FATAL_CHANNELS_MASK);
1138
0
        }
1139
1.88k
        if (ixheaace_validate_channel_mask(pstr_input_config->i_channels_mask,
1140
1.88k
                                           pstr_input_config->i_channels)) {
1141
40
          return IA_EXHEAACE_CONFIG_FATAL_CHANNELS_MASK;
1142
40
        }
1143
16.6k
        for (ele_idx = 0; ele_idx < MAXIMUM_BS_ELE; ele_idx++) {
1144
14.7k
          pstr_api_struct->config[ele_idx].i_channels_mask = pstr_input_config->i_channels_mask;
1145
14.7k
        }
1146
1.84k
        ia_enhaacplus_enc_find_channel_config(
1147
1.84k
            &num_bs_elements, chan_config, element_type, element_slot, element_instance_tag,
1148
1.84k
            pstr_api_struct->config[0].i_num_coupling_chan, pstr_input_config->i_channels_mask);
1149
6.25k
        for (ele_idx = 0; ele_idx < num_bs_elements; ele_idx++) {
1150
4.40k
          pstr_api_struct->config[ele_idx].i_channels = chan_config[ele_idx];
1151
4.40k
          pstr_api_struct->config[ele_idx].i_native_channels = chan_config[ele_idx];
1152
4.40k
          pstr_api_struct->config[ele_idx].chmode_nchannels = chan_config[ele_idx];
1153
4.40k
          pstr_api_struct->config[ele_idx].element_type = element_type[ele_idx];
1154
4.40k
          pstr_api_struct->config[ele_idx].element_slot = element_slot[ele_idx];
1155
4.40k
          pstr_api_struct->config[ele_idx].num_bs_elements = num_bs_elements;
1156
4.40k
          pstr_api_struct->config[ele_idx].element_instance_tag = element_instance_tag[ele_idx];
1157
4.40k
        }
1158
1159
1.84k
        if (pstr_api_struct->config[0].num_bs_elements > 1) {
1160
1.12k
          ia_enhaacplus_enc_allocate_bitrate_between_channels(pstr_api_struct, bitrate,
1161
1.12k
                                                              pstr_input_config->i_bitrate);
1162
1163
4.82k
          for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
1164
3.69k
            pstr_api_struct->config[ele_idx].aac_config.bit_rate = bitrate[ele_idx];
1165
3.69k
          }
1166
1167
10.1k
          for (ele_idx = 0; ele_idx < MAXIMUM_BS_ELE; ele_idx++) {
1168
9.03k
            pstr_api_struct->config[ele_idx].use_parametric_stereo = 0;
1169
9.03k
          }
1170
1.12k
        }
1171
1.84k
      }
1172
2.44k
    } else {
1173
0
      WORD32 num_bs_elements;
1174
0
      WORD32 bitrate[MAXIMUM_BS_ELE];
1175
1176
0
      num_bs_elements = 2;
1177
1178
0
      pstr_api_struct->config[0].i_channels = 1;
1179
0
      pstr_api_struct->config[0].chmode_nchannels = 1;
1180
0
      pstr_api_struct->config[0].element_type = ID_SCE;
1181
0
      pstr_api_struct->config[0].element_slot = 0;
1182
0
      pstr_api_struct->config[0].num_bs_elements = num_bs_elements;
1183
0
      pstr_api_struct->config[0].element_instance_tag = 0;
1184
1185
0
      pstr_api_struct->config[1].i_channels = 1;
1186
0
      pstr_api_struct->config[1].chmode_nchannels = 1;
1187
0
      pstr_api_struct->config[1].element_type = ID_SCE;
1188
0
      pstr_api_struct->config[1].element_slot = 1;
1189
0
      pstr_api_struct->config[1].num_bs_elements = num_bs_elements;
1190
0
      pstr_api_struct->config[1].element_instance_tag = 1;
1191
1192
0
      if (pstr_api_struct->config[0].num_bs_elements > 1) {
1193
0
        ia_enhaacplus_enc_allocate_bitrate_between_channels(
1194
0
            pstr_api_struct, bitrate, pstr_api_struct->config[0].aac_config.bit_rate);
1195
1196
0
        for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
1197
0
          pstr_api_struct->config[ele_idx].aac_config.bit_rate = bitrate[ele_idx];
1198
0
        }
1199
1200
0
        for (ele_idx = 0; ele_idx < MAXIMUM_BS_ELE; ele_idx++) {
1201
0
          pstr_api_struct->config[ele_idx].use_parametric_stereo = 0;
1202
0
        }
1203
0
      }
1204
0
    }
1205
1206
2.40k
    if (pstr_input_config->aot == AOT_PS && pstr_input_config->i_channels == 2) {
1207
247
      pstr_api_struct->config[0].use_parametric_stereo = 1;
1208
247
      pstr_api_struct->config[0].chmode_nchannels = 2;
1209
2.15k
    } else {
1210
6.87k
      for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
1211
4.72k
        pstr_api_struct->config[ele_idx].chmode_nchannels =
1212
4.72k
            pstr_api_struct->config[ele_idx].i_channels;
1213
4.72k
      }
1214
2.15k
    }
1215
1216
    /* Right shift by 10 as 768 is the max bit reservoir calculated for framelength 1024 */
1217
2.40k
    max_bitreservoir_size = (BITRESERVOIR_SIZE_CONFIG_PARAM_DEFAULT_VALUE *
1218
2.40k
                             pstr_api_struct->config[0].frame_length) >>
1219
2.40k
                            10;
1220
2.40k
    if ((pstr_input_config->aac_config.bitreservoir_size > max_bitreservoir_size) ||
1221
1.93k
        (pstr_input_config->aac_config.bitreservoir_size < -1)) {
1222
1.93k
      pstr_input_config->aac_config.bitreservoir_size = max_bitreservoir_size;
1223
1.93k
    }
1224
21.6k
    for (ele_idx = 0; ele_idx < MAXIMUM_BS_ELE; ele_idx++) {
1225
19.2k
      pstr_api_struct->config[ele_idx].aac_config.bitreservoir_size =
1226
19.2k
        pstr_input_config->aac_config.bitreservoir_size;
1227
19.2k
      pstr_api_struct->config[ele_idx].aac_config.full_bandwidth =
1228
19.2k
        pstr_input_config->aac_config.full_bandwidth;
1229
19.2k
    }
1230
2.40k
  }
1231
1232
6.48k
  return IA_NO_ERROR;
1233
6.58k
}
1234
1235
6.48k
static VOID ixheaace_fill_mem_tabs(ixheaace_api_struct *pstr_api_struct, WORD32 aot) {
1236
6.48k
  WORD32 ele_idx;
1237
6.48k
  WORD32 num_channel;
1238
6.48k
  WORD32 frame_length = LEN_SUPERFRAME;
1239
6.48k
  ixheaace_mem_info_struct *pstr_mem_info;
1240
6.48k
  frame_length = pstr_api_struct->config[0].frame_length;
1241
6.48k
  WORD32 offset_size = 0;
1242
6.48k
  if (pstr_api_struct->usac_en) {
1243
4.07k
    WORD32 fac_downsample = 1;
1244
4.07k
    if (pstr_api_struct->config[0].ccfl_idx > NO_SBR_CCFL_1024) {
1245
2.89k
      fac_downsample = pstr_api_struct->config[0].ccfl_idx >> 1;
1246
2.89k
    } else {
1247
1.18k
      fac_downsample = 1;
1248
1.18k
    }
1249
    /* persistant */
1250
4.07k
    {
1251
4.07k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_PERSIST_IDX];
1252
4.07k
      {
1253
4.07k
        pstr_mem_info->ui_size =
1254
4.07k
            IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_state_struct), BYTE_ALIGN_8) +
1255
4.07k
            iusace_calc_pers_buf_sizes(pstr_api_struct);
1256
4.07k
        if (pstr_api_struct->config[0].usac_config.sbr_enable) {
1257
2.89k
          pstr_mem_info->ui_size += ixheaace_sbr_enc_pers_size(
1258
2.89k
              2, 0, pstr_api_struct->config[0].usac_config.sbr_harmonic);
1259
2.89k
        }
1260
4.07k
        offset_size = 2 *
1261
4.07k
                      ia_enhaacplus_enc_sizeof_delay_buffer(
1262
4.07k
                          0, AOT_USAC, pstr_api_struct->config[0].ccfl_idx,
1263
4.07k
                          sizeof(pstr_api_struct->pstr_state->inp_delay[0]),
1264
4.07k
                          pstr_api_struct->config[0].use_mps) *
1265
4.07k
                      pstr_api_struct->config[0].num_bs_elements;
1266
4.07k
        pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1267
1268
4.07k
        if (pstr_api_struct->config[0].use_mps) {
1269
1.20k
          offset_size =
1270
1.20k
              (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]);
1271
1.20k
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1272
1273
1.20k
          offset_size =
1274
1.20k
              (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]);
1275
1.20k
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1276
1277
1.20k
          pstr_mem_info->ui_size +=
1278
1.20k
              IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_mps_212_memory_struct), BYTE_ALIGN_8);
1279
1.20k
        }
1280
4.07k
        if (1 == pstr_api_struct->config[0].usac_config.sbr_enable) {
1281
2.89k
          offset_size =
1282
2.89k
              (MAX_FRAME_LEN * (1 << fac_downsample) + MAX_DS_8_1_FILTER_DELAY + INPUT_DELAY) *
1283
2.89k
              MAX_CHANNELS * sizeof(pstr_mem_info->ui_size);
1284
2.89k
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1285
2.89k
        }
1286
4.07k
        if ((2 != pstr_api_struct->config[0].usac_config.channels) &&
1287
1.26k
            (1 == pstr_api_struct->config[0].usac_config.sbr_enable)) {
1288
758
          offset_size = (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal[0]);
1289
758
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1290
758
        }
1291
4.07k
      }
1292
1293
4.07k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1294
4.07k
      pstr_mem_info->ui_type = IA_MEMTYPE_PERSIST;
1295
4.07k
      pstr_mem_info->ui_placement[0] = 0;
1296
4.07k
      pstr_mem_info->ui_placement[1] = 0;
1297
4.07k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1298
4.07k
      pstr_mem_info->ui_placed[0] = 0;
1299
4.07k
      pstr_mem_info->ui_placed[1] = 0;
1300
4.07k
    }
1301
1302
    /* scratch */
1303
4.07k
    {
1304
4.07k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_SCRATCH_IDX];
1305
4.07k
      UWORD32 usac_scr_size = iusace_scratch_size();
1306
4.07k
      if (pstr_api_struct->config[0].usac_config.sbr_enable) {
1307
2.89k
        UWORD32 sbr_scr_size = ixheaace_sbr_enc_scr_size() + ixheaace_resampler_scr_size();
1308
2.89k
        pstr_mem_info->ui_size = max(usac_scr_size, sbr_scr_size);
1309
2.89k
      } else {
1310
1.18k
        pstr_mem_info->ui_size = usac_scr_size;
1311
1.18k
      }
1312
1313
4.07k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1314
4.07k
      pstr_mem_info->ui_type = IA_MEMTYPE_SCRATCH;
1315
4.07k
      pstr_mem_info->ui_placement[0] = 0;
1316
4.07k
      pstr_mem_info->ui_placement[1] = 0;
1317
4.07k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1318
4.07k
      pstr_mem_info->ui_placed[0] = 0;
1319
4.07k
      pstr_mem_info->ui_placed[1] = 0;
1320
4.07k
    }
1321
1322
    /* input */
1323
4.07k
    {
1324
4.07k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_INPUT_IDX];
1325
4.07k
      WORD32 pcm_wd_sz;
1326
4.07k
      num_channel = pstr_api_struct->config[0].i_channels;
1327
4.07k
      pcm_wd_sz = pstr_api_struct->config[0].usac_config.ui_pcm_wd_sz;
1328
4.07k
      pstr_mem_info->ui_size = frame_length * num_channel * (pcm_wd_sz >> 3);
1329
4.07k
      if (1 == pstr_api_struct->config[0].usac_config.sbr_enable) {
1330
2.89k
        switch (pstr_api_struct->config[0].ccfl_idx) {
1331
333
          case SBR_8_3:  // 8:3
1332
333
            pstr_mem_info->ui_size *= 8;
1333
333
            pstr_mem_info->ui_size /= 3;
1334
333
            break;
1335
1336
1.98k
          case SBR_2_1:  // 2:1
1337
1.98k
            pstr_mem_info->ui_size *= 2;
1338
1.98k
            break;
1339
1340
572
          case SBR_4_1:  // 4:1
1341
572
            pstr_mem_info->ui_size *= 4;
1342
572
            break;
1343
2.89k
        }
1344
2.89k
      }
1345
1346
4.07k
      pstr_mem_info->ui_alignment =
1347
4.07k
          BYTE_ALIGN_8; /* As input is used as scratch memory internally */
1348
4.07k
      pstr_mem_info->ui_type = IA_MEMTYPE_INPUT;
1349
4.07k
      pstr_mem_info->ui_placement[0] = 0;
1350
4.07k
      pstr_mem_info->ui_placement[1] = 0;
1351
4.07k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1352
4.07k
      pstr_mem_info->ui_placed[0] = 0;
1353
4.07k
      pstr_mem_info->ui_placed[1] = 0;
1354
4.07k
    }
1355
1356
    /* output */
1357
0
    {
1358
4.07k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_OUTPUT_IDX];
1359
4.07k
      pstr_mem_info->ui_size =
1360
4.07k
          ((MAX_PREROLL_FRAMES + 1) * (MAX_CHANNEL_BITS / BYTE_NUMBIT) * num_channel) +
1361
4.07k
          MAX_PREROLL_CONFIG_SIZE;
1362
4.07k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1363
4.07k
      pstr_mem_info->ui_type = IA_MEMTYPE_OUTPUT;
1364
4.07k
      pstr_mem_info->ui_placement[0] = 0;
1365
4.07k
      pstr_mem_info->ui_placement[1] = 0;
1366
4.07k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1367
4.07k
      pstr_mem_info->ui_placed[0] = 0;
1368
4.07k
      pstr_mem_info->ui_placed[1] = 0;
1369
4.07k
    }
1370
4.07k
  } else {
1371
    /* persistant */
1372
2.40k
    {
1373
2.40k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_PERSIST_IDX];
1374
2.40k
      {
1375
2.40k
        if (pstr_api_struct->config[0].num_bs_elements == 1) {
1376
1.27k
          num_channel = pstr_api_struct->config[0].aac_classic
1377
1.27k
                            ? pstr_api_struct->config[0].chmode_nchannels
1378
1.27k
                            : (pstr_api_struct->config[0].use_parametric_stereo
1379
1.02k
                                   ? 1
1380
1.02k
                                   : pstr_api_struct->config[0].chmode_nchannels);
1381
1.27k
          pstr_mem_info->ui_size =
1382
1.27k
              IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_state_struct), BYTE_ALIGN_8) +
1383
1.27k
              ia_enhaacplus_enc_aac_enc_pers_size(num_channel, aot);
1384
1.27k
          if (pstr_api_struct->config[0].aot != AOT_AAC_LC &&
1385
1.18k
              pstr_api_struct->config[0].aot != AOT_AAC_LD) {
1386
1.12k
            pstr_mem_info->ui_size += ixheaace_sbr_enc_pers_size(
1387
1.12k
                num_channel, pstr_api_struct->config[0].use_parametric_stereo, 0);
1388
1.12k
          }
1389
1.27k
          offset_size = ia_enhaacplus_enc_sizeof_delay_buffer(
1390
1.27k
                            pstr_api_struct->config[0].aac_config.flag_framelength_small, aot, 3,
1391
1.27k
                            sizeof(pstr_api_struct->pstr_state->inp_delay[0]),
1392
1.27k
                            pstr_api_struct->config[0].use_mps) *
1393
1.27k
                        pstr_api_struct->config[0].num_bs_elements;
1394
1.27k
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1395
1.27k
        }
1396
2.40k
        if (pstr_api_struct->config[0].num_bs_elements > 1) {
1397
1.12k
          offset_size = IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_state_struct), BYTE_ALIGN_8) +
1398
1.12k
                        ia_enhaacplus_enc_sizeof_delay_buffer(
1399
1.12k
                            pstr_api_struct->config[0].aac_config.flag_framelength_small, aot, 3,
1400
1.12k
                            sizeof(pstr_api_struct->pstr_state->inp_delay[0]),
1401
1.12k
                            pstr_api_struct->config[0].use_mps) *
1402
1.12k
                            pstr_api_struct->config[0].num_bs_elements;
1403
1.12k
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1404
4.82k
          for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
1405
3.69k
            num_channel = pstr_api_struct->config[ele_idx].i_channels;
1406
3.69k
            if (pstr_api_struct->config[ele_idx].element_type != ID_LFE)
1407
3.24k
              pstr_mem_info->ui_size += ixheaace_sbr_enc_pers_size(num_channel, 0, 0);
1408
3.69k
            pstr_mem_info->ui_size += ia_enhaacplus_enc_aac_enc_pers_size(num_channel, aot) + 32;
1409
3.69k
          }
1410
1.12k
        }
1411
1412
2.40k
        if (pstr_api_struct->config[0].use_mps) {
1413
575
          if (pstr_api_struct->config[0].aac_config.flag_framelength_small) {
1414
292
            offset_size =
1415
292
                (MAX_INPUT_SAMPLES + (INPUT_DELAY_ELDV2_480 * IXHEAACE_MAX_CH_IN_BS_ELE)) *
1416
292
                sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]);
1417
292
          } else {
1418
283
            offset_size =
1419
283
                (MAX_INPUT_SAMPLES + (INPUT_DELAY_ELDV2_512 * IXHEAACE_MAX_CH_IN_BS_ELE)) *
1420
283
                sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]);
1421
283
          }
1422
575
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1423
575
          if (pstr_api_struct->config[0].mps_tree_config == TREE_212) {
1424
165
            pstr_mem_info->ui_size +=
1425
165
                IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_mps_212_memory_struct), BYTE_ALIGN_8);
1426
410
          } else {
1427
410
            pstr_mem_info->ui_size +=
1428
410
                IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_mps_515_memory_struct), BYTE_ALIGN_8);
1429
410
          }
1430
575
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(
1431
575
              (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]),
1432
575
              BYTE_ALIGN_8);
1433
575
        }
1434
1435
2.40k
        offset_size = IXHEAACE_MAX_PAYLOAD_SIZE * pstr_api_struct->config[0].num_bs_elements;
1436
2.40k
        pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1437
2.40k
        offset_size = (MAX_FRAME_LEN * 2 + MAX_DS_2_1_FILTER_DELAY + INPUT_DELAY) *
1438
2.40k
                      MAX_INPUT_CHAN * sizeof(pstr_mem_info->ui_size);
1439
2.40k
        pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1440
2.40k
      }
1441
1442
2.40k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1443
2.40k
      pstr_mem_info->ui_type = IA_MEMTYPE_PERSIST;
1444
2.40k
      pstr_mem_info->ui_placement[0] = 0;
1445
2.40k
      pstr_mem_info->ui_placement[1] = 0;
1446
2.40k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1447
2.40k
      pstr_mem_info->ui_placed[0] = 0;
1448
2.40k
      pstr_mem_info->ui_placed[1] = 0;
1449
2.40k
    }
1450
1451
    /* scratch */
1452
2.40k
    {
1453
2.40k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_SCRATCH_IDX];
1454
2.40k
      {
1455
2.40k
        pstr_mem_info->ui_size = ia_enhaacplus_enc_aac_enc_scr_size();
1456
2.40k
        UWORD32 sbr_scr_size = ixheaace_sbr_enc_scr_size() + ixheaace_resampler_scr_size();
1457
2.40k
        UWORD32 mps_scr_size = 0;
1458
2.40k
        if (pstr_api_struct->config[0].use_mps) {
1459
575
          if (pstr_api_struct->config[0].mps_tree_config != TREE_212) {
1460
410
            mps_scr_size = ixheaace_mps_515_scratch_size();
1461
410
          }
1462
575
        }
1463
2.40k
        pstr_mem_info->ui_size += MAX(sbr_scr_size, mps_scr_size);
1464
2.40k
      }
1465
2.40k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1466
2.40k
      pstr_mem_info->ui_type = IA_MEMTYPE_SCRATCH;
1467
2.40k
      pstr_mem_info->ui_placement[0] = 0;
1468
2.40k
      pstr_mem_info->ui_placement[1] = 0;
1469
2.40k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1470
2.40k
      pstr_mem_info->ui_placed[0] = 0;
1471
2.40k
      pstr_mem_info->ui_placed[1] = 0;
1472
2.40k
    }
1473
1474
    /* input */
1475
2.40k
    {
1476
2.40k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_INPUT_IDX];
1477
1478
2.40k
      WORD32 pcm_wd_sz;
1479
2.40k
      pcm_wd_sz = pstr_api_struct->config[0].ui_pcm_wd_sz;
1480
2.40k
      num_channel = 0;
1481
7.37k
      for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
1482
4.96k
        num_channel += pstr_api_struct->config[ele_idx].i_channels;
1483
4.96k
      }
1484
2.40k
      {
1485
2.40k
        if (pstr_api_struct->config[0].aac_classic) {
1486
671
          pstr_mem_info->ui_size = (frame_length * (pcm_wd_sz >> 3)) * num_channel;
1487
1.73k
        } else {
1488
1.73k
          pstr_mem_info->ui_size = (frame_length * (pcm_wd_sz >> 3)) * 2 * num_channel;
1489
1.73k
        }
1490
2.40k
      }
1491
2.40k
      pstr_mem_info->ui_alignment =
1492
2.40k
          BYTE_ALIGN_8; /* As input is used as scratch memory internally */
1493
2.40k
      pstr_mem_info->ui_type = IA_MEMTYPE_INPUT;
1494
2.40k
      pstr_mem_info->ui_placement[0] = 0;
1495
2.40k
      pstr_mem_info->ui_placement[1] = 0;
1496
2.40k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1497
2.40k
      pstr_mem_info->ui_placed[0] = 0;
1498
2.40k
      pstr_mem_info->ui_placed[1] = 0;
1499
2.40k
    }
1500
1501
    /* output */
1502
2.40k
    {
1503
2.40k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_OUTPUT_IDX];
1504
2.40k
      if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
1505
1.33k
        pstr_mem_info->ui_size = (6 * frame_length / 8) * num_channel;
1506
1.33k
        pstr_mem_info->ui_size += (7) * IXHEAACE_MAX_CH_IN_BS_ELE * MAXIMUM_BS_ELE;
1507
1.33k
      } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
1508
1.06k
        pstr_mem_info->ui_size = (6 * frame_length / 8) * num_channel;
1509
1.06k
      }
1510
2.40k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1511
2.40k
      pstr_mem_info->ui_type = IA_MEMTYPE_OUTPUT;
1512
2.40k
      pstr_mem_info->ui_placement[0] = 0;
1513
2.40k
      pstr_mem_info->ui_placement[1] = 0;
1514
2.40k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1515
2.40k
      pstr_mem_info->ui_placed[0] = 0;
1516
2.40k
      pstr_mem_info->ui_placed[1] = 0;
1517
2.40k
    }
1518
2.40k
  }
1519
1520
6.48k
  return;
1521
6.48k
}
1522
1523
static WORD32 get_drc_config_size(ixheaace_api_struct *pstr_api_struct,
1524
1.44k
                                  ixheaace_input_config *ptr_in_cfg) {
1525
1.44k
  WORD32 bit_count = 0;
1526
1.44k
  WORD32 total_byte_cnt = 0;
1527
1528
1.44k
  ia_drc_enc_state *pstr_drc_state =
1529
1.44k
      &pstr_api_struct->pstr_state->str_usac_enc_data.str_drc_state;
1530
1.44k
  ia_drc_input_config *pstr_in_drc_cfg = (ia_drc_input_config *)ptr_in_cfg->pv_drc_cfg;
1531
1532
1.44k
  memset(pstr_drc_state, 0, sizeof(*pstr_drc_state));
1533
1534
1.44k
  pstr_drc_state->str_enc_params = pstr_in_drc_cfg->str_enc_params;
1535
1.44k
  pstr_drc_state->str_uni_drc_config = pstr_in_drc_cfg->str_uni_drc_config;
1536
1.44k
  pstr_drc_state->str_gain_enc.str_loudness_info_set = pstr_in_drc_cfg->str_enc_loudness_info_set;
1537
1.44k
  pstr_drc_state->str_enc_gain_extension = pstr_in_drc_cfg->str_enc_gain_extension;
1538
1.44k
  pstr_drc_state->str_gain_enc.str_uni_drc_config = pstr_in_drc_cfg->str_uni_drc_config;
1539
1.44k
  pstr_drc_state->drc_scratch_mem =
1540
1.44k
      pstr_api_struct->pstr_state->str_usac_enc_data.str_scratch.ptr_scratch_buf;
1541
1.44k
  pstr_drc_state->str_gain_enc.base_ch_count = ptr_in_cfg->i_channels;
1542
1543
  //uniDrc payload size
1544
1.44k
  impd_drc_write_uni_drc_config(pstr_drc_state, &bit_count, 0);
1545
1.44k
  total_byte_cnt += ((bit_count + 7) >> 3);
1546
1.44k
  bit_count = 0;
1547
1548
  // LoudnessInfo payload size
1549
1.44k
  impd_drc_write_loudness_info_set(pstr_drc_state, NULL, &bit_count, 0);
1550
1.44k
  total_byte_cnt += ((bit_count + 7) >> 3);
1551
1552
1.44k
  return total_byte_cnt;
1553
1.44k
}
1554
1555
static IA_ERRORCODE ixheaace_alloc_and_assign_mem(ixheaace_api_struct *pstr_api_struct,
1556
                                                  ixheaace_output_config *ptr_out_cfg,
1557
6.48k
                                                  ixheaace_input_config *ptr_in_cfg) {
1558
6.48k
  IA_ERRORCODE err_code = IA_NO_ERROR;
1559
6.48k
  UWORD32 i_idx;
1560
6.48k
  pVOID pv_value;
1561
32.4k
  for (i_idx = 0; i_idx < 4; i_idx++) {
1562
25.9k
    if (i_idx == IA_ENHAACPLUSENC_OUTPUT_IDX &&
1563
6.48k
        pstr_api_struct->config[0].usac_config.use_drc_element) {
1564
1.44k
      WORD32 drc_config_size_expected =
1565
1.44k
          get_drc_config_size(pstr_api_struct, ptr_in_cfg);
1566
1.44k
      if (drc_config_size_expected > MAX_DRC_CONFIG_SIZE_EXPECTED) {
1567
0
        return IA_EXHEAACE_CONFIG_FATAL_DRC_INVALID_CONFIG;
1568
0
      }
1569
1.44k
      pstr_api_struct->pstr_mem_info[i_idx].ui_size += drc_config_size_expected;
1570
1.44k
    }
1571
25.9k
    ptr_out_cfg->mem_info_table[i_idx].ui_size = pstr_api_struct->pstr_mem_info[i_idx].ui_size;
1572
25.9k
    ptr_out_cfg->mem_info_table[i_idx].ui_alignment =
1573
25.9k
        pstr_api_struct->pstr_mem_info[i_idx].ui_alignment;
1574
25.9k
    ptr_out_cfg->mem_info_table[i_idx].ui_type = pstr_api_struct->pstr_mem_info[i_idx].ui_type;
1575
1576
25.9k
    ptr_out_cfg->arr_alloc_memory[ptr_out_cfg->malloc_count] =
1577
25.9k
        ptr_out_cfg->malloc_xheaace(ptr_out_cfg->mem_info_table[i_idx].ui_size +
1578
25.9k
                                        ptr_out_cfg->mem_info_table[i_idx].ui_alignment,
1579
25.9k
                                    ptr_out_cfg->mem_info_table[i_idx].ui_alignment);
1580
1581
25.9k
    if (NULL == ptr_out_cfg->arr_alloc_memory[ptr_out_cfg->malloc_count]) {
1582
0
      return IA_EXHEAACE_API_FATAL_MEM_ALLOC;
1583
0
    }
1584
25.9k
    memset(ptr_out_cfg->arr_alloc_memory[ptr_out_cfg->malloc_count], 0,
1585
25.9k
           ptr_out_cfg->mem_info_table[i_idx].ui_size);
1586
25.9k
    if ((i_idx == IA_ENHAACPLUSENC_PERSIST_IDX) || (i_idx == IA_ENHAACPLUSENC_SCRATCH_IDX)) {
1587
12.9k
      ptr_out_cfg->ui_rem =
1588
12.9k
          (SIZE_T)((SIZE_T)ptr_out_cfg->arr_alloc_memory[ptr_out_cfg->malloc_count] %
1589
12.9k
                   ptr_out_cfg->mem_info_table[i_idx].ui_alignment);
1590
1591
12.9k
      pv_value = ptr_out_cfg->mem_info_table[i_idx].mem_ptr =
1592
12.9k
          (pVOID)((WORD8 *)ptr_out_cfg->arr_alloc_memory[ptr_out_cfg->malloc_count] +
1593
12.9k
                  ptr_out_cfg->mem_info_table[i_idx].ui_alignment - ptr_out_cfg->ui_rem);
1594
12.9k
    } else {
1595
12.9k
      pv_value = ptr_out_cfg->mem_info_table[i_idx].mem_ptr =
1596
12.9k
          ptr_out_cfg->arr_alloc_memory[ptr_out_cfg->malloc_count];
1597
12.9k
    }
1598
1599
25.9k
    pstr_api_struct->pp_mem[i_idx] = ptr_out_cfg->mem_info_table[i_idx].mem_ptr;
1600
25.9k
    memset(pstr_api_struct->pp_mem[i_idx], 0, pstr_api_struct->pstr_mem_info[i_idx].ui_size);
1601
1602
25.9k
    pstr_api_struct->pp_mem[i_idx] = pv_value;
1603
1604
25.9k
    if (i_idx == IA_ENHAACPLUSENC_PERSIST_IDX) {
1605
6.48k
      WORD32 offset_size = IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_state_struct), BYTE_ALIGN_8);
1606
6.48k
      WORD8 *p_offset = NULL;
1607
1608
      /* Set persistent memory pointer in api obj */
1609
6.48k
      pstr_api_struct->pstr_state = (ixheaace_state_struct *)pv_value;
1610
6.48k
      WORD32 i, inp_delay_size;
1611
6.48k
      WORD8 *p_temp;
1612
6.48k
      if (pstr_api_struct->usac_en) {
1613
4.07k
        memset(pstr_api_struct->pstr_state, 0, sizeof(*(pstr_api_struct->pstr_state)));
1614
4.07k
        ia_usac_encoder_config_struct *pstr_usac_config = &pstr_api_struct->config[0].usac_config;
1615
4.07k
        ixheaace_state_struct *pstr_state = pstr_api_struct->pstr_state;
1616
4.07k
        ia_usac_data_struct *pstr_usac_enc_data = &(pstr_state->str_usac_enc_data);
1617
1618
4.07k
        pstr_state->ptr_in_buf = (FLOAT32 **)((WORD8 *)pstr_state + offset_size);
1619
1620
4.07k
        offset_size = pstr_usac_config->channels * sizeof(FLOAT32 *);
1621
4.07k
        p_offset = (WORD8 *)pstr_state->ptr_in_buf +
1622
4.07k
                   IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1623
1624
        // Input delay
1625
4.07k
        pstr_state->inp_delay = (FLOAT32 *)(p_offset);
1626
4.07k
        inp_delay_size =
1627
4.07k
            2 *
1628
4.07k
            ia_enhaacplus_enc_sizeof_delay_buffer(
1629
4.07k
                0, AOT_USAC, pstr_api_struct->config[0].ccfl_idx,
1630
4.07k
                sizeof(pstr_state->inp_delay[0]), pstr_api_struct->config[0].use_mps) *
1631
4.07k
            pstr_api_struct->config[0].num_bs_elements;
1632
4.07k
        memset(pstr_state->inp_delay, 0, inp_delay_size);
1633
4.07k
        p_offset += IXHEAAC_GET_SIZE_ALIGNED(inp_delay_size, BYTE_ALIGN_8);
1634
4.07k
        if (1 == pstr_usac_config->sbr_enable) {
1635
2.89k
          if (2 != pstr_usac_config->channels) {
1636
758
            pstr_api_struct->pstr_state->time_signal = (FLOAT32 *)(p_offset);
1637
1638
758
            memset(pstr_api_struct->pstr_state->time_signal, 0,
1639
758
                   (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal[0]));
1640
758
            offset_size =
1641
758
                (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal[0]);
1642
758
            p_offset += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1643
758
          }
1644
1645
2.89k
          pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[0] =
1646
2.89k
              (struct ixheaace_str_sbr_enc *)p_offset;
1647
2.89k
          p_offset = p_offset + ixheaace_sbr_enc_pers_size(pstr_usac_config->channels, 0,
1648
2.89k
                                                           pstr_usac_config->sbr_harmonic);
1649
2.89k
        }
1650
4.07k
        if (1 == pstr_api_struct->config[0].use_mps) {
1651
1.20k
          pstr_api_struct->pstr_state->time_signal_mps = (FLOAT32 *)(p_offset);
1652
1653
1.20k
          memset(pstr_api_struct->pstr_state->time_signal_mps, 0,
1654
1.20k
                 (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]));
1655
1.20k
          offset_size =
1656
1.20k
              (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]);
1657
1.20k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1658
1.20k
          pstr_api_struct->pstr_state->mps_bs = (UWORD8 *)(p_offset);
1659
1660
1.20k
          memset(pstr_api_struct->pstr_state->mps_bs, 0,
1661
1.20k
                 (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]));
1662
1663
1.20k
          offset_size =
1664
1.20k
              (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]);
1665
1.20k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1666
1667
1.20k
          pstr_api_struct->pstr_state->mps_pers_mem = (ixheaace_mps_212_memory_struct *)p_offset;
1668
1.20k
          p_offset +=
1669
1.20k
              IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_mps_212_memory_struct), BYTE_ALIGN_8);
1670
2.86k
        } else {
1671
2.86k
          pstr_api_struct->pstr_state->mps_bs = NULL;
1672
2.86k
        }
1673
4.07k
        if (1 == pstr_usac_config->use_drc_element) {
1674
1.44k
          pstr_state->pp_drc_in_buf = (FLOAT32 **)((WORD8 *)p_offset);
1675
1.44k
          offset_size = pstr_usac_config->channels * sizeof(pstr_state->pp_drc_in_buf[0]);
1676
1.44k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1677
1.44k
          p_temp = p_offset;
1678
1679
3.89k
          for (i = 0; i < pstr_usac_config->channels; i++) {
1680
2.45k
            pstr_state->pp_drc_in_buf[i] = (FLOAT32 *)(p_offset);
1681
2.45k
            p_offset += IXHEAAC_GET_SIZE_ALIGNED(
1682
2.45k
                (pstr_usac_config->drc_frame_size * sizeof(pstr_state->pp_drc_in_buf[0][0]) * 2),
1683
2.45k
                BYTE_ALIGN_8);
1684
2.45k
            if (pstr_usac_config->use_delay_adjustment == 1) {
1685
2.39k
              p_offset += IXHEAAC_GET_SIZE_ALIGNED(
1686
2.39k
                  ((CC_DELAY_ADJUSTMENT * pstr_usac_config->drc_frame_size) / FRAME_LEN_1024) *
1687
2.39k
                      sizeof(pstr_state->pp_drc_in_buf[0][0]),
1688
2.39k
                  BYTE_ALIGN_8);
1689
2.39k
            }
1690
2.45k
          }
1691
1.44k
          memset(p_temp, 0, (p_offset - p_temp));
1692
1.44k
        }
1693
4.07k
        p_temp = p_offset;
1694
1695
10.9k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1696
6.88k
          pstr_state->ptr_in_buf[i] = (FLOAT32 *)(p_offset);
1697
6.88k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED((pstr_usac_config->ccfl * sizeof(FLOAT32) * 2),
1698
6.88k
                                               BYTE_ALIGN_8);
1699
6.88k
          if (pstr_usac_config->use_delay_adjustment) {
1700
6.66k
            p_offset += IXHEAAC_GET_SIZE_ALIGNED(
1701
6.66k
                ((CC_DELAY_ADJUSTMENT * pstr_usac_config->ccfl) / FRAME_LEN_1024) *
1702
6.66k
                    sizeof(FLOAT32),
1703
6.66k
                BYTE_ALIGN_8);
1704
6.66k
          }
1705
6.88k
        }
1706
4.07k
        memset(p_temp, 0, (p_offset - p_temp));
1707
1708
4.07k
        p_temp = p_offset;
1709
10.9k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1710
6.88k
          pstr_usac_enc_data->ptr_time_data[i] = (FLOAT64 *)(p_offset);
1711
6.88k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED((2 * (pstr_usac_config->ccfl) * sizeof(FLOAT64)),
1712
6.88k
                                                BYTE_ALIGN_8);
1713
6.88k
        }
1714
1715
10.9k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1716
6.88k
          pstr_usac_enc_data->ptr_look_ahead_time_data[i] = (FLOAT64 *)(p_offset);
1717
6.88k
          p_offset +=
1718
6.88k
              IXHEAAC_GET_SIZE_ALIGNED((pstr_usac_config->ccfl * sizeof(FLOAT64)), BYTE_ALIGN_8);
1719
6.88k
        }
1720
1721
10.9k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1722
6.88k
          pstr_usac_enc_data->spectral_line_vector[i] = (FLOAT64 *)(p_offset);
1723
6.88k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED((2 * pstr_usac_config->ccfl * sizeof(FLOAT64)),
1724
6.88k
                                                BYTE_ALIGN_8);
1725
6.88k
        }
1726
1727
10.9k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1728
6.88k
          pstr_usac_enc_data->ptr_2frame_time_data[i] = (FLOAT64 *)(p_offset);
1729
6.88k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED((3 * pstr_usac_config->ccfl * sizeof(FLOAT64)),
1730
6.88k
                                                BYTE_ALIGN_8);
1731
6.88k
        }
1732
4.07k
        memset(p_temp, 0, p_offset - p_temp);
1733
1734
4.07k
        if (pstr_usac_config->tns_select != 0) {
1735
1.85k
          p_temp = p_offset;
1736
5.07k
          for (i = 0; i < pstr_usac_config->channels; i++) {
1737
3.22k
            pstr_usac_enc_data->pstr_tns_info[i] = (ia_tns_info *)(p_offset);
1738
3.22k
            p_offset += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_tns_info), BYTE_ALIGN_8);
1739
3.22k
          }
1740
1.85k
          memset(p_temp, 0, p_offset - p_temp);
1741
1.85k
        }
1742
1743
4.07k
        p_temp = p_offset;
1744
10.9k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1745
6.88k
          pstr_usac_enc_data->td_encoder[i] = (ia_usac_td_encoder_struct *)(p_offset);
1746
6.88k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_usac_td_encoder_struct), BYTE_ALIGN_8);
1747
6.88k
        }
1748
4.07k
        memset(p_temp, 0, p_offset - p_temp);
1749
4.07k
      } else {
1750
2.40k
        WORD32 num_aac_chan;
1751
2.40k
        ixheaace_state_struct *pstr_state = pstr_api_struct->pstr_state;
1752
2.40k
        memset(pstr_api_struct->pstr_state, 0, sizeof(*(pstr_api_struct->pstr_state)));
1753
1754
2.40k
        pstr_api_struct->pstr_state->inp_delay = (FLOAT32 *)((WORD8 *)pstr_state + offset_size);
1755
2.40k
        offset_size = ia_enhaacplus_enc_sizeof_delay_buffer(
1756
2.40k
                          pstr_api_struct->config[0].aac_config.flag_framelength_small,
1757
2.40k
                          pstr_api_struct->config[0].aot, 3,
1758
2.40k
                          sizeof(pstr_api_struct->pstr_state->inp_delay[0]),
1759
2.40k
                          pstr_api_struct->config[0].use_mps) *
1760
2.40k
                      pstr_api_struct->config[0].num_bs_elements;
1761
2.40k
        p_offset = (WORD8 *)pstr_api_struct->pstr_state->inp_delay +
1762
2.40k
                   IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1763
1764
2.40k
        if (pstr_api_struct->config[0].use_mps) {
1765
575
          pstr_api_struct->pstr_state->time_signal_mps = (FLOAT32 *)(p_offset);
1766
1767
575
          memset(pstr_api_struct->pstr_state->time_signal_mps, 0,
1768
575
                 (MAX_INPUT_SAMPLES + (INPUT_DELAY_ELDV2_512 * IXHEAACE_MAX_CH_IN_BS_ELE)) *
1769
575
                     sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]));
1770
575
          offset_size =
1771
575
              (MAX_INPUT_SAMPLES + (INPUT_DELAY_ELDV2_512 * IXHEAACE_MAX_CH_IN_BS_ELE)) *
1772
575
              sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]);
1773
575
          p_offset += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1774
1775
575
          pstr_api_struct->pstr_state->mps_bs = (UWORD8 *)(p_offset);
1776
575
          memset(pstr_api_struct->pstr_state->mps_bs, 0,
1777
575
                 (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]));
1778
575
          offset_size =
1779
575
              (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]);
1780
575
          p_offset += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1781
1782
575
          if (pstr_api_struct->config[0].mps_tree_config == TREE_212) {
1783
165
            pstr_api_struct->pstr_state->mps_pers_mem =
1784
165
                (ixheaace_mps_212_memory_struct *)p_offset;
1785
165
            p_offset +=
1786
165
                IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_mps_212_memory_struct), BYTE_ALIGN_8);
1787
410
          } else {
1788
410
            pstr_api_struct->pstr_state->mps_515_pers_mem =
1789
410
                (ixheaace_mps_515_memory_struct *)p_offset;
1790
410
            p_offset +=
1791
410
                IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_mps_515_memory_struct), BYTE_ALIGN_8);
1792
410
          }
1793
1.83k
        } else {
1794
1.83k
          pstr_api_struct->pstr_state->mps_bs = NULL;
1795
1.83k
        }
1796
1797
7.37k
        for (WORD32 ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements;
1798
4.96k
             ele_idx++) {
1799
4.96k
          num_aac_chan = pstr_api_struct->config[ele_idx].aac_classic
1800
4.96k
                             ? pstr_api_struct->config[ele_idx].chmode_nchannels
1801
4.96k
                             : (pstr_api_struct->config[ele_idx].use_parametric_stereo
1802
3.36k
                                    ? 1
1803
3.36k
                                    : pstr_api_struct->config[ele_idx].chmode_nchannels);
1804
1805
          /* Set aac enc persistent memory pointer in api obj */
1806
4.96k
          pstr_api_struct->pstr_state->aac_enc_pers_mem[ele_idx] =
1807
4.96k
              (iexheaac_encoder_str *)p_offset;
1808
4.96k
          p_offset = p_offset + ia_enhaacplus_enc_aac_enc_pers_size(
1809
4.96k
                                    num_aac_chan, pstr_api_struct->config[ele_idx].aot);
1810
1811
4.96k
          if (pstr_api_struct->config[ele_idx].aot != AOT_AAC_LC &&
1812
4.03k
              pstr_api_struct->config[ele_idx].aot != AOT_AAC_LD) {
1813
3.57k
            if (pstr_api_struct->config[ele_idx].element_type != ID_LFE) {
1814
              /* Set spectral_band_replication_ enc persistent memory pointer in api obj */
1815
3.30k
              pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[ele_idx] =
1816
3.30k
                  (struct ixheaace_str_sbr_enc *)p_offset;
1817
1818
3.30k
              p_offset =
1819
3.30k
                  p_offset +
1820
3.30k
                  ixheaace_sbr_enc_pers_size(
1821
3.30k
                      num_aac_chan, pstr_api_struct->config[ele_idx].use_parametric_stereo, 0);
1822
3.30k
            }
1823
3.57k
          }
1824
4.96k
        }
1825
2.40k
      }
1826
6.48k
    }
1827
1828
25.9k
    if ((i_idx == IA_MEMTYPE_SCRATCH) && pstr_api_struct->usac_en) {
1829
4.07k
      pstr_api_struct->pstr_state->str_usac_enc_data.str_scratch.ptr_scratch_buf =
1830
4.07k
          (UWORD8 *)pstr_api_struct->pp_mem[IA_MEMTYPE_SCRATCH];
1831
4.07k
      memset(pstr_api_struct->pp_mem[IA_MEMTYPE_SCRATCH], 0,
1832
4.07k
             pstr_api_struct->pstr_mem_info[i_idx].ui_size);
1833
4.07k
    }
1834
25.9k
    if (i_idx == IA_ENHAACPLUSENC_INPUT_IDX) {
1835
6.48k
      ptr_out_cfg->ui_inp_buf_size = ptr_out_cfg->mem_info_table[i_idx].ui_size;
1836
6.48k
    }
1837
25.9k
    ptr_out_cfg->malloc_count++;
1838
25.9k
  }
1839
6.48k
  return err_code;
1840
6.48k
}
1841
1842
static VOID ixheaace_write_audio_preroll_data(ixheaace_api_struct *pstr_api_struct,
1843
318k
                                              ia_bit_buf_struct *it_bit_buff) {
1844
318k
  ixheaace_config_struct *pstr_config = &pstr_api_struct->config[0];
1845
318k
  ixheaace_state_struct *pstr_enc_state = pstr_api_struct->pstr_state;
1846
318k
  ia_usac_data_struct *pstr_usac_data = &pstr_enc_state->str_usac_enc_data;
1847
318k
  ia_usac_encoder_config_struct *pstr_usac_config = &pstr_config->usac_config;
1848
318k
  WORD32 i, j, padding_bits;
1849
1850
318k
  if (pstr_usac_config->is_ipf) {
1851
11.1k
    if (pstr_usac_config->iframes_interval == pstr_usac_config->num_preroll_frames) {
1852
4.29k
      WORD32 config_len = 0, num_bits = 0, au_len = 0, config_bits = 0;
1853
4.29k
      WORD32 bytes_to_write;
1854
4.29k
      UWORD8 *ptr_out = (UWORD8 *)pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT];
1855
4.29k
      WORD32 max_output_size =
1856
4.29k
          ((MAX_CHANNEL_BITS / BYTE_NUMBIT) * pstr_usac_config->channels) * MAX_PREROLL_FRAMES +
1857
4.29k
          MAX_PREROLL_CONFIG_SIZE;
1858
4.29k
      UWORD8 *out_data = ptr_out + max_output_size;
1859
4.29k
      UWORD8 residual_bits = 0, residual_data = 0;
1860
4.29k
      memmove(ptr_out + max_output_size, ptr_out, pstr_enc_state->i_out_bytes);
1861
4.29k
      pstr_usac_config->is_ipf = 0;
1862
1863
4.29k
      config_bits = ixheaace_get_usac_config_bytes(NULL, &pstr_enc_state->audio_specific_config,
1864
4.29k
                                                   pstr_config->ccfl_idx);
1865
4.29k
      config_len = (config_bits + 7) >> 3;
1866
4.29k
      num_bits = iusace_write_escape_value(NULL, config_len, 4, 4, 8);
1867
4.29k
      num_bits += (config_len * 8);  // config data bits
1868
4.29k
      num_bits++;                    // apply-crossfade
1869
4.29k
      num_bits++;                    // apr-reserved
1870
      // bits for number of preroll frames
1871
4.29k
      num_bits += iusace_write_escape_value(NULL, pstr_usac_config->num_preroll_frames, 2, 4, 0);
1872
      // bits for au_len
1873
13.8k
      for (i = 0; i < pstr_usac_config->num_preroll_frames; i++) {
1874
9.56k
        num_bits += iusace_write_escape_value(NULL, pstr_usac_data->prev_out_bytes[i], 16, 16, 0);
1875
9.56k
        au_len += pstr_usac_data->prev_out_bytes[i];
1876
9.56k
      }
1877
4.29k
      iusace_reset_bit_buffer(it_bit_buff);
1878
      // total bytes to write
1879
4.29k
      bytes_to_write = (num_bits + 7) >> 3;
1880
      // usacIndependencyFlag
1881
4.29k
      iusace_write_bits_buf(it_bit_buff, pstr_usac_data->usac_independency_flag, 1);
1882
4.29k
      iusace_write_bits_buf(it_bit_buff, 1, 1);  // usacExtElementPresent
1883
4.29k
      iusace_write_bits_buf(it_bit_buff, 0, 1);  // usacExtElementUseDefaultLength
1884
1885
4.29k
      if (au_len + bytes_to_write >= MAXIMUM_VALUE_8BIT) {
1886
4.12k
        iusace_write_escape_value(it_bit_buff, au_len + bytes_to_write + 2, 8, 16, 0);
1887
4.12k
      } else {
1888
177
        iusace_write_bits_buf(it_bit_buff, au_len + bytes_to_write, 8);
1889
177
      }
1890
1891
4.29k
      iusace_write_escape_value(it_bit_buff, config_len, 4, 4, 8);  // configLen
1892
      // Config
1893
4.29k
      ixheaace_get_usac_config_bytes(it_bit_buff, &pstr_enc_state->audio_specific_config,
1894
4.29k
                                     pstr_config->ccfl_idx);
1895
1896
4.29k
      if (config_bits % 8) {
1897
2.71k
        iusace_write_bits_buf(it_bit_buff, 0, (UWORD8)((config_len << 3) - config_bits));
1898
2.71k
      }
1899
1900
4.29k
      iusace_write_bits_buf(it_bit_buff, 0, 1);  // applyCrossfade
1901
4.29k
      iusace_write_bits_buf(it_bit_buff, 0, 1);  // apr_reserved
1902
      // numPreRollFrames
1903
4.29k
      iusace_write_escape_value(it_bit_buff, pstr_usac_config->num_preroll_frames, 2, 4, 0);
1904
13.8k
      for (i = 0; i < pstr_usac_config->num_preroll_frames; i++) {
1905
9.56k
        au_len = pstr_usac_data->prev_out_bytes[i];
1906
1907
9.56k
        if (pstr_usac_config->iframes_interval != 0) {
1908
9.56k
          out_data = pstr_usac_data->prev_out_data[i];
1909
9.56k
        }
1910
1911
        // auLen
1912
9.56k
        iusace_write_escape_value(it_bit_buff, au_len, 16, 16, 0);
1913
1914
        // AccessUnit
1915
7.51M
        for (j = 0; j < au_len; j++) {
1916
7.50M
          iusace_write_bits_buf(it_bit_buff, *out_data, 8);
1917
7.50M
          out_data++;
1918
7.50M
        }
1919
9.56k
      }
1920
1921
4.29k
      if (num_bits % 8) {
1922
2.93k
        iusace_write_bits_buf(it_bit_buff, 0, (UWORD8)((bytes_to_write << 3) - num_bits));
1923
2.93k
      }
1924
      // current frame
1925
4.29k
      au_len = pstr_enc_state->i_out_bits >> 3;
1926
4.29k
      residual_bits = (UWORD8)(pstr_enc_state->i_out_bits - (au_len << 3));
1927
4.29k
      out_data = ptr_out + max_output_size;
1928
3.34M
      for (j = 0; j < au_len; j++) {
1929
3.34M
        iusace_write_bits_buf(it_bit_buff, *out_data, 8);
1930
3.34M
        out_data++;
1931
3.34M
      }
1932
4.29k
      residual_data = *out_data >> (8 - residual_bits);
1933
4.29k
      iusace_write_bits_buf(it_bit_buff, residual_data, residual_bits);
1934
1935
4.29k
      padding_bits = 8 - (it_bit_buff->cnt_bits & 7);
1936
4.29k
      if (padding_bits > 0 && padding_bits < 8) {
1937
3.67k
        ptr_out[it_bit_buff->cnt_bits >> 3] =
1938
3.67k
            (WORD8)((UWORD32)ptr_out[it_bit_buff->cnt_bits >> 3]) & (0xFF << padding_bits);
1939
3.67k
      }
1940
4.29k
      pstr_enc_state->i_out_bytes = (it_bit_buff->cnt_bits + 7) >> 3;
1941
4.29k
      pstr_usac_config->preroll_idx++;
1942
1943
4.29k
      if (!pstr_usac_config->is_first_frame) {
1944
1.45k
        pstr_usac_config->preroll_idx = pstr_usac_config->num_preroll_frames + 1;
1945
1.45k
      }
1946
4.29k
      if (pstr_usac_config->is_first_frame) {
1947
2.84k
        pstr_usac_config->is_first_frame = 0;
1948
2.84k
      }
1949
6.83k
    } else {
1950
6.83k
      if (pstr_usac_config->preroll_idx < pstr_usac_config->num_preroll_frames) {
1951
6.83k
        WORD32 *ptr_prev_out_bytes = pstr_usac_data->prev_out_bytes;
1952
6.83k
        WORD32 pr_idx = pstr_usac_config->preroll_idx;
1953
6.83k
        UWORD8 *ptr_out = (UWORD8 *)pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT];
1954
6.83k
        ptr_prev_out_bytes[pr_idx] = pstr_enc_state->i_out_bytes;
1955
6.83k
        memcpy(pstr_usac_data->prev_out_data[pr_idx++], ptr_out, pstr_enc_state->i_out_bytes);
1956
6.83k
        pstr_usac_config->preroll_idx = pr_idx;
1957
6.83k
        pstr_enc_state->i_out_bytes = 0;
1958
6.83k
      }
1959
6.83k
    }
1960
307k
  } else {
1961
699k
    for (j = 0; j < pstr_usac_config->num_preroll_frames - 1; j++) {
1962
391k
      pstr_usac_data->prev_out_bytes[j] = pstr_usac_data->prev_out_bytes[j + 1];
1963
391k
    }
1964
307k
    if (pstr_usac_config->num_preroll_frames) {
1965
307k
      pstr_usac_data->prev_out_bytes[pstr_usac_config->num_preroll_frames - 1] =
1966
307k
          pstr_enc_state->i_out_bytes;
1967
307k
    }
1968
307k
    pstr_usac_config->preroll_idx = pstr_usac_config->num_preroll_frames + 1;
1969
307k
  }
1970
318k
  return;
1971
318k
}
1972
1973
4.07k
static IA_ERRORCODE ia_usac_enc_init(ixheaace_api_struct *pstr_api_struct, WORD32 ccfl_idx) {
1974
4.07k
  IA_ERRORCODE error = IA_NO_ERROR;
1975
4.07k
  WORD32 i = 0;
1976
4.07k
  ixheaace_config_struct *pstr_config = &pstr_api_struct->config[0];
1977
4.07k
  ixheaace_state_struct *pstr_enc_state = pstr_api_struct->pstr_state;
1978
4.07k
  ia_usac_data_struct *pstr_enc_data = &pstr_enc_state->str_usac_enc_data;
1979
4.07k
  ia_usac_encoder_config_struct *pstr_usac_config = &pstr_config->usac_config;
1980
1981
4.07k
  pstr_usac_config->bit_rate = pstr_api_struct->config[0].aac_config.bit_rate;
1982
1983
4.07k
  if ((pstr_usac_config->codec_mode == USAC_SWITCHED) ||
1984
3.96k
      (pstr_usac_config->codec_mode == USAC_ONLY_FD)) {
1985
3.96k
    pstr_usac_config->aac_allow_scalefacs = 1;
1986
3.96k
    if (pstr_usac_config->aac_scale_facs == 0) pstr_usac_config->aac_allow_scalefacs = 0;
1987
3.96k
  }
1988
1989
4.07k
  if (pstr_usac_config->codec_mode == USAC_ONLY_TD) {
1990
278
    for (i = 0; i < pstr_config->i_channels; i++) {
1991
171
      pstr_enc_data->core_mode_prev[i] = CORE_MODE_FD;
1992
171
      pstr_enc_data->core_mode[i] = CORE_MODE_TD;
1993
171
    }
1994
3.96k
  } else {
1995
10.6k
    for (i = 0; i < pstr_config->i_channels; i++) {
1996
6.71k
      pstr_enc_data->core_mode_prev[i] = CORE_MODE_FD;
1997
6.71k
      pstr_enc_data->core_mode[i] = CORE_MODE_FD;
1998
6.71k
    }
1999
3.96k
  }
2000
2001
4.07k
  if (1 == pstr_usac_config->use_drc_element) {
2002
1.44k
    pstr_enc_data->str_scratch.drc_scratch = pstr_enc_data->str_scratch.ptr_scratch_buf;
2003
1.44k
  }
2004
4.07k
  if (pstr_usac_config->sbr_enable) {
2005
2.89k
    WORD8 *sbr_scr_ptr = (WORD8 *)pstr_enc_data->str_scratch.ptr_scratch_buf;
2006
2.89k
    ixheaace_audio_specific_config_struct *pstr_asc = &pstr_enc_state->audio_specific_config;
2007
2.89k
    ixheaace_str_sbr_cfg spectral_band_replication_config;
2008
    // SBR defaults
2009
2.89k
    iaace_config *pstr_sbr_config = &(pstr_api_struct->config[0].aac_config);
2010
2.89k
    WORD32 sbr_ratio = 0;
2011
2.89k
    WORD32 samples = pstr_usac_config->ccfl;
2012
    // Set scratch buffers for SBR and resampler
2013
2.89k
    pstr_api_struct->pstr_state->temp_buff_sbr =
2014
2.89k
        (WORD8 *)pstr_enc_data->str_scratch.ptr_scratch_buf;
2015
2.89k
    pstr_api_struct->pstr_state->ptr_temp_buff_resamp =
2016
2.89k
        (WORD8 *)pstr_enc_data->str_scratch.ptr_scratch_buf + ixheaace_sbr_enc_scr_size();
2017
2018
2.89k
    ixheaace_initialize_sbr_defaults(&spectral_band_replication_config);
2019
    // Set SBR codec as USAC
2020
2.89k
    spectral_band_replication_config.sbr_codec = USAC_SBR;
2021
2.89k
    spectral_band_replication_config.sbr_pvc_active = pstr_usac_config->sbr_pvc_active;
2022
2.89k
    spectral_band_replication_config.sbr_harmonic = pstr_usac_config->sbr_harmonic;
2023
2.89k
    spectral_band_replication_config.hq_esbr = pstr_usac_config->hq_esbr;
2024
2.89k
    pstr_usac_config->core_sample_rate = pstr_usac_config->sample_rate / 2;
2025
2.89k
    switch (pstr_usac_config->ccfl_idx) {
2026
572
      case SBR_4_1:
2027
572
        spectral_band_replication_config.sbr_ratio_idx = USAC_SBR_RATIO_INDEX_4_1;
2028
572
        spectral_band_replication_config.sbr_pvc_rate = USAC_SBR_DOWNSAMPLE_RATIO_4_1;
2029
572
        pstr_usac_config->core_sample_rate = pstr_usac_config->sample_rate / 4;
2030
572
        sbr_ratio = 4;
2031
572
        samples *= 4;
2032
572
        break;
2033
333
      case SBR_8_3:
2034
333
        spectral_band_replication_config.sbr_ratio_idx = USAC_SBR_RATIO_INDEX_8_3;
2035
333
        spectral_band_replication_config.sbr_pvc_rate = USAC_SBR_DOWNSAMPLE_RATIO_2_1;
2036
333
        sbr_ratio = 2;
2037
333
        samples *= 8;
2038
333
        samples /= 3;
2039
333
        break;
2040
1.98k
      case SBR_2_1:
2041
1.98k
        spectral_band_replication_config.sbr_ratio_idx = USAC_SBR_RATIO_INDEX_2_1;
2042
1.98k
        spectral_band_replication_config.sbr_pvc_rate = USAC_SBR_DOWNSAMPLE_RATIO_2_1;
2043
1.98k
        sbr_ratio = 2;
2044
1.98k
        samples *= 2;
2045
1.98k
        break;
2046
0
      default:
2047
0
        spectral_band_replication_config.sbr_ratio_idx = USAC_SBR_RATIO_NO_SBR;
2048
0
        spectral_band_replication_config.sbr_pvc_rate = 2;
2049
0
        sbr_ratio = 2;
2050
0
        break;
2051
2.89k
    }
2052
2.89k
    if (pstr_api_struct->pstr_state->mps_enable) {
2053
1.13k
      ixheaace_mps_212_memory_struct *pstr_mps_memory;
2054
1.13k
      pstr_mps_memory = pstr_api_struct->pstr_state->mps_pers_mem;
2055
1.13k
      ixheaace_mps_212_open(&pstr_api_struct->pstr_mps_212_enc, pstr_mps_memory);
2056
1.13k
      pstr_asc->str_aac_config.num_sac_cfg_bits = 0;
2057
2058
1.13k
      error = ixheaace_mps_212_initialise(
2059
1.13k
          pstr_api_struct->pstr_mps_212_enc, AOT_USAC, pstr_usac_config->sample_rate,
2060
1.13k
          &pstr_sbr_config->bit_rate, sbr_ratio, (WORD32)samples, samples, 515 * sbr_ratio,
2061
1.13k
          (WORD8 *)pstr_api_struct->pstr_state->ptr_temp_buff_resamp);
2062
1.13k
      if (error) {
2063
31
        return error;
2064
31
      }
2065
2066
1.10k
      pstr_asc->str_aac_config.num_sac_cfg_bits = ixheaace_mps_212_get_spatial_specific_config(
2067
1.10k
          pstr_api_struct->pstr_mps_212_enc, (WORD8 *)pstr_asc->str_aac_config.sac_cfg_data,
2068
1.10k
          sizeof(pstr_asc->str_aac_config.sac_cfg_data), AOT_USAC);
2069
1.10k
    }
2070
2.85k
    ixheaace_adjust_sbr_settings(
2071
2.85k
        &spectral_band_replication_config, pstr_sbr_config->bit_rate,
2072
2.85k
        (pstr_api_struct->pstr_state->mps_enable != 1) ? pstr_config->i_channels : 1,
2073
2.85k
        pstr_usac_config->core_sample_rate, AACENC_TRANS_FAC, 24000,
2074
2.85k
        pstr_api_struct->spectral_band_replication_tabs.ptr_qmf_tab,
2075
2.85k
        pstr_api_struct->pstr_state->aot, (pstr_api_struct->config[0].ccfl_idx == SBR_4_1));
2076
2077
2.85k
    error = ixheaace_env_open(
2078
2.85k
        &pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[0],
2079
2.85k
        &spectral_band_replication_config, &pstr_sbr_config->band_width, sbr_scr_ptr,
2080
2.85k
        &(pstr_api_struct->spectral_band_replication_tabs), &pstr_asc->str_aac_config.sbr_config);
2081
2.85k
    if (error) {
2082
11
      return error;
2083
11
    }
2084
2085
2.84k
    if (pstr_api_struct->config[0].ccfl_idx >= 2) {
2086
2.84k
      pstr_api_struct->pstr_state->downsample[0] = 1;
2087
2.84k
    } else {
2088
0
      pstr_api_struct->pstr_state->downsample[0] = 0;
2089
0
    }
2090
2091
2.84k
    if (pstr_api_struct->pstr_state->downsample[0]) {
2092
2.84k
      IA_ERRORCODE resamp_error = IA_NO_ERROR;
2093
2.84k
      WORD32 resamp_ratio = 0, upsamp_fac = 0, downsamp_fac = 0;
2094
2.84k
      WORD32 ele_idx = 0, ch_idx = 0;
2095
2096
2.84k
      if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
2097
333
        upsamp_fac = 3;
2098
333
        downsamp_fac = 8;
2099
333
        pstr_usac_config->sample_rate /= 2;
2100
2.51k
      } else if (pstr_api_struct->config[0].ccfl_idx == SBR_2_1) {
2101
1.94k
        resamp_ratio = 2;
2102
1.94k
        pstr_usac_config->sample_rate /= 2;
2103
1.94k
      } else if (pstr_api_struct->config[0].ccfl_idx == SBR_4_1) {
2104
569
        resamp_ratio = 4;
2105
569
        pstr_usac_config->sample_rate /= 4;
2106
569
      }
2107
2108
2.84k
      if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
2109
333
        if (upsamp_fac != 3 || downsamp_fac != 8) {
2110
0
          return IA_EXHEAACE_CONFIG_FATAL_USAC_RESAMPLER_RATIO;
2111
0
        }
2112
2.51k
      } else {
2113
2.51k
        if (resamp_ratio != 2 && resamp_ratio != 4) {
2114
0
          return IA_EXHEAACE_CONFIG_FATAL_USAC_RESAMPLER_RATIO;
2115
0
        }
2116
2.51k
      }
2117
2.84k
      if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {  // Upsampler initialization
2118
333
        resamp_error = ia_enhaacplus_enc_init_iir_sos_resampler(
2119
333
            &(pstr_api_struct->pstr_state->up_sampler[ele_idx][ch_idx]), upsamp_fac,
2120
333
            pstr_api_struct->spectral_band_replication_tabs.ptr_sos_upsamp_tab);
2121
333
        if (resamp_error) {
2122
0
          return resamp_error;
2123
0
        }
2124
333
        if (pstr_api_struct->config[0].i_channels > 1) {
2125
170
          resamp_error = ia_enhaacplus_enc_init_iir_sos_resampler(
2126
170
              &(pstr_api_struct->pstr_state->up_sampler[ele_idx][ch_idx + 1]), upsamp_fac,
2127
170
              pstr_api_struct->spectral_band_replication_tabs.ptr_sos_upsamp_tab);
2128
170
          if (resamp_error) {
2129
0
            return resamp_error;
2130
0
          }
2131
170
        }
2132
333
        if (pstr_usac_config->sbr_harmonic) {
2133
0
          resamp_error = ia_enhaacplus_enc_init_iir_sos_resampler(
2134
0
              &(pstr_api_struct->pstr_state->hbe_up_sampler[ele_idx][ch_idx]), upsamp_fac,
2135
0
              pstr_api_struct->spectral_band_replication_tabs.ptr_sos_upsamp_tab);
2136
0
          if (resamp_error) {
2137
0
            return resamp_error;
2138
0
          }
2139
0
          if (pstr_api_struct->config[0].i_channels > 1) {
2140
0
            resamp_error = ia_enhaacplus_enc_init_iir_sos_resampler(
2141
0
                &(pstr_api_struct->pstr_state->hbe_up_sampler[ele_idx][ch_idx + 1]), upsamp_fac,
2142
0
                pstr_api_struct->spectral_band_replication_tabs.ptr_sos_upsamp_tab);
2143
0
            if (resamp_error) {
2144
0
              return resamp_error;
2145
0
            }
2146
0
          }
2147
0
        }
2148
        // Downsampler initialization
2149
333
        resamp_error = ia_enhaacplus_enc_init_iir_sos_resampler(
2150
333
            &(pstr_api_struct->pstr_state->down_samp_sos[ele_idx][ch_idx]), downsamp_fac,
2151
333
            pstr_api_struct->spectral_band_replication_tabs.ptr_sos_downsamp_tab);
2152
333
        if (resamp_error) {
2153
0
          return resamp_error;
2154
0
        }
2155
333
        if (pstr_api_struct->config[0].i_channels > 1) {
2156
170
          resamp_error = ia_enhaacplus_enc_init_iir_sos_resampler(
2157
170
              &(pstr_api_struct->pstr_state->down_samp_sos[ele_idx][ch_idx + 1]), downsamp_fac,
2158
170
              pstr_api_struct->spectral_band_replication_tabs.ptr_sos_downsamp_tab);
2159
170
          if (resamp_error) {
2160
0
            return resamp_error;
2161
0
          }
2162
170
        }
2163
333
        if (pstr_usac_config->sbr_harmonic) {
2164
0
          resamp_error = ia_enhaacplus_enc_init_iir_sos_resampler(
2165
0
              &(pstr_api_struct->pstr_state->hbe_down_samp_sos[ele_idx][ch_idx]), downsamp_fac,
2166
0
              pstr_api_struct->spectral_band_replication_tabs.ptr_sos_downsamp_tab);
2167
0
          if (resamp_error) {
2168
0
            return resamp_error;
2169
0
          }
2170
0
          if (pstr_api_struct->config[0].i_channels > 1) {
2171
0
            resamp_error = ia_enhaacplus_enc_init_iir_sos_resampler(
2172
0
                &(pstr_api_struct->pstr_state->hbe_down_samp_sos[ele_idx][ch_idx + 1]),
2173
0
                downsamp_fac,
2174
0
                pstr_api_struct->spectral_band_replication_tabs.ptr_sos_downsamp_tab);
2175
0
            if (resamp_error) {
2176
0
              return resamp_error;
2177
0
            }
2178
0
          }
2179
0
        }
2180
2.51k
      } else if (pstr_api_struct->config[0].ccfl_idx == SBR_2_1 ||
2181
2.51k
                 pstr_api_struct->config[0].ccfl_idx == SBR_4_1) {
2182
2.51k
        resamp_error = ia_enhaacplus_enc_init_iir_resampler(
2183
2.51k
            &(pstr_api_struct->pstr_state->down_sampler[ele_idx][ch_idx]), resamp_ratio,
2184
2.51k
            pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab);
2185
2.51k
        if (resamp_error) {
2186
0
          return resamp_error;
2187
0
        }
2188
2.51k
        if (pstr_api_struct->config[0].i_channels > 1) {
2189
1.92k
          resamp_error = ia_enhaacplus_enc_init_iir_resampler(
2190
1.92k
              &(pstr_api_struct->pstr_state->down_sampler[ele_idx][ch_idx + 1]), resamp_ratio,
2191
1.92k
              pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab);
2192
1.92k
          if (resamp_error) {
2193
0
            return resamp_error;
2194
0
          }
2195
1.92k
        }
2196
2.51k
        if (pstr_usac_config->sbr_harmonic) {
2197
1.41k
          resamp_error = ia_enhaacplus_enc_init_iir_resampler(
2198
1.41k
              &(pstr_api_struct->pstr_state->hbe_down_sampler[ele_idx][ch_idx]), resamp_ratio,
2199
1.41k
              pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab);
2200
1.41k
          if (resamp_error) {
2201
0
            return resamp_error;
2202
0
          }
2203
1.41k
          if (pstr_api_struct->config[0].i_channels > 1) {
2204
1.29k
            resamp_error = ia_enhaacplus_enc_init_iir_resampler(
2205
1.29k
                &(pstr_api_struct->pstr_state->hbe_down_sampler[ele_idx][ch_idx + 1]),
2206
1.29k
                resamp_ratio, pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab);
2207
1.29k
            if (resamp_error) {
2208
0
              return resamp_error;
2209
0
            }
2210
1.29k
          }
2211
1.41k
        }
2212
2.51k
      }
2213
2.84k
    }
2214
2.84k
  }
2215
2216
4.03k
  error = iusace_enc_init(pstr_usac_config, &pstr_api_struct->pstr_state->audio_specific_config,
2217
4.03k
                          &pstr_api_struct->pstr_state->str_usac_enc_data);
2218
4.03k
  if (error & IA_FATAL_ERROR) {
2219
828
    return error;
2220
828
  }
2221
3.20k
  pstr_api_struct->pstr_state->str_usac_enc_data.frame_count = 0;
2222
3.20k
  pstr_usac_config->is_ipf = 1;
2223
3.20k
  pstr_enc_data->stereo_config_index = (pstr_enc_state->mps_enable == 1) ? 2 : 0;
2224
3.20k
  ia_bit_buf_struct *pstr_ia_asc_bit_buf;
2225
3.20k
  pstr_ia_asc_bit_buf = iusace_create_bit_buffer(
2226
3.20k
      &(pstr_api_struct->pstr_state->str_bit_buf), pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT],
2227
3.20k
      pstr_api_struct->pstr_mem_info[IA_MEMTYPE_OUTPUT].ui_size, 1);
2228
3.20k
  if (pstr_usac_config->sbr_enable) {
2229
9.65k
    for (UWORD32 idx = 0; idx < pstr_usac_config->num_elements; idx++) {
2230
7.35k
      switch (pstr_enc_state->audio_specific_config.str_usac_config.usac_element_type[idx]) {
2231
589
        case ID_USAC_SCE:
2232
2.30k
        case ID_USAC_CPE:
2233
2.30k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2234
2.30k
              .stereo_config_index = pstr_enc_data->stereo_config_index;
2235
2.30k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2236
2.30k
              .str_usac_sbr_config.bs_inter_tes = pstr_usac_config->sbr_inter_tes_active;
2237
2.30k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2238
2.30k
              .str_usac_sbr_config.bs_pvc = pstr_usac_config->sbr_pvc_active;
2239
2.30k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2240
2.30k
              .str_usac_sbr_config.dflt_header_extra1 = 0;
2241
2.30k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2242
2.30k
              .str_usac_sbr_config.dflt_header_extra2 = 0;
2243
2.30k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2244
2.30k
              .str_usac_sbr_config.dflt_start_freq = 0;
2245
2.30k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2246
2.30k
              .str_usac_sbr_config.dflt_stop_freq = 4;
2247
2.30k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2248
2.30k
              .str_usac_sbr_config.harmonic_sbr = pstr_usac_config->sbr_harmonic;
2249
2.30k
          break;
2250
5.05k
        default:
2251
5.05k
          continue;
2252
7.35k
      }
2253
7.35k
    }
2254
2.30k
  }
2255
2256
3.20k
  ixheaace_get_audiospecific_config_bytes(pstr_ia_asc_bit_buf,
2257
3.20k
                                          &pstr_api_struct->pstr_state->audio_specific_config,
2258
3.20k
                                          AOT_USAC, ccfl_idx);
2259
3.20k
  pstr_api_struct->pstr_state->i_out_bytes = (pstr_ia_asc_bit_buf->cnt_bits + 7) >> 3;
2260
2261
3.20k
  return IA_NO_ERROR;
2262
3.20k
}
2263
2264
4.95k
static IA_ERRORCODE ia_enhaacplus_enc_init(ixheaace_api_struct *pstr_api_struct, WORD32 ele_idx) {
2265
4.95k
  IA_ERRORCODE error = IA_NO_ERROR;
2266
4.95k
  WORD32 anc_flag = 0;
2267
4.95k
  WORD32 anc_rate = -1;
2268
4.95k
  WORD32 anc_mode = 0;
2269
4.95k
  WORD32 core_ch;
2270
4.95k
  WORD32 asc_channels;
2271
4.95k
  WORD8 *ptr_resampler_scratch;
2272
4.95k
  iaace_scratch *pstr_aac_scratch;
2273
4.95k
  WORD8 *ptr_spectral_band_replication_scratch;
2274
4.95k
  UWORD32 core_sample_rate;
2275
4.95k
  ixheaace_state_struct *pstr_enc_state = pstr_api_struct->pstr_state;
2276
4.95k
  ixheaace_audio_specific_config_struct *pstr_asc = &pstr_enc_state->audio_specific_config;
2277
2278
4.95k
  iaace_config *pstr_aac_config = &(pstr_api_struct->config[ele_idx].aac_config);
2279
2280
4.95k
  ixheaace_config_ancillary *pstr_ancillary = &(pstr_api_struct->config[ele_idx].pstr_ancillary);
2281
4.95k
  pstr_aac_scratch = (iaace_scratch *)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_SCRATCH_IDX];
2282
4.95k
  ptr_spectral_band_replication_scratch =
2283
4.95k
      ((pWORD8)pstr_aac_scratch) + ia_enhaacplus_enc_aac_enc_scr_size();
2284
4.95k
  ptr_resampler_scratch = ((pWORD8)pstr_aac_scratch) + ia_enhaacplus_enc_aac_enc_scr_size() +
2285
4.95k
                          ixheaace_sbr_enc_scr_size();
2286
2287
  /* fill pstr_ancillary data */
2288
4.95k
  pstr_ancillary->anc_flag = anc_flag;
2289
4.95k
  pstr_ancillary->anc_mode = anc_mode;
2290
4.95k
  pstr_ancillary->anc_rate = anc_rate;
2291
4.95k
  pstr_api_struct->pstr_state->temp_buff_aac =
2292
4.95k
      (void *)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_SCRATCH_IDX];
2293
4.95k
  pstr_api_struct->pstr_state->temp_buff_sbr =
2294
4.95k
      (void *)((WORD8 *)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_SCRATCH_IDX] +
2295
4.95k
               ia_enhaacplus_enc_aac_enc_scr_size());
2296
2297
4.95k
  if (pstr_api_struct->config[ele_idx].use_mps &&
2298
608
      pstr_api_struct->config[ele_idx].mps_tree_config != TREE_212) {
2299
443
    pstr_api_struct->pstr_state->mps_scratch =
2300
443
        (FLOAT32 *)((WORD8 *)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_SCRATCH_IDX] +
2301
443
                    ia_enhaacplus_enc_aac_enc_scr_size());
2302
2303
443
    pstr_api_struct->pstr_state->ptr_temp_buff_resamp =
2304
443
        (void *)((WORD8 *)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_SCRATCH_IDX] +
2305
443
                 ia_enhaacplus_enc_aac_enc_scr_size() + ixheaace_sbr_enc_scr_size());
2306
4.51k
  } else {
2307
4.51k
    pstr_api_struct->pstr_state->ptr_temp_buff_resamp =
2308
4.51k
        (void *)((WORD8 *)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_SCRATCH_IDX] +
2309
4.51k
                 ia_enhaacplus_enc_aac_enc_scr_size() + ixheaace_sbr_enc_scr_size());
2310
4.51k
  }
2311
2312
4.95k
  if (pstr_api_struct->config[ele_idx].chmode_nchannels == 2) {
2313
    /* When the chmode option is not used,
2314
      the number of channels depends on the input file */
2315
2.44k
    pstr_api_struct->config[ele_idx].chmode_nchannels =
2316
2.44k
        pstr_api_struct->config[ele_idx].i_channels;
2317
2.44k
  }
2318
2319
4.95k
  switch (pstr_api_struct->pstr_state->aot) {
2320
1.07k
    case AOT_AAC_LC:
2321
2.86k
    case AOT_SBR:
2322
3.11k
    case AOT_PS:
2323
3.11k
      pstr_api_struct->pstr_state->buffer_offset = INPUT_DELAY_LC;
2324
3.11k
      break;
2325
2326
533
    case AOT_AAC_LD:
2327
533
      if (pstr_aac_config->flag_framelength_small == 1) {
2328
244
        pstr_api_struct->pstr_state->buffer_offset = INPUT_DELAY_LD_480;
2329
289
      } else {
2330
289
        pstr_api_struct->pstr_state->buffer_offset = INPUT_DELAY_LD_512;
2331
289
      }
2332
533
      break;
2333
2334
1.30k
    case AOT_AAC_ELD:
2335
1.30k
      if (pstr_aac_config->flag_framelength_small == 1) {
2336
592
        pstr_api_struct->pstr_state->buffer_offset = INPUT_DELAY_ELD_480;
2337
714
      } else {
2338
714
        pstr_api_struct->pstr_state->buffer_offset = INPUT_DELAY_ELD_512;
2339
714
      }
2340
1.30k
      break;
2341
4.95k
  }
2342
4.95k
  pstr_api_struct->pstr_state->downsample[ele_idx] = 0;
2343
4.95k
  pstr_api_struct->config->frame_count = 0;
2344
4.95k
  core_ch = pstr_api_struct->config[ele_idx].chmode_nchannels;
2345
4.95k
  pstr_aac_config->sample_rate = pstr_api_struct->config[ele_idx].sample_rate;
2346
4.95k
  pstr_aac_config->core_sample_rate = pstr_api_struct->config[ele_idx].sample_rate;
2347
4.95k
  pstr_aac_config->native_sample_rate = pstr_api_struct->config[ele_idx].native_sample_rate;
2348
4.95k
  pstr_aac_config->num_in_channels = pstr_api_struct->config[ele_idx].i_channels;
2349
4.95k
  pstr_aac_config->num_out_channels = pstr_api_struct->config[ele_idx].chmode_nchannels;
2350
4.95k
  if (pstr_api_struct->config[ele_idx].aac_classic == 0) {
2351
3.34k
    pstr_aac_config->core_sample_rate = pstr_aac_config->core_sample_rate / 2;
2352
3.34k
  }
2353
4.95k
  if (pstr_api_struct->pstr_state->mps_enable) {
2354
560
    switch (pstr_api_struct->pstr_state->mps_tree_config) {
2355
161
      case TREE_212:
2356
286
      case TREE_5151:
2357
325
      case TREE_5152:
2358
325
        pstr_aac_config->num_out_channels = 1;
2359
325
        core_ch = 1;
2360
325
        break;
2361
2362
235
      case TREE_525:
2363
235
        pstr_aac_config->num_out_channels = 2;
2364
235
        core_ch = 2;
2365
235
        break;
2366
560
    }
2367
560
  }
2368
2369
4.95k
  if (pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2370
247
    pstr_api_struct->config[ele_idx].chmode_nchannels = 2;
2371
247
    pstr_aac_config->num_out_channels = 1;
2372
247
    core_ch = 1;
2373
247
    pstr_api_struct->config[ele_idx].element_type = ID_SCE;
2374
247
  }
2375
4.95k
  if ((pstr_api_struct->config[ele_idx].i_channels == 2) &&
2376
2.44k
      (pstr_api_struct->config[ele_idx].chmode == 0)) {
2377
0
    pstr_api_struct->config[ele_idx].chmode_nchannels = 1;
2378
0
    pstr_aac_config->num_out_channels = 1;
2379
0
  }
2380
4.95k
  if ((pstr_api_struct->config[ele_idx].use_parametric_stereo) &&
2381
247
      (pstr_api_struct->config[ele_idx].aac_classic)) {
2382
0
    return (IA_EXHEAACE_CONFIG_FATAL_AAC_CLASSIC_WITH_PS);
2383
0
  }
2384
2385
4.95k
  if (!(pstr_api_struct->config[ele_idx].adts_flag) &&
2386
3.28k
      (pstr_api_struct->config[0].aac_config.calc_crc)) {
2387
    // set here default for crc as 0
2388
0
    pstr_api_struct->config[0].aac_config.calc_crc = 0;
2389
0
    return (IA_EXHEAACE_CONFIG_NONFATAL_INVALID_CONFIG);
2390
0
  }
2391
2392
4.95k
  if ((pstr_api_struct->config[0].chmode == 3) &&
2393
0
      (pstr_api_struct->config[0].aac_config.calc_crc == 1)) {
2394
    // dual mono case so crc if enabled must be disabled
2395
0
    pstr_api_struct->config[0].aac_config.calc_crc = 0;
2396
0
    return (IA_EXHEAACE_CONFIG_NONFATAL_INVALID_CONFIG);
2397
0
  }
2398
2399
4.95k
  pstr_aac_config->bit_rate = ixheaac_min32(360000 * core_ch, pstr_aac_config->bit_rate);
2400
4.95k
  pstr_aac_config->bit_rate = ixheaac_max32(8000 * core_ch, pstr_aac_config->bit_rate);
2401
4.95k
  switch (pstr_api_struct->pstr_state->aot) {
2402
1.07k
    case AOT_AAC_LC:
2403
2.86k
    case AOT_SBR:
2404
3.11k
    case AOT_PS:
2405
3.11k
      pstr_aac_config->bit_rate = ia_enhaacplus_aac_limitbitrate(
2406
3.11k
          pstr_aac_config->core_sample_rate,
2407
3.11k
          (pstr_aac_config->flag_framelength_small ? FRAME_LEN_960 : FRAME_LEN_1024), core_ch,
2408
3.11k
          pstr_aac_config->bit_rate);
2409
3.11k
      break;
2410
2411
533
    case AOT_AAC_LD:
2412
1.83k
    case AOT_AAC_ELD:
2413
1.83k
      pstr_aac_config->bit_rate = ia_enhaacplus_aac_limitbitrate(
2414
1.83k
          pstr_aac_config->core_sample_rate,
2415
1.83k
          (pstr_aac_config->flag_framelength_small ? FRAME_LEN_480 : FRAME_LEN_512), core_ch,
2416
1.83k
          pstr_aac_config->bit_rate);
2417
1.83k
      break;
2418
4.95k
  }
2419
4.95k
  if (pstr_api_struct->config[ele_idx].eldsbr_found == 1) {
2420
3.34k
    WORD32 num_iter = 0;
2421
3.34k
    WORD32 initial_bitrate, adjusted_bitrate;
2422
3.34k
    adjusted_bitrate = pstr_aac_config->bit_rate;
2423
2424
    /* Find total bitrate which provides valid configuration for each SBR element. */
2425
3.80k
    do {
2426
3.80k
      WORD32 e;
2427
3.80k
      WORD32 q_format;
2428
3.80k
      initial_bitrate = adjusted_bitrate;
2429
2430
11.4k
      for (e = 0; e < pstr_api_struct->config[ele_idx].num_bs_elements; e++) {
2431
8.52k
        WORD32 sbr_element_bitate_in, sbr_bitrate_out;
2432
2433
8.52k
        sbr_element_bitate_in = pstr_aac_config->bit_rate;
2434
8.52k
        sbr_bitrate_out = ixheaace_sbr_limit_bitrate(
2435
8.52k
            sbr_element_bitate_in, core_ch, pstr_aac_config->core_sample_rate,
2436
8.52k
            pstr_api_struct->spectral_band_replication_tabs.ptr_qmf_tab,
2437
8.52k
            pstr_api_struct->pstr_state->aot);
2438
2439
8.52k
        if (sbr_bitrate_out == 0) {
2440
0
          pstr_aac_config->bit_rate = 0;
2441
0
        }
2442
2443
        /* If bitrates don't match, distribution and limiting needs to be determined again.
2444
        Abort element loop and restart with adapted bitrate. */
2445
8.52k
        if (sbr_element_bitate_in != sbr_bitrate_out) {
2446
912
          if (sbr_element_bitate_in < sbr_bitrate_out) {
2447
912
            adjusted_bitrate = ixheaac_max32(
2448
912
                initial_bitrate,
2449
912
                (WORD32)ixheaac_div32((WORD32)(sbr_bitrate_out + 8), MAX_32, &q_format));
2450
912
            adjusted_bitrate = adjusted_bitrate >> (q_format - 31);
2451
912
            break;
2452
912
          }
2453
2454
0
          if (sbr_element_bitate_in > sbr_bitrate_out) {
2455
0
            adjusted_bitrate = ixheaac_min32(
2456
0
                initial_bitrate,
2457
0
                (WORD32)ixheaac_div32((WORD32)(sbr_bitrate_out - 8), MAX_32, &q_format));
2458
0
            break;
2459
0
          }
2460
2461
0
        } /* sbr_element_bitate_in != sbr_bitrate_out */
2462
2463
8.52k
      } /* elements */
2464
2465
3.80k
      num_iter++; /* restrict iteration to worst case of num elements */
2466
2467
3.80k
    } while ((initial_bitrate != adjusted_bitrate) &&
2468
456
             (num_iter <= pstr_api_struct->config[ele_idx].num_bs_elements));
2469
2470
    /* Unequal bitrates mean that no reasonable bitrate configuration found. */
2471
3.34k
    pstr_aac_config->bit_rate = (initial_bitrate == adjusted_bitrate) ? adjusted_bitrate : 0;
2472
3.34k
  }
2473
2474
4.95k
  pstr_asc->str_aac_config.frame_length_flag = pstr_aac_config->flag_framelength_small;
2475
4.95k
  pstr_asc->sampling_frequency = pstr_api_struct->config[0].native_sample_rate;
2476
4.95k
  asc_channels = pstr_api_struct->config[ele_idx].i_channels_mode;
2477
4.95k
  pstr_asc->channel_configuration =
2478
4.95k
      (pstr_api_struct->pstr_state->mps_enable ? core_ch : asc_channels);
2479
4.95k
  pstr_asc->channel_configuration =
2480
4.95k
      pstr_api_struct->config[ele_idx].use_parametric_stereo
2481
4.95k
          ? 1
2482
4.95k
          : (pstr_api_struct->pstr_state->mps_enable
2483
4.70k
                 ? core_ch
2484
4.70k
                 : pstr_api_struct->config[ele_idx].i_channels_mode);
2485
2486
4.95k
  if (!(pstr_api_struct->config[ele_idx].aac_classic) &&
2487
3.34k
      ixheaace_is_sbr_setting_available(
2488
3.34k
          pstr_aac_config->bit_rate,
2489
3.34k
          (pstr_api_struct->pstr_state->mps_enable
2490
3.34k
               ? 1
2491
3.34k
               : pstr_api_struct->config[ele_idx].chmode_nchannels),
2492
3.34k
          pstr_aac_config->sample_rate, &core_sample_rate,
2493
3.34k
          pstr_api_struct->spectral_band_replication_tabs.ptr_qmf_tab,
2494
3.34k
          pstr_api_struct->pstr_state->aot)) {
2495
3.32k
    ixheaace_str_sbr_cfg spectral_band_replication_config;
2496
3.32k
    ixheaace_initialize_sbr_defaults(&spectral_band_replication_config);
2497
2498
3.32k
    spectral_band_replication_config.use_ps =
2499
3.32k
        pstr_api_struct->config[ele_idx].use_parametric_stereo;
2500
3.32k
    spectral_band_replication_config.crc_sbr = 0;
2501
3.32k
    spectral_band_replication_config.parametric_coding = 1;
2502
3.32k
    spectral_band_replication_config.is_esbr = pstr_api_struct->config[0].esbr_flag;
2503
3.32k
    if (pstr_api_struct->pstr_state->aot == AOT_AAC_ELD) {
2504
1.29k
      spectral_band_replication_config.is_ld_sbr = 1;
2505
1.29k
      spectral_band_replication_config.sbr_codec = ELD_SBR;
2506
1.29k
      spectral_band_replication_config.frame_flag_480 = pstr_aac_config->flag_framelength_small;
2507
2.02k
    } else if (pstr_api_struct->pstr_state->aot == AOT_SBR ||
2508
2.02k
               pstr_api_struct->pstr_state->aot == AOT_PS) {
2509
2.02k
      spectral_band_replication_config.frame_flag_960 = pstr_aac_config->flag_framelength_small;
2510
2.02k
      spectral_band_replication_config.sbr_codec = HEAAC_SBR;
2511
2.02k
    }
2512
2513
3.32k
    if (pstr_api_struct->config[ele_idx].aac_classic == 0) {
2514
3.32k
      pstr_api_struct->pstr_state->downsample[ele_idx] = 1;
2515
3.32k
    }
2516
2517
3.32k
    if (pstr_api_struct->pstr_state->mps_enable) {
2518
556
      if (pstr_api_struct->pstr_state->mps_tree_config == TREE_212) {
2519
157
        WORD32 delay =
2520
157
            ((pstr_aac_config->flag_framelength_small ? FRAME_LEN_480 / 2 : FRAME_LEN_512 / 2) *
2521
157
                 2 +
2522
157
             4);
2523
157
        ixheaace_mps_212_memory_struct *pstr_mps_memory;
2524
157
        pstr_mps_memory = pstr_api_struct->pstr_state->mps_pers_mem;
2525
157
        ixheaace_mps_212_open(&pstr_api_struct->pstr_mps_212_enc, pstr_mps_memory);
2526
2527
157
        pstr_asc->str_aac_config.num_sac_cfg_bits = 0;
2528
2529
157
        error = ixheaace_mps_212_initialise(
2530
157
            pstr_api_struct->pstr_mps_212_enc, AOT_AAC_ELD, pstr_aac_config->sample_rate,
2531
157
            &pstr_aac_config->bit_rate,
2532
157
            pstr_api_struct->config[ele_idx].eldsbr_found ? 2 /*hAacConfig->sbrRatio*/ : 0,
2533
157
            (pstr_aac_config->flag_framelength_small ? FRAME_LEN_480 * 2 : FRAME_LEN_512 * 2),
2534
            /* for dual rate sbr this value is already multiplied by 2 */
2535
157
            (pstr_aac_config->flag_framelength_small
2536
157
                 ? FRAME_LEN_480 * 2
2537
157
                 : FRAME_LEN_512 * 2) /* samples read per ch*/,
2538
157
            delay, (WORD8 *)pstr_api_struct->pstr_state->ptr_temp_buff_resamp);
2539
157
        if (error) {
2540
0
          return error;
2541
0
        }
2542
2543
157
        pstr_asc->str_aac_config.num_sac_cfg_bits = ixheaace_mps_212_get_spatial_specific_config(
2544
157
            pstr_api_struct->pstr_mps_212_enc, (WORD8 *)pstr_asc->str_aac_config.sac_cfg_data,
2545
157
            sizeof(pstr_asc->str_aac_config.sac_cfg_data), AOT_AAC_ELD);
2546
157
        pstr_asc->str_aac_config.eld_ext_type[0] = IAAC_ELDEXT_LDSAC;
2547
157
        pstr_asc->str_aac_config.eld_ext_len[0] =
2548
157
            (pstr_asc->str_aac_config.num_sac_cfg_bits + 7) >> 3;
2549
399
      } else {
2550
399
        WORD32 tree_config;
2551
399
        WORD32 bits_written;
2552
399
        ixheaace_bit_buf bit_buf;
2553
399
        ixheaace_bit_buf_handle ptr_bit_buf;
2554
399
        ixheaace_mps_515_memory_struct *pstr_mps_memory;
2555
399
        if (pstr_api_struct->pstr_state->mps_tree_config == TREE_525) {
2556
235
          tree_config = 525;
2557
235
        } else if (pstr_api_struct->pstr_state->mps_tree_config == TREE_5152) {
2558
39
          tree_config = 5152;
2559
125
        } else {
2560
125
          tree_config = 5151;
2561
125
        }
2562
399
        pstr_mps_memory = pstr_api_struct->pstr_state->mps_515_pers_mem;
2563
399
        ptr_bit_buf = ia_enhaacplus_enc_create_bitbuffer(
2564
399
            &bit_buf, (UWORD8 *)pstr_asc->str_aac_config.sac_cfg_data,
2565
399
            sizeof(pstr_asc->str_aac_config.sac_cfg_data));
2566
2567
399
        error = ixheaace_mps_515_open(
2568
399
            &pstr_api_struct->pstr_mps_515_enc, pstr_aac_config->sample_rate, tree_config,
2569
399
            ptr_bit_buf, &bits_written, pstr_mps_memory, pstr_aac_config->flag_framelength_small);
2570
399
        pstr_asc->str_aac_config.num_sac_cfg_bits = bits_written;
2571
399
        pstr_asc->str_aac_config.eld_ext_type[0] = IAAC_ELDEXT_LDSAC;
2572
399
        pstr_asc->str_aac_config.eld_ext_len[0] = (bits_written + 7) >> 3;
2573
399
        if (error) {
2574
0
          return error;
2575
0
        }
2576
399
      }
2577
556
    }
2578
2579
3.32k
    ixheaace_adjust_sbr_settings(
2580
3.32k
        &spectral_band_replication_config, pstr_aac_config->bit_rate,
2581
3.32k
        pstr_aac_config->num_out_channels, core_sample_rate, AACENC_TRANS_FAC, 24000,
2582
3.32k
        pstr_api_struct->spectral_band_replication_tabs.ptr_qmf_tab,
2583
3.32k
        pstr_api_struct->pstr_state->aot, (pstr_api_struct->config[0].ccfl_idx == SBR_4_1));
2584
2585
3.32k
    if (pstr_api_struct->config[ele_idx].element_type != ID_LFE) {
2586
      /* open SBR PART, set core bandwidth */
2587
3.05k
      error = ixheaace_env_open(
2588
3.05k
          &pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[ele_idx],
2589
3.05k
          &spectral_band_replication_config, &pstr_aac_config->band_width,
2590
3.05k
          ptr_spectral_band_replication_scratch,
2591
3.05k
          &(pstr_api_struct->spectral_band_replication_tabs),
2592
3.05k
          &pstr_asc->str_aac_config.sbr_config);
2593
3.05k
      pstr_asc->str_aac_config.ld_sbr_present_flag =
2594
3.05k
          pstr_api_struct->config[ele_idx].eldsbr_found;
2595
3.05k
      if (error) {
2596
0
        return error;
2597
0
      }
2598
3.05k
      pstr_asc->str_aac_config.ld_sbr_sample_rate =
2599
3.05k
          (spectral_band_replication_config.codec_settings.sample_freq !=
2600
3.05k
           pstr_aac_config->sample_rate)
2601
3.05k
              ? 1
2602
3.05k
              : 0;
2603
3.05k
      pstr_asc->str_aac_config.ld_sbr_crc_flag = spectral_band_replication_config.crc_sbr;
2604
3.05k
    }
2605
2606
3.32k
    if (!pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2607
3.07k
      IA_ERRORCODE resamp_error = IA_NO_ERROR;
2608
3.07k
      WORD32 ch_idx = 0;
2609
2610
3.07k
      resamp_error = ia_enhaacplus_enc_init_iir_resampler(
2611
3.07k
          &(pstr_api_struct->pstr_state->down_sampler[ele_idx][ch_idx]), 2,
2612
3.07k
          pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab);
2613
3.07k
      if (resamp_error) {
2614
0
        return resamp_error;
2615
0
      }
2616
3.07k
      if (pstr_api_struct->config[ele_idx].i_channels > 1) {
2617
1.81k
        resamp_error = ia_enhaacplus_enc_init_iir_resampler(
2618
1.81k
            &(pstr_api_struct->pstr_state->down_sampler[ele_idx][ch_idx + 1]), 2,
2619
1.81k
            pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab);
2620
1.81k
        if (resamp_error) {
2621
0
          return resamp_error;
2622
0
        }
2623
1.81k
      }
2624
3.07k
    }
2625
3.32k
  } else {
2626
1.63k
    if (!(pstr_api_struct->config[ele_idx].aac_classic &&
2627
1.60k
          !(pstr_api_struct->config[ele_idx].eldsbr_found))) {
2628
27
      if (pstr_api_struct->config[ele_idx].aac_classic == 0) {
2629
27
        error = IA_EXHEAACE_INIT_FATAL_AACPLUS_NOT_AVAIL;
2630
27
      } else {
2631
0
        error = IA_EXHEAACE_INIT_FATAL_BITRATE_NOT_SUPPORTED;
2632
0
      }
2633
27
      return error;
2634
27
    }
2635
1.60k
    pstr_api_struct->pstr_state->buffer_offset = 0;
2636
1.60k
    pstr_api_struct->config[ele_idx].aac_classic = 1;
2637
1.60k
  }
2638
4.92k
  {
2639
4.92k
    WORD32 *shared_buf1, *shared_buf2;
2640
4.92k
    WORD32 *shared_buf3;
2641
4.92k
    WORD8 *shared_buf4;
2642
4.92k
    switch (pstr_api_struct->pstr_state->aot) {
2643
1.07k
      case AOT_AAC_LC:
2644
2.85k
      case AOT_SBR:
2645
3.09k
      case AOT_PS:
2646
3.09k
        ia_enhaacplus_enc_get_shared_bufs(
2647
3.09k
            ptr_spectral_band_replication_scratch, &shared_buf1, &shared_buf2, &shared_buf3,
2648
3.09k
            &shared_buf4,
2649
3.09k
            (pstr_aac_config->flag_framelength_small == 1) ? FRAME_LEN_960 : FRAME_LEN_1024);
2650
3.09k
        break;
2651
2652
533
      case AOT_AAC_LD:
2653
1.82k
      case AOT_AAC_ELD:
2654
1.82k
        ia_enhaacplus_enc_get_shared_bufs(
2655
1.82k
            ptr_spectral_band_replication_scratch, &shared_buf1, &shared_buf2, &shared_buf3,
2656
1.82k
            &shared_buf4,
2657
1.82k
            (pstr_aac_config->flag_framelength_small == 1) ? FRAME_LEN_480 : FRAME_LEN_512);
2658
1.82k
        break;
2659
4.92k
    }
2660
2661
4.92k
    ia_enhaacplus_enc_set_shared_bufs(pstr_aac_scratch, &shared_buf1, &shared_buf2, &shared_buf3,
2662
4.92k
                                      &ptr_resampler_scratch);
2663
2664
4.92k
    error = ia_enhaacplus_enc_aac_enc_open(
2665
4.92k
        &(pstr_api_struct->pstr_state->aac_enc_pers_mem[ele_idx]),
2666
4.92k
        *pstr_aac_config /*, *pstr_ancillary*/, pstr_aac_scratch,
2667
4.92k
        &(pstr_api_struct->pstr_aac_tabs), pstr_api_struct->config[ele_idx].element_type,
2668
4.92k
        pstr_api_struct->config[ele_idx].element_instance_tag, pstr_api_struct->pstr_state->aot);
2669
4.92k
    if (error != IA_NO_ERROR) {
2670
0
      return error;
2671
0
    }
2672
4.92k
    if (error) {
2673
0
      if (pstr_api_struct->pstr_mps_212_enc && pstr_api_struct->pstr_state->mps_enable) {
2674
0
        ixheaace_mps_212_close(&(pstr_api_struct->pstr_mps_212_enc));
2675
0
      }
2676
0
      if (pstr_api_struct->pstr_mps_515_enc && pstr_api_struct->pstr_state->mps_enable) {
2677
0
        ixheaace_mps_515_close(pstr_api_struct->pstr_mps_212_enc);
2678
0
      }
2679
2680
0
      return (IA_EXHEAACE_INIT_FATAL_AAC_INIT_FAILED);
2681
0
    }
2682
4.92k
  }
2683
2684
4.92k
  return error;
2685
4.92k
}
2686
2687
static IA_ERRORCODE ia_enhaacplus_enc_execute(ixheaace_api_struct *pstr_api_struct,
2688
307k
                                              WORD32 ele_idx) {
2689
307k
  IA_ERRORCODE error = IA_NO_ERROR;
2690
307k
  WORD32 downsample;
2691
307k
  WORD32 aot;
2692
307k
  WORD32 idx;
2693
307k
  WORD32 header_bytes = 0;
2694
307k
  WORD32 num_samples_read;
2695
307k
  WORD32 env_read_offset = 0;
2696
307k
  WORD32 write_off_set = 0;
2697
307k
  WORD32 aacenc_blocksize;
2698
307k
  WORD32 ch, out_samples;
2699
307k
  WORD32 total_channels = 0, ele, slot;
2700
307k
  WORD32 time_in_stride;
2701
307k
  WORD32 num_bs_elements;
2702
307k
  FLAG flag_last_element;
2703
307k
  WORD32 i_num_coup_channels;
2704
307k
  WORD32 i_channels_mask;
2705
307k
  ixheaace_pstr_sbr_enc pstr_sbr_encoder = NULL;
2706
307k
  iexheaac_encoder_str **pstr_aac_enc;
2707
307k
  iaace_config *pstr_aac_config;
2708
307k
  pWORD16 pw_inp_buf = NULL;
2709
307k
  pUWORD8 pub_out_buf = NULL;
2710
307k
  FLOAT32 *ptr_input_buffer = NULL;
2711
307k
  FLOAT32 *ptr_input_buffer_mps = NULL;
2712
307k
  FLOAT32 *shared_buf1_ring, *shared_buf2_ring;
2713
307k
  WORD32 out_stride = IXHEAACE_MAX_CH_IN_BS_ELE;
2714
307k
  WORD32 *pstr_aac_scratch = (pWORD32)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_SCRATCH_IDX];
2715
307k
  WORD8 *ptr_spectral_band_replication_scratch =
2716
307k
      ((pWORD8)pstr_aac_scratch) + ia_enhaacplus_enc_aac_enc_scr_size();
2717
307k
  WORD32 *total_fill_bits = &(pstr_api_struct->pstr_state->total_fill_bits);
2718
307k
  WORD32 *write_program_config_element =
2719
307k
      &(pstr_api_struct->config[0].write_program_config_element);
2720
2721
307k
  aot = pstr_api_struct->pstr_state->aot;
2722
307k
  num_bs_elements = pstr_api_struct->config[0].num_bs_elements;
2723
307k
  flag_last_element = (ele_idx == (num_bs_elements - 1));
2724
307k
  i_num_coup_channels = pstr_api_struct->config[0].i_num_coupling_chan;
2725
307k
  i_channels_mask = pstr_api_struct->config[0].i_channels_mask;
2726
307k
  aacenc_blocksize = pstr_api_struct->config[0].frame_length;
2727
307k
  downsample = pstr_api_struct->pstr_state->downsample[ele_idx];
2728
307k
  num_samples_read =
2729
307k
      aacenc_blocksize * pstr_api_struct->config[ele_idx].i_native_channels * (1 << downsample);
2730
1.12M
  for (ele = 0; ele < num_bs_elements; ele++) {
2731
813k
    total_channels += pstr_api_struct->config[ele].i_native_channels;
2732
813k
  }
2733
307k
  pstr_aac_config = &(pstr_api_struct->config[ele_idx].aac_config);
2734
307k
  if ((pstr_api_struct->config[ele_idx].aac_classic == 0) &&
2735
217k
      pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2736
23.2k
    time_in_stride = 1;
2737
284k
  } else if ((pstr_api_struct->config[ele_idx].aac_classic == 1) &&
2738
90.2k
             (pstr_api_struct->config[ele_idx].num_bs_elements == 1)) {
2739
9.93k
    time_in_stride = pstr_aac_config->num_out_channels;
2740
274k
  } else {
2741
274k
    time_in_stride = IXHEAACE_MAX_CH_IN_BS_ELE;
2742
274k
  }
2743
307k
  pstr_aac_enc = pstr_api_struct->pstr_state->aac_enc_pers_mem;
2744
2745
307k
  pw_inp_buf = (pWORD16)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_INPUT_IDX];
2746
307k
  if (ele_idx == 0) {
2747
159k
    pstr_api_struct->pstr_state->i_out_bytes = 0;
2748
159k
    pub_out_buf = ((pUWORD8)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_OUTPUT_IDX]);
2749
159k
    *total_fill_bits = 0;
2750
159k
  }
2751
2752
307k
  if (pstr_api_struct->config->adts_flag) {
2753
86.5k
    pub_out_buf = (pUWORD8)(pub_out_buf + 7);
2754
86.5k
  }
2755
2756
923k
  for (ch = 0; ch < IXHEAACE_MAX_CH_IN_BS_ELE; ch++)
2757
615k
    pstr_api_struct->pstr_state->num_anc_data_bytes[ele_idx][ch] = 0;
2758
2759
307k
  if (aot == AOT_SBR || aot == AOT_PS) {
2760
137k
    write_off_set = INPUT_DELAY_LC * IXHEAACE_MAX_CH_IN_BS_ELE;
2761
170k
  } else if (aot == AOT_AAC_ELD && pstr_api_struct->pstr_state->mps_enable != 1) {
2762
47.2k
    if (pstr_api_struct->config[0].aac_config.flag_framelength_small)
2763
17.7k
      write_off_set = INPUT_DELAY_ELD_480 * IXHEAACE_MAX_CH_IN_BS_ELE;
2764
29.5k
    else
2765
29.5k
      write_off_set = INPUT_DELAY_ELD_512 * IXHEAACE_MAX_CH_IN_BS_ELE;
2766
122k
  } else if (aot == AOT_AAC_ELD && pstr_api_struct->pstr_state->mps_enable == 1 &&
2767
32.4k
             pstr_api_struct->pstr_state->mps_tree_config == TREE_212) {
2768
12.4k
    if (pstr_api_struct->config[0].aac_config.flag_framelength_small)
2769
8.71k
      write_off_set = INPUT_DELAY_ELDV2_480 * IXHEAACE_MAX_CH_IN_BS_ELE;
2770
3.76k
    else
2771
3.76k
      write_off_set = INPUT_DELAY_ELDV2_512 * IXHEAACE_MAX_CH_IN_BS_ELE;
2772
110k
  } else if (aot == AOT_AAC_ELD && pstr_api_struct->pstr_state->mps_enable == 1 &&
2773
20.0k
             pstr_api_struct->pstr_state->mps_tree_config != TREE_212) {
2774
20.0k
    write_off_set = INPUT_DELAY_ELD_512_MPS * IXHEAACE_MAX_CH_IN_BS_ELE;
2775
20.0k
  }
2776
307k
  if (pstr_api_struct->config[ele_idx].aac_classic == 1) {
2777
90.2k
    write_off_set = 0;
2778
90.2k
  }
2779
307k
  if (aot == AOT_AAC_ELD && pstr_api_struct->pstr_state->mps_enable == 1) {
2780
32.4k
    if (pstr_api_struct->pstr_state->mps_tree_config == TREE_212) {
2781
12.4k
      env_read_offset = INPUT_DELAY_ELD_512_MPS;
2782
20.0k
    } else {
2783
20.0k
      env_read_offset = write_off_set;
2784
20.0k
    }
2785
32.4k
  }
2786
307k
  if (pstr_api_struct->pstr_state->downsample[ele_idx]) {
2787
217k
    if (!pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2788
194k
      if (aot == AOT_AAC_ELD && pstr_api_struct->pstr_state->mps_enable != 1) {
2789
47.2k
        write_off_set +=
2790
47.2k
            4 * IXHEAACE_MAX_CH_IN_BS_ELE;  // Downsampler delay = 4 samples per channel @input SR
2791
147k
      } else {
2792
147k
        write_off_set += (pstr_api_struct->pstr_state->down_sampler[ele_idx][0].delay) *
2793
147k
                         IXHEAACE_MAX_CH_IN_BS_ELE;
2794
147k
      }
2795
194k
    }
2796
217k
    if (pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2797
23.2k
      env_read_offset = (MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY) * IXHEAACE_MAX_CH_IN_BS_ELE;
2798
23.2k
      write_off_set = env_read_offset;
2799
23.2k
    }
2800
217k
  }
2801
2802
307k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
2803
203k
    ptr_input_buffer = pstr_api_struct->pstr_state->inp_delay +
2804
203k
                       (aacenc_blocksize * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LC) *
2805
203k
                           IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2806
203k
  } else if (aot == AOT_AAC_LD) {
2807
24.4k
    if (pstr_api_struct->config[0].aac_config.flag_framelength_small) {
2808
9.44k
      ptr_input_buffer = pstr_api_struct->pstr_state->inp_delay +
2809
9.44k
                         (FRAME_LEN_480 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LD_480) *
2810
9.44k
                             IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2811
15.0k
    } else {
2812
15.0k
      ptr_input_buffer = pstr_api_struct->pstr_state->inp_delay +
2813
15.0k
                         (FRAME_LEN_512 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LD_512) *
2814
15.0k
                             IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2815
15.0k
    }
2816
79.7k
  } else if (aot == AOT_AAC_ELD) {
2817
79.7k
    if (pstr_api_struct->config[0].aac_config.flag_framelength_small) {
2818
36.1k
      if (pstr_api_struct->pstr_state->mps_enable == 1) {
2819
18.3k
        ptr_input_buffer =
2820
18.3k
            pstr_api_struct->pstr_state->inp_delay +
2821
18.3k
            (FRAME_LEN_480 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_ELDV2_480) *
2822
18.3k
                IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2823
18.3k
      } else {
2824
17.7k
        ptr_input_buffer =
2825
17.7k
            pstr_api_struct->pstr_state->inp_delay +
2826
17.7k
            (FRAME_LEN_480 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_ELD_480) *
2827
17.7k
                IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2828
17.7k
      }
2829
43.6k
    } else {
2830
43.6k
      if (pstr_api_struct->pstr_state->mps_enable == 1) {
2831
14.1k
        ptr_input_buffer =
2832
14.1k
            pstr_api_struct->pstr_state->inp_delay +
2833
14.1k
            (FRAME_LEN_512 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_ELDV2_512) *
2834
14.1k
                IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2835
29.5k
      } else {
2836
29.5k
        ptr_input_buffer =
2837
29.5k
            pstr_api_struct->pstr_state->inp_delay +
2838
29.5k
            (FRAME_LEN_512 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_ELD_512) *
2839
29.5k
                IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2840
29.5k
      }
2841
43.6k
    }
2842
79.7k
  } else {
2843
0
    return IA_EXHEAACE_EXE_FATAL_UNSUPPORTED_AOT;
2844
0
  }
2845
2846
307k
  if (aot != AOT_AAC_LD && aot != AOT_AAC_LC) {
2847
217k
    pstr_sbr_encoder =
2848
217k
        pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[ele_idx];
2849
217k
    if (pstr_api_struct->config[ele_idx].element_type != ID_LFE) {
2850
204k
      ixheaace_sbr_set_scratch_ptr(pstr_sbr_encoder, ptr_spectral_band_replication_scratch);
2851
204k
    }
2852
217k
  }
2853
2854
307k
  {
2855
307k
    ixheaace_mps_enc_ext_payload mps_extension_payload;
2856
307k
    UWORD8 *mps_bs = pstr_api_struct->pstr_state->mps_bs;
2857
307k
    memset(&mps_extension_payload, 0, sizeof(ixheaace_mps_enc_ext_payload));
2858
307k
    mps_extension_payload.p_data = mps_bs;
2859
2860
307k
    if ((pstr_api_struct->config[ele_idx].num_bs_elements == 1) &&
2861
80.7k
        (pstr_api_struct->config[ele_idx].i_channels <= 2)) {
2862
60.7k
      if (pstr_api_struct->config[ele_idx].aac_classic != 1) {
2863
50.7k
        if ((pstr_api_struct->config[ele_idx].i_channels == 2 &&
2864
46.0k
             pstr_api_struct->config[ele_idx].chmode_nchannels == 2) &&
2865
46.0k
            (!((pstr_api_struct->pstr_mps_212_enc != NULL) &&
2866
33.6k
               pstr_api_struct->pstr_state->mps_enable))) {
2867
118M
          for (idx = 0; idx < (num_samples_read); idx++) {
2868
118M
            ptr_input_buffer[write_off_set + idx] = (FLOAT32)pw_inp_buf[idx];
2869
118M
          }
2870
33.6k
        } else if (pstr_api_struct->config[ele_idx].i_channels == 1) {
2871
9.00M
          for (idx = 0; idx < num_samples_read; idx++) {
2872
8.99M
            ptr_input_buffer[write_off_set + (IXHEAACE_MAX_CH_IN_BS_ELE * idx)] =
2873
8.99M
                (FLOAT32)pw_inp_buf[idx];
2874
8.99M
          }
2875
12.4k
        } else if ((pstr_api_struct->pstr_mps_212_enc != NULL) &&
2876
12.4k
                   pstr_api_struct->pstr_state->mps_enable) {
2877
12.4k
          ptr_input_buffer_mps = pstr_api_struct->pstr_state->time_signal_mps;
2878
12.2M
          for (idx = 0; idx < (num_samples_read / 2); idx++) {
2879
12.2M
            ptr_input_buffer_mps[idx] = (FLOAT32)pw_inp_buf[2 * idx];
2880
12.2M
            ptr_input_buffer_mps[(num_samples_read / 2) + idx] =
2881
12.2M
                (FLOAT32)pw_inp_buf[(2 * idx) + 1];
2882
12.2M
          }
2883
12.4k
        }
2884
50.7k
      } else {
2885
11.5M
        for (idx = 0; idx < (num_samples_read + write_off_set); idx++) {
2886
11.5M
          ptr_input_buffer[idx] = (FLOAT32)pw_inp_buf[idx];
2887
11.5M
        }
2888
9.93k
      }
2889
247k
    } else {
2890
247k
      if (pstr_api_struct->config[ele_idx].i_channels == 2) {
2891
114k
        slot = pstr_api_struct->config[ele_idx].element_slot;
2892
162M
        for (idx = 0; idx < num_samples_read / 2; idx++) {
2893
162M
          ptr_input_buffer[2 * idx + write_off_set] =
2894
162M
              (FLOAT32)pw_inp_buf[total_channels * idx + slot];
2895
162M
          ptr_input_buffer[2 * idx + write_off_set + 1] =
2896
162M
              (FLOAT32)pw_inp_buf[total_channels * idx + slot + 1];
2897
162M
        }
2898
114k
      }
2899
2900
247k
      if (pstr_api_struct->config[ele_idx].i_channels == 1) {
2901
112k
        slot = pstr_api_struct->config[ele_idx].element_slot;
2902
161M
        for (idx = 0; idx < num_samples_read; idx++) {
2903
161M
          ptr_input_buffer[write_off_set + (IXHEAACE_MAX_CH_IN_BS_ELE * idx)] =
2904
161M
              (FLOAT32)pw_inp_buf[total_channels * idx + slot];
2905
161M
        }
2906
112k
      }
2907
2908
247k
      if (pstr_api_struct->config[ele_idx].i_channels == 6) {
2909
20.0k
        ptr_input_buffer_mps = pstr_api_struct->pstr_state->time_signal_mps;
2910
119M
        for (idx = 0; idx < num_samples_read; idx++) {
2911
119M
          ptr_input_buffer_mps[idx] = (FLOAT32)pw_inp_buf[idx];
2912
119M
        }
2913
20.0k
      }
2914
247k
    }
2915
2916
307k
    if ((pstr_api_struct->pstr_mps_212_enc != NULL) && pstr_api_struct->pstr_state->mps_enable) {
2917
12.4k
      ptr_input_buffer_mps = pstr_api_struct->pstr_state->time_signal_mps;
2918
12.4k
      error = ixheaace_mps_212_process(pstr_api_struct->pstr_mps_212_enc, ptr_input_buffer_mps,
2919
12.4k
                                       num_samples_read, &mps_extension_payload);
2920
12.4k
      if (error) {
2921
0
        return error;
2922
0
      }
2923
12.4k
      num_samples_read /= 2;
2924
12.2M
      for (idx = 0; idx < num_samples_read; idx++) {
2925
12.2M
        ptr_input_buffer[2 * idx + write_off_set] = (FLOAT32)ptr_input_buffer_mps[idx];
2926
12.2M
      }
2927
12.4k
      env_read_offset = write_off_set;
2928
12.4k
    }
2929
307k
    if ((pstr_api_struct->pstr_mps_515_enc != NULL) && pstr_api_struct->pstr_state->mps_enable) {
2930
20.0k
      ixheaace_bit_buf bit_buf;
2931
20.0k
      ixheaace_bit_buf_handle ptr_bit_buf = NULL;
2932
20.0k
      FLOAT32 *ptr_downmix_buffer_mps = pstr_api_struct->pstr_state->mps_scratch;
2933
20.0k
      VOID *ptr_scratch_515_mps = (VOID *)(pstr_api_struct->pstr_state->mps_scratch +
2934
20.0k
                                           (MAX_INPUT_CHANNELS * MAX_BUFFER_SIZE) +
2935
20.0k
                                           (MAX_OUTPUT_CHANNELS * MAX_BUFFER_SIZE));
2936
20.0k
      ptr_bit_buf = ia_enhaacplus_enc_create_bitbuffer(&bit_buf, mps_bs, MAX_MPS_BS_PAYLOAD_SIZE);
2937
2938
20.0k
      error =
2939
20.0k
          ixheaace_mps_515_apply(pstr_api_struct->pstr_mps_515_enc, &ptr_input_buffer_mps[0],
2940
20.0k
                                 &ptr_downmix_buffer_mps[0], ptr_bit_buf, ptr_scratch_515_mps);
2941
20.0k
      if (error) {
2942
0
        return error;
2943
0
      }
2944
20.0k
      mps_extension_payload.data_size = ptr_bit_buf->cnt_bits;
2945
20.0k
      mps_extension_payload.data_type = IXHEAACE_MPS_EXT_LDSAC_DATA;
2946
20.0k
      mps_extension_payload.associated_ch_element = -1;
2947
2948
20.0k
      if (pstr_api_struct->pstr_state->mps_tree_config == TREE_5151 ||
2949
14.4k
          pstr_api_struct->pstr_state->mps_tree_config == TREE_5152) {
2950
7.29k
        num_samples_read /= 6;
2951
7.23M
        for (idx = 0; idx < num_samples_read; idx++) {
2952
7.23M
          ptr_input_buffer[2 * idx + write_off_set] = (FLOAT32)ptr_downmix_buffer_mps[idx];
2953
7.23M
        }
2954
12.7k
      } else {
2955
12.7k
        num_samples_read /= 3;
2956
25.2M
        for (idx = 0; idx < num_samples_read; idx++) {
2957
25.2M
          ptr_input_buffer[idx + write_off_set] = (FLOAT32)ptr_downmix_buffer_mps[idx];
2958
25.2M
        }
2959
12.7k
      }
2960
20.0k
      env_read_offset = write_off_set;
2961
20.0k
    }
2962
2963
307k
    if (pstr_api_struct->pstr_state->downsample[ele_idx]) {
2964
217k
      ixheaace_resampler_scratch *pstr_scratch_resampler =
2965
217k
          (ixheaace_resampler_scratch *)pstr_api_struct->pstr_state->ptr_temp_buff_resamp;
2966
2967
217k
      if (pstr_api_struct->config[ele_idx].element_type != ID_LFE) {
2968
204k
        error = ixheaace_env_encode_frame(
2969
204k
            pstr_sbr_encoder, ptr_input_buffer + env_read_offset, ptr_input_buffer,
2970
204k
            IXHEAACE_MAX_CH_IN_BS_ELE,
2971
204k
            &(pstr_api_struct->pstr_state->num_anc_data_bytes[ele_idx][0]),
2972
204k
            pstr_api_struct->pstr_state->anc_data_bytes[ele_idx],
2973
204k
            &(pstr_api_struct->spectral_band_replication_tabs), &(pstr_api_struct->common_tabs),
2974
204k
            &(mps_extension_payload.p_data[0]), mps_extension_payload.data_size,
2975
204k
            pstr_api_struct->config[0].aac_config.flag_framelength_small, NULL);
2976
2977
204k
        if (error != IA_NO_ERROR) {
2978
1.47k
          return error;
2979
1.47k
        }
2980
204k
      }
2981
2982
216k
      if (!pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2983
479k
        for (ch = 0; ch < pstr_aac_config->num_out_channels; ch++) {
2984
286k
          ia_enhaacplus_enc_get_scratch_bufs(pstr_api_struct->pstr_state->temp_buff_sbr,
2985
286k
                                             &shared_buf1_ring, &shared_buf2_ring);
2986
286k
          {
2987
286k
            ia_enhaacplus_enc_iir_downsampler(
2988
286k
                &(pstr_api_struct->pstr_state->down_sampler[ele_idx][ch]),
2989
286k
                ptr_input_buffer + write_off_set + ch,
2990
286k
                num_samples_read / pstr_aac_config->num_out_channels, IXHEAACE_MAX_CH_IN_BS_ELE,
2991
286k
                ptr_input_buffer + ch, &out_samples, out_stride, shared_buf1_ring,
2992
286k
                shared_buf2_ring, pstr_scratch_resampler);
2993
286k
          }
2994
286k
        }
2995
192k
      }
2996
216k
    }
2997
2998
306k
    error = ia_enhaacplus_enc_aac_core_encode(
2999
306k
        pstr_aac_enc, ptr_input_buffer, time_in_stride,
3000
306k
        pstr_api_struct->pstr_state->anc_data_bytes[ele_idx],
3001
306k
        pstr_api_struct->pstr_state->num_anc_data_bytes[ele_idx], pub_out_buf,
3002
306k
        &(pstr_api_struct->pstr_state->i_out_bytes), &(pstr_api_struct->pstr_aac_tabs),
3003
306k
        pstr_api_struct->pstr_state->pstr_bit_stream_handle,
3004
306k
        &(pstr_api_struct->pstr_state->bit_stream), flag_last_element,
3005
306k
        write_program_config_element, i_num_coup_channels, i_channels_mask, ele_idx,
3006
306k
        total_fill_bits, total_channels, aot, pstr_api_struct->config->adts_flag,
3007
306k
        num_bs_elements, &pstr_api_struct->pstr_state->is_quant_spec_zero,
3008
306k
        &pstr_api_struct->pstr_state->is_gain_limited);
3009
306k
    if (error != IA_NO_ERROR) {
3010
31.4k
      return error;
3011
31.4k
    }
3012
274k
    if (pstr_sbr_encoder && !(pstr_api_struct->config[ele_idx].use_parametric_stereo)) {
3013
158k
      if (pstr_sbr_encoder && (ptr_input_buffer != NULL)) {
3014
158k
        memmove(ptr_input_buffer,
3015
158k
                ptr_input_buffer + aacenc_blocksize * 2 * IXHEAACE_MAX_CH_IN_BS_ELE,
3016
158k
                write_off_set * sizeof(ptr_input_buffer[0]));
3017
158k
      }
3018
158k
    }
3019
274k
  }
3020
0
  {}
3021
  /*ADTS Header Write*/
3022
274k
  if (pstr_api_struct->config->adts_flag) {
3023
81.3k
    pub_out_buf = ((pUWORD8)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_OUTPUT_IDX]);
3024
81.3k
    {
3025
81.3k
      WORD32 num_channels = 0;
3026
3027
329k
      for (ele = 0; ele < pstr_api_struct->config[0].num_bs_elements; ele++) {
3028
248k
        num_channels += pstr_api_struct->config[ele].i_channels;
3029
248k
      }
3030
81.3k
      {
3031
81.3k
        header_bytes = ia_enhaacplus_enc_write_ADTS_header(
3032
81.3k
            pub_out_buf, pstr_api_struct->pstr_state->i_out_bytes,
3033
81.3k
            pstr_api_struct->config->aac_config.core_sample_rate,
3034
81.3k
            (pstr_api_struct->config[0].num_bs_elements > 1)
3035
81.3k
                ? num_channels
3036
81.3k
                : pstr_api_struct->config[0].aac_config.num_out_channels);
3037
81.3k
      }
3038
81.3k
    }
3039
3040
81.3k
    pstr_api_struct->pstr_state->i_out_bytes += header_bytes;
3041
81.3k
  }
3042
3043
274k
  return IA_NO_ERROR;
3044
306k
}
3045
333k
static IA_ERRORCODE iusace_process(ixheaace_api_struct *pstr_api_struct) {
3046
333k
  IA_ERRORCODE error = IA_NO_ERROR;
3047
333k
  WORD32 idx;
3048
333k
  WORD32 write_off_set = 0;
3049
333k
  WORD32 core_coder_frame_length;
3050
333k
  WORD32 usac_independency_flg;
3051
333k
  UWORD32 padding_bits = 0;
3052
333k
  WORD32 core_sample;
3053
333k
  WORD32 drc_sample;
3054
333k
  WORD32 ptr_inp_buf_offset = 0;
3055
333k
  WORD32 num_ch;
3056
333k
  WORD16 *ps_inp_buf = NULL;
3057
333k
  WORD8 *ps_out_buf = NULL;
3058
333k
  FLOAT32 *ptr_input_buffer = NULL;
3059
333k
  FLOAT32 *ptr_inp_buf[MAX_TIME_CHANNELS];
3060
333k
  FLOAT32 *ptr_drc_inp_buf[MAX_TIME_CHANNELS];
3061
333k
  WORD32 delay = 0;
3062
333k
  ixheaace_state_struct *pstr_state = pstr_api_struct->pstr_state;
3063
333k
  ia_bit_buf_struct *pstr_it_bit_buff = &pstr_state->str_bit_buf;
3064
333k
  ia_usac_encoder_config_struct *pstr_config = &pstr_api_struct->config[0].usac_config;
3065
333k
  ia_usac_data_struct *pstr_usac_data = &pstr_api_struct->pstr_state->str_usac_enc_data;
3066
333k
  iusace_scratch_mem *pstr_scratch = &pstr_usac_data->str_scratch;
3067
333k
  ia_classification_struct *pstr_sig_class_data =
3068
333k
      &pstr_state->str_usac_enc_data.str_sig_class_data;
3069
333k
  core_sample = (pstr_config->ccfl * pstr_config->channels);
3070
333k
  drc_sample = pstr_config->drc_frame_size * pstr_config->channels;
3071
333k
  core_coder_frame_length = pstr_config->ccfl;
3072
333k
  num_ch = pstr_config->channels;
3073
333k
  usac_independency_flg = pstr_usac_data->usac_independency_flag;
3074
333k
  ps_inp_buf = (WORD16 *)pstr_api_struct->pp_mem[IA_MEMTYPE_INPUT];
3075
333k
  ps_out_buf = (WORD8 *)pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT];
3076
3077
333k
  if (pstr_config->use_drc_element) {
3078
73.5k
    if (pstr_config->use_delay_adjustment == 1) {
3079
68.1k
      delay = (CC_DELAY_ADJUSTMENT * pstr_config->drc_frame_size / FRAME_LEN_1024) * num_ch;
3080
68.1k
    }
3081
223M
    for (idx = 0; idx < core_sample + delay; idx++) {
3082
223M
      pstr_api_struct->pstr_state->pp_drc_in_buf[idx % num_ch][idx / num_ch] =
3083
223M
          pstr_api_struct->pstr_state
3084
223M
              ->pp_drc_in_buf[idx % num_ch][idx / num_ch + pstr_config->drc_frame_size];
3085
223M
    }
3086
73.5k
    ptr_inp_buf_offset = pstr_config->drc_frame_size;
3087
204k
    for (idx = 0; idx < num_ch; idx++) {
3088
130k
      ptr_drc_inp_buf[idx] = pstr_api_struct->pstr_state->pp_drc_in_buf[idx];
3089
130k
    }
3090
73.5k
  }
3091
3092
333k
  ixheaace_pstr_sbr_enc pstr_sbr_encoder =
3093
333k
      pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[0];
3094
333k
  if (pstr_config->sbr_enable) {
3095
200k
    ixheaace_mps_enc_ext_payload mps_extension_payload;
3096
200k
    UWORD8 *mps_bs = pstr_api_struct->pstr_state->mps_bs;
3097
200k
    FLOAT32 *time_signal_mps = pstr_api_struct->pstr_state->time_signal_mps;
3098
200k
    WORD32 sbr_pvc_mode = 0;
3099
200k
    WORD32 sbr_patching_mode = 1;
3100
200k
    WORD32 ccfl_size;
3101
200k
    WORD32 num_samples_read;
3102
200k
    WORD32 out_samples, ch;
3103
200k
    WORD32 resamp_ratio =
3104
200k
        ia_enhaacplus_enc_compute_resampling_ratio(pstr_api_struct->config[0].ccfl_idx);
3105
200k
    switch (pstr_config->codec_mode) {
3106
88.9k
      case USAC_SWITCHED:
3107
88.9k
        if (pstr_usac_data->str_sig_class_data.coding_mode == 2) {
3108
15.3k
          sbr_pvc_mode = 0;
3109
73.5k
        } else {
3110
73.5k
          sbr_pvc_mode = 2;
3111
73.5k
        }
3112
88.9k
        sbr_patching_mode = 1;
3113
88.9k
        break;
3114
108k
      case USAC_ONLY_FD:
3115
108k
        sbr_pvc_mode = 0;
3116
108k
        sbr_patching_mode = 0;
3117
108k
        break;
3118
3.15k
      case USAC_ONLY_TD:
3119
3.15k
        sbr_pvc_mode = 2;
3120
3.15k
        sbr_patching_mode = 1;
3121
3.15k
        break;
3122
200k
    }
3123
3124
200k
    write_off_set = INPUT_DELAY_LC * IXHEAACE_MAX_CH_IN_BS_ELE;
3125
3126
200k
    if (pstr_api_struct->config[0].ccfl_idx == SBR_4_1) {
3127
49.1k
      write_off_set = write_off_set * 2;
3128
49.1k
    }
3129
3130
200k
    if (pstr_api_struct->pstr_state->downsample[0]) {
3131
200k
      if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
3132
28.9k
        write_off_set +=
3133
28.9k
            (pstr_api_struct->pstr_state->down_samp_sos[0][0].delay) * IXHEAACE_MAX_CH_IN_BS_ELE;
3134
3135
28.9k
        write_off_set +=
3136
28.9k
            (pstr_api_struct->pstr_state->up_sampler[0][0].delay) * IXHEAACE_MAX_CH_IN_BS_ELE;
3137
171k
      } else if (pstr_api_struct->config[0].ccfl_idx == SBR_2_1 ||
3138
171k
                 pstr_api_struct->config[0].ccfl_idx == SBR_4_1) {
3139
171k
        write_off_set +=
3140
171k
            (pstr_api_struct->pstr_state->down_sampler[0][0].delay) * IXHEAACE_MAX_CH_IN_BS_ELE;
3141
171k
      }
3142
200k
    }
3143
3144
200k
    ptr_input_buffer = pstr_api_struct->pstr_state->inp_delay;
3145
200k
    ccfl_size = pstr_api_struct->config[0].usac_config.ccfl;
3146
200k
    num_samples_read = ccfl_size * pstr_api_struct->config[0].i_channels;
3147
200k
    switch (pstr_api_struct->config[0].ccfl_idx) {
3148
28.9k
      case SBR_8_3:
3149
28.9k
        num_samples_read *= 8;
3150
28.9k
        num_samples_read /= 3;
3151
28.9k
        break;
3152
3153
122k
      case SBR_2_1:
3154
122k
        num_samples_read *= 2;
3155
122k
        break;
3156
3157
49.1k
      case SBR_4_1:
3158
49.1k
        num_samples_read *= 4;
3159
49.1k
        break;
3160
200k
    }
3161
3162
200k
    mps_extension_payload.p_data = mps_bs;
3163
200k
    memset(&mps_extension_payload, 0, sizeof(ixheaace_mps_enc_ext_payload));
3164
3165
200k
    if ((pstr_api_struct->pstr_mps_212_enc != NULL) && pstr_api_struct->pstr_state->mps_enable) {
3166
175M
      for (idx = 0; idx < num_samples_read / 2; idx++) {
3167
175M
        time_signal_mps[idx] = (FLOAT32)ps_inp_buf[2 * idx];
3168
175M
        time_signal_mps[num_samples_read / 2 + idx] = (FLOAT32)ps_inp_buf[2 * idx + 1];
3169
175M
      }
3170
73.5k
      ixheaace_mps_pstr_struct pstr_mps_enc =
3171
73.5k
          (ixheaace_mps_pstr_struct)pstr_api_struct->pstr_mps_212_enc;
3172
73.5k
      pstr_mps_enc->ptr_sac_encoder->independency_flag = usac_independency_flg;
3173
3174
73.5k
      error = ixheaace_mps_212_process(pstr_api_struct->pstr_mps_212_enc, time_signal_mps,
3175
73.5k
                                       num_samples_read, &mps_extension_payload);
3176
73.5k
      if (error) {
3177
0
        return error;
3178
0
      }
3179
73.5k
      if (pstr_api_struct->pstr_state->mps_enable == 1) {
3180
175M
        for (idx = 0; idx < num_samples_read / 2; idx++) {
3181
175M
          ptr_input_buffer[write_off_set + 2 * idx] = time_signal_mps[idx];
3182
175M
          ptr_input_buffer[write_off_set + 2 * idx + 1] =
3183
175M
              time_signal_mps[num_samples_read / 2 + idx];
3184
175M
        }
3185
73.5k
      }
3186
127k
    } else if (pstr_api_struct->config[0].i_channels == 2 &&
3187
66.6k
               pstr_api_struct->config[0].chmode_nchannels == 2) {
3188
338M
      for (idx = 0; idx < (num_samples_read); idx++) {
3189
338M
        ptr_input_buffer[write_off_set + idx] = (FLOAT32)ps_inp_buf[idx];
3190
338M
      }
3191
66.6k
    } else if (pstr_api_struct->config[0].i_channels == 1) {
3192
167M
      for (idx = 0; idx < num_samples_read; idx++) {
3193
167M
        ptr_input_buffer[write_off_set + (IXHEAACE_MAX_CH_IN_BS_ELE * idx)] =
3194
167M
            (FLOAT32)ps_inp_buf[idx];
3195
167M
      }
3196
60.5k
    }
3197
3198
200k
    if (num_ch == 2) {
3199
140k
      if (1 == pstr_config->use_drc_element) {
3200
149M
        for (idx = 0; idx < drc_sample; idx++) {
3201
149M
          ptr_drc_inp_buf[idx % num_ch][(idx >> 1) + ptr_inp_buf_offset] = ptr_input_buffer[idx];
3202
149M
        }
3203
36.4k
      }
3204
3205
      // update Header and bit-stream parameters
3206
140k
      if (0 == pstr_config->sbr_pvc_active) {
3207
98.9k
        sbr_pvc_mode = 0;
3208
98.9k
      }
3209
3210
140k
      ixheaace_set_usac_sbr_params(
3211
140k
          pstr_sbr_encoder, usac_independency_flg, 0, pstr_config->sbr_pvc_active, sbr_pvc_mode,
3212
140k
          pstr_config->sbr_inter_tes_active, pstr_config->sbr_harmonic, sbr_patching_mode);
3213
3214
      // Downsample SBR input buffer for Harmonic SBR
3215
140k
      if (pstr_config->sbr_harmonic) {
3216
100k
        FLOAT32 *in_buffer_temp;
3217
100k
        ixheaace_get_input_scratch_buf(pstr_api_struct->pstr_state->ptr_temp_buff_resamp,
3218
100k
                                       &in_buffer_temp);
3219
100k
        FLOAT32 *outbuf = ixheaace_get_hbe_resample_buffer(pstr_sbr_encoder);
3220
100k
        if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
3221
0
          WORD32 input_tot = num_samples_read / pstr_api_struct->config[0].i_channels;
3222
0
          ixheaace_upsampling_inp_buf_generation(ptr_input_buffer, in_buffer_temp, input_tot,
3223
0
                                                 UPSAMPLE_FAC, 0);
3224
0
        }
3225
3226
        // Resampler
3227
301k
        for (ch = 0; ch < num_ch; ch++) {
3228
200k
          FLOAT32 *shared_buf1_ring, *shared_buf2_ring;
3229
200k
          ixheaace_resampler_scratch *pstr_scratch_resampler =
3230
200k
              (ixheaace_resampler_scratch *)(ixheaace_resampler_scratch *)
3231
200k
                  pstr_api_struct->pstr_state->ptr_temp_buff_resamp;
3232
3233
200k
          ia_enhaacplus_enc_get_scratch_bufs(pstr_api_struct->pstr_state->temp_buff_sbr,
3234
200k
                                             &shared_buf1_ring, &shared_buf2_ring);
3235
3236
200k
          if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
3237
            // Upsampling by factor 3 - SOS implementation
3238
0
            ia_enhaacplus_enc_iir_sos_upsampler(
3239
0
                &(pstr_api_struct->pstr_state->hbe_up_sampler[0][ch]), in_buffer_temp + ch,
3240
0
                num_samples_read / pstr_api_struct->config[0].i_channels,
3241
0
                IXHEAACE_MAX_CH_IN_BS_ELE, in_buffer_temp + ch, &out_samples, shared_buf1_ring,
3242
0
                shared_buf2_ring, pstr_scratch_resampler);
3243
3244
            // Downsampling by factor 8
3245
0
            ia_enhaacplus_enc_iir_sos_downsampler(
3246
0
                &(pstr_api_struct->pstr_state->hbe_down_samp_sos[0][ch]), in_buffer_temp + ch,
3247
0
                out_samples, IXHEAACE_MAX_CH_IN_BS_ELE, outbuf + ch, &out_samples,
3248
0
                shared_buf1_ring, shared_buf2_ring, pstr_scratch_resampler);
3249
200k
          } else {
3250
200k
            WORD32 out_stride = IXHEAACE_MAX_CH_IN_BS_ELE * resamp_ratio;
3251
3252
200k
            ia_enhaacplus_enc_iir_downsampler(
3253
200k
                &(pstr_api_struct->pstr_state->hbe_down_sampler[0][ch]), ptr_input_buffer + ch,
3254
200k
                num_samples_read / pstr_api_struct->config[0].i_channels,
3255
200k
                IXHEAACE_MAX_CH_IN_BS_ELE, outbuf + ch, &out_samples, out_stride,
3256
200k
                shared_buf1_ring, shared_buf2_ring, pstr_scratch_resampler);
3257
200k
          }
3258
200k
        }
3259
100k
      }
3260
3261
      // SBR Encode
3262
140k
      error = ixheaace_env_encode_frame(
3263
140k
          pstr_sbr_encoder, ptr_input_buffer, ptr_input_buffer,
3264
140k
          pstr_api_struct->config[0].i_channels,
3265
140k
          &(pstr_api_struct->pstr_state->num_anc_data_bytes[0][0]),
3266
140k
          pstr_api_struct->pstr_state->anc_data_bytes[0],
3267
140k
          &(pstr_api_struct->spectral_band_replication_tabs), &(pstr_api_struct->common_tabs),
3268
140k
          &(mps_extension_payload.p_data[0]), mps_extension_payload.data_size, 0,
3269
140k
          &pstr_api_struct->pstr_state->str_usac_enc_data.num_sbr_bits);
3270
140k
      if (error != IA_NO_ERROR) {
3271
7.02k
        return error;
3272
7.02k
      }
3273
140k
    } else {
3274
60.5k
      if (0 == pstr_config->sbr_pvc_active) {
3275
34.7k
        sbr_pvc_mode = 0;
3276
34.7k
      }
3277
3278
60.5k
      ixheaace_set_usac_sbr_params(
3279
60.5k
          pstr_sbr_encoder, usac_independency_flg, 0, pstr_config->sbr_pvc_active, sbr_pvc_mode,
3280
60.5k
          pstr_config->sbr_inter_tes_active, pstr_config->sbr_harmonic, sbr_patching_mode);
3281
60.5k
      if (pstr_config->sbr_harmonic) {
3282
6.32k
        FLOAT32 *in_buffer_temp;
3283
6.32k
        ixheaace_get_input_scratch_buf(pstr_api_struct->pstr_state->ptr_temp_buff_resamp,
3284
6.32k
                                       &in_buffer_temp);
3285
6.32k
        FLOAT32 *outbuf = ixheaace_get_hbe_resample_buffer(pstr_sbr_encoder);
3286
6.32k
        if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
3287
0
          WORD32 input_tot = num_samples_read / pstr_api_struct->config[0].i_channels;
3288
0
          ixheaace_upsampling_inp_buf_generation(ptr_input_buffer, in_buffer_temp, input_tot,
3289
0
                                                 UPSAMPLE_FAC, 0);
3290
0
        }
3291
3292
        // Resampler
3293
12.6k
        for (ch = 0; ch < num_ch; ch++) {
3294
6.32k
          FLOAT32 *shared_buf1_ring, *shared_buf2_ring;
3295
6.32k
          ixheaace_resampler_scratch *pstr_scratch_resampler =
3296
6.32k
              (ixheaace_resampler_scratch *)(ixheaace_resampler_scratch *)
3297
6.32k
                  pstr_api_struct->pstr_state->ptr_temp_buff_resamp;
3298
3299
6.32k
          ia_enhaacplus_enc_get_scratch_bufs(pstr_api_struct->pstr_state->temp_buff_sbr,
3300
6.32k
                                             &shared_buf1_ring, &shared_buf2_ring);
3301
3302
6.32k
          if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
3303
            // Upsampling by factor 3 - SOS implementation
3304
0
            ia_enhaacplus_enc_iir_sos_upsampler(
3305
0
                &(pstr_api_struct->pstr_state->hbe_up_sampler[0][ch]), in_buffer_temp + ch,
3306
0
                num_samples_read / pstr_api_struct->config[0].i_channels,
3307
0
                IXHEAACE_MAX_CH_IN_BS_ELE, in_buffer_temp + ch, &out_samples, shared_buf1_ring,
3308
0
                shared_buf2_ring, pstr_scratch_resampler);
3309
3310
            // Downsampling by factor 8
3311
0
            ia_enhaacplus_enc_iir_sos_downsampler(
3312
0
                &(pstr_api_struct->pstr_state->hbe_down_samp_sos[0][ch]), in_buffer_temp + ch,
3313
0
                out_samples, IXHEAACE_MAX_CH_IN_BS_ELE, outbuf + ch, &out_samples,
3314
0
                shared_buf1_ring, shared_buf2_ring, pstr_scratch_resampler);
3315
6.32k
          } else {
3316
6.32k
            WORD32 out_stride = IXHEAACE_MAX_CH_IN_BS_ELE * resamp_ratio;
3317
3318
6.32k
            ia_enhaacplus_enc_iir_downsampler(
3319
6.32k
                &(pstr_api_struct->pstr_state->hbe_down_sampler[0][ch]),
3320
6.32k
                ptr_input_buffer /*input_buffer_fix + write_off_set*/ + ch,
3321
6.32k
                num_samples_read / pstr_api_struct->config[0].i_channels,
3322
6.32k
                IXHEAACE_MAX_CH_IN_BS_ELE, outbuf + ch, &out_samples, out_stride,
3323
6.32k
                shared_buf1_ring, shared_buf2_ring, pstr_scratch_resampler);
3324
6.32k
          }
3325
6.32k
        }
3326
6.32k
      }
3327
3328
60.5k
      FLOAT32 *time_signal = pstr_api_struct->pstr_state->time_signal;
3329
167M
      for (idx = 0; idx < num_samples_read; idx++) {
3330
167M
        time_signal[idx] = (FLOAT32)ptr_input_buffer[idx << 1];
3331
167M
      }
3332
3333
60.5k
      if (1 == pstr_config->use_drc_element) {
3334
16.2M
        for (idx = 0; idx < drc_sample; idx++) {
3335
16.2M
          ptr_drc_inp_buf[0][idx + ptr_inp_buf_offset] = time_signal[idx];
3336
16.2M
        }
3337
7.92k
      }
3338
3339
      // SBR Encode
3340
60.5k
      error = ixheaace_env_encode_frame(
3341
60.5k
          pstr_sbr_encoder, time_signal, time_signal, pstr_api_struct->config[0].i_channels,
3342
60.5k
          &(pstr_api_struct->pstr_state->num_anc_data_bytes[0][0]),
3343
60.5k
          pstr_api_struct->pstr_state->anc_data_bytes[0],
3344
60.5k
          &(pstr_api_struct->spectral_band_replication_tabs), &(pstr_api_struct->common_tabs),
3345
60.5k
          &(mps_extension_payload.p_data[0]), mps_extension_payload.data_size, 0,
3346
60.5k
          &pstr_api_struct->pstr_state->str_usac_enc_data.num_sbr_bits);
3347
60.5k
      if (error != IA_NO_ERROR) {
3348
2.00k
        return error;
3349
2.00k
      }
3350
60.5k
    }
3351
3352
    /* Resampling for USAC core */
3353
191k
    {
3354
191k
      FLOAT32 *in_buffer_temp;
3355
191k
      ixheaace_get_input_scratch_buf(pstr_api_struct->pstr_state->ptr_temp_buff_resamp,
3356
191k
                                     &in_buffer_temp);
3357
191k
      if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
3358
28.9k
        WORD32 input_tot = num_samples_read / pstr_api_struct->config[0].i_channels;
3359
28.9k
        ixheaace_upsampling_inp_buf_generation(ptr_input_buffer, in_buffer_temp, input_tot,
3360
28.9k
                                               UPSAMPLE_FAC, write_off_set - delay);
3361
28.9k
      }
3362
3363
516k
      for (ch = 0; ch < num_ch; ch++) {
3364
324k
        FLOAT32 *shared_buf1_ring, *shared_buf2_ring;
3365
324k
        ixheaace_resampler_scratch *pstr_scratch_resampler =
3366
324k
            (ixheaace_resampler_scratch *)pstr_api_struct->pstr_state->ptr_temp_buff_resamp;
3367
3368
324k
        ia_enhaacplus_enc_get_scratch_bufs(pstr_api_struct->pstr_state->temp_buff_sbr,
3369
324k
                                           &shared_buf1_ring, &shared_buf2_ring);
3370
3371
324k
        if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
3372
          // Upsampling by factor 3 - SOS implementation
3373
39.2k
          ia_enhaacplus_enc_iir_sos_upsampler(
3374
39.2k
              &(pstr_api_struct->pstr_state->up_sampler[0][ch]), in_buffer_temp + ch,
3375
39.2k
              num_samples_read / pstr_api_struct->config[0].i_channels, IXHEAACE_MAX_CH_IN_BS_ELE,
3376
39.2k
              in_buffer_temp + ch, &out_samples, shared_buf1_ring, shared_buf2_ring,
3377
39.2k
              pstr_scratch_resampler);
3378
3379
          // Downsampling by factor 8
3380
39.2k
          ia_enhaacplus_enc_iir_sos_downsampler(
3381
39.2k
              &(pstr_api_struct->pstr_state->down_samp_sos[0][ch]), in_buffer_temp + ch,
3382
39.2k
              out_samples, IXHEAACE_MAX_CH_IN_BS_ELE, ptr_input_buffer + ch, &out_samples,
3383
39.2k
              shared_buf1_ring, shared_buf2_ring, pstr_scratch_resampler);
3384
285k
        } else {
3385
285k
          WORD32 out_stride = IXHEAACE_MAX_CH_IN_BS_ELE * resamp_ratio;
3386
285k
          ia_enhaacplus_enc_iir_downsampler(
3387
285k
              &(pstr_api_struct->pstr_state->down_sampler[0][ch]),
3388
285k
              ptr_input_buffer + write_off_set - delay + ch,
3389
285k
              num_samples_read / pstr_api_struct->config[0].i_channels, IXHEAACE_MAX_CH_IN_BS_ELE,
3390
285k
              ptr_input_buffer + ch, &out_samples, out_stride, shared_buf1_ring, shared_buf2_ring,
3391
285k
              pstr_scratch_resampler);
3392
285k
        }
3393
324k
      }
3394
191k
    }
3395
3396
191k
    if (num_ch != 0) {
3397
516k
      for (idx = 0; idx < num_ch; idx++) {
3398
324k
        ptr_inp_buf[idx] = pstr_api_struct->pstr_state->ptr_in_buf[idx];
3399
324k
      }
3400
3401
191k
      if (num_ch == 1) {
3402
55.2M
        for (idx = 0; idx < core_sample; idx++) {
3403
55.2M
          ptr_inp_buf[0][idx] = ptr_input_buffer[idx << 1];
3404
55.2M
        }
3405
133k
      } else {
3406
267M
        for (idx = 0; idx < core_sample; idx++) {
3407
267M
          ptr_inp_buf[idx % num_ch][idx / num_ch] = ptr_input_buffer[idx];
3408
267M
        }
3409
133k
      }
3410
191k
    }
3411
191k
  } else {
3412
132k
    if (pstr_config->use_delay_adjustment == 1) {
3413
123k
      delay = ((CC_DELAY_ADJUSTMENT * core_coder_frame_length) / FRAME_LEN_1024) * num_ch;
3414
123k
    }
3415
132k
    if (num_ch != 0) {
3416
353k
      for (idx = 0; idx < num_ch; idx++) {
3417
221k
        ptr_inp_buf[idx] = pstr_api_struct->pstr_state->ptr_in_buf[idx];
3418
221k
      }
3419
3420
199M
      for (idx = 0; idx < core_sample; idx++) {
3421
199M
        ptr_inp_buf[idx % num_ch][(idx + delay) / num_ch] = ps_inp_buf[idx];
3422
199M
      }
3423
3424
132k
      if (1 == pstr_config->use_drc_element) {
3425
50.5M
        for (idx = 0; idx < drc_sample; idx++) {
3426
50.4M
          ptr_drc_inp_buf[idx % num_ch][(idx + delay) / num_ch + ptr_inp_buf_offset] =
3427
50.4M
              ps_inp_buf[idx];
3428
50.4M
        }
3429
29.2k
      }
3430
132k
    }
3431
132k
  }
3432
3433
324k
  if (pstr_sig_class_data->is_switch_mode) {
3434
102M
    for (idx = 0; idx < core_coder_frame_length; idx++) {
3435
102M
      pstr_sig_class_data->input_samples[pstr_sig_class_data->n_buffer_samples + idx] =
3436
102M
          pstr_api_struct->pstr_state->ptr_in_buf[0][idx];
3437
102M
    }
3438
108k
    pstr_sig_class_data->n_buffer_samples += core_coder_frame_length;
3439
108k
    iusace_classification(pstr_sig_class_data, pstr_scratch, core_coder_frame_length);
3440
108k
  }
3441
3442
324k
  pstr_it_bit_buff =
3443
324k
      iusace_create_bit_buffer(pstr_it_bit_buff, pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT],
3444
324k
                               pstr_api_struct->pstr_mem_info[IA_MEMTYPE_OUTPUT].ui_size, 1);
3445
324k
  if (pstr_it_bit_buff == NULL) {
3446
0
    return IA_EXHEAACE_INIT_FATAL_USAC_BITBUFFER_INIT_FAILED;
3447
0
  }
3448
324k
  error =
3449
324k
      ixheaace_usac_encode(pstr_api_struct->pstr_state->ptr_in_buf, pstr_config,
3450
324k
                           &pstr_api_struct->pstr_state->str_usac_enc_data,
3451
324k
                           &pstr_api_struct->pstr_state->audio_specific_config, pstr_it_bit_buff,
3452
324k
                           pstr_sbr_encoder, pstr_api_struct->pstr_state->pp_drc_in_buf,
3453
324k
                           &pstr_api_struct->pstr_state->is_quant_spec_zero,
3454
324k
                           &pstr_api_struct->pstr_state->is_gain_limited);
3455
324k
  if (error) return error;
3456
3457
318k
  padding_bits = 8 - (pstr_it_bit_buff->cnt_bits & 7);
3458
318k
  if (padding_bits > 0 && padding_bits < 8) {
3459
274k
    ps_out_buf[pstr_it_bit_buff->cnt_bits >> 3] =
3460
274k
        (WORD8)((UWORD32)ps_out_buf[pstr_it_bit_buff->cnt_bits >> 3]) & (0xFF << padding_bits);
3461
274k
  }
3462
318k
  pstr_api_struct->pstr_state->i_out_bytes =
3463
318k
      (padding_bits > 0 && padding_bits < 8) ? (pstr_it_bit_buff->cnt_bits + padding_bits) >> 3
3464
318k
                                             : pstr_it_bit_buff->cnt_bits >> 3;
3465
318k
  pstr_api_struct->pstr_state->i_out_bits = pstr_it_bit_buff->cnt_bits;
3466
318k
  ixheaace_write_audio_preroll_data(pstr_api_struct, pstr_it_bit_buff);
3467
318k
  pstr_state->str_usac_enc_data.frame_count++;
3468
318k
  pstr_usac_data->usac_independency_flag_count =
3469
318k
      (pstr_usac_data->usac_independency_flag_count + 1) %
3470
318k
      pstr_usac_data->usac_independency_flag_interval;
3471
3472
318k
  if (pstr_config->sbr_enable) {
3473
191k
    WORD32 num_samples = pstr_api_struct->config[0].usac_config.ccfl * IXHEAACE_MAX_CH_IN_BS_ELE;
3474
191k
    switch (pstr_api_struct->config[0].ccfl_idx) {
3475
28.9k
      case SBR_8_3:
3476
28.9k
        num_samples *= 8;
3477
28.9k
        num_samples /= 3;
3478
28.9k
        break;
3479
3480
116k
      case SBR_2_1:
3481
116k
        num_samples *= 2;
3482
116k
        break;
3483
3484
46.1k
      case SBR_4_1:
3485
46.1k
        num_samples *= 4;
3486
46.1k
        break;
3487
191k
    }
3488
3489
191k
    if (ptr_input_buffer != NULL) {
3490
191k
      memmove(ptr_input_buffer, ptr_input_buffer + num_samples,
3491
191k
              write_off_set * sizeof(ptr_input_buffer[0]));
3492
191k
    }
3493
191k
  } else if (!pstr_config->sbr_enable && pstr_config->use_delay_adjustment) {
3494
312k
    for (idx = 0; idx < num_ch; idx++) {
3495
194k
      memmove(&ptr_inp_buf[idx][0], &ptr_inp_buf[idx][core_sample / num_ch],
3496
194k
              sizeof(ptr_inp_buf[idx][0]) * delay / num_ch);
3497
194k
    }
3498
117k
  }
3499
3500
318k
  return IA_NO_ERROR;
3501
318k
}
3502
0
IA_ERRORCODE ixheaace_get_lib_id_strings(pVOID pv_output) {
3503
0
  IA_ERRORCODE err_code = IA_NO_ERROR;
3504
0
  ixheaace_version *pstr_output_config = (ixheaace_version *)pv_output;
3505
0
  pstr_output_config->p_lib_name = (WORD8 *)LIB_NAME;
3506
0
  pstr_output_config->p_version_num = (WORD8 *)ITTIAM_VER;
3507
3508
0
  return err_code;
3509
0
}
3510
3511
static void ixheaace_get_measured_loudness_info(ixheaace_api_struct *pstr_api_struct,
3512
3.53k
                                                ixheaace_input_config *pstr_input_config) {
3513
3.53k
  ia_drc_input_config *pstr_internal_drc_cfg;
3514
3.53k
  ia_drc_uni_drc_config_struct *pstr_uni_drc_config;
3515
3.53k
  ia_drc_loudness_info_set_struct *pstr_enc_loudness_info_set;
3516
3.53k
  WORD32 n, m;
3517
3518
3.53k
  pstr_internal_drc_cfg =
3519
3.53k
      (ia_drc_input_config *)&pstr_api_struct->config[0].usac_config.str_drc_cfg;
3520
3.53k
  if (pstr_input_config->use_drc_element == 0) {
3521
2.62k
    memset(pstr_internal_drc_cfg, 0, sizeof(ia_drc_input_config));
3522
2.62k
  }
3523
3.53k
  pstr_uni_drc_config = &pstr_internal_drc_cfg->str_uni_drc_config;
3524
3.53k
  pstr_enc_loudness_info_set = &pstr_internal_drc_cfg->str_enc_loudness_info_set;
3525
3526
3.53k
  pstr_uni_drc_config->sample_rate_present = 1;
3527
3.53k
  pstr_uni_drc_config->loudness_info_set_present = 1;
3528
3.53k
  pstr_enc_loudness_info_set->loudness_info_count = 1;
3529
3530
7.07k
  for (n = 0; n < pstr_enc_loudness_info_set->loudness_info_count; n++) {
3531
3.53k
    pstr_enc_loudness_info_set->str_loudness_info[n].drc_set_id = 0;
3532
3.53k
    pstr_enc_loudness_info_set->str_loudness_info[n].downmix_id = 0;
3533
3.53k
    pstr_enc_loudness_info_set->str_loudness_info[n].sample_peak_level_present = 1;
3534
3.53k
    pstr_enc_loudness_info_set->str_loudness_info[n].sample_peak_level =
3535
3.53k
        pstr_input_config->sample_peak_level;
3536
3.53k
    pstr_enc_loudness_info_set->str_loudness_info[n].true_peak_level_present = 0;
3537
3.53k
    pstr_enc_loudness_info_set->str_loudness_info[n].measurement_count = 1;
3538
3539
7.07k
    for (m = 0; m < pstr_enc_loudness_info_set->str_loudness_info[n].measurement_count; m++) {
3540
3.53k
      pstr_enc_loudness_info_set->str_loudness_info[n].str_loudness_measure[m].method_definition =
3541
3.53k
          pstr_input_config->method_def;
3542
3.53k
      pstr_enc_loudness_info_set->str_loudness_info[n].str_loudness_measure[m].method_value =
3543
3.53k
          (FLOAT32)pstr_input_config->measured_loudness;
3544
3.53k
      pstr_enc_loudness_info_set->str_loudness_info[n]
3545
3.53k
          .str_loudness_measure[m]
3546
3.53k
          .measurement_system = pstr_input_config->measurement_system;
3547
3.53k
      pstr_enc_loudness_info_set->str_loudness_info[n].str_loudness_measure[m].reliability = 3;
3548
3.53k
    }
3549
3.53k
  }
3550
3.53k
}
3551
3552
7.26k
IA_ERRORCODE ixheaace_allocate(pVOID pv_input, pVOID pv_output) {
3553
7.26k
  IA_ERRORCODE err_code = IA_NO_ERROR;
3554
7.26k
  WORD32 ui_api_size;
3555
7.26k
  pVOID pv_value;
3556
7.26k
  ixheaace_input_config *pstr_input_config = (ixheaace_input_config *)pv_input;
3557
7.26k
  ixheaace_output_config *pstr_output_config = (ixheaace_output_config *)pv_output;
3558
7.26k
  ixheaace_api_struct *pstr_api_struct;
3559
7.26k
  if (1 == pstr_input_config->usac_en) {
3560
4.14k
    pstr_input_config->aot = AOT_USAC;
3561
4.14k
  }
3562
7.26k
  if (pstr_input_config->aot != AOT_AAC_ELD && pstr_input_config->aot != AOT_AAC_LC &&
3563
5.97k
      pstr_input_config->aot != AOT_AAC_LD && pstr_input_config->aot != AOT_SBR &&
3564
5.11k
      pstr_input_config->aot != AOT_PS && pstr_input_config->aot != AOT_USAC) {
3565
386
    return IA_EXHEAACE_API_FATAL_UNSUPPORTED_AOT;
3566
386
  }
3567
6.88k
  ui_api_size = sizeof(ixheaace_api_struct);
3568
6.88k
  pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] =
3569
6.88k
      pstr_output_config->malloc_xheaace(ui_api_size + EIGHT_BYTE_SIZE, DEFAULT_MEM_ALIGN_8);
3570
6.88k
  if (NULL == pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count]) {
3571
0
    return IA_EXHEAACE_API_FATAL_MEM_ALLOC;
3572
0
  }
3573
6.88k
  memset(pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count], 0, ui_api_size);
3574
3575
6.88k
  pstr_output_config->ui_rem =
3576
6.88k
      (SIZE_T)((SIZE_T)pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] %
3577
6.88k
               BYTE_ALIGN_8);
3578
3579
6.88k
  pstr_output_config->pv_ia_process_api_obj =
3580
6.88k
      (pVOID)((WORD8 *)pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] +
3581
6.88k
              BYTE_ALIGN_8 - pstr_output_config->ui_rem);
3582
6.88k
  pstr_output_config->malloc_count++;
3583
3584
6.88k
  pstr_api_struct = (ixheaace_api_struct *)pstr_output_config->pv_ia_process_api_obj;
3585
6.88k
  memset(pstr_api_struct, 0, sizeof(*pstr_api_struct));
3586
3587
6.88k
  ixheaace_set_default_config(pstr_api_struct, pstr_input_config);
3588
3589
6.88k
  err_code = ixheaace_set_config_params(pstr_api_struct, pstr_input_config);
3590
6.88k
  if (err_code) {
3591
400
    return err_code;
3592
400
  }
3593
3594
6.48k
  if (pstr_input_config->aot == AOT_USAC) {
3595
4.07k
    if (pstr_api_struct->config[0].usac_config.is_loudness_configured == 0) {
3596
3.53k
      ixheaace_get_measured_loudness_info(pstr_api_struct, pstr_input_config);
3597
3.53k
      memcpy(pstr_input_config->pv_drc_cfg, &pstr_api_struct->config[0].usac_config.str_drc_cfg,
3598
3.53k
             sizeof(ia_drc_input_config));
3599
3.53k
    }
3600
4.07k
  }
3601
3602
6.48k
  pstr_output_config->ui_proc_mem_tabs_size =
3603
6.48k
      (sizeof(ixheaace_mem_info_struct) + sizeof(pVOID *)) * 4;
3604
6.48k
  pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] =
3605
6.48k
      pstr_output_config->malloc_xheaace(
3606
6.48k
          pstr_output_config->ui_proc_mem_tabs_size + EIGHT_BYTE_SIZE, DEFAULT_MEM_ALIGN_8);
3607
6.48k
  if (NULL == pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count]) {
3608
0
    return IA_EXHEAACE_API_FATAL_MEM_ALLOC;
3609
0
  }
3610
6.48k
  memset(pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count], 0,
3611
6.48k
         pstr_output_config->ui_proc_mem_tabs_size);
3612
3613
6.48k
  pstr_output_config->ui_rem =
3614
6.48k
      (SIZE_T)((SIZE_T)pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] %
3615
6.48k
               BYTE_ALIGN_8);
3616
3617
6.48k
  pv_value =
3618
6.48k
      (pVOID)((WORD8 *)pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] +
3619
6.48k
              BYTE_ALIGN_8 - pstr_output_config->ui_rem);
3620
6.48k
  if (pv_value == NULL) {
3621
0
    return IA_EXHEAACE_API_FATAL_MEM_ALLOC;
3622
0
  }
3623
6.48k
  memset(pv_value, 0, (sizeof(ixheaace_mem_info_struct) + sizeof(pVOID *)) * 4);
3624
3625
6.48k
  pstr_api_struct->pstr_mem_info = (ixheaace_mem_info_struct *)pv_value;
3626
6.48k
  pstr_api_struct->pp_mem = (pVOID *)((WORD8 *)pv_value + sizeof(ixheaace_mem_info_struct) * 4);
3627
3628
6.48k
  pstr_output_config->malloc_count++;
3629
3630
6.48k
  ixheaace_fill_mem_tabs(pstr_api_struct, pstr_input_config->aot);
3631
3632
6.48k
  err_code =
3633
6.48k
      ixheaace_alloc_and_assign_mem(pstr_api_struct, pstr_output_config, pstr_input_config);
3634
6.48k
  if (err_code) {
3635
0
    return err_code;
3636
0
  }
3637
3638
6.48k
  pstr_output_config->is_loudness_configured =
3639
6.48k
      pstr_api_struct->config[0].usac_config.is_loudness_configured;
3640
3641
6.48k
  return err_code;
3642
6.48k
}
3643
3644
6.48k
IA_ERRORCODE ixheaace_init(pVOID pstr_obj_ixheaace, pVOID pv_input, pVOID pv_output) {
3645
6.48k
  IA_ERRORCODE error = IA_NO_ERROR;
3646
6.48k
  WORD32 frame_length;
3647
6.48k
  WORD32 channels, ele_idx;
3648
6.48k
  ixheaace_api_struct *pstr_api_struct = (ixheaace_api_struct *)pstr_obj_ixheaace;
3649
6.48k
  ixheaace_input_config *pstr_input_config = (ixheaace_input_config *)pv_input;
3650
6.48k
  ixheaace_output_config *pstr_output_config = (ixheaace_output_config *)pv_output;
3651
6.48k
  WORD32 total_bitrate_used = 0;
3652
6.48k
  frame_length = pstr_input_config->frame_length;
3653
6.48k
  channels = 0;
3654
15.5k
  for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
3655
9.04k
    channels += pstr_api_struct->config[ele_idx].i_channels;
3656
9.04k
  }
3657
6.48k
  pstr_api_struct->pstr_state->aot = pstr_input_config->aot;
3658
3659
6.48k
  if ((pstr_api_struct->config[0].use_mps == 1) &&
3660
1.78k
      (0 == pstr_api_struct->config->aac_classic ||
3661
1.76k
       pstr_api_struct->pstr_state->aot == AOT_USAC)) {
3662
1.76k
    pstr_api_struct->pstr_state->mps_enable = pstr_api_struct->config[0].use_mps;
3663
1.76k
    pstr_api_struct->pstr_state->mps_tree_config = pstr_api_struct->config[0].mps_tree_config;
3664
1.76k
  }
3665
6.48k
  if (pstr_api_struct->config[0].num_bs_elements == 1) {
3666
5.35k
    pstr_api_struct->config[ele_idx].write_program_config_element = 0;
3667
5.35k
  }
3668
3669
6.48k
  if (pstr_api_struct->pstr_state->aot != AOT_USAC) {
3670
7.33k
    for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
3671
      /* Set config pointer in api obj */
3672
4.95k
      pstr_api_struct->pstr_state->pstr_config[ele_idx] = &pstr_api_struct->config[ele_idx];
3673
3674
4.95k
      error = ia_enhaacplus_enc_init(pstr_api_struct, ele_idx);
3675
4.95k
      if (error) {
3676
27
        return error;
3677
27
      }
3678
3679
4.92k
      pstr_api_struct->pstr_state->ui_init_done = 1;
3680
4.92k
      total_bitrate_used += pstr_api_struct->config[ele_idx].aac_config.bit_rate;
3681
4.92k
    }
3682
2.37k
    if (pstr_input_config->i_bitrate != total_bitrate_used) {
3683
1.68k
      pstr_input_config->i_bitrate = total_bitrate_used;
3684
1.68k
    }
3685
2.37k
    if (pstr_api_struct->config[0].aac_config.bitreservoir_size != -1) {
3686
2.24k
      WORD32 avg_bytes_per_frame_per_ch = pstr_api_struct->config[0].aac_config.bitreservoir_size;
3687
2.24k
      if (pstr_api_struct->config[0].aac_config.flag_framelength_small) {
3688
883
        if (pstr_api_struct->config[0].aot == AOT_AAC_LC ||
3689
748
            pstr_api_struct->config[0].aot == AOT_PS ||
3690
635
            pstr_api_struct->config[0].aot == AOT_SBR) {
3691
436
          avg_bytes_per_frame_per_ch = (pstr_api_struct->config[0].aac_config.bit_rate) *
3692
436
                                       FRAME_LEN_960 /
3693
436
                                       (pstr_api_struct->config[0].aac_config.core_sample_rate *
3694
436
                                        pstr_api_struct->config[0].i_channels * 8);
3695
436
        }
3696
883
        if (pstr_input_config->aot == AOT_AAC_LD || pstr_input_config->aot == AOT_AAC_ELD) {
3697
447
          avg_bytes_per_frame_per_ch = (pstr_api_struct->config[0].aac_config.bit_rate) *
3698
447
                                       FRAME_LEN_480 /
3699
447
                                       (pstr_api_struct->config[0].aac_config.core_sample_rate *
3700
447
                                        pstr_api_struct->config[0].i_channels * 8);
3701
447
        }
3702
1.36k
      } else {
3703
1.36k
        if (pstr_api_struct->config[0].aot == AOT_AAC_LC ||
3704
1.20k
            pstr_api_struct->config[0].aot == AOT_PS ||
3705
1.06k
            pstr_api_struct->config[0].aot == AOT_SBR) {
3706
814
          avg_bytes_per_frame_per_ch = (pstr_api_struct->config[0].aac_config.bit_rate) *
3707
814
                                       FRAME_LEN_1024 /
3708
814
                                       (pstr_api_struct->config[0].aac_config.core_sample_rate *
3709
814
                                        pstr_api_struct->config[0].i_channels * 8);
3710
814
        }
3711
1.36k
        if (pstr_input_config->aot == AOT_AAC_LD || pstr_input_config->aot == AOT_AAC_ELD) {
3712
552
          avg_bytes_per_frame_per_ch = (pstr_api_struct->config[0].aac_config.bit_rate) *
3713
552
                                       FRAME_LEN_512 /
3714
552
                                       (pstr_api_struct->config[0].aac_config.core_sample_rate *
3715
552
                                        pstr_api_struct->config[0].i_channels * 8);
3716
552
        }
3717
1.36k
      }
3718
3719
2.24k
      if (pstr_api_struct->config[0].aac_config.bitreservoir_size < avg_bytes_per_frame_per_ch) {
3720
176
        return IA_EXHEAACE_CONFIG_NONFATAL_BITRES_SIZE_TOO_SMALL;
3721
176
      }
3722
2.24k
    }
3723
2.20k
    if (pstr_input_config->i_use_es) {
3724
      // Write GA header
3725
1.73k
      ia_bit_buf_struct *pstr_ia_asc_bit_buf;
3726
1.73k
      pstr_ia_asc_bit_buf = iusace_create_bit_buffer(
3727
1.73k
          &(pstr_api_struct->pstr_state->str_bit_buf), pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT],
3728
1.73k
          pstr_api_struct->pstr_mem_info[IA_MEMTYPE_OUTPUT].ui_size, 1);
3729
3730
1.73k
      ixheaace_get_audiospecific_config_bytes(pstr_ia_asc_bit_buf,
3731
1.73k
                                              &pstr_api_struct->pstr_state->audio_specific_config,
3732
1.73k
                                              pstr_api_struct->pstr_state->aot,
3733
1.73k
                                              pstr_input_config->ccfl_idx);
3734
3735
1.73k
      pstr_api_struct->pstr_state->i_out_bytes = (pstr_ia_asc_bit_buf->cnt_bits + 7) >> 3;
3736
1.73k
    }
3737
2.20k
    if (pstr_api_struct->config->aac_classic) {
3738
646
      pstr_output_config->input_size =
3739
646
          frame_length * channels * pstr_api_struct->config[0].ui_pcm_wd_sz / 8;
3740
1.55k
    } else {
3741
1.55k
      pstr_output_config->input_size =
3742
1.55k
          2 * frame_length * channels * pstr_api_struct->config[0].ui_pcm_wd_sz / 8;
3743
1.55k
    }
3744
2.20k
    pstr_output_config->samp_freq = pstr_api_struct->config[0].native_sample_rate;
3745
2.20k
    pstr_output_config->header_samp_freq = pstr_api_struct->config[0].aac_config.core_sample_rate;
3746
2.20k
    pstr_output_config->down_sampling_ratio =
3747
2.20k
        pstr_api_struct->config->aac_classic == 0 ? 2.0f : 1.0f;
3748
2.20k
    switch (pstr_api_struct->config->aot) {
3749
284
      case AOT_AAC_LC:
3750
284
        pstr_output_config->audio_profile = AUDIO_PROFILE_AAC_LC_L5;
3751
284
        break;
3752
740
      case AOT_SBR:
3753
740
        pstr_output_config->audio_profile = AUDIO_PROFILE_HEAAC_L5;
3754
740
        break;
3755
224
      case AOT_PS:
3756
224
        pstr_output_config->audio_profile = AUDIO_PROFILE_HEAAC_V2_L5;
3757
224
        break;
3758
155
      case AOT_AAC_LD:
3759
155
        pstr_output_config->audio_profile = AUDIO_PROFILE_AAC_LD_L4;
3760
155
        break;
3761
799
      case AOT_AAC_ELD:
3762
799
        if (pstr_api_struct->config[0].use_mps) {
3763
478
          if (pstr_api_struct->config[0].mps_tree_config == TREE_212) {
3764
127
            pstr_output_config->audio_profile = AUDIO_PROFILE_AAC_ELD_L2;
3765
351
          } else {
3766
351
            pstr_output_config->audio_profile = AUDIO_PROFILE_AAC_ELD_L4;
3767
351
          }
3768
478
        } else {
3769
321
          pstr_output_config->audio_profile = AUDIO_PROFILE_AAC_ELD_L1;
3770
321
        }
3771
799
        break;
3772
0
      default:
3773
0
        pstr_output_config->audio_profile = AUDIO_PROFILE_NOT_SPECIFIED;
3774
0
        break;
3775
2.20k
    }
3776
2.20k
  }
3777
3778
4.07k
  else {
3779
4.07k
    pstr_api_struct->pstr_state->pstr_config[0] = &pstr_api_struct->config[0];
3780
4.07k
    error = ia_usac_enc_init(pstr_api_struct, pstr_input_config->ccfl_idx);
3781
4.07k
    if (error) {
3782
870
      return error;
3783
870
    }
3784
3785
3.20k
    pstr_output_config->input_size =
3786
3.20k
        frame_length * channels * (pstr_api_struct->config[0].usac_config.ui_pcm_wd_sz >> 3);
3787
3788
3.20k
    if (pstr_api_struct->config[0].usac_config.use_drc_element) {
3789
610
      ia_drc_input_config *pstr_drc_cfg = (ia_drc_input_config *)(pstr_input_config->pv_drc_cfg);
3790
610
      memcpy(pstr_drc_cfg, &pstr_api_struct->config[0].usac_config.str_drc_cfg,
3791
610
             sizeof(ia_drc_input_config));
3792
610
    }
3793
3794
3.20k
    pstr_output_config->down_sampling_ratio = 1;
3795
3.20k
    if (pstr_api_struct->config[0].usac_config.sbr_enable == 1) {
3796
2.30k
      switch (pstr_api_struct->config[0].ccfl_idx) {
3797
307
        case SBR_8_3:
3798
307
          pstr_output_config->input_size *= 8;
3799
307
          pstr_output_config->input_size /= 3;
3800
307
          pstr_output_config->down_sampling_ratio = 8.0f / 3.0f;
3801
307
          break;
3802
3803
1.45k
        case SBR_2_1:
3804
1.45k
          pstr_output_config->input_size *= 2;
3805
1.45k
          pstr_output_config->down_sampling_ratio = 2;
3806
1.45k
          break;
3807
3808
543
        case SBR_4_1:
3809
543
          pstr_output_config->input_size *= 4;
3810
543
          pstr_output_config->down_sampling_ratio = 4;
3811
543
          break;
3812
2.30k
      }
3813
2.30k
    }
3814
3.20k
    pstr_output_config->samp_freq = pstr_api_struct->config[0].usac_config.native_sample_rate;
3815
3.20k
    pstr_output_config->header_samp_freq =
3816
3.20k
        pstr_api_struct->config[0].usac_config.native_sample_rate;
3817
3.20k
    pstr_output_config->audio_profile = AUDIO_PROFILE_USAC_L2;
3818
3.20k
    if (pstr_input_config->use_drc_element !=
3819
3.20k
        pstr_api_struct->config[0].usac_config.use_drc_element) {
3820
3
      error = IA_EXHEAACE_EXE_NONFATAL_USAC_INVALID_GAIN_POINTS;
3821
3
    }
3822
3.20k
    pstr_input_config->use_drc_element = pstr_api_struct->config[0].usac_config.use_drc_element;
3823
3.20k
  }
3824
3825
5.40k
  pstr_api_struct->pstr_state->ui_init_done = 1;
3826
5.40k
  pstr_output_config->i_out_bytes = pstr_api_struct->pstr_state->i_out_bytes;
3827
5.40k
  if (pstr_output_config->input_size) {
3828
5.40k
    pstr_output_config->expected_frame_count =
3829
5.40k
        (pstr_input_config->aac_config.length + (pstr_output_config->input_size - 1)) /
3830
5.40k
        pstr_output_config->input_size;
3831
5.40k
    if (pstr_api_struct->config[0].usac_config.use_delay_adjustment == 1) {
3832
3.06k
      pstr_output_config->expected_frame_count -=
3833
3.06k
          pstr_api_struct->config[0].usac_config.num_preroll_frames;
3834
3.06k
    }
3835
5.40k
  }
3836
3837
5.40k
  return error;
3838
6.48k
}
3839
3840
7.26k
IA_ERRORCODE ixheaace_create(pVOID pv_input, pVOID pv_output) {
3841
7.26k
  IA_ERRORCODE err_code = IA_NO_ERROR;
3842
7.26k
  ixheaace_output_config *pstr_out_cfg = (ixheaace_output_config *)pv_output;
3843
7.26k
  err_code = ixheaace_allocate(pv_input, pv_output);
3844
7.26k
  if (!err_code) {
3845
6.48k
    err_code = ixheaace_init(pstr_out_cfg->pv_ia_process_api_obj, pv_input, pv_output);
3846
6.48k
  }
3847
7.26k
  if (err_code & IA_FATAL_ERROR) {
3848
1.68k
    IXHEAACE_MEM_FREE(pv_output);
3849
1.68k
  }
3850
7.26k
  return err_code;
3851
7.26k
}
3852
3853
492k
IA_ERRORCODE ixheaace_process(pVOID pstr_obj_ixheaace, pVOID pv_input, pVOID pv_output) {
3854
492k
  IA_ERRORCODE error = IA_NO_ERROR;
3855
492k
  WORD32 ele_idx;
3856
492k
  (VOID) pv_input;
3857
492k
  ixheaace_api_struct *pstr_api_struct = (ixheaace_api_struct *)pstr_obj_ixheaace;
3858
492k
  ixheaace_output_config *pstr_output_config = (ixheaace_output_config *)pv_output;
3859
492k
  pstr_api_struct->pstr_state->is_quant_spec_zero = 0;
3860
492k
  pstr_api_struct->pstr_state->is_gain_limited = 0;
3861
492k
  if (!pstr_api_struct->usac_en) {
3862
434k
    for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
3863
307k
      error = ia_enhaacplus_enc_execute(pstr_api_struct, ele_idx);
3864
307k
      if (error != IA_NO_ERROR) {
3865
32.9k
        return error;
3866
32.9k
      }
3867
307k
    }
3868
126k
    if ((error == IA_NO_ERROR) && (pstr_api_struct->pstr_state->is_quant_spec_zero)) {
3869
417
      error = IA_EXHEAACE_EXE_NONFATAL_QUANTIZATION_SPECTRUM_ZERO;
3870
417
    }
3871
126k
    if ((error == IA_NO_ERROR) && (pstr_api_struct->pstr_state->is_gain_limited)) {
3872
0
      error = IA_EXHEAACE_EXE_NONFATAL_QUANTIZATION_INSUFFICIENT_BITRES;
3873
0
    }
3874
333k
  } else {
3875
333k
    ia_usac_encoder_config_struct *usac_config = &pstr_api_struct->config[0].usac_config;
3876
333k
    if (usac_config->iframes_interval <= usac_config->num_preroll_frames) {
3877
23.0k
      pstr_api_struct->pstr_state->str_usac_enc_data.usac_independency_flag = 1;
3878
23.0k
      if (usac_config->iframes_interval == usac_config->num_preroll_frames &&
3879
4.29k
          usac_config->is_first_frame == 0) {
3880
1.45k
        usac_config->is_ipf = 1;
3881
1.45k
      }
3882
310k
    } else {
3883
310k
      pstr_api_struct->pstr_state->str_usac_enc_data.usac_independency_flag = 0;
3884
310k
    }
3885
333k
    if (pstr_api_struct->pstr_state->str_usac_enc_data.frame_count >
3886
333k
        usac_config->num_preroll_frames) {
3887
314k
      if (usac_config->iframes_interval <= usac_config->num_preroll_frames) {
3888
4.33k
        pstr_api_struct->pstr_state->str_usac_enc_data.usac_independency_flag = 1;
3889
310k
      } else {
3890
310k
        pstr_api_struct->pstr_state->str_usac_enc_data.usac_independency_flag = 0;
3891
310k
      }
3892
314k
    }
3893
3894
333k
    {
3895
333k
      error = iusace_process(pstr_api_struct);
3896
333k
      if (error & IA_FATAL_ERROR) {
3897
14.4k
        pstr_output_config->i_out_bytes = 0;
3898
14.4k
        return error;
3899
14.4k
      }
3900
318k
      if ((error == IA_NO_ERROR) && (pstr_api_struct->pstr_state->is_quant_spec_zero)) {
3901
350
        error = IA_EXHEAACE_EXE_NONFATAL_USAC_QUANTIZATION_SPECTRUM_ZERO;
3902
350
      }
3903
318k
      if ((error == IA_NO_ERROR) && (pstr_api_struct->pstr_state->is_gain_limited)) {
3904
3
        error = IA_EXHEAACE_EXE_NONFATAL_USAC_QUANTIZATION_INSUFFICIENT_BITRES;
3905
3
      }
3906
318k
    }
3907
3908
0
    usac_config->iframes_interval++;
3909
318k
    if (usac_config->iframes_interval ==
3910
318k
        (usac_config->random_access_interval - usac_config->num_preroll_frames)) {
3911
1.48k
      usac_config->iframes_interval = 0;
3912
1.48k
    }
3913
318k
  }
3914
445k
  pstr_output_config->i_out_bytes = pstr_api_struct->pstr_state->i_out_bytes;
3915
445k
  return error;
3916
492k
}
3917
3918
8.30k
IA_ERRORCODE ixheaace_delete(pVOID pv_output) {
3919
8.30k
  IXHEAACE_MEM_FREE(pv_output);
3920
8.30k
  return IA_NO_ERROR;
3921
8.30k
}