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 // Initialize the reader to start reading at |data|, |size| being size | |
23 // of |data| in bytes. | |
24 BitReader(const uint8* data, off_t size); | |
25 ~BitReader(); | |
26 | |
27 // Read |num_bits| next bits from stream and return in |*out|, first bit | |
28 // from the stream starting at |num_bits| position in |*out|. | |
29 // |num_bits| cannot be larger than the bits the type can hold. | |
30 // Return false if the given number of bits cannot be read (not enough | |
31 // bits in the stream), true otherwise. When return false, the stream will | |
32 // enter a state where further ReadBits/SkipBits operations will always | |
33 // return false unless |num_bits| is 0. The type |T| has to be a primitive | |
34 // integer type. | |
35 template<typename T> | |
36 bool ReadBits(int num_bits, T *out) { | |
Ami GONE FROM CHROMIUM
2012/07/17 23:22:18
This inlines the entire method definition at every
xiaomings
2012/07/19 00:16:32
Use ReadInternal to avoid duplicate code.
| |
37 DCHECK(num_bits <= static_cast<int>(sizeof(T) * 8)); | |
Ami GONE FROM CHROMIUM
2012/07/17 23:22:18
Replace <= with _LE
xiaomings
2012/07/19 00:16:32
Done.
| |
38 | |
39 *out = 0; | |
40 | |
41 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) { | |
42 int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits); | |
43 *out = (*out << bits_to_take) + | |
Ami GONE FROM CHROMIUM
2012/07/17 23:22:18
This can left-shift by the width of *out, which is
xiaomings
2012/07/19 00:16:32
Done.
| |
44 (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take)); | |
45 num_bits -= bits_to_take; | |
46 num_remaining_bits_in_curr_byte_ -= bits_to_take; | |
47 curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1; | |
48 | |
49 if (num_remaining_bits_in_curr_byte_ == 0) | |
50 UpdateCurrByte(); | |
51 } | |
52 | |
53 if (num_bits == 0) | |
54 return true; | |
55 | |
56 *out = 0; | |
57 num_remaining_bits_in_curr_byte_ = 0; | |
Ami GONE FROM CHROMIUM
2012/07/17 23:22:18
how can we get here and this isn't already the cas
xiaomings
2012/07/19 00:16:32
Done.
| |
58 bytes_left_ = 0; | |
Ami GONE FROM CHROMIUM
2012/07/17 23:22:18
ditto
xiaomings
2012/07/19 00:16:32
Done.
| |
59 | |
60 return false; | |
61 } | |
62 | |
63 // Skip |num_bits| next bits from stream. Return false if the given number | |
64 // of bits cannot be skipped (not enough bits in the stream), true | |
65 // otherwise. When return false, the stream will enter a state where | |
66 // further ReadBits/SkipBits operations will always return false unless | |
67 // |num_bits| is 0. | |
68 bool SkipBits(int num_bits); | |
Ami GONE FROM CHROMIUM
2012/07/17 23:22:18
I question the value of this API. Was is this mor
xiaomings
2012/07/19 00:16:32
Done.
| |
69 | |
70 // Return the number of bits left in the stream. | |
71 off_t NumBitsLeft() const; | |
Ami GONE FROM CHROMIUM
2012/07/17 23:22:18
Like SkipBits, I suspect this not the API you want
xiaomings
2012/07/19 00:16:32
Done.
| |
72 | |
73 protected: | |
Ami GONE FROM CHROMIUM
2012/07/17 23:22:18
Why protected?
(esp. given no virtuals in this cla
xiaomings
2012/07/19 00:16:32
Done.
| |
74 // Advance to the next byte, loading it into curr_byte_. | |
75 // If the num_remaining_bits_in_curr_byte_ is 0 after this function returns, | |
76 // the stream has reached the end. | |
77 void UpdateCurrByte(); | |
78 | |
79 // Pointer to the next unread (not in curr_byte_) byte in the stream. | |
80 const uint8* data_; | |
81 | |
82 // Bytes left in the stream (without the curr_byte_). | |
83 off_t bytes_left_; | |
84 | |
85 // Contents of the current byte; first unread bit starting at position | |
86 // 8 - num_remaining_bits_in_curr_byte_ from MSB. | |
87 uint8 curr_byte_; | |
88 | |
89 // Number of bits remaining in curr_byte_ | |
90 int num_remaining_bits_in_curr_byte_; | |
91 | |
92 private: | |
93 DISALLOW_COPY_AND_ASSIGN(BitReader); | |
94 }; | |
95 | |
96 } // namespace media | |
97 | |
98 #endif // MEDIA_BASE_BIT_READER_H_ | |
OLD | NEW |