Coverage Report

Created: 2026-09-01 06:32

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.90M
  for (j = 0; j < num_win_grps; j++) {
66
40.6M
    for (i = 0; i < max_sfb; i++) {
67
38.6M
      if (!((i == 0) && (j == 0))) {
68
37.7M
        diff = ptr_scale_factors[index] - previous_scale_factor;
69
37.7M
        length = huff_tab[diff + 60][0];
70
37.7M
        bit_count += length;
71
37.7M
        previous_scale_factor = ptr_scale_factors[index];
72
37.7M
        if (write_flag == 1) {
73
17.2M
          codeword = huff_tab[diff + 60][1];
74
17.2M
          iusace_write_bits_buf(it_bit_buf, codeword, (UWORD8)length);
75
17.2M
        }
76
37.7M
      }
77
38.6M
      index++;
78
38.6M
    }
79
7.38M
    for (; i < num_sfb; i++) {
80
5.42M
      index++;
81
5.42M
    }
82
1.96M
  }
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
182k
                            WORD32 nr_of_sfb) {
90
182k
  WORD32 write_flag = (it_bit_buf != NULL);
91
182k
  WORD32 bit_count = 0;
92
182k
  WORD32 i, j;
93
182k
  WORD32 ms_mask_write = ms_mask;
94
95
182k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, ms_mask_write, 2);
96
182k
  bit_count += 2;
97
98
182k
  if (ms_mask_write == 1) {
99
87.8k
    for (i = 0; i < num_win_grps; i++) {
100
1.29M
      for (j = 0; j < nr_of_sfb; j++) {
101
1.23M
        if (write_flag) iusace_write_bits_buf(it_bit_buf, ms_used[i][j], 1);
102
1.23M
        bit_count += 1;
103
1.23M
      }
104
62.6k
    }
105
25.2k
  }
106
107
182k
  return bit_count;
108
182k
}
109
110
WORD32 iusace_write_tns_data(ia_bit_buf_struct *it_bit_buf, ia_tns_info *pstr_tns_info,
111
219k
                             WORD32 window_sequence, WORD32 core_mode) {
112
219k
  WORD32 write_flag = (it_bit_buf != NULL);
113
219k
  WORD32 bit_count = 0;
114
219k
  WORD32 num_windows;
115
219k
  WORD32 len_tns_nfilt;
116
219k
  WORD32 len_tns_length;
117
219k
  WORD32 len_tns_order;
118
219k
  WORD32 filt;
119
219k
  WORD32 res_bits;
120
219k
  UWORD32 coeff;
121
219k
  WORD32 w;
122
123
219k
  if (window_sequence == EIGHT_SHORT_SEQUENCE) {
124
26.6k
    num_windows = MAX_SHORT_WINDOWS;
125
26.6k
    len_tns_nfilt = 1;
126
26.6k
    len_tns_length = 4;
127
26.6k
    len_tns_order = 3;
128
193k
  } else {
129
193k
    num_windows = 1;
130
193k
    len_tns_nfilt = 2;
131
193k
    len_tns_length = 6;
132
193k
    len_tns_order = 4;
133
193k
  }
134
219k
  if (core_mode == 1) {
135
0
    num_windows = 1;
136
0
  }
137
138
626k
  for (w = 0; w < num_windows; w++) {
139
406k
    ia_tns_window_data *ptr_win_data = &pstr_tns_info->window_data[w];
140
406k
    WORD32 n_filt = ptr_win_data->n_filt;
141
406k
    if (write_flag) {
142
201k
      iusace_write_bits_buf(it_bit_buf, n_filt, (UWORD8)len_tns_nfilt);
143
201k
    }
144
406k
    bit_count += len_tns_nfilt;
145
406k
    if (n_filt) {
146
320k
      res_bits = ptr_win_data->coef_res;
147
320k
      if (write_flag) {
148
158k
        iusace_write_bits_buf(it_bit_buf, res_bits - DEF_TNS_RES_OFFSET, 1);
149
158k
      }
150
320k
      bit_count += 1;
151
640k
      for (filt = 0; filt < n_filt; filt++) {
152
320k
        ia_tns_filter_data *ptr_tns_filt = &ptr_win_data->tns_filter[filt];
153
320k
        WORD32 order = ptr_tns_filt->order;
154
320k
        if (write_flag) {
155
158k
          iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->length, (UWORD8)len_tns_length);
156
158k
          iusace_write_bits_buf(it_bit_buf, order, (UWORD8)len_tns_order);
157
158k
        }
158
320k
        bit_count += (len_tns_length + len_tns_order);
159
320k
        if (order) {
160
319k
          WORD32 i;
161
319k
          if (write_flag) {
162
157k
            iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->direction, 1);
163
157k
            iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->coef_compress, 1);
164
157k
          }
165
319k
          bit_count += 2;
166
2.42M
          for (i = 1; i <= order; i++) {
167
2.10M
            if (write_flag) {
168
1.03M
              coeff = (UWORD32)(ptr_tns_filt->index[i]) & ((1 << res_bits) - 1);
169
1.03M
              iusace_write_bits_buf(it_bit_buf, coeff, (UWORD8)res_bits);
170
1.03M
            }
171
2.10M
            bit_count += res_bits;
172
2.10M
          }
173
319k
        }
174
320k
      }
