3 * (C) 2008/03/09- yoya@awm.jp
11 static void bitstream_clear(bitstream_t *bs);
14 #ifdef BITSTREAM_DEBUG /* bitstream debug */
17 #undef bitstream_close
19 #define BITSTREAM_DEBUG_TABLE_NUM 0x40000
21 static struct bitstream_debug_ {
25 } bitstream_debug_table[BITSTREAM_DEBUG_TABLE_NUM];
27 static int bitstream_debug_stack = 0;
30 bitstream_debug_start(void) {
32 bitstream_debug_stack ++;
33 if (bitstream_debug_stack > 1) {
34 fprintf(stderr, "bitstream_debug_start: bitstream_debug_stack=%d\n", bitstream_debug_stack);
37 for (i=0 ; i < BITSTREAM_DEBUG_TABLE_NUM ; i++) {
38 bitstream_debug_table[i].ptr = NULL;
40 fprintf(stderr, "bitstream_debug_start: 0/n=0/%d\n", BITSTREAM_DEBUG_TABLE_NUM);
44 bitstream_debug_end(void) {
46 bitstream_debug_stack --;
47 if (bitstream_debug_stack > 0) {
48 fprintf(stderr, "bitstream_debug_end: bitstream_debug_stack=%d\n", bitstream_debug_stack);
51 for (i=0 ; i < BITSTREAM_DEBUG_TABLE_NUM ; i++) {
52 if (bitstream_debug_table[i].ptr) {
53 fprintf(stderr, "XXX (%d) ptr=%p (%s, %d)\n",
54 i, bitstream_debug_table[i].ptr,
55 bitstream_debug_table[i].filename,
56 bitstream_debug_table[i].linenum);
60 fprintf(stderr, "bitstream_debug_end: j/n=%d/%d\n", j, BITSTREAM_DEBUG_TABLE_NUM);
64 bitstream_open_debug(char *filename, int linenum) {
67 ptr = bitstream_open();
68 for (i=0 ; i < BITSTREAM_DEBUG_TABLE_NUM ; i++) {
69 if (bitstream_debug_table[i].ptr == NULL) {
70 bitstream_debug_table[i].ptr = ptr;
71 bitstream_debug_table[i].filename = filename;
72 bitstream_debug_table[i].linenum = linenum;
80 bitstream_close_debug(bitstream_t * bs, char *filename, int linenum) {
83 // fprintf(stderr, "free_debug: ptr=%p (%s,%d)\n", ptr, filename, linenum);
84 for (i=0 ; i < BITSTREAM_DEBUG_TABLE_NUM ; i++) {
85 if (bitstream_debug_table[i].ptr == ptr) {
86 bitstream_debug_table[i].ptr = NULL;
90 if (i == BITSTREAM_DEBUG_TABLE_NUM) {
92 fprintf(stderr, "free non-allocated memory: ptr=%p (%s,%d)\n", ptr,
94 bitstream_debug_end();
101 #endif // BITSTREAM_DEBUG
104 bitstream_open(void) {
105 bitstream_t *bs = (bitstream_t *) calloc(sizeof(*bs), 1);
109 bs->data_alloc_len = 0;
116 bitstream_close(bitstream_t * bs) {
126 bitstream_clear(bitstream_t *bs) {
132 bs->data_alloc_len = 0;
139 bitstream_realloc(bitstream_t *bs) {
141 bs->data_alloc_len *= 2;
142 if (bs->data_alloc_len < BITSTREAM_DATA_LEN_MIN) {
143 bs->data_alloc_len = BITSTREAM_DATA_LEN_MIN;
145 data = (unsigned char *) realloc(bs->data, bs->data_alloc_len);
147 fprintf(stderr, "bitstream_realloc: Can't realloc memory (%p, %lu)\n",
148 bs->data, bs->data_alloc_len);
156 bitstream_input(bitstream_t *bs, unsigned char *data,
157 unsigned long data_len) {
159 bs->data_alloc_len = data_len;
163 bs->data = malloc(bs->data_alloc_len);
164 if (bs->data == NULL) {
165 fprintf(stderr, "bitstream_input: malloc failed\n");
166 bs->data_alloc_len = 0;
170 memcpy(bs->data, data, data_len);
171 bs->data_len = data_len;
176 bitstream_steal(bitstream_t *bs, unsigned long *length) {
177 unsigned char *data, *tmp;
180 fprintf(stderr, "bitstream_steal: bs == NULL\n");
184 *length = bs->data_len;
185 if ((tmp = realloc(data, *length)) == NULL) {
186 fprintf(stderr, "bitstream_steal: Can't realloc\n");
191 bs->data_alloc_len = 0;
196 bitstream_output_sub(bitstream_t *bs, unsigned long offset, unsigned long length) {
199 fprintf(stderr, "bitstream_output_sub: bs == NULL\n");
202 if (bs->data_len < offset + length ) {
203 fprintf(stderr, "bitstream_output_sub: bs->data_len(%lu) < offset(%lu)+length(%lu)\n",
204 bs->data_len, offset, length);
207 data = malloc(length);
209 fprintf(stderr, "bitstream_output_sub: Can't malloc\n");
212 memcpy(data, bs->data + offset, length);
221 bitstream_putbyte(bitstream_t *bs, int byte) {
223 if (bs->data_len < bs->byte_offset ) {
226 if (bs->data_len == bs->byte_offset ) {
227 if (bs->data_alloc_len <= bs->byte_offset ) {
228 bitstream_realloc(bs);
233 bs->data[bs->byte_offset++] = (unsigned char) byte ;
238 bitstream_getbyte(bitstream_t *bs) {
240 if (bs->data_len <= bs->byte_offset) {
241 return -1; /* End of Stream */
243 return bs->data[bs->byte_offset++] & 0xff;
247 bitstream_putstring(bitstream_t *bs,
248 unsigned char *data, signed long data_len) {
250 if (bs->data_len < bs->byte_offset ) {
253 while(bs->data_alloc_len < bs->byte_offset + data_len) {
254 bitstream_realloc(bs);
256 bs->data_len = bs->byte_offset + data_len;
257 memcpy(bs->data + bs->byte_offset, data, data_len);
258 bs->byte_offset += data_len;
263 bitstream_getstring(bitstream_t *bs,
264 unsigned char *data, signed long data_len) {
266 if (bs->data_len < bs->byte_offset + data_len) {
267 return -1; /* End of Stream */
269 memcpy(data, bs->data + bs->byte_offset, data_len);
270 bs->byte_offset += data_len;
275 bitstream_outputstring(bitstream_t *bs) {
277 register unsigned long data_len;
279 data_len = strlen((char *) bs->data + bs->byte_offset);
280 data_len += 1; // + '\0'
281 if (bs->data_len < bs->byte_offset + data_len) {
282 return NULL; /* End of Stream */
284 data = malloc(data_len);
286 fprintf(stderr, "bitstream_outputstring: can't malloc\n");
289 memcpy(data, bs->data + bs->byte_offset, data_len);
290 bs->byte_offset += data_len;
295 bitstream_putbytesLE(bitstream_t *bs, unsigned long bytes, int byte_width) {
297 register unsigned long tmp_bytes = bytes;
298 for (i=0 ; i < byte_width ; i++) {
299 bitstream_putbyte(bs, tmp_bytes & 0xff);
306 bitstream_putbytesBE(bitstream_t *bs, unsigned long bytes, int byte_width) {
308 register unsigned long tmp_bytes = bytes;
309 for (i=0 ; i < byte_width ; i++) {
310 bitstream_putbyte(bs, (tmp_bytes >> ( 8 * (byte_width - 1 - i))) & 0xff);
316 bitstream_getbytesLE(bitstream_t *bs, int byte_width) {
319 register unsigned long bytes = 0;
320 for (i=0 ; i < byte_width ; i++) {
321 byte = bitstream_getbyte(bs);
323 return -1; // End of Stream;
332 bitstream_getbytesBE(bitstream_t *bs, int byte_width) {
335 register unsigned long bytes = 0;
336 for (i=0 ; i < byte_width ; i++) {
338 byte = bitstream_getbyte(bs);
340 return -1; // End of Stream;
352 bitstream_putbit(bitstream_t *bs, int bit) {
353 if (bs->data_len <= bs->byte_offset) {
354 if (bs->data_alloc_len <= bs->byte_offset) {
355 // fprintf(stderr, "bitstream_putbit: alloc_len=%lu\n", bs->data_alloc_len);
356 bitstream_realloc(bs);
358 bs->data[bs->byte_offset] = 0;
361 bs->data[bs->byte_offset] |= (bit & 1) << (7 - bs->bit_offset);
362 bitstream_incrpos(bs, 0, 1);
366 bitstream_getbit(bitstream_t *bs) {
368 if (bs->data_len <= bs->byte_offset) {
369 fprintf(stderr, "bitstream_getbit: bs->data_len(%lu) <= bs->byte_offset(%lu)\n",
370 bs->data_len, bs->byte_offset);
371 return -1; /* End of Stream */
373 bit = bs->data[bs->byte_offset] >> (7 - bs->bit_offset);
374 bitstream_incrpos(bs, 0, 1);
379 bitstream_putbits(bitstream_t *bs, unsigned long bits, int bit_width) {
380 register int i = bit_width;
382 bitstream_putbit(bs, (bits >> i) & 1);
388 bitstream_putbits_signed(bitstream_t *bs, signed long bits, int bit_width) {
390 register signed long msb = 1 << (bit_width - 1);
391 register signed long bitmask = (msb << 1) - 1;
392 bits = (-bits - 1) ^ bitmask;
394 return bitstream_putbits(bs, bits, bit_width);
397 #if BITOPERATION_OPTIMIZE == 1
399 bitstream_getbits(bitstream_t *bs, int bit_width) {
400 register unsigned long bits;
401 register int byte_offset = bs->byte_offset;
402 register int bit_offset = bs->bit_offset;
403 if (bs->data_len <= byte_offset + (bit_offset + bit_width) >> 3) {
404 fprintf(stderr, "bitstream_getbits: bs->data_len(%lu) <= byte_offset(%lu) + (bit_offset(%lu) + bit_width(%d)) >> 3\n",
405 bs->data_len, bs->byte_offset, bs->bit_offset , bit_width);
406 return -1; /* End of Stream */
408 if ((bit_offset + bit_width) < 9) {
409 bits = (bs->data[byte_offset] >> (8 - bit_offset - bit_width)) & bitstream_bitmask_list[bit_width];
410 bit_offset += bit_width;
412 register int len = 8 - bit_offset;
413 bits = bs->data[byte_offset] & bitstream_bitmask_list[len];
419 bits |= ((bs->data[++byte_offset] >> (8 - bit_width))) & bitstream_bitmask_list[bit_width];
420 bit_offset += bit_width - 8;
423 bits |= bs->data[++byte_offset];
429 bitstream_setpos(bs, byte_offset, bit_offset);
432 #else // BITOPERATION_OPTIMIZE
434 bitstream_getbits(bitstream_t *bs, int bit_width) {
436 register unsigned long bits = 0;
437 for (i=0 ; i < bit_width ; i++) {
438 bit = bitstream_getbit(bs);
442 bits |= bit << (bit_width - 1 - i);
446 #endif // BITOPERATION_OPTIMIZE
449 bitstream_getbits_signed(bitstream_t *bs, int bit_width) {
450 register signed long bits = bitstream_getbits(bs, bit_width);
451 register signed long msb = bits & (1 << (bit_width - 1));
453 register signed long bitmask = (msb << 1) - 1;
454 bits = - (bits ^ bitmask) - 1;
460 bitstream_align(bitstream_t *bs) {
461 if (bs->bit_offset > 0) {
472 bitstream_incrpos(bitstream_t *bs, signed long byte_incr,
473 signed long bit_incr) {
474 register signed long byte_offset, bit_offset;
476 byte_offset = bs->byte_offset - ((-bit_incr + 7) >> 3);
477 bit_offset = bs->bit_offset + (bit_incr % 8) + 8;
479 byte_offset = bs->byte_offset + byte_incr;
480 bit_offset = bs->bit_offset + bit_incr;
482 if (bit_offset < 8) {
483 bs->byte_offset = byte_offset;
484 bs->bit_offset = bit_offset;
486 bs->byte_offset = byte_offset + (bit_offset >> 3);
487 bs->bit_offset = bit_offset & 7;
493 bitstream_setpos(bitstream_t *bs, unsigned long byte_offset,
494 unsigned long bit_offset) {
495 if (bs->data_len < byte_offset ) {
496 fprintf(stderr, "bitstream_setpos: bs->data_len(%lu) <= byte_offset(%lu)\n",
497 bs->data_len, byte_offset);
499 bs->byte_offset = byte_offset;
500 bs->bit_offset = bit_offset;
505 bitstream_getbytepos(bitstream_t *bs) {
506 return bs->byte_offset;
510 bitstream_getbitpos(bitstream_t *bs) {
511 return bs->bit_offset;
519 bitstream_buffer(bitstream_t *bs, unsigned long byte_offset) {
520 return bs->data + byte_offset;
524 bitstream_length(bitstream_t *bs) {
533 bitstream_unsigned2signed(unsigned long num, int size) {
534 register unsigned long sig_bit = 1 << (size - 1);
535 if ((sig_bit & num) == 0) {
536 return (signed long) num;
538 unsigned long mask = sig_bit - 1;
539 return - ((num^mask) & mask) - 1;
544 bitstream_signed2unsigned(signed long num, int size) { // XXX check me!
546 return (unsigned long) num;
548 unsigned long sig_bit = 1 << (size - 1);
549 unsigned long mask = sig_bit - 1;
550 return - ((num^mask) & mask) - 1;
555 bitstream_need_bits_unsigned(unsigned long n) {
557 for (i = 0 ; n ; i++) {
564 bitstream_need_bits_signed(signed long n) {
571 for (i = 0 ; n ; i++) {
587 bitstream_iserror(bitstream_t *bs) {
593 bitstream_printerror(bitstream_t *bs) {
602 bitstream_hexdump(bitstream_t *bs, int length) {
603 register unsigned long i, j;
604 for ( i = bs->byte_offset ; i < bs->byte_offset + length ; i++) {
605 if ((i == bs->byte_offset) || (i%16) == 0) {
606 printf("%08lu: ", i);
608 for( j = 0 ; j < (i%16) ; j++) {
613 printf("%02x ", bs->data[i] & 0xff);
627 bitstream_print(bitstream_t *bs) {
628 printf("data=%p data_len=%lu data_alloc_len=%lu\n",
629 bs->data, bs->data_len, bs->data_alloc_len);
630 printf("byte_offset=%lu bit_offset=%lu\n",
631 bs->byte_offset, bs->bit_offset);