Coverage Report

Created: 2026-08-31 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/ixheaace_qc_util.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
23
#include "ixheaac_type_def.h"
24
#include "ixheaac_constants.h"
25
#include "impd_drc_common_enc.h"
26
#include "impd_drc_uni_drc.h"
27
#include "impd_drc_tables.h"
28
#include "impd_drc_api.h"
29
#include "ixheaace_api.h"
30
#include "ixheaace_aac_constants.h"
31
#include "ixheaace_error_codes.h"
32
#include "ixheaac_error_standards.h"
33
#include <stdlib.h>
34
#include "ixheaace_psy_const.h"
35
#include "ixheaace_tns.h"
36
#include "ixheaace_tns_params.h"
37
#include "ixheaace_rom.h"
38
#include "ixheaace_common_rom.h"
39
#include "ixheaace_quant.h"
40
#include "ixheaace_block_switch.h"
41
#include "ixheaace_bitbuffer.h"
42
43
#include "ixheaac_basic_ops32.h"
44
#include "ixheaac_basic_ops16.h"
45
#include "ixheaac_basic_ops40.h"
46
#include "ixheaac_basic_ops.h"
47
#include "ixheaace_psy_const.h"
48
#include "ixheaace_tns.h"
49
#include "ixheaace_psy_data.h"
50
#include "ixheaace_interface.h"
51
#include "ixheaace_adjust_threshold_data.h"
52
53
#include "ixheaace_dynamic_bits.h"
54
#include "ixheaace_qc_data.h"
55
#include "ixheaace_adjust_threshold.h"
56
57
#include "ixheaace_sf_estimation.h"
58
59
#include "ixheaace_static_bits.h"
60
61
#include "ixheaace_bits_count.h"
62
#include "ixheaace_channel_map.h"
63
#include "ixheaace_write_bitstream.h"
64
#include "ixheaace_psy_configuration.h"
65
#include "ixheaace_psy_mod.h"
66
#include "ixheaace_tns_params.h"
67
#include "ixheaace_stereo_preproc.h"
68
#include "ixheaace_enc_main.h"
69
#include "ixheaace_qc_util.h"
70
#include "ixheaace_common_utils.h"
71
72
static WORD32 ia_enhaacplus_enc_calc_frame_len(WORD32 bit_rate, WORD32 sample_rate,
73
                                               FRAME_LEN_RESULT_MODE mode,
74
616k
                                               WORD32 long_frame_len) {
75
616k
  WORD32 result;
76
77
616k
  result = ((long_frame_len) >> 3) * (bit_rate);
78
79
616k
  switch (mode) {
80
308k
    case FRAME_LEN_BYTES_MODULO:
81
308k
      result %= sample_rate;
82
308k
      break;
83
308k
    case FRAME_LEN_BYTES_INT:
84
308k
      result /= sample_rate;
85
308k
      break;
86
0
    default:
87
0
      break;
88
616k
  }
89
90
616k
  return result;
91
616k
}
92
93
static WORD32 ia_enhaacplus_enc_frame_padding(WORD32 bit_rate, WORD32 sample_rate,
94
308k
                                              WORD32 *ptr_padding_rest, WORD32 frame_len_long) {
95
308k
  WORD32 padding_on;
96
308k
  WORD32 difference;
97
98
308k
  padding_on = 0;
99
100
308k
  difference = ia_enhaacplus_enc_calc_frame_len(bit_rate, sample_rate, FRAME_LEN_BYTES_MODULO,
101
308k
                                                frame_len_long);
102
103
308k
  *ptr_padding_rest -= difference;
104
105
308k
  if (*ptr_padding_rest <= 0) {
106
84.0k
    padding_on = 1;
107
108
84.0k
    *ptr_padding_rest += sample_rate;
109
84.0k
  }
110
111
308k
  return padding_on;
112
308k
}
113
114
IA_ERRORCODE ia_enhaacplus_enc_qc_out_new(ixheaace_qc_out *pstr_qc_out, WORD32 num_channels,
115
                                          WORD32 *ptr_shared_buffer1, WORD32 *ptr_shared_buffer3,
116
                                          WORD32 long_frame_len)
