Ver código fonte

LibVideo: Parameterize compound mode parsing in the VP9 decoder

Zaggy1024 2 anos atrás
pai
commit
dd18c42643

+ 10 - 3
Userland/Libraries/LibVideo/VP9/Parser.cpp

@@ -1279,10 +1279,17 @@ DecoderErrorOr<void> Parser::read_ref_frames()
         return {};
     }
     ReferenceMode comp_mode;
-    if (m_reference_mode == ReferenceModeSelect)
-        comp_mode = TRY_READ(m_tree_parser->parse_tree<ReferenceMode>(SyntaxElementType::CompMode));
-    else
+    if (m_reference_mode == ReferenceModeSelect) {
+        Optional<bool> above_single = m_available_u ? m_above_single : Optional<bool>();
+        Optional<bool> left_single = m_available_l ? m_left_single : Optional<bool>();
+        Optional<bool> above_intra = m_available_u ? m_above_intra : Optional<bool>();
+        Optional<bool> left_intra = m_available_l ? m_left_intra : Optional<bool>();
+        Optional<ReferenceFrameType> above_ref_frame_0 = m_available_u ? m_above_ref_frame[0] : Optional<ReferenceFrameType>();
+        Optional<ReferenceFrameType> left_ref_frame_0 = m_available_l ? m_left_ref_frame[0] : Optional<ReferenceFrameType>();
+        comp_mode = TRY_READ(TreeParser::parse_comp_mode(*m_bit_stream, *m_probability_tables, *m_syntax_element_counter, m_comp_fixed_ref, above_single, left_single, above_intra, left_intra, above_ref_frame_0, left_ref_frame_0));
+    } else {
         comp_mode = m_reference_mode;
+    }
     if (comp_mode == CompoundReference) {
         auto idx = m_ref_frame_sign_bias[m_comp_fixed_ref];
         auto comp_ref = TRY_READ(m_tree_parser->parse_tree(SyntaxElementType::CompRef));

+ 40 - 40
Userland/Libraries/LibVideo/VP9/TreeParser.cpp

@@ -306,13 +306,52 @@ ErrorOr<bool> TreeParser::parse_is_inter(BitStream& bit_stream, ProbabilityTable
     return value;
 }
 
+ErrorOr<ReferenceMode> TreeParser::parse_comp_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, SyntaxElementCounter& counter, ReferenceFrameType comp_fixed_ref, Optional<bool> above_single, Optional<bool> left_single, Optional<bool> above_intra, Optional<bool> left_intra, Optional<ReferenceFrameType> above_ref_frame_0, Optional<ReferenceFrameType> left_ref_frame_0)
+{
+    // FIXME: Above and left contexts should be in structs.
+
+    // Probabilities
+    u8 context;
+    if (above_single.has_value() && left_single.has_value()) {
+        if (above_single.value() && left_single.value()) {
+            auto is_above_fixed = above_ref_frame_0.value() == comp_fixed_ref;
+            auto is_left_fixed = left_ref_frame_0.value() == comp_fixed_ref;
+            context = is_above_fixed ^ is_left_fixed;
+        } else if (above_single.value()) {
+            auto is_above_fixed = above_ref_frame_0.value() == comp_fixed_ref;
+            context = 2 + static_cast<u8>(is_above_fixed || above_intra.value());
+        } else if (left_single.value()) {
+            auto is_left_fixed = left_ref_frame_0.value() == comp_fixed_ref;
+            context = 2 + static_cast<u8>(is_left_fixed || left_intra.value());
+        } else {
+            context = 4;
+        }
+    } else if (above_single.has_value()) {
+        if (above_single.value())
+            context = above_ref_frame_0.value() == comp_fixed_ref;
+        else
+            context = 3;
+    } else if (left_single.has_value()) {
+        if (left_single.value())
+            context = static_cast<u8>(left_ref_frame_0.value() == comp_fixed_ref);
+        else
+            context = 3;
+    } else {
+        context = 1;
+    }
+    u8 probability = probability_table.comp_mode_prob()[context];
+
+    auto value = TRY(parse_tree_new<ReferenceMode>(bit_stream, { binary_tree }, [&](u8) { return probability; }));
+    increment_counter(counter.m_counts_comp_mode[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::CompMode:
     case SyntaxElementType::CompRef:
     case SyntaxElementType::SingleRefP1:
     case SyntaxElementType::SingleRefP2:
@@ -347,8 +386,6 @@ TreeParser::TreeSelection TreeParser::select_tree(SyntaxElementType type)
 u8 TreeParser::select_tree_probability(SyntaxElementType type, u8 node)
 {
     switch (type) {
-    case SyntaxElementType::CompMode:
-        return calculate_comp_mode_probability();
     case SyntaxElementType::CompRef:
         return calculate_comp_ref_probability();
     case SyntaxElementType::SingleRefP1:
@@ -398,40 +435,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_comp_mode_probability()
-{
-    if (AVAIL_U && AVAIL_L) {
-        if (ABOVE_SINGLE && LEFT_SINGLE) {
-            auto is_above_fixed = ABOVE_FRAME_0 == m_decoder.m_comp_fixed_ref;
-            auto is_left_fixed = LEFT_FRAME_0 == m_decoder.m_comp_fixed_ref;
-            m_ctx = is_above_fixed ^ is_left_fixed;
-        } else if (ABOVE_SINGLE) {
-            auto is_above_fixed = ABOVE_FRAME_0 == m_decoder.m_comp_fixed_ref;
-            m_ctx = 2 + (is_above_fixed || ABOVE_INTRA);
-        } else if (LEFT_SINGLE) {
-            auto is_left_fixed = LEFT_FRAME_0 == m_decoder.m_comp_fixed_ref;
-            m_ctx = 2 + (is_left_fixed || LEFT_INTRA);
-        } else {
-            m_ctx = 4;
-        }
-    } else if (AVAIL_U) {
-        if (ABOVE_SINGLE) {
-            m_ctx = ABOVE_FRAME_0 == m_decoder.m_comp_fixed_ref;
-        } else {
-            m_ctx = 3;
-        }
-    } else if (AVAIL_L) {
-        if (LEFT_SINGLE) {
-            m_ctx = LEFT_FRAME_0 == m_decoder.m_comp_fixed_ref;
-        } else {
-            m_ctx = 3;
-        }
-    } else {
-        m_ctx = 1;
-    }
-    return m_decoder.m_probability_tables->comp_mode_prob()[m_ctx];
-}
-
 u8 TreeParser::calculate_comp_ref_probability()
 {
     auto fix_ref_idx = m_decoder.m_ref_frame_sign_bias[m_decoder.m_comp_fixed_ref];
@@ -722,9 +725,6 @@ void TreeParser::count_syntax_element(SyntaxElementType type, int value)
         increment_counter(count);
     };
     switch (type) {
-    case SyntaxElementType::CompMode:
-        increment(m_decoder.m_syntax_element_counter->m_counts_comp_mode[m_ctx][value]);
-        return;
     case SyntaxElementType::CompRef:
         increment(m_decoder.m_syntax_element_counter->m_counts_comp_ref[m_ctx][value]);
         return;

+ 1 - 1
Userland/Libraries/LibVideo/VP9/TreeParser.h

@@ -75,6 +75,7 @@ public:
     static ErrorOr<bool> parse_skip(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, Optional<bool> const& above_skip, Optional<bool> const& left_skip);
     static ErrorOr<TXSize> parse_tx_size(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, TXSize max_tx_size, Optional<bool> above_skip, Optional<bool> left_skip, Optional<TXSize> above_tx_size, Optional<TXSize> left_tx_size);
     static ErrorOr<bool> parse_is_inter(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, Optional<bool> above_intra, Optional<bool> left_intra);
+    static ErrorOr<ReferenceMode> parse_comp_mode(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, ReferenceFrameType comp_fixed_ref, Optional<bool> above_single, Optional<bool> left_single, Optional<bool> above_intra, Optional<bool> left_intra, Optional<ReferenceFrameType> above_ref_frame_0, Optional<ReferenceFrameType> left_ref_frame_0);
 
     void set_default_intra_mode_variables(u8 idx, u8 idy)
     {
@@ -108,7 +109,6 @@ public:
     }
 
 private:
-    u8 calculate_comp_mode_probability();
     u8 calculate_comp_ref_probability();
     u8 calculate_single_ref_p1_probability();
     u8 calculate_single_ref_p2_probability();