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 & writer 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))
30 s->buf_end = s->buf + buffer_size;
34 fprintf(stderr, "write Data callback is not supported\n");
36 #ifdef ALT_BITSTREAM_WRITER
38 ((uint32_t*)(s->buf))[0]=0;
39 // memset(buffer, 0, buffer_size);
47 /* return the number of bits output */
48 INT64 get_bit_count(PutBitContext *s)
50 #ifdef ALT_BITSTREAM_WRITER
51 return s->data_out_size * 8 + s->index;
53 return (s->buf_ptr - s->buf + s->data_out_size) * 8 + 32 - (INT64)s->bit_left;
57 void align_put_bits(PutBitContext *s)
59 #ifdef ALT_BITSTREAM_WRITER
60 put_bits(s,( - s->index) & 7,0);
62 put_bits(s,s->bit_left & 7,0);
66 /* pad the end of the output stream with zeros */
67 void flush_put_bits(PutBitContext *s)
69 #ifdef ALT_BITSTREAM_WRITER
72 s->bit_buf<<= s->bit_left;
73 while (s->bit_left < 32) {
74 /* XXX: should test end of buffer */
75 *s->buf_ptr++=s->bit_buf >> 24;
84 /* pad the end of the output stream with zeros */
85 #ifndef ALT_BITSTREAM_WRITER
86 void jflush_put_bits(PutBitContext *s)
89 s->bit_buf<<= s->bit_left;
90 s->bit_buf |= ~1U >> (32 - s->bit_left); /* set all the unused bits to one */
92 while (s->bit_left < 32) {
104 void jflush_put_bits(PutBitContext *s)
106 int num= ( - s->index) & 7;
107 jput_bits(s, num,0xFF>>(8-num));
111 /* bit input functions */
113 void init_get_bits(GetBitContext *s,
114 UINT8 *buffer, int buffer_size)
116 #ifdef ALT_BITSTREAM_READER
122 s->buf_end = buffer + buffer_size;
125 while (s->buf_ptr < s->buf_end &&
127 s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt));
131 s->size= buffer_size;
134 #ifndef ALT_BITSTREAM_READER
135 /* n must be >= 1 and <= 32 */
136 /* also true: n > s->bit_cnt */
137 unsigned int get_bits_long(GetBitContext *s, int n)
141 unsigned int bit_buf;
144 st_bit_counts[st_current_index] += n;
147 bit_buf = s->bit_buf;
148 bit_cnt = s->bit_cnt - n;
150 // if (bit_cnt >= 0) {
151 // val = bit_buf >> (32 - n);
156 val = bit_buf >> (32 - n);
157 buf_ptr = s->buf_ptr;
159 /* handle common case: we can read everything */
160 if (buf_ptr <= s->buf_end) {
162 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4])));
164 bit_buf = (buf_ptr[-4] << 24) |
165 (buf_ptr[-3] << 16) |
172 if (buf_ptr < s->buf_end)
173 bit_buf |= *buf_ptr++ << 24;
174 if (buf_ptr < s->buf_end)
175 bit_buf |= *buf_ptr++ << 16;
176 if (buf_ptr < s->buf_end)
177 bit_buf |= *buf_ptr++ << 8;
178 if (buf_ptr < s->buf_end)
179 bit_buf |= *buf_ptr++;
181 s->buf_ptr = buf_ptr;
182 val |= bit_buf >> (32 + bit_cnt);
183 bit_buf <<= - bit_cnt;
186 s->bit_buf = bit_buf;
187 s->bit_cnt = bit_cnt;
192 void align_get_bits(GetBitContext *s)
194 #ifdef ALT_BITSTREAM_READER
195 s->index= (s->index + 7) & (~7);
205 int check_marker(GetBitContext *s, char *msg)
207 int bit= get_bits1(s);
208 if(!bit) printf("Marker bit missing %s\n", msg);
213 #ifndef ALT_BITSTREAM_READER
214 /* This function is identical to get_bits_long(), the */
215 /* only diference is that it doesn't touch the buffer */
216 /* it is usefull to see the buffer. */
218 unsigned int show_bits_long(GetBitContext *s, int n)
222 unsigned int bit_buf;
225 bit_buf = s->bit_buf;
226 bit_cnt = s->bit_cnt - n;
228 val = bit_buf >> (32 - n);
229 buf_ptr = s->buf_ptr;
232 /* handle common case: we can read everything */
233 if (buf_ptr <= s->buf_end) {
235 bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4])));
237 bit_buf = (buf_ptr[-4] << 24) |
238 (buf_ptr[-3] << 16) |
245 if (buf_ptr < s->buf_end)
246 bit_buf |= *buf_ptr++ << 24;
247 if (buf_ptr < s->buf_end)
248 bit_buf |= *buf_ptr++ << 16;
249 if (buf_ptr < s->buf_end)
250 bit_buf |= *buf_ptr++ << 8;
251 if (buf_ptr < s->buf_end)
252 bit_buf |= *buf_ptr++;
254 val |= bit_buf >> (32 + bit_cnt);
255 bit_buf <<= - bit_cnt;
266 #define GET_DATA(v, table, i, wrap, size) \
268 UINT8 *ptr = (UINT8 *)table + i * wrap;\
283 static int alloc_table(VLC *vlc, int size)
286 index = vlc->table_size;
287 vlc->table_size += size;
288 if (vlc->table_size > vlc->table_allocated) {
289 vlc->table_allocated += (1 << vlc->bits);
290 vlc->table_bits = realloc(vlc->table_bits,
291 sizeof(INT8) * vlc->table_allocated);
292 vlc->table_codes = realloc(vlc->table_codes,
293 sizeof(INT16) * vlc->table_allocated);
294 if (!vlc->table_bits ||
301 static int build_table(VLC *vlc, int table_nb_bits,
303 const void *bits, int bits_wrap, int bits_size,
304 const void *codes, int codes_wrap, int codes_size,
305 UINT32 code_prefix, int n_prefix)
307 int i, j, k, n, table_size, table_index, nb, n1, index;
312 table_size = 1 << table_nb_bits;
313 table_index = alloc_table(vlc, table_size);
315 printf("new table index=%d size=%d code_prefix=%x n=%d\n",
316 table_index, table_size, code_prefix, n_prefix);
320 table_bits = &vlc->table_bits[table_index];
321 table_codes = &vlc->table_codes[table_index];
323 for(i=0;i<table_size;i++) {
328 /* first pass: map codes and compute auxillary table sizes */
329 for(i=0;i<nb_codes;i++) {
330 GET_DATA(n, bits, i, bits_wrap, bits_size);
331 GET_DATA(code, codes, i, codes_wrap, codes_size);
332 /* we accept tables with holes */
335 #if defined(DEBUG_VLC) && 0
336 printf("i=%d n=%d code=0x%x\n", i, n, code);
338 /* if code matches the prefix, it is in the table */
340 if (n > 0 && (code >> n) == code_prefix) {
341 if (n <= table_nb_bits) {
342 /* no need to add another table */
343 j = (code << (table_nb_bits - n)) & (table_size - 1);
344 nb = 1 << (table_nb_bits - n);
347 printf("%4x: code=%d n=%d\n",
350 if (table_bits[j] != 0) {
351 fprintf(stderr, "incorrect codes\n");
360 j = (code >> n) & ((1 << table_nb_bits) - 1);
362 printf("%4x: n=%d (subtable)\n",
365 /* compute table size */
374 /* second pass : fill auxillary tables recursively */
375 for(i=0;i<table_size;i++) {
379 if (n > table_nb_bits) {
383 index = build_table(vlc, n, nb_codes,
384 bits, bits_wrap, bits_size,
385 codes, codes_wrap, codes_size,
386 (code_prefix << table_nb_bits) | i,
387 n_prefix + table_nb_bits);
390 /* note: realloc has been done, so reload tables */
391 table_bits = &vlc->table_bits[table_index];
392 table_codes = &vlc->table_codes[table_index];
393 table_codes[i] = index;
400 /* Build VLC decoding tables suitable for use with get_vlc().
402 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
403 bigger it is, the faster is the decoding. But it should not be too
404 big to save memory and L1 cache. '9' is a good compromise.
406 'nb_codes' : number of vlcs codes
408 'bits' : table which gives the size (in bits) of each vlc code.
410 'codes' : table which gives the bit pattern of of each vlc code.
412 'xxx_wrap' : give the number of bytes between each entry of the
413 'bits' or 'codes' tables.
415 'xxx_size' : gives the number of bytes of each entry of the 'bits'
418 'wrap' and 'size' allows to use any memory configuration and types
419 (byte/word/long) to store the 'bits' and 'codes' tables.
421 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
422 const void *bits, int bits_wrap, int bits_size,
423 const void *codes, int codes_wrap, int codes_size)
426 vlc->table_bits = NULL;
427 vlc->table_codes = NULL;
428 vlc->table_allocated = 0;
431 printf("build table nb_codes=%d\n", nb_codes);
434 if (build_table(vlc, nb_bits, nb_codes,
435 bits, bits_wrap, bits_size,
436 codes, codes_wrap, codes_size,
439 free(vlc->table_bits);
440 if (vlc->table_codes)
441 free(vlc->table_codes);
448 void free_vlc(VLC *vlc)
450 free(vlc->table_bits);
451 free(vlc->table_codes);
454 int ff_gcd(int a, int b){
455 if(b) return ff_gcd(b, a%b);