2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2017 Google LLC. All rights reserved.
|
2017-10-23 05:07:14 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#ifndef PACKAGER_MEDIA_BASE_BIT_WRITER_H_
|
|
|
|
#define PACKAGER_MEDIA_BASE_BIT_WRITER_H_
|
|
|
|
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <cstdint>
|
2017-10-23 05:07:14 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <glog/logging.h>
|
2017-10-23 05:07:14 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
class BitWriter {
|
|
|
|
public:
|
|
|
|
/// Constructor a BitWriter instance which writes to the provided storage.
|
|
|
|
/// @param storage points to vector this BitWriter writes to. Cannot be
|
|
|
|
/// nullptr.
|
|
|
|
explicit BitWriter(std::vector<uint8_t>* storage);
|
|
|
|
~BitWriter() = default;
|
|
|
|
|
|
|
|
/// Appends the sequence 'bits' of length 'number_of_bits' <= 32.
|
|
|
|
/// Note that 'bits' should contain at most 'number_of_bits' bits, i.e.
|
|
|
|
/// bits should be less than 1 << number_of_bits.
|
|
|
|
/// @param bits is the data to write.
|
|
|
|
/// @param number_of_bits is the number of LSB to write, capped at 32. Cannot
|
|
|
|
/// be zero.
|
|
|
|
void WriteBits(uint32_t bits, size_t number_of_bits);
|
|
|
|
|
|
|
|
/// Write pending bits, and align bitstream with extra zero bits.
|
|
|
|
void Flush();
|
|
|
|
|
|
|
|
/// @return last written position, in bits.
|
|
|
|
size_t BitPos() const { return BytePos() * 8 + num_bits_; }
|
|
|
|
|
|
|
|
/// @return last written position, in bytes.
|
|
|
|
size_t BytePos() const { return storage_->size() - initial_storage_size_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
BitWriter(const BitWriter&) = delete;
|
|
|
|
BitWriter& operator=(const BitWriter&) = delete;
|
|
|
|
|
|
|
|
// Accumulator for unwritten bits.
|
|
|
|
uint64_t bits_ = 0;
|
|
|
|
// Number of unwritten bits.
|
|
|
|
int num_bits_ = 0;
|
|
|
|
// Buffer contains the written bits.
|
|
|
|
std::vector<uint8_t>* const storage_ = nullptr;
|
|
|
|
const size_t initial_storage_size_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|
|
|
|
|
|
|
|
#endif // PACKAGER_MEDIA_BASE_BIT_WRITER_H_
|