175
320k
    }
176
406k
  }
177
178
219k
  return bit_count;
179
219k
}
180
181
74.3k
static WORD32 iusace_calc_grouping_bits(const WORD32 *ptr_win_grp_len, WORD32 num_win_grps) {
182
74.3k
  WORD32 grouping_bits = 0;
183
74.3k
  WORD32 tmp[8] = {0};
184
74.3k
  WORD32 i, j;
185
74.3k
  WORD32 index = 0;
186
187
360k
  for (i = 0; i < num_win_grps; i++) {
188
880k
    for (j = 0; j < ptr_win_grp_len[i]; j++) {
189
594k
      tmp[index++] = i;
190
594k
    }
191
285k
  }
192
193
594k
  for (i = 1; i < 8; i++) {
194
520k
    grouping_bits = grouping_bits << 1;
195
520k
    if (tmp[i] == tmp[i - 1]) {
196
309k
      grouping_bits++;
197
309k
    }
198
520k
  }
199
200
74.3k
  return (grouping_bits);
201
74.3k
}
202
203
WORD32 iusace_write_ics_info(ia_bit_buf_struct *it_bit_buf, ia_sfb_params_struct *pstr_sfb_prms,
204
233k
                             WORD32 ch) {
205
233k
  WORD32 write_flag = (it_bit_buf != NULL);
206
233k
  WORD32 bit_count = 0;
207
233k
  WORD32 win_seq = 0;
208
233k
  WORD32 grouping_bits = 0;
209
233k
  WORD32 max_sfb = pstr_sfb_prms->max_sfb[ch];
210
233k
  WORD32 window_sequence = pstr_sfb_prms->window_sequence[ch];
211
233k
  WORD32 window_shape = pstr_sfb_prms->window_shape[ch];
212
233k
  WORD32 num_win_grps = pstr_sfb_prms->num_window_groups[ch];
213
214
233k
  switch (window_sequence) {
215
74.3k
    case EIGHT_SHORT_SEQUENCE:
216
74.3k
      win_seq = 2;
217
74.3k
      break;
218
110k
    case ONLY_LONG_SEQUENCE:
219
110k
      win_seq = 0;
220
110k
      break;
221
26.3k
    case LONG_START_SEQUENCE:
222
26.5k
    case STOP_START_SEQUENCE:
223
26.5k
      win_seq = 1;
224
26.5k
      break;
225
23.0k
    case LONG_STOP_SEQUENCE:
226
23.0k
      win_seq = 3;
227
23.0k
      break;
228
0
    default:
229
0
      win_seq = 3;
230
0
      break;
231
233k
  }
232
233k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, win_seq, 2);
233
233k
  bit_count += 2;
