Coverage Report

Created: 2025-10-13 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/iusace_enc_main.c
Line
Count
Source
1
/******************************************************************************
2
 *                                                                            *
3
 * Copyright (C) 2023 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
 */
20
21
#include <string.h>
22
#include <stdio.h>
23
#include <math.h>
24
25
#include "ixheaac_error_standards.h"
26
#include "iusace_type_def.h"
27
#include "ixheaace_adjust_threshold_data.h"
28
#include "iusace_bitbuffer.h"
29
#include "ixheaace_bitbuffer.h"
30
#include "ixheaace_mps_common_define.h"
31
32
/* DRC */
33
#include "impd_drc_common_enc.h"
34
#include "impd_drc_uni_drc.h"
35
#include "impd_drc_tables.h"
36
#include "impd_drc_api.h"
37
#include "impd_drc_uni_drc_eq.h"
38
#include "impd_drc_uni_drc_filter_bank.h"
39
#include "impd_drc_gain_enc.h"
40
#include "impd_drc_struct_def.h"
41
42
#include "iusace_cnst.h"
43
#include "iusace_tns_usac.h"
44
#include "iusace_psy_mod.h"
45
#include "iusace_psy_utils.h"
46
#include "iusace_fd_qc_util.h"
47
#include "ixheaace_memory_standards.h"
48
#include "iusace_tns_usac.h"
49
#include "iusace_config.h"
50
#include "iusace_arith_enc.h"
51
#include "iusace_fd_quant.h"
52
#include "iusace_ms.h"
53
#include "iusace_block_switch_const.h"
54
#include "iusace_block_switch_struct_def.h"
55
#include "iusace_signal_classifier.h"
56
#include "ixheaace_sbr_header.h"
57
#include "ixheaace_config.h"
58
#include "ixheaace_asc_write.h"
59
#include "iusace_main.h"
60
#include "iusace_write_bitstream.h"
61
#include "iusace_windowing.h"
62
#include "iusace_fd_enc.h"
63
#include "iusace_fd_qc_adjthr.h"
64
#include "iusace_config.h"
65
#include "iusace_tcx_mdct.h"
66
#include "iusace_func_prototypes.h"
67
#include "iusace_block_switch.h"
68
#include "iusace_rom.h"
69
#include "ixheaace_error_codes.h"
70
71
#include "ixheaace_sbr_header.h"
72
#include "ixheaace_sbr_def.h"
73
#include "ixheaace_resampler.h"
74
#include "ixheaace_common_rom.h"
75
76
#include "ixheaace_sbr_header.h"
77
#include "ixheaace_sbr_def.h"
78
#include "ixheaace_resampler.h"
79
#include "ixheaace_sbr_rom.h"
80
#include "ixheaace_common_rom.h"
81
#include "ixheaace_sbr_hbe.h"
82
#include "ixheaace_sbr_qmf_enc.h"
83
#include "ixheaace_sbr_tran_det.h"
84
#include "ixheaace_sbr_frame_info_gen.h"
85
#include "ixheaace_sbr_env_est.h"
86
#include "ixheaace_sbr_code_envelope.h"
87
#include "ixheaace_sbr_rom.h"
88
#include "ixheaace_sbr_main.h"
89
#include "ixheaace_common_rom.h"
90
#include "ixheaace_sbr_missing_harmonics_det.h"
91
#include "ixheaace_sbr_inv_filtering_estimation.h"
92
#include "ixheaace_sbr_noise_floor_est.h"
93
#include "ixheaace_sbr_ton_corr.h"
94
#include "iusace_esbr_pvc.h"
95
#include "iusace_esbr_inter_tes.h"
96
#include "ixheaace_sbr.h"
97
#include "ixheaace_sbr_cmondata.h"
98
#include "ixheaace_sbr_crc.h"
99
#include "ixheaace_sbr_enc_struct.h"
100
101
#include "iusace_esbr_pvc.h"
102
#include "iusace_esbr_inter_tes.h"
103
104
3.77k
static WORD32 iusace_get_num_elements(WORD32 num_channels) {
105
3.77k
  WORD32 num_of_elements = 0;
106
107
3.77k
  switch (num_channels) {
108
1.07k
    case 1:
109
3.77k
    case 2:
110
3.77k
      num_of_elements = 1;
111
3.77k
      break;
112
0
    default:
113
0
      num_of_elements = num_channels;
114
0
      break;
115
3.77k
  }
116
117
3.77k
  return num_of_elements;
118
3.77k
}
119
120
3.77k
static UWORD32 iusace_get_element_type(WORD32 elem_idx, WORD32 num_channels) {
121
3.77k
  UWORD32 elem_type = (UWORD32)USAC_ELEMENT_TYPE_INVALID;
122
3.77k
  (VOID) elem_idx;
123
124
3.77k
  switch (num_channels) {
125
1.07k
    case 1:
126
1.07k
      elem_type = USAC_ELEMENT_TYPE_SCE;
127
1.07k
      break;
128
2.69k
    case 2:
129
2.69k
      elem_type = USAC_ELEMENT_TYPE_CPE;
130
2.69k
      break;
131
0
    default:
132
0
      elem_type = USAC_ELEMENT_TYPE_SCE;
133
0
      break;
134
3.77k
  }
135
136
3.77k
  return elem_type;
137
3.77k
}
138
139
static VOID iusace_bw_init(ia_usac_encoder_config_struct *ptr_usac_config,
140
3.77k
                           ixheaace_audio_specific_config_struct *pstr_asc, WORD32 ele_idx) {
141
3.77k
  ptr_usac_config->bw_limit[ele_idx] = 20000;
142
3.77k
  (VOID) pstr_asc;
143
3.77k
  ptr_usac_config->bw_limit[ele_idx] =
144
3.77k
      MIN(ptr_usac_config->bw_limit[ele_idx], ptr_usac_config->core_sample_rate / 2);
145
146
3.77k
  return;
147
3.77k
}
148
149
4.64k
VOID iusace_scratch_mem_init(ia_usac_data_struct *usac_data, WORD32 total_ch, WORD32 sr) {
150
4.64k
  iusace_scratch_mem *pstr_scratch = &usac_data->str_scratch;
151
4.64k
  UWORD8 *temp_ptr = pstr_scratch->ptr_scratch_buf;
152
153
4.64k
  pstr_scratch->ptr_stack_mem = (FLOAT32 *)(temp_ptr);
154
4.64k
  temp_ptr += USACE_SCR_STACK;
155
156
4.64k
  pstr_scratch->p_fd_mdct_windowed_long_buf = (FLOAT64 *)(temp_ptr);
157
4.64k
  temp_ptr += (2 * FRAME_LEN_LONG) * sizeof(FLOAT64);
158
159
4.64k
  pstr_scratch->p_fd_mdct_windowed_short_buf = (FLOAT64 *)(temp_ptr);
160
  // Size needed for above pointer is (2 * FRAME_LEN_LONG) * sizeof(FLOAT64)
161
162
4.64k
  temp_ptr = (UWORD8 *)pstr_scratch->p_fd_mdct_windowed_short_buf;
163
164
4.64k
  pstr_scratch->p_tns_filter = (FLOAT64 *)(temp_ptr);
165
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT64);
166
167
4.64k
  pstr_scratch->ptr_tns_scratch = (FLOAT64 *)(temp_ptr);
168
4.64k
  temp_ptr +=
169
4.64k
      (MAX_SHIFT_LEN_LONG + (TNS_MAX_ORDER + 1) * 2) * sizeof(pstr_scratch->ptr_tns_scratch[0]);
170
171
4.64k
  pstr_scratch->p_left_fac_time_data = (FLOAT64 *)(temp_ptr);
172
4.64k
  temp_ptr += (2 * FAC_LENGTH + ORDER) * sizeof(FLOAT64);
173
174
4.64k
  pstr_scratch->p_fac_win = (FLOAT64 *)(temp_ptr);
175
  // Size needed for above pointer is (2 * FAC_LENGTH) * sizeof(FLOAT64)
176
177
4.64k
  temp_ptr = (UWORD8 *)pstr_scratch->p_left_fac_time_data;
178
179
4.64k
  pstr_scratch->p_sort_grouping_scratch = (FLOAT64 *)(temp_ptr);
180
  // Size needed for above pointer is (LN2) * sizeof(FLOAT64)
181
182
4.64k
  temp_ptr = (UWORD8 *)pstr_scratch->p_sort_grouping_scratch;
183
184
4.64k
  pstr_scratch->p_noise_filling_highest_tone = (FLOAT64 *)(temp_ptr);
185
4.64k
  temp_ptr += (LN2) * sizeof(FLOAT64);
186
187
4.64k
  pstr_scratch->p_quant_spectrum_spec_scratch = (FLOAT64 *)(temp_ptr);
188
4.64k
  temp_ptr += (2 * FRAME_LEN_LONG) * sizeof(FLOAT64);
189
190
4.64k
  pstr_scratch->p_cmpx_mdct_temp_buf = (FLOAT64 *)(temp_ptr);
191
  // Size needed for above pointer is (LN2) * sizeof(FLOAT64)
192
193
4.64k
  temp_ptr = (UWORD8 *)pstr_scratch->p_noise_filling_highest_tone;
194
195
12.4k
  for (WORD32 i = 0; i < total_ch; i++) {
196
7.85k
    pstr_scratch->p_reconstructed_time_signal[i] = (FLOAT64 *)(temp_ptr);
197
7.85k
    temp_ptr += (4 * FRAME_LEN_LONG) * sizeof(FLOAT64);
198
7.85k
  }
199
4.64k
  pstr_scratch->ptr_next_win_scratch = (WORD32 *)(temp_ptr);
200
4.64k
  temp_ptr += (2 * MAX_TIME_CHANNELS) * sizeof(pstr_scratch->ptr_next_win_scratch[0]);
201
202
4.64k
  pstr_scratch->p_fft_p2_y = (FLOAT32 *)(temp_ptr);
203
4.64k
  temp_ptr += (2 * FRAME_LEN_LONG) * sizeof(FLOAT32);
204
205
4.64k
  pstr_scratch->p_fft_p3_data_3 = (FLOAT32 *)(temp_ptr);
206
4.64k
  temp_ptr += (800) * sizeof(FLOAT32);
207
208
4.64k
  pstr_scratch->p_fft_p3_y = (FLOAT32 *)(temp_ptr);
209
4.64k
  temp_ptr += (2 * FRAME_LEN_LONG) * sizeof(FLOAT32);
210
211
4.64k
  pstr_scratch->p_time_signal = (FLOAT32 *)(temp_ptr);
212
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
213
214
4.64k
  pstr_scratch->p_complex_fft = (FLOAT32 *)(temp_ptr);
215
4.64k
  temp_ptr += (2 * FRAME_LEN_LONG) * sizeof(FLOAT32);
216
217
4.64k
  pstr_scratch->p_tonal_flag = (WORD32 *)(temp_ptr);
218
4.64k
  temp_ptr += (FRAME_LEN_LONG / 2) * sizeof(WORD32);
219
220
4.64k
  pstr_scratch->p_pow_spec = (FLOAT32 *)(temp_ptr);
221
  // Size needed for above pointer is (FRAME_LEN_LONG / 2) * sizeof(FLOAT32)
222
4.64k
  temp_ptr = (UWORD8 *)pstr_scratch->p_time_signal;
223
224
4.64k
  pstr_scratch->p_temp_mdct = (FLOAT32 *)(temp_ptr);
225
4.64k
  temp_ptr += (1024) * sizeof(FLOAT32);
226
227
4.64k
  pstr_scratch->p_buf_synthesis_tool = (FLOAT32 *)(temp_ptr);
228
4.64k
  temp_ptr += (LEN_FRAME_16K + ORDER_LP_FILT_16K) * sizeof(FLOAT32);
