[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 |
---|
[56] | 4 | * granted under this license. |
---|
[5] | 5 | * |
---|
[56] | 6 | * Copyright (c) 2010-2012, 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 | */ |
---|
[2] | 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" |
---|
[56] | 44 | #include "ContextTables.h" |
---|
[2] | 45 | |
---|
[56] | 46 | //! \ingroup TLibCommon |
---|
| 47 | //! \{ |
---|
| 48 | |
---|
[2] | 49 | // ==================================================================================================================== |
---|
| 50 | // Constants |
---|
| 51 | // ==================================================================================================================== |
---|
| 52 | |
---|
| 53 | #define QP_BITS 15 |
---|
| 54 | |
---|
| 55 | // ==================================================================================================================== |
---|
| 56 | // Type definition |
---|
| 57 | // ==================================================================================================================== |
---|
| 58 | |
---|
| 59 | typedef struct |
---|
| 60 | { |
---|
[56] | 61 | Int significantCoeffGroupBits[NUM_SIG_CG_FLAG_CTX][2]; |
---|
| 62 | Int significantBits[NUM_SIG_FLAG_CTX][2]; |
---|
[2] | 63 | Int lastXBits[32]; |
---|
| 64 | Int lastYBits[32]; |
---|
[56] | 65 | Int m_greaterOneBits[NUM_ONE_FLAG_CTX][2]; |
---|
| 66 | Int m_levelAbsBits[NUM_ABS_FLAG_CTX][2]; |
---|
| 67 | |
---|
| 68 | Int blockCbpBits[3*NUM_QT_CBF_CTX][2]; |
---|
[2] | 69 | Int blockRootCbpBits[4][2]; |
---|
| 70 | Int scanZigzag[2]; ///< flag for zigzag scan |
---|
| 71 | Int scanNonZigzag[2]; ///< flag for non zigzag scan |
---|
| 72 | } estBitsSbacStruct; |
---|
| 73 | |
---|
| 74 | // ==================================================================================================================== |
---|
| 75 | // Class definition |
---|
| 76 | // ==================================================================================================================== |
---|
| 77 | |
---|
| 78 | /// QP class |
---|
| 79 | class QpParam |
---|
| 80 | { |
---|
| 81 | public: |
---|
| 82 | QpParam(); |
---|
| 83 | |
---|
| 84 | Int m_iQP; |
---|
| 85 | Int m_iPer; |
---|
| 86 | Int m_iRem; |
---|
| 87 | |
---|
| 88 | public: |
---|
| 89 | Int m_iBits; |
---|
[56] | 90 | |
---|
| 91 | #if H0736_AVC_STYLE_QP_RANGE |
---|
| 92 | Void setQpParam( Int qpScaled, Bool bLowpass, SliceType eSliceType ) |
---|
[2] | 93 | { |
---|
[56] | 94 | m_iQP = qpScaled; |
---|
| 95 | m_iPer = qpScaled / 6; |
---|
| 96 | m_iRem = qpScaled % 6; |
---|
| 97 | m_iBits = QP_BITS + m_iPer; |
---|
[2] | 98 | } |
---|
[56] | 99 | #else |
---|
| 100 | Void setQpParam( Int iQP, Bool bLowpass, SliceType eSliceType ) |
---|
[2] | 101 | { |
---|
| 102 | assert ( iQP >= MIN_QP && iQP <= MAX_QP ); |
---|
| 103 | m_iQP = iQP; |
---|
| 104 | |
---|
| 105 | m_iPer = (iQP + 6*g_uiBitIncrement)/6; |
---|
| 106 | #if FULL_NBIT |
---|
| 107 | m_iPer += g_uiBitDepth - 8; |
---|
| 108 | #endif |
---|
[56] | 109 | m_iRem = iQP % 6; |
---|
[2] | 110 | |
---|
| 111 | m_iBits = QP_BITS + m_iPer; |
---|
| 112 | } |
---|
[56] | 113 | #endif |
---|
[2] | 114 | |
---|
| 115 | Void clear() |
---|
| 116 | { |
---|
| 117 | m_iQP = 0; |
---|
| 118 | m_iPer = 0; |
---|
| 119 | m_iRem = 0; |
---|
| 120 | m_iBits = 0; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | |
---|
[56] | 124 | Int per() const { return m_iPer; } |
---|
| 125 | Int rem() const { return m_iRem; } |
---|
| 126 | Int bits() const { return m_iBits; } |
---|
[2] | 127 | |
---|
| 128 | Int qp() {return m_iQP;} |
---|
| 129 | }; // END CLASS DEFINITION QpParam |
---|
| 130 | |
---|
| 131 | /// transform and quantization class |
---|
| 132 | class TComTrQuant |
---|
| 133 | { |
---|
| 134 | public: |
---|
| 135 | TComTrQuant(); |
---|
| 136 | ~TComTrQuant(); |
---|
| 137 | |
---|
| 138 | // initialize class |
---|
[56] | 139 | Void init ( UInt uiMaxWidth, UInt uiMaxHeight, UInt uiMaxTrSize, Int iSymbolMode = 0, UInt *aTable4 = NULL, UInt *aTable8 = NULL, UInt *aTableLastPosVlcIndex=NULL, Bool bUseRDOQ = false, Bool bEnc = false |
---|
| 140 | #if ADAPTIVE_QP_SELECTION |
---|
| 141 | , Bool bUseAdaptQpSelect = false |
---|
| 142 | #endif |
---|
| 143 | ); |
---|
[2] | 144 | |
---|
| 145 | // transform & inverse transform functions |
---|
[56] | 146 | Void transformNxN( TComDataCU* pcCU, |
---|
| 147 | Pel* pcResidual, |
---|
| 148 | UInt uiStride, |
---|
| 149 | TCoeff* rpcCoeff, |
---|
| 150 | #if ADAPTIVE_QP_SELECTION |
---|
| 151 | Int*& rpcArlCoeff, |
---|
| 152 | #endif |
---|
| 153 | UInt uiWidth, |
---|
| 154 | UInt uiHeight, |
---|
| 155 | UInt& uiAbsSum, |
---|
| 156 | TextType eTType, |
---|
| 157 | UInt uiAbsPartIdx ); |
---|
| 158 | #if LOSSLESS_CODING |
---|
| 159 | Void invtransformNxN( TComDataCU* pcCU, TextType eText, UInt uiMode,Pel* rpcResidual, UInt uiStride, TCoeff* pcCoeff, UInt uiWidth, UInt uiHeight, Int scalingListType); |
---|
[2] | 160 | #else |
---|
[56] | 161 | Void invtransformNxN( TextType eText, UInt uiMode,Pel*& rpcResidual, UInt uiStride, TCoeff* pcCoeff, UInt uiWidth, UInt uiHeight, Int scalingListType); |
---|
[2] | 162 | #endif |
---|
[56] | 163 | Void invRecurTransformNxN ( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eTxt, Pel* rpcResidual, UInt uiAddr, UInt uiStride, UInt uiWidth, UInt uiHeight, |
---|
[2] | 164 | UInt uiMaxTrMode, UInt uiTrMode, TCoeff* rpcCoeff ); |
---|
| 165 | |
---|
| 166 | // Misc functions |
---|
[56] | 167 | #if H0736_AVC_STYLE_QP_RANGE |
---|
| 168 | Void setQPforQuant( Int qpy, Bool bLowpass, SliceType eSliceType, TextType eTxtType, Int qpBdOffset, Int chromaQPOffset); |
---|
| 169 | #else |
---|
| 170 | Void setQPforQuant( Int iQP, Bool bLowpass, SliceType eSliceType, TextType eTxtType, Int Shift); |
---|
| 171 | #endif |
---|
| 172 | |
---|
| 173 | #if RDOQ_CHROMA_LAMBDA |
---|
| 174 | Void setLambda(Double dLambdaLuma, Double dLambdaChroma) { m_dLambdaLuma = dLambdaLuma; m_dLambdaChroma = dLambdaChroma; } |
---|
| 175 | Void selectLambda(TextType eTType) { m_dLambda = (eTType == TEXT_LUMA) ? m_dLambdaLuma : m_dLambdaChroma; } |
---|
| 176 | #else |
---|
[2] | 177 | Void setLambda(Double dLambda) { m_dLambda = dLambda;} |
---|
| 178 | #endif |
---|
[56] | 179 | Void setRDOQOffset( UInt uiRDOQOffset ) { m_uiRDOQOffset = uiRDOQOffset; } |
---|
| 180 | |
---|
[2] | 181 | estBitsSbacStruct* m_pcEstBitsSbac; |
---|
| 182 | |
---|
[56] | 183 | static Int getSigCtxInc ( TCoeff* pcCoeff, |
---|
| 184 | Int posX, |
---|
| 185 | Int posY, |
---|
| 186 | Int blockType, |
---|
| 187 | Int width |
---|
| 188 | ,Int height |
---|
| 189 | ,TextType textureType |
---|
| 190 | ); |
---|
| 191 | static UInt getSigCoeffGroupCtxInc ( const UInt* uiSigCoeffGroupFlag, |
---|
| 192 | const UInt uiCGPosX, |
---|
| 193 | const UInt uiCGPosY, |
---|
| 194 | #if MULTILEVEL_SIGMAP_EXT |
---|
| 195 | const UInt scanIdx, |
---|
[2] | 196 | #endif |
---|
[56] | 197 | Int width, Int height); |
---|
| 198 | #if !REMOVE_INFER_SIGGRP |
---|
| 199 | static Bool bothCGNeighboursOne ( const UInt* uiSigCoeffGroupFlag, |
---|
| 200 | const UInt uiCGPosX, |
---|
| 201 | const UInt uiCGPosY, |
---|
| 202 | #if MULTILEVEL_SIGMAP_EXT |
---|
| 203 | const UInt scanIdx, |
---|
| 204 | #endif |
---|
| 205 | Int width, Int height); |
---|
| 206 | #endif |
---|
| 207 | Void initScalingList (); |
---|
| 208 | Void destroyScalingList (); |
---|
| 209 | Void setErrScaleCoeff ( UInt list, UInt size, UInt qp, UInt dir); |
---|
| 210 | double* getErrScaleCoeff ( UInt list, UInt size, UInt qp, UInt dir) {return m_errScale[size][list][qp][dir];}; //!< get Error Scale Coefficent |
---|
| 211 | Int* getQuantCoeff ( UInt list, UInt qp, UInt size, UInt dir) {return m_quantCoef[size][list][qp][dir];}; //!< get Quant Coefficent |
---|
| 212 | Int* getDequantCoeff ( UInt list, UInt qp, UInt size, UInt dir) {return m_dequantCoef[size][list][qp][dir];}; //!< get DeQuant Coefficent |
---|
| 213 | Void setUseScalingList ( Bool bUseScalingList){ m_scalingListEnabledFlag = bUseScalingList; }; |
---|
| 214 | Bool getUseScalingList (){ return m_scalingListEnabledFlag; }; |
---|
| 215 | Void setFlatScalingList (); |
---|
| 216 | Void xsetFlatScalingList ( UInt list, UInt size, UInt qp); |
---|
| 217 | Void xSetScalingListEnc ( TComScalingList *scalingList, UInt list, UInt size, UInt qp); |
---|
| 218 | Void xSetScalingListDec ( TComScalingList *scalingList, UInt list, UInt size, UInt qp); |
---|
| 219 | Void setScalingList ( TComScalingList *scalingList); |
---|
| 220 | Void setScalingListDec ( TComScalingList *scalingList); |
---|
| 221 | Void processScalingListEnc( Int *coeff, Int *quantcoeff, Int quantScales, UInt height, UInt width, UInt ratio, Int sizuNum, UInt dc); |
---|
| 222 | Void processScalingListDec( Int *coeff, Int *dequantcoeff, Int invQuantScales, UInt height, UInt width, UInt ratio, Int sizuNum, UInt dc); |
---|
| 223 | #if ADAPTIVE_QP_SELECTION |
---|
| 224 | Void initSliceQpDelta() ; |
---|
| 225 | Void storeSliceQpNext(TComSlice* pcSlice); |
---|
| 226 | Void clearSliceARLCnt(); |
---|
| 227 | Int getQpDelta(Int qp) { return m_qpDelta[qp]; } |
---|
| 228 | Int* getSliceNSamples(){ return m_sliceNsamples ;} |
---|
| 229 | Double* getSliceSumC() { return m_sliceSumC; } |
---|
| 230 | #endif |
---|
[2] | 231 | protected: |
---|
[56] | 232 | #if ADAPTIVE_QP_SELECTION |
---|
| 233 | Int m_qpDelta[MAX_QP+1]; |
---|
| 234 | Int m_sliceNsamples[LEVEL_RANGE+1]; |
---|
| 235 | Double m_sliceSumC[LEVEL_RANGE+1] ; |
---|
| 236 | #endif |
---|
| 237 | Int* m_plTempCoeff; |
---|
[2] | 238 | |
---|
| 239 | QpParam m_cQP; |
---|
[56] | 240 | #if RDOQ_CHROMA_LAMBDA |
---|
| 241 | Double m_dLambdaLuma; |
---|
| 242 | Double m_dLambdaChroma; |
---|
| 243 | #endif |
---|
[2] | 244 | Double m_dLambda; |
---|
| 245 | UInt m_uiRDOQOffset; |
---|
| 246 | UInt m_uiMaxTrSize; |
---|
| 247 | Bool m_bEnc; |
---|
| 248 | Bool m_bUseRDOQ; |
---|
[56] | 249 | #if ADAPTIVE_QP_SELECTION |
---|
| 250 | Bool m_bUseAdaptQpSelect; |
---|
[2] | 251 | #endif |
---|
[56] | 252 | |
---|
| 253 | Bool m_scalingListEnabledFlag; |
---|
| 254 | Int *m_quantCoef [SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][SCALING_LIST_REM_NUM][SCALING_LIST_DIR_NUM]; ///< array of quantization matrix coefficient 4x4 |
---|
| 255 | Int *m_dequantCoef [SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][SCALING_LIST_REM_NUM][SCALING_LIST_DIR_NUM]; ///< array of dequantization matrix coefficient 4x4 |
---|
| 256 | double *m_errScale [SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][SCALING_LIST_REM_NUM][SCALING_LIST_DIR_NUM]; ///< array of quantization matrix coefficient 4x4 |
---|
[2] | 257 | private: |
---|
| 258 | // forward Transform |
---|
[56] | 259 | Void xT ( UInt uiMode,Pel* pResidual, UInt uiStride, Int* plCoeff, Int iWidth, Int iHeight ); |
---|
| 260 | |
---|
| 261 | #if MULTIBITS_DATA_HIDING |
---|
| 262 | Void signBitHidingHDQ( TComDataCU* pcCU, TCoeff* pQCoef, TCoeff* pCoef, UInt const *scan, Int* deltaU, Int width, Int height ); |
---|
[2] | 263 | #endif |
---|
[56] | 264 | |
---|
[2] | 265 | // quantization |
---|
[56] | 266 | Void xQuant( TComDataCU* pcCU, |
---|
| 267 | Int* pSrc, |
---|
| 268 | TCoeff* pDes, |
---|
| 269 | #if ADAPTIVE_QP_SELECTION |
---|
| 270 | Int*& pArlDes, |
---|
| 271 | #endif |
---|
| 272 | Int iWidth, |
---|
| 273 | Int iHeight, |
---|
| 274 | UInt& uiAcSum, |
---|
| 275 | TextType eTType, |
---|
| 276 | UInt uiAbsPartIdx ); |
---|
[2] | 277 | |
---|
| 278 | // RDOQ functions |
---|
| 279 | |
---|
| 280 | Void xRateDistOptQuant ( TComDataCU* pcCU, |
---|
[56] | 281 | Int* plSrcCoeff, |
---|
| 282 | TCoeff* piDstCoeff, |
---|
| 283 | #if ADAPTIVE_QP_SELECTION |
---|
| 284 | Int*& piArlDstCoeff, |
---|
| 285 | #endif |
---|
[2] | 286 | UInt uiWidth, |
---|
| 287 | UInt uiHeight, |
---|
| 288 | UInt& uiAbsSum, |
---|
| 289 | TextType eTType, |
---|
| 290 | UInt uiAbsPartIdx ); |
---|
[56] | 291 | __inline UInt xGetCodedLevel ( Double& rd64CodedCost, |
---|
| 292 | Double& rd64CodedCost0, |
---|
| 293 | Double& rd64CodedCostSig, |
---|
| 294 | Int lLevelDouble, |
---|
| 295 | UInt uiMaxAbsLevel, |
---|
| 296 | UShort ui16CtxNumSig, |
---|
| 297 | UShort ui16CtxNumOne, |
---|
| 298 | UShort ui16CtxNumAbs, |
---|
| 299 | UShort ui16AbsGoRice, |
---|
| 300 | #if RESTRICT_GR1GR2FLAG_NUMBER |
---|
| 301 | UInt c1Idx, |
---|
| 302 | UInt c2Idx, |
---|
[2] | 303 | #endif |
---|
[56] | 304 | Int iQBits, |
---|
| 305 | Double dTemp, |
---|
| 306 | Bool bLast ) const; |
---|
| 307 | __inline Double xGetICRateCost ( UInt uiAbsLevel, |
---|
[2] | 308 | UShort ui16CtxNumOne, |
---|
| 309 | UShort ui16CtxNumAbs, |
---|
[56] | 310 | UShort ui16AbsGoRice |
---|
| 311 | #if RESTRICT_GR1GR2FLAG_NUMBER |
---|
| 312 | , UInt c1Idx, |
---|
| 313 | UInt c2Idx |
---|
[2] | 314 | #endif |
---|
[56] | 315 | ) const; |
---|
| 316 | #if MULTIBITS_DATA_HIDING |
---|
| 317 | __inline Int xGetICRate ( UInt uiAbsLevel, |
---|
| 318 | UShort ui16CtxNumOne, |
---|
| 319 | UShort ui16CtxNumAbs, |
---|
| 320 | UShort ui16AbsGoRice |
---|
| 321 | #if RESTRICT_GR1GR2FLAG_NUMBER |
---|
| 322 | , UInt c1Idx, |
---|
| 323 | UInt c2Idx |
---|
[2] | 324 | #endif |
---|
[56] | 325 | ) const; |
---|
[2] | 326 | #endif |
---|
[56] | 327 | __inline Double xGetRateLast ( const UInt uiPosX, |
---|
| 328 | const UInt uiPosY, |
---|
| 329 | const UInt uiBlkWdth ) const; |
---|
| 330 | __inline Double xGetRateSigCoeffGroup ( UShort uiSignificanceCoeffGroup, |
---|
| 331 | UShort ui16CtxNumSig ) const; |
---|
[2] | 332 | __inline Double xGetRateSigCoef ( UShort uiSignificance, |
---|
| 333 | UShort ui16CtxNumSig ) const; |
---|
| 334 | __inline Double xGetICost ( Double dRate ) const; |
---|
| 335 | __inline Double xGetIEPRate ( ) const; |
---|
| 336 | |
---|
| 337 | |
---|
| 338 | // dequantization |
---|
[56] | 339 | Void xDeQuant( const TCoeff* pSrc, Int* pDes, Int iWidth, Int iHeight, Int scalingListType ); |
---|
[2] | 340 | |
---|
| 341 | // inverse transform |
---|
[56] | 342 | Void xIT ( UInt uiMode, Int* plCoef, Pel* pResidual, UInt uiStride, Int iWidth, Int iHeight ); |
---|
| 343 | |
---|
[2] | 344 | };// END CLASS DEFINITION TComTrQuant |
---|
| 345 | |
---|
[56] | 346 | //! \} |
---|
[2] | 347 | |
---|
| 348 | #endif // __TCOMTRQUANT__ |
---|