From 1dfb065a9cc368bcb3ad209722d519a16bbfc94e Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 26 May 2023 15:08:22 -0400 Subject: [PATCH] LibGfx+LibVideo: Make BooleanDecoder usable for both VP8 and VP9 The marker bit is VP9-only, so move that into a new initialize_vp9() function. finish_decode() is VP9-only, so rename that to finish_decode_vp9(). --- .../Libraries/LibGfx/ImageFormats/BooleanDecoder.cpp | 11 ++++++++--- .../Libraries/LibGfx/ImageFormats/BooleanDecoder.h | 9 +++++++-- Userland/Libraries/LibVideo/VP9/Context.h | 2 +- Userland/Libraries/LibVideo/VP9/Parser.cpp | 6 +++--- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/BooleanDecoder.cpp b/Userland/Libraries/LibGfx/ImageFormats/BooleanDecoder.cpp index 69b3a91d76..7b51b419e4 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/BooleanDecoder.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/BooleanDecoder.cpp @@ -11,14 +11,19 @@ namespace Gfx { -/* 9.2.1 */ ErrorOr BooleanDecoder::initialize(MaybeOwned bit_stream, size_t size_in_bytes) { VERIFY(bit_stream->is_aligned_to_byte_boundary()); auto value = TRY(bit_stream->read_value()); u8 range = 255; u64 bits_left = (8 * size_in_bytes) - 8; - BooleanDecoder decoder { move(bit_stream), value, range, bits_left }; + return BooleanDecoder { move(bit_stream), value, range, bits_left }; +} + +/* 9.2.1 */ +ErrorOr BooleanDecoder::initialize_vp9(MaybeOwned bit_stream, size_t size_in_bytes) +{ + BooleanDecoder decoder = TRY(initialize(move(bit_stream), size_in_bytes)); if (TRY(decoder.read_bool(128))) return Error::from_string_literal("Range decoder marker was non-zero"); return decoder; @@ -63,7 +68,7 @@ ErrorOr BooleanDecoder::read_literal(u8 bits) } /* 9.2.3 */ -ErrorOr BooleanDecoder::finish_decode() +ErrorOr BooleanDecoder::finish_decode_vp9() { while (m_bits_left > 0) { auto padding_read_size = min(m_bits_left, 64); diff --git a/Userland/Libraries/LibGfx/ImageFormats/BooleanDecoder.h b/Userland/Libraries/LibGfx/ImageFormats/BooleanDecoder.h index d17a4ee2c7..e56e165329 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/BooleanDecoder.h +++ b/Userland/Libraries/LibGfx/ImageFormats/BooleanDecoder.h @@ -14,13 +14,18 @@ namespace Gfx { +// Can decode bitstreams encoded with VP8's and VP9's arithmetic boolean encoder. class BooleanDecoder { public: - /* (9.2) */ static ErrorOr initialize(MaybeOwned bit_stream, size_t size_in_bytes); + + /* (9.2) */ + static ErrorOr initialize_vp9(MaybeOwned bit_stream, size_t size_in_bytes); + ErrorOr read_bool(u8 probability); ErrorOr read_literal(u8 bits); - ErrorOr finish_decode(); + + ErrorOr finish_decode_vp9(); private: BooleanDecoder(MaybeOwned&& bit_stream, u8 value, u8 range, u64 bits_left) diff --git a/Userland/Libraries/LibVideo/VP9/Context.h b/Userland/Libraries/LibVideo/VP9/Context.h index 9c6b7e7b29..24c2d7a427 100644 --- a/Userland/Libraries/LibVideo/VP9/Context.h +++ b/Userland/Libraries/LibVideo/VP9/Context.h @@ -229,7 +229,7 @@ public: auto context_view = frame_context.m_block_contexts.view(rows_start, columns_start, height, width); auto bit_stream = DECODER_TRY_ALLOC(try_make(DECODER_TRY_ALLOC(try_make(*frame_context.stream)))); - auto decoder = DECODER_TRY(DecoderErrorCategory::Corrupted, BooleanDecoder::initialize(move(bit_stream), tile_size)); + auto decoder = DECODER_TRY(DecoderErrorCategory::Corrupted, BooleanDecoder::initialize_vp9(move(bit_stream), tile_size)); return TileContext { frame_context, diff --git a/Userland/Libraries/LibVideo/VP9/Parser.cpp b/Userland/Libraries/LibVideo/VP9/Parser.cpp index 7128bddce6..a0453da52b 100644 --- a/Userland/Libraries/LibVideo/VP9/Parser.cpp +++ b/Userland/Libraries/LibVideo/VP9/Parser.cpp @@ -593,7 +593,7 @@ void Parser::setup_past_independence() DecoderErrorOr Parser::compressed_header(FrameContext& frame_context) { - auto decoder = TRY_READ(BooleanDecoder::initialize(MaybeOwned(frame_context.bit_stream), frame_context.header_size_in_bytes)); + auto decoder = TRY_READ(BooleanDecoder::initialize_vp9(MaybeOwned(frame_context.bit_stream), frame_context.header_size_in_bytes)); frame_context.transform_mode = TRY(read_tx_mode(decoder, frame_context)); if (frame_context.transform_mode == TransformMode::Select) TRY(tx_mode_probs(decoder)); @@ -610,7 +610,7 @@ DecoderErrorOr Parser::compressed_header(FrameContext& frame_context) TRY(read_partition_probs(decoder)); TRY(mv_probs(decoder, frame_context)); } - TRY_READ(decoder.finish_decode()); + TRY_READ(decoder.finish_decode_vp9()); return {}; } @@ -1002,7 +1002,7 @@ DecoderErrorOr Parser::decode_tile(TileContext& tile_context) TRY(decode_partition(tile_context, row, col, Block_64x64)); } } - TRY_READ(tile_context.decoder.finish_decode()); + TRY_READ(tile_context.decoder.finish_decode_vp9()); return {}; }