blob: 0bb604970bed43c861669541dce433784f1e0c5a [file] [log] [blame]
Danil Chapovalov5af152c2021-08-31 15:27:51 +02001/*
2 * Copyright 2021 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
11#include "rtc_base/bitstream_reader.h"
12
13#include <stdint.h>
14
15#include "absl/numeric/bits.h"
16#include "rtc_base/checks.h"
17#include "rtc_base/numerics/safe_conversions.h"
18
19namespace webrtc {
20
21uint64_t BitstreamReader::ReadBits(int bits) {
22 RTC_DCHECK_GE(bits, 0);
23 RTC_DCHECK_LE(bits, 64);
24 set_last_read_is_verified(false);
25
26 if (remaining_bits_ < bits) {
27 remaining_bits_ -= bits;
28 return 0;
29 }
30
31 int remaining_bits_in_first_byte = remaining_bits_ % 8;
32 remaining_bits_ -= bits;
33 if (bits < remaining_bits_in_first_byte) {
34 // Reading fewer bits than what's left in the current byte, just
35 // return the portion of this byte that is needed.
36 int offset = (remaining_bits_in_first_byte - bits);
37 return ((*bytes_) >> offset) & ((1 << bits) - 1);
38 }
39
40 uint64_t result = 0;
41 if (remaining_bits_in_first_byte > 0) {
42 // Read all bits that were left in the current byte and consume that byte.
43 bits -= remaining_bits_in_first_byte;
44 uint8_t mask = (1 << remaining_bits_in_first_byte) - 1;
45 result = static_cast<uint64_t>(*bytes_ & mask) << bits;
46 ++bytes_;
47 }
48
49 // Read as many full bytes as we can.
50 while (bits >= 8) {
51 bits -= 8;
52 result |= uint64_t{*bytes_} << bits;
53 ++bytes_;
54 }
55 // Whatever is left to read is smaller than a byte, so grab just the needed
56 // bits and shift them into the lowest bits.
57 if (bits > 0) {
58 result |= (*bytes_ >> (8 - bits));
59 }
60 return result;
61}
62
63int BitstreamReader::ReadBit() {
64 set_last_read_is_verified(false);
65 --remaining_bits_;
66 if (remaining_bits_ < 0) {
67 return 0;
68 }
69
70 int bit_position = remaining_bits_ % 8;
71 if (bit_position == 0) {
72 // Read the last bit from current byte and move to the next byte.
73 return (*bytes_++) & 0x01;
74 }
75
76 return (*bytes_ >> bit_position) & 0x01;
77}
78
79void BitstreamReader::ConsumeBits(int bits) {
80 RTC_DCHECK_GE(bits, 0);
81 set_last_read_is_verified(false);
82
83 int remaining_bytes = (remaining_bits_ + 7) / 8;
84 remaining_bits_ -= bits;
85 int new_remaining_bytes = (remaining_bits_ + 7) / 8;
86 // When `remaining_bits_` is negative, `BitstreamReader` is in failure state
87 // and `bytes_' member no longer used, thus its value doesn't matter.
88 // In such case it doesn't matter that negative integer division rounds up
89 // instead of down and thus this byte adjustement might seem incorrect.
90 bytes_ += (remaining_bytes - new_remaining_bytes);
91}
92
93uint32_t BitstreamReader::ReadNonSymmetric(uint32_t num_values) {
94 RTC_DCHECK_GT(num_values, 0);
95 RTC_DCHECK_LE(num_values, uint32_t{1} << 31);
96
97 int width = absl::bit_width(num_values);
98 uint32_t num_min_bits_values = (uint32_t{1} << width) - num_values;
99
100 uint64_t val = ReadBits(width - 1);
101 if (val < num_min_bits_values) {
102 return val;
103 }
104 return (val << 1) + ReadBit() - num_min_bits_values;
105}
106
107uint32_t BitstreamReader::ReadExponentialGolomb() {
108 // Count the number of leading 0.
109 int zero_bit_count = 0;
110 while (ReadBit() == 0) {
111 if (++zero_bit_count >= 32) {
112 // Golob value won't fit into 32 bits of the return value. Fail the parse.
113 Invalidate();
114 return 0;
115 }
116 }
117
118 // The bit count of the value is the number of zeros + 1.
119 // However the first '1' was already read above.
120 return (uint32_t{1} << zero_bit_count) +
121 rtc::dchecked_cast<uint32_t>(ReadBits(zero_bit_count)) - 1;
122}
123
124int BitstreamReader::ReadSignedExponentialGolomb() {
125 uint32_t unsigned_val = ReadExponentialGolomb();
126 if ((unsigned_val & 1) == 0) {
127 return -static_cast<int>(unsigned_val / 2);
128 } else {
129 return (unsigned_val + 1) / 2;
130 }
131}
132
133} // namespace webrtc