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 "TComRdCost.h" |
---|
47 | #include "TComSlice.h" |
---|
48 | #include "TComRdCostWeightPrediction.h" |
---|
49 | #include "../TLibRenderer/TRenModel.h" |
---|
50 | |
---|
51 | //! \ingroup TLibCommon |
---|
52 | //! \{ |
---|
53 | |
---|
54 | #define FIX203 1 |
---|
55 | |
---|
56 | class DistParam; |
---|
57 | class TComPattern; |
---|
58 | class TComRdCost; |
---|
59 | |
---|
60 | // ==================================================================================================================== |
---|
61 | // Type definition |
---|
62 | // ==================================================================================================================== |
---|
63 | |
---|
64 | // for function pointer |
---|
65 | typedef UInt (*FpDistFunc) (DistParam*); |
---|
66 | |
---|
67 | #if HHI_VSO |
---|
68 | typedef Dist (TComRdCost::*FpDistFuncVSO) ( Int, Int, Pel*, Int, Pel*, Int, UInt, UInt, Bool ); |
---|
69 | #endif |
---|
70 | // ==================================================================================================================== |
---|
71 | // Class definition |
---|
72 | // ==================================================================================================================== |
---|
73 | |
---|
74 | /// distortion parameter class |
---|
75 | class DistParam |
---|
76 | { |
---|
77 | public: |
---|
78 | Pel* pOrg; |
---|
79 | Pel* pCur; |
---|
80 | Int iStrideOrg; |
---|
81 | Int iStrideCur; |
---|
82 | Int iRows; |
---|
83 | Int iCols; |
---|
84 | Int iStep; |
---|
85 | FpDistFunc DistFunc; |
---|
86 | |
---|
87 | #if HHI_INTERVIEW_SKIP |
---|
88 | Pel* pUsed; |
---|
89 | Int iStrideUsed; |
---|
90 | #endif |
---|
91 | Bool bApplyWeight; // whether weithed prediction is used or not |
---|
92 | wpScalingParam *wpCur; // weithed prediction scaling parameters for current ref |
---|
93 | UInt uiComp; // uiComp = 0 (luma Y), 1 (chroma U), 2 (chroma V) |
---|
94 | |
---|
95 | #if NS_HAD |
---|
96 | Bool bUseNSHAD; |
---|
97 | #endif |
---|
98 | |
---|
99 | // (vertical) subsampling shift (for reducing complexity) |
---|
100 | // - 0 = no subsampling, 1 = even rows, 2 = every 4th, etc. |
---|
101 | Int iSubShift; |
---|
102 | |
---|
103 | DistParam() |
---|
104 | { |
---|
105 | pOrg = NULL; |
---|
106 | pCur = NULL; |
---|
107 | iStrideOrg = 0; |
---|
108 | iStrideCur = 0; |
---|
109 | iRows = 0; |
---|
110 | iCols = 0; |
---|
111 | iStep = 1; |
---|
112 | DistFunc = NULL; |
---|
113 | iSubShift = 0; |
---|
114 | #if HHI_INTERVIEW_SKIP |
---|
115 | pUsed = 0; |
---|
116 | iStrideUsed = 0; |
---|
117 | #endif |
---|
118 | #if NS_HAD |
---|
119 | bUseNSHAD = false; |
---|
120 | #endif |
---|
121 | } |
---|
122 | }; |
---|
123 | |
---|
124 | /// RD cost computation class |
---|
125 | class TComRdCost |
---|
126 | : public TComRdCostWeightPrediction |
---|
127 | { |
---|
128 | private: |
---|
129 | // for distortion |
---|
130 | Int m_iBlkWidth; |
---|
131 | Int m_iBlkHeight; |
---|
132 | |
---|
133 | #if AMP_SAD |
---|
134 | FpDistFunc m_afpDistortFunc[64]; // [eDFunc] |
---|
135 | #else |
---|
136 | FpDistFunc m_afpDistortFunc[33]; // [eDFunc] |
---|
137 | #endif |
---|
138 | |
---|
139 | #if WEIGHTED_CHROMA_DISTORTION |
---|
140 | Double m_chromaDistortionWeight; |
---|
141 | #endif |
---|
142 | Double m_dLambda; |
---|
143 | Double m_sqrtLambda; |
---|
144 | UInt m_uiLambdaMotionSAD; |
---|
145 | UInt m_uiLambdaMotionSSE; |
---|
146 | Double m_dFrameLambda; |
---|
147 | |
---|
148 | #if HHI_INTERVIEW_SKIP_LAMBDA_SCALE |
---|
149 | Double m_dLambdaScale ; |
---|
150 | #endif |
---|
151 | // for motion cost |
---|
152 | #if FIX203 |
---|
153 | TComMv m_mvPredictor; |
---|
154 | #else |
---|
155 | UInt* m_puiComponentCostOriginP; |
---|
156 | UInt* m_puiComponentCost; |
---|
157 | UInt* m_puiVerCost; |
---|
158 | UInt* m_puiHorCost; |
---|
159 | #endif |
---|
160 | UInt m_uiCost; |
---|
161 | Int m_iCostScale; |
---|
162 | #if !FIX203 |
---|
163 | Int m_iSearchLimit; |
---|
164 | #endif |
---|
165 | |
---|
166 | Bool m_bUseMultiviewReg; |
---|
167 | UInt m_uiLambdaMVReg; |
---|
168 | UInt m_uiLambdaMVRegSAD; |
---|
169 | UInt m_uiLambdaMVRegSSE; |
---|
170 | UInt* m_puiMultiviewRegCostHorOrgP; |
---|
171 | UInt* m_puiMultiviewRegCostVerOrgP; |
---|
172 | UInt* m_puiMultiviewRegCostHor; |
---|
173 | UInt* m_puiMultiviewRegCostVer; |
---|
174 | UInt* m_puiHorRegCost; |
---|
175 | UInt* m_puiVerRegCost; |
---|
176 | TComMv m_cMultiviewOrgMvPred; |
---|
177 | |
---|
178 | public: |
---|
179 | TComRdCost(); |
---|
180 | virtual ~TComRdCost(); |
---|
181 | |
---|
182 | Double calcRdCost ( UInt uiBits, UInt uiDistortion, Bool bFlag = false, DFunc eDFunc = DF_DEFAULT ); |
---|
183 | Double calcRdCost64( UInt64 uiBits, UInt64 uiDistortion, Bool bFlag = false, DFunc eDFunc = DF_DEFAULT ); |
---|
184 | |
---|
185 | #if WEIGHTED_CHROMA_DISTORTION |
---|
186 | Void setChromaDistortionWeight ( Double chromaDistortionWeight) { m_chromaDistortionWeight = chromaDistortionWeight; }; |
---|
187 | #endif |
---|
188 | Void setLambda ( Double dLambda ); |
---|
189 | #if HHI_INTER_VIEW_MOTION_PRED |
---|
190 | Void setLambdaMVReg ( Double dLambda ); |
---|
191 | #endif |
---|
192 | Void setFrameLambda ( Double dLambda ) { m_dFrameLambda = dLambda; } |
---|
193 | |
---|
194 | #if HHI_INTERVIEW_SKIP_LAMBDA_SCALE |
---|
195 | Void setLambdaScale ( Double dLambdaScale) { m_dLambdaScale = dLambdaScale; } |
---|
196 | Double getLambdaScale ( ) { return m_dLambdaScale ; } |
---|
197 | #endif |
---|
198 | Double getSqrtLambda () { return m_sqrtLambda; } |
---|
199 | |
---|
200 | // Distortion Functions |
---|
201 | Void init(); |
---|
202 | |
---|
203 | Void setDistParam( UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc, DistParam& rcDistParam ); |
---|
204 | Void setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, DistParam& rcDistParam ); |
---|
205 | #if NS_HAD |
---|
206 | Void setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, Int iStep, DistParam& rcDistParam, Bool bHADME=false, Bool bUseNSHAD=false ); |
---|
207 | Void setDistParam( DistParam& rcDP, Pel* p1, Int iStride1, Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard = false, Bool bUseNSHAD=false ); |
---|
208 | #else |
---|
209 | Void setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, Int iStep, DistParam& rcDistParam, Bool bHADME=false ); |
---|
210 | Void setDistParam( DistParam& rcDP, Pel* p1, Int iStride1, Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard = false ); |
---|
211 | #endif |
---|
212 | |
---|
213 | UInt calcHAD ( Pel* pi0, Int iStride0, Pel* pi1, Int iStride1, Int iWidth, Int iHeight ); |
---|
214 | |
---|
215 | // for motion cost |
---|
216 | #if !FIX203 |
---|
217 | Void initRateDistortionModel( Int iSubPelSearchLimit ); |
---|
218 | Void xUninit(); |
---|
219 | #endif |
---|
220 | UInt xGetComponentBits( Int iVal ); |
---|
221 | Void getMotionCost( Bool bSad, Int iAdd ) { m_uiCost = (bSad ? m_uiLambdaMotionSAD + iAdd : m_uiLambdaMotionSSE + iAdd); } |
---|
222 | Void setPredictor( TComMv& rcMv ) |
---|
223 | { |
---|
224 | #if FIX203 |
---|
225 | m_mvPredictor = rcMv; |
---|
226 | #else |
---|
227 | m_puiHorCost = m_puiComponentCost - rcMv.getHor(); |
---|
228 | m_puiVerCost = m_puiComponentCost - rcMv.getVer(); |
---|
229 | #endif |
---|
230 | } |
---|
231 | Void setCostScale( Int iCostScale ) { m_iCostScale = iCostScale; } |
---|
232 | __inline UInt getCost( Int x, Int y ) |
---|
233 | { |
---|
234 | #if FIX203 |
---|
235 | return m_uiCost * getBits(x, y) >> 16; |
---|
236 | #else |
---|
237 | return (( m_uiCost * (m_puiHorCost[ x * (1<<m_iCostScale) ] + m_puiVerCost[ y * (1<<m_iCostScale) ]) ) >> 16); |
---|
238 | #endif |
---|
239 | } |
---|
240 | UInt getCost( UInt b ) { return ( m_uiCost * b ) >> 16; } |
---|
241 | UInt getBits( Int x, Int y ) |
---|
242 | { |
---|
243 | #if FIX203 |
---|
244 | return xGetComponentBits((x << m_iCostScale) - m_mvPredictor.getHor()) |
---|
245 | + xGetComponentBits((y << m_iCostScale) - m_mvPredictor.getVer()); |
---|
246 | #else |
---|
247 | return m_puiHorCost[ x * (1<<m_iCostScale)] + m_puiVerCost[ y * (1<<m_iCostScale) ]; |
---|
248 | #endif |
---|
249 | } |
---|
250 | |
---|
251 | Void setMultiviewReg( TComMv* pcMv ) |
---|
252 | { |
---|
253 | if( pcMv ) |
---|
254 | { |
---|
255 | m_bUseMultiviewReg = true; |
---|
256 | m_puiHorRegCost = m_puiMultiviewRegCostHor - pcMv->getHor(); |
---|
257 | m_puiVerRegCost = m_puiMultiviewRegCostVer - pcMv->getVer(); |
---|
258 | m_cMultiviewOrgMvPred = *pcMv; |
---|
259 | } |
---|
260 | else |
---|
261 | { |
---|
262 | m_bUseMultiviewReg = false; |
---|
263 | m_puiHorRegCost = 0; |
---|
264 | m_puiVerRegCost = 0; |
---|
265 | m_cMultiviewOrgMvPred.set( 0, 0 ); |
---|
266 | } |
---|
267 | } |
---|
268 | __inline Bool useMultiviewReg () { return m_bUseMultiviewReg; } |
---|
269 | __inline TComMv& getMultiviewOrgMvPred() { return m_cMultiviewOrgMvPred; } |
---|
270 | __inline UInt getMultiviewRegCost ( Int x, Int y ) |
---|
271 | { |
---|
272 | return ( ( m_uiLambdaMVReg * ( m_puiHorRegCost[ x * ( 1 << m_iCostScale ) ] + m_puiVerRegCost[ y * ( 1 << m_iCostScale ) ] ) ) >> 16 ); |
---|
273 | } |
---|
274 | |
---|
275 | private: |
---|
276 | |
---|
277 | static UInt xGetSSE ( DistParam* pcDtParam ); |
---|
278 | static UInt xGetSSE4 ( DistParam* pcDtParam ); |
---|
279 | static UInt xGetSSE8 ( DistParam* pcDtParam ); |
---|
280 | static UInt xGetSSE16 ( DistParam* pcDtParam ); |
---|
281 | static UInt xGetSSE32 ( DistParam* pcDtParam ); |
---|
282 | static UInt xGetSSE64 ( DistParam* pcDtParam ); |
---|
283 | static UInt xGetSSE16N ( DistParam* pcDtParam ); |
---|
284 | |
---|
285 | static UInt xGetSAD ( DistParam* pcDtParam ); |
---|
286 | static UInt xGetSAD4 ( DistParam* pcDtParam ); |
---|
287 | static UInt xGetSAD8 ( DistParam* pcDtParam ); |
---|
288 | static UInt xGetSAD16 ( DistParam* pcDtParam ); |
---|
289 | static UInt xGetSAD32 ( DistParam* pcDtParam ); |
---|
290 | static UInt xGetSAD64 ( DistParam* pcDtParam ); |
---|
291 | static UInt xGetSAD16N ( DistParam* pcDtParam ); |
---|
292 | |
---|
293 | #if AMP_SAD |
---|
294 | static UInt xGetSAD12 ( DistParam* pcDtParam ); |
---|
295 | static UInt xGetSAD24 ( DistParam* pcDtParam ); |
---|
296 | static UInt xGetSAD48 ( DistParam* pcDtParam ); |
---|
297 | |
---|
298 | #endif |
---|
299 | |
---|
300 | static UInt xGetHADs4 ( DistParam* pcDtParam ); |
---|
301 | static UInt xGetHADs8 ( DistParam* pcDtParam ); |
---|
302 | static UInt xGetHADs ( DistParam* pcDtParam ); |
---|
303 | static UInt xCalcHADs2x2 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
304 | static UInt xCalcHADs4x4 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
305 | static UInt xCalcHADs8x8 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
306 | #if NS_HAD |
---|
307 | static UInt xCalcHADs16x4 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
308 | static UInt xCalcHADs4x16 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
309 | #endif |
---|
310 | |
---|
311 | public: |
---|
312 | #if HHI_INTERVIEW_SKIP |
---|
313 | UInt getDistPart( Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, Pel* piUsed, Int iUsedStride, UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc = DF_SSE ); |
---|
314 | #endif |
---|
315 | #if WEIGHTED_CHROMA_DISTORTION |
---|
316 | UInt getDistPart( Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, Bool bWeighted = false, DFunc eDFunc = DF_SSE ); |
---|
317 | #else |
---|
318 | UInt getDistPart( Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc = DF_SSE ); |
---|
319 | #endif |
---|
320 | |
---|
321 | #if HHI_VSO |
---|
322 | private: |
---|
323 | Double m_dLambdaVSO; |
---|
324 | Double m_dSqrtLambdaVSO; |
---|
325 | UInt m_uiLambdaMotionSADVSO; |
---|
326 | UInt m_uiLambdaMotionSSEVSO; |
---|
327 | Double m_dFrameLambdaVSO; |
---|
328 | |
---|
329 | #if HHI_VSO_DIST_INT |
---|
330 | Bool m_bAllowNegDist; |
---|
331 | #endif |
---|
332 | |
---|
333 | Bool m_bUseVSO; |
---|
334 | Bool m_bUseLambdaScaleVSO; |
---|
335 | UInt m_uiVSOMode; |
---|
336 | |
---|
337 | FpDistFuncVSO m_fpDistortFuncVSO; |
---|
338 | TRenModel* m_pcRenModel; |
---|
339 | public: |
---|
340 | |
---|
341 | Void setRenModel ( TRenModel* pcRenModel ) { m_pcRenModel = pcRenModel; } |
---|
342 | Void setRenModelData ( TComDataCU* pcCU, UInt uiAbsPartIndex, Pel* piData, Int iStride, Int iBlkWidth, Int iBlkHeight ); |
---|
343 | Void setLambdaVSO ( Double dLambda ); |
---|
344 | Void setFrameLambdaVSO ( Double dLambda ) { m_dFrameLambdaVSO = dLambda; }; |
---|
345 | |
---|
346 | |
---|
347 | Void setUseVSO ( Bool bIn ) { m_bUseVSO = bIn; }; |
---|
348 | Bool getUseVSO ( ) { return m_bUseVSO;}; |
---|
349 | |
---|
350 | Bool getUseRenModel ( ) { return (m_bUseVSO && m_uiVSOMode == 4); }; |
---|
351 | Void setUseLambdaScaleVSO(bool bIn) { m_bUseLambdaScaleVSO = bIn; }; |
---|
352 | Bool getUseLambdaScaleVSO( ) { return m_bUseLambdaScaleVSO; }; |
---|
353 | |
---|
354 | Void setVSOMode( UInt uiIn); |
---|
355 | UInt getVSOMode( ) { return m_uiVSOMode; } |
---|
356 | |
---|
357 | #if HHI_VSO_DIST_INT |
---|
358 | Void setAllowNegDist ( Bool bAllowNegDist ); |
---|
359 | #endif |
---|
360 | |
---|
361 | |
---|
362 | Double getSqrtLambdaVSO () { return m_dSqrtLambdaVSO; } |
---|
363 | Double getLambdaVSO () { return m_dLambdaVSO; } |
---|
364 | |
---|
365 | Dist getDistVS( TComDataCU* pcCU, UInt uiAbsPartIndex, Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, Bool bSAD, UInt uiPlane ); |
---|
366 | Double calcRdCostVSO( UInt uiBits, Dist uiDistortion, Bool bFlag = false, DFunc eDFunc = DF_DEFAULT ); |
---|
367 | |
---|
368 | private: |
---|
369 | Dist xGetDistVSOMode4( Int iStartPosX, Int iStartPosY, Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, Bool bSAD ); |
---|
370 | |
---|
371 | #endif // HHI_VSO |
---|
372 | |
---|
373 | };// END CLASS DEFINITION TComRdCost |
---|
374 | |
---|
375 | //! \} |
---|
376 | |
---|
377 | #endif // __TCOMRDCOST__ |
---|