234
235
233k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, window_shape, 1);
236
233k
  bit_count += 1;
237
238
233k
  if (window_sequence == EIGHT_SHORT_SEQUENCE) {
239
74.3k
    if (write_flag) iusace_write_bits_buf(it_bit_buf, max_sfb, 4);
240
74.3k
    bit_count += 4;
241
242
74.3k
    grouping_bits =
243
74.3k
        iusace_calc_grouping_bits(pstr_sfb_prms->window_group_length[ch], num_win_grps);
244
74.3k
    if (write_flag) iusace_write_bits_buf(it_bit_buf, grouping_bits, 7);
245
74.3k
    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
233k
  return (bit_count);
252
233k
}
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
26.0k
                                   WORD32 *ptr_prev_alpha_coeff_im, WORD32 *delta_code_time) {
263
26.0k
  WORD32 write_flag = (it_bit_buf != NULL);
264
26.0k
  WORD32 bit_count = 0;
265
26.0k
  WORD32 i, j;
266
26.0k
  WORD32 g;
267
26.0k
  WORD32 sfb;
268
26.0k
  const WORD32 sfb_per_pred_band = 2;
269
26.0k
  WORD32 length_temp1_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
270
26.0k
      length_temp2_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
271
26.0k
      length_temp1_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
272
26.0k
      length_temp2_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG];
273
26.0k
  WORD32 code_word_temp1_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
274
26.0k
      code_word_temp2_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
275
26.0k
      code_word_temp1_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG],
276
26.0k
      code_word_temp2_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG];
277
26.0k
  WORD32 length_tot1 = 0, length_tot2 = 0;
278
279
26.0k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, cplx_pred_all, 1);
280
26.0k
  bit_count += 1;
281
282
26.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
26.0k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, pred_dir, 1);
292
26.0k
  bit_count += 1;
293
294
26.0k
  if (write_flag) iusace_write_bits_buf(it_bit_buf, complex_coef, 1);
295
26.0k
  bit_count += 1;
296
297
26.0k
  if (complex_coef) {
298
94
    if (!usac_independency_flg) {
299
84
      if (write_flag) iusace_write_bits_buf(it_bit_buf, 1, 1); /* use_prev_frame */
300
84
      bit_count += 1;
301
84
    }
302
94
  }
303
304
26.0k
  if (usac_independency_flg) {
305
636
    *delta_code_time = 0;
306
636
  }
307
308
  /* Switching mechanism for delta_code_time */
309
26.0k
  WORD32 prev_pred_coeff_re_temp1 = 0, prev_pred_coeff_re_temp2 = 0;
310
26.0k
  WORD32 diff_pred_coeff_re_temp1 = 0, diff_pred_coeff_re_temp2 = 0;
311
26.0k
  WORD32 prev_pred_coeff_im_temp1 = 0, prev_pred_coeff_im_temp2 = 0;
312
26.0k
  WORD32 diff_pred_coeff_im_temp1 = 0, diff_pred_coeff_im_temp2 = 0;
