diff options
author | Zaggy1024 <zaggy1024@gmail.com> | 2022-10-08 20:41:43 -0500 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2022-10-09 20:32:40 -0600 |
commit | 647472b716e0ff70ce8d2a91d5293f7a4ce968d6 (patch) | |
tree | 780ab3aef239b789e68003b67b189e2a80eb1fcb /Userland/Libraries/LibVideo/VP9 | |
parent | 13ccde8637bb4d71bbf7ce65c00a844792028d0f (diff) | |
download | serenity-647472b716e0ff70ce8d2a91d5293f7a4ce968d6.zip |
LibVideo: Read multiple raw bits at once to refill the range decoder
This will allow BitStream::read_bool() to read more than one bit from
the range-coded bitstream at a time if needed.
Diffstat (limited to 'Userland/Libraries/LibVideo/VP9')
-rw-r--r-- | Userland/Libraries/LibVideo/VP9/BitStream.cpp | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/Userland/Libraries/LibVideo/VP9/BitStream.cpp b/Userland/Libraries/LibVideo/VP9/BitStream.cpp index 5ad26bbd0c..8b8fba12ab 100644 --- a/Userland/Libraries/LibVideo/VP9/BitStream.cpp +++ b/Userland/Libraries/LibVideo/VP9/BitStream.cpp @@ -5,6 +5,8 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include <AK/BuiltinWrappers.h> + #include "BitStream.h" namespace Video::VP9 { @@ -96,16 +98,15 @@ ErrorOr<bool> BitStream::read_bool(u8 probability) return_bool = true; } - while (m_bool_range < 128) { - bool new_bit; - if (m_bool_max_bits) { - new_bit = TRY(read_bit()); - m_bool_max_bits--; - } else { - new_bit = false; - } - m_bool_range *= 2; - m_bool_value = (m_bool_value << 1u) + new_bit; + if (m_bool_range < 128) { + u8 bits_to_shift_into_range = count_leading_zeroes(m_bool_range); + + if (bits_to_shift_into_range > m_bool_max_bits) + return Error::from_string_literal("Range decoder is out of data"); + + m_bool_range <<= bits_to_shift_into_range; + m_bool_value = (m_bool_value << bits_to_shift_into_range) | TRY(read_bits(bits_to_shift_into_range)); + m_bool_max_bits -= bits_to_shift_into_range; } return return_bool; |