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