313
314
92.0k
  for (i = 0; i < num_win_grps; i++) {
315
    /* delta_code_time = 0*/
316
66.0k
    prev_pred_coeff_re_temp1 = 0;
317
66.0k
    if (complex_coef == 1) {
318
250
      prev_pred_coeff_im_temp1 = 0;
319
250
    }
320
321
521k
    for (j = 0; j < num_sfb; j += 2) {
322
455k
      if (!usac_independency_flg) {
323
        /* delta_code_time = 1*/
324
439k
        if (i > 0) {
325
181k
          prev_pred_coeff_re_temp2 = pred_coeffs_re[i - 1][j];
326
181k
          if (complex_coef == 1) {
327
894
            prev_pred_coeff_im_temp2 = pred_coeffs_im[i - 1][j];
328
894
          }
329
258k
        } else {
330
258k
          prev_pred_coeff_re_temp2 = ptr_prev_alpha_coeff_re[j];
331
258k
          if (complex_coef == 1) {
332
1.18k
            prev_pred_coeff_im_temp2 = ptr_prev_alpha_coeff_im[j];
333
1.18k
          }
334
258k
        }
335
439k
      }
336
337
455k
      if (cplx_pred_used[i][j] == 1) {
338
        /*Differential Huffman coding of real prediction coefficients*/
339
455k
        diff_pred_coeff_re_temp1 =
340
455k
            pred_coeffs_re[i][j] - prev_pred_coeff_re_temp1; /* delta_code_time = 0 */
341
455k
        prev_pred_coeff_re_temp1 = pred_coeffs_re[i][j];     /* delta_code_time = 0 */
342
455k
        if (!usac_independency_flg) {
343
439k
          diff_pred_coeff_re_temp2 =
344
439k
              pred_coeffs_re[i][j] - prev_pred_coeff_re_temp2; /* delta_code_time = 1 */
345
439k
        }
346
347
        /* delta_code_time = 0 */
348
455k
        length_temp1_re[i][j] = huff_tab[diff_pred_coeff_re_temp1 + 60][0];
349
455k
        code_word_temp1_re[i][j] = huff_tab[diff_pred_coeff_re_temp1 + 60][1];
350
351
455k
        length_tot1 += length_temp1_re[i][j];
352
353
455k
        if (!usac_independency_flg) {
354
          /*delta_code_time = 1 */
355
439k
          length_temp2_re[i][j] = huff_tab[diff_pred_coeff_re_temp2 + 60][0];
356
439k
          code_word_temp2_re[i][j] = huff_tab[diff_pred_coeff_re_temp2 + 60][1];
357
358
439k
          length_tot2 += length_temp2_re[i][j];
359
439k
        }
360
361
455k
        if (complex_coef == 1) {
362
          /*Differential Huffman coding of imaginary prediction coefficients*/
363
2.39k
          diff_pred_coeff_im_temp1 =
364
2.39k
              pred_coeffs_im[i][j] - prev_pred_coeff_im_temp1; /* delta_code_time = 0 */
365
2.39k
          prev_pred_coeff_im_temp1 = pred_coeffs_im[i][j];     /* delta_code_time = 0*/
366
367
2.39k
          if (!usac_independency_flg) {
368
2.07k
            diff_pred_coeff_im_temp2 =
369
2.07k
                pred_coeffs_im[i][j] - prev_pred_coeff_im_temp2; /* delta_code_time = 1 */
370
2.07k
          }
371
372
          /*delta_code_time = 0*/
373
2.39k
          length_temp1_im[i][j] = huff_tab[diff_pred_coeff_im_temp1 + 60][0];
374
2.39k
          code_word_temp1_im[i][j] = huff_tab[diff_pred_coeff_im_temp1 + 60][1];
375
376
2.39k
          length_tot1 += length_temp1_im[i][j];
377
378
2.39k
          if (!usac_independency_flg) {
379
            /*delta_code_time = 1*/
380
2.07k
            length_temp2_im[i][j] = huff_tab[diff_pred_coeff_im_temp2 + 60][0];
381
2.07k
            code_word_temp2_im[i][j] = huff_tab[diff_pred_coeff_im_temp2 + 60][1];
382
383
2.07k
            length_tot2 += length_temp2_im[i][j];
384
2.07k
          }
385
2.39k
        }
386
455k
      } 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
455k
      ptr_prev_alpha_coeff_re[j] = pred_coeffs_re[i][j];
398
455k
      if (complex_coef == 1) {
399
2.39k
        ptr_prev_alpha_coeff_im[j] = pred_coeffs_im[i][j];
400
2.39k
      }
401
455k
    }
402
403
2.53M
    for (j = num_sfb; j < MAX_SFB_LONG; j++) {
404
2.47M
      pred_coeffs_re[i][j] = 0;
405
2.47M
      ptr_prev_alpha_coeff_re[j] = 0;
406
2.47M
      if (complex_coef == 1) {
407
8.13k
        pred_coeffs_im[i][j] = 0;
408
8.13k
        ptr_prev_alpha_coeff_im[j] = 0;
409
8.13k
      }
410
2.47M
    }
411
66.0k
  }
