Shaka Packager SDK
bit_writer.cc
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 #include <packager/media/base/bit_writer.h>
8 
9 #include <absl/log/check.h>
10 
11 namespace shaka {
12 namespace media {
13 
14 BitWriter::BitWriter(std::vector<uint8_t>* storage)
15  : storage_(storage), initial_storage_size_(storage_->size()) {}
16 
17 void BitWriter::WriteBits(uint32_t bits, size_t number_of_bits) {
18  DCHECK_NE(number_of_bits, 0u);
19  DCHECK_LE(number_of_bits, 32u);
20  DCHECK_LT(bits, 1ULL << number_of_bits);
21 
22  num_bits_ += number_of_bits;
23  DCHECK_LE(num_bits_, 64);
24  bits_ |= static_cast<uint64_t>(bits) << (64 - num_bits_);
25 
26  while (num_bits_ >= 8) {
27  storage_->push_back(bits_ >> 56);
28  bits_ <<= 8;
29  num_bits_ -= 8;
30  }
31 }
32 
34  while (num_bits_ > 0) {
35  storage_->push_back(bits_ >> 56);
36  bits_ <<= 8;
37  num_bits_ -= 8;
38  }
39  bits_ = 0;
40  num_bits_ = 0;
41 }
42 
43 } // namespace media
44 } // namespace shaka
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
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