LibVideo: Add MotionVector lookup tables as constant expressions

This changes MotionVector by removing the cpp file and moving all
functions to the header, where they are now declared as constexpr
so that they can be compile-time evaluated in LookupTables.h.
This commit is contained in:
Zaggy1024 2022-10-08 22:50:35 -05:00 committed by Andrew Kaster
parent 1dc4652683
commit 6c648329c4
Notes: sideshowbarker 2024-07-17 06:07:55 +09:00
5 changed files with 87 additions and 44 deletions

View file

@ -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

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
* Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
*
* 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
};
}

View file

@ -1,29 +0,0 @@
/*
* Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
*
* 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());
}
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
* Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
*
* 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 };
};
}

View file

@ -1190,7 +1190,7 @@ DecoderErrorOr<void> Parser::read_ref_frames()
DecoderErrorOr<void> 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<void> 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<void> 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 {};
}