From: drowe67 Date: Fri, 25 Apr 2014 00:41:17 +0000 (+0000) Subject: added natural bit packing option, compiles but not tested yet X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=4b4471101e9176dfd1136338234c2543a68f886d;p=freetel-svn-tracking.git added natural bit packing option, compiles but not tested yet git-svn-id: https://svn.code.sf.net/p/freetel/code@1556 01035d8c-6547-0410-b346-abe4f91aad63 --- diff --git a/codec2-dev/src/pack.c b/codec2-dev/src/pack.c index 5d67c329..51b3c4ae 100644 --- a/codec2-dev/src/pack.c +++ b/codec2-dev/src/pack.c @@ -52,8 +52,22 @@ pack( unsigned int fieldWidth/* Width of the field in BITS, not bytes. */ ) { - /* Convert the field to Gray code */ - field = (field >> 1) ^ field; + pack_natural_or_grey(bitArray, bitIndex, field, fieldWidth, 1); +} + +void +pack_natural_or_grey( + unsigned char * bitArray, /* The output bit string. */ + unsigned int * bitIndex, /* Index into the string in BITS, not bytes.*/ + int field, /* The bit field to be packed. */ + unsigned int fieldWidth,/* Width of the field in BITS, not bytes. */ + int gray /* non-zero for gray coding */ + ) +{ + if (gray) { + /* Convert the field to Gray code */ + field = (field >> 1) ^ field; + } do { unsigned int bI = *bitIndex; @@ -80,6 +94,21 @@ unpack( unsigned int * bitIndex, /* Index into the string in BITS, not bytes.*/ unsigned int fieldWidth/* Width of the field in BITS, not bytes. */ ) +{ + unpack_natural_or_gray(bitArray, bitIndex, fieldWidth, 1); +} + +/** Unpack a field from a bit string, to binary, optionally using + * natural or Gray code. + * + */ +int +unpack_natural_or_gray( + const unsigned char * bitArray, /* The input bit string. */ + unsigned int * bitIndex, /* Index into the string in BITS, not bytes.*/ + unsigned int fieldWidth,/* Width of the field in BITS, not bytes. */ + unsigned int gray /* non-zero for Gray coding */ + ) { unsigned int field = 0; unsigned int t; @@ -96,10 +125,16 @@ unpack( fieldWidth -= sliceWidth; } while ( fieldWidth != 0 ); - /* Convert from Gray code to binary. Works for maximum 8-bit fields. */ - t = field ^ (field >> 8); - t ^= (t >> 4); - t ^= (t >> 2); - t ^= (t >> 1); + if (gray) { + /* Convert from Gray code to binary. Works for maximum 8-bit fields. */ + t = field ^ (field >> 8); + t ^= (t >> 4); + t ^= (t >> 2); + t ^= (t >> 1); + } + else { + t = field; + } + return t; } diff --git a/codec2-dev/src/quantise.h b/codec2-dev/src/quantise.h index 158919dd..74359eb2 100644 --- a/codec2-dev/src/quantise.h +++ b/codec2-dev/src/quantise.h @@ -96,7 +96,9 @@ int encode_energy(float e); float decode_energy(int index); void pack(unsigned char * bits, unsigned int *nbit, int index, unsigned int index_bits); +void pack_natural_or_grey(unsigned char * bits, unsigned int *nbit, int index, unsigned int index_bits, int gray); int unpack(const unsigned char * bits, unsigned int *nbit, unsigned int index_bits); +int unpack_natural_or_grey(const unsigned char * bits, unsigned int *nbit, unsigned int index_bits, int gray); int lsp_bits(int i); int lspd_bits(int i);