Coverage Report

Created: 2025-07-12 07:02

/src/libxaac/encoder/ixheaace_write_bitstream.c
Line
Count
Source (jump to first uncovered line)
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 <limits.h>
22
#include <stddef.h>
23
#include "ixheaac_type_def.h"
24
#include "ixheaac_constants.h"
25
#include "impd_drc_common_enc.h"
26
#include "impd_drc_uni_drc.h"
27
#include "impd_drc_tables.h"
28
#include "impd_drc_api.h"
29
#include "ixheaace_api.h"
30
#include "ixheaace_aac_constants.h"
31
#include "ixheaac_error_standards.h"
32
#include "ixheaace_error_codes.h"
33
#include <stdlib.h>
34
35
#include "ixheaac_basic_ops32.h"
36
#include "ixheaac_basic_ops16.h"
37
#include "ixheaac_basic_ops40.h"
38
#include "ixheaac_basic_ops.h"
39
40
#include "ixheaace_bitbuffer.h"
41
#include "ixheaace_psy_const.h"
42
#include "ixheaace_tns.h"
43
#include "ixheaace_tns_params.h"
44
#include "ixheaace_rom.h"
45
#include "ixheaace_common_rom.h"
46
#include "ixheaace_block_switch.h"
47
#include "ixheaace_psy_data.h"
48
#include "ixheaace_interface.h"
49
#include "ixheaace_bits_count.h"
50
#include "ixheaace_dynamic_bits.h"
51
#include "ixheaace_adjust_threshold_data.h"
52
#include "ixheaace_qc_data.h"
53
#include "ixheaace_write_bitstream.h"
54
#include "ixheaace_sbr_header.h"
55
#include "ixheaace_config.h"
56
57
#include "ixheaace_channel_map.h"
58
#include "ixheaace_write_adts_adif.h"
59
60
static VOID PLATFORM_INLINE
61
ia_enhaacplus_enc_encode_gain_control_data(ixheaace_bit_buf_handle pstr_bit_stream_handle)
62
63
350k
{
64
350k
  ixheaace_write_bits(pstr_bit_stream_handle, 0, 1);
65
350k
}
66
static WORD32 ia_enhaacplus_enc_encode_spectral_data(
67
    WORD32 *pstr_sfb_offset, ixheaace_section_data *pstr_section_data, WORD16 *ptr_quant_spectrum,
68
527k
    ixheaace_bit_buf_handle pstr_bit_stream_handle, ixheaace_huffman_tables *ptr_huffman_tbl) {
69
527k
  WORD32 i, sfb;
70
527k
  WORD32 dbg_val;
71
72
527k
  dbg_val = ia_enhaacplus_enc_get_bits_available(pstr_bit_stream_handle);
73
74
2.47M
  for (i = 0; i < pstr_section_data->num_of_sections; i++) {
75
1.94M
    for (sfb = pstr_section_data->section[i].sfb_start;
76
17.3M
         sfb < pstr_section_data->section[i].sfb_start + pstr_section_data->section[i].sfb_cnt;
77
15.3M
         sfb++) {
78
15.3M
      ia_enhaacplus_enc_code_values(ptr_quant_spectrum + pstr_sfb_offset[sfb],
79
15.3M
                                    pstr_sfb_offset[sfb + 1] - pstr_sfb_offset[sfb],
80
15.3M
                                    pstr_section_data->section[i].code_book,
81
15.3M
                                    pstr_bit_stream_handle, ptr_huffman_tbl);
82
15.3M
    }
83
1.94M
  }
84
85
527k
  return (ia_enhaacplus_enc_get_bits_available(pstr_bit_stream_handle) - dbg_val);
86
527k
}
87
88
static VOID ia_enhaacplus_enc_encode_global_gain(WORD32 global_gain, WORD32 log_norm,
89
                                                 WORD32 scalefac,
90
528k
                                                 ixheaace_bit_buf_handle pstr_bit_stream_handle) {
91
528k
  ixheaace_write_bits(pstr_bit_stream_handle,
92
528k
                      global_gain - scalefac + GLOBAL_GAIN_OFFSET - 4 * log_norm, 8);
93
528k
}
94
95
static VOID ia_enhaacplus_enc_encode_ics_info(WORD32 block_type, WORD32 win_shape,
96
                                              WORD32 grouping_mask,
97
                                              ixheaace_section_data *pstr_section_data,
98
                                              ixheaace_bit_buf_handle pstr_bit_stream_handle,
99
362k
                                              WORD32 aot) {
100
362k
  WORD32 tmp;
101
102
362k
  switch (aot) {
103
24.3k
    case AOT_AAC_LD:
104
24.3k
      tmp = ((ICS_RESERVED_BIT << 3) | (block_type << 1) | win_shape);
105
24.3k
      ixheaace_write_bits(pstr_bit_stream_handle, tmp, 4);
106
24.3k
      break;
107
108
63.6k
    case AOT_AAC_LC:
109
187k
    case AOT_SBR:
110
212k
    case AOT_PS:
111
212k
      tmp = ((ICS_RESERVED_BIT << 3) | (block_type << 1) | win_shape);
112
212k
      ixheaace_write_bits(pstr_bit_stream_handle, tmp, 4);
113
212k
      break;
114
362k
  }
115
362k
  switch (block_type) {
116
278k
    case LONG_WINDOW:
117
296k
    case START_WINDOW:
118
312k
    case STOP_WINDOW:
119
120
312k
      switch (aot) {
121
51.5k
        case AOT_AAC_LC:
122
147k
        case AOT_SBR:
123
162k
        case AOT_PS:
124
162k
          tmp = pstr_section_data->max_sfb_per_grp << 1;
125
162k
          ixheaace_write_bits(pstr_bit_stream_handle, tmp, 7);
126
162k
          break;
127
128
24.3k
        case AOT_AAC_LD:
129
24.3k
          tmp = pstr_section_data->max_sfb_per_grp << 1;
130
24.3k
          ixheaace_write_bits(pstr_bit_stream_handle, tmp, 7);
131
24.3k
          break;
132
125k
        default:
133
125k
          ixheaace_write_bits(pstr_bit_stream_handle, pstr_section_data->max_sfb_per_grp, 6);
134
312k
      }
135
312k
      break;
136
137
312k
    case SHORT_WINDOW:
138
139
      // Write grouping bits
140
141
49.5k
      tmp = pstr_section_data->max_sfb_per_grp << (TRANS_FAC - 1) | grouping_mask;
142
49.5k
      ixheaace_write_bits(pstr_bit_stream_handle, tmp, TRANS_FAC - 1 + 4);
143
144
49.5k
      break;
145
362k
  }
146
362k
}
147
148
static WORD32 ia_enhaacplus_enc_encode_section_data(
149
528k
    ixheaace_section_data *pstr_section_data, ixheaace_bit_buf_handle pstr_bit_stream_handle) {
150
528k
  WORD32 sect_escape_val = 0, sect_len_bits = 0;
151
528k
  WORD32 sect_len;
152
528k
  WORD32 i;
153
528k
  WORD32 dbg_val = ia_enhaacplus_enc_get_bits_available(pstr_bit_stream_handle);
154
155
528k
  switch (pstr_section_data->block_type) {
156
387k
    case LONG_WINDOW:
157
417k
    case START_WINDOW:
158
443k
    case STOP_WINDOW:
159
160
443k
      sect_escape_val = SECT_ESC_VAL_LONG;
161
443k
      sect_len_bits = SECT_BITS_LONG;
162
443k
      break;
163
164
84.1k
    case SHORT_WINDOW:
165
166
84.1k
      sect_escape_val = SECT_ESC_VAL_SHORT;
167
84.1k
      sect_len_bits = SECT_BITS_SHORT;
168
84.1k
      break;
169
528k
  }
170
171
2.47M
  for (i = 0; i < pstr_section_data->num_of_sections; i++) {
172
1.94M
    ixheaace_write_bits(pstr_bit_stream_handle, pstr_section_data->section[i].code_book, 4);
173
174
1.94M
    sect_len = pstr_section_data->section[i].sfb_cnt;
175
176
2.32M
    while (sect_len >= sect_escape_val) {
177
380k
      ixheaace_write_bits(pstr_bit_stream_handle, sect_escape_val, (UWORD8)sect_len_bits);
178
179
380k
      sect_len -= sect_escape_val;
180
380k
    }
181
182
1.94M
    ixheaace_write_bits(pstr_bit_stream_handle, sect_len, (UWORD8)sect_len_bits);
183
1.94M
  }
184
185
528k
  return (ia_enhaacplus_enc_get_bits_available(pstr_bit_stream_handle) - dbg_val);
186
528k
}
187
188
static VOID ia_enhaacplus_enc_code_scale_factor_delta_lav(WORD32 delta,
189
                                                            ixheaace_bit_buf_handle ptr_bitstream,
190
                                                            const UWORD16 *ptr_pltabscf,
191
7.43M
                                                            const UWORD32 *ptr_pctabscf) {
192
7.43M
  WORD32 code_word, code_length;
193
194
7.43M
  code_word = ptr_pctabscf[delta];
195
7.43M
  code_length = ptr_pltabscf[delta];
196
197
7.43M
  ixheaace_write_bits(ptr_bitstream, code_word, (UWORD8)code_length);
198
7.43M
}
199
static WORD32 ia_enhaacplus_enc_encode_scalefactor_data(
200
    UWORD16 *ptr_max_val_in_sfb, ixheaace_section_data *pstr_section_data, WORD16 *ptr_scalefac,
201
528k
    ixheaace_bit_buf_handle pstr_bit_stream_handle, ixheaace_huffman_tables *pstr_huffman_tbl) {
202
528k
  WORD32 i, j, last_val_scf, delta_scf;
203
528k
  WORD32 dbg_val = ia_enhaacplus_enc_get_bits_available(pstr_bit_stream_handle);
204
528k
  const UWORD16 *ptr_pltabscf = &pstr_huffman_tbl->huff_ltabscf[CODE_BCK_SCF_LAV];
205
528k
  const UWORD32 *ptr_pctabscf = &pstr_huffman_tbl->huff_ctabscf[CODE_BCK_SCF_LAV];
206
207
528k
  last_val_scf = ptr_scalefac[pstr_section_data->first_scf];
208
209
2.47M
  for (i = 0; i < pstr_section_data->num_of_sections; i++) {
210
1.94M
    if (pstr_section_data->section[i].code_book != CODE_BCK_ZERO_NO) {
211
1.33M
      for (j = pstr_section_data->section[i].sfb_start;
212
8.76M
           j < pstr_section_data->section[i].sfb_start + pstr_section_data->section[i].sfb_cnt;
213
7.43M
           j++) {
214
7.43M
        if (ptr_max_val_in_sfb[j] == 0) {
215
401k
          delta_scf = 0;
216
7.03M
        } else {
217
7.03M
          delta_scf = -(ptr_scalefac[j] - last_val_scf);
218
219
7.03M
          last_val_scf = ptr_scalefac[j];
220
7.03M
        }
221
7.43M
        ia_enhaacplus_enc_code_scale_factor_delta_lav(delta_scf, pstr_bit_stream_handle,
222
7.43M
                                                          ptr_pltabscf, ptr_pctabscf);
223
7.43M
      }
224
1.33M
    }
225
1.94M
  }
226
227
528k
  return (ia_enhaacplus_enc_get_bits_available(pstr_bit_stream_handle) - dbg_val);
228
528k
}
229
230
static VOID ia_enhaacplus_enc_encode_ms_info(WORD32 sfb_cnt, WORD32 sfb_grp, WORD32 max_sfb,
231
                                             WORD32 ms_digest, WORD32 *js_flags,
232
166k
                                             ixheaace_bit_buf_handle pstr_bit_stream_handle) {
233
166k
  WORD32 sfb, sfb_offset;
234
166k
  UWORD32 tmp_var = 0;
235
166k
  WORD32 *jsflag;
236
166k
  UWORD8 num_of_bits, remaining = 0;
237
238
166k
  switch (ms_digest) {
239
16.8k
    case MS_NONE:
240
16.8k
      ixheaace_write_bits(pstr_bit_stream_handle, SI_MS_MASK_NONE, 2);
241
16.8k
      break;
242
243
135k
    case MS_ALL:
244
135k
      ixheaace_write_bits(pstr_bit_stream_handle, SI_MS_MASK_ALL, 2);
245
135k
      break;
246
247
13.8k
    case MS_SOME:
248
13.8k
      ixheaace_write_bits(pstr_bit_stream_handle, SI_MS_MASK_SOME, 2);
249
250
13.8k
      if (max_sfb > 32) {
251
2.49k
        num_of_bits = 32;
252
2.49k
        remaining = (UWORD8)(max_sfb - 32);
253
2.49k
      } else
254
11.3k
        num_of_bits = (UWORD8)max_sfb;
255
256
39.3k
      for (sfb_offset = 0; sfb_offset < sfb_cnt; sfb_offset += sfb_grp) {
257
25.5k
        WORD8 flag;
258
25.5k
        jsflag = &js_flags[sfb_offset];
259
25.5k
        tmp_var = 0;
260
261
442k
        for (sfb = num_of_bits - 1; sfb >= 0; sfb--) {
262
416k
          flag = (WORD8)(*jsflag++);
263
416k
          tmp_var = ((tmp_var << 1) | flag);
264
416k
        }
265
266
25.5k
        ixheaace_write_bits(pstr_bit_stream_handle, tmp_var, num_of_bits);
267
268
25.5k
        if (remaining) {
269
18.9k
          for (sfb = remaining - 1; sfb >= 0; sfb--) {
270
16.4k
            flag = (WORD8)(*jsflag++);
271
16.4k
            tmp_var = ((tmp_var << 1) | flag);
272
16.4k
          }
273
274
2.49k
          ixheaace_write_bits(pstr_bit_stream_handle, tmp_var, remaining);
275
2.49k
        }
276
25.5k
      }
277
278
13.8k
      break;
279
166k
  }
280
166k
}
281
282
static VOID ia_enhaacplus_enc_encode_tns_data(
283
    ixheaace_temporal_noise_shaping_params pstr_tns_info, WORD32 block_type,
284
527k
    ixheaace_bit_buf_handle pstr_bit_stream_handle, WORD32 aot) {
285
527k
  WORD32 i, k;
286
527k
  WORD32 tns_present;
287
527k
  WORD32 num_windows;
288
527k
  WORD32 coef_bits;
289
290
527k
  UWORD32 tmp;
291
527k
  UWORD8 val;
292
293
527k
  num_windows = (block_type == 2 ? TRANS_FAC : 1);
294
295
527k
  tns_present = 0;
296
297
1.64M
  for (i = 0; i < num_windows; i++) {
298
1.11M
    if (pstr_tns_info.tns_active[i] == 1) {
299
408k
      tns_present = 1;
300
408k
    }
301
1.11M
  }
302
303
527k
  if (tns_present == 0) {
304
256k
    ixheaace_write_bits(pstr_bit_stream_handle, 0, 1);
305
306
256k
    if (AOT_AAC_LD == aot) {
307
28.3k
      ia_enhaacplus_enc_encode_gain_control_data(pstr_bit_stream_handle);
308
28.3k
    }
309
271k
  } else {
310
    /* there is data to be written*/
311
312
271k
    ixheaace_write_bits(pstr_bit_stream_handle, 1, 1); /* data_present */
313
314
271k
    if (aot == AOT_AAC_LD) {
315
7.36k
      ia_enhaacplus_enc_encode_gain_control_data(pstr_bit_stream_handle);
316
7.36k
    }
317
318
902k
    for (i = 0; i < num_windows; i++) {
319
631k
      ixheaace_write_bits(pstr_bit_stream_handle, pstr_tns_info.tns_active[i],
320
631k
                          (UWORD8)(block_type == 2 ? 1 : 2));
321
322
631k
      if (pstr_tns_info.tns_active[i]) {
323
408k
        tmp = ((pstr_tns_info.coef_res[i] == 4 ? 1 : 0)
324
408k
                   << ((block_type == 2 ? 4 : 6) + (block_type == 2 ? 3 : 5)) |
325
408k
               pstr_tns_info.length[i] << (block_type == 2 ? 3 : 5) | pstr_tns_info.order[i]);
326
327
408k
        val = (UWORD8)(1 + (block_type == 2 ? 4 : 6) + (block_type == 2 ? 3 : 5));
328
329
408k
        ixheaace_write_bits(pstr_bit_stream_handle, tmp, val);
330
331
408k
        if (pstr_tns_info.order[i]) {
332
408k
          ixheaace_write_bits(pstr_bit_stream_handle, FILTER_DIRECTION, 1);
333
334
408k
          if (pstr_tns_info.coef_res[i] == 4) {
335
219k
            coef_bits = 3;
336
337
772k
            for (k = 0; k < pstr_tns_info.order[i]; k++) {
338
748k
              if (pstr_tns_info.coef[i * TEMPORAL_NOISE_SHAPING_MAX_ORDER_SHORT + k] > 3 ||
339
748k
                  pstr_tns_info.coef[i * TEMPORAL_NOISE_SHAPING_MAX_ORDER_SHORT + k] < -4) {
340
195k
                coef_bits = 4;
341
195k
                break;
342
195k
              }
343
748k
            }
344
219k
          } else {
345
188k
            coef_bits = 2;
346
347
505k
            for (k = 0; k < pstr_tns_info.order[i]; k++) {
348
382k
              if (pstr_tns_info.coef[i * TEMPORAL_NOISE_SHAPING_MAX_ORDER_SHORT + k] > 1 ||
349
382k
                  pstr_tns_info.coef[i * TEMPORAL_NOISE_SHAPING_MAX_ORDER_SHORT + k] < -2) {
350
65.4k
                coef_bits = 3;
351
65.4k
                break;
352
65.4k
              }
353
382k
            }
354
188k
          }
355
356
408k
          ixheaace_write_bits(pstr_bit_stream_handle, -(coef_bits - pstr_tns_info.coef_res[i]),
357
408k
                              1); /*coef_compres*/
358
359
2.82M
          for (k = 0; k < pstr_tns_info.order[i]; k++) {
360
2.41M
            static const WORD32 rmask[] = {0, 1, 3, 7, 15};
361
362
2.41M
            ixheaace_write_bits(
363
2.41M
                pstr_bit_stream_handle,
364
2.41M
                pstr_tns_info.coef[i * TEMPORAL_NOISE_SHAPING_MAX_ORDER_SHORT + k] &
365
2.41M
                    rmask[coef_bits],
366
2.41M
                (UWORD8)coef_bits);
367
2.41M
          }
368
408k
        }
369
408k
      }
370
631k
    }
371
271k
  }
372
527k
}
373
374
static VOID PLATFORM_INLINE
375
350k
ia_enhaacplus_enc_encode_pulse_data(ixheaace_bit_buf_handle pstr_bit_stream_handle) {
376
350k
  ixheaace_write_bits(pstr_bit_stream_handle, 0, 1);
377
350k
}
378
379
static IA_ERRORCODE ia_enhaacplus_enc_write_ic_stream(
380
    WORD32 common_window, WORD32 win_shape, WORD32 grouping_mask, WORD32 *pstr_sfb_offset,
381
    WORD16 *ptr_scf, UWORD16 *ptr_max_val_in_sfb, WORD32 global_gain, WORD16 *ptr_quant_spec,
382
    ixheaace_section_data *pstr_section_data, ixheaace_bit_buf_handle pstr_bit_stream_handle,
383
    WORD32 aot, ixheaace_temporal_noise_shaping_params pstr_tns_info,
384
528k
    ixheaace_aac_tables *pstr_aac_tables) {
385
528k
  WORD32 log_norm = -1;
386
387
528k
  ia_enhaacplus_enc_encode_global_gain(
388
528k
      global_gain, log_norm, ptr_scf[pstr_section_data->first_scf], pstr_bit_stream_handle);
389
528k
  if (!common_window) {
390
196k
    ia_enhaacplus_enc_encode_ics_info(pstr_section_data->block_type, win_shape, grouping_mask,
391
196k
                                      pstr_section_data, pstr_bit_stream_handle, aot);
392
196k
  }
393
394
528k
  if (ia_enhaacplus_enc_encode_section_data(pstr_section_data, pstr_bit_stream_handle) !=
395
528k
      pstr_section_data->side_info_bits) {
396
0
    return IA_EXHEAACE_EXE_FATAL_INVALID_SIDE_INFO_BITS;
397
0
  }
398
399
528k
  if (ia_enhaacplus_enc_encode_scalefactor_data(
400
528k
          ptr_max_val_in_sfb, pstr_section_data, ptr_scf, pstr_bit_stream_handle,
401
528k
          pstr_aac_tables->pstr_huff_tab) != pstr_section_data->scale_fac_bits) {
402
222
    return IA_EXHEAACE_EXE_FATAL_INVALID_SCALE_FACTOR_BITS;
403
222
  }
404
405
527k
  if (aot == AOT_AAC_LC || aot == AOT_AAC_LD || aot == AOT_SBR || aot == AOT_PS) {
406
350k
    ia_enhaacplus_enc_encode_pulse_data(pstr_bit_stream_handle);
407
350k
  }
408
527k
  ia_enhaacplus_enc_encode_tns_data(pstr_tns_info, pstr_section_data->block_type,
409
527k
                                    pstr_bit_stream_handle, aot);
410
411
527k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
412
314k
    ia_enhaacplus_enc_encode_gain_control_data(pstr_bit_stream_handle);
413
314k
  }
