Coverage Report

Created: 2026-03-31 06:52

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
135k
static WORD32 ixheaace_get_esbr_ext_data_size(ixheaace_str_esbr_bs_data *pstr_esbr_bs_data) {
63
135k
  WORD32 num_bits = 1;
64
135k
  if (1 == pstr_esbr_bs_data->sbr_num_chan) {
65
63.1k
    num_bits += 1;
66
63.1k
    if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
67
45.2k
      num_bits += 2;
68
45.2k
      if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
69
39.1k
        num_bits += 7;
70
39.1k
      }
71
45.2k
    }
72
72.0k
  } else if (2 == pstr_esbr_bs_data->sbr_num_chan) {
73
46.6k
    if (pstr_esbr_bs_data->sbr_coupling) {
74
25.3k
      num_bits += 1;
75
25.3k
      if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
76
17.4k
        num_bits += 2;
77
17.4k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
78
16.7k
          num_bits += 7;
79
16.7k
        }
80
17.4k
      }
81
25.3k
    } else {
82
21.3k
      num_bits += 1;
83
21.3k
      if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
84
11.7k
        num_bits += 2;
85
11.7k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
86
10.3k
          num_bits += 7;
87
10.3k
        }
88
11.7k
      }
89
21.3k
      num_bits += 1;
90
21.3k
      if (pstr_esbr_bs_data->sbr_patching_mode[1] == 0) {
91
12.7k
        num_bits += 2;
92
12.7k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[1] == 1) {
93
10.3k
          num_bits += 7;
94
10.3k
        }
95
12.7k
      }
96
21.3k
    }
97
46.6k
  } else {
98
25.4k
    num_bits = 0;
99
25.4k
  }
100
135k
  if (num_bits != 0 && num_bits < 6) {
101
42.1k
    num_bits = 6;
102
42.1k
  }
103
135k
  return num_bits;
104
135k
}
105
static WORD32 iusace_encode_pvc_envelope(ixheaace_bit_buf_handle pstr_bs_handle,
106
                                         ixheaace_pvc_bs_info *pstr_pvc_bs_data,
107
22.7k
                                         WORD32 usac_indep_flag) {
108
22.7k
  WORD32 payload_cnt_bits = 0;
109
22.7k
  payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->div_mode,
110
22.7k
                                          IXHEAACE_ESBR_PVC_DIV_MODE_BITS);
111
22.7k
  payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->ns_mode,
112
22.7k
                                          IXHEAACE_ESBR_PVC_NS_MODE_BITS);
113
114
22.7k
  if (0 == pstr_pvc_bs_data->div_mode) {
115
13.0k
    if (1 == usac_indep_flag) {
116
746
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->pvc_id_bs[0],
117
746
                                              IXHEAACE_ESBR_PVC_ID_BITS);
118
12.2k
    } else {
119
12.2k
      if (1 == pstr_pvc_bs_data->grid_info[0]) {
120
2.26k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, IXHEAACE_ESBR_PVC_REUSE_BITS);
121
2.26k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->pvc_id_bs[0],
122
2.26k
                                                IXHEAACE_ESBR_PVC_ID_BITS);
123
10.0k
      } else {
124
10.0k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 1, IXHEAACE_ESBR_PVC_REUSE_BITS);
125
10.0k
      }
126
12.2k
    }
127
13.0k
  } else if (pstr_pvc_bs_data->div_mode <= 3) {
128
    /* Do nothing */
129
9.68k
  } else {
130
9.68k
    WORD32 gi, is_grid_info;
131
29.0k
    for (gi = 0; gi < pstr_pvc_bs_data->num_grid_info; gi++) {
132
19.3k
      if (gi == 0 && 1 == usac_indep_flag) {
133
300
        is_grid_info = 1;
134
19.0k
      } else {
135
19.0k
        is_grid_info = pstr_pvc_bs_data->grid_info[gi];
136
19.0k
        payload_cnt_bits +=
137
19.0k
            ixheaace_write_bits(pstr_bs_handle, is_grid_info, IXHEAACE_ESBR_PVC_GRID_INFO_BITS);
138
19.0k
      }
139
19.3k
      if (is_grid_info) {
140
16.2k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_pvc_bs_data->pvc_id_bs[gi],
141
16.2k
                                                IXHEAACE_ESBR_PVC_ID_BITS);
142
16.2k
      }
143
19.3k
    }
144
9.68k
  }
145
22.7k
  return payload_cnt_bits;
146
22.7k
}
147
655k
static WORD32 ia_enhaacplus_enc_ceil_ln2(WORD32 x) {
148
655k
  WORD32 tmp = -1;
149
150
1.04M
  while (ixheaac_shl32(1, ++tmp) < x)
151
391k
    ;
152
153
655k
  return (tmp);
154
655k
}
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
734k
                                       ixheaace_sbr_codec_type sbr_codec) {
159
734k
  WORD32 payload_cnt_bits = 0;
160
734k
  WORD32 i, tmp_var;
161
162
734k
  if (ELD_SBR != sbr_codec) {
163
567k
    if (HEAAC_SBR == sbr_codec ||
164
544k
        (USAC_SBR == sbr_codec && pstr_sbr_env_info->sbr_pvc_mode == 0)) {
165
544k
      payload_cnt_bits += ixheaace_write_bits(
166
544k
          pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type, SBR_CLA_BITS);
167
168
544k
      switch (pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type) {
169
381k
        case IXHEAACE_FIXFIX:
170
171
381k
          tmp_var = ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env);
172
173
381k
          payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ENV_BITS);
174
175
381k
          payload_cnt_bits +=
176
381k
              ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->freq_res_fix, SBR_RES_BITS);
177
381k
          break;
178
179
49.7k
        case IXHEAACE_FIXVAR:
180
96.6k
        case IXHEAACE_VARFIX:
181
182
96.6k
          if (pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type == IXHEAACE_FIXVAR) {
183
49.7k
            tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord - 16;
184
49.7k
          } else {
185
46.9k
            tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord;
186
46.9k
          }
187
188
96.6k
          payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ABS_BITS);
189
190
96.6k
          payload_cnt_bits += ixheaace_write_bits(
191
96.6k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->n, SBR_NUM_BITS);
192
193
251k
          for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->n; i++) {
194
154k
            tmp_var = (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_rel_bord[i] - 2) >> 1;
195
196
154k
            payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_REL_BITS);
197
154k
          }
198
199
96.6k
          tmp_var = ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->n + 2);
200
201
96.6k
          payload_cnt_bits += ixheaace_write_bits(
202
96.6k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->p, (UWORD8)tmp_var);
203
204
348k
          for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->n + 1; i++) {
205
251k
            payload_cnt_bits += ixheaace_write_bits(
206
251k
                pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f[i], SBR_RES_BITS);
207
251k
          }
208
96.6k
          break;
209
210
66.0k
        case IXHEAACE_VARVAR:
211
212
66.0k
          tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord_0;
213
214
66.0k
          payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ABS_BITS);
215
66.0k
          tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_abs_bord_1 - 16;
216
217
66.0k
          payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ABS_BITS);
218
219
66.0k
          payload_cnt_bits += ixheaace_write_bits(
220
66.0k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0, SBR_NUM_BITS);
221
222
66.0k
          payload_cnt_bits += ixheaace_write_bits(
223
66.0k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1, SBR_NUM_BITS);
224
225
102k
          for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0; i++) {
226
36.0k
            tmp_var = (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_rel_bord_0[i] - 2) >> 1;
227
228
36.0k
            payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_REL_BITS);
229
36.0k
          }
230
231
190k
          for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1; i++) {
232
124k
            tmp_var = (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_rel_bord_1[i] - 2) >> 1;
233
234
124k
            payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_REL_BITS);
235
124k
          }
236
237
66.0k
          tmp_var =
238
66.0k
              ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0 +
239
66.0k
                                         pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1 + 2);
240
241
66.0k
          payload_cnt_bits += ixheaace_write_bits(
242
66.0k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->p, (UWORD8)tmp_var);
243
244
66.0k
          tmp_var = pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_0 +
