Coverage Report

Created: 2026-02-07 06:17

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.5k
VOID ixheaacd_calc_grp_offset(ia_sfb_info_struct *ptr_sfb_info, pUWORD8 group) {
58
58.5k
  WORD32 group_offset;
59
58.5k
  WORD32 group_idx;
60
58.5k
  WORD32 ixheaacd_drc_offset;
61
58.5k
  WORD16 *group_offset_p;
62
58.5k
  WORD32 sfb, len;
63
64
58.5k
  group_offset = 0;
65
58.5k
  group_idx = 0;
66
338k
  do {
67
338k
    ptr_sfb_info->group_len[group_idx] = group[group_idx] - group_offset;
68
338k
    group_offset = group[group_idx];
69
338k
    group_idx++;
70
338k
  } while (group_offset < 8);
71
58.5k
  ptr_sfb_info->num_groups = group_idx;
72
58.5k
  group_offset_p = ptr_sfb_info->sfb_idx_tbl;
73
58.5k
  ixheaacd_drc_offset = 0;
74
396k
  for (group_idx = 0; group_idx < ptr_sfb_info->num_groups; group_idx++) {
75
338k
    len = ptr_sfb_info->group_len[group_idx];
76
5.34M
    for (sfb = 0; sfb < ptr_sfb_info->sfb_per_sbk; sfb++) {
77
5.00M
      ixheaacd_drc_offset += ptr_sfb_info->sfb_width[sfb] * len;
78
5.00M
      *group_offset_p++ = ixheaacd_drc_offset;
79
5.00M
    }
80
338k
  }
81
58.5k
}
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
42.1k
                         ia_bit_buf_struct *it_bit_buff) {
86
42.1k
  WORD32 j, k, top, coef_res, resolution, compress;
87
42.1k
  WORD32 short_flag, i;
88
42.1k
  WORD16 *sp, tmp, s_mask, n_mask;
89
42.1k
  ia_tns_filter_struct *tns_filt;
90
42.1k
  ia_tns_info_struct *pstr_tns_info;
91
42.1k
  static const WORD16 sgn_mask[] = {0x2, 0x4, 0x8};
92
42.1k
  static const WORD16 neg_mask[] = {(WORD16)0xfffc, (WORD16)0xfff8,
93
42.1k
                                    (WORD16)0xfff0};
94
95
42.1k
  WORD16 n_filt_bits;
96
42.1k
  WORD16 start_band_bits;
97
42.1k
  WORD16 order_bits;
98
99
42.1k
  short_flag = (!ptr_sfb_info->islong);
100
42.1k
  pstr_tns_frame_info->n_subblocks = ptr_sfb_info->max_win_len;
101
102
42.1k
  if (!short_flag) {
103
16.1k
    n_filt_bits = 2;
104
16.1k
    start_band_bits = 6;
105
16.1k
    order_bits = 4;
106
26.0k
  } else {
107
26.0k
    n_filt_bits = 1;
108
26.0k
    start_band_bits = 4;
109
26.0k
    order_bits = 3;
110
26.0k
  }
111
112
266k
  for (i = 0; i < pstr_tns_frame_info->n_subblocks; i++) {
113
224k
    pstr_tns_info = &pstr_tns_frame_info->str_tns_info[i];
114
224k
    if (!(pstr_tns_info->n_filt =
115
224k
              ixheaacd_read_bits_buf(it_bit_buff, n_filt_bits)))
116
139k
      continue;
117
118
85.2k
    pstr_tns_info->coef_res = coef_res =
119
85.2k
        ixheaacd_read_bits_buf(it_bit_buff, 1) + 3;
120
85.2k
    top = ptr_sfb_info->sfb_per_sbk;
121
85.2k
    tns_filt = &pstr_tns_info->str_filter[0];
122
123
179k
    for (j = pstr_tns_info->n_filt; j > 0; j--) {
124
94.3k
      tns_filt->stop_band = top;
125
94.3k
      top = tns_filt->start_band =
126
94.3k
          top - ixheaacd_read_bits_buf(it_bit_buff, start_band_bits);
127
94.3k
      tns_filt->order = ixheaacd_read_bits_buf(it_bit_buff, order_bits);
128
129
94.3k
      if (tns_filt->order) {
130
89.2k
        tns_filt->direction = ixheaacd_read_bits_buf(it_bit_buff, 1);
131
89.2k
        compress = ixheaacd_read_bits_buf(it_bit_buff, 1);
132
89.2k
        resolution = coef_res - compress;
133
89.2k
        s_mask = sgn_mask[resolution - 2];
134
89.2k
        n_mask = neg_mask[resolution - 2];
135
89.2k
        sp = tns_filt->coef;
136
137
418k
        for (k = tns_filt->order; k > 0; k--) {
138
329k
          tmp = ixheaacd_read_bits_buf(it_bit_buff, resolution);
139
329k
          *sp++ = (tmp & s_mask) ? (tmp | n_mask) : tmp;
140
329k
        }
141
89.2k
      }
142
143
94.3k
      tns_filt++;
144
94.3k
    }
145
85.2k
  }
146
42.1k
}
147
VOID ixheaacd_scale_factor_data(ia_sfb_info_struct *info, WORD32 tot_sfb,
148
                                WORD32 max_sfb, WORD32 sfb_per_sbk,
149
240k
                                WORD8 *ptr_code_book) {
150
240k
  WORD band;
151
240k
  WORD sect_cb;
152
240k
  WORD sect_len;
153
240k
  WORD8 *ptr_codebook = ptr_code_book;
154
240k
  WORD8 *temp_ptr_codebook = ptr_codebook;
155
240k
  WORD32 win_group = info->max_win_len;
156
157
240k
  memset(ptr_codebook, 0, 128);
158
159
240k
  band = 0;
160
1.04M
  while (band < tot_sfb || win_group != 0) {
161
809k
    sect_cb = 11;
162
809k
    sect_len = max_sfb;
163
164
809k
    band = band + sfb_per_sbk;
165
166
809k
    sect_len = sect_len - 1;
167
4.66M
    for (; sect_len >= 0; sect_len--) {
168
3.85M
      *temp_ptr_codebook++ = sect_cb;
169
3.85M
    }
170
809k
    ptr_codebook += 16;
171
809k
    temp_ptr_codebook = ptr_codebook;
172
809k
    win_group--;
173
809k
  }
174
240k
  return;
175
240k
}
176
177
WORD32
178
ixheaacd_win_seq_select(WORD32 window_sequence_curr,
179
205k
                        WORD32 window_sequence_last) {
180
205k
  WORD32 window_sequence;
181
182
205k
  switch (window_sequence_curr) {
183
105k
    case ONLY_LONG_SEQUENCE:
184
105k
      window_sequence = ONLY_LONG_SEQUENCE;
185
105k
      break;
186
187
29.6k
    case LONG_START_SEQUENCE:
188
29.6k
      if ((window_sequence_last == LONG_START_SEQUENCE) ||
189
28.8k
          (window_sequence_last == EIGHT_SHORT_SEQUENCE) ||
190
25.9k
          (window_sequence_last == STOP_START_SEQUENCE)) {
191
25.9k
        window_sequence = STOP_START_SEQUENCE;
192
25.9k
      } else {
193
3.71k
        window_sequence = LONG_START_SEQUENCE;
194
3.71k
      }
195
29.6k
      break;
196
197
11.9k
    case LONG_STOP_SEQUENCE:
198
11.9k
      window_sequence = LONG_STOP_SEQUENCE;
199
11.9k
      break;
200
201
58.5k
    case EIGHT_SHORT_SEQUENCE:
202
58.5k
      window_sequence = EIGHT_SHORT_SEQUENCE;
203
58.5k
      break;
204
205
0
    default:
206
0
      return -1;
207
205k
  }
208
209
205k
  return window_sequence;
210
205k
}
211
212
VOID ixheaacd_tns_reset(ia_sfb_info_struct *ptr_sfb_info,
213
194k
                        ia_tns_frame_info_struct *pstr_tns_frame_info) {
214
194k
  WORD32 s;
215
216
194k
  pstr_tns_frame_info->n_subblocks = ptr_sfb_info->max_win_len;
217
755k
  for (s = 0; s < pstr_tns_frame_info->n_subblocks; s++)
218
560k
    pstr_tns_frame_info->str_tns_info[s].n_filt = 0;
219
194k
}
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
240k
                           WORD8 *ptr_code_book) {
225
240k
  WORD32 band;
226
240k
  WORD16 position = 0;
227
240k
  WORD32 group;
228
240k
  WORD16 factor = global_gain;
229
240k
  WORD8 *temp_codebook_ptr;
230
240k
  WORD16 *ptr_scale_fac, *temp_ptr_scale_fac;
231
240k
  WORD16 norm_val;
232
240k
  WORD32 window_grps, trans_sfb;
233
240k
  WORD16 index, length;
234
240k
  const UWORD16 *hscf = usac_data->huffman_code_book_scl;
235
240k
  const UWORD32 *idx_tab = usac_data->huffman_code_book_scl_index;
236
237
240k
  WORD32 start_bit_pos = g_bs->bit_pos;
238
240k
  UWORD8 *start_read_pos = g_bs->ptr_read_next;
239
240k
  UWORD8 *ptr_read_next = g_bs->ptr_read_next;
240
240k
  WORD32 bit_pos = 7 - g_bs->bit_pos;
241
240k
  WORD32 is_1_group = 1;
242
243
240k
  WORD32 bb = 0, i;
244
240k
  WORD32 increment;
245
240k
  WORD32 read_word =
246
240k
      ixheaacd_aac_showbits_32(ptr_read_next, g_bs->cnt_bits, &increment);
247
240k
  ptr_read_next = g_bs->ptr_read_next + increment;
248
249
240k
  trans_sfb = info->sfb_per_sbk;
250
240k
  temp_ptr_scale_fac = factors;
251
240k
  window_grps = info->max_win_len;
252
240k
  memset(factors, 0, MAXBANDS);
253
240k
  band = trans_sfb - 1;
254
255
852k
  for (group = 0; group < window_grps;) {
256
612k
    temp_codebook_ptr = &ptr_code_book[group * 16];
257
612k
    ptr_scale_fac = temp_ptr_scale_fac;
258
612k
    group = *groups++;
259
14.1M
    for (band = trans_sfb - 1; band >= 0; band--) {
260
13.5M
      WORD32 cb_num = *temp_codebook_ptr++;
261
262
13.5M
      if ((band == trans_sfb - 1) && (is_1_group == 1)) {
263
240k
        *temp_ptr_scale_fac = factor;
264
240k
        temp_ptr_scale_fac++;
265
240k
        continue;
266
240k
      }
267
268
13.2M
      if (cb_num == ZERO_HCB)
269
10.7M
        *temp_ptr_scale_fac++ = 0;
270
2.49M
      else {
271
2.49M
        WORD32 pns_band;
272
2.49M
        WORD16 curr_energy = 0;
273
274
2.49M
        UWORD32 read_word1;
275
276
2.49M
        read_word1 = read_word << bit_pos;
277
278
2.49M
        ixheaacd_huffman_decode(read_word1, &index, &length, hscf, idx_tab);
279
280
2.49M
        bit_pos += length;
281
2.49M
        ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
282
2.49M
                                     g_bs->ptr_bit_buf_end);
283
2.49M
        norm_val = index - 60;
284
285
2.49M
        if (cb_num > NOISE_HCB) {
286
0
          position = position + norm_val;
287
0
          *temp_ptr_scale_fac++ = -position;
288
289
2.49M
        } else if (cb_num < NOISE_HCB) {
290
2.49M
          factor = factor + norm_val;
291
2.49M
          *temp_ptr_scale_fac++ = factor;
292
293
2.49M
        } 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.49M
      }
303
13.2M
    }
304
612k
    is_1_group = 0;
305
306
612k
    if (!(info->islong)) {
307
650k
      for (bb++; bb < group; bb++) {
308
3.12M
        for (i = 0; i < trans_sfb; i++) {
309
2.92M
          ptr_scale_fac[i + trans_sfb] = ptr_scale_fac[i];
310
2.92M
        }
311
196k
        temp_ptr_scale_fac += trans_sfb;
312
196k
        ptr_scale_fac += trans_sfb;
313
196k
      }
314
453k
    }
315
612k
  }
