Serenity Operating System
at master 87 lines 2.6 kB view raw
1/* 2 * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com> 3 * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <AK/BuiltinWrappers.h> 9 10#include "BooleanDecoder.h" 11 12namespace Video::VP9 { 13 14/* 9.2.1 */ 15ErrorOr<BooleanDecoder> BooleanDecoder::initialize(MaybeOwned<BigEndianInputBitStream> bit_stream, size_t bytes) 16{ 17 VERIFY(bit_stream->is_aligned_to_byte_boundary()); 18 auto value = TRY(bit_stream->read_value<u8>()); 19 u8 range = 255; 20 u64 max_bits = (8 * bytes) - 8; 21 BooleanDecoder decoder { move(bit_stream), value, range, max_bits }; 22 if (TRY(decoder.read_bool(128))) 23 return Error::from_string_literal("Range decoder marker was non-zero"); 24 return decoder; 25} 26 27/* 9.2.2 */ 28ErrorOr<bool> BooleanDecoder::read_bool(u8 probability) 29{ 30 auto split = 1u + (((m_range - 1u) * probability) >> 8u); 31 bool return_bool; 32 33 if (m_value < split) { 34 m_range = split; 35 return_bool = false; 36 } else { 37 m_range -= split; 38 m_value -= split; 39 return_bool = true; 40 } 41 42 if (m_range < 128) { 43 u8 bits_to_shift_into_range = count_leading_zeroes(m_range); 44 45 if (bits_to_shift_into_range > m_bits_left) 46 return Error::from_string_literal("Range decoder is out of data"); 47 48 m_range <<= bits_to_shift_into_range; 49 m_value = (m_value << bits_to_shift_into_range) | TRY(m_bit_stream->read_bits<u8>(bits_to_shift_into_range)); 50 m_bits_left -= bits_to_shift_into_range; 51 } 52 53 return return_bool; 54} 55 56ErrorOr<u8> BooleanDecoder::read_literal(u8 bits) 57{ 58 u8 return_value = 0; 59 for (size_t i = 0; i < bits; i++) { 60 return_value = (2 * return_value) + TRY(read_bool(128)); 61 } 62 return return_value; 63} 64 65size_t BooleanDecoder::bits_remaining() const 66{ 67 return m_bits_left; 68} 69 70/* 9.2.3 */ 71ErrorOr<void> BooleanDecoder::finish_decode() 72{ 73 while (m_bits_left > 0) { 74 auto padding_read_size = min(m_bits_left, 64); 75 auto padding_bits = TRY(m_bit_stream->read_bits(padding_read_size)); 76 m_bits_left -= padding_read_size; 77 78 if (padding_bits != 0) 79 return Error::from_string_literal("Range decoder has non-zero padding element"); 80 } 81 82 // FIXME: It is a requirement of bitstream conformance that enough padding bits are inserted to ensure that the final coded byte of a frame is not equal to a superframe marker. 83 // A byte b is equal to a superframe marker if and only if (b & 0xe0)is equal to 0xc0, i.e. if the most significant 3 bits are equal to 0b110. 84 return {}; 85} 86 87}