From 372a4ea8c1c17a3b6a6140423db427f671d11d54 Mon Sep 17 00:00:00 2001 From: Zaggy1024 Date: Mon, 7 Nov 2022 01:13:19 -0600 Subject: [PATCH] LibVideo: Parameterize parsing if a block is inter predicted in VP9 --- Userland/Libraries/LibVideo/VP9/Parser.cpp | 9 +++-- .../Libraries/LibVideo/VP9/TreeParser.cpp | 35 +++++++++---------- Userland/Libraries/LibVideo/VP9/TreeParser.h | 2 +- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Userland/Libraries/LibVideo/VP9/Parser.cpp b/Userland/Libraries/LibVideo/VP9/Parser.cpp index 6c09e4f2b4..48e33dac7d 100644 --- a/Userland/Libraries/LibVideo/VP9/Parser.cpp +++ b/Userland/Libraries/LibVideo/VP9/Parser.cpp @@ -1178,10 +1178,13 @@ u8 Parser::get_segment_id() DecoderErrorOr Parser::read_is_inter() { - if (seg_feature_active(SEG_LVL_REF_FRAME)) + if (seg_feature_active(SEG_LVL_REF_FRAME)) { m_is_inter = m_feature_data[m_segment_id][SEG_LVL_REF_FRAME] != IntraFrame; - else - m_is_inter = TRY_READ(m_tree_parser->parse_tree(SyntaxElementType::IsInter)); + } else { + Optional above_intra = m_available_u ? m_above_intra : Optional(); + Optional left_intra = m_available_l ? m_left_intra : Optional(); + m_is_inter = TRY_READ(TreeParser::parse_is_inter(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, above_intra, left_intra)); + } return {}; } diff --git a/Userland/Libraries/LibVideo/VP9/TreeParser.cpp b/Userland/Libraries/LibVideo/VP9/TreeParser.cpp index eff3876047..99bfee4ce8 100644 --- a/Userland/Libraries/LibVideo/VP9/TreeParser.cpp +++ b/Userland/Libraries/LibVideo/VP9/TreeParser.cpp @@ -289,13 +289,29 @@ ErrorOr TreeParser::parse_tx_size(BitStream& bit_stream, ProbabilityTabl return value; } +ErrorOr TreeParser::parse_is_inter(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, Optional above_intra, Optional left_intra) +{ + // FIXME: Above and left contexts should be in structs. + + // Probabilities + u8 context = 0; + if (above_intra.has_value() && left_intra.has_value()) + context = (left_intra.value() && above_intra.value()) ? 3 : static_cast(above_intra.value() || left_intra.value()); + else if (above_intra.has_value() || left_intra.has_value()) + context = 2 * static_cast(above_intra.has_value() ? above_intra.value() : left_intra.value()); + u8 probability = probability_table.is_inter_prob()[context]; + + auto value = TRY(parse_tree_new(bit_stream, { binary_tree }, [&](u8) { return probability; })); + increment_counter(counter.m_counts_is_inter[context][value]); + return value; +} + /* * Select a tree value based on the type of syntax element being parsed, as well as some parser state, as specified in section 9.3.1 */ TreeParser::TreeSelection TreeParser::select_tree(SyntaxElementType type) { switch (type) { - case SyntaxElementType::IsInter: case SyntaxElementType::CompMode: case SyntaxElementType::CompRef: case SyntaxElementType::SingleRefP1: @@ -331,8 +347,6 @@ TreeParser::TreeSelection TreeParser::select_tree(SyntaxElementType type) u8 TreeParser::select_tree_probability(SyntaxElementType type, u8 node) { switch (type) { - case SyntaxElementType::IsInter: - return calculate_is_inter_probability(); case SyntaxElementType::CompMode: return calculate_comp_mode_probability(); case SyntaxElementType::CompRef: @@ -384,18 +398,6 @@ u8 TreeParser::select_tree_probability(SyntaxElementType type, u8 node) #define ABOVE_SINGLE m_decoder.m_above_single #define LEFT_SINGLE m_decoder.m_left_single -u8 TreeParser::calculate_is_inter_probability() -{ - if (AVAIL_U && AVAIL_L) { - m_ctx = (LEFT_INTRA && ABOVE_INTRA) ? 3 : LEFT_INTRA || ABOVE_INTRA; - } else if (AVAIL_U || AVAIL_L) { - m_ctx = 2 * (AVAIL_U ? ABOVE_INTRA : LEFT_INTRA); - } else { - m_ctx = 0; - } - return m_decoder.m_probability_tables->is_inter_prob()[m_ctx]; -} - u8 TreeParser::calculate_comp_mode_probability() { if (AVAIL_U && AVAIL_L) { @@ -720,9 +722,6 @@ void TreeParser::count_syntax_element(SyntaxElementType type, int value) increment_counter(count); }; switch (type) { - case SyntaxElementType::IsInter: - increment(m_decoder.m_syntax_element_counter->m_counts_is_inter[m_ctx][value]); - return; case SyntaxElementType::CompMode: increment(m_decoder.m_syntax_element_counter->m_counts_comp_mode[m_ctx][value]); return; diff --git a/Userland/Libraries/LibVideo/VP9/TreeParser.h b/Userland/Libraries/LibVideo/VP9/TreeParser.h index 7b6d812b3e..fb6a9cfa9c 100644 --- a/Userland/Libraries/LibVideo/VP9/TreeParser.h +++ b/Userland/Libraries/LibVideo/VP9/TreeParser.h @@ -74,6 +74,7 @@ public: static ErrorOr parse_interpolation_filter(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, Optional above_ref_frame, Optional left_ref_frame, Optional above_interpolation_filter, Optional left_interpolation_filter); static ErrorOr parse_skip(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, Optional const& above_skip, Optional const& left_skip); static ErrorOr parse_tx_size(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, TXSize max_tx_size, Optional above_skip, Optional left_skip, Optional above_tx_size, Optional left_tx_size); + static ErrorOr parse_is_inter(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, Optional above_intra, Optional left_intra); void set_default_intra_mode_variables(u8 idx, u8 idy) { @@ -107,7 +108,6 @@ public: } private: - u8 calculate_is_inter_probability(); u8 calculate_comp_mode_probability(); u8 calculate_comp_ref_probability(); u8 calculate_single_ref_p1_probability();