Coverage Report

Created: 2025-08-24 07:17

/src/libxaac/encoder/ixheaace_asc_write.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 <stddef.h>
22
#include "iusace_type_def.h"
23
#include "iusace_bitbuffer.h"
24
/* DRC */
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 "impd_drc_uni_drc_eq.h"
30
#include "impd_drc_uni_drc_filter_bank.h"
31
#include "impd_drc_gain_enc.h"
32
#include "impd_drc_struct_def.h"
33
#include "iusace_cnst.h"
34
35
#include "ixheaace_api.h"
36
#include "iusace_tns_usac.h"
37
#include "iusace_psy_mod.h"
38
#include "ixheaace_sbr_header.h"
39
#include "ixheaace_config.h"
40
#include "iusace_config.h"
41
#include "ixheaace_asc_write.h"
42
#include "iusace_block_switch_const.h"
43
#include "iusace_rom.h"
44
#include "ixheaac_constants.h"
45
#include "ixheaace_aac_constants.h"
46
#include "ixheaace_sbr_def.h"
47
#include "ixheaace_common_rom.h"
48
#include "ixheaace_error_codes.h"
49
50
static WORD32 ixheaace_spatial_specific_config(ia_bit_buf_struct *pstr_it_bit_buff,
51
3.31k
                                               ia_aace_config_struct *pstr_eld_config) {
52
3.31k
  WORD32 bit_cnt = 0, cnt = 0;
53
3.31k
  WORD32 num_bytes = pstr_eld_config->num_sac_cfg_bits >> 3;
54
11.8k
  for (cnt = 0; cnt < num_bytes; cnt++) {
55
8.56k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_eld_config->sac_cfg_data[cnt]), 8);
56
8.56k
  }
57
3.31k
  if (pstr_eld_config->num_sac_cfg_bits & 0x7) {
58
2.66k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_eld_config->sac_cfg_data[cnt]),
59
2.66k
                                     (pstr_eld_config->num_sac_cfg_bits & 0x7));
60
2.66k
  }
61
62
3.31k
  return bit_cnt;
63
3.31k
}
64
static WORD32 iusace_config_extension(ia_bit_buf_struct *pstr_it_bit_buff,
65
15.5k
                                      ia_usac_config_struct *pstr_usac_config) {
66
15.5k
  WORD32 bit_cnt = 0;
67
15.5k
  UWORD32 i, j;
68
15.5k
  UWORD32 fill_byte_val = 0xa5;
69
70
15.5k
  bit_cnt += iusace_write_escape_value(pstr_it_bit_buff,
71
15.5k
                                       pstr_usac_config->num_config_extensions - 1, 2, 4, 8);
72
73
46.5k
  for (j = 0; j < pstr_usac_config->num_config_extensions; j++) {
74
31.0k
    bit_cnt += iusace_write_escape_value(pstr_it_bit_buff,
75
31.0k
                                         pstr_usac_config->usac_config_ext_type[j], 4, 8, 16);
76
77
31.0k
    bit_cnt += iusace_write_escape_value(pstr_it_bit_buff,
78
31.0k
                                         pstr_usac_config->usac_config_ext_len[j], 4, 8, 16);
79
80
31.0k
    switch (pstr_usac_config->usac_config_ext_type[j]) {
81
0
      case ID_CONFIG_EXT_FILL:
82
0
        for (i = 0; i < pstr_usac_config->usac_config_ext_len[j]; i++) {
83
0
          bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, fill_byte_val, 8);
84
0
        }
85
0
        break;
86
15.5k
      case ID_CONFIG_EXT_LOUDNESS_INFO:
87
196k
        for (i = 0; i < pstr_usac_config->usac_config_ext_len[j]; i++) {
88
180k
          bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
89
180k
                                           pstr_usac_config->usac_config_ext_buf[j][i], 8);
90
180k
        }
91
15.5k
        break;
92
15.5k
      case ID_CONFIG_EXT_STREAM_ID:
93
15.5k
        bit_cnt +=
94
15.5k
            iusace_write_bits_buf(pstr_it_bit_buff, pstr_usac_config->stream_identifier, 16);
95
15.5k
        break;
96
0
      default:
97
0
        for (i = 0; i < pstr_usac_config->usac_config_ext_len[j]; i++) {
98
0
          bit_cnt += iusace_write_bits_buf(
99
0
              pstr_it_bit_buff, (UWORD32)pstr_usac_config->usac_cfg_ext_info_buf[j][i], 8);
100
0
        }
101
0
        break;
102
31.0k
    }
103
31.0k
  }
104
15.5k
  return bit_cnt;