414
415
527k
  if (ia_enhaacplus_enc_encode_spectral_data(
416
527k
          pstr_sfb_offset, pstr_section_data, ptr_quant_spec, pstr_bit_stream_handle,
417
527k
          pstr_aac_tables->pstr_huff_tab) != pstr_section_data->huffman_bits) {
418
0
    return IA_EXHEAACE_EXE_FATAL_INVALID_HUFFMAN_BITS;
419
0
  }
420
527k
  return IA_NO_ERROR;
421
527k
}
422
423
static IA_ERRORCODE ia_enhaacplus_enc_write_single_chan_elem(
424
    WORD32 instance_tag, WORD32 *pstr_sfb_offset, ixheaace_qc_out_channel *pstr_qc_out_ch,
425
    ixheaace_bit_buf_handle pstr_bit_stream_handle, WORD32 aot,
426
166k
    ixheaace_temporal_noise_shaping_params pstr_tns_info, ixheaace_aac_tables *pstr_aac_tables) {
427
166k
  IA_ERRORCODE err_code;
428
429
166k
  switch (aot) {
430
25.7k
    case AOT_AAC_LC:
431
71.4k
    case AOT_SBR:
432
95.6k
    case AOT_PS:
433
95.6k
      ixheaace_write_bits(pstr_bit_stream_handle, ID_SCE, 3);
434
95.6k
      ixheaace_write_bits(pstr_bit_stream_handle, instance_tag, 4);
435
95.6k
      break;
436
10.4k
    case AOT_AAC_LD:
437
10.4k
      ixheaace_write_bits(pstr_bit_stream_handle, instance_tag, 4);
438
10.4k
      break;
439
166k
  }
440
441
166k
  err_code = ia_enhaacplus_enc_write_ic_stream(
442
166k
      0, pstr_qc_out_ch->win_shape, pstr_qc_out_ch->grouping_mask, pstr_sfb_offset,
443
166k
      pstr_qc_out_ch->scalefactor, pstr_qc_out_ch->max_val_in_sfb, pstr_qc_out_ch->global_gain,
444
166k
      pstr_qc_out_ch->quant_spec, &(pstr_qc_out_ch->section_data), pstr_bit_stream_handle, aot,
445
166k
      pstr_tns_info, pstr_aac_tables);
446
447
166k
  if (err_code != IA_NO_ERROR) {
448
35
    return err_code;
449
35
  }
450
451
166k
  return IA_NO_ERROR;
452
166k
}
453
454
static IA_ERRORCODE ia_enhaacplus_enc_write_channel_pair_element(
455
    WORD32 instance_tag, WORD32 ms_digest, WORD32 ms_flags[MAXIMUM_GROUPED_SCALE_FACTOR_BAND],
456
    WORD32 *pstr_sfb_offset[2], ixheaace_qc_out_channel pstr_qc_out_ch[2],
457
    ixheaace_bit_buf_handle pstr_bit_stream_handle, WORD32 aot,
458
    ixheaace_temporal_noise_shaping_params pstr_tns_info[2],
459
166k
    ixheaace_aac_tables *pstr_aac_tables) {
460
166k
  IA_ERRORCODE err_code;
461
462
166k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
463
103k
    ixheaace_write_bits(pstr_bit_stream_handle, ID_CPE, 3);
464
103k
  }
