Coverage Report

Created: 2025-08-29 06:15

/src/libxaac/encoder/ixheaace_adjust_threshold.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *                                                                            *
3
 * Copyright (C) 2023 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
 */
20
21
#include <float.h>
22
#include <math.h>
23
#include <stdlib.h>
24
25
#include <string.h>
26
27
#include "ixheaac_type_def.h"
28
#include "ixheaac_constants.h"
29
#include "impd_drc_common_enc.h"
30
#include "impd_drc_uni_drc.h"
31
#include "impd_drc_tables.h"
32
#include "impd_drc_api.h"
33
#include "ixheaace_api.h"
34
#include "ixheaace_aac_constants.h"
35
#include "ixheaace_psy_const.h"
36
#include "ixheaace_tns.h"
37
#include "ixheaace_tns_params.h"
38
#include "ixheaace_rom.h"
39
40
#include "ixheaac_basic_ops32.h"
41
#include "ixheaac_basic_ops16.h"
42
#include "ixheaac_basic_ops40.h"
43
#include "ixheaac_basic_ops.h"
44
45
#include "ixheaace_common_rom.h"
46
#include "ixheaace_bitbuffer.h"
47
#include "ixheaace_adjust_threshold_data.h"
48
#include "ixheaace_dynamic_bits.h"
49
#include "ixheaace_qc_data.h"
50
#include "ixheaace_block_switch.h"
51
#include "ixheaace_psy_data.h"
52
#include "ixheaace_interface.h"
53
#include "ixheaace_adjust_threshold.h"
54
#include "ixheaace_common_utils.h"
55
56
VOID iaace_adj_thr_init(ia_adj_thr_state_struct *pstr_adj_thr_state, const FLOAT32 mean_pe,
57
7.21k
                        WORD32 ch_bitrate, WORD32 aot) {
58
7.21k
  ia_adj_thr_elem_struct *pstr_adj_thr_ele = &pstr_adj_thr_state->str_adj_thr_ele;
59
7.21k
  ia_min_snr_adapt_param_struct *pstr_min_snr_params =
60
7.21k
      &pstr_adj_thr_ele->str_min_snr_adapt_params;
61
62
7.21k
  pstr_adj_thr_state->str_bitres_params_long.clip_save_low = CLIP_SAVE_LO_LONG;
63
7.21k
  pstr_adj_thr_state->str_bitres_params_long.clip_save_high = CLIP_SAVE_HI_LONG;
64
7.21k
  pstr_adj_thr_state->str_bitres_params_long.min_bit_save = MIN_BITS_SAVE_LONG;
65
7.21k
  pstr_adj_thr_state->str_bitres_params_long.max_bit_save = MAX_BITS_SAVE_LONG;
66
7.21k
  pstr_adj_thr_state->str_bitres_params_long.clip_spend_low = CLIP_SPEND_LO_LONG;
67
7.21k
  pstr_adj_thr_state->str_bitres_params_long.clip_spend_high = CLIP_SPEND_HI_LONG;
68
7.21k
  pstr_adj_thr_state->str_bitres_params_long.min_bits_spend = MIN_BITS_SPEND_LONG;
69
7.21k
  pstr_adj_thr_state->str_bitres_params_long.max_bits_spend = MAX_BITS_SPEND_LONG;
70
71
7.21k
  pstr_adj_thr_state->str_bitres_params_short.clip_save_low = CLIP_SAVE_LO_SHORT;
72
7.21k
  pstr_adj_thr_state->str_bitres_params_short.clip_save_high = CLIP_SAVE_HI_SHORT;
73
7.21k
  pstr_adj_thr_state->str_bitres_params_short.min_bit_save = MIN_BITS_SAVE_SHORT;
74
7.21k
  pstr_adj_thr_state->str_bitres_params_short.max_bit_save = MAX_BITS_SAVE_SHORT;
75
7.21k
  pstr_adj_thr_state->str_bitres_params_short.clip_spend_low = CLIP_SPEND_LO_SHORT;
76
7.21k
  pstr_adj_thr_state->str_bitres_params_short.clip_spend_high = CLIP_SPEND_HI_SHORT;
77
7.21k
  pstr_adj_thr_state->str_bitres_params_short.min_bits_spend = MIN_BITS_SPEND_SHORT;
78
7.21k
  pstr_adj_thr_state->str_bitres_params_short.max_bits_spend = MAX_BITS_SPEND_SHORT;
79
80
7.21k
  pstr_adj_thr_ele->pe_min = (FLOAT32)0.8f * mean_pe;
81
7.21k
  pstr_adj_thr_ele->pe_max = (FLOAT32)1.2f * mean_pe;
82
7.21k
  pstr_adj_thr_ele->pe_offset = 0.0f;
83
7.21k
  switch (aot) {
84
1.63k
    case AOT_AAC_LC:
85
4.24k
    case AOT_SBR:
86
4.60k
    case AOT_PS:
87
4.60k
      if (ch_bitrate < 32000) {
88
3.28k
        pstr_adj_thr_ele->pe_offset = MAX(
89
3.28k
            (FLOAT32)50.0f, (FLOAT32)(100.0f) - (FLOAT32)(100.0f / 32000) * (FLOAT32)ch_bitrate);
90
3.28k
      }
91
4.60k
      break;
92
93
664
    case AOT_AAC_LD:
94
2.61k
    case AOT_AAC_ELD:
95
96
2.61k
      if (ch_bitrate <= 32000) {
97
1.59k
        pstr_adj_thr_ele->pe_offset = MAX(
98
1.59k
            (FLOAT32)50.0f, (FLOAT32)(100.0f) - (FLOAT32)(100.0f / 32000) * (FLOAT32)ch_bitrate);
99
1.59k
      }
100
2.61k
      break;
101
7.21k
  }
102
103
7.21k
  if (ch_bitrate > 20000) {
104
3.57k
    pstr_adj_thr_ele->str_ah_param.modify_min_snr = TRUE;
105
3.57k
    pstr_adj_thr_ele->str_ah_param.start_sfb_long = 15;
106
3.57k
    pstr_adj_thr_ele->str_ah_param.start_sfb_short = 3;
107
3.63k
  } else {
108
3.63k
    pstr_adj_thr_ele->str_ah_param.modify_min_snr = FALSE;
109
3.63k
    pstr_adj_thr_ele->str_ah_param.start_sfb_long = 0;
110
3.63k
    pstr_adj_thr_ele->str_ah_param.start_sfb_short = 0;
111
3.63k
  }
112
113
7.21k
  pstr_min_snr_params->max_red = (FLOAT32)0.25f;
114
115
7.21k
  pstr_min_snr_params->start_ratio = (FLOAT32)1.e1f;
116
117
7.21k
  pstr_min_snr_params->max_ratio = (FLOAT32)1.e3f;
118
119
7.21k
  pstr_min_snr_params->red_ratio_fac =
120
7.21k
      (1.0f - pstr_min_snr_params->max_red) /
121
7.21k
      (10.0f * (FLOAT32)log10(pstr_min_snr_params->start_ratio / pstr_min_snr_params->max_ratio));
122
123
7.21k
  pstr_min_snr_params->red_offs = 1.0f - pstr_min_snr_params->red_ratio_fac * 10.0f *
124
7.21k
                                             (FLOAT32)log10(pstr_min_snr_params->start_ratio);
125
126
7.21k
  pstr_adj_thr_ele->pe_last = (FLOAT32)0.0f;
127
7.21k
  pstr_adj_thr_ele->dyn_bits_last = 0;
128
7.21k
  pstr_adj_thr_ele->pe_correction_fac = (FLOAT32)1.0f;
129
7.21k
}
130
994k
FLOAT32 iaace_bits_to_pe(const FLOAT32 bits) { return (bits * 1.18f); }
131
static VOID iaace_calc_sfb_pe_data(ia_qc_pe_data_struct *pstr_qc_pe_data,
132
                                   ixheaace_psy_out_channel **pstr_psy_out, WORD32 num_channels,
133
969k
                                   WORD32 chn) {
134
969k
  WORD32 ch;
135
969k
  WORD32 scf_band_grp;
136
969k
  FLOAT32 num_lines;
137
969k
  FLOAT32 ld_thr, ld_ratio;
138
969k
  WORD32 i = 0, scf;
139
969k
  WORD32 sfb_count;
140
969k
  WORD32 scf_band_per_grp;
141
969k
  WORD32 max_sfb_per_grp;
142
969k
  FLOAT32 *ptr_sfb_energy;
143
969k
  FLOAT32 *ptr_sfb_thr;
144
969k
  ia_qc_pe_chan_data_struct *pstr_qc_pe_chan_data;
145
146
969k
  pstr_qc_pe_data->pe = pstr_qc_pe_data->offset;
147
969k
  pstr_qc_pe_data->const_part = 0.0f;
148
969k
  pstr_qc_pe_data->num_active_lines = 0.0f;
149
150
2.37M
  for (ch = chn; ch < chn + num_channels; ch++) {
151
1.40M
    sfb_count = pstr_psy_out[ch]->sfb_count;
152
1.40M
    scf_band_per_grp = pstr_psy_out[ch]->sfb_per_group;
153
1.40M
    max_sfb_per_grp = pstr_psy_out[ch]->max_sfb_per_grp;
154
1.40M
    ptr_sfb_energy = pstr_psy_out[ch]->ptr_sfb_energy;
155
1.40M
    ptr_sfb_thr = pstr_psy_out[ch]->ptr_sfb_thr;
156
1.40M
    pstr_qc_pe_chan_data = &pstr_qc_pe_data->pe_ch_data[ch];
157
1.40M
    pstr_qc_pe_chan_data->pe = 0;
158
1.40M
    pstr_qc_pe_chan_data->num_active_lines = 0;
159
1.40M
    pstr_qc_pe_chan_data->const_part = 0;
160
161
3.56M
    for (scf_band_grp = 0; scf_band_grp < sfb_count; scf_band_grp += scf_band_per_grp) {
162
2.16M
      i = scf_band_grp;
163
43.1M
      for (scf = max_sfb_per_grp - 1; scf >= 0; scf--, i++) {
164
40.9M
        if (ptr_sfb_energy[i] > ptr_sfb_thr[i]) {
165
24.2M
          ld_thr = (FLOAT32)log(ptr_sfb_thr[i]) * LOG2_1;
166
24.2M
          ld_ratio = pstr_qc_pe_chan_data->sfb_ld_energy[i] - ld_thr;
167
24.2M
          num_lines = pstr_qc_pe_chan_data->sfb_lines[i];
168
24.2M
          if (ld_ratio >= PE_C1) {
169
13.3M
            pstr_qc_pe_chan_data->sfb_pe[i] = num_lines * ld_ratio;
170
13.3M
            pstr_qc_pe_chan_data->sfb_const_part[i] =
171
13.3M
                num_lines * pstr_qc_pe_chan_data->sfb_ld_energy[i];
172
13.3M
          } else {
173
10.8M
            pstr_qc_pe_chan_data->sfb_pe[i] = num_lines * (PE_C2 + PE_C3 * ld_ratio);
174
10.8M
            pstr_qc_pe_chan_data->sfb_const_part[i] =
175
10.8M
                num_lines * (PE_C2 + PE_C3 * pstr_qc_pe_chan_data->sfb_ld_energy[i]);
176
10.8M
            num_lines = num_lines * PE_C3;
177
10.8M
          }
178
24.2M
          pstr_qc_pe_chan_data->num_sfb_active_lines[i] = num_lines;
179
24.2M
        } else {
180
16.6M
          pstr_qc_pe_chan_data->sfb_pe[i] = 0.0f;
181
16.6M
          pstr_qc_pe_chan_data->sfb_const_part[i] = 0.0f;
182
16.6M
          pstr_qc_pe_chan_data->num_sfb_active_lines[i] = 0.0;
183
16.6M
        }
184
185
40.9M
        pstr_qc_pe_chan_data->pe += pstr_qc_pe_chan_data->sfb_pe[i];
186
40.9M
        pstr_qc_pe_chan_data->const_part += pstr_qc_pe_chan_data->sfb_const_part[i];
187
40.9M
        pstr_qc_pe_chan_data->num_active_lines += pstr_qc_pe_chan_data->num_sfb_active_lines[i];
188
40.9M
      }
189
2.16M
    }
190
1.40M
    pstr_qc_pe_data->pe += pstr_qc_pe_chan_data->pe;
191
1.40M
    pstr_qc_pe_data->const_part += pstr_qc_pe_chan_data->const_part;
192
1.40M
    pstr_qc_pe_data->num_active_lines += pstr_qc_pe_chan_data->num_active_lines;
193
1.40M
    pstr_psy_out[ch]->pe = pstr_qc_pe_data->pe;
194
1.40M
  }
195
969k
}
196
379k
static FLOAT32 iaace_calc_bit_save(ia_bitres_param_struct *pstr_bitres_params, FLOAT32 fill_lvl) {
197
379k
  FLOAT32 bit_save;
198
379k
  const FLOAT32 clip_low = pstr_bitres_params->clip_save_low;
199
379k
  const FLOAT32 clip_high = pstr_bitres_params->clip_save_high;
200
379k
  const FLOAT32 min_bit_save = pstr_bitres_params->min_bit_save;
201
379k
  const FLOAT32 max_bit_save = pstr_bitres_params->max_bit_save;
202
203
379k
  fill_lvl = MAX(fill_lvl, clip_low);
204
379k
  fill_lvl = MIN(fill_lvl, clip_high);
205
379k
  bit_save = max_bit_save -
206
379k
             ((max_bit_save - min_bit_save) / (clip_high - clip_low)) * (fill_lvl - clip_low);
207
208
379k
  return bit_save;
209
379k
}
210
211
static FLOAT32 iaace_calc_bit_spend(ia_bitres_param_struct *pstr_bitres_params,
212
379k
                                    FLOAT32 fill_lvl) {
213
379k
  FLOAT32 bit_spend;
214
379k
  const FLOAT32 clip_low = pstr_bitres_params->clip_spend_low;
215
379k
  const FLOAT32 clip_high = pstr_bitres_params->clip_spend_high;
216
379k
  const FLOAT32 min_bits_spend = pstr_bitres_params->min_bits_spend;
217
379k
  const FLOAT32 max_bits_spend = pstr_bitres_params->max_bits_spend;
218
219
379k
  fill_lvl = MAX(fill_lvl, clip_low);
220
379k
  fill_lvl = MIN(fill_lvl, clip_high);
221
379k
  bit_spend = min_bits_spend + ((max_bits_spend - min_bits_spend) / (clip_high - clip_low)) *
222
379k
                                   (fill_lvl - clip_low);
223
224
379k
  return bit_spend;
225
379k
}
226
227
static VOID iaace_adjust_pe_minmax(const FLOAT32 curr_pe, FLOAT32 *ptr_pe_min,
228
379k
                                   FLOAT32 *ptr_pe_max) {
229
379k
  FLOAT32 min_hi_fac = 0.3f, max_hi_fac = 1.0f, min_low_fac = 0.14f, max_low_fac = 0.07f;
230
379k
  FLOAT32 diff;
231
379k
  FLOAT32 min_diff = curr_pe * (FLOAT32)0.1666666667f;
232
233
379k
  if (curr_pe > *ptr_pe_max) {
234
26.5k
    diff = (curr_pe - *ptr_pe_max);
235
26.5k
    *ptr_pe_min += diff * min_hi_fac;
236
26.5k
    *ptr_pe_max += diff * max_hi_fac;
237
352k
  } else {
238
352k
    if (curr_pe < *ptr_pe_min) {
239
249k
      diff = (*ptr_pe_min - curr_pe);
240
249k
      *ptr_pe_min -= diff * min_low_fac;
241
249k
      *ptr_pe_max -= diff * max_low_fac;
242
249k
    } else {
243
103k
      *ptr_pe_min += (curr_pe - *ptr_pe_min) * min_hi_fac;
244
103k
      *ptr_pe_max -= (*ptr_pe_max - curr_pe) * max_low_fac;
245
103k
    }
246
352k
  }
247
248
379k
  if ((*ptr_pe_max - *ptr_pe_min) < min_diff) {
249
16.1k
    FLOAT32 low_part, high_part;
250
16.1k
    low_part = MAX((FLOAT32)0.0f, curr_pe - *ptr_pe_min);
251
16.1k
    high_part = MAX((FLOAT32)0.0f, *ptr_pe_max - curr_pe);
252
16.1k
    *ptr_pe_max = curr_pe + high_part / (low_part + high_part) * min_diff;
253
16.1k
    *ptr_pe_min = curr_pe - low_part / (low_part + high_part) * min_diff;
254
16.1k
    *ptr_pe_min = MAX((FLOAT32)0.0f, *ptr_pe_min);
255
16.1k
  }
256
379k
}
257
258
static FLOAT32 iaace_bitres_calc_bitfac(const WORD32 bitres_bits, const WORD32 max_bitres_bits,
259
                                        const FLOAT32 pe, const WORD32 win_seq,
260
                                        const WORD32 avg_bits, const FLOAT32 max_bit_fac,
261
                                        ia_adj_thr_state_struct *pstr_adj_thr_state,
262
379k
                                        ia_adj_thr_elem_struct *pstr_adj_the_elem) {
263
379k
  ia_bitres_param_struct *pstr_bitres_params;
264
379k
  FLOAT32 pex;
265
379k
  FLOAT32 fill_lvl = 0.0f;
266
379k
  FLOAT32 bit_save, bit_spend, bitres_factor;
267
268
379k
  if (max_bitres_bits) {
269
301k
    fill_lvl = (FLOAT32)bitres_bits / max_bitres_bits;
270
301k
  }
271
272
379k
  if (win_seq != SHORT_WINDOW) {
273
320k
    pstr_bitres_params = &(pstr_adj_thr_state->str_bitres_params_long);
274
320k
  } else {
275
58.8k
    pstr_bitres_params = &(pstr_adj_thr_state->str_bitres_params_short);
276
58.8k
  }
277
278
379k
  pex = MAX(pe, pstr_adj_the_elem->pe_min);
279
379k
  pex = MIN(pex, pstr_adj_the_elem->pe_max);
280
281
379k
  bit_save = iaace_calc_bit_save(pstr_bitres_params, fill_lvl);
282
379k
  bit_spend = iaace_calc_bit_spend(pstr_bitres_params, fill_lvl);
283
284
379k
  bitres_factor =
285
379k
      (FLOAT32)1.0f - bit_save +
286
379k
      ((bit_spend + bit_save) / (pstr_adj_the_elem->pe_max - pstr_adj_the_elem->pe_min)) *
287
379k
          (pex - pstr_adj_the_elem->pe_min);
288
379k
  if (avg_bits)
289
379k
  {
290
379k
    bitres_factor = MIN(bitres_factor,
291
379k
      (FLOAT32)1.0f - (FLOAT32)0.3f + (FLOAT32)bitres_bits / (FLOAT32)avg_bits);
292
379k
  }
293
294
379k
  bitres_factor = MIN(bitres_factor, max_bit_fac);
295
296
379k
  iaace_adjust_pe_minmax(pe, &pstr_adj_the_elem->pe_min, &pstr_adj_the_elem->pe_max);
297
298
379k
  return bitres_factor;
299
379k
}
300
301
static VOID iaace_calc_pe_correction(FLOAT32 *ptr_correction_fac, const FLOAT32 pe_act,
302
379k
                                     const FLOAT32 pe_last, const WORD32 bits_prev) {
303
379k
  if ((bits_prev > 0) && (pe_act < (FLOAT32)1.5f * pe_last) &&
304
379k
      (pe_act > (FLOAT32)0.7f * pe_last) &&
305
379k
      ((FLOAT32)1.2f * iaace_bits_to_pe((FLOAT32)bits_prev) > pe_last) &&
306
379k
      ((FLOAT32)0.65f * iaace_bits_to_pe((FLOAT32)bits_prev) < pe_last)) {
307
183k
    FLOAT32 new_fac = pe_last / iaace_bits_to_pe((FLOAT32)bits_prev);
308
309
183k
    if (new_fac < (FLOAT32)1.0f) {
310
122k
      new_fac = MIN((FLOAT32)1.1f * new_fac, (FLOAT32)1.0f);
311
122k
      new_fac = MAX(new_fac, (FLOAT32)0.85f);
312
122k
    } else {
313
60.9k
      new_fac = MAX((FLOAT32)0.9f * new_fac, (FLOAT32)1.0f);
314
60.9k
      new_fac = MIN(new_fac, (FLOAT32)1.15f);
315
60.9k
    }
316
183k
    if (((new_fac > (FLOAT32)1.0f) && (*ptr_correction_fac < (FLOAT32)1.0f)) ||
317
183k
        ((new_fac < (FLOAT32)1.0f) && (*ptr_correction_fac > (FLOAT32)1.0f))) {
318
4.80k
      *ptr_correction_fac = (FLOAT32)1.0f;
319
4.80k
    }
320
321
183k
    if ((*ptr_correction_fac < (FLOAT32)1.0f && new_fac < *ptr_correction_fac) ||
322
183k
        (*ptr_correction_fac > (FLOAT32)1.0f && new_fac > *ptr_correction_fac)) {
323
48.0k
      *ptr_correction_fac = (FLOAT32)0.85f * (*ptr_correction_fac) + (FLOAT32)0.15f * new_fac;
324
135k
    } else {
325
135k
      *ptr_correction_fac = (FLOAT32)0.7f * (*ptr_correction_fac) + (FLOAT32)0.3f * new_fac;
326
135k
    }
327
328
183k
    *ptr_correction_fac = MIN(*ptr_correction_fac, (FLOAT32)1.15f);
329
183k
    *ptr_correction_fac = MAX(*ptr_correction_fac, (FLOAT32)0.85f);
330
195k
  } else {
331
195k
    *ptr_correction_fac = (FLOAT32)1.0f;
332
195k
  }
333
379k
}
334
335
static VOID iaace_calc_thr_exp(
336
    FLOAT32 thr_exp[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND],
337
235k
    ixheaace_psy_out_channel **pstr_psy_out, WORD32 num_chans, WORD32 chn) {
338
235k
  WORD32 sfb, ch, scf_band_grp;
339
235k
  ixheaace_psy_out_channel *pstr_psy_chan_out;
340
235k
  FLOAT32 *ptr_scf_band_thr;
341
235k
  FLOAT32 *ptr_thr_exp;
342
582k
  for (ch = chn; ch < chn + num_chans; ch++) {
343
346k
    pstr_psy_chan_out = pstr_psy_out[ch];
344
346k
    ptr_thr_exp = thr_exp[ch];
345
898k
    for (scf_band_grp = 0; scf_band_grp < pstr_psy_chan_out->sfb_count;
346
552k
         scf_band_grp += pstr_psy_chan_out->sfb_per_group) {
347
552k
      FLOAT32 *ptr_thr_exp1 = &ptr_thr_exp[scf_band_grp];
348
552k
      ptr_scf_band_thr = &pstr_psy_chan_out->ptr_sfb_thr[scf_band_grp];
349
10.9M
      for (sfb = 0; sfb < pstr_psy_chan_out->max_sfb_per_grp; sfb++) {
350
10.3M
        ptr_thr_exp1[sfb] = (FLOAT32)pow(*ptr_scf_band_thr++, RED_EXP_VAL);
351
10.3M
      }
352
552k
    }
353
346k
  }
354
235k
}
355
356
static VOID iaace_adapt_min_snr(ixheaace_psy_out_channel **pstr_psy_out,
357
                                ia_min_snr_adapt_param_struct *pstr_min_snr_params,
358
235k
                                WORD32 num_chans, WORD32 chn) {
359
235k
  WORD32 num_scf_band = 0, ch, scf_band_cnt, scf_band_offs, sfb;
360
235k
  FLOAT32 avg_energy = 0.0f, db_ratio, min_snr_red;
361
235k
  WORD32 i;
362
363
582k
  for (ch = chn; ch < chn + num_chans; ch++) {
364
346k
    ixheaace_psy_out_channel *pstr_psy_chan_out = pstr_psy_out[ch];
365
346k
    num_scf_band = 0;
366
346k
    avg_energy = 0;
367
346k
    scf_band_cnt = pstr_psy_chan_out->max_sfb_per_grp;
368
369
898k
    for (scf_band_offs = 0; scf_band_offs < pstr_psy_chan_out->sfb_count;
370
552k
         scf_band_offs += pstr_psy_chan_out->sfb_per_group) {
371
552k
      FLOAT32 *ptr_sfb_energy = &pstr_psy_chan_out->ptr_sfb_energy[scf_band_offs];
372
10.9M
      for (sfb = scf_band_cnt - 1; sfb >= 0; sfb--) {
373
10.3M
        avg_energy += ptr_sfb_energy[sfb];
374
10.3M
      }
375
552k
      num_scf_band += scf_band_cnt;
376
552k
    }
377
378
346k
    if (num_scf_band > 0) {
379
346k
      avg_energy /= num_scf_band;
380
346k
    }
381
382
898k
    for (scf_band_offs = 0; scf_band_offs < pstr_psy_chan_out->sfb_count;
383
552k
         scf_band_offs += pstr_psy_chan_out->sfb_per_group) {
384
552k
      i = scf_band_offs;
385
10.9M
      for (sfb = scf_band_cnt - 1; sfb >= 0; sfb--, i++) {
386
10.3M
        if (pstr_min_snr_params->start_ratio * pstr_psy_chan_out->ptr_sfb_energy[i] <
387
10.3M
            avg_energy) {
388
6.58M
          db_ratio =
389
6.58M
              (FLOAT32)(10.0f * log10((MIN_FLT_VAL + avg_energy) /
390
6.58M
                                      (MIN_FLT_VAL + pstr_psy_chan_out->ptr_sfb_energy[i])));
391
6.58M
          min_snr_red =
392
6.58M
              pstr_min_snr_params->red_offs + pstr_min_snr_params->red_ratio_fac * db_ratio;
393
6.58M
          min_snr_red = MAX(min_snr_red, pstr_min_snr_params->max_red);
394
6.58M
          pstr_psy_chan_out->sfb_min_snr[i] =
395
6.58M
              (FLOAT32)pow(pstr_psy_out[ch]->sfb_min_snr[i], min_snr_red);
396
6.58M
          pstr_psy_chan_out->sfb_min_snr[i] =
397
6.58M
              MIN(MIN_SNR_LIMIT, pstr_psy_out[ch]->sfb_min_snr[i]);
398
6.58M
        }
399
10.3M
      }
400
552k
    }
401
346k
  }
402
235k
}
403
404
static VOID iaace_init_avoid_hole_flag(
405
    WORD32 ah_flag[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND],
406
    ixheaace_psy_out_channel **pstr_psy_out, ia_ah_param_struct *pstr_ah_param, WORD32 num_chans,
407
235k
    WORD32 chn, WORD32 aot) {
408
235k
  WORD32 ch;
409
235k
  FLOAT32 sfb_energy;
410
235k
  FLOAT32 scale_spread_energy = 0.0f;
411
235k
  WORD32 scf_band_grp, scf_band_cnt, scf_band;
412
235k
  FLOAT32 *ptr_scf_band_spread_energy, *ptr_scf_band_energy, *ptr_scf_band_min_snr;
413
582k
  for (ch = chn; ch < chn + num_chans; ch++) {
414
346k
    ixheaace_psy_out_channel *pstr_psy_chan_out = pstr_psy_out[ch];
415
416
346k
    if (pstr_psy_chan_out->window_sequence != SHORT_WINDOW) {
417
275k
      switch (aot) {
418
57.6k
        case AOT_AAC_LC:
419
140k
        case AOT_SBR:
420
142k
        case AOT_PS:
421
142k
          scale_spread_energy = 0.5f;
422
142k
          break;
423
424
17.0k
        case AOT_AAC_LD:
425
132k
        case AOT_AAC_ELD:
426
132k
          scale_spread_energy = 0.56f;
427
132k
          break;
428
275k
      }
429
275k
    } else {
430
71.3k
      scale_spread_energy = 0.63f;
431
71.3k
    }
432
433
898k
    for (scf_band_grp = 0; scf_band_grp < pstr_psy_chan_out->sfb_count;
434
552k
         scf_band_grp += pstr_psy_chan_out->sfb_per_group) {
435
552k
      ptr_scf_band_spread_energy = &pstr_psy_chan_out->ptr_sfb_spread_energy[scf_band_grp];
436
552k
      scf_band_cnt = pstr_psy_chan_out->max_sfb_per_grp;
437
10.9M
      for (scf_band = scf_band_cnt - 1; scf_band >= 0; scf_band--) {
438
10.3M
        *ptr_scf_band_spread_energy = *ptr_scf_band_spread_energy * scale_spread_energy;
439
10.3M
        ptr_scf_band_spread_energy++;
440
10.3M
      }
441
552k
    }
442
346k
  }
443
444
235k
  if (pstr_ah_param->modify_min_snr) {
445
204k
    for (ch = chn; ch < chn + num_chans; ch++) {
446
116k
      ixheaace_psy_out_channel *pstr_psy_chan_out = pstr_psy_out[ch];
447
116k
      ptr_scf_band_energy = pstr_psy_chan_out->ptr_sfb_energy;
448
449
116k
      ptr_scf_band_min_snr = pstr_psy_chan_out->sfb_min_snr;
450
451
259k
      for (scf_band_grp = 0; scf_band_grp < pstr_psy_chan_out->sfb_count;
452
143k
           scf_band_grp += pstr_psy_chan_out->sfb_per_group) {
453
3.51M
        for (scf_band = 0; scf_band < pstr_psy_chan_out->max_sfb_per_grp; scf_band++) {
454
3.37M
          FLOAT32 sfb_en_m1, sfb_en_p1, avg_energy;
455
3.37M
          if (scf_band > 0) {
456
3.23M
            sfb_en_m1 = ptr_scf_band_energy[scf_band_grp + scf_band - 1];
457
3.23M
          } else {
458
143k
            sfb_en_m1 = ptr_scf_band_energy[scf_band_grp];
459
143k
          }
460
3.37M
          if (scf_band < pstr_psy_chan_out->max_sfb_per_grp - 1) {
461
3.23M
            sfb_en_p1 = ptr_scf_band_energy[scf_band_grp + scf_band + 1];
462
3.23M
          } else {
463
143k
            sfb_en_p1 = ptr_scf_band_energy[scf_band_grp + scf_band];
464
143k
          }
465
466
3.37M
          avg_energy = (sfb_en_m1 + sfb_en_p1) / (FLOAT32)2.0f;
467
3.37M
          sfb_energy = ptr_scf_band_energy[scf_band_grp + scf_band];
468
3.37M
          FLOAT32 temp_min_snr = 0.0f;
469
3.37M
          if (sfb_energy > avg_energy) {
470
1.14M
            switch (aot) {
471
198k
              case AOT_AAC_LC:
472
453k
              case AOT_SBR:
473
584k
              case AOT_PS:
474
584k
                temp_min_snr = MAX((FLOAT32)0.8f * avg_energy / sfb_energy, (FLOAT32)0.316f);
475
584k
                break;
476
477
26.7k
              case AOT_AAC_LD:
478
563k
              case AOT_AAC_ELD:
479
563k
                temp_min_snr = MAX((FLOAT32)0.8f * avg_energy / sfb_energy, (FLOAT32)0.408f);
480
563k
                break;
481
1.14M
            }
482
1.14M
            if (pstr_psy_chan_out->window_sequence != SHORT_WINDOW) {
483
987k
              switch (aot) {
484
181k
                case AOT_AAC_LC:
485
218k
                case AOT_PS:
486
218k
                  temp_min_snr = MAX(temp_min_snr, (FLOAT32)0.316f);
487
218k
                  break;
488
489
26.7k
                case AOT_AAC_LD:
490
26.7k
                  temp_min_snr = MAX(temp_min_snr, (FLOAT32)0.408f);
491
26.7k
                  break;
492
987k
              }
493
987k
            } else {
494
160k
              temp_min_snr = MAX(temp_min_snr, (FLOAT32)0.5f);
495
160k
            }
496
1.14M
            ptr_scf_band_min_snr[scf_band_grp + scf_band] =
497
1.14M
                MIN(ptr_scf_band_min_snr[scf_band_grp + scf_band], temp_min_snr);
498
1.14M
          }
499
500
3.37M
          if (((FLOAT32)2.0f * sfb_energy < avg_energy) && (sfb_energy > (FLOAT32)0.0f)) {
501
640k
            temp_min_snr = avg_energy / ((FLOAT32)2.0f * sfb_energy) *
502
640k
                           ptr_scf_band_min_snr[scf_band_grp + scf_band];
503
640k
            temp_min_snr = MIN((FLOAT32)0.8f, temp_min_snr);
504
640k
            ptr_scf_band_min_snr[scf_band_grp + scf_band] =
505
640k
                MIN(temp_min_snr, ptr_scf_band_min_snr[scf_band_grp + scf_band] * (FLOAT32)3.16f);
506
640k
          }
507
3.37M
        }
508
143k
      }
509
116k
    }
510
88.4k
  }
511
512
235k
  if (num_chans == 2) {
513
111k
    ixheaace_psy_out_channel *pstr_psy_out_mid = pstr_psy_out[chn];
514
111k
    ixheaace_psy_out_channel *pstr_psy_out_side = pstr_psy_out[chn + 1];
515
111k
    WORD32 sfb;
516
517
4.52M
    for (sfb = 0; sfb < pstr_psy_out_mid->sfb_count; sfb++) {
518
4.41M
      if (pstr_psy_out[chn]->ms_used[sfb]) {
519
3.66M
        FLOAT32 sfb_en_mid = pstr_psy_out_mid->ptr_sfb_energy[sfb];
520
3.66M
        FLOAT32 sfb_en_side = pstr_psy_out_side->ptr_sfb_energy[sfb];
521
3.66M
        FLOAT32 max_sfb_en = MAX(sfb_en_mid, sfb_en_side);
522
3.66M
        FLOAT32 max_thr = 0.25f * pstr_psy_out_mid->sfb_min_snr[sfb] * max_sfb_en;
523
524
3.66M
        pstr_psy_out_mid->sfb_min_snr[sfb] = (FLOAT32)MAX(
525
3.66M
            pstr_psy_out_mid->sfb_min_snr[sfb],
526
3.66M
            MIN(MAX_FLT_VAL, ((FLOAT32)max_thr / (MIN_FLT_VAL + (FLOAT32)sfb_en_mid))));
527
528
3.66M
        if (pstr_psy_out_mid->ptr_sfb_energy[sfb] <= 1.0f) {
529
805k
          pstr_psy_out_mid->ptr_sfb_energy[sfb] =
530
805k
              MIN(pstr_psy_out_mid->ptr_sfb_energy[sfb], 0.8f);
531
805k
        }
532
533
3.66M
        pstr_psy_out_side->sfb_min_snr[sfb] = (FLOAT32)MAX(
534
3.66M
            pstr_psy_out_side->sfb_min_snr[sfb],
535
3.66M
            MIN(MAX_FLT_VAL, ((FLOAT32)max_thr / (MIN_FLT_VAL + (FLOAT32)sfb_en_side))));
536
537
3.66M
        if (pstr_psy_out_side->sfb_min_snr[sfb] <= 1.0f) {
538
857k
          pstr_psy_out_side->sfb_min_snr[sfb] = MIN(pstr_psy_out_side->sfb_min_snr[sfb], 0.8f);
539
857k
        }
540
3.66M
      }
541
4.41M
    }
542
111k
  }
543
544
582k
  for (ch = chn; ch < chn + num_chans; ch++) {
545
346k
    ixheaace_psy_out_channel *pstr_psy_chan_out = pstr_psy_out[ch];
546
898k
    for (scf_band_grp = 0; scf_band_grp < pstr_psy_chan_out->sfb_count;
547
552k
         scf_band_grp += pstr_psy_chan_out->sfb_per_group) {
548
10.9M
      for (scf_band = 0; scf_band < pstr_psy_chan_out->max_sfb_per_grp; scf_band++) {
549
10.3M
        if (pstr_psy_chan_out->ptr_sfb_spread_energy[scf_band_grp + scf_band] >
550
10.3M
                pstr_psy_chan_out->ptr_sfb_energy[scf_band_grp + scf_band] ||
551
10.3M
            pstr_psy_chan_out->sfb_min_snr[scf_band_grp + scf_band] > 1.0f) {
552
3.96M
          ah_flag[ch][scf_band_grp + scf_band] = NO_AH;
553
6.40M
        } else {
554
6.40M
          ah_flag[ch][scf_band_grp + scf_band] = AH_INACTIVE;
555
6.40M
        }
556
10.3M
      }
557
558
552k
      for (scf_band = pstr_psy_chan_out->max_sfb_per_grp;
559
3.70M
           scf_band < pstr_psy_chan_out->sfb_per_group; scf_band++) {
560
3.14M
        ah_flag[ch][scf_band_grp + scf_band] = NO_AH;
561
3.14M
      }
562
552k
    }
563
346k
  }
564
235k
}
565
566
static VOID iaace_reduce_thr(
567
    ixheaace_psy_out_channel **pstr_psy_out,
568
    WORD32 ah_flag[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND],
569
    FLOAT32 thr_exp[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND],
570
588k
    const FLOAT32 red_value, WORD32 num_channels, WORD32 chn) {
571
588k
  WORD32 ch, sfb_group, sfb;
572
588k
  FLOAT32 sfb_energy, sfb_threshold, sfb_thr_reduced;
573
588k
  FLOAT32 *ptr_sfb_energy_fix, *ptr_sfb_threshold_fix, *ptr_sfb_min_snr_fix, *ptr_thr_exp_fix;
574
575
1.44M
  for (ch = chn; ch < chn + num_channels; ch++) {
576
852k
    ixheaace_psy_out_channel *pstr_psy_chan_out = pstr_psy_out[ch];
577
852k
    ptr_sfb_energy_fix = pstr_psy_chan_out->ptr_sfb_energy;
578
852k
    ptr_sfb_threshold_fix = pstr_psy_chan_out->ptr_sfb_thr;
579
852k
    ptr_sfb_min_snr_fix = pstr_psy_chan_out->sfb_min_snr;
580
852k
    ptr_thr_exp_fix = &thr_exp[ch][0];
581
2.18M
    for (sfb_group = 0; sfb_group < pstr_psy_chan_out->sfb_count;
582
1.32M
         sfb_group += pstr_psy_chan_out->sfb_per_group) {
583
26.5M
      for (sfb = 0; sfb < pstr_psy_chan_out->max_sfb_per_grp; sfb++) {
584
25.2M
        sfb_energy = ptr_sfb_energy_fix[sfb_group + sfb];
585
25.2M
        sfb_threshold = ptr_sfb_threshold_fix[sfb_group + sfb];
586
25.2M
        if (sfb_energy > sfb_threshold) {
587
16.1M
          sfb_thr_reduced =
588
16.1M
              (FLOAT32)pow((ptr_thr_exp_fix[sfb_group + sfb] + red_value), INV_RED_EXP_VAL);
589
590
16.1M
          if ((sfb_thr_reduced > ptr_sfb_min_snr_fix[sfb_group + sfb] * sfb_energy) &&
591
16.1M
              (ah_flag[ch][sfb_group + sfb] != NO_AH)) {
592
7.08M
            sfb_thr_reduced =
593
7.08M
                MAX(ptr_sfb_min_snr_fix[sfb_group + sfb] * sfb_energy, sfb_threshold);
594
7.08M
            ah_flag[ch][sfb_group + sfb] = AH_ACTIVE;
595
7.08M
          }
596
16.1M
          ptr_sfb_threshold_fix[sfb_group + sfb] = sfb_thr_reduced;
597
16.1M
        }
598
25.2M
      }
599
1.32M
    }
600
852k
  }
601
588k
}
602
603
static VOID iaace_calc_pe_no_active_holes(
604
    FLOAT32 *ptr_pe, FLOAT32 *ptr_const_part, FLOAT32 *ptr_num_active_lines,
605
    ia_qc_pe_data_struct *pstr_qs_pe_data,
606
    WORD32 ah_flag[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND],
607
355k
    ixheaace_psy_out_channel **pstr_psy_out, WORD32 num_channels, WORD32 chn) {
608
355k
  WORD32 ch, sfb_group, sfb;
609
355k
  *ptr_pe = 0.0f;
610
355k
  *ptr_const_part = 0.0f;
611
355k
  *ptr_num_active_lines = 0;
612
613
864k
  for (ch = chn; ch < chn + num_channels; ch++) {
614
509k
    ixheaace_psy_out_channel *pstr_psy_chan_out = pstr_psy_out[ch];
615
509k
    ia_qc_pe_chan_data_struct *ptr_pe_chan_data = &pstr_qs_pe_data->pe_ch_data[ch];
616
617
1.29M
    for (sfb_group = 0; sfb_group < pstr_psy_chan_out->sfb_count;
618
781k
         sfb_group += pstr_psy_chan_out->sfb_per_group) {
619
15.7M
      for (sfb = 0; sfb < pstr_psy_chan_out->max_sfb_per_grp; sfb++) {
620
14.9M
        if (ah_flag[ch][sfb_group + sfb] < AH_ACTIVE) {
621
10.9M
          *ptr_pe += ptr_pe_chan_data->sfb_pe[sfb_group + sfb];
622
10.9M
          *ptr_const_part += ptr_pe_chan_data->sfb_const_part[sfb_group + sfb];
623
10.9M
          *ptr_num_active_lines += ptr_pe_chan_data->num_sfb_active_lines[sfb_group + sfb];
624
10.9M
        }
625
14.9M
      }
626
781k
    }
627
509k
  }
628
355k
}
629
630
static VOID iaace_correct_thr(
631
    ixheaace_psy_out_channel **pstr_psy_out,
632
    WORD32 ah_flag[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND],
633
    ia_qc_pe_data_struct *pstr_qs_pe_data,
634
    FLOAT32 thr_exp[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND],
635
164k
    const FLOAT32 red_value, const FLOAT32 delta_pe, WORD32 num_channels, WORD32 chn) {
636
164k
  WORD32 i, ch, sfb_group, sfb;
637
164k
  FLOAT32 delta_sfb_pe;
638
164k
  FLOAT32 thr_factor;
639
164k
  FLOAT32 sfb_pe_factors[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND],
640
164k
      norm_factor[IXHEAACE_MAX_CH_IN_BS_ELE] = {MIN_FLT_VAL};
641
164k
  FLOAT32 sfb_en, sfb_thr, sfb_thr_reduced;
642
164k
  FLOAT32 *ptr_thr_exp;
643
164k
  FLOAT32 *ptr_sfb_energy, *ptr_sfb_thr, *ptr_sfb_min_snr;
644
164k
  ixheaace_psy_out_channel *pstr_psy_chan_out = NULL;
645
164k
  ia_qc_pe_chan_data_struct *pstr_pe_chan_data = NULL;
646
647
418k
  for (ch = chn; ch < chn + num_channels; ch++) {
648
254k
    pstr_psy_chan_out = pstr_psy_out[ch];
649
254k
    pstr_pe_chan_data = &pstr_qs_pe_data->pe_ch_data[ch];
650
254k
    norm_factor[ch] = MIN_FLT_VAL;
651
254k
    ptr_thr_exp = thr_exp[ch];
652
653
700k
    for (sfb_group = 0; sfb_group < pstr_psy_chan_out->sfb_count;
654
446k
         sfb_group += pstr_psy_chan_out->sfb_per_group) {
655
8.28M
      for (sfb = 0; sfb < pstr_psy_chan_out->max_sfb_per_grp; sfb++) {
656
7.83M
        if ((ah_flag[ch][sfb_group + sfb] < AH_ACTIVE) || (delta_pe > 0)) {
657
6.66M
          sfb_pe_factors[ch][sfb_group + sfb] =
658
6.66M
              pstr_pe_chan_data->num_sfb_active_lines[sfb_group + sfb] /
659
6.66M
              (ptr_thr_exp[sfb_group + sfb] + red_value);
660
6.66M
          norm_factor[ch] += sfb_pe_factors[ch][sfb_group + sfb];
661
6.66M
        } else {
662
1.17M
          sfb_pe_factors[ch][sfb_group + sfb] = 0.0f;
663
1.17M
        }
664
7.83M
      }
665
446k
    }
666
254k
  }
667
164k
  if (num_channels > 1) {
668
89.8k
    norm_factor[chn] = norm_factor[chn] + norm_factor[chn + 1];
669
89.8k
  }
670
164k
  norm_factor[chn] = 1.0f / norm_factor[chn];
671
672
418k
  for (ch = chn; ch < chn + num_channels; ch++) {
673
254k
    pstr_psy_chan_out = pstr_psy_out[ch];
674
254k
    pstr_pe_chan_data = &pstr_qs_pe_data->pe_ch_data[ch];
675
254k
    ptr_sfb_energy = pstr_psy_chan_out->ptr_sfb_energy;
676
254k
    ptr_sfb_thr = pstr_psy_chan_out->ptr_sfb_thr;
677
254k
    ptr_sfb_min_snr = pstr_psy_chan_out->sfb_min_snr;
678
679
700k
    for (sfb_group = 0; sfb_group < pstr_psy_chan_out->sfb_count;
680
446k
         sfb_group += pstr_psy_chan_out->sfb_per_group) {
681
446k
      i = sfb_group;
682
8.28M
      for (sfb = pstr_psy_chan_out->max_sfb_per_grp - 1; sfb >= 0; sfb--, i++) {
683
7.83M
        delta_sfb_pe = sfb_pe_factors[ch][i] * norm_factor[chn] * delta_pe;
684
7.83M
        if (pstr_pe_chan_data->num_sfb_active_lines[i] > (FLOAT32)0.5f) {
685
4.17M
          sfb_en = ptr_sfb_energy[i];
686
4.17M
          sfb_thr = ptr_sfb_thr[i];
687
4.17M
          thr_factor = MIN(-delta_sfb_pe / pstr_pe_chan_data->num_sfb_active_lines[i], 20.f);
688
4.17M
          thr_factor = (FLOAT32)pow(2.0f, thr_factor);
689
4.17M
          sfb_thr_reduced = sfb_thr * thr_factor;
690
691
4.17M
          if ((sfb_thr_reduced > ptr_sfb_min_snr[i] * sfb_en) &&
692
4.17M
              (ah_flag[ch][i] == AH_INACTIVE)) {
693
56.1k
            sfb_thr_reduced = MAX(ptr_sfb_min_snr[i] * sfb_en, sfb_thr);
694
56.1k
            ah_flag[ch][i] = AH_ACTIVE;
695
56.1k
          }
696
4.17M
          ptr_sfb_thr[i] = sfb_thr_reduced;
697
4.17M
        }
698
7.83M
      }
699
446k
    }
700
254k
  }
701
164k
}
702
703
static VOID iaace_reduce_min_snr(
704
    ixheaace_psy_out_channel **pstr_psy_out, ia_qc_pe_data_struct *pstr_qs_pe_data,
705
    WORD32 ah_flag[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND],
706
71.1k
    const FLOAT32 desired_pe, WORD32 num_channels, WORD32 chn) {
707
71.1k
  WORD32 sfb, sfb_sub_win, ch;
708
71.1k
  FLOAT32 delta_pe;
709
710
71.1k
  sfb_sub_win = pstr_psy_out[chn]->max_sfb_per_grp;
711
712
1.99M
  while (pstr_qs_pe_data->pe > desired_pe && sfb_sub_win > 0) {
713
1.92M
    sfb_sub_win--;
714
3.92M
    for (sfb = sfb_sub_win; sfb < pstr_psy_out[chn]->sfb_count;
715
2.00M
         sfb += pstr_psy_out[chn]->sfb_per_group) {
716
4.52M
      for (ch = chn; ch < chn + num_channels; ch++) {
717
2.52M
        if (ah_flag[ch][sfb] != NO_AH && pstr_psy_out[ch]->sfb_min_snr[sfb] < MIN_SNR_LIMIT) {
718
659k
          pstr_psy_out[ch]->sfb_min_snr[sfb] = MIN_SNR_LIMIT;
719
659k
          pstr_psy_out[ch]->ptr_sfb_thr[sfb] =
720
659k
              pstr_psy_out[ch]->ptr_sfb_energy[sfb] * pstr_psy_out[ch]->sfb_min_snr[sfb];
721
659k
          delta_pe = pstr_qs_pe_data->pe_ch_data[ch].sfb_lines[sfb] * 1.5f -
722
659k
                     pstr_qs_pe_data->pe_ch_data[ch].sfb_pe[sfb];
723
659k
          pstr_qs_pe_data->pe += delta_pe;
724
659k
          pstr_qs_pe_data->pe_ch_data[ch].pe += delta_pe;
725
659k
        }
726
2.52M
      }
727
2.00M
      if (pstr_qs_pe_data->pe <= desired_pe) {
728
4.03k
        break;
729
4.03k
      }
730
2.00M
    }
731
1.92M
  }
732
71.1k
}
733
734
static VOID iaace_allow_more_holes(
735
    ixheaace_psy_out_channel **pstr_psy_out, ia_qc_pe_data_struct *pstr_qs_pe_data,
736
    WORD32 ah_flag[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND],
737
    const ia_ah_param_struct *pstr_str_ah_param, const FLOAT32 desired_pe, WORD32 num_channels,
738
71.1k
    WORD32 chn) {
739
71.1k
  WORD32 sfb, ch;
740
71.1k
  FLOAT32 act_pe = pstr_qs_pe_data->pe;
741
742
71.1k
  if (num_channels == 2 &&
743
71.1k
      pstr_psy_out[chn]->window_sequence == pstr_psy_out[chn + 1]->window_sequence) {
744
21.5k
    ixheaace_psy_out_channel *pstr_psy_out_left = pstr_psy_out[chn];
745
21.5k
    ixheaace_psy_out_channel *pstr_psy_out_right = pstr_psy_out[chn + 1];
746
747
793k
    for (sfb = 0; sfb < pstr_psy_out_left->sfb_count; sfb++) {
748
773k
      if (pstr_psy_out[chn]->ms_used[sfb]) {
749
557k
        if (ah_flag[chn + 1][sfb] != NO_AH &&
750
557k
            0.4f * pstr_psy_out_left->sfb_min_snr[sfb] * pstr_psy_out_left->ptr_sfb_energy[sfb] >
751
81.1k
                pstr_psy_out_right->ptr_sfb_energy[sfb]) {
752
7.87k
          ah_flag[chn + 1][sfb] = NO_AH;
753
754
7.87k
          pstr_psy_out_right->ptr_sfb_thr[sfb] = 2.0f * pstr_psy_out_right->ptr_sfb_energy[sfb];
755
756
7.87k
          act_pe -= pstr_qs_pe_data->pe_ch_data[chn + 1].sfb_pe[sfb];
757
549k
        } else {
758
549k
          if (ah_flag[chn][sfb] != NO_AH && 0.4f * pstr_psy_out_right->sfb_min_snr[sfb] *
759
381k
                                                    pstr_psy_out_right->ptr_sfb_energy[sfb] >
760
381k
                                                pstr_psy_out_left->ptr_sfb_energy[sfb]) {
761
3.39k
            ah_flag[chn][sfb] = NO_AH;
762
763
3.39k
            pstr_psy_out_left->ptr_sfb_thr[sfb] = 2.0f * pstr_psy_out_left->ptr_sfb_energy[sfb];
764
765
3.39k
            act_pe -= pstr_qs_pe_data->pe_ch_data[chn].sfb_pe[sfb];
766
3.39k
          }
767
549k
        }
768
557k
        if (act_pe < desired_pe) {
769
1.46k
          break;
770
1.46k
        }
771
557k
      }
772
773k
    }
773
21.5k
  }
774
71.1k
  if (act_pe > desired_pe) {
775
66.7k
    WORD32 start_sfb[IXHEAACE_MAX_CH_IN_BS_ELE] = {0};
776
66.7k
    FLOAT32 average_energy, min_energy;
777
66.7k
    WORD32 ah_cnt;
778
66.7k
    WORD32 en_idx;
779
66.7k
    FLOAT32 energy[4];
780
66.7k
    WORD32 min_sfb, max_sfb;
781
66.7k
    WORD32 done;
782
783
153k
    for (ch = chn; ch < chn + num_channels; ch++) {
784
86.8k
      if (pstr_psy_out[ch]->window_sequence != SHORT_WINDOW) {
785
82.7k
        start_sfb[ch] = pstr_str_ah_param->start_sfb_long;
786
82.7k
      } else {
787
4.09k
        start_sfb[ch] = pstr_str_ah_param->start_sfb_short;
788
4.09k
      }
789
86.8k
    }
790
791
66.7k
    average_energy = 0.0f;
792
66.7k
    min_energy = MAX_FLT_VAL;
793
66.7k
    ah_cnt = 0;
794
153k
    for (ch = chn; ch < chn + num_channels; ch++) {
795
86.8k
      ixheaace_psy_out_channel *pstr_psy_chan_out = pstr_psy_out[ch];
796
3.05M
      for (sfb = start_sfb[ch]; sfb < pstr_psy_chan_out->sfb_count; sfb++) {
797
2.97M
        if ((ah_flag[ch][sfb] != NO_AH) &&
798
2.97M
            (pstr_psy_chan_out->ptr_sfb_energy[sfb] > pstr_psy_chan_out->ptr_sfb_thr[sfb])) {
799
1.45M
          min_energy = MIN(min_energy, pstr_psy_chan_out->ptr_sfb_energy[sfb]);
800
1.45M
          average_energy += pstr_psy_chan_out->ptr_sfb_energy[sfb];
801
1.45M
          ah_cnt++;
802
1.45M
        }
803
2.97M
      }
804
86.8k
    }
805
806
66.7k
    average_energy = MIN(MAX_FLT_VAL, average_energy / (ah_cnt + MIN_FLT_VAL));
807
808
333k
    for (en_idx = 0; en_idx < 4; en_idx++) {
809
267k
      energy[en_idx] = min_energy * (FLOAT32)pow(average_energy / (min_energy + MIN_FLT_VAL),
810
267k
                                                 (2 * en_idx + 1) / 7.0f);
811
267k
    }
812
66.7k
    max_sfb = pstr_psy_out[chn]->sfb_count - 1;
813
66.7k
    min_sfb = start_sfb[chn];
814
815
66.7k
    if (num_channels == 2) {
816
20.0k
      max_sfb = MAX(max_sfb, pstr_psy_out[chn + 1]->sfb_count - 1);
817
818
20.0k
      min_sfb = MIN(min_sfb, start_sfb[chn + 1]);
819
20.0k
    }
820
821
66.7k
    sfb = max_sfb;
822
66.7k
    en_idx = 0;
823
66.7k
    done = 0;
824
3.22M
    while (!done) {
825
7.11M
      for (ch = chn; ch < chn + num_channels; ch++) {
826
4.01M
        ixheaace_psy_out_channel *pstr_psy_chan_out = pstr_psy_out[ch];
827
4.01M
        if (sfb >= start_sfb[ch] && sfb < pstr_psy_chan_out->sfb_count) {
828
4.01M
          if (ah_flag[ch][sfb] != NO_AH &&
829
4.01M
              pstr_psy_chan_out->ptr_sfb_energy[sfb] < energy[en_idx]) {
830
506k
            ah_flag[ch][sfb] = NO_AH;
831
506k
            pstr_psy_chan_out->ptr_sfb_thr[sfb] = 2.0f * pstr_psy_chan_out->ptr_sfb_energy[sfb];
832
506k
            act_pe -= pstr_qs_pe_data->pe_ch_data[ch].sfb_pe[sfb];
833
506k
          }
834
835
4.01M
          if (act_pe < desired_pe) {
836
54.8k
            done = 1;
837
54.8k
            break;
838
54.8k
          }
839
4.01M
        }
840
4.01M
      }
841
3.15M
      sfb--;
842
3.15M
      if (sfb < min_sfb) {
843
88.2k
        sfb = max_sfb;
844
88.2k
        en_idx++;
845
88.2k
        if (en_idx >= 4) {
846
12.0k
          done = 1;
847
12.0k
        }
848
88.2k
      }
849
3.15M
    }
850
66.7k
  }
851
71.1k
}
852
853
static VOID iaace_adapt_thr_to_pe(
854
    ixheaace_psy_out_channel **pstr_psy_out,
855
    ia_qc_pe_data_struct *pstr_qs_pe_data, const FLOAT32 desired_pe,
856
    ia_ah_param_struct *pstr_ah_param, ia_min_snr_adapt_param_struct *pstr_msa_param,
857
235k
    WORD32 num_channels, WORD32 chn, WORD32 aot) {
858
235k
  FLOAT32 no_red_pe, red_pe, red_pe_no_ah;
859
235k
  FLOAT32 const_part, const_part_no_ah;
860
235k
  FLOAT32 num_active_lines, num_active_lines_no_ah;
861
235k
  FLOAT32 desired_pe_no_ah;
862
235k
  FLOAT32 redval = 0.0f;
863
235k
  WORD32 ah_flag[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND];
864
235k
  FLOAT32 thr_exp[IXHEAACE_MAX_CH_IN_BS_ELE][MAXIMUM_GROUPED_SCALE_FACTOR_BAND];
865
235k
  WORD32 iteration;
866
867
235k
  iaace_calc_thr_exp(thr_exp, pstr_psy_out, num_channels, chn);
868
235k
  iaace_adapt_min_snr(pstr_psy_out, pstr_msa_param, num_channels, chn);
869
235k
  iaace_init_avoid_hole_flag(ah_flag, pstr_psy_out, pstr_ah_param, num_channels, chn, aot);
870
871
235k
  no_red_pe = pstr_qs_pe_data->pe;
872
235k
  const_part = pstr_qs_pe_data->const_part;
873
235k
  num_active_lines = pstr_qs_pe_data->num_active_lines;
874
235k
  if (num_active_lines > FLT_EPSILON) {
875
235k
    FLOAT32 avg_thr_exp =
876
235k
        (FLOAT32)pow(2.0f, (const_part - no_red_pe) / (INV_RED_EXP_VAL * num_active_lines));
877
235k
    redval = (FLOAT32)pow(2.0f, (const_part - desired_pe) / (INV_RED_EXP_VAL * num_active_lines))
878
235k
             - avg_thr_exp;
879
235k
    redval = MAX(0.0f, redval);
880
235k
    iaace_reduce_thr(pstr_psy_out, ah_flag, thr_exp, redval, num_channels, chn);
881
235k
  }
882
883
235k
  iaace_calc_sfb_pe_data(pstr_qs_pe_data, pstr_psy_out, num_channels, chn);
884
235k
  red_pe = pstr_qs_pe_data->pe;
885
886
235k
  iteration = 0;
887
355k
  do {
888
355k
    iaace_calc_pe_no_active_holes(&red_pe_no_ah, &const_part_no_ah, &num_active_lines_no_ah,
889
355k
                                  pstr_qs_pe_data, ah_flag, pstr_psy_out, num_channels, chn);
890
891
355k
    desired_pe_no_ah = MAX(desired_pe - (red_pe - red_pe_no_ah), 0);
892
893
355k
    if (num_active_lines_no_ah > FLT_EPSILON) {
894
352k
      FLOAT32 avg_thr_exp = (FLOAT32)pow(
895
352k
          2.0f, (const_part_no_ah - red_pe_no_ah) / (INV_RED_EXP_VAL * num_active_lines_no_ah));
896
352k
      redval += (FLOAT32)pow(2.0f, (const_part_no_ah - desired_pe_no_ah) /
897
352k
                                       (INV_RED_EXP_VAL * num_active_lines_no_ah)) -
898
352k
                avg_thr_exp;
899
352k
      redval = MAX(0.0f, redval);
900
352k
      iaace_reduce_thr(pstr_psy_out, ah_flag, thr_exp, redval, num_channels, chn);
901
352k
    }
902
903
355k
    iaace_calc_sfb_pe_data(pstr_qs_pe_data, pstr_psy_out, num_channels, chn);
904
905
355k
    red_pe = pstr_qs_pe_data->pe;
906
355k
    iteration++;
907
355k
  } while ((fabs(red_pe - desired_pe) > (0.05f) * desired_pe) && (iteration < 2));
908
909
235k
  if (red_pe < 1.15f * desired_pe) {
910
164k
    iaace_correct_thr(pstr_psy_out, ah_flag, pstr_qs_pe_data, thr_exp, redval,
911
164k
                      desired_pe - red_pe, num_channels, chn);
912
164k
  } else {
913
71.1k
    iaace_reduce_min_snr(pstr_psy_out, pstr_qs_pe_data, ah_flag, 1.05f * desired_pe, num_channels,
914
71.1k
                         chn);
915
71.1k
    iaace_allow_more_holes(pstr_psy_out, pstr_qs_pe_data, ah_flag, pstr_ah_param,
916
71.1k
                           1.05f * desired_pe, num_channels, chn);
917
71.1k
  }
918
235k
}
919
920
VOID iaace_adjust_threshold(ia_adj_thr_state_struct *pstr_adj_thr_state,
921
                            ia_adj_thr_elem_struct *pstr_adj_thr_elem,
922
                            ixheaace_psy_out_channel **pstr_psy_out,
923
                            FLOAT32 *ptr_ch_bit_dist, ixheaace_qc_out_element *pstr_qc_out_el,
924
                            const WORD32 avg_bits, const WORD32 bitres_bits,
925
                            const WORD32 max_bitres_bits, const WORD32 side_info_bits,
926
                            FLOAT32 *max_bit_fac, FLOAT32 *ptr_sfb_n_relevant_lines,
927
                            FLOAT32 *ptr_sfb_ld_energy, WORD32 num_channels, WORD32 chn,
928
379k
                            WORD32 aot, WORD8 *ptr_scratch) {
929
379k
  FLOAT32 no_red_pe, granted_pe, granted_pe_corr;
930
379k
  WORD32 curr_win_sequence;
931
379k
  ia_qc_pe_data_struct *pstr_qc_pe_data = (ia_qc_pe_data_struct *)ptr_scratch;
932
379k
  FLOAT32 bit_factor;
933
379k
  WORD32 ch;
934
935
925k
  for (ch = chn; ch < chn + num_channels; ch++) {
936
546k
    pstr_qc_pe_data->pe_ch_data[ch].sfb_lines =
937
546k
        ptr_sfb_n_relevant_lines + MAXIMUM_GROUPED_SCALE_FACTOR_BAND * ch;
938
546k
    pstr_qc_pe_data->pe_ch_data[ch].sfb_ld_energy =
939
546k
        ptr_sfb_ld_energy + MAXIMUM_GROUPED_SCALE_FACTOR_BAND * ch;
940
546k
  }
941
379k
  pstr_qc_pe_data->offset = pstr_adj_thr_elem->pe_offset;
942
943
379k
  iaace_calc_sfb_pe_data(pstr_qc_pe_data, pstr_psy_out, num_channels, chn);
944
945
379k
  no_red_pe = pstr_qc_pe_data->pe;
946
947
379k
  curr_win_sequence = LONG_WINDOW;
948
379k
  if (num_channels == 2) {
949
167k
    if ((pstr_psy_out[chn]->window_sequence == SHORT_WINDOW) ||
950
167k
        (pstr_psy_out[chn + 1]->window_sequence == SHORT_WINDOW)) {
951
40.1k
      curr_win_sequence = SHORT_WINDOW;
952
40.1k
    }
953
211k
  } else {
954
211k
    curr_win_sequence = pstr_psy_out[chn]->window_sequence;
955
211k
  }
956
957
379k
  bit_factor = iaace_bitres_calc_bitfac(
958
379k
      bitres_bits, max_bitres_bits, no_red_pe + 5.0f * side_info_bits, curr_win_sequence,
959
379k
      avg_bits, *max_bit_fac, pstr_adj_thr_state, pstr_adj_thr_elem);
960
961
379k
  granted_pe = bit_factor * iaace_bits_to_pe((FLOAT32)avg_bits);
962
963
379k
  iaace_calc_pe_correction(&(pstr_adj_thr_elem->pe_correction_fac), MIN(granted_pe, no_red_pe),
964
379k
                           pstr_adj_thr_elem->pe_last, pstr_adj_thr_elem->dyn_bits_last);
965
966
379k
  granted_pe_corr = granted_pe * pstr_adj_thr_elem->pe_correction_fac;
967
968
379k
  if (granted_pe_corr < no_red_pe) {
969
235k
    iaace_adapt_thr_to_pe(pstr_psy_out, pstr_qc_pe_data, granted_pe_corr,
970
235k
                          &pstr_adj_thr_elem->str_ah_param,
971
235k
                          &pstr_adj_thr_elem->str_min_snr_adapt_params, num_channels, chn, aot);
972
235k
  }
973
974
925k
  for (ch = chn; ch < chn + num_channels; ch++) {
975
546k
    FLOAT32 tmp_var, temp1;
976
546k
    if (pstr_qc_pe_data->pe) {
977
516k
      tmp_var = 1.0f - num_channels * 0.2f;
978
516k
      temp1 = pstr_qc_pe_data->pe_ch_data[ch].pe / pstr_qc_pe_data->pe;
979
516k
      temp1 = temp1 * tmp_var;
980
516k
      ptr_ch_bit_dist[ch] = temp1 + 0.2f;
981
516k
      if (ptr_ch_bit_dist[ch] < 0.2f) {
982
0
        ptr_ch_bit_dist[ch] = 0.2f;
983
0
      }
984
516k
    } else {
985
30.6k
      ptr_ch_bit_dist[ch] = 0.2f;
986
30.6k
    }
987
546k
  }
988
989
379k
  pstr_qc_out_el->pe = no_red_pe;
990
379k
  pstr_adj_thr_elem->pe_last = granted_pe;
991
379k
}