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