Coverage Report

Created: 2025-10-10 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/ixheaace_sbr_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 <math.h>
22
#include <stdlib.h>
23
#include "ixheaac_type_def.h"
24
#include "ixheaac_constants.h"
25
#include "ixheaace_aac_constants.h"
26
#include "ixheaac_basic_ops32.h"
27
#include "ixheaac_basic_ops16.h"
28
#include "ixheaac_basic_ops40.h"
29
30
#include "ixheaace_sbr_header.h"
31
#include "ixheaace_sbr_def.h"
32
#include "ixheaace_resampler.h"
33
#include "ixheaace_sbr_rom.h"
34
#include "ixheaace_common_rom.h"
35
#include "ixheaace_bitbuffer.h"
36
#include "ixheaace_sbr_hbe.h"
37
#include "ixheaace_sbr_qmf_enc.h"
38
#include "ixheaace_sbr_tran_det.h"
39
#include "ixheaace_sbr_frame_info_gen.h"
40
#include "ixheaace_sbr_env_est.h"
41
#include "ixheaace_sbr_code_envelope.h"
42
#include "ixheaace_sbr_main.h"
43
#include "ixheaace_sbr_missing_harmonics_det.h"
44
#include "ixheaace_sbr_inv_filtering_estimation.h"
45
#include "ixheaace_sbr_noise_floor_est.h"
46
47
#include "ixheaace_sbr_ton_corr.h"
48
#include "iusace_esbr_pvc.h"
49
#include "iusace_esbr_inter_tes.h"
50
#include "ixheaace_sbr.h"
51
#include "ixheaace_sbr_cmondata.h"
52
#include "iusace_esbr_pvc.h"
53
54
#include "ixheaace_sbr_hybrid.h"
55
#include "ixheaace_sbr_ps_enc.h"
56
#include "ixheaace_sbr_ps_bitenc.h"
57
#include "ixheaace_sbr_write_bitstream.h"
58
#include "ixheaac_error_standards.h"
59
#include "ixheaace_error_codes.h"
60
#include "ixheaace_common_utils.h"
61
62
151k
static WORD32 ixheaace_get_esbr_ext_data_size(ixheaace_str_esbr_bs_data *pstr_esbr_bs_data) {
63
151k
  WORD32 num_bits = 1;
64
151k
  if (1 == pstr_esbr_bs_data->sbr_num_chan) {
65
64.6k
    num_bits += 1;
66
64.6k
    if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
67
48.5k
      num_bits += 2;
68
48.5k
      if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
69
46.1k
        num_bits += 7;
70
46.1k
      }
71
48.5k
    }
72
86.5k
  } else if (2 == pstr_esbr_bs_data->sbr_num_chan) {
73
48.8k
    if (pstr_esbr_bs_data->sbr_coupling) {
74
37.5k
      num_bits += 1;
75
37.5k
      if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
76
33.0k
        num_bits += 2;
77
33.0k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
78
31.5k
          num_bits += 7;
79
31.5k
        }
80
33.0k
      }
81
37.5k
    } else {
82
11.3k
      num_bits += 1;
83
11.3k
      if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
84
9.45k
        num_bits += 2;
85
9.45k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
86
8.69k
          num_bits += 7;
87
8.69k
        }
88
9.45k
      }
89
11.3k
      num_bits += 1;
90
11.3k
      if (pstr_esbr_bs_data->sbr_patching_mode[1] == 0) {
91
9.44k
        num_bits += 2;
92
9.44k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[1] == 1) {
93
8.64k
          num_bits += 7;
94
8.64k
        }
95
9.44k
      }
96
11.3k
    }
97
48.8k
  } else {
98
37.6k
    num_bits = 0;
99
37.6k
  }
100
151k
  if (num_bits != 0 && num_bits < 6) {
101
26.3k
    num_bits = 6;
102
26.3k
  }
103
151k
  return num_bits;
104
151k
}
105
static WORD32 iusace_encode_pvc_envelope(ixheaace_bit_buf_handle pstr_bs_handle,
106
                                         ixheaace_pvc_bs_info *pstr_pvc_bs_data,
107
16.6k
                                         WORD32 usac_indep_flag) {
108
16.6k
  WORD32 payload_cnt_bits = 0;
109
16.6k
  payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->div_mode,
110
16.6k
                                          IXHEAACE_ESBR_PVC_DIV_MODE_BITS);
111
16.6k
  payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->ns_mode,
112
16.6k
                                          IXHEAACE_ESBR_PVC_NS_MODE_BITS);
113
114
16.6k
  if (0 == pstr_pvc_bs_data->div_mode) {
115
10.3k
    if (1 == usac_indep_flag) {
116
623
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->pvc_id_bs[0],
117
623
                                              IXHEAACE_ESBR_PVC_ID_BITS);
118
9.74k
    } else {
119
9.74k
      if (1 == pstr_pvc_bs_data->grid_info[0]) {
120
1.53k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, IXHEAACE_ESBR_PVC_REUSE_BITS);
121
1.53k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->pvc_id_bs[0],
122
1.53k
                                                IXHEAACE_ESBR_PVC_ID_BITS);
123
8.20k
      } else {
124
8.20k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 1, IXHEAACE_ESBR_PVC_REUSE_BITS);
125
8.20k
      }
126
9.74k
    }
127
10.3k
  } else if (pstr_pvc_bs_data->div_mode <= 3) {
128
    /* Do nothing */
129
6.28k
  } else {
130
6.28k
    WORD32 gi, is_grid_info;
131
18.8k
    for (gi = 0; gi < pstr_pvc_bs_data->num_grid_info; gi++) {
132
12.5k
      if (gi == 0 && 1 == usac_indep_flag) {
133
313
        is_grid_info = 1;
134
12.2k
      } else {
135
12.2k
        is_grid_info = pstr_pvc_bs_data->grid_info[gi];
136
12.2k
        payload_cnt_bits +=
137
12.2k
            ixheaace_write_bits(pstr_bs_handle, is_grid_info, IXHEAACE_ESBR_PVC_GRID_INFO_BITS);
138
12.2k
      }
139
12.5k
      if (is_grid_info) {
140
10.7k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->pvc_id_bs[gi],
141
10.7k
                                                IXHEAACE_ESBR_PVC_ID_BITS);
142
10.7k
      }
143
12.5k
    }
144
6.28k
  }
145
16.6k
  return payload_cnt_bits;
146
16.6k
}
147
734k
static WORD32 ia_enhaacplus_enc_ceil_ln2(WORD32 x) {
148
734k
  WORD32 tmp = -1;
149
150
1.11M
  while (ixheaac_shl32(1, ++tmp) < x)
151
377k
    ;
152
153
734k
  return (tmp);
154
734k
}
155
156
static WORD32 ixheaace_encode_sbr_grid(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,
157
                                       ixheaace_bit_buf_handle pstr_bs_handle,
158
818k
                                       ixheaace_sbr_codec_type sbr_codec) {
159
818k
  WORD32 payload_cnt_bits = 0;
160
818k
  WORD32 i, tmp_var;
161
162
818k
  if (ELD_SBR != sbr_codec) {
163
606k
    if (HEAAC_SBR == sbr_codec ||
164
589k
        (USAC_SBR == sbr_codec && pstr_sbr_env_info->sbr_pvc_mode == 0)) {
165
589k
      payload_cnt_bits += ixheaace_write_bits(
166
589k
          pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type, SBR_CLA_BITS);
167
168
589k
      switch (pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type) {
169
425k
        case IXHEAACE_FIXFIX:
170
171
425k
          tmp_var = ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env);
172
173
425k
          payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ENV_BITS);
174
175
425k
          payload_cnt_bits +=
176
425k
              ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->freq_res_fix, SBR_RES_BITS);
177
425k
          break;
178
179
62.4k
        case IXHEAACE_FIXVAR:
180
121k
        case IXHEAACE_VARFIX:
181
182
121k
          if (pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type == IXHEAACE_FIXVAR) {
183
62.4k
            tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord - 16;
184
62.4k
          } else {
185
58.9k
            tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord;
186
58.9k
          }
187
188
121k
          payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ABS_BITS);
189
190
121k
          payload_cnt_bits += ixheaace_write_bits(
191
121k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->n, SBR_NUM_BITS);
192
193
313k
          for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->n; i++) {
194
192k
            tmp_var = (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_rel_bord[i] - 2) >> 1;
195
196
192k
            payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_REL_BITS);
197
192k
          }
198
199
121k
          tmp_var = ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->n + 2);
200
201
121k
          payload_cnt_bits += ixheaace_write_bits(
202
121k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->p, (UWORD8)tmp_var);
203
204
434k
          for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->n + 1; i++) {
205
313k
            payload_cnt_bits += ixheaace_write_bits(
206
313k
                pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f[i], SBR_RES_BITS);
207
313k
          }
208
121k
          break;
209
210
42.5k
        case IXHEAACE_VARVAR:
211
212
42.5k
          tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord_0;
213
214
42.5k
          payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ABS_BITS);
215
42.5k
          tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord_1 - 16;
216
217
42.5k
          payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ABS_BITS);
