Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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_core.h" | |
| 6 | |
| 7 #include <base/sys_byteorder.h> | |
| 8 | |
| 9 namespace media { | |
| 10 | |
| 11 BitReaderCore::ByteStreamProvider::ByteStreamProvider() { | |
| 12 } | |
| 13 | |
| 14 BitReaderCore::ByteStreamProvider::~ByteStreamProvider() { | |
| 15 } | |
| 16 | |
| 17 BitReaderCore::BitReaderCore(ByteStreamProvider* byte_stream_provider) | |
| 18 : byte_stream_provider_(byte_stream_provider), | |
| 19 bit_count_(0), | |
| 20 nbits_(0), | |
| 21 reg_(0), | |
| 22 nbits_next_(0), | |
| 23 reg_next_(0) { | |
| 24 } | |
| 25 | |
| 26 BitReaderCore::~BitReaderCore() { | |
| 27 } | |
| 28 | |
| 29 bool BitReaderCore::ReadFlag(bool* flag) { | |
| 30 if (nbits_ == 0 && !Refill(1)) | |
| 31 return false; | |
| 32 | |
| 33 *flag = (static_cast<int64>(reg_) < 0); | |
|
damienv1
2013/12/26 19:35:04
Should be static_cast<SignedRegType>
damienv1
2013/12/27 19:41:36
Done.
| |
| 34 reg_ <<= 1; | |
| 35 nbits_--; | |
| 36 bit_count_++; | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 bool BitReaderCore::ReadUE(uint32* out) { | |
| 41 // Get the number of leading zeros. | |
| 42 int zero_count = -1; | |
| 43 bool is_one; | |
| 44 do { | |
| 45 if (nbits_ == 0 && !Refill(1)) | |
| 46 return false; | |
| 47 is_one = (static_cast<int64>(reg_) < 0); | |
|
damienv1
2013/12/26 19:35:04
Should be static_cast<SignedRegType>
damienv1
2013/12/27 19:41:36
Done.
| |
| 48 reg_ <<= 1; | |
| 49 nbits_--; | |
| 50 zero_count++; | |
| 51 } while(!is_one); | |
| 52 bit_count_ += (zero_count + 1); | |
| 53 | |
| 54 // If zero_count is greater than 31, the calculated value will overflow. | |
| 55 if (zero_count > 31) | |
| 56 return false; | |
| 57 | |
| 58 // Read the actual value. | |
| 59 uint32 base = (1 << zero_count) - 1; | |
| 60 uint32 offset; | |
| 61 if (!ReadBits(zero_count, &offset)) | |
| 62 return false; | |
| 63 *out = base + offset; | |
| 64 | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 bool BitReaderCore::SkipBits(int num_bits) { | |
| 69 DCHECK_GE(num_bits, 0); | |
| 70 DVLOG_IF(0, num_bits > 100) | |
| 71 << "BitReader::SkipBits inefficient for large skips"; | |
| 72 | |
| 73 uint64 dummy; | |
| 74 while (num_bits >= 32) { | |
| 75 if (!ReadBitsInternal(32, &dummy)) | |
| 76 return false; | |
| 77 num_bits -= 32; | |
| 78 } | |
| 79 return ReadBitsInternal(num_bits, &dummy); | |
| 80 } | |
| 81 | |
| 82 int BitReaderCore::GetBitCount() const { | |
| 83 return bit_count_; | |
| 84 } | |
| 85 | |
| 86 int BitReaderCore::bits_available() const { | |
| 87 return nbits_ + nbits_next_; | |
| 88 } | |
| 89 | |
| 90 bool BitReaderCore::HasMoreRBSPData() { | |
| 91 // Try to have at least 9 bits (strictly more than 1 byte). | |
|
damienv1
2013/12/26 19:35:04
if (nbits_ < 9)
damienv1
2013/12/27 19:41:36
Done.
| |
| 92 Refill(9); | |
| 93 | |
| 94 // Not on last byte. | |
| 95 if (nbits_ > 8) | |
| 96 return true; | |
| 97 | |
| 98 // Last byte, look for stop bit; | |
| 99 // We have more RBSP data if the last non-zero bit we find is not the | |
| 100 // first available bit. | |
| 101 return (reg_ << 1) != 0; | |
| 102 } | |
| 103 | |
| 104 bool BitReaderCore::ReadBitsInternal(int num_bits, uint64* out) { | |
| 105 DCHECK_GE(num_bits, 0); | |
| 106 | |
| 107 if (num_bits == 0) { | |
| 108 *out = 0; | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 if (num_bits > nbits_ && !Refill(num_bits)) { | |
| 113 // Any subsequent ReadBits should fail: | |
| 114 // empty the current bit register for that purposer. | |
| 115 nbits_ = 0; | |
| 116 reg_ = 0; | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 bit_count_ += num_bits; | |
| 121 | |
| 122 if (num_bits == sizeof(RegType) * CHAR_BIT) { | |
| 123 // Special case needed since for example for a 64 bit integer "a" | |
| 124 // "a << 64" is not defined by the C/C++ standard. | |
| 125 *out = reg_; | |
| 126 reg_ = 0; | |
| 127 nbits_ = 0; | |
| 128 return true; | |
| 129 } | |
| 130 | |
| 131 *out = reg_ >> (sizeof(RegType) * CHAR_BIT - num_bits); | |
| 132 reg_ <<= num_bits; | |
| 133 nbits_ -= num_bits; | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 bool BitReaderCore::Refill(int min_nbits) { | |
| 138 DCHECK_LE(min_nbits, static_cast<int>(sizeof(RegType) * CHAR_BIT)); | |
| 139 | |
| 140 // Transfer from the next to the current register. | |
| 141 RefillCurrentRegister(); | |
| 142 if (min_nbits <= nbits_) | |
| 143 return true; | |
| 144 DCHECK_EQ(nbits_next_, 0); | |
| 145 DCHECK_EQ(reg_next_, 0u); | |
| 146 | |
| 147 // Strict minimum number of bytes to refill. | |
| 148 int min_nbytes = (min_nbits - nbits_ + CHAR_BIT - 1) / CHAR_BIT; | |
| 149 | |
| 150 // Max number of bytes to refill. | |
| 151 int max_nbytes = sizeof(RegType); | |
| 152 | |
| 153 // Refill. | |
| 154 const uint8* byte_stream_window; | |
| 155 int window_size = | |
| 156 byte_stream_provider_->GetBytes( | |
| 157 min_nbytes, max_nbytes, &byte_stream_window); | |
| 158 if (window_size < min_nbytes) | |
| 159 return false; | |
| 160 | |
| 161 reg_next_ = 0; | |
| 162 memcpy(®_next_, byte_stream_window, window_size); | |
| 163 reg_next_ = base::NetToHost64(reg_next_); | |
| 164 nbits_next_ = window_size * CHAR_BIT; | |
| 165 | |
| 166 // Transfer from the next to the current register. | |
| 167 RefillCurrentRegister(); | |
| 168 DCHECK_LE(min_nbits, nbits_); | |
| 169 | |
| 170 return true; | |
| 171 } | |
| 172 | |
| 173 void BitReaderCore::RefillCurrentRegister() { | |
|
damienv1
2013/12/26 19:35:04
Comment.
damienv1
2013/12/27 19:41:36
Done.
| |
| 174 if (nbits_ == sizeof(RegType) * CHAR_BIT || nbits_next_ == 0) | |
| 175 return; | |
| 176 | |
| 177 reg_ |= (reg_next_ >> nbits_); | |
| 178 | |
| 179 int free_nbits = static_cast<int>(sizeof(RegType) * CHAR_BIT) - nbits_; | |
| 180 if (free_nbits >= nbits_next_) { | |
| 181 nbits_ += nbits_next_; | |
| 182 reg_next_ = 0; | |
| 183 nbits_next_ = 0; | |
| 184 } else { | |
| 185 nbits_ += free_nbits; | |
| 186 reg_next_ <<= free_nbits; | |
| 187 nbits_next_ -= free_nbits; | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 } // namespace media | |
| OLD | NEW |