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<SignedRegType>(reg_) < 0); | |
|
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
nit: How about just using something like reg_ > ki
damienv1
2013/12/28 02:26:17
The comparison with 0 should be more efficient.
acolwell GONE FROM CHROMIUM
2014/01/06 22:44:06
I think we should let the compiler worry about tha
| |
| 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)) | |
|
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
How about this instead to avoid code duplication.
damienv1
2013/12/28 02:26:17
Might probably be best (even though there is a sli
damienv1
2013/12/28 04:02:23
Done the way you suggested but moved the code to t
| |
| 46 return false; | |
| 47 is_one = (static_cast<SignedRegType>(reg_) < 0); | |
| 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) | |
|
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
nit: Shouldn't we put this inside the loop so we c
damienv1
2013/12/28 04:02:23
I modified the behavior, it's now up to the caller
| |
| 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) { | |
|
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
nit: Why not skip 64 bits at a time since dummy is
damienv1
2013/12/28 02:26:17
Will do.
| |
| 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 (byte_stream_provider_->GetBytesLeft() * CHAR_BIT + | |
|
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
nit: Is using CHAR_BIT here and elsewhere really b
| |
| 88 nbits_next_ + nbits_); | |
| 89 } | |
| 90 | |
| 91 bool BitReaderCore::HasMoreRBSPData() { | |
| 92 // Try to have at least 9 bits (strictly more than 1 byte). | |
| 93 if (nbits_ < 9) | |
| 94 Refill(9); | |
| 95 | |
| 96 // Not on last byte. | |
| 97 if (nbits_ > 8) | |
| 98 return true; | |
| 99 | |
| 100 // Last byte, look for stop bit; | |
| 101 // We have more RBSP data if the last non-zero bit we find is not the | |
| 102 // first available bit. | |
| 103 return (reg_ << 1) != 0; | |
| 104 } | |
| 105 | |
| 106 bool BitReaderCore::ReadBitsInternal(int num_bits, uint64* out) { | |
| 107 DCHECK_GE(num_bits, 0); | |
| 108 | |
| 109 if (num_bits == 0) { | |
| 110 *out = 0; | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 if (num_bits > nbits_ && !Refill(num_bits)) { | |
| 115 // Any subsequent ReadBits should fail: | |
| 116 // empty the current bit register for that purpose. | |
| 117 nbits_ = 0; | |
| 118 reg_ = 0; | |
| 119 return false; | |
| 120 } | |
| 121 | |
| 122 bit_count_ += num_bits; | |
| 123 | |
| 124 if (num_bits == sizeof(RegType) * CHAR_BIT) { | |
| 125 // Special case needed since for example for a 64 bit integer "a" | |
| 126 // "a << 64" is not defined by the C/C++ standard. | |
| 127 *out = reg_; | |
| 128 reg_ = 0; | |
| 129 nbits_ = 0; | |
| 130 return true; | |
| 131 } | |
| 132 | |
| 133 *out = reg_ >> (sizeof(RegType) * CHAR_BIT - num_bits); | |
| 134 reg_ <<= num_bits; | |
| 135 nbits_ -= num_bits; | |
| 136 return true; | |
| 137 } | |
| 138 | |
| 139 bool BitReaderCore::Refill(int min_nbits) { | |
| 140 DCHECK_LE(min_nbits, static_cast<int>(sizeof(RegType) * CHAR_BIT)); | |
| 141 | |
| 142 // Transfer from the next to the current register. | |
| 143 RefillCurrentRegister(); | |
| 144 if (min_nbits <= nbits_) | |
| 145 return true; | |
| 146 DCHECK_EQ(nbits_next_, 0); | |
| 147 DCHECK_EQ(reg_next_, 0u); | |
| 148 | |
| 149 // Strict minimum number of bytes to refill. | |
| 150 int min_nbytes = (min_nbits - nbits_ + CHAR_BIT - 1) / CHAR_BIT; | |
| 151 | |
| 152 // Max number of bytes to refill. | |
| 153 int max_nbytes = sizeof(RegType); | |
| 154 | |
| 155 // Refill. | |
| 156 const uint8* byte_stream_window; | |
| 157 int window_size = | |
| 158 byte_stream_provider_->GetBytes( | |
| 159 min_nbytes, max_nbytes, &byte_stream_window); | |
| 160 if (window_size == 0) | |
|
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
Shouldn't this be window_size < min_nbytes? IIUC t
damienv1
2013/12/28 02:26:17
That's what I was doing originally, but Refill is
| |
| 161 return false; | |
| 162 | |
| 163 reg_next_ = 0; | |
| 164 memcpy(®_next_, byte_stream_window, window_size); | |
| 165 reg_next_ = base::NetToHost64(reg_next_); | |
| 166 nbits_next_ = window_size * CHAR_BIT; | |
| 167 | |
| 168 // Transfer from the next to the current register. | |
| 169 RefillCurrentRegister(); | |
| 170 | |
| 171 return (nbits_ >= min_nbits); | |
|
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
nit: if you adjust the condition above, I believe
damienv1
2013/12/28 02:26:17
See explanation above.
Need the current behavior f
| |
| 172 } | |
| 173 | |
| 174 void BitReaderCore::RefillCurrentRegister() { | |
| 175 // No refill possible if the destination register is full | |
| 176 // or the source register is empty. | |
| 177 if (nbits_ == sizeof(RegType) * CHAR_BIT || nbits_next_ == 0) | |
| 178 return; | |
| 179 | |
| 180 reg_ |= (reg_next_ >> nbits_); | |
| 181 | |
| 182 int free_nbits = static_cast<int>(sizeof(RegType) * CHAR_BIT) - nbits_; | |
|
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
nit: Please make a constant for static_cast<int>(s
damienv1
2013/12/28 04:02:23
Done.
| |
| 183 if (free_nbits >= nbits_next_) { | |
| 184 nbits_ += nbits_next_; | |
| 185 reg_next_ = 0; | |
| 186 nbits_next_ = 0; | |
| 187 } else { | |
|
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
nit: early return and drop else.
damienv1
2013/12/28 04:02:23
Having an early return implicity involves that one
| |
| 188 nbits_ += free_nbits; | |
| 189 reg_next_ <<= free_nbits; | |
| 190 nbits_next_ -= free_nbits; | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 } // namespace media | |
| OLD | NEW |