465
466
166k
  switch (aot) {
467
11.3k
    case AOT_AAC_LD:
468
11.3k
      ixheaace_write_bits(pstr_bit_stream_handle, instance_tag, 4);
469
11.3k
      ixheaace_write_bits(pstr_bit_stream_handle, 1, 1); /* common window */
470
11.3k
      break;
471
472
33.1k
    case AOT_AAC_LC:
473
103k
    case AOT_SBR:
474
103k
    case AOT_PS:
475
103k
      ixheaace_write_bits(pstr_bit_stream_handle, instance_tag, 4);
476
103k
      ixheaace_write_bits(pstr_bit_stream_handle, 1, 1);
477
103k
      break;
478
166k
  }
479
480
166k
  ia_enhaacplus_enc_encode_ics_info(pstr_qc_out_ch[0].section_data.block_type,
481
166k
                                    pstr_qc_out_ch[0].win_shape, pstr_qc_out_ch[0].grouping_mask,
482
166k
                                    &(pstr_qc_out_ch[0].section_data), pstr_bit_stream_handle,
483
166k
                                    aot);
484
485
166k
  ia_enhaacplus_enc_encode_ms_info(pstr_qc_out_ch[0].section_data.sfb_cnt,
486
166k
                                   pstr_qc_out_ch[0].section_data.sfb_per_group,
487
166k
                                   pstr_qc_out_ch[0].section_data.max_sfb_per_grp, ms_digest,
488
166k
                                   ms_flags, pstr_bit_stream_handle);
