OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "media/base/bit_reader.h" |
| 6 |
| 7 namespace media { |
| 8 |
| 9 BitReader::BitReader() |
| 10 : data_(NULL), |
| 11 bytes_left_(0), |
| 12 position_(0), |
| 13 curr_byte_(0), |
| 14 num_remaining_bits_in_curr_byte_(0) { |
| 15 } |
| 16 |
| 17 BitReader::BitReader(const uint8* data, off_t size) { |
| 18 Initialize(data, size); |
| 19 } |
| 20 |
| 21 BitReader::~BitReader() {} |
| 22 |
| 23 void BitReader::Initialize(const uint8* data, off_t size) { |
| 24 DCHECK(data != NULL || size == 0); // Data cannot be NULL if size is not 0. |
| 25 |
| 26 data_ = data; |
| 27 bytes_left_ = size; |
| 28 position_ = 0; |
| 29 num_remaining_bits_in_curr_byte_ = 0; |
| 30 |
| 31 UpdateCurrByte(); |
| 32 } |
| 33 |
| 34 void BitReader::UpdateCurrByte() { |
| 35 DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0); |
| 36 |
| 37 if (bytes_left_ < 1) |
| 38 return; |
| 39 |
| 40 // Load a new byte and advance pointers. |
| 41 curr_byte_ = *data_; |
| 42 ++data_; |
| 43 --bytes_left_; |
| 44 num_remaining_bits_in_curr_byte_ = 8; |
| 45 } |
| 46 |
| 47 bool BitReader::SkipBits(int num_bits) { |
| 48 int dummy; |
| 49 const int kDummySize = static_cast<int>(sizeof(dummy)) * 8; |
| 50 |
| 51 while (num_bits > kDummySize) { |
| 52 if (ReadBits(kDummySize, &dummy)) { |
| 53 num_bits -= kDummySize; |
| 54 } else { |
| 55 return false; |
| 56 } |
| 57 } |
| 58 |
| 59 return ReadBits(num_bits, &dummy); |
| 60 } |
| 61 |
| 62 off_t BitReader::Tell() const { |
| 63 return position_; |
| 64 } |
| 65 |
| 66 off_t BitReader::NumBitsLeft() const { |
| 67 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8); |
| 68 } |
| 69 |
| 70 bool BitReader::HasMoreData() const { |
| 71 return num_remaining_bits_in_curr_byte_ != 0; |
| 72 } |
| 73 |
| 74 } // namespace media |
OLD | NEW |