Coverage Report

Created: 2026-12-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/decoder/ixheaacd_mps_res_channel.c
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2023 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 "ixheaac_type_def.h"
22
#include "ixheaac_constants.h"
23
#include "ixheaacd_cnst.h"
24
#include "ixheaac_basic_ops32.h"
25
#include "ixheaac_basic_ops16.h"
26
#include "ixheaac_basic_ops40.h"
27
#include "ixheaac_basic_ops.h"
28
#include "ixheaacd_bitbuffer.h"
29
#include "ixheaacd_mps_aac_struct.h"
30
#include "ixheaacd_mps_res_rom.h"
31
#include "ixheaacd_mps_res_pulsedata.h"
32
#include "ixheaacd_mps_res_channelinfo.h"
33
#include "ixheaacd_mps_res_block.h"
34
#include "ixheaacd_defines.h"
35
#include "ixheaacd_mps_res_channel.h"
36
#include "ixheaac_basic_op.h"
37
#include "ixheaacd_mps_res_tns.h"
38
#include "ixheaacd_mps_res.h"
39
#include "ixheaacd_mps_res_huffman.h"
40
41
4.23k
UWORD32 ixheaacd_res_aac_showbits_32(UWORD8 *p_read_next) {
42
4.23k
  UWORD8 *v = p_read_next;
43
4.23k
  UWORD32 b = 0;
44
45
4.23k
  _SWAP(v, b);
46
4.23k
  return b;
47
4.23k
}
48
49
static WORD16 ixheaacd_res_c_block_read(
50
    ia_bit_buf_struct *it_bit_buf,
51
    ia_mps_dec_residual_channel_info_struct *p_aac_decoder_channel_info, WORD16 global_gain,
52
    ia_mps_dec_residual_aac_tables_struct *aac_tables_ptr)