316
240k
  ptr_read_next = ptr_read_next - increment;
317
240k
  ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
318
240k
                               g_bs->ptr_bit_buf_end);
319
320
240k
  g_bs->ptr_read_next = ptr_read_next;
321
322
240k
  g_bs->bit_pos = 7 - bit_pos;
323
240k
  {
324
240k
    WORD32 bits_consumed;
325
240k
    bits_consumed = (WORD32)(((g_bs->ptr_read_next - start_read_pos) << 3) +
326
240k
                             (start_bit_pos - g_bs->bit_pos));
327
240k
    g_bs->cnt_bits -= bits_consumed;
328
240k
  }
329
240k
}
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
240k
{
340
240k
  WORD32 i;
341
342
240k
  WORD32 tot_sfb;
343
240k
  WORD32 noise_level = 0;
344
240k
  WORD32 arith_reset_flag;
345
346
240k
  WORD32 arth_size;
347
240k
  WORD16 global_gain;
348
240k
  WORD32 max_spec_coefficients;
349
240k
  WORD32 err_code = 0;
350
240k
  WORD32 noise_offset;
351
352
240k
  WORD32 *fac_data;
353
240k
  ia_sfb_info_struct *info;
354
355
240k
  WORD8 *ptr_code_book = (WORD8 *)&usac_data->scratch_buffer;
356
357
240k
  global_gain = ixheaacd_read_bits_buf(it_bit_buff, 8);
358
359
240k
  if (noise_filling) {
360
154k
    noise_level = ixheaacd_read_bits_buf(it_bit_buff, 3);
361
154k
    noise_offset = ixheaacd_read_bits_buf(it_bit_buff, 5);
362
363
154k
  } else {
364
86.3k
    noise_level = 0;
365
86.3k
    noise_offset = 0;
366
86.3k
  }
367
368
240k
  if (!pstr_core_coder->common_window) {
369
169k
    err_code = ixheaacd_ics_info(usac_data, chn, max_sfb, it_bit_buff,
370
169k
                                 window_sequence_last);
371
372
169k
    if (err_code == -1) {
373
348
      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
348
      } else {
378
348
        return err_code;
379
348
      }
380
348
    }
381
169k
  }
