aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Nvdec.Vp9/Types/Mv.cs
blob: 9ccb5150393cb9cb5afdb9a9527c315cafd51df3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using Ryujinx.Common.Memory;
using Ryujinx.Graphics.Video;
using System;
using System.Diagnostics;

namespace Ryujinx.Graphics.Nvdec.Vp9.Types
{
    internal struct Mv
    {
        public short Row;
        public short Col;

        private static ReadOnlySpan<byte> LogInBase2 => new byte[]
        {
            0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
            4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
            5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
            6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
            6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7,
            7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
            7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
            7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
            7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
            7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
            8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
            8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
            8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
            8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
            8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
            8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
            8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
            8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
            8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
            8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10,
        };

        public readonly bool UseMvHp()
        {
            const int KMvRefThresh = 64; // Threshold for use of high-precision 1/8 mv
            return Math.Abs(Row) < KMvRefThresh && Math.Abs(Col) < KMvRefThresh;
        }

        public static bool MvJointVertical(MvJointType type)
        {
            return type == MvJointType.MvJointHzvnz || type == MvJointType.MvJointHnzvnz;
        }

        public static bool MvJointHorizontal(MvJointType type)
        {
            return type == MvJointType.MvJointHnzvz || type == MvJointType.MvJointHnzvnz;
        }

        private static int MvClassBase(MvClassType c)
        {
            return c != 0 ? Constants.Class0Size << ((int)c + 2) : 0;
        }

        private static MvClassType GetMvClass(int z, Ptr<int> offset)
        {
            MvClassType c = (z >= Constants.Class0Size * 4096) ? MvClassType.MvClass10 : (MvClassType)LogInBase2[z >> 3];
            if (!offset.IsNull)
            {
                offset.Value = z - MvClassBase(c);
            }

            return c;
        }

        private static void IncMvComponent(int v, ref Vp9BackwardUpdates counts, int comp, int incr, int usehp)
        {
            int s, z, c, o = 0, d, e, f;
            Debug.Assert(v != 0); /* Should not be zero */
            s = v < 0 ? 1 : 0;
            counts.Sign[comp][s] += (uint)incr;
            z = (s != 0 ? -v : v) - 1; /* Magnitude - 1 */

            c = (int)GetMvClass(z, new Ptr<int>(ref o));
            counts.Classes[comp][c] += (uint)incr;

            d = (o >> 3);     /* Int mv data */
            f = (o >> 1) & 3; /* Fractional pel mv data */
            e = (o & 1);      /* High precision mv data */

            if (c == (int)MvClassType.MvClass0)
            {
                counts.Class0[comp][d] += (uint)incr;
                counts.Class0Fp[comp][d][f] += (uint)incr;
                counts.Class0Hp[comp][e] += (uint)(usehp * incr);
            }
            else
            {
                int i;
                int b = c + Constants.Class0Bits - 1; // Number of bits
                for (i = 0; i < b; ++i)
                {
                    counts.Bits[comp][i][((d >> i) & 1)] += (uint)incr;
                }

                counts.Fp[comp][f] += (uint)incr;
                counts.Hp[comp][e] += (uint)(usehp * incr);
            }
        }

        private readonly MvJointType GetMvJoint()
        {
            if (Row == 0)
            {
                return Col == 0 ? MvJointType.MvJointZero : MvJointType.MvJointHnzvz;
            }

            return Col == 0 ? MvJointType.MvJointHzvnz : MvJointType.MvJointHnzvnz;
        }

        internal readonly void IncMv(Ptr<Vp9BackwardUpdates> counts)
        {
            if (!counts.IsNull)
            {
                MvJointType j = GetMvJoint();
                ++counts.Value.Joints[(int)j];

                if (MvJointVertical(j))
                {
                    IncMvComponent(Row, ref counts.Value, 0, 1, 1);
                }

                if (MvJointHorizontal(j))
                {
                    IncMvComponent(Col, ref counts.Value, 1, 1, 1);
                }
            }
        }

        public void ClampMv(int minCol, int maxCol, int minRow, int maxRow)
        {
            Col = (short)Math.Clamp(Col, minCol, maxCol);
            Row = (short)Math.Clamp(Row, minRow, maxRow);
        }

        private const int MvBorder = (16 << 3); // Allow 16 pels in 1/8th pel units

        public void ClampMvRef(ref MacroBlockD xd)
        {
            ClampMv(
                xd.MbToLeftEdge - MvBorder,
                xd.MbToRightEdge + MvBorder,
                xd.MbToTopEdge - MvBorder,
                xd.MbToBottomEdge + MvBorder);
        }

        public void LowerMvPrecision(bool allowHP)
        {
            bool useHP = allowHP && UseMvHp();
            if (!useHP)
            {
                if ((Row & 1) != 0)
                {
                    Row += (short)(Row > 0 ? -1 : 1);
                }

                if ((Col & 1) != 0)
                {
                    Col += (short)(Col > 0 ? -1 : 1);
                }
            }
        }
    }
}