218
219
42.5k
          payload_cnt_bits += ixheaace_write_bits(
220
42.5k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0, SBR_NUM_BITS);
221
222
42.5k
          payload_cnt_bits += ixheaace_write_bits(
223
42.5k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1, SBR_NUM_BITS);
224
225
77.4k
          for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0; i++) {
226
34.9k
            tmp_var = (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_rel_bord_0[i] - 2) >> 1;
227
228
34.9k
            payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_REL_BITS);
229
34.9k
          }
230
231
102k
          for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1; i++) {
232
59.7k
            tmp_var = (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_rel_bord_1[i] - 2) >> 1;
233
234
59.7k
            payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_REL_BITS);
235
59.7k
          }
236
237
42.5k
          tmp_var =
238
42.5k
              ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0 +
239
42.5k
                                         pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1 + 2);
240
241
42.5k
          payload_cnt_bits += ixheaace_write_bits(
242
42.5k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->p, (UWORD8)tmp_var);
243
244
42.5k
          tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0 +
245
42.5k
                    pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1 + 1;
246
247
179k
          for (i = 0; i < tmp_var; i++) {
248
137k
            payload_cnt_bits += ixheaace_write_bits(
249
137k
                pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f_lr[i], SBR_RES_BITS);
250
137k
          }
251
42.5k
          break;
252
0
        default:
253
0
          break;
254
589k
      }
255
589k
    } else {
256
      // If PVC mode is non-zero, bit stream parameters are updated here
257
16.6k
      if (pstr_sbr_env_info->no_of_envelopes > 1) {
258
5.31k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 8, SBR_PVC_NOISE_POSITION_BITS);
259
11.3k
      } else {
260
11.3k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SBR_PVC_NOISE_POSITION_BITS);
261
11.3k
      }
262
16.6k
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SBR_PVC_VAR_LEN_HF_BITS);
263
16.6k
    }
264
606k
  } else {
265
212k
    payload_cnt_bits += ixheaace_write_bits(
266
212k
        pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type, LDSBR_CLA_BITS);
267
268
212k
    switch (pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type) {
269
144k
      case IXHEAACE_FIXFIX:
270
144k
        tmp_var = ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env);
271
144k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ENV_BITS);
272
144k
        if (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env == 1) {
273
137k
          payload_cnt_bits += ixheaace_write_bits(
274
137k
              pstr_bs_handle, pstr_sbr_env_info->curr_sbr_amp_res, SI_SBR_AMP_RES_BITS);
275
137k
        }
276
144k
        payload_cnt_bits += ixheaace_write_bits(
277
144k
            pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f[0], SBR_RES_BITS);
278
144k
        break;
279
280
67.5k
      case IXHEAACE_LD_TRAN:
281
67.5k
        payload_cnt_bits += ixheaace_write_bits(
282
67.5k
            pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->bs_transient_position,
283
67.5k
            IXHEAACE_SBR_TRAN_BITS);
284
265k
        for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env; i++) {
285
198k
          payload_cnt_bits += ixheaace_write_bits(
286
198k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f[i], SBR_RES_BITS);
287
198k
        }
288
67.5k
        break;
289
0
      default:
290
0
        break;
291
212k
    }
292
212k
  }
293
294
818k
  return payload_cnt_bits;
295
818k
}
296
297
static WORD32 ixheaace_encode_sbr_dtdf(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,
298
                                       ixheaace_bit_buf_handle pstr_bs_handle,
299
                                       ixheaace_sbr_codec_type sbr_codec, WORD32 usac_indep_flag,
300
1.04M
                                       WORD32 sbr_pvc_mode) {
301
1.04M
  WORD32 i, payload_cnt_bits = 0, num_of_noise_env;
302
303
1.04M
  num_of_noise_env = (pstr_sbr_env_info->no_of_envelopes > 1) ? 2 : 1;
304
305
1.04M
  if (USAC_SBR != sbr_codec) {
306
1.71M
    for (i = 0; i < pstr_sbr_env_info->no_of_envelopes; ++i) {
307
1.09M
      payload_cnt_bits +=
308
1.09M
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->domain_vec[i], SBR_DIR_BITS);
309
1.09M
    }
310
620k
  }
311
312
428k
  else {
313
428k
    if (sbr_pvc_mode == 0) {
314
411k
      WORD32 start_env = 0;
315
411k
      if (1 == usac_indep_flag) {
316
26.9k
        start_env = 1;
317
26.9k
      }
318
903k
      for (i = start_env; i < pstr_sbr_env_info->no_of_envelopes; ++i) {
319
491k
        payload_cnt_bits +=
320
491k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->domain_vec[i], SBR_DIR_BITS);
321
491k
      }
322
411k
    } else {
323
      /* Do nothing */
324
16.6k
    }
325
428k
  }
326
1.04M
  if (USAC_SBR != sbr_codec) {
327
1.51M
    for (i = 0; i < num_of_noise_env; ++i) {
328
890k
      payload_cnt_bits += ixheaace_write_bits(
329
890k
          pstr_bs_handle, pstr_sbr_env_info->domain_vec_noise[i], SBR_DIR_BITS);
330
890k
    }
331
620k
  } else {
332
428k
    WORD32 start_env = 0;
333
428k
    if (1 == usac_indep_flag) {
334
27.9k
      start_env = 1;
335
27.9k
    }
336
337
903k
    for (i = start_env; i < num_of_noise_env; ++i) {
338
475k
      payload_cnt_bits += ixheaace_write_bits(
339
475k
          pstr_bs_handle, pstr_sbr_env_info->domain_vec_noise[i], SBR_DIR_BITS);
340
475k
    }
341
428k
  }
342
1.04M
  return payload_cnt_bits;
343
1.04M
}
344
345
static WORD32 ixheaace_write_noise_lvl_data(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,
346
                                            ixheaace_bit_buf_handle pstr_bs_handle,
347
1.04M
                                            WORD32 coupling) {
348
1.04M
  WORD32 j, i, payload_cnt_bits = 0;
349
1.04M
  WORD32 n_noise_envelopes = ((pstr_sbr_env_info->no_of_envelopes > 1) ? 2 : 1);
350
351
2.43M
  for (i = 0; i < n_noise_envelopes; i++) {
352
1.39M
    switch (pstr_sbr_env_info->domain_vec_noise[i]) {
353
653k
      case FREQ:
354
355
653k
        if (coupling && pstr_sbr_env_info->balance) {
356
61.2k
          payload_cnt_bits += ixheaace_write_bits(
357
61.2k
              pstr_bs_handle,
358
61.2k
              pstr_sbr_env_info->noise_level[i * pstr_sbr_env_info->noise_band_count],
359
61.2k
              (UWORD8)pstr_sbr_env_info->si_sbr_start_noise_bits_balance);
360
591k
        } else {
361
591k
          payload_cnt_bits += ixheaace_write_bits(
362
591k
              pstr_bs_handle,
363
591k
              pstr_sbr_env_info->noise_level[i * pstr_sbr_env_info->noise_band_count],
364
591k
              (UWORD8)pstr_sbr_env_info->si_sbr_start_noise_bits);
365
591k
        }
366
367
653k
        for (j = 1 + i * pstr_sbr_env_info->noise_band_count;
368
1.67M
             j < (pstr_sbr_env_info->noise_band_count * (1 + i)); j++) {
369
1.02M
          if (coupling) {
370
315k
            if (pstr_sbr_env_info->balance) {
371
72.5k
              payload_cnt_bits += ixheaace_write_bits(
372
72.5k
                  pstr_bs_handle,
373
72.5k
                  pstr_sbr_env_info
374
72.5k
                      ->ptr_huff_tab_noise_bal_freq_c[pstr_sbr_env_info->noise_level[j] +
375
72.5k
                                                      CODE_BCK_SCF_LAV_BALANCE11],
376
72.5k
                  pstr_sbr_env_info
377
72.5k
                      ->ptr_huff_tab_noise_bal_freq_l[pstr_sbr_env_info->noise_level[j] +
378
72.5k
                                                      CODE_BCK_SCF_LAV_BALANCE11]);
379
243k
            } else {
380
243k
              payload_cnt_bits += ixheaace_write_bits(
381
243k
                  pstr_bs_handle,
382
243k
                  pstr_sbr_env_info
383
243k
                      ->ptr_huff_tab_noise_lvl_freq_c[pstr_sbr_env_info->noise_level[j] +
384
243k
                                                      CODE_BCK_SCF_LAV11],
385
243k
                  pstr_sbr_env_info
386
243k
                      ->ptr_huff_tab_noise_lvl_freq_l[pstr_sbr_env_info->noise_level[j] +
387
243k
                                                      CODE_BCK_SCF_LAV11]);
388
243k
            }
389
706k
          } else {
390
706k
            payload_cnt_bits += ixheaace_write_bits(
391
706k
                pstr_bs_handle,
392
706k
                pstr_sbr_env_info->ptr_huff_tab_noise_freq_c[pstr_sbr_env_info->noise_level[j] +
393
706k
                                                             CODE_BCK_SCF_LAV11],
394
706k
                pstr_sbr_env_info->ptr_huff_tab_noise_freq_l[pstr_sbr_env_info->noise_level[j] +
395
706k
                                                             CODE_BCK_SCF_LAV11]);
396
706k
          }
397
1.02M
        }
398
653k
        break;
399
400
738k
      case TIME:
401
738k
        for (j = i * pstr_sbr_env_info->noise_band_count;
402
2.55M
             j < (pstr_sbr_env_info->noise_band_count * (1 + i)); j++) {
403
1.81M
          if (coupling) {
404
1.02M
            if (pstr_sbr_env_info->balance) {
405
641k
              payload_cnt_bits += ixheaace_write_bits(
406
641k
                  pstr_bs_handle,
407
641k
                  pstr_sbr_env_info
408
641k
                      ->ptr_huff_tab_noise_bal_time_c[pstr_sbr_env_info->noise_level[j] +
409
641k
                                                      CODE_BCK_SCF_LAV_BALANCE11],
410
641k
                  pstr_sbr_env_info
411
641k
                      ->ptr_huff_tab_noise_bal_time_l[pstr_sbr_env_info->noise_level[j] +
412
641k
                                                      CODE_BCK_SCF_LAV_BALANCE11]);
413
641k
            } else {
414
379k
              payload_cnt_bits += ixheaace_write_bits(
415
379k
                  pstr_bs_handle,
416
379k
                  pstr_sbr_env_info
417
379k
                      ->ptr_huff_tab_noise_lvl_time_c[pstr_sbr_env_info->noise_level[j] +
418
379k
                                                      CODE_BCK_SCF_LAV11],
419
379k
                  pstr_sbr_env_info
420
379k
                      ->ptr_huff_tab_noise_lvl_time_l[pstr_sbr_env_info->noise_level[j] +
421
379k
                                                      CODE_BCK_SCF_LAV11]);
422
379k
            }
423
1.02M
          } else {
424
794k
            payload_cnt_bits += ixheaace_write_bits(
425
794k
                pstr_bs_handle,
426
794k
                pstr_sbr_env_info
427
794k
                    ->ptr_huff_tab_noise_lvl_time_c[pstr_sbr_env_info->noise_level[j] +
428
794k
                                                    CODE_BCK_SCF_LAV11],
429
794k
                pstr_sbr_env_info
430
794k
                    ->ptr_huff_tab_noise_lvl_time_l[pstr_sbr_env_info->noise_level[j] +
431
794k
                                                    CODE_BCK_SCF_LAV11]);
432
794k
          }
433
1.81M
        }
434
738k
        break;
435
1.39M
    }
436
1.39M
  }
437
1.04M
  return payload_cnt_bits;
438
1.04M
}
439
440
static IA_ERRORCODE ixheaace_write_env_data(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,
441
                                            ixheaace_bit_buf_handle pstr_bs_handle,
442
                                            WORD32 coupling, ixheaace_sbr_codec_type sbr_codec,
443
1.03M
                                            WORD32 *ptr_payload_cnt_bits) {
444
1.03M
  WORD32 j, i, delta;
445
446
1.03M
  *ptr_payload_cnt_bits = 0;
447
448
2.64M
  for (j = 0; j < pstr_sbr_env_info->no_of_envelopes; j++) {
449
1.61M
    if (pstr_sbr_env_info->domain_vec[j] == FREQ) {
450
1.21M
      if (coupling && pstr_sbr_env_info->balance) {
451
187k
        *ptr_payload_cnt_bits +=
452
187k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->ienvelope[j][0],
453
187k
                                (UWORD8)pstr_sbr_env_info->si_sbr_start_env_bits_balance);
454
1.02M
      } else {
455
1.02M
        *ptr_payload_cnt_bits +=
456
1.02M
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->ienvelope[j][0],
457
1.02M
                                (UWORD8)pstr_sbr_env_info->si_sbr_start_env_bits);
458
1.02M
      }
459
1.21M
    }
