Coverage Report

Created: 2026-05-30 06:39

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.03k
static WORD32 iusace_scratch_size(VOID) {
143
4.03k
  WORD32 scr_size;
144
4.03k
  scr_size = IXHEAAC_GET_SIZE_ALIGNED(USACE_MAX_SCR_SIZE, BYTE_ALIGN_8);
145
4.03k
  return scr_size;
146
4.03k
}
147
148
4.03k
static WORD32 iusace_calc_pers_buf_sizes(ixheaace_api_struct *pstr_api_struct) {
149
4.03k
  WORD32 pers_size = 0;
150
4.03k
  ia_usac_encoder_config_struct *pstr_config = &pstr_api_struct->config[0].usac_config;
151
152
4.03k
  pers_size += IXHEAAC_GET_SIZE_ALIGNED(pstr_config->channels * sizeof(FLOAT32 *), BYTE_ALIGN_8);
153
4.03k
  pers_size += IXHEAAC_GET_SIZE_ALIGNED(pstr_config->channels * sizeof(FLOAT32 *), BYTE_ALIGN_8);
154
4.03k
  pers_size += IXHEAAC_GET_SIZE_ALIGNED(pstr_config->channels * sizeof(FLOAT32 *), BYTE_ALIGN_8);
155
4.03k
  pers_size += IXHEAAC_GET_SIZE_ALIGNED(pstr_config->channels * sizeof(FLOAT32 *), BYTE_ALIGN_8);
156
157
4.03k
  pers_size +=
158
4.03k
      (IXHEAAC_GET_SIZE_ALIGNED((2 * pstr_config->ccfl * sizeof(FLOAT32)), BYTE_ALIGN_8) *
159
4.03k
       pstr_config->channels);
160
4.03k
  pers_size += (IXHEAAC_GET_SIZE_ALIGNED((2 * pstr_config->drc_frame_size * sizeof(FLOAT32)),
161
4.03k
                                         BYTE_ALIGN_8) *
162
4.03k
                pstr_config->channels);
163
4.03k
  if (pstr_config->use_delay_adjustment == 1) {
164
3.88k
    pers_size +=
165
3.88k
        (IXHEAAC_GET_SIZE_ALIGNED(
166
3.88k
             ((CC_DELAY_ADJUSTMENT * pstr_config->ccfl) / FRAME_LEN_1024) * sizeof(FLOAT32),
167
3.88k
             BYTE_ALIGN_8) *
168
3.88k
         pstr_config->channels);
169
3.88k
    pers_size += (IXHEAAC_GET_SIZE_ALIGNED(
170
3.88k
                      ((CC_DELAY_ADJUSTMENT * pstr_config->drc_frame_size) / FRAME_LEN_1024) *
171
3.88k
                          sizeof(FLOAT32),
172
3.88k
                      BYTE_ALIGN_8) *
173
3.88k
                  pstr_config->channels);
174
3.88k
  }
175
176
4.03k
  pers_size +=
177
4.03k
      (IXHEAAC_GET_SIZE_ALIGNED((2 * pstr_config->ccfl * sizeof(FLOAT64)), BYTE_ALIGN_8) *
178
4.03k
       pstr_config->channels);
179
180
4.03k
  pers_size += (IXHEAAC_GET_SIZE_ALIGNED((pstr_config->ccfl * sizeof(FLOAT64)), BYTE_ALIGN_8) *
181
4.03k
                pstr_config->channels);
182
183
4.03k
  pers_size +=
184
4.03k
      (IXHEAAC_GET_SIZE_ALIGNED((2 * pstr_config->ccfl * sizeof(FLOAT64)), BYTE_ALIGN_8) *
185
4.03k
       pstr_config->channels);
186
187
4.03k
  pers_size +=
188
4.03k
      (IXHEAAC_GET_SIZE_ALIGNED((3 * pstr_config->ccfl * sizeof(FLOAT64)), BYTE_ALIGN_8) *
189
4.03k
       pstr_config->channels);
190
191
4.03k
  if (pstr_config->tns_select != 0) {
192
1.83k
    pers_size +=
193
1.83k
        (IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_tns_info), BYTE_ALIGN_8) * pstr_config->channels);
194
1.83k
  }
195
196
4.03k
  pers_size += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_usac_td_encoder_struct), BYTE_ALIGN_8) *
197
4.03k
                pstr_config->channels);
198
4.03k
  return pers_size;
199
4.03k
}
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.32k
    downsample_fac = 1;
209
2.32k
  }
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
650
  {
214
650
    max_downsamp_delay = MAXIMUM_DS_8_1_FILTER_DELAY;
215
650
    max_upsamp_delay = MAXIMUM_DS_1_3_FILTER_DELAY;
216
12.2k
  } else if (resamp_idx == 3)  // 2:1
217
8.79k
  {
218
8.79k
    max_downsamp_delay = MAXIMUM_DS_2_1_FILTER_DELAY;
219
8.79k
    max_upsamp_delay = 0;
220
8.79k
  } 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.32k
  } else {
225
2.32k
    max_downsamp_delay = MAXIMUM_DS_2_1_FILTER_DELAY;
226
2.32k
    max_upsamp_delay = 0;
227
2.32k
  }
228
229
12.9k
  if (aot == AOT_SBR || aot == AOT_PS) {
230
1.97k
    if (flag_framelength_small)
231
643
      return (FRAME_LEN_960 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
232
643
              INPUT_DELAY_LC) *
233
643
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
234
1.33k
    else
235
1.33k
      return (FRAME_LEN_1024 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
236
1.33k
              INPUT_DELAY_LC) *
237
1.33k
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
238
10.9k
  } else if (aot == AOT_AAC_LC) {
239
745
    if (flag_framelength_small)
240
333
      return (FRAME_LEN_960 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LC) *
241
333
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
242
412
    else
243
412
      return (FRAME_LEN_1024 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LC) *
244
412
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
245
10.2k
  } else if (aot == AOT_USAC) {
246
8.07k
    return (FRAME_LEN_1024 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
247
8.07k
            INPUT_DELAY_LC) *
248
8.07k
           IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
249
8.07k
  } else if (aot == AOT_AAC_LD) {
250
389
    if (flag_framelength_small)
251
159
      return (FRAME_LEN_480 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LD_480) *
252
159
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
253
230
    else
254
230
      return (FRAME_LEN_512 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LD_512) *
255
230
             IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
256
1.73k
  } else if (aot == AOT_AAC_ELD) {
257
1.73k
    if (flag_framelength_small) {
258
835
      if (mps_enable) {
259
589
        return (FRAME_LEN_480 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
260
589
                INPUT_DELAY_ELDV2_480) *
261
589
               IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
262
589
      } else {
263
246
        return (FRAME_LEN_480 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
264
246
                INPUT_DELAY_ELD_480) *
265
246
               IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
266
246
      }
267
904
    } else {
268
904
      if (mps_enable) {
269
558
        return (FRAME_LEN_512 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
270
558
                INPUT_DELAY_ELDV2_512) *
271
558
               IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
272
558
      } else {
273
346
        return (FRAME_LEN_512 * (1 << downsample_fac) + max_upsamp_delay + max_downsamp_delay +
274
346
                INPUT_DELAY_ELD_512) *
275
346
               IXHEAACE_MAX_CH_IN_BS_ELE * delay_buf_size;
276
346
      }
277
904
    }
278
1.73k
  } 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.85k
                                                      WORD32 i_num_coupling_chan) {
288
1.85k
  WORD32 slot = 0, i;
289
290
1.85k
  if ((i_channel_mask & 0x3)) {
291
1.53k
    slots_for_elements[FRONT_LEFT_RIGHT] = slot;
292
1.53k
    slot += 2;
293
1.53k
  }
294
295
1.85k
  if ((i_channel_mask & 0x4)) {
296
1.46k
    slots_for_elements[FRONT_CENTER] = slot;
297
1.46k
    slot += 1;
298
1.46k
  }
299
300
1.85k
  if ((i_channel_mask & 0x8)) {
301
441
    slots_for_elements[LFE_CHANNEL] = slot;
302
441
    slot += 1;
303
441
  }
304
305
1.85k
  if ((i_channel_mask & 0x30)) {
306
790
    slots_for_elements[BACK_LEFT_RIGHT] = slot;
307
790
    slot += 2;
308
790
  }
309
310
1.85k
  if ((i_channel_mask & 0x100)) {
311
210
    slots_for_elements[REAR_CENTER] = slot;
312
210
    slot += 1;
313
210
  }
314
315
1.85k
  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.85k
  return;
323
1.85k
}
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.85k
                                                  WORD32 i_channel_mask) {
330
1.85k
  WORD32 i;
331
1.85k
  WORD32 slots_for_elements[2 * MAXIMUM_BS_ELE];
332
1.85k
  *num_bs_elements = 0;
333
334
1.85k
  ia_enhaacplus_enc_find_slots_for_elements(i_channel_mask, slots_for_elements,
335
1.85k
                                            i_num_coupling_chan);
336
337
1.85k
  if ((i_channel_mask & 0x4)) {
338
    /*Front Center Present*/
339
1.46k
    chan_config[*num_bs_elements] = 1;
340
1.46k
    element_type[*num_bs_elements] = ID_SCE;
341
1.46k
    element_slot[*num_bs_elements] = slots_for_elements[FRONT_CENTER];
342
1.46k
    element_instance_tag[*num_bs_elements] = 0;
343
1.46k
    (*num_bs_elements)++;
344
1.46k
  }
345
346
1.85k
  if ((i_channel_mask & 0x3)) {
347
    /*Front Left and Right Present*/
348
1.53k
    chan_config[*num_bs_elements] = 2;
349
1.53k
    element_type[*num_bs_elements] = ID_CPE;
350
1.53k
    element_slot[*num_bs_elements] = slots_for_elements[FRONT_LEFT_RIGHT];
351
1.53k
    element_instance_tag[*num_bs_elements] = 0;
352
1.53k
    (*num_bs_elements)++;
353
1.53k
  }
354
355
1.85k
  if ((i_channel_mask & 0x30)) {
356
    /*Back Left and Right Present*/
357
790
    chan_config[*num_bs_elements] = 2;
358
790
    element_type[*num_bs_elements] = ID_CPE;
359
790
    element_slot[*num_bs_elements] = slots_for_elements[BACK_LEFT_RIGHT];
360
790
    element_instance_tag[*num_bs_elements] = 1;
361
790
    (*num_bs_elements)++;
362
790
  }
363
364
1.85k
  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.85k
  if ((i_channel_mask & 0x8)) {
374
    /*LFE channel Present*/
375
441
    chan_config[*num_bs_elements] = 1;
376
441
    element_type[*num_bs_elements] = ID_LFE;
377
441
    element_slot[*num_bs_elements] = slots_for_elements[LFE_CHANNEL];
378
441
    element_instance_tag[*num_bs_elements] = 0;
379
441
    (*num_bs_elements)++;
380
441
  }
381
382
1.85k
  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.85k
}
393
394
static VOID ia_enhaacplus_enc_allocate_bitrate_between_channels(
395
1.13k
    ixheaace_api_struct *pstr_api_struct, WORD32 *bitrate, WORD32 inp_bitrate) {
396
1.13k
  WORD32 ele_idx;
397
1.13k
  WORD32 num_lfe = 0, num_mono = 0, num_stereo = 0;
398
1.13k
  WORD32 bitrate_per_stereo, bitrate_per_mono;
399
4.85k
  for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
400
3.71k
    switch (pstr_api_struct->config[ele_idx].element_type) {
401
1.34k
      case ID_SCE:
402
1.34k
      case ID_CCE:
403
1.34k
        num_mono++;
404
1.34k
        break;
405
1.92k
      case ID_CPE:
406
1.92k
        num_stereo++;
407
1.92k
        break;
408
441
      case ID_LFE:
409
441
        num_lfe++;
410
441
        break;
411
0
      default:
412
0
        return;
413
3.71k
    }
414
3.71k
  }
415
1.13k
  bitrate_per_stereo = (WORD32)((inp_bitrate - (num_lfe)*8000) / (num_mono * 0.625 + num_stereo));
416
1.13k
  bitrate_per_mono = (WORD32)(0.625 * bitrate_per_stereo);
417
4.85k
  for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
418
3.71k
    switch (pstr_api_struct->config[ele_idx].element_type) {
419
1.34k
      case ID_SCE:
420
1.34k
      case ID_CCE:
421
1.34k
        bitrate[ele_idx] = bitrate_per_mono;
422
1.34k
        break;
423
1.92k
      case ID_CPE:
424
1.92k
        bitrate[ele_idx] = bitrate_per_stereo;
425
1.92k
        break;
426
441
      case ID_LFE:
427
441
        bitrate[ele_idx] = 8000;
428
441
        break;
429
0
      default:
430
0
        return;
431
3.71k
    }
432
3.71k
  }
433
1.13k
}
434
435
1.90k
static IA_ERRORCODE ixheaace_validate_channel_mask(WORD32 ch_mask, WORD32 num_ch) {
436
1.90k
  IA_ERRORCODE err_code = IA_NO_ERROR;
437
  // If ch_mask not supported, return error
438
1.90k
  WORD32 temp_mask;
439
1.90k
  switch (num_ch) {
440
358
    case 1:
441
358
      temp_mask = CH_MASK_CENTER_FRONT;
442
358
      break;
443
398
    case 2:
444
398
      temp_mask = CH_MASK_LEFT_RIGHT_FRONT;
445
398
      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
351
    case 5:
453
351
      temp_mask = CH_MASK_CENTER_FRONT | CH_MASK_LEFT_RIGHT_FRONT | CH_MASK_LEFT_RIGHT_BACK;
454
351
      break;
455
442
    case 6:
456
442
      temp_mask =
457
442
          CH_MASK_CENTER_FRONT | CH_MASK_LEFT_RIGHT_FRONT | CH_MASK_LEFT_RIGHT_BACK | CH_MASK_LFE;
458
442
      break;
459
0
    default:
460
0
      temp_mask = 0;
461
0
      break;
462
1.90k
  }
463
1.90k
  if (ch_mask != temp_mask) {
464
41
    err_code =  IA_EXHEAACE_CONFIG_FATAL_CHANNELS_MASK;
465
41
  }
466
1.90k
  return err_code;
467
1.90k
}
468
469
1.80k
static VOID ixheaace_set_default_channel_mask(WORD32 *ch_mask, WORD32 num_ch) {
470
1.80k
  switch (num_ch) {
471
279
  case 1:
472
279
    *ch_mask = CH_MASK_CENTER_FRONT;
473
279
    break;
474
408
  case 2:
475
408
    *ch_mask = CH_MASK_LEFT_RIGHT_FRONT;
476
408
    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
345
  case 5:
484
345
    *ch_mask = (CH_MASK_CENTER_FRONT | CH_MASK_LEFT_RIGHT_FRONT | CH_MASK_LEFT_RIGHT_BACK);
485
345
    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.80k
  }
491
1.80k
}
492
493
static VOID ixheaace_set_default_config(ixheaace_api_struct *pstr_api_struct,
494
6.87k
                                        ixheaace_input_config *pstr_input_config) {
495
6.87k
  ia_usac_encoder_config_struct *pstr_usac_config = &pstr_api_struct->config[0].usac_config;
496
6.87k
  WORD32 i;
497
498
61.8k
  for (i = 0; i < MAXIMUM_BS_ELE; i++) {
499
54.9k
    ia_enhaacplus_enc_aac_init_default_config(&pstr_api_struct->config[i].aac_config,
500
54.9k
                                              pstr_input_config->aot);
501
502
54.9k
    pstr_api_struct->config[i].i_channels = NUM_CHANNELS_CONFIG_PARAM_DEFAULT_VALUE;
503
54.9k
    pstr_api_struct->config[i].sample_rate = AAC_SAMP_FREQ_CONFIG_PARAM_DEFAULT_VALUE;
504
54.9k
    pstr_api_struct->config[i].native_sample_rate = AAC_SAMP_FREQ_CONFIG_PARAM_DEFAULT_VALUE;
505
54.9k
    pstr_api_struct->config[i].i_n_memtabs = NUM_MEMTABS_CONFIG_PARAM_DEFAULT_VALUE;
506
54.9k
    pstr_api_struct->config[i].aac_classic = AAC_CLASSIC_CONFIG_PARAM_DEFAULT_VALUE;
507
54.9k
    pstr_api_struct->config[i].use_parametric_stereo = USE_PS_CONFIG_PARAM_DEFAULT_VALUE;
508
54.9k
    pstr_api_struct->config[i].chmode_nchannels = CHMODE_NUM_CHANNELS_CONFIG_PARAM_DEFAULT_VALUE;
509
54.9k
    pstr_api_struct->config[i].chmode = CHMODE_CONFIG_PARAM_DEFAULT_VALUE; /*stereo*/
510
54.9k
    pstr_api_struct->config[i].adts_flag = ADTS_FLAG_CONFIG_PARAM_DEFAULT_VALUE;
511
54.9k
    pstr_api_struct->config[i].num_bs_elements = NUM_BS_ELEMENTS_CONFIG_PARAM_DEFAULT_VALUE;
512
54.9k
    pstr_api_struct->config[i].i_channels_mask = CHANNEL_MASK_CONFIG_PARAM_DEFAULT_VALUE;
513
54.9k
    pstr_api_struct->config[i].i_num_coupling_chan =
514
54.9k
        NUM_COUPLING_CHANNEL_CONFIG_PARAM_DEFAULT_VALUE;
515
54.9k
    pstr_api_struct->config[i].element_type = ELEMENT_TYPE_CONFIG_PARAM_DEFAULT_VALUE;
516
54.9k
    pstr_api_struct->config[i].element_slot = ELEMENT_SLOT_CONFIG_PARAM_DEFAULT_VALUE;
517
54.9k
    pstr_api_struct->config[i].num_bs_elements = NUM_BS_ELEMENTS_CONFIG_PARAM_DEFAULT_VALUE;
518
54.9k
    pstr_api_struct->config[i].element_instance_tag =
519
54.9k
        ELEMENT_INSTANCE_TAG_CONFIG_PARAM_DEFAULT_VALUE;
520
54.9k
    pstr_api_struct->config[i].aac_config.calc_crc = AAC_CFG_CALC_CRC_CONFIG_PARAM_DEFAULT_VALUE;
521
54.9k
    pstr_api_struct->config[i].aac_config.full_bandwidth =
522
54.9k
        AAC_CFG_FULL_BW_CONFIG_PARAM_DEFAULT_VALUE;
523
54.9k
    pstr_api_struct->config[i].eldsbr_found = ELDSBR_FOUND_CONFIG_PARAM_DEFAULT_VALUE;
524
54.9k
    pstr_api_struct->config[i].use_mps = USE_MPS_PARAM_DEFAULT_VALUE;
525
54.9k
    pstr_api_struct->config[i].mps_tree_config = USE_MPS_TREE_CONFIG_PARAM_DEFAULT_VALUE;
526
54.9k
  }
527
6.87k
  if (pstr_input_config->aot == AOT_USAC) {
528
4.13k
    memset(pstr_usac_config, 0, sizeof(*pstr_usac_config));
529
4.13k
    pstr_usac_config->channels = NUM_CHANNELS_CONFIG_PARAM_DEFAULT_VALUE;
530
4.13k
    pstr_usac_config->sample_rate = USAC_SAMP_FREQ_CONFIG_PARAM_DEFAULT_VALUE;
531
4.13k
    pstr_usac_config->core_sample_rate = USAC_SAMP_FREQ_CONFIG_PARAM_DEFAULT_VALUE;
532
4.13k
    pstr_usac_config->native_sample_rate = USAC_SAMP_FREQ_CONFIG_PARAM_DEFAULT_VALUE;
533
4.13k
    pstr_usac_config->sbr_pvc_active = USAC_SBR_PVC_DEFAULT_VALUE;
534
4.13k
    pstr_usac_config->sbr_inter_tes_active = USAC_SBR_INTER_TES_DEFAULT_VALUE;
535
4.13k
    pstr_usac_config->sbr_harmonic = USAC_SBR_HARMONIC_DEFAULT_VALUE;
536
4.13k
    pstr_usac_config->bit_rate = USAC_BITRATE_DEFAULT_VALUE;
537
4.13k
    pstr_usac_config->use_fill_element = USAC_FILL_ELEMENT_DEFAULT_VALUE;
538
4.13k
    pstr_usac_config->use_drc_element = USAC_DRC_DEFAULT_VALUE;
539
4.13k
    pstr_usac_config->cmplx_pred_flag = USAC_COMPLEX_PREDECTION_DEFAULT_VALUE;
540
4.13k
    pstr_usac_config->tns_select = USAC_TNS_DEFAULT_VALUE;
541
4.13k
    pstr_usac_config->flag_noiseFilling = USAC_FLAG_NOISE_FILLING_DEFAULT_VALUE;
542
4.13k
    pstr_usac_config->use_acelp_only = USAC_DEFAULT_ACELP_FLAG_VALUE;
543
4.13k
    pstr_usac_config->is_first_frame = USAC_FIRST_FRAME_FLAG_DEFAULT_VALUE;
544
4.13k
    pstr_usac_config->num_preroll_frames = CC_NUM_PREROLL_FRAMES;
545
4.13k
    pstr_usac_config->stream_id = USAC_DEFAULT_STREAM_ID_VALUE;
546
4.13k
    pstr_usac_config->use_delay_adjustment = USAC_DEFAULT_DELAY_ADJUSTMENT_VALUE;
547
4.13k
    pstr_usac_config->is_loudness_configured = USAC_DEFAULT_MEASURED_LOUDNESS_FLAG_VALUE;
548
4.13k
  }
549
  /* Initialize table pointers */
550
6.87k
  ia_enhaacplus_enc_init_aac_tabs(&(pstr_api_struct->pstr_aac_tabs));
551
6.87k
  ia_enhaacplus_enc_init_sbr_tabs(&(pstr_api_struct->spectral_band_replication_tabs));
552
6.87k
  pstr_api_struct->common_tabs.pstr_common_tab =
553
6.87k
      (ixheaace_common_tables *)&ia_enhaacplus_enc_common_tab;
554
6.87k
}
555
556
6.87k
static IA_ERRORCODE ixheaace_validate_config_params(ixheaace_input_config *pstr_input_config) {
557
6.87k
  IA_ERRORCODE err_code = IA_NO_ERROR;
558
6.87k
  if (pstr_input_config->aot != AOT_AAC_ELD && pstr_input_config->aot != AOT_AAC_LC &&
559
5.55k
      pstr_input_config->aot != AOT_AAC_LD && pstr_input_config->aot != AOT_PS &&
560
4.78k
      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.87k
  pstr_input_config->i_native_samp_freq = pstr_input_config->i_samp_freq;
564
6.87k
  if (pstr_input_config->aot != AOT_USAC) {
565
2.74k
    pstr_input_config->i_samp_freq = iusace_map_sample_rate(pstr_input_config->i_samp_freq);
566
4.13k
  } else {
567
4.13k
    err_code = iusace_validate_baseline_profile_sample_rate(pstr_input_config->i_samp_freq);
568
4.13k
    if (err_code) {
569
94
      return err_code;
570
94
    }
571
4.13k
  }
572
573
6.77k
  if ((pstr_input_config->i_channels < MIN_NUM_CORE_CODER_CHANNELS) ||
574
6.50k
      (pstr_input_config->i_channels > MAX_NUM_CORE_CODER_CHANNELS)) {
575
601
    pstr_input_config->i_channels = 1;
576
601
  }
577
6.77k
  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.77k
  if ((pstr_input_config->esbr_flag == 1) &&
581
4.22k
      ((pstr_input_config->aot != AOT_SBR) && (pstr_input_config->aot != AOT_PS) &&
582
3.32k
       (pstr_input_config->aot != AOT_USAC))) {
583
1.02k
    pstr_input_config->esbr_flag = 0;
584
1.02k
  }
585
6.77k
  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.77k
  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.77k
  if (pstr_input_config->aot != AOT_AAC_ELD && pstr_input_config->aot != AOT_USAC) {
593
1.76k
    pstr_input_config->i_use_mps = 0;
594
1.76k
  }
595
6.77k
  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
865
      pstr_input_config->ccfl_idx = SBR_2_1;
598
865
    }
599
1.51k
  }
600
6.77k
  if (AOT_USAC == pstr_input_config->aot) {
601
4.03k
    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
1.99k
      pstr_input_config->i_use_mps = 0;
605
1.99k
    }
606
4.03k
  }
607
6.77k
  if (pstr_input_config->i_use_mps == 1) {
608
1.80k
    if (pstr_input_config->i_channels == 2) {
609
1.39k
      if (pstr_input_config->i_mps_tree_config != TREE_212) {
610
1.36k
        pstr_input_config->i_mps_tree_config = TREE_212;
611
1.36k
      }
612
1.39k
    } 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
373
          pstr_input_config->i_mps_tree_config != TREE_525) {
616
133
        pstr_input_config->i_mps_tree_config = TREE_5151;
617
133
      }
618
410
    }
619
4.97k
  } else {
620
4.97k
    pstr_input_config->i_mps_tree_config = INVALID_TREE_CONFIG;
621
4.97k
  }
622
6.77k
  if (pstr_input_config->aot == AOT_USAC || pstr_input_config->aot == AOT_AAC_ELD ||
623
5.21k
      pstr_input_config->aot == AOT_AAC_LD) {
624
5.21k
    pstr_input_config->i_use_adts = 0;
625
5.21k
    pstr_input_config->i_use_es = 1;
626
5.21k
  }
