source: 3DVCSoftware/trunk/source/Lib/TLibCommon/TComAdaptiveLoopFilter.h @ 56

Last change on this file since 56 was 56, checked in by hschwarz, 12 years ago

updated trunk (move to HM6.1)

  • Property svn:eol-style set to native
File size: 14.7 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-2012, 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     TComAdaptiveLoopFilter.h
35    \brief    adaptive loop filter class (header)
36*/
37
38#ifndef __TCOMADAPTIVELOOPFILTER__
39#define __TCOMADAPTIVELOOPFILTER__
40
41#include "CommonDef.h"
42#include "TComPic.h"
43
44//! \ingroup TLibCommon
45//! \{
46
47// ====================================================================================================================
48// Constants
49// ====================================================================================================================
50
51#if LCU_SYNTAX_ALF
52  #define LCUALF_QP_DEPENDENT_BITS    1 
53#endif
54
55#if ALF_SINGLE_FILTER_SHAPE
56#define ALF_FILTER_LEN       10
57#define ALF_MAX_NUM_COEF     ALF_FILTER_LEN    //!< maximum number of filter coefficients
58#else
59#define ALF_MAX_NUM_COEF      9                                       //!< maximum number of filter coefficients
60#endif
61#define MAX_SQR_FILT_LENGTH   41                                      //!< ((max_horizontal_tap * max_vertical_tap) / 2 + 1) = ((11 * 5) / 2 + 1)
62
63#if LCU_SYNTAX_ALF && LCUALF_QP_DEPENDENT_BITS
64#define ALF_QP1               28
65#define ALF_QP2               34
66#define ALF_QP3               39
67#else
68#define ALF_NUM_BIT_SHIFT     8                                       ///< bit shift parameter for quantization of ALF param.
69#endif
70
71#define VAR_SIZE_H            4
72#define VAR_SIZE_W            4
73#define NO_VAR_BINS           16
74#define NO_FILTERS            16
75#define MAX_SCAN_VAL          13
76#define MAX_EXP_GOLOMB        16
77
78
79
80#if LCU_SYNTAX_ALF
81/// Luma/Chroma component ID
82enum ALFComponentID
83{
84  ALF_Y = 0,
85  ALF_Cb,
86  ALF_Cr,
87  NUM_ALF_COMPONENT
88};
89/// ALF LCU merge type
90enum ALFLCUMergeType
91{
92  ALF_MERGE_DISABLED = 0,
93  ALF_MERGE_UP,
94  ALF_MERGE_LEFT,
95  ALF_MERGE_FIRST,
96  NUM_ALF_MERGE_TYPE
97};
98#else
99///
100/// Chroma component ID
101///
102enum AlfChromaID
103{
104  ALF_Cb = 0,
105  ALF_Cr = 1
106};
107
108
109///
110/// Adaptation mode ID
111///
112enum ALFClassficationMethod
113{
114  ALF_BA =0,
115  ALF_RA,
116  NUM_ALF_CLASS_METHOD
117};
118#endif
119///
120/// Filter shape
121///
122enum ALFFilterShape
123{
124#if ALF_SINGLE_FILTER_SHAPE
125  ALF_CROSS9x7_SQUARE3x3 = 0,
126#else
127  ALF_STAR5x5 = 0,
128  ALF_CROSS9x9,
129#endif
130  NUM_ALF_FILTER_SHAPE
131};
132
133#if LCU_SYNTAX_ALF
134extern Int* kTableTabShapes[NUM_ALF_FILTER_SHAPE];
135#endif
136#if ALF_SINGLE_FILTER_SHAPE
137extern Int depthIntShape1Sym[ALF_MAX_NUM_COEF+1];
138#else
139extern Int depthIntShape0Sym[10];
140extern Int depthIntShape1Sym[10];
141#endif
142extern Int *pDepthIntTabShapes[NUM_ALF_FILTER_SHAPE];
143
144// ====================================================================================================================
145// Class definition
146// ====================================================================================================================
147
148/// ALF CU control parameters
149struct AlfCUCtrlInfo
150{
151  Int  cu_control_flag;                    //!< slice-level ALF CU control enabled/disabled flag
152  UInt num_alf_cu_flag;                    //!< number of ALF CU control flags
153  UInt alf_max_depth;                      //!< ALF CU control depth
154  std::vector<UInt> alf_cu_flag;           //!< ALF CU control flags (container)
155
156  const AlfCUCtrlInfo& operator= (const AlfCUCtrlInfo& src);  //!< "=" operator
157  AlfCUCtrlInfo():cu_control_flag(0), num_alf_cu_flag(0), alf_max_depth(0) {} //!< constructor
158#if LCU_SYNTAX_ALF
159  Void reset();
160#endif
161};
162
163
164///
165/// LCU-based ALF processing info
166///
167struct AlfLCUInfo
168{
169  TComDataCU* pcCU;            //!< TComDataCU pointer
170  Int         sliceID;        //!< slice ID
171  Int         tileID;         //!< tile ID
172  UInt        numSGU;        //!< number of slice granularity blocks
173  UInt        startSU;       //!< starting SU z-scan address in LCU
174  UInt        endSU;         //!< ending SU z-scan address in LCU
175  Bool        bAllSUsInLCUInSameSlice; //!< true: all SUs in this LCU belong to same slice
176  std::vector<NDBFBlockInfo*> vpAlfBlock; //!< container for filter block pointers
177
178  NDBFBlockInfo& operator[] (Int idx) { return *( vpAlfBlock[idx]); } //!< [] operator
179  AlfLCUInfo():pcCU(NULL), sliceID(0), tileID(0), numSGU(0), startSU(0), endSU(0), bAllSUsInLCUInSameSlice(false) {} //!< constructor
180};
181
182
183///
184/// adaptive loop filter class
185///
186class TComAdaptiveLoopFilter
187{
188
189protected: //protected member variables
190
191  // filter shape information
192#if ALF_SINGLE_FILTER_SHAPE
193  static Int weightsShape1Sym[ALF_MAX_NUM_COEF+1];
194#else
195  static Int weightsShape0Sym[10];
196  static Int weightsShape1Sym[10];
197#endif
198  static Int *weightsTabShapes[NUM_ALF_FILTER_SHAPE];
199  static Int m_sqrFiltLengthTab[NUM_ALF_FILTER_SHAPE];
200
201  // temporary buffer
202  TComPicYuv*   m_pcTempPicYuv;                          ///< temporary picture buffer for ALF processing
203  TComPicYuv* m_pcSliceYuvTmp;    //!< temporary picture buffer pointer when non-across slice boundary ALF is enabled
204
205
206  //filter coefficients buffer
207  Int **m_filterCoeffSym;
208
209  //classification
210  Int      m_varIndTab[NO_VAR_BINS];
211#if !LCU_SYNTAX_ALF
212  UInt     m_uiVarGenMethod;
213  Pel** m_varImgMethods[NUM_ALF_CLASS_METHOD];
214#endif
215  Pel** m_varImg;
216
217  //parameters
218  Int   m_img_height;
219  Int   m_img_width;
220  Bool  m_bUseNonCrossALF;       //!< true for performing non-cross slice boundary ALF
221  UInt  m_uiNumSlicesInPic;      //!< number of slices in picture
222  Int   m_iSGDepth;              //!< slice granularity depth
223  UInt  m_uiNumCUsInFrame;
224
225  Int m_lcuHeight;
226  Int m_lineIdxPadBot;
227  Int m_lineIdxPadTop;
228
229  Int m_lcuHeightChroma;
230  Int m_lineIdxPadBotChroma;
231  Int m_lineIdxPadTopChroma;
232
233  //slice
234  TComPic* m_pcPic;
235  AlfLCUInfo** m_ppSliceAlfLCUs;
236  std::vector< AlfLCUInfo* > *m_pvpAlfLCU;
237  std::vector< std::vector< AlfLCUInfo* > > *m_pvpSliceTileAlfLCU;
238
239#if LCU_SYNTAX_ALF
240  Int m_suWidth;
241  Int m_suHeight;
242  Int m_numLCUInPicWidth;
243  Int m_numLCUInPicHeight;
244  ALFParam** m_alfFiltInfo[NUM_ALF_COMPONENT];
245  Bool m_isNonCrossSlice;
246  Int m_alfQP;
247#endif
248
249private: //private member variables
250
251
252protected: //protected methods
253
254#if LCU_SYNTAX_ALF
255  Void createLCUAlfInfo();
256  Void destroyLCUAlfInfo();
257  Pel* getPicBuf(TComPicYuv* pPicYuv, Int compIdx);
258  Void predictALFCoeffChroma(Int* coeff, Int numCoef= ALF_MAX_NUM_COEF);
259  Void assignAlfOnOffControlFlags(TComPic* pcPic, std::vector<AlfCUCtrlInfo>& vAlfCUCtrlParam);
260  Void recALF(Int compIdx, ALFParam** alfLCUParams, Pel* pDec, Pel* pRest, Int stride, Int formatShift, std::vector<AlfCUCtrlInfo>* alfCUCtrlParam, Bool caculateBAIdx);
261  Void reconstructCoefInfo(Int compIdx, ALFParam* alfLCUParam, Int** filterCoeff, Int* varIndTab= NULL);
262  Void reconstructLumaCoefficients(ALFParam* alfLCUParam, Int** filterCoeff);
263  Void reconstructChromaCoefficients(ALFParam* alfLCUParam, Int** filterCoeff);
264  Void filterRegion(Int compIdx, ALFParam** alfLCUParams, std::vector<AlfLCUInfo*>& regionLCUInfo, Pel* pDec, Pel* pRest, Int stride, Int formatShift, Bool caculateBAIdx);
265  Void filterRegionCUControl(ALFParam** alfLCUParams, std::vector<AlfLCUInfo*>& regionLCUInfo, Pel* pDec, Pel* pRest, Int stride, Bool caculateBAIdx);
266  Bool isEnabledComponent(ALFParam** alfLCUParam);
267  Int  getAlfPrecisionBit(Int qp);
268#if ALF_SINGLE_FILTER_SHAPE
269  Void filterOneCompRegion(Pel *imgRes, Pel *imgPad, Int stride, Bool isChroma, Int yPos, Int yPosEnd, Int xPos, Int xPosEnd, Int** filterSet, Int* mergeTable, Pel** varImg); 
270#endif
271  Void calcOneRegionVar(Pel **imgYvar, Pel *imgYpad, Int stride, Bool isOnlyOneGroup, Int yPos, Int yPosEnd, Int xPos, Int xPosEnd);
272#endif
273
274
275  Void InitAlfLCUInfo(AlfLCUInfo& rAlfLCU, Int sliceID, Int tileID, TComDataCU* pcCU, UInt maxNumSUInLCU);
276#if !LCU_SYNTAX_ALF
277  Void createRegionIndexMap(Pel **imgY_var, Int img_width, Int img_height); //!< create RA index for regions
278  Void calcVar(Pel **imgYvar, Pel *imgYpad, Int stride, Int adaptationMode); //!< Calculate ALF grouping indices for block-based (BA) mode
279  Void filterLuma(Pel *pImgYRes, Pel *pImgYPad, Int stride, Int ypos, Int yposEnd, Int xpos, Int xposEnd, Int filtNo, Int** filterSet, Int* mergeTable, Pel** ppVarImg); //!< filtering operation for luma region
280  Void filterChroma(Pel *pImgRes, Pel *pImgPad, Int stride, Int ypos, Int yposEnd, Int xpos, Int xposEnd, Int filtNo, Int* coef);
281  Void filterChromaRegion(std::vector<AlfLCUInfo*> &vpAlfLCU, Pel* pDec, Pel* pRest, Int stride, Int *coeff, Int filtNo, Int chromaFormatShift); //!< filtering operation for chroma region
282  Void xCUAdaptive   (TComPic* pcPic, Int filtNo, Pel *imgYFilt, Pel *imgYRec, Int Stride);
283  Void xSubCUAdaptive(TComDataCU* pcCU, Int filtNo, Pel *imgYFilt, Pel *imgYRec, UInt uiAbsPartIdx, UInt uiDepth, Int Stride);
284  Void reconstructFilterCoeffs(ALFParam* pcAlfParam,int **pfilterCoeffSym);
285  Void predictALFCoeffLuma  ( ALFParam* pAlfParam );                    //!< prediction of luma ALF coefficients
286#endif
287  Void checkFilterCoeffValue( Int *filter, Int filterLength, Bool isChroma );
288#if !LCU_SYNTAX_ALF
289  Void decodeFilterSet(ALFParam* pcAlfParam, Int* varIndTab, Int** filterCoeff);
290  Void xALFChroma   ( ALFParam* pcAlfParam, TComPicYuv* pcPicDec, TComPicYuv* pcPicRest );
291  Void xFilterChromaSlices(Int componentID, TComPicYuv* pcPicDecYuv, TComPicYuv* pcPicRestYuv, Int *coeff, Int filtNo, Int chromaFormatShift);
292  Void xFilterChromaOneCmp(Int componentID, TComPicYuv *pDecYuv, TComPicYuv *pRestYuv, Int shape, Int *pCoeff);
293  Void xALFLuma( TComPic* pcPic, ALFParam* pcAlfParam, std::vector<AlfCUCtrlInfo>& vAlfCUCtrlParam, TComPicYuv* pcPicDec, TComPicYuv* pcPicRest );
294#endif
295  Void setAlfCtrlFlags(AlfCUCtrlInfo* pAlfParam, TComDataCU *pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt &idx);
296  Void transferCtrlFlagsFromAlfParam(std::vector<AlfCUCtrlInfo>& vAlfParamSlices); //!< Copy ALF CU control flags from ALF parameters for slices 
297  Void transferCtrlFlagsFromAlfParamOneSlice(std::vector<AlfLCUInfo*> &vpAlfLCU, Bool bCUCtrlEnabled, Int iAlfDepth, std::vector<UInt>& vCtrlFlags); //!< Copy ALF CU control flags from ALF parameter for one slice
298  Void extendBorderCoreFunction(Pel* pPel, Int stride, Bool* pbAvail, UInt width, UInt height, UInt extSize); //!< Extend slice boundary border 
299  Void copyRegion(std::vector<AlfLCUInfo*> &vpAlfLCU, Pel* pPicDst, Pel* pPicSrc, Int stride, Int formatShift = 0);
300  Void extendRegionBorder(std::vector<AlfLCUInfo*> &vpAlfLCU, Pel* pPelSrc, Int stride, Int formatShift = 0);
301#if !LCU_SYNTAX_ALF 
302  Void filterLumaRegion (std::vector<AlfLCUInfo*> &vpAlfLCU, Pel* imgDec, Pel* imgRest, Int stride, Int filtNo, Int** filterCoeff, Int* mergeTable, Pel** varImg);
303  Void xCUAdaptiveRegion(std::vector<AlfLCUInfo*> &vpAlfLCU, Pel* imgDec, Pel* imgRest, Int stride, Int filtNo, Int** filterCoeff, Int* mergeTable, Pel** varImg);
304#endif
305  Int  getCtrlFlagsFromAlfParam(AlfLCUInfo* pcAlfLCU, Int iAlfDepth, UInt* puiFlags);
306
307  Void xPCMRestoration        (TComPic* pcPic);
308  Void xPCMCURestoration      (TComDataCU* pcCU, UInt uiAbsZorderIdx, UInt uiDepth);
309  Void xPCMSampleRestoration  (TComDataCU* pcCU, UInt uiAbsZorderIdx, UInt uiDepth, TextType ttText);
310
311public: //public methods, interface functions
312
313  TComAdaptiveLoopFilter();
314  virtual ~TComAdaptiveLoopFilter() {}
315
316  Void create  ( Int iPicWidth, Int iPicHeight, UInt uiMaxCUWidth, UInt uiMaxCUHeight, UInt uiMaxCUDepth );
317  Void destroy ();
318
319#if LCU_SYNTAX_ALF
320  Void ALFProcess          (TComPic* pcPic, std::vector<AlfCUCtrlInfo>& vAlfCUCtrlParam, Bool isAlfCoefInSlice);
321  Void resetLCUAlfInfo     ();
322  Int  getNumLCUInPicWidth ()  {return m_numLCUInPicWidth;}
323  Int  getNumLCUInPicHeight() {return m_numLCUInPicHeight;}
324
325  ALFParam*** getAlfLCUParam() {return m_alfFiltInfo;}
326#else
327  Void predictALFCoeffChroma  ( ALFParam* pAlfParam );                  //!< prediction of chroma ALF coefficients
328#if ALF_CHROMA_COEF_PRED_HARMONIZATION
329  Void reconstructALFCoeffChroma( ALFParam* pAlfParam );
330#endif
331  Void ALFProcess             ( TComPic* pcPic, ALFParam* pcAlfParam, std::vector<AlfCUCtrlInfo>& vAlfCUCtrlParam ); ///< interface function for ALF process
332
333  Void allocALFParam  ( ALFParam* pAlfParam ); //!< allocate ALF parameters
334  Void freeALFParam   ( ALFParam* pAlfParam ); //!< free ALF parameters
335  Void copyALFParam   ( ALFParam* pDesAlfParam, ALFParam* pSrcAlfParam ); //!< copy ALF parameters
336#endif
337  Int  getNumCUsInPic()  {return m_uiNumCUsInFrame;} //!< get number of LCU in picture for ALF process
338#if LCU_SYNTAX_ALF
339  Void createPicAlfInfo (TComPic* pcPic, Int uiNumSlicesInPic = 1, Int alfQP = 26);
340#else
341  Void createPicAlfInfo (TComPic* pcPic, Int numSlicesInPic = 1);
342#endif
343  Void destroyPicAlfInfo();
344
345  Void PCMLFDisableProcess    ( TComPic* pcPic);                        ///< interface function for ALF process
346
347protected: //memory allocation
348  Void destroyMatrix_Pel(Pel **m2D);
349  Void destroyMatrix_int(int **m2D);
350  Void initMatrix_int(int ***m2D, int d1, int d2);
351  Void initMatrix_Pel(Pel ***m2D, int d1, int d2);
352  Void destroyMatrix4D_double(double ****m4D, int d1, int d2);
353  Void destroyMatrix3D_double(double ***m3D, int d1);
354  Void destroyMatrix_double(double **m2D);
355  Void initMatrix4D_double(double *****m4D, int d1, int d2, int d3, int d4);
356  Void initMatrix3D_double(double ****m3D, int d1, int d2, int d3);
357  Void initMatrix_double(double ***m2D, int d1, int d2);
358  Void no_mem_exit(const char *where);
359  Void xError(const char *text, int code);
360};
361
362//! \}
363
364#endif
Note: See TracBrowser for help on using the repository browser.