Coverage Report

Created: 2025-11-09 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/iusace_write_bitstream.c
Line
Count
Source
1
/******************************************************************************
2
 *                                                                            *
3
 * Copyright (C) 2023 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
 */
20
21
#include <string.h>
22
#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
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
946k
                             WORD32 global_gain, const WORD32 huff_tab[CODE_BOOK_ALPHA_LAV][2]) {
59
946k
  WORD32 write_flag = (it_bit_buf != NULL);
60
946k
  WORD32 i, j, bit_count = 0;
61
946k
  WORD32 diff, length, codeword;
62
946k
  WORD32 index = 0;
63
946k
  WORD32 previous_scale_factor = global_gain;
64
65
2.85M
  for (j = 0; j < num_win_grps; j++) {
66
39.3M
    for (i = 0; i < max_sfb; i++) {
67
37.4M
      if (!((i == 0) && (j == 0))) {
68
36.5M
        diff = ptr_scale_factors[index] - previous_scale_factor;
69
36.5M
        length = huff_tab[diff + 60][0];
70
36.5M
        bit_count += length;
71
36.5M
        previous_scale_factor = ptr_scale_factors[index];
72
36.5M
        if (write_flag == 1) {
73
17.3M
          codeword = huff_tab[diff + 60][1];
74
17.3M
          iusace_write_bits_buf(it_bit_buf, codeword, (UWORD8)length);
75
17.3M
        }
76
36.5M
      }
77
37.4M
      index++;
78
37.4M
    }
79
8.01M
    for (; i < num_sfb; i++) {
80
6.10M
      index++;
81
6.10M
    }
82
1.90M
  }
83
84
946k
  return (bit_count);
85
946k
}
86
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
200k
                            WORD32 nr_of_sfb) {
90
200k
  WORD32 write_flag = (it_bit_buf != NULL);
91
200k
  WORD32 bit_count = 0;
92
200k
  WORD32 i, j;
93
200k
  WORD32 ms_mask_write = ms_mask;
94
95
200k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, ms_mask_write, 2);
96
200k
  bit_count += 2;
97
98
200k
  if (ms_mask_write == 1) {
99
81.1k
    for (i = 0; i < num_win_grps; i++) {
100
1.15M
      for (j = 0; j < nr_of_sfb; j++) {
101
1.10M
        if (write_flag) iusace_write_bits_buf(it_bit_buf, ms_used[i][j], 1);
102
1.10M
        bit_count += 1;
103
1.10M
      }
104
58.2k
    }
105
22.8k
  }
106
107
200k
  return bit_count;