229
230
4.64k
  pstr_scratch->p_mdct_spec_float = (FLOAT32 *)(temp_ptr);
231
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
232
233
4.64k
  pstr_scratch->p_sq_gain_en = (FLOAT32 *)(temp_ptr);
234
4.64k
  temp_ptr += (FRAME_LEN_LONG / 4) * sizeof(FLOAT32);
235
236
4.64k
  pstr_scratch->p_fft_mdct_buf = (FLOAT32 *)(temp_ptr);
237
4.64k
  temp_ptr += (4 * FRAME_LEN_LONG) * sizeof(FLOAT32);
238
239
4.64k
  pstr_scratch->p_arith_map_prev_scratch = (WORD32 *)(temp_ptr);
240
4.64k
  temp_ptr += (516) * sizeof(WORD32);
241
242
4.64k
  pstr_scratch->p_arith_map_pres_scratch = (WORD32 *)(temp_ptr);
243
4.64k
  temp_ptr += (516) * sizeof(WORD32);
244
245
4.64k
  pstr_scratch->p_ol_pitch_buf_tmp = (FLOAT32 *)(temp_ptr);
246
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
247
248
4.64k
  pstr_scratch->p_ol_pitch_speech_buf = (FLOAT32 *)(temp_ptr);
249
4.64k
  temp_ptr += (FRAME_LEN_LONG + LAG_MAX) * sizeof(FLOAT32);
250
251
4.64k
  pstr_scratch->p_ol_pitch_w_table = (FLOAT32 *)(temp_ptr);
252
4.64k
  temp_ptr += (LEN_CORR_R) * sizeof(FLOAT32);
253
254
4.64k
  pstr_scratch->p_ol_pitch_R = (FLOAT32 *)(temp_ptr);
255
4.64k
  temp_ptr += (LEN_CORR_R) * sizeof(FLOAT32);
256
257
4.64k
  WORD32 R0_size = (54 + 6 * ((WORD32)(34.f * ((FLOAT32)sr / 2.f) / 12800.f + 0.5f) * 2)) / 2;
258
4.64k
  pstr_scratch->p_ol_pitch_R0 = (FLOAT32 *)(temp_ptr);
259
4.64k
  temp_ptr += (R0_size) * sizeof(FLOAT32);
260
261
4.64k
  pstr_scratch->p_lpd_frm_enc_scratch = (FLOAT32 *)(temp_ptr);
262
4.64k
  temp_ptr += (LEN_FRAME + 1) * sizeof(FLOAT32);
263
264
4.64k
  pstr_scratch->p_wsig_buf = (FLOAT32 *)(temp_ptr + 128 * sizeof(FLOAT32));
265
4.64k
  temp_ptr += (128 + FRAME_LEN_LONG) * sizeof(FLOAT32);
266
267
4.64k
  pstr_scratch->p_wsyn_tcx_buf = (FLOAT32 *)(temp_ptr + 128 * sizeof(FLOAT32));
268
4.64k
  temp_ptr += (128 + FRAME_LEN_LONG) * sizeof(FLOAT32);
269
270
4.64k
  pstr_scratch->p_synth_tcx_buf = (FLOAT32 *)(temp_ptr + 128 * sizeof(FLOAT32));
271
4.64k
  temp_ptr += (128 + FRAME_LEN_LONG) * sizeof(FLOAT32);
272
273
4.64k
  pstr_scratch->p_wsyn_buf = (FLOAT32 *)(temp_ptr + 128 * sizeof(FLOAT32));
274
4.64k
  temp_ptr += (128 + FRAME_LEN_LONG) * sizeof(FLOAT32);
275
276
4.64k
  pstr_scratch->p_synth_buf = (FLOAT32 *)(temp_ptr + 128 * sizeof(FLOAT32));
277
4.64k
  temp_ptr += (128 + FRAME_LEN_LONG) * sizeof(FLOAT32);
278
279
4.64k
  pstr_scratch->p_temp_wsyn_buf = (FLOAT32 *)temp_ptr;
280
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
281
282
4.64k
  pstr_scratch->p_lp_filter_coeff = (FLOAT32 *)(temp_ptr);
283
4.64k
  temp_ptr += ((NUM_SUBFR_SUPERFRAME + 1) * (ORDER + 1)) * sizeof(FLOAT32);
284
285
4.64k
  pstr_scratch->p_lp_filter_coeff_q = (FLOAT32 *)(temp_ptr);
286
4.64k
  temp_ptr += ((NUM_SUBFR_SUPERFRAME + 1) * (ORDER + 1)) * sizeof(FLOAT32);
287
288
4.64k
  pstr_scratch->p_wsp_prev_buf = (FLOAT32 *)(temp_ptr);
289
4.64k
  temp_ptr += ((MAX_PITCH1 / OPL_DECIM) + LEN_FRAME) * sizeof(FLOAT32);
290
291
4.64k
  pstr_scratch->ptr_lpd_scratch = (UWORD8 *)temp_ptr;
292
4.64k
  temp_ptr += ((2 * (NUM_SUBFR_SUPERFRAME + 1) * (ORDER + 1)) + (4 * (NUM_FRAMES + 1) * ORDER) +
293
4.64k
               (((NUM_FRAMES >> 1) + 1) * ORDER) * 4) *
294
4.64k
                  sizeof(FLOAT32) +
295
4.64k
              100 * sizeof(WORD32) + 6 * sizeof(ia_usac_lpd_scratch);
296
297
4.64k
  pstr_scratch->p_prm_tcx = (WORD32 *)(temp_ptr);
298
4.64k
  temp_ptr += (NUM_TCX80_PRM) * sizeof(WORD32);
299
300
4.64k
  pstr_scratch->p_buf_speech = (FLOAT32 *)(temp_ptr);
301
4.64k
  temp_ptr += (2 * LEN_FRAME + ORDER) * sizeof(FLOAT32);
302
303
4.64k
  pstr_scratch->p_buf_res = (FLOAT32 *)(temp_ptr);
304
4.64k
  temp_ptr += (2 * LEN_FRAME) * sizeof(FLOAT32);
305
306
4.64k
  pstr_scratch->p_buf_signal = (FLOAT32 *)(temp_ptr);
307
4.64k
  temp_ptr += (ORDER + LEN_FRAME) * sizeof(FLOAT32);
308
309
4.64k
  pstr_scratch->p_xn1_tcx = (FLOAT32 *)(temp_ptr);
310
4.64k
  temp_ptr += (2 * FAC_LENGTH) * sizeof(FLOAT32);
311
312
4.64k
  pstr_scratch->p_xn_buf_tcx = (FLOAT32 *)(temp_ptr);
313
4.64k
  temp_ptr += (128 + FRAME_LEN_LONG + 128) * sizeof(FLOAT32);
314
315
4.64k
  pstr_scratch->p_x_tcx = (FLOAT32 *)(temp_ptr);
316
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
317
318
4.64k
  pstr_scratch->p_x_tmp_tcx = (FLOAT32 *)(temp_ptr);
319
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
320
321
4.64k
  pstr_scratch->p_en_tcx = (FLOAT32 *)(temp_ptr);
322
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
323
324
4.64k
  pstr_scratch->p_alfd_gains_tcx = (FLOAT32 *)(temp_ptr);
325
4.64k
  temp_ptr += (FRAME_LEN_LONG / (4 * 8)) * sizeof(FLOAT32);
326
327
4.64k
  pstr_scratch->p_sq_enc_tcx = (FLOAT32 *)(temp_ptr);
328
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
329
330
4.64k
  pstr_scratch->p_sq_quant_tcx = (WORD32 *)(temp_ptr);
331
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(WORD32);
332
333
4.64k
  pstr_scratch->p_gain1_tcx = (FLOAT32 *)(temp_ptr);
334
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
335
336
4.64k
  pstr_scratch->p_gain2_tcx = (FLOAT32 *)(temp_ptr);
337
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
338
339
4.64k
  pstr_scratch->p_facelp_tcx = (FLOAT32 *)(temp_ptr);
340
4.64k
  temp_ptr += (FAC_LENGTH) * sizeof(FLOAT32);
341
342
4.64k
  pstr_scratch->p_xn2_tcx = (FLOAT32 *)(temp_ptr);
343
4.64k
  temp_ptr += (2 * FAC_LENGTH) * sizeof(FLOAT32);
344
345
4.64k
  pstr_scratch->p_fac_window_tcx = (FLOAT32 *)(temp_ptr);
346
4.64k
  temp_ptr += (2 * FAC_LENGTH) * sizeof(FLOAT32);
347
348
4.64k
  pstr_scratch->p_x1_tcx = (FLOAT32 *)(temp_ptr);
349
4.64k
  temp_ptr += (FAC_LENGTH) * sizeof(FLOAT32);
350
351
4.64k
  pstr_scratch->p_x2_tcx = (FLOAT32 *)(temp_ptr);
352
4.64k
  temp_ptr += (FAC_LENGTH) * sizeof(FLOAT32);
353
354
4.64k
  pstr_scratch->p_y_tcx = (WORD32 *)(temp_ptr);
355
4.64k
  temp_ptr += (FAC_LENGTH) * sizeof(WORD32);
356
357
4.64k
  pstr_scratch->p_in_out_tcx = (FLOAT32 *)(temp_ptr);
358
4.64k
  temp_ptr += (FRAME_LEN_LONG * 2 * 2) * sizeof(FLOAT32);
359
360
4.64k
  pstr_scratch->p_tcx_input = (FLOAT32 *)(temp_ptr);
361
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
362
363
4.64k
  pstr_scratch->ptr_tcx_scratch = (FLOAT32 *)(temp_ptr);
364
4.64k
  temp_ptr += 3 * (FRAME_LEN_LONG) * sizeof(pstr_scratch->ptr_tcx_scratch[0]);
365
366
4.64k
  pstr_scratch->p_tcx_output = (FLOAT32 *)(temp_ptr);
367
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
368
369
4.64k
  pstr_scratch->p_buf_aut_corr = (FLOAT32 *)(temp_ptr);
370
  // Size needed for above pointer is (LEN_WIN_PLUS) * sizeof(FLOAT32)
371
4.64k
  temp_ptr = (UWORD8 *)pstr_scratch->p_buf_aut_corr;
372
373
4.64k
  pstr_scratch->p_xn2 = (FLOAT32 *)(temp_ptr);
374
4.64k
  temp_ptr += (FAC_LENGTH + ORDER) * sizeof(FLOAT32);
375
376
4.64k
  pstr_scratch->p_fac_dec = (FLOAT32 *)(temp_ptr);
377
4.64k
  temp_ptr += (2 * FAC_LENGTH) * sizeof(FLOAT32);
378
379
4.64k
  pstr_scratch->p_right_fac_spec = (FLOAT32 *)(temp_ptr);
380
4.64k
  temp_ptr += (FAC_LENGTH) * sizeof(FLOAT32);
381
382
4.64k
  pstr_scratch->p_x2 = (FLOAT32 *)(temp_ptr);
383
4.64k
  temp_ptr += (FAC_LENGTH) * sizeof(FLOAT32);
384
385
4.64k
  pstr_scratch->p_param = (WORD32 *)(temp_ptr);
386
4.64k
  temp_ptr += (FAC_LENGTH + 1) * sizeof(WORD32);
387
388
4.64k
  pstr_scratch->p_x = (FLOAT32 *)(temp_ptr);
389
4.64k
  temp_ptr += (FAC_LENGTH) * sizeof(FLOAT32);
390
391
4.64k
  pstr_scratch->p_xn_2 = (FLOAT32 *)(temp_ptr);
392
4.64k
  temp_ptr += (2 * FAC_LENGTH + ORDER) * sizeof(FLOAT32);
393
394
4.64k
  pstr_scratch->p_fac_window = (FLOAT32 *)(temp_ptr);
