bits.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "arch.h"
  32. #include "os_support.h"
  33. /* Maximum size of the bit-stream (for fixed-size allocation) */
  34. #ifndef MAX_CHARS_PER_FRAME
  35. #define MAX_CHARS_PER_FRAME (2000/BYTES_PER_CHAR)
  36. #endif
  37. EXPORT void speex_bits_init(SpeexBits *bits)
  38. {
  39. bits->chars = (char*)speex_alloc(MAX_CHARS_PER_FRAME);
  40. if (!bits->chars)
  41. return;
  42. bits->buf_size = MAX_CHARS_PER_FRAME;
  43. bits->owner=1;
  44. speex_bits_reset(bits);
  45. }
  46. EXPORT void speex_bits_init_buffer(SpeexBits *bits, void *buff, int buf_size)
  47. {
  48. bits->chars = (char*)buff;
  49. bits->buf_size = buf_size;
  50. bits->owner=0;
  51. speex_bits_reset(bits);
  52. }
  53. EXPORT void speex_bits_set_bit_buffer(SpeexBits *bits, void *buff, int buf_size)
  54. {
  55. bits->chars = (char*)buff;
  56. bits->buf_size = buf_size;
  57. bits->owner=0;
  58. bits->nbBits=buf_size<<LOG2_BITS_PER_CHAR;
  59. bits->charPtr=0;
  60. bits->bitPtr=0;
  61. bits->overflow=0;
  62. }
  63. EXPORT void speex_bits_destroy(SpeexBits *bits)
  64. {
  65. if (bits->owner)
  66. speex_free(bits->chars);
  67. /* Will do something once the allocation is dynamic */
  68. }
  69. EXPORT void speex_bits_reset(SpeexBits *bits)
  70. {
  71. /* We only need to clear the first byte now */
  72. bits->chars[0]=0;
  73. bits->nbBits=0;
  74. bits->charPtr=0;
  75. bits->bitPtr=0;
  76. bits->overflow=0;
  77. }
  78. EXPORT void speex_bits_rewind(SpeexBits *bits)
  79. {
  80. bits->charPtr=0;
  81. bits->bitPtr=0;
  82. bits->overflow=0;
  83. }
  84. EXPORT void speex_bits_read_from(SpeexBits *bits, const char *chars, int len)
  85. {
  86. int i;
  87. int nchars = len / BYTES_PER_CHAR;
  88. if (nchars > bits->buf_size)
  89. {
  90. speex_notify("Packet is larger than allocated buffer");
  91. if (bits->owner)
  92. {
  93. char *tmp = (char*)speex_realloc(bits->chars, nchars);
  94. if (tmp)
  95. {
  96. bits->buf_size=nchars;
  97. bits->chars=tmp;
  98. } else {
  99. nchars=bits->buf_size;
  100. speex_warning("Could not resize input buffer: truncating input");
  101. }
  102. } else {
  103. speex_warning("Do not own input buffer: truncating oversize input");
  104. nchars=bits->buf_size;
  105. }
  106. }
  107. #if (BYTES_PER_CHAR==2)
  108. /* Swap bytes to proper endian order (could be done externally) */
  109. #define HTOLS(A) ((((A) >> 8)&0xff)|(((A) & 0xff)<<8))
  110. #else
  111. #define HTOLS(A) (A)
  112. #endif
  113. for (i=0;i<nchars;i++)
  114. bits->chars[i]=HTOLS(chars[i]);
  115. bits->nbBits=nchars<<LOG2_BITS_PER_CHAR;
  116. bits->charPtr=0;
  117. bits->bitPtr=0;
  118. bits->overflow=0;
  119. }
  120. static void speex_bits_flush(SpeexBits *bits)
  121. {
  122. int nchars = ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR);
  123. if (bits->charPtr>0)
  124. SPEEX_MOVE(bits->chars, &bits->chars[bits->charPtr], nchars-bits->charPtr);
  125. bits->nbBits -= bits->charPtr<<LOG2_BITS_PER_CHAR;
  126. bits->charPtr=0;
  127. }
  128. EXPORT void speex_bits_read_whole_bytes(SpeexBits *bits, const char *chars, int nbytes)
  129. {
  130. int i,pos;
  131. int nchars = nbytes/BYTES_PER_CHAR;
  132. if (((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR)+nchars > bits->buf_size)
  133. {
  134. /* Packet is larger than allocated buffer */
  135. if (bits->owner)
  136. {
  137. char *tmp = (char*)speex_realloc(bits->chars, (bits->nbBits>>LOG2_BITS_PER_CHAR)+nchars+1);
  138. if (tmp)
  139. {
  140. bits->buf_size=(bits->nbBits>>LOG2_BITS_PER_CHAR)+nchars+1;
  141. bits->chars=tmp;
  142. } else {
  143. nchars=bits->buf_size-(bits->nbBits>>LOG2_BITS_PER_CHAR)-1;
  144. speex_warning("Could not resize input buffer: truncating oversize input");
  145. }
  146. } else {
  147. speex_warning("Do not own input buffer: truncating oversize input");
  148. nchars=bits->buf_size;
  149. }
  150. }
  151. speex_bits_flush(bits);
  152. pos=bits->nbBits>>LOG2_BITS_PER_CHAR;
  153. for (i=0;i<nchars;i++)
  154. bits->chars[pos+i]=HTOLS(chars[i]);
  155. bits->nbBits+=nchars<<LOG2_BITS_PER_CHAR;
  156. }
  157. EXPORT int speex_bits_write(SpeexBits *bits, char *chars, int max_nbytes)
  158. {
  159. int i;
  160. int max_nchars = max_nbytes/BYTES_PER_CHAR;
  161. int charPtr, bitPtr, nbBits;
  162. /* Insert terminator, but save the data so we can put it back after */
  163. bitPtr=bits->bitPtr;
  164. charPtr=bits->charPtr;
  165. nbBits=bits->nbBits;
  166. speex_bits_insert_terminator(bits);
  167. bits->bitPtr=bitPtr;
  168. bits->charPtr=charPtr;
  169. bits->nbBits=nbBits;
  170. if (max_nchars > ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR))
  171. max_nchars = ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR);
  172. for (i=0;i<max_nchars;i++)
  173. chars[i]=HTOLS(bits->chars[i]);
  174. return max_nchars*BYTES_PER_CHAR;
  175. }
  176. EXPORT int speex_bits_write_whole_bytes(SpeexBits *bits, char *chars, int max_nbytes)
  177. {
  178. int max_nchars = max_nbytes/BYTES_PER_CHAR;
  179. int i;
  180. if (max_nchars > ((bits->nbBits)>>LOG2_BITS_PER_CHAR))
  181. max_nchars = ((bits->nbBits)>>LOG2_BITS_PER_CHAR);
  182. for (i=0;i<max_nchars;i++)
  183. chars[i]=HTOLS(bits->chars[i]);
  184. if (bits->bitPtr>0)
  185. bits->chars[0]=bits->chars[max_nchars];
  186. else
  187. bits->chars[0]=0;
  188. bits->charPtr=0;
  189. bits->nbBits &= (BITS_PER_CHAR-1);
  190. return max_nchars*BYTES_PER_CHAR;
  191. }
  192. EXPORT void speex_bits_pack(SpeexBits *bits, int data, int nbBits)
  193. {
  194. unsigned int d=data;
  195. if (bits->charPtr+((nbBits+bits->bitPtr)>>LOG2_BITS_PER_CHAR) >= bits->buf_size)
  196. {
  197. speex_notify("Buffer too small to pack bits");
  198. if (bits->owner)
  199. {
  200. int new_nchars = ((bits->buf_size+5)*3)>>1;
  201. char *tmp = (char*)speex_realloc(bits->chars, new_nchars);
  202. if (tmp)
  203. {
  204. bits->buf_size=new_nchars;
  205. bits->chars=tmp;
  206. } else {
  207. speex_warning("Could not resize input buffer: not packing");
  208. return;
  209. }
  210. } else {
  211. speex_warning("Do not own input buffer: not packing");
  212. return;
  213. }
  214. }
  215. while(nbBits)
  216. {
  217. int bit;
  218. bit = (d>>(nbBits-1))&1;
  219. bits->chars[bits->charPtr] |= bit<<(BITS_PER_CHAR-1-bits->bitPtr);
  220. bits->bitPtr++;
  221. if (bits->bitPtr==BITS_PER_CHAR)
  222. {
  223. bits->bitPtr=0;
  224. bits->charPtr++;
  225. bits->chars[bits->charPtr] = 0;
  226. }
  227. bits->nbBits++;
  228. nbBits--;
  229. }
  230. }
  231. EXPORT int speex_bits_unpack_signed(SpeexBits *bits, int nbBits)
  232. {
  233. unsigned int d=speex_bits_unpack_unsigned(bits,nbBits);
  234. /* If number is negative */
  235. if (d>>(nbBits-1))
  236. {
  237. d |= (-1)<<nbBits;
  238. }
  239. return d;
  240. }
  241. EXPORT unsigned int speex_bits_unpack_unsigned(SpeexBits *bits, int nbBits)
  242. {
  243. unsigned int d=0;
  244. if ((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+nbBits>bits->nbBits)
  245. bits->overflow=1;
  246. if (bits->overflow)
  247. return 0;
  248. while(nbBits)
  249. {
  250. d<<=1;
  251. d |= (bits->chars[bits->charPtr]>>(BITS_PER_CHAR-1 - bits->bitPtr))&1;
  252. bits->bitPtr++;
  253. if (bits->bitPtr==BITS_PER_CHAR)
  254. {
  255. bits->bitPtr=0;
  256. bits->charPtr++;
  257. }
  258. nbBits--;
  259. }
  260. return d;
  261. }
  262. EXPORT unsigned int speex_bits_peek_unsigned(SpeexBits *bits, int nbBits)
  263. {
  264. unsigned int d=0;
  265. int bitPtr, charPtr;
  266. char *chars;
  267. if ((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+nbBits>bits->nbBits)
  268. bits->overflow=1;
  269. if (bits->overflow)
  270. return 0;
  271. bitPtr=bits->bitPtr;
  272. charPtr=bits->charPtr;
  273. chars = bits->chars;
  274. while(nbBits)
  275. {
  276. d<<=1;
  277. d |= (chars[charPtr]>>(BITS_PER_CHAR-1 - bitPtr))&1;
  278. bitPtr++;
  279. if (bitPtr==BITS_PER_CHAR)
  280. {
  281. bitPtr=0;
  282. charPtr++;
  283. }
  284. nbBits--;
  285. }
  286. return d;
  287. }
  288. EXPORT int speex_bits_peek(SpeexBits *bits)
  289. {
  290. if ((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+1>bits->nbBits)
  291. bits->overflow=1;
  292. if (bits->overflow)
  293. return 0;
  294. return (bits->chars[bits->charPtr]>>(BITS_PER_CHAR-1 - bits->bitPtr))&1;
  295. }
  296. EXPORT void speex_bits_advance(SpeexBits *bits, int n)
  297. {
  298. if (((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr+n>bits->nbBits) || bits->overflow){
  299. bits->overflow=1;
  300. return;
  301. }
  302. bits->charPtr += (bits->bitPtr+n) >> LOG2_BITS_PER_CHAR; /* divide by BITS_PER_CHAR */
  303. bits->bitPtr = (bits->bitPtr+n) & (BITS_PER_CHAR-1); /* modulo by BITS_PER_CHAR */
  304. }
  305. EXPORT int speex_bits_remaining(SpeexBits *bits)
  306. {
  307. if (bits->overflow)
  308. return -1;
  309. else
  310. return bits->nbBits-((bits->charPtr<<LOG2_BITS_PER_CHAR)+bits->bitPtr);
  311. }
  312. EXPORT int speex_bits_nbytes(SpeexBits *bits)
  313. {
  314. return ((bits->nbBits+BITS_PER_CHAR-1)>>LOG2_BITS_PER_CHAR);
  315. }
  316. EXPORT void speex_bits_insert_terminator(SpeexBits *bits)
  317. {
  318. if (bits->bitPtr)
  319. speex_bits_pack(bits, 0, 1);
  320. while (bits->bitPtr)
  321. speex_bits_pack(bits, 1, 1);
  322. }