diff --git a/Userland/Libraries/LibVideo/CMakeLists.txt b/Userland/Libraries/LibVideo/CMakeLists.txt index 0d18a82a277..5da617f9546 100644 --- a/Userland/Libraries/LibVideo/CMakeLists.txt +++ b/Userland/Libraries/LibVideo/CMakeLists.txt @@ -2,7 +2,6 @@ set(SOURCES MatroskaReader.cpp VP9/BitStream.cpp VP9/Decoder.cpp - VP9/MotionVector.cpp VP9/Parser.cpp VP9/ProbabilityTables.cpp VP9/SyntaxElementCounter.cpp diff --git a/Userland/Libraries/LibVideo/VP9/LookupTables.h b/Userland/Libraries/LibVideo/VP9/LookupTables.h index 9dc6966f2e4..3d384e5f55d 100644 --- a/Userland/Libraries/LibVideo/VP9/LookupTables.h +++ b/Userland/Libraries/LibVideo/VP9/LookupTables.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Hunter Salyer + * Copyright (c) 2022, Gregory Bertilson * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,6 +8,7 @@ #pragma once #include "Enums.h" +#include "MotionVector.h" #include "Symbols.h" namespace Video::VP9 { @@ -289,4 +291,44 @@ static constexpr u8 cat_probs[7][14] = { { 254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129 } }; +static constexpr MotionVector mv_ref_blocks[BLOCK_SIZES][MVREF_NEIGHBOURS] = { + { { -1, 0 }, { 0, -1 }, { -1, -1 }, { -2, 0 }, { 0, -2 }, { -2, -1 }, { -1, -2 }, { -2, -2 } }, + { { -1, 0 }, { 0, -1 }, { -1, -1 }, { -2, 0 }, { 0, -2 }, { -2, -1 }, { -1, -2 }, { -2, -2 } }, + { { -1, 0 }, { 0, -1 }, { -1, -1 }, { -2, 0 }, { 0, -2 }, { -2, -1 }, { -1, -2 }, { -2, -2 } }, + { { -1, 0 }, { 0, -1 }, { -1, -1 }, { -2, 0 }, { 0, -2 }, { -2, -1 }, { -1, -2 }, { -2, -2 } }, + { { 0, -1 }, { -1, 0 }, { 1, -1 }, { -1, -1 }, { 0, -2 }, { -2, 0 }, { -2, -1 }, { -1, -2 } }, + { { -1, 0 }, { 0, -1 }, { -1, 1 }, { -1, -1 }, { -2, 0 }, { 0, -2 }, { -1, -2 }, { -2, -1 } }, + { { -1, 0 }, { 0, -1 }, { -1, 1 }, { 1, -1 }, { -1, -1 }, { -3, 0 }, { 0, -3 }, { -3, -3 } }, + { { 0, -1 }, { -1, 0 }, { 2, -1 }, { -1, -1 }, { -1, 1 }, { 0, -3 }, { -3, 0 }, { -3, -3 } }, + { { -1, 0 }, { 0, -1 }, { -1, 2 }, { -1, -1 }, { 1, -1 }, { -3, 0 }, { 0, -3 }, { -3, -3 } }, + { { -1, 1 }, { 1, -1 }, { -1, 2 }, { 2, -1 }, { -1, -1 }, { -3, 0 }, { 0, -3 }, { -3, -3 } }, + { { 0, -1 }, { -1, 0 }, { 4, -1 }, { -1, 2 }, { -1, -1 }, { 0, -3 }, { -3, 0 }, { 2, -1 } }, + { { -1, 0 }, { 0, -1 }, { -1, 4 }, { 2, -1 }, { -1, -1 }, { -3, 0 }, { 0, -3 }, { -1, 2 } }, + { { -1, 3 }, { 3, -1 }, { -1, 4 }, { 4, -1 }, { -1, -1 }, { -1, 0 }, { 0, -1 }, { -1, 6 } } +}; + +static constexpr u8 mode_2_counter[MB_MODE_COUNT] = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 0, 3, 1 }; + +static constexpr u8 counter_to_context[19] = { + BOTH_PREDICTED, + NEW_PLUS_NON_INTRA, + BOTH_NEW, + ZERO_PLUS_PREDICTED, + NEW_PLUS_NON_INTRA, + INVALID_CASE, + BOTH_ZERO, + INVALID_CASE, + INVALID_CASE, + INTRA_PLUS_NON_INTRA, + INTRA_PLUS_NON_INTRA, + INVALID_CASE, + INTRA_PLUS_NON_INTRA, + INVALID_CASE, + INVALID_CASE, + INVALID_CASE, + INVALID_CASE, + INVALID_CASE, + BOTH_INTRA +}; + } diff --git a/Userland/Libraries/LibVideo/VP9/MotionVector.cpp b/Userland/Libraries/LibVideo/VP9/MotionVector.cpp deleted file mode 100644 index e4e7b21698a..00000000000 --- a/Userland/Libraries/LibVideo/VP9/MotionVector.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2021, Hunter Salyer - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include "MotionVector.h" - -namespace Video::VP9 { - -MotionVector::MotionVector(u32 row, u32 col) - : m_row(row) - , m_col(col) -{ -} - -MotionVector& MotionVector::operator=(i32 value) -{ - m_row = value; - m_col = value; - return *this; -} - -MotionVector MotionVector::operator+(MotionVector const& other) const -{ - return MotionVector(this->row() + other.row(), this->col() + other.col()); -} - -} diff --git a/Userland/Libraries/LibVideo/VP9/MotionVector.h b/Userland/Libraries/LibVideo/VP9/MotionVector.h index 6c13300dd72..dac793aa4dc 100644 --- a/Userland/Libraries/LibVideo/VP9/MotionVector.h +++ b/Userland/Libraries/LibVideo/VP9/MotionVector.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Hunter Salyer + * Copyright (c) 2022, Gregory Bertilson * * SPDX-License-Identifier: BSD-2-Clause */ @@ -10,22 +11,52 @@ namespace Video::VP9 { -class MotionVector { +struct MotionVector { public: - MotionVector() = default; - MotionVector(u32 row, u32 col); + constexpr MotionVector() = default; + constexpr MotionVector(MotionVector const& other) = default; + constexpr MotionVector(i32 row, i32 col) + : m_row(row) + , m_column(col) + { + } - u32 row() const { return m_row; } - void set_row(u32 row) { m_row = row; } - u32 col() const { return m_col; } - void set_col(u32 col) { m_col = col; } + constexpr MotionVector& operator=(MotionVector const& other) = default; + constexpr MotionVector& operator=(MotionVector&& other) = default; - MotionVector& operator=(i32 value); - MotionVector operator+(MotionVector const& other) const; + constexpr i32 row() const { return m_row; } + constexpr void set_row(i32 row) { m_row = row; } + constexpr i32 column() const { return m_column; } + constexpr void set_column(i32 col) { m_column = col; } + + constexpr MotionVector operator+(MotionVector const& other) const + { + return MotionVector(this->row() + other.row(), this->column() + other.column()); + } + constexpr MotionVector& operator+=(MotionVector const& other) + { + *this = *this + other; + return *this; + } + + constexpr MotionVector operator*(i32 scalar) const + { + return MotionVector(this->row() * scalar, this->column() * scalar); + } + constexpr MotionVector& operator*=(i32 scalar) + { + *this = *this * scalar; + return *this; + } + + constexpr bool operator==(MotionVector const& other) const + { + return this->row() == other.row() && this->column() == other.column(); + } private: - u32 m_row { 0 }; - u32 m_col { 0 }; + i32 m_row { 0 }; + i32 m_column { 0 }; }; } diff --git a/Userland/Libraries/LibVideo/VP9/Parser.cpp b/Userland/Libraries/LibVideo/VP9/Parser.cpp index fb02e14ffed..ff22ce5d604 100644 --- a/Userland/Libraries/LibVideo/VP9/Parser.cpp +++ b/Userland/Libraries/LibVideo/VP9/Parser.cpp @@ -1190,7 +1190,7 @@ DecoderErrorOr Parser::read_ref_frames() DecoderErrorOr Parser::assign_mv(bool is_compound) { - m_mv[1] = 0; + m_mv[1] = {}; for (auto i = 0; i < 1 + is_compound; i++) { if (m_y_mode == NewMv) { TRY(read_mv(i)); @@ -1199,7 +1199,7 @@ DecoderErrorOr Parser::assign_mv(bool is_compound) } else if (m_y_mode == NearMv) { m_mv[i] = m_near_mv[i]; } else { - m_mv[i] = 0; + m_mv[i] = {}; } } return {}; @@ -1213,7 +1213,7 @@ DecoderErrorOr Parser::read_mv(u8 ref) if (mv_joint == MvJointHzvnz || mv_joint == MvJointHnzvnz) diff_mv.set_row(TRY(read_mv_component(0))); if (mv_joint == MvJointHnzvz || mv_joint == MvJointHnzvnz) - diff_mv.set_col(TRY(read_mv_component(1))); + diff_mv.set_column(TRY(read_mv_component(1))); m_mv[ref] = m_best_mv[ref] + diff_mv; return {}; }