Coverage Report

Created: 2025-12-08 06:50

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
2.47M
#define SPEC(ptr, w, gl) ((ptr) + ((w) * (gl)))
92
93
UWORD32 ixheaacd_aac_showbits_32(UWORD8 *ptr_read_next, WORD32 cnt_bits,
94
3.41M
                                 WORD32 *increment) {
95
3.41M
  UWORD8 *v = ptr_read_next;
96
3.41M
  UWORD32 b = 0;
97
3.41M
  WORD32 i;
98
3.41M
  WORD32 bumped = 0;
99
100
17.0M
  for (i = 0; i < 4; i++) {
101
13.6M
    b = b << 8;
102
13.6M
    if (cnt_bits > 0) {
103
13.6M
      b = b | *v;
104
13.6M
      v++;
105
13.6M
      bumped++;
106
13.6M
    }
107
13.6M
    cnt_bits -= 8;
108
13.6M
  }
109
3.41M
  if (increment != NULL) {
110
2.26M
    *increment = bumped;
111
2.26M
  }
112
3.41M
  return b;
113
3.41M
}
114
115
WORD16 *ixheaacd_getscalefactorbandoffsets(
116
    ia_ics_info_struct *ptr_ics_info,
117
376k
    ia_aac_dec_tables_struct *ptr_aac_tables) {
118
376k
  if (ptr_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
119
278k
    return ptr_aac_tables->sfb_long_table;
120
278k
  } else {
121
97.5k
    return ptr_aac_tables->sfb_short_table;
122
97.5k
  }
123
376k
}
124
125
WORD8 *ixheaacd_getscalefactorbandwidth(
126
    ia_ics_info_struct *ptr_ics_info,
127
53.2k
    ia_aac_dec_tables_struct *ptr_aac_tables) {
128
53.2k
  if (ptr_ics_info->frame_length == 512) {
129
15.8k
    return (
130
15.8k
        WORD8 *)(&ptr_aac_tables
131
15.8k
                      ->scale_fac_bands_512[ptr_ics_info->sampling_rate_index]
132
15.8k
                                           [0]);
133
37.4k
  } else if (ptr_ics_info->frame_length == 1024) {
134
10.9k
    return (
135
10.9k
        WORD8 *)(&ptr_aac_tables
136
10.9k
                      ->scale_factor_bands_long[ptr_ics_info->sampling_rate_index]
137
10.9k
                                               [0]);
138
26.5k
  } else {
139
26.5k
    return (
140
26.5k
        WORD8 *)(&ptr_aac_tables
141
26.5k
                      ->scale_fac_bands_480[ptr_ics_info->sampling_rate_index]
142
26.5k
                                           [0]);
143
26.5k
  }
144
53.2k
}
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
53.2k
    WORD32 object_type, WORD32 aac_sf_data_resil_flag) {
154
53.2k
  WORD32 *ptr_spect_coeff = ptr_aac_dec_channel_info->ptr_spec_coeff;
155
53.2k
  WORD8 *ptr_sfb_width = (WORD8 *)(ixheaacd_getscalefactorbandwidth(
156
53.2k
      &(ptr_aac_dec_channel_info->str_ics_info), ptr_aac_tables));
157
53.2k
  WORD16 *ptr_scale_fac = ptr_aac_dec_channel_info->ptr_scale_factor;
158
53.2k
  WORD tot_bands = ptr_aac_dec_channel_info->str_ics_info.max_sfb;
159
160
53.2k
  WORD num_win_grp, group_len;
161
53.2k
  WORD32 *ptr_scale_table = ptr_aac_tables->pstr_block_tables->scale_table;
162
53.2k
  ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
163
164
53.2k
  if (object_type == AOT_ER_AAC_LC)
165
31.0k
    ptr_sfb_width = ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence]
166
31.0k
      .sfb_width;
167
132k
  for (num_win_grp = 0; num_win_grp < ptr_ics_info->num_window_groups;
168
78.8k
      num_win_grp++) {
169
78.8k
    for (group_len = 0;
170
277k
        group_len < ptr_ics_info->window_group_length[num_win_grp];
171
199k
        group_len++) {
172
199k
      (*ixheaacd_scale_factor_process)(
173
199k
          &ptr_spect_coeff[0], &ptr_scale_fac[0], tot_bands,
174
199k
          (WORD8 *)ptr_sfb_width, ptr_scale_table, total_channels,
175
199k
          object_type, aac_sf_data_resil_flag);
176
177
199k
      ptr_spect_coeff += MAX_BINS_SHORT;
178
199k
    }
179
78.8k
    ptr_scale_fac += MAX_SCALE_FACTOR_BANDS_SHORT;
180
78.8k
  }
