Shaka Packager SDK
bit_writer.h
1 // Copyright 2017 Google Inc. 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 <stdint.h>
11 
12 #include <vector>
13 
14 #include "packager/base/logging.h"
15 
16 namespace shaka {
17 namespace media {
18 
19 class BitWriter {
20  public:
24  explicit BitWriter(std::vector<uint8_t>* storage);
25  ~BitWriter() = default;
26 
33  void WriteBits(uint32_t bits, size_t number_of_bits);
34 
36  void Flush();
37 
39  size_t BitPos() const { return BytePos() * 8 + num_bits_; }
40 
42  size_t BytePos() const { return storage_->size() - initial_storage_size_; }
43 
44  private:
45  BitWriter(const BitWriter&) = delete;
46  BitWriter& operator=(const BitWriter&) = delete;
47 
48  // Accumulator for unwritten bits.
49  uint64_t bits_ = 0;
50  // Number of unwritten bits.
51  int num_bits_ = 0;
52  // Buffer contains the written bits.
53  std::vector<uint8_t>* const storage_ = nullptr;
54  const size_t initial_storage_size_ = 0;
55 };
56 
57 } // namespace media
58 } // namespace shaka
59 
60 #endif // PACKAGER_MEDIA_BASE_BIT_WRITER_H_
BitWriter(std::vector< uint8_t > *storage)
Definition: bit_writer.cc:12
void Flush()
Write pending bits, and align bitstream with extra zero bits.
Definition: bit_writer.cc:31
size_t BitPos() const
Definition: bit_writer.h:39
size_t BytePos() const
Definition: bit_writer.h:42
void WriteBits(uint32_t bits, size_t number_of_bits)
Definition: bit_writer.cc:15
All the methods that are virtual are virtual for mocking.