Newer
Older

Karsten Suehring
committed
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
/* The copyright in this software is being made available under the BSD
* License, included below. This software may be subject to other third party
* and contributor rights, including patent rights, and no such rights are
* granted under this license.
*
* Copyright (c) 2010-2018, ITU/ISO/IEC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/** \file Mv.h
\brief motion vector class (header)
*/
#ifndef __MV__
#define __MV__
#include "CommonDef.h"
//! \ingroup CommonLib
//! \{
// ====================================================================================================================
// Class definition
// ====================================================================================================================
/// basic motion vector class
class Mv
{
public:
int hor; ///< horizontal component of motion vector
int ver; ///< vertical component of motion vector

Karsten Suehring
committed
bool highPrec;///< true if the vector is high precision
#endif
// ------------------------------------------------------------------------------------------------------------------
// constructors
// ------------------------------------------------------------------------------------------------------------------

Karsten Suehring
committed
Mv( ) : hor( 0 ), ver( 0 ), highPrec( false ) {}
Mv( int iHor, int iVer, bool _highPrec = false ) : hor( iHor ), ver( iVer ), highPrec( _highPrec ) {}
#else
Mv( ) : hor( 0 ), ver( 0 ) {}
Mv( int iHor, int iVer ) : hor( iHor ), ver( iVer ) {}
#endif
// ------------------------------------------------------------------------------------------------------------------
// set
// ------------------------------------------------------------------------------------------------------------------
void set ( int iHor, int iVer) { hor = iHor; ver = iVer; }
void setHor ( int i ) { hor = i; }
void setVer ( int i ) { ver = i; }
void setZero () { hor = ver = 0; }
// ------------------------------------------------------------------------------------------------------------------
// get
// ------------------------------------------------------------------------------------------------------------------
int getHor () const { return hor; }
int getVer () const { return ver; }
int getAbsHor () const { return abs( hor ); }
int getAbsVer () const { return abs( ver ); }
// ------------------------------------------------------------------------------------------------------------------
// operations
// ------------------------------------------------------------------------------------------------------------------
const Mv& operator += (const Mv& _rcMv)
{

Karsten Suehring
committed
if( highPrec == _rcMv.highPrec )
{
hor += _rcMv.hor;
ver += _rcMv.ver;
}
else
#endif
{
Mv rcMv = _rcMv;

Karsten Suehring
committed
if( highPrec && !rcMv.highPrec ) rcMv.setHighPrec();
if( !highPrec && rcMv.highPrec ) setHighPrec();
#endif
hor += rcMv.hor;
ver += rcMv.ver;
}
return *this;
}
const Mv& operator-= (const Mv& _rcMv)
{

Karsten Suehring
committed
if( highPrec == _rcMv.highPrec )
{
hor -= _rcMv.hor;
ver -= _rcMv.ver;
}
else
#endif
{
Mv rcMv = _rcMv;

Karsten Suehring
committed
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
if( highPrec && !rcMv.highPrec ) rcMv.setHighPrec();
if( !highPrec && rcMv.highPrec ) setHighPrec();
#endif
hor -= rcMv.hor;
ver -= rcMv.ver;
}
return *this;
}
//! shift right with rounding
void divideByPowerOf2 (const int i)
{
#if ME_ENABLE_ROUNDING_OF_MVS
const int offset = (i == 0) ? 0 : 1 << (i - 1);
hor += offset;
ver += offset;
#endif
hor >>= i;
ver >>= i;
}
const Mv& operator<<= (const int i)
{
hor <<= i;
ver <<= i;
return *this;
}
const Mv& operator>>= ( const int i )
{
hor >>= i;
ver >>= i;
return *this;
}
const Mv operator - ( const Mv& rcMv ) const
{

Karsten Suehring
committed
if( rcMv.highPrec == highPrec )
{
return Mv( hor - rcMv.hor, ver - rcMv.ver, highPrec );
}
else
{
Mv self = *this; self.setHighPrec();
Mv other = rcMv; other.setHighPrec();
return self - other;
}
#else
return Mv( hor - rcMv.hor, ver - rcMv.ver );
#endif
}
const Mv operator + ( const Mv& rcMv ) const
{

Karsten Suehring
committed
if( rcMv.highPrec == highPrec )
{
return Mv( hor + rcMv.hor, ver + rcMv.ver, highPrec );
}
else
{
Mv self = *this; self.setHighPrec();
Mv other = rcMv; other.setHighPrec();
return self + other;
}
#else
return Mv( hor + rcMv.hor, ver + rcMv.ver );
#endif
}
bool operator== ( const Mv& rcMv ) const
{

Karsten Suehring
committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
if( rcMv.highPrec == highPrec )
{
return ( hor == rcMv.hor && ver == rcMv.ver );
}
else if( rcMv.highPrec )
{
return ( ( hor << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE ) == rcMv.hor && ( ver << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE ) == rcMv.ver );
}
else
{
return ( ( rcMv.hor << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE ) == hor && ( rcMv.ver << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE ) == ver );
}
#else
return ( hor == rcMv.hor && ver == rcMv.ver );
#endif
}
bool operator!= ( const Mv& rcMv ) const
{
return !( *this == rcMv );
}
const Mv scaleMv( int iScale ) const
{
const int mvx = Clip3( -32768, 32767, (iScale * getHor() + 127 + (iScale * getHor() < 0)) >> 8 );
const int mvy = Clip3( -32768, 32767, (iScale * getVer() + 127 + (iScale * getVer() < 0)) >> 8 );

Karsten Suehring
committed
return Mv( mvx, mvy, highPrec );
#else
return Mv( mvx, mvy );
#endif
}
void roundMV2SignalPrecision()
{
#if REMOVE_MV_ADAPT_PREC
const int nShift = VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE;
const int nOffset = 1 << (nShift - 1);
hor = hor >= 0 ? (hor + nOffset) >> nShift : -((-hor + nOffset) >> nShift);
ver = ver >= 0 ? (ver + nOffset) >> nShift : -((-ver + nOffset) >> nShift);
hor = hor >= 0 ? (hor) << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE : -((-hor) << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE);
ver = ver >= 0 ? (ver) << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE : -((-ver) << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE);
#else

Karsten Suehring
committed
const bool isHP = highPrec;
setLowPrec();
if( isHP ) setHighPrec();

Karsten Suehring
committed
}

Karsten Suehring
committed
void setLowPrec()
{
if( !highPrec ) return;
const int nShift = VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE;
const int nOffset = 1 << ( nShift - 1 );
hor = hor >= 0 ? ( hor + nOffset ) >> nShift : -( ( -hor + nOffset ) >> nShift );
ver = ver >= 0 ? ( ver + nOffset ) >> nShift : -( ( -ver + nOffset ) >> nShift );
highPrec = false;
}
void setHighPrec()
{
if( highPrec ) return;
hor = hor >= 0 ? ( hor ) << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE : -( ( -hor ) << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE );
ver = ver >= 0 ? ( ver ) << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE : -( ( -ver ) << VCEG_AZ07_MV_ADD_PRECISION_BIT_FOR_STORE );
highPrec = true;
}
#endif
};// END CLASS DEFINITION MV
#if JVET_L0293_CPR
namespace std
{
template <>
struct hash<Mv> : public unary_function<Mv, uint64_t>
{
uint64_t operator()(const Mv& value) const
{
return (((uint64_t)value.hor << 32) + value.ver);
}
};
};
#endif

Karsten Suehring
committed
void roundMV( Mv& rcMv, unsigned imvShift );
void clipMv ( Mv& rcMv, const struct Position& pos, const class SPS& sps );
void roundAffineMv( int& mvx, int& mvy, int nShift );
//! \}
#endif // __MV__