460
461
15.8M
    for (i = 1 - pstr_sbr_env_info->domain_vec[j]; i < pstr_sbr_env_info->no_scf_bands[j]; i++) {
462
14.2M
      delta = pstr_sbr_env_info->ienvelope[j][i];
463
464
14.2M
      if (coupling && pstr_sbr_env_info->balance) {
465
3.03M
        if (abs(delta) > pstr_sbr_env_info->code_book_scf_lav_balance) {
466
0
          return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_CODEBOOK;
467
0
        }
468
11.1M
      } else {
469
11.1M
        if (abs(delta) > pstr_sbr_env_info->code_book_scf_lav) {
470
567
          return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_CODEBOOK;
471
567
        }
472
11.1M
      }
473
474
14.2M
      if (coupling) {
475
5.98M
        if (pstr_sbr_env_info->balance) {
476
3.03M
          if (pstr_sbr_env_info->domain_vec[j]) {
477
1.34M
            *ptr_payload_cnt_bits += ixheaace_write_bits(
478
1.34M
                pstr_bs_handle,
479
1.34M
                pstr_sbr_env_info
480
1.34M
                    ->ptr_huff_tab_bal_time_c[delta +
481
1.34M
                                              pstr_sbr_env_info->code_book_scf_lav_balance],
482
1.34M
                pstr_sbr_env_info
483
1.34M
                    ->ptr_huff_tab_bal_time_l[delta +
484
1.34M
                                              pstr_sbr_env_info->code_book_scf_lav_balance]);
485
1.69M
          } else {
486
1.69M
            *ptr_payload_cnt_bits += ixheaace_write_bits(
487
1.69M
                pstr_bs_handle,
488
1.69M
                pstr_sbr_env_info
489
1.69M
                    ->ptr_huff_tab_bal_freq_c[delta +
490
1.69M
                                              pstr_sbr_env_info->code_book_scf_lav_balance],
491
1.69M
                pstr_sbr_env_info
492
1.69M
                    ->ptr_huff_tab_bal_freq_l[delta +
493
1.69M
                                              pstr_sbr_env_info->code_book_scf_lav_balance]);
494
1.69M
          }
495
3.03M
        } else {
496
2.94M
          if (pstr_sbr_env_info->domain_vec[j]) {
497
798k
            *ptr_payload_cnt_bits += ixheaace_write_bits(
498
798k
                pstr_bs_handle,
499
798k
                pstr_sbr_env_info
500
798k
                    ->ptr_huff_tab_lvl_time_c[delta + pstr_sbr_env_info->code_book_scf_lav],
501
798k
                pstr_sbr_env_info
502
798k
                    ->ptr_huff_tab_lvl_time_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
503
2.14M
          } else {
504
2.14M
            *ptr_payload_cnt_bits += ixheaace_write_bits(
505
2.14M
                pstr_bs_handle,
506
2.14M
                pstr_sbr_env_info
507
2.14M
                    ->ptr_huff_tab_lvl_freq_c[delta + pstr_sbr_env_info->code_book_scf_lav],
508
2.14M
                pstr_sbr_env_info
509
2.14M
                    ->ptr_huff_tab_lvl_freq_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
510
2.14M
          }
511
2.94M
        }
512
8.22M
      } else {
513
8.22M
        if (pstr_sbr_env_info->domain_vec[j]) {
514
1.37M
          *ptr_payload_cnt_bits += ixheaace_write_bits(
515
1.37M
              pstr_bs_handle,
516
1.37M
              pstr_sbr_env_info
517
1.37M
                  ->ptr_huff_tab_time_c[delta + pstr_sbr_env_info->code_book_scf_lav],
518
1.37M
              pstr_sbr_env_info
519
1.37M
                  ->ptr_huff_tab_time_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
520
6.84M
        } else {
521
6.84M
          *ptr_payload_cnt_bits += ixheaace_write_bits(
522
6.84M
              pstr_bs_handle,
523
6.84M
              pstr_sbr_env_info
524
6.84M
                  ->ptr_huff_tab_freq_c[delta + pstr_sbr_env_info->code_book_scf_lav],
525
6.84M
              pstr_sbr_env_info
526
6.84M
                  ->ptr_huff_tab_freq_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
527
6.84M
        }
528
8.22M
      }
529
14.2M
    }
530
1.61M
    if (USAC_SBR == sbr_codec) {
531
518k
      if (1 == pstr_sbr_env_info->sbr_inter_tes) {
532
222k
        *ptr_payload_cnt_bits +=
533
222k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->ptr_sbr_inter_tes_shape[j],
534
222k
                                IXHEAACE_SBR_TES_SHAPE_BITS);
535
222k
        if (1 == pstr_sbr_env_info->ptr_sbr_inter_tes_shape[j]) {
536
0
          *ptr_payload_cnt_bits += ixheaace_write_bits(
537
0
              pstr_bs_handle, pstr_sbr_env_info->ptr_sbr_inter_tes_shape_mode[j],
538
0
              IXHEAACE_SBR_TES_SHAPE_MODE_BITS);
539
0
        }
540
222k
      }
541
518k
    }
542
1.61M
  }
543
544
1.03M
  return IA_NO_ERROR;
545
1.03M
}
546
547
static WORD32 ixheaace_write_synthetic_coding_data(ixheaace_pstr_sbr_env_data pstr_sbr_env_info,
548
                                                   ixheaace_bit_buf_handle pstr_bs_handle,
549
                                                   ixheaace_sbr_codec_type sbr_codec,
550
                                                   WORD32 sbr_pvc_mode)
551
552
1.04M
{
553
1.04M
  WORD32 i;
554
1.04M
  WORD32 payload_cnt_bits = 0;
555
556
1.04M
  payload_cnt_bits +=
557
1.04M
      ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->add_harmonic_flag, 1);
558
559
1.04M
  if (pstr_sbr_env_info->add_harmonic_flag) {
560
5.60M
    for (i = 0; i < pstr_sbr_env_info->no_harmonics; i++) {
561
5.15M
      payload_cnt_bits +=
562
5.15M
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->add_harmonic[i], 1);
563
5.15M
    }
564
447k
    if (USAC_SBR == sbr_codec && 0 != sbr_pvc_mode) {
565
8.69k
      if (pstr_sbr_env_info->sbr_sinusoidal_pos_flag) {
566
0
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 1, 1);
567
0
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 31, 5);
568
8.69k
      } else {
569
8.69k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, 1);
570
8.69k
      }
