BitReader.h (1924B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef BIT_READER_H_ 6 #define BIT_READER_H_ 7 8 #include "MediaData.h" 9 10 namespace mozilla { 11 12 class BitReader { 13 public: 14 explicit BitReader(const MediaByteBuffer* aBuffer); 15 BitReader(const MediaByteBuffer* aBuffer, size_t aBits); 16 BitReader(const uint8_t* aBuffer, size_t aBits); 17 ~BitReader(); 18 uint32_t ReadBits(size_t aNum); 19 bool ReadBit() { return ReadBits(1) != 0; } 20 uint32_t ReadU32() { return ReadBits(32); } 21 uint64_t ReadU64(); 22 23 // Read the UTF-8 sequence and convert it to its 64-bit UCS-4 encoded form. 24 // Return 0xfffffffffffffff if sequence was invalid. 25 uint64_t ReadUTF8(); 26 // Read unsigned integer Exp-Golomb-coded. 27 uint32_t ReadUE(); 28 // Read signed integer Exp-Golomb-coded. 29 int32_t ReadSE(); 30 // Read unsigned integer Little Endian Base 128 coded. 31 // Limited to unsigned 64 bits. 32 CheckedUint64 ReadULEB128(); 33 34 // Advance bits and return the actual number of bits forwarded. Unlike 35 // ReadBits, which can only read up to 32 bits, this function does not limit 36 // how many bits it can advance. If fewer bits are available than requested, 37 // it will only advance the available bits. 38 size_t AdvanceBits(size_t aNum); 39 40 // Return the number of bits parsed so far; 41 size_t BitCount() const; 42 // Return the number of bits left. 43 size_t BitsLeft() const; 44 45 // Return RBSP bit length. 46 static uint32_t GetBitLength(const MediaByteBuffer* aNAL); 47 48 private: 49 void FillReservoir(); 50 const uint8_t* mData; 51 const size_t mOriginalBitSize; 52 size_t mTotalBitsLeft; 53 size_t mSize; // Size left in bytes 54 uint32_t mReservoir; // Left-aligned bits 55 size_t mNumBitsLeft; // Number of bits left in reservoir. 56 }; 57 58 } // namespace mozilla 59 60 #endif // BIT_READER_H_