source: 3DVCSoftware/branches/HTM-DEV-0.3-dev1/source/Lib/TLibCommon/TComDataCU.cpp @ 459

Last change on this file since 459 was 459, checked in by hhi, 11 years ago

Integation of depth intra methods in macro H_3D_DIM, including:

  • DMM coding modes in H_3D_DIM_DMM.
  • RBC coding mode in H_3D_DIM_RBC.
  • Property svn:eol-style set to native
File size: 166.9 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-2013, 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     TComDataCU.cpp
35    \brief    CU data structure
36    \todo     not all entities are documented
37*/
38
39#include "TComDataCU.h"
40#include "TComPic.h"
41
42//! \ingroup TLibCommon
43//! \{
44
45#if ADAPTIVE_QP_SELECTION
46Int * TComDataCU::m_pcGlbArlCoeffY  = NULL;
47Int * TComDataCU::m_pcGlbArlCoeffCb = NULL;
48Int * TComDataCU::m_pcGlbArlCoeffCr = NULL;
49#endif
50
51// ====================================================================================================================
52// Constructor / destructor / create / destroy
53// ====================================================================================================================
54
55TComDataCU::TComDataCU()
56{
57  m_pcPic              = NULL;
58  m_pcSlice            = NULL;
59  m_puhDepth           = NULL;
60 
61  m_skipFlag           = NULL;
62
63  m_pePartSize         = NULL;
64  m_pePredMode         = NULL;
65  m_CUTransquantBypass = NULL;
66  m_puhWidth           = NULL;
67  m_puhHeight          = NULL;
68  m_phQP               = NULL;
69  m_pbMergeFlag        = NULL;
70  m_puhMergeIndex      = NULL;
71  m_puhLumaIntraDir    = NULL;
72  m_puhChromaIntraDir  = NULL;
73  m_puhInterDir        = NULL;
74  m_puhTrIdx           = NULL;
75  m_puhTransformSkip[0] = NULL;
76  m_puhTransformSkip[1] = NULL;
77  m_puhTransformSkip[2] = NULL;
78  m_puhCbf[0]          = NULL;
79  m_puhCbf[1]          = NULL;
80  m_puhCbf[2]          = NULL;
81  m_pcTrCoeffY         = NULL;
82  m_pcTrCoeffCb        = NULL;
83  m_pcTrCoeffCr        = NULL;
84#if ADAPTIVE_QP_SELECTION 
85  m_ArlCoeffIsAliasedAllocation = false;
86  m_pcArlCoeffY        = NULL;
87  m_pcArlCoeffCb       = NULL;
88  m_pcArlCoeffCr       = NULL;
89#endif
90 
91  m_pbIPCMFlag         = NULL;
92  m_pcIPCMSampleY      = NULL;
93  m_pcIPCMSampleCb     = NULL;
94  m_pcIPCMSampleCr     = NULL;
95
96  m_pcPattern          = NULL;
97 
98  m_pcCUAboveLeft      = NULL;
99  m_pcCUAboveRight     = NULL;
100  m_pcCUAbove          = NULL;
101  m_pcCULeft           = NULL;
102 
103  m_apcCUColocated[0]  = NULL;
104  m_apcCUColocated[1]  = NULL;
105 
106  m_apiMVPIdx[0]       = NULL;
107  m_apiMVPIdx[1]       = NULL;
108  m_apiMVPNum[0]       = NULL;
109  m_apiMVPNum[1]       = NULL;
110
111#if H_3D_DIM
112  for( Int i = 0; i < DIM_NUM_TYPE; i++ )
113  {
114    m_dimDeltaDC[i][0] = NULL; 
115    m_dimDeltaDC[i][1] = NULL;
116  }
117#if H_3D_DIM_DMM
118  for( Int i = 0; i < DMM_NUM_TYPE; i++ )
119  {
120    m_dmmWedgeTabIdx[i] = NULL;
121  }
122  m_dmm2DeltaEnd    = NULL;
123  m_dmm3IntraTabIdx = NULL;
124#endif
125#if H_3D_DIM_RBC
126  m_pucEdgeCode     = NULL;
127  m_pucEdgeNumber   = NULL;
128  m_pucEdgeStartPos = NULL;
129  m_pbEdgeLeftFirst = NULL;
130  m_pbEdgePartition = NULL;
131#endif
132#endif
133
134  m_bDecSubCu          = false;
135  m_sliceStartCU        = 0;
136  m_sliceSegmentStartCU = 0;
137}
138
139TComDataCU::~TComDataCU()
140{
141}
142
143Void TComDataCU::create(UInt uiNumPartition, UInt uiWidth, UInt uiHeight, Bool bDecSubCu, Int unitSize
144#if ADAPTIVE_QP_SELECTION
145                        , Bool bGlobalRMARLBuffer
146#endif                                             
147                        )
148{
149  m_bDecSubCu = bDecSubCu;
150 
151  m_pcPic              = NULL;
152  m_pcSlice            = NULL;
153  m_uiNumPartition     = uiNumPartition;
154  m_unitSize = unitSize;
155 
156  if ( !bDecSubCu )
157  {
158    m_phQP               = (Char*     )xMalloc(Char,     uiNumPartition);
159    m_puhDepth           = (UChar*    )xMalloc(UChar,    uiNumPartition);
160    m_puhWidth           = (UChar*    )xMalloc(UChar,    uiNumPartition);
161    m_puhHeight          = (UChar*    )xMalloc(UChar,    uiNumPartition);
162
163    m_skipFlag           = new Bool[ uiNumPartition ];
164
165    m_pePartSize         = new Char[ uiNumPartition ];
166    memset( m_pePartSize, SIZE_NONE,uiNumPartition * sizeof( *m_pePartSize ) );
167    m_pePredMode         = new Char[ uiNumPartition ];
168    m_CUTransquantBypass = new Bool[ uiNumPartition ];
169    m_pbMergeFlag        = (Bool*  )xMalloc(Bool,   uiNumPartition);
170    m_puhMergeIndex      = (UChar* )xMalloc(UChar,  uiNumPartition);
171    m_puhLumaIntraDir    = (UChar* )xMalloc(UChar,  uiNumPartition);
172    m_puhChromaIntraDir  = (UChar* )xMalloc(UChar,  uiNumPartition);
173    m_puhInterDir        = (UChar* )xMalloc(UChar,  uiNumPartition);
174   
175    m_puhTrIdx           = (UChar* )xMalloc(UChar,  uiNumPartition);
176    m_puhTransformSkip[0] = (UChar* )xMalloc(UChar,  uiNumPartition);
177    m_puhTransformSkip[1] = (UChar* )xMalloc(UChar,  uiNumPartition);
178    m_puhTransformSkip[2] = (UChar* )xMalloc(UChar,  uiNumPartition);
179
180    m_puhCbf[0]          = (UChar* )xMalloc(UChar,  uiNumPartition);
181    m_puhCbf[1]          = (UChar* )xMalloc(UChar,  uiNumPartition);
182    m_puhCbf[2]          = (UChar* )xMalloc(UChar,  uiNumPartition);
183   
184    m_apiMVPIdx[0]       = new Char[ uiNumPartition ];
185    m_apiMVPIdx[1]       = new Char[ uiNumPartition ];
186    m_apiMVPNum[0]       = new Char[ uiNumPartition ];
187    m_apiMVPNum[1]       = new Char[ uiNumPartition ];
188    memset( m_apiMVPIdx[0], -1,uiNumPartition * sizeof( Char ) );
189    memset( m_apiMVPIdx[1], -1,uiNumPartition * sizeof( Char ) );
190   
191    m_pcTrCoeffY         = (TCoeff*)xMalloc(TCoeff, uiWidth*uiHeight);
192    m_pcTrCoeffCb        = (TCoeff*)xMalloc(TCoeff, uiWidth*uiHeight/4);
193    m_pcTrCoeffCr        = (TCoeff*)xMalloc(TCoeff, uiWidth*uiHeight/4);
194    memset( m_pcTrCoeffY, 0,uiWidth*uiHeight * sizeof( TCoeff ) );
195    memset( m_pcTrCoeffCb, 0,uiWidth*uiHeight/4 * sizeof( TCoeff ) );
196    memset( m_pcTrCoeffCr, 0,uiWidth*uiHeight/4 * sizeof( TCoeff ) );
197#if ADAPTIVE_QP_SELECTION   
198    if( bGlobalRMARLBuffer )
199    {
200      if( m_pcGlbArlCoeffY == NULL )
201      {
202        m_pcGlbArlCoeffY   = (Int*)xMalloc(Int, uiWidth*uiHeight);
203        m_pcGlbArlCoeffCb  = (Int*)xMalloc(Int, uiWidth*uiHeight/4);
204        m_pcGlbArlCoeffCr  = (Int*)xMalloc(Int, uiWidth*uiHeight/4);
205      }
206      m_pcArlCoeffY        = m_pcGlbArlCoeffY;
207      m_pcArlCoeffCb       = m_pcGlbArlCoeffCb;
208      m_pcArlCoeffCr       = m_pcGlbArlCoeffCr;
209      m_ArlCoeffIsAliasedAllocation = true;
210    }
211    else
212    {
213      m_pcArlCoeffY        = (Int*)xMalloc(Int, uiWidth*uiHeight);
214      m_pcArlCoeffCb       = (Int*)xMalloc(Int, uiWidth*uiHeight/4);
215      m_pcArlCoeffCr       = (Int*)xMalloc(Int, uiWidth*uiHeight/4);
216    }
217#endif
218   
219    m_pbIPCMFlag         = (Bool*  )xMalloc(Bool, uiNumPartition);
220    m_pcIPCMSampleY      = (Pel*   )xMalloc(Pel , uiWidth*uiHeight);
221    m_pcIPCMSampleCb     = (Pel*   )xMalloc(Pel , uiWidth*uiHeight/4);
222    m_pcIPCMSampleCr     = (Pel*   )xMalloc(Pel , uiWidth*uiHeight/4);
223
224    m_acCUMvField[0].create( uiNumPartition );
225    m_acCUMvField[1].create( uiNumPartition );
226   
227#if H_3D_DIM
228    for( Int i = 0; i < DIM_NUM_TYPE; i++ )
229    {
230      m_dimDeltaDC[i][0] = (Pel* )xMalloc(Pel, uiNumPartition); 
231      m_dimDeltaDC[i][1] = (Pel* )xMalloc(Pel, uiNumPartition);
232    }
233#if H_3D_DIM_DMM
234    for( Int i = 0; i < DMM_NUM_TYPE; i++ )
235    {
236      m_dmmWedgeTabIdx[i]    = (UInt*)xMalloc(UInt, uiNumPartition);
237    }
238    m_dmm2DeltaEnd    = (Int* )xMalloc(Int,  uiNumPartition);
239    m_dmm3IntraTabIdx = (UInt*)xMalloc(UInt, uiNumPartition);
240#endif
241#if H_3D_DIM_RBC
242    m_pucEdgeCode     = (UChar*)xMalloc(UChar, uiNumPartition * RBC_MAX_EDGE_NUM_PER_4x4);
243    m_pucEdgeNumber   = (UChar*)xMalloc(UChar, uiNumPartition);
244    m_pucEdgeStartPos = (UChar*)xMalloc(UChar, uiNumPartition);
245    m_pbEdgeLeftFirst = (Bool*)xMalloc(Bool, uiNumPartition);
246    m_pbEdgePartition = (Bool*)xMalloc(Bool, uiNumPartition * 16);
247#endif
248#endif
249  }
250  else
251  {
252    m_acCUMvField[0].setNumPartition(uiNumPartition );
253    m_acCUMvField[1].setNumPartition(uiNumPartition );
254  }
255 
256  m_sliceStartCU        = (UInt*  )xMalloc(UInt, uiNumPartition);
257  m_sliceSegmentStartCU = (UInt*  )xMalloc(UInt, uiNumPartition);
258 
259  // create pattern memory
260  m_pcPattern            = (TComPattern*)xMalloc(TComPattern, 1);
261 
262  // create motion vector fields
263 
264  m_pcCUAboveLeft      = NULL;
265  m_pcCUAboveRight     = NULL;
266  m_pcCUAbove          = NULL;
267  m_pcCULeft           = NULL;
268 
269  m_apcCUColocated[0]  = NULL;
270  m_apcCUColocated[1]  = NULL;
271}
272
273Void TComDataCU::destroy()
274{
275  m_pcPic              = NULL;
276  m_pcSlice            = NULL;
277 
278  if ( m_pcPattern )
279  { 
280    xFree(m_pcPattern);
281    m_pcPattern = NULL;
282  }
283 
284  // encoder-side buffer free
285  if ( !m_bDecSubCu )
286  {
287    if ( m_phQP               ) { xFree(m_phQP);                m_phQP              = NULL; }
288    if ( m_puhDepth           ) { xFree(m_puhDepth);            m_puhDepth          = NULL; }
289    if ( m_puhWidth           ) { xFree(m_puhWidth);            m_puhWidth          = NULL; }
290    if ( m_puhHeight          ) { xFree(m_puhHeight);           m_puhHeight         = NULL; }
291
292    if ( m_skipFlag           ) { delete[] m_skipFlag;          m_skipFlag          = NULL; }
293
294    if ( m_pePartSize         ) { delete[] m_pePartSize;        m_pePartSize        = NULL; }
295    if ( m_pePredMode         ) { delete[] m_pePredMode;        m_pePredMode        = NULL; }
296    if ( m_CUTransquantBypass ) { delete[] m_CUTransquantBypass;m_CUTransquantBypass = NULL; }
297    if ( m_puhCbf[0]          ) { xFree(m_puhCbf[0]);           m_puhCbf[0]         = NULL; }
298    if ( m_puhCbf[1]          ) { xFree(m_puhCbf[1]);           m_puhCbf[1]         = NULL; }
299    if ( m_puhCbf[2]          ) { xFree(m_puhCbf[2]);           m_puhCbf[2]         = NULL; }
300    if ( m_puhInterDir        ) { xFree(m_puhInterDir);         m_puhInterDir       = NULL; }
301    if ( m_pbMergeFlag        ) { xFree(m_pbMergeFlag);         m_pbMergeFlag       = NULL; }
302    if ( m_puhMergeIndex      ) { xFree(m_puhMergeIndex);       m_puhMergeIndex     = NULL; }
303    if ( m_puhLumaIntraDir    ) { xFree(m_puhLumaIntraDir);     m_puhLumaIntraDir   = NULL; }
304    if ( m_puhChromaIntraDir  ) { xFree(m_puhChromaIntraDir);   m_puhChromaIntraDir = NULL; }
305    if ( m_puhTrIdx           ) { xFree(m_puhTrIdx);            m_puhTrIdx          = NULL; }
306    if ( m_puhTransformSkip[0]) { xFree(m_puhTransformSkip[0]); m_puhTransformSkip[0] = NULL; }
307    if ( m_puhTransformSkip[1]) { xFree(m_puhTransformSkip[1]); m_puhTransformSkip[1] = NULL; }
308    if ( m_puhTransformSkip[2]) { xFree(m_puhTransformSkip[2]); m_puhTransformSkip[2] = NULL; }
309    if ( m_pcTrCoeffY         ) { xFree(m_pcTrCoeffY);          m_pcTrCoeffY        = NULL; }
310    if ( m_pcTrCoeffCb        ) { xFree(m_pcTrCoeffCb);         m_pcTrCoeffCb       = NULL; }
311    if ( m_pcTrCoeffCr        ) { xFree(m_pcTrCoeffCr);         m_pcTrCoeffCr       = NULL; }
312#if ADAPTIVE_QP_SELECTION
313    if (!m_ArlCoeffIsAliasedAllocation)
314    {
315      xFree(m_pcArlCoeffY); m_pcArlCoeffY = 0;
316      xFree(m_pcArlCoeffCb); m_pcArlCoeffCb = 0;
317      xFree(m_pcArlCoeffCr); m_pcArlCoeffCr = 0;
318    }
319    if ( m_pcGlbArlCoeffY     ) { xFree(m_pcGlbArlCoeffY);      m_pcGlbArlCoeffY    = NULL; }
320    if ( m_pcGlbArlCoeffCb    ) { xFree(m_pcGlbArlCoeffCb);     m_pcGlbArlCoeffCb   = NULL; }
321    if ( m_pcGlbArlCoeffCr    ) { xFree(m_pcGlbArlCoeffCr);     m_pcGlbArlCoeffCr   = NULL; }
322#endif
323    if ( m_pbIPCMFlag         ) { xFree(m_pbIPCMFlag   );       m_pbIPCMFlag        = NULL; }
324    if ( m_pcIPCMSampleY      ) { xFree(m_pcIPCMSampleY);       m_pcIPCMSampleY     = NULL; }
325    if ( m_pcIPCMSampleCb     ) { xFree(m_pcIPCMSampleCb);      m_pcIPCMSampleCb    = NULL; }
326    if ( m_pcIPCMSampleCr     ) { xFree(m_pcIPCMSampleCr);      m_pcIPCMSampleCr    = NULL; }
327    if ( m_apiMVPIdx[0]       ) { delete[] m_apiMVPIdx[0];      m_apiMVPIdx[0]      = NULL; }
328    if ( m_apiMVPIdx[1]       ) { delete[] m_apiMVPIdx[1];      m_apiMVPIdx[1]      = NULL; }
329    if ( m_apiMVPNum[0]       ) { delete[] m_apiMVPNum[0];      m_apiMVPNum[0]      = NULL; }
330    if ( m_apiMVPNum[1]       ) { delete[] m_apiMVPNum[1];      m_apiMVPNum[1]      = NULL; }
331   
332    m_acCUMvField[0].destroy();
333    m_acCUMvField[1].destroy();
334   
335#if H_3D_DIM
336    for( Int i = 0; i < DIM_NUM_TYPE; i++ )
337    {
338      if ( m_dimDeltaDC[i][0] ) { xFree( m_dimDeltaDC[i][0] ); m_dimDeltaDC[i][0] = NULL; }
339      if ( m_dimDeltaDC[i][1] ) { xFree( m_dimDeltaDC[i][1] ); m_dimDeltaDC[i][1] = NULL; }
340    }
341#if H_3D_DIM_DMM
342    for( Int i = 0; i < DMM_NUM_TYPE; i++ )
343    {
344      if ( m_dmmWedgeTabIdx[i] ) { xFree( m_dmmWedgeTabIdx[i] ); m_dmmWedgeTabIdx[i] = NULL; }
345    }
346    if ( m_dmm2DeltaEnd    ) { xFree( m_dmm2DeltaEnd    ); m_dmm2DeltaEnd    = NULL; }
347    if ( m_dmm3IntraTabIdx ) { xFree( m_dmm3IntraTabIdx ); m_dmm3IntraTabIdx = NULL; }
348#endif
349#if H_3D_DIM_RBC
350    if ( m_pbEdgeLeftFirst ) { xFree( m_pbEdgeLeftFirst ); m_pbEdgeLeftFirst = NULL; }
351    if ( m_pucEdgeStartPos ) { xFree( m_pucEdgeStartPos ); m_pucEdgeStartPos = NULL; }
352    if ( m_pucEdgeNumber   ) { xFree( m_pucEdgeNumber   ); m_pucEdgeNumber   = NULL; }
353    if ( m_pucEdgeCode     ) { xFree( m_pucEdgeCode     ); m_pucEdgeCode     = NULL; }
354    if ( m_pbEdgePartition ) { xFree( m_pbEdgePartition ); m_pbEdgePartition = NULL; }
355#endif
356#endif
357  }
358 
359  m_pcCUAboveLeft       = NULL;
360  m_pcCUAboveRight      = NULL;
361  m_pcCUAbove           = NULL;
362  m_pcCULeft            = NULL;
363 
364  m_apcCUColocated[0]   = NULL;
365  m_apcCUColocated[1]   = NULL;
366
367  if( m_sliceStartCU )
368  {
369    xFree(m_sliceStartCU);
370    m_sliceStartCU=NULL;
371  }
372  if(m_sliceSegmentStartCU )
373  {
374    xFree(m_sliceSegmentStartCU);
375    m_sliceSegmentStartCU=NULL;
376  }
377}
378
379const NDBFBlockInfo& NDBFBlockInfo::operator= (const NDBFBlockInfo& src)
380{
381  this->tileID = src.tileID;
382  this->sliceID= src.sliceID;
383  this->startSU= src.startSU;
384  this->endSU  = src.endSU;
385  this->widthSU= src.widthSU;
386  this->heightSU=src.heightSU;
387  this->posX   = src.posX;
388  this->posY   = src.posY;
389  this->width  = src.width;
390  this->height = src.height;
391  ::memcpy(this->isBorderAvailable, src.isBorderAvailable, sizeof(Bool)*((Int)NUM_SGU_BORDER));
392  this->allBordersAvailable = src.allBordersAvailable;
393
394  return *this;
395}
396
397
398// ====================================================================================================================
399// Public member functions
400// ====================================================================================================================
401
402// --------------------------------------------------------------------------------------------------------------------
403// Initialization
404// --------------------------------------------------------------------------------------------------------------------
405
406/**
407 - initialize top-level CU
408 - internal buffers are already created
409 - set values before encoding a CU
410 .
411 \param  pcPic     picture (TComPic) class pointer
412 \param  iCUAddr   CU address
413 */
414Void TComDataCU::initCU( TComPic* pcPic, UInt iCUAddr )
415{
416
417  m_pcPic              = pcPic;
418  m_pcSlice            = pcPic->getSlice(pcPic->getCurrSliceIdx());
419  m_uiCUAddr           = iCUAddr;
420  m_uiCUPelX           = ( iCUAddr % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth;
421  m_uiCUPelY           = ( iCUAddr / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight;
422  m_uiAbsIdxInLCU      = 0;
423  m_dTotalCost         = MAX_DOUBLE;
424  m_uiTotalDistortion  = 0;
425  m_uiTotalBits        = 0;
426  m_uiTotalBins        = 0;
427  m_uiNumPartition     = pcPic->getNumPartInCU();
428 
429  for(Int i=0; i<pcPic->getNumPartInCU(); i++)
430  {
431    if(pcPic->getPicSym()->getInverseCUOrderMap(iCUAddr)*pcPic->getNumPartInCU()+i>=getSlice()->getSliceCurStartCUAddr())
432    {
433      m_sliceStartCU[i]=getSlice()->getSliceCurStartCUAddr();
434    }
435    else
436    {
437      m_sliceStartCU[i]=pcPic->getCU(getAddr())->m_sliceStartCU[i];
438    }
439  }
440  for(Int i=0; i<pcPic->getNumPartInCU(); i++)
441  {
442    if(pcPic->getPicSym()->getInverseCUOrderMap(iCUAddr)*pcPic->getNumPartInCU()+i>=getSlice()->getSliceSegmentCurStartCUAddr())
443    {
444      m_sliceSegmentStartCU[i]=getSlice()->getSliceSegmentCurStartCUAddr();
445    }
446    else
447    {
448      m_sliceSegmentStartCU[i]=pcPic->getCU(getAddr())->m_sliceSegmentStartCU[i];
449    }
450  }
451
452  Int partStartIdx = getSlice()->getSliceSegmentCurStartCUAddr() - pcPic->getPicSym()->getInverseCUOrderMap(iCUAddr) * pcPic->getNumPartInCU();
453
454  Int numElements = min<Int>( partStartIdx, m_uiNumPartition );
455  for ( Int ui = 0; ui < numElements; ui++ )
456  {
457    TComDataCU * pcFrom = pcPic->getCU(getAddr());
458    m_skipFlag[ui]   = pcFrom->getSkipFlag(ui);
459    m_pePartSize[ui] = pcFrom->getPartitionSize(ui);
460    m_pePredMode[ui] = pcFrom->getPredictionMode(ui);
461    m_CUTransquantBypass[ui] = pcFrom->getCUTransquantBypass(ui);
462    m_puhDepth[ui] = pcFrom->getDepth(ui);
463    m_puhWidth  [ui] = pcFrom->getWidth(ui);
464    m_puhHeight [ui] = pcFrom->getHeight(ui);
465    m_puhTrIdx  [ui] = pcFrom->getTransformIdx(ui);
466    m_puhTransformSkip[0][ui] = pcFrom->getTransformSkip(ui,TEXT_LUMA);
467    m_puhTransformSkip[1][ui] = pcFrom->getTransformSkip(ui,TEXT_CHROMA_U);
468    m_puhTransformSkip[2][ui] = pcFrom->getTransformSkip(ui,TEXT_CHROMA_V);
469    m_apiMVPIdx[0][ui] = pcFrom->m_apiMVPIdx[0][ui];;
470    m_apiMVPIdx[1][ui] = pcFrom->m_apiMVPIdx[1][ui];
471    m_apiMVPNum[0][ui] = pcFrom->m_apiMVPNum[0][ui];
472    m_apiMVPNum[1][ui] = pcFrom->m_apiMVPNum[1][ui];
473    m_phQP[ui]=pcFrom->m_phQP[ui];
474    m_pbMergeFlag[ui]=pcFrom->m_pbMergeFlag[ui];
475    m_puhMergeIndex[ui]=pcFrom->m_puhMergeIndex[ui];
476    m_puhLumaIntraDir[ui]=pcFrom->m_puhLumaIntraDir[ui];
477    m_puhChromaIntraDir[ui]=pcFrom->m_puhChromaIntraDir[ui];
478    m_puhInterDir[ui]=pcFrom->m_puhInterDir[ui];
479    m_puhCbf[0][ui]=pcFrom->m_puhCbf[0][ui];
480    m_puhCbf[1][ui]=pcFrom->m_puhCbf[1][ui];
481    m_puhCbf[2][ui]=pcFrom->m_puhCbf[2][ui];
482    m_pbIPCMFlag[ui] = pcFrom->m_pbIPCMFlag[ui];
483  }
484 
485  Int firstElement = max<Int>( partStartIdx, 0 );
486  numElements = m_uiNumPartition - firstElement;
487 
488  if ( numElements > 0 )
489  {
490    memset( m_skipFlag          + firstElement, false,                    numElements * sizeof( *m_skipFlag ) );
491
492    memset( m_pePartSize        + firstElement, SIZE_NONE,                numElements * sizeof( *m_pePartSize ) );
493    memset( m_pePredMode        + firstElement, MODE_NONE,                numElements * sizeof( *m_pePredMode ) );
494    memset( m_CUTransquantBypass+ firstElement, false,                    numElements * sizeof( *m_CUTransquantBypass) );
495    memset( m_puhDepth          + firstElement, 0,                        numElements * sizeof( *m_puhDepth ) );
496    memset( m_puhTrIdx          + firstElement, 0,                        numElements * sizeof( *m_puhTrIdx ) );
497    memset( m_puhTransformSkip[0] + firstElement, 0,                      numElements * sizeof( *m_puhTransformSkip[0]) );
498    memset( m_puhTransformSkip[1] + firstElement, 0,                      numElements * sizeof( *m_puhTransformSkip[1]) );
499    memset( m_puhTransformSkip[2] + firstElement, 0,                      numElements * sizeof( *m_puhTransformSkip[2]) );
500    memset( m_puhWidth          + firstElement, g_uiMaxCUWidth,           numElements * sizeof( *m_puhWidth ) );
501    memset( m_puhHeight         + firstElement, g_uiMaxCUHeight,          numElements * sizeof( *m_puhHeight ) );
502    memset( m_apiMVPIdx[0]      + firstElement, -1,                       numElements * sizeof( *m_apiMVPIdx[0] ) );
503    memset( m_apiMVPIdx[1]      + firstElement, -1,                       numElements * sizeof( *m_apiMVPIdx[1] ) );
504    memset( m_apiMVPNum[0]      + firstElement, -1,                       numElements * sizeof( *m_apiMVPNum[0] ) );
505    memset( m_apiMVPNum[1]      + firstElement, -1,                       numElements * sizeof( *m_apiMVPNum[1] ) );
506    memset( m_phQP              + firstElement, getSlice()->getSliceQp(), numElements * sizeof( *m_phQP ) );
507    memset( m_pbMergeFlag       + firstElement, false,                    numElements * sizeof( *m_pbMergeFlag ) );
508    memset( m_puhMergeIndex     + firstElement, 0,                        numElements * sizeof( *m_puhMergeIndex ) );
509    memset( m_puhLumaIntraDir   + firstElement, DC_IDX,                   numElements * sizeof( *m_puhLumaIntraDir ) );
510    memset( m_puhChromaIntraDir + firstElement, 0,                        numElements * sizeof( *m_puhChromaIntraDir ) );
511    memset( m_puhInterDir       + firstElement, 0,                        numElements * sizeof( *m_puhInterDir ) );
512    memset( m_puhCbf[0]         + firstElement, 0,                        numElements * sizeof( *m_puhCbf[0] ) );
513    memset( m_puhCbf[1]         + firstElement, 0,                        numElements * sizeof( *m_puhCbf[1] ) );
514    memset( m_puhCbf[2]         + firstElement, 0,                        numElements * sizeof( *m_puhCbf[2] ) );
515    memset( m_pbIPCMFlag        + firstElement, false,                    numElements * sizeof( *m_pbIPCMFlag ) );
516
517#if H_3D_DIM
518    for( Int i = 0; i < DIM_NUM_TYPE; i++ )
519    {
520      memset( m_dimDeltaDC[i][0] + firstElement, 0,                       numElements * sizeof( *m_dimDeltaDC[i][0] ) );
521      memset( m_dimDeltaDC[i][1] + firstElement, 0,                       numElements * sizeof( *m_dimDeltaDC[i][1] ) );
522    }
523#if H_3D_DIM_DMM
524    for( Int i = 0; i < DMM_NUM_TYPE; i++ )
525    {
526      memset( m_dmmWedgeTabIdx[i] + firstElement, 0,                      numElements * sizeof( *m_dmmWedgeTabIdx[i] ) );
527    }
528    memset( m_dmm2DeltaEnd      + firstElement, 0,                        numElements * sizeof( *m_dmm2DeltaEnd    ) );
529    memset( m_dmm3IntraTabIdx   + firstElement, 0,                        numElements * sizeof( *m_dmm3IntraTabIdx ) );
530#endif
531#if H_3D_DIM_RBC
532    memset( m_pucEdgeCode       + firstElement, 0,                        numElements * sizeof( *m_pucEdgeCode     ) * RBC_MAX_EDGE_NUM_PER_4x4 );
533    memset( m_pucEdgeNumber     + firstElement, 0,                        numElements * sizeof( *m_pucEdgeNumber   ) );
534    memset( m_pucEdgeStartPos   + firstElement, 0,                        numElements * sizeof( *m_pucEdgeStartPos ) );
535    memset( m_pbEdgeLeftFirst   + firstElement, false,                    numElements * sizeof( *m_pbEdgeLeftFirst ) );
536    memset( m_pbEdgePartition   + firstElement, false,                    numElements * sizeof( *m_pbEdgePartition ) * 16 );
537#endif
538#endif
539  }
540 
541  UInt uiTmp = g_uiMaxCUWidth*g_uiMaxCUHeight;
542  if ( 0 >= partStartIdx ) 
543  {
544    m_acCUMvField[0].clearMvField();
545    m_acCUMvField[1].clearMvField();
546    memset( m_pcTrCoeffY , 0, sizeof( TCoeff ) * uiTmp );
547#if ADAPTIVE_QP_SELECTION
548    memset( m_pcArlCoeffY , 0, sizeof( Int ) * uiTmp ); 
549#endif
550    memset( m_pcIPCMSampleY , 0, sizeof( Pel ) * uiTmp );
551    uiTmp  >>= 2;
552    memset( m_pcTrCoeffCb, 0, sizeof( TCoeff ) * uiTmp );
553    memset( m_pcTrCoeffCr, 0, sizeof( TCoeff ) * uiTmp );
554#if ADAPTIVE_QP_SELECTION 
555    memset( m_pcArlCoeffCb, 0, sizeof( Int ) * uiTmp );
556    memset( m_pcArlCoeffCr, 0, sizeof( Int ) * uiTmp );
557#endif
558    memset( m_pcIPCMSampleCb , 0, sizeof( Pel ) * uiTmp );
559    memset( m_pcIPCMSampleCr , 0, sizeof( Pel ) * uiTmp );
560  }
561  else 
562  {
563    TComDataCU * pcFrom = pcPic->getCU(getAddr());
564    m_acCUMvField[0].copyFrom(&pcFrom->m_acCUMvField[0],m_uiNumPartition,0);
565    m_acCUMvField[1].copyFrom(&pcFrom->m_acCUMvField[1],m_uiNumPartition,0);
566    for(Int i=0; i<uiTmp; i++)
567    {
568      m_pcTrCoeffY[i]=pcFrom->m_pcTrCoeffY[i];
569#if ADAPTIVE_QP_SELECTION
570      m_pcArlCoeffY[i]=pcFrom->m_pcArlCoeffY[i];
571#endif
572      m_pcIPCMSampleY[i]=pcFrom->m_pcIPCMSampleY[i];
573    }
574    for(Int i=0; i<(uiTmp>>2); i++)
575    {
576      m_pcTrCoeffCb[i]=pcFrom->m_pcTrCoeffCb[i];
577      m_pcTrCoeffCr[i]=pcFrom->m_pcTrCoeffCr[i];
578#if ADAPTIVE_QP_SELECTION
579      m_pcArlCoeffCb[i]=pcFrom->m_pcArlCoeffCb[i];
580      m_pcArlCoeffCr[i]=pcFrom->m_pcArlCoeffCr[i];
581#endif
582      m_pcIPCMSampleCb[i]=pcFrom->m_pcIPCMSampleCb[i];
583      m_pcIPCMSampleCr[i]=pcFrom->m_pcIPCMSampleCr[i];
584    }
585  }
586
587  // Setting neighbor CU
588  m_pcCULeft        = NULL;
589  m_pcCUAbove       = NULL;
590  m_pcCUAboveLeft   = NULL;
591  m_pcCUAboveRight  = NULL;
592
593  m_apcCUColocated[0] = NULL;
594  m_apcCUColocated[1] = NULL;
595
596  UInt uiWidthInCU = pcPic->getFrameWidthInCU();
597  if ( m_uiCUAddr % uiWidthInCU )
598  {
599    m_pcCULeft = pcPic->getCU( m_uiCUAddr - 1 );
600  }
601
602  if ( m_uiCUAddr / uiWidthInCU )
603  {
604    m_pcCUAbove = pcPic->getCU( m_uiCUAddr - uiWidthInCU );
605  }
606
607  if ( m_pcCULeft && m_pcCUAbove )
608  {
609    m_pcCUAboveLeft = pcPic->getCU( m_uiCUAddr - uiWidthInCU - 1 );
610  }
611
612  if ( m_pcCUAbove && ( (m_uiCUAddr%uiWidthInCU) < (uiWidthInCU-1) )  )
613  {
614    m_pcCUAboveRight = pcPic->getCU( m_uiCUAddr - uiWidthInCU + 1 );
615  }
616
617  if ( getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 )
618  {
619    m_apcCUColocated[0] = getSlice()->getRefPic( REF_PIC_LIST_0, 0)->getCU( m_uiCUAddr );
620  }
621
622  if ( getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) > 0 )
623  {
624    m_apcCUColocated[1] = getSlice()->getRefPic( REF_PIC_LIST_1, 0)->getCU( m_uiCUAddr );
625  }
626}
627
628/** initialize prediction data with enabling sub-LCU-level delta QP
629*\param  uiDepth  depth of the current CU
630*\param  qp     qp for the current CU
631*- set CU width and CU height according to depth
632*- set qp value according to input qp
633*- set last-coded qp value according to input last-coded qp
634*/
635Void TComDataCU::initEstData( UInt uiDepth, Int qp )
636{
637  m_dTotalCost         = MAX_DOUBLE;
638  m_uiTotalDistortion  = 0;
639  m_uiTotalBits        = 0;
640  m_uiTotalBins        = 0;
641
642  UChar uhWidth  = g_uiMaxCUWidth  >> uiDepth;
643  UChar uhHeight = g_uiMaxCUHeight >> uiDepth;
644
645  for (UInt ui = 0; ui < m_uiNumPartition; ui++)
646  {
647    if(getPic()->getPicSym()->getInverseCUOrderMap(getAddr())*m_pcPic->getNumPartInCU()+m_uiAbsIdxInLCU+ui >= getSlice()->getSliceSegmentCurStartCUAddr())
648    {
649      m_apiMVPIdx[0][ui] = -1;
650      m_apiMVPIdx[1][ui] = -1;
651      m_apiMVPNum[0][ui] = -1;
652      m_apiMVPNum[1][ui] = -1;
653      m_puhDepth  [ui] = uiDepth;
654      m_puhWidth  [ui] = uhWidth;
655      m_puhHeight [ui] = uhHeight;
656      m_puhTrIdx  [ui] = 0;
657      m_puhTransformSkip[0][ui] = 0;
658      m_puhTransformSkip[1][ui] = 0;
659      m_puhTransformSkip[2][ui] = 0;
660      m_skipFlag[ui]   = false;
661      m_pePartSize[ui] = SIZE_NONE;
662      m_pePredMode[ui] = MODE_NONE;
663      m_CUTransquantBypass[ui] = false;
664      m_pbIPCMFlag[ui] = 0;
665      m_phQP[ui] = qp;
666      m_pbMergeFlag[ui] = 0;
667      m_puhMergeIndex[ui] = 0;
668      m_puhLumaIntraDir[ui] = DC_IDX;
669      m_puhChromaIntraDir[ui] = 0;
670      m_puhInterDir[ui] = 0;
671      m_puhCbf[0][ui] = 0;
672      m_puhCbf[1][ui] = 0;
673      m_puhCbf[2][ui] = 0;
674
675#if H_3D_DIM
676      for( Int i = 0; i < DIM_NUM_TYPE; i++ )
677      {
678        m_dimDeltaDC[i][0] [ui] = 0;
679        m_dimDeltaDC[i][1] [ui] = 0;
680      }
681#if H_3D_DIM_DMM
682      for( Int i = 0; i < DMM_NUM_TYPE; i++ )
683      {
684        m_dmmWedgeTabIdx[i] [ui] = 0;
685      }
686      m_dmm2DeltaEnd    [ui] = 0;
687      m_dmm3IntraTabIdx [ui] = 0;
688#endif
689#endif
690    }
691  }
692
693  UInt uiTmp = uhWidth*uhHeight;
694
695  if(getPic()->getPicSym()->getInverseCUOrderMap(getAddr())*m_pcPic->getNumPartInCU()+m_uiAbsIdxInLCU >= getSlice()->getSliceSegmentCurStartCUAddr())
696  {
697    m_acCUMvField[0].clearMvField();
698    m_acCUMvField[1].clearMvField();
699    uiTmp = uhWidth*uhHeight;
700   
701    memset( m_pcTrCoeffY,    0, uiTmp * sizeof( *m_pcTrCoeffY    ) );
702#if ADAPTIVE_QP_SELECTION
703    memset( m_pcArlCoeffY ,  0, uiTmp * sizeof( *m_pcArlCoeffY   ) );
704#endif
705    memset( m_pcIPCMSampleY, 0, uiTmp * sizeof( *m_pcIPCMSampleY ) );
706
707    uiTmp>>=2;
708    memset( m_pcTrCoeffCb,    0, uiTmp * sizeof( *m_pcTrCoeffCb    ) );
709    memset( m_pcTrCoeffCr,    0, uiTmp * sizeof( *m_pcTrCoeffCr    ) );
710#if ADAPTIVE_QP_SELECTION 
711    memset( m_pcArlCoeffCb,   0, uiTmp * sizeof( *m_pcArlCoeffCb   ) );
712    memset( m_pcArlCoeffCr,   0, uiTmp * sizeof( *m_pcArlCoeffCr   ) );
713#endif
714    memset( m_pcIPCMSampleCb, 0, uiTmp * sizeof( *m_pcIPCMSampleCb ) );
715    memset( m_pcIPCMSampleCr, 0, uiTmp * sizeof( *m_pcIPCMSampleCr ) );
716  }
717}
718
719
720// initialize Sub partition
721Void TComDataCU::initSubCU( TComDataCU* pcCU, UInt uiPartUnitIdx, UInt uiDepth, Int qp )
722{
723  assert( uiPartUnitIdx<4 );
724
725  UInt uiPartOffset = ( pcCU->getTotalNumPart()>>2 )*uiPartUnitIdx;
726
727  m_pcPic              = pcCU->getPic();
728  m_pcSlice            = m_pcPic->getSlice(m_pcPic->getCurrSliceIdx());
729  m_uiCUAddr           = pcCU->getAddr();
730  m_uiAbsIdxInLCU      = pcCU->getZorderIdxInCU() + uiPartOffset;
731
732  m_uiCUPelX           = pcCU->getCUPelX() + ( g_uiMaxCUWidth>>uiDepth  )*( uiPartUnitIdx &  1 );
733  m_uiCUPelY           = pcCU->getCUPelY() + ( g_uiMaxCUHeight>>uiDepth  )*( uiPartUnitIdx >> 1 );
734
735  m_dTotalCost         = MAX_DOUBLE;
736  m_uiTotalDistortion  = 0;
737  m_uiTotalBits        = 0;
738  m_uiTotalBins        = 0;
739  m_uiNumPartition     = pcCU->getTotalNumPart() >> 2;
740
741  Int iSizeInUchar = sizeof( UChar  ) * m_uiNumPartition;
742  Int iSizeInBool  = sizeof( Bool   ) * m_uiNumPartition;
743
744  Int sizeInChar = sizeof( Char  ) * m_uiNumPartition;
745  memset( m_phQP,              qp,  sizeInChar );
746
747  memset( m_pbMergeFlag,        0, iSizeInBool  );
748  memset( m_puhMergeIndex,      0, iSizeInUchar );
749  memset( m_puhLumaIntraDir,    DC_IDX, iSizeInUchar );
750  memset( m_puhChromaIntraDir,  0, iSizeInUchar );
751  memset( m_puhInterDir,        0, iSizeInUchar );
752  memset( m_puhTrIdx,           0, iSizeInUchar );
753  memset( m_puhTransformSkip[0], 0, iSizeInUchar );
754  memset( m_puhTransformSkip[1], 0, iSizeInUchar );
755  memset( m_puhTransformSkip[2], 0, iSizeInUchar );
756  memset( m_puhCbf[0],          0, iSizeInUchar );
757  memset( m_puhCbf[1],          0, iSizeInUchar );
758  memset( m_puhCbf[2],          0, iSizeInUchar );
759  memset( m_puhDepth,     uiDepth, iSizeInUchar );
760
761  UChar uhWidth  = g_uiMaxCUWidth  >> uiDepth;
762  UChar uhHeight = g_uiMaxCUHeight >> uiDepth;
763  memset( m_puhWidth,          uhWidth,  iSizeInUchar );
764  memset( m_puhHeight,         uhHeight, iSizeInUchar );
765  memset( m_pbIPCMFlag,        0, iSizeInBool  );
766 
767#if H_3D_DIM
768  for( Int i = 0; i < DIM_NUM_TYPE; i++ )
769  {
770    memset( m_dimDeltaDC[i][0], 0, sizeof(Pel ) * m_uiNumPartition );
771    memset( m_dimDeltaDC[i][1], 0, sizeof(Pel ) * m_uiNumPartition );
772  }
773#if H_3D_DIM_DMM
774  for( Int i = 0; i < DMM_NUM_TYPE; i++ )
775  {
776    memset( m_dmmWedgeTabIdx[i], 0, sizeof(UInt) * m_uiNumPartition );
777  }
778  memset( m_dmm2DeltaEnd   , 0, sizeof(Int ) * m_uiNumPartition );
779  memset( m_dmm3IntraTabIdx, 0, sizeof(UInt) * m_uiNumPartition );
780#endif
781#if H_3D_DIM_RBC
782  memset( m_pucEdgeCode    , 0, iSizeInUchar * RBC_MAX_EDGE_NUM_PER_4x4 );
783  memset( m_pucEdgeNumber  , 0, iSizeInUchar );
784  memset( m_pucEdgeStartPos, 0, iSizeInUchar );
785  memset( m_pbEdgeLeftFirst, 0, iSizeInBool );
786  memset( m_pbEdgePartition, 0, iSizeInBool * 16 );
787#endif
788#endif
789 
790  for (UInt ui = 0; ui < m_uiNumPartition; ui++)
791  {
792    m_skipFlag[ui]   = false;
793    m_pePartSize[ui] = SIZE_NONE;
794    m_pePredMode[ui] = MODE_NONE;
795    m_CUTransquantBypass[ui] = false;
796    m_apiMVPIdx[0][ui] = -1;
797    m_apiMVPIdx[1][ui] = -1;
798    m_apiMVPNum[0][ui] = -1;
799    m_apiMVPNum[1][ui] = -1;
800    if(m_pcPic->getPicSym()->getInverseCUOrderMap(getAddr())*m_pcPic->getNumPartInCU()+m_uiAbsIdxInLCU+ui<getSlice()->getSliceSegmentCurStartCUAddr())
801    {
802      m_apiMVPIdx[0][ui] = pcCU->m_apiMVPIdx[0][uiPartOffset+ui];
803      m_apiMVPIdx[1][ui] = pcCU->m_apiMVPIdx[1][uiPartOffset+ui];;
804      m_apiMVPNum[0][ui] = pcCU->m_apiMVPNum[0][uiPartOffset+ui];;
805      m_apiMVPNum[1][ui] = pcCU->m_apiMVPNum[1][uiPartOffset+ui];;
806      m_puhDepth  [ui] = pcCU->getDepth(uiPartOffset+ui);
807      m_puhWidth  [ui] = pcCU->getWidth(uiPartOffset+ui);
808      m_puhHeight  [ui] = pcCU->getHeight(uiPartOffset+ui);
809      m_puhTrIdx  [ui] = pcCU->getTransformIdx(uiPartOffset+ui);
810      m_puhTransformSkip[0][ui] = pcCU->getTransformSkip(uiPartOffset+ui,TEXT_LUMA);
811      m_puhTransformSkip[1][ui] = pcCU->getTransformSkip(uiPartOffset+ui,TEXT_CHROMA_U);
812      m_puhTransformSkip[2][ui] = pcCU->getTransformSkip(uiPartOffset+ui,TEXT_CHROMA_V);
813      m_skipFlag[ui]   = pcCU->getSkipFlag(uiPartOffset+ui);
814      m_pePartSize[ui] = pcCU->getPartitionSize(uiPartOffset+ui);
815      m_pePredMode[ui] = pcCU->getPredictionMode(uiPartOffset+ui);
816      m_CUTransquantBypass[ui] = pcCU->getCUTransquantBypass(uiPartOffset+ui);
817      m_pbIPCMFlag[ui]=pcCU->m_pbIPCMFlag[uiPartOffset+ui];
818      m_phQP[ui] = pcCU->m_phQP[uiPartOffset+ui];
819      m_pbMergeFlag[ui]=pcCU->m_pbMergeFlag[uiPartOffset+ui];
820      m_puhMergeIndex[ui]=pcCU->m_puhMergeIndex[uiPartOffset+ui];
821      m_puhLumaIntraDir[ui]=pcCU->m_puhLumaIntraDir[uiPartOffset+ui];
822      m_puhChromaIntraDir[ui]=pcCU->m_puhChromaIntraDir[uiPartOffset+ui];
823      m_puhInterDir[ui]=pcCU->m_puhInterDir[uiPartOffset+ui];
824      m_puhCbf[0][ui]=pcCU->m_puhCbf[0][uiPartOffset+ui];
825      m_puhCbf[1][ui]=pcCU->m_puhCbf[1][uiPartOffset+ui];
826      m_puhCbf[2][ui]=pcCU->m_puhCbf[2][uiPartOffset+ui];
827
828#if H_3D_DIM
829      for( Int i = 0; i < DIM_NUM_TYPE; i++ )
830      {
831        m_dimDeltaDC[i][0] [ui] = pcCU->m_dimDeltaDC[i][0] [uiPartOffset+ui];
832        m_dimDeltaDC[i][1] [ui] = pcCU->m_dimDeltaDC[i][1] [uiPartOffset+ui];
833      }
834#if H_3D_DIM_DMM
835      for( Int i = 0; i < DMM_NUM_TYPE; i++ )
836      {
837        m_dmmWedgeTabIdx[i] [ui] = pcCU->m_dmmWedgeTabIdx[i] [uiPartOffset+ui];
838      }
839      m_dmm2DeltaEnd    [ui] = pcCU->m_dmm2DeltaEnd   [uiPartOffset+ui];
840      m_dmm3IntraTabIdx [ui] = pcCU->m_dmm3IntraTabIdx[uiPartOffset+ui];
841#endif
842#endif
843    }
844  }
845  UInt uiTmp = uhWidth*uhHeight;
846  memset( m_pcTrCoeffY , 0, sizeof(TCoeff)*uiTmp );
847#if ADAPTIVE_QP_SELECTION 
848  memset( m_pcArlCoeffY , 0, sizeof(Int)*uiTmp );
849#endif
850  memset( m_pcIPCMSampleY , 0, sizeof( Pel ) * uiTmp );
851  uiTmp >>= 2;
852  memset( m_pcTrCoeffCb, 0, sizeof(TCoeff)*uiTmp );
853  memset( m_pcTrCoeffCr, 0, sizeof(TCoeff)*uiTmp );
854#if ADAPTIVE_QP_SELECTION
855  memset( m_pcArlCoeffCb, 0, sizeof(Int)*uiTmp );
856  memset( m_pcArlCoeffCr, 0, sizeof(Int)*uiTmp );
857#endif
858  memset( m_pcIPCMSampleCb , 0, sizeof( Pel ) * uiTmp );
859  memset( m_pcIPCMSampleCr , 0, sizeof( Pel ) * uiTmp );
860  m_acCUMvField[0].clearMvField();
861  m_acCUMvField[1].clearMvField();
862
863  if(m_pcPic->getPicSym()->getInverseCUOrderMap(getAddr())*m_pcPic->getNumPartInCU()+m_uiAbsIdxInLCU<getSlice()->getSliceSegmentCurStartCUAddr())
864  {
865    // Part of this CU contains data from an older slice. Now copy in that data.
866    UInt uiMaxCuWidth=pcCU->getSlice()->getSPS()->getMaxCUWidth();
867    UInt uiMaxCuHeight=pcCU->getSlice()->getSPS()->getMaxCUHeight();
868    TComDataCU * bigCU = getPic()->getCU(getAddr());
869    Int minui = uiPartOffset;
870    minui = -minui;
871    pcCU->m_acCUMvField[0].copyTo(&m_acCUMvField[0],minui,uiPartOffset,m_uiNumPartition);
872    pcCU->m_acCUMvField[1].copyTo(&m_acCUMvField[1],minui,uiPartOffset,m_uiNumPartition);
873    UInt uiCoffOffset = uiMaxCuWidth*uiMaxCuHeight*m_uiAbsIdxInLCU/pcCU->getPic()->getNumPartInCU();
874    uiTmp = uhWidth*uhHeight;
875    for(Int i=0; i<uiTmp; i++)
876    {
877      m_pcTrCoeffY[i]=bigCU->m_pcTrCoeffY[uiCoffOffset+i];
878#if ADAPTIVE_QP_SELECTION
879      m_pcArlCoeffY[i]=bigCU->m_pcArlCoeffY[uiCoffOffset+i];
880#endif
881      m_pcIPCMSampleY[i]=bigCU->m_pcIPCMSampleY[uiCoffOffset+i];
882    }
883    uiTmp>>=2;
884    uiCoffOffset>>=2;
885    for(Int i=0; i<uiTmp; i++)
886    {
887      m_pcTrCoeffCr[i]=bigCU->m_pcTrCoeffCr[uiCoffOffset+i];
888      m_pcTrCoeffCb[i]=bigCU->m_pcTrCoeffCb[uiCoffOffset+i];
889#if ADAPTIVE_QP_SELECTION
890      m_pcArlCoeffCr[i]=bigCU->m_pcArlCoeffCr[uiCoffOffset+i];
891      m_pcArlCoeffCb[i]=bigCU->m_pcArlCoeffCb[uiCoffOffset+i];
892#endif
893      m_pcIPCMSampleCb[i]=bigCU->m_pcIPCMSampleCb[uiCoffOffset+i];
894      m_pcIPCMSampleCr[i]=bigCU->m_pcIPCMSampleCr[uiCoffOffset+i];
895    }
896  }
897
898  m_pcCULeft        = pcCU->getCULeft();
899  m_pcCUAbove       = pcCU->getCUAbove();
900  m_pcCUAboveLeft   = pcCU->getCUAboveLeft();
901  m_pcCUAboveRight  = pcCU->getCUAboveRight();
902
903  m_apcCUColocated[0] = pcCU->getCUColocated(REF_PIC_LIST_0);
904  m_apcCUColocated[1] = pcCU->getCUColocated(REF_PIC_LIST_1);
905  memcpy(m_sliceStartCU,pcCU->m_sliceStartCU+uiPartOffset,sizeof(UInt)*m_uiNumPartition);
906  memcpy(m_sliceSegmentStartCU,pcCU->m_sliceSegmentStartCU+uiPartOffset,sizeof(UInt)*m_uiNumPartition);
907}
908
909Void TComDataCU::setOutsideCUPart( UInt uiAbsPartIdx, UInt uiDepth )
910{
911  UInt uiNumPartition = m_uiNumPartition >> (uiDepth << 1);
912  UInt uiSizeInUchar = sizeof( UChar  ) * uiNumPartition;
913
914  UChar uhWidth  = g_uiMaxCUWidth  >> uiDepth;
915  UChar uhHeight = g_uiMaxCUHeight >> uiDepth;
916  memset( m_puhDepth    + uiAbsPartIdx,     uiDepth,  uiSizeInUchar );
917  memset( m_puhWidth    + uiAbsPartIdx,     uhWidth,  uiSizeInUchar );
918  memset( m_puhHeight   + uiAbsPartIdx,     uhHeight, uiSizeInUchar );
919}
920
921// --------------------------------------------------------------------------------------------------------------------
922// Copy
923// --------------------------------------------------------------------------------------------------------------------
924
925Void TComDataCU::copySubCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
926{
927  UInt uiPart = uiAbsPartIdx;
928 
929  m_pcPic              = pcCU->getPic();
930  m_pcSlice            = pcCU->getSlice();
931  m_uiCUAddr           = pcCU->getAddr();
932  m_uiAbsIdxInLCU      = uiAbsPartIdx;
933 
934  m_uiCUPelX           = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
935  m_uiCUPelY           = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
936 
937  UInt uiWidth         = g_uiMaxCUWidth  >> uiDepth;
938  UInt uiHeight        = g_uiMaxCUHeight >> uiDepth;
939 
940  m_skipFlag=pcCU->getSkipFlag()          + uiPart;
941
942  m_phQP=pcCU->getQP()                    + uiPart;
943  m_pePartSize = pcCU->getPartitionSize() + uiPart;
944  m_pePredMode=pcCU->getPredictionMode()  + uiPart;
945  m_CUTransquantBypass  = pcCU->getCUTransquantBypass()+uiPart;
946 
947  m_pbMergeFlag         = pcCU->getMergeFlag()        + uiPart;
948  m_puhMergeIndex       = pcCU->getMergeIndex()       + uiPart;
949
950  m_puhLumaIntraDir     = pcCU->getLumaIntraDir()     + uiPart;
951  m_puhChromaIntraDir   = pcCU->getChromaIntraDir()   + uiPart;
952  m_puhInterDir         = pcCU->getInterDir()         + uiPart;
953  m_puhTrIdx            = pcCU->getTransformIdx()     + uiPart;
954  m_puhTransformSkip[0] = pcCU->getTransformSkip(TEXT_LUMA)     + uiPart;
955  m_puhTransformSkip[1] = pcCU->getTransformSkip(TEXT_CHROMA_U) + uiPart;
956  m_puhTransformSkip[2] = pcCU->getTransformSkip(TEXT_CHROMA_V) + uiPart;
957
958  m_puhCbf[0]= pcCU->getCbf(TEXT_LUMA)            + uiPart;
959  m_puhCbf[1]= pcCU->getCbf(TEXT_CHROMA_U)        + uiPart;
960  m_puhCbf[2]= pcCU->getCbf(TEXT_CHROMA_V)        + uiPart;
961 
962#if H_3D_DIM
963  for( Int i = 0; i < DIM_NUM_TYPE; i++ )
964  {
965    m_dimDeltaDC[i][0] = pcCU->getDimDeltaDC( i, 0 ) + uiPart;
966    m_dimDeltaDC[i][1] = pcCU->getDimDeltaDC( i, 1 ) + uiPart;
967  }
968#if H_3D_DIM_DMM
969  for( Int i = 0; i < DMM_NUM_TYPE; i++ )
970  {
971    m_dmmWedgeTabIdx[i] = pcCU->getDmmWedgeTabIdx( i ) + uiPart;
972  }
973  m_dmm2DeltaEnd    = pcCU->getDmm2DeltaEnd()    + uiPart;
974  m_dmm3IntraTabIdx = pcCU->getDmm3IntraTabIdx() + uiPart;
975#endif
976#if H_3D_DIM_RBC
977  m_pucEdgeCode     = pcCU->getEdgeCode( uiPart );
978  m_pucEdgeNumber   = pcCU->getEdgeNumber()      + uiPart;
979  m_pucEdgeStartPos = pcCU->getEdgeStartPos()    + uiPart;
980  m_pbEdgeLeftFirst = pcCU->getEdgeLeftFirst()   + uiPart;
981  m_pbEdgePartition = pcCU->getEdgePartition( uiPart );
982#endif
983#endif
984
985  m_puhDepth=pcCU->getDepth()                     + uiPart;
986  m_puhWidth=pcCU->getWidth()                     + uiPart;
987  m_puhHeight=pcCU->getHeight()                   + uiPart;
988 
989  m_apiMVPIdx[0]=pcCU->getMVPIdx(REF_PIC_LIST_0)  + uiPart;
990  m_apiMVPIdx[1]=pcCU->getMVPIdx(REF_PIC_LIST_1)  + uiPart;
991  m_apiMVPNum[0]=pcCU->getMVPNum(REF_PIC_LIST_0)  + uiPart;
992  m_apiMVPNum[1]=pcCU->getMVPNum(REF_PIC_LIST_1)  + uiPart;
993 
994  m_pbIPCMFlag         = pcCU->getIPCMFlag()        + uiPart;
995
996  m_pcCUAboveLeft      = pcCU->getCUAboveLeft();
997  m_pcCUAboveRight     = pcCU->getCUAboveRight();
998  m_pcCUAbove          = pcCU->getCUAbove();
999  m_pcCULeft           = pcCU->getCULeft();
1000 
1001  m_apcCUColocated[0] = pcCU->getCUColocated(REF_PIC_LIST_0);
1002  m_apcCUColocated[1] = pcCU->getCUColocated(REF_PIC_LIST_1);
1003 
1004  UInt uiTmp = uiWidth*uiHeight;
1005  UInt uiMaxCuWidth=pcCU->getSlice()->getSPS()->getMaxCUWidth();
1006  UInt uiMaxCuHeight=pcCU->getSlice()->getSPS()->getMaxCUHeight();
1007 
1008  UInt uiCoffOffset = uiMaxCuWidth*uiMaxCuHeight*uiAbsPartIdx/pcCU->getPic()->getNumPartInCU();
1009 
1010  m_pcTrCoeffY = pcCU->getCoeffY() + uiCoffOffset;
1011#if ADAPTIVE_QP_SELECTION
1012  m_pcArlCoeffY= pcCU->getArlCoeffY() + uiCoffOffset; 
1013#endif
1014  m_pcIPCMSampleY = pcCU->getPCMSampleY() + uiCoffOffset;
1015
1016  uiTmp >>= 2;
1017  uiCoffOffset >>=2;
1018  m_pcTrCoeffCb=pcCU->getCoeffCb() + uiCoffOffset;
1019  m_pcTrCoeffCr=pcCU->getCoeffCr() + uiCoffOffset;
1020#if ADAPTIVE_QP_SELECTION 
1021  m_pcArlCoeffCb=pcCU->getArlCoeffCb() + uiCoffOffset;
1022  m_pcArlCoeffCr=pcCU->getArlCoeffCr() + uiCoffOffset;
1023#endif
1024  m_pcIPCMSampleCb = pcCU->getPCMSampleCb() + uiCoffOffset;
1025  m_pcIPCMSampleCr = pcCU->getPCMSampleCr() + uiCoffOffset;
1026
1027  m_acCUMvField[0].linkToWithOffset( pcCU->getCUMvField(REF_PIC_LIST_0), uiPart );
1028  m_acCUMvField[1].linkToWithOffset( pcCU->getCUMvField(REF_PIC_LIST_1), uiPart );
1029  memcpy(m_sliceStartCU,pcCU->m_sliceStartCU+uiPart,sizeof(UInt)*m_uiNumPartition);
1030  memcpy(m_sliceSegmentStartCU,pcCU->m_sliceSegmentStartCU+uiPart,sizeof(UInt)*m_uiNumPartition);
1031}
1032
1033// Copy inter prediction info from the biggest CU
1034Void TComDataCU::copyInterPredInfoFrom    ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefPicList )
1035{
1036  m_pcPic              = pcCU->getPic();
1037  m_pcSlice            = pcCU->getSlice();
1038  m_uiCUAddr           = pcCU->getAddr();
1039  m_uiAbsIdxInLCU      = uiAbsPartIdx;
1040 
1041  Int iRastPartIdx     = g_auiZscanToRaster[uiAbsPartIdx];
1042  m_uiCUPelX           = pcCU->getCUPelX() + m_pcPic->getMinCUWidth ()*( iRastPartIdx % m_pcPic->getNumPartInWidth() );
1043  m_uiCUPelY           = pcCU->getCUPelY() + m_pcPic->getMinCUHeight()*( iRastPartIdx / m_pcPic->getNumPartInWidth() );
1044 
1045  m_pcCUAboveLeft      = pcCU->getCUAboveLeft();
1046  m_pcCUAboveRight     = pcCU->getCUAboveRight();
1047  m_pcCUAbove          = pcCU->getCUAbove();
1048  m_pcCULeft           = pcCU->getCULeft();
1049 
1050  m_apcCUColocated[0]  = pcCU->getCUColocated(REF_PIC_LIST_0);
1051  m_apcCUColocated[1]  = pcCU->getCUColocated(REF_PIC_LIST_1);
1052 
1053  m_skipFlag           = pcCU->getSkipFlag ()             + uiAbsPartIdx;
1054
1055  m_pePartSize         = pcCU->getPartitionSize ()        + uiAbsPartIdx;
1056  m_pePredMode         = pcCU->getPredictionMode()        + uiAbsPartIdx;
1057  m_CUTransquantBypass = pcCU->getCUTransquantBypass()    + uiAbsPartIdx;
1058  m_puhInterDir        = pcCU->getInterDir      ()        + uiAbsPartIdx;
1059 
1060  m_puhDepth           = pcCU->getDepth ()                + uiAbsPartIdx;
1061  m_puhWidth           = pcCU->getWidth ()                + uiAbsPartIdx;
1062  m_puhHeight          = pcCU->getHeight()                + uiAbsPartIdx;
1063 
1064  m_pbMergeFlag        = pcCU->getMergeFlag()             + uiAbsPartIdx;
1065  m_puhMergeIndex      = pcCU->getMergeIndex()            + uiAbsPartIdx;
1066
1067  m_apiMVPIdx[eRefPicList] = pcCU->getMVPIdx(eRefPicList) + uiAbsPartIdx;
1068  m_apiMVPNum[eRefPicList] = pcCU->getMVPNum(eRefPicList) + uiAbsPartIdx;
1069 
1070  m_acCUMvField[ eRefPicList ].linkToWithOffset( pcCU->getCUMvField(eRefPicList), uiAbsPartIdx );
1071
1072  memcpy(m_sliceStartCU,pcCU->m_sliceStartCU+uiAbsPartIdx,sizeof(UInt)*m_uiNumPartition);
1073  memcpy(m_sliceSegmentStartCU,pcCU->m_sliceSegmentStartCU+uiAbsPartIdx,sizeof(UInt)*m_uiNumPartition);
1074}
1075
1076// Copy small CU to bigger CU.
1077// One of quarter parts overwritten by predicted sub part.
1078Void TComDataCU::copyPartFrom( TComDataCU* pcCU, UInt uiPartUnitIdx, UInt uiDepth )
1079{
1080  assert( uiPartUnitIdx<4 );
1081 
1082  m_dTotalCost         += pcCU->getTotalCost();
1083  m_uiTotalDistortion  += pcCU->getTotalDistortion();
1084  m_uiTotalBits        += pcCU->getTotalBits();
1085 
1086  UInt uiOffset         = pcCU->getTotalNumPart()*uiPartUnitIdx;
1087 
1088  UInt uiNumPartition = pcCU->getTotalNumPart();
1089  Int iSizeInUchar  = sizeof( UChar ) * uiNumPartition;
1090  Int iSizeInBool   = sizeof( Bool  ) * uiNumPartition;
1091 
1092  Int sizeInChar  = sizeof( Char ) * uiNumPartition;
1093  memcpy( m_skipFlag   + uiOffset, pcCU->getSkipFlag(),       sizeof( *m_skipFlag )   * uiNumPartition );
1094  memcpy( m_phQP       + uiOffset, pcCU->getQP(),             sizeInChar                        );
1095  memcpy( m_pePartSize + uiOffset, pcCU->getPartitionSize(),  sizeof( *m_pePartSize ) * uiNumPartition );
1096  memcpy( m_pePredMode + uiOffset, pcCU->getPredictionMode(), sizeof( *m_pePredMode ) * uiNumPartition );
1097  memcpy( m_CUTransquantBypass + uiOffset, pcCU->getCUTransquantBypass(), sizeof( *m_CUTransquantBypass ) * uiNumPartition );
1098  memcpy( m_pbMergeFlag         + uiOffset, pcCU->getMergeFlag(),         iSizeInBool  );
1099  memcpy( m_puhMergeIndex       + uiOffset, pcCU->getMergeIndex(),        iSizeInUchar );
1100  memcpy( m_puhLumaIntraDir     + uiOffset, pcCU->getLumaIntraDir(),      iSizeInUchar );
1101  memcpy( m_puhChromaIntraDir   + uiOffset, pcCU->getChromaIntraDir(),    iSizeInUchar );
1102  memcpy( m_puhInterDir         + uiOffset, pcCU->getInterDir(),          iSizeInUchar );
1103  memcpy( m_puhTrIdx            + uiOffset, pcCU->getTransformIdx(),      iSizeInUchar );
1104  memcpy( m_puhTransformSkip[0] + uiOffset, pcCU->getTransformSkip(TEXT_LUMA),     iSizeInUchar );
1105  memcpy( m_puhTransformSkip[1] + uiOffset, pcCU->getTransformSkip(TEXT_CHROMA_U), iSizeInUchar );
1106  memcpy( m_puhTransformSkip[2] + uiOffset, pcCU->getTransformSkip(TEXT_CHROMA_V), iSizeInUchar );
1107
1108  memcpy( m_puhCbf[0] + uiOffset, pcCU->getCbf(TEXT_LUMA)    , iSizeInUchar );
1109  memcpy( m_puhCbf[1] + uiOffset, pcCU->getCbf(TEXT_CHROMA_U), iSizeInUchar );
1110  memcpy( m_puhCbf[2] + uiOffset, pcCU->getCbf(TEXT_CHROMA_V), iSizeInUchar );
1111 
1112#if H_3D_DIM
1113  for( Int i = 0; i < DIM_NUM_TYPE; i++ )
1114  {
1115    memcpy( m_dimDeltaDC[i][0] + uiOffset, pcCU->getDimDeltaDC( i, 0 ), sizeof(Pel ) * uiNumPartition );
1116    memcpy( m_dimDeltaDC[i][1] + uiOffset, pcCU->getDimDeltaDC( i, 1 ), sizeof(Pel ) * uiNumPartition );
1117  }
1118#if H_3D_DIM_DMM
1119  for( Int i = 0; i < DMM_NUM_TYPE; i++ )
1120  {
1121    memcpy( m_dmmWedgeTabIdx[i] + uiOffset, pcCU->getDmmWedgeTabIdx( i ), sizeof(UInt) * uiNumPartition );
1122  }
1123  memcpy( m_dmm2DeltaEnd    + uiOffset, pcCU->getDmm2DeltaEnd()   , sizeof(Int ) * uiNumPartition );
1124  memcpy( m_dmm3IntraTabIdx + uiOffset, pcCU->getDmm3IntraTabIdx(), sizeof(UInt) * uiNumPartition );
1125#endif
1126#if H_3D_DIM_RBC
1127  memcpy( getEdgeCode( uiOffset ),       pcCU->getEdgeCode(0),      iSizeInUchar * RBC_MAX_EDGE_NUM_PER_4x4 );
1128  memcpy( getEdgeNumber()    + uiOffset, pcCU->getEdgeNumber(),     iSizeInUchar );
1129  memcpy( getEdgeStartPos()  + uiOffset, pcCU->getEdgeStartPos(),   iSizeInUchar );
1130  memcpy( getEdgeLeftFirst() + uiOffset, pcCU->getEdgeLeftFirst(),  iSizeInBool );
1131  memcpy( getEdgePartition( uiOffset ),  pcCU->getEdgePartition(0), iSizeInBool * 16 );
1132#endif
1133#endif
1134
1135  memcpy( m_puhDepth  + uiOffset, pcCU->getDepth(),  iSizeInUchar );
1136  memcpy( m_puhWidth  + uiOffset, pcCU->getWidth(),  iSizeInUchar );
1137  memcpy( m_puhHeight + uiOffset, pcCU->getHeight(), iSizeInUchar );
1138 
1139  memcpy( m_apiMVPIdx[0] + uiOffset, pcCU->getMVPIdx(REF_PIC_LIST_0), iSizeInUchar );
1140  memcpy( m_apiMVPIdx[1] + uiOffset, pcCU->getMVPIdx(REF_PIC_LIST_1), iSizeInUchar );
1141  memcpy( m_apiMVPNum[0] + uiOffset, pcCU->getMVPNum(REF_PIC_LIST_0), iSizeInUchar );
1142  memcpy( m_apiMVPNum[1] + uiOffset, pcCU->getMVPNum(REF_PIC_LIST_1), iSizeInUchar );
1143 
1144  memcpy( m_pbIPCMFlag + uiOffset, pcCU->getIPCMFlag(), iSizeInBool );
1145
1146  m_pcCUAboveLeft      = pcCU->getCUAboveLeft();
1147  m_pcCUAboveRight     = pcCU->getCUAboveRight();
1148  m_pcCUAbove          = pcCU->getCUAbove();
1149  m_pcCULeft           = pcCU->getCULeft();
1150 
1151  m_apcCUColocated[0] = pcCU->getCUColocated(REF_PIC_LIST_0);
1152  m_apcCUColocated[1] = pcCU->getCUColocated(REF_PIC_LIST_1);
1153 
1154  m_acCUMvField[0].copyFrom( pcCU->getCUMvField( REF_PIC_LIST_0 ), pcCU->getTotalNumPart(), uiOffset );
1155  m_acCUMvField[1].copyFrom( pcCU->getCUMvField( REF_PIC_LIST_1 ), pcCU->getTotalNumPart(), uiOffset );
1156 
1157  UInt uiTmp  = g_uiMaxCUWidth*g_uiMaxCUHeight >> (uiDepth<<1);
1158  UInt uiTmp2 = uiPartUnitIdx*uiTmp;
1159  memcpy( m_pcTrCoeffY  + uiTmp2, pcCU->getCoeffY(),  sizeof(TCoeff)*uiTmp );
1160#if ADAPTIVE_QP_SELECTION
1161  memcpy( m_pcArlCoeffY  + uiTmp2, pcCU->getArlCoeffY(),  sizeof(Int)*uiTmp );
1162#endif
1163  memcpy( m_pcIPCMSampleY + uiTmp2 , pcCU->getPCMSampleY(), sizeof(Pel) * uiTmp );
1164
1165  uiTmp >>= 2; uiTmp2>>= 2;
1166  memcpy( m_pcTrCoeffCb + uiTmp2, pcCU->getCoeffCb(), sizeof(TCoeff)*uiTmp );
1167  memcpy( m_pcTrCoeffCr + uiTmp2, pcCU->getCoeffCr(), sizeof(TCoeff)*uiTmp );
1168#if ADAPTIVE_QP_SELECTION
1169  memcpy( m_pcArlCoeffCb + uiTmp2, pcCU->getArlCoeffCb(), sizeof(Int)*uiTmp );
1170  memcpy( m_pcArlCoeffCr + uiTmp2, pcCU->getArlCoeffCr(), sizeof(Int)*uiTmp );
1171#endif
1172  memcpy( m_pcIPCMSampleCb + uiTmp2 , pcCU->getPCMSampleCb(), sizeof(Pel) * uiTmp );
1173  memcpy( m_pcIPCMSampleCr + uiTmp2 , pcCU->getPCMSampleCr(), sizeof(Pel) * uiTmp );
1174  m_uiTotalBins += pcCU->getTotalBins();
1175  memcpy( m_sliceStartCU        + uiOffset, pcCU->m_sliceStartCU,        sizeof( UInt ) * uiNumPartition  );
1176  memcpy( m_sliceSegmentStartCU + uiOffset, pcCU->m_sliceSegmentStartCU, sizeof( UInt ) * uiNumPartition  );
1177}
1178
1179// Copy current predicted part to a CU in picture.
1180// It is used to predict for next part
1181Void TComDataCU::copyToPic( UChar uhDepth )
1182{
1183  TComDataCU*& rpcCU = m_pcPic->getCU( m_uiCUAddr );
1184 
1185  rpcCU->getTotalCost()       = m_dTotalCost;
1186  rpcCU->getTotalDistortion() = m_uiTotalDistortion;
1187  rpcCU->getTotalBits()       = m_uiTotalBits;
1188 
1189  Int iSizeInUchar  = sizeof( UChar ) * m_uiNumPartition;
1190  Int iSizeInBool   = sizeof( Bool  ) * m_uiNumPartition;
1191 
1192  Int sizeInChar  = sizeof( Char ) * m_uiNumPartition;
1193
1194  memcpy( rpcCU->getSkipFlag() + m_uiAbsIdxInLCU, m_skipFlag, sizeof( *m_skipFlag ) * m_uiNumPartition );
1195
1196  memcpy( rpcCU->getQP() + m_uiAbsIdxInLCU, m_phQP, sizeInChar  );
1197
1198  memcpy( rpcCU->getPartitionSize()  + m_uiAbsIdxInLCU, m_pePartSize, sizeof( *m_pePartSize ) * m_uiNumPartition );
1199  memcpy( rpcCU->getPredictionMode() + m_uiAbsIdxInLCU, m_pePredMode, sizeof( *m_pePredMode ) * m_uiNumPartition );
1200  memcpy( rpcCU->getCUTransquantBypass()+ m_uiAbsIdxInLCU, m_CUTransquantBypass, sizeof( *m_CUTransquantBypass ) * m_uiNumPartition );
1201  memcpy( rpcCU->getMergeFlag()         + m_uiAbsIdxInLCU, m_pbMergeFlag,         iSizeInBool  );
1202  memcpy( rpcCU->getMergeIndex()        + m_uiAbsIdxInLCU, m_puhMergeIndex,       iSizeInUchar );
1203  memcpy( rpcCU->getLumaIntraDir()      + m_uiAbsIdxInLCU, m_puhLumaIntraDir,     iSizeInUchar );
1204  memcpy( rpcCU->getChromaIntraDir()    + m_uiAbsIdxInLCU, m_puhChromaIntraDir,   iSizeInUchar );
1205  memcpy( rpcCU->getInterDir()          + m_uiAbsIdxInLCU, m_puhInterDir,         iSizeInUchar );
1206  memcpy( rpcCU->getTransformIdx()      + m_uiAbsIdxInLCU, m_puhTrIdx,            iSizeInUchar );
1207  memcpy( rpcCU->getTransformSkip(TEXT_LUMA)     + m_uiAbsIdxInLCU, m_puhTransformSkip[0], iSizeInUchar );
1208  memcpy( rpcCU->getTransformSkip(TEXT_CHROMA_U) + m_uiAbsIdxInLCU, m_puhTransformSkip[1], iSizeInUchar );
1209  memcpy( rpcCU->getTransformSkip(TEXT_CHROMA_V) + m_uiAbsIdxInLCU, m_puhTransformSkip[2], iSizeInUchar );
1210
1211  memcpy( rpcCU->getCbf(TEXT_LUMA)     + m_uiAbsIdxInLCU, m_puhCbf[0], iSizeInUchar );
1212  memcpy( rpcCU->getCbf(TEXT_CHROMA_U) + m_uiAbsIdxInLCU, m_puhCbf[1], iSizeInUchar );
1213  memcpy( rpcCU->getCbf(TEXT_CHROMA_V) + m_uiAbsIdxInLCU, m_puhCbf[2], iSizeInUchar );
1214 
1215#if H_3D_DIM
1216  for( Int i = 0; i < DIM_NUM_TYPE; i++ )
1217  {
1218    memcpy( rpcCU->getDimDeltaDC( i, 0 ) + m_uiAbsIdxInLCU, m_dimDeltaDC[i][0], sizeof(Pel ) * m_uiNumPartition );
1219    memcpy( rpcCU->getDimDeltaDC( i, 1 ) + m_uiAbsIdxInLCU, m_dimDeltaDC[i][1], sizeof(Pel ) * m_uiNumPartition );
1220  }
1221#if H_3D_DIM_DMM
1222  for( Int i = 0; i < DMM_NUM_TYPE; i++ )
1223  {
1224    memcpy( rpcCU->getDmmWedgeTabIdx( i ) + m_uiAbsIdxInLCU, m_dmmWedgeTabIdx[i], sizeof(UInt) * m_uiNumPartition );
1225  }
1226  memcpy( rpcCU->getDmm2DeltaEnd()    + m_uiAbsIdxInLCU, m_dmm2DeltaEnd   , sizeof(Int ) * m_uiNumPartition );
1227  memcpy( rpcCU->getDmm3IntraTabIdx() + m_uiAbsIdxInLCU, m_dmm3IntraTabIdx, sizeof(UInt) * m_uiNumPartition );
1228#endif
1229#if H_3D_DIM_RBC
1230  memcpy( rpcCU->getEdgeCode( m_uiAbsIdxInLCU ),         m_pucEdgeCode,     iSizeInUchar * RBC_MAX_EDGE_NUM_PER_4x4 );
1231  memcpy( rpcCU->getEdgeNumber()      + m_uiAbsIdxInLCU, m_pucEdgeNumber,   iSizeInUchar );
1232  memcpy( rpcCU->getEdgeStartPos()    + m_uiAbsIdxInLCU, m_pucEdgeStartPos, iSizeInUchar );
1233  memcpy( rpcCU->getEdgeLeftFirst()   + m_uiAbsIdxInLCU, m_pbEdgeLeftFirst, iSizeInBool );
1234  memcpy( rpcCU->getEdgePartition( m_uiAbsIdxInLCU ),    m_pbEdgePartition, iSizeInBool * 16 );
1235#endif
1236#endif
1237
1238  memcpy( rpcCU->getDepth()  + m_uiAbsIdxInLCU, m_puhDepth,  iSizeInUchar );
1239  memcpy( rpcCU->getWidth()  + m_uiAbsIdxInLCU, m_puhWidth,  iSizeInUchar );
1240  memcpy( rpcCU->getHeight() + m_uiAbsIdxInLCU, m_puhHeight, iSizeInUchar );
1241 
1242  memcpy( rpcCU->getMVPIdx(REF_PIC_LIST_0) + m_uiAbsIdxInLCU, m_apiMVPIdx[0], iSizeInUchar );
1243  memcpy( rpcCU->getMVPIdx(REF_PIC_LIST_1) + m_uiAbsIdxInLCU, m_apiMVPIdx[1], iSizeInUchar );
1244  memcpy( rpcCU->getMVPNum(REF_PIC_LIST_0) + m_uiAbsIdxInLCU, m_apiMVPNum[0], iSizeInUchar );
1245  memcpy( rpcCU->getMVPNum(REF_PIC_LIST_1) + m_uiAbsIdxInLCU, m_apiMVPNum[1], iSizeInUchar );
1246 
1247  m_acCUMvField[0].copyTo( rpcCU->getCUMvField( REF_PIC_LIST_0 ), m_uiAbsIdxInLCU );
1248  m_acCUMvField[1].copyTo( rpcCU->getCUMvField( REF_PIC_LIST_1 ), m_uiAbsIdxInLCU );
1249 
1250  memcpy( rpcCU->getIPCMFlag() + m_uiAbsIdxInLCU, m_pbIPCMFlag,         iSizeInBool  );
1251
1252  UInt uiTmp  = (g_uiMaxCUWidth*g_uiMaxCUHeight)>>(uhDepth<<1);
1253  UInt uiTmp2 = m_uiAbsIdxInLCU*m_pcPic->getMinCUWidth()*m_pcPic->getMinCUHeight();
1254  memcpy( rpcCU->getCoeffY()  + uiTmp2, m_pcTrCoeffY,  sizeof(TCoeff)*uiTmp  );
1255#if ADAPTIVE_QP_SELECTION 
1256  memcpy( rpcCU->getArlCoeffY()  + uiTmp2, m_pcArlCoeffY,  sizeof(Int)*uiTmp  );
1257#endif
1258  memcpy( rpcCU->getPCMSampleY() + uiTmp2 , m_pcIPCMSampleY, sizeof(Pel)*uiTmp );
1259
1260  uiTmp >>= 2; uiTmp2 >>= 2;
1261  memcpy( rpcCU->getCoeffCb() + uiTmp2, m_pcTrCoeffCb, sizeof(TCoeff)*uiTmp  );
1262  memcpy( rpcCU->getCoeffCr() + uiTmp2, m_pcTrCoeffCr, sizeof(TCoeff)*uiTmp  );
1263#if ADAPTIVE_QP_SELECTION
1264  memcpy( rpcCU->getArlCoeffCb() + uiTmp2, m_pcArlCoeffCb, sizeof(Int)*uiTmp  );
1265  memcpy( rpcCU->getArlCoeffCr() + uiTmp2, m_pcArlCoeffCr, sizeof(Int)*uiTmp  );
1266#endif
1267  memcpy( rpcCU->getPCMSampleCb() + uiTmp2 , m_pcIPCMSampleCb, sizeof( Pel ) * uiTmp );
1268  memcpy( rpcCU->getPCMSampleCr() + uiTmp2 , m_pcIPCMSampleCr, sizeof( Pel ) * uiTmp );
1269  rpcCU->getTotalBins() = m_uiTotalBins;
1270  memcpy( rpcCU->m_sliceStartCU        + m_uiAbsIdxInLCU, m_sliceStartCU,        sizeof( UInt ) * m_uiNumPartition  );
1271  memcpy( rpcCU->m_sliceSegmentStartCU + m_uiAbsIdxInLCU, m_sliceSegmentStartCU, sizeof( UInt ) * m_uiNumPartition  );
1272}
1273
1274Void TComDataCU::copyToPic( UChar uhDepth, UInt uiPartIdx, UInt uiPartDepth )
1275{
1276  TComDataCU*&  rpcCU       = m_pcPic->getCU( m_uiCUAddr );
1277  UInt          uiQNumPart  = m_uiNumPartition>>(uiPartDepth<<1);
1278 
1279  UInt uiPartStart          = uiPartIdx*uiQNumPart;
1280  UInt uiPartOffset         = m_uiAbsIdxInLCU + uiPartStart;
1281 
1282  rpcCU->getTotalCost()       = m_dTotalCost;
1283  rpcCU->getTotalDistortion() = m_uiTotalDistortion;
1284  rpcCU->getTotalBits()       = m_uiTotalBits;
1285 
1286  Int iSizeInUchar  = sizeof( UChar  ) * uiQNumPart;
1287  Int iSizeInBool   = sizeof( Bool   ) * uiQNumPart;
1288 
1289  Int sizeInChar  = sizeof( Char ) * uiQNumPart;
1290  memcpy( rpcCU->getSkipFlag()       + uiPartOffset, m_skipFlag,   sizeof( *m_skipFlag )   * uiQNumPart );
1291
1292  memcpy( rpcCU->getQP() + uiPartOffset, m_phQP, sizeInChar );
1293  memcpy( rpcCU->getPartitionSize()  + uiPartOffset, m_pePartSize, sizeof( *m_pePartSize ) * uiQNumPart );
1294  memcpy( rpcCU->getPredictionMode() + uiPartOffset, m_pePredMode, sizeof( *m_pePredMode ) * uiQNumPart );
1295  memcpy( rpcCU->getCUTransquantBypass()+ uiPartOffset, m_CUTransquantBypass, sizeof( *m_CUTransquantBypass ) * uiQNumPart );
1296  memcpy( rpcCU->getMergeFlag()         + uiPartOffset, m_pbMergeFlag,         iSizeInBool  );
1297  memcpy( rpcCU->getMergeIndex()        + uiPartOffset, m_puhMergeIndex,       iSizeInUchar );
1298  memcpy( rpcCU->getLumaIntraDir()      + uiPartOffset, m_puhLumaIntraDir,     iSizeInUchar );
1299  memcpy( rpcCU->getChromaIntraDir()    + uiPartOffset, m_puhChromaIntraDir,   iSizeInUchar );
1300  memcpy( rpcCU->getInterDir()          + uiPartOffset, m_puhInterDir,         iSizeInUchar );
1301  memcpy( rpcCU->getTransformIdx()      + uiPartOffset, m_puhTrIdx,            iSizeInUchar );
1302  memcpy( rpcCU->getTransformSkip(TEXT_LUMA)     + uiPartOffset, m_puhTransformSkip[0], iSizeInUchar );
1303  memcpy( rpcCU->getTransformSkip(TEXT_CHROMA_U) + uiPartOffset, m_puhTransformSkip[1], iSizeInUchar );
1304  memcpy( rpcCU->getTransformSkip(TEXT_CHROMA_V) + uiPartOffset, m_puhTransformSkip[2], iSizeInUchar );
1305  memcpy( rpcCU->getCbf(TEXT_LUMA)     + uiPartOffset, m_puhCbf[0], iSizeInUchar );
1306  memcpy( rpcCU->getCbf(TEXT_CHROMA_U) + uiPartOffset, m_puhCbf[1], iSizeInUchar );
1307  memcpy( rpcCU->getCbf(TEXT_CHROMA_V) + uiPartOffset, m_puhCbf[2], iSizeInUchar );
1308 
1309#if H_3D_DIM
1310  for( Int i = 0; i < DMM_NUM_TYPE; i++ )
1311  {
1312    memcpy( rpcCU->getDimDeltaDC( i, 0 ) + uiPartOffset, m_dimDeltaDC[i][0], sizeof(Pel ) * uiQNumPart );
1313    memcpy( rpcCU->getDimDeltaDC( i, 1 ) + uiPartOffset, m_dimDeltaDC[i][1], sizeof(Pel ) * uiQNumPart );
1314  }
1315#if H_3D_DIM_DMM
1316  for( Int i = 0; i < DMM_NUM_TYPE; i++ )
1317  {
1318    memcpy( rpcCU->getDmmWedgeTabIdx( i ) + uiPartOffset, m_dmmWedgeTabIdx[i], sizeof(UInt) * uiQNumPart );
1319  }
1320  memcpy( rpcCU->getDmm2DeltaEnd()    + uiPartOffset, m_dmm2DeltaEnd   , sizeof(Int ) * uiQNumPart );
1321  memcpy( rpcCU->getDmm3IntraTabIdx() + uiPartOffset, m_dmm3IntraTabIdx, sizeof(UInt) * uiQNumPart );
1322#endif
1323#if H_3D_DIM_RBC
1324  memcpy( rpcCU->getEdgeCode( uiPartOffset ),         m_pucEdgeCode,     iSizeInUchar * RBC_MAX_EDGE_NUM_PER_4x4 );
1325  memcpy( rpcCU->getEdgeNumber()      + uiPartOffset, m_pucEdgeNumber,   iSizeInUchar );
1326  memcpy( rpcCU->getEdgeStartPos()    + uiPartOffset, m_pucEdgeStartPos, iSizeInUchar );
1327  memcpy( rpcCU->getEdgeLeftFirst()   + uiPartOffset, m_pbEdgeLeftFirst, iSizeInBool );
1328  memcpy( rpcCU->getEdgePartition( uiPartOffset ),    m_pbEdgePartition, iSizeInBool * 16 );
1329#endif
1330#endif
1331
1332  memcpy( rpcCU->getDepth()  + uiPartOffset, m_puhDepth,  iSizeInUchar );
1333  memcpy( rpcCU->getWidth()  + uiPartOffset, m_puhWidth,  iSizeInUchar );
1334  memcpy( rpcCU->getHeight() + uiPartOffset, m_puhHeight, iSizeInUchar );
1335 
1336  memcpy( rpcCU->getMVPIdx(REF_PIC_LIST_0) + uiPartOffset, m_apiMVPIdx[0], iSizeInUchar );
1337  memcpy( rpcCU->getMVPIdx(REF_PIC_LIST_1) + uiPartOffset, m_apiMVPIdx[1], iSizeInUchar );
1338  memcpy( rpcCU->getMVPNum(REF_PIC_LIST_0) + uiPartOffset, m_apiMVPNum[0], iSizeInUchar );
1339  memcpy( rpcCU->getMVPNum(REF_PIC_LIST_1) + uiPartOffset, m_apiMVPNum[1], iSizeInUchar );
1340  m_acCUMvField[0].copyTo( rpcCU->getCUMvField( REF_PIC_LIST_0 ), m_uiAbsIdxInLCU, uiPartStart, uiQNumPart );
1341  m_acCUMvField[1].copyTo( rpcCU->getCUMvField( REF_PIC_LIST_1 ), m_uiAbsIdxInLCU, uiPartStart, uiQNumPart );
1342 
1343  memcpy( rpcCU->getIPCMFlag() + uiPartOffset, m_pbIPCMFlag,         iSizeInBool  );
1344
1345  UInt uiTmp  = (g_uiMaxCUWidth*g_uiMaxCUHeight)>>((uhDepth+uiPartDepth)<<1);
1346  UInt uiTmp2 = uiPartOffset*m_pcPic->getMinCUWidth()*m_pcPic->getMinCUHeight();
1347  memcpy( rpcCU->getCoeffY()  + uiTmp2, m_pcTrCoeffY,  sizeof(TCoeff)*uiTmp  );
1348#if ADAPTIVE_QP_SELECTION
1349  memcpy( rpcCU->getArlCoeffY()  + uiTmp2, m_pcArlCoeffY,  sizeof(Int)*uiTmp  );
1350#endif
1351 
1352  memcpy( rpcCU->getPCMSampleY() + uiTmp2 , m_pcIPCMSampleY, sizeof( Pel ) * uiTmp );
1353
1354  uiTmp >>= 2; uiTmp2 >>= 2;
1355  memcpy( rpcCU->getCoeffCb() + uiTmp2, m_pcTrCoeffCb, sizeof(TCoeff)*uiTmp  );
1356  memcpy( rpcCU->getCoeffCr() + uiTmp2, m_pcTrCoeffCr, sizeof(TCoeff)*uiTmp  );
1357#if ADAPTIVE_QP_SELECTION
1358  memcpy( rpcCU->getArlCoeffCb() + uiTmp2, m_pcArlCoeffCb, sizeof(Int)*uiTmp  );
1359  memcpy( rpcCU->getArlCoeffCr() + uiTmp2, m_pcArlCoeffCr, sizeof(Int)*uiTmp  );
1360#endif
1361
1362  memcpy( rpcCU->getPCMSampleCb() + uiTmp2 , m_pcIPCMSampleCb, sizeof( Pel ) * uiTmp );
1363  memcpy( rpcCU->getPCMSampleCr() + uiTmp2 , m_pcIPCMSampleCr, sizeof( Pel ) * uiTmp );
1364  rpcCU->getTotalBins() = m_uiTotalBins;
1365  memcpy( rpcCU->m_sliceStartCU        + uiPartOffset, m_sliceStartCU,        sizeof( UInt ) * uiQNumPart  );
1366  memcpy( rpcCU->m_sliceSegmentStartCU + uiPartOffset, m_sliceSegmentStartCU, sizeof( UInt ) * uiQNumPart  );
1367}
1368
1369// --------------------------------------------------------------------------------------------------------------------
1370// Other public functions
1371// --------------------------------------------------------------------------------------------------------------------
1372
1373TComDataCU* TComDataCU::getPULeft( UInt& uiLPartUnitIdx, 
1374                                   UInt uiCurrPartUnitIdx, 
1375                                   Bool bEnforceSliceRestriction, 
1376                                   Bool bEnforceTileRestriction )
1377{
1378  UInt uiAbsPartIdx       = g_auiZscanToRaster[uiCurrPartUnitIdx];
1379  UInt uiAbsZorderCUIdx   = g_auiZscanToRaster[m_uiAbsIdxInLCU];
1380  UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
1381 
1382  if ( !RasterAddress::isZeroCol( uiAbsPartIdx, uiNumPartInCUWidth ) )
1383  {
1384    uiLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx - 1 ];
1385    if ( RasterAddress::isEqualCol( uiAbsPartIdx, uiAbsZorderCUIdx, uiNumPartInCUWidth ) )
1386    {
1387      return m_pcPic->getCU( getAddr() );
1388    }
1389    else
1390    {
1391      uiLPartUnitIdx -= m_uiAbsIdxInLCU;
1392      return this;
1393    }
1394  }
1395 
1396  uiLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx + uiNumPartInCUWidth - 1 ];
1397
1398
1399  if ( (bEnforceSliceRestriction && (m_pcCULeft==NULL || m_pcCULeft->getSlice()==NULL || m_pcCULeft->getSCUAddr()+uiLPartUnitIdx < m_pcPic->getCU( getAddr() )->getSliceStartCU(uiCurrPartUnitIdx)))
1400      ||
1401       (bEnforceTileRestriction && ( m_pcCULeft==NULL || m_pcCULeft->getSlice()==NULL || (m_pcPic->getPicSym()->getTileIdxMap( m_pcCULeft->getAddr() ) != m_pcPic->getPicSym()->getTileIdxMap(getAddr()))  )  )
1402      )
1403  {
1404    return NULL;
1405  }
1406  return m_pcCULeft;
1407}
1408
1409TComDataCU* TComDataCU::getPUAbove( UInt& uiAPartUnitIdx,
1410                                    UInt uiCurrPartUnitIdx, 
1411                                    Bool bEnforceSliceRestriction, 
1412                                    Bool planarAtLCUBoundary ,
1413                                    Bool bEnforceTileRestriction )
1414{
1415  UInt uiAbsPartIdx       = g_auiZscanToRaster[uiCurrPartUnitIdx];
1416  UInt uiAbsZorderCUIdx   = g_auiZscanToRaster[m_uiAbsIdxInLCU];
1417  UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
1418 
1419  if ( !RasterAddress::isZeroRow( uiAbsPartIdx, uiNumPartInCUWidth ) )
1420  {
1421    uiAPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx - uiNumPartInCUWidth ];
1422    if ( RasterAddress::isEqualRow( uiAbsPartIdx, uiAbsZorderCUIdx, uiNumPartInCUWidth ) )
1423    {
1424      return m_pcPic->getCU( getAddr() );
1425    }
1426    else
1427    {
1428      uiAPartUnitIdx -= m_uiAbsIdxInLCU;
1429      return this;
1430    }
1431  }
1432
1433  if(planarAtLCUBoundary)
1434  {
1435    return NULL;
1436  }
1437 
1438  uiAPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx + m_pcPic->getNumPartInCU() - uiNumPartInCUWidth ];
1439
1440  if ( (bEnforceSliceRestriction && (m_pcCUAbove==NULL || m_pcCUAbove->getSlice()==NULL || m_pcCUAbove->getSCUAddr()+uiAPartUnitIdx < m_pcPic->getCU( getAddr() )->getSliceStartCU(uiCurrPartUnitIdx)))
1441      ||
1442       (bEnforceTileRestriction &&(m_pcCUAbove==NULL || m_pcCUAbove->getSlice()==NULL || (m_pcPic->getPicSym()->getTileIdxMap( m_pcCUAbove->getAddr() ) != m_pcPic->getPicSym()->getTileIdxMap(getAddr()))))
1443      )
1444  {
1445    return NULL;
1446  }
1447  return m_pcCUAbove;
1448}
1449
1450TComDataCU* TComDataCU::getPUAboveLeft( UInt& uiALPartUnitIdx, UInt uiCurrPartUnitIdx, Bool bEnforceSliceRestriction )
1451{
1452  UInt uiAbsPartIdx       = g_auiZscanToRaster[uiCurrPartUnitIdx];
1453  UInt uiAbsZorderCUIdx   = g_auiZscanToRaster[m_uiAbsIdxInLCU];
1454  UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
1455 
1456  if ( !RasterAddress::isZeroCol( uiAbsPartIdx, uiNumPartInCUWidth ) )
1457  {
1458    if ( !RasterAddress::isZeroRow( uiAbsPartIdx, uiNumPartInCUWidth ) )
1459    {
1460      uiALPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx - uiNumPartInCUWidth - 1 ];
1461      if ( RasterAddress::isEqualRowOrCol( uiAbsPartIdx, uiAbsZorderCUIdx, uiNumPartInCUWidth ) )
1462      {
1463        return m_pcPic->getCU( getAddr() );
1464      }
1465      else
1466      {
1467        uiALPartUnitIdx -= m_uiAbsIdxInLCU;
1468        return this;
1469      }
1470    }
1471    uiALPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx + getPic()->getNumPartInCU() - uiNumPartInCUWidth - 1 ];
1472    if ( (bEnforceSliceRestriction && (m_pcCUAbove==NULL || m_pcCUAbove->getSlice()==NULL ||
1473       m_pcCUAbove->getSCUAddr()+uiALPartUnitIdx < m_pcPic->getCU( getAddr() )->getSliceStartCU(uiCurrPartUnitIdx)||
1474       (m_pcPic->getPicSym()->getTileIdxMap( m_pcCUAbove->getAddr() ) != m_pcPic->getPicSym()->getTileIdxMap(getAddr()))
1475       ))
1476     )
1477    {
1478      return NULL;
1479    }
1480    return m_pcCUAbove;
1481  }
1482 
1483  if ( !RasterAddress::isZeroRow( uiAbsPartIdx, uiNumPartInCUWidth ) )
1484  {
1485    uiALPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx - 1 ];
1486    if ( (bEnforceSliceRestriction && (m_pcCULeft==NULL || m_pcCULeft->getSlice()==NULL || 
1487       m_pcCULeft->getSCUAddr()+uiALPartUnitIdx < m_pcPic->getCU( getAddr() )->getSliceStartCU(uiCurrPartUnitIdx)||
1488       (m_pcPic->getPicSym()->getTileIdxMap( m_pcCULeft->getAddr() ) != m_pcPic->getPicSym()->getTileIdxMap(getAddr()))
1489       ))
1490     )
1491    {
1492      return NULL;
1493    }
1494    return m_pcCULeft;
1495  }
1496 
1497  uiALPartUnitIdx = g_auiRasterToZscan[ m_pcPic->getNumPartInCU() - 1 ];
1498  if ( (bEnforceSliceRestriction && (m_pcCUAboveLeft==NULL || m_pcCUAboveLeft->getSlice()==NULL ||
1499       m_pcCUAboveLeft->getSCUAddr()+uiALPartUnitIdx < m_pcPic->getCU( getAddr() )->getSliceStartCU(uiCurrPartUnitIdx)||
1500       (m_pcPic->getPicSym()->getTileIdxMap( m_pcCUAboveLeft->getAddr() ) != m_pcPic->getPicSym()->getTileIdxMap(getAddr()))
1501       ))
1502     )
1503  {
1504    return NULL;
1505  }
1506  return m_pcCUAboveLeft;
1507}
1508
1509TComDataCU* TComDataCU::getPUAboveRight( UInt& uiARPartUnitIdx, UInt uiCurrPartUnitIdx, Bool bEnforceSliceRestriction )
1510{
1511  UInt uiAbsPartIdxRT     = g_auiZscanToRaster[uiCurrPartUnitIdx];
1512  UInt uiAbsZorderCUIdx   = g_auiZscanToRaster[ m_uiAbsIdxInLCU ] + m_puhWidth[0] / m_pcPic->getMinCUWidth() - 1;
1513  UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
1514 
1515  if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxRT] + m_pcPic->getMinCUWidth() ) >= m_pcSlice->getSPS()->getPicWidthInLumaSamples() )
1516  {
1517    uiARPartUnitIdx = MAX_UINT;
1518    return NULL;
1519  }
1520 
1521  if ( RasterAddress::lessThanCol( uiAbsPartIdxRT, uiNumPartInCUWidth - 1, uiNumPartInCUWidth ) )
1522  {
1523    if ( !RasterAddress::isZeroRow( uiAbsPartIdxRT, uiNumPartInCUWidth ) )
1524    {
1525      if ( uiCurrPartUnitIdx > g_auiRasterToZscan[ uiAbsPartIdxRT - uiNumPartInCUWidth + 1 ] )
1526      {
1527        uiARPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxRT - uiNumPartInCUWidth + 1 ];
1528        if ( RasterAddress::isEqualRowOrCol( uiAbsPartIdxRT, uiAbsZorderCUIdx, uiNumPartInCUWidth ) )
1529        {
1530          return m_pcPic->getCU( getAddr() );
1531        }
1532        else
1533        {
1534          uiARPartUnitIdx -= m_uiAbsIdxInLCU;
1535          return this;
1536        }
1537      }
1538      uiARPartUnitIdx = MAX_UINT;
1539      return NULL;
1540    }
1541    uiARPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxRT + m_pcPic->getNumPartInCU() - uiNumPartInCUWidth + 1 ];
1542    if ( (bEnforceSliceRestriction && (m_pcCUAbove==NULL || m_pcCUAbove->getSlice()==NULL ||
1543       m_pcCUAbove->getSCUAddr()+uiARPartUnitIdx < m_pcPic->getCU( getAddr() )->getSliceStartCU(uiCurrPartUnitIdx)||
1544       (m_pcPic->getPicSym()->getTileIdxMap( m_pcCUAbove->getAddr() ) != m_pcPic->getPicSym()->getTileIdxMap(getAddr()))
1545       ))
1546     )
1547    {
1548      return NULL;
1549    }
1550    return m_pcCUAbove;
1551  }
1552 
1553  if ( !RasterAddress::isZeroRow( uiAbsPartIdxRT, uiNumPartInCUWidth ) )
1554  {
1555    uiARPartUnitIdx = MAX_UINT;
1556    return NULL;
1557  }
1558 
1559  uiARPartUnitIdx = g_auiRasterToZscan[ m_pcPic->getNumPartInCU() - uiNumPartInCUWidth ];
1560  if ( (bEnforceSliceRestriction && (m_pcCUAboveRight==NULL || m_pcCUAboveRight->getSlice()==NULL ||
1561       m_pcPic->getPicSym()->getInverseCUOrderMap( m_pcCUAboveRight->getAddr()) > m_pcPic->getPicSym()->getInverseCUOrderMap( getAddr()) ||
1562       m_pcCUAboveRight->getSCUAddr()+uiARPartUnitIdx < m_pcPic->getCU( getAddr() )->getSliceStartCU(uiCurrPartUnitIdx)||
1563       (m_pcPic->getPicSym()->getTileIdxMap( m_pcCUAboveRight->getAddr() ) != m_pcPic->getPicSym()->getTileIdxMap(getAddr()))
1564       ))
1565     )
1566  {
1567    return NULL;
1568  }
1569  return m_pcCUAboveRight;
1570}
1571
1572TComDataCU* TComDataCU::getPUBelowLeft( UInt& uiBLPartUnitIdx, UInt uiCurrPartUnitIdx, Bool bEnforceSliceRestriction )
1573{
1574  UInt uiAbsPartIdxLB     = g_auiZscanToRaster[uiCurrPartUnitIdx];
1575  UInt uiAbsZorderCUIdxLB = g_auiZscanToRaster[ m_uiAbsIdxInLCU ] + (m_puhHeight[0] / m_pcPic->getMinCUHeight() - 1)*m_pcPic->getNumPartInWidth();
1576  UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
1577 
1578  if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxLB] + m_pcPic->getMinCUHeight() ) >= m_pcSlice->getSPS()->getPicHeightInLumaSamples() )
1579  {
1580    uiBLPartUnitIdx = MAX_UINT;
1581    return NULL;
1582  }
1583 
1584  if ( RasterAddress::lessThanRow( uiAbsPartIdxLB, m_pcPic->getNumPartInHeight() - 1, uiNumPartInCUWidth ) )
1585  {
1586    if ( !RasterAddress::isZeroCol( uiAbsPartIdxLB, uiNumPartInCUWidth ) )
1587    {
1588      if ( uiCurrPartUnitIdx > g_auiRasterToZscan[ uiAbsPartIdxLB + uiNumPartInCUWidth - 1 ] )
1589      {
1590        uiBLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxLB + uiNumPartInCUWidth - 1 ];
1591        if ( RasterAddress::isEqualRowOrCol( uiAbsPartIdxLB, uiAbsZorderCUIdxLB, uiNumPartInCUWidth ) )
1592        {
1593          return m_pcPic->getCU( getAddr() );
1594        }
1595        else
1596        {
1597          uiBLPartUnitIdx -= m_uiAbsIdxInLCU;
1598          return this;
1599        }
1600      }
1601      uiBLPartUnitIdx = MAX_UINT;
1602      return NULL;
1603    }
1604    uiBLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxLB + uiNumPartInCUWidth*2 - 1 ];
1605    if ( (bEnforceSliceRestriction && (m_pcCULeft==NULL || m_pcCULeft->getSlice()==NULL || 
1606       m_pcCULeft->getSCUAddr()+uiBLPartUnitIdx < m_pcPic->getCU( getAddr() )->getSliceStartCU(uiCurrPartUnitIdx)||
1607       (m_pcPic->getPicSym()->getTileIdxMap( m_pcCULeft->getAddr() ) != m_pcPic->getPicSym()->getTileIdxMap(getAddr()))
1608       ))
1609     )
1610    {
1611      return NULL;
1612    }
1613    return m_pcCULeft;
1614  }
1615 
1616  uiBLPartUnitIdx = MAX_UINT;
1617  return NULL;
1618}
1619
1620TComDataCU* TComDataCU::getPUBelowLeftAdi(UInt& uiBLPartUnitIdx,  UInt uiCurrPartUnitIdx, UInt uiPartUnitOffset, Bool bEnforceSliceRestriction )
1621{
1622  UInt uiAbsPartIdxLB     = g_auiZscanToRaster[uiCurrPartUnitIdx];
1623  UInt uiAbsZorderCUIdxLB = g_auiZscanToRaster[ m_uiAbsIdxInLCU ] + ((m_puhHeight[0] / m_pcPic->getMinCUHeight()) - 1)*m_pcPic->getNumPartInWidth();
1624  UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
1625 
1626  if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxLB] + (m_pcPic->getPicSym()->getMinCUHeight() * uiPartUnitOffset)) >= m_pcSlice->getSPS()->getPicHeightInLumaSamples())
1627  {
1628    uiBLPartUnitIdx = MAX_UINT;
1629    return NULL;
1630  }
1631 
1632  if ( RasterAddress::lessThanRow( uiAbsPartIdxLB, m_pcPic->getNumPartInHeight() - uiPartUnitOffset, uiNumPartInCUWidth ) )
1633  {
1634    if ( !RasterAddress::isZeroCol( uiAbsPartIdxLB, uiNumPartInCUWidth ) )
1635    {
1636      if ( uiCurrPartUnitIdx > g_auiRasterToZscan[ uiAbsPartIdxLB + uiPartUnitOffset * uiNumPartInCUWidth - 1 ] )
1637      {
1638        uiBLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxLB + uiPartUnitOffset * uiNumPartInCUWidth - 1 ];
1639        if ( RasterAddress::isEqualRowOrCol( uiAbsPartIdxLB, uiAbsZorderCUIdxLB, uiNumPartInCUWidth ) )
1640        {
1641          return m_pcPic->getCU( getAddr() );
1642        }
1643        else
1644        {
1645          uiBLPartUnitIdx -= m_uiAbsIdxInLCU;
1646          return this;
1647        }
1648      }
1649      uiBLPartUnitIdx = MAX_UINT;
1650      return NULL;
1651    }
1652    uiBLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxLB + (1+uiPartUnitOffset) * uiNumPartInCUWidth - 1 ];
1653    if ( (bEnforceSliceRestriction && (m_pcCULeft==NULL || m_pcCULeft->getSlice()==NULL || 
1654       m_pcCULeft->getSCUAddr()+uiBLPartUnitIdx < m_pcPic->getCU( getAddr() )->getSliceStartCU(uiCurrPartUnitIdx)||
1655       (m_pcPic->getPicSym()->getTileIdxMap( m_pcCULeft->getAddr() ) != m_pcPic->getPicSym()->getTileIdxMap(getAddr()))
1656       ))
1657     )
1658    {
1659      return NULL;
1660    }
1661    return m_pcCULeft;
1662  }
1663 
1664  uiBLPartUnitIdx = MAX_UINT;
1665  return NULL;
1666}
1667
1668TComDataCU* TComDataCU::getPUAboveRightAdi(UInt&  uiARPartUnitIdx, UInt uiCurrPartUnitIdx, UInt uiPartUnitOffset, Bool bEnforceSliceRestriction )
1669{
1670  UInt uiAbsPartIdxRT     = g_auiZscanToRaster[uiCurrPartUnitIdx];
1671  UInt uiAbsZorderCUIdx   = g_auiZscanToRaster[ m_uiAbsIdxInLCU ] + (m_puhWidth[0] / m_pcPic->getMinCUWidth()) - 1;
1672  UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
1673 
1674  if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxRT] + (m_pcPic->getPicSym()->getMinCUHeight() * uiPartUnitOffset)) >= m_pcSlice->getSPS()->getPicWidthInLumaSamples() )
1675  {
1676    uiARPartUnitIdx = MAX_UINT;
1677    return NULL;
1678  }
1679 
1680  if ( RasterAddress::lessThanCol( uiAbsPartIdxRT, uiNumPartInCUWidth - uiPartUnitOffset, uiNumPartInCUWidth ) )
1681  {
1682    if ( !RasterAddress::isZeroRow( uiAbsPartIdxRT, uiNumPartInCUWidth ) )
1683    {
1684      if ( uiCurrPartUnitIdx > g_auiRasterToZscan[ uiAbsPartIdxRT - uiNumPartInCUWidth + uiPartUnitOffset ] )
1685      {
1686        uiARPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxRT - uiNumPartInCUWidth + uiPartUnitOffset ];
1687        if ( RasterAddress::isEqualRowOrCol( uiAbsPartIdxRT, uiAbsZorderCUIdx, uiNumPartInCUWidth ) )
1688        {
1689          return m_pcPic->getCU( getAddr() );
1690        }
1691        else
1692        {
1693          uiARPartUnitIdx -= m_uiAbsIdxInLCU;
1694          return this;
1695        }
1696      }
1697      uiARPartUnitIdx = MAX_UINT;
1698      return NULL;
1699    }
1700    uiARPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxRT + m_pcPic->getNumPartInCU() - uiNumPartInCUWidth + uiPartUnitOffset ];
1701    if ( (bEnforceSliceRestriction && (m_pcCUAbove==NULL || m_pcCUAbove->getSlice()==NULL || 
1702       m_pcCUAbove->getSCUAddr()+uiARPartUnitIdx < m_pcPic->getCU( getAddr() )->getSliceStartCU(uiCurrPartUnitIdx)||
1703       (m_pcPic->getPicSym()->getTileIdxMap( m_pcCUAbove->getAddr() ) != m_pcPic->getPicSym()->getTileIdxMap(getAddr()))
1704       ))
1705     )
1706    {
1707      return NULL;
1708    }
1709    return m_pcCUAbove;
1710  }
1711 
1712  if ( !RasterAddress::isZeroRow( uiAbsPartIdxRT, uiNumPartInCUWidth ) )
1713  {
1714    uiARPartUnitIdx = MAX_UINT;
1715    return NULL;
1716  }
1717 
1718  uiARPartUnitIdx = g_auiRasterToZscan[ m_pcPic->getNumPartInCU() - uiNumPartInCUWidth + uiPartUnitOffset-1 ];
1719  if ( (bEnforceSliceRestriction && (m_pcCUAboveRight==NULL || m_pcCUAboveRight->getSlice()==NULL ||
1720       m_pcPic->getPicSym()->getInverseCUOrderMap( m_pcCUAboveRight->getAddr()) > m_pcPic->getPicSym()->getInverseCUOrderMap( getAddr()) ||
1721       m_pcCUAboveRight->getSCUAddr()+uiARPartUnitIdx < m_pcPic->getCU( getAddr() )->getSliceStartCU(uiCurrPartUnitIdx)||
1722       (m_pcPic->getPicSym()->getTileIdxMap( m_pcCUAboveRight->getAddr() ) != m_pcPic->getPicSym()->getTileIdxMap(getAddr()))
1723       ))
1724     )
1725  {
1726    return NULL;
1727  }
1728  return m_pcCUAboveRight;
1729}
1730
1731/** Get left QpMinCu
1732*\param   uiLPartUnitIdx
1733*\param   uiCurrAbsIdxInLCU
1734*\returns TComDataCU*   point of TComDataCU of left QpMinCu
1735*/
1736TComDataCU* TComDataCU::getQpMinCuLeft( UInt& uiLPartUnitIdx, UInt uiCurrAbsIdxInLCU)
1737{
1738  UInt numPartInCUWidth = m_pcPic->getNumPartInWidth();
1739  UInt absZorderQpMinCUIdx = (uiCurrAbsIdxInLCU>>((g_uiMaxCUDepth - getSlice()->getPPS()->getMaxCuDQPDepth())<<1))<<((g_uiMaxCUDepth -getSlice()->getPPS()->getMaxCuDQPDepth())<<1);
1740  UInt absRorderQpMinCUIdx = g_auiZscanToRaster[absZorderQpMinCUIdx];
1741
1742  // check for left LCU boundary
1743  if ( RasterAddress::isZeroCol(absRorderQpMinCUIdx, numPartInCUWidth) )
1744  {
1745    return NULL;
1746  }
1747
1748  // get index of left-CU relative to top-left corner of current quantization group
1749  uiLPartUnitIdx = g_auiRasterToZscan[absRorderQpMinCUIdx - 1];
1750
1751  // return pointer to current LCU
1752  return m_pcPic->getCU( getAddr() );
1753}
1754
1755/** Get Above QpMinCu
1756*\param   aPartUnitIdx
1757*\param   currAbsIdxInLCU
1758*\returns TComDataCU*   point of TComDataCU of above QpMinCu
1759*/
1760TComDataCU* TComDataCU::getQpMinCuAbove( UInt& aPartUnitIdx, UInt currAbsIdxInLCU )
1761{
1762  UInt numPartInCUWidth = m_pcPic->getNumPartInWidth();
1763  UInt absZorderQpMinCUIdx = (currAbsIdxInLCU>>((g_uiMaxCUDepth - getSlice()->getPPS()->getMaxCuDQPDepth())<<1))<<((g_uiMaxCUDepth - getSlice()->getPPS()->getMaxCuDQPDepth())<<1);
1764  UInt absRorderQpMinCUIdx = g_auiZscanToRaster[absZorderQpMinCUIdx];
1765
1766  // check for top LCU boundary
1767  if ( RasterAddress::isZeroRow( absRorderQpMinCUIdx, numPartInCUWidth) )
1768  {
1769    return NULL;
1770  }
1771
1772  // get index of top-CU relative to top-left corner of current quantization group
1773  aPartUnitIdx = g_auiRasterToZscan[absRorderQpMinCUIdx - numPartInCUWidth];
1774
1775  // return pointer to current LCU
1776  return m_pcPic->getCU( getAddr() );
1777}
1778
1779/** Get reference QP from left QpMinCu or latest coded QP
1780*\param   uiCurrAbsIdxInLCU
1781*\returns Char   reference QP value
1782*/
1783Char TComDataCU::getRefQP( UInt uiCurrAbsIdxInLCU )
1784{
1785  UInt        lPartIdx = 0, aPartIdx = 0;
1786  TComDataCU* cULeft  = getQpMinCuLeft ( lPartIdx, m_uiAbsIdxInLCU + uiCurrAbsIdxInLCU );
1787  TComDataCU* cUAbove = getQpMinCuAbove( aPartIdx, m_uiAbsIdxInLCU + uiCurrAbsIdxInLCU );
1788  return (((cULeft? cULeft->getQP( lPartIdx ): getLastCodedQP( uiCurrAbsIdxInLCU )) + (cUAbove? cUAbove->getQP( aPartIdx ): getLastCodedQP( uiCurrAbsIdxInLCU )) + 1) >> 1);
1789}
1790
1791Int TComDataCU::getLastValidPartIdx( Int iAbsPartIdx )
1792{
1793  Int iLastValidPartIdx = iAbsPartIdx-1;
1794  while ( iLastValidPartIdx >= 0
1795       && getPredictionMode( iLastValidPartIdx ) == MODE_NONE )
1796  {
1797    UInt uiDepth = getDepth( iLastValidPartIdx );
1798    iLastValidPartIdx -= m_uiNumPartition>>(uiDepth<<1);
1799  }
1800  return iLastValidPartIdx;
1801}
1802
1803Char TComDataCU::getLastCodedQP( UInt uiAbsPartIdx )
1804{
1805  UInt uiQUPartIdxMask = ~((1<<((g_uiMaxCUDepth - getSlice()->getPPS()->getMaxCuDQPDepth())<<1))-1);
1806  Int iLastValidPartIdx = getLastValidPartIdx( uiAbsPartIdx&uiQUPartIdxMask );
1807  if ( uiAbsPartIdx < m_uiNumPartition
1808    && (getSCUAddr()+iLastValidPartIdx < getSliceStartCU(m_uiAbsIdxInLCU+uiAbsPartIdx)))
1809  {
1810    return getSlice()->getSliceQp();
1811  }
1812  else
1813  if ( iLastValidPartIdx >= 0 )
1814  {
1815    return getQP( iLastValidPartIdx );
1816  }
1817  else
1818  {
1819    if ( getZorderIdxInCU() > 0 )
1820    {
1821      return getPic()->getCU( getAddr() )->getLastCodedQP( getZorderIdxInCU() );
1822    }
1823    else if ( getPic()->getPicSym()->getInverseCUOrderMap(getAddr()) > 0
1824      && getPic()->getPicSym()->getTileIdxMap(getAddr()) == getPic()->getPicSym()->getTileIdxMap(getPic()->getPicSym()->getCUOrderMap(getPic()->getPicSym()->getInverseCUOrderMap(getAddr())-1))
1825      && !( getSlice()->getPPS()->getEntropyCodingSyncEnabledFlag() && getAddr() % getPic()->getFrameWidthInCU() == 0 ) )
1826    {
1827      return getPic()->getCU( getPic()->getPicSym()->getCUOrderMap(getPic()->getPicSym()->getInverseCUOrderMap(getAddr())-1) )->getLastCodedQP( getPic()->getNumPartInCU() );
1828    }
1829    else
1830    {
1831      return getSlice()->getSliceQp();
1832    }
1833  }
1834}
1835/** Check whether the CU is coded in lossless coding mode
1836 * \param   uiAbsPartIdx
1837 * \returns true if the CU is coded in lossless coding mode; false if otherwise
1838 */
1839Bool TComDataCU::isLosslessCoded(UInt absPartIdx)
1840{
1841  return (getSlice()->getPPS()->getTransquantBypassEnableFlag() && getCUTransquantBypass (absPartIdx));
1842}
1843
1844/** Get allowed chroma intra modes
1845*\param   uiAbsPartIdx
1846*\param   uiModeList  pointer to chroma intra modes array
1847*\returns
1848*- fill uiModeList with chroma intra modes
1849*/
1850Void TComDataCU::getAllowedChromaDir( UInt uiAbsPartIdx, UInt* uiModeList )
1851{
1852  uiModeList[0] = PLANAR_IDX;
1853  uiModeList[1] = VER_IDX;
1854  uiModeList[2] = HOR_IDX;
1855  uiModeList[3] = DC_IDX;
1856  uiModeList[4] = DM_CHROMA_IDX;
1857
1858  UInt uiLumaMode = getLumaIntraDir( uiAbsPartIdx );
1859
1860  for( Int i = 0; i < NUM_CHROMA_MODE - 1; i++ )
1861  {
1862    if( uiLumaMode == uiModeList[i] )
1863    {
1864      uiModeList[i] = 34; // VER+8 mode
1865      break;
1866    }
1867  }
1868}
1869
1870/** Get most probable intra modes
1871*\param   uiAbsPartIdx
1872*\param   uiIntraDirPred  pointer to the array for MPM storage
1873*\param   piMode          it is set with MPM mode in case both MPM are equal. It is used to restrict RD search at encode side.
1874*\returns Number of MPM
1875*/
1876Int TComDataCU::getIntraDirLumaPredictor( UInt uiAbsPartIdx, Int* uiIntraDirPred, Int* piMode  )
1877{
1878  TComDataCU* pcTempCU;
1879  UInt        uiTempPartIdx;
1880  Int         iLeftIntraDir, iAboveIntraDir;
1881  Int         uiPredNum = 0;
1882 
1883  // Get intra direction of left PU
1884  pcTempCU = getPULeft( uiTempPartIdx, m_uiAbsIdxInLCU + uiAbsPartIdx );
1885 
1886  iLeftIntraDir  = pcTempCU ? ( pcTempCU->isIntra( uiTempPartIdx ) ? pcTempCU->getLumaIntraDir( uiTempPartIdx ) : DC_IDX ) : DC_IDX;
1887#if H_3D_DIM
1888  mapDepthModeToIntraDir( iLeftIntraDir );
1889#endif
1890 
1891  // Get intra direction of above PU
1892  pcTempCU = getPUAbove( uiTempPartIdx, m_uiAbsIdxInLCU + uiAbsPartIdx, true, true );
1893 
1894  iAboveIntraDir = pcTempCU ? ( pcTempCU->isIntra( uiTempPartIdx ) ? pcTempCU->getLumaIntraDir( uiTempPartIdx ) : DC_IDX ) : DC_IDX;
1895#if H_3D_DIM
1896  mapDepthModeToIntraDir( iAboveIntraDir );
1897#endif
1898 
1899  uiPredNum = 3;
1900  if(iLeftIntraDir == iAboveIntraDir)
1901  {
1902    if( piMode )
1903    {
1904      *piMode = 1;
1905    }
1906   
1907    if (iLeftIntraDir > 1) // angular modes
1908    {
1909      uiIntraDirPred[0] = iLeftIntraDir;
1910      uiIntraDirPred[1] = ((iLeftIntraDir + 29) % 32) + 2;
1911      uiIntraDirPred[2] = ((iLeftIntraDir - 1 ) % 32) + 2;
1912    }
1913    else //non-angular
1914    {
1915      uiIntraDirPred[0] = PLANAR_IDX;
1916      uiIntraDirPred[1] = DC_IDX;
1917      uiIntraDirPred[2] = VER_IDX; 
1918    }
1919  }
1920  else
1921  {
1922    if( piMode )
1923    {
1924      *piMode = 2;
1925    }
1926    uiIntraDirPred[0] = iLeftIntraDir;
1927    uiIntraDirPred[1] = iAboveIntraDir;
1928   
1929    if (iLeftIntraDir && iAboveIntraDir ) //both modes are non-planar
1930    {
1931      uiIntraDirPred[2] = PLANAR_IDX;
1932    }
1933    else
1934    {
1935      uiIntraDirPred[2] =  (iLeftIntraDir+iAboveIntraDir)<2? VER_IDX : DC_IDX;
1936    }
1937  }
1938 
1939  return uiPredNum;
1940}
1941
1942UInt TComDataCU::getCtxSplitFlag( UInt uiAbsPartIdx, UInt uiDepth )
1943{
1944  TComDataCU* pcTempCU;
1945  UInt        uiTempPartIdx;
1946  UInt        uiCtx;
1947  // Get left split flag
1948  pcTempCU = getPULeft( uiTempPartIdx, m_uiAbsIdxInLCU + uiAbsPartIdx );
1949  uiCtx  = ( pcTempCU ) ? ( ( pcTempCU->getDepth( uiTempPartIdx ) > uiDepth ) ? 1 : 0 ) : 0;
1950 
1951  // Get above split flag
1952  pcTempCU = getPUAbove( uiTempPartIdx, m_uiAbsIdxInLCU + uiAbsPartIdx );
1953  uiCtx += ( pcTempCU ) ? ( ( pcTempCU->getDepth( uiTempPartIdx ) > uiDepth ) ? 1 : 0 ) : 0;
1954 
1955  return uiCtx;
1956}
1957
1958UInt TComDataCU::getCtxQtCbf( TextType eType, UInt uiTrDepth )
1959{
1960  if( eType )
1961  {
1962    return uiTrDepth;
1963  }
1964  else
1965  {
1966    const UInt uiCtx = ( uiTrDepth == 0 ? 1 : 0 );
1967    return uiCtx;
1968  }
1969}
1970
1971UInt TComDataCU::getQuadtreeTULog2MinSizeInCU( UInt absPartIdx )
1972{
1973  UInt log2CbSize = g_aucConvertToBit[getWidth( absPartIdx )] + 2;
1974  PartSize  partSize  = getPartitionSize( absPartIdx );
1975  UInt quadtreeTUMaxDepth = getPredictionMode( absPartIdx ) == MODE_INTRA ? m_pcSlice->getSPS()->getQuadtreeTUMaxDepthIntra() : m_pcSlice->getSPS()->getQuadtreeTUMaxDepthInter(); 
1976  Int intraSplitFlag = ( getPredictionMode( absPartIdx ) == MODE_INTRA && partSize == SIZE_NxN ) ? 1 : 0;
1977  Int interSplitFlag = ((quadtreeTUMaxDepth == 1) && (getPredictionMode( absPartIdx ) == MODE_INTER) && (partSize != SIZE_2Nx2N) );
1978 
1979  UInt log2MinTUSizeInCU = 0;
1980  if (log2CbSize < (m_pcSlice->getSPS()->getQuadtreeTULog2MinSize() + quadtreeTUMaxDepth - 1 + interSplitFlag + intraSplitFlag) ) 
1981  {
1982    // when fully making use of signaled TUMaxDepth + inter/intraSplitFlag, resulting luma TB size is < QuadtreeTULog2MinSize
1983    log2MinTUSizeInCU = m_pcSlice->getSPS()->getQuadtreeTULog2MinSize();
1984  }
1985  else
1986  {
1987    // when fully making use of signaled TUMaxDepth + inter/intraSplitFlag, resulting luma TB size is still >= QuadtreeTULog2MinSize
1988    log2MinTUSizeInCU = log2CbSize - ( quadtreeTUMaxDepth - 1 + interSplitFlag + intraSplitFlag); // stop when trafoDepth == hierarchy_depth = splitFlag
1989    if ( log2MinTUSizeInCU > m_pcSlice->getSPS()->getQuadtreeTULog2MaxSize())
1990    {
1991      // when fully making use of signaled TUMaxDepth + inter/intraSplitFlag, resulting luma TB size is still > QuadtreeTULog2MaxSize
1992      log2MinTUSizeInCU = m_pcSlice->getSPS()->getQuadtreeTULog2MaxSize();
1993    } 
1994  }
1995  return log2MinTUSizeInCU;
1996}
1997
1998UInt TComDataCU::getCtxSkipFlag( UInt uiAbsPartIdx )
1999{
2000  TComDataCU* pcTempCU;
2001  UInt        uiTempPartIdx;
2002  UInt        uiCtx = 0;
2003 
2004  // Get BCBP of left PU
2005  pcTempCU = getPULeft( uiTempPartIdx, m_uiAbsIdxInLCU + uiAbsPartIdx );
2006  uiCtx    = ( pcTempCU ) ? pcTempCU->isSkipped( uiTempPartIdx ) : 0;
2007 
2008  // Get BCBP of above PU
2009  pcTempCU = getPUAbove( uiTempPartIdx, m_uiAbsIdxInLCU + uiAbsPartIdx );
2010  uiCtx   += ( pcTempCU ) ? pcTempCU->isSkipped( uiTempPartIdx ) : 0;
2011 
2012  return uiCtx;
2013}
2014
2015UInt TComDataCU::getCtxInterDir( UInt uiAbsPartIdx )
2016{
2017  return getDepth( uiAbsPartIdx );
2018}
2019
2020Void TComDataCU::setCbfSubParts( UInt uiCbfY, UInt uiCbfU, UInt uiCbfV, UInt uiAbsPartIdx, UInt uiDepth )
2021{
2022  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
2023  memset( m_puhCbf[0] + uiAbsPartIdx, uiCbfY, sizeof( UChar ) * uiCurrPartNumb );
2024  memset( m_puhCbf[1] + uiAbsPartIdx, uiCbfU, sizeof( UChar ) * uiCurrPartNumb );
2025  memset( m_puhCbf[2] + uiAbsPartIdx, uiCbfV, sizeof( UChar ) * uiCurrPartNumb );
2026}
2027
2028Void TComDataCU::setCbfSubParts( UInt uiCbf, TextType eTType, UInt uiAbsPartIdx, UInt uiDepth )
2029{
2030  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
2031  memset( m_puhCbf[g_aucConvertTxtTypeToIdx[eTType]] + uiAbsPartIdx, uiCbf, sizeof( UChar ) * uiCurrPartNumb );
2032}
2033
2034/** Sets a coded block flag for all sub-partitions of a partition
2035 * \param uiCbf The value of the coded block flag to be set
2036 * \param eTType
2037 * \param uiAbsPartIdx
2038 * \param uiPartIdx
2039 * \param uiDepth
2040 * \returns Void
2041 */
2042Void TComDataCU::setCbfSubParts ( UInt uiCbf, TextType eTType, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
2043{
2044  setSubPart<UChar>( uiCbf, m_puhCbf[g_aucConvertTxtTypeToIdx[eTType]], uiAbsPartIdx, uiDepth, uiPartIdx );
2045}
2046
2047Void TComDataCU::setDepthSubParts( UInt uiDepth, UInt uiAbsPartIdx )
2048{
2049  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
2050  memset( m_puhDepth + uiAbsPartIdx, uiDepth, sizeof(UChar)*uiCurrPartNumb );
2051}
2052
2053Bool TComDataCU::isFirstAbsZorderIdxInDepth (UInt uiAbsPartIdx, UInt uiDepth)
2054{
2055  UInt uiPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
2056  return (((m_uiAbsIdxInLCU + uiAbsPartIdx)% uiPartNumb) == 0);
2057}
2058
2059Void TComDataCU::setPartSizeSubParts( PartSize eMode, UInt uiAbsPartIdx, UInt uiDepth )
2060{
2061  assert( sizeof( *m_pePartSize) == 1 );
2062  memset( m_pePartSize + uiAbsPartIdx, eMode, m_pcPic->getNumPartInCU() >> ( 2 * uiDepth ) );
2063}
2064
2065Void TComDataCU::setCUTransquantBypassSubParts( Bool flag, UInt uiAbsPartIdx, UInt uiDepth )
2066{
2067  memset( m_CUTransquantBypass + uiAbsPartIdx, flag, m_pcPic->getNumPartInCU() >> ( 2 * uiDepth ) );
2068}
2069
2070Void TComDataCU::setSkipFlagSubParts( Bool skip, UInt absPartIdx, UInt depth )
2071{
2072  assert( sizeof( *m_skipFlag) == 1 );
2073  memset( m_skipFlag + absPartIdx, skip, m_pcPic->getNumPartInCU() >> ( 2 * depth ) );
2074}
2075
2076Void TComDataCU::setPredModeSubParts( PredMode eMode, UInt uiAbsPartIdx, UInt uiDepth )
2077{
2078  assert( sizeof( *m_pePredMode) == 1 );
2079  memset( m_pePredMode + uiAbsPartIdx, eMode, m_pcPic->getNumPartInCU() >> ( 2 * uiDepth ) );
2080}
2081
2082Void TComDataCU::setQPSubCUs( Int qp, TComDataCU* pcCU, UInt absPartIdx, UInt depth, Bool &foundNonZeroCbf )
2083{
2084  UInt currPartNumb = m_pcPic->getNumPartInCU() >> (depth << 1);
2085  UInt currPartNumQ = currPartNumb >> 2;
2086
2087  if(!foundNonZeroCbf)
2088  {
2089    if(pcCU->getDepth(absPartIdx) > depth)
2090    {
2091      for ( UInt partUnitIdx = 0; partUnitIdx < 4; partUnitIdx++ )
2092      {
2093        pcCU->setQPSubCUs( qp, pcCU, absPartIdx+partUnitIdx*currPartNumQ, depth+1, foundNonZeroCbf );
2094      }
2095    }
2096    else
2097    {
2098      if(pcCU->getCbf( absPartIdx, TEXT_LUMA ) || pcCU->getCbf( absPartIdx, TEXT_CHROMA_U ) || pcCU->getCbf( absPartIdx, TEXT_CHROMA_V ) )
2099      {
2100        foundNonZeroCbf = true;
2101      }
2102      else
2103      {
2104        setQPSubParts(qp, absPartIdx, depth);
2105      }
2106    }
2107  }
2108}
2109
2110Void TComDataCU::setQPSubParts( Int qp, UInt uiAbsPartIdx, UInt uiDepth )
2111{
2112  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
2113  TComSlice * pcSlice = getPic()->getSlice(getPic()->getCurrSliceIdx());
2114
2115  for(UInt uiSCUIdx = uiAbsPartIdx; uiSCUIdx < uiAbsPartIdx+uiCurrPartNumb; uiSCUIdx++)
2116  {
2117    if( m_pcPic->getCU( getAddr() )->getSliceSegmentStartCU(uiSCUIdx+getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr() )
2118    {
2119      m_phQP[uiSCUIdx] = qp;
2120    }
2121  }
2122}
2123
2124Void TComDataCU::setLumaIntraDirSubParts( UInt uiDir, UInt uiAbsPartIdx, UInt uiDepth )
2125{
2126  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
2127 
2128  memset( m_puhLumaIntraDir + uiAbsPartIdx, uiDir, sizeof(UChar)*uiCurrPartNumb );
2129}
2130
2131template<typename T>
2132Void TComDataCU::setSubPart( T uiParameter, T* puhBaseLCU, UInt uiCUAddr, UInt uiCUDepth, UInt uiPUIdx )
2133{
2134  assert( sizeof(T) == 1 ); // Using memset() works only for types of size 1
2135 
2136  UInt uiCurrPartNumQ = (m_pcPic->getNumPartInCU() >> (2 * uiCUDepth)) >> 2;
2137  switch ( m_pePartSize[ uiCUAddr ] )
2138  {
2139    case SIZE_2Nx2N:
2140      memset( puhBaseLCU + uiCUAddr, uiParameter, 4 * uiCurrPartNumQ );
2141      break;
2142    case SIZE_2NxN:
2143      memset( puhBaseLCU + uiCUAddr, uiParameter, 2 * uiCurrPartNumQ );
2144      break;
2145    case SIZE_Nx2N:
2146      memset( puhBaseLCU + uiCUAddr, uiParameter, uiCurrPartNumQ );
2147      memset( puhBaseLCU + uiCUAddr + 2 * uiCurrPartNumQ, uiParameter, uiCurrPartNumQ );
2148      break;
2149    case SIZE_NxN:
2150      memset( puhBaseLCU + uiCUAddr, uiParameter, uiCurrPartNumQ ); 
2151      break;
2152    case SIZE_2NxnU:
2153      if ( uiPUIdx == 0 )
2154      {
2155        memset( puhBaseLCU + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 1) );                     
2156        memset( puhBaseLCU + uiCUAddr + uiCurrPartNumQ, uiParameter, (uiCurrPartNumQ >> 1) );                     
2157      }
2158      else if ( uiPUIdx == 1 )
2159      {
2160        memset( puhBaseLCU + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 1) );                     
2161        memset( puhBaseLCU + uiCUAddr + uiCurrPartNumQ, uiParameter, ((uiCurrPartNumQ >> 1) + (uiCurrPartNumQ << 1)) );                     
2162      }
2163      else
2164      {
2165        assert(0);
2166      }
2167      break;
2168    case SIZE_2NxnD:
2169      if ( uiPUIdx == 0 )
2170      {
2171        memset( puhBaseLCU + uiCUAddr, uiParameter, ((uiCurrPartNumQ << 1) + (uiCurrPartNumQ >> 1)) );                     
2172        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ << 1) + uiCurrPartNumQ, uiParameter, (uiCurrPartNumQ >> 1) );                     
2173      }
2174      else if ( uiPUIdx == 1 )
2175      {
2176        memset( puhBaseLCU + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 1) );                     
2177        memset( puhBaseLCU + uiCUAddr + uiCurrPartNumQ, uiParameter, (uiCurrPartNumQ >> 1) );                     
2178      }
2179      else
2180      {
2181        assert(0);
2182      }
2183      break;
2184    case SIZE_nLx2N:
2185      if ( uiPUIdx == 0 )
2186      {
2187        memset( puhBaseLCU + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 2) );
2188        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) ); 
2189        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ << 1), uiParameter, (uiCurrPartNumQ >> 2) ); 
2190        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ << 1) + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) ); 
2191      }
2192      else if ( uiPUIdx == 1 )
2193      {
2194        memset( puhBaseLCU + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 2) );
2195        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ + (uiCurrPartNumQ >> 2)) ); 
2196        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ << 1), uiParameter, (uiCurrPartNumQ >> 2) ); 
2197        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ << 1) + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ + (uiCurrPartNumQ >> 2)) ); 
2198      }
2199      else
2200      {
2201        assert(0);
2202      }
2203      break;
2204    case SIZE_nRx2N:
2205      if ( uiPUIdx == 0 )
2206      {     
2207        memset( puhBaseLCU + uiCUAddr, uiParameter, (uiCurrPartNumQ + (uiCurrPartNumQ >> 2)) );                           
2208        memset( puhBaseLCU + uiCUAddr + uiCurrPartNumQ + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) );                           
2209        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ << 1), uiParameter, (uiCurrPartNumQ + (uiCurrPartNumQ >> 2)) );                           
2210        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ << 1) + uiCurrPartNumQ + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) );                           
2211      }
2212      else if ( uiPUIdx == 1 )
2213      {
2214        memset( puhBaseLCU + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 2) );                           
2215        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) );                           
2216        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ << 1), uiParameter, (uiCurrPartNumQ >> 2) );                           
2217        memset( puhBaseLCU + uiCUAddr + (uiCurrPartNumQ << 1) + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) );                         
2218      }
2219      else
2220      {
2221        assert(0);
2222      }
2223      break;
2224    default:
2225      assert( 0 );
2226  }
2227}
2228
2229Void TComDataCU::setMergeFlagSubParts ( Bool bMergeFlag, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
2230{
2231  setSubPart( bMergeFlag, m_pbMergeFlag, uiAbsPartIdx, uiDepth, uiPartIdx );
2232}
2233
2234Void TComDataCU::setMergeIndexSubParts ( UInt uiMergeIndex, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
2235{
2236  setSubPart<UChar>( uiMergeIndex, m_puhMergeIndex, uiAbsPartIdx, uiDepth, uiPartIdx );
2237}
2238
2239Void TComDataCU::setChromIntraDirSubParts( UInt uiDir, UInt uiAbsPartIdx, UInt uiDepth )
2240{
2241  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
2242 
2243  memset( m_puhChromaIntraDir + uiAbsPartIdx, uiDir, sizeof(UChar)*uiCurrPartNumb );
2244}
2245
2246Void TComDataCU::setInterDirSubParts( UInt uiDir, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
2247{
2248  setSubPart<UChar>( uiDir, m_puhInterDir, uiAbsPartIdx, uiDepth, uiPartIdx );
2249}
2250
2251Void TComDataCU::setMVPIdxSubParts( Int iMVPIdx, RefPicList eRefPicList, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
2252{
2253  setSubPart<Char>( iMVPIdx, m_apiMVPIdx[eRefPicList], uiAbsPartIdx, uiDepth, uiPartIdx );
2254}
2255
2256Void TComDataCU::setMVPNumSubParts( Int iMVPNum, RefPicList eRefPicList, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
2257{
2258  setSubPart<Char>( iMVPNum, m_apiMVPNum[eRefPicList], uiAbsPartIdx, uiDepth, uiPartIdx );
2259}
2260
2261
2262Void TComDataCU::setTrIdxSubParts( UInt uiTrIdx, UInt uiAbsPartIdx, UInt uiDepth )
2263{
2264  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
2265 
2266  memset( m_puhTrIdx + uiAbsPartIdx, uiTrIdx, sizeof(UChar)*uiCurrPartNumb );
2267}
2268
2269Void TComDataCU::setTransformSkipSubParts( UInt useTransformSkipY, UInt useTransformSkipU, UInt useTransformSkipV, UInt uiAbsPartIdx, UInt uiDepth )
2270{
2271  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
2272
2273  memset( m_puhTransformSkip[0] + uiAbsPartIdx, useTransformSkipY, sizeof( UChar ) * uiCurrPartNumb );
2274  memset( m_puhTransformSkip[1] + uiAbsPartIdx, useTransformSkipU, sizeof( UChar ) * uiCurrPartNumb );
2275  memset( m_puhTransformSkip[2] + uiAbsPartIdx, useTransformSkipV, sizeof( UChar ) * uiCurrPartNumb );
2276}
2277
2278Void TComDataCU::setTransformSkipSubParts( UInt useTransformSkip, TextType eType, UInt uiAbsPartIdx, UInt uiDepth)
2279{
2280  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
2281
2282  memset( m_puhTransformSkip[g_aucConvertTxtTypeToIdx[eType]] + uiAbsPartIdx, useTransformSkip, sizeof( UChar ) * uiCurrPartNumb );
2283}
2284
2285Void TComDataCU::setSizeSubParts( UInt uiWidth, UInt uiHeight, UInt uiAbsPartIdx, UInt uiDepth )
2286{
2287  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
2288 
2289  memset( m_puhWidth  + uiAbsPartIdx, uiWidth,  sizeof(UChar)*uiCurrPartNumb );
2290  memset( m_puhHeight + uiAbsPartIdx, uiHeight, sizeof(UChar)*uiCurrPartNumb );
2291}
2292
2293UChar TComDataCU::getNumPartInter()
2294{
2295  UChar iNumPart = 0;
2296 
2297  switch ( m_pePartSize[0] )
2298  {
2299    case SIZE_2Nx2N:    iNumPart = 1; break;
2300    case SIZE_2NxN:     iNumPart = 2; break;
2301    case SIZE_Nx2N:     iNumPart = 2; break;
2302    case SIZE_NxN:      iNumPart = 4; break;
2303    case SIZE_2NxnU:    iNumPart = 2; break;
2304    case SIZE_2NxnD:    iNumPart = 2; break;
2305    case SIZE_nLx2N:    iNumPart = 2; break;
2306    case SIZE_nRx2N:    iNumPart = 2; break;
2307    default:            assert (0);   break;
2308  }
2309 
2310  return  iNumPart;
2311}
2312
2313Void TComDataCU::getPartIndexAndSize( UInt uiPartIdx, UInt& ruiPartAddr, Int& riWidth, Int& riHeight )
2314{
2315  switch ( m_pePartSize[0] )
2316  {
2317    case SIZE_2NxN:
2318      riWidth = getWidth(0);      riHeight = getHeight(0) >> 1; ruiPartAddr = ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 1;
2319      break;
2320    case SIZE_Nx2N:
2321      riWidth = getWidth(0) >> 1; riHeight = getHeight(0);      ruiPartAddr = ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 2;
2322      break;
2323    case SIZE_NxN:
2324      riWidth = getWidth(0) >> 1; riHeight = getHeight(0) >> 1; ruiPartAddr = ( m_uiNumPartition >> 2 ) * uiPartIdx;
2325      break;
2326    case SIZE_2NxnU:
2327      riWidth     = getWidth(0);
2328      riHeight    = ( uiPartIdx == 0 ) ?  getHeight(0) >> 2 : ( getHeight(0) >> 2 ) + ( getHeight(0) >> 1 );
2329      ruiPartAddr = ( uiPartIdx == 0 ) ? 0 : m_uiNumPartition >> 3;
2330      break;
2331    case SIZE_2NxnD:
2332      riWidth     = getWidth(0);
2333      riHeight    = ( uiPartIdx == 0 ) ?  ( getHeight(0) >> 2 ) + ( getHeight(0) >> 1 ) : getHeight(0) >> 2;
2334      ruiPartAddr = ( uiPartIdx == 0 ) ? 0 : (m_uiNumPartition >> 1) + (m_uiNumPartition >> 3);
2335      break;
2336    case SIZE_nLx2N:
2337      riWidth     = ( uiPartIdx == 0 ) ? getWidth(0) >> 2 : ( getWidth(0) >> 2 ) + ( getWidth(0) >> 1 );
2338      riHeight    = getHeight(0);
2339      ruiPartAddr = ( uiPartIdx == 0 ) ? 0 : m_uiNumPartition >> 4;
2340      break;
2341    case SIZE_nRx2N:
2342      riWidth     = ( uiPartIdx == 0 ) ? ( getWidth(0) >> 2 ) + ( getWidth(0) >> 1 ) : getWidth(0) >> 2;
2343      riHeight    = getHeight(0);
2344      ruiPartAddr = ( uiPartIdx == 0 ) ? 0 : (m_uiNumPartition >> 2) + (m_uiNumPartition >> 4);
2345      break;
2346    default:
2347      assert ( m_pePartSize[0] == SIZE_2Nx2N );
2348      riWidth = getWidth(0);      riHeight = getHeight(0);      ruiPartAddr = 0;
2349      break;
2350  }
2351}
2352
2353
2354Void TComDataCU::getMvField ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefPicList, TComMvField& rcMvField )
2355{
2356  if ( pcCU == NULL )  // OUT OF BOUNDARY
2357  {
2358    TComMv  cZeroMv;
2359    rcMvField.setMvField( cZeroMv, NOT_VALID );
2360    return;
2361  }
2362 
2363  TComCUMvField*  pcCUMvField = pcCU->getCUMvField( eRefPicList );
2364  rcMvField.setMvField( pcCUMvField->getMv( uiAbsPartIdx ), pcCUMvField->getRefIdx( uiAbsPartIdx ) );
2365}
2366
2367Void TComDataCU::deriveLeftRightTopIdxGeneral ( UInt uiAbsPartIdx, UInt uiPartIdx, UInt& ruiPartIdxLT, UInt& ruiPartIdxRT )
2368{
2369  ruiPartIdxLT = m_uiAbsIdxInLCU + uiAbsPartIdx;
2370  UInt uiPUWidth = 0;
2371 
2372  switch ( m_pePartSize[uiAbsPartIdx] )
2373  {
2374    case SIZE_2Nx2N: uiPUWidth = m_puhWidth[uiAbsPartIdx];  break;
2375    case SIZE_2NxN:  uiPUWidth = m_puhWidth[uiAbsPartIdx];   break;
2376    case SIZE_Nx2N:  uiPUWidth = m_puhWidth[uiAbsPartIdx]  >> 1;  break;
2377    case SIZE_NxN:   uiPUWidth = m_puhWidth[uiAbsPartIdx]  >> 1; break;
2378    case SIZE_2NxnU:   uiPUWidth = m_puhWidth[uiAbsPartIdx]; break;
2379    case SIZE_2NxnD:   uiPUWidth = m_puhWidth[uiAbsPartIdx]; break;
2380    case SIZE_nLx2N:   
2381      if ( uiPartIdx == 0 )
2382      {
2383        uiPUWidth = m_puhWidth[uiAbsPartIdx]  >> 2; 
2384      }
2385      else if ( uiPartIdx == 1 )
2386      {
2387        uiPUWidth = (m_puhWidth[uiAbsPartIdx]  >> 1) + (m_puhWidth[uiAbsPartIdx]  >> 2); 
2388      }
2389      else
2390      {
2391        assert(0);
2392      }
2393      break;
2394    case SIZE_nRx2N:   
2395      if ( uiPartIdx == 0 )
2396      {
2397        uiPUWidth = (m_puhWidth[uiAbsPartIdx]  >> 1) + (m_puhWidth[uiAbsPartIdx]  >> 2); 
2398      }
2399      else if ( uiPartIdx == 1 )
2400      {
2401        uiPUWidth = m_puhWidth[uiAbsPartIdx]  >> 2; 
2402      }
2403      else
2404      {
2405        assert(0);
2406      }
2407      break;
2408    default:
2409      assert (0);
2410      break;
2411  }
2412 
2413  ruiPartIdxRT = g_auiRasterToZscan [g_auiZscanToRaster[ ruiPartIdxLT ] + uiPUWidth / m_pcPic->getMinCUWidth() - 1 ];
2414}
2415
2416Void TComDataCU::deriveLeftBottomIdxGeneral( UInt uiAbsPartIdx, UInt uiPartIdx, UInt& ruiPartIdxLB )
2417{
2418  UInt uiPUHeight = 0;
2419  switch ( m_pePartSize[uiAbsPartIdx] )
2420  {
2421    case SIZE_2Nx2N: uiPUHeight = m_puhHeight[uiAbsPartIdx];    break;
2422    case SIZE_2NxN:  uiPUHeight = m_puhHeight[uiAbsPartIdx] >> 1;    break;
2423    case SIZE_Nx2N:  uiPUHeight = m_puhHeight[uiAbsPartIdx];  break;
2424    case SIZE_NxN:   uiPUHeight = m_puhHeight[uiAbsPartIdx] >> 1;    break;
2425    case SIZE_2NxnU: 
2426      if ( uiPartIdx == 0 )
2427      {
2428        uiPUHeight = m_puhHeight[uiAbsPartIdx] >> 2;   
2429      }
2430      else if ( uiPartIdx == 1 )
2431      {
2432        uiPUHeight = (m_puhHeight[uiAbsPartIdx] >> 1) + (m_puhHeight[uiAbsPartIdx] >> 2);   
2433      }
2434      else
2435      {
2436        assert(0);
2437      }
2438      break;
2439    case SIZE_2NxnD: 
2440      if ( uiPartIdx == 0 )
2441      {
2442        uiPUHeight = (m_puhHeight[uiAbsPartIdx] >> 1) + (m_puhHeight[uiAbsPartIdx] >> 2);   
2443      }
2444      else if ( uiPartIdx == 1 )
2445      {
2446        uiPUHeight = m_puhHeight[uiAbsPartIdx] >> 2;   
2447      }
2448      else
2449      {
2450        assert(0);
2451      }
2452      break;
2453    case SIZE_nLx2N: uiPUHeight = m_puhHeight[uiAbsPartIdx];  break;
2454    case SIZE_nRx2N: uiPUHeight = m_puhHeight[uiAbsPartIdx];  break;
2455    default:
2456      assert (0);
2457      break;
2458  }
2459 
2460  ruiPartIdxLB      = g_auiRasterToZscan [g_auiZscanToRaster[ m_uiAbsIdxInLCU + uiAbsPartIdx ] + ((uiPUHeight / m_pcPic->getMinCUHeight()) - 1)*m_pcPic->getNumPartInWidth()];
2461}
2462
2463Void TComDataCU::deriveLeftRightTopIdx ( UInt uiPartIdx, UInt& ruiPartIdxLT, UInt& ruiPartIdxRT )
2464{
2465  ruiPartIdxLT = m_uiAbsIdxInLCU;
2466  ruiPartIdxRT = g_auiRasterToZscan [g_auiZscanToRaster[ ruiPartIdxLT ] + m_puhWidth[0] / m_pcPic->getMinCUWidth() - 1 ];
2467 
2468  switch ( m_pePartSize[0] )
2469  {
2470    case SIZE_2Nx2N:                                                                                                                                break;
2471    case SIZE_2NxN:
2472      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 1; ruiPartIdxRT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 1;
2473      break;
2474    case SIZE_Nx2N:
2475      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 2; ruiPartIdxRT -= ( uiPartIdx == 1 )? 0 : m_uiNumPartition >> 2;
2476      break;
2477    case SIZE_NxN:
2478      ruiPartIdxLT += ( m_uiNumPartition >> 2 ) * uiPartIdx;         ruiPartIdxRT +=  ( m_uiNumPartition >> 2 ) * ( uiPartIdx - 1 );
2479      break;
2480    case SIZE_2NxnU:
2481      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 3;
2482      ruiPartIdxRT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 3;
2483      break;
2484    case SIZE_2NxnD:
2485      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : ( m_uiNumPartition >> 1 ) + ( m_uiNumPartition >> 3 );
2486      ruiPartIdxRT += ( uiPartIdx == 0 )? 0 : ( m_uiNumPartition >> 1 ) + ( m_uiNumPartition >> 3 );
2487      break;
2488    case SIZE_nLx2N:
2489      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 4;
2490      ruiPartIdxRT -= ( uiPartIdx == 1 )? 0 : ( m_uiNumPartition >> 2 ) + ( m_uiNumPartition >> 4 );
2491      break;
2492    case SIZE_nRx2N:
2493      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : ( m_uiNumPartition >> 2 ) + ( m_uiNumPartition >> 4 );
2494      ruiPartIdxRT -= ( uiPartIdx == 1 )? 0 : m_uiNumPartition >> 4;
2495      break;
2496    default:
2497      assert (0);
2498      break;
2499  }
2500 
2501}
2502
2503Void TComDataCU::deriveLeftBottomIdx( UInt  uiPartIdx,      UInt&      ruiPartIdxLB )
2504{
2505  ruiPartIdxLB      = g_auiRasterToZscan [g_auiZscanToRaster[ m_uiAbsIdxInLCU ] + ( ((m_puhHeight[0] / m_pcPic->getMinCUHeight())>>1) - 1)*m_pcPic->getNumPartInWidth()];
2506 
2507  switch ( m_pePartSize[0] )
2508  {
2509    case SIZE_2Nx2N:
2510      ruiPartIdxLB += m_uiNumPartition >> 1;
2511      break;
2512    case SIZE_2NxN:
2513      ruiPartIdxLB += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 1;
2514      break;
2515    case SIZE_Nx2N:
2516      ruiPartIdxLB += ( uiPartIdx == 0 )? m_uiNumPartition >> 1 : (m_uiNumPartition >> 2)*3;
2517      break;
2518    case SIZE_NxN:
2519      ruiPartIdxLB += ( m_uiNumPartition >> 2 ) * uiPartIdx;
2520      break;
2521    case SIZE_2NxnU:
2522      ruiPartIdxLB += ( uiPartIdx == 0 ) ? -((Int)m_uiNumPartition >> 3) : m_uiNumPartition >> 1;
2523      break;
2524    case SIZE_2NxnD:
2525      ruiPartIdxLB += ( uiPartIdx == 0 ) ? (m_uiNumPartition >> 2) + (m_uiNumPartition >> 3): m_uiNumPartition >> 1;
2526      break;
2527    case SIZE_nLx2N:
2528      ruiPartIdxLB += ( uiPartIdx == 0 ) ? m_uiNumPartition >> 1 : (m_uiNumPartition >> 1) + (m_uiNumPartition >> 4);
2529      break;
2530    case SIZE_nRx2N:
2531      ruiPartIdxLB += ( uiPartIdx == 0 ) ? m_uiNumPartition >> 1 : (m_uiNumPartition >> 1) + (m_uiNumPartition >> 2) + (m_uiNumPartition >> 4);
2532      break;
2533    default:
2534      assert (0);
2535      break;
2536  }
2537}
2538
2539/** Derives the partition index of neighbouring bottom right block
2540 * \param [in]  eCUMode
2541 * \param [in]  uiPartIdx
2542 * \param [out] ruiPartIdxRB
2543 */
2544Void TComDataCU::deriveRightBottomIdx( UInt  uiPartIdx,      UInt&      ruiPartIdxRB )
2545{
2546  ruiPartIdxRB      = g_auiRasterToZscan [g_auiZscanToRaster[ m_uiAbsIdxInLCU ] + ( ((m_puhHeight[0] / m_pcPic->getMinCUHeight())>>1) - 1)*m_pcPic->getNumPartInWidth() +  m_puhWidth[0] / m_pcPic->getMinCUWidth() - 1];
2547
2548  switch ( m_pePartSize[0] )
2549  {
2550    case SIZE_2Nx2N: 
2551      ruiPartIdxRB += m_uiNumPartition >> 1;   
2552      break;
2553    case SIZE_2NxN: 
2554      ruiPartIdxRB += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 1;   
2555      break;
2556    case SIZE_Nx2N: 
2557      ruiPartIdxRB += ( uiPartIdx == 0 )? m_uiNumPartition >> 2 : (m_uiNumPartition >> 1);   
2558      break;
2559    case SIZE_NxN:   
2560      ruiPartIdxRB += ( m_uiNumPartition >> 2 ) * ( uiPartIdx - 1 );   
2561      break;
2562    case SIZE_2NxnU:
2563      ruiPartIdxRB += ( uiPartIdx == 0 ) ? -((Int)m_uiNumPartition >> 3) : m_uiNumPartition >> 1;
2564      break;
2565    case SIZE_2NxnD:
2566      ruiPartIdxRB += ( uiPartIdx == 0 ) ? (m_uiNumPartition >> 2) + (m_uiNumPartition >> 3): m_uiNumPartition >> 1;
2567      break;
2568    case SIZE_nLx2N:
2569      ruiPartIdxRB += ( uiPartIdx == 0 ) ? (m_uiNumPartition >> 3) + (m_uiNumPartition >> 4): m_uiNumPartition >> 1;
2570      break;
2571    case SIZE_nRx2N:
2572      ruiPartIdxRB += ( uiPartIdx == 0 ) ? (m_uiNumPartition >> 2) + (m_uiNumPartition >> 3) + (m_uiNumPartition >> 4) : m_uiNumPartition >> 1;
2573      break;
2574    default:
2575      assert (0);
2576      break;
2577  }
2578}
2579
2580Void TComDataCU::deriveLeftRightTopIdxAdi ( UInt& ruiPartIdxLT, UInt& ruiPartIdxRT, UInt uiPartOffset, UInt uiPartDepth )
2581{
2582  UInt uiNumPartInWidth = (m_puhWidth[0]/m_pcPic->getMinCUWidth())>>uiPartDepth;
2583  ruiPartIdxLT = m_uiAbsIdxInLCU + uiPartOffset;
2584  ruiPartIdxRT = g_auiRasterToZscan[ g_auiZscanToRaster[ ruiPartIdxLT ] + uiNumPartInWidth - 1 ];
2585}
2586
2587Void TComDataCU::deriveLeftBottomIdxAdi( UInt& ruiPartIdxLB, UInt uiPartOffset, UInt uiPartDepth )
2588{
2589  UInt uiAbsIdx;
2590  UInt uiMinCuWidth, uiWidthInMinCus;
2591 
2592  uiMinCuWidth    = getPic()->getMinCUWidth();
2593  uiWidthInMinCus = (getWidth(0)/uiMinCuWidth)>>uiPartDepth;
2594  uiAbsIdx        = getZorderIdxInCU()+uiPartOffset+(m_uiNumPartition>>(uiPartDepth<<1))-1;
2595  uiAbsIdx        = g_auiZscanToRaster[uiAbsIdx]-(uiWidthInMinCus-1);
2596  ruiPartIdxLB    = g_auiRasterToZscan[uiAbsIdx];
2597}
2598
2599Bool TComDataCU::hasEqualMotion( UInt uiAbsPartIdx, TComDataCU* pcCandCU, UInt uiCandAbsPartIdx )
2600{
2601
2602  if ( getInterDir( uiAbsPartIdx ) != pcCandCU->getInterDir( uiCandAbsPartIdx ) )
2603  {
2604    return false;
2605  }
2606
2607  for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2608  {
2609    if ( getInterDir( uiAbsPartIdx ) & ( 1 << uiRefListIdx ) )
2610    {
2611      if ( getCUMvField( RefPicList( uiRefListIdx ) )->getMv( uiAbsPartIdx )     != pcCandCU->getCUMvField( RefPicList( uiRefListIdx ) )->getMv( uiCandAbsPartIdx ) || 
2612        getCUMvField( RefPicList( uiRefListIdx ) )->getRefIdx( uiAbsPartIdx ) != pcCandCU->getCUMvField( RefPicList( uiRefListIdx ) )->getRefIdx( uiCandAbsPartIdx ) )
2613      {
2614        return false;
2615      }
2616    }
2617  }
2618
2619  return true;
2620}
2621
2622/** Constructs a list of merging candidates
2623 * \param uiAbsPartIdx
2624 * \param uiPUIdx
2625 * \param uiDepth
2626 * \param pcMvFieldNeighbours
2627 * \param puhInterDirNeighbours
2628 * \param numValidMergeCand
2629 */
2630Void TComDataCU::getInterMergeCandidates( UInt uiAbsPartIdx, UInt uiPUIdx, TComMvField* pcMvFieldNeighbours, UChar* puhInterDirNeighbours, Int& numValidMergeCand, Int mrgCandIdx )
2631{
2632  UInt uiAbsPartAddr = m_uiAbsIdxInLCU + uiAbsPartIdx;
2633  Bool abCandIsInter[ MRG_MAX_NUM_CANDS ];
2634  for( UInt ui = 0; ui < getSlice()->getMaxNumMergeCand(); ++ui )
2635  {
2636    abCandIsInter[ui] = false;
2637    pcMvFieldNeighbours[ ( ui << 1 )     ].setRefIdx(NOT_VALID);
2638    pcMvFieldNeighbours[ ( ui << 1 ) + 1 ].setRefIdx(NOT_VALID);
2639  }
2640  numValidMergeCand = getSlice()->getMaxNumMergeCand();
2641  // compute the location of the current PU
2642  Int xP, yP, nPSW, nPSH;
2643  this->getPartPosition(uiPUIdx, xP, yP, nPSW, nPSH);
2644
2645  Int iCount = 0;
2646
2647  UInt uiPartIdxLT, uiPartIdxRT, uiPartIdxLB;
2648  PartSize cCurPS = getPartitionSize( uiAbsPartIdx );
2649  deriveLeftRightTopIdxGeneral( uiAbsPartIdx, uiPUIdx, uiPartIdxLT, uiPartIdxRT );
2650  deriveLeftBottomIdxGeneral  ( uiAbsPartIdx, uiPUIdx, uiPartIdxLB );
2651
2652  //left
2653  UInt uiLeftPartIdx = 0;
2654  TComDataCU* pcCULeft = 0;
2655  pcCULeft = getPULeft( uiLeftPartIdx, uiPartIdxLB );
2656  Bool isAvailableA1 = pcCULeft &&
2657  pcCULeft->isDiffMER(xP -1, yP+nPSH-1, xP, yP) &&
2658  !( uiPUIdx == 1 && (cCurPS == SIZE_Nx2N || cCurPS == SIZE_nLx2N || cCurPS == SIZE_nRx2N) ) &&
2659  !pcCULeft->isIntra( uiLeftPartIdx ) ;
2660  if ( isAvailableA1 )
2661  {
2662    abCandIsInter[iCount] = true;
2663    // get Inter Dir
2664    puhInterDirNeighbours[iCount] = pcCULeft->getInterDir( uiLeftPartIdx );
2665    // get Mv from Left
2666    pcCULeft->getMvField( pcCULeft, uiLeftPartIdx, REF_PIC_LIST_0, pcMvFieldNeighbours[iCount<<1] );
2667    if ( getSlice()->isInterB() )
2668    {
2669      pcCULeft->getMvField( pcCULeft, uiLeftPartIdx, REF_PIC_LIST_1, pcMvFieldNeighbours[(iCount<<1)+1] );
2670    }
2671    if ( mrgCandIdx == iCount )
2672    {
2673      return;
2674    }
2675    iCount ++;
2676  }
2677 
2678  // early termination
2679  if (iCount == getSlice()->getMaxNumMergeCand()) 
2680  {
2681    return;
2682  }
2683  // above
2684  UInt uiAbovePartIdx = 0;
2685  TComDataCU* pcCUAbove = 0;
2686  pcCUAbove = getPUAbove( uiAbovePartIdx, uiPartIdxRT );
2687  Bool isAvailableB1 = pcCUAbove &&
2688  pcCUAbove->isDiffMER(xP+nPSW-1, yP-1, xP, yP) &&
2689  !( uiPUIdx == 1 && (cCurPS == SIZE_2NxN || cCurPS == SIZE_2NxnU || cCurPS == SIZE_2NxnD) ) &&
2690  !pcCUAbove->isIntra( uiAbovePartIdx );
2691  if ( isAvailableB1 && (!isAvailableA1 || !pcCULeft->hasEqualMotion( uiLeftPartIdx, pcCUAbove, uiAbovePartIdx ) ) )
2692  {
2693    abCandIsInter[iCount] = true;
2694    // get Inter Dir
2695    puhInterDirNeighbours[iCount] = pcCUAbove->getInterDir( uiAbovePartIdx );
2696    // get Mv from Left
2697    pcCUAbove->getMvField( pcCUAbove, uiAbovePartIdx, REF_PIC_LIST_0, pcMvFieldNeighbours[iCount<<1] );
2698    if ( getSlice()->isInterB() )
2699    {
2700      pcCUAbove->getMvField( pcCUAbove, uiAbovePartIdx, REF_PIC_LIST_1, pcMvFieldNeighbours[(iCount<<1)+1] );
2701    }
2702    if ( mrgCandIdx == iCount )
2703    {
2704      return;
2705    }
2706    iCount ++;
2707  }
2708  // early termination
2709  if (iCount == getSlice()->getMaxNumMergeCand()) 
2710  {
2711    return;
2712  }
2713
2714  // above right
2715  UInt uiAboveRightPartIdx = 0;
2716  TComDataCU* pcCUAboveRight = 0;
2717  pcCUAboveRight = getPUAboveRight( uiAboveRightPartIdx, uiPartIdxRT );
2718  Bool isAvailableB0 = pcCUAboveRight &&
2719  pcCUAboveRight->isDiffMER(xP+nPSW, yP-1, xP, yP) &&
2720  !pcCUAboveRight->isIntra( uiAboveRightPartIdx );
2721  if ( isAvailableB0 && ( !isAvailableB1 || !pcCUAbove->hasEqualMotion( uiAbovePartIdx, pcCUAboveRight, uiAboveRightPartIdx ) ) )
2722  {
2723    abCandIsInter[iCount] = true;
2724    // get Inter Dir
2725    puhInterDirNeighbours[iCount] = pcCUAboveRight->getInterDir( uiAboveRightPartIdx );
2726    // get Mv from Left
2727    pcCUAboveRight->getMvField( pcCUAboveRight, uiAboveRightPartIdx, REF_PIC_LIST_0, pcMvFieldNeighbours[iCount<<1] );
2728    if ( getSlice()->isInterB() )
2729    {
2730      pcCUAboveRight->getMvField( pcCUAboveRight, uiAboveRightPartIdx, REF_PIC_LIST_1, pcMvFieldNeighbours[(iCount<<1)+1] );
2731    }
2732    if ( mrgCandIdx == iCount )
2733    {
2734      return;
2735    }
2736    iCount ++;
2737  }
2738  // early termination
2739  if (iCount == getSlice()->getMaxNumMergeCand()) 
2740  {
2741    return;
2742  }
2743
2744  //left bottom
2745  UInt uiLeftBottomPartIdx = 0;
2746  TComDataCU* pcCULeftBottom = 0;
2747  pcCULeftBottom = this->getPUBelowLeft( uiLeftBottomPartIdx, uiPartIdxLB );
2748  Bool isAvailableA0 = pcCULeftBottom &&
2749  pcCULeftBottom->isDiffMER(xP-1, yP+nPSH, xP, yP) &&
2750  !pcCULeftBottom->isIntra( uiLeftBottomPartIdx ) ;
2751  if ( isAvailableA0 && ( !isAvailableA1 || !pcCULeft->hasEqualMotion( uiLeftPartIdx, pcCULeftBottom, uiLeftBottomPartIdx ) ) )
2752  {
2753    abCandIsInter[iCount] = true;
2754    // get Inter Dir
2755    puhInterDirNeighbours[iCount] = pcCULeftBottom->getInterDir( uiLeftBottomPartIdx );
2756    // get Mv from Left
2757    pcCULeftBottom->getMvField( pcCULeftBottom, uiLeftBottomPartIdx, REF_PIC_LIST_0, pcMvFieldNeighbours[iCount<<1] );
2758    if ( getSlice()->isInterB() )
2759    {
2760      pcCULeftBottom->getMvField( pcCULeftBottom, uiLeftBottomPartIdx, REF_PIC_LIST_1, pcMvFieldNeighbours[(iCount<<1)+1] );
2761    }
2762    if ( mrgCandIdx == iCount )
2763    {
2764      return;
2765    }
2766    iCount ++;
2767  }
2768  // early termination
2769  if (iCount == getSlice()->getMaxNumMergeCand()) 
2770  {
2771    return;
2772  }
2773  // above left
2774  if( iCount < 4 )
2775  {
2776    UInt uiAboveLeftPartIdx = 0;
2777    TComDataCU* pcCUAboveLeft = 0;
2778    pcCUAboveLeft = getPUAboveLeft( uiAboveLeftPartIdx, uiAbsPartAddr );
2779    Bool isAvailableB2 = pcCUAboveLeft &&
2780    pcCUAboveLeft->isDiffMER(xP-1, yP-1, xP, yP) &&
2781    !pcCUAboveLeft->isIntra( uiAboveLeftPartIdx );
2782    if ( isAvailableB2 && ( !isAvailableA1 || !pcCULeft->hasEqualMotion( uiLeftPartIdx, pcCUAboveLeft, uiAboveLeftPartIdx ) )
2783        && ( !isAvailableB1 || !pcCUAbove->hasEqualMotion( uiAbovePartIdx, pcCUAboveLeft, uiAboveLeftPartIdx ) ) )
2784    {
2785      abCandIsInter[iCount] = true;
2786      // get Inter Dir
2787      puhInterDirNeighbours[iCount] = pcCUAboveLeft->getInterDir( uiAboveLeftPartIdx );
2788      // get Mv from Left
2789      pcCUAboveLeft->getMvField( pcCUAboveLeft, uiAboveLeftPartIdx, REF_PIC_LIST_0, pcMvFieldNeighbours[iCount<<1] );
2790      if ( getSlice()->isInterB() )
2791      {
2792        pcCUAboveLeft->getMvField( pcCUAboveLeft, uiAboveLeftPartIdx, REF_PIC_LIST_1, pcMvFieldNeighbours[(iCount<<1)+1] );
2793      }
2794      if ( mrgCandIdx == iCount )
2795      {
2796        return;
2797      }
2798      iCount ++;
2799    }
2800  }
2801  // early termination
2802  if (iCount == getSlice()->getMaxNumMergeCand()) 
2803  {
2804    return;
2805  }
2806  if ( getSlice()->getEnableTMVPFlag())
2807  {
2808    //>> MTK colocated-RightBottom
2809    UInt uiPartIdxRB;
2810    Int uiLCUIdx = getAddr();
2811
2812    deriveRightBottomIdx( uiPUIdx, uiPartIdxRB ); 
2813
2814    UInt uiAbsPartIdxTmp = g_auiZscanToRaster[uiPartIdxRB];
2815    UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
2816
2817    TComMv cColMv;
2818    Int iRefIdx;
2819
2820    if      ( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxTmp] + m_pcPic->getMinCUWidth() ) >= m_pcSlice->getSPS()->getPicWidthInLumaSamples() )  // image boundary check
2821    {
2822      uiLCUIdx = -1;
2823    }
2824    else if ( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxTmp] + m_pcPic->getMinCUHeight() ) >= m_pcSlice->getSPS()->getPicHeightInLumaSamples() )
2825    {
2826      uiLCUIdx = -1;
2827    }
2828    else
2829    {
2830      if ( ( uiAbsPartIdxTmp % uiNumPartInCUWidth < uiNumPartInCUWidth - 1 ) &&           // is not at the last column of LCU
2831        ( uiAbsPartIdxTmp / uiNumPartInCUWidth < m_pcPic->getNumPartInHeight() - 1 ) ) // is not at the last row    of LCU
2832      {
2833        uiAbsPartAddr = g_auiRasterToZscan[ uiAbsPartIdxTmp + uiNumPartInCUWidth + 1 ];
2834        uiLCUIdx = getAddr();
2835      }
2836      else if ( uiAbsPartIdxTmp % uiNumPartInCUWidth < uiNumPartInCUWidth - 1 )           // is not at the last column of LCU But is last row of LCU
2837      {
2838        uiAbsPartAddr = g_auiRasterToZscan[ (uiAbsPartIdxTmp + uiNumPartInCUWidth + 1) % m_pcPic->getNumPartInCU() ];
2839        uiLCUIdx = -1 ; 
2840      }
2841      else if ( uiAbsPartIdxTmp / uiNumPartInCUWidth < m_pcPic->getNumPartInHeight() - 1 ) // is not at the last row of LCU But is last column of LCU
2842      {
2843        uiAbsPartAddr = g_auiRasterToZscan[ uiAbsPartIdxTmp + 1 ];
2844        uiLCUIdx = getAddr() + 1;
2845      }
2846      else //is the right bottom corner of LCU                       
2847      {
2848        uiAbsPartAddr = 0;
2849        uiLCUIdx = -1 ; 
2850      }
2851    }
2852   
2853   
2854    iRefIdx = 0;
2855    Bool bExistMV = false;
2856    UInt uiPartIdxCenter;
2857    UInt uiCurLCUIdx = getAddr();
2858    Int dir = 0;
2859    UInt uiArrayAddr = iCount;
2860    xDeriveCenterIdx( uiPUIdx, uiPartIdxCenter );
2861    bExistMV = uiLCUIdx >= 0 && xGetColMVP( REF_PIC_LIST_0, uiLCUIdx, uiAbsPartAddr, cColMv, iRefIdx );
2862    if( bExistMV == false )
2863    {
2864      bExistMV = xGetColMVP( REF_PIC_LIST_0, uiCurLCUIdx, uiPartIdxCenter, cColMv, iRefIdx );
2865    }
2866    if( bExistMV )
2867    {
2868      dir |= 1;
2869      pcMvFieldNeighbours[ 2 * uiArrayAddr ].setMvField( cColMv, iRefIdx );
2870    }
2871   
2872    if ( getSlice()->isInterB() )
2873    {
2874      bExistMV = uiLCUIdx >= 0 && xGetColMVP( REF_PIC_LIST_1, uiLCUIdx, uiAbsPartAddr, cColMv, iRefIdx);
2875      if( bExistMV == false )
2876      {
2877        bExistMV = xGetColMVP( REF_PIC_LIST_1, uiCurLCUIdx, uiPartIdxCenter, cColMv, iRefIdx );
2878      }
2879      if( bExistMV )
2880      {
2881        dir |= 2;
2882        pcMvFieldNeighbours[ 2 * uiArrayAddr + 1 ].setMvField( cColMv, iRefIdx );
2883      }
2884    }
2885   
2886    if (dir != 0)
2887    {
2888      puhInterDirNeighbours[uiArrayAddr] = dir;
2889      abCandIsInter[uiArrayAddr] = true;
2890
2891      if ( mrgCandIdx == iCount )
2892      {
2893        return;
2894      }
2895      iCount++;
2896    }
2897  }
2898  // early termination
2899  if (iCount == getSlice()->getMaxNumMergeCand()) 
2900  {
2901    return;
2902  }
2903  UInt uiArrayAddr = iCount;
2904  UInt uiCutoff = uiArrayAddr;
2905   
2906  if ( getSlice()->isInterB())
2907  {
2908    UInt uiPriorityList0[12] = {0 , 1, 0, 2, 1, 2, 0, 3, 1, 3, 2, 3};
2909    UInt uiPriorityList1[12] = {1 , 0, 2, 0, 2, 1, 3, 0, 3, 1, 3, 2};
2910
2911    for (Int idx=0; idx<uiCutoff*(uiCutoff-1) && uiArrayAddr!= getSlice()->getMaxNumMergeCand(); idx++)
2912    {
2913      Int i = uiPriorityList0[idx]; Int j = uiPriorityList1[idx];
2914      if (abCandIsInter[i] && abCandIsInter[j]&& (puhInterDirNeighbours[i]&0x1)&&(puhInterDirNeighbours[j]&0x2))
2915      {
2916        abCandIsInter[uiArrayAddr] = true;
2917        puhInterDirNeighbours[uiArrayAddr] = 3;
2918
2919        // get Mv from cand[i] and cand[j]
2920        pcMvFieldNeighbours[uiArrayAddr << 1].setMvField(pcMvFieldNeighbours[i<<1].getMv(), pcMvFieldNeighbours[i<<1].getRefIdx());
2921        pcMvFieldNeighbours[( uiArrayAddr << 1 ) + 1].setMvField(pcMvFieldNeighbours[(j<<1)+1].getMv(), pcMvFieldNeighbours[(j<<1)+1].getRefIdx());
2922
2923        Int iRefPOCL0 = m_pcSlice->getRefPOC( REF_PIC_LIST_0, pcMvFieldNeighbours[(uiArrayAddr<<1)].getRefIdx() );
2924        Int iRefPOCL1 = m_pcSlice->getRefPOC( REF_PIC_LIST_1, pcMvFieldNeighbours[(uiArrayAddr<<1)+1].getRefIdx() );
2925        if (iRefPOCL0 == iRefPOCL1 && pcMvFieldNeighbours[(uiArrayAddr<<1)].getMv() == pcMvFieldNeighbours[(uiArrayAddr<<1)+1].getMv())
2926        {
2927          abCandIsInter[uiArrayAddr] = false;
2928        }
2929        else
2930        {
2931          uiArrayAddr++;
2932        }
2933      }
2934    }
2935  }
2936  // early termination
2937  if (uiArrayAddr == getSlice()->getMaxNumMergeCand()) 
2938  {
2939    return;
2940  }
2941  Int iNumRefIdx = (getSlice()->isInterB()) ? min(m_pcSlice->getNumRefIdx(REF_PIC_LIST_0), m_pcSlice->getNumRefIdx(REF_PIC_LIST_1)) : m_pcSlice->getNumRefIdx(REF_PIC_LIST_0);
2942  Int r = 0;
2943  Int refcnt = 0;
2944  while (uiArrayAddr < getSlice()->getMaxNumMergeCand())
2945  {
2946    abCandIsInter[uiArrayAddr] = true;
2947    puhInterDirNeighbours[uiArrayAddr] = 1;
2948    pcMvFieldNeighbours[uiArrayAddr << 1].setMvField( TComMv(0, 0), r);
2949
2950    if ( getSlice()->isInterB() )
2951    {
2952      puhInterDirNeighbours[uiArrayAddr] = 3;
2953      pcMvFieldNeighbours[(uiArrayAddr << 1) + 1].setMvField(TComMv(0, 0), r);
2954    }
2955    uiArrayAddr++;
2956    if ( refcnt == iNumRefIdx - 1 )
2957    {
2958      r = 0;
2959    }
2960    else
2961    {
2962      ++r;
2963      ++refcnt;
2964    }
2965  }
2966
2967  numValidMergeCand = uiArrayAddr;
2968}
2969
2970/** Check whether the current PU and a spatial neighboring PU are in a same ME region.
2971 * \param xN, xN   location of the upper-left corner pixel of a neighboring PU
2972 * \param xP, yP   location of the upper-left corner pixel of the current PU
2973 * \returns Bool
2974 */
2975Bool TComDataCU::isDiffMER(Int xN, Int yN, Int xP, Int yP)
2976{
2977
2978  UInt plevel = this->getSlice()->getPPS()->getLog2ParallelMergeLevelMinus2() + 2;
2979  if ((xN>>plevel)!= (xP>>plevel))
2980  {
2981    return true;
2982  }
2983  if ((yN>>plevel)!= (yP>>plevel))
2984  {
2985    return true;
2986  }
2987  return false;
2988}
2989/** calculate the location of upper-left corner pixel and size of the current PU.
2990 * \param partIdx  PU index within a CU
2991 * \param xP, yP   location of the upper-left corner pixel of the current PU
2992 * \param PSW, nPSH    size of the curren PU
2993 * \returns Void
2994 */
2995Void TComDataCU::getPartPosition( UInt partIdx, Int& xP, Int& yP, Int& nPSW, Int& nPSH)
2996{
2997  UInt col = m_uiCUPelX;
2998  UInt row = m_uiCUPelY;
2999
3000  switch ( m_pePartSize[0] )
3001  {
3002  case SIZE_2NxN:
3003    nPSW = getWidth(0);     
3004    nPSH = getHeight(0) >> 1; 
3005    xP   = col;
3006    yP   = (partIdx ==0)? row: row + nPSH;
3007    break;
3008  case SIZE_Nx2N:
3009    nPSW = getWidth(0) >> 1; 
3010    nPSH = getHeight(0);     
3011    xP   = (partIdx ==0)? col: col + nPSW;
3012    yP   = row;
3013    break;
3014  case SIZE_NxN:
3015    nPSW = getWidth(0) >> 1; 
3016    nPSH = getHeight(0) >> 1; 
3017    xP   = col + (partIdx&0x1)*nPSW;
3018    yP   = row + (partIdx>>1)*nPSH;
3019    break;
3020  case SIZE_2NxnU:
3021    nPSW = getWidth(0);
3022    nPSH = ( partIdx == 0 ) ?  getHeight(0) >> 2 : ( getHeight(0) >> 2 ) + ( getHeight(0) >> 1 );
3023    xP   = col;
3024    yP   = (partIdx ==0)? row: row + getHeight(0) - nPSH;
3025
3026    break;
3027  case SIZE_2NxnD:
3028    nPSW = getWidth(0);
3029    nPSH = ( partIdx == 0 ) ?  ( getHeight(0) >> 2 ) + ( getHeight(0) >> 1 ) : getHeight(0) >> 2;
3030    xP   = col;
3031    yP   = (partIdx ==0)? row: row + getHeight(0) - nPSH;
3032    break;
3033  case SIZE_nLx2N:
3034    nPSW = ( partIdx == 0 ) ? getWidth(0) >> 2 : ( getWidth(0) >> 2 ) + ( getWidth(0) >> 1 );
3035    nPSH = getHeight(0);
3036    xP   = (partIdx ==0)? col: col + getWidth(0) - nPSW;
3037    yP   = row;
3038    break;
3039  case SIZE_nRx2N:
3040    nPSW = ( partIdx == 0 ) ? ( getWidth(0) >> 2 ) + ( getWidth(0) >> 1 ) : getWidth(0) >> 2;
3041    nPSH = getHeight(0);
3042    xP   = (partIdx ==0)? col: col + getWidth(0) - nPSW;
3043    yP   = row;
3044    break;
3045  default:
3046    assert ( m_pePartSize[0] == SIZE_2Nx2N );
3047    nPSW = getWidth(0);     
3048    nPSH = getHeight(0);     
3049    xP   = col ;
3050    yP   = row ;
3051
3052    break;
3053  }
3054}
3055
3056/** Constructs a list of candidates for AMVP
3057 * \param uiPartIdx
3058 * \param uiPartAddr
3059 * \param eRefPicList
3060 * \param iRefIdx
3061 * \param pInfo
3062 */
3063Void TComDataCU::fillMvpCand ( UInt uiPartIdx, UInt uiPartAddr, RefPicList eRefPicList, Int iRefIdx, AMVPInfo* pInfo )
3064{
3065  TComMv cMvPred;
3066  Bool bAddedSmvp = false;
3067
3068  pInfo->iN = 0; 
3069  if (iRefIdx < 0)
3070  {
3071    return;
3072  }
3073 
3074  //-- Get Spatial MV
3075  UInt uiPartIdxLT, uiPartIdxRT, uiPartIdxLB;
3076  UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
3077  Bool bAdded = false;
3078 
3079  deriveLeftRightTopIdx( uiPartIdx, uiPartIdxLT, uiPartIdxRT );
3080  deriveLeftBottomIdx( uiPartIdx, uiPartIdxLB );
3081 
3082  TComDataCU* tmpCU = NULL;
3083  UInt idx;
3084  tmpCU = getPUBelowLeft(idx, uiPartIdxLB);
3085  bAddedSmvp = (tmpCU != NULL) && (tmpCU->getPredictionMode(idx) != MODE_INTRA);
3086
3087  if (!bAddedSmvp)
3088  {
3089    tmpCU = getPULeft(idx, uiPartIdxLB);
3090    bAddedSmvp = (tmpCU != NULL) && (tmpCU->getPredictionMode(idx) != MODE_INTRA);
3091  }
3092
3093  // Left predictor search
3094  bAdded = xAddMVPCand( pInfo, eRefPicList, iRefIdx, uiPartIdxLB, MD_BELOW_LEFT);
3095  if (!bAdded) 
3096  {
3097    bAdded = xAddMVPCand( pInfo, eRefPicList, iRefIdx, uiPartIdxLB, MD_LEFT );
3098  }
3099 
3100  if(!bAdded)
3101  {
3102    bAdded = xAddMVPCandOrder( pInfo, eRefPicList, iRefIdx, uiPartIdxLB, MD_BELOW_LEFT);
3103    if (!bAdded) 
3104    {
3105      bAdded = xAddMVPCandOrder( pInfo, eRefPicList, iRefIdx, uiPartIdxLB, MD_LEFT );
3106    }
3107  }
3108  // Above predictor search
3109  bAdded = xAddMVPCand( pInfo, eRefPicList, iRefIdx, uiPartIdxRT, MD_ABOVE_RIGHT);
3110
3111  if (!bAdded) 
3112  {
3113    bAdded = xAddMVPCand( pInfo, eRefPicList, iRefIdx, uiPartIdxRT, MD_ABOVE);
3114  }
3115
3116  if(!bAdded)
3117  {
3118    bAdded = xAddMVPCand( pInfo, eRefPicList, iRefIdx, uiPartIdxLT, MD_ABOVE_LEFT);
3119  }
3120  bAdded = bAddedSmvp;
3121  if (pInfo->iN==2) bAdded = true;
3122
3123  if(!bAdded)
3124  {
3125    bAdded = xAddMVPCandOrder( pInfo, eRefPicList, iRefIdx, uiPartIdxRT, MD_ABOVE_RIGHT);
3126    if (!bAdded) 
3127    {
3128      bAdded = xAddMVPCandOrder( pInfo, eRefPicList, iRefIdx, uiPartIdxRT, MD_ABOVE);
3129    }
3130
3131    if(!bAdded)
3132    {
3133      bAdded = xAddMVPCandOrder( pInfo, eRefPicList, iRefIdx, uiPartIdxLT, MD_ABOVE_LEFT);
3134    }
3135  }
3136 
3137  if ( pInfo->iN == 2 )
3138  {
3139    if ( pInfo->m_acMvCand[ 0 ] == pInfo->m_acMvCand[ 1 ] )
3140    {
3141      pInfo->iN = 1;
3142    }
3143  }
3144
3145  if ( getSlice()->getEnableTMVPFlag() )
3146  {
3147    // Get Temporal Motion Predictor
3148    Int iRefIdx_Col = iRefIdx;
3149    TComMv cColMv;
3150    UInt uiPartIdxRB;
3151    UInt uiAbsPartIdx; 
3152    UInt uiAbsPartAddr;
3153    Int uiLCUIdx = getAddr();
3154
3155    deriveRightBottomIdx( uiPartIdx, uiPartIdxRB );
3156    uiAbsPartAddr = m_uiAbsIdxInLCU + uiPartAddr;
3157
3158    //----  co-located RightBottom Temporal Predictor (H) ---//
3159    uiAbsPartIdx = g_auiZscanToRaster[uiPartIdxRB];
3160    if ( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdx] + m_pcPic->getMinCUWidth() ) >= m_pcSlice->getSPS()->getPicWidthInLumaSamples() )  // image boundary check
3161    {
3162      uiLCUIdx = -1;
3163    }
3164    else if ( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdx] + m_pcPic->getMinCUHeight() ) >= m_pcSlice->getSPS()->getPicHeightInLumaSamples() )
3165    {
3166      uiLCUIdx = -1;
3167    }
3168    else
3169    {
3170      if ( ( uiAbsPartIdx % uiNumPartInCUWidth < uiNumPartInCUWidth - 1 ) &&           // is not at the last column of LCU
3171        ( uiAbsPartIdx / uiNumPartInCUWidth < m_pcPic->getNumPartInHeight() - 1 ) ) // is not at the last row    of LCU
3172      {
3173        uiAbsPartAddr = g_auiRasterToZscan[ uiAbsPartIdx + uiNumPartInCUWidth + 1 ];
3174        uiLCUIdx = getAddr();
3175      }
3176      else if ( uiAbsPartIdx % uiNumPartInCUWidth < uiNumPartInCUWidth - 1 )           // is not at the last column of LCU But is last row of LCU
3177      {
3178        uiAbsPartAddr = g_auiRasterToZscan[ (uiAbsPartIdx + uiNumPartInCUWidth + 1) % m_pcPic->getNumPartInCU() ];
3179        uiLCUIdx      = -1 ; 
3180      }
3181      else if ( uiAbsPartIdx / uiNumPartInCUWidth < m_pcPic->getNumPartInHeight() - 1 ) // is not at the last row of LCU But is last column of LCU
3182      {
3183        uiAbsPartAddr = g_auiRasterToZscan[ uiAbsPartIdx + 1 ];
3184        uiLCUIdx = getAddr() + 1;
3185      }
3186      else //is the right bottom corner of LCU                       
3187      {
3188        uiAbsPartAddr = 0;
3189        uiLCUIdx      = -1 ; 
3190      }
3191    }
3192    if ( uiLCUIdx >= 0 && xGetColMVP( eRefPicList, uiLCUIdx, uiAbsPartAddr, cColMv, iRefIdx_Col ) )
3193    {
3194      pInfo->m_acMvCand[pInfo->iN++] = cColMv;
3195    }
3196    else 
3197    {
3198      UInt uiPartIdxCenter;
3199      UInt uiCurLCUIdx = getAddr();
3200      xDeriveCenterIdx( uiPartIdx, uiPartIdxCenter );
3201      if (xGetColMVP( eRefPicList, uiCurLCUIdx, uiPartIdxCenter,  cColMv, iRefIdx_Col ))
3202      {
3203        pInfo->m_acMvCand[pInfo->iN++] = cColMv;
3204      }
3205    }
3206    //----  co-located RightBottom Temporal Predictor  ---//
3207  }
3208
3209  if (pInfo->iN > AMVP_MAX_NUM_CANDS)
3210  {
3211    pInfo->iN = AMVP_MAX_NUM_CANDS;
3212  }
3213  while (pInfo->iN < AMVP_MAX_NUM_CANDS)
3214  {
3215      pInfo->m_acMvCand[pInfo->iN].set(0,0);
3216      pInfo->iN++;
3217  }
3218  return ;
3219}
3220
3221Bool TComDataCU::isBipredRestriction(UInt puIdx)
3222{
3223  Int width = 0;
3224  Int height = 0;
3225  UInt partAddr;
3226
3227  getPartIndexAndSize( puIdx, partAddr, width, height );
3228  if ( getWidth(0) == 8 && (width < 8 || height < 8) )
3229  {
3230    return true;
3231  }
3232  return false;
3233}
3234
3235Void TComDataCU::clipMv    (TComMv&  rcMv)
3236{
3237  Int  iMvShift = 2;
3238  Int iOffset = 8;
3239  Int iHorMax = ( m_pcSlice->getSPS()->getPicWidthInLumaSamples() + iOffset - m_uiCUPelX - 1 ) << iMvShift;
3240  Int iHorMin = (       -(Int)g_uiMaxCUWidth - iOffset - (Int)m_uiCUPelX + 1 ) << iMvShift;
3241 
3242  Int iVerMax = ( m_pcSlice->getSPS()->getPicHeightInLumaSamples() + iOffset - m_uiCUPelY - 1 ) << iMvShift;
3243  Int iVerMin = (       -(Int)g_uiMaxCUHeight - iOffset - (Int)m_uiCUPelY + 1 ) << iMvShift;
3244 
3245  rcMv.setHor( min (iHorMax, max (iHorMin, rcMv.getHor())) );
3246  rcMv.setVer( min (iVerMax, max (iVerMin, rcMv.getVer())) );
3247}
3248
3249UInt TComDataCU::getIntraSizeIdx(UInt uiAbsPartIdx)
3250{
3251  UInt uiShift = ( (m_puhTrIdx[uiAbsPartIdx]==0) && (m_pePartSize[uiAbsPartIdx]==SIZE_NxN) ) ? m_puhTrIdx[uiAbsPartIdx]+1 : m_puhTrIdx[uiAbsPartIdx];
3252  uiShift = ( m_pePartSize[uiAbsPartIdx]==SIZE_NxN ? 1 : 0 );
3253 
3254  UChar uiWidth = m_puhWidth[uiAbsPartIdx]>>uiShift;
3255  UInt  uiCnt = 0;
3256  while( uiWidth )
3257  {
3258    uiCnt++;
3259    uiWidth>>=1;
3260  }
3261  uiCnt-=2;
3262  return uiCnt > 6 ? 6 : uiCnt;
3263}
3264
3265Void TComDataCU::clearCbf( UInt uiIdx, TextType eType, UInt uiNumParts )
3266{
3267  ::memset( &m_puhCbf[g_aucConvertTxtTypeToIdx[eType]][uiIdx], 0, sizeof(UChar)*uiNumParts);
3268}
3269
3270/** Set a I_PCM flag for all sub-partitions of a partition.
3271 * \param bIpcmFlag I_PCM flag
3272 * \param uiAbsPartIdx patition index
3273 * \param uiDepth CU depth
3274 * \returns Void
3275 */
3276Void TComDataCU::setIPCMFlagSubParts  (Bool bIpcmFlag, UInt uiAbsPartIdx, UInt uiDepth)
3277{
3278  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
3279
3280  memset(m_pbIPCMFlag + uiAbsPartIdx, bIpcmFlag, sizeof(Bool)*uiCurrPartNumb );
3281}
3282
3283/** Test whether the current block is skipped
3284 * \param uiPartIdx Block index
3285 * \returns Flag indicating whether the block is skipped
3286 */
3287Bool TComDataCU::isSkipped( UInt uiPartIdx )
3288{
3289  return ( getSkipFlag( uiPartIdx ) );
3290}
3291
3292// ====================================================================================================================
3293// Protected member functions
3294// ====================================================================================================================
3295
3296Bool TComDataCU::xAddMVPCand( AMVPInfo* pInfo, RefPicList eRefPicList, Int iRefIdx, UInt uiPartUnitIdx, MVP_DIR eDir )
3297{
3298  TComDataCU* pcTmpCU = NULL;
3299  UInt uiIdx;
3300  switch( eDir )
3301  {
3302    case MD_LEFT:
3303    {
3304      pcTmpCU = getPULeft(uiIdx, uiPartUnitIdx);
3305      break;
3306    }
3307    case MD_ABOVE:
3308    {
3309      pcTmpCU = getPUAbove(uiIdx, uiPartUnitIdx );
3310      break;
3311    }
3312    case MD_ABOVE_RIGHT:
3313    {
3314      pcTmpCU = getPUAboveRight(uiIdx, uiPartUnitIdx);
3315      break;
3316    }
3317    case MD_BELOW_LEFT:
3318    {
3319      pcTmpCU = getPUBelowLeft(uiIdx, uiPartUnitIdx);
3320      break;
3321    }
3322    case MD_ABOVE_LEFT:
3323    {
3324      pcTmpCU = getPUAboveLeft(uiIdx, uiPartUnitIdx);
3325      break;
3326    }
3327    default:
3328    {
3329      break;
3330    }
3331  }
3332
3333  if ( pcTmpCU == NULL )
3334  {
3335    return false;
3336  }
3337 
3338#if L0363_MVP_POC
3339  if ( pcTmpCU->getCUMvField(eRefPicList)->getRefIdx(uiIdx) >= 0 && m_pcSlice->getRefPic( eRefPicList, iRefIdx)->getPOC() == pcTmpCU->getSlice()->getRefPOC( eRefPicList, pcTmpCU->getCUMvField(eRefPicList)->getRefIdx(uiIdx) ))
3340#else
3341  if ( m_pcSlice->isEqualRef(eRefPicList, pcTmpCU->getCUMvField(eRefPicList)->getRefIdx(uiIdx), iRefIdx) )
3342#endif
3343  {
3344    TComMv cMvPred = pcTmpCU->getCUMvField(eRefPicList)->getMv(uiIdx);
3345   
3346    pInfo->m_acMvCand[ pInfo->iN++] = cMvPred;
3347    return true;
3348  }
3349
3350  RefPicList eRefPicList2nd = REF_PIC_LIST_0;
3351  if(       eRefPicList == REF_PIC_LIST_0 )
3352  {
3353    eRefPicList2nd = REF_PIC_LIST_1;
3354  }
3355  else if ( eRefPicList == REF_PIC_LIST_1)
3356  {
3357    eRefPicList2nd = REF_PIC_LIST_0;
3358  }
3359
3360
3361  Int iCurrRefPOC = m_pcSlice->getRefPic( eRefPicList, iRefIdx)->getPOC();
3362  Int iNeibRefPOC;
3363
3364
3365  if( pcTmpCU->getCUMvField(eRefPicList2nd)->getRefIdx(uiIdx) >= 0 )
3366  {
3367    iNeibRefPOC = pcTmpCU->getSlice()->getRefPOC( eRefPicList2nd, pcTmpCU->getCUMvField(eRefPicList2nd)->getRefIdx(uiIdx) );
3368    if( iNeibRefPOC == iCurrRefPOC ) // Same Reference Frame But Diff List//
3369    {
3370      TComMv cMvPred = pcTmpCU->getCUMvField(eRefPicList2nd)->getMv(uiIdx);
3371      pInfo->m_acMvCand[ pInfo->iN++] = cMvPred;
3372      return true;
3373    }
3374  }
3375  return false;
3376}
3377
3378/**
3379 * \param pInfo
3380 * \param eRefPicList
3381 * \param iRefIdx
3382 * \param uiPartUnitIdx
3383 * \param eDir
3384 * \returns Bool
3385 */
3386Bool TComDataCU::xAddMVPCandOrder( AMVPInfo* pInfo, RefPicList eRefPicList, Int iRefIdx, UInt uiPartUnitIdx, MVP_DIR eDir )
3387{
3388  TComDataCU* pcTmpCU = NULL;
3389  UInt uiIdx;
3390  switch( eDir )
3391  {
3392  case MD_LEFT:
3393    {
3394      pcTmpCU = getPULeft(uiIdx, uiPartUnitIdx);
3395      break;
3396    }
3397  case MD_ABOVE:
3398    {
3399      pcTmpCU = getPUAbove(uiIdx, uiPartUnitIdx);
3400      break;
3401    }
3402  case MD_ABOVE_RIGHT:
3403    {
3404      pcTmpCU = getPUAboveRight(uiIdx, uiPartUnitIdx);
3405      break;
3406    }
3407  case MD_BELOW_LEFT:
3408    {
3409      pcTmpCU = getPUBelowLeft(uiIdx, uiPartUnitIdx);
3410      break;
3411    }
3412  case MD_ABOVE_LEFT:
3413    {
3414      pcTmpCU = getPUAboveLeft(uiIdx, uiPartUnitIdx);
3415      break;
3416    }
3417  default:
3418    {
3419      break;
3420    }
3421  }
3422
3423  if ( pcTmpCU == NULL ) 
3424  {
3425    return false;
3426  }
3427 
3428  RefPicList eRefPicList2nd = REF_PIC_LIST_0;
3429  if(       eRefPicList == REF_PIC_LIST_0 )
3430  {
3431    eRefPicList2nd = REF_PIC_LIST_1;
3432  }
3433  else if ( eRefPicList == REF_PIC_LIST_1)
3434  {
3435    eRefPicList2nd = REF_PIC_LIST_0;
3436  }
3437
3438  Int iCurrPOC = m_pcSlice->getPOC();
3439  Int iCurrRefPOC = m_pcSlice->getRefPic( eRefPicList, iRefIdx)->getPOC();
3440  Int iNeibPOC = iCurrPOC;
3441  Int iNeibRefPOC;
3442
3443  Bool bIsCurrRefLongTerm = m_pcSlice->getRefPic( eRefPicList, iRefIdx)->getIsLongTerm();
3444  Bool bIsNeibRefLongTerm = false;
3445  //---------------  V1 (END) ------------------//
3446  if( pcTmpCU->getCUMvField(eRefPicList)->getRefIdx(uiIdx) >= 0)
3447  {
3448    iNeibRefPOC = pcTmpCU->getSlice()->getRefPOC( eRefPicList, pcTmpCU->getCUMvField(eRefPicList)->getRefIdx(uiIdx) );
3449    TComMv cMvPred = pcTmpCU->getCUMvField(eRefPicList)->getMv(uiIdx);
3450    TComMv rcMv;
3451
3452    bIsNeibRefLongTerm = pcTmpCU->getSlice()->getRefPic( eRefPicList, pcTmpCU->getCUMvField(eRefPicList)->getRefIdx(uiIdx) )->getIsLongTerm();
3453    if ( bIsCurrRefLongTerm == bIsNeibRefLongTerm ) 
3454    {
3455    if ( bIsCurrRefLongTerm || bIsNeibRefLongTerm )
3456    {
3457      rcMv = cMvPred;
3458    }
3459    else
3460    {
3461      Int iScale = xGetDistScaleFactor( iCurrPOC, iCurrRefPOC, iNeibPOC, iNeibRefPOC );
3462      if ( iScale == 4096 )
3463      {
3464        rcMv = cMvPred;
3465      }
3466      else
3467      {
3468        rcMv = cMvPred.scaleMv( iScale );
3469      }
3470    }
3471    pInfo->m_acMvCand[ pInfo->iN++] = rcMv;
3472    return true;
3473    }
3474  }
3475  //---------------------- V2(END) --------------------//
3476  if( pcTmpCU->getCUMvField(eRefPicList2nd)->getRefIdx(uiIdx) >= 0)
3477  {
3478    iNeibRefPOC = pcTmpCU->getSlice()->getRefPOC( eRefPicList2nd, pcTmpCU->getCUMvField(eRefPicList2nd)->getRefIdx(uiIdx) );
3479    TComMv cMvPred = pcTmpCU->getCUMvField(eRefPicList2nd)->getMv(uiIdx);
3480    TComMv rcMv;
3481
3482    bIsNeibRefLongTerm = pcTmpCU->getSlice()->getRefPic( eRefPicList2nd, pcTmpCU->getCUMvField(eRefPicList2nd)->getRefIdx(uiIdx) )->getIsLongTerm();
3483    if ( bIsCurrRefLongTerm == bIsNeibRefLongTerm ) 
3484    {
3485    if ( bIsCurrRefLongTerm || bIsNeibRefLongTerm )
3486    {
3487      rcMv = cMvPred;
3488    }
3489    else
3490    {
3491      Int iScale = xGetDistScaleFactor( iCurrPOC, iCurrRefPOC, iNeibPOC, iNeibRefPOC );
3492      if ( iScale == 4096 )
3493      {
3494        rcMv = cMvPred;
3495      }
3496      else
3497      {
3498        rcMv = cMvPred.scaleMv( iScale );
3499      }
3500    }
3501    pInfo->m_acMvCand[ pInfo->iN++] = rcMv;
3502    return true;
3503    }
3504  }
3505  //---------------------- V3(END) --------------------//
3506  return false;
3507}
3508
3509/**
3510 * \param eRefPicList
3511 * \param uiCUAddr
3512 * \param uiPartUnitIdx
3513 * \param riRefIdx
3514 * \returns Bool
3515 */
3516Bool TComDataCU::xGetColMVP( RefPicList eRefPicList, Int uiCUAddr, Int uiPartUnitIdx, TComMv& rcMv, Int& riRefIdx )
3517{
3518  UInt uiAbsPartAddr = uiPartUnitIdx;
3519
3520  RefPicList  eColRefPicList;
3521  Int iColPOC, iColRefPOC, iCurrPOC, iCurrRefPOC, iScale;
3522  TComMv cColMv;
3523
3524  // use coldir.
3525  TComPic *pColPic = getSlice()->getRefPic( RefPicList(getSlice()->isInterB() ? 1-getSlice()->getColFromL0Flag() : 0), getSlice()->getColRefIdx());
3526  TComDataCU *pColCU = pColPic->getCU( uiCUAddr );
3527  if(pColCU->getPic()==0||pColCU->getPartitionSize(uiPartUnitIdx)==SIZE_NONE)
3528  {
3529    return false;
3530  }
3531  iCurrPOC = m_pcSlice->getPOC();   
3532  iCurrRefPOC = m_pcSlice->getRefPic(eRefPicList, riRefIdx)->getPOC();
3533  iColPOC = pColCU->getSlice()->getPOC(); 
3534
3535  if (pColCU->isIntra(uiAbsPartAddr))
3536  {
3537    return false;
3538  }
3539  eColRefPicList = getSlice()->getCheckLDC() ? eRefPicList : RefPicList(getSlice()->getColFromL0Flag());
3540
3541  Int iColRefIdx = pColCU->getCUMvField(RefPicList(eColRefPicList))->getRefIdx(uiAbsPartAddr);
3542
3543  if (iColRefIdx < 0 )
3544  {
3545    eColRefPicList = RefPicList(1 - eColRefPicList);
3546    iColRefIdx = pColCU->getCUMvField(RefPicList(eColRefPicList))->getRefIdx(uiAbsPartAddr);
3547
3548    if (iColRefIdx < 0 )
3549    {
3550      return false;
3551    }
3552  }
3553
3554  // Scale the vector.
3555  iColRefPOC = pColCU->getSlice()->getRefPOC(eColRefPicList, iColRefIdx);
3556  cColMv = pColCU->getCUMvField(eColRefPicList)->getMv(uiAbsPartAddr);
3557
3558  iCurrRefPOC = m_pcSlice->getRefPic(eRefPicList, riRefIdx)->getPOC();
3559  Bool bIsCurrRefLongTerm = m_pcSlice->getRefPic(eRefPicList, riRefIdx)->getIsLongTerm();
3560  Bool bIsColRefLongTerm = pColCU->getSlice()->getIsUsedAsLongTerm(eColRefPicList, iColRefIdx);
3561
3562  if ( bIsCurrRefLongTerm != bIsColRefLongTerm ) 
3563  {
3564    return false;
3565  }
3566
3567  if ( bIsCurrRefLongTerm || bIsColRefLongTerm )
3568  {
3569    rcMv = cColMv;
3570  }
3571  else
3572  {
3573    iScale = xGetDistScaleFactor(iCurrPOC, iCurrRefPOC, iColPOC, iColRefPOC);
3574    if ( iScale == 4096 )
3575    {
3576      rcMv = cColMv;
3577    }
3578    else
3579    {
3580      rcMv = cColMv.scaleMv( iScale );
3581    }
3582  }
3583  return true;
3584}
3585
3586UInt TComDataCU::xGetMvdBits(TComMv cMvd)
3587{
3588  return ( xGetComponentBits(cMvd.getHor()) + xGetComponentBits(cMvd.getVer()) );
3589}
3590
3591UInt TComDataCU::xGetComponentBits(Int iVal)
3592{
3593  UInt uiLength = 1;
3594  UInt uiTemp   = ( iVal <= 0) ? (-iVal<<1)+1: (iVal<<1);
3595 
3596  assert ( uiTemp );
3597 
3598  while ( 1 != uiTemp )
3599  {
3600    uiTemp >>= 1;
3601    uiLength += 2;
3602  }
3603 
3604  return uiLength;
3605}
3606
3607
3608Int TComDataCU::xGetDistScaleFactor(Int iCurrPOC, Int iCurrRefPOC, Int iColPOC, Int iColRefPOC)
3609{
3610  Int iDiffPocD = iColPOC - iColRefPOC;
3611  Int iDiffPocB = iCurrPOC - iCurrRefPOC;
3612 
3613  if( iDiffPocD == iDiffPocB )
3614  {
3615    return 4096;
3616  }
3617  else
3618  {
3619    Int iTDB      = Clip3( -128, 127, iDiffPocB );
3620    Int iTDD      = Clip3( -128, 127, iDiffPocD );
3621    Int iX        = (0x4000 + abs(iTDD/2)) / iTDD;
3622    Int iScale    = Clip3( -4096, 4095, (iTDB * iX + 32) >> 6 );
3623    return iScale;
3624  }
3625}
3626
3627/**
3628 * \param eCUMode
3629 * \param uiPartIdx
3630 * \param ruiPartIdxCenter
3631 * \returns Void
3632 */
3633Void TComDataCU::xDeriveCenterIdx( UInt uiPartIdx, UInt& ruiPartIdxCenter )
3634{
3635  UInt uiPartAddr;
3636  Int  iPartWidth;
3637  Int  iPartHeight;
3638  getPartIndexAndSize( uiPartIdx, uiPartAddr, iPartWidth, iPartHeight);
3639 
3640  ruiPartIdxCenter = m_uiAbsIdxInLCU+uiPartAddr; // partition origin.
3641  ruiPartIdxCenter = g_auiRasterToZscan[ g_auiZscanToRaster[ ruiPartIdxCenter ]
3642                                        + ( iPartHeight/m_pcPic->getMinCUHeight()  )/2*m_pcPic->getNumPartInWidth()
3643                                        + ( iPartWidth/m_pcPic->getMinCUWidth()  )/2];
3644}
3645
3646Void TComDataCU::compressMV()
3647{
3648  Int scaleFactor = 4 * AMVP_DECIMATION_FACTOR / m_unitSize;
3649  if (scaleFactor > 0)
3650  {
3651    m_acCUMvField[0].compress(m_pePredMode, scaleFactor);
3652    m_acCUMvField[1].compress(m_pePredMode, scaleFactor);   
3653  }
3654}
3655
3656UInt TComDataCU::getCoefScanIdx(UInt uiAbsPartIdx, UInt uiWidth, Bool bIsLuma, Bool bIsIntra)
3657{
3658  UInt uiCTXIdx;
3659  UInt uiScanIdx;
3660  UInt uiDirMode;
3661
3662  if ( !bIsIntra ) 
3663  {
3664    uiScanIdx = SCAN_DIAG;
3665    return uiScanIdx;
3666  }
3667
3668  switch(uiWidth)
3669  {
3670    case  2: uiCTXIdx = 6; break;
3671    case  4: uiCTXIdx = 5; break;
3672    case  8: uiCTXIdx = 4; break;
3673    case 16: uiCTXIdx = 3; break;
3674    case 32: uiCTXIdx = 2; break;
3675    case 64: uiCTXIdx = 1; break;
3676    default: uiCTXIdx = 0; break;
3677  }
3678
3679  if ( bIsLuma )
3680  {
3681    uiDirMode = getLumaIntraDir(uiAbsPartIdx);
3682#if H_3D_DIM
3683    mapDepthModeToIntraDir( uiDirMode );
3684#endif
3685    uiScanIdx = SCAN_DIAG;
3686    if (uiCTXIdx >3 && uiCTXIdx < 6) //if multiple scans supported for transform size
3687    {
3688      uiScanIdx = abs((Int) uiDirMode - VER_IDX) < 5 ? SCAN_HOR : (abs((Int)uiDirMode - HOR_IDX) < 5 ? SCAN_VER : SCAN_DIAG);
3689    }
3690  }
3691  else
3692  {
3693    uiDirMode = getChromaIntraDir(uiAbsPartIdx);
3694    if( uiDirMode == DM_CHROMA_IDX )
3695    {
3696      // get number of partitions in current CU
3697      UInt depth = getDepth(uiAbsPartIdx);
3698      UInt numParts = getPic()->getNumPartInCU() >> (2 * depth);
3699     
3700      // get luma mode from upper-left corner of current CU
3701      uiDirMode = getLumaIntraDir((uiAbsPartIdx/numParts)*numParts);
3702#if H_3D_DIM
3703      mapDepthModeToIntraDir( uiDirMode );
3704#endif
3705    }
3706    uiScanIdx = SCAN_DIAG;
3707    if (uiCTXIdx >4 && uiCTXIdx < 7) //if multiple scans supported for transform size
3708    {
3709      uiScanIdx = abs((Int) uiDirMode - VER_IDX) < 5 ? SCAN_HOR : (abs((Int)uiDirMode - HOR_IDX) < 5 ? SCAN_VER : SCAN_DIAG);
3710    }
3711  }
3712
3713  return uiScanIdx;
3714}
3715
3716UInt TComDataCU::getSCUAddr()
3717{ 
3718  return getPic()->getPicSym()->getInverseCUOrderMap(m_uiCUAddr)*(1<<(m_pcSlice->getSPS()->getMaxCUDepth()<<1))+m_uiAbsIdxInLCU; 
3719}
3720
3721/** Set neighboring blocks availabilities for non-deblocked filtering
3722 * \param numLCUInPicWidth number of LCUs in picture width
3723 * \param numLCUInPicHeight number of LCUs in picture height
3724 * \param numSUInLCUWidth number of SUs in LCU width
3725 * \param numSUInLCUHeight number of SUs in LCU height
3726 * \param picWidth picture width
3727 * \param picHeight picture height
3728 * \param bIndependentSliceBoundaryEnabled true for independent slice boundary enabled
3729 * \param bTopTileBoundary true means that top boundary coincides tile boundary
3730 * \param bDownTileBoundary true means that bottom boundary coincides tile boundary
3731 * \param bLeftTileBoundary true means that left boundary coincides tile boundary
3732 * \param bRightTileBoundary true means that right boundary coincides tile boundary
3733 * \param bIndependentTileBoundaryEnabled true for independent tile boundary enabled
3734 */
3735Void TComDataCU::setNDBFilterBlockBorderAvailability(UInt numLCUInPicWidth, UInt /*numLCUInPicHeight*/, UInt numSUInLCUWidth, UInt numSUInLCUHeight, UInt picWidth, UInt picHeight
3736                                                    ,std::vector<Bool>& LFCrossSliceBoundary
3737                                                    ,Bool bTopTileBoundary, Bool bDownTileBoundary, Bool bLeftTileBoundary, Bool bRightTileBoundary
3738                                                    ,Bool bIndependentTileBoundaryEnabled)
3739{
3740  UInt numSUInLCU = numSUInLCUWidth*numSUInLCUHeight;
3741  Int* pSliceIDMapLCU = m_piSliceSUMap;
3742  Bool onlyOneSliceInPic = ((Int)LFCrossSliceBoundary.size() == 1);
3743  UInt uiLPelX, uiTPelY;
3744  UInt width, height;
3745  Bool bPicRBoundary, bPicBBoundary, bPicTBoundary, bPicLBoundary;
3746  Bool bLCURBoundary= false, bLCUBBoundary= false, bLCUTBoundary= false, bLCULBoundary= false;
3747  Bool* pbAvailBorder;
3748  Bool* pbAvail;
3749  UInt rTLSU, rBRSU, widthSU, heightSU;
3750  UInt zRefSU;
3751  Int* pRefID;
3752  Int* pRefMapLCU;
3753  UInt rTRefSU= 0, rBRefSU= 0, rLRefSU= 0, rRRefSU= 0;
3754  Int* pRRefMapLCU= NULL;
3755  Int* pLRefMapLCU= NULL;
3756  Int* pTRefMapLCU= NULL;
3757  Int* pBRefMapLCU= NULL;
3758  Int  sliceID;
3759  UInt numSGU = (UInt)m_vNDFBlock.size();
3760
3761  for(Int i=0; i< numSGU; i++)
3762  {
3763    NDBFBlockInfo& rSGU = m_vNDFBlock[i];
3764
3765    sliceID = rSGU.sliceID;
3766    uiLPelX = rSGU.posX;
3767    uiTPelY = rSGU.posY;
3768    width   = rSGU.width;
3769    height  = rSGU.height;
3770    rTLSU     = g_auiZscanToRaster[ rSGU.startSU ];
3771    rBRSU     = g_auiZscanToRaster[ rSGU.endSU   ];
3772    widthSU   = rSGU.widthSU;
3773    heightSU  = rSGU.heightSU;
3774
3775    pbAvailBorder = rSGU.isBorderAvailable;
3776
3777    bPicTBoundary= (uiTPelY == 0                       )?(true):(false);
3778    bPicLBoundary= (uiLPelX == 0                       )?(true):(false);
3779    bPicRBoundary= (!(uiLPelX+ width < picWidth )  )?(true):(false);
3780    bPicBBoundary= (!(uiTPelY + height < picHeight))?(true):(false);
3781
3782    bLCULBoundary = (rTLSU % numSUInLCUWidth == 0)?(true):(false);
3783    bLCURBoundary = ( (rTLSU+ widthSU) % numSUInLCUWidth == 0)?(true):(false);
3784    bLCUTBoundary = ( (UInt)(rTLSU / numSUInLCUWidth)== 0)?(true):(false);
3785    bLCUBBoundary = ( (UInt)(rBRSU / numSUInLCUWidth) == (numSUInLCUHeight-1) )?(true):(false);
3786
3787    //       SGU_L
3788    pbAvail = &(pbAvailBorder[SGU_L]);
3789    if(bPicLBoundary)
3790    {
3791      *pbAvail = false;
3792    }
3793    else if (onlyOneSliceInPic)
3794    {
3795      *pbAvail = true;
3796    }
3797    else
3798    {
3799      //      bLCULBoundary = (rTLSU % uiNumSUInLCUWidth == 0)?(true):(false);
3800      if(bLCULBoundary)
3801      {
3802        rLRefSU     = rTLSU + numSUInLCUWidth -1;
3803        zRefSU      = g_auiRasterToZscan[rLRefSU];
3804        pRefMapLCU = pLRefMapLCU= (pSliceIDMapLCU - numSUInLCU);
3805      }
3806      else
3807      {
3808        zRefSU   = g_auiRasterToZscan[rTLSU - 1];
3809        pRefMapLCU  = pSliceIDMapLCU;
3810      }
3811      pRefID = pRefMapLCU + zRefSU;
3812      *pbAvail = (*pRefID == sliceID)?(true):((*pRefID > sliceID)?(LFCrossSliceBoundary[*pRefID]):(LFCrossSliceBoundary[sliceID]));
3813    }
3814
3815    //       SGU_R
3816    pbAvail = &(pbAvailBorder[SGU_R]);
3817    if(bPicRBoundary)
3818    {
3819      *pbAvail = false;
3820    }
3821    else if (onlyOneSliceInPic)
3822    {
3823      *pbAvail = true;
3824    }
3825    else
3826    {
3827      //       bLCURBoundary = ( (rTLSU+ uiWidthSU) % uiNumSUInLCUWidth == 0)?(true):(false);
3828      if(bLCURBoundary)
3829      {
3830        rRRefSU      = rTLSU + widthSU - numSUInLCUWidth;
3831        zRefSU       = g_auiRasterToZscan[rRRefSU];
3832        pRefMapLCU  = pRRefMapLCU= (pSliceIDMapLCU + numSUInLCU);
3833      }
3834      else
3835      {
3836        zRefSU       = g_auiRasterToZscan[rTLSU + widthSU];
3837        pRefMapLCU  = pSliceIDMapLCU;
3838      }
3839      pRefID = pRefMapLCU + zRefSU;
3840      *pbAvail = (*pRefID == sliceID)?(true):((*pRefID > sliceID)?(LFCrossSliceBoundary[*pRefID]):(LFCrossSliceBoundary[sliceID]));
3841    }
3842
3843    //       SGU_T
3844    pbAvail = &(pbAvailBorder[SGU_T]);
3845    if(bPicTBoundary)
3846    {
3847      *pbAvail = false;
3848    }
3849    else if (onlyOneSliceInPic)
3850    {
3851      *pbAvail = true;
3852    }
3853    else
3854    {
3855      //      bLCUTBoundary = ( (UInt)(rTLSU / uiNumSUInLCUWidth)== 0)?(true):(false);
3856      if(bLCUTBoundary)
3857      {
3858        rTRefSU      = numSUInLCU - (numSUInLCUWidth - rTLSU);
3859        zRefSU       = g_auiRasterToZscan[rTRefSU];
3860        pRefMapLCU  = pTRefMapLCU= (pSliceIDMapLCU - (numLCUInPicWidth*numSUInLCU));
3861      }
3862      else
3863      {
3864        zRefSU       = g_auiRasterToZscan[rTLSU - numSUInLCUWidth];
3865        pRefMapLCU  = pSliceIDMapLCU;
3866      }
3867      pRefID = pRefMapLCU + zRefSU;
3868      *pbAvail = (*pRefID == sliceID)?(true):((*pRefID > sliceID)?(LFCrossSliceBoundary[*pRefID]):(LFCrossSliceBoundary[sliceID]));
3869    }
3870
3871    //       SGU_B
3872    pbAvail = &(pbAvailBorder[SGU_B]);
3873    if(bPicBBoundary)
3874    {
3875      *pbAvail = false;
3876    }
3877    else if (onlyOneSliceInPic)
3878    {
3879      *pbAvail = true;
3880    }
3881    else
3882    {
3883      //      bLCUBBoundary = ( (UInt)(rBRSU / uiNumSUInLCUWidth) == (uiNumSUInLCUHeight-1) )?(true):(false);
3884      if(bLCUBBoundary)
3885      {
3886        rBRefSU      = rTLSU % numSUInLCUWidth;
3887        zRefSU       = g_auiRasterToZscan[rBRefSU];
3888        pRefMapLCU  = pBRefMapLCU= (pSliceIDMapLCU + (numLCUInPicWidth*numSUInLCU));
3889      }
3890      else
3891      {
3892        zRefSU       = g_auiRasterToZscan[rTLSU + (heightSU*numSUInLCUWidth)];
3893        pRefMapLCU  = pSliceIDMapLCU;
3894      }
3895      pRefID = pRefMapLCU + zRefSU;
3896      *pbAvail = (*pRefID == sliceID)?(true):((*pRefID > sliceID)?(LFCrossSliceBoundary[*pRefID]):(LFCrossSliceBoundary[sliceID]));
3897    }
3898
3899    //       SGU_TL
3900    pbAvail = &(pbAvailBorder[SGU_TL]);
3901    if(bPicTBoundary || bPicLBoundary)
3902    {
3903      *pbAvail = false;
3904    }
3905    else if (onlyOneSliceInPic)
3906    {
3907      *pbAvail = true;
3908    }
3909    else
3910    {
3911      if(bLCUTBoundary && bLCULBoundary)
3912      {
3913        zRefSU       = numSUInLCU -1;
3914        pRefMapLCU  = pSliceIDMapLCU - ( (numLCUInPicWidth+1)*numSUInLCU);
3915      }
3916      else if(bLCUTBoundary)
3917      {
3918        zRefSU       = g_auiRasterToZscan[ rTRefSU- 1];
3919        pRefMapLCU  = pTRefMapLCU;
3920      }
3921      else if(bLCULBoundary)
3922      {
3923        zRefSU       = g_auiRasterToZscan[ rLRefSU- numSUInLCUWidth ];
3924        pRefMapLCU  = pLRefMapLCU;
3925      }
3926      else //inside LCU
3927      {
3928        zRefSU       = g_auiRasterToZscan[ rTLSU - numSUInLCUWidth -1];
3929        pRefMapLCU  = pSliceIDMapLCU;
3930      }
3931      pRefID = pRefMapLCU + zRefSU;
3932      *pbAvail = (*pRefID == sliceID)?(true):((*pRefID > sliceID)?(LFCrossSliceBoundary[*pRefID]):(LFCrossSliceBoundary[sliceID]));
3933    }
3934
3935    //       SGU_TR
3936    pbAvail = &(pbAvailBorder[SGU_TR]);
3937    if(bPicTBoundary || bPicRBoundary)
3938    {
3939      *pbAvail = false;
3940    }
3941    else if (onlyOneSliceInPic)
3942    {
3943      *pbAvail = true;
3944    }
3945    else
3946    {
3947      if(bLCUTBoundary && bLCURBoundary)
3948      {
3949        zRefSU      = g_auiRasterToZscan[numSUInLCU - numSUInLCUWidth];
3950        pRefMapLCU  = pSliceIDMapLCU - ( (numLCUInPicWidth-1)*numSUInLCU);       
3951      }
3952      else if(bLCUTBoundary)
3953      {
3954        zRefSU       = g_auiRasterToZscan[ rTRefSU+ widthSU];
3955        pRefMapLCU  = pTRefMapLCU;
3956      }
3957      else if(bLCURBoundary)
3958      {
3959        zRefSU       = g_auiRasterToZscan[ rRRefSU- numSUInLCUWidth ];
3960        pRefMapLCU  = pRRefMapLCU;
3961      }
3962      else //inside LCU
3963      {
3964        zRefSU       = g_auiRasterToZscan[ rTLSU - numSUInLCUWidth +widthSU];
3965        pRefMapLCU  = pSliceIDMapLCU;
3966      }
3967      pRefID = pRefMapLCU + zRefSU;
3968      *pbAvail = (*pRefID == sliceID)?(true):((*pRefID > sliceID)?(LFCrossSliceBoundary[*pRefID]):(LFCrossSliceBoundary[sliceID]));
3969    }
3970
3971    //       SGU_BL
3972    pbAvail = &(pbAvailBorder[SGU_BL]);
3973    if(bPicBBoundary || bPicLBoundary)
3974    {
3975      *pbAvail = false;
3976    }
3977    else if (onlyOneSliceInPic)
3978    {
3979      *pbAvail = true;
3980    }
3981    else
3982    {
3983      if(bLCUBBoundary && bLCULBoundary)
3984      {
3985        zRefSU      = g_auiRasterToZscan[numSUInLCUWidth - 1];
3986        pRefMapLCU  = pSliceIDMapLCU + ( (numLCUInPicWidth-1)*numSUInLCU);       
3987      }
3988      else if(bLCUBBoundary)
3989      {
3990        zRefSU       = g_auiRasterToZscan[ rBRefSU - 1];
3991        pRefMapLCU  = pBRefMapLCU;
3992      }
3993      else if(bLCULBoundary)
3994      {
3995        zRefSU       = g_auiRasterToZscan[ rLRefSU+ heightSU*numSUInLCUWidth ];
3996        pRefMapLCU  = pLRefMapLCU;
3997      }
3998      else //inside LCU
3999      {
4000        zRefSU       = g_auiRasterToZscan[ rTLSU + heightSU*numSUInLCUWidth -1];
4001        pRefMapLCU  = pSliceIDMapLCU;
4002      }
4003      pRefID = pRefMapLCU + zRefSU;
4004      *pbAvail = (*pRefID == sliceID)?(true):((*pRefID > sliceID)?(LFCrossSliceBoundary[*pRefID]):(LFCrossSliceBoundary[sliceID]));
4005    }
4006
4007    //       SGU_BR
4008    pbAvail = &(pbAvailBorder[SGU_BR]);
4009    if(bPicBBoundary || bPicRBoundary)
4010    {
4011      *pbAvail = false;
4012    }
4013    else if (onlyOneSliceInPic)
4014    {
4015      *pbAvail = true;
4016    }
4017    else
4018    {
4019      if(bLCUBBoundary && bLCURBoundary)
4020      {
4021        zRefSU = 0;
4022        pRefMapLCU = pSliceIDMapLCU+ ( (numLCUInPicWidth+1)*numSUInLCU);
4023      }
4024      else if(bLCUBBoundary)
4025      {
4026        zRefSU      = g_auiRasterToZscan[ rBRefSU + widthSU];
4027        pRefMapLCU = pBRefMapLCU;
4028      }
4029      else if(bLCURBoundary)
4030      {
4031        zRefSU      = g_auiRasterToZscan[ rRRefSU + (heightSU*numSUInLCUWidth)];
4032        pRefMapLCU = pRRefMapLCU;
4033      }
4034      else //inside LCU
4035      {
4036        zRefSU      = g_auiRasterToZscan[ rTLSU + (heightSU*numSUInLCUWidth)+ widthSU];
4037        pRefMapLCU = pSliceIDMapLCU;
4038      }
4039      pRefID = pRefMapLCU + zRefSU;
4040      *pbAvail = (*pRefID == sliceID)?(true):((*pRefID > sliceID)?(LFCrossSliceBoundary[*pRefID]):(LFCrossSliceBoundary[sliceID]));
4041    }
4042
4043    if(bIndependentTileBoundaryEnabled)
4044    {
4045      //left LCU boundary
4046      if(!bPicLBoundary && bLCULBoundary)
4047      {
4048        if(bLeftTileBoundary)
4049        {
4050          pbAvailBorder[SGU_L] = pbAvailBorder[SGU_TL] = pbAvailBorder[SGU_BL] = false;
4051        }
4052      }
4053      //right LCU boundary
4054      if(!bPicRBoundary && bLCURBoundary)
4055      {
4056        if(bRightTileBoundary)
4057        {
4058          pbAvailBorder[SGU_R] = pbAvailBorder[SGU_TR] = pbAvailBorder[SGU_BR] = false;
4059        }
4060      }
4061      //top LCU boundary
4062      if(!bPicTBoundary && bLCUTBoundary)
4063      {
4064        if(bTopTileBoundary)
4065        {
4066          pbAvailBorder[SGU_T] = pbAvailBorder[SGU_TL] = pbAvailBorder[SGU_TR] = false;
4067        }
4068      }
4069      //down LCU boundary
4070      if(!bPicBBoundary && bLCUBBoundary)
4071      {
4072        if(bDownTileBoundary)
4073        {
4074          pbAvailBorder[SGU_B] = pbAvailBorder[SGU_BL] = pbAvailBorder[SGU_BR] = false;
4075        }
4076      }
4077    }
4078    rSGU.allBordersAvailable = true;
4079    for(Int b=0; b< NUM_SGU_BORDER; b++)
4080    {
4081      if(pbAvailBorder[b] == false)
4082      {
4083        rSGU.allBordersAvailable = false;
4084        break;
4085      }
4086    }
4087  }
4088}
4089
4090#if H_3D
4091Void TComDataCU::getPosInPic( UInt uiAbsPartIndex, Int& riPosX, Int& riPosY )
4092{
4093  riPosX = g_auiRasterToPelX[g_auiZscanToRaster[uiAbsPartIndex]] + getCUPelX();
4094  riPosY = g_auiRasterToPelY[g_auiZscanToRaster[uiAbsPartIndex]] + getCUPelY(); 
4095}
4096#if H_3D_DIM_DMM
4097Void TComDataCU::setDmmWedgeTabIdxSubParts( UInt tabIdx, UInt dmmType, UInt uiAbsPartIdx, UInt uiDepth )
4098{
4099  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
4100  for( UInt ui = 0; ui < uiCurrPartNumb; ui++ ) { m_dmmWedgeTabIdx[dmmType][uiAbsPartIdx+ui] = tabIdx; }
4101}
4102Void  TComDataCU::setDmm2DeltaEndSubParts( Int iDelta, UInt uiAbsPartIdx, UInt uiDepth )
4103{
4104  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
4105  for( UInt ui = 0; ui < uiCurrPartNumb; ui++ ) { m_dmm2DeltaEnd[uiAbsPartIdx+ui] = iDelta; }
4106}
4107Void  TComDataCU::setDmm3IntraTabIdxSubParts( UInt uiTIdx, UInt uiAbsPartIdx, UInt uiDepth )
4108{
4109  UInt uiCurrPartNumb = m_pcPic->getNumPartInCU() >> (uiDepth << 1);
4110  for( UInt ui = 0; ui < uiCurrPartNumb; ui++ ) { m_dmm3IntraTabIdx[uiAbsPartIdx+ui] = uiTIdx; }
4111}
4112#endif
4113#if H_3D_DIM_RBC
4114Void TComDataCU::reconPartition( UInt uiAbsPartIdx, UInt uiDepth, Bool bLeft, UChar ucStartPos, UChar ucNumEdge, UChar* pucEdgeCode, Bool* pbRegion )
4115{
4116  Int iWidth;
4117  Int iHeight;
4118  if( uiDepth == 0 )
4119  {
4120    iWidth = 64;
4121    iHeight = 64;
4122  }
4123  else if( uiDepth == 1 )
4124  {
4125    iWidth = 32;
4126    iHeight = 32;
4127  }
4128  else if( uiDepth == 2 )
4129  {
4130    iWidth = 16;
4131    iHeight = 16;
4132  }
4133  else if( uiDepth == 3 )
4134  {
4135    iWidth = 8;
4136    iHeight = 8;
4137  }
4138  else // uiDepth == 4
4139  {
4140    iWidth = 4;
4141    iHeight = 4;
4142  }
4143
4144  Int iPtr = 0;
4145  Int iX, iY;
4146  Int iDir = -1;
4147  Int iDiffX = 0, iDiffY = 0;
4148
4149  // 1. Edge Code -> Vert & Horz Edges
4150  Bool*  pbEdge = (Bool*) xMalloc( Bool, 4 * iWidth * iHeight );
4151
4152  for( UInt ui = 0; ui < 4 * iWidth * iHeight; ui++ )
4153    pbEdge  [ ui ] = false;
4154
4155  // Direction : left(0), right(1), top(2), bottom(3), left-top(4), right-top(5), left-bottom(6), right-bottom(7)
4156  // Code      : 0deg(0), 45deg(1), -45deg(2), 90deg(3), -90deg(4), 135deg(5), -135deg(6)
4157  const UChar tableDir[8][7] = { { 0, 6, 4, 3, 2, 7, 5 },
4158  { 1, 5, 7, 2, 3, 4, 6 },
4159  { 2, 4, 5, 0, 1, 6, 7 },
4160  { 3, 7, 6, 1, 0, 5, 4 },
4161  { 4, 0, 2, 6, 5, 3, 1 },
4162  { 5, 2, 1, 4, 7, 0, 3 },
4163  { 6, 3, 0, 7, 4, 1, 2 },
4164  { 7, 1, 3, 5, 6, 2, 0 }};
4165
4166  UChar ucCode = pucEdgeCode[iPtr++];
4167
4168  if( !bLeft )
4169  {
4170    iX = ucStartPos;
4171    iY = 0;
4172
4173    switch(ucCode)
4174    {
4175    case 0: // bottom
4176      iDir = 3;
4177      if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4178      break;
4179    case 2: // left-bottom
4180      iDir = 6;
4181      if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4182      break;
4183    case 1: // right-bottom
4184      iDir = 7;
4185      if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4186      if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4187      break;
4188    case 4: // left
4189      iDir = 0;
4190      assert(false);
4191      break;
4192    case 3: // right
4193      iDir = 1;
4194      if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4195      if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4196      break;
4197    }
4198  }
4199  else
4200  {
4201    iX = 0;
4202    iY = ucStartPos;
4203
4204    switch(ucCode)
4205    {
4206    case 0: // right
4207      iDir = 1;
4208      if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4209      break;
4210    case 1: // right-top
4211      iDir = 5;
4212      if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4213      if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4214      break;
4215    case 2: // right-bottom
4216      iDir = 7;
4217      if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4218      break;
4219    case 3: // top
4220      iDir = 2;
4221      if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4222      if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4223      break;
4224    case 4: // bottom
4225      iDir = 3;
4226      assert(false);
4227      break;
4228    }
4229  }
4230
4231  switch( iDir )
4232  {
4233  case 0: // left
4234    iDiffX = -1;
4235    iDiffY = 0;
4236    break;
4237  case 1: // right
4238    iDiffX = +1;
4239    iDiffY = 0;
4240    break;
4241  case 2: // top
4242    iDiffX = 0;
4243    iDiffY = -1;
4244    break;
4245  case 3: // bottom
4246    iDiffX = 0;
4247    iDiffY = +1;
4248    break;
4249  case 4: // left-top
4250    iDiffX = -1;
4251    iDiffY = -1;
4252    break;
4253  case 5: // right-top
4254    iDiffX = +1;
4255    iDiffY = -1;
4256    break;
4257  case 6: // left-bottom
4258    iDiffX = -1;
4259    iDiffY = +1;
4260    break;
4261  case 7: // right-bottom
4262    iDiffX = +1;
4263    iDiffY = +1;
4264    break;
4265  }
4266
4267  iX += iDiffX;
4268  iY += iDiffY;
4269
4270  while( iPtr < ucNumEdge )
4271  {
4272    ucCode = pucEdgeCode[iPtr++];
4273
4274    Int iNewDir = tableDir[iDir][ucCode];
4275
4276    switch( iNewDir )
4277    {
4278    case 0: // left
4279      iDiffX = -1;
4280      iDiffY = 0;
4281      break;
4282    case 1: // right
4283      iDiffX = +1;
4284      iDiffY = 0;
4285      break;
4286    case 2: // top
4287      iDiffX = 0;
4288      iDiffY = -1;
4289      break;
4290    case 3: // bottom
4291      iDiffX = 0;
4292      iDiffY = +1;
4293      break;
4294    case 4: // left-top
4295      iDiffX = -1;
4296      iDiffY = -1;
4297      break;
4298    case 5: // right-top
4299      iDiffX = +1;
4300      iDiffY = -1;
4301      break;
4302    case 6: // left-bottom
4303      iDiffX = -1;
4304      iDiffY = +1;
4305      break;
4306    case 7: // right-bottom
4307      iDiffX = +1;
4308      iDiffY = +1;
4309      break;
4310    }
4311
4312    switch( iDir )
4313    {
4314    case 0: // left
4315      switch( ucCode )
4316      {
4317      case 0:
4318      case 2:
4319        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4320        break;
4321      case 1:
4322      case 3:
4323        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4324        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4325        break;
4326      case 4:
4327      case 6:
4328        // no
4329        break;
4330      case 5:
4331        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4332        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4333        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4334        break;
4335      }
4336      break;
4337    case 1: // right
4338      switch( ucCode )
4339      {
4340      case 0:
4341      case 2:
4342        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4343        break;
4344      case 1:
4345      case 3:
4346        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4347        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4348        break;
4349      case 4:
4350      case 6:
4351        // no
4352        break;
4353      case 5:
4354        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4355        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4356        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4357        break;
4358      }
4359      break;
4360    case 2: // top
4361      switch( ucCode )
4362      {
4363      case 0:
4364      case 2:
4365        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4366        break;
4367      case 1:
4368      case 3:
4369        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4370        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4371        break;
4372      case 4:
4373      case 6:
4374        // no
4375        break;
4376      case 5:
4377        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4378        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4379        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4380        break;
4381      }
4382      break;
4383    case 3: // bottom
4384      switch( ucCode )
4385      {
4386      case 0:
4387      case 2:
4388        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4389        break;
4390      case 1:
4391      case 3:
4392        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4393        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4394        break;
4395      case 4:
4396      case 6:
4397        // no
4398        break;
4399      case 5:
4400        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4401        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4402        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4403        break;
4404      }
4405      break;
4406    case 4: // left-top
4407      switch( ucCode )
4408      {
4409      case 0:
4410      case 1:
4411        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4412        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4413        break;
4414      case 2:
4415      case 4:
4416        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4417        break;
4418      case 3:
4419      case 5:
4420        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4421        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4422        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4423        break;
4424      case 6:
4425        // no
4426        break;
4427      }
4428      break;
4429    case 5: // right-top
4430      switch( ucCode )
4431      {
4432      case 0:
4433      case 1:
4434        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4435        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4436        break;
4437      case 2:
4438      case 4:
4439        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4440        break;
4441      case 3:
4442      case 5:
4443        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4444        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4445        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4446        break;
4447      case 6:
4448        // no
4449        break;
4450      }
4451      break;
4452    case 6: // left-bottom
4453      switch( ucCode )
4454      {
4455      case 0:
4456      case 1:
4457        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4458        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4459        break;
4460      case 2:
4461      case 4:
4462        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4463        break;
4464      case 3:
4465      case 5:
4466        if(iY > 0) pbEdge[ 2 * iX + (2 * (iY - 1) + 1) * 2 * iWidth ] = true;
4467        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4468        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4469        break;
4470      case 6:
4471        // no
4472        break;
4473      }
4474      break;
4475    case 7: // right-bottom
4476      switch( ucCode )
4477      {
4478      case 0:
4479      case 1:
4480        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4481        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4482        break;
4483      case 2:
4484      case 4:
4485        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4486        break;
4487      case 3:
4488      case 5:
4489        if(iX > 0) pbEdge[ 2 * (iX - 1) + 1 + iY * 4 * iWidth ] = true;
4490        if(iX < iWidth - 1) pbEdge[ 2 * (iX + 0) + 1 + iY * 4 * iWidth ] = true;
4491        if(iY < iHeight - 1) pbEdge[ 2 * iX + (2 * (iY + 0) + 1) * 2 * iWidth ] = true;
4492        break;
4493      case 6:
4494        // no
4495        break;
4496      }
4497      break;
4498    }
4499
4500    assert( iX >= 0 && iX <= iWidth );
4501    assert( iY >= 0 && iY <= iHeight );
4502
4503    iX += iDiffX;
4504    iY += iDiffY;
4505    iDir = iNewDir;
4506  }
4507
4508  // finalize edge chain
4509  if( iX == iWidth-1 )
4510  {
4511    if( iY == 0 )
4512    {
4513      if( iDir == 1 )
4514      {
4515        pbEdge[ 2 * iX + (2 * iY + 1) * 2 * iWidth ] = true;
4516      }
4517      else if( iDir == 5 )
4518      {
4519        pbEdge[ 2 * iX + (2 * iY + 1) * 2 * iWidth ] = true;
4520      }
4521      else
4522      {
4523        assert(false);
4524      }
4525    }
4526    else if( iY == iHeight-1 )
4527    {
4528      if( iDir == 3 )
4529      {
4530        pbEdge[ 2 * iX - 1 + 2 * iY * 2 * iWidth ] = true;
4531      }
4532      else if( iDir == 7 )
4533      {
4534        pbEdge[ 2 * iX - 1 + 2 * iY * 2 * iWidth ] = true;
4535      }
4536      else
4537      {
4538        assert(false);
4539      }
4540    }
4541    else
4542    {
4543      if( iDir == 1 )
4544      {
4545        pbEdge[ 2 * iX + (2 * iY + 1) * 2 * iWidth ] = true;
4546      }
4547      else if( iDir == 3 )
4548      {
4549        pbEdge[ 2 * iX - 1 + 2 * iY * 2 * iWidth ] = true;
4550        pbEdge[ 2 * iX + (2 * iY + 1) * 2 * iWidth ] = true;
4551      }
4552      else if( iDir == 5 )
4553      {
4554        pbEdge[ 2 * iX + (2 * iY + 1) * 2 * iWidth ] = true;
4555      }
4556      else if( iDir == 7 )
4557      {
4558        pbEdge[ 2 * iX - 1 + 2 * iY * 2 * iWidth ] = true;
4559        pbEdge[ 2 * iX + (2 * iY + 1) * 2 * iWidth ] = true;
4560      }
4561      else
4562      {
4563        assert(false);
4564      }
4565    }
4566  }
4567  else if( iX == 0 )
4568  {
4569    if( iY == 0 )
4570    {
4571      if( iDir == 2 )
4572      {
4573        pbEdge[ 2 * iX + 1 + 2 * iY * 2 * iWidth ] = true;
4574      }
4575      else if( iDir == 4 )
4576      {
4577        pbEdge[ 2 * iX + 1 + 2 * iY * 2 * iWidth ] = true;
4578      }
4579      else
4580      {
4581        assert(false);
4582      }
4583    }
4584    else if( iY == iHeight-1 )
4585    {
4586      if( iDir == 0 )
4587      {
4588        pbEdge[ 2 * iX + (2 * iY - 1) * 2 * iWidth ] = true;
4589      }
4590      else if( iDir == 6 )
4591      {
4592        pbEdge[ 2 * iX + (2 * iY - 1) * 2 * iWidth ] = true;
4593      }
4594      else
4595      {
4596        assert(false);
4597      }
4598    }
4599    else
4600    {
4601      if( iDir == 0 )
4602      {
4603        pbEdge[ 2 * iX + (2 * iY - 1) * 2 * iWidth ] = true;
4604      }
4605      else if( iDir == 2 )
4606      {
4607        pbEdge[ 2 * iX + 1 + 2 * iY * 2 * iWidth ] = true;
4608        pbEdge[ 2 * iX + (2 * iY - 1) * 2 * iWidth ] = true;
4609      }
4610      else if( iDir == 4 )
4611      {
4612        pbEdge[ 2 * iX + 1 + 2 * iY * 2 * iWidth ] = true;
4613        pbEdge[ 2 * iX + (2 * iY - 1) * 2 * iWidth ] = true;
4614      }
4615      else if( iDir == 6 )
4616      {
4617        pbEdge[ 2 * iX + (2 * iY - 1) * 2 * iWidth ] = true;
4618      }
4619      else
4620      {
4621        assert(false);
4622      }
4623    }
4624  }
4625  else if( iY == 0 )
4626  {
4627    if( iDir == 1 )
4628    {
4629      pbEdge[ 2 * iX + (2 * iY + 1) * 2 * iWidth ] = true;
4630      pbEdge[ 2 * iX + 1 + 2 * iY * 2 * iWidth ] = true;
4631    }
4632    else if( iDir == 2 )
4633    {
4634      pbEdge[ 2 * iX + 1 + 2 * iY * 2 * iWidth ] = true;
4635    }
4636    else if( iDir == 4 )
4637    {
4638      pbEdge[ 2 * iX + 1 + 2 * iY * 2 * iWidth ] = true;
4639    }
4640    else if( iDir == 5 )
4641    {
4642      pbEdge[ 2 * iX + (2 * iY + 1) * 2 * iWidth ] = true;
4643      pbEdge[ 2 * iX + 1 + 2 * iY * 2 * iWidth ] = true;
4644    }
4645    else
4646    {
4647      assert(false);
4648    }
4649  }
4650  else if( iY == iHeight-1 )
4651  {
4652    if( iDir == 0 )
4653    {
4654      pbEdge[ 2 * iX + (2 * iY - 1) * 2 * iWidth ] = true;
4655      pbEdge[ 2 * iX - 1 + 2 * iY * 2 * iWidth ] = true;
4656    }
4657    else if( iDir == 3 )
4658    {
4659      pbEdge[ 2 * iX - 1 + 2 * iY * 2 * iWidth ] = true;
4660    }
4661    else if( iDir == 6 )
4662    {
4663      pbEdge[ 2 * iX + (2 * iY - 1) * 2 * iWidth ] = true;
4664      pbEdge[ 2 * iX - 1 + 2 * iY * 2 * iWidth ] = true;
4665    }
4666    else if( iDir == 7 )
4667    {
4668      pbEdge[ 2 * iX - 1 + 2 * iY * 2 * iWidth ] = true;
4669    }
4670    else
4671    {
4672      assert(false);
4673    }
4674  }
4675  else
4676  {
4677    printf("reconPartiton: wrong termination\n");
4678    assert(false);
4679  }
4680
4681  // Reconstruct Region from Chain Code
4682  Bool* pbVisit  = (Bool*) xMalloc( Bool, iWidth * iHeight );
4683  Int*  piStack  = (Int* ) xMalloc( Int,  iWidth * iHeight );
4684
4685  for( UInt ui = 0; ui < iWidth * iHeight; ui++ )
4686  {
4687    pbRegion[ ui ] = true; // fill it as region 1 (we'll discover region 0 next)
4688    pbVisit [ ui ] = false;
4689  }
4690
4691  iPtr = 0;
4692  piStack[iPtr++] = (0 << 8) | (0);
4693  pbRegion[ 0 ] = false;
4694
4695  while(iPtr > 0)
4696  {
4697    Int iTmp = piStack[--iPtr];
4698    Int iX1, iY1;
4699    iX1 = iTmp & 0xff;
4700    iY1 = (iTmp >> 8) & 0xff;
4701
4702    pbVisit[ iX1 + iY1 * iWidth ] = true;
4703
4704    assert( iX1 >= 0 && iX1 < iWidth );
4705    assert( iY1 >= 0 && iY1 < iHeight );
4706
4707    if( iX1 > 0 && !pbEdge[ 2 * iX1 - 1 + 4 * iY1 * iWidth ] && !pbVisit[ iX1 - 1 + iY1 * iWidth ] )
4708    {
4709      piStack[iPtr++] = (iY1 << 8) | (iX1 - 1);
4710      pbRegion[ iX1 - 1 + iY1 * iWidth ] = false;
4711    }
4712    if( iX1 < iWidth - 1 && !pbEdge[ 2 * iX1 + 1 + 4 * iY1 * iWidth ] && !pbVisit[ iX1 + 1 + iY1 * iWidth ] )
4713    {
4714      piStack[iPtr++] = (iY1 << 8) | (iX1 + 1);
4715      pbRegion[ iX1 + 1 + iY1 * iWidth ] = false;
4716    }
4717    if( iY1 > 0 && !pbEdge[ 2 * iX1 + 2 * (2 * iY1 - 1) * iWidth ] && !pbVisit[ iX1 + (iY1 - 1) * iWidth ] )
4718    {
4719      piStack[iPtr++] = ((iY1 - 1) << 8) | iX1;
4720      pbRegion[ iX1 + (iY1 - 1) * iWidth ] = false;
4721    }
4722    if( iY1 < iHeight - 1 && !pbEdge[ 2 * iX1 + 2 * (2 * iY1 + 1) * iWidth ] && !pbVisit[ iX1 + (iY1 + 1) * iWidth ] )
4723    {
4724      piStack[iPtr++] = ((iY1 + 1) << 8) | iX1;
4725      pbRegion[ iX1 + (iY1 + 1) * iWidth ] = false;
4726    }
4727  }
4728
4729  xFree( pbEdge );
4730  xFree( pbVisit );
4731  xFree( piStack );
4732}
4733#endif
4734#endif
4735
4736//! \}
Note: See TracBrowser for help on using the repository browser.