3 * Copyright (c) 2000, 2001 Gerard Lantau.
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.
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.
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.
19 * alternative bitstream reader by Michael Niedermayer <michaelni@gmx.at>
24 void init_put_bits(PutBitContext *s,
25 UINT8 *buffer, int buffer_size,
27 void (*write_data)(void *, UINT8 *, int))
31 s->buf_end = s->buf + buffer_size;
35 s->write_data = write_data;
39 static void flush_buffer(PutBitContext *s)
43 size = s->buf_ptr - s->buf;
45 s->write_data(s->opaque, s->buf, size);
47 s->data_out_size += size;
51 void put_bits(PutBitContext *s, int n, unsigned int value)
57 st_out_bit_counts[st_current_index] += n;
59 // printf("put_bits=%d %x\n", n, value);
60 assert(n == 32 || value < (1U << n));
65 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
67 if (n < (32-bit_cnt)) {
68 bit_buf |= value << (32 - n - bit_cnt);
71 bit_buf |= value >> (n + bit_cnt - 32);
72 *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
73 //printf("bitbuf = %08x\n", bit_buf);
75 if (s->buf_ptr >= s->buf_end)
77 bit_cnt=bit_cnt + n - 32;
81 bit_buf = value << (32 - bit_cnt);
89 /* return the number of bits output */
90 INT64 get_bit_count(PutBitContext *s)
92 return (s->buf_ptr - s->buf + s->data_out_size) * 8 + (INT64)s->bit_cnt;
95 void align_put_bits(PutBitContext *s)
97 put_bits(s,(8 - s->bit_cnt) & 7,0);
100 /* pad the end of the output stream with zeros */
101 void flush_put_bits(PutBitContext *s)
103 while (s->bit_cnt > 0) {
104 /* XXX: should test end of buffer */
105 *s->buf_ptr++=s->bit_buf >> 24;
114 /* for jpeg : escape 0xff with 0x00 after it */
115 void jput_bits(PutBitContext *s, int n, unsigned int value)
117 unsigned int bit_buf, b;
120 assert(n == 32 || value < (1U << n));
122 bit_buf = s->bit_buf;
123 bit_cnt = s->bit_cnt;
125 //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
127 if (n < (32-bit_cnt)) {
128 bit_buf |= value << (32 - n - bit_cnt);
131 bit_buf |= value >> (n + bit_cnt - 32);
140 /* we flush the buffer sooner to handle worst case */
141 if (s->buf_ptr >= (s->buf_end - 8))
144 bit_cnt=bit_cnt + n - 32;
148 bit_buf = value << (32 - bit_cnt);
152 s->bit_buf = bit_buf;
153 s->bit_cnt = bit_cnt;
156 /* pad the end of the output stream with zeros */
157 void jflush_put_bits(PutBitContext *s)
161 while (s->bit_cnt > 0) {
162 b = s->bit_buf >> 24;
174 /* bit input functions */
176 void init_get_bits(GetBitContext *s,
177 UINT8 *buffer, int buffer_size)
179 #ifdef ALT_BITSTREAM_READER
185 s->buf_end = buffer + buffer_size;
188 while (s->buf_ptr < s->buf_end &&
190 s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt));
196 #ifndef ALT_BITSTREAM_READER
197 /* n must be >= 1 and <= 32 */
198 /* also true: n > s->bit_cnt */
199 unsigned int get_bits_long(GetBitContext *s, int n)
203 unsigned int bit_buf;
206 st_bit_counts[st_current_index] += n;
209 bit_buf = s->bit_buf;
210 bit_cnt = s->bit_cnt - n;
212 // if (bit_cnt >= 0) {
213 // val = bit_buf >> (32 - n);
218 val = bit_buf >> (32 - n);
219 buf_ptr = s->buf_ptr;
221 /* handle common case: we can read everything */
222 if (buf_ptr <= s->buf_end) {
224 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4])));
226 bit_buf = (buf_ptr[-4] << 24) |
227 (buf_ptr[-3] << 16) |
234 if (buf_ptr < s->buf_end)
235 bit_buf |= *buf_ptr++ << 24;
236 if (buf_ptr < s->buf_end)
237 bit_buf |= *buf_ptr++ << 16;
238 if (buf_ptr < s->buf_end)
239 bit_buf |= *buf_ptr++ << 8;
240 if (buf_ptr < s->buf_end)
241 bit_buf |= *buf_ptr++;
243 s->buf_ptr = buf_ptr;
244 val |= bit_buf >> (32 + bit_cnt);
245 bit_buf <<= - bit_cnt;
248 s->bit_buf = bit_buf;
249 s->bit_cnt = bit_cnt;
254 void align_get_bits(GetBitContext *s)
256 #ifdef ALT_BITSTREAM_READER
257 s->index= (s->index + 7) & (~7);
267 #ifndef ALT_BITSTREAM_READER
268 /* This function is identical to get_bits_long(), the */
269 /* only diference is that it doesn't touch the buffer */
270 /* it is usefull to see the buffer. */
272 unsigned int show_bits_long(GetBitContext *s, int n)
276 unsigned int bit_buf;
279 bit_buf = s->bit_buf;
280 bit_cnt = s->bit_cnt - n;
282 val = bit_buf >> (32 - n);
283 buf_ptr = s->buf_ptr;
286 /* handle common case: we can read everything */
287 if (buf_ptr <= s->buf_end) {
289 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4])));
291 bit_buf = (buf_ptr[-4] << 24) |
292 (buf_ptr[-3] << 16) |
299 if (buf_ptr < s->buf_end)
300 bit_buf |= *buf_ptr++ << 24;
301 if (buf_ptr < s->buf_end)
302 bit_buf |= *buf_ptr++ << 16;
303 if (buf_ptr < s->buf_end)
304 bit_buf |= *buf_ptr++ << 8;
305 if (buf_ptr < s->buf_end)
306 bit_buf |= *buf_ptr++;
308 val |= bit_buf >> (32 + bit_cnt);
309 bit_buf <<= - bit_cnt;
320 #define GET_DATA(v, table, i, wrap, size) \
322 UINT8 *ptr = (UINT8 *)table + i * wrap;\
337 static int alloc_table(VLC *vlc, int size)
340 index = vlc->table_size;
341 vlc->table_size += size;
342 if (vlc->table_size > vlc->table_allocated) {
343 vlc->table_allocated += (1 << vlc->bits);
344 vlc->table_bits = realloc(vlc->table_bits,
345 sizeof(INT8) * vlc->table_allocated);
346 vlc->table_codes = realloc(vlc->table_codes,
347 sizeof(INT16) * vlc->table_allocated);
348 if (!vlc->table_bits ||
355 static int build_table(VLC *vlc, int table_nb_bits,
357 const void *bits, int bits_wrap, int bits_size,
358 const void *codes, int codes_wrap, int codes_size,
359 UINT32 code_prefix, int n_prefix)
361 int i, j, k, n, table_size, table_index, nb, n1, index;
366 table_size = 1 << table_nb_bits;
367 table_index = alloc_table(vlc, table_size);
369 printf("new table index=%d size=%d code_prefix=%x n=%d\n",
370 table_index, table_size, code_prefix, n_prefix);
374 table_bits = &vlc->table_bits[table_index];
375 table_codes = &vlc->table_codes[table_index];
377 for(i=0;i<table_size;i++) {
382 /* first pass: map codes and compute auxillary table sizes */
383 for(i=0;i<nb_codes;i++) {
384 GET_DATA(n, bits, i, bits_wrap, bits_size);
385 GET_DATA(code, codes, i, codes_wrap, codes_size);
386 /* we accept tables with holes */
389 #if defined(DEBUG_VLC) && 0
390 printf("i=%d n=%d code=0x%x\n", i, n, code);
392 /* if code matches the prefix, it is in the table */
394 if (n > 0 && (code >> n) == code_prefix) {
395 if (n <= table_nb_bits) {
396 /* no need to add another table */
397 j = (code << (table_nb_bits - n)) & (table_size - 1);
398 nb = 1 << (table_nb_bits - n);
401 printf("%4x: code=%d n=%d\n",
404 if (table_bits[j] != 0) {
405 fprintf(stderr, "incorrect codes\n");
414 j = (code >> n) & ((1 << table_nb_bits) - 1);
416 printf("%4x: n=%d (subtable)\n",
419 /* compute table size */
428 /* second pass : fill auxillary tables recursively */
429 for(i=0;i<table_size;i++) {
433 if (n > table_nb_bits) {
437 index = build_table(vlc, n, nb_codes,
438 bits, bits_wrap, bits_size,
439 codes, codes_wrap, codes_size,
440 (code_prefix << table_nb_bits) | i,
441 n_prefix + table_nb_bits);
444 /* note: realloc has been done, so reload tables */
445 table_bits = &vlc->table_bits[table_index];
446 table_codes = &vlc->table_codes[table_index];
447 table_codes[i] = index;
454 /* Build VLC decoding tables suitable for use with get_vlc().
456 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
457 bigger it is, the faster is the decoding. But it should not be too
458 big to save memory and L1 cache. '9' is a good compromise.
460 'nb_codes' : number of vlcs codes
462 'bits' : table which gives the size (in bits) of each vlc code.
464 'codes' : table which gives the bit pattern of of each vlc code.
466 'xxx_wrap' : give the number of bytes between each entry of the
467 'bits' or 'codes' tables.
469 'xxx_size' : gives the number of bytes of each entry of the 'bits'
472 'wrap' and 'size' allows to use any memory configuration and types
473 (byte/word/long) to store the 'bits' and 'codes' tables.
475 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
476 const void *bits, int bits_wrap, int bits_size,
477 const void *codes, int codes_wrap, int codes_size)
480 vlc->table_bits = NULL;
481 vlc->table_codes = NULL;
482 vlc->table_allocated = 0;
485 printf("build table nb_codes=%d\n", nb_codes);
488 if (build_table(vlc, nb_bits, nb_codes,
489 bits, bits_wrap, bits_size,
490 codes, codes_wrap, codes_size,
493 free(vlc->table_bits);
494 if (vlc->table_codes)
495 free(vlc->table_codes);
502 void free_vlc(VLC *vlc)
504 free(vlc->table_bits);
505 free(vlc->table_codes);
508 int get_vlc(GetBitContext *s, VLC *vlc)
510 int bit_cnt, code, n, nb_bits, index;
518 table_codes = vlc->table_codes;
519 table_bits = vlc->table_bits;
521 SHOW_BITS(s, index, nb_bits);
522 code = table_codes[index];
523 n = table_bits[index];
525 /* most common case */
528 st_bit_counts[st_current_index] += n;
536 st_bit_counts[st_current_index] += nb_bits;
539 table_codes = vlc->table_codes + code;
540 table_bits = vlc->table_bits + code;