181
53.2k
}
182
183
WORD32 ixheaacd_read_pulse_data(ia_bit_buf_struct *it_bit_buff,
184
                                ia_pulse_info_struct *ptr_pulse_info,
185
48.1k
                                ia_aac_dec_tables_struct *ptr_aac_tables) {
186
48.1k
  WORD32 i, total_offset;
187
48.1k
  WORD32 error_code = 0;
188
189
48.1k
  WORD32 value = ixheaacd_read_bits_buf(it_bit_buff, 8);
190
48.1k
  ptr_pulse_info->number_pulse = value >> 6;
191
48.1k
  ptr_pulse_info->pulse_start_band = value & 0x3F;
192
193
48.1k
  if (ptr_pulse_info->pulse_start_band >= 52) {
194
172
    return (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_PULSEDATA_ERROR;
195
172
  }
196
197
47.9k
  total_offset = ptr_aac_tables->str_aac_sfb_info[0]
198
47.9k
                     .sfb_index[ptr_pulse_info->pulse_start_band];
199
200
154k
  for (i = 0; i < ptr_pulse_info->number_pulse + 1; i++) {
201
106k
    WORD32 value = ixheaacd_read_bits_buf(it_bit_buff, 9);
202
106k
    ptr_pulse_info->pulse_offset[i] = value >> 4;
203
106k
    ptr_pulse_info->pulse_amp[i] = value & 0xF;
204
106k
    total_offset += ptr_pulse_info->pulse_offset[i];
205
206
106k
    if (total_offset >= 1024) {
207
16
      error_code = (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_PULSEDATA_ERROR;
208
16
    }
209
106k
  }
210
211
47.9k
  return error_code;
212
48.1k
}
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
302k
{
223
302k
  FLAG gain_control_data_present;
224
302k
  WORD16 error_code = AAC_DEC_OK;
225
226
302k
  if (ptr_aac_dec_channel_info->str_ics_info.window_sequence ==
227
302k
      EIGHT_SHORT_SEQUENCE) {
228
56.1k
    memset(ptr_aac_dec_channel_info->ptr_scale_factor, 0,
229
56.1k
           MAX_WINDOWS * MAX_SCALE_FACTOR_BANDS_SHORT * 3);
230
56.1k
  }
231
232
302k
  error_code = ixheaacd_read_section_data(
233
302k
      it_bit_buff, ptr_aac_dec_channel_info, aac_spect_data_resil_flag,
234
302k
      aac_sect_data_resil_flag, ptr_aac_tables);
235
236
302k
  if (error_code) {
237
1.90k
    return error_code;
238
1.90k
  }
239
300k
  if (aac_sf_data_resil_flag &&
240
102k
      ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)))
241
67.2k
    ixheaacd_rvlc_read(it_bit_buff, ptr_aac_dec_channel_info);
242
233k
  else
243
233k
    ixheaacd_read_scale_factor_data(it_bit_buff, ptr_aac_dec_channel_info,
244
233k
                                    ptr_aac_tables, object_type);
245
246
300k
  error_code = 0;
247
300k
  if (object_type != AOT_ER_AAC_ELD) {
248
235k
    ptr_aac_dec_channel_info->str_pulse_info.pulse_data_present =
249
235k
        ixheaacd_read_bits_buf(it_bit_buff, 1);
250
235k
    if (ptr_aac_dec_channel_info->str_pulse_info.pulse_data_present) {
251
48.1k
      error_code = ixheaacd_read_pulse_data(
252
48.1k
          it_bit_buff, &ptr_aac_dec_channel_info->str_pulse_info,
253
48.1k
          ptr_aac_tables);
254
48.1k
    }
255
256
235k
    if (error_code) {
257
178
      return error_code;
258
178
    }
259
235k
  }
260
261
300k
  ptr_aac_dec_channel_info->str_tns_info.tns_data_present =
262
300k
      (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
263
264
300k
  if (object_type < ER_OBJECT_START) {
265
86.1k
    error_code = 0;
266
86.1k
    if (ptr_aac_dec_channel_info->str_tns_info.tns_data_present) {
267
27.6k
      error_code =
268
27.6k
          ixheaacd_read_tns_data(it_bit_buff, ptr_aac_dec_channel_info);
269
27.6k
    }
270
271
86.1k
    if (error_code) {
272
29
      return error_code;
273
29
    }
274
86.1k
  }
275
276
300k
  if (object_type != AOT_ER_AAC_ELD) {
277
235k
    gain_control_data_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
278
279
235k
    if (gain_control_data_present) {
280
584
      return (WORD16)(
281
584
          (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_GAIN_CONTROL_DATA_PRESENT);
282
584
    }
283
235k
  }
284
285
300k
  if (object_type == AOT_ER_AAC_ELD) {
286
65.3k
    if (ptr_aac_dec_channel_info->str_tns_info.tns_data_present)
287
7.28k
      error_code =
288
7.28k
          ixheaacd_read_tns_data(it_bit_buff, ptr_aac_dec_channel_info);
289
65.3k
    if (error_code) return error_code;
290
65.3k
  }
291
292
299k
  if (aac_spect_data_resil_flag &&
293
54.1k
      ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)
294
31.6k
      || (object_type == AOT_ER_AAC_LC)))
295
54.1k
    ixheaacd_hcr_read(it_bit_buff, ptr_aac_dec_channel_info, ele_type);
296
297
299k
  if (aac_sf_data_resil_flag &&
298
102k
      ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD))) {
299
67.0k
    error_code = ixheaacd_rvlc_dec(
300
67.0k
        ptr_aac_dec_channel_info, ptr_aac_dec_static_channel_info, it_bit_buff);
301
67.0k
    if (error_code) return error_code;
302
303
66.9k
    it_bit_buff->bit_pos = 7 - it_bit_buff->bit_pos;
304
66.9k
  }
305
306
299k
  if (object_type == AOT_ER_AAC_LD || object_type == AOT_ER_AAC_LC) {
307
148k
    if (ptr_aac_dec_channel_info->str_tns_info.tns_data_present)
308
44.1k
      error_code =
309
44.1k
          ixheaacd_read_tns_data(it_bit_buff, ptr_aac_dec_channel_info);
310
148k
    if (error_code) return error_code;
311
148k
  }
312
313
299k
  { it_bit_buff->bit_pos = 7 - it_bit_buff->bit_pos; }
314
315
299k
  error_code = ixheaacd_read_spectral_data(
316
299k
      it_bit_buff, ptr_aac_dec_channel_info, ptr_aac_tables, total_channels,
317
299k
      frame_size, object_type, aac_spect_data_resil_flag,
318
299k
      aac_sf_data_resil_flag);
319
320
299k
  it_bit_buff->bit_pos = (7 - it_bit_buff->bit_pos);
321
322
299k
  return error_code;
323
299k
}
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
16.4k
                                 WORD32 ch) {
329
16.4k
  IA_ERRORCODE retval = AAC_DEC_OK;
330
331
16.4k
  if (ptr_ics_info->predictor_data_present) {
332
6.41k
    if (ch == 0) {
333
3.21k
      ixheaacd_init_ltp_object(&(ptr_ics_info->ltp));
334
3.21k
      ptr_ics_info->ltp.data_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
335
3.21k
      if (ptr_ics_info->ltp.data_present) {
336
845
        if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
337
845
                                        &(ptr_ics_info->ltp), it_bit_buff,
338
845
                                        frame_size)) > 0) {
339
0
          return retval;
340
0
        }
341
845
      }
342
3.21k
    } else {
343
3.20k
      ixheaacd_init_ltp_object(&(ptr_ics_info->ltp2));
344
3.20k
      ptr_ics_info->ltp2.data_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
345
3.20k
      if (ptr_ics_info->ltp2.data_present) {
346
1.71k
        if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
347
1.71k
                                        &(ptr_ics_info->ltp2), it_bit_buff,
348
1.71k
                                        frame_size)) > 0) {
349
0
          return retval;
350
0
        }
351
1.71k
      }
352
3.20k
    }
353
6.41k
  }
354
16.4k
  return retval;