489
166k
  err_code = ia_enhaacplus_enc_write_ic_stream(
490
166k
      1, pstr_qc_out_ch[0].win_shape, pstr_qc_out_ch[0].grouping_mask, pstr_sfb_offset[0],
491
166k
      pstr_qc_out_ch[0].scalefactor, pstr_qc_out_ch[0].max_val_in_sfb,
492
166k
      pstr_qc_out_ch[0].global_gain, pstr_qc_out_ch[0].quant_spec,
493
166k
      &(pstr_qc_out_ch[0].section_data), pstr_bit_stream_handle, aot, pstr_tns_info[0],
494
166k
      pstr_aac_tables);
495
496
166k
  if (err_code != IA_NO_ERROR) {
497
79
    return err_code;
498
79
  }
499
500
165k
  err_code = ia_enhaacplus_enc_write_ic_stream(
501
165k
      1, pstr_qc_out_ch[1].win_shape, pstr_qc_out_ch[1].grouping_mask, pstr_sfb_offset[1],
502
165k
      pstr_qc_out_ch[1].scalefactor, pstr_qc_out_ch[1].max_val_in_sfb,
503
165k
      pstr_qc_out_ch[1].global_gain, pstr_qc_out_ch[1].quant_spec,
504
165k
      &(pstr_qc_out_ch[1].section_data), pstr_bit_stream_handle, aot, pstr_tns_info[1],
505
165k
      pstr_aac_tables);
