/src/vvenc/source/Lib/CommonLib/MotionInfo.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 MotionInfo.h |
43 | | \brief motion information handling classes (header) |
44 | | \todo MvField seems to be better to be inherited from Mv |
45 | | */ |
46 | | |
47 | | #pragma once |
48 | | |
49 | | #include "CommonDef.h" |
50 | | #include "Mv.h" |
51 | | |
52 | | //! \ingroup CommonLib |
53 | | //! \{ |
54 | | |
55 | | namespace vvenc { |
56 | | |
57 | | // ==================================================================================================================== |
58 | | // Type definition |
59 | | // ==================================================================================================================== |
60 | | |
61 | | /// parameters for AMVP |
62 | | struct AMVPInfo |
63 | | { |
64 | | Mv mvCand[ AMVP_MAX_NUM_CANDS_MEM ]; ///< array of motion vector predictor candidates |
65 | | unsigned numCand; ///< number of motion vector predictor candidates |
66 | | }; |
67 | | |
68 | | struct AffineAMVPInfo |
69 | | { |
70 | | Mv mvCandLT[ AMVP_MAX_NUM_CANDS_MEM ]; ///< array of affine motion vector predictor candidates for left-top corner |
71 | | Mv mvCandRT[ AMVP_MAX_NUM_CANDS_MEM ]; ///< array of affine motion vector predictor candidates for right-top corner |
72 | | Mv mvCandLB[ AMVP_MAX_NUM_CANDS_MEM ]; ///< array of affine motion vector predictor candidates for left-bottom corner |
73 | | unsigned numCand; ///< number of motion vector predictor candidates |
74 | | }; |
75 | | |
76 | | // ==================================================================================================================== |
77 | | // Class definition |
78 | | // ==================================================================================================================== |
79 | | |
80 | | /// class for motion vector with reference index |
81 | | struct MvField |
82 | | { |
83 | | Mv mv; |
84 | | int16_t refIdx; |
85 | | |
86 | 0 | MvField() : refIdx( NOT_VALID ) {} |
87 | 0 | MvField( Mv const & cMv, const int iRefIdx ) : mv( cMv ), refIdx( iRefIdx ) {} |
88 | | |
89 | | void setMvField( Mv const & cMv, const int iRefIdx ) |
90 | 0 | { |
91 | 0 | CHECK( iRefIdx == NOT_VALID && cMv != Mv(0,0), "Must not happen." ); |
92 | 0 | mv = cMv; |
93 | 0 | refIdx = iRefIdx; |
94 | 0 | } |
95 | | |
96 | | bool operator==( const MvField& other ) const |
97 | 0 | { |
98 | 0 | CHECK( refIdx == NOT_VALID && mv != Mv(0,0), "Error in operator== of MvField." ); |
99 | 0 | CHECK( other.refIdx == NOT_VALID && other.mv != Mv(0,0), "Error in operator== of MvField." ); |
100 | 0 | return refIdx == other.refIdx && mv == other.mv; |
101 | 0 | } |
102 | | bool operator!=( const MvField& other ) const |
103 | 0 | { |
104 | 0 | CHECK( refIdx == NOT_VALID && mv != Mv(0,0), "Error in operator!= of MvField." ); |
105 | 0 | CHECK( other.refIdx == NOT_VALID && other.mv != Mv(0,0), "Error in operator!= of MvField." ); |
106 | 0 | return refIdx != other.refIdx || mv != other.mv; |
107 | 0 | } |
108 | | }; |
109 | | |
110 | | struct MotionInfo |
111 | | { |
112 | | Mv mv [NUM_REF_PIC_LIST_01]; |
113 | | int8_t miRefIdx[NUM_REF_PIC_LIST_01] = { MI_NOT_VALID, MI_NOT_VALID }; |
114 | | |
115 | | bool operator==( const MotionInfo& mi ) const |
116 | 0 | { |
117 | 0 | if( miRefIdx[0] != mi.miRefIdx[0] ) return false; |
118 | 0 | if( miRefIdx[0] != MI_NOT_VALID && mv[0] != mi.mv[0] ) return false; |
119 | | |
120 | 0 | if( miRefIdx[1] != mi.miRefIdx[1] ) return false; |
121 | 0 | if( miRefIdx[1] != MI_NOT_VALID && mv[1] != mi.mv[1] ) return false; |
122 | | |
123 | 0 | return true; |
124 | 0 | } |
125 | | |
126 | | bool operator!=( const MotionInfo& mi ) const |
127 | 0 | { |
128 | 0 | return !( *this == mi ); |
129 | 0 | } |
130 | | |
131 | | int interDir() const |
132 | 0 | { |
133 | 0 | int |
134 | 0 | interDir = miRefIdx[0] != MI_NOT_VALID ? 1 : 0; |
135 | 0 | interDir += miRefIdx[1] != MI_NOT_VALID ? 2 : 0; |
136 | 0 | return interDir; |
137 | 0 | } |
138 | | |
139 | 0 | int isInter() const { return interDir() != 0; } |
140 | | }; |
141 | | |
142 | | struct HPMVInfo |
143 | | { |
144 | | Mv mv [NUM_REF_PIC_LIST_01]; |
145 | | int8_t mhRefIdx[NUM_REF_PIC_LIST_01] = { MH_NOT_VALID, MH_NOT_VALID }; |
146 | | |
147 | | uint8_t BcwIdx = 0; |
148 | | bool useAltHpelIf = false;; |
149 | | |
150 | 0 | HPMVInfo() = default; |
151 | | HPMVInfo( const MotionInfo& mi, uint8_t _bcwIdx, bool _useAltHpelIf, bool isIBC ) |
152 | 0 | { |
153 | 0 | mv[0] = mi.mv[0]; |
154 | 0 | mv[1] = mi.mv[1]; |
155 | |
|
156 | 0 | mhRefIdx[0] = mi.miRefIdx[0] + ( isIBC ? 1 : 0 ); |
157 | 0 | mhRefIdx[1] = mi.miRefIdx[1]; |
158 | |
|
159 | 0 | BcwIdx = _bcwIdx; |
160 | 0 | useAltHpelIf = _useAltHpelIf; |
161 | 0 | } |
162 | | |
163 | | int interDir() const |
164 | 0 | { |
165 | 0 | int |
166 | 0 | interDir = mhRefIdx[0] != MH_NOT_VALID ? 1 : 0; |
167 | 0 | interDir += mhRefIdx[1] != MH_NOT_VALID ? 2 : 0; |
168 | 0 | return interDir; |
169 | 0 | } |
170 | | |
171 | | bool operator==( const HPMVInfo& mi ) const |
172 | 0 | { |
173 | 0 | if( mhRefIdx[0] != mi.mhRefIdx[0] ) return false; |
174 | 0 | if( mhRefIdx[0] != MH_NOT_VALID && mv[0] != mi.mv[0] ) return false; |
175 | | |
176 | 0 | if( mhRefIdx[1] != mi.mhRefIdx[1] ) return false; |
177 | 0 | if( mhRefIdx[1] != MH_NOT_VALID && mv[1] != mi.mv[1] ) return false; |
178 | | |
179 | 0 | return true; |
180 | 0 | } |
181 | | |
182 | | bool operator==( const MotionInfo& mi ) const |
183 | 0 | { |
184 | 0 | if( mhRefIdx[0] != mi.miRefIdx[0] ) return false; |
185 | 0 | if( mhRefIdx[0] != MH_NOT_VALID && mv[0] != mi.mv[0] ) return false; |
186 | | |
187 | 0 | if( mhRefIdx[1] != mi.miRefIdx[1] ) return false; |
188 | 0 | if( mhRefIdx[1] != MH_NOT_VALID && mv[1] != mi.mv[1] ) return false; |
189 | | |
190 | 0 | return true; |
191 | 0 | } |
192 | | |
193 | | bool operator!=( const HPMVInfo& mi ) const |
194 | 0 | { |
195 | 0 | return !( *this == mi ); |
196 | 0 | } |
197 | | |
198 | | bool operator!=( const MotionInfo& mi ) const |
199 | 0 | { |
200 | 0 | return !( *this == mi ); |
201 | 0 | } |
202 | | }; |
203 | | |
204 | | struct LutMotionCand |
205 | | { |
206 | | static_vector<HPMVInfo, MAX_NUM_HMVP_CANDS> lut; |
207 | | static_vector<HPMVInfo, MAX_NUM_HMVP_CANDS> lutIbc; |
208 | | }; |
209 | | |
210 | | struct IbcBvCand |
211 | | { |
212 | | Mv m_bvCands[IBC_NUM_CANDIDATES]; |
213 | | int currCnt; |
214 | | void resetIbcBvCand() |
215 | 0 | { |
216 | 0 | for( int i = 0; i < IBC_NUM_CANDIDATES; i++ ) |
217 | 0 | { |
218 | 0 | m_bvCands[ i ].setZero(); |
219 | 0 | } |
220 | 0 | currCnt = 0; |
221 | 0 | } |
222 | | }; |
223 | | |
224 | | class BcwMotionParam |
225 | | { |
226 | | bool m_readOnly[2][33]; // 2 RefLists, 33 RefFrams |
227 | | Mv m_mv[2][33]; |
228 | | Distortion m_dist[2][33]; |
229 | | |
230 | | bool m_readOnlyAffine[2][2][33]; |
231 | | Mv m_mvAffine[2][2][33][3]; |
232 | | Distortion m_distAffine[2][2][33]; |
233 | | int m_mvpIdx[2][2][33]; |
234 | | |
235 | | public: |
236 | | |
237 | | void reset() |
238 | 0 | { |
239 | 0 | Mv* pMv = &(m_mv[0][0]); |
240 | 0 | for (int ui = 0; ui < 1 * 2 * 33; ++ui, ++pMv) |
241 | 0 | { |
242 | 0 | pMv->set(std::numeric_limits<int16_t>::max(), std::numeric_limits<int16_t>::max()); |
243 | 0 | } |
244 | |
|
245 | 0 | Mv* pAffineMv = &(m_mvAffine[0][0][0][0]); |
246 | 0 | for (int ui = 0; ui < 2 * 2 * 33 * 3; ++ui, ++pAffineMv) |
247 | 0 | { |
248 | 0 | pAffineMv->set(0, 0); |
249 | 0 | } |
250 | |
|
251 | 0 | memset(m_readOnly, false, 2 * 33 * sizeof(bool)); |
252 | 0 | memset(m_dist, -1, 2 * 33 * sizeof(Distortion)); |
253 | 0 | memset(m_readOnlyAffine, false, 2 * 2 * 33 * sizeof(bool)); |
254 | 0 | memset(m_distAffine, -1, 2 * 2 * 33 * sizeof(Distortion)); |
255 | 0 | memset( m_mvpIdx, 0, 2 * 2 * 33 * sizeof( int ) ); |
256 | 0 | } |
257 | | |
258 | 0 | void setReadMode(bool b, uint32_t uiRefList, uint32_t uiRefIdx) { m_readOnly[uiRefList][uiRefIdx] = b; } |
259 | 0 | bool isReadMode(uint32_t uiRefList, uint32_t uiRefIdx) { return m_readOnly[uiRefList][uiRefIdx]; } |
260 | | |
261 | 0 | void setReadModeAffine(bool b, uint32_t uiRefList, uint32_t uiRefIdx, int bP4) { m_readOnlyAffine[bP4][uiRefList][uiRefIdx] = b; } |
262 | 0 | bool isReadModeAffine(uint32_t uiRefList, uint32_t uiRefIdx, int bP4) { return m_readOnlyAffine[bP4][uiRefList][uiRefIdx]; } |
263 | | |
264 | 0 | Mv& getMv(uint32_t uiRefList, uint32_t uiRefIdx) { return m_mv[uiRefList][uiRefIdx]; } |
265 | | |
266 | | void copyFrom(Mv& rcMv, Distortion uiDist, uint32_t uiRefList, uint32_t uiRefIdx) |
267 | 0 | { |
268 | 0 | m_mv[uiRefList][uiRefIdx] = rcMv; |
269 | 0 | m_dist[uiRefList][uiRefIdx] = uiDist; |
270 | 0 | } |
271 | | |
272 | | void copyTo(Mv& rcMv, Distortion& ruiDist, uint32_t uiRefList, uint32_t uiRefIdx) |
273 | 0 | { |
274 | 0 | rcMv = m_mv[uiRefList][uiRefIdx]; |
275 | 0 | ruiDist = m_dist[uiRefList][uiRefIdx]; |
276 | 0 | } |
277 | | |
278 | 0 | Mv& getAffineMv(uint32_t uiRefList, uint32_t uiRefIdx, uint32_t uiAffineMvIdx, int bP4) { return m_mvAffine[bP4][uiRefList][uiRefIdx][uiAffineMvIdx]; } |
279 | | |
280 | | void copyAffineMvFrom(Mv(&racAffineMvs)[3], Distortion uiDist, uint32_t uiRefList, uint32_t uiRefIdx, int bP4 |
281 | | , const int mvpIdx |
282 | | ) |
283 | 0 | { |
284 | 0 | memcpy(m_mvAffine[bP4][uiRefList][uiRefIdx], racAffineMvs, 3 * sizeof(Mv)); |
285 | 0 | m_distAffine[bP4][uiRefList][uiRefIdx] = uiDist; |
286 | 0 | m_mvpIdx[bP4][uiRefList][uiRefIdx] = mvpIdx; |
287 | 0 | } |
288 | | |
289 | | void copyAffineMvTo(Mv acAffineMvs[3], Distortion& ruiDist, uint32_t uiRefList, uint32_t uiRefIdx, int bP4 |
290 | | , int& mvpIdx |
291 | | ) |
292 | 0 | { |
293 | 0 | memcpy(acAffineMvs, m_mvAffine[bP4][uiRefList][uiRefIdx], 3 * sizeof(Mv)); |
294 | 0 | ruiDist = m_distAffine[bP4][uiRefList][uiRefIdx]; |
295 | 0 | mvpIdx = m_mvpIdx[bP4][uiRefList][uiRefIdx]; |
296 | 0 | } |
297 | | }; |
298 | | |
299 | | } // namespace vvenc |
300 | | |
301 | | //! \} |
302 | | |