117
118
4.78k
{
119
4.78k
  WORD32 i;
120
121
11.7k
  for (i = 0; i < num_channels; i++) {
122
7.00k
    pstr_qc_out->qc_channel[i]->quant_spec = &((WORD16 *)ptr_shared_buffer1)[i * long_frame_len];
123
124
7.00k
    memset(pstr_qc_out->qc_channel[i]->quant_spec, 0,
125
7.00k
           sizeof(*pstr_qc_out->qc_channel[i]->quant_spec) * long_frame_len);
126
127
7.00k
    pstr_qc_out->qc_channel[i]->max_val_in_sfb =
128
7.00k
        &((UWORD16 *)&ptr_shared_buffer3[(long_frame_len + long_frame_len / 2) +
129
7.00k
                                         IXHEAACE_MAX_CH_IN_BS_ELE *
130
7.00k
                                             MAXIMUM_GROUPED_SCALE_FACTOR_BAND /
131
7.00k
                                             2])[i * MAXIMUM_GROUPED_SCALE_FACTOR_BAND];
132
7.00k
    memset(
133
7.00k
        pstr_qc_out->qc_channel[i]->max_val_in_sfb, 0,
134
7.00k
        sizeof(*pstr_qc_out->qc_channel[i]->max_val_in_sfb) * MAXIMUM_GROUPED_SCALE_FACTOR_BAND);
135
136
7.00k
    pstr_qc_out->qc_channel[i]->scalefactor = &((WORD16 *)&ptr_shared_buffer3[(
137
7.00k
        long_frame_len + long_frame_len / 2)])[i * MAXIMUM_GROUPED_SCALE_FACTOR_BAND];
138
139
7.00k
    memset(pstr_qc_out->qc_channel[i]->scalefactor, 0,
140
7.00k
           sizeof(*pstr_qc_out->qc_channel[i]->scalefactor) * MAXIMUM_GROUPED_SCALE_FACTOR_BAND);
141
7.00k
  }
142
143
4.78k
  if (pstr_qc_out == NULL) {
144
0
    return IA_EXHEAACE_INIT_FATAL_AAC_INIT_FAILED;
145
0
  }
146
4.78k
  return IA_NO_ERROR;
147
4.78k
}
148
149
IA_ERRORCODE ia_enhaacplus_enc_qc_new(ixheaace_qc_state *pstr_qc_state,
150
                                      WORD32 *ptr_shared_buffer_2, WORD32 long_frame_len
151
4.78k
) {
152
4.78k
  memset(pstr_qc_state, 0, sizeof(ixheaace_qc_state));
153
4.78k
  pstr_qc_state->qc_scr.shared_buffer_2 =
154
4.78k
      (ptr_shared_buffer_2 + long_frame_len * IXHEAACE_MAX_CH_IN_BS_ELE + 16);
155
156
4.78k
  return IA_NO_ERROR;
157
4.78k
}
158
159
IA_ERRORCODE ia_enhaacplus_enc_qc_init(ixheaace_qc_state *pstr_qc_state, WORD32 aot,
160
4.78k
                                       ixheaace_qc_init *pstr_init, FLAG flag_framelength_small) {
161
4.78k
  IA_ERRORCODE error = IA_NO_ERROR;
162
4.78k
  pstr_qc_state->num_channels = pstr_init->pstr_element_info->n_channels_in_el;
163
4.78k
  pstr_qc_state->max_bits_tot = pstr_init->max_bits;
164
4.78k
  switch (aot) {
165
1.00k
    case AOT_AAC_LC:
166
2.72k
    case AOT_SBR:
167
2.98k
    case AOT_PS:
168
2.98k
      pstr_qc_state->bit_res_tot = pstr_init->bit_res - pstr_init->average_bits;
169
2.98k
      break;
170
171
533
    case AOT_AAC_LD:
172
1.80k
    case AOT_AAC_ELD:
173
1.80k
      if (pstr_init->bit_res) {
174
1.18k
        pstr_qc_state->bit_res_tot = pstr_init->bit_res - pstr_init->average_bits;
175
1.18k
      } else {
176
621
        pstr_qc_state->bit_res_tot = 0;
177
621
      }
178
1.80k
      break;
179
4.78k
  }
180
4.78k
  pstr_qc_state->average_bits_tot = pstr_init->average_bits;
181
4.78k
  pstr_qc_state->max_bit_fac = pstr_init->max_bit_fac;
182
4.78k
  pstr_qc_state->padding.padding_rest = pstr_init->padding.padding_rest;
183
184
4.78k
  pstr_qc_state->quality_level = pstr_init->inv_quant;
185
4.78k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
186
2.98k
    pstr_qc_state->glob_stat_bits = 3; /* for ID_END */
187
2.98k
  }