108
200k
}
109
110
WORD32 iusace_write_tns_data(ia_bit_buf_struct *it_bit_buf, ia_tns_info *pstr_tns_info,
111
178k
                             WORD32 window_sequence, WORD32 core_mode) {
112
178k
  WORD32 write_flag = (it_bit_buf != NULL);
113
178k
  WORD32 bit_count = 0;
114
178k
  WORD32 num_windows;
115
178k
  WORD32 len_tns_nfilt;
116
178k
  WORD32 len_tns_length;
117
178k
  WORD32 len_tns_order;
118
178k
  WORD32 filt;
119
178k
  WORD32 res_bits;
120
178k
  UWORD32 coeff;
121
178k
  WORD32 w;
122
123
178k
  if (window_sequence == EIGHT_SHORT_SEQUENCE) {
124
21.9k
    num_windows = MAX_SHORT_WINDOWS;
125
21.9k
    len_tns_nfilt = 1;
126
21.9k
    len_tns_length = 4;
127
21.9k
    len_tns_order = 3;
128
156k
  } else {
129
156k
    num_windows = 1;
130
156k
    len_tns_nfilt = 2;
131
156k
    len_tns_length = 6;
132
156k
    len_tns_order = 4;
133
156k
  }
134
178k
  if (core_mode == 1) {
135
0
    num_windows = 1;
136
0
  }
137
138
510k
  for (w = 0; w < num_windows; w++) {
139
332k
    ia_tns_window_data *ptr_win_data = &pstr_tns_info->window_data[w];
140
332k
    WORD32 n_filt = ptr_win_data->n_filt;
141
332k
    if (write_flag) {
142
166k
      iusace_write_bits_buf(it_bit_buf, n_filt, (UWORD8)len_tns_nfilt);
143
166k
    }
144
332k
    bit_count += len_tns_nfilt;
145
332k
    if (n_filt) {
146
250k
      res_bits = ptr_win_data->coef_res;
147
250k
      if (write_flag) {
148
125k
        iusace_write_bits_buf(it_bit_buf, res_bits - DEF_TNS_RES_OFFSET, 1);
149
125k
      }
150
250k
      bit_count += 1;
151
500k
      for (filt = 0; filt < n_filt; filt++) {
152
250k
        ia_tns_filter_data *ptr_tns_filt = &ptr_win_data->tns_filter[filt];
153
250k
        WORD32 order = ptr_tns_filt->order;
154
250k
        if (write_flag) {
155
125k
          iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->length, (UWORD8)len_tns_length);
156
125k
          iusace_write_bits_buf(it_bit_buf, order, (UWORD8)len_tns_order);
157
125k
        }
158
250k
        bit_count += (len_tns_length + len_tns_order);
159
250k
        if (order) {
160
249k
          WORD32 i;
161
249k
          if (write_flag) {
162
124k
            iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->direction, 1);
163
124k
            iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->coef_compress, 1);
164
124k
          }
165
249k
          bit_count += 2;
166
1.99M
          for (i = 1; i <= order; i++) {
167
1.74M
            if (write_flag) {
168
870k
              coeff = (UWORD32)(ptr_tns_filt->index[i]) & ((1 << res_bits) - 1);
169
870k
              iusace_write_bits_buf(it_bit_buf, coeff, (UWORD8)res_bits);
170
870k
            }
171
1.74M
            bit_count += res_bits;
172
1.74M
          }
173
249k
        }
174
250k
      }
175
250k
    }
176
332k
  }
177
178
178k
  return bit_count;
179
178k
}
180
181
77.9k
static WORD32 iusace_calc_grouping_bits(const WORD32 *ptr_win_grp_len, WORD32 num_win_grps) {
182
77.9k
  WORD32 grouping_bits = 0;
183
77.9k
  WORD32 tmp[8] = {0};
184
77.9k
  WORD32 i, j;
185
77.9k
  WORD32 index = 0;
186
187
376k
  for (i = 0; i < num_win_grps; i++) {
188
922k
    for (j = 0; j < ptr_win_grp_len[i]; j++) {
189
623k
      tmp[index++] = i;
190
623k
    }
191
298k
  }
192
193
623k
  for (i = 1; i < 8; i++) {
194
545k
    grouping_bits = grouping_bits << 1;
195
545k
    if (tmp[i] == tmp[i - 1]) {
196
324k
      grouping_bits++;
197
324k
    }
198
545k
  }
199
200
77.9k
  return (grouping_bits);
201
77.9k
}
202
203
WORD32 iusace_write_ics_info(ia_bit_buf_struct *it_bit_buf, ia_sfb_params_struct *pstr_sfb_prms,
204
237k
                             WORD32 ch) {
205
237k
  WORD32 write_flag = (it_bit_buf != NULL);
206
237k
  WORD32 bit_count = 0;
207
237k
  WORD32 win_seq = 0;
208
237k
  WORD32 grouping_bits = 0;
209
237k
  WORD32 max_sfb = pstr_sfb_prms->max_sfb[ch];
210
237k
  WORD32 window_sequence = pstr_sfb_prms->window_sequence[ch];
211
237k
  WORD32 window_shape = pstr_sfb_prms->window_shape[ch];
212
237k
  WORD32 num_win_grps = pstr_sfb_prms->num_window_groups[ch];
213
214
237k
  switch (window_sequence) {
215
77.9k
    case EIGHT_SHORT_SEQUENCE:
216
77.9k
      win_seq = 2;
217
77.9k
      break;
218
107k
    case ONLY_LONG_SEQUENCE:
219
107k
      win_seq = 0;
220
107k
      break;
221
28.0k
    case LONG_START_SEQUENCE:
222
28.1k
    case STOP_START_SEQUENCE:
223
28.1k
      win_seq = 1;
224
28.1k
      break;
225
24.4k
    case LONG_STOP_SEQUENCE:
226
24.4k
      win_seq = 3;
227
24.4k
      break;
228
0
    default:
229
0
      win_seq = 3;
230
0
      break;
231
237k
  }
232
237k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, win_seq, 2);
233
237k
  bit_count += 2;