355
16.4k
}
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
313k
                         WORD32 common_window, WORD32 frame_size) {
360
313k
  WORD i;
361
313k
  WORD mask;
362
313k
  WORD value = 0;
363
364
313k
  if (object_type == AOT_ER_AAC_ELD) {
365
87.9k
    ptr_ics_info->window_sequence = 0;
366
87.9k
    ptr_ics_info->window_shape = 1;
367
225k
  } else {
368
225k
    if (object_type != AOT_ER_AAC_LD)
369
140k
    {
370
140k
      if (frame_size == 960)
371
64.5k
        ptr_ics_info->frame_length = 960;
372
76.2k
      else
373
76.2k
        ptr_ics_info->frame_length = 1024;
374
140k
    }
375
225k
    value = ixheaacd_read_bits_buf(it_bit_buff, 4);
376
225k
    ptr_ics_info->window_sequence = (WORD16)((value & 0x6) >> 1);
377
225k
    ptr_ics_info->window_shape = (WORD16)((value & 0x1));
378
225k
  }
379
380
313k
  if (ptr_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
381
263k
    ptr_ics_info->num_swb_window = num_swb_window[0];
382
383
263k
    ptr_ics_info->num_window_groups = 1;
384
263k
    ptr_ics_info->window_group_length[0] = 1;
385
386
263k
    if (object_type == AOT_ER_AAC_ELD) {
387
87.9k
      ptr_ics_info->max_sfb = ixheaacd_read_bits_buf(it_bit_buff, 6);
388
87.9k
      if (ptr_ics_info->max_sfb == 0) ptr_ics_info->num_swb_window = 0;
389
175k
    } else {
390
175k
      value = ixheaacd_read_bits_buf(it_bit_buff, 7);
391
175k
      ptr_ics_info->max_sfb = (value & 0x7E) >> 1;
392
175k
    }
393
394
263k
    if ((object_type != AOT_ER_AAC_LD) && (object_type != AOT_AAC_LTP)) {
395
163k
      if (value & 1) {
396
449
        return (WORD16)(
397
449
            (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_PREDICTION_DATA_PRESENT);
398
449
      }
399
400
163k
    } else {
401
99.3k
      ptr_ics_info->predictor_data_present = value & 1;
402
403
99.3k
      if (ptr_ics_info->predictor_data_present) {
404
64.1k
        WORD32 retval = AAC_DEC_OK;
405
406
64.1k
        ixheaacd_init_ltp_object(&(ptr_ics_info->ltp));
407
64.1k
        if (object_type < ER_OBJECT_START) {
408
11.6k
          if ((ptr_ics_info->ltp.data_present =
409
11.6k
                   ixheaacd_read_bits_buf(it_bit_buff, 1)) &
410
11.6k
              1) {
411
11.6k
            if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
412
11.6k
                                            &(ptr_ics_info->ltp), it_bit_buff,
413
11.6k
                                            frame_size)) > 0) {
414
0
              return retval;
415
0
            }
416
11.6k
          }
417
11.6k
          if (common_window) {
418
327
            ixheaacd_init_ltp_object(&(ptr_ics_info->ltp2));
419
327
            if ((ptr_ics_info->ltp2.data_present =
420
327
                     ixheaacd_read_bits_buf(it_bit_buff, 1)) &
421
327
                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
327
          }
429
11.6k
        }
430
64.1k
        if ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)) {
431
52.5k
          if (!common_window && (object_type >= ER_OBJECT_START)) {
432
49.2k
            if ((ptr_ics_info->ltp.data_present =
433
49.2k
                     ixheaacd_read_bits_buf(it_bit_buff, 1)) &
434
49.2k
                1) {
435
44.1k
              if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
436
44.1k
                                              &(ptr_ics_info->ltp), it_bit_buff,
437
44.1k
                                              frame_size)) < 0) {
438
3
                return retval;
439
3
              }
440
44.1k
            }
441
49.2k
          }
442
52.5k
        }
443
64.1k
      }
444
99.3k
    }
445
446
263k
  } else {
447
50.4k
    WORD32 num_groups = 0, scale_factor_grouping;
448
50.4k
    ptr_ics_info->num_swb_window = num_swb_window[1];
449
450
50.4k
    value = ixheaacd_read_bits_buf(it_bit_buff, 11);
451
50.4k
    ptr_ics_info->max_sfb = (value & 0x780) >> 7;
452
453
50.4k
    scale_factor_grouping = (value & 0x7F);
454
455
50.4k
    mask = 0x40;
456
403k
    for (i = 0; i < 7; i++) {
457
352k
      ptr_ics_info->window_group_length[i] = 1;
458
459
352k
      if (scale_factor_grouping & mask) {
460
221k
        ptr_ics_info->window_group_length[num_groups] =
461
221k
            ptr_ics_info->window_group_length[num_groups] + 1;
462
463
221k
      } else {
464
131k
        num_groups = num_groups + 1;
465
131k
      }
466
467
352k
      mask = mask >> 1;
468
352k
    }
469
470
50.4k
    ptr_ics_info->window_group_length[7] = 1;
471
50.4k
    ptr_ics_info->num_window_groups = num_groups + 1;
472
50.4k
  }
473
474
313k
  if (ptr_ics_info->max_sfb > ptr_ics_info->num_swb_window) {
475
34.4k
    return (WORD16)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_SFB_TRANSMITTED;
476
34.4k
  }
477
478
278k
  return AAC_DEC_OK;