188
4.78k
  error = ia_enhaacplus_enc_init_element_bits(
189
4.78k
      &pstr_qc_state->element_bits, *pstr_init->pstr_element_info, pstr_init->bitrate,
190
4.78k
      pstr_init->average_bits, aot, pstr_qc_state->glob_stat_bits, pstr_init->bit_res,
191
4.78k
      flag_framelength_small);
192
193
4.78k
  if (error != IA_NO_ERROR) {
194
0
    return error;
195
0
  }
196
4.78k
  iaace_adj_thr_init(&pstr_qc_state->str_adj_thr, pstr_init->mean_pe,
197
4.78k
                     pstr_qc_state->element_bits.ch_bitrate, aot);
198
199
4.78k
  ia_enhaacplus_enc_bitcount_init((WORD32 *)pstr_qc_state->side_info_tab_long,
200
4.78k
                                  (WORD32 *)pstr_qc_state->side_info_tab_short);
201
202
4.78k
  return IA_NO_ERROR;
203
4.78k
}
204
205
VOID ia_enhaacplus_enc_update_bit_reservoir(ixheaace_qc_state *pstr_qc_kernel,
206
                                            ixheaace_qc_out *pstr_qc_out)
207
208
308k
{
209
308k
  ixheaace_element_bits *pstr_el_bits;
210
211
308k
  pstr_qc_kernel->bit_res_tot = 0;
212
213
308k
  pstr_el_bits = &pstr_qc_kernel->element_bits;
214
215
308k
  if (pstr_el_bits->average_bits > 0) {
216
    /* constant bitrate */
217
308k
    pstr_el_bits->bit_res_level +=
218
308k
        pstr_el_bits->average_bits -
219
308k
        (pstr_qc_out->qc_element.static_bits_used + pstr_qc_out->qc_element.dyn_bits_used +
220
308k
         pstr_qc_out->qc_element.anc_bits_used + pstr_qc_out->qc_element.fill_bits);
221
222
308k
    pstr_qc_kernel->bit_res_tot += pstr_el_bits->bit_res_level;
223
308k
  } else {
224
    /* variable bitrate */
225
0
    pstr_el_bits->bit_res_level = pstr_el_bits->max_bits;
226
0
    pstr_qc_kernel->bit_res_tot = pstr_qc_kernel->max_bits_tot;
227
0
  }
228
308k
}
229
230
IA_ERRORCODE ia_enhaacplus_enc_finalize_bit_consumption(ixheaace_qc_state *pstr_qc_kernel,
231
                                                        ixheaace_qc_out *pstr_qc_out,
232
                                                        WORD32 flag_last_element, WORD32 cnt_bits,
233
                                                        WORD32 *tot_fill_bits,
234
                                                        iexheaac_encoder_str **pstr_aac_enc,
235
308k
                                                        WORD32 num_bs_elements, WORD32 aot) {
236
308k
  WORD32 n_full_fill_elem, diff_bits;
237
308k
  WORD32 total_fill_bits = 0;
238
239
308k
  const WORD32 max_fill_elem_bits = 7 + 270 * 8;
240
308k
  WORD32 tfb_flag = 0;
241
308k
  WORD32 tfb_flag1 = 0;
242
308k
  WORD32 tfb_flag2 = 0;
243
244
308k
  pstr_qc_out->tot_static_bits_used = (flag_last_element ? pstr_qc_kernel->glob_stat_bits : 0);
245
246
308k
  pstr_qc_out->tot_dyn_bits_used = 0;
247
308k
  pstr_qc_out->tot_anc_bits_used = 0;
248
308k
  pstr_qc_out->total_fill_bits = 0;
249
308k
  pstr_qc_out->tot_static_bits_used += pstr_qc_out->qc_element.static_bits_used;
250
308k
  pstr_qc_out->tot_dyn_bits_used += pstr_qc_out->qc_element.dyn_bits_used;
251
308k
  pstr_qc_out->tot_anc_bits_used += pstr_qc_out->qc_element.anc_bits_used;
252
308k
  pstr_qc_out->total_fill_bits += pstr_qc_out->qc_element.fill_bits;
253
254
  /* Accumulate total fill bits */
255
308k
  *tot_fill_bits += pstr_qc_out->qc_element.fill_bits;
256
308k
  if (flag_last_element) {
257
148k
    WORD32 i, j, temp_resv;
258
148k
    WORD32 bit_resv_spc[(MAXIMUM_BS_ELE << 1) + 1];
259
148k
    WORD32 bit_resv_spc_sort[(MAXIMUM_BS_ELE << 1) + 1] = {0, 1,  2,  3,  4,  5,  6,  7, 8,
260
148k
                                                           9, 10, 11, 12, 13, 14, 15, 16};
261
262
148k
    total_fill_bits = *tot_fill_bits;
263
264
    /* Distribute fill bits among all channel elements for next frame */
265
148k
    if (total_fill_bits > 0) {
266
      /* Generate array of vacancies in bit reservoirs */
267
226k
      for (i = 0, temp_resv = 0; i < num_bs_elements; i++, temp_resv++) {
268
152k
        bit_resv_spc[temp_resv] = (pstr_aac_enc[i]->qc_kernel.element_bits.max_bit_res_bits -
269
152k
                                   pstr_aac_enc[i]->qc_kernel.element_bits.bit_res_level);
270
271
        /* CPE gets double the weight of SCE, so split CPE reservoir into two */
272
152k
        if (pstr_aac_enc[i]->qc_kernel.num_channels == 2) {
273
69.1k
          bit_resv_spc[temp_resv + 1] = bit_resv_spc[temp_resv] >> 1;
274
69.1k
          bit_resv_spc[temp_resv] -= bit_resv_spc[temp_resv + 1];
275
69.1k
          temp_resv++;
276
69.1k
        }
277
152k
      }
278
279
      /* Sort bit_resv_spc[] in descending order of levels and
280
      store the order in bit_resv_spc_sort[] */
281
221k
      for (i = (temp_resv - 1); i > 0; i--) {
282
505k
        for (j = 0; j < i; j++) {
283
357k
          if (bit_resv_spc[bit_resv_spc_sort[j]] < bit_resv_spc[bit_resv_spc_sort[j + 1]]) {
284
29.6k
            WORD32 tmp_var = bit_resv_spc_sort[j];
285
29.6k
            bit_resv_spc_sort[j] = bit_resv_spc_sort[j + 1];
286
29.6k
            bit_resv_spc_sort[j + 1] = tmp_var;
287
29.6k
          }
288
357k
        }
289
147k
      }
290
291
      /* One dummy full reservoir at the end to help in bit distribution */
292
74.2k
      bit_resv_spc[temp_resv] = 0;
293
74.2k
      bit_resv_spc_sort[temp_resv] = temp_resv;
294
295
      /* Distribute fill bits among reservoirs in the order of bit_resv_spc_sort[]:
296
      - Bring up [0] to the level of [1]
297
      - Next bring up [0] and [1] to the level of [2]...and so on */
298
282k
      for (i = 1; ((i < (temp_resv + 1)) && (total_fill_bits > 0)); i++) {
299
207k
        if (((bit_resv_spc[bit_resv_spc_sort[0]] - bit_resv_spc[bit_resv_spc_sort[i]]) * i) <=
300
207k
            total_fill_bits) {
301
203k
          total_fill_bits -=
302
203k
              ((bit_resv_spc[bit_resv_spc_sort[0]] - bit_resv_spc[bit_resv_spc_sort[i]]) * i);
303
709k
          for (j = 0; j < i; j++) {
304
506k
            bit_resv_spc[bit_resv_spc_sort[j]] = bit_resv_spc[bit_resv_spc_sort[i]];
305
506k
          }
306
203k
        } else {
307
4.66k
          WORD32 div_bs_ele;
308
309
4.66k
          div_bs_ele = (WORD32)(total_fill_bits / i);
310
4.66k
          total_fill_bits -= (div_bs_ele * i);
311
312
17.0k
          for (j = 0; j < i; j++) {
313
12.3k
            bit_resv_spc[bit_resv_spc_sort[j]] -= div_bs_ele;
314
12.3k
          }
315
316
8.57k
          for (j = 0; ((j < i) && (total_fill_bits > 0)); j++) {
317
3.91k
            bit_resv_spc[bit_resv_spc_sort[j]]--;
318
3.91k
            total_fill_bits--;
319
3.91k
          }
320
4.66k
        }
321
207k
      }
322
323
      /* Supply additional bits added for coding next frame */
324
226k
      for (i = 0, temp_resv = 0; i < num_bs_elements; i++, temp_resv++) {
325
152k
        WORD32 add_bits;
326
327
152k
        add_bits = (pstr_aac_enc[i]->qc_kernel.element_bits.max_bit_res_bits -
328
152k
                    pstr_aac_enc[i]->qc_kernel.element_bits.bit_res_level) -
329
152k
                   bit_resv_spc[temp_resv];
330
331
        /* Because CPE reservoir has been split into two */
332
152k
        if (pstr_aac_enc[i]->qc_kernel.num_channels == 2) {
333
69.1k
          temp_resv++;
334
69.1k
          add_bits -= bit_resv_spc[temp_resv];
335
69.1k
        }
336
337
        /* These will be in addition to the avg. bitrate for the next frame */
338
152k
        pstr_aac_enc[i]->qc_kernel.element_bits.carry_bits = add_bits;
339
152k
      }
340
341
      /* Update remaining fill bits */
342
74.2k
      *tot_fill_bits = total_fill_bits;
343
74.2k
    }
344
345
148k
    n_full_fill_elem = (total_fill_bits - 1) / max_fill_elem_bits;
346
347
148k
    if (n_full_fill_elem) {
348
43.8k
      total_fill_bits -= n_full_fill_elem * max_fill_elem_bits;
349
43.8k
    }
350
351
148k
    if (total_fill_bits > 0) {
352
      /* minimum Fillelement contains 7 (TAG + byte cnt) bits */
353
69.5k
      total_fill_bits = MAX(7, total_fill_bits);
354
355
      /* fill element size equals n*8 + 7 */
356
69.5k
      total_fill_bits += ((8 - (total_fill_bits - 7) % 8) % 8);
357
358
69.5k
      switch (total_fill_bits) {
359
1.59k
        case 7:
360
1.59k
          tfb_flag2 = 1;
361
1.59k
          break;
362
363
798
        case 15:
364
798
          tfb_flag1 = 1;
365
798
          break;
366
367
67.1k
        default:
368
67.1k
          tfb_flag = 1;
369
67.1k
          break;
370
69.5k
      }
371
69.5k
    }
372
373
148k
    total_fill_bits += n_full_fill_elem * max_fill_elem_bits;
374
375
148k
    pstr_qc_out->align_bits =
376
148k
        7 - (cnt_bits + pstr_qc_out->tot_dyn_bits_used + pstr_qc_out->tot_static_bits_used +
377
148k
             pstr_qc_out->tot_anc_bits_used + +total_fill_bits - 1) %
378
148k
                8;
379
148k
    if (((pstr_qc_out->align_bits + total_fill_bits - *tot_fill_bits) == 8) &&
380
39.0k
        (total_fill_bits > 8)) {
381
38.4k
      total_fill_bits -= 8;
382
38.4k
    }
383
384
148k
    diff_bits = (pstr_qc_out->align_bits + total_fill_bits) - *tot_fill_bits;
385
386
148k
    if (diff_bits) {
387
96.7k
      if (diff_bits < 0) {
388
0
        return IA_EXHEAACE_EXE_FATAL_INVALID_BIT_CONSUMPTION;
389
96.7k
      } else {
390
96.7k
        {
391
96.7k
          if (cnt_bits + pstr_qc_out->tot_static_bits_used + pstr_qc_out->tot_dyn_bits_used +
392
96.7k
                  pstr_qc_out->tot_anc_bits_used + total_fill_bits >
393
96.7k
              12288) {
394
17.2k
            if ((diff_bits > 8) && (total_fill_bits > 8)) {
395
6.63k
              if (tfb_flag || tfb_flag1) {
396
6.61k
                total_fill_bits -= 8;
397
6.61k
              }
398
6.63k
              if (tfb_flag2) {
399
13
                total_fill_bits -= 7;
400
13
              }
401
6.63k
            }
402
79.4k
          } else {
403
79.4k
            if (pstr_qc_kernel->element_bits.bit_res_level - diff_bits > 0) {
404
74.1k
              pstr_qc_kernel->element_bits.bit_res_level -= diff_bits;
405
74.1k
              pstr_qc_kernel->bit_res_tot = pstr_qc_kernel->element_bits.bit_res_level;
406
74.1k
            } else {
407
5.36k
              if ((diff_bits > 8) && (total_fill_bits > 8) && (tfb_flag)) {
408
2.57k
                total_fill_bits -= 8;
409
2.78k
              } else if ((diff_bits > 8) && (total_fill_bits > 8) && (tfb_flag1)) {
410
291
                total_fill_bits -= 8;
411
2.49k
              } else if ((diff_bits > 8) && (total_fill_bits > 8) && (tfb_flag2)) {
412
213
                total_fill_bits -= 7;
413
213
              }
414
5.36k
            }
415
79.4k
          }
416
96.7k
        }
417
96.7k
      }
418
96.7k
    }
419
148k
    switch (aot) {
420
24.5k
      case AOT_AAC_LC:
421
64.3k
      case AOT_SBR:
422
86.3k
      case AOT_PS:
423
86.3k
        *tot_fill_bits = total_fill_bits;
424
86.3k
        break;
425
426
10.6k
      case AOT_AAC_LD:
427
61.8k
      case AOT_AAC_ELD:
428
61.8k
        pstr_qc_out->total_fill_bits = total_fill_bits;
429
61.8k
        *tot_fill_bits = 0;
430
61.8k
        break;
431
148k
    }
432
148k
  }  // if flag_last_element
433
159k
  else {
434
159k
    pstr_qc_out->align_bits = 0;
435
159k
  }
436
437
308k
  if ((pstr_qc_out->tot_dyn_bits_used + pstr_qc_out->tot_static_bits_used +
438
308k
       pstr_qc_out->tot_anc_bits_used + pstr_qc_out->total_fill_bits + pstr_qc_out->align_bits) >
439
308k
      pstr_qc_kernel->max_bits_tot) {
440
29.4k
  }
441
442
308k
  return IA_NO_ERROR;
443
308k
}
444
445
VOID ia_enhaacplus_enc_adjust_bitrate(ixheaace_qc_state *pstr_qc_state, WORD32 bit_rate,
446
                                      WORD32 sample_rate, WORD32 flag_last_element,
447
                                      WORD32 frame_len_long)