234
235
237k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, window_shape, 1);
236
237k
  bit_count += 1;
237
238
237k
  if (window_sequence == EIGHT_SHORT_SEQUENCE) {
239
77.9k
    if (write_flag) iusace_write_bits_buf(it_bit_buf, max_sfb, 4);
240
77.9k
    bit_count += 4;
241
242
77.9k
    grouping_bits =
243
77.9k
        iusace_calc_grouping_bits(pstr_sfb_prms->window_group_length[ch], num_win_grps);
244
77.9k
    if (write_flag) iusace_write_bits_buf(it_bit_buf, grouping_bits, 7);
245
77.9k
    bit_count += 7;
246
159k
  } else {
247
159k
    if (write_flag) iusace_write_bits_buf(it_bit_buf, max_sfb, 6);
248
159k
    bit_count += 6;
249
159k
  }
250
251
237k
  return (bit_count);
252
237k
}
253
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
25.0k
                                   WORD32 *ptr_prev_alpha_coeff_im, WORD32 *delta_code_time) {
263
25.0k
  WORD32 write_flag = (it_bit_buf != NULL);
264
25.0k
  WORD32 bit_count = 0;
265
25.0k
  WORD32 i, j;
266
25.0k
  WORD32 g;
267
25.0k
  WORD32 sfb;
268
25.0k
  const WORD32 sfb_per_pred_band = 2;
269
25.0k
  WORD32 length_temp1_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
270
25.0k
      length_temp2_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
271
25.0k
      length_temp1_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
272
25.0k
      length_temp2_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG];
273
25.0k
  WORD32 code_word_temp1_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
274
25.0k
      code_word_temp2_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
275
25.0k
      code_word_temp1_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
276
25.0k
      code_word_temp2_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG];
277
25.0k
  WORD32 length_tot1 = 0, length_tot2 = 0;
278
279
25.0k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, cplx_pred_all, 1);
280
25.0k
  bit_count += 1;
281
282
25.0k
  if (cplx_pred_all == 0) {
283
0
    for (g = 0; g < num_win_grps; g++) {
284
0
      for (sfb = 0; sfb < num_sfb; sfb += sfb_per_pred_band) {
285
0
        iusace_write_bits_buf(it_bit_buf, cplx_pred_used[g][sfb], 1);
286
0
        bit_count += 1;
287
0
      }
288
0
    }
289
0
  }
290
291
25.0k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, pred_dir, 1);
292
25.0k
  bit_count += 1;
293
294
25.0k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, complex_coef, 1);
295
25.0k
  bit_count += 1;
296
297
25.0k
  if (complex_coef) {
298
66
    if (!usac_independency_flg) {
299
49
      if (write_flag) iusace_write_bits_buf(it_bit_buf, 1, 1); /* use_prev_frame */
300
49
      bit_count += 1;
301
49
    }
302
66
  }
303
304
25.0k
  if (usac_independency_flg) {
305
981
    *delta_code_time = 0;
306
981
  }
307
308
  /* Switching mechanism for delta_code_time */
309
25.0k
  WORD32 prev_pred_coeff_re_temp1 = 0, prev_pred_coeff_re_temp2 = 0;
310
25.0k
  WORD32 diff_pred_coeff_re_temp1 = 0, diff_pred_coeff_re_temp2 = 0;
311
25.0k
  WORD32 prev_pred_coeff_im_temp1 = 0, prev_pred_coeff_im_temp2 = 0;
312
25.0k
  WORD32 diff_pred_coeff_im_temp1 = 0, diff_pred_coeff_im_temp2 = 0;
