Chromium Code Reviews| 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) { | |
|
acolwell GONE FROM CHROMIUM
2012/07/03 01:00:12
nit: reverse check and early return
| |
| 38 // Load a new byte and advance pointers. | |
| 39 curr_byte_ = *data_; | |
| 40 ++data_; | |
| 41 --bytes_left_; | |
| 42 num_remaining_bits_in_curr_byte_ = 8; | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 bool BitReader::SkipBits(int num_bits) { | |
| 47 int dummy; | |
| 48 const int kDummySize = static_cast<int>(sizeof(dummy)) * 8; | |
| 49 | |
| 50 while (num_bits > kDummySize) { | |
| 51 if (ReadBits(kDummySize, &dummy)) { | |
| 52 num_bits -= kDummySize; | |
| 53 } else { | |
| 54 return false; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 return ReadBits(num_bits, &dummy); | |
| 59 } | |
| 60 | |
| 61 off_t BitReader::Tell() const { | |
| 62 return position_; | |
| 63 } | |
| 64 | |
| 65 off_t BitReader::NumBitsLeft() const { | |
| 66 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8); | |
| 67 } | |
| 68 | |
| 69 bool BitReader::HasMoreData() const { | |
| 70 return num_remaining_bits_in_curr_byte_ != 0; | |
| 71 } | |
| 72 | |
| 73 } // namespace media | |
| OLD | NEW |