Coverage Report

Created: 2026-01-25 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/ixheaace_qc_main_hp.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 <math.h>
22
#include <stdlib.h>
23
24
#include "ixheaac_type_def.h"
25
#include "ixheaac_constants.h"
26
#include "ixheaace_aac_constants.h"
27
28
#include "ixheaac_error_standards.h"
29
#include "ixheaace_error_codes.h"
30
#include "ixheaace_psy_const.h"
31
#include "ixheaace_tns.h"
32
#include "ixheaace_tns_params.h"
33
#include "ixheaace_rom.h"
34
#include "ixheaace_common_rom.h"
35
#include "ixheaace_quant.h"
36
#include "ixheaace_block_switch.h"
37
#include "ixheaace_bitbuffer.h"
38
39
#include "ixheaac_basic_ops32.h"
40
#include "ixheaac_basic_ops16.h"
41
#include "ixheaac_basic_ops40.h"
42
#include "ixheaac_basic_ops.h"
43
44
#include "ixheaace_tns.h"
45
#include "ixheaace_psy_data.h"
46
#include "ixheaace_interface.h"
47
#include "ixheaace_adjust_threshold_data.h"
48
49
#include "ixheaace_dynamic_bits.h"
50
#include "ixheaace_qc_data.h"
51
#include "ixheaace_adjust_threshold.h"
52
53
#include "ixheaace_sf_estimation.h"
54
55
#include "ixheaace_static_bits.h"
56
57
#include "ixheaace_bits_count.h"
58
59
#include "ixheaace_channel_map.h"
60
#include "ixheaace_write_bitstream.h"
61
#include "ixheaace_psy_configuration.h"
62
#include "ixheaace_psy_mod.h"
63
#include "ixheaace_tns_params.h"
64
#include "ixheaace_stereo_preproc.h"
65
#include "ixheaace_enc_main.h"
66
#include "ixheaace_qc_util.h"
67
#include "ixheaace_common_utils.h"
68
69
IA_ERRORCODE ia_enhaacplus_enc_qc_main(
70
    ixheaace_qc_state *pstr_qc_state, WORD32 num_channels, ixheaace_element_bits *pstr_el_bits,
71
    ixheaace_psy_out_channel **psy_out_ch,
72
    ixheaace_psy_out_element *pstr_psy_out_element,
73
    ixheaace_qc_out_channel **pstr_qc_out_ch,
74
    ixheaace_qc_out_element *pstr_qc_out_element, WORD32 ancillary_data_bytes,
75
    ixheaace_aac_tables *pstr_aac_tables, WORD32 adts_flag, WORD32 aot, WORD32 stat_bits_flag,
76
    WORD32 flag_last_element, WORD32 frame_len_long, WORD8 *ptr_scratch,
77
332k
    WORD32 *is_quant_spec_zero, WORD32 *is_gain_limited) {
78
332k
  IA_ERRORCODE err_code;
79
332k
  WORD32 ch;
80
332k
  WORD32 i = 0;
81
332k
  WORD32 k = 0;
82
332k
  WORD32 j = 0;
83
332k
  WORD32 iterations = 0;
84
332k
  WORD32 constraints_fulfilled;
85
332k
  WORD32 ch_dyn_bits;
86
332k
  WORD32 max_ch_dyn_bits[IXHEAACE_MAX_CH_IN_BS_ELE];
87
332k
  FLOAT32 ch_bit_dist[IXHEAACE_MAX_CH_IN_BS_ELE];
88
332k
  ixheaace_qc_stack *ptr_stack = (ixheaace_qc_stack *)ptr_scratch;
89
332k
  ptr_scratch += sizeof(ixheaace_qc_stack);
90
91
332k
  ia_adj_thr_elem_struct *pstr_adj_thr_elem = &pstr_qc_state->str_adj_thr.str_adj_thr_ele;
92
332k
  WORD32 gain;
93
94
332k
  if (pstr_el_bits->bit_res_level < 0) {
95
345
    return IA_EXHEAACE_EXE_FATAL_INVALID_BIT_RES_LEVEL;
96
345
  }
97
98
332k
  if (pstr_el_bits->bit_res_level > pstr_el_bits->max_bit_res_bits) {
99
0
    return IA_EXHEAACE_EXE_FATAL_INVALID_BIT_RES_LEVEL;
100
0
  }
101
332k
  pstr_qc_out_element->static_bits_used =
102
332k
      ia_enhaacplus_enc_count_static_bitdemand(psy_out_ch, pstr_psy_out_element, num_channels,
103
332k
                                               aot, adts_flag, stat_bits_flag, flag_last_element);
104
105
332k
  if (ancillary_data_bytes) {
106
212k
    pstr_qc_out_element->anc_bits_used =
107
212k
        7 + 8 * (ancillary_data_bytes + (ancillary_data_bytes >= 15));
108
212k
  } else {
109
119k
    pstr_qc_out_element->anc_bits_used = 0;
110
119k
  }
111
112
823k
  for (ch = 0; ch < num_channels; ch++) {
113
491k
    iaace_calc_form_fac_per_chan(ptr_stack->sfb_form_fac[ch],
114
491k
                                 ptr_stack->sfb_num_relevant_lines[ch], psy_out_ch[ch],
115
491k
                                 ptr_stack->sfb_ld_energy[ch]);
116
491k
  }
117
118
332k
  iaace_adjust_threshold(
119
332k
      &pstr_qc_state->str_adj_thr, pstr_adj_thr_elem, psy_out_ch, ch_bit_dist,
120
332k
      pstr_qc_out_element,
121
332k
      pstr_el_bits->average_bits - pstr_qc_out_element->static_bits_used -
122
332k
          pstr_qc_out_element->anc_bits_used,
123
332k
      pstr_el_bits->bit_res_level, pstr_el_bits->max_bit_res_bits,
124
332k
      pstr_qc_out_element->static_bits_used + pstr_qc_out_element->anc_bits_used,
125
332k
      &pstr_qc_state->max_bit_fac, ptr_stack->sfb_num_relevant_lines[0],
126
332k
      ptr_stack->sfb_ld_energy[0], num_channels, 0, aot, ptr_scratch);
127
128
332k
  iaace_estimate_scfs_chan(psy_out_ch, pstr_qc_out_ch, ptr_stack->sfb_form_fac,
129
332k
                           ptr_stack->sfb_num_relevant_lines, num_channels, 0, frame_len_long);
130
131
823k
  for (ch = 0; ch < num_channels; ch++) {
132
491k
    max_ch_dyn_bits[ch] =
133
491k
        (pstr_el_bits->average_bits + pstr_el_bits->bit_res_level - 7 -
134
491k
         pstr_qc_out_element->static_bits_used - pstr_qc_out_element->anc_bits_used);
135
136
491k
    max_ch_dyn_bits[ch] = (WORD32)floor(ch_bit_dist[ch] * (FLOAT32)(max_ch_dyn_bits[ch]));
137
491k
  }
138
139
332k
  pstr_qc_out_element->dyn_bits_used = 0;
140
141
823k
  for (ch = 0; ch < num_channels; ch++) {
142
    /* now loop until bitstream constraints (ch_dyn_bits < maxChDynBits)
143
       are fulfilled */
144
491k
    WORD32 spec_idx, sfb_offs, sfb;
145
491k
    iterations = 0;
146
491k
    gain = 0;
147
402M
    for (spec_idx = 0; spec_idx < frame_len_long; spec_idx++) {
148
401M
      ptr_stack->exp_spec[spec_idx] = (FLOAT32)psy_out_ch[ch]->ptr_spec_coeffs[spec_idx];
149
401M
      ptr_stack->mdct_spec_float[spec_idx] = (FLOAT32)psy_out_ch[ch]->ptr_spec_coeffs[spec_idx];
150
401M
    }
151
526k
    do {
152
526k
      WORD32 max_val;
153
526k
      constraints_fulfilled = 1;
154
526k
      WORD32 quant_spec_is_zero = 1;
155
526k
      if (iterations > 0) {
156
99.1k
        for (sfb_offs = 0; sfb_offs < psy_out_ch[ch]->sfb_count;
157
63.8k
             sfb_offs += psy_out_ch[ch]->sfb_per_group) {
158
1.19M
          for (sfb = 0; sfb < psy_out_ch[ch]->max_sfb_per_grp; sfb++) {
159
1.12M
            WORD32 scalefactor = pstr_qc_out_ch[ch]->scalefactor[sfb + sfb_offs];
160
1.12M
            gain = MAX(gain, pstr_qc_out_ch[ch]->global_gain - scalefactor);
161
1.12M
            iaace_quantize_lines(
162
1.12M
                pstr_qc_out_ch[ch]->global_gain - scalefactor,
163
1.12M
                psy_out_ch[ch]->sfb_offsets[sfb_offs + sfb + 1] -
164
1.12M
                    psy_out_ch[ch]->sfb_offsets[sfb_offs + sfb],
165
1.12M
                ptr_stack->exp_spec + psy_out_ch[ch]->sfb_offsets[sfb_offs + sfb],
166
1.12M
                pstr_qc_out_ch[ch]->quant_spec + psy_out_ch[ch]->sfb_offsets[sfb_offs + sfb],
167
1.12M
                ptr_stack->mdct_spec_float + psy_out_ch[ch]->sfb_offsets[sfb_offs + sfb]);
168
1.12M
          }
169
63.8k
        }
170
35.3k
      }
171
172
526k
      max_val = iaace_calc_max_val_in_sfb(
173
526k
          psy_out_ch[ch]->sfb_count, psy_out_ch[ch]->max_sfb_per_grp,
174
526k
          psy_out_ch[ch]->sfb_per_group,
175
526k
          psy_out_ch[ch]->sfb_offsets, pstr_qc_out_ch[ch]->quant_spec,
176
526k
          pstr_qc_out_ch[ch]->max_val_in_sfb);
177
178
526k
      if (max_val > MAXIMUM_QUANT) {
179
7.74k
        constraints_fulfilled = 0;
180
7.74k
      }
181
182
1.12M
      for (k = 0; ((k < psy_out_ch[ch]->sfb_count) && (quant_spec_is_zero));
183
596k
           k += psy_out_ch[ch]->sfb_per_group) {
184
4.59M
        for (i = 0; ((i < psy_out_ch[ch]->max_sfb_per_grp) && (quant_spec_is_zero)); i++) {
185
55.7M
          for (j = psy_out_ch[ch]->sfb_offsets[i+k]; j < psy_out_ch[ch]->sfb_offsets[i+k+1]; j++)
186
52.1M
          {
187
52.1M
            if (pstr_qc_out_ch[ch]->quant_spec[j] != 0) {
188
331k
              quant_spec_is_zero = 0;
189
331k
              break;
190
331k
            }
191
52.1M
          }
192
3.99M
        }
193
596k
      }
194
526k
      err_code = ia_enhaacplus_enc_dyn_bitcount(
195
526k
          pstr_qc_out_ch[ch]->quant_spec, pstr_qc_out_ch[ch]->max_val_in_sfb,
196
526k
          pstr_qc_out_ch[ch]->scalefactor, psy_out_ch[ch]->window_sequence,
197
526k
          psy_out_ch[ch]->sfb_count, psy_out_ch[ch]->max_sfb_per_grp,
198
526k
          psy_out_ch[ch]->sfb_per_group,
199
526k
          psy_out_ch[ch]->sfb_offsets, &pstr_qc_out_ch[ch]->section_data,
200
526k
          pstr_qc_state->side_info_tab_long, pstr_qc_state->side_info_tab_short,
201
526k
          pstr_aac_tables->pstr_huff_tab, pstr_qc_state->qc_scr.shared_buffer_2, aot,
202
526k
          &ch_dyn_bits);
203
204
526k
      if (err_code != IA_NO_ERROR) {
205
0
        return err_code;
206
0
      }
207
208
526k
      if (ch_dyn_bits >= max_ch_dyn_bits[ch]) {
209
27.6k
        constraints_fulfilled = 0;
210
27.6k
      }
211
212
526k
      if (quant_spec_is_zero == 1) {
213
195k
        constraints_fulfilled = 1;
214
        /*Bit consuption is exceding bit reservoir, there is no scope left for bit consumption
215
          reduction, as spectrum is zero. Hence breaking the quantization loop. */
216
195k
        if (iterations > 0) {
217
492
          *is_quant_spec_zero = 1;
218
492
          ch_dyn_bits = max_ch_dyn_bits[ch];
219
492
        }
220
195k
      }
221
526k
      if ((gain == MAX_GAIN_INDEX) && (constraints_fulfilled == 0)) {
222
        /* Bit consuption is exceding bit reservoir, there is no scope left for bit consumption
223
           reduction, as gain has reached the maximum value. Hence breaking the quantization
224
           loop. */
225
0
        constraints_fulfilled = 1;
226
0
        *is_gain_limited = 1;
227
0
        ch_dyn_bits = max_ch_dyn_bits[ch];
228
0
      }
229
526k
      if (!constraints_fulfilled) {
230
35.3k
        pstr_qc_out_ch[ch]->global_gain++;
231
35.3k
      }
232
526k
      iterations++;
233
234
526k
    } while (!constraints_fulfilled);
235
236
491k
    pstr_qc_out_element->dyn_bits_used += ch_dyn_bits;
237
238
491k
    pstr_qc_out_ch[ch]->grouping_mask = psy_out_ch[ch]->grouping_mask;
239
491k
    pstr_qc_out_ch[ch]->win_shape = psy_out_ch[ch]->window_shape;
240
491k
  }
241
242
332k
  pstr_adj_thr_elem->dyn_bits_last = pstr_qc_out_element->dyn_bits_used;
243
332k
  {
244
332k
    WORD32 bit_res_space = pstr_el_bits->max_bit_res_bits - pstr_el_bits->bit_res_level;
245
332k
    WORD32 delta_bit_res = pstr_el_bits->average_bits - (pstr_qc_out_element->static_bits_used +
246
332k
                                                         pstr_qc_out_element->dyn_bits_used +
247
332k
                                                         pstr_qc_out_element->anc_bits_used);
248
249
332k
    pstr_qc_out_element->fill_bits = MAX(0, (delta_bit_res - bit_res_space));
250
332k
  }
251
252
332k
  return IA_NO_ERROR;
253
332k
}
254
255
WORD32 iaace_calc_max_val_in_sfb(WORD32 sfb_count, WORD32 max_sfb_per_grp, WORD32 ptr_sfb_per_grp,
256
                                 WORD32 *ptr_sfb_offset, WORD16 *ptr_quant_spec,
257
526k
                                 UWORD16 *ptr_max_value) {
258
526k
  WORD32 sfb;
259
526k
  WORD32 max = 0;
260
526k
  WORD32 sfb_offs;
261
262
1.34M
  for (sfb_offs = 0; sfb_offs < sfb_count; sfb_offs += ptr_sfb_per_grp) {
263
15.6M
    for (sfb = 0; sfb < max_sfb_per_grp; sfb++) {
264
14.7M
      WORD32 line;
265
14.7M
      WORD32 local_max = 0;
266
228M
      for (line = ptr_sfb_offset[sfb + sfb_offs]; line < ptr_sfb_offset[sfb + sfb_offs + 1];
267
213M
           line++) {
268
213M
        if (abs(ptr_quant_spec[line]) > local_max) {
269
11.5M
          local_max = abs(ptr_quant_spec[line]);
270
11.5M
        }
271
213M
      }
272
14.7M
      ptr_max_value[sfb_offs + sfb] = (UWORD16)local_max;
273
14.7M
      if (local_max > max) {
274
469k
        max = local_max;
275
469k
      }
276
14.7M
    }
277
818k
  }
278
279
526k
  return max;
280
526k
}