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 // Return the number of bytes left in the stream. |
| 28 virtual int GetBytesLeft() const = 0; |
| 29 }; |
| 30 |
| 31 // Lifetime of |byte_stream_provider| should be longer than BitReaderCore. |
| 32 explicit BitReaderCore(ByteStreamProvider* byte_stream_provider); |
| 33 ~BitReaderCore(); |
| 34 |
| 35 // Read |num_bits| next bits from stream and return in |*out|, first bit |
| 36 // from the stream starting at |num_bits| position in |*out|. |
| 37 // |num_bits| cannot be larger than the bits the type can hold. |
| 38 // Return false if the given number of bits cannot be read (not enough |
| 39 // bits in the stream), true otherwise. When return false, the stream will |
| 40 // enter a state where further ReadBits/SkipBits operations will always |
| 41 // return false unless |num_bits| is 0. The type |T| has to be a primitive |
| 42 // integer type. |
| 43 // T can especially not be a boolean since it generates |
| 44 // some optimization warnings during compilation on windows platforms, |
| 45 // use ReadFlag instead. |
| 46 template<typename T> bool ReadBits(int num_bits, T* out) { |
| 47 DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8)); |
| 48 uint64 temp; |
| 49 bool ret = ReadBitsInternal(num_bits, &temp); |
| 50 *out = static_cast<T>(temp); |
| 51 return ret; |
| 52 } |
| 53 |
| 54 // Read a boolean. |
| 55 bool ReadFlag(bool* flag); |
| 56 |
| 57 // Retrieve some bits without actually consuming them. |
| 58 // Bits returned in |*out| are written from MSB to LSB. |
| 59 // Return the number of bits written in |out|. |
| 60 // Note: if the number of remaining bits is less than |num_bits|, |
| 61 // only the remaining bits are returned and in this case, the number |
| 62 // of bits returned is less than |num_bits|. |
| 63 int PeekBitsMsbAligned(int num_bits, uint64* out); |
| 64 |
| 65 // Skip |num_bits| next bits from stream. Return false if the given number of |
| 66 // bits cannot be skipped (not enough bits in the stream), true otherwise. |
| 67 // When return false, the stream will enter a state where further |
| 68 // ReadBits/ReadFlag/SkipBits operations |
| 69 // will always return false unless |num_bits| is 0. |
| 70 bool SkipBits(int num_bits); |
| 71 |
| 72 // Returns the number of bits read so far. |
| 73 int GetBitCount() const; |
| 74 |
| 75 // Returns the number of bits available. |
| 76 int bits_available() const; |
| 77 |
| 78 private: |
| 79 typedef uint64 RegType; |
| 80 typedef int64 SignedRegType; |
| 81 static const int kRegWidthInBits; |
| 82 |
| 83 // Help function used by ReadBits to avoid inlining the bit reading logic. |
| 84 bool ReadBitsInternal(int num_bits, uint64* out); |
| 85 |
| 86 // Refill bit registers to have at least |min_nbits| bits available. |
| 87 // Return true if the mininimum bit count condition is met after the refill. |
| 88 bool Refill(int min_nbits); |
| 89 |
| 90 // Refill the current bit register from the next bit register. |
| 91 void RefillCurrentRegister(); |
| 92 |
| 93 ByteStreamProvider* const byte_stream_provider_; |
| 94 |
| 95 // Number of bits read so far. |
| 96 int bit_count_; |
| 97 |
| 98 // Number of valid bits left in |reg|. |
| 99 // Note: bits are consumed from MSB to LSB. |
| 100 int nbits_; |
| 101 RegType reg_; |
| 102 |
| 103 // Number of valid bits left in |reg_next_|. |
| 104 // Note: bits are consumed from MSB to LSB. |
| 105 int nbits_next_; |
| 106 RegType reg_next_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(BitReaderCore); |
| 109 }; |
| 110 |
| 111 } // namespace media |
| 112 |
| 113 #endif // MEDIA_BASE_BIT_READER_CORE_H_ |
OLD | NEW |