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