Coverage Report

Created: 2026-05-30 06:39

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
612k
                                               WORD32 long_frame_len) {
75
612k
  WORD32 result;
76
77
612k
  result = ((long_frame_len) >> 3) * (bit_rate);
78
79
612k
  switch (mode) {
80
306k
    case FRAME_LEN_BYTES_MODULO:
81
306k
      result %= sample_rate;
82
306k
      break;
83
306k
    case FRAME_LEN_BYTES_INT:
84
306k
      result /= sample_rate;
85
306k
      break;
86
0
    default:
87
0
      break;
88
612k
  }
89
90
612k
  return result;
91
612k
}
92
93
static WORD32 ia_enhaacplus_enc_frame_padding(WORD32 bit_rate, WORD32 sample_rate,
94
306k
                                              WORD32 *ptr_padding_rest, WORD32 frame_len_long) {
95
306k
  WORD32 padding_on;
96
306k
  WORD32 difference;
97
98
306k
  padding_on = 0;
99
100
306k
  difference = ia_enhaacplus_enc_calc_frame_len(bit_rate, sample_rate, FRAME_LEN_BYTES_MODULO,
101
306k
                                                frame_len_long);
102
103
306k
  *ptr_padding_rest -= difference;
104
105
306k
  if (*ptr_padding_rest <= 0) {
106
81.6k
    padding_on = 1;
107
108
81.6k
    *ptr_padding_rest += sample_rate;
109
81.6k
  }
110
111
306k
  return padding_on;
112
306k
}
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.96k
{
119
4.96k
  WORD32 i;
120
121
12.2k
  for (i = 0; i < num_channels; i++) {
122
7.25k
    pstr_qc_out->qc_channel[i]->quant_spec = &((WORD16 *)ptr_shared_buffer1)[i * long_frame_len];
123
124
7.25k
    memset(pstr_qc_out->qc_channel[i]->quant_spec, 0,
125
7.25k
           sizeof(*pstr_qc_out->qc_channel[i]->quant_spec) * long_frame_len);
126
127
7.25k
    pstr_qc_out->qc_channel[i]->max_val_in_sfb =
128
7.25k
        &((UWORD16 *)&ptr_shared_buffer3[(long_frame_len + long_frame_len / 2) +
129
7.25k
                                         IXHEAACE_MAX_CH_IN_BS_ELE *
130
7.25k
                                             MAXIMUM_GROUPED_SCALE_FACTOR_BAND /
131
7.25k
                                             2])[i * MAXIMUM_GROUPED_SCALE_FACTOR_BAND];
132
7.25k
    memset(
133
7.25k
        pstr_qc_out->qc_channel[i]->max_val_in_sfb, 0,
134
7.25k
        sizeof(*pstr_qc_out->qc_channel[i]->max_val_in_sfb) * MAXIMUM_GROUPED_SCALE_FACTOR_BAND);
135
136
7.25k
    pstr_qc_out->qc_channel[i]->scalefactor = &((WORD16 *)&ptr_shared_buffer3[(
137
7.25k
        long_frame_len + long_frame_len / 2)])[i * MAXIMUM_GROUPED_SCALE_FACTOR_BAND];
138
139
7.25k
    memset(pstr_qc_out->qc_channel[i]->scalefactor, 0,
140
7.25k
           sizeof(*pstr_qc_out->qc_channel[i]->scalefactor) * MAXIMUM_GROUPED_SCALE_FACTOR_BAND);
141
7.25k
  }
142
143
4.96k
  if (pstr_qc_out == NULL) {
144
0
    return IA_EXHEAACE_INIT_FATAL_AAC_INIT_FAILED;
145
0
  }
146
4.96k
  return IA_NO_ERROR;
147
4.96k
}
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.96k
) {
152
4.96k
  memset(pstr_qc_state, 0, sizeof(ixheaace_qc_state));
153
4.96k
  pstr_qc_state->qc_scr.shared_buffer_2 =
154
4.96k
      (ptr_shared_buffer_2 + long_frame_len * IXHEAACE_MAX_CH_IN_BS_ELE + 16);
155
156
4.96k
  return IA_NO_ERROR;
157
4.96k
}
158
159
IA_ERRORCODE ia_enhaacplus_enc_qc_init(ixheaace_qc_state *pstr_qc_state, WORD32 aot,
160
4.96k
                                       ixheaace_qc_init *pstr_init, FLAG flag_framelength_small) {
161
4.96k
  IA_ERRORCODE error = IA_NO_ERROR;
162
4.96k
  pstr_qc_state->num_channels = pstr_init->pstr_element_info->n_channels_in_el;
163
4.96k
  pstr_qc_state->max_bits_tot = pstr_init->max_bits;
164
4.96k
  switch (aot) {
165
1.09k
    case AOT_AAC_LC:
166
2.90k
    case AOT_SBR:
167
3.15k
    case AOT_PS:
168
3.15k
      pstr_qc_state->bit_res_tot = pstr_init->bit_res - pstr_init->average_bits;
169
3.15k
      break;
170
171
526
    case AOT_AAC_LD:
172
1.81k
    case AOT_AAC_ELD:
173
1.81k
      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
624
        pstr_qc_state->bit_res_tot = 0;
177
624
      }
178
1.81k
      break;
179
4.96k
  }
