Imported Upstream version 1.4
[deb_x265.git] / source / common / mv.h
1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
3 *
4 * Authors: Steve Borho <steve@borho.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
23
24 #ifndef X265_MV_H
25 #define X265_MV_H
26
27 #include "common.h"
28 #include "primitives.h"
29
30 namespace x265 {
31 // private x265 namespace
32
33 #if _MSC_VER
34 #pragma warning(disable: 4201) // non-standard extension used (nameless struct/union)
35 #endif
36
37 struct MV
38 {
39 public:
40
41 union {
42 struct { int16_t x, y; };
43
44 int32_t word;
45 };
46
47 MV() : word(0) {}
48
49 MV(int16_t _x, int16_t _y) : x(_x), y(_y) {}
50
51 const MV& operator =(uint32_t w) { word = w; return *this; }
52
53 const MV& operator +=(const MV& other) { x += other.x; y += other.y; return *this; }
54
55 const MV& operator -=(const MV& other) { x -= other.x; y -= other.y; return *this; }
56
57 const MV& operator >>=(int i) { x >>= i; y >>= i; return *this; }
58
59 const MV& operator <<=(int i) { x <<= i; y <<= i; return *this; }
60
61 MV operator >>(int i) const { return MV(x >> i, y >> i); }
62
63 MV operator <<(int i) const { return MV(x << i, y << i); }
64
65 MV operator *(int16_t i) const { return MV(x * i, y * i); }
66
67 const MV operator -(const MV& other) const { return MV(x - other.x, y - other.y); }
68
69 const MV operator +(const MV& other) const { return MV(x + other.x, y + other.y); }
70
71 bool operator ==(const MV& other) const { return word == other.word; }
72
73 bool operator !=(const MV& other) const { return word != other.word; }
74
75 // Scale down a QPEL mv to FPEL mv, rounding up by one HPEL offset
76 MV roundToFPel() const { return MV(x + 2, y + 2) >> 2; }
77
78 // Scale up an FPEL mv to QPEL by shifting up two bits
79 MV toQPel() const { return *this << 2; }
80
81 bool inline notZero() const { return this->word != 0; }
82
83 bool inline isSubpel() const { return (this->word & 0x00030003) != 0; }
84
85 MV mvmin(const MV& m) const { return MV(x > m.x ? m.x : x, y > m.y ? m.y : y); }
86
87 MV mvmax(const MV& m) const { return MV(x < m.x ? m.x : x, y < m.y ? m.y : y); }
88
89 MV clipped(const MV& _min, const MV& _max) const
90 {
91 MV cl = mvmin(_max);
92
93 return cl.mvmax(_min);
94 }
95
96 // returns true if MV is within range (inclusive)
97 bool checkRange(const MV& _min, const MV& _max) const
98 {
99 return x >= _min.x && x <= _max.x && y >= _min.y && y <= _max.y;
100 }
101 };
102 }
103
104 #endif // ifndef X265_MV_H