627
6.77k
  if (pstr_input_config->aot == AOT_USAC) {
628
4.03k
    if (pstr_input_config->codec_mode != USAC_SWITCHED &&
629
2.71k
        pstr_input_config->codec_mode != USAC_ONLY_FD &&
630
2.68k
        pstr_input_config->codec_mode != USAC_ONLY_TD) {
631
2.57k
      pstr_input_config->codec_mode = USAC_ONLY_FD;
632
2.57k
    }
633
4.03k
    if (pstr_input_config->ccfl_idx < NO_SBR_CCFL_768 || pstr_input_config->ccfl_idx > SBR_4_1) {
634
1.43k
      pstr_input_config->ccfl_idx = NO_SBR_CCFL_1024;  // default value
635
1.43k
    }
636
4.03k
    if ((pstr_input_config->ccfl_idx == SBR_4_1) && (pstr_input_config->i_samp_freq < 32000))
637
208
    {
638
208
      if (pstr_input_config->i_samp_freq >= 16000)
639
154
      {
640
154
        pstr_input_config->ccfl_idx = SBR_2_1;
641
154
      }
642
54
      else
643
54
      {
644
54
        pstr_input_config->ccfl_idx = NO_SBR_CCFL_1024;
645
54
      }
646
208
    }
647
4.03k
    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.03k
    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.03k
    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.03k
    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.03k
    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.03k
    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.03k
    if ((pstr_input_config->ccfl_idx != 3 && pstr_input_config->ccfl_idx != 4)) {
667
2.27k
      pstr_input_config->harmonic_sbr = 0;
668
2.27k
    }
669
4.03k
    if (pstr_input_config->harmonic_sbr != 1) {
670
2.62k
      pstr_input_config->hq_esbr = 0;
671
2.62k
    }
672
4.03k
    if (pstr_input_config->i_bitrate != 64000 && pstr_input_config->i_bitrate != 96000) {
673
3.69k
      pstr_input_config->i_bitrate = USAC_BITRATE_DEFAULT_VALUE;
674
3.69k
    }
675
4.03k
    {
676
4.03k
      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.03k
      if (pstr_input_config->ccfl_idx == NO_SBR_CCFL_768 ||
680
3.41k
          pstr_input_config->ccfl_idx == NO_SBR_CCFL_1024) {
681
2.13k
        if (pstr_input_config->i_bitrate >
682
2.13k
            (WORD32)(6 * pstr_input_config->i_samp_freq * pstr_input_config->i_channels)) {
683
284
          pstr_input_config->i_bitrate =
684
284
              (6 * pstr_input_config->i_samp_freq * pstr_input_config->i_channels);
685
284
        }
686
2.13k
      } else if (pstr_input_config->ccfl_idx == SBR_8_3) {
687
139
        if (pstr_input_config->i_bitrate >
688
139
            (WORD32)(6 * ((pstr_input_config->i_samp_freq * 3) / 8) *
689
139
                     pstr_input_config->i_channels)) {
690
105
          pstr_input_config->i_bitrate =
691
105
              (6 * ((pstr_input_config->i_samp_freq * 3) / 8) * pstr_input_config->i_channels);
692
105
        }
693
1.76k
      } else if (pstr_input_config->ccfl_idx == SBR_2_1) {
694
1.19k
        if (pstr_input_config->i_bitrate >
695
1.19k
            (WORD32)(6 * (pstr_input_config->i_samp_freq / 2) * pstr_input_config->i_channels)) {
696
93
          pstr_input_config->i_bitrate =
697
93
              (6 * (pstr_input_config->i_samp_freq / 2) * pstr_input_config->i_channels);
698
93
        }
699
1.19k
      } else if (pstr_input_config->ccfl_idx == SBR_4_1) {
700
573
        if (pstr_input_config->i_bitrate >
701
573
            (WORD32)(6 * (pstr_input_config->i_samp_freq / 4) * pstr_input_config->i_channels)) {
702
171
          pstr_input_config->i_bitrate =
703
171
              (6 * (pstr_input_config->i_samp_freq / 4) * pstr_input_config->i_channels);
704
171
        }
705
573
      }
706
4.03k
    }
707
708
4.03k
    {
709
4.03k
      if ((pstr_input_config->codec_mode == USAC_SWITCHED ||
710
2.71k
           pstr_input_config->codec_mode == USAC_ONLY_TD) && pstr_input_config->esbr_flag &&
711
892
          pstr_input_config->i_samp_freq > 24000) {
712
760
        if (pstr_input_config->ccfl_idx == NO_SBR_CCFL_768) {
713
157
          pstr_input_config->ccfl_idx = SBR_8_3;  // Use 8:3 eSBR
714
157
        }
715
760
        if (pstr_input_config->ccfl_idx == NO_SBR_CCFL_1024) {
716
162
          pstr_input_config->ccfl_idx = SBR_2_1;  // Use 2:1 eSBR
717
162
        }
718
760
      }
719
720
4.03k
      if (pstr_input_config->codec_mode == USAC_ONLY_FD &&
721
2.59k
          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
29
          pstr_input_config->ccfl_idx = SBR_8_3;  // Use 8:3 eSBR
725
29
        }
726
1.06k
        if (pstr_input_config->ccfl_idx == NO_SBR_CCFL_1024) {
727
622
          pstr_input_config->ccfl_idx = SBR_2_1;  // Use 2:1 eSBR
728
622
        }
729
1.06k
      }
730
731
4.03k
      if (pstr_input_config->ccfl_idx == NO_SBR_CCFL_768 ||
732
3.59k
          pstr_input_config->ccfl_idx == SBR_8_3) {
733
765
        pstr_input_config->frame_length = LEN_SUPERFRAME_768;
734
3.27k
      } else {
735
3.27k
        pstr_input_config->frame_length = LEN_SUPERFRAME;
736
3.27k
      }
737
4.03k
    }
738
4.03k
    if (pstr_input_config->random_access_interval < MIN_RAP_INTERVAL_IN_MS) {
739
1.72k
      pstr_input_config->random_access_interval = DEFAULT_RAP_INTERVAL_IN_MS;
740
1.72k
    }
741
4.03k
    if (pstr_input_config->method_def > MAX_METHOD_DEFINITION_TYPE) {
742
2.98k
      pstr_input_config->method_def = METHOD_DEFINITION_PROGRAM_LOUDNESS;
743
2.98k
    }
744
4.03k
    if (pstr_input_config->measurement_system != MEASUREMENT_SYSTEM_BS_1770_3) {
745
4.03k
      pstr_input_config->measurement_system = MEASUREMENT_SYSTEM_BS_1770_3;
746
4.03k
    }
747
4.03k
    if (pstr_input_config->measured_loudness > MAX_METHOD_VALUE ||
748
4.03k
        pstr_input_config->measured_loudness < MIN_METHOD_VALUE) {
749
113
      pstr_input_config->measured_loudness = DEFAULT_METHOD_VALUE;
750
113
    }
751
4.03k
    if (pstr_input_config->sample_peak_level > MAX_SAMPLE_PEAK_LEVEL ||
752
4.03k
        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.03k
    if (pstr_input_config->use_delay_adjustment != 0 &&
756
3.88k
        pstr_input_config->use_delay_adjustment != 1) {
757
3.88k
      pstr_input_config->use_delay_adjustment = USAC_DEFAULT_DELAY_ADJUSTMENT_VALUE;
758
3.88k
    }
759
4.03k
    if (pstr_input_config->use_drc_element) {
760
2.12k
      ia_drc_input_config *pstr_drc_cfg = (ia_drc_input_config *)pstr_input_config->pv_drc_cfg;
761
2.12k
      err_code = impd_drc_validate_config_params(pstr_drc_cfg);
762
2.12k
      if (err_code & IA_FATAL_ERROR) {
763
0
        return err_code;
764
0
      }
765
2.12k
      if (err_code) {
766
675
        pstr_input_config->use_drc_element = 0;
767
675
        err_code = IA_NO_ERROR;
768
675
      }
769
2.12k
    }
770
4.03k
  } else {
771
2.74k
    pstr_input_config->cplx_pred = 0;
772
2.74k
    pstr_input_config->harmonic_sbr = 0;
773
2.74k
    pstr_input_config->pvc_active = 0;
774
2.74k
    pstr_input_config->inter_tes_active = 0;
775
2.74k
    pstr_input_config->use_drc_element = 0;
776
2.74k
    pstr_input_config->hq_esbr = 0;
777
2.74k
    pstr_input_config->use_delay_adjustment = 0;
778
2.74k
    if (pstr_input_config->i_channels != 2 && pstr_input_config->aot == AOT_PS) {
779
301
      pstr_input_config->aot = AOT_SBR;
780
301
    }
781
2.74k
    if (pstr_input_config->aac_config.use_tns != 1 &&
782
917
        pstr_input_config->aac_config.use_tns != 0) {
783
0
      pstr_input_config->aac_config.use_tns = 0;
784
0
    }
785
2.74k
    if (pstr_input_config->aac_config.noise_filling != 1 &&
786
1.06k
        pstr_input_config->aac_config.noise_filling != 0) {
787
0
      pstr_input_config->aac_config.noise_filling = 0;
788
0
    }
789
2.74k
    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.74k
    if (pstr_input_config->aac_config.full_bandwidth != 1 &&
794
1.67k
        pstr_input_config->aac_config.full_bandwidth != 0) {
795
0
      pstr_input_config->aac_config.full_bandwidth = 0;
796
0
    }
797
2.74k
    {
798
2.74k
      if (pstr_input_config->i_bitrate < MINIMUM_BITRATE * pstr_input_config->i_channels) {
799
1.37k
        pstr_input_config->i_bitrate = MINIMUM_BITRATE * pstr_input_config->i_channels;
800
1.37k
      }
801
2.74k
      if (pstr_input_config->i_bitrate >
802
2.74k
          (WORD32)(6 * pstr_input_config->i_samp_freq * pstr_input_config->i_channels)) {
803
854
        pstr_input_config->i_bitrate =
804
854
            (6 * pstr_input_config->i_samp_freq * pstr_input_config->i_channels);
805
854
      }
806
2.74k
    }
807
808
2.74k
    {
809
2.74k
      if (pstr_input_config->aot == AOT_AAC_LC || pstr_input_config->aot == AOT_SBR ||
810
1.56k
          pstr_input_config->aot == AOT_PS) {
811
1.56k
        if (pstr_input_config->frame_length != LEN_SUPERFRAME &&
812
1.34k
            pstr_input_config->frame_length != FRAME_LEN_960) {
813
830
          pstr_input_config->frame_length = LEN_SUPERFRAME;
814
830
        }
815
1.56k
      } 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
555
          pstr_input_config->frame_length = FRAME_LEN_512;
819
555
        }
820
1.18k
      } else {
821
0
        pstr_input_config->frame_length = LEN_SUPERFRAME;
822
0
      }
823
2.74k
    }
824
2.74k
    if ((pstr_input_config->frame_length == FRAME_LEN_960) &&
825
514
        (pstr_input_config->esbr_flag == 1)) {
826
299
      pstr_input_config->esbr_flag = 0;
827
299
    }
828
2.74k
  }
829
6.77k
  return err_code;
830
6.77k
}
831
832
static IA_ERRORCODE ixheaace_set_config_params(ixheaace_api_struct *pstr_api_struct,
833
6.87k
                                               ixheaace_input_config *pstr_input_config) {
834
6.87k
  WORD32 ele_idx;
835
6.87k
  IA_ERRORCODE err_code = IA_NO_ERROR;
836
6.87k
  ia_usac_encoder_config_struct *pstr_usac_config = &pstr_api_struct->config[0].usac_config;
837
6.87k
  err_code = ixheaace_validate_config_params(pstr_input_config);
838
6.87k
  if (err_code) {
839
94
    return err_code;
840
94
  }
841
842
6.77k
  if (pstr_input_config->ui_pcm_wd_sz != 16) {
843
210
    return (IA_EXHEAACE_CONFIG_FATAL_PCM_WDSZ);
844
210
  }
845
6.56k
  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.56k
  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.56k
  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.56k
  if ((pstr_input_config->aac_config.use_tns != 0) &&
859
3.57k
      (pstr_input_config->aac_config.use_tns != 1)) {
860
0
    return (IA_EXHEAACE_CONFIG_FATAL_USE_TNS);
861
0
  }
862
6.56k
  if ((pstr_input_config->aac_config.full_bandwidth != 0) &&
863
2.79k
      (pstr_input_config->aac_config.full_bandwidth != 1)) {
864
0
    return (IA_EXHEAACE_CONFIG_FATAL_USE_FULL_BANDWIDTH);
865
0
  }
866
867
59.1k
  for (ele_idx = 0; ele_idx < MAXIMUM_BS_ELE; ele_idx++) {
868
52.5k
    pstr_api_struct->config[ele_idx].aac_classic = 1;
869
52.5k
    pstr_api_struct->config[ele_idx].firstframe = 1;
870
52.5k
    pstr_api_struct->config[ele_idx].aot = pstr_input_config->aot;
871
52.5k
    pstr_api_struct->config[ele_idx].adts_flag = pstr_input_config->i_use_adts;
872
52.5k
    pstr_api_struct->config[ele_idx].sample_rate = pstr_input_config->i_samp_freq;
873
52.5k
    pstr_api_struct->config[ele_idx].native_sample_rate = pstr_input_config->i_native_samp_freq;
874
875
52.5k
    pstr_api_struct->config[ele_idx].i_channels = pstr_input_config->i_channels;
876
52.5k
    pstr_api_struct->config[ele_idx].i_native_channels = pstr_input_config->i_channels;
877
52.5k
    pstr_api_struct->config[ele_idx].i_channels_mode = pstr_input_config->i_channels;
878
52.5k
    pstr_api_struct->config[ele_idx].aac_config.bit_rate = pstr_input_config->i_bitrate;
879
52.5k
    pstr_api_struct->config[ele_idx].esbr_flag = pstr_input_config->esbr_flag;
880
52.5k
    pstr_api_struct->config[ele_idx].use_mps = pstr_input_config->i_use_mps;
881
52.5k
    pstr_api_struct->config[ele_idx].mps_tree_config = pstr_input_config->i_mps_tree_config;
882
52.5k
    pstr_api_struct->config[ele_idx].i_num_coupling_chan = pstr_input_config->i_num_coupling_chan;
883
52.5k
    pstr_api_struct->config[ele_idx].ui_pcm_wd_sz = pstr_input_config->ui_pcm_wd_sz;
884
52.5k
    pstr_api_struct->config[ele_idx].write_program_config_element =
885
52.5k
        pstr_input_config->write_program_config_element;
886
52.5k
    pstr_api_struct->config[ele_idx].frame_length = pstr_input_config->frame_length;
887
52.5k
    pstr_api_struct->config[ele_idx].aac_config.num_stereo_preprocessing =
888
52.5k
        pstr_input_config->aac_config.f_no_stereo_preprocessing;
889
890
52.5k
    pstr_api_struct->config[ele_idx].aac_config.use_tns = pstr_input_config->aac_config.use_tns;
891
52.5k
    if (pstr_input_config->aot == AOT_AAC_LD || pstr_input_config->aot == AOT_AAC_ELD) {
892
8.78k
      pstr_api_struct->config[ele_idx].aac_config.inv_quant =
893
8.78k
          pstr_input_config->aac_config.inv_quant;
894
8.78k
      if (pstr_input_config->frame_length == FRAME_LEN_512) {
895
4.75k
        pstr_api_struct->config[ele_idx].aac_config.flag_framelength_small = 0;
896
4.75k
      } else if (pstr_input_config->frame_length == FRAME_LEN_480) {
897
4.03k
        pstr_api_struct->config[ele_idx].aac_config.flag_framelength_small = 1;
898
4.03k
      }
899
43.7k
    } else if (pstr_input_config->aot == AOT_AAC_LC || pstr_input_config->aot == AOT_SBR ||
900
34.3k
               pstr_input_config->aot == AOT_PS) {
901
11.4k
      pstr_api_struct->config[ele_idx].aac_config.inv_quant = 0;
902
11.4k
      if (pstr_input_config->frame_length == FRAME_LEN_1024) {
903
7.52k
        pstr_api_struct->config[ele_idx].aac_config.flag_framelength_small = 0;
904
7.52k
      } else if (pstr_input_config->frame_length == FRAME_LEN_960) {
905
3.94k
        pstr_api_struct->config[ele_idx].aac_config.flag_framelength_small = 1;
906
3.94k
      }
907
11.4k
    }
908
52.5k
    if ((AOT_SBR == pstr_input_config->aot) || (AOT_PS == pstr_input_config->aot) ||
909
44.7k
        (AOT_AAC_ELD == pstr_input_config->aot)) {
910
14.6k
      pstr_api_struct->config[ele_idx].aac_classic = 0;
911
14.6k
    }
912
52.5k
    if (pstr_api_struct->config[ele_idx].sample_rate < 32000) {
913
10.8k
      if (pstr_api_struct->config[ele_idx].aac_classic == 0) {
914
242
        pstr_api_struct->config[ele_idx].aac_classic = 1;
915
242
      }
916
10.8k
      if (pstr_input_config->aot == AOT_SBR || pstr_input_config->aot == AOT_PS) {
917
160
        pstr_input_config->aot = AOT_AAC_LC;
918
160
      }
919
10.8k
      if (pstr_input_config->aot == AOT_AAC_ELD) {
920
82
        pstr_input_config->aot = AOT_AAC_LD;
921
82
      }
922
10.8k
    }
923
52.5k
    pstr_api_struct->config[ele_idx].eldsbr_found =
924
52.5k
        !(pstr_api_struct->config[ele_idx].aac_classic);
925
52.5k
  }
926
6.56k
  if (pstr_input_config->aot == AOT_USAC) {
927
4.03k
    if (pstr_input_config->i_channels > 2) {
928
0
      return IA_EXHEAACE_CONFIG_FATAL_NUM_CHANNELS;
929
0
    }
930
4.03k
    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.03k
    pstr_api_struct->usac_en = 1;
934
935
4.03k
    pstr_usac_config->codec_mode = pstr_input_config->codec_mode;
936
4.03k
    pstr_usac_config->channels = pstr_input_config->i_channels;
937
938
4.03k
    pstr_usac_config->core_sample_rate = pstr_input_config->i_samp_freq;
939
4.03k
    pstr_usac_config->sample_rate = pstr_input_config->i_samp_freq;
940
4.03k
    pstr_usac_config->native_sample_rate = pstr_input_config->i_native_samp_freq;
941
942
4.03k
    pstr_usac_config->ui_pcm_wd_sz = pstr_input_config->ui_pcm_wd_sz;
943
4.03k
    pstr_usac_config->ccfl_idx = pstr_input_config->ccfl_idx;
944
4.03k
    pstr_usac_config->bit_rate = pstr_input_config->i_bitrate;
945
4.03k
    pstr_usac_config->basic_bitrate = pstr_input_config->i_bitrate;
946
4.03k
    pstr_usac_config->tns_select = pstr_input_config->aac_config.use_tns;
947
4.03k
    pstr_usac_config->cmplx_pred_flag = pstr_input_config->cplx_pred;
948
4.03k
    pstr_usac_config->flag_noiseFilling = pstr_input_config->aac_config.noise_filling;
949
4.03k
    pstr_usac_config->sbr_pvc_active = pstr_input_config->pvc_active;
950
4.03k
    pstr_usac_config->sbr_harmonic = pstr_input_config->harmonic_sbr;
951
4.03k
    pstr_usac_config->hq_esbr = pstr_input_config->hq_esbr;
952
4.03k
    pstr_usac_config->sbr_inter_tes_active = pstr_input_config->inter_tes_active;
953
4.03k
    pstr_api_struct->config[0].chmode_nchannels = pstr_api_struct->config[0].i_channels;
954
955
4.03k
    switch (pstr_input_config->ccfl_idx) {
956
440
      case NO_SBR_CCFL_768:
957
440
        pstr_usac_config->ccfl = LEN_SUPERFRAME_768;
958
440
        pstr_usac_config->in_frame_length = LEN_SUPERFRAME_768;
959
440
        pstr_usac_config->sbr_enable = 0;
960
440
        pstr_usac_config->drc_frame_size = LEN_SUPERFRAME_768;
961
440
        break;
962
724
      case NO_SBR_CCFL_1024:
963
724
        pstr_usac_config->ccfl = LEN_SUPERFRAME;
964
724
        pstr_usac_config->in_frame_length = LEN_SUPERFRAME;
965
724
        pstr_usac_config->sbr_enable = 0;
966
724
        pstr_usac_config->drc_frame_size = LEN_SUPERFRAME;
967
724
        break;
968
325
      case SBR_8_3:
969
325
        pstr_usac_config->ccfl = LEN_SUPERFRAME_768;
970
325
        pstr_usac_config->in_frame_length = (LEN_SUPERFRAME_768 * 8) / 3;
971
325
        pstr_usac_config->sbr_enable = 1;
972
325
        pstr_usac_config->drc_frame_size = (LEN_SUPERFRAME_768 * 8) / 3;
973
325
        break;
974
1.97k
      case SBR_2_1:
975
1.97k
        pstr_usac_config->ccfl = LEN_SUPERFRAME;
976
1.97k
        pstr_usac_config->in_frame_length = (LEN_SUPERFRAME * 2);
977
1.97k
        pstr_usac_config->sbr_enable = 1;
978
1.97k
        pstr_usac_config->drc_frame_size = (LEN_SUPERFRAME * 2);
979
1.97k
        break;
980
573
      case SBR_4_1:
981
573
        pstr_usac_config->ccfl = LEN_SUPERFRAME;
982
573
        pstr_usac_config->in_frame_length = (LEN_SUPERFRAME * 4);
983
573
        pstr_usac_config->sbr_enable = 1;
984
573
        pstr_usac_config->drc_frame_size = (LEN_SUPERFRAME * 4);
985
573
        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.03k
    }
993
4.03k
    pstr_usac_config->random_access_interval = pstr_input_config->random_access_interval;
994
4.03k
    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.03k
    pstr_usac_config->use_delay_adjustment = pstr_input_config->use_delay_adjustment;
1002
4.03k
    if (pstr_usac_config->random_access_interval) {
1003
4.03k
      pstr_usac_config->preroll_flag = 1;
1004
4.03k
    }
1005
4.03k
    if (pstr_usac_config->sbr_enable == 1) {
1006
2.87k
      pstr_usac_config->num_preroll_frames++;
1007
2.87k
      if (pstr_usac_config->sbr_harmonic == 1) {
1008
1.41k
        pstr_usac_config->num_preroll_frames++;
1009
1.41k
      }
1010
2.87k
    } else {
1011
1.16k
      if (pstr_usac_config->use_delay_adjustment == 1) {
1012
1.07k
        pstr_usac_config->num_preroll_frames++;
1013
1.07k
      }
1014
1.16k
    }
1015
4.03k
    pstr_usac_config->stream_id = pstr_input_config->stream_id;
1016
4.03k
    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.03k
    } else {
1019
4.03k
      pstr_api_struct->config[0].ccfl_idx = pstr_input_config->ccfl_idx;
1020
4.03k
    }
1021
4.03k
    if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
1022
325
      pstr_api_struct->spectral_band_replication_tabs.ptr_sos_upsamp_tab =
1023
325
          (ixheaace_resampler_sos_table *)&iixheaace_resamp_1_to_3_filt_params;
1024
1025
325
      pstr_api_struct->spectral_band_replication_tabs.ptr_sos_downsamp_tab =
1026
325
          (ixheaace_resampler_sos_table *)&iixheaace_resamp_8_to_1_filt_params;
1027
3.71k
    } else if (pstr_api_struct->config[0].ccfl_idx == SBR_2_1) {
1028
1.97k
      pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab =
1029
1.97k
          (ixheaace_resampler_table *)&ixheaace_resamp_2_to_1_iir_filt_params;
1030
1.97k
    } else if (pstr_api_struct->config[0].ccfl_idx == SBR_4_1) {
1031
573
      pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab =
1032
573
          (ixheaace_resampler_table *)&ixheaace_resamp_4_to_1_iir_filt_params;
1033
573
    }
1034
4.03k
    pstr_usac_config->use_drc_element = pstr_input_config->use_drc_element;
1035
4.03k
    {
1036
4.03k
      ia_drc_input_config *pstr_drc_cfg = (ia_drc_input_config *)pstr_input_config->pv_drc_cfg;
1037
4.03k
      pstr_drc_cfg->str_uni_drc_config.str_channel_layout.base_ch_count =
1038
4.03k
          pstr_input_config->i_channels;
1039
4.03k
      pstr_drc_cfg->str_enc_params.sample_rate = pstr_input_config->i_samp_freq;
1040
4.03k
      pstr_drc_cfg->str_enc_params.domain = TIME_DOMAIN;
1041
4.03k
      pstr_drc_cfg->str_uni_drc_config.sample_rate = pstr_drc_cfg->str_enc_params.sample_rate;
1042
4.03k
      if (pstr_usac_config->use_drc_element) {
1043
3.00k
        for (WORD32 i = 0; i < pstr_drc_cfg->str_uni_drc_config.drc_coefficients_uni_drc_count;
1044
1.56k
             i++) {
1045
1.56k
          for (WORD32 j = 0;
1046
4.21k
               j <
1047
4.21k
               pstr_drc_cfg->str_uni_drc_config.str_drc_coefficients_uni_drc[i].gain_set_count;
1048
2.64k
               j++) {
1049
2.64k
            pstr_drc_cfg->str_uni_drc_config.str_drc_coefficients_uni_drc[i]
1050
2.64k
                .str_gain_set_params[j]
1051
2.64k
                .delta_tmin =
1052
2.64k
                impd_drc_get_delta_t_min(pstr_drc_cfg->str_uni_drc_config.sample_rate);
1053
2.64k
          }
1054
1.56k
        }
1055
2.16k
        for (WORD32 i = 0; i < pstr_drc_cfg->str_uni_drc_config.str_uni_drc_config_ext
1056
2.16k
                                   .drc_coefficients_uni_drc_v1_count;
1057
1.44k
             i++) {
1058
2.26k
          for (WORD32 j = 0; j < pstr_drc_cfg->str_uni_drc_config.str_uni_drc_config_ext
1059
2.26k
                                     .str_drc_coefficients_uni_drc_v1[i]
1060
2.26k
                                     .gain_set_count;
1061
1.55k
               j++) {
1062
1.55k
            pstr_drc_cfg->str_uni_drc_config.str_uni_drc_config_ext
1063
1.55k
                .str_drc_coefficients_uni_drc_v1[i]
1064
1.55k
                .str_gain_set_params[j]
1065
1.55k
                .delta_tmin =
1066
1.55k
                impd_drc_get_delta_t_min(pstr_drc_cfg->str_uni_drc_config.sample_rate);
1067
1.55k
          }
1068
714
        }
1069
1.44k
      }
1070
4.03k
      pstr_usac_config->str_drc_cfg = *pstr_drc_cfg;
1071
4.03k
      pstr_usac_config->str_drc_cfg.str_enc_params.frame_size = pstr_usac_config->drc_frame_size;
1072
4.03k
      pstr_usac_config->str_drc_cfg.str_uni_drc_config.str_drc_coefficients_uni_drc
1073
4.03k
          ->drc_frame_size = pstr_usac_config->drc_frame_size;
1074
4.03k
      pstr_input_config->drc_frame_size = pstr_usac_config->drc_frame_size;
1075
1076
4.03k
      ia_drc_loudness_info_set_struct *pstr_enc_loudness_info_set =
1077
4.03k
          &pstr_usac_config->str_drc_cfg.str_enc_loudness_info_set;
1078
1079
4.03k
      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
530
        pstr_usac_config->is_loudness_configured = 1;
1087
3.50k
      } else {
1088
3.50k
        pstr_usac_config->is_loudness_configured = 0;
1089
3.50k
      }