506
507
165k
  if (err_code != IA_NO_ERROR) {
508
52
    return err_code;
509
52
  }
510
511
165k
  return IA_NO_ERROR;
512
165k
}
513
514
static VOID ia_enhaacplus_enc_write_fill_element_LD(
515
    const UWORD8 *ptr_anc_bytes, WORD32 total_fill_bits,
516
84.7k
    ixheaace_bit_buf_handle pstr_bit_stream_handle) {
517
84.7k
  WORD32 i, cnt, cnt1, remaining = 0;
518
519
  /*
520
    Write fill Element(s):
521
    amount of a fill element can be 7+X*8 Bits, X element of [0..270]
522
  */
523
  // ID_FIL and the FILL element payload size, ( 3+ 4 bits) are not sent.
524
  // but they are accounted in the earlier code, so subtracting it here
525
526
84.7k
  total_fill_bits -= 7;
527
208k
  while (total_fill_bits > 0) {
528
124k
    cnt = MIN((total_fill_bits >> 3), ((1 << 4) - 1));
529
530
124k
    if (ptr_anc_bytes) {
531
1.61M
      for (i = 0; i < cnt; i++) {
532
1.49M
        ixheaace_write_bits(pstr_bit_stream_handle, *ptr_anc_bytes++, 8);
533
1.49M
        total_fill_bits -= 8;
534
1.49M
      }
535
124k
      if (total_fill_bits < 8) {
536
60.4k
        ixheaace_write_bits(pstr_bit_stream_handle, *ptr_anc_bytes++, (UWORD8)total_fill_bits);
537
60.4k
        ixheaace_write_bits(pstr_bit_stream_handle, 0, (UWORD8)(8 - total_fill_bits));
538
60.4k
        total_fill_bits = 0;
539
60.4k
      }
540
124k
    } else {
541
0
      cnt1 = cnt >> 2;
542
0
      remaining = cnt - (cnt1 << 2);
543
544
0
      for (i = 0; i < cnt1; i++) {
545
0
        ixheaace_write_bits(pstr_bit_stream_handle, 0, 32);
546
0
        total_fill_bits -= 32;
547
0
      }
548
0
      if (remaining)
549
0
        for (i = 0; i < remaining; i++) {
550
0
          ixheaace_write_bits(pstr_bit_stream_handle, 0, 8);
551
0
          total_fill_bits -= 8;
552
0
        }
553
0
    }
554
124k
  }
555
84.7k
}
556
557
static VOID ia_enhaacplus_enc_write_fill_element_LC(const UWORD8 *ptr_anc_bytes,
558
                                                    WORD32 total_fill_bits,
559
307k
                                                    ixheaace_bit_buf_handle pstr_h_bit_stream) {
560
307k
  WORD32 i, cnt, esc_count;
561
562
670k
  while (total_fill_bits >= (3 + 4)) {
563
363k
    cnt = MIN((total_fill_bits - (3 + 4)) / 8, ((1 << 4) - 1));
564
565
363k
    ixheaace_write_bits(pstr_h_bit_stream, ID_FIL, 3);
566
567
363k
    ixheaace_write_bits(pstr_h_bit_stream, cnt, 4);
568
569
363k
    total_fill_bits -= (3 + 4);
570
571
363k
    if (cnt == (1 << 4) - 1) {
572
273k
      esc_count = MIN((total_fill_bits / 8) - ((1 << 4) - 1), (1 << 8) - 1);
573
574
273k
      ixheaace_write_bits(pstr_h_bit_stream, esc_count, 8);
575
576
273k
      total_fill_bits -= 8;
577
578
273k
      cnt += esc_count - 1;
579
273k
    }
580
581
55.9M
    for (i = 0; i < cnt; i++) {
582
55.6M
      if (ptr_anc_bytes)
583
2.01M
        ixheaace_write_bits(pstr_h_bit_stream, *ptr_anc_bytes++, 8);
584
53.5M
      else
585
53.5M
        ixheaace_write_bits(pstr_h_bit_stream, 0, 8);
586
587
55.6M
      total_fill_bits -= 8;
588
55.6M
    }
589
363k
  }
590
307k
}
591
592
static IA_ERRORCODE ia_enhaacplus_enc_write_single_channel_element_LFE(
593
    WORD32 instance_tag, WORD32 *ptr_sfb_offset, ixheaace_qc_out_channel *pstr_qc_out_ch,
594
    ixheaace_bit_buf_handle pstr_bit_stream_handle, WORD32 aot,
595
29.7k
    ixheaace_temporal_noise_shaping_params pstr_tns_info, ixheaace_aac_tables *pstr_aac_tables) {
596
29.7k
  IA_ERRORCODE err_code;
597
29.7k
  switch (aot) {
598
4.77k
    case AOT_AAC_LC:
599
13.3k
    case AOT_SBR:
600
13.3k
    case AOT_PS:
601
13.3k
      ixheaace_write_bits(pstr_bit_stream_handle, ID_LFE, 3);
602
13.3k
      ixheaace_write_bits(pstr_bit_stream_handle, instance_tag, 4);
603
13.3k
      break;
604
605
2.50k
    case AOT_AAC_LD:
606
2.50k
      ixheaace_write_bits(pstr_bit_stream_handle, instance_tag, 4);
607
2.50k
      break;
608
29.7k
  }
609
610
29.7k
  err_code = ia_enhaacplus_enc_write_ic_stream(
611
29.7k
      0, pstr_qc_out_ch->win_shape, pstr_qc_out_ch->grouping_mask, ptr_sfb_offset,
612
29.7k
      pstr_qc_out_ch->scalefactor, pstr_qc_out_ch->max_val_in_sfb, pstr_qc_out_ch->global_gain,
613
29.7k
      pstr_qc_out_ch->quant_spec, &(pstr_qc_out_ch->section_data), pstr_bit_stream_handle, aot,
614
29.7k
      pstr_tns_info, pstr_aac_tables);
615
616
29.7k
  if (err_code != IA_NO_ERROR) {
617
56
    return err_code;
618
56
  }
619
29.7k
  return IA_NO_ERROR;
620
29.7k
}
621
622
static IA_ERRORCODE ia_enhaacplus_enc_write_single_channel_ind_coupling_element(
623
    WORD32 *ptr_sfb_offset, ixheaace_qc_out_channel *pstr_qc_out_ch,
624
    ixheaace_bit_buf_handle pstr_bit_stream_handle, WORD32 aot,
625
0
    ixheaace_temporal_noise_shaping_params pstr_tns_info, ixheaace_aac_tables *pstr_aac_tables) {
626
0
  IA_ERRORCODE err_code;
627
0
  ixheaace_write_bits(pstr_bit_stream_handle, ID_CCE, 3);
628
629
  /*Flag indication that this is an independent coupling channel*/
630
0
  ixheaace_write_bits(pstr_bit_stream_handle, 1, 1);
631
632
  /*number of coupled channel elements*/
633
0
  ixheaace_write_bits(pstr_bit_stream_handle, NUM_COUPLED_ELE, 3);
634
635
  /*Flag indicating target is CPE*/
636
0
  ixheaace_write_bits(pstr_bit_stream_handle, 1, 1);
637
638
  /*Instance tag of target CPE*/
639
0
  ixheaace_write_bits(pstr_bit_stream_handle, 0, 4);
640
641
0
  ixheaace_write_bits(pstr_bit_stream_handle, 3, 2);
642
643
  /*Flag indicating coupling after TNS*/
644
0
  ixheaace_write_bits(pstr_bit_stream_handle, 1, 1);
645
646
  /*Flag indicating sign of coupling*/
647
0
  ixheaace_write_bits(pstr_bit_stream_handle, 0, 1);
648
649
  /*Flag indicating Scale of coupling*/
650
0
  ixheaace_write_bits(pstr_bit_stream_handle, SCALE_COUPLING_LEVEL0, 2);
651
652
0
  err_code = ia_enhaacplus_enc_write_ic_stream(
653
0
      0, pstr_qc_out_ch->win_shape, pstr_qc_out_ch->grouping_mask, ptr_sfb_offset,
654
0
      pstr_qc_out_ch->scalefactor, pstr_qc_out_ch->max_val_in_sfb, pstr_qc_out_ch->global_gain,
655
0
      pstr_qc_out_ch->quant_spec, &(pstr_qc_out_ch->section_data), pstr_bit_stream_handle, aot,
656
0
      pstr_tns_info, pstr_aac_tables);
657
0
  if (err_code != IA_NO_ERROR) {
658
0
    return err_code;
659
0
  }
660
661
0
  ia_enhaacplus_enc_code_scale_factor_delta(-1, pstr_bit_stream_handle,
662
0
                                            pstr_aac_tables->pstr_huff_tab);
663
664
0
  return IA_NO_ERROR;
665
0
}
666
667
IA_ERRORCODE ia_enhaacplus_enc_write_bitstream(
668
    ixheaace_bit_buf_handle pstr_bit_stream_handle, ixheaace_element_info pstr_element_info,
669
    ixheaace_qc_out *pstr_qc_out, ixheaace_psy_out *pstr_psy_out, WORD32 *glob_used_bits,
670
    const UWORD8 *ptr_anc_bytes, ixheaace_aac_tables *pstr_aac_tables, FLAG flag_last_element,
671
    WORD32 *write_program_config_element, WORD32 i_num_coup_channels, WORD32 i_channels_mask,
672
362k
    WORD32 i_samp_freq, WORD32 ele_idx, WORD32 aot, WORD32 *total_fill_bits) {
673
362k
  IA_ERRORCODE err_code;
674
362k
  WORD32 bit_markup, element_used_bits, frame_bits;
675
676
362k
  bit_markup = ia_enhaacplus_enc_get_bits_available(pstr_bit_stream_handle);
677
678
362k
  *glob_used_bits = 0;
679
680
362k
  {
681
362k
    WORD32 *ptr_sfb_offset[2];
682
362k
    ixheaace_temporal_noise_shaping_params tns_info[2];
683
684
362k
    element_used_bits = 0;
685
686
362k
    if ((*write_program_config_element == 1) && (ele_idx == 0)) {
687
0
      WORD32 samp_rate = i_samp_freq, ch_mask = i_channels_mask, n_cc_ch = i_num_coup_channels;
688
689
      /*Write Program Config Element*/
690
0
      ixheaace_write_bits(pstr_bit_stream_handle, ID_PCE, 3);
691
0
      ia_enhaacplus_enc_write_pce(samp_rate, ch_mask, n_cc_ch, pstr_bit_stream_handle);
692
0
      *write_program_config_element = 0;
693
0
    }
694
695
362k
    switch (pstr_element_info.el_type) {
696
166k
      case ID_SCE: /* single channel */
697
698
166k
        ptr_sfb_offset[0] =
699
166k
            pstr_psy_out->psy_out_ch[pstr_element_info.channel_index[0]]->sfb_offsets;
700
166k
        tns_info[0] = pstr_psy_out->psy_out_ch[pstr_element_info.channel_index[0]]->tns_info;
701
702
166k
        err_code = ia_enhaacplus_enc_write_single_chan_elem(
703
166k
            pstr_element_info.instance_tag, ptr_sfb_offset[0],
704
166k
            pstr_qc_out->qc_channel[pstr_element_info.channel_index[0]], pstr_bit_stream_handle,
705
166k
            aot, tns_info[0], pstr_aac_tables);
706
707
166k
        if (err_code != IA_NO_ERROR) {
708
35
          return err_code;
709
35
        }
710
166k
        break;
711
712
166k
      case ID_CCE: /* single channel independent coupling element*/
713
714
0
        ptr_sfb_offset[0] =
715
0
            pstr_psy_out->psy_out_ch[pstr_element_info.channel_index[0]]->sfb_offsets;
716
0
        tns_info[0] = pstr_psy_out->psy_out_ch[pstr_element_info.channel_index[0]]->tns_info;
717
718
0
        err_code = ia_enhaacplus_enc_write_single_channel_ind_coupling_element(
719
0
            ptr_sfb_offset[0], pstr_qc_out->qc_channel[pstr_element_info.channel_index[0]],
720
0
            pstr_bit_stream_handle, aot, tns_info[0], pstr_aac_tables);
721
722
0
        if (err_code != IA_NO_ERROR) {
723
0
          return err_code;
724
0
        }
725
0
        break;
726
727
29.7k
      case ID_LFE: /* single channel */
728
729
29.7k
        ptr_sfb_offset[0] =
730
29.7k
            pstr_psy_out->psy_out_ch[pstr_element_info.channel_index[0]]->sfb_offsets;
731
29.7k
        tns_info[0] = pstr_psy_out->psy_out_ch[pstr_element_info.channel_index[0]]->tns_info;
732
733
29.7k
        err_code = ia_enhaacplus_enc_write_single_channel_element_LFE(
734
29.7k
            pstr_element_info.instance_tag, ptr_sfb_offset[0],
735
29.7k
            pstr_qc_out->qc_channel[pstr_element_info.channel_index[0]], pstr_bit_stream_handle,
736
29.7k
            aot, tns_info[0], pstr_aac_tables);
737
738
29.7k
        if (err_code != IA_NO_ERROR) {
739
56
          return err_code;
740
56
        }
741
29.7k
        break;
742
743
166k
      case ID_CPE: /* channel pair */
744
166k
      {
745
166k
        WORD32 ms_digest = pstr_psy_out->psy_out_element.tools_info.ms_digest;
746
166k
        WORD32 *ms_flags = pstr_psy_out->psy_out_element.tools_info.ms_mask;
747
748
166k
        ptr_sfb_offset[0] =
749
166k
            pstr_psy_out->psy_out_ch[pstr_element_info.channel_index[0]]->sfb_offsets;
750
166k
        ptr_sfb_offset[1] =
751
166k
            pstr_psy_out->psy_out_ch[pstr_element_info.channel_index[1]]->sfb_offsets;
752
753
166k
        tns_info[0] = pstr_psy_out->psy_out_ch[pstr_element_info.channel_index[0]]->tns_info;
754
166k
        tns_info[1] = pstr_psy_out->psy_out_ch[pstr_element_info.channel_index[1]]->tns_info;
755
756
166k
        err_code = ia_enhaacplus_enc_write_channel_pair_element(
757
166k
            pstr_element_info.instance_tag, ms_digest, ms_flags, ptr_sfb_offset,
758
166k
            pstr_qc_out->qc_channel[pstr_element_info.channel_index[0]], pstr_bit_stream_handle,
759
166k
            aot, tns_info, pstr_aac_tables);
760
166k
        if (err_code != IA_NO_ERROR) {
761
131
          return err_code;
762
131
        }
763
166k
      } break;
764
765
165k
      default:
766
0
        return IA_EXHEAACE_INIT_FATAL_INVALID_ELEMENT_TYPE;
767
768
362k
    } /* switch */
769
770
361k
    element_used_bits -= bit_markup;
771
772
361k
    bit_markup = ia_enhaacplus_enc_get_bits_available(pstr_bit_stream_handle);
773
774
361k
    frame_bits = element_used_bits + bit_markup;
775
361k
  }
776
777
361k
  if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
778
211k
    ia_enhaacplus_enc_write_fill_element_LC(ptr_anc_bytes, pstr_qc_out->tot_anc_bits_used,
779
211k
                                            pstr_bit_stream_handle);
780
211k
  }