245
66.0k
                    pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_rel_1 + 1;
246
247
292k
          for (i = 0; i < tmp_var; i++) {
248
226k
            payload_cnt_bits += ixheaace_write_bits(
249
226k
                pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f_lr[i], SBR_RES_BITS);
250
226k
          }
251
66.0k
          break;
252
0
        default:
253
0
          break;
254
544k
      }
255
544k
    } else {
256
      // If PVC mode is non-zero, bit stream parameters are updated here
257
22.7k
      if (pstr_sbr_env_info->no_of_envelopes > 1) {
258
8.09k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 8, SBR_PVC_NOISE_POSITION_BITS);
259
14.6k
      } else {
260
14.6k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SBR_PVC_NOISE_POSITION_BITS);
261
14.6k
      }
262
22.7k
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SBR_PVC_VAR_LEN_HF_BITS);
263
22.7k
    }
264
567k
  } else {
265
167k
    payload_cnt_bits += ixheaace_write_bits(
266
167k
        pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type, LDSBR_CLA_BITS);
267
268
167k
    switch (pstr_sbr_env_info->pstr_sbr_bs_grid->frame_type) {
269
110k
      case IXHEAACE_FIXFIX:
270
110k
        tmp_var = ia_enhaacplus_enc_ceil_ln2(pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env);
271
110k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, tmp_var, SBR_ENV_BITS);
272
110k
        if (pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env == 1) {
273
106k
          payload_cnt_bits += ixheaace_write_bits(
274
106k
              pstr_bs_handle, pstr_sbr_env_info->curr_sbr_amp_res, SI_SBR_AMP_RES_BITS);
275
106k
        }
276
110k
        payload_cnt_bits += ixheaace_write_bits(
277
110k
            pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f[0], SBR_RES_BITS);
278
110k
        break;
279
280
56.3k
      case IXHEAACE_LD_TRAN:
281
56.3k
        payload_cnt_bits += ixheaace_write_bits(
282
56.3k
            pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->bs_transient_position,
283
56.3k
            IXHEAACE_SBR_TRAN_BITS);
284
220k
        for (i = 0; i < pstr_sbr_env_info->pstr_sbr_bs_grid->bs_num_env; i++) {
285
164k
          payload_cnt_bits += ixheaace_write_bits(
286
164k
              pstr_bs_handle, pstr_sbr_env_info->pstr_sbr_bs_grid->v_f[i], SBR_RES_BITS);
287
164k
        }
288
56.3k
        break;
289
0
      default:
290
0
        break;
291
167k
    }
292
167k
  }
293
294
734k
  return payload_cnt_bits;
295
734k
}
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
917k
                                       WORD32 sbr_pvc_mode) {
301
917k
  WORD32 i, payload_cnt_bits = 0, num_of_noise_env;
302
303
917k
  num_of_noise_env = (pstr_sbr_env_info->no_of_envelopes > 1) ? 2 : 1;
304
305
917k
  if (USAC_SBR != sbr_codec) {
306
1.48M
    for (i = 0; i < pstr_sbr_env_info->no_of_envelopes; ++i) {
307
964k
      payload_cnt_bits +=
308
964k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->domain_vec[i], SBR_DIR_BITS);
309
964k
    }
310
517k
  }
311
312
400k
  else {
313
400k
    if (sbr_pvc_mode == 0) {
314
377k
      WORD32 start_env = 0;
315
377k
      if (1 == usac_indep_flag) {
316
23.0k
        start_env = 1;
317
23.0k
      }
318
846k
      for (i = start_env; i < pstr_sbr_env_info->no_of_envelopes; ++i) {
319
468k
        payload_cnt_bits +=
320
468k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->domain_vec[i], SBR_DIR_BITS);
321
468k
      }
322
377k
    } else {
323
      /* Do nothing */
324
22.7k
    }
325
400k
  }
326
917k
  if (USAC_SBR != sbr_codec) {
327
1.26M
    for (i = 0; i < num_of_noise_env; ++i) {
328
752k
      payload_cnt_bits += ixheaace_write_bits(
329
752k
          pstr_bs_handle, pstr_sbr_env_info->domain_vec_noise[i], SBR_DIR_BITS);
330
752k
    }
331
517k
  } else {
332
400k
    WORD32 start_env = 0;
333
400k
    if (1 == usac_indep_flag) {
334
24.1k
      start_env = 1;
335
24.1k
    }
336
337
858k
    for (i = start_env; i < num_of_noise_env; ++i) {
338
457k
      payload_cnt_bits += ixheaace_write_bits(
339
457k
          pstr_bs_handle, pstr_sbr_env_info->domain_vec_noise[i], SBR_DIR_BITS);
340
457k
    }
341
400k
  }
342
917k
  return payload_cnt_bits;