1090
4.03k
    }
1091
4.03k
  } else {
1092
2.53k
    WORD32 max_bitreservoir_size;
1093
2.53k
    if ((pstr_input_config->i_channels > MAX_NUM_CORE_CODER_CHANNELS)) {
1094
0
      return (IA_EXHEAACE_CONFIG_FATAL_NUM_CHANNELS);
1095
0
    }
1096
2.53k
    if (!((pstr_input_config->i_native_samp_freq == 7350) ||
1097
2.48k
          (pstr_input_config->i_native_samp_freq == 8000) ||
1098
2.44k
          (pstr_input_config->i_native_samp_freq == 11025) ||
1099
2.40k
          (pstr_input_config->i_native_samp_freq == 12000) ||
1100
2.32k
          (pstr_input_config->i_native_samp_freq == 16000) ||
1101
2.28k
          (pstr_input_config->i_native_samp_freq == 22050) ||
1102
2.26k
          (pstr_input_config->i_native_samp_freq == 24000) ||
1103
2.18k
          (pstr_input_config->i_native_samp_freq == 32000) ||
1104
1.58k
          (pstr_input_config->i_native_samp_freq == 44100) ||
1105
1.00k
          (pstr_input_config->i_native_samp_freq == 48000) ||
1106
311
          (pstr_input_config->i_native_samp_freq == 64000) ||
1107
255
          (pstr_input_config->i_native_samp_freq == 88200) ||
1108
141
          (pstr_input_config->i_native_samp_freq == 96000))) {
1109
66
      return (IA_EXHEAACE_CONFIG_FATAL_SAMP_FREQ);
1110
66
    }
1111
1112
2.46k
    if ((pstr_input_config->aot == AOT_AAC_ELD) && (pstr_input_config->i_use_mps == 1) &&
1113
566
        (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
237
        pstr_api_struct->config[0].element_type = ID_CPE;
1121
237
      }
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.46k
    if (pstr_input_config->i_channels_mask == 0) {
1128
1.80k
      ixheaace_set_default_channel_mask(&pstr_input_config->i_channels_mask,
1129
1.80k
                                        pstr_input_config->i_channels);
1130
1.80k
    }
1131
2.46k
    if (pstr_api_struct->config[0].aac_config.dual_mono != 1) {
1132
2.46k
      if (pstr_input_config->aot != AOT_AAC_ELD || (pstr_input_config->i_use_mps != 1)) {
1133
1.90k
        WORD32 num_bs_elements, chan_config[MAXIMUM_BS_ELE], element_type[MAXIMUM_BS_ELE],
1134
1.90k
            element_slot[MAXIMUM_BS_ELE], element_instance_tag[MAXIMUM_BS_ELE],
1135
1.90k
            bitrate[MAXIMUM_BS_ELE];
1136
1.90k
        if ((pstr_input_config->i_channels_mask > 0x3FFFF)) {
1137
0
          return (IA_EXHEAACE_CONFIG_FATAL_CHANNELS_MASK);
1138
0
        }
1139
1.90k
        if (ixheaace_validate_channel_mask(pstr_input_config->i_channels_mask,
1140
1.90k
                                           pstr_input_config->i_channels)) {
1141
41
          return IA_EXHEAACE_CONFIG_FATAL_CHANNELS_MASK;
1142
41
        }
1143
16.7k
        for (ele_idx = 0; ele_idx < MAXIMUM_BS_ELE; ele_idx++) {
1144
14.8k
          pstr_api_struct->config[ele_idx].i_channels_mask = pstr_input_config->i_channels_mask;
1145
14.8k
        }
1146
1.85k
        ia_enhaacplus_enc_find_channel_config(
1147
1.85k
            &num_bs_elements, chan_config, element_type, element_slot, element_instance_tag,
1148
1.85k
            pstr_api_struct->config[0].i_num_coupling_chan, pstr_input_config->i_channels_mask);
1149
6.29k
        for (ele_idx = 0; ele_idx < num_bs_elements; ele_idx++) {
1150
4.43k
          pstr_api_struct->config[ele_idx].i_channels = chan_config[ele_idx];
1151
4.43k
          pstr_api_struct->config[ele_idx].i_native_channels = chan_config[ele_idx];
1152
4.43k
          pstr_api_struct->config[ele_idx].chmode_nchannels = chan_config[ele_idx];
1153
4.43k
          pstr_api_struct->config[ele_idx].element_type = element_type[ele_idx];
1154
4.43k
          pstr_api_struct->config[ele_idx].element_slot = element_slot[ele_idx];
1155
4.43k
          pstr_api_struct->config[ele_idx].num_bs_elements = num_bs_elements;
1156
4.43k
          pstr_api_struct->config[ele_idx].element_instance_tag = element_instance_tag[ele_idx];
1157
4.43k
        }
1158
1159
1.85k
        if (pstr_api_struct->config[0].num_bs_elements > 1) {
1160
1.13k
          ia_enhaacplus_enc_allocate_bitrate_between_channels(pstr_api_struct, bitrate,
1161
1.13k
                                                              pstr_input_config->i_bitrate);
1162
1163
4.85k
          for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
1164
3.71k
            pstr_api_struct->config[ele_idx].aac_config.bit_rate = bitrate[ele_idx];
1165
3.71k
          }
1166
1167
10.2k
          for (ele_idx = 0; ele_idx < MAXIMUM_BS_ELE; ele_idx++) {
1168
9.11k
            pstr_api_struct->config[ele_idx].use_parametric_stereo = 0;
1169
9.11k
          }
1170
1.13k
        }
1171
1.85k
      }
1172
2.46k
    } 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.42k
    if (pstr_input_config->aot == AOT_PS && pstr_input_config->i_channels == 2) {
1207
254
      pstr_api_struct->config[0].use_parametric_stereo = 1;
1208
254
      pstr_api_struct->config[0].chmode_nchannels = 2;
1209
2.17k
    } else {
1210
6.92k
      for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
1211
4.75k
        pstr_api_struct->config[ele_idx].chmode_nchannels =
1212
4.75k
            pstr_api_struct->config[ele_idx].i_channels;
1213
4.75k
      }
1214
2.17k
    }
1215
1216
    /* Right shift by 10 as 768 is the max bit reservoir calculated for framelength 1024 */
1217
2.42k
    max_bitreservoir_size = (BITRESERVOIR_SIZE_CONFIG_PARAM_DEFAULT_VALUE *
1218
2.42k
                             pstr_api_struct->config[0].frame_length) >>
1219
2.42k
                            10;
1220
2.42k
    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.8k
    for (ele_idx = 0; ele_idx < MAXIMUM_BS_ELE; ele_idx++) {
1225
19.4k
      pstr_api_struct->config[ele_idx].aac_config.bitreservoir_size =
1226
19.4k
        pstr_input_config->aac_config.bitreservoir_size;
1227
19.4k
      pstr_api_struct->config[ele_idx].aac_config.full_bandwidth =
1228
19.4k
        pstr_input_config->aac_config.full_bandwidth;
1229
19.4k
    }
1230
2.42k
  }
1231
1232
6.46k
  return IA_NO_ERROR;
1233
6.56k
}
1234
1235
6.46k
static VOID ixheaace_fill_mem_tabs(ixheaace_api_struct *pstr_api_struct, WORD32 aot) {
1236
6.46k
  WORD32 ele_idx;
1237
6.46k
  WORD32 num_channel;
1238
6.46k
  WORD32 frame_length = LEN_SUPERFRAME;
1239
6.46k
  ixheaace_mem_info_struct *pstr_mem_info;
1240
6.46k
  frame_length = pstr_api_struct->config[0].frame_length;
1241
6.46k
  WORD32 offset_size = 0;
1242
6.46k
  if (pstr_api_struct->usac_en) {
1243
4.03k
    WORD32 fac_downsample = 1;
1244
4.03k
    if (pstr_api_struct->config[0].ccfl_idx > NO_SBR_CCFL_1024) {
1245
2.87k
      fac_downsample = pstr_api_struct->config[0].ccfl_idx >> 1;
1246
2.87k
    } else {
1247
1.16k
      fac_downsample = 1;
1248
1.16k
    }
1249
    /* persistant */
1250
4.03k
    {
1251
4.03k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_PERSIST_IDX];
1252
4.03k
      {
1253
4.03k
        pstr_mem_info->ui_size =
1254
4.03k
            IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_state_struct), BYTE_ALIGN_8) +
1255
4.03k
            iusace_calc_pers_buf_sizes(pstr_api_struct);
1256
4.03k
        if (pstr_api_struct->config[0].usac_config.sbr_enable) {
1257
2.87k
          pstr_mem_info->ui_size += ixheaace_sbr_enc_pers_size(
1258
2.87k
              2, 0, pstr_api_struct->config[0].usac_config.sbr_harmonic);
1259
2.87k
        }
1260
4.03k
        offset_size = 2 *
1261
4.03k
                      ia_enhaacplus_enc_sizeof_delay_buffer(
1262
4.03k
                          0, AOT_USAC, pstr_api_struct->config[0].ccfl_idx,
1263
4.03k
                          sizeof(pstr_api_struct->pstr_state->inp_delay[0]),
1264
4.03k
                          pstr_api_struct->config[0].use_mps) *
1265
4.03k
                      pstr_api_struct->config[0].num_bs_elements;
1266
4.03k
        pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1267
1268
4.03k
        if (pstr_api_struct->config[0].use_mps) {
1269
1.22k
          offset_size =
1270
1.22k
              (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]);
1271
1.22k
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1272
1273
1.22k
          offset_size =
1274
1.22k
              (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]);
1275
1.22k
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1276
1277
1.22k
          pstr_mem_info->ui_size +=
1278
1.22k
              IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_mps_212_memory_struct), BYTE_ALIGN_8);
1279
1.22k
        }
1280
4.03k
        if (1 == pstr_api_struct->config[0].usac_config.sbr_enable) {
1281
2.87k
          offset_size =
1282
2.87k
              (MAX_FRAME_LEN * (1 << fac_downsample) + MAX_DS_8_1_FILTER_DELAY + INPUT_DELAY) *
1283
2.87k
              MAX_CHANNELS * sizeof(pstr_mem_info->ui_size);
1284
2.87k
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1285
2.87k
        }
1286
4.03k
        if ((2 != pstr_api_struct->config[0].usac_config.channels) &&
1287
1.23k
            (1 == pstr_api_struct->config[0].usac_config.sbr_enable)) {
1288
736
          offset_size = (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal[0]);
1289
736
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1290
736
        }
1291
4.03k
      }
1292
1293
4.03k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1294
4.03k
      pstr_mem_info->ui_type = IA_MEMTYPE_PERSIST;
1295
4.03k
      pstr_mem_info->ui_placement[0] = 0;
1296
4.03k
      pstr_mem_info->ui_placement[1] = 0;
1297
4.03k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1298
4.03k
      pstr_mem_info->ui_placed[0] = 0;
1299
4.03k
      pstr_mem_info->ui_placed[1] = 0;
1300
4.03k
    }
1301
1302
    /* scratch */
1303
4.03k
    {
1304
4.03k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_SCRATCH_IDX];
1305
4.03k
      UWORD32 usac_scr_size = iusace_scratch_size();
1306
4.03k
      if (pstr_api_struct->config[0].usac_config.sbr_enable) {
1307
2.87k
        UWORD32 sbr_scr_size = ixheaace_sbr_enc_scr_size() + ixheaace_resampler_scr_size();
1308
2.87k
        pstr_mem_info->ui_size = max(usac_scr_size, sbr_scr_size);
1309
2.87k
      } else {
1310
1.16k
        pstr_mem_info->ui_size = usac_scr_size;
1311
1.16k
      }
1312
1313
4.03k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1314
4.03k
      pstr_mem_info->ui_type = IA_MEMTYPE_SCRATCH;
1315
4.03k
      pstr_mem_info->ui_placement[0] = 0;
1316
4.03k
      pstr_mem_info->ui_placement[1] = 0;
1317
4.03k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1318
4.03k
      pstr_mem_info->ui_placed[0] = 0;
1319
4.03k
      pstr_mem_info->ui_placed[1] = 0;
1320
4.03k
    }
1321
1322
    /* input */
1323
4.03k
    {
1324
4.03k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_INPUT_IDX];
1325
4.03k
      WORD32 pcm_wd_sz;
1326
4.03k
      num_channel = pstr_api_struct->config[0].i_channels;
1327
4.03k
      pcm_wd_sz = pstr_api_struct->config[0].usac_config.ui_pcm_wd_sz;
1328
4.03k
      pstr_mem_info->ui_size = frame_length * num_channel * (pcm_wd_sz >> 3);
1329
4.03k
      if (1 == pstr_api_struct->config[0].usac_config.sbr_enable) {
1330
2.87k
        switch (pstr_api_struct->config[0].ccfl_idx) {
1331
325
          case SBR_8_3:  // 8:3
1332
325
            pstr_mem_info->ui_size *= 8;
1333
325
            pstr_mem_info->ui_size /= 3;
1334
325
            break;
1335
1336
1.97k
          case SBR_2_1:  // 2:1
1337
1.97k
            pstr_mem_info->ui_size *= 2;
1338
1.97k
            break;
1339
1340
573
          case SBR_4_1:  // 4:1
1341
573
            pstr_mem_info->ui_size *= 4;
1342
573
            break;
1343
2.87k
        }
1344
2.87k
      }
1345
1346
4.03k
      pstr_mem_info->ui_alignment =
1347
4.03k
          BYTE_ALIGN_8; /* As input is used as scratch memory internally */
1348
4.03k
      pstr_mem_info->ui_type = IA_MEMTYPE_INPUT;
1349
4.03k
      pstr_mem_info->ui_placement[0] = 0;
1350
4.03k
      pstr_mem_info->ui_placement[1] = 0;
1351
4.03k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1352
4.03k
      pstr_mem_info->ui_placed[0] = 0;
1353
4.03k
      pstr_mem_info->ui_placed[1] = 0;
1354
4.03k
    }
1355
1356
    /* output */
1357
0
    {
1358
4.03k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_OUTPUT_IDX];
1359
4.03k
      pstr_mem_info->ui_size =
1360
4.03k
          ((MAX_PREROLL_FRAMES + 1) * (MAX_CHANNEL_BITS / BYTE_NUMBIT) * num_channel) +
1361
4.03k
          MAX_PREROLL_CONFIG_SIZE;
1362
4.03k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1363
4.03k
      pstr_mem_info->ui_type = IA_MEMTYPE_OUTPUT;
1364
4.03k
      pstr_mem_info->ui_placement[0] = 0;
1365
4.03k
      pstr_mem_info->ui_placement[1] = 0;
1366
4.03k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1367
4.03k
      pstr_mem_info->ui_placed[0] = 0;
1368
4.03k
      pstr_mem_info->ui_placed[1] = 0;
1369
4.03k
    }
1370
4.03k
  } else {
1371
    /* persistant */
1372
2.42k
    {
1373
2.42k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_PERSIST_IDX];
1374
2.42k
      {
1375
2.42k
        if (pstr_api_struct->config[0].num_bs_elements == 1) {
1376
1.28k
          num_channel = pstr_api_struct->config[0].aac_classic
1377
1.28k
                            ? pstr_api_struct->config[0].chmode_nchannels
1378
1.28k
                            : (pstr_api_struct->config[0].use_parametric_stereo
1379
1.03k
                                   ? 1
1380
1.03k
                                   : pstr_api_struct->config[0].chmode_nchannels);
1381
1.28k
          pstr_mem_info->ui_size =
1382
1.28k
              IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_state_struct), BYTE_ALIGN_8) +
1383
1.28k
              ia_enhaacplus_enc_aac_enc_pers_size(num_channel, aot);
1384
1.28k
          if (pstr_api_struct->config[0].aot != AOT_AAC_LC &&
1385
1.19k
              pstr_api_struct->config[0].aot != AOT_AAC_LD) {
1386
1.13k
            pstr_mem_info->ui_size += ixheaace_sbr_enc_pers_size(
1387
1.13k
                num_channel, pstr_api_struct->config[0].use_parametric_stereo, 0);
1388
1.13k
          }
1389
1.28k
          offset_size = ia_enhaacplus_enc_sizeof_delay_buffer(
1390
1.28k
                            pstr_api_struct->config[0].aac_config.flag_framelength_small, aot, 3,
1391
1.28k
                            sizeof(pstr_api_struct->pstr_state->inp_delay[0]),
1392
1.28k
                            pstr_api_struct->config[0].use_mps) *
1393
1.28k
                        pstr_api_struct->config[0].num_bs_elements;
1394
1.28k
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1395
1.28k
        }
1396
2.42k
        if (pstr_api_struct->config[0].num_bs_elements > 1) {
1397
1.13k
          offset_size = IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_state_struct), BYTE_ALIGN_8) +
1398
1.13k
                        ia_enhaacplus_enc_sizeof_delay_buffer(
1399
1.13k
                            pstr_api_struct->config[0].aac_config.flag_framelength_small, aot, 3,
1400
1.13k
                            sizeof(pstr_api_struct->pstr_state->inp_delay[0]),
1401
1.13k
                            pstr_api_struct->config[0].use_mps) *
1402
1.13k
                            pstr_api_struct->config[0].num_bs_elements;
1403
1.13k
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1404
4.85k
          for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
1405
3.71k
            num_channel = pstr_api_struct->config[ele_idx].i_channels;
1406
3.71k
            if (pstr_api_struct->config[ele_idx].element_type != ID_LFE)
1407
3.27k
              pstr_mem_info->ui_size += ixheaace_sbr_enc_pers_size(num_channel, 0, 0);
1408
3.71k
            pstr_mem_info->ui_size += ia_enhaacplus_enc_aac_enc_pers_size(num_channel, aot) + 32;
1409
3.71k
          }
1410
1.13k
        }
1411
1412
2.42k
        if (pstr_api_struct->config[0].use_mps) {
1413
581
          if (pstr_api_struct->config[0].aac_config.flag_framelength_small) {
1414
297
            offset_size =
1415
297
                (MAX_INPUT_SAMPLES + (INPUT_DELAY_ELDV2_480 * IXHEAACE_MAX_CH_IN_BS_ELE)) *
1416
297
                sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]);
1417
297
          } else {
1418
284
            offset_size =
1419
284
                (MAX_INPUT_SAMPLES + (INPUT_DELAY_ELDV2_512 * IXHEAACE_MAX_CH_IN_BS_ELE)) *
1420
284
                sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]);
1421
284
          }
1422
581
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1423
581
          if (pstr_api_struct->config[0].mps_tree_config == TREE_212) {
1424
171
            pstr_mem_info->ui_size +=
1425
171
                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
581
          pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(
1431
581
              (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]),
1432
581
              BYTE_ALIGN_8);
1433
581
        }
1434
1435
2.42k
        offset_size = IXHEAACE_MAX_PAYLOAD_SIZE * pstr_api_struct->config[0].num_bs_elements;
1436
2.42k
        pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1437
2.42k
        offset_size = (MAX_FRAME_LEN * 2 + MAX_DS_2_1_FILTER_DELAY + INPUT_DELAY) *
1438
2.42k
                      MAX_INPUT_CHAN * sizeof(pstr_mem_info->ui_size);
1439
2.42k
        pstr_mem_info->ui_size += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1440
2.42k
      }
1441
1442
2.42k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1443
2.42k
      pstr_mem_info->ui_type = IA_MEMTYPE_PERSIST;
1444
2.42k
      pstr_mem_info->ui_placement[0] = 0;
1445
2.42k
      pstr_mem_info->ui_placement[1] = 0;
1446
2.42k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1447
2.42k
      pstr_mem_info->ui_placed[0] = 0;
1448
2.42k
      pstr_mem_info->ui_placed[1] = 0;
1449
2.42k
    }
1450
1451
    /* scratch */
1452
2.42k
    {
1453
2.42k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_SCRATCH_IDX];
1454
2.42k
      {
1455
2.42k
        pstr_mem_info->ui_size = ia_enhaacplus_enc_aac_enc_scr_size();
1456
2.42k
        UWORD32 sbr_scr_size = ixheaace_sbr_enc_scr_size() + ixheaace_resampler_scr_size();
1457
2.42k
        UWORD32 mps_scr_size = 0;
1458
2.42k
        if (pstr_api_struct->config[0].use_mps) {
1459
581
          if (pstr_api_struct->config[0].mps_tree_config != TREE_212) {
1460
410
            mps_scr_size = ixheaace_mps_515_scratch_size();
1461
410
          }
1462
581
        }
1463
2.42k
        pstr_mem_info->ui_size += MAX(sbr_scr_size, mps_scr_size);
1464
2.42k
      }
1465
2.42k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1466
2.42k
      pstr_mem_info->ui_type = IA_MEMTYPE_SCRATCH;
1467
2.42k
      pstr_mem_info->ui_placement[0] = 0;
1468
2.42k
      pstr_mem_info->ui_placement[1] = 0;
1469
2.42k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1470
2.42k
      pstr_mem_info->ui_placed[0] = 0;
1471
2.42k
      pstr_mem_info->ui_placed[1] = 0;
1472
2.42k
    }
1473
1474
    /* input */
1475
2.42k
    {
1476
2.42k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_INPUT_IDX];
1477
1478
2.42k
      WORD32 pcm_wd_sz;
1479
2.42k
      pcm_wd_sz = pstr_api_struct->config[0].ui_pcm_wd_sz;
1480
2.42k
      num_channel = 0;
1481
7.43k
      for (ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements; ele_idx++) {
1482
5.00k
        num_channel += pstr_api_struct->config[ele_idx].i_channels;
1483
5.00k
      }
1484
2.42k
      {
1485
2.42k
        if (pstr_api_struct->config[0].aac_classic) {
1486
675
          pstr_mem_info->ui_size = (frame_length * (pcm_wd_sz >> 3)) * num_channel;
1487
1.75k
        } else {
1488
1.75k
          pstr_mem_info->ui_size = (frame_length * (pcm_wd_sz >> 3)) * 2 * num_channel;
1489
1.75k
        }
1490
2.42k
      }
1491
2.42k
      pstr_mem_info->ui_alignment =
1492
2.42k
          BYTE_ALIGN_8; /* As input is used as scratch memory internally */
1493
2.42k
      pstr_mem_info->ui_type = IA_MEMTYPE_INPUT;
1494
2.42k
      pstr_mem_info->ui_placement[0] = 0;
1495
2.42k
      pstr_mem_info->ui_placement[1] = 0;
1496
2.42k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1497
2.42k
      pstr_mem_info->ui_placed[0] = 0;
1498
2.42k
      pstr_mem_info->ui_placed[1] = 0;
1499
2.42k
    }
1500
1501
    /* output */
1502
2.42k
    {
1503
2.42k
      pstr_mem_info = &pstr_api_struct->pstr_mem_info[IA_ENHAACPLUSENC_OUTPUT_IDX];
1504
2.42k
      if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
1505
1.36k
        pstr_mem_info->ui_size = (6 * frame_length / 8) * num_channel;
1506
1.36k
        pstr_mem_info->ui_size += (7) * IXHEAACE_MAX_CH_IN_BS_ELE * MAXIMUM_BS_ELE;
1507
1.36k
      } 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.42k
      pstr_mem_info->ui_alignment = BYTE_ALIGN_8;
1511
2.42k
      pstr_mem_info->ui_type = IA_MEMTYPE_OUTPUT;
1512
2.42k
      pstr_mem_info->ui_placement[0] = 0;
1513
2.42k
      pstr_mem_info->ui_placement[1] = 0;
1514
2.42k
      pstr_mem_info->ui_priority = IA_MEMPRIORITY_ANYWHERE;
1515
2.42k
      pstr_mem_info->ui_placed[0] = 0;
1516
2.42k
      pstr_mem_info->ui_placed[1] = 0;
1517
2.42k
    }
1518
2.42k
  }
1519
1520
6.46k
  return;