395
4.64k
  temp_ptr += (2 * FAC_LENGTH) * sizeof(FLOAT32);
396
397
4.64k
  pstr_scratch->p_fir_sig_buf = (FLOAT32 *)(temp_ptr);
398
  // Size needed for above pointer is (3 + LEN_FRAME) * sizeof(FLOAT32)
399
4.64k
  temp_ptr = (UWORD8 *)pstr_scratch->p_fir_sig_buf;
400
401
4.64k
  pstr_scratch->p_acelp_ir_buf = (FLOAT32 *)(temp_ptr);
402
403
4.64k
  temp_ptr += (4 * LEN_SUBFR) * sizeof(FLOAT32);
404
405
4.64k
  pstr_scratch->ptr_acelp_scratch = (FLOAT32 *)(temp_ptr);
406
4.64k
  temp_ptr += ((11 * LEN_SUBFR) + (ORDER + LEN_SUBFR + 8) + 1024) *
407
4.64k
              sizeof(pstr_scratch->ptr_acelp_scratch[0]);
408
409
4.64k
  pstr_scratch->p_acelp_exc_buf = (FLOAT32 *)(temp_ptr);
410
  // Size needed for above pointer is ((3 * LEN_FRAME) + 1 + 41) * sizeof(FLOAT32)
411
412
4.64k
  temp_ptr = (UWORD8 *)pstr_scratch->p_lpd_frm_enc_scratch;
413
414
4.64k
  pstr_scratch->p_fac_bits_word = (WORD16 *)(temp_ptr);
415
4.64k
  temp_ptr += (5000) * sizeof(WORD16);
416
417
4.64k
  pstr_scratch->p_left_fac_timedata_flt = (FLOAT32 *)(temp_ptr);
418
4.64k
  temp_ptr += (2 * FAC_LENGTH + ORDER) * sizeof(FLOAT32);
419
420
4.64k
  pstr_scratch->p_left_fac_spec = (FLOAT32 *)(temp_ptr);
421
4.64k
  temp_ptr += (FAC_LENGTH) * sizeof(FLOAT32);
422
423
4.64k
  pstr_scratch->p_fac_prm = (WORD32 *)(temp_ptr);
424
4.64k
  temp_ptr += (FAC_LENGTH + 1) * sizeof(WORD32);
425
426
4.64k
  pstr_scratch->p_acelp_folded_scratch = (FLOAT32 *)(temp_ptr);
427
  // Size needed for above pointer is (FAC_LENGTH) * sizeof(FLOAT32)
428
429
4.64k
  temp_ptr = (UWORD8 *)pstr_scratch->p_fac_bits_word;
430
431
4.64k
  pstr_scratch->p_exp_spec = (FLOAT32 *)(temp_ptr);
432
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
433
434
4.64k
  pstr_scratch->p_adjthr_ptr_exp_spec = (FLOAT32 *)(temp_ptr);
435
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
436
437
4.64k
  pstr_scratch->p_adjthr_mdct_spec_float = (FLOAT32 *)(temp_ptr);
438
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(FLOAT32);
439
440
4.64k
  pstr_scratch->p_adjthr_quant_spec_temp = (WORD16 *)(temp_ptr);
441
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(WORD16);
442
443
4.64k
  pstr_scratch->p_degroup_scratch = (WORD32 *)(temp_ptr);
444
4.64k
  temp_ptr += (FRAME_LEN_LONG) * sizeof(WORD32);
445
446
  /*Newly added*/
447
4.64k
  pstr_scratch->ptr_drc_scratch_buf = (UWORD8 *)(temp_ptr);
448
449
4.64k
  pstr_scratch->ptr_num_fac_bits = (WORD32 *)temp_ptr;
450
4.64k
  temp_ptr += MAX_TIME_CHANNELS * sizeof(pstr_scratch->ptr_num_fac_bits[0]);
451
4.64k
  pstr_scratch->ptr_tns_data_present = (WORD32 *)temp_ptr;
452
4.64k
  temp_ptr += MAX_TIME_CHANNELS * sizeof(pstr_scratch->ptr_tns_data_present[0]);
453
454
4.64k
  pstr_scratch->ptr_tmp_lp_res = (FLOAT32 *)temp_ptr;
455
4.64k
  temp_ptr += FAC_LENGTH * sizeof(pstr_scratch->ptr_tmp_lp_res[0]);
456
457
12.4k
  for (WORD32 i = 0; i < total_ch; i++) {
458
7.85k
    pstr_scratch->ptr_sfb_form_fac[i] = (FLOAT32 *)temp_ptr;
459
7.85k
    temp_ptr += (MAX_NUM_GROUPED_SFB) * sizeof(FLOAT32);
460
7.85k
  }
461
12.4k
  for (WORD32 i = 0; i < total_ch; i++) {
462
7.85k
    pstr_scratch->ptr_sfb_num_relevant_lines[i] = (FLOAT32 *)temp_ptr;
463
7.85k
    temp_ptr += (MAX_NUM_GROUPED_SFB) * sizeof(FLOAT32);
464
7.85k
  }
465
12.4k
  for (WORD32 i = 0; i < total_ch; i++) {
466
7.85k
    pstr_scratch->ptr_sfb_ld_energy[i] = (FLOAT32 *)temp_ptr;
467
7.85k
    temp_ptr += (MAX_NUM_GROUPED_SFB) * sizeof(FLOAT32);
468
7.85k
  }
469
4.64k
  pstr_scratch->ptr_num_scfs = (WORD32 *)temp_ptr;
470
4.64k
  temp_ptr += (MAX_TIME_CHANNELS) * sizeof(pstr_scratch->ptr_num_scfs[0]);
471
472
4.64k
  pstr_scratch->ptr_max_ch_dyn_bits = (WORD32 *)temp_ptr;
473
4.64k
  temp_ptr += (MAX_TIME_CHANNELS) * sizeof(pstr_scratch->ptr_max_ch_dyn_bits[0]);
474
4.64k
  pstr_scratch->ptr_ch_bit_dist = (FLOAT32 *)temp_ptr;
475
4.64k
  temp_ptr += (MAX_TIME_CHANNELS) * sizeof(pstr_scratch->ptr_ch_bit_dist[0]);
476
4.64k
  pstr_scratch->ptr_fd_scratch = (UWORD8 *)temp_ptr;
477
  // Size needed for above pointer is (IXHEAACE_MAX_CH_IN_BS_ELE * MAX_NUM_GROUPED_SFB * 3) *
478
  // sizeof(WORD32)
479
480
4.64k
  return;
481
4.64k
}
482
483
WORD32 iusace_limitbitrate(WORD32 core_sample_rate, WORD32 frame_len, WORD32 num_ch,
484
0
                           WORD32 bit_rate) {
485
0
  WORD32 transport_bits, prev_bit_rate, shift = 0, iter = 0;
486
487
0
  while ((frame_len & ~((1 << (shift + 1)) - 1)) == frame_len &&
488
0
         (core_sample_rate & ~((1 << (shift + 1)) - 1)) == core_sample_rate) {
489
0
    shift++;
490
0
  }
491
492
0
  do {
493
0
    prev_bit_rate = bit_rate;
494
    /* Assume some worst case */
495
0
    transport_bits = 208;
496
497
0
    bit_rate =
498
0
        MAX(bit_rate, ((((40 * num_ch) + transport_bits) * (core_sample_rate)) / frame_len));
499
0
    bit_rate =
500
0
        MIN(bit_rate, ((num_ch * 6144) * (core_sample_rate >> shift)) / (frame_len >> shift));
501
502
0
  } while (prev_bit_rate != bit_rate && iter++ < 3);
503
504
0
  return bit_rate;
505
0
}
506
507
WORD32 iusace_map_sample_rate(WORD32 sample_rate)
508
788k
{
509
788k
  WORD32 mapped_sample_rate = sample_rate;
510
511
788k
  if ((mapped_sample_rate >= 0) && (mapped_sample_rate < 9391))
512
37.5k
  {
513
37.5k
    mapped_sample_rate = 8000;
514
37.5k
  }
515
750k
  else if ((mapped_sample_rate >= 9391) && (mapped_sample_rate < 11502))
516
21.1k
  {
517
21.1k
    mapped_sample_rate = 11025;
518
21.1k
  }
519
729k
  else if ((mapped_sample_rate >= 11502) && (mapped_sample_rate < 13856))
520
86.8k
  {
521
86.8k
    mapped_sample_rate = 12000;
522
86.8k
  }
523
642k
  else if ((mapped_sample_rate >= 13856) && (mapped_sample_rate < 18783))
524
48.2k
  {
525
48.2k
    mapped_sample_rate = 16000;
526
48.2k
  }
527
594k
  else if ((mapped_sample_rate >= 18783) && (mapped_sample_rate < 23004))
528
50.2k
  {
529
50.2k
    mapped_sample_rate = 22050;
530
50.2k
  }
531
544k
  else if ((mapped_sample_rate >= 23004) && (mapped_sample_rate < 27713))
532
139k
  {
533
139k
    mapped_sample_rate = 24000;
534
139k
  }
535
404k
  else if ((mapped_sample_rate >= 27713) && (mapped_sample_rate < 37566))
536
109k
  {
537
109k
    mapped_sample_rate = 32000;
538
109k
  }
539
294k
  else if ((mapped_sample_rate >= 37566) && (mapped_sample_rate < 46009))
540
42.7k
  {
541
42.7k
    mapped_sample_rate = 44100;
542
42.7k
  }
543
252k
  else if ((mapped_sample_rate >= 46009) && (mapped_sample_rate < 55426))
544
10.7k
  {
545
10.7k
    mapped_sample_rate = 48000;
546
10.7k
  }
547
241k
  else if ((mapped_sample_rate >= 55426) && (mapped_sample_rate < 75132))
548
1.77k
  {
549
1.77k
    mapped_sample_rate = 64000;
550
1.77k
  }
551
239k
  else if ((mapped_sample_rate >= 75132) && (mapped_sample_rate < 92017))
552
1.00k
  {
553
1.00k
    mapped_sample_rate = 88200;
554
1.00k
  }
555
238k
  else if (mapped_sample_rate >= 92017)
556
238k
  {
557
238k
    mapped_sample_rate = 96000;
558
238k
  }
559
63
  else
560
63
  {
561
63
    mapped_sample_rate = 48000;
562
63
  }
563
788k
  return mapped_sample_rate;
564
788k
}
565
566
/**
567
 *  iusace_enc_init
568
 *
569
 *  \brief Enocder initialization
570
 *
571
 *  \param [in] ptr_usac_config  Pointer to encoder config structure
572
 *  \param [in] ptr_asc          Pointer to audio specific config strcuture
573
 *  \param [in] pstr_state       Poniter to encoder handle structure
574
 *
575
 *  \return IA_ERRORCODE         Error code
576
 */