105
15.5k
}
106
107
static WORD32 iusace_sbr_config(ia_bit_buf_struct *pstr_it_bit_buff,
108
9.14k
                                ia_usac_enc_sbr_config_struct *pstr_usac_sbr_config) {
109
9.14k
  WORD32 bit_cnt = 0;
110
111
9.14k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->harmonic_sbr), 1);
112
9.14k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->bs_inter_tes), 1);
113
9.14k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->bs_pvc), 1);
114
9.14k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->dflt_start_freq), 4);
115
9.14k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->dflt_stop_freq), 4);
116
9.14k
  bit_cnt +=
117
9.14k
      iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->dflt_header_extra1), 1);
118
9.14k
  bit_cnt +=
119
9.14k
      iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->dflt_header_extra2), 1);
120
121
9.14k
  if (pstr_usac_sbr_config->dflt_header_extra1) {
122
0
    bit_cnt +=
123
0
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->dflt_freq_scale), 2);
124
0
    bit_cnt +=
125
0
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->dflt_alter_scale), 2);
126
0
    bit_cnt +=
127
0
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->dflt_noise_bands), 2);
128
0
  }
129
130
9.14k
  if (pstr_usac_sbr_config->dflt_header_extra2) {
131
0
    bit_cnt +=
132
0
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->dflt_limiter_bands), 2);
133
0
    bit_cnt +=
134
0
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->dflt_limiter_gains), 2);
135
0
    bit_cnt +=
136
0
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->dflt_interpol_freq), 1);
137
0
    bit_cnt +=
138
0
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_sbr_config->dflt_smoothing_mode), 1);
139
0
  }
140
141
9.14k
  return bit_cnt;
142
9.14k
}
143
144
static WORD32 iusace_cpe_config(ia_bit_buf_struct *pstr_it_bit_buff,
145
                                ia_usac_enc_element_config_struct *pstr_usac_enc_conf,
146
10.9k
                                WORD32 sbr_ratio_idx, ia_aace_config_struct *pstr_eld_config) {
147
10.9k
  WORD32 bit_count = 0;
148
149
10.9k
  if (sbr_ratio_idx > 0) {
150
7.16k
    bit_count += iusace_sbr_config(pstr_it_bit_buff, &(pstr_usac_enc_conf->str_usac_sbr_config));
151
7.16k
    bit_count +=
152
7.16k
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_enc_conf->stereo_config_index), 2);
153
7.16k
  }
154
155
10.9k
  if (pstr_usac_enc_conf->stereo_config_index > 0) {
156
2.66k
    if (pstr_eld_config->num_sac_cfg_bits) {
157
2.66k
      {
158
2.66k
        bit_count += ixheaace_spatial_specific_config(pstr_it_bit_buff, pstr_eld_config);
159
2.66k
      }
160
2.66k
    }
161
2.66k
  }
162
163
10.9k
  return bit_count;
