packing.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. packing.c
  4. Copyright (C) The Internet Society (2004).
  5. All Rights Reserved.
  6. ******************************************************************/
  7. #include <math.h>
  8. #include <stdlib.h>
  9. #include "iLBC_define.h"
  10. #include "constants.h"
  11. #include "helpfun.h"
  12. #include "string.h"
  13. /*----------------------------------------------------------------*
  14. * splitting an integer into first most significant bits and
  15. * remaining least significant bits
  16. *---------------------------------------------------------------*/
  17. void packsplit(
  18. int *index, /* (i) the value to split */
  19. int *firstpart, /* (o) the value specified by most
  20. significant bits */
  21. int *rest, /* (o) the value specified by least
  22. significant bits */
  23. int bitno_firstpart, /* (i) number of bits in most
  24. significant part */
  25. int bitno_total /* (i) number of bits in full range
  26. of value */
  27. ){
  28. int bitno_rest = bitno_total-bitno_firstpart;
  29. *firstpart = *index>>(bitno_rest);
  30. *rest = *index-(*firstpart<<(bitno_rest));
  31. }
  32. /*----------------------------------------------------------------*
  33. * combining a value corresponding to msb's with a value
  34. * corresponding to lsb's
  35. *---------------------------------------------------------------*/
  36. void packcombine(
  37. int *index, /* (i/o) the msb value in the
  38. combined value out */
  39. int rest, /* (i) the lsb value */
  40. int bitno_rest /* (i) the number of bits in the
  41. lsb part */
  42. ){
  43. *index = *index<<bitno_rest;
  44. *index += rest;
  45. }
  46. /*----------------------------------------------------------------*
  47. * packing of bits into bitstream, i.e., vector of bytes
  48. *---------------------------------------------------------------*/
  49. void dopack(
  50. unsigned char **bitstream, /* (i/o) on entrance pointer to
  51. place in bitstream to pack
  52. new data, on exit pointer
  53. to place in bitstream to
  54. pack future data */
  55. int index, /* (i) the value to pack */
  56. int bitno, /* (i) the number of bits that the
  57. value will fit within */
  58. int *pos /* (i/o) write position in the
  59. current byte */
  60. ){
  61. int posLeft;
  62. /* Clear the bits before starting in a new byte */
  63. if ((*pos)==0) {
  64. **bitstream=0;
  65. }
  66. while (bitno>0) {
  67. /* Jump to the next byte if end of this byte is reached*/
  68. if (*pos==8) {
  69. *pos=0;
  70. (*bitstream)++;
  71. **bitstream=0;
  72. }
  73. posLeft=8-(*pos);
  74. /* Insert index into the bitstream */
  75. if (bitno <= posLeft) {
  76. **bitstream |= (unsigned char)(index<<(posLeft-bitno));
  77. *pos+=bitno;
  78. bitno=0;
  79. } else {
  80. **bitstream |= (unsigned char)(index>>(bitno-posLeft));
  81. *pos=8;
  82. index-=((index>>(bitno-posLeft))<<(bitno-posLeft));
  83. bitno-=posLeft;
  84. }
  85. }
  86. }
  87. /*----------------------------------------------------------------*
  88. * unpacking of bits from bitstream, i.e., vector of bytes
  89. *---------------------------------------------------------------*/
  90. void unpack(
  91. unsigned char **bitstream, /* (i/o) on entrance pointer to
  92. place in bitstream to
  93. unpack new data from, on
  94. exit pointer to place in
  95. bitstream to unpack future
  96. data from */
  97. int *index, /* (o) resulting value */
  98. int bitno, /* (i) number of bits used to
  99. represent the value */
  100. int *pos /* (i/o) read position in the
  101. current byte */
  102. ){
  103. int BitsLeft;
  104. *index=0;
  105. while (bitno>0) {
  106. /* move forward in bitstream when the end of the
  107. byte is reached */
  108. if (*pos==8) {
  109. *pos=0;
  110. (*bitstream)++;
  111. }
  112. BitsLeft=8-(*pos);
  113. /* Extract bits to index */
  114. if (BitsLeft>=bitno) {
  115. *index+=((((**bitstream)<<(*pos)) & 0xFF)>>(8-bitno));
  116. *pos+=bitno;
  117. bitno=0;
  118. } else {
  119. if ((8-bitno)>0) {
  120. *index+=((((**bitstream)<<(*pos)) & 0xFF)>>
  121. (8-bitno));
  122. *pos=8;
  123. } else {
  124. *index+=(((int)(((**bitstream)<<(*pos)) & 0xFF))<<
  125. (bitno-8));
  126. *pos=8;
  127. }
  128. bitno-=BitsLeft;
  129. }
  130. }
  131. }