781
782
361k
  if (flag_last_element) {
783
180k
    if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
784
95.7k
      ia_enhaacplus_enc_write_fill_element_LC(NULL, *total_fill_bits, pstr_bit_stream_handle);
785
786
95.7k
      *total_fill_bits = 0;
787
788
95.7k
      ixheaace_write_bits(pstr_bit_stream_handle, ID_END, 3);
789
95.7k
    }
790
791
180k
    if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
792
84.7k
      {
793
84.7k
        WORD32 i, cnt = 0;
794
84.7k
        ia_enhaacplus_enc_write_fill_element_LD(ptr_anc_bytes, pstr_qc_out->tot_anc_bits_used,
795
84.7k
                                                pstr_bit_stream_handle);
796
797
84.7k
        *total_fill_bits += pstr_qc_out->total_fill_bits;
798
84.7k
        *total_fill_bits += pstr_qc_out->align_bits;
799
84.7k
        cnt = *total_fill_bits >> 3;
800
11.9M
        for (i = 0; i < cnt; i++) {
801
11.8M
          ixheaace_write_bits(pstr_bit_stream_handle, 0, 8);
802
11.8M
        }
803
84.7k
        cnt = *total_fill_bits - (cnt << 3);
804
84.7k
        ixheaace_write_bits(pstr_bit_stream_handle, 0, (UWORD8)cnt);
805
84.7k
        *total_fill_bits = 0;
806
84.7k
      }
807
84.7k
    }
808
    /* byte alignement */
809
810
180k
    ixheaace_write_bits(pstr_bit_stream_handle, 0,
811
180k
                        (UWORD8)((8 - (pstr_bit_stream_handle->cnt_bits % 8)) % 8));
812
180k
  }
813
814
361k
  *glob_used_bits -= bit_markup;
815
816
361k
  bit_markup = ia_enhaacplus_enc_get_bits_available(pstr_bit_stream_handle);
817
818
361k
  *glob_used_bits += bit_markup;
819
820
361k
  frame_bits += *glob_used_bits;
821
822
361k
  if (frame_bits != pstr_qc_out->tot_static_bits_used + pstr_qc_out->tot_dyn_bits_used +
823
361k
                        pstr_qc_out->tot_anc_bits_used + +pstr_qc_out->total_fill_bits +
824
361k
                        pstr_qc_out->align_bits) {
825
240k
  }
826
827
361k
  return IA_NO_ERROR;
828
362k
}