164
10.9k
}
165
static WORD32 iusace_ext_element_config(ia_bit_buf_struct *pstr_it_bit_buff,
166
32.7k
                                        ia_usac_enc_element_config_struct *pstr_usac_enc_conf) {
167
32.7k
  WORD32 bit_count = 0;
168
169
32.7k
  bit_count += iusace_write_escape_value(pstr_it_bit_buff, pstr_usac_enc_conf->usac_ext_ele_type,
170
32.7k
                                         4, 8, 16);
171
32.7k
  bit_count += iusace_write_escape_value(pstr_it_bit_buff,
172
32.7k
                                         pstr_usac_enc_conf->usac_ext_ele_cfg_len, 4, 8, 16);
173
32.7k
  bit_count += iusace_write_bits_buf(pstr_it_bit_buff,
174
32.7k
                                     (pstr_usac_enc_conf->usac_ext_ele_dflt_len_present), 1);
175
176
32.7k
  if (pstr_usac_enc_conf->usac_ext_ele_dflt_len_present) {
177
0
    bit_count += iusace_write_escape_value(
178
0
        pstr_it_bit_buff, pstr_usac_enc_conf->usac_ext_ele_dflt_len - 1, 8, 16, 0);
179
0
  }
180
32.7k
  bit_count += iusace_write_bits_buf(pstr_it_bit_buff,
181
32.7k
                                     (pstr_usac_enc_conf->usac_ext_ele_payload_present), 1);
182
183
32.7k
  switch (pstr_usac_enc_conf->usac_ext_ele_type) {
184
15.5k
    case ID_EXT_ELE_FILL:
185
15.5k
      break;
186
1.66k
    case ID_EXT_ELE_UNI_DRC: {
187
1.66k
      UWORD32 i;
188
35.1k
      for (i = 0; i < pstr_usac_enc_conf->usac_ext_ele_cfg_len; i++) {
189
33.5k
        bit_count +=
190
33.5k
            iusace_write_bits_buf(pstr_it_bit_buff, pstr_usac_enc_conf->drc_config_data[i], 8);
191
33.5k
      }
192
1.66k
    } break;
193
15.5k
    default:
194
15.5k
      break;
195
32.7k
  }
196
197
32.7k
  return bit_count;
198
32.7k
}
199
200
static WORD32 iusace_encoder_config(ia_bit_buf_struct *pstr_it_bit_buff,
201
                                    ia_usac_config_struct *pstr_usac_cfg, WORD32 sbr_ratio_idx,
202
15.5k
                                    ia_aace_config_struct *pstr_eld_config) {
203
15.5k
  WORD32 bit_cnt = 0;
204
15.5k
  UWORD32 elem_idx = 0;
205
15.5k
  ia_usac_enc_element_config_struct *pstr_usac_enc_conf;
206
207
15.5k
  bit_cnt +=
208
15.5k
      iusace_write_escape_value(pstr_it_bit_buff, pstr_usac_cfg->num_elements - 1, 4, 8, 16);
209
210
63.7k
  for (elem_idx = 0; elem_idx < pstr_usac_cfg->num_elements; elem_idx++) {
211
48.2k
    unsigned long tmp = pstr_usac_cfg->usac_element_type[elem_idx];
212
48.2k
    pstr_usac_enc_conf = &pstr_usac_cfg->str_usac_element_config[elem_idx];
213
48.2k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, tmp, 2);
214
215
48.2k
    switch (pstr_usac_cfg->usac_element_type[elem_idx]) {
216
4.52k
      case ID_USAC_SCE:
217
4.52k
        bit_cnt += iusace_write_bits_buf(
218
4.52k
            pstr_it_bit_buff, (pstr_usac_enc_conf->tw_mdct),
219
4.52k
            1);  // For extended HE AAC profile tw_mdct shall be encoded with 0.
220
4.52k
        bit_cnt +=
221
4.52k
            iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_enc_conf->noise_filling), 1);
222
4.52k
        if (sbr_ratio_idx > 0) {
223
1.98k
          bit_cnt +=
224
1.98k
              iusace_sbr_config(pstr_it_bit_buff, &(pstr_usac_enc_conf->str_usac_sbr_config));
225
1.98k
        }
226
4.52k
        break;
227
10.9k
      case ID_USAC_CPE:
228
10.9k
        bit_cnt += iusace_write_bits_buf(
229
10.9k
            pstr_it_bit_buff, (pstr_usac_enc_conf->tw_mdct),
230
10.9k
            1);  // For extended HE AAC profile tw_mdct shall be encoded with 0.
231
10.9k
        bit_cnt +=
232
10.9k
            iusace_write_bits_buf(pstr_it_bit_buff, (pstr_usac_enc_conf->noise_filling), 1);
233
10.9k
        bit_cnt += iusace_cpe_config(pstr_it_bit_buff, pstr_usac_enc_conf, sbr_ratio_idx,
234
10.9k
                                     pstr_eld_config);
235
10.9k
        break;
236
32.7k
      case ID_USAC_EXT:
237
32.7k
        bit_cnt += iusace_ext_element_config(pstr_it_bit_buff, pstr_usac_enc_conf);
238
32.7k
        break;
239
0
      default:
240
0
        return IA_EXHEAACE_INIT_FATAL_USAC_INVALID_ELEMENT_TYPE;
241
0
        break;
242
48.2k
    }
243
48.2k
  }
244
245
15.5k
  return bit_cnt;
246
15.5k
}
247
248
15.5k
static UWORD32 ixheaace_sbr_ratio(UWORD32 core_sbr_framelength_idx) {
249
15.5k
  UWORD32 sbr_ratio_index = 0x0FF;
250
251
15.5k
  switch (core_sbr_framelength_idx) {
252
2.41k
    case 0:
253
6.37k
    case 1:
254
6.37k
      sbr_ratio_index = USAC_SBR_RATIO_NO_SBR;
255
6.37k
      break;
256
1.50k
    case 2:
257
1.50k
      sbr_ratio_index = USAC_SBR_RATIO_INDEX_8_3;
258
1.50k
      break;
259
5.26k
    case 3:
260
5.26k
      sbr_ratio_index = USAC_SBR_RATIO_INDEX_2_1;
261
5.26k
      break;
262
2.37k
    case 4:
263
2.37k
      sbr_ratio_index = USAC_SBR_RATIO_INDEX_4_1;
264
2.37k
      break;
265
15.5k
  }
266
267
15.5k
  return sbr_ratio_index;
268
15.5k
}
269
static WORD32 sbr_header(ia_bit_buf_struct *pstr_it_bit_buff,
270
1.58k
                         ixheaace_pstr_sbr_hdr_data pstr_sbr_config) {
271
1.58k
  WORD32 bit_cnt = 0;
272
1.58k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->sbr_amp_res), 1);
273
1.58k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->sbr_start_freq), 4);
274
1.58k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->sbr_stop_freq), 4);
275
1.58k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->sbr_xover_band), 3);
276
1.58k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (0), 2);  // reserved bits
277
1.58k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->header_extra_1), 1);
278
1.58k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->header_extra_2), 1);
279
1.58k
  if (pstr_sbr_config->header_extra_1) {
280
1.17k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->freq_scale), 2);
281
1.17k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->alter_scale), 1);
282
1.17k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->sbr_noise_bands), 2);
283
1.17k
  }
