Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/base/bit_reader.h" | 5 #include "media/base/bit_reader.h" |
| 6 | 6 |
| 7 namespace media { | 7 namespace media { |
| 8 | 8 |
| 9 BitReader::BitReader(const uint8* data, off_t size) | 9 BitReader::BitReader(const uint8* data, off_t size) |
| 10 : data_(data), bytes_left_(size), num_remaining_bits_in_curr_byte_(0) { | 10 : data_(data), bytes_left_(size), num_remaining_bits_in_curr_byte_(0) { |
| 11 DCHECK(data_ != NULL && bytes_left_ > 0); | 11 DCHECK(data_ != NULL && bytes_left_ > 0); |
| 12 | 12 |
| 13 UpdateCurrByte(); | 13 UpdateCurrByte(); |
| 14 } | 14 } |
| 15 | 15 |
| 16 BitReader::~BitReader() {} | 16 BitReader::~BitReader() {} |
| 17 | 17 |
| 18 bool BitReader::SkipBits(int num_bits) { | |
| 19 DCHECK_GE(num_bits, 0); | |
| 20 | |
| 21 // Skip any bits in the current byte waiting to be processed, then | |
| 22 // process full bytes until less than 8 bits remaining. | |
| 23 while (num_bits > 0 && num_bits > num_remaining_bits_in_curr_byte_) { | |
| 24 num_bits -= num_remaining_bits_in_curr_byte_; | |
| 25 num_remaining_bits_in_curr_byte_ = 0; | |
| 26 UpdateCurrByte(); | |
|
xhwang
2013/05/22 22:18:32
nit: can we skip multiple bytes in one operation i
jrummell
2013/05/22 23:59:46
I added a DLOG message to print a warning if large
| |
| 27 | |
| 28 // If there is no more data remaining, only return true if we | |
| 29 // skipped all that were requested. | |
| 30 if (num_remaining_bits_in_curr_byte_ == 0) | |
| 31 return (num_bits == 0); | |
| 32 } | |
| 33 | |
| 34 // Less than 8 bits remaining to skip. Use ReadBitsInternal to verify | |
| 35 // that the remaining bits we need exist, and adjust them as necessary | |
| 36 // for subsequent operations. | |
| 37 uint64 not_needed; | |
| 38 return ReadBitsInternal(num_bits, ¬_needed); | |
| 39 } | |
| 40 | |
| 18 int BitReader::bits_available() const { | 41 int BitReader::bits_available() const { |
| 19 return 8 * bytes_left_ + num_remaining_bits_in_curr_byte_; | 42 return 8 * bytes_left_ + num_remaining_bits_in_curr_byte_; |
| 20 } | 43 } |
| 21 | 44 |
| 22 bool BitReader::ReadBitsInternal(int num_bits, uint64* out) { | 45 bool BitReader::ReadBitsInternal(int num_bits, uint64* out) { |
| 23 DCHECK_LE(num_bits, 64); | 46 DCHECK_LE(num_bits, 64); |
| 24 | 47 |
| 25 *out = 0; | 48 *out = 0; |
| 26 | 49 |
| 27 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) { | 50 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 47 return; | 70 return; |
| 48 | 71 |
| 49 // Load a new byte and advance pointers. | 72 // Load a new byte and advance pointers. |
| 50 curr_byte_ = *data_; | 73 curr_byte_ = *data_; |
| 51 ++data_; | 74 ++data_; |
| 52 --bytes_left_; | 75 --bytes_left_; |
| 53 num_remaining_bits_in_curr_byte_ = 8; | 76 num_remaining_bits_in_curr_byte_ = 8; |
| 54 } | 77 } |
| 55 | 78 |
| 56 } // namespace media | 79 } // namespace media |
| OLD | NEW |