Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

352 lines
8.4 KiB

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