284
285
1.58k
  if (pstr_sbr_config->header_extra_2) {
286
0
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->sbr_limiter_bands), 2);
287
0
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->sbr_limiter_gains), 2);
288
0
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->sbr_interpol_freq), 1);
289
0
    bit_cnt +=
290
0
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_sbr_config->sbr_smoothing_length), 1);
291
0
  }
292
1.58k
  return bit_cnt;
293
1.58k
}
294
295
static WORD32 ld_sbr_header(ia_bit_buf_struct *pstr_it_bit_buff,
296
                            ixheaace_pstr_sbr_hdr_data pstr_sbr_config,
297
1.05k
                            WORD32 channel_configuration) {
298
1.05k
  WORD32 num_sbr_header, el, bit_cnt = 0;
299
1.05k
  switch (channel_configuration) {
300
468
    case 1:
301
761
    case 2:
302
761
      num_sbr_header = 1;
303
761
      break;
304
51
    case 3:
305
51
      num_sbr_header = 2;
306
51
      break;
307
39
    case 4:
308
79
    case 5:
309
242
    case 6:
310
242
      num_sbr_header = 3;
311
242
      break;
312
0
    case 7:
313
0
      num_sbr_header = 4;
314
0
      break;
315
0
    default:
316
0
      num_sbr_header = 0;
317
0
      break;
318
1.05k
  }
319
2.64k
  for (el = 0; el < num_sbr_header; el++) {
320
1.58k
    bit_cnt += sbr_header(pstr_it_bit_buff, pstr_sbr_config);
321
1.58k
  }
322
1.05k
  return bit_cnt;
323
1.05k
}
324
325
static WORD32 iaace_get_eld_specific_config_bytes(ia_bit_buf_struct *pstr_it_bit_buff,
326
                                                  ia_aace_config_struct *pstr_eld_config,
327
1.05k
                                                  WORD32 channel_configuration) {
328
1.05k
  WORD32 bit_cnt = 0;
329
1.05k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_eld_config->frame_length_flag), 1);
330
1.05k
  bit_cnt +=
331
1.05k
      iusace_write_bits_buf(pstr_it_bit_buff, (pstr_eld_config->aac_sec_data_resilience_flag), 1);
332
1.05k
  bit_cnt +=
333
1.05k
      iusace_write_bits_buf(pstr_it_bit_buff, (pstr_eld_config->aac_sf_data_resilience_flag), 1);
334
1.05k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
335
1.05k
                                   (pstr_eld_config->aac_spec_data_resilience_flag), 1);
336
1.05k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_eld_config->ld_sbr_present_flag), 1);
337
338
1.05k
  if (pstr_eld_config->ld_sbr_present_flag) {
339
1.05k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_eld_config->ld_sbr_sample_rate), 1);
340
1.05k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_eld_config->ld_sbr_crc_flag), 1);
341
1.05k
    bit_cnt +=
342
1.05k
        ld_sbr_header(pstr_it_bit_buff, pstr_eld_config->sbr_config, channel_configuration);
343
1.05k
  }
344
345
1.05k
  if (pstr_eld_config->num_sac_cfg_bits) {
346
653
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (pstr_eld_config->eld_ext_type[0]), 4);
347
653
    bit_cnt +=
348
653
        iusace_write_escape_value(pstr_it_bit_buff, (pstr_eld_config->eld_ext_len[0]), 4, 8, 16);
349
653
    if (IAAC_ELDEXT_LDSAC == pstr_eld_config->eld_ext_type[0]) {
350
653
      bit_cnt += ixheaace_spatial_specific_config(pstr_it_bit_buff, pstr_eld_config);
351
653
    }
352
653
  }
353
1.05k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, (IAAC_ELDEXT_TERM), 4);
354
355
1.05k
  return bit_cnt;
