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 <stdint.h>
9 #include <sys/types.h>
10 
11 #include "packager/base/logging.h"
12 
13 namespace shaka {
14 namespace media {
15 
17 class BitReader {
18  public:
22  BitReader(const uint8_t* data, size_t size);
23  ~BitReader();
24 
34  template <typename T>
35  bool ReadBits(size_t num_bits, T* out) {
36  DCHECK_LE(num_bits, sizeof(T) * 8);
37  uint64_t temp;
38  bool ret = ReadBitsInternal(num_bits, &temp);
39  *out = static_cast<T>(temp);
40  return ret;
41  }
42 
43  // Explicit T=bool overload to make MSVC happy.
44  bool ReadBits(size_t num_bits, bool* out) {
45  DCHECK_EQ(num_bits, 1u);
46  uint64_t temp;
47  bool ret = ReadBitsInternal(num_bits, &temp);
48  *out = temp != 0;
49  return ret;
50  }
51 
58  bool SkipBits(size_t num_bits);
59 
69  bool SkipBitsConditional(bool condition, size_t num_bits) {
70  bool condition_read = true;
71  if (!ReadBits(1, &condition_read))
72  return false;
73  return condition_read == condition ? SkipBits(num_bits) : true;
74  }
75 
78  void SkipToNextByte();
79 
86  bool SkipBytes(size_t num_bytes);
87 
89  size_t bits_available() const {
90  return 8 * bytes_left_ + num_remaining_bits_in_curr_byte_;
91  }
92 
94  size_t bit_position() const { return 8 * initial_size_ - bits_available(); }
95 
97  const uint8_t* current_byte_ptr() const { return data_ - 1; }
98 
99  private:
100  // Help function used by ReadBits to avoid inlining the bit reading logic.
101  bool ReadBitsInternal(size_t num_bits, uint64_t* out);
102 
103  // Advance to the next byte, loading it into curr_byte_.
104  // If the num_remaining_bits_in_curr_byte_ is 0 after this function returns,
105  // the stream has reached the end.
106  void UpdateCurrByte();
107 
108  // Pointer to the next unread (not in curr_byte_) byte in the stream.
109  const uint8_t* data_;
110 
111  // Initial size of the input data.
112  size_t initial_size_;
113 
114  // Bytes left in the stream (without the curr_byte_).
115  size_t bytes_left_;
116 
117  // Contents of the current byte; first unread bit starting at position
118  // 8 - num_remaining_bits_in_curr_byte_ from MSB.
119  uint8_t curr_byte_;
120 
121  // Number of bits remaining in curr_byte_
122  size_t num_remaining_bits_in_curr_byte_;
123 
124  private:
125  DISALLOW_COPY_AND_ASSIGN(BitReader);
126 };
127 
128 } // namespace media
129 } // namespace shaka
130 
131 #endif // PACKAGER_MEDIA_BASE_BIT_READER_H_
A class to read bit streams.
Definition: bit_reader.h:17
size_t bit_position() const
Definition: bit_reader.h:94
BitReader(const uint8_t *data, size_t size)
Definition: bit_reader.cc:12
bool SkipBits(size_t num_bits)
Definition: bit_reader.cc:24
bool SkipBytes(size_t num_bytes)
Definition: bit_reader.cc:63
size_t bits_available() const
Definition: bit_reader.h:89
bool SkipBitsConditional(bool condition, size_t num_bits)
Definition: bit_reader.h:69
const uint8_t * current_byte_ptr() const
Definition: bit_reader.h:97
bool ReadBits(size_t num_bits, T *out)
Definition: bit_reader.h:35
All the methods that are virtual are virtual for mocking.