343
917k
}
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
916k
                                            WORD32 coupling) {
348
916k
  WORD32 j, i, payload_cnt_bits = 0;
349
916k
  WORD32 n_noise_envelopes = ((pstr_sbr_env_info->no_of_envelopes > 1) ? 2 : 1);
350
351
2.14M
  for (i = 0; i < n_noise_envelopes; i++) {
352
1.23M
    switch (pstr_sbr_env_info->domain_vec_noise[i]) {
353
549k
      case FREQ:
354
355
549k
        if (coupling && pstr_sbr_env_info->balance) {
356
50.7k
          payload_cnt_bits += ixheaace_write_bits(
357
50.7k
              pstr_bs_handle,
358
50.7k
              pstr_sbr_env_info->noise_level[i * pstr_sbr_env_info->noise_band_count],
359
50.7k
              (UWORD8)pstr_sbr_env_info->si_sbr_start_noise_bits_balance);
360
499k
        } else {
361
499k
          payload_cnt_bits += ixheaace_write_bits(
362
499k
              pstr_bs_handle,
363
499k
              pstr_sbr_env_info->noise_level[i * pstr_sbr_env_info->noise_band_count],
364
499k
              (UWORD8)pstr_sbr_env_info->si_sbr_start_noise_bits);
365
499k
        }
366
367
549k
        for (j = 1 + i * pstr_sbr_env_info->noise_band_count;
368
1.47M
             j < (pstr_sbr_env_info->noise_band_count * (1 + i)); j++) {
369
921k
          if (coupling) {
370
283k
            if (pstr_sbr_env_info->balance) {
371
69.9k
              payload_cnt_bits += ixheaace_write_bits(
372
69.9k
                  pstr_bs_handle,
373
69.9k
                  pstr_sbr_env_info
374
69.9k
                      ->ptr_huff_tab_noise_bal_freq_c[pstr_sbr_env_info->noise_level[j] +
375
69.9k
                                                      CODE_BCK_SCF_LAV_BALANCE11],
376
69.9k
                  pstr_sbr_env_info
377
69.9k
                      ->ptr_huff_tab_noise_bal_freq_l[pstr_sbr_env_info->noise_level[j] +
378
69.9k
                                                      CODE_BCK_SCF_LAV_BALANCE11]);
379
213k
            } else {
380
213k
              payload_cnt_bits += ixheaace_write_bits(
381
213k
                  pstr_bs_handle,
382
213k
                  pstr_sbr_env_info
383
213k
                      ->ptr_huff_tab_noise_lvl_freq_c[pstr_sbr_env_info->noise_level[j] +
384
213k
                                                      CODE_BCK_SCF_LAV11],
385
213k
                  pstr_sbr_env_info
386
213k
                      ->ptr_huff_tab_noise_lvl_freq_l[pstr_sbr_env_info->noise_level[j] +
387
213k
                                                      CODE_BCK_SCF_LAV11]);
388
213k
            }
389
637k
          } else {
390
637k
            payload_cnt_bits += ixheaace_write_bits(
391
637k
                pstr_bs_handle,
392
637k
                pstr_sbr_env_info->ptr_huff_tab_noise_freq_c[pstr_sbr_env_info->noise_level[j] +
393
637k
                                                             CODE_BCK_SCF_LAV11],
394
637k
                pstr_sbr_env_info->ptr_huff_tab_noise_freq_l[pstr_sbr_env_info->noise_level[j] +
395
637k
                                                             CODE_BCK_SCF_LAV11]);
396
637k
          }
397
921k
        }
398
549k
        break;
399
400
683k
      case TIME:
401
683k
        for (j = i * pstr_sbr_env_info->noise_band_count;
402
2.45M
             j < (pstr_sbr_env_info->noise_band_count * (1 + i)); j++) {
403
1.77M
          if (coupling) {
404
861k
            if (pstr_sbr_env_info->balance) {
405
538k
              payload_cnt_bits += ixheaace_write_bits(
406
538k
                  pstr_bs_handle,
407
538k
                  pstr_sbr_env_info
408
538k
                      ->ptr_huff_tab_noise_bal_time_c[pstr_sbr_env_info->noise_level[j] +
409
538k
                                                      CODE_BCK_SCF_LAV_BALANCE11],
410
538k
                  pstr_sbr_env_info
411
538k
                      ->ptr_huff_tab_noise_bal_time_l[pstr_sbr_env_info->noise_level[j] +
412
538k
                                                      CODE_BCK_SCF_LAV_BALANCE11]);
413
538k
            } else {
414
322k
              payload_cnt_bits += ixheaace_write_bits(
415
322k
                  pstr_bs_handle,
416
322k
                  pstr_sbr_env_info
417
322k
                      ->ptr_huff_tab_noise_lvl_time_c[pstr_sbr_env_info->noise_level[j] +
418
322k
                                                      CODE_BCK_SCF_LAV11],
419
322k
                  pstr_sbr_env_info
420
322k
                      ->ptr_huff_tab_noise_lvl_time_l[pstr_sbr_env_info->noise_level[j] +
421
322k
                                                      CODE_BCK_SCF_LAV11]);
422
322k
            }
423
914k
          } else {
424
914k
            payload_cnt_bits += ixheaace_write_bits(
425
914k
                pstr_bs_handle,
426
914k
                pstr_sbr_env_info
427
914k
                    ->ptr_huff_tab_noise_lvl_time_c[pstr_sbr_env_info->noise_level[j] +
428
914k
                                                    CODE_BCK_SCF_LAV11],
429
914k
                pstr_sbr_env_info
430
914k
                    ->ptr_huff_tab_noise_lvl_time_l[pstr_sbr_env_info->noise_level[j] +
431
914k
                                                    CODE_BCK_SCF_LAV11]);
432
914k
          }
433
1.77M
        }
434
683k
        break;
435
1.23M
    }
436
1.23M
  }
437
916k
  return payload_cnt_bits;
438
916k
}
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
894k
                                            WORD32 *ptr_payload_cnt_bits) {
444
894k
  WORD32 j, i, delta;
445
446
894k
  *ptr_payload_cnt_bits = 0;
447
448
2.34M
  for (j = 0; j < pstr_sbr_env_info->no_of_envelopes; j++) {
449
1.45M
    if (pstr_sbr_env_info->domain_vec[j] == FREQ) {
450
1.10M
      if (coupling && pstr_sbr_env_info->balance) {
451
154k
        *ptr_payload_cnt_bits +=
452
154k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->ienvelope[j][0],
453
154k
                                (UWORD8)pstr_sbr_env_info->si_sbr_start_env_bits_balance);
454
952k
      } else {
455
952k
        *ptr_payload_cnt_bits +=
456
952k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->ienvelope[j][0],
457
952k
                                (UWORD8)pstr_sbr_env_info->si_sbr_start_env_bits);
458
952k
      }
459
1.10M
    }
460
461
14.6M
    for (i = 1 - pstr_sbr_env_info->domain_vec[j]; i < pstr_sbr_env_info->no_scf_bands[j]; i++) {
462
13.1M
      delta = pstr_sbr_env_info->ienvelope[j][i];
463
464
13.1M
      if (coupling && pstr_sbr_env_info->balance) {
465
2.52M
        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
10.6M
      } else {
469
10.6M
        if (abs(delta) > pstr_sbr_env_info->code_book_scf_lav) {
470
438
          return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_CODEBOOK;
471
438
        }
472
10.6M
      }
473
474
13.1M
      if (coupling) {
475
4.96M
        if (pstr_sbr_env_info->balance) {
476
2.52M
          if (pstr_sbr_env_info->domain_vec[j]) {
477
1.12M
            *ptr_payload_cnt_bits += ixheaace_write_bits(
478
1.12M
                pstr_bs_handle,
479
1.12M
                pstr_sbr_env_info
480
1.12M
                    ->ptr_huff_tab_bal_time_c[delta +
481
1.12M
                                              pstr_sbr_env_info->code_book_scf_lav_balance],
482
1.12M
                pstr_sbr_env_info
483
1.12M
                    ->ptr_huff_tab_bal_time_l[delta +
484
1.12M
                                              pstr_sbr_env_info->code_book_scf_lav_balance]);
485
1.40M
          } else {
486
1.40M
            *ptr_payload_cnt_bits += ixheaace_write_bits(
487
1.40M
                pstr_bs_handle,
488
1.40M
                pstr_sbr_env_info
489
1.40M
                    ->ptr_huff_tab_bal_freq_c[delta +
490
1.40M
                                              pstr_sbr_env_info->code_book_scf_lav_balance],
491
1.40M
                pstr_sbr_env_info
492
1.40M
                    ->ptr_huff_tab_bal_freq_l[delta +
493
1.40M
                                              pstr_sbr_env_info->code_book_scf_lav_balance]);
494
1.40M
          }
495
2.52M
        } else {
496
2.44M
          if (pstr_sbr_env_info->domain_vec[j]) {
497
593k
            *ptr_payload_cnt_bits += ixheaace_write_bits(
498
593k
                pstr_bs_handle,
499
593k
                pstr_sbr_env_info
500
593k
                    ->ptr_huff_tab_lvl_time_c[delta + pstr_sbr_env_info->code_book_scf_lav],
501
593k
                pstr_sbr_env_info
502
593k
                    ->ptr_huff_tab_lvl_time_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
503
1.85M
          } else {
504
1.85M
            *ptr_payload_cnt_bits += ixheaace_write_bits(
505
1.85M
                pstr_bs_handle,
506
1.85M
                pstr_sbr_env_info
507
1.85M
                    ->ptr_huff_tab_lvl_freq_c[delta + pstr_sbr_env_info->code_book_scf_lav],
508
1.85M
                pstr_sbr_env_info
509
1.85M
                    ->ptr_huff_tab_lvl_freq_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
510
1.85M
          }
511
2.44M
        }
512
8.21M
      } else {
513
8.21M
        if (pstr_sbr_env_info->domain_vec[j]) {
514
1.43M
          *ptr_payload_cnt_bits += ixheaace_write_bits(
515
1.43M
              pstr_bs_handle,
516
1.43M
              pstr_sbr_env_info
517
1.43M
                  ->ptr_huff_tab_time_c[delta + pstr_sbr_env_info->code_book_scf_lav],
518
1.43M
              pstr_sbr_env_info
519
1.43M
                  ->ptr_huff_tab_time_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
520
6.77M
        } else {
521
6.77M
          *ptr_payload_cnt_bits += ixheaace_write_bits(
522
6.77M
              pstr_bs_handle,
523
6.77M
              pstr_sbr_env_info
524
6.77M
                  ->ptr_huff_tab_freq_c[delta + pstr_sbr_env_info->code_book_scf_lav],
525
6.77M
              pstr_sbr_env_info
526
6.77M
                  ->ptr_huff_tab_freq_l[delta + pstr_sbr_env_info->code_book_scf_lav]);
527
6.77M
        }
528
8.21M
      }
529
13.1M
    }
530
1.45M
    if (USAC_SBR == sbr_codec) {
531
491k
      if (1 == pstr_sbr_env_info->sbr_inter_tes) {
532
206k
        *ptr_payload_cnt_bits +=
533
206k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->ptr_sbr_inter_tes_shape[j],
534
206k
                                IXHEAACE_SBR_TES_SHAPE_BITS);
535
206k
        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
206k
      }
541
491k
    }
542
1.45M
  }
543
544
894k
  return IA_NO_ERROR;
545
894k
}
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
916k
{
553
916k
  WORD32 i;
554
916k
  WORD32 payload_cnt_bits = 0;
555
556
916k
  payload_cnt_bits +=
557
916k
      ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->add_harmonic_flag, 1);
