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