Coverage Report

Created: 2026-06-10 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/decoder/ixheaacd_channel.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
24
#include "ixheaac_constants.h"
25
#include "ixheaac_basic_ops32.h"
26
#include "ixheaac_basic_ops16.h"
27
#include "ixheaac_basic_ops40.h"
28
#include "ixheaac_basic_ops.h"
29
30
#include "ixheaacd_bitbuffer.h"
31
#include "ixheaacd_error_codes.h"
32
#include "ixheaacd_defines.h"
33
#include "ixheaacd_aac_rom.h"
34
#include "ixheaacd_pulsedata.h"
35
36
#include "ixheaacd_pns.h"
37
#include "ixheaacd_drc_data_struct.h"
38
39
#include "ixheaacd_lt_predict.h"
40
#include "ixheaacd_cnst.h"
41
#include "ixheaacd_ec_defines.h"
42
#include "ixheaacd_ec_struct_def.h"
43
#include "ixheaacd_channelinfo.h"
44
#include "ixheaacd_drc_dec.h"
45
#include "ixheaacd_sbrdecoder.h"
46
47
#include "ixheaacd_block.h"
48
#include "ixheaacd_channel.h"
49
50
#include "ixheaacd_common_rom.h"
51
#include "ixheaacd_basic_funcs.h"
52
#include "ixheaac_basic_op.h"
53
#include "ixheaacd_intrinsics.h"
54
#include "ixheaacd_stereo.h"
55
56
#include "ixheaacd_tns.h"
57
58
#include "ixheaacd_audioobjtypes.h"
59
#include "ixheaacd_latmdemux.h"
60
61
#include "ixheaacd_aacdec.h"
62
#include "ixheaacd_definitions.h"
63
64
#include "ixheaacd_error_codes.h"
65
66
#include "ixheaacd_pulsedata.h"
67
68
#include "ixheaacd_sbrdecsettings.h"
69
#include "ixheaacd_sbr_scale.h"
70
#include "ixheaacd_env_extr_part.h"
71
#include "ixheaacd_sbr_rom.h"
72
#include "ixheaacd_audioobjtypes.h"
73
#include "ixheaacd_sbrdecoder.h"
74
#include "ixheaacd_memory_standards.h"
75
#include "ixheaacd_hybrid.h"
76
#include "ixheaacd_ps_dec.h"
77
#include "ixheaacd_mps_polyphase.h"
78
#include "ixheaacd_config.h"
79
#include "ixheaacd_qmf_dec.h"
80
#include "ixheaacd_mps_macro_def.h"
81
#include "ixheaacd_mps_struct_def.h"
82
#include "ixheaacd_mps_res_rom.h"
83
#include "ixheaacd_mps_aac_struct.h"
84
#include "ixheaacd_mps_dec.h"
85
#include "ixheaacd_struct_def.h"
86
#include "ixheaacd_adts_crc_check.h"
87
#include "ixheaacd_rvlc.h"
88
#include "ixheaacd_hcr.h"
89
#include "ixheaacd_function_selector.h"
90
91
1.63M
#define SPEC(ptr, w, gl) ((ptr) + ((w) * (gl)))
92
93
UWORD32 ixheaacd_aac_showbits_32(UWORD8 *ptr_read_next, WORD32 cnt_bits,
94
1.81M
                                 WORD32 *increment) {
95
1.81M
  UWORD8 *v = ptr_read_next;
96
1.81M
  UWORD32 b = 0;
97
1.81M
  WORD32 i;
98
1.81M
  WORD32 bumped = 0;
99
100
9.06M
  for (i = 0; i < 4; i++) {
101
7.25M
    b = b << 8;
102
7.25M
    if (cnt_bits > 0) {
103
7.24M
      b = b | *v;
104
7.24M
      v++;
105
7.24M
      bumped++;
106
7.24M
    }
107
7.25M
    cnt_bits -= 8;
108
7.25M
  }
109
1.81M
  if (increment != NULL) {
110
1.21M
    *increment = bumped;
111
1.21M
  }
112
1.81M
  return b;
113
1.81M
}
114
115
WORD16 *ixheaacd_getscalefactorbandoffsets(
116
    ia_ics_info_struct *ptr_ics_info,
117
269k
    ia_aac_dec_tables_struct *ptr_aac_tables) {
118
269k
  if (ptr_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
119
210k
    return ptr_aac_tables->sfb_long_table;
120
210k
  } else {
121
58.6k
    return ptr_aac_tables->sfb_short_table;
122
58.6k
  }
123
269k
}
124
125
WORD8 *ixheaacd_getscalefactorbandwidth(
126
    ia_ics_info_struct *ptr_ics_info,
127
41.6k
    ia_aac_dec_tables_struct *ptr_aac_tables) {
128
41.6k
  if (ptr_ics_info->frame_length == 512) {
129
12.8k
    return (
130
12.8k
        WORD8 *)(&ptr_aac_tables
131
12.8k
                      ->scale_fac_bands_512[ptr_ics_info->sampling_rate_index]
132
12.8k
                                           [0]);
133
28.8k
  } else if (ptr_ics_info->frame_length == 1024) {
134
4.87k
    return (
135
4.87k
        WORD8 *)(&ptr_aac_tables
136
4.87k
                      ->scale_factor_bands_long[ptr_ics_info->sampling_rate_index]
137
4.87k
                                               [0]);
138
23.9k
  } else {
139
23.9k
    return (
140
23.9k
        WORD8 *)(&ptr_aac_tables
141
23.9k
                      ->scale_fac_bands_480[ptr_ics_info->sampling_rate_index]
142
23.9k
                                           [0]);
143
23.9k
  }
144
41.6k
}
145
146
WORD32 ixheaacd_cblock_inv_quant_spect_data(
147
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
148
    ia_aac_dec_tables_struct *ptr_aac_tables);
149
150
void ixheaacd_cblock_scale_spect_data(
151
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
152
    ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 total_channels,
153
41.6k
    WORD32 object_type, WORD32 aac_sf_data_resil_flag) {
154
41.6k
  WORD32 *ptr_spect_coeff = ptr_aac_dec_channel_info->ptr_spec_coeff;
155
41.6k
  WORD8 *ptr_sfb_width = (WORD8 *)(ixheaacd_getscalefactorbandwidth(
156
41.6k
      &(ptr_aac_dec_channel_info->str_ics_info), ptr_aac_tables));
157
41.6k
  WORD16 *ptr_scale_fac = ptr_aac_dec_channel_info->ptr_scale_factor;
158
41.6k
  WORD tot_bands = ptr_aac_dec_channel_info->str_ics_info.max_sfb;
159
160
41.6k
  WORD num_win_grp, group_len;
161
41.6k
  WORD32 *ptr_scale_table = ptr_aac_tables->pstr_block_tables->scale_table;
162
41.6k
  ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
163
164
41.6k
  if (object_type == AOT_ER_AAC_LC)
165
21.3k
    ptr_sfb_width = ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence]
166
21.3k
      .sfb_width;
167
96.0k
  for (num_win_grp = 0; num_win_grp < ptr_ics_info->num_window_groups;
168
54.3k
      num_win_grp++) {
169
54.3k
    for (group_len = 0;
170
180k
        group_len < ptr_ics_info->window_group_length[num_win_grp];
171
126k
        group_len++) {
172
126k
      (*ixheaacd_scale_factor_process)(
173
126k
          &ptr_spect_coeff[0], &ptr_scale_fac[0], tot_bands,
174
126k
          (WORD8 *)ptr_sfb_width, ptr_scale_table, total_channels,
175
126k
          object_type, aac_sf_data_resil_flag);
176
177
126k
      ptr_spect_coeff += MAX_BINS_SHORT;
178
126k
    }
179
54.3k
    ptr_scale_fac += MAX_SCALE_FACTOR_BANDS_SHORT;
180
54.3k
  }
