Coverage Report

Created: 2026-08-31 07:06

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