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
23 #include "ixheaac_type_def.h"
24 #include "ixheaac_constants.h"
25 #include "impd_drc_common_enc.h"
26 #include "impd_drc_uni_drc.h"
27 #include "impd_drc_tables.h"
28 #include "impd_drc_api.h"
29 #include "ixheaace_api.h"
30 #include "ixheaace_aac_constants.h"
31 #include "ixheaace_error_codes.h"
32 #include "ixheaac_error_standards.h"
33 #include "ixheaac_basic_ops32.h"
34 #include "ixheaac_basic_ops16.h"
35 #include "ixheaac_basic_ops40.h"
36 #include "ixheaac_basic_ops.h"
37
38 #include "ixheaace_sbr_header.h"
39 #include "ixheaace_sbr_def.h"
40 #include "ixheaace_resampler.h"
41 #include "ixheaace_sbr_rom.h"
42 #include "ixheaace_common_rom.h"
43 #include "ixheaace_sbr_hbe.h"
44 #include "ixheaace_sbr_qmf_enc.h"
45 #include "ixheaace_sbr_tran_det.h"
46 #include "ixheaace_sbr_frame_info_gen.h"
47 #include "ixheaace_sbr_env_est.h"
48 #include "ixheaace_sbr_code_envelope.h"
49 #include "ixheaace_psy_const.h"
50 #include "ixheaace_tns.h"
51 #include "ixheaace_tns_params.h"
52 #include "ixheaace_rom.h"
53 #include "ixheaace_common_rom.h"
54 #include "ixheaace_bitbuffer.h"
55
56 #include "ixheaace_sbr_main.h"
57 #include "ixheaace_common_rom.h"
58 #include "ixheaace_sbr_missing_harmonics_det.h"
59 #include "ixheaace_sbr_inv_filtering_estimation.h"
60 #include "ixheaace_sbr_noise_floor_est.h"
61 #include "ixheaace_sbr_ton_corr.h"
62 #include "iusace_esbr_pvc.h"
63 #include "iusace_esbr_inter_tes.h"
64 #include "ixheaace_sbr.h"
65
66 #include "ixheaace_sbr_freq_scaling.h"
67
68 #include "ixheaace_bitbuffer.h"
69
70 #include "ixheaace_sbr_hybrid.h"
71 #include "ixheaace_sbr_ps_enc.h"
72 #include "ixheaace_sbr_cmondata.h"
73 #include "ixheaace_sbr_crc.h"
74 #include "ixheaace_sbr_enc_struct.h"
75
ixheaace_set_usac_sbr_params(ixheaace_pstr_sbr_enc pstr_env_enc,WORD32 usac_indep_flag,WORD32 sbr_pre_proc,WORD32 sbr_pvc_active,WORD32 sbr_pvc_mode,WORD32 inter_tes_active,WORD32 sbr_harmonic,WORD32 sbr_patching_mode)76 VOID ixheaace_set_usac_sbr_params(ixheaace_pstr_sbr_enc pstr_env_enc, WORD32 usac_indep_flag,
77 WORD32 sbr_pre_proc, WORD32 sbr_pvc_active, WORD32 sbr_pvc_mode,
78 WORD32 inter_tes_active, WORD32 sbr_harmonic,
79 WORD32 sbr_patching_mode) {
80 WORD32 ch;
81 pstr_env_enc->str_sbr_bs.usac_indep_flag = usac_indep_flag;
82 pstr_env_enc->str_sbr_hdr.sbr_pre_proc = sbr_pre_proc;
83 pstr_env_enc->str_sbr_hdr.sbr_pvc_active = sbr_pvc_active;
84 if (pstr_env_enc->str_sbr_cfg.num_ch == 2) {
85 pstr_env_enc->str_sbr_hdr.sbr_pvc_mode = 0;
86 } else {
87 pstr_env_enc->str_sbr_hdr.sbr_pvc_mode = sbr_pvc_mode;
88 }
89 pstr_env_enc->str_sbr_hdr.sbr_inter_tes_active = inter_tes_active;
90 pstr_env_enc->str_sbr_hdr.sbr_harmonic = sbr_harmonic;
91 for (ch = 0; ch < pstr_env_enc->str_sbr_cfg.num_ch; ch++) {
92 pstr_env_enc->pstr_env_channel[ch]->enc_env_data.sbr_inter_tes = inter_tes_active;
93 pstr_env_enc->pstr_env_channel[ch]->enc_env_data.sbr_patching_mode = sbr_patching_mode;
94 }
95 }
ixheaace_get_hbe_resample_buffer(ixheaace_pstr_sbr_enc pstr_env_enc)96 FLOAT32 *ixheaace_get_hbe_resample_buffer(ixheaace_pstr_sbr_enc pstr_env_enc) {
97 return pstr_env_enc->ptr_hbe_resample_buf;
98 }
99
ia_enhaacplus_enc_get_sbr_tuning_table_idx(UWORD32 bitrate,UWORD32 num_ch,UWORD32 sample_rate,ixheaace_str_qmf_tabs * pstr_qmf_tab,UWORD32 * ptr_closest_br,WORD32 * ptr_idx_sr,WORD32 * ptr_idx_ch,WORD32 * ptr_idx_entry,ixheaace_sbr_tuning_tables sbr_tune_table[10][2][10])100 static FLAG ia_enhaacplus_enc_get_sbr_tuning_table_idx(
101 UWORD32 bitrate, UWORD32 num_ch, UWORD32 sample_rate, ixheaace_str_qmf_tabs *pstr_qmf_tab,
102 UWORD32 *ptr_closest_br, WORD32 *ptr_idx_sr, WORD32 *ptr_idx_ch, WORD32 *ptr_idx_entry,
103 ixheaace_sbr_tuning_tables sbr_tune_table[10][2][10]) {
104 WORD32 found = 0;
105 WORD32 i_sr, br_closest_lower_idx_sr = -1, br_closest_upper_idx_sr = -1;
106 WORD32 br_closest_lower_idx_ch = -1, br_closest_upper_idx_sch = -1;
107 UWORD32 i_ch;
108 WORD32 i_entry, br_closest_lower_idx_entry = -1, br_closest_upper_idx_entry = -1;
109 UWORD32 br_closest_upper = 0, br_closest_lower = IXHEAACE_DISTANCE_CEIL_VALUE;
110 const UWORD32 *ptr_sample_rate_supported = pstr_qmf_tab->supported_sample_rate;
111 WORD32 check_size = sizeof(sbr_tune_table[i_sr][i_ch]) / sizeof(sbr_tune_table[i_sr][i_ch][0]);
112 for (i_sr = 0; i_sr < 9; i_sr++) {
113 for (i_ch = 0; i_ch < 2; i_ch++) {
114 found = 1;
115 if ((num_ch - 1) == (i_ch) && (sample_rate == ptr_sample_rate_supported[i_sr])) {
116 for (i_entry = 0; i_entry < check_size - 1; i_entry++) {
117 if ((bitrate >= sbr_tune_table[i_sr][i_ch][i_entry].bitrate_from) &&
118 (bitrate < sbr_tune_table[i_sr][i_ch][i_entry].bitrate_to)) {
119 *ptr_idx_sr = i_sr;
120 *ptr_idx_ch = i_ch;
121 *ptr_idx_entry = i_entry;
122 return IXHEAACE_TABLE_IDX_FOUND;
123 } else {
124 if ((sbr_tune_table[i_sr][i_ch][i_entry].bitrate_from > bitrate) &&
125 (sbr_tune_table[i_sr][i_ch][i_entry].bitrate_from < br_closest_lower)) {
126 br_closest_lower = sbr_tune_table[i_sr][i_ch][i_entry].bitrate_from;
127 br_closest_lower_idx_sr = i_sr;
128 br_closest_lower_idx_ch = i_ch;
129 br_closest_lower_idx_entry = i_entry;
130 }
131 if ((sbr_tune_table[i_sr][i_ch][i_entry].bitrate_to <= bitrate) &&
132 (sbr_tune_table[i_sr][i_ch][i_entry].bitrate_to > br_closest_upper)) {
133 br_closest_upper = sbr_tune_table[i_sr][i_ch][i_entry].bitrate_to - 1;
134 br_closest_upper_idx_sr = i_sr;
135 br_closest_upper_idx_sch = i_ch;
136 br_closest_upper_idx_entry = i_entry;
137 }
138 }
139
140 if (sbr_tune_table[i_sr][i_ch][i_entry + 1].bitrate_from == 0) {
141 *ptr_idx_sr = i_sr;
142 *ptr_idx_ch = i_ch;
143 *ptr_idx_entry = i_entry;
144 break;
145 }
146 }
147 }
148 }
149 }
150
151 if (br_closest_upper_idx_entry >= 0) {
152 return IXHEAACE_TABLE_IDX_FOUND;
153 }
154
155 if (ptr_closest_br != NULL) {
156 if (found) {
157 WORD32 distance_upper = IXHEAACE_DISTANCE_CEIL_VALUE,
158 distance_lower = IXHEAACE_DISTANCE_CEIL_VALUE;
159 if (br_closest_lower_idx_entry >= 0) {
160 distance_lower = sbr_tune_table[br_closest_lower_idx_sr][br_closest_lower_idx_ch]
161 [br_closest_lower_idx_entry]
162 .bitrate_from -
163 bitrate;
164 }
165 if (br_closest_upper_idx_entry >= 0) {
166 distance_upper =
167 bitrate - sbr_tune_table[br_closest_upper_idx_sr][br_closest_upper_idx_sch]
168 [br_closest_lower_idx_entry]
169 .bitrate_to;
170 }
171
172 *ptr_closest_br = (distance_upper < distance_lower) ? br_closest_upper : br_closest_lower;
173 } else {
174 *ptr_closest_br = 0;
175 }
176 }
177
178 return IXHEAACE_TABLE_IDX_NOT_FOUND;
179 }
180
ixheaace_create_env_channel(WORD32 ch,ixheaace_pstr_sbr_config_data pstr_sbr_cfg,ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,ixheaace_pstr_enc_channel pstr_env,ixheaace_pstr_sbr_cfg params,WORD32 * ptr_common_buf,WORD32 * ptr_common_buffer2,FLOAT32 * ptr_sbr_env_r_buf,FLOAT32 * ptr_sbr_env_i_buf,ixheaace_str_sbr_tabs * pstr_sbr_tab)181 static IA_ERRORCODE ixheaace_create_env_channel(
182 WORD32 ch, ixheaace_pstr_sbr_config_data pstr_sbr_cfg,
183 ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr, ixheaace_pstr_enc_channel pstr_env,
184 ixheaace_pstr_sbr_cfg params, WORD32 *ptr_common_buf, WORD32 *ptr_common_buffer2,
185 FLOAT32 *ptr_sbr_env_r_buf, FLOAT32 *ptr_sbr_env_i_buf, ixheaace_str_sbr_tabs *pstr_sbr_tab) {
186 WORD32 e = 1;
187 WORD32 tran_fc = 0;
188 IA_ERRORCODE err_code = IA_NO_ERROR;
189
190 WORD32 start_index;
191 WORD32 noise_groups[2] = {3, 3};
192
193 e = ixheaac_shl32(1, params->e);
194
195 if (params->use_low_freq_res == 1) {
196 pstr_env->enc_env_data.freq_res_fix = FREQ_RES_LOW;
197 } else {
198 pstr_env->enc_env_data.freq_res_fix = FREQ_RES_HIGH;
199 }
200
201 pstr_env->enc_env_data.sbr_xpos_mode = (ixheaace_sbr_xpos_mode)params->sbr_xpos_mode;
202 pstr_env->enc_env_data.sbr_xpos_ctrl = params->sbr_xpos_ctrl;
203
204 if (params->is_ld_sbr) {
205 pstr_env->str_sbr_qmf.num_time_slots = 16;
206 pstr_env->str_sbr_qmf.rate = 1;
207 if (params->frame_flag_480 == 1) {
208 pstr_env->str_sbr_qmf.num_time_slots = 15;
209 }
210 } else {
211 pstr_env->str_sbr_qmf.num_time_slots = 16;
212 pstr_env->str_sbr_qmf.rate = 2;
213 if (params->frame_flag_960 == 1) {
214 pstr_env->str_sbr_qmf.num_time_slots = 15;
215 }
216 if (pstr_sbr_cfg->sbr_codec == USAC_SBR) {
217 if (USAC_SBR_RATIO_INDEX_4_1 == pstr_sbr_cfg->sbr_ratio_idx) {
218 pstr_env->str_sbr_qmf.rate = 4;
219 }
220 }
221 }
222
223 ixheaace_create_qmf_bank(&pstr_env->str_sbr_qmf, pstr_sbr_tab, params->is_ld_sbr);
224
225 start_index = 576;
226
227 err_code = ixheaace_create_ton_corr_param_extr(
228 ch, &pstr_env->str_ton_corr, pstr_sbr_cfg->sample_freq, 64, params->sbr_xpos_ctrl,
229 pstr_sbr_cfg->ptr_freq_band_tab[LOW_RES][0], pstr_sbr_cfg->ptr_v_k_master,
230 pstr_sbr_cfg->num_master, params->ana_max_level, pstr_sbr_cfg->ptr_freq_band_tab,
231 pstr_sbr_cfg->num_scf, pstr_sbr_hdr->sbr_noise_bands, params->use_speech_config,
232 ptr_common_buf, pstr_sbr_tab->ptr_qmf_tab, params->is_ld_sbr);
233 if (err_code) {
234 return err_code;
235 }
236
237 pstr_env->enc_env_data.noise_band_count =
238 pstr_env->str_ton_corr.sbr_noise_floor_est.num_of_noise_bands;
239
240 noise_groups[0] = pstr_env->enc_env_data.noise_band_count;
241 noise_groups[1] = pstr_env->enc_env_data.noise_band_count;
242
243 pstr_env->enc_env_data.sbr_invf_mode = (ixheaace_invf_mode)params->sbr_invf_mode;
244
245 if (pstr_env->enc_env_data.sbr_invf_mode == IXHEAACE_INVF_SWITCHED) {
246 pstr_env->enc_env_data.sbr_invf_mode = IXHEAACE_INVF_MID_LEVEL;
247 pstr_env->str_ton_corr.switch_inverse_filt = TRUE;
248 } else {
249 pstr_env->str_ton_corr.switch_inverse_filt = FALSE;
250 }
251
252 tran_fc = params->tran_fc;
253
254 if (tran_fc == 0) {
255 tran_fc = ixheaac_min32(5000, ixheaace_get_sbr_start_freq_raw(pstr_sbr_hdr->sbr_start_freq,
256 64, pstr_sbr_cfg->sample_freq));
257 }
258
259 tran_fc = (tran_fc * 4 * 64 / pstr_sbr_cfg->sample_freq + 1) >> 1;
260 if (params->sbr_codec == USAC_SBR) {
261 pstr_env->str_sbr_extract_env.sbr_ratio_idx = params->sbr_ratio_idx;
262 }
263 err_code = ixheaace_create_extract_sbr_envelope(
264 ch, &pstr_env->str_sbr_extract_env, start_index, ptr_common_buffer2, ptr_sbr_env_r_buf,
265 ptr_sbr_env_i_buf, params->is_ld_sbr, params->frame_flag_480, params->sbr_codec);
266 if (err_code) {
267 return err_code;
268 }
269
270 ixheaace_create_sbr_code_envelope(&pstr_env->str_sbr_code_env, pstr_sbr_cfg->num_scf,
271 params->delta_t_across_frames, params->df_edge_1st_env,
272 params->df_edge_incr);
273
274 ixheaace_create_sbr_code_envelope(&pstr_env->str_sbr_code_noise_floor, noise_groups,
275 params->delta_t_across_frames, 0, 0);
276
277 pstr_env->sbr_amp_res_init = pstr_sbr_hdr->sbr_amp_res;
278
279 err_code = ixheaace_init_sbr_huffman_tabs(
280 &pstr_env->enc_env_data, &pstr_env->str_sbr_code_env, &pstr_env->str_sbr_code_noise_floor,
281 pstr_env->sbr_amp_res_init, pstr_sbr_tab->ptr_sbr_huff_tab);
282 if (err_code) {
283 return err_code;
284 }
285
286 ixheaace_create_frame_info_generator(&pstr_env->str_sbr_env_frame, params->spread, e,
287 params->stat, pstr_env->enc_env_data.freq_res_fix,
288 params->use_low_freq_res);
289
290 ixheaace_create_sbr_transient_detector(
291 &pstr_env->str_sbr_trans_detector, pstr_sbr_cfg->sample_freq,
292 params->codec_settings.standard_bitrate * params->codec_settings.num_channels,
293 params->codec_settings.bit_rate, params->tran_thr, params->tran_det_mode, tran_fc,
294 params->frame_flag_480, params->is_ld_sbr, params->sbr_ratio_idx, params->sbr_codec,
295 pstr_sbr_cfg->ptr_freq_band_tab[0][0]);
296
297 pstr_sbr_cfg->xpos_control_switch = params->sbr_xpos_ctrl;
298 pstr_env->enc_env_data.no_harmonics = pstr_sbr_cfg->num_scf[HI];
299 pstr_env->enc_env_data.synthetic_coding = pstr_sbr_cfg->detect_missing_harmonics;
300 pstr_env->enc_env_data.add_harmonic_flag = 0;
301 ixheaace_init_esbr_inter_tes(&pstr_env->str_inter_tes_enc, params->sbr_ratio_idx);
302 pstr_env->enc_env_data.ptr_sbr_inter_tes_shape = pstr_env->str_inter_tes_enc.bs_tes_shape;
303 pstr_env->enc_env_data.ptr_sbr_inter_tes_shape_mode =
304 pstr_env->str_inter_tes_enc.bs_tes_shape_mode;
305
306 if (params->sbr_codec == USAC_SBR) {
307 pstr_env->enc_env_data.harmonic_sbr = pstr_sbr_hdr->sbr_harmonic;
308 if (1 == pstr_env->enc_env_data.harmonic_sbr) {
309 WORD32 persist_mem_used = 0, bd;
310 WORD32 upsamp_4_flag, num_aac_samples, num_out_samples;
311 switch (pstr_sbr_cfg->sbr_ratio_idx) {
312 case USAC_SBR_RATIO_INDEX_2_1:
313 upsamp_4_flag = 0;
314 num_aac_samples = 1024;
315 num_out_samples = 2048;
316 break;
317 case USAC_SBR_RATIO_INDEX_4_1:
318 upsamp_4_flag = 1;
319 num_aac_samples = 1024;
320 num_out_samples = 4096;
321 break;
322 case USAC_SBR_RATIO_INDEX_8_3:
323 upsamp_4_flag = 0;
324 num_aac_samples = 768;
325 num_out_samples = 2048;
326 break;
327 default:
328 upsamp_4_flag = 0;
329 num_aac_samples = 1024;
330 num_out_samples = 2048;
331 break;
332 }
333
334 ixheaace_esbr_hbe_data_init(
335 pstr_env->pstr_hbe_enc->pstr_hbe_txposer, num_aac_samples, upsamp_4_flag,
336 num_out_samples, pstr_env->pstr_hbe_enc->ptr_hbe_txposer_buffers, &persist_mem_used);
337
338 ixheaace_esbr_qmf_init(&(pstr_env->pstr_hbe_enc->str_codec_qmf_bank),
339 pstr_sbr_cfg->sbr_ratio_idx, num_out_samples);
340
341 for (bd = 0; bd < (IXHEAACE_MAX_FREQ_COEFFS / 2 + 1); bd++) {
342 pstr_env->pstr_hbe_enc->pstr_hbe_txposer->freq_band_tbl_lo[bd] =
343 pstr_sbr_cfg->sbr_freq_band_tab_lo[bd];
344 pstr_env->pstr_hbe_enc->pstr_hbe_txposer->freq_band_tbl_hi[bd] =
345 pstr_sbr_cfg->sbr_freq_band_tab_hi[bd];
346 }
347
348 for (; bd < (IXHEAACE_MAX_FREQ_COEFFS + 1); bd++) {
349 pstr_env->pstr_hbe_enc->pstr_hbe_txposer->freq_band_tbl_hi[bd] =
350 pstr_sbr_cfg->sbr_freq_band_tab_hi[bd];
351 }
352
353 pstr_env->pstr_hbe_enc->pstr_hbe_txposer->ptr_freq_band_tab[LO] =
354 pstr_env->pstr_hbe_enc->pstr_hbe_txposer->freq_band_tbl_lo;
355 pstr_env->pstr_hbe_enc->pstr_hbe_txposer->ptr_freq_band_tab[HI] =
356 pstr_env->pstr_hbe_enc->pstr_hbe_txposer->freq_band_tbl_hi;
357 pstr_env->pstr_hbe_enc->pstr_hbe_txposer->num_sf_bands[0] =
358 (WORD16)pstr_sbr_cfg->num_scf[0];
359 pstr_env->pstr_hbe_enc->pstr_hbe_txposer->num_sf_bands[1] =
360 (WORD16)pstr_sbr_cfg->num_scf[1];
361 pstr_env->pstr_hbe_enc->pstr_hbe_txposer->upsamp_4_flag = upsamp_4_flag;
362 err_code = ixheaace_dft_hbe_data_reinit(pstr_env->pstr_hbe_enc->pstr_hbe_txposer);
363 if (err_code) {
364 return err_code;
365 }
366 err_code = ixheaace_qmf_hbe_data_reinit(pstr_env->pstr_hbe_enc->pstr_hbe_txposer);
367 if (err_code) {
368 return err_code;
369 }
370 }
371 } else
372
373 {
374 pstr_env->enc_env_data.harmonic_sbr = 0;
375 }
376
377 return err_code;
378 }
379
380 UWORD32
ixheaace_is_sbr_setting_available(UWORD32 bitrate,UWORD32 num_output_channels,UWORD32 sample_rate_input,UWORD32 * ptr_core_sr,ixheaace_str_qmf_tabs * pstr_qmf_tab,WORD32 aot)381 ixheaace_is_sbr_setting_available(UWORD32 bitrate, UWORD32 num_output_channels,
382 UWORD32 sample_rate_input, UWORD32 *ptr_core_sr,
383 ixheaace_str_qmf_tabs *pstr_qmf_tab, WORD32 aot) {
384 FLAG table_found = IXHEAACE_TABLE_IDX_NOT_FOUND;
385 WORD32 idx_sr;
386 WORD32 idx_ch;
387 WORD32 idx_entry;
388
389 switch (num_output_channels) {
390 case MONO:
391 if (sample_rate_input < 16000 || sample_rate_input > 48000) {
392 return 0;
393 }
394 case STEREO:
395 if (sample_rate_input < 32000 || sample_rate_input > 48000) {
396 return 0;
397 }
398 }
399
400 *ptr_core_sr = sample_rate_input / 2;
401
402 table_found = ia_enhaacplus_enc_get_sbr_tuning_table_idx(
403 bitrate, num_output_channels, *ptr_core_sr, pstr_qmf_tab, NULL, &idx_sr, &idx_ch,
404 &idx_entry,
405 ((AOT_AAC_ELD == aot) ? pstr_qmf_tab->sbr_tuning_table_ld
406 : pstr_qmf_tab->sbr_tuning_table_lc));
407
408 return (table_found == IXHEAACE_TABLE_IDX_NOT_FOUND) ? 0 : 1;
409 }
410
ixheaace_sbr_limit_bitrate(UWORD32 bit_rate,UWORD32 num_ch,UWORD32 core_sample_rate,ixheaace_str_qmf_tabs * pstr_qmf_tab,WORD32 aot)411 UWORD32 ixheaace_sbr_limit_bitrate(UWORD32 bit_rate, UWORD32 num_ch, UWORD32 core_sample_rate,
412 ixheaace_str_qmf_tabs *pstr_qmf_tab, WORD32 aot) {
413 UWORD32 new_bit_rate;
414 WORD32 index;
415 WORD32 idx_sr;
416 WORD32 idx_ch;
417 WORD32 idx_entry;
418
419 index = ia_enhaacplus_enc_get_sbr_tuning_table_idx(
420 bit_rate, num_ch, core_sample_rate, pstr_qmf_tab, &new_bit_rate, &idx_sr, &idx_ch,
421 &idx_entry,
422 ((AOT_AAC_ELD == aot) ? pstr_qmf_tab->sbr_tuning_table_ld
423 : pstr_qmf_tab->sbr_tuning_table_lc));
424 if (index != IXHEAACE_TABLE_IDX_NOT_FOUND) {
425 new_bit_rate = bit_rate;
426 }
427
428 return new_bit_rate;
429 }
430
ixheaace_adjust_sbr_settings(const ixheaace_pstr_sbr_cfg pstr_config,UWORD32 bit_rate,UWORD32 num_ch,UWORD32 fs_core,UWORD32 trans_fac,UWORD32 std_br,ixheaace_str_qmf_tabs * pstr_qmf_tab,WORD32 aot)431 VOID ixheaace_adjust_sbr_settings(const ixheaace_pstr_sbr_cfg pstr_config, UWORD32 bit_rate,
432 UWORD32 num_ch, UWORD32 fs_core, UWORD32 trans_fac,
433 UWORD32 std_br, ixheaace_str_qmf_tabs *pstr_qmf_tab,
434 WORD32 aot) {
435 FLAG table_found = IXHEAACE_TABLE_IDX_NOT_FOUND;
436 WORD32 idx_sr = 0;
437 WORD32 idx_ch = 0;
438 WORD32 idx_entry = 0;
439 /* set the codec settings */
440 pstr_config->codec_settings.bit_rate = bit_rate;
441 pstr_config->codec_settings.num_channels = num_ch;
442 pstr_config->codec_settings.sample_freq = fs_core;
443 pstr_config->codec_settings.trans_fac = trans_fac;
444 pstr_config->codec_settings.standard_bitrate = std_br;
445
446 if (bit_rate <= 20000) {
447 pstr_config->parametric_coding = 0;
448 pstr_config->use_speech_config = 1;
449 }
450
451 table_found = ia_enhaacplus_enc_get_sbr_tuning_table_idx(
452 bit_rate, num_ch, fs_core, pstr_qmf_tab, NULL, &idx_sr, &idx_ch, &idx_entry,
453 ((AOT_AAC_ELD == aot) ? pstr_qmf_tab->sbr_tuning_table_ld
454 : pstr_qmf_tab->sbr_tuning_table_lc));
455
456 if (table_found == IXHEAACE_TABLE_IDX_NOT_FOUND) {
457 if (aot == AOT_USAC) {
458 if (num_ch == 1) {
459 if (bit_rate >= 30000) {
460 pstr_config->start_freq = 7;
461 pstr_config->stop_freq = 9;
462 } else {
463 pstr_config->start_freq = 5;
464 pstr_config->stop_freq = 7;
465 }
466 } else {
467 pstr_config->start_freq = 12;
468 pstr_config->stop_freq = 9;
469 }
470 }
471 } else {
472 switch (aot) {
473 case AOT_AAC_ELD: {
474 pstr_config->start_freq =
475 pstr_qmf_tab->sbr_tuning_table_ld[idx_sr][idx_ch][idx_entry].freq_band.start_freq;
476 pstr_config->stop_freq =
477 pstr_qmf_tab->sbr_tuning_table_ld[idx_sr][idx_ch][idx_entry].freq_band.stop_freq;
478
479 pstr_config->sbr_noise_bands =
480 pstr_qmf_tab->sbr_tuning_table_ld[idx_sr][idx_ch][idx_entry].noise.num_noise_bands;
481
482 pstr_config->noise_floor_offset =
483 pstr_qmf_tab->sbr_tuning_table_ld[idx_sr][idx_ch][idx_entry].noise.noise_floor_offset;
484
485 pstr_config->ana_max_level =
486 pstr_qmf_tab->sbr_tuning_table_ld[idx_sr][idx_ch][idx_entry].noise.noise_max_level;
487 pstr_config->stereo_mode =
488 pstr_qmf_tab->sbr_tuning_table_ld[idx_sr][idx_ch][idx_entry].stereo_mode;
489 pstr_config->freq_scale =
490 pstr_qmf_tab->sbr_tuning_table_ld[idx_sr][idx_ch][idx_entry].freq_scale;
491 break;
492 }
493 default: {
494 pstr_config->start_freq =
495 pstr_qmf_tab->sbr_tuning_table_lc[idx_sr][idx_ch][idx_entry].freq_band.start_freq;
496 pstr_config->stop_freq =
497 pstr_qmf_tab->sbr_tuning_table_lc[idx_sr][idx_ch][idx_entry].freq_band.stop_freq;
498
499 pstr_config->sbr_noise_bands =
500 pstr_qmf_tab->sbr_tuning_table_lc[idx_sr][idx_ch][idx_entry].noise.num_noise_bands;
501
502 pstr_config->noise_floor_offset =
503 pstr_qmf_tab->sbr_tuning_table_lc[idx_sr][idx_ch][idx_entry].noise.noise_floor_offset;
504
505 pstr_config->ana_max_level =
506 pstr_qmf_tab->sbr_tuning_table_lc[idx_sr][idx_ch][idx_entry].noise.noise_max_level;
507 pstr_config->stereo_mode =
508 pstr_qmf_tab->sbr_tuning_table_lc[idx_sr][idx_ch][idx_entry].stereo_mode;
509 pstr_config->freq_scale =
510 pstr_qmf_tab->sbr_tuning_table_lc[idx_sr][idx_ch][idx_entry].freq_scale;
511 break;
512 }
513 }
514
515 if (pstr_config->sbr_codec == ELD_SBR) {
516 pstr_config->send_header_data_time = -1;
517 if ((num_ch == NUM_CHANS_MONO) && (bit_rate <= 22000)) {
518 pstr_config->use_low_freq_res = 1;
519 }
520 if ((num_ch == NUM_CHANS_STEREO) && (bit_rate <= 48000)) {
521 pstr_config->use_low_freq_res = 1;
522 }
523 }
524 else {
525 if ((num_ch == NUM_CHANS_MONO) && (bit_rate <= 18000)) {
526 pstr_config->use_low_freq_res = 1;
527 }
528 if ((num_ch == NUM_CHANS_STEREO) && (bit_rate <= 28000)) {
529 pstr_config->use_low_freq_res = 1;
530 }
531 }
532 if (bit_rate <= 20000) {
533 pstr_config->parametric_coding = 0;
534 pstr_config->use_speech_config = 1;
535 }
536
537 if (pstr_config->use_ps) {
538 pstr_config->ps_mode = ixheaace_get_ps_mode(bit_rate);
539 }
540 }
541 }
542
ixheaace_initialize_sbr_defaults(ixheaace_pstr_sbr_cfg pstr_config)543 VOID ixheaace_initialize_sbr_defaults(ixheaace_pstr_sbr_cfg pstr_config) {
544 pstr_config->send_header_data_time = 500;
545 pstr_config->crc_sbr = 0;
546 pstr_config->tran_thr = 13000;
547 pstr_config->detect_missing_harmonics = 1;
548 pstr_config->parametric_coding = 1;
549 pstr_config->use_speech_config = 0;
550
551 pstr_config->sbr_data_extra = 0;
552 pstr_config->amp_res = IXHEAACE_SBR_AMP_RES_3_0;
553 pstr_config->tran_fc = 0;
554 pstr_config->tran_det_mode = 1;
555 pstr_config->spread = 1;
556 pstr_config->stat = 0;
557 pstr_config->e = 1;
558 pstr_config->delta_t_across_frames = 1;
559 pstr_config->df_edge_1st_env = 0.3f;
560 pstr_config->df_edge_incr = 0.3f;
561
562 pstr_config->sbr_invf_mode = IXHEAACE_INVF_SWITCHED;
563 pstr_config->sbr_xpos_mode = IXHEAACE_XPOS_LC;
564 pstr_config->sbr_xpos_ctrl = SBR_XPOS_CTRL_DEFAULT;
565 pstr_config->sbr_xpos_lvl = 0;
566
567 pstr_config->use_ps = 0;
568 pstr_config->ps_mode = -1;
569
570 pstr_config->stereo_mode = IXHEAACE_SBR_MODE_SWITCH_LRC;
571 pstr_config->ana_max_level = 6;
572 pstr_config->noise_floor_offset = 0;
573 pstr_config->start_freq = 5;
574 pstr_config->stop_freq = 9;
575
576 pstr_config->freq_scale = SBR_FREQ_SCALE_DEFAULT;
577 pstr_config->alter_scale = SBR_ALTER_SCALE_DEFAULT;
578 pstr_config->sbr_noise_bands = SBR_NOISE_BANDS_DEFAULT;
579
580 pstr_config->sbr_limiter_bands = SBR_LIMITER_BANDS_DEFAULT;
581 pstr_config->sbr_limiter_gains = SBR_LIMITER_GAINS_DEFAULT;
582 pstr_config->sbr_interpol_freq = SBR_INTERPOL_FREQ_DEFAULT;
583 pstr_config->sbr_smoothing_length = SBR_SMOOTHING_LENGTH_DEFAULT;
584 pstr_config->is_ld_sbr = 0;
585 pstr_config->is_esbr = 0;
586 pstr_config->frame_flag_960 = 0;
587 pstr_config->frame_flag_480 = 0;
588 pstr_config->hq_esbr = 0;
589 pstr_config->sbr_pvc_active = 0;
590 pstr_config->sbr_harmonic = 0;
591 pstr_config->sbr_ratio_idx = 0; // NO_SBR
592 pstr_config->use_low_freq_res = 0;
593 }
594
ia_enhaacplus_enc_update_freq_band_tab(ixheaace_pstr_sbr_config_data pstr_sbr_cfg,ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,WORD32 num_qmf_ch)595 static IA_ERRORCODE ia_enhaacplus_enc_update_freq_band_tab(
596 ixheaace_pstr_sbr_config_data pstr_sbr_cfg, ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,
597 WORD32 num_qmf_ch) {
598 IA_ERRORCODE err_code = IA_NO_ERROR;
599 WORD32 k0, k2;
600 WORD32 samp_freq = pstr_sbr_cfg->sample_freq;
601 if ((pstr_sbr_cfg->sbr_codec == USAC_SBR) &&
602 (pstr_sbr_cfg->sbr_ratio_idx == USAC_SBR_RATIO_INDEX_4_1)) {
603 samp_freq = samp_freq / 2;
604 }
605 err_code = ixheaace_find_start_and_stop_band(
606 samp_freq, num_qmf_ch, pstr_sbr_hdr->sbr_start_freq, pstr_sbr_hdr->sbr_stop_freq,
607 pstr_sbr_hdr->sample_rate_mode, &k0, &k2, pstr_sbr_cfg->sbr_ratio_idx,
608 pstr_sbr_cfg->sbr_codec);
609 if (err_code) {
610 return err_code;
611 }
612
613 err_code = ixheaace_update_freq_scale(
614 pstr_sbr_cfg->ptr_v_k_master, &pstr_sbr_cfg->num_master, k0, k2, pstr_sbr_hdr->freq_scale,
615 pstr_sbr_hdr->alter_scale, pstr_sbr_hdr->sample_rate_mode);
616 if (err_code) {
617 return err_code;
618 }
619
620 pstr_sbr_hdr->sbr_xover_band = 0;
621
622 ixheaace_update_high_res(pstr_sbr_cfg->ptr_freq_band_tab[HI], &pstr_sbr_cfg->num_scf[HI],
623 pstr_sbr_cfg->ptr_v_k_master, pstr_sbr_cfg->num_master,
624 &pstr_sbr_hdr->sbr_xover_band, pstr_sbr_hdr->sample_rate_mode,
625 num_qmf_ch);
626
627 ixheaace_update_low_res(pstr_sbr_cfg->ptr_freq_band_tab[LO], &pstr_sbr_cfg->num_scf[LO],
628 pstr_sbr_cfg->ptr_freq_band_tab[HI], pstr_sbr_cfg->num_scf[HI]);
629
630 pstr_sbr_cfg->xover_freq =
631 (pstr_sbr_cfg->ptr_freq_band_tab[LOW_RES][0] * pstr_sbr_cfg->sample_freq / num_qmf_ch +
632 1) >>
633 1;
634
635 return err_code;
636 }
637
ia_enhaacplus_enc_init_sbr_tabs(ixheaace_str_sbr_tabs * pstr_sbr_tabs)638 VOID ia_enhaacplus_enc_init_sbr_tabs(ixheaace_str_sbr_tabs *pstr_sbr_tabs) {
639 pstr_sbr_tabs->ptr_ps_tab = (ixheaace_str_ps_tab *)&ia_enhaacplus_enc_ps_tab;
640 pstr_sbr_tabs->ptr_qmf_tab = (ixheaace_str_qmf_tabs *)&ixheaace_qmf_tab;
641 pstr_sbr_tabs->ptr_sbr_huff_tab = (ixheaace_str_sbr_huff_tabs *)&ixheaace_sbr_huff_tab;
642 pstr_sbr_tabs->ptr_resamp_tab =
643 (ixheaace_resampler_table *)&ixheaace_resamp_2_to_1_iir_filt_params;
644 pstr_sbr_tabs->ptr_esbr_sfb_tab = (ixheaace_str_esbr_sfb_bin_tabs *)&ia_esbr_sfb_bin_tabs;
645 }
646
647 IA_ERRORCODE
ixheaace_env_encode_frame(ixheaace_pstr_sbr_enc pstr_env_encoder,FLOAT32 * ptr_samples,FLOAT32 * ptr_core_buffer,UWORD32 time_sn_stride,UWORD8 * ptr_num_anc_bytes,UWORD8 * ptr_anc_data,ixheaace_str_sbr_tabs * pstr_sbr_tab,ixheaace_comm_tables * pstr_common_tab,UWORD8 * ptr_mps_data,WORD32 mps_bits,WORD32 flag_fl_small,WORD32 * usac_stat_bits)648 ixheaace_env_encode_frame(ixheaace_pstr_sbr_enc pstr_env_encoder, FLOAT32 *ptr_samples,
649 FLOAT32 *ptr_core_buffer, UWORD32 time_sn_stride,
650 UWORD8 *ptr_num_anc_bytes, UWORD8 *ptr_anc_data,
651 ixheaace_str_sbr_tabs *pstr_sbr_tab,
652 ixheaace_comm_tables *pstr_common_tab, UWORD8 *ptr_mps_data,
653 WORD32 mps_bits, WORD32 flag_fl_small, WORD32 *usac_stat_bits) {
654 IA_ERRORCODE err_code = IA_NO_ERROR;
655
656 if (pstr_env_encoder != NULL) {
657 ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs = &pstr_env_encoder->str_sbr_bs;
658
659 pstr_sbr_bs->header_active = 0;
660 if ((pstr_env_encoder->str_sbr_cfg.is_ld_sbr) &&
661 (pstr_sbr_bs->count_send_header_data == pstr_sbr_bs->nr_send_header_data - 1)) {
662 pstr_sbr_bs->header_active = 1;
663 }
664
665 if (pstr_sbr_bs->count_send_header_data == 0) {
666 pstr_sbr_bs->header_active = 1;
667 }
668
669 if (pstr_sbr_bs->nr_send_header_data == 0) {
670 pstr_sbr_bs->count_send_header_data = 1;
671 } else {
672 if (pstr_env_encoder->str_sbr_cfg.is_ld_sbr) {
673 if (pstr_sbr_bs->count_send_header_data >= 0) {
674 pstr_sbr_bs->count_send_header_data++;
675
676 pstr_sbr_bs->count_send_header_data %= pstr_sbr_bs->nr_send_header_data;
677 }
678 } else {
679 pstr_sbr_bs->count_send_header_data++;
680
681 pstr_sbr_bs->count_send_header_data %= pstr_sbr_bs->nr_send_header_data;
682 }
683 }
684
685 ixheaace_init_sbr_bitstream(
686 &pstr_env_encoder->str_cmon_data, (UWORD8 *)pstr_env_encoder->sbr_payload,
687 sizeof(pstr_env_encoder->sbr_payload), pstr_env_encoder->str_sbr_bs.crc_active,
688 pstr_env_encoder->str_sbr_cfg.sbr_codec);
689
690 err_code = ixheaace_extract_sbr_envelope(ptr_samples, ptr_core_buffer, time_sn_stride,
691 pstr_env_encoder, pstr_sbr_tab, pstr_common_tab,
692 flag_fl_small);
693 if (err_code) {
694 return err_code;
695 }
696
697 if (mps_bits) {
698 WORD32 num_bytes;
699 if (pstr_env_encoder->str_sbr_cfg.sbr_codec == ELD_SBR) {
700 ixheaace_write_bits(&pstr_env_encoder->str_cmon_data.str_sbr_bit_buf,
701 IXHEAACE_MPS_EXT_LDSAC_DATA, 4);
702 ixheaace_write_bits(&pstr_env_encoder->str_cmon_data.str_sbr_bit_buf, *ptr_mps_data++, 4);
703 }
704 num_bytes = mps_bits >> 3;
705 for (WORD32 k = 0; k < num_bytes; k++) {
706 ixheaace_write_bits(&pstr_env_encoder->str_cmon_data.str_sbr_bit_buf, *ptr_mps_data++, 8);
707 }
708 if (mps_bits & 0x7) {
709 ixheaace_write_bits(&pstr_env_encoder->str_cmon_data.str_sbr_bit_buf,
710 (*ptr_mps_data++) >> (8 - (mps_bits & 0x7)), mps_bits & 0x7);
711 }
712 }
713
714 ixheaace_assemble_sbr_bitstream(&pstr_env_encoder->str_cmon_data,
715 pstr_env_encoder->str_sbr_cfg.sbr_codec);
716
717 pstr_env_encoder->sbr_payload_size =
718 ((ia_enhaacplus_enc_get_bits_available(&pstr_env_encoder->str_cmon_data.str_sbr_bit_buf) +
719 7) /
720 8);
721
722 if (pstr_env_encoder->sbr_payload_size > IXHEAACE_MAX_PAYLOAD_SIZE) {
723 pstr_env_encoder->sbr_payload_size = 0;
724 }
725
726 if (ptr_anc_data) {
727 *ptr_num_anc_bytes = (UWORD8)pstr_env_encoder->sbr_payload_size;
728 memcpy(ptr_anc_data, pstr_env_encoder->sbr_payload, pstr_env_encoder->sbr_payload_size);
729 }
730 if (usac_stat_bits) {
731 *usac_stat_bits = pstr_env_encoder->str_cmon_data.sbr_hdr_bits +
732 pstr_env_encoder->str_cmon_data.sbr_data_bits;
733 }
734 }
735 return err_code;
736 }
737
ixheaace_sbr_enc_scr_size(VOID)738 WORD32 ixheaace_sbr_enc_scr_size(VOID) {
739 return IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_sbr_enc_scratch), BYTE_ALIGN_8);
740 }
741
ia_enhaacplus_enc_get_scratch_bufs(VOID * ptr_scr,FLOAT32 ** ptr_shared_buf1,FLOAT32 ** ptr_shared_buf2)742 VOID ia_enhaacplus_enc_get_scratch_bufs(VOID *ptr_scr, FLOAT32 **ptr_shared_buf1,
743 FLOAT32 **ptr_shared_buf2) {
744 ixheaace_str_sbr_enc_scratch *ptr_sbr_enc_scr = (ixheaace_str_sbr_enc_scratch *)ptr_scr;
745
746 *ptr_shared_buf1 = ptr_sbr_enc_scr->sbr_env_r_buf;
747 *ptr_shared_buf2 = ptr_sbr_enc_scr->sbr_env_i_buf;
748 }
749
ia_enhaacplus_enc_get_shared_bufs(VOID * ptr_scr,WORD32 ** ptr_shared_buf1,WORD32 ** ptr_shared_buf2,WORD32 ** ptr_shared_buf3,WORD8 ** ptr_shared_buf4,WORD32 aacenc_blocksize)750 VOID ia_enhaacplus_enc_get_shared_bufs(VOID *ptr_scr, WORD32 **ptr_shared_buf1,
751 WORD32 **ptr_shared_buf2, WORD32 **ptr_shared_buf3,
752 WORD8 **ptr_shared_buf4, WORD32 aacenc_blocksize) {
753 ixheaace_str_sbr_enc_scratch *ptr_sbr_enc_scr = (ixheaace_str_sbr_enc_scratch *)ptr_scr;
754
755 *ptr_shared_buf1 = (WORD32 *)ptr_sbr_enc_scr->ps_buf3;
756 *ptr_shared_buf2 = (WORD32 *)ptr_sbr_enc_scr->sbr_env_r_buf;
757 *ptr_shared_buf3 = (WORD32 *)ptr_sbr_enc_scr->sbr_env_i_buf;
758 *ptr_shared_buf4 =
759 (WORD8 *)&ptr_sbr_enc_scr
760 ->sbr_env_i_buf[IXHEAACE_QMF_TIME_SLOTS * IXHEAACE_QMF_CHANNELS + aacenc_blocksize];
761 }
762
ixheaace_sbr_set_scratch_ptr(ixheaace_pstr_sbr_enc pstr_env_enc,VOID * ptr_scr)763 VOID ixheaace_sbr_set_scratch_ptr(ixheaace_pstr_sbr_enc pstr_env_enc, VOID *ptr_scr) {
764 pstr_env_enc->ptr_sbr_enc_scr = ptr_scr;
765 }
766
ixheaace_sbr_enc_pers_size(WORD32 num_ch,WORD32 use_ps,WORD32 harmonic_sbr)767 WORD32 ixheaace_sbr_enc_pers_size(WORD32 num_ch, WORD32 use_ps, WORD32 harmonic_sbr) {
768 WORD32 num_bytes;
769 num_bytes = IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_sbr_enc), BYTE_ALIGN_8);
770 num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_enc_channel), BYTE_ALIGN_8) *
771 num_ch);
772 num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_pvc_enc), BYTE_ALIGN_8);
773 num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(2 * sizeof(FLOAT32) * QMF_FILTER_LENGTH, BYTE_ALIGN_8) *
774 num_ch);
775 if (1 == harmonic_sbr) {
776 num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_hbe_enc), BYTE_ALIGN_8) * num_ch);
777 num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_esbr_hbe_txposer), BYTE_ALIGN_8) *
778 num_ch);
779 num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(IXHEAACE_MAX_HBE_PERSISTENT_SIZE, BYTE_ALIGN_8) *
780 num_ch);
781 num_bytes += IXHEAAC_GET_SIZE_ALIGNED(ESBR_RESAMP_SAMPLES * sizeof(FLOAT32), BYTE_ALIGN_8);
782 }
783 num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * 5 * NO_OF_ESTIMATES *
784 MAXIMUM_FREQ_COEFFS, BYTE_ALIGN_8) * num_ch);
785
786 num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * MAX_QMF_TIME_SLOTS *
787 IXHEAACE_QMF_CHANNELS, BYTE_ALIGN_8) * num_ch);
788
789 if (use_ps) {
790 num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_enc_channel), BYTE_ALIGN_8);
791 num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_ps_enc), BYTE_ALIGN_8);
792 num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * QMF_FILTER_LENGTH, BYTE_ALIGN_8);
793
794 num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(WORD32) * 5 * NO_OF_ESTIMATES *
795 MAXIMUM_FREQ_COEFFS, BYTE_ALIGN_8);
796
797 /*shared between spectral_band_replication_envYBuffer_fix and IIC IDD PS data buffers*/
798 num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(WORD32) * IXHEAACE_QMF_TIME_SLOTS *
799 IXHEAACE_QMF_CHANNELS, BYTE_ALIGN_8);
800 }
801 num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_sbr_qmf_filter_bank), BYTE_ALIGN_8);
802 return num_bytes;
803 }
804
ia_enhaacplus_enc_sbr_set_persist_buf(WORD8 * ptr_base,WORD32 num_ch,WORD32 use_ps,WORD32 harmonic_sbr)805 VOID ia_enhaacplus_enc_sbr_set_persist_buf(WORD8 *ptr_base, WORD32 num_ch, WORD32 use_ps,
806 WORD32 harmonic_sbr) {
807 struct ixheaace_str_sbr_enc *pstr_env_enc;
808 WORD8 *ptr_curr_mem = ptr_base +
809 IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_sbr_enc), BYTE_ALIGN_8);
810 WORD32 i;
811
812 pstr_env_enc = (struct ixheaace_str_sbr_enc *)ptr_base;
813
814 for (i = 0; i < num_ch; i++) {
815 pstr_env_enc->pstr_env_channel[i] = (struct ixheaace_str_enc_channel *)(ptr_curr_mem);
816 ptr_curr_mem = ptr_curr_mem +
817 IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_enc_channel), BYTE_ALIGN_8);
818 }
819
820 for (i = 0; i < num_ch; i++) {
821 pstr_env_enc->pstr_env_channel[i]->str_sbr_qmf.ptr_sbr_qmf_states_ana =
822 (FLOAT32 *)ptr_curr_mem;
823 ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(
824 sizeof(pstr_env_enc->pstr_env_channel[i]->str_sbr_qmf.ptr_sbr_qmf_states_ana[0]) *
825 QMF_FILTER_LENGTH, BYTE_ALIGN_8);
826 }
827 if (!use_ps) {
828 pstr_env_enc->ptr_common_buffer1 = (WORD32 *)ptr_curr_mem;
829 ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(sizeof(pstr_env_enc->ptr_common_buffer1[0]) *
830 num_ch * 5 * NO_OF_ESTIMATES * MAXIMUM_FREQ_COEFFS, BYTE_ALIGN_8);
831
832 pstr_env_enc->ptr_common_buffer2 = (WORD32 *)ptr_curr_mem;
833 ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(sizeof(pstr_env_enc->ptr_common_buffer2[0]) *
834 num_ch * MAX_QMF_TIME_SLOTS * IXHEAACE_QMF_CHANNELS, BYTE_ALIGN_8);
835 } else {
836 pstr_env_enc->ptr_common_buffer1 = (WORD32 *)ptr_curr_mem;
837 ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(2 * sizeof(pstr_env_enc->ptr_common_buffer1[0]) *
838 5 * NO_OF_ESTIMATES * MAXIMUM_FREQ_COEFFS, BYTE_ALIGN_8);
839
840 pstr_env_enc->ptr_common_buffer2 = (WORD32 *)ptr_curr_mem;
841 ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(2 * sizeof(pstr_env_enc->ptr_common_buffer2[0]) *
842 IXHEAACE_QMF_TIME_SLOTS * IXHEAACE_QMF_CHANNELS, BYTE_ALIGN_8);
843 }
844 // PVC encoder
845 pstr_env_enc->pstr_pvc_enc = (ixheaace_pvc_enc *)ptr_curr_mem;
846 ptr_curr_mem = ptr_curr_mem + IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_pvc_enc), BYTE_ALIGN_8);
847 // Harmonic SBR
848 if (1 == harmonic_sbr) {
849 for (i = 0; i < num_ch; i++) {
850 pstr_env_enc->pstr_env_channel[i]->pstr_hbe_enc = (ixheaace_str_hbe_enc *)ptr_curr_mem;
851 ptr_curr_mem = ptr_curr_mem +
852 IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_hbe_enc), BYTE_ALIGN_8);
853 pstr_env_enc->pstr_env_channel[i]->pstr_hbe_enc->pstr_hbe_txposer =
854 (ixheaace_str_esbr_hbe_txposer *)ptr_curr_mem;
855 ptr_curr_mem = ptr_curr_mem +
856 IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_esbr_hbe_txposer), BYTE_ALIGN_8);
857 pstr_env_enc->pstr_env_channel[i]->pstr_hbe_enc->ptr_hbe_txposer_buffers =
858 (VOID *)ptr_curr_mem;
859 ptr_curr_mem = ptr_curr_mem + IXHEAACE_MAX_HBE_PERSISTENT_SIZE;
860 }
861 pstr_env_enc->ptr_hbe_resample_buf = (FLOAT32 *)ptr_curr_mem;
862 ptr_curr_mem = ptr_curr_mem + IXHEAAC_GET_SIZE_ALIGNED(
863 (ESBR_RESAMP_SAMPLES * sizeof(pstr_env_enc->ptr_hbe_resample_buf[0])), BYTE_ALIGN_8);
864 }
865 if (use_ps) {
866 pstr_env_enc->pstr_env_channel[1] = (struct ixheaace_str_enc_channel *)(ptr_curr_mem);
867 ptr_curr_mem = ptr_curr_mem +
868 IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_enc_channel), BYTE_ALIGN_8);
869 memset(pstr_env_enc->pstr_env_channel[1], 0, sizeof(struct ixheaace_str_enc_channel));
870
871 pstr_env_enc->pstr_env_channel[1]->str_sbr_qmf.ptr_sbr_qmf_states_ana =
872 (FLOAT32 *)ptr_curr_mem;
873 ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(
874 sizeof(pstr_env_enc->pstr_env_channel[1]->str_sbr_qmf.ptr_sbr_qmf_states_ana[0]) *
875 QMF_FILTER_LENGTH, BYTE_ALIGN_8);
876 memset(pstr_env_enc->pstr_env_channel[1]->str_sbr_qmf.ptr_sbr_qmf_states_ana, 0,
877 sizeof(pstr_env_enc->pstr_env_channel[1]->str_sbr_qmf.ptr_sbr_qmf_states_ana[0]) *
878 QMF_FILTER_LENGTH);
879
880 pstr_env_enc->pstr_ps_enc = (struct ixheaace_ps_enc *)(ptr_curr_mem);
881 ptr_curr_mem = ptr_curr_mem +
882 IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_ps_enc), BYTE_ALIGN_8);
883 memset(pstr_env_enc->pstr_ps_enc, 0, sizeof(struct ixheaace_ps_enc));
884 }
885 pstr_env_enc->pstr_synthesis_qmf_bank = (ixheaace_str_sbr_qmf_filter_bank *)(ptr_curr_mem);
886 memset(pstr_env_enc->pstr_synthesis_qmf_bank, 0, sizeof(ixheaace_str_sbr_qmf_filter_bank));
887
888 pstr_env_enc->pstr_synthesis_qmf_bank->ptr_sbr_qmf_states_ana = NULL;
889 }
890
891 IA_ERRORCODE
ixheaace_env_open(ixheaace_pstr_sbr_enc * pstr_env_encoder,ixheaace_pstr_sbr_cfg params,WORD32 * ptr_core_bw,WORD8 * ptr_sbr_scratch,ixheaace_str_sbr_tabs * pstr_sbr_tab,ixheaace_pstr_sbr_hdr_data * pstr_sbr_config)892 ixheaace_env_open(ixheaace_pstr_sbr_enc *pstr_env_encoder, ixheaace_pstr_sbr_cfg params,
893 WORD32 *ptr_core_bw, WORD8 *ptr_sbr_scratch,
894 ixheaace_str_sbr_tabs *pstr_sbr_tab,
895 ixheaace_pstr_sbr_hdr_data *pstr_sbr_config) {
896 ixheaace_pstr_sbr_enc pstr_env_enc;
897 WORD32 ch;
898 IA_ERRORCODE err_code = IA_NO_ERROR;
899
900 pstr_env_enc = *pstr_env_encoder;
901
902 memset(pstr_env_enc, 0, sizeof(struct ixheaace_str_sbr_enc));
903
904 *pstr_sbr_config = &pstr_env_enc->str_sbr_hdr;
905
906 ixheaace_sbr_set_scratch_ptr(pstr_env_enc, ptr_sbr_scratch);
907
908 ia_enhaacplus_enc_sbr_set_persist_buf((WORD8 *)pstr_env_enc,
909 params->codec_settings.num_channels, params->use_ps,
910 params->sbr_harmonic);
911
912 if ((params->codec_settings.num_channels < 1) ||
913 (params->codec_settings.num_channels > IXHEAACE_MAX_CH_IN_BS_ELE)) {
914 return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_NUM_CHANNELS;
915 }
916
917 pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[LO] =
918 pstr_env_enc->str_sbr_cfg.sbr_freq_band_tab_lo;
919
920 memset(
921 pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[LO], 0,
922 sizeof(pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[LO][0]) * (MAXIMUM_FREQ_COEFFS / 2 + 1));
923
924 pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[HI] =
925 pstr_env_enc->str_sbr_cfg.sbr_freq_band_tab_hi;
926
927 memset(pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[HI], 0,
928 sizeof(pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[HI][0]) * (MAXIMUM_FREQ_COEFFS + 1));
929
930 pstr_env_enc->str_sbr_cfg.ptr_v_k_master = pstr_env_enc->str_sbr_cfg.sbr_v_k_master;
931
932 memset(pstr_env_enc->str_sbr_cfg.ptr_v_k_master, 0,
933 sizeof(pstr_env_enc->str_sbr_cfg.ptr_v_k_master[0]) * (MAXIMUM_FREQ_COEFFS + 1));
934
935 ia_enhaacplus_enc_create_bitbuffer(&pstr_env_enc->str_cmon_data.str_sbr_bit_buf,
936 (UWORD8 *)pstr_env_enc->sbr_payload,
937 sizeof(pstr_env_enc->sbr_payload));
938
939 pstr_env_enc->str_cmon_data.prev_bit_buf_read_offset = 0;
940 pstr_env_enc->str_cmon_data.prev_bit_buf_write_offset = 0;
941
942 ia_enhaacplus_enc_create_bitbuffer(&pstr_env_enc->str_cmon_data.str_sbr_bit_buf_prev,
943 (UWORD8 *)pstr_env_enc->sbr_payload_prev,
944 sizeof(pstr_env_enc->sbr_payload));
945
946 pstr_env_enc->str_cmon_data.str_sbr_bit_buf_prev.ptr_read_next =
947 pstr_env_enc->str_cmon_data.str_sbr_bit_buf_prev.ptr_bit_buf_base +
948 pstr_env_enc->str_cmon_data.prev_bit_buf_read_offset;
949 pstr_env_enc->str_cmon_data.str_sbr_bit_buf_prev.ptr_write_next =
950 pstr_env_enc->str_cmon_data.str_sbr_bit_buf_prev.ptr_bit_buf_base +
951 pstr_env_enc->str_cmon_data.prev_bit_buf_write_offset;
952
953 pstr_env_enc->str_sbr_cfg.num_ch = params->codec_settings.num_channels;
954 pstr_env_enc->str_sbr_cfg.is_ld_sbr = params->is_ld_sbr;
955 pstr_env_enc->str_sbr_cfg.sbr_codec = params->sbr_codec;
956 pstr_env_enc->str_sbr_cfg.is_esbr = params->is_esbr;
957 if (pstr_env_enc->str_sbr_cfg.sbr_codec == USAC_SBR) {
958 pstr_env_enc->str_sbr_cfg.sbr_ratio_idx = params->sbr_ratio_idx;
959 }
960 pstr_env_enc->str_sbr_cfg.stereo_mode =
961 (params->codec_settings.num_channels == 2) ? params->stereo_mode : IXHEAACE_SBR_MODE_MONO;
962
963 if (params->codec_settings.sample_freq <= 24000) {
964 pstr_env_enc->str_sbr_hdr.sample_rate_mode = IXHEAACE_DUAL_RATE;
965 if (params->sbr_codec == USAC_SBR) {
966 pstr_env_enc->str_sbr_cfg.sample_freq = 2 * params->codec_settings.sample_freq;
967 if (USAC_SBR_RATIO_INDEX_4_1 == params->sbr_ratio_idx) {
968 pstr_env_enc->str_sbr_cfg.sample_freq = 4 * params->codec_settings.sample_freq;
969 pstr_env_enc->str_sbr_hdr.sample_rate_mode = IXHEAACE_QUAD_RATE;
970 }
971 } else {
972 pstr_env_enc->str_sbr_cfg.sample_freq = 2 * params->codec_settings.sample_freq;
973 }
974 } else {
975 pstr_env_enc->str_sbr_hdr.sample_rate_mode = IXHEAACE_SINGLE_RATE;
976 pstr_env_enc->str_sbr_cfg.sample_freq = params->codec_settings.sample_freq;
977 }
978 if (params->is_ld_sbr) {
979 pstr_env_enc->str_sbr_bs.count_send_header_data = -1;
980 } else {
981 pstr_env_enc->str_sbr_bs.count_send_header_data = 0;
982 }
983 if (params->send_header_data_time > 0) {
984 pstr_env_enc->str_sbr_bs.nr_send_header_data = (WORD32)(
985 params->send_header_data_time * 0.001 * pstr_env_enc->str_sbr_cfg.sample_freq / 2048);
986
987 pstr_env_enc->str_sbr_bs.nr_send_header_data =
988 ixheaac_max32(pstr_env_enc->str_sbr_bs.nr_send_header_data, 1);
989 } else {
990 pstr_env_enc->str_sbr_bs.nr_send_header_data = 0;
991 }
992
993 pstr_env_enc->str_sbr_hdr.sbr_data_extra = params->sbr_data_extra;
994 pstr_env_enc->str_sbr_bs.crc_active = params->crc_sbr;
995 pstr_env_enc->str_sbr_bs.header_active = 0;
996 pstr_env_enc->str_sbr_hdr.sbr_start_freq = params->start_freq;
997 pstr_env_enc->str_sbr_hdr.sbr_stop_freq = params->stop_freq;
998 pstr_env_enc->str_sbr_hdr.sbr_xover_band = 0;
999
1000 if (params->sbr_xpos_ctrl != SBR_XPOS_CTRL_DEFAULT) {
1001 pstr_env_enc->str_sbr_hdr.sbr_data_extra = 1;
1002 }
1003
1004 pstr_env_enc->str_sbr_hdr.protocol_version = SI_SBR_PROTOCOL_VERSION_ID;
1005
1006 pstr_env_enc->str_sbr_hdr.sbr_amp_res = (ixheaace_amp_res)params->amp_res;
1007
1008 pstr_env_enc->str_sbr_hdr.freq_scale = params->freq_scale;
1009 pstr_env_enc->str_sbr_hdr.alter_scale = params->alter_scale;
1010 pstr_env_enc->str_sbr_hdr.sbr_noise_bands = params->sbr_noise_bands;
1011 pstr_env_enc->str_sbr_hdr.header_extra_1 = 0;
1012
1013 if ((params->freq_scale != SBR_FREQ_SCALE_DEFAULT) ||
1014 (params->alter_scale != SBR_ALTER_SCALE_DEFAULT) ||
1015 (params->sbr_noise_bands != SBR_NOISE_BANDS_DEFAULT)) {
1016 pstr_env_enc->str_sbr_hdr.header_extra_1 = 1;
1017 }
1018
1019 pstr_env_enc->str_sbr_hdr.sbr_limiter_bands = params->sbr_limiter_bands;
1020 pstr_env_enc->str_sbr_hdr.sbr_limiter_gains = params->sbr_limiter_gains;
1021 pstr_env_enc->str_sbr_hdr.sbr_interpol_freq = params->sbr_interpol_freq;
1022 pstr_env_enc->str_sbr_hdr.sbr_smoothing_length = params->sbr_smoothing_length;
1023 pstr_env_enc->str_sbr_hdr.header_extra_2 = 0;
1024
1025 if ((params->sbr_limiter_bands != SBR_LIMITER_BANDS_DEFAULT) ||
1026 (params->sbr_limiter_gains != SBR_LIMITER_GAINS_DEFAULT) ||
1027 (params->sbr_interpol_freq != SBR_INTERPOL_FREQ_DEFAULT) ||
1028 (params->sbr_smoothing_length != SBR_SMOOTHING_LENGTH_DEFAULT)) {
1029 pstr_env_enc->str_sbr_hdr.header_extra_2 = 1;
1030 }
1031 pstr_env_enc->str_sbr_hdr.sbr_harmonic = params->sbr_harmonic;
1032 pstr_env_enc->str_sbr_hdr.sbr_pvc_active = params->sbr_pvc_active;
1033 pstr_env_enc->str_sbr_hdr.hq_esbr = params->hq_esbr;
1034
1035 pstr_env_enc->str_sbr_cfg.detect_missing_harmonics = params->detect_missing_harmonics;
1036 pstr_env_enc->str_sbr_cfg.use_parametric_coding = params->parametric_coding;
1037
1038 err_code = ia_enhaacplus_enc_update_freq_band_tab(
1039 &pstr_env_enc->str_sbr_cfg, &pstr_env_enc->str_sbr_hdr, IXHEAACE_QMF_CHANNELS);
1040 if (err_code) {
1041 return err_code;
1042 }
1043 ch = 0;
1044 while (ch < pstr_env_enc->str_sbr_cfg.num_ch) {
1045 FLOAT32 *ptr_sbr_env_r_buf = &pstr_env_enc->ptr_sbr_enc_scr->sbr_env_r_buf[0];
1046 FLOAT32 *ptr_sbr_env_i_buf = &pstr_env_enc->ptr_sbr_enc_scr->sbr_env_i_buf[0];
1047 if (!params->use_ps) {
1048 ptr_sbr_env_r_buf = &pstr_env_enc->ptr_sbr_enc_scr->sbr_env_r_buffer[0];
1049 ptr_sbr_env_i_buf = &pstr_env_enc->ptr_sbr_enc_scr->sbr_env_i_buffer[0];
1050 }
1051
1052 err_code = ixheaace_create_env_channel(
1053 ch, &pstr_env_enc->str_sbr_cfg, &pstr_env_enc->str_sbr_hdr,
1054 pstr_env_enc->pstr_env_channel[ch], params, pstr_env_enc->ptr_common_buffer1,
1055 pstr_env_enc->ptr_common_buffer2, ptr_sbr_env_r_buf, ptr_sbr_env_i_buf, pstr_sbr_tab);
1056 if (err_code) {
1057 return err_code;
1058 }
1059 ch++;
1060 }
1061
1062 if (params->use_ps) {
1063 err_code = ixheaace_create_env_channel(
1064 1, &pstr_env_enc->str_sbr_cfg, &pstr_env_enc->str_sbr_hdr,
1065 pstr_env_enc->pstr_env_channel[1], params, pstr_env_enc->ptr_common_buffer1,
1066 pstr_env_enc->ptr_common_buffer2, pstr_env_enc->ptr_sbr_enc_scr->sbr_env_r_buf,
1067 pstr_env_enc->ptr_sbr_enc_scr->sbr_env_i_buf, pstr_sbr_tab);
1068 if (err_code) {
1069 return err_code;
1070 }
1071
1072 ixheaace_create_synthesis_qmf_bank(pstr_env_enc->pstr_synthesis_qmf_bank,
1073 pstr_env_enc->ptr_common_buffer1, pstr_sbr_tab);
1074
1075 err_code = ixheaace_create_ps_enc(pstr_env_enc->pstr_ps_enc, params->ps_mode,
1076 (FLOAT32 *)pstr_env_enc->ptr_common_buffer1,
1077 (FLOAT32 *)pstr_env_enc->ptr_common_buffer2,
1078 (FLOAT32 *)pstr_env_enc->ptr_sbr_enc_scr->ps_buf3);
1079 if (err_code) {
1080 return err_code;
1081 }
1082 }
1083
1084 pstr_env_enc->str_cmon_data.sbr_num_channels = pstr_env_enc->str_sbr_cfg.num_ch;
1085 if (USAC_SBR == params->sbr_codec) {
1086 ixheaace_pvc_enc_init(pstr_env_enc->pstr_pvc_enc, params->sbr_pvc_rate);
1087 }
1088 *pstr_env_encoder = pstr_env_enc;
1089 *ptr_core_bw = pstr_env_enc->str_sbr_cfg.xover_freq;
1090
1091 return err_code;
1092 }
1093