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