1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BIT_READER_H_ 6 #define BIT_READER_H_ 7 8 #include <stdint.h> 9 #include <string> 10 11 #include "base/compiler_specific.h" 12 #include "base/macros.h" 13 #include "bit_reader_core.h" 14 15 namespace media { 16 17 class BitReader 18 : NON_EXPORTED_BASE(private BitReaderCore::ByteStreamProvider) { 19 public: 20 // Initialize the reader to start reading at |data|, |size| being size 21 // of |data| in bytes. 22 BitReader(const uint8_t* data, int size); 23 ~BitReader() override; 24 25 template<typename T> bool ReadBits(int num_bits, T* out) { 26 return bit_reader_core_.ReadBits(num_bits, out); 27 } 28 29 bool ReadFlag(bool* flag) { 30 return bit_reader_core_.ReadFlag(flag); 31 } 32 33 // Read |num_bits| of binary data into |str|. |num_bits| must be a positive 34 // multiple of 8. This is not efficient for extracting large strings. 35 // If false is returned, |str| may not be valid. 36 bool ReadString(int num_bits, std::string* str); 37 38 bool SkipBits(int num_bits) { 39 return bit_reader_core_.SkipBits(num_bits); 40 } 41 42 int bits_available() const { 43 return initial_size_ * 8 - bits_read(); 44 } 45 46 int bits_read() const { 47 return bit_reader_core_.bits_read(); 48 } 49 50 private: 51 // BitReaderCore::ByteStreamProvider implementation. 52 int GetBytes(int max_n, const uint8_t** out) override; 53 54 // Total number of bytes that was initially passed to BitReader. 55 const int initial_size_; 56 57 // Pointer to the next unread byte in the stream. 58 const uint8_t* data_; 59 60 // Bytes left in the stream. 61 int bytes_left_; 62 63 BitReaderCore bit_reader_core_; 64 65 DISALLOW_COPY_AND_ASSIGN(BitReader); 66 }; 67 68 } // namespace media 69 70 #endif // BIT_READER_H_ 71