313
314
84.6k
  for (i = 0; i < num_win_grps; i++) {
315
    /* delta_code_time = 0*/
316
59.6k
    prev_pred_coeff_re_temp1 = 0;
317
59.6k
    if (complex_coef == 1) {
318
170
      prev_pred_coeff_im_temp1 = 0;
319
170
    }
320
321
519k
    for (j = 0; j < num_sfb; j += 2) {
322
460k
      if (!usac_independency_flg) {
323
        /* delta_code_time = 1*/
324
435k
        if (i > 0) {
325
155k
          prev_pred_coeff_re_temp2 = pred_coeffs_re[i - 1][j];
326
155k
          if (complex_coef == 1) {
327
456
            prev_pred_coeff_im_temp2 = pred_coeffs_im[i - 1][j];
328
456
          }
329
279k
        } else {
330
279k
          prev_pred_coeff_re_temp2 = ptr_prev_alpha_coeff_re[j];
331
279k
          if (complex_coef == 1) {
332
778
            prev_pred_coeff_im_temp2 = ptr_prev_alpha_coeff_im[j];
333
778
          }
334
279k
        }
335
435k
      }
336
337
460k
      if (cplx_pred_used[i][j] == 1) {
338
        /*Differential Huffman coding of real prediction coefficients*/
339
460k
        diff_pred_coeff_re_temp1 =
340
460k
            pred_coeffs_re[i][j] - prev_pred_coeff_re_temp1; /* delta_code_time = 0 */
341
460k
        prev_pred_coeff_re_temp1 = pred_coeffs_re[i][j];     /* delta_code_time = 0 */
342
460k
        if (!usac_independency_flg) {
343
435k
          diff_pred_coeff_re_temp2 =
344
435k
              pred_coeffs_re[i][j] - prev_pred_coeff_re_temp2; /* delta_code_time = 1 */
345
435k
        }
346
347
        /* delta_code_time = 0 */
348
460k
        length_temp1_re[i][j] = huff_tab[diff_pred_coeff_re_temp1 + 60][0];
349
460k
        code_word_temp1_re[i][j] = huff_tab[diff_pred_coeff_re_temp1 + 60][1];
350
351
460k
        length_tot1 += length_temp1_re[i][j];
352
353
460k
        if (!usac_independency_flg) {
354
          /*delta_code_time = 1 */
355
435k
          length_temp2_re[i][j] = huff_tab[diff_pred_coeff_re_temp2 + 60][0];
356
435k
          code_word_temp2_re[i][j] = huff_tab[diff_pred_coeff_re_temp2 + 60][1];
357
358
435k
          length_tot2 += length_temp2_re[i][j];
359
435k
        }
360
361
460k
        if (complex_coef == 1) {
362
          /*Differential Huffman coding of imaginary prediction coefficients*/
363
1.74k
          diff_pred_coeff_im_temp1 =
364
1.74k
              pred_coeffs_im[i][j] - prev_pred_coeff_im_temp1; /* delta_code_time = 0 */
365
1.74k
          prev_pred_coeff_im_temp1 = pred_coeffs_im[i][j];     /* delta_code_time = 0*/
366
367
1.74k
          if (!usac_independency_flg) {
368
1.23k
            diff_pred_coeff_im_temp2 =
369
1.23k
                pred_coeffs_im[i][j] - prev_pred_coeff_im_temp2; /* delta_code_time = 1 */
370
1.23k
          }
371
372
          /*delta_code_time = 0*/
373
1.74k
          length_temp1_im[i][j] = huff_tab[diff_pred_coeff_im_temp1 + 60][0];
374
1.74k
          code_word_temp1_im[i][j] = huff_tab[diff_pred_coeff_im_temp1 + 60][1];
375
376
1.74k
          length_tot1 += length_temp1_im[i][j];
377
378
1.74k
          if (!usac_independency_flg) {
379
            /*delta_code_time = 1*/
380
1.23k
            length_temp2_im[i][j] = huff_tab[diff_pred_coeff_im_temp2 + 60][0];
381
1.23k
            code_word_temp2_im[i][j] = huff_tab[diff_pred_coeff_im_temp2 + 60][1];
382
383
1.23k
            length_tot2 += length_temp2_im[i][j];
384
1.23k
          }
385
1.74k
        }
386
460k
      } else {
387
0
        pred_coeffs_re[i][j] = 0;
388
        /*delta_code_time = 0*/
389
0
        prev_pred_coeff_re_temp1 = pred_coeffs_re[i][j];
390
0
        if (complex_coef == 1) {
391
0
          pred_coeffs_im[i][j] = 0;
392
          /*delta_code_time = 0*/
393
0
          prev_pred_coeff_im_temp1 = pred_coeffs_im[i][j];
394
0
        }
395
0
      }
396
397
460k
      ptr_prev_alpha_coeff_re[j] = pred_coeffs_re[i][j];
398
460k
      if (complex_coef == 1) {
399
1.74k
        ptr_prev_alpha_coeff_im[j] = pred_coeffs_im[i][j];
400
1.74k
      }
401
460k
    }
402
403
2.19M
    for (j = num_sfb; j < MAX_SFB_LONG; j++) {
404
2.13M
      pred_coeffs_re[i][j] = 0;
405
2.13M
      ptr_prev_alpha_coeff_re[j] = 0;
406
2.13M
      if (complex_coef == 1) {
407
5.32k
        pred_coeffs_im[i][j] = 0;
408
5.32k
        ptr_prev_alpha_coeff_im[j] = 0;
409
5.32k
      }
410
2.13M
    }
411
59.6k
  }
