Coverage Report

Created: 2026-12-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/decoder/ixheaacd_decode_main.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 <stdlib.h>
21
#include <string.h>
22
#include "ixheaac_type_def.h"
23
#include "ixheaac_error_standards.h"
24
#include "ixheaacd_memory_standards.h"
25
#include "ixheaacd_sbrdecsettings.h"
26
#include "ixheaacd_sbr_scale.h"
27
#include "ixheaacd_env_extr_part.h"
28
#include "ixheaacd_defines.h"
29
#include "ixheaacd_aac_rom.h"
30
#include "ixheaacd_common_rom.h"
31
#include "ixheaacd_sbr_rom.h"
32
#include "ixheaacd_bitbuffer.h"
33
#include "ixheaacd_pulsedata.h"
34
#include "ixheaacd_pns.h"
35
#include "ixheaacd_interface.h"
36
#include "ixheaacd_info.h"
37
#include "ixheaacd_lt_predict.h"
38
#include "ixheaacd_cnst.h"
39
#include "ixheaacd_ec_defines.h"
40
#include "ixheaacd_ec_struct_def.h"
41
#include "ixheaacd_channelinfo.h"
42
#include "ixheaacd_sbr_common.h"
43
#include "ixheaacd_drc_data_struct.h"
44
#include "ixheaacd_drc_dec.h"
45
#include "ixheaacd_channel.h"
46
#include "ixheaacd_sbrdecoder.h"
47
#include "ixheaacd_audioobjtypes.h"
48
#include "ixheaacd_latmdemux.h"
49
#include "ixheaacd_aacdec.h"
50
#include "ixheaacd_sbr_common.h"
51
#include "ixheaacd_hybrid.h"
52
#include "ixheaacd_ps_dec.h"
53
#include "ixheaacd_mps_polyphase.h"
54
#include "ixheaacd_config.h"
55
#include "ixheaacd_qmf_dec.h"
56
#include "ixheaacd_mps_macro_def.h"
57
#include "ixheaacd_mps_struct_def.h"
58
#include "ixheaacd_mps_res_rom.h"
59
#include "ixheaacd_mps_aac_struct.h"
60
#include "ixheaac_constants.h"
61
#include "ixheaacd_mps_dec.h"
62
#include "ixheaacd_struct_def.h"
63
#include "ixheaacd_bitbuffer.h"
64
#include "ixheaacd_interface.h"
65
#include "ixheaacd_tns_usac.h"
66
67
#include "ixheaacd_acelp_info.h"
68
#include "ixheaacd_sbrdecsettings.h"
69
#include "ixheaacd_info.h"
70
#include "ixheaacd_sbrdecoder.h"
71
#include "ixheaacd_mps_polyphase.h"
72
#include "ixheaac_sbr_const.h"
73
#include "ixheaacd_main.h"
74
#include "ixheaacd_arith_dec.h"
75
#include "ixheaacd_config.h"
76
#include "ixheaacd_struct.h"
77
#include "ixheaacd_create.h"
78
#include "ixheaacd_dec_main.h"
79
#include "ixheaac_error_standards.h"
80
#include "ixheaacd_headerdecode.h"
81
#include "ixheaacd_error_codes.h"
82
VOID ixheaacd_samples_sat(WORD8 *outbuffer, WORD32 num_samples_out,
83
                          WORD32 pcmsize, FLOAT32 (*out_samples)[4096],
84
315k
                          WORD32 *out_bytes, WORD32 num_channel_out) {
85
315k
  WORD32 num;
86
315k
  WORD32 i;
87
315k
  WORD32 write_local;
88
315k
  FLOAT32 write_local_float;
89
90
315k
  WORD16 *out_buf = (WORD16 *)outbuffer;
91
92
315k
  num = num_channel_out * num_samples_out;
93
94
315k
  if (pcmsize == 16) {
95
1.20G
    for (i = 0; i < num; i++) {
96
1.20G
      write_local_float =
97
1.20G
          (out_samples[i % num_channel_out][i / num_channel_out]);
98
99
1.20G
      if (write_local_float > 32767.0f) {
100
26.1M
        write_local_float = 32767.0f;
101
1.17G
      } else if (write_local_float < -32768.0f) {
102
35.4M
        write_local_float = -32768.0f;
103
35.4M
      }
104
1.20G
      out_buf[i] = (WORD16)write_local_float;
105
1.20G
    }
106
107
315k
    *out_bytes = num * sizeof(WORD16);
108
315k
  } else {
109
0
    WORD8 *out_24bit = (WORD8 *)out_buf;
110
0
    for (i = 0; i < num; i++) {
111
0
      write_local_float =
112
0
          (out_samples[i % num_channel_out][i / num_channel_out] * 256);
113
114
0
      if (write_local_float > 8388607.0f) {
115
0
        write_local_float = 8388607.0f;
116
0
      } else if (write_local_float < -8388608.0f) {
117
0
        write_local_float = -8388608.0f;
118
0
      }
119
0
      write_local = (WORD32)write_local_float;
120
121
0
      *out_24bit++ = (WORD32)write_local & 0xff;
122
0
      *out_24bit++ = ((WORD32)write_local >> 8) & 0xff;
123
0
      *out_24bit++ = ((WORD32)write_local >> 16) & 0xff;
124
0
    }
125
126
0
    *out_bytes = num * 3 * sizeof(WORD8);
127
0
  }
128
315k
}
129
130
VOID ixheaacd_samples_sat_mc(WORD8* outbuffer, WORD32 num_samples_out,
131
    FLOAT32(*out_samples)[4096], WORD32* out_bytes,
132
308
    WORD32 num_channel_out, WORD32 ch_fac) {
133
308
  WORD32 num;
134
308
  WORD32 i;
135
308
  FLOAT32 write_local_float;
136
137
308
  WORD16* out_buf = (WORD16*)outbuffer;
138
139
308
  num = num_channel_out * num_samples_out;
140
308
  if (num_channel_out == 1) {
141
479k
    for (i = 0; i < num; i++) {
142
479k
      write_local_float =
143
479k
          (out_samples[i % num_channel_out][i / num_channel_out]);
144
145
479k
      if (write_local_float > 32767.0f) {
146
213
        write_local_float = 32767.0f;
147
479k
      } else if (write_local_float < -32768.0f) {
148
0
        write_local_float = -32768.0f;
149
0
      }
150
479k
      out_buf[i * ch_fac] = (WORD16)write_local_float;
151
479k
    }
152
234
  } else if (num_channel_out == 2) {
153
151k
    for (i = 0; i < num_samples_out; i++) {
154
151k
      write_local_float =
155
151k
          (out_samples[(2*i) % num_channel_out][(2 * i) / num_channel_out]);
156
157
151k
      if (write_local_float > 32767.0f) {
158
0
          write_local_float = 32767.0f;
159
151k
      } else if (write_local_float < -32768.0f) {
160
0
          write_local_float = -32768.0f;
161
0
      }
162
151k
      out_buf[i * ch_fac] = (WORD16)write_local_float;
163
164
151k
      write_local_float =
165
151k
          (out_samples[((2 * i) + 1) % num_channel_out][((2 * i) + 1) / num_channel_out]);
166
167
151k
      if (write_local_float > 32767.0f) {
168
0
          write_local_float = 32767.0f;
169
151k
      } else if (write_local_float < -32768.0f) {
170
0
          write_local_float = -32768.0f;
171
0
      }
172
151k
      out_buf[i * ch_fac + 1] = (WORD16)write_local_float;
173
151k
    }
174
74
  }
175
308
  *out_bytes = num * sizeof(WORD16);
176
308
}
177
178
/* audio pre roll frame parsing*/
179
static WORD32 ixheaacd_audio_preroll_parsing(
180
    ia_dec_data_struct *pstr_dec_data, UWORD8 *conf_buf, WORD32 *preroll_units,
181
    WORD32 *preroll_frame_offset, ia_aac_dec_state_struct *aac_dec_handle,
182
29.6k
    WORD32 *config_changed, WORD32 *apply_crossfade) {
183
29.6k
  ia_bit_buf_struct *temp_buff =
184
29.6k
      (ia_bit_buf_struct *)&(pstr_dec_data->dec_bit_buf);
185
186
29.6k
  WORD32 ext_ele_present = 0;
187
29.6k
  WORD32 ext_ele_use_dflt_len = 0;
188
29.6k
  WORD32 ext_ele_payload_len = 0;
189
29.6k
  WORD32 num_pre_roll_frames = 0;
190
191
29.6k
  WORD32 frame_idx = 0;
192
29.6k
  WORD32 temp = 0;
193
194
29.6k
  WORD32 config_len = 0;
195
29.6k
  WORD32 loop;
196
197
29.6k
  if (pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config
198
29.6k
          .str_usac_dec_config.usac_element_type[0] == ID_USAC_EXT) {
199
28.7k
    temp = ixheaacd_show_bits_buf(temp_buff, 3);
200
28.7k
    ext_ele_present = (temp >> 1) & 0x1;
201
202
28.7k
    if (ext_ele_present) {
203
1.80k
      ext_ele_use_dflt_len = temp & 0x1;
204
1.80k
      if (ext_ele_use_dflt_len != 0) return 0;
205
206
761
      ixheaacd_read_bits_buf(temp_buff, 3);
207
208
761
      ext_ele_payload_len = ixheaacd_read_bits_buf(temp_buff, 8);
209
210
761
      if (ext_ele_payload_len == 255) {
211
23
        WORD32 val_add = 0;
212
23
        val_add = ixheaacd_read_bits_buf(temp_buff, 16);
213
23
        ext_ele_payload_len =
214
23
            (UWORD32)((WORD32)ext_ele_payload_len + val_add - 2);
215
23
      }
216
217
761
      config_len = ixheaacd_read_bits_buf(temp_buff, 4);
218
761
      if (config_len == 15) {
219
59
        WORD32 val_add = 0;
220
59
        val_add = ixheaacd_read_bits_buf(temp_buff, 4);
221
59
        config_len += val_add;
222
59
        if (val_add == 15) {
223
15
          WORD32 val_add1 = 0;
224
15
          val_add1 = ixheaacd_read_bits_buf(temp_buff, 8);
225
15
          config_len += val_add1;
226
15
        }
227
59
      }
228
229
4.73k
      for (loop = 0; loop < config_len; loop++)
230
3.97k
        conf_buf[loop] = ixheaacd_read_bits_buf(temp_buff, 8);
231
232
761
      if (aac_dec_handle->preroll_config_present == 1) {
233
529
        if (!(memcmp(aac_dec_handle->preroll_config_prev, conf_buf,
234
529
                     sizeof(UWORD8) * config_len))) {
235
475
          config_len = 0;
236
475
        }
237
529
        if (memcmp(aac_dec_handle->preroll_config_prev, conf_buf,
238
529
                   sizeof(UWORD8) * config_len) != 0) {
239
54
          *config_changed = 1;
240
475
        } else {
241
475
          *config_changed = 0;
242
475
        }
243
529
      }
244
761
      aac_dec_handle->preroll_config_present = 1;
245
761
      memcpy(aac_dec_handle->preroll_config_prev, conf_buf,
246
761
             sizeof(UWORD8) * config_len);
247
248
761
      *apply_crossfade = ixheaacd_read_bits_buf(temp_buff, 1);
249
761
      ixheaacd_read_bits_buf(temp_buff, 1);
250
251
761
      num_pre_roll_frames = ixheaacd_read_bits_buf(temp_buff, 2);
252
761
      if (num_pre_roll_frames == 3) {
253
33
        WORD32 val_add = 0;
254
33
        val_add = ixheaacd_read_bits_buf(temp_buff, 4);
255
33
        num_pre_roll_frames += val_add;
256
33
      }
257
258
761
      if (num_pre_roll_frames > MAX_AUDIO_PREROLLS) {
259
6
        if (pstr_dec_data->str_usac_data.ec_flag) {
260
0
          num_pre_roll_frames = 0;
261
0
          longjmp(*(pstr_dec_data->xaac_jmp_buf),
262
0
                  IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
263
6
        } else {
264
6
          return IA_FATAL_ERROR;
265
6
        }
266
6
      }
267
268
1.75k
      for (frame_idx = 0; frame_idx < num_pre_roll_frames; frame_idx++) {
269
1.03k
        WORD32 au_len = 0;
270
1.03k
        au_len = ixheaacd_read_bits_buf(temp_buff, 16);
271
1.03k
        if (au_len == 65535) {
272
4
          WORD32 val_add = ixheaacd_read_bits_buf(temp_buff, 16);
273
4
          au_len += val_add;
274
4
        }
275
1.03k
        if (config_len != 0) {
276
112
          preroll_frame_offset[frame_idx] =
277
112
              temp_buff->size - temp_buff->cnt_bits;
278
112
        }
279
1.03k
        temp_buff->ptr_read_next += au_len;
280
1.03k
        temp_buff->cnt_bits -= au_len * 8;
281
1.03k
        if (temp_buff->cnt_bits < 0) {
282
35
          if (pstr_dec_data->str_usac_data.ec_flag) {
283
0
            temp_buff->cnt_bits = 0;
284
0
            longjmp(*(pstr_dec_data->xaac_jmp_buf),
285
0
                    IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
286
35
          } else {
287
35
            return IA_FATAL_ERROR;
288
35
          }
289
35
        }
290
1.03k
      }
291
755
    }
292
28.7k
  }
293
28.5k
  if (config_len == 0)
294
28.4k
    *preroll_units = 0;
295
91
  else
296
91
    *preroll_units = num_pre_roll_frames;
297
298
28.5k
  return config_len;
299
29.6k
}
300
301
WORD32 ixheaacd_dec_main(VOID *temp_handle, WORD8 *inbuffer, WORD8 *outbuffer,
302
                         WORD32 *out_bytes, WORD32 frames_done, WORD32 pcmsize,
303
296k
                         WORD32 *num_channel_out) {
304
296k
  WORD32 err = 0;
305
296k
  ia_exhaacplus_dec_api_struct *handle =
306
296k
      (ia_exhaacplus_dec_api_struct *)temp_handle;
307
296k
  ia_aac_dec_state_struct *aac_dec_handle = handle->p_state_aac;
308
309
296k
  WORD32 tmp;
310
296k
  ia_audio_specific_config_struct *pstr_audio_specific_config =
311
296k
      (ia_audio_specific_config_struct *)
312
296k
          aac_dec_handle->ia_audio_specific_config;
313
296k
  WORD32 suitable_tracks = 1;
314
296k
  WORD32 num_samples_out;
315
296k
  ia_dec_data_struct *pstr_dec_data;
316
296k
  UWORD8 config[MAX_PREROLL_SIZE];
317
296k
  WORD32 config_len;
318
296k
  WORD32 delay;
319
296k
  WORD preroll_frame_offset[MAX_PREROLL_FRAME_OFFSET] = {0};
320
296k
  WORD preroll_units = -1;
321
296k
  WORD32 access_units = 0;
322
296k
  WORD32 bits_consumed = 0;
323
324
296k
  if (frames_done == 0) {
325
10.3k
    if ((pstr_audio_specific_config->channel_configuration > 2) ||
326
10.3k
        (pstr_audio_specific_config->channel_configuration == 0)) {
327
7
      return -1;
328
7
    }
329
330
10.3k
    pstr_dec_data = (ia_dec_data_struct *)aac_dec_handle->pstr_dec_data;
331
332
10.3k
    tmp = pstr_audio_specific_config->channel_configuration;
333
334
10.3k
    suitable_tracks =
335
10.3k
        ixheaacd_frm_data_init(pstr_audio_specific_config, pstr_dec_data);
336
337
10.3k
    pstr_audio_specific_config->channel_configuration = tmp;
338
339
10.3k
    if (suitable_tracks <= 0) {
340
0
      return -1;
341
0
    }
342
10.3k
  }
343
344
296k
  {
345
296k
    WORD32 tot_out_bytes = 0;
346
296k
    jmp_buf local;
347
296k
    pstr_dec_data = (ia_dec_data_struct *)aac_dec_handle->pstr_dec_data;
348
296k
    pstr_dec_data->str_usac_data.frame_ok = 1;
349
296k
    pstr_dec_data->str_usac_data.ec_flag = aac_dec_handle->p_config->ui_err_conceal;
350
296k
    if (pstr_dec_data->str_usac_data.ec_flag) {
351
0
      err = setjmp(local);
352
0
    }
353
354
296k
    if (aac_dec_handle->p_config->ui_err_conceal) {
355
0
      if (err == 0) {
356
0
        if (pstr_dec_data->dec_bit_buf.cnt_bits) {
357
0
          aac_dec_handle->ui_in_bytes += (pstr_dec_data->dec_bit_buf.cnt_bits >> 3);
358
0
          if (aac_dec_handle->ui_in_bytes > IA_MAX_INP_BUFFER_SIZE) {
359
0
            aac_dec_handle->ui_in_bytes = 0;
360
0
          }
361
0
        }
362
0
      } else {
363
0
        pstr_dec_data->str_usac_data.frame_ok = 0;
364
0
      }
365
0
    }
366
367
296k
    if (frames_done == 0) {
368
10.3k
      WORD32 delay;
369
10.3k
      pstr_dec_data->str_usac_data.first_frame = 1;
370
10.3k
      if (aac_dec_handle->decode_create_done == 0) {
371
10.3k
        delay = ixheaacd_decode_create(
372
10.3k
            handle, pstr_dec_data,
373
10.3k
            pstr_dec_data->str_frame_data.scal_out_select + 1);
374
10.3k
        if (delay == -1) return -1;
375
10.3k
      }
376
10.3k
      pstr_dec_data->dec_bit_buf.max_size =
377
10.3k
          handle->p_mem_info_aac[IA_MEMTYPE_INPUT].ui_size;
378
10.3k
      *num_channel_out = pstr_dec_data->str_frame_data.scal_out_num_channels;
379
10.3k
      return 0;
380
10.3k
    }
381
382
285k
    pstr_dec_data->dec_bit_buf.ptr_bit_buf_base = (UWORD8 *)inbuffer;
383
285k
    pstr_dec_data->dec_bit_buf.size = aac_dec_handle->ui_in_bytes << 3;
384
285k
    pstr_dec_data->dec_bit_buf.ptr_bit_buf_end =
385
285k
        (UWORD8 *)inbuffer + aac_dec_handle->ui_in_bytes - 1;
386
285k
    pstr_dec_data->dec_bit_buf.ptr_read_next = (UWORD8 *)inbuffer;
387
285k
    pstr_dec_data->dec_bit_buf.bit_pos = 7;
388
285k
    pstr_dec_data->dec_bit_buf.cnt_bits = pstr_dec_data->dec_bit_buf.size;
389
285k
    pstr_dec_data->dec_bit_buf.xaac_jmp_buf = &(aac_dec_handle->xaac_jmp_buf);
390
285k
    if (pstr_dec_data->str_usac_data.ec_flag) {
391
0
      pstr_dec_data->xaac_jmp_buf = &local;
392
0
    }
393
285k
    pstr_dec_data->str_usac_data.usac_flag = aac_dec_handle->usac_flag;
394
285k
    pstr_dec_data->str_usac_data.esbr_hq = handle->aac_config.ui_hq_esbr;
395
285k
    pstr_dec_data->str_usac_data.enh_sbr = 1;
396
285k
    pstr_dec_data->str_usac_data.enh_sbr_ps = handle->aac_config.ui_enh_sbr_ps;
397
285k
    if (pstr_dec_data->dec_bit_buf.size > pstr_dec_data->dec_bit_buf.max_size)
398
2.10k
      pstr_dec_data->dec_bit_buf.max_size = pstr_dec_data->dec_bit_buf.size;
399
    /* audio pre roll frame parsing*/
400
401
285k
    if (aac_dec_handle->bs_format == LOAS_BSFORMAT && pstr_dec_data->str_usac_data.frame_ok) {
402
283k
      WORD32 sync = ixheaacd_read_bits_buf(&pstr_dec_data->dec_bit_buf, 11);
403
283k
      if (sync == 0x2b7) {
404
56.2k
        WORD32 result = ixheaacd_latm_audio_mux_element(
405
56.2k
          &pstr_dec_data->dec_bit_buf, &aac_dec_handle->latm_struct_element,
406
56.2k
          aac_dec_handle,
407
56.2k
          (ia_sampling_rate_info_struct *)&handle->aac_tables
408
56.2k
          .pstr_huffmann_tables->str_sample_rate_info[0]);
409
56.2k
        if (result < 0) {
410
176
          if (aac_dec_handle->p_config->ui_err_conceal)
411
0
            pstr_dec_data->str_usac_data.frame_ok = 0;
412
176
          else
413
176
            return result;
414
176
        }
415
56.2k
      }
416
283k
      bits_consumed = pstr_dec_data->dec_bit_buf.size - pstr_dec_data->dec_bit_buf.cnt_bits;
417
283k
    }
418
419
285k
    do {
420
285k
      config_len = 0;
421
285k
      if (err == 0 || aac_dec_handle->p_config->ui_err_conceal == 0) {
422
285k
        if (access_units == 0 &&
423
285k
            pstr_audio_specific_config->str_usac_config.str_usac_dec_config.preroll_flag) {
424
29.6k
          config_len = ixheaacd_audio_preroll_parsing(
425
29.6k
              pstr_dec_data, &config[0], &preroll_units, &preroll_frame_offset[0], aac_dec_handle,
426
29.6k
              &aac_dec_handle->drc_config_changed, &aac_dec_handle->apply_crossfade);
427
428
29.6k
          if (config_len == IA_FATAL_ERROR) return IA_FATAL_ERROR;
429
29.6k
        }
430
431
285k
        if (config_len != 0) {
432
86
          ia_bit_buf_struct config_bit_buf = {0};
433
434
86
          config_bit_buf.ptr_bit_buf_base = config;
435
86
          config_bit_buf.size = config_len << 3;
436
86
          config_bit_buf.ptr_read_next = config_bit_buf.ptr_bit_buf_base;
437
86
          config_bit_buf.ptr_bit_buf_end = (UWORD8 *)config + config_len;
438
86
          config_bit_buf.bit_pos = 7;
439
86
          config_bit_buf.cnt_bits = config_bit_buf.size;
440
86
          if (pstr_dec_data->str_usac_data.ec_flag) {
441
0
            config_bit_buf.xaac_jmp_buf = &local;
442
86
          } else {
443
86
            config_bit_buf.xaac_jmp_buf = &(aac_dec_handle->xaac_jmp_buf);
444
86
          }
445
446
86
          suitable_tracks = ixheaacd_frm_data_init(pstr_audio_specific_config, pstr_dec_data);
447
448
86
          if (suitable_tracks <= 0) return -1;
449
450
86
          aac_dec_handle->decode_create_done = 0;
451
86
          if (aac_dec_handle->p_config->ui_err_conceal) {
452
0
            if (pstr_dec_data->str_usac_data.frame_ok == 1 && err == 0) {
453
0
              err = ixheaacd_config(
454
0
                  &config_bit_buf,
455
0
                  &(pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config),
456
0
                  &(pstr_audio_specific_config->channel_configuration),
457
0
                  aac_dec_handle->p_config->ui_err_conceal);
458
0
              if (err != 0) {
459
0
                if (frames_done == 0)
460
0
                  return -1;
461
0
                else
462
0
                  pstr_dec_data->str_usac_data.frame_ok = 0;
463
0
              }
464
0
            }
465
86
          } else {
466
86
            err = ixheaacd_config(
467
86
                &config_bit_buf,
468
86
                &(pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config),
469
86
                &(pstr_audio_specific_config->channel_configuration),
470
86
                aac_dec_handle->p_config->ui_err_conceal);
471
86
            if (err != 0) {
472
14
              return err;
473
14
            }
474
86
          }
475
476
72
          pstr_dec_data->str_frame_data.str_audio_specific_config.sampling_frequency =
477
72
              pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config
478
72
                  .usac_sampling_frequency;
479
72
          delay = ixheaacd_decode_create(handle, pstr_dec_data,
480
72
                                         pstr_dec_data->str_frame_data.scal_out_select + 1);
481
72
          if (delay == -1) return -1;
482
65
          *num_channel_out = pstr_dec_data->str_frame_data.scal_out_num_channels;
483
65
        }
484
285k
      } else {
485
37
        pstr_dec_data->str_usac_data.frame_ok = 0;
486
37
      }
487
488
285k
      pstr_dec_data->dec_bit_buf.ptr_bit_buf_base = (UWORD8 *)inbuffer;
489
285k
      pstr_dec_data->dec_bit_buf.size = aac_dec_handle->ui_in_bytes << 3;
490
285k
      pstr_dec_data->dec_bit_buf.ptr_bit_buf_end =
491
285k
          (UWORD8 *)inbuffer + aac_dec_handle->ui_in_bytes - 1;
492
285k
      pstr_dec_data->dec_bit_buf.ptr_read_next = (UWORD8 *)inbuffer;
493
285k
      pstr_dec_data->dec_bit_buf.bit_pos = 7;
494
285k
      pstr_dec_data->dec_bit_buf.cnt_bits = pstr_dec_data->dec_bit_buf.size;
495
285k
      pstr_dec_data->dec_bit_buf.xaac_jmp_buf = &(aac_dec_handle->xaac_jmp_buf);
496
497
285k
      pstr_dec_data->str_usac_data.usac_flag = aac_dec_handle->usac_flag;
498
285k
      pstr_dec_data->str_usac_data.esbr_hq = handle->aac_config.ui_hq_esbr;
499
285k
      pstr_dec_data->str_usac_data.enh_sbr = 1;
500
285k
      pstr_dec_data->str_usac_data.enh_sbr_ps = handle->aac_config.ui_enh_sbr_ps;
501
502
285k
      if (preroll_frame_offset[access_units] &&
503
62
          ((pstr_dec_data->str_usac_data.ec_flag && pstr_dec_data->str_usac_data.frame_ok == 1) ||
504
62
           pstr_dec_data->str_usac_data.ec_flag == 0)) {
505
62
        pstr_dec_data->dec_bit_buf.cnt_bits =
506
62
            pstr_dec_data->dec_bit_buf.size -
507
62
            preroll_frame_offset[access_units];
508
62
        pstr_dec_data->dec_bit_buf.bit_pos =
509
62
            7 - preroll_frame_offset[access_units] % 8;
510
62
        pstr_dec_data->dec_bit_buf.ptr_read_next =
511
62
            pstr_dec_data->dec_bit_buf.ptr_read_next +
512
62
            (preroll_frame_offset[access_units] / 8);
513
285k
      } else {
514
285k
        pstr_dec_data->dec_bit_buf.cnt_bits =
515
285k
          pstr_dec_data->dec_bit_buf.size -
516
285k
          (bits_consumed);
517
285k
        pstr_dec_data->dec_bit_buf.bit_pos =
518
285k
          7 - (bits_consumed) % 8;
519
285k
        pstr_dec_data->dec_bit_buf.ptr_read_next =
520
285k
          pstr_dec_data->dec_bit_buf.ptr_read_next +
521
285k
          (bits_consumed / 8);
522
285k
      }
523
524
285k
      if (pstr_dec_data->str_usac_data.ec_flag) {
525
0
        if (!aac_dec_handle->decode_create_done && pstr_dec_data->str_usac_data.frame_ok == 1 &&
526
0
            config_len != 0)
527
0
          return IA_FATAL_ERROR;
528
285k
      } else {
529
285k
        if (!aac_dec_handle->decode_create_done) return IA_FATAL_ERROR;
530
285k
      }
531
532
285k
      err =
533
285k
          ixheaacd_usac_process(pstr_dec_data, num_channel_out, aac_dec_handle);
534
535
285k
      switch (pstr_dec_data->str_usac_data.sbr_ratio_idx) {
536
62.8k
        case 0:
537
62.8k
          handle->aac_config.ui_sbr_mode = 0;
538
62.8k
          break;
539
168k
        case 1:
540
168k
          handle->aac_config.ui_sbr_mode = 1;
541
168k
          break;
542
25.9k
        case 2:
543
25.9k
          handle->aac_config.ui_sbr_mode = 1;
544
25.9k
          break;
545
24.1k
        case 3:
546
24.1k
          handle->aac_config.ui_sbr_mode = 3;
547
24.1k
          break;
548
549
0
        default:
550
0
          handle->aac_config.ui_sbr_mode = 0;
551
285k
      }
552
553
281k
      if (err == -1) return err;
554
555
279k
      num_samples_out = pstr_dec_data->str_usac_data.output_samples;
556
279k
      if (!handle->aac_config.peak_limiter_off && pstr_dec_data->str_usac_data.ec_flag) {
557
0
        aac_dec_handle->peak_limiter.num_channels = *num_channel_out;
558
559
0
        ixheaacd_peak_limiter_process_float(&aac_dec_handle->peak_limiter,
560
0
                                            pstr_dec_data->str_usac_data.time_sample_vector,
561
0
                                            num_samples_out);
562
0
      }
563
564
279k
      ixheaacd_samples_sat((WORD8 *)outbuffer + tot_out_bytes, num_samples_out,
565
279k
                           pcmsize,
566
279k
                           pstr_dec_data->str_usac_data.time_sample_vector,
567
279k
                           out_bytes, *num_channel_out);
568
279k
      {
569
279k
        WORD32 preroll_counter =
570
279k
            pstr_dec_data->str_frame_data.str_audio_specific_config
571
279k
                .str_usac_config.str_usac_dec_config.preroll_counter;
572
573
279k
        UWORD8 i;  // for looping index used for payload calculation
574
279k
        WORD32 payload_buffer_offset = 0;
575
279k
        WORD32 copy_bytes =
576
279k
            pstr_dec_data->str_frame_data.str_audio_specific_config
577
279k
                .str_usac_config.str_usac_dec_config
578
279k
                .usac_ext_gain_payload_len[preroll_counter] *
579
279k
            sizeof(WORD8);
580
581
279k
        pstr_audio_specific_config->str_usac_config.str_usac_dec_config
582
279k
            .usac_ext_gain_payload_len[preroll_counter] =
583
279k
            pstr_dec_data->str_frame_data.str_audio_specific_config
584
279k
                .str_usac_config.str_usac_dec_config
585
279k
                .usac_ext_gain_payload_len[preroll_counter];
586
587
280k
        for (i = 0; i < preroll_counter; i++)
588
81
          payload_buffer_offset +=
589
81
              pstr_dec_data->str_frame_data.str_audio_specific_config
590
81
                  .str_usac_config.str_usac_dec_config
591
81
                  .usac_ext_gain_payload_len[i] *
592
81
              sizeof(WORD8);
593
594
279k
        memcpy(pstr_audio_specific_config->str_usac_config.str_usac_dec_config
595
279k
                       .usac_ext_gain_payload_buf +
596
279k
                   payload_buffer_offset,
597
279k
               pstr_dec_data->str_frame_data.str_audio_specific_config
598
279k
                       .str_usac_config.str_usac_dec_config
599
279k
                       .usac_ext_gain_payload_buf +
600
279k
                   payload_buffer_offset,
601
279k
               copy_bytes);
602
603
279k
        pstr_audio_specific_config->str_usac_config.str_usac_dec_config
604
279k
            .preroll_bytes[preroll_counter] = *out_bytes;
605
606
279k
        preroll_counter++;
607
608
279k
        if (preroll_counter > (MAX_AUDIO_PREROLLS + 1)) return IA_FATAL_ERROR;
609
610
279k
        pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config
611
279k
            .str_usac_dec_config.preroll_counter = preroll_counter;
612
613
279k
        ia_usac_decoder_config_struct *pstr_usac_dec_config_state =
614
279k
            &pstr_audio_specific_config->str_usac_config.str_usac_dec_config;
615
279k
        ia_usac_decoder_config_struct *pstr_usac_dec_config_dec_data =
616
279k
            &pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config
617
279k
            .str_usac_dec_config;
618
279k
        pstr_usac_dec_config_state->num_config_extensions =
619
279k
            pstr_usac_dec_config_dec_data->num_config_extensions;
620
279k
        pstr_usac_dec_config_state->num_elements =
621
279k
            pstr_usac_dec_config_dec_data->num_elements;
622
279k
        memcpy(pstr_usac_dec_config_state->usac_cfg_ext_info_buf,
623
279k
            pstr_usac_dec_config_dec_data->usac_cfg_ext_info_buf,
624
279k
            sizeof(pstr_usac_dec_config_state->usac_cfg_ext_info_buf));
625
279k
        memcpy(pstr_usac_dec_config_state->usac_ext_ele_payload_present,
626
279k
            pstr_usac_dec_config_dec_data->usac_ext_ele_payload_present,
627
279k
            sizeof(pstr_usac_dec_config_dec_data->usac_ext_ele_payload_present));
628
279k
        memcpy(pstr_usac_dec_config_state->usac_ext_ele_payload_buf,
629
279k
            pstr_usac_dec_config_dec_data->usac_ext_ele_payload_buf,
630
279k
            sizeof(pstr_usac_dec_config_state->usac_ext_ele_payload_buf));
631
279k
      }
632
633
0
      access_units++;
634
279k
      preroll_units--;
635
279k
      tot_out_bytes += (*out_bytes);
636
279k
    } while (preroll_units >= 0);
637
279k
    *out_bytes = tot_out_bytes;
638
279k
  }
639
640
279k
  if (aac_dec_handle->bs_format == LOAS_BSFORMAT) {
641
278k
    pstr_dec_data->dec_bit_buf.ptr_bit_buf_base = (UWORD8 *)inbuffer;
642
278k
    pstr_dec_data->dec_bit_buf.size = aac_dec_handle->ui_in_bytes << 3;
643
278k
    pstr_dec_data->dec_bit_buf.ptr_bit_buf_end =
644
278k
      (UWORD8 *)inbuffer + aac_dec_handle->ui_in_bytes - 1;
645
278k
    pstr_dec_data->dec_bit_buf.ptr_read_next = (UWORD8 *)inbuffer;
646
278k
    pstr_dec_data->dec_bit_buf.bit_pos = 7;
647
278k
    pstr_dec_data->dec_bit_buf.cnt_bits = pstr_dec_data->dec_bit_buf.size;
648
278k
    pstr_dec_data->dec_bit_buf.xaac_jmp_buf = &(aac_dec_handle->xaac_jmp_buf);
649
650
278k
    ixheaacd_read_bits_buf(&pstr_dec_data->dec_bit_buf, 11);
651
278k
    aac_dec_handle->i_bytes_consumed =
652
278k
        ixheaacd_read_bits_buf(&pstr_dec_data->dec_bit_buf, 13) + 3;
653
278k
  }
654
655
279k
  return err;
656
285k
}