/src/vvdec/source/Lib/CommonLib/Mv.h
Line | Count | Source |
1 | | /* ----------------------------------------------------------------------------- |
2 | | The copyright in this software is being made available under the Clear BSD |
3 | | License, included below. No patent rights, trademark rights and/or |
4 | | other Intellectual Property Rights other than the copyrights concerning |
5 | | the Software are granted under this license. |
6 | | |
7 | | The Clear BSD License |
8 | | |
9 | | Copyright (c) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC Authors. |
10 | | All rights reserved. |
11 | | |
12 | | Redistribution and use in source and binary forms, with or without modification, |
13 | | are permitted (subject to the limitations in the disclaimer below) provided that |
14 | | the following conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright notice, |
17 | | this list of conditions and the following disclaimer. |
18 | | |
19 | | * Redistributions in binary form must reproduce the above copyright |
20 | | notice, this list of conditions and the following disclaimer in the |
21 | | documentation and/or other materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the copyright holder nor the names of its |
24 | | contributors may be used to endorse or promote products derived from this |
25 | | software without specific prior written permission. |
26 | | |
27 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
28 | | THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
29 | | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
31 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
32 | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
33 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
34 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
35 | | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
36 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
37 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38 | | POSSIBILITY OF SUCH DAMAGE. |
39 | | |
40 | | |
41 | | ------------------------------------------------------------------------------------------- */ |
42 | | |
43 | | /** \file Mv.h |
44 | | \brief motion vector class (header) |
45 | | */ |
46 | | |
47 | | #pragma once |
48 | | |
49 | | #include "CommonDef.h" |
50 | | |
51 | | namespace vvdec |
52 | | { |
53 | | |
54 | | class SPS; |
55 | | class PPS; |
56 | | struct Position; |
57 | | struct Size; |
58 | | |
59 | | //! \ingroup CommonLib |
60 | | //! \{ |
61 | | |
62 | | // ==================================================================================================================== |
63 | | // Class definition |
64 | | // ==================================================================================================================== |
65 | | |
66 | | enum MvPrecision |
67 | | { |
68 | | MV_PRECISION_4PEL = 0, // 4-pel |
69 | | MV_PRECISION_INT = 2, // 1-pel, shift 2 bits from 4-pel |
70 | | MV_PRECISION_HALF = 3, // 1/2-pel |
71 | | MV_PRECISION_QUARTER = 4, // 1/4-pel (the precision of regular MV difference signaling), shift 4 bits from 4-pel |
72 | | MV_PRECISION_INTERNAL = 6, // 1/16-pel (the precision of internal MV), shift 6 bits from 4-pel |
73 | | }; |
74 | | |
75 | | /// basic motion vector class |
76 | | class Mv |
77 | | { |
78 | | private: |
79 | | static const MvPrecision m_amvrPrecision[4]; |
80 | | static const int mvClipPeriod = (1 << MV_BITS); |
81 | | static const int halMvClipPeriod = (1 << (MV_BITS - 1)); |
82 | | |
83 | | public: |
84 | | int hor; ///< horizontal component of motion vector |
85 | | int ver; ///< vertical component of motion vector |
86 | | |
87 | | // ------------------------------------------------------------------------------------------------------------------ |
88 | | // constructors |
89 | | // ------------------------------------------------------------------------------------------------------------------ |
90 | | |
91 | 0 | Mv( ) : hor( 0 ), ver( 0 ) {} |
92 | 0 | Mv( int iHor, int iVer ) : hor( iHor ), ver( iVer ) {} |
93 | | |
94 | | // ------------------------------------------------------------------------------------------------------------------ |
95 | | // set |
96 | | // ------------------------------------------------------------------------------------------------------------------ |
97 | | |
98 | 0 | void set ( int iHor, int iVer) { hor = iHor; ver = iVer; } |
99 | 0 | void setHor ( int i ) { hor = i; } |
100 | 0 | void setVer ( int i ) { ver = i; } |
101 | 0 | void setZero () { hor = ver = 0; } |
102 | | |
103 | | // ------------------------------------------------------------------------------------------------------------------ |
104 | | // get |
105 | | // ------------------------------------------------------------------------------------------------------------------ |
106 | | |
107 | 0 | int getHor () const { return hor; } |
108 | 0 | int getVer () const { return ver; } |
109 | 0 | int getAbsHor () const { return abs( hor ); } |
110 | 0 | int getAbsVer () const { return abs( ver ); } |
111 | | |
112 | | // ------------------------------------------------------------------------------------------------------------------ |
113 | | // operations |
114 | | // ------------------------------------------------------------------------------------------------------------------ |
115 | | |
116 | | const Mv& operator += ( const Mv& _rcMv ) |
117 | 0 | { |
118 | 0 | hor += _rcMv.hor; |
119 | 0 | ver += _rcMv.ver; |
120 | |
|
121 | 0 | return *this; |
122 | 0 | } |
123 | | |
124 | | const Mv& operator-= (const Mv& _rcMv) |
125 | 0 | { |
126 | 0 | hor -= _rcMv.hor; |
127 | 0 | ver -= _rcMv.ver; |
128 | 0 |
|
129 | 0 | return *this; |
130 | 0 | } |
131 | | |
132 | | const Mv& operator<<= (const int i) |
133 | 0 | { |
134 | 0 | hor *= 1<<i; |
135 | 0 | ver *= 1<<i; |
136 | 0 | return *this; |
137 | 0 | } |
138 | | |
139 | | const Mv& operator>>= ( const int i ) |
140 | 0 | { |
141 | 0 | if (i != 0) |
142 | 0 | { |
143 | 0 | const int offset = (1 << (i - 1)); |
144 | 0 | hor = (hor + offset - (hor >= 0)) >> i; |
145 | 0 | ver = (ver + offset - (ver >= 0)) >> i; |
146 | 0 | } |
147 | 0 | return *this; |
148 | 0 | } |
149 | | |
150 | | const Mv operator - ( const Mv& rcMv ) const |
151 | 0 | { |
152 | 0 | return Mv( hor - rcMv.hor, ver - rcMv.ver ); |
153 | 0 | } |
154 | | |
155 | | const Mv operator + ( const Mv& rcMv ) const |
156 | 0 | { |
157 | 0 | return Mv( hor + rcMv.hor, ver + rcMv.ver ); |
158 | 0 | } |
159 | | |
160 | | bool operator== ( const Mv& rcMv ) const |
161 | 0 | { |
162 | 0 | return ( hor == rcMv.hor && ver == rcMv.ver ); |
163 | 0 | } |
164 | | |
165 | | bool operator!= ( const Mv& rcMv ) const |
166 | 0 | { |
167 | 0 | return !( *this == rcMv ); |
168 | 0 | } |
169 | | |
170 | | const Mv scaleMv( int iScale ) const |
171 | 0 | { |
172 | 0 | const int mvx = Clip3( -131072, 131071, (iScale * getHor() + 128 - (iScale * getHor() >= 0)) >> 8); |
173 | 0 | const int mvy = Clip3( -131072, 131071, (iScale * getVer() + 128 - (iScale * getVer() >= 0)) >> 8); |
174 | 0 | return Mv( mvx, mvy ); |
175 | 0 | } |
176 | | |
177 | | void changePrecision(const MvPrecision& src, const MvPrecision& dst) |
178 | 0 | { |
179 | 0 | const int shift = (int)dst - (int)src; |
180 | 0 | if (shift >= 0) |
181 | 0 | { |
182 | 0 | *this <<= shift; |
183 | 0 | } |
184 | 0 | else |
185 | 0 | { |
186 | 0 | const int rightShift = -shift; |
187 | 0 | const int nOffset = 1 << (rightShift - 1); |
188 | 0 | hor = hor >= 0 ? (hor + nOffset - 1) >> rightShift : (hor + nOffset) >> rightShift; |
189 | 0 | ver = ver >= 0 ? (ver + nOffset - 1) >> rightShift : (ver + nOffset) >> rightShift; |
190 | 0 | } |
191 | 0 | } |
192 | | |
193 | | void changePrecisionAmvr(const int amvr, const MvPrecision& dst) |
194 | 0 | { |
195 | 0 | changePrecision(m_amvrPrecision[amvr], dst); |
196 | 0 | } |
197 | | |
198 | | void roundToPrecision(const MvPrecision& src, const MvPrecision& dst) |
199 | 0 | { |
200 | 0 | changePrecision(src, dst); |
201 | 0 | changePrecision(dst, src); |
202 | 0 | } |
203 | | |
204 | | void roundToAmvrSignalPrecision(const MvPrecision& src, const int amvr) |
205 | 0 | { |
206 | 0 | roundToPrecision(src, m_amvrPrecision[amvr]); |
207 | 0 | } |
208 | | |
209 | | Mv getSymmvdMv(const Mv& curMvPred, const Mv& tarMvPred) |
210 | 0 | { |
211 | 0 | return Mv(tarMvPred.hor - hor + curMvPred.hor, tarMvPred.ver - ver + curMvPred.ver); |
212 | 0 | } |
213 | | |
214 | | void clipToStorageBitDepth() |
215 | 0 | { |
216 | 0 | hor = Clip3( -( 1 << 17 ), ( 1 << 17 ) - 1, hor ); |
217 | 0 | ver = Clip3( -( 1 << 17 ), ( 1 << 17 ) - 1, ver ); |
218 | 0 | } |
219 | | |
220 | | void mvCliptoStorageBitDepth() // periodic clipping |
221 | 0 | { |
222 | 0 | hor = (hor + mvClipPeriod) & (mvClipPeriod - 1); |
223 | 0 | hor = (hor >= halMvClipPeriod) ? (hor - mvClipPeriod) : hor; |
224 | 0 | ver = (ver + mvClipPeriod) & (mvClipPeriod - 1); |
225 | 0 | ver = (ver >= halMvClipPeriod) ? (ver - mvClipPeriod) : ver; |
226 | 0 | } |
227 | | };// END CLASS DEFINITION MV |
228 | | |
229 | | extern void(*clipMv) ( Mv& rcMv, const Position& pos, const struct Size& size, const SPS& sps, const PPS& pps ); |
230 | | void clipMvInPic ( Mv& rcMv, const Position& pos, const struct Size& size, const SPS& sps, const PPS& pps ); |
231 | | void clipMvInSubpic ( Mv& rcMv, const Position& pos, const struct Size& size, const SPS& sps, const PPS& pps ); |
232 | | bool wrapClipMv ( Mv& rcMv, const Position& pos, const struct Size& size, const SPS& sps, const PPS& pps ); |
233 | | |
234 | | void roundAffineMv( int& mvx, int& mvy, int nShift ); |
235 | | |
236 | | } |