Mercurial > mplayer.hg
annotate libfaad2/huffman.c @ 14410:9b7f3566b603
Playback video with multiple windows.
author | reimar |
---|---|
date | Fri, 07 Jan 2005 11:11:02 +0000 |
parents | 6d50ef45a058 |
children | 2ae5ab4331ca |
rev | line source |
---|---|
10989 | 1 /* |
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding | |
12527 | 3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com |
10989 | 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 ** | |
12625
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
25 ** Initially modified for use with MPlayer by Alex Beregszaszi on 2003/10/03 |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
26 ** $Id: huffman.c,v 1.3 2004/06/23 13:50:50 diego Exp $ |
12625
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ |
10989 | 28 **/ |
29 | |
30 #include "common.h" | |
31 #include "structs.h" | |
32 | |
33 #include <stdlib.h> | |
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 | |
12527 | 43 /* static function declarations */ |
44 static INLINE void huffman_sign_bits(bitfile *ld, int16_t *sp, uint8_t len); | |
45 static INLINE int16_t huffman_getescape(bitfile *ld, int16_t sp); | |
46 static uint8_t huffman_2step_quad(uint8_t cb, bitfile *ld, int16_t *sp); | |
47 static uint8_t huffman_2step_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp); | |
48 static uint8_t huffman_2step_pair(uint8_t cb, bitfile *ld, int16_t *sp); | |
49 static uint8_t huffman_2step_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp); | |
50 static uint8_t huffman_binary_quad(uint8_t cb, bitfile *ld, int16_t *sp); | |
51 static uint8_t huffman_binary_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp); | |
52 static uint8_t huffman_binary_pair(uint8_t cb, bitfile *ld, int16_t *sp); | |
53 static uint8_t huffman_binary_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp); | |
54 static int16_t huffman_codebook(uint8_t i); | |
55 | |
10989 | 56 int8_t huffman_scale_factor(bitfile *ld) |
57 { | |
58 uint16_t offset = 0; | |
59 | |
60 while (hcb_sf[offset][1]) | |
61 { | |
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 { | |
68 /* printf("ERROR: offset into hcb_sf = %d >240!\n", offset); */ | |
69 return -1; | |
70 } | |
71 } | |
72 | |
73 return hcb_sf[offset][0]; | |
74 } | |
75 | |
76 | |
77 hcb *hcb_table[] = { | |
78 0, hcb1_1, hcb2_1, 0, hcb4_1, 0, hcb6_1, 0, hcb8_1, 0, hcb10_1, hcb11_1 | |
79 }; | |
80 | |
81 hcb_2_quad *hcb_2_quad_table[] = { | |
82 0, hcb1_2, hcb2_2, 0, hcb4_2, 0, 0, 0, 0, 0, 0, 0 | |
83 }; | |
84 | |
85 hcb_2_pair *hcb_2_pair_table[] = { | |
86 0, 0, 0, 0, 0, 0, hcb6_2, 0, hcb8_2, 0, hcb10_2, hcb11_2 | |
87 }; | |
88 | |
89 hcb_bin_pair *hcb_bin_table[] = { | |
90 0, 0, 0, 0, 0, hcb5, 0, hcb7, 0, hcb9, 0, 0 | |
91 }; | |
92 | |
93 uint8_t hcbN[] = { 0, 5, 5, 0, 5, 0, 5, 0, 5, 0, 6, 5 }; | |
94 | |
95 /* defines whether a huffman codebook is unsigned or not */ | |
96 /* Table 4.6.2 */ | |
97 uint8_t unsigned_cb[] = { 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, | |
98 /* codebook 16 to 31 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 | |
99 }; | |
100 | |
101 int hcb_2_quad_table_size[] = { 0, 114, 86, 0, 185, 0, 0, 0, 0, 0, 0, 0 }; | |
102 int hcb_2_pair_table_size[] = { 0, 0, 0, 0, 0, 0, 126, 0, 83, 0, 210, 373 }; | |
103 int hcb_bin_table_size[] = { 0, 0, 0, 161, 0, 161, 0, 127, 0, 337, 0, 0 }; | |
104 | |
105 static INLINE void huffman_sign_bits(bitfile *ld, int16_t *sp, uint8_t len) | |
106 { | |
107 uint8_t i; | |
108 | |
109 for (i = 0; i < len; i++) | |
110 { | |
111 if(sp[i]) | |
112 { | |
113 if(faad_get1bit(ld | |
114 DEBUGVAR(1,5,"huffman_sign_bits(): sign bit")) & 1) | |
115 { | |
116 sp[i] = -sp[i]; | |
117 } | |
118 } | |
119 } | |
120 } | |
121 | |
122 static INLINE int16_t huffman_getescape(bitfile *ld, int16_t sp) | |
123 { | |
124 uint8_t neg, i; | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
125 int16_t j; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
126 int16_t off; |
10989 | 127 |
128 if (sp < 0) | |
129 { | |
130 if (sp != -16) | |
131 return sp; | |
132 neg = 1; | |
133 } else { | |
12527 | 134 if (sp != 16) |
10989 | 135 return sp; |
136 neg = 0; | |
137 } | |
138 | |
139 for (i = 4; ; i++) | |
140 { | |
141 if (faad_get1bit(ld | |
142 DEBUGVAR(1,6,"huffman_getescape(): escape size")) == 0) | |
143 { | |
144 break; | |
145 } | |
146 } | |
147 | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
148 off = (int16_t)faad_getbits(ld, i |
10989 | 149 DEBUGVAR(1,9,"huffman_getescape(): escape")); |
150 | |
12527 | 151 j = off | (1<<i); |
10989 | 152 if (neg) |
153 j = -j; | |
154 | |
155 return j; | |
156 } | |
157 | |
158 static uint8_t huffman_2step_quad(uint8_t cb, bitfile *ld, int16_t *sp) | |
159 { | |
160 uint32_t cw; | |
161 uint16_t offset = 0; | |
162 uint8_t extra_bits; | |
163 | |
164 cw = faad_showbits(ld, hcbN[cb]); | |
165 offset = hcb_table[cb][cw].offset; | |
166 extra_bits = hcb_table[cb][cw].extra_bits; | |
167 | |
168 if (extra_bits) | |
169 { | |
170 /* we know for sure it's more than hcbN[cb] bits long */ | |
171 faad_flushbits(ld, hcbN[cb]); | |
172 offset += (uint16_t)faad_showbits(ld, extra_bits); | |
173 faad_flushbits(ld, hcb_2_quad_table[cb][offset].bits - hcbN[cb]); | |
174 } else { | |
175 faad_flushbits(ld, hcb_2_quad_table[cb][offset].bits); | |
176 } | |
177 | |
178 if (offset > hcb_2_quad_table_size[cb]) | |
179 { | |
180 /* printf("ERROR: offset into hcb_2_quad_table = %d >%d!\n", offset, | |
181 hcb_2_quad_table_size[cb]); */ | |
182 return 10; | |
183 } | |
184 | |
185 sp[0] = hcb_2_quad_table[cb][offset].x; | |
186 sp[1] = hcb_2_quad_table[cb][offset].y; | |
187 sp[2] = hcb_2_quad_table[cb][offset].v; | |
188 sp[3] = hcb_2_quad_table[cb][offset].w; | |
189 | |
190 return 0; | |
191 } | |
192 | |
193 static uint8_t huffman_2step_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp) | |
194 { | |
195 uint8_t err = huffman_2step_quad(cb, ld, sp); | |
196 huffman_sign_bits(ld, sp, QUAD_LEN); | |
197 | |
198 return err; | |
199 } | |
200 | |
201 static uint8_t huffman_2step_pair(uint8_t cb, bitfile *ld, int16_t *sp) | |
202 { | |
203 uint32_t cw; | |
204 uint16_t offset = 0; | |
205 uint8_t extra_bits; | |
206 | |
207 cw = faad_showbits(ld, hcbN[cb]); | |
208 offset = hcb_table[cb][cw].offset; | |
209 extra_bits = hcb_table[cb][cw].extra_bits; | |
210 | |
211 if (extra_bits) | |
212 { | |
213 /* we know for sure it's more than hcbN[cb] bits long */ | |
214 faad_flushbits(ld, hcbN[cb]); | |
215 offset += (uint16_t)faad_showbits(ld, extra_bits); | |
216 faad_flushbits(ld, hcb_2_pair_table[cb][offset].bits - hcbN[cb]); | |
217 } else { | |
218 faad_flushbits(ld, hcb_2_pair_table[cb][offset].bits); | |
219 } | |
220 | |
221 if (offset > hcb_2_pair_table_size[cb]) | |
222 { | |
223 /* printf("ERROR: offset into hcb_2_pair_table = %d >%d!\n", offset, | |
224 hcb_2_pair_table_size[cb]); */ | |
225 return 10; | |
226 } | |
227 | |
228 sp[0] = hcb_2_pair_table[cb][offset].x; | |
229 sp[1] = hcb_2_pair_table[cb][offset].y; | |
230 | |
231 return 0; | |
232 } | |
233 | |
12527 | 234 static uint8_t huffman_2step_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp) |
10989 | 235 { |
236 uint8_t err = huffman_2step_pair(cb, ld, sp); | |
237 huffman_sign_bits(ld, sp, PAIR_LEN); | |
238 | |
239 return err; | |
240 } | |
241 | |
242 static uint8_t huffman_binary_quad(uint8_t cb, bitfile *ld, int16_t *sp) | |
243 { | |
244 uint16_t offset = 0; | |
245 | |
246 while (!hcb3[offset].is_leaf) | |
247 { | |
248 uint8_t b = faad_get1bit(ld | |
249 DEBUGVAR(1,255,"huffman_spectral_data():3")); | |
250 offset += hcb3[offset].data[b]; | |
251 } | |
252 | |
253 if (offset > hcb_bin_table_size[cb]) | |
254 { | |
255 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset, | |
256 hcb_bin_table_size[cb]); */ | |
257 return 10; | |
258 } | |
259 | |
260 sp[0] = hcb3[offset].data[0]; | |
261 sp[1] = hcb3[offset].data[1]; | |
262 sp[2] = hcb3[offset].data[2]; | |
263 sp[3] = hcb3[offset].data[3]; | |
264 | |
265 return 0; | |
266 } | |
267 | |
268 static uint8_t huffman_binary_quad_sign(uint8_t cb, bitfile *ld, int16_t *sp) | |
269 { | |
270 uint8_t err = huffman_binary_quad(cb, ld, sp); | |
271 huffman_sign_bits(ld, sp, QUAD_LEN); | |
272 | |
273 return err; | |
274 } | |
275 | |
276 static uint8_t huffman_binary_pair(uint8_t cb, bitfile *ld, int16_t *sp) | |
277 { | |
278 uint16_t offset = 0; | |
279 | |
280 while (!hcb_bin_table[cb][offset].is_leaf) | |
281 { | |
282 uint8_t b = faad_get1bit(ld | |
283 DEBUGVAR(1,255,"huffman_spectral_data():9")); | |
284 offset += hcb_bin_table[cb][offset].data[b]; | |
285 } | |
286 | |
287 if (offset > hcb_bin_table_size[cb]) | |
288 { | |
289 /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset, | |
290 hcb_bin_table_size[cb]); */ | |
291 return 10; | |
292 } | |
293 | |
294 sp[0] = hcb_bin_table[cb][offset].data[0]; | |
295 sp[1] = hcb_bin_table[cb][offset].data[1]; | |
296 | |
297 return 0; | |
298 } | |
299 | |
300 static uint8_t huffman_binary_pair_sign(uint8_t cb, bitfile *ld, int16_t *sp) | |
301 { | |
302 uint8_t err = huffman_binary_pair(cb, ld, sp); | |
303 huffman_sign_bits(ld, sp, PAIR_LEN); | |
304 | |
305 return err; | |
306 } | |
307 | |
308 static int16_t huffman_codebook(uint8_t i) | |
309 { | |
310 static const uint32_t data = 16428320; | |
311 if (i == 0) return (int16_t)(data >> 16) & 0xFFFF; | |
312 else return (int16_t)data & 0xFFFF; | |
313 } | |
314 | |
315 uint8_t huffman_spectral_data(uint8_t cb, bitfile *ld, int16_t *sp) | |
316 { | |
317 switch (cb) | |
318 { | |
319 case 1: /* 2-step method for data quadruples */ | |
320 case 2: | |
321 return huffman_2step_quad(cb, ld, sp); | |
322 case 3: /* binary search for data quadruples */ | |
323 return huffman_binary_quad_sign(cb, ld, sp); | |
324 case 4: /* 2-step method for data quadruples */ | |
325 return huffman_2step_quad_sign(cb, ld, sp); | |
326 case 5: /* binary search for data pairs */ | |
327 return huffman_binary_pair(cb, ld, sp); | |
328 case 6: /* 2-step method for data pairs */ | |
329 return huffman_2step_pair(cb, ld, sp); | |
330 case 7: /* binary search for data pairs */ | |
331 case 9: | |
332 return huffman_binary_pair_sign(cb, ld, sp); | |
333 case 8: /* 2-step method for data pairs */ | |
334 case 10: | |
335 return huffman_2step_pair_sign(cb, ld, sp); | |
336 case 12: { | |
12527 | 337 uint8_t err = huffman_2step_pair(11, ld, sp); |
10989 | 338 sp[0] = huffman_codebook(0); sp[1] = huffman_codebook(1); |
339 return err; } | |
340 case 11: | |
341 #ifdef ERROR_RESILIENCE | |
342 /* VCB11 uses codebook 11 */ | |
343 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: | |
344 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31: | |
345 /* TODO: If ER is used, some extra error checking should be done */ | |
346 #endif | |
347 { | |
348 uint8_t err = huffman_2step_pair_sign(11, ld, sp); | |
349 sp[0] = huffman_getescape(ld, sp[0]); | |
350 sp[1] = huffman_getescape(ld, sp[1]); | |
351 return err; | |
352 } | |
353 default: | |
354 /* Non existent codebook number, something went wrong */ | |
355 return 11; | |
356 } | |
357 | |
358 return 0; | |
359 } | |
360 | |
361 | |
362 #ifdef ERROR_RESILIENCE | |
363 | |
364 /* Special version of huffman_spectral_data | |
365 Will not read from a bitfile but a bits_t structure. | |
366 Will keep track of the bits decoded and return the number of bits remaining. | |
367 Do not read more than ld->len, return -1 if codeword would be longer */ | |
368 | |
369 int8_t huffman_spectral_data_2(uint8_t cb, bits_t *ld, int16_t *sp) | |
370 { | |
371 uint32_t cw; | |
372 uint16_t offset = 0; | |
373 uint8_t extra_bits; | |
374 uint8_t i; | |
375 | |
376 | |
377 switch (cb) | |
378 { | |
379 case 1: /* 2-step method for data quadruples */ | |
380 case 2: | |
381 case 4: | |
382 | |
383 cw = showbits_hcr(ld, hcbN[cb]); | |
384 offset = hcb_table[cb][cw].offset; | |
385 extra_bits = hcb_table[cb][cw].extra_bits; | |
386 | |
387 if (extra_bits) | |
388 { | |
389 /* we know for sure it's more than hcbN[cb] bits long */ | |
390 if ( flushbits_hcr(ld, hcbN[cb]) ) return -1; | |
391 offset += (uint16_t)showbits_hcr(ld, extra_bits); | |
392 if ( flushbits_hcr(ld, hcb_2_quad_table[cb][offset].bits - hcbN[cb]) ) return -1; | |
393 } else { | |
394 if ( flushbits_hcr(ld, hcb_2_quad_table[cb][offset].bits) ) return -1; | |
395 } | |
396 | |
397 sp[0] = hcb_2_quad_table[cb][offset].x; | |
398 sp[1] = hcb_2_quad_table[cb][offset].y; | |
399 sp[2] = hcb_2_quad_table[cb][offset].v; | |
400 sp[3] = hcb_2_quad_table[cb][offset].w; | |
401 break; | |
402 | |
403 case 6: /* 2-step method for data pairs */ | |
404 case 8: | |
405 case 10: | |
406 case 11: | |
407 /* VCB11 uses codebook 11 */ | |
408 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: | |
409 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31: | |
410 | |
411 /* TODO: If ER is used, some extra error checking should be done */ | |
412 if (cb >= 16) | |
413 cb = 11; | |
414 | |
415 cw = showbits_hcr(ld, hcbN[cb]); | |
416 offset = hcb_table[cb][cw].offset; | |
417 extra_bits = hcb_table[cb][cw].extra_bits; | |
418 | |
419 if (extra_bits) | |
420 { | |
421 /* we know for sure it's more than hcbN[cb] bits long */ | |
422 if ( flushbits_hcr(ld, hcbN[cb]) ) return -1; | |
423 offset += (uint16_t)showbits_hcr(ld, extra_bits); | |
424 if ( flushbits_hcr(ld, hcb_2_pair_table[cb][offset].bits - hcbN[cb]) ) return -1; | |
425 } else { | |
426 if ( flushbits_hcr(ld, hcb_2_pair_table[cb][offset].bits) ) return -1; | |
427 } | |
428 sp[0] = hcb_2_pair_table[cb][offset].x; | |
429 sp[1] = hcb_2_pair_table[cb][offset].y; | |
430 break; | |
431 | |
432 case 3: /* binary search for data quadruples */ | |
433 | |
434 while (!hcb3[offset].is_leaf) | |
435 { | |
436 uint8_t b; | |
437 | |
438 if ( get1bit_hcr(ld, &b) ) return -1; | |
439 offset += hcb3[offset].data[b]; | |
440 } | |
441 | |
442 sp[0] = hcb3[offset].data[0]; | |
443 sp[1] = hcb3[offset].data[1]; | |
444 sp[2] = hcb3[offset].data[2]; | |
445 sp[3] = hcb3[offset].data[3]; | |
446 | |
447 break; | |
448 | |
449 case 5: /* binary search for data pairs */ | |
450 case 7: | |
451 case 9: | |
452 | |
453 while (!hcb_bin_table[cb][offset].is_leaf) | |
454 { | |
455 uint8_t b; | |
456 | |
457 if (get1bit_hcr(ld, &b) ) return -1; | |
458 offset += hcb_bin_table[cb][offset].data[b]; | |
459 } | |
460 | |
461 sp[0] = hcb_bin_table[cb][offset].data[0]; | |
462 sp[1] = hcb_bin_table[cb][offset].data[1]; | |
463 | |
464 break; | |
465 } | |
466 | |
467 /* decode sign bits */ | |
468 if (unsigned_cb[cb]) { | |
469 | |
470 for(i = 0; i < ((cb < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN); i++) | |
471 { | |
472 if(sp[i]) | |
473 { | |
474 uint8_t b; | |
475 if ( get1bit_hcr(ld, &b) ) return -1; | |
476 if (b != 0) { | |
477 sp[i] = -sp[i]; | |
478 } | |
479 } | |
480 } | |
481 } | |
482 | |
483 /* decode huffman escape bits */ | |
484 if ((cb == ESC_HCB) || (cb >= 16)) | |
485 { | |
486 uint8_t k; | |
487 for (k = 0; k < 2; k++) | |
488 { | |
489 if ((sp[k] == 16) || (sp[k] == -16)) | |
490 { | |
491 uint8_t neg, i; | |
492 int32_t j; | |
493 uint32_t off; | |
494 | |
495 neg = (sp[k] < 0) ? 1 : 0; | |
496 | |
497 for (i = 4; ; i++) | |
498 { | |
499 uint8_t b; | |
500 if (get1bit_hcr(ld, &b)) | |
501 return -1; | |
502 if (b == 0) | |
503 break; | |
504 } | |
505 // TODO: here we would need to test "off" if VCB11 is used! | |
506 if (getbits_hcr(ld, i, &off)) | |
507 return -1; | |
508 j = off + (1<<i); | |
509 sp[k] = (int16_t)((neg) ? -j : j); | |
510 } | |
511 } | |
512 } | |
513 return ld->len; | |
514 } | |
515 | |
516 #endif | |
517 |