Coverage Report

Created: 2026-03-21 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/decoder/ixheaacd_common_lpfuncs.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
#include "ixheaac_constants.h"
24
#include "ixheaac_basic_ops32.h"
25
#include "ixheaac_basic_ops16.h"
26
#include "ixheaac_basic_ops40.h"
27
#include "ixheaacd_bitbuffer.h"
28
#include "ixheaacd_defines.h"
29
#include "ixheaacd_aac_rom.h"
30
#include "ixheaacd_pulsedata.h"
31
32
#include "ixheaacd_pns.h"
33
#include "ixheaacd_drc_data_struct.h"
34
35
#include "ixheaacd_lt_predict.h"
36
37
#include "ixheaacd_cnst.h"
38
#include "ixheaacd_ec_defines.h"
39
#include "ixheaacd_ec_struct_def.h"
40
#include "ixheaacd_channelinfo.h"
41
#include "ixheaacd_drc_dec.h"
42
#include "ixheaacd_sbrdecoder.h"
43
44
#include "ixheaacd_audioobjtypes.h"
45
#include "ixheaacd_sbrdecsettings.h"
46
#include "ixheaacd_memory_standards.h"
47
#include "ixheaacd_error_codes.h"
48
49
#include "ixheaacd_defines.h"
50
51
#include "ixheaacd_sbr_scale.h"
52
#include "ixheaacd_lpp_tran.h"
53
#include "ixheaacd_env_extr_part.h"
54
#include "ixheaacd_sbr_rom.h"
55
56
#include "ixheaacd_hybrid.h"
57
#include "ixheaacd_ps_dec.h"
58
#include "ixheaacd_ps_bitdec.h"
59
60
#include "ixheaacd_pulsedata.h"
61
62
#include "ixheaacd_pns.h"
63
64
#include "ixheaacd_channelinfo.h"
65
66
#include "ixheaacd_env_extr.h"
67
#include "ixheaacd_common_rom.h"
68
#include "ixheaacd_block.h"
69
#include "ixheaacd_channel.h"
70
#include "ixheaacd_audioobjtypes.h"
71
#include "ixheaacd_latmdemux.h"
72
#include "ixheaacd_aacdec.h"
73
#include "ixheaacd_hybrid.h"
74
#include "ixheaacd_ps_dec.h"
75
76
#include "ixheaacd_mps_polyphase.h"
77
#include "ixheaacd_config.h"
78
#include "ixheaacd_qmf_dec.h"
79
#include "ixheaacd_mps_macro_def.h"
80
#include "ixheaacd_mps_struct_def.h"
81
#include "ixheaacd_mps_res_rom.h"
82
#include "ixheaacd_mps_aac_struct.h"
83
#include "ixheaacd_mps_dec.h"
84
#include "ixheaacd_struct_def.h"
85
#include "ixheaacd_headerdecode.h"
86
87
#include "ixheaacd_multichannel.h"
88
89
#include "ixheaac_basic_op.h"
90
#include "ixheaacd_intrinsics.h"
91
92
static PLATFORM_INLINE UWORD32
93
16.0k
ixheaacd_aac_showbits_7(ia_bit_buf_struct *it_bit_buff) {
94
16.0k
  UWORD8 *v = it_bit_buff->ptr_read_next;
95
16.0k
  UWORD32 b = 0;
96
16.0k
  UWORD32 x;
97
16.0k
  b = ((WORD32)v[0] << 8);
98
16.0k
  if (it_bit_buff->bit_pos < 6) {
99
10.8k
    b |= (WORD32)(v[1]);
100
10.8k
  }
101
16.0k
  x = (UWORD32)b << (15 + 8 - it_bit_buff->bit_pos);
102
16.0k
  x = (UWORD32)x >> (25);
103
104
16.0k
  return x;
105
16.0k
}
106
107
WORD ixheaacd_get_channel_mask(
108
7.93k
    ia_exhaacplus_dec_api_struct *p_obj_enhaacplus_dec) {
109
7.93k
  WORD ixheaacd_drc_offset = 0, channel_mask = 0;
110
7.93k
  WORD flag1 = 0, flag2 = 0;
111
7.93k
  WORD ch_idx;
112
7.93k
  WORD *ptr_slot_element = p_obj_enhaacplus_dec->aac_config.slot_element;
113
7.93k
  WORD *ptr_element_type = p_obj_enhaacplus_dec->aac_config.element_type;
114
115
7.93k
  memset(ptr_slot_element, 0, sizeof(WORD) * MAX_BS_ELEMENT);
116
117
72.0k
  for (ch_idx = 0; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
118
65.6k
    if (ptr_element_type[ch_idx] == 1) {
119
1.52k
      channel_mask += 0x3;
120
1.52k
      ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
121
1.52k
      ixheaacd_drc_offset += 2;
122
1.52k
      flag1 = ch_idx + 1;
123
1.52k
      break;
124
1.52k
    }
125
65.6k
  }
126
127
34.4k
  for (ch_idx = 0; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
128
31.8k
    if (ptr_element_type[ch_idx] == 0) {
129
5.29k
      channel_mask += 0x4;
130
5.29k
      ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
131
5.29k
      ixheaacd_drc_offset += 1;
132
5.29k
      flag2 = ch_idx + 1;
133
5.29k
      break;
134
5.29k
    }
135
31.8k
  }
136
74.6k
  for (ch_idx = 0; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
137
68.0k
    if (ptr_element_type[ch_idx] == 3) {
138
1.27k
      channel_mask += 0x8;
139
1.27k
      ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
140
1.27k
      ixheaacd_drc_offset += 1;
141
1.27k
      break;
142
1.27k
    }
143
68.0k
  }
144
85.1k
  for (ch_idx = flag1; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
145
77.2k
    if (ptr_element_type[ch_idx] == 1) {
146
51
      channel_mask += 0x30;
147
51
      ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
148
51
      ixheaacd_drc_offset += 2;
149
51
      flag1 = ch_idx + 1;
150
51
      break;
151
51
    }
152
77.2k
  }
153
78.5k
  for (ch_idx = flag2; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
154
70.9k
    if (ptr_element_type[ch_idx] == 0) {
155
367
      channel_mask += 0x100;
156
367
      ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
157
367
      ixheaacd_drc_offset += 1;
158
367
      break;
159
367
    }
160
70.9k
  }
161
85.3k
  for (ch_idx = flag1; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
162
77.4k
    if (ptr_element_type[ch_idx] == 1) {
163
18
      {
164
18
        channel_mask += (0x40 + 0x80);
165
18
        ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
166
18
        ixheaacd_drc_offset += 2;
167
18
        break;
168
18
      }
169
18
    }
170
77.4k
  }
171
172
7.93k
  return channel_mask;
173
7.93k
}
174
175
VOID ixheaacd_read_data_stream_element(ia_bit_buf_struct *it_bit_buff,
176
                                       WORD32 *byte_align_bits,
177
12.8k
                                       ia_drc_dec_struct *drc_handle) {
178
12.8k
  ia_bit_buf_struct temp_bs = {0};
179
12.8k
  WORD32 count = ixheaacd_read_bits_buf(it_bit_buff, 13);
180
12.8k
  WORD32 cnt = (count & 0xff);
181
12.8k
  WORD32 start_pos = 0;
182
183
12.8k
  if (cnt == 255) {
184
621
    cnt += ixheaacd_read_bits_buf(it_bit_buff, 8);
185
621
  }
186
187
12.8k
  if ((count & 0x0100) >> 8) {
188
10.4k
    ixheaacd_byte_align(it_bit_buff, byte_align_bits);
189
10.4k
  }
190
191
12.8k
  {
192
12.8k
    memcpy(&temp_bs, it_bit_buff, sizeof(ia_bit_buf_struct));
193
12.8k
    start_pos = temp_bs.cnt_bits;
194
195
12.8k
    if (ixheaacd_read_bits_buf(&temp_bs, 8) == DVB_ANC_DATA_SYNC_BYTE) {
196
75
      int dmx_level_present, compression_present;
197
75
      int coarse_gain_present, fine_grain_present;
198
199
75
      ixheaacd_read_bits_buf(&temp_bs, 8);
200
201
75
      ixheaacd_read_bits_buf(&temp_bs, 3);
202
75
      dmx_level_present = ixheaacd_read_bits_buf(&temp_bs, 1);
203
75
      ixheaacd_read_bits_buf(&temp_bs, 1);
204
75
      compression_present = ixheaacd_read_bits_buf(&temp_bs, 1);
205
75
      coarse_gain_present = ixheaacd_read_bits_buf(&temp_bs, 1);
206
75
      fine_grain_present = ixheaacd_read_bits_buf(&temp_bs, 1);
207
208
75
      if (dmx_level_present) ixheaacd_read_bits_buf(&temp_bs, 8);
209
210
75
      if (compression_present) ixheaacd_read_bits_buf(&temp_bs, 16);
211
212
75
      if (coarse_gain_present) ixheaacd_read_bits_buf(&temp_bs, 16);
213
214
75
      if (fine_grain_present) ixheaacd_read_bits_buf(&temp_bs, 16);
215
216
75
      if (!drc_handle->dvb_anc_data_present && temp_bs.cnt_bits >= 0) {
217
53
        drc_handle->dvb_anc_data_pos = start_pos;
218
53
        drc_handle->dvb_anc_data_present = 1;
219
53
      }
220
75
    }
221
12.8k
  }
222
223
12.8k
  if (it_bit_buff->cnt_bits < (cnt << 3)) {
224
110
    longjmp(*(it_bit_buff->xaac_jmp_buf),
225
110
            IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
226
110
  }
227
12.7k
  it_bit_buff->ptr_read_next += cnt;
228
12.7k
  it_bit_buff->cnt_bits -= ((cnt) << 3);
229
12.7k
}
230
231
VOID ixheaacd_read_fill_element(
232
    ia_bit_buf_struct *it_bit_buff, ia_drc_dec_struct *drc_dummy,
233
    ia_drc_dec_struct *ptr_drc_dec, UWORD8 *mps_buffer, WORD32 *mps_header,
234
10.9k
    WORD32 *mps_bytes) {
235
10.9k
  WORD32 count;
236
10.9k
  count = ixheaacd_read_bits_buf(it_bit_buff, 4);
237
238
10.9k
  if ((count - 15) == 0) {
239
7
    count = ixheaacd_read_bits_buf(it_bit_buff, 8);
240
7
    count = (count + 14);
241
7
  }
242
243
10.9k
  if (count > 0) {
244
10.9k
    WORD32 extension_type;
245
246
10.9k
    extension_type = ixheaacd_read_bits_buf(it_bit_buff, 4);
247
248
10.9k
    if (extension_type == EXT_DYNAMIC_RANGE) {
249
12
      ptr_drc_dec->drc_element_found = 1;
250
12
      count -=
251
12
          ixheaacd_dec_drc_read_element(ptr_drc_dec, drc_dummy, it_bit_buff);
252
12
    }
253
10.9k
    if (EXT_SAC_DATA == extension_type) {
254
10.6k
      WORD32 anc_type, i;
255
10.6k
      anc_type = ixheaacd_read_bits_buf(it_bit_buff, 2);
256
10.6k
      *mps_header = anc_type;
257
258
10.6k
      ixheaacd_read_bits_buf(it_bit_buff, 1);
259
260
10.6k
      ixheaacd_read_bits_buf(it_bit_buff, 1);
261
262
105k
      for (i = 0; i < count - 1; i++) {
263
95.3k
        mps_buffer[i] = ixheaacd_read_bits_buf(it_bit_buff, 8);
264
95.3k
      }
265
266
10.6k
      *mps_bytes = count - 1;
267
10.6k
    }
268
301
    else {
269
301
      ixheaacd_read_bits_buf(it_bit_buff, 4);
270
271
301
      if (it_bit_buff->cnt_bits < ((count - 1) << 3)) {
272
26
        longjmp(*(it_bit_buff->xaac_jmp_buf),
273
26
                IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
274
26
      }
275
275
      it_bit_buff->ptr_read_next += count - 1;
276
275
      it_bit_buff->cnt_bits -= ((count - 1) << 3);
277
275
    }
278
10.9k
  }
279
10.9k
}
280
281
WORD32 ixheaacd_get_element_index_tag(
282
    ia_exhaacplus_dec_api_struct *p_obj_enhaacplus_dec, WORD ch_idx1,
283
    WORD *ch_idx, WORD *channel, WORD *ele_idx_order, WORD total_elements,
284
    WORD8 *element_used, WORD total_channels, ia_drc_dec_struct *pstr_drc_dec,
285
    ia_drc_dec_struct *drc_dummy
286
    ,
287
    UWORD8 *mps_buffer, WORD32 *mps_header, WORD32 *mps_bytes
288
61.3k
) {
289
61.3k
  WORD element_tag, j;
290
61.3k
  ia_aac_dec_state_struct *p_state_enhaacplus_dec =
291
61.3k
      p_obj_enhaacplus_dec->p_state_aac;
292
293
61.3k
  ia_bit_buf_struct *it_bit_buff = p_state_enhaacplus_dec->ptr_bit_stream;
294
61.3k
  WORD element_idx;
295
61.3k
  WORD element_type;
296
297
61.3k
  ia_aac_decoder_struct *aac_dec_handle =
298
61.3k
      p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx1];
299
300
61.3k
  *ch_idx = ch_idx1;
301
302
61.3k
  if (p_state_enhaacplus_dec->bs_format != LOAS_BSFORMAT) {
303
34.8k
    if (ch_idx1 == 0) {
304
33.4k
      ixheaacd_byte_align(it_bit_buff, &aac_dec_handle->byte_align_bits);
305
33.4k
    }
306
34.8k
  }
307
61.3k
  {
308
61.3k
    if (ch_idx1 == 0) {
309
45.4k
      aac_dec_handle->byte_align_bits = it_bit_buff->cnt_bits;
310
45.4k
    }
311
61.3k
  }
312
313
61.3k
  if (it_bit_buff->cnt_bits < 3) {
314
5
    it_bit_buff->cnt_bits = -1;
315
5
    return (WORD16)(
316
5
        (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
317
5
  }
318
319
61.3k
  element_tag = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 7);
320
61.3k
  ixheaacd_read_bidirection(it_bit_buff, -7);
321
322
61.3k
  element_idx = (element_tag & 0xF);
323
61.3k
  element_type = (element_tag >> 4) & 0x7;
324
325
61.3k
  p_obj_enhaacplus_dec->aac_config.str_prog_config.alignment_bits =
326
61.3k
      it_bit_buff->bit_pos;
327
328
77.5k
  while (element_type == 4 || element_type == 5 || element_type == 6) {
329
16.4k
    WORD type = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 3);
330
331
16.4k
    if (it_bit_buff->cnt_bits < 3) {
332
0
      it_bit_buff->cnt_bits = -1;
333
0
      return (WORD16)(
334
0
          (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
335
0
    }
336
337
16.4k
    if (type == 4) {
338
5.13k
      ixheaacd_read_data_stream_element(
339
5.13k
          it_bit_buff, &aac_dec_handle->byte_align_bits, pstr_drc_dec);
340
5.13k
    }
341
16.4k
    if (type == 5) {
342
292
      WORD32 error_code = 0;
343
292
      error_code = ixheaacd_decode_pce(
344
292
          it_bit_buff, &p_obj_enhaacplus_dec->aac_config.ui_pce_found_in_hdr,
345
292
          &p_obj_enhaacplus_dec->aac_config.str_prog_config);
346
292
      if (error_code != 0) {
347
181
        if (error_code < 0) return error_code;
348
154
        return IA_XHEAAC_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
349
181
      }
350
292
    }
351
16.2k
    if (type == 6) {
352
10.9k
      ixheaacd_read_fill_element(it_bit_buff, drc_dummy, pstr_drc_dec,
353
10.9k
                                 mps_buffer, mps_header, mps_bytes);
354
10.9k
    }
355
356
16.2k
    if (it_bit_buff->cnt_bits < 7) {
357
9
      it_bit_buff->cnt_bits = -1;
358
9
      return (WORD16)(
359
9
          (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
360
9
    }
361
362
16.2k
    element_tag = (WORD)ixheaacd_aac_showbits_7(it_bit_buff);
363
16.2k
    element_idx = (element_tag & 0xF);
364
16.2k
    element_type = (element_tag >> 4) & 0x7;
365
16.2k
  }
366
367
61.1k
  if (total_elements == 2 && total_channels == 2 &&
368
18.9k
      p_state_enhaacplus_dec->p_config->ui_pce_found_in_hdr == 2 &&
369
1.60k
      ch_idx1 == 0) {
370
863
    ixheaacd_fill_prog_config_slots(p_state_enhaacplus_dec);
371
863
  }
372
373
61.1k
  *channel = 1;
374
61.1k
  if (element_type == 1) {
375
1.45k
    *channel = 2;
376
1.45k
  }
377
378
90.7k
  for (j = 0; j < total_elements; j++) {
379
88.3k
    if (p_obj_enhaacplus_dec->aac_config.element_type[j] == element_type &&
380
82.4k
        (element_idx == ele_idx_order[j]) && (element_used[j] == 0)) {
381
58.7k
      *ch_idx = j;
382
58.7k
      element_used[j] = 1;
383
58.7k
      break;
384
58.7k
    }
385
88.3k
  }
386
387
61.1k
  if (j == total_elements) {
388
2.17k
    if (it_bit_buff->cnt_bits < 0) {
389
0
      return (WORD16)(
390
0
          (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
391
0
    }
392
393
2.17k
    ixheaacd_read_bidirection(
394
2.17k
        it_bit_buff, (WORD16)(it_bit_buff->cnt_bits - it_bit_buff->size));
395
2.17k
    return IA_XHEAAC_DEC_EXE_NONFATAL_ELE_INSTANCE_TAG_NOT_FOUND;
396
2.17k
  } else
397
58.9k
    return 0;
398
61.1k
}