From 647472b716e0ff70ce8d2a91d5293f7a4ce968d6 Mon Sep 17 00:00:00 2001 From: Zaggy1024 Date: Sat, 8 Oct 2022 20:41:43 -0500 Subject: [PATCH] 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. --- Userland/Libraries/LibVideo/VP9/BitStream.cpp | 21 ++++++++++--------- 1 file 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 + #include "BitStream.h" namespace Video::VP9 { @@ -96,16 +98,15 @@ ErrorOr 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;