source: 3DVCSoftware/trunk/source/Lib/TLibCommon/TComTrQuant.h

Last change on this file was 1413, checked in by tech, 6 years ago

Merged HTM-16.2-dev@1412

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