annotate libfaad2/huffman.h @ 10836:93e7f28d836c

100l typo found by Dima K. <dimakar@yahoo.com>
author alex
date Sun, 07 Sep 2003 17:49:56 +0000
parents e989150f8216
children 3185f64f6350
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10725
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
1 /*
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
4 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
5 ** This program is free software; you can redistribute it and/or modify
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
6 ** it under the terms of the GNU General Public License as published by
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
7 ** the Free Software Foundation; either version 2 of the License, or
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
8 ** (at your option) any later version.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
9 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
10 ** This program is distributed in the hope that it will be useful,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
13 ** GNU General Public License for more details.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
14 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
15 ** You should have received a copy of the GNU General Public License
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
16 ** along with this program; if not, write to the Free Software
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
18 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
19 ** Any non-GPL usage of this software or parts of this software is strictly
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
20 ** forbidden.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
21 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
22 ** Commercial non-GPL licensing of this software is possible.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
24 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
25 ** $Id: huffman.h,v 1.13 2003/07/29 08:20:12 menno Exp $
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
26 **/
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
27
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
28 #ifndef __HUFFMAN_H__
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
29 #define __HUFFMAN_H__
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
30
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
31 #ifdef __cplusplus
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
32 extern "C" {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
33 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
34
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
35 #include <stdlib.h>
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
36 #ifdef ANALYSIS
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
37 #include <stdio.h>
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
38 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
39 #include "bits.h"
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
40 #include "codebook/hcb.h"
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
41
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
42
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
43 static INLINE int8_t huffman_scale_factor(bitfile *ld)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
44 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
45 uint16_t offset = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
46
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
47 while (hcb_sf[offset][1])
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
48 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
49 uint8_t b = faad_get1bit(ld
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
50 DEBUGVAR(1,255,"huffman_scale_factor()"));
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
51 offset += hcb_sf[offset][b];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
52
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
53 if (offset > 240)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
54 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
55 /* printf("ERROR: offset into hcb_sf = %d >240!\n", offset); */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
56 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
57 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
58 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
59
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
60 return hcb_sf[offset][0];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
61 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
62
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
63
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
64 hcb *hcb_table[] = {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
65 0, hcb1_1, hcb2_1, 0, hcb4_1, 0, hcb6_1, 0, hcb8_1, 0, hcb10_1, hcb11_1
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
66 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
67
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
68 hcb_2_quad *hcb_2_quad_table[] = {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
69 0, hcb1_2, hcb2_2, 0, hcb4_2, 0, 0, 0, 0, 0, 0, 0
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
70 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
71
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
72 hcb_2_pair *hcb_2_pair_table[] = {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
73 0, 0, 0, 0, 0, 0, hcb6_2, 0, hcb8_2, 0, hcb10_2, hcb11_2
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
74 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
75
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
76 hcb_bin_pair *hcb_bin_table[] = {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
77 0, 0, 0, 0, 0, hcb5, 0, hcb7, 0, hcb9, 0, 0
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
78 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
79
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
80 uint8_t hcbN[] = { 0, 5, 5, 0, 5, 0, 5, 0, 5, 0, 6, 5 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
81
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
82 /* defines whether a huffman codebook is unsigned or not */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
83 /* Table 4.6.2 */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
84 uint8_t unsigned_cb[] = { 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
85 /* codebook 16 to 31 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
86 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
87
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
88 int hcb_2_quad_table_size[] = { 0, 114, 86, 0, 185, 0, 0, 0, 0, 0, 0, 0 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
89 int hcb_2_pair_table_size[] = { 0, 0, 0, 0, 0, 0, 126, 0, 83, 0, 210, 373 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
90 int hcb_bin_table_size[] = { 0, 0, 0, 161, 0, 161, 0, 127, 0, 337, 0, 0 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
91
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
92 static INLINE void huffman_sign_bits(bitfile *ld, int16_t *sp, uint8_t len)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
93 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
94 uint8_t i;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
95
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
96 for(i = 0; i < len; i++)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
97 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
98 if(sp[i])
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
99 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
100 if(faad_get1bit(ld
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
101 DEBUGVAR(1,5,"huffman_sign_bits(): sign bit")) & 1)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
102 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
103 sp[i] = -sp[i];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
104 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
105 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
106 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
107 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
108
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
109 static INLINE int16_t huffman_getescape(bitfile *ld, int16_t sp)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
110 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
111 uint8_t neg, i;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
112 int16_t j;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
113 int32_t off;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
114
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
115 if (sp < 0) {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
116 if(sp != -16)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
117 return sp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
118 neg = 1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
119 } else {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
120 if(sp != 16)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
121 return sp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
122 neg = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
123 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
124
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
125 for (i = 4; ; i++)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
126 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
127 if (faad_get1bit(ld
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
128 DEBUGVAR(1,6,"huffman_getescape(): escape size")) == 0)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
129 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
130 break;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
131 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
132 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
133
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
134 off = faad_getbits(ld, i
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
135 DEBUGVAR(1,9,"huffman_getescape(): escape"));
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
136
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
137 j = off + (1<<i);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
138 if (neg)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
139 j = -j;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
140
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
141 return j;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
142 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
143
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
144 static uint8_t huffman_2step_quad(uint8_t cb, bitfile *ld, int16_t *sp)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
145 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
146 uint32_t cw;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
147 uint16_t offset = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
148 uint8_t extra_bits;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
149
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
150 cw = faad_showbits(ld, hcbN[cb]);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
151 offset = hcb_table[cb][cw].offset;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
152 extra_bits = hcb_table[cb][cw].extra_bits;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
153
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
154 if (extra_bits)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
155 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
156 /* we know for sure it's more than hcbN[cb] bits long */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
157 faad_flushbits(ld, hcbN[cb]);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
158 offset += (uint16_t)faad_showbits(ld, extra_bits);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
159 faad_flushbits(ld, hcb_2_quad_table[cb][offset].bits - hcbN[cb]);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
160 } else {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
161 faad_flushbits(ld, hcb_2_quad_table[cb][offset].bits);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
162 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
163
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
164 if (offset > hcb_2_quad_table_size[cb])
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
165 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
166 /* printf("ERROR: offset into hcb_2_quad_table = %d >%d!\n", offset,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
167 hcb_2_quad_table_size[cb]); */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
168 return 10;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
169 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
170
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
171 sp[0] = hcb_2_quad_table[cb][offset].x;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
172 sp[1] = hcb_2_quad_table[cb][offset].y;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
173 sp[2] = hcb_2_quad_table[cb][offset].v;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
174 sp[3] = hcb_2_quad_table[cb][offset].w;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
175
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
176 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
177 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
178
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
179 static uint8_t huffman_2step_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
180 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
181 uint8_t err = huffman_2step_quad(cb, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
182 huffman_sign_bits(ld, sp, QUAD_LEN);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
183
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
184 return err;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
185 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
186
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
187 static uint8_t huffman_2step_pair(uint8_t cb, bitfile *ld, int16_t *sp)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
188 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
189 uint32_t cw;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
190 uint16_t offset = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
191 uint8_t extra_bits;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
192
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
193 cw = faad_showbits(ld, hcbN[cb]);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
194 offset = hcb_table[cb][cw].offset;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
195 extra_bits = hcb_table[cb][cw].extra_bits;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
196
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
197 if (extra_bits)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
198 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
199 /* we know for sure it's more than hcbN[cb] bits long */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
200 faad_flushbits(ld, hcbN[cb]);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
201 offset += (uint16_t)faad_showbits(ld, extra_bits);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
202 faad_flushbits(ld, hcb_2_pair_table[cb][offset].bits - hcbN[cb]);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
203 } else {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
204 faad_flushbits(ld, hcb_2_pair_table[cb][offset].bits);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
205 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
206
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
207 if (offset > hcb_2_pair_table_size[cb])
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
208 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
209 /* printf("ERROR: offset into hcb_2_pair_table = %d >%d!\n", offset,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
210 hcb_2_pair_table_size[cb]); */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
211 return 10;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
212 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
213
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
214 sp[0] = hcb_2_pair_table[cb][offset].x;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
215 sp[1] = hcb_2_pair_table[cb][offset].y;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
216
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
217 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
218 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
219
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
220 static huffman_2step_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
221 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
222 uint8_t err = huffman_2step_pair(cb, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
223 huffman_sign_bits(ld, sp, PAIR_LEN);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
224
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
225 return err;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
226 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
227
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
228 static uint8_t huffman_binary_quad(uint8_t cb, bitfile *ld, int16_t *sp)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
229 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
230 uint16_t offset = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
231
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
232 while (!hcb3[offset].is_leaf)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
233 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
234 uint8_t b = faad_get1bit(ld
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
235 DEBUGVAR(1,255,"huffman_spectral_data():3"));
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
236 offset += hcb3[offset].data[b];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
237 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
238
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
239 if (offset > hcb_bin_table_size[cb])
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
240 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
241 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
242 hcb_bin_table_size[cb]); */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
243 return 10;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
244 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
245
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
246 sp[0] = hcb3[offset].data[0];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
247 sp[1] = hcb3[offset].data[1];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
248 sp[2] = hcb3[offset].data[2];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
249 sp[3] = hcb3[offset].data[3];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
250
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
251 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
252 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
253
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
254 static uint8_t huffman_binary_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
255 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
256 uint8_t err = huffman_binary_quad(cb, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
257 huffman_sign_bits(ld, sp, QUAD_LEN);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
258
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
259 return err;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
260 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
261
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
262 static uint8_t huffman_binary_pair(uint8_t cb, bitfile *ld, int16_t *sp)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
263 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
264 uint16_t offset = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
265
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
266 while (!hcb_bin_table[cb][offset].is_leaf)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
267 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
268 uint8_t b = faad_get1bit(ld
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
269 DEBUGVAR(1,255,"huffman_spectral_data():9"));
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
270 offset += hcb_bin_table[cb][offset].data[b];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
271 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
272
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
273 if (offset > hcb_bin_table_size[cb])
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
274 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
275 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
276 hcb_bin_table_size[cb]); */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
277 return 10;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
278 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
279
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
280 sp[0] = hcb_bin_table[cb][offset].data[0];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
281 sp[1] = hcb_bin_table[cb][offset].data[1];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
282
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
283 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
284 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
285
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
286 static uint8_t huffman_binary_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
287 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
288 uint8_t err = huffman_binary_pair(cb, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
289 huffman_sign_bits(ld, sp, PAIR_LEN);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
290
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
291 return err;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
292 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
293
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
294 static int16_t huffman_codebook(uint8_t i)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
295 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
296 static const uint32_t data = 16428320;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
297 if (i == 0) return (int16_t)(data >> 16) & 0xFFFF;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
298 else return (int16_t)data & 0xFFFF;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
299 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
300
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
301 static INLINE uint8_t huffman_spectral_data(uint8_t cb, bitfile *ld, int16_t *sp)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
302 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
303 switch (cb)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
304 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
305 case 1: /* 2-step method for data quadruples */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
306 case 2:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
307 return huffman_2step_quad(cb, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
308 case 3: /* binary search for data quadruples */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
309 return huffman_binary_quad_sign(cb, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
310 case 4: /* 2-step method for data quadruples */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
311 return huffman_2step_quad_sign(cb, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
312 case 5: /* binary search for data pairs */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
313 return huffman_binary_pair(cb, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
314 case 6: /* 2-step method for data pairs */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
315 return huffman_2step_pair(cb, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
316 case 7: /* binary search for data pairs */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
317 case 9:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
318 return huffman_binary_pair_sign(cb, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
319 case 8: /* 2-step method for data pairs */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
320 case 10:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
321 return huffman_2step_pair_sign(cb, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
322 case 12: {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
323 uint8_t err = huffman_2step_quad(1, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
324 sp[0] = huffman_codebook(0); sp[1] = huffman_codebook(1);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
325 return err; }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
326 case 11:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
327 #ifdef ERROR_RESILIENCE
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
328 /* VCB11 uses codebook 11 */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
329 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
330 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
331 /* TODO: If ER is used, some extra error checking should be done */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
332 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
333 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
334 uint8_t err = huffman_2step_pair_sign(11, ld, sp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
335 sp[0] = huffman_getescape(ld, sp[0]);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
336 sp[1] = huffman_getescape(ld, sp[1]);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
337 return err;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
338 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
339 default:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
340 /* Non existent codebook number, something went wrong */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
341 return 11;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
342 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
343
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
344 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
345 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
346
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
347 #ifdef __cplusplus
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
348 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
349 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
350 #endif