479
313k
}
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
276k
    ia_eld_specific_config_struct eld_specific_config, WORD32 ele_type) {
485
276k
  WORD16 error_code = AAC_DEC_OK;
486
276k
  WORD32 ch;
487
276k
  WORD32 crc_reg = 0;
488
489
576k
  for (ch = 0; ch < num_ch; ch++) {
490
304k
    ia_aac_dec_channel_info_struct *ptr_aac_dec_ch_info =
491
304k
        aac_dec_handle->pstr_aac_dec_ch_info[ch];
492
304k
    ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_ch_info->str_ics_info;
493
494
304k
    if (ch == 1) {
495
27.5k
      if (it_bit_buff->pstr_adts_crc_info->crc_active == 1 &&
496
44
          (it_bit_buff->pstr_adts_crc_info->no_reg < 7)) {
497
38
        crc_reg =
498
38
            ixheaacd_adts_crc_start_reg(it_bit_buff->pstr_adts_crc_info,
499
38
                                        it_bit_buff, CRC_ADTS_RAW_IIND_ICS);
500
38
      }
501
27.5k
    }
502
304k
    ptr_aac_dec_ch_info->global_gain =
503
304k
        (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 8);
504
505
304k
    if (!(aac_dec_handle->pstr_aac_dec_ch_info[LEFT]->common_window)) {
506
254k
      error_code = ixheaacd_ics_read(
507
254k
          it_bit_buff, ptr_ics_info, aac_dec_handle->num_swb_window,
508
254k
          object_type,
509
254k
          aac_dec_handle->pstr_aac_dec_ch_info[LEFT]->common_window,
510
254k
          aac_dec_handle->samples_per_frame);
511
254k
      if (ch == 1)
512
3.26k
        aac_dec_handle->pstr_aac_dec_ch_info[ch - 1]->str_ics_info.ltp2.lag =
513
3.26k
            ptr_ics_info->ltp.lag;
514
515
254k
      if (error_code) {
516
1.23k
        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
1.23k
        return error_code;
521
1.23k
      }
522
254k
    }
523
524
303k
    error_code = ixheaacd_read_block_data(
525
303k
        it_bit_buff, ptr_aac_dec_ch_info, aac_dec_handle->pstr_aac_tables,
526
303k
        total_channels, frame_size, object_type,
527
303k
        eld_specific_config.aac_spect_data_resil_flag,
528
303k
        eld_specific_config.aac_sect_data_resil_flag,
529
303k
        eld_specific_config.aac_sf_data_resil_flag, ele_type,
530
303k
        aac_dec_handle->pstr_aac_dec_overlap_info[ch]);
531
303k
    if (error_code) {
532
3.44k
      if (it_bit_buff->cnt_bits < 0) {
533
82
        error_code = (WORD16)(
534
82
            (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
535
82
      }
536
537
3.44k
      return error_code;
538
3.44k
    }
539
540
299k
    if (ch == 0) {
541
271k
      if ((object_type == AOT_ER_AAC_LD) &&
542
81.6k
          (aac_dec_handle->pstr_aac_dec_ch_info[LEFT]->common_window) &&
543
8.19k
          (ele_type == ID_CPE)) {
544
8.19k
        IA_ERRORCODE temp =
545
8.19k
            ixheaacd_ltp_decode(it_bit_buff, ptr_ics_info, object_type,
546
8.19k
                                aac_dec_handle->samples_per_frame, 1);
547
548
8.19k
        if (temp != 0) {
549
1
          return temp;
550
1
        }
551
8.19k
        aac_dec_handle->pstr_aac_dec_ch_info[ch + 1]->str_ics_info.ltp.lag =
552
8.19k
            ptr_ics_info->ltp2.lag;
553
8.19k
      }
554
271k
    }
555
299k
    if (ch == 1) {
556
27.1k
      if (it_bit_buff->pstr_adts_crc_info->crc_active == 1) {
557
35
        ixheaacd_adts_crc_end_reg(it_bit_buff->pstr_adts_crc_info, it_bit_buff,
558
35
                                  crc_reg);
559
35
      }
560
27.1k
    }
561
299k
  }
562
563
272k
  return error_code;
564
276k
}
565
566
VOID ixheaacd_read_ms_data(
567
    ia_bit_buf_struct *it_bit_buff,
568
24.9k
    ia_aac_dec_channel_info_struct *ptr_aac_dec_ch_info) {
569
24.9k
  WORD32 num_win_group, sfb;
570
24.9k
  WORD32 ms_mask_present;
571
24.9k
  UWORD8 *ptr_ms_used = &ptr_aac_dec_ch_info->pstr_stereo_info->ms_used[0][0];
572
24.9k
  WORD32 num_window_groups =
573
24.9k
      ptr_aac_dec_ch_info->str_ics_info.num_window_groups;
574
24.9k
  WORD16 max_sfb = ptr_aac_dec_ch_info->str_ics_info.max_sfb;
575
576
24.9k
  ms_mask_present = ixheaacd_read_bits_buf(it_bit_buff, 2);
577
578
24.9k
  if (ms_mask_present < 1) {
579
15.1k
    memset(ptr_ms_used, 0,
580
15.1k
           sizeof(UWORD8) * JOINT_STEREO_MAX_BANDS * JOINT_STEREO_MAX_GROUPS);
581
15.1k
  }
582
583
9.85k
  else if (ms_mask_present == 1) {
584
3.90k
    for (num_win_group = 0; num_win_group < num_window_groups;
585
2.04k
         num_win_group++) {
586
9.47k
      for (sfb = 0; sfb < max_sfb; sfb++) {
587
7.43k
        ptr_aac_dec_ch_info->pstr_stereo_info->ms_used[num_win_group][sfb] =
588
7.43k
            (UWORD8)ixheaacd_read_bits_buf(it_bit_buff, 1);
589
7.43k
      }
590
2.04k
    }
591
592
7.99k
  } else {
593
23.6k
    for (num_win_group = 0; num_win_group < num_window_groups;
594
15.6k
         num_win_group++) {
595
15.6k
      ptr_ms_used =
596
15.6k
          &ptr_aac_dec_ch_info->pstr_stereo_info->ms_used[num_win_group][0];
597
15.6k
      memset(ptr_ms_used, 1, (max_sfb) * sizeof(UWORD8));
598
15.6k
    }
599
7.99k
  }
600
24.9k
}
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
270k
    WORD32 *out_data, void *self_ptr) {
607
270k
  WORD32 channel;
608
270k
  IA_ERRORCODE err = IA_NO_ERROR;
609
270k
  ia_aac_decoder_struct *self = self_ptr;
610
270k
  if (aac_spect_data_resil_flag &&
611
53.0k
      ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_ER_AAC_ELD) ||
612
53.0k
      (object_type == AOT_ER_AAC_LC))) {
613
106k
    for (channel = 0; channel < num_ch; channel++) {
614
53.2k
      err = ixheaacd_cblock_inv_quant_spect_data(
615
53.2k
          ptr_aac_dec_channel_info[channel], ptr_aac_tables);
616
53.2k
      if (err) return err;
617
53.2k
      ixheaacd_cblock_scale_spect_data(ptr_aac_dec_channel_info[channel],
618
53.2k
                                       ptr_aac_tables, num_ch, object_type,
619
53.2k
                                       aac_sf_data_resil_flag);
620
53.2k
    }
621
53.0k
  }
622
623
270k
  if (num_ch > 1) {
624
27.1k
    if (ptr_aac_dec_channel_info[LEFT]->common_window) {
625
23.9k
      if (ptr_aac_dec_channel_info[LEFT]->str_pns_info.pns_active ||
626
21.6k
          ptr_aac_dec_channel_info[RIGHT]->str_pns_info.pns_active) {
627
2.43k
        ixheaacd_map_ms_mask_pns(ptr_aac_dec_channel_info);
628
2.43k
      }
629
630
23.9k
      ixheaacd_ms_stereo_process(ptr_aac_dec_channel_info, ptr_aac_tables);
631
23.9k
    }
632
633
27.1k
    ixheaacd_intensity_stereo_process(
634
27.1k
        ptr_aac_dec_channel_info, ptr_aac_tables, object_type,
635
27.1k
        aac_sf_data_resil_flag,
636
27.1k
        ptr_aac_dec_channel_info[LEFT]->str_ics_info.frame_length);
637
27.1k
  }
