2023-05-26 15:04:31 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
|
|
|
|
* Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
2023-05-26 15:08:22 -04:00
|
|
|
// Can decode bitstreams encoded with VP8's and VP9's arithmetic boolean encoder.
|
2023-05-26 15:04:31 -04:00
|
|
|
class BooleanDecoder {
|
|
|
|
public:
|
2023-06-02 13:52:40 -05:00
|
|
|
static ErrorOr<BooleanDecoder> initialize(ReadonlyBytes data);
|
2023-05-26 15:08:22 -04:00
|
|
|
|
|
|
|
/* (9.2) */
|
2023-06-01 19:16:57 -05:00
|
|
|
bool read_bool(u8 probability);
|
|
|
|
u8 read_literal(u8 bits);
|
2023-05-26 15:08:22 -04:00
|
|
|
|
2023-06-02 13:52:40 -05:00
|
|
|
ErrorOr<void> finish_decode();
|
2023-05-26 15:04:31 -04:00
|
|
|
|
|
|
|
private:
|
2023-06-02 13:52:40 -05:00
|
|
|
using ValueType = size_t;
|
|
|
|
static constexpr u8 reserve_bytes = sizeof(ValueType) - 1;
|
|
|
|
static constexpr u8 reserve_bits = reserve_bytes * 8;
|
|
|
|
|
|
|
|
BooleanDecoder(u8 const* data, u64 bytes_left)
|
|
|
|
: m_data(data + 1)
|
|
|
|
, m_bytes_left(bytes_left - 1)
|
|
|
|
, m_range(255)
|
|
|
|
, m_value(static_cast<ValueType>(*data) << reserve_bits)
|
|
|
|
, m_value_bits_left(8)
|
2023-05-26 15:04:31 -04:00
|
|
|
{
|
2023-06-01 19:16:57 -05:00
|
|
|
fill_reservoir();
|
2023-05-26 15:04:31 -04:00
|
|
|
}
|
|
|
|
|
2023-06-01 19:16:57 -05:00
|
|
|
void fill_reservoir();
|
2023-06-02 13:52:40 -05:00
|
|
|
|
|
|
|
u8 const* m_data;
|
|
|
|
size_t m_bytes_left { 0 };
|
2023-06-01 19:16:57 -05:00
|
|
|
bool m_overread { false };
|
2023-06-02 13:52:40 -05:00
|
|
|
// This value will never exceed 255. If this is a u8, the compiler will generate a truncation in read_bool().
|
|
|
|
u32 m_range { 0 };
|
|
|
|
ValueType m_value { 0 };
|
|
|
|
// Like above, this will never exceed reserve_bits, but will truncate if it is a u8.
|
|
|
|
u32 m_value_bits_left { 0 };
|
2023-05-26 15:04:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|