1521
6.46k
}
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.46k
                                                  ixheaace_input_config *ptr_in_cfg) {
1558
6.46k
  IA_ERRORCODE err_code = IA_NO_ERROR;
1559
6.46k
  UWORD32 i_idx;
1560
6.46k
  pVOID pv_value;
1561
32.3k
  for (i_idx = 0; i_idx < 4; i_idx++) {
1562
25.8k
    if (i_idx == IA_ENHAACPLUSENC_OUTPUT_IDX &&
1563
6.46k
        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.8k
    ptr_out_cfg->mem_info_table[i_idx].ui_size = pstr_api_struct->pstr_mem_info[i_idx].ui_size;
1572
25.8k
    ptr_out_cfg->mem_info_table[i_idx].ui_alignment =
1573
25.8k
        pstr_api_struct->pstr_mem_info[i_idx].ui_alignment;
1574
25.8k
    ptr_out_cfg->mem_info_table[i_idx].ui_type = pstr_api_struct->pstr_mem_info[i_idx].ui_type;
1575
1576
25.8k
    ptr_out_cfg->arr_alloc_memory[ptr_out_cfg->malloc_count] =
1577
25.8k
        ptr_out_cfg->malloc_xheaace(ptr_out_cfg->mem_info_table[i_idx].ui_size +
1578
25.8k
                                        ptr_out_cfg->mem_info_table[i_idx].ui_alignment,
1579
25.8k
                                    ptr_out_cfg->mem_info_table[i_idx].ui_alignment);
1580
1581
25.8k
    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.8k
    memset(ptr_out_cfg->arr_alloc_memory[ptr_out_cfg->malloc_count], 0,
1585
25.8k
           ptr_out_cfg->mem_info_table[i_idx].ui_size);
1586
25.8k
    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.8k
    pstr_api_struct->pp_mem[i_idx] = ptr_out_cfg->mem_info_table[i_idx].mem_ptr;
1600
25.8k
    memset(pstr_api_struct->pp_mem[i_idx], 0, pstr_api_struct->pstr_mem_info[i_idx].ui_size);
1601
1602
25.8k
    pstr_api_struct->pp_mem[i_idx] = pv_value;
1603
1604
25.8k
    if (i_idx == IA_ENHAACPLUSENC_PERSIST_IDX) {
1605
6.46k
      WORD32 offset_size = IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_state_struct), BYTE_ALIGN_8);
1606
6.46k
      WORD8 *p_offset = NULL;
1607
1608
      /* Set persistent memory pointer in api obj */
1609
6.46k
      pstr_api_struct->pstr_state = (ixheaace_state_struct *)pv_value;
1610
6.46k
      WORD32 i, inp_delay_size;
1611
6.46k
      WORD8 *p_temp;
1612
6.46k
      if (pstr_api_struct->usac_en) {
1613
4.03k
        memset(pstr_api_struct->pstr_state, 0, sizeof(*(pstr_api_struct->pstr_state)));
1614
4.03k
        ia_usac_encoder_config_struct *pstr_usac_config = &pstr_api_struct->config[0].usac_config;
1615
4.03k
        ixheaace_state_struct *pstr_state = pstr_api_struct->pstr_state;
1616
4.03k
        ia_usac_data_struct *pstr_usac_enc_data = &(pstr_state->str_usac_enc_data);
1617
1618
4.03k
        pstr_state->ptr_in_buf = (FLOAT32 **)((WORD8 *)pstr_state + offset_size);
1619
1620
4.03k
        offset_size = pstr_usac_config->channels * sizeof(FLOAT32 *);
1621
4.03k
        p_offset = (WORD8 *)pstr_state->ptr_in_buf +
1622
4.03k
                   IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1623
1624
        // Input delay
1625
4.03k
        pstr_state->inp_delay = (FLOAT32 *)(p_offset);
1626
4.03k
        inp_delay_size =
1627
4.03k
            2 *
1628
4.03k
            ia_enhaacplus_enc_sizeof_delay_buffer(
1629
4.03k
                0, AOT_USAC, pstr_api_struct->config[0].ccfl_idx,
1630
4.03k
                sizeof(pstr_state->inp_delay[0]), pstr_api_struct->config[0].use_mps) *
1631
4.03k
            pstr_api_struct->config[0].num_bs_elements;
1632
4.03k
        memset(pstr_state->inp_delay, 0, inp_delay_size);
1633
4.03k
        p_offset += IXHEAAC_GET_SIZE_ALIGNED(inp_delay_size, BYTE_ALIGN_8);
1634
4.03k
        if (1 == pstr_usac_config->sbr_enable) {
1635
2.87k
          if (2 != pstr_usac_config->channels) {
1636
736
            pstr_api_struct->pstr_state->time_signal = (FLOAT32 *)(p_offset);
1637
1638
736
            memset(pstr_api_struct->pstr_state->time_signal, 0,
1639
736
                   (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal[0]));
1640
736
            offset_size =
1641
736
                (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal[0]);
1642
736
            p_offset += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1643
736
          }
1644
1645
2.87k
          pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[0] =
1646
2.87k
              (struct ixheaace_str_sbr_enc *)p_offset;
1647
2.87k
          p_offset = p_offset + ixheaace_sbr_enc_pers_size(pstr_usac_config->channels, 0,
1648
2.87k
                                                           pstr_usac_config->sbr_harmonic);
1649
2.87k
        }
1650
4.03k
        if (1 == pstr_api_struct->config[0].use_mps) {
1651
1.22k
          pstr_api_struct->pstr_state->time_signal_mps = (FLOAT32 *)(p_offset);
1652
1653
1.22k
          memset(pstr_api_struct->pstr_state->time_signal_mps, 0,
1654
1.22k
                 (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]));
1655
1.22k
          offset_size =
1656
1.22k
              (MAX_INPUT_SAMPLES) * sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]);
1657
1.22k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1658
1.22k
          pstr_api_struct->pstr_state->mps_bs = (UWORD8 *)(p_offset);
1659
1660
1.22k
          memset(pstr_api_struct->pstr_state->mps_bs, 0,
1661
1.22k
                 (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]));
1662
1663
1.22k
          offset_size =
1664
1.22k
              (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]);
1665
1.22k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1666
1667
1.22k
          pstr_api_struct->pstr_state->mps_pers_mem = (ixheaace_mps_212_memory_struct *)p_offset;
1668
1.22k
          p_offset +=
1669
1.22k
              IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_mps_212_memory_struct), BYTE_ALIGN_8);
1670
2.81k
        } else {
1671
2.81k
          pstr_api_struct->pstr_state->mps_bs = NULL;
1672
2.81k
        }
1673
4.03k
        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.44k
            pstr_state->pp_drc_in_buf[i] = (FLOAT32 *)(p_offset);
1681
2.44k
            p_offset += IXHEAAC_GET_SIZE_ALIGNED(
1682
2.44k
                (pstr_usac_config->drc_frame_size * sizeof(pstr_state->pp_drc_in_buf[0][0]) * 2),
1683
2.44k
                BYTE_ALIGN_8);
1684
2.44k
            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.44k
          }
1691
1.44k
          memset(p_temp, 0, (p_offset - p_temp));
1692
1.44k
        }
1693
4.03k
        p_temp = p_offset;
1694
1695
10.8k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1696
6.83k
          pstr_state->ptr_in_buf[i] = (FLOAT32 *)(p_offset);
1697
6.83k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED((pstr_usac_config->ccfl * sizeof(FLOAT32) * 2),
1698
6.83k
                                               BYTE_ALIGN_8);
1699
6.83k
          if (pstr_usac_config->use_delay_adjustment) {
1700
6.61k
            p_offset += IXHEAAC_GET_SIZE_ALIGNED(
1701
6.61k
                ((CC_DELAY_ADJUSTMENT * pstr_usac_config->ccfl) / FRAME_LEN_1024) *
1702
6.61k
                    sizeof(FLOAT32),
1703
6.61k
                BYTE_ALIGN_8);
1704
6.61k
          }
1705
6.83k
        }
1706
4.03k
        memset(p_temp, 0, (p_offset - p_temp));
1707
1708
4.03k
        p_temp = p_offset;
1709
10.8k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1710
6.83k
          pstr_usac_enc_data->ptr_time_data[i] = (FLOAT64 *)(p_offset);
1711
6.83k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED((2 * (pstr_usac_config->ccfl) * sizeof(FLOAT64)),
1712
6.83k
                                                BYTE_ALIGN_8);
1713
6.83k
        }
1714
1715
10.8k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1716
6.83k
          pstr_usac_enc_data->ptr_look_ahead_time_data[i] = (FLOAT64 *)(p_offset);
1717
6.83k
          p_offset +=
1718
6.83k
              IXHEAAC_GET_SIZE_ALIGNED((pstr_usac_config->ccfl * sizeof(FLOAT64)), BYTE_ALIGN_8);
1719
6.83k
        }
1720
1721
10.8k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1722
6.83k
          pstr_usac_enc_data->spectral_line_vector[i] = (FLOAT64 *)(p_offset);
1723
6.83k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED((2 * pstr_usac_config->ccfl * sizeof(FLOAT64)),
1724
6.83k
                                                BYTE_ALIGN_8);
1725
6.83k
        }
1726
1727
10.8k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1728
6.83k
          pstr_usac_enc_data->ptr_2frame_time_data[i] = (FLOAT64 *)(p_offset);
1729
6.83k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED((3 * pstr_usac_config->ccfl * sizeof(FLOAT64)),
1730
6.83k
                                                BYTE_ALIGN_8);
1731
6.83k
        }
1732
4.03k
        memset(p_temp, 0, p_offset - p_temp);
1733
1734
4.03k
        if (pstr_usac_config->tns_select != 0) {
1735
1.83k
          p_temp = p_offset;
1736
5.03k
          for (i = 0; i < pstr_usac_config->channels; i++) {
1737
3.19k
            pstr_usac_enc_data->pstr_tns_info[i] = (ia_tns_info *)(p_offset);
1738
3.19k
            p_offset += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_tns_info), BYTE_ALIGN_8);
1739
3.19k
          }
1740
1.83k
          memset(p_temp, 0, p_offset - p_temp);
1741
1.83k
        }
1742
1743
4.03k
        p_temp = p_offset;
1744
10.8k
        for (i = 0; i < pstr_usac_config->channels; i++) {
1745
6.83k
          pstr_usac_enc_data->td_encoder[i] = (ia_usac_td_encoder_struct *)(p_offset);
1746
6.83k
          p_offset += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_usac_td_encoder_struct), BYTE_ALIGN_8);
1747
6.83k
        }
1748
4.03k
        memset(p_temp, 0, p_offset - p_temp);
1749
4.03k
      } else {
1750
2.42k
        WORD32 num_aac_chan;
1751
2.42k
        ixheaace_state_struct *pstr_state = pstr_api_struct->pstr_state;
1752
2.42k
        memset(pstr_api_struct->pstr_state, 0, sizeof(*(pstr_api_struct->pstr_state)));
1753
1754
2.42k
        pstr_api_struct->pstr_state->inp_delay = (FLOAT32 *)((WORD8 *)pstr_state + offset_size);
1755
2.42k
        offset_size = ia_enhaacplus_enc_sizeof_delay_buffer(
1756
2.42k
                          pstr_api_struct->config[0].aac_config.flag_framelength_small,
1757
2.42k
                          pstr_api_struct->config[0].aot, 3,
1758
2.42k
                          sizeof(pstr_api_struct->pstr_state->inp_delay[0]),
1759
2.42k
                          pstr_api_struct->config[0].use_mps) *
1760
2.42k
                      pstr_api_struct->config[0].num_bs_elements;
1761
2.42k
        p_offset = (WORD8 *)pstr_api_struct->pstr_state->inp_delay +
1762
2.42k
                   IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1763
1764
2.42k
        if (pstr_api_struct->config[0].use_mps) {
1765
581
          pstr_api_struct->pstr_state->time_signal_mps = (FLOAT32 *)(p_offset);
1766
1767
581
          memset(pstr_api_struct->pstr_state->time_signal_mps, 0,
1768
581
                 (MAX_INPUT_SAMPLES + (INPUT_DELAY_ELDV2_512 * IXHEAACE_MAX_CH_IN_BS_ELE)) *
1769
581
                     sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]));
1770
581
          offset_size =
1771
581
              (MAX_INPUT_SAMPLES + (INPUT_DELAY_ELDV2_512 * IXHEAACE_MAX_CH_IN_BS_ELE)) *
1772
581
              sizeof(pstr_api_struct->pstr_state->time_signal_mps[0]);
1773
581
          p_offset += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1774
1775
581
          pstr_api_struct->pstr_state->mps_bs = (UWORD8 *)(p_offset);
1776
581
          memset(pstr_api_struct->pstr_state->mps_bs, 0,
1777
581
                 (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]));
1778
581
          offset_size =
1779
581
              (MAX_MPS_BS_PAYLOAD_SIZE) * sizeof(pstr_api_struct->pstr_state->mps_bs[0]);
1780
581
          p_offset += IXHEAAC_GET_SIZE_ALIGNED(offset_size, BYTE_ALIGN_8);
1781
1782
581
          if (pstr_api_struct->config[0].mps_tree_config == TREE_212) {
1783
171
            pstr_api_struct->pstr_state->mps_pers_mem =
1784
171
                (ixheaace_mps_212_memory_struct *)p_offset;
1785
171
            p_offset +=
1786
171
                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.84k
        } else {
1794
1.84k
          pstr_api_struct->pstr_state->mps_bs = NULL;
1795
1.84k
        }
1796
1797
7.43k
        for (WORD32 ele_idx = 0; ele_idx < pstr_api_struct->config[0].num_bs_elements;
1798
5.00k
             ele_idx++) {
1799
5.00k
          num_aac_chan = pstr_api_struct->config[ele_idx].aac_classic
1800
5.00k
                             ? pstr_api_struct->config[ele_idx].chmode_nchannels
1801
5.00k
                             : (pstr_api_struct->config[ele_idx].use_parametric_stereo
1802
3.38k
                                    ? 1
1803
3.38k
                                    : pstr_api_struct->config[ele_idx].chmode_nchannels);
1804
1805
          /* Set aac enc persistent memory pointer in api obj */
1806
5.00k
          pstr_api_struct->pstr_state->aac_enc_pers_mem[ele_idx] =
1807
5.00k
              (iexheaac_encoder_str *)p_offset;
1808
5.00k
          p_offset = p_offset + ia_enhaacplus_enc_aac_enc_pers_size(
1809
5.00k
                                    num_aac_chan, pstr_api_struct->config[ele_idx].aot);
1810
1811
5.00k
          if (pstr_api_struct->config[ele_idx].aot != AOT_AAC_LC &&
1812
4.05k
              pstr_api_struct->config[ele_idx].aot != AOT_AAC_LD) {
1813
3.60k
            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.33k
              pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[ele_idx] =
1816
3.33k
                  (struct ixheaace_str_sbr_enc *)p_offset;
1817
1818
3.33k
              p_offset =
1819
3.33k
                  p_offset +
1820
3.33k
                  ixheaace_sbr_enc_pers_size(
1821
3.33k
                      num_aac_chan, pstr_api_struct->config[ele_idx].use_parametric_stereo, 0);
1822
3.33k
            }
1823
3.60k
          }
1824
5.00k
        }
1825
2.42k
      }
1826
6.46k
    }
1827
1828
25.8k
    if ((i_idx == IA_MEMTYPE_SCRATCH) && pstr_api_struct->usac_en) {
1829
4.03k
      pstr_api_struct->pstr_state->str_usac_enc_data.str_scratch.ptr_scratch_buf =
1830
4.03k
          (UWORD8 *)pstr_api_struct->pp_mem[IA_MEMTYPE_SCRATCH];
1831
4.03k
      memset(pstr_api_struct->pp_mem[IA_MEMTYPE_SCRATCH], 0,
1832
4.03k
             pstr_api_struct->pstr_mem_info[i_idx].ui_size);
1833
4.03k
    }
1834
25.8k
    if (i_idx == IA_ENHAACPLUSENC_INPUT_IDX) {
1835
6.46k
      ptr_out_cfg->ui_inp_buf_size = ptr_out_cfg->mem_info_table[i_idx].ui_size;
1836
6.46k
    }
1837
25.8k
    ptr_out_cfg->malloc_count++;
1838
25.8k
  }
1839
6.46k
  return err_code;
1840
6.46k
}
1841
1842
static VOID ixheaace_write_audio_preroll_data(ixheaace_api_struct *pstr_api_struct,
1843
307k
                                              ia_bit_buf_struct *it_bit_buff) {
1844
307k
  ixheaace_config_struct *pstr_config = &pstr_api_struct->config[0];
1845
307k
  ixheaace_state_struct *pstr_enc_state = pstr_api_struct->pstr_state;
1846
307k
  ia_usac_data_struct *pstr_usac_data = &pstr_enc_state->str_usac_enc_data;
1847
307k
  ia_usac_encoder_config_struct *pstr_usac_config = &pstr_config->usac_config;
1848
307k
  WORD32 i, j, padding_bits;
1849
1850
307k
  if (pstr_usac_config->is_ipf) {
1851
10.9k
    if (pstr_usac_config->iframes_interval == pstr_usac_config->num_preroll_frames) {
1852
4.21k
      WORD32 config_len = 0, num_bits = 0, au_len = 0, config_bits = 0;
1853
4.21k
      WORD32 bytes_to_write;
1854
4.21k
      UWORD8 *ptr_out = (UWORD8 *)pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT];
1855
4.21k
      WORD32 max_output_size =
1856
4.21k
          ((MAX_CHANNEL_BITS / BYTE_NUMBIT) * pstr_usac_config->channels) * MAX_PREROLL_FRAMES +
1857
4.21k
          MAX_PREROLL_CONFIG_SIZE;
1858
4.21k
      UWORD8 *out_data = ptr_out + max_output_size;
1859
4.21k
      UWORD8 residual_bits = 0, residual_data = 0;
1860
4.21k
      memmove(ptr_out + max_output_size, ptr_out, pstr_enc_state->i_out_bytes);
1861
4.21k
      pstr_usac_config->is_ipf = 0;
1862
1863
4.21k
      config_bits = ixheaace_get_usac_config_bytes(NULL, &pstr_enc_state->audio_specific_config,
1864
4.21k
                                                   pstr_config->ccfl_idx);
1865
4.21k
      config_len = (config_bits + 7) >> 3;
1866
4.21k
      num_bits = iusace_write_escape_value(NULL, config_len, 4, 4, 8);
1867
4.21k
      num_bits += (config_len * 8);  // config data bits
1868
4.21k
      num_bits++;                    // apply-crossfade
1869
4.21k
      num_bits++;                    // apr-reserved
1870
      // bits for number of preroll frames
1871
4.21k
      num_bits += iusace_write_escape_value(NULL, pstr_usac_config->num_preroll_frames, 2, 4, 0);
1872
      // bits for au_len
1873
13.6k
      for (i = 0; i < pstr_usac_config->num_preroll_frames; i++) {
1874
9.39k
        num_bits += iusace_write_escape_value(NULL, pstr_usac_data->prev_out_bytes[i], 16, 16, 0);
1875
9.39k
        au_len += pstr_usac_data->prev_out_bytes[i];
1876
9.39k
      }
1877
4.21k
      iusace_reset_bit_buffer(it_bit_buff);
1878
      // total bytes to write
1879
4.21k
      bytes_to_write = (num_bits + 7) >> 3;
1880
      // usacIndependencyFlag
1881
4.21k
      iusace_write_bits_buf(it_bit_buff, pstr_usac_data->usac_independency_flag, 1);
1882
4.21k
      iusace_write_bits_buf(it_bit_buff, 1, 1);  // usacExtElementPresent
1883
4.21k
      iusace_write_bits_buf(it_bit_buff, 0, 1);  // usacExtElementUseDefaultLength
1884
1885
4.21k
      if (au_len + bytes_to_write >= MAXIMUM_VALUE_8BIT) {
1886
4.04k
        iusace_write_escape_value(it_bit_buff, au_len + bytes_to_write + 2, 8, 16, 0);
1887
4.04k
      } else {
1888
169
        iusace_write_bits_buf(it_bit_buff, au_len + bytes_to_write, 8);
1889
169
      }
1890
1891
4.21k
      iusace_write_escape_value(it_bit_buff, config_len, 4, 4, 8);  // configLen
1892
      // Config
1893
4.21k
      ixheaace_get_usac_config_bytes(it_bit_buff, &pstr_enc_state->audio_specific_config,
1894
4.21k
                                     pstr_config->ccfl_idx);
1895
1896
4.21k
      if (config_bits % 8) {
1897
2.69k
        iusace_write_bits_buf(it_bit_buff, 0, (UWORD8)((config_len << 3) - config_bits));
1898
2.69k
      }
1899
1900
4.21k
      iusace_write_bits_buf(it_bit_buff, 0, 1);  // applyCrossfade
1901
4.21k
      iusace_write_bits_buf(it_bit_buff, 0, 1);  // apr_reserved
1902
      // numPreRollFrames
1903
4.21k
      iusace_write_escape_value(it_bit_buff, pstr_usac_config->num_preroll_frames, 2, 4, 0);
1904
13.6k
      for (i = 0; i < pstr_usac_config->num_preroll_frames; i++) {
1905
9.39k
        au_len = pstr_usac_data->prev_out_bytes[i];
1906
1907
9.39k
        if (pstr_usac_config->iframes_interval != 0) {
1908
9.39k
          out_data = pstr_usac_data->prev_out_data[i];
1909
9.39k
        }
1910
1911
        // auLen
1912
9.39k
        iusace_write_escape_value(it_bit_buff, au_len, 16, 16, 0);
1913
1914
        // AccessUnit
1915
7.38M
        for (j = 0; j < au_len; j++) {
1916
7.37M
          iusace_write_bits_buf(it_bit_buff, *out_data, 8);
1917
7.37M
          out_data++;
1918
7.37M
        }
1919
9.39k
      }
1920
1921
4.21k
      if (num_bits % 8) {
1922
2.85k
        iusace_write_bits_buf(it_bit_buff, 0, (UWORD8)((bytes_to_write << 3) - num_bits));
1923
2.85k
      }
1924
      // current frame
1925
4.21k
      au_len = pstr_enc_state->i_out_bits >> 3;
1926
4.21k
      residual_bits = (UWORD8)(pstr_enc_state->i_out_bits - (au_len << 3));
1927
4.21k
      out_data = ptr_out + max_output_size;
1928
3.27M
      for (j = 0; j < au_len; j++) {
1929
3.26M
        iusace_write_bits_buf(it_bit_buff, *out_data, 8);
1930
3.26M
        out_data++;
1931
3.26M
      }
1932
4.21k
      residual_data = *out_data >> (8 - residual_bits);
1933
4.21k
      iusace_write_bits_buf(it_bit_buff, residual_data, residual_bits);
1934
1935
4.21k
      padding_bits = 8 - (it_bit_buff->cnt_bits & 7);
1936
4.21k
      if (padding_bits > 0 && padding_bits < 8) {
1937
3.62k
        ptr_out[it_bit_buff->cnt_bits >> 3] =
1938
3.62k
            (WORD8)((UWORD32)ptr_out[it_bit_buff->cnt_bits >> 3]) & (0xFF << padding_bits);
1939
3.62k
      }
1940
4.21k
      pstr_enc_state->i_out_bytes = (it_bit_buff->cnt_bits + 7) >> 3;
1941
4.21k
      pstr_usac_config->preroll_idx++;
1942
1943
4.21k
      if (!pstr_usac_config->is_first_frame) {
1944
1.41k
        pstr_usac_config->preroll_idx = pstr_usac_config->num_preroll_frames + 1;
1945
1.41k
      }
1946
4.21k
      if (pstr_usac_config->is_first_frame) {
1947
2.80k
        pstr_usac_config->is_first_frame = 0;
1948
2.80k
      }
1949
6.75k
    } else {
1950
6.75k
      if (pstr_usac_config->preroll_idx < pstr_usac_config->num_preroll_frames) {
1951
6.75k
        WORD32 *ptr_prev_out_bytes = pstr_usac_data->prev_out_bytes;
1952
6.75k
        WORD32 pr_idx = pstr_usac_config->preroll_idx;
1953
6.75k
        UWORD8 *ptr_out = (UWORD8 *)pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT];
1954
6.75k
        ptr_prev_out_bytes[pr_idx] = pstr_enc_state->i_out_bytes;
1955
6.75k
        memcpy(pstr_usac_data->prev_out_data[pr_idx++], ptr_out, pstr_enc_state->i_out_bytes);
1956
6.75k
        pstr_usac_config->preroll_idx = pr_idx;
1957
6.75k
        pstr_enc_state->i_out_bytes = 0;
1958
6.75k
      }
1959
6.75k
    }
1960
296k
  } else {
1961
674k
    for (j = 0; j < pstr_usac_config->num_preroll_frames - 1; j++) {
1962
377k
      pstr_usac_data->prev_out_bytes[j] = pstr_usac_data->prev_out_bytes[j + 1];
1963
377k
    }
1964
296k
    if (pstr_usac_config->num_preroll_frames) {
1965
296k
      pstr_usac_data->prev_out_bytes[pstr_usac_config->num_preroll_frames - 1] =
1966
296k
          pstr_enc_state->i_out_bytes;
1967
296k
    }
1968
296k
    pstr_usac_config->preroll_idx = pstr_usac_config->num_preroll_frames + 1;
1969
296k
  }
1970
307k
  return;