181
41.6k
}
182
183
WORD32 ixheaacd_read_pulse_data(ia_bit_buf_struct *it_bit_buff,
184
                                ia_pulse_info_struct *ptr_pulse_info,
185
34.2k
                                ia_aac_dec_tables_struct *ptr_aac_tables) {
186
34.2k
  WORD32 i, total_offset;
187
34.2k
  WORD32 error_code = 0;
188
189
34.2k
  WORD32 value = ixheaacd_read_bits_buf(it_bit_buff, 8);
190
34.2k
  ptr_pulse_info->number_pulse = value >> 6;
191
34.2k
  ptr_pulse_info->pulse_start_band = value & 0x3F;
192
193
34.2k
  if (ptr_pulse_info->pulse_start_band >= 52) {
194
125
    return (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_PULSEDATA_ERROR;
195
125
  }
196
197
34.1k
  total_offset = ptr_aac_tables->str_aac_sfb_info[0]
198
34.1k
                     .sfb_index[ptr_pulse_info->pulse_start_band];
199
200
109k
  for (i = 0; i < ptr_pulse_info->number_pulse + 1; i++) {
201
75.8k
    WORD32 value = ixheaacd_read_bits_buf(it_bit_buff, 9);
202
75.8k
    ptr_pulse_info->pulse_offset[i] = value >> 4;
203
75.8k
    ptr_pulse_info->pulse_amp[i] = value & 0xF;
204
75.8k
    total_offset += ptr_pulse_info->pulse_offset[i];
205
206
75.8k
    if (total_offset >= 1024) {
207
26
      error_code = (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_PULSEDATA_ERROR;
208
26
    }
209
75.8k
  }
210
211
34.1k
  return error_code;
212
34.2k
}
213
214
static IA_ERRORCODE ixheaacd_read_block_data(
215
    ia_bit_buf_struct *it_bit_buff,
216
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
217
    ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 total_channels,
218
    WORD32 frame_size, WORD32 object_type, WORD32 aac_spect_data_resil_flag,
219
    WORD32 aac_sect_data_resil_flag, WORD32 aac_sf_data_resil_flag,
220
    WORD32 ele_type, ia_aac_dec_overlap_info *ptr_aac_dec_static_channel_info)
221
222
216k
{
223
216k
  FLAG gain_control_data_present;
224
216k
  WORD16 error_code = AAC_DEC_OK;
225
226
216k
  if (ptr_aac_dec_channel_info->str_ics_info.window_sequence ==
227
216k
      EIGHT_SHORT_SEQUENCE) {
228
34.8k
    memset(ptr_aac_dec_channel_info->ptr_scale_factor, 0,
229
34.8k
           MAX_WINDOWS * MAX_SCALE_FACTOR_BANDS_SHORT * 3);
230
34.8k
  }
231
232
216k
  error_code = ixheaacd_read_section_data(
233
216k
      it_bit_buff, ptr_aac_dec_channel_info, aac_spect_data_resil_flag,
234
216k
      aac_sect_data_resil_flag, ptr_aac_tables);
235
236
216k
  if (error_code) {
237
1.26k
    return error_code;
238
1.26k
  }
239
214k
  if (aac_sf_data_resil_flag &&
240
81.1k
      ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)))
241
55.7k
    ixheaacd_rvlc_read(it_bit_buff, ptr_aac_dec_channel_info);
242
159k
  else
243
159k
    ixheaacd_read_scale_factor_data(it_bit_buff, ptr_aac_dec_channel_info,
244
159k
                                    ptr_aac_tables, object_type);
245
246
214k
  error_code = 0;
247
214k
  if (object_type != AOT_ER_AAC_ELD) {
248
172k
    ptr_aac_dec_channel_info->str_pulse_info.pulse_data_present =
249
172k
        ixheaacd_read_bits_buf(it_bit_buff, 1);
250
172k
    if (ptr_aac_dec_channel_info->str_pulse_info.pulse_data_present) {
251
34.2k
      error_code = ixheaacd_read_pulse_data(
252
34.2k
          it_bit_buff, &ptr_aac_dec_channel_info->str_pulse_info,
253
34.2k
          ptr_aac_tables);
254
34.2k
    }
255
256
172k
    if (error_code) {
257
136
      return error_code;
258
136
    }
259
172k
  }
260
261
214k
  ptr_aac_dec_channel_info->str_tns_info.tns_data_present =
262
214k
      (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
263
264
214k
  if (object_type < ER_OBJECT_START) {
265
63.4k
    error_code = 0;
266
63.4k
    if (ptr_aac_dec_channel_info->str_tns_info.tns_data_present) {
267
16.9k
      error_code =
268
16.9k
          ixheaacd_read_tns_data(it_bit_buff, ptr_aac_dec_channel_info);
269
16.9k
    }
270
271
63.4k
    if (error_code) {
272
45
      return error_code;
273
45
    }
274
63.4k
  }
275
276
214k
  if (object_type != AOT_ER_AAC_ELD) {
277
172k
    gain_control_data_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
278
279
172k
    if (gain_control_data_present) {
280
479
      return (WORD16)(
281
479
          (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_GAIN_CONTROL_DATA_PRESENT);
282
479
    }
283
172k
  }
284
285
214k
  if (object_type == AOT_ER_AAC_ELD) {
286
42.1k
    if (ptr_aac_dec_channel_info->str_tns_info.tns_data_present)
287
4.17k
      error_code =
288
4.17k
          ixheaacd_read_tns_data(it_bit_buff, ptr_aac_dec_channel_info);
289
42.1k
    if (error_code) return error_code;
290
42.1k
  }
291
292
214k
  if (aac_spect_data_resil_flag &&
293
42.4k
      ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)
294
21.9k
      || (object_type == AOT_ER_AAC_LC)))
295
42.4k
    ixheaacd_hcr_read(it_bit_buff, ptr_aac_dec_channel_info, ele_type);
296
297
214k
  if (aac_sf_data_resil_flag &&
298
80.9k
      ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD))) {
299
55.6k
    error_code = ixheaacd_rvlc_dec(
300
55.6k
        ptr_aac_dec_channel_info, ptr_aac_dec_static_channel_info, it_bit_buff);
301
55.6k
    if (error_code) return error_code;
302
303
55.5k
    it_bit_buff->bit_pos = 7 - it_bit_buff->bit_pos;
304
55.5k
  }
305
306
214k
  if (object_type == AOT_ER_AAC_LD || object_type == AOT_ER_AAC_LC) {
307
108k
    if (ptr_aac_dec_channel_info->str_tns_info.tns_data_present)
308
30.1k
      error_code =
309
30.1k
          ixheaacd_read_tns_data(it_bit_buff, ptr_aac_dec_channel_info);
310
108k
    if (error_code) return error_code;
311
108k
  }
312
313
214k
  { it_bit_buff->bit_pos = 7 - it_bit_buff->bit_pos; }
314
315
214k
  error_code = ixheaacd_read_spectral_data(
316
214k
      it_bit_buff, ptr_aac_dec_channel_info, ptr_aac_tables, total_channels,
317
214k
      frame_size, object_type, aac_spect_data_resil_flag,
318
214k
      aac_sf_data_resil_flag);
319
320
214k
  it_bit_buff->bit_pos = (7 - it_bit_buff->bit_pos);
321
322
214k
  return error_code;