412
413
  /*Make a decison on the value of delta_code_time per frame */
414
26.0k
  if (!usac_independency_flg) {
415
    // Compare the code-word lengths
416
25.4k
    if (length_tot1 <= length_tot2) {
417
13.3k
      *delta_code_time = 0;
418
13.3k
    } else {
419
12.1k
      *delta_code_time = 1;
420
12.1k
    }
421
422
    /* Write the value of delta_code_time to bitstream */
423
25.4k
    if (write_flag) iusace_write_bits_buf(it_bit_buf, *delta_code_time, 1);
424
25.4k
    bit_count += 1;
425
25.4k
  }
426
427
26.0k
  if (*delta_code_time == 0) {
428
52.5k
    for (i = 0; i < num_win_grps; i++) {
429
313k
      for (j = 0; j < num_sfb; j += 2) {
430
275k
        if (cplx_pred_used[i][j] == 1) {
431
275k
          if (write_flag)
432
107k
            iusace_write_bits_buf(it_bit_buf, code_word_temp1_re[i][j],
433
107k
                                  (UWORD8)length_temp1_re[i][j]);
434
275k
          bit_count += length_temp1_re[i][j];
435
436
275k
          if (complex_coef == 1) {
437
1.92k
            if (write_flag)
438
876
              iusace_write_bits_buf(it_bit_buf, code_word_temp1_im[i][j],
439
876
                                    (UWORD8)length_temp1_im[i][j]);
440
1.92k
            bit_count += length_temp1_im[i][j];
441
1.92k
          }
442
275k
        }
443
275k
      }
444
38.5k
    }
445
13.9k
  } else {
446
39.5k
    for (i = 0; i < num_win_grps; i++) {
447
207k
      for (j = 0; j < num_sfb; j += 2) {
448
180k
        if (cplx_pred_used[i][j] == 1) {
449
180k
          if (write_flag)
450
69.3k
            iusace_write_bits_buf(it_bit_buf, code_word_temp2_re[i][j],
451
69.3k
                                  (UWORD8)length_temp2_re[i][j]);
452
180k
          bit_count += length_temp2_re[i][j];
453
454
180k
          if (complex_coef == 1) {
455
476
            if (write_flag)
456
226
              iusace_write_bits_buf(it_bit_buf, code_word_temp2_im[i][j],
457
226
                                    (UWORD8)length_temp2_im[i][j]);
458
476
            bit_count += length_temp2_im[i][j];
459
476
          }
460
180k
        }
461
180k
      }
462
27.4k
    }
463
12.1k
  }
464
465
26.0k
  return bit_count;
466
26.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
182k
                        ia_usac_data_struct *pstr_usac_data, WORD32 ch) {
472
182k
  WORD32 bit_count = 0;
473
182k
  WORD32 ms_mask = pstr_usac_data->str_ms_info[ch].ms_mask;
474
182k
  WORD32 common_max_sfb = 1;
475
182k
  WORD32 tns_active = tns_data_present[0] || tns_data_present[1];
476
182k
  ia_tns_info *pstr_tns_info = pstr_usac_data->pstr_tns_info[ch];
477
182k
  (VOID) pstr_usac_config;
478
479
182k
  iusace_write_bits_buf(it_bit_buf, tns_active, 1);
480
182k
  bit_count += 1;
481
482
182k
  iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->common_win[ch], 1);
483
182k
  bit_count += 1;