571
8.69k
    }
572
447k
  }
573
1.04M
  return payload_cnt_bits;
574
1.04M
}
575
576
static IA_ERRORCODE ixheaace_encode_sbr_single_channel_element(
577
    ixheaace_pstr_sbr_env_data pstr_sbr_env_info, ixheaace_bit_buf_handle pstr_bs_handle,
578
    ixheaace_sbr_codec_type sbr_codec, WORD32 *ptr_num_bits)
579
580
240k
{
581
240k
  WORD32 payload_cnt_bits = 0;
582
240k
  IA_ERRORCODE err_code = IA_NO_ERROR;
583
584
240k
  if (sbr_codec != USAC_SBR) {
585
110k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, 1);
586
129k
  } else {
587
129k
    if (pstr_sbr_env_info->harmonic_sbr) {
588
      // USAC Harmonic SBR data
589
64.1k
      payload_cnt_bits +=
590
64.1k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_patching_mode, 1);
591
64.1k
      if (0 == pstr_sbr_env_info->sbr_patching_mode) {
592
52.8k
        payload_cnt_bits +=
593
52.8k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_oversampling_flag, 1);
594
52.8k
        payload_cnt_bits +=
595
52.8k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_pitchin_bins_flag, 1);
596
52.8k
        if (0 != pstr_sbr_env_info->sbr_pitchin_bins_flag) {
597
52.8k
          payload_cnt_bits +=
598
52.8k
              ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_pitchin_bins, 7);
599
52.8k
        }
600
52.8k
      }
601
64.1k
    }
602
129k
  }
603
240k
  payload_cnt_bits += ixheaace_encode_sbr_grid(pstr_sbr_env_info, pstr_bs_handle, sbr_codec);
604
240k
  if (sbr_codec == USAC_SBR) {
605
129k
    payload_cnt_bits += ixheaace_encode_sbr_dtdf(pstr_sbr_env_info, pstr_bs_handle, sbr_codec,
606
129k
                                                 pstr_sbr_env_info->usac_indep_flag,
607
129k
                                                 pstr_sbr_env_info->sbr_pvc_mode);
608
129k
  } else {
609
110k
    payload_cnt_bits +=
610
110k
        ixheaace_encode_sbr_dtdf(pstr_sbr_env_info, pstr_bs_handle, sbr_codec, 0, 0);
611
110k
  }
612
613
240k
  {
614
240k
    WORD32 i;
615
731k
    for (i = 0; i < pstr_sbr_env_info->noise_band_count; i++) {
616
491k
      payload_cnt_bits += ixheaace_write_bits(
617
491k
          pstr_bs_handle, pstr_sbr_env_info->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
618
491k
    }
619
240k
  }
620
240k
  if (sbr_codec != USAC_SBR) {
621
110k
    WORD32 env_data_len;
622
110k
    err_code =
623
110k
        ixheaace_write_env_data(pstr_sbr_env_info, pstr_bs_handle, 0, sbr_codec, &env_data_len);
624
110k
    if (err_code) {
625
46
      *ptr_num_bits = payload_cnt_bits;
626
46
      return err_code;
627
46
    }
628
110k
    payload_cnt_bits += env_data_len;
629
129k
  } else {
630
129k
    if (0 == pstr_sbr_env_info->sbr_pvc_mode) {
631
112k
      WORD32 env_data_len;
632
112k
      err_code =
633
112k
          ixheaace_write_env_data(pstr_sbr_env_info, pstr_bs_handle, 0, sbr_codec, &env_data_len);
634
112k
      if (err_code) {
635
0
        *ptr_num_bits = payload_cnt_bits;
636
0
        return err_code;
637
0
      }
638
112k
      payload_cnt_bits += env_data_len;
639
112k
    } else {
640
      // PVC envelope goes here
641
16.6k
      payload_cnt_bits += iusace_encode_pvc_envelope(pstr_bs_handle, &pstr_sbr_env_info->pvc_info,
642
16.6k
                                                     pstr_sbr_env_info->usac_indep_flag);
643
16.6k
    }
644
129k
  }
645
240k
  payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_info, pstr_bs_handle, 0);
646
647
240k
  if (USAC_SBR == sbr_codec) {
648
129k
    payload_cnt_bits += ixheaace_write_synthetic_coding_data(
649
129k
        pstr_sbr_env_info, pstr_bs_handle, sbr_codec, pstr_sbr_env_info->sbr_pvc_mode);
650
129k
  } else {
651
110k
    payload_cnt_bits +=
652
110k
        ixheaace_write_synthetic_coding_data(pstr_sbr_env_info, pstr_bs_handle, sbr_codec, 0);
653
110k
  }
654
655
240k
  *ptr_num_bits = payload_cnt_bits;
656
240k
  return err_code;
657
240k
}
658
659
static IA_ERRORCODE ixheaace_encode_sbr_channel_pair_element(
660
    ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,
661
    ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right, ixheaace_bit_buf_handle pstr_bs_handle,
662
404k
    WORD32 coupling, ixheaace_sbr_codec_type sbr_codec, WORD32 *ptr_num_bits) {
663
404k
  IA_ERRORCODE err_code = IA_NO_ERROR;
664
404k
  WORD32 payload_cnt_bits = 0;
665
404k
  WORD32 env_data_len;
666
404k
  WORD32 i = 0;
667
668
404k
  if (USAC_SBR != sbr_codec) {
669
254k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, 1); /* no reserved bits */
670
254k
  }
671
672
404k
  payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, coupling, SI_SBR_COUPLING_BITS);
673
674
404k
  if (coupling) {
675
230k
    if (sbr_codec == USAC_SBR && pstr_sbr_env_data_left->harmonic_sbr) {
676
      // USAC Harmonic SBR data
677
49.0k
      payload_cnt_bits +=
678
49.0k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_patching_mode, 1);
679
49.0k
      if (0 == pstr_sbr_env_data_left->sbr_patching_mode) {
680
33.5k
        payload_cnt_bits +=
681
33.5k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_oversampling_flag, 1);
682
33.5k
        payload_cnt_bits +=
683
33.5k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins_flag, 1);
684
33.5k
        if (0 != pstr_sbr_env_data_left->sbr_pitchin_bins_flag) {
685
33.0k
          payload_cnt_bits +=
686
33.0k
              ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins, 7);
687
33.0k
        }
688
33.5k
      }
689
49.0k
    }
690
230k
    payload_cnt_bits +=
691
230k
        ixheaace_encode_sbr_grid(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec);
692
693
230k
    payload_cnt_bits +=
694
230k
        ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec,
695
230k
                                 pstr_sbr_env_data_left->usac_indep_flag, 0);
696
697
230k
    payload_cnt_bits +=
698
230k
        ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_right, pstr_bs_handle, sbr_codec,
699
230k
                                 pstr_sbr_env_data_left->usac_indep_flag, 0);
700
701
822k
    for (i = 0; i < pstr_sbr_env_data_left->noise_band_count; i++) {
702
591k
      payload_cnt_bits += ixheaace_write_bits(
703
591k
          pstr_bs_handle, pstr_sbr_env_data_left->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
704
591k
    }
705
706
230k
    err_code = ixheaace_write_env_data(pstr_sbr_env_data_left, pstr_bs_handle, 1, sbr_codec,
707
230k
                                       &env_data_len);
708
230k
    if (err_code) {
709
0
      *ptr_num_bits = payload_cnt_bits;
710
0
      return err_code;
711
0
    }
712
713
230k
    payload_cnt_bits += env_data_len;
714
715
230k
    payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_left, pstr_bs_handle, 1);
716
717
230k
    err_code = ixheaace_write_env_data(pstr_sbr_env_data_right, pstr_bs_handle, 1, sbr_codec,
718
230k
                                       &env_data_len);
719
230k
    if (err_code) {
720
0
      *ptr_num_bits = payload_cnt_bits;
721
0
      return err_code;
722
0
    }
723
230k
    payload_cnt_bits += env_data_len;
724
725
230k
    payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_right, pstr_bs_handle, 1);
726
727
230k
    payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_left,
728
230k
                                                             pstr_bs_handle, sbr_codec, 0);
729
730
230k
    payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_right,
731
230k
                                                             pstr_bs_handle, sbr_codec, 0);
