Coverage Report

Created: 2026-03-31 06:52

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.1k
static WORD32 iusace_count_ms_bits(WORD32 sfb_count, WORD32 sfb_per_grp, WORD32 max_sfb_per_grp) {
64
22.1k
  WORD32 ms_bits = 0, sfb_offs, sfb;
65
76.4k
  for (sfb_offs = 0; sfb_offs < sfb_count; sfb_offs += sfb_per_grp) {
66
1.11M
    for (sfb = 0; sfb < max_sfb_per_grp; sfb++) {
67
1.05M
      ms_bits++;
68
1.05M
    }
69
54.3k
  }
70
22.1k
  return (ms_bits);
71
22.1k
}
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
235k
                                       WORD32 chn, WORD32 usac_independency_flag, WORD32 ele_id) {
78
235k
  WORD32 ms_mask = ptr_usac_data->str_ms_info[chn].ms_mask;
79
235k
  WORD32 noise_filling = ptr_usac_data->noise_filling[ele_id];
80
235k
  WORD32 stat_bits = 0, i;
81
235k
  WORD32 tns_active = 0, tns_present_both = 0;
82
235k
  WORD32 max_sfb = pstr_psy_out[chn].max_sfb_per_grp;
83
235k
  (VOID) ptr_usac_config;
84
85
235k
  if (channels == 1) {
86
49.8k
    stat_bits += 1;  // core mode
87
49.8k
    stat_bits += 1;  // tns active
88
89
49.8k
    switch (pstr_psy_out[chn].window_sequence) {
90
27.2k
      case ONLY_LONG_SEQUENCE:
91
32.8k
      case LONG_START_SEQUENCE:
92
37.2k
      case LONG_STOP_SEQUENCE:
93
37.2k
        stat_bits += SI_ICS_INFO_BITS_LONG;
94
37.2k
        break;
95
12.5k
      case EIGHT_SHORT_SEQUENCE:
96
12.5k
        stat_bits += SI_ICS_INFO_BITS_SHORT;
97
12.5k
        break;
98
49.8k
    }
99
185k
  } else {
100
185k
    stat_bits += 2;  // core mode
101
185k
    stat_bits += 2;  // tns and common window
102
103
185k
    switch (pstr_psy_out[chn].window_sequence) {
104
80.4k
      case ONLY_LONG_SEQUENCE:
105
101k
      case LONG_START_SEQUENCE:
106
119k
      case LONG_STOP_SEQUENCE:
107
119k
        stat_bits += SI_ICS_INFO_BITS_LONG;
108
119k
        break;
109
66.0k
      case EIGHT_SHORT_SEQUENCE:
110
66.0k
        stat_bits += SI_ICS_INFO_BITS_SHORT;
111
66.0k
        break;
112
185k
    }
113
114
185k
    stat_bits += 1;  // common_max_sfb
115
185k
    stat_bits += SI_CPE_MS_MASK_BITS;
116
117
185k
    if (ms_mask != 3) {
118
168k
      if (ms_mask == 1) {
119
22.1k
        stat_bits += iusace_count_ms_bits(pstr_psy_out[chn].sfb_count,
120
22.1k
                                          pstr_psy_out[chn].sfb_per_group, max_sfb);
121
22.1k
      }
122
168k
    } else {
123
17.0k
      stat_bits += iusace_write_cplx_pred_data(
124
17.0k
          NULL, pstr_sfb_params->num_window_groups[chn], max_sfb,
125
17.0k
          ptr_usac_data->complex_coef[chn], ptr_usac_data->pred_coef_re[chn],
126
17.0k
          ptr_usac_data->pred_coef_im[chn], iusace_huffman_code_table, usac_independency_flag,
127
17.0k
          ptr_usac_data->pred_dir_idx[chn], ptr_usac_data->cplx_pred_used[chn],
128
17.0k
          ptr_usac_data->cplx_pred_all[chn], ptr_usac_data->temp_pred_coef_re_prev[chn],
129
17.0k
          ptr_usac_data->temp_pred_coef_im_prev[chn], &ptr_usac_data->delta_code_time[chn]);
130
17.0k
    }
131
132
185k
    if (ptr_usac_data->pstr_tns_info[chn] != NULL &&
133
83.6k
        ptr_usac_data->pstr_tns_info[chn + 1] != NULL) {
134
83.6k
      tns_active = ptr_usac_data->pstr_tns_info[chn]->tns_data_present ||
135
38.7k
                   ptr_usac_data->pstr_tns_info[chn + 1]->tns_data_present;
136
83.6k
      tns_present_both = ptr_usac_data->pstr_tns_info[chn]->tns_data_present &&
137
44.9k
                         ptr_usac_data->pstr_tns_info[chn + 1]->tns_data_present;
138
83.6k
    }
139
185k
    if (tns_active) {
140
48.5k
      stat_bits += 1;  // common_tns
141
142
48.5k
      stat_bits += 1;  // tns_present_both
143
144
48.5k
      if (!tns_present_both) {
145
11.1k
        stat_bits += 1;  // tns_data_present1
146
11.1k
      }
147
48.5k
    }
148
185k
  }
149
150
657k
  for (i = chn; i < chn + channels; i++) {
151
421k
    stat_bits += 8;  // global_gain
152
153
421k
    if (noise_filling) {
154
184k
      stat_bits += 8;
155
184k
    }
156
421k
  }
157
158
657k
  for (i = chn; i < chn + channels; i++) {
159
421k
    if (ptr_usac_data->pstr_tns_info[i] != NULL &&
160
175k
        ptr_usac_data->pstr_tns_info[i]->tns_data_present == 1) {
161
90.5k
      stat_bits += iusace_write_tns_data(NULL, ptr_usac_data->pstr_tns_info[i],
162
90.5k
                                         pstr_psy_out[i].window_sequence, 0);
163
90.5k
    }
164
165
421k
    if (!usac_independency_flag) {
166
402k
      stat_bits += 1;  // arith_reset_flag
167
402k
    }
168
169
421k
    stat_bits += 1;  // fac_data_present
170
421k
    stat_bits += ptr_usac_data->str_scratch.ptr_num_fac_bits[i];
171
421k
  }
172
173
235k
  stat_bits += (ptr_usac_data->num_sbr_bits + ptr_usac_data->num_drc_bits);
174
175
235k
  return stat_bits;
176
235k
}
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
144k
                                     WORD32 nr_of_sfb, WORD32 ccfl) {
182
144k
  WORD32 i, j, ii;
183
144k
  WORD32 index = 0;
184
144k
  WORD32 group_offset = 0;
185
144k
  WORD32 k = 0;
186
144k
  WORD32 frame_len_short = (ccfl * FRAME_LEN_SHORT_128) / FRAME_LEN_LONG;
187
188
144k
  sfb_offset[k] = 0;
189
2.11M
  for (k = 1; k < nr_of_sfb + 1; k++) {
190
1.96M
    sfb_offset[k] = sfb_offset[k - 1] + sfb_width_table[k - 1];
191
1.96M
  }
192
193
144k
  index = 0;
194
144k
  group_offset = 0;
195
701k
  for (i = 0; i < num_window_groups; i++) {
196
8.13M
    for (k = 0; k < nr_of_sfb; k++) {
197
23.3M
      for (j = 0; j < window_group_length[i]; j++) {
198
148M
        for (ii = 0; ii < sfb_width_table[k]; ii++) {
199
132M
          ptr_scratch[index++] =
200
132M
              ptr_spec[ii + sfb_offset[k] + frame_len_short * j + group_offset];
201
132M
        }
202
15.7M
      }
203
7.57M
    }
204
556k
    group_offset += frame_len_short * window_group_length[i];
205
556k
  }
206
207
144k
  memcpy(ptr_spec, ptr_scratch, ccfl * sizeof(ptr_spec[0]));
208
209
144k
  index = 0;
210
144k
  sfb_offset[index++] = 0;
211
701k
  for (i = 0; i < num_window_groups; i++) {
212
8.13M
    for (k = 0; k < nr_of_sfb; k++) {
213
7.57M
      sfb_offset[index] = sfb_offset[index - 1] + sfb_width_table[k] * window_group_length[i];
214
7.57M
      index++;
215
7.57M
    }
216
556k
  }
217
218
144k
  return;
219
144k
}
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
220k
{
226
220k
  WORD32 i, j, k, n;
227
220k
  WORD32 index, group_offset;
228
220k
  WORD32 loop1, loop2;
229
220k
  WORD32 frame_len_short = (ccfl * FRAME_LEN_SHORT_128) / FRAME_LEN_LONG;
230
220k
  index = 0;
231
220k
  group_offset = 0;
232
233
220k
  memset(ptr_scratch, 0, ccfl * sizeof(WORD32));
234
235
1.06M
  for (i = 0; i < num_window_groups; i++) {
236
11.9M
    for (j = 0; j < sfb_per_group; j++) {
237
11.0M
      WORD32 idx = i * sfb_per_group + j;
238
11.0M
      loop1 = ((ptr_grouped_sfb_offsets[idx + 1] - ptr_grouped_sfb_offsets[idx]) /
239
11.0M
               window_group_length[i]);
240
241
34.1M
      for (k = 0; k < window_group_length[i]; k++) {
242
23.0M
        loop2 = ((ptr_grouped_sfb_offsets[idx] - group_offset) / window_group_length[i]) +
243
23.0M
                frame_len_short * k + group_offset;
244
245
214M
        for (n = 0; n < loop1; n++) {
246
191M
          ptr_scratch[n + loop2] = ptr_spec[index++];
247
191M
        }
248
23.0M
      }
249
11.0M
    }
250
848k
    group_offset += frame_len_short * window_group_length[i];
251
848k
  }
252
253
220k
  memcpy(ptr_spec, ptr_scratch, ccfl * sizeof(WORD32));
254
255
220k
  return;
256
220k
}
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
185k
                                ia_usac_encoder_config_struct *ptr_usac_config, WORD32 chn) {
261
185k
  IA_ERRORCODE err_code;
262
185k
  WORD32 i = 0;
263
185k
  WORD32 j = 0;
264
185k
  iusace_scratch_mem *pstr_scratch = &ptr_usac_data->str_scratch;
265
185k
  ia_psy_mod_data_struct *pstr_psy_data = ptr_usac_data->str_psy_mod.str_psy_data;
266
185k
  ia_psy_mod_out_data_struct *pstr_psy_out = ptr_usac_data->str_psy_mod.str_psy_out_data;
267
185k
  WORD32 *ptr_num_sfb = pstr_sfb_prms->num_sfb;
268
185k
  WORD32 *ptr_num_window_groups = pstr_sfb_prms->num_window_groups;
269
185k
  WORD32 ccfl = ptr_usac_config->ccfl;
270
185k
  ia_ms_info_struct *pstr_ms_info = &ptr_usac_data->str_ms_info[chn];
271
272
185k
  FLOAT64 tmp = 0.0f;
273
185k
  FLOAT32 nrg_mid = 0.0f, nrg_side = 0.0f, nrg_left = 0.0f, nrg_right = 0.0f;
274
185k
  FLOAT64 *ptr_scratch_spec = pstr_scratch->p_quant_spectrum_spec_scratch;
275
185k
  FLOAT32 ratio_mid = 0.0f, ratio_side = 0.0f;
276
185k
  FLOAT32 eps = 1.0e-6f;
277
  /* Save a copy of left and right channel MDCT spectra before they are modified */
278
185k
  memcpy(ptr_usac_data->left_chan_save[chn], ptr_usac_data->spectral_line_vector[chn],
279
185k
         ccfl * sizeof(FLOAT64));
280
185k
  memcpy(ptr_usac_data->right_chan_save[chn], ptr_usac_data->spectral_line_vector[chn + 1],
281
185k
         ccfl * sizeof(FLOAT64));
282
283
185k
  if (ptr_usac_config->cmplx_pred_flag == 1) {
284
    /* Refinement - decision on whether to use complex prediction or MS */
285
106M
    for (i = 0; i < ccfl; i++) {
286
106M
      tmp = ptr_usac_data->spectral_line_vector[chn][i];
287
106M
      ptr_scratch_spec[i] = 0.5f * (ptr_usac_data->spectral_line_vector[chn][i] +
288
106M
                                    ptr_usac_data->spectral_line_vector[chn + 1][i]);
289
106M
      ptr_scratch_spec[ccfl + i] = 0.5f * (tmp - ptr_usac_data->spectral_line_vector[chn + 1][i]);
290
106M
    }
291
292
106M
    for (i = 0; i < ccfl; i++) {
293
106M
      nrg_mid += (FLOAT32)(ptr_scratch_spec[i] * ptr_scratch_spec[i]);
294
106M
      nrg_side += (FLOAT32)(ptr_scratch_spec[ccfl + i] * ptr_scratch_spec[ccfl + i]);
295
106M
      nrg_left += (FLOAT32)(ptr_usac_data->spectral_line_vector[chn][i] *
296
106M
                            ptr_usac_data->spectral_line_vector[chn][i]);
297
106M
      nrg_right += (FLOAT32)(ptr_usac_data->spectral_line_vector[chn + 1][i] *
298
106M
                             ptr_usac_data->spectral_line_vector[chn + 1][i]);
299
106M
    }
300
301
117k
    ratio_mid = nrg_mid / (MAX(nrg_left, nrg_right) + eps);
302
117k
    ratio_side = nrg_side / (MAX(nrg_left, nrg_right) + eps);
303
304
117k
    if (ratio_mid >= 0.8f || ratio_side >= 0.8f || nrg_mid == 0.f || nrg_side == 0.f) {
305
85.0k
      pstr_ms_info->ms_mask = 0;
306
85.0k
    } else {
307
32.1k
      pstr_ms_info->ms_mask = 3;
308
32.1k
    }
309
117k
  }
310
311
185k
  if (pstr_ms_info->ms_mask != 3) {
312
153k
    WORD32 idx = 0;
313
153k
    WORD32 sfb;
314
315
5.43M
    for (sfb = 0; sfb < ptr_num_sfb[chn]; sfb++) {
316
5.28M
      ptr_usac_data->pred_coef_re_prev[chn][sfb] = 0;
317
5.28M
      ptr_usac_data->pred_coef_im_prev[chn][sfb] = 0;
318
5.28M
      ptr_usac_data->temp_pred_coef_re_prev[chn][sfb] = 0;
319
5.28M
      ptr_usac_data->temp_pred_coef_im_prev[chn][sfb] = 0;
320
5.28M
    }
321
322
153k
    memset(ptr_usac_data->str_ms_info[chn].ms_used, 0,
323
153k
           MAX_SFB_LONG * MAX_SHORT_WINDOWS * sizeof(WORD32));
324
325
153k
    iusace_ms_apply(pstr_psy_data, ptr_usac_data->spectral_line_vector[chn],
326
153k
                    ptr_usac_data->spectral_line_vector[chn + 1],
327
153k
                    &ptr_usac_data->str_ms_info[chn].ms_mask,
328
153k
                    ptr_usac_data->str_ms_info[chn].ms_used,
329
153k
                    ptr_num_window_groups[chn] * ptr_num_sfb[chn], ptr_num_sfb[chn],
330
153k
                    pstr_psy_out[chn].max_sfb_per_grp, pstr_sfb_prms->grouped_sfb_offset[chn],
331
153k
                    chn, ptr_usac_config->cmplx_pred_flag == 1 ? ptr_scratch_spec : NULL);
332
333
444k
    for (i = 0; i < ptr_num_window_groups[chn]; i++) {
334
7.47M
      for (j = 0; j < ptr_num_sfb[chn]; j++) {
335
7.17M
        pstr_psy_out[chn].ms_used[idx++] = ptr_usac_data->str_ms_info[chn].ms_used[i][j];
336
7.17M
      }
337
291k
    }
338
153k
  } else {
339
    /* Reset buffer to zero */
340
289k
    for (WORD32 group = 0; group < MAX_SHORT_WINDOWS; group++) {
341
257k
      memset(ptr_usac_data->cplx_pred_used[chn][group], 0, MAX_SFB_LONG * sizeof(WORD32));
342
257k
    }
343
344
32.1k
    ptr_usac_data->cplx_pred_all[chn] = 1; /* Disable bandwise switching to L/R */
345
114k
    for (i = 0; i < ptr_num_window_groups[chn]; i++) {
346
827k
      for (j = 0; j < ptr_num_sfb[chn]; j += 2) {
347
745k
        ptr_usac_data->cplx_pred_used[chn][i][j] = 1;
348
745k
        if ((j + 1) < ptr_num_sfb[chn]) {
349
720k
          ptr_usac_data->cplx_pred_used[chn][i][j + 1] = ptr_usac_data->cplx_pred_used[chn][i][j];
350
720k
        }
351
745k
      }
352
82.5k
    }
353
354
32.1k
    err_code = iusace_cplx_pred_proc(
355
32.1k
        ptr_usac_data, ptr_usac_config, usac_independancy_flag, pstr_sfb_prms, chn, pstr_psy_data,
356
32.1k
        pstr_sfb_prms->grouped_sfb_offset[chn], pstr_scratch->p_cmpx_mdct_temp_buf,
357
32.1k
        ptr_scratch_spec, nrg_mid, nrg_side);
358
32.1k
    if (err_code != IA_NO_ERROR) {
359
0
      return err_code;
360
0
    }
361
32.1k
  }
362
185k
  return IA_NO_ERROR;
363
185k
}
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
235k
                             WORD32 ele_id) {
369
235k
  WORD32 i = 0, grp, sfb, wnd;
370
235k
  WORD32 j = 0;
371
235k
  WORD32 k;
372
235k
  WORD32 ch;
373
235k
  ia_psy_mod_struct *pstr_psy_config = &ptr_usac_data->str_psy_mod;
374
235k
  ia_psy_mod_data_struct *pstr_psy_data = ptr_usac_data->str_psy_mod.str_psy_data;
375
235k
  WORD32 *ptr_window_sequence = pstr_sfb_prms->window_sequence;
376
235k
  WORD32 *ptr_num_sfb = pstr_sfb_prms->num_sfb;
377
235k
  WORD32 *ptr_num_window_groups = pstr_sfb_prms->num_window_groups;
378
235k
  ia_psy_mod_out_data_struct *pstr_psy_out = ptr_usac_data->str_psy_mod.str_psy_out_data;
379
380
657k
  for (ch = chn; ch < chn + num_chans; ch++) {
381
421k
    if (ptr_window_sequence[ch] == EIGHT_SHORT_SEQUENCE) {
382
144k
      iusace_sort_for_grouping(
383
144k
          pstr_sfb_prms->grouped_sfb_offset[ch], pstr_sfb_prms->sfb_width_table[ch],
384
144k
          ptr_usac_data->str_scratch.p_sort_grouping_scratch,
385
144k
          ptr_usac_data->spectral_line_vector[ch], ptr_num_window_groups[ch],
386
144k
          pstr_sfb_prms->window_group_length[ch], ptr_num_sfb[ch], ptr_usac_config->ccfl);
387
276k
    } else if ((ptr_window_sequence[ch] == ONLY_LONG_SEQUENCE) ||
388
88.6k
               (ptr_window_sequence[ch] == LONG_START_SEQUENCE) ||
389
41.9k
               (ptr_window_sequence[ch] == LONG_STOP_SEQUENCE) ||
390
276k
               (ptr_window_sequence[ch] == STOP_START_SEQUENCE)) {
391
276k
      pstr_sfb_prms->grouped_sfb_offset[ch][0] = 0;
392
276k
      k = 0;
393
12.2M
      for (i = 0; i < ptr_num_sfb[ch]; i++) {
394
11.9M
        pstr_sfb_prms->grouped_sfb_offset[ch][i] = k;
395
11.9M
        k += pstr_sfb_prms->sfb_width_table[ch][i];
396
11.9M
      }
397
276k
      pstr_sfb_prms->grouped_sfb_offset[ch][i] = k;
398
276k
    } else {
399
0
      return IA_EXHEAACE_EXE_FATAL_USAC_INVALID_WINDOW_TYPE;
400
0
    }
401
421k
  }
402
403
657k
  for (ch = chn; ch < chn + num_chans; ch++) {
404
421k
    if (pstr_psy_data[ch].window_sequence == 2) {
405
144k
      i = 0;
406
701k
      for (grp = 0; grp < ptr_num_window_groups[ch]; grp++) {
407
8.13M
        for (sfb = 0; sfb < ptr_num_sfb[ch]; sfb++) {
408
7.57M
          pstr_psy_out[ch].sfb_min_snr[i++] =
409
7.57M
              pstr_psy_config->str_psy_short_config[ele_id].sfb_min_snr[sfb];
410
7.57M
        }
411
556k
      }
412
144k
      wnd = 0;
413
144k
      i = 0;
414
701k
      for (grp = 0; grp < ptr_num_window_groups[ch]; grp++) {
415
8.13M
        for (sfb = 0; sfb < ptr_num_sfb[ch]; sfb++) {
416
7.57M
          FLOAT32 threshold = pstr_psy_data[ch].sfb_thr_short[wnd][sfb];
417
7.57M
          FLOAT32 energy = pstr_psy_data[ch].sfb_energy_short[wnd][sfb];
418
7.57M
          FLOAT32 energy_ms = pstr_psy_data[ch].ptr_sfb_energy_short_ms[wnd][sfb];
419
7.57M
          FLOAT32 spread_energy = pstr_psy_data[ch].sfb_spreaded_energy_short[wnd][sfb];
420
15.7M
          for (j = 1; j < pstr_sfb_prms->window_group_length[ch][grp]; j++) {
421
8.17M
            threshold = threshold + pstr_psy_data[ch].sfb_thr_short[wnd + j][sfb];
422
8.17M
            energy = energy + pstr_psy_data[ch].sfb_energy_short[wnd + j][sfb];
423
8.17M
            spread_energy =
424
8.17M
                spread_energy + pstr_psy_data[ch].sfb_spreaded_energy_short[wnd + j][sfb];
425
8.17M
            energy_ms = energy_ms + pstr_psy_data[ch].ptr_sfb_energy_short_ms[wnd + j][sfb];
426
8.17M
          }
427
7.57M
          pstr_psy_data[ch].ptr_sfb_thr_long[i] = threshold;
428
7.57M
          pstr_psy_data[ch].ptr_sfb_energy_long[i] = energy;
429
7.57M
          pstr_psy_data[ch].ptr_sfb_energy_long_ms[i] = energy_ms;
430
7.57M
          pstr_psy_data[ch].ptr_sfb_spreaded_energy_long[i++] = spread_energy;
431
7.57M
        }
432
556k
        wnd += pstr_sfb_prms->window_group_length[ch][grp];
433
556k
      }
434
276k
    } else {
435
12.2M
      for (sfb = 0; sfb < ptr_num_sfb[ch]; sfb++) {
436
11.9M
        pstr_psy_out[ch].sfb_min_snr[sfb] =
437
11.9M
            pstr_psy_config->str_psy_long_config[ele_id].sfb_min_snr[sfb];
438
11.9M
      }
439
276k
    }
440
421k
  }
441
235k
  return IA_NO_ERROR;
442
235k
}
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
235k
                                  WORD32 *is_gain_limited) {
450
235k
  IA_ERRORCODE err_code;
451
235k
  WORD32 i = 0, sfb;
452
235k
  WORD32 j = 0;
453
235k
  WORD32 k;
454
235k
  WORD32 max_bits;
455
235k
  WORD32 ch;
456
235k
  iusace_scratch_mem *pstr_scratch = &ptr_usac_data->str_scratch;
457
235k
  WORD32 num_scfs[2];
458
235k
  FLOAT32 **sfb_form_fac = &pstr_scratch->ptr_sfb_form_fac[0];
459
235k
  WORD32 max_ch_dyn_bits[2] = {0};
460
235k
  FLOAT32 ch_bit_dist[2];
461
235k
  WORD32 constraints_fulfilled;
462
235k
  WORD32 iterations = 0;
463
235k
  WORD32 max_val;
464
235k
  WORD32 kk, idx = 0;
465
466
235k
  FLOAT32 *ptr_exp_spec = pstr_scratch->p_exp_spec;
467
235k
  FLOAT32 *ptr_mdct_spec_float = pstr_scratch->p_mdct_spec_float;
468
235k
  ia_psy_mod_data_struct *pstr_psy_data = ptr_usac_data->str_psy_mod.str_psy_data;
469
235k
  ia_qc_out_data_struct *pstr_qc_out = &ptr_usac_data->str_qc_main.str_qc_out;
470
235k
  ia_psy_mod_out_data_struct *pstr_psy_out = ptr_usac_data->str_psy_mod.str_psy_out_data;
471
235k
  ia_qc_data_struct *pstr_qc_data = &ptr_usac_data->str_qc_main.str_qc_data[ele_id];
472
235k
  ia_adj_thr_elem_struct *pstr_adj_thr_elem = &pstr_qc_data->str_adj_thr_ele;
473
235k
  WORD32 *ptr_window_sequence = pstr_sfb_prms->window_sequence;
474
235k
  WORD32 *ptr_max_sfb = pstr_sfb_prms->max_sfb;
475
235k
  WORD32 *ptr_num_sfb = pstr_sfb_prms->num_sfb;
476
235k
  WORD32 *ptr_num_window_groups = pstr_sfb_prms->num_window_groups;
477
235k
  WORD32 bitres_bits, bitres_diff;
478
235k
  WORD32 gain;
479
480
235k
  memset(num_scfs, 0, 2 * sizeof(num_scfs[0]));
481
482
657k
  for (ch = chn; ch < chn + num_chans; ch++) {
483
421k
    num_scfs[idx] = ptr_num_sfb[ch] * ptr_num_window_groups[ch];
484
485
421k
    pstr_psy_out[ch].sfb_count = num_scfs[idx];
486
421k
    pstr_psy_out[ch].sfb_per_group = num_scfs[idx] / ptr_num_window_groups[ch];
487
421k
    pstr_psy_out[ch].window_sequence = pstr_psy_data[ch].window_sequence;
488
421k
    pstr_psy_out[ch].window_shape = iusace_window_shape[pstr_psy_data[ch].window_sequence];
489
421k
    pstr_psy_out[ch].ptr_spec_coeffs = ptr_usac_data->spectral_line_vector[ch];
490
421k
    pstr_psy_out[ch].ptr_sfb_energy = pstr_psy_data[ch].ptr_sfb_energy_long;
491
421k
    pstr_psy_out[ch].ptr_sfb_thr = pstr_psy_data[ch].ptr_sfb_thr_long;
492
421k
    pstr_psy_out[ch].ptr_sfb_spread_energy = pstr_psy_data[ch].ptr_sfb_spreaded_energy_long;
493
494
19.9M
    for (j = 0; j < num_scfs[idx]; j++) {
495
19.5M
      pstr_psy_out[ch].sfb_offsets[j] = pstr_sfb_prms->grouped_sfb_offset[ch][j];
496
19.5M
    }
497
421k
    pstr_psy_out[ch].sfb_offsets[num_scfs[idx]] =
498
421k
        pstr_sfb_prms->grouped_sfb_offset[ch][num_scfs[idx]];
499
500
25.7M
    for (j = 0; j < MAX_NUM_GROUPED_SFB; j++) {
501
25.2M
      sfb_form_fac[idx][j] = MIN_FLT_VAL;
502
25.2M
    }
503
504
421k
    iusace_calc_form_fac_per_chan(&pstr_psy_out[ch], pstr_scratch, idx);
505
421k
    idx++;
506
421k
  }
507
508
235k
  pstr_qc_out->static_bits =
509
235k
      iusace_count_static_bits(ptr_usac_data, ptr_usac_config, pstr_sfb_prms, pstr_psy_out,
510
235k
                               num_chans, chn, usac_independancy_flag, ele_id);
511
512
235k
  iusace_adj_bitrate(pstr_qc_data, pstr_qc_data->ch_bitrate, ptr_usac_config->core_sample_rate,
513
235k
                     ptr_usac_config->ccfl);
514
235k
  err_code =
515
235k
      iusace_adj_thr(pstr_adj_thr_elem, pstr_psy_out, ch_bit_dist, pstr_qc_out,
516
235k
                     pstr_qc_data->avg_bits - pstr_qc_out->static_bits, pstr_qc_data->bit_res_lvl,
517
235k
                     pstr_qc_data->max_bitres_bits, pstr_qc_out->static_bits,
518
235k
                     &pstr_qc_data->max_bit_fac, num_chans, chn, pstr_scratch);
519
235k
  if (err_code != IA_NO_ERROR) {
520
0
    return err_code;
521
0
  }
522
523
235k
  iusace_estimate_scfs_chan(pstr_psy_out, pstr_qc_out->str_qc_out_chan, num_chans, chn,
524
235k
                            pstr_scratch);
525
235k
  idx = 0;
526
657k
  for (ch = 0; ch < num_chans; ch++) {
527
421k
    max_ch_dyn_bits[ch] = (WORD32)floor(
528
421k
        ch_bit_dist[ch] * (FLOAT32)(pstr_qc_data->avg_bits + pstr_qc_data->bit_res_lvl - 7 -
529
421k
                                    pstr_qc_out->static_bits));
530
421k
    idx++;
531
421k
  }
532
533
235k
  pstr_qc_out->dyn_bits = 0;
534
235k
  idx = 0;
535
657k
  for (ch = chn; ch < chn + num_chans; ch++) {
536
421k
    iterations = 0;
537
421k
    gain = 0;
538
397M
    for (kk = 0; kk < ptr_usac_config->ccfl; kk++) {
539
397M
      ptr_exp_spec[kk] = (FLOAT32)pstr_psy_out[ch].ptr_spec_coeffs[kk];
540
397M
      ptr_mdct_spec_float[kk] = (FLOAT32)pstr_psy_out[ch].ptr_spec_coeffs[kk];
541
397M
    }
542
523k
    do {
543
523k
      constraints_fulfilled = 1;
544
523k
      WORD32 quant_spec_is_zero = 1;
545
523k
      if (iterations > 0) {
546
420k
        for (WORD32 sfb_offs = 0; sfb_offs < pstr_psy_out[ch].sfb_count;
547
318k
             sfb_offs += pstr_psy_out[ch].sfb_per_group) {
548
3.30M
          for (sfb = 0; sfb < pstr_psy_out[ch].max_sfb_per_grp; sfb++) {
549
2.98M
            WORD32 scalefactor = pstr_qc_out->str_qc_out_chan[idx].scalefactor[sfb + sfb_offs];
550
2.98M
            gain = MAX(gain, pstr_qc_out->str_qc_out_chan[idx].global_gain - scalefactor);
551
2.98M
            iusace_quantize_lines(
552
2.98M
                pstr_qc_out->str_qc_out_chan[idx].global_gain - scalefactor,
553
2.98M
                pstr_psy_out[ch].sfb_offsets[sfb_offs + sfb + 1] -
554
2.98M
                    pstr_psy_out[ch].sfb_offsets[sfb_offs + sfb],
555
2.98M
                ptr_exp_spec + pstr_psy_out[ch].sfb_offsets[sfb_offs + sfb],
556
2.98M
                pstr_qc_out->str_qc_out_chan[idx].quant_spec +
557
2.98M
                    pstr_psy_out[ch].sfb_offsets[sfb_offs + sfb],
558
2.98M
                ptr_mdct_spec_float + pstr_psy_out[ch].sfb_offsets[sfb_offs + sfb]);
559
2.98M
          }
560
318k
        }
561
101k
      }
562
523k
      max_val =
563
523k
          iusace_calc_max_val_in_sfb(pstr_psy_out[ch].sfb_count, pstr_psy_out[ch].max_sfb_per_grp,
564
523k
                                     pstr_psy_out[ch].sfb_per_group, pstr_psy_out[ch].sfb_offsets,
565
523k
                                     pstr_qc_out->str_qc_out_chan[idx].quant_spec);
566
523k
      if (max_val > MAX_QUANT) {
567
0
        constraints_fulfilled = 0;
568
0
      }
569
570
24.5M
      for (k = 0; k < num_scfs[idx]; k++) {
571
24.0M
        for (i = pstr_sfb_prms->grouped_sfb_offset[ch][k];
572
499M
             i < pstr_sfb_prms->grouped_sfb_offset[ch][k + 1]; i++) {
573
475M
          ptr_usac_data->str_quant_info[idx].quant_degroup[i] =
574
475M
              (WORD32)pstr_qc_out->str_qc_out_chan[idx].quant_spec[i];
575
475M
          if (ptr_usac_data->str_quant_info[idx].quant_degroup[i] != 0) {
576
107M
            quant_spec_is_zero = 0;
577
107M
          }
578
475M
        }
579
24.0M
      }
580
581
523k
      if (ptr_window_sequence[ch] == EIGHT_SHORT_SEQUENCE) {
582
220k
        iusace_degroup_int(pstr_sfb_prms->grouped_sfb_offset[ch], ptr_num_sfb[ch],
583
220k
                           ptr_usac_data->str_scratch.p_degroup_scratch,
584
220k
                           ptr_usac_data->str_quant_info[idx].quant_degroup,
585
220k
                           ptr_num_window_groups[ch], pstr_sfb_prms->window_group_length[ch],
586
220k
                           ptr_usac_config->ccfl);
587
220k
      }
588
589
523k
      ptr_usac_data->str_quant_info[idx].max_spec_coeffs = 0;
590
14.1M
      for (k = 0; k < ptr_max_sfb[ch]; k++) {
591
13.6M
        ptr_usac_data->str_quant_info[idx].max_spec_coeffs +=
592
13.6M
            pstr_sfb_prms->sfb_width_table[ch][k];
593
13.6M
      }
594
595
24.5M
      for (i = 0; i < num_scfs[idx]; i++) {
596
24.0M
        ptr_usac_data->str_quant_info[idx].scale_factor[i] =
597
24.0M
            pstr_qc_out->str_qc_out_chan[idx].global_gain -
598
24.0M
            pstr_qc_out->str_qc_out_chan[idx].scalefactor[i] + SF_OFFSET;
599
24.0M
      }
600
601
523k
      max_bits = iusace_count_fd_bits(pstr_sfb_prms, ptr_usac_data, usac_independancy_flag,
602
523k
                                      ptr_usac_config, ch, idx);
603
604
523k
      if (max_bits > max_ch_dyn_bits[idx]) {
605
108k
        constraints_fulfilled = 0;
606
108k
      }
607
523k
      if (quant_spec_is_zero == 1) {
608
117k
        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
117k
        if (iterations > 0) {
612
911
          *is_quant_spec_zero = 1;
613
911
          max_bits = max_ch_dyn_bits[idx];
614
911
        }
615
117k
      }
616
523k
      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
12
        constraints_fulfilled = 1;
621
12
        *is_gain_limited = 1;
622
12
        max_bits = max_ch_dyn_bits[idx];
623
12
      }
624
523k
      if (!constraints_fulfilled) {
625
101k
        pstr_qc_out->str_qc_out_chan[idx].global_gain++;
626
101k
      }
627
523k
      iterations++;
628
523k
    } while (!constraints_fulfilled);
629
630
421k
    pstr_qc_out->dyn_bits += max_bits;
631
632
421k
    if (ptr_usac_data->noise_filling[ele_id]) {
633
184k
      WORD32 max_nf_sfb = ptr_max_sfb[ch];
634
635
184k
      if (ptr_window_sequence[ch] != EIGHT_SHORT_SEQUENCE) {
636
125k
        iusace_noise_filling(
637
125k
            &ptr_usac_data->noise_level[idx], &ptr_usac_data->noise_offset[idx],
638
125k
            ptr_usac_data->spectral_line_vector[ch], &ptr_usac_data->str_quant_info[idx],
639
125k
            pstr_sfb_prms->grouped_sfb_offset[ch], max_nf_sfb, ptr_usac_config->ccfl,
640
125k
            ptr_num_window_groups[ch], pstr_sfb_prms->window_group_length[ch], 160,
641
125k
            ptr_usac_data->str_scratch.p_noise_filling_highest_tone);
642
125k
      } else {
643
59.3k
        iusace_noise_filling(
644
59.3k
            &ptr_usac_data->noise_level[idx], &ptr_usac_data->noise_offset[idx],
645
59.3k
            ptr_usac_data->spectral_line_vector[ch], &ptr_usac_data->str_quant_info[idx],
646
59.3k
            pstr_sfb_prms->grouped_sfb_offset[ch], max_nf_sfb, ptr_usac_config->ccfl >> 3,
647
59.3k
            ptr_num_window_groups[ch], pstr_sfb_prms->window_group_length[ch], 20,
648
59.3k
            (FLOAT64 *)ptr_usac_data->str_scratch.p_noise_filling_highest_tone);
649
59.3k
      }
650
651
184k
      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
184k
    }
656
421k
    idx++;
657
421k
  }