484
485
182k
  if (pstr_sfb_prms->max_sfb[ch] != pstr_sfb_prms->max_sfb[ch + 1]) {
486
1.35k
    common_max_sfb = 0;
487
1.35k
  }
488
489
182k
  if (pstr_sfb_prms->common_win[ch]) {
490
182k
    bit_count += iusace_write_ics_info(it_bit_buf, pstr_sfb_prms, ch);
491
492
182k
    iusace_write_bits_buf(it_bit_buf, common_max_sfb, 1);
493
182k
    bit_count += 1;
494
495
182k
    if (common_max_sfb == 0) {
496
1.35k
      if (pstr_sfb_prms->window_sequence[ch] != EIGHT_SHORT_SEQUENCE) {
497
650
        iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->max_sfb[ch + 1], 6);
498
650
        bit_count += 6;
499
704
      } else {
500
704
        iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->max_sfb[ch + 1], 4);
501
704
        bit_count += 4;
502
704
      }
503
1.35k
    }
504
505
182k
    pstr_sfb_prms->max_sfb_ste = MAX(pstr_sfb_prms->max_sfb[ch], pstr_sfb_prms->max_sfb[ch + 1]);
506
507
182k
    bit_count +=
508
182k
        iusace_write_ms_data(it_bit_buf, ms_mask, pstr_usac_data->str_ms_info[ch].ms_used,
509
182k
                             pstr_sfb_prms->num_window_groups[ch], pstr_sfb_prms->max_sfb_ste);
510
511
182k
    {
512
182k
      if ((ms_mask == 3) && (pstr_usac_data->stereo_config_index == 0)) {
513
9.86k
        bit_count += iusace_write_cplx_pred_data(
514
9.86k
            it_bit_buf, pstr_sfb_prms->num_window_groups[ch], pstr_sfb_prms->max_sfb_ste,
515
9.86k
            pstr_usac_data->complex_coef[ch], pstr_usac_data->pred_coef_re[ch],
516
9.86k
            pstr_usac_data->pred_coef_im[ch], iusace_huffman_code_table, usac_independency_flg,
517
9.86k
            pstr_usac_data->pred_dir_idx[ch], pstr_usac_data->cplx_pred_used[ch],
518
9.86k
            pstr_usac_data->cplx_pred_all[ch], pstr_usac_data->pred_coef_re_prev[ch],
519
9.86k
            pstr_usac_data->pred_coef_im_prev[ch], &pstr_usac_data->delta_code_time[ch]);
520
9.86k
      }
521
182k
    }
522
182k
  }
523
524
182k
  if (tns_active) {
525
55.7k
    WORD32 common_tns = 0;
526
55.7k
    WORD32 tns_on_lr = 1;
527
55.7k
    WORD32 tns_present_both = tns_data_present[0] && tns_data_present[1];
528
55.7k
    WORD32 tns_data_present1 = tns_data_present[1];
529
530
55.7k
    if (pstr_sfb_prms->common_win[ch]) {
531
55.7k
      iusace_write_bits_buf(it_bit_buf, common_tns, 1);
532
55.7k
      bit_count += 1;
533
55.7k
    }
534
535
55.7k
    iusace_write_bits_buf(it_bit_buf, tns_on_lr, 1);
536
55.7k
    bit_count += 1;
537
538
55.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
55.7k
    } else {
542
55.7k
      iusace_write_bits_buf(it_bit_buf, tns_present_both, 1);
543
55.7k
      bit_count += 1;
544
545
55.7k
      if (!tns_present_both) {
546
11.9k
        iusace_write_bits_buf(it_bit_buf, tns_data_present1, 1);
547
11.9k
        bit_count += 1;
548
11.9k
      }
549
55.7k
    }
550
55.7k
  }
551
552
182k
  return (bit_count);
