Coverage Report

Created: 2025-11-09 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/ixheaace_fd_qc_adjthr.c
Line
Count
Source
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 <string.h>
22
#include <math.h>
23
#include <stdlib.h>
24
#include <limits.h>
25
#include <float.h>
26
#include "iusace_type_def.h"
27
#include "ixheaac_error_standards.h"
28
#include "ixheaace_error_codes.h"
29
#include "ixheaace_psy_const.h"
30
#include "ixheaace_tns.h"
31
#include "ixheaace_tns_params.h"
32
#include "ixheaace_rom.h"
33
#include "iusace_block_switch_const.h"
34
#include "iusace_cnst.h"
35
#include "iusace_rom.h"
36
#include "ixheaace_mps_common_define.h"
37
#include "iusace_bitbuffer.h"
38
#include "impd_drc_common_enc.h"
39
#include "impd_drc_uni_drc.h"
40
#include "impd_drc_api.h"
41
#include "impd_drc_uni_drc_eq.h"
42
#include "impd_drc_uni_drc_filter_bank.h"
43
#include "impd_drc_gain_enc.h"
44
#include "impd_drc_struct_def.h"
45
46
#include "ixheaace_memory_standards.h"
47
#include "iusace_tns_usac.h"
48
#include "iusace_psy_mod.h"
49
#include "iusace_config.h"
50
#include "ixheaace_adjust_threshold_data.h"
51
#include "iusace_fd_qc_util.h"
52
#include "iusace_fd_qc_adjthr.h"
53
#include "ixheaace_aac_constants.h"
54
#include "ixheaace_sbr_def.h"
55
56
631k
FLOAT32 iusace_bits_to_pe(const FLOAT32 bits) { return (bits * 1.18f); }
57
58
VOID iusace_adj_thr_init(ia_adj_thr_elem_struct *pstr_adj_thr_ele, const FLOAT32 mean_pe,
59
3.59k
                         WORD32 ch_bitrate) {
60
3.59k
  ia_min_snr_adapt_param_struct *pstr_min_snr_params =
61
3.59k
      &pstr_adj_thr_ele->str_min_snr_adapt_params;
62
63
3.59k
  pstr_adj_thr_ele->pe_min = (FLOAT32)0.8f * mean_pe;
64
3.59k
  pstr_adj_thr_ele->pe_max = (FLOAT32)1.2f * mean_pe;
65
3.59k
  pstr_adj_thr_ele->pe_offset = 0.0f;
66
67
3.59k
  if (ch_bitrate < 32000) {
68
46
    pstr_adj_thr_ele->pe_offset =
69
46
        MAX((FLOAT32)50.0f, (FLOAT32)(100.0f) - (FLOAT32)(100.0f / 32000) * (FLOAT32)ch_bitrate);
70
46
  }
71
72
3.59k
  if (ch_bitrate > 20000) {
73
3.56k
    pstr_adj_thr_ele->str_ah_param.modify_min_snr = TRUE;
74
3.56k
    pstr_adj_thr_ele->str_ah_param.start_sfb_long = 15;
75
3.56k
    pstr_adj_thr_ele->str_ah_param.start_sfb_short = 3;
76
3.56k
  } else {
77
30
    pstr_adj_thr_ele->str_ah_param.modify_min_snr = FALSE;
78
30
    pstr_adj_thr_ele->str_ah_param.start_sfb_long = 0;
79
30
    pstr_adj_thr_ele->str_ah_param.start_sfb_short = 0;
80
30
  }
81
82
3.59k
  pstr_min_snr_params->max_red = (FLOAT32)0.25f;
83
84
3.59k
  pstr_min_snr_params->start_ratio = (FLOAT32)10.0f;
85
86
3.59k
  pstr_min_snr_params->max_ratio = (FLOAT32)1000.0f;
87
88
3.59k
  pstr_min_snr_params->red_ratio_fac =
89
3.59k
      (1.0f - pstr_min_snr_params->max_red) /
90
3.59k
      (10.0f * (FLOAT32)log10(pstr_min_snr_params->start_ratio / pstr_min_snr_params->max_ratio));
91
92
3.59k
  pstr_min_snr_params->red_offs = 1.0f - pstr_min_snr_params->red_ratio_fac * 10.0f *
93
3.59k
                                             (FLOAT32)log10(pstr_min_snr_params->start_ratio);
94
95
3.59k
  pstr_adj_thr_ele->pe_last = (FLOAT32)0.0f;
96
3.59k
  pstr_adj_thr_ele->dyn_bits_last = 0;
97
3.59k
  pstr_adj_thr_ele->pe_correction_fac = (FLOAT32)1.0f;
98
3.59k
}
99
100
static VOID iusace_calc_sfb_pe_data(ia_qc_pe_data_struct *pstr_qc_pe_data,
101
                                    ia_psy_mod_out_data_struct *pstr_psy_out, WORD32 num_channels,
102
525k
                                    WORD32 chn) {
103
525k
  WORD32 ch, idx = 0;
104
525k
  WORD32 scf_band_grp;
105
525k
  FLOAT32 num_lines;
106
525k
  FLOAT32 ld_thr, ld_ratio;
107
525k
  WORD32 i = 0, scf;
108
525k
  WORD32 sfb_count;
109
525k
  WORD32 scf_band_per_grp;
110
525k
  WORD32 max_sfb_per_grp;
111
525k
  FLOAT32 *ptr_sfb_energy;
112
525k
  FLOAT32 *ptr_sfb_thr;
113
525k
  ia_qc_pe_chan_data_struct *str_qc_pe_chan_data;
114
115
525k
  pstr_qc_pe_data->pe = pstr_qc_pe_data->offset;
116
525k
  pstr_qc_pe_data->const_part = 0.0f;
117
525k
  pstr_qc_pe_data->num_active_lines = 0.0f;
118
119
1.48M
  for (ch = chn; ch < chn + num_channels; ch++) {
120
960k
    sfb_count = pstr_psy_out[ch].sfb_count;
121
960k
    scf_band_per_grp = pstr_psy_out[ch].sfb_per_group;
122
960k
    max_sfb_per_grp = pstr_psy_out[ch].max_sfb_per_grp;
123
960k
    ptr_sfb_energy = pstr_psy_out[ch].ptr_sfb_energy;
124
960k
    ptr_sfb_thr = pstr_psy_out[ch].ptr_sfb_thr;
125
960k
    str_qc_pe_chan_data = &pstr_qc_pe_data->pe_ch_data[idx];
126
960k
    str_qc_pe_chan_data->pe = 0;
127
960k
    str_qc_pe_chan_data->num_active_lines = 0;
128
960k
    str_qc_pe_chan_data->const_part = 0;
129
130
2.88M
    for (scf_band_grp = 0; scf_band_grp < sfb_count; scf_band_grp += scf_band_per_grp) {
131
1.92M
      i = scf_band_grp;
132
39.9M
      for (scf = max_sfb_per_grp - 1; scf >= 0; scf--, i++) {
133
38.0M
        if (ptr_sfb_energy[i] > ptr_sfb_thr[i]) {
134
23.0M
          ld_thr = (FLOAT32)log(ptr_sfb_thr[i]) * LOG2_1;
135
23.0M
          ld_ratio = str_qc_pe_chan_data->sfb_ld_energy[i] - ld_thr;
136
23.0M
          num_lines = str_qc_pe_chan_data->sfb_lines[i];
137
23.0M
          if (ld_ratio >= PE_C1) {
138
17.3M
            str_qc_pe_chan_data->sfb_pe[i] = num_lines * ld_ratio;
139
17.3M
            str_qc_pe_chan_data->sfb_const_part[i] =
140
17.3M
                num_lines * str_qc_pe_chan_data->sfb_ld_energy[i];
141
17.3M
          } else {
142
5.73M
            str_qc_pe_chan_data->sfb_pe[i] = num_lines * (PE_C2 + PE_C3 * ld_ratio);
143
5.73M
            str_qc_pe_chan_data->sfb_const_part[i] =
144
5.73M
                num_lines * (PE_C2 + PE_C3 * str_qc_pe_chan_data->sfb_ld_energy[i]);
145
5.73M
            num_lines = num_lines * PE_C3;
146
5.73M
          }
147
23.0M
          str_qc_pe_chan_data->num_sfb_active_lines[i] = num_lines;
148
23.0M
        } else {
149
14.9M
          str_qc_pe_chan_data->sfb_pe[i] = 0.0f;
150
14.9M
          str_qc_pe_chan_data->sfb_const_part[i] = 0.0f;
151
14.9M
          str_qc_pe_chan_data->num_sfb_active_lines[i] = 0.0;
152
14.9M
        }
153
154
38.0M
        str_qc_pe_chan_data->pe += str_qc_pe_chan_data->sfb_pe[i];
155
38.0M
        str_qc_pe_chan_data->const_part += str_qc_pe_chan_data->sfb_const_part[i];
156
38.0M
        str_qc_pe_chan_data->num_active_lines += str_qc_pe_chan_data->num_sfb_active_lines[i];
157
38.0M
      }
158
1.92M
    }
159
960k
    pstr_qc_pe_data->pe += str_qc_pe_chan_data->pe;
160
960k
    pstr_qc_pe_data->const_part += str_qc_pe_chan_data->const_part;
161
960k
    pstr_qc_pe_data->num_active_lines += str_qc_pe_chan_data->num_active_lines;
162
960k
    pstr_psy_out[ch].pe = pstr_qc_pe_data->pe;
163
960k
    idx++;
164
960k
  }
165
525k
  return;
166
525k
}
167
168
239k
static VOID iusace_adj_pe_minmax(const FLOAT32 curr_pe, FLOAT32 *pe_min, FLOAT32 *pe_max) {
169
239k
  FLOAT32 min_hi_fac = 0.3f, max_hi_fac = 1.0f, min_low_fac = 0.14f, max_low_fac = 0.07f;
170
239k
  FLOAT32 diff;
171
239k
  FLOAT32 min_diff = curr_pe * (FLOAT32)0.1666666667f;
172
173
239k
  if (curr_pe > *pe_max) {
174
17.6k
    diff = (curr_pe - *pe_max);
175
17.6k
    *pe_min += diff * min_hi_fac;
176
17.6k
    *pe_max += diff * max_hi_fac;
177
222k
  } else {
178
222k
    if (curr_pe < *pe_min) {
179
154k
      diff = (*pe_min - curr_pe);
180
154k
      *pe_min -= diff * min_low_fac;
181
154k
      *pe_max -= diff * max_low_fac;
182
154k
    } else {
183
67.8k
      *pe_min += (curr_pe - *pe_min) * min_hi_fac;
184
67.8k
      *pe_max -= (*pe_max - curr_pe) * max_low_fac;
185
67.8k
    }
186
222k
  }
187
188
239k
  if ((*pe_max - *pe_min) < min_diff) {
189
14.1k
    FLOAT32 low_part, high_part;
190
14.1k
    low_part = MAX((FLOAT32)0.0f, curr_pe - *pe_min);
191
14.1k
    high_part = MAX((FLOAT32)0.0f, *pe_max - curr_pe);
192
14.1k
    *pe_max = curr_pe + high_part / (low_part + high_part) * min_diff;
193
14.1k
    *pe_min = curr_pe - low_part / (low_part + high_part) * min_diff;
194
14.1k
    *pe_min = MAX((FLOAT32)0.0f, *pe_min);
195
14.1k
  }
196
197
239k
  return;
198
239k
}
199
200
static FLOAT32 iusace_bitres_calc_bitfac(const WORD32 bitres_bits, const WORD32 max_bitres_bits,
201
                                         const FLOAT32 pe, const WORD32 win_seq,
202
                                         const WORD32 avg_bits, const FLOAT32 max_bit_fac,
203
239k
                                         ia_adj_thr_elem_struct *pstr_adj_thr_elem) {
204
239k
  FLOAT32 pex;
205
239k
  FLOAT32 fill_lvl = 0.0f;
206
239k
  FLOAT32 bit_save, bit_spend, bitres_factor;
207
208
239k
  if (max_bitres_bits) {
209
207k
    fill_lvl = (FLOAT32)bitres_bits / max_bitres_bits;
210
207k
  }
211
212
239k
  if (win_seq != EIGHT_SHORT_SEQUENCE) {
213
160k
    fill_lvl = MAX(fill_lvl, CLIP_SAVE_LO_LONG);
214
160k
    fill_lvl = MIN(fill_lvl, CLIP_SAVE_HI_LONG);
215
160k
    bit_save = MAX_BITS_SAVE_LONG - (BITS_SAVE_RATIO_LONG * (fill_lvl - CLIP_SAVE_LO_LONG));
216
160k
    bit_spend = MIN_BITS_SPEND_LONG + (BITS_SPEND_RATIO_LONG * (fill_lvl - CLIP_SPEND_LO_LONG));
217
160k
  } else {
218
79.5k
    fill_lvl = MAX(fill_lvl, CLIP_SPEND_LO_SHORT);
219
79.5k
    fill_lvl = MIN(fill_lvl, CLIP_SPEND_HI_SHORT);
220
79.5k
    bit_save = MAX_BITS_SAVE_SHORT - (BITS_SAVE_RATIO_SHORT * (fill_lvl - CLIP_SAVE_LO_SHORT));
221
79.5k
    bit_spend =
222
79.5k
        MIN_BITS_SPEND_SHORT + (BITS_SPEND_RATIO_SHORT * (fill_lvl - CLIP_SPEND_LO_SHORT));
223
79.5k
  }
224
225
239k
  pex = MAX(pe, pstr_adj_thr_elem->pe_min);
226
239k
  pex = MIN(pex, pstr_adj_thr_elem->pe_max);
227
228
239k
  bitres_factor =
229
239k
      (FLOAT32)1.0f - bit_save +
230
239k
      ((bit_spend + bit_save) / (pstr_adj_thr_elem->pe_max - pstr_adj_thr_elem->pe_min)) *
231
239k
          (pex - pstr_adj_thr_elem->pe_min);
232
239k
  bitres_factor = MIN(bitres_factor,
233
239k
                      (FLOAT32)1.0f - (FLOAT32)0.3f + (FLOAT32)bitres_bits / (FLOAT32)avg_bits);
234
235
239k
  bitres_factor = MIN(bitres_factor, max_bit_fac);
236
239k
  iusace_adj_pe_minmax(pe, &pstr_adj_thr_elem->pe_min, &pstr_adj_thr_elem->pe_max);
237
238
239k
  return bitres_factor;
239
239k
}
240
241
static VOID iusace_calc_pe_correction(FLOAT32 *correction_fac, const FLOAT32 pe_act,
242
239k
                                      const FLOAT32 pe_last, const WORD32 bits_prev) {
243
239k
  if ((bits_prev > 0) && (pe_act < (FLOAT32)1.5f * pe_last) &&
244
224k
      (pe_act > (FLOAT32)0.7f * pe_last) &&
245
148k
      ((FLOAT32)1.2f * iusace_bits_to_pe((FLOAT32)bits_prev) > pe_last) &&
246
119k
      ((FLOAT32)0.65f * iusace_bits_to_pe((FLOAT32)bits_prev) < pe_last)) {
247
111k
    FLOAT32 new_fac = pe_last / iusace_bits_to_pe((FLOAT32)bits_prev);
248
249
111k
    if (new_fac < (FLOAT32)1.0f) {
250
55.0k
      new_fac = MIN((FLOAT32)1.1f * new_fac, (FLOAT32)1.0f);
251
55.0k
      new_fac = MAX(new_fac, (FLOAT32)0.85f);
252
56.3k
    } else {
253
56.3k
      new_fac = MAX((FLOAT32)0.9f * new_fac, (FLOAT32)1.0f);
254
56.3k
      new_fac = MIN(new_fac, (FLOAT32)1.15f);
255
56.3k
    }
256
111k
    if (((new_fac > (FLOAT32)1.0f) && (*correction_fac < (FLOAT32)1.0f)) ||
257
110k
        ((new_fac < (FLOAT32)1.0f) && (*correction_fac > (FLOAT32)1.0f))) {
258
2.91k
      *correction_fac = (FLOAT32)1.0f;
259
2.91k
    }
260
261
111k
    if ((*correction_fac < (FLOAT32)1.0f && new_fac < *correction_fac) ||
262
87.3k
        (*correction_fac > (FLOAT32)1.0f && new_fac > *correction_fac))
263
28.4k
      *correction_fac = (FLOAT32)0.85f * (*correction_fac) + (FLOAT32)0.15f * new_fac;
264
82.9k
    else
265
82.9k
      *correction_fac = (FLOAT32)0.7f * (*correction_fac) + (FLOAT32)0.3f * new_fac;
266
267
111k
    *correction_fac = MIN(*correction_fac, (FLOAT32)1.15f);
268
111k
    *correction_fac = MAX(*correction_fac, (FLOAT32)0.85f);
269
128k
  } else {
270
128k
    *correction_fac = (FLOAT32)1.0f;
271
128k
  }
272
273
239k
  return;
274
239k
}
275
276
static VOID iusace_calc_thr_exp(FLOAT32 **thr_exp, ia_psy_mod_out_data_struct *pstr_psy_out,
277
128k
                                WORD32 num_chans, WORD32 chn) {
278
128k
  WORD32 sfb, ch, scf_band_grp, idx = 0;
279
128k
  ia_psy_mod_out_data_struct *pstr_psy_chan_out;
280
128k
  FLOAT32 *scf_band_thr;
281
128k
  FLOAT32 *ptr_thr_exp;
282
358k
  for (ch = chn; ch < chn + num_chans; ch++) {
283
230k
    pstr_psy_chan_out = &pstr_psy_out[ch];
284
230k
    ptr_thr_exp = thr_exp[idx];
285
715k
    for (scf_band_grp = 0; scf_band_grp < pstr_psy_chan_out->sfb_count;
286
485k
         scf_band_grp += pstr_psy_chan_out->sfb_per_group) {
287
485k
      FLOAT32 *thr_exp1 = &ptr_thr_exp[scf_band_grp];
288
485k
      scf_band_thr = &pstr_psy_chan_out->ptr_sfb_thr[scf_band_grp];
289
9.65M
      for (sfb = 0; sfb < pstr_psy_chan_out->max_sfb_per_grp; sfb++) {
290
9.16M
        thr_exp1[sfb] = (FLOAT32)pow(*scf_band_thr++, RED_EXP_VAL);
291
9.16M
      }
292
485k
    }
293
230k
    idx++;
294
230k
  }
295
296
128k
  return;
297
128k
}
298
299
static VOID iusace_adapt_min_snr(ia_psy_mod_out_data_struct *pstr_psy_out,
300
                                 ia_min_snr_adapt_param_struct *pstr_min_snr_params,
301
128k
                                 WORD32 num_chans, WORD32 chn) {
302
128k
  WORD32 num_sfb = 0, ch, scf_band_cnt, sfb_off, sfb;
303
128k
  FLOAT32 avg_energy = 0.0f, db_ratio, min_snr_red;
304
128k
  WORD32 i;
305
358k
  for (ch = chn; ch < chn + num_chans; ch++) {
306
230k
    ia_psy_mod_out_data_struct *pstr_psy_chan_out = &pstr_psy_out[ch];
307
308
230k
    num_sfb = 0;
309
230k
    avg_energy = 0;
310
230k
    scf_band_cnt = pstr_psy_chan_out->max_sfb_per_grp;
311
312
715k
    for (sfb_off = 0; sfb_off < pstr_psy_chan_out->sfb_count;
313
485k
         sfb_off += pstr_psy_chan_out->sfb_per_group) {
314
485k
      FLOAT32 *sfb_energy = &pstr_psy_chan_out->ptr_sfb_energy[sfb_off];
315
9.65M
      for (sfb = scf_band_cnt - 1; sfb >= 0; sfb--) {
316
9.16M
        avg_energy += sfb_energy[sfb];
317
9.16M
      }
318
485k
      num_sfb += scf_band_cnt;
319
485k
    }
320
321
230k
    if (num_sfb > 0) {
322
229k
      avg_energy /= num_sfb;
323
229k
    }
324
325
715k
    for (sfb_off = 0; sfb_off < pstr_psy_chan_out->sfb_count;
326
485k
         sfb_off += pstr_psy_chan_out->sfb_per_group) {
327
485k
      i = sfb_off;
328
9.65M
      for (sfb = scf_band_cnt - 1; sfb >= 0; sfb--, i++) {
329
9.16M
        if (pstr_min_snr_params->start_ratio * pstr_psy_chan_out->ptr_sfb_energy[i] <
330
9.16M
            avg_energy) {
331
6.16M
          db_ratio =
332
6.16M
              (FLOAT32)(10.0 * log10((MIN_FLT_VAL + avg_energy) /
333
6.16M
                                     (MIN_FLT_VAL + pstr_psy_chan_out->ptr_sfb_energy[i])));
334
6.16M
          min_snr_red =
335
6.16M
              pstr_min_snr_params->red_offs + pstr_min_snr_params->red_ratio_fac * db_ratio;
336
6.16M
          min_snr_red = MAX(min_snr_red, pstr_min_snr_params->max_red);
337
6.16M
          pstr_psy_chan_out->sfb_min_snr[i] =
338
6.16M
              (FLOAT32)pow(pstr_psy_out[ch].sfb_min_snr[i], min_snr_red);
339
6.16M
          pstr_psy_chan_out->sfb_min_snr[i] = MIN(MIN_SNR_LIMIT, pstr_psy_out[ch].sfb_min_snr[i]);
340
6.16M
        }
341
9.16M
      }
342
485k
    }
343
230k
  }
344
345
128k
  return;
346
128k
}
347
348
static VOID iusace_init_avoid_hole_flag(WORD32 **ah_flag,
349
                                        ia_psy_mod_out_data_struct *pstr_psy_out,
350
                                        ia_ah_param_struct *pstr_ah_param, WORD32 num_chans,
351
128k
                                        WORD32 chn) {
352
128k
  WORD32 ch, idx;
353
128k
  FLOAT32 sfb_energy;
354
128k
  FLOAT32 scale_spread_energy;
355
128k
  WORD32 scf_band_grp, sfb, scf_band;
356
128k
  FLOAT32 *ptr_scf_band_spread_energy, *ptr_scf_band_energy, *ptr_scf_band_min_snr;
357
358k
  for (ch = chn; ch < chn + num_chans; ch++) {
358
230k
    ia_psy_mod_out_data_struct *pstr_psy_chan_out = &pstr_psy_out[ch];
359
360
230k
    if (pstr_psy_chan_out->window_sequence != EIGHT_SHORT_SEQUENCE) {
361
142k
      scale_spread_energy = 0.5f;
362
142k
    } else {
363
87.5k
      scale_spread_energy = 0.63f;
364
87.5k
    }
365
366
715k
    for (scf_band_grp = 0; scf_band_grp < pstr_psy_chan_out->sfb_count;
367
485k
         scf_band_grp += pstr_psy_chan_out->sfb_per_group) {
368
485k
      ptr_scf_band_spread_energy = &pstr_psy_chan_out->ptr_sfb_spread_energy[scf_band_grp];
369
485k
      sfb = pstr_psy_chan_out->max_sfb_per_grp;
370
9.65M
      for (scf_band = sfb - 1; scf_band >= 0; scf_band--) {
371
9.16M
        *ptr_scf_band_spread_energy = *ptr_scf_band_spread_energy * scale_spread_energy;
372
9.16M
        ptr_scf_band_spread_energy++;
373
9.16M
      }
374
485k
    }
375
230k
  }
376
377
128k
  if (pstr_ah_param->modify_min_snr) {
378
357k
    for (ch = chn; ch < chn + num_chans; ch++) {
379
229k
      ia_psy_mod_out_data_struct *pstr_psy_chan_out = &pstr_psy_out[ch];
380
229k
      ptr_scf_band_energy = pstr_psy_chan_out->ptr_sfb_energy;
381
382
229k
      ptr_scf_band_min_snr = pstr_psy_chan_out->sfb_min_snr;
383
384
712k
      for (scf_band_grp = 0; scf_band_grp < pstr_psy_chan_out->sfb_count;
385
482k
           scf_band_grp += pstr_psy_chan_out->sfb_per_group) {
386
9.60M
        for (scf_band = 0; scf_band < pstr_psy_chan_out->max_sfb_per_grp; scf_band++) {
387
9.12M
          FLOAT32 sfb_en_m1, sfb_en_p1, avg_energy;
388
9.12M
          if (scf_band > 0) {
389
8.63M
            sfb_en_m1 = ptr_scf_band_energy[scf_band_grp + scf_band - 1];
390
8.63M
          } else {
391
482k
            sfb_en_m1 = ptr_scf_band_energy[scf_band_grp];
392
482k
          }
393
9.12M
          if (scf_band < pstr_psy_chan_out->max_sfb_per_grp - 1)
394
8.63M
            sfb_en_p1 = ptr_scf_band_energy[scf_band_grp + scf_band + 1];
395
482k
          else
396
482k
            sfb_en_p1 = ptr_scf_band_energy[scf_band_grp + scf_band];
397
398
9.12M
          avg_energy = (sfb_en_m1 + sfb_en_p1) / (FLOAT32)2.0f;
399
9.12M
          sfb_energy = ptr_scf_band_energy[scf_band_grp + scf_band];
400
401
9.12M
          if (sfb_energy > avg_energy) {
402
2.71M
            FLOAT32 temp_min_snr = MAX((FLOAT32)0.8f * avg_energy / sfb_energy, (FLOAT32)0.316f);
403
2.71M
            if (pstr_psy_chan_out->window_sequence != EIGHT_SHORT_SEQUENCE)
404
1.76M
              temp_min_snr = MAX(temp_min_snr, (FLOAT32)0.316f);
405
947k
            else
406
947k
              temp_min_snr = MAX(temp_min_snr, (FLOAT32)0.5f);
407
2.71M
            ptr_scf_band_min_snr[scf_band_grp + scf_band] =
408
2.71M
                MIN(ptr_scf_band_min_snr[scf_band_grp + scf_band], temp_min_snr);
409
2.71M
          }
410
411
9.12M
          if (((FLOAT32)2.0f * sfb_energy < avg_energy) && (sfb_energy > (FLOAT32)0.0f)) {
412
1.59M
            FLOAT32 temp_min_snr = avg_energy / ((FLOAT32)2.0f * sfb_energy) *
413
1.59M
                                   ptr_scf_band_min_snr[scf_band_grp + scf_band];
414
1.59M
            temp_min_snr = MIN((FLOAT32)0.8f, temp_min_snr);
415
1.59M
            ptr_scf_band_min_snr[scf_band_grp + scf_band] =
416
1.59M
                MIN(temp_min_snr, ptr_scf_band_min_snr[scf_band_grp + scf_band] * (FLOAT32)3.16f);
417
1.59M
          }
418
9.12M
        }
419
482k
      }
420
229k
    }
421
127k
  }
422
423
128k
  if (num_chans == 2) {
424
102k
    ia_psy_mod_out_data_struct *psy_out_mid = &pstr_psy_out[chn];
425
102k
    ia_psy_mod_out_data_struct *psy_out_side = &pstr_psy_out[chn + 1];
426
427
4.74M
    for (sfb = 0; sfb < psy_out_mid->sfb_count; sfb++) {
428
4.64M
      if (pstr_psy_out[chn].ms_used[sfb]) {
429
3.62M
        FLOAT32 sfb_en_mid = psy_out_mid->ptr_sfb_energy[sfb];
430
3.62M
        FLOAT32 sfb_en_side = psy_out_side->ptr_sfb_energy[sfb];
431
3.62M
        FLOAT32 max_sfb_en = MAX(sfb_en_mid, sfb_en_side);
432
3.62M
        FLOAT32 max_thr = 0.25f * psy_out_mid->sfb_min_snr[sfb] * max_sfb_en;
433
434
3.62M
        psy_out_mid->sfb_min_snr[sfb] = (FLOAT32)MAX(
435
3.62M
            psy_out_mid->sfb_min_snr[sfb],
436
3.62M
            MIN(MAX_FLT_VAL, ((double)max_thr / (MIN_FLT_VAL + (double)sfb_en_mid))));
437
438
3.62M
        if (psy_out_mid->ptr_sfb_energy[sfb] <= 1.0f) {
439
461k
          psy_out_mid->ptr_sfb_energy[sfb] = MIN(psy_out_mid->ptr_sfb_energy[sfb], 0.8f);
440
461k
        }
441
442
3.62M
        psy_out_side->sfb_min_snr[sfb] = (FLOAT32)MAX(
443
3.62M
            psy_out_side->sfb_min_snr[sfb],
444
3.62M
            MIN(MAX_FLT_VAL, ((double)max_thr / (MIN_FLT_VAL + (double)sfb_en_side))));
445
446
3.62M
        if (psy_out_side->sfb_min_snr[sfb] <= 1.0f) {
447
1.72M
          psy_out_side->sfb_min_snr[sfb] = MIN(psy_out_side->sfb_min_snr[sfb], 0.8f);
448
1.72M
        }
449
450
3.62M
        if (sfb_en_mid > psy_out_mid->ptr_sfb_spread_energy[sfb]) {
451
2.99M
          psy_out_side->ptr_sfb_spread_energy[sfb] = 0.9f * sfb_en_side;
452
2.99M
        }
453
454
3.62M
        if (sfb_en_side > psy_out_side->ptr_sfb_spread_energy[sfb]) {
455
1.89M
          psy_out_mid->ptr_sfb_spread_energy[sfb] = 0.9f * sfb_en_mid;
456
1.89M
        }
457
3.62M
      }
458
4.64M
    }
459
102k
  }
460
128k
  idx = 0;
461
358k
  for (ch = chn; ch < chn + num_chans; ch++) {
462
230k
    ia_psy_mod_out_data_struct *pstr_psy_chan_out = &pstr_psy_out[ch];
463
715k
    for (scf_band_grp = 0; scf_band_grp < pstr_psy_chan_out->sfb_count;
464
485k
         scf_band_grp += pstr_psy_chan_out->sfb_per_group) {
465
9.65M
      for (scf_band = 0; scf_band < pstr_psy_chan_out->max_sfb_per_grp; scf_band++) {
466
9.16M
        if (pstr_psy_chan_out->ptr_sfb_spread_energy[scf_band_grp + scf_band] >
467
9.16M
                pstr_psy_chan_out->ptr_sfb_energy[scf_band_grp + scf_band] ||
468
7.81M
            pstr_psy_chan_out->sfb_min_snr[scf_band_grp + scf_band] > (float)1.0) {
469
3.23M
          ah_flag[idx][scf_band_grp + scf_band] = NO_AH;
470
5.92M
        } else {
471
5.92M
          ah_flag[idx][scf_band_grp + scf_band] = AH_INACTIVE;
472
5.92M
        }
473
9.16M
      }
474
475
485k
      for (scf_band = pstr_psy_chan_out->max_sfb_per_grp;
476
1.75M
           scf_band < pstr_psy_chan_out->sfb_per_group; scf_band++) {
477
1.26M
        ah_flag[idx][scf_band_grp + scf_band] = NO_AH;
478
1.26M
      }
479
485k
    }
480
230k
    idx++;
481
230k
  }
482
483
128k
  return;
484
128k
}
485
486
static VOID iusace_reduce_thr(ia_psy_mod_out_data_struct *pstr_psy_out, WORD32 **ah_flag,
487
                              FLOAT32 **thr_exp, const FLOAT32 red_value, WORD32 num_channels,
488
283k
                              WORD32 chn) {
489
283k
  WORD32 ch, sfb_group, sfb, idx = 0;
490
283k
  FLOAT32 sfb_energy, sfb_threshold, sfb_thr_reduced;
491
283k
  FLOAT32 *sfb_energy_fix, *sfb_threshold_fix, *sfb_min_snr_fix, *thr_exp_fix;
492
797k
  for (ch = chn; ch < chn + num_channels; ch++) {
493
513k
    ia_psy_mod_out_data_struct *pstr_psy_chan_out = &pstr_psy_out[ch];
494
513k
    sfb_energy_fix = pstr_psy_chan_out->ptr_sfb_energy;
495
513k
    sfb_threshold_fix = pstr_psy_chan_out->ptr_sfb_thr;
496
513k
    sfb_min_snr_fix = pstr_psy_chan_out->sfb_min_snr;
497
513k
    thr_exp_fix = &thr_exp[idx][0];
498
1.56M
    for (sfb_group = 0; sfb_group < pstr_psy_chan_out->sfb_count;
499
1.05M
         sfb_group += pstr_psy_chan_out->sfb_per_group) {
500
21.0M
      for (sfb = 0; sfb < pstr_psy_chan_out->max_sfb_per_grp; sfb++) {
501
19.9M
        sfb_energy = sfb_energy_fix[sfb_group + sfb];
502
19.9M
        sfb_threshold = sfb_threshold_fix[sfb_group + sfb];
503
19.9M
        if (sfb_energy > sfb_threshold) {
504
13.6M
          sfb_thr_reduced =
505
13.6M
              (FLOAT32)pow((thr_exp_fix[sfb_group + sfb] + red_value), INV_RED_EXP_VAL);
506
507
13.6M
          if ((sfb_thr_reduced > sfb_min_snr_fix[sfb_group + sfb] * sfb_energy) &&
508
3.42M
              (ah_flag[idx][sfb_group + sfb] != NO_AH)) {
509
3.17M
            sfb_thr_reduced = MAX(sfb_min_snr_fix[sfb_group + sfb] * sfb_energy, sfb_threshold);
510
3.17M
            ah_flag[idx][sfb_group + sfb] = AH_ACTIVE;
511
3.17M
          }
512
13.6M
          sfb_threshold_fix[sfb_group + sfb] = sfb_thr_reduced;
513
13.6M
        }
514
19.9M
      }
515
1.05M
    }
516
513k
    idx++;
517
513k
  }
518
519
283k
  return;
520
283k
}
521
522
static VOID iusace_calc_pe_no_active_holes(FLOAT32 *pe, FLOAT32 *const_part,
523
                                           FLOAT32 *nactive_lines,
524
                                           ia_qc_pe_data_struct *pstr_qs_pe_data,
525
                                           WORD32 **ah_flag,
526
                                           ia_psy_mod_out_data_struct *pstr_psy_out,
527
157k
                                           WORD32 num_channels, WORD32 chn) {
528
157k
  WORD32 ch, sfb_group, sfb, idx = 0;
529
157k
  *pe = 0.0f;
530
157k
  *const_part = 0.0f;
531
157k
  *nactive_lines = 0;
532
445k
  for (ch = chn; ch < chn + num_channels; ch++) {
533
287k
    ia_psy_mod_out_data_struct *pstr_psy_chan_out = &pstr_psy_out[ch];
534
287k
    ia_qc_pe_chan_data_struct *pe_channel_data = &pstr_qs_pe_data->pe_ch_data[idx];
535
536
859k
    for (sfb_group = 0; sfb_group < pstr_psy_chan_out->sfb_count;
537
571k
         sfb_group += pstr_psy_chan_out->sfb_per_group) {
538
11.5M
      for (sfb = 0; sfb < pstr_psy_chan_out->max_sfb_per_grp; sfb++) {
539
10.9M
        if (ah_flag[idx][sfb_group + sfb] < AH_ACTIVE) {
540
9.25M
          *pe += pe_channel_data->sfb_pe[sfb_group + sfb];
541
9.25M
          *const_part += pe_channel_data->sfb_const_part[sfb_group + sfb];
542
9.25M
          *nactive_lines += pe_channel_data->num_sfb_active_lines[sfb_group + sfb];
543
9.25M
        }
544
10.9M
      }
545
571k
    }
546
287k
    idx++;
547
287k
  }
548
549
157k
  return;
550
157k
}
551
552
static IA_ERRORCODE iusace_correct_thr(ia_psy_mod_out_data_struct *pstr_psy_out, WORD32 **ah_flag,
553
                                       ia_qc_pe_data_struct *pstr_qs_pe_data, FLOAT32 **thr_exp,
554
                                       const FLOAT32 red_value, const FLOAT32 delta_pe,
555
116k
                                       WORD32 num_channels, WORD32 chn, UWORD8 *ptr_scratch) {
556
116k
  WORD32 i, ch, sfb_group, sfb, idx = 0;
557
116k
  FLOAT32 delta_sfb_pe;
558
116k
  FLOAT32 thr_factor;
559
116k
  FLOAT32 norm_factor[2] = {0};
560
116k
  FLOAT32 *sfb_pe_factors[2];
561
349k
  for (i = 0; i < 2; i++) {
562
233k
    sfb_pe_factors[i] = (FLOAT32 *)ptr_scratch;
563
233k
    ptr_scratch += (MAX_NUM_GROUPED_SFB) * sizeof(sfb_pe_factors[0][0]);
564
233k
  }
565
116k
  FLOAT32 sfb_en, sfb_thr, sfb_thr_reduced;
566
116k
  FLOAT32 *p_thr_exp;
567
116k
  FLOAT32 *p_sfb_energy, *p_sfb_thr, *p_sfb_min_snr;
568
116k
  ia_psy_mod_out_data_struct *pstr_psy_chan_out = NULL;
569
116k
  ia_qc_pe_chan_data_struct *pe_channel_data = NULL;
570
571
324k
  for (ch = chn; ch < chn + num_channels; ch++) {
572
207k
    if (idx >= IXHEAACE_MAX_CH_IN_BS_ELE) {
573
0
      return IA_EXHEAACE_EXE_FATAL_USAC_INVALID_NUM_CHANNEL;
574
0
    }
575
207k
    pstr_psy_chan_out = &pstr_psy_out[ch];
576
207k
    pe_channel_data = &pstr_qs_pe_data->pe_ch_data[idx];
577
207k
    norm_factor[idx] = MIN_FLT_VAL;
578
207k
    p_thr_exp = thr_exp[idx];
579
580
660k
    for (sfb_group = 0; sfb_group < pstr_psy_chan_out->sfb_count;
581
453k
         sfb_group += pstr_psy_chan_out->sfb_per_group) {
582
8.91M
      for (sfb = 0; sfb < pstr_psy_chan_out->max_sfb_per_grp; sfb++) {
583
8.45M
        if ((ah_flag[idx][sfb_group + sfb] < AH_ACTIVE) || (delta_pe > 0)) {
584
7.59M
          sfb_pe_factors[idx][sfb_group + sfb] =
585
7.59M
              pe_channel_data->num_sfb_active_lines[sfb_group + sfb] /
586
7.59M
              (p_thr_exp[sfb_group + sfb] + red_value);
587
7.59M
          norm_factor[idx] += sfb_pe_factors[idx][sfb_group + sfb];
588
7.59M
        } else {
589
869k
          sfb_pe_factors[idx][sfb_group + sfb] = 0.0f;
590
869k
        }
591
8.45M
      }
592
453k
    }
593
207k
    idx++;
594
207k
  }
595
116k
  if (num_channels > 1) {
596
90.9k
    norm_factor[0] = norm_factor[0] + norm_factor[1];
597
90.9k
  }
598
116k
  norm_factor[0] = 1.0f / norm_factor[0];
599
116k
  idx = 0;
600
324k
  for (ch = chn; ch < chn + num_channels; ch++) {
601
207k
    if (idx >= IXHEAACE_MAX_CH_IN_BS_ELE) {
602
0
      return IA_EXHEAACE_EXE_FATAL_USAC_INVALID_NUM_CHANNEL;
603
0
    }
604
207k
    pstr_psy_chan_out = &pstr_psy_out[ch];
605
207k
    pe_channel_data = &pstr_qs_pe_data->pe_ch_data[idx];
606
207k
    p_sfb_energy = pstr_psy_chan_out->ptr_sfb_energy;
607
207k
    p_sfb_thr = pstr_psy_chan_out->ptr_sfb_thr;
608
207k
    p_sfb_min_snr = pstr_psy_chan_out->sfb_min_snr;
609
610
660k
    for (sfb_group = 0; sfb_group < pstr_psy_chan_out->sfb_count;
611
453k
         sfb_group += pstr_psy_chan_out->sfb_per_group) {
612
453k
      i = sfb_group;
613
8.91M
      for (sfb = pstr_psy_chan_out->max_sfb_per_grp - 1; sfb >= 0; sfb--, i++) {
614
8.45M
        delta_sfb_pe = sfb_pe_factors[idx][i] * norm_factor[0] * delta_pe;
615
8.45M
        if (pe_channel_data->num_sfb_active_lines[i] > (FLOAT32)0.5f) {
616
5.46M
          sfb_en = p_sfb_energy[i];
617
5.46M
          sfb_thr = p_sfb_thr[i];
618
5.46M
          thr_factor = MIN(-delta_sfb_pe / pe_channel_data->num_sfb_active_lines[i], 20.f);
619
5.46M
          thr_factor = (FLOAT32)pow(2.0f, thr_factor);
620
5.46M
          sfb_thr_reduced = sfb_thr * thr_factor;
621
622
5.46M
          if ((sfb_thr_reduced > p_sfb_min_snr[i] * sfb_en) && (ah_flag[idx][i] == AH_INACTIVE)) {
623
42.2k
            sfb_thr_reduced = MAX(p_sfb_min_snr[i] * sfb_en, sfb_thr);
624
42.2k
            ah_flag[idx][i] = AH_ACTIVE;
625
42.2k
          }
626
5.46M
          p_sfb_thr[i] = sfb_thr_reduced;
627
5.46M
        }
628
8.45M
      }
629
453k
    }
630
207k
    idx++;
631
207k
  }
632
633
116k
  return IA_NO_ERROR;
634
116k
}
635
636
static VOID iusace_reduce_min_snr(ia_psy_mod_out_data_struct *pstr_psy_out,
637
                                  ia_qc_pe_data_struct *pstr_qs_pe_data, WORD32 **ah_flag,
638
11.5k
                                  const FLOAT32 desired_pe, WORD32 num_channels, WORD32 chn) {
639
11.5k
  WORD32 sfb, sfb_sub_win, ch, idx;
640
11.5k
  FLOAT32 delta_pe;
641
642
11.5k
  sfb_sub_win = pstr_psy_out[chn].max_sfb_per_grp;
643
644
327k
  while (pstr_qs_pe_data->pe > desired_pe && sfb_sub_win > 0) {
645
315k
    sfb_sub_win--;
646
664k
    for (sfb = sfb_sub_win; sfb < pstr_psy_out[chn].sfb_count;
647
350k
         sfb += pstr_psy_out[chn].sfb_per_group) {
648
350k
      idx = 0;
649
1.04M
      for (ch = chn; ch < chn + num_channels; ch++) {
650
698k
        if (ah_flag[idx][sfb] != NO_AH && pstr_psy_out[ch].sfb_min_snr[sfb] < MIN_SNR_LIMIT) {
651
281k
          pstr_psy_out[ch].sfb_min_snr[sfb] = MIN_SNR_LIMIT;
652
281k
          pstr_psy_out[ch].ptr_sfb_thr[sfb] =
653
281k
              pstr_psy_out[ch].ptr_sfb_energy[sfb] * pstr_psy_out[ch].sfb_min_snr[sfb];
654
281k
          delta_pe = pstr_qs_pe_data->pe_ch_data[idx].sfb_lines[sfb] * 1.5f -
655
281k
                     pstr_qs_pe_data->pe_ch_data[idx].sfb_pe[sfb];
656
281k
          pstr_qs_pe_data->pe += delta_pe;
657
281k
          pstr_qs_pe_data->pe_ch_data[idx].pe += delta_pe;
658
281k
        }
659
698k
        idx++;
660
698k
      }
661
350k
      if (pstr_qs_pe_data->pe <= desired_pe) break;
662
350k
    }
663
315k
  }
664
665
11.5k
  return;
666
11.5k
}
667
668
static VOID iusace_allow_more_holes(ia_psy_mod_out_data_struct *pstr_psy_out,
669
                                    ia_qc_pe_data_struct *pstr_qs_pe_data, WORD32 **ah_flag,
670
                                    const ia_ah_param_struct *str_ah_param,
671
                                    const FLOAT32 desired_pe, WORD32 num_channels, WORD32 chn,
672
11.5k
                                    UWORD8 *ptr_scratch) {
673
11.5k
  WORD32 sfb, ch, idx;
674
11.5k
  FLOAT32 act_pe = pstr_qs_pe_data->pe;
675
676
11.5k
  if (num_channels == 2 &&
677
11.4k
      pstr_psy_out[chn].window_sequence == pstr_psy_out[chn + 1].window_sequence) {
678
11.4k
    ia_psy_mod_out_data_struct *psy_out_left = &pstr_psy_out[chn];
679
11.4k
    ia_psy_mod_out_data_struct *psy_out_right = &pstr_psy_out[chn + 1];
680
681
368k
    for (sfb = 0; sfb < psy_out_left->sfb_count; sfb++) {
682
359k
      if (pstr_psy_out[chn].ms_used[sfb]) {
683
216k
        if (ah_flag[1][sfb] != NO_AH &&
684
123k
            0.4f * psy_out_left->sfb_min_snr[sfb] * psy_out_left->ptr_sfb_energy[sfb] >
685
123k
                psy_out_right->ptr_sfb_energy[sfb]) {
686
9.71k
          ah_flag[1][sfb] = NO_AH;
687
688
9.71k
          psy_out_right->ptr_sfb_thr[sfb] = 2.0f * psy_out_right->ptr_sfb_energy[sfb];
689
690
9.71k
          act_pe -= pstr_qs_pe_data->pe_ch_data[1].sfb_pe[sfb];
691
206k
        } else {
692
206k
          if (ah_flag[0][sfb] != NO_AH &&
693
190k
              0.4f * psy_out_right->sfb_min_snr[sfb] * psy_out_right->ptr_sfb_energy[sfb] >
694
190k
                  psy_out_left->ptr_sfb_energy[sfb]) {
695
6.67k
            ah_flag[0][sfb] = NO_AH;
696
697
6.67k
            psy_out_left->ptr_sfb_thr[sfb] = 2.0f * psy_out_left->ptr_sfb_energy[sfb];
698
699
6.67k
            act_pe -= pstr_qs_pe_data->pe_ch_data[0].sfb_pe[sfb];
700
6.67k
          }
701
206k
        }
702
216k
        if (act_pe < desired_pe) break;
703
216k
      }
704
359k
    }
705
11.4k
  }
706
11.5k
  if (act_pe > desired_pe) {
707
8.51k
    WORD32 *start_sfb = (WORD32 *)ptr_scratch;
708
8.51k
    memset(start_sfb, 0, MAX_TIME_CHANNELS * sizeof(start_sfb[0]));
709
8.51k
    FLOAT32 average_energy, min_energy;
710
8.51k
    WORD32 ah_cnt;
711
8.51k
    WORD32 en_idx;
712
8.51k
    FLOAT32 energy[4];
713
8.51k
    WORD32 min_sfb, max_sfb;
714
8.51k
    WORD32 done;
715
25.5k
    for (ch = chn; ch < chn + num_channels; ch++) {
716
17.0k
      if (pstr_psy_out[ch].window_sequence != EIGHT_SHORT_SEQUENCE)
717
15.4k
        start_sfb[ch] = str_ah_param->start_sfb_long;
718
1.55k
      else
719
1.55k
        start_sfb[ch] = str_ah_param->start_sfb_short;
720
17.0k
    }
721
722
8.51k
    average_energy = 0.0f;
723
8.51k
    min_energy = MAX_FLT_VAL;
724
8.51k
    ah_cnt = 0;
725
8.51k
    idx = 0;
726
25.5k
    for (ch = chn; ch < chn + num_channels; ch++) {
727
17.0k
      ia_psy_mod_out_data_struct *pstr_psy_chan_out = &pstr_psy_out[ch];
728
431k
      for (sfb = start_sfb[ch]; sfb < pstr_psy_chan_out->sfb_count; sfb++) {
729
414k
        if ((ah_flag[idx][sfb] != NO_AH) &&
730
227k
            (pstr_psy_chan_out->ptr_sfb_energy[sfb] > pstr_psy_chan_out->ptr_sfb_thr[sfb])) {
731
227k
          min_energy = MIN(min_energy, pstr_psy_chan_out->ptr_sfb_energy[sfb]);
732
227k
          average_energy += pstr_psy_chan_out->ptr_sfb_energy[sfb];
733
227k
          ah_cnt++;
734
227k
        }
735
414k
      }
736
17.0k
      idx++;
737
17.0k
    }
738
739
8.51k
    average_energy = MIN(MAX_FLT_VAL, average_energy / (ah_cnt + MIN_FLT_VAL));
740
741
42.5k
    for (en_idx = 0; en_idx < 4; en_idx++) {
742
34.0k
      energy[en_idx] = min_energy * (FLOAT32)pow(average_energy / (min_energy + MIN_FLT_VAL),
743
34.0k
                                                 (2 * en_idx + 1) / 7.0f);
744
34.0k
    }
745
8.51k
    max_sfb = pstr_psy_out[chn].sfb_count - 1;
746
8.51k
    min_sfb = start_sfb[chn];
747
748
8.51k
    if (num_channels == 2) {
749
8.50k
      max_sfb = MAX(max_sfb, pstr_psy_out[chn + 1].sfb_count - 1);
750
751
8.50k
      min_sfb = MIN(min_sfb, start_sfb[chn + 1]);
752
8.50k
    }
753
754
8.51k
    sfb = max_sfb;
755
8.51k
    en_idx = 0;
756
8.51k
    done = 0;
757
370k
    while (!done) {
758
362k
      idx = 0;
759
1.07M
      for (ch = chn; ch < chn + num_channels; ch++) {
760
719k
        ia_psy_mod_out_data_struct *pstr_psy_chan_out = &pstr_psy_out[ch];
761
719k
        if (sfb >= start_sfb[ch] && sfb < pstr_psy_chan_out->sfb_count) {
762
719k
          if (ah_flag[idx][sfb] != NO_AH &&
763
369k
              pstr_psy_chan_out->ptr_sfb_energy[sfb] < energy[en_idx]) {
764
66.2k
            ah_flag[idx][sfb] = NO_AH;
765
66.2k
            pstr_psy_chan_out->ptr_sfb_thr[sfb] = 2.0f * pstr_psy_chan_out->ptr_sfb_energy[sfb];
766
66.2k
            act_pe -= pstr_qs_pe_data->pe_ch_data[idx].sfb_pe[sfb];
767
66.2k
          }
768
769
719k
          if (act_pe < desired_pe) {
770
7.43k
            done = 1;
771
7.43k
            break;
772
7.43k
          }
773
719k
        }
774
711k
        idx++;
775
711k
      }
776
362k
      sfb--;
777
362k
      if (sfb < min_sfb) {
778
11.0k
        sfb = max_sfb;
779
11.0k
        en_idx++;
780
11.0k
        if (en_idx >= 4) {
781
1.13k
          done = 1;
782
1.13k
        }
783
11.0k
      }
784
362k
    }
785
8.51k
  }
786
787
11.5k
  return;
788
11.5k
}
789
790
static IA_ERRORCODE iusace_adapt_thr_to_pe(ia_psy_mod_out_data_struct *pstr_psy_out,
791
                                           ia_qc_pe_data_struct *pstr_qs_pe_data,
792
                                           const FLOAT32 desired_pe,
793
                                           ia_ah_param_struct *str_ah_param,
794
                                           ia_min_snr_adapt_param_struct *msa_param,
795
128k
                                           WORD32 num_channels, WORD32 chn, UWORD8 *ptr_scratch) {
796
128k
  IA_ERRORCODE err_code;
797
128k
  FLOAT32 no_red_pe, red_pe, red_pe_no_ah;
798
128k
  FLOAT32 const_part, const_part_no_ah;
799
128k
  FLOAT32 nactive_lines, nactive_lines_no_ah;
800
128k
  FLOAT32 desired_pe_no_ah;
801
128k
  FLOAT32 redval = 0.0f;
802
128k
  WORD32 *ah_flag[2];
803
128k
  WORD32 iteration;
804
384k
  for (WORD32 i = 0; i < 2; i++) {
805
256k
    ah_flag[i] = (WORD32 *)ptr_scratch;
806
256k
    ptr_scratch += (MAX_NUM_GROUPED_SFB) * sizeof(ah_flag[0][0]);
807
256k
  }
808
128k
  FLOAT32 *thr_exp[2];
809
384k
  for (WORD32 i = 0; i < 2; i++) {
810
256k
    thr_exp[i] = (FLOAT32 *)ptr_scratch;
811
256k
    ptr_scratch += (MAX_NUM_GROUPED_SFB) * sizeof(thr_exp[0][0]);
812
256k
  }
813
814
128k
  iusace_calc_thr_exp(thr_exp, pstr_psy_out, num_channels, chn);
815
128k
  iusace_adapt_min_snr(pstr_psy_out, msa_param, num_channels, chn);
816
128k
  iusace_init_avoid_hole_flag(ah_flag, pstr_psy_out, str_ah_param, num_channels, chn);
817
818
128k
  no_red_pe = pstr_qs_pe_data->pe;
819
128k
  const_part = pstr_qs_pe_data->const_part;
820
128k
  nactive_lines = pstr_qs_pe_data->num_active_lines;
821
128k
  if (nactive_lines > FLT_EPSILON) {
822
128k
    FLOAT32 avg_thr_exp = (FLOAT32)pow(2.0f, (const_part - no_red_pe) /
823
128k
                  (INV_RED_EXP_VAL * nactive_lines));
824
128k
    redval = (FLOAT32)pow(2.0f, (const_part - desired_pe) / (INV_RED_EXP_VAL * nactive_lines)) -
825
128k
             avg_thr_exp;
826
128k
    redval = MAX(0.0f, redval);
827
128k
    iusace_reduce_thr(pstr_psy_out, ah_flag, thr_exp, redval, num_channels, chn);
828
128k
  }
829
830
128k
  iusace_calc_sfb_pe_data(pstr_qs_pe_data, pstr_psy_out, num_channels, chn);
831
128k
  red_pe = pstr_qs_pe_data->pe;
832
833
128k
  iteration = 0;
834
157k
  do {
835
157k
    iusace_calc_pe_no_active_holes(&red_pe_no_ah, &const_part_no_ah, &nactive_lines_no_ah,
836
157k
                                   pstr_qs_pe_data, ah_flag, pstr_psy_out, num_channels, chn);
837
838
157k
    desired_pe_no_ah = MAX(desired_pe - (red_pe - red_pe_no_ah), 0);
839
157k
    if (nactive_lines_no_ah > FLT_EPSILON) {
840
155k
      FLOAT32 avg_thr_exp = (FLOAT32)pow(
841
155k
          2.0f, (const_part_no_ah - red_pe_no_ah) / (INV_RED_EXP_VAL * nactive_lines_no_ah));
842
155k
      redval += (FLOAT32)pow(2.0f, (const_part_no_ah - desired_pe_no_ah) /
843
155k
                                       (INV_RED_EXP_VAL * nactive_lines_no_ah)) -
844
155k
                avg_thr_exp;
845
155k
      redval = MAX(0.0f, redval);
846
155k
      iusace_reduce_thr(pstr_psy_out, ah_flag, thr_exp, redval, num_channels, chn);
847
155k
    }
848
849
157k
    iusace_calc_sfb_pe_data(pstr_qs_pe_data, pstr_psy_out, num_channels, chn);
850
157k
    red_pe = pstr_qs_pe_data->pe;
851
157k
    iteration++;
852
157k
  } while ((fabs(red_pe - desired_pe) > (0.05f) * desired_pe) && (iteration < 2));
853
128k
  if (red_pe < 1.15f * desired_pe) {
854
116k
    err_code = iusace_correct_thr(pstr_psy_out, ah_flag, pstr_qs_pe_data, thr_exp, redval,
855
116k
                                  desired_pe - red_pe, num_channels, chn, ptr_scratch);
856
116k
    if (err_code != IA_NO_ERROR) {
857
0
      return err_code;
858
0
    }
859
116k
  } else {
860
11.5k
    iusace_reduce_min_snr(pstr_psy_out, pstr_qs_pe_data, ah_flag, 1.05f * desired_pe,
861
11.5k
                          num_channels, chn);
862
11.5k
    iusace_allow_more_holes(pstr_psy_out, pstr_qs_pe_data, ah_flag, str_ah_param,
863
11.5k
                            1.05f * desired_pe, num_channels, chn, ptr_scratch);
864
11.5k
  }
865
866
128k
  return IA_NO_ERROR;
867
128k
}
868
869
IA_ERRORCODE iusace_adj_thr(ia_adj_thr_elem_struct *pstr_adj_thr_elem,
870
                            ia_psy_mod_out_data_struct *pstr_psy_out, FLOAT32 *ch_bit_dist,
871
                            ia_qc_out_data_struct *pstr_qc_out, const WORD32 avg_bits,
872
                            const WORD32 bitres_bits, const WORD32 max_bitres_bits,
873
                            const WORD32 side_info_bits, FLOAT32 *max_bit_fac,
874
239k
                            WORD32 num_channels, WORD32 chn, iusace_scratch_mem *pstr_scratch) {
875
239k
  IA_ERRORCODE err_code;
876
239k
  FLOAT32 no_red_pe, granted_pe, granted_pe_corr;
877
239k
  WORD32 curr_win_sequence;
878
239k
  ia_qc_pe_data_struct *pstr_qc_pe_data = (ia_qc_pe_data_struct *)pstr_scratch->ptr_fd_scratch;
879
239k
  pUWORD8 ptr_scratch = pstr_scratch->ptr_fd_scratch + sizeof(ia_qc_pe_data_struct);
880
239k
  FLOAT32 bit_factor;
881
239k
  WORD32 ch;
882
883
239k
  pstr_qc_pe_data->pe_ch_data[0].sfb_lines = pstr_scratch->ptr_sfb_num_relevant_lines[0];
884
239k
  pstr_qc_pe_data->pe_ch_data[0].sfb_ld_energy = pstr_scratch->ptr_sfb_ld_energy[0];
885
239k
  if (num_channels == 2) {
886
202k
    pstr_qc_pe_data->pe_ch_data[1].sfb_lines = pstr_scratch->ptr_sfb_num_relevant_lines[1];
887
202k
    pstr_qc_pe_data->pe_ch_data[1].sfb_ld_energy = pstr_scratch->ptr_sfb_ld_energy[1];
888
202k
  }
889
239k
  pstr_qc_pe_data->offset = pstr_adj_thr_elem->pe_offset;
890
891
239k
  iusace_calc_sfb_pe_data(pstr_qc_pe_data, pstr_psy_out, num_channels, chn);
892
239k
  no_red_pe = pstr_qc_pe_data->pe;
893
894
239k
  curr_win_sequence = ONLY_LONG_SEQUENCE;
895
239k
  if (num_channels == 2) {
896
202k
    if ((pstr_psy_out[chn].window_sequence == EIGHT_SHORT_SEQUENCE) ||
897
131k
        (pstr_psy_out[chn + 1].window_sequence == EIGHT_SHORT_SEQUENCE)) {
898
70.2k
      curr_win_sequence = EIGHT_SHORT_SEQUENCE;
899
70.2k
    }
900
202k
  } else {
901
37.5k
    curr_win_sequence = pstr_psy_out[chn].window_sequence;
902
37.5k
  }
903
904
239k
  bit_factor =
905
239k
      iusace_bitres_calc_bitfac(bitres_bits, max_bitres_bits, no_red_pe + 5.0f * side_info_bits,
906
239k
                                curr_win_sequence, avg_bits, *max_bit_fac, pstr_adj_thr_elem);
907
239k
  granted_pe = bit_factor * iusace_bits_to_pe((FLOAT32)avg_bits);
908
239k
  iusace_calc_pe_correction(&(pstr_adj_thr_elem->pe_correction_fac), MIN(granted_pe, no_red_pe),
909
239k
                            pstr_adj_thr_elem->pe_last, pstr_adj_thr_elem->dyn_bits_last);
910
239k
  granted_pe_corr = granted_pe * pstr_adj_thr_elem->pe_correction_fac;
911
912
239k
  if (granted_pe_corr < no_red_pe) {
913
128k
    err_code = iusace_adapt_thr_to_pe(
914
128k
        pstr_psy_out, pstr_qc_pe_data, granted_pe_corr, &pstr_adj_thr_elem->str_ah_param,
915
128k
        &pstr_adj_thr_elem->str_min_snr_adapt_params, num_channels, chn, ptr_scratch);
916
128k
    if (err_code != IA_NO_ERROR) {
917
0
      return err_code;
918
0
    }
919
128k
  }
920
921
681k
  for (ch = 0; ch < num_channels; ch++) {
922
441k
    FLOAT32 tmp_var, temp1;
923
441k
    if (pstr_qc_pe_data->pe) {
924
400k
      tmp_var = 1.0f - num_channels * 0.2f;
925
400k
      temp1 = pstr_qc_pe_data->pe_ch_data[ch].pe / pstr_qc_pe_data->pe;
926
400k
      temp1 = temp1 * tmp_var;
927
400k
      ch_bit_dist[ch] = temp1 + 0.2f;
928
400k
      if (ch_bit_dist[ch] < 0.2f) ch_bit_dist[ch] = 0.2f;
929
400k
    } else {
930
41.3k
      ch_bit_dist[ch] = 0.2f;
931
41.3k
    }
932
441k
  }
933
934
239k
  pstr_qc_out->pe = no_red_pe;
935
239k
  pstr_adj_thr_elem->pe_last = granted_pe;
936
937
239k
  return IA_NO_ERROR;
938
239k
}
939
940
VOID iusace_calc_form_fac_per_chan(ia_psy_mod_out_data_struct *pstr_psy_out_chan,
941
441k
                                   iusace_scratch_mem *pstr_scratch, WORD32 i_ch) {
942
441k
  WORD32 i, j, sfb_offs;
943
441k
  WORD32 sfb, sfb_width;
944
441k
  FLOAT32 *ptr_sfb_form_factor = pstr_scratch->ptr_sfb_form_fac[i_ch];
945
441k
  FLOAT32 *ptr_sfb_num_relevant_lines = pstr_scratch->ptr_sfb_num_relevant_lines[i_ch];
946
441k
  FLOAT32 *ptr_sfb_ld_energy = pstr_scratch->ptr_sfb_ld_energy[i_ch];
947
441k
  FLOAT64 spec_coef = 0;
948
949
441k
  memset(ptr_sfb_num_relevant_lines, 0, sizeof(FLOAT32) * pstr_psy_out_chan->sfb_count);
950
951
1.30M
  for (sfb_offs = 0; sfb_offs < pstr_psy_out_chan->sfb_count;
952
866k
       sfb_offs += pstr_psy_out_chan->sfb_per_group) {
953
866k
    i = sfb_offs;
954
18.7M
    for (sfb = 0; sfb < pstr_psy_out_chan->max_sfb_per_grp; sfb++, i++) {
955
17.8M
      ptr_sfb_form_factor[i] = MIN_FLT_VAL;
956
17.8M
      if (pstr_psy_out_chan->ptr_sfb_energy[i] > pstr_psy_out_chan->ptr_sfb_thr[i]) {
957
9.51M
        FLOAT32 avg_form_factor;
958
959
175M
        for (j = pstr_psy_out_chan->sfb_offsets[i]; j < pstr_psy_out_chan->sfb_offsets[i + 1];
960
165M
             j++) {
961
165M
          spec_coef = fabs(pstr_psy_out_chan->ptr_spec_coeffs[j]);
962
165M
          if (spec_coef < EPS && spec_coef != 0) spec_coef = EPS;
963
165M
          ptr_sfb_form_factor[i] += (FLOAT32)sqrt(spec_coef);
964
165M
        }
965
966
9.51M
        sfb_width = pstr_psy_out_chan->sfb_offsets[i + 1] - pstr_psy_out_chan->sfb_offsets[i];
967
9.51M
        avg_form_factor =
968
9.51M
            (FLOAT32)pow(pstr_psy_out_chan->ptr_sfb_energy[i] / (FLOAT32)sfb_width, 0.25);
969
9.51M
        ptr_sfb_num_relevant_lines[i] = ptr_sfb_form_factor[i] / avg_form_factor;
970
9.51M
        ptr_sfb_ld_energy[i] = (FLOAT32)(log(pstr_psy_out_chan->ptr_sfb_energy[i]) * LOG2_1);
971
9.51M
      }
972
17.8M
    }
973
866k
  }
974
975
441k
  return;
976
441k
}
977
978
VOID iusace_quantize_lines(const WORD32 gain, const WORD32 num_lines, FLOAT32 *ptr_exp_spectrum,
979
1.82M
                           WORD16 *ptr_quant_spectrum, FLOAT32 *ptr_mdct_spec) {
980
1.82M
  FLOAT32 quantizer;
981
1.82M
  FLOAT32 k = 0.4054f;
982
1.82M
  WORD32 line;
983
984
1.82M
  quantizer = ixheaace_fd_quant_table[gain + 128];
985
20.8M
  for (line = 0; line < num_lines; line++) {
986
19.0M
    FLOAT32 tmp = ptr_mdct_spec[line];
987
19.0M
    if (tmp < 0.0f) {
988
3.47M
      ptr_exp_spectrum[line] = (FLOAT32)sqrt(-tmp);
989
3.47M
      ptr_exp_spectrum[line] *= (FLOAT32)sqrt(ptr_exp_spectrum[line]);
990
3.47M
      ptr_quant_spectrum[line] = -(WORD16)(k + quantizer * ptr_exp_spectrum[line]);
991
15.5M
    } else {
992
15.5M
      ptr_exp_spectrum[line] = (FLOAT32)sqrt(tmp);
993
15.5M
      ptr_exp_spectrum[line] *= (FLOAT32)sqrt(ptr_exp_spectrum[line]);
994
15.5M
      ptr_quant_spectrum[line] = (WORD16)(k + quantizer * ptr_exp_spectrum[line]);
995
15.5M
    }
996
19.0M
  }
997
1.82M
  return;
998
1.82M
}
999
1000
VOID iusace_calculate_exp_spec(const WORD32 num_lines, FLOAT32 *ptr_exp_spectrum,
1001
9.11M
                               FLOAT32 *ptr_mdct_spec) {
1002
9.11M
  WORD32 line;
1003
1004
168M
  for (line = 0; line < num_lines; line++) {
1005
159M
    ptr_exp_spectrum[line] = (FLOAT32)sqrt(fabs(ptr_mdct_spec[line]));
1006
159M
    ptr_exp_spectrum[line] *= (FLOAT32)sqrt(ptr_exp_spectrum[line]);
1007
159M
  }
1008
9.11M
  return;
1009
9.11M
}
1010
1011
190M
static WORD32 iusace_scf_delta_bit_count(WORD32 delta) {
1012
190M
  if (delta > 60) {
1013
16
    return (iusace_huffman_code_table[120][0]);
1014
16
  }
1015
190M
  if (delta < -60) {
1016
6
    return (iusace_huffman_code_table[0][0]);
1017
6
  }
1018
190M
  return (iusace_huffman_code_table[delta + 60][0]);
1019
190M
}
1020
1021
37.2M
static WORD32 iusace_count_single_scf_bits(WORD32 scf, WORD32 left_scf, WORD32 right_scf) {
1022
37.2M
  WORD32 scf_bits;
1023
1024
37.2M
  scf_bits =
1025
37.2M
      iusace_scf_delta_bit_count(left_scf - scf) + iusace_scf_delta_bit_count(scf - right_scf);
1026
1027
37.2M
  return scf_bits;
1028
37.2M
}
1029
1030
static FLOAT32 iusace_calc_single_spec_pe(WORD32 scf, FLOAT32 sfb_const_pe_part,
1031
37.2M
                                          FLOAT32 num_lines) {
1032
37.2M
  FLOAT32 spec_pe;
1033
37.2M
  FLOAT32 ld_ratio;
1034
1035
37.2M
  ld_ratio = sfb_const_pe_part - (FLOAT32)0.375f * (FLOAT32)scf;
1036
1037
37.2M
  if (ld_ratio >= PE_C1) {
1038
28.9M
    spec_pe = (FLOAT32)0.7f * num_lines * ld_ratio;
1039
28.9M
  } else {
1040
8.35M
    spec_pe = (FLOAT32)0.7f * num_lines * (PE_C2 + PE_C3 * ld_ratio);
1041
8.35M
  }
1042
1043
37.2M
  return spec_pe;
1044
37.2M
}
1045
1046
static WORD32 iusace_count_scf_bits_diff(WORD16 *ptr_sfb_prev, WORD16 *ptr_sfb_new,
1047
8.98M
                                         WORD32 sfb_count, WORD32 start_sfb, WORD32 stop_sfb) {
1048
8.98M
  WORD32 scf_bits_diff = 0;
1049
8.98M
  WORD32 sfb = 0, sfb_last;
1050
8.98M
  WORD32 sfb_prev, sfb_next;
1051
1052
8.98M
  sfb_last = start_sfb;
1053
1054
8.98M
  while ((sfb_last < stop_sfb) && (ptr_sfb_prev[sfb_last] == SHRT_MIN)) {
1055
0
    sfb_last++;
1056
0
  }
1057
1058
8.98M
  sfb_prev = start_sfb - 1;
1059
1060
18.0M
  while ((sfb_prev >= 0) && (ptr_sfb_prev[sfb_prev] == SHRT_MIN)) {
1061
9.05M
    sfb_prev--;
1062
9.05M
  }
1063
1064
8.98M
  if (sfb_prev >= 0) {
1065
5.95M
    scf_bits_diff += iusace_scf_delta_bit_count(ptr_sfb_new[sfb_prev] - ptr_sfb_new[sfb_last]) -
1066
5.95M
                     iusace_scf_delta_bit_count(ptr_sfb_prev[sfb_prev] - ptr_sfb_prev[sfb_last]);
1067
5.95M
  }
1068
1069
89.0M
  for (sfb = sfb_last + 1; sfb < stop_sfb; sfb++) {
1070
80.0M
    if (ptr_sfb_prev[sfb] != SHRT_MIN) {
1071
45.4M
      scf_bits_diff += iusace_scf_delta_bit_count(ptr_sfb_new[sfb_last] - ptr_sfb_new[sfb]) -
1072
45.4M
                       iusace_scf_delta_bit_count(ptr_sfb_prev[sfb_last] - ptr_sfb_prev[sfb]);
1073
1074
45.4M
      sfb_last = sfb;
1075
45.4M
    }
1076
80.0M
  }
1077
1078
8.98M
  sfb_next = stop_sfb;
1079
1080
8.98M
  while ((sfb_next < sfb_count) && (ptr_sfb_prev[sfb_next] == SHRT_MIN)) {
1081
0
    sfb_next++;
1082
0
  }
1083
1084
8.98M
  if (sfb_next < sfb_count) {
1085
6.54M
    scf_bits_diff += iusace_scf_delta_bit_count(ptr_sfb_new[sfb_last] - ptr_sfb_new[sfb_next]) -
1086
6.54M
                     iusace_scf_delta_bit_count(ptr_sfb_prev[sfb_last] - ptr_sfb_prev[sfb_next]);
1087
6.54M
  }
1088
1089
8.98M
  return scf_bits_diff;
1090
8.98M
}
1091
1092
static FLOAT32 iusace_calc_spec_pe_diff(ia_psy_mod_out_data_struct *pstr_psy_out,
1093
                                        WORD16 *scf_prev, WORD16 *scf_new,
1094
                                        FLOAT32 *ptr_sfb_const_pe_part, FLOAT32 *ptr_sfb_form_fac,
1095
                                        FLOAT32 *ptr_sfb_num_rel_lines, WORD32 start_sfb,
1096
8.98M
                                        WORD32 stop_sfb) {
1097
8.98M
  FLOAT32 spec_pe_diff = 0.0f;
1098
8.98M
  WORD32 sfb;
1099
1100
97.9M
  for (sfb = start_sfb; sfb < stop_sfb; sfb++) {
1101
89.0M
    if (scf_prev[sfb] != SHRT_MIN) {
1102
54.4M
      FLOAT32 ld_ratio_prev, ld_ratio_new, pe_prev, pe_new;
1103
1104
54.4M
      if (ptr_sfb_const_pe_part[sfb] == MIN_FLT_VAL) {
1105
2.60M
        ptr_sfb_const_pe_part[sfb] = (FLOAT32)log(pstr_psy_out->ptr_sfb_energy[sfb] *
1106
2.60M
                                                  (FLOAT32)6.75f / ptr_sfb_form_fac[sfb]) *
1107
2.60M
                                     LOG2_1;
1108
2.60M
      }
1109
1110
54.4M
      ld_ratio_prev = ptr_sfb_const_pe_part[sfb] - 0.375f * scf_prev[sfb];
1111
54.4M
      ld_ratio_new = ptr_sfb_const_pe_part[sfb] - 0.375f * scf_new[sfb];
1112
1113
54.4M
      if (ld_ratio_prev >= PE_C1) {
1114
38.9M
        pe_prev = ld_ratio_prev;
1115
38.9M
      } else {
1116
15.4M
        pe_prev = PE_C2 + PE_C3 * ld_ratio_prev;
1117
15.4M
      }
1118
1119
54.4M
      if (ld_ratio_new >= PE_C1) {
1120
48.7M
        pe_new = ld_ratio_new;
1121
48.7M
      } else {
1122
5.70M
        pe_new = PE_C2 + PE_C3 * ld_ratio_new;
1123
5.70M
      }
1124
1125
54.4M
      spec_pe_diff += (FLOAT32)0.7f * ptr_sfb_num_rel_lines[sfb] * (pe_new - pe_prev);
1126
54.4M
    }
1127
89.0M
  }
1128
1129
8.98M
  return spec_pe_diff;
1130
8.98M
}
1131
1132
FLOAT32 iusace_calc_sfb_dist(const FLOAT32 *ptr_spec, const FLOAT32 *ptr_exp_spec,
1133
54.2M
                             WORD16 *ptr_quant_spec, WORD32 sfb_width, WORD32 gain) {
1134
54.2M
  WORD32 i;
1135
54.2M
  FLOAT32 dist = 0;
1136
54.2M
  FLOAT32 k = 0.4054f;
1137
54.2M
  FLOAT32 quantizer = ixheaace_fd_quant_table[gain + 128];
1138
54.2M
  FLOAT32 inv_quantizer = ixheaace_fd_inv_quant_table[gain + 128];
1139
1140
845M
  for (i = 0; i < sfb_width; i++) {
1141
791M
    FLOAT32 iq_val;
1142
791M
    FLOAT32 diff;
1143
1144
791M
    ptr_quant_spec[i] = (WORD16)(k + quantizer * ptr_exp_spec[i]);
1145
1146
791M
    if (ptr_quant_spec[i] < 64) {
1147
768M
      iq_val = ixheaace_pow_4_3_table[ptr_quant_spec[i]] * inv_quantizer;
1148
768M
    } else {
1149
22.6M
      iq_val = (FLOAT32)((pow((FLOAT32)abs(ptr_quant_spec[i]), 4.0f / 3.0f)) * inv_quantizer);
1150
22.6M
    }
1151
1152
791M
    diff = (FLOAT32)fabs(ptr_spec[i]) - iq_val;
1153
1154
791M
    dist += diff * diff;
1155
791M
  }
1156
1157
54.2M
  return dist;
1158
54.2M
}
1159
1160
static WORD16 iusace_improve_scf(FLOAT32 *ptr_spec, FLOAT32 *ptr_exp_spec, WORD16 *ptr_quant_spec,
1161
                                 WORD16 *ptr_quant_spec_temp, WORD32 sfb_width, FLOAT32 threshold,
1162
                                 WORD16 scf, WORD16 min_scf, FLOAT32 *dist,
1163
9.11M
                                 WORD16 *ptr_min_calc_scf) {
1164
9.11M
  FLOAT32 sfb_dist;
1165
9.11M
  WORD16 best_scf = scf;
1166
9.11M
  WORD32 j;
1167
1168
9.11M
  sfb_dist = iusace_calc_sfb_dist(ptr_spec, ptr_exp_spec, ptr_quant_spec, sfb_width, scf);
1169
1170
9.11M
  *ptr_min_calc_scf = scf;
1171
1172
9.11M
  if (sfb_dist > (1.25 * threshold)) {
1173
1.83M
    WORD16 estimated_scf = scf;
1174
1.83M
    FLOAT32 best_sfb_dist = sfb_dist;
1175
1.83M
    WORD32 count;
1176
1177
1.83M
    count = 0;
1178
1179
5.68M
    while ((sfb_dist > (1.25 * threshold)) && (count++ < SCF_COUNT_LIMIT_THREE)) {
1180
3.84M
      scf++;
1181
1182
3.84M
      sfb_dist =
1183
3.84M
          iusace_calc_sfb_dist(ptr_spec, ptr_exp_spec, ptr_quant_spec_temp, sfb_width, scf);
1184
1185
3.84M
      if (sfb_dist < best_sfb_dist) {
1186
1.25M
        best_scf = scf;
1187
1.25M
        best_sfb_dist = sfb_dist;
1188
1189
1.25M
        memcpy(ptr_quant_spec, ptr_quant_spec_temp, sfb_width * sizeof(WORD16));
1190
1.25M
      }
1191
3.84M
    }
1192
1193
1.83M
    count = 0;
1194
1.83M
    scf = estimated_scf;
1195
1.83M
    sfb_dist = best_sfb_dist;
1196
1197
2.67M
    while ((sfb_dist > (1.25 * threshold)) && (count++ < SCF_COUNT_LIMIT_ONE) &&
1198
840k
      (scf > min_scf)) {
1199
833k
      scf--;
1200
1201
833k
      sfb_dist =
1202
833k
          iusace_calc_sfb_dist(ptr_spec, ptr_exp_spec, ptr_quant_spec_temp, sfb_width, scf);
1203
1204
833k
      if (sfb_dist < best_sfb_dist) {
1205
719k
        best_scf = scf;
1206
719k
        best_sfb_dist = sfb_dist;
1207
1208
719k
        memcpy(ptr_quant_spec, ptr_quant_spec_temp, sfb_width * sizeof(WORD16));
1209
719k
      }
1210
833k
      *ptr_min_calc_scf = scf;
1211
833k
    }
1212
1.83M
    *dist = best_sfb_dist;
1213
7.27M
  } else {
1214
7.27M
    FLOAT32 best_sfb_dist = sfb_dist;
1215
7.27M
    FLOAT32 allowed_sfb_dist = MIN(sfb_dist * 1.25f, threshold);
1216
7.27M
    WORD32 count;
1217
1218
29.1M
    for (count = 0; count < SCF_COUNT_LIMIT_THREE; count++) {
1219
21.8M
      scf++;
1220
1221
21.8M
      sfb_dist =
1222
21.8M
          iusace_calc_sfb_dist(ptr_spec, ptr_exp_spec, ptr_quant_spec_temp, sfb_width, scf);
1223
1224
21.8M
      if (sfb_dist < allowed_sfb_dist) {
1225
3.46M
        *ptr_min_calc_scf = best_scf + 1;
1226
1227
3.46M
        best_scf = scf;
1228
3.46M
        best_sfb_dist = sfb_dist;
1229
3.46M
        memcpy(ptr_quant_spec, ptr_quant_spec_temp, sfb_width * sizeof(WORD16));
1230
3.46M
      }
1231
21.8M
    }
1232
7.27M
    *dist = best_sfb_dist;
1233
7.27M
  }
1234
1235
168M
  for (j = 0; j < sfb_width; j++) {
1236
159M
    if (ptr_spec[j] < 0) {
1237
76.3M
      ptr_quant_spec[j] = -ptr_quant_spec[j];
1238
76.3M
    }
1239
159M
  }
1240
1241
9.11M
  return best_scf;
1242
9.11M
}
1243
1244
static VOID iusace_assimilate_single_scf(ia_psy_mod_out_data_struct *pstr_psy_out,
1245
                                         FLOAT32 *ptr_exp_spec, WORD16 *ptr_quant_spec,
1246
                                         WORD16 *ptr_quant_spec_temp, WORD16 *scf,
1247
                                         WORD16 *ptr_min_scf, FLOAT32 *ptr_sfb_dist,
1248
                                         FLOAT32 *ptr_sfb_const_pe_part,
1249
                                         FLOAT32 *ptr_sfb_form_fac, FLOAT32 *ptr_sfb_num_lines,
1250
441k
                                         WORD16 *ptr_min_calc_scf, FLOAT32 *ptr_mdct_spec_float) {
1251
441k
  WORD32 sfb_prev, sfb_act, sfb_next;
1252
441k
  WORD16 scf_act = 0, *scf_prev, *scf_next, min_scf, max_scf;
1253
441k
  WORD32 sfb_width, sfb_offs;
1254
441k
  FLOAT32 energy;
1255
441k
  FLOAT32 sfb_pe_prev, sfb_pe_new;
1256
441k
  FLOAT32 sfb_dist_new;
1257
441k
  WORD32 j;
1258
441k
  WORD32 success = 0;
1259
441k
  FLOAT32 delta_pe = 0.0f, delta_pe_new, delta_pe_temp;
1260
441k
  WORD16 prev_scf_last[MAX_NUM_GROUPED_SFB], prev_scf_next[MAX_NUM_GROUPED_SFB];
1261
441k
  FLOAT32 delta_pe_last[MAX_NUM_GROUPED_SFB];
1262
441k
  WORD32 update_min_scf;
1263
1264
20.8M
  for (j = 0; j < pstr_psy_out->sfb_count; j++) {
1265
20.4M
    prev_scf_last[j] = SHRT_MAX;
1266
20.4M
    prev_scf_next[j] = SHRT_MAX;
1267
20.4M
    delta_pe_last[j] = MAX_FLT_VAL;
1268
20.4M
  }
1269
1270
441k
  sfb_prev = -1;
1271
441k
  sfb_act = -1;
1272
441k
  sfb_next = -1;
1273
441k
  scf_prev = 0;
1274
441k
  scf_next = 0;
1275
441k
  min_scf = SHRT_MAX;
1276
441k
  max_scf = SHRT_MAX;
1277
1278
40.4M
  do {
1279
40.4M
    sfb_next++;
1280
1281
65.4M
    while ((sfb_next < pstr_psy_out->sfb_count) && (scf[sfb_next] == SHRT_MIN)) {
1282
25.0M
      sfb_next++;
1283
25.0M
    }
1284
1285
40.4M
    if ((sfb_prev >= 0) && (sfb_act >= 0) && (sfb_next < pstr_psy_out->sfb_count)) {
1286
33.8M
      scf_act = scf[sfb_act];
1287
1288
33.8M
      scf_prev = scf + sfb_prev;
1289
33.8M
      scf_next = scf + sfb_next;
1290
1291
33.8M
      min_scf = MIN(*scf_prev, *scf_next);
1292
1293
33.8M
      max_scf = MAX(*scf_prev, *scf_next);
1294
33.8M
    } else {
1295
6.61M
      if ((sfb_prev == -1) && (sfb_act >= 0) && (sfb_next < pstr_psy_out->sfb_count)) {
1296
3.06M
        scf_act = scf[sfb_act];
1297
1298
3.06M
        scf_prev = &scf_act;
1299
1300
3.06M
        scf_next = scf + sfb_next;
1301
1302
3.06M
        min_scf = *scf_next;
1303
1304
3.06M
        max_scf = *scf_next;
1305
3.54M
      } else {
1306
3.54M
        if ((sfb_prev >= 0) && (sfb_act >= 0) && (sfb_next == pstr_psy_out->sfb_count)) {
1307
341k
          scf_act = scf[sfb_act];
1308
1309
341k
          scf_prev = scf + sfb_prev;
1310
1311
341k
          scf_next = &scf_act;
1312
1313
341k
          min_scf = *scf_prev;
1314
1315
341k
          max_scf = *scf_prev;
1316
341k
        }
1317
3.54M
      }
1318
6.61M
    }
1319
1320
40.4M
    if (sfb_act >= 0) {
1321
37.2M
      min_scf = MAX(min_scf, ptr_min_scf[sfb_act]);
1322
37.2M
    }
1323
1324
40.4M
    if ((sfb_act >= 0) && (sfb_prev >= 0 || sfb_next < pstr_psy_out->sfb_count) &&
1325
37.2M
        (scf_act > min_scf) && (scf_act <= min_scf + MAX_SCF_DELTA) &&
1326
18.0M
        (scf_act >= max_scf - MAX_SCF_DELTA) &&
1327
18.0M
        (*scf_prev != prev_scf_last[sfb_act] || *scf_next != prev_scf_next[sfb_act] ||
1328
10.2M
         delta_pe < delta_pe_last[sfb_act])) {
1329
10.2M
      success = 0;
1330
1331
10.2M
      sfb_width = pstr_psy_out->sfb_offsets[sfb_act + 1] - pstr_psy_out->sfb_offsets[sfb_act];
1332
1333
10.2M
      sfb_offs = pstr_psy_out->sfb_offsets[sfb_act];
1334
1335
10.2M
      energy = pstr_psy_out->ptr_sfb_energy[sfb_act];
1336
1337
10.2M
      if (ptr_sfb_const_pe_part[sfb_act] == MIN_FLT_VAL) {
1338
5.64M
        ptr_sfb_const_pe_part[sfb_act] =
1339
5.64M
            (FLOAT32)log(energy * (FLOAT32)6.75f / ptr_sfb_form_fac[sfb_act]) * LOG2_1;
1340
5.64M
      }
1341
1342
10.2M
      sfb_pe_prev = iusace_calc_single_spec_pe(scf_act, ptr_sfb_const_pe_part[sfb_act],
1343
10.2M
                                               ptr_sfb_num_lines[sfb_act]) +
1344
10.2M
                    iusace_count_single_scf_bits(scf_act, *scf_prev, *scf_next);
1345
1346
10.2M
      delta_pe_new = delta_pe;
1347
10.2M
      update_min_scf = 1;
1348
1349
34.5M
      do {
1350
34.5M
        scf_act--;
1351
1352
34.5M
        if (scf_act < ptr_min_calc_scf[sfb_act] && scf_act >= max_scf - MAX_SCF_DELTA) {
1353
27.0M
          sfb_pe_new = iusace_calc_single_spec_pe(scf_act, ptr_sfb_const_pe_part[sfb_act],
1354
27.0M
                                                  ptr_sfb_num_lines[sfb_act]) +
1355
27.0M
                       (FLOAT32)iusace_count_single_scf_bits(scf_act, *scf_prev, *scf_next);
1356
1357
27.0M
          delta_pe_temp = delta_pe + sfb_pe_new - sfb_pe_prev;
1358
1359
27.0M
          if (delta_pe_temp < (FLOAT32)10.0f) {
1360
10.3M
            sfb_dist_new =
1361
10.3M
                iusace_calc_sfb_dist(ptr_mdct_spec_float + sfb_offs, ptr_exp_spec + sfb_offs,
1362
10.3M
                                     ptr_quant_spec_temp + sfb_offs, sfb_width, scf_act);
1363
1364
10.3M
            if (sfb_dist_new < ptr_sfb_dist[sfb_act]) {
1365
5.03M
              scf[sfb_act] = scf_act;
1366
5.03M
              ptr_sfb_dist[sfb_act] = sfb_dist_new;
1367
1368
42.6M
              for (j = sfb_offs; j < sfb_offs + sfb_width; j++) {
1369
37.6M
                ptr_quant_spec[j] = ptr_quant_spec_temp[j];
1370
1371
37.6M
                if (ptr_mdct_spec_float[j] < 0.0f) {
1372
17.3M
                  ptr_quant_spec[j] = -ptr_quant_spec[j];
1373
17.3M
                }
1374
37.6M
              }
1375
5.03M
              delta_pe_new = delta_pe_temp;
1376
5.03M
              success = 1;
1377
5.03M
            }
1378
1379
10.3M
            if (update_min_scf) {
1380
9.88M
              ptr_min_calc_scf[sfb_act] = scf_act;
1381
9.88M
            }
1382
16.6M
          } else {
1383
16.6M
            update_min_scf = 0;
1384
16.6M
          }
1385
27.0M
        }
1386
34.5M
      } while (scf_act > min_scf);
1387
1388
10.2M
      delta_pe = delta_pe_new;
1389
1390
10.2M
      prev_scf_last[sfb_act] = *scf_prev;
1391
10.2M
      prev_scf_next[sfb_act] = *scf_next;
1392
10.2M
      delta_pe_last[sfb_act] = delta_pe;
1393
10.2M
    }
1394
1395
40.4M
    if (success) {
1396
2.75M
      sfb_prev = -1;
1397
2.75M
      sfb_act = -1;
1398
2.75M
      sfb_next = -1;
1399
2.75M
      scf_prev = 0;
1400
2.75M
      scf_next = 0;
1401
2.75M
      min_scf = SHRT_MAX;
1402
2.75M
      max_scf = SHRT_MAX;
1403
2.75M
      success = 0;
1404
37.6M
    } else {
1405
37.6M
      sfb_prev = sfb_act;
1406
37.6M
      sfb_act = sfb_next;
1407
37.6M
    }
1408
40.4M
  } while (sfb_next < pstr_psy_out->sfb_count);
1409
441k
  return;
1410
441k
}
1411
1412
static VOID iusace_assimilate_multiple_scf(ia_psy_mod_out_data_struct *pstr_psy_out,
1413
                                           FLOAT32 *ptr_exp_spec, WORD16 *ptr_quant_spec,
1414
                                           WORD16 *ptr_quant_spec_temp, WORD16 *ptr_scf,
1415
                                           WORD16 *ptr_min_scf, FLOAT32 *ptr_sfb_dist,
1416
                                           FLOAT32 *ptr_sfb_const_pe_part,
1417
                                           FLOAT32 *ptr_sfb_form_fac, FLOAT32 *ptr_sfb_num_lines,
1418
441k
                                           FLOAT32 *ptr_mdct_spec_float, pUWORD8 pstr_scratch) {
1419
441k
  WORD32 sfb, start_sfb, stop_sfb;
1420
441k
  WORD16 scf_temp[MAX_NUM_GROUPED_SFB], min_scf, max_scf, scf_act;
1421
441k
  WORD32 possible_region_found;
1422
441k
  WORD32 sfb_width, sfb_offs, j;
1423
441k
  FLOAT32 prev_dist_sum, new_dist_sum;
1424
441k
  WORD32 delta_scf_bits;
1425
441k
  FLOAT32 delta_spec_pe;
1426
441k
  FLOAT32 delta_pe = 0.0f, delta_pe_new;
1427
441k
  WORD32 sfb_count = pstr_psy_out->sfb_count;
1428
441k
  FLOAT32 *sfb_dist_new = (FLOAT32 *)pstr_scratch;
1429
441k
  min_scf = SHRT_MAX;
1430
441k
  max_scf = SHRT_MIN;
1431
1432
20.8M
  for (sfb = 0; sfb < sfb_count; sfb++) {
1433
20.4M
    if (ptr_scf[sfb] != SHRT_MIN) {
1434
9.11M
      min_scf = MIN(min_scf, ptr_scf[sfb]);
1435
1436
9.11M
      max_scf = MAX(max_scf, ptr_scf[sfb]);
1437
9.11M
    }
1438
20.4M
  }
1439
1440
441k
  if (max_scf != SHRT_MIN && max_scf <= min_scf + MAX_SCF_DELTA) {
1441
317k
    scf_act = max_scf;
1442
1443
5.58M
    do {
1444
5.58M
      scf_act--;
1445
1446
5.58M
      memcpy(scf_temp, ptr_scf, MAX_NUM_GROUPED_SFB * sizeof(WORD16));
1447
1448
5.58M
      stop_sfb = 0;
1449
1450
15.7M
      do {
1451
15.7M
        sfb = stop_sfb;
1452
1453
177M
        while (sfb < sfb_count && (ptr_scf[sfb] == SHRT_MIN || ptr_scf[sfb] <= scf_act)) {
1454
162M
          sfb++;
1455
162M
        }
1456
1457
15.7M
        start_sfb = sfb;
1458
1459
15.7M
        sfb++;
1460
1461
108M
        while (sfb < sfb_count && (ptr_scf[sfb] == SHRT_MIN || ptr_scf[sfb] > scf_act)) {
1462
92.6M
          sfb++;
1463
92.6M
        }
1464
1465
15.7M
        stop_sfb = sfb;
1466
1467
15.7M
        possible_region_found = 0;
1468
1469
15.7M
        if (start_sfb < sfb_count) {
1470
10.1M
          possible_region_found = 1;
1471
1472
99.6M
          for (sfb = start_sfb; sfb < stop_sfb; sfb++) {
1473
90.6M
            if (ptr_scf[sfb] != SHRT_MIN) {
1474
55.9M
              if (scf_act < ptr_min_scf[sfb]) {
1475
1.21M
                possible_region_found = 0;
1476
1.21M
                break;
1477
1.21M
              }
1478
55.9M
            }
1479
90.6M
          }
1480
10.1M
        }
1481
1482
15.7M
        if (possible_region_found) {
1483
97.9M
          for (sfb = start_sfb; sfb < stop_sfb; sfb++) {
1484
89.0M
            if (scf_temp[sfb] != SHRT_MIN) {
1485
54.4M
              scf_temp[sfb] = scf_act;
1486
54.4M
            }
1487
89.0M
          }
1488
1489
8.98M
          delta_scf_bits =
1490
8.98M
              iusace_count_scf_bits_diff(ptr_scf, scf_temp, sfb_count, start_sfb, stop_sfb);
1491
1492
8.98M
          delta_spec_pe =
1493
8.98M
              iusace_calc_spec_pe_diff(pstr_psy_out, ptr_scf, scf_temp, ptr_sfb_const_pe_part,
1494
8.98M
                                       ptr_sfb_form_fac, ptr_sfb_num_lines, start_sfb, stop_sfb);
1495
1496
8.98M
          delta_pe_new = delta_pe + (FLOAT32)delta_scf_bits + delta_spec_pe;
1497
1498
8.98M
          if (delta_pe_new < (FLOAT32)10.0f) {
1499
3.76M
            prev_dist_sum = new_dist_sum = 0.0f;
1500
1501
20.5M
            for (sfb = start_sfb; sfb < stop_sfb; sfb++) {
1502
18.0M
              if (scf_temp[sfb] != SHRT_MIN) {
1503
8.22M
                prev_dist_sum += ptr_sfb_dist[sfb];
1504
1505
8.22M
                sfb_width = pstr_psy_out->sfb_offsets[sfb + 1] - pstr_psy_out->sfb_offsets[sfb];
1506
1507
8.22M
                sfb_offs = pstr_psy_out->sfb_offsets[sfb];
1508
1509
8.22M
                sfb_dist_new[sfb] =
1510
8.22M
                    iusace_calc_sfb_dist(ptr_mdct_spec_float + sfb_offs, ptr_exp_spec + sfb_offs,
1511
8.22M
                                         ptr_quant_spec_temp + sfb_offs, sfb_width, scf_act);
1512
1513
8.22M
                if (sfb_dist_new[sfb] > pstr_psy_out->ptr_sfb_thr[sfb]) {
1514
1.26M
                  new_dist_sum = (FLOAT32)2.0f * prev_dist_sum;
1515
1.26M
                  break;
1516
1.26M
                }
1517
1518
6.95M
                new_dist_sum += sfb_dist_new[sfb];
1519
6.95M
              }
1520
18.0M
            }
1521
1522
3.76M
            if (new_dist_sum < prev_dist_sum) {
1523
687k
              delta_pe = delta_pe_new;
1524
1525
4.48M
              for (sfb = start_sfb; sfb < stop_sfb; sfb++) {
1526
3.80M
                if (ptr_scf[sfb] != SHRT_MIN) {
1527
1.72M
                  sfb_width = pstr_psy_out->sfb_offsets[sfb + 1] - pstr_psy_out->sfb_offsets[sfb];
1528
1529
1.72M
                  sfb_offs = pstr_psy_out->sfb_offsets[sfb];
1530
1.72M
                  ptr_scf[sfb] = scf_act;
1531
1.72M
                  ptr_sfb_dist[sfb] = sfb_dist_new[sfb];
1532
1533
20.4M
                  for (j = sfb_offs; j < sfb_offs + sfb_width; j++) {
1534
18.7M
                    ptr_quant_spec[j] = ptr_quant_spec_temp[j];
1535
1536
18.7M
                    if (ptr_mdct_spec_float[j] < 0.0f) {
1537
8.20M
                      ptr_quant_spec[j] = -ptr_quant_spec[j];
1538
8.20M
                    }
1539
18.7M
                  }
1540
1.72M
                }
1541
3.80M
              }
1542
687k
            }
1543
3.76M
          }
1544
8.98M
        }
1545
1546
15.7M
      } while (stop_sfb <= sfb_count);
1547
1548
5.58M
    } while (scf_act > min_scf);
1549
317k
  }
1550
441k
  return;
1551
441k
}
1552
1553
VOID iusace_estimate_scfs_chan(ia_psy_mod_out_data_struct *pstr_psy_out,
1554
                               ia_qc_out_chan_struct *str_qc_out_chan, WORD32 num_channels,
1555
239k
                               WORD32 chn, iusace_scratch_mem *pstr_scratch) {
1556
239k
  WORD16 *ptr_scalefactor;
1557
239k
  WORD32 *global_gain;
1558
239k
  FLOAT32 *p_sfb_form_factor;
1559
239k
  FLOAT32 *p_sfb_num_relevant_lines;
1560
239k
  WORD16 *ptr_quant_spec;
1561
239k
  WORD32 i, ch, j, idx = 0;
1562
239k
  FLOAT32 thresh, energy, energy_part, thr_part;
1563
239k
  FLOAT32 scf_float;
1564
239k
  WORD16 scf_int = 0, min_scf = 0, max_scf = 0;
1565
239k
  FLOAT64 max_spec = 0.0f;
1566
239k
  WORD16 min_sf_max_quant[MAX_NUM_GROUPED_SFB] = {0};
1567
239k
  pUWORD8 ptr_scratch = pstr_scratch->ptr_fd_scratch;
1568
239k
  FLOAT32 *ptr_sfb_dist = (FLOAT32 *)ptr_scratch;
1569
239k
  ptr_scratch += MAX_NUM_GROUPED_SFB * sizeof(ptr_sfb_dist[0]);
1570
239k
  WORD16 min_calc_scf[MAX_NUM_GROUPED_SFB] = {0};
1571
1572
239k
  WORD16 *ptr_quant_spec_temp = pstr_scratch->p_adjthr_quant_spec_temp;
1573
239k
  FLOAT32 *ptr_exp_spec = pstr_scratch->p_adjthr_ptr_exp_spec;
1574
239k
  FLOAT32 *ptr_mdct_spec_float = pstr_scratch->p_adjthr_mdct_spec_float;
1575
239k
  FLOAT32 *sfb_const_pe_part = (FLOAT32 *)ptr_scratch;
1576
1577
239k
  FLOAT32 **ptr_sfb_form_factor = &pstr_scratch->ptr_sfb_form_fac[0];
1578
239k
  FLOAT32 **ptr_sfb_num_relevant_lines = &pstr_scratch->ptr_sfb_num_relevant_lines[0];
1579
1580
239k
  ptr_scratch += MAX_NUM_GROUPED_SFB * sizeof(sfb_const_pe_part[0]);
1581
1582
239k
  memset(ptr_quant_spec_temp, 0, FRAME_LEN_LONG * sizeof(WORD16));
1583
239k
  memset(ptr_mdct_spec_float, 0, FRAME_LEN_LONG * sizeof(FLOAT32));
1584
239k
  memset(ptr_exp_spec, 0, FRAME_LEN_LONG * sizeof(FLOAT32));
1585
239k
  memset(ptr_sfb_dist, 0, MAX_NUM_GROUPED_SFB * sizeof(FLOAT32));
1586
681k
  for (ch = chn; ch < chn + num_channels; ch++) {
1587
441k
    ia_psy_mod_out_data_struct *ptr_psy_out = &pstr_psy_out[ch];
1588
441k
    str_qc_out_chan[idx].global_gain = 0;
1589
1590
441k
    memset(str_qc_out_chan[idx].scalefactor, 0,
1591
441k
           sizeof(str_qc_out_chan[idx].scalefactor[0]) * pstr_psy_out[ch].sfb_count);
1592
441k
    memset(str_qc_out_chan[idx].quant_spec, 0,
1593
441k
           sizeof(str_qc_out_chan[idx].quant_spec[0]) * FRAME_LEN_LONG);
1594
1595
441k
    ptr_scalefactor = str_qc_out_chan[idx].scalefactor;
1596
441k
    global_gain = &str_qc_out_chan[idx].global_gain;
1597
441k
    p_sfb_form_factor = &ptr_sfb_form_factor[idx][0];
1598
441k
    p_sfb_num_relevant_lines = &ptr_sfb_num_relevant_lines[idx][0];
1599
441k
    ptr_quant_spec = str_qc_out_chan[idx].quant_spec;
1600
20.8M
    for (i = 0; i < ptr_psy_out->sfb_count; i++) {
1601
20.4M
      thresh = ptr_psy_out->ptr_sfb_thr[i];
1602
20.4M
      energy = ptr_psy_out->ptr_sfb_energy[i];
1603
20.4M
      max_spec = 0.0;
1604
1605
431M
      for (j = ptr_psy_out->sfb_offsets[i]; j < ptr_psy_out->sfb_offsets[i + 1]; j++) {
1606
410M
        max_spec = MAX(max_spec, fabs(ptr_psy_out->ptr_spec_coeffs[j]));
1607
410M
      }
1608
1609
20.4M
      ptr_scalefactor[i] = MIN_SHRT_VAL;
1610
20.4M
      min_sf_max_quant[i] = MIN_SHRT_VAL;
1611
1612
20.4M
      if ((max_spec > 0.0) && (energy > thresh) && (p_sfb_form_factor[i] != MIN_FLT_VAL)) {
1613
9.11M
        energy_part = (FLOAT32)log10(p_sfb_form_factor[i] + FLT_EPSILON);
1614
1615
9.11M
        thr_part = (FLOAT32)log10(6.75 * thresh + MIN_FLT_VAL);
1616
9.11M
        scf_float = 8.8585f * (thr_part - energy_part);
1617
9.11M
        scf_int = (WORD16)floor(scf_float);
1618
9.11M
        min_sf_max_quant[i] = (WORD16)ceil(C1_SF + C2_SF * log(max_spec));
1619
9.11M
        scf_int = MAX(scf_int, min_sf_max_quant[i]);
1620
9.11M
        scf_int = MAX(scf_int, MIN_GAIN_INDEX);
1621
9.11M
        scf_int = MIN(scf_int, (MAX_GAIN_INDEX - SCF_COUNT_LIMIT_THREE));
1622
168M
        for (j = 0; j < ptr_psy_out->sfb_offsets[i + 1] - ptr_psy_out->sfb_offsets[i]; j++) {
1623
159M
          ptr_exp_spec[ptr_psy_out->sfb_offsets[i] + j] =
1624
159M
              (FLOAT32)(ptr_psy_out->ptr_spec_coeffs[ptr_psy_out->sfb_offsets[i] + j]);
1625
159M
          ptr_mdct_spec_float[ptr_psy_out->sfb_offsets[i] + j] =
1626
159M
              (FLOAT32)(ptr_psy_out->ptr_spec_coeffs[ptr_psy_out->sfb_offsets[i] + j]);
1627
159M
        }
1628
1629
9.11M
        iusace_calculate_exp_spec(ptr_psy_out->sfb_offsets[i + 1] - ptr_psy_out->sfb_offsets[i],
1630
9.11M
                                  ptr_exp_spec + ptr_psy_out->sfb_offsets[i],
1631
9.11M
                                  ptr_mdct_spec_float + ptr_psy_out->sfb_offsets[i]);
1632
1633
9.11M
        scf_int = iusace_improve_scf(
1634
9.11M
            ptr_mdct_spec_float + ptr_psy_out->sfb_offsets[i],
1635
9.11M
            ptr_exp_spec + ptr_psy_out->sfb_offsets[i],
1636
9.11M
            ptr_quant_spec + ptr_psy_out->sfb_offsets[i],
1637
9.11M
            ptr_quant_spec_temp + ptr_psy_out->sfb_offsets[i],
1638
9.11M
            ptr_psy_out->sfb_offsets[i + 1] - ptr_psy_out->sfb_offsets[i], thresh, scf_int,
1639
9.11M
            min_sf_max_quant[i], &ptr_sfb_dist[i], &min_calc_scf[i]);
1640
1641
9.11M
        ptr_scalefactor[i] = scf_int;
1642
9.11M
      }
1643
20.4M
    }
1644
1645
20.8M
    for (i = 0; i < ptr_psy_out->sfb_count; i++) {
1646
20.4M
      sfb_const_pe_part[i] = MIN_FLT_VAL;
1647
20.4M
    }
1648
1649
441k
    iusace_assimilate_single_scf(ptr_psy_out, ptr_exp_spec, ptr_quant_spec, ptr_quant_spec_temp,
1650
441k
                                 ptr_scalefactor, min_sf_max_quant, ptr_sfb_dist,
1651
441k
                                 sfb_const_pe_part, p_sfb_form_factor, p_sfb_num_relevant_lines,
1652
441k
                                 min_calc_scf, ptr_mdct_spec_float);
1653
1654
441k
    iusace_assimilate_multiple_scf(ptr_psy_out, ptr_exp_spec, ptr_quant_spec, ptr_quant_spec_temp,
1655
441k
                                   ptr_scalefactor, min_sf_max_quant, ptr_sfb_dist,
1656
441k
                                   sfb_const_pe_part, p_sfb_form_factor, p_sfb_num_relevant_lines,
1657
441k
                                   ptr_mdct_spec_float, ptr_scratch);
1658
1659
441k
    max_scf = MIN_SHRT_VAL;
1660
441k
    min_scf = MAX_SHRT_VAL;
1661
20.8M
    for (i = 0; i < ptr_psy_out->sfb_count; i++) {
1662
20.4M
      if (max_scf < ptr_scalefactor[i]) {
1663
850k
        max_scf = ptr_scalefactor[i];
1664
850k
      }
1665
20.4M
      if ((ptr_scalefactor[i] != MIN_SHRT_VAL) && (min_scf > ptr_scalefactor[i])) {
1666
1.52M
        min_scf = ptr_scalefactor[i];
1667
1.52M
      }
1668
20.4M
    }
1669
1670
20.8M
    for (i = 0; i < pstr_psy_out[ch].sfb_count; i++) {
1671
20.4M
      if ((ptr_scalefactor[i] != MIN_SHRT_VAL) &&
1672
9.11M
          (min_scf + MAX_SCF_DELTA) < ptr_scalefactor[i]) {
1673
1.52k
        ptr_scalefactor[i] = min_scf + MAX_SCF_DELTA;
1674
1675
1.52k
        iusace_calc_sfb_dist(ptr_mdct_spec_float + ptr_psy_out->sfb_offsets[i],
1676
1.52k
                             ptr_exp_spec + ptr_psy_out->sfb_offsets[i],
1677
1.52k
                             ptr_quant_spec + ptr_psy_out->sfb_offsets[i],
1678
1.52k
                             ptr_psy_out->sfb_offsets[i + 1] - ptr_psy_out->sfb_offsets[i],
1679
1.52k
                             ptr_scalefactor[i]);
1680
1.52k
      }
1681
20.4M
    }
1682
1683
441k
    max_scf = MIN((min_scf + MAX_SCF_DELTA), max_scf);
1684
1685
441k
    if (max_scf > MIN_SHRT_VAL) {
1686
319k
      *global_gain = max_scf;
1687
15.3M
      for (i = 0; i < ptr_psy_out->sfb_count; i++) {
1688
15.0M
        if (ptr_scalefactor[i] == MIN_SHRT_VAL) {
1689
5.90M
          ptr_scalefactor[i] = 0;
1690
5.90M
          memset(
1691
5.90M
              &ptr_psy_out->ptr_spec_coeffs[ptr_psy_out->sfb_offsets[i]], 0,
1692
5.90M
              (ptr_psy_out->sfb_offsets[i + 1] - ptr_psy_out->sfb_offsets[i]) * sizeof(FLOAT64));
1693
9.11M
        } else {
1694
9.11M
          ptr_scalefactor[i] = max_scf - ptr_scalefactor[i];
1695
9.11M
        }
1696
15.0M
      }
1697
319k
    } else {
1698
122k
      *global_gain = 0;
1699
5.55M
      for (i = 0; i < ptr_psy_out->sfb_count; i++) {
1700
5.43M
        ptr_scalefactor[i] = 0;
1701
5.43M
        memset(&ptr_psy_out->ptr_spec_coeffs[ptr_psy_out->sfb_offsets[i]], 0,
1702
5.43M
               (ptr_psy_out->sfb_offsets[i + 1] - ptr_psy_out->sfb_offsets[i]) * sizeof(FLOAT64));
1703
5.43M
      }
1704
122k
    }
1705
441k
    idx++;
1706
441k
  }
1707
1708
239k
  return;
1709
239k
}