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 | |
10 #include "base/basictypes.h" | |
11 #include "base/logging.h" | |
12 #include "media/base/media_export.h" | |
13 | |
14 namespace media { | |
15 | |
16 // A class to read bit streams. | |
17 class MEDIA_EXPORT BitReader { | |
18 public: | |
19 // Initialize the reader to start reading at |data|, |size| being size | |
20 // of |data| in bytes. | |
21 BitReader(const uint8* data, off_t size); | |
22 ~BitReader(); | |
23 | |
24 // Read |num_bits| next bits from stream and return in |*out|, first bit | |
25 // from the stream starting at |num_bits| position in |*out|. | |
26 // |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 | |
28 // bits in the stream), true otherwise. When return false, the stream will | |
29 // 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 | |
31 // integer type. | |
32 template<typename T> | |
33 bool ReadBits(int num_bits, T *out) { | |
34 DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8)); | |
Ami GONE FROM CHROMIUM
2012/07/19 00:28:59
For being in the header there's a lot of space tak
xiaomings
2012/07/19 01:11:02
Done.
| |
35 | |
36 uint64 temp; | |
37 | |
38 if (ReadBitsInternal(num_bits, &temp)) { | |
39 *out = static_cast<T>(temp); | |
40 | |
41 return true; | |
42 } | |
43 | |
44 *out = 0; | |
45 return false; | |
46 } | |
47 | |
48 private: | |
49 // Help function used by ReadBits to avoid inlining the bit reading logic. | |
50 bool ReadBitsInternal(int num_bits, uint64* out); | |
51 | |
52 // Advance to the next byte, loading it into curr_byte_. | |
53 // If the num_remaining_bits_in_curr_byte_ is 0 after this function returns, | |
54 // the stream has reached the end. | |
55 void UpdateCurrByte(); | |
56 | |
57 // Pointer to the next unread (not in curr_byte_) byte in the stream. | |
58 const uint8* data_; | |
59 | |
60 // Bytes left in the stream (without the curr_byte_). | |
61 off_t bytes_left_; | |
62 | |
63 // Contents of the current byte; first unread bit starting at position | |
64 // 8 - num_remaining_bits_in_curr_byte_ from MSB. | |
65 uint8 curr_byte_; | |
66 | |
67 // Number of bits remaining in curr_byte_ | |
68 int num_remaining_bits_in_curr_byte_; | |
69 | |
70 private: | |
71 DISALLOW_COPY_AND_ASSIGN(BitReader); | |
72 }; | |
73 | |
74 } // namespace media | |
75 | |
76 #endif // MEDIA_BASE_BIT_READER_H_ | |
OLD | NEW |