553
182k
}
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
416k
                            WORD32 ele_id, WORD32 idx) {
560
416k
  WORD32 bit_count = 0;
561
416k
  WORD32 fac_data_present = (num_fac_bits > 0) ? 1 : 0;
562
416k
  WORD16 *ptr_fac_data = pstr_usac_data->fac_out_stream[ch_idx];
563
564
416k
  WORD32 is_noise_filling = pstr_usac_data->noise_filling[ele_id];
565
416k
  WORD32 common_window = pstr_sfb_prms->common_win[ch_idx];
566
416k
  ia_usac_quant_info_struct *pstr_quant_info = &(pstr_usac_data->str_quant_info[idx]);
567
416k
  ia_tns_info *pstr_tns_info = pstr_usac_data->pstr_tns_info[ch_idx];
568
416k
  WORD32 global_gain = pstr_usac_data->str_quant_info[idx].scale_factor[0];
569
570
416k
  iusace_write_bits_buf(it_bit_buf, global_gain, 8);
571
416k
  bit_count += 8;
572
573
416k
  if (is_noise_filling) {
574
210k
    iusace_write_bits_buf(it_bit_buf, pstr_usac_data->noise_level[idx], 3);
575
576
210k
    iusace_write_bits_buf(it_bit_buf, pstr_usac_data->noise_offset[idx], 5);
577
210k
    bit_count += 8;
578
210k
  }
579
580
416k
  if (!common_window) {
581
51.3k
    bit_count += iusace_write_ics_info(it_bit_buf, pstr_sfb_prms, ch_idx);
582
51.3k
  }
583
584
416k
  bit_count += iusace_write_scf_data(
585
416k
      it_bit_buf, pstr_sfb_prms->max_sfb[ch_idx], pstr_sfb_prms->num_sfb[ch_idx],
586
416k
      pstr_quant_info->scale_factor, pstr_sfb_prms->num_window_groups[ch_idx], global_gain,
587
416k
      iusace_huffman_code_table);
588
589
416k
  if (pstr_tns_info != NULL && pstr_tns_info->tns_data_present == 1) {
590
107k
    bit_count += iusace_write_tns_data(it_bit_buf, pstr_tns_info,
591
107k
                                       pstr_sfb_prms->window_sequence[ch_idx], 0);
592
107k
  }
593
594
416k
  if (!usac_independency_flg) {
595
396k
    iusace_write_bits_buf(it_bit_buf, pstr_quant_info->reset, 1);
596
396k
    bit_count += 1;
597
396k
  }
598
599
416k
  if (pstr_quant_info->max_spec_coeffs == FRAME_LEN_SHORT_768) {
600
5.16k
    pstr_quant_info->max_spec_coeffs = pstr_quant_info->max_spec_coeffs;
601
5.16k
  }
602
416k
  bit_count += iusace_arith_enc_spec(
603
416k
      it_bit_buf, pstr_sfb_prms->window_sequence[ch_idx], pstr_quant_info->quant_degroup,
604
416k
      pstr_quant_info->max_spec_coeffs, pstr_quant_info->c_pres, pstr_quant_info->c_prev,
605
416k
      &(pstr_quant_info->arith_size_prev), usac_independency_flg || pstr_quant_info->reset,
606
416k
      pstr_usac_config->ccfl);
607
608
416k
  iusace_write_bits_buf(it_bit_buf, fac_data_present, 1);
609
416k
  bit_count += 1;
610
611
416k
  if (fac_data_present) {
612
1.75k
    WORD32 i;
613
40.2k
    for (i = 0; i < num_fac_bits; i += 8) {
614
38.4k
      WORD32 bits_to_write = MIN(8, num_fac_bits - i);
615
38.4k
      iusace_write_bits_buf(it_bit_buf, ptr_fac_data[i / 8] >> (8 - bits_to_write),
616
38.4k
                            (UWORD8)bits_to_write);
617
38.4k
    }
618
1.75k
    bit_count += num_fac_bits;
619
1.75k
  }
620
621
416k
  return (bit_count);
622
416k
}
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
529k
                            WORD32 idx) {
628
529k
  WORD32 bit_count = 0;
629
529k
  ia_usac_quant_info_struct *pstr_quant_info = &pstr_usac_data->str_quant_info[idx];
630
529k
  WORD32 window_sequence = pstr_sfb_prms->window_sequence[ch_idx];
631
529k
  WORD32 global_gain = pstr_quant_info->scale_factor[0];
632
529k
  WORD32 max_sfb = pstr_sfb_prms->max_sfb[ch_idx];
633
529k
  WORD32 num_sfb = pstr_sfb_prms->num_sfb[ch_idx];
634
529k
  WORD32 num_win_grps = pstr_sfb_prms->num_window_groups[ch_idx];
635
636
529k
  bit_count += iusace_write_scf_data(NULL, max_sfb, num_sfb, pstr_quant_info->scale_factor,
637
529k
                                     num_win_grps, global_gain, iusace_huffman_code_table);
638
639
529k
  WORD32 temp_c_pres[516], temp_c_prev[516], temp_size = pstr_quant_info->arith_size_prev;
640
529k
  memcpy(temp_c_pres, pstr_quant_info->c_pres, 516 * sizeof(pstr_quant_info->c_pres[0]));
641
529k
  memcpy(temp_c_prev, pstr_quant_info->c_prev, 516 * sizeof(pstr_quant_info->c_prev[0]));
642
529k
  bit_count += iusace_arith_enc_spec(
643
529k
      NULL, window_sequence, pstr_quant_info->quant_degroup, pstr_quant_info->max_spec_coeffs,
644
529k
      temp_c_pres, temp_c_prev, &(temp_size), usac_independency_flg || pstr_quant_info->reset,
645
529k
      pstr_usac_config->ccfl);
646
647
529k
  return (bit_count);
648
529k
}
649
650
330k
WORD32 iusace_write_fill_ele(ia_bit_buf_struct *it_bit_buf, WORD32 num_bits) {
651
330k
  WORD32 write_flag = (it_bit_buf != NULL);
652
330k
  WORD32 bit_count = 0;
653
654
330k
  if (num_bits <= 8) {
655
114k
    if (write_flag) {
656
114k
      iusace_write_bits_buf(it_bit_buf, 0, 1);
657
114k
    }
658
114k
    bit_count++;
659
114k
    num_bits--;
660
216k
  } else {
661
216k
    if (write_flag) {
662
216k
      iusace_write_bits_buf(it_bit_buf, 1, 1);
663
216k
    }
664
216k
    bit_count++;
665
216k
    num_bits--;
666
667
216k
    if (num_bits <= 8) {
668
153
      if (write_flag) {
669
153
        iusace_write_bits_buf(it_bit_buf, 1, 1);
670
153
      }
671
153
      bit_count++;
672
153
      num_bits--;
673
216k
    } else {
674
216k
      WORD32 bytes_to_write = 0;
675
216k
      if (write_flag) {
676
216k
        iusace_write_bits_buf(it_bit_buf, 0, 1);
677
216k
      }
678
216k
      bit_count++;
679
216k
      num_bits--;
680
216k
      bytes_to_write = num_bits >> 3;
681
682
216k
      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
74.1k
        bytes_to_write--;
694
74.1k
        if (write_flag) {
695
74.1k
          iusace_write_bits_buf(it_bit_buf, bytes_to_write, 8);
696
74.1k
        }
697
74.1k
        bit_count += 8;
698
74.1k
        num_bits -= 8;
699
74.1k
      }
700
701
88.7M
      while (bytes_to_write > 0) {
702
88.5M
        if (write_flag) {
703
88.5M
          iusace_write_bits_buf(it_bit_buf, 0xA9, 8);
704
88.5M
        }
705
88.5M
        bit_count += 8;
706
88.5M
        num_bits -= 8;
707
88.5M
        bytes_to_write--;
708
88.5M
      }
709
216k
    }
710
216k
  }
711
330k
  return bit_count;
712
330k
}