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 | |
11 #include "base/basictypes.h" | |
12 #include "base/logging.h" | |
13 #include "media/base/media_export.h" | |
14 | |
15 | |
16 namespace media { | |
17 | |
18 // A class to read bit streams. | |
19 // Classes inherited this class can override its UpdateCurrByte function | |
20 // to support specific escape mechanism, which is widely used by streaming | |
21 // formats like H.264 Annex B. | |
22 // TODO(posciak): need separate unittests for this class. | |
23 class MEDIA_EXPORT BitReader { | |
24 public: | |
25 BitReader(); | |
26 BitReader(const void* data, off_t size); | |
27 virtual ~BitReader(); | |
28 | |
29 // Initialize the reader to start reading at |data|, |size| being size | |
30 // of |data| in bytes. | |
31 // Return false on insufficient size of stream.. | |
acolwell GONE FROM CHROMIUM
2012/06/28 17:31:25
Remove this line since you've removed the return v
| |
32 void Initialize(const void* data, off_t size); | |
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 status | |
39 // remains unchanged. The type |T| has to be a primitive integer type. | |
40 template<typename T> | |
41 bool ReadBits(int num_bits, T *out) { | |
42 BitReader save = *this; | |
43 DCHECK(num_bits <= static_cast<int>(sizeof(T) * kBitsInByte)); | |
44 | |
45 *out = 0; | |
46 position_ += num_bits; | |
47 | |
48 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) { | |
49 int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits); | |
50 *out = *out * (1 << bits_to_take) + | |
strobe_
2012/06/28 14:58:22
Windows try bot: "..\media/base/bit_reader.h(50) :
acolwell GONE FROM CHROMIUM
2012/06/28 17:31:25
nit: Any reason not to use "(*out << bits_to_take)
| |
51 (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take)); | |
52 num_bits -= bits_to_take; | |
53 num_remaining_bits_in_curr_byte_ -= bits_to_take; | |
54 curr_byte_ %= (1 << num_remaining_bits_in_curr_byte_); | |
acolwell GONE FROM CHROMIUM
2012/06/28 17:31:25
nit: You're dealing with bits here so I'd expect y
| |
55 | |
56 if (num_remaining_bits_in_curr_byte_ == 0) | |
57 UpdateCurrByte(); | |
58 } | |
59 | |
60 if (num_bits != 0) { | |
61 *out = 0; | |
62 *this = save; | |
acolwell GONE FROM CHROMIUM
2012/06/28 17:31:25
Whoa.. I'm not a fan of this. Why don't you just c
| |
63 } | |
64 | |
65 return num_bits == 0; | |
66 } | |
67 | |
68 bool SkipBits(int num_bits); | |
69 | |
70 // Return the current position in the stream in unit of bit. | |
71 // This includes the skipped escape bytes if there are any. | |
72 off_t Tell() const; | |
73 | |
74 // Return the number of bits left in the stream. | |
75 // This doesn't take any escape sequence into account. | |
76 off_t NumBitsLeft() const; | |
77 | |
78 bool HasMoreData() const; | |
79 | |
80 protected: | |
81 // Advance to the next byte, loading it into curr_byte_. | |
82 // This function can be overridden to support specific escape mechanism. | |
83 // If the num_remaining_bits_in_curr_byte_ is 0 after this function returns, | |
84 // the stream has reached the end. | |
85 virtual void UpdateCurrByte(); | |
86 | |
87 // Pointer to the next unread (not in curr_byte_) byte in the stream. | |
88 const uint8* data_; | |
89 | |
90 // Bytes left in the stream (without the curr_byte_). | |
91 off_t bytes_left_; | |
92 | |
93 // Current position in bits. | |
94 off_t position_; | |
95 | |
96 // Contents of the current byte; first unread bit starting at position | |
97 // 8 - num_remaining_bits_in_curr_byte_ from MSB. | |
98 uint8 curr_byte_; | |
99 | |
100 // Number of bits remaining in curr_byte_ | |
101 int num_remaining_bits_in_curr_byte_; | |
102 | |
103 private: | |
104 static const size_t kBitsInByte = 8; | |
105 }; | |
106 | |
107 } // namespace media | |
108 | |
109 #endif // MEDIA_BASE_BIT_READER_H_ | |
OLD | NEW |