Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "mp4_demuxer/bit_reader.h" |
michael@0 | 6 | #include <algorithm> |
michael@0 | 7 | |
michael@0 | 8 | namespace mp4_demuxer { |
michael@0 | 9 | |
michael@0 | 10 | BitReader::BitReader(const uint8_t* data, off_t size) |
michael@0 | 11 | : data_(data), bytes_left_(size), num_remaining_bits_in_curr_byte_(0) { |
michael@0 | 12 | DCHECK(data_ != nullptr && bytes_left_ > 0); |
michael@0 | 13 | |
michael@0 | 14 | UpdateCurrByte(); |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | BitReader::~BitReader() {} |
michael@0 | 18 | |
michael@0 | 19 | int BitReader::bits_available() const { |
michael@0 | 20 | return 8 * bytes_left_ + num_remaining_bits_in_curr_byte_; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | bool BitReader::ReadBitsInternal(int num_bits, uint64_t* out) { |
michael@0 | 24 | DCHECK_LE(num_bits, 64); |
michael@0 | 25 | |
michael@0 | 26 | *out = 0; |
michael@0 | 27 | |
michael@0 | 28 | while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) { |
michael@0 | 29 | int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits); |
michael@0 | 30 | |
michael@0 | 31 | *out <<= bits_to_take; |
michael@0 | 32 | *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take); |
michael@0 | 33 | num_bits -= bits_to_take; |
michael@0 | 34 | num_remaining_bits_in_curr_byte_ -= bits_to_take; |
michael@0 | 35 | curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1; |
michael@0 | 36 | |
michael@0 | 37 | if (num_remaining_bits_in_curr_byte_ == 0) |
michael@0 | 38 | UpdateCurrByte(); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | return num_bits == 0; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | void BitReader::UpdateCurrByte() { |
michael@0 | 45 | DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0); |
michael@0 | 46 | |
michael@0 | 47 | if (bytes_left_ == 0) |
michael@0 | 48 | return; |
michael@0 | 49 | |
michael@0 | 50 | // Load a new byte and advance pointers. |
michael@0 | 51 | curr_byte_ = *data_; |
michael@0 | 52 | ++data_; |
michael@0 | 53 | --bytes_left_; |
michael@0 | 54 | num_remaining_bits_in_curr_byte_ = 8; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | } // namespace mp4_demuxer |