OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 MEDIA_BASE_BIT_READER_CORE_H_ |
| 6 #define MEDIA_BASE_BIT_READER_CORE_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/logging.h" |
| 10 #include "media/base/media_export.h" |
| 11 |
| 12 namespace media { |
| 13 |
| 14 class MEDIA_EXPORT BitReaderCore { |
| 15 public: |
| 16 class ByteStreamProvider { |
| 17 public: |
| 18 ByteStreamProvider(); |
| 19 virtual ~ByteStreamProvider(); |
| 20 |
| 21 // Request n number of bytes where: |
| 22 // - n must be less than or equal to |max_n|. |
| 23 // Return the actual number of bytes available in |*array|. |
| 24 // Note: |*array| is ensured to be valid until the next call to GetBytes. |
| 25 virtual int GetBytes(int max_n, const uint8** array) = 0; |
| 26 }; |
| 27 |
| 28 // Lifetime of |byte_stream_provider| should be longer than BitReaderCore. |
| 29 explicit BitReaderCore(ByteStreamProvider* byte_stream_provider); |
| 30 ~BitReaderCore(); |
| 31 |
| 32 // Read a boolean from the stream and return in |*out|. |
| 33 // Remark: do not use the template version for reading a bool |
| 34 // since it generates some optimization warnings during compilation |
| 35 // on windows platforms. |
| 36 bool ReadBits(int num_bits, bool* out) { |
| 37 DCHECK_EQ(num_bits, 1); |
| 38 return ReadFlag(out); |
| 39 } |
| 40 |
| 41 // Read |num_bits| next bits from stream and return in |*out|, first bit |
| 42 // from the stream starting at |num_bits| position in |*out|. |
| 43 // |num_bits| cannot be larger than the bits the type can hold. |
| 44 // Return false if the given number of bits cannot be read (not enough |
| 45 // bits in the stream), true otherwise. When return false, the stream will |
| 46 // enter a state where further ReadBits/SkipBits operations will always |
| 47 // return false unless |num_bits| is 0. The type |T| has to be a primitive |
| 48 // integer type. |
| 49 // T can especially not be a boolean since it generates |
| 50 // some optimization warnings during compilation on windows platforms, |
| 51 // use ReadFlag instead. |
| 52 template<typename T> bool ReadBits(int num_bits, T* out) { |
| 53 DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8)); |
| 54 uint64 temp; |
| 55 bool ret = ReadBitsInternal(num_bits, &temp); |
| 56 *out = static_cast<T>(temp); |
| 57 return ret; |
| 58 } |
| 59 |
| 60 // Read a boolean. |
| 61 bool ReadFlag(bool* flag); |
| 62 |
| 63 // Retrieve some bits without actually consuming them. |
| 64 // Bits returned in |*out| are written from MSB to LSB. |
| 65 // Return the number of bits written in |out|. |
| 66 // Note: if the number of remaining bits is less than |num_bits|, |
| 67 // only the remaining bits are returned and in this case, the number |
| 68 // of bits returned is less than |num_bits|. |
| 69 int PeekBitsMsbAligned(int num_bits, uint64* out); |
| 70 |
| 71 // Skip |num_bits| next bits from stream. Return false if the given number of |
| 72 // bits cannot be skipped (not enough bits in the stream), true otherwise. |
| 73 // When return false, the stream will enter a state where further |
| 74 // ReadBits/ReadFlag/SkipBits operations |
| 75 // will always return false unless |num_bits| is 0. |
| 76 bool SkipBits(int num_bits); |
| 77 |
| 78 // Returns the number of bits read so far. |
| 79 int bits_read() const; |
| 80 |
| 81 // Returns the number of bits buffered in BitReaderCore. |
| 82 int bits_buffered() const; |
| 83 |
| 84 private: |
| 85 // Help function used by ReadBits to avoid inlining the bit reading logic. |
| 86 bool ReadBitsInternal(int num_bits, uint64* out); |
| 87 |
| 88 // Refill bit registers to have at least |min_nbits| bits available. |
| 89 // Return true if the mininimum bit count condition is met after the refill. |
| 90 bool Refill(int min_nbits); |
| 91 |
| 92 // Refill the current bit register from the next bit register. |
| 93 void RefillCurrentRegister(); |
| 94 |
| 95 ByteStreamProvider* const byte_stream_provider_; |
| 96 |
| 97 // Number of bits read so far. |
| 98 int bits_read_; |
| 99 |
| 100 // Number of valid bits left in |reg|. |
| 101 // Note: bits are consumed from MSB to LSB. |
| 102 int nbits_; |
| 103 uint64 reg_; |
| 104 |
| 105 // Number of valid bits left in |reg_next_|. |
| 106 // Note: bits are consumed from MSB to LSB. |
| 107 int nbits_next_; |
| 108 uint64 reg_next_; |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(BitReaderCore); |
| 111 }; |
| 112 |
| 113 } // namespace media |
| 114 |
| 115 #endif // MEDIA_BASE_BIT_READER_CORE_H_ |
OLD | NEW |