OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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_H_ | |
6 #define MEDIA_BASE_BIT_READER_H_ | |
7 | |
8 #include <sys/types.h> | |
9 #include <algorithm> | |
10 #include <climits> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/logging.h" | |
14 #include "media/base/media_export.h" | |
15 | |
16 | |
17 namespace media { | |
18 | |
19 // A class to read bit streams. | |
20 class MEDIA_EXPORT BitReader { | |
21 public: | |
22 BitReader(); | |
acolwell GONE FROM CHROMIUM
2012/07/17 20:07:32
Remove this constructor.
xiaomings
2012/07/17 22:09:44
Done.
| |
23 BitReader(const uint8* data, off_t size); | |
24 ~BitReader(); | |
25 | |
26 // Initialize the reader to start reading at |data|, |size| being size | |
27 // of |data| in bytes. | |
28 void Initialize(const uint8* data, off_t size); | |
acolwell GONE FROM CHROMIUM
2012/07/17 20:07:32
Remove this and put it's body into the two argumen
xiaomings
2012/07/17 22:09:44
Done.
| |
29 | |
30 // Read |num_bits| next bits from stream and return in |*out|, first bit | |
31 // from the stream starting at |num_bits| position in |*out|. | |
32 // |num_bits| cannot be larger than the bits the type can hold. | |
33 // Return false if the given number of bits cannot be read (not enough | |
34 // bits in the stream), true otherwise. When return false, the stream will | |
35 // enter a state where further ReadBits operations will always return false | |
36 // unless |num_bits| is 0. The type |T| has to be a primitive integer type. | |
37 template<typename T> | |
38 bool ReadBits(int num_bits, T *out) { | |
39 DCHECK(num_bits <= static_cast<int>(sizeof(T) * CHAR_BIT)); | |
acolwell GONE FROM CHROMIUM
2012/07/17 20:07:32
Any reason not to use 8 instead of CHAR_BIT?
xiaomings
2012/07/17 22:09:44
Had thought a predefined MACRO might be better. Bu
| |
40 | |
41 *out = 0; | |
42 position_ += num_bits; | |
43 | |
44 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) { | |
45 int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits); | |
46 *out = (*out << bits_to_take) + | |
47 (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take)); | |
48 num_bits -= bits_to_take; | |
49 num_remaining_bits_in_curr_byte_ -= bits_to_take; | |
50 curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1; | |
51 | |
52 if (num_remaining_bits_in_curr_byte_ == 0) | |
53 UpdateCurrByte(); | |
54 } | |
55 | |
56 if (num_bits == 0) | |
57 return true; | |
58 | |
59 *out = 0; | |
60 num_remaining_bits_in_curr_byte_ = 0; | |
61 bytes_left_ = 0; | |
62 | |
63 return false; | |
64 } | |
65 | |
66 bool SkipBits(int num_bits); | |
acolwell GONE FROM CHROMIUM
2012/07/17 20:07:32
Add comment documenting this method's behavior and
xiaomings
2012/07/17 22:09:44
Done.
| |
67 | |
68 // Return the current position in the stream in unit of bit. | |
69 off_t Tell() const; | |
acolwell GONE FROM CHROMIUM
2012/07/17 20:07:32
Do we really need this method now? Only tests are
xiaomings
2012/07/17 22:09:44
Removed
| |
70 | |
71 // Return the number of bits left in the stream. | |
72 off_t NumBitsLeft() const; | |
73 | |
74 protected: | |
75 // Advance to the next byte, loading it into curr_byte_. | |
76 // If the num_remaining_bits_in_curr_byte_ is 0 after this function returns, | |
77 // the stream has reached the end. | |
78 void UpdateCurrByte(); | |
79 | |
80 // Pointer to the next unread (not in curr_byte_) byte in the stream. | |
81 const uint8* data_; | |
82 | |
83 // Bytes left in the stream (without the curr_byte_). | |
84 off_t bytes_left_; | |
85 | |
86 // Current position in bits. | |
87 off_t position_; | |
88 | |
89 // Contents of the current byte; first unread bit starting at position | |
90 // 8 - num_remaining_bits_in_curr_byte_ from MSB. | |
91 uint8 curr_byte_; | |
92 | |
93 // Number of bits remaining in curr_byte_ | |
94 int num_remaining_bits_in_curr_byte_; | |
95 | |
96 private: | |
97 DISALLOW_COPY_AND_ASSIGN(BitReader); | |
98 }; | |
99 | |
100 } // namespace media | |
101 | |
102 #endif // MEDIA_BASE_BIT_READER_H_ | |
OLD | NEW |