Index: media/base/bit_reader.cc |
diff --git a/media/base/bit_reader.cc b/media/base/bit_reader.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..52fe3870ae6d718949642450c68cc2b3771e091d |
--- /dev/null |
+++ b/media/base/bit_reader.cc |
@@ -0,0 +1,58 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/base/bit_reader.h" |
+ |
+namespace media { |
+ |
+BitReader::BitReader(const uint8* data, off_t size) |
+ : data_(data), bytes_left_(size), num_remaining_bits_in_curr_byte_(0) { |
+ DCHECK(data_ != NULL && bytes_left_ > 0); |
+ |
+ UpdateCurrByte(); |
+} |
+ |
+BitReader::~BitReader() {} |
+ |
+bool BitReader::ReadBitsInternal(int num_bits, uint64* out) { |
+ DCHECK_LE(num_bits, 64); |
+ |
+ *out = 0; |
+ |
+ while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) { |
+ int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits); |
+ |
+ if (*out != 0) |
Ami GONE FROM CHROMIUM
2012/07/19 00:28:59
This conditional is unnecessary.
xiaomings
2012/07/19 01:11:02
Done.
|
+ *out <<= bits_to_take; |
+ *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take); |
+ num_bits -= bits_to_take; |
+ num_remaining_bits_in_curr_byte_ -= bits_to_take; |
+ curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1; |
+ |
+ if (num_remaining_bits_in_curr_byte_ == 0) |
+ UpdateCurrByte(); |
+ } |
+ |
+ if (num_bits == 0) |
+ return true; |
+ |
+ *out = 0; |
Ami GONE FROM CHROMIUM
2012/07/19 00:28:59
I'm not sure why you want this. It smells like ho
xiaomings
2012/07/19 01:11:02
Done.
|
+ |
Ami GONE FROM CHROMIUM
2012/07/19 00:28:59
extra newline
xiaomings
2012/07/19 01:11:02
Removed the if.
|
+ return false; |
+} |
+ |
+void BitReader::UpdateCurrByte() { |
+ DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0); |
+ |
+ if (bytes_left_ == 0) |
+ return; |
+ |
+ // Load a new byte and advance pointers. |
+ curr_byte_ = *data_; |
+ ++data_; |
+ --bytes_left_; |
+ num_remaining_bits_in_curr_byte_ = 8; |
+} |
+ |
+} // namespace media |