added natural bit packing option, compiles but not tested yet
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 25 Apr 2014 00:41:17 +0000 (00:41 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 25 Apr 2014 00:41:17 +0000 (00:41 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@1556 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/src/pack.c
codec2-dev/src/quantise.h

index 5d67c3296e4d9eb65b6a03c1ead98c50aa5ceae3..51b3c4ae5254ea22f719efb9e997d083c939ea38 100644 (file)
@@ -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;
 }
index 158919dd57918f43a3c05a2505f4e8b3fa6c9793..74359eb2a999c2840e6c5511b5e76404511923c2 100644 (file)
@@ -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);