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