xref: /aosp_15_r20/external/libxaac/encoder/ixheaace_sbr_write_bitstream.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 <math.h>
22 #include <stdlib.h>
23 #include "ixheaac_type_def.h"
24 #include "ixheaac_constants.h"
25 #include "ixheaace_aac_constants.h"
26 #include "ixheaac_basic_ops32.h"
27 #include "ixheaac_basic_ops16.h"
28 #include "ixheaac_basic_ops40.h"
29 
30 #include "ixheaace_sbr_header.h"
31 #include "ixheaace_sbr_def.h"
32 #include "ixheaace_resampler.h"
33 #include "ixheaace_sbr_rom.h"
34 #include "ixheaace_common_rom.h"
35 #include "ixheaace_bitbuffer.h"
36 #include "ixheaace_sbr_hbe.h"
37 #include "ixheaace_sbr_qmf_enc.h"
38 #include "ixheaace_sbr_tran_det.h"
39 #include "ixheaace_sbr_frame_info_gen.h"
40 #include "ixheaace_sbr_env_est.h"
41 #include "ixheaace_sbr_code_envelope.h"
42 #include "ixheaace_sbr_main.h"
43 #include "ixheaace_sbr_missing_harmonics_det.h"
44 #include "ixheaace_sbr_inv_filtering_estimation.h"
45 #include "ixheaace_sbr_noise_floor_est.h"
46 
47 #include "ixheaace_sbr_ton_corr.h"
48 #include "iusace_esbr_pvc.h"
49 #include "iusace_esbr_inter_tes.h"
50 #include "ixheaace_sbr.h"
51 #include "ixheaace_sbr_cmondata.h"
52 #include "iusace_esbr_pvc.h"
53 
54 #include "ixheaace_sbr_hybrid.h"
55 #include "ixheaace_sbr_ps_enc.h"
56 #include "ixheaace_sbr_ps_bitenc.h"
57 #include "ixheaace_sbr_write_bitstream.h"
58 #include "ixheaac_error_standards.h"
59 #include "ixheaace_error_codes.h"
60 #include "ixheaace_common_utils.h"
61 
ixheaace_get_esbr_ext_data_size(ixheaace_str_esbr_bs_data * pstr_esbr_bs_data)62 static WORD32 ixheaace_get_esbr_ext_data_size(ixheaace_str_esbr_bs_data *pstr_esbr_bs_data) {
63   WORD32 num_bits = 1;
64   if (1 == pstr_esbr_bs_data->sbr_num_chan) {
65     num_bits += 1;
66     if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
67       num_bits += 2;
68       if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
69         num_bits += 7;
70       }
71     }
72   } else if (2 == pstr_esbr_bs_data->sbr_num_chan) {
73     if (pstr_esbr_bs_data->sbr_coupling) {
74       num_bits += 1;
75       if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
76         num_bits += 2;
77         if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
78           num_bits += 7;
79         }
80       }
81     } else {
82       num_bits += 1;
83       if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
84         num_bits += 2;
85         if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
86           num_bits += 7;
87         }
88       }
89       num_bits += 1;
90       if (pstr_esbr_bs_data->sbr_patching_mode[1] == 0) {
91         num_bits += 2;
92         if (pstr_esbr_bs_data->sbr_pitchin_flags[1] == 1) {
93           num_bits += 7;
94         }
95       }
96     }
97   } else {
98     num_bits = 0;
99   }
100   if (num_bits != 0 && num_bits < 6) {
101     num_bits = 6;
102   }
103   return num_bits;
104 }
iusace_encode_pvc_envelope(ixheaace_bit_buf_handle pstr_bs_handle,ixheaace_pvc_bs_info * pstr_pvc_bs_data,WORD32 usac_indep_flag)105 static WORD32 iusace_encode_pvc_envelope(ixheaace_bit_buf_handle pstr_bs_handle,
106                                          ixheaace_pvc_bs_info *pstr_pvc_bs_data,
107                                          WORD32 usac_indep_flag) {
108   WORD32 payload_cnt_bits = 0;
109   payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->div_mode,
110                                           IXHEAACE_ESBR_PVC_DIV_MODE_BITS);
111   payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->ns_mode,
112                                           IXHEAACE_ESBR_PVC_NS_MODE_BITS);
113 
114   if (0 == pstr_pvc_bs_data->div_mode) {
115     if (1 == usac_indep_flag) {
116       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->pvc_id_bs[0],
117                                               IXHEAACE_ESBR_PVC_ID_BITS);
118     } else {
119       if (1 == pstr_pvc_bs_data->grid_info[0]) {
120         payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, IXHEAACE_ESBR_PVC_REUSE_BITS);
121         payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->pvc_id_bs[0],
122                                                 IXHEAACE_ESBR_PVC_ID_BITS);
123       } else {
124         payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 1, IXHEAACE_ESBR_PVC_REUSE_BITS);
125       }
126     }
127   } else if (pstr_pvc_bs_data->div_mode <= 3) {
128     /* Do nothing */
129   } else {
130     WORD32 gi, is_grid_info;
131     for (gi = 0; gi < pstr_pvc_bs_data->num_grid_info; gi++) {
132       if (gi == 0 && 1 == usac_indep_flag) {
133         is_grid_info = 1;
134       } else {
135         is_grid_info = pstr_pvc_bs_data->grid_info[gi];
136         payload_cnt_bits +=
137             ixheaace_write_bits(pstr_bs_handle, is_grid_info, IXHEAACE_ESBR_PVC_GRID_INFO_BITS);
138       }
139       if (is_grid_info) {
140         payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->pvc_id_bs[gi],
141                                                 IXHEAACE_ESBR_PVC_ID_BITS);
142       }
143     }
144   }
145   return payload_cnt_bits;
146 }
ia_enhaacplus_enc_ceil_ln2(WORD32 x)147 static WORD32 ia_enhaacplus_enc_ceil_ln2(WORD32 x) {
148   WORD32 tmp = -1;
149 
150   while (ixheaac_shl32(1, ++tmp) < x)
151     ;
152 
153   return (tmp);
154 }
155 
ixheaace_encode_sbr_grid(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,ixheaace_bit_buf_handle pstr_bs_handle,ixheaace_sbr_codec_type sbr_codec)156 static WORD32 ixheaace_encode_sbr_grid(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,
157                                        ixheaace_bit_buf_handle pstr_bs_handle,
158                                        ixheaace_sbr_codec_type sbr_codec) {
159   WORD32 payload_cnt_bits = 0;
160   WORD32 i, tmp_var;
161 
162   if (ELD_SBR != sbr_codec) {
163     if (HEAAC_SBR == sbr_codec ||
164         (USAC_SBR == sbr_codec && pstr_sbr_env_info->sbr_pvc_mode == 0)) {
165       payload_cnt_bits += ixheaace_write_bits(
166           pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type, SBR_CLA_BITS);
167 
168       switch (pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type) {
169         case IXHEAACE_FIXFIX:
170 
171           tmp_var = ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env);
172 
173           payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ENV_BITS);
174 
175           payload_cnt_bits +=
176               ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->freq_res_fix, SBR_RES_BITS);
177           break;
178 
179         case IXHEAACE_FIXVAR:
180         case IXHEAACE_VARFIX:
181 
182           if (pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type == IXHEAACE_FIXVAR) {
183             tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord - 16;
184           } else {
185             tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord;
186           }
187 
188           payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ABS_BITS);
189 
190           payload_cnt_bits += ixheaace_write_bits(
191               pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->n, SBR_NUM_BITS);
192 
193           for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->n; i++) {
194             tmp_var = (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_rel_bord[i] - 2) >> 1;
195 
196             payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_REL_BITS);
197           }
198 
199           tmp_var = ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->n + 2);
200 
201           payload_cnt_bits += ixheaace_write_bits(
202               pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->p, (UWORD8)tmp_var);
203 
204           for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->n + 1; i++) {
205             payload_cnt_bits += ixheaace_write_bits(
206                 pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f[i], SBR_RES_BITS);
207           }
208           break;
209 
210         case IXHEAACE_VARVAR:
211 
212           tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord_0;
213 
214           payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ABS_BITS);
215           tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord_1 - 16;
216 
217           payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ABS_BITS);
218 
219           payload_cnt_bits += ixheaace_write_bits(
220               pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0, SBR_NUM_BITS);
221 
222           payload_cnt_bits += ixheaace_write_bits(
223               pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1, SBR_NUM_BITS);
224 
225           for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0; i++) {
226             tmp_var = (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_rel_bord_0[i] - 2) >> 1;
227 
228             payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_REL_BITS);
229           }
230 
231           for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1; i++) {
232             tmp_var = (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_rel_bord_1[i] - 2) >> 1;
233 
234             payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_REL_BITS);
235           }
236 
237           tmp_var =
238               ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0 +
239                                          pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1 + 2);
240 
241           payload_cnt_bits += ixheaace_write_bits(
242               pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->p, (UWORD8)tmp_var);
243 
244           tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0 +
245                     pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1 + 1;
246 
247           for (i = 0; i < tmp_var; i++) {
248             payload_cnt_bits += ixheaace_write_bits(
249                 pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f_lr[i], SBR_RES_BITS);
250           }
251           break;
252         default:
253           break;
254       }
255     } else {
256       // If PVC mode is non-zero, bit stream parameters are updated here
257       if (pstr_sbr_env_info->no_of_envelopes > 1) {
258         payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 8, SBR_PVC_NOISE_POSITION_BITS);
259       } else {
260         payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SBR_PVC_NOISE_POSITION_BITS);
261       }
262       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SBR_PVC_VAR_LEN_HF_BITS);
263     }
264   } else {
265     payload_cnt_bits += ixheaace_write_bits(
266         pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type, LDSBR_CLA_BITS);
267 
268     switch (pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type) {
269       case IXHEAACE_FIXFIX:
270         tmp_var = ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env);
271         payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ENV_BITS);
272         if (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env == 1) {
273           payload_cnt_bits += ixheaace_write_bits(
274               pstr_bs_handle, pstr_sbr_env_info->curr_sbr_amp_res, SI_SBR_AMP_RES_BITS);
275         }
276         payload_cnt_bits += ixheaace_write_bits(
277             pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f[0], SBR_RES_BITS);
278         break;
279 
280       case IXHEAACE_LD_TRAN:
281         payload_cnt_bits += ixheaace_write_bits(
282             pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->bs_transient_position,
283             IXHEAACE_SBR_TRAN_BITS);
284         for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env; i++) {
285           payload_cnt_bits += ixheaace_write_bits(
286               pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f[i], SBR_RES_BITS);
287         }
288         break;
289       default:
290         break;
291     }
292   }
293 
294   return payload_cnt_bits;
295 }
296 
ixheaace_encode_sbr_dtdf(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,ixheaace_bit_buf_handle pstr_bs_handle,ixheaace_sbr_codec_type sbr_codec,WORD32 usac_indep_flag,WORD32 sbr_pvc_mode)297 static WORD32 ixheaace_encode_sbr_dtdf(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,
298                                        ixheaace_bit_buf_handle pstr_bs_handle,
299                                        ixheaace_sbr_codec_type sbr_codec, WORD32 usac_indep_flag,
300                                        WORD32 sbr_pvc_mode) {
301   WORD32 i, payload_cnt_bits = 0, num_of_noise_env;
302 
303   num_of_noise_env = (pstr_sbr_env_info->no_of_envelopes > 1) ? 2 : 1;
304 
305   if (USAC_SBR != sbr_codec) {
306     for (i = 0; i < pstr_sbr_env_info->no_of_envelopes; ++i) {
307       payload_cnt_bits +=
308           ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->domain_vec[i], SBR_DIR_BITS);
309     }
310   }
311 
312   else {
313     if (sbr_pvc_mode == 0) {
314       WORD32 start_env = 0;
315       if (1 == usac_indep_flag) {
316         start_env = 1;
317       }
318       for (i = start_env; i < pstr_sbr_env_info->no_of_envelopes; ++i) {
319         payload_cnt_bits +=
320             ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->domain_vec[i], SBR_DIR_BITS);
321       }
322     } else {
323       /* Do nothing */
324     }
325   }
326   if (USAC_SBR != sbr_codec) {
327     for (i = 0; i < num_of_noise_env; ++i) {
328       payload_cnt_bits += ixheaace_write_bits(
329           pstr_bs_handle, pstr_sbr_env_info->domain_vec_noise[i], SBR_DIR_BITS);
330     }
331   } else {
332     WORD32 start_env = 0;
333     if (1 == usac_indep_flag) {
334       start_env = 1;
335     }
336 
337     for (i = start_env; i < num_of_noise_env; ++i) {
338       payload_cnt_bits += ixheaace_write_bits(
339           pstr_bs_handle, pstr_sbr_env_info->domain_vec_noise[i], SBR_DIR_BITS);
340     }
341   }
342   return payload_cnt_bits;
343 }
344 
ixheaace_write_noise_lvl_data(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,ixheaace_bit_buf_handle pstr_bs_handle,WORD32 coupling)345 static WORD32 ixheaace_write_noise_lvl_data(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,
346                                             ixheaace_bit_buf_handle pstr_bs_handle,
347                                             WORD32 coupling) {
348   WORD32 j, i, payload_cnt_bits = 0;
349   WORD32 n_noise_envelopes = ((pstr_sbr_env_info->no_of_envelopes > 1) ? 2 : 1);
350 
351   for (i = 0; i < n_noise_envelopes; i++) {
352     switch (pstr_sbr_env_info->domain_vec_noise[i]) {
353       case FREQ:
354 
355         if (coupling && pstr_sbr_env_info->balance) {
356           payload_cnt_bits += ixheaace_write_bits(
357               pstr_bs_handle,
358               pstr_sbr_env_info->noise_level[i * pstr_sbr_env_info->noise_band_count],
359               (UWORD8)pstr_sbr_env_info->si_sbr_start_noise_bits_balance);
360         } else {
361           payload_cnt_bits += ixheaace_write_bits(
362               pstr_bs_handle,
363               pstr_sbr_env_info->noise_level[i * pstr_sbr_env_info->noise_band_count],
364               (UWORD8)pstr_sbr_env_info->si_sbr_start_noise_bits);
365         }
366 
367         for (j = 1 + i * pstr_sbr_env_info->noise_band_count;
368              j < (pstr_sbr_env_info->noise_band_count * (1 + i)); j++) {
369           if (coupling) {
370             if (pstr_sbr_env_info->balance) {
371               payload_cnt_bits += ixheaace_write_bits(
372                   pstr_bs_handle,
373                   pstr_sbr_env_info
374                       ->ptr_huff_tab_noise_bal_freq_c[pstr_sbr_env_info->noise_level[j] +
375                                                       CODE_BCK_SCF_LAV_BALANCE11],
376                   pstr_sbr_env_info
377                       ->ptr_huff_tab_noise_bal_freq_l[pstr_sbr_env_info->noise_level[j] +
378                                                       CODE_BCK_SCF_LAV_BALANCE11]);
379             } else {
380               payload_cnt_bits += ixheaace_write_bits(
381                   pstr_bs_handle,
382                   pstr_sbr_env_info
383                       ->ptr_huff_tab_noise_lvl_freq_c[pstr_sbr_env_info->noise_level[j] +
384                                                       CODE_BCK_SCF_LAV11],
385                   pstr_sbr_env_info
386                       ->ptr_huff_tab_noise_lvl_freq_l[pstr_sbr_env_info->noise_level[j] +
387                                                       CODE_BCK_SCF_LAV11]);
388             }
389           } else {
390             payload_cnt_bits += ixheaace_write_bits(
391                 pstr_bs_handle,
392                 pstr_sbr_env_info->ptr_huff_tab_noise_freq_c[pstr_sbr_env_info->noise_level[j] +
393                                                              CODE_BCK_SCF_LAV11],
394                 pstr_sbr_env_info->ptr_huff_tab_noise_freq_l[pstr_sbr_env_info->noise_level[j] +
395                                                              CODE_BCK_SCF_LAV11]);
396           }
397         }
398         break;
399 
400       case TIME:
401         for (j = i * pstr_sbr_env_info->noise_band_count;
402              j < (pstr_sbr_env_info->noise_band_count * (1 + i)); j++) {
403           if (coupling) {
404             if (pstr_sbr_env_info->balance) {
405               payload_cnt_bits += ixheaace_write_bits(
406                   pstr_bs_handle,
407                   pstr_sbr_env_info
408                       ->ptr_huff_tab_noise_bal_time_c[pstr_sbr_env_info->noise_level[j] +
409                                                       CODE_BCK_SCF_LAV_BALANCE11],
410                   pstr_sbr_env_info
411                       ->ptr_huff_tab_noise_bal_time_l[pstr_sbr_env_info->noise_level[j] +
412                                                       CODE_BCK_SCF_LAV_BALANCE11]);
413             } else {
414               payload_cnt_bits += ixheaace_write_bits(
415                   pstr_bs_handle,
416                   pstr_sbr_env_info
417                       ->ptr_huff_tab_noise_lvl_time_c[pstr_sbr_env_info->noise_level[j] +
418                                                       CODE_BCK_SCF_LAV11],
419                   pstr_sbr_env_info
420                       ->ptr_huff_tab_noise_lvl_time_l[pstr_sbr_env_info->noise_level[j] +
421                                                       CODE_BCK_SCF_LAV11]);
422             }
423           } else {
424             payload_cnt_bits += ixheaace_write_bits(
425                 pstr_bs_handle,
426                 pstr_sbr_env_info
427                     ->ptr_huff_tab_noise_lvl_time_c[pstr_sbr_env_info->noise_level[j] +
428                                                     CODE_BCK_SCF_LAV11],
429                 pstr_sbr_env_info
430                     ->ptr_huff_tab_noise_lvl_time_l[pstr_sbr_env_info->noise_level[j] +
431                                                     CODE_BCK_SCF_LAV11]);
432           }
433         }
434         break;
435     }
436   }
437   return payload_cnt_bits;
438 }
439 
ixheaace_write_env_data(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,ixheaace_bit_buf_handle pstr_bs_handle,WORD32 coupling,ixheaace_sbr_codec_type sbr_codec,WORD32 * ptr_payload_cnt_bits)440 static IA_ERRORCODE ixheaace_write_env_data(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,
441                                             ixheaace_bit_buf_handle pstr_bs_handle,
442                                             WORD32 coupling, ixheaace_sbr_codec_type sbr_codec,
443                                             WORD32 *ptr_payload_cnt_bits) {
444   WORD32 j, i, delta;
445 
446   *ptr_payload_cnt_bits = 0;
447 
448   for (j = 0; j < pstr_sbr_env_info->no_of_envelopes; j++) {
449     if (pstr_sbr_env_info->domain_vec[j] == FREQ) {
450       if (coupling && pstr_sbr_env_info->balance) {
451         *ptr_payload_cnt_bits +=
452             ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->ienvelope[j][0],
453                                 (UWORD8)pstr_sbr_env_info->si_sbr_start_env_bits_balance);
454       } else {
455         *ptr_payload_cnt_bits +=
456             ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->ienvelope[j][0],
457                                 (UWORD8)pstr_sbr_env_info->si_sbr_start_env_bits);
458       }
459     }
460 
461     for (i = 1 - pstr_sbr_env_info->domain_vec[j]; i < pstr_sbr_env_info->no_scf_bands[j]; i++) {
462       delta = pstr_sbr_env_info->ienvelope[j][i];
463 
464       if (coupling && pstr_sbr_env_info->balance) {
465         if (abs(delta) > pstr_sbr_env_info->code_book_scf_lav_balance) {
466           return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_CODEBOOK;
467         }
468       } else {
469         if (abs(delta) > pstr_sbr_env_info->code_book_scf_lav) {
470           return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_CODEBOOK;
471         }
472       }
473 
474       if (coupling) {
475         if (pstr_sbr_env_info->balance) {
476           if (pstr_sbr_env_info->domain_vec[j]) {
477             *ptr_payload_cnt_bits += ixheaace_write_bits(
478                 pstr_bs_handle,
479                 pstr_sbr_env_info
480                     ->ptr_huff_tab_bal_time_c[delta +
481                                               pstr_sbr_env_info->code_book_scf_lav_balance],
482                 pstr_sbr_env_info
483                     ->ptr_huff_tab_bal_time_l[delta +
484                                               pstr_sbr_env_info->code_book_scf_lav_balance]);
485           } else {
486             *ptr_payload_cnt_bits += ixheaace_write_bits(
487                 pstr_bs_handle,
488                 pstr_sbr_env_info
489                     ->ptr_huff_tab_bal_freq_c[delta +
490                                               pstr_sbr_env_info->code_book_scf_lav_balance],
491                 pstr_sbr_env_info
492                     ->ptr_huff_tab_bal_freq_l[delta +
493                                               pstr_sbr_env_info->code_book_scf_lav_balance]);
494           }
495         } else {
496           if (pstr_sbr_env_info->domain_vec[j]) {
497             *ptr_payload_cnt_bits += ixheaace_write_bits(
498                 pstr_bs_handle,
499                 pstr_sbr_env_info
500                     ->ptr_huff_tab_lvl_time_c[delta + pstr_sbr_env_info->code_book_scf_lav],
501                 pstr_sbr_env_info
502                     ->ptr_huff_tab_lvl_time_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
503           } else {
504             *ptr_payload_cnt_bits += ixheaace_write_bits(
505                 pstr_bs_handle,
506                 pstr_sbr_env_info
507                     ->ptr_huff_tab_lvl_freq_c[delta + pstr_sbr_env_info->code_book_scf_lav],
508                 pstr_sbr_env_info
509                     ->ptr_huff_tab_lvl_freq_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
510           }
511         }
512       } else {
513         if (pstr_sbr_env_info->domain_vec[j]) {
514           *ptr_payload_cnt_bits += ixheaace_write_bits(
515               pstr_bs_handle,
516               pstr_sbr_env_info
517                   ->ptr_huff_tab_time_c[delta + pstr_sbr_env_info->code_book_scf_lav],
518               pstr_sbr_env_info
519                   ->ptr_huff_tab_time_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
520         } else {
521           *ptr_payload_cnt_bits += ixheaace_write_bits(
522               pstr_bs_handle,
523               pstr_sbr_env_info
524                   ->ptr_huff_tab_freq_c[delta + pstr_sbr_env_info->code_book_scf_lav],
525               pstr_sbr_env_info
526                   ->ptr_huff_tab_freq_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
527         }
528       }
529     }
530     if (USAC_SBR == sbr_codec) {
531       if (1 == pstr_sbr_env_info->sbr_inter_tes) {
532         *ptr_payload_cnt_bits +=
533             ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->ptr_sbr_inter_tes_shape[j],
534                                 IXHEAACE_SBR_TES_SHAPE_BITS);
535         if (1 == pstr_sbr_env_info->ptr_sbr_inter_tes_shape[j]) {
536           *ptr_payload_cnt_bits += ixheaace_write_bits(
537               pstr_bs_handle, pstr_sbr_env_info->ptr_sbr_inter_tes_shape_mode[j],
538               IXHEAACE_SBR_TES_SHAPE_MODE_BITS);
539         }
540       }
541     }
542   }
543 
544   return IA_NO_ERROR;
545 }
546 
ixheaace_write_synthetic_coding_data(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,ixheaace_bit_buf_handle pstr_bs_handle,ixheaace_sbr_codec_type sbr_codec,WORD32 sbr_pvc_mode)547 static WORD32 ixheaace_write_synthetic_coding_data(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,
548                                                    ixheaace_bit_buf_handle pstr_bs_handle,
549                                                    ixheaace_sbr_codec_type sbr_codec,
550                                                    WORD32 sbr_pvc_mode)
551 
552 {
553   WORD32 i;
554   WORD32 payload_cnt_bits = 0;
555 
556   payload_cnt_bits +=
557       ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->add_harmonic_flag, 1);
558 
559   if (pstr_sbr_env_info->add_harmonic_flag) {
560     for (i = 0; i < pstr_sbr_env_info->no_harmonics; i++) {
561       payload_cnt_bits +=
562           ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->add_harmonic[i], 1);
563     }
564     if (USAC_SBR == sbr_codec && 0 != sbr_pvc_mode) {
565       if (pstr_sbr_env_info->sbr_sinusoidal_pos_flag) {
566         payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 1, 1);
567         payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 31, 5);
568       } else {
569         payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, 1);
570       }
571     }
572   }
573   return payload_cnt_bits;
574 }
575 
ixheaace_encode_sbr_single_channel_element(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,ixheaace_bit_buf_handle pstr_bs_handle,ixheaace_sbr_codec_type sbr_codec,WORD32 * ptr_num_bits)576 static IA_ERRORCODE ixheaace_encode_sbr_single_channel_element(
577     ixheaace_pstr_sbr_env_data pstr_sbr_env_info, ixheaace_bit_buf_handle pstr_bs_handle,
578     ixheaace_sbr_codec_type sbr_codec, WORD32 *ptr_num_bits)
579 
580 {
581   WORD32 payload_cnt_bits = 0;
582   IA_ERRORCODE err_code = IA_NO_ERROR;
583 
584   if (sbr_codec != USAC_SBR) {
585     payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, 1);
586   } else {
587     if (pstr_sbr_env_info->harmonic_sbr) {
588       // USAC Harmonic SBR data
589       payload_cnt_bits +=
590           ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_patching_mode, 1);
591       if (0 == pstr_sbr_env_info->sbr_patching_mode) {
592         payload_cnt_bits +=
593             ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_oversampling_flag, 1);
594         payload_cnt_bits +=
595             ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_pitchin_bins_flag, 1);
596         if (0 != pstr_sbr_env_info->sbr_pitchin_bins_flag) {
597           payload_cnt_bits +=
598               ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_pitchin_bins, 7);
599         }
600       }
601     }
602   }
603   payload_cnt_bits += ixheaace_encode_sbr_grid(pstr_sbr_env_info, pstr_bs_handle, sbr_codec);
604   if (sbr_codec == USAC_SBR) {
605     payload_cnt_bits += ixheaace_encode_sbr_dtdf(pstr_sbr_env_info, pstr_bs_handle, sbr_codec,
606                                                  pstr_sbr_env_info->usac_indep_flag,
607                                                  pstr_sbr_env_info->sbr_pvc_mode);
608   } else {
609     payload_cnt_bits +=
610         ixheaace_encode_sbr_dtdf(pstr_sbr_env_info, pstr_bs_handle, sbr_codec, 0, 0);
611   }
612 
613   {
614     WORD32 i;
615     for (i = 0; i < pstr_sbr_env_info->noise_band_count; i++) {
616       payload_cnt_bits += ixheaace_write_bits(
617           pstr_bs_handle, pstr_sbr_env_info->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
618     }
619   }
620   if (sbr_codec != USAC_SBR) {
621     WORD32 env_data_len;
622     err_code =
623         ixheaace_write_env_data(pstr_sbr_env_info, pstr_bs_handle, 0, sbr_codec, &env_data_len);
624     if (err_code) {
625       *ptr_num_bits = payload_cnt_bits;
626       return err_code;
627     }
628     payload_cnt_bits += env_data_len;
629   } else {
630     if (0 == pstr_sbr_env_info->sbr_pvc_mode) {
631       WORD32 env_data_len;
632       err_code =
633           ixheaace_write_env_data(pstr_sbr_env_info, pstr_bs_handle, 0, sbr_codec, &env_data_len);
634       if (err_code) {
635         *ptr_num_bits = payload_cnt_bits;
636         return err_code;
637       }
638       payload_cnt_bits += env_data_len;
639     } else {
640       // PVC envelope goes here
641       payload_cnt_bits += iusace_encode_pvc_envelope(pstr_bs_handle, &pstr_sbr_env_info->pvc_info,
642                                                      pstr_sbr_env_info->usac_indep_flag);
643     }
644   }
645   payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_info, pstr_bs_handle, 0);
646 
647   if (USAC_SBR == sbr_codec) {
648     payload_cnt_bits += ixheaace_write_synthetic_coding_data(
649         pstr_sbr_env_info, pstr_bs_handle, sbr_codec, pstr_sbr_env_info->sbr_pvc_mode);
650   } else {
651     payload_cnt_bits +=
652         ixheaace_write_synthetic_coding_data(pstr_sbr_env_info, pstr_bs_handle, sbr_codec, 0);
653   }
654 
655   *ptr_num_bits = payload_cnt_bits;
656   return err_code;
657 }
658 
ixheaace_encode_sbr_channel_pair_element(ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right,ixheaace_bit_buf_handle pstr_bs_handle,WORD32 coupling,ixheaace_sbr_codec_type sbr_codec,WORD32 * ptr_num_bits)659 static IA_ERRORCODE ixheaace_encode_sbr_channel_pair_element(
660     ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,
661     ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right, ixheaace_bit_buf_handle pstr_bs_handle,
662     WORD32 coupling, ixheaace_sbr_codec_type sbr_codec, WORD32 *ptr_num_bits) {
663   IA_ERRORCODE err_code = IA_NO_ERROR;
664   WORD32 payload_cnt_bits = 0;
665   WORD32 env_data_len;
666   WORD32 i = 0;
667 
668   if (USAC_SBR != sbr_codec) {
669     payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, 1); /* no reserved bits */
670   }
671 
672   payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, coupling, SI_SBR_COUPLING_BITS);
673 
674   if (coupling) {
675     if (sbr_codec == USAC_SBR && pstr_sbr_env_data_left->harmonic_sbr) {
676       // USAC Harmonic SBR data
677       payload_cnt_bits +=
678           ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_patching_mode, 1);
679       if (0 == pstr_sbr_env_data_left->sbr_patching_mode) {
680         payload_cnt_bits +=
681             ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_oversampling_flag, 1);
682         payload_cnt_bits +=
683             ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins_flag, 1);
684         if (0 != pstr_sbr_env_data_left->sbr_pitchin_bins_flag) {
685           payload_cnt_bits +=
686               ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins, 7);
687         }
688       }
689     }
690     payload_cnt_bits +=
691         ixheaace_encode_sbr_grid(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec);
692 
693     payload_cnt_bits +=
694         ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec,
695                                  pstr_sbr_env_data_left->usac_indep_flag, 0);
696 
697     payload_cnt_bits +=
698         ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_right, pstr_bs_handle, sbr_codec,
699                                  pstr_sbr_env_data_left->usac_indep_flag, 0);
700 
701     for (i = 0; i < pstr_sbr_env_data_left->noise_band_count; i++) {
702       payload_cnt_bits += ixheaace_write_bits(
703           pstr_bs_handle, pstr_sbr_env_data_left->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
704     }
705 
706     err_code = ixheaace_write_env_data(pstr_sbr_env_data_left, pstr_bs_handle, 1, sbr_codec,
707                                        &env_data_len);
708     if (err_code) {
709       *ptr_num_bits = payload_cnt_bits;
710       return err_code;
711     }
712 
713     payload_cnt_bits += env_data_len;
714 
715     payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_left, pstr_bs_handle, 1);
716 
717     err_code = ixheaace_write_env_data(pstr_sbr_env_data_right, pstr_bs_handle, 1, sbr_codec,
718                                        &env_data_len);
719     if (err_code) {
720       *ptr_num_bits = payload_cnt_bits;
721       return err_code;
722     }
723     payload_cnt_bits += env_data_len;
724 
725     payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_right, pstr_bs_handle, 1);
726 
727     payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_left,
728                                                              pstr_bs_handle, sbr_codec, 0);
729 
730     payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_right,
731                                                              pstr_bs_handle, sbr_codec, 0);
732   } else {
733     if (sbr_codec == USAC_SBR && pstr_sbr_env_data_left->harmonic_sbr) {
734       // USAC Harmonic SBR data
735       payload_cnt_bits +=
736           ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_patching_mode, 1);
737       if (0 == pstr_sbr_env_data_left->sbr_patching_mode) {
738         payload_cnt_bits +=
739             ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_oversampling_flag, 1);
740         payload_cnt_bits +=
741             ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins_flag, 1);
742         if (0 != pstr_sbr_env_data_left->sbr_pitchin_bins_flag) {
743           payload_cnt_bits +=
744               ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins, 7);
745         }
746       }
747 
748       payload_cnt_bits +=
749           ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_right->sbr_patching_mode, 1);
750       if (0 == pstr_sbr_env_data_right->sbr_patching_mode) {
751         payload_cnt_bits += ixheaace_write_bits(
752             pstr_bs_handle, pstr_sbr_env_data_right->sbr_oversampling_flag, 1);
753         payload_cnt_bits += ixheaace_write_bits(
754             pstr_bs_handle, pstr_sbr_env_data_right->sbr_pitchin_bins_flag, 1);
755         if (0 != pstr_sbr_env_data_right->sbr_pitchin_bins_flag) {
756           payload_cnt_bits +=
757               ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_right->sbr_pitchin_bins, 7);
758         }
759       }
760     }
761     payload_cnt_bits +=
762         ixheaace_encode_sbr_grid(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec);
763 
764     payload_cnt_bits +=
765         ixheaace_encode_sbr_grid(pstr_sbr_env_data_right, pstr_bs_handle, sbr_codec);
766 
767     payload_cnt_bits +=
768         ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec,
769                                  pstr_sbr_env_data_left->usac_indep_flag, 0);
770 
771     payload_cnt_bits +=
772         ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_right, pstr_bs_handle, sbr_codec,
773                                  pstr_sbr_env_data_left->usac_indep_flag, 0);
774 
775     for (i = 0; i < pstr_sbr_env_data_left->noise_band_count; i++) {
776       payload_cnt_bits += ixheaace_write_bits(
777           pstr_bs_handle, pstr_sbr_env_data_left->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
778     }
779 
780     for (i = 0; i < pstr_sbr_env_data_right->noise_band_count; i++) {
781       payload_cnt_bits += ixheaace_write_bits(
782           pstr_bs_handle, pstr_sbr_env_data_right->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
783     }
784 
785     err_code = ixheaace_write_env_data(pstr_sbr_env_data_left, pstr_bs_handle, 0, sbr_codec,
786                                        &env_data_len);
787     if (err_code) {
788       *ptr_num_bits = payload_cnt_bits;
789       return err_code;
790     }
791     payload_cnt_bits += env_data_len;
792 
793     err_code = ixheaace_write_env_data(pstr_sbr_env_data_right, pstr_bs_handle, 0, sbr_codec,
794                                        &env_data_len);
795     if (err_code) {
796       *ptr_num_bits = payload_cnt_bits;
797       return err_code;
798     }
799     payload_cnt_bits += env_data_len;
800 
801     payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_left, pstr_bs_handle, 0);
802 
803     payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_right, pstr_bs_handle, 0);
804 
805     payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_left,
806                                                              pstr_bs_handle, sbr_codec, 0);
807 
808     payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_right,
809                                                              pstr_bs_handle, sbr_codec, 0);
810   }
811 
812   *ptr_num_bits = payload_cnt_bits;
813   return err_code;
814 }
815 
iexhaace_esbr_write_bs(ixheaace_str_esbr_bs_data * pstr_esbr_bs_data,ixheaace_bit_buf_handle pstr_bs_handle)816 static WORD32 iexhaace_esbr_write_bs(ixheaace_str_esbr_bs_data *pstr_esbr_bs_data,
817                                      ixheaace_bit_buf_handle pstr_bs_handle) {
818   WORD32 num_bits = 0;
819   num_bits +=
820       ixheaace_write_bits(pstr_bs_handle, EXTENSION_ID_ESBR_CODING, SI_SBR_EXTENSION_ID_BITS);
821   num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_preprocessing, 1);
822   if (1 == pstr_esbr_bs_data->sbr_num_chan) {
823     num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[0], 1);
824     if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
825       num_bits +=
826           ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[0], 1);
827       num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[0], 1);
828       if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
829         num_bits +=
830             ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
831       }
832     }
833   } else if (2 == pstr_esbr_bs_data->sbr_num_chan) {
834     if (pstr_esbr_bs_data->sbr_coupling) {
835       num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[0], 1);
836       if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
837         num_bits +=
838             ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[0], 1);
839         num_bits +=
840             ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[0], 1);
841         if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
842           num_bits +=
843               ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
844         }
845       }
846     } else {
847       num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[0], 1);
848       if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
849         num_bits +=
850             ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[0], 1);
851         num_bits +=
852             ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[0], 1);
853         if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
854           num_bits +=
855               ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
856         } else {
857           pstr_esbr_bs_data->sbr_patching_mode[0] = pstr_esbr_bs_data->sbr_patching_mode[0];
858         }
859       }
860       num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[1], 1);
861       if (pstr_esbr_bs_data->sbr_patching_mode[1] == 0) {
862         num_bits +=
863             ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[1], 1);
864         num_bits +=
865             ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[1], 1);
866         if (pstr_esbr_bs_data->sbr_pitchin_flags[1] == 1) {
867           num_bits +=
868               ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
869         }
870       }
871     }
872   }
873   if (num_bits < 8) {
874     num_bits += ixheaace_write_bits(pstr_bs_handle, 0, (UWORD8)(8 - num_bits));
875   }
876 
877   return num_bits;
878 }
879 
ixheaace_get_sbr_extended_data_size(struct ixheaace_ps_enc * pstr_ps_handle,WORD32 is_hdr_active,ixheaace_str_sbr_tabs * pstr_sbr_tab,WORD32 is_esbr,ixheaace_str_esbr_bs_data * pstr_esbr_data)880 static WORD32 ixheaace_get_sbr_extended_data_size(struct ixheaace_ps_enc *pstr_ps_handle,
881                                                   WORD32 is_hdr_active,
882                                                   ixheaace_str_sbr_tabs *pstr_sbr_tab,
883                                                   WORD32 is_esbr,
884                                                   ixheaace_str_esbr_bs_data *pstr_esbr_data) {
885   WORD32 ext_data_bits = 0;
886 
887   if (pstr_ps_handle) {
888     ext_data_bits +=
889         ixheaace_enc_write_ps_data(pstr_ps_handle, is_hdr_active, pstr_sbr_tab->ptr_ps_tab);
890   }
891   if (is_esbr) {
892     ext_data_bits += ixheaace_get_esbr_ext_data_size(pstr_esbr_data);
893   }
894 
895   if (ext_data_bits != 0) {
896     ext_data_bits += SI_SBR_EXTENSION_ID_BITS;
897   }
898 
899   return (ext_data_bits + 7) >> 3;
900 }
901 
ixheaace_encode_extended_data(struct ixheaace_ps_enc * pstr_ps_handle,WORD32 is_hdr_active,ixheaace_bit_buf_handle pstr_bs_prev,WORD32 * ptr_sbr_hdr_bits,ixheaace_bit_buf_handle pstr_bs_handle,WORD32 * ptr_payload_bits,ixheaace_str_sbr_tabs * pstr_sbr_tab,WORD32 is_esbr,ixheaace_str_esbr_bs_data * pstr_esbr_data)902 static VOID ixheaace_encode_extended_data(struct ixheaace_ps_enc *pstr_ps_handle,
903                                           WORD32 is_hdr_active,
904                                           ixheaace_bit_buf_handle pstr_bs_prev,
905                                           WORD32 *ptr_sbr_hdr_bits,
906                                           ixheaace_bit_buf_handle pstr_bs_handle,
907                                           WORD32 *ptr_payload_bits,
908                                           ixheaace_str_sbr_tabs *pstr_sbr_tab, WORD32 is_esbr,
909                                           ixheaace_str_esbr_bs_data *pstr_esbr_data) {
910   WORD32 ext_data_size;
911   WORD32 payload_bits_in = *ptr_payload_bits;
912   WORD32 payload_cnt_bits = 0;
913 
914   ext_data_size = ixheaace_get_sbr_extended_data_size(pstr_ps_handle, is_hdr_active, pstr_sbr_tab,
915                                                       is_esbr, pstr_esbr_data);
916 
917   if (ext_data_size != 0) {
918     if (pstr_ps_handle && ixheaace_append_ps_bitstream(pstr_ps_handle, NULL_PTR, 0)) {
919       ixheaace_bit_buf bitbuf_tmp;
920       UWORD8 tmp[IXHEAACE_MAX_PAYLOAD_SIZE];
921       WORD32 max_ext_size = (1 << SI_SBR_EXTENSION_SIZE_BITS) - 1;
922       WORD32 num_bits;
923 
924       num_bits = ia_enhaacplus_enc_get_bits_available(&pstr_ps_handle->ps_bit_buf);
925       num_bits += SI_SBR_EXTENSION_ID_BITS;
926       if (is_esbr) {
927         num_bits += ixheaace_get_esbr_ext_data_size(pstr_esbr_data);
928         num_bits += SI_SBR_EXTENSION_ID_BITS;
929       }
930       ext_data_size = (num_bits + 7) >> 3;
931       if (ia_enhaacplus_enc_get_bits_available(pstr_bs_prev) == 0) {
932         pstr_ps_handle->hdr_bits_prev_frame = *ptr_sbr_hdr_bits;
933         ia_enhaacplus_enc_copy_bitbuf(pstr_bs_handle, pstr_bs_prev);
934       } else {
935         WORD32 tmp_bits;
936         ia_enhaacplus_enc_create_bitbuffer(&bitbuf_tmp, tmp, sizeof(tmp));
937         tmp_bits = *ptr_sbr_hdr_bits;
938         *ptr_sbr_hdr_bits = pstr_ps_handle->hdr_bits_prev_frame;
939         pstr_ps_handle->hdr_bits_prev_frame = tmp_bits;
940         ixheaace_copy_bitbuf_to_and_fro(pstr_bs_prev, pstr_bs_handle);
941       }
942       ixheaace_write_bits(pstr_bs_handle, 1, SI_SBR_EXTENDED_DATA_BITS);
943 
944       if (ext_data_size < max_ext_size) {
945         ixheaace_write_bits(pstr_bs_handle, ext_data_size, SI_SBR_EXTENSION_SIZE_BITS);
946       } else {
947         ixheaace_write_bits(pstr_bs_handle, max_ext_size, SI_SBR_EXTENSION_SIZE_BITS);
948         ixheaace_write_bits(pstr_bs_handle, ext_data_size - max_ext_size,
949                             SI_SBR_EXTENSION_ESC_COUNT_BITS);
950       }
951       WORD32 start_bits = pstr_bs_handle->cnt_bits;
952       *ptr_payload_bits =
953           ixheaace_append_ps_bitstream(pstr_ps_handle, pstr_bs_handle, ptr_sbr_hdr_bits);
954 
955       if (is_esbr) {
956         *ptr_payload_bits += iexhaace_esbr_write_bs(pstr_esbr_data, pstr_bs_handle);
957       }
958 
959       WORD32 fill_bits = (ext_data_size << 3) - (pstr_bs_handle->cnt_bits - start_bits);
960       ixheaace_write_bits(pstr_bs_handle, 0, (UWORD8)fill_bits);
961       *ptr_payload_bits = *ptr_payload_bits + fill_bits;
962     } else {
963       if (is_esbr) {
964         WORD32 max_ext_size = (1 << SI_SBR_EXTENSION_SIZE_BITS) - 1;
965         WORD32 num_bits;
966         num_bits = ixheaace_get_esbr_ext_data_size(pstr_esbr_data);
967         ext_data_size = (num_bits + SI_SBR_EXTENSION_ID_BITS + 7) >> 3;
968         ixheaace_write_bits(pstr_bs_handle, 1, SI_SBR_EXTENDED_DATA_BITS);
969         if (ext_data_size < max_ext_size) {
970           ixheaace_write_bits(pstr_bs_handle, ext_data_size, SI_SBR_EXTENSION_SIZE_BITS);
971         } else {
972           ixheaace_write_bits(pstr_bs_handle, max_ext_size, SI_SBR_EXTENSION_SIZE_BITS);
973           ixheaace_write_bits(pstr_bs_handle, ext_data_size - max_ext_size,
974                               SI_SBR_EXTENSION_ESC_COUNT_BITS);
975         }
976         WORD32 start_bits = pstr_bs_handle->cnt_bits;
977         *ptr_payload_bits += iexhaace_esbr_write_bs(pstr_esbr_data, pstr_bs_handle);
978         UWORD8 fill_bits =
979             (UWORD8)((ext_data_size << 3) - (pstr_bs_handle->cnt_bits - start_bits));
980         ixheaace_write_bits(pstr_bs_handle, 0, fill_bits);
981         *ptr_payload_bits = *ptr_payload_bits + fill_bits;
982       } else {
983         WORD32 max_ext_size = (1 << SI_SBR_EXTENSION_SIZE_BITS) - 1;
984         payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 1, SI_SBR_EXTENDED_DATA_BITS);
985 
986         if (ext_data_size < max_ext_size) {
987           payload_cnt_bits +=
988               ixheaace_write_bits(pstr_bs_handle, ext_data_size, SI_SBR_EXTENSION_SIZE_BITS);
989         } else {
990           payload_cnt_bits +=
991               ixheaace_write_bits(pstr_bs_handle, max_ext_size, SI_SBR_EXTENSION_SIZE_BITS);
992 
993           payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, ext_data_size - max_ext_size,
994                                                   SI_SBR_EXTENSION_ESC_COUNT_BITS);
995         }
996 
997         *ptr_payload_bits = payload_cnt_bits + payload_bits_in;
998       }
999     }
1000   } else {
1001     payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SI_SBR_EXTENDED_DATA_BITS);
1002 
1003     *ptr_payload_bits = payload_cnt_bits + payload_bits_in;
1004   }
1005 }
1006 
ixheaace_encode_sbr_data(ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right,ixheaace_pstr_common_data pstr_cmon_data,ixheaace_sbr_element_type sbr_ele_type,struct ixheaace_ps_enc * pstr_ps_handle,WORD32 is_hdr_active,WORD32 coupling,ixheaace_str_sbr_tabs * pstr_sbr_tab,ixheaace_sbr_codec_type sbr_codec,WORD32 is_esbr,ixheaace_str_esbr_bs_data * pstr_esbr_data,WORD32 * ptr_num_bits)1007 static IA_ERRORCODE ixheaace_encode_sbr_data(
1008     ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,
1009     ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right, ixheaace_pstr_common_data pstr_cmon_data,
1010     ixheaace_sbr_element_type sbr_ele_type, struct ixheaace_ps_enc *pstr_ps_handle,
1011     WORD32 is_hdr_active, WORD32 coupling, ixheaace_str_sbr_tabs *pstr_sbr_tab,
1012     ixheaace_sbr_codec_type sbr_codec, WORD32 is_esbr, ixheaace_str_esbr_bs_data *pstr_esbr_data,
1013     WORD32 *ptr_num_bits) {
1014   WORD32 payload_cnt_bits = 0;
1015   IA_ERRORCODE err_code = IA_NO_ERROR;
1016 
1017   switch (sbr_ele_type) {
1018     case IXHEAACE_SBR_ID_SCE:
1019 
1020       err_code = ixheaace_encode_sbr_single_channel_element(
1021           pstr_sbr_env_data_left, &pstr_cmon_data->str_sbr_bit_buf, sbr_codec, &payload_cnt_bits);
1022       if (err_code) {
1023         return err_code;
1024       }
1025       if (USAC_SBR != sbr_codec) {
1026         ixheaace_encode_extended_data(
1027             pstr_ps_handle, is_hdr_active, &pstr_cmon_data->str_sbr_bit_buf_prev,
1028             &pstr_cmon_data->sbr_hdr_bits, &pstr_cmon_data->str_sbr_bit_buf, &payload_cnt_bits,
1029             pstr_sbr_tab, is_esbr, pstr_esbr_data);
1030       }
1031       break;
1032     case IXHEAACE_SBR_ID_CPE:
1033 
1034       err_code = ixheaace_encode_sbr_channel_pair_element(
1035           pstr_sbr_env_data_left, pstr_sbr_env_data_right, &pstr_cmon_data->str_sbr_bit_buf,
1036           coupling, sbr_codec, &payload_cnt_bits);
1037       if (err_code) {
1038         return err_code;
1039       }
1040       if (USAC_SBR != sbr_codec) {
1041         ixheaace_encode_extended_data(NULL_PTR, 0, NULL_PTR, 0, &pstr_cmon_data->str_sbr_bit_buf,
1042                                       &payload_cnt_bits, pstr_sbr_tab, is_esbr, pstr_esbr_data);
1043       }
1044       break;
1045   }
1046 
1047   pstr_cmon_data->sbr_data_bits = payload_cnt_bits;
1048   *ptr_num_bits = payload_cnt_bits;
1049 
1050   pstr_cmon_data->prev_bit_buf_read_offset =
1051       (WORD32)(pstr_cmon_data->str_sbr_bit_buf_prev.ptr_read_next -
1052                pstr_cmon_data->str_sbr_bit_buf_prev.ptr_bit_buf_base);
1053   pstr_cmon_data->prev_bit_buf_write_offset =
1054       (WORD32)(pstr_cmon_data->str_sbr_bit_buf_prev.ptr_write_next -
1055                pstr_cmon_data->str_sbr_bit_buf_prev.ptr_bit_buf_base);
1056 
1057   return err_code;
1058 }
1059 
ixheaace_encode_sbr_header_data(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,ixheaace_bit_buf_handle pstr_bs_handle)1060 static WORD32 ixheaace_encode_sbr_header_data(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,
1061                                               ixheaace_bit_buf_handle pstr_bs_handle)
1062 
1063 {
1064   WORD32 payload_cnt_bits = 0;
1065 
1066   if (pstr_sbr_hdr != NULL_PTR) {
1067     payload_cnt_bits +=
1068         ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_amp_res, SI_SBR_AMP_RES_BITS);
1069 
1070     payload_cnt_bits +=
1071         ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_start_freq, SI_SBR_START_FREQ_BITS);
1072 
1073     payload_cnt_bits +=
1074         ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_stop_freq, SI_SBR_STOP_FREQ_BITS);
1075 
1076     payload_cnt_bits +=
1077         ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_xover_band, SI_SBR_XOVER_BAND_BITS);
1078 
1079     payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SI_SBR_RESERVED_BITS);
1080 
1081     payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_1,
1082                                             SI_SBR_HEADER_EXTRA_1_BITS);
1083 
1084     payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_2,
1085                                             SI_SBR_HEADER_EXTRA_2_BITS);
1086 
1087     if (pstr_sbr_hdr->header_extra_1) {
1088       payload_cnt_bits +=
1089           ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->freq_scale, SI_SBR_FREQ_SCALE_BITS);
1090 
1091       payload_cnt_bits +=
1092           ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->alter_scale, SI_SBR_ALTER_SCALE_BITS);
1093 
1094       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_noise_bands,
1095                                               SI_SBR_NOISE_BANDS_BITS);
1096     }
1097 
1098     if (pstr_sbr_hdr->header_extra_2) {
1099       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_limiter_bands,
1100                                               SI_SBR_LIMITER_BANDS_BITS);
1101       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_limiter_gains,
1102                                               SI_SBR_LIMITER_GAINS_BITS);
1103 
1104       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_interpol_freq,
1105                                               SI_SBR_INTERPOL_FREQ_BITS);
1106 
1107       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_smoothing_length,
1108                                               SI_SBR_SMOOTHING_LENGTH_BITS);
1109     }
1110   }
1111 
1112   return payload_cnt_bits;
1113 }
1114 
ia_enhaacplus_enc_encode_sbr_header(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,ixheaace_pstr_common_data pstr_cmon_data)1115 static WORD32 ia_enhaacplus_enc_encode_sbr_header(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,
1116                                                   ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,
1117                                                   ixheaace_pstr_common_data pstr_cmon_data) {
1118   WORD32 payload_cnt_bits = 0;
1119 
1120   if (pstr_sbr_bs->crc_active) {
1121     pstr_cmon_data->sbr_crc_len = 1;
1122   } else {
1123     pstr_cmon_data->sbr_crc_len = 0;
1124   }
1125 
1126   if (pstr_sbr_bs->header_active) {
1127     payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 1, 1);
1128 
1129     payload_cnt_bits +=
1130         ixheaace_encode_sbr_header_data(pstr_sbr_hdr, &pstr_cmon_data->str_sbr_bit_buf);
1131   } else {
1132     payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 0, 1);
1133   }
1134 
1135   pstr_cmon_data->sbr_hdr_bits = payload_cnt_bits;
1136 
1137   return payload_cnt_bits;
1138 }
iusace_encode_sbr_header_data(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,ixheaace_bit_buf_handle pstr_bs_handle)1139 static WORD32 iusace_encode_sbr_header_data(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,
1140                                             ixheaace_bit_buf_handle pstr_bs_handle) {
1141   WORD32 payload_cnt_bits = 0;
1142 
1143   if (pstr_sbr_hdr != NULL_PTR) {
1144     payload_cnt_bits +=
1145         ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_start_freq, SI_SBR_START_FREQ_BITS);
1146 
1147     payload_cnt_bits +=
1148         ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_stop_freq, SI_SBR_STOP_FREQ_BITS);
1149 
1150     payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_1,
1151                                             SI_SBR_HEADER_EXTRA_1_BITS);
1152 
1153     payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_2,
1154                                             SI_SBR_HEADER_EXTRA_2_BITS);
1155 
1156     if (pstr_sbr_hdr->header_extra_1) {
1157       payload_cnt_bits +=
1158           ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->freq_scale, SI_SBR_FREQ_SCALE_BITS);
1159 
1160       payload_cnt_bits +=
1161           ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->alter_scale, SI_SBR_ALTER_SCALE_BITS);
1162 
1163       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_noise_bands,
1164                                               SI_SBR_NOISE_BANDS_BITS);
1165     }
1166 
1167     if (pstr_sbr_hdr->header_extra_2) {
1168       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_limiter_bands,
1169                                               SI_SBR_LIMITER_BANDS_BITS);
1170 
1171       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_limiter_gains,
1172                                               SI_SBR_LIMITER_GAINS_BITS);
1173 
1174       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_interpol_freq,
1175                                               SI_SBR_INTERPOL_FREQ_BITS);
1176 
1177       payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_smoothing_length,
1178                                               SI_SBR_SMOOTHING_LENGTH_BITS);
1179     }
1180   }
1181 
1182   return payload_cnt_bits;
1183 }
1184 
ia_usac_enc_encode_sbr_header(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,ixheaace_pstr_common_data pstr_cmon_data)1185 static WORD32 ia_usac_enc_encode_sbr_header(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,
1186                                             ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,
1187                                             ixheaace_pstr_common_data pstr_cmon_data) {
1188   WORD32 payload_cnt_bits = 0;
1189   WORD32 sbr_info_flag = 0;
1190   WORD32 sbr_hdr_flag = 0;
1191   if (pstr_sbr_bs->usac_indep_flag) {
1192     sbr_hdr_flag = 1;
1193     sbr_info_flag = 1;
1194   } else {
1195     if (pstr_sbr_bs->header_active) {
1196       sbr_info_flag = 1;
1197       payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 1, 1);
1198       sbr_hdr_flag = 1;
1199       payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 1, 1);
1200     } else {
1201       payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 0, 1);
1202     }
1203   }
1204 
1205   if (1 == sbr_info_flag) {
1206     payload_cnt_bits +=
1207         ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_amp_res, 1);
1208 
1209     payload_cnt_bits +=
1210         ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_xover_band, 4);
1211 
1212     payload_cnt_bits +=
1213         ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_pre_proc, 1);
1214     if (pstr_sbr_hdr->sbr_pvc_active) {
1215       payload_cnt_bits +=
1216           ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_pvc_mode, 2);
1217     }
1218   }
1219 
1220   if (1 == sbr_hdr_flag) {
1221     WORD32 sbr_def_hdr = 0;
1222     // SBR default header
1223     payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, sbr_def_hdr, 1);
1224     if (0 == sbr_def_hdr) {
1225       payload_cnt_bits +=
1226           iusace_encode_sbr_header_data(pstr_sbr_hdr, &pstr_cmon_data->str_sbr_bit_buf);
1227     }
1228   }
1229   pstr_cmon_data->sbr_hdr_bits = payload_cnt_bits;
1230 
1231   return payload_cnt_bits;
1232 }
1233 IA_ERRORCODE
ixheaace_write_env_single_channel_element(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,ixheaace_pstr_sbr_env_data pstr_sbr_env_info,struct ixheaace_ps_enc * pstr_ps_handle,ixheaace_pstr_common_data pstr_cmon_data,ixheaace_str_sbr_tabs * pstr_sbr_tab,ixheaace_sbr_codec_type sbr_codec,WORD32 is_esbr,ixheaace_str_esbr_bs_data * pstr_esbr_data,WORD32 * ptr_num_bits)1234 ixheaace_write_env_single_channel_element(
1235     ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr, ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,
1236     ixheaace_pstr_sbr_env_data pstr_sbr_env_info, struct ixheaace_ps_enc *pstr_ps_handle,
1237     ixheaace_pstr_common_data pstr_cmon_data, ixheaace_str_sbr_tabs *pstr_sbr_tab,
1238     ixheaace_sbr_codec_type sbr_codec, WORD32 is_esbr, ixheaace_str_esbr_bs_data *pstr_esbr_data,
1239     WORD32 *ptr_num_bits) {
1240   WORD32 payload_cnt_bits = 0;
1241   WORD32 num_sbr_data_bits = 0;
1242   IA_ERRORCODE err_code = IA_NO_ERROR;
1243 
1244   pstr_cmon_data->sbr_hdr_bits = 0;
1245   pstr_cmon_data->sbr_data_bits = 0;
1246   pstr_cmon_data->sbr_crc_len = 0;
1247 
1248   if (pstr_sbr_env_info != NULL_PTR) {
1249     if (USAC_SBR == sbr_codec) {
1250       payload_cnt_bits +=
1251           ia_usac_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1252     } else {
1253       /* write header */
1254       payload_cnt_bits +=
1255           ia_enhaacplus_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1256     }
1257     /* write data */
1258     err_code =
1259         ixheaace_encode_sbr_data(pstr_sbr_env_info, NULL_PTR, pstr_cmon_data, IXHEAACE_SBR_ID_SCE,
1260                                  pstr_ps_handle, pstr_sbr_bs->header_active, 0, pstr_sbr_tab,
1261                                  sbr_codec, is_esbr, pstr_esbr_data, &num_sbr_data_bits);
1262     if (err_code) {
1263       return err_code;
1264     }
1265     payload_cnt_bits += num_sbr_data_bits;
1266   }
1267 
1268   *ptr_num_bits = payload_cnt_bits;
1269   return err_code;
1270 }
1271 
1272 IA_ERRORCODE
ixheaace_write_env_channel_pair_element(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right,ixheaace_pstr_common_data pstr_cmon_data,ixheaace_str_sbr_tabs * pstr_sbr_tab,ixheaace_sbr_codec_type sbr_codec,WORD32 is_esbr,ixheaace_str_esbr_bs_data * pstr_esbr_data,WORD32 * ptr_num_bits)1273 ixheaace_write_env_channel_pair_element(
1274     ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr, ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,
1275     ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,
1276     ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right, ixheaace_pstr_common_data pstr_cmon_data,
1277     ixheaace_str_sbr_tabs *pstr_sbr_tab, ixheaace_sbr_codec_type sbr_codec, WORD32 is_esbr,
1278     ixheaace_str_esbr_bs_data *pstr_esbr_data, WORD32 *ptr_num_bits)
1279 
1280 {
1281   WORD32 payload_cnt_bits = 0;
1282   WORD32 num_sbr_data_bits = 0;
1283   IA_ERRORCODE err_code = IA_NO_ERROR;
1284 
1285   pstr_cmon_data->sbr_hdr_bits = 0;
1286   pstr_cmon_data->sbr_data_bits = 0;
1287   pstr_cmon_data->sbr_crc_len = 0;
1288 
1289   /* write pure SBR data */
1290   if ((pstr_sbr_env_data_left != NULL_PTR) && (pstr_sbr_env_data_right != NULL_PTR)) {
1291     if (USAC_SBR == sbr_codec) {
1292       payload_cnt_bits +=
1293           ia_usac_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1294     } else {
1295       /* write header */
1296       payload_cnt_bits +=
1297           ia_enhaacplus_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1298     }
1299 
1300     /* write data */
1301     err_code = ixheaace_encode_sbr_data(pstr_sbr_env_data_left, pstr_sbr_env_data_right,
1302                                         pstr_cmon_data, IXHEAACE_SBR_ID_CPE, NULL_PTR, 0,
1303                                         pstr_sbr_hdr->coupling, pstr_sbr_tab, sbr_codec, is_esbr,
1304                                         pstr_esbr_data, &num_sbr_data_bits);
1305     if (err_code) {
1306       return err_code;
1307     }
1308     payload_cnt_bits += num_sbr_data_bits;
1309   }
1310 
1311   *ptr_num_bits = payload_cnt_bits;
1312   return err_code;
1313 }
1314 
1315 IA_ERRORCODE
ixheaace_count_sbr_channel_pair_element(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right,ixheaace_pstr_common_data pstr_cmon_data,ixheaace_str_sbr_tabs * pstr_sbr_tab,ixheaace_sbr_codec_type sbr_codec,WORD32 is_esbr,ixheaace_str_esbr_bs_data * pstr_esbr_data,WORD32 * ptr_num_bits)1316 ixheaace_count_sbr_channel_pair_element(
1317     ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr, ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,
1318     ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,
1319     ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right, ixheaace_pstr_common_data pstr_cmon_data,
1320     ixheaace_str_sbr_tabs *pstr_sbr_tab, ixheaace_sbr_codec_type sbr_codec, WORD32 is_esbr,
1321     ixheaace_str_esbr_bs_data *pstr_esbr_data, WORD32 *ptr_num_bits)
1322 
1323 {
1324   IA_ERRORCODE err_code = IA_NO_ERROR;
1325   ixheaace_bit_buf bit_buf_tmp = pstr_cmon_data->str_sbr_bit_buf;
1326 
1327   err_code = ixheaace_write_env_channel_pair_element(
1328       pstr_sbr_hdr, pstr_sbr_bs, pstr_sbr_env_data_left, pstr_sbr_env_data_right, pstr_cmon_data,
1329       pstr_sbr_tab, sbr_codec, is_esbr, pstr_esbr_data, ptr_num_bits);
1330 
1331   pstr_cmon_data->str_sbr_bit_buf = bit_buf_tmp;
1332 
1333   return err_code;
1334 }
1335 
ixheaace_map_low_res_energy_value(WORD32 curr_val,WORD32 * ptr_prev_data,WORD32 offset,WORD32 index,ixheaace_freq_res res)1336 VOID ixheaace_map_low_res_energy_value(WORD32 curr_val, WORD32 *ptr_prev_data, WORD32 offset,
1337                                        WORD32 index, ixheaace_freq_res res) {
1338   if (res == FREQ_RES_LOW) {
1339     if (offset >= 0) {
1340       if (index < offset) {
1341         ptr_prev_data[index] = curr_val;
1342       } else {
1343         ptr_prev_data[2 * index - offset] = curr_val;
1344         ptr_prev_data[2 * index + 1 - offset] = curr_val;
1345       }
1346     } else {
1347       offset = -offset;
1348 
1349       if (index < offset) {
1350         ptr_prev_data[3 * index] = curr_val;
1351         ptr_prev_data[3 * index + 1] = curr_val;
1352         ptr_prev_data[3 * index + 2] = curr_val;
1353       } else {
1354         ptr_prev_data[2 * index + offset] = curr_val;
1355         ptr_prev_data[2 * index + 1 + offset] = curr_val;
1356       }
1357     }
1358   } else {
1359     ptr_prev_data[index] = curr_val;
1360   }
1361 }
1362 
1363 IA_ERRORCODE
ixheaace_compute_bits(WORD32 delta,WORD32 code_book_scf_lav_lvl,WORD32 code_book_scf_lav_balance,const UWORD8 * ptr_huff_tbl_lvl,const UWORD8 * ptr_huff_tbl_bal,WORD32 coupling,WORD32 ch,WORD32 * ptr_delta_bits)1364 ixheaace_compute_bits(WORD32 delta, WORD32 code_book_scf_lav_lvl,
1365                       WORD32 code_book_scf_lav_balance, const UWORD8 *ptr_huff_tbl_lvl,
1366                       const UWORD8 *ptr_huff_tbl_bal, WORD32 coupling, WORD32 ch,
1367                       WORD32 *ptr_delta_bits) {
1368   WORD32 index;
1369   *ptr_delta_bits = 0;
1370 
1371   if (coupling) {
1372     if (ch == 1) {
1373       index = (delta < 0) ? ixheaac_max32(delta, -code_book_scf_lav_balance)
1374                           : ixheaac_min32(delta, code_book_scf_lav_balance);
1375 
1376       if (index != delta) {
1377         return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_BS;
1378       }
1379 
1380       *ptr_delta_bits = ptr_huff_tbl_bal[index + code_book_scf_lav_balance];
1381     } else {
1382       index = (delta < 0) ? ixheaac_max32(delta, -code_book_scf_lav_lvl)
1383                           : ixheaac_min32(delta, code_book_scf_lav_lvl);
1384 
1385       if (index != delta) {
1386         return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_BS;
1387       }
1388 
1389       *ptr_delta_bits = ptr_huff_tbl_lvl[index + code_book_scf_lav_lvl];
1390     }
1391   } else {
1392     index = (delta < 0) ? ixheaac_max32(delta, -code_book_scf_lav_lvl)
1393                         : ixheaac_min32(delta, code_book_scf_lav_lvl);
1394 
1395     *ptr_delta_bits = ptr_huff_tbl_lvl[index + code_book_scf_lav_lvl];
1396   }
1397 
1398   return IA_NO_ERROR;
1399 }
1400