ALACBitUtilities.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. /*=============================================================================
  21. File: ALACBitUtilities.c
  22. $NoKeywords: $
  23. =============================================================================*/
  24. #include <stdio.h>
  25. #include "ALACBitUtilities.h"
  26. // BitBufferInit
  27. //
  28. void BitBufferInit( BitBuffer * bits, uint8_t * buffer, uint32_t byteSize )
  29. {
  30. bits->cur = buffer;
  31. bits->end = bits->cur + byteSize;
  32. bits->bitIndex = 0;
  33. bits->byteSize = byteSize;
  34. }
  35. // BitBufferRead
  36. //
  37. uint32_t BitBufferRead( BitBuffer * bits, uint8_t numBits )
  38. {
  39. uint32_t returnBits;
  40. //Assert( numBits <= 16 );
  41. returnBits = ((uint32_t)bits->cur[0] << 16) | ((uint32_t)bits->cur[1] << 8) | ((uint32_t)bits->cur[2]);
  42. returnBits = returnBits << bits->bitIndex;
  43. returnBits &= 0x00FFFFFF;
  44. bits->bitIndex += numBits;
  45. returnBits = returnBits >> (24 - numBits);
  46. bits->cur += (bits->bitIndex >> 3);
  47. bits->bitIndex &= 7;
  48. //Assert( bits->cur <= bits->end );
  49. return returnBits;
  50. }
  51. // BitBufferReadSmall
  52. //
  53. // Reads up to 8 bits
  54. uint8_t BitBufferReadSmall( BitBuffer * bits, uint8_t numBits )
  55. {
  56. uint16_t returnBits;
  57. //Assert( numBits <= 8 );
  58. returnBits = (bits->cur[0] << 8) | bits->cur[1];
  59. returnBits = returnBits << bits->bitIndex;
  60. bits->bitIndex += numBits;
  61. returnBits = returnBits >> (16 - numBits);
  62. bits->cur += (bits->bitIndex >> 3);
  63. bits->bitIndex &= 7;
  64. //Assert( bits->cur <= bits->end );
  65. return (uint8_t)returnBits;
  66. }
  67. // BitBufferReadOne
  68. //
  69. // Reads one byte
  70. uint8_t BitBufferReadOne( BitBuffer * bits )
  71. {
  72. uint8_t returnBits;
  73. returnBits = (bits->cur[0] >> (7 - bits->bitIndex)) & 1;
  74. bits->bitIndex++;
  75. bits->cur += (bits->bitIndex >> 3);
  76. bits->bitIndex &= 7;
  77. //Assert( bits->cur <= bits->end );
  78. return returnBits;
  79. }
  80. // BitBufferPeek
  81. //
  82. uint32_t BitBufferPeek( BitBuffer * bits, uint8_t numBits )
  83. {
  84. return ((((((uint32_t) bits->cur[0] << 16) | ((uint32_t) bits->cur[1] << 8) |
  85. ((uint32_t) bits->cur[2])) << bits->bitIndex) & 0x00FFFFFF) >> (24 - numBits));
  86. }
  87. // BitBufferPeekOne
  88. //
  89. uint32_t BitBufferPeekOne( BitBuffer * bits )
  90. {
  91. return ((bits->cur[0] >> (7 - bits->bitIndex)) & 1);
  92. }
  93. // BitBufferUnpackBERSize
  94. //
  95. uint32_t BitBufferUnpackBERSize( BitBuffer * bits )
  96. {
  97. uint32_t size;
  98. uint8_t tmp;
  99. for ( size = 0, tmp = 0x80u; tmp &= 0x80u; size = (size << 7u) | (tmp & 0x7fu) )
  100. tmp = (uint8_t) BitBufferReadSmall( bits, 8 );
  101. return size;
  102. }
  103. // BitBufferGetPosition
  104. //
  105. uint32_t BitBufferGetPosition( BitBuffer * bits )
  106. {
  107. uint8_t * begin;
  108. begin = bits->end - bits->byteSize;
  109. return ((uint32_t)(bits->cur - begin) * 8) + bits->bitIndex;
  110. }
  111. // BitBufferByteAlign
  112. //
  113. void BitBufferByteAlign( BitBuffer * bits, int32_t addZeros )
  114. {
  115. // align bit buffer to next byte boundary, writing zeros if requested
  116. if ( bits->bitIndex == 0 )
  117. return;
  118. if ( addZeros )
  119. BitBufferWrite( bits, 0, 8 - bits->bitIndex );
  120. else
  121. BitBufferAdvance( bits, 8 - bits->bitIndex );
  122. }
  123. // BitBufferAdvance
  124. //
  125. void BitBufferAdvance( BitBuffer * bits, uint32_t numBits )
  126. {
  127. if ( numBits )
  128. {
  129. bits->bitIndex += numBits;
  130. bits->cur += (bits->bitIndex >> 3);
  131. bits->bitIndex &= 7;
  132. }
  133. }
  134. // BitBufferRewind
  135. //
  136. void BitBufferRewind( BitBuffer * bits, uint32_t numBits )
  137. {
  138. uint32_t numBytes;
  139. if ( numBits == 0 )
  140. return;
  141. if ( bits->bitIndex >= numBits )
  142. {
  143. bits->bitIndex -= numBits;
  144. return;
  145. }
  146. numBits -= bits->bitIndex;
  147. bits->bitIndex = 0;
  148. numBytes = numBits / 8;
  149. numBits = numBits % 8;
  150. bits->cur -= numBytes;
  151. if ( numBits > 0 )
  152. {
  153. bits->bitIndex = 8 - numBits;
  154. bits->cur--;
  155. }
  156. if ( bits->cur < (bits->end - bits->byteSize) )
  157. {
  158. //DebugCMsg("BitBufferRewind: Rewound too far.");
  159. bits->cur = (bits->end - bits->byteSize);
  160. bits->bitIndex = 0;
  161. }
  162. }
  163. // BitBufferWrite
  164. //
  165. void BitBufferWrite( BitBuffer * bits, uint32_t bitValues, uint32_t numBits )
  166. {
  167. uint32_t invBitIndex;
  168. RequireAction( bits != nil, return; );
  169. RequireActionSilent( numBits > 0, return; );
  170. invBitIndex = 8 - bits->bitIndex;
  171. while ( numBits > 0 )
  172. {
  173. uint32_t tmp;
  174. uint8_t shift;
  175. uint8_t mask;
  176. uint32_t curNum;
  177. curNum = MIN( invBitIndex, numBits );
  178. tmp = bitValues >> (numBits - curNum);
  179. shift = (uint8_t)(invBitIndex - curNum);
  180. mask = 0xffu >> (8 - curNum); // must be done in two steps to avoid compiler sequencing ambiguity
  181. mask <<= shift;
  182. bits->cur[0] = (bits->cur[0] & ~mask) | (((uint8_t) tmp << shift) & mask);
  183. numBits -= curNum;
  184. // increment to next byte if need be
  185. invBitIndex -= curNum;
  186. if ( invBitIndex == 0 )
  187. {
  188. invBitIndex = 8;
  189. bits->cur++;
  190. }
  191. }
  192. bits->bitIndex = 8 - invBitIndex;
  193. }
  194. void BitBufferReset( BitBuffer * bits )
  195. //void BitBufferInit( BitBuffer * bits, uint8_t * buffer, uint32_t byteSize )
  196. {
  197. bits->cur = bits->end - bits->byteSize;
  198. bits->bitIndex = 0;
  199. }
  200. #if PRAGMA_MARK
  201. #pragma mark -
  202. #endif