558
559
916k
  if (pstr_sbr_env_info->add_harmonic_flag) {
560
4.38M
    for (i = 0; i < pstr_sbr_env_info->no_harmonics; i++) {
561
4.04M
      payload_cnt_bits +=
562
4.04M
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->add_harmonic[i], 1);
563
4.04M
    }
564
339k
    if (USAC_SBR == sbr_codec && 0 != sbr_pvc_mode) {
565
11.9k
      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
11.9k
      } else {
569
11.9k
        payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, 1);
570
11.9k
      }
571
11.9k
    }
572
339k
  }
573
916k
  return payload_cnt_bits;
574
916k
}
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
230k
{
581
230k
  WORD32 payload_cnt_bits = 0;
582
230k
  IA_ERRORCODE err_code = IA_NO_ERROR;
583
584
230k
  if (sbr_codec != USAC_SBR) {
585
105k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, 1);
586
125k
  } else {
587
125k
    if (pstr_sbr_env_info->harmonic_sbr) {
588
      // USAC Harmonic SBR data
589
61.9k
      payload_cnt_bits +=
590
61.9k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_patching_mode, 1);
591
61.9k
      if (0 == pstr_sbr_env_info->sbr_patching_mode) {
592
53.6k
        payload_cnt_bits +=
593
53.6k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_oversampling_flag, 1);
594
53.6k
        payload_cnt_bits +=
595
53.6k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_pitchin_bins_flag, 1);
596
53.6k
        if (0 != pstr_sbr_env_info->sbr_pitchin_bins_flag) {
597
53.6k
          payload_cnt_bits +=
598
53.6k
              ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_info->sbr_pitchin_bins, 7);
599
53.6k
        }
600
53.6k
      }
601
61.9k
    }
602
125k
  }
603
230k
  payload_cnt_bits += ixheaace_encode_sbr_grid(pstr_sbr_env_info, pstr_bs_handle, sbr_codec);
604
230k
  if (sbr_codec == USAC_SBR) {
605
125k
    payload_cnt_bits += ixheaace_encode_sbr_dtdf(pstr_sbr_env_info, pstr_bs_handle, sbr_codec,
606
125k
                                                 pstr_sbr_env_info->usac_indep_flag,
607
125k
                                                 pstr_sbr_env_info->sbr_pvc_mode);
608
125k
  } else {
609
105k
    payload_cnt_bits +=
610
105k
        ixheaace_encode_sbr_dtdf(pstr_sbr_env_info, pstr_bs_handle, sbr_codec, 0, 0);
611
105k
  }
612
613
230k
  {
614
230k
    WORD32 i;
615
705k
    for (i = 0; i < pstr_sbr_env_info->noise_band_count; i++) {
616
474k
      payload_cnt_bits += ixheaace_write_bits(
617
474k
          pstr_bs_handle, pstr_sbr_env_info->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
618
474k
    }
619
230k
  }
620
230k
  if (sbr_codec != USAC_SBR) {
621
105k
    WORD32 env_data_len;
622
105k
    err_code =
623
105k
        ixheaace_write_env_data(pstr_sbr_env_info, pstr_bs_handle, 0, sbr_codec, &env_data_len);
624
105k
    if (err_code) {
625
56
      *ptr_num_bits = payload_cnt_bits;
626
56
      return err_code;
627
56
    }
628
105k
    payload_cnt_bits += env_data_len;
629
125k
  } else {
630
125k
    if (0 == pstr_sbr_env_info->sbr_pvc_mode) {
631
102k
      WORD32 env_data_len;
632
102k
      err_code =
633
102k
          ixheaace_write_env_data(pstr_sbr_env_info, pstr_bs_handle, 0, sbr_codec, &env_data_len);
634
102k
      if (err_code) {
635
0
        *ptr_num_bits = payload_cnt_bits;
636
0
        return err_code;
637
0
      }
638
102k
      payload_cnt_bits += env_data_len;
639
102k
    } else {
640
      // PVC envelope goes here
641
22.7k
      payload_cnt_bits += iusace_encode_pvc_envelope(pstr_bs_handle, &pstr_sbr_env_info->pvc_info,
642
22.7k
                                                     pstr_sbr_env_info->usac_indep_flag);
643
22.7k
    }
644
125k
  }
645
230k
  payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_info, pstr_bs_handle, 0);
646
647
230k
  if (USAC_SBR == sbr_codec) {
648
125k
    payload_cnt_bits += ixheaace_write_synthetic_coding_data(
649
125k
        pstr_sbr_env_info, pstr_bs_handle, sbr_codec, pstr_sbr_env_info->sbr_pvc_mode);
650
125k
  } else {
651
105k
    payload_cnt_bits +=
652
105k
        ixheaace_write_synthetic_coding_data(pstr_sbr_env_info, pstr_bs_handle, sbr_codec, 0);
653
105k
  }
654
655
230k
  *ptr_num_bits = payload_cnt_bits;
656
230k
  return err_code;
657
230k
}
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
343k
    WORD32 coupling, ixheaace_sbr_codec_type sbr_codec, WORD32 *ptr_num_bits) {
663
343k
  IA_ERRORCODE err_code = IA_NO_ERROR;
664
343k
  WORD32 payload_cnt_bits = 0;
665
343k
  WORD32 env_data_len;
666
343k
  WORD32 i = 0;
667
668
343k
  if (USAC_SBR != sbr_codec) {
669
205k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, 1); /* no reserved bits */
670
205k
  }
671
672
343k
  payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, coupling, SI_SBR_COUPLING_BITS);
673
674
343k
  if (coupling) {
675
182k
    if (sbr_codec == USAC_SBR && pstr_sbr_env_data_left->harmonic_sbr) {
676
      // USAC Harmonic SBR data
677
38.2k
      payload_cnt_bits +=
678
38.2k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_patching_mode, 1);
679
38.2k
      if (0 == pstr_sbr_env_data_left->sbr_patching_mode) {
680
24.2k
        payload_cnt_bits +=
681
24.2k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_oversampling_flag, 1);
682
24.2k
        payload_cnt_bits +=
683
24.2k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins_flag, 1);
684
24.2k
        if (0 != pstr_sbr_env_data_left->sbr_pitchin_bins_flag) {
685
23.9k
          payload_cnt_bits +=
686
23.9k
              ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins, 7);
687
23.9k
        }
688
24.2k
      }
689
38.2k
    }
690
182k
    payload_cnt_bits +=
691
182k
        ixheaace_encode_sbr_grid(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec);
692
693
182k
    payload_cnt_bits +=
694
182k
        ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec,
695
182k
                                 pstr_sbr_env_data_left->usac_indep_flag, 0);
696
697
182k
    payload_cnt_bits +=
698
182k
        ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_right, pstr_bs_handle, sbr_codec,
699
182k
                                 pstr_sbr_env_data_left->usac_indep_flag, 0);
700
701
679k
    for (i = 0; i < pstr_sbr_env_data_left->noise_band_count; i++) {
702
496k
      payload_cnt_bits += ixheaace_write_bits(
703
496k
          pstr_bs_handle, pstr_sbr_env_data_left->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
704
496k
    }
705
706
182k
    err_code = ixheaace_write_env_data(pstr_sbr_env_data_left, pstr_bs_handle, 1, sbr_codec,
707
182k
                                       &env_data_len);
708
182k
    if (err_code) {
709
0
      *ptr_num_bits = payload_cnt_bits;
710
0
      return err_code;
711
0
    }
712
713
182k
    payload_cnt_bits += env_data_len;
714
715
182k
    payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_left, pstr_bs_handle, 1);
