Coverage Report

Created: 2026-08-14 06:54

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
270k
                          WORD32 *out_bytes, WORD32 num_channel_out) {
85
270k
  WORD32 num;
86
270k
  WORD32 i;
87
270k
  WORD32 write_local;
88
270k
  FLOAT32 write_local_float;
89
90
270k
  WORD16 *out_buf = (WORD16 *)outbuffer;
91
92
270k
  num = num_channel_out * num_samples_out;
93
94
270k
  if (pcmsize == 16) {
95
1.03G
    for (i = 0; i < num; i++) {
96
1.02G
      write_local_float =
97
1.02G
          (out_samples[i % num_channel_out][i / num_channel_out]);
98
99
1.02G
      if (write_local_float > 32767.0f) {
100
25.4M
        write_local_float = 32767.0f;
101
1.00G
      } else if (write_local_float < -32768.0f) {
102
30.9M
        write_local_float = -32768.0f;
103
30.9M
      }
104
1.02G
      out_buf[i] = (WORD16)write_local_float;
105
1.02G
    }
106
107
270k
    *out_bytes = num * sizeof(WORD16);
108
270k
  } 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
270k
}
129
130
VOID ixheaacd_samples_sat_mc(WORD8* outbuffer, WORD32 num_samples_out,
131
    FLOAT32(*out_samples)[4096], WORD32* out_bytes,
132
152
    WORD32 num_channel_out, WORD32 ch_fac) {
133
152
  WORD32 num;
134
152
  WORD32 i;
135
152
  FLOAT32 write_local_float;
136
137
152
  WORD16* out_buf = (WORD16*)outbuffer;
138
139
152
  num = num_channel_out * num_samples_out;
140
152
  if (num_channel_out == 1) {
141
235k
    for (i = 0; i < num; i++) {
142
235k
      write_local_float =
143
235k
          (out_samples[i % num_channel_out][i / num_channel_out]);
144
145
235k
      if (write_local_float > 32767.0f) {
146
78
        write_local_float = 32767.0f;
147
235k
      } else if (write_local_float < -32768.0f) {
148
0
        write_local_float = -32768.0f;
149
0
      }
150
235k
      out_buf[i * ch_fac] = (WORD16)write_local_float;
151
235k
    }
152
115
  } else if (num_channel_out == 2) {
153
75.8k
    for (i = 0; i < num_samples_out; i++) {
154
75.7k
      write_local_float =
155
75.7k
          (out_samples[(2*i) % num_channel_out][(2 * i) / num_channel_out]);
156
157
75.7k
      if (write_local_float > 32767.0f) {
158
0
          write_local_float = 32767.0f;
159
75.7k
      } else if (write_local_float < -32768.0f) {
160
0
          write_local_float = -32768.0f;
161
0
      }
162
75.7k
      out_buf[i * ch_fac] = (WORD16)write_local_float;
163
164
75.7k
      write_local_float =
165
75.7k
          (out_samples[((2 * i) + 1) % num_channel_out][((2 * i) + 1) / num_channel_out]);
166
167
75.7k
      if (write_local_float > 32767.0f) {
168
0
          write_local_float = 32767.0f;
169
75.7k
      } else if (write_local_float < -32768.0f) {
170
0
          write_local_float = -32768.0f;
171
0
      }
172
75.7k
      out_buf[i * ch_fac + 1] = (WORD16)write_local_float;
173
75.7k
    }
174
37
  }
175
152
  *out_bytes = num * sizeof(WORD16);
176
152
}
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
24.7k
    WORD32 *config_changed, WORD32 *apply_crossfade) {
183
24.7k
  ia_bit_buf_struct *temp_buff =
184
24.7k
      (ia_bit_buf_struct *)&(pstr_dec_data->dec_bit_buf);
185
186
24.7k
  WORD32 ext_ele_present = 0;
187
24.7k
  WORD32 ext_ele_use_dflt_len = 0;
188
24.7k
  WORD32 ext_ele_payload_len = 0;
189
24.7k
  WORD32 num_pre_roll_frames = 0;
190
191
24.7k
  WORD32 frame_idx = 0;
192
24.7k
  WORD32 temp = 0;
193
194
24.7k
  WORD32 config_len = 0;
195
24.7k
  WORD32 loop;
196
197
24.7k
  if (pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config
198
24.7k
          .str_usac_dec_config.usac_element_type[0] == ID_USAC_EXT) {
199
24.5k
    temp = ixheaacd_show_bits_buf(temp_buff, 3);
200
24.5k
    ext_ele_present = (temp >> 1) & 0x1;
201
202
24.5k
    if (ext_ele_present) {
203
1.55k
      ext_ele_use_dflt_len = temp & 0x1;
204
1.55k
      if (ext_ele_use_dflt_len != 0) return 0;
205
206
494
      ixheaacd_read_bits_buf(temp_buff, 3);
207
208
494
      ext_ele_payload_len = ixheaacd_read_bits_buf(temp_buff, 8);
209
210
494
      if (ext_ele_payload_len == 255) {
211
4
        WORD32 val_add = 0;
212
4
        val_add = ixheaacd_read_bits_buf(temp_buff, 16);
213
4
        ext_ele_payload_len =
214
4
            (UWORD32)((WORD32)ext_ele_payload_len + val_add - 2);
215
4
      }
216
217
494
      config_len = ixheaacd_read_bits_buf(temp_buff, 4);
218
494
      if (config_len == 15) {
219
28
        WORD32 val_add = 0;
220
28
        val_add = ixheaacd_read_bits_buf(temp_buff, 4);
221
28
        config_len += val_add;
222
28
        if (val_add == 15) {
223
4
          WORD32 val_add1 = 0;
224
4
          val_add1 = ixheaacd_read_bits_buf(temp_buff, 8);
225
4
          config_len += val_add1;
226
4
        }
227
28
      }
228
229
2.08k
      for (loop = 0; loop < config_len; loop++)
230
1.59k
        conf_buf[loop] = ixheaacd_read_bits_buf(temp_buff, 8);
231
232
494
      if (aac_dec_handle->preroll_config_present == 1) {
233
346
        if (!(memcmp(aac_dec_handle->preroll_config_prev, conf_buf,
234
346
                     sizeof(UWORD8) * config_len))) {
235
314
          config_len = 0;
236
314
        }
237
346
        if (memcmp(aac_dec_handle->preroll_config_prev, conf_buf,
238
346
                   sizeof(UWORD8) * config_len) != 0) {
239
32
          *config_changed = 1;
240
314
        } else {
241
314
          *config_changed = 0;
242
314
        }
243
346
      }
244
494
      aac_dec_handle->preroll_config_present = 1;
245
494
      memcpy(aac_dec_handle->preroll_config_prev, conf_buf,
246
494
             sizeof(UWORD8) * config_len);
247
248
494
      *apply_crossfade = ixheaacd_read_bits_buf(temp_buff, 1);
249
494
      ixheaacd_read_bits_buf(temp_buff, 1);
250
251
494
      num_pre_roll_frames = ixheaacd_read_bits_buf(temp_buff, 2);
252
494
      if (num_pre_roll_frames == 3) {
253
21
        WORD32 val_add = 0;
254
21
        val_add = ixheaacd_read_bits_buf(temp_buff, 4);
255
21
        num_pre_roll_frames += val_add;
256
21
      }
257
258
494
      if (num_pre_roll_frames > MAX_AUDIO_PREROLLS) {
259
5
        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
5
        } else {
264
5
          return IA_FATAL_ERROR;
265
5
        }
266
5
      }
267
268
1.10k
      for (frame_idx = 0; frame_idx < num_pre_roll_frames; frame_idx++) {
269
654
        WORD32 au_len = 0;
270
654
        au_len = ixheaacd_read_bits_buf(temp_buff, 16);
271
654
        if (au_len == 65535) {
272
5
          WORD32 val_add = ixheaacd_read_bits_buf(temp_buff, 16);
273
5
          au_len += val_add;
274
5
        }
275
654
        if (config_len != 0) {
276
58
          preroll_frame_offset[frame_idx] =
277
58
              temp_buff->size - temp_buff->cnt_bits;
278
58
        }
279
654
        temp_buff->ptr_read_next += au_len;
280
654
        temp_buff->cnt_bits -= au_len * 8;
281
654
        if (temp_buff->cnt_bits < 0) {
282
39
          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
39
          } else {
287
39
            return IA_FATAL_ERROR;
288
39
          }
289
39
        }
290
654
      }
291
489
    }
