OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/base/bit_reader_base.h" | |
6 | |
7 #include <base/sys_byteorder.h> | |
8 | |
9 namespace media { | |
10 | |
11 BitReaderBase::BitReaderBase() | |
12 : nbits_(0), | |
13 reg_(0), | |
14 nbits_next_(0), | |
15 reg_next_(0) { | |
16 } | |
17 | |
18 BitReaderBase::~BitReaderBase() { | |
19 } | |
20 | |
21 bool BitReaderBase::ReadFlag(bool* flag) { | |
22 if (nbits_ == 0) { | |
23 if (!Refill(1)) | |
damienv1
2013/12/19 03:29:12
Could be only one condition.
damienv1
2013/12/19 21:46:34
Done.
| |
24 return false; | |
25 } | |
26 | |
27 *flag = (static_cast<int64>(reg_) < 0); | |
28 reg_ <<= 1; | |
29 nbits_--; | |
30 return true; | |
31 | |
32 } | |
33 | |
34 bool BitReaderBase::ReadUE(uint32* out) { | |
35 // Get the number of leading zeros. | |
36 int zero_count = -1; | |
37 bool is_one; | |
38 do { | |
39 if (nbits_ == 0 && !Refill(1)) | |
40 return false; | |
41 is_one = (static_cast<int64>(reg_) < 0); | |
42 reg_ <<= 1; | |
43 nbits_--; | |
damienv1
2013/12/19 03:46:51
zero_count++;
damienv1
2013/12/19 21:46:34
Done.
| |
44 } while(!is_one); | |
45 | |
46 // If zero_count is greater than 31, the calculated value will overflow. | |
47 if (zero_count > 31) | |
48 return false; | |
49 | |
50 // Read the actual value. | |
51 uint32 base = (1 << zero_count) - 1; | |
52 uint32 offset; | |
53 if (!ReadBits(zero_count, &offset)) | |
54 return false; | |
55 *out = base + offset; | |
56 | |
57 return true; | |
58 } | |
59 | |
60 bool BitReaderBase::SkipBits(int num_bits) { | |
61 DCHECK_GE(num_bits, 0); | |
62 DVLOG_IF(0, num_bits > 100) | |
63 << "BitReader::SkipBits inefficient for large skips"; | |
64 | |
65 uint64 dummy; | |
66 while (num_bits >= 32) { | |
67 if (!ReadBitsInternal(32, &dummy)) | |
68 return false; | |
69 num_bits -= 32; | |
70 } | |
71 | |
72 if (num_bits > 0) | |
73 return ReadBitsInternal(num_bits, &dummy); | |
74 | |
75 return true; | |
76 } | |
77 | |
78 int BitReaderBase::bits_available_in_registers() const { | |
79 return nbits_ + nbits_next_; | |
80 } | |
81 | |
82 bool BitReaderBase::ReadBitsInternal(int num_bits, uint64* out) { | |
83 DCHECK_GE(num_bits, 0); | |
84 | |
85 if (num_bits == 0) { | |
86 *out = 0; | |
87 return true; | |
88 } | |
89 | |
90 if (num_bits > nbits_) { | |
91 if (!Refill(num_bits)) { | |
damienv1
2013/12/19 03:29:12
Could be only one condition.
damienv1
2013/12/19 21:46:34
Done.
| |
92 ResetRegisters(); | |
93 return false; | |
94 } | |
95 } | |
96 | |
97 *out = reg_ >> (sizeof(RegType) * CHAR_BIT - num_bits); | |
98 reg_ <<= num_bits; | |
99 nbits_ -= num_bits; | |
100 return true; | |
101 } | |
102 | |
103 bool BitReaderBase::Refill(int min_nbits) { | |
104 DCHECK_LE(min_nbits, static_cast<int>(sizeof(RegType) * CHAR_BIT)); | |
105 | |
106 // Transfer from the next to the current register. | |
107 RefillCurrentRegister(); | |
108 if (min_nbits <= nbits_) | |
109 return true; | |
110 DCHECK_EQ(nbits_next_, 0); | |
111 | |
112 // Strict minimum number of bytes to refill. | |
113 int min_nbytes = (min_nbits - nbits_ + CHAR_BIT - 1) / CHAR_BIT; | |
114 | |
115 // Max number of bytes to refill. | |
116 int max_nbytes = 8; | |
damienv1
2013/12/19 02:01:25
Should be sizeof(RegType)
damienv1
2013/12/19 21:46:34
Done.
| |
117 | |
118 // Refill. | |
119 const uint8* byte_stream_window; | |
120 int window_size = GetBytes(min_nbytes, max_nbytes, &byte_stream_window); | |
121 if (window_size < min_nbytes) | |
122 return false; | |
123 | |
124 reg_next_ = 0; | |
125 memcpy(®_next_, byte_stream_window, window_size); | |
126 reg_next_ = base::ByteSwap(reg_next_); | |
damienv1
2013/12/19 03:29:12
Should be NetToHost64 !
damienv1
2013/12/19 21:46:34
Done.
| |
127 nbits_next_ = window_size * CHAR_BIT; | |
128 | |
129 // Transfer from the next to the current register. | |
130 RefillCurrentRegister(); | |
131 DCHECK_LE(min_nbits, nbits_); | |
132 | |
133 return true; | |
134 } | |
135 | |
136 void BitReaderBase::RefillCurrentRegister() { | |
137 if (nbits_ == 64 || nbits_next_ == 0) | |
damienv1
2013/12/19 02:01:25
Should be sizeof(RegType) * CHAR_BIT
damienv1
2013/12/19 21:46:34
Done.
| |
138 return; | |
139 | |
140 reg_ |= (reg_next_ >> nbits_); | |
141 | |
142 int free_nbits = static_cast<int>(sizeof(RegType) * CHAR_BIT) - nbits_; | |
143 if (free_nbits >= nbits_next_) { | |
144 nbits_ += nbits_next_; | |
145 reg_next_ = 0; | |
146 nbits_next_ = 0; | |
147 } else { | |
148 nbits_ += free_nbits; | |
149 reg_next_ <<= free_nbits; | |
150 nbits_next_ -= free_nbits; | |
151 } | |
152 } | |
153 | |
154 void BitReaderBase::ResetRegisters() { | |
155 reg_ = 0; | |
156 nbits_ = 0; | |
157 reg_next_ = 0; | |
158 nbits_next_ = 0; | |
159 } | |
160 | |
161 } // namespace media | |
OLD | NEW |