/src/libxaac/decoder/ixheaacd_bitbuffer.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 | | |
21 | | #include <string.h> |
22 | | #include <assert.h> |
23 | | #include "ixheaacd_sbr_common.h" |
24 | | #include "ixheaac_type_def.h" |
25 | | #include "ixheaac_constants.h" |
26 | | #include "ixheaac_basic_ops32.h" |
27 | | #include "ixheaac_basic_ops16.h" |
28 | | #include "ixheaac_basic_ops40.h" |
29 | | #include "ixheaac_basic_ops.h" |
30 | | |
31 | | #include "ixheaac_basic_op.h" |
32 | | #include "ixheaacd_intrinsics.h" |
33 | | #include "ixheaacd_bitbuffer.h" |
34 | | |
35 | | #include "ixheaacd_adts_crc_check.h" |
36 | | #include "ixheaacd_error_codes.h" |
37 | | |
38 | | VOID ixheaacd_byte_align(ia_bit_buf_struct *it_bit_buff, |
39 | 136k | WORD32 *align_bits_cnt) { |
40 | 136k | WORD alignment; |
41 | 136k | alignment = (WORD)((*align_bits_cnt - it_bit_buff->cnt_bits) & 0x07); |
42 | | |
43 | 136k | if (alignment) { |
44 | 51.4k | ixheaacd_read_bits_buf(it_bit_buff, (8 - alignment)); |
45 | 51.4k | } |
46 | | |
47 | 136k | *align_bits_cnt = it_bit_buff->cnt_bits; |
48 | 136k | } |
49 | | |
50 | 5.03G | WORD32 ixheaacd_skip_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) { |
51 | 5.03G | UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next; |
52 | 5.03G | WORD bit_pos = it_bit_buff->bit_pos; |
53 | | |
54 | 5.03G | if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0) |
55 | 127 | longjmp(*(it_bit_buff->xaac_jmp_buf), |
56 | 127 | IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES); |
57 | 5.03G | it_bit_buff->cnt_bits -= no_of_bits; |
58 | | |
59 | 5.03G | ptr_read_next += no_of_bits / 8; |
60 | 5.03G | bit_pos -= (no_of_bits % 8); |
61 | 5.03G | if (bit_pos < 0) { |
62 | 629M | bit_pos += 8; |
63 | 629M | ptr_read_next++; |
64 | 629M | } |
65 | 5.03G | assert(bit_pos >= 0 && bit_pos <= 7); |
66 | | |
67 | 5.03G | it_bit_buff->ptr_read_next = ptr_read_next; |
68 | 5.03G | it_bit_buff->bit_pos = (WORD16)bit_pos; |
69 | 5.03G | return no_of_bits; |
70 | 5.03G | } |
71 | | |
72 | 4.99M | WORD32 ixheaacd_show_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) { |
73 | 4.99M | UWORD32 ret_val; |
74 | 4.99M | UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next; |
75 | 4.99M | WORD bit_pos = it_bit_buff->bit_pos; |
76 | | |
77 | 4.99M | if (no_of_bits == 0) { |
78 | 126 | return 0; |
79 | 126 | } |
80 | | |
81 | 4.99M | if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0 || |
82 | 4.99M | no_of_bits > 25) { |
83 | 3 | longjmp(*(it_bit_buff->xaac_jmp_buf), |
84 | 3 | IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES); |
85 | 3 | } |
86 | | |
87 | 4.99M | ret_val = (UWORD32)*ptr_read_next; |
88 | | |
89 | 4.99M | bit_pos -= no_of_bits; |
90 | 17.1M | while (bit_pos < -1) { |
91 | 12.1M | bit_pos += 8; |
92 | 12.1M | ptr_read_next++; |
93 | | |
94 | 12.1M | ret_val <<= 8; |
95 | | |
96 | 12.1M | ret_val |= (UWORD32)*ptr_read_next; |
97 | 12.1M | } |
98 | | |
99 | 4.99M | if (bit_pos == -1) { |
100 | 617k | bit_pos += 8; |
101 | 617k | ret_val <<= 8; |
102 | 617k | ptr_read_next++; |
103 | 617k | } |
104 | | |
105 | 4.99M | ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits); |
106 | | |
107 | 4.99M | return ret_val; |
108 | 4.99M | } |
109 | | |
110 | 389M | WORD32 ixheaacd_read_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) { |
111 | 389M | UWORD32 ret_val; |
112 | 389M | UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next; |
113 | 389M | WORD bit_pos = it_bit_buff->bit_pos; |
114 | | |
115 | 389M | if (no_of_bits == 0) { |
116 | 28.6M | return 0; |
117 | 28.6M | } |
118 | | |
119 | 360M | if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0 || |
120 | 360M | no_of_bits > 25) { |
121 | 3.30k | longjmp(*(it_bit_buff->xaac_jmp_buf), |
122 | 3.30k | IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES); |
123 | 3.30k | } |
124 | | |
125 | 360M | it_bit_buff->cnt_bits -= no_of_bits; |
126 | 360M | ret_val = (UWORD32)*ptr_read_next; |
127 | | |
128 | 360M | bit_pos -= no_of_bits; |
129 | 417M | while (bit_pos < -1) { |
130 | 56.6M | bit_pos += 8; |
131 | 56.6M | ptr_read_next++; |
132 | | |
133 | 56.6M | ret_val <<= 8; |
134 | | |
135 | 56.6M | ret_val |= (UWORD32)*ptr_read_next; |
136 | 56.6M | } |
137 | | |
138 | 360M | if (bit_pos == -1) { |
139 | 50.6M | bit_pos += 8; |
140 | 50.6M | ret_val <<= 8; |
141 | 50.6M | ptr_read_next++; |
142 | 50.6M | } |
143 | | |
144 | 360M | ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits); |
145 | 360M | it_bit_buff->ptr_read_next = ptr_read_next; |
146 | 360M | it_bit_buff->bit_pos = (WORD16)bit_pos; |
147 | 360M | return ret_val; |
148 | 360M | } |
149 | | |
150 | | VOID ixheaacd_aac_read_byte(UWORD8 **ptr_read_next, WORD32 *bit_pos, |
151 | 2.63k | WORD32 *readword) { |
152 | 2.63k | UWORD8 *v = *ptr_read_next; |
153 | 2.63k | WORD32 bits_consumed = *bit_pos; |
154 | | |
155 | 2.63k | if ((bits_consumed -= 8) >= 0) { |
156 | 631 | *readword = (*readword << 8) | *v; |
157 | 631 | v++; |
158 | 2.00k | } else { |
159 | 2.00k | bits_consumed += 8; |
160 | 2.00k | } |
161 | 2.63k | *bit_pos = bits_consumed; |
162 | 2.63k | *ptr_read_next = v; |
163 | 2.63k | return; |
164 | 2.63k | } |
165 | | |
166 | | VOID ixheaacd_aac_read_byte_corr1(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos, |
167 | 8.16M | WORD32 *readword, UWORD8 *p_bit_buf_end) { |
168 | 8.16M | UWORD8 *v = *ptr_read_next; |
169 | 8.16M | WORD32 bits_consumed = *ptr_bit_pos; |
170 | 8.16M | WORD32 temp_bit_count = 0; |
171 | | |
172 | 11.2M | while (bits_consumed >= 8) { |
173 | 3.04M | bits_consumed -= 8; |
174 | 3.04M | if ((p_bit_buf_end < v) && (p_bit_buf_end != 0)) |
175 | 39.8k | temp_bit_count += 8; |
176 | 3.00M | else { |
177 | 3.00M | *readword = (*readword << 8) | *v; |
178 | 3.00M | v++; |
179 | 3.00M | } |
180 | 3.04M | } |
181 | | |
182 | 8.16M | if (bits_consumed > (31 - temp_bit_count)) { |
183 | 5.34k | if ((p_bit_buf_end != NULL) && (p_bit_buf_end < v)) { |
184 | 5.34k | bits_consumed = 31 - temp_bit_count; |
185 | 5.34k | } |
186 | 5.34k | } |
187 | | |
188 | 8.16M | *ptr_bit_pos = bits_consumed + temp_bit_count; |
189 | 8.16M | *ptr_read_next = v; |
190 | 8.16M | return; |
191 | 8.16M | } |
192 | | |
193 | | VOID ixheaacd_aac_read_byte_corr(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos, |
194 | 26.7M | WORD32 *readword, UWORD8 *p_bit_buf_end) { |
195 | 26.7M | UWORD8 *v = *ptr_read_next; |
196 | 26.7M | WORD32 bits_consumed = *ptr_bit_pos; |
197 | | |
198 | 26.7M | if ((bits_consumed -= 8) >= 0) { |
199 | 7.19M | if (p_bit_buf_end < v) |
200 | 264k | bits_consumed += 8; |
201 | 6.92M | else { |
202 | 6.92M | *readword = (*readword << 8) | *v; |
203 | 6.92M | v++; |
204 | 6.92M | } |
205 | 19.5M | } else { |
206 | 19.5M | bits_consumed += 8; |
207 | 19.5M | } |
208 | | |
209 | 26.7M | if (bits_consumed > 31) { |
210 | 166k | if (p_bit_buf_end < v) { |
211 | 166k | bits_consumed = 31; |
212 | 166k | } |
213 | 166k | } |
214 | | |
215 | 26.7M | *ptr_bit_pos = bits_consumed; |
216 | 26.7M | *ptr_read_next = v; |
217 | 26.7M | return; |
218 | 26.7M | } |
219 | | |
220 | 12.4M | WORD32 ixheaacd_aac_read_bit(ia_bit_buf_struct *it_bit_buff) { |
221 | 12.4M | UWORD8 ret_val; |
222 | 12.4M | UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next; |
223 | 12.4M | WORD bit_pos = it_bit_buff->bit_pos; |
224 | 12.4M | UWORD32 temp; |
225 | 12.4M | WORD no_of_bits = 1; |
226 | | |
227 | 12.4M | if (bit_pos < 0) { |
228 | 1.29M | bit_pos = 7; |
229 | 1.29M | ptr_read_next--; |
230 | 1.29M | } |
231 | | |
232 | 12.4M | if (ptr_read_next < it_bit_buff->ptr_bit_buf_base) { |
233 | 148 | longjmp(*(it_bit_buff->xaac_jmp_buf), |
234 | 148 | IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES); |
235 | 148 | } |
236 | | |
237 | 12.4M | it_bit_buff->cnt_bits += no_of_bits; |
238 | 12.4M | ret_val = *ptr_read_next; |
239 | 12.4M | bit_pos -= no_of_bits; |
240 | | |
241 | 12.4M | temp = (ret_val << 24) << (bit_pos + no_of_bits); |
242 | 12.4M | it_bit_buff->ptr_read_next = ptr_read_next; |
243 | 12.4M | it_bit_buff->bit_pos = (WORD16)bit_pos; |
244 | | |
245 | 12.4M | return temp >> (32 - no_of_bits); |
246 | 12.4M | } |
247 | | |
248 | 9.74M | WORD32 ixheaacd_aac_read_bit_rev(ia_bit_buf_struct *it_bit_buff) { |
249 | 9.74M | UWORD8 ret_val; |
250 | 9.74M | UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next; |
251 | 9.74M | WORD bit_pos = it_bit_buff->bit_pos; |
252 | 9.74M | UWORD32 temp; |
253 | 9.74M | WORD no_of_bits = 1; |
254 | | |
255 | 9.74M | if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0) { |
256 | 56 | longjmp(*(it_bit_buff->xaac_jmp_buf), |
257 | 56 | IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES); |
258 | 56 | } |
259 | | |
260 | 9.74M | if (bit_pos >= 8) { |
261 | 551k | bit_pos -= 8; |
262 | 551k | ptr_read_next++; |
263 | 551k | } |
264 | | |
265 | 9.74M | it_bit_buff->cnt_bits -= no_of_bits; |
266 | 9.74M | ret_val = *ptr_read_next; |
267 | 9.74M | bit_pos += no_of_bits; |
268 | | |
269 | 9.74M | temp = (ret_val << 24) << (bit_pos - no_of_bits); |
270 | 9.74M | it_bit_buff->ptr_read_next = ptr_read_next; |
271 | 9.74M | it_bit_buff->bit_pos = (WORD16)bit_pos; |
272 | | |
273 | 9.74M | return temp >> (32 - no_of_bits); |
274 | 9.74M | } |
275 | | |
276 | | VOID ixheaacd_write_bit(ia_bit_buf_struct *it_bit_buff, WORD32 value, |
277 | | WORD32 no_of_bits) |
278 | | |
279 | 17.7M | { |
280 | 17.7M | WORD32 mask; |
281 | | |
282 | 17.7M | if (no_of_bits == 0) return; |
283 | | |
284 | 17.5M | mask = 0x1; |
285 | 17.5M | mask <<= no_of_bits - 1; |
286 | | |
287 | 17.5M | it_bit_buff->bit_count += no_of_bits; |
288 | | |
289 | 37.4M | while (no_of_bits > 0) { |
290 | 61.7M | while (no_of_bits > 0 && it_bit_buff->valid_bits < 8) { |
291 | 41.8M | it_bit_buff->byte <<= 1; |
292 | 41.8M | if (value & mask) it_bit_buff->byte |= 0x1; |
293 | 41.8M | value <<= 1; |
294 | 41.8M | no_of_bits--; |
295 | 41.8M | it_bit_buff->valid_bits++; |
296 | 41.8M | } |
297 | 19.9M | if (it_bit_buff->valid_bits == 8) { |
298 | 5.22M | *it_bit_buff->byte_ptr++ = it_bit_buff->byte; |
299 | 5.22M | it_bit_buff->byte = 0; |
300 | 5.22M | it_bit_buff->valid_bits = 0; |
301 | 5.22M | } |
302 | 19.9M | } |
303 | 17.5M | } |
304 | | |
305 | 571k | WORD32 ixheaacd_read_bit(ia_bit_buf_struct *it_bit_buff, WORD32 no_of_bits) { |
306 | 571k | UWORD32 ret_val; |
307 | 571k | UWORD8 *ptr_read_next = it_bit_buff->byte_ptr; |
308 | | |
309 | 571k | if (no_of_bits == 0) { |
310 | 274k | return 0; |
311 | 274k | } |
312 | | |
313 | 296k | ret_val = |
314 | 296k | ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->bit_count, NULL); |
315 | 296k | it_bit_buff->byte_ptr += (no_of_bits >> 3); |
316 | | |
317 | 296k | if (it_bit_buff->valid_bits != 8) { |
318 | 11.3k | UWORD8 *v = it_bit_buff->byte_ptr; |
319 | 11.3k | ret_val = (ret_val << (8 - it_bit_buff->valid_bits)) | |
320 | 11.3k | (*v >> it_bit_buff->valid_bits); |
321 | 11.3k | } |
322 | | |
323 | 296k | it_bit_buff->valid_bits -= (no_of_bits % 8); |
324 | | |
325 | 296k | ret_val = ret_val >> (32 - no_of_bits); |
326 | | |
327 | 296k | return ret_val; |
328 | 571k | } |