1 // Copyright 2014 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 "bit_reader_core.h"
6
7 #include <stdint.h>
8
9 #include "base/sys_byteorder.h"
10
11 namespace {
12 const int kRegWidthInBits = sizeof(uint64_t) * 8;
13 }
14
15 namespace media {
16
ByteStreamProvider()17 BitReaderCore::ByteStreamProvider::ByteStreamProvider() {
18 }
19
~ByteStreamProvider()20 BitReaderCore::ByteStreamProvider::~ByteStreamProvider() {
21 }
22
BitReaderCore(ByteStreamProvider * byte_stream_provider)23 BitReaderCore::BitReaderCore(ByteStreamProvider* byte_stream_provider)
24 : byte_stream_provider_(byte_stream_provider),
25 bits_read_(0),
26 nbits_(0),
27 reg_(0),
28 nbits_next_(0),
29 reg_next_(0) {
30 }
31
~BitReaderCore()32 BitReaderCore::~BitReaderCore() {
33 }
34
ReadFlag(bool * flag)35 bool BitReaderCore::ReadFlag(bool* flag) {
36 if (nbits_ == 0 && !Refill(1))
37 return false;
38
39 *flag = (reg_ & (UINT64_C(1) << (kRegWidthInBits - 1))) != 0;
40 reg_ <<= 1;
41 nbits_--;
42 bits_read_++;
43 return true;
44 }
45
PeekBitsMsbAligned(int num_bits,uint64_t * out)46 int BitReaderCore::PeekBitsMsbAligned(int num_bits, uint64_t* out) {
47 // Try to have at least |num_bits| in the bit register.
48 if (nbits_ < num_bits)
49 Refill(num_bits);
50
51 *out = reg_;
52 return nbits_;
53 }
54
SkipBitsSmall(int num_bits)55 bool BitReaderCore::SkipBitsSmall(int num_bits) {
56 DCHECK_GE(num_bits, 0);
57 uint64_t dummy;
58 while (num_bits >= kRegWidthInBits) {
59 if (!ReadBitsInternal(kRegWidthInBits, &dummy))
60 return false;
61 num_bits -= kRegWidthInBits;
62 }
63 return ReadBitsInternal(num_bits, &dummy);
64 }
65
SkipBits(int num_bits)66 bool BitReaderCore::SkipBits(int num_bits) {
67 DCHECK_GE(num_bits, 0);
68
69 const int remaining_bits = nbits_ + nbits_next_;
70 if (remaining_bits >= num_bits)
71 return SkipBitsSmall(num_bits);
72
73 // Skip first the remaining available bits.
74 num_bits -= remaining_bits;
75 bits_read_ += remaining_bits;
76 nbits_ = 0;
77 reg_ = 0;
78 nbits_next_ = 0;
79 reg_next_ = 0;
80
81 // Next, skip an integer number of bytes.
82 const int nbytes = num_bits / 8;
83 if (nbytes > 0) {
84 const uint8_t* byte_stream_window;
85 const int window_size =
86 byte_stream_provider_->GetBytes(nbytes, &byte_stream_window);
87 DCHECK_GE(window_size, 0);
88 DCHECK_LE(window_size, nbytes);
89 if (window_size < nbytes) {
90 // Note that some bytes were consumed.
91 bits_read_ += 8 * window_size;
92 return false;
93 }
94 num_bits -= 8 * nbytes;
95 bits_read_ += 8 * nbytes;
96 }
97
98 // Skip the remaining bits.
99 return SkipBitsSmall(num_bits);
100 }
101
bits_read() const102 int BitReaderCore::bits_read() const {
103 return bits_read_;
104 }
105
ReadBitsInternal(int num_bits,uint64_t * out)106 bool BitReaderCore::ReadBitsInternal(int num_bits, uint64_t* out) {
107 DCHECK_GE(num_bits, 0);
108
109 if (num_bits == 0) {
110 *out = 0;
111 return true;
112 }
113
114 if (num_bits > nbits_ && !Refill(num_bits)) {
115 // Any subsequent ReadBits should fail:
116 // empty the current bit register for that purpose.
117 nbits_ = 0;
118 reg_ = 0;
119 return false;
120 }
121
122 bits_read_ += num_bits;
123
124 if (num_bits == kRegWidthInBits) {
125 // Special case needed since for example for a 64 bit integer "a"
126 // "a << 64" is not defined by the C/C++ standard.
127 *out = reg_;
128 reg_ = 0;
129 nbits_ = 0;
130 return true;
131 }
132
133 *out = reg_ >> (kRegWidthInBits - num_bits);
134 reg_ <<= num_bits;
135 nbits_ -= num_bits;
136 return true;
137 }
138
Refill(int min_nbits)139 bool BitReaderCore::Refill(int min_nbits) {
140 DCHECK_LE(min_nbits, kRegWidthInBits);
141
142 // Transfer from the next to the current register.
143 RefillCurrentRegister();
144 if (min_nbits <= nbits_)
145 return true;
146 DCHECK_EQ(nbits_next_, 0);
147 DCHECK_EQ(reg_next_, 0u);
148
149 // Max number of bytes to refill.
150 int max_nbytes = sizeof(reg_next_);
151
152 // Refill.
153 const uint8_t* byte_stream_window;
154 int window_size =
155 byte_stream_provider_->GetBytes(max_nbytes, &byte_stream_window);
156 DCHECK_GE(window_size, 0);
157 DCHECK_LE(window_size, max_nbytes);
158 if (window_size == 0)
159 return false;
160
161 reg_next_ = 0;
162 memcpy(®_next_, byte_stream_window, window_size);
163 reg_next_ = base::NetToHost64(reg_next_);
164 nbits_next_ = window_size * 8;
165
166 // Transfer from the next to the current register.
167 RefillCurrentRegister();
168
169 return (nbits_ >= min_nbits);
170 }
171
RefillCurrentRegister()172 void BitReaderCore::RefillCurrentRegister() {
173 // No refill possible if the destination register is full
174 // or the source register is empty.
175 if (nbits_ == kRegWidthInBits || nbits_next_ == 0)
176 return;
177
178 reg_ |= (reg_next_ >> nbits_);
179
180 int free_nbits = kRegWidthInBits - nbits_;
181 if (free_nbits >= nbits_next_) {
182 nbits_ += nbits_next_;
183 reg_next_ = 0;
184 nbits_next_ = 0;
185 return;
186 }
187
188 nbits_ += free_nbits;
189 reg_next_ <<= free_nbits;
190 nbits_next_ -= free_nbits;
191 }
192
193 } // namespace media
194