732
230k
  } else {
733
173k
    if (sbr_codec == USAC_SBR && pstr_sbr_env_data_left->harmonic_sbr) {
734
      // USAC Harmonic SBR data
735
40.3k
      payload_cnt_bits +=
736
40.3k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_patching_mode, 1);
737
40.3k
      if (0 == pstr_sbr_env_data_left->sbr_patching_mode) {
738
26.5k
        payload_cnt_bits +=
739
26.5k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_oversampling_flag, 1);
740
26.5k
        payload_cnt_bits +=
741
26.5k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins_flag, 1);
742
26.5k
        if (0 != pstr_sbr_env_data_left->sbr_pitchin_bins_flag) {
743
26.1k
          payload_cnt_bits +=
744
26.1k
              ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins, 7);
745
26.1k
        }
746
26.5k
      }
747
748
40.3k
      payload_cnt_bits +=
749
40.3k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_right->sbr_patching_mode, 1);
750
40.3k
      if (0 == pstr_sbr_env_data_right->sbr_patching_mode) {
751
26.5k
        payload_cnt_bits += ixheaace_write_bits(
752
26.5k
            pstr_bs_handle, pstr_sbr_env_data_right->sbr_oversampling_flag, 1);
753
26.5k
        payload_cnt_bits += ixheaace_write_bits(
754
26.5k
            pstr_bs_handle, pstr_sbr_env_data_right->sbr_pitchin_bins_flag, 1);
755
26.5k
        if (0 != pstr_sbr_env_data_right->sbr_pitchin_bins_flag) {
756
18.3k
          payload_cnt_bits +=
757
18.3k
              ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_right->sbr_pitchin_bins, 7);
758
18.3k
        }
759
26.5k
      }
760
40.3k
    }
761
173k
    payload_cnt_bits +=
762
173k
        ixheaace_encode_sbr_grid(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec);
763
764
173k
    payload_cnt_bits +=
765
173k
        ixheaace_encode_sbr_grid(pstr_sbr_env_data_right, pstr_bs_handle, sbr_codec);
766
767
173k
    payload_cnt_bits +=
768
173k
        ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec,
769
173k
                                 pstr_sbr_env_data_left->usac_indep_flag, 0);
770
771
173k
    payload_cnt_bits +=
772
173k
        ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_right, pstr_bs_handle, sbr_codec,
773
173k
                                 pstr_sbr_env_data_left->usac_indep_flag, 0);
774
775
679k
    for (i = 0; i < pstr_sbr_env_data_left->noise_band_count; i++) {
776
505k
      payload_cnt_bits += ixheaace_write_bits(
777
505k
          pstr_bs_handle, pstr_sbr_env_data_left->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
778
505k
    }
779
780
679k
    for (i = 0; i < pstr_sbr_env_data_right->noise_band_count; i++) {
781
505k
      payload_cnt_bits += ixheaace_write_bits(
782
505k
          pstr_bs_handle, pstr_sbr_env_data_right->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
783
505k
    }
784
785
173k
    err_code = ixheaace_write_env_data(pstr_sbr_env_data_left, pstr_bs_handle, 0, sbr_codec,
786
173k
                                       &env_data_len);
787
173k
    if (err_code) {
788
463
      *ptr_num_bits = payload_cnt_bits;
789
463
      return err_code;
790
463
    }
791
173k
    payload_cnt_bits += env_data_len;
792
793
173k
    err_code = ixheaace_write_env_data(pstr_sbr_env_data_right, pstr_bs_handle, 0, sbr_codec,
794
173k
                                       &env_data_len);
795
173k
    if (err_code) {
796
58
      *ptr_num_bits = payload_cnt_bits;
797
58
      return err_code;
798
58
    }
799
173k
    payload_cnt_bits += env_data_len;
800
801
173k
    payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_left, pstr_bs_handle, 0);
802
803
173k
    payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_right, pstr_bs_handle, 0);
804
805
173k
    payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_left,
806
173k
                                                             pstr_bs_handle, sbr_codec, 0);
807
808
173k
    payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_right,
809
173k
                                                             pstr_bs_handle, sbr_codec, 0);
810
173k
  }
811
812
403k
  *ptr_num_bits = payload_cnt_bits;
813
403k
  return err_code;
814
404k
}
815
816
static WORD32 iexhaace_esbr_write_bs(ixheaace_str_esbr_bs_data *pstr_esbr_bs_data,
817
56.7k
                                     ixheaace_bit_buf_handle pstr_bs_handle) {
818
56.7k
  WORD32 num_bits = 0;
819
56.7k
  num_bits +=
820
56.7k
      ixheaace_write_bits(pstr_bs_handle, EXTENSION_ID_ESBR_CODING, SI_SBR_EXTENSION_ID_BITS);
821
56.7k
  num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_preprocessing, 1);
822
56.7k
  if (1 == pstr_esbr_bs_data->sbr_num_chan) {
823
32.3k
    num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[0], 1);
824
32.3k
    if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
825
24.2k
      num_bits +=
826
24.2k
          ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[0], 1);
827
24.2k
      num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[0], 1);
828
24.2k
      if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
829
23.0k
        num_bits +=
830
23.0k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
831
23.0k
      }
832
24.2k
    }
833
32.3k
  } else if (2 == pstr_esbr_bs_data->sbr_num_chan) {
834
24.4k
    if (pstr_esbr_bs_data->sbr_coupling) {
835
18.7k
      num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[0], 1);
836
18.7k
      if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
837
16.5k
        num_bits +=
838
16.5k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[0], 1);
839
16.5k
        num_bits +=
840
16.5k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[0], 1);
841
16.5k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
842
15.7k
          num_bits +=
843
15.7k
              ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
844
15.7k
        }
845
16.5k
      }
846
18.7k
    } else {
847
5.66k
      num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[0], 1);
848
5.66k
      if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
849
4.72k
        num_bits +=
850
4.72k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[0], 1);
851
4.72k
        num_bits +=
852
4.72k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[0], 1);
853
4.72k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
854
4.34k
          num_bits +=
855
4.34k
              ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
856
4.34k
        } else {
857
380
          pstr_esbr_bs_data->sbr_patching_mode[0] = pstr_esbr_bs_data->sbr_patching_mode[0];
858
380
        }
859
4.72k
      }
860
5.66k
      num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[1], 1);
861
5.66k
      if (pstr_esbr_bs_data->sbr_patching_mode[1] == 0) {
862
4.72k
        num_bits +=
863
4.72k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[1], 1);
864
4.72k
        num_bits +=
865
4.72k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[1], 1);
866
4.72k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[1] == 1) {
867
4.32k
          num_bits +=
868
4.32k
              ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
869
4.32k
        }
870
4.72k
      }
871
5.66k
    }
872
24.4k
  }
873
56.7k
  if (num_bits < 8) {
874
13.1k
    num_bits += ixheaace_write_bits(pstr_bs_handle, 0, (UWORD8)(8 - num_bits));
875
13.1k
  }
876
877
56.7k
  return num_bits;