716
717
182k
    err_code = ixheaace_write_env_data(pstr_sbr_env_data_right, pstr_bs_handle, 1, sbr_codec,
718
182k
                                       &env_data_len);
719
182k
    if (err_code) {
720
0
      *ptr_num_bits = payload_cnt_bits;
721
0
      return err_code;
722
0
    }
723
182k
    payload_cnt_bits += env_data_len;
724
725
182k
    payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_right, pstr_bs_handle, 1);
726
727
182k
    payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_left,
728
182k
                                                             pstr_bs_handle, sbr_codec, 0);
729
730
182k
    payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_right,
731
182k
                                                             pstr_bs_handle, sbr_codec, 0);
732
182k
  } else {
733
160k
    if (sbr_codec == USAC_SBR && pstr_sbr_env_data_left->harmonic_sbr) {
734
      // USAC Harmonic SBR data
735
33.1k
      payload_cnt_bits +=
736
33.1k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_patching_mode, 1);
737
33.1k
      if (0 == pstr_sbr_env_data_left->sbr_patching_mode) {
738
19.6k
        payload_cnt_bits +=
739
19.6k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_oversampling_flag, 1);
740
19.6k
        payload_cnt_bits +=
741
19.6k
            ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins_flag, 1);
742
19.6k
        if (0 != pstr_sbr_env_data_left->sbr_pitchin_bins_flag) {
743
19.3k
          payload_cnt_bits +=
744
19.3k
              ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_left->sbr_pitchin_bins, 7);
745
19.3k
        }
746
19.6k
      }
747
748
33.1k
      payload_cnt_bits +=
749
33.1k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_right->sbr_patching_mode, 1);
750
33.1k
      if (0 == pstr_sbr_env_data_right->sbr_patching_mode) {
751
19.6k
        payload_cnt_bits += ixheaace_write_bits(
752
19.6k
            pstr_bs_handle, pstr_sbr_env_data_right->sbr_oversampling_flag, 1);
753
19.6k
        payload_cnt_bits += ixheaace_write_bits(
754
19.6k
            pstr_bs_handle, pstr_sbr_env_data_right->sbr_pitchin_bins_flag, 1);
755
19.6k
        if (0 != pstr_sbr_env_data_right->sbr_pitchin_bins_flag) {
756
14.8k
          payload_cnt_bits +=
757
14.8k
              ixheaace_write_bits(pstr_bs_handle, pstr_sbr_env_data_right->sbr_pitchin_bins, 7);
758
14.8k
        }
759
19.6k
      }
760
33.1k
    }
761
160k
    payload_cnt_bits +=
762
160k
        ixheaace_encode_sbr_grid(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec);
763
764
160k
    payload_cnt_bits +=
765
160k
        ixheaace_encode_sbr_grid(pstr_sbr_env_data_right, pstr_bs_handle, sbr_codec);
766
767
160k
    payload_cnt_bits +=
768
160k
        ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_left, pstr_bs_handle, sbr_codec,
769
160k
                                 pstr_sbr_env_data_left->usac_indep_flag, 0);
770
771
160k
    payload_cnt_bits +=
772
160k
        ixheaace_encode_sbr_dtdf(pstr_sbr_env_data_right, pstr_bs_handle, sbr_codec,
773
160k
                                 pstr_sbr_env_data_left->usac_indep_flag, 0);
774
775
649k
    for (i = 0; i < pstr_sbr_env_data_left->noise_band_count; i++) {
776
489k
      payload_cnt_bits += ixheaace_write_bits(
777
489k
          pstr_bs_handle, pstr_sbr_env_data_left->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
778
489k
    }
779
780
649k
    for (i = 0; i < pstr_sbr_env_data_right->noise_band_count; i++) {
781
489k
      payload_cnt_bits += ixheaace_write_bits(
782
489k
          pstr_bs_handle, pstr_sbr_env_data_right->sbr_invf_mode_vec[i], SI_SBR_INVF_MODE_BITS);
783
489k
    }
784
785
160k
    err_code = ixheaace_write_env_data(pstr_sbr_env_data_left, pstr_bs_handle, 0, sbr_codec,
786
160k
                                       &env_data_len);
787
160k
    if (err_code) {
788
311
      *ptr_num_bits = payload_cnt_bits;
789
311
      return err_code;
790
311
    }
791
160k
    payload_cnt_bits += env_data_len;
792
793
160k
    err_code = ixheaace_write_env_data(pstr_sbr_env_data_right, pstr_bs_handle, 0, sbr_codec,
794
160k
                                       &env_data_len);
795
160k
    if (err_code) {
796
71
      *ptr_num_bits = payload_cnt_bits;
797
71
      return err_code;
798
71
    }
799
160k
    payload_cnt_bits += env_data_len;
800
801
160k
    payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_left, pstr_bs_handle, 0);
802
803
160k
    payload_cnt_bits += ixheaace_write_noise_lvl_data(pstr_sbr_env_data_right, pstr_bs_handle, 0);
804
805
160k
    payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_left,
806
160k
                                                             pstr_bs_handle, sbr_codec, 0);
807
808
160k
    payload_cnt_bits += ixheaace_write_synthetic_coding_data(pstr_sbr_env_data_right,
809
160k
                                                             pstr_bs_handle, sbr_codec, 0);
810
160k
  }
811
812
343k
  *ptr_num_bits = payload_cnt_bits;
813
343k
  return err_code;
814
343k
}
815
816
static WORD32 iexhaace_esbr_write_bs(ixheaace_str_esbr_bs_data *pstr_esbr_bs_data,
817
54.8k
                                     ixheaace_bit_buf_handle pstr_bs_handle) {
818
54.8k
  WORD32 num_bits = 0;
819
54.8k
  num_bits +=
820
54.8k
      ixheaace_write_bits(pstr_bs_handle, EXTENSION_ID_ESBR_CODING, SI_SBR_EXTENSION_ID_BITS);
821
54.8k
  num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_preprocessing, 1);
822
54.8k
  if (1 == pstr_esbr_bs_data->sbr_num_chan) {
823
31.5k
    num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[0], 1);
824
31.5k
    if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
825
22.6k
      num_bits +=
826
22.6k
          ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[0], 1);
827
22.6k
      num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[0], 1);
828
22.6k
      if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
829
19.5k
        num_bits +=
830
19.5k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
831
19.5k
      }
832
22.6k
    }
833
31.5k
  } else if (2 == pstr_esbr_bs_data->sbr_num_chan) {
834
23.3k
    if (pstr_esbr_bs_data->sbr_coupling) {
835
12.6k
      num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[0], 1);
836
12.6k
      if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
837
8.70k
        num_bits +=
838
8.70k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[0], 1);
839
8.70k
        num_bits +=
840
8.70k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[0], 1);
841
8.70k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
842
8.37k
          num_bits +=
843
8.37k
              ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
844
8.37k
        }
845
8.70k
      }
846
12.6k
    } else {
847
10.6k
      num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[0], 1);
848
10.6k
      if (pstr_esbr_bs_data->sbr_patching_mode[0] == 0) {
849
5.89k
        num_bits +=
850
5.89k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[0], 1);
851
5.89k
        num_bits +=
852
5.89k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[0], 1);
853
5.89k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[0] == 1) {
854
5.19k
          num_bits +=
855
5.19k
              ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
856
5.19k
        } else {
857
696
          pstr_esbr_bs_data->sbr_patching_mode[0] = pstr_esbr_bs_data->sbr_patching_mode[0];
858
696
        }
859
5.89k
      }
860
10.6k
      num_bits += ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_patching_mode[1], 1);
861
10.6k
      if (pstr_esbr_bs_data->sbr_patching_mode[1] == 0) {
862
6.35k
        num_bits +=
863
6.35k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_oversampling_flag[1], 1);
864
6.35k
        num_bits +=
865
6.35k
            ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_flags[1], 1);
866
6.35k
        if (pstr_esbr_bs_data->sbr_pitchin_flags[1] == 1) {
867
5.16k
          num_bits +=
868
5.16k
              ixheaace_write_bits(pstr_bs_handle, pstr_esbr_bs_data->sbr_pitchin_bins[0], 7);
869
5.16k
        }
