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