bits.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* Copyright (C) 2002 Jean-Marc Valin
  2. File: speex_bits.c
  3. Handles bit packing/unpacking
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include <speex/speex_bits.h>
  31. #include "misc.h"
  32. /* Maximum size of the bit-stream (for fixed-size allocation) */
  33. #ifndef MAX_CHARS_PER_FRAME
  34. #define MAX_CHARS_PER_FRAME (2000/BYTES_PER_CHAR)
  35. #endif
  36. void speex_bits_init(SpeexBits *bits)
  37. {
  38. bits->chars = (char*)speex_alloc(MAX_CHARS_PER_FRAME);
  39. if (!bits->chars)
  40. return;
  41. bits->buf_size = MAX_CHARS_PER_FRAME;
  42. bits->owner=1;
  43. speex_bits_reset(bits);
  44. }
  45. void speex_bits_init_buffer(SpeexBits *bits, void *buff, int buf_size)
  46. {
  47. bits->chars = (char*)buff;
  48. bits->buf_size = buf_size;
  49. bits->owner=0;
  50. speex_bits_reset(bits);
  51. }
  52. void speex_bits_destroy(SpeexBits *bits)
  53. {
  54. if (bits->owner)
  55. speex_free(bits->chars);
  56. /* Will do something once the allocation is dynamic */
  57. }
  58. void speex_bits_reset(SpeexBits *bits)
  59. {
  60. bits->chars[0]=0;
  61. bits->nbBits=0;
  62. bits->charPtr=0;
  63. bits->bitPtr=0;
  64. bits->overflow=0;
  65. }
  66. void speex_bits_rewind(SpeexBits *bits)
  67. {
  68. bits->charPtr=0;
  69. bits->bitPtr=0;
  70. bits->overflow=0;
  71. }
  72. void speex_bits_read_from(SpeexBits *bits, char *chars, int len)
  73. {
  74. int i;
  75. if (len > bits->buf_size)
  76. {
  77. speex_warning_int("Packet is larger than allocated buffer: ", len);
  78. if (bits->owner)
  79. {
  80. char *tmp = (char*)speex_realloc(bits->chars, len);
  81. if (tmp)
  82. {
  83. bits->buf_size=len;
  84. bits->chars=tmp;
  85. } else {
  86. len=bits->buf_size;
  87. speex_warning("Could not resize input buffer: truncating input");
  88. }
  89. } else {
  90. speex_warning("Do not own input buffer: truncating input");
  91. len=bits->buf_size;
  92. }
  93. }
  94. for (i=0;i<len;i++)
  95. bits->chars[i]=chars[i];
  96. bits->nbBits=len<<3;
  97. bits->charPtr=0;
  98. bits->bitPtr=0;
  99. bits->overflow=0;
  100. }
  101. static void speex_bits_flush(SpeexBits *bits)
  102. {
  103. int i;
  104. int nchars = ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR);
  105. if (bits->charPtr>0)
  106. {
  107. for (i=bits->charPtr;i<nchars; i++)
  108. bits->chars[i-bits->charPtr]=bits->chars[i];
  109. }
  110. bits->nbBits -= bits->charPtr<<LOG2_BITS_PER_CHAR;
  111. bits->charPtr=0;
  112. }
  113. void speex_bits_read_whole_bytes(SpeexBits *bits, char *chars, int nbytes)
  114. {
  115. int i,pos;
  116. int nchars = nbytes/BYTES_PER_CHAR;
  117. if (((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR)+nchars > bits->buf_size)
  118. {
  119. /* Packet is larger than allocated buffer */
  120. if (bits->owner)
  121. {
  122. char *tmp = (char*)speex_realloc(bits->chars, (bits->nbBits>>LOG2_BITS_PER_CHAR)+nchars+1);
  123. if (tmp)
  124. {
  125. bits->buf_size=(bits->nbBits>>LOG2_BITS_PER_CHAR)+nchars+1;
  126. bits->chars=tmp;
  127. } else {
  128. nchars=bits->buf_size-(bits->nbBits>>LOG2_BITS_PER_CHAR)-1;
  129. speex_warning("Could not resize input buffer: truncating input");
  130. }
  131. } else {
  132. speex_warning("Do not own input buffer: truncating input");
  133. nchars=bits->buf_size;
  134. }
  135. }
  136. speex_bits_flush(bits);
  137. pos=bits->nbBits>>LOG2_BITS_PER_CHAR;
  138. for (i=0;i<nchars;i++)
  139. bits->chars[pos+i]=chars[i];
  140. bits->nbBits+=nchars<<LOG2_BITS_PER_CHAR;
  141. }
  142. int speex_bits_write(SpeexBits *bits, char *chars, int max_nbytes)
  143. {
  144. int i;
  145. int max_nchars = max_nbytes/BYTES_PER_CHAR;
  146. int charPtr, bitPtr, nbBits;
  147. /* Insert terminator, but save the data so we can put it back after */
  148. bitPtr=bits->bitPtr;
  149. charPtr=bits->charPtr;
  150. nbBits=bits->nbBits;
  151. speex_bits_insert_terminator(bits);
  152. bits->bitPtr=bitPtr;
  153. bits->charPtr=charPtr;
  154. bits->nbBits=nbBits;
  155. if (max_nchars > ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR))
  156. max_nchars = ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR);
  157. #if BYTES_PER_CHAR==1
  158. #define HTOLS(A) (A)
  159. #else
  160. #define HTOLS(A) ((((A) >> 8)&0xff)|(((A) & 0xff)<<8))
  161. #endif
  162. for (i=0;i<max_nchars;i++)
  163. chars[i]=HTOLS(bits->chars[i]);
  164. return max_nchars*BYTES_PER_CHAR;
  165. }
  166. int speex_bits_write_whole_bytes(SpeexBits *bits, char *chars, int max_nbytes)
  167. {
  168. int max_nchars = max_nbytes/BYTES_PER_CHAR;
  169. int i;
  170. if (max_nchars > ((bits->nbBits)>>LOG2_BITS_PER_CHAR))
  171. max_nchars = ((bits->nbBits)>>LOG2_BITS_PER_CHAR);
  172. for (i=0;i<max_nchars;i++)
  173. chars[i]=bits->chars[i];
  174. if (bits->bitPtr>0)
  175. bits->chars[0]=bits->chars[max_nchars];
  176. else
  177. bits->chars[0]=0;
  178. for (i=1;i<((bits->nbBits)>>LOG2_BITS_PER_CHAR)+1;i++)
  179. bits->chars[i]=0;
  180. bits->charPtr=0;
  181. bits->nbBits &= (BITS_PER_CHAR-1);
  182. return max_nchars*BYTES_PER_CHAR;
  183. }
  184. void speex_bits_pack(SpeexBits *bits, int data, int nbBits)
  185. {
  186. unsigned int d=data;
  187. if (bits->charPtr+((nbBits+bits->bitPtr)>>LOG2_BITS_PER_CHAR) >= bits->buf_size)
  188. {
  189. speex_warning("Buffer too small to pack bits");
  190. if (bits->owner)
  191. {
  192. int new_nchars = ((bits->buf_size+5)*3)>>1;
  193. char *tmp = (char*)speex_realloc(bits->chars, new_nchars);
  194. if (tmp)
  195. {
  196. speex_memset_bytes(tmp, 0, new_nchars);
  197. bits->buf_size=new_nchars;
  198. bits->chars=tmp;
  199. } else {
  200. speex_warning("Could not resize input buffer: not packing");
  201. return;
  202. }
  203. } else {
  204. speex_warning("Do not own input buffer: not packing");
  205. return;
  206. }
  207. }
  208. while(nbBits)
  209. {
  210. int bit;
  211. bit = (d>>(nbBits-1))&1;
  212. bits->chars[bits->charPtr] |= bit<<(BITS_PER_CHAR-1-bits->bitPtr);
  213. bits->bitPtr++;
  214. if (bits->bitPtr==BITS_PER_CHAR)
  215. {
  216. bits->bitPtr=0;
  217. bits->charPtr++;
  218. bits->chars[bits->charPtr] = 0;
  219. }
  220. bits->nbBits++;
  221. nbBits--;
  222. }
  223. }
  224. int speex_bits_unpack_signed(SpeexBits *bits, int nbBits)
  225. {
  226. unsigned int d=speex_bits_unpack_unsigned(bits,nbBits);
  227. /* If number is negative */
  228. if (d>>(nbBits-1))
  229. {
  230. d |= (-1)<<nbBits;
  231. }
  232. return d;
  233. }
  234. unsigned int speex_bits_unpack_unsigned(SpeexBits *bits, int nbBits)
  235. {
  236. unsigned int d=0;
  237. if ((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+nbBits>bits->nbBits)
  238. bits->overflow=1;
  239. if (bits->overflow)
  240. return 0;
  241. while(nbBits)
  242. {
  243. d<<=1;
  244. d |= (bits->chars[bits->charPtr]>>(BITS_PER_CHAR-1 - bits->bitPtr))&1;
  245. bits->bitPtr++;
  246. if (bits->bitPtr==BITS_PER_CHAR)
  247. {
  248. bits->bitPtr=0;
  249. bits->charPtr++;
  250. }
  251. nbBits--;
  252. }
  253. return d;
  254. }
  255. unsigned int speex_bits_peek_unsigned(SpeexBits *bits, int nbBits)
  256. {
  257. unsigned int d=0;
  258. int bitPtr, charPtr;
  259. char *chars;
  260. if ((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+nbBits>bits->nbBits)
  261. bits->overflow=1;
  262. if (bits->overflow)
  263. return 0;
  264. bitPtr=bits->bitPtr;
  265. charPtr=bits->charPtr;
  266. chars = bits->chars;
  267. while(nbBits)
  268. {
  269. d<<=1;
  270. d |= (chars[charPtr]>>(BITS_PER_CHAR-1 - bitPtr))&1;
  271. bitPtr++;
  272. if (bitPtr==BITS_PER_CHAR)
  273. {
  274. bitPtr=0;
  275. charPtr++;
  276. }
  277. nbBits--;
  278. }
  279. return d;
  280. }
  281. int speex_bits_peek(SpeexBits *bits)
  282. {
  283. if ((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+1>bits->nbBits)
  284. bits->overflow=1;
  285. if (bits->overflow)
  286. return 0;
  287. return (bits->chars[bits->charPtr]>>(BITS_PER_CHAR-1 - bits->bitPtr))&1;
  288. }
  289. void speex_bits_advance(SpeexBits *bits, int n)
  290. {
  291. if (((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+n>bits->nbBits) || bits->overflow){
  292. bits->overflow=1;
  293. return;
  294. }
  295. bits->charPtr += (bits->bitPtr+n) >> LOG2_BITS_PER_CHAR; /* divide by BITS_PER_CHAR */
  296. bits->bitPtr = (bits->bitPtr+n) & (BITS_PER_CHAR-1); /* modulo by BITS_PER_CHAR */
  297. }
  298. int speex_bits_remaining(SpeexBits *bits)
  299. {
  300. if (bits->overflow)
  301. return -1;
  302. else
  303. return bits->nbBits-((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr);
  304. }
  305. int speex_bits_nbytes(SpeexBits *bits)
  306. {
  307. return ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR);
  308. }
  309. void speex_bits_insert_terminator(SpeexBits *bits)
  310. {
  311. if (bits->bitPtr)
  312. speex_bits_pack(bits, 0, 1);
  313. while (bits->bitPtr)
  314. speex_bits_pack(bits, 1, 1);
  315. }
粤ICP备19079148号