Coverage Report

Created: 2026-12-01 07:00

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