1 /******************************************************************************
2 * *
3 * Copyright (C) 2018 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 #include <stdlib.h>
21 #include <math.h>
22 #include <string.h>
23
24 #include "ixheaacd_type_def.h"
25 #include "ixheaacd_error_standards.h"
26 #include "ixheaacd_sbr_const.h"
27 #include "ixheaacd_sbrdecsettings.h"
28 #include "ixheaacd_bitbuffer.h"
29 #include "ixheaacd_sbr_common.h"
30 #include "ixheaacd_drc_data_struct.h"
31 #include "ixheaacd_drc_dec.h"
32 #include "ixheaacd_sbrdecoder.h"
33 #include "ixheaacd_bitbuffer.h"
34 #include "ixheaacd_env_extr_part.h"
35 #include "ixheaacd_sbr_rom.h"
36 #include "ixheaacd_common_rom.h"
37 #include "ixheaacd_hybrid.h"
38 #include "ixheaacd_sbr_scale.h"
39 #include "ixheaacd_ps_dec.h"
40 #include "ixheaacd_freq_sca.h"
41 #include "ixheaacd_lpp_tran.h"
42 #include "ixheaacd_env_extr.h"
43
44 #include "ixheaacd_esbr_rom.h"
45
46 #define ABS(A) fabs(A)
47
ixheaacd_shellsort(WORD32 * in,WORD32 n)48 VOID ixheaacd_shellsort(WORD32 *in, WORD32 n) {
49 WORD32 i, j, v;
50 WORD32 inc = 1;
51
52 do
53 inc = 3 * inc + 1;
54 while (inc <= n);
55
56 do {
57 inc = inc / 3;
58 for (i = inc + 1; i <= n; i++) {
59 v = in[i - 1];
60 j = i;
61 while (in[j - inc - 1] > v) {
62 in[j - 1] = in[j - inc - 1];
63 j -= inc;
64 if (j <= inc) break;
65 }
66 in[j - 1] = v;
67 }
68 } while (inc > 1);
69 }
70
ixheaacd_sbr_env_calc(ia_sbr_frame_info_data_struct * frame_data,FLOAT32 input_real[][64],FLOAT32 input_imag[][64],FLOAT32 input_real1[][64],FLOAT32 input_imag1[][64],WORD32 x_over_qmf[MAX_NUM_PATCHES],FLOAT32 * scratch_buff,FLOAT32 * env_out)71 WORD32 ixheaacd_sbr_env_calc(ia_sbr_frame_info_data_struct *frame_data,
72 FLOAT32 input_real[][64], FLOAT32 input_imag[][64],
73 FLOAT32 input_real1[][64],
74 FLOAT32 input_imag1[][64],
75 WORD32 x_over_qmf[MAX_NUM_PATCHES],
76 FLOAT32 *scratch_buff, FLOAT32 *env_out) {
77 WORD8 harmonics[64];
78 FLOAT32(*env_tmp)[48];
79 FLOAT32(*noise_level_pvc)[48];
80 FLOAT32(*nrg_est_pvc)[48];
81 FLOAT32(*nrg_ref_pvc)[48];
82 FLOAT32(*nrg_gain_pvc)[48];
83 FLOAT32(*nrg_tone_pvc)[48];
84
85 WORD32 n, c, li, ui, i, j, k = 0, l, m = 0, kk = 0, o, next = -1, ui2, flag,
86 tmp, noise_absc_flag, smooth_length;
87 WORD32 upsamp_4_flag = frame_data->pstr_sbr_header->is_usf_4;
88
89 FLOAT32 *ptr_real_buf, *ptr_imag_buf, nrg = 0, p_ref, p_est, avg_gain, g_max,
90 p_adj, boost_gain, sb_gain, sb_noise,
91 temp[64];
92
93 WORD32 t;
94 WORD32 start_pos = 0;
95 WORD32 end_pos = 0;
96
97 WORD32 slot_idx;
98
99 FLOAT32 *prev_env_noise_level = frame_data->prev_noise_level;
100 FLOAT32 *nrg_tone = scratch_buff;
101 FLOAT32 *noise_level = scratch_buff + 64;
102 FLOAT32 *nrg_est = scratch_buff + 128;
103 FLOAT32 *nrg_ref = scratch_buff + 192;
104 FLOAT32 *nrg_gain = scratch_buff + 256;
105
106 const FLOAT32 *smooth_filt;
107
108 FLOAT32 *sfb_nrg = frame_data->flt_env_sf_arr;
109 FLOAT32 *noise_floor = frame_data->flt_noise_floor;
110 ia_frame_info_struct *p_frame_info = &frame_data->str_frame_info_details;
111
112 ia_frame_info_struct *pvc_frame_info = &frame_data->str_pvc_frame_info;
113 WORD32 smoothing_length = frame_data->pstr_sbr_header->smoothing_mode ? 0 : 4;
114 WORD32 int_mode = frame_data->pstr_sbr_header->interpol_freq;
115 WORD32 limiter_band = frame_data->pstr_sbr_header->limiter_bands;
116 WORD32 limiter_gains = frame_data->pstr_sbr_header->limiter_gains;
117 WORD32 *add_harmonics = frame_data->add_harmonics;
118 WORD32 sub_band_start =
119 frame_data->pstr_sbr_header->pstr_freq_band_data->sub_band_start;
120 WORD32 sub_band_end =
121 frame_data->pstr_sbr_header->pstr_freq_band_data->sub_band_end;
122 WORD32 reset = frame_data->reset_flag;
123 WORD32 num_subbands = sub_band_end - sub_band_start;
124 WORD32 bs_num_env = p_frame_info->num_env;
125 WORD32 trans_env = p_frame_info->transient_env;
126 WORD32 sbr_mode = frame_data->sbr_mode;
127 WORD32 prev_sbr_mode = frame_data->prev_sbr_mode;
128
129 WORD16 *freq_band_table[2];
130 const WORD16 *num_sf_bands =
131 frame_data->pstr_sbr_header->pstr_freq_band_data->num_sf_bands;
132 WORD16 *freq_band_table_noise =
133 frame_data->pstr_sbr_header->pstr_freq_band_data->freq_band_tbl_noise;
134 WORD32 num_nf_bands =
135 frame_data->pstr_sbr_header->pstr_freq_band_data->num_nf_bands;
136
137 WORD32 harm_index = frame_data->harm_index;
138 WORD32 phase_index = frame_data->phase_index;
139 WORD32 esbr_start_up = frame_data->pstr_sbr_header->esbr_start_up;
140 WORD32 esbr_start_up_pvc = frame_data->pstr_sbr_header->esbr_start_up_pvc;
141 WORD8(*harm_flag_prev)[64] = &frame_data->harm_flag_prev;
142 FLOAT32(*e_gain)[5][64] = &frame_data->e_gain;
143 FLOAT32(*noise_buf)[5][64] = &frame_data->noise_buf;
144 WORD32(*lim_table)[4][12 + 1] = &frame_data->lim_table;
145 WORD32(*gate_mode)[4] = &frame_data->gate_mode;
146 WORD32 freq_inv = 1;
147
148 WORD8(*harm_flag_varlen_prev)[64] = &frame_data->harm_flag_varlen_prev;
149 WORD8(*harm_flag_varlen)[64] = &frame_data->harm_flag_varlen;
150 WORD32 band_loop_end;
151
152 WORD32 rate = upsamp_4_flag ? 4 : 2;
153
154 env_tmp = frame_data->env_tmp;
155 noise_level_pvc = frame_data->noise_level_pvc;
156 nrg_est_pvc = frame_data->nrg_est_pvc;
157 nrg_ref_pvc = frame_data->nrg_ref_pvc;
158 nrg_gain_pvc = frame_data->nrg_gain_pvc;
159 nrg_tone_pvc = frame_data->nrg_tone_pvc;
160
161 freq_band_table[0] =
162 frame_data->pstr_sbr_header->pstr_freq_band_data->freq_band_table[0];
163 freq_band_table[1] =
164 frame_data->pstr_sbr_header->pstr_freq_band_data->freq_band_table[1];
165
166 if (reset) {
167 esbr_start_up = 1;
168 esbr_start_up_pvc = 1;
169 phase_index = 0;
170 if (ixheaacd_createlimiterbands(
171 (*lim_table), (*gate_mode),
172 frame_data->pstr_sbr_header->pstr_freq_band_data->freq_band_tbl_lo,
173 num_sf_bands[LOW], x_over_qmf, frame_data->sbr_patching_mode,
174 upsamp_4_flag, &frame_data->patch_param))
175 return IA_FATAL_ERROR;
176 }
177
178 if (frame_data->sbr_patching_mode != frame_data->prev_sbr_patching_mode) {
179 if (ixheaacd_createlimiterbands(
180 (*lim_table), (*gate_mode),
181 frame_data->pstr_sbr_header->pstr_freq_band_data->freq_band_tbl_lo,
182 num_sf_bands[LOW], x_over_qmf, frame_data->sbr_patching_mode,
183 upsamp_4_flag, &frame_data->patch_param))
184 return IA_FATAL_ERROR;
185
186 frame_data->prev_sbr_patching_mode = frame_data->sbr_patching_mode;
187 }
188
189 memset(harmonics, 0, 64 * sizeof(WORD8));
190
191 if (sbr_mode == PVC_SBR) {
192 for (i = 0; i < num_sf_bands[HIGH]; i++) {
193 li =
194 frame_data->pstr_sbr_header->pstr_freq_band_data->freq_band_tbl_hi[i];
195 ui = frame_data->pstr_sbr_header->pstr_freq_band_data
196 ->freq_band_tbl_hi[i + 1];
197 tmp = ((ui + li) - (sub_band_start << 1)) >> 1;
198 if ((tmp >= 64) || (tmp < 0)) return -1;
199
200 harmonics[tmp] = add_harmonics[i];
201 }
202
203 for (t = 0; t < p_frame_info->border_vec[0]; t++) {
204 for (c = 0; c < 64; c++) {
205 frame_data->qmapped_pvc[c][t] = frame_data->qmapped_pvc[c][t + 16];
206 }
207 }
208
209 for (i = 0; i < bs_num_env; i++) {
210 if (kk > MAX_NOISE_ENVELOPES) return IA_FATAL_ERROR;
211 if (p_frame_info->border_vec[i] == p_frame_info->noise_border_vec[kk])
212 kk++, next++;
213
214 start_pos = p_frame_info->border_vec[i];
215 end_pos = p_frame_info->border_vec[i + 1];
216 if ((start_pos < 0) || (end_pos > MAX_FREQ_COEFFS_SBR))
217 return IA_FATAL_ERROR;
218 for (t = start_pos; t < end_pos; t++) {
219 band_loop_end = num_sf_bands[p_frame_info->freq_res[i]];
220
221 for (c = 0, o = 0, j = 0; j < band_loop_end; j++) {
222 li = freq_band_table[p_frame_info->freq_res[i]][j];
223 ui = freq_band_table[p_frame_info->freq_res[i]][j + 1];
224 ui2 = frame_data->pstr_sbr_header->pstr_freq_band_data
225 ->freq_band_tbl_noise[o + 1];
226
227 for (k = 0; k < ui - li; k++) {
228 o = (k + li >= ui2) ? o + 1 : o;
229 if (o >= MAX_NOISE_COEFFS) return IA_FATAL_ERROR;
230 ui2 = freq_band_table_noise[o + 1];
231
232 frame_data->qmapped_pvc[c][t] =
233 noise_floor[next * num_nf_bands + o];
234 c++;
235 }
236 }
237 }
238 }
239
240 kk = 0;
241 next = -1;
242
243 for (i = 0; i < bs_num_env; i++) {
244 if (kk > MAX_NOISE_ENVELOPES) return IA_FATAL_ERROR;
245 if (p_frame_info->border_vec[i] == p_frame_info->noise_border_vec[kk])
246 kk++, next++;
247
248 start_pos = pvc_frame_info->border_vec[i];
249 end_pos = pvc_frame_info->border_vec[i + 1];
250 if ((start_pos < 0) || (end_pos > MAX_FREQ_COEFFS_SBR))
251 return IA_FATAL_ERROR;
252 for (t = start_pos; t < end_pos; t++) {
253 for (c = 0; c < 64; c++) {
254 env_tmp[c][t] = env_out[64 * t + c];
255 }
256 }
257
258 noise_absc_flag =
259 (i == trans_env || i == frame_data->env_short_flag_prev) ? 1 : 0;
260
261 if (prev_sbr_mode == ORIG_SBR) noise_absc_flag = 0;
262
263 smooth_length = (noise_absc_flag ? 0 : smoothing_length);
264 smooth_filt = *ixheaacd_fir_table[smooth_length];
265
266 for (t = start_pos; t < frame_data->sin_len_for_cur_top; t++) {
267 band_loop_end =
268 num_sf_bands[frame_data->str_frame_info_prev
269 .freq_res[frame_data->var_len_id_prev]];
270
271 for (c = 0, o = 0, j = 0; j < band_loop_end; j++) {
272 double tmp;
273
274 li = freq_band_table[frame_data->str_frame_info_prev
275 .freq_res[frame_data->var_len_id_prev]][j];
276 ui = freq_band_table[frame_data->str_frame_info_prev
277 .freq_res[frame_data->var_len_id_prev]]
278 [j + 1];
279 ui2 = frame_data->pstr_sbr_header->pstr_freq_band_data
280 ->freq_band_tbl_noise[o + 1];
281
282 for (flag = 0, k = li; k < ui; k++) {
283 flag = ((*harm_flag_varlen)[c] &&
284 (t >= frame_data->sin_start_for_cur_top ||
285 (*harm_flag_varlen_prev)[c + sub_band_start]))
286 ? 1
287 : flag;
288
289 nrg_ref_pvc[c][t] = env_tmp[k][t];
290 for (nrg = 0, l = 0; l < rate; l++) {
291 nrg +=
292 (input_real[rate * t + l][k] * input_real[rate * t + l][k]) +
293 (input_imag[rate * t + l][k] * input_imag[rate * t + l][k]);
294 }
295 nrg_est_pvc[c][t] = nrg / rate;
296 c++;
297 }
298
299 if (!int_mode) {
300 for (nrg = 0, k = c - (ui - li); k < c; k++) {
301 nrg += nrg_est_pvc[k][t];
302 }
303 nrg /= (ui - li);
304 }
305 c -= (ui - li);
306
307 for (k = 0; k < ui - li; k++) {
308 o = (k + li >= ui2) ? o + 1 : o;
309 if (o >= MAX_NOISE_COEFFS) return IA_FATAL_ERROR;
310 ui2 = freq_band_table_noise[o + 1];
311 nrg_est_pvc[c][t] = (!int_mode) ? nrg : nrg_est_pvc[c][t];
312 nrg_tone_pvc[c][t] = 0.0f;
313
314 tmp = frame_data->qmapped_pvc[c][t] /
315 (1 + frame_data->qmapped_pvc[c][t]);
316
317 if (flag) {
318 nrg_gain_pvc[c][t] = (FLOAT32)sqrt(nrg_ref_pvc[c][t] * tmp /
319 (nrg_est_pvc[c][t] + 1));
320
321 nrg_tone_pvc[c][t] = (FLOAT32)(
322 (harmonics[c] && (t >= frame_data->sine_position ||
323 (*harm_flag_prev)[c + sub_band_start]))
324 ? sqrt(nrg_ref_pvc[c][t] * tmp /
325 frame_data->qmapped_pvc[c][t])
326 : nrg_tone_pvc[c][t]);
327
328 nrg_tone_pvc[c][t] = (FLOAT32)(
329 ((*harm_flag_varlen)[c] &&
330 (t >= frame_data->sin_start_for_cur_top ||
331 (*harm_flag_varlen_prev)[c + sub_band_start]))
332 ? sqrt(nrg_ref_pvc[c][t] * tmp / prev_env_noise_level[o])
333 : nrg_tone_pvc[c][t]);
334
335 } else {
336 if (noise_absc_flag) {
337 nrg_gain_pvc[c][t] =
338 (FLOAT32)sqrt(nrg_ref_pvc[c][t] / (nrg_est_pvc[c][t] + 1));
339 } else {
340 nrg_gain_pvc[c][t] = (FLOAT32)sqrt(
341 nrg_ref_pvc[c][t] * tmp /
342 ((nrg_est_pvc[c][t] + 1) * frame_data->qmapped_pvc[c][t]));
343 }
344 }
345
346 noise_level_pvc[c][t] = (FLOAT32)sqrt(nrg_ref_pvc[c][t] * tmp);
347 c++;
348 }
349 }
350
351 for (c = 0; c < (*gate_mode)[limiter_band]; c++) {
352 p_ref = p_est = 0.0f;
353 p_adj = 0;
354 for (k = (*lim_table)[limiter_band][c];
355 k < (*lim_table)[limiter_band][c + 1]; k++) {
356 p_ref += nrg_ref_pvc[k][t];
357 p_est += nrg_est_pvc[k][t];
358 }
359 avg_gain = (FLOAT32)sqrt((p_ref + EPS) / (p_est + EPS));
360 g_max = avg_gain * ixheaacd_g_lim_gains[limiter_gains];
361 g_max > 1.0e5f ? g_max = 1.0e5f : 0;
362 for (k = (*lim_table)[limiter_band][c];
363 k < (*lim_table)[limiter_band][c + 1]; k++) {
364 if (g_max <= nrg_gain_pvc[k][t]) {
365 noise_level_pvc[k][t] =
366 noise_level_pvc[k][t] * (g_max / nrg_gain_pvc[k][t]);
367 nrg_gain_pvc[k][t] = g_max;
368 }
369
370 p_adj +=
371 nrg_gain_pvc[k][t] * nrg_gain_pvc[k][t] * nrg_est_pvc[k][t];
372
373 if (nrg_tone_pvc[k][t]) {
374 p_adj += nrg_tone_pvc[k][t] * nrg_tone_pvc[k][t];
375 } else if (!noise_absc_flag) {
376 p_adj += noise_level_pvc[k][t] * noise_level_pvc[k][t];
377 }
378 }
379 boost_gain = (FLOAT32)sqrt((p_ref + EPS) / (p_adj + EPS));
380 boost_gain = boost_gain > 1.584893192f ? 1.584893192f : boost_gain;
381
382 for (k = (*lim_table)[limiter_band][c];
383 k < (*lim_table)[limiter_band][c + 1]; k++) {
384 nrg_gain_pvc[k][t] *= boost_gain;
385 noise_level_pvc[k][t] *= boost_gain;
386 nrg_tone_pvc[k][t] *= boost_gain;
387 }
388 }
389 }
390
391 for (; t < end_pos; t++) {
392 band_loop_end = num_sf_bands[pvc_frame_info->freq_res[i]];
393
394 for (c = 0, o = 0, j = 0; j < band_loop_end; j++) {
395 double tmp;
396
397 li = freq_band_table[pvc_frame_info->freq_res[i]][j];
398 ui = freq_band_table[pvc_frame_info->freq_res[i]][j + 1];
399 ui2 = frame_data->pstr_sbr_header->pstr_freq_band_data
400 ->freq_band_tbl_noise[o + 1];
401
402 for (flag = 0, k = li; k < ui; k++) {
403 flag = (harmonics[c] && (t >= frame_data->sine_position ||
404 (*harm_flag_prev)[c + sub_band_start]))
405 ? 1
406 : flag;
407
408 nrg_ref_pvc[c][t] = env_tmp[k][t];
409 for (nrg = 0, l = 0; l < rate; l++) {
410 nrg +=
411 (input_real[rate * t + l][k] * input_real[rate * t + l][k]) +
412 (input_imag[rate * t + l][k] * input_imag[rate * t + l][k]);
413 }
414 nrg_est_pvc[c][t] = nrg / rate;
415 c++;
416 }
417
418 if (!int_mode) {
419 for (nrg = 0, k = c - (ui - li); k < c; k++) {
420 nrg += nrg_est_pvc[k][t];
421 }
422 nrg /= (ui - li);
423 }
424 c -= (ui - li);
425
426 for (k = 0; k < ui - li; k++) {
427 o = (k + li >= ui2) ? o + 1 : o;
428 if (o >= MAX_NOISE_COEFFS) return IA_FATAL_ERROR;
429 ui2 = freq_band_table_noise[o + 1];
430 nrg_est_pvc[c][t] = (!int_mode) ? nrg : nrg_est_pvc[c][t];
431 nrg_tone_pvc[c][t] = 0.0f;
432
433 tmp = frame_data->qmapped_pvc[c][t] /
434 (1 + frame_data->qmapped_pvc[c][t]);
435
436 if (flag) {
437 nrg_gain_pvc[c][t] = (FLOAT32)sqrt(nrg_ref_pvc[c][t] * tmp /
438 (nrg_est_pvc[c][t] + 1));
439
440 nrg_tone_pvc[c][t] = (FLOAT32)(
441 (harmonics[c] && (t >= frame_data->sine_position ||
442 (*harm_flag_prev)[c + sub_band_start]))
443 ? sqrt(nrg_ref_pvc[c][t] * tmp /
444 frame_data->qmapped_pvc[c][t])
445 : nrg_tone_pvc[c][t]);
446 } else {
447 if (noise_absc_flag) {
448 nrg_gain_pvc[c][t] =
449 (FLOAT32)sqrt(nrg_ref_pvc[c][t] / (nrg_est_pvc[c][t] + 1));
450 } else {
451 nrg_gain_pvc[c][t] = (FLOAT32)sqrt(
452 nrg_ref_pvc[c][t] * tmp /
453 ((nrg_est_pvc[c][t] + 1) * frame_data->qmapped_pvc[c][t]));
454 }
455 }
456
457 noise_level_pvc[c][t] = (FLOAT32)sqrt(nrg_ref_pvc[c][t] * tmp);
458 c++;
459 }
460 }
461
462 for (c = 0; c < (*gate_mode)[limiter_band]; c++) {
463 p_ref = p_est = 0.0f;
464 p_adj = 0;
465 for (k = (*lim_table)[limiter_band][c];
466 k < (*lim_table)[limiter_band][c + 1]; k++) {
467 p_ref += nrg_ref_pvc[k][t];
468 p_est += nrg_est_pvc[k][t];
469 }
470 avg_gain = (FLOAT32)sqrt((p_ref + EPS) / (p_est + EPS));
471 g_max = avg_gain * ixheaacd_g_lim_gains[limiter_gains];
472 g_max > 1.0e5f ? g_max = 1.0e5f : 0;
473
474 for (k = (*lim_table)[limiter_band][c];
475 k < (*lim_table)[limiter_band][c + 1]; k++) {
476 if (g_max <= nrg_gain_pvc[k][t]) {
477 noise_level_pvc[k][t] =
478 noise_level_pvc[k][t] * (g_max / nrg_gain_pvc[k][t]);
479 nrg_gain_pvc[k][t] = g_max;
480 }
481
482 p_adj +=
483 nrg_gain_pvc[k][t] * nrg_gain_pvc[k][t] * nrg_est_pvc[k][t];
484
485 if (nrg_tone_pvc[k][t]) {
486 p_adj += nrg_tone_pvc[k][t] * nrg_tone_pvc[k][t];
487 } else if (!noise_absc_flag) {
488 p_adj += noise_level_pvc[k][t] * noise_level_pvc[k][t];
489 }
490 }
491
492 boost_gain = (FLOAT32)sqrt((p_ref + EPS) / (p_adj + EPS));
493 boost_gain = boost_gain > 1.584893192f ? 1.584893192f : boost_gain;
494
495 for (k = (*lim_table)[limiter_band][c];
496 k < (*lim_table)[limiter_band][c + 1]; k++) {
497 nrg_gain_pvc[k][t] *= boost_gain;
498 noise_level_pvc[k][t] *= boost_gain;
499 nrg_tone_pvc[k][t] *= boost_gain;
500 }
501 }
502 }
503
504 if (esbr_start_up_pvc) {
505 for (n = 0; n < 4; n++) {
506 for (c = 0; c < num_subbands; c++) {
507 (*e_gain)[n][c] = nrg_gain_pvc[c][start_pos];
508 (*noise_buf)[n][c] = noise_level_pvc[c][start_pos];
509 }
510 }
511 esbr_start_up_pvc = 0;
512 esbr_start_up = 0;
513 }
514 for (l = rate * pvc_frame_info->border_vec[i];
515 l < rate * pvc_frame_info->border_vec[1 + i]; l++) {
516 ptr_real_buf = *(input_real + l) + sub_band_start;
517 ptr_imag_buf = *(input_imag + l) + sub_band_start;
518
519 slot_idx = (WORD32)l / rate;
520 if (sub_band_start & 1) {
521 freq_inv = -1;
522 }
523
524 for (k = 0; k < num_subbands; k++) {
525 (*e_gain)[4][k] = nrg_gain_pvc[k][slot_idx];
526 (*noise_buf)[4][k] = noise_level_pvc[k][slot_idx];
527 c = 0, sb_gain = 0, sb_noise = 0;
528 for (n = 4 - smooth_length; n <= 4; n++) {
529 sb_gain += (*e_gain)[n][k] * smooth_filt[c];
530 sb_noise += (*noise_buf)[n][k] * smooth_filt[c++];
531 }
532 phase_index = (phase_index + 1) & 511;
533 sb_noise = (nrg_tone_pvc[k][slot_idx] != 0 || noise_absc_flag)
534 ? 0
535 : sb_noise;
536
537 *ptr_real_buf =
538 *ptr_real_buf * sb_gain +
539 sb_noise * ixheaacd_random_phase[phase_index][0] +
540 nrg_tone_pvc[k][slot_idx] * ixheaacd_hphase_tbl[0][harm_index];
541 *ptr_imag_buf = *ptr_imag_buf * sb_gain +
542 sb_noise * ixheaacd_random_phase[phase_index][1] +
543 nrg_tone_pvc[k][slot_idx] * freq_inv *
544 ixheaacd_hphase_tbl[1][harm_index];
545
546 ptr_real_buf++;
547 ptr_imag_buf++;
548 freq_inv = -freq_inv;
549 }
550
551 harm_index = (harm_index + 1) & 3;
552
553 memcpy(temp, (*e_gain)[0], 64 * sizeof(FLOAT32));
554 for (n = 0; n < 4; n++) {
555 memcpy((*e_gain)[n], (*e_gain)[n + 1], 64 * sizeof(FLOAT32));
556 }
557 memcpy((*e_gain)[4], temp, 64 * sizeof(FLOAT32));
558
559 memcpy(temp, (*noise_buf)[0], 64 * sizeof(FLOAT32));
560 for (n = 0; n < 4; n++) {
561 memcpy((*noise_buf)[n], (*noise_buf)[n + 1], 64 * sizeof(FLOAT32));
562 }
563 memcpy((*noise_buf)[4], temp, 64 * sizeof(FLOAT32));
564 }
565 }
566 } else {
567 for (i = 0; i < num_sf_bands[HIGH]; i++) {
568 li =
569 frame_data->pstr_sbr_header->pstr_freq_band_data->freq_band_tbl_hi[i];
570 ui = frame_data->pstr_sbr_header->pstr_freq_band_data
571 ->freq_band_tbl_hi[i + 1];
572 tmp = ((ui + li) - (sub_band_start << 1)) >> 1;
573 if ((tmp >= 64) || (tmp < 0)) return -1;
574
575 harmonics[tmp] = add_harmonics[i];
576 }
577
578 for (i = 0; i < bs_num_env; i++) {
579 if (kk > MAX_NOISE_ENVELOPES) return IA_FATAL_ERROR;
580
581 if (p_frame_info->border_vec[i] == p_frame_info->noise_border_vec[kk])
582 kk++, next++;
583
584 noise_absc_flag =
585 (i == trans_env || i == frame_data->env_short_flag_prev) ? 1 : 0;
586
587 smooth_length = (noise_absc_flag ? 0 : smoothing_length);
588 smooth_filt = *ixheaacd_fir_table[smooth_length];
589
590 if (sbr_mode == ORIG_SBR) {
591 for (c = 0, o = 0, j = 0; j < num_sf_bands[p_frame_info->freq_res[i]];
592 j++) {
593 double tmp;
594 li = freq_band_table[p_frame_info->freq_res[i]][j];
595 ui = freq_band_table[p_frame_info->freq_res[i]][j + 1];
596 ui2 = frame_data->pstr_sbr_header->pstr_freq_band_data
597 ->freq_band_tbl_noise[o + 1];
598 for (flag = 0, k = li; k < ui; k++) {
599 for (nrg = 0, l = rate * p_frame_info->border_vec[i];
600 l < rate * p_frame_info->border_vec[i + 1]; l++) {
601 nrg += (input_real[l][k] * input_real[l][k]) +
602 (input_imag[l][k] * input_imag[l][k]);
603 }
604 flag = (harmonics[c] &&
605 (i >= trans_env || (*harm_flag_prev)[c + sub_band_start]))
606 ? 1
607 : flag;
608 nrg_est[c++] = nrg / (rate * p_frame_info->border_vec[i + 1] -
609 rate * p_frame_info->border_vec[i]);
610 }
611 if (!int_mode) {
612 for (nrg = 0, k = c - (ui - li); k < c; k++) {
613 nrg += nrg_est[k];
614 }
615 nrg /= (ui - li);
616 }
617 c -= (ui - li);
618
619 for (k = 0; k < ui - li; k++) {
620 FLOAT64 guard = 1e-17;
621 o = (k + li >= ui2) ? o + 1 : o;
622 if (o >= MAX_NOISE_COEFFS) return IA_FATAL_ERROR;
623 ui2 = frame_data->pstr_sbr_header->pstr_freq_band_data
624 ->freq_band_tbl_noise[o + 1];
625 nrg_ref[c] = sfb_nrg[m];
626 nrg_est[c] = (!int_mode) ? nrg : nrg_est[c];
627 nrg_tone[c] = 0;
628 tmp = noise_floor[next * num_nf_bands + o] /
629 (1 + noise_floor[next * num_nf_bands + o]);
630 if (flag) {
631 nrg_gain[c] = (FLOAT32)sqrt(nrg_ref[c] * tmp / (nrg_est[c] + 1));
632 nrg_tone[c] = (FLOAT32)(
633 (harmonics[c] &&
634 (i >= trans_env || (*harm_flag_prev)[c + sub_band_start]))
635 ? sqrt(nrg_ref[c] * tmp /
636 ABS(noise_floor[next * num_nf_bands + o] + guard))
637 : nrg_tone[c]);
638 } else {
639 if (noise_absc_flag)
640 nrg_gain[c] = (FLOAT32)sqrt(nrg_ref[c] / (nrg_est[c] + 1));
641 else
642 nrg_gain[c] = (FLOAT32)sqrt(
643 nrg_ref[c] * tmp /
644 ((nrg_est[c] + 1) *
645 ABS(noise_floor[next * num_nf_bands + o] + guard)));
646 }
647 noise_level[c] = (FLOAT32)sqrt(nrg_ref[c] * tmp);
648 c++;
649 }
650 m++;
651 }
652
653 for (c = 0; c < (*gate_mode)[limiter_band]; c++) {
654 p_ref = p_est = 0;
655 for (k = (*lim_table)[limiter_band][c];
656 k < (*lim_table)[limiter_band][c + 1]; k++) {
657 p_ref += nrg_ref[k];
658 p_est += nrg_est[k];
659 }
660 avg_gain = (FLOAT32)sqrt((p_ref + EPS) / (p_est + EPS));
661 g_max = avg_gain * ixheaacd_g_lim_gains[limiter_gains];
662 g_max > 1.0e5f ? g_max = 1.0e5f : 0;
663 for (k = (*lim_table)[limiter_band][c];
664 k < (*lim_table)[limiter_band][c + 1]; k++) {
665 if (g_max <= nrg_gain[k]) {
666 noise_level[k] = noise_level[k] * (g_max / nrg_gain[k]);
667 nrg_gain[k] = g_max;
668 }
669 }
670 p_adj = 0;
671 for (k = (*lim_table)[limiter_band][c];
672 k < (*lim_table)[limiter_band][c + 1]; k++) {
673 p_adj += nrg_gain[k] * nrg_gain[k] * nrg_est[k];
674 if (nrg_tone[k])
675 p_adj += nrg_tone[k] * nrg_tone[k];
676 else if (!noise_absc_flag)
677 p_adj += noise_level[k] * noise_level[k];
678 }
679 boost_gain = (FLOAT32)sqrt((p_ref + EPS) / (p_adj + EPS));
680 boost_gain = boost_gain > 1.584893192f ? 1.584893192f : boost_gain;
681 for (k = (*lim_table)[limiter_band][c];
682 k < (*lim_table)[limiter_band][c + 1]; k++) {
683 nrg_gain[k] *= boost_gain;
684 noise_level[k] *= boost_gain;
685 nrg_tone[k] *= boost_gain;
686 }
687 }
688
689 if (esbr_start_up) {
690 for (n = 0; n < 4; n++) {
691 memcpy((*e_gain)[n], nrg_gain, num_subbands * sizeof(FLOAT32));
692 memcpy((*noise_buf)[n], noise_level,
693 num_subbands * sizeof(FLOAT32));
694 }
695 esbr_start_up = 0;
696 esbr_start_up_pvc = 0;
697 }
698
699 for (l = rate * p_frame_info->border_vec[i];
700 l < rate * p_frame_info->border_vec[i + 1]; l++) {
701 ptr_real_buf = *(input_real + l) + sub_band_start;
702 ptr_imag_buf = *(input_imag + l) + sub_band_start;
703
704 for (k = 0; k < num_subbands; k++) {
705 (*e_gain)[4][k] = nrg_gain[k];
706 (*noise_buf)[4][k] = noise_level[k];
707 c = 0, sb_gain = 0, sb_noise = 0;
708 for (n = 4 - smooth_length; n <= 4; n++) {
709 sb_gain += (*e_gain)[n][k] * smooth_filt[c];
710 sb_noise += (*noise_buf)[n][k] * smooth_filt[c++];
711 }
712
713 phase_index = (phase_index + 1) & 511;
714 sb_noise = (nrg_tone[k] != 0 || noise_absc_flag) ? 0 : sb_noise;
715
716 *ptr_real_buf = *ptr_real_buf * sb_gain +
717 sb_noise * ixheaacd_random_phase[phase_index][0];
718 *ptr_imag_buf = *ptr_imag_buf * sb_gain +
719 sb_noise * ixheaacd_random_phase[phase_index][1];
720
721 ptr_real_buf++;
722 ptr_imag_buf++;
723 }
724
725 memcpy(temp, (*e_gain)[0], 64 * sizeof(FLOAT32));
726 for (n = 0; n < 4; n++)
727 memcpy((*e_gain)[n], (*e_gain)[n + 1], 64 * sizeof(FLOAT32));
728 memcpy((*e_gain)[4], temp, 64 * sizeof(FLOAT32));
729 memcpy(temp, (*noise_buf)[0], 64 * sizeof(FLOAT32));
730 for (n = 0; n < 4; n++)
731 memcpy((*noise_buf)[n], (*noise_buf)[n + 1], 64 * sizeof(FLOAT32));
732 memcpy((*noise_buf)[4], temp, 64 * sizeof(FLOAT32));
733 }
734
735 ixheaacd_apply_inter_tes(
736 *(input_real1 + rate * p_frame_info->border_vec[i]),
737 *(input_imag1 + rate * p_frame_info->border_vec[i]),
738 *(input_real + rate * p_frame_info->border_vec[i]),
739 *(input_imag + rate * p_frame_info->border_vec[i]),
740 rate * p_frame_info->border_vec[i + 1] -
741 rate * p_frame_info->border_vec[i],
742 sub_band_start, num_subbands, frame_data->inter_temp_shape_mode[i]);
743
744 for (l = rate * p_frame_info->border_vec[i];
745 l < rate * p_frame_info->border_vec[i + 1]; l++) {
746 ptr_real_buf = *(input_real + l) + sub_band_start;
747 ptr_imag_buf = *(input_imag + l) + sub_band_start;
748 if (sub_band_start & 1) {
749 freq_inv = -1;
750 }
751 for (k = 0; k < num_subbands; k++) {
752 *ptr_real_buf += nrg_tone[k] * ixheaacd_hphase_tbl[0][harm_index];
753 *ptr_imag_buf +=
754 nrg_tone[k] * freq_inv * ixheaacd_hphase_tbl[1][harm_index];
755
756 ptr_real_buf++;
757 ptr_imag_buf++;
758 freq_inv = -freq_inv;
759 }
760 harm_index = (harm_index + 1) & 3;
761 }
762 }
763 }
764 }
765
766 for (i = 0; i < 64; i++) {
767 (*harm_flag_varlen_prev)[i] = (*harm_flag_prev)[i];
768 (*harm_flag_varlen)[i] = harmonics[i];
769 }
770
771 memcpy(&((*harm_flag_prev)[0]) + sub_band_start, harmonics,
772 (64 - sub_band_start) * sizeof(WORD8));
773
774 if (trans_env == bs_num_env) {
775 frame_data->env_short_flag_prev = 0;
776 } else {
777 frame_data->env_short_flag_prev = -1;
778 }
779
780 memcpy((VOID *)&frame_data->str_frame_info_prev,
781 (VOID *)&frame_data->str_frame_info_details,
782 sizeof(ia_frame_info_struct));
783
784 if (frame_data->str_frame_info_details.num_env == 1) {
785 frame_data->var_len_id_prev = 0;
786 } else if (frame_data->str_frame_info_details.num_env == 2) {
787 frame_data->var_len_id_prev = 1;
788 }
789
790 if ((frame_data->str_frame_info_details.num_noise_env < 1) ||
791 (frame_data->str_frame_info_details.num_noise_env > 2))
792 return IA_FATAL_ERROR;
793
794 for (i = 0; i < num_nf_bands; i++) {
795 prev_env_noise_level[i] =
796 frame_data->flt_noise_floor
797 [(frame_data->str_frame_info_details.num_noise_env - 1) *
798 num_nf_bands +
799 i];
800 }
801
802 frame_data->harm_index = harm_index;
803 frame_data->phase_index = phase_index;
804 frame_data->pstr_sbr_header->esbr_start_up = esbr_start_up;
805 frame_data->pstr_sbr_header->esbr_start_up_pvc = esbr_start_up_pvc;
806 return 0;
807 }
808
ixheaacd_createlimiterbands(WORD32 lim_table[4][12+1],WORD32 gate_mode[4],WORD16 * freq_band_tbl,WORD32 ixheaacd_num_bands,WORD32 x_over_qmf[MAX_NUM_PATCHES],WORD32 b_patching_mode,WORD32 upsamp_4_flag,struct ixheaacd_lpp_trans_patch * patch_param)809 IA_ERRORCODE ixheaacd_createlimiterbands(
810 WORD32 lim_table[4][12 + 1], WORD32 gate_mode[4], WORD16 *freq_band_tbl,
811 WORD32 ixheaacd_num_bands, WORD32 x_over_qmf[MAX_NUM_PATCHES],
812 WORD32 b_patching_mode, WORD32 upsamp_4_flag,
813 struct ixheaacd_lpp_trans_patch *patch_param) {
814 WORD32 i, j, k, is_patch_border[2];
815 WORD32 patch_borders[MAX_NUM_PATCHES + 1];
816 WORD32 temp_limiter_band_calc[32 + MAX_NUM_PATCHES + 1];
817
818 double num_octave;
819 WORD32 num_patches;
820
821 WORD32 sub_band_start = freq_band_tbl[0];
822 WORD32 sub_band_end = freq_band_tbl[ixheaacd_num_bands];
823
824 const double log2 = log(2.0);
825 const double limbnd_per_oct[4] = {0, 1.2, 2.0, 3.0};
826
827 if (!b_patching_mode && (x_over_qmf != NULL)) {
828 num_patches = 0;
829 if (upsamp_4_flag) {
830 for (i = 1; i < MAX_NUM_PATCHES; i++)
831 if (x_over_qmf[i] != 0) num_patches++;
832 } else {
833 for (i = 1; i < 4; i++)
834 if (x_over_qmf[i] != 0) num_patches++;
835 }
836 for (i = 0; i < num_patches; i++) {
837 patch_borders[i] = x_over_qmf[i] - sub_band_start;
838 }
839 } else {
840 num_patches = patch_param->num_patches;
841 for (i = 0; i < num_patches; i++) {
842 patch_borders[i] = patch_param->start_subband[i] - sub_band_start;
843 }
844 }
845 patch_borders[i] = sub_band_end - sub_band_start;
846
847 lim_table[0][0] = freq_band_tbl[0] - sub_band_start;
848 lim_table[0][1] = freq_band_tbl[ixheaacd_num_bands] - sub_band_start;
849 gate_mode[0] = 1;
850
851 for (i = 1; i < 4; i++) {
852 for (k = 0; k <= ixheaacd_num_bands; k++) {
853 temp_limiter_band_calc[k] = freq_band_tbl[k] - sub_band_start;
854 }
855
856 for (k = 1; k < num_patches; k++) {
857 temp_limiter_band_calc[ixheaacd_num_bands + k] = patch_borders[k];
858 }
859
860 gate_mode[i] = ixheaacd_num_bands + num_patches - 1;
861 ixheaacd_shellsort(temp_limiter_band_calc, gate_mode[i] + 1);
862
863 for (j = 1; j <= gate_mode[i]; j++) {
864 num_octave = log((double)(temp_limiter_band_calc[j] + sub_band_start) /
865 (temp_limiter_band_calc[j - 1] + sub_band_start)) /
866 log2;
867
868 if (num_octave * limbnd_per_oct[i] < 0.49) {
869 if (temp_limiter_band_calc[j] == temp_limiter_band_calc[j - 1]) {
870 temp_limiter_band_calc[j] = sub_band_end;
871 ixheaacd_shellsort(temp_limiter_band_calc, gate_mode[i] + 1);
872 gate_mode[i]--;
873 j--;
874 continue;
875 }
876
877 is_patch_border[0] = is_patch_border[1] = 0;
878
879 for (k = 0; k <= num_patches; k++) {
880 if (temp_limiter_band_calc[j - 1] == patch_borders[k]) {
881 is_patch_border[0] = 1;
882 break;
883 }
884 }
885
886 for (k = 0; k <= num_patches; k++) {
887 if (temp_limiter_band_calc[j] == patch_borders[k]) {
888 is_patch_border[1] = 1;
889 break;
890 }
891 }
892
893 if (!is_patch_border[1]) {
894 temp_limiter_band_calc[j] = sub_band_end;
895 ixheaacd_shellsort(temp_limiter_band_calc, gate_mode[i] + 1);
896 gate_mode[i]--;
897 j--;
898 } else if (!is_patch_border[0]) {
899 temp_limiter_band_calc[j - 1] = sub_band_end;
900 ixheaacd_shellsort(temp_limiter_band_calc, gate_mode[i] + 1);
901 gate_mode[i]--;
902 j--;
903 }
904 }
905 }
906 if (gate_mode[i] > 12) return IA_FATAL_ERROR;
907 for (k = 0; k <= gate_mode[i]; k++) {
908 lim_table[i][k] = temp_limiter_band_calc[k];
909 }
910 }
911 return IA_NO_ERROR;
912 }
913
ixheaacd_apply_inter_tes(FLOAT32 * qmf_real1,FLOAT32 * qmf_imag1,FLOAT32 * qmf_real,FLOAT32 * qmf_imag,WORD32 num_sample,WORD32 sub_band_start,WORD32 num_subband,WORD32 gamma_idx)914 VOID ixheaacd_apply_inter_tes(FLOAT32 *qmf_real1, FLOAT32 *qmf_imag1,
915 FLOAT32 *qmf_real, FLOAT32 *qmf_imag,
916 WORD32 num_sample, WORD32 sub_band_start,
917 WORD32 num_subband, WORD32 gamma_idx) {
918 WORD32 sub_band_end = sub_band_start + num_subband;
919 FLOAT32 subsample_power_high[TIMESLOT_BUFFER_SIZE],
920 subsample_power_low[TIMESLOT_BUFFER_SIZE];
921 FLOAT32 total_power_high = 0.0f;
922 FLOAT32 total_power_low = 0.0f, total_power_high_after = 1.0e-6f;
923 FLOAT32 gain[TIMESLOT_BUFFER_SIZE];
924 FLOAT32 gain_adj, gain_adj_2;
925 FLOAT32 gamma = ixheaacd_q_gamma_table[gamma_idx];
926 WORD32 i, j;
927
928 if (gamma > 0) {
929 for (i = 0; i < num_sample; i++) {
930 memcpy(&qmf_real[64 * i], &qmf_real1[64 * i],
931 sub_band_start * sizeof(FLOAT32));
932 memcpy(&qmf_imag[64 * i], &qmf_imag1[64 * i],
933 sub_band_start * sizeof(FLOAT32));
934 }
935
936 for (i = 0; i < num_sample; i++) {
937 subsample_power_low[i] = 0.0f;
938 for (j = 0; j < sub_band_start; j++) {
939 subsample_power_low[i] += qmf_real[64 * i + j] * qmf_real[64 * i + j];
940 subsample_power_low[i] += qmf_imag[64 * i + j] * qmf_imag[64 * i + j];
941 }
942 subsample_power_high[i] = 0.0f;
943 for (j = sub_band_start; j < sub_band_end; j++) {
944 subsample_power_high[i] += qmf_real[64 * i + j] * qmf_real[64 * i + j];
945 subsample_power_high[i] += qmf_imag[64 * i + j] * qmf_imag[64 * i + j];
946 }
947 total_power_low += subsample_power_low[i];
948 total_power_high += subsample_power_high[i];
949 }
950
951 for (i = 0; i < num_sample; i++) {
952 gain[i] = (FLOAT32)(sqrt(subsample_power_low[i] * num_sample /
953 (total_power_low + 1.0e-6f)));
954 }
955
956 for (i = 0; i < num_sample; i++) {
957 gain[i] = (FLOAT32)(1.0f + gamma * (gain[i] - 1.0f));
958 }
959
960 for (i = 0; i < num_sample; i++) {
961 if (gain[i] < 0.2f) {
962 gain[i] = 0.2f;
963 }
964
965 subsample_power_high[i] *= gain[i] * gain[i];
966 total_power_high_after += subsample_power_high[i];
967 }
968
969 gain_adj_2 = total_power_high / total_power_high_after;
970 gain_adj = (FLOAT32)(sqrt(gain_adj_2));
971
972 for (i = 0; i < num_sample; i++) {
973 gain[i] *= gain_adj;
974
975 for (j = sub_band_start; j < sub_band_end; j++) {
976 qmf_real[64 * i + j] *= gain[i];
977 qmf_imag[64 * i + j] *= gain[i];
978 }
979 }
980 }
981 }
982