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.
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
);
53 #ifdef ERROR_RESILIENCE
54 static void vcb11_check_LAV(uint8_t cb
, int16_t *sp
);
57 int8_t huffman_scale_factor(bitfile
*ld
)
61 while (hcb_sf
[offset
][1])
63 uint8_t b
= faad_get1bit(ld
64 DEBUGVAR(1,255,"huffman_scale_factor()"));
65 offset
+= hcb_sf
[offset
][b
];
69 /* printf("ERROR: offset into hcb_sf = %d >240!\n", offset); */
74 return hcb_sf
[offset
][0];
79 0, hcb1_1
, hcb2_1
, 0, hcb4_1
, 0, hcb6_1
, 0, hcb8_1
, 0, hcb10_1
, hcb11_1
82 hcb_2_quad
*hcb_2_quad_table
[] = {
83 0, hcb1_2
, hcb2_2
, 0, hcb4_2
, 0, 0, 0, 0, 0, 0, 0
86 hcb_2_pair
*hcb_2_pair_table
[] = {
87 0, 0, 0, 0, 0, 0, hcb6_2
, 0, hcb8_2
, 0, hcb10_2
, hcb11_2
90 hcb_bin_pair
*hcb_bin_table
[] = {
91 0, 0, 0, 0, 0, hcb5
, 0, hcb7
, 0, hcb9
, 0, 0
94 uint8_t hcbN
[] = { 0, 5, 5, 0, 5, 0, 5, 0, 5, 0, 6, 5 };
96 /* defines whether a huffman codebook is unsigned or not */
98 uint8_t unsigned_cb
[] = { 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0,
99 /* codebook 16 to 31 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
102 int hcb_2_quad_table_size
[] = { 0, 114, 86, 0, 185, 0, 0, 0, 0, 0, 0, 0 };
103 int hcb_2_pair_table_size
[] = { 0, 0, 0, 0, 0, 0, 126, 0, 83, 0, 210, 373 };
104 int hcb_bin_table_size
[] = { 0, 0, 0, 161, 0, 161, 0, 127, 0, 337, 0, 0 };
106 static INLINE
void huffman_sign_bits(bitfile
*ld
, int16_t *sp
, uint8_t len
)
110 for (i
= 0; i
< len
; i
++)
115 DEBUGVAR(1,5,"huffman_sign_bits(): sign bit")) & 1)
123 static INLINE
int16_t huffman_getescape(bitfile
*ld
, int16_t sp
)
143 DEBUGVAR(1,6,"huffman_getescape(): escape size")) == 0)
149 off
= (int16_t)faad_getbits(ld
, i
150 DEBUGVAR(1,9,"huffman_getescape(): escape"));
159 static uint8_t huffman_2step_quad(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
165 cw
= faad_showbits(ld
, hcbN
[cb
]);
166 offset
= hcb_table
[cb
][cw
].offset
;
167 extra_bits
= hcb_table
[cb
][cw
].extra_bits
;
171 /* we know for sure it's more than hcbN[cb] bits long */
172 faad_flushbits(ld
, hcbN
[cb
]);
173 offset
+= (uint16_t)faad_showbits(ld
, extra_bits
);
174 faad_flushbits(ld
, hcb_2_quad_table
[cb
][offset
].bits
- hcbN
[cb
]);
176 faad_flushbits(ld
, hcb_2_quad_table
[cb
][offset
].bits
);
179 if (offset
> hcb_2_quad_table_size
[cb
])
181 /* printf("ERROR: offset into hcb_2_quad_table = %d >%d!\n", offset,
182 hcb_2_quad_table_size[cb]); */
186 sp
[0] = hcb_2_quad_table
[cb
][offset
].x
;
187 sp
[1] = hcb_2_quad_table
[cb
][offset
].y
;
188 sp
[2] = hcb_2_quad_table
[cb
][offset
].v
;
189 sp
[3] = hcb_2_quad_table
[cb
][offset
].w
;
194 static uint8_t huffman_2step_quad_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
196 uint8_t err
= huffman_2step_quad(cb
, ld
, sp
);
197 huffman_sign_bits(ld
, sp
, QUAD_LEN
);
202 static uint8_t huffman_2step_pair(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
208 cw
= faad_showbits(ld
, hcbN
[cb
]);
209 offset
= hcb_table
[cb
][cw
].offset
;
210 extra_bits
= hcb_table
[cb
][cw
].extra_bits
;
214 /* we know for sure it's more than hcbN[cb] bits long */
215 faad_flushbits(ld
, hcbN
[cb
]);
216 offset
+= (uint16_t)faad_showbits(ld
, extra_bits
);
217 faad_flushbits(ld
, hcb_2_pair_table
[cb
][offset
].bits
- hcbN
[cb
]);
219 faad_flushbits(ld
, hcb_2_pair_table
[cb
][offset
].bits
);
222 if (offset
> hcb_2_pair_table_size
[cb
])
224 /* printf("ERROR: offset into hcb_2_pair_table = %d >%d!\n", offset,
225 hcb_2_pair_table_size[cb]); */
229 sp
[0] = hcb_2_pair_table
[cb
][offset
].x
;
230 sp
[1] = hcb_2_pair_table
[cb
][offset
].y
;
235 static uint8_t huffman_2step_pair_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
237 uint8_t err
= huffman_2step_pair(cb
, ld
, sp
);
238 huffman_sign_bits(ld
, sp
, PAIR_LEN
);
243 static uint8_t huffman_binary_quad(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
247 while (!hcb3
[offset
].is_leaf
)
249 uint8_t b
= faad_get1bit(ld
250 DEBUGVAR(1,255,"huffman_spectral_data():3"));
251 offset
+= hcb3
[offset
].data
[b
];
254 if (offset
> hcb_bin_table_size
[cb
])
256 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
257 hcb_bin_table_size[cb]); */
261 sp
[0] = hcb3
[offset
].data
[0];
262 sp
[1] = hcb3
[offset
].data
[1];
263 sp
[2] = hcb3
[offset
].data
[2];
264 sp
[3] = hcb3
[offset
].data
[3];
269 static uint8_t huffman_binary_quad_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
271 uint8_t err
= huffman_binary_quad(cb
, ld
, sp
);
272 huffman_sign_bits(ld
, sp
, QUAD_LEN
);
277 static uint8_t huffman_binary_pair(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
281 while (!hcb_bin_table
[cb
][offset
].is_leaf
)
283 uint8_t b
= faad_get1bit(ld
284 DEBUGVAR(1,255,"huffman_spectral_data():9"));
285 offset
+= hcb_bin_table
[cb
][offset
].data
[b
];
288 if (offset
> hcb_bin_table_size
[cb
])
290 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
291 hcb_bin_table_size[cb]); */
295 sp
[0] = hcb_bin_table
[cb
][offset
].data
[0];
296 sp
[1] = hcb_bin_table
[cb
][offset
].data
[1];
301 static uint8_t huffman_binary_pair_sign(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
303 uint8_t err
= huffman_binary_pair(cb
, ld
, sp
);
304 huffman_sign_bits(ld
, sp
, PAIR_LEN
);
309 static int16_t huffman_codebook(uint8_t i
)
311 static const uint32_t data
= 16428320;
312 if (i
== 0) return (int16_t)(data
>> 16) & 0xFFFF;
313 else return (int16_t)data
& 0xFFFF;
316 #ifdef ERROR_RESILIENCE
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
))
338 uint8_t huffman_spectral_data(uint8_t cb
, bitfile
*ld
, int16_t *sp
)
342 case 1: /* 2-step method for data quadruples */
344 return huffman_2step_quad(cb
, ld
, sp
);
345 case 3: /* binary search for data quadruples */
346 return huffman_binary_quad_sign(cb
, ld
, sp
);
347 case 4: /* 2-step method for data quadruples */
348 return huffman_2step_quad_sign(cb
, ld
, sp
);
349 case 5: /* binary search for data pairs */
350 return huffman_binary_pair(cb
, ld
, sp
);
351 case 6: /* 2-step method for data pairs */
352 return huffman_2step_pair(cb
, ld
, sp
);
353 case 7: /* binary search for data pairs */
355 return huffman_binary_pair_sign(cb
, ld
, sp
);
356 case 8: /* 2-step method for data pairs */
358 return huffman_2step_pair_sign(cb
, ld
, sp
);
360 uint8_t err
= huffman_2step_pair(11, ld
, sp
);
361 sp
[0] = huffman_codebook(0); sp
[1] = huffman_codebook(1);
365 uint8_t err
= huffman_2step_pair_sign(11, ld
, sp
);
366 sp
[0] = huffman_getescape(ld
, sp
[0]);
367 sp
[1] = huffman_getescape(ld
, sp
[1]);
370 #ifdef ERROR_RESILIENCE
371 /* VCB11 uses codebook 11 */
372 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
373 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
375 uint8_t err
= huffman_2step_pair_sign(11, ld
, sp
);
376 sp
[0] = huffman_getescape(ld
, sp
[0]);
377 sp
[1] = huffman_getescape(ld
, sp
[1]);
379 /* check LAV (Largest Absolute Value) */
380 /* this finds errors in the ESCAPE signal */
381 vcb11_check_LAV(cb
, sp
);
387 /* Non existent codebook number, something went wrong */
395 #ifdef ERROR_RESILIENCE
397 /* Special version of huffman_spectral_data
398 Will not read from a bitfile but a bits_t structure.
399 Will keep track of the bits decoded and return the number of bits remaining.
400 Do not read more than ld->len, return -1 if codeword would be longer */
402 int8_t huffman_spectral_data_2(uint8_t cb
, bits_t
*ld
, int16_t *sp
)
407 uint8_t i
, vcb11
= 0;
412 case 1: /* 2-step method for data quadruples */
416 cw
= showbits_hcr(ld
, hcbN
[cb
]);
417 offset
= hcb_table
[cb
][cw
].offset
;
418 extra_bits
= hcb_table
[cb
][cw
].extra_bits
;
422 /* we know for sure it's more than hcbN[cb] bits long */
423 if ( flushbits_hcr(ld
, hcbN
[cb
]) ) return -1;
424 offset
+= (uint16_t)showbits_hcr(ld
, extra_bits
);
425 if ( flushbits_hcr(ld
, hcb_2_quad_table
[cb
][offset
].bits
- hcbN
[cb
]) ) return -1;
427 if ( flushbits_hcr(ld
, hcb_2_quad_table
[cb
][offset
].bits
) ) return -1;
430 sp
[0] = hcb_2_quad_table
[cb
][offset
].x
;
431 sp
[1] = hcb_2_quad_table
[cb
][offset
].y
;
432 sp
[2] = hcb_2_quad_table
[cb
][offset
].v
;
433 sp
[3] = hcb_2_quad_table
[cb
][offset
].w
;
436 case 6: /* 2-step method for data pairs */
440 /* VCB11 uses codebook 11 */
441 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
442 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
446 /* store the virtual codebook */
451 cw
= showbits_hcr(ld
, hcbN
[cb
]);
452 offset
= hcb_table
[cb
][cw
].offset
;
453 extra_bits
= hcb_table
[cb
][cw
].extra_bits
;
457 /* we know for sure it's more than hcbN[cb] bits long */
458 if ( flushbits_hcr(ld
, hcbN
[cb
]) ) return -1;
459 offset
+= (uint16_t)showbits_hcr(ld
, extra_bits
);
460 if ( flushbits_hcr(ld
, hcb_2_pair_table
[cb
][offset
].bits
- hcbN
[cb
]) ) return -1;
462 if ( flushbits_hcr(ld
, hcb_2_pair_table
[cb
][offset
].bits
) ) return -1;
464 sp
[0] = hcb_2_pair_table
[cb
][offset
].x
;
465 sp
[1] = hcb_2_pair_table
[cb
][offset
].y
;
468 case 3: /* binary search for data quadruples */
470 while (!hcb3
[offset
].is_leaf
)
474 if ( get1bit_hcr(ld
, &b
) ) return -1;
475 offset
+= hcb3
[offset
].data
[b
];
478 sp
[0] = hcb3
[offset
].data
[0];
479 sp
[1] = hcb3
[offset
].data
[1];
480 sp
[2] = hcb3
[offset
].data
[2];
481 sp
[3] = hcb3
[offset
].data
[3];
485 case 5: /* binary search for data pairs */
489 while (!hcb_bin_table
[cb
][offset
].is_leaf
)
493 if (get1bit_hcr(ld
, &b
) ) return -1;
494 offset
+= hcb_bin_table
[cb
][offset
].data
[b
];
497 sp
[0] = hcb_bin_table
[cb
][offset
].data
[0];
498 sp
[1] = hcb_bin_table
[cb
][offset
].data
[1];
503 /* decode sign bits */
506 for(i
= 0; i
< ((cb
< FIRST_PAIR_HCB
) ? QUAD_LEN
: PAIR_LEN
); i
++)
511 if ( get1bit_hcr(ld
, &b
) ) return -1;
519 /* decode huffman escape bits */
520 if ((cb
== ESC_HCB
) || (cb
>= 16))
523 for (k
= 0; k
< 2; k
++)
525 if ((sp
[k
] == 16) || (sp
[k
] == -16))
531 neg
= (sp
[k
] < 0) ? 1 : 0;
536 if (get1bit_hcr(ld
, &b
))
542 if (getbits_hcr(ld
, i
, &off
))
545 sp
[k
] = (int16_t)((neg
) ? -j
: j
);
551 /* check LAV (Largest Absolute Value) */
552 /* this finds errors in the ESCAPE signal */
553 vcb11_check_LAV(vcb11
, sp
);