577
IA_ERRORCODE iusace_enc_init(ia_usac_encoder_config_struct *ptr_usac_config,
578
                             ixheaace_audio_specific_config_struct *pstr_asc,
579
4.64k
                             ia_usac_data_struct *pstr_state) {
580
4.64k
  IA_ERRORCODE err_code = IA_NO_ERROR;
581
4.64k
  WORD32 i, j, k, idx, i_ch;
582
4.64k
  UWORD32 elem_idx = 0;
583
4.64k
  ia_usac_data_struct *usac_data = (pstr_state);
584
4.64k
  ixheaace_audio_specific_config_struct *p_audio_specific_config = pstr_asc;
585
4.64k
  ia_usac_config_struct *pstr_asc_usac_config = &(p_audio_specific_config->str_usac_config);
586
4.64k
  WORD32 nbuff = 2048;
587
4.64k
  usac_data->usac_independency_flag_count = 0;
588
4.64k
  usac_data->usac_independency_flag_interval = 25;
589
4.64k
  usac_data->use_acelp_only = ptr_usac_config->use_acelp_only;
590
13.9k
  for (j = 0; j < MAX_TIME_CHANNELS; j++) {
591
9.28k
    memset(usac_data->overlap_buf[j], 0, nbuff * sizeof(FLOAT64 *));
592
593
9.28k
    usac_data->str_ms_info[j].ms_mask = 0;
594
83.5k
    for (i = 0; i < MAX_SHORT_WINDOWS; i++) {
595
3.86M
      for (k = 0; k < MAX_SFB_LONG; k++) {
596
3.78M
        usac_data->str_ms_info[j].ms_used[i][k] = 0;
597
3.78M
      }
598
74.2k
    }
599
9.28k
  }
600
601
4.64k
  iusace_scratch_mem_init(usac_data, ptr_usac_config->channels,
602
4.64k
                          ptr_usac_config->core_sample_rate);
603
604
13.9k
  for (i = 0; i < MAX_TIME_CHANNELS; i++) {
605
9.28k
    if (ptr_usac_config->cmplx_pred_flag) {
606
6.13k
      usac_data->str_ms_info[i].ms_mask = 3;
607
6.13k
    }
608
9.28k
    usac_data->ptr_dmx_re_save[i] = &usac_data->arr_dmx_save_float[i][0];
609
9.28k
    usac_data->ptr_dmx_im[i] = &usac_data->arr_dmx_im[i][0];
610
9.28k
  }
611
612
4.64k
  pstr_asc_usac_config->num_elements = 0;
613
4.64k
  pstr_asc_usac_config->usac_cfg_ext_present = 0;
614
4.64k
  pstr_asc_usac_config->num_config_extensions = 0;
615
616
4.64k
  if (ptr_usac_config->channels > 0) {
617
4.64k
    if (ptr_usac_config->channels < 7) {
618
4.64k
      p_audio_specific_config->channel_configuration = ptr_usac_config->channels;
619
4.64k
    }
620
4.64k
  }
621
4.64k
  ia_usac_enc_element_config_struct *pstr_usac_elem_config =
622
4.64k
      &(pstr_asc_usac_config->str_usac_element_config[pstr_asc_usac_config->num_elements]);
623
4.64k
  pstr_asc_usac_config->usac_element_type[pstr_asc_usac_config->num_elements] = ID_USAC_EXT;
624
4.64k
  pstr_usac_elem_config->usac_ext_ele_type = ID_EXT_ELE_AUDIOPREROLL;
625
4.64k
  pstr_usac_elem_config->usac_ext_ele_dflt_len_present = 0;
626
4.64k
  pstr_usac_elem_config->usac_ext_ele_payload_present = 0;
627
4.64k
  pstr_usac_elem_config->usac_ext_ele_cfg_len = 0;
628
4.64k
  pstr_asc_usac_config->num_elements++;
629
  // DRC Config
630
4.64k
  if (ptr_usac_config->use_drc_element) {
631
1.45k
    ptr_usac_config->str_drc_cfg.str_uni_drc_config.str_channel_layout.base_ch_count =
632
1.45k
        ptr_usac_config->channels;
633
634
1.45k
    memset(&usac_data->str_drc_state, 0, sizeof(ia_drc_enc_state));
635
636
1.45k
    err_code = impd_drc_enc_init(&usac_data->str_drc_state, pstr_state->str_scratch.drc_scratch,
637
1.45k
                                 &ptr_usac_config->str_drc_cfg);
638
1.45k
    if (err_code & IA_FATAL_ERROR) {
639
869
      return err_code;
640
869
    }
641
581
    if (err_code) {
642
0
      ptr_usac_config->use_drc_element = 0;
643
0
      err_code = IA_NO_ERROR;
644
0
    }
645
646
581
    if (ptr_usac_config->use_drc_element) {
647
581
      ia_usac_enc_element_config_struct *pstr_usac_elem_config =
648
581
          &(pstr_asc_usac_config->str_usac_element_config[pstr_asc_usac_config->num_elements]);
649
581
      pstr_asc_usac_config->usac_element_type[pstr_asc_usac_config->num_elements] = ID_USAC_EXT;
650
581
      pstr_usac_elem_config->usac_ext_ele_type = ID_EXT_ELE_UNI_DRC;
651
581
      pstr_usac_elem_config->usac_ext_ele_dflt_len_present = 0;
652
581
      pstr_usac_elem_config->usac_ext_ele_payload_present = 0;
653
581
      pstr_usac_elem_config->drc_config_data = usac_data->str_drc_state.bit_buf_base_cfg;
654
581
      pstr_usac_elem_config->usac_ext_ele_cfg_len =
655
581
          (usac_data->str_drc_state.drc_config_data_size_bit + 7) >> 3;
656
581
      pstr_asc_usac_config->num_elements++;
657
581
    }
658
3.19k
  } else {
659
3.19k
    err_code =
660
3.19k
        impd_loudness_info_init(&usac_data->str_drc_state, &ptr_usac_config->str_drc_cfg);
661
3.19k
    if (err_code) {
662
0
      return err_code;
663
0
    }
664
3.19k
  }
665
666
3.77k
  pstr_asc_usac_config->usac_config_ext_type[pstr_asc_usac_config->num_config_extensions] =
667
3.77k
      ID_CONFIG_EXT_STREAM_ID;
668
3.77k
  pstr_asc_usac_config->usac_config_ext_len[pstr_asc_usac_config->num_config_extensions] =
669
3.77k
      CONFIG_EXT_LEN_STREAM_ID;
670
3.77k
  pstr_asc_usac_config->num_config_extensions++;
671
3.77k
  pstr_asc_usac_config->stream_identifier = ptr_usac_config->stream_id;
672
673
3.77k
  pstr_asc_usac_config->usac_config_ext_type[pstr_asc_usac_config->num_config_extensions] =
674
3.77k
      ID_CONFIG_EXT_LOUDNESS_INFO;
675
3.77k
  pstr_asc_usac_config->usac_config_ext_len[pstr_asc_usac_config->num_config_extensions] =
676
3.77k
      (usac_data->str_drc_state.drc_config_ext_data_size_bit + 7) >> 3;
677
3.77k
  pstr_asc_usac_config->usac_config_ext_buf[pstr_asc_usac_config->num_config_extensions] =
678
3.77k
      usac_data->str_drc_state.bit_buf_base_cfg_ext;
679
3.77k
  pstr_asc_usac_config->num_config_extensions++;
680
681
3.77k
  if (pstr_asc_usac_config->num_config_extensions) {
682
3.77k
    pstr_asc_usac_config->usac_cfg_ext_present = 1;
683
3.77k
  }
684
685
3.77k
  p_audio_specific_config->sampling_frequency = ptr_usac_config->native_sample_rate;
686
3.77k
  p_audio_specific_config->num_audio_channels = ptr_usac_config->channels;
687
3.77k
  elem_idx = pstr_asc_usac_config->num_elements;
688
3.77k
  ptr_usac_config->num_ext_elements = elem_idx;
689
3.77k
  pstr_asc_usac_config->num_ext_elements = elem_idx;
690
3.77k
  i = elem_idx;
691
692
3.77k
  if (ptr_usac_config->channels != 0) {
693
3.77k
    ptr_usac_config->num_elements = iusace_get_num_elements(ptr_usac_config->channels);
694
3.77k
    pstr_asc_usac_config->num_elements += ptr_usac_config->num_elements;
695
696
7.54k
    for (; i < (WORD32)pstr_asc_usac_config->num_elements; i++) {
697
3.77k
      pstr_asc_usac_config->usac_element_type[i] = iusace_get_element_type(
698
3.77k
          (i - ptr_usac_config->num_ext_elements), ptr_usac_config->channels);
699
3.77k
    }
700
3.77k
  }
701
702
3.77k
  WORD32 count = ptr_usac_config->num_elements;
703
3.77k
  ptr_usac_config->num_elements = pstr_asc_usac_config->num_elements;
704
3.77k
  iusace_qc_create(&usac_data->str_qc_main);
705
706
3.77k
  if (count > 2) {
707
0
    WORD32 num_mono = 0, num_stereo = 0, num_lfe = 0;
708
709
0
    for (WORD8 ch_idx = 0; ch_idx < count; ch_idx++) {
710
0
      switch (
711
0
          pstr_asc_usac_config->usac_element_type[ch_idx + ptr_usac_config->num_ext_elements]) {
712
0
        case ID_USAC_SCE:
713
0
          num_mono++;
714
0
          break;
715
0
        case ID_USAC_CPE:
716
0
          num_stereo++;
717
0
          break;
718
0
        case ID_USAC_EXT:
719
0
          break;
720
0
        default:
721
0
          return IA_EXHEAACE_INIT_FATAL_USAC_INVALID_ELEMENT_TYPE;
722
0
      }
723
0
    }
724
725
0
    WORD32 bitrate_per_stereo = (WORD32)((ptr_usac_config->basic_bitrate - (num_lfe)*8000) /
726
0
                                         (num_mono * 0.625 + num_stereo));
727
0
    WORD32 bitrate_per_mono = (WORD32)(0.625 * bitrate_per_stereo);
728
729
0
    for (WORD8 ch_idx = 0; ch_idx < count; ch_idx++) {
730
0
      switch (
731
0
          pstr_asc_usac_config->usac_element_type[ch_idx + ptr_usac_config->num_ext_elements]) {
732
0
        case ID_USAC_SCE:
733
0
          usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate = bitrate_per_mono;
734
0
          break;
735
0
        case ID_USAC_CPE:
736
0
          usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate = bitrate_per_stereo;
737
0
          break;
738
0
        case ID_USAC_EXT:
739
0
          break;
740
0
        default:
741
0
          return IA_EXHEAACE_INIT_FATAL_USAC_INVALID_ELEMENT_TYPE;
742
0
      }
743
744
0
      usac_data->str_qc_main.str_qc_data[ch_idx].num_ch = 1;
745
0
      if (ID_USAC_CPE ==
746
0
          pstr_asc_usac_config->usac_element_type[ch_idx + ptr_usac_config->num_ext_elements]) {
747
0
        usac_data->str_qc_main.str_qc_data[ch_idx].num_ch = 2;
748
0
      }
749
750
0
      usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate =
751
0
          MIN(360000 * usac_data->str_qc_main.str_qc_data[ch_idx].num_ch,
752
0
              usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate);
753
0
      usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate =
754
0
          MAX(8000 * usac_data->str_qc_main.str_qc_data[ch_idx].num_ch,
755
0
              usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate);
756
757
0
      usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate =
758
0
          iusace_limitbitrate(ptr_usac_config->core_sample_rate, 512,
759
0
                              usac_data->str_qc_main.str_qc_data[ch_idx].num_ch,
760
0
                              usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate);
761
762
0
      usac_data->str_qc_main.str_qc_data[ch_idx].avg_bits =
763
0
          (usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate * ptr_usac_config->ccfl) /
764
0
          ptr_usac_config->core_sample_rate;
765
0
    }
766
3.77k
  } else {
767
7.54k
    for (WORD8 ch_idx = 0; ch_idx < count; ch_idx++) {
768
3.77k
      usac_data->str_qc_main.str_qc_data[ch_idx].num_ch = (WORD8)ptr_usac_config->channels;
769
3.77k
      usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate = ptr_usac_config->basic_bitrate;
770
3.77k
      usac_data->str_qc_main.str_qc_data[ch_idx].avg_bits =
771
3.77k
          (usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate * ptr_usac_config->ccfl) /
772
3.77k
          ptr_usac_config->core_sample_rate;
773
3.77k
    }
774
3.77k
  }
775
776
3.77k
  for (i_ch = 0;
777
7.54k
       i_ch < (WORD32)(ptr_usac_config->num_elements - ptr_usac_config->num_ext_elements);
778
3.77k
       i_ch++) {
779
3.77k
    iusace_bw_init(ptr_usac_config, p_audio_specific_config, i_ch);
780
781
3.77k
    usac_data->noise_filling[i_ch] = ptr_usac_config->flag_noiseFilling;
782
3.77k
  }
783
784
3.77k
  memset(&usac_data->str_psy_mod.str_psy_out_data, 0,
785
3.77k
         sizeof(ia_psy_mod_out_data_struct) * MAX_TIME_CHANNELS);
786
787
3.77k
  i_ch = 0;
788
3.77k
  for (UWORD32 ch_idx = 0;
789
7.54k
       ch_idx < pstr_asc_usac_config->num_elements - ptr_usac_config->num_ext_elements;
790
3.77k
       ch_idx++) {
791
3.77k
    iusace_psy_mod_init(
792
3.77k
        &usac_data->str_psy_mod,
793
3.77k
        ((LEN_SUPERFRAME_768 == ptr_usac_config->ccfl) && (0 == ptr_usac_config->sbr_enable)) ?
794
619
        (iusace_map_sample_rate((ptr_usac_config->core_sample_rate * 4) / 3)) :
795
3.77k
        (ptr_usac_config->core_sample_rate),
796
3.77k
        usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate, ptr_usac_config->bw_limit[ch_idx],
797
3.77k
        usac_data->str_qc_main.str_qc_data[ch_idx].num_ch, i_ch, ch_idx, ptr_usac_config->ccfl);
798
3.77k
    i_ch += usac_data->str_qc_main.str_qc_data[ch_idx].num_ch;
799
3.77k
  }
800
801
7.54k
  for (; elem_idx < pstr_asc_usac_config->num_elements; elem_idx++) {
802
3.77k
    idx = elem_idx - pstr_asc_usac_config->num_ext_elements;
803
3.77k
    pstr_asc_usac_config->str_usac_element_config[elem_idx].noise_filling =
804
3.77k
        usac_data->noise_filling[idx];
805
3.77k
    usac_data->channel_elem_type[idx] = pstr_asc_usac_config->usac_element_type[elem_idx];
806
3.77k
  }
807
808
3.77k
  if (ptr_usac_config->use_fill_element) {
809
3.77k
    ia_usac_enc_element_config_struct *pstr_usac_elem_config =
810
3.77k
        &(pstr_asc_usac_config->str_usac_element_config[pstr_asc_usac_config->num_elements]);
811
3.77k
    pstr_asc_usac_config->usac_element_type[pstr_asc_usac_config->num_elements] = ID_USAC_EXT;
812
3.77k
    pstr_usac_elem_config->usac_ext_ele_type = ID_EXT_ELE_FILL;
813
3.77k
    pstr_usac_elem_config->usac_ext_ele_cfg_len = 0;
814
3.77k
    pstr_usac_elem_config->usac_ext_ele_dflt_len_present = 0;
815
3.77k
    pstr_usac_elem_config->usac_ext_ele_payload_present = 0;
816
3.77k
    pstr_asc_usac_config->num_elements++;
817
3.77k
    ptr_usac_config->num_ext_elements++;
818
3.77k
    ptr_usac_config->num_elements++;
819
3.77k
  }
820
821
3.77k
  if (ptr_usac_config->codec_mode == USAC_SWITCHED) {
822
1.58k
    iusace_init_classification(&usac_data->str_sig_class_data);
823
1.58k
  }
824
825
3.77k
  i_ch = 0;
826
3.77k
  for (UWORD32 ch_idx = 0;
827
7.54k
       ch_idx < pstr_asc_usac_config->num_elements - ptr_usac_config->num_ext_elements;
828
3.77k
       ch_idx++) {
829
10.2k
    for (idx = 0; idx < usac_data->str_qc_main.str_qc_data[ch_idx].num_ch; idx++, i_ch++) {
830
6.46k
      iusace_init_block_switching(&usac_data->block_switch_ctrl[i_ch],
831
6.46k
                                  usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate,
832
6.46k
                                  usac_data->str_qc_main.str_qc_data[ch_idx].num_ch);
833
6.46k
    }
834
3.77k
  }
835
836
3.77k
  pstr_asc_usac_config->str_usac_element_config[elem_idx].stereo_config_index = 0;
837
838
10.2k
  for (i_ch = 0; i_ch < ptr_usac_config->channels; i_ch++) {
839
6.46k
    ptr_usac_config->window_sequence[i_ch] = ONLY_LONG_SEQUENCE;
840
6.46k
    ptr_usac_config->window_shape_prev[i_ch] = WIN_SEL_0;
841
6.46k
  }
842
843
10.2k
  for (i_ch = 0; i_ch < ptr_usac_config->channels; i_ch++) {
844
6.46k
    memset(usac_data->td_in_buf[i_ch], 0,
845
6.46k
           (FRAME_LEN_LONG + LEN_NEXT_HIGH_RATE) * sizeof(usac_data->td_in_buf[i_ch][0]));
846
6.46k
  }
847
848
3.77k
  usac_data->max_bitreservoir_bits = MAX_CHANNEL_BITS * ptr_usac_config->channels;
849
3.77k
  usac_data->available_bitreservoir_bits = usac_data->max_bitreservoir_bits;
850
3.77k
  usac_data->available_bitreservoir_bits -=
851
3.77k
      (ptr_usac_config->bit_rate * ptr_usac_config->ccfl) / ptr_usac_config->core_sample_rate;
852
853
3.77k
  if (usac_data->available_bitreservoir_bits < 0) {
854
0
    return IA_EXHEAACE_INIT_FATAL_USAC_BITRES_SIZE_TOO_SMALL;
855
0
  }
856
3.77k
  i_ch = 0;
857
3.77k
  for (UWORD32 ch_idx = 0;
858
7.52k
       ch_idx < pstr_asc_usac_config->num_elements - ptr_usac_config->num_ext_elements;
859
3.77k
       ch_idx++) {
860
10.2k
    for (idx = 0; idx < usac_data->str_qc_main.str_qc_data[ch_idx].num_ch; idx++, i_ch++) {
861
6.46k
      usac_data->td_encoder[i_ch]->max_sfb_short =
862
6.46k
          usac_data->str_psy_mod.str_psy_short_config[ch_idx].sfb_count;
863
6.46k
      if (ptr_usac_config->tns_select == 0) {
864
3.38k
        usac_data->pstr_tns_info[i_ch] = NULL;
865
3.38k
      } else {
866
3.08k
        usac_data->pstr_tns_info[i_ch]->sfb_offset_table_short =
867
3.08k
            usac_data->str_psy_mod.str_psy_short_config[ch_idx].sfb_offset;
868
3.08k
        usac_data->pstr_tns_info[i_ch]->sfb_offset_table_long =
869
3.08k
            usac_data->str_psy_mod.str_psy_long_config[ch_idx].sfb_offset;
870
3.08k
        usac_data->pstr_tns_info[i_ch]->max_sfb_short =
871
3.08k
            usac_data->str_psy_mod.str_psy_short_config[ch_idx].sfb_count;
872
3.08k
        usac_data->pstr_tns_info[i_ch]->max_sfb_long =
873
3.08k
            usac_data->str_psy_mod.str_psy_long_config[ch_idx].sfb_count;
874
875
3.08k
        err_code = iusace_tns_init(ptr_usac_config->core_sample_rate,
876
3.08k
                            usac_data->str_qc_main.str_qc_data[ch_idx].ch_bitrate /
877
3.08k
                                usac_data->str_qc_main.str_qc_data[ch_idx].num_ch,
878
3.08k
                            usac_data->pstr_tns_info[i_ch],
879
3.08k
                            usac_data->str_qc_main.str_qc_data[ch_idx].num_ch);
880
3.08k
        if (err_code) {
881
20
          return err_code;
882
20
        }
883
3.08k
      }
884
6.46k
    }
885
3.77k
  }
886
887
11.2k
  for (i = 0; i < MAX_TIME_CHANNELS; i++) usac_data->str_quant_info[i].reset = 1;
888
889
3.75k
  if (ptr_usac_config->codec_mode == USAC_SWITCHED ||
890
2.17k
      ptr_usac_config->codec_mode == USAC_ONLY_TD) {
891
4.38k
    for (i_ch = 0; i_ch < ptr_usac_config->channels; i_ch++) {
892
2.69k
      if ((ptr_usac_config->core_sample_rate) < SR_MIN ||
893
2.69k
          (ptr_usac_config->core_sample_rate) > SR_MAX) {
894
2
        return IA_EXHEAACE_CONFIG_FATAL_USAC_SAMP_FREQ;
895
2.69k
      } else {
896
2.69k
        usac_data->td_encoder[i_ch]->fscale = ptr_usac_config->core_sample_rate;
897
898
2.69k
        iusace_init_td_data(usac_data->td_encoder[i_ch], ptr_usac_config->ccfl);
899
2.69k
      }
900
901
2.69k
      usac_data->td_bitrate[i_ch] = ptr_usac_config->bit_rate;
902
2.69k
      usac_data->td_bitrate[i_ch] /= ptr_usac_config->channels;
903
2.69k
      iusace_config_acelp_core_mode(usac_data->td_encoder[i_ch],
904
2.69k
                                    ptr_usac_config->core_sample_rate,
905
2.69k
                                    usac_data->td_bitrate[i_ch]);
906
907
2.69k
      usac_data->acelp_core_mode[i_ch] = (usac_data->td_encoder[i_ch])->acelp_core_mode;
908
2.69k
      if (ptr_usac_config->core_sample_rate == 8000) {
909
197
        usac_data->use_acelp_only = 1;
910
197
      }
911
2.69k
    }
912
2.06k
  } else {
913
2.06k
    usac_data->acelp_core_mode[0] = 0;
914
2.06k
  }
915
916
3.75k
  for (UWORD32 ch = 0;
917
7.50k
       ch < pstr_asc_usac_config->num_elements - ptr_usac_config->num_ext_elements; ch++) {
918
3.75k
    iusace_qc_init(&usac_data->str_qc_main.str_qc_data[ch], MAX_CHANNEL_BITS,
919
3.75k
                   ptr_usac_config->core_sample_rate, ptr_usac_config->bw_limit[ch],
920
3.75k
                   usac_data->str_qc_main.str_qc_data[ch].num_ch, ptr_usac_config->ccfl);
921
3.75k
  }
922
923
3.75k
  return err_code;
924
3.75k
}
925
926
static WORD32 iexheaax_append_bitstream(ixheaace_bit_buf_handle hdl_bitbuf_write,
927
                                        ixheaace_bit_buf_handle hdl_bitbuf_read,
928
203k
                                        WORD32 num_bits) {
929
203k
  WORD32 idx;
930
203k
  UWORD32 value;
931
932
203k
  if (num_bits > 16) {
933
202k
    WORD32 cnt, rem;
934
202k
    cnt = num_bits >> 4;
935
202k
    rem = num_bits % 16;
936
937
1.37M
    for (idx = 0; idx < cnt; idx++) {
938
1.16M
      value = ixheaace_readbits(hdl_bitbuf_read, 16);
939
1.16M
      ixheaace_write_bits(hdl_bitbuf_write, value, 16);
940
1.16M
    }
941
202k
    if (rem) {
942
187k
      value = ixheaace_readbits(hdl_bitbuf_read, (UWORD8)rem);
943
187k
      ixheaace_write_bits(hdl_bitbuf_write, value, (UWORD8)rem);
944
187k
    }
945
202k
  } else {
946
749
    value = ixheaace_readbits(hdl_bitbuf_read, (UWORD8)num_bits);
947
749
    ixheaace_write_bits(hdl_bitbuf_write, value, (UWORD8)num_bits);
948
749
  }
949
950
203k
  return num_bits;
951
203k
}
952
953
static IA_ERRORCODE iusace_enc_ext_elemts(UWORD32 usac_ext_ele_type,
954
                                          ia_usac_encoder_config_struct *pstr_usac_config,
955
                                          ia_usac_data_struct *pstr_usac_data,
956
                                          ixheaace_audio_specific_config_struct *pstr_asc,
957
                                          FLOAT32 **pptr_input, ia_bit_buf_struct *it_bit_buff,
958
56.6k
                                          WORD32 *num_bits_written) {
959
56.6k
  WORD8 idx = 0;
960
56.6k
  LOOPIDX idx_2 = 0;
961
56.6k
  WORD32 num_bits_payload = 0;
962
56.6k
  WORD32 num_byts_payload = 0;
963
56.6k
  ia_usac_config_struct *pstr_asc_usac_config = &(pstr_asc->str_usac_config);
964
56.6k
  VOID *pstr_scratch = &pstr_usac_data->str_scratch;
965
56.6k
  IA_ERRORCODE err_code = IA_NO_ERROR;
966
967
283k
  for (idx = 0; idx < (WORD32)pstr_asc_usac_config->num_elements; idx++) {
968
226k
    if (ID_USAC_EXT != pstr_asc_usac_config->usac_element_type[idx]) {
969
56.6k
      continue;
970
56.6k
    }
971
972
169k
    ia_usac_enc_element_config_struct *pstr_usac_elem_config =
973
169k
        &(pstr_asc_usac_config->str_usac_element_config[idx]);
974
975
169k
    if (usac_ext_ele_type != pstr_usac_elem_config->usac_ext_ele_type) {
976
113k
      continue;
977
113k
    }
978
979
56.6k
    switch (pstr_usac_elem_config->usac_ext_ele_type) {
980
56.6k
      case ID_EXT_ELE_UNI_DRC: {
981
56.6k
        if (pstr_usac_data->str_drc_state.is_first_drc_process_complete == 0) {
982
550
          iusace_reset_bit_buffer(&pstr_usac_data->str_drc_state.str_bit_buf_out);
983
550
          err_code = impd_drc_enc(&pstr_usac_data->str_drc_state, pptr_input, 0,
984
550
                                  &num_bits_payload, pstr_scratch);
985
550
          if (err_code) {
986
0
            return err_code;
987
0
          }
988
550
          pstr_usac_data->str_drc_state.is_first_drc_process_complete = 1;
989
550
          num_bits_payload = 0;
990
550
        }
991
992
56.6k
        iusace_reset_bit_buffer(&pstr_usac_data->str_drc_state.str_bit_buf_out);
993
56.6k
        err_code =
994
56.6k
            impd_drc_enc(&pstr_usac_data->str_drc_state, pptr_input,
995
56.6k
                         pstr_usac_config->drc_frame_size, &num_bits_payload, pstr_scratch);
996
56.6k
        if (err_code) {
997
0
          return err_code;
998
0
        }
999
56.6k
        num_byts_payload = (num_bits_payload + 7) >> 3;
1000
56.6k
      } break;
1001
0
      default: {
1002
0
      } break;
1003
56.6k
    }
1004
1005
56.6k
    if (num_byts_payload <= 0) {
1006
0
      *num_bits_written += iusace_write_bits_buf(it_bit_buff, 0, 1);  // usacExtElementPresent
1007
56.6k
    } else {
1008
56.6k
      *num_bits_written += iusace_write_bits_buf(it_bit_buff, 1, 1);  // usacExtElementPresent
1009
1010
56.6k
      *num_bits_written +=
1011
56.6k
          iusace_write_bits_buf(it_bit_buff, 0, 1);  // usacExtElementUseDefaultLength
1012
1013
56.6k
      if (num_byts_payload >= 255) {
1014
0
        *num_bits_written +=
1015
0
            iusace_write_bits_buf(it_bit_buff, 255, 8);  // usacExtElementPayloadLength
1016
1017
0
        UWORD16 value_add = (UWORD16)(num_byts_payload - 255 + 2);
1018
0
        *num_bits_written += iusace_write_bits_buf(it_bit_buff, value_add, 16);
1019
56.6k
      } else {
1020
56.6k
        *num_bits_written += iusace_write_bits_buf(it_bit_buff, num_byts_payload,
1021
56.6k
                                                   8);  // usacExtElementPayloadLength
1022
56.6k
      }
1023
1024
56.6k
      switch (pstr_usac_elem_config->usac_ext_ele_type) {
1025
56.6k
        case ID_EXT_ELE_UNI_DRC: {
1026
477k
          for (idx_2 = 0; idx_2 < num_byts_payload; idx_2++) {
1027
420k
            *num_bits_written += iusace_write_bits_buf(
1028
420k
                it_bit_buff, pstr_usac_data->str_drc_state.bit_buf_base_out[idx_2], 8);
1029
420k
          }
1030
56.6k
        } break;
1031
0
        default: {
1032
0
        } break;
1033
56.6k
      }
1034
56.6k
    }
1035
56.6k
  }
1036
1037
56.6k
  return err_code;
1038
56.6k
}
1039
1040
IA_ERRORCODE ixheaace_usac_encode(FLOAT32 **ptr_input,
1041
                                  ia_usac_encoder_config_struct *ptr_usac_config,
1042
                                  ia_usac_data_struct *pstr_state,
1043
                                  ixheaace_audio_specific_config_struct *pstr_asc,
1044
                                  ia_bit_buf_struct *pstr_it_bit_buff,
1045
                                  ixheaace_pstr_sbr_enc ptr_env_encoder, FLOAT32 **pp_drc_inp,
1046
342k
                                  WORD32 *is_quant_spec_zero, WORD32 *is_gain_limited) {
1047
342k
  IA_ERRORCODE err = IA_NO_ERROR;
1048
342k
  WORD32 i_ch, i, k;
1049
342k
  ia_usac_data_struct *ptr_usac_data = pstr_state;
1050
342k
  iusace_scratch_mem *pstr_scratch = &ptr_usac_data->str_scratch;
1051
342k
  WORD32 bits_written = 0;
1052
1053
342k
  WORD32 *next_window_sequence = pstr_scratch->ptr_next_win_scratch;
1054
342k
  WORD32 *new_win_seq = pstr_scratch->ptr_next_win_scratch + MAX_TIME_CHANNELS;
1055
342k
  memset(next_window_sequence, 0, MAX_TIME_CHANNELS * sizeof(next_window_sequence));
1056
342k
  memset(new_win_seq, 0, MAX_TIME_CHANNELS * sizeof(new_win_seq));
1057
342k
  ia_sfb_params_struct *pstr_sfb_prms = &ptr_usac_config->str_sfb_prms;
1058
342k
  memset(pstr_sfb_prms, 0, sizeof(ia_sfb_params_struct));
1059
1060
342k
  WORD32 *num_window_groups = pstr_sfb_prms->num_window_groups;
1061
342k
  WORD32 average_bits_total;
1062
342k
  WORD32 num_bits;
1063
342k
  WORD32 padding_bits;
1064
342k
  WORD32 *common_win = pstr_sfb_prms->common_win;
1065
342k
  WORD32 usac_independency_flg;
1066
342k
  WORD32 mod[NUM_FRAMES] = {0};
1067
342k
  WORD32 len_frame;
1068
342k
  WORD32 len_lpc0;
1069
342k
  WORD32 len_next_high_rate;
1070
342k
  WORD8 elem_idx, nr_core_coder_channels = 0, chn = 0;
1071
342k
  WORD32 ch_offset = 0;
1072
342k
  WORD32 elem_idx_max = ptr_usac_config->num_elements - ptr_usac_config->num_ext_elements;
1073
342k
  WORD32 td_buffer_offset = (TD_BUFFER_OFFSET * ptr_usac_config->ccfl) / FRAME_LEN_LONG;
1074
342k
  usac_independency_flg = ptr_usac_data->usac_independency_flag;
1075
1076
342k
  len_frame = ptr_usac_config->ccfl;
1077
342k
  len_lpc0 = (LEN_LPC0 * len_frame) / FRAME_LEN_LONG;
1078
342k
  len_next_high_rate = (LEN_NEXT_HIGH_RATE * len_frame) / FRAME_LEN_LONG;
1079
1080
342k
  average_bits_total =
1081
342k
      (ptr_usac_config->bit_rate * ptr_usac_config->ccfl) / ptr_usac_config->core_sample_rate;
1082
1083
342k
  ptr_usac_data->min_bits_needed =
1084
342k
      (long)(ptr_usac_data->available_bitreservoir_bits + 2 * average_bits_total -
1085
342k
             ptr_usac_data->max_bitreservoir_bits);
1086
342k
  if (ptr_usac_data->min_bits_needed < 0) {
1087
73.3k
    ptr_usac_data->min_bits_needed = 0;
1088
73.3k
  }
1089
1090
342k
  num_bits = 0;
1091
342k
  if (ptr_usac_config->preroll_flag) {
1092
342k
    if (ptr_usac_config->iframes_interval != ptr_usac_config->num_preroll_frames) {
1093
338k
      iusace_write_bits_buf(pstr_it_bit_buff, usac_independency_flg, 1);
1094
338k
      num_bits++;
1095
338k
    }
1096
342k
  } else {
1097
0
    iusace_write_bits_buf(pstr_it_bit_buff, usac_independency_flg, 1);
1098
0
    num_bits++;
1099
0
  }
1100
684k
  for (elem_idx = 0; elem_idx < elem_idx_max; elem_idx++) {
1101
342k
    switch (ptr_usac_data->channel_elem_type[elem_idx]) {
1102
87.0k
      case USAC_ELEMENT_TYPE_SCE:
1103
87.0k
        nr_core_coder_channels = 1;
1104
87.0k
        break;
1105
255k
      case USAC_ELEMENT_TYPE_CPE:
1106
255k
        nr_core_coder_channels = 2;
1107
255k
        break;
1108
342k
    }
1109
1110
342k
    if (ptr_usac_data->core_mode[0] == CORE_MODE_FD) {
1111
703k
      for (chn = 0, i_ch = ch_offset; chn < nr_core_coder_channels; chn++, i_ch++) {
1112
456k
        iusace_block_switching(&ptr_usac_data->block_switch_ctrl[i_ch], ptr_input[i_ch],
1113
456k
                               ptr_usac_config->ccfl);
1114
456k
      }
1115
247k
    }
1116
1117
342k
    i_ch = ch_offset;
1118
342k
    if (nr_core_coder_channels == 2) {
1119
255k
      iusace_sync_block_switching(&ptr_usac_data->block_switch_ctrl[i_ch],
1120
255k
                                  &ptr_usac_data->block_switch_ctrl[i_ch + 1]);
1121
255k
    }
1122
1123
941k
    for (chn = 0, i_ch = ch_offset; chn < nr_core_coder_channels; chn++, i_ch++) {
1124
598k
      switch (ptr_usac_config->codec_mode) {
1125
172k
        case USAC_SWITCHED:
1126
172k
          if (ptr_usac_data->str_sig_class_data.coding_mode == 2) {
1127
35.9k
            ptr_usac_data->core_mode_next[i_ch] = CORE_MODE_FD;
1128
136k
          } else {
1129
136k
            ptr_usac_data->core_mode_next[i_ch] = CORE_MODE_TD;
1130
136k
          }
1131
172k
          break;
1132
418k
        case USAC_ONLY_FD:
1133
418k
          ptr_usac_data->core_mode_next[i_ch] = CORE_MODE_FD;
1134
418k
          break;
1135
7.30k
        case USAC_ONLY_TD:
1136
7.30k
          ptr_usac_data->core_mode_next[i_ch] = CORE_MODE_TD;
1137
7.30k
          break;
1138
0
        default:
1139
0
          return IA_EXHEAACE_INIT_FATAL_USAC_INVALID_CODEC_MODE;
1140
598k
      }
1141
598k
      if (ptr_usac_data->core_mode[i_ch] == CORE_MODE_TD) {
1142
133M
        for (i = 0; i < ptr_usac_config->ccfl; i++) {
1143
133M
          ptr_usac_data->ptr_2frame_time_data[i_ch][i] = ptr_usac_data->ptr_time_data[i_ch][i];
1144
133M
          ptr_usac_data->ptr_2frame_time_data[i_ch][ptr_usac_config->ccfl + i] =
1145
133M
              ptr_usac_data->ptr_look_ahead_time_data[i_ch][i];
1146
133M
          ptr_usac_data->ptr_time_data[i_ch][i] =
1147
133M
              ptr_usac_data->ptr_look_ahead_time_data[i_ch][i];
1148
133M
          ptr_usac_data->ptr_look_ahead_time_data[i_ch][i] = (FLOAT64)ptr_input[i_ch][i];
1149
133M
        }
1150
456k
      } else {
1151
424M
        for (i = 0; i < ptr_usac_config->ccfl; i++) {
1152
424M
          ptr_usac_data->ptr_2frame_time_data[i_ch][i] = ptr_usac_data->ptr_time_data[i_ch][i];
1153
424M
          ptr_usac_data->ptr_2frame_time_data[i_ch][ptr_usac_config->ccfl + i] =
1154
424M
              ptr_usac_data->ptr_look_ahead_time_data[i_ch][i];
1155
424M
          ptr_usac_data->ptr_time_data[i_ch][i] = ptr_input[i_ch][i];
1156
424M
          ptr_usac_data->ptr_look_ahead_time_data[i_ch][i] = (FLOAT64)ptr_input[i_ch][i];
1157
424M
        }
1158
456k
      }
1159
1160
714M
      for (i = 0; i < len_frame + len_next_high_rate; i++) {
1161
714M
        ptr_usac_data->td_in_buf[i_ch][i] =
1162
714M
            (FLOAT32)(ptr_usac_data->ptr_2frame_time_data[i_ch][i + td_buffer_offset]);
1163
714M
      }
1164
854M
      for (i = 0; i < len_frame + len_next_high_rate + len_lpc0; i++) {
1165
853M
        ptr_usac_data->td_in_prev_buf[i_ch][i] =
1166
853M
            (FLOAT32)(ptr_usac_data->ptr_2frame_time_data[i_ch][i + td_buffer_offset - len_lpc0]);
1167
853M
      }
1168
1169
598k
      if (ptr_usac_data->core_mode[i_ch] == CORE_MODE_FD) {
1170
456k
        ptr_usac_data->window_size_samples[i_ch] = ptr_usac_config->ccfl;
1171
456k
        pstr_sfb_prms->window_sequence[i_ch] = ptr_usac_data->block_switch_ctrl[i_ch].window_seq;
1172
456k
        ptr_usac_config->window_sequence[i_ch] = pstr_sfb_prms->window_sequence[i_ch];
1173
456k
        new_win_seq[i_ch] = ptr_usac_data->block_switch_ctrl[i_ch].next_win_seq;
1174
456k
      }
1175
1176
598k
      err = iusace_sfb_params_init(((LEN_SUPERFRAME_768 == ptr_usac_config->ccfl) &&
1177
216k
                                  (0 == ptr_usac_config->sbr_enable)) ?
1178
172k
                                  (iusace_map_sample_rate((ptr_usac_config->core_sample_rate
1179
425k
                                   * 4) / 3)) :(ptr_usac_config->core_sample_rate),
1180
598k
                                   ptr_usac_config->ccfl, pstr_sfb_prms->sfb_width_table[i_ch],
1181
598k
                                   &pstr_sfb_prms->num_sfb[i_ch],
1182
598k
                                   pstr_sfb_prms->window_sequence[i_ch]);
1183
1184
598k
      if (err) {
1185
0
        return err;
1186
0
      }
1187
1188
598k
      pstr_sfb_prms->sfb_offset[i_ch][0] = 0;
1189
598k
      k = 0;
1190
22.0M
      for (i = 0; i < pstr_sfb_prms->num_sfb[i_ch]; i++) {
1191
21.4M
        pstr_sfb_prms->sfb_offset[i_ch][i] = k;
1192
21.4M
        k += pstr_sfb_prms->sfb_width_table[i_ch][i];
1193
21.4M
      }
1194
598k
      pstr_sfb_prms->sfb_offset[i_ch][i] = k;
1195
1196
598k
      if (ptr_usac_data->core_mode[i_ch] != CORE_MODE_TD) {
1197
456k
        next_window_sequence[i_ch] = new_win_seq[i_ch];
1198
456k
        if (ptr_usac_data->core_mode_next[i_ch] == CORE_MODE_TD) {
1199
4.30k
          next_window_sequence[i_ch] = EIGHT_SHORT_SEQUENCE;
1200
4.30k
        }
1201
1202
456k
        if (ptr_usac_data->core_mode[i_ch] == CORE_MODE_TD &&
1203
0
            ptr_usac_data->core_mode_next[i_ch] != CORE_MODE_TD) {
1204
0
          next_window_sequence[i_ch] = LONG_STOP_SEQUENCE;
1205
0
        }
1206
1207
456k
        if (next_window_sequence[i_ch] == EIGHT_SHORT_SEQUENCE) {
1208
128k
          if (pstr_sfb_prms->window_sequence[i_ch] == ONLY_LONG_SEQUENCE) {
1209
1.14k
            pstr_sfb_prms->window_sequence[i_ch] = LONG_START_SEQUENCE;
1210
1.14k
          }
1211
128k
          if (pstr_sfb_prms->window_sequence[i_ch] == LONG_STOP_SEQUENCE) {
1212
169
            pstr_sfb_prms->window_sequence[i_ch] = STOP_START_SEQUENCE;
1213
169
          }
1214
128k
        }
1215
1216
456k
        if (next_window_sequence[i_ch] == ONLY_LONG_SEQUENCE) {
1217
252k
          if (pstr_sfb_prms->window_sequence[i_ch] == EIGHT_SHORT_SEQUENCE) {
1218
5.25k
            next_window_sequence[i_ch] = LONG_STOP_SEQUENCE;
1219
5.25k
          }
1220
252k
        }
1221
1222
456k
        if (pstr_sfb_prms->window_sequence[i_ch] == EIGHT_SHORT_SEQUENCE) {
1223
155k
          num_window_groups[i_ch] = ptr_usac_data->block_switch_ctrl[i_ch].tot_grps_cnt;
1224
1.40M
          for (i = 0; i < 8; i++) {
1225
1.24M
            pstr_sfb_prms->window_group_length[i_ch][i] =
1226
1.24M
                ptr_usac_data->block_switch_ctrl[i_ch].group_len[i];
1227
1.24M
          }
1228
301k
        } else {
1229
301k
          num_window_groups[i_ch] = 1;
1230
301k
          pstr_sfb_prms->window_group_length[i_ch][0] = 1;
1231
301k
        }
1232
1233
456k
        pstr_sfb_prms->window_shape[i_ch] = ptr_usac_config->window_shape_prev[i_ch];
1234
1235
456k
        err = iusace_fd_mdct(ptr_usac_data, ptr_usac_config, i_ch);
1236
1237
456k
        if (err) {
1238
0
          return err;
1239
0
        }
1240
1241
456k
        if (pstr_sfb_prms->window_sequence[i_ch] != EIGHT_SHORT_SEQUENCE) {
1242
301k
          iusace_psy_mod_lb(&ptr_usac_data->str_psy_mod, pstr_sfb_prms,
1243
301k
                            ptr_usac_data->spectral_line_vector[i_ch],
1244
301k
                            ptr_usac_data->pstr_tns_info, ptr_usac_config->tns_select, i_ch, chn,
1245
301k
                            ptr_usac_data->channel_elem_type[elem_idx],
1246
301k
                            pstr_scratch->p_tns_filter, elem_idx, pstr_scratch->ptr_tns_scratch,
1247
301k
                            ptr_usac_config->ccfl);
1248
301k
        } else {
1249
155k
          iusace_psy_mod_sb(&(ptr_usac_data->str_psy_mod), pstr_sfb_prms,
1250
155k
                            ptr_usac_data->spectral_line_vector[i_ch],
1251
155k
                            ptr_usac_data->pstr_tns_info, ptr_usac_config->tns_select, i_ch, chn,
1252
155k
                            ptr_usac_data->channel_elem_type[elem_idx],
1253
155k
                            pstr_scratch->p_tns_filter, elem_idx, pstr_scratch->ptr_tns_scratch,
1254
155k
                            ptr_usac_config->ccfl);
1255
155k
        }
1256
1257
456k
        pstr_sfb_prms->max_sfb[i_ch] =
1258
456k
            ptr_usac_data->str_psy_mod.str_psy_out_data[i_ch].max_sfb_per_grp;
1259
456k
      }
1260
598k
    }
1261
685k
    for (chn = 0, i_ch = ch_offset; chn < nr_core_coder_channels; chn++, i_ch++) {
1262
342k
      if (nr_core_coder_channels == 2) {
1263
255k
        if ((pstr_sfb_prms->window_shape[i_ch] == pstr_sfb_prms->window_shape[i_ch + 1]) &&
1264
255k
            (pstr_sfb_prms->window_sequence[i_ch] == pstr_sfb_prms->window_sequence[i_ch + 1]) &&
1265
255k
            (ptr_usac_data->core_mode[i_ch] == ptr_usac_data->core_mode[i_ch + 1])) {
1266
255k
          common_win[i_ch] = common_win[i_ch + 1] = 1;
1267
255k
        } else {
1268
0
          common_win[i_ch] = 0;
1269
0
        }
1270
255k
        chn++;
1271
255k
      } else {
1272
87.0k
        common_win[i_ch] = 0;
1273
87.0k
      }
1274
342k
    }
1275
342k
    if (nr_core_coder_channels == 2) {
1276
255k
      if (i_ch == (ch_offset + 1)) {
1277
255k
        if (pstr_sfb_prms->window_sequence[i_ch] != EIGHT_SHORT_SEQUENCE) {
1278
182k
          iusace_calc_ms_band_energy(
1279
182k
              ptr_usac_data->spectral_line_vector[ch_offset],
1280
182k
              ptr_usac_data->spectral_line_vector[ch_offset + 1],
1281
182k
              ptr_usac_data->str_psy_mod.str_psy_long_config[elem_idx].sfb_offset,
1282
182k
              ptr_usac_data->str_psy_mod.str_psy_long_config[elem_idx].sfb_active,
1283
182k
              ptr_usac_data->str_psy_mod.str_psy_data[ch_offset].ptr_sfb_energy_long_ms,
1284
182k
              ptr_usac_data->str_psy_mod.str_psy_data[ch_offset + 1].ptr_sfb_energy_long_ms);
1285
182k
        } else {
1286
73.6k
          WORD32 frame_len_short = (ptr_usac_config->ccfl * FRAME_LEN_SHORT_128) / FRAME_LEN_LONG;
1287
662k
          for (WORD32 w = 0; w < MAX_SHORT_WINDOWS; w++) {
1288
588k
            WORD32 w_offset = w * frame_len_short;
1289
1290
588k
            iusace_calc_ms_band_energy(
1291
588k
                ptr_usac_data->spectral_line_vector[ch_offset] + w_offset,
1292
588k
                ptr_usac_data->spectral_line_vector[ch_offset + 1] + w_offset,
1293
588k
                ptr_usac_data->str_psy_mod.str_psy_short_config[elem_idx].sfb_offset,
1294
588k
                ptr_usac_data->str_psy_mod.str_psy_short_config[elem_idx].sfb_active,
1295
588k
                ptr_usac_data->str_psy_mod.str_psy_data[ch_offset].ptr_sfb_energy_short_ms[w],
1296
588k
                ptr_usac_data->str_psy_mod.str_psy_data[ch_offset + 1]
1297
588k
                    .ptr_sfb_energy_short_ms[w]);
1298
588k
          }
1299
73.6k
        }
1300
255k
      }
1301
255k
    }
1302
342k
    if ((nr_core_coder_channels == 2)
1303
342k
            ? ((ptr_usac_data->core_mode[ch_offset] == CORE_MODE_FD) &&
1304
209k
               (ptr_usac_data->core_mode[ch_offset + 1] == CORE_MODE_FD))
1305
342k
            : ((ptr_usac_data->core_mode[ch_offset] == CORE_MODE_FD))) {
1306
247k
      iusace_grouping(pstr_sfb_prms, nr_core_coder_channels, ptr_usac_data, ptr_usac_config,
1307
247k
                      ch_offset, elem_idx);
1308
1309
247k
      if (nr_core_coder_channels == 2) {
1310
209k
        err = iusace_stereo_proc(pstr_sfb_prms, usac_independency_flg, ptr_usac_data,
1311
209k
                                 ptr_usac_config, ch_offset);
1312
209k
        if (err != IA_NO_ERROR) {
1313
0
          return err;
1314
0
        }
1315
209k
      }
1316
247k
    }
1317
342k
    if (ptr_usac_config->preroll_flag) {
1318
342k
      if (ptr_usac_config->iframes_interval != ptr_usac_config->num_preroll_frames) {
1319
338k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);  // extension element present
1320
338k
        num_bits++;
1321
338k
      }
1322
342k
    }
1323
342k
    if (ptr_usac_config->use_drc_element) {
1324
56.6k
      WORD32 num_bits_ext_elem = 0;
1325
56.6k
      err = iusace_enc_ext_elemts(ID_EXT_ELE_UNI_DRC, ptr_usac_config, pstr_state, pstr_asc,
1326
56.6k
                                  pp_drc_inp, pstr_it_bit_buff, &num_bits_ext_elem);
1327
56.6k
      if (err & IA_FATAL_ERROR) {
1328
0
        return err;
1329
0
      }
1330
56.6k
      num_bits += num_bits_ext_elem;
1331
56.6k
      ptr_usac_data->num_drc_bits = num_bits_ext_elem;
1332
#ifdef DRC_BITRATE_CONSIDERATION
1333
      ptr_usac_data->drc_data_bit_cnt = num_bits_ext_elem;
1334
#endif
1335
56.6k
    }
1336
1337
941k
    for (chn = 0, i_ch = ch_offset; chn < nr_core_coder_channels; chn++, i_ch++) {
1338
598k
      iusace_write_bits_buf(pstr_it_bit_buff, ptr_usac_data->core_mode[i_ch], 1);
1339
598k
      num_bits++;
1340
598k
    }
1341
1342
941k
    for (chn = 0, i_ch = ch_offset; chn < nr_core_coder_channels; chn++, i_ch++) {
1343
598k
      if (ptr_usac_data->core_mode[i_ch] == CORE_MODE_FD) {
1344
456k
        ptr_usac_data->window_size_samples[i_ch] = ptr_usac_config->ccfl;
1345
456k
        pstr_sfb_prms->window_sequence[i_ch] = ptr_usac_data->block_switch_ctrl[i_ch].window_seq;
1346
456k
        ptr_usac_config->window_sequence[i_ch] = pstr_sfb_prms->window_sequence[i_ch];
1347
456k
        new_win_seq[i_ch] = ptr_usac_data->block_switch_ctrl[i_ch].next_win_seq;
1348
456k
      }
1349
1350
598k
      if (ptr_usac_data->core_mode[i_ch] == CORE_MODE_TD) {
1351
141k
        WORD32 error;
1352
1353
141k
        error = iusace_lpd_frm_enc(ptr_usac_data, mod, usac_independency_flg, len_frame, i_ch,
1354
141k
                                   pstr_it_bit_buff);
1355
141k
        if (error) return error;
1356
1357
141k
        num_bits = pstr_it_bit_buff->cnt_bits;
1358
1359
141k
        if ((ptr_usac_data->core_mode_prev[i_ch] == CORE_MODE_FD) && (mod[0] == 0)) {
1360
181k
          for (i = 0; i < ptr_usac_data->num_td_fac_bits[i_ch]; i++) {
1361
178k
            iusace_write_bits_buf(pstr_it_bit_buff, ptr_usac_data->fac_out_stream[i_ch][i], 1);
1362
178k
            num_bits++;
1363
178k
          }
1364
3.27k
        }
1365
456k
      } else {
1366
456k
        next_window_sequence[i_ch] = new_win_seq[i_ch];
1367
456k
        if (ptr_usac_data->core_mode_next[i_ch] == CORE_MODE_TD) {
1368
4.30k
          next_window_sequence[i_ch] = EIGHT_SHORT_SEQUENCE;
1369
4.30k
        }
1370
1371
456k
        if (ptr_usac_data->core_mode[i_ch] == CORE_MODE_TD &&
1372
0
            ptr_usac_data->core_mode_next[i_ch] != CORE_MODE_TD) {
1373
0
          next_window_sequence[i_ch] = LONG_STOP_SEQUENCE;
1374
0
        }
1375
1376
456k
        if (next_window_sequence[i_ch] == EIGHT_SHORT_SEQUENCE) {
1377
128k
          if (pstr_sfb_prms->window_sequence[i_ch] == ONLY_LONG_SEQUENCE) {
1378
1.14k
            pstr_sfb_prms->window_sequence[i_ch] = LONG_START_SEQUENCE;
1379
1.14k
          }
1380
128k
          if (pstr_sfb_prms->window_sequence[i_ch] == LONG_STOP_SEQUENCE) {
1381
169
            pstr_sfb_prms->window_sequence[i_ch] = STOP_START_SEQUENCE;
1382
169
          }
1383
128k
        }
1384
456k
        if (next_window_sequence[i_ch] == ONLY_LONG_SEQUENCE) {
1385
252k
          if (pstr_sfb_prms->window_sequence[i_ch] == EIGHT_SHORT_SEQUENCE) {
1386
5.25k
            next_window_sequence[i_ch] = LONG_STOP_SEQUENCE;
1387
5.25k
          }
1388
252k
        }
1389
456k
        if (pstr_sfb_prms->window_sequence[i_ch] == EIGHT_SHORT_SEQUENCE) {
1390
155k
          num_window_groups[i_ch] = ptr_usac_data->block_switch_ctrl[i_ch].tot_grps_cnt;
1391
1.40M
          for (i = 0; i < 8; i++) {
1392
1.24M
            pstr_sfb_prms->window_group_length[i_ch][i] =
1393
1.24M
                ptr_usac_data->block_switch_ctrl[i_ch].group_len[i];
1394
1.24M
          }
1395
301k
        } else {
1396
301k
          num_window_groups[i_ch] = 1;
1397
301k
          pstr_sfb_prms->window_group_length[i_ch][0] = 1;
1398
301k
        }
1399
456k
        pstr_sfb_prms->window_shape[i_ch] = ptr_usac_config->window_shape_prev[i_ch];
1400
456k
      }
1401
598k
    }
1402
1403
342k
    if ((nr_core_coder_channels == 2)
1404
342k
            ? ((ptr_usac_data->core_mode[ch_offset] == CORE_MODE_FD) &&
1405
209k
               (ptr_usac_data->core_mode[ch_offset + 1] == CORE_MODE_FD))
1406
342k
            : ((ptr_usac_data->core_mode[ch_offset] == CORE_MODE_FD))) {
1407
247k
      err = iusace_fd_encode(pstr_sfb_prms, usac_independency_flg, ptr_usac_data, ptr_usac_config,
1408
247k
                             pstr_it_bit_buff, nr_core_coder_channels, ch_offset, elem_idx,
1409
247k
                             &bits_written, is_quant_spec_zero, is_gain_limited);
1410
1411
247k
      if (err) {
1412
1.11k
        return err;
1413
1.11k
      }
1414
1415
246k
      num_bits += bits_written;
1416
246k
    }
1417
1418
937k
    for (chn = 0, i_ch = ch_offset; chn < nr_core_coder_channels; chn++, i_ch++) {
1419
596k
      ptr_usac_config->window_shape_prev[i_ch] = pstr_sfb_prms->window_shape[i_ch];
1420
596k
      ptr_usac_config->window_sequence_prev[i_ch] = ptr_usac_config->window_sequence[i_ch];
1421
596k
      ptr_usac_config->window_sequence[i_ch] = next_window_sequence[i_ch];
1422
596k
      ptr_usac_data->core_mode_prev[i_ch] = ptr_usac_data->core_mode[i_ch];
1423
596k
      ptr_usac_data->core_mode[i_ch] = ptr_usac_data->core_mode_next[i_ch];
1424
596k
    }
1425
341k
    ch_offset += nr_core_coder_channels;
1426
341k
  }
