You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2015 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/bit_buffer.h"
  11. #include <algorithm>
  12. #include <limits>
  13. #include "rtc_base/checks.h"
  14. namespace {
  15. // Returns the lowest (right-most) |bit_count| bits in |byte|.
  16. uint8_t LowestBits(uint8_t byte, size_t bit_count) {
  17. RTC_DCHECK_LE(bit_count, 8);
  18. return byte & ((1 << bit_count) - 1);
  19. }
  20. // Returns the highest (left-most) |bit_count| bits in |byte|, shifted to the
  21. // lowest bits (to the right).
  22. uint8_t HighestBits(uint8_t byte, size_t bit_count) {
  23. RTC_DCHECK_LE(bit_count, 8);
  24. uint8_t shift = 8 - static_cast<uint8_t>(bit_count);
  25. uint8_t mask = 0xFF << shift;
  26. return (byte & mask) >> shift;
  27. }
  28. // Returns the highest byte of |val| in a uint8_t.
  29. uint8_t HighestByte(uint64_t val) {
  30. return static_cast<uint8_t>(val >> 56);
  31. }
  32. // Returns the result of writing partial data from |source|, of
  33. // |source_bit_count| size in the highest bits, to |target| at
  34. // |target_bit_offset| from the highest bit.
  35. uint8_t WritePartialByte(uint8_t source,
  36. size_t source_bit_count,
  37. uint8_t target,
  38. size_t target_bit_offset) {
  39. RTC_DCHECK(target_bit_offset < 8);
  40. RTC_DCHECK(source_bit_count < 9);
  41. RTC_DCHECK(source_bit_count <= (8 - target_bit_offset));
  42. // Generate a mask for just the bits we're going to overwrite, so:
  43. uint8_t mask =
  44. // The number of bits we want, in the most significant bits...
  45. static_cast<uint8_t>(0xFF << (8 - source_bit_count))
  46. // ...shifted over to the target offset from the most signficant bit.
  47. >> target_bit_offset;
  48. // We want the target, with the bits we'll overwrite masked off, or'ed with
  49. // the bits from the source we want.
  50. return (target & ~mask) | (source >> target_bit_offset);
  51. }
  52. // Counts the number of bits used in the binary representation of val.
  53. size_t CountBits(uint64_t val) {
  54. size_t bit_count = 0;
  55. while (val != 0) {
  56. bit_count++;
  57. val >>= 1;
  58. }
  59. return bit_count;
  60. }
  61. } // namespace
  62. namespace rtc {
  63. BitBuffer::BitBuffer(const uint8_t* bytes, size_t byte_count)
  64. : bytes_(bytes), byte_count_(byte_count), byte_offset_(), bit_offset_() {
  65. RTC_DCHECK(static_cast<uint64_t>(byte_count_) <=
  66. std::numeric_limits<uint32_t>::max());
  67. }
  68. uint64_t BitBuffer::RemainingBitCount() const {
  69. return (static_cast<uint64_t>(byte_count_) - byte_offset_) * 8 - bit_offset_;
  70. }
  71. bool BitBuffer::ReadUInt8(uint8_t* val) {
  72. uint32_t bit_val;
  73. if (!ReadBits(&bit_val, sizeof(uint8_t) * 8)) {
  74. return false;
  75. }
  76. RTC_DCHECK(bit_val <= std::numeric_limits<uint8_t>::max());
  77. *val = static_cast<uint8_t>(bit_val);
  78. return true;
  79. }
  80. bool BitBuffer::ReadUInt16(uint16_t* val) {
  81. uint32_t bit_val;
  82. if (!ReadBits(&bit_val, sizeof(uint16_t) * 8)) {
  83. return false;
  84. }
  85. RTC_DCHECK(bit_val <= std::numeric_limits<uint16_t>::max());
  86. *val = static_cast<uint16_t>(bit_val);
  87. return true;
  88. }
  89. bool BitBuffer::ReadUInt32(uint32_t* val) {
  90. return ReadBits(val, sizeof(uint32_t) * 8);
  91. }
  92. bool BitBuffer::PeekBits(uint32_t* val, size_t bit_count) {
  93. // TODO(nisse): Could allow bit_count == 0 and always return success. But
  94. // current code reads one byte beyond end of buffer in the case that
  95. // RemainingBitCount() == 0 and bit_count == 0.
  96. RTC_DCHECK(bit_count > 0);
  97. if (!val || bit_count > RemainingBitCount() || bit_count > 32) {
  98. return false;
  99. }
  100. const uint8_t* bytes = bytes_ + byte_offset_;
  101. size_t remaining_bits_in_current_byte = 8 - bit_offset_;
  102. uint32_t bits = LowestBits(*bytes++, remaining_bits_in_current_byte);
  103. // If we're reading fewer bits than what's left in the current byte, just
  104. // return the portion of this byte that we need.
  105. if (bit_count < remaining_bits_in_current_byte) {
  106. *val = HighestBits(bits, bit_offset_ + bit_count);
  107. return true;
  108. }
  109. // Otherwise, subtract what we've read from the bit count and read as many
  110. // full bytes as we can into bits.
  111. bit_count -= remaining_bits_in_current_byte;
  112. while (bit_count >= 8) {
  113. bits = (bits << 8) | *bytes++;
  114. bit_count -= 8;
  115. }
  116. // Whatever we have left is smaller than a byte, so grab just the bits we need
  117. // and shift them into the lowest bits.
  118. if (bit_count > 0) {
  119. bits <<= bit_count;
  120. bits |= HighestBits(*bytes, bit_count);
  121. }
  122. *val = bits;
  123. return true;
  124. }
  125. bool BitBuffer::ReadBits(uint32_t* val, size_t bit_count) {
  126. return PeekBits(val, bit_count) && ConsumeBits(bit_count);
  127. }
  128. bool BitBuffer::ConsumeBytes(size_t byte_count) {
  129. return ConsumeBits(byte_count * 8);
  130. }
  131. bool BitBuffer::ConsumeBits(size_t bit_count) {
  132. if (bit_count > RemainingBitCount()) {
  133. return false;
  134. }
  135. byte_offset_ += (bit_offset_ + bit_count) / 8;
  136. bit_offset_ = (bit_offset_ + bit_count) % 8;
  137. return true;
  138. }
  139. bool BitBuffer::ReadNonSymmetric(uint32_t* val, uint32_t num_values) {
  140. RTC_DCHECK_GT(num_values, 0);
  141. RTC_DCHECK_LE(num_values, uint32_t{1} << 31);
  142. size_t count_bits = CountBits(num_values);
  143. uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values;
  144. if (!ReadBits(val, count_bits - 1)) {
  145. return false;
  146. }
  147. if (*val < num_min_bits_values) {
  148. return true;
  149. }
  150. uint32_t extra_bit;
  151. if (!ReadBits(&extra_bit, /*bit_count=*/1)) {
  152. return false;
  153. }
  154. *val = (*val << 1) + extra_bit - num_min_bits_values;
  155. return true;
  156. }
  157. bool BitBuffer::ReadExponentialGolomb(uint32_t* val) {
  158. if (!val) {
  159. return false;
  160. }
  161. // Store off the current byte/bit offset, in case we want to restore them due
  162. // to a failed parse.
  163. size_t original_byte_offset = byte_offset_;
  164. size_t original_bit_offset = bit_offset_;
  165. // Count the number of leading 0 bits by peeking/consuming them one at a time.
  166. size_t zero_bit_count = 0;
  167. uint32_t peeked_bit;
  168. while (PeekBits(&peeked_bit, 1) && peeked_bit == 0) {
  169. zero_bit_count++;
  170. ConsumeBits(1);
  171. }
  172. // We should either be at the end of the stream, or the next bit should be 1.
  173. RTC_DCHECK(!PeekBits(&peeked_bit, 1) || peeked_bit == 1);
  174. // The bit count of the value is the number of zeros + 1. Make sure that many
  175. // bits fits in a uint32_t and that we have enough bits left for it, and then
  176. // read the value.
  177. size_t value_bit_count = zero_bit_count + 1;
  178. if (value_bit_count > 32 || !ReadBits(val, value_bit_count)) {
  179. RTC_CHECK(Seek(original_byte_offset, original_bit_offset));
  180. return false;
  181. }
  182. *val -= 1;
  183. return true;
  184. }
  185. bool BitBuffer::ReadSignedExponentialGolomb(int32_t* val) {
  186. uint32_t unsigned_val;
  187. if (!ReadExponentialGolomb(&unsigned_val)) {
  188. return false;
  189. }
  190. if ((unsigned_val & 1) == 0) {
  191. *val = -static_cast<int32_t>(unsigned_val / 2);
  192. } else {
  193. *val = (unsigned_val + 1) / 2;
  194. }
  195. return true;
  196. }
  197. void BitBuffer::GetCurrentOffset(size_t* out_byte_offset,
  198. size_t* out_bit_offset) {
  199. RTC_CHECK(out_byte_offset != nullptr);
  200. RTC_CHECK(out_bit_offset != nullptr);
  201. *out_byte_offset = byte_offset_;
  202. *out_bit_offset = bit_offset_;
  203. }
  204. bool BitBuffer::Seek(size_t byte_offset, size_t bit_offset) {
  205. if (byte_offset > byte_count_ || bit_offset > 7 ||
  206. (byte_offset == byte_count_ && bit_offset > 0)) {
  207. return false;
  208. }
  209. byte_offset_ = byte_offset;
  210. bit_offset_ = bit_offset;
  211. return true;
  212. }
  213. BitBufferWriter::BitBufferWriter(uint8_t* bytes, size_t byte_count)
  214. : BitBuffer(bytes, byte_count), writable_bytes_(bytes) {}
  215. bool BitBufferWriter::WriteUInt8(uint8_t val) {
  216. return WriteBits(val, sizeof(uint8_t) * 8);
  217. }
  218. bool BitBufferWriter::WriteUInt16(uint16_t val) {
  219. return WriteBits(val, sizeof(uint16_t) * 8);
  220. }
  221. bool BitBufferWriter::WriteUInt32(uint32_t val) {
  222. return WriteBits(val, sizeof(uint32_t) * 8);
  223. }
  224. bool BitBufferWriter::WriteBits(uint64_t val, size_t bit_count) {
  225. if (bit_count > RemainingBitCount()) {
  226. return false;
  227. }
  228. size_t total_bits = bit_count;
  229. // For simplicity, push the bits we want to read from val to the highest bits.
  230. val <<= (sizeof(uint64_t) * 8 - bit_count);
  231. uint8_t* bytes = writable_bytes_ + byte_offset_;
  232. // The first byte is relatively special; the bit offset to write to may put us
  233. // in the middle of the byte, and the total bit count to write may require we
  234. // save the bits at the end of the byte.
  235. size_t remaining_bits_in_current_byte = 8 - bit_offset_;
  236. size_t bits_in_first_byte =
  237. std::min(bit_count, remaining_bits_in_current_byte);
  238. *bytes = WritePartialByte(HighestByte(val), bits_in_first_byte, *bytes,
  239. bit_offset_);
  240. if (bit_count <= remaining_bits_in_current_byte) {
  241. // Nothing left to write, so quit early.
  242. return ConsumeBits(total_bits);
  243. }
  244. // Subtract what we've written from the bit count, shift it off the value, and
  245. // write the remaining full bytes.
  246. val <<= bits_in_first_byte;
  247. bytes++;
  248. bit_count -= bits_in_first_byte;
  249. while (bit_count >= 8) {
  250. *bytes++ = HighestByte(val);
  251. val <<= 8;
  252. bit_count -= 8;
  253. }
  254. // Last byte may also be partial, so write the remaining bits from the top of
  255. // val.
  256. if (bit_count > 0) {
  257. *bytes = WritePartialByte(HighestByte(val), bit_count, *bytes, 0);
  258. }
  259. // All done! Consume the bits we've written.
  260. return ConsumeBits(total_bits);
  261. }
  262. bool BitBufferWriter::WriteNonSymmetric(uint32_t val, uint32_t num_values) {
  263. RTC_DCHECK_LT(val, num_values);
  264. RTC_DCHECK_LE(num_values, uint32_t{1} << 31);
  265. size_t count_bits = CountBits(num_values);
  266. uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values;
  267. return val < num_min_bits_values
  268. ? WriteBits(val, count_bits - 1)
  269. : WriteBits(val + num_min_bits_values, count_bits);
  270. }
  271. size_t BitBufferWriter::SizeNonSymmetricBits(uint32_t val,
  272. uint32_t num_values) {
  273. RTC_DCHECK_LT(val, num_values);
  274. RTC_DCHECK_LE(num_values, uint32_t{1} << 31);
  275. size_t count_bits = CountBits(num_values);
  276. uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values;
  277. return val < num_min_bits_values ? (count_bits - 1) : count_bits;
  278. }
  279. bool BitBufferWriter::WriteExponentialGolomb(uint32_t val) {
  280. // We don't support reading UINT32_MAX, because it doesn't fit in a uint32_t
  281. // when encoded, so don't support writing it either.
  282. if (val == std::numeric_limits<uint32_t>::max()) {
  283. return false;
  284. }
  285. uint64_t val_to_encode = static_cast<uint64_t>(val) + 1;
  286. // We need to write CountBits(val+1) 0s and then val+1. Since val (as a
  287. // uint64_t) has leading zeros, we can just write the total golomb encoded
  288. // size worth of bits, knowing the value will appear last.
  289. return WriteBits(val_to_encode, CountBits(val_to_encode) * 2 - 1);
  290. }
  291. bool BitBufferWriter::WriteSignedExponentialGolomb(int32_t val) {
  292. if (val == 0) {
  293. return WriteExponentialGolomb(0);
  294. } else if (val > 0) {
  295. uint32_t signed_val = val;
  296. return WriteExponentialGolomb((signed_val * 2) - 1);
  297. } else {
  298. if (val == std::numeric_limits<int32_t>::min())
  299. return false; // Not supported, would cause overflow.
  300. uint32_t signed_val = -val;
  301. return WriteExponentialGolomb(signed_val * 2);
  302. }
  303. }
  304. } // namespace rtc