Shaka Packager SDK
Loading...
Searching...
No Matches
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 <cstddef>
10#include <cstdint>
11#include <vector>
12
13#include <absl/log/check.h>
14
15namespace shaka {
16namespace media {
17
18BitWriter::BitWriter(std::vector<uint8_t>* storage)
19 : storage_(storage), initial_storage_size_(storage_->size()) {}
20
21void BitWriter::WriteBits(uint32_t bits, size_t number_of_bits) {
22 DCHECK_NE(number_of_bits, 0u);
23 DCHECK_LE(number_of_bits, 32u);
24 DCHECK_LT(bits, 1ULL << number_of_bits);
25
26 num_bits_ += number_of_bits;
27 DCHECK_LE(num_bits_, 64);
28 bits_ |= static_cast<uint64_t>(bits) << (64 - num_bits_);
29
30 while (num_bits_ >= 8) {
31 storage_->push_back(bits_ >> 56);
32 bits_ <<= 8;
33 num_bits_ -= 8;
34 }
35}
36
38 while (num_bits_ > 0) {
39 storage_->push_back(bits_ >> 56);
40 bits_ <<= 8;
41 num_bits_ -= 8;
42 }
43 bits_ = 0;
44 num_bits_ = 0;
45}
46
47} // namespace media
48} // namespace shaka
BitWriter(std::vector< uint8_t > *storage)
Definition bit_writer.cc:18
void Flush()
Write pending bits, and align bitstream with extra zero bits.
Definition bit_writer.cc:37
void WriteBits(uint32_t bits, size_t number_of_bits)
Definition bit_writer.cc:21
All the methods that are virtual are virtual for mocking.