[313] | 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 |
---|
[1029] | 4 | * granted under this license. |
---|
[313] | 5 | * |
---|
[1259] | 6 | * Copyright (c) 2010-2015, ITU/ISO/IEC |
---|
[313] | 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 TComTrQuant.h |
---|
| 35 | \brief transform and quantization class (header) |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #ifndef __TCOMTRQUANT__ |
---|
| 39 | #define __TCOMTRQUANT__ |
---|
| 40 | |
---|
| 41 | #include "CommonDef.h" |
---|
| 42 | #include "TComYuv.h" |
---|
| 43 | #include "TComDataCU.h" |
---|
[1029] | 44 | #include "TComChromaFormat.h" |
---|
[313] | 45 | #include "ContextTables.h" |
---|
| 46 | |
---|
| 47 | //! \ingroup TLibCommon |
---|
| 48 | //! \{ |
---|
| 49 | |
---|
| 50 | // ==================================================================================================================== |
---|
| 51 | // Constants |
---|
| 52 | // ==================================================================================================================== |
---|
| 53 | |
---|
| 54 | #define QP_BITS 15 |
---|
| 55 | |
---|
| 56 | // ==================================================================================================================== |
---|
| 57 | // Type definition |
---|
| 58 | // ==================================================================================================================== |
---|
| 59 | |
---|
| 60 | typedef struct |
---|
| 61 | { |
---|
[1029] | 62 | Int significantCoeffGroupBits[NUM_SIG_CG_FLAG_CTX][2 /*Flag = [0|1]*/]; |
---|
| 63 | Int significantBits[NUM_SIG_FLAG_CTX][2 /*Flag = [0|1]*/]; |
---|
| 64 | Int lastXBits[MAX_NUM_CHANNEL_TYPE][LAST_SIGNIFICANT_GROUPS]; |
---|
| 65 | Int lastYBits[MAX_NUM_CHANNEL_TYPE][LAST_SIGNIFICANT_GROUPS]; |
---|
| 66 | Int m_greaterOneBits[NUM_ONE_FLAG_CTX][2 /*Flag = [0|1]*/]; |
---|
| 67 | Int m_levelAbsBits[NUM_ABS_FLAG_CTX][2 /*Flag = [0|1]*/]; |
---|
[313] | 68 | |
---|
[1029] | 69 | Int blockCbpBits[NUM_QT_CBF_CTX_SETS * NUM_QT_CBF_CTX_PER_SET][2 /*Flag = [0|1]*/]; |
---|
| 70 | Int blockRootCbpBits[4][2 /*Flag = [0|1]*/]; |
---|
| 71 | |
---|
| 72 | Int golombRiceAdaptationStatistics[RExt__GOLOMB_RICE_ADAPTATION_STATISTICS_SETS]; |
---|
[313] | 73 | } estBitsSbacStruct; |
---|
| 74 | |
---|
| 75 | // ==================================================================================================================== |
---|
| 76 | // Class definition |
---|
| 77 | // ==================================================================================================================== |
---|
| 78 | |
---|
[1029] | 79 | /// QP struct |
---|
| 80 | struct QpParam |
---|
[313] | 81 | { |
---|
[1029] | 82 | Int Qp; |
---|
| 83 | Int per; |
---|
| 84 | Int rem; |
---|
[313] | 85 | |
---|
[1029] | 86 | QpParam(const Int qpy, |
---|
| 87 | const ChannelType chType, |
---|
| 88 | const Int qpBdOffset, |
---|
| 89 | const Int chromaQPOffset, |
---|
| 90 | const ChromaFormat chFmt ); |
---|
| 91 | |
---|
| 92 | QpParam(const TComDataCU &cu, const ComponentID compID); |
---|
| 93 | |
---|
| 94 | }; // END STRUCT DEFINITION QpParam |
---|
| 95 | |
---|
| 96 | |
---|
[313] | 97 | /// transform and quantization class |
---|
| 98 | class TComTrQuant |
---|
| 99 | { |
---|
| 100 | public: |
---|
| 101 | TComTrQuant(); |
---|
| 102 | ~TComTrQuant(); |
---|
[1029] | 103 | |
---|
[313] | 104 | // initialize class |
---|
[1029] | 105 | Void init ( UInt uiMaxTrSize, |
---|
| 106 | Bool useRDOQ = false, |
---|
| 107 | Bool useRDOQTS = false, |
---|
[1298] | 108 | #if T0196_SELECTIVE_RDOQ |
---|
| 109 | Bool useSelectiveRDOQ = false, |
---|
| 110 | #endif |
---|
[1029] | 111 | Bool bEnc = false, |
---|
| 112 | Bool useTransformSkipFast = false |
---|
[313] | 113 | #if ADAPTIVE_QP_SELECTION |
---|
[1029] | 114 | , Bool bUseAdaptQpSelect = false |
---|
| 115 | #endif |
---|
| 116 | ); |
---|
| 117 | |
---|
[313] | 118 | // transform & inverse transform functions |
---|
[1029] | 119 | Void transformNxN( TComTU & rTu, |
---|
| 120 | const ComponentID compID, |
---|
| 121 | Pel * pcResidual, |
---|
| 122 | const UInt uiStride, |
---|
| 123 | TCoeff * rpcCoeff, |
---|
[313] | 124 | #if ADAPTIVE_QP_SELECTION |
---|
[1029] | 125 | TCoeff * rpcArlCoeff, |
---|
[313] | 126 | #endif |
---|
[1029] | 127 | TCoeff & uiAbsSum, |
---|
| 128 | const QpParam & cQP |
---|
| 129 | ); |
---|
[313] | 130 | |
---|
[1029] | 131 | |
---|
| 132 | Void invTransformNxN( TComTU & rTu, |
---|
| 133 | const ComponentID compID, |
---|
| 134 | Pel *pcResidual, |
---|
| 135 | const UInt uiStride, |
---|
| 136 | TCoeff * pcCoeff, |
---|
| 137 | const QpParam & cQP |
---|
| 138 | DEBUG_STRING_FN_DECLAREP(psDebug)); |
---|
| 139 | |
---|
| 140 | Void invRecurTransformNxN ( const ComponentID compID, TComYuv *pResidual, TComTU &rTu ); |
---|
| 141 | |
---|
| 142 | Void rdpcmNxN ( TComTU& rTu, const ComponentID compID, Pel* pcResidual, const UInt uiStride, const QpParam& cQP, TCoeff* pcCoeff, TCoeff &uiAbsSum, RDPCMMode& rdpcmMode ); |
---|
| 143 | Void invRdpcmNxN( TComTU& rTu, const ComponentID compID, Pel* pcResidual, const UInt uiStride ); |
---|
| 144 | |
---|
| 145 | Void applyForwardRDPCM( TComTU& rTu, const ComponentID compID, Pel* pcResidual, const UInt uiStride, const QpParam& cQP, TCoeff* pcCoeff, TCoeff &uiAbsSum, const RDPCMMode mode ); |
---|
| 146 | |
---|
[313] | 147 | // Misc functions |
---|
| 148 | |
---|
[1029] | 149 | #if RDOQ_CHROMA_LAMBDA |
---|
| 150 | Void setLambdas(const Double lambdas[MAX_NUM_COMPONENT]) { for (UInt component = 0; component < MAX_NUM_COMPONENT; component++) m_lambdas[component] = lambdas[component]; } |
---|
| 151 | Void selectLambda(const ComponentID compIdx) { m_dLambda = m_lambdas[compIdx]; } |
---|
[313] | 152 | #else |
---|
| 153 | Void setLambda(Double dLambda) { m_dLambda = dLambda;} |
---|
| 154 | #endif |
---|
| 155 | Void setRDOQOffset( UInt uiRDOQOffset ) { m_uiRDOQOffset = uiRDOQOffset; } |
---|
[1029] | 156 | |
---|
[313] | 157 | estBitsSbacStruct* m_pcEstBitsSbac; |
---|
| 158 | |
---|
[1029] | 159 | static Int calcPatternSigCtx( const UInt* sigCoeffGroupFlag, UInt uiCGPosX, UInt uiCGPosY, UInt widthInGroups, UInt heightInGroups ); |
---|
| 160 | |
---|
| 161 | static Int getSigCtxInc ( Int patternSigCtx, |
---|
| 162 | const TUEntropyCodingParameters &codingParameters, |
---|
| 163 | const Int scanPosition, |
---|
| 164 | const Int log2BlockWidth, |
---|
| 165 | const Int log2BlockHeight, |
---|
| 166 | const ChannelType chanType |
---|
[313] | 167 | ); |
---|
[1029] | 168 | |
---|
| 169 | static UInt getSigCoeffGroupCtxInc (const UInt* uiSigCoeffGroupFlag, |
---|
| 170 | const UInt uiCGPosX, |
---|
| 171 | const UInt uiCGPosY, |
---|
| 172 | const UInt widthInGroups, |
---|
| 173 | const UInt heightInGroups); |
---|
| 174 | |
---|
[313] | 175 | Void initScalingList (); |
---|
| 176 | Void destroyScalingList (); |
---|
[1287] | 177 | Void setErrScaleCoeff ( UInt list, UInt size, Int qp, const Int maxLog2TrDynamicRange[MAX_NUM_CHANNEL_TYPE], const BitDepths &bitDepths ); |
---|
[1029] | 178 | Double* getErrScaleCoeff ( UInt list, UInt size, Int qp ) { return m_errScale [size][list][qp]; }; //!< get Error Scale Coefficent |
---|
| 179 | Double& getErrScaleCoeffNoScalingList ( UInt list, UInt size, Int qp ) { return m_errScaleNoScalingList[size][list][qp]; }; //!< get Error Scale Coefficent |
---|
| 180 | Int* getQuantCoeff ( UInt list, Int qp, UInt size ) { return m_quantCoef [size][list][qp]; }; //!< get Quant Coefficent |
---|
| 181 | Int* getDequantCoeff ( UInt list, Int qp, UInt size ) { return m_dequantCoef [size][list][qp]; }; //!< get DeQuant Coefficent |
---|
[313] | 182 | Void setUseScalingList ( Bool bUseScalingList){ m_scalingListEnabledFlag = bUseScalingList; }; |
---|
[1029] | 183 | Bool getUseScalingList (const UInt width, const UInt height, const Bool isTransformSkip){ return m_scalingListEnabledFlag && (!isTransformSkip || ((width == 4) && (height == 4))); }; |
---|
[1307] | 184 | Void setFlatScalingList (const Int maxLog2TrDynamicRange[MAX_NUM_CHANNEL_TYPE], const BitDepths &bitDepths); |
---|
| 185 | Void xsetFlatScalingList ( UInt list, UInt size, Int qp); |
---|
| 186 | Void xSetScalingListEnc ( TComScalingList *scalingList, UInt list, UInt size, Int qp); |
---|
| 187 | Void xSetScalingListDec ( const TComScalingList &scalingList, UInt list, UInt size, Int qp); |
---|
| 188 | Void setScalingList ( TComScalingList *scalingList, const Int maxLog2TrDynamicRange[MAX_NUM_CHANNEL_TYPE], const BitDepths &bitDepths); |
---|
| 189 | Void setScalingListDec ( const TComScalingList &scalingList); |
---|
[313] | 190 | Void processScalingListEnc( Int *coeff, Int *quantcoeff, Int quantScales, UInt height, UInt width, UInt ratio, Int sizuNum, UInt dc); |
---|
[1235] | 191 | Void processScalingListDec( const Int *coeff, Int *dequantcoeff, Int invQuantScales, UInt height, UInt width, UInt ratio, Int sizuNum, UInt dc); |
---|
[313] | 192 | #if ADAPTIVE_QP_SELECTION |
---|
| 193 | Void initSliceQpDelta() ; |
---|
| 194 | Void storeSliceQpNext(TComSlice* pcSlice); |
---|
| 195 | Void clearSliceARLCnt(); |
---|
[1029] | 196 | Int getQpDelta(Int qp) { return m_qpDelta[qp]; } |
---|
| 197 | Int* getSliceNSamples(){ return m_sliceNsamples ;} |
---|
[313] | 198 | Double* getSliceSumC() { return m_sliceSumC; } |
---|
| 199 | #endif |
---|
[1240] | 200 | Void transformSkipQuantOneSample(TComTU &rTu, const ComponentID compID, const TCoeff resiDiff, TCoeff* pcCoeff, const UInt uiPos, const QpParam &cQP, const Bool bUseHalfRoundingPoint); |
---|
[1029] | 201 | Void invTrSkipDeQuantOneSample(TComTU &rTu, ComponentID compID, TCoeff pcCoeff, Pel &reconSample, const QpParam &cQP, UInt uiPos ); |
---|
| 202 | |
---|
[313] | 203 | protected: |
---|
| 204 | #if ADAPTIVE_QP_SELECTION |
---|
[1029] | 205 | Int m_qpDelta[MAX_QP+1]; |
---|
| 206 | Int m_sliceNsamples[LEVEL_RANGE+1]; |
---|
| 207 | Double m_sliceSumC[LEVEL_RANGE+1] ; |
---|
[313] | 208 | #endif |
---|
[1029] | 209 | TCoeff* m_plTempCoeff; |
---|
| 210 | |
---|
| 211 | // QpParam m_cQP; - removed - placed on the stack. |
---|
[313] | 212 | #if RDOQ_CHROMA_LAMBDA |
---|
[1029] | 213 | Double m_lambdas[MAX_NUM_COMPONENT]; |
---|
[313] | 214 | #endif |
---|
| 215 | Double m_dLambda; |
---|
| 216 | UInt m_uiRDOQOffset; |
---|
| 217 | UInt m_uiMaxTrSize; |
---|
| 218 | Bool m_bEnc; |
---|
| 219 | Bool m_useRDOQ; |
---|
| 220 | Bool m_useRDOQTS; |
---|
[1298] | 221 | #if T0196_SELECTIVE_RDOQ |
---|
| 222 | Bool m_useSelectiveRDOQ; |
---|
| 223 | #endif |
---|
[313] | 224 | #if ADAPTIVE_QP_SELECTION |
---|
| 225 | Bool m_bUseAdaptQpSelect; |
---|
| 226 | #endif |
---|
| 227 | Bool m_useTransformSkipFast; |
---|
[1029] | 228 | |
---|
[313] | 229 | Bool m_scalingListEnabledFlag; |
---|
[1029] | 230 | |
---|
| 231 | Int *m_quantCoef [SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][SCALING_LIST_REM_NUM]; ///< array of quantization matrix coefficient 4x4 |
---|
| 232 | Int *m_dequantCoef [SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][SCALING_LIST_REM_NUM]; ///< array of dequantization matrix coefficient 4x4 |
---|
| 233 | Double *m_errScale [SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][SCALING_LIST_REM_NUM]; ///< array of quantization matrix coefficient 4x4 |
---|
| 234 | Double m_errScaleNoScalingList[SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][SCALING_LIST_REM_NUM]; ///< array of quantization matrix coefficient 4x4 |
---|
| 235 | |
---|
[313] | 236 | private: |
---|
| 237 | // forward Transform |
---|
[1287] | 238 | Void xT ( const Int channelBitDepth, Bool useDST, Pel* piBlkResi, UInt uiStride, TCoeff* psCoeff, Int iWidth, Int iHeight, const Int maxLog2TrDynamicRange ); |
---|
[1029] | 239 | |
---|
[313] | 240 | // skipping Transform |
---|
[1029] | 241 | Void xTransformSkip ( Pel* piBlkResi, UInt uiStride, TCoeff* psCoeff, TComTU &rTu, const ComponentID component ); |
---|
[313] | 242 | |
---|
[1307] | 243 | Void signBitHidingHDQ( TCoeff* pQCoef, TCoeff* pCoef, TCoeff* deltaU, const TUEntropyCodingParameters &codingParameters, const Int maxLog2TrDynamicRange ); |
---|
[313] | 244 | |
---|
| 245 | // quantization |
---|
[1029] | 246 | Void xQuant( TComTU &rTu, |
---|
| 247 | TCoeff * pSrc, |
---|
| 248 | TCoeff * pDes, |
---|
[313] | 249 | #if ADAPTIVE_QP_SELECTION |
---|
[1029] | 250 | TCoeff *pArlDes, |
---|
[313] | 251 | #endif |
---|
[1029] | 252 | TCoeff &uiAbsSum, |
---|
| 253 | const ComponentID compID, |
---|
| 254 | const QpParam &cQP ); |
---|
[313] | 255 | |
---|
[1298] | 256 | #if T0196_SELECTIVE_RDOQ |
---|
| 257 | Bool xNeedRDOQ( TComTU &rTu, |
---|
| 258 | TCoeff * pSrc, |
---|
| 259 | const ComponentID compID, |
---|
| 260 | const QpParam &cQP ); |
---|
| 261 | #endif |
---|
| 262 | |
---|
[313] | 263 | // RDOQ functions |
---|
[1029] | 264 | |
---|
| 265 | Void xRateDistOptQuant ( TComTU &rTu, |
---|
| 266 | TCoeff * plSrcCoeff, |
---|
| 267 | TCoeff * piDstCoeff, |
---|
[313] | 268 | #if ADAPTIVE_QP_SELECTION |
---|
[1029] | 269 | TCoeff *piArlDstCoeff, |
---|
[313] | 270 | #endif |
---|
[1029] | 271 | TCoeff &uiAbsSum, |
---|
| 272 | const ComponentID compID, |
---|
| 273 | const QpParam &cQP ); |
---|
| 274 | |
---|
| 275 | __inline UInt xGetCodedLevel ( Double& rd64CodedCost, |
---|
| 276 | Double& rd64CodedCost0, |
---|
| 277 | Double& rd64CodedCostSig, |
---|
| 278 | Intermediate_Int lLevelDouble, |
---|
| 279 | UInt uiMaxAbsLevel, |
---|
| 280 | UShort ui16CtxNumSig, |
---|
| 281 | UShort ui16CtxNumOne, |
---|
| 282 | UShort ui16CtxNumAbs, |
---|
| 283 | UShort ui16AbsGoRice, |
---|
| 284 | UInt c1Idx, |
---|
| 285 | UInt c2Idx, |
---|
| 286 | Int iQBits, |
---|
| 287 | Double errorScale, |
---|
| 288 | Bool bLast, |
---|
| 289 | Bool useLimitedPrefixLength, |
---|
[1303] | 290 | const Int maxLog2TrDynamicRange |
---|
[1029] | 291 | ) const; |
---|
| 292 | |
---|
| 293 | |
---|
[1285] | 294 | __inline Int xGetICRate ( const UInt uiAbsLevel, |
---|
| 295 | const UShort ui16CtxNumOne, |
---|
| 296 | const UShort ui16CtxNumAbs, |
---|
| 297 | const UShort ui16AbsGoRice, |
---|
| 298 | const UInt c1Idx, |
---|
| 299 | const UInt c2Idx, |
---|
| 300 | const Bool useLimitedPrefixLength, |
---|
| 301 | const Int maxLog2TrDynamicRange |
---|
[1029] | 302 | ) const; |
---|
| 303 | |
---|
| 304 | __inline Double xGetRateLast ( const UInt uiPosX, const UInt uiPosY, const ComponentID component ) const; |
---|
| 305 | __inline Double xGetRateSigCoeffGroup( UShort uiSignificanceCoeffGroup, UShort ui16CtxNumSig ) const; |
---|
| 306 | __inline Double xGetRateSigCoef ( UShort uiSignificance, UShort ui16CtxNumSig ) const; |
---|
| 307 | __inline Double xGetICost ( Double dRate ) const; |
---|
| 308 | __inline Double xGetIEPRate ( ) const; |
---|
| 309 | |
---|
| 310 | |
---|
[313] | 311 | // dequantization |
---|
[1029] | 312 | Void xDeQuant( TComTU &rTu, |
---|
| 313 | const TCoeff * pSrc, |
---|
| 314 | TCoeff * pDes, |
---|
| 315 | const ComponentID compID, |
---|
| 316 | const QpParam &cQP ); |
---|
| 317 | |
---|
[313] | 318 | // inverse transform |
---|
[1287] | 319 | Void xIT ( const Int channelBitDepth, Bool useDST, TCoeff* plCoef, Pel* pResidual, UInt uiStride, Int iWidth, Int iHeight, const Int maxLog2TrDynamicRange ); |
---|
[1029] | 320 | |
---|
[313] | 321 | // inverse skipping transform |
---|
[1029] | 322 | Void xITransformSkip ( TCoeff* plCoef, Pel* pResidual, UInt uiStride, TComTU &rTu, const ComponentID component ); |
---|
| 323 | |
---|
| 324 | public: |
---|
| 325 | static Void crossComponentPrediction( TComTU &rTu, |
---|
| 326 | const ComponentID compID, |
---|
| 327 | const Pel *piResiL, |
---|
| 328 | const Pel *piResiC, |
---|
| 329 | Pel *piResiT, |
---|
| 330 | const Int width, |
---|
| 331 | const Int height, |
---|
| 332 | const Int strideL, |
---|
| 333 | const Int strideC, |
---|
| 334 | const Int strideT, |
---|
| 335 | const Bool reverse); |
---|
| 336 | |
---|
[313] | 337 | };// END CLASS DEFINITION TComTrQuant |
---|
| 338 | |
---|
| 339 | //! \} |
---|
| 340 | |
---|
| 341 | #endif // __TCOMTRQUANT__ |
---|