BitStream.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. // Not currently being maintained.
  26. // FIXME: Big-endian code is untested
  27. // FIXME: Not suitable for storing large buffers > 500 MB in size
  28. #ifndef CAT_BIT_STREAM_HPP
  29. #define CAT_BIT_STREAM_HPP
  30. #include <cat/Platform.hpp>
  31. namespace cat {
  32. //// BitStream manipulators ////
  33. namespace bs_bit
  34. {
  35. template<u32 N_BITS> struct set
  36. {
  37. u32 bits;
  38. set(u32 c_bits) : bits(c_bits) { }
  39. };
  40. template<u32 N_BITS> struct get
  41. {
  42. u32 &ref;
  43. get(u32 &c_ref) : ref(c_ref) { }
  44. };
  45. }
  46. namespace bs_byte
  47. {
  48. struct set
  49. {
  50. u32 bytes;
  51. const void *ref;
  52. set(u32 b, const void *r) : ref(r), bytes(b) { }
  53. };
  54. struct get
  55. {
  56. u32 bytes;
  57. void *ref;
  58. get(u32 b, void *r) : ref(r), bytes(b) { }
  59. };
  60. }
  61. //// BitStream ////
  62. class BitStream
  63. {
  64. bool fixed_buffer;
  65. u8 *buffer;
  66. u32 buffer_bytes;
  67. bool read_underrun;
  68. // grow to be able to write a number of bits
  69. void grow(u32 bits);
  70. public:
  71. u32 read_offset, write_offset; // in bits
  72. public:
  73. BitStream(u32 bytes = 0, void *vbuffer = 0);
  74. ~BitStream();
  75. // free unused buffer space
  76. void shrink();
  77. public:
  78. u8 *get() { return buffer; }
  79. bool aligned() { return read_offset % 8 == 0; }
  80. u8 *peek() { return buffer + read_offset / 8; }
  81. public:
  82. // returns true iff the buffer is valid
  83. bool valid() { return buffer != 0; }
  84. // returns count of remaining readable bits
  85. int unread() { return write_offset - read_offset; }
  86. // returns true if a recent read operation would have overrun the buffer
  87. bool underrun();
  88. // skip ahead a number of bits
  89. void skip(u32 bits);
  90. public:
  91. // insertion
  92. void write1(u8 data); // data MUST be 1 or 0
  93. template<class T> void write(T data)
  94. {
  95. grow(sizeof(T) * 8);
  96. u32 byte_offset = write_offset / 8;
  97. u32 shift = write_offset % 8;
  98. if (shift)
  99. {
  100. buffer[byte_offset] |= (u8)(data << shift);
  101. data = data >> (8 - shift);
  102. ++byte_offset;
  103. }
  104. *(T*)(buffer + byte_offset) = data;
  105. write_offset += sizeof(T) * 8;
  106. }
  107. template<> void write(s8 data) { write((u8)data); }
  108. template<> void write(s16 data) { write((u16)data); }
  109. template<> void write(s32 data) { write((u32)data); }
  110. template<> void write(s64 data) { write((u64)data); }
  111. template<> void write(float data) { write(*(u32*)&data); }
  112. template<> void write(double data) { write(*(u64*)&data); }
  113. void writeBits(u32 data, int count);
  114. void writeBytes(const void *data, u32 byte_count);
  115. public:
  116. // stream-mode insertion
  117. BitStream &operator<<(const char *data) { writeBytes(data, (u32)strlen(data)); return *this; }
  118. template<class T> BitStream &operator<<(T data) { write(data); return *this; }
  119. template<u32 N_BITS> BitStream &operator<<(const bs_bit::set<N_BITS> &n) { writeBits(n.bits, N_BITS); return *this; }
  120. template<> BitStream &operator<<(const bs_bit::set<1> &n) { write1((u8)n.bits); return *this; }
  121. template<> BitStream &operator<<(const bs_bit::set<8> &n) { write((u8)n.bits); return *this; }
  122. template<> BitStream &operator<<(const bs_bit::set<16> &n) { write((u16)n.bits); return *this; }
  123. template<> BitStream &operator<<(const bs_bit::set<32> &n) { write((u32)n.bits); return *this; }
  124. BitStream &operator<<(const bs_byte::set &n) { writeBytes(n.ref, n.bytes); return *this; }
  125. public:
  126. // extraction
  127. u8 read1();
  128. template<class T> void read(T &data)
  129. {
  130. const u32 bits = sizeof(T) * 8;
  131. if (read_offset + bits > write_offset)
  132. {
  133. read_underrun = true;
  134. return;
  135. }
  136. u32 byte_offset = read_offset / 8;
  137. u32 shift = read_offset % 8;
  138. if (shift)
  139. data = (*(T*)(buffer + byte_offset + 1) << (8 - shift)) | (buffer[byte_offset] >> shift);
  140. else
  141. data = *(T*)(buffer + byte_offset);
  142. read_offset += bits;
  143. }
  144. template<> void read(float &data) { read((u32&)data); }
  145. template<> void read(double &data) { write(*(u64*)&data); }
  146. template<class T> T read() { T temp; read(temp); return temp; }
  147. u32 readBits(u32 count);
  148. void readBytes(void *data, u32 byte_count);
  149. public:
  150. // stream-mode extraction
  151. template<class T> BitStream &operator>>(T &data) { read(data); return *this; }
  152. template<u32 N_BITS> BitStream &operator>>(const bs_bit::get<N_BITS> &n) { n.ref = readBits(N_BITS); return *this; }
  153. template<> BitStream &operator>>(const bs_bit::get<1> &n) { n.ref = read1(); return *this; }
  154. template<> BitStream &operator>>(const bs_bit::get<8> &n) { n.ref = read<u8>(); return *this; }
  155. template<> BitStream &operator>>(const bs_bit::get<16> &n) { n.ref = read<u16>(); return *this; }
  156. template<> BitStream &operator>>(const bs_bit::get<32> &n) { read(n.ref); return *this; }
  157. BitStream &operator>>(const bs_byte::get &n) { readBytes(n.ref, n.bytes); return *this; }
  158. };
  159. } // namespace cat
  160. #endif // CAT_BIT_STREAM_HPP
粤ICP备19079148号