878
56.7k
}
879
880
static WORD32 ixheaace_get_sbr_extended_data_size(struct ixheaace_ps_enc *pstr_ps_handle,
881
                                                  WORD32 is_hdr_active,
882
                                                  ixheaace_str_sbr_tabs *pstr_sbr_tab,
883
                                                  WORD32 is_esbr,
884
365k
                                                  ixheaace_str_esbr_bs_data *pstr_esbr_data) {
885
365k
  WORD32 ext_data_bits = 0;
886
887
365k
  if (pstr_ps_handle) {
888
24.1k
    ext_data_bits +=
889
24.1k
        ixheaace_enc_write_ps_data(pstr_ps_handle, is_hdr_active, pstr_sbr_tab->ptr_ps_tab);
890
24.1k
  }
891
365k
  if (is_esbr) {
892
94.4k
    ext_data_bits += ixheaace_get_esbr_ext_data_size(pstr_esbr_data);
893
94.4k
  }
894
895
365k
  if (ext_data_bits != 0) {
896
68.0k
    ext_data_bits += SI_SBR_EXTENSION_ID_BITS;
897
68.0k
  }
898
899
365k
  return (ext_data_bits + 7) >> 3;
900
365k
}
901
902
static VOID ixheaace_encode_extended_data(struct ixheaace_ps_enc *pstr_ps_handle,
903
                                          WORD32 is_hdr_active,
904
                                          ixheaace_bit_buf_handle pstr_bs_prev,
905
                                          WORD32 *ptr_sbr_hdr_bits,
906
                                          ixheaace_bit_buf_handle pstr_bs_handle,
907
                                          WORD32 *ptr_payload_bits,
908
                                          ixheaace_str_sbr_tabs *pstr_sbr_tab, WORD32 is_esbr,
909
365k
                                          ixheaace_str_esbr_bs_data *pstr_esbr_data) {
910
365k
  WORD32 ext_data_size;
911
365k
  WORD32 payload_bits_in = *ptr_payload_bits;
912
365k
  WORD32 payload_cnt_bits = 0;
913
914
365k
  ext_data_size = ixheaace_get_sbr_extended_data_size(pstr_ps_handle, is_hdr_active, pstr_sbr_tab,
915
365k
                                                      is_esbr, pstr_esbr_data);
916
917
365k
  if (ext_data_size != 0) {
918
68.0k
    if (pstr_ps_handle && ixheaace_append_ps_bitstream(pstr_ps_handle, NULL_PTR, 0)) {
919
24.1k
      ixheaace_bit_buf bitbuf_tmp;
920
24.1k
      UWORD8 tmp[IXHEAACE_MAX_PAYLOAD_SIZE];
921
24.1k
      WORD32 max_ext_size = (1 << SI_SBR_EXTENSION_SIZE_BITS) - 1;
922
24.1k
      WORD32 num_bits;
923
924
24.1k
      num_bits = ia_enhaacplus_enc_get_bits_available(&pstr_ps_handle->ps_bit_buf);
925
24.1k
      num_bits += SI_SBR_EXTENSION_ID_BITS;
926
24.1k
      if (is_esbr) {
927
12.8k
        num_bits += ixheaace_get_esbr_ext_data_size(pstr_esbr_data);
928
12.8k
        num_bits += SI_SBR_EXTENSION_ID_BITS;
929
12.8k
      }
930
24.1k
      ext_data_size = (num_bits + 7) >> 3;
931
24.1k
      if (ia_enhaacplus_enc_get_bits_available(pstr_bs_prev) == 0) {
932
303
        pstr_ps_handle->hdr_bits_prev_frame = *ptr_sbr_hdr_bits;
933
303
        ia_enhaacplus_enc_copy_bitbuf(pstr_bs_handle, pstr_bs_prev);
934
23.8k
      } else {
935
23.8k
        WORD32 tmp_bits;
936
23.8k
        ia_enhaacplus_enc_create_bitbuffer(&bitbuf_tmp, tmp, sizeof(tmp));
937
23.8k
        tmp_bits = *ptr_sbr_hdr_bits;
938
23.8k
        *ptr_sbr_hdr_bits = pstr_ps_handle->hdr_bits_prev_frame;
939
23.8k
        pstr_ps_handle->hdr_bits_prev_frame = tmp_bits;
940
23.8k
        ixheaace_copy_bitbuf_to_and_fro(pstr_bs_prev, pstr_bs_handle);
941
23.8k
      }
942
24.1k
      ixheaace_write_bits(pstr_bs_handle, 1, SI_SBR_EXTENDED_DATA_BITS);
943
944
24.1k
      if (ext_data_size < max_ext_size) {
945
23.3k
        ixheaace_write_bits(pstr_bs_handle, ext_data_size, SI_SBR_EXTENSION_SIZE_BITS);
946
23.3k
      } else {
947
784
        ixheaace_write_bits(pstr_bs_handle, max_ext_size, SI_SBR_EXTENSION_SIZE_BITS);
948
784
        ixheaace_write_bits(pstr_bs_handle, ext_data_size - max_ext_size,
949
784
                            SI_SBR_EXTENSION_ESC_COUNT_BITS);
950
784
      }
951
24.1k
      WORD32 start_bits = pstr_bs_handle->cnt_bits;
952
24.1k
      *ptr_payload_bits =
953
24.1k
          ixheaace_append_ps_bitstream(pstr_ps_handle, pstr_bs_handle, ptr_sbr_hdr_bits);
954
955
24.1k
      if (is_esbr) {
956
12.8k
        *ptr_payload_bits += iexhaace_esbr_write_bs(pstr_esbr_data, pstr_bs_handle);
957
12.8k
      }
958
959
24.1k
      WORD32 fill_bits = (ext_data_size << 3) - (pstr_bs_handle->cnt_bits - start_bits);
960
24.1k
      ixheaace_write_bits(pstr_bs_handle, 0, (UWORD8)fill_bits);
961
24.1k
      *ptr_payload_bits = *ptr_payload_bits + fill_bits;
962
43.9k
    } else {
963
43.9k
      if (is_esbr) {
964
43.9k
        WORD32 max_ext_size = (1 << SI_SBR_EXTENSION_SIZE_BITS) - 1;
965
43.9k
        WORD32 num_bits;
966
43.9k
        num_bits = ixheaace_get_esbr_ext_data_size(pstr_esbr_data);
967
43.9k
        ext_data_size = (num_bits + SI_SBR_EXTENSION_ID_BITS + 7) >> 3;
968
43.9k
        ixheaace_write_bits(pstr_bs_handle, 1, SI_SBR_EXTENDED_DATA_BITS);
969
43.9k
        if (ext_data_size < max_ext_size) {
970
43.9k
          ixheaace_write_bits(pstr_bs_handle, ext_data_size, SI_SBR_EXTENSION_SIZE_BITS);
971
43.9k
        } else {
972
0
          ixheaace_write_bits(pstr_bs_handle, max_ext_size, SI_SBR_EXTENSION_SIZE_BITS);
973
0
          ixheaace_write_bits(pstr_bs_handle, ext_data_size - max_ext_size,
974
0
                              SI_SBR_EXTENSION_ESC_COUNT_BITS);
975
0
        }
976
43.9k
        WORD32 start_bits = pstr_bs_handle->cnt_bits;
977
43.9k
        *ptr_payload_bits += iexhaace_esbr_write_bs(pstr_esbr_data, pstr_bs_handle);
978
43.9k
        UWORD8 fill_bits =
979
43.9k
            (UWORD8)((ext_data_size << 3) - (pstr_bs_handle->cnt_bits - start_bits));
980
43.9k
        ixheaace_write_bits(pstr_bs_handle, 0, fill_bits);
981
43.9k
        *ptr_payload_bits = *ptr_payload_bits + fill_bits;
982
43.9k
      } else {
983
0
        WORD32 max_ext_size = (1 << SI_SBR_EXTENSION_SIZE_BITS) - 1;
984
0
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 1, SI_SBR_EXTENDED_DATA_BITS);
985
986
0
        if (ext_data_size < max_ext_size) {
987
0
          payload_cnt_bits +=
988
0
              ixheaace_write_bits(pstr_bs_handle, ext_data_size, SI_SBR_EXTENSION_SIZE_BITS);
989
0
        } else {
990
0
          payload_cnt_bits +=
991
0
              ixheaace_write_bits(pstr_bs_handle, max_ext_size, SI_SBR_EXTENSION_SIZE_BITS);
992
993
0
          payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, ext_data_size - max_ext_size,
994
0
                                                  SI_SBR_EXTENSION_ESC_COUNT_BITS);
995
0
        }
996
997
0
        *ptr_payload_bits = payload_cnt_bits + payload_bits_in;
998
0
      }
999
43.9k
    }
1000
297k
  } else {
1001
297k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SI_SBR_EXTENDED_DATA_BITS);
1002
1003
297k
    *ptr_payload_bits = payload_cnt_bits + payload_bits_in;
1004
297k
  }
1005
365k
}
1006
1007
static IA_ERRORCODE ixheaace_encode_sbr_data(
1008
    ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,
1009
    ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right, ixheaace_pstr_common_data pstr_cmon_data,
1010
    ixheaace_sbr_element_type sbr_ele_type, struct ixheaace_ps_enc *pstr_ps_handle,
1011
    WORD32 is_hdr_active, WORD32 coupling, ixheaace_str_sbr_tabs *pstr_sbr_tab,
1012
    ixheaace_sbr_codec_type sbr_codec, WORD32 is_esbr, ixheaace_str_esbr_bs_data *pstr_esbr_data,
1013
644k
    WORD32 *ptr_num_bits) {
1014
644k
  WORD32 payload_cnt_bits = 0;
1015
644k
  IA_ERRORCODE err_code = IA_NO_ERROR;
1016
1017
644k
  switch (sbr_ele_type) {
1018
240k
    case IXHEAACE_SBR_ID_SCE:
1019
1020
240k
      err_code = ixheaace_encode_sbr_single_channel_element(
1021
240k
          pstr_sbr_env_data_left, &pstr_cmon_data->str_sbr_bit_buf, sbr_codec, &payload_cnt_bits);
1022
240k
      if (err_code) {
1023
46
        return err_code;
1024
46
      }
1025
240k
      if (USAC_SBR != sbr_codec) {
1026
110k
        ixheaace_encode_extended_data(
1027
110k
            pstr_ps_handle, is_hdr_active, &pstr_cmon_data->str_sbr_bit_buf_prev,
1028
110k
            &pstr_cmon_data->sbr_hdr_bits, &pstr_cmon_data->str_sbr_bit_buf, &payload_cnt_bits,
1029
110k
            pstr_sbr_tab, is_esbr, pstr_esbr_data);
1030
110k
      }
1031
240k
      break;
1032
404k
    case IXHEAACE_SBR_ID_CPE:
1033
1034
404k
      err_code = ixheaace_encode_sbr_channel_pair_element(
1035
404k
          pstr_sbr_env_data_left, pstr_sbr_env_data_right, &pstr_cmon_data->str_sbr_bit_buf,
1036
404k
          coupling, sbr_codec, &payload_cnt_bits);
1037
404k
      if (err_code) {
1038
521
        return err_code;
1039
521
      }
1040
403k
      if (USAC_SBR != sbr_codec) {
1041
254k
        ixheaace_encode_extended_data(NULL_PTR, 0, NULL_PTR, 0, &pstr_cmon_data->str_sbr_bit_buf,
1042
254k
                                      &payload_cnt_bits, pstr_sbr_tab, is_esbr, pstr_esbr_data);
1043
254k
      }
1044
403k
      break;
1045
644k
  }
1046
1047
644k
  pstr_cmon_data->sbr_data_bits = payload_cnt_bits;
1048
644k
  *ptr_num_bits = payload_cnt_bits;
1049
1050
644k
  pstr_cmon_data->prev_bit_buf_read_offset =
1051
644k
      (WORD32)(pstr_cmon_data->str_sbr_bit_buf_prev.ptr_read_next -
1052
644k
               pstr_cmon_data->str_sbr_bit_buf_prev.ptr_bit_buf_base);
1053
644k
  pstr_cmon_data->prev_bit_buf_write_offset =
1054
644k
      (WORD32)(pstr_cmon_data->str_sbr_bit_buf_prev.ptr_write_next -
1055
644k
               pstr_cmon_data->str_sbr_bit_buf_prev.ptr_bit_buf_base);
1056
1057
644k
  return err_code;
1058
644k
}
1059
1060
static WORD32 ixheaace_encode_sbr_header_data(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,
1061
                                              ixheaace_bit_buf_handle pstr_bs_handle)