356
1.05k
}
357
358
static WORD32 iaace_ga_specific_config_bytes(ia_bit_buf_struct *pstr_it_bit_buff,
359
                                             ia_aace_config_struct *pstr_ga_specific_config,
360
1.48k
                                             WORD32 channel_configuration, WORD32 aot) {
361
1.48k
  WORD32 bit_cnt = 0;
362
1.48k
  bit_cnt +=
363
1.48k
      iusace_write_bits_buf(pstr_it_bit_buff, (pstr_ga_specific_config->frame_length_flag), 1);
364
1.48k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
365
1.48k
                                   (pstr_ga_specific_config->depends_on_core_coder), 1);
366
1.48k
  if (pstr_ga_specific_config->depends_on_core_coder) {
367
0
    bit_cnt +=
368
0
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_ga_specific_config->core_coder_delay), 14);
369
0
  }
370
1.48k
  if (!channel_configuration) {
371
0
  }
372
373
1.48k
  if (AOT_AAC_LD == aot) {
374
279
    pstr_ga_specific_config->extension_flag = 1;
375
279
  }
376
1.48k
  if (AOT_AAC_LC == aot) {
377
1.20k
    pstr_ga_specific_config->extension_flag = 0;
378
1.20k
  }
379
1.48k
  bit_cnt +=
380
1.48k
      iusace_write_bits_buf(pstr_it_bit_buff, (pstr_ga_specific_config->extension_flag), 1);
381
382
1.48k
  if ((pstr_ga_specific_config->extension_flag) && ((AOT_AAC_LD == aot))) {
383
279
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
384
279
                                     (pstr_ga_specific_config->aac_sec_data_resilience_flag), 1);
385
279
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
386
279
                                     (pstr_ga_specific_config->aac_sf_data_resilience_flag), 1);
387
279
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
388
279
                                     (pstr_ga_specific_config->aac_spec_data_resilience_flag), 1);
389
390
    // extension flag 3
391
279
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
392
279
  }
393
1.48k
  return bit_cnt;
394
1.48k
}
395
396
WORD32 ixheaace_get_audiospecific_config_bytes(
397
    ia_bit_buf_struct *pstr_it_bit_buff,
398
    ixheaace_audio_specific_config_struct *pstr_audio_specific_config, WORD32 aot,
399
7.42k
    WORD32 ccfl_idx) {
400
7.42k
  WORD32 bit_cnt = 0, i;
401
7.42k
  UWORD32 tmp = 0x0f;  // initialized to indicate no sampling frequency index field
402
7.42k
  WORD32 ext_aot = -1;
403
7.42k
  WORD32 ext_id = 0;
404
7.42k
  WORD32 sbr_present_flag = 0, ps_present_flag = 0;
405
406
7.42k
  if (((AOT_AAC_ELD == aot) &&
407
7.42k
       (1 == pstr_audio_specific_config->str_aac_config.ld_sbr_sample_rate)) ||
408
7.42k
      (AOT_SBR == aot) || (AOT_PS == aot)) {
409
    // dual rate
410
1.89k
    pstr_audio_specific_config->ext_sampling_frequency =
411
1.89k
        pstr_audio_specific_config->sampling_frequency;
412
1.89k
    pstr_audio_specific_config->sampling_frequency /= 2;
413
414
1.89k
    if ((AOT_SBR == aot) || (AOT_PS == aot)) {
415
839
      aot = AOT_AAC_LC;
416
839
    }
417
1.89k
  }
418
7.42k
  pstr_audio_specific_config->audio_object_type = aot;
419
7.42k
  if (aot == AOT_USAC) {
420
53.3k
    for (i = 0; i < sizeof(iusace_sampl_freq_idx_table) / sizeof(iusace_sampl_freq_idx_table[0]);
421
52.6k
         i++) {
422
52.6k
      if (pstr_audio_specific_config->sampling_frequency == iusace_sampl_freq_idx_table[i]) {
423
4.18k
        tmp = i;
424
4.18k
        break;
425
4.18k
      }
426
52.6k
    }
427
4.88k
  } else {
428
19.3k
    for (i = 0; i < sizeof(ia_sampl_freq_table) / sizeof(ia_sampl_freq_table[0]); i++) {
429
19.3k
      if (ia_sampl_freq_table[i] == pstr_audio_specific_config->sampling_frequency) {
430
2.54k
        tmp = i;
431
2.54k
        break;
432
2.54k
      }
433
19.3k
    }
434
2.54k
  }
435
436
7.42k
  pstr_audio_specific_config->samp_freq_index = (UWORD32)tmp;
437
438
  // Write Audio Object Type
439
7.42k
  if (pstr_audio_specific_config->audio_object_type > 31) {
440
5.93k
    tmp = pstr_audio_specific_config->audio_object_type - 32;
441
5.93k
    pstr_audio_specific_config->audio_object_type = 31;
442
5.93k
  }
443
7.42k
  bit_cnt +=
444
7.42k
      iusace_write_bits_buf(pstr_it_bit_buff, (pstr_audio_specific_config->audio_object_type), 5);
445
7.42k
  if (pstr_audio_specific_config->audio_object_type == 31) {
446
5.93k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, tmp, 6);
447
5.93k
    pstr_audio_specific_config->audio_object_type = tmp + 32;
448
5.93k
  }
