File: bit_reader.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (196 lines) | stat: -rw-r--r-- 5,059 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/bit_reader.h"

#include "base/bits.h"
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/numerics/byte_conversions.h"
#include "base/numerics/safe_conversions.h"

namespace media {

namespace {
constexpr size_t kBitsPerByte = 8u;
constexpr size_t kRegWidthInBits = sizeof(uint64_t) * kBitsPerByte;
}  // namespace

BitReader::BitReader(base::span<const uint8_t> data)
    : initial_size_(data.size()), data_(data) {}

BitReader::BitReader(const uint8_t* data, int size)
    : BitReader(
          // TODO(crbug.com/40284755): Remove this.
          UNSAFE_TODO(base::span(data, base::checked_cast<size_t>(size)))) {
  DCHECK(data != nullptr);
  DCHECK_GE(size, 0);
}

BitReader::~BitReader() = default;

bool BitReader::ReadFlag(bool* flag) {
  if (nbits_ == 0 && !Refill(1)) {
    return false;
  }

  *flag = (reg_ & base::bits::LeftmostBit<uint64_t>()) != 0;
  reg_ <<= 1;
  nbits_--;
  bits_read_++;
  return true;
}

bool BitReader::ReadString(size_t num_bits, std::string* str) {
  DCHECK_EQ(num_bits % kBitsPerByte, 0u);
  DCHECK(str);
  const int num_bytes = num_bits / kBitsPerByte;
  str->resize(num_bytes);
  return ReadSpan(base::as_writable_byte_span(*str));
}

bool BitReader::ReadSpan(base::span<uint8_t> out) {
  for (uint8_t& c : out) {
    if (!ReadBits(kBitsPerByte, &c)) {
      return false;
    }
  }
  return true;
}

bool BitReader::SkipBitsSmall(size_t num_bits) {
  uint64_t dummy;
  while (num_bits >= kRegWidthInBits) {
    if (!ReadBitsInternal(kRegWidthInBits, &dummy)) {
      return false;
    }
    num_bits -= kRegWidthInBits;
  }
  return ReadBitsInternal(num_bits, &dummy);
}

bool BitReader::SkipBits(size_t num_bits) {
  const size_t remaining_bits = nbits_ + nbits_next_;
  if (remaining_bits >= num_bits) {
    return SkipBitsSmall(num_bits);
  }

  // Skip first the remaining available bits.
  num_bits -= remaining_bits;
  bits_read_ += remaining_bits;
  nbits_ = 0;
  reg_ = 0;
  nbits_next_ = 0;
  reg_next_ = 0;

  // Next, skip an integer number of bytes.
  if (num_bits > 0) {
    const size_t nbytes = num_bits / kBitsPerByte;
    base::span<const uint8_t> byte_stream_window = GetBytes(nbytes);
    if (byte_stream_window.size() < nbytes) {
      // Note that some bytes were consumed.
      bits_read_ += kBitsPerByte * byte_stream_window.size();
      return false;
    }
    num_bits -= kBitsPerByte * nbytes;
    bits_read_ += kBitsPerByte * nbytes;
  }

  // Skip the remaining bits.
  return SkipBitsSmall(num_bits);
}

bool BitReader::ReadBitsInternal(size_t num_bits, uint64_t* out) {
  if (num_bits == 0) {
    *out = 0;
    return true;
  }

  if (num_bits > nbits_ && !Refill(num_bits)) {
    // Any subsequent ReadBits should fail:
    // empty the current bit register for that purpose.
    nbits_ = 0;
    reg_ = 0;
    *out = 0;
    return false;
  }

  bits_read_ += num_bits;

  if (num_bits == kRegWidthInBits) {
    // Special case needed since for example for a 64 bit integer "a"
    // "a << 64" is not defined by the C/C++ standard.
    *out = reg_;
    reg_ = 0;
    nbits_ = 0;
    return true;
  }

  *out = reg_ >> (kRegWidthInBits - num_bits);
  reg_ <<= num_bits;
  nbits_ -= num_bits;
  return true;
}

bool BitReader::Refill(size_t min_nbits) {
  DCHECK_LE(min_nbits, kRegWidthInBits);

  // Transfer from the next to the current register.
  RefillCurrentRegister();
  if (min_nbits <= nbits_) {
    return true;
  }
  DCHECK_EQ(nbits_next_, 0u);
  DCHECK_EQ(reg_next_, 0u);

  // Max number of bytes to refill.
  static constexpr size_t kRegNextByteSize = sizeof(reg_next_);

  // Refill.
  auto byte_stream_window = GetBytes(kRegNextByteSize);
  if (byte_stream_window.empty()) {
    return false;
  }

  // Pad the window to 8 big-endian bytes to fill `reg_next_`. `reg_next_` is
  // read from the MSB, so the new bytes are written to the front in big-endian.
  std::array<uint8_t, kRegNextByteSize> bytes = {};
  base::span(bytes).copy_prefix_from(byte_stream_window);
  reg_next_ = base::U64FromBigEndian(bytes);
  nbits_next_ = byte_stream_window.size() * kBitsPerByte;

  // Transfer from the next to the current register.
  RefillCurrentRegister();

  return (nbits_ >= min_nbits);
}

void BitReader::RefillCurrentRegister() {
  // No refill possible if the destination register is full
  // or the source register is empty.
  if (nbits_ == kRegWidthInBits || nbits_next_ == 0) {
    return;
  }

  reg_ |= (reg_next_ >> nbits_);

  const size_t free_nbits = kRegWidthInBits - nbits_;
  if (free_nbits >= nbits_next_) {
    nbits_ += nbits_next_;
    reg_next_ = 0;
    nbits_next_ = 0;
    return;
  }

  nbits_ += free_nbits;
  reg_next_ <<= free_nbits;
  nbits_next_ -= free_nbits;
}

base::span<const uint8_t> BitReader::GetBytes(size_t max_nbytes) {
  return data_.take_first(std::min(max_nbytes, data_.size()));
}

}  // namespace media