OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_H_ | 5 #ifndef MEDIA_BASE_BIT_READER_BASE_H_ |
6 #define MEDIA_BASE_BIT_READER_H_ | 6 #define MEDIA_BASE_BIT_READER_BASE_H_ |
7 | |
8 #include <sys/types.h> | |
9 | 7 |
10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
11 #include "base/logging.h" | 9 #include "base/logging.h" |
12 #include "media/base/media_export.h" | 10 #include "media/base/media_export.h" |
13 | 11 |
14 namespace media { | 12 namespace media { |
15 | 13 |
16 // A class to read bit streams. | 14 class MEDIA_EXPORT BitReaderBase { |
17 class MEDIA_EXPORT BitReader { | |
18 public: | 15 public: |
19 // Initialize the reader to start reading at |data|, |size| being size | 16 BitReaderBase(); |
20 // of |data| in bytes. | 17 virtual ~BitReaderBase(); |
21 BitReader(const uint8* data, off_t size); | 18 |
22 ~BitReader(); | 19 virtual int GetBytes(int min_nbytes, int max_nbytes, const uint8** out) = 0; |
23 | 20 |
24 // Read |num_bits| next bits from stream and return in |*out|, first bit | 21 // Read |num_bits| next bits from stream and return in |*out|, first bit |
25 // from the stream starting at |num_bits| position in |*out|. | 22 // from the stream starting at |num_bits| position in |*out|. |
26 // |num_bits| cannot be larger than the bits the type can hold. | 23 // |num_bits| cannot be larger than the bits the type can hold. |
27 // Return false if the given number of bits cannot be read (not enough | 24 // Return false if the given number of bits cannot be read (not enough |
28 // bits in the stream), true otherwise. When return false, the stream will | 25 // bits in the stream), true otherwise. When return false, the stream will |
29 // enter a state where further ReadBits/SkipBits operations will always | 26 // enter a state where further ReadBits/SkipBits operations will always |
30 // return false unless |num_bits| is 0. The type |T| has to be a primitive | 27 // return false unless |num_bits| is 0. The type |T| has to be a primitive |
31 // integer type. | 28 // integer type. |
| 29 // T can especially not be a boolean since it generates |
| 30 // some optimization warnings during compilation on windows platforms, |
| 31 // use ReadFlag instead. |
32 template<typename T> bool ReadBits(int num_bits, T *out) { | 32 template<typename T> bool ReadBits(int num_bits, T *out) { |
33 DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8)); | 33 DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8)); |
34 uint64 temp; | 34 uint64 temp; |
35 bool ret = ReadBitsInternal(num_bits, &temp); | 35 bool ret = ReadBitsInternal(num_bits, &temp); |
36 *out = static_cast<T>(temp); | 36 *out = static_cast<T>(temp); |
37 return ret; | 37 return ret; |
38 } | 38 } |
39 | 39 |
| 40 // Read a boolean. |
| 41 bool ReadFlag(bool* flag); |
| 42 |
| 43 // Read an unsigned exp-golomb value. |
| 44 bool ReadUE(uint32* out); |
| 45 |
40 // Skip |num_bits| next bits from stream. Return false if the given number of | 46 // Skip |num_bits| next bits from stream. Return false if the given number of |
41 // bits cannot be skipped (not enough bits in the stream), true otherwise. | 47 // bits cannot be skipped (not enough bits in the stream), true otherwise. |
42 // When return false, the stream will enter a state where further ReadBits/ | 48 // When return false, the stream will enter a state where further ReadBits/ |
43 // SkipBits operations will always return false unless |num_bits| is 0. | 49 // SkipBits operations will always return false unless |num_bits| is 0. |
44 bool SkipBits(int num_bits); | 50 bool SkipBits(int num_bits); |
45 | 51 |
46 // Returns the number of bits available for reading. | 52 protected: |
47 int bits_available() const; | 53 // Returns the number of bits available in the registers. |
| 54 int bits_available_in_registers() const; |
48 | 55 |
49 private: | 56 private: |
| 57 typedef uint64 RegType; |
| 58 |
50 // Help function used by ReadBits to avoid inlining the bit reading logic. | 59 // Help function used by ReadBits to avoid inlining the bit reading logic. |
51 bool ReadBitsInternal(int num_bits, uint64* out); | 60 bool ReadBitsInternal(int num_bits, uint64* out); |
52 | 61 |
53 // Advance to the next byte, loading it into curr_byte_. | 62 // Refill bit registers to have at least |min_nbits| bits available. |
54 // If the num_remaining_bits_in_curr_byte_ is 0 after this function returns, | 63 // Return true if the mininimum bit count condition is met after the refill. |
55 // the stream has reached the end. | 64 bool Refill(int min_nbits); |
56 void UpdateCurrByte(); | |
57 | 65 |
58 // Pointer to the next unread (not in curr_byte_) byte in the stream. | 66 // Refill the current bit register from the next bit register. |
59 const uint8* data_; | 67 void RefillCurrentRegister(); |
60 | 68 |
61 // Bytes left in the stream (without the curr_byte_). | 69 void ResetRegisters(); |
62 off_t bytes_left_; | |
63 | 70 |
64 // Contents of the current byte; first unread bit starting at position | 71 // Number of valid bits left in |reg|. |
65 // 8 - num_remaining_bits_in_curr_byte_ from MSB. | 72 int nbits_; |
66 uint8 curr_byte_; | 73 RegType reg_; |
67 | 74 |
68 // Number of bits remaining in curr_byte_ | 75 // Number of valid bits left in |reg_next_|. |
69 int num_remaining_bits_in_curr_byte_; | 76 int nbits_next_; |
| 77 RegType reg_next_; |
70 | 78 |
71 private: | 79 DISALLOW_COPY_AND_ASSIGN(BitReaderBase); |
72 DISALLOW_COPY_AND_ASSIGN(BitReader); | |
73 }; | 80 }; |
74 | 81 |
75 } // namespace media | 82 } // namespace media |
76 | 83 |
77 #endif // MEDIA_BASE_BIT_READER_H_ | 84 #endif // MEDIA_BASE_BIT_READER_BASE_H_ |
OLD | NEW |