| 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() {} |
| 48 | MV(int32_t w) : word(w) {} |
| 49 | MV(int16_t _x, int16_t _y) : x(_x), y(_y) {} |
| 50 | |
| 51 | MV& operator =(uint32_t w) { word = w; return *this; } |
| 52 | |
| 53 | MV& operator +=(const MV& other) { x += other.x; y += other.y; return *this; } |
| 54 | |
| 55 | MV& operator -=(const MV& other) { x -= other.x; y -= other.y; return *this; } |
| 56 | |
| 57 | MV& operator >>=(int i) { x >>= i; y >>= i; return *this; } |
| 58 | |
| 59 | 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 | MV operator -(const MV& other) const { return MV(x - other.x, y - other.y); } |
| 68 | |
| 69 | 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 | bool operator !() const { return !word; } |
| 76 | |
| 77 | // Scale down a QPEL mv to FPEL mv, rounding up by one HPEL offset |
| 78 | MV roundToFPel() const { return MV((x + 2) >> 2, (y + 2) >> 2); } |
| 79 | |
| 80 | // Scale up an FPEL mv to QPEL by shifting up two bits |
| 81 | MV toQPel() const { return *this << 2; } |
| 82 | |
| 83 | bool inline notZero() const { return this->word != 0; } |
| 84 | |
| 85 | bool inline isSubpel() const { return (this->word & 0x00030003) != 0; } |
| 86 | |
| 87 | MV mvmin(const MV& m) const { return MV(x > m.x ? m.x : x, y > m.y ? m.y : y); } |
| 88 | |
| 89 | MV mvmax(const MV& m) const { return MV(x < m.x ? m.x : x, y < m.y ? m.y : y); } |
| 90 | |
| 91 | MV clipped(const MV& _min, const MV& _max) const |
| 92 | { |
| 93 | MV cl = mvmin(_max); |
| 94 | |
| 95 | return cl.mvmax(_min); |
| 96 | } |
| 97 | |
| 98 | // returns true if MV is within range (inclusive) |
| 99 | bool checkRange(const MV& _min, const MV& _max) const |
| 100 | { |
| 101 | return x >= _min.x && x <= _max.x && y >= _min.y && y <= _max.y; |
| 102 | } |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | #endif // ifndef X265_MV_H |