Coverage Report

Created: 2026-03-21 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/decoder/ixheaacd_spectrum_dec.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 <math.h>
21
#include <stdio.h>
22
#include <string.h>
23
#include "ixheaac_type_def.h"
24
25
#include "ixheaacd_bitbuffer.h"
26
27
#include "ixheaacd_interface.h"
28
29
#include "ixheaacd_tns_usac.h"
30
#include "ixheaacd_cnst.h"
31
#include "ixheaacd_error_codes.h"
32
#include "ixheaacd_acelp_info.h"
33
34
#include "ixheaacd_sbrdecsettings.h"
35
#include "ixheaacd_info.h"
36
#include "ixheaacd_sbr_common.h"
37
#include "ixheaacd_drc_data_struct.h"
38
#include "ixheaacd_drc_dec.h"
39
#include "ixheaacd_sbrdecoder.h"
40
#include "ixheaacd_mps_polyphase.h"
41
#include "ixheaac_sbr_const.h"
42
#include "ixheaacd_ec_defines.h"
43
#include "ixheaacd_ec_struct_def.h"
44
#include "ixheaacd_main.h"
45
#include "ixheaacd_arith_dec.h"
46
47
#include "ixheaacd_bit_extract.h"
48
49
#include "ixheaacd_func_def.h"
50
#include "ixheaacd_interface.h"
51
#include "ixheaacd_info.h"
52
53
#include "ixheaacd_defines.h"
54
55
#define MAX_NR_OF_SWB 120
56
57
58.8k
VOID ixheaacd_calc_grp_offset(ia_sfb_info_struct *ptr_sfb_info, pUWORD8 group) {
58
58.8k
  WORD32 group_offset;
59
58.8k
  WORD32 group_idx;
60
58.8k
  WORD32 ixheaacd_drc_offset;
61
58.8k
  WORD16 *group_offset_p;
62
58.8k
  WORD32 sfb, len;
63
64
58.8k
  group_offset = 0;
65
58.8k
  group_idx = 0;
66
336k
  do {
67
336k
    ptr_sfb_info->group_len[group_idx] = group[group_idx] - group_offset;
68
336k
    group_offset = group[group_idx];
69
336k
    group_idx++;
70
336k
  } while (group_offset < 8);
71
58.8k
  ptr_sfb_info->num_groups = group_idx;
72
58.8k
  group_offset_p = ptr_sfb_info->sfb_idx_tbl;
73
58.8k
  ixheaacd_drc_offset = 0;
74
395k
  for (group_idx = 0; group_idx < ptr_sfb_info->num_groups; group_idx++) {
75
336k
    len = ptr_sfb_info->group_len[group_idx];
76
5.30M
    for (sfb = 0; sfb < ptr_sfb_info->sfb_per_sbk; sfb++) {
77
4.97M
      ixheaacd_drc_offset += ptr_sfb_info->sfb_width[sfb] * len;
78
4.97M
      *group_offset_p++ = ixheaacd_drc_offset;
79
4.97M
    }
80
336k
  }
81
58.8k
}
82
83
VOID ixheaacd_read_tns_u(ia_sfb_info_struct *ptr_sfb_info,
84
                         ia_tns_frame_info_struct *pstr_tns_frame_info,
85
45.3k
                         ia_bit_buf_struct *it_bit_buff) {
86
45.3k
  WORD32 j, k, top, coef_res, resolution, compress;
87
45.3k
  WORD32 short_flag, i;
88
45.3k
  WORD16 *sp, tmp, s_mask, n_mask;
89
45.3k
  ia_tns_filter_struct *tns_filt;
90
45.3k
  ia_tns_info_struct *pstr_tns_info;
91
45.3k
  static const WORD16 sgn_mask[] = {0x2, 0x4, 0x8};
92
45.3k
  static const WORD16 neg_mask[] = {(WORD16)0xfffc, (WORD16)0xfff8,
93
45.3k
                                    (WORD16)0xfff0};
94
95
45.3k
  WORD16 n_filt_bits;
96
45.3k
  WORD16 start_band_bits;
97
45.3k
  WORD16 order_bits;
98
99
45.3k
  short_flag = (!ptr_sfb_info->islong);
100
45.3k
  pstr_tns_frame_info->n_subblocks = ptr_sfb_info->max_win_len;
101
102
45.3k
  if (!short_flag) {
103
16.0k
    n_filt_bits = 2;
104
16.0k
    start_band_bits = 6;
105
16.0k
    order_bits = 4;
106
29.2k
  } else {
107
29.2k
    n_filt_bits = 1;
108
29.2k
    start_band_bits = 4;
109
29.2k
    order_bits = 3;
110
29.2k
  }
111
112
295k
  for (i = 0; i < pstr_tns_frame_info->n_subblocks; i++) {
113
250k
    pstr_tns_info = &pstr_tns_frame_info->str_tns_info[i];
114
250k
    if (!(pstr_tns_info->n_filt =
115
250k
              ixheaacd_read_bits_buf(it_bit_buff, n_filt_bits)))
116
155k
      continue;
117
118
95.3k
    pstr_tns_info->coef_res = coef_res =
119
95.3k
        ixheaacd_read_bits_buf(it_bit_buff, 1) + 3;
120
95.3k
    top = ptr_sfb_info->sfb_per_sbk;
121
95.3k
    tns_filt = &pstr_tns_info->str_filter[0];
122
123
200k
    for (j = pstr_tns_info->n_filt; j > 0; j--) {
124
104k
      tns_filt->stop_band = top;
125
104k
      top = tns_filt->start_band =
126
104k
          top - ixheaacd_read_bits_buf(it_bit_buff, start_band_bits);
127
104k
      tns_filt->order = ixheaacd_read_bits_buf(it_bit_buff, order_bits);
128
129
104k
      if (tns_filt->order) {
130
99.3k
        tns_filt->direction = ixheaacd_read_bits_buf(it_bit_buff, 1);
131
99.3k
        compress = ixheaacd_read_bits_buf(it_bit_buff, 1);
132
99.3k
        resolution = coef_res - compress;
133
99.3k
        s_mask = sgn_mask[resolution - 2];
134
99.3k
        n_mask = neg_mask[resolution - 2];
135
99.3k
        sp = tns_filt->coef;
136
137
455k
        for (k = tns_filt->order; k > 0; k--) {
138
356k
          tmp = ixheaacd_read_bits_buf(it_bit_buff, resolution);
139
356k
          *sp++ = (tmp & s_mask) ? (tmp | n_mask) : tmp;
140
356k
        }
141
99.3k
      }
142
143
104k
      tns_filt++;
144
104k
    }
145
95.3k
  }
146
45.3k
}
147
VOID ixheaacd_scale_factor_data(ia_sfb_info_struct *info, WORD32 tot_sfb,
148
                                WORD32 max_sfb, WORD32 sfb_per_sbk,
149
238k
                                WORD8 *ptr_code_book) {
150
238k
  WORD band;
151
238k
  WORD sect_cb;
152
238k
  WORD sect_len;
153
238k
  WORD8 *ptr_codebook = ptr_code_book;
154
238k
  WORD8 *temp_ptr_codebook = ptr_codebook;
155
238k
  WORD32 win_group = info->max_win_len;
156
157
238k
  memset(ptr_codebook, 0, 128);
158
159
238k
  band = 0;
160
1.05M
  while (band < tot_sfb || win_group != 0) {
161
819k
    sect_cb = 11;
162
819k
    sect_len = max_sfb;
163
164
819k
    band = band + sfb_per_sbk;
165
166
819k
    sect_len = sect_len - 1;
167
4.84M
    for (; sect_len >= 0; sect_len--) {
168
4.02M
      *temp_ptr_codebook++ = sect_cb;
169
4.02M
    }
170
819k
    ptr_codebook += 16;
171
819k
    temp_ptr_codebook = ptr_codebook;
172
819k
    win_group--;
173
819k
  }
174
238k
  return;
175
238k
}
176
177
WORD32
178
ixheaacd_win_seq_select(WORD32 window_sequence_curr,
179
202k
                        WORD32 window_sequence_last) {
180
202k
  WORD32 window_sequence;
181
182
202k
  switch (window_sequence_curr) {
183
102k
    case ONLY_LONG_SEQUENCE:
184
102k
      window_sequence = ONLY_LONG_SEQUENCE;
185
102k
      break;
186
187
27.4k
    case LONG_START_SEQUENCE:
188
27.4k
      if ((window_sequence_last == LONG_START_SEQUENCE) ||
189
26.5k
          (window_sequence_last == EIGHT_SHORT_SEQUENCE) ||
190
23.0k
          (window_sequence_last == STOP_START_SEQUENCE)) {
191
23.0k
        window_sequence = STOP_START_SEQUENCE;
192
23.0k
      } else {
193
4.43k
        window_sequence = LONG_START_SEQUENCE;
194
4.43k
      }
195
27.4k
      break;
196
197
13.8k
    case LONG_STOP_SEQUENCE:
198
13.8k
      window_sequence = LONG_STOP_SEQUENCE;
199
13.8k
      break;
200
201
58.8k
    case EIGHT_SHORT_SEQUENCE:
202
58.8k
      window_sequence = EIGHT_SHORT_SEQUENCE;
203
58.8k
      break;
204
205
0
    default:
206
0
      return -1;
207
202k
  }
208
209
202k
  return window_sequence;
210
202k
}
211
212
VOID ixheaacd_tns_reset(ia_sfb_info_struct *ptr_sfb_info,
213
189k
                        ia_tns_frame_info_struct *pstr_tns_frame_info) {
214
189k
  WORD32 s;
215
216
189k
  pstr_tns_frame_info->n_subblocks = ptr_sfb_info->max_win_len;
217
731k
  for (s = 0; s < pstr_tns_frame_info->n_subblocks; s++)
218
542k
    pstr_tns_frame_info->str_tns_info[s].n_filt = 0;
219
189k
}
220
221
VOID ixheaacd_section_data(ia_usac_data_struct *usac_data,
222
                           ia_bit_buf_struct *g_bs, ia_sfb_info_struct *info,
223
                           WORD16 global_gain, pWORD16 factors, pUWORD8 groups,
224
238k
                           WORD8 *ptr_code_book) {
225
238k
  WORD32 band;
226
238k
  WORD16 position = 0;
227
238k
  WORD32 group;
228
238k
  WORD16 factor = global_gain;
229
238k
  WORD8 *temp_codebook_ptr;
230
238k
  WORD16 *ptr_scale_fac, *temp_ptr_scale_fac;
231
238k
  WORD16 norm_val;
232
238k
  WORD32 window_grps, trans_sfb;
233
238k
  WORD16 index, length;
234
238k
  const UWORD16 *hscf = usac_data->huffman_code_book_scl;
235
238k
  const UWORD32 *idx_tab = usac_data->huffman_code_book_scl_index;
236
237
238k
  WORD32 start_bit_pos = g_bs->bit_pos;
238
238k
  UWORD8 *start_read_pos = g_bs->ptr_read_next;
239
238k
  UWORD8 *ptr_read_next = g_bs->ptr_read_next;
240
238k
  WORD32 bit_pos = 7 - g_bs->bit_pos;
241
238k
  WORD32 is_1_group = 1;
242
243
238k
  WORD32 bb = 0, i;
244
238k
  WORD32 increment;
245
238k
  WORD32 read_word =
246
238k
      ixheaacd_aac_showbits_32(ptr_read_next, g_bs->cnt_bits, &increment);
247
238k
  ptr_read_next = g_bs->ptr_read_next + increment;
248
249
238k
  trans_sfb = info->sfb_per_sbk;
250
238k
  temp_ptr_scale_fac = factors;
251
238k
  window_grps = info->max_win_len;
252
238k
  memset(factors, 0, MAXBANDS);
253
238k
  band = trans_sfb - 1;
254
255
852k
  for (group = 0; group < window_grps;) {
256
613k
    temp_codebook_ptr = &ptr_code_book[group * 16];
257
613k
    ptr_scale_fac = temp_ptr_scale_fac;
258
613k
    group = *groups++;
259
14.0M
    for (band = trans_sfb - 1; band >= 0; band--) {
260
13.3M
      WORD32 cb_num = *temp_codebook_ptr++;
261
262
13.3M
      if ((band == trans_sfb - 1) && (is_1_group == 1)) {
263
238k
        *temp_ptr_scale_fac = factor;
264
238k
        temp_ptr_scale_fac++;
265
238k
        continue;
266
238k
      }
267
268
13.1M
      if (cb_num == ZERO_HCB)
269
10.5M
        *temp_ptr_scale_fac++ = 0;
270
2.58M
      else {
271
2.58M
        WORD32 pns_band;
272
2.58M
        WORD16 curr_energy = 0;
273
274
2.58M
        UWORD32 read_word1;
275
276
2.58M
        read_word1 = read_word << bit_pos;
277
278
2.58M
        ixheaacd_huffman_decode(read_word1, &index, &length, hscf, idx_tab);
279
280
2.58M
        bit_pos += length;
281
2.58M
        ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
282
2.58M
                                     g_bs->ptr_bit_buf_end);
283
2.58M
        norm_val = index - 60;
284
285
2.58M
        if (cb_num > NOISE_HCB) {
286
0
          position = position + norm_val;
287
0
          *temp_ptr_scale_fac++ = -position;
288
289
2.58M
        } else if (cb_num < NOISE_HCB) {
290
2.58M
          factor = factor + norm_val;
291
2.58M
          *temp_ptr_scale_fac++ = factor;
292
293
2.58M
        } else {
294
0
          curr_energy += norm_val;
295
296
0
          pns_band = (group << 4) + trans_sfb - band - 1;
297
298
0
          temp_ptr_scale_fac[pns_band] = curr_energy;
299
300
0
          temp_ptr_scale_fac++;
301
0
        }
302
2.58M
      }
303
13.1M
    }
304
613k
    is_1_group = 0;
305
306
613k
    if (!(info->islong)) {
307
664k
      for (bb++; bb < group; bb++) {
308
3.25M
        for (i = 0; i < trans_sfb; i++) {
309
3.05M
          ptr_scale_fac[i + trans_sfb] = ptr_scale_fac[i];
310
3.05M
        }
311
205k
        temp_ptr_scale_fac += trans_sfb;
312
205k
        ptr_scale_fac += trans_sfb;
313
205k
      }
314
458k
    }
315
613k
  }
