| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_BASE_BIT_READER_CORE_H_ | 5 #ifndef MEDIA_BASE_BIT_READER_CORE_H_ |
| 6 #define MEDIA_BASE_BIT_READER_CORE_H_ | 6 #define MEDIA_BASE_BIT_READER_CORE_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/logging.h" | 8 #include "base/logging.h" |
| 10 #include "media/base/media_export.h" | 9 #include "media/base/media_export.h" |
| 11 | 10 |
| 12 namespace media { | 11 namespace media { |
| 13 | 12 |
| 14 class MEDIA_EXPORT BitReaderCore { | 13 class MEDIA_EXPORT BitReaderCore { |
| 15 public: | 14 public: |
| 16 class ByteStreamProvider { | 15 class ByteStreamProvider { |
| 17 public: | 16 public: |
| 18 ByteStreamProvider(); | 17 ByteStreamProvider(); |
| 19 virtual ~ByteStreamProvider(); | 18 virtual ~ByteStreamProvider(); |
| 20 | 19 |
| 21 // Consume at most the following |max_n| bytes of the stream | 20 // Consume at most the following |max_n| bytes of the stream |
| 22 // and return the number n of bytes actually consumed. | 21 // and return the number n of bytes actually consumed. |
| 23 // Set |*array| to point to a memory buffer containing those n bytes. | 22 // Set |*array| to point to a memory buffer containing those n bytes. |
| 24 // Note: |*array| must be valid until the next call to GetBytes | 23 // Note: |*array| must be valid until the next call to GetBytes |
| 25 // but there is no guarantee it is valid after. | 24 // but there is no guarantee it is valid after. |
| 26 virtual int GetBytes(int max_n, const uint8** array) = 0; | 25 virtual int GetBytes(int max_n, const uint8_t** array) = 0; |
| 27 }; | 26 }; |
| 28 | 27 |
| 29 // Lifetime of |byte_stream_provider| must be longer than BitReaderCore. | 28 // Lifetime of |byte_stream_provider| must be longer than BitReaderCore. |
| 30 explicit BitReaderCore(ByteStreamProvider* byte_stream_provider); | 29 explicit BitReaderCore(ByteStreamProvider* byte_stream_provider); |
| 31 ~BitReaderCore(); | 30 ~BitReaderCore(); |
| 32 | 31 |
| 33 // Read one bit from the stream and return it as a boolean in |*out|. | 32 // Read one bit from the stream and return it as a boolean in |*out|. |
| 34 // Remark: we do not use the template version for reading a bool | 33 // Remark: we do not use the template version for reading a bool |
| 35 // since it generates some optimization warnings during compilation | 34 // since it generates some optimization warnings during compilation |
| 36 // on Windows platforms. | 35 // on Windows platforms. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 47 // - |num_bits| cannot be larger than the bits the type can hold. | 46 // - |num_bits| cannot be larger than the bits the type can hold. |
| 48 // - From the above description, passing a signed type in |T| does not | 47 // - From the above description, passing a signed type in |T| does not |
| 49 // mean the first bit read from the stream gives the sign of the value. | 48 // mean the first bit read from the stream gives the sign of the value. |
| 50 // Return false if the given number of bits cannot be read (not enough | 49 // Return false if the given number of bits cannot be read (not enough |
| 51 // bits in the stream), true otherwise. When return false, the stream will | 50 // bits in the stream), true otherwise. When return false, the stream will |
| 52 // enter a state where further ReadBits/SkipBits operations will always | 51 // enter a state where further ReadBits/SkipBits operations will always |
| 53 // return false unless |num_bits| is 0. The type |T| has to be a primitive | 52 // return false unless |num_bits| is 0. The type |T| has to be a primitive |
| 54 // integer type. | 53 // integer type. |
| 55 template<typename T> bool ReadBits(int num_bits, T* out) { | 54 template<typename T> bool ReadBits(int num_bits, T* out) { |
| 56 DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8)); | 55 DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8)); |
| 57 uint64 temp; | 56 uint64_t temp; |
| 58 bool ret = ReadBitsInternal(num_bits, &temp); | 57 bool ret = ReadBitsInternal(num_bits, &temp); |
| 59 *out = static_cast<T>(temp); | 58 *out = static_cast<T>(temp); |
| 60 return ret; | 59 return ret; |
| 61 } | 60 } |
| 62 | 61 |
| 63 // Read one bit from the stream and return it as a boolean in |*flag|. | 62 // Read one bit from the stream and return it as a boolean in |*flag|. |
| 64 bool ReadFlag(bool* flag); | 63 bool ReadFlag(bool* flag); |
| 65 | 64 |
| 66 // Retrieve some bits without actually consuming them. | 65 // Retrieve some bits without actually consuming them. |
| 67 // Bits returned in |*out| are shifted so the most significant bit contains | 66 // Bits returned in |*out| are shifted so the most significant bit contains |
| 68 // the next bit that can be read from the stream. | 67 // the next bit that can be read from the stream. |
| 69 // Return the number of bits actually written in |out|. | 68 // Return the number of bits actually written in |out|. |
| 70 // Note: |num_bits| is just a suggestion of how many bits the caller | 69 // Note: |num_bits| is just a suggestion of how many bits the caller |
| 71 // wish to get in |*out| and must be less than 64: | 70 // wish to get in |*out| and must be less than 64: |
| 72 // - The number of bits returned can be more than |num_bits|. | 71 // - The number of bits returned can be more than |num_bits|. |
| 73 // - However, it will be strictly less than |num_bits| | 72 // - However, it will be strictly less than |num_bits| |
| 74 // if and only if there are not enough bits left in the stream. | 73 // if and only if there are not enough bits left in the stream. |
| 75 int PeekBitsMsbAligned(int num_bits, uint64* out); | 74 int PeekBitsMsbAligned(int num_bits, uint64_t* out); |
| 76 | 75 |
| 77 // Skip |num_bits| next bits from stream. Return false if the given number of | 76 // Skip |num_bits| next bits from stream. Return false if the given number of |
| 78 // bits cannot be skipped (not enough bits in the stream), true otherwise. | 77 // bits cannot be skipped (not enough bits in the stream), true otherwise. |
| 79 // When return false, the stream will enter a state where further | 78 // When return false, the stream will enter a state where further |
| 80 // ReadBits/ReadFlag/SkipBits operations | 79 // ReadBits/ReadFlag/SkipBits operations |
| 81 // will always return false unless |num_bits| is 0. | 80 // will always return false unless |num_bits| is 0. |
| 82 bool SkipBits(int num_bits); | 81 bool SkipBits(int num_bits); |
| 83 | 82 |
| 84 // Returns the number of bits read so far. | 83 // Returns the number of bits read so far. |
| 85 int bits_read() const; | 84 int bits_read() const; |
| 86 | 85 |
| 87 private: | 86 private: |
| 88 // This function can skip any number of bits but is more efficient | 87 // This function can skip any number of bits but is more efficient |
| 89 // for small numbers. Return false if the given number of bits cannot be | 88 // for small numbers. Return false if the given number of bits cannot be |
| 90 // skipped (not enough bits in the stream), true otherwise. | 89 // skipped (not enough bits in the stream), true otherwise. |
| 91 bool SkipBitsSmall(int num_bits); | 90 bool SkipBitsSmall(int num_bits); |
| 92 | 91 |
| 93 // Help function used by ReadBits to avoid inlining the bit reading logic. | 92 // Help function used by ReadBits to avoid inlining the bit reading logic. |
| 94 bool ReadBitsInternal(int num_bits, uint64* out); | 93 bool ReadBitsInternal(int num_bits, uint64_t* out); |
| 95 | 94 |
| 96 // Refill bit registers to have at least |min_nbits| bits available. | 95 // Refill bit registers to have at least |min_nbits| bits available. |
| 97 // Return true if the mininimum bit count condition is met after the refill. | 96 // Return true if the mininimum bit count condition is met after the refill. |
| 98 bool Refill(int min_nbits); | 97 bool Refill(int min_nbits); |
| 99 | 98 |
| 100 // Refill the current bit register from the next bit register. | 99 // Refill the current bit register from the next bit register. |
| 101 void RefillCurrentRegister(); | 100 void RefillCurrentRegister(); |
| 102 | 101 |
| 103 ByteStreamProvider* const byte_stream_provider_; | 102 ByteStreamProvider* const byte_stream_provider_; |
| 104 | 103 |
| 105 // Number of bits read so far. | 104 // Number of bits read so far. |
| 106 int bits_read_; | 105 int bits_read_; |
| 107 | 106 |
| 108 // Number of bits in |reg_| that have not been consumed yet. | 107 // Number of bits in |reg_| that have not been consumed yet. |
| 109 // Note: bits are consumed from MSB to LSB. | 108 // Note: bits are consumed from MSB to LSB. |
| 110 int nbits_; | 109 int nbits_; |
| 111 uint64 reg_; | 110 uint64_t reg_; |
| 112 | 111 |
| 113 // Number of bits in |reg_next_| that have not been consumed yet. | 112 // Number of bits in |reg_next_| that have not been consumed yet. |
| 114 // Note: bits are consumed from MSB to LSB. | 113 // Note: bits are consumed from MSB to LSB. |
| 115 int nbits_next_; | 114 int nbits_next_; |
| 116 uint64 reg_next_; | 115 uint64_t reg_next_; |
| 117 | 116 |
| 118 DISALLOW_COPY_AND_ASSIGN(BitReaderCore); | 117 DISALLOW_COPY_AND_ASSIGN(BitReaderCore); |
| 119 }; | 118 }; |
| 120 | 119 |
| 121 } // namespace media | 120 } // namespace media |
| 122 | 121 |
| 123 #endif // MEDIA_BASE_BIT_READER_CORE_H_ | 122 #endif // MEDIA_BASE_BIT_READER_CORE_H_ |
| OLD | NEW |