1971
307k
}
1972
1973
4.03k
static IA_ERRORCODE ia_usac_enc_init(ixheaace_api_struct *pstr_api_struct, WORD32 ccfl_idx) {
1974
4.03k
  IA_ERRORCODE error = IA_NO_ERROR;
1975
4.03k
  WORD32 i = 0;
1976
4.03k
  ixheaace_config_struct *pstr_config = &pstr_api_struct->config[0];
1977
4.03k
  ixheaace_state_struct *pstr_enc_state = pstr_api_struct->pstr_state;
1978
4.03k
  ia_usac_data_struct *pstr_enc_data = &pstr_enc_state->str_usac_enc_data;
1979
4.03k
  ia_usac_encoder_config_struct *pstr_usac_config = &pstr_config->usac_config;
1980
1981
4.03k
  pstr_usac_config->bit_rate = pstr_api_struct->config[0].aac_config.bit_rate;
1982
1983
4.03k
  if ((pstr_usac_config->codec_mode == USAC_SWITCHED) ||
1984
3.92k
      (pstr_usac_config->codec_mode == USAC_ONLY_FD)) {
1985
3.92k
    pstr_usac_config->aac_allow_scalefacs = 1;
1986
3.92k
    if (pstr_usac_config->aac_scale_facs == 0) pstr_usac_config->aac_allow_scalefacs = 0;
1987
3.92k
  }
1988
1989
4.03k
  if (pstr_usac_config->codec_mode == USAC_ONLY_TD) {
1990
295
    for (i = 0; i < pstr_config->i_channels; i++) {
1991
184
      pstr_enc_data->core_mode_prev[i] = CORE_MODE_FD;
1992
184
      pstr_enc_data->core_mode[i] = CORE_MODE_TD;
1993
184
    }
1994
3.92k
  } else {
1995
10.5k
    for (i = 0; i < pstr_config->i_channels; i++) {
1996
6.65k
      pstr_enc_data->core_mode_prev[i] = CORE_MODE_FD;
1997
6.65k
      pstr_enc_data->core_mode[i] = CORE_MODE_FD;
1998
6.65k
    }
1999
3.92k
  }
2000
2001
4.03k
  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.03k
  if (pstr_usac_config->sbr_enable) {
2005
2.87k
    WORD8 *sbr_scr_ptr = (WORD8 *)pstr_enc_data->str_scratch.ptr_scratch_buf;
2006
2.87k
    ixheaace_audio_specific_config_struct *pstr_asc = &pstr_enc_state->audio_specific_config;
2007
2.87k
    ixheaace_str_sbr_cfg spectral_band_replication_config;
2008
    // SBR defaults
2009
2.87k
    iaace_config *pstr_sbr_config = &(pstr_api_struct->config[0].aac_config);
2010
2.87k
    WORD32 sbr_ratio = 0;
2011
2.87k
    WORD32 samples = pstr_usac_config->ccfl;
2012
    // Set scratch buffers for SBR and resampler
2013
2.87k
    pstr_api_struct->pstr_state->temp_buff_sbr =
2014
2.87k
        (WORD8 *)pstr_enc_data->str_scratch.ptr_scratch_buf;
2015
2.87k
    pstr_api_struct->pstr_state->ptr_temp_buff_resamp =
2016
2.87k
        (WORD8 *)pstr_enc_data->str_scratch.ptr_scratch_buf + ixheaace_sbr_enc_scr_size();
2017
2018
2.87k
    ixheaace_initialize_sbr_defaults(&spectral_band_replication_config);
2019
    // Set SBR codec as USAC
2020
2.87k
    spectral_band_replication_config.sbr_codec = USAC_SBR;
2021
2.87k
    spectral_band_replication_config.sbr_pvc_active = pstr_usac_config->sbr_pvc_active;
2022
2.87k
    spectral_band_replication_config.sbr_harmonic = pstr_usac_config->sbr_harmonic;
2023
2.87k
    spectral_band_replication_config.hq_esbr = pstr_usac_config->hq_esbr;
2024
2.87k
    pstr_usac_config->core_sample_rate = pstr_usac_config->sample_rate / 2;
2025
2.87k
    switch (pstr_usac_config->ccfl_idx) {
2026
573
      case SBR_4_1:
2027
573
        spectral_band_replication_config.sbr_ratio_idx = USAC_SBR_RATIO_INDEX_4_1;
2028
573
        spectral_band_replication_config.sbr_pvc_rate = USAC_SBR_DOWNSAMPLE_RATIO_4_1;
2029
573
        pstr_usac_config->core_sample_rate = pstr_usac_config->sample_rate / 4;
2030
573
        sbr_ratio = 4;
2031
573
        samples *= 4;
2032
573
        break;
2033
325
      case SBR_8_3:
2034
325
        spectral_band_replication_config.sbr_ratio_idx = USAC_SBR_RATIO_INDEX_8_3;
2035
325
        spectral_band_replication_config.sbr_pvc_rate = USAC_SBR_DOWNSAMPLE_RATIO_2_1;
2036
325
        sbr_ratio = 2;
2037
325
        samples *= 8;
2038
325
        samples /= 3;
2039
325
        break;
2040
1.97k
      case SBR_2_1:
2041
1.97k
        spectral_band_replication_config.sbr_ratio_idx = USAC_SBR_RATIO_INDEX_2_1;
2042
1.97k
        spectral_band_replication_config.sbr_pvc_rate = USAC_SBR_DOWNSAMPLE_RATIO_2_1;
2043
1.97k
        sbr_ratio = 2;
2044
1.97k
        samples *= 2;
2045
1.97k
        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.87k
    }
2052
2.87k
    if (pstr_api_struct->pstr_state->mps_enable) {
2053
1.14k
      ixheaace_mps_212_memory_struct *pstr_mps_memory;
2054
1.14k
      pstr_mps_memory = pstr_api_struct->pstr_state->mps_pers_mem;
2055
1.14k
      ixheaace_mps_212_open(&pstr_api_struct->pstr_mps_212_enc, pstr_mps_memory);
2056
1.14k
      pstr_asc->str_aac_config.num_sac_cfg_bits = 0;
2057
2058
1.14k
      error = ixheaace_mps_212_initialise(
2059
1.14k
          pstr_api_struct->pstr_mps_212_enc, AOT_USAC, pstr_usac_config->sample_rate,
2060
1.14k
          &pstr_sbr_config->bit_rate, sbr_ratio, (WORD32)samples, samples, 515 * sbr_ratio,
2061
1.14k
          (WORD8 *)pstr_api_struct->pstr_state->ptr_temp_buff_resamp);
2062
1.14k
      if (error) {
2063
31
        return error;
2064
31
      }
2065
2066
1.11k
      pstr_asc->str_aac_config.num_sac_cfg_bits = ixheaace_mps_212_get_spatial_specific_config(
2067
1.11k
          pstr_api_struct->pstr_mps_212_enc, (WORD8 *)pstr_asc->str_aac_config.sac_cfg_data,
2068
1.11k
          sizeof(pstr_asc->str_aac_config.sac_cfg_data), AOT_USAC);
2069
1.11k
    }
2070
2.84k
    ixheaace_adjust_sbr_settings(
2071
2.84k
        &spectral_band_replication_config, pstr_sbr_config->bit_rate,
2072
2.84k
        (pstr_api_struct->pstr_state->mps_enable != 1) ? pstr_config->i_channels : 1,
2073
2.84k
        pstr_usac_config->core_sample_rate, AACENC_TRANS_FAC, 24000,
2074
2.84k
        pstr_api_struct->spectral_band_replication_tabs.ptr_qmf_tab,
2075
2.84k
        pstr_api_struct->pstr_state->aot, (pstr_api_struct->config[0].ccfl_idx == SBR_4_1));
2076
2077
2.84k
    error = ixheaace_env_open(
2078
2.84k
        &pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[0],
2079
2.84k
        &spectral_band_replication_config, &pstr_sbr_config->band_width, sbr_scr_ptr,
2080
2.84k
        &(pstr_api_struct->spectral_band_replication_tabs), &pstr_asc->str_aac_config.sbr_config);
2081
2.84k
    if (error) {
2082
13
      return error;
2083
13
    }
2084
2085
2.82k
    if (pstr_api_struct->config[0].ccfl_idx >= 2) {
2086
2.82k
      pstr_api_struct->pstr_state->downsample[0] = 1;
2087
2.82k
    } else {
2088
0
      pstr_api_struct->pstr_state->downsample[0] = 0;
2089
0
    }
2090
2091
2.82k
    if (pstr_api_struct->pstr_state->downsample[0]) {
2092
2.82k
      IA_ERRORCODE resamp_error = IA_NO_ERROR;
2093
2.82k
      WORD32 resamp_ratio = 0, upsamp_fac = 0, downsamp_fac = 0;
2094
2.82k
      WORD32 ele_idx = 0, ch_idx = 0;
2095
2096
2.82k
      if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
2097
325
        upsamp_fac = 3;
2098
325
        downsamp_fac = 8;
2099
325
        pstr_usac_config->sample_rate /= 2;
2100
2.50k
      } else if (pstr_api_struct->config[0].ccfl_idx == SBR_2_1) {
2101
1.93k
        resamp_ratio = 2;
2102
1.93k
        pstr_usac_config->sample_rate /= 2;
2103
1.93k
      } else if (pstr_api_struct->config[0].ccfl_idx == SBR_4_1) {
2104
570
        resamp_ratio = 4;
2105
570
        pstr_usac_config->sample_rate /= 4;
2106
570
      }
2107
2108
2.82k
      if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
2109
325
        if (upsamp_fac != 3 || downsamp_fac != 8) {
2110
0
          return IA_EXHEAACE_CONFIG_FATAL_USAC_RESAMPLER_RATIO;
2111
0
        }
2112
2.50k
      } else {
2113
2.50k
        if (resamp_ratio != 2 && resamp_ratio != 4) {
2114
0
          return IA_EXHEAACE_CONFIG_FATAL_USAC_RESAMPLER_RATIO;
2115
0
        }
2116
2.50k
      }
2117
2.82k
      if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {  // Upsampler initialization
2118
325
        resamp_error = ia_enhaacplus_enc_init_iir_sos_resampler(
2119
325
            &(pstr_api_struct->pstr_state->up_sampler[ele_idx][ch_idx]), upsamp_fac,
2120
325
            pstr_api_struct->spectral_band_replication_tabs.ptr_sos_upsamp_tab);
2121
325
        if (resamp_error) {
2122
0
          return resamp_error;
2123
0
        }
2124
325
        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
325
        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
325
        resamp_error = ia_enhaacplus_enc_init_iir_sos_resampler(
2150
325
            &(pstr_api_struct->pstr_state->down_samp_sos[ele_idx][ch_idx]), downsamp_fac,
2151
325
            pstr_api_struct->spectral_band_replication_tabs.ptr_sos_downsamp_tab);
2152
325
        if (resamp_error) {
2153
0
          return resamp_error;
2154
0
        }
2155
325
        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
325
        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.50k
      } else if (pstr_api_struct->config[0].ccfl_idx == SBR_2_1 ||
2181
2.50k
                 pstr_api_struct->config[0].ccfl_idx == SBR_4_1) {
2182
2.50k
        resamp_error = ia_enhaacplus_enc_init_iir_resampler(
2183
2.50k
            &(pstr_api_struct->pstr_state->down_sampler[ele_idx][ch_idx]), resamp_ratio,
2184
2.50k
            pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab);
2185
2.50k
        if (resamp_error) {
2186
0
          return resamp_error;
2187
0
        }
2188
2.50k
        if (pstr_api_struct->config[0].i_channels > 1) {
2189
1.93k
          resamp_error = ia_enhaacplus_enc_init_iir_resampler(
2190
1.93k
              &(pstr_api_struct->pstr_state->down_sampler[ele_idx][ch_idx + 1]), resamp_ratio,
2191
1.93k
              pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab);
2192
1.93k
          if (resamp_error) {
2193
0
            return resamp_error;
2194
0
          }
2195
1.93k
        }
2196
2.50k
        if (pstr_usac_config->sbr_harmonic) {
2197
1.39k
          resamp_error = ia_enhaacplus_enc_init_iir_resampler(
2198
1.39k
              &(pstr_api_struct->pstr_state->hbe_down_sampler[ele_idx][ch_idx]), resamp_ratio,
2199
1.39k
              pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab);
2200
1.39k
          if (resamp_error) {
2201
0
            return resamp_error;
2202
0
          }
2203
1.39k
          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.39k
        }
2212
2.50k
      }
2213
2.82k
    }
2214
2.82k
  }
2215
2216
3.99k
  error = iusace_enc_init(pstr_usac_config, &pstr_api_struct->pstr_state->audio_specific_config,
2217
3.99k
                          &pstr_api_struct->pstr_state->str_usac_enc_data);
2218
3.99k
  if (error & IA_FATAL_ERROR) {
2219
825
    return error;
2220
825
  }
2221
3.16k
  pstr_api_struct->pstr_state->str_usac_enc_data.frame_count = 0;
2222
3.16k
  pstr_usac_config->is_ipf = 1;
2223
3.16k
  pstr_enc_data->stereo_config_index = (pstr_enc_state->mps_enable == 1) ? 2 : 0;
2224
3.16k
  ia_bit_buf_struct *pstr_ia_asc_bit_buf;
2225
3.16k
  pstr_ia_asc_bit_buf = iusace_create_bit_buffer(
2226
3.16k
      &(pstr_api_struct->pstr_state->str_bit_buf), pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT],
2227
3.16k
      pstr_api_struct->pstr_mem_info[IA_MEMTYPE_OUTPUT].ui_size, 1);
2228
3.16k
  if (pstr_usac_config->sbr_enable) {
2229
9.60k
    for (UWORD32 idx = 0; idx < pstr_usac_config->num_elements; idx++) {
2230
7.31k
      switch (pstr_enc_state->audio_specific_config.str_usac_config.usac_element_type[idx]) {
2231
569
        case ID_USAC_SCE:
2232
2.29k
        case ID_USAC_CPE:
2233
2.29k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2234
2.29k
              .stereo_config_index = pstr_enc_data->stereo_config_index;
2235
2.29k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2236
2.29k
              .str_usac_sbr_config.bs_inter_tes = pstr_usac_config->sbr_inter_tes_active;
2237
2.29k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2238
2.29k
              .str_usac_sbr_config.bs_pvc = pstr_usac_config->sbr_pvc_active;
2239
2.29k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2240
2.29k
              .str_usac_sbr_config.dflt_header_extra1 = 0;
2241
2.29k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2242
2.29k
              .str_usac_sbr_config.dflt_header_extra2 = 0;
2243
2.29k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2244
2.29k
              .str_usac_sbr_config.dflt_start_freq = 0;
2245
2.29k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2246
2.29k
              .str_usac_sbr_config.dflt_stop_freq = 4;
2247
2.29k
          pstr_enc_state->audio_specific_config.str_usac_config.str_usac_element_config[idx]
2248
2.29k
              .str_usac_sbr_config.harmonic_sbr = pstr_usac_config->sbr_harmonic;
2249
2.29k
          break;
2250
5.02k
        default:
2251
5.02k
          continue;
2252
7.31k
      }
2253
7.31k
    }
2254
2.29k
  }
2255
2256
3.16k
  ixheaace_get_audiospecific_config_bytes(pstr_ia_asc_bit_buf,
2257
3.16k
                                          &pstr_api_struct->pstr_state->audio_specific_config,
2258
3.16k
                                          AOT_USAC, ccfl_idx);
2259
3.16k
  pstr_api_struct->pstr_state->i_out_bytes = (pstr_ia_asc_bit_buf->cnt_bits + 7) >> 3;
2260
2261
3.16k
  return IA_NO_ERROR;
2262
3.16k
}
2263
2264
4.99k
static IA_ERRORCODE ia_enhaacplus_enc_init(ixheaace_api_struct *pstr_api_struct, WORD32 ele_idx) {
2265
4.99k
  IA_ERRORCODE error = IA_NO_ERROR;
2266
4.99k
  WORD32 anc_flag = 0;
2267
4.99k
  WORD32 anc_rate = -1;
2268
4.99k
  WORD32 anc_mode = 0;
2269
4.99k
  WORD32 core_ch;
2270
4.99k
  WORD32 asc_channels;
2271
4.99k
  WORD8 *ptr_resampler_scratch;
2272
4.99k
  iaace_scratch *pstr_aac_scratch;
2273
4.99k
  WORD8 *ptr_spectral_band_replication_scratch;
2274
4.99k
  UWORD32 core_sample_rate;
2275
4.99k
  ixheaace_state_struct *pstr_enc_state = pstr_api_struct->pstr_state;
2276
4.99k
  ixheaace_audio_specific_config_struct *pstr_asc = &pstr_enc_state->audio_specific_config;
2277
2278
4.99k
  iaace_config *pstr_aac_config = &(pstr_api_struct->config[ele_idx].aac_config);
2279
2280
4.99k
  ixheaace_config_ancillary *pstr_ancillary = &(pstr_api_struct->config[ele_idx].pstr_ancillary);
2281
4.99k
  pstr_aac_scratch = (iaace_scratch *)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_SCRATCH_IDX];
2282
4.99k
  ptr_spectral_band_replication_scratch =
2283
4.99k
      ((pWORD8)pstr_aac_scratch) + ia_enhaacplus_enc_aac_enc_scr_size();
2284
4.99k
  ptr_resampler_scratch = ((pWORD8)pstr_aac_scratch) + ia_enhaacplus_enc_aac_enc_scr_size() +
2285
4.99k
                          ixheaace_sbr_enc_scr_size();
2286
2287
  /* fill pstr_ancillary data */
2288
4.99k
  pstr_ancillary->anc_flag = anc_flag;
2289
4.99k
  pstr_ancillary->anc_mode = anc_mode;
2290
4.99k
  pstr_ancillary->anc_rate = anc_rate;
2291
4.99k
  pstr_api_struct->pstr_state->temp_buff_aac =
2292
4.99k
      (void *)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_SCRATCH_IDX];
2293
4.99k
  pstr_api_struct->pstr_state->temp_buff_sbr =
2294
4.99k
      (void *)((WORD8 *)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_SCRATCH_IDX] +
2295
4.99k
               ia_enhaacplus_enc_aac_enc_scr_size());
2296
2297
4.99k
  if (pstr_api_struct->config[ele_idx].use_mps &&
2298
614
      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.54k
  } else {
2307
4.54k
    pstr_api_struct->pstr_state->ptr_temp_buff_resamp =
2308
4.54k
        (void *)((WORD8 *)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_SCRATCH_IDX] +
2309
4.54k
                 ia_enhaacplus_enc_aac_enc_scr_size() + ixheaace_sbr_enc_scr_size());
2310
4.54k
  }
2311
2312
4.99k
  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.47k
    pstr_api_struct->config[ele_idx].chmode_nchannels =
2316
2.47k
        pstr_api_struct->config[ele_idx].i_channels;
2317
2.47k
  }
2318
2319
4.99k
  switch (pstr_api_struct->pstr_state->aot) {
2320
1.09k
    case AOT_AAC_LC:
2321
2.91k
    case AOT_SBR:
2322
3.17k
    case AOT_PS:
2323
3.17k
      pstr_api_struct->pstr_state->buffer_offset = INPUT_DELAY_LC;
2324
3.17k
      break;
2325
2326
526
    case AOT_AAC_LD:
2327
526
      if (pstr_aac_config->flag_framelength_small == 1) {
2328
240
        pstr_api_struct->pstr_state->buffer_offset = INPUT_DELAY_LD_480;
2329
286
      } else {
2330
286
        pstr_api_struct->pstr_state->buffer_offset = INPUT_DELAY_LD_512;
2331
286
      }
2332
526
      break;
2333
2334
1.29k
    case AOT_AAC_ELD:
2335
1.29k
      if (pstr_aac_config->flag_framelength_small == 1) {
2336
598
        pstr_api_struct->pstr_state->buffer_offset = INPUT_DELAY_ELD_480;
2337
697
      } else {
2338
697
        pstr_api_struct->pstr_state->buffer_offset = INPUT_DELAY_ELD_512;
2339
697
      }
2340
1.29k
      break;
2341
4.99k
  }
2342
4.99k
  pstr_api_struct->pstr_state->downsample[ele_idx] = 0;
2343
4.99k
  pstr_api_struct->config->frame_count = 0;
2344
4.99k
  core_ch = pstr_api_struct->config[ele_idx].chmode_nchannels;
2345
4.99k
  pstr_aac_config->sample_rate = pstr_api_struct->config[ele_idx].sample_rate;
2346
4.99k
  pstr_aac_config->core_sample_rate = pstr_api_struct->config[ele_idx].sample_rate;
2347
4.99k
  pstr_aac_config->native_sample_rate = pstr_api_struct->config[ele_idx].native_sample_rate;
2348
4.99k
  pstr_aac_config->num_in_channels = pstr_api_struct->config[ele_idx].i_channels;
2349
4.99k
  pstr_aac_config->num_out_channels = pstr_api_struct->config[ele_idx].chmode_nchannels;
2350
4.99k
  if (pstr_api_struct->config[ele_idx].aac_classic == 0) {
2351
3.37k
    pstr_aac_config->core_sample_rate = pstr_aac_config->core_sample_rate / 2;
2352
3.37k
  }
2353
4.99k
  if (pstr_api_struct->pstr_state->mps_enable) {
2354
566
    switch (pstr_api_struct->pstr_state->mps_tree_config) {
2355
167
      case TREE_212:
2356
293
      case TREE_5151:
2357
329
      case TREE_5152:
2358
329
        pstr_aac_config->num_out_channels = 1;
2359
329
        core_ch = 1;
2360
329
        break;
2361
2362
237
      case TREE_525:
2363
237
        pstr_aac_config->num_out_channels = 2;
2364
237
        core_ch = 2;
2365
237
        break;
2366
566
    }
2367
566
  }
2368
2369
4.99k
  if (pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2370
254
    pstr_api_struct->config[ele_idx].chmode_nchannels = 2;
2371
254
    pstr_aac_config->num_out_channels = 1;
2372
254
    core_ch = 1;
2373
254
    pstr_api_struct->config[ele_idx].element_type = ID_SCE;
2374
254
  }