448
449
308k
{
450
308k
  WORD32 padding_on;
451
308k
  WORD32 frame_len;
452
308k
  WORD32 code_bits;
453
308k
  WORD32 code_bits_last;
454
455
308k
  padding_on = ia_enhaacplus_enc_frame_padding(
456
308k
      bit_rate, sample_rate, &pstr_qc_state->padding.padding_rest, frame_len_long);
457
458
308k
  frame_len = padding_on + ia_enhaacplus_enc_calc_frame_len(bit_rate, sample_rate,
459
308k
                                                            FRAME_LEN_BYTES_INT, frame_len_long);
460
461
308k
  frame_len <<= 3;
462
463
308k
  if (flag_last_element) {
464
148k
    code_bits_last = pstr_qc_state->average_bits_tot - pstr_qc_state->glob_stat_bits;
465
466
148k
    code_bits = frame_len - pstr_qc_state->glob_stat_bits;
467
159k
  } else {
468
159k
    code_bits_last = pstr_qc_state->average_bits_tot;
469
470
159k
    code_bits = frame_len;
471
159k
  }
472
473
  /* calculate bits for every channel element */
474
308k
  if (code_bits != code_bits_last) {
475
89.2k
    WORD32 total_bits = 0;
476
477
89.2k
    pstr_qc_state->element_bits.average_bits =
478
89.2k
        (WORD32)(pstr_qc_state->element_bits.relative_bits * code_bits);
479
480
89.2k
    total_bits += pstr_qc_state->element_bits.average_bits;
481
482
89.2k
    pstr_qc_state->element_bits.average_bits += code_bits - total_bits;
483
89.2k
  }
484
485
308k
  pstr_qc_state->average_bits_tot = frame_len;
486
487
  /* Bits carried over from previous frame due to distribution of fill bits */
488
308k
  pstr_qc_state->element_bits.average_bits += pstr_qc_state->element_bits.carry_bits;
489
308k
  pstr_qc_state->average_bits_tot += pstr_qc_state->element_bits.carry_bits;
490
491
  /* Flush for current frame */
492
308k
  pstr_qc_state->element_bits.carry_bits = 0;
493
308k
}
494
495
WORD32 ia_enhaacplus_aac_limitbitrate(WORD32 core_sampling_rate, WORD32 frame_length,
496
4.80k
                                      WORD32 num_channels, WORD32 bit_rate) {
497
4.80k
  WORD32 prev_bit_rate, shift = 0, iter = 0;
498
4.80k
  WORD32 max_ch_bits = MAXIMUM_CHANNEL_BITS_1024;
499
500
26.9k
  while ((frame_length & ~((1 << (shift + 1)) - 1)) == frame_length &&
501
25.7k
         (core_sampling_rate & ~((1 << (shift + 1)) - 1)) == core_sampling_rate) {
502
22.1k
    shift++;
503
22.1k
  }
504
505
4.80k
  max_ch_bits = MAXIMUM_CHANNEL_BITS_1024 * frame_length / MAX_FRAME_LEN;
506
507
6.46k
  do {
508
6.46k
    prev_bit_rate = bit_rate;
509
510
6.46k
    bit_rate = MAX(bit_rate, ((((40 * num_channels) + TRANSPORT_BITS) * (core_sampling_rate)) /
511
6.46k
                              frame_length));
512
6.46k
    bit_rate = MIN(bit_rate, ((num_channels * max_ch_bits) * (core_sampling_rate >> shift)) /
513
6.46k
                                 (frame_length >> shift));
514
515
6.46k
  } while (prev_bit_rate != bit_rate && iter++ < 3);
516
517
4.80k
  return bit_rate;
518
4.80k
}