// Copyright (c) the JPEG XL Project Authors. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.
struct BitWriter { // Upper bound on `n_bits` in each call to Write. We shift a 64-bit word by // 7 bits (max already valid bits in the last byte) and at least 1 bit is // needed to zero-initialize the bit-stream ahead (i.e. if 7 bits are valid // and we write 57 bits, then the next write will access a byte that was not // yet zero-initialized). static constexpr size_t kMaxBitsPerCall = 56;
// Example usage: bytes = std::move(writer).TakeBytes(); Useful for the // top-level encoder which returns PaddedBytes, not a BitWriter. // *this must be an rvalue reference and is invalid afterwards.
PaddedBytes&& TakeBytes() && { // Callers must ensure byte alignment to avoid uninitialized bits.
JXL_DASSERT(bits_written_ % kBitsPerByte == 0);
Status status = storage_.resize(DivCeil(bits_written_, kBitsPerByte));
JXL_DASSERT(status); // Can never fail, because we are resizing to a lower size.
(void)status; return std::move(storage_);
}
// Must be byte-aligned before calling.
Status AppendByteAligned(const Span<const uint8_t>& span);
// NOTE: no allotment needed, the other BitWriters have already been charged.
Status AppendByteAligned( const std::vector<std::unique_ptr<BitWriter>>& others);
Status AppendUnaligned(const BitWriter& other);
// Writes bits into bytes in increasing addresses, and within a byte // least-significant-bit first. // // The function can write up to 56 bits in one go. void Write(size_t n_bits, uint64_t bits);
// This should only rarely be used - e.g. when the current location will be // referenced via byte offset (TOCs point to groups), or byte-aligned reading // is required for speed. void ZeroPadToByte() { const size_t remainder_bits =
RoundUpBitsToByteMultiple(bits_written_) - bits_written_; if (remainder_bits == 0) return;
Write(remainder_bits, 0);
JXL_DASSERT(bits_written_ % kBitsPerByte == 0);
}
Status ReclaimAndCharge(BitWriter* JXL_RESTRICT writer, LayerType layer,
AuxOut* JXL_RESTRICT aux_out);
private: friendstruct BitWriter;
// Expands a BitWriter's storage. Must happen before calling Write or // ZeroPadToByte. Must call ReclaimUnused after writing to reclaim the // unused storage so that BitWriter memory use remains tightly bounded.
Status Init(BitWriter* JXL_RESTRICT writer);
Status PrivateReclaim(BitWriter* JXL_RESTRICT writer,
size_t* JXL_RESTRICT used_bits,
size_t* JXL_RESTRICT unused_bits);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.