diff options
author | Zaggy1024 <zaggy1024@gmail.com> | 2022-11-05 17:45:53 -0500 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2022-11-12 10:17:27 -0700 |
commit | 981997c0390745050285da2fddea5d67ee283359 (patch) | |
tree | c485c1db5d103f007a912116d8ac5bacfd2df202 | |
parent | 1c6d0a9777d8fcb955745eb39f86996a586ff681 (diff) | |
download | serenity-981997c0390745050285da2fddea5d67ee283359.zip |
LibVideo: Combine VP9's Intra- and InterMode enums into PredictionMode
The two different mode sets are stored in single fields, and the
underlying values didn't overlap, so there was no reason to keep them
separate.
The enum is now an enum class as well, to enforce that almost all uses
of the enum are named. The only case where underlying values are used
is in lookup tables, but it may be worth abstracting that as well to
make array bounds more clear.
-rw-r--r-- | Userland/Libraries/LibVideo/VP9/Decoder.cpp | 28 | ||||
-rw-r--r-- | Userland/Libraries/LibVideo/VP9/Enums.h | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibVideo/VP9/LookupTables.h | 22 | ||||
-rw-r--r-- | Userland/Libraries/LibVideo/VP9/Parser.cpp | 38 | ||||
-rw-r--r-- | Userland/Libraries/LibVideo/VP9/Parser.h | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibVideo/VP9/TreeParser.cpp | 21 |
6 files changed, 61 insertions, 65 deletions
diff --git a/Userland/Libraries/LibVideo/VP9/Decoder.cpp b/Userland/Libraries/LibVideo/VP9/Decoder.cpp index bbe8b52c4e..d498910d3c 100644 --- a/Userland/Libraries/LibVideo/VP9/Decoder.cpp +++ b/Userland/Libraries/LibVideo/VP9/Decoder.cpp @@ -363,13 +363,13 @@ DecoderErrorOr<void> Decoder::predict_intra(u8 plane, u32 x, u32 y, bool have_le // 1. If plane is greater than 0, mode is set equal to uv_mode. // 2. Otherwise, if MiSize is greater than or equal to BLOCK_8X8, mode is set equal to y_mode. // 3. Otherwise, mode is set equal to sub_modes[ blockIdx ]. - IntraMode mode; + PredictionMode mode; if (plane > 0) - mode = static_cast<IntraMode>(m_parser->m_uv_mode); + mode = m_parser->m_uv_mode; else if (m_parser->m_mi_size >= Block_8x8) - mode = static_cast<IntraMode>(m_parser->m_y_mode); + mode = m_parser->m_y_mode; else - mode = static_cast<IntraMode>(m_parser->m_block_sub_modes[block_index]); + mode = m_parser->m_block_sub_modes[block_index]; // The variable log2Size specifying the base 2 logarithm of the width of the transform block is set equal to txSz + 2. u8 log2_of_block_size = tx_size + 2; @@ -473,7 +473,7 @@ DecoderErrorOr<void> Decoder::predict_intra(u8 plane, u32 x, u32 y, bool have_le // FIXME: One of the two below should be a simple memcpy of 1D arrays. switch (mode) { - case IntraMode::VPred: + case PredictionMode::VPred: // − If mode is equal to V_PRED, pred[ i ][ j ] is set equal to aboveRow[ j ] with j = 0..size-1 and i = 0..size-1 // (each row of the block is filled with a copy of aboveRow). for (auto j = 0u; j < block_size; j++) { @@ -481,7 +481,7 @@ DecoderErrorOr<void> Decoder::predict_intra(u8 plane, u32 x, u32 y, bool have_le predicted_sample_at(i, j) = above_row_at(j); } break; - case IntraMode::HPred: + case PredictionMode::HPred: // − Otherwise if mode is equal to H_PRED, pred[ i ][ j ] is set equal to leftCol[ i ] with j = 0..size-1 and i = // 0..size-1 (each column of the block is filled with a copy of leftCol). for (auto j = 0u; j < block_size; j++) { @@ -489,7 +489,7 @@ DecoderErrorOr<void> Decoder::predict_intra(u8 plane, u32 x, u32 y, bool have_le predicted_sample_at(i, j) = left_column[i]; } break; - case IntraMode::D207Pred: + case PredictionMode::D207Pred: // − Otherwise if mode is equal to D207_PRED, the following applies: // 1. pred[ size - 1 ][ j ] = leftCol[ size - 1] for j = 0..size-1 for (auto j = 0u; j < block_size; j++) @@ -512,7 +512,7 @@ DecoderErrorOr<void> Decoder::predict_intra(u8 plane, u32 x, u32 y, bool have_le i--; } break; - case IntraMode::D45Pred: + case PredictionMode::D45Pred: // Otherwise if mode is equal to D45_PRED, // for i = 0..size-1, for j = 0..size-1. for (auto i = 0u; i < block_size; i++) { @@ -527,7 +527,7 @@ DecoderErrorOr<void> Decoder::predict_intra(u8 plane, u32 x, u32 y, bool have_le } } break; - case IntraMode::D63Pred: + case PredictionMode::D63Pred: // Otherwise if mode is equal to D63_PRED, for (auto i = 0u; i < block_size; i++) { for (auto j = 0u; j < block_size; j++) { @@ -543,7 +543,7 @@ DecoderErrorOr<void> Decoder::predict_intra(u8 plane, u32 x, u32 y, bool have_le } } break; - case IntraMode::D117Pred: + case PredictionMode::D117Pred: // Otherwise if mode is equal to D117_PRED, the following applies: // 1. pred[ 0 ][ j ] = Round2( aboveRow[ j - 1 ] + aboveRow[ j ], 1 ) for j = 0..size-1 for (auto j = 0; j < block_size; j++) @@ -564,7 +564,7 @@ DecoderErrorOr<void> Decoder::predict_intra(u8 plane, u32 x, u32 y, bool have_le predicted_sample_at(i, j) = predicted_sample_at(i - 2, j - 1); } break; - case IntraMode::D135Pred: + case PredictionMode::D135Pred: // Otherwise if mode is equal to D135_PRED, the following applies: // 1. pred[ 0 ][ 0 ] = Round2( leftCol[ 0 ] + 2 * aboveRow[ -1 ] + aboveRow[ 0 ], 2 ) predicted_sample_at(0, 0) = round_2(left_column[0] + 2 * above_row_at(-1) + above_row_at(0), 2); @@ -582,7 +582,7 @@ DecoderErrorOr<void> Decoder::predict_intra(u8 plane, u32 x, u32 y, bool have_le predicted_sample_at(i, j) = predicted_sample_at(i - 1, j - 1); } break; - case IntraMode::D153Pred: + case PredictionMode::D153Pred: // Otherwise if mode is equal to D153_PRED, the following applies: // 1. pred[ 0 ][ 0 ] = Round2( leftCol[ 0 ] + aboveRow[ -1 ], 1 ) predicted_sample_at(0, 0) = round_2(left_column[0] + above_row_at(-1), 1); @@ -605,7 +605,7 @@ DecoderErrorOr<void> Decoder::predict_intra(u8 plane, u32 x, u32 y, bool have_le predicted_sample_at(i, j) = predicted_sample_at(i - 1, j - 2); } break; - case IntraMode::TmPred: + case PredictionMode::TmPred: // Otherwise if mode is equal to TM_PRED, // pred[ i ][ j ] is set equal to Clip1( aboveRow[ j ] + leftCol[ i ] - aboveRow[ -1 ] ) // for i = 0..size-1, for j = 0..size-1. @@ -614,7 +614,7 @@ DecoderErrorOr<void> Decoder::predict_intra(u8 plane, u32 x, u32 y, bool have_le predicted_sample_at(i, j) = clip_1(m_parser->m_bit_depth, above_row_at(j) + left_column[i] - above_row_at(-1)); } break; - case IntraMode::DcPred: { + case PredictionMode::DcPred: { // FIXME: All indices are set equally below, use memset. Intermediate average = 0; diff --git a/Userland/Libraries/LibVideo/VP9/Enums.h b/Userland/Libraries/LibVideo/VP9/Enums.h index de5a59282c..e9a8d94d56 100644 --- a/Userland/Libraries/LibVideo/VP9/Enums.h +++ b/Userland/Libraries/LibVideo/VP9/Enums.h @@ -89,7 +89,7 @@ enum Partition : u8 { PartitionSplit = 3, }; -enum IntraMode : u8 { +enum class PredictionMode : u8 { DcPred = 0, VPred = 1, HPred = 2, @@ -100,9 +100,6 @@ enum IntraMode : u8 { D207Pred = 7, D63Pred = 8, TmPred = 9, -}; - -enum InterMode : u8 { NearestMv = 10, NearMv = 11, ZeroMv = 12, diff --git a/Userland/Libraries/LibVideo/VP9/LookupTables.h b/Userland/Libraries/LibVideo/VP9/LookupTables.h index 95a123ca50..b0a3fcde6e 100644 --- a/Userland/Libraries/LibVideo/VP9/LookupTables.h +++ b/Userland/Libraries/LibVideo/VP9/LookupTables.h @@ -107,15 +107,15 @@ static constexpr int partition_tree[6] = { static constexpr int cols_partition_tree[2] = { -PartitionHorizontal, -PartitionSplit }; static constexpr int rows_partition_tree[2] = { -PartitionVertical, -PartitionSplit }; static constexpr int intra_mode_tree[18] = { - -DcPred, 2, - -TmPred, 4, - -VPred, 6, + -to_underlying(PredictionMode::DcPred), 2, + -to_underlying(PredictionMode::TmPred), 4, + -to_underlying(PredictionMode::VPred), 6, 8, 12, - -HPred, 10, - -D135Pred, -D117Pred, - -D45Pred, 14, - -D63Pred, 16, - -D153Pred, -D207Pred + -to_underlying(PredictionMode::HPred), 10, + -to_underlying(PredictionMode::D135Pred), -to_underlying(PredictionMode::D117Pred), + -to_underlying(PredictionMode::D45Pred), 14, + -to_underlying(PredictionMode::D63Pred), 16, + -to_underlying(PredictionMode::D153Pred), -to_underlying(PredictionMode::D207Pred) }; static constexpr int segment_tree[14] = { 2, 4, 6, 8, 10, 12, @@ -133,9 +133,9 @@ static constexpr int tx_size_16_tree[4] = { }; static constexpr int tx_size_8_tree[2] = { -TX_4x4, -TX_8x8 }; static constexpr int inter_mode_tree[6] = { - -(ZeroMv - NearestMv), 2, - -(NearestMv - NearestMv), 4, - -(NearMv - NearestMv), -(NewMv - NearestMv) + -to_underlying(PredictionMode::ZeroMv), 2, + -to_underlying(PredictionMode::NearestMv), 4, + -to_underlying(PredictionMode::NearMv), -to_underlying(PredictionMode::NewMv) }; static constexpr int interp_filter_tree[4] = { -EightTap, 2, diff --git a/Userland/Libraries/LibVideo/VP9/Parser.cpp b/Userland/Libraries/LibVideo/VP9/Parser.cpp index 2355b9ae1b..eac043cede 100644 --- a/Userland/Libraries/LibVideo/VP9/Parser.cpp +++ b/Userland/Libraries/LibVideo/VP9/Parser.cpp @@ -993,7 +993,7 @@ DecoderErrorOr<void> Parser::decode_block(u32 row, u32 col, BlockSubsize subsize } } else { for (size_t b = 0; b < 4; b++) - m_sub_modes[pos][b] = static_cast<IntraMode>(m_block_sub_modes[b]); + m_sub_modes[pos][b] = static_cast<PredictionMode>(m_block_sub_modes[b]); } } } @@ -1018,7 +1018,7 @@ DecoderErrorOr<void> Parser::intra_frame_mode_info() m_ref_frame[1] = None; m_is_inter = false; if (m_mi_size >= Block_8x8) { - m_default_intra_mode = TRY_READ(m_tree_parser->parse_tree<IntraMode>(SyntaxElementType::DefaultIntraMode)); + m_default_intra_mode = TRY_READ(m_tree_parser->parse_tree<PredictionMode>(SyntaxElementType::DefaultIntraMode)); m_y_mode = m_default_intra_mode; for (auto& block_sub_mode : m_block_sub_modes) block_sub_mode = m_y_mode; @@ -1028,7 +1028,7 @@ DecoderErrorOr<void> Parser::intra_frame_mode_info() 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<IntraMode>(SyntaxElementType::DefaultIntraMode)); + m_default_intra_mode = TRY_READ(m_tree_parser->parse_tree<PredictionMode>(SyntaxElementType::DefaultIntraMode)); 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; @@ -1039,7 +1039,7 @@ DecoderErrorOr<void> Parser::intra_frame_mode_info() } m_y_mode = m_default_intra_mode; } - m_uv_mode = TRY_READ(m_tree_parser->parse_tree<u8>(SyntaxElementType::DefaultUVMode)); + m_uv_mode = TRY_READ(m_tree_parser->parse_tree<PredictionMode>(SyntaxElementType::DefaultUVMode)); return {}; } @@ -1164,16 +1164,16 @@ DecoderErrorOr<void> Parser::intra_block_mode_info() m_ref_frame[0] = IntraFrame; m_ref_frame[1] = None; if (m_mi_size >= Block_8x8) { - m_y_mode = TRY_READ(m_tree_parser->parse_tree<u8>(SyntaxElementType::IntraMode)); + m_y_mode = TRY_READ(m_tree_parser->parse_tree<PredictionMode>(SyntaxElementType::IntraMode)); for (auto& block_sub_mode : m_block_sub_modes) block_sub_mode = m_y_mode; } else { m_num_4x4_w = num_4x4_blocks_wide_lookup[m_mi_size]; m_num_4x4_h = num_4x4_blocks_high_lookup[m_mi_size]; - u8 sub_intra_mode; + PredictionMode sub_intra_mode; for (auto idy = 0; idy < 2; idy += m_num_4x4_h) { for (auto idx = 0; idx < 2; idx += m_num_4x4_w) { - sub_intra_mode = TRY_READ(m_tree_parser->parse_tree<u8>(SyntaxElementType::SubIntraMode)); + sub_intra_mode = TRY_READ(m_tree_parser->parse_tree<PredictionMode>(SyntaxElementType::SubIntraMode)); for (auto y = 0; y < m_num_4x4_h; y++) { for (auto x = 0; x < m_num_4x4_w; x++) m_block_sub_modes[(idy + y) * 2 + idx + x] = sub_intra_mode; @@ -1182,7 +1182,7 @@ DecoderErrorOr<void> Parser::intra_block_mode_info() } m_y_mode = sub_intra_mode; } - m_uv_mode = TRY_READ(m_tree_parser->parse_tree<u8>(SyntaxElementType::UVMode)); + m_uv_mode = TRY_READ(m_tree_parser->parse_tree<PredictionMode>(SyntaxElementType::UVMode)); return {}; } @@ -1197,10 +1197,9 @@ DecoderErrorOr<void> Parser::inter_block_mode_info() } auto is_compound = m_ref_frame[1] > IntraFrame; if (seg_feature_active(SEG_LVL_SKIP)) { - m_y_mode = ZeroMv; + m_y_mode = PredictionMode::ZeroMv; } else if (m_mi_size >= Block_8x8) { - auto inter_mode = TRY_READ(m_tree_parser->parse_tree(SyntaxElementType::InterMode)); - m_y_mode = NearestMv + inter_mode; + m_y_mode = TRY_READ(m_tree_parser->parse_tree<PredictionMode>(SyntaxElementType::InterMode)); } if (m_interpolation_filter == Switchable) m_interp_filter = TRY_READ(m_tree_parser->parse_tree<InterpolationFilter>(SyntaxElementType::InterpFilter)); @@ -1211,9 +1210,8 @@ DecoderErrorOr<void> Parser::inter_block_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) { - auto inter_mode = TRY_READ(m_tree_parser->parse_tree(SyntaxElementType::InterMode)); - m_y_mode = NearestMv + inter_mode; - if (m_y_mode == NearestMv || m_y_mode == NearMv) { + m_y_mode = TRY_READ(m_tree_parser->parse_tree<PredictionMode>(SyntaxElementType::InterMode)); + if (m_y_mode == PredictionMode::NearestMv || m_y_mode == PredictionMode::NearMv) { for (auto j = 0; j < 1 + is_compound; j++) append_sub8x8_mvs(idy * 2 + idx, j); } @@ -1273,11 +1271,11 @@ DecoderErrorOr<void> Parser::assign_mv(bool is_compound) { m_mv[1] = {}; for (auto i = 0; i < 1 + is_compound; i++) { - if (m_y_mode == NewMv) { + if (m_y_mode == PredictionMode::NewMv) { TRY(read_mv(i)); - } else if (m_y_mode == NearestMv) { + } else if (m_y_mode == PredictionMode::NearestMv) { m_mv[i] = m_nearest_mv[i]; - } else if (m_y_mode == NearMv) { + } else if (m_y_mode == PredictionMode::NearMv) { m_mv[i] = m_near_mv[i]; } else { m_mv[i] = {}; @@ -1457,9 +1455,9 @@ u32 const* Parser::get_scan(size_t plane, TXSize tx_size, u32 block_index) if (m_lossless || m_is_inter) m_tx_type = DCT_DCT; else - m_tx_type = mode_to_txfm_map[m_mi_size < Block_8x8 ? m_block_sub_modes[block_index] : m_y_mode]; + m_tx_type = mode_to_txfm_map[to_underlying(m_mi_size < Block_8x8 ? m_block_sub_modes[block_index] : m_y_mode)]; } else { - m_tx_type = mode_to_txfm_map[m_y_mode]; + m_tx_type = mode_to_txfm_map[to_underlying(m_y_mode)]; } if (tx_size == TX_4x4) { if (m_tx_type == ADST_DCT) @@ -1613,7 +1611,7 @@ void Parser::find_mv_refs(ReferenceFrameType reference_frame, i32 block) auto candidate_index = get_image_index(candidate.row(), candidate.column()); auto index = get_image_index(candidate.row(), candidate.column()); different_ref_found = true; - context_counter += mode_2_counter[m_y_modes[index]]; + context_counter += mode_2_counter[to_underlying(m_y_modes[index])]; for (auto ref_list = 0u; ref_list < 2; ref_list++) { if (m_ref_frames[candidate_index][ref_list] == reference_frame) { diff --git a/Userland/Libraries/LibVideo/VP9/Parser.h b/Userland/Libraries/LibVideo/VP9/Parser.h index 7d72a24014..e4eec171bc 100644 --- a/Userland/Libraries/LibVideo/VP9/Parser.h +++ b/Userland/Libraries/LibVideo/VP9/Parser.h @@ -236,13 +236,13 @@ private: ReferenceFrameType m_ref_frame[2]; bool m_is_inter { false }; bool m_is_compound { false }; - IntraMode m_default_intra_mode { DcPred }; - u8 m_y_mode { 0 }; - u8 m_block_sub_modes[4]; + PredictionMode m_default_intra_mode { PredictionMode::DcPred }; + PredictionMode m_y_mode { 0 }; + PredictionMode m_block_sub_modes[4]; u8 m_num_4x4_w { 0 }; u8 m_num_4x4_h { 0 }; - u8 m_uv_mode { 0 }; // FIXME: Is u8 the right size? - Vector<Array<IntraMode, 4>> m_sub_modes; + PredictionMode m_uv_mode { 0 }; // FIXME: Is u8 the right size? + Vector<Array<PredictionMode, 4>> m_sub_modes; ReferenceFrameType m_left_ref_frame[2]; ReferenceFrameType m_above_ref_frame[2]; bool m_left_intra { false }; @@ -278,7 +278,7 @@ private: Vector<bool> m_skips; Vector<TXSize> m_tx_sizes; Vector<u32> m_mi_sizes; - Vector<u8> m_y_modes; + Vector<PredictionMode> m_y_modes; Vector<u8> m_segment_ids; Vector<Array<ReferenceFrameType, 2>> m_ref_frames; Vector<Array<ReferenceFrameType, 2>> m_prev_ref_frames; diff --git a/Userland/Libraries/LibVideo/VP9/TreeParser.cpp b/Userland/Libraries/LibVideo/VP9/TreeParser.cpp index 274201a787..c385cc6e89 100644 --- a/Userland/Libraries/LibVideo/VP9/TreeParser.cpp +++ b/Userland/Libraries/LibVideo/VP9/TreeParser.cpp @@ -5,9 +5,10 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "TreeParser.h" +#include "Enums.h" #include "LookupTables.h" #include "Parser.h" +#include "TreeParser.h" namespace Video::VP9 { @@ -34,7 +35,7 @@ template ErrorOr<int> TreeParser::parse_tree(SyntaxElementType); template ErrorOr<bool> TreeParser::parse_tree(SyntaxElementType); template ErrorOr<u8> TreeParser::parse_tree(SyntaxElementType); template ErrorOr<u32> TreeParser::parse_tree(SyntaxElementType); -template ErrorOr<IntraMode> TreeParser::parse_tree(SyntaxElementType); +template ErrorOr<PredictionMode> TreeParser::parse_tree(SyntaxElementType); template ErrorOr<TXSize> TreeParser::parse_tree(SyntaxElementType); template ErrorOr<InterpolationFilter> TreeParser::parse_tree(SyntaxElementType); template ErrorOr<ReferenceMode> TreeParser::parse_tree(SyntaxElementType); @@ -214,21 +215,21 @@ u8 TreeParser::calculate_partition_probability(u8 node) u8 TreeParser::calculate_default_intra_mode_probability(u8 node) { - u32 above_mode, left_mode; + 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] - : DcPred; + : 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] - : DcPred; + : 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] - : DcPred; + : PredictionMode::DcPred; } if (m_idx) { @@ -236,15 +237,15 @@ u8 TreeParser::calculate_default_intra_mode_probability(u8 node) } 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] - : DcPred; + : PredictionMode::DcPred; } } - return m_decoder.m_probability_tables->kf_y_mode_probs()[above_mode][left_mode][node]; + 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()[m_decoder.m_y_mode][node]; + return m_decoder.m_probability_tables->kf_uv_mode_prob()[to_underlying(m_decoder.m_y_mode)][node]; } u8 TreeParser::calculate_intra_mode_probability(u8 node) @@ -261,7 +262,7 @@ u8 TreeParser::calculate_sub_intra_mode_probability(u8 node) u8 TreeParser::calculate_uv_mode_probability(u8 node) { - m_ctx = m_decoder.m_y_mode; + m_ctx = to_underlying(m_decoder.m_y_mode); return m_decoder.m_probability_tables->uv_mode_probs()[m_ctx][node]; } |