292
24.5k
  }
293
23.6k
  if (config_len == 0)
294
23.6k
    *preroll_units = 0;
295
50
  else
296
50
    *preroll_units = num_pre_roll_frames;
297
298
23.6k
  return config_len;
299
24.7k
}
300
301
WORD32 ixheaacd_dec_main(VOID *temp_handle, WORD8 *inbuffer, WORD8 *outbuffer,
302
                         WORD32 *out_bytes, WORD32 frames_done, WORD32 pcmsize,
303
256k
                         WORD32 *num_channel_out) {
304
256k
  WORD32 err = 0;
305
256k
  ia_exhaacplus_dec_api_struct *handle =
306
256k
      (ia_exhaacplus_dec_api_struct *)temp_handle;
307
256k
  ia_aac_dec_state_struct *aac_dec_handle = handle->p_state_aac;
308
309
256k
  WORD32 tmp;
310
256k
  ia_audio_specific_config_struct *pstr_audio_specific_config =
311
256k
      (ia_audio_specific_config_struct *)
312
256k
          aac_dec_handle->ia_audio_specific_config;
313
256k
  WORD32 suitable_tracks = 1;
314
256k
  WORD32 num_samples_out;
315
256k
  ia_dec_data_struct *pstr_dec_data;
316
256k
  UWORD8 config[MAX_PREROLL_SIZE];
317
256k
  WORD32 config_len;
318
256k
  WORD32 delay;
319
256k
  WORD preroll_frame_offset[MAX_PREROLL_FRAME_OFFSET] = {0};
320
256k
  WORD preroll_units = -1;
321
256k
  WORD32 access_units = 0;
322
256k
  WORD32 bits_consumed = 0;
323
324
256k
  if (frames_done == 0) {
325
7.83k
    if ((pstr_audio_specific_config->channel_configuration > 2) ||
326
7.82k
        (pstr_audio_specific_config->channel_configuration == 0)) {
327
6
      return -1;
328
6
    }
329
330
7.82k
    pstr_dec_data = (ia_dec_data_struct *)aac_dec_handle->pstr_dec_data;
331
332
7.82k
    tmp = pstr_audio_specific_config->channel_configuration;
333
334
7.82k
    suitable_tracks =
335
7.82k
        ixheaacd_frm_data_init(pstr_audio_specific_config, pstr_dec_data);
336
337
7.82k
    pstr_audio_specific_config->channel_configuration = tmp;
338
339
7.82k
    if (suitable_tracks <= 0) {
340
0
      return -1;
341
0
    }
342
7.82k
  }
343
344
256k
  {
345
256k
    WORD32 tot_out_bytes = 0;
346
256k
    jmp_buf local;
347
256k
    pstr_dec_data = (ia_dec_data_struct *)aac_dec_handle->pstr_dec_data;
348
256k
    pstr_dec_data->str_usac_data.frame_ok = 1;
349
256k
    pstr_dec_data->str_usac_data.ec_flag = aac_dec_handle->p_config->ui_err_conceal;
350
256k
    if (pstr_dec_data->str_usac_data.ec_flag) {
351
0
      err = setjmp(local);
352
0
    }
353
354
256k
    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
256k
    if (frames_done == 0) {
368
7.82k
      WORD32 delay;
369
7.82k
      pstr_dec_data->str_usac_data.first_frame = 1;
370
7.82k
      if (aac_dec_handle->decode_create_done == 0) {
371
7.82k
        delay = ixheaacd_decode_create(
372
7.82k
            handle, pstr_dec_data,
373
7.82k
            pstr_dec_data->str_frame_data.scal_out_select + 1);
374
7.82k
        if (delay == -1) return -1;
375
7.82k
      }
376
7.81k
      pstr_dec_data->dec_bit_buf.max_size =
377
7.81k
          handle->p_mem_info_aac[IA_MEMTYPE_INPUT].ui_size;
378
7.81k
      *num_channel_out = pstr_dec_data->str_frame_data.scal_out_num_channels;
379
7.81k
      return 0;
380
7.82k
    }
381
382
248k
    pstr_dec_data->dec_bit_buf.ptr_bit_buf_base = (UWORD8 *)inbuffer;
383
248k
    pstr_dec_data->dec_bit_buf.size = aac_dec_handle->ui_in_bytes << 3;
384
248k
    pstr_dec_data->dec_bit_buf.ptr_bit_buf_end =
385
248k
        (UWORD8 *)inbuffer + aac_dec_handle->ui_in_bytes - 1;
386
248k
    pstr_dec_data->dec_bit_buf.ptr_read_next = (UWORD8 *)inbuffer;
387
248k
    pstr_dec_data->dec_bit_buf.bit_pos = 7;
388
248k
    pstr_dec_data->dec_bit_buf.cnt_bits = pstr_dec_data->dec_bit_buf.size;
389
248k
    pstr_dec_data->dec_bit_buf.xaac_jmp_buf = &(aac_dec_handle->xaac_jmp_buf);
390
248k
    if (pstr_dec_data->str_usac_data.ec_flag) {
391
0
      pstr_dec_data->xaac_jmp_buf = &local;
392
0
    }
393
248k
    pstr_dec_data->str_usac_data.usac_flag = aac_dec_handle->usac_flag;
394
248k
    pstr_dec_data->str_usac_data.esbr_hq = handle->aac_config.ui_hq_esbr;
395
248k
    pstr_dec_data->str_usac_data.enh_sbr = 1;
396
248k
    pstr_dec_data->str_usac_data.enh_sbr_ps = handle->aac_config.ui_enh_sbr_ps;
397
248k
    if (pstr_dec_data->dec_bit_buf.size > pstr_dec_data->dec_bit_buf.max_size)
398
2.03k
      pstr_dec_data->dec_bit_buf.max_size = pstr_dec_data->dec_bit_buf.size;
399
    /* audio pre roll frame parsing*/
400
401
248k
    if (aac_dec_handle->bs_format == LOAS_BSFORMAT && pstr_dec_data->str_usac_data.frame_ok) {
402
247k
      WORD32 sync = ixheaacd_read_bits_buf(&pstr_dec_data->dec_bit_buf, 11);
403
247k
      if (sync == 0x2b7) {
404
43.7k
        WORD32 result = ixheaacd_latm_audio_mux_element(
405
43.7k
          &pstr_dec_data->dec_bit_buf, &aac_dec_handle->latm_struct_element,
406
43.7k
          aac_dec_handle,
407
43.7k
          (ia_sampling_rate_info_struct *)&handle->aac_tables
408
43.7k
          .pstr_huffmann_tables->str_sample_rate_info[0]);
409
43.7k
        if (result < 0) {
410
134
          if (aac_dec_handle->p_config->ui_err_conceal)
411
0
            pstr_dec_data->str_usac_data.frame_ok = 0;
412
134
          else
413
134
            return result;
414
134
        }
415
43.7k
      }
416
247k
      bits_consumed = pstr_dec_data->dec_bit_buf.size - pstr_dec_data->dec_bit_buf.cnt_bits;
417
247k
    }
418
419
248k
    do {
420
248k
      config_len = 0;
421
248k
      if (err == 0 || aac_dec_handle->p_config->ui_err_conceal == 0) {
422
248k
        if (access_units == 0 &&
423
248k
            pstr_audio_specific_config->str_usac_config.str_usac_dec_config.preroll_flag) {
424
24.7k
          config_len = ixheaacd_audio_preroll_parsing(
425
24.7k
              pstr_dec_data, &config[0], &preroll_units, &preroll_frame_offset[0], aac_dec_handle,
426
24.7k
              &aac_dec_handle->drc_config_changed, &aac_dec_handle->apply_crossfade);
427
428
24.7k
          if (config_len == IA_FATAL_ERROR) return IA_FATAL_ERROR;
429
24.7k
        }
430
431
248k
        if (config_len != 0) {
432
46
          ia_bit_buf_struct config_bit_buf = {0};
433
434
46
          config_bit_buf.ptr_bit_buf_base = config;
435
46
          config_bit_buf.size = config_len << 3;
436
46
          config_bit_buf.ptr_read_next = config_bit_buf.ptr_bit_buf_base;
437
46
          config_bit_buf.ptr_bit_buf_end = (UWORD8 *)config + config_len;
438
46
          config_bit_buf.bit_pos = 7;
439
46
          config_bit_buf.cnt_bits = config_bit_buf.size;
440
46
          if (pstr_dec_data->str_usac_data.ec_flag) {
441
0
            config_bit_buf.xaac_jmp_buf = &local;
442
46
          } else {
443
46
            config_bit_buf.xaac_jmp_buf = &(aac_dec_handle->xaac_jmp_buf);
444
46
          }
445
446
46
          suitable_tracks = ixheaacd_frm_data_init(pstr_audio_specific_config, pstr_dec_data);
447
448
46
          if (suitable_tracks <= 0) return -1;
449
450
46
          aac_dec_handle->decode_create_done = 0;
451
46
          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
46
          } else {
466
46
            err = ixheaacd_config(
467
46
                &config_bit_buf,
468
46
                &(pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config),
469
46
                &(pstr_audio_specific_config->channel_configuration),
470
46
                aac_dec_handle->p_config->ui_err_conceal);
471
46
            if (err != 0) {
472
23
              return err;
473
23
            }
474
46
          }
475
476
23
          pstr_dec_data->str_frame_data.str_audio_specific_config.sampling_frequency =
477
23
              pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config
478
23
                  .usac_sampling_frequency;
479
23
          delay = ixheaacd_decode_create(handle, pstr_dec_data,
480
23
                                         pstr_dec_data->str_frame_data.scal_out_select + 1);
481
23
          if (delay == -1) return -1;
482
19
          *num_channel_out = pstr_dec_data->str_frame_data.scal_out_num_channels;
483
19
        }
484
248k
      } else {
485
21
        pstr_dec_data->str_usac_data.frame_ok = 0;
486
21
      }
487
488
248k
      pstr_dec_data->dec_bit_buf.ptr_bit_buf_base = (UWORD8 *)inbuffer;
489
248k
      pstr_dec_data->dec_bit_buf.size = aac_dec_handle->ui_in_bytes << 3;
490
248k
      pstr_dec_data->dec_bit_buf.ptr_bit_buf_end =
491
248k
          (UWORD8 *)inbuffer + aac_dec_handle->ui_in_bytes - 1;
492
248k
      pstr_dec_data->dec_bit_buf.ptr_read_next = (UWORD8 *)inbuffer;
493
248k
      pstr_dec_data->dec_bit_buf.bit_pos = 7;
494
248k
      pstr_dec_data->dec_bit_buf.cnt_bits = pstr_dec_data->dec_bit_buf.size;
495
248k
      pstr_dec_data->dec_bit_buf.xaac_jmp_buf = &(aac_dec_handle->xaac_jmp_buf);
496
497
248k
      pstr_dec_data->str_usac_data.usac_flag = aac_dec_handle->usac_flag;
498
248k
      pstr_dec_data->str_usac_data.esbr_hq = handle->aac_config.ui_hq_esbr;
499
248k
      pstr_dec_data->str_usac_data.enh_sbr = 1;
500
248k
      pstr_dec_data->str_usac_data.enh_sbr_ps = handle->aac_config.ui_enh_sbr_ps;
501
502
248k
      if (preroll_frame_offset[access_units] &&
503
19
          ((pstr_dec_data->str_usac_data.ec_flag && pstr_dec_data->str_usac_data.frame_ok == 1) ||
504
19
           pstr_dec_data->str_usac_data.ec_flag == 0)) {
505
19
        pstr_dec_data->dec_bit_buf.cnt_bits =
506
19
            pstr_dec_data->dec_bit_buf.size -
507
19
            preroll_frame_offset[access_units];
508
19
        pstr_dec_data->dec_bit_buf.bit_pos =
509
19
            7 - preroll_frame_offset[access_units] % 8;
510
19
        pstr_dec_data->dec_bit_buf.ptr_read_next =
511
19
            pstr_dec_data->dec_bit_buf.ptr_read_next +
512
19
            (preroll_frame_offset[access_units] / 8);
513
248k
      } else {
514
248k
        pstr_dec_data->dec_bit_buf.cnt_bits =
515
248k
          pstr_dec_data->dec_bit_buf.size -
516
248k
          (bits_consumed);
517
248k
        pstr_dec_data->dec_bit_buf.bit_pos =
518
248k
          7 - (bits_consumed) % 8;
519
248k
        pstr_dec_data->dec_bit_buf.ptr_read_next =
520
248k
          pstr_dec_data->dec_bit_buf.ptr_read_next +
521
248k
          (bits_consumed / 8);
522
248k
      }
523
524
248k
      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
248k
      } else {
529
248k
        if (!aac_dec_handle->decode_create_done) return IA_FATAL_ERROR;
530
248k
      }
531
532
248k
      err =
533
248k
          ixheaacd_usac_process(pstr_dec_data, num_channel_out, aac_dec_handle);
534
535
248k
      switch (pstr_dec_data->str_usac_data.sbr_ratio_idx) {
536
52.1k
        case 0:
537
52.1k
          handle->aac_config.ui_sbr_mode = 0;
538
52.1k
          break;
539
159k
        case 1:
540
159k
          handle->aac_config.ui_sbr_mode = 1;
541
159k
          break;
542
16.3k
        case 2:
543
16.3k
          handle->aac_config.ui_sbr_mode = 1;
544
16.3k
          break;
545
17.4k
        case 3:
546
17.4k
          handle->aac_config.ui_sbr_mode = 3;
547
17.4k
          break;
548
549
0
        default:
550
0
          handle->aac_config.ui_sbr_mode = 0;
551
248k
      }
552
553
245k
      if (err == -1) return err;
554
555
244k
      num_samples_out = pstr_dec_data->str_usac_data.output_samples;
556
244k
      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
244k
      ixheaacd_samples_sat((WORD8 *)outbuffer + tot_out_bytes, num_samples_out,
565
244k
                           pcmsize,
566
244k
                           pstr_dec_data->str_usac_data.time_sample_vector,
567
244k
                           out_bytes, *num_channel_out);
568
244k
      {
569
244k
        WORD32 preroll_counter =
570
244k
            pstr_dec_data->str_frame_data.str_audio_specific_config
571
244k
                .str_usac_config.str_usac_dec_config.preroll_counter;
572
573
244k
        UWORD8 i;  // for looping index used for payload calculation
574
244k
        WORD32 payload_buffer_offset = 0;
575
244k
        WORD32 copy_bytes =
576
244k
            pstr_dec_data->str_frame_data.str_audio_specific_config
577
244k
                .str_usac_config.str_usac_dec_config
578
244k
                .usac_ext_gain_payload_len[preroll_counter] *
579
244k
            sizeof(WORD8);
580
581
244k
        pstr_audio_specific_config->str_usac_config.str_usac_dec_config
582
244k
            .usac_ext_gain_payload_len[preroll_counter] =
583
244k
            pstr_dec_data->str_frame_data.str_audio_specific_config
584
244k
                .str_usac_config.str_usac_dec_config
585
244k
                .usac_ext_gain_payload_len[preroll_counter];
586
587
244k
        for (i = 0; i < preroll_counter; i++)
588
24
          payload_buffer_offset +=
589
24
              pstr_dec_data->str_frame_data.str_audio_specific_config
590
24
                  .str_usac_config.str_usac_dec_config
591
24
                  .usac_ext_gain_payload_len[i] *
592
24
              sizeof(WORD8);
593
594
244k
        memcpy(pstr_audio_specific_config->str_usac_config.str_usac_dec_config
595
244k
                       .usac_ext_gain_payload_buf +
596
244k
                   payload_buffer_offset,
597
244k
               pstr_dec_data->str_frame_data.str_audio_specific_config
598
244k
                       .str_usac_config.str_usac_dec_config
599
244k
                       .usac_ext_gain_payload_buf +
600
244k
                   payload_buffer_offset,
601
244k
               copy_bytes);
602
603
244k
        pstr_audio_specific_config->str_usac_config.str_usac_dec_config
604
244k
            .preroll_bytes[preroll_counter] = *out_bytes;
605
606
244k
        preroll_counter++;
607
608
244k
        if (preroll_counter > (MAX_AUDIO_PREROLLS + 1)) return IA_FATAL_ERROR;
609
610
244k
        pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config
611
244k
            .str_usac_dec_config.preroll_counter = preroll_counter;
612
613
244k
        ia_usac_decoder_config_struct *pstr_usac_dec_config_state =
614
244k
            &pstr_audio_specific_config->str_usac_config.str_usac_dec_config;
615
244k
        ia_usac_decoder_config_struct *pstr_usac_dec_config_dec_data =
616
244k
            &pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config
617
244k
            .str_usac_dec_config;
618
244k
        pstr_usac_dec_config_state->num_config_extensions =
619
244k
            pstr_usac_dec_config_dec_data->num_config_extensions;
620
244k
        pstr_usac_dec_config_state->num_elements =
621
244k
            pstr_usac_dec_config_dec_data->num_elements;
622
244k
        memcpy(pstr_usac_dec_config_state->usac_cfg_ext_info_buf,
623
244k
            pstr_usac_dec_config_dec_data->usac_cfg_ext_info_buf,
624
244k
            sizeof(pstr_usac_dec_config_state->usac_cfg_ext_info_buf));
625
244k
        memcpy(pstr_usac_dec_config_state->usac_ext_ele_payload_present,
626
244k
            pstr_usac_dec_config_dec_data->usac_ext_ele_payload_present,
627
244k
            sizeof(pstr_usac_dec_config_dec_data->usac_ext_ele_payload_present));
628
244k
        memcpy(pstr_usac_dec_config_state->usac_ext_ele_payload_buf,
629
244k
            pstr_usac_dec_config_dec_data->usac_ext_ele_payload_buf,
630
244k
            sizeof(pstr_usac_dec_config_state->usac_ext_ele_payload_buf));
631
244k
      }
632
633
0
      access_units++;
634
244k
      preroll_units--;
635
244k
      tot_out_bytes += (*out_bytes);
636
244k
    } while (preroll_units >= 0);