638
639
568k
  for (channel = 0; channel < num_ch; channel++) {
640
297k
    WORD32 *p_spectrum = ptr_aac_dec_channel_info[channel]->ptr_spec_coeff;
641
642
297k
    if (total_channels > 2) {
643
11.5k
      if (ptr_aac_dec_channel_info[channel]->str_ics_info.window_sequence !=
644
11.5k
          EIGHT_SHORT_SEQUENCE) {
645
8.14k
        WORD16 *band_offsets = ptr_aac_tables->sfb_long_table;
646
8.14k
        WORD32 no_spec_coeff = band_offsets[ptr_aac_dec_channel_info[channel]
647
8.14k
                                                ->str_ics_info.max_sfb];
648
8.14k
        ixheaacd_right_shift_block(p_spectrum, no_spec_coeff, 3);
649
8.14k
      } else {
650
3.38k
        ixheaacd_right_shift_block(p_spectrum, 1024, 3);
651
3.38k
      }
652
11.5k
    }
653
654
297k
    ixheaacd_pns_process(ptr_aac_dec_channel_info, channel, ptr_aac_tables);
655
656
297k
    if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
657
124k
      {
658
124k
        if (channel == 0) {
659
114k
          ltp_info *ltp1 =
660
114k
              &(ptr_aac_dec_channel_info[channel]->str_ics_info.ltp);
661
114k
          ixheaacd_lt_prediction(ptr_aac_dec_channel_info[channel], ltp1,
662
114k
                                 p_spectrum, ptr_aac_tables,
663
114k
                                 self->ptr_aac_dec_static_channel_info[LEFT]
664
114k
                                     ->overlap_add_data.win_shape,
665
114k
                                 self->sampling_rate_index, object_type,
666
114k
                                 self->samples_per_frame, in_data, out_data);
667
668
114k
        } else {
669
10.6k
          ltp_info *ltp2 =
670
10.6k
              (self->pstr_aac_dec_ch_info[0]->common_window)
671
10.6k
                  ? &(ptr_aac_dec_channel_info[0]->str_ics_info.ltp2)
672
10.6k
                  :
673
674
10.6k
                  &(ptr_aac_dec_channel_info[1]->str_ics_info.ltp);
675
10.6k
          ixheaacd_lt_prediction(ptr_aac_dec_channel_info[channel], ltp2,
676
10.6k
                                 p_spectrum, ptr_aac_tables,
677
10.6k
                                 self->ptr_aac_dec_static_channel_info[RIGHT]
678
10.6k
                                     ->overlap_add_data.win_shape,
679
10.6k
                                 self->sampling_rate_index, object_type,
680
10.6k
                                 self->samples_per_frame, in_data, out_data);
681
10.6k
        }
682
124k
      }
683
124k
    }
684
685
297k
    if (ptr_aac_dec_channel_info[channel]->str_tns_info.tns_data_present) {
686
78.2k
      ixheaacd_aac_tns_process(ptr_aac_dec_channel_info[channel],
687
78.2k
                               total_channels, ptr_aac_tables, object_type, 1,
688
78.2k
                               NULL);
689
78.2k
    }
690
297k
  }
691
270k
  return err;
692
270k
}
693
694
VOID ixheaacd_set_corr_info(
695
48.7k
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info, WORD16 pns_band) {
696
48.7k
  ia_pns_correlation_info_struct *ptr_corr_info =
697
48.7k
      ptr_aac_dec_channel_info->pstr_pns_corr_info;
698
48.7k
  ptr_corr_info->correlated[(pns_band >> PNS_BAND_FLAGS_SHIFT)] |=
699
48.7k
      (1 << (pns_band & PNS_BAND_FLAGS_MASK));
700
48.7k
}
701
702
VOID ixheaacd_map_ms_mask_pns(
703
2.43k
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info[CHANNELS]) {
704
2.43k
  WORD32 num_win_group, sfb;
705
706
2.43k
  for (num_win_group = 0;
707
4.92k
       num_win_group <
708
4.92k
       ptr_aac_dec_channel_info[LEFT]->str_ics_info.num_window_groups;
709
2.49k
       num_win_group++) {
710
53.3k
    for (sfb = 0; sfb < ptr_aac_dec_channel_info[LEFT]->str_ics_info.max_sfb;
711
50.8k
         sfb++) {
712
50.8k
      if (ptr_aac_dec_channel_info[LEFT]
713
50.8k
              ->pstr_stereo_info->ms_used[num_win_group][sfb]) {
714
48.7k
        WORD16 pns_band = (num_win_group << 4) + sfb;
715
48.7k
        ixheaacd_set_corr_info(ptr_aac_dec_channel_info[LEFT], pns_band);
716
717
48.7k
        if (ptr_aac_dec_channel_info[LEFT]->str_pns_info.pns_used[pns_band] &&
718
31.3k
            ptr_aac_dec_channel_info[RIGHT]->str_pns_info.pns_used[pns_band]) {
719
19.7k
          ptr_aac_dec_channel_info[LEFT]
720
19.7k
              ->pstr_stereo_info->ms_used[num_win_group][sfb] ^= 1;
721
19.7k
        }
722
48.7k
      }
723
50.8k
    }
724
2.49k
  }
725
2.43k
}
726
727
VOID ixheaacd_pulse_data_apply(ia_pulse_info_struct *ptr_pulse_info,
728
                               WORD8 *pulse_scratch,
729
210k
                               const WORD16 *ptr_swb_offset, WORD object_type) {
730
210k
  WORD i;
731
210k
  WORD32 k;
732
733
210k
  memset(pulse_scratch, 0, sizeof(WORD32) * 256);
734
735
210k
  if (object_type != AOT_ER_AAC_ELD) {
736
148k
    if (ptr_pulse_info->pulse_data_present) {
737
33.9k
      k = ptr_swb_offset[ptr_pulse_info->pulse_start_band];
738
739
108k
      for (i = 0; i <= ptr_pulse_info->number_pulse; i++) {
740
74.6k
        k = k + ptr_pulse_info->pulse_offset[i];
741
74.6k
        pulse_scratch[k] = ptr_pulse_info->pulse_amp[i];
742
74.6k
      }
743
33.9k
    }
744
148k
  } else {
745
61.9k
    ptr_pulse_info->pulse_data_present = 0;
746
61.9k
  }
747
210k
}
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
299k
    WORD32 aac_sf_data_resil_flag) {
755
299k
  WORD sfb, max_sfb;
756
299k
  WORD num_win_grp, group_len, grp_offset;
757
758
299k
  WORD index;
759
299k
  WORD8 *ptr_code_book;
760
299k
  WORD16 *ptr_scale_factor;
761
299k
  WORD32 *ptr_spec_coef;
762
299k
  ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
763
299k
  WORD16 *swb_offset;
764
765
299k
  WORD32 maximum_bins_short = ptr_ics_info->frame_length >> 3;
766
767
299k
  WORD32 *ptr_spec_coef_out;
768
769
299k
  ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
770
299k
  ptr_scale_factor = ptr_aac_dec_channel_info->ptr_scale_factor;
771
299k
  ptr_spec_coef = ptr_aac_dec_channel_info->ptr_spec_coeff;
772
299k
  max_sfb = ptr_ics_info->max_sfb;
773
774
299k
  swb_offset =
775
299k
      ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence].sfb_index;
