Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
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.
5 #include "mp4_demuxer/bit_reader.h"
6 #include <algorithm>
8 namespace mp4_demuxer {
10 BitReader::BitReader(const uint8_t* data, off_t size)
11 : data_(data), bytes_left_(size), num_remaining_bits_in_curr_byte_(0) {
12 DCHECK(data_ != nullptr && bytes_left_ > 0);
14 UpdateCurrByte();
15 }
17 BitReader::~BitReader() {}
19 int BitReader::bits_available() const {
20 return 8 * bytes_left_ + num_remaining_bits_in_curr_byte_;
21 }
23 bool BitReader::ReadBitsInternal(int num_bits, uint64_t* out) {
24 DCHECK_LE(num_bits, 64);
26 *out = 0;
28 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) {
29 int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits);
31 *out <<= bits_to_take;
32 *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take);
33 num_bits -= bits_to_take;
34 num_remaining_bits_in_curr_byte_ -= bits_to_take;
35 curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1;
37 if (num_remaining_bits_in_curr_byte_ == 0)
38 UpdateCurrByte();
39 }
41 return num_bits == 0;
42 }
44 void BitReader::UpdateCurrByte() {
45 DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0);
47 if (bytes_left_ == 0)
48 return;
50 // Load a new byte and advance pointers.
51 curr_byte_ = *data_;
52 ++data_;
53 --bytes_left_;
54 num_remaining_bits_in_curr_byte_ = 8;
55 }
57 } // namespace mp4_demuxer