2375
4.99k
  if ((pstr_api_struct->config[ele_idx].i_channels == 2) &&
2376
2.47k
      (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.99k
  if ((pstr_api_struct->config[ele_idx].use_parametric_stereo) &&
2381
254
      (pstr_api_struct->config[ele_idx].aac_classic)) {
2382
0
    return (IA_EXHEAACE_CONFIG_FATAL_AAC_CLASSIC_WITH_PS);
2383
0
  }
2384
2385
4.99k
  if (!(pstr_api_struct->config[ele_idx].adts_flag) &&
2386
3.30k
      (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.99k
  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.99k
  pstr_aac_config->bit_rate = ixheaac_min32(360000 * core_ch, pstr_aac_config->bit_rate);
2400
4.99k
  pstr_aac_config->bit_rate = ixheaac_max32(8000 * core_ch, pstr_aac_config->bit_rate);
2401
4.99k
  switch (pstr_api_struct->pstr_state->aot) {
2402
1.09k
    case AOT_AAC_LC:
2403
2.91k
    case AOT_SBR:
2404
3.17k
    case AOT_PS:
2405
3.17k
      pstr_aac_config->bit_rate = ia_enhaacplus_aac_limitbitrate(
2406
3.17k
          pstr_aac_config->core_sample_rate,
2407
3.17k
          (pstr_aac_config->flag_framelength_small ? FRAME_LEN_960 : FRAME_LEN_1024), core_ch,
2408
3.17k
          pstr_aac_config->bit_rate);
2409
3.17k
      break;
2410
2411
526
    case AOT_AAC_LD:
2412
1.82k
    case AOT_AAC_ELD:
2413
1.82k
      pstr_aac_config->bit_rate = ia_enhaacplus_aac_limitbitrate(
2414
1.82k
          pstr_aac_config->core_sample_rate,
2415
1.82k
          (pstr_aac_config->flag_framelength_small ? FRAME_LEN_480 : FRAME_LEN_512), core_ch,
2416
1.82k
          pstr_aac_config->bit_rate);
2417
1.82k
      break;
2418
4.99k
  }
2419
4.99k
  if (pstr_api_struct->config[ele_idx].eldsbr_found == 1) {
2420
3.37k
    WORD32 num_iter = 0;
2421
3.37k
    WORD32 initial_bitrate, adjusted_bitrate;
2422
3.37k
    adjusted_bitrate = pstr_aac_config->bit_rate;
2423
2424
    /* Find total bitrate which provides valid configuration for each SBR element. */
2425
3.82k
    do {
2426
3.82k
      WORD32 e;
2427
3.82k
      WORD32 q_format;
2428
3.82k
      initial_bitrate = adjusted_bitrate;
2429
2430
11.4k
      for (e = 0; e < pstr_api_struct->config[ele_idx].num_bs_elements; e++) {
2431
8.54k
        WORD32 sbr_element_bitate_in, sbr_bitrate_out;
2432
2433
8.54k
        sbr_element_bitate_in = pstr_aac_config->bit_rate;
2434
8.54k
        sbr_bitrate_out = ixheaace_sbr_limit_bitrate(
2435
8.54k
            sbr_element_bitate_in, core_ch, pstr_aac_config->core_sample_rate,
2436
8.54k
            pstr_api_struct->spectral_band_replication_tabs.ptr_qmf_tab,
2437
8.54k
            pstr_api_struct->pstr_state->aot);
2438
2439
8.54k
        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.54k
        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.54k
      } /* elements */
2464
2465
3.82k
      num_iter++; /* restrict iteration to worst case of num elements */
2466
2467
3.82k
    } 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.37k
    pstr_aac_config->bit_rate = (initial_bitrate == adjusted_bitrate) ? adjusted_bitrate : 0;
2472
3.37k
  }
2473
2474
4.99k
  pstr_asc->str_aac_config.frame_length_flag = pstr_aac_config->flag_framelength_small;
2475
4.99k
  pstr_asc->sampling_frequency = pstr_api_struct->config[0].native_sample_rate;
2476
4.99k
  asc_channels = pstr_api_struct->config[ele_idx].i_channels_mode;
2477
4.99k
  pstr_asc->channel_configuration =
2478
4.99k
      (pstr_api_struct->pstr_state->mps_enable ? core_ch : asc_channels);
2479
4.99k
  pstr_asc->channel_configuration =
2480
4.99k
      pstr_api_struct->config[ele_idx].use_parametric_stereo
2481
4.99k
          ? 1
2482
4.99k
          : (pstr_api_struct->pstr_state->mps_enable
2483
4.73k
                 ? core_ch
2484
4.73k
                 : pstr_api_struct->config[ele_idx].i_channels_mode);
2485
2486
4.99k
  if (!(pstr_api_struct->config[ele_idx].aac_classic) &&
2487
3.37k
      ixheaace_is_sbr_setting_available(
2488
3.37k
          pstr_aac_config->bit_rate,
2489
3.37k
          (pstr_api_struct->pstr_state->mps_enable
2490
3.37k
               ? 1
2491
3.37k
               : pstr_api_struct->config[ele_idx].chmode_nchannels),
2492
3.37k
          pstr_aac_config->sample_rate, &core_sample_rate,
2493
3.37k
          pstr_api_struct->spectral_band_replication_tabs.ptr_qmf_tab,
2494
3.37k
          pstr_api_struct->pstr_state->aot)) {
2495
3.34k
    ixheaace_str_sbr_cfg spectral_band_replication_config;
2496
3.34k
    ixheaace_initialize_sbr_defaults(&spectral_band_replication_config);
2497
2498
3.34k
    spectral_band_replication_config.use_ps =
2499
3.34k
        pstr_api_struct->config[ele_idx].use_parametric_stereo;
2500
3.34k
    spectral_band_replication_config.crc_sbr = 0;
2501
3.34k
    spectral_band_replication_config.parametric_coding = 1;
2502
3.34k
    spectral_band_replication_config.is_esbr = pstr_api_struct->config[0].esbr_flag;
2503
3.34k
    if (pstr_api_struct->pstr_state->aot == AOT_AAC_ELD) {
2504
1.28k
      spectral_band_replication_config.is_ld_sbr = 1;
2505
1.28k
      spectral_band_replication_config.sbr_codec = ELD_SBR;
2506
1.28k
      spectral_band_replication_config.frame_flag_480 = pstr_aac_config->flag_framelength_small;
2507
2.05k
    } else if (pstr_api_struct->pstr_state->aot == AOT_SBR ||
2508
2.05k
               pstr_api_struct->pstr_state->aot == AOT_PS) {
2509
2.05k
      spectral_band_replication_config.frame_flag_960 = pstr_aac_config->flag_framelength_small;
2510
2.05k
      spectral_band_replication_config.sbr_codec = HEAAC_SBR;
2511
2.05k
    }
2512
2513
3.34k
    if (pstr_api_struct->config[ele_idx].aac_classic == 0) {
2514
3.34k
      pstr_api_struct->pstr_state->downsample[ele_idx] = 1;
2515
3.34k
    }
2516
2517
3.34k
    if (pstr_api_struct->pstr_state->mps_enable) {
2518
562
      if (pstr_api_struct->pstr_state->mps_tree_config == TREE_212) {
2519
163
        WORD32 delay =
2520
163
            ((pstr_aac_config->flag_framelength_small ? FRAME_LEN_480 / 2 : FRAME_LEN_512 / 2) *
2521
163
                 2 +
2522
163
             4);
2523
163
        ixheaace_mps_212_memory_struct *pstr_mps_memory;
2524
163
        pstr_mps_memory = pstr_api_struct->pstr_state->mps_pers_mem;
2525
163
        ixheaace_mps_212_open(&pstr_api_struct->pstr_mps_212_enc, pstr_mps_memory);
2526
2527
163
        pstr_asc->str_aac_config.num_sac_cfg_bits = 0;
2528
2529
163
        error = ixheaace_mps_212_initialise(
2530
163
            pstr_api_struct->pstr_mps_212_enc, AOT_AAC_ELD, pstr_aac_config->sample_rate,
2531
163
            &pstr_aac_config->bit_rate,
2532
163
            pstr_api_struct->config[ele_idx].eldsbr_found ? 2 /*hAacConfig->sbrRatio*/ : 0,
2533
163
            (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
163
            (pstr_aac_config->flag_framelength_small
2536
163
                 ? FRAME_LEN_480 * 2
2537
163
                 : FRAME_LEN_512 * 2) /* samples read per ch*/,
2538
163
            delay, (WORD8 *)pstr_api_struct->pstr_state->ptr_temp_buff_resamp);
2539
163
        if (error) {
2540
0
          return error;
2541
0
        }
2542
2543
163
        pstr_asc->str_aac_config.num_sac_cfg_bits = ixheaace_mps_212_get_spatial_specific_config(
2544
163
            pstr_api_struct->pstr_mps_212_enc, (WORD8 *)pstr_asc->str_aac_config.sac_cfg_data,
2545
163
            sizeof(pstr_asc->str_aac_config.sac_cfg_data), AOT_AAC_ELD);
2546
163
        pstr_asc->str_aac_config.eld_ext_type[0] = IAAC_ELDEXT_LDSAC;
2547
163
        pstr_asc->str_aac_config.eld_ext_len[0] =
2548
163
            (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
237
          tree_config = 525;
2557
237
        } else if (pstr_api_struct->pstr_state->mps_tree_config == TREE_5152) {
2558
36
          tree_config = 5152;
2559
126
        } else {
2560
126
          tree_config = 5151;
2561
126
        }
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
562
    }
2578
2579
3.34k
    ixheaace_adjust_sbr_settings(
2580
3.34k
        &spectral_band_replication_config, pstr_aac_config->bit_rate,
2581
3.34k
        pstr_aac_config->num_out_channels, core_sample_rate, AACENC_TRANS_FAC, 24000,
2582
3.34k
        pstr_api_struct->spectral_band_replication_tabs.ptr_qmf_tab,
2583
3.34k
        pstr_api_struct->pstr_state->aot, (pstr_api_struct->config[0].ccfl_idx == SBR_4_1));
2584
2585
3.34k
    if (pstr_api_struct->config[ele_idx].element_type != ID_LFE) {
2586
      /* open SBR PART, set core bandwidth */
2587
3.07k
      error = ixheaace_env_open(
2588
3.07k
          &pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[ele_idx],
2589
3.07k
          &spectral_band_replication_config, &pstr_aac_config->band_width,
2590
3.07k
          ptr_spectral_band_replication_scratch,
2591
3.07k
          &(pstr_api_struct->spectral_band_replication_tabs),
2592
3.07k
          &pstr_asc->str_aac_config.sbr_config);
2593
3.07k
      pstr_asc->str_aac_config.ld_sbr_present_flag =
2594
3.07k
          pstr_api_struct->config[ele_idx].eldsbr_found;
2595
3.07k
      if (error) {
2596
0
        return error;
2597
0
      }
2598
3.07k
      pstr_asc->str_aac_config.ld_sbr_sample_rate =
2599
3.07k
          (spectral_band_replication_config.codec_settings.sample_freq !=
2600
3.07k
           pstr_aac_config->sample_rate)
2601
3.07k
              ? 1
2602
3.07k
              : 0;
2603
3.07k
      pstr_asc->str_aac_config.ld_sbr_crc_flag = spectral_band_replication_config.crc_sbr;
2604
3.07k
    }
2605
2606
3.34k
    if (!pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2607
3.08k
      IA_ERRORCODE resamp_error = IA_NO_ERROR;
2608
3.08k
      WORD32 ch_idx = 0;
2609
2610
3.08k
      resamp_error = ia_enhaacplus_enc_init_iir_resampler(
2611
3.08k
          &(pstr_api_struct->pstr_state->down_sampler[ele_idx][ch_idx]), 2,
2612
3.08k
          pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab);
2613
3.08k
      if (resamp_error) {
2614
0
        return resamp_error;
2615
0
      }
2616
3.08k
      if (pstr_api_struct->config[ele_idx].i_channels > 1) {
2617
1.82k
        resamp_error = ia_enhaacplus_enc_init_iir_resampler(
2618
1.82k
            &(pstr_api_struct->pstr_state->down_sampler[ele_idx][ch_idx + 1]), 2,
2619
1.82k
            pstr_api_struct->spectral_band_replication_tabs.ptr_resamp_tab);
2620
1.82k
        if (resamp_error) {
2621
0
          return resamp_error;
2622
0
        }
2623
1.82k
      }
2624
3.08k
    }
2625
3.34k
  } else {
2626
1.64k
    if (!(pstr_api_struct->config[ele_idx].aac_classic &&
2627
1.62k
          !(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.62k
    pstr_api_struct->pstr_state->buffer_offset = 0;
2636
1.62k
    pstr_api_struct->config[ele_idx].aac_classic = 1;
2637
1.62k
  }
2638
4.96k
  {
2639
4.96k
    WORD32 *shared_buf1, *shared_buf2;
2640
4.96k
    WORD32 *shared_buf3;
2641
4.96k
    WORD8 *shared_buf4;
2642
4.96k
    switch (pstr_api_struct->pstr_state->aot) {
2643
1.09k
      case AOT_AAC_LC:
2644
2.90k
      case AOT_SBR:
2645
3.15k
      case AOT_PS:
2646
3.15k
        ia_enhaacplus_enc_get_shared_bufs(
2647
3.15k
            ptr_spectral_band_replication_scratch, &shared_buf1, &shared_buf2, &shared_buf3,
2648
3.15k
            &shared_buf4,
2649
3.15k
            (pstr_aac_config->flag_framelength_small == 1) ? FRAME_LEN_960 : FRAME_LEN_1024);
2650
3.15k
        break;
2651
2652
526
      case AOT_AAC_LD:
2653
1.81k
      case AOT_AAC_ELD:
2654
1.81k
        ia_enhaacplus_enc_get_shared_bufs(
2655
1.81k
            ptr_spectral_band_replication_scratch, &shared_buf1, &shared_buf2, &shared_buf3,
2656
1.81k
            &shared_buf4,
2657
1.81k
            (pstr_aac_config->flag_framelength_small == 1) ? FRAME_LEN_480 : FRAME_LEN_512);
2658
1.81k
        break;
2659
4.96k
    }
2660
2661
4.96k
    ia_enhaacplus_enc_set_shared_bufs(pstr_aac_scratch, &shared_buf1, &shared_buf2, &shared_buf3,
2662
4.96k
                                      &ptr_resampler_scratch);
2663
2664
4.96k
    error = ia_enhaacplus_enc_aac_enc_open(
2665
4.96k
        &(pstr_api_struct->pstr_state->aac_enc_pers_mem[ele_idx]),
2666
4.96k
        *pstr_aac_config /*, *pstr_ancillary*/, pstr_aac_scratch,
2667
4.96k
        &(pstr_api_struct->pstr_aac_tabs), pstr_api_struct->config[ele_idx].element_type,
2668
4.96k
        pstr_api_struct->config[ele_idx].element_instance_tag, pstr_api_struct->pstr_state->aot);
2669
4.96k
    if (error != IA_NO_ERROR) {
2670
0
      return error;
2671
0
    }
2672
4.96k
    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.96k
  }
2683
2684
4.96k
  return error;
2685
4.96k
}
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.11M
  for (ele = 0; ele < num_bs_elements; ele++) {
2731
808k
    total_channels += pstr_api_struct->config[ele].i_native_channels;
2732
808k
  }
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
216k
      pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2736
24.8k
    time_in_stride = 1;
2737
283k
  } else if ((pstr_api_struct->config[ele_idx].aac_classic == 1) &&
2738
91.4k
             (pstr_api_struct->config[ele_idx].num_bs_elements == 1)) {
2739
10.1k
    time_in_stride = pstr_aac_config->num_out_channels;
2740
272k
  } else {
2741
272k
    time_in_stride = IXHEAACE_MAX_CH_IN_BS_ELE;
2742
272k
  }
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
161k
    pstr_api_struct->pstr_state->i_out_bytes = 0;
2748
161k
    pub_out_buf = ((pUWORD8)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_OUTPUT_IDX]);
2749
161k
    *total_fill_bits = 0;
2750
161k
  }
2751
2752
307k
  if (pstr_api_struct->config->adts_flag) {
2753
88.8k
    pub_out_buf = (pUWORD8)(pub_out_buf + 7);
2754
88.8k
  }
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
140k
    write_off_set = INPUT_DELAY_LC * IXHEAACE_MAX_CH_IN_BS_ELE;
2761
167k
  } else if (aot == AOT_AAC_ELD && pstr_api_struct->pstr_state->mps_enable != 1) {
2762
42.3k
    if (pstr_api_struct->config[0].aac_config.flag_framelength_small)
2763
17.2k
      write_off_set = INPUT_DELAY_ELD_480 * IXHEAACE_MAX_CH_IN_BS_ELE;
2764
25.0k
    else
2765
25.0k
      write_off_set = INPUT_DELAY_ELD_512 * IXHEAACE_MAX_CH_IN_BS_ELE;
2766
124k
  } else if (aot == AOT_AAC_ELD && pstr_api_struct->pstr_state->mps_enable == 1 &&
2767
33.5k
             pstr_api_struct->pstr_state->mps_tree_config == TREE_212) {
2768
13.6k
    if (pstr_api_struct->config[0].aac_config.flag_framelength_small)
2769
9.30k
      write_off_set = INPUT_DELAY_ELDV2_480 * IXHEAACE_MAX_CH_IN_BS_ELE;
2770
4.33k
    else
2771
4.33k
      write_off_set = INPUT_DELAY_ELDV2_512 * IXHEAACE_MAX_CH_IN_BS_ELE;
2772
111k
  } else if (aot == AOT_AAC_ELD && pstr_api_struct->pstr_state->mps_enable == 1 &&
2773
19.8k
             pstr_api_struct->pstr_state->mps_tree_config != TREE_212) {
2774
19.8k
    write_off_set = INPUT_DELAY_ELD_512_MPS * IXHEAACE_MAX_CH_IN_BS_ELE;
2775
19.8k
  }
2776
307k
  if (pstr_api_struct->config[ele_idx].aac_classic == 1) {
2777
91.4k
    write_off_set = 0;
2778
91.4k
  }
2779
307k
  if (aot == AOT_AAC_ELD && pstr_api_struct->pstr_state->mps_enable == 1) {
2780
33.5k
    if (pstr_api_struct->pstr_state->mps_tree_config == TREE_212) {
2781
13.6k
      env_read_offset = INPUT_DELAY_ELD_512_MPS;
2782
19.8k
    } else {
2783
19.8k
      env_read_offset = write_off_set;
2784
19.8k
    }
2785
33.5k
  }
2786
307k
  if (pstr_api_struct->pstr_state->downsample[ele_idx]) {
2787
216k
    if (!pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2788
191k
      if (aot == AOT_AAC_ELD && pstr_api_struct->pstr_state->mps_enable != 1) {
2789
42.3k
        write_off_set +=
2790
42.3k
            4 * IXHEAACE_MAX_CH_IN_BS_ELE;  // Downsampler delay = 4 samples per channel @input SR
2791
149k
      } else {
2792
149k
        write_off_set += (pstr_api_struct->pstr_state->down_sampler[ele_idx][0].delay) *
2793
149k
                         IXHEAACE_MAX_CH_IN_BS_ELE;
2794
149k
      }
2795
191k
    }
2796
216k
    if (pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2797
24.8k
      env_read_offset = (MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY) * IXHEAACE_MAX_CH_IN_BS_ELE;
2798
24.8k
      write_off_set = env_read_offset;
2799
24.8k
    }
2800
216k
  }
2801
2802
307k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
2803
208k
    ptr_input_buffer = pstr_api_struct->pstr_state->inp_delay +
2804
208k
                       (aacenc_blocksize * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LC) *
2805
208k
                           IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2806
208k
  } else if (aot == AOT_AAC_LD) {
2807
23.3k
    if (pstr_api_struct->config[0].aac_config.flag_framelength_small) {
2808
9.29k
      ptr_input_buffer = pstr_api_struct->pstr_state->inp_delay +
2809
9.29k
                         (FRAME_LEN_480 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LD_480) *
2810
9.29k
                             IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2811
14.0k
    } else {
2812
14.0k
      ptr_input_buffer = pstr_api_struct->pstr_state->inp_delay +
2813
14.0k
                         (FRAME_LEN_512 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_LD_512) *
2814
14.0k
                             IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2815
14.0k
    }
2816
75.9k
  } else if (aot == AOT_AAC_ELD) {
2817
75.9k
    if (pstr_api_struct->config[0].aac_config.flag_framelength_small) {
2818
36.6k
      if (pstr_api_struct->pstr_state->mps_enable == 1) {
2819
19.3k
        ptr_input_buffer =
2820
19.3k
            pstr_api_struct->pstr_state->inp_delay +
2821
19.3k
            (FRAME_LEN_480 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_ELDV2_480) *
2822
19.3k
                IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2823
19.3k
      } else {
2824
17.2k
        ptr_input_buffer =
2825
17.2k
            pstr_api_struct->pstr_state->inp_delay +
2826
17.2k
            (FRAME_LEN_480 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_ELD_480) *
2827
17.2k
                IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2828
17.2k
      }
2829
39.2k
    } else {
2830
39.2k
      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
25.0k
      } else {
2836
25.0k
        ptr_input_buffer =
2837
25.0k
            pstr_api_struct->pstr_state->inp_delay +
2838
25.0k
            (FRAME_LEN_512 * 2 + MAXIMUM_DS_2_1_FILTER_DELAY + INPUT_DELAY_ELD_512) *
2839
25.0k
                IXHEAACE_MAX_CH_IN_BS_ELE * ele_idx;
2840
25.0k
      }
2841
39.2k
    }
2842
75.9k
  } 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
216k
    pstr_sbr_encoder =
2848
216k
        pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[ele_idx];
2849
216k
    if (pstr_api_struct->config[ele_idx].element_type != ID_LFE) {
2850
203k
      ixheaace_sbr_set_scratch_ptr(pstr_sbr_encoder, ptr_spectral_band_replication_scratch);
2851
203k
    }
2852
216k
  }
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
82.7k
        (pstr_api_struct->config[ele_idx].i_channels <= 2)) {
2862
62.8k
      if (pstr_api_struct->config[ele_idx].aac_classic != 1) {
2863
52.6k
        if ((pstr_api_struct->config[ele_idx].i_channels == 2 &&
2864
48.2k
             pstr_api_struct->config[ele_idx].chmode_nchannels == 2) &&
2865
48.2k
            (!((pstr_api_struct->pstr_mps_212_enc != NULL) &&
2866
34.6k
               pstr_api_struct->pstr_state->mps_enable))) {
2867
123M
          for (idx = 0; idx < (num_samples_read); idx++) {
2868
123M
            ptr_input_buffer[write_off_set + idx] = (FLOAT32)pw_inp_buf[idx];
2869
123M
          }
2870
34.6k
        } else if (pstr_api_struct->config[ele_idx].i_channels == 1) {
2871
8.74M
          for (idx = 0; idx < num_samples_read; idx++) {
2872
8.74M
            ptr_input_buffer[write_off_set + (IXHEAACE_MAX_CH_IN_BS_ELE * idx)] =
2873
8.74M
                (FLOAT32)pw_inp_buf[idx];
2874
8.74M
          }
2875
13.6k
        } else if ((pstr_api_struct->pstr_mps_212_enc != NULL) &&
2876
13.6k
                   pstr_api_struct->pstr_state->mps_enable) {
2877
13.6k
          ptr_input_buffer_mps = pstr_api_struct->pstr_state->time_signal_mps;
2878
13.3M
          for (idx = 0; idx < (num_samples_read / 2); idx++) {
2879
13.3M
            ptr_input_buffer_mps[idx] = (FLOAT32)pw_inp_buf[2 * idx];
2880
13.3M
            ptr_input_buffer_mps[(num_samples_read / 2) + idx] =
2881
13.3M
                (FLOAT32)pw_inp_buf[(2 * idx) + 1];
2882
13.3M
          }
2883
13.6k
        }
2884
52.6k
      } else {
2885
11.9M
        for (idx = 0; idx < (num_samples_read + write_off_set); idx++) {
2886
11.9M
          ptr_input_buffer[idx] = (FLOAT32)pw_inp_buf[idx];
2887
11.9M
        }
2888
10.1k
      }
2889
245k
    } else {
2890
245k
      if (pstr_api_struct->config[ele_idx].i_channels == 2) {
2891
114k
        slot = pstr_api_struct->config[ele_idx].element_slot;
2892
163M
        for (idx = 0; idx < num_samples_read / 2; idx++) {
2893
163M
          ptr_input_buffer[2 * idx + write_off_set] =
2894
163M
              (FLOAT32)pw_inp_buf[total_channels * idx + slot];
2895
163M
          ptr_input_buffer[2 * idx + write_off_set + 1] =
2896
163M
              (FLOAT32)pw_inp_buf[total_channels * idx + slot + 1];
2897
163M
        }
2898
114k
      }
2899
2900
245k
      if (pstr_api_struct->config[ele_idx].i_channels == 1) {
2901
110k
        slot = pstr_api_struct->config[ele_idx].element_slot;
2902
160M
        for (idx = 0; idx < num_samples_read; idx++) {
2903
160M
          ptr_input_buffer[write_off_set + (IXHEAACE_MAX_CH_IN_BS_ELE * idx)] =
2904
160M
              (FLOAT32)pw_inp_buf[total_channels * idx + slot];
2905
160M
        }
2906
110k
      }
2907
2908
245k
      if (pstr_api_struct->config[ele_idx].i_channels == 6) {
2909
19.8k
        ptr_input_buffer_mps = pstr_api_struct->pstr_state->time_signal_mps;
2910
118M
        for (idx = 0; idx < num_samples_read; idx++) {
2911
118M
          ptr_input_buffer_mps[idx] = (FLOAT32)pw_inp_buf[idx];
2912
118M
        }
2913
19.8k
      }
2914
245k
    }
2915
2916
307k
    if ((pstr_api_struct->pstr_mps_212_enc != NULL) && pstr_api_struct->pstr_state->mps_enable) {
2917
13.6k
      ptr_input_buffer_mps = pstr_api_struct->pstr_state->time_signal_mps;
2918
13.6k
      error = ixheaace_mps_212_process(pstr_api_struct->pstr_mps_212_enc, ptr_input_buffer_mps,
2919
13.6k
                                       num_samples_read, &mps_extension_payload);
2920
13.6k
      if (error) {
2921
0
        return error;
2922
0
      }
2923
13.6k
      num_samples_read /= 2;
2924
13.3M
      for (idx = 0; idx < num_samples_read; idx++) {
2925
13.3M
        ptr_input_buffer[2 * idx + write_off_set] = (FLOAT32)ptr_input_buffer_mps[idx];
2926
13.3M
      }
2927
13.6k
      env_read_offset = write_off_set;
2928
13.6k
    }
2929
307k
    if ((pstr_api_struct->pstr_mps_515_enc != NULL) && pstr_api_struct->pstr_state->mps_enable) {
2930
19.8k
      ixheaace_bit_buf bit_buf;
2931
19.8k
      ixheaace_bit_buf_handle ptr_bit_buf = NULL;
2932
19.8k
      FLOAT32 *ptr_downmix_buffer_mps = pstr_api_struct->pstr_state->mps_scratch;
2933
19.8k
      VOID *ptr_scratch_515_mps = (VOID *)(pstr_api_struct->pstr_state->mps_scratch +
2934
19.8k
                                           (MAX_INPUT_CHANNELS * MAX_BUFFER_SIZE) +
2935
19.8k
                                           (MAX_OUTPUT_CHANNELS * MAX_BUFFER_SIZE));
2936
19.8k
      ptr_bit_buf = ia_enhaacplus_enc_create_bitbuffer(&bit_buf, mps_bs, MAX_MPS_BS_PAYLOAD_SIZE);
2937
2938
19.8k
      error =
2939
19.8k
          ixheaace_mps_515_apply(pstr_api_struct->pstr_mps_515_enc, &ptr_input_buffer_mps[0],
2940
19.8k
                                 &ptr_downmix_buffer_mps[0], ptr_bit_buf, ptr_scratch_515_mps);
2941
19.8k
      if (error) {
2942
0
        return error;
2943
0
      }
2944
19.8k
      mps_extension_payload.data_size = ptr_bit_buf->cnt_bits;
2945
19.8k
      mps_extension_payload.data_type = IXHEAACE_MPS_EXT_LDSAC_DATA;
2946
19.8k
      mps_extension_payload.associated_ch_element = -1;
2947
2948
19.8k
      if (pstr_api_struct->pstr_state->mps_tree_config == TREE_5151 ||
2949
14.8k
          pstr_api_struct->pstr_state->mps_tree_config == TREE_5152) {
2950
6.72k
        num_samples_read /= 6;
2951
6.64M
        for (idx = 0; idx < num_samples_read; idx++) {
2952
6.63M
          ptr_input_buffer[2 * idx + write_off_set] = (FLOAT32)ptr_downmix_buffer_mps[idx];
2953
6.63M
        }
2954
13.1k
      } else {
2955
13.1k
        num_samples_read /= 3;
2956
26.1M
        for (idx = 0; idx < num_samples_read; idx++) {
2957
26.1M
          ptr_input_buffer[idx + write_off_set] = (FLOAT32)ptr_downmix_buffer_mps[idx];
2958
26.1M
        }
2959
13.1k
      }
2960
19.8k
      env_read_offset = write_off_set;
2961
19.8k
    }
2962
2963
307k
    if (pstr_api_struct->pstr_state->downsample[ele_idx]) {
2964
216k
      ixheaace_resampler_scratch *pstr_scratch_resampler =
2965
216k
          (ixheaace_resampler_scratch *)pstr_api_struct->pstr_state->ptr_temp_buff_resamp;
2966
2967
216k
      if (pstr_api_struct->config[ele_idx].element_type != ID_LFE) {
2968
203k
        error = ixheaace_env_encode_frame(
2969
203k
            pstr_sbr_encoder, ptr_input_buffer + env_read_offset, ptr_input_buffer,
2970
203k
            IXHEAACE_MAX_CH_IN_BS_ELE,
2971
203k
            &(pstr_api_struct->pstr_state->num_anc_data_bytes[ele_idx][0]),
2972
203k
            pstr_api_struct->pstr_state->anc_data_bytes[ele_idx],
2973
203k
            &(pstr_api_struct->spectral_band_replication_tabs), &(pstr_api_struct->common_tabs),
2974
203k
            &(mps_extension_payload.p_data[0]), mps_extension_payload.data_size,
2975
203k
            pstr_api_struct->config[0].aac_config.flag_framelength_small, NULL);
2976
2977
203k
        if (error != IA_NO_ERROR) {
2978
1.46k
          return error;
2979
1.46k
        }
2980
203k
      }
2981
2982
215k
      if (!pstr_api_struct->config[ele_idx].use_parametric_stereo) {
2983
473k
        for (ch = 0; ch < pstr_aac_config->num_out_channels; ch++) {
2984
283k
          ia_enhaacplus_enc_get_scratch_bufs(pstr_api_struct->pstr_state->temp_buff_sbr,
2985
283k
                                             &shared_buf1_ring, &shared_buf2_ring);
2986
283k
          {
2987
283k
            ia_enhaacplus_enc_iir_downsampler(
2988
283k
                &(pstr_api_struct->pstr_state->down_sampler[ele_idx][ch]),
2989
283k
                ptr_input_buffer + write_off_set + ch,
2990
283k
                num_samples_read / pstr_aac_config->num_out_channels, IXHEAACE_MAX_CH_IN_BS_ELE,
2991
283k
                ptr_input_buffer + ch, &out_samples, out_stride, shared_buf1_ring,
2992
283k
                shared_buf2_ring, pstr_scratch_resampler);
2993
283k
          }
2994
283k
        }
2995
190k
      }
2996
215k
    }
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
32.3k
      return error;
3011
32.3k
    }
3012
274k
    if (pstr_sbr_encoder && !(pstr_api_struct->config[ele_idx].use_parametric_stereo)) {
3013
155k
      if (pstr_sbr_encoder && (ptr_input_buffer != NULL)) {
3014
155k
        memmove(ptr_input_buffer,
3015
155k
                ptr_input_buffer + aacenc_blocksize * 2 * IXHEAACE_MAX_CH_IN_BS_ELE,
3016
155k
                write_off_set * sizeof(ptr_input_buffer[0]));
3017
155k
      }
3018
155k
    }
3019
274k
  }
3020
0
  {}
3021
  /*ADTS Header Write*/
3022
274k
  if (pstr_api_struct->config->adts_flag) {
3023
83.6k
    pub_out_buf = ((pUWORD8)pstr_api_struct->pp_mem[IA_ENHAACPLUSENC_OUTPUT_IDX]);
3024
83.6k
    {
3025
83.6k
      WORD32 num_channels = 0;
3026
3027
338k
      for (ele = 0; ele < pstr_api_struct->config[0].num_bs_elements; ele++) {
3028
254k
        num_channels += pstr_api_struct->config[ele].i_channels;
3029
254k
      }
3030
83.6k
      {
3031
83.6k
        header_bytes = ia_enhaacplus_enc_write_ADTS_header(
3032
83.6k
            pub_out_buf, pstr_api_struct->pstr_state->i_out_bytes,
3033
83.6k
            pstr_api_struct->config->aac_config.core_sample_rate,
3034
83.6k
            (pstr_api_struct->config[0].num_bs_elements > 1)
3035
83.6k
                ? num_channels
3036
83.6k
                : pstr_api_struct->config[0].aac_config.num_out_channels);
3037
83.6k
      }
3038
83.6k
    }
3039
3040
83.6k
    pstr_api_struct->pstr_state->i_out_bytes += header_bytes;
3041
83.6k
  }