449
450
  // Write Audio Object Type
451
7.42k
  bit_cnt +=
452
7.42k
      iusace_write_bits_buf(pstr_it_bit_buff, (pstr_audio_specific_config->samp_freq_index), 4);
453
7.42k
  if (pstr_audio_specific_config->samp_freq_index == 0xf) {
454
700
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
455
700
                                     (pstr_audio_specific_config->sampling_frequency), 24);
456
6.72k
  } else if (AOT_USAC != aot) {
457
2.54k
    pstr_audio_specific_config->sampling_frequency =
458
2.54k
        ia_sampl_freq_table[pstr_audio_specific_config->samp_freq_index];
459
2.54k
  }
460
7.42k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
461
7.42k
                                   (pstr_audio_specific_config->channel_configuration), 4);
462
463
7.42k
  if ((AOT_SBR == aot) || (AOT_PS == aot)) {
464
0
    ext_aot = AOT_SBR;
465
0
    sbr_present_flag = 1;
466
0
    tmp = 0x0f;
467
0
    for (i = 0; i < sizeof(ia_sampl_freq_table) / sizeof(ia_sampl_freq_table[0]); i++) {
468
0
      if (ia_sampl_freq_table[i] == pstr_audio_specific_config->ext_sampling_frequency) {
469
0
        tmp = i;
470
0
        break;
471
0
      }
472
0
    }
473
0
    pstr_audio_specific_config->ext_samp_freq_index = (UWORD32)tmp;
474
0
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
475
0
                                     (pstr_audio_specific_config->ext_samp_freq_index), 4);
476
0
    if (pstr_audio_specific_config->ext_samp_freq_index == 0xf) {
477
0
      bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
478
0
                                       (pstr_audio_specific_config->ext_sampling_frequency), 24);
479
0
    }
480
0
    if (AOT_PS == aot) {
481
0
      ps_present_flag = 1;
482
0
    }
483
484
0
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, AOT_AAC_LC, 5);
485
0
  }
486
7.42k
  switch (aot) {
487
1.05k
    case AOT_AAC_ELD: {
488
1.05k
      bit_cnt += iaace_get_eld_specific_config_bytes(
489
1.05k
          pstr_it_bit_buff, &pstr_audio_specific_config->str_aac_config,
490
1.05k
          pstr_audio_specific_config->channel_configuration);
491
1.05k
      break;
492
0
    }
493
279
    case AOT_AAC_LD:
494
1.48k
    case AOT_AAC_LC:
495
1.48k
    case AOT_SBR:
496
1.48k
    case AOT_PS: {
497
1.48k
      bit_cnt += iaace_ga_specific_config_bytes(
498
1.48k
          pstr_it_bit_buff, &pstr_audio_specific_config->str_aac_config,
499
1.48k
          pstr_audio_specific_config->channel_configuration, aot);
500
501
1.48k
      if (AOT_AAC_LD == aot) {
502
        // epconfig
503
279
        bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, 0, 2);
504
279
      }
505
1.48k
      if (AOT_SBR == ext_aot) {
506
0
        ext_id = 0x2b7;
507
0
        bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, ext_id, 11);
508
509
0
        bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, aot, 5);
510
0
        bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, sbr_present_flag, 1);
511
0
        if (sbr_present_flag) {
512
0
          bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
513
0
                                           (pstr_audio_specific_config->ext_samp_freq_index), 4);
514
0
          if (pstr_audio_specific_config->ext_samp_freq_index == 0xf) {
515
0
            bit_cnt += iusace_write_bits_buf(
516
0
                pstr_it_bit_buff, (pstr_audio_specific_config->ext_sampling_frequency), 24);
517
0
          }
518
519
0
          if (AOT_PS == aot) {
520
0
            ext_id = 0x548;
521
0
            bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, ext_id, 11);
522
0
            bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, ps_present_flag, 1);
523
0
          }
524
0
        }
525
0
      }
526
1.48k
      break;
527
1.48k
    }
