Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * Common bit i/o utils | |
3 | * Copyright (c) 2000, 2001 Gerard Lantau. | |
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., 675 Mass Ave, Cambridge, MA 02139, USA. | |
27a3e2c5 MN |
18 | * |
19 | * alternative bitstream reader by Michael Niedermayer <michaelni@gmx.at> | |
de6d9b64 | 20 | */ |
de6d9b64 | 21 | #include "common.h" |
1a565432 | 22 | #include <math.h> |
a9b3f630 | 23 | |
de6d9b64 FB |
24 | void init_put_bits(PutBitContext *s, |
25 | UINT8 *buffer, int buffer_size, | |
26 | void *opaque, | |
27 | void (*write_data)(void *, UINT8 *, int)) | |
28 | { | |
29 | s->buf = buffer; | |
30 | s->buf_ptr = s->buf; | |
31 | s->buf_end = s->buf + buffer_size; | |
32 | s->bit_cnt=0; | |
33 | s->bit_buf=0; | |
34 | s->data_out_size = 0; | |
35 | s->write_data = write_data; | |
36 | s->opaque = opaque; | |
37 | } | |
38 | ||
39 | static void flush_buffer(PutBitContext *s) | |
40 | { | |
41 | int size; | |
42 | if (s->write_data) { | |
43 | size = s->buf_ptr - s->buf; | |
44 | if (size > 0) | |
45 | s->write_data(s->opaque, s->buf, size); | |
46 | s->buf_ptr = s->buf; | |
47 | s->data_out_size += size; | |
48 | } | |
49 | } | |
50 | ||
51 | void put_bits(PutBitContext *s, int n, unsigned int value) | |
52 | { | |
53 | unsigned int bit_buf; | |
54 | int bit_cnt; | |
55 | ||
56 | #ifdef STATS | |
57 | st_out_bit_counts[st_current_index] += n; | |
58 | #endif | |
59 | // printf("put_bits=%d %x\n", n, value); | |
60 | assert(n == 32 || value < (1U << n)); | |
61 | ||
62 | bit_buf = s->bit_buf; | |
63 | bit_cnt = s->bit_cnt; | |
64 | ||
65 | // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); | |
66 | /* XXX: optimize */ | |
67 | if (n < (32-bit_cnt)) { | |
68 | bit_buf |= value << (32 - n - bit_cnt); | |
69 | bit_cnt+=n; | |
70 | } else { | |
71 | bit_buf |= value >> (n + bit_cnt - 32); | |
1a565432 | 72 | *(UINT32 *)s->buf_ptr = be2me_32(bit_buf); |
de6d9b64 FB |
73 | //printf("bitbuf = %08x\n", bit_buf); |
74 | s->buf_ptr+=4; | |
75 | if (s->buf_ptr >= s->buf_end) | |
76 | flush_buffer(s); | |
77 | bit_cnt=bit_cnt + n - 32; | |
78 | if (bit_cnt == 0) { | |
79 | bit_buf = 0; | |
80 | } else { | |
81 | bit_buf = value << (32 - bit_cnt); | |
82 | } | |
83 | } | |
84 | ||
85 | s->bit_buf = bit_buf; | |
86 | s->bit_cnt = bit_cnt; | |
87 | } | |
88 | ||
89 | /* return the number of bits output */ | |
1a565432 | 90 | INT64 get_bit_count(PutBitContext *s) |
de6d9b64 | 91 | { |
1a565432 | 92 | return (s->buf_ptr - s->buf + s->data_out_size) * 8 + (INT64)s->bit_cnt; |
de6d9b64 FB |
93 | } |
94 | ||
95 | void align_put_bits(PutBitContext *s) | |
96 | { | |
97 | put_bits(s,(8 - s->bit_cnt) & 7,0); | |
98 | } | |
99 | ||
100 | /* pad the end of the output stream with zeros */ | |
101 | void flush_put_bits(PutBitContext *s) | |
102 | { | |
103 | while (s->bit_cnt > 0) { | |
104 | /* XXX: should test end of buffer */ | |
105 | *s->buf_ptr++=s->bit_buf >> 24; | |
106 | s->bit_buf<<=8; | |
107 | s->bit_cnt-=8; | |
108 | } | |
109 | flush_buffer(s); | |
110 | s->bit_cnt=0; | |
111 | s->bit_buf=0; | |
112 | } | |
113 | ||
4e66ab3b | 114 | /* for jpeg : escape 0xff with 0x00 after it */ |
de6d9b64 FB |
115 | void jput_bits(PutBitContext *s, int n, unsigned int value) |
116 | { | |
117 | unsigned int bit_buf, b; | |
118 | int bit_cnt, i; | |
119 | ||
120 | assert(n == 32 || value < (1U << n)); | |
121 | ||
122 | bit_buf = s->bit_buf; | |
123 | bit_cnt = s->bit_cnt; | |
124 | ||
125 | //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); | |
126 | /* XXX: optimize */ | |
127 | if (n < (32-bit_cnt)) { | |
128 | bit_buf |= value << (32 - n - bit_cnt); | |
129 | bit_cnt+=n; | |
130 | } else { | |
131 | bit_buf |= value >> (n + bit_cnt - 32); | |
132 | /* handle escape */ | |
133 | for(i=0;i<4;i++) { | |
134 | b = (bit_buf >> 24); | |
135 | *(s->buf_ptr++) = b; | |
136 | if (b == 0xff) | |
137 | *(s->buf_ptr++) = 0; | |
138 | bit_buf <<= 8; | |
139 | } | |
140 | /* we flush the buffer sooner to handle worst case */ | |
141 | if (s->buf_ptr >= (s->buf_end - 8)) | |
142 | flush_buffer(s); | |
143 | ||
144 | bit_cnt=bit_cnt + n - 32; | |
145 | if (bit_cnt == 0) { | |
146 | bit_buf = 0; | |
147 | } else { | |
148 | bit_buf = value << (32 - bit_cnt); | |
149 | } | |
150 | } | |
151 | ||
152 | s->bit_buf = bit_buf; | |
153 | s->bit_cnt = bit_cnt; | |
154 | } | |
155 | ||
156 | /* pad the end of the output stream with zeros */ | |
157 | void jflush_put_bits(PutBitContext *s) | |
158 | { | |
159 | unsigned int b; | |
580b82fa | 160 | s->bit_buf |= ~1U >> s->bit_cnt; /* set all the unused bits to one */ |
de6d9b64 FB |
161 | |
162 | while (s->bit_cnt > 0) { | |
163 | b = s->bit_buf >> 24; | |
164 | *s->buf_ptr++ = b; | |
165 | if (b == 0xff) | |
166 | *s->buf_ptr++ = 0; | |
167 | s->bit_buf<<=8; | |
168 | s->bit_cnt-=8; | |
169 | } | |
170 | flush_buffer(s); | |
171 | s->bit_cnt=0; | |
172 | s->bit_buf=0; | |
173 | } | |
174 | ||
175 | /* bit input functions */ | |
176 | ||
177 | void init_get_bits(GetBitContext *s, | |
178 | UINT8 *buffer, int buffer_size) | |
179 | { | |
27a3e2c5 MN |
180 | #ifdef ALT_BITSTREAM_READER |
181 | s->index=0; | |
182 | s->buffer= buffer; | |
183 | #else | |
de6d9b64 FB |
184 | s->buf = buffer; |
185 | s->buf_ptr = buffer; | |
186 | s->buf_end = buffer + buffer_size; | |
187 | s->bit_cnt = 0; | |
188 | s->bit_buf = 0; | |
189 | while (s->buf_ptr < s->buf_end && | |
190 | s->bit_cnt < 32) { | |
191 | s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt)); | |
192 | s->bit_cnt += 8; | |
193 | } | |
27a3e2c5 | 194 | #endif |
de6d9b64 FB |
195 | } |
196 | ||
27a3e2c5 | 197 | #ifndef ALT_BITSTREAM_READER |
de6d9b64 | 198 | /* n must be >= 1 and <= 32 */ |
2931ecb9 A |
199 | /* also true: n > s->bit_cnt */ |
200 | unsigned int get_bits_long(GetBitContext *s, int n) | |
de6d9b64 FB |
201 | { |
202 | unsigned int val; | |
203 | int bit_cnt; | |
204 | unsigned int bit_buf; | |
de6d9b64 FB |
205 | |
206 | #ifdef STATS | |
207 | st_bit_counts[st_current_index] += n; | |
208 | #endif | |
209 | ||
de6d9b64 | 210 | bit_buf = s->bit_buf; |
2931ecb9 | 211 | bit_cnt = s->bit_cnt - n; |
de6d9b64 | 212 | |
2931ecb9 A |
213 | // if (bit_cnt >= 0) { |
214 | // val = bit_buf >> (32 - n); | |
215 | // bit_buf <<= n; | |
216 | // } else | |
217 | { | |
218 | UINT8 *buf_ptr; | |
de6d9b64 FB |
219 | val = bit_buf >> (32 - n); |
220 | buf_ptr = s->buf_ptr; | |
221 | buf_ptr += 4; | |
222 | /* handle common case: we can read everything */ | |
223 | if (buf_ptr <= s->buf_end) { | |
a9b3f630 NK |
224 | #if ARCH_X86 |
225 | bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4]))); | |
226 | #else | |
227 | bit_buf = (buf_ptr[-4] << 24) | | |
228 | (buf_ptr[-3] << 16) | | |
de6d9b64 | 229 | (buf_ptr[-2] << 8) | |
a9b3f630 NK |
230 | (buf_ptr[-1]); |
231 | #endif | |
de6d9b64 FB |
232 | } else { |
233 | buf_ptr -= 4; | |
234 | bit_buf = 0; | |
235 | if (buf_ptr < s->buf_end) | |
236 | bit_buf |= *buf_ptr++ << 24; | |
237 | if (buf_ptr < s->buf_end) | |
238 | bit_buf |= *buf_ptr++ << 16; | |
239 | if (buf_ptr < s->buf_end) | |
240 | bit_buf |= *buf_ptr++ << 8; | |
241 | if (buf_ptr < s->buf_end) | |
242 | bit_buf |= *buf_ptr++; | |
243 | } | |
244 | s->buf_ptr = buf_ptr; | |
245 | val |= bit_buf >> (32 + bit_cnt); | |
246 | bit_buf <<= - bit_cnt; | |
247 | bit_cnt += 32; | |
248 | } | |
249 | s->bit_buf = bit_buf; | |
250 | s->bit_cnt = bit_cnt; | |
251 | return val; | |
252 | } | |
27a3e2c5 | 253 | #endif |
de6d9b64 FB |
254 | |
255 | void align_get_bits(GetBitContext *s) | |
256 | { | |
27a3e2c5 MN |
257 | #ifdef ALT_BITSTREAM_READER |
258 | s->index= (s->index + 7) & (~7); | |
259 | #else | |
de6d9b64 FB |
260 | int n; |
261 | n = s->bit_cnt & 7; | |
262 | if (n > 0) { | |
263 | get_bits(s, n); | |
264 | } | |
27a3e2c5 | 265 | #endif |
de6d9b64 | 266 | } |
27a3e2c5 MN |
267 | |
268 | #ifndef ALT_BITSTREAM_READER | |
4949028f J |
269 | /* This function is identical to get_bits_long(), the */ |
270 | /* only diference is that it doesn't touch the buffer */ | |
271 | /* it is usefull to see the buffer. */ | |
272 | ||
273 | unsigned int show_bits_long(GetBitContext *s, int n) | |
274 | { | |
275 | unsigned int val; | |
276 | int bit_cnt; | |
277 | unsigned int bit_buf; | |
278 | UINT8 *buf_ptr; | |
279 | ||
280 | bit_buf = s->bit_buf; | |
281 | bit_cnt = s->bit_cnt - n; | |
282 | ||
283 | val = bit_buf >> (32 - n); | |
284 | buf_ptr = s->buf_ptr; | |
285 | buf_ptr += 4; | |
286 | ||
287 | /* handle common case: we can read everything */ | |
288 | if (buf_ptr <= s->buf_end) { | |
289 | #ifdef ARCH_X86 | |
290 | bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4]))); | |
291 | #else | |
292 | bit_buf = (buf_ptr[-4] << 24) | | |
293 | (buf_ptr[-3] << 16) | | |
294 | (buf_ptr[-2] << 8) | | |
295 | (buf_ptr[-1]); | |
296 | #endif | |
297 | } else { | |
298 | buf_ptr -= 4; | |
299 | bit_buf = 0; | |
300 | if (buf_ptr < s->buf_end) | |
301 | bit_buf |= *buf_ptr++ << 24; | |
302 | if (buf_ptr < s->buf_end) | |
303 | bit_buf |= *buf_ptr++ << 16; | |
304 | if (buf_ptr < s->buf_end) | |
305 | bit_buf |= *buf_ptr++ << 8; | |
306 | if (buf_ptr < s->buf_end) | |
307 | bit_buf |= *buf_ptr++; | |
308 | } | |
309 | val |= bit_buf >> (32 + bit_cnt); | |
310 | bit_buf <<= - bit_cnt; | |
311 | bit_cnt += 32; | |
312 | ||
313 | return val; | |
314 | } | |
27a3e2c5 | 315 | #endif |
de6d9b64 FB |
316 | |
317 | /* VLC decoding */ | |
318 | ||
319 | //#define DEBUG_VLC | |
320 | ||
321 | #define GET_DATA(v, table, i, wrap, size) \ | |
322 | {\ | |
323 | UINT8 *ptr = (UINT8 *)table + i * wrap;\ | |
324 | switch(size) {\ | |
325 | case 1:\ | |
326 | v = *(UINT8 *)ptr;\ | |
327 | break;\ | |
328 | case 2:\ | |
329 | v = *(UINT16 *)ptr;\ | |
330 | break;\ | |
331 | default:\ | |
332 | v = *(UINT32 *)ptr;\ | |
333 | break;\ | |
334 | }\ | |
335 | } | |
336 | ||
337 | ||
338 | static int alloc_table(VLC *vlc, int size) | |
339 | { | |
340 | int index; | |
341 | index = vlc->table_size; | |
342 | vlc->table_size += size; | |
343 | if (vlc->table_size > vlc->table_allocated) { | |
344 | vlc->table_allocated += (1 << vlc->bits); | |
345 | vlc->table_bits = realloc(vlc->table_bits, | |
346 | sizeof(INT8) * vlc->table_allocated); | |
347 | vlc->table_codes = realloc(vlc->table_codes, | |
348 | sizeof(INT16) * vlc->table_allocated); | |
349 | if (!vlc->table_bits || | |
350 | !vlc->table_codes) | |
351 | return -1; | |
352 | } | |
353 | return index; | |
354 | } | |
355 | ||
356 | static int build_table(VLC *vlc, int table_nb_bits, | |
357 | int nb_codes, | |
358 | const void *bits, int bits_wrap, int bits_size, | |
359 | const void *codes, int codes_wrap, int codes_size, | |
360 | UINT32 code_prefix, int n_prefix) | |
361 | { | |
362 | int i, j, k, n, table_size, table_index, nb, n1, index; | |
363 | UINT32 code; | |
364 | INT8 *table_bits; | |
365 | INT16 *table_codes; | |
366 | ||
367 | table_size = 1 << table_nb_bits; | |
368 | table_index = alloc_table(vlc, table_size); | |
369 | #ifdef DEBUG_VLC | |
370 | printf("new table index=%d size=%d code_prefix=%x n=%d\n", | |
371 | table_index, table_size, code_prefix, n_prefix); | |
372 | #endif | |
373 | if (table_index < 0) | |
374 | return -1; | |
375 | table_bits = &vlc->table_bits[table_index]; | |
376 | table_codes = &vlc->table_codes[table_index]; | |
377 | ||
378 | for(i=0;i<table_size;i++) { | |
379 | table_bits[i] = 0; | |
380 | table_codes[i] = -1; | |
381 | } | |
382 | ||
383 | /* first pass: map codes and compute auxillary table sizes */ | |
384 | for(i=0;i<nb_codes;i++) { | |
385 | GET_DATA(n, bits, i, bits_wrap, bits_size); | |
386 | GET_DATA(code, codes, i, codes_wrap, codes_size); | |
387 | /* we accept tables with holes */ | |
388 | if (n <= 0) | |
389 | continue; | |
390 | #if defined(DEBUG_VLC) && 0 | |
391 | printf("i=%d n=%d code=0x%x\n", i, n, code); | |
392 | #endif | |
393 | /* if code matches the prefix, it is in the table */ | |
394 | n -= n_prefix; | |
395 | if (n > 0 && (code >> n) == code_prefix) { | |
396 | if (n <= table_nb_bits) { | |
397 | /* no need to add another table */ | |
398 | j = (code << (table_nb_bits - n)) & (table_size - 1); | |
399 | nb = 1 << (table_nb_bits - n); | |
400 | for(k=0;k<nb;k++) { | |
401 | #ifdef DEBUG_VLC | |
402 | printf("%4x: code=%d n=%d\n", | |
403 | j, i, n); | |
404 | #endif | |
405 | if (table_bits[j] != 0) { | |
406 | fprintf(stderr, "incorrect codes\n"); | |
407 | exit(1); | |
408 | } | |
409 | table_bits[j] = n; | |
410 | table_codes[j] = i; | |
411 | j++; | |
412 | } | |
413 | } else { | |
414 | n -= table_nb_bits; | |
415 | j = (code >> n) & ((1 << table_nb_bits) - 1); | |
416 | #ifdef DEBUG_VLC | |
417 | printf("%4x: n=%d (subtable)\n", | |
418 | j, n); | |
419 | #endif | |
420 | /* compute table size */ | |
421 | n1 = -table_bits[j]; | |
422 | if (n > n1) | |
423 | n1 = n; | |
424 | table_bits[j] = -n1; | |
425 | } | |
426 | } | |
427 | } | |
428 | ||
429 | /* second pass : fill auxillary tables recursively */ | |
430 | for(i=0;i<table_size;i++) { | |
431 | n = table_bits[i]; | |
432 | if (n < 0) { | |
433 | n = -n; | |
434 | if (n > table_nb_bits) { | |
435 | n = table_nb_bits; | |
436 | table_bits[i] = -n; | |
437 | } | |
438 | index = build_table(vlc, n, nb_codes, | |
439 | bits, bits_wrap, bits_size, | |
440 | codes, codes_wrap, codes_size, | |
441 | (code_prefix << table_nb_bits) | i, | |
442 | n_prefix + table_nb_bits); | |
443 | if (index < 0) | |
444 | return -1; | |
445 | /* note: realloc has been done, so reload tables */ | |
446 | table_bits = &vlc->table_bits[table_index]; | |
447 | table_codes = &vlc->table_codes[table_index]; | |
448 | table_codes[i] = index; | |
449 | } | |
450 | } | |
451 | return table_index; | |
452 | } | |
453 | ||
454 | ||
4e66ab3b FB |
455 | /* Build VLC decoding tables suitable for use with get_vlc(). |
456 | ||
457 | 'nb_bits' set thee decoding table size (2^nb_bits) entries. The | |
458 | bigger it is, the faster is the decoding. But it should not be too | |
459 | big to save memory and L1 cache. '9' is a good compromise. | |
460 | ||
461 | 'nb_codes' : number of vlcs codes | |
462 | ||
463 | 'bits' : table which gives the size (in bits) of each vlc code. | |
464 | ||
465 | 'codes' : table which gives the bit pattern of of each vlc code. | |
466 | ||
467 | 'xxx_wrap' : give the number of bytes between each entry of the | |
468 | 'bits' or 'codes' tables. | |
469 | ||
470 | 'xxx_size' : gives the number of bytes of each entry of the 'bits' | |
471 | or 'codes' tables. | |
472 | ||
473 | 'wrap' and 'size' allows to use any memory configuration and types | |
474 | (byte/word/long) to store the 'bits' and 'codes' tables. | |
475 | */ | |
de6d9b64 FB |
476 | int init_vlc(VLC *vlc, int nb_bits, int nb_codes, |
477 | const void *bits, int bits_wrap, int bits_size, | |
478 | const void *codes, int codes_wrap, int codes_size) | |
479 | { | |
480 | vlc->bits = nb_bits; | |
481 | vlc->table_bits = NULL; | |
482 | vlc->table_codes = NULL; | |
483 | vlc->table_allocated = 0; | |
484 | vlc->table_size = 0; | |
485 | #ifdef DEBUG_VLC | |
486 | printf("build table nb_codes=%d\n", nb_codes); | |
487 | #endif | |
488 | ||
489 | if (build_table(vlc, nb_bits, nb_codes, | |
490 | bits, bits_wrap, bits_size, | |
491 | codes, codes_wrap, codes_size, | |
492 | 0, 0) < 0) { | |
493 | if (vlc->table_bits) | |
494 | free(vlc->table_bits); | |
495 | if (vlc->table_codes) | |
496 | free(vlc->table_codes); | |
497 | return -1; | |
498 | } | |
499 | return 0; | |
500 | } | |
501 | ||
502 | ||
503 | void free_vlc(VLC *vlc) | |
504 | { | |
505 | free(vlc->table_bits); | |
506 | free(vlc->table_codes); | |
507 | } | |
508 |