/src/vvdec/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) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC 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 | | /** \file Mv.cpp |
44 | | \brief motion vector class |
45 | | */ |
46 | | |
47 | | #include "Mv.h" |
48 | | |
49 | | #include "Common.h" |
50 | | #include "Slice.h" |
51 | | |
52 | | namespace vvdec |
53 | | { |
54 | | |
55 | | 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 |
56 | | |
57 | | void roundAffineMv( int& mvx, int& mvy, int nShift ) |
58 | 0 | { |
59 | 0 | const int nOffset = 1 << (nShift - 1); |
60 | 0 | mvx = (mvx + nOffset - (mvx >= 0)) >> nShift; |
61 | 0 | mvy = (mvy + nOffset - (mvy >= 0)) >> nShift; |
62 | 0 | } |
63 | | |
64 | | void clipMvInPic ( Mv& rcMv, const Position& pos, const struct Size& size, const SPS& sps, const PPS& pps ) |
65 | 0 | { |
66 | 0 | if( sps.getUseWrapAround() ) |
67 | 0 | { |
68 | 0 | wrapClipMv( rcMv, pos, size, sps, pps ); |
69 | 0 | return; |
70 | 0 | } |
71 | | |
72 | 0 | int mvShift = MV_FRACTIONAL_BITS_INTERNAL; |
73 | 0 | int offset = 8; |
74 | 0 | int horMax = (pps.getPicWidthInLumaSamples() + offset - (int)pos.x - 1) *(1 << mvShift); |
75 | 0 | int horMin = (-(int)sps.getMaxCUWidth() - offset - (int)pos.x + 1) *(1 << mvShift); |
76 | |
|
77 | 0 | int verMax = (pps.getPicHeightInLumaSamples() + offset - (int)pos.y - 1) *(1 << mvShift); |
78 | 0 | int verMin = (-(int)sps.getMaxCUHeight() - offset - (int)pos.y + 1) *(1 << mvShift); |
79 | |
|
80 | 0 | rcMv.setHor(std::min(horMax, std::max(horMin, rcMv.getHor()))); |
81 | 0 | rcMv.setVer(std::min(verMax, std::max(verMin, rcMv.getVer()))); |
82 | 0 | } |
83 | | |
84 | | void clipMvInSubpic ( Mv& rcMv, const struct Position& pos, const struct Size& size, const SPS& sps, const PPS& pps ) |
85 | 0 | { |
86 | 0 | if( sps.getUseWrapAround() ) |
87 | 0 | { |
88 | 0 | wrapClipMv( rcMv, pos, size, sps, pps ); |
89 | 0 | return; |
90 | 0 | } |
91 | | |
92 | 0 | int mvShift = MV_FRACTIONAL_BITS_INTERNAL; |
93 | 0 | int offset = 8; |
94 | 0 | int horMax = (pps.getPicWidthInLumaSamples() + offset - (int)pos.x - 1) << mvShift; |
95 | 0 | int horMin = (-(int)sps.getMaxCUWidth() - offset - (int)pos.x + 1) *(1<< mvShift); |
96 | |
|
97 | 0 | int verMax = (pps.getPicHeightInLumaSamples() + offset - (int)pos.y - 1) << mvShift; |
98 | 0 | int verMin = (-(int)sps.getMaxCUHeight() - offset - (int)pos.y + 1) *(1<< mvShift); |
99 | 0 | const SubPic& curSubPic = pps.getSubPicFromPos(pos); |
100 | 0 | if (curSubPic.getTreatedAsPicFlag()) |
101 | 0 | { |
102 | 0 | horMax = ((curSubPic.getSubPicRight() + 1) + offset - (int)pos.x - 1) << mvShift; |
103 | 0 | horMin = (-(int)sps.getMaxCUWidth() - offset - ((int)pos.x - curSubPic.getSubPicLeft()) + 1) *(1<< mvShift); |
104 | |
|
105 | 0 | verMax = ((curSubPic.getSubPicBottom() + 1) + offset - (int)pos.y - 1) << mvShift; |
106 | 0 | verMin = (-(int)sps.getMaxCUHeight() - offset - ((int)pos.y - curSubPic.getSubPicTop()) + 1) *(1<< mvShift); |
107 | 0 | } |
108 | 0 | rcMv.setHor(std::min(horMax, std::max(horMin, rcMv.getHor()))); |
109 | 0 | rcMv.setVer(std::min(verMax, std::max(verMin, rcMv.getVer()))); |
110 | 0 | } |
111 | | |
112 | | bool wrapClipMv( Mv& rcMv, const Position& pos, const struct Size& size, const SPS& sps, const PPS& pps ) |
113 | 0 | { |
114 | 0 | bool wrapRef = true; |
115 | 0 | int iMvShift = MV_FRACTIONAL_BITS_INTERNAL; |
116 | 0 | int iOffset = 8; |
117 | 0 | int iHorMax = ( pps.getPicWidthInLumaSamples() + sps.getMaxCUWidth() - size.width + iOffset - (int)pos.x - 1 ) << iMvShift; |
118 | 0 | int iHorMin = ( -( int ) sps.getMaxCUWidth() - iOffset - ( int ) pos.x + 1 ) *(1<< iMvShift); |
119 | 0 | int iVerMax = ( pps.getPicHeightInLumaSamples() + iOffset - (int)pos.y - 1 ) << iMvShift; |
120 | 0 | int iVerMin = ( -( int ) sps.getMaxCUHeight() - iOffset - ( int ) pos.y + 1 ) *(1<< iMvShift); |
121 | 0 | int mvX = rcMv.getHor(); |
122 | |
|
123 | 0 | if(mvX > iHorMax) |
124 | 0 | { |
125 | 0 | mvX -= ( pps.getWrapAroundOffset() << iMvShift ); |
126 | 0 | mvX = std::min( iHorMax, std::max( iHorMin, mvX ) ); |
127 | 0 | wrapRef = false; |
128 | 0 | } |
129 | 0 | if(mvX < iHorMin) |
130 | 0 | { |
131 | 0 | mvX += ( pps.getWrapAroundOffset() << iMvShift ); |
132 | 0 | mvX = std::min( iHorMax, std::max( iHorMin, mvX ) ); |
133 | 0 | wrapRef = false; |
134 | 0 | } |
135 | |
|
136 | 0 | rcMv.setHor( mvX ); |
137 | 0 | rcMv.setVer( std::min( iVerMax, std::max( iVerMin, rcMv.getVer() ) ) ); |
138 | 0 | return wrapRef; |
139 | 0 | } |
140 | | |
141 | | |
142 | | } |