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.
23 #include <sys/param.h>
25 #include <netinet/in.h>
32 void init_put_bits(PutBitContext *s,
33 UINT8 *buffer, int buffer_size,
35 void (*write_data)(void *, UINT8 *, int))
39 s->buf_end = s->buf + buffer_size;
43 s->write_data = write_data;
47 static void flush_buffer(PutBitContext *s)
51 size = s->buf_ptr - s->buf;
53 s->write_data(s->opaque, s->buf, size);
55 s->data_out_size += size;
59 void put_bits(PutBitContext *s, int n, unsigned int value)
65 st_out_bit_counts[st_current_index] += n;
67 // printf("put_bits=%d %x\n", n, value);
68 assert(n == 32 || value < (1U << n));
73 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
75 if (n < (32-bit_cnt)) {
76 bit_buf |= value << (32 - n - bit_cnt);
79 bit_buf |= value >> (n + bit_cnt - 32);
80 *(UINT32 *)s->buf_ptr = htonl(bit_buf);
81 //printf("bitbuf = %08x\n", bit_buf);
83 if (s->buf_ptr >= s->buf_end)
85 bit_cnt=bit_cnt + n - 32;
89 bit_buf = value << (32 - bit_cnt);
97 /* return the number of bits output */
98 long long get_bit_count(PutBitContext *s)
100 return (s->buf_ptr - s->buf + s->data_out_size) * 8 + (long long)s->bit_cnt;
103 void align_put_bits(PutBitContext *s)
105 put_bits(s,(8 - s->bit_cnt) & 7,0);
108 /* pad the end of the output stream with zeros */
109 void flush_put_bits(PutBitContext *s)
111 while (s->bit_cnt > 0) {
112 /* XXX: should test end of buffer */
113 *s->buf_ptr++=s->bit_buf >> 24;
122 /* for jpeg : espace 0xff with 0x00 after it */
123 void jput_bits(PutBitContext *s, int n, unsigned int value)
125 unsigned int bit_buf, b;
128 assert(n == 32 || value < (1U << n));
130 bit_buf = s->bit_buf;
131 bit_cnt = s->bit_cnt;
133 //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
135 if (n < (32-bit_cnt)) {
136 bit_buf |= value << (32 - n - bit_cnt);
139 bit_buf |= value >> (n + bit_cnt - 32);
148 /* we flush the buffer sooner to handle worst case */
149 if (s->buf_ptr >= (s->buf_end - 8))
152 bit_cnt=bit_cnt + n - 32;
156 bit_buf = value << (32 - bit_cnt);
160 s->bit_buf = bit_buf;
161 s->bit_cnt = bit_cnt;
164 /* pad the end of the output stream with zeros */
165 void jflush_put_bits(PutBitContext *s)
169 while (s->bit_cnt > 0) {
170 b = s->bit_buf >> 24;
182 /* bit input functions */
184 void init_get_bits(GetBitContext *s,
185 UINT8 *buffer, int buffer_size)
189 s->buf_end = buffer + buffer_size;
192 while (s->buf_ptr < s->buf_end &&
194 s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt));
199 /* n must be >= 1 and <= 32 */
200 unsigned int get_bits(GetBitContext *s, int n)
204 unsigned int bit_buf;
208 st_bit_counts[st_current_index] += n;
211 bit_cnt = s->bit_cnt;
212 bit_buf = s->bit_buf;
216 /* most common case here */
217 val = bit_buf >> (32 - n);
220 val = bit_buf >> (32 - n);
221 buf_ptr = s->buf_ptr;
223 /* handle common case: we can read everything */
224 if (buf_ptr <= s->buf_end) {
225 bit_buf = (buf_ptr[-4] << 24) |
226 (buf_ptr[-3] << 16) |
232 if (buf_ptr < s->buf_end)
233 bit_buf |= *buf_ptr++ << 24;
234 if (buf_ptr < s->buf_end)
235 bit_buf |= *buf_ptr++ << 16;
236 if (buf_ptr < s->buf_end)
237 bit_buf |= *buf_ptr++ << 8;
238 if (buf_ptr < s->buf_end)
239 bit_buf |= *buf_ptr++;
241 s->buf_ptr = buf_ptr;
242 val |= bit_buf >> (32 + bit_cnt);
243 bit_buf <<= - bit_cnt;
246 s->bit_buf = bit_buf;
247 s->bit_cnt = bit_cnt;
251 void align_get_bits(GetBitContext *s)
264 #define GET_DATA(v, table, i, wrap, size) \
266 UINT8 *ptr = (UINT8 *)table + i * wrap;\
281 static int alloc_table(VLC *vlc, int size)
284 index = vlc->table_size;
285 vlc->table_size += size;
286 if (vlc->table_size > vlc->table_allocated) {
287 vlc->table_allocated += (1 << vlc->bits);
288 vlc->table_bits = realloc(vlc->table_bits,
289 sizeof(INT8) * vlc->table_allocated);
290 vlc->table_codes = realloc(vlc->table_codes,
291 sizeof(INT16) * vlc->table_allocated);
292 if (!vlc->table_bits ||
299 static int build_table(VLC *vlc, int table_nb_bits,
301 const void *bits, int bits_wrap, int bits_size,
302 const void *codes, int codes_wrap, int codes_size,
303 UINT32 code_prefix, int n_prefix)
305 int i, j, k, n, table_size, table_index, nb, n1, index;
310 table_size = 1 << table_nb_bits;
311 table_index = alloc_table(vlc, table_size);
313 printf("new table index=%d size=%d code_prefix=%x n=%d\n",
314 table_index, table_size, code_prefix, n_prefix);
318 table_bits = &vlc->table_bits[table_index];
319 table_codes = &vlc->table_codes[table_index];
321 for(i=0;i<table_size;i++) {
326 /* first pass: map codes and compute auxillary table sizes */
327 for(i=0;i<nb_codes;i++) {
328 GET_DATA(n, bits, i, bits_wrap, bits_size);
329 GET_DATA(code, codes, i, codes_wrap, codes_size);
330 /* we accept tables with holes */
333 #if defined(DEBUG_VLC) && 0
334 printf("i=%d n=%d code=0x%x\n", i, n, code);
336 /* if code matches the prefix, it is in the table */
338 if (n > 0 && (code >> n) == code_prefix) {
339 if (n <= table_nb_bits) {
340 /* no need to add another table */
341 j = (code << (table_nb_bits - n)) & (table_size - 1);
342 nb = 1 << (table_nb_bits - n);
345 printf("%4x: code=%d n=%d\n",
348 if (table_bits[j] != 0) {
349 fprintf(stderr, "incorrect codes\n");
358 j = (code >> n) & ((1 << table_nb_bits) - 1);
360 printf("%4x: n=%d (subtable)\n",
363 /* compute table size */
372 /* second pass : fill auxillary tables recursively */
373 for(i=0;i<table_size;i++) {
377 if (n > table_nb_bits) {
381 index = build_table(vlc, n, nb_codes,
382 bits, bits_wrap, bits_size,
383 codes, codes_wrap, codes_size,
384 (code_prefix << table_nb_bits) | i,
385 n_prefix + table_nb_bits);
388 /* note: realloc has been done, so reload tables */
389 table_bits = &vlc->table_bits[table_index];
390 table_codes = &vlc->table_codes[table_index];
391 table_codes[i] = index;
398 /* wrap and size allow to handle most types of storage. */
399 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
400 const void *bits, int bits_wrap, int bits_size,
401 const void *codes, int codes_wrap, int codes_size)
404 vlc->table_bits = NULL;
405 vlc->table_codes = NULL;
406 vlc->table_allocated = 0;
409 printf("build table nb_codes=%d\n", nb_codes);
412 if (build_table(vlc, nb_bits, nb_codes,
413 bits, bits_wrap, bits_size,
414 codes, codes_wrap, codes_size,
417 free(vlc->table_bits);
418 if (vlc->table_codes)
419 free(vlc->table_codes);
426 void free_vlc(VLC *vlc)
428 free(vlc->table_bits);
429 free(vlc->table_codes);
432 int get_vlc(GetBitContext *s, VLC *vlc)
434 int bit_cnt, code, n, nb_bits, index;
442 table_codes = vlc->table_codes;
443 table_bits = vlc->table_bits;
445 SHOW_BITS(s, index, nb_bits);
446 code = table_codes[index];
447 n = table_bits[index];
449 /* most common case */
452 st_bit_counts[st_current_index] += n;
460 st_bit_counts[st_current_index] += nb_bits;
463 table_codes = vlc->table_codes + code;
464 table_bits = vlc->table_bits + code;