add unary methods, read CRC-16 support, get rid of gotos

This commit is contained in:
Josh Coalson
2001-03-27 01:17:07 +00:00
parent d30d894ad0
commit ee5cfb5010

View File

@@ -21,6 +21,7 @@
#include <stdlib.h> /* for malloc() */ #include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcpy(), memset() */ #include <string.h> /* for memcpy(), memset() */
#include "private/bitbuffer.h" #include "private/bitbuffer.h"
#include "private/crc.h"
static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = 65536; /* bytes */ static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = 65536; /* bytes */
@@ -44,24 +45,24 @@ static unsigned ilog2_(unsigned v)
static unsigned silog2_(int v) static unsigned silog2_(int v)
{ {
doit_: while(1) {
if(v == 0) { if(v == 0) {
return 0; return 0;
} }
else if(v > 0) { else if(v > 0) {
unsigned l = 0; unsigned l = 0;
while(v) { while(v) {
l++; l++;
v >>= 1; v >>= 1;
}
return l+1;
}
else if(v == -1) {
return 2;
}
else {
v = -(++v);
} }
return l+1;
}
else if(v == -1) {
return 2;
}
else {
v = -(++v);
goto doit_;
} }
} }
@@ -75,6 +76,7 @@ static bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
if(bb->capacity == new_capacity) if(bb->capacity == new_capacity)
return true; return true;
fprintf(stderr,"@@@ bitbuffer_resize_(bb=%p, capacity=%u, new_capactity=%u)\n",bb,bb->capacity,new_capacity);
new_buffer = (byte*)malloc(sizeof(byte) * new_capacity); new_buffer = (byte*)malloc(sizeof(byte) * new_capacity);
if(new_buffer == 0) if(new_buffer == 0)
return false; return false;
@@ -85,6 +87,7 @@ static bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
bb->bits = 0; bb->bits = 0;
bb->total_bits = (new_capacity<<3); bb->total_bits = (new_capacity<<3);
} }
//@@@ boy oh boy, is this a bug? maybe should drop the 'if' statement
if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) { if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
bb->consumed_bytes = new_capacity; bb->consumed_bytes = new_capacity;
bb->consumed_bits = 0; bb->consumed_bits = 0;
@@ -174,10 +177,18 @@ bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const byte buffer[], unsigne
} }
} }
void FLAC__bitbuffer_init_read_crc16(FLAC__BitBuffer *bb, uint16 seed)
{
assert(bb != 0);
bb->read_crc16 = seed;
}
bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src) bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
{ {
static byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; static const byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
unsigned bits_to_add = src->total_bits - src->total_consumed_bits; unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
assert(dest != 0); assert(dest != 0);
assert(src != 0); assert(src != 0);
@@ -246,6 +257,7 @@ bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
dest->consumed_bytes = src->consumed_bytes; dest->consumed_bytes = src->consumed_bytes;
dest->consumed_bits = src->consumed_bits; dest->consumed_bits = src->consumed_bits;
dest->total_consumed_bits = src->total_consumed_bits; dest->total_consumed_bits = src->total_consumed_bits;
dest->read_crc16 = src->read_crc16;
return true; return true;
} }
@@ -277,7 +289,7 @@ bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits) bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits)
{ {
static uint32 mask[] = { static const uint32 mask[] = {
0, 0,
0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF, 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
@@ -351,7 +363,7 @@ bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bi
bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits) bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
{ {
static uint64 mask[] = { static const uint64 mask[] = {
0, 0,
0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F, 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF, 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
@@ -423,6 +435,19 @@ bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bi
return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits); return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
} }
bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
{
if(val < 32)
return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
else if(val < 64)
return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
else {
if(!FLAC__bitbuffer_write_zeroes(bb, val))
return false;
return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
}
}
unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter) unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
{ {
unsigned msbs, uval; unsigned msbs, uval;
@@ -949,7 +974,7 @@ bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data) bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
{ {
static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 }; static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
/* to avoid a drastic speed penalty we don't: /* to avoid a drastic speed penalty we don't:
assert(bb != 0); assert(bb != 0);
@@ -957,21 +982,21 @@ bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_ca
assert(bb->bits == 0); assert(bb->bits == 0);
*/ */
read_bit_: while(1) {
if(bb->total_consumed_bits < bb->total_bits) { if(bb->total_consumed_bits < bb->total_bits) {
*val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0; *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
return true; return true;
} }
else { else {
if(!bitbuffer_read_from_client_(bb, read_callback, client_data)) if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
return false; return false;
goto read_bit_; }
} }
} }
bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data) bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
{ {
static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 }; static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
/* to avoid a drastic speed penalty we don't: /* to avoid a drastic speed penalty we don't:
assert(bb != 0); assert(bb != 0);
@@ -979,27 +1004,28 @@ bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_ca
assert(bb->bits == 0); assert(bb->bits == 0);
*/ */
read_bit_: while(1) {
if(bb->total_consumed_bits < bb->total_bits) { if(bb->total_consumed_bits < bb->total_bits) {
*val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0; *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
bb->consumed_bits++; bb->consumed_bits++;
if(bb->consumed_bits == 8) { if(bb->consumed_bits == 8) {
bb->consumed_bytes++; FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
bb->consumed_bits = 0; bb->consumed_bytes++;
bb->consumed_bits = 0;
}
bb->total_consumed_bits++;
return true;
}
else {
if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
return false;
} }
bb->total_consumed_bits++;
return true;
}
else {
if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
return false;
goto read_bit_;
} }
} }
bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, uint32 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data) bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, uint32 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
{ {
static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 }; static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
/* to avoid a drastic speed penalty we don't: /* to avoid a drastic speed penalty we don't:
assert(bb != 0); assert(bb != 0);
@@ -1007,28 +1033,29 @@ bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, uint32 *val, bool (
assert(bb->bits == 0); assert(bb->bits == 0);
*/ */
read_bit_: while(1) {
if(bb->total_consumed_bits < bb->total_bits) { if(bb->total_consumed_bits < bb->total_bits) {
*val <<= 1; *val <<= 1;
*val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0; *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
bb->consumed_bits++; bb->consumed_bits++;
if(bb->consumed_bits == 8) { if(bb->consumed_bits == 8) {
bb->consumed_bytes++; FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
bb->consumed_bits = 0; bb->consumed_bytes++;
bb->consumed_bits = 0;
}
bb->total_consumed_bits++;
return true;
}
else {
if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
return false;
} }
bb->total_consumed_bits++;
return true;
}
else {
if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
return false;
goto read_bit_;
} }
} }
bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, uint64 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data) bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, uint64 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
{ {
static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 }; static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
/* to avoid a drastic speed penalty we don't: /* to avoid a drastic speed penalty we don't:
assert(bb != 0); assert(bb != 0);
@@ -1036,22 +1063,23 @@ bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, uint64 *val, bool (
assert(bb->bits == 0); assert(bb->bits == 0);
*/ */
read_bit_: while(1) {
if(bb->total_consumed_bits < bb->total_bits) { if(bb->total_consumed_bits < bb->total_bits) {
*val <<= 1; *val <<= 1;
*val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0; *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
bb->consumed_bits++; bb->consumed_bits++;
if(bb->consumed_bits == 8) { if(bb->consumed_bits == 8) {
bb->consumed_bytes++; FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
bb->consumed_bits = 0; bb->consumed_bytes++;
bb->consumed_bits = 0;
}
bb->total_consumed_bits++;
return true;
}
else {
if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
return false;
} }
bb->total_consumed_bits++;
return true;
}
else {
if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
return false;
goto read_bit_;
} }
} }
@@ -1145,6 +1173,27 @@ bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, int64 *val, unsigned bi
return true; return true;
} }
bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
{
unsigned bit, val_ = 0;
assert(bb != 0);
assert(bb->buffer != 0);
assert(bits <= 32);
while(1) {
if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
return false;
if(bit)
break;
else
val_++;
}
*val = val_;
return true;
}
bool FLAC__bitbuffer_read_symmetric_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data) bool FLAC__bitbuffer_read_symmetric_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
{ {
uint32 sign = 0, lsbs = 0, msbs = 0; uint32 sign = 0, lsbs = 0, msbs = 0;
@@ -1336,23 +1385,24 @@ bool FLAC__bitbuffer_read_utf8_uint32(FLAC__BitBuffer *bb, uint32 *val, bool (*r
v = x & 0x01; v = x & 0x01;
i = 5; i = 5;
} }
else else {
goto invalid_; *val = 0xffffffff;
return true;
}
for( ; i; i--) { for( ; i; i--) {
if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data)) if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
return false; return false;
if(raw) if(raw)
raw[(*rawlen)++] = (byte)x; raw[(*rawlen)++] = (byte)x;
if(!(x & 0x80) || (x & 0x40)) /* 10xxxxxx */ if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
goto invalid_; *val = 0xffffffff;
return true;
}
v <<= 6; v <<= 6;
v |= (x & 0x3F); v |= (x & 0x3F);
} }
*val = v; *val = v;
return true; return true;
invalid_:
*val = 0xffffffff;
return true;
} }
/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */ /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
@@ -1394,23 +1444,24 @@ bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, uint64 *val, bool (*r
v = 0; v = 0;
i = 6; i = 6;
} }
else else {
goto invalid_; *val = 0xffffffffffffffff;
return true;
}
for( ; i; i--) { for( ; i; i--) {
if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data)) if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
return false; return false;
if(raw) if(raw)
raw[(*rawlen)++] = (byte)x; raw[(*rawlen)++] = (byte)x;
if(!(x & 0x80) || (x & 0x40)) /* 10xxxxxx */ if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
goto invalid_; *val = 0xffffffffffffffff;
return true;
}
v <<= 6; v <<= 6;
v |= (x & 0x3F); v |= (x & 0x3F);
} }
*val = v; *val = v;
return true; return true;
invalid_:
*val = 0xffffffff;
return true;
} }
void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out) void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)