/src/vvenc/source/Lib/CommonLib/Common.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 Common.h |
43 | | * \brief Common 2D-geometrical structures |
44 | | */ |
45 | | |
46 | | #pragma once |
47 | | |
48 | | #include "CommonDef.h" |
49 | | |
50 | | //! \ingroup CommonLib |
51 | | //! \{ |
52 | | |
53 | | namespace vvenc { |
54 | | |
55 | | typedef int PosType; |
56 | | typedef uint32_t SizeType; |
57 | | struct Position |
58 | | { |
59 | | PosType x; |
60 | | PosType y; |
61 | | |
62 | 0 | Position() : x(0), y(0) { } |
63 | 0 | Position(const PosType _x, const PosType _y) : x(_x), y(_y) { } |
64 | | |
65 | 0 | bool operator!=(const Position& other) const { return x != other.x || y != other.y; } |
66 | 0 | bool operator==(const Position& other) const { return x == other.x && y == other.y; } |
67 | | |
68 | 0 | Position offset(const Position pos) const { return Position(x + pos.x, y + pos.y); } |
69 | 0 | Position offset(const PosType _x, const PosType _y) const { return Position(x + _x , y + _y ); } |
70 | 0 | void repositionTo(const Position newPos) { x = newPos.x; y = newPos.y; } |
71 | 0 | void relativeTo (const Position origin) { x -= origin.x; y -= origin.y; } |
72 | | |
73 | 0 | Position operator-( const Position& other ) const { return{ x - other.x, y - other.y }; } |
74 | | }; |
75 | | |
76 | | struct Size |
77 | | { |
78 | | SizeType width; |
79 | | SizeType height; |
80 | | |
81 | 0 | Size() : width(0), height(0) { } |
82 | 0 | Size(const SizeType _width, const SizeType _height) : width(_width), height(_height) { } |
83 | | |
84 | 0 | bool operator!=(const Size& other) const { return (width != other.width) || (height != other.height); } |
85 | 0 | bool operator==(const Size& other) const { return (width == other.width) && (height == other.height); } |
86 | 0 | uint32_t area() const { return (uint32_t) width * (uint32_t) height; } |
87 | 0 | void clipSize( int clipW, int clipH ) { width = width > clipW ? clipW: width; |
88 | 0 | height = height > clipH ? clipH: height; } |
89 | 0 | SizeType maxDim() const { return std::max( width, height ); } |
90 | 0 | SizeType minDim() const { return std::min( width, height ); } |
91 | | }; |
92 | | |
93 | | struct Area : public Position, public Size |
94 | | { |
95 | 0 | Area() : Position(), Size() { } |
96 | 0 | Area(const Position& _pos, const Size& _size) : Position(_pos), Size(_size) { } |
97 | 0 | Area(const PosType _x, const PosType _y, const SizeType _w, const SizeType _h) : Position(_x, _y), Size(_w, _h) { } |
98 | | |
99 | 0 | Position& pos() { return *this; } |
100 | 0 | const Position& pos() const { return *this; } |
101 | 0 | Size& size() { return *this; } |
102 | 0 | const Size& size() const { return *this; } |
103 | | |
104 | 0 | const Position& topLeft() const { return *this; } |
105 | 0 | Position topRight() const { return { (PosType) (x + width - 1), y }; } |
106 | 0 | Position bottomLeft() const { return { x , (PosType) (y + height - 1) }; } |
107 | 0 | Position bottomRight() const { return { (PosType) (x + width - 1), (PosType) (y + height - 1) }; } |
108 | 0 | Position center() const { return { (PosType) (x + width / 2), (PosType) (y + height / 2) }; } |
109 | | |
110 | 0 | bool contains(const Position& _pos) const { return ((unsigned)(_pos.x-x) < (unsigned)width) && ((unsigned)(_pos.y-y) < (unsigned)height); } |
111 | 0 | bool contains(const Area& _area) const { return contains(_area.pos()) && contains(_area.bottomRight()); } |
112 | | |
113 | 0 | bool operator!=(const Area& other) const { return (Size::operator!=(other)) || (Position::operator!=(other)); } |
114 | 0 | bool operator==(const Area& other) const { return (Size::operator==(other)) && (Position::operator==(other)); } |
115 | | }; |
116 | | |
117 | | struct UnitScale |
118 | | { |
119 | | enum ScaliningType |
120 | | { |
121 | | UNIT_MAP, |
122 | | LF_PARAM_MAP, |
123 | | MI_MAP |
124 | | }; |
125 | | |
126 | 0 | UnitScale() : posx( 0), posy( 0), area(posx+posy) {} |
127 | 256 | UnitScale( int sx, int sy ) : posx(sx), posy(sy), area(posx+posy) {} |
128 | | int posx; |
129 | | int posy; |
130 | | int area; |
131 | | |
132 | 0 | template<typename T> T scaleHor ( const T &in ) const { return in >> posx; }Unexecuted instantiation: unsigned int vvenc::UnitScale::scaleHor<unsigned int>(unsigned int const&) const Unexecuted instantiation: int vvenc::UnitScale::scaleHor<int>(int const&) const |
133 | 0 | template<typename T> T scaleVer ( const T &in ) const { return in >> posy; } |
134 | 0 | template<typename T> T scaleArea( const T &in ) const { return in >> area; } |
135 | | |
136 | 0 | Position scale( const Position& pos ) const { return { pos.x >> posx, pos.y >> posy }; } |
137 | 0 | Size scale( const Size &size ) const { return { size.width >> posx, size.height >> posy }; } |
138 | 0 | Area scale( const Area &_area ) const { return Area( scale( _area.pos() ), scale( _area.size() ) ); } |
139 | | }; |
140 | | |
141 | | inline ptrdiff_t rsAddr(const Position& pos, const uint32_t stride, const UnitScale &unitScale ) |
142 | 0 | { |
143 | 0 | return (ptrdiff_t)(stride >> unitScale.posx) * (ptrdiff_t)(pos.y >> unitScale.posy) + (ptrdiff_t)(pos.x >> unitScale.posx); |
144 | 0 | } |
145 | | |
146 | | inline ptrdiff_t rsAddr(const Position& pos, const Position& origin, const uint32_t stride, const UnitScale &unitScale ) |
147 | 0 | { |
148 | 0 | return (ptrdiff_t)(stride >> unitScale.posx) * (ptrdiff_t)((pos.y - origin.y) >> unitScale.posy) + (ptrdiff_t)((pos.x - origin.x) >> unitScale.posx); |
149 | 0 | } |
150 | | |
151 | | inline ptrdiff_t rsAddr(const Position& pos, const uint32_t stride ) |
152 | 0 | { |
153 | 0 | return (ptrdiff_t)stride * (ptrdiff_t)pos.y + (ptrdiff_t)pos.x; |
154 | 0 | } |
155 | | |
156 | | inline ptrdiff_t rsAddr(const Position& pos, const Position& origin, const uint32_t stride ) |
157 | 0 | { |
158 | 0 | return (ptrdiff_t)stride * (ptrdiff_t)(pos.y - origin.y) + (ptrdiff_t)(pos.x - origin.x); |
159 | 0 | } |
160 | | |
161 | | inline Area clipArea(const Area& _area, const Area& boundingBox) |
162 | 0 | { |
163 | 0 | Area area = _area; |
164 | |
|
165 | 0 | if (area.x + area.width > boundingBox.x + boundingBox.width) |
166 | 0 | { |
167 | 0 | area.width = boundingBox.x + boundingBox.width - area.x; |
168 | 0 | } |
169 | |
|
170 | 0 | if (area.y + area.height > boundingBox.y + boundingBox.height) |
171 | 0 | { |
172 | 0 | area.height = boundingBox.y + boundingBox.height - area.y; |
173 | 0 | } |
174 | |
|
175 | 0 | return area; |
176 | 0 | } |
177 | | |
178 | | |
179 | | } // namespace vvenc |
180 | | |
181 | | namespace std |
182 | | { |
183 | | template <> |
184 | | struct hash<vvenc::Position> |
185 | | { |
186 | | uint64_t operator()(const vvenc::Position& value) const |
187 | 0 | { |
188 | 0 | return (((uint64_t)value.x << 32) + value.y); |
189 | 0 | } |
190 | | }; |
191 | | |
192 | | template <> |
193 | | struct hash<vvenc::Size> |
194 | | { |
195 | | uint64_t operator()(const vvenc::Size& value) const |
196 | 0 | { |
197 | 0 | return (((uint64_t)value.width << 32) + value.height); |
198 | 0 | } |
199 | | }; |
200 | | } |
201 | | |
202 | | //! \} |
203 | | |