Coverage Report

Created: 2026-08-13 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/decoder/ixheaacd_huff_code_reorder.c
Line
Count
Source
1
/******************************************************************************
2
 *                                                                            *
3
 * Copyright (C) 2018 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
#include <string.h>
21
#include "ixheaacd_sbr_common.h"
22
#include "ixheaac_type_def.h"
23
#include "ixheaac_error_standards.h"
24
25
#include "ixheaac_constants.h"
26
#include "ixheaac_basic_ops32.h"
27
#include "ixheaac_basic_ops40.h"
28
#include "ixheaac_basic_ops.h"
29
30
#include "ixheaacd_bitbuffer.h"
31
#include "ixheaacd_defines.h"
32
#include "ixheaacd_aac_rom.h"
33
#include "ixheaacd_pulsedata.h"
34
35
#include "ixheaacd_pns.h"
36
37
#include "ixheaacd_lt_predict.h"
38
#include "ixheaacd_cnst.h"
39
#include "ixheaacd_ec_defines.h"
40
#include "ixheaacd_ec_struct_def.h"
41
#include "ixheaacd_channelinfo.h"
42
43
#include "ixheaacd_drc_data_struct.h"
44
#include "ixheaacd_drc_dec.h"
45
46
#include "ixheaacd_sbrdecoder.h"
47
48
#include "ixheaacd_block.h"
49
#include "ixheaacd_channel.h"
50
#include "ixheaacd_common_rom.h"
51
52
#include "ixheaacd_aacdec.h"
53
54
#include "ixheaacd_sbrdecsettings.h"
55
#include "ixheaacd_sbr_scale.h"
56
#include "ixheaacd_env_extr_part.h"
57
#include "ixheaacd_sbr_rom.h"
58
#include "ixheaacd_audioobjtypes.h"
59
#include "ixheaacd_memory_standards.h"
60
#include "ixheaacd_latmdemux.h"
61
#include "ixheaacd_mps_polyphase.h"
62
#include "ixheaacd_config.h"
63
#include "ixheaacd_hybrid.h"
64
#include "ixheaacd_ps_dec.h"
65
#include "ixheaacd_qmf_dec.h"
66
#include "ixheaacd_mps_macro_def.h"
67
#include "ixheaacd_mps_struct_def.h"
68
#include "ixheaacd_mps_res_rom.h"
69
#include "ixheaacd_mps_aac_struct.h"
70
#include "ixheaacd_mps_dec.h"
71
#include "ixheaacd_struct_def.h"
72
73
#include "ixheaacd_rvlc.h"
74
75
const UWORD8 ixheaacd_min_huff_cb_pair_tbl[MAX_CB_PAIRS] = {
76
    0,  1,  3,  5,  7,  9,  16, 17, 18, 19, 20, 21,
77
    22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 11};
78
const UWORD8 ixheaacd_max_huff_cb_pair_table[MAX_CB_PAIRS] = {
79
    0,  2,  4,  6,  8,  10, 16, 17, 18, 19, 20, 21,
80
    22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 11};
81
const UWORD8 ixheaacd_max_huff_cw_len_table[MAX_CB] = {
82
    0,  11, 9,  20, 16, 13, 11, 14, 12, 17, 14, 49, 0,  0,  0,  0,
83
    14, 17, 21, 21, 25, 25, 29, 29, 29, 29, 33, 33, 33, 37, 37, 41};
84
const UWORD8 ixheaacd_huff_cb_dim_table[MAX_CB] = {
85
    2, 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,
86
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
87
const UWORD8 ixheaacd_huff_cb_dim_shift_table[MAX_CB] = {
88
    1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
89
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
90
const UWORD8 ixheaacd_huff_cb_sign_table[MAX_CB] = {
91
    0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0,
92
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
93
const UWORD8 ixheaacd_huff_cb_priority_table[MAX_CB] = {
94
    0, 1, 1, 2, 2,  3,  3,  4,  4,  5,  5,  22, 0,  0,  0,  0,
95
    6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};
96
const UWORD16 ixheaacd_huff_reord_lav_table[MAX_CB] = {
97
    0,    1,   1,   2,   2,   4,   4,   7,   7,    12,  12,
98
    8191, 0,   0,   0,   0,   15,  31,  47,  63,   95,  127,
99
    159,  191, 223, 255, 319, 383, 511, 767, 1023, 2047};
100
101
364k
VOID ixheaacd_huff_code_reorder_tbl_init(ia_hcr_info_struct *ptr_hcr_info) {
102
364k
  ptr_hcr_info->codebook_pairs.ptr_min_cb_pair_tbl =
103
364k
      ixheaacd_min_huff_cb_pair_tbl;
104
364k
  ptr_hcr_info->codebook_pairs.ptr_max_cb_pair_tbl =
105
364k
      ixheaacd_max_huff_cb_pair_table;
106
364k
  ptr_hcr_info->table_info.ptr_max_cw_len_tbl = ixheaacd_max_huff_cw_len_table;
107
364k
  ptr_hcr_info->table_info.ptr_cb_dimension_tbl = ixheaacd_huff_cb_dim_table;
108
364k
  ptr_hcr_info->table_info.ptr_cb_dim_shift_tbl =
109
364k
      ixheaacd_huff_cb_dim_shift_table;
110
364k
  ptr_hcr_info->table_info.ptr_cb_sign_tbl = ixheaacd_huff_cb_sign_table;
111
364k
  ptr_hcr_info->table_info.ptr_cb_priority = ixheaacd_huff_cb_priority_table;
112
364k
  ptr_hcr_info->table_info.ptr_lav_tbl = ixheaacd_huff_reord_lav_table;
113
364k
}
114
115
37.4k
VOID ixheaacd_huff_mute_erroneous_lines(ia_hcr_info_struct *ptr_hcr_info) {
116
37.4k
  WORD32 c;
117
37.4k
  WORD32 *ptr_long = ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base;
118
119
38.3M
  for (c = 0; c < 1024; c++) {
120
38.3M
    if (ptr_long[c] == (WORD32)8192) {
121
84.4k
      ptr_long[c] = 0;
122
84.4k
    }
123
38.3M
  }
124
37.4k
}
125
126
static UWORD8 ixheaacd_err_detect_pcw_segment(WORD8 remaining_bits_in_segment,
127
                                              ia_hcr_info_struct *ptr_hcr_info,
128
                                              ia_pcw_type_struct kind,
129
                                              WORD32 *qsc_base_of_cw,
130
251k
                                              UWORD8 dimension) {
131
251k
  WORD8 i;
132
251k
  if (remaining_bits_in_segment < 0) {
133
0
    switch (kind) {
134
0
      case PCW:
135
0
        ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 31);
136
0
        break;
137
0
      case PCW_SIGN:
138
0
        ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 30);
139
0
        break;
140
0
      case PCW_ESC_SIGN:
141
0
        ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 29);
142
0
        break;
143
0
    }
144
0
    for (i = dimension; i != 0; i--) {
145
0
      *qsc_base_of_cw++ = (WORD32)8192;
146
0
    }
147
0
    return 1;
148
0
  }
149
251k
  return 0;
150
251k
}
151
152
188k
static VOID ixheaacd_nonpcw_sideinfo_init(ia_hcr_info_struct *ptr_hcr_info) {
153
188k
  UWORD16 i, k;
154
188k
  UWORD8 cb_dim;
155
188k
  UWORD8 *ptr_cb = ptr_hcr_info->str_non_pcw_side_info.ptr_cb;
156
188k
  UWORD16 *res_ptr_idx = ptr_hcr_info->str_non_pcw_side_info.res_ptr_idx;
157
188k
  UWORD16 *ptr_num_ext_sorted_cw_in_sect =
158
188k
      ptr_hcr_info->sect_info.ptr_num_ext_sorted_cw_in_sect;
159
188k
  WORD32 num_ext_sorted_cw_in_sect_idx =
160
188k
      ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx;
161
188k
  UWORD8 *ptr_ext_sorted_cw = ptr_hcr_info->sect_info.ptr_ext_sorted_cw;
162
188k
  WORD32 ext_sorted_cw_idx = ptr_hcr_info->sect_info.ext_sorted_cw_idx;
163
188k
  UWORD16 *ptr_num_ext_sorted_sect_in_sets =
164
188k
      ptr_hcr_info->sect_info.ptr_num_ext_sorted_sect_in_sets;
165
188k
  WORD32 num_ext_sorted_sect_in_sets_idx =
166
188k
      ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx;
167
188k
  WORD32 quant_spec_coeff_idx = ptr_hcr_info->str_dec_io.quant_spec_coeff_idx;
168
188k
  const UWORD8 *ptr_cb_dimension_tbl =
169
188k
      ptr_hcr_info->table_info.ptr_cb_dimension_tbl;
170
188k
  WORD32 loop_idx = 0;
171
172
188k
  for (i = ptr_num_ext_sorted_sect_in_sets[num_ext_sorted_sect_in_sets_idx];
173
6.89M
       i != 0; i--) {
174
6.78M
    cb_dim = ptr_cb_dimension_tbl[ptr_ext_sorted_cw[ext_sorted_cw_idx]];
175
176
6.78M
    for (k = ptr_num_ext_sorted_cw_in_sect[num_ext_sorted_cw_in_sect_idx];
177
17.2M
         k != 0; k--) {
178
10.4M
      loop_idx++;
179
10.4M
      if (loop_idx > 256) {
180
28.2k
        return;
181
28.2k
      }
182
10.4M
      *ptr_cb++ = ptr_ext_sorted_cw[ext_sorted_cw_idx];
183
10.4M
      *res_ptr_idx++ = quant_spec_coeff_idx;
184
10.4M
      quant_spec_coeff_idx += cb_dim;
185
10.4M
      if (quant_spec_coeff_idx >= 1024) {
186
493
        return;
187
493
      }
188
10.4M
    }
189
6.76M
    num_ext_sorted_cw_in_sect_idx++;
190
6.76M
    ext_sorted_cw_idx++;
191
6.76M
    if (num_ext_sorted_cw_in_sect_idx >= (MAX_SFB_HCR + MAX_HCR_SETS) ||
192
6.70M
        ext_sorted_cw_idx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
193
50.7k
      return;
194
50.7k
    }
195
6.76M
  }
196
108k
  num_ext_sorted_sect_in_sets_idx++;
197
108k
  if (num_ext_sorted_cw_in_sect_idx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
198
0
    return;
199
0
  }
200
201
108k
  ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx =
202
108k
      num_ext_sorted_cw_in_sect_idx;
203
108k
  ptr_hcr_info->sect_info.ext_sorted_cw_idx = ext_sorted_cw_idx;
204
108k
  ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx =
205
108k
      num_ext_sorted_sect_in_sets_idx;
206
108k
  ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx =
207
108k
      num_ext_sorted_cw_in_sect_idx;
208
108k
  ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = quant_spec_coeff_idx;
209
108k
}
210
211
static VOID ixheaacd_calc_num_ext_sorted_sect_sets(
212
    UWORD32 num_segment, UWORD16 *ptr_num_ext_sorted_cw_in_sect,
213
    WORD32 num_ext_sorted_cw_in_sect_idx,
214
    UWORD16 *ptr_num_ext_sorted_sect_in_sets,
215
40.1k
    WORD32 num_ext_sorted_sect_in_sets_idx) {
216
40.1k
  UWORD16 counter = 0;
217
40.1k
  UWORD32 cw_sum = 0;
218
40.1k
  UWORD16 *ptr_num_ext_sort_cw_in_sect = ptr_num_ext_sorted_cw_in_sect;
219
40.1k
  UWORD16 *ptr_num_ext_sort_sect_in_sets = ptr_num_ext_sorted_sect_in_sets;
220
221
618k
  while (ptr_num_ext_sort_cw_in_sect[num_ext_sorted_cw_in_sect_idx] != 0) {
222
583k
    cw_sum += ptr_num_ext_sort_cw_in_sect[num_ext_sorted_cw_in_sect_idx];
223
583k
    num_ext_sorted_cw_in_sect_idx++;
224
583k
    if (num_ext_sorted_cw_in_sect_idx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
225
0
      return;
226
0
    }
227
583k
    if (cw_sum > num_segment) {
228
0
      return;
229
0
    }
230
583k
    counter++;
231
583k
    if (counter > 256) {
232
0
      return;
233
0
    }
234
583k
    if (cw_sum == num_segment) {
235
145k
      ptr_num_ext_sort_sect_in_sets[num_ext_sorted_sect_in_sets_idx] = counter;
236
145k
      num_ext_sorted_sect_in_sets_idx++;
237
145k
      if (num_ext_sorted_sect_in_sets_idx >= MAX_HCR_SETS) {
238
4.81k
        return;
239
4.81k
      }
240
140k
      counter = 0;
241
140k
      cw_sum = 0;
242
140k
    }
243
583k
  }
244
35.3k
  ptr_num_ext_sort_sect_in_sets[num_ext_sorted_sect_in_sets_idx] = counter;
245
35.3k
}
246
247
static VOID ixheaacd_validate_hcr_sideinfo(WORD8 cb, WORD32 num_line,
248
546k
                                           UWORD32 *error_word) {
249
546k
  if (cb < ZERO_HCB || cb >= MAX_CB_CHECK || cb == (ESC_HCB + 1)) {
250
0
    *error_word |= (ERROR_POS << 4);
251
0
  }
252
546k
  if (num_line < 0 || num_line > 1024) {
253
0
    *error_word |= (ERROR_POS << 5);
254
0
  }
255
546k
}
256
257
static VOID ixheaacd_validate_hcr_lengths(WORD8 longest_cw_len,
258
                                          WORD16 reordered_spec_data_len,
259
45.6k
                                          UWORD32 *error_word) {
260
45.6k
  if (reordered_spec_data_len < longest_cw_len) {
261
223
    *error_word |= (ERROR_POS << 8);
262
223
  }
263
45.6k
}
264
265
UWORD32 ixheaacd_huff_code_reorder_init(
266
    ia_hcr_info_struct *ptr_hcr_info,
267
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
268
45.6k
    ia_aac_dec_tables_struct *ptr_aac_tables, ia_bit_buf_struct *itt_bit_buff) {
269
45.6k
  ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
270
45.6k
  WORD16 *ptr_num_sect_lines;
271
45.6k
  UWORD8 *ptr_cb;
272
45.6k
  WORD16 num_sect;
273
45.6k
  WORD8 cb;
274
45.6k
  WORD32 num_line;
275
45.6k
  WORD32 i;
276
277
45.6k
  ptr_hcr_info->str_dec_io.reordered_spec_data_len =
278
45.6k
      ptr_aac_dec_channel_info->reorder_spect_data_len;
279
45.6k
  ptr_hcr_info->str_dec_io.longest_cw_len =
280
45.6k
      ptr_aac_dec_channel_info->longest_cw_len;
281
45.6k
  ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base =
282
45.6k
      ptr_aac_dec_channel_info->ptr_spec_coeff;
283
45.6k
  ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = 0;
284
45.6k
  ptr_hcr_info->str_dec_io.ptr_cb = ptr_aac_dec_channel_info->cb4_hcr_arr;
285
45.6k
  ptr_hcr_info->str_dec_io.ptr_num_line_in_sect =
286
45.6k
      ptr_aac_dec_channel_info->num_line_in_sec4_hcr_arr;
287
45.6k
  ptr_hcr_info->str_dec_io.num_sect = ptr_aac_dec_channel_info->number_sect;
288
45.6k
  ptr_hcr_info->str_dec_io.err_log = 0;
289
45.6k
  ptr_hcr_info->str_non_pcw_side_info.ptr_result_base =
290
45.6k
      ptr_aac_dec_channel_info->ptr_spec_coeff;
291
292
45.6k
  ptr_hcr_info->str_dec_io.bit_str_idx =
293
45.6k
      itt_bit_buff->size - itt_bit_buff->cnt_bits;
294
45.6k
  itt_bit_buff->byte_ptr = (UWORD8 *)ptr_aac_dec_channel_info->scratch_buf_ptr;
295
45.6k
  itt_bit_buff->ptr_start = (UWORD8 *)ptr_aac_dec_channel_info->scratch_buf_ptr;
296
297
45.6k
  if (ptr_aac_dec_channel_info->str_ics_info.window_sequence ==
298
45.6k
      EIGHT_SHORT_SEQUENCE) {
299
20.1k
    WORD16 band;
300
20.1k
    WORD16 max_band;
301
20.1k
    WORD8 group;
302
20.1k
    WORD8 win_group_len;
303
20.1k
    WORD8 window;
304
20.1k
    WORD8 num_unit_in_band;
305
20.1k
    WORD8 cnt_unit_in_band;
306
20.1k
    WORD8 grp_win;
307
20.1k
    WORD8 cb_prev;
308
309
20.1k
    WORD8 *ptr_code_book;
310
20.1k
    const WORD16 *band_offsets;
311
20.1k
    WORD16 num_groups;
312
313
20.1k
    ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
314
20.1k
    ptr_num_sect_lines = ptr_hcr_info->str_dec_io.ptr_num_line_in_sect;
315
20.1k
    ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
316
20.1k
    band_offsets = (WORD16 *)ixheaacd_getscalefactorbandoffsets(ptr_ics_info,
317
20.1k
                                                                ptr_aac_tables);
318
20.1k
    num_groups = ptr_ics_info->num_window_groups;
319
320
20.1k
    num_line = 0;
321
20.1k
    num_sect = 0;
322
20.1k
    cb = ptr_code_book[0];
323
20.1k
    cb_prev = ptr_code_book[0];
324
325
20.1k
    *ptr_cb++ = cb_prev;
326
327
20.1k
    max_band = ptr_ics_info->max_sfb;
328
184k
    for (band = 0; band < max_band; band++) {
329
164k
      num_unit_in_band = ((band_offsets[band + 1] - band_offsets[band]) >> 2);
330
478k
      for (cnt_unit_in_band = num_unit_in_band; cnt_unit_in_band != 0;
331
314k
           cnt_unit_in_band--) {
332
1.06M
        for (window = 0, group = 0; group < num_groups; group++) {
333
746k
          win_group_len = ptr_ics_info->window_group_length[group];
334
3.26M
          for (grp_win = win_group_len; grp_win != 0; grp_win--, window++) {
335
2.51M
            cb = ptr_code_book[group * 16 + band];
336
2.51M
            if (cb != cb_prev) {
337
526k
              ixheaacd_validate_hcr_sideinfo(cb, num_line,
338
526k
                                             &ptr_hcr_info->str_dec_io.err_log);
339
526k
              if (ptr_hcr_info->str_dec_io.err_log != 0) {
340
0
                return (ptr_hcr_info->str_dec_io.err_log);
341
0
              }
342
526k
              *ptr_cb++ = cb;
343
526k
              *ptr_num_sect_lines++ = num_line;
344
526k
              num_sect++;
345
346
526k
              cb_prev = cb;
347
526k
              num_line = LINES_PER_UNIT;
348
1.99M
            } else {
349
1.99M
              num_line += LINES_PER_UNIT;
350
1.99M
            }
351
2.51M
          }
352
746k
        }
353
314k
      }
354
164k
    }
355
356
20.1k
    num_sect++;
357
358
20.1k
    ixheaacd_validate_hcr_sideinfo(cb, num_line,
359
20.1k
                                   &ptr_hcr_info->str_dec_io.err_log);
360
20.1k
    if (num_sect <= 0 || num_sect > 1024 / 2) {
361
0
      ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 7);
362
0
    }
363
20.1k
    ixheaacd_validate_hcr_lengths(
364
20.1k
        ptr_hcr_info->str_dec_io.longest_cw_len,
365
20.1k
        ptr_hcr_info->str_dec_io.reordered_spec_data_len,
366
20.1k
        &ptr_hcr_info->str_dec_io.err_log);
367
20.1k
    if (ptr_hcr_info->str_dec_io.err_log != 0) {
368
57
      return (ptr_hcr_info->str_dec_io.err_log);
369
57
    }
370
371
20.0k
    *ptr_cb = cb;
372
20.0k
    *ptr_num_sect_lines = num_line;
373
20.0k
    ptr_hcr_info->str_dec_io.num_sect = num_sect;
374
375
25.5k
  } else {
376
25.5k
    ixheaacd_validate_hcr_lengths(
377
25.5k
        ptr_hcr_info->str_dec_io.longest_cw_len,
378
25.5k
        ptr_hcr_info->str_dec_io.reordered_spec_data_len,
379
25.5k
        &ptr_hcr_info->str_dec_io.err_log);
380
25.5k
    num_sect = ptr_hcr_info->str_dec_io.num_sect;
381
25.5k
    ptr_num_sect_lines = ptr_hcr_info->str_dec_io.ptr_num_line_in_sect;
382
25.5k
    ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
383
25.5k
    if (num_sect <= 0 || num_sect > 64) {
384
154
      ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 6);
385
154
      num_sect = 0;
386
154
    }
387
388
165k
    for (i = num_sect; i != 0; i--) {
389
139k
      cb = *ptr_cb++;
390
391
139k
      if (cb < ZERO_HCB || cb >= MAX_CB_CHECK || cb == (ESC_HCB + 1)) {
392
0
        ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 2);
393
0
      }
394
395
139k
      num_line = *ptr_num_sect_lines++;
396
397
139k
      if ((num_line <= 0) || (num_line > 1024)) {
398
140
        ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 3);
399
140
      }
400
139k
    }
401
25.5k
    if (ptr_hcr_info->str_dec_io.err_log != 0) {
402
330
      return (ptr_hcr_info->str_dec_io.err_log);
403
330
    }
404
25.5k
  }
405
406
45.2k
  ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
407
728k
  for (i = 0; i < num_sect; i++) {
408
683k
    if ((*ptr_cb == NOISE_HCB) || (*ptr_cb == INTENSITY_HCB2) ||
409
621k
        (*ptr_cb == INTENSITY_HCB)) {
410
102k
      *ptr_cb = 0;
411
102k
    }
412
683k
    ptr_cb++;
413
683k
  }
414
415
45.2k
  return (ptr_hcr_info->str_dec_io.err_log);
416
45.6k
}
417
418
45.2k
static VOID ixheaacd_huff_calc_num_cwd(ia_hcr_info_struct *ptr_hcr_info) {
419
45.2k
  WORD32 sect_idx;
420
45.2k
  UWORD32 num_code_word;
421
422
45.2k
  UWORD32 num_sect = ptr_hcr_info->str_dec_io.num_sect;
423
45.2k
  UWORD8 *ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
424
45.2k
  WORD16 *ptr_num_line_in_sect = ptr_hcr_info->str_dec_io.ptr_num_line_in_sect;
425
45.2k
  const UWORD8 *ptr_cb_dim_shift_tbl =
426
45.2k
      ptr_hcr_info->table_info.ptr_cb_dim_shift_tbl;
427
45.2k
  UWORD16 *ptr_num_cw_in_sect = ptr_hcr_info->sect_info.ptr_num_cw_in_sect;
428
429
45.2k
  num_code_word = 0;
430
728k
  for (sect_idx = num_sect; sect_idx != 0; sect_idx--) {
431
683k
    *ptr_num_cw_in_sect =
432
683k
        *ptr_num_line_in_sect++ >> ptr_cb_dim_shift_tbl[*ptr_cb];
433
683k
    if (*ptr_cb != 0) {
434
569k
      num_code_word += *ptr_num_cw_in_sect;
435
569k
    }
436
683k
    ptr_num_cw_in_sect++;
437
683k
    ptr_cb++;
438
683k
  }
439
45.2k
  ptr_hcr_info->sect_info.num_code_word = num_code_word;
440
45.2k
}
441
442
45.2k
static VOID ixheaacd_huff_sort_sect_cb_cwd(ia_hcr_info_struct *ptr_hcr_info) {
443
45.2k
  UWORD32 i, j, k;
444
45.2k
  UWORD8 temp;
445
45.2k
  UWORD32 counter;
446
45.2k
  UWORD32 start_offset;
447
45.2k
  UWORD32 num_zero_sect;
448
45.2k
  UWORD8 *ptr_dest;
449
45.2k
  UWORD32 num_sect_dec;
450
451
45.2k
  UWORD32 num_sect = ptr_hcr_info->str_dec_io.num_sect;
452
45.2k
  UWORD8 *ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
453
45.2k
  UWORD8 *ptr_sorted_cb = ptr_hcr_info->sect_info.ptr_sorted_cb;
454
45.2k
  UWORD16 *ptr_num_cw_in_sect = ptr_hcr_info->sect_info.ptr_num_cw_in_sect;
455
45.2k
  UWORD16 *ptr_num_sorted_cw_in_sect =
456
45.2k
      ptr_hcr_info->sect_info.ptr_num_sorted_cw_in_sect;
457
45.2k
  UWORD8 *ptr_cb_switch = ptr_hcr_info->sect_info.ptr_cb_switch;
458
45.2k
  UWORD16 *ptr_reorder_offset = ptr_hcr_info->sect_info.ptr_reorder_offset;
459
45.2k
  const UWORD8 *ptr_cb_priority = ptr_hcr_info->table_info.ptr_cb_priority;
460
45.2k
  const UWORD8 *ptr_min_cb_pair_tbl =
461
45.2k
      ptr_hcr_info->codebook_pairs.ptr_min_cb_pair_tbl;
462
45.2k
  const UWORD8 *ptr_max_cb_pair_tbl =
463
45.2k
      ptr_hcr_info->codebook_pairs.ptr_max_cb_pair_tbl;
464
45.2k
  const UWORD8 *ptr_cb_dim_shift_tbl =
465
45.2k
      ptr_hcr_info->table_info.ptr_cb_dim_shift_tbl;
466
467
45.2k
  UWORD32 search_start_idx = 0;
468
469
45.2k
  ptr_dest = ptr_sorted_cb;
470
45.2k
  num_zero_sect = 0;
471
728k
  for (i = num_sect; i != 0; i--) {
472
683k
    if (ptr_cb_priority[*ptr_cb] == 0) {
473
114k
      num_zero_sect += 1;
474
114k
    }
475
683k
    *ptr_dest++ = ptr_cb_priority[*ptr_cb++];
476
683k
  }
477
45.2k
  ptr_hcr_info->sect_info.num_sorted_section = num_sect - num_zero_sect;
478
45.2k
  ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
479
480
45.2k
  num_sect_dec = num_sect - 1;
481
45.2k
  if (num_sect_dec > 0) {
482
35.3k
    counter = num_sect_dec;
483
673k
    for (j = num_sect_dec; j != 0; j--) {
484
13.4M
      for (i = 0; i < counter; i++) {
485
12.8M
        if (ptr_sorted_cb[i + 1] > ptr_sorted_cb[i]) {
486
5.09M
          temp = ptr_sorted_cb[i];
487
5.09M
          ptr_sorted_cb[i] = ptr_sorted_cb[i + 1];
488
5.09M
          ptr_sorted_cb[i + 1] = temp;
489
5.09M
        }
490
12.8M
      }
491
638k
      counter -= 1;
492
638k
    }
493
35.3k
  }
494
495
728k
  for (i = num_sect; i != 0; i--) {
496
683k
    *ptr_cb_switch++ = 0;
497
683k
  }
498
45.2k
  ptr_cb_switch = ptr_hcr_info->sect_info.ptr_cb_switch;
499
500
728k
  for (j = 0; j < num_sect; j++) {
501
10.3M
    for (i = search_start_idx; i < num_sect; i++) {
502
10.3M
      if (ptr_cb_switch[i] == 0 &&
503
5.78M
          (ptr_min_cb_pair_tbl[ptr_sorted_cb[j]] == ptr_cb[i] ||
504
5.16M
           ptr_max_cb_pair_tbl[ptr_sorted_cb[j]] == ptr_cb[i])) {
505
683k
        ptr_cb_switch[i] = 1;
506
683k
        ptr_sorted_cb[j] = ptr_cb[i];
507
683k
        ptr_num_sorted_cw_in_sect[j] = ptr_num_cw_in_sect[i];
508
509
683k
        start_offset = 0;
510
13.4M
        for (k = 0; k < i; k++) {
511
12.8M
          start_offset += ptr_num_cw_in_sect[k]
512
12.8M
                          << ptr_cb_dim_shift_tbl[ptr_cb[k]];
513
12.8M
        }
514
683k
        ptr_reorder_offset[j] = start_offset;
515
516
683k
        if (i == search_start_idx) {
517
213k
          UWORD32 k = i;
518
896k
          while (ptr_cb_switch[k++] == 1) search_start_idx++;
519
213k
        }
520
683k
        break;
521
683k
      }
522
10.3M
    }
523
683k
  }
524
45.2k
}
525
526
41.0k
static VOID ixheaacd_huff_ext_sect_info(ia_hcr_info_struct *ptr_hcr_info) {
527
41.0k
  UWORD32 srt_sec_cnt = 0;
528
41.0k
  UWORD32 x_srt_sc_cnt = 0;
529
41.0k
  UWORD32 remain_num_cw_sort_sec;
530
41.0k
  UWORD32 in_segment_remain_num_cw;
531
532
41.0k
  UWORD32 num_sorted_section = ptr_hcr_info->sect_info.num_sorted_section;
533
41.0k
  UWORD8 *ptr_sorted_cb = ptr_hcr_info->sect_info.ptr_sorted_cb;
534
41.0k
  UWORD16 *ptr_num_sorted_cw_in_sect =
535
41.0k
      ptr_hcr_info->sect_info.ptr_num_sorted_cw_in_sect;
536
41.0k
  UWORD8 *ptr_extended_sorted_code_book =
537
41.0k
      ptr_hcr_info->sect_info.ptr_ext_sorted_cw;
538
41.0k
  UWORD16 *ptr_num_ext_sort_cw_sect =
539
41.0k
      ptr_hcr_info->sect_info.ptr_num_ext_sorted_cw_in_sect;
540
41.0k
  UWORD32 num_segment = ptr_hcr_info->str_segment_info.num_segment;
541
41.0k
  UWORD8 *ptr_ext_sorted_sect_max_cb_len =
542
41.0k
      ptr_hcr_info->sect_info.ptr_ext_sorted_sect_max_cb_len;
543
41.0k
  WORD8 longest_cw_len = ptr_hcr_info->str_dec_io.longest_cw_len;
544
41.0k
  const UWORD8 *ptr_max_cw_len_tbl =
545
41.0k
      ptr_hcr_info->table_info.ptr_max_cw_len_tbl;
546
547
41.0k
  remain_num_cw_sort_sec = ptr_num_sorted_cw_in_sect[srt_sec_cnt];
548
41.0k
  in_segment_remain_num_cw = num_segment;
549
550
1.07M
  while (srt_sec_cnt < num_sorted_section) {
551
1.03M
    if (in_segment_remain_num_cw < remain_num_cw_sort_sec) {
552
478k
      ptr_num_ext_sort_cw_sect[x_srt_sc_cnt] = in_segment_remain_num_cw;
553
478k
      ptr_extended_sorted_code_book[x_srt_sc_cnt] = ptr_sorted_cb[srt_sec_cnt];
554
555
478k
      remain_num_cw_sort_sec -= in_segment_remain_num_cw;
556
478k
      in_segment_remain_num_cw = num_segment;
557
555k
    } else if (in_segment_remain_num_cw == remain_num_cw_sort_sec) {
558
68.9k
      ptr_num_ext_sort_cw_sect[x_srt_sc_cnt] = in_segment_remain_num_cw;
559
68.9k
      ptr_extended_sorted_code_book[x_srt_sc_cnt] = ptr_sorted_cb[srt_sec_cnt];
560
561
68.9k
      srt_sec_cnt++;
562
68.9k
      remain_num_cw_sort_sec = ptr_num_sorted_cw_in_sect[srt_sec_cnt];
563
68.9k
      in_segment_remain_num_cw = num_segment;
564
486k
    } else {
565
486k
      ptr_num_ext_sort_cw_sect[x_srt_sc_cnt] = remain_num_cw_sort_sec;
566
486k
      ptr_extended_sorted_code_book[x_srt_sc_cnt] = ptr_sorted_cb[srt_sec_cnt];
567
568
486k
      in_segment_remain_num_cw -= remain_num_cw_sort_sec;
569
486k
      srt_sec_cnt++;
570
486k
      remain_num_cw_sort_sec = ptr_num_sorted_cw_in_sect[srt_sec_cnt];
571
486k
    }
572
1.03M
    ptr_ext_sorted_sect_max_cb_len[x_srt_sc_cnt] =
573
1.03M
        min(ptr_max_cw_len_tbl[ptr_extended_sorted_code_book[x_srt_sc_cnt]],
574
1.03M
            longest_cw_len);
575
576
1.03M
    x_srt_sc_cnt += 1;
577
578
1.03M
    if (x_srt_sc_cnt >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
579
869
      ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 28);
580
869
      return;
581
869
    }
582
1.03M
  }
583
40.1k
  ptr_num_ext_sort_cw_sect[x_srt_sc_cnt] = 0;
584
40.1k
}
585
586
static UWORD32 ixheaacd_hcr_prepare_segmentation_grid(
587
45.2k
    ia_hcr_info_struct *ptr_hcr_info) {
588
45.2k
  UWORD16 i, j;
589
45.2k
  UWORD16 num_segment = 0;
590
45.2k
  UWORD16 segment_start = 0;
591
45.2k
  UWORD8 segment_width;
592
45.2k
  UWORD8 last_segment_width;
593
45.2k
  UWORD8 sorted_code_book;
594
45.2k
  UWORD8 end_flag = 0;
595
45.2k
  UWORD16 intermediate_result;
596
597
45.2k
  WORD8 longest_cw_len = ptr_hcr_info->str_dec_io.longest_cw_len;
598
45.2k
  WORD16 reordered_spec_data_len =
599
45.2k
      ptr_hcr_info->str_dec_io.reordered_spec_data_len;
600
45.2k
  UWORD32 num_sorted_section = ptr_hcr_info->sect_info.num_sorted_section;
601
45.2k
  UWORD8 *ptr_sorted_cb = ptr_hcr_info->sect_info.ptr_sorted_cb;
602
45.2k
  UWORD16 *ptr_num_sorted_cw_in_sect =
603
45.2k
      ptr_hcr_info->sect_info.ptr_num_sorted_cw_in_sect;
604
45.2k
  UWORD16 *arr_seg_start_l = ptr_hcr_info->str_segment_info.arr_seg_start_l;
605
45.2k
  UWORD16 *arr_seg_start_r = ptr_hcr_info->str_segment_info.arr_seg_start_r;
606
45.2k
  WORD8 *p_remaining_bits_in_seg =
607
45.2k
      ptr_hcr_info->str_segment_info.p_remaining_bits_in_seg;
608
45.2k
  UWORD16 bit_str_idx = ptr_hcr_info->str_dec_io.bit_str_idx;
609
45.2k
  const UWORD8 *ptr_max_cw_len_tbl =
610
45.2k
      ptr_hcr_info->table_info.ptr_max_cw_len_tbl;
611
612
281k
  for (i = num_sorted_section; i != 0; i--) {
613
254k
    sorted_code_book = *ptr_sorted_cb++;
614
254k
    segment_width = min(ptr_max_cw_len_tbl[sorted_code_book], longest_cw_len);
615
616
7.46M
    for (j = *ptr_num_sorted_cw_in_sect; j != 0; j--) {
617
7.22M
      intermediate_result = bit_str_idx + segment_start;
618
7.22M
      if ((segment_start + segment_width) <= reordered_spec_data_len) {
619
7.20M
        *arr_seg_start_l++ = intermediate_result;
620
7.20M
        *arr_seg_start_r++ = intermediate_result + segment_width - 1;
621
7.20M
        *p_remaining_bits_in_seg++ = segment_width;
622
7.20M
        segment_start += segment_width;
623
7.20M
        num_segment += 1;
624
7.20M
      } else {
625
18.8k
        arr_seg_start_l--;
626
18.8k
        arr_seg_start_r--;
627
18.8k
        p_remaining_bits_in_seg--;
628
18.8k
        segment_start = *arr_seg_start_l - bit_str_idx;
629
630
18.8k
        last_segment_width = reordered_spec_data_len - segment_start;
631
18.8k
        *p_remaining_bits_in_seg = last_segment_width;
632
18.8k
        *arr_seg_start_r = bit_str_idx + segment_start + last_segment_width - 1;
633
18.8k
        end_flag = 1;
634
18.8k
        break;
635
18.8k
      }
636
7.22M
    }
637
254k
    ptr_num_sorted_cw_in_sect++;
638
254k
    if (end_flag != 0) {
639
18.8k
      break;
640
18.8k
    }
641
254k
  }
642
643
45.2k
  if (num_segment == 0) ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 9);
644
645
45.2k
  ptr_hcr_info->str_segment_info.num_segment = num_segment;
646
647
45.2k
  return (ptr_hcr_info->str_dec_io.err_log);
648
45.2k
}
649
650
static PLATFORM_INLINE UWORD16 *ixheaacd_huff_dec_pair_hcr_pcw(
651
    ia_hcr_info_struct *ptr_hcr_info, ia_bit_buf_struct *it_bit_buff,
652
    WORD no_bands, const UWORD16 *code_book_tbl, WORD32 *read_word,
653
    WORD32 tbl_sign, const UWORD32 *idx_table, UWORD16 *arr_seg_start_l,
654
    WORD32 *read_bits, WORD32 huff_mode, WORD8 *p_remaining_bits_in_seg,
655
    WORD32 *ptr_num_decoded_bits)
656
657
41.6k
{
658
41.6k
  WORD32 spec_index = ptr_hcr_info->str_dec_io.quant_spec_coeff_idx;
659
41.6k
  WORD32 *spec_coef =
660
41.6k
      ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base + spec_index;
661
41.6k
  WORD16 index, length;
662
41.6k
  WORD32 y, z;
663
41.6k
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
664
41.6k
  WORD32 *bit_pos = &it_bit_buff->bit_pos;
665
666
5.70M
  do {
667
5.70M
    UWORD32 read_word1;
668
669
5.70M
    WORD32 read_bit_offset =
670
5.70M
        *arr_seg_start_l - (it_bit_buff->size - *read_bits);
671
672
5.70M
    if (read_bit_offset) {
673
5.56M
      *read_bits -= read_bit_offset;
674
5.56M
      *bit_pos += read_bit_offset;
675
5.56M
      ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
676
5.56M
                                  it_bit_buff->ptr_bit_buf_end);
677
5.56M
    }
678
5.70M
    *bit_pos = max(0, *bit_pos);
679
5.70M
    read_word1 = *read_word << *bit_pos;
680
5.70M
    ixheaacd_huffman_decode(read_word1, &index, &length, code_book_tbl,
681
5.70M
                            idx_table);
682
5.70M
    *bit_pos += length;
683
5.70M
    *ptr_num_decoded_bits += length;
684
5.70M
    *p_remaining_bits_in_seg -= length;
685
5.70M
    *arr_seg_start_l += length;
686
5.70M
    *read_bits -= length;
687
5.70M
    ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
688
5.70M
                                it_bit_buff->ptr_bit_buf_end);
689
5.70M
    if (tbl_sign) {
690
78.1k
      WORD32 temp_word;
691
78.1k
      temp_word = *read_word << *bit_pos;
692
78.1k
      y = index / huff_mode;
693
78.1k
      z = index - huff_mode * y;
694
695
78.1k
      if (y) {
696
34.3k
        if (temp_word & 0x80000000) y = -y;
697
698
34.3k
        temp_word = temp_word << 1;
699
34.3k
        *bit_pos += 1;
700
34.3k
        *p_remaining_bits_in_seg -= 1;
701
34.3k
        *ptr_num_decoded_bits += 1;
702
34.3k
        *arr_seg_start_l += 1;
703
34.3k
        *read_bits -= 1;
704
34.3k
      }
705
78.1k
      *spec_coef++ = y;
706
78.1k
      spec_index++;
707
708
78.1k
      if (z) {
709
35.8k
        if (temp_word & 0x80000000) {
710
19.5k
          z = -z;
711
19.5k
        }
712
35.8k
        temp_word <<= 1;
713
35.8k
        *bit_pos += 1;
714
35.8k
        *p_remaining_bits_in_seg -= 1;
715
35.8k
        *ptr_num_decoded_bits += 1;
716
35.8k
        *arr_seg_start_l += 1;
717
35.8k
        *read_bits -= 1;
718
35.8k
      }
719
78.1k
      *spec_coef++ = z;
720
78.1k
      spec_index++;
721
5.62M
    } else {
722
5.62M
      y = (index / huff_mode) - 4;
723
5.62M
      z = index - ((y + 4) * huff_mode) - 4;
724
725
5.62M
      *spec_coef++ = y;
726
5.62M
      *spec_coef++ = z;
727
5.62M
      spec_index += 2;
728
5.62M
    }
729
5.70M
    ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
730
5.70M
                                it_bit_buff->ptr_bit_buf_end);
731
5.70M
    no_bands--;
732
5.70M
    arr_seg_start_l++;
733
5.70M
    p_remaining_bits_in_seg++;
734
5.70M
  } while (no_bands != 0);
735
736
41.6k
  it_bit_buff->ptr_read_next = ptr_read_next;
737
41.6k
  ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = spec_index;
738
739
41.6k
  return arr_seg_start_l;
740
41.6k
}
741
742
static PLATFORM_INLINE WORD16 ixheaacd_huff_dec_pair_hcr_non_pcw(
743
    ia_bit_buf_struct *itt_bit_buff, WORD32 *spec_coef,
744
    const UWORD16 *code_book_tbl, WORD32 tbl_sign, const UWORD32 *idx_table,
745
    WORD32 huff_mode)
746
747
271k
{
748
271k
  WORD16 index, length;
749
271k
  WORD32 y, z;
750
271k
  WORD32 read_word1;
751
271k
  WORD32 read_word;
752
753
271k
  read_word = ixheaacd_aac_showbits_32(itt_bit_buff->byte_ptr,
754
271k
                                       itt_bit_buff->bit_count, NULL);
755
756
271k
  ixheaacd_huffman_decode(read_word, &index, &length, code_book_tbl, idx_table);
757
271k
  read_word1 = read_word << length;
758
271k
  if (tbl_sign) {
759
150k
    WORD32 temp_word;
760
150k
    temp_word = read_word1;
761
150k
    y = index / huff_mode;
762
150k
    z = index - huff_mode * y;
763
764
150k
    if (y) {
765
63.5k
      if (temp_word & 0x80000000) y = -y;
766
767
63.5k
      temp_word = temp_word << 1;
768
63.5k
      length++;
769
63.5k
    }
770
150k
    *spec_coef++ = y;
771
772
150k
    if (z) {
773
53.4k
      if (temp_word & 0x80000000) {
774
14.7k
        z = -z;
775
14.7k
      }
776
53.4k
      temp_word <<= 1;
777
53.4k
      length++;
778
53.4k
    }
779
150k
    *spec_coef++ = z;
780
150k
  } else {
781
121k
    y = (index / huff_mode) - 4;
782
121k
    z = index - ((y + 4) * huff_mode) - 4;
783
784
121k
    *spec_coef++ = y;
785
121k
    *spec_coef++ = z;
786
121k
  }
787
788
271k
  return length;
789
271k
}
790
791
static PLATFORM_INLINE UWORD16 *ixheaacd_huff_dec_quad_hcr_pcw(
792
    ia_hcr_info_struct *ptr_hcr_info, ia_bit_buf_struct *it_bit_buff,
793
    WORD no_bands, const UWORD16 *code_book_tbl, WORD32 tbl_sign,
794
    const UWORD32 *idx_table, WORD32 *read_word, WORD32 *read_bits,
795
    UWORD16 *arr_seg_start_l, WORD8 *p_remaining_bits_in_seg,
796
16.0k
    WORD32 *ptr_num_decoded_bits) {
797
16.0k
  WORD16 index, length;
798
799
16.0k
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
800
16.0k
  WORD32 spec_index = ptr_hcr_info->str_dec_io.quant_spec_coeff_idx;
801
16.0k
  WORD32 *spec_coef =
802
16.0k
      ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base + spec_index;
803
16.0k
  WORD32 *bit_pos = &it_bit_buff->bit_pos;
804
805
59.3k
  do {
806
59.3k
    UWORD32 read_word1;
807
808
59.3k
    WORD32 read_bit_offset =
809
59.3k
        *arr_seg_start_l - (it_bit_buff->size - *read_bits);
810
811
59.3k
    if (read_bit_offset) {
812
51.0k
      *read_bits -= read_bit_offset;
813
51.0k
      *bit_pos += read_bit_offset;
814
51.0k
      ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
815
51.0k
                                  it_bit_buff->ptr_bit_buf_end);
816
51.0k
    }
817
59.3k
    *bit_pos = max(0, *bit_pos);
818
59.3k
    read_word1 = *read_word << *bit_pos;
819
59.3k
    ixheaacd_huffman_decode(read_word1, &index, &length, code_book_tbl,
820
59.3k
                            idx_table);
821
59.3k
    *bit_pos += length;
822
59.3k
    *p_remaining_bits_in_seg -= length;
823
59.3k
    *read_bits -= length;
824
59.3k
    *ptr_num_decoded_bits += length;
825
59.3k
    *arr_seg_start_l += length;
826
59.3k
    ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
827
59.3k
                                it_bit_buff->ptr_bit_buf_end);
828
59.3k
    if (tbl_sign) {
829
23.5k
      WORD32 temp_word;
830
23.5k
      WORD32 w, x, y, z;
831
23.5k
      temp_word = *read_word << *bit_pos;
832
23.5k
      w = index / 27;
833
23.5k
      index = index - w * 27;
834
23.5k
      x = index / 9;
835
23.5k
      index = index - x * 9;
836
23.5k
      y = index / 3;
837
23.5k
      z = index - y * 3;
838
23.5k
      if (w) {
839
14.8k
        if (temp_word & 0x80000000) w = -w;
840
14.8k
        temp_word <<= 1;
841
14.8k
        *bit_pos += 1;
842
14.8k
        *p_remaining_bits_in_seg -= 1;
843
14.8k
        *read_bits -= 1;
844
14.8k
        *ptr_num_decoded_bits += 1;
845
14.8k
        *arr_seg_start_l += 1;
846
14.8k
      }
847
23.5k
      *spec_coef++ = w;
848
23.5k
      spec_index++;
849
850
23.5k
      if (x) {
851
12.5k
        if (temp_word & 0x80000000) x = -x;
852
12.5k
        temp_word <<= 1;
853
12.5k
        *bit_pos += 1;
854
12.5k
        *p_remaining_bits_in_seg -= 1;
855
12.5k
        *read_bits -= 1;
856
12.5k
        *ptr_num_decoded_bits += 1;
857
12.5k
        *arr_seg_start_l += 1;
858
12.5k
      }
859
23.5k
      *spec_coef++ = x;
860
23.5k
      spec_index++;
861
23.5k
      if (y) {
862
12.7k
        if (temp_word & 0x80000000) y = -y;
863
12.7k
        temp_word <<= 1;
864
12.7k
        *bit_pos += 1;
865
12.7k
        *p_remaining_bits_in_seg -= 1;
866
12.7k
        *read_bits -= 1;
867
12.7k
        *ptr_num_decoded_bits += 1;
868
12.7k
        *arr_seg_start_l += 1;
869
12.7k
      }
870
23.5k
      *spec_coef++ = y;
871
23.5k
      spec_index++;
872
23.5k
      if (z) {
873
14.9k
        if (temp_word & 0x80000000) z = -z;
874
14.9k
        temp_word <<= 1;
875
14.9k
        *bit_pos += 1;
876
14.9k
        *p_remaining_bits_in_seg -= 1;
877
14.9k
        *read_bits -= 1;
878
14.9k
        *ptr_num_decoded_bits += 1;
879
14.9k
        *arr_seg_start_l += 1;
880
14.9k
      }
881
23.5k
      *spec_coef++ = z;
882
23.5k
      spec_index++;
883
884
23.5k
    }
885
886
35.8k
    else {
887
35.8k
      WORD32 w, x, y, z;
888
889
35.8k
      w = index / 27 - 1;
890
35.8k
      index = index - (w + 1) * 27;
891
35.8k
      x = index / 9 - 1;
892
35.8k
      index = index - (x + 1) * 9;
893
35.8k
      y = index / 3 - 1;
894
35.8k
      z = index - ((y + 1) * 3) - 1;
895
35.8k
      *spec_coef++ = w;
896
897
35.8k
      *spec_coef++ = x;
898
899
35.8k
      *spec_coef++ = y;
900
901
35.8k
      *spec_coef++ = z;
902
35.8k
      spec_index += 4;
903
35.8k
    }
904
905
59.3k
    ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
906
59.3k
                                it_bit_buff->ptr_bit_buf_end);
907
59.3k
    arr_seg_start_l++;
908
59.3k
    p_remaining_bits_in_seg++;
909
59.3k
    no_bands--;
910
59.3k
  } while (no_bands != 0);
911
912
16.0k
  it_bit_buff->ptr_read_next = ptr_read_next;
913
16.0k
  ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = spec_index;
914
915
16.0k
  return arr_seg_start_l;
916
16.0k
}
917
918
static UWORD16 *ixheaacd_huff_dec_word_hcr_pcw(
919
    ia_hcr_info_struct *ptr_hcr_info, ia_bit_buf_struct *it_bit_buff,
920
    WORD no_bands, const UWORD16 *code_book_tbl, WORD32 *read_word,
921
    const UWORD32 *idx_table, UWORD16 *arr_seg_start_l, WORD32 *read_bits,
922
193k
    WORD8 *p_remaining_bits_in_seg, WORD32 *ptr_num_decoded_bits) {
923
193k
  WORD32 sp1, sp2;
924
193k
  WORD32 flush_cw;
925
193k
  WORD32 i, value, norm_val, off;
926
193k
  WORD32 out1, out2;
927
193k
  WORD16 index;
928
193k
  WORD32 length;
929
193k
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
930
193k
  WORD32 spec_index = ptr_hcr_info->str_dec_io.quant_spec_coeff_idx;
931
193k
  WORD32 *spec_coef =
932
193k
      ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base + spec_index;
933
193k
  WORD32 *bit_pos = &it_bit_buff->bit_pos;
934
935
1.44M
  do {
936
1.44M
    UWORD32 read_word1;
937
938
1.44M
    WORD32 read_bit_offset =
939
1.44M
        *arr_seg_start_l - (it_bit_buff->size - *read_bits);
940
941
1.44M
    if (read_bit_offset) {
942
1.39M
      *read_bits -= read_bit_offset;
943
1.39M
      *bit_pos += read_bit_offset;
944
1.39M
      ixheaacd_aac_read_byte_corr1(&ptr_read_next, bit_pos, read_word,
945
1.39M
                                   it_bit_buff->ptr_bit_buf_end);
946
1.39M
    }
947
1.44M
    *bit_pos = max(0, *bit_pos);
948
1.44M
    read_word1 = *read_word << *bit_pos;
949
1.44M
    ixheaacd_huff_sfb_table(read_word1, &index, &length, code_book_tbl,
950
1.44M
                            idx_table);
951
1.44M
    *bit_pos += length;
952
1.44M
    *read_bits -= length;
953
1.44M
    *arr_seg_start_l += length;
954
1.44M
    *p_remaining_bits_in_seg -= length;
955
1.44M
    *ptr_num_decoded_bits += length;
956
1.44M
    ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
957
1.44M
                                it_bit_buff->ptr_bit_buf_end);
958
959
1.44M
    out1 = index / 17;
960
1.44M
    out2 = index - out1 * 17;
961
1.44M
    flush_cw = *read_word << *bit_pos;
962
963
1.44M
    sp1 = out1;
964
1.44M
    sp2 = out2;
965
966
1.44M
    if (out1) {
967
1.14M
      if (flush_cw & 0x80000000) {
968
619k
        out1 = -out1;
969
619k
      }
970
1.14M
      *bit_pos += 1;
971
1.14M
      *read_bits -= 1;
972
1.14M
      *p_remaining_bits_in_seg -= 1;
973
1.14M
      *ptr_num_decoded_bits += 1;
974
1.14M
      *arr_seg_start_l += 1;
975
1.14M
      flush_cw = (WORD32)flush_cw << 1;
976
1.14M
    }
977
978
1.44M
    if (out2) {
979
1.16M
      *bit_pos += 1;
980
1.16M
      *read_bits -= 1;
981
1.16M
      *p_remaining_bits_in_seg -= 1;
982
1.16M
      *ptr_num_decoded_bits += 1;
983
1.16M
      *arr_seg_start_l += 1;
984
1.16M
      if (flush_cw & 0x80000000) {
985
508k
        out2 = -out2;
986
508k
      }
987
1.16M
    }
988
989
1.44M
    ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
990
1.44M
                                it_bit_buff->ptr_bit_buf_end);
991
992
1.44M
    if (sp1 == 16) {
993
163k
      i = 4;
994
163k
      value = ixheaac_extu(*read_word, *bit_pos, 23);
995
163k
      value = value | 0xfffffe00;
996
163k
      norm_val = ixheaac_norm32(value);
997
998
163k
      i += (norm_val - 22);
999
163k
      *bit_pos += (norm_val - 21);
1000
163k
      *p_remaining_bits_in_seg -= (norm_val - 21);
1001
163k
      *ptr_num_decoded_bits += (norm_val - 21);
1002
163k
      *read_bits -= (norm_val - 21);
1003
163k
      *arr_seg_start_l += (norm_val - 21);
1004
1005
163k
      ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1006
163k
                                  it_bit_buff->ptr_bit_buf_end);
1007
1008
163k
      off = ixheaac_extu(*read_word, *bit_pos, 32 - i);
1009
1010
163k
      *bit_pos += i;
1011
163k
      *p_remaining_bits_in_seg -= i;
1012
163k
      *ptr_num_decoded_bits += i;
1013
163k
      *read_bits -= i;
1014
163k
      *arr_seg_start_l += i;
1015
1016
163k
      ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1017
163k
                                  it_bit_buff->ptr_bit_buf_end);
1018
163k
      ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1019
163k
                                  it_bit_buff->ptr_bit_buf_end);
1020
1021
163k
      i = off + ((WORD32)1 << i);
1022
1023
163k
      if (out1 < 0)
1024
97.7k
        *spec_coef++ = -i;
1025
65.3k
      else
1026
65.3k
        *spec_coef++ = i;
1027
163k
      spec_index++;
1028
1.28M
    } else {
1029
1.28M
      *spec_coef++ = out1;
1030
1.28M
      spec_index++;
1031
1.28M
    }
1032
1033
1.44M
    if (sp2 == 16) {
1034
93.8k
      i = 4;
1035
93.8k
      value = ixheaac_extu(*read_word, *bit_pos, 23);
1036
93.8k
      value = value | 0xfffffe00;
1037
93.8k
      norm_val = ixheaac_norm32(value);
1038
1039
93.8k
      i += (norm_val - 22);
1040
1041
93.8k
      *bit_pos += (norm_val - 21);
1042
93.8k
      *read_bits -= (norm_val - 21);
1043
93.8k
      *p_remaining_bits_in_seg -= (norm_val - 21);
1044
93.8k
      *ptr_num_decoded_bits += (norm_val - 21);
1045
93.8k
      *arr_seg_start_l += (norm_val - 21);
1046
93.8k
      ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1047
93.8k
                                  it_bit_buff->ptr_bit_buf_end);
1048
1049
93.8k
      off = ixheaac_extu(*read_word, *bit_pos, 32 - i);
1050
1051
93.8k
      *bit_pos += i;
1052
93.8k
      *p_remaining_bits_in_seg -= i;
1053
93.8k
      *ptr_num_decoded_bits += i;
1054
93.8k
      *read_bits -= i;
1055
93.8k
      *arr_seg_start_l += i;
1056
1057
93.8k
      ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1058
93.8k
                                  it_bit_buff->ptr_bit_buf_end);
1059
93.8k
      ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1060
93.8k
                                  it_bit_buff->ptr_bit_buf_end);
1061
1062
93.8k
      i = off + ((WORD32)1 << i);
1063
1064
93.8k
      if (out2 < 0)
1065
34.5k
        *spec_coef++ = -i;
1066
59.3k
      else
1067
59.3k
        *spec_coef++ = i;
1068
93.8k
      spec_index++;
1069
1.35M
    } else {
1070
1.35M
      *spec_coef++ = out2;
1071
1.35M
      spec_index++;
1072
1.35M
    }
1073
1074
1.44M
    arr_seg_start_l++;
1075
1.44M
    p_remaining_bits_in_seg++;
1076
1077
1.44M
    no_bands--;
1078
1.44M
  } while (no_bands != 0);
1079
1080
193k
  it_bit_buff->ptr_read_next = ptr_read_next;
1081
193k
  ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = spec_index;
1082
1083
193k
  return arr_seg_start_l;
1084
193k
}
1085
1086
static VOID ixheaacd_decode_pcw(ia_bit_buf_struct *itt_bit_buff,
1087
                                ia_hcr_info_struct *ptr_hcr_info,
1088
40.1k
                                ia_aac_dec_tables_struct *ptr_aac_tables) {
1089
40.1k
  UWORD16 ext_sort_sec;
1090
40.1k
  UWORD16 cur_ext_sort_cw_sec;
1091
40.1k
  UWORD8 codebook;
1092
40.1k
  UWORD8 dimension;
1093
40.1k
  WORD32 increment;
1094
1095
40.1k
  WORD32 num_ext_sorted_cw_in_sect_idx =
1096
40.1k
      ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx;
1097
40.1k
  UWORD8 *ptr_ext_sorted_cw = ptr_hcr_info->sect_info.ptr_ext_sorted_cw;
1098
40.1k
  WORD32 ext_sorted_cw_idx = ptr_hcr_info->sect_info.ext_sorted_cw_idx;
1099
40.1k
  UWORD16 *ptr_num_ext_sorted_sect_in_sets =
1100
40.1k
      ptr_hcr_info->sect_info.ptr_num_ext_sorted_sect_in_sets;
1101
40.1k
  WORD32 num_ext_sorted_sect_in_sets_idx =
1102
40.1k
      ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx;
1103
40.1k
  WORD32 *ptr_quant_spec_coeff =
1104
40.1k
      ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base;
1105
40.1k
  UWORD16 *arr_seg_start_l = ptr_hcr_info->str_segment_info.arr_seg_start_l;
1106
40.1k
  WORD8 *p_remaining_bits_in_seg =
1107
40.1k
      ptr_hcr_info->str_segment_info.p_remaining_bits_in_seg;
1108
40.1k
  UWORD8 *ptr_ext_sorted_sect_max_cb_len =
1109
40.1k
      ptr_hcr_info->sect_info.ptr_ext_sorted_sect_max_cb_len;
1110
40.1k
  WORD32 ext_sorted_sect_max_cb_len_idx =
1111
40.1k
      ptr_hcr_info->sect_info.ext_sorted_sect_max_cb_len_idx;
1112
40.1k
  UWORD8 max_allowed_cw_len;
1113
40.1k
  WORD32 num_decoded_bits;
1114
40.1k
  const UWORD8 *ptr_cb_dimension_tbl =
1115
40.1k
      ptr_hcr_info->table_info.ptr_cb_dimension_tbl;
1116
1117
40.1k
  WORD32 read_word = ixheaacd_aac_showbits_32(
1118
40.1k
      itt_bit_buff->ptr_read_next, itt_bit_buff->cnt_bits, &increment);
1119
40.1k
  WORD32 read_bits = itt_bit_buff->cnt_bits;
1120
1121
40.1k
  itt_bit_buff->ptr_read_next += increment;
1122
1123
40.1k
  for (ext_sort_sec =
1124
40.1k
           ptr_num_ext_sorted_sect_in_sets[num_ext_sorted_sect_in_sets_idx];
1125
291k
       ext_sort_sec != 0; ext_sort_sec--) {
1126
251k
    codebook = ptr_ext_sorted_cw[ext_sorted_cw_idx];
1127
251k
    if (codebook <= 0) return;
1128
1129
251k
    ext_sorted_cw_idx++;
1130
251k
    if (ext_sorted_cw_idx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
1131
0
      return;
1132
0
    }
1133
251k
    dimension = ptr_cb_dimension_tbl[codebook];
1134
251k
    max_allowed_cw_len =
1135
251k
        ptr_ext_sorted_sect_max_cb_len[ext_sorted_sect_max_cb_len_idx];
1136
251k
    ext_sorted_sect_max_cb_len_idx++;
1137
251k
    if (ext_sorted_sect_max_cb_len_idx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
1138
0
      return;
1139
0
    }
1140
1141
251k
    if (codebook <= 4) {
1142
16.0k
      WORD32 tbl_sign = 0;
1143
16.0k
      const UWORD16 *cb_table =
1144
16.0k
          (UWORD16 *)(ptr_aac_tables->code_book[codebook]);
1145
16.0k
      const UWORD32 *idx_table =
1146
16.0k
          (UWORD32 *)(ptr_aac_tables->index_table[codebook]);
1147
1148
16.0k
      if (codebook > 2) {
1149
5.29k
        tbl_sign = 1;
1150
5.29k
      }
1151
1152
16.0k
      {
1153
16.0k
        num_decoded_bits = 0;
1154
16.0k
        cur_ext_sort_cw_sec =
1155
16.0k
            ptr_hcr_info->sect_info
1156
16.0k
                .ptr_num_ext_sorted_cw_in_sect[num_ext_sorted_cw_in_sect_idx];
1157
1158
16.0k
        arr_seg_start_l = ixheaacd_huff_dec_quad_hcr_pcw(
1159
16.0k
            ptr_hcr_info, itt_bit_buff, cur_ext_sort_cw_sec, cb_table, tbl_sign,
1160
16.0k
            idx_table, &read_word, &read_bits, arr_seg_start_l,
1161
16.0k
            p_remaining_bits_in_seg, &num_decoded_bits);
1162
1163
16.0k
        p_remaining_bits_in_seg += cur_ext_sort_cw_sec;
1164
1165
16.0k
        if (cur_ext_sort_cw_sec * max_allowed_cw_len < num_decoded_bits) {
1166
6.26k
          ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 19);
1167
6.26k
        }
1168
1169
16.0k
        if (1 ==
1170
16.0k
            ixheaacd_err_detect_pcw_segment(
1171
16.0k
                *p_remaining_bits_in_seg, ptr_hcr_info, PCW,
1172
16.0k
                ptr_quant_spec_coeff +
1173
16.0k
                    ptr_hcr_info->str_dec_io.quant_spec_coeff_idx - dimension,
1174
16.0k
                dimension)) {
1175
0
          return;
1176
0
        }
1177
16.0k
      }
1178
235k
    } else if (codebook < 11) {
1179
41.6k
      {
1180
41.6k
        WORD32 tbl_sign = 0;
1181
41.6k
        WORD32 huff_mode = 9;
1182
41.6k
        const UWORD16 *cb_table =
1183
41.6k
            (UWORD16 *)(ptr_aac_tables->code_book[codebook]);
1184
41.6k
        const UWORD32 *idx_table =
1185
41.6k
            (UWORD32 *)(ptr_aac_tables->index_table[codebook]);
1186
41.6k
        num_decoded_bits = 0;
1187
1188
41.6k
        if (codebook > 6) {
1189
22.4k
          if (codebook > 8)
1190
17.8k
            huff_mode = 13;
1191
4.59k
          else
1192
4.59k
            huff_mode = 8;
1193
22.4k
          tbl_sign = 1;
1194
22.4k
        }
1195
1196
41.6k
        cur_ext_sort_cw_sec =
1197
41.6k
            ptr_hcr_info->sect_info
1198
41.6k
                .ptr_num_ext_sorted_cw_in_sect[num_ext_sorted_cw_in_sect_idx];
1199
1200
41.6k
        arr_seg_start_l = ixheaacd_huff_dec_pair_hcr_pcw(
1201
41.6k
            ptr_hcr_info, itt_bit_buff, cur_ext_sort_cw_sec, cb_table,
1202
41.6k
            &read_word, tbl_sign, idx_table, arr_seg_start_l, &read_bits,
1203
41.6k
            huff_mode, p_remaining_bits_in_seg, &num_decoded_bits);
1204
1205
41.6k
        p_remaining_bits_in_seg += cur_ext_sort_cw_sec;
1206
1207
41.6k
        if (cur_ext_sort_cw_sec * max_allowed_cw_len < num_decoded_bits) {
1208
18.5k
          ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 18);
1209
18.5k
        }
1210
1211
41.6k
        if (1 ==
1212
41.6k
            ixheaacd_err_detect_pcw_segment(
1213
41.6k
                *p_remaining_bits_in_seg, ptr_hcr_info, PCW_SIGN,
1214
41.6k
                ptr_quant_spec_coeff +
1215
41.6k
                    ptr_hcr_info->str_dec_io.quant_spec_coeff_idx - dimension,
1216
41.6k
                dimension)) {
1217
0
          return;
1218
0
        }
1219
41.6k
      }
1220
193k
    } else if ((codebook >= 11)) {
1221
193k
      const UWORD32 *idx_table =
1222
193k
          ptr_aac_tables->pstr_huffmann_tables->idx_table_hf11;
1223
193k
      const UWORD16 *cb_table =
1224
193k
          ptr_aac_tables->pstr_huffmann_tables->input_table_cb11;
1225
193k
      num_decoded_bits = 0;
1226
1227
193k
      cur_ext_sort_cw_sec =
1228
193k
          ptr_hcr_info->sect_info
1229
193k
              .ptr_num_ext_sorted_cw_in_sect[num_ext_sorted_cw_in_sect_idx];
1230
1231
193k
      arr_seg_start_l = ixheaacd_huff_dec_word_hcr_pcw(
1232
193k
          ptr_hcr_info, itt_bit_buff, cur_ext_sort_cw_sec, cb_table, &read_word,
1233
193k
          idx_table, arr_seg_start_l, &read_bits, p_remaining_bits_in_seg,
1234
193k
          &num_decoded_bits);
1235
1236
193k
      p_remaining_bits_in_seg += cur_ext_sort_cw_sec;
1237
1238
193k
      if (cur_ext_sort_cw_sec * max_allowed_cw_len < num_decoded_bits) {
1239
100k
        ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 17);
1240
100k
      }
1241
1242
193k
      if (1 == ixheaacd_err_detect_pcw_segment(
1243
193k
                   *p_remaining_bits_in_seg, ptr_hcr_info, PCW_ESC_SIGN,
1244
193k
                   ptr_quant_spec_coeff +
1245
193k
                       ptr_hcr_info->str_dec_io.quant_spec_coeff_idx - 2,
1246
193k
                   2)) {
1247
0
        return;
1248
0
      }
1249
193k
    }
1250
1251
251k
    num_ext_sorted_cw_in_sect_idx++;
1252
251k
    if (num_ext_sorted_cw_in_sect_idx >= MAX_SFB_HCR + MAX_HCR_SETS) {
1253
0
      return;
1254
0
    }
1255
251k
  }
1256
1257
40.1k
  num_ext_sorted_sect_in_sets_idx++;
1258
40.1k
  if (num_ext_sorted_sect_in_sets_idx >= MAX_HCR_SETS) {
1259
0
    return;
1260
0
  }
1261
1262
40.1k
  itt_bit_buff->cnt_bits = read_bits;
1263
1264
40.1k
  ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx =
1265
40.1k
      num_ext_sorted_cw_in_sect_idx;
1266
40.1k
  ptr_hcr_info->sect_info.ext_sorted_cw_idx = ext_sorted_cw_idx;
1267
40.1k
  ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx =
1268
40.1k
      num_ext_sorted_sect_in_sets_idx;
1269
40.1k
  ptr_hcr_info->sect_info.ext_sorted_sect_max_cb_len_idx =
1270
40.1k
      ext_sorted_sect_max_cb_len_idx;
1271
40.1k
}
1272
1273
static UWORD32 ixheaacd_init_segment_bit_field(WORD32 num_segment,
1274
21.7k
                                               WORD8 *p_remaining_bits_in_seg) {
1275
21.7k
  WORD16 i;
1276
21.7k
  WORD16 num_valid_segment = 0;
1277
1278
1.55M
  for (i = 0; i < num_segment; i++) {
1279
1.52M
    if (p_remaining_bits_in_seg[i] != 0) {
1280
1.45M
      num_valid_segment += 1;
1281
1.45M
    }
1282
1.52M
  }
1283
1284
21.7k
  return num_valid_segment;
1285
21.7k
}
1286
1287
187k
UWORD8 ixheaacd_toggle_read_dir(UWORD8 read_direction) {
1288
187k
  if (read_direction == FROM_LEFT_TO_RIGHT) {
1289
87.4k
    return FROM_RIGHT_TO_LEFT;
1290
100k
  } else {
1291
100k
    return FROM_LEFT_TO_RIGHT;
1292
100k
  }
1293
187k
}
1294
1295
static PLATFORM_INLINE UWORD16 ixheaacd_huff_dec_quad_hcr_non_pcw(
1296
    ia_bit_buf_struct *itt_bit_buff, WORD32 *spec_coef,
1297
154k
    const UWORD16 *code_book_tbl, WORD32 tbl_sign, const UWORD32 *idx_table) {
1298
154k
  WORD16 index, length;
1299
154k
  WORD16 cw_len;
1300
154k
  WORD32 read_word;
1301
1302
154k
  read_word = ixheaacd_aac_showbits_32(itt_bit_buff->byte_ptr,
1303
154k
                                       itt_bit_buff->bit_count, NULL);
1304
154k
  ixheaacd_huffman_decode(read_word, &index, &length, code_book_tbl, idx_table);
1305
154k
  cw_len = length;
1306
154k
  if (tbl_sign) {
1307
103k
    WORD32 temp_word;
1308
103k
    WORD32 w, x, y, z;
1309
103k
    temp_word = read_word << length;
1310
103k
    w = index / 27;
1311
103k
    index = index - w * 27;
1312
103k
    x = index / 9;
1313
103k
    index = index - x * 9;
1314
103k
    y = index / 3;
1315
103k
    z = index - y * 3;
1316
103k
    if (w) {
1317
70.6k
      if (temp_word & 0x80000000) w = -w;
1318
70.6k
      temp_word <<= 1;
1319
70.6k
      cw_len++;
1320
70.6k
    }
1321
103k
    *spec_coef++ = w;
1322
1323
103k
    if (x) {
1324
64.6k
      if (temp_word & 0x80000000) x = -x;
1325
64.6k
      temp_word <<= 1;
1326
64.6k
      cw_len++;
1327
64.6k
    }
1328
103k
    *spec_coef++ = x;
1329
103k
    if (y) {
1330
71.1k
      if (temp_word & 0x80000000) y = -y;
1331
71.1k
      temp_word <<= 1;
1332
71.1k
      cw_len++;
1333
71.1k
    }
1334
103k
    *spec_coef++ = y;
1335
103k
    if (z) {
1336
74.3k
      if (temp_word & 0x80000000) z = -z;
1337
74.3k
      temp_word <<= 1;
1338
74.3k
      cw_len++;
1339
74.3k
    }
1340
103k
    *spec_coef++ = z;
1341
1342
103k
  }
1343
1344
50.6k
  else {
1345
50.6k
    WORD32 w, x, y, z;
1346
1347
50.6k
    w = index / 27 - 1;
1348
50.6k
    index = index - (w + 1) * 27;
1349
50.6k
    x = index / 9 - 1;
1350
50.6k
    index = index - (x + 1) * 9;
1351
50.6k
    y = index / 3 - 1;
1352
50.6k
    z = index - ((y + 1) * 3) - 1;
1353
50.6k
    *spec_coef++ = w;
1354
50.6k
    *spec_coef++ = x;
1355
50.6k
    *spec_coef++ = y;
1356
50.6k
    *spec_coef++ = z;
1357
50.6k
  }
1358
1359
154k
  return cw_len;
1360
154k
}
1361
1362
static PLATFORM_INLINE UWORD16 ixheaacd_huff_dec_word_hcr_non_pcw(
1363
    ia_bit_buf_struct *itt_bit_buff, WORD32 *spec_coef,
1364
912k
    const UWORD16 *code_book_tbl, const UWORD32 *idx_table) {
1365
912k
  WORD32 sp1, sp2;
1366
912k
  WORD32 flush_cw;
1367
912k
  WORD32 i, value, norm_val, off;
1368
912k
  WORD32 out1, out2;
1369
912k
  UWORD16 cw_len;
1370
1371
912k
  WORD16 index;
1372
912k
  WORD32 length;
1373
1374
912k
  WORD32 read_word;
1375
912k
  WORD32 increment;
1376
912k
  UWORD8 *ptr_read_next;
1377
1378
912k
  read_word = ixheaacd_aac_showbits_32(itt_bit_buff->byte_ptr,
1379
912k
                                       itt_bit_buff->bit_count, &increment);
1380
1381
912k
  ptr_read_next = itt_bit_buff->byte_ptr;
1382
912k
  ptr_read_next += increment;
1383
1384
912k
  ixheaacd_huff_sfb_table(read_word, &index, &length, code_book_tbl, idx_table);
1385
912k
  cw_len = length;
1386
1387
912k
  ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1388
1389
912k
  out1 = index / 17;
1390
912k
  out2 = index - out1 * 17;
1391
912k
  flush_cw = read_word << length;
1392
1393
912k
  sp1 = out1;
1394
912k
  sp2 = out2;
1395
1396
912k
  if (out1) {
1397
710k
    if (flush_cw & 0x80000000) {
1398
285k
      out1 = -out1;
1399
285k
    }
1400
710k
    flush_cw = (WORD32)flush_cw << 1;
1401
710k
    length++;
1402
710k
    cw_len++;
1403
710k
  }
1404
1405
912k
  if (out2) {
1406
709k
    if (flush_cw & 0x80000000) {
1407
246k
      out2 = -out2;
1408
246k
    }
1409
709k
    length++;
1410
709k
    cw_len++;
1411
709k
  }
1412
1413
912k
  ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1414
1415
912k
  if (sp1 == 16) {
1416
154k
    i = 4;
1417
154k
    value = ixheaac_extu(read_word, length, 23);
1418
154k
    value = value | 0xfffffe00;
1419
154k
    norm_val = ixheaac_norm32(value);
1420
1421
154k
    i += (norm_val - 22);
1422
154k
    length += (norm_val - 21);
1423
154k
    cw_len += (norm_val - 21);
1424
1425
154k
    ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1426
1427
154k
    off = ixheaac_extu(read_word, length, 32 - i);
1428
154k
    length += i;
1429
154k
    cw_len += i;
1430
1431
154k
    ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1432
1433
154k
    i = off + ((WORD32)1 << i);
1434
1435
154k
    if (out1 < 0)
1436
48.3k
      *spec_coef++ = -i;
1437
105k
    else
1438
105k
      *spec_coef++ = i;
1439
758k
  } else {
1440
758k
    *spec_coef++ = out1;
1441
758k
  }
1442
1443
912k
  if (sp2 == 16) {
1444
128k
    i = 4;
1445
128k
    value = ixheaac_extu(read_word, length, 23);
1446
128k
    value = value | 0xfffffe00;
1447
128k
    norm_val = ixheaac_norm32(value);
1448
1449
128k
    i += (norm_val - 22);
1450
128k
    length += (norm_val - 21);
1451
128k
    cw_len += (norm_val - 21);
1452
1453
128k
    ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1454
1455
128k
    off = ixheaac_extu(read_word, length, 32 - i);
1456
128k
    length += i;
1457
128k
    cw_len += i;
1458
1459
128k
    ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1460
128k
    i = off + ((WORD32)1 << i);
1461
1462
128k
    if (out2 < 0)
1463
49.0k
      *spec_coef++ = -i;
1464
79.6k
    else
1465
79.6k
      *spec_coef++ = i;
1466
783k
  } else {
1467
783k
    *spec_coef++ = out2;
1468
783k
  }
1469
1470
912k
  return cw_len;
1471
912k
}
1472
1473
static VOID ixheaacd_decode_hcr_non_pcw(
1474
    ia_bit_buf_struct *itt_bit_buff, ia_hcr_info_struct *ptr_hcr_info,
1475
    ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 *cw_offset, WORD32 trial,
1476
5.44M
    WORD32 start) {
1477
5.44M
  WORD16 codeword_len = 0;
1478
5.44M
  WORD8 seg_bits_left;
1479
5.44M
  UWORD8 tot_bits_to_save, code_bits_to_save, extra_code_bits;
1480
5.44M
  WORD32 segment_offset = 0;
1481
5.44M
  WORD8 *p_remaining_bits_in_seg =
1482
5.44M
      ptr_hcr_info->str_segment_info.p_remaining_bits_in_seg;
1483
5.44M
  WORD32 num_segment = ptr_hcr_info->str_segment_info.num_segment;
1484
1485
122M
  for (segment_offset = start; segment_offset < trial;
1486
116M
       segment_offset++, *cw_offset += 1) {
1487
116M
    if (p_remaining_bits_in_seg[segment_offset] &&
1488
57.0M
        !ptr_hcr_info->str_segment_info.is_decoded[*cw_offset]) {
1489
1.33M
      {
1490
1.33M
        UWORD32 i_qsc;
1491
1.33M
        WORD8 current_seg_bits = p_remaining_bits_in_seg[segment_offset];
1492
1493
1.33M
        itt_bit_buff->byte_ptr = itt_bit_buff->ptr_start;
1494
1.33M
        itt_bit_buff->valid_bits = 0;
1495
1.33M
        itt_bit_buff->byte = 0;
1496
1.33M
        itt_bit_buff->bit_count = 0;
1497
1.33M
        itt_bit_buff->write_bit_count = 0;
1498
1499
1.33M
        if (ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset]) {
1500
309k
          extra_code_bits = max(
1501
309k
              ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset] - 32, 0);
1502
309k
          code_bits_to_save =
1503
309k
              min(ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset], 32);
1504
1505
309k
          ixheaacd_write_bit(
1506
309k
              itt_bit_buff,
1507
309k
              ptr_hcr_info->str_segment_info.code_extra[*cw_offset],
1508
309k
              extra_code_bits);
1509
309k
          ixheaacd_write_bit(itt_bit_buff,
1510
309k
                             ptr_hcr_info->str_segment_info.code[*cw_offset],
1511
309k
                             code_bits_to_save);
1512
309k
        }
1513
1.33M
        {
1514
1.33M
          UWORD32 bit;
1515
1.33M
          WORD32 read_bit_offset;
1516
1517
1.33M
          if (ptr_hcr_info->str_segment_info.read_direction ==
1518
1.33M
              FROM_LEFT_TO_RIGHT) {
1519
494k
            read_bit_offset =
1520
494k
                ptr_hcr_info->str_segment_info.arr_seg_start_l[segment_offset] -
1521
494k
                (itt_bit_buff->size - itt_bit_buff->cnt_bits);
1522
494k
            if (read_bit_offset) {
1523
494k
              itt_bit_buff->cnt_bits += -read_bit_offset;
1524
494k
            }
1525
494k
            itt_bit_buff->ptr_read_next =
1526
494k
                itt_bit_buff->ptr_bit_buf_base +
1527
494k
                ((itt_bit_buff->size - itt_bit_buff->cnt_bits) >> 3);
1528
494k
            itt_bit_buff->bit_pos =
1529
494k
                ((itt_bit_buff->size - itt_bit_buff->cnt_bits) & 7);
1530
1531
6.15M
            for (; p_remaining_bits_in_seg[segment_offset] > 0;
1532
5.66M
                 p_remaining_bits_in_seg[segment_offset] -= 1) {
1533
5.66M
              bit = ixheaacd_aac_read_bit_rev(itt_bit_buff);
1534
5.66M
              ptr_hcr_info->str_segment_info.arr_seg_start_l[segment_offset] +=
1535
5.66M
                  1;
1536
1537
5.66M
              ixheaacd_write_bit(itt_bit_buff, bit, 1);
1538
5.66M
            }
1539
1540
843k
          } else {
1541
843k
            read_bit_offset =
1542
843k
                ptr_hcr_info->str_segment_info.arr_seg_start_r[segment_offset] -
1543
843k
                (itt_bit_buff->size - itt_bit_buff->cnt_bits);
1544
843k
            if (read_bit_offset) {
1545
843k
              itt_bit_buff->cnt_bits += -read_bit_offset;
1546
843k
            }
1547
843k
            itt_bit_buff->ptr_read_next =
1548
843k
                itt_bit_buff->ptr_bit_buf_base +
1549
843k
                ((itt_bit_buff->size - itt_bit_buff->cnt_bits) >> 3);
1550
843k
            itt_bit_buff->bit_pos =
1551
843k
                ((itt_bit_buff->size - itt_bit_buff->cnt_bits) & 7);
1552
1553
13.9M
            for (; p_remaining_bits_in_seg[segment_offset] > 0;
1554
13.0M
                 p_remaining_bits_in_seg[segment_offset] -= 1) {
1555
13.0M
              bit = ixheaacd_aac_read_bit(itt_bit_buff);
1556
13.0M
              ptr_hcr_info->str_segment_info.arr_seg_start_r[segment_offset] -=
1557
13.0M
                  1;
1558
13.0M
              ixheaacd_write_bit(itt_bit_buff, bit, 1);
1559
13.0M
            }
1560
843k
          }
1561
1.33M
        }
1562
1563
1.33M
        ixheaacd_write_bit(itt_bit_buff, 0, 32 - itt_bit_buff->bit_count % 32);
1564
1.33M
        itt_bit_buff->valid_bits = 8;
1565
1.33M
        itt_bit_buff->byte_ptr = itt_bit_buff->ptr_start;
1566
1.33M
        itt_bit_buff->byte = *itt_bit_buff->ptr_start;
1567
1568
1.33M
        if (current_seg_bits) {
1569
1.33M
          i_qsc = ptr_hcr_info->str_non_pcw_side_info
1570
1.33M
                      .res_ptr_idx[*cw_offset % num_segment];
1571
1572
1.33M
          if (ptr_hcr_info->str_non_pcw_side_info
1573
1.33M
                  .ptr_cb[*cw_offset % num_segment] <= 4) {
1574
154k
            WORD32 tbl_sign = 0;
1575
154k
            const UWORD16 *cb_table =
1576
154k
                (UWORD16
1577
154k
                     *)(ptr_aac_tables
1578
154k
                            ->code_book[ptr_hcr_info->str_non_pcw_side_info
1579
154k
                                            .ptr_cb[*cw_offset % num_segment]]);
1580
154k
            const UWORD32 *idx_table =
1581
154k
                (UWORD32 *)(ptr_aac_tables->index_table
1582
154k
                                [ptr_hcr_info->str_non_pcw_side_info
1583
154k
                                     .ptr_cb[*cw_offset % num_segment]]);
1584
1585
154k
            if (ptr_hcr_info->str_non_pcw_side_info
1586
154k
                    .ptr_cb[*cw_offset % num_segment] > 2) {
1587
103k
              tbl_sign = 1;
1588
103k
            }
1589
1590
154k
            codeword_len = ixheaacd_huff_dec_quad_hcr_non_pcw(
1591
154k
                itt_bit_buff,
1592
154k
                &ptr_hcr_info->str_non_pcw_side_info.ptr_result_base[i_qsc],
1593
154k
                cb_table, tbl_sign, idx_table);
1594
1595
154k
            seg_bits_left =
1596
154k
                current_seg_bits - codeword_len +
1597
154k
                ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset];
1598
1599
154k
          }
1600
1601
1.18M
          else if (ptr_hcr_info->str_non_pcw_side_info
1602
1.18M
                       .ptr_cb[*cw_offset % num_segment] < 11) {
1603
271k
            WORD32 tbl_sign = 0;
1604
271k
            WORD32 huff_mode = 9;
1605
1606
271k
            const UWORD16 *cb_table =
1607
271k
                (UWORD16
1608
271k
                     *)(ptr_aac_tables
1609
271k
                            ->code_book[ptr_hcr_info->str_non_pcw_side_info
1610
271k
                                            .ptr_cb[*cw_offset % num_segment]]);
1611
271k
            const UWORD32 *idx_table =
1612
271k
                (UWORD32 *)(ptr_aac_tables->index_table
1613
271k
                                [ptr_hcr_info->str_non_pcw_side_info
1614
271k
                                     .ptr_cb[*cw_offset % num_segment]]);
1615
1616
271k
            if (ptr_hcr_info->str_non_pcw_side_info
1617
271k
                    .ptr_cb[*cw_offset % num_segment] > 6) {
1618
150k
              if (ptr_hcr_info->str_non_pcw_side_info
1619
150k
                      .ptr_cb[*cw_offset % num_segment] > 8)
1620
145k
                huff_mode = 13;
1621
5.42k
              else
1622
5.42k
                huff_mode = 8;
1623
150k
              tbl_sign = 1;
1624
150k
            }
1625
271k
            codeword_len = ixheaacd_huff_dec_pair_hcr_non_pcw(
1626
271k
                itt_bit_buff,
1627
271k
                &ptr_hcr_info->str_non_pcw_side_info.ptr_result_base[i_qsc],
1628
271k
                cb_table, tbl_sign, idx_table, huff_mode);
1629
1630
271k
            seg_bits_left =
1631
271k
                current_seg_bits - codeword_len +
1632
271k
                ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset];
1633
271k
          }
1634
1.33M
          if (ptr_hcr_info->str_non_pcw_side_info
1635
1.33M
                  .ptr_cb[*cw_offset % num_segment] >= 11) {
1636
912k
            const UWORD32 *idx_table =
1637
912k
                ptr_aac_tables->pstr_huffmann_tables->idx_table_hf11;
1638
912k
            const UWORD16 *cb_table =
1639
912k
                ptr_aac_tables->pstr_huffmann_tables->input_table_cb11;
1640
1641
912k
            codeword_len = ixheaacd_huff_dec_word_hcr_non_pcw(
1642
912k
                itt_bit_buff,
1643
912k
                &ptr_hcr_info->str_non_pcw_side_info.ptr_result_base[i_qsc],
1644
912k
                cb_table, idx_table);
1645
1646
912k
            seg_bits_left =
1647
912k
                current_seg_bits - codeword_len +
1648
912k
                ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset];
1649
912k
          }
1650
1.33M
          if (seg_bits_left < 0) {
1651
343k
            tot_bits_to_save =
1652
343k
                current_seg_bits +
1653
343k
                ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset];
1654
343k
            extra_code_bits = max(tot_bits_to_save - 32, 0);
1655
343k
            code_bits_to_save = min(tot_bits_to_save, 32);
1656
1657
343k
            ptr_hcr_info->str_segment_info.code_extra[*cw_offset] =
1658
343k
                ixheaacd_read_bit(itt_bit_buff, extra_code_bits);
1659
343k
            ptr_hcr_info->str_segment_info.code[*cw_offset] =
1660
343k
                ixheaacd_read_bit(itt_bit_buff, code_bits_to_save);
1661
343k
            ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset] =
1662
343k
                tot_bits_to_save;
1663
1664
343k
            p_remaining_bits_in_seg[segment_offset] = 0;
1665
343k
            if (p_remaining_bits_in_seg[segment_offset] < 0)
1666
0
              p_remaining_bits_in_seg[segment_offset] = 0;
1667
994k
          } else {
1668
994k
            p_remaining_bits_in_seg[segment_offset] =
1669
994k
                current_seg_bits -
1670
994k
                (codeword_len -
1671
994k
                 ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset]);
1672
994k
            ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset] = 0;
1673
994k
            ptr_hcr_info->str_segment_info.is_decoded[*cw_offset] = 1;
1674
994k
            if (p_remaining_bits_in_seg[segment_offset] < 0)
1675
0
              p_remaining_bits_in_seg[segment_offset] = 0;
1676
994k
          }
1677
1678
1.33M
          if (p_remaining_bits_in_seg[segment_offset] > 0) {
1679
936k
            if (ptr_hcr_info->str_segment_info.read_direction ==
1680
936k
                FROM_LEFT_TO_RIGHT)
1681
312k
              ptr_hcr_info->str_segment_info.arr_seg_start_l[segment_offset] -=
1682
312k
                  (p_remaining_bits_in_seg[segment_offset]);
1683
624k
            else
1684
624k
              ptr_hcr_info->str_segment_info.arr_seg_start_r[segment_offset] +=
1685
624k
                  (p_remaining_bits_in_seg[segment_offset]);
1686
936k
          }
1687
1.33M
        }
1688
1.33M
      }
1689
1.33M
    }
1690
116M
  }
1691
5.44M
}
1692
1693
VOID ixheaacd_decode_non_pcw(ia_bit_buf_struct *itt_bit_buff,
1694
                             ia_hcr_info_struct *ptr_hcr_info,
1695
21.7k
                             ia_aac_dec_tables_struct *ptr_aac_tables) {
1696
21.7k
  UWORD32 num_valid_segment;
1697
21.7k
  WORD32 cw_offset;
1698
21.7k
  WORD32 trial;
1699
21.7k
  WORD32 num_segment;
1700
21.7k
  WORD32 num_code_word;
1701
21.7k
  UWORD8 num_set;
1702
21.7k
  UWORD8 current_set;
1703
21.7k
  WORD32 code_word_set;
1704
21.7k
  WORD32 loop1, loop2;
1705
1706
21.7k
  num_segment = ptr_hcr_info->str_segment_info.num_segment;
1707
1708
21.7k
  num_valid_segment = ixheaacd_init_segment_bit_field(
1709
21.7k
      num_segment, ptr_hcr_info->str_segment_info.p_remaining_bits_in_seg);
1710
1711
21.7k
  if (num_valid_segment != 0) {
1712
21.3k
    num_code_word = ptr_hcr_info->sect_info.num_code_word;
1713
21.3k
    num_set = ((num_code_word - 1) / num_segment) + 1;
1714
1715
21.3k
    ptr_hcr_info->str_segment_info.read_direction = FROM_RIGHT_TO_LEFT;
1716
1717
209k
    for (current_set = 1; current_set < num_set; current_set++) {
1718
188k
      num_code_word -= num_segment;
1719
188k
      if (num_code_word < num_segment) {
1720
14.4k
        code_word_set = num_code_word;
1721
173k
      } else {
1722
173k
        code_word_set = num_segment;
1723
173k
      }
1724
1725
188k
      ixheaacd_nonpcw_sideinfo_init(ptr_hcr_info);
1726
1727
188k
      cw_offset = num_segment * current_set;
1728
1729
188k
      ixheaacd_decode_hcr_non_pcw(itt_bit_buff, ptr_hcr_info, ptr_aac_tables,
1730
188k
                                  &cw_offset, code_word_set, 0);
1731
1732
2.81M
      for (trial = 1; trial < num_segment; trial++) {
1733
2.62M
        cw_offset = num_segment * current_set;
1734
1735
2.62M
        loop1 = min(num_segment, trial + code_word_set);
1736
2.62M
        loop2 = max(0, trial + code_word_set - num_segment);
1737
1738
2.62M
        ixheaacd_decode_hcr_non_pcw(itt_bit_buff, ptr_hcr_info, ptr_aac_tables,
1739
2.62M
                                    &cw_offset, loop1, trial);
1740
1741
2.62M
        ixheaacd_decode_hcr_non_pcw(itt_bit_buff, ptr_hcr_info, ptr_aac_tables,
1742
2.62M
                                    &cw_offset, loop2, 0);
1743
2.62M
      }
1744
1745
188k
      ptr_hcr_info->str_segment_info.read_direction = ixheaacd_toggle_read_dir(
1746
188k
          ptr_hcr_info->str_segment_info.read_direction);
1747
188k
    }
1748
21.3k
  }
1749
21.7k
}
1750
1751
static VOID ixheaacd_hcr_reorder_quantized_spec_coeff(
1752
    ia_hcr_info_struct *ptr_hcr_info,
1753
40.0k
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info) {
1754
40.0k
  WORD32 qsc;
1755
40.0k
  UWORD32 abs_qsc;
1756
40.0k
  UWORD32 i, j;
1757
40.0k
  UWORD16 num_spec_val_sect;
1758
40.0k
  WORD32 *ptr_teva;
1759
40.0k
  UWORD16 lav_err_cnt = 0;
1760
1761
40.0k
  UWORD32 num_sect = ptr_hcr_info->str_dec_io.num_sect;
1762
40.0k
  WORD32 *ptr_quant_spec_coeff_base =
1763
40.0k
      ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base;
1764
40.0k
  WORD32 *ptr_quant_spec_coeff =
1765
40.0k
      ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base;
1766
40.0k
  const UWORD8 *ptr_cb_dim_shift_tbl =
1767
40.0k
      ptr_hcr_info->table_info.ptr_cb_dim_shift_tbl;
1768
40.0k
  const UWORD16 *ptr_lav_tbl = ptr_hcr_info->table_info.ptr_lav_tbl;
1769
40.0k
  UWORD8 *ptr_sorted_cb = ptr_hcr_info->sect_info.ptr_sorted_cb;
1770
40.0k
  UWORD16 *ptr_num_sorted_cw_in_sect =
1771
40.0k
      ptr_hcr_info->sect_info.ptr_num_sorted_cw_in_sect;
1772
40.0k
  UWORD16 *ptr_reorder_offset = ptr_hcr_info->sect_info.ptr_reorder_offset;
1773
40.0k
  WORD32 *arr_temp_values = ptr_hcr_info->str_segment_info.arr_temp_values;
1774
40.0k
  WORD32 *ptr_bak = ptr_hcr_info->str_segment_info.arr_temp_values;
1775
1776
695k
  for (i = num_sect; i != 0; i--) {
1777
655k
    num_spec_val_sect = *ptr_num_sorted_cw_in_sect++
1778
655k
                        << ptr_cb_dim_shift_tbl[*ptr_sorted_cb];
1779
655k
    ptr_teva = &arr_temp_values[*ptr_reorder_offset++];
1780
24.7M
    for (j = num_spec_val_sect; j != 0; j--) {
1781
24.0M
      qsc = *ptr_quant_spec_coeff++;
1782
24.0M
      abs_qsc = ixheaac_abs32(qsc);
1783
24.0M
      if (abs_qsc <= ptr_lav_tbl[*ptr_sorted_cb]) {
1784
23.9M
        *ptr_teva++ = (WORD32)qsc;
1785
23.9M
      } else {
1786
84.6k
        if (abs_qsc == 8192) {
1787
237
          *ptr_teva++ = (WORD32)qsc;
1788
84.4k
        } else {
1789
84.4k
          *ptr_teva++ = (WORD32)8192;
1790
84.4k
          lav_err_cnt += 1;
1791
84.4k
        }
1792
84.6k
      }
1793
24.0M
    }
1794
655k
    ptr_sorted_cb++;
1795
655k
  }
1796
1797
40.0k
  if (ptr_aac_dec_channel_info->str_ics_info.window_sequence ==
1798
40.0k
      EIGHT_SHORT_SEQUENCE) {
1799
18.0k
    WORD32 *ptr_out;
1800
18.0k
    WORD8 window;
1801
1802
18.0k
    ptr_bak = ptr_hcr_info->str_segment_info.arr_temp_values;
1803
162k
    for (window = 0; window < 8; window++) {
1804
144k
      ptr_out = ptr_quant_spec_coeff_base +
1805
144k
                (window * ptr_aac_dec_channel_info->granule_len);
1806
4.75M
      for (i = 0; i < (LINES_PER_UNIT_GROUP); i++) {
1807
4.61M
        ptr_teva = ptr_bak + (window << 2) + i * 32;
1808
23.0M
        for (j = (LINES_PER_UNIT); j != 0; j--) {
1809
18.4M
          *ptr_out++ = *ptr_teva++;
1810
18.4M
        }
1811
4.61M
      }
1812
144k
    }
1813
22.0k
  } else {
1814
22.0k
    ptr_quant_spec_coeff = ptr_quant_spec_coeff_base;
1815
22.5M
    for (i = 1024; i != 0; i--) {
1816
22.5M
      *ptr_quant_spec_coeff++ = *ptr_bak++;
1817
22.5M
    }
1818
22.0k
  }
1819
1820
40.0k
  if (lav_err_cnt != 0) {
1821
12.4k
    ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 1);
1822
12.4k
  }
1823
40.0k
}
1824
1825
static VOID ixheaacd_err_detect_segmentation_final(
1826
40.0k
    ia_hcr_info_struct *ptr_hcr_info) {
1827
40.0k
  UWORD8 segmentation_err_flag = 0;
1828
40.0k
  UWORD16 i;
1829
40.0k
  WORD8 *p_remaining_bits_in_seg =
1830
40.0k
      ptr_hcr_info->str_segment_info.p_remaining_bits_in_seg;
1831
40.0k
  UWORD32 num_segment = ptr_hcr_info->str_segment_info.num_segment;
1832
1833
7.23M
  for (i = num_segment; i != 0; i--) {
1834
7.19M
    if (*p_remaining_bits_in_seg++ != 0) {
1835
6.63M
      segmentation_err_flag = 1;
1836
6.63M
    }
1837
7.19M
  }
1838
40.0k
  if (segmentation_err_flag == 1) {
1839
28.4k
    ptr_hcr_info->str_dec_io.err_log |= ERROR_POS;
1840
28.4k
  }
1841
40.0k
}
1842
1843
UWORD32 ixheaacd_hcr_decoder(
1844
    ia_hcr_info_struct *ptr_hcr_info,
1845
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
1846
45.2k
    ia_aac_dec_tables_struct *ptr_aac_tables, ia_bit_buf_struct *itt_bit_buff) {
1847
45.2k
  WORD32 ptr_tmp1, ptr_tmp2, ptr_tmp3, ptr_tmp4;
1848
45.2k
  WORD32 ptr_tmp5;
1849
1850
45.2k
  WORD32 bit_cnt_offset;
1851
45.2k
  UWORD32 save_bit_cnt = itt_bit_buff->cnt_bits;
1852
1853
45.2k
  ixheaacd_huff_calc_num_cwd(ptr_hcr_info);
1854
1855
45.2k
  ixheaacd_huff_sort_sect_cb_cwd(ptr_hcr_info);
1856
1857
45.2k
  if (ixheaacd_hcr_prepare_segmentation_grid(ptr_hcr_info) != 0)
1858
4.24k
    return (ptr_hcr_info->str_dec_io.err_log);
1859
1860
41.0k
  ixheaacd_huff_ext_sect_info(ptr_hcr_info);
1861
1862
41.0k
  if ((ptr_hcr_info->str_dec_io.err_log & HCR_FATAL_PCW_ERROR_MASK) != 0) {
1863
869
    return (ptr_hcr_info->str_dec_io.err_log);
1864
869
  }
1865
1866
40.1k
  ixheaacd_calc_num_ext_sorted_sect_sets(
1867
40.1k
      ptr_hcr_info->str_segment_info.num_segment,
1868
40.1k
      ptr_hcr_info->sect_info.ptr_num_ext_sorted_cw_in_sect,
1869
40.1k
      ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx,
1870
40.1k
      ptr_hcr_info->sect_info.ptr_num_ext_sorted_sect_in_sets,
1871
40.1k
      ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx);
1872
1873
40.1k
  ptr_tmp1 = ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx;
1874
40.1k
  ptr_tmp2 = ptr_hcr_info->sect_info.ext_sorted_cw_idx;
1875
40.1k
  ptr_tmp3 = ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx;
1876
40.1k
  ptr_tmp4 = ptr_hcr_info->str_dec_io.quant_spec_coeff_idx;
1877
40.1k
  ptr_tmp5 = ptr_hcr_info->sect_info.ext_sorted_sect_max_cb_len_idx;
1878
1879
40.1k
  ixheaacd_decode_pcw(itt_bit_buff, ptr_hcr_info, ptr_aac_tables);
1880
1881
40.1k
  if ((ptr_hcr_info->str_dec_io.err_log & HCR_FATAL_PCW_ERROR_MASK) == 0) {
1882
21.7k
    ixheaacd_decode_non_pcw(itt_bit_buff, ptr_hcr_info, ptr_aac_tables);
1883
21.7k
  }
1884
1885
40.1k
  ixheaacd_err_detect_segmentation_final(ptr_hcr_info);
1886
1887
40.1k
  ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx = ptr_tmp1;
1888
40.1k
  ptr_hcr_info->sect_info.ext_sorted_cw_idx = ptr_tmp2;
1889
40.1k
  ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx = ptr_tmp3;
1890
40.1k
  ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = ptr_tmp4;
1891
40.1k
  ptr_hcr_info->sect_info.ext_sorted_sect_max_cb_len_idx = ptr_tmp5;
1892
1893
40.1k
  ixheaacd_hcr_reorder_quantized_spec_coeff(ptr_hcr_info,
1894
40.1k
                                            ptr_aac_dec_channel_info);
1895
1896
40.1k
  bit_cnt_offset = (WORD32)itt_bit_buff->cnt_bits - (WORD32)save_bit_cnt;
1897
40.1k
  if (bit_cnt_offset) {
1898
39.9k
    itt_bit_buff->cnt_bits += -bit_cnt_offset;
1899
39.9k
    itt_bit_buff->ptr_read_next =
1900
39.9k
        itt_bit_buff->ptr_bit_buf_base +
1901
39.9k
        ((itt_bit_buff->size - itt_bit_buff->cnt_bits) >> 3);
1902
39.9k
    itt_bit_buff->bit_pos = (itt_bit_buff->size - itt_bit_buff->cnt_bits) & 7;
1903
39.9k
  }
1904
1905
40.1k
  return (ptr_hcr_info->str_dec_io.err_log);
1906
41.0k
}