382
240k
  info = usac_data->pstr_sfb_info[chn];
383
384
240k
  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
240k
  if (*max_sfb == 0) {
395
100k
    tot_sfb = 0;
396
140k
  } else {
397
140k
    i = 0;
398
140k
    tot_sfb = info->sfb_per_sbk;
399
400
352k
    while (usac_data->group_dis[chn][i++] < info->max_win_len) {
401
212k
      tot_sfb += info->sfb_per_sbk;
402
212k
    }
403
140k
  }
404
405
240k
  ixheaacd_scale_factor_data(info, tot_sfb, *max_sfb, info->sfb_per_sbk,
406
240k
                             ptr_code_book);
407
408
240k
  if ((it_bit_buff->ptr_read_next > it_bit_buff->ptr_bit_buf_end - 3) &&
409
70
      (it_bit_buff->size == it_bit_buff->max_size)) {
410
0
    return -1;
411
0
  }
412
413
240k
  ixheaacd_section_data(usac_data, it_bit_buff, info, global_gain,
414
240k
                        usac_data->factors[chn], usac_data->group_dis[chn],
415
240k
                        ptr_code_book);
416
417
240k
  if (pstr_core_coder->tns_data_present[ch] == 0)
418
194k
    ixheaacd_tns_reset(info, usac_data->pstr_tns[chn]);