316
238k
  ptr_read_next = ptr_read_next - increment;
317
238k
  ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
318
238k
                               g_bs->ptr_bit_buf_end);
319
320
238k
  g_bs->ptr_read_next = ptr_read_next;
321
322
238k
  g_bs->bit_pos = 7 - bit_pos;
323
238k
  {
324
238k
    WORD32 bits_consumed;
325
238k
    bits_consumed = (WORD32)(((g_bs->ptr_read_next - start_read_pos) << 3) +
326
238k
                             (start_bit_pos - g_bs->bit_pos));
327
238k
    g_bs->cnt_bits -= bits_consumed;
328
238k
  }
329
238k
}
330
331
WORD32 ixheaacd_fd_channel_stream(
332
    ia_usac_data_struct *usac_data,
333
    ia_usac_tmp_core_coder_struct *pstr_core_coder, UWORD8 *max_sfb,
334
    WORD32 window_sequence_last, WORD32 chn, WORD32 noise_filling, WORD32 ch,
335
    ia_bit_buf_struct *it_bit_buff
336
337
    )
338
339
238k
{
340
238k
  WORD32 i;
341
342
238k
  WORD32 tot_sfb;
343
238k
  WORD32 noise_level = 0;
344
238k
  WORD32 arith_reset_flag;
345
346
238k
  WORD32 arth_size;
347
238k
  WORD16 global_gain;
348
238k
  WORD32 max_spec_coefficients;
349
238k
  WORD32 err_code = 0;
350
238k
  WORD32 noise_offset;
351
352
238k
  WORD32 *fac_data;
353
238k
  ia_sfb_info_struct *info;
354
355
238k
  WORD8 *ptr_code_book = (WORD8 *)&usac_data->scratch_buffer;
356
357
238k
  global_gain = ixheaacd_read_bits_buf(it_bit_buff, 8);
358
359
238k
  if (noise_filling) {
360
145k
    noise_level = ixheaacd_read_bits_buf(it_bit_buff, 3);
361
145k
    noise_offset = ixheaacd_read_bits_buf(it_bit_buff, 5);
362
363
145k
  } else {
364
93.2k
    noise_level = 0;
365
93.2k
    noise_offset = 0;
366
93.2k
  }
367
368
238k
  if (!pstr_core_coder->common_window) {
369
166k
    err_code = ixheaacd_ics_info(usac_data, chn, max_sfb, it_bit_buff,
370
166k
                                 window_sequence_last);
371
372
166k
    if (err_code == -1) {
373
246
      if (usac_data->ec_flag) {
374
0
        memcpy(usac_data->max_sfb, pstr_core_coder->max_sfb, sizeof(pstr_core_coder->max_sfb));
375
0
        longjmp(*(it_bit_buff->xaac_jmp_buf),
376
0
                IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
377
246
      } else {
378
246
        return err_code;
379
246
      }
380
246
    }
381
166k
  }
382
238k
  info = usac_data->pstr_sfb_info[chn];
383
384
238k
  if (!pstr_core_coder->common_tw && usac_data->tw_mdct[0] == 1) {
385
0
    usac_data->tw_data_present[chn] = ixheaacd_read_bits_buf(it_bit_buff, 1);
386
0
    if (usac_data->tw_data_present[chn]) {
387
0
      WORD32 i;
388
0
      for (i = 0; i < NUM_TW_NODES; i++) {
389
0
        usac_data->tw_ratio[chn][i] = ixheaacd_read_bits_buf(it_bit_buff, 3);
390
0
      }
391
0
    }
392
0
  }
393
394
238k
  if (*max_sfb == 0) {
395
94.7k
    tot_sfb = 0;
396
143k
  } else {
397
143k
    i = 0;
398
143k
    tot_sfb = info->sfb_per_sbk;
399
400
373k
    while (usac_data->group_dis[chn][i++] < info->max_win_len) {
401
230k
      tot_sfb += info->sfb_per_sbk;
402
230k
    }
403
143k
  }
404
405
238k
  ixheaacd_scale_factor_data(info, tot_sfb, *max_sfb, info->sfb_per_sbk,
406
238k
                             ptr_code_book);
407
408
238k
  if ((it_bit_buff->ptr_read_next > it_bit_buff->ptr_bit_buf_end - 3) &&
409
71
      (it_bit_buff->size == it_bit_buff->max_size)) {
410
0
    return -1;
411
0
  }
412
413
238k
  ixheaacd_section_data(usac_data, it_bit_buff, info, global_gain,
414
238k
                        usac_data->factors[chn], usac_data->group_dis[chn],
415
238k
                        ptr_code_book);
416
417
238k
  if (pstr_core_coder->tns_data_present[ch] == 0)
418
189k
    ixheaacd_tns_reset(info, usac_data->pstr_tns[chn]);
419
420
238k
  if (pstr_core_coder->tns_data_present[ch] == 1)
421
41.6k
    ixheaacd_read_tns_u(info, usac_data->pstr_tns[chn], it_bit_buff);
422
423
238k
  if (*max_sfb > 0) {
424
143k
    max_spec_coefficients =
425
143k
        info->sfb_idx_tbl[*max_sfb - 1] / info->group_len[0];
426
143k
  } else {
427
94.7k
    max_spec_coefficients = 0;
428
94.7k
  }
429
430
238k
  if (usac_data->usac_independency_flg)
431
17.7k
    arith_reset_flag = 1;
432
220k
  else
433
220k
    arith_reset_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
434
435
238k
  switch (usac_data->window_sequence[chn]) {
436
83.0k
    case EIGHT_SHORT_SEQUENCE:
437
83.0k
      arth_size = usac_data->ccfl / 8;
438
83.0k
      break;
439
155k
    default:
440
155k
      arth_size = usac_data->ccfl;
441
155k
      break;
442
238k
  }
443
444
238k
  err_code = ixheaacd_ac_spectral_data(
445
238k
      usac_data, max_spec_coefficients, noise_level, noise_offset, arth_size,
446
238k
      it_bit_buff, *max_sfb, arith_reset_flag, noise_filling, chn);
447
448
238k
  if (err_code != 0) return err_code;
449
450
238k
  usac_data->fac_data_present[chn] = ixheaacd_read_bits_buf(it_bit_buff, 1);
451
452
238k
  if (usac_data->fac_data_present[chn]) {
453
80.4k
    WORD32 fac_len;
454
80.4k
    if ((usac_data->window_sequence[chn]) == EIGHT_SHORT_SEQUENCE) {
455
25.7k
      fac_len = (usac_data->ccfl) / 16;
456
54.7k
    } else {
457
54.7k
      fac_len = (usac_data->ccfl) / 8;
458
54.7k
    }
459
460
80.4k
    fac_data = usac_data->fac_data[chn];
461
80.4k
    fac_data[0] = ixheaacd_read_bits_buf(it_bit_buff, 7);
462
80.4k
    ixheaacd_fac_decoding(fac_len, 0, &fac_data[1], it_bit_buff);
463
80.4k
  }
464
465
238k
  return 0;
466
238k
}