Coverage Report

Created: 2024-06-21 06:45

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