323
214k
}
324
325
IA_ERRORCODE ixheaacd_ltp_decode(ia_bit_buf_struct *it_bit_buff,
326
                                 ia_ics_info_struct *ptr_ics_info,
327
                                 WORD32 object_type, WORD32 frame_size,
328
9.58k
                                 WORD32 ch) {
329
9.58k
  IA_ERRORCODE retval = AAC_DEC_OK;
330
331
9.58k
  if (ptr_ics_info->predictor_data_present) {
332
3.94k
    if (ch == 0) {
333
1.98k
      ixheaacd_init_ltp_object(&(ptr_ics_info->ltp));
334
1.98k
      ptr_ics_info->ltp.data_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
335
1.98k
      if (ptr_ics_info->ltp.data_present) {
336
656
        if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
337
656
                                        &(ptr_ics_info->ltp), it_bit_buff,
338
656
                                        frame_size)) > 0) {
339
0
          return retval;
340
0
        }
341
656
      }
342
1.98k
    } else {
343
1.96k
      ixheaacd_init_ltp_object(&(ptr_ics_info->ltp2));
344
1.96k
      ptr_ics_info->ltp2.data_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
345
1.96k
      if (ptr_ics_info->ltp2.data_present) {
346
1.06k
        if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
347
1.06k
                                        &(ptr_ics_info->ltp2), it_bit_buff,
348
1.06k
                                        frame_size)) > 0) {
349
0
          return retval;
350
0
        }
351
1.06k
      }
352
1.96k
    }
353
3.94k
  }
354
9.58k
  return retval;
355
9.58k
}
356
WORD16 ixheaacd_ics_read(ia_bit_buf_struct *it_bit_buff,
357
                         ia_ics_info_struct *ptr_ics_info,
358
                         WORD8 num_swb_window[2], WORD32 object_type,
359
225k
                         WORD32 common_window, WORD32 frame_size) {
360
225k
  WORD i;
361
225k
  WORD mask;
362
225k
  WORD value = 0;
363
364
225k
  if (object_type == AOT_ER_AAC_ELD) {
365
57.8k
    ptr_ics_info->window_sequence = 0;
366
57.8k
    ptr_ics_info->window_shape = 1;
367
167k
  } else {
368
167k
    if (object_type != AOT_ER_AAC_LD)
369
101k
    {
370
101k
      if (frame_size == 960)
371
51.6k
        ptr_ics_info->frame_length = 960;
372
49.7k
      else
373
49.7k
        ptr_ics_info->frame_length = 1024;
374
101k
    }
375
167k
    value = ixheaacd_read_bits_buf(it_bit_buff, 4);
376
167k
    ptr_ics_info->window_sequence = (WORD16)((value & 0x6) >> 1);
377
167k
    ptr_ics_info->window_shape = (WORD16)((value & 0x1));
378
167k
  }
379
380
225k
  if (ptr_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
381
193k
    ptr_ics_info->num_swb_window = num_swb_window[0];
382
383
193k
    ptr_ics_info->num_window_groups = 1;
384
193k
    ptr_ics_info->window_group_length[0] = 1;
385
386
193k
    if (object_type == AOT_ER_AAC_ELD) {
387
57.8k
      ptr_ics_info->max_sfb = ixheaacd_read_bits_buf(it_bit_buff, 6);
388
57.8k
      if (ptr_ics_info->max_sfb == 0) ptr_ics_info->num_swb_window = 0;
389
136k
    } else {
390
136k
      value = ixheaacd_read_bits_buf(it_bit_buff, 7);
391
136k
      ptr_ics_info->max_sfb = (value & 0x7E) >> 1;
392
136k
    }
393
394
193k
    if ((object_type != AOT_ER_AAC_LD) && (object_type != AOT_AAC_LTP)) {
395
111k
      if (value & 1) {
396
282
        return (WORD16)(
397
282
            (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_PREDICTION_DATA_PRESENT);
398
282
      }
399
400
111k
    } else {
401
82.5k
      ptr_ics_info->predictor_data_present = value & 1;
402
403
82.5k
      if (ptr_ics_info->predictor_data_present) {
404
52.2k
        WORD32 retval = AAC_DEC_OK;
405
406
52.2k
        ixheaacd_init_ltp_object(&(ptr_ics_info->ltp));
407
52.2k
        if (object_type < ER_OBJECT_START) {
408
8.16k
          if ((ptr_ics_info->ltp.data_present =
409
8.16k
                   ixheaacd_read_bits_buf(it_bit_buff, 1)) &
410
8.16k
              1) {
411
8.10k
            if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
412
8.10k
                                            &(ptr_ics_info->ltp), it_bit_buff,
413
8.10k
                                            frame_size)) > 0) {
414
0
              return retval;
415
0
            }
416
8.10k
          }
417
8.16k
          if (common_window) {
418
292
            ixheaacd_init_ltp_object(&(ptr_ics_info->ltp2));
419
292
            if ((ptr_ics_info->ltp2.data_present =
420
292
                     ixheaacd_read_bits_buf(it_bit_buff, 1)) &
421
292
                1) {
422
10
              if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
423
10
                                              &(ptr_ics_info->ltp2),
424
10
                                              it_bit_buff, frame_size)) > 0) {
425
0
                return retval;
426
0
              }
427
10
            }
428
292
          }
429
8.16k
        }
430
52.2k
        if ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)) {
431
44.0k
          if (!common_window && (object_type >= ER_OBJECT_START)) {
432
41.9k
            if ((ptr_ics_info->ltp.data_present =
433
41.9k
                     ixheaacd_read_bits_buf(it_bit_buff, 1)) &
434
41.9k
                1) {
435
35.1k
              if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
436
35.1k
                                              &(ptr_ics_info->ltp), it_bit_buff,
437
35.1k
                                              frame_size)) < 0) {
438
5
                return retval;
439
5
              }
440
35.1k
            }
441
41.9k
          }
442
44.0k
        }
443
52.2k
      }
444
82.5k
    }
445
446
193k
  } else {
447
31.5k
    WORD32 num_groups = 0, scale_factor_grouping;
448
31.5k
    ptr_ics_info->num_swb_window = num_swb_window[1];
449
450
31.5k
    value = ixheaacd_read_bits_buf(it_bit_buff, 11);
451
31.5k
    ptr_ics_info->max_sfb = (value & 0x780) >> 7;
452
453
31.5k
    scale_factor_grouping = (value & 0x7F);
454
455
31.5k
    mask = 0x40;
456
252k
    for (i = 0; i < 7; i++) {
457
220k
      ptr_ics_info->window_group_length[i] = 1;
458
459
220k
      if (scale_factor_grouping & mask) {
460
140k
        ptr_ics_info->window_group_length[num_groups] =
461
140k
            ptr_ics_info->window_group_length[num_groups] + 1;
462
463
140k
      } else {
464
80.5k
        num_groups = num_groups + 1;
465
80.5k
      }
466
467
220k
      mask = mask >> 1;
468
220k
    }
469
470
31.5k
    ptr_ics_info->window_group_length[7] = 1;
471
31.5k
    ptr_ics_info->num_window_groups = num_groups + 1;
472
31.5k
  }
473
474
225k
  if (ptr_ics_info->max_sfb > ptr_ics_info->num_swb_window) {
475
24.9k
    return (WORD16)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_SFB_TRANSMITTED;
476
24.9k
  }
477
478
200k
  return AAC_DEC_OK;
