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