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