528
529
4.88k
    case AOT_USAC: {
530
4.88k
      WORD32 sbr_ratio_idx;
531
4.88k
      ia_usac_config_struct *pstr_usac_config = &(pstr_audio_specific_config->str_usac_config);
532
4.88k
      WORD32 ia_ccfl_tbl[5] = {768, 1024, 768, 1024, 1024};
533
4.88k
      pstr_audio_specific_config->core_sbr_framelength_index =
534
4.88k
          ccfl_idx;  // 768 core coder frame length without SBR
535
4.88k
      pstr_usac_config->ccfl =
536
4.88k
          ia_ccfl_tbl[pstr_audio_specific_config->core_sbr_framelength_index];
537
4.88k
      tmp = 0x1f;
538
48.6k
      for (i = 0; i < sizeof(ia_usac_sampl_freq_table) / sizeof(ia_usac_sampl_freq_table[0]);
539
47.9k
           i++) {
540
47.9k
        if (ia_usac_sampl_freq_table[i] == pstr_audio_specific_config->sampling_frequency) {
541
4.17k
          tmp = i;
542
4.17k
          break;
543
4.17k
        }
544
47.9k
      }
545
4.88k
      pstr_audio_specific_config->samp_freq_index = (UWORD32)tmp;
546
547
4.88k
      if (pstr_audio_specific_config->samp_freq_index == 0x1f) {
548
708
        bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, 0x1f, 5);
549
708
        bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
550
708
                                         (pstr_audio_specific_config->sampling_frequency), 24);
551
4.17k
      } else {
552
4.17k
        bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
553
4.17k
                                         (pstr_audio_specific_config->samp_freq_index), 5);
554
4.17k
      }
555
556
4.88k
      bit_cnt += iusace_write_bits_buf(
557
4.88k
          pstr_it_bit_buff, (pstr_audio_specific_config->core_sbr_framelength_index), 3);
558
559
4.88k
      bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
560
4.88k
                                       (pstr_audio_specific_config->channel_configuration), 5);
561
562
4.88k
      if (pstr_audio_specific_config->channel_configuration == 0) {
563
0
        bit_cnt += iusace_write_escape_value(
564
0
            pstr_it_bit_buff, pstr_audio_specific_config->num_audio_channels, 5, 8, 16);
565
566
0
        for (i = 0; i < pstr_audio_specific_config->num_audio_channels; i++) {
567
0
          tmp = pstr_audio_specific_config->output_channel_pos[i];
568
0
          bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, tmp, 5);
569
0
        }
570
0
      }
571
572
4.88k
      sbr_ratio_idx = ixheaace_sbr_ratio(pstr_audio_specific_config->core_sbr_framelength_index);
573
574
4.88k
      bit_cnt += iusace_encoder_config(pstr_it_bit_buff, pstr_usac_config, sbr_ratio_idx,
575
4.88k
                                       &pstr_audio_specific_config->str_aac_config);
576
577
4.88k
      bit_cnt +=
578
4.88k
          iusace_write_bits_buf(pstr_it_bit_buff, pstr_usac_config->usac_cfg_ext_present, 1);
579
4.88k
      if (pstr_usac_config->usac_cfg_ext_present) {
580
4.88k
        bit_cnt += iusace_config_extension(pstr_it_bit_buff, pstr_usac_config);
581
4.88k
      }
582
583
4.88k
      if (sbr_ratio_idx)
584
3.05k
        pstr_audio_specific_config->sbr_present_flag = 1;
585
1.82k
      else
586
1.82k
        pstr_audio_specific_config->sbr_present_flag = 0;
587
588
4.88k
      pstr_audio_specific_config->ext_audio_object_type = 0;
589
590
4.88k
      if (pstr_audio_specific_config->ext_audio_object_type == AOT_SBR) {
591
0
        pstr_audio_specific_config->ext_sync_word = 0x2b7;
592
0
        bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
593
0
                                         (pstr_audio_specific_config->ext_sync_word), 11);
594
595
0
        bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
596
0
                                         (pstr_audio_specific_config->ext_audio_object_type), 5);
597
598
0
        bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
599
0
                                         (pstr_audio_specific_config->sbr_present_flag), 1);
600
601
0
        if (pstr_audio_specific_config->sbr_present_flag == 1) {
602
0
          bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
603
0
                                           (pstr_audio_specific_config->ext_samp_freq_index), 4);
604
605
0
          if (pstr_audio_specific_config->ext_samp_freq_index == 0xf) {
606
0
            bit_cnt += iusace_write_bits_buf(
607
0
                pstr_it_bit_buff, (pstr_audio_specific_config->ext_sampling_frequency), 24);
608
0
          }
609
0
        }
610
0
      }
611
4.88k
    }
612
4.88k
    default:
613
4.88k
      break;
614
7.42k
  }
615
7.42k
  return bit_cnt;
