1 | |
---|
2 | |
---|
3 | /** \file TComTrQuant.h |
---|
4 | \brief transform and quantization class (header) |
---|
5 | */ |
---|
6 | |
---|
7 | #ifndef __TCOMTRQUANT__ |
---|
8 | #define __TCOMTRQUANT__ |
---|
9 | |
---|
10 | #include "CommonDef.h" |
---|
11 | #include "TComYuv.h" |
---|
12 | #include "TComDataCU.h" |
---|
13 | |
---|
14 | // ==================================================================================================================== |
---|
15 | // Constants |
---|
16 | // ==================================================================================================================== |
---|
17 | |
---|
18 | #define QP_BITS 15 |
---|
19 | |
---|
20 | // AQO Parameter |
---|
21 | #define QOFFSET_BITS 15 |
---|
22 | #define QOFFSET_BITS_LTR 9 |
---|
23 | |
---|
24 | // LTR Butterfly Paramter |
---|
25 | #define ECore16Shift 10 |
---|
26 | #define DCore16Shift 10 |
---|
27 | #define ECore32Shift 10 |
---|
28 | #define DCore32Shift 10 |
---|
29 | |
---|
30 | #define DenShift16 6 |
---|
31 | #define DenShift32 8 |
---|
32 | |
---|
33 | // ==================================================================================================================== |
---|
34 | // Type definition |
---|
35 | // ==================================================================================================================== |
---|
36 | |
---|
37 | typedef struct |
---|
38 | { |
---|
39 | Int significantBits[16][2]; |
---|
40 | #if PCP_SIGMAP_SIMPLE_LAST |
---|
41 | Int lastXBits[32]; |
---|
42 | Int lastYBits[32]; |
---|
43 | #else |
---|
44 | Int lastBits[16][2]; |
---|
45 | #endif |
---|
46 | Int greaterOneBits[6][2][5][2]; |
---|
47 | Int blockCbpBits[45][2]; |
---|
48 | Int blockRootCbpBits[4][2]; |
---|
49 | Int scanZigzag[2]; ///< flag for zigzag scan |
---|
50 | Int scanNonZigzag[2]; ///< flag for non zigzag scan |
---|
51 | } estBitsSbacStruct; |
---|
52 | |
---|
53 | #if QC_MOD_LCEC_RDOQ |
---|
54 | typedef struct |
---|
55 | { |
---|
56 | Int level[4]; |
---|
57 | Int pre_level; |
---|
58 | Int coeff_ctr; |
---|
59 | Long levelDouble; |
---|
60 | Double errLevel[4]; |
---|
61 | Int noLevels; |
---|
62 | Long levelQ; |
---|
63 | Bool lowerInt; |
---|
64 | UInt quantInd; |
---|
65 | } levelDataStruct; |
---|
66 | |
---|
67 | typedef struct |
---|
68 | { |
---|
69 | Int run; |
---|
70 | Int maxrun; |
---|
71 | Int nextLev; |
---|
72 | Int nexLevelVal; |
---|
73 | } quantLevelStruct; |
---|
74 | #endif |
---|
75 | |
---|
76 | |
---|
77 | |
---|
78 | class TEncCavlc; |
---|
79 | |
---|
80 | // ==================================================================================================================== |
---|
81 | // Class definition |
---|
82 | // ==================================================================================================================== |
---|
83 | |
---|
84 | /// QP class |
---|
85 | class QpParam |
---|
86 | { |
---|
87 | public: |
---|
88 | QpParam(); |
---|
89 | |
---|
90 | Int m_iQP; |
---|
91 | Int m_iPer; |
---|
92 | Int m_iRem; |
---|
93 | |
---|
94 | Int m_iAdd2x2; |
---|
95 | Int m_iAdd4x4; |
---|
96 | Int m_iAdd8x8; |
---|
97 | Int m_iAdd16x16; |
---|
98 | Int m_iAdd32x32; |
---|
99 | private: |
---|
100 | Int m_aiAdd2x2[MAX_QP+1][3]; |
---|
101 | Int m_aiAdd4x4[MAX_QP+1][3]; |
---|
102 | Int m_aiAdd8x8[MAX_QP+1][3]; |
---|
103 | Int m_aiAdd16x16[MAX_QP+1][3]; |
---|
104 | Int m_aiAdd32x32[MAX_QP+1][3]; |
---|
105 | public: |
---|
106 | Int m_iBits; |
---|
107 | |
---|
108 | Void initOffsetParam(Int iStartQP = MIN_QP, Int iEndQP = MAX_QP ); |
---|
109 | Void setQOffset( Int iQP, SliceType eSliceType ) |
---|
110 | { |
---|
111 | m_iAdd2x2 = m_aiAdd2x2[iQP][eSliceType]; |
---|
112 | m_iAdd4x4 = m_aiAdd4x4[iQP][eSliceType]; |
---|
113 | m_iAdd8x8 = m_aiAdd8x8[iQP][eSliceType]; |
---|
114 | m_iAdd16x16 = m_aiAdd16x16[iQP][eSliceType]; |
---|
115 | m_iAdd32x32 = m_aiAdd32x32[iQP][eSliceType]; |
---|
116 | } |
---|
117 | |
---|
118 | Void setQpParam( Int iQP, Bool bLowpass, SliceType eSliceType, Bool bEnc ) |
---|
119 | { |
---|
120 | assert ( iQP >= MIN_QP && iQP <= MAX_QP ); |
---|
121 | m_iQP = iQP; |
---|
122 | |
---|
123 | m_iPer = (iQP + 6*g_uiBitIncrement)/6; |
---|
124 | #if FULL_NBIT |
---|
125 | m_iPer += g_uiBitDepth - 8; |
---|
126 | #endif |
---|
127 | m_iRem = (iQP + 6*g_uiBitIncrement)%6; |
---|
128 | |
---|
129 | m_iBits = QP_BITS + m_iPer; |
---|
130 | |
---|
131 | if ( bEnc ) |
---|
132 | { |
---|
133 | setQOffset(iQP, eSliceType); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | Void clear() |
---|
138 | { |
---|
139 | m_iQP = 0; |
---|
140 | m_iPer = 0; |
---|
141 | m_iRem = 0; |
---|
142 | m_iBits = 0; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | const Int per() const { return m_iPer; } |
---|
147 | const Int rem() const { return m_iRem; } |
---|
148 | const Int bits() const { return m_iBits; } |
---|
149 | |
---|
150 | Int qp() {return m_iQP;} |
---|
151 | }; // END CLASS DEFINITION QpParam |
---|
152 | |
---|
153 | /// transform and quantization class |
---|
154 | class TComTrQuant |
---|
155 | { |
---|
156 | public: |
---|
157 | TComTrQuant(); |
---|
158 | ~TComTrQuant(); |
---|
159 | |
---|
160 | // initialize class |
---|
161 | #if QC_MOD_LCEC |
---|
162 | 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 ); |
---|
163 | #else |
---|
164 | Void init ( UInt uiMaxWidth, UInt uiMaxHeight, UInt uiMaxTrSize, Int iSymbolMode = 0, UInt *aTable4 = NULL, UInt *aTable8 = NULL, Bool bUseRDOQ = false, Bool bEnc = false ); |
---|
165 | #endif |
---|
166 | |
---|
167 | // transform & inverse transform functions |
---|
168 | Void transformNxN ( TComDataCU* pcCU, Pel* pcResidual, UInt uiStride, TCoeff*& rpcCoeff, UInt uiWidth, UInt uiHeight, |
---|
169 | UInt& uiAbsSum, TextType eTType, UInt uiAbsPartIdx ); |
---|
170 | #if INTRA_DST_TYPE_7 |
---|
171 | Void invtransformNxN (TextType eText, UInt uiMode,Pel*& rpcResidual, UInt uiStride, TCoeff* pcCoeff, UInt uiWidth, UInt uiHeight); |
---|
172 | #else |
---|
173 | Void invtransformNxN ( Pel*& rpcResidual, UInt uiStride, TCoeff* pcCoeff, UInt uiWidth, UInt uiHeight ); |
---|
174 | #endif |
---|
175 | Void invRecurTransformNxN ( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eTxt, Pel*& rpcResidual, UInt uiAddr, UInt uiStride, UInt uiWidth, UInt uiHeight, |
---|
176 | UInt uiMaxTrMode, UInt uiTrMode, TCoeff* rpcCoeff ); |
---|
177 | |
---|
178 | // Misc functions |
---|
179 | Void setQPforQuant( Int iQP, Bool bLowpass, SliceType eSliceType, TextType eTxtType); |
---|
180 | Void setLambda(Double dLambda) { m_dLambda = dLambda;} |
---|
181 | #if QC_MOD_LCEC_RDOQ |
---|
182 | Void setRDOQOffset ( UInt uiRDOQOffset ) { m_uiRDOQOffset = uiRDOQOffset; } |
---|
183 | #endif |
---|
184 | estBitsSbacStruct* m_pcEstBitsSbac; |
---|
185 | |
---|
186 | static UInt getSigCtxInc ( TCoeff* pcCoeff, |
---|
187 | const UInt uiPosX, |
---|
188 | const UInt uiPosY, |
---|
189 | const UInt uiLog2BlkSize, |
---|
190 | const UInt uiStride ); |
---|
191 | #if !PCP_SIGMAP_SIMPLE_LAST |
---|
192 | static UInt getLastCtxInc ( const UInt uiPosX, |
---|
193 | const UInt uiPosY, |
---|
194 | const UInt uiLog2BlkSize ); |
---|
195 | #endif |
---|
196 | protected: |
---|
197 | Long* m_plTempCoeff; |
---|
198 | UInt* m_puiQuantMtx; |
---|
199 | |
---|
200 | QpParam m_cQP; |
---|
201 | Double m_dLambda; |
---|
202 | #if QC_MOD_LCEC_RDOQ |
---|
203 | UInt m_uiRDOQOffset; |
---|
204 | #endif |
---|
205 | UInt m_uiMaxTrSize; |
---|
206 | Bool m_bEnc; |
---|
207 | Bool m_bUseRDOQ; |
---|
208 | |
---|
209 | UInt *m_uiLPTableE8; |
---|
210 | UInt *m_uiLPTableE4; |
---|
211 | Int m_iSymbolMode; |
---|
212 | #if QC_MOD_LCEC |
---|
213 | UInt *m_uiLastPosVlcIndex; |
---|
214 | #endif |
---|
215 | |
---|
216 | private: |
---|
217 | // forward Transform |
---|
218 | #if INTRA_DST_TYPE_7 |
---|
219 | Void xT ( UInt uiMode,Pel* pResidual, UInt uiStride, Long* plCoeff, Int iSize ); |
---|
220 | #else |
---|
221 | Void xT ( Pel* pResidual, UInt uiStride, Long* plCoeff, Int iSize ); |
---|
222 | #endif |
---|
223 | Void xT2 ( Pel* pResidual, UInt uiStride, Long* plCoeff ); |
---|
224 | Void xT4 ( Pel* pResidual, UInt uiStride, Long* plCoeff ); |
---|
225 | Void xT8 ( Pel* pResidual, UInt uiStride, Long* plCoeff ); |
---|
226 | Void xT16 ( Pel* pResidual, UInt uiStride, Long* plCoeff ); |
---|
227 | Void xT32 ( Pel* pResidual, UInt uiStride, Long* plCoeff ); |
---|
228 | |
---|
229 | // quantization |
---|
230 | Void xQuant ( TComDataCU* pcCU, Long* pSrc, TCoeff*& pDes, Int iWidth, Int iHeight, UInt& uiAcSum, TextType eTType, UInt uiAbsPartIdx ); |
---|
231 | Void xQuantLTR ( TComDataCU* pcCU, Long* pSrc, TCoeff*& pDes, Int iWidth, Int iHeight, UInt& uiAcSum, TextType eTType, UInt uiAbsPartIdx ); |
---|
232 | Void xQuant2x2 ( Long* plSrcCoef, TCoeff*& pDstCoef, UInt& uiAbsSum ); |
---|
233 | Void xQuant4x4 ( TComDataCU* pcCU, Long* plSrcCoef, TCoeff*& pDstCoef, UInt& uiAbsSum, TextType eTType, UInt uiAbsPartIdx ); |
---|
234 | Void xQuant8x8 ( TComDataCU* pcCU, Long* plSrcCoef, TCoeff*& pDstCoef, UInt& uiAbsSum, TextType eTType, UInt uiAbsPartIdx ); |
---|
235 | |
---|
236 | |
---|
237 | // RDOQ functions |
---|
238 | |
---|
239 | #if QC_MOD_LCEC_RDOQ |
---|
240 | Int xCodeCoeffCountBitsLast(TCoeff* scoeff, levelDataStruct* levelData, Int nTab, UInt uiNoCoeff); |
---|
241 | UInt xCountVlcBits(UInt uiTableNumber, UInt uiCodeNumber); |
---|
242 | #if CAVLC_COEF_LRG_BLK |
---|
243 | Int bitCountRDOQ(Int coeff, Int pos, Int nTab, Int lastCoeffFlag,Int levelMode,Int run, Int maxrun, Int vlc_adaptive, Int N, |
---|
244 | UInt uiTr1, Int iSum_big_coef, Int iBlockType, TComDataCU* pcCU, const UInt **pLumaRunTr1); |
---|
245 | #else |
---|
246 | Int bitCountRDOQ(Int coeff, Int pos, Int nTab, Int lastCoeffFlag,Int levelMode,Int run, Int maxrun, Int vlc_adaptive, Int N, |
---|
247 | UInt uiTr1, Int iSum_big_coef, Int iBlockType, TComDataCU* pcCU); |
---|
248 | #endif |
---|
249 | #else |
---|
250 | #if QC_MOD_LCEC |
---|
251 | Int bitCount_LCEC(Int k,Int pos,Int nTab, Int lpflag,Int levelMode,Int run, Int maxrun, Int vlc_adaptive, Int N, UInt uiTr1); |
---|
252 | #else |
---|
253 | Int bitCount_LCEC(Int k,Int pos,Int n,Int lpflag,Int levelMode,Int run,Int maxrun,Int vlc_adaptive,Int N); |
---|
254 | #endif |
---|
255 | #endif |
---|
256 | #if QC_MDCS |
---|
257 | UInt getCurrLineNum(UInt uiScanIdx, UInt uiPosX, UInt uiPosY); |
---|
258 | #endif |
---|
259 | Void xRateDistOptQuant_LCEC ( TComDataCU* pcCU, |
---|
260 | Long* plSrcCoeff, |
---|
261 | TCoeff*& piDstCoeff, |
---|
262 | UInt uiWidth, |
---|
263 | UInt uiHeight, |
---|
264 | UInt& uiAbsSum, |
---|
265 | TextType eTType, |
---|
266 | UInt uiAbsPartIdx ); |
---|
267 | |
---|
268 | Void xRateDistOptQuant ( TComDataCU* pcCU, |
---|
269 | Long* plSrcCoeff, |
---|
270 | TCoeff*& piDstCoeff, |
---|
271 | UInt uiWidth, |
---|
272 | UInt uiHeight, |
---|
273 | UInt& uiAbsSum, |
---|
274 | TextType eTType, |
---|
275 | UInt uiAbsPartIdx ); |
---|
276 | __inline UInt xGetCodedLevel ( Double& rd64UncodedCost, |
---|
277 | Double& rd64CodedCost, |
---|
278 | #if PCP_SIGMAP_SIMPLE_LAST |
---|
279 | Double& rd64CodedLastCost, |
---|
280 | UInt& ruiBestNonZeroLevel, |
---|
281 | Long lLevelDouble, |
---|
282 | UInt uiMaxAbsLevel, |
---|
283 | #else |
---|
284 | Long lLevelDouble, |
---|
285 | UInt uiMaxAbsLevel, |
---|
286 | bool bLastScanPos, |
---|
287 | #endif |
---|
288 | UShort ui16CtxNumSig, |
---|
289 | UShort ui16CtxNumOne, |
---|
290 | UShort ui16CtxNumAbs, |
---|
291 | #if E253 |
---|
292 | UShort ui16AbsGoRice, |
---|
293 | #endif |
---|
294 | Int iQBits, |
---|
295 | Double dTemp, |
---|
296 | UShort ui16CtxBase ) const; |
---|
297 | __inline Double xGetICRateCost ( UInt uiAbsLevel, |
---|
298 | #if !PCP_SIGMAP_SIMPLE_LAST |
---|
299 | Bool bLastScanPos, |
---|
300 | UShort ui16CtxNumSig, |
---|
301 | #endif |
---|
302 | UShort ui16CtxNumOne, |
---|
303 | UShort ui16CtxNumAbs, |
---|
304 | #if E253 |
---|
305 | UShort ui16AbsGoRice, |
---|
306 | #endif |
---|
307 | UShort ui16CtxBase ) const; |
---|
308 | #if PCP_SIGMAP_SIMPLE_LAST |
---|
309 | __inline Double xGetRateLast ( UInt uiPosX, |
---|
310 | UInt uiPosY ) const; |
---|
311 | __inline Double xGetRateSigCoef ( UShort uiSignificance, |
---|
312 | UShort ui16CtxNumSig ) const; |
---|
313 | #endif |
---|
314 | __inline Double xGetICost ( Double dRate ) const; |
---|
315 | __inline Double xGetIEPRate ( ) const; |
---|
316 | |
---|
317 | |
---|
318 | __inline Int xRound ( Int i ) { return ((i)+(1<<5))>>6; } |
---|
319 | __inline static Long xTrRound ( Long i, UInt uiShift ) { return ((i)>>uiShift); } |
---|
320 | |
---|
321 | // dequantization |
---|
322 | Void xDeQuant ( TCoeff* pSrc, Long*& pDes, Int iWidth, Int iHeight ); |
---|
323 | Void xDeQuantLTR ( TCoeff* pSrc, Long*& pDes, Int iWidth, Int iHeight ); |
---|
324 | Void xDeQuant2x2 ( TCoeff* pSrcCoef, Long*& rplDstCoef ); |
---|
325 | Void xDeQuant4x4 ( TCoeff* pSrcCoef, Long*& rplDstCoef ); |
---|
326 | Void xDeQuant8x8 ( TCoeff* pSrcCoef, Long*& rplDstCoef ); |
---|
327 | |
---|
328 | // inverse transform |
---|
329 | #if INTRA_DST_TYPE_7 |
---|
330 | Void xIT ( UInt uiMode, Long* plCoef, Pel* pResidual, UInt uiStride, Int iSize ); |
---|
331 | #else |
---|
332 | Void xIT ( Long* plCoef, Pel* pResidual, UInt uiStride, Int iSize ); |
---|
333 | #endif |
---|
334 | Void xIT2 ( Long* plCoef, Pel* pResidual, UInt uiStride ); |
---|
335 | Void xIT4 ( Long* plCoef, Pel* pResidual, UInt uiStride ); |
---|
336 | Void xIT8 ( Long* plCoef, Pel* pResidual, UInt uiStride ); |
---|
337 | Void xIT16 ( Long* plCoef, Pel* pResidual, UInt uiStride ); |
---|
338 | Void xIT32 ( Long* plCoef, Pel* pResidual, UInt uiStride ); |
---|
339 | |
---|
340 | |
---|
341 | };// END CLASS DEFINITION TComTrQuant |
---|
342 | |
---|
343 | |
---|
344 | #endif // __TCOMTRQUANT__ |
---|
345 | |
---|