658
659
235k
  pstr_adj_thr_elem->dyn_bits_last = pstr_qc_out->dyn_bits;
660
661
235k
  bitres_bits = pstr_qc_data->max_bitres_bits - pstr_qc_data->bit_res_lvl;
662
235k
  bitres_diff = pstr_qc_data->avg_bits - (pstr_qc_out->static_bits + pstr_qc_out->dyn_bits);
663
235k
  pstr_qc_out->fill_bits = MAX(0, (bitres_diff - bitres_bits));
664
665
235k
  if (pstr_qc_data->avg_bits > 0) {
666
235k
    pstr_qc_data->bit_res_lvl +=
667
235k
        pstr_qc_data->avg_bits -
668
235k
        (pstr_qc_out->static_bits + pstr_qc_out->dyn_bits + pstr_qc_out->fill_bits);
669
235k
  } else {
670
0
    pstr_qc_data->bit_res_lvl = pstr_qc_data->max_bits;
671
0
  }
672
673
235k
  if (pstr_qc_data->bit_res_lvl < 0 ||
674
230k
      pstr_qc_data->bit_res_lvl > pstr_qc_data->max_bitres_bits) {
675
5.32k
    return IA_EXHEAACE_EXE_FATAL_USAC_INVALID_BIT_RSVR_LVL;
676
5.32k
  }
677
678
230k
  return IA_NO_ERROR;
679
235k
}