Coverage Report

Created: 2026-08-13 06:34

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
329k
static WORD32 ia_enhaacplus_enc_calc_side_info_bits(WORD32 sfb_cnt, WORD32 block_type) {
53
329k
  WORD32 seg_len_bits = (block_type == SHORT_WINDOW ? 3 : 5);
54
329k
  WORD32 escape_val = (block_type == SHORT_WINDOW ? 7 : 31);
55
329k
  WORD32 side_info_bits, tmp;
56
57
329k
  side_info_bits = CODE_BCK_BITS;
58
329k
  tmp = sfb_cnt;
59
60
814k
  while (tmp >= 0) {
61
484k
    side_info_bits += seg_len_bits;
62
484k
    tmp -= escape_val;
63
484k
  }
64
65
329k
  return (side_info_bits);
66
329k
}
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
641k
    WORD32 aot) {
74
641k
  WORD8 i;
75
76
13.5M
  for (i = 0; i < max_sfb; i++) {
77
12.8M
    WORD32 sfb_width, max_val;
78
79
12.8M
    pstr_section_info[i].sfb_cnt = 1;
80
12.8M
    pstr_section_info[i].sfb_start = i;
81
82
12.8M
    switch (aot) {
83
3.03M
      case AOT_AAC_LC:
84
9.21M
      case AOT_SBR:
85
9.96M
      case AOT_PS:
86
9.96M
        pstr_section_info[i].section_bits = INVALID_BITCOUNT_LC;
87
9.96M
        break;
88
89
730k
      case AOT_AAC_LD:
90
2.91M
      case AOT_AAC_ELD:
91
2.91M
        pstr_section_info[i].section_bits = INVALID_BITCOUNT_LD;
92
2.91M
        break;
93
12.8M
    }
94
95
12.8M
    pstr_section_info[i].code_book = -1;
96
97
12.8M
    sfb_width = ptr_sfb_offset[i + 1] - ptr_sfb_offset[i];
98
99
12.8M
    max_val = sfb_max[i];
100
101
12.8M
    ia_enhaacplus_enc_bitcount(ptr_quant_spec + ptr_sfb_offset[i], sfb_width, max_val,
102
12.8M
                               bit_look_up[i], pstr_huffman_tbl, aot);
103
12.8M
  }
104
641k
}
105
106
static WORD32 ia_enhaacplus_enc_find_best_book(const WORD32 *ptr_bit_cnt, WORD8 *ptr_codebook,
107
14.5M
                                               WORD32 aot) {
108
14.5M
  WORD32 min_bits = 0, temp1, temp2;
109
14.5M
  WORD8 temp_book = CODE_BCK_ESC_NDX;
110
14.5M
  WORD8 j;
111
112
14.5M
  switch (aot) {
113
3.41M
    case AOT_AAC_LC:
114
10.3M
    case AOT_SBR:
115
11.3M
    case AOT_PS:
116
11.3M
      min_bits = INVALID_BITCOUNT_LC;
117
11.3M
      break;
118
119
813k
    case AOT_AAC_LD:
120
3.24M
    case AOT_AAC_ELD:
121
3.24M
      min_bits = INVALID_BITCOUNT_LD;
122
3.24M
      break;
123
14.5M
  }
124
125
102M
  for (j = 0; j <= CODE_BCK_ESC_NDX; j += 2) {
126
87.5M
    temp1 = *ptr_bit_cnt++;
127
87.5M
    temp2 = *ptr_bit_cnt++;
128
87.5M
    if (temp1 < min_bits) {
129
12.4M
      min_bits = temp1;
130
12.4M
      temp_book = j;
131
12.4M
    }
132
87.5M
    if (temp2 < min_bits) {
133
8.03M
      min_bits = temp2;
134
8.03M
      temp_book = j + 1;
135
8.03M
    }
136
87.5M
  }
137
138
14.5M
  *ptr_codebook = temp_book;
139
14.5M
  return min_bits;
140
14.5M
}
141
142
static WORD32 ia_enhaacplus_enc_find_min_merge_bits(const WORD32 *ptr_bit_cnt1,
143
6.85M
                                                    const WORD32 *ptr_bit_cnt2, WORD32 aot) {
144
6.85M
  WORD32 min_bits = 0, j, temp1, temp2, temp3, temp4;
145
146
6.85M
  switch (aot) {
147
1.34M
    case AOT_AAC_LC:
148
4.57M
    case AOT_SBR:
149
5.26M
    case AOT_PS:
150
5.26M
      min_bits = INVALID_BITCOUNT_LC;
151
5.26M
      break;
152
153
442k
    case AOT_AAC_LD:
154
1.58M
    case AOT_AAC_ELD:
155
1.58M
      min_bits = INVALID_BITCOUNT_LD;
156
1.58M
      break;
157
6.85M
  }
158
159
47.9M
  for (j = CODE_BCK_ESC_NDX; j >= 0; j -= 2) {
160
41.1M
    temp1 = *ptr_bit_cnt1++;
161
41.1M
    temp2 = *ptr_bit_cnt2++;
162
41.1M
    temp3 = *ptr_bit_cnt1++;
163
164
41.1M
    temp1 = temp1 + temp2;
165
166
41.1M
    min_bits = MIN(temp1, min_bits);
167
41.1M
    temp4 = *ptr_bit_cnt2++;
168
41.1M
    temp1 = temp3 + temp4;
169
170
41.1M
    min_bits = MIN(temp1, min_bits);
171
41.1M
  }
172
173
6.85M
  return min_bits;
174
6.85M
}
175
176
static VOID ia_enhaacplus_enc_merge_bit_look_up(WORD32 *ptr_bit_cnt1, const WORD32 *ptr_bit_cnt2,
177
11.1M
                                                WORD32 aot) {
178
11.1M
  WORD32 j, temp1, temp2, temp3, temp4;
179
11.1M
  WORD32 invalid_bitcnt = 0;
180
181
11.1M
  switch (aot) {
182
2.65M
    case AOT_AAC_LC:
183
8.04M
    case AOT_SBR:
184
8.59M
    case AOT_PS:
185
8.59M
      invalid_bitcnt = INVALID_BITCOUNT_LC;
186
8.59M
      break;
187
188
646k
    case AOT_AAC_LD:
189
2.57M
    case AOT_AAC_ELD:
190
2.57M
      invalid_bitcnt = INVALID_BITCOUNT_LD;
191
2.57M
      break;
192
11.1M
  }
193
78.2M
  for (j = 0; j <= CODE_BCK_ESC_NDX; j += 2) {
194
67.0M
    temp1 = ptr_bit_cnt1[j];
195
67.0M
    temp2 = ptr_bit_cnt2[j];
196
67.0M
    temp3 = ptr_bit_cnt1[j + 1];
197
198
67.0M
    ptr_bit_cnt1[j] = MIN(temp1 + temp2, invalid_bitcnt);
199
67.0M
    temp4 = ptr_bit_cnt2[j + 1];
200
67.0M
    ptr_bit_cnt1[j + 1] = MIN(temp3 + temp4, invalid_bitcnt);
201
67.0M
  }
202
11.1M
}
203
204
static WORD32 ia_enhaacplus_enc_find_max_merge(
205
    const WORD32 merge_gain_look_up[MAXIMUM_SCALE_FACTOR_BAND_LONG],
206
2.75M
    const ixheaace_section_info *section, const WORD32 max_sfb, WORD32 *max_ndx) {
207
2.75M
  WORD32 i, max_merge_gain = 0;
208
209
20.7M
  for (i = 0; i + section[i].sfb_cnt < max_sfb; i += section[i].sfb_cnt) {
210
17.9M
    if (merge_gain_look_up[i] > max_merge_gain) {
211
3.95M
      max_merge_gain = merge_gain_look_up[i];
212
3.95M
      *max_ndx = i;
213
3.95M
    }
214
17.9M
  }
215
216
2.75M
  return (max_merge_gain);
217
2.75M
}
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.85M
    const WORD32 *pstr_side_info_tab, const WORD32 idx1, const WORD32 idx2, WORD32 aot) {
223
6.85M
  WORD32 split_bits;
224
6.85M
  WORD32 merge_bits;
225
6.85M
  WORD32 merge_gain;
226
227
6.85M
  split_bits = pstr_section_info[idx1].section_bits + pstr_section_info[idx2].section_bits;
228
229
6.85M
  merge_bits =
230
6.85M
      pstr_side_info_tab[pstr_section_info[idx1].sfb_cnt + pstr_section_info[idx2].sfb_cnt] +
231
6.85M
      ia_enhaacplus_enc_find_min_merge_bits(bit_look_up[idx1], bit_look_up[idx2], aot);
232
233
6.85M
  merge_gain = split_bits - merge_bits;
234
235
6.85M
  return merge_gain;
236
6.85M
}
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
641k
    const WORD32 max_sfb, WORD32 aot) {
242
641k
  WORD32 i = 0;
243
641k
  WORD32 invalid_bitcnt = 0;
244
245
641k
  switch (aot) {
246
162k
    case AOT_AAC_LC:
247
468k
    case AOT_SBR:
248
512k
    case AOT_PS:
249
512k
      invalid_bitcnt = INVALID_BITCOUNT_LC;
250
512k
      break;
251
252
34.4k
    case AOT_AAC_LD:
253
129k
    case AOT_AAC_ELD:
254
255
129k
      invalid_bitcnt = INVALID_BITCOUNT_LD;
256
129k
      break;
257
641k
  }
258
13.5M
  while (i < max_sfb) {
259
12.8M
    if (pstr_section[i].section_bits == invalid_bitcnt) {
260
12.8M
      pstr_section[i].section_bits = (WORD16)ia_enhaacplus_enc_find_best_book(
261
12.8M
          bit_look_up[i], &(pstr_section[i].code_book), aot);
262
12.8M
    }
263
12.8M
    i++;
264
12.8M
  }
265
641k
}
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
641k
    const WORD32 max_sfb, const WORD32 *ptr_side_info_tab, WORD32 aot) {
271
641k
  WORD32 merge_start = 0, merge_end;
272
273
3.82M
  do {
274
12.8M
    for (merge_end = merge_start + 1; merge_end < max_sfb; merge_end++) {
275
12.2M
      if (pstr_section_info[merge_start].code_book != pstr_section_info[merge_end].code_book) {
276
3.18M
        break;
277
3.18M
      }
278
279
9.05M
      pstr_section_info[merge_start].sfb_cnt++;
280
281
9.05M
      pstr_section_info[merge_start].section_bits += pstr_section_info[merge_end].section_bits;
282
283
9.05M
      ia_enhaacplus_enc_merge_bit_look_up(bit_look_up[merge_start], bit_look_up[merge_end], aot);
284
9.05M
    }
285
286
3.82M
    pstr_section_info[merge_start].section_bits +=
287
3.82M
        (WORD16)ptr_side_info_tab[pstr_section_info[merge_start].sfb_cnt];
288
289
3.82M
    pstr_section_info[merge_end - 1].sfb_start = pstr_section_info[merge_start].sfb_start;
290
291
3.82M
    merge_start = merge_end;
292
293
3.82M
  } while (merge_start < max_sfb);
294
641k
}
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
641k
    const WORD32 max_sfb, const WORD32 *ptr_side_info_tab, WORD32 aot) {
301
641k
  WORD32 i;
302
303
3.82M
  for (i = 0; i + pstr_section_info[i].sfb_cnt < max_sfb; i += pstr_section_info[i].sfb_cnt) {
304
3.18M
    merge_gain_look_up[i] =
305
3.18M
        ia_enhaacplus_enc_calc_merge_gain(pstr_section_info, bit_look_up, ptr_side_info_tab, i,
306
3.18M
                                          i + pstr_section_info[i].sfb_cnt, aot);
307
3.18M
  }
308
309
2.75M
  while (TRUE) {
310
2.75M
    WORD32 max_merge_gain = 0, max_idx = 0, max_idx_next = 0, max_idx_last = 0;
311
312
2.75M
    max_merge_gain = ia_enhaacplus_enc_find_max_merge(merge_gain_look_up, pstr_section_info,
313
2.75M
                                                      max_sfb, &max_idx);
314
315
2.75M
    if (max_merge_gain <= 0) {
316
641k
      break;
317
641k
    }
318
319
2.11M
    max_idx_next = max_idx + pstr_section_info[max_idx].sfb_cnt;
320
321
2.11M
    pstr_section_info[max_idx].sfb_cnt += pstr_section_info[max_idx_next].sfb_cnt;
322
323
2.11M
    pstr_section_info[max_idx].section_bits +=
324
2.11M
        pstr_section_info[max_idx_next].section_bits - (WORD16)max_merge_gain;
325
326
2.11M
    ia_enhaacplus_enc_merge_bit_look_up(bit_look_up[max_idx], bit_look_up[max_idx_next], aot);
327
328
2.11M
    if (max_idx != 0) {
329
1.75M
      max_idx_last = pstr_section_info[max_idx - 1].sfb_start;
330
331
1.75M
      merge_gain_look_up[max_idx_last] = ia_enhaacplus_enc_calc_merge_gain(
332
1.75M
          pstr_section_info, bit_look_up, ptr_side_info_tab, max_idx_last, max_idx, aot);
333
1.75M
    }
334
335
2.11M
    max_idx_next = max_idx + pstr_section_info[max_idx].sfb_cnt;
336
337
2.11M
    pstr_section_info[max_idx_next - 1].sfb_start = pstr_section_info[max_idx].sfb_start;
338
339
2.11M
    if (max_idx_next < max_sfb) {
340
1.92M
      merge_gain_look_up[max_idx] = ia_enhaacplus_enc_calc_merge_gain(
341
1.92M
          pstr_section_info, bit_look_up, ptr_side_info_tab, max_idx, max_idx_next, aot);
342
1.92M
    }
343
2.11M
  }
344
641k
}
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
464k
    ixheaace_huffman_tables *pstr_huffman_tbl, WORD32 aot) {
353
464k
  WORD32 grp_idx;
354
464k
  WORD32 *ptr_side_info_tab = 0;
355
464k
  ixheaace_section_info *pstr_section_info;
356
464k
  WORD32 i;
357
358
24.1M
  for (i = 0; i < MAXIMUM_SCALE_FACTOR_BAND_LONG; i++) {
359
23.7M
    memset(bit_look_up[i], 0, (CODE_BCK_ESC_NDX + 1) * sizeof(bit_look_up[0][0]));
360
23.7M
  }
361
362
  /* counting previous operations */
363
464k
  switch (block_type) {
364
336k
    case LONG_WINDOW:
365
357k
    case START_WINDOW:
366
376k
    case STOP_WINDOW:
367
368
376k
      ptr_side_info_tab = ptr_side_info_tab_long;
369
376k
      break;
370
88.1k
    case SHORT_WINDOW:
371
372
88.1k
      ptr_side_info_tab = ptr_side_info_tab_short;
373
88.1k
      break;
374
0
    default:
375
0
      return IA_EXHEAACE_EXE_FATAL_INVALID_BLOCK_TYPE;
376
464k
  }
377
378
464k
  pstr_section_data->num_of_sections = 0;
379
464k
  pstr_section_data->huffman_bits = 0;
380
464k
  pstr_section_data->side_info_bits = 0;
381
382
464k
  if (pstr_section_data->max_sfb_per_grp == 0) {
383
77.0k
    return IA_NO_ERROR;
384
77.0k
  }
385
386
1.02M
  for (grp_idx = 0; grp_idx < pstr_section_data->sfb_cnt;
387
641k
       grp_idx += pstr_section_data->sfb_per_group) {
388
641k
    pstr_section_info = pstr_section_data->section + pstr_section_data->num_of_sections;
389
390
641k
    ia_enhaacplus_enc_build_bit_look_up(ptr_quant_spec, pstr_section_data->max_sfb_per_grp,
391
641k
                                        ptr_sfb_offset + grp_idx, ptr_max_val_in_sfb + grp_idx,
392
641k
                                        bit_look_up, pstr_section_info, pstr_huffman_tbl, aot);
393
394
641k
    ia_enhaacplus_enc_gm_stage0(pstr_section_info, bit_look_up,
395
641k
                                pstr_section_data->max_sfb_per_grp, aot);
396
397
641k
    ia_enhaacplus_enc_gm_stage1(pstr_section_info, bit_look_up,
398
641k
                                pstr_section_data->max_sfb_per_grp, ptr_side_info_tab, aot);
399
400
641k
    ia_enhaacplus_enc_gm_stage2(pstr_section_info, merge_gain_look_up, bit_look_up,
401
641k
                                pstr_section_data->max_sfb_per_grp, ptr_side_info_tab, aot);
402
403
2.34M
    for (i = 0; i < pstr_section_data->max_sfb_per_grp; i += pstr_section_info[i].sfb_cnt) {
404
1.70M
      ia_enhaacplus_enc_find_best_book(bit_look_up[i], &(pstr_section_info[i].code_book), aot);
405
406
1.70M
      pstr_section_info[i].sfb_start += (WORD8)grp_idx;
407
408
1.70M
      pstr_section_data->huffman_bits +=
409
1.70M
          pstr_section_info[i].section_bits - ptr_side_info_tab[pstr_section_info[i].sfb_cnt];
410
411
1.70M
      pstr_section_data->side_info_bits += ptr_side_info_tab[pstr_section_info[i].sfb_cnt];
412
413
1.70M
      pstr_section_data->section[pstr_section_data->num_of_sections++] = pstr_section_info[i];
414
1.70M
    }
415
641k
  }
416
387k
  return IA_NO_ERROR;
417
464k
}
418
419
static WORD32 ia_enhaacplus_enc_bit_count_scalefactor_delta(
420
7.02M
    WORD32 delta, ixheaace_huffman_tables *pstr_huffman_tbl) {
421
7.02M
  return pstr_huffman_tbl->huff_ltabscf[delta + CODE_BCK_SCF_LAV];
422
7.02M
}
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
464k
                                                ixheaace_huffman_tables *pstr_huffman_tbl) {
428
464k
  WORD32 sect_idx1 = 0;
429
464k
  WORD32 sfb_idx1 = 0;
430
464k
  WORD32 scf_idx = 0;
431
464k
  WORD32 sect_idx2 = 0;
432
464k
  WORD32 sfb_idx2 = 0;
433
434
464k
  WORD32 last_val_scf = 0;
435
464k
  WORD32 delta_scf = 0;
436
464k
  WORD32 found = 0;
437
464k
  WORD32 scf_skip_counter = 0;
438
439
464k
  pstr_section_data->scale_fac_bits = 0;
440
441
464k
  if (ptr_scale_fac == 0) {
442
0
    return IA_EXHEAACE_EXE_FATAL_INVALID_SCALE_FACTOR_GAIN;
443
0
  }
444
464k
  last_val_scf = 0;
445
464k
  pstr_section_data->first_scf = 0;
446
447
627k
  for (sect_idx1 = 0; sect_idx1 < pstr_section_data->num_of_sections; sect_idx1++) {
448
460k
    if (pstr_section_data->section[sect_idx1].code_book != CODE_BCK_ZERO_NO) {
449
297k
      pstr_section_data->first_scf = pstr_section_data->section[sect_idx1].sfb_start;
450
451
297k
      last_val_scf = ptr_scale_fac[pstr_section_data->first_scf];
452
297k
      break;
453
297k
    }
454
460k
  }
455
456
2.17M
  for (sect_idx1 = 0; sect_idx1 < pstr_section_data->num_of_sections; sect_idx1++) {
457
1.70M
    if ((pstr_section_data->section[sect_idx1].code_book != CODE_BCK_ZERO_NO) &&
458
1.19M
        (pstr_section_data->section[sect_idx1].code_book != CODE_BCK_PNS_NO)) {
459
1.19M
      for (sfb_idx1 = pstr_section_data->section[sect_idx1].sfb_start;
460
8.21M
           sfb_idx1 < pstr_section_data->section[sect_idx1].sfb_start +
461
8.21M
                          pstr_section_data->section[sect_idx1].sfb_cnt;
462
7.02M
           sfb_idx1++) {
463
7.02M
        if (ptr_max_val_in_sfb[sfb_idx1] == 0) {
464
432k
          found = 0;
465
466
432k
          if (scf_skip_counter == 0) {
467
305k
            if (sfb_idx1 == (pstr_section_data->section[sect_idx1].sfb_start +
468
305k
                             pstr_section_data->section[sect_idx1].sfb_cnt - 1)) {
469
35.4k
              found = 0;
470
270k
            } else {
471
270k
              for (scf_idx = (sfb_idx1 + 1);
472
409k
                   scf_idx < pstr_section_data->section[sect_idx1].sfb_start +
473
409k
                                 pstr_section_data->section[sect_idx1].sfb_cnt;
474
399k
                   scf_idx++) {
475
399k
                if (ptr_max_val_in_sfb[scf_idx] != 0) {
476
260k
                  found = 1;
477
478
260k
                  if ((abs32(ptr_scale_fac[scf_idx] - last_val_scf)) < CODE_BCK_SCF_LAV) {
479
257k
                    delta_scf = 0;
480
257k
                  } else {
481
2.56k
                    delta_scf = -(ptr_scale_fac[sfb_idx1] - last_val_scf);
482
483
2.56k
                    last_val_scf = ptr_scale_fac[sfb_idx1];
484
2.56k
                    scf_skip_counter = 0;
485
2.56k
                  }
486
260k
                  break;
487
260k
                }
488
                /* count scalefactor skip */
489
138k
                scf_skip_counter = scf_skip_counter + 1;
490
138k
              }
491
270k
            }
492
493
            /* search for the next ptr_max_val_in_sfb[] != 0 in all other sections */
494
305k
            for (sect_idx2 = (sect_idx1 + 1);
495
325k
                 (sect_idx2 < pstr_section_data->num_of_sections) && (found == 0); sect_idx2++) {
496
19.5k
              if ((pstr_section_data->section[sect_idx2].code_book != CODE_BCK_ZERO_NO) &&
497
17.8k
                  (pstr_section_data->section[sect_idx2].code_book != CODE_BCK_PNS_NO)) {
498
17.8k
                for (sfb_idx2 = pstr_section_data->section[sect_idx2].sfb_start;
499
18.7k
                     sfb_idx2 < pstr_section_data->section[sect_idx2].sfb_start +
500
18.7k
                                    pstr_section_data->section[sect_idx2].sfb_cnt;
501
18.7k
                     sfb_idx2++) {
502
18.7k
                  if (ptr_max_val_in_sfb[sfb_idx2] != 0) {
503
17.8k
                    found = 1;
504
505
17.8k
                    if ((abs32(ptr_scale_fac[sfb_idx2] - last_val_scf)) < CODE_BCK_SCF_LAV) {
506
17.4k
                      delta_scf = 0;
507
17.4k
                    } else {
508
361
                      delta_scf = -(ptr_scale_fac[sfb_idx1] - last_val_scf);
509
510
361
                      last_val_scf = ptr_scale_fac[sfb_idx1];
511
361
                      scf_skip_counter = 0;
512
361
                    }
513
17.8k
                    break;
514
17.8k
                  }
515
516
900
                  scf_skip_counter = scf_skip_counter + 1;
517
900
                }
518
17.8k
              }
519
19.5k
            }
520
521
305k
            if (found == 0) {
522
27.7k
              delta_scf = 0;
523
27.7k
              scf_skip_counter = 0;
524
27.7k
            }
525
305k
          } else {
526
126k
            delta_scf = 0;
527
528
126k
            scf_skip_counter = scf_skip_counter - 1;
529
126k
          }
530
6.59M
        } else {
531
6.59M
          delta_scf = -(ptr_scale_fac[sfb_idx1] - last_val_scf);
532
533
6.59M
          last_val_scf = ptr_scale_fac[sfb_idx1];
534
6.59M
        }
535
536
7.02M
        pstr_section_data->scale_fac_bits +=
537
7.02M
            ia_enhaacplus_enc_bit_count_scalefactor_delta(delta_scf, pstr_huffman_tbl);
538
7.02M
      }
539
1.19M
    }
540
1.70M
  }
541
464k
  return IA_NO_ERROR;
542
464k
}
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
464k
    WORD32 *ptr_scratch_buf, WORD32 aot, WORD32 *bit_cnt) {
551
464k
  IA_ERRORCODE err_code;
552
464k
  WORD32(*ptr_bit_look_up)
553
464k
  [CODE_BCK_ESC_NDX + 1] = (WORD32(*)[CODE_BCK_ESC_NDX + 1]) ptr_scratch_buf;
554
464k
  WORD32 *ptr_merge_gain_look_up =
555
464k
      ptr_scratch_buf + MAXIMUM_SCALE_FACTOR_BAND_LONG * (CODE_BCK_ESC_NDX + 1);
556
464k
  *bit_cnt = 0;
557
464k
  pstr_section_data->block_type = block_type;
558
464k
  pstr_section_data->sfb_cnt = sfb_cnt;
559
464k
  pstr_section_data->sfb_per_group = sfb_per_grp;
560
561
464k
  pstr_section_data->total_groups_cnt = sfb_cnt / sfb_per_grp;
562
563
464k
  pstr_section_data->max_sfb_per_grp = max_sfb_per_grp;
564
565
464k
  err_code = ia_enhaacplus_enc_noiseless_counter(
566
464k
      pstr_section_data, ptr_merge_gain_look_up, ptr_bit_look_up, ptr_quant_spec,
567
464k
      ptr_max_val_in_sfb, ptr_sfb_offset, block_type, ptr_side_info_tab_long,
568
464k
      ptr_side_info_tab_short, ptr_huffman_tbl, aot);
569
570
464k
  if (err_code != IA_NO_ERROR) {
571
0
    return err_code;
572
0
  }
573
574
464k
  err_code = ia_enhaacplus_enc_scf_count(ptr_scale_fac, ptr_max_val_in_sfb, pstr_section_data,
575
464k
                                         ptr_huffman_tbl);
576
577
464k
  if (err_code != IA_NO_ERROR) {
578
0
    return err_code;
579
0
  }
580
581
464k
  *bit_cnt = (pstr_section_data->huffman_bits + pstr_section_data->side_info_bits +
582
464k
              pstr_section_data->scale_fac_bits);
583
464k
  return IA_NO_ERROR;
584
464k
}
585
586
4.84k
VOID ia_enhaacplus_enc_bitcount_init(WORD32 *side_info_tab_long, WORD32 *side_info_tab_short) {
587
4.84k
  WORD32 i;
588
589
  /* side_info_tab_long[] */
590
256k
  for (i = 0; i <= MAXIMUM_SCALE_FACTOR_BAND_LONG; i++) {
591
252k
    side_info_tab_long[i] = ia_enhaacplus_enc_calc_side_info_bits(i, LONG_WINDOW);
592
252k
  }
593
594
  /* side_info_tab_short[] */
595
82.4k
  for (i = 0; i <= MAXIMUM_SCALE_FACTOR_BAND_SHORT; i++) {
596
77.5k
    side_info_tab_short[i] = ia_enhaacplus_enc_calc_side_info_bits(i, SHORT_WINDOW);
597
77.5k
  }
598
4.84k
}