3042
3043
274k
  return IA_NO_ERROR;
3044
306k
}
3045
321k
static IA_ERRORCODE iusace_process(ixheaace_api_struct *pstr_api_struct) {
3046
321k
  IA_ERRORCODE error = IA_NO_ERROR;
3047
321k
  WORD32 idx;
3048
321k
  WORD32 write_off_set = 0;
3049
321k
  WORD32 core_coder_frame_length;
3050
321k
  WORD32 usac_independency_flg;
3051
321k
  UWORD32 padding_bits = 0;
3052
321k
  WORD32 core_sample;
3053
321k
  WORD32 drc_sample;
3054
321k
  WORD32 ptr_inp_buf_offset = 0;
3055
321k
  WORD32 num_ch;
3056
321k
  WORD16 *ps_inp_buf = NULL;
3057
321k
  WORD8 *ps_out_buf = NULL;
3058
321k
  FLOAT32 *ptr_input_buffer = NULL;
3059
321k
  FLOAT32 *ptr_inp_buf[MAX_TIME_CHANNELS];
3060
321k
  FLOAT32 *ptr_drc_inp_buf[MAX_TIME_CHANNELS];
3061
321k
  WORD32 delay = 0;
3062
321k
  ixheaace_state_struct *pstr_state = pstr_api_struct->pstr_state;
3063
321k
  ia_bit_buf_struct *pstr_it_bit_buff = &pstr_state->str_bit_buf;
3064
321k
  ia_usac_encoder_config_struct *pstr_config = &pstr_api_struct->config[0].usac_config;
3065
321k
  ia_usac_data_struct *pstr_usac_data = &pstr_api_struct->pstr_state->str_usac_enc_data;
3066
321k
  iusace_scratch_mem *pstr_scratch = &pstr_usac_data->str_scratch;
3067
321k
  ia_classification_struct *pstr_sig_class_data =
3068
321k
      &pstr_state->str_usac_enc_data.str_sig_class_data;
3069
321k
  core_sample = (pstr_config->ccfl * pstr_config->channels);
3070
321k
  drc_sample = pstr_config->drc_frame_size * pstr_config->channels;
3071
321k
  core_coder_frame_length = pstr_config->ccfl;
3072
321k
  num_ch = pstr_config->channels;
3073
321k
  usac_independency_flg = pstr_usac_data->usac_independency_flag;
3074
321k
  ps_inp_buf = (WORD16 *)pstr_api_struct->pp_mem[IA_MEMTYPE_INPUT];
3075
321k
  ps_out_buf = (WORD8 *)pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT];
3076
3077
321k
  if (pstr_config->use_drc_element) {
3078
73.6k
    if (pstr_config->use_delay_adjustment == 1) {
3079
69.1k
      delay = (CC_DELAY_ADJUSTMENT * pstr_config->drc_frame_size / FRAME_LEN_1024) * num_ch;
3080
69.1k
    }
3081
225M
    for (idx = 0; idx < core_sample + delay; idx++) {
3082
225M
      pstr_api_struct->pstr_state->pp_drc_in_buf[idx % num_ch][idx / num_ch] =
3083
225M
          pstr_api_struct->pstr_state
3084
225M
              ->pp_drc_in_buf[idx % num_ch][idx / num_ch + pstr_config->drc_frame_size];
3085
225M
    }
3086
73.6k
    ptr_inp_buf_offset = pstr_config->drc_frame_size;
3087
205k
    for (idx = 0; idx < num_ch; idx++) {
3088
131k
      ptr_drc_inp_buf[idx] = pstr_api_struct->pstr_state->pp_drc_in_buf[idx];
3089
131k
    }
3090
73.6k
  }
3091
3092
321k
  ixheaace_pstr_sbr_enc pstr_sbr_encoder =
3093
321k
      pstr_api_struct->pstr_state->spectral_band_replication_enc_pers_mem[0];
3094
321k
  if (pstr_config->sbr_enable) {
3095
195k
    ixheaace_mps_enc_ext_payload mps_extension_payload;
3096
195k
    UWORD8 *mps_bs = pstr_api_struct->pstr_state->mps_bs;
3097
195k
    FLOAT32 *time_signal_mps = pstr_api_struct->pstr_state->time_signal_mps;
3098
195k
    WORD32 sbr_pvc_mode = 0;
3099
195k
    WORD32 sbr_patching_mode = 1;
3100
195k
    WORD32 ccfl_size;
3101
195k
    WORD32 num_samples_read;
3102
195k
    WORD32 out_samples, ch;
3103
195k
    WORD32 resamp_ratio =
3104
195k
        ia_enhaacplus_enc_compute_resampling_ratio(pstr_api_struct->config[0].ccfl_idx);
3105
195k
    switch (pstr_config->codec_mode) {
3106
86.6k
      case USAC_SWITCHED:
3107
86.6k
        if (pstr_usac_data->str_sig_class_data.coding_mode == 2) {
3108
15.0k
          sbr_pvc_mode = 0;
3109
71.6k
        } else {
3110
71.6k
          sbr_pvc_mode = 2;
3111
71.6k
        }
3112
86.6k
        sbr_patching_mode = 1;
3113
86.6k
        break;
3114
106k
      case USAC_ONLY_FD:
3115
106k
        sbr_pvc_mode = 0;
3116
106k
        sbr_patching_mode = 0;
3117
106k
        break;
3118
3.10k
      case USAC_ONLY_TD:
3119
3.10k
        sbr_pvc_mode = 2;
3120
3.10k
        sbr_patching_mode = 1;
3121
3.10k
        break;
3122
195k
    }
3123
3124
195k
    write_off_set = INPUT_DELAY_LC * IXHEAACE_MAX_CH_IN_BS_ELE;
3125
3126
195k
    if (pstr_api_struct->config[0].ccfl_idx == SBR_4_1) {
3127
49.0k
      write_off_set = write_off_set * 2;
3128
49.0k
    }
3129
3130
195k
    if (pstr_api_struct->pstr_state->downsample[0]) {
3131
195k
      if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
3132
27.7k
        write_off_set +=
3133
27.7k
            (pstr_api_struct->pstr_state->down_samp_sos[0][0].delay) * IXHEAACE_MAX_CH_IN_BS_ELE;
3134
3135
27.7k
        write_off_set +=
3136
27.7k
            (pstr_api_struct->pstr_state->up_sampler[0][0].delay) * IXHEAACE_MAX_CH_IN_BS_ELE;
3137
168k
      } else if (pstr_api_struct->config[0].ccfl_idx == SBR_2_1 ||
3138
168k
                 pstr_api_struct->config[0].ccfl_idx == SBR_4_1) {
3139
168k
        write_off_set +=
3140
168k
            (pstr_api_struct->pstr_state->down_sampler[0][0].delay) * IXHEAACE_MAX_CH_IN_BS_ELE;
3141
168k
      }
3142
195k
    }
3143
3144
195k
    ptr_input_buffer = pstr_api_struct->pstr_state->inp_delay;
3145
195k
    ccfl_size = pstr_api_struct->config[0].usac_config.ccfl;
3146
195k
    num_samples_read = ccfl_size * pstr_api_struct->config[0].i_channels;
3147
195k
    switch (pstr_api_struct->config[0].ccfl_idx) {
3148
27.7k
      case SBR_8_3:
3149
27.7k
        num_samples_read *= 8;
3150
27.7k
        num_samples_read /= 3;
3151
27.7k
        break;
3152
3153
119k
      case SBR_2_1:
3154
119k
        num_samples_read *= 2;
3155
119k
        break;
3156
3157
49.0k
      case SBR_4_1:
3158
49.0k
        num_samples_read *= 4;
3159
49.0k
        break;
3160
195k
    }
3161
3162
195k
    mps_extension_payload.p_data = mps_bs;
3163
195k
    memset(&mps_extension_payload, 0, sizeof(ixheaace_mps_enc_ext_payload));
3164
3165
195k
    if ((pstr_api_struct->pstr_mps_212_enc != NULL) && pstr_api_struct->pstr_state->mps_enable) {
3166
174M
      for (idx = 0; idx < num_samples_read / 2; idx++) {
3167
173M
        time_signal_mps[idx] = (FLOAT32)ps_inp_buf[2 * idx];
3168
173M
        time_signal_mps[num_samples_read / 2 + idx] = (FLOAT32)ps_inp_buf[2 * idx + 1];
3169
173M
      }
3170
72.6k
      ixheaace_mps_pstr_struct pstr_mps_enc =
3171
72.6k
          (ixheaace_mps_pstr_struct)pstr_api_struct->pstr_mps_212_enc;
3172
72.6k
      pstr_mps_enc->ptr_sac_encoder->independency_flag = usac_independency_flg;
3173
3174
72.6k
      error = ixheaace_mps_212_process(pstr_api_struct->pstr_mps_212_enc, time_signal_mps,
3175
72.6k
                                       num_samples_read, &mps_extension_payload);
3176
72.6k
      if (error) {
3177
0
        return error;
3178
0
      }
3179
72.6k
      if (pstr_api_struct->pstr_state->mps_enable == 1) {
3180
174M
        for (idx = 0; idx < num_samples_read / 2; idx++) {
3181
173M
          ptr_input_buffer[write_off_set + 2 * idx] = time_signal_mps[idx];
3182
173M
          ptr_input_buffer[write_off_set + 2 * idx + 1] =
3183
173M
              time_signal_mps[num_samples_read / 2 + idx];
3184
173M
        }
3185
72.6k
      }
3186
123k
    } else if (pstr_api_struct->config[0].i_channels == 2 &&
3187
65.4k
               pstr_api_struct->config[0].chmode_nchannels == 2) {
3188
333M
      for (idx = 0; idx < (num_samples_read); idx++) {
3189
333M
        ptr_input_buffer[write_off_set + idx] = (FLOAT32)ps_inp_buf[idx];
3190
333M
      }
3191
65.4k
    } else if (pstr_api_struct->config[0].i_channels == 1) {
3192
161M
      for (idx = 0; idx < num_samples_read; idx++) {
3193
161M
        ptr_input_buffer[write_off_set + (IXHEAACE_MAX_CH_IN_BS_ELE * idx)] =
3194
161M
            (FLOAT32)ps_inp_buf[idx];
3195
161M
      }
3196
57.7k
    }
3197
3198
195k
    if (num_ch == 2) {
3199
138k
      if (1 == pstr_config->use_drc_element) {
3200
150M
        for (idx = 0; idx < drc_sample; idx++) {
3201
150M
          ptr_drc_inp_buf[idx % num_ch][(idx >> 1) + ptr_inp_buf_offset] = ptr_input_buffer[idx];
3202
150M
        }
3203
36.7k
      }
3204
3205
      // update Header and bit-stream parameters
3206
138k
      if (0 == pstr_config->sbr_pvc_active) {
3207
96.2k
        sbr_pvc_mode = 0;
3208
96.2k
      }
3209
3210
138k
      ixheaace_set_usac_sbr_params(
3211
138k
          pstr_sbr_encoder, usac_independency_flg, 0, pstr_config->sbr_pvc_active, sbr_pvc_mode,
3212
138k
          pstr_config->sbr_inter_tes_active, pstr_config->sbr_harmonic, sbr_patching_mode);
3213
3214
      // Downsample SBR input buffer for Harmonic SBR
3215
138k
      if (pstr_config->sbr_harmonic) {
3216
98.3k
        FLOAT32 *in_buffer_temp;
3217
98.3k
        ixheaace_get_input_scratch_buf(pstr_api_struct->pstr_state->ptr_temp_buff_resamp,
3218
98.3k
                                       &in_buffer_temp);
3219
98.3k
        FLOAT32 *outbuf = ixheaace_get_hbe_resample_buffer(pstr_sbr_encoder);
3220
98.3k
        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
294k
        for (ch = 0; ch < num_ch; ch++) {
3228
196k
          FLOAT32 *shared_buf1_ring, *shared_buf2_ring;
3229
196k
          ixheaace_resampler_scratch *pstr_scratch_resampler =
3230
196k
              (ixheaace_resampler_scratch *)(ixheaace_resampler_scratch *)
3231
196k
                  pstr_api_struct->pstr_state->ptr_temp_buff_resamp;
3232
3233
196k
          ia_enhaacplus_enc_get_scratch_bufs(pstr_api_struct->pstr_state->temp_buff_sbr,
3234
196k
                                             &shared_buf1_ring, &shared_buf2_ring);
3235
3236
196k
          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
196k
          } else {
3250
196k
            WORD32 out_stride = IXHEAACE_MAX_CH_IN_BS_ELE * resamp_ratio;
3251
3252
196k
            ia_enhaacplus_enc_iir_downsampler(
3253
196k
                &(pstr_api_struct->pstr_state->hbe_down_sampler[0][ch]), ptr_input_buffer + ch,
3254
196k
                num_samples_read / pstr_api_struct->config[0].i_channels,
3255
196k
                IXHEAACE_MAX_CH_IN_BS_ELE, outbuf + ch, &out_samples, out_stride,
3256
196k
                shared_buf1_ring, shared_buf2_ring, pstr_scratch_resampler);
3257
196k
          }
3258
196k
        }
3259
98.3k
      }
3260
3261
      // SBR Encode
3262
138k
      error = ixheaace_env_encode_frame(
3263
138k
          pstr_sbr_encoder, ptr_input_buffer, ptr_input_buffer,
3264
138k
          pstr_api_struct->config[0].i_channels,
3265
138k
          &(pstr_api_struct->pstr_state->num_anc_data_bytes[0][0]),
3266
138k
          pstr_api_struct->pstr_state->anc_data_bytes[0],
3267
138k
          &(pstr_api_struct->spectral_band_replication_tabs), &(pstr_api_struct->common_tabs),
3268
138k
          &(mps_extension_payload.p_data[0]), mps_extension_payload.data_size, 0,
3269
138k
          &pstr_api_struct->pstr_state->str_usac_enc_data.num_sbr_bits);
3270
138k
      if (error != IA_NO_ERROR) {
3271
7.02k
        return error;
3272
7.02k
      }
3273
138k
    } else {
3274
57.7k
      if (0 == pstr_config->sbr_pvc_active) {
3275
32.9k
        sbr_pvc_mode = 0;
3276
32.9k
      }
3277
3278
57.7k
      ixheaace_set_usac_sbr_params(
3279
57.7k
          pstr_sbr_encoder, usac_independency_flg, 0, pstr_config->sbr_pvc_active, sbr_pvc_mode,
3280
57.7k
          pstr_config->sbr_inter_tes_active, pstr_config->sbr_harmonic, sbr_patching_mode);
3281
57.7k
      if (pstr_config->sbr_harmonic) {
3282
5.45k
        FLOAT32 *in_buffer_temp;
3283
5.45k
        ixheaace_get_input_scratch_buf(pstr_api_struct->pstr_state->ptr_temp_buff_resamp,
3284
5.45k
                                       &in_buffer_temp);
3285
5.45k
        FLOAT32 *outbuf = ixheaace_get_hbe_resample_buffer(pstr_sbr_encoder);
3286
5.45k
        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
10.9k
        for (ch = 0; ch < num_ch; ch++) {
3294
5.45k
          FLOAT32 *shared_buf1_ring, *shared_buf2_ring;
3295
5.45k
          ixheaace_resampler_scratch *pstr_scratch_resampler =
3296
5.45k
              (ixheaace_resampler_scratch *)(ixheaace_resampler_scratch *)
3297
5.45k
                  pstr_api_struct->pstr_state->ptr_temp_buff_resamp;
3298
3299
5.45k
          ia_enhaacplus_enc_get_scratch_bufs(pstr_api_struct->pstr_state->temp_buff_sbr,
3300
5.45k
                                             &shared_buf1_ring, &shared_buf2_ring);
3301
3302
5.45k
          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
5.45k
          } else {
3316
5.45k
            WORD32 out_stride = IXHEAACE_MAX_CH_IN_BS_ELE * resamp_ratio;
3317
3318
5.45k
            ia_enhaacplus_enc_iir_downsampler(
3319
5.45k
                &(pstr_api_struct->pstr_state->hbe_down_sampler[0][ch]),
3320
5.45k
                ptr_input_buffer /*input_buffer_fix + write_off_set*/ + ch,
3321
5.45k
                num_samples_read / pstr_api_struct->config[0].i_channels,
3322
5.45k
                IXHEAACE_MAX_CH_IN_BS_ELE, outbuf + ch, &out_samples, out_stride,
3323
5.45k
                shared_buf1_ring, shared_buf2_ring, pstr_scratch_resampler);
3324
5.45k
          }
3325
5.45k
        }
3326
5.45k
      }
3327
3328
57.7k
      FLOAT32 *time_signal = pstr_api_struct->pstr_state->time_signal;
3329
161M
      for (idx = 0; idx < num_samples_read; idx++) {
3330
161M
        time_signal[idx] = (FLOAT32)ptr_input_buffer[idx << 1];
3331
161M
      }
3332
3333
57.7k
      if (1 == pstr_config->use_drc_element) {
3334
14.3M
        for (idx = 0; idx < drc_sample; idx++) {
3335
14.3M
          ptr_drc_inp_buf[0][idx + ptr_inp_buf_offset] = time_signal[idx];
3336
14.3M
        }
3337
7.02k
      }
3338
3339
      // SBR Encode
3340
57.7k
      error = ixheaace_env_encode_frame(
3341
57.7k
          pstr_sbr_encoder, time_signal, time_signal, pstr_api_struct->config[0].i_channels,
3342
57.7k
          &(pstr_api_struct->pstr_state->num_anc_data_bytes[0][0]),
3343
57.7k
          pstr_api_struct->pstr_state->anc_data_bytes[0],
3344
57.7k
          &(pstr_api_struct->spectral_band_replication_tabs), &(pstr_api_struct->common_tabs),
3345
57.7k
          &(mps_extension_payload.p_data[0]), mps_extension_payload.data_size, 0,
3346
57.7k
          &pstr_api_struct->pstr_state->str_usac_enc_data.num_sbr_bits);
3347
57.7k
      if (error != IA_NO_ERROR) {
3348
2.00k
        return error;
3349
2.00k
      }
3350
57.7k
    }
3351
3352
    /* Resampling for USAC core */
3353
186k
    {
3354
186k
      FLOAT32 *in_buffer_temp;
3355
186k
      ixheaace_get_input_scratch_buf(pstr_api_struct->pstr_state->ptr_temp_buff_resamp,
3356
186k
                                     &in_buffer_temp);
3357
186k
      if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
3358
27.7k
        WORD32 input_tot = num_samples_read / pstr_api_struct->config[0].i_channels;
3359
27.7k
        ixheaace_upsampling_inp_buf_generation(ptr_input_buffer, in_buffer_temp, input_tot,
3360
27.7k
                                               UPSAMPLE_FAC, write_off_set - delay);
3361
27.7k
      }
3362
3363
504k
      for (ch = 0; ch < num_ch; ch++) {
3364
317k
        FLOAT32 *shared_buf1_ring, *shared_buf2_ring;
3365
317k
        ixheaace_resampler_scratch *pstr_scratch_resampler =
3366
317k
            (ixheaace_resampler_scratch *)pstr_api_struct->pstr_state->ptr_temp_buff_resamp;
3367
3368
317k
        ia_enhaacplus_enc_get_scratch_bufs(pstr_api_struct->pstr_state->temp_buff_sbr,
3369
317k
                                           &shared_buf1_ring, &shared_buf2_ring);
3370
3371
317k
        if (pstr_api_struct->config[0].ccfl_idx == SBR_8_3) {
3372
          // Upsampling by factor 3 - SOS implementation
3373
38.2k
          ia_enhaacplus_enc_iir_sos_upsampler(
3374
38.2k
              &(pstr_api_struct->pstr_state->up_sampler[0][ch]), in_buffer_temp + ch,
3375
38.2k
              num_samples_read / pstr_api_struct->config[0].i_channels, IXHEAACE_MAX_CH_IN_BS_ELE,
3376
38.2k
              in_buffer_temp + ch, &out_samples, shared_buf1_ring, shared_buf2_ring,
3377
38.2k
              pstr_scratch_resampler);
3378
3379
          // Downsampling by factor 8
3380
38.2k
          ia_enhaacplus_enc_iir_sos_downsampler(
3381
38.2k
              &(pstr_api_struct->pstr_state->down_samp_sos[0][ch]), in_buffer_temp + ch,
3382
38.2k
              out_samples, IXHEAACE_MAX_CH_IN_BS_ELE, ptr_input_buffer + ch, &out_samples,
3383
38.2k
              shared_buf1_ring, shared_buf2_ring, pstr_scratch_resampler);
3384
279k
        } else {
3385
279k
          WORD32 out_stride = IXHEAACE_MAX_CH_IN_BS_ELE * resamp_ratio;
3386
279k
          ia_enhaacplus_enc_iir_downsampler(
3387
279k
              &(pstr_api_struct->pstr_state->down_sampler[0][ch]),
3388
279k
              ptr_input_buffer + write_off_set - delay + ch,
3389
279k
              num_samples_read / pstr_api_struct->config[0].i_channels, IXHEAACE_MAX_CH_IN_BS_ELE,
3390
279k
              ptr_input_buffer + ch, &out_samples, out_stride, shared_buf1_ring, shared_buf2_ring,
3391
279k
              pstr_scratch_resampler);
3392
279k
        }
3393
317k
      }
3394
186k
    }
3395
3396
186k
    if (num_ch != 0) {
3397
504k
      for (idx = 0; idx < num_ch; idx++) {
3398
317k
        ptr_inp_buf[idx] = pstr_api_struct->pstr_state->ptr_in_buf[idx];
3399
317k
      }
3400
3401
186k
      if (num_ch == 1) {
3402
52.7M
        for (idx = 0; idx < core_sample; idx++) {
3403
52.7M
          ptr_inp_buf[0][idx] = ptr_input_buffer[idx << 1];
3404
52.7M
        }
3405
131k
      } else {
3406
263M
        for (idx = 0; idx < core_sample; idx++) {
3407
262M
          ptr_inp_buf[idx % num_ch][idx / num_ch] = ptr_input_buffer[idx];
3408
262M
        }
3409
131k
      }
3410
186k
    }
3411
186k
  } else {
3412
125k
    if (pstr_config->use_delay_adjustment == 1) {
3413
116k
      delay = ((CC_DELAY_ADJUSTMENT * core_coder_frame_length) / FRAME_LEN_1024) * num_ch;
3414
116k
    }
3415
125k
    if (num_ch != 0) {
3416
336k
      for (idx = 0; idx < num_ch; idx++) {
3417
211k
        ptr_inp_buf[idx] = pstr_api_struct->pstr_state->ptr_in_buf[idx];
3418
211k
      }
3419
3420
190M
      for (idx = 0; idx < core_sample; idx++) {
3421
190M
        ptr_inp_buf[idx % num_ch][(idx + delay) / num_ch] = ps_inp_buf[idx];
3422
190M
      }
3423
3424
125k
      if (1 == pstr_config->use_drc_element) {
3425
51.4M
        for (idx = 0; idx < drc_sample; idx++) {
3426
51.4M
          ptr_drc_inp_buf[idx % num_ch][(idx + delay) / num_ch + ptr_inp_buf_offset] =
3427
51.4M
              ps_inp_buf[idx];
3428
51.4M
        }
3429
29.8k
      }
3430
125k
    }
3431
125k
  }
3432
3433
312k
  if (pstr_sig_class_data->is_switch_mode) {
3434
98.6M
    for (idx = 0; idx < core_coder_frame_length; idx++) {
3435
98.5M
      pstr_sig_class_data->input_samples[pstr_sig_class_data->n_buffer_samples + idx] =
3436
98.5M
          pstr_api_struct->pstr_state->ptr_in_buf[0][idx];
3437
98.5M
    }
3438
104k
    pstr_sig_class_data->n_buffer_samples += core_coder_frame_length;
3439
104k
    iusace_classification(pstr_sig_class_data, pstr_scratch, core_coder_frame_length);
3440
104k
  }
3441
3442
312k
  pstr_it_bit_buff =
3443
312k
      iusace_create_bit_buffer(pstr_it_bit_buff, pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT],
3444
312k
                               pstr_api_struct->pstr_mem_info[IA_MEMTYPE_OUTPUT].ui_size, 1);
3445
312k
  if (pstr_it_bit_buff == NULL) {
3446
0
    return IA_EXHEAACE_INIT_FATAL_USAC_BITBUFFER_INIT_FAILED;
3447
0
  }
3448
312k
  error =
3449
312k
      ixheaace_usac_encode(pstr_api_struct->pstr_state->ptr_in_buf, pstr_config,
3450
312k
                           &pstr_api_struct->pstr_state->str_usac_enc_data,
3451
312k
                           &pstr_api_struct->pstr_state->audio_specific_config, pstr_it_bit_buff,
3452
312k
                           pstr_sbr_encoder, pstr_api_struct->pstr_state->pp_drc_in_buf,
3453
312k
                           &pstr_api_struct->pstr_state->is_quant_spec_zero,
3454
312k
                           &pstr_api_struct->pstr_state->is_gain_limited);
3455
312k
  if (error) return error;
3456
3457
307k
  padding_bits = 8 - (pstr_it_bit_buff->cnt_bits & 7);
3458
307k
  if (padding_bits > 0 && padding_bits < 8) {
3459
265k
    ps_out_buf[pstr_it_bit_buff->cnt_bits >> 3] =
3460
265k
        (WORD8)((UWORD32)ps_out_buf[pstr_it_bit_buff->cnt_bits >> 3]) & (0xFF << padding_bits);
3461
265k
  }
3462
307k
  pstr_api_struct->pstr_state->i_out_bytes =
3463
307k
      (padding_bits > 0 && padding_bits < 8) ? (pstr_it_bit_buff->cnt_bits + padding_bits) >> 3
3464
307k
                                             : pstr_it_bit_buff->cnt_bits >> 3;
3465
307k
  pstr_api_struct->pstr_state->i_out_bits = pstr_it_bit_buff->cnt_bits;
3466
307k
  ixheaace_write_audio_preroll_data(pstr_api_struct, pstr_it_bit_buff);
3467
307k
  pstr_state->str_usac_enc_data.frame_count++;
3468
307k
  pstr_usac_data->usac_independency_flag_count =
3469
307k
      (pstr_usac_data->usac_independency_flag_count + 1) %
3470
307k
      pstr_usac_data->usac_independency_flag_interval;