870
6.35k
      }
871
10.6k
    }
872
23.3k
  }
873
54.8k
  if (num_bits < 8) {
874
21.0k
    num_bits += ixheaace_write_bits(pstr_bs_handle, 0, (UWORD8)(8 - num_bits));
875
21.0k
  }
876
877
54.8k
  return num_bits;
878
54.8k
}
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
310k
                                                  ixheaace_str_esbr_bs_data *pstr_esbr_data) {
885
310k
  WORD32 ext_data_bits = 0;
886
887
310k
  if (pstr_ps_handle) {
888
23.4k
    ext_data_bits +=
889
23.4k
        ixheaace_enc_write_ps_data(pstr_ps_handle, is_hdr_active, pstr_sbr_tab->ptr_ps_tab);
890
23.4k
  }
891
310k
  if (is_esbr) {
892
80.3k
    ext_data_bits += ixheaace_get_esbr_ext_data_size(pstr_esbr_data);
893
80.3k
  }
894
895
310k
  if (ext_data_bits != 0) {
896
66.5k
    ext_data_bits += SI_SBR_EXTENSION_ID_BITS;
897
66.5k
  }
898
899
310k
  return (ext_data_bits + 7) >> 3;
900
310k
}
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
310k
                                          ixheaace_str_esbr_bs_data *pstr_esbr_data) {
910
310k
  WORD32 ext_data_size;
911
310k
  WORD32 payload_bits_in = *ptr_payload_bits;
912
310k
  WORD32 payload_cnt_bits = 0;
913
914
310k
  ext_data_size = ixheaace_get_sbr_extended_data_size(pstr_ps_handle, is_hdr_active, pstr_sbr_tab,
915
310k
                                                      is_esbr, pstr_esbr_data);
916
917
310k
  if (ext_data_size != 0) {
918
66.5k
    if (pstr_ps_handle && ixheaace_append_ps_bitstream(pstr_ps_handle, NULL_PTR, 0)) {
919
23.4k
      ixheaace_bit_buf bitbuf_tmp;
920
23.4k
      UWORD8 tmp[IXHEAACE_MAX_PAYLOAD_SIZE];
921
23.4k
      WORD32 max_ext_size = (1 << SI_SBR_EXTENSION_SIZE_BITS) - 1;
922
23.4k
      WORD32 num_bits;
923
924
23.4k
      num_bits = ia_enhaacplus_enc_get_bits_available(&pstr_ps_handle->ps_bit_buf);
925
23.4k
      num_bits += SI_SBR_EXTENSION_ID_BITS;
926
23.4k
      if (is_esbr) {
927
11.7k
        num_bits += ixheaace_get_esbr_ext_data_size(pstr_esbr_data);
928
11.7k
        num_bits += SI_SBR_EXTENSION_ID_BITS;
929
11.7k
      }
930
23.4k
      ext_data_size = (num_bits + 7) >> 3;
931
23.4k
      if (ia_enhaacplus_enc_get_bits_available(pstr_bs_prev) == 0) {
932
266
        pstr_ps_handle->hdr_bits_prev_frame = *ptr_sbr_hdr_bits;
933
266
        ia_enhaacplus_enc_copy_bitbuf(pstr_bs_handle, pstr_bs_prev);
934
23.1k
      } else {
935
23.1k
        WORD32 tmp_bits;
936
23.1k
        ia_enhaacplus_enc_create_bitbuffer(&bitbuf_tmp, tmp, sizeof(tmp));
937
23.1k
        tmp_bits = *ptr_sbr_hdr_bits;
938
23.1k
        *ptr_sbr_hdr_bits = pstr_ps_handle->hdr_bits_prev_frame;
939
23.1k
        pstr_ps_handle->hdr_bits_prev_frame = tmp_bits;
940
23.1k
        ixheaace_copy_bitbuf_to_and_fro(pstr_bs_prev, pstr_bs_handle);
941
23.1k
      }
942
23.4k
      ixheaace_write_bits(pstr_bs_handle, 1, SI_SBR_EXTENDED_DATA_BITS);
943
944
23.4k
      if (ext_data_size < max_ext_size) {
945
22.6k
        ixheaace_write_bits(pstr_bs_handle, ext_data_size, SI_SBR_EXTENSION_SIZE_BITS);
946
22.6k
      } else {
947
818
        ixheaace_write_bits(pstr_bs_handle, max_ext_size, SI_SBR_EXTENSION_SIZE_BITS);
948
818
        ixheaace_write_bits(pstr_bs_handle, ext_data_size - max_ext_size,
949
818
                            SI_SBR_EXTENSION_ESC_COUNT_BITS);
950
818
      }
951
23.4k
      WORD32 start_bits = pstr_bs_handle->cnt_bits;
952
23.4k
      *ptr_payload_bits =
953
23.4k
          ixheaace_append_ps_bitstream(pstr_ps_handle, pstr_bs_handle, ptr_sbr_hdr_bits);
954
955
23.4k
      if (is_esbr) {
956
11.7k
        *ptr_payload_bits += iexhaace_esbr_write_bs(pstr_esbr_data, pstr_bs_handle);
957
11.7k
      }
958
959
23.4k
      WORD32 fill_bits = (ext_data_size << 3) - (pstr_bs_handle->cnt_bits - start_bits);
960
23.4k
      ixheaace_write_bits(pstr_bs_handle, 0, (UWORD8)fill_bits);
961
23.4k
      *ptr_payload_bits = *ptr_payload_bits + fill_bits;
962
43.1k
    } else {
963
43.1k
      if (is_esbr) {
964
43.1k
        WORD32 max_ext_size = (1 << SI_SBR_EXTENSION_SIZE_BITS) - 1;
965
43.1k
        WORD32 num_bits;
966
43.1k
        num_bits = ixheaace_get_esbr_ext_data_size(pstr_esbr_data);
967
43.1k
        ext_data_size = (num_bits + SI_SBR_EXTENSION_ID_BITS + 7) >> 3;
968
43.1k
        ixheaace_write_bits(pstr_bs_handle, 1, SI_SBR_EXTENDED_DATA_BITS);
969
43.1k
        if (ext_data_size < max_ext_size) {
970
43.1k
          ixheaace_write_bits(pstr_bs_handle, ext_data_size, SI_SBR_EXTENSION_SIZE_BITS);
971
43.1k
        } 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.1k
        WORD32 start_bits = pstr_bs_handle->cnt_bits;
977
43.1k
        *ptr_payload_bits += iexhaace_esbr_write_bs(pstr_esbr_data, pstr_bs_handle);
978
43.1k
        UWORD8 fill_bits =
979
43.1k
            (UWORD8)((ext_data_size << 3) - (pstr_bs_handle->cnt_bits - start_bits));
980
43.1k
        ixheaace_write_bits(pstr_bs_handle, 0, fill_bits);
981
43.1k
        *ptr_payload_bits = *ptr_payload_bits + fill_bits;
982
43.1k
      } 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.1k
    }
1000
244k
  } else {
1001
244k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SI_SBR_EXTENDED_DATA_BITS);
1002
1003
244k
    *ptr_payload_bits = payload_cnt_bits + payload_bits_in;
1004
244k
  }
