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