Shaka Packager SDK
bit_writer.h
1 // Copyright 2017 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef PACKAGER_MEDIA_BASE_BIT_WRITER_H_
8 #define PACKAGER_MEDIA_BASE_BIT_WRITER_H_
9 
10 #include <cstdint>
11 #include <vector>
12 
13 #include <absl/log/log.h>
14 
15 namespace shaka {
16 namespace media {
17 
18 class BitWriter {
19  public:
23  explicit BitWriter(std::vector<uint8_t>* storage);
24  ~BitWriter() = default;
25 
32  void WriteBits(uint32_t bits, size_t number_of_bits);
33 
35  void Flush();
36 
38  size_t BitPos() const { return BytePos() * 8 + num_bits_; }
39 
41  size_t BytePos() const { return storage_->size() - initial_storage_size_; }
42 
43  private:
44  BitWriter(const BitWriter&) = delete;
45  BitWriter& operator=(const BitWriter&) = delete;
46 
47  // Accumulator for unwritten bits.
48  uint64_t bits_ = 0;
49  // Number of unwritten bits.
50  int num_bits_ = 0;
51  // Buffer contains the written bits.
52  std::vector<uint8_t>* const storage_ = nullptr;
53  const size_t initial_storage_size_ = 0;
54 };
55 
56 } // namespace media
57 } // namespace shaka
58 
59 #endif // PACKAGER_MEDIA_BASE_BIT_WRITER_H_
BitWriter(std::vector< uint8_t > *storage)
Definition: bit_writer.cc:14
void Flush()
Write pending bits, and align bitstream with extra zero bits.
Definition: bit_writer.cc:33
size_t BitPos() const
Definition: bit_writer.h:38
size_t BytePos() const
Definition: bit_writer.h:41
void WriteBits(uint32_t bits, size_t number_of_bits)
Definition: bit_writer.cc:17
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66