412
413
  /*Make a decison on the value of delta_code_time per frame */
414
25.0k
  if (!usac_independency_flg) {
415
    // Compare the code-word lengths
416
24.0k
    if (length_tot1 <= length_tot2) {
417
11.9k
      *delta_code_time = 0;
418
12.0k
    } else {
419
12.0k
      *delta_code_time = 1;
420
12.0k
    }
421
422
    /* Write the value of delta_code_time to bitstream */
423
24.0k
    if (write_flag) iusace_write_bits_buf(it_bit_buf, *delta_code_time, 1);
424
24.0k
    bit_count += 1;
425
24.0k
  }
426
427
25.0k
  if (*delta_code_time == 0) {
428
46.1k
    for (i = 0; i < num_win_grps; i++) {
429
300k
      for (j = 0; j < num_sfb; j += 2) {
430
267k
        if (cplx_pred_used[i][j] == 1) {
431
267k
          if (write_flag)
432
115k
            iusace_write_bits_buf(it_bit_buf, code_word_temp1_re[i][j],
433
115k
                                  (UWORD8)length_temp1_re[i][j]);
434
267k
          bit_count += length_temp1_re[i][j];
435
436
267k
          if (complex_coef == 1) {
437
1.60k
            if (write_flag)
438
600
              iusace_write_bits_buf(it_bit_buf, code_word_temp1_im[i][j],
439
600
                                    (UWORD8)length_temp1_im[i][j]);
440
1.60k
            bit_count += length_temp1_im[i][j];
441
1.60k
          }
442
267k
        }
443
267k
      }
444
33.1k
    }
445
12.9k
  } else {
446
38.5k
    for (i = 0; i < num_win_grps; i++) {
447
219k
      for (j = 0; j < num_sfb; j += 2) {
448
192k
        if (cplx_pred_used[i][j] == 1) {
449
192k
          if (write_flag)
450
92.2k
            iusace_write_bits_buf(it_bit_buf, code_word_temp2_re[i][j],
451
92.2k
                                  (UWORD8)length_temp2_re[i][j]);
452
192k
          bit_count += length_temp2_re[i][j];
453
454
192k
          if (complex_coef == 1) {
455
144
            if (write_flag)
456
60
              iusace_write_bits_buf(it_bit_buf, code_word_temp2_im[i][j],
457
60
                                    (UWORD8)length_temp2_im[i][j]);
458
144
            bit_count += length_temp2_im[i][j];
459
144
          }
460
192k
        }
461
192k
      }
462
26.4k
    }
463
12.0k
  }
464
465
25.0k
  return bit_count;
466
25.0k
}
467
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
200k
                        ia_usac_data_struct *pstr_usac_data, WORD32 ch) {
472
200k
  WORD32 bit_count = 0;
473
200k
  WORD32 ms_mask = pstr_usac_data->str_ms_info[ch].ms_mask;
474
200k
  WORD32 common_max_sfb = 1;
475
200k
  WORD32 tns_active = tns_data_present[0] || tns_data_present[1];
476
200k
  ia_tns_info *pstr_tns_info = pstr_usac_data->pstr_tns_info[ch];
477
200k
  (VOID) pstr_usac_config;
478
479
200k
  iusace_write_bits_buf(it_bit_buf, tns_active, 1);
480
200k
  bit_count += 1;
481
482
200k
  iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->common_win[ch], 1);
