From 4f569c6386f7b5490ef33b671ac48a097ab56cce Mon Sep 17 00:00:00 2001 From: Josh Coalson Date: Sat, 17 Mar 2001 00:13:09 +0000 Subject: [PATCH] add symmetric rice coding back in --- src/libFLAC/bitbuffer.c | 157 ++++++++++++++++++++++++++++---- src/libFLAC/include/private/bitbuffer.h | 3 + 2 files changed, 140 insertions(+), 20 deletions(-) diff --git a/src/libFLAC/bitbuffer.c b/src/libFLAC/bitbuffer.c index 71110c1..041b835 100644 --- a/src/libFLAC/bitbuffer.c +++ b/src/libFLAC/bitbuffer.c @@ -485,9 +485,91 @@ unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter) return bits; } +bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter) +{ + unsigned total_bits, interesting_bits, msbs; + uint32 pattern; + + assert(bb != 0); + assert(bb->buffer != 0); + assert(parameter <= 31); + + /* init pattern with the unary end bit and the sign bit */ + if(val < 0) { + pattern = 3; + val = -val; + } + else + pattern = 2; + + msbs = val >> parameter; + interesting_bits = 2 + parameter; + total_bits = interesting_bits + msbs; + pattern <<= parameter; + pattern |= (val & ((1<buffer != 0); + assert(parameter <= 31); + + *overflow = false; + + /* init pattern with the unary end bit and the sign bit */ + if(val < 0) { + pattern = 3; + val = -val; + } + else + pattern = 2; + + msbs = val >> parameter; + interesting_bits = 2 + parameter; + total_bits = interesting_bits + msbs; + pattern <<= parameter; + pattern |= (val & ((1< max_bits) { + *overflow = true; + return true; + } + else { + /* write the unary MSBs */ + if(!FLAC__bitbuffer_write_zeroes(bb, msbs)) + return false; + /* write the unary end bit, the sign bit, and binary LSBs */ + if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits)) + return false; + } + return true; +} + bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter) { - unsigned bits, msbs, uval; + unsigned total_bits, interesting_bits, msbs, uval; uint32 pattern; assert(bb != 0); @@ -505,12 +587,13 @@ bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned pa uval = (unsigned)(val << 1); msbs = uval >> parameter; - bits = 1 + parameter + msbs; + interesting_bits = 1 + parameter; + total_bits = interesting_bits + msbs; pattern = 1 << parameter; /* the unary end bit */ pattern |= (uval & ((1<> parameter; - bits = 1 + parameter + msbs; + interesting_bits = 1 + parameter; + total_bits = interesting_bits + msbs; pattern = 1 << parameter; /* the unary end bit */ pattern |= (uval & ((1< max_bits) { + else if(total_bits > max_bits) { *overflow = true; return true; } @@ -563,7 +647,7 @@ bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, uns if(!FLAC__bitbuffer_write_zeroes(bb, msbs)) return false; /* write the unary end bit and binary LSBs */ - if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, parameter+1)) + if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits)) return false; } return true; @@ -571,7 +655,7 @@ bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, uns bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter) { - unsigned bits, msbs, uval; + unsigned total_bits, msbs, uval; unsigned k; assert(bb != 0); @@ -595,12 +679,12 @@ bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned assert(k <= 30); msbs = uval >> k; - bits = 1 + k + msbs; + total_bits = 1 + k + msbs; pattern = 1 << k; /* the unary end bit */ pattern |= (uval & ((1u<> k; - bits = 1 + k + msbs; + total_bits = 1 + k + msbs; pattern = 1 << k; /* the unary end bit */ pattern |= (uval & ((1u<buffer != 0); + assert(parameter <= 31); + + /* read the unary MSBs and end bit */ + while(1) { + if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data)) + return false; + if(bit) + break; + else + msbs++; + } + /* read the sign bit */ + if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data)) + return false; + /* read the binary LSBs */ + if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data)) + return false; + + /* compose the value */ + *val = (msbs << parameter) | lsbs; + if(sign) + *val = -(*val); + + return true; +} + bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data) { uint32 lsbs = 0, msbs = 0; @@ -1010,7 +1127,7 @@ bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned pa assert(bb != 0); assert(bb->buffer != 0); - assert(parameter <= 32); + assert(parameter <= 31); /* read the unary MSBs and end bit */ while(1) { diff --git a/src/libFLAC/include/private/bitbuffer.h b/src/libFLAC/include/private/bitbuffer.h index c3562bb..90ba4a0 100644 --- a/src/libFLAC/include/private/bitbuffer.h +++ b/src/libFLAC/include/private/bitbuffer.h @@ -46,6 +46,8 @@ bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bi unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter); unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter); unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned val, unsigned parameter); +bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter); +bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow); bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter); bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow); bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter); @@ -63,6 +65,7 @@ bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, uint32 *val, unsigned bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, int32 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data); bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, uint64 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data); bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, int64 *val, unsigned bits, 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); bool FLAC__bitbuffer_read_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_golomb_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_golomb_unsigned(FLAC__BitBuffer *bb, unsigned *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data); -- 2.7.4