/src/vvenc/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) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC 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 | | /** \file Mv.h |
43 | | \brief motion vector class (header) |
44 | | */ |
45 | | |
46 | | #pragma once |
47 | | |
48 | | #include "CommonDef.h" |
49 | | |
50 | | //! \ingroup CommonLib |
51 | | //! \{ |
52 | | |
53 | | namespace vvenc { |
54 | | |
55 | | // ==================================================================================================================== |
56 | | // Class definition |
57 | | // ==================================================================================================================== |
58 | | class CodingStructure; |
59 | | |
60 | | enum MvPrecision |
61 | | { |
62 | | MV_PRECISION_4PEL = 0, // 4-pel |
63 | | MV_PRECISION_INT = 2, // 1-pel, shift 2 bits from 4-pel |
64 | | MV_PRECISION_HALF = 3, // 1/2-pel |
65 | | MV_PRECISION_QUARTER = 4, // 1/4-pel (the precision of regular MV difference signaling), shift 4 bits from 4-pel |
66 | | MV_PRECISION_SIXTEENTH = 6, // 1/16-pel (the precision of internal MV), shift 6 bits from 4-pel |
67 | | MV_PRECISION_INTERNAL = 2 + MV_FRACTIONAL_BITS_INTERNAL, |
68 | | }; |
69 | | |
70 | | /// basic motion vector class |
71 | | class Mv |
72 | | { |
73 | | private: |
74 | | static const MvPrecision m_amvrPrecision[4]; |
75 | | static const MvPrecision m_amvrPrecAffine[3]; |
76 | | static const MvPrecision m_amvrPrecIbc[3]; |
77 | | |
78 | | static const int mvClipPeriod = (1 << MV_BITS); |
79 | | static const int halMvClipPeriod = (1 << (MV_BITS - 1)); |
80 | | |
81 | | public: |
82 | | int hor; ///< horizontal component of motion vector |
83 | | int ver; ///< vertical component of motion vector |
84 | | |
85 | | // ------------------------------------------------------------------------------------------------------------------ |
86 | | // constructors |
87 | | // ------------------------------------------------------------------------------------------------------------------ |
88 | | |
89 | 0 | Mv( ) : hor( 0 ), ver( 0 ) {} |
90 | 4.60k | Mv( int iHor, int iVer ) : hor( iHor ), ver( iVer ) {} |
91 | | |
92 | | // ------------------------------------------------------------------------------------------------------------------ |
93 | | // set |
94 | | // ------------------------------------------------------------------------------------------------------------------ |
95 | | |
96 | 0 | void set ( int iHor, int iVer) { hor = iHor; ver = iVer; } |
97 | 0 | void setZero () { hor = ver = 0; } |
98 | | |
99 | | // ------------------------------------------------------------------------------------------------------------------ |
100 | | // get |
101 | | // ------------------------------------------------------------------------------------------------------------------ |
102 | | |
103 | 0 | int getAbsHor () const { return abs( hor ); } |
104 | 0 | int getAbsVer () const { return abs( ver ); } |
105 | | |
106 | | // ------------------------------------------------------------------------------------------------------------------ |
107 | | // operations |
108 | | // ------------------------------------------------------------------------------------------------------------------ |
109 | | |
110 | | const Mv& operator += (const Mv& _rcMv) |
111 | 0 | { |
112 | 0 | { |
113 | 0 | Mv rcMv = _rcMv; |
114 | |
|
115 | 0 | hor += rcMv.hor; |
116 | 0 | ver += rcMv.ver; |
117 | 0 | } |
118 | 0 | return *this; |
119 | 0 | } |
120 | | |
121 | | const Mv& operator-= (const Mv& _rcMv) |
122 | 0 | { |
123 | 0 | { |
124 | 0 | Mv rcMv = _rcMv; |
125 | |
|
126 | 0 | hor -= rcMv.hor; |
127 | 0 | ver -= rcMv.ver; |
128 | 0 | } |
129 | 0 | return *this; |
130 | 0 | } |
131 | | |
132 | | |
133 | | //! shift right with rounding |
134 | | void divideByPowerOf2 (const int i) |
135 | 0 | { |
136 | 0 | if (i != 0) |
137 | 0 | { |
138 | 0 | const int offset = (1 << (i - 1)); |
139 | 0 | hor = (hor + offset - (hor >= 0)) >> i; |
140 | 0 | ver = (ver + offset - (ver >= 0)) >> i; |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | | const Mv& operator<<= (const int i) |
145 | 0 | { |
146 | 0 | hor *= 1 << i; |
147 | 0 | ver *= 1 << i; |
148 | 0 | return *this; |
149 | 0 | } |
150 | | |
151 | | const Mv& operator>>= ( const int i ) |
152 | 0 | { |
153 | 0 | if (i != 0) |
154 | 0 | { |
155 | 0 | const int offset = (1 << (i - 1)); |
156 | 0 | hor = (hor + offset - (hor >= 0)) >> i; |
157 | 0 | ver = (ver + offset - (ver >= 0)) >> i; |
158 | 0 | } |
159 | 0 | return *this; |
160 | 0 | } |
161 | | |
162 | | const Mv operator - ( const Mv& rcMv ) const |
163 | 0 | { |
164 | 0 | return Mv( hor - rcMv.hor, ver - rcMv.ver ); |
165 | 0 | } |
166 | | |
167 | | const Mv operator + ( const Mv& rcMv ) const |
168 | 0 | { |
169 | 0 | return Mv( hor + rcMv.hor, ver + rcMv.ver ); |
170 | 0 | } |
171 | | |
172 | | bool operator== ( const Mv& rcMv ) const |
173 | 0 | { |
174 | 0 | return ( hor == rcMv.hor && ver == rcMv.ver ); |
175 | 0 | } |
176 | | |
177 | | bool operator!= ( const Mv& rcMv ) const |
178 | 0 | { |
179 | 0 | return !( *this == rcMv ); |
180 | 0 | } |
181 | | |
182 | | const Mv scaleMv( int iScale ) const |
183 | 0 | { |
184 | 0 | const int mvx = Clip3(MV_MIN, MV_MAX, (iScale * hor + 128 - (iScale * hor >= 0)) >> 8); |
185 | 0 | const int mvy = Clip3(MV_MIN, MV_MAX, (iScale * ver + 128 - (iScale * ver >= 0)) >> 8); |
186 | 0 | return Mv( mvx, mvy ); |
187 | 0 | } |
188 | | |
189 | | void changePrecision(const MvPrecision& src, const MvPrecision& dst) |
190 | 0 | { |
191 | 0 | const int shift = (int)dst - (int)src; |
192 | 0 | if (shift >= 0) |
193 | 0 | { |
194 | 0 | *this <<= shift; |
195 | 0 | } |
196 | 0 | else |
197 | 0 | { |
198 | 0 | const int rightShift = -shift; |
199 | 0 | const int nOffset = 1 << (rightShift - 1); |
200 | 0 | hor = hor >= 0 ? (hor + nOffset - 1) >> rightShift : (hor + nOffset) >> rightShift; |
201 | 0 | ver = ver >= 0 ? (ver + nOffset - 1) >> rightShift : (ver + nOffset) >> rightShift; |
202 | 0 | } |
203 | 0 | } |
204 | | |
205 | | void roundToPrecision(const MvPrecision& src, const MvPrecision& dst) |
206 | 0 | { |
207 | 0 | changePrecision(src, dst); |
208 | 0 | changePrecision(dst, src); |
209 | 0 | } |
210 | | |
211 | | // translational MV |
212 | | void changeTransPrecInternal2Amvr(const int amvr) |
213 | 0 | { |
214 | 0 | changePrecision(MV_PRECISION_INTERNAL, m_amvrPrecision[amvr]); |
215 | 0 | } |
216 | | |
217 | | void changeTransPrecAmvr2Internal(const int amvr) |
218 | 0 | { |
219 | 0 | changePrecision(m_amvrPrecision[amvr], MV_PRECISION_INTERNAL); |
220 | 0 | } |
221 | | |
222 | | void roundTransPrecInternal2Amvr(const int amvr) |
223 | 0 | { |
224 | 0 | roundToPrecision(MV_PRECISION_INTERNAL, m_amvrPrecision[amvr]); |
225 | 0 | } |
226 | | |
227 | | void roundTransPrecInternal2AmvrVertical(const int amvr) |
228 | 0 | { |
229 | 0 | const int shift = MV_PRECISION_INTERNAL - Mv::m_amvrPrecision[amvr]; |
230 | 0 | const int rightShift = shift; |
231 | 0 | const int nOffset = 1 << (rightShift - 1); |
232 | 0 | ver = ver >= 0 ? (ver + nOffset - 1) >> rightShift : (ver + nOffset) >> rightShift; |
233 | 0 | ver = ver << shift; |
234 | 0 | } |
235 | | |
236 | | // affine MV |
237 | | void changeAffinePrecInternal2Amvr(const int amvr) |
238 | 0 | { |
239 | 0 | changePrecision(MV_PRECISION_INTERNAL, m_amvrPrecAffine[amvr]); |
240 | 0 | } |
241 | | |
242 | | void changeAffinePrecAmvr2Internal(const int amvr) |
243 | 0 | { |
244 | 0 | changePrecision(m_amvrPrecAffine[amvr], MV_PRECISION_INTERNAL); |
245 | 0 | } |
246 | | |
247 | | void roundAffinePrecInternal2Amvr(const int amvr) |
248 | 0 | { |
249 | 0 | roundToPrecision(MV_PRECISION_INTERNAL, m_amvrPrecAffine[amvr]); |
250 | 0 | } |
251 | | |
252 | | // IBC block vector |
253 | | void changeIbcPrecInternal2Amvr(const int amvr) |
254 | 0 | { |
255 | 0 | changePrecision(MV_PRECISION_INTERNAL, m_amvrPrecIbc[amvr]); |
256 | 0 | } |
257 | | |
258 | | void changeIbcPrecAmvr2Internal(const int amvr) |
259 | 0 | { |
260 | 0 | changePrecision(m_amvrPrecIbc[amvr], MV_PRECISION_INTERNAL); |
261 | 0 | } |
262 | | |
263 | | void roundIbcPrecInternal2Amvr(const int amvr) |
264 | 0 | { |
265 | 0 | roundToPrecision(MV_PRECISION_INTERNAL, m_amvrPrecIbc[amvr]); |
266 | 0 | } |
267 | | |
268 | | |
269 | | Mv getSymmvdMv(const Mv& curMvPred, const Mv& tarMvPred) |
270 | 0 | { |
271 | 0 | return Mv(tarMvPred.hor - hor + curMvPred.hor, tarMvPred.ver - ver + curMvPred.ver); |
272 | 0 | } |
273 | | |
274 | | void clipToStorageBitDepth() |
275 | 0 | { |
276 | 0 | hor = Clip3( -(1 << 17), (1 << 17) - 1, hor ); |
277 | 0 | ver = Clip3( -(1 << 17), (1 << 17) - 1, ver ); |
278 | 0 | } |
279 | | void mvCliptoStorageBitDepth() // periodic clipping |
280 | 0 | { |
281 | 0 | hor = (hor + mvClipPeriod) & (mvClipPeriod - 1); |
282 | 0 | hor = (hor >= halMvClipPeriod) ? (hor - mvClipPeriod) : hor; |
283 | 0 | ver = (ver + mvClipPeriod) & (mvClipPeriod - 1); |
284 | 0 | ver = (ver >= halMvClipPeriod) ? (ver - mvClipPeriod) : ver; |
285 | 0 | } |
286 | | };// END CLASS DEFINITION MV |
287 | | |
288 | | void clipMv ( Mv& rcMv, const Position& pos, |
289 | | const Size& size, |
290 | | const PreCalcValues& pcv ); |
291 | | void clipMv(Mv& rcMv, const Position& pos, const Size& size, const PreCalcValues& pcv, const PPS& pps,bool m_clipMvInSubPic); |
292 | | |
293 | | bool wrapClipMv( Mv& rcMv, const Position& pos, |
294 | | const Size& size, |
295 | | const CodingStructure& cs ); |
296 | | |
297 | | void roundAffineMv( int& mvx, int& mvy, int nShift ); |
298 | | |
299 | | } // namespace vvenc |
300 | | |
301 | | namespace std |
302 | | { |
303 | | template <> |
304 | | struct hash<vvenc::Mv> |
305 | | { |
306 | | uint64_t operator()(const vvenc::Mv& value) const |
307 | 0 | { |
308 | 0 | return (((uint64_t)value.hor << 32) + value.ver); |
309 | 0 | } |
310 | | }; |
311 | | } |
312 | | |
313 | | //! \} |
314 | | |