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