53
54
2.17k
{
55
2.17k
  FLAG gain_control_data_present;
56
2.17k
  WORD16 error_status = AAC_DEC_OK;
57
58
2.17k
  if (p_aac_decoder_channel_info->ics_info.window_sequence == EIGHT_SHORT_SEQUENCE) {
59
769
    memset(p_aac_decoder_channel_info->p_scale_factor, 0, MAX_WINDOWS * MAX_SFB_SHORT * 3);
60
769
  }
61
62
2.17k
  error_status = ixheaacd_c_block_read_section_data(it_bit_buf, p_aac_decoder_channel_info);
63
64
2.17k
  if (error_status) return error_status;
65
66
2.14k
  ixheaacd_res_c_block_read_scf_data(it_bit_buf, p_aac_decoder_channel_info, global_gain,
67
2.14k
                                     aac_tables_ptr);
68
69
2.14k
  error_status = ixheaacd_res_c_pulse_data_read(
70
2.14k
      it_bit_buf, &p_aac_decoder_channel_info->pulse_data, aac_tables_ptr);
71
2.14k
  if (error_status) return error_status;
72
73
2.13k
  p_aac_decoder_channel_info->tns_data.tns_data_present =
74
2.13k
      (FLAG)ixheaacd_read_bits_buf(it_bit_buf, 1);
75
76
2.13k
  error_status = ixheaacd_res_c_tns_read(it_bit_buf, p_aac_decoder_channel_info);
77
2.13k
  if (error_status) {
78
5
    return error_status;
79
5
  }
80
81
2.13k
  gain_control_data_present = ixheaacd_read_bits_buf(it_bit_buf, 1);
82
83
2.13k
  if (gain_control_data_present) {
84
4
    return (WORD16)((WORD32)AAC_DEC_UNIMPLEMENTED_GAIN_CONTROL_DATA);
85
4
  }
86
87
2.12k
  {
88
2.12k
    it_bit_buf->bit_pos = (7 - it_bit_buf->bit_pos);
89
90
2.12k
    error_status = ixheaacd_res_c_block_read_spec_data(it_bit_buf, p_aac_decoder_channel_info,
91
2.12k
                                                       aac_tables_ptr);
92
93
2.12k
    it_bit_buf->bit_pos = (7 - it_bit_buf->bit_pos);
94
2.12k
  }
95
96
2.12k
  return error_status;
97
2.13k
}
98
99
WORD16 ixheaacd_res_read_ics(
100
    ia_bit_buf_struct *it_bit_buf,
101
    ia_mps_dec_residual_channel_info_struct *p_aac_decoder_channel_info[CHANNELS], WORD32 num_ch,
102
2.23k
    ia_mps_dec_residual_aac_tables_struct *aac_tables_ptr, WORD8 tot_sf_bands_ls[2]) {
103
2.23k
  WORD16 error_status = AAC_DEC_OK;
104
2.23k
  WORD32 ch;
105
106
4.33k
  for (ch = 0; ch < num_ch; ch++) {
107
2.23k
    ia_mps_dec_residual_channel_info_struct *p_aac_dec_ch_info = p_aac_decoder_channel_info[ch];
108
2.23k
    ia_mps_dec_residual_ics_info_struct *p_ics_info = &p_aac_dec_ch_info->ics_info;
109
110
2.23k
    p_aac_dec_ch_info->global_gain = (WORD16)ixheaacd_read_bits_buf(it_bit_buf, 8);
111
112
2.23k
    if (!p_aac_decoder_channel_info[LEFT]->common_window) {
113
2.20k
      error_status = ixheaacd_res_ics_read(it_bit_buf, p_ics_info, tot_sf_bands_ls);
114
2.20k
      if (error_status) {
115
25
        if (it_bit_buf->cnt_bits < 0)
116
0
          error_status = (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
117
25
        return error_status;
118
25
      }
119
2.20k
    }
120
121
2.20k
    error_status = ixheaacd_res_c_block_read(it_bit_buf, p_aac_dec_ch_info,
122
2.20k
                                             p_aac_dec_ch_info->global_gain, aac_tables_ptr);
123
2.20k
    if (error_status) {
124
99
      if (it_bit_buf->cnt_bits < 0)
125
0
        error_status = (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
126
127
99
      return error_status;
128
99
    }
129
2.20k
  }
130
131
2.10k
  return error_status;
132
2.23k
}
133
134
VOID ixheaacd_res_c_pulse_data_apply(ia_mps_dec_residual_pulse_data_struct *pulse_data,
135
                                     WORD8 *p_pulse_arr,
136
1.38k
                                     const WORD16 *p_scale_factor_band_offsets) {
137
1.38k
  WORD i, number_pulse;
138
1.38k
  WORD32 k;
139
140
1.38k
  memset(p_pulse_arr, 0, sizeof(WORD32) * 256);
141
142
1.38k
  if (pulse_data->pulse_data_present) {
143
112
    k = p_scale_factor_band_offsets[pulse_data->pulse_start_band];
144
112
    number_pulse = pulse_data->number_pulse;
145
146
350
    for (i = 0; i <= number_pulse; i++) {
147
238
      k = add_d(k, pulse_data->pulse_offset[i]);
148
238
      p_pulse_arr[k] = pulse_data->pulse_amp[i];
149
238
    }
150
112
  }
151
1.38k
}
152
153
WORD16 ixheaacd_res_c_block_read_spec_data(
154
    ia_bit_buf_struct *it_bit_buf,
155
    ia_mps_dec_residual_channel_info_struct *p_aac_decoder_channel_info,
156
2.11k
    ia_mps_dec_residual_aac_tables_struct *aac_tables_ptr) {
157
2.11k
  WORD band, tot_bands, tot_groups = 0;
158
2.11k
  WORD group, groupwin, groupoffset;
159
160
2.11k
  WORD index;
161
2.11k
  WORD8 *p_code_book, *p_codebook_tmp;
162
2.11k
  WORD16 *p_scale_factor;
163
2.11k
  WORD32 *p_spectral_coefficient;
164
2.11k
  ia_mps_dec_residual_ics_info_struct *p_ics_info = &p_aac_decoder_channel_info->ics_info;
165
2.11k
  WORD16 *band_offsets;
166
2.11k
  WORD32 maximum_bins_short = ixheaac_shr16_dir_sat(p_ics_info->frame_length, 3);
167
168
2.11k
  WORD32 *p_spec_coeff_out;
169
170
2.11k
  p_code_book = p_aac_decoder_channel_info->p_code_book;
171
2.11k
  p_scale_factor = p_aac_decoder_channel_info->p_scale_factor;
172
2.11k
  p_spectral_coefficient = p_aac_decoder_channel_info->p_spectral_coefficient;
173
2.11k
  tot_groups = p_ics_info->window_groups;
174
2.11k
  tot_bands = p_ics_info->max_sf_bands;
175
2.11k
  band_offsets = (WORD16 *)ixheaacd_res_get_sfb_offsets(p_ics_info, aac_tables_ptr);
176
177
2.11k
  if (p_aac_decoder_channel_info->ics_info.window_sequence != EIGHT_SHORT_SEQUENCE) {
178
1.38k
    WORD8 *p_pul_arr = (WORD8 *)p_aac_decoder_channel_info->p_tns_scratch;
179
1.38k
    ixheaacd_res_c_pulse_data_apply(&p_aac_decoder_channel_info->pulse_data, p_pul_arr,
180
1.38k
                                    band_offsets);
181
182
1.38k
    p_spec_coeff_out = &p_spectral_coefficient[0];
183
2.62k
    for (band = 0; band < tot_bands;) {
184
1.26k
      WORD ret_val;
185
1.26k
      WORD32 len;
186
1.26k
      WORD32 code_no = p_code_book[band];
187
1.26k
      WORD start = band;
188
189
13.1k
      for (; band < tot_bands && (p_code_book[band] == code_no); band++)
190
11.8k
        ;
191
192
1.26k
      len = band_offsets[band] - band_offsets[start];
193
194
1.26k
      if (code_no > ZERO_HCB && (code_no < NOISE_HCB)) {
195
893
        ret_val = ixheaacd_res_c_block_decode_huff_word_all_lb(
196
893
            it_bit_buf, code_no, len, aac_tables_ptr, p_spec_coeff_out, p_pul_arr);
197
198
893
        if (ret_val != 0)
199
24
          return (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
200
893
      } else {
201
376
        if (p_aac_decoder_channel_info->pulse_data.pulse_data_present)
202
76
          ixheaacd_res_inverse_quant_lb(
203
76
              p_spec_coeff_out, len,
204
76
              (WORD32 *)aac_tables_ptr->res_block_tables_ptr->pow_table_q17, p_pul_arr);
205
300
        else
206
300
          memset(p_spec_coeff_out, 0, sizeof(WORD32) * len);
207
376
      }
208
1.24k
      p_pul_arr += len;
209
1.24k
      p_spec_coeff_out += len;
210
1.24k
    }
211
212
1.35k
    index = 1024 - band_offsets[tot_bands];
213
1.35k
    memset(p_spec_coeff_out, 0, sizeof(WORD32) * index);
214
1.35k
  } else {
215
732
    memset(p_spectral_coefficient, 0, sizeof(WORD32) * 1024);
216
217
732
    groupoffset = 0;
218
219
2.51k
    for (group = 0; group < tot_groups; group++) {
220
1.80k
      WORD grp_win = p_ics_info->window_group_length[group];
221
1.80k
      p_codebook_tmp = &p_code_book[group * MAX_SFB_SHORT];
222
1.80k
      p_spec_coeff_out = &p_spectral_coefficient[groupoffset * maximum_bins_short];
223
224
3.58k
      for (band = 0; band < tot_bands;) {
225
1.80k
        WORD code_no = *p_codebook_tmp;
226
1.80k
        WORD start = band;
227
1.80k
        WORD ret_val;
228
229
9.32k
        for (; band < tot_bands && (*p_codebook_tmp == code_no); band++, p_codebook_tmp++)
230
7.52k
          ;
231
232
1.80k
        if (code_no > ZERO_HCB && (code_no < NOISE_HCB)) {
233
1.29k
          ret_val = ixheaacd_res_c_block_decode_huff_word_all(
234
1.29k
              it_bit_buf, code_no, p_spec_coeff_out, (WORD16 *)band_offsets, start, band, grp_win,
235
1.29k
              aac_tables_ptr, maximum_bins_short);
236
237
1.29k
          if (ret_val != 0)
238
28
            return (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
239
1.29k
        }
240
1.80k
      }
241
1.77k
      groupoffset = (groupoffset + grp_win);
242
1.77k
    }
243
732
  }
244
245
2.06k
  {
246
2.06k
    WORD8 *p_win_grp_len = &p_ics_info->window_group_length[0];
247
2.06k
    WORD8 *psfb_width = (WORD8 *)ixheaacd_res_get_sfb_width(p_ics_info, aac_tables_ptr);
248
2.06k
    WORD32 *scale_table_ptr;
249
2.06k
    if (120 == maximum_bins_short) {
250
0
      scale_table_ptr = aac_tables_ptr->res_block_tables_ptr->scale_table_960;
251
2.06k
    } else {
252
2.06k
      scale_table_ptr = aac_tables_ptr->res_block_tables_ptr->scale_table;
253
2.06k
    }
254
3.13k
    do {
255
3.13k
      groupwin = *p_win_grp_len++;
256
6.98k
      do {
257
6.98k
        ixheaacd_res_apply_scfs(&p_spectral_coefficient[0], &p_scale_factor[0], tot_bands,
258
6.98k
                                (WORD8 *)psfb_width, scale_table_ptr);
259
260
6.98k
        p_spectral_coefficient += maximum_bins_short;
261
6.98k
        groupwin--;
262
6.98k
      } while (groupwin != 0);
263
264
3.13k
      p_scale_factor += MAX_SFB_SHORT;
265
3.13k
      tot_groups--;
266
3.13k
    } while (tot_groups != 0);
267
2.06k
  }
268
269
2.06k
  return AAC_DEC_OK;
270
2.11k
}
271
272
WORD16
273
ixheaacd_res_c_tns_read(ia_bit_buf_struct *it_bit_buf,
274
2.12k
                        ia_mps_dec_residual_channel_info_struct *p_aac_decoder_channel_info) {
275
2.12k
  WORD window, window_per_frame;
276
2.12k
  WORD n_filt_bits, len_bits, order_bits;
277
2.12k
  WORD32 next_stop_band_tmp;
278
279
2.12k
  ia_mps_dec_residual_ics_info_struct *p_ics_info = &p_aac_decoder_channel_info->ics_info;
280
2.12k
  ia_mps_dec_residual_tns_data *p_tns_data = &p_aac_decoder_channel_info->tns_data;
281
282
2.12k
  if (!p_tns_data->tns_data_present) return AAC_DEC_OK;
283
284
379
  if (p_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
285
116
    n_filt_bits = 2;
286
116
    len_bits = 6;
287
116
    order_bits = 5;
288
116
    window_per_frame = 1;
289
263
  } else {
290
263
    n_filt_bits = 1;
291
263
    len_bits = 4;
292
263
    order_bits = 3;
293
263
    window_per_frame = 8;
294
263
  }
295
296
379
  next_stop_band_tmp = p_ics_info->total_sf_bands;
297
298
2.57k
  for (window = 0; window < window_per_frame; window++) {
299
2.20k
    WORD n_filt;
300
2.20k
    WORD length, coef_res;
301
2.20k
    p_tns_data->number_of_filters[window] = n_filt =
302
2.20k
        (WORD16)ixheaacd_read_bits_buf(it_bit_buf, n_filt_bits);
303
304
2.20k
    if (n_filt) {
305
866
      WORD32 accu;
306
866
      WORD index;
307
866
      WORD nextstopband;
308
309
866
      coef_res = ixheaacd_read_bits_buf(it_bit_buf, 1);
310
311
866
      nextstopband = next_stop_band_tmp;
312
1.77k
      for (index = 0; index < n_filt; index++) {
313
909
        WORD order;
314
909
        ia_mps_dec_residual_filter_struct *filter = &p_tns_data->filter[window][index];
315
909
        length = ixheaacd_read_bits_buf(it_bit_buf, len_bits);
316
317
909
        filter->start_band = nextstopband - length;
318
909
        filter->stop_band = nextstopband;
319
320
909
        nextstopband = filter->start_band;
321
322
909
        if (filter->start_band < 0) {
323
3
          filter->order = -1;
324
3
          return (WORD16)((WORD32)AAC_DEC_TNS_RANGE_ERROR);
325
3
        }
326
327
906
        filter->order = order = ixheaacd_read_bits_buf(it_bit_buf, order_bits);
328
906
        accu = order - MAX_ORDER_LONG;
329
330
906
        if (accu > 0) return (WORD16)((WORD32)AAC_DEC_TNS_ORDER_ERROR);
331
332
904
        if (order) {
333
710
          WORD i;
334
710
          WORD32 coef, coef_compress;
335
710
          WORD resolution, shift;
336
337
710
          filter->direction = (WORD8)(ixheaacd_read_bits_buf(it_bit_buf, 1) ? -1 : 1);
338
339
710
          coef_compress = ixheaacd_read_bits_buf(it_bit_buf, 1);
340
341
710
          filter->resolution = coef_res;
342
710
          resolution = coef_res + 3 - coef_compress;
343
710
          shift = 32 - resolution;
344
345
3.97k
          for (i = 0; i < order; i++) {
346
3.26k
            coef = ixheaacd_read_bits_buf(it_bit_buf, resolution);
347
3.26k
            coef = coef << shift;
348
3.26k
            filter->coeff[i] = (WORD8)(coef >> shift);
349
3.26k
          }
350
710
        }
351
904
      }
352
866
    }
353
2.20k
  }
354
374
  return AAC_DEC_OK;
355
379
}
356
357
WORD32 ixheaacd_res_inv_quant(WORD32 *px_quant, WORD32 *pow_table_q17)
358
359
377
{
360
377
  WORD32 q1;
361
377
  WORD32 temp;
362
377
  WORD32 q_abs;
363
377
  WORD16 interp;
364
377
  WORD32 shift;
365
366
377
  q_abs = *px_quant;
367
368
377
  if (q_abs > (8191 + 32)) return IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL;
369
370
248
  if (q_abs < 1024)
371
160
    shift = 3;
372
88
  else
373
88
    shift = 6;
374
375
248
  q1 = (q_abs) >> shift;
376
377
248
  interp = q_abs - (q1 << shift);
378
379
248
  temp = pow_table_q17[q1 + 1] - pow_table_q17[q1];
380
381
248
  temp = (WORD32)(temp * (WORD32)interp);
382
383
248
  temp = temp + (pow_table_q17[q1] << shift);
384
385
248
  if (shift == 3)
386
160
    temp = temp << 1;
387
88
  else
388
88
    temp = temp << 2;
389
390
248
  *px_quant = temp;
391
392
248
  return 0;
393
377
}