Coverage Report

Created: 2025-09-17 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/ixheaace_fd_quant.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 "iusace_type_def.h"
24
#include "ixheaac_error_standards.h"
25
#include "ixheaace_error_codes.h"
26
#include "ixheaace_mps_common_define.h"
27
#include "iusace_cnst.h"
28
#include "iusace_block_switch_const.h"
29
#include "iusace_bitbuffer.h"
30
#include "impd_drc_common_enc.h"
31
#include "impd_drc_uni_drc.h"
32
#include "impd_drc_api.h"
33
#include "impd_drc_uni_drc_eq.h"
34
#include "impd_drc_uni_drc_filter_bank.h"
35
#include "impd_drc_gain_enc.h"
36
#include "impd_drc_struct_def.h"
37
38
#include "ixheaace_memory_standards.h"
39
#include "iusace_tns_usac.h"
40
#include "iusace_psy_mod.h"
41
#include "iusace_config.h"
42
#include "iusace_ms.h"
43
#include "ixheaace_adjust_threshold_data.h"
44
#include "iusace_fd_qc_util.h"
45
#include "iusace_arith_enc.h"
46
#include "iusace_fd_quant.h"
47
#include "iusace_signal_classifier.h"
48
#include "iusace_block_switch_struct_def.h"
49
#include "ixheaace_sbr_header.h"
50
#include "ixheaace_config.h"
51
#include "ixheaace_asc_write.h"
52
#include "iusace_main.h"
53
#include "iusace_write_bitstream.h"
54
#include "ixheaace_nf.h"
55
#include "iusace_fd_qc_adjthr.h"
56
#include "iusace_block_switch_const.h"
57
#include "iusace_rom.h"
58
#include "ixheaace_cplx_pred.h"
59
#include "ixheaace_aac_constants.h"
60
61
static WORD32 iusace_window_shape[5] = {WIN_SEL_1, WIN_SEL_0, WIN_SEL_0, WIN_SEL_1, WIN_SEL_0};
62
63
22.3k
static WORD32 iusace_count_ms_bits(WORD32 sfb_count, WORD32 sfb_per_grp, WORD32 max_sfb_per_grp) {
64
22.3k
  WORD32 ms_bits = 0, sfb_offs, sfb;
65
81.2k
  for (sfb_offs = 0; sfb_offs < sfb_count; sfb_offs += sfb_per_grp) {
66
1.14M
    for (sfb = 0; sfb < max_sfb_per_grp; sfb++) {
67
1.08M
      ms_bits++;
68
1.08M
    }
69
58.8k
  }
70
22.3k
  return (ms_bits);
71
22.3k
}
72
73
static WORD32 iusace_count_static_bits(ia_usac_data_struct *ptr_usac_data,
74
                                       ia_usac_encoder_config_struct *ptr_usac_config,
75
                                       ia_sfb_params_struct *pstr_sfb_params,
76
                                       ia_psy_mod_out_data_struct *pstr_psy_out, WORD32 channels,
77
245k
                                       WORD32 chn, WORD32 usac_independency_flag, WORD32 ele_id) {
78
245k
  WORD32 ms_mask = ptr_usac_data->str_ms_info[chn].ms_mask;
79
245k
  WORD32 noise_filling = ptr_usac_data->noise_filling[ele_id];
80
245k
  WORD32 stat_bits = 0, i;
81
245k
  WORD32 tns_active = 0, tns_present_both = 0;
82
245k
  WORD32 max_sfb = pstr_psy_out[chn].max_sfb_per_grp;
83
245k
  (VOID) ptr_usac_config;
84
85
245k
  if (channels == 1) {
86
36.2k
    stat_bits += 1;  // core mode
87
36.2k
    stat_bits += 1;  // tns active
88
89
36.2k
    switch (pstr_psy_out[chn].window_sequence) {
90
21.3k
      case ONLY_LONG_SEQUENCE:
91
25.5k
      case LONG_START_SEQUENCE:
92
28.2k
      case LONG_STOP_SEQUENCE:
93
28.2k
        stat_bits += SI_ICS_INFO_BITS_LONG;
94
28.2k
        break;
95
7.90k
      case EIGHT_SHORT_SEQUENCE:
96
7.90k
        stat_bits += SI_ICS_INFO_BITS_SHORT;
97
7.90k
        break;
98
36.2k
    }
99
209k
  } else {
100
209k
    stat_bits += 2;  // core mode
101
209k
    stat_bits += 2;  // tns and common window
102
103
209k
    switch (pstr_psy_out[chn].window_sequence) {
104
87.3k
      case ONLY_LONG_SEQUENCE:
105
112k
      case LONG_START_SEQUENCE:
106
135k
      case LONG_STOP_SEQUENCE:
107
135k
        stat_bits += SI_ICS_INFO_BITS_LONG;
108
135k
        break;
109
73.9k
      case EIGHT_SHORT_SEQUENCE:
110
73.9k
        stat_bits += SI_ICS_INFO_BITS_SHORT;
111
73.9k
        break;
112
209k
    }
113
114
209k
    stat_bits += 1;  // common_max_sfb
115
209k
    stat_bits += SI_CPE_MS_MASK_BITS;
116
117
209k
    if (ms_mask != 3) {
118
196k
      if (ms_mask == 1) {
119
22.3k
        stat_bits += iusace_count_ms_bits(pstr_psy_out[chn].sfb_count,
120
22.3k
                                          pstr_psy_out[chn].sfb_per_group, max_sfb);
121
22.3k
      }
122
196k
    } else {
123
12.7k
      stat_bits += iusace_write_cplx_pred_data(
124
12.7k
          NULL, pstr_sfb_params->num_window_groups[chn], max_sfb,
125
12.7k
          ptr_usac_data->complex_coef[chn], ptr_usac_data->pred_coef_re[chn],
126
12.7k
          ptr_usac_data->pred_coef_im[chn], iusace_huffman_code_table, usac_independency_flag,
127
12.7k
          ptr_usac_data->pred_dir_idx[chn], ptr_usac_data->cplx_pred_used[chn],
128
12.7k
          ptr_usac_data->cplx_pred_all[chn], ptr_usac_data->temp_pred_coef_re_prev[chn],
129
12.7k
          ptr_usac_data->temp_pred_coef_im_prev[chn], &ptr_usac_data->delta_code_time[chn]);
130
12.7k
    }
131
132
209k
    if (ptr_usac_data->pstr_tns_info[chn] != NULL &&
133
82.3k
        ptr_usac_data->pstr_tns_info[chn + 1] != NULL) {
134
82.3k
      tns_active = ptr_usac_data->pstr_tns_info[chn]->tns_data_present ||
135
39.8k
                   ptr_usac_data->pstr_tns_info[chn + 1]->tns_data_present;
136
82.3k
      tns_present_both = ptr_usac_data->pstr_tns_info[chn]->tns_data_present &&
137
42.4k
                         ptr_usac_data->pstr_tns_info[chn + 1]->tns_data_present;
138
82.3k
    }
139
209k
    if (tns_active) {
140
46.6k
      stat_bits += 1;  // common_tns
141
142
46.6k
      stat_bits += 1;  // tns_present_both
143
144
46.6k
      if (!tns_present_both) {
145
9.48k
        stat_bits += 1;  // tns_data_present1
146
9.48k
      }
147
46.6k
    }
148
209k
  }
149
150
700k
  for (i = chn; i < chn + channels; i++) {
151
454k
    stat_bits += 8;  // global_gain
152
153
454k
    if (noise_filling) {
154
176k
      stat_bits += 8;
155
176k
    }
156
454k
  }
157
158
700k
  for (i = chn; i < chn + channels; i++) {
159
454k
    if (ptr_usac_data->pstr_tns_info[i] != NULL &&
160
169k
        ptr_usac_data->pstr_tns_info[i]->tns_data_present == 1) {
161
85.9k
      stat_bits += iusace_write_tns_data(NULL, ptr_usac_data->pstr_tns_info[i],
162
85.9k
                                         pstr_psy_out[i].window_sequence, 0);
163
85.9k
    }
164
165
454k
    if (!usac_independency_flag) {
166
434k
      stat_bits += 1;  // arith_reset_flag
167
434k
    }
168
169
454k
    stat_bits += 1;  // fac_data_present
170
454k
    stat_bits += ptr_usac_data->str_scratch.ptr_num_fac_bits[i];
171
454k
  }
172
173
245k
  stat_bits += (ptr_usac_data->num_sbr_bits + ptr_usac_data->num_drc_bits);
174
175
245k
  return stat_bits;
176
245k
}
177
178
static VOID iusace_sort_for_grouping(WORD32 *sfb_offset, const WORD32 *sfb_width_table,
179
                                     FLOAT64 *ptr_scratch, FLOAT64 *ptr_spec,
180
                                     WORD32 num_window_groups, const WORD32 *window_group_length,
181
155k
                                     WORD32 nr_of_sfb, WORD32 ccfl) {
182
155k
  WORD32 i, j, ii;
183
155k
  WORD32 index = 0;
184
155k
  WORD32 group_offset = 0;
185
155k
  WORD32 k = 0;
186
155k
  WORD32 frame_len_short = (ccfl * FRAME_LEN_SHORT_128) / FRAME_LEN_LONG;
187
188
155k
  sfb_offset[k] = 0;
189
2.27M
  for (k = 1; k < nr_of_sfb + 1; k++) {
190
2.12M
    sfb_offset[k] = sfb_offset[k - 1] + sfb_width_table[k - 1];
191
2.12M
  }
192
193
155k
  index = 0;
194
155k
  group_offset = 0;
195
753k
  for (i = 0; i < num_window_groups; i++) {
196
8.74M
    for (k = 0; k < nr_of_sfb; k++) {
197
25.1M
      for (j = 0; j < window_group_length[i]; j++) {
198
158M
        for (ii = 0; ii < sfb_width_table[k]; ii++) {
199
141M
          ptr_scratch[index++] =
200
141M
              ptr_spec[ii + sfb_offset[k] + frame_len_short * j + group_offset];
201
141M
        }
202
16.9M
      }
203
8.14M
    }
204
597k
    group_offset += frame_len_short * window_group_length[i];
205
597k
  }
206
207
155k
  memcpy(ptr_spec, ptr_scratch, ccfl * sizeof(ptr_spec[0]));
208
209
155k
  index = 0;
210
155k
  sfb_offset[index++] = 0;
211
753k
  for (i = 0; i < num_window_groups; i++) {
212
8.74M
    for (k = 0; k < nr_of_sfb; k++) {
213
8.14M
      sfb_offset[index] = sfb_offset[index - 1] + sfb_width_table[k] * window_group_length[i];
214
8.14M
      index++;
215
8.14M
    }
216
597k
  }
217
218
155k
  return;
219
155k
}
220
221
static VOID iusace_degroup_int(const WORD32 *ptr_grouped_sfb_offsets, WORD32 sfb_per_group,
222
                               WORD32 *ptr_scratch, WORD32 *ptr_spec, WORD32 num_window_groups,
223
                               const WORD32 *window_group_length, WORD32 ccfl)
