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