1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:47:45 +00:00

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.
This commit is contained in:
Zaggy1024 2022-10-08 20:41:43 -05:00 committed by Andrew Kaster
parent 13ccde8637
commit 647472b716

View file

@ -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;