1062
1063
28.0k
{
1064
28.0k
  WORD32 payload_cnt_bits = 0;
1065
1066
28.0k
  if (pstr_sbr_hdr != NULL_PTR) {
1067
28.0k
    payload_cnt_bits +=
1068
28.0k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_amp_res, SI_SBR_AMP_RES_BITS);
1069
1070
28.0k
    payload_cnt_bits +=
1071
28.0k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_start_freq, SI_SBR_START_FREQ_BITS);
1072
1073
28.0k
    payload_cnt_bits +=
1074
28.0k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_stop_freq, SI_SBR_STOP_FREQ_BITS);
1075
1076
28.0k
    payload_cnt_bits +=
1077
28.0k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_xover_band, SI_SBR_XOVER_BAND_BITS);
1078
1079
28.0k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SI_SBR_RESERVED_BITS);
1080
1081
28.0k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_1,
1082
28.0k
                                            SI_SBR_HEADER_EXTRA_1_BITS);
1083
1084
28.0k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_2,
1085
28.0k
                                            SI_SBR_HEADER_EXTRA_2_BITS);
1086
1087
28.0k
    if (pstr_sbr_hdr->header_extra_1) {
1088
24.5k
      payload_cnt_bits +=
1089
24.5k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->freq_scale, SI_SBR_FREQ_SCALE_BITS);
1090
1091
24.5k
      payload_cnt_bits +=
1092
24.5k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->alter_scale, SI_SBR_ALTER_SCALE_BITS);
1093
1094
24.5k
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_noise_bands,
1095
24.5k
                                              SI_SBR_NOISE_BANDS_BITS);
1096
24.5k
    }
1097
1098
28.0k
    if (pstr_sbr_hdr->header_extra_2) {
1099
0
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_limiter_bands,
1100
0
                                              SI_SBR_LIMITER_BANDS_BITS);
1101
0
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_limiter_gains,
1102
0
                                              SI_SBR_LIMITER_GAINS_BITS);
1103
1104
0
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_interpol_freq,
1105
0
                                              SI_SBR_INTERPOL_FREQ_BITS);
1106
1107
0
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_smoothing_length,
1108
0
                                              SI_SBR_SMOOTHING_LENGTH_BITS);
1109
0
    }
1110
28.0k
  }
1111
1112
28.0k
  return payload_cnt_bits;
1113
28.0k
}
1114
1115
static WORD32 ia_enhaacplus_enc_encode_sbr_header(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,
1116
                                                  ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,
1117
365k
                                                  ixheaace_pstr_common_data pstr_cmon_data) {
1118
365k
  WORD32 payload_cnt_bits = 0;
1119
1120
365k
  if (pstr_sbr_bs->crc_active) {
1121
0
    pstr_cmon_data->sbr_crc_len = 1;
1122
365k
  } else {
1123
365k
    pstr_cmon_data->sbr_crc_len = 0;
1124
365k
  }
1125
1126
365k
  if (pstr_sbr_bs->header_active) {
1127
28.0k
    payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 1, 1);
1128
1129
28.0k
    payload_cnt_bits +=
1130
28.0k
        ixheaace_encode_sbr_header_data(pstr_sbr_hdr, &pstr_cmon_data->str_sbr_bit_buf);
1131
337k
  } else {
1132
337k
    payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 0, 1);
1133
337k
  }
1134
1135
365k
  pstr_cmon_data->sbr_hdr_bits = payload_cnt_bits;
1136
1137
365k
  return payload_cnt_bits;
1138
365k
}
1139
static WORD32 iusace_encode_sbr_header_data(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,
1140
55.6k
                                            ixheaace_bit_buf_handle pstr_bs_handle) {
1141
55.6k
  WORD32 payload_cnt_bits = 0;
1142
1143
55.6k
  if (pstr_sbr_hdr != NULL_PTR) {
1144
55.6k
    payload_cnt_bits +=
1145
55.6k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_start_freq, SI_SBR_START_FREQ_BITS);
1146
1147
55.6k
    payload_cnt_bits +=
1148
55.6k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_stop_freq, SI_SBR_STOP_FREQ_BITS);
1149
1150
55.6k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_1,
1151
55.6k
                                            SI_SBR_HEADER_EXTRA_1_BITS);
1152
1153
55.6k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_2,
1154
55.6k
                                            SI_SBR_HEADER_EXTRA_2_BITS);
1155
1156
55.6k
    if (pstr_sbr_hdr->header_extra_1) {
1157
30.0k
      payload_cnt_bits +=
1158
30.0k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->freq_scale, SI_SBR_FREQ_SCALE_BITS);
1159
1160
30.0k
      payload_cnt_bits +=
1161
30.0k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->alter_scale, SI_SBR_ALTER_SCALE_BITS);
1162
1163
30.0k
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_noise_bands,
1164
30.0k
                                              SI_SBR_NOISE_BANDS_BITS);
1165
30.0k
    }
1166
1167
55.6k
    if (pstr_sbr_hdr->header_extra_2) {
1168
0
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_limiter_bands,
1169
0
                                              SI_SBR_LIMITER_BANDS_BITS);
1170
1171
0
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_limiter_gains,
1172
0
                                              SI_SBR_LIMITER_GAINS_BITS);
1173
1174
0
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_interpol_freq,
1175
0
                                              SI_SBR_INTERPOL_FREQ_BITS);
1176
1177
0
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_smoothing_length,
1178
0
                                              SI_SBR_SMOOTHING_LENGTH_BITS);
1179
0
    }
1180
55.6k
  }
1181
1182
55.6k
  return payload_cnt_bits;
1183
55.6k
}
1184
1185
static WORD32 ia_usac_enc_encode_sbr_header(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,
1186
                                            ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,
1187
278k
                                            ixheaace_pstr_common_data pstr_cmon_data) {
1188
278k
  WORD32 payload_cnt_bits = 0;
1189
278k
  WORD32 sbr_info_flag = 0;
1190
278k
  WORD32 sbr_hdr_flag = 0;
1191
278k
  if (pstr_sbr_bs->usac_indep_flag) {
1192
16.6k
    sbr_hdr_flag = 1;
1193
16.6k
    sbr_info_flag = 1;
1194
262k
  } else {
1195
262k
    if (pstr_sbr_bs->header_active) {
1196
38.9k
      sbr_info_flag = 1;
1197
38.9k
      payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 1, 1);
1198
38.9k
      sbr_hdr_flag = 1;
1199
38.9k
      payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 1, 1);
1200
223k
    } else {
1201
223k
      payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 0, 1);
1202
223k
    }
1203
262k
  }
1204
1205
278k
  if (1 == sbr_info_flag) {
1206
55.6k
    payload_cnt_bits +=
1207
55.6k
        ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_amp_res, 1);
1208
1209
55.6k
    payload_cnt_bits +=
1210
55.6k
        ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_xover_band, 4);
1211
1212
55.6k
    payload_cnt_bits +=
1213
55.6k
        ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_pre_proc, 1);
1214
55.6k
    if (pstr_sbr_hdr->sbr_pvc_active) {
1215
13.9k
      payload_cnt_bits +=
1216
13.9k
          ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_pvc_mode, 2);
1217
13.9k
    }
1218
55.6k
  }
1219
1220
278k
  if (1 == sbr_hdr_flag) {
1221
55.6k
    WORD32 sbr_def_hdr = 0;
1222
    // SBR default header
1223
55.6k
    payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, sbr_def_hdr, 1);
1224
55.6k
    if (0 == sbr_def_hdr) {
1225
55.6k
      payload_cnt_bits +=
1226
55.6k
          iusace_encode_sbr_header_data(pstr_sbr_hdr, &pstr_cmon_data->str_sbr_bit_buf);
1227
55.6k
    }
1228
55.6k
  }
1229
278k
  pstr_cmon_data->sbr_hdr_bits = payload_cnt_bits;
1230
1231
278k
  return payload_cnt_bits;