776
777
299k
  if (!aac_spect_data_resil_flag) {
778
245k
    if (ptr_aac_dec_channel_info->str_ics_info.window_sequence !=
779
245k
        EIGHT_SHORT_SEQUENCE) {
780
210k
      WORD8 *ptr_scratch;
781
782
210k
      if (object_type == AOT_ER_AAC_ELD)
783
61.9k
        ptr_scratch = (WORD8 *)ptr_aac_dec_channel_info->pulse_scratch;
784
148k
      else
785
148k
        ptr_scratch = (WORD8 *)ptr_aac_dec_channel_info->scratch_buf_ptr;
786
787
210k
      memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
788
789
210k
      ixheaacd_pulse_data_apply(&ptr_aac_dec_channel_info->str_pulse_info,
790
210k
                                ptr_scratch, swb_offset, object_type);
791
792
210k
      ptr_spec_coef_out = &ptr_spec_coef[0];
793
476k
      for (sfb = 0; sfb < max_sfb;) {
794
265k
        WORD ret_val;
795
265k
        WORD32 sfb_width;
796
265k
        WORD32 sect_cb = ptr_code_book[sfb];
797
265k
        WORD start = sfb;
798
265k
        if ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)
799
173k
            || (object_type == AOT_ER_AAC_LC)) {
800
173k
          if ((sect_cb >= 16) && (sect_cb <= 31)) {
801
66.1k
            ptr_code_book[sfb] = sect_cb = 11;
802
66.1k
          }
803
173k
        }
804
2.61M
        for (; sfb < max_sfb && (ptr_code_book[sfb] == sect_cb); sfb++)
805
2.34M
          ;
806
807
265k
        sfb_width = swb_offset[sfb] - swb_offset[start];
808
809
265k
        if (sect_cb > ZERO_HCB && (sect_cb < NOISE_HCB)) {
810
188k
          ret_val = ixheaacd_huffman_dec_word2(it_bit_buff, sect_cb, sfb_width,
811
188k
                                               ptr_aac_tables,
812
188k
                                               ptr_spec_coef_out, ptr_scratch);
813
814
188k
          if (ret_val != 0) {
815
188
            return (WORD16)(
816
188
                (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
817
188
          }
818
188k
        }
819
820
77.2k
        else {
821
77.2k
          if (ptr_aac_dec_channel_info->str_pulse_info.pulse_data_present) {
822
31.8k
            ixheaacd_inverse_quantize(
823
31.8k
                ptr_spec_coef_out, sfb_width,
824
31.8k
                (WORD32 *)
825
31.8k
                    ptr_aac_tables->pstr_block_tables->ixheaacd_pow_table_Q13,
826
31.8k
                ptr_scratch);
827
31.8k
          }
828
77.2k
        }
829
265k
        ptr_scratch += sfb_width;
830
265k
        ptr_spec_coef_out += sfb_width;
831
265k
      }
832
833
210k
      if ((object_type != AOT_ER_AAC_ELD) && (object_type != AOT_ER_AAC_LD))
834
95.4k
        index = 1024 - swb_offset[max_sfb];
835
115k
      else
836
115k
        index = frame_size - swb_offset[max_sfb];
837
838
210k
      if (index < 0) return -1;
839
840
210k
    } else {
841
34.5k
      memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
842
843
34.5k
      grp_offset = 0;
844
845
209k
      for (num_win_grp = 0; num_win_grp < ptr_ics_info->num_window_groups;
846
175k
           num_win_grp++) {
847
175k
        WORD grp_len = ptr_ics_info->window_group_length[num_win_grp];
848
849
175k
        if (maximum_bins_short == 120)
850
49.0k
          ptr_spec_coef_out = &ptr_spec_coef[grp_offset * maximum_bins_short];
851
126k
        else
852
126k
          ptr_spec_coef_out = &ptr_spec_coef[grp_offset * MAX_BINS_SHORT];
853
854
175k
        WORD bands = num_win_grp * MAX_SCALE_FACTOR_BANDS_SHORT;
855
274k
        for (sfb = 0; sfb < max_sfb;) {
856
99.2k
          WORD sect_cb = ptr_code_book[bands];
857
99.2k
          WORD start = sfb;
858
99.2k
          WORD ret_val;
859
860
99.2k
          if ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)
861
99.2k
              || (object_type == AOT_ER_AAC_LC)) {
862
84.4k
            if ((sect_cb >= 16) && (sect_cb <= 31)) {
863
63.3k
              ptr_code_book[bands] = sect_cb = 11;
864
63.3k
            }
865
84.4k
          }
866
867
314k
          for (; sfb < max_sfb && (ptr_code_book[bands] == sect_cb);
868
215k
               sfb++, bands++)
869
215k
            ;
870
871
99.2k
          if (sect_cb > ZERO_HCB && (sect_cb < NOISE_HCB)) {
872
80.6k
            ret_val = ixheaacd_decode_huffman(
873
80.6k
                it_bit_buff, sect_cb, ptr_spec_coef_out, (WORD16 *)swb_offset,
874
80.6k
                start, sfb, grp_len, ptr_aac_tables, maximum_bins_short);
875
876
80.6k
            if (ret_val != 0) {
877
50
              return (WORD16)(
878
50
                  (WORD32)
879
50
                      IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
880
50
            }
881
80.6k
          }
882
99.2k
        }
883
175k
        grp_offset = (grp_offset + grp_len);
884
175k
      }
885
34.5k
    }
886
245k
    {
887
245k
      WORD32 *ptr_scale_table;
888
889
245k
      if (maximum_bins_short != 120)
890
198k
        ptr_scale_table = ptr_aac_tables->pstr_block_tables->scale_table;
891
46.6k
      else
892
46.6k
        ptr_scale_table = ptr_aac_tables->pstr_block_tables->scale_table_960;
893
894
245k
      WORD8 *ptr_sfb_width =
895
245k
          ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence]
896
245k
              .sfb_width;
897
898
631k
      for (num_win_grp = 0; num_win_grp < ptr_ics_info->num_window_groups;
899
385k
           num_win_grp++) {
900
385k
        for (group_len = 0;
901
872k
             group_len < ptr_ics_info->window_group_length[num_win_grp];
902
486k
             group_len++) {
903
486k
          (*ixheaacd_scale_factor_process)(
904
486k
              &ptr_spec_coef[0], &ptr_scale_factor[0], max_sfb,
905
486k
              (WORD8 *)ptr_sfb_width, ptr_scale_table, total_channels,
906
486k
              object_type, aac_sf_data_resil_flag);
907
908
486k
          if (maximum_bins_short == 120)
909
104k
            ptr_spec_coef += maximum_bins_short;
910
382k
          else
911
382k
            ptr_spec_coef += MAX_BINS_SHORT;
912
486k
        }
913
914
385k
        ptr_scale_factor += MAX_SCALE_FACTOR_BANDS_SHORT;
915
385k
      }
916
245k
    }
