Coverage Report

Created: 2025-11-24 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/ixheaace_dynamic_bits.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 <limits.h>
22
#include <stddef.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include "ixheaac_type_def.h"
26
#include "ixheaac_constants.h"
27
#include "impd_drc_common_enc.h"
28
#include "impd_drc_uni_drc.h"
29
#include "impd_drc_tables.h"
30
#include "impd_drc_api.h"
31
#include "ixheaace_api.h"
32
#include "ixheaace_aac_constants.h"
33
#include "ixheaac_error_standards.h"
34
#include "ixheaace_error_codes.h"
35
36
#include "ixheaac_basic_ops32.h"
37
#include "ixheaac_basic_ops16.h"
38
#include "ixheaac_basic_ops40.h"
39
#include "ixheaac_basic_ops.h"
40
41
#include "ixheaace_psy_const.h"
42
#include "ixheaace_tns.h"
43
#include "ixheaace_tns_params.h"
44
#include "ixheaace_rom.h"
45
#include "ixheaace_common_rom.h"
46
#include "ixheaace_bitbuffer.h"
47
#include "ixheaace_bits_count.h"
48
49
#include "ixheaace_dynamic_bits.h"
50
#include "ixheaace_common_utils.h"
51
52
404k
static WORD32 ia_enhaacplus_enc_calc_side_info_bits(WORD32 sfb_cnt, WORD32 block_type) {
53
404k
  WORD32 seg_len_bits = (block_type == SHORT_WINDOW ? 3 : 5);
54
404k
  WORD32 escape_val = (block_type == SHORT_WINDOW ? 7 : 31);
55
404k
  WORD32 side_info_bits, tmp;
56
57
404k
  side_info_bits = CODE_BCK_BITS;
58
404k
  tmp = sfb_cnt;
59
60
998k
  while (tmp >= 0) {
61
594k
    side_info_bits += seg_len_bits;
62
594k
    tmp -= escape_val;
63
594k
  }
64
65
404k
  return (side_info_bits);
66
404k
}
67
68
static VOID ia_enhaacplus_enc_build_bit_look_up(
69
    const WORD16 *ptr_quant_spec, const WORD32 max_sfb, const WORD32 *ptr_sfb_offset,
70
    const UWORD16 *sfb_max,
71
    WORD32 bit_look_up[MAXIMUM_SCALE_FACTOR_BAND_LONG][CODE_BCK_ESC_NDX + 1],
72
    ixheaace_section_info *pstr_section_info, ixheaace_huffman_tables *pstr_huffman_tbl,
73
695k
    WORD32 aot) {
74
695k
  WORD8 i;
75
76
14.2M
  for (i = 0; i < max_sfb; i++) {
77
13.5M
    WORD32 sfb_width, max_val;
78
79
13.5M
    pstr_section_info[i].sfb_cnt = 1;
80
13.5M
    pstr_section_info[i].sfb_start = i;
81
82
13.5M
    switch (aot) {
83
3.39M
      case AOT_AAC_LC:
84
8.61M
      case AOT_SBR:
85
9.80M
      case AOT_PS:
86
9.80M
        pstr_section_info[i].section_bits = INVALID_BITCOUNT_LC;
87
9.80M
        break;
88
89
512k
      case AOT_AAC_LD:
90
3.79M
      case AOT_AAC_ELD:
91
3.79M
        pstr_section_info[i].section_bits = INVALID_BITCOUNT_LD;
92
3.79M
        break;
93
13.5M
    }
94
95
13.5M
    pstr_section_info[i].code_book = -1;
96
97
13.5M
    sfb_width = ptr_sfb_offset[i + 1] - ptr_sfb_offset[i];
98
99
13.5M
    max_val = sfb_max[i];
100
101
13.5M
    ia_enhaacplus_enc_bitcount(ptr_quant_spec + ptr_sfb_offset[i], sfb_width, max_val,
102
13.5M
                               bit_look_up[i], pstr_huffman_tbl, aot);
103
13.5M
  }
104
695k
}
105
106
static WORD32 ia_enhaacplus_enc_find_best_book(const WORD32 *ptr_bit_cnt, WORD8 *ptr_codebook,
107
15.4M
                                               WORD32 aot) {
108
15.4M
  WORD32 min_bits = 0, temp1, temp2;
109
15.4M
  WORD8 temp_book = CODE_BCK_ESC_NDX;
110
15.4M
  WORD8 j;
111
112
15.4M
  switch (aot) {
113
3.82M
    case AOT_AAC_LC:
114
9.72M
    case AOT_SBR:
115
11.2M
    case AOT_PS:
116
11.2M
      min_bits = INVALID_BITCOUNT_LC;
117
11.2M
      break;
118
119
567k
    case AOT_AAC_LD:
120
4.21M
    case AOT_AAC_ELD:
121
4.21M
      min_bits = INVALID_BITCOUNT_LD;
122
4.21M
      break;
123
15.4M
  }
124
125
108M
  for (j = 0; j <= CODE_BCK_ESC_NDX; j += 2) {
126
92.7M
    temp1 = *ptr_bit_cnt++;
127
92.7M
    temp2 = *ptr_bit_cnt++;
128
92.7M
    if (temp1 < min_bits) {
129
13.2M
      min_bits = temp1;
130
13.2M
      temp_book = j;
131
13.2M
    }
132
92.7M
    if (temp2 < min_bits) {
133
7.96M
      min_bits = temp2;
134
7.96M
      temp_book = j + 1;
135
7.96M
    }
136
92.7M
  }
137
138
15.4M
  *ptr_codebook = temp_book;
139
15.4M
  return min_bits;
140
15.4M
}
141
142
static WORD32 ia_enhaacplus_enc_find_min_merge_bits(const WORD32 *ptr_bit_cnt1,
143
6.83M
                                                    const WORD32 *ptr_bit_cnt2, WORD32 aot) {
144
6.83M
  WORD32 min_bits = 0, j, temp1, temp2, temp3, temp4;
145
146
6.83M
  switch (aot) {
147
1.63M
    case AOT_AAC_LC:
148
3.81M
    case AOT_SBR:
149
4.96M
    case AOT_PS:
150
4.96M
      min_bits = INVALID_BITCOUNT_LC;
151
4.96M
      break;
152
153
268k
    case AOT_AAC_LD:
154
1.86M
    case AOT_AAC_ELD:
155
1.86M
      min_bits = INVALID_BITCOUNT_LD;
156
1.86M
      break;
157
6.83M
  }
158
159
47.8M
  for (j = CODE_BCK_ESC_NDX; j >= 0; j -= 2) {
160
41.0M
    temp1 = *ptr_bit_cnt1++;
161
41.0M
    temp2 = *ptr_bit_cnt2++;
162
41.0M
    temp3 = *ptr_bit_cnt1++;
163
164
41.0M
    temp1 = temp1 + temp2;
165
166
41.0M
    min_bits = MIN(temp1, min_bits);
167
41.0M
    temp4 = *ptr_bit_cnt2++;
168
41.0M
    temp1 = temp3 + temp4;
169
170
41.0M
    min_bits = MIN(temp1, min_bits);
171
41.0M
  }
172
173
6.83M
  return min_bits;
174
6.83M
}
175
176
static VOID ia_enhaacplus_enc_merge_bit_look_up(WORD32 *ptr_bit_cnt1, const WORD32 *ptr_bit_cnt2,
177
11.7M
                                                WORD32 aot) {
178
11.7M
  WORD32 j, temp1, temp2, temp3, temp4;
179
11.7M
  WORD32 invalid_bitcnt = 0;
180
181
11.7M
  switch (aot) {
182
2.96M
    case AOT_AAC_LC:
183
7.49M
    case AOT_SBR:
184
8.36M
    case AOT_PS:
185
8.36M
      invalid_bitcnt = INVALID_BITCOUNT_LC;
186
8.36M
      break;
187
188
456k
    case AOT_AAC_LD:
189
3.36M
    case AOT_AAC_ELD:
190
3.36M
      invalid_bitcnt = INVALID_BITCOUNT_LD;
191
3.36M
      break;
192
11.7M
  }
193
82.1M
  for (j = 0; j <= CODE_BCK_ESC_NDX; j += 2) {
194
70.3M
    temp1 = ptr_bit_cnt1[j];
195
70.3M
    temp2 = ptr_bit_cnt2[j];
196
70.3M
    temp3 = ptr_bit_cnt1[j + 1];
197
198
70.3M
    ptr_bit_cnt1[j] = MIN(temp1 + temp2, invalid_bitcnt);
199
70.3M
    temp4 = ptr_bit_cnt2[j + 1];
200
70.3M
    ptr_bit_cnt1[j + 1] = MIN(temp3 + temp4, invalid_bitcnt);
201
70.3M
  }
202
11.7M
}
203
204
static WORD32 ia_enhaacplus_enc_find_max_merge(
205
    const WORD32 merge_gain_look_up[MAXIMUM_SCALE_FACTOR_BAND_LONG],
206
2.76M
    const ixheaace_section_info *section, const WORD32 max_sfb, WORD32 *max_ndx) {
207
2.76M
  WORD32 i, max_merge_gain = 0;
208
209
19.9M
  for (i = 0; i + section[i].sfb_cnt < max_sfb; i += section[i].sfb_cnt) {
210
17.2M
    if (merge_gain_look_up[i] > max_merge_gain) {
211
3.73M
      max_merge_gain = merge_gain_look_up[i];
212
3.73M
      *max_ndx = i;
213
3.73M
    }
214
17.2M
  }
215
216
2.76M
  return (max_merge_gain);
217
2.76M
}
218
219
static WORD32 ia_enhaacplus_enc_calc_merge_gain(
220
    const ixheaace_section_info *pstr_section_info,
221
    WORD32 bit_look_up[MAXIMUM_SCALE_FACTOR_BAND_LONG][CODE_BCK_ESC_NDX + 1],
222
6.83M
    const WORD32 *pstr_side_info_tab, const WORD32 idx1, const WORD32 idx2, WORD32 aot) {
223
6.83M
  WORD32 split_bits;
224
6.83M
  WORD32 merge_bits;
225
6.83M
  WORD32 merge_gain;
226
227
6.83M
  split_bits = pstr_section_info[idx1].section_bits + pstr_section_info[idx2].section_bits;
228
229
6.83M
  merge_bits =
230
6.83M
      pstr_side_info_tab[pstr_section_info[idx1].sfb_cnt + pstr_section_info[idx2].sfb_cnt] +
231
6.83M
      ia_enhaacplus_enc_find_min_merge_bits(bit_look_up[idx1], bit_look_up[idx2], aot);
232
233
6.83M
  merge_gain = split_bits - merge_bits;
234
235
6.83M
  return merge_gain;
236
6.83M
}
237
238
static VOID ia_enhaacplus_enc_gm_stage0(
239
    ixheaace_section_info *pstr_section,
240
    WORD32 bit_look_up[MAXIMUM_SCALE_FACTOR_BAND_LONG][CODE_BCK_ESC_NDX + 1],
241
695k
    const WORD32 max_sfb, WORD32 aot) {
242
695k
  WORD32 i = 0;
243
695k
  WORD32 invalid_bitcnt = 0;
244
245
695k
  switch (aot) {
246
188k
    case AOT_AAC_LC:
247
459k
    case AOT_SBR:
248
534k
    case AOT_PS:
249
534k
      invalid_bitcnt = INVALID_BITCOUNT_LC;
250
534k
      break;
251
252
24.6k
    case AOT_AAC_LD:
253
161k
    case AOT_AAC_ELD:
254
255
161k
      invalid_bitcnt = INVALID_BITCOUNT_LD;
256
161k
      break;
257
695k
  }
258
14.2M
  while (i < max_sfb) {
259
13.5M
    if (pstr_section[i].section_bits == invalid_bitcnt) {
260
13.5M
      pstr_section[i].section_bits = (WORD16)ia_enhaacplus_enc_find_best_book(
261
13.5M
          bit_look_up[i], &(pstr_section[i].code_book), aot);
262
13.5M
    }
263
13.5M
    i++;
264
13.5M
  }
265
695k
}
266
267
static VOID ia_enhaacplus_enc_gm_stage1(
268
    ixheaace_section_info *pstr_section_info,
269
    WORD32 bit_look_up[MAXIMUM_SCALE_FACTOR_BAND_LONG][CODE_BCK_ESC_NDX + 1],
270
695k
    const WORD32 max_sfb, const WORD32 *ptr_side_info_tab, WORD32 aot) {
271
695k
  WORD32 merge_start = 0, merge_end;
272
273
3.92M
  do {
274
13.5M
    for (merge_end = merge_start + 1; merge_end < max_sfb; merge_end++) {
275
12.8M
      if (pstr_section_info[merge_start].code_book != pstr_section_info[merge_end].code_book) {
276
3.23M
        break;
277
3.23M
      }
278
279
9.66M
      pstr_section_info[merge_start].sfb_cnt++;
280
281
9.66M
      pstr_section_info[merge_start].section_bits += pstr_section_info[merge_end].section_bits;
282
283
9.66M
      ia_enhaacplus_enc_merge_bit_look_up(bit_look_up[merge_start], bit_look_up[merge_end], aot);
284
9.66M
    }
285
286
3.92M
    pstr_section_info[merge_start].section_bits +=
287
3.92M
        (WORD16)ptr_side_info_tab[pstr_section_info[merge_start].sfb_cnt];
288
289
3.92M
    pstr_section_info[merge_end - 1].sfb_start = pstr_section_info[merge_start].sfb_start;
290
291
3.92M
    merge_start = merge_end;
292
293
3.92M
  } while (merge_start < max_sfb);
294
695k
}
295
296
static VOID ia_enhaacplus_enc_gm_stage2(
297
    ixheaace_section_info *pstr_section_info,
298
    WORD32 merge_gain_look_up[MAXIMUM_SCALE_FACTOR_BAND_LONG],
299
    WORD32 bit_look_up[MAXIMUM_SCALE_FACTOR_BAND_LONG][CODE_BCK_ESC_NDX + 1],
300
695k
    const WORD32 max_sfb, const WORD32 *ptr_side_info_tab, WORD32 aot) {
301
695k
  WORD32 i;
302
303
3.92M
  for (i = 0; i + pstr_section_info[i].sfb_cnt < max_sfb; i += pstr_section_info[i].sfb_cnt) {
304
3.23M
    merge_gain_look_up[i] =
305
3.23M
        ia_enhaacplus_enc_calc_merge_gain(pstr_section_info, bit_look_up, ptr_side_info_tab, i,
306
3.23M
                                          i + pstr_section_info[i].sfb_cnt, aot);
307
3.23M
  }
308
309
2.76M
  while (TRUE) {
310
2.76M
    WORD32 max_merge_gain = 0, max_idx = 0, max_idx_next = 0, max_idx_last = 0;
311
312
2.76M
    max_merge_gain = ia_enhaacplus_enc_find_max_merge(merge_gain_look_up, pstr_section_info,
313
2.76M
                                                      max_sfb, &max_idx);
314
315
2.76M
    if (max_merge_gain <= 0) {
316
695k
      break;
317
695k
    }
318
319
2.06M
    max_idx_next = max_idx + pstr_section_info[max_idx].sfb_cnt;
320
321
2.06M
    pstr_section_info[max_idx].sfb_cnt += pstr_section_info[max_idx_next].sfb_cnt;
322
323
2.06M
    pstr_section_info[max_idx].section_bits +=
324
2.06M
        pstr_section_info[max_idx_next].section_bits - (WORD16)max_merge_gain;
325
326
2.06M
    ia_enhaacplus_enc_merge_bit_look_up(bit_look_up[max_idx], bit_look_up[max_idx_next], aot);
327
328
2.06M
    if (max_idx != 0) {
329
1.72M
      max_idx_last = pstr_section_info[max_idx - 1].sfb_start;
330
331
1.72M
      merge_gain_look_up[max_idx_last] = ia_enhaacplus_enc_calc_merge_gain(
332
1.72M
          pstr_section_info, bit_look_up, ptr_side_info_tab, max_idx_last, max_idx, aot);
333
1.72M
    }
334
335
2.06M
    max_idx_next = max_idx + pstr_section_info[max_idx].sfb_cnt;
336
337
2.06M
    pstr_section_info[max_idx_next - 1].sfb_start = pstr_section_info[max_idx].sfb_start;
338
339
2.06M
    if (max_idx_next < max_sfb) {
340
1.87M
      merge_gain_look_up[max_idx] = ia_enhaacplus_enc_calc_merge_gain(
341
1.87M
          pstr_section_info, bit_look_up, ptr_side_info_tab, max_idx, max_idx_next, aot);
342
1.87M
    }
343
2.06M
  }
344
695k
}
345
346
static IA_ERRORCODE ia_enhaacplus_enc_noiseless_counter(
347
    ixheaace_section_data *pstr_section_data,
348
    WORD32 merge_gain_look_up[MAXIMUM_SCALE_FACTOR_BAND_LONG],
349
    WORD32 bit_look_up[MAXIMUM_SCALE_FACTOR_BAND_LONG][CODE_BCK_ESC_NDX + 1],
350
    const WORD16 *ptr_quant_spec, const UWORD16 *ptr_max_val_in_sfb, const WORD32 *ptr_sfb_offset,
351
    const WORD32 block_type, WORD32 *ptr_side_info_tab_long, WORD32 *ptr_side_info_tab_short,
352
473k
    ixheaace_huffman_tables *pstr_huffman_tbl, WORD32 aot) {
353
473k
  WORD32 grp_idx;
354
473k
  WORD32 *ptr_side_info_tab = 0;
355
473k
  ixheaace_section_info *pstr_section_info;
356
473k
  WORD32 i;
357
358
24.6M
  for (i = 0; i < MAXIMUM_SCALE_FACTOR_BAND_LONG; i++) {
359
24.1M
    memset(bit_look_up[i], 0, (CODE_BCK_ESC_NDX + 1) * sizeof(bit_look_up[0][0]));
360
24.1M
  }
361
362
  /* counting previous operations */
363
473k
  switch (block_type) {
364
324k
    case LONG_WINDOW:
365
353k
    case START_WINDOW:
366
379k
    case STOP_WINDOW:
367
368
379k
      ptr_side_info_tab = ptr_side_info_tab_long;
369
379k
      break;
370
93.8k
    case SHORT_WINDOW:
371
372
93.8k
      ptr_side_info_tab = ptr_side_info_tab_short;
373
93.8k
      break;
374
0
    default:
375
0
      return IA_EXHEAACE_EXE_FATAL_INVALID_BLOCK_TYPE;
376
473k
  }
377
378
473k
  pstr_section_data->num_of_sections = 0;
379
473k
  pstr_section_data->huffman_bits = 0;
380
473k
  pstr_section_data->side_info_bits = 0;
381
382
473k
  if (pstr_section_data->max_sfb_per_grp == 0) {
383
45.5k
    return IA_NO_ERROR;
384
45.5k
  }
385
386
1.12M
  for (grp_idx = 0; grp_idx < pstr_section_data->sfb_cnt;
387
695k
       grp_idx += pstr_section_data->sfb_per_group) {
388
695k
    pstr_section_info = pstr_section_data->section + pstr_section_data->num_of_sections;
389
390
695k
    ia_enhaacplus_enc_build_bit_look_up(ptr_quant_spec, pstr_section_data->max_sfb_per_grp,
391
695k
                                        ptr_sfb_offset + grp_idx, ptr_max_val_in_sfb + grp_idx,
392
695k
                                        bit_look_up, pstr_section_info, pstr_huffman_tbl, aot);
393
394
695k
    ia_enhaacplus_enc_gm_stage0(pstr_section_info, bit_look_up,
395
695k
                                pstr_section_data->max_sfb_per_grp, aot);
396
397
695k
    ia_enhaacplus_enc_gm_stage1(pstr_section_info, bit_look_up,
398
695k
                                pstr_section_data->max_sfb_per_grp, ptr_side_info_tab, aot);
399
400
695k
    ia_enhaacplus_enc_gm_stage2(pstr_section_info, merge_gain_look_up, bit_look_up,
401
695k
                                pstr_section_data->max_sfb_per_grp, ptr_side_info_tab, aot);
402
403
2.55M
    for (i = 0; i < pstr_section_data->max_sfb_per_grp; i += pstr_section_info[i].sfb_cnt) {
404
1.86M
      ia_enhaacplus_enc_find_best_book(bit_look_up[i], &(pstr_section_info[i].code_book), aot);
405
406
1.86M
      pstr_section_info[i].sfb_start += (WORD8)grp_idx;
407
408
1.86M
      pstr_section_data->huffman_bits +=
409
1.86M
          pstr_section_info[i].section_bits - ptr_side_info_tab[pstr_section_info[i].sfb_cnt];
410
411
1.86M
      pstr_section_data->side_info_bits += ptr_side_info_tab[pstr_section_info[i].sfb_cnt];
412
413
1.86M
      pstr_section_data->section[pstr_section_data->num_of_sections++] = pstr_section_info[i];
414
1.86M
    }
415
695k
  }
416
427k
  return IA_NO_ERROR;
417
473k
}
418
419
static WORD32 ia_enhaacplus_enc_bit_count_scalefactor_delta(
420
6.92M
    WORD32 delta, ixheaace_huffman_tables *pstr_huffman_tbl) {
421
6.92M
  return pstr_huffman_tbl->huff_ltabscf[delta + CODE_BCK_SCF_LAV];
422
6.92M
}
423
424
static IA_ERRORCODE ia_enhaacplus_enc_scf_count(const WORD16 *ptr_scale_fac,
425
                                                const UWORD16 *ptr_max_val_in_sfb,
426
                                                ixheaace_section_data *pstr_section_data,
427
473k
                                                ixheaace_huffman_tables *pstr_huffman_tbl) {
428
473k
  WORD32 sect_idx1 = 0;
429
473k
  WORD32 sfb_idx1 = 0;
430
473k
  WORD32 scf_idx = 0;
431
473k
  WORD32 sect_idx2 = 0;
432
473k
  WORD32 sfb_idx2 = 0;
433
434
473k
  WORD32 last_val_scf = 0;
435
473k
  WORD32 delta_scf = 0;
436
473k
  WORD32 found = 0;
437
473k
  WORD32 scf_skip_counter = 0;
438
439
473k
  pstr_section_data->scale_fac_bits = 0;
440
441
473k
  if (ptr_scale_fac == 0) {
442
0
    return IA_EXHEAACE_EXE_FATAL_INVALID_SCALE_FACTOR_GAIN;
443
0
  }
444
473k
  last_val_scf = 0;
445
473k
  pstr_section_data->first_scf = 0;
446
447
675k
  for (sect_idx1 = 0; sect_idx1 < pstr_section_data->num_of_sections; sect_idx1++) {
448
518k
    if (pstr_section_data->section[sect_idx1].code_book != CODE_BCK_ZERO_NO) {
449
315k
      pstr_section_data->first_scf = pstr_section_data->section[sect_idx1].sfb_start;
450
451
315k
      last_val_scf = ptr_scale_fac[pstr_section_data->first_scf];
452
315k
      break;
453
315k
    }
454
518k
  }
455
456
2.33M
  for (sect_idx1 = 0; sect_idx1 < pstr_section_data->num_of_sections; sect_idx1++) {
457
1.86M
    if ((pstr_section_data->section[sect_idx1].code_book != CODE_BCK_ZERO_NO) &&
458
1.28M
        (pstr_section_data->section[sect_idx1].code_book != CODE_BCK_PNS_NO)) {
459
1.28M
      for (sfb_idx1 = pstr_section_data->section[sect_idx1].sfb_start;
460
8.20M
           sfb_idx1 < pstr_section_data->section[sect_idx1].sfb_start +
461
8.20M
                          pstr_section_data->section[sect_idx1].sfb_cnt;
462
6.92M
           sfb_idx1++) {
463
6.92M
        if (ptr_max_val_in_sfb[sfb_idx1] == 0) {
464
494k
          found = 0;
465
466
494k
          if (scf_skip_counter == 0) {
467
352k
            if (sfb_idx1 == (pstr_section_data->section[sect_idx1].sfb_start +
468
352k
                             pstr_section_data->section[sect_idx1].sfb_cnt - 1)) {
469
43.6k
              found = 0;
470
309k
            } else {
471
309k
              for (scf_idx = (sfb_idx1 + 1);
472
465k
                   scf_idx < pstr_section_data->section[sect_idx1].sfb_start +
473
465k
                                 pstr_section_data->section[sect_idx1].sfb_cnt;
474
452k
                   scf_idx++) {
475
452k
                if (ptr_max_val_in_sfb[scf_idx] != 0) {
476
295k
                  found = 1;
477
478
295k
                  if ((abs32(ptr_scale_fac[scf_idx] - last_val_scf)) < CODE_BCK_SCF_LAV) {
479
293k
                    delta_scf = 0;
480
293k
                  } else {
481
1.69k
                    delta_scf = -(ptr_scale_fac[sfb_idx1] - last_val_scf);
482
483
1.69k
                    last_val_scf = ptr_scale_fac[sfb_idx1];
484
1.69k
                    scf_skip_counter = 0;
485
1.69k
                  }
486
295k
                  break;
487
295k
                }
488
                /* count scalefactor skip */
489
156k
                scf_skip_counter = scf_skip_counter + 1;
490
156k
              }
491
309k
            }
492
493
            /* search for the next ptr_max_val_in_sfb[] != 0 in all other sections */
494
352k
            for (sect_idx2 = (sect_idx1 + 1);
495
377k
                 (sect_idx2 < pstr_section_data->num_of_sections) && (found == 0); sect_idx2++) {
496
25.2k
              if ((pstr_section_data->section[sect_idx2].code_book != CODE_BCK_ZERO_NO) &&
497
22.0k
                  (pstr_section_data->section[sect_idx2].code_book != CODE_BCK_PNS_NO)) {
498
22.0k
                for (sfb_idx2 = pstr_section_data->section[sect_idx2].sfb_start;
499
23.0k
                     sfb_idx2 < pstr_section_data->section[sect_idx2].sfb_start +
500
23.0k
                                    pstr_section_data->section[sect_idx2].sfb_cnt;
501
23.0k
                     sfb_idx2++) {
502
23.0k
                  if (ptr_max_val_in_sfb[sfb_idx2] != 0) {
503
22.0k
                    found = 1;
504
505
22.0k
                    if ((abs32(ptr_scale_fac[sfb_idx2] - last_val_scf)) < CODE_BCK_SCF_LAV) {
506
21.6k
                      delta_scf = 0;
507
21.6k
                    } else {
508
403
                      delta_scf = -(ptr_scale_fac[sfb_idx1] - last_val_scf);
509
510
403
                      last_val_scf = ptr_scale_fac[sfb_idx1];
511
403
                      scf_skip_counter = 0;
512
403
                    }
513
22.0k
                    break;
514
22.0k
                  }
515
516
1.05k
                  scf_skip_counter = scf_skip_counter + 1;
517
1.05k
                }
518
22.0k
              }
519
25.2k
            }
520
521
352k
            if (found == 0) {
522
35.1k
              delta_scf = 0;
523
35.1k
              scf_skip_counter = 0;
524
35.1k
            }
525
352k
          } else {
526
141k
            delta_scf = 0;
527
528
141k
            scf_skip_counter = scf_skip_counter - 1;
529
141k
          }
530
6.42M
        } else {
531
6.42M
          delta_scf = -(ptr_scale_fac[sfb_idx1] - last_val_scf);
532
533
6.42M
          last_val_scf = ptr_scale_fac[sfb_idx1];
534
6.42M
        }
535
536
6.92M
        pstr_section_data->scale_fac_bits +=
537
6.92M
            ia_enhaacplus_enc_bit_count_scalefactor_delta(delta_scf, pstr_huffman_tbl);
538
6.92M
      }
539
1.28M
    }
540
1.86M
  }
541
473k
  return IA_NO_ERROR;
542
473k
}
543
544
IA_ERRORCODE ia_enhaacplus_enc_dyn_bitcount(
545
    const WORD16 *ptr_quant_spec, const UWORD16 *ptr_max_val_in_sfb, const WORD16 *ptr_scale_fac,
546
    const WORD32 block_type, const WORD32 sfb_cnt, const WORD32 max_sfb_per_grp,
547
    const WORD32 sfb_per_grp, const WORD32 *ptr_sfb_offset,
548
    ixheaace_section_data *pstr_section_data, WORD32 *ptr_side_info_tab_long,
549
    WORD32 *ptr_side_info_tab_short, ixheaace_huffman_tables *ptr_huffman_tbl,
550
473k
    WORD32 *ptr_scratch_buf, WORD32 aot, WORD32 *bit_cnt) {
551
473k
  IA_ERRORCODE err_code;
552
473k
  WORD32(*ptr_bit_look_up)
553
473k
  [CODE_BCK_ESC_NDX + 1] = (WORD32(*)[CODE_BCK_ESC_NDX + 1]) ptr_scratch_buf;
554
473k
  WORD32 *ptr_merge_gain_look_up =
555
473k
      ptr_scratch_buf + MAXIMUM_SCALE_FACTOR_BAND_LONG * (CODE_BCK_ESC_NDX + 1);
556
473k
  *bit_cnt = 0;
557
473k
  pstr_section_data->block_type = block_type;
558
473k
  pstr_section_data->sfb_cnt = sfb_cnt;
559
473k
  pstr_section_data->sfb_per_group = sfb_per_grp;
560
561
473k
  pstr_section_data->total_groups_cnt = sfb_cnt / sfb_per_grp;
562
563
473k
  pstr_section_data->max_sfb_per_grp = max_sfb_per_grp;
564
565
473k
  err_code = ia_enhaacplus_enc_noiseless_counter(
566
473k
      pstr_section_data, ptr_merge_gain_look_up, ptr_bit_look_up, ptr_quant_spec,
567
473k
      ptr_max_val_in_sfb, ptr_sfb_offset, block_type, ptr_side_info_tab_long,
568
473k
      ptr_side_info_tab_short, ptr_huffman_tbl, aot);
569
570
473k
  if (err_code != IA_NO_ERROR) {
571
0
    return err_code;
572
0
  }
573
574
473k
  err_code = ia_enhaacplus_enc_scf_count(ptr_scale_fac, ptr_max_val_in_sfb, pstr_section_data,
575
473k
                                         ptr_huffman_tbl);
576
577
473k
  if (err_code != IA_NO_ERROR) {
578
0
    return err_code;
579
0
  }
580
581
473k
  *bit_cnt = (pstr_section_data->huffman_bits + pstr_section_data->side_info_bits +
582
473k
              pstr_section_data->scale_fac_bits);
583
473k
  return IA_NO_ERROR;
584
473k
}
585
586
5.94k
VOID ia_enhaacplus_enc_bitcount_init(WORD32 *side_info_tab_long, WORD32 *side_info_tab_short) {
587
5.94k
  WORD32 i;
588
589
  /* side_info_tab_long[] */
590
315k
  for (i = 0; i <= MAXIMUM_SCALE_FACTOR_BAND_LONG; i++) {
591
309k
    side_info_tab_long[i] = ia_enhaacplus_enc_calc_side_info_bits(i, LONG_WINDOW);
592
309k
  }
593
594
  /* side_info_tab_short[] */
595
101k
  for (i = 0; i <= MAXIMUM_SCALE_FACTOR_BAND_SHORT; i++) {
596
95.1k
    side_info_tab_short[i] = ia_enhaacplus_enc_calc_side_info_bits(i, SHORT_WINDOW);
597
95.1k
  }
598
5.94k
}