3 * (C) 2008/03/09- yoya@awm.jp
11 static void bitstream_clear(bitstream_t *bs);
14 bitstream_open(void) {
15 bitstream_t *bs = (bitstream_t *) calloc(sizeof(*bs), 1);
18 bs->data_alloc_len = 0;
25 bitstream_close(bitstream_t * bs) {
35 bitstream_clear(bitstream_t *bs) {
41 bs->data_alloc_len = 0;
48 bitstream_realloc(bitstream_t *bs) {
50 bs->data_alloc_len *= 2;
51 if (bs->data_alloc_len < BITSTREAM_DATA_LEN_MIN) {
52 bs->data_alloc_len = BITSTREAM_DATA_LEN_MIN;
54 data = (unsigned char *) realloc(bs->data, bs->data_alloc_len);
56 fprintf(stderr, "bitstream_realloc: Can't realloc memory (%p, %lu)\n",
57 bs->data, bs->data_alloc_len);
65 bitstream_input(bitstream_t *bs, unsigned char *data,
66 unsigned long data_len) {
68 bs->data_alloc_len = data_len;
69 bs->data = malloc(bs->data_alloc_len);
70 if (bs->data == NULL) {
71 fprintf(stderr, "bitstream_input: malloc failed\n");
72 bs->data_alloc_len = 0;
76 memcpy(bs->data, data, data_len);
77 bs->data_len = data_len;
82 bitstream_steal(bitstream_t *bs, unsigned long *length) {
83 unsigned char *data, *tmp;
86 fprintf(stderr, "bitstream_steal: bs == NULL\n");
90 *length = bs->data_len;
91 if ((tmp = realloc(data, *length)) == NULL) {
92 fprintf(stderr, "bitstream_steal: Can't realloc\n");
97 bs->data_alloc_len = 0;
102 bitstream_output_sub(bitstream_t *bs, unsigned long offset, unsigned long length) {
105 fprintf(stderr, "bitstream_output_sub: bs == NULL\n");
108 if (bs->data_len < offset + length ) {
109 fprintf(stderr, "bitstream_output_sub: bs->data_len(%lu) < offset(%lu)+length(%lu)\n",
110 bs->data_len, offset, length);
113 data = malloc(length);
115 fprintf(stderr, "bitstream_output_sub: Can't malloc\n");
118 memcpy(data, bs->data + offset, length);
127 bitstream_putbyte(bitstream_t *bs, int byte) {
129 if (bs->data_len < bs->byte_offset ) {
132 if (bs->data_len == bs->byte_offset ) {
133 if (bs->data_alloc_len <= bs->byte_offset ) {
134 bitstream_realloc(bs);
139 bs->data[bs->byte_offset] = (unsigned char) byte ;
145 bitstream_getbyte(bitstream_t *bs) {
148 if (bs->data_len <= bs->byte_offset) {
149 return -1; /* End of Stream */
151 byte = bs->data[bs->byte_offset] & 0xff;
157 bitstream_putstring(bitstream_t *bs,
158 unsigned char *data, signed long data_len) {
160 if (bs->data_len < bs->byte_offset ) {
163 while(bs->data_alloc_len < bs->byte_offset + data_len) {
164 bitstream_realloc(bs);
166 bs->data_len = bs->byte_offset + data_len;
167 memcpy(bs->data + bs->byte_offset, data, data_len);
168 bs->byte_offset += data_len;
173 bitstream_getstring(bitstream_t *bs,
174 unsigned char *data, signed long data_len) {
176 if (bs->data_len < bs->byte_offset + data_len) {
177 return -1; /* End of Stream */
179 memcpy(data, bs->data + bs->byte_offset, data_len);
180 bs->byte_offset += data_len;
185 bitstream_outputstring(bitstream_t *bs) {
187 unsigned long data_len;
189 data_len = strlen((char *) bs->data + bs->byte_offset);
190 data_len += 1; // + '\0'
191 if (bs->data_len < bs->byte_offset + data_len) {
192 return NULL; /* End of Stream */
194 data = malloc(data_len);
196 fprintf(stderr, "bitstream_outputstring: can't malloc\n");
199 memcpy(data, bs->data + bs->byte_offset, data_len);
200 bs->byte_offset += data_len;
205 bitstream_putbytesLE(bitstream_t *bs, unsigned long bytes, int byte_width) {
208 for (i=0 ; i < byte_width ; i++) {
210 bitstream_putbyte(bs, byte);
217 bitstream_putbytesBE(bitstream_t *bs, unsigned long bytes, int byte_width) {
220 for (i=0 ; i < byte_width ; i++) {
221 byte = bytes >> ( 8 * (byte_width - 1 - i));
222 bitstream_putbyte(bs, byte & 0xff);
228 bitstream_getbytesLE(bitstream_t *bs, int byte_width) {
230 unsigned long byte, bytes = 0;
231 for (i=0 ; i < byte_width ; i++) {
232 byte = bitstream_getbyte(bs);
240 bitstream_getbytesBE(bitstream_t *bs, int byte_width) {
242 unsigned long byte, bytes = 0;
243 for (i=0 ; i < byte_width ; i++) {
245 byte = bitstream_getbyte(bs);
256 bitstream_putbit(bitstream_t *bs, int bit) {
258 if (bs->data_len <= bs->byte_offset) {
259 // fprintf(stderr, "bs->data_len(%ld) <= bs->byte_offset(%ld)\n",
260 // bs->data_len, bs->byte_offset);
261 if (bs->data_alloc_len <= bs->byte_offset) {
262 // fprintf(stderr, "bitstream_putbit: alloc_len=%lu\n", bs->data_alloc_len);
263 bitstream_realloc(bs);
265 bs->data[bs->byte_offset] = 0;
270 byte = bs->data[bs->byte_offset];
271 byte |= bit << (7 - bs->bit_offset);
272 bs->data[bs->byte_offset] = byte;
273 bitstream_incrpos(bs, 0, 1);
277 bitstream_getbit(bitstream_t *bs) {
279 if (bs->data_len <= bs->byte_offset) {
280 fprintf(stderr, "bitstream_getbit: bs->data_len(%ld) <= bs->byte_offset(%ld)\n",
281 bs->data_len, bs->byte_offset);
282 return -1; /* End of Stream */
284 byte = bs->data[bs->byte_offset];
285 bit = byte >> (7 - bs->bit_offset);
286 bitstream_incrpos(bs, 0, 1);
291 bitstream_putbits(bitstream_t *bs, unsigned long bits, int bit_width) {
293 for (i=0 ; i < bit_width ; i++) {
294 bit = bits >> (bit_width - 1 - i);
296 bitstream_putbit(bs, bit);
302 bitstream_putbits_signed(bitstream_t *bs, signed long bits, int bit_width) {
304 register signed long msb = 1 << (bit_width - 1);
305 register signed long bitmask = (2 * msb) - 1;
306 bits = (-bits - 1) ^ bitmask;
308 return bitstream_putbits(bs, bits, bit_width);
312 bitstream_getbits(bitstream_t *bs, int bit_width) {
315 unsigned long bits = 0;
316 for (i=0 ; i < bit_width ; i++) {
317 bit = bitstream_getbit(bs);
321 bits |= bit << (bit_width - 1 - i);
327 bitstream_getbits_signed(bitstream_t *bs, int bit_width) {
328 signed long bits = bitstream_getbits(bs, bit_width);
329 register signed long msb = bits & (1 << (bit_width - 1));
331 register signed long bitmask = (2 * msb) - 1;
332 bits = - (bits ^ bitmask) - 1;
338 bitstream_align(bitstream_t *bs) {
339 if (bs->bit_offset > 0) {
350 bitstream_incrpos(bitstream_t *bs, signed long byte_incr,
351 signed long bit_incr) {
352 while (bit_incr < 0) {
356 bs->byte_offset += byte_incr;
357 bs->bit_offset += bit_incr;
358 while (bs->bit_offset >= 8) {
366 bitstream_setpos(bitstream_t *bs, unsigned long byte_offset,
367 unsigned long bit_offset) {
368 if (bs->data_len <= byte_offset ) {
369 fprintf(stderr, "bitstream_setpos: bs->data_len(%ld) <= byte_offset(%ld)\n",
370 bs->data_len, byte_offset);
372 bs->byte_offset = byte_offset;
373 bs->bit_offset = bit_offset;
378 bitstream_getbytepos(bitstream_t *bs) {
379 return bs->byte_offset;
383 bitstream_getbitpos(bitstream_t *bs) {
384 return bs->bit_offset;
392 bitstream_buffer(bitstream_t *bs, unsigned long byte_offset) {
393 return bs->data + byte_offset;
397 bitstream_length(bitstream_t *bs) {
406 bitstream_unsigned2signed(unsigned long num, int size) {
407 unsigned long sig_bit = 1 << (size - 1);
408 if ((sig_bit & num) == 0) {
409 return (signed long) num;
411 unsigned long mask = sig_bit - 1;
412 return - ((num^mask) & mask) - 1;
417 bitstream_signed2unsigned(signed long num, int size) { // XXX check me!
419 return (unsigned long) num;
421 unsigned long sig_bit = 1 << (size - 1);
422 unsigned long mask = sig_bit - 1;
423 return - ((num^mask) & mask) - 1;
428 bitstream_need_bits_unsigned(unsigned long n) {
430 for (i = 0 ; n ; i++) {
437 bitstream_need_bits_signed(signed long n) {
444 for (i = 0 ; n ; i++) {
460 bitstream_iserror(bitstream_t *bs) {
466 bitstream_printerror(bitstream_t *bs) {
475 bitstream_hexdump(bitstream_t *bs, int length) {
477 for ( i = bs->byte_offset ; i < bs->byte_offset + length ; i++) {
478 if ((i == bs->byte_offset) || (i%16) == 0) {
479 printf("%08lu: ", i);
481 for( j = 0 ; j < (i%16) ; j++) {
486 printf("%02x ", bs->data[i] & 0xff);
500 bitstream_print(bitstream_t *bs) {
501 printf("data=%p data_len=%lu data_alloc_len=%lu\n",
502 bs->data, bs->data_len, bs->data_alloc_len);
503 printf("byte_offset=%lu bit_offset=%lu\n",
504 bs->byte_offset, bs->bit_offset);