616
7.42k
}
617
618
WORD32 ixheaace_get_usac_config_bytes(
619
    ia_bit_buf_struct *pstr_it_bit_buff,
620
10.6k
    ixheaace_audio_specific_config_struct *pstr_audio_specific_config, WORD32 ccfl_idx) {
621
10.6k
  WORD32 sbr_ratio_idx;
622
10.6k
  ia_usac_config_struct *pstr_usac_config = &(pstr_audio_specific_config->str_usac_config);
623
10.6k
  WORD32 ia_ccfl_tbl[5] = {768, 1024, 768, 1024, 1024};
624
10.6k
  WORD32 i, tmp, bit_cnt = 0;
625
10.6k
  pstr_audio_specific_config->core_sbr_framelength_index =
626
10.6k
      ccfl_idx;  // 768 core coder frame length without SBR
627
10.6k
  pstr_usac_config->ccfl = ia_ccfl_tbl[pstr_audio_specific_config->core_sbr_framelength_index];
628
10.6k
  tmp = 0x1f;
629
126k
  for (i = 0; i < sizeof(ia_usac_sampl_freq_table) / sizeof(ia_usac_sampl_freq_table[0]); i++) {
630
124k
    if (ia_usac_sampl_freq_table[i] == pstr_audio_specific_config->sampling_frequency) {
631
8.43k
      tmp = i;
632
8.43k
      break;
633
8.43k
    }
634
124k
  }
635
10.6k
  pstr_audio_specific_config->samp_freq_index = (UWORD32)tmp;
636
637
10.6k
  if (pstr_audio_specific_config->samp_freq_index == 0x1f) {
638
2.20k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, 0x1f, 5);
639
2.20k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
640
2.20k
                                     (pstr_audio_specific_config->sampling_frequency), 24);
641
8.43k
  } else {
642
8.43k
    bit_cnt +=
643
8.43k
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_audio_specific_config->samp_freq_index), 5);
644
8.43k
  }
645
646
10.6k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
647
10.6k
                                   (pstr_audio_specific_config->core_sbr_framelength_index), 3);
648
649
10.6k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
650
10.6k
                                   (pstr_audio_specific_config->channel_configuration), 5);
651
652
10.6k
  if (pstr_audio_specific_config->channel_configuration == 0) {
653
0
    bit_cnt += iusace_write_escape_value(
654
0
        pstr_it_bit_buff, pstr_audio_specific_config->num_audio_channels, 5, 8, 16);
655
656
0
    for (i = 0; i < pstr_audio_specific_config->num_audio_channels; i++) {
657
0
      tmp = pstr_audio_specific_config->output_channel_pos[i];
658
0
      bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, tmp, 5);
659
0
    }
660
0
  }
661
662
10.6k
  sbr_ratio_idx = ixheaace_sbr_ratio(pstr_audio_specific_config->core_sbr_framelength_index);
663
664
10.6k
  bit_cnt += iusace_encoder_config(pstr_it_bit_buff, pstr_usac_config, sbr_ratio_idx,
665
10.6k
                                   &pstr_audio_specific_config->str_aac_config);
666
667
10.6k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, pstr_usac_config->usac_cfg_ext_present, 1);
668
10.6k
  if (pstr_usac_config->usac_cfg_ext_present) {
669
10.6k
    bit_cnt += iusace_config_extension(pstr_it_bit_buff, pstr_usac_config);
670
10.6k
  }
671
672
10.6k
  if (sbr_ratio_idx)
673
6.08k
    pstr_audio_specific_config->sbr_present_flag = 1;
674
4.55k
  else
675
4.55k
    pstr_audio_specific_config->sbr_present_flag = 0;
676
677
10.6k
  pstr_audio_specific_config->ext_audio_object_type = 0;
678
679
10.6k
  if (pstr_audio_specific_config->ext_audio_object_type == AOT_SBR) {
680
0
    pstr_audio_specific_config->ext_sync_word = 0x2b7;
681
0
    bit_cnt +=
682
0
        iusace_write_bits_buf(pstr_it_bit_buff, (pstr_audio_specific_config->ext_sync_word), 11);
683
684
0
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
685
0
                                     (pstr_audio_specific_config->ext_audio_object_type), 5);
686
687
0
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
688
0
                                     (pstr_audio_specific_config->sbr_present_flag), 1);
689
690
0
    if (pstr_audio_specific_config->sbr_present_flag == 1) {
691
0
      bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff,
692
0
                                       (pstr_audio_specific_config->ext_samp_freq_index), 4);
693
694
0
      if (pstr_audio_specific_config->ext_samp_freq_index == 0xf) {
695
0
        bit_cnt += iusace_write_bits_buf(
696
0
            pstr_it_bit_buff, (pstr_audio_specific_config->ext_sampling_frequency), 24);
697
0
      }
698
0
    }
699
0
  }
700
10.6k
  return bit_cnt;
701
10.6k
}