mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:07:45 +00:00
AK: Move bit streams from LibCore
This commit is contained in:
parent
94f139c111
commit
2470dd3bb5
14 changed files with 167 additions and 158 deletions
|
@ -8,17 +8,15 @@
|
|||
|
||||
#include "FlacTypes.h"
|
||||
#include "Loader.h"
|
||||
#include <AK/BitStream.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Span.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibCore/BitStream.h>
|
||||
#include <LibCore/MemoryStream.h>
|
||||
#include <LibCore/Stream.h>
|
||||
|
||||
namespace Audio {
|
||||
|
||||
using Core::Stream::BigEndianInputBitStream;
|
||||
|
||||
// Experimentally determined to be a decent buffer size on i686:
|
||||
// 4K (the default) is slightly worse, and 64K is much worse.
|
||||
// At sufficiently large buffer sizes, the advantage of infrequent read() calls is outweighed by the memmove() overhead.
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Array.h>
|
||||
#include <AK/BitStream.h>
|
||||
#include <AK/Span.h>
|
||||
|
||||
namespace Audio::MP3::Tables::Huffman {
|
||||
|
@ -105,7 +106,7 @@ struct HuffmanDecodeResult {
|
|||
};
|
||||
|
||||
template<typename T>
|
||||
HuffmanDecodeResult<T> huffman_decode(Core::Stream::BigEndianInputBitStream& bitstream, Span<HuffmanNode<T> const> tree, size_t max_bits_to_read)
|
||||
HuffmanDecodeResult<T> huffman_decode(BigEndianInputBitStream& bitstream, Span<HuffmanNode<T> const> tree, size_t max_bits_to_read)
|
||||
{
|
||||
HuffmanNode<T> const* node = &tree[0];
|
||||
size_t bits_read = 0;
|
||||
|
|
|
@ -41,7 +41,7 @@ Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(Byte
|
|||
|
||||
MaybeLoaderError MP3LoaderPlugin::initialize()
|
||||
{
|
||||
m_bitstream = LOADER_TRY(Core::Stream::BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*m_stream)));
|
||||
m_bitstream = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*m_stream)));
|
||||
|
||||
TRY(synchronize());
|
||||
|
||||
|
@ -242,7 +242,7 @@ ErrorOr<MP3::MP3Frame, LoaderError> MP3LoaderPlugin::read_frame_data(MP3::Header
|
|||
|
||||
TRY(m_bit_reservoir.discard(old_reservoir_size - frame.main_data_begin));
|
||||
|
||||
auto reservoir_stream = TRY(Core::Stream::BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(m_bit_reservoir)));
|
||||
auto reservoir_stream = TRY(BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(m_bit_reservoir)));
|
||||
|
||||
for (size_t granule_index = 0; granule_index < 2; granule_index++) {
|
||||
for (size_t channel_index = 0; channel_index < header.channel_count(); channel_index++) {
|
||||
|
@ -418,7 +418,7 @@ Array<float, 576> MP3LoaderPlugin::calculate_frame_exponents(MP3::MP3Frame const
|
|||
return exponents;
|
||||
}
|
||||
|
||||
ErrorOr<size_t, LoaderError> MP3LoaderPlugin::read_scale_factors(MP3::MP3Frame& frame, Core::Stream::BigEndianInputBitStream& reservoir, size_t granule_index, size_t channel_index)
|
||||
ErrorOr<size_t, LoaderError> MP3LoaderPlugin::read_scale_factors(MP3::MP3Frame& frame, BigEndianInputBitStream& reservoir, size_t granule_index, size_t channel_index)
|
||||
{
|
||||
auto& channel = frame.channels[channel_index];
|
||||
auto const& granule = channel.granules[granule_index];
|
||||
|
@ -486,7 +486,7 @@ ErrorOr<size_t, LoaderError> MP3LoaderPlugin::read_scale_factors(MP3::MP3Frame&
|
|||
return bits_read;
|
||||
}
|
||||
|
||||
MaybeLoaderError MP3LoaderPlugin::read_huffman_data(MP3::MP3Frame& frame, Core::Stream::BigEndianInputBitStream& reservoir, size_t granule_index, size_t channel_index, size_t granule_bits_read)
|
||||
MaybeLoaderError MP3LoaderPlugin::read_huffman_data(MP3::MP3Frame& frame, BigEndianInputBitStream& reservoir, size_t granule_index, size_t channel_index, size_t granule_bits_read)
|
||||
{
|
||||
auto const exponents = calculate_frame_exponents(frame, granule_index, channel_index);
|
||||
auto& granule = frame.channels[channel_index].granules[granule_index];
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
|
||||
#include "Loader.h"
|
||||
#include "MP3Types.h"
|
||||
#include <AK/BitStream.h>
|
||||
#include <AK/Tuple.h>
|
||||
#include <LibCore/BitStream.h>
|
||||
#include <LibCore/MemoryStream.h>
|
||||
#include <LibCore/Stream.h>
|
||||
#include <LibDSP/MDCT.h>
|
||||
|
@ -48,8 +48,8 @@ private:
|
|||
ErrorOr<MP3::MP3Frame, LoaderError> read_next_frame();
|
||||
ErrorOr<MP3::MP3Frame, LoaderError> read_frame_data(MP3::Header const&);
|
||||
MaybeLoaderError read_side_information(MP3::MP3Frame&);
|
||||
ErrorOr<size_t, LoaderError> read_scale_factors(MP3::MP3Frame&, Core::Stream::BigEndianInputBitStream& reservoir, size_t granule_index, size_t channel_index);
|
||||
MaybeLoaderError read_huffman_data(MP3::MP3Frame&, Core::Stream::BigEndianInputBitStream& reservoir, size_t granule_index, size_t channel_index, size_t granule_bits_read);
|
||||
ErrorOr<size_t, LoaderError> read_scale_factors(MP3::MP3Frame&, BigEndianInputBitStream& reservoir, size_t granule_index, size_t channel_index);
|
||||
MaybeLoaderError read_huffman_data(MP3::MP3Frame&, BigEndianInputBitStream& reservoir, size_t granule_index, size_t channel_index, size_t granule_bits_read);
|
||||
static AK::Array<float, 576> calculate_frame_exponents(MP3::MP3Frame const&, size_t granule_index, size_t channel_index);
|
||||
static void reorder_samples(MP3::Granule&, u32 sample_rate);
|
||||
static void reduce_alias(MP3::Granule&, size_t max_subband_index = 576);
|
||||
|
@ -72,7 +72,7 @@ private:
|
|||
|
||||
AK::Optional<MP3::MP3Frame> m_current_frame;
|
||||
u32 m_current_frame_read;
|
||||
OwnPtr<Core::Stream::BigEndianInputBitStream> m_bitstream;
|
||||
OwnPtr<BigEndianInputBitStream> m_bitstream;
|
||||
Core::Stream::AllocatingMemoryStream m_bit_reservoir;
|
||||
};
|
||||
|
||||
|
|
|
@ -6,15 +6,15 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/BitStream.h>
|
||||
#include <AK/CircularQueue.h>
|
||||
#include <AK/FixedArray.h>
|
||||
#include <LibCore/BitStream.h>
|
||||
#include <LibCore/Stream.h>
|
||||
|
||||
namespace Compress {
|
||||
|
||||
using AK::LittleEndianInputBitStream;
|
||||
using AK::Stream;
|
||||
using Core::Stream::LittleEndianInputBitStream;
|
||||
|
||||
class BrotliDecompressionStream : public Stream {
|
||||
public:
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <AK/Assertions.h>
|
||||
#include <AK/BinaryHeap.h>
|
||||
#include <AK/BinarySearch.h>
|
||||
#include <AK/BitStream.h>
|
||||
#include <LibCore/MemoryStream.h>
|
||||
#include <string.h>
|
||||
|
||||
|
@ -98,7 +99,7 @@ Optional<CanonicalCode> CanonicalCode::from_bytes(ReadonlyBytes bytes)
|
|||
return code;
|
||||
}
|
||||
|
||||
ErrorOr<u32> CanonicalCode::read_symbol(Core::Stream::LittleEndianInputBitStream& stream) const
|
||||
ErrorOr<u32> CanonicalCode::read_symbol(LittleEndianInputBitStream& stream) const
|
||||
{
|
||||
u32 code_bits = 1;
|
||||
|
||||
|
@ -115,7 +116,7 @@ ErrorOr<u32> CanonicalCode::read_symbol(Core::Stream::LittleEndianInputBitStream
|
|||
}
|
||||
}
|
||||
|
||||
ErrorOr<void> CanonicalCode::write_symbol(Core::Stream::LittleEndianOutputBitStream& stream, u32 symbol) const
|
||||
ErrorOr<void> CanonicalCode::write_symbol(LittleEndianOutputBitStream& stream, u32 symbol) const
|
||||
{
|
||||
TRY(stream.write_bits(m_bit_codes[symbol], m_bit_code_lengths[symbol]));
|
||||
return {};
|
||||
|
@ -195,7 +196,7 @@ ErrorOr<NonnullOwnPtr<DeflateDecompressor>> DeflateDecompressor::construct(Maybe
|
|||
}
|
||||
|
||||
DeflateDecompressor::DeflateDecompressor(MaybeOwned<AK::Stream> stream, CircularBuffer output_buffer)
|
||||
: m_input_stream(make<Core::Stream::LittleEndianInputBitStream>(move(stream)))
|
||||
: m_input_stream(make<LittleEndianInputBitStream>(move(stream)))
|
||||
, m_output_buffer(move(output_buffer))
|
||||
{
|
||||
}
|
||||
|
@ -448,12 +449,12 @@ ErrorOr<void> DeflateDecompressor::decode_codes(CanonicalCode& literal_code, Opt
|
|||
|
||||
ErrorOr<NonnullOwnPtr<DeflateCompressor>> DeflateCompressor::construct(MaybeOwned<AK::Stream> stream, CompressionLevel compression_level)
|
||||
{
|
||||
auto bit_stream = TRY(Core::Stream::LittleEndianOutputBitStream::construct(move(stream)));
|
||||
auto bit_stream = TRY(LittleEndianOutputBitStream::construct(move(stream)));
|
||||
auto deflate_compressor = TRY(adopt_nonnull_own_or_enomem(new (nothrow) DeflateCompressor(move(bit_stream), compression_level)));
|
||||
return deflate_compressor;
|
||||
}
|
||||
|
||||
DeflateCompressor::DeflateCompressor(NonnullOwnPtr<Core::Stream::LittleEndianOutputBitStream> stream, CompressionLevel compression_level)
|
||||
DeflateCompressor::DeflateCompressor(NonnullOwnPtr<LittleEndianOutputBitStream> stream, CompressionLevel compression_level)
|
||||
: m_compression_level(compression_level)
|
||||
, m_compression_constants(compression_constants[static_cast<int>(m_compression_level)])
|
||||
, m_output_stream(move(stream))
|
||||
|
|
|
@ -9,9 +9,10 @@
|
|||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/Endian.h>
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/MaybeOwned.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCompress/DeflateTables.h>
|
||||
#include <LibCore/BitStream.h>
|
||||
#include <LibCore/Stream.h>
|
||||
|
||||
namespace Compress {
|
||||
|
@ -19,8 +20,8 @@ namespace Compress {
|
|||
class CanonicalCode {
|
||||
public:
|
||||
CanonicalCode() = default;
|
||||
ErrorOr<u32> read_symbol(Core::Stream::LittleEndianInputBitStream&) const;
|
||||
ErrorOr<void> write_symbol(Core::Stream::LittleEndianOutputBitStream&, u32) const;
|
||||
ErrorOr<u32> read_symbol(LittleEndianInputBitStream&) const;
|
||||
ErrorOr<void> write_symbol(LittleEndianOutputBitStream&, u32) const;
|
||||
|
||||
static CanonicalCode const& fixed_literal_codes();
|
||||
static CanonicalCode const& fixed_distance_codes();
|
||||
|
@ -100,7 +101,7 @@ private:
|
|||
UncompressedBlock m_uncompressed_block;
|
||||
};
|
||||
|
||||
MaybeOwned<Core::Stream::LittleEndianInputBitStream> m_input_stream;
|
||||
MaybeOwned<LittleEndianInputBitStream> m_input_stream;
|
||||
CircularBuffer m_output_buffer;
|
||||
};
|
||||
|
||||
|
@ -152,7 +153,7 @@ public:
|
|||
static ErrorOr<ByteBuffer> compress_all(ReadonlyBytes bytes, CompressionLevel = CompressionLevel::GOOD);
|
||||
|
||||
private:
|
||||
DeflateCompressor(NonnullOwnPtr<Core::Stream::LittleEndianOutputBitStream>, CompressionLevel = CompressionLevel::GOOD);
|
||||
DeflateCompressor(NonnullOwnPtr<LittleEndianOutputBitStream>, CompressionLevel = CompressionLevel::GOOD);
|
||||
|
||||
Bytes pending_block() { return { m_rolling_window + block_size, block_size }; }
|
||||
|
||||
|
@ -184,7 +185,7 @@ private:
|
|||
bool m_finished { false };
|
||||
CompressionLevel m_compression_level;
|
||||
CompressionConstants m_compression_constants;
|
||||
NonnullOwnPtr<Core::Stream::LittleEndianOutputBitStream> m_output_stream;
|
||||
NonnullOwnPtr<LittleEndianOutputBitStream> m_output_stream;
|
||||
|
||||
u8 m_rolling_window[window_size];
|
||||
size_t m_pending_block_size { 0 };
|
||||
|
|
|
@ -1,407 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/Concepts.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/MaybeOwned.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/Span.h>
|
||||
#include <AK/StdLibExtraDetails.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibCore/Stream.h>
|
||||
|
||||
namespace Core::Stream {
|
||||
|
||||
/// A stream wrapper class that allows you to read arbitrary amounts of bits
|
||||
/// in big-endian order from another stream.
|
||||
class BigEndianInputBitStream : public AK::Stream {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<BigEndianInputBitStream>> construct(MaybeOwned<Stream> stream)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem<BigEndianInputBitStream>(new BigEndianInputBitStream(move(stream)));
|
||||
}
|
||||
|
||||
// ^Stream
|
||||
virtual ErrorOr<Bytes> read(Bytes bytes) override
|
||||
{
|
||||
if (m_current_byte.has_value() && is_aligned_to_byte_boundary()) {
|
||||
bytes[0] = m_current_byte.release_value();
|
||||
return m_stream->read(bytes.slice(1));
|
||||
}
|
||||
align_to_byte_boundary();
|
||||
return m_stream->read(bytes);
|
||||
}
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_stream->write(bytes); }
|
||||
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override { return m_stream->write_entire_buffer(bytes); }
|
||||
virtual bool is_eof() const override { return m_stream->is_eof() && !m_current_byte.has_value(); }
|
||||
virtual bool is_open() const override { return m_stream->is_open(); }
|
||||
virtual void close() override
|
||||
{
|
||||
m_stream->close();
|
||||
align_to_byte_boundary();
|
||||
}
|
||||
|
||||
ErrorOr<bool> read_bit()
|
||||
{
|
||||
return read_bits<bool>(1);
|
||||
}
|
||||
/// Depending on the number of bits to read, the return type can be chosen appropriately.
|
||||
/// This avoids a bunch of static_cast<>'s for the user.
|
||||
// TODO: Support u128, u256 etc. as well: The concepts would be quite complex.
|
||||
template<Unsigned T = u64>
|
||||
ErrorOr<T> read_bits(size_t count)
|
||||
{
|
||||
if constexpr (IsSame<bool, T>) {
|
||||
VERIFY(count == 1);
|
||||
}
|
||||
T result = 0;
|
||||
|
||||
size_t nread = 0;
|
||||
while (nread < count) {
|
||||
if (m_current_byte.has_value()) {
|
||||
if constexpr (!IsSame<bool, T> && !IsSame<u8, T>) {
|
||||
// read as many bytes as possible directly
|
||||
if (((count - nread) >= 8) && is_aligned_to_byte_boundary()) {
|
||||
// shift existing data over
|
||||
result <<= 8;
|
||||
result |= m_current_byte.value();
|
||||
nread += 8;
|
||||
m_current_byte.clear();
|
||||
} else {
|
||||
auto const bit = (m_current_byte.value() >> (7 - m_bit_offset)) & 1;
|
||||
result <<= 1;
|
||||
result |= bit;
|
||||
++nread;
|
||||
if (m_bit_offset++ == 7)
|
||||
m_current_byte.clear();
|
||||
}
|
||||
} else {
|
||||
// Always take this branch for booleans or u8: there's no purpose in reading more than a single bit
|
||||
auto const bit = (m_current_byte.value() >> (7 - m_bit_offset)) & 1;
|
||||
if constexpr (IsSame<bool, T>)
|
||||
result = bit;
|
||||
else {
|
||||
result <<= 1;
|
||||
result |= bit;
|
||||
}
|
||||
++nread;
|
||||
if (m_bit_offset++ == 7)
|
||||
m_current_byte.clear();
|
||||
}
|
||||
} else {
|
||||
auto temp_buffer = TRY(ByteBuffer::create_uninitialized(1));
|
||||
TRY(m_stream->read(temp_buffer.bytes()));
|
||||
m_current_byte = temp_buffer[0];
|
||||
m_bit_offset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Discards any sub-byte stream positioning the input stream may be keeping track of.
|
||||
/// Non-bitwise reads will implicitly call this.
|
||||
void align_to_byte_boundary()
|
||||
{
|
||||
m_current_byte.clear();
|
||||
m_bit_offset = 0;
|
||||
}
|
||||
|
||||
/// Whether we are (accidentally or intentionally) at a byte boundary right now.
|
||||
ALWAYS_INLINE bool is_aligned_to_byte_boundary() const { return m_bit_offset == 0; }
|
||||
|
||||
private:
|
||||
BigEndianInputBitStream(MaybeOwned<Stream> stream)
|
||||
: m_stream(move(stream))
|
||||
{
|
||||
}
|
||||
|
||||
Optional<u8> m_current_byte;
|
||||
size_t m_bit_offset { 0 };
|
||||
MaybeOwned<Stream> m_stream;
|
||||
};
|
||||
|
||||
/// A stream wrapper class that allows you to read arbitrary amounts of bits
|
||||
/// in little-endian order from another stream.
|
||||
class LittleEndianInputBitStream : public AK::Stream {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<LittleEndianInputBitStream>> construct(MaybeOwned<Stream> stream)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem<LittleEndianInputBitStream>(new LittleEndianInputBitStream(move(stream)));
|
||||
}
|
||||
|
||||
LittleEndianInputBitStream(MaybeOwned<Stream> stream)
|
||||
: m_stream(move(stream))
|
||||
{
|
||||
}
|
||||
|
||||
// ^Stream
|
||||
virtual ErrorOr<Bytes> read(Bytes bytes) override
|
||||
{
|
||||
if (m_current_byte.has_value() && is_aligned_to_byte_boundary()) {
|
||||
bytes[0] = m_current_byte.release_value();
|
||||
return m_stream->read(bytes.slice(1));
|
||||
}
|
||||
align_to_byte_boundary();
|
||||
return m_stream->read(bytes);
|
||||
}
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_stream->write(bytes); }
|
||||
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override { return m_stream->write_entire_buffer(bytes); }
|
||||
virtual bool is_eof() const override { return m_stream->is_eof() && !m_current_byte.has_value(); }
|
||||
virtual bool is_open() const override { return m_stream->is_open(); }
|
||||
virtual void close() override
|
||||
{
|
||||
m_stream->close();
|
||||
align_to_byte_boundary();
|
||||
}
|
||||
|
||||
ErrorOr<bool> read_bit()
|
||||
{
|
||||
return read_bits<bool>(1);
|
||||
}
|
||||
/// Depending on the number of bits to read, the return type can be chosen appropriately.
|
||||
/// This avoids a bunch of static_cast<>'s for the user.
|
||||
// TODO: Support u128, u256 etc. as well: The concepts would be quite complex.
|
||||
template<Unsigned T = u64>
|
||||
ErrorOr<T> read_bits(size_t count)
|
||||
{
|
||||
if constexpr (IsSame<bool, T>) {
|
||||
VERIFY(count == 1);
|
||||
}
|
||||
T result = 0;
|
||||
|
||||
size_t nread = 0;
|
||||
while (nread < count) {
|
||||
if (m_current_byte.has_value()) {
|
||||
if constexpr (!IsSame<bool, T> && !IsSame<u8, T>) {
|
||||
// read as many bytes as possible directly
|
||||
if (((count - nread) >= 8) && is_aligned_to_byte_boundary()) {
|
||||
// shift existing data over
|
||||
result |= (m_current_byte.value() << nread);
|
||||
nread += 8;
|
||||
m_current_byte.clear();
|
||||
} else {
|
||||
auto const bit = (m_current_byte.value() >> m_bit_offset) & 1;
|
||||
result |= (bit << nread);
|
||||
++nread;
|
||||
if (m_bit_offset++ == 7)
|
||||
m_current_byte.clear();
|
||||
}
|
||||
} else {
|
||||
// Always take this branch for booleans or u8: there's no purpose in reading more than a single bit
|
||||
auto const bit = (m_current_byte.value() >> m_bit_offset) & 1;
|
||||
if constexpr (IsSame<bool, T>)
|
||||
result = bit;
|
||||
else
|
||||
result |= (bit << nread);
|
||||
++nread;
|
||||
if (m_bit_offset++ == 7)
|
||||
m_current_byte.clear();
|
||||
}
|
||||
} else {
|
||||
auto temp_buffer = TRY(ByteBuffer::create_uninitialized(1));
|
||||
auto read_bytes = TRY(m_stream->read(temp_buffer.bytes()));
|
||||
if (read_bytes.is_empty())
|
||||
return Error::from_string_literal("eof");
|
||||
m_current_byte = temp_buffer[0];
|
||||
m_bit_offset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Discards any sub-byte stream positioning the input stream may be keeping track of.
|
||||
/// Non-bitwise reads will implicitly call this.
|
||||
u8 align_to_byte_boundary()
|
||||
{
|
||||
u8 remaining_bits = m_current_byte.value_or(0) >> m_bit_offset;
|
||||
m_current_byte.clear();
|
||||
m_bit_offset = 0;
|
||||
return remaining_bits;
|
||||
}
|
||||
|
||||
/// Whether we are (accidentally or intentionally) at a byte boundary right now.
|
||||
ALWAYS_INLINE bool is_aligned_to_byte_boundary() const { return m_bit_offset == 0; }
|
||||
|
||||
private:
|
||||
Optional<u8> m_current_byte;
|
||||
size_t m_bit_offset { 0 };
|
||||
MaybeOwned<Stream> m_stream;
|
||||
};
|
||||
|
||||
/// A stream wrapper class that allows you to write arbitrary amounts of bits
|
||||
/// in big-endian order to another stream.
|
||||
class BigEndianOutputBitStream : public AK::Stream {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<BigEndianOutputBitStream>> construct(MaybeOwned<Stream> stream)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem<BigEndianOutputBitStream>(new BigEndianOutputBitStream(move(stream)));
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes) override
|
||||
{
|
||||
return Error::from_errno(EBADF);
|
||||
}
|
||||
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override
|
||||
{
|
||||
VERIFY(m_bit_offset == 0);
|
||||
return m_stream->write(bytes);
|
||||
}
|
||||
|
||||
template<Unsigned T>
|
||||
ErrorOr<void> write_bits(T value, size_t bit_count)
|
||||
{
|
||||
VERIFY(m_bit_offset <= 7);
|
||||
|
||||
while (bit_count > 0) {
|
||||
u8 next_bit = (value >> (bit_count - 1)) & 1;
|
||||
bit_count--;
|
||||
|
||||
m_current_byte <<= 1;
|
||||
m_current_byte |= next_bit;
|
||||
m_bit_offset++;
|
||||
|
||||
if (m_bit_offset > 7) {
|
||||
TRY(m_stream->write({ &m_current_byte, sizeof(m_current_byte) }));
|
||||
m_bit_offset = 0;
|
||||
m_current_byte = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
virtual bool is_eof() const override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool is_open() const override
|
||||
{
|
||||
return m_stream->is_open();
|
||||
}
|
||||
|
||||
virtual void close() override
|
||||
{
|
||||
}
|
||||
|
||||
size_t bit_offset() const
|
||||
{
|
||||
return m_bit_offset;
|
||||
}
|
||||
|
||||
ErrorOr<void> align_to_byte_boundary()
|
||||
{
|
||||
if (m_bit_offset == 0)
|
||||
return {};
|
||||
|
||||
TRY(write_bits(0u, 8 - m_bit_offset));
|
||||
VERIFY(m_bit_offset == 0);
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
BigEndianOutputBitStream(MaybeOwned<Stream> stream)
|
||||
: m_stream(move(stream))
|
||||
{
|
||||
}
|
||||
|
||||
MaybeOwned<Stream> m_stream;
|
||||
u8 m_current_byte { 0 };
|
||||
size_t m_bit_offset { 0 };
|
||||
};
|
||||
|
||||
/// A stream wrapper class that allows you to write arbitrary amounts of bits
|
||||
/// in little-endian order to another stream.
|
||||
class LittleEndianOutputBitStream : public AK::Stream {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<LittleEndianOutputBitStream>> construct(MaybeOwned<Stream> stream)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem<LittleEndianOutputBitStream>(new LittleEndianOutputBitStream(move(stream)));
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes) override
|
||||
{
|
||||
return Error::from_errno(EBADF);
|
||||
}
|
||||
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override
|
||||
{
|
||||
VERIFY(m_bit_offset == 0);
|
||||
return m_stream->write(bytes);
|
||||
}
|
||||
|
||||
template<Unsigned T>
|
||||
ErrorOr<void> write_bits(T value, size_t bit_count)
|
||||
{
|
||||
VERIFY(m_bit_offset <= 7);
|
||||
|
||||
size_t input_offset = 0;
|
||||
while (input_offset < bit_count) {
|
||||
u8 next_bit = (value >> input_offset) & 1;
|
||||
input_offset++;
|
||||
|
||||
m_current_byte |= next_bit << m_bit_offset;
|
||||
m_bit_offset++;
|
||||
|
||||
if (m_bit_offset > 7) {
|
||||
TRY(m_stream->write({ &m_current_byte, sizeof(m_current_byte) }));
|
||||
m_bit_offset = 0;
|
||||
m_current_byte = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
virtual bool is_eof() const override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool is_open() const override
|
||||
{
|
||||
return m_stream->is_open();
|
||||
}
|
||||
|
||||
virtual void close() override
|
||||
{
|
||||
}
|
||||
|
||||
size_t bit_offset() const
|
||||
{
|
||||
return m_bit_offset;
|
||||
}
|
||||
|
||||
ErrorOr<void> align_to_byte_boundary()
|
||||
{
|
||||
if (m_bit_offset == 0)
|
||||
return {};
|
||||
|
||||
TRY(write_bits(0u, 8 - m_bit_offset));
|
||||
VERIFY(m_bit_offset == 0);
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
LittleEndianOutputBitStream(MaybeOwned<Stream> stream)
|
||||
: m_stream(move(stream))
|
||||
{
|
||||
}
|
||||
|
||||
MaybeOwned<Stream> m_stream;
|
||||
u8 m_current_byte { 0 };
|
||||
size_t m_bit_offset { 0 };
|
||||
};
|
||||
|
||||
}
|
|
@ -5,8 +5,8 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/BitStream.h>
|
||||
#include <AK/Tuple.h>
|
||||
#include <LibCore/BitStream.h>
|
||||
#include <LibCore/MemoryStream.h>
|
||||
#include <LibPDF/CommonNames.h>
|
||||
#include <LibPDF/Document.h>
|
||||
|
@ -598,7 +598,7 @@ PDFErrorOr<Vector<DocumentParser::PageOffsetHintTableEntry>> DocumentParser::par
|
|||
auto input_stream = TRY(Core::Stream::FixedMemoryStream::construct(hint_stream_bytes));
|
||||
TRY(input_stream->seek(sizeof(PageOffsetHintTable)));
|
||||
|
||||
auto bit_stream = TRY(Core::Stream::LittleEndianInputBitStream::construct(move(input_stream)));
|
||||
auto bit_stream = TRY(LittleEndianInputBitStream::construct(move(input_stream)));
|
||||
|
||||
auto number_of_pages = m_linearization_dictionary.value().number_of_pages;
|
||||
Vector<PageOffsetHintTableEntry> entries;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue