2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
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.
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.
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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
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"
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
28 ** $Id: huffman.c,v 1.26 2007/11/01 12:33:30 menno Exp $
41 #include "codebook/hcb.h"
44 /* static function declarations */
45 static INLINE void huffman_sign_bits(bitfile *ld, int16_t *sp, uint8_t len);
46 static INLINE int16_t huffman_getescape(bitfile *ld, int16_t sp);
47 static uint8_t huffman_2step_quad(uint8_t cb, bitfile *ld, int16_t *sp);
48 static uint8_t huffman_2step_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp);
49 static uint8_t huffman_2step_pair(uint8_t cb, bitfile *ld, int16_t *sp);
50 static uint8_t huffman_2step_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp);
51 static uint8_t huffman_binary_quad(uint8_t cb, bitfile *ld, int16_t *sp);
52 static uint8_t huffman_binary_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp);
53 static uint8_t huffman_binary_pair(uint8_t cb, bitfile *ld, int16_t *sp);
54 static uint8_t huffman_binary_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp);
55 static int16_t huffman_codebook(uint8_t i);
56 static void vcb11_check_LAV(uint8_t cb, int16_t *sp);
58 int8_t huffman_scale_factor(bitfile *ld)
62 while (hcb_sf[offset][1])
64 uint8_t b = faad_get1bit(ld
65 DEBUGVAR(1,255,"huffman_scale_factor()"));
66 offset += hcb_sf[offset][b];
70 /* printf("ERROR: offset into hcb_sf = %d >240!\n", offset); */
75 return hcb_sf[offset][0];
80 0, hcb1_1, hcb2_1, 0, hcb4_1, 0, hcb6_1, 0, hcb8_1, 0, hcb10_1, hcb11_1
83 hcb_2_quad *hcb_2_quad_table[] = {
84 0, hcb1_2, hcb2_2, 0, hcb4_2, 0, 0, 0, 0, 0, 0, 0
87 hcb_2_pair *hcb_2_pair_table[] = {
88 0, 0, 0, 0, 0, 0, hcb6_2, 0, hcb8_2, 0, hcb10_2, hcb11_2
91 hcb_bin_pair *hcb_bin_table[] = {
92 0, 0, 0, 0, 0, hcb5, 0, hcb7, 0, hcb9, 0, 0
95 uint8_t hcbN[] = { 0, 5, 5, 0, 5, 0, 5, 0, 5, 0, 6, 5 };
97 /* defines whether a huffman codebook is unsigned or not */
99 uint8_t unsigned_cb[] = { 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
103 int hcb_2_quad_table_size[] = { 0, 114, 86, 0, 185, 0, 0, 0, 0, 0, 0, 0 };
104 int hcb_2_pair_table_size[] = { 0, 0, 0, 0, 0, 0, 126, 0, 83, 0, 210, 373 };
105 int hcb_bin_table_size[] = { 0, 0, 0, 161, 0, 161, 0, 127, 0, 337, 0, 0 };
107 static INLINE void huffman_sign_bits(bitfile *ld, int16_t *sp, uint8_t len)
111 for (i = 0; i < len; i++)
116 DEBUGVAR(1,5,"huffman_sign_bits(): sign bit")) & 1)
124 static INLINE int16_t huffman_getescape(bitfile *ld, int16_t sp)
144 DEBUGVAR(1,6,"huffman_getescape(): escape size")) == 0)
150 off = (int16_t)faad_getbits(ld, i
151 DEBUGVAR(1,9,"huffman_getescape(): escape"));
160 static uint8_t huffman_2step_quad(uint8_t cb, bitfile *ld, int16_t *sp)
166 cw = faad_showbits(ld, hcbN[cb]);
167 offset = hcb_table[cb][cw].offset;
168 extra_bits = hcb_table[cb][cw].extra_bits;
172 /* we know for sure it's more than hcbN[cb] bits long */
173 faad_flushbits(ld, hcbN[cb]);
174 offset += (uint16_t)faad_showbits(ld, extra_bits);
175 faad_flushbits(ld, hcb_2_quad_table[cb][offset].bits - hcbN[cb]);
177 faad_flushbits(ld, hcb_2_quad_table[cb][offset].bits);
180 if (offset > hcb_2_quad_table_size[cb])
182 /* printf("ERROR: offset into hcb_2_quad_table = %d >%d!\n", offset,
183 hcb_2_quad_table_size[cb]); */
187 sp[0] = hcb_2_quad_table[cb][offset].x;
188 sp[1] = hcb_2_quad_table[cb][offset].y;
189 sp[2] = hcb_2_quad_table[cb][offset].v;
190 sp[3] = hcb_2_quad_table[cb][offset].w;
195 static uint8_t huffman_2step_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp)
197 uint8_t err = huffman_2step_quad(cb, ld, sp);
198 huffman_sign_bits(ld, sp, QUAD_LEN);
203 static uint8_t huffman_2step_pair(uint8_t cb, bitfile *ld, int16_t *sp)
209 cw = faad_showbits(ld, hcbN[cb]);
210 offset = hcb_table[cb][cw].offset;
211 extra_bits = hcb_table[cb][cw].extra_bits;
215 /* we know for sure it's more than hcbN[cb] bits long */
216 faad_flushbits(ld, hcbN[cb]);
217 offset += (uint16_t)faad_showbits(ld, extra_bits);
218 faad_flushbits(ld, hcb_2_pair_table[cb][offset].bits - hcbN[cb]);
220 faad_flushbits(ld, hcb_2_pair_table[cb][offset].bits);
223 if (offset > hcb_2_pair_table_size[cb])
225 /* printf("ERROR: offset into hcb_2_pair_table = %d >%d!\n", offset,
226 hcb_2_pair_table_size[cb]); */
230 sp[0] = hcb_2_pair_table[cb][offset].x;
231 sp[1] = hcb_2_pair_table[cb][offset].y;
236 static uint8_t huffman_2step_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp)
238 uint8_t err = huffman_2step_pair(cb, ld, sp);
239 huffman_sign_bits(ld, sp, PAIR_LEN);
244 static uint8_t huffman_binary_quad(uint8_t cb, bitfile *ld, int16_t *sp)
248 while (!hcb3[offset].is_leaf)
250 uint8_t b = faad_get1bit(ld
251 DEBUGVAR(1,255,"huffman_spectral_data():3"));
252 offset += hcb3[offset].data[b];
255 if (offset > hcb_bin_table_size[cb])
257 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
258 hcb_bin_table_size[cb]); */
262 sp[0] = hcb3[offset].data[0];
263 sp[1] = hcb3[offset].data[1];
264 sp[2] = hcb3[offset].data[2];
265 sp[3] = hcb3[offset].data[3];
270 static uint8_t huffman_binary_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp)
272 uint8_t err = huffman_binary_quad(cb, ld, sp);
273 huffman_sign_bits(ld, sp, QUAD_LEN);
278 static uint8_t huffman_binary_pair(uint8_t cb, bitfile *ld, int16_t *sp)
282 while (!hcb_bin_table[cb][offset].is_leaf)
284 uint8_t b = faad_get1bit(ld
285 DEBUGVAR(1,255,"huffman_spectral_data():9"));
286 offset += hcb_bin_table[cb][offset].data[b];
289 if (offset > hcb_bin_table_size[cb])
291 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
292 hcb_bin_table_size[cb]); */
296 sp[0] = hcb_bin_table[cb][offset].data[0];
297 sp[1] = hcb_bin_table[cb][offset].data[1];
302 static uint8_t huffman_binary_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp)
304 uint8_t err = huffman_binary_pair(cb, ld, sp);
305 huffman_sign_bits(ld, sp, PAIR_LEN);
310 static int16_t huffman_codebook(uint8_t i)
312 static const uint32_t data = 16428320;
313 if (i == 0) return (int16_t)(data >> 16) & 0xFFFF;
314 else return (int16_t)data & 0xFFFF;
317 static void vcb11_check_LAV(uint8_t cb, int16_t *sp)
319 static const uint16_t vcb11_LAV_tab[] = {
320 16, 31, 47, 63, 95, 127, 159, 191, 223,
321 255, 319, 383, 511, 767, 1023, 2047
325 if (cb < 16 || cb > 31)
328 max = vcb11_LAV_tab[cb - 16];
330 if ((abs(sp[0]) > max) || (abs(sp[1]) > max))
337 uint8_t huffman_spectral_data(uint8_t cb, bitfile *ld, int16_t *sp)
341 case 1: /* 2-step method for data quadruples */
343 return huffman_2step_quad(cb, ld, sp);
344 case 3: /* binary search for data quadruples */
345 return huffman_binary_quad_sign(cb, ld, sp);
346 case 4: /* 2-step method for data quadruples */
347 return huffman_2step_quad_sign(cb, ld, sp);
348 case 5: /* binary search for data pairs */
349 return huffman_binary_pair(cb, ld, sp);
350 case 6: /* 2-step method for data pairs */
351 return huffman_2step_pair(cb, ld, sp);
352 case 7: /* binary search for data pairs */
354 return huffman_binary_pair_sign(cb, ld, sp);
355 case 8: /* 2-step method for data pairs */
357 return huffman_2step_pair_sign(cb, ld, sp);
359 uint8_t err = huffman_2step_pair(11, ld, sp);
360 sp[0] = huffman_codebook(0); sp[1] = huffman_codebook(1);
364 uint8_t err = huffman_2step_pair_sign(11, ld, sp);
365 sp[0] = huffman_getescape(ld, sp[0]);
366 sp[1] = huffman_getescape(ld, sp[1]);
369 #ifdef ERROR_RESILIENCE
370 /* VCB11 uses codebook 11 */
371 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
372 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
374 uint8_t err = huffman_2step_pair_sign(11, ld, sp);
375 sp[0] = huffman_getescape(ld, sp[0]);
376 sp[1] = huffman_getescape(ld, sp[1]);
378 /* check LAV (Largest Absolute Value) */
379 /* this finds errors in the ESCAPE signal */
380 vcb11_check_LAV(cb, sp);
386 /* Non existent codebook number, something went wrong */
394 #ifdef ERROR_RESILIENCE
396 /* Special version of huffman_spectral_data
397 Will not read from a bitfile but a bits_t structure.
398 Will keep track of the bits decoded and return the number of bits remaining.
399 Do not read more than ld->len, return -1 if codeword would be longer */
401 int8_t huffman_spectral_data_2(uint8_t cb, bits_t *ld, int16_t *sp)
406 uint8_t i, vcb11 = 0;
411 case 1: /* 2-step method for data quadruples */
415 cw = showbits_hcr(ld, hcbN[cb]);
416 offset = hcb_table[cb][cw].offset;
417 extra_bits = hcb_table[cb][cw].extra_bits;
421 /* we know for sure it's more than hcbN[cb] bits long */
422 if ( flushbits_hcr(ld, hcbN[cb]) ) return -1;
423 offset += (uint16_t)showbits_hcr(ld, extra_bits);
424 if ( flushbits_hcr(ld, hcb_2_quad_table[cb][offset].bits - hcbN[cb]) ) return -1;
426 if ( flushbits_hcr(ld, hcb_2_quad_table[cb][offset].bits) ) return -1;
429 sp[0] = hcb_2_quad_table[cb][offset].x;
430 sp[1] = hcb_2_quad_table[cb][offset].y;
431 sp[2] = hcb_2_quad_table[cb][offset].v;
432 sp[3] = hcb_2_quad_table[cb][offset].w;
435 case 6: /* 2-step method for data pairs */
439 /* VCB11 uses codebook 11 */
440 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
441 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
445 /* store the virtual codebook */
450 cw = showbits_hcr(ld, hcbN[cb]);
451 offset = hcb_table[cb][cw].offset;
452 extra_bits = hcb_table[cb][cw].extra_bits;
456 /* we know for sure it's more than hcbN[cb] bits long */
457 if ( flushbits_hcr(ld, hcbN[cb]) ) return -1;
458 offset += (uint16_t)showbits_hcr(ld, extra_bits);
459 if ( flushbits_hcr(ld, hcb_2_pair_table[cb][offset].bits - hcbN[cb]) ) return -1;
461 if ( flushbits_hcr(ld, hcb_2_pair_table[cb][offset].bits) ) return -1;
463 sp[0] = hcb_2_pair_table[cb][offset].x;
464 sp[1] = hcb_2_pair_table[cb][offset].y;
467 case 3: /* binary search for data quadruples */
469 while (!hcb3[offset].is_leaf)
473 if ( get1bit_hcr(ld, &b) ) return -1;
474 offset += hcb3[offset].data[b];
477 sp[0] = hcb3[offset].data[0];
478 sp[1] = hcb3[offset].data[1];
479 sp[2] = hcb3[offset].data[2];
480 sp[3] = hcb3[offset].data[3];
484 case 5: /* binary search for data pairs */
488 while (!hcb_bin_table[cb][offset].is_leaf)
492 if (get1bit_hcr(ld, &b) ) return -1;
493 offset += hcb_bin_table[cb][offset].data[b];
496 sp[0] = hcb_bin_table[cb][offset].data[0];
497 sp[1] = hcb_bin_table[cb][offset].data[1];
502 /* decode sign bits */
505 for(i = 0; i < ((cb < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN); i++)
510 if ( get1bit_hcr(ld, &b) ) return -1;
518 /* decode huffman escape bits */
519 if ((cb == ESC_HCB) || (cb >= 16))
522 for (k = 0; k < 2; k++)
524 if ((sp[k] == 16) || (sp[k] == -16))
530 neg = (sp[k] < 0) ? 1 : 0;
535 if (get1bit_hcr(ld, &b))
541 if (getbits_hcr(ld, i, &off))
544 sp[k] = (int16_t)((neg) ? -j : j);
550 /* check LAV (Largest Absolute Value) */
551 /* this finds errors in the ESCAPE signal */
552 vcb11_check_LAV(vcb11, sp);