1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 19 #ifndef BITSTREAMPARSER_H_INCLUDED 20 #define BITSTREAMPARSER_H_INCLUDED 21 22 23 #include "oscl_assert.h" 24 #include "oscl_error_codes.h" 25 #include "oscl_exception.h" 26 27 #define MOST_SIG_BIT 7 28 #define LEAST_SIG_BIT 0 29 #define BITS_PER_BYTE 8 30 #define BITS_PER_UINT8 8 31 #define BITS_PER_UINT16 16 32 #define BITS_PER_UINT32 32 33 #define LEAST_SIG_3_BITS_MASK 0x07 34 35 36 //Macro to convert bits to bytes (rounding up to next whole byte). 37 #define BITS_TO_BYTES(bits) ((bits + MOST_SIG_BIT) / BITS_PER_BYTE) 38 39 40 class BitStreamParser 41 { 42 public: 43 //Constructor requires a pointer to the stream and the size in bytes. 44 OSCL_IMPORT_REF BitStreamParser(uint8* stream, uint32 size); 45 46 // Reset bitstream parser 47 OSCL_IMPORT_REF void ResetBitStreamParser(uint8* stream, uint32 size); 48 49 //Returns the specified number of bits from the stream (up to 32 bits). 50 OSCL_IMPORT_REF uint32 ReadBits(uint8 numberOfBits); 51 52 //Returns a 8-bit byte from the stream. 53 OSCL_IMPORT_REF uint8 ReadUInt8(void); 54 55 //Returns a 16-bit word from the stream, converting from network-byte-order to host-byte-order. 56 OSCL_IMPORT_REF uint16 ReadUInt16(void); 57 58 //Returns a 32-bit long from the stream, converting from network-byte-order to host-byte-order. 59 OSCL_IMPORT_REF uint32 ReadUInt32(void); 60 61 //Writes the specified number of bits to the stream. 62 OSCL_IMPORT_REF void WriteBits(uint8 numberOfBits, const uint8* data); 63 64 //Writes 8 bits to the stream. 65 OSCL_IMPORT_REF void WriteUInt8(uint8 data); 66 67 //Writes 16 bits to the stream, converting from host-byte-order to network-byte-order. 68 OSCL_IMPORT_REF void WriteUInt16(uint16 data); 69 70 //Write 32 bits to the stream, converting from host-byte-order to network-byte-order. 71 OSCL_IMPORT_REF void WriteUInt32(uint32 data); 72 73 //Skips over exactly one bit. This is more efficient than NextBits(1). NextBit(void)74 OSCL_EXPORT_REF inline void NextBit(void) 75 { 76 if (0 != bitpos) 77 { 78 bitpos--; 79 } 80 else 81 { 82 if (bytepos >= (start + size)) 83 { 84 OSCL_LEAVE(OsclErrOverflow); 85 } 86 bitpos = MOST_SIG_BIT; 87 bytepos++; 88 } 89 } 90 91 //Skips over the specified number if bits. 92 OSCL_IMPORT_REF void NextBits(uint32 numberOfBits); 93 94 //Returns the byte position of the stream. GetBytePos(void)95 OSCL_EXPORT_REF inline void* GetBytePos(void) const 96 { 97 return bytepos; 98 } 99 100 //Returns the bit position of the current byte. GetBitPos(void)101 OSCL_EXPORT_REF inline uint8 GetBitPos(void) const 102 { 103 return bitpos; 104 } 105 106 //The returns the number of whole bytes read (not including partial bytes). BytesRead(void)107 OSCL_EXPORT_REF inline uint32 BytesRead(void) const 108 { 109 return (bytepos - start); 110 } 111 BitsRead(void)112 OSCL_EXPORT_REF inline uint32 BitsRead(void) const 113 { 114 return (BytesRead() * BITS_PER_BYTE) + (MOST_SIG_BIT - bitpos); 115 } 116 117 //This is only 100% accurate when the bitpos is 7. 118 //Otherwise, part of the first byte was already consumed. BytesLeft(void)119 OSCL_EXPORT_REF inline uint32 BytesLeft(void) const 120 { 121 return size - BytesRead(); 122 } 123 BitsLeft(void)124 OSCL_EXPORT_REF inline uint32 BitsLeft(void) const 125 { 126 return ((BytesLeft() - 1) * BITS_PER_BYTE) + (1 + bitpos); 127 } 128 GetSize(void)129 OSCL_EXPORT_REF inline uint32 GetSize(void) const 130 { 131 return size; 132 } 133 134 private: 135 uint8* start; 136 uint32 size; 137 uint8* bytepos; //The current byte position in the stream. 138 uint8 bitpos; //The current bit position of the current byte. Counts down from 7 to 0. 139 }; 140 141 142 #endif //BITSTREAMPARSER_H_INCLUDED 143 144 145