479
225k
}
480
481
WORD16 ixheaacd_individual_ch_stream(
482
    ia_bit_buf_struct *it_bit_buff, ia_aac_decoder_struct *aac_dec_handle,
483
    WORD32 num_ch, WORD32 frame_size, WORD32 total_channels, WORD32 object_type,
484
198k
    ia_eld_specific_config_struct eld_specific_config, WORD32 ele_type) {
485
198k
  WORD16 error_code = AAC_DEC_OK;
486
198k
  WORD32 ch;
487
198k
  WORD32 crc_reg = 0;
488
489
412k
  for (ch = 0; ch < num_ch; ch++) {
490
217k
    ia_aac_dec_channel_info_struct *ptr_aac_dec_ch_info =
491
217k
        aac_dec_handle->pstr_aac_dec_ch_info[ch];
492
217k
    ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_ch_info->str_ics_info;
493
494
217k
    if (ch == 1) {
495
18.8k
      if (it_bit_buff->pstr_adts_crc_info->crc_active == 1 &&
496
36
          (it_bit_buff->pstr_adts_crc_info->no_reg < 7)) {
497
29
        crc_reg =
498
29
            ixheaacd_adts_crc_start_reg(it_bit_buff->pstr_adts_crc_info,
499
29
                                        it_bit_buff, CRC_ADTS_RAW_IIND_ICS);
500
29
      }
501
18.8k
    }
502
217k
    ptr_aac_dec_ch_info->global_gain =
503
217k
        (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 8);
504
505
217k
    if (!(aac_dec_handle->pstr_aac_dec_ch_info[LEFT]->common_window)) {
506
184k
      error_code = ixheaacd_ics_read(
507
184k
          it_bit_buff, ptr_ics_info, aac_dec_handle->num_swb_window,
508
184k
          object_type,
509
184k
          aac_dec_handle->pstr_aac_dec_ch_info[LEFT]->common_window,
510
184k
          aac_dec_handle->samples_per_frame);
511
184k
      if (ch == 1)
512
2.69k
        aac_dec_handle->pstr_aac_dec_ch_info[ch - 1]->str_ics_info.ltp2.lag =
513
2.69k
            ptr_ics_info->ltp.lag;
514
515
184k
      if (error_code) {
516
696
        if (it_bit_buff->cnt_bits < 0) {
517
0
          error_code = (WORD16)(
518
0
              (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
519
0
        }
520
696
        return error_code;
521
696
      }
522
184k
    }
523
524
216k
    error_code = ixheaacd_read_block_data(
525
216k
        it_bit_buff, ptr_aac_dec_ch_info, aac_dec_handle->pstr_aac_tables,
526
216k
        total_channels, frame_size, object_type,
527
216k
        eld_specific_config.aac_spect_data_resil_flag,
528
216k
        eld_specific_config.aac_sect_data_resil_flag,
529
216k
        eld_specific_config.aac_sf_data_resil_flag, ele_type,
530
216k
        aac_dec_handle->pstr_aac_dec_overlap_info[ch]);
531
216k
    if (error_code) {
532
2.66k
      if (it_bit_buff->cnt_bits < 0) {
533
87
        error_code = (WORD16)(
534
87
            (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
535
87
      }
536
537
2.66k
      return error_code;
538
2.66k
    }
539
540
213k
    if (ch == 0) {
541
194k
      if ((object_type == AOT_ER_AAC_LD) &&
542
63.3k
          (aac_dec_handle->pstr_aac_dec_ch_info[LEFT]->common_window) &&
543
4.78k
          (ele_type == ID_CPE)) {
544
4.78k
        IA_ERRORCODE temp =
545
4.78k
            ixheaacd_ltp_decode(it_bit_buff, ptr_ics_info, object_type,
546
4.78k
                                aac_dec_handle->samples_per_frame, 1);
547
548
4.78k
        if (temp != 0) {
549
1
          return temp;
550
1
        }
551
4.78k
        aac_dec_handle->pstr_aac_dec_ch_info[ch + 1]->str_ics_info.ltp.lag =
552
4.78k
            ptr_ics_info->ltp2.lag;
553
4.78k
      }
554
194k
    }
555
213k
    if (ch == 1) {
556
18.5k
      if (it_bit_buff->pstr_adts_crc_info->crc_active == 1) {
557
25
        ixheaacd_adts_crc_end_reg(it_bit_buff->pstr_adts_crc_info, it_bit_buff,
558
25
                                  crc_reg);
559
25
      }
560
18.5k
    }
561
213k
  }
562
563
194k
  return error_code;
564
198k
}
565
566
VOID ixheaacd_read_ms_data(
567
    ia_bit_buf_struct *it_bit_buff,
568
16.7k
    ia_aac_dec_channel_info_struct *ptr_aac_dec_ch_info) {
569
16.7k
  WORD32 num_win_group, sfb;
570
16.7k
  WORD32 ms_mask_present;
571
16.7k
  UWORD8 *ptr_ms_used = &ptr_aac_dec_ch_info->pstr_stereo_info->ms_used[0][0];
572
16.7k
  WORD32 num_window_groups =
573
16.7k
      ptr_aac_dec_ch_info->str_ics_info.num_window_groups;
574
16.7k
  WORD16 max_sfb = ptr_aac_dec_ch_info->str_ics_info.max_sfb;
575
576
16.7k
  ms_mask_present = ixheaacd_read_bits_buf(it_bit_buff, 2);
577
578
16.7k
  if (ms_mask_present < 1) {
579
8.45k
    memset(ptr_ms_used, 0,
580
8.45k
           sizeof(UWORD8) * JOINT_STEREO_MAX_BANDS * JOINT_STEREO_MAX_GROUPS);
581
8.45k
  }
582
583
8.31k
  else if (ms_mask_present == 1) {
584
3.71k
    for (num_win_group = 0; num_win_group < num_window_groups;
585
1.92k
         num_win_group++) {
586
7.50k
      for (sfb = 0; sfb < max_sfb; sfb++) {
587
5.57k
        ptr_aac_dec_ch_info->pstr_stereo_info->ms_used[num_win_group][sfb] =
588
5.57k
            (UWORD8)ixheaacd_read_bits_buf(it_bit_buff, 1);
589
5.57k
      }
590
1.92k
    }
591
592
6.53k
  } else {
593
19.3k
    for (num_win_group = 0; num_win_group < num_window_groups;
594
12.8k
         num_win_group++) {
595
12.8k
      ptr_ms_used =
596
12.8k
          &ptr_aac_dec_ch_info->pstr_stereo_info->ms_used[num_win_group][0];
597
12.8k
      memset(ptr_ms_used, 1, (max_sfb) * sizeof(UWORD8));
598
12.8k
    }
599
6.53k
  }
600
16.7k
}
601
602
IA_ERRORCODE ixheaacd_channel_pair_process(
603
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info[CHANNELS], WORD32 num_ch,
604
    ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 total_channels, WORD32 object_type,
605
    WORD32 aac_spect_data_resil_flag, WORD32 aac_sf_data_resil_flag, WORD32 *in_data,
606
194k
    WORD32 *out_data, void *self_ptr) {
607
194k
  WORD32 channel;
608
194k
  IA_ERRORCODE err = IA_NO_ERROR;
609
194k
  ia_aac_decoder_struct *self = self_ptr;
610
194k
  if (aac_spect_data_resil_flag &&
611
41.5k
      ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_ER_AAC_ELD) ||
612
41.5k
      (object_type == AOT_ER_AAC_LC))) {
613
83.1k
    for (channel = 0; channel < num_ch; channel++) {
614
41.6k
      err = ixheaacd_cblock_inv_quant_spect_data(
615
41.6k
          ptr_aac_dec_channel_info[channel], ptr_aac_tables);
616
41.6k
      if (err) return err;
617
41.6k
      ixheaacd_cblock_scale_spect_data(ptr_aac_dec_channel_info[channel],
618
41.6k
                                       ptr_aac_tables, num_ch, object_type,
619
41.6k
                                       aac_sf_data_resil_flag);
620
41.6k
    }
621
41.5k
  }
622
623
194k
  if (num_ch > 1) {
624
18.5k
    if (ptr_aac_dec_channel_info[LEFT]->common_window) {
625
15.8k
      if (ptr_aac_dec_channel_info[LEFT]->str_pns_info.pns_active ||
626
12.7k
          ptr_aac_dec_channel_info[RIGHT]->str_pns_info.pns_active) {
627
3.17k
        ixheaacd_map_ms_mask_pns(ptr_aac_dec_channel_info);
628
3.17k
      }
629
630
15.8k
      ixheaacd_ms_stereo_process(ptr_aac_dec_channel_info, ptr_aac_tables);
631
15.8k
    }
632
633
18.5k
    ixheaacd_intensity_stereo_process(
634
18.5k
        ptr_aac_dec_channel_info, ptr_aac_tables, object_type,
635
18.5k
        aac_sf_data_resil_flag,
636
18.5k
        ptr_aac_dec_channel_info[LEFT]->str_ics_info.frame_length);
637
18.5k
  }
638
639
406k
  for (channel = 0; channel < num_ch; channel++) {
640
212k
    WORD32 *p_spectrum = ptr_aac_dec_channel_info[channel]->ptr_spec_coeff;
641
642
212k
    if (total_channels > 2) {
643
12.3k
      if (ptr_aac_dec_channel_info[channel]->str_ics_info.window_sequence !=
644
12.3k
          EIGHT_SHORT_SEQUENCE) {
645
9.38k
        WORD16 *band_offsets = ptr_aac_tables->sfb_long_table;
646
9.38k
        WORD32 no_spec_coeff = band_offsets[ptr_aac_dec_channel_info[channel]
647
9.38k
                                                ->str_ics_info.max_sfb];
648
9.38k
        ixheaacd_right_shift_block(p_spectrum, no_spec_coeff, 3);
649
9.38k
      } else {
650
3.00k
        ixheaacd_right_shift_block(p_spectrum, 1024, 3);
651
3.00k
      }
652
12.3k
    }
653
654
212k
    ixheaacd_pns_process(ptr_aac_dec_channel_info, channel, ptr_aac_tables);
655
656
212k
    if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
657
98.3k
      {
658
98.3k
        if (channel == 0) {
659
91.4k
          ltp_info *ltp1 =
660
91.4k
              &(ptr_aac_dec_channel_info[channel]->str_ics_info.ltp);
661
91.4k
          ixheaacd_lt_prediction(ptr_aac_dec_channel_info[channel], ltp1,
662
91.4k
                                 p_spectrum, ptr_aac_tables,
663
91.4k
                                 self->ptr_aac_dec_static_channel_info[LEFT]
664
91.4k
                                     ->overlap_add_data.win_shape,
665
91.4k
                                 self->sampling_rate_index, object_type,
666
91.4k
                                 self->samples_per_frame, in_data, out_data);
667
668
91.4k
        } else {
669
6.93k
          ltp_info *ltp2 =
670
6.93k
              (self->pstr_aac_dec_ch_info[0]->common_window)
671
6.93k
                  ? &(ptr_aac_dec_channel_info[0]->str_ics_info.ltp2)
672
6.93k
                  :
673
674
6.93k
                  &(ptr_aac_dec_channel_info[1]->str_ics_info.ltp);
675
6.93k
          ixheaacd_lt_prediction(ptr_aac_dec_channel_info[channel], ltp2,
676
6.93k
                                 p_spectrum, ptr_aac_tables,
677
6.93k
                                 self->ptr_aac_dec_static_channel_info[RIGHT]
678
6.93k
                                     ->overlap_add_data.win_shape,
679
6.93k
                                 self->sampling_rate_index, object_type,
680
6.93k
                                 self->samples_per_frame, in_data, out_data);
681
6.93k
        }
682
98.3k
      }
683
98.3k
    }
684
685
212k
    if (ptr_aac_dec_channel_info[channel]->str_tns_info.tns_data_present) {
686
50.6k
      ixheaacd_aac_tns_process(ptr_aac_dec_channel_info[channel],
687
50.6k
                               total_channels, ptr_aac_tables, object_type, 1,
688
50.6k
                               NULL);
689
50.6k
    }
690
212k
  }
691
194k
  return err;
692
194k
}
693
694
VOID ixheaacd_set_corr_info(
695
57.6k
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info, WORD16 pns_band) {
696
57.6k
  ia_pns_correlation_info_struct *ptr_corr_info =
697
57.6k
      ptr_aac_dec_channel_info->pstr_pns_corr_info;
698
57.6k
  ptr_corr_info->correlated[(pns_band >> PNS_BAND_FLAGS_SHIFT)] |=
699
57.6k
      (1 << (pns_band & PNS_BAND_FLAGS_MASK));
700
57.6k
}
701
702
VOID ixheaacd_map_ms_mask_pns(
703
3.17k
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info[CHANNELS]) {
704
3.17k
  WORD32 num_win_group, sfb;
705
706
3.17k
  for (num_win_group = 0;
707
6.40k
       num_win_group <
708
6.40k
       ptr_aac_dec_channel_info[LEFT]->str_ics_info.num_window_groups;
709
3.22k
       num_win_group++) {
710
72.0k
    for (sfb = 0; sfb < ptr_aac_dec_channel_info[LEFT]->str_ics_info.max_sfb;
711
68.8k
         sfb++) {
712
68.8k
      if (ptr_aac_dec_channel_info[LEFT]
713
68.8k
              ->pstr_stereo_info->ms_used[num_win_group][sfb]) {
714
57.6k
        WORD16 pns_band = (num_win_group << 4) + sfb;
715
57.6k
        ixheaacd_set_corr_info(ptr_aac_dec_channel_info[LEFT], pns_band);
716
717
57.6k
        if (ptr_aac_dec_channel_info[LEFT]->str_pns_info.pns_used[pns_band] &&
718
32.8k
            ptr_aac_dec_channel_info[RIGHT]->str_pns_info.pns_used[pns_band]) {
719
16.0k
          ptr_aac_dec_channel_info[LEFT]
720
16.0k
              ->pstr_stereo_info->ms_used[num_win_group][sfb] ^= 1;
721
16.0k
        }
722
57.6k
      }
723
68.8k
    }
724
3.22k
  }
725
3.17k
}
726
727
VOID ixheaacd_pulse_data_apply(ia_pulse_info_struct *ptr_pulse_info,
728
                               WORD8 *pulse_scratch,
729
149k
                               const WORD16 *ptr_swb_offset, WORD object_type) {
730
149k
  WORD i;
731
149k
  WORD32 k;
732
733
149k
  memset(pulse_scratch, 0, sizeof(WORD32) * 256);
734
735
149k
  if (object_type != AOT_ER_AAC_ELD) {
736
109k
    if (ptr_pulse_info->pulse_data_present) {
737
23.7k
      k = ptr_swb_offset[ptr_pulse_info->pulse_start_band];
738
739
75.9k
      for (i = 0; i <= ptr_pulse_info->number_pulse; i++) {
740
52.2k
        k = k + ptr_pulse_info->pulse_offset[i];
741
52.2k
        pulse_scratch[k] = ptr_pulse_info->pulse_amp[i];
742
52.2k
      }
743
23.7k
    }
744
109k
  } else {
745
39.5k
    ptr_pulse_info->pulse_data_present = 0;
746
39.5k
  }
747
149k
}
748
749
IA_ERRORCODE ixheaacd_read_spectral_data(
750
    ia_bit_buf_struct *it_bit_buff,
751
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
752
    ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 total_channels,
753
    WORD32 frame_size, WORD32 object_type, WORD32 aac_spect_data_resil_flag,
754
213k
    WORD32 aac_sf_data_resil_flag) {
755
213k
  WORD sfb, max_sfb;
756
213k
  WORD num_win_grp, group_len, grp_offset;
757
758
213k
  WORD index;
759
213k
  WORD8 *ptr_code_book;
760
213k
  WORD16 *ptr_scale_factor;
761
213k
  WORD32 *ptr_spec_coef;
762
213k
  ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
763
213k
  WORD16 *swb_offset;
764
765
213k
  WORD32 maximum_bins_short = ptr_ics_info->frame_length >> 3;
766
767
213k
  WORD32 *ptr_spec_coef_out;
768
769
213k
  ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
770
213k
  ptr_scale_factor = ptr_aac_dec_channel_info->ptr_scale_factor;
771
213k
  ptr_spec_coef = ptr_aac_dec_channel_info->ptr_spec_coeff;
772
213k
  max_sfb = ptr_ics_info->max_sfb;
773
774
213k
  swb_offset =
775
213k
      ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence].sfb_index;
776
777
213k
  if (!aac_spect_data_resil_flag) {
778
171k
    if (ptr_aac_dec_channel_info->str_ics_info.window_sequence !=
779
171k
        EIGHT_SHORT_SEQUENCE) {
780
149k
      WORD8 *ptr_scratch;
781
782
149k
      if (object_type == AOT_ER_AAC_ELD)
783
39.5k
        ptr_scratch = (WORD8 *)ptr_aac_dec_channel_info->pulse_scratch;
784
109k
      else
785
109k
        ptr_scratch = (WORD8 *)ptr_aac_dec_channel_info->scratch_buf_ptr;
786
787
149k
      memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
788
789
149k
      ixheaacd_pulse_data_apply(&ptr_aac_dec_channel_info->str_pulse_info,
790
149k
                                ptr_scratch, swb_offset, object_type);
791
792
149k
      ptr_spec_coef_out = &ptr_spec_coef[0];
793
342k
      for (sfb = 0; sfb < max_sfb;) {
794
193k
        WORD ret_val;
795
193k
        WORD32 sfb_width;
796
193k
        WORD32 sect_cb = ptr_code_book[sfb];
797
193k
        WORD start = sfb;
798
193k
        if ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)
799
128k
            || (object_type == AOT_ER_AAC_LC)) {
800
128k
          if ((sect_cb >= 16) && (sect_cb <= 31)) {
801
45.9k
            ptr_code_book[sfb] = sect_cb = 11;
802
45.9k
          }
803
128k
        }
804
1.92M
        for (; sfb < max_sfb && (ptr_code_book[sfb] == sect_cb); sfb++)
805
1.73M
          ;
806
807
193k
        sfb_width = swb_offset[sfb] - swb_offset[start];
808
809
193k
        if (sect_cb > ZERO_HCB && (sect_cb < NOISE_HCB)) {
810
131k
          ret_val = ixheaacd_huffman_dec_word2(it_bit_buff, sect_cb, sfb_width,
811
131k
                                               ptr_aac_tables,
812
131k
                                               ptr_spec_coef_out, ptr_scratch);
813
814
131k
          if (ret_val != 0) {
815
193
            return (WORD16)(
816
193
                (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
817
193
          }
818
131k
        }
819
820
61.8k
        else {
821
61.8k
          if (ptr_aac_dec_channel_info->str_pulse_info.pulse_data_present) {
822
20.6k
            ixheaacd_inverse_quantize(
823
20.6k
                ptr_spec_coef_out, sfb_width,
824
20.6k
                (WORD32 *)
825
20.6k
                    ptr_aac_tables->pstr_block_tables->ixheaacd_pow_table_Q13,
826
20.6k
                ptr_scratch);
827
20.6k
          }
828
61.8k
        }
829
193k
        ptr_scratch += sfb_width;
830
193k
        ptr_spec_coef_out += sfb_width;
831
193k
      }
832
833
148k
      if ((object_type != AOT_ER_AAC_ELD) && (object_type != AOT_ER_AAC_LD))
834
69.3k
        index = 1024 - swb_offset[max_sfb];
835
79.5k
      else
836
79.5k
        index = frame_size - swb_offset[max_sfb];
837
838
148k
      if (index < 0) return -1;
839
840
148k
    } else {
841
22.3k
      memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
842
843
22.3k
      grp_offset = 0;
844
845
131k
      for (num_win_grp = 0; num_win_grp < ptr_ics_info->num_window_groups;
846
108k
           num_win_grp++) {
847
108k
        WORD grp_len = ptr_ics_info->window_group_length[num_win_grp];
848
849
108k
        if (maximum_bins_short == 120)
850
28.0k
          ptr_spec_coef_out = &ptr_spec_coef[grp_offset * maximum_bins_short];
851
80.9k
        else
852
80.9k
          ptr_spec_coef_out = &ptr_spec_coef[grp_offset * MAX_BINS_SHORT];
853
854
108k
        WORD bands = num_win_grp * MAX_SCALE_FACTOR_BANDS_SHORT;
855
189k
        for (sfb = 0; sfb < max_sfb;) {
856
80.0k
          WORD sect_cb = ptr_code_book[bands];
857
80.0k
          WORD start = sfb;
858
80.0k
          WORD ret_val;
859
860
80.0k
          if ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)
861
80.0k
              || (object_type == AOT_ER_AAC_LC)) {
862
68.1k
            if ((sect_cb >= 16) && (sect_cb <= 31)) {
863
50.8k
              ptr_code_book[bands] = sect_cb = 11;
864
50.8k
            }
865
68.1k
          }
866
867
255k
          for (; sfb < max_sfb && (ptr_code_book[bands] == sect_cb);
868
175k
               sfb++, bands++)
869
175k
            ;
870
871
80.0k
          if (sect_cb > ZERO_HCB && (sect_cb < NOISE_HCB)) {
872
64.5k
            ret_val = ixheaacd_decode_huffman(
873
64.5k
                it_bit_buff, sect_cb, ptr_spec_coef_out, (WORD16 *)swb_offset,
874
64.5k
                start, sfb, grp_len, ptr_aac_tables, maximum_bins_short);
875
876
64.5k
            if (ret_val != 0) {
877
69
              return (WORD16)(
878
69
                  (WORD32)
879
69
                      IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
880
69
            }
881
64.5k
          }
882
80.0k
        }
883
108k
        grp_offset = (grp_offset + grp_len);
884
108k
      }
885
22.3k
    }
886
171k
    {
887
171k
      WORD32 *ptr_scale_table;
888
889
171k
      if (maximum_bins_short != 120)
890
135k
        ptr_scale_table = ptr_aac_tables->pstr_block_tables->scale_table;
891
35.7k
      else
892
35.7k
        ptr_scale_table = ptr_aac_tables->pstr_block_tables->scale_table_960;
893
894
171k
      WORD8 *ptr_sfb_width =
895
171k
          ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence]
896
171k
              .sfb_width;
897
898
428k
      for (num_win_grp = 0; num_win_grp < ptr_ics_info->num_window_groups;
899
257k
           num_win_grp++) {
900
257k
        for (group_len = 0;
901
585k
             group_len < ptr_ics_info->window_group_length[num_win_grp];
902
327k
             group_len++) {
903
327k
          (*ixheaacd_scale_factor_process)(
904
327k
              &ptr_spec_coef[0], &ptr_scale_factor[0], max_sfb,
905
327k
              (WORD8 *)ptr_sfb_width, ptr_scale_table, total_channels,
906
327k
              object_type, aac_sf_data_resil_flag);
907
908
327k
          if (maximum_bins_short == 120)
909
70.6k
            ptr_spec_coef += maximum_bins_short;
910
256k
          else
911
256k
            ptr_spec_coef += MAX_BINS_SHORT;
912
327k
        }
913
914
257k
        ptr_scale_factor += MAX_SCALE_FACTOR_BANDS_SHORT;
915
257k
      }
916
171k
    }