1005
310k
}
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
574k
    WORD32 *ptr_num_bits) {
1014
574k
  WORD32 payload_cnt_bits = 0;
1015
574k
  IA_ERRORCODE err_code = IA_NO_ERROR;
1016
1017
574k
  switch (sbr_ele_type) {
1018
230k
    case IXHEAACE_SBR_ID_SCE:
1019
1020
230k
      err_code = ixheaace_encode_sbr_single_channel_element(
1021
230k
          pstr_sbr_env_data_left, &pstr_cmon_data->str_sbr_bit_buf, sbr_codec, &payload_cnt_bits);
1022
230k
      if (err_code) {
1023
56
        return err_code;
1024
56
      }
1025
230k
      if (USAC_SBR != sbr_codec) {
1026
105k
        ixheaace_encode_extended_data(
1027
105k
            pstr_ps_handle, is_hdr_active, &pstr_cmon_data->str_sbr_bit_buf_prev,
1028
105k
            &pstr_cmon_data->sbr_hdr_bits, &pstr_cmon_data->str_sbr_bit_buf, &payload_cnt_bits,
1029
105k
            pstr_sbr_tab, is_esbr, pstr_esbr_data);
1030
105k
      }
1031
230k
      break;
1032
343k
    case IXHEAACE_SBR_ID_CPE:
1033
1034
343k
      err_code = ixheaace_encode_sbr_channel_pair_element(
1035
343k
          pstr_sbr_env_data_left, pstr_sbr_env_data_right, &pstr_cmon_data->str_sbr_bit_buf,
1036
343k
          coupling, sbr_codec, &payload_cnt_bits);
1037
343k
      if (err_code) {
1038
382
        return err_code;
1039
382
      }
1040
343k
      if (USAC_SBR != sbr_codec) {
1041
205k
        ixheaace_encode_extended_data(NULL_PTR, 0, NULL_PTR, 0, &pstr_cmon_data->str_sbr_bit_buf,
1042
205k
                                      &payload_cnt_bits, pstr_sbr_tab, is_esbr, pstr_esbr_data);
1043
205k
      }
1044
343k
      break;
1045
574k
  }
1046
1047
573k
  pstr_cmon_data->sbr_data_bits = payload_cnt_bits;
1048
573k
  *ptr_num_bits = payload_cnt_bits;
1049
1050
573k
  pstr_cmon_data->prev_bit_buf_read_offset =
1051
573k
      (WORD32)(pstr_cmon_data->str_sbr_bit_buf_prev.ptr_read_next -
1052
573k
               pstr_cmon_data->str_sbr_bit_buf_prev.ptr_bit_buf_base);
1053
573k
  pstr_cmon_data->prev_bit_buf_write_offset =
1054
573k
      (WORD32)(pstr_cmon_data->str_sbr_bit_buf_prev.ptr_write_next -
1055
573k
               pstr_cmon_data->str_sbr_bit_buf_prev.ptr_bit_buf_base);
1056
1057
573k
  return err_code;
1058
574k
}
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
25.5k
{
1064
25.5k
  WORD32 payload_cnt_bits = 0;
1065
1066
25.5k
  if (pstr_sbr_hdr != NULL_PTR) {
1067
25.5k
    payload_cnt_bits +=
1068
25.5k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_amp_res, SI_SBR_AMP_RES_BITS);
1069
1070
25.5k
    payload_cnt_bits +=
1071
25.5k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_start_freq, SI_SBR_START_FREQ_BITS);
1072
1073
25.5k
    payload_cnt_bits +=
1074
25.5k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_stop_freq, SI_SBR_STOP_FREQ_BITS);
1075
1076
25.5k
    payload_cnt_bits +=
1077
25.5k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_xover_band, SI_SBR_XOVER_BAND_BITS);
1078
1079
25.5k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, 0, SI_SBR_RESERVED_BITS);
1080
1081
25.5k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_1,
1082
25.5k
                                            SI_SBR_HEADER_EXTRA_1_BITS);
1083
1084
25.5k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_2,
1085
25.5k
                                            SI_SBR_HEADER_EXTRA_2_BITS);
1086
1087
25.5k
    if (pstr_sbr_hdr->header_extra_1) {
1088
20.1k
      payload_cnt_bits +=
1089
20.1k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->freq_scale, SI_SBR_FREQ_SCALE_BITS);
1090
1091
20.1k
      payload_cnt_bits +=
1092
20.1k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->alter_scale, SI_SBR_ALTER_SCALE_BITS);
1093
1094
20.1k
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_noise_bands,
1095
20.1k
                                              SI_SBR_NOISE_BANDS_BITS);
1096
20.1k
    }
1097
1098
25.5k
    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
25.5k
  }
1111
1112
25.5k
  return payload_cnt_bits;
1113
25.5k
}
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
311k
                                                  ixheaace_pstr_common_data pstr_cmon_data) {
1118
311k
  WORD32 payload_cnt_bits = 0;
1119
1120
311k
  if (pstr_sbr_bs->crc_active) {
1121
0
    pstr_cmon_data->sbr_crc_len = 1;
1122
311k
  } else {
1123
311k
    pstr_cmon_data->sbr_crc_len = 0;
1124
311k
  }
1125
1126
311k
  if (pstr_sbr_bs->header_active) {
1127
25.5k
    payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 1, 1);
1128
1129
25.5k
    payload_cnt_bits +=
1130
25.5k
        ixheaace_encode_sbr_header_data(pstr_sbr_hdr, &pstr_cmon_data->str_sbr_bit_buf);
1131
285k
  } else {
1132
285k
    payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 0, 1);
1133
285k
  }
1134
1135
311k
  pstr_cmon_data->sbr_hdr_bits = payload_cnt_bits;
1136
1137
311k
  return payload_cnt_bits;
1138
311k
}
1139
static WORD32 iusace_encode_sbr_header_data(ixheaace_pstr_sbr_hdr_data pstr_sbr_hdr,
1140
53.9k
                                            ixheaace_bit_buf_handle pstr_bs_handle) {
1141
53.9k
  WORD32 payload_cnt_bits = 0;
1142
1143
53.9k
  if (pstr_sbr_hdr != NULL_PTR) {
1144
53.9k
    payload_cnt_bits +=
1145
53.9k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_start_freq, SI_SBR_START_FREQ_BITS);
1146
1147
53.9k
    payload_cnt_bits +=
1148
53.9k
        ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_stop_freq, SI_SBR_STOP_FREQ_BITS);
1149
1150
53.9k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_1,
1151
53.9k
                                            SI_SBR_HEADER_EXTRA_1_BITS);
1152
1153
53.9k
    payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->header_extra_2,
1154
53.9k
                                            SI_SBR_HEADER_EXTRA_2_BITS);
1155
1156
53.9k
    if (pstr_sbr_hdr->header_extra_1) {
1157
28.9k
      payload_cnt_bits +=
1158
28.9k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->freq_scale, SI_SBR_FREQ_SCALE_BITS);
1159
1160
28.9k
      payload_cnt_bits +=
1161
28.9k
          ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->alter_scale, SI_SBR_ALTER_SCALE_BITS);
1162
1163
28.9k
      payload_cnt_bits += ixheaace_write_bits(pstr_bs_handle, pstr_sbr_hdr->sbr_noise_bands,
1164
28.9k
                                              SI_SBR_NOISE_BANDS_BITS);
1165
28.9k
    }
1166
1167
53.9k
    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
53.9k
  }
1181
1182
53.9k
  return payload_cnt_bits;
1183
53.9k
}
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
262k
                                            ixheaace_pstr_common_data pstr_cmon_data) {
1188
262k
  WORD32 payload_cnt_bits = 0;
1189
262k
  WORD32 sbr_info_flag = 0;
1190
262k
  WORD32 sbr_hdr_flag = 0;
1191
262k
  if (pstr_sbr_bs->usac_indep_flag) {
1192
14.7k
    sbr_hdr_flag = 1;
1193
14.7k
    sbr_info_flag = 1;
1194
248k
  } else {
1195
248k
    if (pstr_sbr_bs->header_active) {
1196
39.2k
      sbr_info_flag = 1;
1197
39.2k
      payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 1, 1);
1198
39.2k
      sbr_hdr_flag = 1;
1199
39.2k
      payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 1, 1);
1200
208k
    } else {
1201
208k
      payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 0, 1);
1202
208k
    }
1203
248k
  }
1204
1205
262k
  if (1 == sbr_info_flag) {
1206
53.9k
    payload_cnt_bits +=
1207
53.9k
        ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_amp_res, 1);
1208
1209
53.9k
    payload_cnt_bits +=
1210
53.9k
        ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_xover_band, 4);
1211
1212
53.9k
    payload_cnt_bits +=
1213
53.9k
        ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_pre_proc, 1);