224
225
184k
{
226
184k
  WORD32 i, j, k, n;
227
184k
  WORD32 index, group_offset;
228
184k
  WORD32 loop1, loop2;
229
184k
  WORD32 frame_len_short = (ccfl * FRAME_LEN_SHORT_128) / FRAME_LEN_LONG;
230
184k
  index = 0;
231
184k
  group_offset = 0;
232
233
184k
  memset(ptr_scratch, 0, ccfl * sizeof(WORD32));
234
235
894k
  for (i = 0; i < num_window_groups; i++) {
236
10.2M
    for (j = 0; j < sfb_per_group; j++) {
237
9.49M
      WORD32 idx = i * sfb_per_group + j;
238
9.49M
      loop1 = ((ptr_grouped_sfb_offsets[idx + 1] - ptr_grouped_sfb_offsets[idx]) /
239
9.49M
               window_group_length[i]);
240
241
29.2M
      for (k = 0; k < window_group_length[i]; k++) {
242
19.7M
        loop2 = ((ptr_grouped_sfb_offsets[idx] - group_offset) / window_group_length[i]) +
243
19.7M
                frame_len_short * k + group_offset;
244
245
183M
        for (n = 0; n < loop1; n++) {
246
164M
          ptr_scratch[n + loop2] = ptr_spec[index++];
247
164M
        }
248
19.7M
      }
249
9.49M
    }
250
709k
    group_offset += frame_len_short * window_group_length[i];
251
709k
  }
252
253
184k
  memcpy(ptr_spec, ptr_scratch, ccfl * sizeof(WORD32));
254
255
184k
  return;
256
184k
}
257
258
IA_ERRORCODE iusace_stereo_proc(ia_sfb_params_struct *pstr_sfb_prms,
259
                                WORD32 usac_independancy_flag, ia_usac_data_struct *ptr_usac_data,
260
209k
                                ia_usac_encoder_config_struct *ptr_usac_config, WORD32 chn) {
261
209k
  IA_ERRORCODE err_code;
262
209k
  WORD32 i = 0;
263
209k
  WORD32 j = 0;
264
209k
  iusace_scratch_mem *pstr_scratch = &ptr_usac_data->str_scratch;
265
209k
  ia_psy_mod_data_struct *pstr_psy_data = ptr_usac_data->str_psy_mod.str_psy_data;
266
209k
  ia_psy_mod_out_data_struct *pstr_psy_out = ptr_usac_data->str_psy_mod.str_psy_out_data;
267
209k
  WORD32 *ptr_num_sfb = pstr_sfb_prms->num_sfb;
268
209k
  WORD32 *ptr_num_window_groups = pstr_sfb_prms->num_window_groups;
269
209k
  WORD32 ccfl = ptr_usac_config->ccfl;
270
209k
  ia_ms_info_struct *pstr_ms_info = &ptr_usac_data->str_ms_info[chn];
271
272
209k
  FLOAT64 tmp = 0.0f;
273
209k
  FLOAT32 nrg_mid = 0.0f, nrg_side = 0.0f, nrg_left = 0.0f, nrg_right = 0.0f;
274
209k
  FLOAT64 *ptr_scratch_spec = pstr_scratch->p_quant_spectrum_spec_scratch;
275
209k
  FLOAT32 ratio_mid = 0.0f, ratio_side = 0.0f;
276
209k
  FLOAT32 eps = 1.0e-6f;
277
  /* Save a copy of left and right channel MDCT spectra before they are modified */
278
209k
  memcpy(ptr_usac_data->left_chan_save[chn], ptr_usac_data->spectral_line_vector[chn],
279
209k
         ccfl * sizeof(FLOAT64));
280
209k
  memcpy(ptr_usac_data->right_chan_save[chn], ptr_usac_data->spectral_line_vector[chn + 1],
281
209k
         ccfl * sizeof(FLOAT64));
282
283
209k
  if (ptr_usac_config->cmplx_pred_flag == 1) {
284
    /* Refinement - decision on whether to use complex prediction or MS */
285
141M
    for (i = 0; i < ccfl; i++) {
286
141M
      tmp = ptr_usac_data->spectral_line_vector[chn][i];
287
141M
      ptr_scratch_spec[i] = 0.5f * (ptr_usac_data->spectral_line_vector[chn][i] +
288
141M
                                    ptr_usac_data->spectral_line_vector[chn + 1][i]);
289
141M
      ptr_scratch_spec[ccfl + i] = 0.5f * (tmp - ptr_usac_data->spectral_line_vector[chn + 1][i]);
290
141M
    }
291
292
141M
    for (i = 0; i < ccfl; i++) {
293
141M
      nrg_mid += (FLOAT32)(ptr_scratch_spec[i] * ptr_scratch_spec[i]);
294
141M
      nrg_side += (FLOAT32)(ptr_scratch_spec[ccfl + i] * ptr_scratch_spec[ccfl + i]);
295
141M
      nrg_left += (FLOAT32)(ptr_usac_data->spectral_line_vector[chn][i] *
296
141M
                            ptr_usac_data->spectral_line_vector[chn][i]);
297
141M
      nrg_right += (FLOAT32)(ptr_usac_data->spectral_line_vector[chn + 1][i] *
298
141M
                             ptr_usac_data->spectral_line_vector[chn + 1][i]);
299
141M
    }
300
301
156k
    ratio_mid = nrg_mid / (MAX(nrg_left, nrg_right) + eps);
302
156k
    ratio_side = nrg_side / (MAX(nrg_left, nrg_right) + eps);
303
304
156k
    if (ratio_mid >= 0.8f || ratio_side >= 0.8f || nrg_mid == 0.f || nrg_side == 0.f) {
305
126k
      pstr_ms_info->ms_mask = 0;
306
126k
    } else {
307
30.0k
      pstr_ms_info->ms_mask = 3;
308
30.0k
    }
309
156k
  }
310
311
209k
  if (pstr_ms_info->ms_mask != 3) {
312
179k
    WORD32 idx = 0;
313
179k
    WORD32 sfb;
314
315
6.25M
    for (sfb = 0; sfb < ptr_num_sfb[chn]; sfb++) {
316
6.07M
      ptr_usac_data->pred_coef_re_prev[chn][sfb] = 0;
317
6.07M
      ptr_usac_data->pred_coef_im_prev[chn][sfb] = 0;
318
6.07M
      ptr_usac_data->temp_pred_coef_re_prev[chn][sfb] = 0;
319
6.07M
      ptr_usac_data->temp_pred_coef_im_prev[chn][sfb] = 0;
320
6.07M
    }
321
322
179k
    memset(ptr_usac_data->str_ms_info[chn].ms_used, 0,
323
179k
           MAX_SFB_LONG * MAX_SHORT_WINDOWS * sizeof(WORD32));
324
325
179k
    iusace_ms_apply(pstr_psy_data, ptr_usac_data->spectral_line_vector[chn],
326
179k
                    ptr_usac_data->spectral_line_vector[chn + 1],
327
179k
                    &ptr_usac_data->str_ms_info[chn].ms_mask,
328
179k
                    ptr_usac_data->str_ms_info[chn].ms_used,
329
179k
                    ptr_num_window_groups[chn] * ptr_num_sfb[chn], ptr_num_sfb[chn],
330
179k
                    pstr_psy_out[chn].max_sfb_per_grp, pstr_sfb_prms->grouped_sfb_offset[chn],
331
179k
                    chn, ptr_usac_config->cmplx_pred_flag == 1 ? ptr_scratch_spec : NULL);
332
333
522k
    for (i = 0; i < ptr_num_window_groups[chn]; i++) {
334
8.65M
      for (j = 0; j < ptr_num_sfb[chn]; j++) {
335
8.30M
        pstr_psy_out[chn].ms_used[idx++] = ptr_usac_data->str_ms_info[chn].ms_used[i][j];
336
8.30M
      }
337
343k
    }
338
179k
  } else {
339
    /* Reset buffer to zero */
340
270k
    for (WORD32 group = 0; group < MAX_SHORT_WINDOWS; group++) {
341
240k
      memset(ptr_usac_data->cplx_pred_used[chn][group], 0, MAX_SFB_LONG * sizeof(WORD32));
342
240k
    }
343
344
30.0k
    ptr_usac_data->cplx_pred_all[chn] = 1; /* Disable bandwise switching to L/R */
345
105k
    for (i = 0; i < ptr_num_window_groups[chn]; i++) {
346
798k
      for (j = 0; j < ptr_num_sfb[chn]; j += 2) {
347
723k
        ptr_usac_data->cplx_pred_used[chn][i][j] = 1;
348
723k
        if ((j + 1) < ptr_num_sfb[chn]) {
349
694k
          ptr_usac_data->cplx_pred_used[chn][i][j + 1] = ptr_usac_data->cplx_pred_used[chn][i][j];
350
694k
        }
351
723k
      }
352
75.4k
    }
353
354
30.0k
    err_code = iusace_cplx_pred_proc(
355
30.0k
        ptr_usac_data, ptr_usac_config, usac_independancy_flag, pstr_sfb_prms, chn, pstr_psy_data,
356
30.0k
        pstr_sfb_prms->grouped_sfb_offset[chn], pstr_scratch->p_cmpx_mdct_temp_buf,
357
30.0k
        ptr_scratch_spec, nrg_mid, nrg_side);
358
30.0k
    if (err_code != IA_NO_ERROR) {
359
0
      return err_code;
360
0
    }
361
30.0k
  }
362
209k
  return IA_NO_ERROR;
363
209k
}
364
365
IA_ERRORCODE iusace_grouping(ia_sfb_params_struct *pstr_sfb_prms, WORD32 num_chans,
366
                             ia_usac_data_struct *ptr_usac_data,
367
                             ia_usac_encoder_config_struct *ptr_usac_config, WORD32 chn,
368
245k
                             WORD32 ele_id) {
369
245k
  WORD32 i = 0, grp, sfb, wnd;
370
245k
  WORD32 j = 0;
371
245k
  WORD32 k;
372
245k
  WORD32 ch;
373
245k
  ia_psy_mod_struct *pstr_psy_config = &ptr_usac_data->str_psy_mod;
374
245k
  ia_psy_mod_data_struct *pstr_psy_data = ptr_usac_data->str_psy_mod.str_psy_data;
375
245k
  WORD32 *ptr_window_sequence = pstr_sfb_prms->window_sequence;
376
245k
  WORD32 *ptr_num_sfb = pstr_sfb_prms->num_sfb;
377
245k
  WORD32 *ptr_num_window_groups = pstr_sfb_prms->num_window_groups;
378
245k
  ia_psy_mod_out_data_struct *pstr_psy_out = ptr_usac_data->str_psy_mod.str_psy_out_data;
379
380
700k
  for (ch = chn; ch < chn + num_chans; ch++) {
381
454k
    if (ptr_window_sequence[ch] == EIGHT_SHORT_SEQUENCE) {
382
155k
      iusace_sort_for_grouping(
383
155k
          pstr_sfb_prms->grouped_sfb_offset[ch], pstr_sfb_prms->sfb_width_table[ch],
384
155k
          ptr_usac_data->str_scratch.p_sort_grouping_scratch,
385
155k
          ptr_usac_data->spectral_line_vector[ch], ptr_num_window_groups[ch],
386
155k
          pstr_sfb_prms->window_group_length[ch], ptr_num_sfb[ch], ptr_usac_config->ccfl);
387
298k
    } else if ((ptr_window_sequence[ch] == ONLY_LONG_SEQUENCE) ||
388
102k
               (ptr_window_sequence[ch] == LONG_START_SEQUENCE) ||
389
48.3k
               (ptr_window_sequence[ch] == LONG_STOP_SEQUENCE) ||
390
298k
               (ptr_window_sequence[ch] == STOP_START_SEQUENCE)) {
391
298k
      pstr_sfb_prms->grouped_sfb_offset[ch][0] = 0;
392
298k
      k = 0;
393
13.2M
      for (i = 0; i < ptr_num_sfb[ch]; i++) {
394
12.9M
        pstr_sfb_prms->grouped_sfb_offset[ch][i] = k;
395
12.9M
        k += pstr_sfb_prms->sfb_width_table[ch][i];
396
12.9M
      }
397
298k
      pstr_sfb_prms->grouped_sfb_offset[ch][i] = k;
398
298k
    } else {
399
0
      return IA_EXHEAACE_EXE_FATAL_USAC_INVALID_WINDOW_TYPE;
400
0
    }
401
454k
  }
402
403
700k
  for (ch = chn; ch < chn + num_chans; ch++) {
404
454k
    if (pstr_psy_data[ch].window_sequence == 2) {
405
155k
      i = 0;
406
753k
      for (grp = 0; grp < ptr_num_window_groups[ch]; grp++) {
407
8.74M
        for (sfb = 0; sfb < ptr_num_sfb[ch]; sfb++) {
408
8.14M
          pstr_psy_out[ch].sfb_min_snr[i++] =
409
8.14M
              pstr_psy_config->str_psy_short_config[ele_id].sfb_min_snr[sfb];
410
8.14M
        }
411
597k
      }
412
155k
      wnd = 0;
413
155k
      i = 0;
414
753k
      for (grp = 0; grp < ptr_num_window_groups[ch]; grp++) {
415
8.74M
        for (sfb = 0; sfb < ptr_num_sfb[ch]; sfb++) {
416
8.14M
          FLOAT32 threshold = pstr_psy_data[ch].sfb_thr_short[wnd][sfb];
417
8.14M
          FLOAT32 energy = pstr_psy_data[ch].sfb_energy_short[wnd][sfb];
418
8.14M
          FLOAT32 energy_ms = pstr_psy_data[ch].ptr_sfb_energy_short_ms[wnd][sfb];
419
8.14M
          FLOAT32 spread_energy = pstr_psy_data[ch].sfb_spreaded_energy_short[wnd][sfb];
420
16.9M
          for (j = 1; j < pstr_sfb_prms->window_group_length[ch][grp]; j++) {
421
8.82M
            threshold = threshold + pstr_psy_data[ch].sfb_thr_short[wnd + j][sfb];
422
8.82M
            energy = energy + pstr_psy_data[ch].sfb_energy_short[wnd + j][sfb];
423
8.82M
            spread_energy =
424
8.82M
                spread_energy + pstr_psy_data[ch].sfb_spreaded_energy_short[wnd + j][sfb];
425
8.82M
            energy_ms = energy_ms + pstr_psy_data[ch].ptr_sfb_energy_short_ms[wnd + j][sfb];
426
8.82M
          }
427
8.14M
          pstr_psy_data[ch].ptr_sfb_thr_long[i] = threshold;
428
8.14M
          pstr_psy_data[ch].ptr_sfb_energy_long[i] = energy;
429
8.14M
          pstr_psy_data[ch].ptr_sfb_energy_long_ms[i] = energy_ms;
430
8.14M
          pstr_psy_data[ch].ptr_sfb_spreaded_energy_long[i++] = spread_energy;
431
8.14M
        }
432
597k
        wnd += pstr_sfb_prms->window_group_length[ch][grp];
433
597k
      }
434
298k
    } else {
435
13.2M
      for (sfb = 0; sfb < ptr_num_sfb[ch]; sfb++) {
436
12.9M
        pstr_psy_out[ch].sfb_min_snr[sfb] =
437
12.9M
            pstr_psy_config->str_psy_long_config[ele_id].sfb_min_snr[sfb];
438
12.9M
      }
439
298k
    }
440
454k
  }
441
245k
  return IA_NO_ERROR;
442
245k
}
443
444
IA_ERRORCODE iusace_quantize_spec(ia_sfb_params_struct *pstr_sfb_prms,
445
                                  WORD32 usac_independancy_flag, WORD32 num_chans,
446
                                  ia_usac_data_struct *ptr_usac_data,
447
                                  ia_usac_encoder_config_struct *ptr_usac_config, WORD32 chn,
448
                                  WORD32 ele_id, WORD32 *is_quant_spec_zero,
449
245k
                                  WORD32 *is_gain_limited) {
450
245k
  IA_ERRORCODE err_code;
451
245k
  WORD32 i = 0, sfb;
452
245k
  WORD32 j = 0;
453
245k
  WORD32 k;
454
245k
  WORD32 max_bits;
455
245k
  WORD32 ch;
456
245k
  iusace_scratch_mem *pstr_scratch = &ptr_usac_data->str_scratch;
457
245k
  WORD32 num_scfs[2];
458
245k
  FLOAT32 **sfb_form_fac = &pstr_scratch->ptr_sfb_form_fac[0];
459
245k
  WORD32 max_ch_dyn_bits[2] = {0};
460
245k
  FLOAT32 ch_bit_dist[2];
461
245k
  WORD32 constraints_fulfilled;
462
245k
  WORD32 iterations = 0;
463
245k
  WORD32 max_val;
464
245k
  WORD32 kk, idx = 0;
465
466
245k
  FLOAT32 *ptr_exp_spec = pstr_scratch->p_exp_spec;
467
245k
  FLOAT32 *ptr_mdct_spec_float = pstr_scratch->p_mdct_spec_float;
468
245k
  ia_psy_mod_data_struct *pstr_psy_data = ptr_usac_data->str_psy_mod.str_psy_data;
469
245k
  ia_qc_out_data_struct *pstr_qc_out = &ptr_usac_data->str_qc_main.str_qc_out;
470
245k
  ia_psy_mod_out_data_struct *pstr_psy_out = ptr_usac_data->str_psy_mod.str_psy_out_data;
471
245k
  ia_qc_data_struct *pstr_qc_data = &ptr_usac_data->str_qc_main.str_qc_data[ele_id];
472
245k
  ia_adj_thr_elem_struct *pstr_adj_thr_elem = &pstr_qc_data->str_adj_thr_ele;
473
245k
  WORD32 *ptr_window_sequence = pstr_sfb_prms->window_sequence;
474
245k
  WORD32 *ptr_max_sfb = pstr_sfb_prms->max_sfb;
475
245k
  WORD32 *ptr_num_sfb = pstr_sfb_prms->num_sfb;
476
245k
  WORD32 *ptr_num_window_groups = pstr_sfb_prms->num_window_groups;
477
245k
  WORD32 bitres_bits, bitres_diff;
478
245k
  WORD32 gain;
479
480
245k
  memset(num_scfs, 0, 2 * sizeof(num_scfs[0]));
481
482
700k
  for (ch = chn; ch < chn + num_chans; ch++) {
483
454k
    num_scfs[idx] = ptr_num_sfb[ch] * ptr_num_window_groups[ch];
484
485
454k
    pstr_psy_out[ch].sfb_count = num_scfs[idx];
486
454k
    pstr_psy_out[ch].sfb_per_group = num_scfs[idx] / ptr_num_window_groups[ch];
487
454k
    pstr_psy_out[ch].window_sequence = pstr_psy_data[ch].window_sequence;
488
454k
    pstr_psy_out[ch].window_shape = iusace_window_shape[pstr_psy_data[ch].window_sequence];
489
454k
    pstr_psy_out[ch].ptr_spec_coeffs = ptr_usac_data->spectral_line_vector[ch];
490
454k
    pstr_psy_out[ch].ptr_sfb_energy = pstr_psy_data[ch].ptr_sfb_energy_long;
491
454k
    pstr_psy_out[ch].ptr_sfb_thr = pstr_psy_data[ch].ptr_sfb_thr_long;
492
454k
    pstr_psy_out[ch].ptr_sfb_spread_energy = pstr_psy_data[ch].ptr_sfb_spreaded_energy_long;
493
494
21.5M
    for (j = 0; j < num_scfs[idx]; j++) {
495
21.1M
      pstr_psy_out[ch].sfb_offsets[j] = pstr_sfb_prms->grouped_sfb_offset[ch][j];
496
21.1M
    }
497
454k
    pstr_psy_out[ch].sfb_offsets[num_scfs[idx]] =
498
454k
        pstr_sfb_prms->grouped_sfb_offset[ch][num_scfs[idx]];
499
500
27.7M
    for (j = 0; j < MAX_NUM_GROUPED_SFB; j++) {
501
27.2M
      sfb_form_fac[idx][j] = MIN_FLT_VAL;
502
27.2M
    }
503
504
454k
    iusace_calc_form_fac_per_chan(&pstr_psy_out[ch], pstr_scratch, idx);
505
454k
    idx++;
506
454k
  }
507
508
245k
  pstr_qc_out->static_bits =
509
245k
      iusace_count_static_bits(ptr_usac_data, ptr_usac_config, pstr_sfb_prms, pstr_psy_out,
510
245k
                               num_chans, chn, usac_independancy_flag, ele_id);
511
512
245k
  iusace_adj_bitrate(pstr_qc_data, pstr_qc_data->ch_bitrate, ptr_usac_config->core_sample_rate,
513
245k
                     ptr_usac_config->ccfl);
514
245k
  err_code =
515
245k
      iusace_adj_thr(pstr_adj_thr_elem, pstr_psy_out, ch_bit_dist, pstr_qc_out,
516
245k
                     pstr_qc_data->avg_bits - pstr_qc_out->static_bits, pstr_qc_data->bit_res_lvl,
517
245k
                     pstr_qc_data->max_bitres_bits, pstr_qc_out->static_bits,
518
245k
                     &pstr_qc_data->max_bit_fac, num_chans, chn, pstr_scratch);
519
245k
  if (err_code != IA_NO_ERROR) {
520
0
    return err_code;
521
0
  }
522
523
245k
  iusace_estimate_scfs_chan(pstr_psy_out, pstr_qc_out->str_qc_out_chan, num_chans, chn,
524
245k
                            pstr_scratch);
525
245k
  idx = 0;
526
700k
  for (ch = 0; ch < num_chans; ch++) {
527
454k
    max_ch_dyn_bits[ch] = (WORD32)floor(
528
454k
        ch_bit_dist[ch] * (FLOAT32)(pstr_qc_data->avg_bits + pstr_qc_data->bit_res_lvl - 7 -
529
454k
                                    pstr_qc_out->static_bits));
530
454k
    idx++;
531
454k
  }
532
533
245k
  pstr_qc_out->dyn_bits = 0;
534
245k
  idx = 0;
535
700k
  for (ch = chn; ch < chn + num_chans; ch++) {
536
454k
    iterations = 0;
537
454k
    gain = 0;
538
423M
    for (kk = 0; kk < ptr_usac_config->ccfl; kk++) {
539
422M
      ptr_exp_spec[kk] = (FLOAT32)pstr_psy_out[ch].ptr_spec_coeffs[kk];
540
422M
      ptr_mdct_spec_float[kk] = (FLOAT32)pstr_psy_out[ch].ptr_spec_coeffs[kk];
541
422M
    }
542
493k
    do {
543
493k
      constraints_fulfilled = 1;
544
493k
      WORD32 quant_spec_is_zero = 1;
545
493k
      if (iterations > 0) {
546
161k
        for (WORD32 sfb_offs = 0; sfb_offs < pstr_psy_out[ch].sfb_count;
547
122k
             sfb_offs += pstr_psy_out[ch].sfb_per_group) {
548
1.25M
          for (sfb = 0; sfb < pstr_psy_out[ch].max_sfb_per_grp; sfb++) {
549
1.13M
            WORD32 scalefactor = pstr_qc_out->str_qc_out_chan[idx].scalefactor[sfb + sfb_offs];
550
1.13M
            gain = MAX(gain, pstr_qc_out->str_qc_out_chan[idx].global_gain - scalefactor);
551
1.13M
            iusace_quantize_lines(
552
1.13M
                pstr_qc_out->str_qc_out_chan[idx].global_gain - scalefactor,
553
1.13M
                pstr_psy_out[ch].sfb_offsets[sfb_offs + sfb + 1] -
554
1.13M
                    pstr_psy_out[ch].sfb_offsets[sfb_offs + sfb],
555
1.13M
                ptr_exp_spec + pstr_psy_out[ch].sfb_offsets[sfb_offs + sfb],
556
1.13M
                pstr_qc_out->str_qc_out_chan[idx].quant_spec +
557
1.13M
                    pstr_psy_out[ch].sfb_offsets[sfb_offs + sfb],
558
1.13M
                ptr_mdct_spec_float + pstr_psy_out[ch].sfb_offsets[sfb_offs + sfb]);
559
1.13M
          }
560
122k
        }
561
39.3k
      }
562
493k
      max_val =
563
493k
          iusace_calc_max_val_in_sfb(pstr_psy_out[ch].sfb_count, pstr_psy_out[ch].max_sfb_per_grp,
564
493k
                                     pstr_psy_out[ch].sfb_per_group, pstr_psy_out[ch].sfb_offsets,
565
493k
                                     pstr_qc_out->str_qc_out_chan[idx].quant_spec);
566
493k
      if (max_val > MAX_QUANT) {
567
0
        constraints_fulfilled = 0;
568
0
      }
569
570
23.3M
      for (k = 0; k < num_scfs[idx]; k++) {
571
22.8M
        for (i = pstr_sfb_prms->grouped_sfb_offset[ch][k];
572
475M
             i < pstr_sfb_prms->grouped_sfb_offset[ch][k + 1]; i++) {
573
452M
          ptr_usac_data->str_quant_info[idx].quant_degroup[i] =
574
452M
              (WORD32)pstr_qc_out->str_qc_out_chan[idx].quant_spec[i];
575
452M
          if (ptr_usac_data->str_quant_info[idx].quant_degroup[i] != 0) {
576
115M
            quant_spec_is_zero = 0;
577
115M
          }
578
452M
        }
579
22.8M
      }
580
581
493k
      if (ptr_window_sequence[ch] == EIGHT_SHORT_SEQUENCE) {
582
184k
        iusace_degroup_int(pstr_sfb_prms->grouped_sfb_offset[ch], ptr_num_sfb[ch],
583
184k
                           ptr_usac_data->str_scratch.p_degroup_scratch,
584
184k
                           ptr_usac_data->str_quant_info[idx].quant_degroup,
585
184k
                           ptr_num_window_groups[ch], pstr_sfb_prms->window_group_length[ch],
586
184k
                           ptr_usac_config->ccfl);
587
184k
      }
588
589
493k
      ptr_usac_data->str_quant_info[idx].max_spec_coeffs = 0;
590
14.4M
      for (k = 0; k < ptr_max_sfb[ch]; k++) {
591
13.9M
        ptr_usac_data->str_quant_info[idx].max_spec_coeffs +=
592
13.9M
            pstr_sfb_prms->sfb_width_table[ch][k];
593
13.9M
      }
594
595
23.3M
      for (i = 0; i < num_scfs[idx]; i++) {
596
22.8M
        ptr_usac_data->str_quant_info[idx].scale_factor[i] =
597
22.8M
            pstr_qc_out->str_qc_out_chan[idx].global_gain -
598
22.8M
            pstr_qc_out->str_qc_out_chan[idx].scalefactor[i] + SF_OFFSET;
599
22.8M
      }
600
601
493k
      max_bits = iusace_count_fd_bits(pstr_sfb_prms, ptr_usac_data, usac_independancy_flag,
602
493k
                                      ptr_usac_config, ch, idx);
603
604
493k
      if (max_bits > max_ch_dyn_bits[idx]) {
605
41.4k
        constraints_fulfilled = 0;
606
41.4k
      }
607
493k
      if (quant_spec_is_zero == 1) {
608
141k
        constraints_fulfilled = 1;
609
        /*Bit consuption is exceding bit reservoir, there is no scope left for bit consumption
610
          reduction, as spectrum is zero. Hence breaking the quantization loop. */
611
141k
        if (iterations > 0) {
612
120
          *is_quant_spec_zero = 1;
613
120
          max_bits = max_ch_dyn_bits[idx];
614
120
        }
615
141k
      }
616
493k
      if ((gain == MAX_GAIN_INDEX) && (constraints_fulfilled == 0)) {
617
        /* Bit consuption is exceding bit reservoir, there is no scope left for bit consumption
618
           reduction, as gain has reached the maximum value. Hence breaking the quantization
619
           loop. */
620
1
        constraints_fulfilled = 1;
621
1
        *is_gain_limited = 1;
622
1
        max_bits = max_ch_dyn_bits[idx];
623
1
      }
624
493k
      if (!constraints_fulfilled) {
625
39.3k
        pstr_qc_out->str_qc_out_chan[idx].global_gain++;
626
39.3k
      }
627
493k
      iterations++;
628
493k
    } while (!constraints_fulfilled);
629
630
454k
    pstr_qc_out->dyn_bits += max_bits;
631
632
454k
    if (ptr_usac_data->noise_filling[ele_id]) {
633
176k
      WORD32 max_nf_sfb = ptr_max_sfb[ch];
634
635
176k
      if (ptr_window_sequence[ch] != EIGHT_SHORT_SEQUENCE) {
636
122k
        iusace_noise_filling(
637
122k
            &ptr_usac_data->noise_level[idx], &ptr_usac_data->noise_offset[idx],
638
122k
            ptr_usac_data->spectral_line_vector[ch], &ptr_usac_data->str_quant_info[idx],
639
122k
            pstr_sfb_prms->grouped_sfb_offset[ch], max_nf_sfb, ptr_usac_config->ccfl,
640
122k
            ptr_num_window_groups[ch], pstr_sfb_prms->window_group_length[ch], 160,
641
122k
            ptr_usac_data->str_scratch.p_noise_filling_highest_tone);
642
122k
      } else {
643
54.8k
        iusace_noise_filling(
644
54.8k
            &ptr_usac_data->noise_level[idx], &ptr_usac_data->noise_offset[idx],
645
54.8k
            ptr_usac_data->spectral_line_vector[ch], &ptr_usac_data->str_quant_info[idx],
646
54.8k
            pstr_sfb_prms->grouped_sfb_offset[ch], max_nf_sfb, ptr_usac_config->ccfl >> 3,
647
54.8k
            ptr_num_window_groups[ch], pstr_sfb_prms->window_group_length[ch], 20,
648
54.8k
            (FLOAT64 *)ptr_usac_data->str_scratch.p_noise_filling_highest_tone);
649
54.8k
      }
650
651
176k
      if (ptr_usac_data->noise_level[idx] == 0 && ptr_usac_data->noise_offset[idx] != 0 &&
652
0
          pstr_sfb_prms->common_win[ch]) {
653
0
        ptr_usac_data->complex_coef[ch] = 0;
654
0
      }
655
176k
    }
656
454k
    idx++;
657
454k
  }
658
659
245k
  pstr_adj_thr_elem->dyn_bits_last = pstr_qc_out->dyn_bits;
660
661
245k
  bitres_bits = pstr_qc_data->max_bitres_bits - pstr_qc_data->bit_res_lvl;
662
245k
  bitres_diff = pstr_qc_data->avg_bits - (pstr_qc_out->static_bits + pstr_qc_out->dyn_bits);
663
245k
  pstr_qc_out->fill_bits = MAX(0, (bitres_diff - bitres_bits));
664
665
245k
  if (pstr_qc_data->avg_bits > 0) {
666
245k
    pstr_qc_data->bit_res_lvl +=
667
245k
        pstr_qc_data->avg_bits -
668
245k
        (pstr_qc_out->static_bits + pstr_qc_out->dyn_bits + pstr_qc_out->fill_bits);
669
245k
  } else {
670
0
    pstr_qc_data->bit_res_lvl = pstr_qc_data->max_bits;
671
0
  }
672
673
245k
  if (pstr_qc_data->bit_res_lvl < 0 ||
674
243k
      pstr_qc_data->bit_res_lvl > pstr_qc_data->max_bitres_bits) {
675
1.77k
    return IA_EXHEAACE_EXE_FATAL_USAC_INVALID_BIT_RSVR_LVL;
676
1.77k
  }
677
678
243k
  return IA_NO_ERROR;
679
245k
}