917
171k
  } else {
918
42.4k
    ia_hcr_info_struct *pstr_hcr_info = &ptr_aac_dec_channel_info->str_hcr_info;
919
42.4k
    WORD32 error = 0;
920
921
42.4k
    memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
922
923
42.4k
    if (ptr_aac_dec_channel_info->reorder_spect_data_len != 0) {
924
31.2k
      error = ixheaacd_huff_code_reorder_init(
925
31.2k
          pstr_hcr_info, ptr_aac_dec_channel_info, ptr_aac_tables, it_bit_buff);
926
927
31.2k
      if (error != 0) {
928
256
        return IA_XHEAAC_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
929
256
      }
930
31.0k
      error = ixheaacd_hcr_decoder(pstr_hcr_info, ptr_aac_dec_channel_info,
931
31.0k
                                   ptr_aac_tables, it_bit_buff);
932
933
31.0k
      if (error != 0) {
934
25.2k
        ixheaacd_huff_mute_erroneous_lines(pstr_hcr_info);
935
25.2k
      }
936
937
31.0k
      if (it_bit_buff->cnt_bits <
938
31.0k
          ptr_aac_dec_channel_info->reorder_spect_data_len) {
939
280
        longjmp(*(it_bit_buff->xaac_jmp_buf),
940
280
                IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
941
280
      }
942
943
30.7k
      it_bit_buff->cnt_bits +=
944
30.7k
          -ptr_aac_dec_channel_info->reorder_spect_data_len;
945
30.7k
      it_bit_buff->ptr_read_next =
946
30.7k
          it_bit_buff->ptr_bit_buf_base +
947
30.7k
          ((it_bit_buff->size - it_bit_buff->cnt_bits) >> 3);
948
30.7k
      it_bit_buff->bit_pos = (it_bit_buff->size - it_bit_buff->cnt_bits) & 7;
949
950
30.7k
    } else {
951
11.1k
      it_bit_buff->ptr_read_next =
952
11.1k
          it_bit_buff->ptr_bit_buf_base +
953
11.1k
          ((it_bit_buff->size - it_bit_buff->cnt_bits) >> 3);
954
11.1k
      it_bit_buff->bit_pos = (it_bit_buff->size - it_bit_buff->cnt_bits) & 7;
955
11.1k
    }
956
42.4k
  }
957
958
213k
  return AAC_DEC_OK;
959
213k
}
960
961
WORD16 ixheaacd_read_tns_data(
962
    ia_bit_buf_struct *it_bit_buff,
963
51.2k
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info) {
964
51.2k
  WORD win_size, window_per_frame;
965
51.2k
  WORD n_filt_bits, start_band_bits, order_bits;
966
51.2k
  WORD32 bottom;
967
968
51.2k
  ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
969
51.2k
  ia_tns_info_aac_struct *ptr_tns_info =
970
51.2k
      &ptr_aac_dec_channel_info->str_tns_info;
971
972
51.2k
  if (ptr_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
973
35.7k
    n_filt_bits = 2;
974
35.7k
    start_band_bits = 6;
975
35.7k
    order_bits = 5;
976
35.7k
    window_per_frame = 1;
977
978
35.7k
  } else {
979
15.5k
    n_filt_bits = 1;
980
15.5k
    start_band_bits = 4;
981
15.5k
    order_bits = 3;
982
15.5k
    window_per_frame = 8;
983
15.5k
  }
984
985
51.2k
  bottom = ptr_ics_info->num_swb_window;
986
987
211k
  for (win_size = 0; win_size < window_per_frame; win_size++) {
988
159k
    WORD n_filt;
989
159k
    WORD start_band, coef_res;
990
159k
    ptr_tns_info->n_filt[win_size] = n_filt =
991
159k
        (WORD16)ixheaacd_read_bits_buf(it_bit_buff, n_filt_bits);
992
993
159k
    if (n_filt) {
994
63.4k
      WORD filt;
995
63.4k
      WORD top;
996
997
63.4k
      coef_res = ixheaacd_read_bits_buf(it_bit_buff, 1);
998
999
63.4k
      top = bottom;
1000
136k
      for (filt = 0; filt < n_filt; filt++) {
1001
73.4k
        WORD order;
1002
73.4k
        ia_filter_info_struct *filter =
1003
73.4k
            &ptr_tns_info->str_filter[win_size][filt];
1004
1005
73.4k
        start_band = ixheaacd_read_bits_buf(it_bit_buff, start_band_bits);
1006
1007
73.4k
        if (top < start_band) {
1008
57.8k
          top = start_band;
1009
57.8k
        }
1010
73.4k
        filter->start_band = top - start_band;
1011
73.4k
        filter->stop_band = top;
1012
1013
73.4k
        top = filter->start_band;
1014
1015
73.4k
        if (filter->start_band < 0) {
1016
0
          filter->order = -1;
1017
0
          return (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_FATAL_TNS_RANGE_ERROR);
1018
0
        }
1019
1020
73.4k
        filter->order = order = ixheaacd_read_bits_buf(it_bit_buff, order_bits);
1021
1022
73.4k
        if ((order - MAX_ORDER_LONG) > 0) {
1023
176
          return (WORD16)(
1024
176
              (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_TNS_ORDER_ERROR);
1025
176
        }
1026
1027
73.2k
        if (order) {
1028
63.8k
          WORD i;
1029
63.8k
          WORD32 coef, coef_compress;
1030
63.8k
          WORD resolution, shift;
1031
1032
63.8k
          filter->direction =
1033
63.8k
              (WORD8)(ixheaacd_read_bits_buf(it_bit_buff, 1) ? -1 : 1);
1034
1035
63.8k
          coef_compress = ixheaacd_read_bits_buf(it_bit_buff, 1);
1036
1037
63.8k
          filter->resolution = coef_res;
1038
1039
63.8k
          resolution = coef_res + 3 - coef_compress;
1040
1041
63.8k
          shift = 32 - resolution;
1042
1043
452k
          for (i = 0; i < order; i++) {
1044
388k
            coef = ixheaacd_read_bits_buf(it_bit_buff, resolution);
1045
388k
            coef = coef << shift;
1046
388k
            filter->coef[i] = (WORD8)(coef >> shift);
1047
388k
          }
1048
63.8k
        }
1049
73.2k
      }
1050
63.4k
    }
1051
159k
  }
1052
51.0k
  return AAC_DEC_OK;
1053
51.2k
}
1054
1055
WORD32 ixheaacd_inv_quant(WORD32 *px_quant, WORD32 *ixheaacd_pow_table_Q13)
1056
1057
20.1k
{
1058
20.1k
  WORD32 q1;
1059
20.1k
  WORD32 temp;
1060
20.1k
  WORD32 q_abs;
1061
20.1k
  WORD16 interp;
1062
20.1k
  WORD32 shift;
1063
1064
20.1k
  q_abs = *px_quant;
1065
1066
20.1k
  if (q_abs > (8191 + 32))
1067
246
    return IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL;
1068
1069
19.8k
  if (q_abs < 1024) {
1070
15.1k
    shift = 3;
1071
15.1k
  } else {
1072
4.73k
    shift = 6;
1073
4.73k
  }
1074
1075
19.8k
  q1 = (q_abs) >> shift;
1076
1077
19.8k
  interp = q_abs - (q1 << shift);
1078
1079
19.8k
  temp = ixheaacd_pow_table_Q13[q1 + 1] - ixheaacd_pow_table_Q13[q1];
1080
1081
19.8k
  temp = (WORD32)(temp * (WORD32)interp);
1082
1083
19.8k
  temp = temp + (ixheaacd_pow_table_Q13[q1] << shift);
1084
1085
19.8k
  if (shift == 3)
1086
15.1k
    temp = temp << 1;
1087
4.73k
  else
1088
4.73k
    temp = temp << 2;
1089
1090
19.8k
  *px_quant = temp;
1091
1092
19.8k
  return 0;
1093
20.1k
}
1094
1095
0
void ixheaacd_scale_value_in_place(WORD32 *value, WORD32 scalefactor) {
1096
0
  WORD32 newscale;
1097
1098
0
  if ((newscale = (scalefactor)) >= 0) {
1099
0
    *(value) <<= newscale;
1100
0
  } else {
1101
0
    *(value) >>= -newscale;
1102
0
  }
1103
0
}
1104
1105
WORD32 ixheaacd_cblock_inv_quant_spect_data(
1106
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
1107
41.6k
    ia_aac_dec_tables_struct *ptr_aac_tables) {
1108
41.6k
  int window, group, grp_win, band;
1109
41.6k
  IA_ERRORCODE err = IA_NO_ERROR;
1110
41.6k
  int sf_bands_transmitted = ptr_aac_dec_channel_info->str_ics_info.max_sfb;
1111
41.6k
  WORD8 *ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
1112
41.6k
  const WORD16 *band_offsets = (WORD16 *)ixheaacd_getscalefactorbandoffsets(
1113
41.6k
      &(ptr_aac_dec_channel_info->str_ics_info), ptr_aac_tables);
1114
41.6k
  WORD32 *ptr_pow_table_Q13 =
1115
41.6k
      (WORD32 *)ptr_aac_tables->pstr_block_tables->ixheaacd_pow_table_Q13;
1116
1117
41.6k
  for (window = 0, group = 0;
1118
96.0k
       group < ptr_aac_dec_channel_info->str_ics_info.num_window_groups;
1119
54.3k
       group++) {
1120
54.3k
    for (grp_win = 0;
1121
180k
         grp_win <
1122
180k
         ptr_aac_dec_channel_info->str_ics_info.window_group_length[group];
1123
126k
         grp_win++, window++) {
1124
1.76M
      for (band = 0; band < sf_bands_transmitted; band++) {
1125
1.63M
        WORD32 *ptr_spec_coef =
1126
1.63M
            SPEC(ptr_aac_dec_channel_info->ptr_spec_coeff, window,
1127
1.63M
                 ptr_aac_dec_channel_info->granule_len) +
1128
1.63M
            band_offsets[band];
1129
1.63M
        int num_lines = band_offsets[band + 1] - band_offsets[band];
1130
1.63M
        int bnds = group * 16 + band;
1131
1.63M
        int i;
1132
1133
1.63M
        if ((ptr_code_book[bnds] == ZERO_HCB) ||
1134
1.58M
            (ptr_code_book[bnds] == INTENSITY_HCB) ||
1135
1.34M
            (ptr_code_book[bnds] == INTENSITY_HCB2))
1136
518k
          continue;
1137
1138
1.12M
        if (ptr_code_book[bnds] == NOISE_HCB) {
1139
143k
          continue;
1140
143k
        }
1141
1142
13.5M
        for (i = 0; i < num_lines; i++) {
1143
12.5M
          WORD8 temp = 0;
1144
12.5M
          WORD32 out1 = ptr_spec_coef[i];
1145
12.5M
          if (out1 <= 0) {
1146
10.1M
            out1 = sub_d(temp, out1);
1147
10.1M
            if (out1 > 127) {
1148
3.26k
              err = ixheaacd_inv_quant(&out1, ptr_pow_table_Q13);
1149
3.26k
              if (err) return err;
1150
3.26k
            } else
1151
10.1M
              out1 = ptr_pow_table_Q13[out1];
1152
10.1M
            ptr_spec_coef[i] = -out1;
1153
1154
10.1M
          } else {
1155
2.36M
            if (out1 > 127) {
1156
3.81k
              err = ixheaacd_inv_quant(&out1, ptr_pow_table_Q13);
1157
3.81k
              if (err) return err;
1158
3.81k
            } else
1159
2.36M
              out1 = ptr_pow_table_Q13[out1];
1160
1161
2.36M
            ptr_spec_coef[i] = out1;
1162
2.36M
          }
1163
12.5M
        }
1164
977k
      }
1165
126k
    }
1166
54.3k
  }
1167
1168
41.6k
  return AAC_DEC_OK;
1169
41.6k
}
1170
1171
56.4k
void ixheaacd_init_ltp_object(ltp_info *ltp) {
1172
56.4k
  ltp->data_present = 0;
1173
56.4k
  ltp->last_band = 0;
1174
1175
56.4k
  ltp->lag_update = 0;
1176
56.4k
  ltp->coef = 0;
1177
56.4k
}
1178
1179
WORD32 ixheaacd_ltp_data(WORD32 object_type, ia_ics_info_struct *ics,
1180
                         ltp_info *ltp, ia_bit_buf_struct *it_bit_buf,
1181
44.9k
                         WORD32 frame_len) {
1182
44.9k
  UWORD8 sfb, w;
1183
1184
44.9k
  if (object_type == AOT_ER_AAC_LD) {
1185
36.8k
    ltp->lag_update = ixheaacd_read_bits_buf(it_bit_buf, 1);
1186
1187
36.8k
    if (ltp->lag_update) {
1188
11.4k
      ltp->lag = (UWORD16)ixheaacd_read_bits_buf(it_bit_buf, 10);
1189
11.4k
    }
1190
36.8k
  } else {
1191
8.11k
    ltp->lag = (UWORD16)ixheaacd_read_bits_buf(it_bit_buf, 11);
1192
8.11k
  }
1193
1194
44.9k
  if (ltp->lag > (frame_len << 1)) return -1;
1195
1196
38.9k
  ltp->coef = (UWORD8)ixheaacd_read_bits_buf(it_bit_buf, 3);
1197
1198
38.9k
  if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
1199
0
    for (w = 0; w < 8; w++) {
1200
0
      if ((ltp->short_used[w] = ixheaacd_read_bits_buf(it_bit_buf, 1)) & 1) {
1201
0
        ltp->short_lag_present[w] = ixheaacd_read_bits_buf(it_bit_buf, 1);
1202
0
        if (ltp->short_lag_present[w]) {
1203
0
          ltp->short_lag[w] = (UWORD8)ixheaacd_read_bits_buf(it_bit_buf, 4);
1204
0
        }
1205
0
      }
1206
0
    }
1207
38.9k
  } else {
1208
38.9k
    ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
1209
1210
848k
    for (sfb = 0; sfb < ltp->last_band; sfb++) {
1211
809k
      ltp->long_used[sfb] = ixheaacd_read_bits_buf(it_bit_buf, 1);
1212
809k
    }
1213
38.9k
  }
1214
38.9k
  if (ics->frame_length == 480) {
1215
3.06k
    if ((ics->sampling_rate_index > 5) &&
1216
169
        (ltp->last_band > MAX_LTP_SFB_SR_FIVE_PLUS_480))
1217
120
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_PLUS_480;
1218
2.94k
    else if ((ics->sampling_rate_index == 5) &&
1219
1.83k
             (ltp->last_band > MAX_LTP_SFB_SR_FIVE_480))
1220
62
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_480;
1221
2.88k
    else if ((ics->sampling_rate_index < 5) &&
1222
1.06k
             (ltp->last_band > MAX_LTP_SFB_SR_FIVE_LESS_480))
1223
86
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_LESS_480;
1224
35.8k
  } else if (ics->frame_length == 512) {
1225
24.7k
    if ((ics->sampling_rate_index > 5) &&
1226
978
        (ltp->last_band > MAX_LTP_SFB_SR_FIVE_PLUS_512))
1227
97
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_PLUS_512;
1228
24.6k
    else if ((ics->sampling_rate_index == 5) &&
1229
22.4k
             (ltp->last_band > MAX_LTP_SFB_SR_FIVE_512))
1230
17
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_512;
1231
24.6k
    else if ((ics->sampling_rate_index < 5) &&
1232
1.31k
             (ltp->last_band > MAX_LTP_SFB_SR_FIVE_LESS_512))
1233
10
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_LESS_512;
1234
24.7k
  }
1235
38.9k
  return 0;
1236
44.9k
}