[5] | 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 |
---|
[1313] | 4 | * granted under this license. |
---|
[5] | 5 | * |
---|
[1313] | 6 | * Copyright (c) 2010-2015, ITU/ISO/IEC |
---|
[5] | 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. |
---|
[56] | 17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
[5] | 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 | |
---|
[2] | 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" |
---|
[56] | 45 | |
---|
| 46 | #include "TComSlice.h" |
---|
| 47 | #include "TComRdCostWeightPrediction.h" |
---|
[2] | 48 | |
---|
[56] | 49 | //! \ingroup TLibCommon |
---|
| 50 | //! \{ |
---|
| 51 | |
---|
[2] | 52 | class DistParam; |
---|
| 53 | class TComPattern; |
---|
| 54 | |
---|
| 55 | // ==================================================================================================================== |
---|
| 56 | // Type definition |
---|
| 57 | // ==================================================================================================================== |
---|
| 58 | |
---|
| 59 | // for function pointer |
---|
[1313] | 60 | typedef Distortion (*FpDistFunc) (DistParam*); // TODO: can this pointer be replaced with a reference? - there are no NULL checks on pointer. |
---|
[2] | 61 | |
---|
[1386] | 62 | |
---|
[2] | 63 | // ==================================================================================================================== |
---|
| 64 | // Class definition |
---|
| 65 | // ==================================================================================================================== |
---|
| 66 | |
---|
| 67 | /// distortion parameter class |
---|
| 68 | class DistParam |
---|
| 69 | { |
---|
| 70 | public: |
---|
[1386] | 71 | const Pel* pOrg; |
---|
| 72 | const Pel* pCur; |
---|
[2] | 73 | Int iStrideOrg; |
---|
| 74 | Int iStrideCur; |
---|
| 75 | Int iRows; |
---|
| 76 | Int iCols; |
---|
| 77 | Int iStep; |
---|
| 78 | FpDistFunc DistFunc; |
---|
[608] | 79 | Int bitDepth; |
---|
[56] | 80 | |
---|
[1313] | 81 | Bool bApplyWeight; // whether weighted prediction is used or not |
---|
[1386] | 82 | Bool bIsBiPred; |
---|
| 83 | |
---|
| 84 | const WPScalingParam *wpCur; // weighted prediction scaling parameters for current ref |
---|
[1313] | 85 | ComponentID compIdx; |
---|
[1386] | 86 | Distortion m_maximumDistortionForEarlyExit; /// During cost calculations, if distortion exceeds this value, cost calculations may early-terminate. |
---|
[2] | 87 | |
---|
| 88 | // (vertical) subsampling shift (for reducing complexity) |
---|
| 89 | // - 0 = no subsampling, 1 = even rows, 2 = every 4th, etc. |
---|
| 90 | Int iSubShift; |
---|
[1313] | 91 | |
---|
[2] | 92 | DistParam() |
---|
[1386] | 93 | : pOrg(NULL), |
---|
| 94 | pCur(NULL), |
---|
| 95 | iStrideOrg(0), |
---|
| 96 | iStrideCur(0), |
---|
| 97 | iRows(0), |
---|
| 98 | iCols(0), |
---|
| 99 | iStep(1), |
---|
| 100 | DistFunc(NULL), |
---|
| 101 | bitDepth(0), |
---|
| 102 | bApplyWeight(false), |
---|
| 103 | bIsBiPred(false), |
---|
| 104 | wpCur(NULL), |
---|
| 105 | compIdx(MAX_NUM_COMPONENT), |
---|
| 106 | m_maximumDistortionForEarlyExit(std::numeric_limits<Distortion>::max()), |
---|
| 107 | iSubShift(0) |
---|
[2] | 108 | { |
---|
| 109 | } |
---|
| 110 | }; |
---|
| 111 | |
---|
| 112 | /// RD cost computation class |
---|
| 113 | class TComRdCost |
---|
| 114 | { |
---|
| 115 | private: |
---|
| 116 | // for distortion |
---|
[1313] | 117 | |
---|
| 118 | FpDistFunc m_afpDistortFunc[DF_TOTAL_FUNCTIONS]; // [eDFunc] |
---|
| 119 | CostMode m_costMode; |
---|
| 120 | Double m_distortionWeight[MAX_NUM_COMPONENT]; // only chroma values are used. |
---|
[2] | 121 | Double m_dLambda; |
---|
| 122 | Double m_sqrtLambda; |
---|
[1313] | 123 | Double m_dLambdaMotionSAD[2 /* 0=standard, 1=for transquant bypass when mixed-lossless cost evaluation enabled*/]; |
---|
| 124 | Double m_dLambdaMotionSSE[2 /* 0=standard, 1=for transquant bypass when mixed-lossless cost evaluation enabled*/]; |
---|
[2] | 125 | Double m_dFrameLambda; |
---|
[1313] | 126 | |
---|
[2] | 127 | // for motion cost |
---|
[56] | 128 | TComMv m_mvPredictor; |
---|
[1386] | 129 | Double m_motionLambda; |
---|
[2] | 130 | Int m_iCostScale; |
---|
[1313] | 131 | |
---|
[2] | 132 | public: |
---|
| 133 | TComRdCost(); |
---|
| 134 | virtual ~TComRdCost(); |
---|
[1386] | 135 | Double calcRdCost( Double numBits, Double distortion, DFunc eDFunc = DF_DEFAULT ); |
---|
[608] | 136 | |
---|
[1386] | 137 | |
---|
[1313] | 138 | Void setDistortionWeight ( const ComponentID compID, const Double distortionWeight ) { m_distortionWeight[compID] = distortionWeight; } |
---|
| 139 | Void setLambda ( Double dLambda, const BitDepths &bitDepths ); |
---|
[2] | 140 | Void setFrameLambda ( Double dLambda ) { m_dFrameLambda = dLambda; } |
---|
[1313] | 141 | |
---|
[608] | 142 | Double getSqrtLambda () { return m_sqrtLambda; } |
---|
[100] | 143 | |
---|
[608] | 144 | Double getLambda() { return m_dLambda; } |
---|
[1313] | 145 | Double getChromaWeight () { return ((m_distortionWeight[COMPONENT_Cb] + m_distortionWeight[COMPONENT_Cr]) / 2.0); } |
---|
| 146 | |
---|
| 147 | Void setCostMode(CostMode m ) { m_costMode = m; } |
---|
| 148 | |
---|
[2] | 149 | // Distortion Functions |
---|
| 150 | Void init(); |
---|
[1313] | 151 | |
---|
[2] | 152 | Void setDistParam( UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc, DistParam& rcDistParam ); |
---|
[1386] | 153 | Void setDistParam( const TComPattern* const pcPatternKey, const Pel* piRefY, Int iRefStride, DistParam& rcDistParam ); |
---|
| 154 | Void setDistParam( const TComPattern* const pcPatternKey, const Pel* piRefY, Int iRefStride, Int iStep, DistParam& rcDistParam, Bool bHADME=false ); |
---|
| 155 | Void setDistParam( DistParam& rcDP, Int bitDepth, const Pel* p1, Int iStride1, const Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard = false ); |
---|
[1313] | 156 | |
---|
| 157 | |
---|
[1386] | 158 | Distortion calcHAD(Int bitDepth, const Pel* pi0, Int iStride0, const Pel* pi1, Int iStride1, Int iWidth, Int iHeight ); |
---|
[1313] | 159 | |
---|
| 160 | |
---|
[2] | 161 | // for motion cost |
---|
[1313] | 162 | static UInt xGetExpGolombNumberOfBits( Int iVal ); |
---|
[1386] | 163 | Void selectMotionLambda( Bool bSad, Int iAdd, Bool bIsTransquantBypass ) { m_motionLambda = (bSad ? m_dLambdaMotionSAD[(bIsTransquantBypass && m_costMode==COST_MIXED_LOSSLESS_LOSSY_CODING) ?1:0] + iAdd : m_dLambdaMotionSSE[(bIsTransquantBypass && m_costMode==COST_MIXED_LOSSLESS_LOSSY_CODING)?1:0] + iAdd); } |
---|
[2] | 164 | Void setPredictor( TComMv& rcMv ) |
---|
| 165 | { |
---|
[56] | 166 | m_mvPredictor = rcMv; |
---|
[2] | 167 | } |
---|
| 168 | Void setCostScale( Int iCostScale ) { m_iCostScale = iCostScale; } |
---|
[1386] | 169 | Distortion getCost( UInt b ) { return Distortion(( m_motionLambda * b ) / 65536.0); } |
---|
| 170 | Distortion getCostOfVectorWithPredictor( const Int x, const Int y ) |
---|
[2] | 171 | { |
---|
[1386] | 172 | return Distortion((m_motionLambda * getBitsOfVectorWithPredictor(x, y)) / 65536.0); |
---|
[2] | 173 | } |
---|
[1386] | 174 | UInt getBitsOfVectorWithPredictor( const Int x, const Int y ) |
---|
[1313] | 175 | { |
---|
| 176 | return xGetExpGolombNumberOfBits((x << m_iCostScale) - m_mvPredictor.getHor()) |
---|
| 177 | + xGetExpGolombNumberOfBits((y << m_iCostScale) - m_mvPredictor.getVer()); |
---|
[56] | 178 | } |
---|
[1313] | 179 | |
---|
[2] | 180 | private: |
---|
[1313] | 181 | |
---|
| 182 | static Distortion xGetSSE ( DistParam* pcDtParam ); |
---|
| 183 | static Distortion xGetSSE4 ( DistParam* pcDtParam ); |
---|
| 184 | static Distortion xGetSSE8 ( DistParam* pcDtParam ); |
---|
| 185 | static Distortion xGetSSE16 ( DistParam* pcDtParam ); |
---|
| 186 | static Distortion xGetSSE32 ( DistParam* pcDtParam ); |
---|
| 187 | static Distortion xGetSSE64 ( DistParam* pcDtParam ); |
---|
| 188 | static Distortion xGetSSE16N ( DistParam* pcDtParam ); |
---|
| 189 | |
---|
| 190 | static Distortion xGetSAD ( DistParam* pcDtParam ); |
---|
| 191 | static Distortion xGetSAD4 ( DistParam* pcDtParam ); |
---|
| 192 | static Distortion xGetSAD8 ( DistParam* pcDtParam ); |
---|
| 193 | static Distortion xGetSAD16 ( DistParam* pcDtParam ); |
---|
| 194 | static Distortion xGetSAD32 ( DistParam* pcDtParam ); |
---|
| 195 | static Distortion xGetSAD64 ( DistParam* pcDtParam ); |
---|
| 196 | static Distortion xGetSAD16N ( DistParam* pcDtParam ); |
---|
[100] | 197 | |
---|
[608] | 198 | |
---|
[1313] | 199 | static Distortion xGetSAD12 ( DistParam* pcDtParam ); |
---|
| 200 | static Distortion xGetSAD24 ( DistParam* pcDtParam ); |
---|
| 201 | static Distortion xGetSAD48 ( DistParam* pcDtParam ); |
---|
[2] | 202 | |
---|
[1313] | 203 | |
---|
| 204 | |
---|
| 205 | static Distortion xGetHADs ( DistParam* pcDtParam ); |
---|
[1386] | 206 | static Distortion xCalcHADs2x2 ( const Pel *piOrg, const Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
| 207 | static Distortion xCalcHADs4x4 ( const Pel *piOrg, const Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
| 208 | static Distortion xCalcHADs8x8 ( const Pel *piOrg, const Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep ); |
---|
[1313] | 209 | |
---|
| 210 | |
---|
[2] | 211 | public: |
---|
[608] | 212 | |
---|
[1386] | 213 | Distortion getDistPart(Int bitDepth, const Pel* piCur, Int iCurStride, const Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, const ComponentID compID, DFunc eDFunc = DF_SSE ); |
---|
[1313] | 214 | |
---|
[100] | 215 | |
---|
[608] | 216 | |
---|
[2] | 217 | |
---|
| 218 | };// END CLASS DEFINITION TComRdCost |
---|
| 219 | |
---|
[56] | 220 | //! \} |
---|
[2] | 221 | |
---|
| 222 | #endif // __TCOMRDCOST__ |
---|