1232
278k
}
1233
IA_ERRORCODE
1234
ixheaace_write_env_single_channel_element(
1235
    ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr, ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,
1236
    ixheaace_pstr_sbr_env_data pstr_sbr_env_info, struct ixheaace_ps_enc *pstr_ps_handle,
1237
    ixheaace_pstr_common_data pstr_cmon_data, ixheaace_str_sbr_tabs *pstr_sbr_tab,
1238
    ixheaace_sbr_codec_type sbr_codec, WORD32 is_esbr, ixheaace_str_esbr_bs_data *pstr_esbr_data,
1239
240k
    WORD32 *ptr_num_bits) {
1240
240k
  WORD32 payload_cnt_bits = 0;
1241
240k
  WORD32 num_sbr_data_bits = 0;
1242
240k
  IA_ERRORCODE err_code = IA_NO_ERROR;
1243
1244
240k
  pstr_cmon_data->sbr_hdr_bits = 0;
1245
240k
  pstr_cmon_data->sbr_data_bits = 0;
1246
240k
  pstr_cmon_data->sbr_crc_len = 0;
1247
1248
240k
  if (pstr_sbr_env_info != NULL_PTR) {
1249
240k
    if (USAC_SBR == sbr_codec) {
1250
129k
      payload_cnt_bits +=
1251
129k
          ia_usac_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1252
129k
    } else {
1253
      /* write header */
1254
110k
      payload_cnt_bits +=
1255
110k
          ia_enhaacplus_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1256
110k
    }
1257
    /* write data */
1258
240k
    err_code =
1259
240k
        ixheaace_encode_sbr_data(pstr_sbr_env_info, NULL_PTR, pstr_cmon_data, IXHEAACE_SBR_ID_SCE,
1260
240k
                                 pstr_ps_handle, pstr_sbr_bs->header_active, 0, pstr_sbr_tab,
1261
240k
                                 sbr_codec, is_esbr, pstr_esbr_data, &num_sbr_data_bits);
1262
240k
    if (err_code) {
1263
46
      return err_code;
1264
46
    }
1265
240k
    payload_cnt_bits += num_sbr_data_bits;
1266
240k
  }
1267
1268
240k
  *ptr_num_bits = payload_cnt_bits;
1269
240k
  return err_code;
1270
240k
}
1271
1272
IA_ERRORCODE
1273
ixheaace_write_env_channel_pair_element(
1274
    ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr, ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,
1275
    ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,
1276
    ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right, ixheaace_pstr_common_data pstr_cmon_data,
1277
    ixheaace_str_sbr_tabs *pstr_sbr_tab, ixheaace_sbr_codec_type sbr_codec, WORD32 is_esbr,
1278
    ixheaace_str_esbr_bs_data *pstr_esbr_data, WORD32 *ptr_num_bits)
1279
1280
404k
{
1281
404k
  WORD32 payload_cnt_bits = 0;
1282
404k
  WORD32 num_sbr_data_bits = 0;
1283
404k
  IA_ERRORCODE err_code = IA_NO_ERROR;
1284
1285
404k
  pstr_cmon_data->sbr_hdr_bits = 0;
1286
404k
  pstr_cmon_data->sbr_data_bits = 0;
1287
404k
  pstr_cmon_data->sbr_crc_len = 0;
1288
1289
  /* write pure SBR data */
1290
404k
  if ((pstr_sbr_env_data_left != NULL_PTR) && (pstr_sbr_env_data_right != NULL_PTR)) {
1291
404k
    if (USAC_SBR == sbr_codec) {
1292
149k
      payload_cnt_bits +=
1293
149k
          ia_usac_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1294
254k
    } else {
1295
      /* write header */
1296
254k
      payload_cnt_bits +=
1297
254k
          ia_enhaacplus_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1298
254k
    }
1299
1300
    /* write data */
1301
404k
    err_code = ixheaace_encode_sbr_data(pstr_sbr_env_data_left, pstr_sbr_env_data_right,
1302
404k
                                        pstr_cmon_data, IXHEAACE_SBR_ID_CPE, NULL_PTR, 0,
1303
404k
                                        pstr_sbr_hdr->coupling, pstr_sbr_tab, sbr_codec, is_esbr,
1304
404k
                                        pstr_esbr_data, &num_sbr_data_bits);
1305
404k
    if (err_code) {
1306
521
      return err_code;
1307
521
    }
1308
403k
    payload_cnt_bits += num_sbr_data_bits;
1309
403k
  }
1310
1311
403k
  *ptr_num_bits = payload_cnt_bits;
1312
403k
  return err_code;
1313
404k
}
1314
1315
IA_ERRORCODE
1316
ixheaace_count_sbr_channel_pair_element(
1317
    ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr, ixheaace_pstr_sbr_bitstream_data pstr_sbr_bs,
1318
    ixheaace_pstr_sbr_env_data pstr_sbr_env_data_left,
1319
    ixheaace_pstr_sbr_env_data pstr_sbr_env_data_right, ixheaace_pstr_common_data pstr_cmon_data,
1320
    ixheaace_str_sbr_tabs *pstr_sbr_tab, ixheaace_sbr_codec_type sbr_codec, WORD32 is_esbr,
1321
    ixheaace_str_esbr_bs_data *pstr_esbr_data, WORD32 *ptr_num_bits)
1322
1323
233k
{
1324
233k
  IA_ERRORCODE err_code = IA_NO_ERROR;
1325
233k
  ixheaace_bit_buf bit_buf_tmp = pstr_cmon_data->str_sbr_bit_buf;
1326
1327
233k
  err_code = ixheaace_write_env_channel_pair_element(
1328
233k
      pstr_sbr_hdr, pstr_sbr_bs, pstr_sbr_env_data_left, pstr_sbr_env_data_right, pstr_cmon_data,
1329
233k
      pstr_sbr_tab, sbr_codec, is_esbr, pstr_esbr_data, ptr_num_bits);
1330
1331
233k
  pstr_cmon_data->str_sbr_bit_buf = bit_buf_tmp;
1332
1333
233k
  return err_code;
1334
233k
}
1335
1336
VOID ixheaace_map_low_res_energy_value(WORD32 curr_val, WORD32 *ptr_prev_data, WORD32 offset,
1337
15.2M
                                       WORD32 index, ixheaace_freq_res res) {
1338
15.2M
  if (res == FREQ_RES_LOW) {
1339
2.91M
    if (offset >= 0) {
1340
2.91M
      if (index < offset) {
1341
0
        ptr_prev_data[index] = curr_val;
1342
2.91M
      } else {
1343
2.91M
        ptr_prev_data[2 * index - offset] = curr_val;
1344
2.91M
        ptr_prev_data[2 * index + 1 - offset] = curr_val;
1345
2.91M
      }
1346
2.91M
    } else {
1347
0
      offset = -offset;
1348
1349
0
      if (index < offset) {
1350
0
        ptr_prev_data[3 * index] = curr_val;
1351
0
        ptr_prev_data[3 * index + 1] = curr_val;
1352
0
        ptr_prev_data[3 * index + 2] = curr_val;
1353
0
      } else {
1354
0
        ptr_prev_data[2 * index + offset] = curr_val;
1355
0
        ptr_prev_data[2 * index + 1 + offset] = curr_val;
1356
0
      }
1357
0
    }
1358
12.2M
  } else {
1359
12.2M
    ptr_prev_data[index] = curr_val;
1360
12.2M
  }
1361
15.2M
}
1362
1363
IA_ERRORCODE
1364
ixheaace_compute_bits(WORD32 delta, WORD32 code_book_scf_lav_lvl,
1365
                      WORD32 code_book_scf_lav_balance, const UWORD8 *ptr_huff_tbl_lvl,
1366
                      const UWORD8 *ptr_huff_tbl_bal, WORD32 coupling, WORD32 ch,
1367
23.7M
                      WORD32 *ptr_delta_bits) {
1368
23.7M
  WORD32 index;
1369
23.7M
  *ptr_delta_bits = 0;
1370
1371
23.7M
  if (coupling) {
1372
6.79M
    if (ch == 1) {
1373
3.38M
      index = (delta < 0) ? ixheaac_max32(delta, -code_book_scf_lav_balance)
1374
3.38M
                          : ixheaac_min32(delta, code_book_scf_lav_balance);
1375
1376
3.38M
      if (index != delta) {
1377
752
        return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_BS;
1378
752
      }
1379
1380
3.38M
      *ptr_delta_bits = ptr_huff_tbl_bal[index + code_book_scf_lav_balance];
1381
3.41M
    } else {
1382
3.41M
      index = (delta < 0) ? ixheaac_max32(delta, -code_book_scf_lav_lvl)
1383
3.41M
                          : ixheaac_min32(delta, code_book_scf_lav_lvl);
1384
1385
3.41M
      if (index != delta) {
1386
825
        return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_BS;
1387
825
      }
1388
1389
3.41M
      *ptr_delta_bits = ptr_huff_tbl_lvl[index + code_book_scf_lav_lvl];
1390
3.41M
    }
1391
16.9M
  } else {
1392
16.9M
    index = (delta < 0) ? ixheaac_max32(delta, -code_book_scf_lav_lvl)
1393
16.9M
                        : ixheaac_min32(delta, code_book_scf_lav_lvl);
1394
1395
16.9M
    *ptr_delta_bits = ptr_huff_tbl_lvl[index + code_book_scf_lav_lvl];
1396
16.9M
  }
1397
1398
23.7M
  return IA_NO_ERROR;
1399
23.7M
}