blob: b63dd237e74289be714d20bad2dd718ac80630fe
1 | /* |
2 | ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding |
3 | ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com |
4 | ** |
5 | ** This program is free software; you can redistribute it and/or modify |
6 | ** it under the terms of the GNU General Public License as published by |
7 | ** the Free Software Foundation; either version 2 of the License, or |
8 | ** (at your option) any later version. |
9 | ** |
10 | ** This program is distributed in the hope that it will be useful, |
11 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | ** GNU General Public License for more details. |
14 | ** |
15 | ** You should have received a copy of the GNU General Public License |
16 | ** along with this program; if not, write to the Free Software |
17 | ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 | ** |
19 | ** Any non-GPL usage of this software or parts of this software is strictly |
20 | ** forbidden. |
21 | ** |
22 | ** The "appropriate copyright message" mentioned in section 2c of the GPLv2 |
23 | ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com" |
24 | ** |
25 | ** Commercial non-GPL licensing of this software is possible. |
26 | ** For more info contact Nero AG through Mpeg4AAClicense@nero.com. |
27 | ** |
28 | ** $Id: bits.h,v 1.45 2007/11/01 12:33:29 menno Exp $ |
29 | **/ |
30 | |
31 | #ifndef __BITS_H__ |
32 | #define __BITS_H__ |
33 | |
34 | #ifdef __cplusplus |
35 | extern "C" { |
36 | #endif |
37 | |
38 | #include "analysis.h" |
39 | #ifdef ANALYSIS |
40 | #include <stdio.h> |
41 | #endif |
42 | |
43 | #define BYTE_NUMBIT 8 |
44 | #define BYTE_NUMBIT_LD 3 |
45 | //#define bit2byte(a) ((a+7)/BYTE_NUMBIT) |
46 | #define bit2byte(a) ((a+7)>>BYTE_NUMBIT_LD) |
47 | |
48 | typedef struct _bitfile { |
49 | /* bit input */ |
50 | uint32_t bufa; |
51 | uint32_t bufb; |
52 | uint32_t bits_left; |
53 | uint32_t buffer_size; /* size of the buffer in bytes */ |
54 | uint32_t bytes_left; |
55 | uint8_t error; |
56 | uint32_t *tail; |
57 | uint32_t *start; |
58 | const void *buffer; |
59 | } bitfile; |
60 | |
61 | |
62 | #if 0 |
63 | static uint32_t const bitmask[] = { |
64 | 0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, |
65 | 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, |
66 | 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, |
67 | 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, |
68 | 0xFFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF |
69 | /* added bitmask 32, correct?!?!?! */ |
70 | , 0xFFFFFFFF |
71 | }; |
72 | #endif |
73 | |
74 | void faad_initbits(bitfile *ld, const void *buffer, const uint32_t buffer_size); |
75 | void faad_endbits(bitfile *ld); |
76 | void faad_initbits_rev(bitfile *ld, void *buffer, |
77 | uint32_t bits_in_buffer); |
78 | uint8_t faad_byte_align(bitfile *ld); |
79 | uint32_t faad_get_processed_bits(bitfile *ld); |
80 | void faad_flushbits_ex(bitfile *ld, uint32_t bits); |
81 | void faad_rewindbits(bitfile *ld); |
82 | void faad_resetbits(bitfile *ld, int bits); |
83 | uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits |
84 | DEBUGDEC); |
85 | #ifdef DRM |
86 | void *faad_origbitbuffer(bitfile *ld); |
87 | uint32_t faad_origbitbuffer_size(bitfile *ld); |
88 | #endif |
89 | |
90 | /* circumvent memory alignment errors on ARM */ |
91 | static INLINE uint32_t getdword(void *mem) |
92 | { |
93 | uint32_t tmp; |
94 | #ifndef ARCH_IS_BIG_ENDIAN |
95 | ((uint8_t*)&tmp)[0] = ((uint8_t*)mem)[3]; |
96 | ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[2]; |
97 | ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[1]; |
98 | ((uint8_t*)&tmp)[3] = ((uint8_t*)mem)[0]; |
99 | #else |
100 | ((uint8_t*)&tmp)[0] = ((uint8_t*)mem)[0]; |
101 | ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[1]; |
102 | ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[2]; |
103 | ((uint8_t*)&tmp)[3] = ((uint8_t*)mem)[3]; |
104 | #endif |
105 | |
106 | return tmp; |
107 | } |
108 | |
109 | /* reads only n bytes from the stream instead of the standard 4 */ |
110 | static /*INLINE*/ uint32_t getdword_n(void *mem, int n) |
111 | { |
112 | uint32_t tmp = 0; |
113 | #ifndef ARCH_IS_BIG_ENDIAN |
114 | switch (n) { |
115 | case 3: |
116 | ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[2]; |
117 | case 2: |
118 | ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[1]; |
119 | case 1: |
120 | ((uint8_t*)&tmp)[3] = ((uint8_t*)mem)[0]; |
121 | default: |
122 | break; |
123 | } |
124 | #else |
125 | switch (n) { |
126 | case 3: |
127 | ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[2]; |
128 | case 2: |
129 | ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[1]; |
130 | case 1: |
131 | ((uint8_t*)&tmp)[0] = ((uint8_t*)mem)[0]; |
132 | default: |
133 | break; |
134 | } |
135 | #endif |
136 | |
137 | return tmp; |
138 | } |
139 | |
140 | static INLINE uint32_t faad_showbits(bitfile *ld, uint32_t bits) |
141 | { |
142 | if (bits <= ld->bits_left) { |
143 | //return (ld->bufa >> (ld->bits_left - bits)) & bitmask[bits]; |
144 | return (ld->bufa << (32 - ld->bits_left)) >> (32 - bits); |
145 | } |
146 | |
147 | bits -= ld->bits_left; |
148 | //return ((ld->bufa & bitmask[ld->bits_left]) << bits) | (ld->bufb >> (32 - bits)); |
149 | return ((ld->bufa & ((1 << ld->bits_left) - 1)) << bits) | (ld->bufb >> (32 - bits)); |
150 | } |
151 | |
152 | static INLINE void faad_flushbits(bitfile *ld, uint32_t bits) |
153 | { |
154 | /* do nothing if error */ |
155 | if (ld->error != 0) { |
156 | return; |
157 | } |
158 | |
159 | if (bits < ld->bits_left) { |
160 | ld->bits_left -= bits; |
161 | } else { |
162 | faad_flushbits_ex(ld, bits); |
163 | } |
164 | } |
165 | |
166 | /* return next n bits (right adjusted) */ |
167 | static /*INLINE*/ uint32_t faad_getbits(bitfile *ld, uint32_t n DEBUGDEC) |
168 | { |
169 | uint32_t ret; |
170 | |
171 | if (n == 0) { |
172 | return 0; |
173 | } |
174 | |
175 | ret = faad_showbits(ld, n); |
176 | faad_flushbits(ld, n); |
177 | |
178 | #ifdef ANALYSIS |
179 | if (print) { |
180 | fprintf(stdout, "%4d %2d bits, val: %4d, variable: %d %s\n", dbg_count++, n, ret, var, dbg); |
181 | } |
182 | #endif |
183 | |
184 | return ret; |
185 | } |
186 | |
187 | static INLINE uint8_t faad_get1bit(bitfile *ld DEBUGDEC) |
188 | { |
189 | uint8_t r; |
190 | |
191 | if (ld->bits_left > 0) { |
192 | ld->bits_left--; |
193 | r = (uint8_t)((ld->bufa >> ld->bits_left) & 1); |
194 | return r; |
195 | } |
196 | |
197 | /* bits_left == 0 */ |
198 | #if 0 |
199 | r = (uint8_t)(ld->bufb >> 31); |
200 | faad_flushbits_ex(ld, 1); |
201 | #else |
202 | r = (uint8_t)faad_getbits(ld, 1); |
203 | #endif |
204 | return r; |
205 | } |
206 | |
207 | /* reversed bitreading routines */ |
208 | static INLINE uint32_t faad_showbits_rev(bitfile *ld, uint32_t bits) |
209 | { |
210 | uint8_t i; |
211 | uint32_t B = 0; |
212 | |
213 | if (bits <= ld->bits_left) { |
214 | for (i = 0; i < bits; i++) { |
215 | if (ld->bufa & (1 << (i + (32 - ld->bits_left)))) { |
216 | B |= (1 << (bits - i - 1)); |
217 | } |
218 | } |
219 | return B; |
220 | } else { |
221 | for (i = 0; i < ld->bits_left; i++) { |
222 | if (ld->bufa & (1 << (i + (32 - ld->bits_left)))) { |
223 | B |= (1 << (bits - i - 1)); |
224 | } |
225 | } |
226 | for (i = 0; i < bits - ld->bits_left; i++) { |
227 | if (ld->bufb & (1 << (i + (32 - ld->bits_left)))) { |
228 | B |= (1 << (bits - ld->bits_left - i - 1)); |
229 | } |
230 | } |
231 | return B; |
232 | } |
233 | } |
234 | |
235 | static INLINE void faad_flushbits_rev(bitfile *ld, uint32_t bits) |
236 | { |
237 | /* do nothing if error */ |
238 | if (ld->error != 0) { |
239 | return; |
240 | } |
241 | |
242 | if (bits < ld->bits_left) { |
243 | ld->bits_left -= bits; |
244 | } else { |
245 | uint32_t tmp; |
246 | |
247 | ld->bufa = ld->bufb; |
248 | tmp = getdword(ld->start); |
249 | ld->bufb = tmp; |
250 | ld->start--; |
251 | ld->bits_left += (32 - bits); |
252 | |
253 | if (ld->bytes_left < 4) { |
254 | ld->error = 1; |
255 | ld->bytes_left = 0; |
256 | } else { |
257 | ld->bytes_left -= 4; |
258 | } |
259 | // if (ld->bytes_left == 0) |
260 | // ld->no_more_reading = 1; |
261 | } |
262 | } |
263 | |
264 | static /*INLINE*/ uint32_t faad_getbits_rev(bitfile *ld, uint32_t n |
265 | DEBUGDEC) |
266 | { |
267 | uint32_t ret; |
268 | |
269 | if (n == 0) { |
270 | return 0; |
271 | } |
272 | |
273 | ret = faad_showbits_rev(ld, n); |
274 | faad_flushbits_rev(ld, n); |
275 | |
276 | #ifdef ANALYSIS |
277 | if (print) { |
278 | fprintf(stdout, "%4d %2d bits, val: %4d, variable: %d %s\n", dbg_count++, n, ret, var, dbg); |
279 | } |
280 | #endif |
281 | |
282 | return ret; |
283 | } |
284 | |
285 | #ifdef DRM |
286 | /* CRC lookup table for G8 polynome in DRM standard */ |
287 | static const uint8_t crc_table_G8[256] = { |
288 | 0x0, 0x1d, 0x3a, 0x27, 0x74, 0x69, 0x4e, 0x53, |
289 | 0xe8, 0xf5, 0xd2, 0xcf, 0x9c, 0x81, 0xa6, 0xbb, |
290 | 0xcd, 0xd0, 0xf7, 0xea, 0xb9, 0xa4, 0x83, 0x9e, |
291 | 0x25, 0x38, 0x1f, 0x2, 0x51, 0x4c, 0x6b, 0x76, |
292 | 0x87, 0x9a, 0xbd, 0xa0, 0xf3, 0xee, 0xc9, 0xd4, |
293 | 0x6f, 0x72, 0x55, 0x48, 0x1b, 0x6, 0x21, 0x3c, |
294 | 0x4a, 0x57, 0x70, 0x6d, 0x3e, 0x23, 0x4, 0x19, |
295 | 0xa2, 0xbf, 0x98, 0x85, 0xd6, 0xcb, 0xec, 0xf1, |
296 | 0x13, 0xe, 0x29, 0x34, 0x67, 0x7a, 0x5d, 0x40, |
297 | 0xfb, 0xe6, 0xc1, 0xdc, 0x8f, 0x92, 0xb5, 0xa8, |
298 | 0xde, 0xc3, 0xe4, 0xf9, 0xaa, 0xb7, 0x90, 0x8d, |
299 | 0x36, 0x2b, 0xc, 0x11, 0x42, 0x5f, 0x78, 0x65, |
300 | 0x94, 0x89, 0xae, 0xb3, 0xe0, 0xfd, 0xda, 0xc7, |
301 | 0x7c, 0x61, 0x46, 0x5b, 0x8, 0x15, 0x32, 0x2f, |
302 | 0x59, 0x44, 0x63, 0x7e, 0x2d, 0x30, 0x17, 0xa, |
303 | 0xb1, 0xac, 0x8b, 0x96, 0xc5, 0xd8, 0xff, 0xe2, |
304 | 0x26, 0x3b, 0x1c, 0x1, 0x52, 0x4f, 0x68, 0x75, |
305 | 0xce, 0xd3, 0xf4, 0xe9, 0xba, 0xa7, 0x80, 0x9d, |
306 | 0xeb, 0xf6, 0xd1, 0xcc, 0x9f, 0x82, 0xa5, 0xb8, |
307 | 0x3, 0x1e, 0x39, 0x24, 0x77, 0x6a, 0x4d, 0x50, |
308 | 0xa1, 0xbc, 0x9b, 0x86, 0xd5, 0xc8, 0xef, 0xf2, |
309 | 0x49, 0x54, 0x73, 0x6e, 0x3d, 0x20, 0x7, 0x1a, |
310 | 0x6c, 0x71, 0x56, 0x4b, 0x18, 0x5, 0x22, 0x3f, |
311 | 0x84, 0x99, 0xbe, 0xa3, 0xf0, 0xed, 0xca, 0xd7, |
312 | 0x35, 0x28, 0xf, 0x12, 0x41, 0x5c, 0x7b, 0x66, |
313 | 0xdd, 0xc0, 0xe7, 0xfa, 0xa9, 0xb4, 0x93, 0x8e, |
314 | 0xf8, 0xe5, 0xc2, 0xdf, 0x8c, 0x91, 0xb6, 0xab, |
315 | 0x10, 0xd, 0x2a, 0x37, 0x64, 0x79, 0x5e, 0x43, |
316 | 0xb2, 0xaf, 0x88, 0x95, 0xc6, 0xdb, 0xfc, 0xe1, |
317 | 0x5a, 0x47, 0x60, 0x7d, 0x2e, 0x33, 0x14, 0x9, |
318 | 0x7f, 0x62, 0x45, 0x58, 0xb, 0x16, 0x31, 0x2c, |
319 | 0x97, 0x8a, 0xad, 0xb0, 0xe3, 0xfe, 0xd9, 0xc4, |
320 | }; |
321 | |
322 | static uint8_t faad_check_CRC(bitfile *ld, uint16_t len) |
323 | { |
324 | int bytes, rem; |
325 | unsigned int CRC; |
326 | unsigned int r = 255; /* Initialize to all ones */ |
327 | |
328 | /* CRC polynome used x^8 + x^4 + x^3 + x^2 +1 */ |
329 | #define GPOLY 0435 |
330 | |
331 | faad_rewindbits(ld); |
332 | |
333 | CRC = (unsigned int) ~faad_getbits(ld, 8 |
334 | DEBUGVAR(1, 999, "faad_check_CRC(): CRC")) & 0xFF; /* CRC is stored inverted */ |
335 | |
336 | bytes = len >> 3; |
337 | rem = len & 0x7; |
338 | |
339 | for (; bytes > 0; bytes--) { |
340 | r = crc_table_G8[(r ^ faad_getbits(ld, 8 DEBUGVAR(1, 998, ""))) & 0xFF]; |
341 | } |
342 | for (; rem > 0; rem--) { |
343 | r = ((r << 1) ^(((faad_get1bit(ld |
344 | DEBUGVAR(1, 998, "")) & 1) ^((r >> 7) & 1)) * GPOLY)) & 0xFF; |
345 | } |
346 | |
347 | if (r != CRC) |
348 | // if (0) |
349 | { |
350 | return 28; |
351 | } else { |
352 | return 0; |
353 | } |
354 | } |
355 | |
356 | static uint8_t tabFlipbits[256] = { |
357 | 0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, |
358 | 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, |
359 | 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, |
360 | 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, |
361 | 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242, |
362 | 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, |
363 | 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, |
364 | 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, |
365 | 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, |
366 | 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, |
367 | 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, |
368 | 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, |
369 | 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, |
370 | 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251, |
371 | 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247, |
372 | 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255 |
373 | }; |
374 | #endif |
375 | |
376 | #ifdef ERROR_RESILIENCE |
377 | |
378 | /* Modified bit reading functions for HCR */ |
379 | |
380 | typedef struct { |
381 | /* bit input */ |
382 | uint32_t bufa; |
383 | uint32_t bufb; |
384 | int8_t len; |
385 | } bits_t; |
386 | |
387 | |
388 | static INLINE uint32_t showbits_hcr(bits_t *ld, uint8_t bits) |
389 | { |
390 | if (bits == 0) { |
391 | return 0; |
392 | } |
393 | if (ld->len <= 32) { |
394 | /* huffman_spectral_data_2 needs to read more than may be available, bits maybe |
395 | > ld->len, deliver 0 than */ |
396 | if (ld->len >= bits) { |
397 | return ((ld->bufa >> (ld->len - bits)) & (0xFFFFFFFF >> (32 - bits))); |
398 | } else { |
399 | return ((ld->bufa << (bits - ld->len)) & (0xFFFFFFFF >> (32 - bits))); |
400 | } |
401 | } else { |
402 | if ((ld->len - bits) < 32) { |
403 | return ((ld->bufb & (0xFFFFFFFF >> (64 - ld->len))) << (bits - ld->len + 32)) | |
404 | (ld->bufa >> (ld->len - bits)); |
405 | } else { |
406 | return ((ld->bufb >> (ld->len - bits - 32)) & (0xFFFFFFFF >> (32 - bits))); |
407 | } |
408 | } |
409 | } |
410 | |
411 | /* return 1 if position is outside of buffer, 0 otherwise */ |
412 | static INLINE int8_t flushbits_hcr(bits_t *ld, uint8_t bits) |
413 | { |
414 | ld->len -= bits; |
415 | |
416 | if (ld->len < 0) { |
417 | ld->len = 0; |
418 | return 1; |
419 | } else { |
420 | return 0; |
421 | } |
422 | } |
423 | |
424 | static INLINE int8_t getbits_hcr(bits_t *ld, uint8_t n, uint32_t *result) |
425 | { |
426 | *result = showbits_hcr(ld, n); |
427 | return flushbits_hcr(ld, n); |
428 | } |
429 | |
430 | static INLINE int8_t get1bit_hcr(bits_t *ld, uint8_t *result) |
431 | { |
432 | uint32_t res; |
433 | int8_t ret; |
434 | |
435 | ret = getbits_hcr(ld, 1, &res); |
436 | *result = (int8_t)(res & 1); |
437 | return ret; |
438 | } |
439 | |
440 | #endif |
441 | |
442 | |
443 | #ifdef __cplusplus |
444 | } |
445 | #endif |
446 | #endif |
447 |