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