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 "ixheaacd_sbr_common.h"
21 #include <ixheaacd_type_def.h>
22 #include "ixheaacd_constants.h"
23 #include <ixheaacd_basic_ops32.h>
24 #include <ixheaacd_basic_ops16.h>
25 #include <ixheaacd_basic_ops40.h>
26 #include "ixheaacd_basic_ops.h"
27
28 #include "ixheaacd_defines.h"
29 #include <ixheaacd_aac_rom.h>
30 #include "ixheaacd_aac_imdct.h"
31 #include "ixheaacd_bitbuffer.h"
32 #include <ixheaacd_basic_op.h>
33 #include "ixheaacd_intrinsics.h"
34
35 #include "ixheaacd_pulsedata.h"
36
37 #include "ixheaacd_pns.h"
38 #include "ixheaacd_common_rom.h"
39 #include "ixheaacd_drc_data_struct.h"
40
41 #include "ixheaacd_lt_predict.h"
42
43 #include "ixheaacd_channelinfo.h"
44 #include "ixheaacd_cnst.h"
45 #include "ixheaacd_drc_dec.h"
46
47 #include "ixheaacd_block.h"
48 #include "ixheaacd_channel.h"
49
50 #include "ixheaacd_pulsedata.h"
51 #include "ixheaacd_pns.h"
52 #include "ixheaacd_sbrdecoder.h"
53 #include "ixheaacd_error_codes.h"
54 #include "ixheaacd_audioobjtypes.h"
55 #include "ixheaacd_latmdemux.h"
56 #include "ixheaacd_aacdec.h"
57
58 #define LONG_BLOCK_SECT_LEN 5
59 #define SHORT_BLOCK_SECT_LEN 3
60
ixheaacd_read_section_data(ia_bit_buf_struct * it_bit_buff,ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,WORD32 aac_spect_data_resil_flag,WORD32 aac_sect_data_resil_flag,ia_aac_dec_tables_struct * ptr_aac_tables)61 WORD16 ixheaacd_read_section_data(
62 ia_bit_buf_struct *it_bit_buff,
63 ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
64 WORD32 aac_spect_data_resil_flag, WORD32 aac_sect_data_resil_flag,
65 ia_aac_dec_tables_struct *ptr_aac_tables) {
66 WORD sfb;
67 WORD sect_cb;
68 WORD sect_len;
69 WORD sect_len_incr;
70 WORD sect_esc_val;
71 ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
72 WORD max_sfb = ptr_ics_info->max_sfb;
73 WORD num_win_group;
74
75 WORD8 *ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
76 WORD8 *ptr_code_book_temp = ptr_code_book;
77 WORD32 sect_bitlen = LONG_BLOCK_SECT_LEN;
78 int num_lines_sec_idx = 0, top;
79 short *ptr_num_sect_lines =
80 ptr_aac_dec_channel_info->num_line_in_sec4_hcr_arr;
81 UWORD8 *ptr_hcr_code_book = ptr_aac_dec_channel_info->cb4_hcr_arr;
82 const short *band_offsets = (WORD16 *)ixheaacd_getscalefactorbandoffsets(
83 &(ptr_aac_dec_channel_info->str_ics_info), ptr_aac_tables);
84 ptr_aac_dec_channel_info->number_sect = 0;
85
86 if (ptr_aac_dec_channel_info->str_ics_info.window_sequence ==
87 EIGHT_SHORT_SEQUENCE) {
88 sect_bitlen = SHORT_BLOCK_SECT_LEN;
89 }
90
91 sect_esc_val = (1 << sect_bitlen) - 1;
92
93 for (num_win_group = 0; num_win_group < ptr_ics_info->num_window_groups;
94 num_win_group++) {
95 sfb = 0;
96
97 while (sfb < max_sfb) {
98 sect_len = 0;
99 if (aac_sect_data_resil_flag) {
100 sect_cb = ixheaacd_read_bits_buf(it_bit_buff, 5);
101 } else {
102 sect_cb = ixheaacd_read_bits_buf(it_bit_buff, 4);
103 }
104
105 if ((aac_sect_data_resil_flag == 0) ||
106 ((sect_cb < 11) || ((sect_cb > 11) && (sect_cb < 16)))) {
107 sect_len_incr = ixheaacd_read_bits_buf(it_bit_buff, sect_bitlen);
108 while (sect_len_incr == sect_esc_val) {
109 sect_len = (sect_len + sect_esc_val);
110 sect_len_incr = ixheaacd_read_bits_buf(it_bit_buff, sect_bitlen);
111 }
112 } else
113 sect_len_incr = 1;
114
115 sect_len = (sect_len + sect_len_incr);
116
117 if (aac_spect_data_resil_flag) {
118 top = (sfb + sect_len);
119 if ((num_lines_sec_idx >= MAX_SFB_HCR) ||
120 (top >= MAX_SCALE_FACTOR_BANDS_LONG)) {
121 return -1;
122 }
123 ptr_num_sect_lines[num_lines_sec_idx] =
124 band_offsets[top] - band_offsets[sfb];
125 num_lines_sec_idx++;
126 if (sect_cb == (ESC_HCB + 1)) {
127 return IA_ENHAACPLUS_DEC_EXE_NONFATAL_INVALID_CODE_BOOK;
128 } else {
129 *ptr_hcr_code_book++ = sect_cb;
130 }
131 ptr_aac_dec_channel_info->number_sect++;
132 }
133
134 sfb = (sfb + sect_len);
135 if (sfb > max_sfb) {
136 return (WORD16)(
137 (WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_EXCEEDS_SFB_TRANSMITTED);
138 }
139 if (sect_cb == (ESC_HCB + 1)) {
140 return (WORD16)(
141 (WORD32)IA_ENHAACPLUS_DEC_EXE_NONFATAL_INVALID_CODE_BOOK);
142 }
143
144 while (sect_len--) {
145 *ptr_code_book_temp++ = sect_cb;
146 }
147 }
148 ptr_code_book += MAX_SCALE_FACTOR_BANDS_SHORT;
149 ptr_code_book_temp = ptr_code_book;
150 }
151
152 return AAC_DEC_OK;
153 }
154
ixheaacd_read_scale_factor_data(ia_bit_buf_struct * it_bit_buff,ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,ia_aac_dec_tables_struct * ptr_aac_tables,WORD32 object_type)155 VOID ixheaacd_read_scale_factor_data(
156 ia_bit_buf_struct *it_bit_buff,
157 ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
158 ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 object_type) {
159 WORD sfb;
160 WORD16 position = 0;
161 WORD num_win_group;
162 WORD16 factor = ptr_aac_dec_channel_info->global_gain;
163 WORD8 *ptr_code_book, *ptr_code_book_short;
164 WORD16 *ptr_scale_fact, *ptr_scale_fact_short;
165 WORD16 norm_value;
166 WORD16 index, length;
167 const UWORD16 *hcod_sf =
168 ptr_aac_tables->pstr_huffmann_tables->huffman_code_book_scl;
169 const UWORD32 *table_idx =
170 ptr_aac_tables->pstr_huffmann_tables->huffman_code_book_scl_index;
171
172 WORD start_bit_pos = it_bit_buff->bit_pos;
173 UWORD8 *start_read_pos = it_bit_buff->ptr_read_next;
174 UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
175 WORD32 bit_pos = 7 - it_bit_buff->bit_pos;
176 WORD32 read_word;
177 WORD32 increment;
178 read_word = ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->cnt_bits,
179 &increment);
180 ptr_read_next += increment;
181
182 ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
183
184 ptr_scale_fact = ptr_aac_dec_channel_info->ptr_scale_factor;
185
186 for (num_win_group = 0;
187 num_win_group < ptr_aac_dec_channel_info->str_ics_info.num_window_groups;
188 num_win_group++) {
189 ptr_code_book_short =
190 &ptr_code_book[num_win_group * MAX_SCALE_FACTOR_BANDS_SHORT];
191 ptr_scale_fact_short =
192 &ptr_scale_fact[num_win_group * MAX_SCALE_FACTOR_BANDS_SHORT];
193 for (sfb = ptr_aac_dec_channel_info->str_ics_info.max_sfb - 1; sfb >= 0;
194 sfb--) {
195 WORD32 sfb_cb = *ptr_code_book_short++;
196 if (sfb_cb == ZERO_HCB)
197 *ptr_scale_fact_short++ = 0;
198 else {
199 {
200 WORD32 pns_present = 0;
201 WORD pns_band;
202
203 ia_pns_info_struct *ptr_pns_info =
204 &ptr_aac_dec_channel_info->str_pns_info;
205
206 if (sfb_cb == NOISE_HCB && (ptr_pns_info->pns_active != 1)) {
207 pns_present = 1;
208 }
209
210 if (!pns_present) {
211 UWORD32 read_word1;
212
213 read_word1 = read_word << bit_pos;
214
215 ixheaacd_huffman_decode(read_word1, &index, &length, hcod_sf,
216 table_idx);
217
218 bit_pos += length;
219 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
220 it_bit_buff->ptr_bit_buf_end);
221
222 ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
223 it_bit_buff->ptr_bit_buf_end);
224
225 norm_value = index - 60;
226 }
227
228 else {
229 WORD32 noise_start_value;
230 UWORD32 temp;
231
232 temp = (read_word << bit_pos);
233 temp = ((UWORD32)temp >> (32 - 9));
234 noise_start_value = temp;
235 bit_pos += 9;
236
237 ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
238 it_bit_buff->ptr_bit_buf_end);
239
240 norm_value = noise_start_value - 256;
241 ptr_pns_info->pns_active = 1;
242
243 ptr_pns_info->noise_energy =
244 ptr_aac_dec_channel_info->global_gain - NOISE_OFFSET;
245 }
246
247 if ((object_type != AOT_ER_AAC_ELD) &&
248 (object_type != AOT_ER_AAC_LD)) {
249 if (sfb_cb > NOISE_HCB) {
250 position = position + norm_value;
251 *ptr_scale_fact_short++ = -position;
252 } else if (sfb_cb < NOISE_HCB) {
253 factor = factor + norm_value;
254 *ptr_scale_fact_short++ = factor;
255 } else {
256 ptr_pns_info->noise_energy =
257 ixheaacd_add16_sat(ptr_pns_info->noise_energy, norm_value);
258
259 pns_band = (num_win_group << 4) +
260 ptr_aac_dec_channel_info->str_ics_info.max_sfb - sfb -
261 1;
262 ptr_aac_dec_channel_info->ptr_scale_factor[pns_band] =
263 ptr_pns_info->noise_energy;
264
265 ptr_pns_info->pns_used[pns_band] = 1;
266 ptr_scale_fact_short++;
267 }
268 } else {
269 if ((sfb_cb == INTENSITY_HCB) || (sfb_cb == INTENSITY_HCB2)) {
270 position = position + norm_value;
271 *ptr_scale_fact_short++ = -position;
272 } else if (sfb_cb == NOISE_HCB) {
273 ptr_pns_info->noise_energy =
274 ixheaacd_add16_sat(ptr_pns_info->noise_energy, norm_value);
275
276 pns_band = (num_win_group << 4) +
277 ptr_aac_dec_channel_info->str_ics_info.max_sfb - sfb -
278 1;
279 ptr_aac_dec_channel_info->ptr_scale_factor[pns_band] =
280 ptr_pns_info->noise_energy;
281
282 ptr_pns_info->pns_used[pns_band] = 1;
283 ptr_scale_fact_short++;
284
285 } else {
286 factor = factor + norm_value;
287 *ptr_scale_fact_short++ = factor;
288 }
289 }
290 }
291 }
292 }
293 }
294
295 it_bit_buff->ptr_read_next = ptr_read_next - increment;
296
297 it_bit_buff->bit_pos = 7 - bit_pos;
298 {
299 WORD bits_consumed;
300 bits_consumed = ((it_bit_buff->ptr_read_next - start_read_pos) << 3) +
301 (start_bit_pos - it_bit_buff->bit_pos);
302 it_bit_buff->cnt_bits -= bits_consumed;
303 }
304 }
305