483
200k
  bit_count += 1;
484
485
200k
  if (pstr_sfb_prms->max_sfb[ch] != pstr_sfb_prms->max_sfb[ch + 1]) {
486
1.93k
    common_max_sfb = 0;
487
1.93k
  }
488
489
200k
  if (pstr_sfb_prms->common_win[ch]) {
490
200k
    bit_count += iusace_write_ics_info(it_bit_buf, pstr_sfb_prms, ch);
491
492
200k
    iusace_write_bits_buf(it_bit_buf, common_max_sfb, 1);
493
200k
    bit_count += 1;
494
495
200k
    if (common_max_sfb == 0) {
496
1.93k
      if (pstr_sfb_prms->window_sequence[ch] != EIGHT_SHORT_SEQUENCE) {
497
1.14k
        iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->max_sfb[ch + 1], 6);
498
1.14k
        bit_count += 6;
499
1.14k
      } else {
500
792
        iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->max_sfb[ch + 1], 4);
501
792
        bit_count += 4;
502
792
      }
503
1.93k
    }
504
505
200k
    pstr_sfb_prms->max_sfb_ste = MAX(pstr_sfb_prms->max_sfb[ch], pstr_sfb_prms->max_sfb[ch + 1]);
506
507
200k
    bit_count +=
508
200k
        iusace_write_ms_data(it_bit_buf, ms_mask, pstr_usac_data->str_ms_info[ch].ms_used,
509
200k
                             pstr_sfb_prms->num_window_groups[ch], pstr_sfb_prms->max_sfb_ste);
510
511
200k
    {
512
200k
      if ((ms_mask == 3) && (pstr_usac_data->stereo_config_index == 0)) {
513
11.1k
        bit_count += iusace_write_cplx_pred_data(
514
11.1k
            it_bit_buf, pstr_sfb_prms->num_window_groups[ch], pstr_sfb_prms->max_sfb_ste,
515
11.1k
            pstr_usac_data->complex_coef[ch], pstr_usac_data->pred_coef_re[ch],
516
11.1k
            pstr_usac_data->pred_coef_im[ch], iusace_huffman_code_table, usac_independency_flg,
517
11.1k
            pstr_usac_data->pred_dir_idx[ch], pstr_usac_data->cplx_pred_used[ch],
518
11.1k
            pstr_usac_data->cplx_pred_all[ch], pstr_usac_data->pred_coef_re_prev[ch],
519
11.1k
            pstr_usac_data->pred_coef_im_prev[ch], &pstr_usac_data->delta_code_time[ch]);
520
11.1k
      }
521
200k
    }
522
200k
  }
523
524
200k
  if (tns_active) {
525
50.7k
    WORD32 common_tns = 0;
526
50.7k
    WORD32 tns_on_lr = 1;
527
50.7k
    WORD32 tns_present_both = tns_data_present[0] && tns_data_present[1];
528
50.7k
    WORD32 tns_data_present1 = tns_data_present[1];
529
530
50.7k
    if (pstr_sfb_prms->common_win[ch]) {
531
50.7k
      iusace_write_bits_buf(it_bit_buf, common_tns, 1);
532
50.7k
      bit_count += 1;
533
50.7k
    }
534
535
50.7k
    iusace_write_bits_buf(it_bit_buf, tns_on_lr, 1);
536
50.7k
    bit_count += 1;
537
538
50.7k
    if (common_tns) {
539
0
      bit_count +=
540
0
          iusace_write_tns_data(it_bit_buf, pstr_tns_info, pstr_sfb_prms->window_sequence[ch], 0);
541
50.7k
    } else {
542
50.7k
      iusace_write_bits_buf(it_bit_buf, tns_present_both, 1);
543
50.7k
      bit_count += 1;
544
545
50.7k
      if (!tns_present_both) {
546
14.0k
        iusace_write_bits_buf(it_bit_buf, tns_data_present1, 1);
547
14.0k
        bit_count += 1;
548
14.0k
      }
549
50.7k
    }
550
50.7k
  }
551
552
200k
  return (bit_count);