1427
1428
341k
  if (1 == ptr_usac_config->sbr_enable) {
1429
    // Append SBR bits
1430
203k
    ixheaace_bit_buf_handle pstr_it_bit_buff_temp =
1431
203k
        &ptr_env_encoder->str_cmon_data.str_sbr_bit_buf;
1432
203k
    WORD32 check_num_bits = ia_enhaacplus_enc_get_bits_available(pstr_it_bit_buff_temp);
1433
1434
203k
    num_bits += iexheaax_append_bitstream((ixheaace_bit_buf_handle)pstr_it_bit_buff,
1435
203k
                                          pstr_it_bit_buff_temp, check_num_bits);
1436
203k
  }
1437
1438
341k
  if (ptr_usac_config->use_fill_element) {
1439
341k
    WORD32 full_elem_num_bits = 0;
1440
341k
    padding_bits = ptr_usac_data->min_bits_needed - num_bits;
1441
341k
    full_elem_num_bits = iusace_write_fill_ele(pstr_it_bit_buff, padding_bits);
1442
341k
    num_bits += full_elem_num_bits;
1443
341k
  }
1444
1445
341k
  ptr_usac_data->available_bitreservoir_bits -= num_bits;
1446
1447
341k
  if (num_bits % 8) {
1448
296k
    ptr_usac_data->available_bitreservoir_bits -= 8 - (num_bits % 8);
1449
296k
  }
