summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libfaad/huffman.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libfaad/huffman.c')
-rw-r--r--lib/rbcodec/codecs/libfaad/huffman.c563
1 files changed, 563 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libfaad/huffman.c b/lib/rbcodec/codecs/libfaad/huffman.c
new file mode 100644
index 0000000000..bea0dd7bf2
--- /dev/null
+++ b/lib/rbcodec/codecs/libfaad/huffman.c
@@ -0,0 +1,563 @@
1/*
2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3** Copyright (C) 2003-2004 M. Bakker, Ahead Software 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** Commercial non-GPL licensing of this software is possible.
23** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24**
25** $Id$
26**/
27
28#include "common.h"
29#include "structs.h"
30
31#include <stdlib.h>
32#ifdef ANALYSIS
33#include <stdio.h>
34#endif
35
36#include "bits.h"
37#include "huffman.h"
38#include "codebook/hcb.h"
39
40
41/* static function declarations */
42static INLINE void huffman_sign_bits_pair(bitfile *ld, int16_t *sp);
43static INLINE void huffman_sign_bits_quad(bitfile *ld, int16_t *sp);
44static INLINE int16_t huffman_getescape(bitfile *ld, int16_t sp);
45static uint8_t huffman_2step_quad(uint8_t cb, bitfile *ld, int16_t *sp);
46static uint8_t huffman_2step_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp);
47static INLINE uint8_t huffman_2step_pair(uint8_t cb, bitfile *ld, int16_t *sp);
48static INLINE uint8_t huffman_2step_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp);
49static uint8_t huffman_binary_quad(uint8_t cb, bitfile *ld, int16_t *sp);
50static uint8_t huffman_binary_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp);
51static uint8_t huffman_binary_pair(uint8_t cb, bitfile *ld, int16_t *sp);
52static uint8_t huffman_binary_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp);
53static int16_t huffman_codebook(uint8_t i) ICODE_ATTR_FAAD_LARGE_IRAM;
54#ifdef ERROR_RESILIENCE
55static void vcb11_check_LAV(uint8_t cb, int16_t *sp);
56#endif
57
58int8_t huffman_scale_factor(bitfile *ld)
59{
60 uint16_t offset = 0;
61
62 while (hcb_sf[offset][1])
63 {
64 uint8_t b = faad_get1bit(ld
65 DEBUGVAR(1,255,"huffman_scale_factor()"));
66 offset += hcb_sf[offset][b];
67
68 if (offset > 240)
69 {
70 /* printf("ERROR: offset into hcb_sf = %d >240!\n", offset); */
71 return -1;
72 }
73 }
74
75 return hcb_sf[offset][0];
76}
77
78
79hcb *hcb_table[] ICONST_ATTR = {
80 0, hcb1_1, hcb2_1, 0, hcb4_1, 0, hcb6_1, 0, hcb8_1, 0, hcb10_1, hcb11_1
81};
82
83hcb_2_quad *hcb_2_quad_table[] ICONST_ATTR = {
84 0, hcb1_2, hcb2_2, 0, hcb4_2, 0, 0, 0, 0, 0, 0, 0
85};
86
87hcb_2_pair *hcb_2_pair_table[] ICONST_ATTR = {
88 0, 0, 0, 0, 0, 0, hcb6_2, 0, hcb8_2, 0, hcb10_2, hcb11_2
89};
90
91hcb_bin_pair *hcb_bin_table[] ICONST_ATTR = {
92 0, 0, 0, 0, 0, hcb5, 0, hcb7, 0, hcb9, 0, 0
93};
94
95uint8_t hcbN[] ICONST_ATTR = { 0, 5, 5, 0, 5, 0, 5, 0, 5, 0, 6, 5 };
96
97/* defines whether a huffman codebook is unsigned or not */
98/* Table 4.6.2 */
99uint8_t unsigned_cb[] ICONST_ATTR = { 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0,
100 /* codebook 16 to 31 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
101};
102
103int hcb_2_quad_table_size[] ICONST_ATTR = { 0, 114, 86, 0, 185, 0, 0, 0, 0, 0, 0, 0 };
104int hcb_2_pair_table_size[] ICONST_ATTR = { 0, 0, 0, 0, 0, 0, 126, 0, 83, 0, 210, 373 };
105int hcb_bin_table_size[] ICONST_ATTR = { 0, 0, 0, 161, 0, 161, 0, 127, 0, 337, 0, 0 };
106
107#define FAAD_GET_SIGN(idx) \
108 if (sp[idx]) \
109 if (faad_get1bit(ld)&1) \
110 sp[idx] = -sp[idx]; \
111
112static INLINE void huffman_sign_bits_pair(bitfile *ld, int16_t *sp)
113{
114 FAAD_GET_SIGN(0)
115 FAAD_GET_SIGN(1)
116}
117
118static INLINE void huffman_sign_bits_quad(bitfile *ld, int16_t *sp)
119{
120 FAAD_GET_SIGN(0)
121 FAAD_GET_SIGN(1)
122 FAAD_GET_SIGN(2)
123 FAAD_GET_SIGN(3)
124}
125
126static INLINE int16_t huffman_getescape(bitfile *ld, int16_t sp)
127{
128 uint8_t neg, i;
129 int16_t j;
130 int16_t off;
131
132 if (sp < 0)
133 {
134 if (sp != -16)
135 return sp;
136 neg = 1;
137 } else {
138 if (sp != 16)
139 return sp;
140 neg = 0;
141 }
142
143 for (i = 4; ; i++)
144 {
145 if (faad_get1bit(ld
146 DEBUGVAR(1,6,"huffman_getescape(): escape size")) == 0)
147 {
148 break;
149 }
150 }
151
152 off = (int16_t)faad_getbits(ld, i
153 DEBUGVAR(1,9,"huffman_getescape(): escape"));
154
155 j = off | (1<<i);
156 if (neg)
157 j = -j;
158
159 return j;
160}
161
162static uint8_t huffman_2step_quad(uint8_t cb, bitfile *ld, int16_t *sp)
163{
164 uint32_t cw;
165 uint16_t offset = 0;
166 uint8_t extra_bits;
167
168 cw = faad_showbits(ld, hcbN[cb]);
169 offset = hcb_table[cb][cw].offset;
170 extra_bits = hcb_table[cb][cw].extra_bits;
171
172 if (extra_bits)
173 {
174 /* we know for sure it's more than hcbN[cb] bits long */
175 faad_flushbits(ld, hcbN[cb]);
176 offset += (uint16_t)faad_showbits(ld, extra_bits);
177 faad_flushbits(ld, hcb_2_quad_table[cb][offset].bits - hcbN[cb]);
178 } else {
179 faad_flushbits(ld, hcb_2_quad_table[cb][offset].bits);
180 }
181
182 if (offset > hcb_2_quad_table_size[cb])
183 {
184 /* printf("ERROR: offset into hcb_2_quad_table = %d >%d!\n", offset,
185 hcb_2_quad_table_size[cb]); */
186 return 10;
187 }
188
189 sp[0] = hcb_2_quad_table[cb][offset].x;
190 sp[1] = hcb_2_quad_table[cb][offset].y;
191 sp[2] = hcb_2_quad_table[cb][offset].v;
192 sp[3] = hcb_2_quad_table[cb][offset].w;
193
194 return 0;
195}
196
197static uint8_t huffman_2step_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp)
198{
199 uint8_t err = huffman_2step_quad(cb, ld, sp);
200 huffman_sign_bits_quad(ld, sp);
201
202 return err;
203}
204
205static INLINE uint8_t huffman_2step_pair(uint8_t cb, bitfile *ld, int16_t *sp)
206{
207 uint32_t cw;
208 uint16_t offset = 0;
209 uint8_t extra_bits;
210
211 cw = faad_showbits(ld, hcbN[cb]);
212 offset = hcb_table[cb][cw].offset;
213 extra_bits = hcb_table[cb][cw].extra_bits;
214
215 if (extra_bits)
216 {
217 /* we know for sure it's more than hcbN[cb] bits long */
218 faad_flushbits(ld, hcbN[cb]);
219 offset += (uint16_t)faad_showbits(ld, extra_bits);
220 faad_flushbits(ld, hcb_2_pair_table[cb][offset].bits - hcbN[cb]);
221 } else {
222 faad_flushbits(ld, hcb_2_pair_table[cb][offset].bits);
223 }
224
225 if (offset > hcb_2_pair_table_size[cb])
226 {
227 /* printf("ERROR: offset into hcb_2_pair_table = %d >%d!\n", offset,
228 hcb_2_pair_table_size[cb]); */
229 return 10;
230 }
231
232 sp[0] = hcb_2_pair_table[cb][offset].x;
233 sp[1] = hcb_2_pair_table[cb][offset].y;
234
235 return 0;
236}
237
238static INLINE uint8_t huffman_2step_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp)
239{
240 uint8_t err = huffman_2step_pair(cb, ld, sp);
241 huffman_sign_bits_pair(ld, sp);
242
243 return err;
244}
245
246static uint8_t huffman_binary_quad(uint8_t cb, bitfile *ld, int16_t *sp)
247{
248 uint16_t offset = 0;
249
250 while (!hcb3[offset].is_leaf)
251 {
252 uint8_t b = faad_get1bit(ld
253 DEBUGVAR(1,255,"huffman_spectral_data():3"));
254 offset += hcb3[offset].data[b];
255 }
256
257 if (offset > hcb_bin_table_size[cb])
258 {
259 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
260 hcb_bin_table_size[cb]); */
261 return 10;
262 }
263
264 sp[0] = hcb3[offset].data[0];
265 sp[1] = hcb3[offset].data[1];
266 sp[2] = hcb3[offset].data[2];
267 sp[3] = hcb3[offset].data[3];
268
269 return 0;
270}
271
272static uint8_t huffman_binary_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp)
273{
274 uint8_t err = huffman_binary_quad(cb, ld, sp);
275 huffman_sign_bits_quad(ld, sp);
276
277 return err;
278}
279
280static uint8_t huffman_binary_pair(uint8_t cb, bitfile *ld, int16_t *sp)
281{
282 uint16_t offset = 0;
283
284 while (!hcb_bin_table[cb][offset].is_leaf)
285 {
286 uint8_t b = faad_get1bit(ld
287 DEBUGVAR(1,255,"huffman_spectral_data():9"));
288 offset += hcb_bin_table[cb][offset].data[b];
289 }
290
291 if (offset > hcb_bin_table_size[cb])
292 {
293 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
294 hcb_bin_table_size[cb]); */
295 return 10;
296 }
297
298 sp[0] = hcb_bin_table[cb][offset].data[0];
299 sp[1] = hcb_bin_table[cb][offset].data[1];
300
301 return 0;
302}
303
304static uint8_t huffman_binary_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp)
305{
306 uint8_t err = huffman_binary_pair(cb, ld, sp);
307 huffman_sign_bits_pair(ld, sp);
308
309 return err;
310}
311
312static int16_t huffman_codebook(uint8_t i)
313{
314 static const uint32_t data = 16428320;
315 if (i == 0) return (int16_t)(data >> 16) & 0xFFFF;
316 else return (int16_t)data & 0xFFFF;
317}
318
319#ifdef ERROR_RESILIENCE
320static void vcb11_check_LAV(uint8_t cb, int16_t *sp)
321{
322 static const uint16_t vcb11_LAV_tab[] = {
323 16, 31, 47, 63, 95, 127, 159, 191, 223,
324 255, 319, 383, 511, 767, 1023, 2047
325 };
326 uint16_t max = 0;
327
328 if (cb < 16 || cb > 31)
329 return;
330
331 max = vcb11_LAV_tab[cb - 16];
332
333 if ((abs(sp[0]) > max) || (abs(sp[1]) > max))
334 {
335 sp[0] = 0;
336 sp[1] = 0;
337 }
338}
339#endif
340
341uint8_t huffman_spectral_data(uint8_t cb, bitfile *ld, int16_t *sp)
342{
343 switch (cb)
344 {
345 case 1: /* 2-step method for data quadruples */
346 case 2:
347 return huffman_2step_quad(cb, ld, sp);
348 case 3: /* binary search for data quadruples */
349 return huffman_binary_quad_sign(cb, ld, sp);
350 case 4: /* 2-step method for data quadruples */
351 return huffman_2step_quad_sign(cb, ld, sp);
352 case 5: /* binary search for data pairs */
353 return huffman_binary_pair(cb, ld, sp);
354 case 6: /* 2-step method for data pairs */
355 return huffman_2step_pair(cb, ld, sp);
356 case 7: /* binary search for data pairs */
357 case 9:
358 return huffman_binary_pair_sign(cb, ld, sp);
359 case 8: /* 2-step method for data pairs */
360 case 10:
361 return huffman_2step_pair_sign(cb, ld, sp);
362 case 12: {
363 uint8_t err = huffman_2step_pair(11, ld, sp);
364 sp[0] = huffman_codebook(0); sp[1] = huffman_codebook(1);
365 return err; }
366 case 11:
367 {
368 uint8_t err = huffman_2step_pair_sign(11, ld, sp);
369 sp[0] = huffman_getescape(ld, sp[0]);
370 sp[1] = huffman_getescape(ld, sp[1]);
371 return err;
372 }
373#ifdef ERROR_RESILIENCE
374 /* VCB11 uses codebook 11 */
375 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
376 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
377 {
378 uint8_t err = huffman_2step_pair_sign(11, ld, sp);
379 sp[0] = huffman_getescape(ld, sp[0]);
380 sp[1] = huffman_getescape(ld, sp[1]);
381
382 /* check LAV (Largest Absolute Value) */
383 /* this finds errors in the ESCAPE signal */
384 vcb11_check_LAV(cb, sp);
385
386 return err;
387 }
388#endif
389 default:
390 /* Non existent codebook number, something went wrong */
391 return 11;
392 }
393
394 return 0;
395}
396
397
398#ifdef ERROR_RESILIENCE
399
400/* Special version of huffman_spectral_data
401Will not read from a bitfile but a bits_t structure.
402Will keep track of the bits decoded and return the number of bits remaining.
403Do not read more than ld->len, return -1 if codeword would be longer */
404
405int8_t huffman_spectral_data_2(uint8_t cb, bits_t *ld, int16_t *sp)
406{
407 uint32_t cw;
408 uint16_t offset = 0;
409 uint8_t extra_bits;
410 uint8_t i, vcb11 = 0;
411
412
413 switch (cb)
414 {
415 case 1: /* 2-step method for data quadruples */
416 case 2:
417 case 4:
418
419 cw = showbits_hcr(ld, hcbN[cb]);
420 offset = hcb_table[cb][cw].offset;
421 extra_bits = hcb_table[cb][cw].extra_bits;
422
423 if (extra_bits)
424 {
425 /* we know for sure it's more than hcbN[cb] bits long */
426 if ( flushbits_hcr(ld, hcbN[cb]) ) return -1;
427 offset += (uint16_t)showbits_hcr(ld, extra_bits);
428 if ( flushbits_hcr(ld, hcb_2_quad_table[cb][offset].bits - hcbN[cb]) ) return -1;
429 } else {
430 if ( flushbits_hcr(ld, hcb_2_quad_table[cb][offset].bits) ) return -1;
431 }
432
433 sp[0] = hcb_2_quad_table[cb][offset].x;
434 sp[1] = hcb_2_quad_table[cb][offset].y;
435 sp[2] = hcb_2_quad_table[cb][offset].v;
436 sp[3] = hcb_2_quad_table[cb][offset].w;
437 break;
438
439 case 6: /* 2-step method for data pairs */
440 case 8:
441 case 10:
442 case 11:
443 /* VCB11 uses codebook 11 */
444 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
445 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
446
447 if (cb >= 16)
448 {
449 /* store the virtual codebook */
450 vcb11 = cb;
451 cb = 11;
452 }
453
454 cw = showbits_hcr(ld, hcbN[cb]);
455 offset = hcb_table[cb][cw].offset;
456 extra_bits = hcb_table[cb][cw].extra_bits;
457
458 if (extra_bits)
459 {
460 /* we know for sure it's more than hcbN[cb] bits long */
461 if ( flushbits_hcr(ld, hcbN[cb]) ) return -1;
462 offset += (uint16_t)showbits_hcr(ld, extra_bits);
463 if ( flushbits_hcr(ld, hcb_2_pair_table[cb][offset].bits - hcbN[cb]) ) return -1;
464 } else {
465 if ( flushbits_hcr(ld, hcb_2_pair_table[cb][offset].bits) ) return -1;
466 }
467 sp[0] = hcb_2_pair_table[cb][offset].x;
468 sp[1] = hcb_2_pair_table[cb][offset].y;
469 break;
470
471 case 3: /* binary search for data quadruples */
472
473 while (!hcb3[offset].is_leaf)
474 {
475 uint8_t b;
476
477 if ( get1bit_hcr(ld, &b) ) return -1;
478 offset += hcb3[offset].data[b];
479 }
480
481 sp[0] = hcb3[offset].data[0];
482 sp[1] = hcb3[offset].data[1];
483 sp[2] = hcb3[offset].data[2];
484 sp[3] = hcb3[offset].data[3];
485
486 break;
487
488 case 5: /* binary search for data pairs */
489 case 7:
490 case 9:
491
492 while (!hcb_bin_table[cb][offset].is_leaf)
493 {
494 uint8_t b;
495
496 if (get1bit_hcr(ld, &b) ) return -1;
497 offset += hcb_bin_table[cb][offset].data[b];
498 }
499
500 sp[0] = hcb_bin_table[cb][offset].data[0];
501 sp[1] = hcb_bin_table[cb][offset].data[1];
502
503 break;
504 }
505
506 /* decode sign bits */
507 if (unsigned_cb[cb])
508 {
509 for(i = 0; i < ((cb < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN); i++)
510 {
511 if(sp[i])
512 {
513 uint8_t b;
514 if ( get1bit_hcr(ld, &b) ) return -1;
515 if (b != 0) {
516 sp[i] = -sp[i];
517 }
518 }
519 }
520 }
521
522 /* decode huffman escape bits */
523 if ((cb == ESC_HCB) || (cb >= 16))
524 {
525 uint8_t k;
526 for (k = 0; k < 2; k++)
527 {
528 if ((sp[k] == 16) || (sp[k] == -16))
529 {
530 uint8_t neg, i;
531 int32_t j;
532 uint32_t off;
533
534 neg = (sp[k] < 0) ? 1 : 0;
535
536 for (i = 4; ; i++)
537 {
538 uint8_t b;
539 if (get1bit_hcr(ld, &b))
540 return -1;
541 if (b == 0)
542 break;
543 }
544
545 if (getbits_hcr(ld, i, &off))
546 return -1;
547 j = off + (1<<i);
548 sp[k] = (int16_t)((neg) ? -j : j);
549 }
550 }
551
552 if (vcb11 != 0)
553 {
554 /* check LAV (Largest Absolute Value) */
555 /* this finds errors in the ESCAPE signal */
556 vcb11_check_LAV(vcb11, sp);
557 }
558 }
559 return ld->len;
560}
561
562#endif
563