2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software 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 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id: huffman.c,v 1.20 2004/03/10 19:45:41 menno Exp $
38 #include "codebook/hcb.h"
41 /* static function declarations */
42 static INLINE
void huffman_sign_bits(bitfile
*ld
, int16_t *sp
, uint8_t len
);
43 static INLINE
int16_t huffman_getescape(bitfile
*ld
, int16_t sp
);
44 static uint8_t huffman_2step_quad(uint8_t cb
, bitfile
*ld
, int16_t *sp
);
45 static uint8_t huffman_2step_quad_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
);
46 static uint8_t huffman_2step_pair(uint8_t cb
, bitfile
*ld
, int16_t *sp
);
47 static uint8_t huffman_2step_pair_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
);
48 static uint8_t huffman_binary_quad(uint8_t cb
, bitfile
*ld
, int16_t *sp
);
49 static uint8_t huffman_binary_quad_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
);
50 static uint8_t huffman_binary_pair(uint8_t cb
, bitfile
*ld
, int16_t *sp
);
51 static uint8_t huffman_binary_pair_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
);
52 static int16_t huffman_codebook(uint8_t i
);
54 int8_t huffman_scale_factor(bitfile
*ld
)
58 while (hcb_sf
[offset
][1])
60 uint8_t b
= faad_get1bit(ld
61 DEBUGVAR(1,255,"huffman_scale_factor()"));
62 offset
+= hcb_sf
[offset
][b
];
66 /* printf("ERROR: offset into hcb_sf = %d >240!\n", offset); */
71 return hcb_sf
[offset
][0];
76 0, hcb1_1
, hcb2_1
, 0, hcb4_1
, 0, hcb6_1
, 0, hcb8_1
, 0, hcb10_1
, hcb11_1
79 hcb_2_quad
*hcb_2_quad_table
[] = {
80 0, hcb1_2
, hcb2_2
, 0, hcb4_2
, 0, 0, 0, 0, 0, 0, 0
83 hcb_2_pair
*hcb_2_pair_table
[] = {
84 0, 0, 0, 0, 0, 0, hcb6_2
, 0, hcb8_2
, 0, hcb10_2
, hcb11_2
87 hcb_bin_pair
*hcb_bin_table
[] = {
88 0, 0, 0, 0, 0, hcb5
, 0, hcb7
, 0, hcb9
, 0, 0
91 uint8_t hcbN
[] = { 0, 5, 5, 0, 5, 0, 5, 0, 5, 0, 6, 5 };
93 /* defines whether a huffman codebook is unsigned or not */
95 uint8_t unsigned_cb
[] = { 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0,
96 /* codebook 16 to 31 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
99 int hcb_2_quad_table_size
[] = { 0, 114, 86, 0, 185, 0, 0, 0, 0, 0, 0, 0 };
100 int hcb_2_pair_table_size
[] = { 0, 0, 0, 0, 0, 0, 126, 0, 83, 0, 210, 373 };
101 int hcb_bin_table_size
[] = { 0, 0, 0, 161, 0, 161, 0, 127, 0, 337, 0, 0 };
103 static INLINE
void huffman_sign_bits(bitfile
*ld
, int16_t *sp
, uint8_t len
)
107 for (i
= 0; i
< len
; i
++)
112 DEBUGVAR(1,5,"huffman_sign_bits(): sign bit")) & 1)
120 static INLINE
int16_t huffman_getescape(bitfile
*ld
, int16_t sp
)
140 DEBUGVAR(1,6,"huffman_getescape(): escape size")) == 0)
146 off
= (int16_t)faad_getbits(ld
, i
147 DEBUGVAR(1,9,"huffman_getescape(): escape"));
156 static uint8_t huffman_2step_quad(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
162 cw
= faad_showbits(ld
, hcbN
[cb
]);
163 offset
= hcb_table
[cb
][cw
].offset
;
164 extra_bits
= hcb_table
[cb
][cw
].extra_bits
;
168 /* we know for sure it's more than hcbN[cb] bits long */
169 faad_flushbits(ld
, hcbN
[cb
]);
170 offset
+= (uint16_t)faad_showbits(ld
, extra_bits
);
171 faad_flushbits(ld
, hcb_2_quad_table
[cb
][offset
].bits
- hcbN
[cb
]);
173 faad_flushbits(ld
, hcb_2_quad_table
[cb
][offset
].bits
);
176 if (offset
> hcb_2_quad_table_size
[cb
])
178 /* printf("ERROR: offset into hcb_2_quad_table = %d >%d!\n", offset,
179 hcb_2_quad_table_size[cb]); */
183 sp
[0] = hcb_2_quad_table
[cb
][offset
].x
;
184 sp
[1] = hcb_2_quad_table
[cb
][offset
].y
;
185 sp
[2] = hcb_2_quad_table
[cb
][offset
].v
;
186 sp
[3] = hcb_2_quad_table
[cb
][offset
].w
;
191 static uint8_t huffman_2step_quad_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
193 uint8_t err
= huffman_2step_quad(cb
, ld
, sp
);
194 huffman_sign_bits(ld
, sp
, QUAD_LEN
);
199 static uint8_t huffman_2step_pair(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
205 cw
= faad_showbits(ld
, hcbN
[cb
]);
206 offset
= hcb_table
[cb
][cw
].offset
;
207 extra_bits
= hcb_table
[cb
][cw
].extra_bits
;
211 /* we know for sure it's more than hcbN[cb] bits long */
212 faad_flushbits(ld
, hcbN
[cb
]);
213 offset
+= (uint16_t)faad_showbits(ld
, extra_bits
);
214 faad_flushbits(ld
, hcb_2_pair_table
[cb
][offset
].bits
- hcbN
[cb
]);
216 faad_flushbits(ld
, hcb_2_pair_table
[cb
][offset
].bits
);
219 if (offset
> hcb_2_pair_table_size
[cb
])
221 /* printf("ERROR: offset into hcb_2_pair_table = %d >%d!\n", offset,
222 hcb_2_pair_table_size[cb]); */
226 sp
[0] = hcb_2_pair_table
[cb
][offset
].x
;
227 sp
[1] = hcb_2_pair_table
[cb
][offset
].y
;
232 static uint8_t huffman_2step_pair_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
234 uint8_t err
= huffman_2step_pair(cb
, ld
, sp
);
235 huffman_sign_bits(ld
, sp
, PAIR_LEN
);
240 static uint8_t huffman_binary_quad(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
244 while (!hcb3
[offset
].is_leaf
)
246 uint8_t b
= faad_get1bit(ld
247 DEBUGVAR(1,255,"huffman_spectral_data():3"));
248 offset
+= hcb3
[offset
].data
[b
];
251 if (offset
> hcb_bin_table_size
[cb
])
253 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
254 hcb_bin_table_size[cb]); */
258 sp
[0] = hcb3
[offset
].data
[0];
259 sp
[1] = hcb3
[offset
].data
[1];
260 sp
[2] = hcb3
[offset
].data
[2];
261 sp
[3] = hcb3
[offset
].data
[3];
266 static uint8_t huffman_binary_quad_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
268 uint8_t err
= huffman_binary_quad(cb
, ld
, sp
);
269 huffman_sign_bits(ld
, sp
, QUAD_LEN
);
274 static uint8_t huffman_binary_pair(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
278 while (!hcb_bin_table
[cb
][offset
].is_leaf
)
280 uint8_t b
= faad_get1bit(ld
281 DEBUGVAR(1,255,"huffman_spectral_data():9"));
282 offset
+= hcb_bin_table
[cb
][offset
].data
[b
];
285 if (offset
> hcb_bin_table_size
[cb
])
287 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
288 hcb_bin_table_size[cb]); */
292 sp
[0] = hcb_bin_table
[cb
][offset
].data
[0];
293 sp
[1] = hcb_bin_table
[cb
][offset
].data
[1];
298 static uint8_t huffman_binary_pair_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
300 uint8_t err
= huffman_binary_pair(cb
, ld
, sp
);
301 huffman_sign_bits(ld
, sp
, PAIR_LEN
);
306 static int16_t huffman_codebook(uint8_t i
)
308 static const uint32_t data
= 16428320;
309 if (i
== 0) return (int16_t)(data
>> 16) & 0xFFFF;
310 else return (int16_t)data
& 0xFFFF;
313 uint8_t huffman_spectral_data(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
317 case 1: /* 2-step method for data quadruples */
319 return huffman_2step_quad(cb
, ld
, sp
);
320 case 3: /* binary search for data quadruples */
321 return huffman_binary_quad_sign(cb
, ld
, sp
);
322 case 4: /* 2-step method for data quadruples */
323 return huffman_2step_quad_sign(cb
, ld
, sp
);
324 case 5: /* binary search for data pairs */
325 return huffman_binary_pair(cb
, ld
, sp
);
326 case 6: /* 2-step method for data pairs */
327 return huffman_2step_pair(cb
, ld
, sp
);
328 case 7: /* binary search for data pairs */
330 return huffman_binary_pair_sign(cb
, ld
, sp
);
331 case 8: /* 2-step method for data pairs */
333 return huffman_2step_pair_sign(cb
, ld
, sp
);
335 uint8_t err
= huffman_2step_pair(11, ld
, sp
);
336 sp
[0] = huffman_codebook(0); sp
[1] = huffman_codebook(1);
339 #ifdef ERROR_RESILIENCE
340 /* VCB11 uses codebook 11 */
341 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
342 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
343 /* TODO: If ER is used, some extra error checking should be done */
346 uint8_t err
= huffman_2step_pair_sign(11, ld
, sp
);
347 sp
[0] = huffman_getescape(ld
, sp
[0]);
348 sp
[1] = huffman_getescape(ld
, sp
[1]);
352 /* Non existent codebook number, something went wrong */
360 #ifdef ERROR_RESILIENCE
362 /* Special version of huffman_spectral_data
363 Will not read from a bitfile but a bits_t structure.
364 Will keep track of the bits decoded and return the number of bits remaining.
365 Do not read more than ld->len, return -1 if codeword would be longer */
367 int8_t huffman_spectral_data_2(uint8_t cb
, bits_t
*ld
, int16_t *sp
)
377 case 1: /* 2-step method for data quadruples */
381 cw
= showbits_hcr(ld
, hcbN
[cb
]);
382 offset
= hcb_table
[cb
][cw
].offset
;
383 extra_bits
= hcb_table
[cb
][cw
].extra_bits
;
387 /* we know for sure it's more than hcbN[cb] bits long */
388 if ( flushbits_hcr(ld
, hcbN
[cb
]) ) return -1;
389 offset
+= (uint16_t)showbits_hcr(ld
, extra_bits
);
390 if ( flushbits_hcr(ld
, hcb_2_quad_table
[cb
][offset
].bits
- hcbN
[cb
]) ) return -1;
392 if ( flushbits_hcr(ld
, hcb_2_quad_table
[cb
][offset
].bits
) ) return -1;
395 sp
[0] = hcb_2_quad_table
[cb
][offset
].x
;
396 sp
[1] = hcb_2_quad_table
[cb
][offset
].y
;
397 sp
[2] = hcb_2_quad_table
[cb
][offset
].v
;
398 sp
[3] = hcb_2_quad_table
[cb
][offset
].w
;
401 case 6: /* 2-step method for data pairs */
405 /* VCB11 uses codebook 11 */
406 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
407 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
409 /* TODO: If ER is used, some extra error checking should be done */
413 cw
= showbits_hcr(ld
, hcbN
[cb
]);
414 offset
= hcb_table
[cb
][cw
].offset
;
415 extra_bits
= hcb_table
[cb
][cw
].extra_bits
;
419 /* we know for sure it's more than hcbN[cb] bits long */
420 if ( flushbits_hcr(ld
, hcbN
[cb
]) ) return -1;
421 offset
+= (uint16_t)showbits_hcr(ld
, extra_bits
);
422 if ( flushbits_hcr(ld
, hcb_2_pair_table
[cb
][offset
].bits
- hcbN
[cb
]) ) return -1;
424 if ( flushbits_hcr(ld
, hcb_2_pair_table
[cb
][offset
].bits
) ) return -1;
426 sp
[0] = hcb_2_pair_table
[cb
][offset
].x
;
427 sp
[1] = hcb_2_pair_table
[cb
][offset
].y
;
430 case 3: /* binary search for data quadruples */
432 while (!hcb3
[offset
].is_leaf
)
436 if ( get1bit_hcr(ld
, &b
) ) return -1;
437 offset
+= hcb3
[offset
].data
[b
];
440 sp
[0] = hcb3
[offset
].data
[0];
441 sp
[1] = hcb3
[offset
].data
[1];
442 sp
[2] = hcb3
[offset
].data
[2];
443 sp
[3] = hcb3
[offset
].data
[3];
447 case 5: /* binary search for data pairs */
451 while (!hcb_bin_table
[cb
][offset
].is_leaf
)
455 if (get1bit_hcr(ld
, &b
) ) return -1;
456 offset
+= hcb_bin_table
[cb
][offset
].data
[b
];
459 sp
[0] = hcb_bin_table
[cb
][offset
].data
[0];
460 sp
[1] = hcb_bin_table
[cb
][offset
].data
[1];
465 /* decode sign bits */
466 if (unsigned_cb
[cb
]) {
468 for(i
= 0; i
< ((cb
< FIRST_PAIR_HCB
) ? QUAD_LEN
: PAIR_LEN
); i
++)
473 if ( get1bit_hcr(ld
, &b
) ) return -1;
481 /* decode huffman escape bits */
482 if ((cb
== ESC_HCB
) || (cb
>= 16))
485 for (k
= 0; k
< 2; k
++)
487 if ((sp
[k
] == 16) || (sp
[k
] == -16))
493 neg
= (sp
[k
] < 0) ? 1 : 0;
498 if (get1bit_hcr(ld
, &b
))
503 // TODO: here we would need to test "off" if VCB11 is used!
504 if (getbits_hcr(ld
, i
, &off
))
507 sp
[k
] = (int16_t)((neg
) ? -j
: j
);