From 6e6cc1ddb2dc6b273d9e761eef9e6fb56dbbe455 Mon Sep 17 00:00:00 2001 From: Zaggy1024 Date: Sun, 16 Apr 2023 10:03:21 -0500 Subject: [PATCH] LibVideo/VP9: Make a lookup table for bit reversals Bit reversals are used very often in intra-predicted frames. Turning these into a constexpr lookup table reduces the branching needed for block transforms significantly. This reduces the times spent decoding an intra-heavy 1080p video by about 9% (~14.3s -> ~12.9s). --- Userland/Libraries/LibVideo/VP9/Decoder.cpp | 4 +-- Userland/Libraries/LibVideo/VP9/Utilities.h | 27 +++++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibVideo/VP9/Decoder.cpp b/Userland/Libraries/LibVideo/VP9/Decoder.cpp index a58526c0ae..1a88774f22 100644 --- a/Userland/Libraries/LibVideo/VP9/Decoder.cpp +++ b/Userland/Libraries/LibVideo/VP9/Decoder.cpp @@ -1400,7 +1400,7 @@ inline DecoderErrorOr Decoder::inverse_discrete_cosine_transform_array_per // 1.2. T[ i ] is set equal to copyT[ brev( n, i ) ] for i = 0..((1<(i)]; return {}; } @@ -1432,7 +1432,7 @@ inline DecoderErrorOr Decoder::inverse_discrete_cosine_transform(Span(index), false); } // 2.7 If n is greater than or equal to 3: diff --git a/Userland/Libraries/LibVideo/VP9/Utilities.h b/Userland/Libraries/LibVideo/VP9/Utilities.h index ff79929fa9..726331e05b 100644 --- a/Userland/Libraries/LibVideo/VP9/Utilities.h +++ b/Userland/Libraries/LibVideo/VP9/Utilities.h @@ -34,15 +34,26 @@ u16 clip_1(u8 bit_depth, T x) return x; } -template -inline T brev(C bit_count, T value) +template +inline u8 brev(u8 value) { - T result = 0; - for (C i = 0; i < bit_count; i++) { - auto bit = (value >> i) & 1; - result |= bit << (bit_count - 1 - i); - } - return result; + static_assert(bits <= 8, "brev() expects an 8-bit value."); + + static constexpr auto lookup_table = [] { + constexpr size_t value_count = 1 << bits; + Array the_table; + for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) { + u8 reversed = 0; + for (u8 bit_index = 0; bit_index < bits; bit_index++) { + auto bit = (lookup_value >> bit_index) & 1; + reversed |= bit << (bits - 1 - bit_index); + } + the_table[lookup_value] = reversed; + } + return the_table; + }(); + + return lookup_table[value]; } inline BlockSubsize get_subsampled_block_size(BlockSubsize size, bool subsampling_x, bool subsampling_y)