637
244k
    *out_bytes = tot_out_bytes;
638
244k
  }
639
640
244k
  if (aac_dec_handle->bs_format == LOAS_BSFORMAT) {
641
242k
    pstr_dec_data->dec_bit_buf.ptr_bit_buf_base = (UWORD8 *)inbuffer;
642
242k
    pstr_dec_data->dec_bit_buf.size = aac_dec_handle->ui_in_bytes << 3;
643
242k
    pstr_dec_data->dec_bit_buf.ptr_bit_buf_end =
644
242k
      (UWORD8 *)inbuffer + aac_dec_handle->ui_in_bytes - 1;
645
242k
    pstr_dec_data->dec_bit_buf.ptr_read_next = (UWORD8 *)inbuffer;
646
242k
    pstr_dec_data->dec_bit_buf.bit_pos = 7;
647
242k
    pstr_dec_data->dec_bit_buf.cnt_bits = pstr_dec_data->dec_bit_buf.size;
648
242k
    pstr_dec_data->dec_bit_buf.xaac_jmp_buf = &(aac_dec_handle->xaac_jmp_buf);
649
650
242k
    ixheaacd_read_bits_buf(&pstr_dec_data->dec_bit_buf, 11);
651
242k
    aac_dec_handle->i_bytes_consumed =
652
242k
        ixheaacd_read_bits_buf(&pstr_dec_data->dec_bit_buf, 13) + 3;
653
242k
  }
654
655
244k
  return err;
656
248k
}