1450
341k
  ptr_usac_data->available_bitreservoir_bits += average_bits_total;
1451
1452
341k
  if (ptr_usac_data->available_bitreservoir_bits > ptr_usac_data->max_bitreservoir_bits) {
1453
0
    ptr_usac_data->available_bitreservoir_bits = ptr_usac_data->max_bitreservoir_bits;
1454
0
  }
1455
1456
341k
  return err;
1457
342k
}
1458
1459
4.88k
IA_ERRORCODE iusace_validate_baseline_profile_sample_rate(WORD32 sample_rate) {
1460
4.88k
  IA_ERRORCODE err_code = IA_NO_ERROR;
1461
1462
4.88k
  if ((sample_rate != 7350) && (sample_rate != 8000) && (sample_rate != 9600) &&
1463
4.63k
      (sample_rate != 11025) && (sample_rate != 12000) && (sample_rate != 12800) &&
1464
4.36k
      (sample_rate != 16000) && (sample_rate != 19200) && (sample_rate != 22050) &&
1465
3.98k
      (sample_rate != 24000) && (sample_rate != 32000) && (sample_rate != 38400) &&
1466
2.80k
      (sample_rate != 44100) && (sample_rate != 48000) && (sample_rate != 64000) &&
1467
651
      (sample_rate != 88200) && (sample_rate != 96000)) {
1468
191
    err_code = IA_EXHEAACE_CONFIG_FATAL_USAC_BASELINE_INVALID_SAMP_FREQ;
1469
191
  }
1470
1471
4.88k
  return err_code;
1472
4.88k
}