Coverage Report

Created: 2025-08-26 06:53

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