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-2015, 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 | #if T0196_SELECTIVE_RDOQ |
---|
109 | Bool useSelectiveRDOQ = false, |
---|
110 | #endif |
---|
111 | Bool bEnc = false, |
---|
112 | Bool useTransformSkipFast = false |
---|
113 | #if ADAPTIVE_QP_SELECTION |
---|
114 | , Bool bUseAdaptQpSelect = false |
---|
115 | #endif |
---|
116 | ); |
---|
117 | |
---|
118 | // transform & inverse transform functions |
---|
119 | Void transformNxN( TComTU & rTu, |
---|
120 | const ComponentID compID, |
---|
121 | Pel * pcResidual, |
---|
122 | const UInt uiStride, |
---|
123 | TCoeff * rpcCoeff, |
---|
124 | #if ADAPTIVE_QP_SELECTION |
---|
125 | TCoeff * rpcArlCoeff, |
---|
126 | #endif |
---|
127 | TCoeff & uiAbsSum, |
---|
128 | const QpParam & cQP |
---|
129 | ); |
---|
130 | |
---|
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 | |
---|
147 | // Misc functions |
---|
148 | |
---|
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]; } |
---|
152 | #else |
---|
153 | Void setLambda(Double dLambda) { m_dLambda = dLambda;} |
---|
154 | #endif |
---|
155 | Void setRDOQOffset( UInt uiRDOQOffset ) { m_uiRDOQOffset = uiRDOQOffset; } |
---|
156 | |
---|
157 | estBitsSbacStruct* m_pcEstBitsSbac; |
---|
158 | |
---|
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 |
---|
167 | ); |
---|
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 | |
---|
175 | Void initScalingList (); |
---|
176 | Void destroyScalingList (); |
---|
177 | Void setErrScaleCoeff ( UInt list, UInt size, Int qp, const Int maxLog2TrDynamicRange[MAX_NUM_CHANNEL_TYPE], const BitDepths &bitDepths ); |
---|
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 |
---|
182 | Void setUseScalingList ( Bool bUseScalingList){ m_scalingListEnabledFlag = bUseScalingList; }; |
---|
183 | Bool getUseScalingList (const UInt width, const UInt height, const Bool isTransformSkip){ return m_scalingListEnabledFlag && (!isTransformSkip || ((width == 4) && (height == 4))); }; |
---|
184 | Void setFlatScalingList (const ChromaFormat format, const Int maxLog2TrDynamicRange[MAX_NUM_CHANNEL_TYPE], const BitDepths &bitDepths); |
---|
185 | Void xsetFlatScalingList ( UInt list, UInt size, Int qp, const ChromaFormat format); |
---|
186 | Void xSetScalingListEnc ( TComScalingList *scalingList, UInt list, UInt size, Int qp, const ChromaFormat format); |
---|
187 | Void xSetScalingListDec ( const TComScalingList &scalingList, UInt list, UInt size, Int qp, const ChromaFormat format); |
---|
188 | Void setScalingList ( TComScalingList *scalingList, const ChromaFormat format, const Int maxLog2TrDynamicRange[MAX_NUM_CHANNEL_TYPE], const BitDepths &bitDepths); |
---|
189 | Void setScalingListDec ( const TComScalingList &scalingList, const ChromaFormat format); |
---|
190 | Void processScalingListEnc( Int *coeff, Int *quantcoeff, Int quantScales, UInt height, UInt width, UInt ratio, Int sizuNum, UInt dc); |
---|
191 | Void processScalingListDec( const Int *coeff, Int *dequantcoeff, Int invQuantScales, UInt height, UInt width, UInt ratio, Int sizuNum, UInt dc); |
---|
192 | #if ADAPTIVE_QP_SELECTION |
---|
193 | Void initSliceQpDelta() ; |
---|
194 | Void storeSliceQpNext(TComSlice* pcSlice); |
---|
195 | Void clearSliceARLCnt(); |
---|
196 | Int getQpDelta(Int qp) { return m_qpDelta[qp]; } |
---|
197 | Int* getSliceNSamples(){ return m_sliceNsamples ;} |
---|
198 | Double* getSliceSumC() { return m_sliceSumC; } |
---|
199 | #endif |
---|
200 | Void transformSkipQuantOneSample(TComTU &rTu, const ComponentID compID, const TCoeff resiDiff, TCoeff* pcCoeff, const UInt uiPos, const QpParam &cQP, const Bool bUseHalfRoundingPoint); |
---|
201 | Void invTrSkipDeQuantOneSample(TComTU &rTu, ComponentID compID, TCoeff pcCoeff, Pel &reconSample, const QpParam &cQP, UInt uiPos ); |
---|
202 | |
---|
203 | protected: |
---|
204 | #if ADAPTIVE_QP_SELECTION |
---|
205 | Int m_qpDelta[MAX_QP+1]; |
---|
206 | Int m_sliceNsamples[LEVEL_RANGE+1]; |
---|
207 | Double m_sliceSumC[LEVEL_RANGE+1] ; |
---|
208 | #endif |
---|
209 | TCoeff* m_plTempCoeff; |
---|
210 | |
---|
211 | // QpParam m_cQP; - removed - placed on the stack. |
---|
212 | #if RDOQ_CHROMA_LAMBDA |
---|
213 | Double m_lambdas[MAX_NUM_COMPONENT]; |
---|
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; |
---|
221 | #if T0196_SELECTIVE_RDOQ |
---|
222 | Bool m_useSelectiveRDOQ; |
---|
223 | #endif |
---|
224 | #if ADAPTIVE_QP_SELECTION |
---|
225 | Bool m_bUseAdaptQpSelect; |
---|
226 | #endif |
---|
227 | Bool m_useTransformSkipFast; |
---|
228 | |
---|
229 | Bool m_scalingListEnabledFlag; |
---|
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 | |
---|
236 | private: |
---|
237 | // forward Transform |
---|
238 | Void xT ( const Int channelBitDepth, Bool useDST, Pel* piBlkResi, UInt uiStride, TCoeff* psCoeff, Int iWidth, Int iHeight, const Int maxLog2TrDynamicRange ); |
---|
239 | |
---|
240 | // skipping Transform |
---|
241 | Void xTransformSkip ( Pel* piBlkResi, UInt uiStride, TCoeff* psCoeff, TComTU &rTu, const ComponentID component ); |
---|
242 | |
---|
243 | Void signBitHidingHDQ( const ComponentID compID, TCoeff* pQCoef, TCoeff* pCoef, TCoeff* deltaU, const TUEntropyCodingParameters &codingParameters, const Int maxLog2TrDynamicRange ); |
---|
244 | |
---|
245 | // quantization |
---|
246 | Void xQuant( TComTU &rTu, |
---|
247 | TCoeff * pSrc, |
---|
248 | TCoeff * pDes, |
---|
249 | #if ADAPTIVE_QP_SELECTION |
---|
250 | TCoeff *pArlDes, |
---|
251 | #endif |
---|
252 | TCoeff &uiAbsSum, |
---|
253 | const ComponentID compID, |
---|
254 | const QpParam &cQP ); |
---|
255 | |
---|
256 | #if T0196_SELECTIVE_RDOQ |
---|
257 | Bool xNeedRDOQ( TComTU &rTu, |
---|
258 | TCoeff * pSrc, |
---|
259 | const ComponentID compID, |
---|
260 | const QpParam &cQP ); |
---|
261 | #endif |
---|
262 | |
---|
263 | // RDOQ functions |
---|
264 | |
---|
265 | Void xRateDistOptQuant ( TComTU &rTu, |
---|
266 | TCoeff * plSrcCoeff, |
---|
267 | TCoeff * piDstCoeff, |
---|
268 | #if ADAPTIVE_QP_SELECTION |
---|
269 | TCoeff *piArlDstCoeff, |
---|
270 | #endif |
---|
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, |
---|
290 | ChannelType channelType |
---|
291 | ) const; |
---|
292 | |
---|
293 | |
---|
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 |
---|
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 | |
---|
311 | // dequantization |
---|
312 | Void xDeQuant( TComTU &rTu, |
---|
313 | const TCoeff * pSrc, |
---|
314 | TCoeff * pDes, |
---|
315 | const ComponentID compID, |
---|
316 | const QpParam &cQP ); |
---|
317 | |
---|
318 | // inverse transform |
---|
319 | Void xIT ( const Int channelBitDepth, Bool useDST, TCoeff* plCoef, Pel* pResidual, UInt uiStride, Int iWidth, Int iHeight, const Int maxLog2TrDynamicRange ); |
---|
320 | |
---|
321 | // inverse skipping transform |
---|
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 | |
---|
337 | };// END CLASS DEFINITION TComTrQuant |
---|
338 | |
---|
339 | //! \} |
---|
340 | |
---|
341 | #endif // __TCOMTRQUANT__ |
---|