1 | /* The copyright in this software is being made available under the BSD |
---|
2 | * License, included below. This software may be subject to other third party |
---|
3 | * and contributor rights, including patent rights, and no such rights are |
---|
4 | * granted under this license. |
---|
5 | * |
---|
6 | * Copyright (c) 2010-2012, ITU/ISO/IEC |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * |
---|
12 | * * Redistributions of source code must retain the above copyright notice, |
---|
13 | * this list of conditions and the following disclaimer. |
---|
14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | * this list of conditions and the following disclaimer in the documentation |
---|
16 | * and/or other materials provided with the distribution. |
---|
17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
18 | * be used to endorse or promote products derived from this software without |
---|
19 | * specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | */ |
---|
33 | |
---|
34 | /** \file TComRdCost.h |
---|
35 | \brief RD cost computation classes (header) |
---|
36 | */ |
---|
37 | |
---|
38 | #ifndef __TCOMRDCOST__ |
---|
39 | #define __TCOMRDCOST__ |
---|
40 | |
---|
41 | |
---|
42 | #include "CommonDef.h" |
---|
43 | #include "TComPattern.h" |
---|
44 | #include "TComMv.h" |
---|
45 | |
---|
46 | #include "TComSlice.h" |
---|
47 | #include "TComRdCostWeightPrediction.h" |
---|
48 | |
---|
49 | //! \ingroup TLibCommon |
---|
50 | //! \{ |
---|
51 | |
---|
52 | #define FIX203 1 |
---|
53 | |
---|
54 | class DistParam; |
---|
55 | class TComPattern; |
---|
56 | |
---|
57 | // ==================================================================================================================== |
---|
58 | // Type definition |
---|
59 | // ==================================================================================================================== |
---|
60 | |
---|
61 | // for function pointer |
---|
62 | typedef UInt (*FpDistFunc) (DistParam*); |
---|
63 | |
---|
64 | // ==================================================================================================================== |
---|
65 | // Class definition |
---|
66 | // ==================================================================================================================== |
---|
67 | |
---|
68 | /// distortion parameter class |
---|
69 | class DistParam |
---|
70 | { |
---|
71 | public: |
---|
72 | Pel* pOrg; |
---|
73 | Pel* pCur; |
---|
74 | Int iStrideOrg; |
---|
75 | Int iStrideCur; |
---|
76 | Int iRows; |
---|
77 | Int iCols; |
---|
78 | Int iStep; |
---|
79 | FpDistFunc DistFunc; |
---|
80 | |
---|
81 | Bool bApplyWeight; // whether weithed prediction is used or not |
---|
82 | wpScalingParam *wpCur; // weithed prediction scaling parameters for current ref |
---|
83 | UInt uiComp; // uiComp = 0 (luma Y), 1 (chroma U), 2 (chroma V) |
---|
84 | |
---|
85 | #if NS_HAD |
---|
86 | Bool bUseNSHAD; |
---|
87 | #endif |
---|
88 | |
---|
89 | // (vertical) subsampling shift (for reducing complexity) |
---|
90 | // - 0 = no subsampling, 1 = even rows, 2 = every 4th, etc. |
---|
91 | Int iSubShift; |
---|
92 | |
---|
93 | DistParam() |
---|
94 | { |
---|
95 | pOrg = NULL; |
---|
96 | pCur = NULL; |
---|
97 | iStrideOrg = 0; |
---|
98 | iStrideCur = 0; |
---|
99 | iRows = 0; |
---|
100 | iCols = 0; |
---|
101 | iStep = 1; |
---|
102 | DistFunc = NULL; |
---|
103 | iSubShift = 0; |
---|
104 | #if NS_HAD |
---|
105 | bUseNSHAD = false; |
---|
106 | #endif |
---|
107 | } |
---|
108 | }; |
---|
109 | |
---|
110 | /// RD cost computation class |
---|
111 | class TComRdCost |
---|
112 | : public TComRdCostWeightPrediction |
---|
113 | { |
---|
114 | private: |
---|
115 | // for distortion |
---|
116 | Int m_iBlkWidth; |
---|
117 | Int m_iBlkHeight; |
---|
118 | |
---|
119 | #if AMP_SAD |
---|
120 | FpDistFunc m_afpDistortFunc[64]; // [eDFunc] |
---|
121 | #else |
---|
122 | FpDistFunc m_afpDistortFunc[33]; // [eDFunc] |
---|
123 | #endif |
---|
124 | |
---|
125 | #if WEIGHTED_CHROMA_DISTORTION |
---|
126 | Double m_chromaDistortionWeight; |
---|
127 | #endif |
---|
128 | Double m_dLambda; |
---|
129 | Double m_sqrtLambda; |
---|
130 | UInt m_uiLambdaMotionSAD; |
---|
131 | UInt m_uiLambdaMotionSSE; |
---|
132 | Double m_dFrameLambda; |
---|
133 | |
---|
134 | // for motion cost |
---|
135 | #if FIX203 |
---|
136 | TComMv m_mvPredictor; |
---|
137 | #else |
---|
138 | UInt* m_puiComponentCostOriginP; |
---|
139 | UInt* m_puiComponentCost; |
---|
140 | UInt* m_puiVerCost; |
---|
141 | UInt* m_puiHorCost; |
---|
142 | #endif |
---|
143 | UInt m_uiCost; |
---|
144 | Int m_iCostScale; |
---|
145 | #if !FIX203 |
---|
146 | Int m_iSearchLimit; |
---|
147 | #endif |
---|
148 | |
---|
149 | public: |
---|
150 | TComRdCost(); |
---|
151 | virtual ~TComRdCost(); |
---|
152 | |
---|
153 | Double calcRdCost ( UInt uiBits, UInt uiDistortion, Bool bFlag = false, DFunc eDFunc = DF_DEFAULT ); |
---|
154 | Double calcRdCost64( UInt64 uiBits, UInt64 uiDistortion, Bool bFlag = false, DFunc eDFunc = DF_DEFAULT ); |
---|
155 | |
---|
156 | #if WEIGHTED_CHROMA_DISTORTION |
---|
157 | Void setChromaDistortionWeight ( Double chromaDistortionWeight) { m_chromaDistortionWeight = chromaDistortionWeight; }; |
---|
158 | #endif |
---|
159 | Void setLambda ( Double dLambda ); |
---|
160 | Void setFrameLambda ( Double dLambda ) { m_dFrameLambda = dLambda; } |
---|
161 | |
---|
162 | Double getSqrtLambda () { return m_sqrtLambda; } |
---|
163 | |
---|
164 | // Distortion Functions |
---|
165 | Void init(); |
---|
166 | |
---|
167 | Void setDistParam( UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc, DistParam& rcDistParam ); |
---|
168 | Void setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, DistParam& rcDistParam ); |
---|
169 | #if NS_HAD |
---|
170 | Void setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, Int iStep, DistParam& rcDistParam, Bool bHADME=false, Bool bUseNSHAD=false ); |
---|
171 | Void setDistParam( DistParam& rcDP, Pel* p1, Int iStride1, Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard = false, Bool bUseNSHAD=false ); |
---|
172 | #else |
---|
173 | Void setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, Int iStep, DistParam& rcDistParam, Bool bHADME=false ); |
---|
174 | Void setDistParam( DistParam& rcDP, Pel* p1, Int iStride1, Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard = false ); |
---|
175 | #endif |
---|
176 | |
---|
177 | UInt calcHAD ( Pel* pi0, Int iStride0, Pel* pi1, Int iStride1, Int iWidth, Int iHeight ); |
---|
178 | |
---|
179 | // for motion cost |
---|
180 | #if !FIX203 |
---|
181 | Void initRateDistortionModel( Int iSubPelSearchLimit ); |
---|
182 | Void xUninit(); |
---|
183 | #endif |
---|
184 | UInt xGetComponentBits( Int iVal ); |
---|
185 | Void getMotionCost( Bool bSad, Int iAdd ) { m_uiCost = (bSad ? m_uiLambdaMotionSAD + iAdd : m_uiLambdaMotionSSE + iAdd); } |
---|
186 | Void setPredictor( TComMv& rcMv ) |
---|
187 | { |
---|
188 | #if FIX203 |
---|
189 | m_mvPredictor = rcMv; |
---|
190 | #else |
---|
191 | m_puiHorCost = m_puiComponentCost - rcMv.getHor(); |
---|
192 | m_puiVerCost = m_puiComponentCost - rcMv.getVer(); |
---|
193 | #endif |
---|
194 | } |
---|
195 | Void setCostScale( Int iCostScale ) { m_iCostScale = iCostScale; } |
---|
196 | __inline UInt getCost( Int x, Int y ) |
---|
197 | { |
---|
198 | #if FIX203 |
---|
199 | return m_uiCost * getBits(x, y) >> 16; |
---|
200 | #else |
---|
201 | return (( m_uiCost * (m_puiHorCost[ x * (1<<m_iCostScale) ] + m_puiVerCost[ y * (1<<m_iCostScale) ]) ) >> 16); |
---|
202 | #endif |
---|
203 | } |
---|
204 | UInt getCost( UInt b ) { return ( m_uiCost * b ) >> 16; } |
---|
205 | UInt getBits( Int x, Int y ) |
---|
206 | { |
---|
207 | #if FIX203 |
---|
208 | return xGetComponentBits((x << m_iCostScale) - m_mvPredictor.getHor()) |
---|
209 | + xGetComponentBits((y << m_iCostScale) - m_mvPredictor.getVer()); |
---|
210 | #else |
---|
211 | return m_puiHorCost[ x * (1<<m_iCostScale)] + m_puiVerCost[ y * (1<<m_iCostScale) ]; |
---|
212 | #endif |
---|
213 | } |
---|
214 | |
---|
215 | private: |
---|
216 | |
---|
217 | static UInt xGetSSE ( DistParam* pcDtParam ); |
---|
218 | static UInt xGetSSE4 ( DistParam* pcDtParam ); |
---|
219 | static UInt xGetSSE8 ( DistParam* pcDtParam ); |
---|
220 | static UInt xGetSSE16 ( DistParam* pcDtParam ); |
---|
221 | static UInt xGetSSE32 ( DistParam* pcDtParam ); |
---|
222 | static UInt xGetSSE64 ( DistParam* pcDtParam ); |
---|
223 | static UInt xGetSSE16N ( DistParam* pcDtParam ); |
---|
224 | |
---|
225 | static UInt xGetSAD ( DistParam* pcDtParam ); |
---|
226 | static UInt xGetSAD4 ( DistParam* pcDtParam ); |
---|
227 | static UInt xGetSAD8 ( DistParam* pcDtParam ); |
---|
228 | static UInt xGetSAD16 ( DistParam* pcDtParam ); |
---|
229 | static UInt xGetSAD32 ( DistParam* pcDtParam ); |
---|
230 | static UInt xGetSAD64 ( DistParam* pcDtParam ); |
---|
231 | static UInt xGetSAD16N ( DistParam* pcDtParam ); |
---|
232 | |
---|
233 | #if AMP_SAD |
---|
234 | static UInt xGetSAD12 ( DistParam* pcDtParam ); |
---|
235 | static UInt xGetSAD24 ( DistParam* pcDtParam ); |
---|
236 | static UInt xGetSAD48 ( DistParam* pcDtParam ); |
---|
237 | |
---|
238 | #endif |
---|
239 | |
---|
240 | static UInt xGetHADs4 ( DistParam* pcDtParam ); |
---|
241 | static UInt xGetHADs8 ( DistParam* pcDtParam ); |
---|
242 | static UInt xGetHADs ( DistParam* pcDtParam ); |
---|
243 | static UInt xCalcHADs2x2 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
244 | static UInt xCalcHADs4x4 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
245 | static UInt xCalcHADs8x8 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
246 | #if NS_HAD |
---|
247 | static UInt xCalcHADs16x4 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
248 | static UInt xCalcHADs4x16 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
249 | #endif |
---|
250 | |
---|
251 | public: |
---|
252 | #if WEIGHTED_CHROMA_DISTORTION |
---|
253 | UInt getDistPart( Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, Bool bWeighted = false, DFunc eDFunc = DF_SSE ); |
---|
254 | #else |
---|
255 | UInt getDistPart( Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc = DF_SSE ); |
---|
256 | #endif |
---|
257 | |
---|
258 | };// END CLASS DEFINITION TComRdCost |
---|
259 | |
---|
260 | //! \} |
---|
261 | |
---|
262 | #endif // __TCOMRDCOST__ |
---|