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.
22
void init_put_bits(PutBitContext *s,
23
UINT8 *buffer, int buffer_size,
25
void (*write_data)(void *, UINT8 *, int))
29
s->buf_end = s->buf + buffer_size;
33
s->write_data = write_data;
37
static void flush_buffer(PutBitContext *s)
41
size = s->buf_ptr - s->buf;
43
s->write_data(s->opaque, s->buf, size);
45
s->data_out_size += size;
49
void put_bits(PutBitContext *s, int n, unsigned int value)
55
st_out_bit_counts[st_current_index] += n;
57
// printf("put_bits=%d %x\n", n, value);
58
assert(n == 32 || value < (1U << n));
63
// printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
65
if (n < (32-bit_cnt)) {
66
bit_buf |= value << (32 - n - bit_cnt);
69
bit_buf |= value >> (n + bit_cnt - 32);
70
*(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
71
//printf("bitbuf = %08x\n", bit_buf);
73
if (s->buf_ptr >= s->buf_end)
75
bit_cnt=bit_cnt + n - 32;
79
bit_buf = value << (32 - bit_cnt);
87
/* return the number of bits output */
88
INT64 get_bit_count(PutBitContext *s)
90
return (s->buf_ptr - s->buf + s->data_out_size) * 8 + (INT64)s->bit_cnt;
93
void align_put_bits(PutBitContext *s)
95
put_bits(s,(8 - s->bit_cnt) & 7,0);
98
/* pad the end of the output stream with zeros */
99
void flush_put_bits(PutBitContext *s)
101
while (s->bit_cnt > 0) {
102
/* XXX: should test end of buffer */
103
*s->buf_ptr++=s->bit_buf >> 24;
112
/* for jpeg : escape 0xff with 0x00 after it */
113
void jput_bits(PutBitContext *s, int n, unsigned int value)
115
unsigned int bit_buf, b;
118
assert(n == 32 || value < (1U << n));
120
bit_buf = s->bit_buf;
121
bit_cnt = s->bit_cnt;
123
//printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
125
if (n < (32-bit_cnt)) {
126
bit_buf |= value << (32 - n - bit_cnt);
129
bit_buf |= value >> (n + bit_cnt - 32);
138
/* we flush the buffer sooner to handle worst case */
139
if (s->buf_ptr >= (s->buf_end - 8))
142
bit_cnt=bit_cnt + n - 32;
146
bit_buf = value << (32 - bit_cnt);
150
s->bit_buf = bit_buf;
151
s->bit_cnt = bit_cnt;
154
/* pad the end of the output stream with zeros */
155
void jflush_put_bits(PutBitContext *s)
159
while (s->bit_cnt > 0) {
160
b = s->bit_buf >> 24;
172
/* bit input functions */
174
void init_get_bits(GetBitContext *s,
175
UINT8 *buffer, int buffer_size)
179
s->buf_end = buffer + buffer_size;
182
while (s->buf_ptr < s->buf_end &&
184
s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt));
189
/* n must be >= 1 and <= 32 */
190
/* also true: n > s->bit_cnt */
191
unsigned int get_bits_long(GetBitContext *s, int n)
195
unsigned int bit_buf;
198
st_bit_counts[st_current_index] += n;
201
bit_buf = s->bit_buf;
202
bit_cnt = s->bit_cnt - n;
204
// if (bit_cnt >= 0) {
205
// val = bit_buf >> (32 - n);
210
val = bit_buf >> (32 - n);
211
buf_ptr = s->buf_ptr;
213
/* handle common case: we can read everything */
214
if (buf_ptr <= s->buf_end) {
216
bit_buf = bswap_32(*((unsigned long*)(&buf_ptr[-4])));
218
bit_buf = (buf_ptr[-4] << 24) |
219
(buf_ptr[-3] << 16) |
226
if (buf_ptr < s->buf_end)
227
bit_buf |= *buf_ptr++ << 24;
228
if (buf_ptr < s->buf_end)
229
bit_buf |= *buf_ptr++ << 16;
230
if (buf_ptr < s->buf_end)
231
bit_buf |= *buf_ptr++ << 8;
232
if (buf_ptr < s->buf_end)
233
bit_buf |= *buf_ptr++;
235
s->buf_ptr = buf_ptr;
236
val |= bit_buf >> (32 + bit_cnt);
237
bit_buf <<= - bit_cnt;
240
s->bit_buf = bit_buf;
241
s->bit_cnt = bit_cnt;
245
void align_get_bits(GetBitContext *s)
258
#define GET_DATA(v, table, i, wrap, size) \
260
UINT8 *ptr = (UINT8 *)table + i * wrap;\
275
static int alloc_table(VLC *vlc, int size)
278
index = vlc->table_size;
279
vlc->table_size += size;
280
if (vlc->table_size > vlc->table_allocated) {
281
vlc->table_allocated += (1 << vlc->bits);
282
vlc->table_bits = realloc(vlc->table_bits,
283
sizeof(INT8) * vlc->table_allocated);
284
vlc->table_codes = realloc(vlc->table_codes,
285
sizeof(INT16) * vlc->table_allocated);
286
if (!vlc->table_bits ||
293
static int build_table(VLC *vlc, int table_nb_bits,
295
const void *bits, int bits_wrap, int bits_size,
296
const void *codes, int codes_wrap, int codes_size,
297
UINT32 code_prefix, int n_prefix)
299
int i, j, k, n, table_size, table_index, nb, n1, index;
304
table_size = 1 << table_nb_bits;
305
table_index = alloc_table(vlc, table_size);
307
printf("new table index=%d size=%d code_prefix=%x n=%d\n",
308
table_index, table_size, code_prefix, n_prefix);
312
table_bits = &vlc->table_bits[table_index];
313
table_codes = &vlc->table_codes[table_index];
315
for(i=0;i<table_size;i++) {
320
/* first pass: map codes and compute auxillary table sizes */
321
for(i=0;i<nb_codes;i++) {
322
GET_DATA(n, bits, i, bits_wrap, bits_size);
323
GET_DATA(code, codes, i, codes_wrap, codes_size);
324
/* we accept tables with holes */
327
#if defined(DEBUG_VLC) && 0
328
printf("i=%d n=%d code=0x%x\n", i, n, code);
330
/* if code matches the prefix, it is in the table */
332
if (n > 0 && (code >> n) == code_prefix) {
333
if (n <= table_nb_bits) {
334
/* no need to add another table */
335
j = (code << (table_nb_bits - n)) & (table_size - 1);
336
nb = 1 << (table_nb_bits - n);
339
printf("%4x: code=%d n=%d\n",
342
if (table_bits[j] != 0) {
343
fprintf(stderr, "incorrect codes\n");
352
j = (code >> n) & ((1 << table_nb_bits) - 1);
354
printf("%4x: n=%d (subtable)\n",
357
/* compute table size */
366
/* second pass : fill auxillary tables recursively */
367
for(i=0;i<table_size;i++) {
371
if (n > table_nb_bits) {
375
index = build_table(vlc, n, nb_codes,
376
bits, bits_wrap, bits_size,
377
codes, codes_wrap, codes_size,
378
(code_prefix << table_nb_bits) | i,
379
n_prefix + table_nb_bits);
382
/* note: realloc has been done, so reload tables */
383
table_bits = &vlc->table_bits[table_index];
384
table_codes = &vlc->table_codes[table_index];
385
table_codes[i] = index;
392
/* Build VLC decoding tables suitable for use with get_vlc().
394
'nb_bits' set thee decoding table size (2^nb_bits) entries. The
395
bigger it is, the faster is the decoding. But it should not be too
396
big to save memory and L1 cache. '9' is a good compromise.
398
'nb_codes' : number of vlcs codes
400
'bits' : table which gives the size (in bits) of each vlc code.
402
'codes' : table which gives the bit pattern of of each vlc code.
404
'xxx_wrap' : give the number of bytes between each entry of the
405
'bits' or 'codes' tables.
407
'xxx_size' : gives the number of bytes of each entry of the 'bits'
410
'wrap' and 'size' allows to use any memory configuration and types
411
(byte/word/long) to store the 'bits' and 'codes' tables.
413
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
414
const void *bits, int bits_wrap, int bits_size,
415
const void *codes, int codes_wrap, int codes_size)
418
vlc->table_bits = NULL;
419
vlc->table_codes = NULL;
420
vlc->table_allocated = 0;
423
printf("build table nb_codes=%d\n", nb_codes);
426
if (build_table(vlc, nb_bits, nb_codes,
427
bits, bits_wrap, bits_size,
428
codes, codes_wrap, codes_size,
431
free(vlc->table_bits);
432
if (vlc->table_codes)
433
free(vlc->table_codes);
440
void free_vlc(VLC *vlc)
442
free(vlc->table_bits);
443
free(vlc->table_codes);
446
int get_vlc(GetBitContext *s, VLC *vlc)
448
int bit_cnt, code, n, nb_bits, index;
456
table_codes = vlc->table_codes;
457
table_bits = vlc->table_bits;
459
SHOW_BITS(s, index, nb_bits);
460
code = table_codes[index];
461
n = table_bits[index];
463
/* most common case */
466
st_bit_counts[st_current_index] += n;
474
st_bit_counts[st_current_index] += nb_bits;
477
table_codes = vlc->table_codes + code;
478
table_bits = vlc->table_bits + code;