917
245k
  } else {
918
54.1k
    ia_hcr_info_struct *pstr_hcr_info = &ptr_aac_dec_channel_info->str_hcr_info;
919
54.1k
    WORD32 error = 0;
920
921
54.1k
    memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
922
923
54.1k
    if (ptr_aac_dec_channel_info->reorder_spect_data_len != 0) {
924
43.4k
      error = ixheaacd_huff_code_reorder_init(
925
43.4k
          pstr_hcr_info, ptr_aac_dec_channel_info, ptr_aac_tables, it_bit_buff);
926
927
43.4k
      if (error != 0) {
928
284
        return IA_XHEAAC_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
929
284
      }
930
43.1k
      error = ixheaacd_hcr_decoder(pstr_hcr_info, ptr_aac_dec_channel_info,
931
43.1k
                                   ptr_aac_tables, it_bit_buff);
932
933
43.1k
      if (error != 0) {
934
37.4k
        ixheaacd_huff_mute_erroneous_lines(pstr_hcr_info);
935
37.4k
      }
936
937
43.1k
      if (it_bit_buff->cnt_bits <
938
43.1k
          ptr_aac_dec_channel_info->reorder_spect_data_len) {
939
474
        longjmp(*(it_bit_buff->xaac_jmp_buf),
940
474
                IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
941
474
      }
942
943
42.6k
      it_bit_buff->cnt_bits +=
944
42.6k
          -ptr_aac_dec_channel_info->reorder_spect_data_len;
945
42.6k
      it_bit_buff->ptr_read_next =
946
42.6k
          it_bit_buff->ptr_bit_buf_base +
947
42.6k
          ((it_bit_buff->size - it_bit_buff->cnt_bits) >> 3);
948
42.6k
      it_bit_buff->bit_pos = (it_bit_buff->size - it_bit_buff->cnt_bits) & 7;
949
950
42.6k
    } else {
951
10.7k
      it_bit_buff->ptr_read_next =
952
10.7k
          it_bit_buff->ptr_bit_buf_base +
953
10.7k
          ((it_bit_buff->size - it_bit_buff->cnt_bits) >> 3);
954
10.7k
      it_bit_buff->bit_pos = (it_bit_buff->size - it_bit_buff->cnt_bits) & 7;
955
10.7k
    }
956
54.1k
  }
957
958
298k
  return AAC_DEC_OK;
959
299k
}
960
961
WORD16 ixheaacd_read_tns_data(
962
    ia_bit_buf_struct *it_bit_buff,
963
79.0k
    ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info) {
964
79.0k
  WORD win_size, window_per_frame;
965
79.0k
  WORD n_filt_bits, start_band_bits, order_bits;
966
79.0k
  WORD32 bottom;
967
968
79.0k
  ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
969
79.0k
  ia_tns_info_aac_struct *ptr_tns_info =
970
79.0k
      &ptr_aac_dec_channel_info->str_tns_info;
971
972
79.0k
  if (ptr_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
973
54.4k
    n_filt_bits = 2;
974
54.4k
    start_band_bits = 6;
975
54.4k
    order_bits = 5;
976
54.4k
    window_per_frame = 1;
977
978
54.4k
  } else {
979
24.6k
    n_filt_bits = 1;
980
24.6k
    start_band_bits = 4;
981
24.6k
    order_bits = 3;
982
24.6k
    window_per_frame = 8;
983
24.6k
  }
984
985
79.0k
  bottom = ptr_ics_info->num_swb_window;
986
987
330k
  for (win_size = 0; win_size < window_per_frame; win_size++) {
988
251k
    WORD n_filt;
989
251k
    WORD start_band, coef_res;
990
251k
    ptr_tns_info->n_filt[win_size] = n_filt =
991
251k
        (WORD16)ixheaacd_read_bits_buf(it_bit_buff, n_filt_bits);
992
993
251k
    if (n_filt) {
994
98.1k
      WORD filt;
995
98.1k
      WORD top;
996
997
98.1k
      coef_res = ixheaacd_read_bits_buf(it_bit_buff, 1);
998
999
98.1k
      top = bottom;
1000
212k
      for (filt = 0; filt < n_filt; filt++) {
1001
114k
        WORD order;
1002
114k
        ia_filter_info_struct *filter =
1003
114k
            &ptr_tns_info->str_filter[win_size][filt];
1004
1005
114k
        start_band = ixheaacd_read_bits_buf(it_bit_buff, start_band_bits);
1006
1007
114k
        if (top < start_band) {
1008
82.6k
          top = start_band;
1009
82.6k
        }
1010
114k
        filter->start_band = top - start_band;
1011
114k
        filter->stop_band = top;
1012
1013
114k
        top = filter->start_band;
1014
1015
114k
        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
114k
        filter->order = order = ixheaacd_read_bits_buf(it_bit_buff, order_bits);
1021
1022
114k
        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
114k
        if (order) {
1028
97.4k
          WORD i;
1029
97.4k
          WORD32 coef, coef_compress;
1030
97.4k
          WORD resolution, shift;
1031
1032
97.4k
          filter->direction =
1033
97.4k
              (WORD8)(ixheaacd_read_bits_buf(it_bit_buff, 1) ? -1 : 1);
1034
1035
97.4k
          coef_compress = ixheaacd_read_bits_buf(it_bit_buff, 1);
1036
1037
97.4k
          filter->resolution = coef_res;
1038
1039
97.4k
          resolution = coef_res + 3 - coef_compress;
1040
1041
97.4k
          shift = 32 - resolution;
1042
1043
670k
          for (i = 0; i < order; i++) {
1044
573k
            coef = ixheaacd_read_bits_buf(it_bit_buff, resolution);
1045
573k
            coef = coef << shift;
1046
573k
            filter->coef[i] = (WORD8)(coef >> shift);
1047
573k
          }
1048
97.4k
        }
1049
114k
      }
1050
98.1k
    }
1051
251k
  }
1052
78.8k
  return AAC_DEC_OK;
1053
79.0k
}
1054
1055
WORD32 ixheaacd_inv_quant(WORD32 *px_quant, WORD32 *ixheaacd_pow_table_Q13)
1056
1057
31.8k
{
1058
31.8k
  WORD32 q1;
1059
31.8k
  WORD32 temp;
1060
31.8k
  WORD32 q_abs;
1061
31.8k
  WORD16 interp;
1062
31.8k
  WORD32 shift;
1063
1064
31.8k
  q_abs = *px_quant;
1065
1066
31.8k
  if (q_abs > (8191 + 32))
1067
169
    return IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL;
1068
1069
31.7k
  if (q_abs < 1024) {
1070
24.6k
    shift = 3;
1071
24.6k
  } else {
1072
7.09k
    shift = 6;
1073
7.09k
  }
1074
1075
31.7k
  q1 = (q_abs) >> shift;
1076
1077
31.7k
  interp = q_abs - (q1 << shift);
1078
1079
31.7k
  temp = ixheaacd_pow_table_Q13[q1 + 1] - ixheaacd_pow_table_Q13[q1];
1080
1081
31.7k
  temp = (WORD32)(temp * (WORD32)interp);
1082
1083
31.7k
  temp = temp + (ixheaacd_pow_table_Q13[q1] << shift);
1084
1085
31.7k
  if (shift == 3)
1086
24.6k
    temp = temp << 1;
1087
7.09k
  else
1088
7.09k
    temp = temp << 2;
1089
1090
31.7k
  *px_quant = temp;
1091
1092
31.7k
  return 0;
1093
31.8k
}
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
53.2k
    ia_aac_dec_tables_struct *ptr_aac_tables) {
1108
53.2k
  int window, group, grp_win, band;
1109
53.2k
  IA_ERRORCODE err = IA_NO_ERROR;
1110
53.2k
  int sf_bands_transmitted = ptr_aac_dec_channel_info->str_ics_info.max_sfb;
1111
53.2k
  WORD8 *ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
1112
53.2k
  const WORD16 *band_offsets = (WORD16 *)ixheaacd_getscalefactorbandoffsets(
1113
53.2k
      &(ptr_aac_dec_channel_info->str_ics_info), ptr_aac_tables);
1114
53.2k
  WORD32 *ptr_pow_table_Q13 =
1115
53.2k
      (WORD32 *)ptr_aac_tables->pstr_block_tables->ixheaacd_pow_table_Q13;
1116
1117
53.2k
  for (window = 0, group = 0;
1118
132k
       group < ptr_aac_dec_channel_info->str_ics_info.num_window_groups;
1119
78.8k
       group++) {
1120
78.8k
    for (grp_win = 0;
1121
277k
         grp_win <
1122
277k
         ptr_aac_dec_channel_info->str_ics_info.window_group_length[group];
1123
199k
         grp_win++, window++) {
1124
2.67M
      for (band = 0; band < sf_bands_transmitted; band++) {
1125
2.47M
        WORD32 *ptr_spec_coef =
1126
2.47M
            SPEC(ptr_aac_dec_channel_info->ptr_spec_coeff, window,
1127
2.47M
                 ptr_aac_dec_channel_info->granule_len) +
1128
2.47M
            band_offsets[band];
1129
2.47M
        int num_lines = band_offsets[band + 1] - band_offsets[band];
1130
2.47M
        int bnds = group * 16 + band;
1131
2.47M
        int i;
1132
1133
2.47M
        if ((ptr_code_book[bnds] == ZERO_HCB) ||
1134
2.40M
            (ptr_code_book[bnds] == INTENSITY_HCB) ||
1135
2.08M
            (ptr_code_book[bnds] == INTENSITY_HCB2))
1136
663k
          continue;
1137
1138
1.81M
        if (ptr_code_book[bnds] == NOISE_HCB) {
1139
105k
          continue;
1140
105k
        }
1141
1142
20.1M
        for (i = 0; i < num_lines; i++) {
1143
18.4M
          WORD8 temp = 0;
1144
18.4M
          WORD32 out1 = ptr_spec_coef[i];
1145
18.4M
          if (out1 <= 0) {
1146
14.6M
            out1 = sub_d(temp, out1);
1147
14.6M
            if (out1 > 127) {
1148
7.72k
              err = ixheaacd_inv_quant(&out1, ptr_pow_table_Q13);
1149
7.72k
              if (err) return err;
1150
7.72k
            } else
1151
14.6M
              out1 = ptr_pow_table_Q13[out1];
1152
14.6M
            ptr_spec_coef[i] = -out1;
1153
1154
14.6M
          } else {
1155
3.82M
            if (out1 > 127) {
1156
8.25k
              err = ixheaacd_inv_quant(&out1, ptr_pow_table_Q13);
1157
8.25k
              if (err) return err;
1158
8.25k
            } else
1159
3.81M
              out1 = ptr_pow_table_Q13[out1];
1160
1161
3.82M
            ptr_spec_coef[i] = out1;
1162
3.82M
          }
1163
18.4M
        }
1164
1.70M
      }
1165
199k
    }
1166
78.8k
  }
1167
1168
53.2k
  return AAC_DEC_OK;
1169
53.2k
}
1170
1171
70.9k
void ixheaacd_init_ltp_object(ltp_info *ltp) {
1172
70.9k
  ltp->data_present = 0;
1173
70.9k
  ltp->last_band = 0;
1174
1175
70.9k
  ltp->lag_update = 0;
1176
70.9k
  ltp->coef = 0;
1177
70.9k
}
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
58.3k
                         WORD32 frame_len) {
1182
58.3k
  UWORD8 sfb, w;
1183
1184
58.3k
  if (object_type == AOT_ER_AAC_LD) {
1185
46.7k
    ltp->lag_update = ixheaacd_read_bits_buf(it_bit_buf, 1);
1186
1187
46.7k
    if (ltp->lag_update) {
1188
14.2k
      ltp->lag = (UWORD16)ixheaacd_read_bits_buf(it_bit_buf, 10);
1189
14.2k
    }
1190
46.7k
  } else {
1191
11.6k
    ltp->lag = (UWORD16)ixheaacd_read_bits_buf(it_bit_buf, 11);
1192
11.6k
  }
1193
1194
58.3k
  if (ltp->lag > (frame_len << 1)) return -1;
1195
1196
49.0k
  ltp->coef = (UWORD8)ixheaacd_read_bits_buf(it_bit_buf, 3);
1197
1198
49.0k
  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
49.0k
  } else {
1208
49.0k
    ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
1209
1210
1.12M
    for (sfb = 0; sfb < ltp->last_band; sfb++) {
1211
1.07M
      ltp->long_used[sfb] = ixheaacd_read_bits_buf(it_bit_buf, 1);
1212
1.07M
    }
1213
49.0k
  }