180
4.96k
  pstr_qc_state->average_bits_tot = pstr_init->average_bits;
181
4.96k
  pstr_qc_state->max_bit_fac = pstr_init->max_bit_fac;
182
4.96k
  pstr_qc_state->padding.padding_rest = pstr_init->padding.padding_rest;
183
184
4.96k
  pstr_qc_state->quality_level = pstr_init->inv_quant;
185
4.96k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
186
3.15k
    pstr_qc_state->glob_stat_bits = 3; /* for ID_END */
187
3.15k
  }
188
4.96k
  error = ia_enhaacplus_enc_init_element_bits(
189
4.96k
      &pstr_qc_state->element_bits, *pstr_init->pstr_element_info, pstr_init->bitrate,
190
4.96k
      pstr_init->average_bits, aot, pstr_qc_state->glob_stat_bits, pstr_init->bit_res,
191
4.96k
      flag_framelength_small);
192
193
4.96k
  if (error != IA_NO_ERROR) {
194
0
    return error;
195
0
  }
196
4.96k
  iaace_adj_thr_init(&pstr_qc_state->str_adj_thr, pstr_init->mean_pe,
197
4.96k
                     pstr_qc_state->element_bits.ch_bitrate, aot);
198
199
4.96k
  ia_enhaacplus_enc_bitcount_init((WORD32 *)pstr_qc_state->side_info_tab_long,
200
4.96k
                                  (WORD32 *)pstr_qc_state->side_info_tab_short);
201
202
4.96k
  return IA_NO_ERROR;
203
4.96k
}
204
205
VOID ia_enhaacplus_enc_update_bit_reservoir(ixheaace_qc_state *pstr_qc_kernel,
206
                                            ixheaace_qc_out *pstr_qc_out)