3471
3472
307k
  if (pstr_config->sbr_enable) {
3473
186k
    WORD32 num_samples = pstr_api_struct->config[0].usac_config.ccfl * IXHEAACE_MAX_CH_IN_BS_ELE;
3474
186k
    switch (pstr_api_struct->config[0].ccfl_idx) {
3475
27.7k
      case SBR_8_3:
3476
27.7k
        num_samples *= 8;
3477
27.7k
        num_samples /= 3;
3478
27.7k
        break;
3479
3480
113k
      case SBR_2_1:
3481
113k
        num_samples *= 2;
3482
113k
        break;
3483
3484
46.0k
      case SBR_4_1:
3485
46.0k
        num_samples *= 4;
3486
46.0k
        break;
3487
186k
    }
3488
3489
186k
    if (ptr_input_buffer != NULL) {
3490
186k
      memmove(ptr_input_buffer, ptr_input_buffer + num_samples,
3491
186k
              write_off_set * sizeof(ptr_input_buffer[0]));
3492
186k
    }
3493
186k
  } else if (!pstr_config->sbr_enable && pstr_config->use_delay_adjustment) {
3494
296k
    for (idx = 0; idx < num_ch; idx++) {
3495
185k
      memmove(&ptr_inp_buf[idx][0], &ptr_inp_buf[idx][core_sample / num_ch],
3496
185k
              sizeof(ptr_inp_buf[idx][0]) * delay / num_ch);
3497
185k
    }
3498
111k
  }
3499
3500
307k
  return IA_NO_ERROR;
3501
307k
}
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.50k
                                                ixheaace_input_config *pstr_input_config) {
3513
3.50k
  ia_drc_input_config *pstr_internal_drc_cfg;
3514
3.50k
  ia_drc_uni_drc_config_struct *pstr_uni_drc_config;
3515
3.50k
  ia_drc_loudness_info_set_struct *pstr_enc_loudness_info_set;
3516
3.50k
  WORD32 n, m;
3517
3518
3.50k
  pstr_internal_drc_cfg =
3519
3.50k
      (ia_drc_input_config *)&pstr_api_struct->config[0].usac_config.str_drc_cfg;
3520
3.50k
  if (pstr_input_config->use_drc_element == 0) {
3521
2.59k
    memset(pstr_internal_drc_cfg, 0, sizeof(ia_drc_input_config));
3522
2.59k
  }
3523
3.50k
  pstr_uni_drc_config = &pstr_internal_drc_cfg->str_uni_drc_config;
3524
3.50k
  pstr_enc_loudness_info_set = &pstr_internal_drc_cfg->str_enc_loudness_info_set;
3525
3526
3.50k
  pstr_uni_drc_config->sample_rate_present = 1;
3527
3.50k
  pstr_uni_drc_config->loudness_info_set_present = 1;
3528
3.50k
  pstr_enc_loudness_info_set->loudness_info_count = 1;
3529
3530
7.01k
  for (n = 0; n < pstr_enc_loudness_info_set->loudness_info_count; n++) {
3531
3.50k
    pstr_enc_loudness_info_set->str_loudness_info[n].drc_set_id = 0;
3532
3.50k
    pstr_enc_loudness_info_set->str_loudness_info[n].downmix_id = 0;
3533
3.50k
    pstr_enc_loudness_info_set->str_loudness_info[n].sample_peak_level_present = 1;
3534
3.50k
    pstr_enc_loudness_info_set->str_loudness_info[n].sample_peak_level =
3535
3.50k
        pstr_input_config->sample_peak_level;
3536
3.50k
    pstr_enc_loudness_info_set->str_loudness_info[n].true_peak_level_present = 0;
3537
3.50k
    pstr_enc_loudness_info_set->str_loudness_info[n].measurement_count = 1;
3538
3539
7.01k
    for (m = 0; m < pstr_enc_loudness_info_set->str_loudness_info[n].measurement_count; m++) {
3540
3.50k
      pstr_enc_loudness_info_set->str_loudness_info[n].str_loudness_measure[m].method_definition =
3541
3.50k
          pstr_input_config->method_def;
3542
3.50k
      pstr_enc_loudness_info_set->str_loudness_info[n].str_loudness_measure[m].method_value =
3543
3.50k
          (FLOAT32)pstr_input_config->measured_loudness;
3544
3.50k
      pstr_enc_loudness_info_set->str_loudness_info[n]
3545
3.50k
          .str_loudness_measure[m]
3546
3.50k
          .measurement_system = pstr_input_config->measurement_system;
3547
3.50k
      pstr_enc_loudness_info_set->str_loudness_info[n].str_loudness_measure[m].reliability = 3;
3548
3.50k
    }
3549
3.50k
  }
3550
3.50k
}
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.10k
    pstr_input_config->aot = AOT_USAC;
3561
4.10k
  }
3562
7.26k
  if (pstr_input_config->aot != AOT_AAC_ELD && pstr_input_config->aot != AOT_AAC_LC &&
3563
5.95k
      pstr_input_config->aot != AOT_AAC_LD && pstr_input_config->aot != AOT_SBR &&
3564
5.09k
      pstr_input_config->aot != AOT_PS && pstr_input_config->aot != AOT_USAC) {
3565
397
    return IA_EXHEAACE_API_FATAL_UNSUPPORTED_AOT;
3566
397
  }
3567
6.87k
  ui_api_size = sizeof(ixheaace_api_struct);
3568
6.87k
  pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] =
3569
6.87k
      pstr_output_config->malloc_xheaace(ui_api_size + EIGHT_BYTE_SIZE, DEFAULT_MEM_ALIGN_8);
3570
6.87k
  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.87k
  memset(pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count], 0, ui_api_size);
3574
3575
6.87k
  pstr_output_config->ui_rem =
3576
6.87k
      (SIZE_T)((SIZE_T)pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] %
3577
6.87k
               BYTE_ALIGN_8);
3578
3579
6.87k
  pstr_output_config->pv_ia_process_api_obj =
3580
6.87k
      (pVOID)((WORD8 *)pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] +
3581
6.87k
              BYTE_ALIGN_8 - pstr_output_config->ui_rem);
3582
6.87k
  pstr_output_config->malloc_count++;
3583
3584
6.87k
  pstr_api_struct = (ixheaace_api_struct *)pstr_output_config->pv_ia_process_api_obj;
3585
6.87k
  memset(pstr_api_struct, 0, sizeof(*pstr_api_struct));
3586
3587
6.87k
  ixheaace_set_default_config(pstr_api_struct, pstr_input_config);
3588
3589
6.87k
  err_code = ixheaace_set_config_params(pstr_api_struct, pstr_input_config);
3590
6.87k
  if (err_code) {
3591
411
    return err_code;
3592
411
  }
3593
3594
6.46k
  if (pstr_input_config->aot == AOT_USAC) {
3595
4.03k
    if (pstr_api_struct->config[0].usac_config.is_loudness_configured == 0) {
3596
3.50k
      ixheaace_get_measured_loudness_info(pstr_api_struct, pstr_input_config);
3597
3.50k
      memcpy(pstr_input_config->pv_drc_cfg, &pstr_api_struct->config[0].usac_config.str_drc_cfg,
3598
3.50k
             sizeof(ia_drc_input_config));
3599
3.50k
    }
3600
4.03k
  }
3601
3602
6.46k
  pstr_output_config->ui_proc_mem_tabs_size =
3603
6.46k
      (sizeof(ixheaace_mem_info_struct) + sizeof(pVOID *)) * 4;
3604
6.46k
  pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] =
3605
6.46k
      pstr_output_config->malloc_xheaace(
3606
6.46k
          pstr_output_config->ui_proc_mem_tabs_size + EIGHT_BYTE_SIZE, DEFAULT_MEM_ALIGN_8);
3607
6.46k
  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.46k
  memset(pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count], 0,
3611
6.46k
         pstr_output_config->ui_proc_mem_tabs_size);
3612
3613
6.46k
  pstr_output_config->ui_rem =
3614
6.46k
      (SIZE_T)((SIZE_T)pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] %
3615
6.46k
               BYTE_ALIGN_8);
3616
3617
6.46k
  pv_value =
3618
6.46k
      (pVOID)((WORD8 *)pstr_output_config->arr_alloc_memory[pstr_output_config->malloc_count] +
3619
6.46k
              BYTE_ALIGN_8 - pstr_output_config->ui_rem);
3620
6.46k
  if (pv_value == NULL) {
3621
0
    return IA_EXHEAACE_API_FATAL_MEM_ALLOC;
3622
0
  }
3623
6.46k
  memset(pv_value, 0, (sizeof(ixheaace_mem_info_struct) + sizeof(pVOID *)) * 4);
3624
3625
6.46k
  pstr_api_struct->pstr_mem_info = (ixheaace_mem_info_struct *)pv_value;
3626
6.46k
  pstr_api_struct->pp_mem = (pVOID *)((WORD8 *)pv_value + sizeof(ixheaace_mem_info_struct) * 4);
3627
3628
6.46k
  pstr_output_config->malloc_count++;
3629
3630
6.46k
  ixheaace_fill_mem_tabs(pstr_api_struct, pstr_input_config->aot);
3631
3632
6.46k
  err_code =
3633
6.46k
      ixheaace_alloc_and_assign_mem(pstr_api_struct, pstr_output_config, pstr_input_config);
3634
6.46k
  if (err_code) {
3635
0
    return err_code;
3636
0
  }
3637
3638
6.46k
  pstr_output_config->is_loudness_configured =
3639
6.46k
      pstr_api_struct->config[0].usac_config.is_loudness_configured;
3640
3641
6.46k
  return err_code;
3642
6.46k
}
3643
3644
6.46k
IA_ERRORCODE ixheaace_init(pVOID pstr_obj_ixheaace, pVOID pv_input, pVOID pv_output) {
3645
6.46k
  IA_ERRORCODE error = IA_NO_ERROR;
3646
6.46k
  WORD32 frame_length;
3647
6.46k
  WORD32 channels, ele_idx;
3648
6.46k
  ixheaace_api_struct *pstr_api_struct = (ixheaace_api_struct *)pstr_obj_ixheaace;
3649
6.46k
  ixheaace_input_config *pstr_input_config = (ixheaace_input_config *)pv_input;
3650
6.46k
  ixheaace_output_config *pstr_output_config = (ixheaace_output_config *)pv_output;
3651
6.46k
  WORD32 total_bitrate_used = 0;
3652
6.46k
  frame_length = pstr_input_config->frame_length;
3653
6.46k
  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.46k
  pstr_api_struct->pstr_state->aot = pstr_input_config->aot;
3658
3659
6.46k
  if ((pstr_api_struct->config[0].use_mps == 1) &&
3660
1.80k
      (0 == pstr_api_struct->config->aac_classic ||
3661
1.79k
       pstr_api_struct->pstr_state->aot == AOT_USAC)) {
3662
1.79k
    pstr_api_struct->pstr_state->mps_enable = pstr_api_struct->config[0].use_mps;
3663
1.79k
    pstr_api_struct->pstr_state->mps_tree_config = pstr_api_struct->config[0].mps_tree_config;
3664
1.79k
  }
3665
6.46k
  if (pstr_api_struct->config[0].num_bs_elements == 1) {
3666
5.32k
    pstr_api_struct->config[ele_idx].write_program_config_element = 0;
3667
5.32k
  }
3668
3669
6.46k
  if (pstr_api_struct->pstr_state->aot != AOT_USAC) {
3670
7.38k
    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.99k
      pstr_api_struct->pstr_state->pstr_config[ele_idx] = &pstr_api_struct->config[ele_idx];
3673
3674
4.99k
      error = ia_enhaacplus_enc_init(pstr_api_struct, ele_idx);
3675
4.99k
      if (error) {
3676
27
        return error;
3677
27
      }
3678
3679
4.96k
      pstr_api_struct->pstr_state->ui_init_done = 1;
3680
4.96k
      total_bitrate_used += pstr_api_struct->config[ele_idx].aac_config.bit_rate;
3681
4.96k
    }
3682
2.39k
    if (pstr_input_config->i_bitrate != total_bitrate_used) {
3683
1.70k
      pstr_input_config->i_bitrate = total_bitrate_used;
3684
1.70k
    }
3685
2.39k
    if (pstr_api_struct->config[0].aac_config.bitreservoir_size != -1) {
3686
2.26k
      WORD32 avg_bytes_per_frame_per_ch = pstr_api_struct->config[0].aac_config.bitreservoir_size;
3687
2.26k
      if (pstr_api_struct->config[0].aac_config.flag_framelength_small) {
3688
898
        if (pstr_api_struct->config[0].aot == AOT_AAC_LC ||
3689
758
            pstr_api_struct->config[0].aot == AOT_PS ||
3690
643
            pstr_api_struct->config[0].aot == AOT_SBR) {
3691
448
          avg_bytes_per_frame_per_ch = (pstr_api_struct->config[0].aac_config.bit_rate) *
3692
448
                                       FRAME_LEN_960 /
3693
448
                                       (pstr_api_struct->config[0].aac_config.core_sample_rate *
3694
448
                                        pstr_api_struct->config[0].i_channels * 8);
3695
448
        }
3696
898
        if (pstr_input_config->aot == AOT_AAC_LD || pstr_input_config->aot == AOT_AAC_ELD) {
3697
450
          avg_bytes_per_frame_per_ch = (pstr_api_struct->config[0].aac_config.bit_rate) *
3698
450
                                       FRAME_LEN_480 /
3699
450
                                       (pstr_api_struct->config[0].aac_config.core_sample_rate *
3700
450
                                        pstr_api_struct->config[0].i_channels * 8);
3701
450
        }
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.05k
            pstr_api_struct->config[0].aot == AOT_SBR) {
3706
819
          avg_bytes_per_frame_per_ch = (pstr_api_struct->config[0].aac_config.bit_rate) *
3707
819
                                       FRAME_LEN_1024 /
3708
819
                                       (pstr_api_struct->config[0].aac_config.core_sample_rate *
3709
819
                                        pstr_api_struct->config[0].i_channels * 8);
3710
819
        }
3711
1.36k
        if (pstr_input_config->aot == AOT_AAC_LD || pstr_input_config->aot == AOT_AAC_ELD) {
3712
547
          avg_bytes_per_frame_per_ch = (pstr_api_struct->config[0].aac_config.bit_rate) *
3713
547
                                       FRAME_LEN_512 /
3714
547
                                       (pstr_api_struct->config[0].aac_config.core_sample_rate *
3715
547
                                        pstr_api_struct->config[0].i_channels * 8);
3716
547
        }
3717
1.36k
      }
3718
3719
2.26k
      if (pstr_api_struct->config[0].aac_config.bitreservoir_size < avg_bytes_per_frame_per_ch) {
3720
182
        return IA_EXHEAACE_CONFIG_NONFATAL_BITRES_SIZE_TOO_SMALL;
3721
182
      }
3722
2.26k
    }
3723
2.21k
    if (pstr_input_config->i_use_es) {
3724
      // Write GA header
3725
1.74k
      ia_bit_buf_struct *pstr_ia_asc_bit_buf;
3726
1.74k
      pstr_ia_asc_bit_buf = iusace_create_bit_buffer(
3727
1.74k
          &(pstr_api_struct->pstr_state->str_bit_buf), pstr_api_struct->pp_mem[IA_MEMTYPE_OUTPUT],
3728
1.74k
          pstr_api_struct->pstr_mem_info[IA_MEMTYPE_OUTPUT].ui_size, 1);
3729
3730
1.74k
      ixheaace_get_audiospecific_config_bytes(pstr_ia_asc_bit_buf,
3731
1.74k
                                              &pstr_api_struct->pstr_state->audio_specific_config,
3732
1.74k
                                              pstr_api_struct->pstr_state->aot,
3733
1.74k
                                              pstr_input_config->ccfl_idx);
3734
3735
1.74k
      pstr_api_struct->pstr_state->i_out_bytes = (pstr_ia_asc_bit_buf->cnt_bits + 7) >> 3;
3736
1.74k
    }
3737
2.21k
    if (pstr_api_struct->config->aac_classic) {
3738
650
      pstr_output_config->input_size =
3739
650
          frame_length * channels * pstr_api_struct->config[0].ui_pcm_wd_sz / 8;
3740
1.56k
    } else {
3741
1.56k
      pstr_output_config->input_size =
3742
1.56k
          2 * frame_length * channels * pstr_api_struct->config[0].ui_pcm_wd_sz / 8;
3743
1.56k
    }
3744
2.21k
    pstr_output_config->samp_freq = pstr_api_struct->config[0].native_sample_rate;
3745
2.21k
    pstr_output_config->header_samp_freq = pstr_api_struct->config[0].aac_config.core_sample_rate;
3746
2.21k
    pstr_output_config->down_sampling_ratio =
3747
2.21k
        pstr_api_struct->config->aac_classic == 0 ? 2.0f : 1.0f;
3748
2.21k
    switch (pstr_api_struct->config->aot) {
3749
290
      case AOT_AAC_LC:
3750
290
        pstr_output_config->audio_profile = AUDIO_PROFILE_AAC_LC_L5;
3751
290
        break;
3752
751
      case AOT_SBR:
3753
751
        pstr_output_config->audio_profile = AUDIO_PROFILE_HEAAC_L5;
3754
751
        break;
3755
229
      case AOT_PS:
3756
229
        pstr_output_config->audio_profile = AUDIO_PROFILE_HEAAC_V2_L5;
3757
229
        break;
3758
152
      case AOT_AAC_LD:
3759
152
        pstr_output_config->audio_profile = AUDIO_PROFILE_AAC_LD_L4;
3760
152
        break;
3761
794
      case AOT_AAC_ELD:
3762
794
        if (pstr_api_struct->config[0].use_mps) {
3763
480
          if (pstr_api_struct->config[0].mps_tree_config == TREE_212) {
3764
132
            pstr_output_config->audio_profile = AUDIO_PROFILE_AAC_ELD_L2;
3765
348
          } else {
3766
348
            pstr_output_config->audio_profile = AUDIO_PROFILE_AAC_ELD_L4;
3767
348
          }
3768
480
        } else {
3769
314
          pstr_output_config->audio_profile = AUDIO_PROFILE_AAC_ELD_L1;
3770
314
        }
3771
794
        break;
3772
0
      default:
3773
0
        pstr_output_config->audio_profile = AUDIO_PROFILE_NOT_SPECIFIED;
3774
0
        break;
3775
2.21k
    }
3776
2.21k
  }
3777
3778
4.03k
  else {
3779
4.03k
    pstr_api_struct->pstr_state->pstr_config[0] = &pstr_api_struct->config[0];
3780
4.03k
    error = ia_usac_enc_init(pstr_api_struct, pstr_input_config->ccfl_idx);
3781
4.03k
    if (error) {
3782
869
      return error;
3783
869
    }
3784
3785
3.16k
    pstr_output_config->input_size =
3786
3.16k
        frame_length * channels * (pstr_api_struct->config[0].usac_config.ui_pcm_wd_sz >> 3);
3787
3788
3.16k
    if (pstr_api_struct->config[0].usac_config.use_drc_element) {
3789
608
      ia_drc_input_config *pstr_drc_cfg = (ia_drc_input_config *)(pstr_input_config->pv_drc_cfg);
3790
608
      memcpy(pstr_drc_cfg, &pstr_api_struct->config[0].usac_config.str_drc_cfg,
3791
608
             sizeof(ia_drc_input_config));
3792
608
    }
3793
3794
3.16k
    pstr_output_config->down_sampling_ratio = 1;
3795
3.16k
    if (pstr_api_struct->config[0].usac_config.sbr_enable == 1) {
3796
2.29k
      switch (pstr_api_struct->config[0].ccfl_idx) {
3797
299
        case SBR_8_3:
3798
299
          pstr_output_config->input_size *= 8;
3799
299
          pstr_output_config->input_size /= 3;
3800
299
          pstr_output_config->down_sampling_ratio = 8.0f / 3.0f;
3801
299
          break;
3802
3803
1.44k
        case SBR_2_1:
3804
1.44k
          pstr_output_config->input_size *= 2;
3805
1.44k
          pstr_output_config->down_sampling_ratio = 2;
3806
1.44k
          break;
3807
3808
544
        case SBR_4_1:
3809
544
          pstr_output_config->input_size *= 4;
3810
544
          pstr_output_config->down_sampling_ratio = 4;
3811
544
          break;
3812
2.29k
      }
3813
2.29k
    }
3814
3.16k
    pstr_output_config->samp_freq = pstr_api_struct->config[0].usac_config.native_sample_rate;
3815
3.16k
    pstr_output_config->header_samp_freq =
3816
3.16k
        pstr_api_struct->config[0].usac_config.native_sample_rate;
3817
3.16k
    pstr_output_config->audio_profile = AUDIO_PROFILE_USAC_L2;
3818
3.16k
    if (pstr_input_config->use_drc_element !=
3819
3.16k
        pstr_api_struct->config[0].usac_config.use_drc_element) {
3820
4
      error = IA_EXHEAACE_EXE_NONFATAL_USAC_INVALID_GAIN_POINTS;
3821
4
    }
3822
3.16k
    pstr_input_config->use_drc_element = pstr_api_struct->config[0].usac_config.use_drc_element;
3823
3.16k
  }
3824
3825
5.38k
  pstr_api_struct->pstr_state->ui_init_done = 1;
3826
5.38k
  pstr_output_config->i_out_bytes = pstr_api_struct->pstr_state->i_out_bytes;
3827
5.38k
  if (pstr_output_config->input_size) {
3828
5.38k
    pstr_output_config->expected_frame_count =
3829
5.38k
        (pstr_input_config->aac_config.length + (pstr_output_config->input_size - 1)) /
3830
5.38k
        pstr_output_config->input_size;
3831
5.38k
    if (pstr_api_struct->config[0].usac_config.use_delay_adjustment == 1) {
3832
3.02k
      pstr_output_config->expected_frame_count -=
3833
3.02k
          pstr_api_struct->config[0].usac_config.num_preroll_frames;
3834
3.02k
    }
3835
5.38k
  }
3836
3837
5.38k
  return error;
3838
6.46k
}
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.46k
    err_code = ixheaace_init(pstr_out_cfg->pv_ia_process_api_obj, pv_input, pv_output);
3846
6.46k
  }
3847
7.26k
  if (err_code & IA_FATAL_ERROR) {
3848
1.70k
    IXHEAACE_MEM_FREE(pv_output);
3849
1.70k
  }
3850
7.26k
  return err_code;
3851
7.26k
}
3852
3853
482k
IA_ERRORCODE ixheaace_process(pVOID pstr_obj_ixheaace, pVOID pv_input, pVOID pv_output) {
3854
482k
  IA_ERRORCODE error = IA_NO_ERROR;
3855
482k
  WORD32 ele_idx;
3856
482k
  (VOID) pv_input;
3857
482k
  ixheaace_api_struct *pstr_api_struct = (ixheaace_api_struct *)pstr_obj_ixheaace;
3858
482k
  ixheaace_output_config *pstr_output_config = (ixheaace_output_config *)pv_output;
3859
482k
  pstr_api_struct->pstr_state->is_quant_spec_zero = 0;
3860
482k
  pstr_api_struct->pstr_state->is_gain_limited = 0;
3861
482k
  if (!pstr_api_struct->usac_en) {
3862
435k
    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
33.7k
        return error;
3866
33.7k
      }
3867
307k
    }
3868
127k
    if ((error == IA_NO_ERROR) && (pstr_api_struct->pstr_state->is_quant_spec_zero)) {
3869
414
      error = IA_EXHEAACE_EXE_NONFATAL_QUANTIZATION_SPECTRUM_ZERO;
3870
414
    }
3871
127k
    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
321k
  } else {
3875
321k
    ia_usac_encoder_config_struct *usac_config = &pstr_api_struct->config[0].usac_config;
3876
321k
    if (usac_config->iframes_interval <= usac_config->num_preroll_frames) {
3877
22.8k
      pstr_api_struct->pstr_state->str_usac_enc_data.usac_independency_flag = 1;
3878
22.8k
      if (usac_config->iframes_interval == usac_config->num_preroll_frames &&
3879
4.24k
          usac_config->is_first_frame == 0) {
3880
1.41k
        usac_config->is_ipf = 1;
3881
1.41k
      }
3882
298k
    } else {
3883
298k
      pstr_api_struct->pstr_state->str_usac_enc_data.usac_independency_flag = 0;
3884
298k
    }
3885
321k
    if (pstr_api_struct->pstr_state->str_usac_enc_data.frame_count >
3886
321k
        usac_config->num_preroll_frames) {
3887
302k
      if (usac_config->iframes_interval <= usac_config->num_preroll_frames) {
3888
4.21k
        pstr_api_struct->pstr_state->str_usac_enc_data.usac_independency_flag = 1;
3889
298k
      } else {
3890
298k
        pstr_api_struct->pstr_state->str_usac_enc_data.usac_independency_flag = 0;
3891
298k
      }
3892
302k
    }
3893
3894
321k
    {
3895
321k
      error = iusace_process(pstr_api_struct);
3896
321k
      if (error & IA_FATAL_ERROR) {
3897
14.0k
        pstr_output_config->i_out_bytes = 0;
3898
14.0k
        return error;
3899
14.0k
      }
3900
307k
      if ((error == IA_NO_ERROR) && (pstr_api_struct->pstr_state->is_quant_spec_zero)) {
3901
321
        error = IA_EXHEAACE_EXE_NONFATAL_USAC_QUANTIZATION_SPECTRUM_ZERO;
3902
321
      }
3903
307k
      if ((error == IA_NO_ERROR) && (pstr_api_struct->pstr_state->is_gain_limited)) {
3904
13
        error = IA_EXHEAACE_EXE_NONFATAL_USAC_QUANTIZATION_INSUFFICIENT_BITRES;
3905
13
      }
3906
307k
    }
3907
3908
0
    usac_config->iframes_interval++;
3909
307k
    if (usac_config->iframes_interval ==
3910
307k
        (usac_config->random_access_interval - usac_config->num_preroll_frames)) {
3911
1.43k
      usac_config->iframes_interval = 0;
3912
1.43k
    }
3913
307k
  }
3914
434k
  pstr_output_config->i_out_bytes = pstr_api_struct->pstr_state->i_out_bytes;
3915
434k
  return error;
3916
482k
}
3917
3918
8.34k
IA_ERRORCODE ixheaace_delete(pVOID pv_output) {
3919
8.34k
  IXHEAACE_MEM_FREE(pv_output);
3920
8.34k
  return IA_NO_ERROR;
3921
8.34k
}