2
0

bitstream.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: bitstream.c,v 1.2 2005/09/27 20:31:11 jrecker Exp $
  3. *
  4. * Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file,
  7. * are subject to the current version of the RealNetworks Public
  8. * Source License (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the current version of the RealNetworks Community
  11. * Source License (the "RCSL") available at
  12. * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13. * will apply. You may also obtain the license terms directly from
  14. * RealNetworks. You may not use this file except in compliance with
  15. * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16. * to this file, the RCSL. Please see the applicable RPSL or RCSL for
  17. * the rights, obligations and limitations governing use of the
  18. * contents of the file.
  19. *
  20. * This file is part of the Helix DNA Technology. RealNetworks is the
  21. * developer of the Original Code and owns the copyrights in the
  22. * portions it created.
  23. *
  24. * This file, and the files included with this file, is distributed
  25. * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  26. * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  27. * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  28. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  29. * ENJOYMENT OR NON-INFRINGEMENT.
  30. *
  31. * Technology Compatibility Kit Test Suite(s) Location:
  32. * http://www.helixcommunity.org/content/tck
  33. *
  34. * Contributor(s):
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /**************************************************************************************
  38. * Fixed-point HE-AAC decoder
  39. * Jon Recker (jrecker@real.com)
  40. * February 2005
  41. *
  42. * bitstream.c - bitstream parsing functions
  43. **************************************************************************************/
  44. #include "bitstream.h"
  45. /**************************************************************************************
  46. * Function: SetBitstreamPointer
  47. *
  48. * Description: initialize bitstream reader
  49. *
  50. * Inputs: pointer to BitStreamInfo struct
  51. * number of bytes in bitstream
  52. * pointer to byte-aligned buffer of data to read from
  53. *
  54. * Outputs: initialized bitstream info struct
  55. *
  56. * Return: none
  57. **************************************************************************************/
  58. void SetBitstreamPointer(BitStreamInfo *bsi, int nBytes, unsigned char *buf)
  59. {
  60. /* init bitstream */
  61. bsi->bytePtr = buf;
  62. bsi->iCache = 0; /* 4-byte unsigned int */
  63. bsi->cachedBits = 0; /* i.e. zero bits in cache */
  64. bsi->nBytes = nBytes;
  65. }
  66. /**************************************************************************************
  67. * Function: RefillBitstreamCache
  68. *
  69. * Description: read new data from bitstream buffer into 32-bit cache
  70. *
  71. * Inputs: pointer to initialized BitStreamInfo struct
  72. *
  73. * Outputs: updated bitstream info struct
  74. *
  75. * Return: none
  76. *
  77. * Notes: only call when iCache is completely drained (resets bitOffset to 0)
  78. * always loads 4 new bytes except when bsi->nBytes < 4 (end of buffer)
  79. * stores data as big-endian in cache, regardless of machine endian-ness
  80. **************************************************************************************/
  81. //Optimized for REV16, REV32 (FB)
  82. static __inline void RefillBitstreamCache(BitStreamInfo *bsi)
  83. {
  84. int nBytes = bsi->nBytes;
  85. if (nBytes >= 4) {
  86. /* optimize for common case, independent of machine endian-ness */
  87. bsi->iCache = (*bsi->bytePtr++) << 24;
  88. bsi->iCache |= (*bsi->bytePtr++) << 16;
  89. bsi->iCache |= (*bsi->bytePtr++) << 8;
  90. bsi->iCache |= (*bsi->bytePtr++);
  91. bsi->cachedBits = 32;
  92. bsi->nBytes -= 4;
  93. } else {
  94. bsi->iCache = 0;
  95. while (nBytes--) {
  96. bsi->iCache |= (*bsi->bytePtr++);
  97. bsi->iCache <<= 8;
  98. }
  99. bsi->iCache <<= ((3 - bsi->nBytes)*8);
  100. bsi->cachedBits = 8*bsi->nBytes;
  101. bsi->nBytes = 0;
  102. }
  103. }
  104. /**************************************************************************************
  105. * Function: GetBits
  106. *
  107. * Description: get bits from bitstream, advance bitstream pointer
  108. *
  109. * Inputs: pointer to initialized BitStreamInfo struct
  110. * number of bits to get from bitstream
  111. *
  112. * Outputs: updated bitstream info struct
  113. *
  114. * Return: the next nBits bits of data from bitstream buffer
  115. *
  116. * Notes: nBits must be in range [0, 31], nBits outside this range masked by 0x1f
  117. * for speed, does not indicate error if you overrun bit buffer
  118. * if nBits == 0, returns 0
  119. **************************************************************************************/
  120. unsigned int GetBits(BitStreamInfo *bsi, int nBits)
  121. {
  122. unsigned int data, lowBits;
  123. nBits &= 0x1f; /* nBits mod 32 to avoid unpredictable results like >> by negative amount */
  124. data = bsi->iCache >> (31 - nBits); /* unsigned >> so zero-extend */
  125. data >>= 1; /* do as >> 31, >> 1 so that nBits = 0 works okay (returns 0) */
  126. bsi->iCache <<= nBits; /* left-justify cache */
  127. bsi->cachedBits -= nBits; /* how many bits have we drawn from the cache so far */
  128. /* if we cross an int boundary, refill the cache */
  129. if (bsi->cachedBits < 0) {
  130. lowBits = -bsi->cachedBits;
  131. RefillBitstreamCache(bsi);
  132. data |= bsi->iCache >> (32 - lowBits); /* get the low-order bits */
  133. bsi->cachedBits -= lowBits; /* how many bits have we drawn from the cache so far */
  134. bsi->iCache <<= lowBits; /* left-justify cache */
  135. }
  136. return data;
  137. }
  138. /**************************************************************************************
  139. * Function: GetBitsNoAdvance
  140. *
  141. * Description: get bits from bitstream, do not advance bitstream pointer
  142. *
  143. * Inputs: pointer to initialized BitStreamInfo struct
  144. * number of bits to get from bitstream
  145. *
  146. * Outputs: none (state of BitStreamInfo struct left unchanged)
  147. *
  148. * Return: the next nBits bits of data from bitstream buffer
  149. *
  150. * Notes: nBits must be in range [0, 31], nBits outside this range masked by 0x1f
  151. * for speed, does not indicate error if you overrun bit buffer
  152. * if nBits == 0, returns 0
  153. **************************************************************************************/
  154. unsigned int GetBitsNoAdvance(BitStreamInfo *bsi, int nBits)
  155. {
  156. unsigned char *buf;
  157. unsigned int data, iCache;
  158. signed int lowBits;
  159. nBits &= 0x1f; /* nBits mod 32 to avoid unpredictable results like >> by negative amount */
  160. data = bsi->iCache >> (31 - nBits); /* unsigned >> so zero-extend */
  161. data >>= 1; /* do as >> 31, >> 1 so that nBits = 0 works okay (returns 0) */
  162. lowBits = nBits - bsi->cachedBits; /* how many bits do we have left to read */
  163. /* if we cross an int boundary, read next bytes in buffer */
  164. if (lowBits > 0) {
  165. iCache = 0;
  166. buf = bsi->bytePtr;
  167. while (lowBits > 0) {
  168. iCache <<= 8;
  169. if (buf < bsi->bytePtr + bsi->nBytes)
  170. iCache |= (unsigned int)*buf++;
  171. lowBits -= 8;
  172. }
  173. lowBits = -lowBits;
  174. data |= iCache >> lowBits;
  175. }
  176. return data;
  177. }
  178. /**************************************************************************************
  179. * Function: AdvanceBitstream
  180. *
  181. * Description: move bitstream pointer ahead
  182. *
  183. * Inputs: pointer to initialized BitStreamInfo struct
  184. * number of bits to advance bitstream
  185. *
  186. * Outputs: updated bitstream info struct
  187. *
  188. * Return: none
  189. *
  190. * Notes: generally used following GetBitsNoAdvance(bsi, maxBits)
  191. **************************************************************************************/
  192. void AdvanceBitstream(BitStreamInfo *bsi, int nBits)
  193. {
  194. nBits &= 0x1f;
  195. if (nBits > bsi->cachedBits) {
  196. nBits -= bsi->cachedBits;
  197. RefillBitstreamCache(bsi);
  198. }
  199. bsi->iCache <<= nBits;
  200. bsi->cachedBits -= nBits;
  201. }
  202. /**************************************************************************************
  203. * Function: CalcBitsUsed
  204. *
  205. * Description: calculate how many bits have been read from bitstream
  206. *
  207. * Inputs: pointer to initialized BitStreamInfo struct
  208. * pointer to start of bitstream buffer
  209. * bit offset into first byte of startBuf (0-7)
  210. *
  211. * Outputs: none
  212. *
  213. * Return: number of bits read from bitstream, as offset from startBuf:startOffset
  214. **************************************************************************************/
  215. int CalcBitsUsed(BitStreamInfo *bsi, unsigned char *startBuf, int startOffset)
  216. {
  217. int bitsUsed;
  218. bitsUsed = (bsi->bytePtr - startBuf) * 8;
  219. bitsUsed -= bsi->cachedBits;
  220. bitsUsed -= startOffset;
  221. return bitsUsed;
  222. }
  223. /**************************************************************************************
  224. * Function: ByteAlignBitstream
  225. *
  226. * Description: bump bitstream pointer to start of next byte
  227. *
  228. * Inputs: pointer to initialized BitStreamInfo struct
  229. *
  230. * Outputs: byte-aligned bitstream BitStreamInfo struct
  231. *
  232. * Return: none
  233. *
  234. * Notes: if bitstream is already byte-aligned, do nothing
  235. **************************************************************************************/
  236. void ByteAlignBitstream(BitStreamInfo *bsi)
  237. {
  238. int offset;
  239. offset = bsi->cachedBits & 0x07;
  240. AdvanceBitstream(bsi, offset);
  241. }