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