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