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 ) |
---|
222 | { |
---|
223 | m_uiCost = (bSad ? m_uiLambdaMotionSAD + iAdd : m_uiLambdaMotionSSE + iAdd); |
---|
224 | #if HHI_FIX |
---|
225 | m_uiLambdaMVReg = ( bSad ? m_uiLambdaMVRegSAD : m_uiLambdaMVRegSSE ); |
---|
226 | #endif |
---|
227 | } |
---|
228 | Void setPredictor( TComMv& rcMv ) |
---|
229 | { |
---|
230 | #if FIX203 |
---|
231 | m_mvPredictor = rcMv; |
---|
232 | #else |
---|
233 | m_puiHorCost = m_puiComponentCost - rcMv.getHor(); |
---|
234 | m_puiVerCost = m_puiComponentCost - rcMv.getVer(); |
---|
235 | #endif |
---|
236 | } |
---|
237 | Void setCostScale( Int iCostScale ) { m_iCostScale = iCostScale; } |
---|
238 | __inline UInt getCost( Int x, Int y ) |
---|
239 | { |
---|
240 | #if FIX203 |
---|
241 | return m_uiCost * getBits(x, y) >> 16; |
---|
242 | #else |
---|
243 | return (( m_uiCost * (m_puiHorCost[ x * (1<<m_iCostScale) ] + m_puiVerCost[ y * (1<<m_iCostScale) ]) ) >> 16); |
---|
244 | #endif |
---|
245 | } |
---|
246 | UInt getCost( UInt b ) { return ( m_uiCost * b ) >> 16; } |
---|
247 | UInt getBits( Int x, Int y ) |
---|
248 | { |
---|
249 | #if FIX203 |
---|
250 | return xGetComponentBits((x << m_iCostScale) - m_mvPredictor.getHor()) |
---|
251 | + xGetComponentBits((y << m_iCostScale) - m_mvPredictor.getVer()); |
---|
252 | #else |
---|
253 | return m_puiHorCost[ x * (1<<m_iCostScale)] + m_puiVerCost[ y * (1<<m_iCostScale) ]; |
---|
254 | #endif |
---|
255 | } |
---|
256 | |
---|
257 | Void setMultiviewReg( TComMv* pcMv ) |
---|
258 | { |
---|
259 | if( pcMv ) |
---|
260 | { |
---|
261 | m_bUseMultiviewReg = true; |
---|
262 | m_puiHorRegCost = m_puiMultiviewRegCostHor - pcMv->getHor(); |
---|
263 | m_puiVerRegCost = m_puiMultiviewRegCostVer - pcMv->getVer(); |
---|
264 | m_cMultiviewOrgMvPred = *pcMv; |
---|
265 | } |
---|
266 | else |
---|
267 | { |
---|
268 | m_bUseMultiviewReg = false; |
---|
269 | m_puiHorRegCost = 0; |
---|
270 | m_puiVerRegCost = 0; |
---|
271 | m_cMultiviewOrgMvPred.set( 0, 0 ); |
---|
272 | } |
---|
273 | } |
---|
274 | __inline Bool useMultiviewReg () { return m_bUseMultiviewReg; } |
---|
275 | __inline TComMv& getMultiviewOrgMvPred() { return m_cMultiviewOrgMvPred; } |
---|
276 | __inline UInt getMultiviewRegCost ( Int x, Int y ) |
---|
277 | { |
---|
278 | #if FIX203 |
---|
279 | return m_uiLambdaMVReg * getBits(x, y) >> 16; |
---|
280 | #else |
---|
281 | return ( ( m_uiLambdaMVReg * ( m_puiHorRegCost[ x * ( 1 << m_iCostScale ) ] + m_puiVerRegCost[ y * ( 1 << m_iCostScale ) ] ) ) >> 16 ); |
---|
282 | #endif |
---|
283 | } |
---|
284 | |
---|
285 | private: |
---|
286 | |
---|
287 | static UInt xGetSSE ( DistParam* pcDtParam ); |
---|
288 | static UInt xGetSSE4 ( DistParam* pcDtParam ); |
---|
289 | static UInt xGetSSE8 ( DistParam* pcDtParam ); |
---|
290 | static UInt xGetSSE16 ( DistParam* pcDtParam ); |
---|
291 | static UInt xGetSSE32 ( DistParam* pcDtParam ); |
---|
292 | static UInt xGetSSE64 ( DistParam* pcDtParam ); |
---|
293 | static UInt xGetSSE16N ( DistParam* pcDtParam ); |
---|
294 | |
---|
295 | static UInt xGetSAD ( DistParam* pcDtParam ); |
---|
296 | static UInt xGetSAD4 ( DistParam* pcDtParam ); |
---|
297 | static UInt xGetSAD8 ( DistParam* pcDtParam ); |
---|
298 | static UInt xGetSAD16 ( DistParam* pcDtParam ); |
---|
299 | static UInt xGetSAD32 ( DistParam* pcDtParam ); |
---|
300 | static UInt xGetSAD64 ( DistParam* pcDtParam ); |
---|
301 | static UInt xGetSAD16N ( DistParam* pcDtParam ); |
---|
302 | |
---|
303 | #if AMP_SAD |
---|
304 | static UInt xGetSAD12 ( DistParam* pcDtParam ); |
---|
305 | static UInt xGetSAD24 ( DistParam* pcDtParam ); |
---|
306 | static UInt xGetSAD48 ( DistParam* pcDtParam ); |
---|
307 | |
---|
308 | #endif |
---|
309 | |
---|
310 | static UInt xGetHADs4 ( DistParam* pcDtParam ); |
---|
311 | static UInt xGetHADs8 ( DistParam* pcDtParam ); |
---|
312 | static UInt xGetHADs ( DistParam* pcDtParam ); |
---|
313 | static UInt xCalcHADs2x2 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
314 | static UInt xCalcHADs4x4 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
315 | static UInt xCalcHADs8x8 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
316 | #if NS_HAD |
---|
317 | static UInt xCalcHADs16x4 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
318 | static UInt xCalcHADs4x16 ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
319 | #endif |
---|
320 | |
---|
321 | public: |
---|
322 | #if HHI_INTERVIEW_SKIP |
---|
323 | UInt getDistPart( Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, Pel* piUsed, Int iUsedStride, UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc = DF_SSE ); |
---|
324 | #endif |
---|
325 | #if WEIGHTED_CHROMA_DISTORTION |
---|
326 | UInt getDistPart( Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, Bool bWeighted = false, DFunc eDFunc = DF_SSE ); |
---|
327 | #else |
---|
328 | UInt getDistPart( Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc = DF_SSE ); |
---|
329 | #endif |
---|
330 | |
---|
331 | #if HHI_VSO |
---|
332 | private: |
---|
333 | Double m_dLambdaVSO; |
---|
334 | Double m_dSqrtLambdaVSO; |
---|
335 | UInt m_uiLambdaMotionSADVSO; |
---|
336 | UInt m_uiLambdaMotionSSEVSO; |
---|
337 | Double m_dFrameLambdaVSO; |
---|
338 | |
---|
339 | #if HHI_VSO_DIST_INT |
---|
340 | Bool m_bAllowNegDist; |
---|
341 | #endif |
---|
342 | |
---|
343 | Bool m_bUseVSO; |
---|
344 | Bool m_bUseLambdaScaleVSO; |
---|
345 | UInt m_uiVSOMode; |
---|
346 | |
---|
347 | FpDistFuncVSO m_fpDistortFuncVSO; |
---|
348 | TRenModel* m_pcRenModel; |
---|
349 | public: |
---|
350 | |
---|
351 | Void setRenModel ( TRenModel* pcRenModel ) { m_pcRenModel = pcRenModel; } |
---|
352 | Void setRenModelData ( TComDataCU* pcCU, UInt uiAbsPartIndex, Pel* piData, Int iStride, Int iBlkWidth, Int iBlkHeight ); |
---|
353 | Void setLambdaVSO ( Double dLambda ); |
---|
354 | Void setFrameLambdaVSO ( Double dLambda ) { m_dFrameLambdaVSO = dLambda; }; |
---|
355 | |
---|
356 | |
---|
357 | Void setUseVSO ( Bool bIn ) { m_bUseVSO = bIn; }; |
---|
358 | Bool getUseVSO ( ) { return m_bUseVSO;}; |
---|
359 | |
---|
360 | Bool getUseRenModel ( ) { return (m_bUseVSO && m_uiVSOMode == 4); }; |
---|
361 | Void setUseLambdaScaleVSO(bool bIn) { m_bUseLambdaScaleVSO = bIn; }; |
---|
362 | Bool getUseLambdaScaleVSO( ) { return m_bUseLambdaScaleVSO; }; |
---|
363 | |
---|
364 | Void setVSOMode( UInt uiIn); |
---|
365 | UInt getVSOMode( ) { return m_uiVSOMode; } |
---|
366 | |
---|
367 | #if HHI_VSO_DIST_INT |
---|
368 | Void setAllowNegDist ( Bool bAllowNegDist ); |
---|
369 | #endif |
---|
370 | |
---|
371 | |
---|
372 | Double getSqrtLambdaVSO () { return m_dSqrtLambdaVSO; } |
---|
373 | Double getLambdaVSO () { return m_dLambdaVSO; } |
---|
374 | |
---|
375 | Dist getDistVS( TComDataCU* pcCU, UInt uiAbsPartIndex, Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, Bool bSAD, UInt uiPlane ); |
---|
376 | Double calcRdCostVSO( UInt uiBits, Dist uiDistortion, Bool bFlag = false, DFunc eDFunc = DF_DEFAULT ); |
---|
377 | |
---|
378 | private: |
---|
379 | Dist xGetDistVSOMode4( Int iStartPosX, Int iStartPosY, Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, Bool bSAD ); |
---|
380 | |
---|
381 | #endif // HHI_VSO |
---|
382 | |
---|
383 | };// END CLASS DEFINITION TComRdCost |
---|
384 | |
---|
385 | //! \} |
---|
386 | |
---|
387 | #endif // __TCOMRDCOST__ |
---|