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