/src/vvenc/source/Lib/CommonLib/Mv.cpp
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 | | |
43 | | |
44 | | /** \file Mv.cpp |
45 | | \brief motion vector class |
46 | | */ |
47 | | |
48 | | #include "Mv.h" |
49 | | #include "Common.h" |
50 | | #include "CodingStructure.h" |
51 | | |
52 | | //! \ingroup CommonLib |
53 | | //! \{ |
54 | | |
55 | | namespace vvenc { |
56 | | |
57 | | const MvPrecision Mv::m_amvrPrecision[4] = { MV_PRECISION_QUARTER, MV_PRECISION_INT, MV_PRECISION_4PEL, MV_PRECISION_HALF }; // for cu.imv=0, 1, 2 and 3 |
58 | | const MvPrecision Mv::m_amvrPrecAffine[3] = { MV_PRECISION_QUARTER, MV_PRECISION_SIXTEENTH, MV_PRECISION_INT }; // for cu.imv=0, 1 and 2 |
59 | | const MvPrecision Mv::m_amvrPrecIbc[3] = { MV_PRECISION_INT, MV_PRECISION_INT, MV_PRECISION_4PEL }; // for cu.imv=0, 1 and 2 |
60 | | |
61 | | void roundAffineMv( int& mvx, int& mvy, int nShift ) |
62 | 0 | { |
63 | 0 | const int nOffset = 1 << (nShift - 1); |
64 | 0 | mvx = (mvx + nOffset - (mvx >= 0)) >> nShift; |
65 | 0 | mvy = (mvy + nOffset - (mvy >= 0)) >> nShift; |
66 | 0 | } |
67 | | |
68 | | void clipMv( Mv& rcMv, const Position& pos, const struct Size& size, const PreCalcValues& pcv ) |
69 | 0 | { |
70 | 0 | int iMvShift = MV_FRACTIONAL_BITS_INTERNAL; |
71 | 0 | int iOffset = 8; |
72 | 0 | int iHorMax = ( pcv.lumaWidth + iOffset - ( int ) pos.x - 1 ) << iMvShift; |
73 | 0 | int iHorMin = ( -( int ) pcv.maxCUSize - iOffset - ( int ) pos.x + 1 ) * (1 << iMvShift); |
74 | |
|
75 | 0 | int iVerMax = ( pcv.lumaHeight + iOffset - ( int ) pos.y - 1 ) << iMvShift; |
76 | 0 | int iVerMin = ( -( int ) pcv.maxCUSize - iOffset - ( int ) pos.y + 1 ) * (1 << iMvShift); |
77 | |
|
78 | 0 | rcMv.hor = ( std::min( iHorMax, std::max( iHorMin, rcMv.hor ) ) ); |
79 | 0 | rcMv.ver = ( std::min( iVerMax, std::max( iVerMin, rcMv.ver ) ) ); |
80 | 0 | } |
81 | | |
82 | | void clipMvHor( Mv& rcMv, const Position& pos, const struct Size& size, const PreCalcValues& pcv ) |
83 | 0 | { |
84 | 0 | int iMvShift = MV_FRACTIONAL_BITS_INTERNAL; |
85 | 0 | int iOffset = 8; |
86 | 0 | int iHorMax = ( pcv.lumaWidth + iOffset - ( int ) pos.x - 1 ) << iMvShift; |
87 | 0 | int iHorMin = ( -( int ) pcv.maxCUSize - iOffset - ( int ) pos.x + 1 ) * (1 << iMvShift); |
88 | 0 | rcMv.hor = ( std::min( iHorMax, std::max( iHorMin, rcMv.hor ) ) ); |
89 | 0 | } |
90 | | |
91 | | void clipMv(Mv& rcMv, const Position& pos, const struct Size& size, const PreCalcValues& pcv, const PPS& pps, bool m_clipMvInSubPic) |
92 | 0 | { |
93 | 0 | int iMvShift = MV_FRACTIONAL_BITS_INTERNAL; |
94 | 0 | int iOffset = 8; |
95 | 0 | int iHorMax = (pcv.lumaWidth + iOffset - (int)pos.x - 1) << iMvShift; |
96 | 0 | int iHorMin = (-(int)pcv.maxCUSize - iOffset - (int)pos.x + 1) * (1 << iMvShift); |
97 | |
|
98 | 0 | int iVerMax = (pcv.lumaHeight + iOffset - (int)pos.y - 1) << iMvShift; |
99 | 0 | int iVerMin = (-(int)pcv.maxCUSize - iOffset - (int)pos.y + 1) * (1 << iMvShift); |
100 | 0 | const SubPic& curSubPic = pps.getSubPicFromPos(pos); |
101 | 0 | if (curSubPic.treatedAsPic && m_clipMvInSubPic) |
102 | 0 | { |
103 | 0 | iHorMax = ((curSubPic.subPicRight + 1) + iOffset - (int)pos.x - 1) << iMvShift; |
104 | 0 | iHorMin = (-(int)pcv.maxCUSize - iOffset - ((int)pos.x - curSubPic.subPicLeft) + 1) * (1 << iMvShift); |
105 | |
|
106 | 0 | iVerMax = ((curSubPic.subPicBottom + 1) + iOffset - (int)pos.y - 1) << iMvShift; |
107 | 0 | iVerMin = (-(int)pcv.maxCUSize - iOffset - ((int)pos.y - curSubPic.subPicTop) + 1) * (1 << iMvShift); |
108 | 0 | } |
109 | 0 | rcMv.hor = (std::min(iHorMax, std::max(iHorMin, rcMv.hor))); |
110 | 0 | rcMv.ver = (std::min(iVerMax, std::max(iVerMin, rcMv.ver))); |
111 | 0 | } |
112 | | |
113 | | bool wrapClipMv( Mv& rcMv, const Position& pos, const struct Size& size, const CodingStructure& cs ) |
114 | 0 | { |
115 | 0 | bool wrapRef = true; |
116 | 0 | int iMvShift = MV_FRACTIONAL_BITS_INTERNAL; |
117 | 0 | int iOffset = 8; |
118 | 0 | int iHorMax = ( cs.pcv->lumaWidth + cs.pcv->maxCUSize - size.width + iOffset - ( int ) pos.x - 1 ) << iMvShift; |
119 | 0 | int iHorMin = ( -( int ) cs.pcv->maxCUSize - iOffset - ( int ) pos.x + 1 ) * (1 << iMvShift); |
120 | 0 | int iVerMax = ( cs.pcv->lumaHeight + iOffset - ( int ) pos.y - 1 ) << iMvShift; |
121 | 0 | int iVerMin = ( -( int ) cs.pcv->maxCUSize - iOffset - ( int ) pos.y + 1 ) * (1 << iMvShift); |
122 | 0 | int mvX = rcMv.hor; |
123 | |
|
124 | 0 | if(mvX > iHorMax) |
125 | 0 | { |
126 | 0 | mvX -= ( cs.pps->wrapAroundOffset << iMvShift ); |
127 | 0 | mvX = std::min( iHorMax, std::max( iHorMin, mvX ) ); |
128 | 0 | wrapRef = false; |
129 | 0 | } |
130 | 0 | if(mvX < iHorMin) |
131 | 0 | { |
132 | 0 | mvX += ( cs.pps->wrapAroundOffset << iMvShift ); |
133 | 0 | mvX = std::min( iHorMax, std::max( iHorMin, mvX ) ); |
134 | 0 | wrapRef = false; |
135 | 0 | } |
136 | |
|
137 | 0 | rcMv.hor = ( mvX ); |
138 | 0 | rcMv.ver = ( std::min( iVerMax, std::max( iVerMin, rcMv.ver ) ) ); |
139 | 0 | return wrapRef; |
140 | 0 | } |
141 | | |
142 | | } // namespace vvenc |
143 | | |
144 | | //! \} |
145 | | |