Coverage Report

Created: 2026-10-31 07:10

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
3.61k
UWORD32 ixheaacd_res_aac_showbits_32(UWORD8 *p_read_next) {
42
3.61k
  UWORD8 *v = p_read_next;
43
3.61k
  UWORD32 b = 0;
44
45
3.61k
  _SWAP(v, b);
46
3.61k
  return b;
47
3.61k
}
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
1.79k
{
55
1.79k
  FLAG gain_control_data_present;
56
1.79k
  WORD16 error_status = AAC_DEC_OK;
57
58
1.79k
  if (p_aac_decoder_channel_info->ics_info.window_sequence == EIGHT_SHORT_SEQUENCE) {
59
624
    memset(p_aac_decoder_channel_info->p_scale_factor, 0, MAX_WINDOWS * MAX_SFB_SHORT * 3);
60
624
  }
61
62
1.79k
  error_status = ixheaacd_c_block_read_section_data(it_bit_buf, p_aac_decoder_channel_info);
63
64
1.79k
  if (error_status) return error_status;
65
66
1.76k
  ixheaacd_res_c_block_read_scf_data(it_bit_buf, p_aac_decoder_channel_info, global_gain,
67
1.76k
                                     aac_tables_ptr);
68
69
1.76k
  error_status = ixheaacd_res_c_pulse_data_read(
70
1.76k
      it_bit_buf, &p_aac_decoder_channel_info->pulse_data, aac_tables_ptr);
71
1.76k
  if (error_status) return error_status;
72
73
1.75k
  p_aac_decoder_channel_info->tns_data.tns_data_present =
74
1.75k
      (FLAG)ixheaacd_read_bits_buf(it_bit_buf, 1);
75
76
1.75k
  error_status = ixheaacd_res_c_tns_read(it_bit_buf, p_aac_decoder_channel_info);
77
1.75k
  if (error_status) {
78
6
    return error_status;
79
6
  }
80
81
1.74k
  gain_control_data_present = ixheaacd_read_bits_buf(it_bit_buf, 1);
82
83
1.74k
  if (gain_control_data_present) {
84
7
    return (WORD16)((WORD32)AAC_DEC_UNIMPLEMENTED_GAIN_CONTROL_DATA);
85
7
  }
86
87
1.74k
  {
88
1.74k
    it_bit_buf->bit_pos = (7 - it_bit_buf->bit_pos);
89
90
1.74k
    error_status = ixheaacd_res_c_block_read_spec_data(it_bit_buf, p_aac_decoder_channel_info,
91
1.74k
                                                       aac_tables_ptr);
92
93
1.74k
    it_bit_buf->bit_pos = (7 - it_bit_buf->bit_pos);
94
1.74k
  }
95
96
1.74k
  return error_status;
97
1.74k
}
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
1.84k
    ia_mps_dec_residual_aac_tables_struct *aac_tables_ptr, WORD8 tot_sf_bands_ls[2]) {
103
1.84k
  WORD16 error_status = AAC_DEC_OK;
104
1.84k
  WORD32 ch;
105
106
3.58k
  for (ch = 0; ch < num_ch; ch++) {
107
1.84k
    ia_mps_dec_residual_channel_info_struct *p_aac_dec_ch_info = p_aac_decoder_channel_info[ch];
108
1.84k
    ia_mps_dec_residual_ics_info_struct *p_ics_info = &p_aac_dec_ch_info->ics_info;
109
110
1.84k
    p_aac_dec_ch_info->global_gain = (WORD16)ixheaacd_read_bits_buf(it_bit_buf, 8);
111
112
1.84k
    if (!p_aac_decoder_channel_info[LEFT]->common_window) {
113
1.82k
      error_status = ixheaacd_res_ics_read(it_bit_buf, p_ics_info, tot_sf_bands_ls);
114
1.82k
      if (error_status) {
115
23
        if (it_bit_buf->cnt_bits < 0)
116
0
          error_status = (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
117
23
        return error_status;
118
23
      }
119
1.82k
    }
120
121
1.82k
    error_status = ixheaacd_res_c_block_read(it_bit_buf, p_aac_dec_ch_info,
122
1.82k
                                             p_aac_dec_ch_info->global_gain, aac_tables_ptr);
123
1.82k
    if (error_status) {
124
87
      if (it_bit_buf->cnt_bits < 0)
125
1
        error_status = (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
126
127
87
      return error_status;
128
87
    }
129
1.82k
  }
130
131
1.73k
  return error_status;
132
1.84k
}
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.13k
                                     const WORD16 *p_scale_factor_band_offsets) {
137
1.13k
  WORD i, number_pulse;
138
1.13k
  WORD32 k;
139
140
1.13k
  memset(p_pulse_arr, 0, sizeof(WORD32) * 256);
141
142
1.13k
  if (pulse_data->pulse_data_present) {
143
101
    k = p_scale_factor_band_offsets[pulse_data->pulse_start_band];
144
101
    number_pulse = pulse_data->number_pulse;
145
146
324
    for (i = 0; i <= number_pulse; i++) {
147
223
      k = add_d(k, pulse_data->pulse_offset[i]);
148
223
      p_pulse_arr[k] = pulse_data->pulse_amp[i];
149
223
    }
150
101
  }
151
1.13k
}
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
1.72k
    ia_mps_dec_residual_aac_tables_struct *aac_tables_ptr) {
157
1.72k
  WORD band, tot_bands, tot_groups = 0;
158
1.72k
  WORD group, groupwin, groupoffset;
159
160
1.72k
  WORD index;
161
1.72k
  WORD8 *p_code_book, *p_codebook_tmp;
162
1.72k
  WORD16 *p_scale_factor;
163
1.72k
  WORD32 *p_spectral_coefficient;
164
1.72k
  ia_mps_dec_residual_ics_info_struct *p_ics_info = &p_aac_decoder_channel_info->ics_info;
165
1.72k
  WORD16 *band_offsets;
166
1.72k
  WORD32 maximum_bins_short = ixheaac_shr16_dir_sat(p_ics_info->frame_length, 3);
167
168
1.72k
  WORD32 *p_spec_coeff_out;
169
170
1.72k
  p_code_book = p_aac_decoder_channel_info->p_code_book;
171
1.72k
  p_scale_factor = p_aac_decoder_channel_info->p_scale_factor;
172
1.72k
  p_spectral_coefficient = p_aac_decoder_channel_info->p_spectral_coefficient;
173
1.72k
  tot_groups = p_ics_info->window_groups;
174
1.72k
  tot_bands = p_ics_info->max_sf_bands;
175
1.72k
  band_offsets = (WORD16 *)ixheaacd_res_get_sfb_offsets(p_ics_info, aac_tables_ptr);
176
177
1.72k
  if (p_aac_decoder_channel_info->ics_info.window_sequence != EIGHT_SHORT_SEQUENCE) {
178
1.13k
    WORD8 *p_pul_arr = (WORD8 *)p_aac_decoder_channel_info->p_tns_scratch;
179
1.13k
    ixheaacd_res_c_pulse_data_apply(&p_aac_decoder_channel_info->pulse_data, p_pul_arr,
180
1.13k
                                    band_offsets);
181
182
1.13k
    p_spec_coeff_out = &p_spectral_coefficient[0];
183
2.32k
    for (band = 0; band < tot_bands;) {
184
1.21k
      WORD ret_val;
185
1.21k
      WORD32 len;
186
1.21k
      WORD32 code_no = p_code_book[band];
187
1.21k
      WORD start = band;
188
189
12.0k
      for (; band < tot_bands && (p_code_book[band] == code_no); band++)
190
10.8k
        ;
191
192
1.21k
      len = band_offsets[band] - band_offsets[start];
193
194
1.21k
      if (code_no > ZERO_HCB && (code_no < NOISE_HCB)) {
195
872
        ret_val = ixheaacd_res_c_block_decode_huff_word_all_lb(
196
872
            it_bit_buf, code_no, len, aac_tables_ptr, p_spec_coeff_out, p_pul_arr);
197
198
872
        if (ret_val != 0)
199
21
          return (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
200
872
      } else {
201
343
        if (p_aac_decoder_channel_info->pulse_data.pulse_data_present)
202
67
          ixheaacd_res_inverse_quant_lb(
203
67
              p_spec_coeff_out, len,
204
67
              (WORD32 *)aac_tables_ptr->res_block_tables_ptr->pow_table_q17, p_pul_arr);
205
276
        else
206
276
          memset(p_spec_coeff_out, 0, sizeof(WORD32) * len);
207
343
      }
208
1.19k
      p_pul_arr += len;
209
1.19k
      p_spec_coeff_out += len;
210
1.19k
    }
211
212
1.11k
    index = 1024 - band_offsets[tot_bands];
213
1.11k
    memset(p_spec_coeff_out, 0, sizeof(WORD32) * index);
214
1.11k
  } else {
215
595
    memset(p_spectral_coefficient, 0, sizeof(WORD32) * 1024);
216
217
595
    groupoffset = 0;
218
219
2.09k
    for (group = 0; group < tot_groups; group++) {
220
1.52k
      WORD grp_win = p_ics_info->window_group_length[group];
221
1.52k
      p_codebook_tmp = &p_code_book[group * MAX_SFB_SHORT];
222
1.52k
      p_spec_coeff_out = &p_spectral_coefficient[groupoffset * maximum_bins_short];
223
224
2.94k
      for (band = 0; band < tot_bands;) {
225
1.43k
        WORD code_no = *p_codebook_tmp;
226
1.43k
        WORD start = band;
227
1.43k
        WORD ret_val;
228
229
7.61k
        for (; band < tot_bands && (*p_codebook_tmp == code_no); band++, p_codebook_tmp++)
230
6.17k
          ;
231
232
1.43k
        if (code_no > ZERO_HCB && (code_no < NOISE_HCB)) {
233
1.05k
          ret_val = ixheaacd_res_c_block_decode_huff_word_all(
234
1.05k
              it_bit_buf, code_no, p_spec_coeff_out, (WORD16 *)band_offsets, start, band, grp_win,
235
1.05k
              aac_tables_ptr, maximum_bins_short);
236
237
1.05k
          if (ret_val != 0)
238
16
            return (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
239
1.05k
        }
240
1.43k
      }
241
1.50k
      groupoffset = (groupoffset + grp_win);
242
1.50k
    }
243
595
  }
244
245
1.69k
  {
246
1.69k
    WORD8 *p_win_grp_len = &p_ics_info->window_group_length[0];
247
1.69k
    WORD8 *psfb_width = (WORD8 *)ixheaacd_res_get_sfb_width(p_ics_info, aac_tables_ptr);
248
1.69k
    WORD32 *scale_table_ptr;
249
1.69k
    if (120 == maximum_bins_short) {
250
0
      scale_table_ptr = aac_tables_ptr->res_block_tables_ptr->scale_table_960;
251
1.69k
    } else {
252
1.69k
      scale_table_ptr = aac_tables_ptr->res_block_tables_ptr->scale_table;
253
1.69k
    }
254
2.61k
    do {
255
2.61k
      groupwin = *p_win_grp_len++;
256
5.74k
      do {
257
5.74k
        ixheaacd_res_apply_scfs(&p_spectral_coefficient[0], &p_scale_factor[0], tot_bands,
258
5.74k
                                (WORD8 *)psfb_width, scale_table_ptr);
259
260
5.74k
        p_spectral_coefficient += maximum_bins_short;
261
5.74k
        groupwin--;
262
5.74k
      } while (groupwin != 0);
263
264
2.61k
      p_scale_factor += MAX_SFB_SHORT;
265
2.61k
      tot_groups--;
266
2.61k
    } while (tot_groups != 0);
267
1.69k
  }
268
269
1.69k
  return AAC_DEC_OK;
270
1.72k
}
271
272
WORD16
273
ixheaacd_res_c_tns_read(ia_bit_buf_struct *it_bit_buf,
274
1.74k
                        ia_mps_dec_residual_channel_info_struct *p_aac_decoder_channel_info) {
275
1.74k
  WORD window, window_per_frame;
276
1.74k
  WORD n_filt_bits, len_bits, order_bits;
277
1.74k
  WORD32 next_stop_band_tmp;
278
279
1.74k
  ia_mps_dec_residual_ics_info_struct *p_ics_info = &p_aac_decoder_channel_info->ics_info;
280
1.74k
  ia_mps_dec_residual_tns_data *p_tns_data = &p_aac_decoder_channel_info->tns_data;
281
282
1.74k
  if (!p_tns_data->tns_data_present) return AAC_DEC_OK;
283
284
337
  if (p_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
285
114
    n_filt_bits = 2;
286
114
    len_bits = 6;
287
114
    order_bits = 5;
288
114
    window_per_frame = 1;
289
223
  } else {
290
223
    n_filt_bits = 1;
291
223
    len_bits = 4;
292
223
    order_bits = 3;
293
223
    window_per_frame = 8;
294
223
  }
295
296
337
  next_stop_band_tmp = p_ics_info->total_sf_bands;
297
298
2.21k
  for (window = 0; window < window_per_frame; window++) {
299
1.88k
    WORD n_filt;
300
1.88k
    WORD length, coef_res;
301
1.88k
    p_tns_data->number_of_filters[window] = n_filt =
302
1.88k
        (WORD16)ixheaacd_read_bits_buf(it_bit_buf, n_filt_bits);
303
304
1.88k
    if (n_filt) {
305
738
      WORD32 accu;
306
738
      WORD index;
307
738
      WORD nextstopband;
308
309
738
      coef_res = ixheaacd_read_bits_buf(it_bit_buf, 1);
310
311
738
      nextstopband = next_stop_band_tmp;
312
1.50k
      for (index = 0; index < n_filt; index++) {
313
776
        WORD order;
314
776
        ia_mps_dec_residual_filter_struct *filter = &p_tns_data->filter[window][index];
315
776
        length = ixheaacd_read_bits_buf(it_bit_buf, len_bits);
316
317
776
        filter->start_band = nextstopband - length;
318
776
        filter->stop_band = nextstopband;
319
320
776
        nextstopband = filter->start_band;
321
322
776
        if (filter->start_band < 0) {
323
4
          filter->order = -1;
324
4
          return (WORD16)((WORD32)AAC_DEC_TNS_RANGE_ERROR);
325
4
        }
326
327
772
        filter->order = order = ixheaacd_read_bits_buf(it_bit_buf, order_bits);
328
772
        accu = order - MAX_ORDER_LONG;
329
330
772
        if (accu > 0) return (WORD16)((WORD32)AAC_DEC_TNS_ORDER_ERROR);
331
332
770
        if (order) {
333
614
          WORD i;
334
614
          WORD32 coef, coef_compress;
335
614
          WORD resolution, shift;
336
337
614
          filter->direction = (WORD8)(ixheaacd_read_bits_buf(it_bit_buf, 1) ? -1 : 1);
338
339
614
          coef_compress = ixheaacd_read_bits_buf(it_bit_buf, 1);
340
341
614
          filter->resolution = coef_res;
342
614
          resolution = coef_res + 3 - coef_compress;
343
614
          shift = 32 - resolution;
344
345
3.46k
          for (i = 0; i < order; i++) {
346
2.85k
            coef = ixheaacd_read_bits_buf(it_bit_buf, resolution);
347
2.85k
            coef = coef << shift;
348
2.85k
            filter->coeff[i] = (WORD8)(coef >> shift);
349
2.85k
          }
350
614
        }
351
770
      }
352
738
    }
353
1.88k
  }
354
331
  return AAC_DEC_OK;
355
337
}
356
357
WORD32 ixheaacd_res_inv_quant(WORD32 *px_quant, WORD32 *pow_table_q17)
358
359
300
{
360
300
  WORD32 q1;
361
300
  WORD32 temp;
362
300
  WORD32 q_abs;
363
300
  WORD16 interp;
364
300
  WORD32 shift;
365
366
300
  q_abs = *px_quant;
367
368
300
  if (q_abs > (8191 + 32)) return IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL;
369
370
211
  if (q_abs < 1024)
371
153
    shift = 3;
372
58
  else
373
58
    shift = 6;
374
375
211
  q1 = (q_abs) >> shift;
376
377
211
  interp = q_abs - (q1 << shift);
378
379
211
  temp = pow_table_q17[q1 + 1] - pow_table_q17[q1];
380
381
211
  temp = (WORD32)(temp * (WORD32)interp);
382
383
211
  temp = temp + (pow_table_q17[q1] << shift);
384
385
211
  if (shift == 3)
386
153
    temp = temp << 1;
387
58
  else
388
58
    temp = temp << 2;
389
390
211
  *px_quant = temp;
391
392
211
  return 0;
393
300
}