1214
53.9k
    if (pstr_sbr_hdr->sbr_pvc_active) {
1215
14.8k
      payload_cnt_bits +=
1216
14.8k
          ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, pstr_sbr_hdr->sbr_pvc_mode, 2);
1217
14.8k
    }
1218
53.9k
  }
1219
1220
262k
  if (1 == sbr_hdr_flag) {
1221
53.9k
    WORD32 sbr_def_hdr = 0;
1222
    // SBR default header
1223
53.9k
    payload_cnt_bits += ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, sbr_def_hdr, 1);
1224
53.9k
    if (0 == sbr_def_hdr) {
1225
53.9k
      payload_cnt_bits +=
1226
53.9k
          iusace_encode_sbr_header_data(pstr_sbr_hdr, &pstr_cmon_data->str_sbr_bit_buf);
1227
53.9k
    }
1228
53.9k
  }
1229
262k
  pstr_cmon_data->sbr_hdr_bits = payload_cnt_bits;
1230
1231
262k
  return payload_cnt_bits;
1232
262k
}
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
230k
    WORD32 *ptr_num_bits) {
1240
230k
  WORD32 payload_cnt_bits = 0;
1241
230k
  WORD32 num_sbr_data_bits = 0;
1242
230k
  IA_ERRORCODE err_code = IA_NO_ERROR;
1243
1244
230k
  pstr_cmon_data->sbr_hdr_bits = 0;
1245
230k
  pstr_cmon_data->sbr_data_bits = 0;
1246
230k
  pstr_cmon_data->sbr_crc_len = 0;
1247
1248
230k
  if (pstr_sbr_env_info != NULL_PTR) {
1249
230k
    if (USAC_SBR == sbr_codec) {
1250
125k
      payload_cnt_bits +=
1251
125k
          ia_usac_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1252
125k
    } else {
1253
      /* write header */
1254
105k
      payload_cnt_bits +=
1255
105k
          ia_enhaacplus_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1256
105k
    }
1257
    /* write data */
1258
230k
    err_code =
1259
230k
        ixheaace_encode_sbr_data(pstr_sbr_env_info, NULL_PTR, pstr_cmon_data, IXHEAACE_SBR_ID_SCE,
1260
230k
                                 pstr_ps_handle, pstr_sbr_bs->header_active, 0, pstr_sbr_tab,
1261
230k
                                 sbr_codec, is_esbr, pstr_esbr_data, &num_sbr_data_bits);
1262
230k
    if (err_code) {
1263
56
      return err_code;
1264
56
    }
1265
230k
    payload_cnt_bits += num_sbr_data_bits;
1266
230k
  }
1267
1268
230k
  *ptr_num_bits = payload_cnt_bits;
1269
230k
  return err_code;
1270
230k
}
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
343k
{
1281
343k
  WORD32 payload_cnt_bits = 0;
1282
343k
  WORD32 num_sbr_data_bits = 0;
1283
343k
  IA_ERRORCODE err_code = IA_NO_ERROR;
1284
1285
343k
  pstr_cmon_data->sbr_hdr_bits = 0;
1286
343k
  pstr_cmon_data->sbr_data_bits = 0;
1287
343k
  pstr_cmon_data->sbr_crc_len = 0;
1288
1289
  /* write pure SBR data */
1290
343k
  if ((pstr_sbr_env_data_left != NULL_PTR) && (pstr_sbr_env_data_right != NULL_PTR)) {
1291
343k
    if (USAC_SBR == sbr_codec) {
1292
137k
      payload_cnt_bits +=
1293
137k
          ia_usac_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1294
205k
    } else {
1295
      /* write header */
1296
205k
      payload_cnt_bits +=
1297
205k
          ia_enhaacplus_enc_encode_sbr_header(pstr_sbr_hdr, pstr_sbr_bs, pstr_cmon_data);
1298
205k
    }
1299
1300
    /* write data */
1301
343k
    err_code = ixheaace_encode_sbr_data(pstr_sbr_env_data_left, pstr_sbr_env_data_right,
1302
343k
                                        pstr_cmon_data, IXHEAACE_SBR_ID_CPE, NULL_PTR, 0,
1303
343k
                                        pstr_sbr_hdr->coupling, pstr_sbr_tab, sbr_codec, is_esbr,
1304
343k
                                        pstr_esbr_data, &num_sbr_data_bits);
1305
343k
    if (err_code) {
1306
382
      return err_code;
1307
382
    }
1308
343k
    payload_cnt_bits += num_sbr_data_bits;
1309
343k
  }
1310
1311
343k
  *ptr_num_bits = payload_cnt_bits;
1312
343k
  return err_code;
1313
343k
}
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
184k
{
1324
184k
  IA_ERRORCODE err_code = IA_NO_ERROR;
1325
184k
  ixheaace_bit_buf bit_buf_tmp = pstr_cmon_data->str_sbr_bit_buf;
1326
1327
184k
  err_code = ixheaace_write_env_channel_pair_element(
1328
184k
      pstr_sbr_hdr, pstr_sbr_bs, pstr_sbr_env_data_left, pstr_sbr_env_data_right, pstr_cmon_data,
1329
184k
      pstr_sbr_tab, sbr_codec, is_esbr, pstr_esbr_data, ptr_num_bits);
1330
1331
184k
  pstr_cmon_data->str_sbr_bit_buf = bit_buf_tmp;
1332
1333
184k
  return err_code;
1334
184k
}
1335
1336
VOID ixheaace_map_low_res_energy_value(WORD32 curr_val, WORD32 *ptr_prev_data, WORD32 offset,
1337
14.6M
                                       WORD32 index, ixheaace_freq_res res) {
1338
14.6M
  if (res == FREQ_RES_LOW) {
1339
2.72M
    if (offset >= 0) {
1340
2.72M
      if (index < offset) {
1341
0
        ptr_prev_data[index] = curr_val;
1342
2.72M
      } else {
1343
2.72M
        ptr_prev_data[2 * index - offset] = curr_val;
1344
2.72M
        ptr_prev_data[2 * index + 1 - offset] = curr_val;
1345
2.72M
      }
1346
2.72M
    } 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
11.8M
  } else {
1359
11.8M
    ptr_prev_data[index] = curr_val;
1360
11.8M
  }
1361
14.6M
}
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.2M
                      WORD32 *ptr_delta_bits) {
1368
23.2M
  WORD32 index;
1369
23.2M
  *ptr_delta_bits = 0;
1370
1371
23.2M
  if (coupling) {
1372
5.66M
    if (ch == 1) {
1373
2.82M
      index = (delta < 0) ? ixheaac_max32(delta, -code_book_scf_lav_balance)
1374
2.82M
                          : ixheaac_min32(delta, code_book_scf_lav_balance);
1375
1376
2.82M
      if (index != delta) {
1377
496
        return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_BS;
1378
496
      }
1379
1380
2.82M
      *ptr_delta_bits = ptr_huff_tbl_bal[index + code_book_scf_lav_balance];
1381
2.84M
    } else {
1382
2.84M
      index = (delta < 0) ? ixheaac_max32(delta, -code_book_scf_lav_lvl)
1383
2.84M
                          : ixheaac_min32(delta, code_book_scf_lav_lvl);
1384
1385
2.84M
      if (index != delta) {
1386
725
        return IA_EXHEAACE_EXE_FATAL_SBR_INVALID_BS;
1387
725
      }
1388
1389
2.84M
      *ptr_delta_bits = ptr_huff_tbl_lvl[index + code_book_scf_lav_lvl];
1390
2.84M
    }
1391
17.5M
  } else {
1392
17.5M
    index = (delta < 0) ? ixheaac_max32(delta, -code_book_scf_lav_lvl)
1393
17.5M
                        : ixheaac_min32(delta, code_book_scf_lav_lvl);
1394
1395
17.5M
    *ptr_delta_bits = ptr_huff_tbl_lvl[index + code_book_scf_lav_lvl];
1396
17.5M
  }
1397
1398
23.1M
  return IA_NO_ERROR;
1399
23.2M
}