diff --git a/Userland/Libraries/LibVideo/VP9/Parser.cpp b/Userland/Libraries/LibVideo/VP9/Parser.cpp index 0af35b176e..cb33f79084 100644 --- a/Userland/Libraries/LibVideo/VP9/Parser.cpp +++ b/Userland/Libraries/LibVideo/VP9/Parser.cpp @@ -1017,8 +1017,19 @@ DecoderErrorOr Parser::intra_frame_mode_info() m_ref_frame[0] = IntraFrame; m_ref_frame[1] = None; m_is_inter = false; + // FIXME: This if statement is also present in parse_default_intra_mode. The selection of parameters for + // the probability table lookup should be inlined here. if (m_mi_size >= Block_8x8) { - m_default_intra_mode = TRY_READ(m_tree_parser->parse_tree(SyntaxElementType::DefaultIntraMode)); + // FIXME: This context should be available in the block setup. Make a struct to store the context + // that is needed to call the tree parses and set it in decode_block(). + auto above_context = Optional const&>(); + auto left_context = Optional const&>(); + if (m_available_u) + above_context = m_sub_modes[get_image_index(m_mi_row - 1, m_mi_col)]; + if (m_available_l) + left_context = m_sub_modes[get_image_index(m_mi_row, m_mi_col - 1)]; + m_default_intra_mode = TRY_READ(TreeParser::parse_default_intra_mode(*m_bit_stream, *m_probability_tables, m_mi_size, above_context, left_context, m_block_sub_modes, 0, 0)); + m_y_mode = m_default_intra_mode; for (auto& block_sub_mode : m_block_sub_modes) block_sub_mode = m_y_mode; @@ -1027,8 +1038,15 @@ DecoderErrorOr Parser::intra_frame_mode_info() m_num_4x4_h = num_4x4_blocks_high_lookup[m_mi_size]; for (auto idy = 0; idy < 2; idy += m_num_4x4_h) { for (auto idx = 0; idx < 2; idx += m_num_4x4_w) { - m_tree_parser->set_default_intra_mode_variables(idx, idy); - m_default_intra_mode = TRY_READ(m_tree_parser->parse_tree(SyntaxElementType::DefaultIntraMode)); + // FIXME: See the FIXME above. + auto above_context = Optional const&>(); + auto left_context = Optional const&>(); + if (m_available_u) + above_context = m_sub_modes[get_image_index(m_mi_row - 1, m_mi_col)]; + if (m_available_l) + left_context = m_sub_modes[get_image_index(m_mi_row, m_mi_col - 1)]; + m_default_intra_mode = TRY_READ(TreeParser::parse_default_intra_mode(*m_bit_stream, *m_probability_tables, m_mi_size, above_context, left_context, m_block_sub_modes, idx, idy)); + for (auto y = 0; y < m_num_4x4_h; y++) { for (auto x = 0; x < m_num_4x4_w; x++) { auto index = (idy + y) * 2 + idx + x; diff --git a/Userland/Libraries/LibVideo/VP9/Parser.h b/Userland/Libraries/LibVideo/VP9/Parser.h index b8a8299e92..8ad62e4d51 100644 --- a/Userland/Libraries/LibVideo/VP9/Parser.h +++ b/Userland/Libraries/LibVideo/VP9/Parser.h @@ -243,6 +243,9 @@ private: u8 m_num_4x4_w { 0 }; u8 m_num_4x4_h { 0 }; PredictionMode m_uv_mode { 0 }; // FIXME: Is u8 the right size? + // FIXME: From spec: NOTE – We are using a 2D array to store the SubModes for clarity. It is possible to reduce memory + // consumption by only storing one intra mode for each 8x8 horizontal and vertical position, i.e. to use two 1D + // arrays instead. Vector> m_sub_modes; ReferenceFramePair m_left_ref_frame; ReferenceFramePair m_above_ref_frame; diff --git a/Userland/Libraries/LibVideo/VP9/TreeParser.cpp b/Userland/Libraries/LibVideo/VP9/TreeParser.cpp index 8fc4fc8270..2cb90bc91f 100644 --- a/Userland/Libraries/LibVideo/VP9/TreeParser.cpp +++ b/Userland/Libraries/LibVideo/VP9/TreeParser.cpp @@ -7,10 +7,10 @@ #include -#include "TreeParser.h" #include "Enums.h" #include "LookupTables.h" #include "Parser.h" +#include "TreeParser.h" namespace Video::VP9 { @@ -104,13 +104,42 @@ ErrorOr TreeParser::parse_partition(BitStream& bit_stream, Probabilit return value; } +ErrorOr TreeParser::parse_default_intra_mode(BitStream& bit_stream, ProbabilityTables const& probability_table, BlockSubsize mi_size, Optional const&> above_context, Optional const&> left_context, PredictionMode block_sub_modes[4], u8 index_x, u8 index_y) +{ + // FIXME: This should use a struct for the above and left contexts. + + // Tree + TreeParser::TreeSelection tree = { intra_mode_tree }; + + // Probabilities + PredictionMode above_mode, left_mode; + if (mi_size >= Block_8x8) { + above_mode = above_context.has_value() ? above_context.value()[2] : PredictionMode::DcPred; + left_mode = left_context.has_value() ? left_context.value()[1] : PredictionMode::DcPred; + } else { + if (index_y > 0) + above_mode = block_sub_modes[index_x]; + else + above_mode = above_context.has_value() ? above_context.value()[2 + index_x] : PredictionMode::DcPred; + + if (index_x > 0) + left_mode = block_sub_modes[index_y << 1]; + else + left_mode = left_context.has_value() ? left_context.value()[1 + (index_y << 1)] : PredictionMode::DcPred; + } + u8 const* probabilities = probability_table.kf_y_mode_probs()[to_underlying(above_mode)][to_underlying(left_mode)]; + + auto value = TRY(parse_tree_new(bit_stream, tree, [&](u8 node) { return probabilities[node]; })); + // Default intra mode is not counted. + 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::DefaultIntraMode: case SyntaxElementType::DefaultUVMode: case SyntaxElementType::IntraMode: case SyntaxElementType::SubIntraMode: @@ -166,8 +195,6 @@ TreeParser::TreeSelection TreeParser::select_tree(SyntaxElementType type) u8 TreeParser::select_tree_probability(SyntaxElementType type, u8 node) { switch (type) { - case SyntaxElementType::DefaultIntraMode: - return calculate_default_intra_mode_probability(node); case SyntaxElementType::DefaultUVMode: return calculate_default_uv_mode_probability(node); case SyntaxElementType::IntraMode: @@ -241,36 +268,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_default_intra_mode_probability(u8 node) -{ - PredictionMode above_mode, left_mode; - if (m_decoder.m_mi_size >= Block_8x8) { - above_mode = AVAIL_U - ? m_decoder.m_sub_modes[m_decoder.get_image_index(m_decoder.m_mi_row - 1, m_decoder.m_mi_col)][2] - : PredictionMode::DcPred; - left_mode = AVAIL_L - ? m_decoder.m_sub_modes[m_decoder.get_image_index(m_decoder.m_mi_row, m_decoder.m_mi_col - 1)][1] - : PredictionMode::DcPred; - } else { - if (m_idy) { - above_mode = m_decoder.m_block_sub_modes[m_idx]; - } else { - above_mode = AVAIL_U - ? m_decoder.m_sub_modes[m_decoder.get_image_index(m_decoder.m_mi_row - 1, m_decoder.m_mi_col)][2 + m_idx] - : PredictionMode::DcPred; - } - - if (m_idx) { - left_mode = m_decoder.m_block_sub_modes[m_idy * 2]; - } else { - left_mode = AVAIL_L - ? m_decoder.m_sub_modes[m_decoder.get_image_index(m_decoder.m_mi_row, m_decoder.m_mi_col - 1)][1 + m_idy * 2] - : PredictionMode::DcPred; - } - } - return m_decoder.m_probability_tables->kf_y_mode_probs()[to_underlying(above_mode)][to_underlying(left_mode)][node]; -} - u8 TreeParser::calculate_default_uv_mode_probability(u8 node) { return m_decoder.m_probability_tables->kf_uv_mode_prob()[to_underlying(m_decoder.m_y_mode)][node]; @@ -772,7 +769,6 @@ void TreeParser::count_syntax_element(SyntaxElementType type, int value) case SyntaxElementType::MoreCoefs: increment(m_decoder.m_syntax_element_counter->m_counts_more_coefs[m_tx_size][m_plane > 0][m_decoder.m_is_inter][m_band][m_ctx][value]); return; - case SyntaxElementType::DefaultIntraMode: case SyntaxElementType::DefaultUVMode: case SyntaxElementType::SegmentID: case SyntaxElementType::SegIDPredicted: diff --git a/Userland/Libraries/LibVideo/VP9/TreeParser.h b/Userland/Libraries/LibVideo/VP9/TreeParser.h index 18b890bea6..7f031c0382 100644 --- a/Userland/Libraries/LibVideo/VP9/TreeParser.h +++ b/Userland/Libraries/LibVideo/VP9/TreeParser.h @@ -63,6 +63,7 @@ public: void count_syntax_element(SyntaxElementType type, int value); static ErrorOr parse_partition(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, bool has_rows, bool has_columns, BlockSubsize block_subsize, u8 num_8x8, Vector const& above_partition_context, Vector const& left_partition_context, u32 row, u32 column, bool frame_is_intra); + static ErrorOr parse_default_intra_mode(BitStream&, ProbabilityTables const&, BlockSubsize mi_size, Optional const&> above_context, Optional const&> left_context, PredictionMode block_sub_modes[4], u8 index_x, u8 index_y); void set_default_intra_mode_variables(u8 idx, u8 idy) { @@ -96,7 +97,6 @@ public: } private: - u8 calculate_default_intra_mode_probability(u8 node); u8 calculate_default_uv_mode_probability(u8 node); u8 calculate_intra_mode_probability(u8 node); u8 calculate_sub_intra_mode_probability(u8 node);