• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *                                                                            *
3  * Copyright (C) 2023 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19  */
20 
21 #include <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,WORD32 is_esbr_4_1)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, WORD32 is_esbr_4_1) {
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   if (is_esbr_4_1) {
543     pstr_config->start_freq = 10;
544     pstr_config->stop_freq = 11;
545   }
546 }
547 
ixheaace_initialize_sbr_defaults(ixheaace_pstr_sbr_cfg pstr_config)548 VOID ixheaace_initialize_sbr_defaults(ixheaace_pstr_sbr_cfg pstr_config) {
549   pstr_config->send_header_data_time = 500;
550   pstr_config->crc_sbr = 0;
551   pstr_config->tran_thr = 13000;
552   pstr_config->detect_missing_harmonics = 1;
553   pstr_config->parametric_coding = 1;
554   pstr_config->use_speech_config = 0;
555 
556   pstr_config->sbr_data_extra = 0;
557   pstr_config->amp_res = IXHEAACE_SBR_AMP_RES_3_0;
558   pstr_config->tran_fc = 0;
559   pstr_config->tran_det_mode = 1;
560   pstr_config->spread = 1;
561   pstr_config->stat = 0;
562   pstr_config->e = 1;
563   pstr_config->delta_t_across_frames = 1;
564   pstr_config->df_edge_1st_env = 0.3f;
565   pstr_config->df_edge_incr = 0.3f;
566 
567   pstr_config->sbr_invf_mode = IXHEAACE_INVF_SWITCHED;
568   pstr_config->sbr_xpos_mode = IXHEAACE_XPOS_LC;
569   pstr_config->sbr_xpos_ctrl = SBR_XPOS_CTRL_DEFAULT;
570   pstr_config->sbr_xpos_lvl = 0;
571 
572   pstr_config->use_ps = 0;
573   pstr_config->ps_mode = -1;
574 
575   pstr_config->stereo_mode = IXHEAACE_SBR_MODE_SWITCH_LRC;
576   pstr_config->ana_max_level = 6;
577   pstr_config->noise_floor_offset = 0;
578   pstr_config->start_freq = 5;
579   pstr_config->stop_freq = 9;
580 
581   pstr_config->freq_scale = SBR_FREQ_SCALE_DEFAULT;
582   pstr_config->alter_scale = SBR_ALTER_SCALE_DEFAULT;
583   pstr_config->sbr_noise_bands = SBR_NOISE_BANDS_DEFAULT;
584 
585   pstr_config->sbr_limiter_bands = SBR_LIMITER_BANDS_DEFAULT;
586   pstr_config->sbr_limiter_gains = SBR_LIMITER_GAINS_DEFAULT;
587   pstr_config->sbr_interpol_freq = SBR_INTERPOL_FREQ_DEFAULT;
588   pstr_config->sbr_smoothing_length = SBR_SMOOTHING_LENGTH_DEFAULT;
589   pstr_config->is_ld_sbr = 0;
590   pstr_config->is_esbr = 0;
591   pstr_config->frame_flag_960 = 0;
592   pstr_config->frame_flag_480 = 0;
593   pstr_config->hq_esbr = 0;
594   pstr_config->sbr_pvc_active = 0;
595   pstr_config->sbr_harmonic = 0;
596   pstr_config->sbr_ratio_idx = 0;  // NO_SBR
597   pstr_config->use_low_freq_res = 0;
598 }
599 
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)600 static IA_ERRORCODE ia_enhaacplus_enc_update_freq_band_tab(
601     ixheaace_pstr_sbr_config_data pstr_sbr_cfg, ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,
602     WORD32 num_qmf_ch) {
603   IA_ERRORCODE err_code = IA_NO_ERROR;
604   WORD32 k0, k2;
605   WORD32 samp_freq = pstr_sbr_cfg->sample_freq;
606   if ((pstr_sbr_cfg->sbr_codec == USAC_SBR) &&
607       (pstr_sbr_cfg->sbr_ratio_idx == USAC_SBR_RATIO_INDEX_4_1)) {
608     samp_freq = samp_freq / 2;
609   }
610   err_code = ixheaace_find_start_and_stop_band(
611       samp_freq, num_qmf_ch, pstr_sbr_hdr->sbr_start_freq, pstr_sbr_hdr->sbr_stop_freq,
612       pstr_sbr_hdr->sample_rate_mode, &k0, &k2, pstr_sbr_cfg->sbr_ratio_idx,
613       pstr_sbr_cfg->sbr_codec);
614   if (err_code) {
615     return err_code;
616   }
617 
618   err_code = ixheaace_update_freq_scale(
619       pstr_sbr_cfg->ptr_v_k_master, &pstr_sbr_cfg->num_master, k0, k2, pstr_sbr_hdr->freq_scale,
620       pstr_sbr_hdr->alter_scale, pstr_sbr_hdr->sample_rate_mode);
621   if (err_code) {
622     return err_code;
623   }
624 
625   pstr_sbr_hdr->sbr_xover_band = 0;
626 
627   ixheaace_update_high_res(pstr_sbr_cfg->ptr_freq_band_tab[HI], &pstr_sbr_cfg->num_scf[HI],
628                            pstr_sbr_cfg->ptr_v_k_master, pstr_sbr_cfg->num_master,
629                            &pstr_sbr_hdr->sbr_xover_band, pstr_sbr_hdr->sample_rate_mode,
630                            num_qmf_ch);
631 
632   ixheaace_update_low_res(pstr_sbr_cfg->ptr_freq_band_tab[LO], &pstr_sbr_cfg->num_scf[LO],
633                           pstr_sbr_cfg->ptr_freq_band_tab[HI], pstr_sbr_cfg->num_scf[HI]);
634 
635   pstr_sbr_cfg->xover_freq =
636       (pstr_sbr_cfg->ptr_freq_band_tab[LOW_RES][0] * pstr_sbr_cfg->sample_freq / num_qmf_ch +
637        1) >>
638       1;
639 
640   return err_code;
641 }
642 
ia_enhaacplus_enc_init_sbr_tabs(ixheaace_str_sbr_tabs * pstr_sbr_tabs)643 VOID ia_enhaacplus_enc_init_sbr_tabs(ixheaace_str_sbr_tabs *pstr_sbr_tabs) {
644   pstr_sbr_tabs->ptr_ps_tab = (ixheaace_str_ps_tab *)&ia_enhaacplus_enc_ps_tab;
645   pstr_sbr_tabs->ptr_qmf_tab = (ixheaace_str_qmf_tabs *)&ixheaace_qmf_tab;
646   pstr_sbr_tabs->ptr_sbr_huff_tab = (ixheaace_str_sbr_huff_tabs *)&ixheaace_sbr_huff_tab;
647   pstr_sbr_tabs->ptr_resamp_tab =
648       (ixheaace_resampler_table *)&ixheaace_resamp_2_to_1_iir_filt_params;
649   pstr_sbr_tabs->ptr_esbr_sfb_tab = (ixheaace_str_esbr_sfb_bin_tabs *)&ia_esbr_sfb_bin_tabs;
650 }
651 
652 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)653 ixheaace_env_encode_frame(ixheaace_pstr_sbr_enc pstr_env_encoder, FLOAT32 *ptr_samples,
654                           FLOAT32 *ptr_core_buffer, UWORD32 time_sn_stride,
655                           UWORD8 *ptr_num_anc_bytes, UWORD8 *ptr_anc_data,
656                           ixheaace_str_sbr_tabs *pstr_sbr_tab,
657                           ixheaace_comm_tables *pstr_common_tab, UWORD8 *ptr_mps_data,
658                           WORD32 mps_bits, WORD32 flag_fl_small, WORD32 *usac_stat_bits) {
659   IA_ERRORCODE err_code = IA_NO_ERROR;
660 
661   if (pstr_env_encoder != NULL) {
662     ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs = &pstr_env_encoder->str_sbr_bs;
663 
664     pstr_sbr_bs->header_active = 0;
665     if ((pstr_env_encoder->str_sbr_cfg.is_ld_sbr) &&
666         (pstr_sbr_bs->count_send_header_data == pstr_sbr_bs->nr_send_header_data - 1)) {
667       pstr_sbr_bs->header_active = 1;
668     }
669 
670     if (pstr_sbr_bs->count_send_header_data == 0) {
671       pstr_sbr_bs->header_active = 1;
672     }
673 
674     if (pstr_sbr_bs->nr_send_header_data == 0) {
675       pstr_sbr_bs->count_send_header_data = 1;
676     } else {
677       if (pstr_env_encoder->str_sbr_cfg.is_ld_sbr) {
678         if (pstr_sbr_bs->count_send_header_data >= 0) {
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       } else {
684         pstr_sbr_bs->count_send_header_data++;
685 
686         pstr_sbr_bs->count_send_header_data %= pstr_sbr_bs->nr_send_header_data;
687       }
688     }
689 
690     ixheaace_init_sbr_bitstream(
691         &pstr_env_encoder->str_cmon_data, (UWORD8 *)pstr_env_encoder->sbr_payload,
692         sizeof(pstr_env_encoder->sbr_payload), pstr_env_encoder->str_sbr_bs.crc_active,
693         pstr_env_encoder->str_sbr_cfg.sbr_codec);
694 
695     err_code = ixheaace_extract_sbr_envelope(ptr_samples, ptr_core_buffer, time_sn_stride,
696                                              pstr_env_encoder, pstr_sbr_tab, pstr_common_tab,
697                                              flag_fl_small);
698     if (err_code) {
699       return err_code;
700     }
701 
702     if (mps_bits) {
703       WORD32 num_bytes;
704       if (pstr_env_encoder->str_sbr_cfg.sbr_codec == ELD_SBR) {
705         ixheaace_write_bits(&pstr_env_encoder->str_cmon_data.str_sbr_bit_buf,
706                             IXHEAACE_MPS_EXT_LDSAC_DATA, 4);
707         ixheaace_write_bits(&pstr_env_encoder->str_cmon_data.str_sbr_bit_buf, *ptr_mps_data++, 4);
708       }
709       num_bytes = mps_bits >> 3;
710       for (WORD32 k = 0; k < num_bytes; k++) {
711         ixheaace_write_bits(&pstr_env_encoder->str_cmon_data.str_sbr_bit_buf, *ptr_mps_data++, 8);
712       }
713       if (mps_bits & 0x7) {
714         ixheaace_write_bits(&pstr_env_encoder->str_cmon_data.str_sbr_bit_buf,
715                             (*ptr_mps_data++) >> (8 - (mps_bits & 0x7)), mps_bits & 0x7);
716       }
717     }
718 
719     ixheaace_assemble_sbr_bitstream(&pstr_env_encoder->str_cmon_data,
720                                     pstr_env_encoder->str_sbr_cfg.sbr_codec);
721 
722     pstr_env_encoder->sbr_payload_size =
723         ((ia_enhaacplus_enc_get_bits_available(&pstr_env_encoder->str_cmon_data.str_sbr_bit_buf) +
724           7) /
725          8);
726 
727     if (pstr_env_encoder->sbr_payload_size > IXHEAACE_MAX_PAYLOAD_SIZE) {
728       pstr_env_encoder->sbr_payload_size = 0;
729     }
730 
731     if (ptr_anc_data) {
732       *ptr_num_anc_bytes = (UWORD8)pstr_env_encoder->sbr_payload_size;
733       memcpy(ptr_anc_data, pstr_env_encoder->sbr_payload, pstr_env_encoder->sbr_payload_size);
734     }
735     if (usac_stat_bits) {
736       *usac_stat_bits = pstr_env_encoder->str_cmon_data.sbr_hdr_bits +
737                         pstr_env_encoder->str_cmon_data.sbr_data_bits;
738     }
739   }
740   return err_code;
741 }
742 
ixheaace_sbr_enc_scr_size(VOID)743 WORD32 ixheaace_sbr_enc_scr_size(VOID) {
744   return IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_sbr_enc_scratch), BYTE_ALIGN_8);
745 }
746 
ia_enhaacplus_enc_get_scratch_bufs(VOID * ptr_scr,FLOAT32 ** ptr_shared_buf1,FLOAT32 ** ptr_shared_buf2)747 VOID ia_enhaacplus_enc_get_scratch_bufs(VOID *ptr_scr, FLOAT32 **ptr_shared_buf1,
748                                         FLOAT32 **ptr_shared_buf2) {
749   ixheaace_str_sbr_enc_scratch *ptr_sbr_enc_scr = (ixheaace_str_sbr_enc_scratch *)ptr_scr;
750 
751   *ptr_shared_buf1 = ptr_sbr_enc_scr->sbr_env_r_buf;
752   *ptr_shared_buf2 = ptr_sbr_enc_scr->sbr_env_i_buf;
753 }
754 
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)755 VOID ia_enhaacplus_enc_get_shared_bufs(VOID *ptr_scr, WORD32 **ptr_shared_buf1,
756                                        WORD32 **ptr_shared_buf2, WORD32 **ptr_shared_buf3,
757                                        WORD8 **ptr_shared_buf4, WORD32 aacenc_blocksize) {
758   ixheaace_str_sbr_enc_scratch *ptr_sbr_enc_scr = (ixheaace_str_sbr_enc_scratch *)ptr_scr;
759 
760   *ptr_shared_buf1 = (WORD32 *)ptr_sbr_enc_scr->ps_buf3;
761   *ptr_shared_buf2 = (WORD32 *)ptr_sbr_enc_scr->sbr_env_r_buf;
762   *ptr_shared_buf3 = (WORD32 *)ptr_sbr_enc_scr->sbr_env_i_buf;
763   *ptr_shared_buf4 =
764       (WORD8 *)&ptr_sbr_enc_scr
765           ->sbr_env_i_buf[IXHEAACE_QMF_TIME_SLOTS * IXHEAACE_QMF_CHANNELS + aacenc_blocksize];
766 }
767 
ixheaace_sbr_set_scratch_ptr(ixheaace_pstr_sbr_enc pstr_env_enc,VOID * ptr_scr)768 VOID ixheaace_sbr_set_scratch_ptr(ixheaace_pstr_sbr_enc pstr_env_enc, VOID *ptr_scr) {
769   pstr_env_enc->ptr_sbr_enc_scr = ptr_scr;
770 }
771 
ixheaace_sbr_enc_pers_size(WORD32 num_ch,WORD32 use_ps,WORD32 harmonic_sbr)772 WORD32 ixheaace_sbr_enc_pers_size(WORD32 num_ch, WORD32 use_ps, WORD32 harmonic_sbr) {
773   WORD32 num_bytes;
774   num_bytes = IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_sbr_enc), BYTE_ALIGN_8);
775   num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_enc_channel), BYTE_ALIGN_8) *
776                 num_ch);
777   num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_pvc_enc), BYTE_ALIGN_8);
778   num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(2 * sizeof(FLOAT32) * QMF_FILTER_LENGTH, BYTE_ALIGN_8) *
779                num_ch);
780   if (1 == harmonic_sbr) {
781     num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_hbe_enc), BYTE_ALIGN_8) * num_ch);
782     num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_esbr_hbe_txposer), BYTE_ALIGN_8) *
783                   num_ch);
784     num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(IXHEAACE_MAX_HBE_PERSISTENT_SIZE, BYTE_ALIGN_8) *
785                   num_ch);
786     num_bytes += IXHEAAC_GET_SIZE_ALIGNED(ESBR_RESAMP_SAMPLES * sizeof(FLOAT32), BYTE_ALIGN_8);
787   }
788   num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * 5 * NO_OF_ESTIMATES *
789                 MAXIMUM_FREQ_COEFFS, BYTE_ALIGN_8) * num_ch);
790 
791   num_bytes += (IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * MAX_QMF_TIME_SLOTS *
792                 IXHEAACE_QMF_CHANNELS, BYTE_ALIGN_8) * num_ch);
793 
794   if (use_ps) {
795     num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_enc_channel), BYTE_ALIGN_8);
796     num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_ps_enc), BYTE_ALIGN_8);
797     num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * QMF_FILTER_LENGTH, BYTE_ALIGN_8);
798 
799     num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(WORD32) * 5 * NO_OF_ESTIMATES *
800                  MAXIMUM_FREQ_COEFFS, BYTE_ALIGN_8);
801 
802     /*shared between spectral_band_replication_envYBuffer_fix and IIC IDD PS data buffers*/
803     num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(WORD32) * IXHEAACE_QMF_TIME_SLOTS *
804                  IXHEAACE_QMF_CHANNELS, BYTE_ALIGN_8);
805   }
806   num_bytes += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_sbr_qmf_filter_bank), BYTE_ALIGN_8);
807   return num_bytes;
808 }
809 
ia_enhaacplus_enc_sbr_set_persist_buf(WORD8 * ptr_base,WORD32 num_ch,WORD32 use_ps,WORD32 harmonic_sbr)810 VOID ia_enhaacplus_enc_sbr_set_persist_buf(WORD8 *ptr_base, WORD32 num_ch, WORD32 use_ps,
811                                            WORD32 harmonic_sbr) {
812   struct ixheaace_str_sbr_enc *pstr_env_enc;
813   WORD8 *ptr_curr_mem = ptr_base +
814     IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_sbr_enc), BYTE_ALIGN_8);
815   WORD32 i;
816 
817   pstr_env_enc = (struct ixheaace_str_sbr_enc *)ptr_base;
818 
819   for (i = 0; i < num_ch; i++) {
820     pstr_env_enc->pstr_env_channel[i] = (struct ixheaace_str_enc_channel *)(ptr_curr_mem);
821     ptr_curr_mem = ptr_curr_mem +
822       IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_enc_channel), BYTE_ALIGN_8);
823   }
824 
825   for (i = 0; i < num_ch; i++) {
826     pstr_env_enc->pstr_env_channel[i]->str_sbr_qmf.ptr_sbr_qmf_states_ana =
827         (FLOAT32 *)ptr_curr_mem;
828     ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(
829         sizeof(pstr_env_enc->pstr_env_channel[i]->str_sbr_qmf.ptr_sbr_qmf_states_ana[0]) *
830         QMF_FILTER_LENGTH, BYTE_ALIGN_8);
831   }
832   if (!use_ps) {
833     pstr_env_enc->ptr_common_buffer1 = (WORD32 *)ptr_curr_mem;
834     ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(sizeof(pstr_env_enc->ptr_common_buffer1[0]) *
835                     num_ch * 5 * NO_OF_ESTIMATES * MAXIMUM_FREQ_COEFFS, BYTE_ALIGN_8);
836 
837     pstr_env_enc->ptr_common_buffer2 = (WORD32 *)ptr_curr_mem;
838     ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(sizeof(pstr_env_enc->ptr_common_buffer2[0]) *
839                     num_ch * MAX_QMF_TIME_SLOTS * IXHEAACE_QMF_CHANNELS, BYTE_ALIGN_8);
840   } else {
841     pstr_env_enc->ptr_common_buffer1 = (WORD32 *)ptr_curr_mem;
842     ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(2 * sizeof(pstr_env_enc->ptr_common_buffer1[0]) *
843                     5 * NO_OF_ESTIMATES * MAXIMUM_FREQ_COEFFS, BYTE_ALIGN_8);
844 
845     pstr_env_enc->ptr_common_buffer2 = (WORD32 *)ptr_curr_mem;
846     ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(2 * sizeof(pstr_env_enc->ptr_common_buffer2[0]) *
847                     IXHEAACE_QMF_TIME_SLOTS * IXHEAACE_QMF_CHANNELS, BYTE_ALIGN_8);
848   }
849   // PVC encoder
850   pstr_env_enc->pstr_pvc_enc = (ixheaace_pvc_enc *)ptr_curr_mem;
851   ptr_curr_mem = ptr_curr_mem + IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_pvc_enc), BYTE_ALIGN_8);
852   // Harmonic SBR
853   if (1 == harmonic_sbr) {
854     for (i = 0; i < num_ch; i++) {
855       pstr_env_enc->pstr_env_channel[i]->pstr_hbe_enc = (ixheaace_str_hbe_enc *)ptr_curr_mem;
856       ptr_curr_mem = ptr_curr_mem +
857         IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_hbe_enc), BYTE_ALIGN_8);
858       pstr_env_enc->pstr_env_channel[i]->pstr_hbe_enc->pstr_hbe_txposer =
859           (ixheaace_str_esbr_hbe_txposer *)ptr_curr_mem;
860       ptr_curr_mem = ptr_curr_mem +
861         IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_str_esbr_hbe_txposer), BYTE_ALIGN_8);
862       pstr_env_enc->pstr_env_channel[i]->pstr_hbe_enc->ptr_hbe_txposer_buffers =
863           (VOID *)ptr_curr_mem;
864       ptr_curr_mem = ptr_curr_mem + IXHEAACE_MAX_HBE_PERSISTENT_SIZE;
865     }
866     pstr_env_enc->ptr_hbe_resample_buf = (FLOAT32 *)ptr_curr_mem;
867     ptr_curr_mem = ptr_curr_mem + IXHEAAC_GET_SIZE_ALIGNED(
868       (ESBR_RESAMP_SAMPLES * sizeof(pstr_env_enc->ptr_hbe_resample_buf[0])), BYTE_ALIGN_8);
869   }
870   if (use_ps) {
871     pstr_env_enc->pstr_env_channel[1] = (struct ixheaace_str_enc_channel *)(ptr_curr_mem);
872     ptr_curr_mem = ptr_curr_mem +
873       IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_str_enc_channel), BYTE_ALIGN_8);
874     memset(pstr_env_enc->pstr_env_channel[1], 0, sizeof(struct ixheaace_str_enc_channel));
875 
876     pstr_env_enc->pstr_env_channel[1]->str_sbr_qmf.ptr_sbr_qmf_states_ana =
877         (FLOAT32 *)ptr_curr_mem;
878     ptr_curr_mem += IXHEAAC_GET_SIZE_ALIGNED(
879       sizeof(pstr_env_enc->pstr_env_channel[1]->str_sbr_qmf.ptr_sbr_qmf_states_ana[0]) *
880         QMF_FILTER_LENGTH, BYTE_ALIGN_8);
881     memset(pstr_env_enc->pstr_env_channel[1]->str_sbr_qmf.ptr_sbr_qmf_states_ana, 0,
882            sizeof(pstr_env_enc->pstr_env_channel[1]->str_sbr_qmf.ptr_sbr_qmf_states_ana[0]) *
883                QMF_FILTER_LENGTH);
884 
885     pstr_env_enc->pstr_ps_enc = (struct ixheaace_ps_enc *)(ptr_curr_mem);
886     ptr_curr_mem = ptr_curr_mem +
887       IXHEAAC_GET_SIZE_ALIGNED(sizeof(struct ixheaace_ps_enc), BYTE_ALIGN_8);
888     memset(pstr_env_enc->pstr_ps_enc, 0, sizeof(struct ixheaace_ps_enc));
889   }
890   pstr_env_enc->pstr_synthesis_qmf_bank = (ixheaace_str_sbr_qmf_filter_bank *)(ptr_curr_mem);
891   memset(pstr_env_enc->pstr_synthesis_qmf_bank, 0, sizeof(ixheaace_str_sbr_qmf_filter_bank));
892 
893   pstr_env_enc->pstr_synthesis_qmf_bank->ptr_sbr_qmf_states_ana = NULL;
894 }
895 
896 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)897 ixheaace_env_open(ixheaace_pstr_sbr_enc *pstr_env_encoder, ixheaace_pstr_sbr_cfg params,
898                   WORD32 *ptr_core_bw, WORD8 *ptr_sbr_scratch,
899                   ixheaace_str_sbr_tabs *pstr_sbr_tab,
900                   ixheaace_pstr_sbr_hdr_data *pstr_sbr_config) {
901   ixheaace_pstr_sbr_enc pstr_env_enc;
902   WORD32 ch;
903   IA_ERRORCODE err_code = IA_NO_ERROR;
904 
905   pstr_env_enc = *pstr_env_encoder;
906 
907   memset(pstr_env_enc, 0, sizeof(struct ixheaace_str_sbr_enc));
908 
909   *pstr_sbr_config = &pstr_env_enc->str_sbr_hdr;
910 
911   ixheaace_sbr_set_scratch_ptr(pstr_env_enc, ptr_sbr_scratch);
912 
913   ia_enhaacplus_enc_sbr_set_persist_buf((WORD8 *)pstr_env_enc,
914                                         params->codec_settings.num_channels, params->use_ps,
915                                         params->sbr_harmonic);
916 
917   if ((params->codec_settings.num_channels < 1) ||
918       (params->codec_settings.num_channels > IXHEAACE_MAX_CH_IN_BS_ELE)) {
919     return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_NUM_CHANNELS;
920   }
921 
922   pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[LO] =
923       pstr_env_enc->str_sbr_cfg.sbr_freq_band_tab_lo;
924 
925   memset(
926       pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[LO], 0,
927       sizeof(pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[LO][0]) * (MAXIMUM_FREQ_COEFFS / 2 + 1));
928 
929   pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[HI] =
930       pstr_env_enc->str_sbr_cfg.sbr_freq_band_tab_hi;
931 
932   memset(pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[HI], 0,
933          sizeof(pstr_env_enc->str_sbr_cfg.ptr_freq_band_tab[HI][0]) * (MAXIMUM_FREQ_COEFFS + 1));
934 
935   pstr_env_enc->str_sbr_cfg.ptr_v_k_master = pstr_env_enc->str_sbr_cfg.sbr_v_k_master;
936 
937   memset(pstr_env_enc->str_sbr_cfg.ptr_v_k_master, 0,
938          sizeof(pstr_env_enc->str_sbr_cfg.ptr_v_k_master[0]) * (MAXIMUM_FREQ_COEFFS + 1));
939 
940   ia_enhaacplus_enc_create_bitbuffer(&pstr_env_enc->str_cmon_data.str_sbr_bit_buf,
941                                      (UWORD8 *)pstr_env_enc->sbr_payload,
942                                      sizeof(pstr_env_enc->sbr_payload));
943 
944   pstr_env_enc->str_cmon_data.prev_bit_buf_read_offset = 0;
945   pstr_env_enc->str_cmon_data.prev_bit_buf_write_offset = 0;
946 
947   ia_enhaacplus_enc_create_bitbuffer(&pstr_env_enc->str_cmon_data.str_sbr_bit_buf_prev,
948                                      (UWORD8 *)pstr_env_enc->sbr_payload_prev,
949                                      sizeof(pstr_env_enc->sbr_payload));
950 
951   pstr_env_enc->str_cmon_data.str_sbr_bit_buf_prev.ptr_read_next =
952       pstr_env_enc->str_cmon_data.str_sbr_bit_buf_prev.ptr_bit_buf_base +
953       pstr_env_enc->str_cmon_data.prev_bit_buf_read_offset;
954   pstr_env_enc->str_cmon_data.str_sbr_bit_buf_prev.ptr_write_next =
955       pstr_env_enc->str_cmon_data.str_sbr_bit_buf_prev.ptr_bit_buf_base +
956       pstr_env_enc->str_cmon_data.prev_bit_buf_write_offset;
957 
958   pstr_env_enc->str_sbr_cfg.num_ch = params->codec_settings.num_channels;
959   pstr_env_enc->str_sbr_cfg.is_ld_sbr = params->is_ld_sbr;
960   pstr_env_enc->str_sbr_cfg.sbr_codec = params->sbr_codec;
961   pstr_env_enc->str_sbr_cfg.is_esbr = params->is_esbr;
962   if (pstr_env_enc->str_sbr_cfg.sbr_codec == USAC_SBR) {
963     pstr_env_enc->str_sbr_cfg.sbr_ratio_idx = params->sbr_ratio_idx;
964   }
965   pstr_env_enc->str_sbr_cfg.stereo_mode =
966       (params->codec_settings.num_channels == 2) ? params->stereo_mode : IXHEAACE_SBR_MODE_MONO;
967 
968   if (params->codec_settings.sample_freq <= 24000) {
969     pstr_env_enc->str_sbr_hdr.sample_rate_mode = IXHEAACE_DUAL_RATE;
970     if (params->sbr_codec == USAC_SBR) {
971       pstr_env_enc->str_sbr_cfg.sample_freq = 2 * params->codec_settings.sample_freq;
972       if (USAC_SBR_RATIO_INDEX_4_1 == params->sbr_ratio_idx) {
973         pstr_env_enc->str_sbr_cfg.sample_freq = 4 * params->codec_settings.sample_freq;
974         pstr_env_enc->str_sbr_hdr.sample_rate_mode = IXHEAACE_QUAD_RATE;
975       }
976     } else {
977       pstr_env_enc->str_sbr_cfg.sample_freq = 2 * params->codec_settings.sample_freq;
978     }
979   } else {
980     pstr_env_enc->str_sbr_hdr.sample_rate_mode = IXHEAACE_SINGLE_RATE;
981     pstr_env_enc->str_sbr_cfg.sample_freq = params->codec_settings.sample_freq;
982   }
983   if (params->is_ld_sbr) {
984     pstr_env_enc->str_sbr_bs.count_send_header_data = -1;
985   } else {
986     pstr_env_enc->str_sbr_bs.count_send_header_data = 0;
987   }
988   if (params->send_header_data_time > 0) {
989     pstr_env_enc->str_sbr_bs.nr_send_header_data = (WORD32)(
990         params->send_header_data_time * 0.001 * pstr_env_enc->str_sbr_cfg.sample_freq / 2048);
991 
992     pstr_env_enc->str_sbr_bs.nr_send_header_data =
993         ixheaac_max32(pstr_env_enc->str_sbr_bs.nr_send_header_data, 1);
994   } else {
995     pstr_env_enc->str_sbr_bs.nr_send_header_data = 0;
996   }
997 
998   pstr_env_enc->str_sbr_hdr.sbr_data_extra = params->sbr_data_extra;
999   pstr_env_enc->str_sbr_bs.crc_active = params->crc_sbr;
1000   pstr_env_enc->str_sbr_bs.header_active = 0;
1001   pstr_env_enc->str_sbr_hdr.sbr_start_freq = params->start_freq;
1002   pstr_env_enc->str_sbr_hdr.sbr_stop_freq = params->stop_freq;
1003   pstr_env_enc->str_sbr_hdr.sbr_xover_band = 0;
1004 
1005   if (params->sbr_xpos_ctrl != SBR_XPOS_CTRL_DEFAULT) {
1006     pstr_env_enc->str_sbr_hdr.sbr_data_extra = 1;
1007   }
1008 
1009   pstr_env_enc->str_sbr_hdr.protocol_version = SI_SBR_PROTOCOL_VERSION_ID;
1010 
1011   pstr_env_enc->str_sbr_hdr.sbr_amp_res = (ixheaace_amp_res)params->amp_res;
1012 
1013   pstr_env_enc->str_sbr_hdr.freq_scale = params->freq_scale;
1014   pstr_env_enc->str_sbr_hdr.alter_scale = params->alter_scale;
1015   pstr_env_enc->str_sbr_hdr.sbr_noise_bands = params->sbr_noise_bands;
1016   pstr_env_enc->str_sbr_hdr.header_extra_1 = 0;
1017 
1018   if ((params->freq_scale != SBR_FREQ_SCALE_DEFAULT) ||
1019       (params->alter_scale != SBR_ALTER_SCALE_DEFAULT) ||
1020       (params->sbr_noise_bands != SBR_NOISE_BANDS_DEFAULT)) {
1021     pstr_env_enc->str_sbr_hdr.header_extra_1 = 1;
1022   }
1023 
1024   pstr_env_enc->str_sbr_hdr.sbr_limiter_bands = params->sbr_limiter_bands;
1025   pstr_env_enc->str_sbr_hdr.sbr_limiter_gains = params->sbr_limiter_gains;
1026   pstr_env_enc->str_sbr_hdr.sbr_interpol_freq = params->sbr_interpol_freq;
1027   pstr_env_enc->str_sbr_hdr.sbr_smoothing_length = params->sbr_smoothing_length;
1028   pstr_env_enc->str_sbr_hdr.header_extra_2 = 0;
1029 
1030   if ((params->sbr_limiter_bands != SBR_LIMITER_BANDS_DEFAULT) ||
1031       (params->sbr_limiter_gains != SBR_LIMITER_GAINS_DEFAULT) ||
1032       (params->sbr_interpol_freq != SBR_INTERPOL_FREQ_DEFAULT) ||
1033       (params->sbr_smoothing_length != SBR_SMOOTHING_LENGTH_DEFAULT)) {
1034     pstr_env_enc->str_sbr_hdr.header_extra_2 = 1;
1035   }
1036   pstr_env_enc->str_sbr_hdr.sbr_harmonic = params->sbr_harmonic;
1037   pstr_env_enc->str_sbr_hdr.sbr_pvc_active = params->sbr_pvc_active;
1038   pstr_env_enc->str_sbr_hdr.hq_esbr = params->hq_esbr;
1039 
1040   pstr_env_enc->str_sbr_cfg.detect_missing_harmonics = params->detect_missing_harmonics;
1041   pstr_env_enc->str_sbr_cfg.use_parametric_coding = params->parametric_coding;
1042 
1043   err_code = ia_enhaacplus_enc_update_freq_band_tab(
1044       &pstr_env_enc->str_sbr_cfg, &pstr_env_enc->str_sbr_hdr, IXHEAACE_QMF_CHANNELS);
1045   if (err_code) {
1046     return err_code;
1047   }
1048   ch = 0;
1049   while (ch < pstr_env_enc->str_sbr_cfg.num_ch) {
1050     FLOAT32 *ptr_sbr_env_r_buf = &pstr_env_enc->ptr_sbr_enc_scr->sbr_env_r_buf[0];
1051     FLOAT32 *ptr_sbr_env_i_buf = &pstr_env_enc->ptr_sbr_enc_scr->sbr_env_i_buf[0];
1052     if (!params->use_ps) {
1053       ptr_sbr_env_r_buf = &pstr_env_enc->ptr_sbr_enc_scr->sbr_env_r_buffer[0];
1054       ptr_sbr_env_i_buf = &pstr_env_enc->ptr_sbr_enc_scr->sbr_env_i_buffer[0];
1055     }
1056 
1057     err_code = ixheaace_create_env_channel(
1058         ch, &pstr_env_enc->str_sbr_cfg, &pstr_env_enc->str_sbr_hdr,
1059         pstr_env_enc->pstr_env_channel[ch], params, pstr_env_enc->ptr_common_buffer1,
1060         pstr_env_enc->ptr_common_buffer2, ptr_sbr_env_r_buf, ptr_sbr_env_i_buf, pstr_sbr_tab);
1061     if (err_code) {
1062       return err_code;
1063     }
1064     ch++;
1065   }
1066 
1067   if (params->use_ps) {
1068     err_code = ixheaace_create_env_channel(
1069         1, &pstr_env_enc->str_sbr_cfg, &pstr_env_enc->str_sbr_hdr,
1070         pstr_env_enc->pstr_env_channel[1], params, pstr_env_enc->ptr_common_buffer1,
1071         pstr_env_enc->ptr_common_buffer2, pstr_env_enc->ptr_sbr_enc_scr->sbr_env_r_buf,
1072         pstr_env_enc->ptr_sbr_enc_scr->sbr_env_i_buf, pstr_sbr_tab);
1073     if (err_code) {
1074       return err_code;
1075     }
1076 
1077     ixheaace_create_synthesis_qmf_bank(pstr_env_enc->pstr_synthesis_qmf_bank,
1078                                        pstr_env_enc->ptr_common_buffer1, pstr_sbr_tab);
1079 
1080     err_code = ixheaace_create_ps_enc(pstr_env_enc->pstr_ps_enc, params->ps_mode,
1081                                       (FLOAT32 *)pstr_env_enc->ptr_common_buffer1,
1082                                       (FLOAT32 *)pstr_env_enc->ptr_common_buffer2,
1083                                       (FLOAT32 *)pstr_env_enc->ptr_sbr_enc_scr->ps_buf3);
1084     if (err_code) {
1085       return err_code;
1086     }
1087   }
1088 
1089   pstr_env_enc->str_cmon_data.sbr_num_channels = pstr_env_enc->str_sbr_cfg.num_ch;
1090   if (USAC_SBR == params->sbr_codec) {
1091     ixheaace_pvc_enc_init(pstr_env_enc->pstr_pvc_enc, params->sbr_pvc_rate);
1092   }
1093   *pstr_env_encoder = pstr_env_enc;
1094   *ptr_core_bw = pstr_env_enc->str_sbr_cfg.xover_freq;
1095 
1096   return err_code;
1097 }
1098