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