Coverage Report

Created: 2025-10-13 07:04

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