Shaka Packager SDK
bit_reader.h
1 // Copyright (c) 2012 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 #ifndef PACKAGER_MEDIA_BASE_BIT_READER_H_
6 #define PACKAGER_MEDIA_BASE_BIT_READER_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
13 
14 #include <packager/macros/classes.h>
15 
16 namespace shaka {
17 namespace media {
18 
20 class BitReader {
21  public:
25  BitReader(const uint8_t* data, size_t size);
26  ~BitReader();
27 
37  template <typename T>
38  bool ReadBits(size_t num_bits, T* out) {
39  DCHECK_LE(num_bits, sizeof(T) * 8);
40  uint64_t temp;
41  bool ret = ReadBitsInternal(num_bits, &temp);
42  *out = static_cast<T>(temp);
43  return ret;
44  }
45 
46  // Explicit T=bool overload to make MSVC happy.
47  bool ReadBits(size_t num_bits, bool* out) {
48  DCHECK_EQ(num_bits, 1u);
49  uint64_t temp;
50  bool ret = ReadBitsInternal(num_bits, &temp);
51  *out = temp != 0;
52  return ret;
53  }
54 
61  bool SkipBits(size_t num_bits);
62 
72  bool SkipBitsConditional(bool condition, size_t num_bits) {
73  bool condition_read = true;
74  if (!ReadBits(1, &condition_read))
75  return false;
76  return condition_read == condition ? SkipBits(num_bits) : true;
77  }
78 
81  void SkipToNextByte();
82 
89  bool SkipBytes(size_t num_bytes);
90 
92  size_t bits_available() const {
93  return 8 * bytes_left_ + num_remaining_bits_in_curr_byte_;
94  }
95 
97  size_t bit_position() const { return 8 * initial_size_ - bits_available(); }
98 
100  const uint8_t* current_byte_ptr() const { return data_ - 1; }
101 
102  private:
103  // Help function used by ReadBits to avoid inlining the bit reading logic.
104  bool ReadBitsInternal(size_t num_bits, uint64_t* out);
105 
106  // Advance to the next byte, loading it into curr_byte_.
107  // If the num_remaining_bits_in_curr_byte_ is 0 after this function returns,
108  // the stream has reached the end.
109  void UpdateCurrByte();
110 
111  // Pointer to the next unread (not in curr_byte_) byte in the stream.
112  const uint8_t* data_;
113 
114  // Initial size of the input data.
115  size_t initial_size_;
116 
117  // Bytes left in the stream (without the curr_byte_).
118  size_t bytes_left_;
119 
120  // Contents of the current byte; first unread bit starting at position
121  // 8 - num_remaining_bits_in_curr_byte_ from MSB.
122  uint8_t curr_byte_;
123 
124  // Number of bits remaining in curr_byte_
125  size_t num_remaining_bits_in_curr_byte_;
126 
127  private:
128  DISALLOW_COPY_AND_ASSIGN(BitReader);
129 };
130 
131 } // namespace media
132 } // namespace shaka
133 
134 #endif // PACKAGER_MEDIA_BASE_BIT_READER_H_
A class to read bit streams.
Definition: bit_reader.h:20
size_t bit_position() const
Definition: bit_reader.h:97
BitReader(const uint8_t *data, size_t size)
Definition: bit_reader.cc:14
bool SkipBits(size_t num_bits)
Definition: bit_reader.cc:26
bool SkipBytes(size_t num_bytes)
Definition: bit_reader.cc:65
size_t bits_available() const
Definition: bit_reader.h:92
bool SkipBitsConditional(bool condition, size_t num_bits)
Definition: bit_reader.h:72
const uint8_t * current_byte_ptr() const
Definition: bit_reader.h:100
bool ReadBits(size_t num_bits, T *out)
Definition: bit_reader.h:38
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66