419
420
240k
  if (pstr_core_coder->tns_data_present[ch] == 1)
421
38.5k
    ixheaacd_read_tns_u(info, usac_data->pstr_tns[chn], it_bit_buff);
422
423
240k
  if (*max_sfb > 0) {
424
139k
    max_spec_coefficients =
425
139k
        info->sfb_idx_tbl[*max_sfb - 1] / info->group_len[0];
426
139k
  } else {
427
100k
    max_spec_coefficients = 0;
428
100k
  }
429
430
240k
  if (usac_data->usac_independency_flg)
431
16.8k
    arith_reset_flag = 1;
432
223k
  else
433
223k
    arith_reset_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
434
435
240k
  switch (usac_data->window_sequence[chn]) {
436
81.2k
    case EIGHT_SHORT_SEQUENCE:
437
81.2k
      arth_size = usac_data->ccfl / 8;
438
81.2k
      break;
439
159k
    default:
440
159k
      arth_size = usac_data->ccfl;
441
159k
      break;
442
240k
  }
443
444
240k
  err_code = ixheaacd_ac_spectral_data(
445
240k
      usac_data, max_spec_coefficients, noise_level, noise_offset, arth_size,
446
240k
      it_bit_buff, *max_sfb, arith_reset_flag, noise_filling, chn);
447
448
240k
  if (err_code != 0) return err_code;
449
450
240k
  usac_data->fac_data_present[chn] = ixheaacd_read_bits_buf(it_bit_buff, 1);
451
452
240k
  if (usac_data->fac_data_present[chn]) {
453
71.3k
    WORD32 fac_len;
454
71.3k
    if ((usac_data->window_sequence[chn]) == EIGHT_SHORT_SEQUENCE) {
455
24.0k
      fac_len = (usac_data->ccfl) / 16;
456
47.2k
    } else {
457
47.2k
      fac_len = (usac_data->ccfl) / 8;
458
47.2k
    }
459
460
71.3k
    fac_data = usac_data->fac_data[chn];
461
71.3k
    fac_data[0] = ixheaacd_read_bits_buf(it_bit_buff, 7);
462
71.3k
    ixheaacd_fac_decoding(fac_len, 0, &fac_data[1], it_bit_buff);
463
71.3k
  }
464
465
240k
  return 0;
466
240k
}