Coverage Report

Created: 2026-05-08 06:29

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