553
200k
}
554
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
438k
                            WORD32 ele_id, WORD32 idx) {
560
438k
  WORD32 bit_count = 0;
561
438k
  WORD32 fac_data_present = (num_fac_bits > 0) ? 1 : 0;
562
438k
  WORD16 *ptr_fac_data = pstr_usac_data->fac_out_stream[ch_idx];
563
564
438k
  WORD32 is_noise_filling = pstr_usac_data->noise_filling[ele_id];
565
438k
  WORD32 common_window = pstr_sfb_prms->common_win[ch_idx];
566
438k
  ia_usac_quant_info_struct *pstr_quant_info = &(pstr_usac_data->str_quant_info[idx]);
567
438k
  ia_tns_info *pstr_tns_info = pstr_usac_data->pstr_tns_info[ch_idx];
568
438k
  WORD32 global_gain = pstr_usac_data->str_quant_info[idx].scale_factor[0];
569
570
438k
  iusace_write_bits_buf(it_bit_buf, global_gain, 8);
571
438k
  bit_count += 8;
572
573
438k
  if (is_noise_filling) {
574
188k
    iusace_write_bits_buf(it_bit_buf, pstr_usac_data->noise_level[idx], 3);
575
576
188k
    iusace_write_bits_buf(it_bit_buf, pstr_usac_data->noise_offset[idx], 5);
577
188k
    bit_count += 8;
578
188k
  }
579
580
438k
  if (!common_window) {
581
37.5k
    bit_count += iusace_write_ics_info(it_bit_buf, pstr_sfb_prms, ch_idx);
582
37.5k
  }
583
584
438k
  bit_count += iusace_write_scf_data(
585
438k
      it_bit_buf, pstr_sfb_prms->max_sfb[ch_idx], pstr_sfb_prms->num_sfb[ch_idx],
586
438k
      pstr_quant_info->scale_factor, pstr_sfb_prms->num_window_groups[ch_idx], global_gain,
587
438k
      iusace_huffman_code_table);
588
589
438k
  if (pstr_tns_info != NULL && pstr_tns_info->tns_data_present == 1) {
590
89.3k
    bit_count += iusace_write_tns_data(it_bit_buf, pstr_tns_info,
591
89.3k
                                       pstr_sfb_prms->window_sequence[ch_idx], 0);
592
89.3k
  }
593
594
438k
  if (!usac_independency_flg) {
595
417k
    iusace_write_bits_buf(it_bit_buf, pstr_quant_info->reset, 1);
596
417k
    bit_count += 1;
597
417k
  }
598
599
438k
  if (pstr_quant_info->max_spec_coeffs == FRAME_LEN_SHORT_768) {
600
4.68k
    pstr_quant_info->max_spec_coeffs = pstr_quant_info->max_spec_coeffs;
601
4.68k
  }
602
438k
  bit_count += iusace_arith_enc_spec(
603
438k
      it_bit_buf, pstr_sfb_prms->window_sequence[ch_idx], pstr_quant_info->quant_degroup,
604
438k
      pstr_quant_info->max_spec_coeffs, pstr_quant_info->c_pres, pstr_quant_info->c_prev,
605
438k
      &(pstr_quant_info->arith_size_prev), usac_independency_flg || pstr_quant_info->reset,
606
438k
      pstr_usac_config->ccfl);
607
608
438k
  iusace_write_bits_buf(it_bit_buf, fac_data_present, 1);
609
438k
  bit_count += 1;
610
611
438k
  if (fac_data_present) {
612
1.34k
    WORD32 i;
613
30.7k
    for (i = 0; i < num_fac_bits; i += 8) {
614
29.4k
      WORD32 bits_to_write = MIN(8, num_fac_bits - i);
615
29.4k
      iusace_write_bits_buf(it_bit_buf, ptr_fac_data[i / 8] >> (8 - bits_to_write),
616
29.4k
                            (UWORD8)bits_to_write);
617
29.4k
    }
618
1.34k
    bit_count += num_fac_bits;
619
1.34k
  }
620
621
438k
  return (bit_count);
622
438k
}
623
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
507k
                            WORD32 idx) {
628
507k
  WORD32 bit_count = 0;
629
507k
  ia_usac_quant_info_struct *pstr_quant_info = &pstr_usac_data->str_quant_info[idx];
630
507k
  WORD32 window_sequence = pstr_sfb_prms->window_sequence[ch_idx];
631
507k
  WORD32 global_gain = pstr_quant_info->scale_factor[0];
632
507k
  WORD32 max_sfb = pstr_sfb_prms->max_sfb[ch_idx];
633
507k
  WORD32 num_sfb = pstr_sfb_prms->num_sfb[ch_idx];
634
507k
  WORD32 num_win_grps = pstr_sfb_prms->num_window_groups[ch_idx];
635
636
507k
  bit_count += iusace_write_scf_data(NULL, max_sfb, num_sfb, pstr_quant_info->scale_factor,
637
507k
                                     num_win_grps, global_gain, iusace_huffman_code_table);
638
639
507k
  WORD32 temp_c_pres[516], temp_c_prev[516], temp_size = pstr_quant_info->arith_size_prev;
640
507k
  memcpy(temp_c_pres, pstr_quant_info->c_pres, 516 * sizeof(pstr_quant_info->c_pres[0]));
641
507k
  memcpy(temp_c_prev, pstr_quant_info->c_prev, 516 * sizeof(pstr_quant_info->c_prev[0]));
642
507k
  bit_count += iusace_arith_enc_spec(
643
507k
      NULL, window_sequence, pstr_quant_info->quant_degroup, pstr_quant_info->max_spec_coeffs,
644
507k
      temp_c_pres, temp_c_prev, &(temp_size), usac_independency_flg || pstr_quant_info->reset,
645
507k
      pstr_usac_config->ccfl);
646
647
507k
  return (bit_count);
648
507k
}
649
650
340k
WORD32 iusace_write_fill_ele(ia_bit_buf_struct *it_bit_buf, WORD32 num_bits) {
651
340k
  WORD32 write_flag = (it_bit_buf != NULL);
652
340k
  WORD32 bit_count = 0;
653
654
340k
  if (num_bits <= 8) {
655
127k
    if (write_flag) {
656
127k
      iusace_write_bits_buf(it_bit_buf, 0, 1);
657
127k
    }
658
127k
    bit_count++;
659
127k
    num_bits--;
660
212k
  } else {
661
212k
    if (write_flag) {
662
212k
      iusace_write_bits_buf(it_bit_buf, 1, 1);
663
212k
    }
664
212k
    bit_count++;
665
212k
    num_bits--;
666
667
212k
    if (num_bits <= 8) {
668
41
      if (write_flag) {
669
41
        iusace_write_bits_buf(it_bit_buf, 1, 1);
670
41
      }
671
41
      bit_count++;
672
41
      num_bits--;
673
212k
    } else {
674
212k
      WORD32 bytes_to_write = 0;
675
212k
      if (write_flag) {
676
212k
        iusace_write_bits_buf(it_bit_buf, 0, 1);
677
212k
      }
678
212k
      bit_count++;
679
212k
      num_bits--;
680
212k
      bytes_to_write = num_bits >> 3;
681
682
212k
      if (bytes_to_write > 255) {
683
141k
        bytes_to_write -= 3;
684
141k
        if (write_flag) {
685
141k
          iusace_write_bits_buf(it_bit_buf, 255, 8);
686
141k
        }
687
141k
        if (write_flag) {
688
141k
          iusace_write_bits_buf(it_bit_buf, bytes_to_write - 253, 16);
689
141k
        }
690
141k
        bit_count += 24;
691
141k
        num_bits -= 24;
692
141k
      } else {
693
70.6k
        bytes_to_write--;
694
70.6k
        if (write_flag) {
695
70.6k
          iusace_write_bits_buf(it_bit_buf, bytes_to_write, 8);
696
70.6k
        }
697
70.6k
        bit_count += 8;
698
70.6k
        num_bits -= 8;
699
70.6k
      }
700
701
90.1M
      while (bytes_to_write > 0) {
702
89.9M
        if (write_flag) {
703
89.9M
          iusace_write_bits_buf(it_bit_buf, 0xA9, 8);
704
89.9M
        }
705
89.9M
        bit_count += 8;
706
89.9M
        num_bits -= 8;
707
89.9M
        bytes_to_write--;
708
89.9M
      }
709
212k
    }
710
212k
  }
711
340k
  return bit_count;
712
340k
}