207
208
306k
{
209
306k
  ixheaace_element_bits *pstr_el_bits;
210
211
306k
  pstr_qc_kernel->bit_res_tot = 0;
212
213
306k
  pstr_el_bits = &pstr_qc_kernel->element_bits;
214
215
306k
  if (pstr_el_bits->average_bits > 0) {
216
    /* constant bitrate */
217
306k
    pstr_el_bits->bit_res_level +=
218
306k
        pstr_el_bits->average_bits -
219
306k
        (pstr_qc_out->qc_element.static_bits_used + pstr_qc_out->qc_element.dyn_bits_used +
220
306k
         pstr_qc_out->qc_element.anc_bits_used + pstr_qc_out->qc_element.fill_bits);
221
222
306k
    pstr_qc_kernel->bit_res_tot += pstr_el_bits->bit_res_level;
223
306k
  } 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
306k
}
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
306k
                                                        WORD32 num_bs_elements, WORD32 aot) {
236
306k
  WORD32 n_full_fill_elem, diff_bits;
237
306k
  WORD32 total_fill_bits = 0;
238
239
306k
  const WORD32 max_fill_elem_bits = 7 + 270 * 8;
240
306k
  WORD32 tfb_flag = 0;
241
306k
  WORD32 tfb_flag1 = 0;
242
306k
  WORD32 tfb_flag2 = 0;
243
244
306k
  pstr_qc_out->tot_static_bits_used = (flag_last_element ? pstr_qc_kernel->glob_stat_bits : 0);
245
246
306k
  pstr_qc_out->tot_dyn_bits_used = 0;
247
306k
  pstr_qc_out->tot_anc_bits_used = 0;
248
306k
  pstr_qc_out->total_fill_bits = 0;
249
306k
  pstr_qc_out->tot_static_bits_used += pstr_qc_out->qc_element.static_bits_used;
250
306k
  pstr_qc_out->tot_dyn_bits_used += pstr_qc_out->qc_element.dyn_bits_used;
251
306k
  pstr_qc_out->tot_anc_bits_used += pstr_qc_out->qc_element.anc_bits_used;
252
306k
  pstr_qc_out->total_fill_bits += pstr_qc_out->qc_element.fill_bits;
253
254
  /* Accumulate total fill bits */
255
306k
  *tot_fill_bits += pstr_qc_out->qc_element.fill_bits;
256
306k
  if (flag_last_element) {
257
149k
    WORD32 i, j, temp_resv;
258
149k
    WORD32 bit_resv_spc[(MAXIMUM_BS_ELE << 1) + 1];
259
149k
    WORD32 bit_resv_spc_sort[(MAXIMUM_BS_ELE << 1) + 1] = {0, 1,  2,  3,  4,  5,  6,  7, 8,
260
149k
                                                           9, 10, 11, 12, 13, 14, 15, 16};
261
262
149k
    total_fill_bits = *tot_fill_bits;
263
264
    /* Distribute fill bits among all channel elements for next frame */
265
149k
    if (total_fill_bits > 0) {
266
      /* Generate array of vacancies in bit reservoirs */
267
229k
      for (i = 0, temp_resv = 0; i < num_bs_elements; i++, temp_resv++) {
268
153k
        bit_resv_spc[temp_resv] = (pstr_aac_enc[i]->qc_kernel.element_bits.max_bit_res_bits -
269
153k
                                   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
153k
        if (pstr_aac_enc[i]->qc_kernel.num_channels == 2) {
273
69.3k
          bit_resv_spc[temp_resv + 1] = bit_resv_spc[temp_resv] >> 1;
274
69.3k
          bit_resv_spc[temp_resv] -= bit_resv_spc[temp_resv + 1];
275
69.3k
          temp_resv++;
276
69.3k
        }
277
153k
      }
278
279
      /* Sort bit_resv_spc[] in descending order of levels and
280
      store the order in bit_resv_spc_sort[] */
281
223k
      for (i = (temp_resv - 1); i > 0; i--) {
282
500k
        for (j = 0; j < i; j++) {
283
353k
          if (bit_resv_spc[bit_resv_spc_sort[j]] < bit_resv_spc[bit_resv_spc_sort[j + 1]]) {
284
28.4k
            WORD32 tmp_var = bit_resv_spc_sort[j];
285
28.4k
            bit_resv_spc_sort[j] = bit_resv_spc_sort[j + 1];
286
28.4k
            bit_resv_spc_sort[j + 1] = tmp_var;
287
28.4k
          }
288
353k
        }
289
147k
      }
290
291
      /* One dummy full reservoir at the end to help in bit distribution */
292
76.0k
      bit_resv_spc[temp_resv] = 0;
293
76.0k
      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
285k
      for (i = 1; ((i < (temp_resv + 1)) && (total_fill_bits > 0)); i++) {
299
209k
        if (((bit_resv_spc[bit_resv_spc_sort[0]] - bit_resv_spc[bit_resv_spc_sort[i]]) * i) <=
300
209k
            total_fill_bits) {
301
204k
          total_fill_bits -=
302
204k
              ((bit_resv_spc[bit_resv_spc_sort[0]] - bit_resv_spc[bit_resv_spc_sort[i]]) * i);
303
707k
          for (j = 0; j < i; j++) {
304
502k
            bit_resv_spc[bit_resv_spc_sort[j]] = bit_resv_spc[bit_resv_spc_sort[i]];
305
502k
          }
306
204k
        } else {
307
4.63k
          WORD32 div_bs_ele;
308
309
4.63k
          div_bs_ele = (WORD32)(total_fill_bits / i);
310
4.63k
          total_fill_bits -= (div_bs_ele * i);
311
312
17.0k
          for (j = 0; j < i; j++) {
313
12.4k
            bit_resv_spc[bit_resv_spc_sort[j]] -= div_bs_ele;
314
12.4k
          }
315
316
8.61k
          for (j = 0; ((j < i) && (total_fill_bits > 0)); j++) {
317
3.97k
            bit_resv_spc[bit_resv_spc_sort[j]]--;
318
3.97k
            total_fill_bits--;
319
3.97k
          }
320
4.63k
        }
321
209k
      }
322
323
      /* Supply additional bits added for coding next frame */
324
229k
      for (i = 0, temp_resv = 0; i < num_bs_elements; i++, temp_resv++) {
325
153k
        WORD32 add_bits;
326
327
153k
        add_bits = (pstr_aac_enc[i]->qc_kernel.element_bits.max_bit_res_bits -
328
153k
                    pstr_aac_enc[i]->qc_kernel.element_bits.bit_res_level) -
329
153k
                   bit_resv_spc[temp_resv];
330
331
        /* Because CPE reservoir has been split into two */
332
153k
        if (pstr_aac_enc[i]->qc_kernel.num_channels == 2) {
333
69.3k
          temp_resv++;
334
69.3k
          add_bits -= bit_resv_spc[temp_resv];
335
69.3k
        }
336
337
        /* These will be in addition to the avg. bitrate for the next frame */
338
153k
        pstr_aac_enc[i]->qc_kernel.element_bits.carry_bits = add_bits;
339
153k
      }
340
341
      /* Update remaining fill bits */
342
76.0k
      *tot_fill_bits = total_fill_bits;
343
76.0k
    }
344
345
149k
    n_full_fill_elem = (total_fill_bits - 1) / max_fill_elem_bits;
346
347
149k
    if (n_full_fill_elem) {
348
42.7k
      total_fill_bits -= n_full_fill_elem * max_fill_elem_bits;
349
42.7k
    }
350
351
149k
    if (total_fill_bits > 0) {
352
      /* minimum Fillelement contains 7 (TAG + byte cnt) bits */
353
71.3k
      total_fill_bits = MAX(7, total_fill_bits);
354
355
      /* fill element size equals n*8 + 7 */
356
71.3k
      total_fill_bits += ((8 - (total_fill_bits - 7) % 8) % 8);
357
358
71.3k
      switch (total_fill_bits) {
359
1.33k
        case 7:
360
1.33k
          tfb_flag2 = 1;
361
1.33k
          break;
362
363
644
        case 15:
364
644
          tfb_flag1 = 1;
365
644
          break;
366
367
69.4k
        default:
368
69.4k
          tfb_flag = 1;
369
69.4k
          break;
370
71.3k
      }
371
71.3k
    }
372
373
149k
    total_fill_bits += n_full_fill_elem * max_fill_elem_bits;
374
375
149k
    pstr_qc_out->align_bits =
376
149k
        7 - (cnt_bits + pstr_qc_out->tot_dyn_bits_used + pstr_qc_out->tot_static_bits_used +
377
149k
             pstr_qc_out->tot_anc_bits_used + +total_fill_bits - 1) %
378
149k
                8;
379
149k
    if (((pstr_qc_out->align_bits + total_fill_bits - *tot_fill_bits) == 8) &&
380
41.2k
        (total_fill_bits > 8)) {
381
40.6k
      total_fill_bits -= 8;
382
40.6k
    }
383
384
149k
    diff_bits = (pstr_qc_out->align_bits + total_fill_bits) - *tot_fill_bits;
385
386
149k
    if (diff_bits) {
387
95.8k
      if (diff_bits < 0) {
388
0
        return IA_EXHEAACE_EXE_FATAL_INVALID_BIT_CONSUMPTION;
389
95.8k
      } else {
390
95.8k
        {
391
95.8k
          if (cnt_bits + pstr_qc_out->tot_static_bits_used + pstr_qc_out->tot_dyn_bits_used +
392
95.8k
                  pstr_qc_out->tot_anc_bits_used + total_fill_bits >
393
95.8k
              12288) {
394
16.3k
            if ((diff_bits > 8) && (total_fill_bits > 8)) {
395
6.50k
              if (tfb_flag || tfb_flag1) {
396
6.49k
                total_fill_bits -= 8;
397
6.49k
              }
398
6.50k
              if (tfb_flag2) {
399
4
                total_fill_bits -= 7;
400
4
              }
401
6.50k
            }
402
79.5k
          } else {
403
79.5k
            if (pstr_qc_kernel->element_bits.bit_res_level - diff_bits > 0) {
404
73.7k
              pstr_qc_kernel->element_bits.bit_res_level -= diff_bits;
405
73.7k
              pstr_qc_kernel->bit_res_tot = pstr_qc_kernel->element_bits.bit_res_level;
406
73.7k
            } else {
407
5.83k
              if ((diff_bits > 8) && (total_fill_bits > 8) && (tfb_flag)) {
408
2.83k
                total_fill_bits -= 8;
409
3.00k
              } else if ((diff_bits > 8) && (total_fill_bits > 8) && (tfb_flag1)) {
410
139
                total_fill_bits -= 8;
411
2.86k
              } else if ((diff_bits > 8) && (total_fill_bits > 8) && (tfb_flag2)) {
412
131
                total_fill_bits -= 7;
413
131
              }
414
5.83k
            }
415
79.5k
          }
416
95.8k
        }
417
95.8k
      }
418
95.8k
    }
419
149k
    switch (aot) {
420
27.6k
      case AOT_AAC_LC:
421
64.6k
      case AOT_SBR:
422
89.4k
      case AOT_PS:
423
89.4k
        *tot_fill_bits = total_fill_bits;
424
89.4k
        break;
425
426
9.84k
      case AOT_AAC_LD:
427
59.8k
      case AOT_AAC_ELD:
428
59.8k
        pstr_qc_out->total_fill_bits = total_fill_bits;
429
59.8k
        *tot_fill_bits = 0;
430
59.8k
        break;
431
149k
    }
432
149k
  }  // if flag_last_element
433
156k
  else {
434
156k
    pstr_qc_out->align_bits = 0;
435
156k
  }
436
437
306k
  if ((pstr_qc_out->tot_dyn_bits_used + pstr_qc_out->tot_static_bits_used +
438
306k
       pstr_qc_out->tot_anc_bits_used + pstr_qc_out->total_fill_bits + pstr_qc_out->align_bits) >
439
306k
      pstr_qc_kernel->max_bits_tot) {
440
30.1k
  }
441
442
306k
  return IA_NO_ERROR;
443
306k
}
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
306k
{
450
306k
  WORD32 padding_on;
451
306k
  WORD32 frame_len;
452
306k
  WORD32 code_bits;
453
306k
  WORD32 code_bits_last;
454
455
306k
  padding_on = ia_enhaacplus_enc_frame_padding(
456
306k
      bit_rate, sample_rate, &pstr_qc_state->padding.padding_rest, frame_len_long);
457
458
306k
  frame_len = padding_on + ia_enhaacplus_enc_calc_frame_len(bit_rate, sample_rate,
459
306k
                                                            FRAME_LEN_BYTES_INT, frame_len_long);
460
461
306k
  frame_len <<= 3;
462
463
306k
  if (flag_last_element) {
464
149k
    code_bits_last = pstr_qc_state->average_bits_tot - pstr_qc_state->glob_stat_bits;
465
466
149k
    code_bits = frame_len - pstr_qc_state->glob_stat_bits;
467
156k
  } else {
468
156k
    code_bits_last = pstr_qc_state->average_bits_tot;
469
470
156k
    code_bits = frame_len;
471
156k
  }
472
473
  /* calculate bits for every channel element */
474
306k
  if (code_bits != code_bits_last) {
475
89.0k
    WORD32 total_bits = 0;
476
477
89.0k
    pstr_qc_state->element_bits.average_bits =
478
89.0k
        (WORD32)(pstr_qc_state->element_bits.relative_bits * code_bits);
479
480
89.0k
    total_bits += pstr_qc_state->element_bits.average_bits;
481
482
89.0k
    pstr_qc_state->element_bits.average_bits += code_bits - total_bits;
483
89.0k
  }
484
485
306k
  pstr_qc_state->average_bits_tot = frame_len;
486
487
  /* Bits carried over from previous frame due to distribution of fill bits */
488
306k
  pstr_qc_state->element_bits.average_bits += pstr_qc_state->element_bits.carry_bits;
489
306k
  pstr_qc_state->average_bits_tot += pstr_qc_state->element_bits.carry_bits;
490
491
  /* Flush for current frame */
492
306k
  pstr_qc_state->element_bits.carry_bits = 0;
493
306k
}
494
495
WORD32 ia_enhaacplus_aac_limitbitrate(WORD32 core_sampling_rate, WORD32 frame_length,
496
4.99k
                                      WORD32 num_channels, WORD32 bit_rate) {
497
4.99k
  WORD32 prev_bit_rate, shift = 0, iter = 0;
498
4.99k
  WORD32 max_ch_bits = MAXIMUM_CHANNEL_BITS_1024;
499
500
28.3k
  while ((frame_length & ~((1 << (shift + 1)) - 1)) == frame_length &&
501
27.0k
         (core_sampling_rate & ~((1 << (shift + 1)) - 1)) == core_sampling_rate) {
502
23.3k
    shift++;
503
23.3k
  }
504
505
4.99k
  max_ch_bits = MAXIMUM_CHANNEL_BITS_1024 * frame_length / MAX_FRAME_LEN;
506
507
6.71k
  do {
508
6.71k
    prev_bit_rate = bit_rate;
509
510
6.71k
    bit_rate = MAX(bit_rate, ((((40 * num_channels) + TRANSPORT_BITS) * (core_sampling_rate)) /
511
6.71k
                              frame_length));
512
6.71k
    bit_rate = MIN(bit_rate, ((num_channels * max_ch_bits) * (core_sampling_rate >> shift)) /
513
6.71k
                                 (frame_length >> shift));
514
515
6.71k
  } while (prev_bit_rate != bit_rate && iter++ < 3);
516
517
4.99k
  return bit_rate;
518
4.99k
}