1214
49.0k
  if (ics->frame_length == 480) {
1215
3.57k
    if ((ics->sampling_rate_index > 5) &&
1216
128
        (ltp->last_band > MAX_LTP_SFB_SR_FIVE_PLUS_480))
1217
64
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_PLUS_480;
1218
3.51k
    else if ((ics->sampling_rate_index == 5) &&
1219
1.45k
             (ltp->last_band > MAX_LTP_SFB_SR_FIVE_480))
1220
36
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_480;
1221
3.47k
    else if ((ics->sampling_rate_index < 5) &&
1222
1.99k
             (ltp->last_band > MAX_LTP_SFB_SR_FIVE_LESS_480))
1223
10
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_LESS_480;
1224
45.5k
  } else if (ics->frame_length == 512) {
1225
30.3k
    if ((ics->sampling_rate_index > 5) &&
1226
693
        (ltp->last_band > MAX_LTP_SFB_SR_FIVE_PLUS_512))
1227
55
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_PLUS_512;
1228
30.2k
    else if ((ics->sampling_rate_index == 5) &&
1229
28.5k
             (ltp->last_band > MAX_LTP_SFB_SR_FIVE_512))
1230
44
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_512;
1231
30.2k
    else if ((ics->sampling_rate_index < 5) &&
1232
1.14k
             (ltp->last_band > MAX_LTP_SFB_SR_FIVE_LESS_512))
1233
10
      ltp->last_band = MAX_LTP_SFB_SR_FIVE_LESS_512;
1234
30.3k
  }
1235
49.0k
  return 0;
1236
58.3k
}