xref: /aosp_15_r20/external/libxaac/encoder/iusace_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 <string.h>
22 #include <math.h>
23 #include "ixheaac_type_def.h"
24 #include "iusace_bitbuffer.h"
25 
26 /* DRC */
27 #include "impd_drc_common_enc.h"
28 #include "impd_drc_uni_drc.h"
29 #include "impd_drc_tables.h"
30 #include "impd_drc_api.h"
31 #include "impd_drc_uni_drc_eq.h"
32 #include "impd_drc_uni_drc_filter_bank.h"
33 #include "impd_drc_gain_enc.h"
34 #include "impd_drc_struct_def.h"
35 
36 #include "ixheaace_mps_common_define.h"
37 #include "iusace_cnst.h"
38 #include "iusace_tns_usac.h"
39 #include "iusace_psy_mod.h"
40 #include "iusace_tns_usac.h"
41 #include "iusace_config.h"
42 #include "iusace_arith_enc.h"
43 #include "ixheaace_adjust_threshold_data.h"
44 #include "iusace_block_switch_const.h"
45 #include "iusace_block_switch_struct_def.h"
46 #include "iusace_fd_qc_util.h"
47 #include "iusace_fd_quant.h"
48 #include "iusace_ms.h"
49 #include "iusace_signal_classifier.h"
50 #include "ixheaace_sbr_header.h"
51 #include "ixheaace_config.h"
52 #include "ixheaace_asc_write.h"
53 #include "iusace_main.h"
54 #include "iusace_rom.h"
55 
iusace_write_scf_data(ia_bit_buf_struct * it_bit_buf,WORD32 max_sfb,WORD32 num_sfb,const WORD32 * ptr_scale_factors,WORD32 num_win_grps,WORD32 global_gain,const WORD32 huff_tab[CODE_BOOK_ALPHA_LAV][2])56 WORD32 iusace_write_scf_data(ia_bit_buf_struct *it_bit_buf, WORD32 max_sfb, WORD32 num_sfb,
57                              const WORD32 *ptr_scale_factors, WORD32 num_win_grps,
58                              WORD32 global_gain, const WORD32 huff_tab[CODE_BOOK_ALPHA_LAV][2]) {
59   WORD32 write_flag = (it_bit_buf != NULL);
60   WORD32 i, j, bit_count = 0;
61   WORD32 diff, length, codeword;
62   WORD32 index = 0;
63   WORD32 previous_scale_factor = global_gain;
64 
65   for (j = 0; j < num_win_grps; j++) {
66     for (i = 0; i < max_sfb; i++) {
67       if (!((i == 0) && (j == 0))) {
68         diff = ptr_scale_factors[index] - previous_scale_factor;
69         length = huff_tab[diff + 60][0];
70         bit_count += length;
71         previous_scale_factor = ptr_scale_factors[index];
72         if (write_flag == 1) {
73           codeword = huff_tab[diff + 60][1];
74           iusace_write_bits_buf(it_bit_buf, codeword, (UWORD8)length);
75         }
76       }
77       index++;
78     }
79     for (; i < num_sfb; i++) {
80       index++;
81     }
82   }
83 
84   return (bit_count);
85 }
86 
iusace_write_ms_data(ia_bit_buf_struct * it_bit_buf,WORD32 ms_mask,WORD32 ms_used[MAX_SHORT_WINDOWS][MAX_SFB_LONG],WORD32 num_win_grps,WORD32 nr_of_sfb)87 WORD32 iusace_write_ms_data(ia_bit_buf_struct *it_bit_buf, WORD32 ms_mask,
88                             WORD32 ms_used[MAX_SHORT_WINDOWS][MAX_SFB_LONG], WORD32 num_win_grps,
89                             WORD32 nr_of_sfb) {
90   WORD32 write_flag = (it_bit_buf != NULL);
91   WORD32 bit_count = 0;
92   WORD32 i, j;
93   WORD32 ms_mask_write = ms_mask;
94 
95   if (write_flag) iusace_write_bits_buf(it_bit_buf, ms_mask_write, 2);
96   bit_count += 2;
97 
98   if (ms_mask_write == 1) {
99     for (i = 0; i < num_win_grps; i++) {
100       for (j = 0; j < nr_of_sfb; j++) {
101         if (write_flag) iusace_write_bits_buf(it_bit_buf, ms_used[i][j], 1);
102         bit_count += 1;
103       }
104     }
105   }
106 
107   return bit_count;
108 }
109 
iusace_write_tns_data(ia_bit_buf_struct * it_bit_buf,ia_tns_info * pstr_tns_info,WORD32 window_sequence,WORD32 core_mode)110 WORD32 iusace_write_tns_data(ia_bit_buf_struct *it_bit_buf, ia_tns_info *pstr_tns_info,
111                              WORD32 window_sequence, WORD32 core_mode) {
112   WORD32 write_flag = (it_bit_buf != NULL);
113   WORD32 bit_count = 0;
114   WORD32 num_windows;
115   WORD32 len_tns_nfilt;
116   WORD32 len_tns_length;
117   WORD32 len_tns_order;
118   WORD32 filt;
119   WORD32 res_bits;
120   UWORD32 coeff;
121   WORD32 w;
122 
123   if (window_sequence == EIGHT_SHORT_SEQUENCE) {
124     num_windows = MAX_SHORT_WINDOWS;
125     len_tns_nfilt = 1;
126     len_tns_length = 4;
127     len_tns_order = 3;
128   } else {
129     num_windows = 1;
130     len_tns_nfilt = 2;
131     len_tns_length = 6;
132     len_tns_order = 4;
133   }
134   if (core_mode == 1) {
135     num_windows = 1;
136   }
137 
138   for (w = 0; w < num_windows; w++) {
139     ia_tns_window_data *ptr_win_data = &pstr_tns_info->window_data[w];
140     WORD32 n_filt = ptr_win_data->n_filt;
141     if (write_flag) {
142       iusace_write_bits_buf(it_bit_buf, n_filt, (UWORD8)len_tns_nfilt);
143     }
144     bit_count += len_tns_nfilt;
145     if (n_filt) {
146       res_bits = ptr_win_data->coef_res;
147       if (write_flag) {
148         iusace_write_bits_buf(it_bit_buf, res_bits - DEF_TNS_RES_OFFSET, 1);
149       }
150       bit_count += 1;
151       for (filt = 0; filt < n_filt; filt++) {
152         ia_tns_filter_data *ptr_tns_filt = &ptr_win_data->tns_filter[filt];
153         WORD32 order = ptr_tns_filt->order;
154         if (write_flag) {
155           iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->length, (UWORD8)len_tns_length);
156           iusace_write_bits_buf(it_bit_buf, order, (UWORD8)len_tns_order);
157         }
158         bit_count += (len_tns_length + len_tns_order);
159         if (order) {
160           WORD32 i;
161           if (write_flag) {
162             iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->direction, 1);
163             iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->coef_compress, 1);
164           }
165           bit_count += 2;
166           for (i = 1; i <= order; i++) {
167             if (write_flag) {
168               coeff = (UWORD32)(ptr_tns_filt->index[i]) & ((1 << res_bits) - 1);
169               iusace_write_bits_buf(it_bit_buf, coeff, (UWORD8)res_bits);
170             }
171             bit_count += res_bits;
172           }
173         }
174       }
175     }
176   }
177 
178   return bit_count;
179 }
180 
iusace_calc_grouping_bits(const WORD32 * ptr_win_grp_len,WORD32 num_win_grps)181 static WORD32 iusace_calc_grouping_bits(const WORD32 *ptr_win_grp_len, WORD32 num_win_grps) {
182   WORD32 grouping_bits = 0;
183   WORD32 tmp[8] = {0};
184   WORD32 i, j;
185   WORD32 index = 0;
186 
187   for (i = 0; i < num_win_grps; i++) {
188     for (j = 0; j < ptr_win_grp_len[i]; j++) {
189       tmp[index++] = i;
190     }
191   }
192 
193   for (i = 1; i < 8; i++) {
194     grouping_bits = grouping_bits << 1;
195     if (tmp[i] == tmp[i - 1]) {
196       grouping_bits++;
197     }
198   }
199 
200   return (grouping_bits);
201 }
202 
iusace_write_ics_info(ia_bit_buf_struct * it_bit_buf,ia_sfb_params_struct * pstr_sfb_prms,WORD32 ch)203 WORD32 iusace_write_ics_info(ia_bit_buf_struct *it_bit_buf, ia_sfb_params_struct *pstr_sfb_prms,
204                              WORD32 ch) {
205   WORD32 write_flag = (it_bit_buf != NULL);
206   WORD32 bit_count = 0;
207   WORD32 win_seq = 0;
208   WORD32 grouping_bits = 0;
209   WORD32 max_sfb = pstr_sfb_prms->max_sfb[ch];
210   WORD32 window_sequence = pstr_sfb_prms->window_sequence[ch];
211   WORD32 window_shape = pstr_sfb_prms->window_shape[ch];
212   WORD32 num_win_grps = pstr_sfb_prms->num_window_groups[ch];
213 
214   switch (window_sequence) {
215     case EIGHT_SHORT_SEQUENCE:
216       win_seq = 2;
217       break;
218     case ONLY_LONG_SEQUENCE:
219       win_seq = 0;
220       break;
221     case LONG_START_SEQUENCE:
222     case STOP_START_SEQUENCE:
223       win_seq = 1;
224       break;
225     case LONG_STOP_SEQUENCE:
226       win_seq = 3;
227       break;
228     default:
229       win_seq = 3;
230       break;
231   }
232   if (write_flag) iusace_write_bits_buf(it_bit_buf, win_seq, 2);
233   bit_count += 2;
234 
235   if (write_flag) iusace_write_bits_buf(it_bit_buf, window_shape, 1);
236   bit_count += 1;
237 
238   if (window_sequence == EIGHT_SHORT_SEQUENCE) {
239     if (write_flag) iusace_write_bits_buf(it_bit_buf, max_sfb, 4);
240     bit_count += 4;
241 
242     grouping_bits =
243         iusace_calc_grouping_bits(pstr_sfb_prms->window_group_length[ch], num_win_grps);
244     if (write_flag) iusace_write_bits_buf(it_bit_buf, grouping_bits, 7);
245     bit_count += 7;
246   } else {
247     if (write_flag) iusace_write_bits_buf(it_bit_buf, max_sfb, 6);
248     bit_count += 6;
249   }
250 
251   return (bit_count);
252 }
253 
iusace_write_cplx_pred_data(ia_bit_buf_struct * it_bit_buf,WORD32 num_win_grps,WORD32 num_sfb,WORD32 complex_coef,WORD32 pred_coeffs_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],WORD32 pred_coeffs_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG],const WORD32 huff_tab[CODE_BOOK_ALPHA_LAV][2],WORD32 const usac_independency_flg,WORD32 pred_dir,WORD32 cplx_pred_used[MAX_SHORT_WINDOWS][MAX_SFB_LONG],WORD32 cplx_pred_all,WORD32 * ptr_prev_alpha_coeff_re,WORD32 * ptr_prev_alpha_coeff_im,WORD32 * delta_code_time)254 WORD32 iusace_write_cplx_pred_data(ia_bit_buf_struct *it_bit_buf, WORD32 num_win_grps,
255                                    WORD32 num_sfb, WORD32 complex_coef,
256                                    WORD32 pred_coeffs_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
257                                    WORD32 pred_coeffs_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
258                                    const WORD32 huff_tab[CODE_BOOK_ALPHA_LAV][2],
259                                    WORD32 const usac_independency_flg, WORD32 pred_dir,
260                                    WORD32 cplx_pred_used[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
261                                    WORD32 cplx_pred_all, WORD32 *ptr_prev_alpha_coeff_re,
262                                    WORD32 *ptr_prev_alpha_coeff_im, WORD32 *delta_code_time) {
263   WORD32 write_flag = (it_bit_buf != NULL);
264   WORD32 bit_count = 0;
265   WORD32 i, j;
266   WORD32 g;
267   WORD32 sfb;
268   const WORD32 sfb_per_pred_band = 2;
269   WORD32 length_temp1_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
270       length_temp2_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
271       length_temp1_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
272       length_temp2_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG];
273   WORD32 code_word_temp1_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
274       code_word_temp2_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
275       code_word_temp1_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
276       code_word_temp2_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG];
277   WORD32 length_tot1 = 0, length_tot2 = 0;
278 
279   if (write_flag) iusace_write_bits_buf(it_bit_buf, cplx_pred_all, 1);
280   bit_count += 1;
281 
282   if (cplx_pred_all == 0) {
283     for (g = 0; g < num_win_grps; g++) {
284       for (sfb = 0; sfb < num_sfb; sfb += sfb_per_pred_band) {
285         iusace_write_bits_buf(it_bit_buf, cplx_pred_used[g][sfb], 1);
286         bit_count += 1;
287       }
288     }
289   }
290 
291   if (write_flag) iusace_write_bits_buf(it_bit_buf, pred_dir, 1);
292   bit_count += 1;
293 
294   if (write_flag) iusace_write_bits_buf(it_bit_buf, complex_coef, 1);
295   bit_count += 1;
296 
297   if (complex_coef) {
298     if (!usac_independency_flg) {
299       if (write_flag) iusace_write_bits_buf(it_bit_buf, 1, 1); /* use_prev_frame */
300       bit_count += 1;
301     }
302   }
303 
304   if (usac_independency_flg) {
305     *delta_code_time = 0;
306   }
307 
308   /* Switching mechanism for delta_code_time */
309   WORD32 prev_pred_coeff_re_temp1 = 0, prev_pred_coeff_re_temp2 = 0;
310   WORD32 diff_pred_coeff_re_temp1 = 0, diff_pred_coeff_re_temp2 = 0;
311   WORD32 prev_pred_coeff_im_temp1 = 0, prev_pred_coeff_im_temp2 = 0;
312   WORD32 diff_pred_coeff_im_temp1 = 0, diff_pred_coeff_im_temp2 = 0;
313 
314   for (i = 0; i < num_win_grps; i++) {
315     /* delta_code_time = 0*/
316     prev_pred_coeff_re_temp1 = 0;
317     if (complex_coef == 1) {
318       prev_pred_coeff_im_temp1 = 0;
319     }
320 
321     for (j = 0; j < num_sfb; j += 2) {
322       if (!usac_independency_flg) {
323         /* delta_code_time = 1*/
324         if (i > 0) {
325           prev_pred_coeff_re_temp2 = pred_coeffs_re[i - 1][j];
326           if (complex_coef == 1) {
327             prev_pred_coeff_im_temp2 = pred_coeffs_im[i - 1][j];
328           }
329         } else {
330           prev_pred_coeff_re_temp2 = ptr_prev_alpha_coeff_re[j];
331           if (complex_coef == 1) {
332             prev_pred_coeff_im_temp2 = ptr_prev_alpha_coeff_im[j];
333           }
334         }
335       }
336 
337       if (cplx_pred_used[i][j] == 1) {
338         /*Differential Huffman coding of real prediction coefficients*/
339         diff_pred_coeff_re_temp1 =
340             pred_coeffs_re[i][j] - prev_pred_coeff_re_temp1; /* delta_code_time = 0 */
341         prev_pred_coeff_re_temp1 = pred_coeffs_re[i][j];     /* delta_code_time = 0 */
342         if (!usac_independency_flg) {
343           diff_pred_coeff_re_temp2 =
344               pred_coeffs_re[i][j] - prev_pred_coeff_re_temp2; /* delta_code_time = 1 */
345         }
346 
347         /* delta_code_time = 0 */
348         length_temp1_re[i][j] = huff_tab[diff_pred_coeff_re_temp1 + 60][0];
349         code_word_temp1_re[i][j] = huff_tab[diff_pred_coeff_re_temp1 + 60][1];
350 
351         length_tot1 += length_temp1_re[i][j];
352 
353         if (!usac_independency_flg) {
354           /*delta_code_time = 1 */
355           length_temp2_re[i][j] = huff_tab[diff_pred_coeff_re_temp2 + 60][0];
356           code_word_temp2_re[i][j] = huff_tab[diff_pred_coeff_re_temp2 + 60][1];
357 
358           length_tot2 += length_temp2_re[i][j];
359         }
360 
361         if (complex_coef == 1) {
362           /*Differential Huffman coding of imaginary prediction coefficients*/
363           diff_pred_coeff_im_temp1 =
364               pred_coeffs_im[i][j] - prev_pred_coeff_im_temp1; /* delta_code_time = 0 */
365           prev_pred_coeff_im_temp1 = pred_coeffs_im[i][j];     /* delta_code_time = 0*/
366 
367           if (!usac_independency_flg) {
368             diff_pred_coeff_im_temp2 =
369                 pred_coeffs_im[i][j] - prev_pred_coeff_im_temp2; /* delta_code_time = 1 */
370           }
371 
372           /*delta_code_time = 0*/
373           length_temp1_im[i][j] = huff_tab[diff_pred_coeff_im_temp1 + 60][0];
374           code_word_temp1_im[i][j] = huff_tab[diff_pred_coeff_im_temp1 + 60][1];
375 
376           length_tot1 += length_temp1_im[i][j];
377 
378           if (!usac_independency_flg) {
379             /*delta_code_time = 1*/
380             length_temp2_im[i][j] = huff_tab[diff_pred_coeff_im_temp2 + 60][0];
381             code_word_temp2_im[i][j] = huff_tab[diff_pred_coeff_im_temp2 + 60][1];
382 
383             length_tot2 += length_temp2_im[i][j];
384           }
385         }
386       } else {
387         pred_coeffs_re[i][j] = 0;
388         /*delta_code_time = 0*/
389         prev_pred_coeff_re_temp1 = pred_coeffs_re[i][j];
390         if (complex_coef == 1) {
391           pred_coeffs_im[i][j] = 0;
392           /*delta_code_time = 0*/
393           prev_pred_coeff_im_temp1 = pred_coeffs_im[i][j];
394         }
395       }
396 
397       ptr_prev_alpha_coeff_re[j] = pred_coeffs_re[i][j];
398       if (complex_coef == 1) {
399         ptr_prev_alpha_coeff_im[j] = pred_coeffs_im[i][j];
400       }
401     }
402 
403     for (j = num_sfb; j < MAX_SFB_LONG; j++) {
404       pred_coeffs_re[i][j] = 0;
405       ptr_prev_alpha_coeff_re[j] = 0;
406       if (complex_coef == 1) {
407         pred_coeffs_im[i][j] = 0;
408         ptr_prev_alpha_coeff_im[j] = 0;
409       }
410     }
411   }
412 
413   /*Make a decison on the value of delta_code_time per frame */
414   if (!usac_independency_flg) {
415     // Compare the code-word lengths
416     if (length_tot1 <= length_tot2) {
417       *delta_code_time = 0;
418     } else {
419       *delta_code_time = 1;
420     }
421 
422     /* Write the value of delta_code_time to bitstream */
423     if (write_flag) iusace_write_bits_buf(it_bit_buf, *delta_code_time, 1);
424     bit_count += 1;
425   }
426 
427   if (*delta_code_time == 0) {
428     for (i = 0; i < num_win_grps; i++) {
429       for (j = 0; j < num_sfb; j += 2) {
430         if (cplx_pred_used[i][j] == 1) {
431           if (write_flag)
432             iusace_write_bits_buf(it_bit_buf, code_word_temp1_re[i][j],
433                                   (UWORD8)length_temp1_re[i][j]);
434           bit_count += length_temp1_re[i][j];
435 
436           if (complex_coef == 1) {
437             if (write_flag)
438               iusace_write_bits_buf(it_bit_buf, code_word_temp1_im[i][j],
439                                     (UWORD8)length_temp1_im[i][j]);
440             bit_count += length_temp1_im[i][j];
441           }
442         }
443       }
444     }
445   } else {
446     for (i = 0; i < num_win_grps; i++) {
447       for (j = 0; j < num_sfb; j += 2) {
448         if (cplx_pred_used[i][j] == 1) {
449           if (write_flag)
450             iusace_write_bits_buf(it_bit_buf, code_word_temp2_re[i][j],
451                                   (UWORD8)length_temp2_re[i][j]);
452           bit_count += length_temp2_re[i][j];
453 
454           if (complex_coef == 1) {
455             if (write_flag)
456               iusace_write_bits_buf(it_bit_buf, code_word_temp2_im[i][j],
457                                     (UWORD8)length_temp2_im[i][j]);
458             bit_count += length_temp2_im[i][j];
459           }
460         }
461       }
462     }
463   }
464 
465   return bit_count;
466 }
467 
iusace_write_cpe(ia_sfb_params_struct * pstr_sfb_prms,ia_bit_buf_struct * it_bit_buf,WORD32 * tns_data_present,WORD32 const usac_independency_flg,ia_usac_encoder_config_struct * pstr_usac_config,ia_usac_data_struct * pstr_usac_data,WORD32 ch)468 WORD32 iusace_write_cpe(ia_sfb_params_struct *pstr_sfb_prms, ia_bit_buf_struct *it_bit_buf,
469                         WORD32 *tns_data_present, WORD32 const usac_independency_flg,
470                         ia_usac_encoder_config_struct *pstr_usac_config,
471                         ia_usac_data_struct *pstr_usac_data, WORD32 ch) {
472   WORD32 bit_count = 0;
473   WORD32 ms_mask = pstr_usac_data->str_ms_info[ch].ms_mask;
474   WORD32 common_max_sfb = 1;
475   WORD32 tns_active = tns_data_present[0] || tns_data_present[1];
476   ia_tns_info *pstr_tns_info = pstr_usac_data->pstr_tns_info[ch];
477   (VOID) pstr_usac_config;
478 
479   iusace_write_bits_buf(it_bit_buf, tns_active, 1);
480   bit_count += 1;
481 
482   iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->common_win[ch], 1);
483   bit_count += 1;
484 
485   if (pstr_sfb_prms->max_sfb[ch] != pstr_sfb_prms->max_sfb[ch + 1]) {
486     common_max_sfb = 0;
487   }
488 
489   if (pstr_sfb_prms->common_win[ch]) {
490     bit_count += iusace_write_ics_info(it_bit_buf, pstr_sfb_prms, ch);
491 
492     iusace_write_bits_buf(it_bit_buf, common_max_sfb, 1);
493     bit_count += 1;
494 
495     if (common_max_sfb == 0) {
496       if (pstr_sfb_prms->window_sequence[ch] != EIGHT_SHORT_SEQUENCE) {
497         iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->max_sfb[ch + 1], 6);
498         bit_count += 6;
499       } else {
500         iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->max_sfb[ch + 1], 4);
501         bit_count += 4;
502       }
503     }
504 
505     pstr_sfb_prms->max_sfb_ste = MAX(pstr_sfb_prms->max_sfb[ch], pstr_sfb_prms->max_sfb[ch + 1]);
506 
507     bit_count +=
508         iusace_write_ms_data(it_bit_buf, ms_mask, pstr_usac_data->str_ms_info[ch].ms_used,
509                              pstr_sfb_prms->num_window_groups[ch], pstr_sfb_prms->max_sfb_ste);
510 
511     {
512       if ((ms_mask == 3) && (pstr_usac_data->stereo_config_index == 0)) {
513         bit_count += iusace_write_cplx_pred_data(
514             it_bit_buf, pstr_sfb_prms->num_window_groups[ch], pstr_sfb_prms->max_sfb_ste,
515             pstr_usac_data->complex_coef[ch], pstr_usac_data->pred_coef_re[ch],
516             pstr_usac_data->pred_coef_im[ch], iusace_huffman_code_table, usac_independency_flg,
517             pstr_usac_data->pred_dir_idx[ch], pstr_usac_data->cplx_pred_used[ch],
518             pstr_usac_data->cplx_pred_all[ch], pstr_usac_data->pred_coef_re_prev[ch],
519             pstr_usac_data->pred_coef_im_prev[ch], &pstr_usac_data->delta_code_time[ch]);
520       }
521     }
522   }
523 
524   if (tns_active) {
525     WORD32 common_tns = 0;
526     WORD32 tns_on_lr = 1;
527     WORD32 tns_present_both = tns_data_present[0] && tns_data_present[1];
528     WORD32 tns_data_present1 = tns_data_present[1];
529 
530     if (pstr_sfb_prms->common_win[ch]) {
531       iusace_write_bits_buf(it_bit_buf, common_tns, 1);
532       bit_count += 1;
533     }
534 
535     iusace_write_bits_buf(it_bit_buf, tns_on_lr, 1);
536     bit_count += 1;
537 
538     if (common_tns) {
539       bit_count +=
540           iusace_write_tns_data(it_bit_buf, pstr_tns_info, pstr_sfb_prms->window_sequence[ch], 0);
541     } else {
542       iusace_write_bits_buf(it_bit_buf, tns_present_both, 1);
543       bit_count += 1;
544 
545       if (!tns_present_both) {
546         iusace_write_bits_buf(it_bit_buf, tns_data_present1, 1);
547         bit_count += 1;
548       }
549     }
550   }
551 
552   return (bit_count);
553 }
554 
iusace_write_fd_data(ia_bit_buf_struct * it_bit_buf,ia_sfb_params_struct * pstr_sfb_prms,WORD32 num_fac_bits,WORD32 usac_independency_flg,ia_usac_data_struct * pstr_usac_data,ia_usac_encoder_config_struct * pstr_usac_config,WORD32 ch_idx,WORD32 ele_id,WORD32 idx)555 WORD32 iusace_write_fd_data(ia_bit_buf_struct *it_bit_buf, ia_sfb_params_struct *pstr_sfb_prms,
556                             WORD32 num_fac_bits, WORD32 usac_independency_flg,
557                             ia_usac_data_struct *pstr_usac_data,
558                             ia_usac_encoder_config_struct *pstr_usac_config, WORD32 ch_idx,
559                             WORD32 ele_id, WORD32 idx) {
560   WORD32 bit_count = 0;
561   WORD32 fac_data_present = (num_fac_bits > 0) ? 1 : 0;
562   WORD16 *ptr_fac_data = pstr_usac_data->fac_out_stream[ch_idx];
563 
564   WORD32 is_noise_filling = pstr_usac_data->noise_filling[ele_id];
565   WORD32 common_window = pstr_sfb_prms->common_win[ch_idx];
566   ia_usac_quant_info_struct *pstr_quant_info = &(pstr_usac_data->str_quant_info[idx]);
567   ia_tns_info *pstr_tns_info = pstr_usac_data->pstr_tns_info[ch_idx];
568   WORD32 global_gain = pstr_usac_data->str_quant_info[idx].scale_factor[0];
569 
570   iusace_write_bits_buf(it_bit_buf, global_gain, 8);
571   bit_count += 8;
572 
573   if (is_noise_filling) {
574     iusace_write_bits_buf(it_bit_buf, pstr_usac_data->noise_level[idx], 3);
575 
576     iusace_write_bits_buf(it_bit_buf, pstr_usac_data->noise_offset[idx], 5);
577     bit_count += 8;
578   }
579 
580   if (!common_window) {
581     bit_count += iusace_write_ics_info(it_bit_buf, pstr_sfb_prms, ch_idx);
582   }
583 
584   bit_count += iusace_write_scf_data(
585       it_bit_buf, pstr_sfb_prms->max_sfb[ch_idx], pstr_sfb_prms->num_sfb[ch_idx],
586       pstr_quant_info->scale_factor, pstr_sfb_prms->num_window_groups[ch_idx], global_gain,
587       iusace_huffman_code_table);
588 
589   if (pstr_tns_info != NULL && pstr_tns_info->tns_data_present == 1) {
590     bit_count += iusace_write_tns_data(it_bit_buf, pstr_tns_info,
591                                        pstr_sfb_prms->window_sequence[ch_idx], 0);
592   }
593 
594   if (!usac_independency_flg) {
595     iusace_write_bits_buf(it_bit_buf, pstr_quant_info->reset, 1);
596     bit_count += 1;
597   }
598 
599   if (pstr_quant_info->max_spec_coeffs == FRAME_LEN_SHORT_768) {
600     pstr_quant_info->max_spec_coeffs = pstr_quant_info->max_spec_coeffs;
601   }
602   bit_count += iusace_arith_enc_spec(
603       it_bit_buf, pstr_sfb_prms->window_sequence[ch_idx], pstr_quant_info->quant_degroup,
604       pstr_quant_info->max_spec_coeffs, pstr_quant_info->c_pres, pstr_quant_info->c_prev,
605       &(pstr_quant_info->arith_size_prev), usac_independency_flg || pstr_quant_info->reset,
606       pstr_usac_config->ccfl);
607 
608   iusace_write_bits_buf(it_bit_buf, fac_data_present, 1);
609   bit_count += 1;
610 
611   if (fac_data_present) {
612     WORD32 i;
613     for (i = 0; i < num_fac_bits; i += 8) {
614       WORD32 bits_to_write = MIN(8, num_fac_bits - i);
615       iusace_write_bits_buf(it_bit_buf, ptr_fac_data[i / 8] >> (8 - bits_to_write),
616                             (UWORD8)bits_to_write);
617     }
618     bit_count += num_fac_bits;
619   }
620 
621   return (bit_count);
622 }
623 
iusace_count_fd_bits(ia_sfb_params_struct * pstr_sfb_prms,ia_usac_data_struct * pstr_usac_data,WORD32 usac_independency_flg,ia_usac_encoder_config_struct * pstr_usac_config,WORD32 ch_idx,WORD32 idx)624 WORD32 iusace_count_fd_bits(ia_sfb_params_struct *pstr_sfb_prms,
625                             ia_usac_data_struct *pstr_usac_data, WORD32 usac_independency_flg,
626                             ia_usac_encoder_config_struct *pstr_usac_config, WORD32 ch_idx,
627                             WORD32 idx) {
628   WORD32 bit_count = 0;
629   ia_usac_quant_info_struct *pstr_quant_info = &pstr_usac_data->str_quant_info[idx];
630   WORD32 window_sequence = pstr_sfb_prms->window_sequence[ch_idx];
631   WORD32 global_gain = pstr_quant_info->scale_factor[0];
632   WORD32 max_sfb = pstr_sfb_prms->max_sfb[ch_idx];
633   WORD32 num_sfb = pstr_sfb_prms->num_sfb[ch_idx];
634   WORD32 num_win_grps = pstr_sfb_prms->num_window_groups[ch_idx];
635 
636   bit_count += iusace_write_scf_data(NULL, max_sfb, num_sfb, pstr_quant_info->scale_factor,
637                                      num_win_grps, global_gain, iusace_huffman_code_table);
638 
639   WORD32 temp_c_pres[516], temp_c_prev[516], temp_size = pstr_quant_info->arith_size_prev;
640   memcpy(temp_c_pres, pstr_quant_info->c_pres, 516 * sizeof(pstr_quant_info->c_pres[0]));
641   memcpy(temp_c_prev, pstr_quant_info->c_prev, 516 * sizeof(pstr_quant_info->c_prev[0]));
642   bit_count += iusace_arith_enc_spec(
643       NULL, window_sequence, pstr_quant_info->quant_degroup, pstr_quant_info->max_spec_coeffs,
644       temp_c_pres, temp_c_prev, &(temp_size), usac_independency_flg || pstr_quant_info->reset,
645       pstr_usac_config->ccfl);
646 
647   return (bit_count);
648 }
649 
iusace_write_fill_ele(ia_bit_buf_struct * it_bit_buf,WORD32 num_bits)650 WORD32 iusace_write_fill_ele(ia_bit_buf_struct *it_bit_buf, WORD32 num_bits) {
651   WORD32 write_flag = (it_bit_buf != NULL);
652   WORD32 bit_count = 0;
653 
654   if (num_bits <= 8) {
655     if (write_flag) {
656       iusace_write_bits_buf(it_bit_buf, 0, 1);
657     }
658     bit_count++;
659     num_bits--;
660   } else {
661     if (write_flag) {
662       iusace_write_bits_buf(it_bit_buf, 1, 1);
663     }
664     bit_count++;
665     num_bits--;
666 
667     if (num_bits <= 8) {
668       if (write_flag) {
669         iusace_write_bits_buf(it_bit_buf, 1, 1);
670       }
671       bit_count++;
672       num_bits--;
673     } else {
674       WORD32 bytes_to_write = 0;
675       if (write_flag) {
676         iusace_write_bits_buf(it_bit_buf, 0, 1);
677       }
678       bit_count++;
679       num_bits--;
680       bytes_to_write = num_bits >> 3;
681 
682       if (bytes_to_write > 255) {
683         bytes_to_write -= 3;
684         if (write_flag) {
685           iusace_write_bits_buf(it_bit_buf, 255, 8);
686         }
687         if (write_flag) {
688           iusace_write_bits_buf(it_bit_buf, bytes_to_write - 253, 16);
689         }
690         bit_count += 24;
691         num_bits -= 24;
692       } else {
693         bytes_to_write--;
694         if (write_flag) {
695           iusace_write_bits_buf(it_bit_buf, bytes_to_write, 8);
696         }
697         bit_count += 8;
698         num_bits -= 8;
699       }
700 
701       while (bytes_to_write > 0) {
702         if (write_flag) {
703           iusace_write_bits_buf(it_bit_buf, 0xA9, 8);
704         }
705         bit_count += 8;
706         num_bits -= 8;
707         bytes_to_write--;
708       }
709     }
710   }
711   return bit_count;
712 }
713