Coverage Report

Created: 2025-11-09 07:01

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