source: SHVCSoftware/branches/SHM-dev/source/Lib/TLibCommon/TComDataCU.cpp @ 1240

Last change on this file since 1240 was 1236, checked in by seregin, 10 years ago

port rev 4220

  • Property svn:eol-style set to native
File size: 125.6 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-2014, 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 "TComTU.h"
41#include "TComPic.h"
42
43//! \ingroup TLibCommon
44//! \{
45
46#if ADAPTIVE_QP_SELECTION
47  TCoeff * TComDataCU::m_pcGlbArlCoeff[MAX_NUM_COMPONENT] = { NULL, NULL, NULL };
48#endif
49
50// ====================================================================================================================
51// Constructor / destructor / create / destroy
52// ====================================================================================================================
53
54TComDataCU::TComDataCU()
55{
56  m_pcPic              = NULL;
57  m_pcSlice            = NULL;
58  m_puhDepth           = NULL;
59
60  m_skipFlag           = NULL;
61
62  m_pePartSize         = NULL;
63  m_pePredMode         = NULL;
64  m_CUTransquantBypass = NULL;
65  m_puhWidth           = NULL;
66  m_puhHeight          = NULL;
67  m_phQP               = NULL;
68  m_ChromaQpAdj        = NULL;
69  m_pbMergeFlag        = NULL;
70  m_puhMergeIndex      = NULL;
71  for(UInt i=0; i<MAX_NUM_CHANNEL_TYPE; i++)
72  {
73    m_puhIntraDir[i]     = NULL;
74  }
75  m_puhInterDir        = NULL;
76  m_puhTrIdx           = NULL;
77
78  for (UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
79  {
80    m_puhCbf[comp]                        = NULL;
81    m_crossComponentPredictionAlpha[comp] = NULL;
82    m_puhTransformSkip[comp]              = NULL;
83    m_pcTrCoeff[comp]                     = NULL;
84#if ADAPTIVE_QP_SELECTION
85    m_pcArlCoeff[comp]                    = NULL;
86#endif
87    m_pcIPCMSample[comp]                  = NULL;
88    m_explicitRdpcmMode[comp]             = NULL;
89  }
90#if ADAPTIVE_QP_SELECTION
91  m_ArlCoeffIsAliasedAllocation = false;
92#endif
93  m_pbIPCMFlag         = NULL;
94
95  m_pCtuAboveLeft      = NULL;
96  m_pCtuAboveRight     = NULL;
97  m_pCtuAbove          = NULL;
98  m_pCtuLeft           = NULL;
99
100  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
101  {
102    m_apcCUColocated[i]  = NULL;
103    m_apiMVPIdx[i]       = NULL;
104    m_apiMVPNum[i]       = NULL;
105  }
106
107  m_bDecSubCu          = false;
108}
109
110TComDataCU::~TComDataCU()
111{
112}
113
114Void TComDataCU::create( ChromaFormat chromaFormatIDC, UInt uiNumPartition, UInt uiWidth, UInt uiHeight, Bool bDecSubCu, Int unitSize
115#if ADAPTIVE_QP_SELECTION
116                        , Bool bGlobalRMARLBuffer
117#endif
118                        )
119{
120  m_bDecSubCu = bDecSubCu;
121
122  m_pcPic              = NULL;
123  m_pcSlice            = NULL;
124  m_uiNumPartition     = uiNumPartition;
125  m_unitSize = unitSize;
126
127  if ( !bDecSubCu )
128  {
129    m_phQP               = (Char*     )xMalloc(Char,     uiNumPartition);
130    m_puhDepth           = (UChar*    )xMalloc(UChar,    uiNumPartition);
131    m_puhWidth           = (UChar*    )xMalloc(UChar,    uiNumPartition);
132    m_puhHeight          = (UChar*    )xMalloc(UChar,    uiNumPartition);
133
134    m_ChromaQpAdj        = new UChar[ uiNumPartition ];
135    m_skipFlag           = new Bool[ uiNumPartition ];
136    m_pePartSize         = new Char[ uiNumPartition ];
137    memset( m_pePartSize, NUMBER_OF_PART_SIZES,uiNumPartition * sizeof( *m_pePartSize ) );
138    m_pePredMode         = new Char[ uiNumPartition ];
139    m_CUTransquantBypass = new Bool[ uiNumPartition ];
140
141    m_pbMergeFlag        = (Bool*  )xMalloc(Bool,   uiNumPartition);
142    m_puhMergeIndex      = (UChar* )xMalloc(UChar,  uiNumPartition);
143
144    for (UInt ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
145    {
146      m_puhIntraDir[ch] = (UChar* )xMalloc(UChar,  uiNumPartition);
147    }
148    m_puhInterDir        = (UChar* )xMalloc(UChar,  uiNumPartition);
149
150    m_puhTrIdx           = (UChar* )xMalloc(UChar,  uiNumPartition);
151
152    for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
153    {
154      const RefPicList rpl=RefPicList(i);
155      m_apiMVPIdx[rpl]       = new Char[ uiNumPartition ];
156      m_apiMVPNum[rpl]       = new Char[ uiNumPartition ];
157      memset( m_apiMVPIdx[rpl], -1,uiNumPartition * sizeof( Char ) );
158    }
159
160    for (UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
161    {
162      const ComponentID compID = ComponentID(comp);
163      const UInt chromaShift = getComponentScaleX(compID, chromaFormatIDC) + getComponentScaleY(compID, chromaFormatIDC);
164      const UInt totalSize   = (uiWidth * uiHeight) >> chromaShift;
165
166      m_crossComponentPredictionAlpha[compID] = (Char*  )xMalloc(Char,   uiNumPartition);
167      m_puhTransformSkip[compID]              = (UChar* )xMalloc(UChar,  uiNumPartition);
168      m_explicitRdpcmMode[compID]             = (UChar* )xMalloc(UChar,  uiNumPartition);
169      m_puhCbf[compID]                        = (UChar* )xMalloc(UChar,  uiNumPartition);
170      m_pcTrCoeff[compID]                     = (TCoeff*)xMalloc(TCoeff, totalSize);
171      memset( m_pcTrCoeff[compID], 0, (totalSize * sizeof( TCoeff )) );
172
173#if ADAPTIVE_QP_SELECTION
174      if( bGlobalRMARLBuffer )
175      {
176#if LAYER_CTB
177        if (m_pcGlbArlCoeff[compID] == NULL) m_pcGlbArlCoeff[compID] = (TCoeff*)xMalloc(TCoeff, MAX_CU_SIZE * MAX_CU_SIZE);
178#else
179        if (m_pcGlbArlCoeff[compID] == NULL) m_pcGlbArlCoeff[compID] = (TCoeff*)xMalloc(TCoeff, totalSize);
180#endif
181
182        m_pcArlCoeff[compID] = m_pcGlbArlCoeff[compID];
183        m_ArlCoeffIsAliasedAllocation = true;
184      }
185      else
186      {
187         m_pcArlCoeff[compID] = (TCoeff*)xMalloc(TCoeff, totalSize);
188      }
189#endif
190      m_pcIPCMSample[compID] = (Pel*   )xMalloc(Pel , totalSize);
191    }
192
193    m_pbIPCMFlag         = (Bool*  )xMalloc(Bool, uiNumPartition);
194
195    for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
196    {
197      m_acCUMvField[i].create( uiNumPartition );
198    }
199
200  }
201  else
202  {
203    for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
204    {
205      m_acCUMvField[i].setNumPartition(uiNumPartition );
206    }
207  }
208
209  // create motion vector fields
210
211  m_pCtuAboveLeft      = NULL;
212  m_pCtuAboveRight     = NULL;
213  m_pCtuAbove          = NULL;
214  m_pCtuLeft           = NULL;
215
216  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
217  {
218    m_apcCUColocated[i]  = NULL;
219  }
220}
221
222Void TComDataCU::destroy()
223{
224  // encoder-side buffer free
225  if ( !m_bDecSubCu )
226  {
227    if ( m_phQP               ) { xFree(m_phQP);                m_phQP               = NULL; }
228    if ( m_puhDepth           ) { xFree(m_puhDepth);            m_puhDepth           = NULL; }
229    if ( m_puhWidth           ) { xFree(m_puhWidth);            m_puhWidth           = NULL; }
230    if ( m_puhHeight          ) { xFree(m_puhHeight);           m_puhHeight          = NULL; }
231
232    if ( m_skipFlag           ) { delete[] m_skipFlag;          m_skipFlag          = NULL; }
233
234    if ( m_pePartSize         ) { delete[] m_pePartSize;        m_pePartSize         = NULL; }
235    if ( m_pePredMode         ) { delete[] m_pePredMode;        m_pePredMode         = NULL; }
236    if ( m_ChromaQpAdj        ) { delete[] m_ChromaQpAdj;       m_ChromaQpAdj        = NULL; }
237    if ( m_CUTransquantBypass ) { delete[] m_CUTransquantBypass;m_CUTransquantBypass = NULL; }
238    if ( m_puhInterDir        ) { xFree(m_puhInterDir);         m_puhInterDir        = NULL; }
239    if ( m_pbMergeFlag        ) { xFree(m_pbMergeFlag);         m_pbMergeFlag        = NULL; }
240    if ( m_puhMergeIndex      ) { xFree(m_puhMergeIndex);       m_puhMergeIndex      = NULL; }
241
242    for (UInt ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
243    {
244      xFree(m_puhIntraDir[ch]);
245      m_puhIntraDir[ch] = NULL;
246    }
247
248    if ( m_puhTrIdx           ) { xFree(m_puhTrIdx);            m_puhTrIdx          = NULL; }
249
250    for (UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
251    {
252      if ( m_crossComponentPredictionAlpha[comp] ) { xFree(m_crossComponentPredictionAlpha[comp]); m_crossComponentPredictionAlpha[comp] = NULL; }
253      if ( m_puhTransformSkip[comp]              ) { xFree(m_puhTransformSkip[comp]);              m_puhTransformSkip[comp]              = NULL; }
254      if ( m_puhCbf[comp]                        ) { xFree(m_puhCbf[comp]);                        m_puhCbf[comp]                        = NULL; }
255      if ( m_pcTrCoeff[comp]                     ) { xFree(m_pcTrCoeff[comp]);                     m_pcTrCoeff[comp]                     = NULL; }
256      if ( m_explicitRdpcmMode[comp]             ) { xFree(m_explicitRdpcmMode[comp]);             m_explicitRdpcmMode[comp]             = NULL; }
257
258#if ADAPTIVE_QP_SELECTION
259      if (!m_ArlCoeffIsAliasedAllocation)
260      {
261        if ( m_pcArlCoeff[comp]     ) { xFree(m_pcArlCoeff[comp]);      m_pcArlCoeff[comp]    = NULL; }
262      }
263
264      if ( m_pcGlbArlCoeff[comp]  ) { xFree(m_pcGlbArlCoeff[comp]);   m_pcGlbArlCoeff[comp] = NULL; }
265#endif
266
267      if ( m_pcIPCMSample[comp]   ) { xFree(m_pcIPCMSample[comp]);    m_pcIPCMSample[comp]  = NULL; }
268    }
269    if ( m_pbIPCMFlag         ) { xFree(m_pbIPCMFlag   );       m_pbIPCMFlag        = NULL; }
270
271    for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
272    {
273      const RefPicList rpl=RefPicList(i);
274      if ( m_apiMVPIdx[rpl]       ) { delete[] m_apiMVPIdx[rpl];      m_apiMVPIdx[rpl]      = NULL; }
275      if ( m_apiMVPNum[rpl]       ) { delete[] m_apiMVPNum[rpl];      m_apiMVPNum[rpl]      = NULL; }
276    }
277
278    for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
279    {
280      const RefPicList rpl=RefPicList(i);
281      m_acCUMvField[rpl].destroy();
282    }
283  }
284
285  m_pcPic              = NULL;
286  m_pcSlice            = NULL;
287
288  m_pCtuAboveLeft      = NULL;
289  m_pCtuAboveRight     = NULL;
290  m_pCtuAbove          = NULL;
291  m_pCtuLeft           = NULL;
292
293
294  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
295  {
296    m_apcCUColocated[i]  = NULL;
297  }
298
299}
300
301Bool TComDataCU::CUIsFromSameTile            ( const TComDataCU *pCU /* Can be NULL */) const
302{
303  return pCU!=NULL &&
304         pCU->getSlice() != NULL &&
305         m_pcPic->getPicSym()->getTileIdxMap( pCU->getCtuRsAddr() ) == m_pcPic->getPicSym()->getTileIdxMap(getCtuRsAddr());
306}
307
308Bool TComDataCU::CUIsFromSameSliceAndTile    ( const TComDataCU *pCU /* Can be NULL */) const
309{
310  return pCU!=NULL &&
311         pCU->getSlice() != NULL &&
312         pCU->getSlice()->getSliceCurStartCtuTsAddr() == getSlice()->getSliceCurStartCtuTsAddr() &&
313         m_pcPic->getPicSym()->getTileIdxMap( pCU->getCtuRsAddr() ) == m_pcPic->getPicSym()->getTileIdxMap(getCtuRsAddr())
314         ;
315}
316
317Bool TComDataCU::CUIsFromSameSliceTileAndWavefrontRow( const TComDataCU *pCU /* Can be NULL */) const
318{
319  return CUIsFromSameSliceAndTile(pCU)
320         && (!getSlice()->getPPS()->getEntropyCodingSyncEnabledFlag() || getPic()->getCtu(getCtuRsAddr())->getCUPelY() == getPic()->getCtu(pCU->getCtuRsAddr())->getCUPelY());
321}
322
323Bool TComDataCU::isLastSubCUOfCtu(const UInt absPartIdx)
324{
325  TComPic* pcPic = getPic();
326  TComSlice * pcSlice = pcPic->getSlice(pcPic->getCurrSliceIdx());
327
328#if SVC_EXTENSION
329  const UInt picWidth = pcSlice->getPicWidthInLumaSamples();
330  const UInt picHeight = pcSlice->getPicHeightInLumaSamples();
331#else
332  const UInt picWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
333  const UInt picHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
334#endif
335  const UInt granularityWidth = g_uiMaxCUWidth;
336
337  const UInt cuPosX = getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[absPartIdx] ];
338  const UInt cuPosY = getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[absPartIdx] ];
339
340  return (((cuPosX+getWidth( absPartIdx))%granularityWidth==0||(cuPosX+getWidth( absPartIdx)==picWidth ))
341       && ((cuPosY+getHeight(absPartIdx))%granularityWidth==0||(cuPosY+getHeight(absPartIdx)==picHeight)));
342}
343
344// ====================================================================================================================
345// Public member functions
346// ====================================================================================================================
347
348// --------------------------------------------------------------------------------------------------------------------
349// Initialization
350// --------------------------------------------------------------------------------------------------------------------
351
352/**
353 - initialize top-level CU
354 - internal buffers are already created
355 - set values before encoding a CU
356 .
357 \param  pcPic     picture (TComPic) class pointer
358 \param  iCUAddr   CU address
359 */
360Void TComDataCU::initCtu( TComPic* pcPic, UInt ctuRsAddr )
361{
362
363  m_pcPic              = pcPic;
364  m_pcSlice            = pcPic->getSlice(pcPic->getCurrSliceIdx());
365  m_ctuRsAddr          = ctuRsAddr;
366  m_uiCUPelX           = ( ctuRsAddr % pcPic->getFrameWidthInCtus() ) * g_uiMaxCUWidth;
367  m_uiCUPelY           = ( ctuRsAddr / pcPic->getFrameWidthInCtus() ) * g_uiMaxCUHeight;
368  m_absZIdxInCtu       = 0;
369  m_dTotalCost         = MAX_DOUBLE;
370  m_uiTotalDistortion  = 0;
371  m_uiTotalBits        = 0;
372  m_uiTotalBins        = 0;
373  m_uiNumPartition     = pcPic->getNumPartitionsInCtu();
374
375  memset( m_skipFlag          , false,                      m_uiNumPartition * sizeof( *m_skipFlag ) );
376
377#if SVC_EXTENSION
378  m_layerId           = pcPic->getLayerId();
379#endif
380
381  memset( m_pePartSize        , NUMBER_OF_PART_SIZES,       m_uiNumPartition * sizeof( *m_pePartSize ) );
382  memset( m_pePredMode        , NUMBER_OF_PREDICTION_MODES, m_uiNumPartition * sizeof( *m_pePredMode ) );
383  memset( m_CUTransquantBypass, false,                      m_uiNumPartition * sizeof( *m_CUTransquantBypass) );
384  memset( m_puhDepth          , 0,                          m_uiNumPartition * sizeof( *m_puhDepth ) );
385  memset( m_puhTrIdx          , 0,                          m_uiNumPartition * sizeof( *m_puhTrIdx ) );
386  memset( m_puhWidth          , g_uiMaxCUWidth,             m_uiNumPartition * sizeof( *m_puhWidth ) );
387  memset( m_puhHeight         , g_uiMaxCUHeight,            m_uiNumPartition * sizeof( *m_puhHeight ) );
388  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
389  {
390    const RefPicList rpl=RefPicList(i);
391    memset( m_apiMVPIdx[rpl]  , -1,                         m_uiNumPartition * sizeof( *m_apiMVPIdx[rpl] ) );
392    memset( m_apiMVPNum[rpl]  , -1,                         m_uiNumPartition * sizeof( *m_apiMVPNum[rpl] ) );
393  }
394  memset( m_phQP              , getSlice()->getSliceQp(),   m_uiNumPartition * sizeof( *m_phQP ) );
395  memset( m_ChromaQpAdj       , 0,                          m_uiNumPartition * sizeof( *m_ChromaQpAdj ) );
396  for(UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
397  {
398    memset( m_crossComponentPredictionAlpha[comp] , 0,                     m_uiNumPartition * sizeof( *m_crossComponentPredictionAlpha[comp] ) );
399    memset( m_puhTransformSkip[comp]              , 0,                     m_uiNumPartition * sizeof( *m_puhTransformSkip[comp]) );
400    memset( m_puhCbf[comp]                        , 0,                     m_uiNumPartition * sizeof( *m_puhCbf[comp] ) );
401    memset( m_explicitRdpcmMode[comp]             , NUMBER_OF_RDPCM_MODES, m_uiNumPartition * sizeof( *m_explicitRdpcmMode[comp] ) );
402  }
403  memset( m_pbMergeFlag       , false,                    m_uiNumPartition * sizeof( *m_pbMergeFlag ) );
404  memset( m_puhMergeIndex     , 0,                        m_uiNumPartition * sizeof( *m_puhMergeIndex ) );
405  for (UInt ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
406  {
407    memset( m_puhIntraDir[ch] , ((ch==0) ? DC_IDX : 0),   m_uiNumPartition * sizeof( *(m_puhIntraDir[ch]) ) );
408  }
409  memset( m_puhInterDir       , 0,                        m_uiNumPartition * sizeof( *m_puhInterDir ) );
410  memset( m_pbIPCMFlag        , false,                    m_uiNumPartition * sizeof( *m_pbIPCMFlag ) );
411
412  const UInt numCoeffY    = g_uiMaxCUWidth*g_uiMaxCUHeight;
413  for (UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
414  {
415    const UInt componentShift = m_pcPic->getComponentScaleX(ComponentID(comp)) + m_pcPic->getComponentScaleY(ComponentID(comp));
416    memset( m_pcTrCoeff[comp], 0, sizeof(TCoeff)* numCoeffY>>componentShift );
417#if ADAPTIVE_QP_SELECTION
418    memset( m_pcArlCoeff[comp], 0, sizeof(TCoeff)* numCoeffY>>componentShift );
419#endif
420  }
421
422  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
423  {
424    m_acCUMvField[i].clearMvField();
425  }
426
427  // Setting neighbor CU
428  m_pCtuLeft        = NULL;
429  m_pCtuAbove       = NULL;
430  m_pCtuAboveLeft   = NULL;
431  m_pCtuAboveRight  = NULL;
432
433
434  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
435  {
436    m_apcCUColocated[i]  = NULL;
437  }
438
439  UInt frameWidthInCtus = pcPic->getFrameWidthInCtus();
440  if ( m_ctuRsAddr % frameWidthInCtus )
441  {
442    m_pCtuLeft = pcPic->getCtu( m_ctuRsAddr - 1 );
443  }
444
445  if ( m_ctuRsAddr / frameWidthInCtus )
446  {
447    m_pCtuAbove = pcPic->getCtu( m_ctuRsAddr - frameWidthInCtus );
448  }
449
450  if ( m_pCtuLeft && m_pCtuAbove )
451  {
452    m_pCtuAboveLeft = pcPic->getCtu( m_ctuRsAddr - frameWidthInCtus - 1 );
453  }
454
455  if ( m_pCtuAbove && ( (m_ctuRsAddr%frameWidthInCtus) < (frameWidthInCtus-1) )  )
456  {
457    m_pCtuAboveRight = pcPic->getCtu( m_ctuRsAddr - frameWidthInCtus + 1 );
458  }
459
460  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
461  {
462    const RefPicList rpl=RefPicList(i);
463    if ( getSlice()->getNumRefIdx( rpl ) > 0 )
464    {
465      m_apcCUColocated[rpl] = getSlice()->getRefPic( rpl, 0)->getCtu( m_ctuRsAddr );
466    }
467  }
468}
469
470
471/** initialize prediction data with enabling sub-CTU-level delta QP
472*\param  uiDepth  depth of the current CU
473*\param  qp     qp for the current CU
474*- set CU width and CU height according to depth
475*- set qp value according to input qp
476*- set last-coded qp value according to input last-coded qp
477*/
478Void TComDataCU::initEstData( const UInt uiDepth, const Int qp, const Bool bTransquantBypass )
479{
480  m_dTotalCost         = MAX_DOUBLE;
481  m_uiTotalDistortion  = 0;
482  m_uiTotalBits        = 0;
483  m_uiTotalBins        = 0;
484
485  UChar uhWidth  = g_uiMaxCUWidth  >> uiDepth;
486  UChar uhHeight = g_uiMaxCUHeight >> uiDepth;
487
488  for (UInt ui = 0; ui < m_uiNumPartition; ui++)
489  {
490    for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
491    {
492      const RefPicList rpl=RefPicList(i);
493      m_apiMVPIdx[rpl][ui]  = -1;
494      m_apiMVPNum[rpl][ui]  = -1;
495    }
496    m_puhDepth  [ui]    = uiDepth;
497    m_puhWidth  [ui]    = uhWidth;
498    m_puhHeight [ui]    = uhHeight;
499    m_puhTrIdx  [ui]    = 0;
500    for(UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
501    {
502      m_crossComponentPredictionAlpha[comp][ui] = 0;
503      m_puhTransformSkip             [comp][ui] = 0;
504      m_explicitRdpcmMode            [comp][ui] = NUMBER_OF_RDPCM_MODES;
505    }
506    m_skipFlag[ui]      = false;
507    m_pePartSize[ui]    = NUMBER_OF_PART_SIZES;
508    m_pePredMode[ui]    = NUMBER_OF_PREDICTION_MODES;
509    m_CUTransquantBypass[ui] = bTransquantBypass;
510    m_pbIPCMFlag[ui]    = 0;
511    m_phQP[ui]          = qp;
512    m_ChromaQpAdj[ui]   = 0;
513    m_pbMergeFlag[ui]   = 0;
514    m_puhMergeIndex[ui] = 0;
515
516    for (UInt ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
517    {
518      m_puhIntraDir[ch][ui] = ((ch==0) ? DC_IDX : 0);
519    }
520
521    m_puhInterDir[ui] = 0;
522    for (UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
523    {
524      m_puhCbf[comp][ui] = 0;
525    }
526  }
527
528  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
529  {
530    m_acCUMvField[i].clearMvField();
531  }
532
533  const UInt numCoeffY = uhWidth*uhHeight;
534
535  for (UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
536  {
537    const ComponentID component = ComponentID(comp);
538    const UInt numCoeff = numCoeffY >> (getPic()->getComponentScaleX(component) + getPic()->getComponentScaleY(component));
539    memset( m_pcTrCoeff[comp],    0, numCoeff * sizeof( TCoeff ) );
540#if ADAPTIVE_QP_SELECTION
541    memset( m_pcArlCoeff[comp],   0, numCoeff * sizeof( TCoeff ) );
542#endif
543    memset( m_pcIPCMSample[comp], 0, numCoeff * sizeof( Pel) );
544  }
545}
546
547
548// initialize Sub partition
549Void TComDataCU::initSubCU( TComDataCU* pcCU, UInt uiPartUnitIdx, UInt uiDepth, Int qp )
550{
551  assert( uiPartUnitIdx<4 );
552
553  UInt uiPartOffset = ( pcCU->getTotalNumPart()>>2 )*uiPartUnitIdx;
554
555  m_pcPic              = pcCU->getPic();
556  m_pcSlice            = m_pcPic->getSlice(m_pcPic->getCurrSliceIdx());
557  m_ctuRsAddr          = pcCU->getCtuRsAddr();
558  m_absZIdxInCtu       = pcCU->getZorderIdxInCtu() + uiPartOffset;
559
560  m_uiCUPelX           = pcCU->getCUPelX() + ( g_uiMaxCUWidth>>uiDepth  )*( uiPartUnitIdx &  1 );
561  m_uiCUPelY           = pcCU->getCUPelY() + ( g_uiMaxCUHeight>>uiDepth  )*( uiPartUnitIdx >> 1 );
562
563  m_dTotalCost         = MAX_DOUBLE;
564  m_uiTotalDistortion  = 0;
565  m_uiTotalBits        = 0;
566  m_uiTotalBins        = 0;
567  m_uiNumPartition     = pcCU->getTotalNumPart() >> 2;
568
569  Int iSizeInUchar = sizeof( UChar  ) * m_uiNumPartition;
570  Int iSizeInBool  = sizeof( Bool   ) * m_uiNumPartition;
571  Int sizeInChar = sizeof( Char  ) * m_uiNumPartition;
572
573  memset( m_phQP,              qp,  sizeInChar );
574  memset( m_pbMergeFlag,        0, iSizeInBool  );
575  memset( m_puhMergeIndex,      0, iSizeInUchar );
576  for (UInt ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
577  {
578    memset( m_puhIntraDir[ch],  ((ch==0) ? DC_IDX : 0), iSizeInUchar );
579  }
580
581  memset( m_puhInterDir,        0, iSizeInUchar );
582  memset( m_puhTrIdx,           0, iSizeInUchar );
583
584  for(UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
585  {
586    memset( m_crossComponentPredictionAlpha[comp], 0, iSizeInUchar );
587    memset( m_puhTransformSkip[comp],              0, iSizeInUchar );
588    memset( m_puhCbf[comp],                        0, iSizeInUchar );
589    memset( m_explicitRdpcmMode[comp],             NUMBER_OF_RDPCM_MODES, iSizeInUchar );
590  }
591
592  memset( m_puhDepth,     uiDepth, iSizeInUchar );
593
594  UChar uhWidth  = g_uiMaxCUWidth  >> uiDepth;
595  UChar uhHeight = g_uiMaxCUHeight >> uiDepth;
596  memset( m_puhWidth,          uhWidth,  iSizeInUchar );
597  memset( m_puhHeight,         uhHeight, iSizeInUchar );
598  memset( m_pbIPCMFlag,        0, iSizeInBool  );
599  for (UInt ui = 0; ui < m_uiNumPartition; ui++)
600  {
601    m_skipFlag[ui]   = false;
602    m_pePartSize[ui] = NUMBER_OF_PART_SIZES;
603    m_pePredMode[ui] = NUMBER_OF_PREDICTION_MODES;
604    m_CUTransquantBypass[ui] = false;
605    m_ChromaQpAdj[ui] = 0;
606
607    for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
608    {
609      const RefPicList rpl=RefPicList(i);
610      m_apiMVPIdx[rpl][ui] = -1;
611      m_apiMVPNum[rpl][ui] = -1;
612    }
613  }
614
615  const UInt numCoeffY    = uhWidth*uhHeight;
616  for (UInt ch=0; ch<MAX_NUM_COMPONENT; ch++)
617  {
618    const UInt componentShift = m_pcPic->getComponentScaleX(ComponentID(ch)) + m_pcPic->getComponentScaleY(ComponentID(ch));
619    memset( m_pcTrCoeff[ch],  0, sizeof(TCoeff)*(numCoeffY>>componentShift) );
620#if ADAPTIVE_QP_SELECTION
621    memset( m_pcArlCoeff[ch], 0, sizeof(TCoeff)*(numCoeffY>>componentShift) );
622#endif
623    memset( m_pcIPCMSample[ch], 0, sizeof(Pel)* (numCoeffY>>componentShift) );
624  }
625
626  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
627  {
628    m_acCUMvField[i].clearMvField();
629  }
630
631  m_pCtuLeft        = pcCU->getCtuLeft();
632  m_pCtuAbove       = pcCU->getCtuAbove();
633  m_pCtuAboveLeft   = pcCU->getCtuAboveLeft();
634  m_pCtuAboveRight  = pcCU->getCtuAboveRight();
635
636  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
637  {
638    m_apcCUColocated[i] = pcCU->getCUColocated(RefPicList(i));
639  }
640}
641
642Void TComDataCU::setOutsideCUPart( UInt uiAbsPartIdx, UInt uiDepth )
643{
644  UInt uiNumPartition = m_uiNumPartition >> (uiDepth << 1);
645  UInt uiSizeInUchar = sizeof( UChar  ) * uiNumPartition;
646
647  UChar uhWidth  = g_uiMaxCUWidth  >> uiDepth;
648  UChar uhHeight = g_uiMaxCUHeight >> uiDepth;
649  memset( m_puhDepth    + uiAbsPartIdx,     uiDepth,  uiSizeInUchar );
650  memset( m_puhWidth    + uiAbsPartIdx,     uhWidth,  uiSizeInUchar );
651  memset( m_puhHeight   + uiAbsPartIdx,     uhHeight, uiSizeInUchar );
652}
653
654// --------------------------------------------------------------------------------------------------------------------
655// Copy
656// --------------------------------------------------------------------------------------------------------------------
657
658Void TComDataCU::copySubCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
659{
660  UInt uiPart = uiAbsPartIdx;
661
662  m_pcPic              = pcCU->getPic();
663  m_pcSlice            = pcCU->getSlice();
664  m_ctuRsAddr          = pcCU->getCtuRsAddr();
665  m_absZIdxInCtu       = uiAbsPartIdx;
666
667  m_uiCUPelX           = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
668  m_uiCUPelY           = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
669
670  m_skipFlag=pcCU->getSkipFlag()          + uiPart;
671
672  m_phQP=pcCU->getQP()                    + uiPart;
673  m_ChromaQpAdj = pcCU->getChromaQpAdj()  + uiPart;
674  m_pePartSize = pcCU->getPartitionSize() + uiPart;
675  m_pePredMode=pcCU->getPredictionMode()  + uiPart;
676  m_CUTransquantBypass  = pcCU->getCUTransquantBypass()+uiPart;
677
678  m_pbMergeFlag         = pcCU->getMergeFlag()        + uiPart;
679  m_puhMergeIndex       = pcCU->getMergeIndex()       + uiPart;
680
681  for (UInt ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
682  {
683    m_puhIntraDir[ch]   = pcCU->getIntraDir(ChannelType(ch)) + uiPart;
684  }
685
686  m_puhInterDir         = pcCU->getInterDir()         + uiPart;
687  m_puhTrIdx            = pcCU->getTransformIdx()     + uiPart;
688
689  for(UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
690  {
691    m_crossComponentPredictionAlpha[comp] = pcCU->getCrossComponentPredictionAlpha(ComponentID(comp)) + uiPart;
692    m_puhTransformSkip[comp]              = pcCU->getTransformSkip(ComponentID(comp))                 + uiPart;
693    m_puhCbf[comp]                        = pcCU->getCbf(ComponentID(comp))                           + uiPart;
694    m_explicitRdpcmMode[comp]             = pcCU->getExplicitRdpcmMode(ComponentID(comp))             + uiPart;
695  }
696
697  m_puhDepth=pcCU->getDepth()                     + uiPart;
698  m_puhWidth=pcCU->getWidth()                     + uiPart;
699  m_puhHeight=pcCU->getHeight()                   + uiPart;
700
701  m_pbIPCMFlag         = pcCU->getIPCMFlag()        + uiPart;
702
703  m_pCtuAboveLeft      = pcCU->getCtuAboveLeft();
704  m_pCtuAboveRight     = pcCU->getCtuAboveRight();
705  m_pCtuAbove          = pcCU->getCtuAbove();
706  m_pCtuLeft           = pcCU->getCtuLeft();
707
708  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
709  {
710    const RefPicList rpl=RefPicList(i);
711    m_apcCUColocated[rpl] = pcCU->getCUColocated(rpl);
712    m_apiMVPIdx[rpl]=pcCU->getMVPIdx(rpl)  + uiPart;
713    m_apiMVPNum[rpl]=pcCU->getMVPNum(rpl)  + uiPart;
714  }
715
716  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
717  {
718    const RefPicList rpl=RefPicList(i);
719    m_acCUMvField[rpl].linkToWithOffset( pcCU->getCUMvField(rpl), uiPart );
720  }
721
722  UInt uiMaxCuWidth=pcCU->getSlice()->getSPS()->getMaxCUWidth();
723  UInt uiMaxCuHeight=pcCU->getSlice()->getSPS()->getMaxCUHeight();
724
725  UInt uiCoffOffset = uiMaxCuWidth*uiMaxCuHeight*uiAbsPartIdx/pcCU->getPic()->getNumPartitionsInCtu();
726
727  for (UInt ch=0; ch<MAX_NUM_COMPONENT; ch++)
728  {
729    const ComponentID component = ComponentID(ch);
730    const UInt componentShift   = m_pcPic->getComponentScaleX(component) + m_pcPic->getComponentScaleY(component);
731    const UInt offset           = uiCoffOffset >> componentShift;
732    m_pcTrCoeff[ch] = pcCU->getCoeff(component) + offset;
733#if ADAPTIVE_QP_SELECTION
734    m_pcArlCoeff[ch] = pcCU->getArlCoeff(component) + offset;
735#endif
736    m_pcIPCMSample[ch] = pcCU->getPCMSample(component) + offset;
737  }
738}
739
740// Copy inter prediction info from the biggest CU
741Void TComDataCU::copyInterPredInfoFrom    ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefPicList )
742{
743  m_pcPic              = pcCU->getPic();
744  m_pcSlice            = pcCU->getSlice();
745  m_ctuRsAddr          = pcCU->getCtuRsAddr();
746  m_absZIdxInCtu       = uiAbsPartIdx;
747
748  Int iRastPartIdx     = g_auiZscanToRaster[uiAbsPartIdx];
749  m_uiCUPelX           = pcCU->getCUPelX() + m_pcPic->getMinCUWidth ()*( iRastPartIdx % m_pcPic->getNumPartInCtuWidth() );
750  m_uiCUPelY           = pcCU->getCUPelY() + m_pcPic->getMinCUHeight()*( iRastPartIdx / m_pcPic->getNumPartInCtuWidth() );
751
752  m_pCtuAboveLeft      = pcCU->getCtuAboveLeft();
753  m_pCtuAboveRight     = pcCU->getCtuAboveRight();
754  m_pCtuAbove          = pcCU->getCtuAbove();
755  m_pCtuLeft           = pcCU->getCtuLeft();
756
757  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
758  {
759    m_apcCUColocated[i]  = pcCU->getCUColocated(RefPicList(i));
760  }
761
762  m_skipFlag           = pcCU->getSkipFlag ()             + uiAbsPartIdx;
763
764  m_pePartSize         = pcCU->getPartitionSize ()        + uiAbsPartIdx;
765  m_pePredMode         = pcCU->getPredictionMode()        + uiAbsPartIdx;
766  m_ChromaQpAdj        = pcCU->getChromaQpAdj()           + uiAbsPartIdx;
767  m_CUTransquantBypass = pcCU->getCUTransquantBypass()    + uiAbsPartIdx;
768  m_puhInterDir        = pcCU->getInterDir      ()        + uiAbsPartIdx;
769
770  m_puhDepth           = pcCU->getDepth ()                + uiAbsPartIdx;
771  m_puhWidth           = pcCU->getWidth ()                + uiAbsPartIdx;
772  m_puhHeight          = pcCU->getHeight()                + uiAbsPartIdx;
773
774  m_pbMergeFlag        = pcCU->getMergeFlag()             + uiAbsPartIdx;
775  m_puhMergeIndex      = pcCU->getMergeIndex()            + uiAbsPartIdx;
776
777  m_apiMVPIdx[eRefPicList] = pcCU->getMVPIdx(eRefPicList) + uiAbsPartIdx;
778  m_apiMVPNum[eRefPicList] = pcCU->getMVPNum(eRefPicList) + uiAbsPartIdx;
779
780  m_acCUMvField[ eRefPicList ].linkToWithOffset( pcCU->getCUMvField(eRefPicList), uiAbsPartIdx );
781}
782
783// Copy small CU to bigger CU.
784// One of quarter parts overwritten by predicted sub part.
785Void TComDataCU::copyPartFrom( TComDataCU* pcCU, UInt uiPartUnitIdx, UInt uiDepth )
786{
787  assert( uiPartUnitIdx<4 );
788
789  m_dTotalCost         += pcCU->getTotalCost();
790  m_uiTotalDistortion  += pcCU->getTotalDistortion();
791  m_uiTotalBits        += pcCU->getTotalBits();
792
793  UInt uiOffset         = pcCU->getTotalNumPart()*uiPartUnitIdx;
794  const UInt numValidComp=pcCU->getPic()->getNumberValidComponents();
795  const UInt numValidChan=pcCU->getPic()->getChromaFormat()==CHROMA_400 ? 1:2;
796
797  UInt uiNumPartition = pcCU->getTotalNumPart();
798  Int iSizeInUchar  = sizeof( UChar ) * uiNumPartition;
799  Int iSizeInBool   = sizeof( Bool  ) * uiNumPartition;
800
801  Int sizeInChar  = sizeof( Char ) * uiNumPartition;
802  memcpy( m_skipFlag   + uiOffset, pcCU->getSkipFlag(),       sizeof( *m_skipFlag )   * uiNumPartition );
803  memcpy( m_phQP       + uiOffset, pcCU->getQP(),             sizeInChar                        );
804  memcpy( m_pePartSize + uiOffset, pcCU->getPartitionSize(),  sizeof( *m_pePartSize ) * uiNumPartition );
805  memcpy( m_pePredMode + uiOffset, pcCU->getPredictionMode(), sizeof( *m_pePredMode ) * uiNumPartition );
806  memcpy( m_ChromaQpAdj + uiOffset, pcCU->getChromaQpAdj(),   sizeof( *m_ChromaQpAdj ) * uiNumPartition );
807  memcpy( m_CUTransquantBypass + uiOffset, pcCU->getCUTransquantBypass(), sizeof( *m_CUTransquantBypass ) * uiNumPartition );
808  memcpy( m_pbMergeFlag         + uiOffset, pcCU->getMergeFlag(),         iSizeInBool  );
809  memcpy( m_puhMergeIndex       + uiOffset, pcCU->getMergeIndex(),        iSizeInUchar );
810
811  for (UInt ch=0; ch<numValidChan; ch++)
812  {
813    memcpy( m_puhIntraDir[ch]   + uiOffset, pcCU->getIntraDir(ChannelType(ch)), iSizeInUchar );
814  }
815
816  memcpy( m_puhInterDir         + uiOffset, pcCU->getInterDir(),          iSizeInUchar );
817  memcpy( m_puhTrIdx            + uiOffset, pcCU->getTransformIdx(),      iSizeInUchar );
818
819  for(UInt comp=0; comp<numValidComp; comp++)
820  {
821    memcpy( m_crossComponentPredictionAlpha[comp] + uiOffset, pcCU->getCrossComponentPredictionAlpha(ComponentID(comp)), iSizeInUchar );
822    memcpy( m_puhTransformSkip[comp]              + uiOffset, pcCU->getTransformSkip(ComponentID(comp))                , iSizeInUchar );
823    memcpy( m_puhCbf[comp]                        + uiOffset, pcCU->getCbf(ComponentID(comp))                          , iSizeInUchar );
824    memcpy( m_explicitRdpcmMode[comp]             + uiOffset, pcCU->getExplicitRdpcmMode(ComponentID(comp))            , iSizeInUchar );
825  }
826
827  memcpy( m_puhDepth  + uiOffset, pcCU->getDepth(),  iSizeInUchar );
828  memcpy( m_puhWidth  + uiOffset, pcCU->getWidth(),  iSizeInUchar );
829  memcpy( m_puhHeight + uiOffset, pcCU->getHeight(), iSizeInUchar );
830
831  memcpy( m_pbIPCMFlag + uiOffset, pcCU->getIPCMFlag(), iSizeInBool );
832
833  m_pCtuAboveLeft      = pcCU->getCtuAboveLeft();
834  m_pCtuAboveRight     = pcCU->getCtuAboveRight();
835  m_pCtuAbove          = pcCU->getCtuAbove();
836  m_pCtuLeft           = pcCU->getCtuLeft();
837
838  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
839  {
840    const RefPicList rpl=RefPicList(i);
841    memcpy( m_apiMVPIdx[rpl] + uiOffset, pcCU->getMVPIdx(rpl), iSizeInUchar );
842    memcpy( m_apiMVPNum[rpl] + uiOffset, pcCU->getMVPNum(rpl), iSizeInUchar );
843    m_apcCUColocated[rpl] = pcCU->getCUColocated(rpl);
844  }
845
846  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
847  {
848    const RefPicList rpl=RefPicList(i);
849    m_acCUMvField[rpl].copyFrom( pcCU->getCUMvField( rpl ), pcCU->getTotalNumPart(), uiOffset );
850  }
851
852  const UInt numCoeffY = g_uiMaxCUWidth*g_uiMaxCUHeight >> (uiDepth<<1);
853  const UInt offsetY   = uiPartUnitIdx*numCoeffY;
854  for (UInt ch=0; ch<numValidComp; ch++)
855  {
856    const ComponentID component = ComponentID(ch);
857    const UInt componentShift   = m_pcPic->getComponentScaleX(component) + m_pcPic->getComponentScaleY(component);
858    const UInt offset           = offsetY>>componentShift;
859    memcpy( m_pcTrCoeff [ch] + offset, pcCU->getCoeff(component),    sizeof(TCoeff)*(numCoeffY>>componentShift) );
860#if ADAPTIVE_QP_SELECTION
861    memcpy( m_pcArlCoeff[ch] + offset, pcCU->getArlCoeff(component), sizeof(TCoeff)*(numCoeffY>>componentShift) );
862#endif
863    memcpy( m_pcIPCMSample[ch] + offset, pcCU->getPCMSample(component), sizeof(Pel)*(numCoeffY>>componentShift) );
864  }
865
866  m_uiTotalBins += pcCU->getTotalBins();
867}
868
869// Copy current predicted part to a CU in picture.
870// It is used to predict for next part
871Void TComDataCU::copyToPic( UChar uhDepth )
872{
873  TComDataCU* pCtu = m_pcPic->getCtu( m_ctuRsAddr );
874  const UInt numValidComp=pCtu->getPic()->getNumberValidComponents();
875  const UInt numValidChan=pCtu->getPic()->getChromaFormat()==CHROMA_400 ? 1:2;
876
877  pCtu->getTotalCost()       = m_dTotalCost;
878  pCtu->getTotalDistortion() = m_uiTotalDistortion;
879  pCtu->getTotalBits()       = m_uiTotalBits;
880
881  Int iSizeInUchar  = sizeof( UChar ) * m_uiNumPartition;
882  Int iSizeInBool   = sizeof( Bool  ) * m_uiNumPartition;
883  Int sizeInChar  = sizeof( Char ) * m_uiNumPartition;
884
885  memcpy( pCtu->getSkipFlag() + m_absZIdxInCtu, m_skipFlag, sizeof( *m_skipFlag ) * m_uiNumPartition );
886
887  memcpy( pCtu->getQP() + m_absZIdxInCtu, m_phQP, sizeInChar  );
888
889  memcpy( pCtu->getPartitionSize()  + m_absZIdxInCtu, m_pePartSize, sizeof( *m_pePartSize ) * m_uiNumPartition );
890  memcpy( pCtu->getPredictionMode() + m_absZIdxInCtu, m_pePredMode, sizeof( *m_pePredMode ) * m_uiNumPartition );
891  memcpy( pCtu->getChromaQpAdj() + m_absZIdxInCtu, m_ChromaQpAdj, sizeof( *m_ChromaQpAdj ) * m_uiNumPartition );
892  memcpy( pCtu->getCUTransquantBypass()+ m_absZIdxInCtu, m_CUTransquantBypass, sizeof( *m_CUTransquantBypass ) * m_uiNumPartition );
893  memcpy( pCtu->getMergeFlag()         + m_absZIdxInCtu, m_pbMergeFlag,         iSizeInBool  );
894  memcpy( pCtu->getMergeIndex()        + m_absZIdxInCtu, m_puhMergeIndex,       iSizeInUchar );
895  for (UInt ch=0; ch<numValidChan; ch++)
896  {
897    memcpy( pCtu->getIntraDir(ChannelType(ch)) + m_absZIdxInCtu, m_puhIntraDir[ch], iSizeInUchar);
898  }
899
900  memcpy( pCtu->getInterDir()          + m_absZIdxInCtu, m_puhInterDir,         iSizeInUchar );
901  memcpy( pCtu->getTransformIdx()      + m_absZIdxInCtu, m_puhTrIdx,            iSizeInUchar );
902
903  for(UInt comp=0; comp<numValidComp; comp++)
904  {
905    memcpy( pCtu->getCrossComponentPredictionAlpha(ComponentID(comp)) + m_absZIdxInCtu, m_crossComponentPredictionAlpha[comp], iSizeInUchar );
906    memcpy( pCtu->getTransformSkip(ComponentID(comp))                 + m_absZIdxInCtu, m_puhTransformSkip[comp],              iSizeInUchar );
907    memcpy( pCtu->getCbf(ComponentID(comp))                           + m_absZIdxInCtu, m_puhCbf[comp],                        iSizeInUchar );
908    memcpy( pCtu->getExplicitRdpcmMode(ComponentID(comp))             + m_absZIdxInCtu, m_explicitRdpcmMode[comp],             iSizeInUchar );
909  }
910
911  memcpy( pCtu->getDepth()  + m_absZIdxInCtu, m_puhDepth,  iSizeInUchar );
912  memcpy( pCtu->getWidth()  + m_absZIdxInCtu, m_puhWidth,  iSizeInUchar );
913  memcpy( pCtu->getHeight() + m_absZIdxInCtu, m_puhHeight, iSizeInUchar );
914
915  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
916  {
917    const RefPicList rpl=RefPicList(i);
918    memcpy( pCtu->getMVPIdx(rpl) + m_absZIdxInCtu, m_apiMVPIdx[rpl], iSizeInUchar );
919    memcpy( pCtu->getMVPNum(rpl) + m_absZIdxInCtu, m_apiMVPNum[rpl], iSizeInUchar );
920  }
921
922  for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
923  {
924    const RefPicList rpl=RefPicList(i);
925    m_acCUMvField[rpl].copyTo( pCtu->getCUMvField( rpl ), m_absZIdxInCtu );
926  }
927
928  memcpy( pCtu->getIPCMFlag() + m_absZIdxInCtu, m_pbIPCMFlag,         iSizeInBool  );
929
930  const UInt numCoeffY    = (g_uiMaxCUWidth*g_uiMaxCUHeight)>>(uhDepth<<1);
931  const UInt offsetY      = m_absZIdxInCtu*m_pcPic->getMinCUWidth()*m_pcPic->getMinCUHeight();
932  for (UInt comp=0; comp<numValidComp; comp++)
933  {
934    const ComponentID component = ComponentID(comp);
935    const UInt componentShift   = m_pcPic->getComponentScaleX(component) + m_pcPic->getComponentScaleY(component);
936    memcpy( pCtu->getCoeff(component)   + (offsetY>>componentShift), m_pcTrCoeff[component], sizeof(TCoeff)*(numCoeffY>>componentShift) );
937#if ADAPTIVE_QP_SELECTION
938    memcpy( pCtu->getArlCoeff(component) + (offsetY>>componentShift), m_pcArlCoeff[component], sizeof(TCoeff)*(numCoeffY>>componentShift) );
939#endif
940    memcpy( pCtu->getPCMSample(component) + (offsetY>>componentShift), m_pcIPCMSample[component], sizeof(Pel)*(numCoeffY>>componentShift) );
941  }
942
943  pCtu->getTotalBins() = m_uiTotalBins;
944}
945
946// --------------------------------------------------------------------------------------------------------------------
947// Other public functions
948// --------------------------------------------------------------------------------------------------------------------
949
950TComDataCU* TComDataCU::getPULeft( UInt& uiLPartUnitIdx,
951                                   UInt uiCurrPartUnitIdx,
952                                   Bool bEnforceSliceRestriction,
953                                   Bool bEnforceTileRestriction )
954{
955  UInt uiAbsPartIdx       = g_auiZscanToRaster[uiCurrPartUnitIdx];
956  UInt uiAbsZorderCUIdx   = g_auiZscanToRaster[m_absZIdxInCtu];
957  const UInt numPartInCtuWidth = m_pcPic->getNumPartInCtuWidth();
958
959  if ( !RasterAddress::isZeroCol( uiAbsPartIdx, numPartInCtuWidth ) )
960  {
961    uiLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx - 1 ];
962    if ( RasterAddress::isEqualCol( uiAbsPartIdx, uiAbsZorderCUIdx, numPartInCtuWidth ) )
963    {
964      return m_pcPic->getCtu( getCtuRsAddr() );
965    }
966    else
967    {
968      uiLPartUnitIdx -= m_absZIdxInCtu;
969      return this;
970    }
971  }
972
973  uiLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx + numPartInCtuWidth - 1 ];
974  if ( (bEnforceSliceRestriction && !CUIsFromSameSlice(m_pCtuLeft)) || (bEnforceTileRestriction && !CUIsFromSameTile(m_pCtuLeft)) )
975  {
976    return NULL;
977  }
978  return m_pCtuLeft;
979}
980
981
982TComDataCU* TComDataCU::getPUAbove( UInt& uiAPartUnitIdx,
983                                    UInt uiCurrPartUnitIdx,
984                                    Bool bEnforceSliceRestriction,
985                                    Bool planarAtCtuBoundary,
986                                    Bool bEnforceTileRestriction )
987{
988  UInt uiAbsPartIdx       = g_auiZscanToRaster[uiCurrPartUnitIdx];
989  UInt uiAbsZorderCUIdx   = g_auiZscanToRaster[m_absZIdxInCtu];
990  const UInt numPartInCtuWidth = m_pcPic->getNumPartInCtuWidth();
991
992  if ( !RasterAddress::isZeroRow( uiAbsPartIdx, numPartInCtuWidth ) )
993  {
994    uiAPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx - numPartInCtuWidth ];
995    if ( RasterAddress::isEqualRow( uiAbsPartIdx, uiAbsZorderCUIdx, numPartInCtuWidth ) )
996    {
997      return m_pcPic->getCtu( getCtuRsAddr() );
998    }
999    else
1000    {
1001      uiAPartUnitIdx -= m_absZIdxInCtu;
1002      return this;
1003    }
1004  }
1005
1006  if(planarAtCtuBoundary)
1007  {
1008    return NULL;
1009  }
1010
1011  uiAPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx + m_pcPic->getNumPartitionsInCtu() - numPartInCtuWidth ];
1012
1013  if ( (bEnforceSliceRestriction && !CUIsFromSameSlice(m_pCtuAbove)) || (bEnforceTileRestriction && !CUIsFromSameTile(m_pCtuAbove)) )
1014  {
1015    return NULL;
1016  }
1017  return m_pCtuAbove;
1018}
1019
1020TComDataCU* TComDataCU::getPUAboveLeft( UInt& uiALPartUnitIdx, UInt uiCurrPartUnitIdx, Bool bEnforceSliceRestriction )
1021{
1022  UInt uiAbsPartIdx       = g_auiZscanToRaster[uiCurrPartUnitIdx];
1023  UInt uiAbsZorderCUIdx   = g_auiZscanToRaster[m_absZIdxInCtu];
1024  const UInt numPartInCtuWidth = m_pcPic->getNumPartInCtuWidth();
1025
1026  if ( !RasterAddress::isZeroCol( uiAbsPartIdx, numPartInCtuWidth ) )
1027  {
1028    if ( !RasterAddress::isZeroRow( uiAbsPartIdx, numPartInCtuWidth ) )
1029    {
1030      uiALPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx - numPartInCtuWidth - 1 ];
1031      if ( RasterAddress::isEqualRowOrCol( uiAbsPartIdx, uiAbsZorderCUIdx, numPartInCtuWidth ) )
1032      {
1033        return m_pcPic->getCtu( getCtuRsAddr() );
1034      }
1035      else
1036      {
1037        uiALPartUnitIdx -= m_absZIdxInCtu;
1038        return this;
1039      }
1040    }
1041    uiALPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx + getPic()->getNumPartitionsInCtu() - numPartInCtuWidth - 1 ];
1042    if ( bEnforceSliceRestriction && !CUIsFromSameSliceAndTile(m_pCtuAbove) )
1043    {
1044      return NULL;
1045    }
1046    return m_pCtuAbove;
1047  }
1048
1049  if ( !RasterAddress::isZeroRow( uiAbsPartIdx, numPartInCtuWidth ) )
1050  {
1051    uiALPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdx - 1 ];
1052    if ( bEnforceSliceRestriction && !CUIsFromSameSliceAndTile(m_pCtuLeft) )
1053    {
1054      return NULL;
1055    }
1056    return m_pCtuLeft;
1057  }
1058
1059  uiALPartUnitIdx = g_auiRasterToZscan[ m_pcPic->getNumPartitionsInCtu() - 1 ];
1060  if ( bEnforceSliceRestriction && !CUIsFromSameSliceAndTile(m_pCtuAboveLeft) )
1061  {
1062    return NULL;
1063  }
1064  return m_pCtuAboveLeft;
1065}
1066
1067TComDataCU* TComDataCU::getPUAboveRight( UInt& uiARPartUnitIdx, UInt uiCurrPartUnitIdx, Bool bEnforceSliceRestriction )
1068{
1069  UInt uiAbsPartIdxRT     = g_auiZscanToRaster[uiCurrPartUnitIdx];
1070  UInt uiAbsZorderCUIdx   = g_auiZscanToRaster[ m_absZIdxInCtu ] + m_puhWidth[0] / m_pcPic->getMinCUWidth() - 1;
1071  const UInt numPartInCtuWidth = m_pcPic->getNumPartInCtuWidth();
1072 
1073#if SVC_EXTENSION
1074  if( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxRT] + m_pcPic->getMinCUWidth() ) >= m_pcSlice->getPicWidthInLumaSamples() )
1075#else
1076  if( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxRT] + m_pcPic->getMinCUWidth() ) >= m_pcSlice->getSPS()->getPicWidthInLumaSamples() )
1077#endif
1078  {
1079    uiARPartUnitIdx = MAX_UINT;
1080    return NULL;
1081  }
1082
1083  if ( RasterAddress::lessThanCol( uiAbsPartIdxRT, numPartInCtuWidth - 1, numPartInCtuWidth ) )
1084  {
1085    if ( !RasterAddress::isZeroRow( uiAbsPartIdxRT, numPartInCtuWidth ) )
1086    {
1087      if ( uiCurrPartUnitIdx > g_auiRasterToZscan[ uiAbsPartIdxRT - numPartInCtuWidth + 1 ] )
1088      {
1089        uiARPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxRT - numPartInCtuWidth + 1 ];
1090        if ( RasterAddress::isEqualRowOrCol( uiAbsPartIdxRT, uiAbsZorderCUIdx, numPartInCtuWidth ) )
1091        {
1092          return m_pcPic->getCtu( getCtuRsAddr() );
1093        }
1094        else
1095        {
1096          uiARPartUnitIdx -= m_absZIdxInCtu;
1097          return this;
1098        }
1099      }
1100      uiARPartUnitIdx = MAX_UINT;
1101      return NULL;
1102    }
1103    uiARPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxRT + m_pcPic->getNumPartitionsInCtu() - numPartInCtuWidth + 1 ];
1104
1105    if ( bEnforceSliceRestriction && !CUIsFromSameSliceAndTile(m_pCtuAbove) )
1106    {
1107      return NULL;
1108    }
1109    return m_pCtuAbove;
1110  }
1111
1112  if ( !RasterAddress::isZeroRow( uiAbsPartIdxRT, numPartInCtuWidth ) )
1113  {
1114    uiARPartUnitIdx = MAX_UINT;
1115    return NULL;
1116  }
1117
1118  uiARPartUnitIdx = g_auiRasterToZscan[ m_pcPic->getNumPartitionsInCtu() - numPartInCtuWidth ];
1119
1120  if ( bEnforceSliceRestriction && !CUIsFromSameSliceAndTile(m_pCtuAboveRight) )
1121  {
1122    return NULL;
1123  }
1124  return m_pCtuAboveRight;
1125}
1126
1127TComDataCU* TComDataCU::getPUBelowLeft( UInt& uiBLPartUnitIdx, UInt uiCurrPartUnitIdx, Bool bEnforceSliceRestriction )
1128{
1129  UInt uiAbsPartIdxLB     = g_auiZscanToRaster[uiCurrPartUnitIdx];
1130  const UInt numPartInCtuWidth = m_pcPic->getNumPartInCtuWidth();
1131  UInt uiAbsZorderCUIdxLB = g_auiZscanToRaster[ m_absZIdxInCtu ] + (m_puhHeight[0] / m_pcPic->getMinCUHeight() - 1)*numPartInCtuWidth;
1132
1133#if SVC_EXTENSION
1134  if( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxLB] + m_pcPic->getMinCUHeight() ) >= m_pcSlice->getPicHeightInLumaSamples() )
1135#else
1136  if( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxLB] + m_pcPic->getMinCUHeight() ) >= m_pcSlice->getSPS()->getPicHeightInLumaSamples() )
1137#endif
1138  {
1139    uiBLPartUnitIdx = MAX_UINT;
1140    return NULL;
1141  }
1142
1143  if ( RasterAddress::lessThanRow( uiAbsPartIdxLB, m_pcPic->getNumPartInCtuHeight() - 1, numPartInCtuWidth ) )
1144  {
1145    if ( !RasterAddress::isZeroCol( uiAbsPartIdxLB, numPartInCtuWidth ) )
1146    {
1147      if ( uiCurrPartUnitIdx > g_auiRasterToZscan[ uiAbsPartIdxLB + numPartInCtuWidth - 1 ] )
1148      {
1149        uiBLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxLB + numPartInCtuWidth - 1 ];
1150        if ( RasterAddress::isEqualRowOrCol( uiAbsPartIdxLB, uiAbsZorderCUIdxLB, numPartInCtuWidth ) )
1151        {
1152          return m_pcPic->getCtu( getCtuRsAddr() );
1153        }
1154        else
1155        {
1156          uiBLPartUnitIdx -= m_absZIdxInCtu;
1157          return this;
1158        }
1159      }
1160      uiBLPartUnitIdx = MAX_UINT;
1161      return NULL;
1162    }
1163    uiBLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxLB + numPartInCtuWidth*2 - 1 ];
1164    if ( bEnforceSliceRestriction && !CUIsFromSameSliceAndTile(m_pCtuLeft) )
1165    {
1166      return NULL;
1167    }
1168    return m_pCtuLeft;
1169  }
1170
1171  uiBLPartUnitIdx = MAX_UINT;
1172  return NULL;
1173}
1174
1175TComDataCU* TComDataCU::getPUBelowLeftAdi(UInt& uiBLPartUnitIdx,  UInt uiCurrPartUnitIdx, UInt uiPartUnitOffset, Bool bEnforceSliceRestriction)
1176{
1177  UInt uiAbsPartIdxLB     = g_auiZscanToRaster[uiCurrPartUnitIdx];
1178  const UInt numPartInCtuWidth = m_pcPic->getNumPartInCtuWidth();
1179  UInt uiAbsZorderCUIdxLB = g_auiZscanToRaster[ m_absZIdxInCtu ] + ((m_puhHeight[0] / m_pcPic->getMinCUHeight()) - 1)*numPartInCtuWidth;
1180 
1181#if SVC_EXTENSION
1182  if( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxLB] + (m_pcPic->getPicSym()->getMinCUHeight() * uiPartUnitOffset)) >= m_pcSlice->getPicHeightInLumaSamples())
1183#else
1184  if( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxLB] + (m_pcPic->getPicSym()->getMinCUHeight() * uiPartUnitOffset)) >= m_pcSlice->getSPS()->getPicHeightInLumaSamples())
1185#endif
1186  {
1187    uiBLPartUnitIdx = MAX_UINT;
1188    return NULL;
1189  }
1190
1191  if ( RasterAddress::lessThanRow( uiAbsPartIdxLB, m_pcPic->getNumPartInCtuHeight() - uiPartUnitOffset, numPartInCtuWidth ) )
1192  {
1193    if ( !RasterAddress::isZeroCol( uiAbsPartIdxLB, numPartInCtuWidth ) )
1194    {
1195      if ( uiCurrPartUnitIdx > g_auiRasterToZscan[ uiAbsPartIdxLB + uiPartUnitOffset * numPartInCtuWidth - 1 ] )
1196      {
1197        uiBLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxLB + uiPartUnitOffset * numPartInCtuWidth - 1 ];
1198        if ( RasterAddress::isEqualRowOrCol( uiAbsPartIdxLB, uiAbsZorderCUIdxLB, numPartInCtuWidth ) )
1199        {
1200          return m_pcPic->getCtu( getCtuRsAddr() );
1201        }
1202        else
1203        {
1204          uiBLPartUnitIdx -= m_absZIdxInCtu;
1205          return this;
1206        }
1207      }
1208      uiBLPartUnitIdx = MAX_UINT;
1209      return NULL;
1210    }
1211    uiBLPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxLB + (1+uiPartUnitOffset) * numPartInCtuWidth - 1 ];
1212    if ( bEnforceSliceRestriction && !CUIsFromSameSliceAndTile(m_pCtuLeft) )
1213    {
1214      return NULL;
1215    }
1216    return m_pCtuLeft;
1217  }
1218
1219  uiBLPartUnitIdx = MAX_UINT;
1220  return NULL;
1221}
1222
1223TComDataCU* TComDataCU::getPUAboveRightAdi(UInt&  uiARPartUnitIdx, UInt uiCurrPartUnitIdx, UInt uiPartUnitOffset, Bool bEnforceSliceRestriction)
1224{
1225  UInt uiAbsPartIdxRT     = g_auiZscanToRaster[uiCurrPartUnitIdx];
1226  UInt uiAbsZorderCUIdx   = g_auiZscanToRaster[ m_absZIdxInCtu ] + (m_puhWidth[0] / m_pcPic->getMinCUWidth()) - 1;
1227  const UInt numPartInCtuWidth = m_pcPic->getNumPartInCtuWidth();
1228 
1229#if SVC_EXTENSION
1230  if( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxRT] + (m_pcPic->getPicSym()->getMinCUHeight() * uiPartUnitOffset)) >= m_pcSlice->getPicWidthInLumaSamples() )
1231#else
1232  if( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxRT] + (m_pcPic->getPicSym()->getMinCUHeight() * uiPartUnitOffset)) >= m_pcSlice->getSPS()->getPicWidthInLumaSamples() )
1233#endif
1234  {
1235    uiARPartUnitIdx = MAX_UINT;
1236    return NULL;
1237  }
1238
1239  if ( RasterAddress::lessThanCol( uiAbsPartIdxRT, numPartInCtuWidth - uiPartUnitOffset, numPartInCtuWidth ) )
1240  {
1241    if ( !RasterAddress::isZeroRow( uiAbsPartIdxRT, numPartInCtuWidth ) )
1242    {
1243      if ( uiCurrPartUnitIdx > g_auiRasterToZscan[ uiAbsPartIdxRT - numPartInCtuWidth + uiPartUnitOffset ] )
1244      {
1245        uiARPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxRT - numPartInCtuWidth + uiPartUnitOffset ];
1246        if ( RasterAddress::isEqualRowOrCol( uiAbsPartIdxRT, uiAbsZorderCUIdx, numPartInCtuWidth ) )
1247        {
1248          return m_pcPic->getCtu( getCtuRsAddr() );
1249        }
1250        else
1251        {
1252          uiARPartUnitIdx -= m_absZIdxInCtu;
1253          return this;
1254        }
1255      }
1256      uiARPartUnitIdx = MAX_UINT;
1257      return NULL;
1258    }
1259
1260    uiARPartUnitIdx = g_auiRasterToZscan[ uiAbsPartIdxRT + m_pcPic->getNumPartitionsInCtu() - numPartInCtuWidth + uiPartUnitOffset ];
1261    if ( bEnforceSliceRestriction && !CUIsFromSameSliceAndTile(m_pCtuAbove) )
1262    {
1263      return NULL;
1264    }
1265    return m_pCtuAbove;
1266  }
1267
1268  if ( !RasterAddress::isZeroRow( uiAbsPartIdxRT, numPartInCtuWidth ) )
1269  {
1270    uiARPartUnitIdx = MAX_UINT;
1271    return NULL;
1272  }
1273
1274  uiARPartUnitIdx = g_auiRasterToZscan[ m_pcPic->getNumPartitionsInCtu() - numPartInCtuWidth + uiPartUnitOffset-1 ];
1275  if ( bEnforceSliceRestriction && !CUIsFromSameSliceAndTile(m_pCtuAboveRight) )
1276  {
1277    return NULL;
1278  }
1279  return m_pCtuAboveRight;
1280}
1281
1282/** Get left QpMinCu
1283*\param   uiLPartUnitIdx
1284*\param   uiCurrAbsIdxInCtu
1285*\returns TComDataCU*   point of TComDataCU of left QpMinCu
1286*/
1287TComDataCU* TComDataCU::getQpMinCuLeft( UInt& uiLPartUnitIdx, UInt uiCurrAbsIdxInCtu )
1288{
1289  const UInt numPartInCtuWidth = m_pcPic->getNumPartInCtuWidth();
1290  UInt absZorderQpMinCUIdx = (uiCurrAbsIdxInCtu>>((g_uiMaxCUDepth - getSlice()->getPPS()->getMaxCuDQPDepth())<<1))<<((g_uiMaxCUDepth -getSlice()->getPPS()->getMaxCuDQPDepth())<<1);
1291  UInt absRorderQpMinCUIdx = g_auiZscanToRaster[absZorderQpMinCUIdx];
1292
1293  // check for left CTU boundary
1294  if ( RasterAddress::isZeroCol(absRorderQpMinCUIdx, numPartInCtuWidth) )
1295  {
1296    return NULL;
1297  }
1298
1299  // get index of left-CU relative to top-left corner of current quantization group
1300  uiLPartUnitIdx = g_auiRasterToZscan[absRorderQpMinCUIdx - 1];
1301
1302  // return pointer to current CTU
1303  return m_pcPic->getCtu( getCtuRsAddr() );
1304}
1305
1306/** Get Above QpMinCu
1307*\param   uiAPartUnitIdx
1308*\param   uiCurrAbsIdxInCtu
1309*\returns TComDataCU*   point of TComDataCU of above QpMinCu
1310*/
1311TComDataCU* TComDataCU::getQpMinCuAbove( UInt& uiAPartUnitIdx, UInt uiCurrAbsIdxInCtu )
1312{
1313  const UInt numPartInCtuWidth = m_pcPic->getNumPartInCtuWidth();
1314  UInt absZorderQpMinCUIdx = (uiCurrAbsIdxInCtu>>((g_uiMaxCUDepth - getSlice()->getPPS()->getMaxCuDQPDepth())<<1))<<((g_uiMaxCUDepth - getSlice()->getPPS()->getMaxCuDQPDepth())<<1);
1315  UInt absRorderQpMinCUIdx = g_auiZscanToRaster[absZorderQpMinCUIdx];
1316
1317  // check for top CTU boundary
1318  if ( RasterAddress::isZeroRow( absRorderQpMinCUIdx, numPartInCtuWidth) )
1319  {
1320    return NULL;
1321  }
1322
1323  // get index of top-CU relative to top-left corner of current quantization group
1324  uiAPartUnitIdx = g_auiRasterToZscan[absRorderQpMinCUIdx - numPartInCtuWidth];
1325
1326  // return pointer to current CTU
1327  return m_pcPic->getCtu( getCtuRsAddr() );
1328}
1329
1330
1331
1332/** Get reference QP from left QpMinCu or latest coded QP
1333*\param   uiCurrAbsIdxInCtu
1334*\returns Char   reference QP value
1335*/
1336Char TComDataCU::getRefQP( UInt uiCurrAbsIdxInCtu )
1337{
1338  UInt lPartIdx = MAX_UINT;
1339  UInt aPartIdx = MAX_UINT;
1340  TComDataCU* cULeft  = getQpMinCuLeft ( lPartIdx, m_absZIdxInCtu + uiCurrAbsIdxInCtu );
1341  TComDataCU* cUAbove = getQpMinCuAbove( aPartIdx, m_absZIdxInCtu + uiCurrAbsIdxInCtu );
1342  return (((cULeft? cULeft->getQP( lPartIdx ): getLastCodedQP( uiCurrAbsIdxInCtu )) + (cUAbove? cUAbove->getQP( aPartIdx ): getLastCodedQP( uiCurrAbsIdxInCtu )) + 1) >> 1);
1343}
1344
1345Int TComDataCU::getLastValidPartIdx( Int iAbsPartIdx )
1346{
1347  Int iLastValidPartIdx = iAbsPartIdx-1;
1348  while ( iLastValidPartIdx >= 0
1349       && getPredictionMode( iLastValidPartIdx ) == NUMBER_OF_PREDICTION_MODES )
1350  {
1351    UInt uiDepth = getDepth( iLastValidPartIdx );
1352    iLastValidPartIdx -= m_uiNumPartition>>(uiDepth<<1);
1353  }
1354  return iLastValidPartIdx;
1355}
1356
1357Char TComDataCU::getLastCodedQP( UInt uiAbsPartIdx )
1358{
1359  UInt uiQUPartIdxMask = ~((1<<((g_uiMaxCUDepth - getSlice()->getPPS()->getMaxCuDQPDepth())<<1))-1);
1360  Int iLastValidPartIdx = getLastValidPartIdx( uiAbsPartIdx&uiQUPartIdxMask ); // A idx will be invalid if it is off the right or bottom edge of the picture.
1361  // If this CU is in the first CTU of the slice and there is no valid part before this one, use slice QP
1362  if ( getPic()->getPicSym()->getCtuTsToRsAddrMap(getSlice()->getSliceCurStartCtuTsAddr()) == getCtuRsAddr() && Int(getZorderIdxInCtu())+iLastValidPartIdx<0)
1363  {
1364    return getSlice()->getSliceQp();
1365  }
1366  else if ( iLastValidPartIdx >= 0 )
1367  {
1368    // If there is a valid part within the current Sub-CU, use it
1369    return getQP( iLastValidPartIdx );
1370  }
1371  else
1372  {
1373    if ( getZorderIdxInCtu() > 0 )
1374    {
1375      // If this wasn't the first sub-cu within the Ctu, explore the CTU itself.
1376      return getPic()->getCtu( getCtuRsAddr() )->getLastCodedQP( getZorderIdxInCtu() ); // TODO - remove this recursion
1377    }
1378    else if ( getPic()->getPicSym()->getCtuRsToTsAddrMap(getCtuRsAddr()) > 0
1379      && CUIsFromSameSliceTileAndWavefrontRow(getPic()->getCtu(getPic()->getPicSym()->getCtuTsToRsAddrMap(getPic()->getPicSym()->getCtuRsToTsAddrMap(getCtuRsAddr())-1))) )
1380    {
1381      // If this isn't the first Ctu (how can it be due to the first 'if'?), and the previous Ctu is from the same tile, examine the previous Ctu.
1382      return getPic()->getCtu( getPic()->getPicSym()->getCtuTsToRsAddrMap(getPic()->getPicSym()->getCtuRsToTsAddrMap(getCtuRsAddr())-1) )->getLastCodedQP( getPic()->getNumPartitionsInCtu() );  // TODO - remove this recursion
1383    }
1384    else
1385    {
1386      // No other options available - use the slice-level QP.
1387      return getSlice()->getSliceQp();
1388    }
1389  }
1390}
1391
1392
1393/** Check whether the CU is coded in lossless coding mode
1394 * \param   uiAbsPartIdx
1395 * \returns true if the CU is coded in lossless coding mode; false if otherwise
1396 */
1397Bool TComDataCU::isLosslessCoded(UInt absPartIdx)
1398{
1399  return (getSlice()->getPPS()->getTransquantBypassEnableFlag() && getCUTransquantBypass (absPartIdx));
1400}
1401
1402
1403/** Get allowed chroma intra modes
1404*\param   uiAbsPartIdx
1405*\param   uiModeList  pointer to chroma intra modes array
1406*\returns
1407*- fill uiModeList with chroma intra modes
1408*/
1409Void TComDataCU::getAllowedChromaDir( UInt uiAbsPartIdx, UInt uiModeList[NUM_CHROMA_MODE] )
1410{
1411  uiModeList[0] = PLANAR_IDX;
1412  uiModeList[1] = VER_IDX;
1413  uiModeList[2] = HOR_IDX;
1414  uiModeList[3] = DC_IDX;
1415  uiModeList[4] = DM_CHROMA_IDX;
1416  assert(4<NUM_CHROMA_MODE);
1417
1418  UInt uiLumaMode = getIntraDir( CHANNEL_TYPE_LUMA, uiAbsPartIdx );
1419
1420  for( Int i = 0; i < NUM_CHROMA_MODE - 1; i++ )
1421  {
1422    if( uiLumaMode == uiModeList[i] )
1423    {
1424      uiModeList[i] = 34; // VER+8 mode
1425      break;
1426    }
1427  }
1428}
1429
1430/** Get most probable intra modes
1431*\param   uiAbsPartIdx
1432*\param   uiIntraDirPred  pointer to the array for MPM storage
1433*\param   piMode          it is set with MPM mode in case both MPM are equal. It is used to restrict RD search at encode side.
1434*\returns Number of MPM
1435*/
1436Int TComDataCU::getIntraDirPredictor( UInt uiAbsPartIdx, Int uiIntraDirPred[NUM_MOST_PROBABLE_MODES], const ComponentID compID, Int* piMode  )
1437{
1438  TComDataCU* pcCULeft, *pcCUAbove;
1439  UInt        LeftPartIdx  = MAX_UINT;
1440  UInt        AbovePartIdx = MAX_UINT;
1441  Int         iLeftIntraDir, iAboveIntraDir;
1442  Int         uiPredNum = 0;
1443
1444  const ChannelType chType = toChannelType(compID);
1445  const ChromaFormat chForm = getPic()->getChromaFormat();
1446  // Get intra direction of left PU
1447  pcCULeft = getPULeft( LeftPartIdx, m_absZIdxInCtu + uiAbsPartIdx );
1448
1449  if (isChroma(compID)) LeftPartIdx = getChromasCorrespondingPULumaIdx(LeftPartIdx, chForm);
1450  iLeftIntraDir  = pcCULeft ? ( pcCULeft->isIntra( LeftPartIdx ) ? pcCULeft->getIntraDir( chType, LeftPartIdx ) : DC_IDX ) : DC_IDX;
1451
1452  // Get intra direction of above PU
1453  pcCUAbove = getPUAbove( AbovePartIdx, m_absZIdxInCtu + uiAbsPartIdx, true, true );
1454
1455  if (isChroma(compID)) AbovePartIdx = getChromasCorrespondingPULumaIdx(AbovePartIdx, chForm);
1456  iAboveIntraDir = pcCUAbove ? ( pcCUAbove->isIntra( AbovePartIdx ) ? pcCUAbove->getIntraDir( chType, AbovePartIdx ) : DC_IDX ) : DC_IDX;
1457
1458  if (isChroma(chType))
1459  {
1460    if (iLeftIntraDir  == DM_CHROMA_IDX) iLeftIntraDir  = pcCULeft-> getIntraDir( CHANNEL_TYPE_LUMA, LeftPartIdx  );
1461    if (iAboveIntraDir == DM_CHROMA_IDX) iAboveIntraDir = pcCUAbove->getIntraDir( CHANNEL_TYPE_LUMA, AbovePartIdx );
1462  }
1463
1464  assert (2<NUM_MOST_PROBABLE_MODES);
1465  uiPredNum = NUM_MOST_PROBABLE_MODES;
1466  if(iLeftIntraDir == iAboveIntraDir)
1467  {
1468    if( piMode )
1469    {
1470      *piMode = 1;
1471    }
1472
1473    if (iLeftIntraDir > 1) // angular modes
1474    {
1475      uiIntraDirPred[0] = iLeftIntraDir;
1476      uiIntraDirPred[1] = ((iLeftIntraDir + 29) % 32) + 2;
1477      uiIntraDirPred[2] = ((iLeftIntraDir - 1 ) % 32) + 2;
1478    }
1479    else //non-angular
1480    {
1481      uiIntraDirPred[0] = PLANAR_IDX;
1482      uiIntraDirPred[1] = DC_IDX;
1483      uiIntraDirPred[2] = VER_IDX;
1484    }
1485  }
1486  else
1487  {
1488    if( piMode )
1489    {
1490      *piMode = 2;
1491    }
1492    uiIntraDirPred[0] = iLeftIntraDir;
1493    uiIntraDirPred[1] = iAboveIntraDir;
1494
1495    if (iLeftIntraDir && iAboveIntraDir ) //both modes are non-planar
1496    {
1497      uiIntraDirPred[2] = PLANAR_IDX;
1498    }
1499    else
1500    {
1501      uiIntraDirPred[2] =  (iLeftIntraDir+iAboveIntraDir)<2? VER_IDX : DC_IDX;
1502    }
1503  }
1504  for (Int i=0; i<uiPredNum; i++)
1505    assert(uiIntraDirPred[i] < 35);
1506
1507  return uiPredNum;
1508}
1509
1510UInt TComDataCU::getCtxSplitFlag( UInt uiAbsPartIdx, UInt uiDepth )
1511{
1512  TComDataCU* pcTempCU;
1513  UInt        uiTempPartIdx;
1514  UInt        uiCtx;
1515  // Get left split flag
1516  pcTempCU = getPULeft( uiTempPartIdx, m_absZIdxInCtu + uiAbsPartIdx );
1517  uiCtx  = ( pcTempCU ) ? ( ( pcTempCU->getDepth( uiTempPartIdx ) > uiDepth ) ? 1 : 0 ) : 0;
1518
1519  // Get above split flag
1520  pcTempCU = getPUAbove( uiTempPartIdx, m_absZIdxInCtu + uiAbsPartIdx );
1521  uiCtx += ( pcTempCU ) ? ( ( pcTempCU->getDepth( uiTempPartIdx ) > uiDepth ) ? 1 : 0 ) : 0;
1522
1523  return uiCtx;
1524}
1525
1526UInt TComDataCU::getCtxQtCbf( TComTU &rTu, const ChannelType chType )
1527{
1528  const UInt transformDepth = rTu.GetTransformDepthRel();
1529
1530  if (isChroma(chType))
1531  {
1532    return transformDepth;
1533  }
1534  else
1535  {
1536    const UInt uiCtx = ( transformDepth == 0 ? 1 : 0 );
1537    return uiCtx;
1538  }
1539}
1540
1541UInt TComDataCU::getQuadtreeTULog2MinSizeInCU( UInt absPartIdx )
1542{
1543  UInt log2CbSize = g_aucConvertToBit[getWidth( absPartIdx )] + 2;
1544  PartSize  partSize  = getPartitionSize( absPartIdx );
1545  UInt quadtreeTUMaxDepth = isIntra( absPartIdx ) ? m_pcSlice->getSPS()->getQuadtreeTUMaxDepthIntra() : m_pcSlice->getSPS()->getQuadtreeTUMaxDepthInter();
1546  Int intraSplitFlag = ( isIntra( absPartIdx ) && partSize == SIZE_NxN ) ? 1 : 0;
1547  Int interSplitFlag = ((quadtreeTUMaxDepth == 1) && isInter( absPartIdx ) && (partSize != SIZE_2Nx2N) );
1548
1549  UInt log2MinTUSizeInCU = 0;
1550  if (log2CbSize < (m_pcSlice->getSPS()->getQuadtreeTULog2MinSize() + quadtreeTUMaxDepth - 1 + interSplitFlag + intraSplitFlag) )
1551  {
1552    // when fully making use of signaled TUMaxDepth + inter/intraSplitFlag, resulting luma TB size is < QuadtreeTULog2MinSize
1553    log2MinTUSizeInCU = m_pcSlice->getSPS()->getQuadtreeTULog2MinSize();
1554  }
1555  else
1556  {
1557    // when fully making use of signaled TUMaxDepth + inter/intraSplitFlag, resulting luma TB size is still >= QuadtreeTULog2MinSize
1558    log2MinTUSizeInCU = log2CbSize - ( quadtreeTUMaxDepth - 1 + interSplitFlag + intraSplitFlag); // stop when trafoDepth == hierarchy_depth = splitFlag
1559    if ( log2MinTUSizeInCU > m_pcSlice->getSPS()->getQuadtreeTULog2MaxSize())
1560    {
1561      // when fully making use of signaled TUMaxDepth + inter/intraSplitFlag, resulting luma TB size is still > QuadtreeTULog2MaxSize
1562      log2MinTUSizeInCU = m_pcSlice->getSPS()->getQuadtreeTULog2MaxSize();
1563    }
1564  }
1565  return log2MinTUSizeInCU;
1566}
1567
1568UInt TComDataCU::getCtxSkipFlag( UInt uiAbsPartIdx )
1569{
1570  TComDataCU* pcTempCU;
1571  UInt        uiTempPartIdx;
1572  UInt        uiCtx = 0;
1573
1574  // Get BCBP of left PU
1575  pcTempCU = getPULeft( uiTempPartIdx, m_absZIdxInCtu + uiAbsPartIdx );
1576  uiCtx    = ( pcTempCU ) ? pcTempCU->isSkipped( uiTempPartIdx ) : 0;
1577
1578  // Get BCBP of above PU
1579  pcTempCU = getPUAbove( uiTempPartIdx, m_absZIdxInCtu + uiAbsPartIdx );
1580  uiCtx   += ( pcTempCU ) ? pcTempCU->isSkipped( uiTempPartIdx ) : 0;
1581
1582  return uiCtx;
1583}
1584
1585UInt TComDataCU::getCtxInterDir( UInt uiAbsPartIdx )
1586{
1587  return getDepth( uiAbsPartIdx );
1588}
1589
1590
1591UChar TComDataCU::getQtRootCbf( UInt uiIdx )
1592{
1593  const UInt numberValidComponents = getPic()->getNumberValidComponents();
1594  return getCbf( uiIdx, COMPONENT_Y, 0 )
1595          || ((numberValidComponents > COMPONENT_Cb) && getCbf( uiIdx, COMPONENT_Cb, 0 ))
1596          || ((numberValidComponents > COMPONENT_Cr) && getCbf( uiIdx, COMPONENT_Cr, 0 ));
1597}
1598
1599Void TComDataCU::setCbfSubParts( const UInt uiCbf[MAX_NUM_COMPONENT], UInt uiAbsPartIdx, UInt uiDepth )
1600{
1601  UInt uiCurrPartNumb = m_pcPic->getNumPartitionsInCtu() >> (uiDepth << 1);
1602  for(UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
1603  {
1604    memset( m_puhCbf[comp] + uiAbsPartIdx, uiCbf[comp], sizeof( UChar ) * uiCurrPartNumb );
1605  }
1606}
1607
1608Void TComDataCU::setCbfSubParts( UInt uiCbf, ComponentID compID, UInt uiAbsPartIdx, UInt uiDepth )
1609{
1610  UInt uiCurrPartNumb = m_pcPic->getNumPartitionsInCtu() >> (uiDepth << 1);
1611  memset( m_puhCbf[compID] + uiAbsPartIdx, uiCbf, sizeof( UChar ) * uiCurrPartNumb );
1612}
1613
1614/** Sets a coded block flag for all sub-partitions of a partition
1615 * \param uiCbf The value of the coded block flag to be set
1616 * \param eTType
1617 * \param uiAbsPartIdx
1618 * \param uiPartIdx
1619 * \param uiDepth
1620 * \returns Void
1621 */
1622Void TComDataCU::setCbfSubParts ( UInt uiCbf, ComponentID compID, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
1623{
1624  setSubPart<UChar>( uiCbf, m_puhCbf[compID], uiAbsPartIdx, uiDepth, uiPartIdx );
1625}
1626
1627Void TComDataCU::setCbfPartRange ( UInt uiCbf, ComponentID compID, UInt uiAbsPartIdx, UInt uiCoveredPartIdxes )
1628{
1629  memset((m_puhCbf[compID] + uiAbsPartIdx), uiCbf, (sizeof(UChar) * uiCoveredPartIdxes));
1630}
1631
1632Void TComDataCU::bitwiseOrCbfPartRange( UInt uiCbf, ComponentID compID, UInt uiAbsPartIdx, UInt uiCoveredPartIdxes )
1633{
1634  const UInt stopAbsPartIdx = uiAbsPartIdx + uiCoveredPartIdxes;
1635
1636  for (UInt subPartIdx = uiAbsPartIdx; subPartIdx < stopAbsPartIdx; subPartIdx++)
1637  {
1638    m_puhCbf[compID][subPartIdx] |= uiCbf;
1639  }
1640}
1641
1642Void TComDataCU::setDepthSubParts( UInt uiDepth, UInt uiAbsPartIdx )
1643{
1644  UInt uiCurrPartNumb = m_pcPic->getNumPartitionsInCtu() >> (uiDepth << 1);
1645  memset( m_puhDepth + uiAbsPartIdx, uiDepth, sizeof(UChar)*uiCurrPartNumb );
1646}
1647
1648Bool TComDataCU::isFirstAbsZorderIdxInDepth (UInt uiAbsPartIdx, UInt uiDepth)
1649{
1650  UInt uiPartNumb = m_pcPic->getNumPartitionsInCtu() >> (uiDepth << 1);
1651  return (((m_absZIdxInCtu + uiAbsPartIdx)% uiPartNumb) == 0);
1652}
1653
1654Void TComDataCU::setPartSizeSubParts( PartSize eMode, UInt uiAbsPartIdx, UInt uiDepth )
1655{
1656  assert( sizeof( *m_pePartSize) == 1 );
1657  memset( m_pePartSize + uiAbsPartIdx, eMode, m_pcPic->getNumPartitionsInCtu() >> ( 2 * uiDepth ) );
1658}
1659
1660Void TComDataCU::setCUTransquantBypassSubParts( Bool flag, UInt uiAbsPartIdx, UInt uiDepth )
1661{
1662  memset( m_CUTransquantBypass + uiAbsPartIdx, flag, m_pcPic->getNumPartitionsInCtu() >> ( 2 * uiDepth ) );
1663}
1664
1665Void TComDataCU::setSkipFlagSubParts( Bool skip, UInt absPartIdx, UInt depth )
1666{
1667  assert( sizeof( *m_skipFlag) == 1 );
1668  memset( m_skipFlag + absPartIdx, skip, m_pcPic->getNumPartitionsInCtu() >> ( 2 * depth ) );
1669}
1670
1671Void TComDataCU::setPredModeSubParts( PredMode eMode, UInt uiAbsPartIdx, UInt uiDepth )
1672{
1673  assert( sizeof( *m_pePredMode) == 1 );
1674  memset( m_pePredMode + uiAbsPartIdx, eMode, m_pcPic->getNumPartitionsInCtu() >> ( 2 * uiDepth ) );
1675}
1676
1677Void TComDataCU::setChromaQpAdjSubParts( UChar val, Int absPartIdx, Int depth )
1678{
1679  assert( sizeof(*m_ChromaQpAdj) == 1 );
1680  memset( m_ChromaQpAdj + absPartIdx, val, m_pcPic->getNumPartitionsInCtu() >> ( 2 * depth ) );
1681}
1682
1683Void TComDataCU::setQPSubCUs( Int qp, UInt absPartIdx, UInt depth, Bool &foundNonZeroCbf )
1684{
1685  UInt currPartNumb = m_pcPic->getNumPartitionsInCtu() >> (depth << 1);
1686  UInt currPartNumQ = currPartNumb >> 2;
1687  const UInt numValidComp = m_pcPic->getNumberValidComponents();
1688
1689  if(!foundNonZeroCbf)
1690  {
1691    if(getDepth(absPartIdx) > depth)
1692    {
1693      for ( UInt partUnitIdx = 0; partUnitIdx < 4; partUnitIdx++ )
1694      {
1695        setQPSubCUs( qp, absPartIdx+partUnitIdx*currPartNumQ, depth+1, foundNonZeroCbf );
1696      }
1697    }
1698    else
1699    {
1700      if(getCbf( absPartIdx, COMPONENT_Y ) || (numValidComp>COMPONENT_Cb && getCbf( absPartIdx, COMPONENT_Cb )) || (numValidComp>COMPONENT_Cr && getCbf( absPartIdx, COMPONENT_Cr) ) )
1701      {
1702        foundNonZeroCbf = true;
1703      }
1704      else
1705      {
1706        setQPSubParts(qp, absPartIdx, depth);
1707      }
1708    }
1709  }
1710}
1711
1712Void TComDataCU::setQPSubParts( Int qp, UInt uiAbsPartIdx, UInt uiDepth )
1713{
1714  const UInt numPart = m_pcPic->getNumPartitionsInCtu() >> (uiDepth << 1);
1715  memset(m_phQP+uiAbsPartIdx, qp, numPart);
1716}
1717
1718Void TComDataCU::setIntraDirSubParts( const ChannelType channelType, const UInt dir, const UInt absPartIdx, const UInt depth )
1719{
1720  UInt numPart = m_pcPic->getNumPartitionsInCtu() >> (depth << 1);
1721  memset( m_puhIntraDir[channelType] + absPartIdx, dir,sizeof(UChar)*numPart );
1722}
1723
1724template<typename T>
1725Void TComDataCU::setSubPart( T uiParameter, T* puhBaseCtu, UInt uiCUAddr, UInt uiCUDepth, UInt uiPUIdx )
1726{
1727  assert( sizeof(T) == 1 ); // Using memset() works only for types of size 1
1728
1729  UInt uiCurrPartNumQ = (m_pcPic->getNumPartitionsInCtu() >> (2 * uiCUDepth)) >> 2;
1730  switch ( m_pePartSize[ uiCUAddr ] )
1731  {
1732    case SIZE_2Nx2N:
1733      memset( puhBaseCtu + uiCUAddr, uiParameter, 4 * uiCurrPartNumQ );
1734      break;
1735    case SIZE_2NxN:
1736      memset( puhBaseCtu + uiCUAddr, uiParameter, 2 * uiCurrPartNumQ );
1737      break;
1738    case SIZE_Nx2N:
1739      memset( puhBaseCtu + uiCUAddr, uiParameter, uiCurrPartNumQ );
1740      memset( puhBaseCtu + uiCUAddr + 2 * uiCurrPartNumQ, uiParameter, uiCurrPartNumQ );
1741      break;
1742    case SIZE_NxN:
1743      memset( puhBaseCtu + uiCUAddr, uiParameter, uiCurrPartNumQ );
1744      break;
1745    case SIZE_2NxnU:
1746      if ( uiPUIdx == 0 )
1747      {
1748        memset( puhBaseCtu + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 1) );
1749        memset( puhBaseCtu + uiCUAddr + uiCurrPartNumQ, uiParameter, (uiCurrPartNumQ >> 1) );
1750      }
1751      else if ( uiPUIdx == 1 )
1752      {
1753        memset( puhBaseCtu + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 1) );
1754        memset( puhBaseCtu + uiCUAddr + uiCurrPartNumQ, uiParameter, ((uiCurrPartNumQ >> 1) + (uiCurrPartNumQ << 1)) );
1755      }
1756      else
1757      {
1758        assert(0);
1759      }
1760      break;
1761    case SIZE_2NxnD:
1762      if ( uiPUIdx == 0 )
1763      {
1764        memset( puhBaseCtu + uiCUAddr, uiParameter, ((uiCurrPartNumQ << 1) + (uiCurrPartNumQ >> 1)) );
1765        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ << 1) + uiCurrPartNumQ, uiParameter, (uiCurrPartNumQ >> 1) );
1766      }
1767      else if ( uiPUIdx == 1 )
1768      {
1769        memset( puhBaseCtu + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 1) );
1770        memset( puhBaseCtu + uiCUAddr + uiCurrPartNumQ, uiParameter, (uiCurrPartNumQ >> 1) );
1771      }
1772      else
1773      {
1774        assert(0);
1775      }
1776      break;
1777    case SIZE_nLx2N:
1778      if ( uiPUIdx == 0 )
1779      {
1780        memset( puhBaseCtu + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 2) );
1781        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) );
1782        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ << 1), uiParameter, (uiCurrPartNumQ >> 2) );
1783        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ << 1) + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) );
1784      }
1785      else if ( uiPUIdx == 1 )
1786      {
1787        memset( puhBaseCtu + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 2) );
1788        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ + (uiCurrPartNumQ >> 2)) );
1789        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ << 1), uiParameter, (uiCurrPartNumQ >> 2) );
1790        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ << 1) + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ + (uiCurrPartNumQ >> 2)) );
1791      }
1792      else
1793      {
1794        assert(0);
1795      }
1796      break;
1797    case SIZE_nRx2N:
1798      if ( uiPUIdx == 0 )
1799      {
1800        memset( puhBaseCtu + uiCUAddr, uiParameter, (uiCurrPartNumQ + (uiCurrPartNumQ >> 2)) );
1801        memset( puhBaseCtu + uiCUAddr + uiCurrPartNumQ + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) );
1802        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ << 1), uiParameter, (uiCurrPartNumQ + (uiCurrPartNumQ >> 2)) );
1803        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ << 1) + uiCurrPartNumQ + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) );
1804      }
1805      else if ( uiPUIdx == 1 )
1806      {
1807        memset( puhBaseCtu + uiCUAddr, uiParameter, (uiCurrPartNumQ >> 2) );
1808        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) );
1809        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ << 1), uiParameter, (uiCurrPartNumQ >> 2) );
1810        memset( puhBaseCtu + uiCUAddr + (uiCurrPartNumQ << 1) + (uiCurrPartNumQ >> 1), uiParameter, (uiCurrPartNumQ >> 2) );
1811      }
1812      else
1813      {
1814        assert(0);
1815      }
1816      break;
1817    default:
1818      assert( 0 );
1819      break;
1820  }
1821}
1822
1823Void TComDataCU::setMergeFlagSubParts ( Bool bMergeFlag, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
1824{
1825  setSubPart( bMergeFlag, m_pbMergeFlag, uiAbsPartIdx, uiDepth, uiPartIdx );
1826}
1827
1828Void TComDataCU::setMergeIndexSubParts ( UInt uiMergeIndex, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
1829{
1830  setSubPart<UChar>( uiMergeIndex, m_puhMergeIndex, uiAbsPartIdx, uiDepth, uiPartIdx );
1831}
1832
1833Void TComDataCU::setInterDirSubParts( UInt uiDir, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
1834{
1835  setSubPart<UChar>( uiDir, m_puhInterDir, uiAbsPartIdx, uiDepth, uiPartIdx );
1836}
1837
1838Void TComDataCU::setMVPIdxSubParts( Int iMVPIdx, RefPicList eRefPicList, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
1839{
1840  setSubPart<Char>( iMVPIdx, m_apiMVPIdx[eRefPicList], uiAbsPartIdx, uiDepth, uiPartIdx );
1841}
1842
1843Void TComDataCU::setMVPNumSubParts( Int iMVPNum, RefPicList eRefPicList, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth )
1844{
1845  setSubPart<Char>( iMVPNum, m_apiMVPNum[eRefPicList], uiAbsPartIdx, uiDepth, uiPartIdx );
1846}
1847
1848
1849Void TComDataCU::setTrIdxSubParts( UInt uiTrIdx, UInt uiAbsPartIdx, UInt uiDepth )
1850{
1851  UInt uiCurrPartNumb = m_pcPic->getNumPartitionsInCtu() >> (uiDepth << 1);
1852
1853  memset( m_puhTrIdx + uiAbsPartIdx, uiTrIdx, sizeof(UChar)*uiCurrPartNumb );
1854}
1855
1856Void TComDataCU::setTransformSkipSubParts( const UInt useTransformSkip[MAX_NUM_COMPONENT], UInt uiAbsPartIdx, UInt uiDepth )
1857{
1858  UInt uiCurrPartNumb = m_pcPic->getNumPartitionsInCtu() >> (uiDepth << 1);
1859
1860  for(UInt i=0; i<MAX_NUM_COMPONENT; i++)
1861  {
1862    memset( m_puhTransformSkip[i] + uiAbsPartIdx, useTransformSkip[i], sizeof( UChar ) * uiCurrPartNumb );
1863  }
1864}
1865
1866Void TComDataCU::setTransformSkipSubParts( UInt useTransformSkip, ComponentID compID, UInt uiAbsPartIdx, UInt uiDepth)
1867{
1868  UInt uiCurrPartNumb = m_pcPic->getNumPartitionsInCtu() >> (uiDepth << 1);
1869
1870  memset( m_puhTransformSkip[compID] + uiAbsPartIdx, useTransformSkip, sizeof( UChar ) * uiCurrPartNumb );
1871}
1872
1873Void TComDataCU::setTransformSkipPartRange ( UInt useTransformSkip, ComponentID compID, UInt uiAbsPartIdx, UInt uiCoveredPartIdxes )
1874{
1875  memset((m_puhTransformSkip[compID] + uiAbsPartIdx), useTransformSkip, (sizeof(UChar) * uiCoveredPartIdxes));
1876}
1877
1878Void TComDataCU::setCrossComponentPredictionAlphaPartRange( Char alphaValue, ComponentID compID, UInt uiAbsPartIdx, UInt uiCoveredPartIdxes )
1879{
1880  memset((m_crossComponentPredictionAlpha[compID] + uiAbsPartIdx), alphaValue, (sizeof(Char) * uiCoveredPartIdxes));
1881}
1882
1883Void TComDataCU::setExplicitRdpcmModePartRange ( UInt rdpcmMode, ComponentID compID, UInt uiAbsPartIdx, UInt uiCoveredPartIdxes )
1884{
1885  memset((m_explicitRdpcmMode[compID] + uiAbsPartIdx), rdpcmMode, (sizeof(UChar) * uiCoveredPartIdxes));
1886}
1887
1888Void TComDataCU::setSizeSubParts( UInt uiWidth, UInt uiHeight, UInt uiAbsPartIdx, UInt uiDepth )
1889{
1890  UInt uiCurrPartNumb = m_pcPic->getNumPartitionsInCtu() >> (uiDepth << 1);
1891
1892  memset( m_puhWidth  + uiAbsPartIdx, uiWidth,  sizeof(UChar)*uiCurrPartNumb );
1893  memset( m_puhHeight + uiAbsPartIdx, uiHeight, sizeof(UChar)*uiCurrPartNumb );
1894}
1895
1896UChar TComDataCU::getNumPartitions(const UInt uiAbsPartIdx)
1897{
1898  UChar iNumPart = 0;
1899
1900  switch ( m_pePartSize[uiAbsPartIdx] )
1901  {
1902    case SIZE_2Nx2N:    iNumPart = 1; break;
1903    case SIZE_2NxN:     iNumPart = 2; break;
1904    case SIZE_Nx2N:     iNumPart = 2; break;
1905    case SIZE_NxN:      iNumPart = 4; break;
1906    case SIZE_2NxnU:    iNumPart = 2; break;
1907    case SIZE_2NxnD:    iNumPart = 2; break;
1908    case SIZE_nLx2N:    iNumPart = 2; break;
1909    case SIZE_nRx2N:    iNumPart = 2; break;
1910    default:            assert (0);   break;
1911  }
1912
1913  return  iNumPart;
1914}
1915
1916// This is for use by a leaf/sub CU object only, with no additional AbsPartIdx
1917Void TComDataCU::getPartIndexAndSize( UInt uiPartIdx, UInt& ruiPartAddr, Int& riWidth, Int& riHeight )
1918{
1919  switch ( m_pePartSize[0] )
1920  {
1921    case SIZE_2NxN:
1922      riWidth = getWidth(0);      riHeight = getHeight(0) >> 1; ruiPartAddr = ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 1;
1923      break;
1924    case SIZE_Nx2N:
1925      riWidth = getWidth(0) >> 1; riHeight = getHeight(0);      ruiPartAddr = ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 2;
1926      break;
1927    case SIZE_NxN:
1928      riWidth = getWidth(0) >> 1; riHeight = getHeight(0) >> 1; ruiPartAddr = ( m_uiNumPartition >> 2 ) * uiPartIdx;
1929      break;
1930    case SIZE_2NxnU:
1931      riWidth     = getWidth(0);
1932      riHeight    = ( uiPartIdx == 0 ) ?  getHeight(0) >> 2 : ( getHeight(0) >> 2 ) + ( getHeight(0) >> 1 );
1933      ruiPartAddr = ( uiPartIdx == 0 ) ? 0 : m_uiNumPartition >> 3;
1934      break;
1935    case SIZE_2NxnD:
1936      riWidth     = getWidth(0);
1937      riHeight    = ( uiPartIdx == 0 ) ?  ( getHeight(0) >> 2 ) + ( getHeight(0) >> 1 ) : getHeight(0) >> 2;
1938      ruiPartAddr = ( uiPartIdx == 0 ) ? 0 : (m_uiNumPartition >> 1) + (m_uiNumPartition >> 3);
1939      break;
1940    case SIZE_nLx2N:
1941      riWidth     = ( uiPartIdx == 0 ) ? getWidth(0) >> 2 : ( getWidth(0) >> 2 ) + ( getWidth(0) >> 1 );
1942      riHeight    = getHeight(0);
1943      ruiPartAddr = ( uiPartIdx == 0 ) ? 0 : m_uiNumPartition >> 4;
1944      break;
1945    case SIZE_nRx2N:
1946      riWidth     = ( uiPartIdx == 0 ) ? ( getWidth(0) >> 2 ) + ( getWidth(0) >> 1 ) : getWidth(0) >> 2;
1947      riHeight    = getHeight(0);
1948      ruiPartAddr = ( uiPartIdx == 0 ) ? 0 : (m_uiNumPartition >> 2) + (m_uiNumPartition >> 4);
1949      break;
1950    default:
1951      assert ( m_pePartSize[0] == SIZE_2Nx2N );
1952      riWidth = getWidth(0);      riHeight = getHeight(0);      ruiPartAddr = 0;
1953      break;
1954  }
1955}
1956
1957
1958Void TComDataCU::getMvField ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefPicList, TComMvField& rcMvField )
1959{
1960  if ( pcCU == NULL )  // OUT OF BOUNDARY
1961  {
1962    TComMv  cZeroMv;
1963    rcMvField.setMvField( cZeroMv, NOT_VALID );
1964    return;
1965  }
1966
1967  TComCUMvField*  pcCUMvField = pcCU->getCUMvField( eRefPicList );
1968  rcMvField.setMvField( pcCUMvField->getMv( uiAbsPartIdx ), pcCUMvField->getRefIdx( uiAbsPartIdx ) );
1969}
1970
1971Void TComDataCU::deriveLeftRightTopIdxGeneral ( UInt uiAbsPartIdx, UInt uiPartIdx, UInt& ruiPartIdxLT, UInt& ruiPartIdxRT )
1972{
1973  ruiPartIdxLT = m_absZIdxInCtu + uiAbsPartIdx;
1974  UInt uiPUWidth = 0;
1975
1976  switch ( m_pePartSize[uiAbsPartIdx] )
1977  {
1978    case SIZE_2Nx2N: uiPUWidth = m_puhWidth[uiAbsPartIdx];  break;
1979    case SIZE_2NxN:  uiPUWidth = m_puhWidth[uiAbsPartIdx];   break;
1980    case SIZE_Nx2N:  uiPUWidth = m_puhWidth[uiAbsPartIdx]  >> 1;  break;
1981    case SIZE_NxN:   uiPUWidth = m_puhWidth[uiAbsPartIdx]  >> 1; break;
1982    case SIZE_2NxnU:   uiPUWidth = m_puhWidth[uiAbsPartIdx]; break;
1983    case SIZE_2NxnD:   uiPUWidth = m_puhWidth[uiAbsPartIdx]; break;
1984    case SIZE_nLx2N:
1985      if ( uiPartIdx == 0 )
1986      {
1987        uiPUWidth = m_puhWidth[uiAbsPartIdx]  >> 2;
1988      }
1989      else if ( uiPartIdx == 1 )
1990      {
1991        uiPUWidth = (m_puhWidth[uiAbsPartIdx]  >> 1) + (m_puhWidth[uiAbsPartIdx]  >> 2);
1992      }
1993      else
1994      {
1995        assert(0);
1996      }
1997      break;
1998    case SIZE_nRx2N:
1999      if ( uiPartIdx == 0 )
2000      {
2001        uiPUWidth = (m_puhWidth[uiAbsPartIdx]  >> 1) + (m_puhWidth[uiAbsPartIdx]  >> 2);
2002      }
2003      else if ( uiPartIdx == 1 )
2004      {
2005        uiPUWidth = m_puhWidth[uiAbsPartIdx]  >> 2;
2006      }
2007      else
2008      {
2009        assert(0);
2010      }
2011      break;
2012    default:
2013      assert (0);
2014      break;
2015  }
2016
2017  ruiPartIdxRT = g_auiRasterToZscan [g_auiZscanToRaster[ ruiPartIdxLT ] + uiPUWidth / m_pcPic->getMinCUWidth() - 1 ];
2018}
2019
2020Void TComDataCU::deriveLeftBottomIdxGeneral( UInt uiAbsPartIdx, UInt uiPartIdx, UInt& ruiPartIdxLB )
2021{
2022  UInt uiPUHeight = 0;
2023  switch ( m_pePartSize[uiAbsPartIdx] )
2024  {
2025    case SIZE_2Nx2N: uiPUHeight = m_puhHeight[uiAbsPartIdx];    break;
2026    case SIZE_2NxN:  uiPUHeight = m_puhHeight[uiAbsPartIdx] >> 1;    break;
2027    case SIZE_Nx2N:  uiPUHeight = m_puhHeight[uiAbsPartIdx];  break;
2028    case SIZE_NxN:   uiPUHeight = m_puhHeight[uiAbsPartIdx] >> 1;    break;
2029    case SIZE_2NxnU:
2030      if ( uiPartIdx == 0 )
2031      {
2032        uiPUHeight = m_puhHeight[uiAbsPartIdx] >> 2;
2033      }
2034      else if ( uiPartIdx == 1 )
2035      {
2036        uiPUHeight = (m_puhHeight[uiAbsPartIdx] >> 1) + (m_puhHeight[uiAbsPartIdx] >> 2);
2037      }
2038      else
2039      {
2040        assert(0);
2041      }
2042      break;
2043    case SIZE_2NxnD:
2044      if ( uiPartIdx == 0 )
2045      {
2046        uiPUHeight = (m_puhHeight[uiAbsPartIdx] >> 1) + (m_puhHeight[uiAbsPartIdx] >> 2);
2047      }
2048      else if ( uiPartIdx == 1 )
2049      {
2050        uiPUHeight = m_puhHeight[uiAbsPartIdx] >> 2;
2051      }
2052      else
2053      {
2054        assert(0);
2055      }
2056      break;
2057    case SIZE_nLx2N: uiPUHeight = m_puhHeight[uiAbsPartIdx];  break;
2058    case SIZE_nRx2N: uiPUHeight = m_puhHeight[uiAbsPartIdx];  break;
2059    default:
2060      assert (0);
2061      break;
2062  }
2063
2064  ruiPartIdxLB      = g_auiRasterToZscan [g_auiZscanToRaster[ m_absZIdxInCtu + uiAbsPartIdx ] + ((uiPUHeight / m_pcPic->getMinCUHeight()) - 1)*m_pcPic->getNumPartInCtuWidth()];
2065}
2066
2067Void TComDataCU::deriveLeftRightTopIdx ( UInt uiPartIdx, UInt& ruiPartIdxLT, UInt& ruiPartIdxRT )
2068{
2069  ruiPartIdxLT = m_absZIdxInCtu;
2070  ruiPartIdxRT = g_auiRasterToZscan [g_auiZscanToRaster[ ruiPartIdxLT ] + m_puhWidth[0] / m_pcPic->getMinCUWidth() - 1 ];
2071
2072  switch ( m_pePartSize[0] )
2073  {
2074    case SIZE_2Nx2N:                                                                                                                                break;
2075    case SIZE_2NxN:
2076      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 1; ruiPartIdxRT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 1;
2077      break;
2078    case SIZE_Nx2N:
2079      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 2; ruiPartIdxRT -= ( uiPartIdx == 1 )? 0 : m_uiNumPartition >> 2;
2080      break;
2081    case SIZE_NxN:
2082      ruiPartIdxLT += ( m_uiNumPartition >> 2 ) * uiPartIdx;         ruiPartIdxRT +=  ( m_uiNumPartition >> 2 ) * ( uiPartIdx - 1 );
2083      break;
2084    case SIZE_2NxnU:
2085      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 3;
2086      ruiPartIdxRT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 3;
2087      break;
2088    case SIZE_2NxnD:
2089      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : ( m_uiNumPartition >> 1 ) + ( m_uiNumPartition >> 3 );
2090      ruiPartIdxRT += ( uiPartIdx == 0 )? 0 : ( m_uiNumPartition >> 1 ) + ( m_uiNumPartition >> 3 );
2091      break;
2092    case SIZE_nLx2N:
2093      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 4;
2094      ruiPartIdxRT -= ( uiPartIdx == 1 )? 0 : ( m_uiNumPartition >> 2 ) + ( m_uiNumPartition >> 4 );
2095      break;
2096    case SIZE_nRx2N:
2097      ruiPartIdxLT += ( uiPartIdx == 0 )? 0 : ( m_uiNumPartition >> 2 ) + ( m_uiNumPartition >> 4 );
2098      ruiPartIdxRT -= ( uiPartIdx == 1 )? 0 : m_uiNumPartition >> 4;
2099      break;
2100    default:
2101      assert (0);
2102      break;
2103  }
2104
2105}
2106
2107Void TComDataCU::deriveLeftBottomIdx( UInt  uiPartIdx,      UInt&      ruiPartIdxLB )
2108{
2109  ruiPartIdxLB      = g_auiRasterToZscan [g_auiZscanToRaster[ m_absZIdxInCtu ] + ( ((m_puhHeight[0] / m_pcPic->getMinCUHeight())>>1) - 1)*m_pcPic->getNumPartInCtuWidth()];
2110
2111  switch ( m_pePartSize[0] )
2112  {
2113    case SIZE_2Nx2N:
2114      ruiPartIdxLB += m_uiNumPartition >> 1;
2115      break;
2116    case SIZE_2NxN:
2117      ruiPartIdxLB += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 1;
2118      break;
2119    case SIZE_Nx2N:
2120      ruiPartIdxLB += ( uiPartIdx == 0 )? m_uiNumPartition >> 1 : (m_uiNumPartition >> 2)*3;
2121      break;
2122    case SIZE_NxN:
2123      ruiPartIdxLB += ( m_uiNumPartition >> 2 ) * uiPartIdx;
2124      break;
2125    case SIZE_2NxnU:
2126      ruiPartIdxLB += ( uiPartIdx == 0 ) ? -((Int)m_uiNumPartition >> 3) : m_uiNumPartition >> 1;
2127      break;
2128    case SIZE_2NxnD:
2129      ruiPartIdxLB += ( uiPartIdx == 0 ) ? (m_uiNumPartition >> 2) + (m_uiNumPartition >> 3): m_uiNumPartition >> 1;
2130      break;
2131    case SIZE_nLx2N:
2132      ruiPartIdxLB += ( uiPartIdx == 0 ) ? m_uiNumPartition >> 1 : (m_uiNumPartition >> 1) + (m_uiNumPartition >> 4);
2133      break;
2134    case SIZE_nRx2N:
2135      ruiPartIdxLB += ( uiPartIdx == 0 ) ? m_uiNumPartition >> 1 : (m_uiNumPartition >> 1) + (m_uiNumPartition >> 2) + (m_uiNumPartition >> 4);
2136      break;
2137    default:
2138      assert (0);
2139      break;
2140  }
2141}
2142
2143/** Derives the partition index of neighbouring bottom right block
2144 * \param [in]  eCUMode
2145 * \param [in]  uiPartIdx
2146 * \param [out] ruiPartIdxRB
2147 */
2148Void TComDataCU::deriveRightBottomIdx( UInt  uiPartIdx,      UInt&      ruiPartIdxRB )
2149{
2150  ruiPartIdxRB      = g_auiRasterToZscan [g_auiZscanToRaster[ m_absZIdxInCtu ] + ( ((m_puhHeight[0] / m_pcPic->getMinCUHeight())>>1) - 1)*m_pcPic->getNumPartInCtuWidth() +  m_puhWidth[0] / m_pcPic->getMinCUWidth() - 1];
2151
2152  switch ( m_pePartSize[0] )
2153  {
2154    case SIZE_2Nx2N:
2155      ruiPartIdxRB += m_uiNumPartition >> 1;
2156      break;
2157    case SIZE_2NxN:
2158      ruiPartIdxRB += ( uiPartIdx == 0 )? 0 : m_uiNumPartition >> 1;
2159      break;
2160    case SIZE_Nx2N:
2161      ruiPartIdxRB += ( uiPartIdx == 0 )? m_uiNumPartition >> 2 : (m_uiNumPartition >> 1);
2162      break;
2163    case SIZE_NxN:
2164      ruiPartIdxRB += ( m_uiNumPartition >> 2 ) * ( uiPartIdx - 1 );
2165      break;
2166    case SIZE_2NxnU:
2167      ruiPartIdxRB += ( uiPartIdx == 0 ) ? -((Int)m_uiNumPartition >> 3) : m_uiNumPartition >> 1;
2168      break;
2169    case SIZE_2NxnD:
2170      ruiPartIdxRB += ( uiPartIdx == 0 ) ? (m_uiNumPartition >> 2) + (m_uiNumPartition >> 3): m_uiNumPartition >> 1;
2171      break;
2172    case SIZE_nLx2N:
2173      ruiPartIdxRB += ( uiPartIdx == 0 ) ? (m_uiNumPartition >> 3) + (m_uiNumPartition >> 4): m_uiNumPartition >> 1;
2174      break;
2175    case SIZE_nRx2N:
2176      ruiPartIdxRB += ( uiPartIdx == 0 ) ? (m_uiNumPartition >> 2) + (m_uiNumPartition >> 3) + (m_uiNumPartition >> 4) : m_uiNumPartition >> 1;
2177      break;
2178    default:
2179      assert (0);
2180      break;
2181  }
2182}
2183
2184Void TComDataCU::deriveLeftRightTopIdxAdi ( UInt& ruiPartIdxLT, UInt& ruiPartIdxRT, UInt uiPartOffset, UInt uiPartDepth )
2185{
2186  UInt uiNumPartInWidth = (m_puhWidth[0]/m_pcPic->getMinCUWidth())>>uiPartDepth;
2187  ruiPartIdxLT = m_absZIdxInCtu + uiPartOffset;
2188  ruiPartIdxRT = g_auiRasterToZscan[ g_auiZscanToRaster[ ruiPartIdxLT ] + uiNumPartInWidth - 1 ];
2189}
2190
2191Void TComDataCU::deriveLeftBottomIdxAdi( UInt& ruiPartIdxLB, UInt uiPartOffset, UInt uiPartDepth )
2192{
2193  UInt uiAbsIdx;
2194  UInt uiMinCuWidth, uiWidthInMinCus;
2195
2196  uiMinCuWidth    = getPic()->getMinCUWidth();
2197  uiWidthInMinCus = (getWidth(0)/uiMinCuWidth)>>uiPartDepth;
2198  uiAbsIdx        = getZorderIdxInCtu()+uiPartOffset+(m_uiNumPartition>>(uiPartDepth<<1))-1;
2199  uiAbsIdx        = g_auiZscanToRaster[uiAbsIdx]-(uiWidthInMinCus-1);
2200  ruiPartIdxLB    = g_auiRasterToZscan[uiAbsIdx];
2201}
2202
2203Bool TComDataCU::hasEqualMotion( UInt uiAbsPartIdx, TComDataCU* pcCandCU, UInt uiCandAbsPartIdx )
2204{
2205  if ( getInterDir( uiAbsPartIdx ) != pcCandCU->getInterDir( uiCandAbsPartIdx ) )
2206  {
2207    return false;
2208  }
2209
2210  for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2211  {
2212    if ( getInterDir( uiAbsPartIdx ) & ( 1 << uiRefListIdx ) )
2213    {
2214      if ( getCUMvField( RefPicList( uiRefListIdx ) )->getMv( uiAbsPartIdx )     != pcCandCU->getCUMvField( RefPicList( uiRefListIdx ) )->getMv( uiCandAbsPartIdx ) ||
2215        getCUMvField( RefPicList( uiRefListIdx ) )->getRefIdx( uiAbsPartIdx ) != pcCandCU->getCUMvField( RefPicList( uiRefListIdx ) )->getRefIdx( uiCandAbsPartIdx ) )
2216      {
2217        return false;
2218      }
2219    }
2220  }
2221
2222  return true;
2223}
2224
2225/** Constructs a list of merging candidates
2226 * \param uiAbsPartIdx
2227 * \param uiPUIdx
2228 * \param uiDepth
2229 * \param pcMvFieldNeighbours
2230 * \param puhInterDirNeighbours
2231 * \param numValidMergeCand
2232 */
2233Void TComDataCU::getInterMergeCandidates( UInt uiAbsPartIdx, UInt uiPUIdx, TComMvField* pcMvFieldNeighbours, UChar* puhInterDirNeighbours, Int& numValidMergeCand, Int mrgCandIdx )
2234{
2235  UInt uiAbsPartAddr = m_absZIdxInCtu + uiAbsPartIdx;
2236  Bool abCandIsInter[ MRG_MAX_NUM_CANDS ];
2237  for( UInt ui = 0; ui < getSlice()->getMaxNumMergeCand(); ++ui )
2238  {
2239    abCandIsInter[ui] = false;
2240    pcMvFieldNeighbours[ ( ui << 1 )     ].setRefIdx(NOT_VALID);
2241    pcMvFieldNeighbours[ ( ui << 1 ) + 1 ].setRefIdx(NOT_VALID);
2242  }
2243  numValidMergeCand = getSlice()->getMaxNumMergeCand();
2244  // compute the location of the current PU
2245  Int xP, yP, nPSW, nPSH;
2246  this->getPartPosition(uiPUIdx, xP, yP, nPSW, nPSH);
2247
2248  Int iCount = 0;
2249
2250  UInt uiPartIdxLT, uiPartIdxRT, uiPartIdxLB;
2251  PartSize cCurPS = getPartitionSize( uiAbsPartIdx );
2252  deriveLeftRightTopIdxGeneral( uiAbsPartIdx, uiPUIdx, uiPartIdxLT, uiPartIdxRT );
2253  deriveLeftBottomIdxGeneral( uiAbsPartIdx, uiPUIdx, uiPartIdxLB );
2254
2255  //left
2256  UInt uiLeftPartIdx = 0;
2257  TComDataCU* pcCULeft = 0;
2258  pcCULeft = getPULeft( uiLeftPartIdx, uiPartIdxLB );
2259
2260  Bool isAvailableA1 = pcCULeft &&
2261                       pcCULeft->isDiffMER(xP -1, yP+nPSH-1, xP, yP) &&
2262                       !( uiPUIdx == 1 && (cCurPS == SIZE_Nx2N || cCurPS == SIZE_nLx2N || cCurPS == SIZE_nRx2N) ) &&
2263                       pcCULeft->isInter( uiLeftPartIdx ) ;
2264
2265  if ( isAvailableA1 )
2266  {
2267    abCandIsInter[iCount] = true;
2268    // get Inter Dir
2269    puhInterDirNeighbours[iCount] = pcCULeft->getInterDir( uiLeftPartIdx );
2270    // get Mv from Left
2271    pcCULeft->getMvField( pcCULeft, uiLeftPartIdx, REF_PIC_LIST_0, pcMvFieldNeighbours[iCount<<1] );
2272    if ( getSlice()->isInterB() )
2273    {
2274      pcCULeft->getMvField( pcCULeft, uiLeftPartIdx, REF_PIC_LIST_1, pcMvFieldNeighbours[(iCount<<1)+1] );
2275    }
2276    if ( mrgCandIdx == iCount )
2277    {
2278      return;
2279    }
2280    iCount ++;
2281  }
2282
2283  // early termination
2284  if (iCount == getSlice()->getMaxNumMergeCand())
2285  {
2286    return;
2287  }
2288  // above
2289  UInt uiAbovePartIdx = 0;
2290  TComDataCU* pcCUAbove = 0;
2291  pcCUAbove = getPUAbove( uiAbovePartIdx, uiPartIdxRT );
2292
2293  Bool isAvailableB1 = pcCUAbove &&
2294                       pcCUAbove->isDiffMER(xP+nPSW-1, yP-1, xP, yP) &&
2295                       !( uiPUIdx == 1 && (cCurPS == SIZE_2NxN || cCurPS == SIZE_2NxnU || cCurPS == SIZE_2NxnD) ) &&
2296                       pcCUAbove->isInter( uiAbovePartIdx );
2297
2298  if ( isAvailableB1 && (!isAvailableA1 || !pcCULeft->hasEqualMotion( uiLeftPartIdx, pcCUAbove, uiAbovePartIdx ) ) )
2299  {
2300    abCandIsInter[iCount] = true;
2301    // get Inter Dir
2302    puhInterDirNeighbours[iCount] = pcCUAbove->getInterDir( uiAbovePartIdx );
2303    // get Mv from Left
2304    pcCUAbove->getMvField( pcCUAbove, uiAbovePartIdx, REF_PIC_LIST_0, pcMvFieldNeighbours[iCount<<1] );
2305    if ( getSlice()->isInterB() )
2306    {
2307      pcCUAbove->getMvField( pcCUAbove, uiAbovePartIdx, REF_PIC_LIST_1, pcMvFieldNeighbours[(iCount<<1)+1] );
2308    }
2309    if ( mrgCandIdx == iCount )
2310    {
2311      return;
2312    }
2313    iCount ++;
2314  }
2315  // early termination
2316  if (iCount == getSlice()->getMaxNumMergeCand())
2317  {
2318    return;
2319  }
2320
2321  // above right
2322  UInt uiAboveRightPartIdx = 0;
2323  TComDataCU* pcCUAboveRight = 0;
2324  pcCUAboveRight = getPUAboveRight( uiAboveRightPartIdx, uiPartIdxRT );
2325
2326  Bool isAvailableB0 = pcCUAboveRight &&
2327                       pcCUAboveRight->isDiffMER(xP+nPSW, yP-1, xP, yP) &&
2328                       pcCUAboveRight->isInter( uiAboveRightPartIdx );
2329
2330  if ( isAvailableB0 && ( !isAvailableB1 || !pcCUAbove->hasEqualMotion( uiAbovePartIdx, pcCUAboveRight, uiAboveRightPartIdx ) ) )
2331  {
2332    abCandIsInter[iCount] = true;
2333    // get Inter Dir
2334    puhInterDirNeighbours[iCount] = pcCUAboveRight->getInterDir( uiAboveRightPartIdx );
2335    // get Mv from Left
2336    pcCUAboveRight->getMvField( pcCUAboveRight, uiAboveRightPartIdx, REF_PIC_LIST_0, pcMvFieldNeighbours[iCount<<1] );
2337    if ( getSlice()->isInterB() )
2338    {
2339      pcCUAboveRight->getMvField( pcCUAboveRight, uiAboveRightPartIdx, REF_PIC_LIST_1, pcMvFieldNeighbours[(iCount<<1)+1] );
2340    }
2341    if ( mrgCandIdx == iCount )
2342    {
2343      return;
2344    }
2345    iCount ++;
2346  }
2347  // early termination
2348  if (iCount == getSlice()->getMaxNumMergeCand())
2349  {
2350    return;
2351  }
2352
2353  //left bottom
2354  UInt uiLeftBottomPartIdx = 0;
2355  TComDataCU* pcCULeftBottom = 0;
2356  pcCULeftBottom = this->getPUBelowLeft( uiLeftBottomPartIdx, uiPartIdxLB );
2357
2358  Bool isAvailableA0 = pcCULeftBottom &&
2359                       pcCULeftBottom->isDiffMER(xP-1, yP+nPSH, xP, yP) &&
2360                       pcCULeftBottom->isInter( uiLeftBottomPartIdx ) ;
2361
2362  if ( isAvailableA0 && ( !isAvailableA1 || !pcCULeft->hasEqualMotion( uiLeftPartIdx, pcCULeftBottom, uiLeftBottomPartIdx ) ) )
2363  {
2364    abCandIsInter[iCount] = true;
2365    // get Inter Dir
2366    puhInterDirNeighbours[iCount] = pcCULeftBottom->getInterDir( uiLeftBottomPartIdx );
2367    // get Mv from Left
2368    pcCULeftBottom->getMvField( pcCULeftBottom, uiLeftBottomPartIdx, REF_PIC_LIST_0, pcMvFieldNeighbours[iCount<<1] );
2369    if ( getSlice()->isInterB() )
2370    {
2371      pcCULeftBottom->getMvField( pcCULeftBottom, uiLeftBottomPartIdx, REF_PIC_LIST_1, pcMvFieldNeighbours[(iCount<<1)+1] );
2372    }
2373    if ( mrgCandIdx == iCount )
2374    {
2375      return;
2376    }
2377    iCount ++;
2378  }
2379  // early termination
2380  if (iCount == getSlice()->getMaxNumMergeCand())
2381  {
2382    return;
2383  }
2384
2385  // above left
2386  if( iCount < 4 )
2387  {
2388    UInt uiAboveLeftPartIdx = 0;
2389    TComDataCU* pcCUAboveLeft = 0;
2390    pcCUAboveLeft = getPUAboveLeft( uiAboveLeftPartIdx, uiAbsPartAddr );
2391
2392    Bool isAvailableB2 = pcCUAboveLeft &&
2393                         pcCUAboveLeft->isDiffMER(xP-1, yP-1, xP, yP) &&
2394                         pcCUAboveLeft->isInter( uiAboveLeftPartIdx );
2395
2396    if ( isAvailableB2 && ( !isAvailableA1 || !pcCULeft->hasEqualMotion( uiLeftPartIdx, pcCUAboveLeft, uiAboveLeftPartIdx ) )
2397        && ( !isAvailableB1 || !pcCUAbove->hasEqualMotion( uiAbovePartIdx, pcCUAboveLeft, uiAboveLeftPartIdx ) ) )
2398    {
2399      abCandIsInter[iCount] = true;
2400      // get Inter Dir
2401      puhInterDirNeighbours[iCount] = pcCUAboveLeft->getInterDir( uiAboveLeftPartIdx );
2402      // get Mv from Left
2403      pcCUAboveLeft->getMvField( pcCUAboveLeft, uiAboveLeftPartIdx, REF_PIC_LIST_0, pcMvFieldNeighbours[iCount<<1] );
2404      if ( getSlice()->isInterB() )
2405      {
2406        pcCUAboveLeft->getMvField( pcCUAboveLeft, uiAboveLeftPartIdx, REF_PIC_LIST_1, pcMvFieldNeighbours[(iCount<<1)+1] );
2407      }
2408      if ( mrgCandIdx == iCount )
2409      {
2410        return;
2411      }
2412      iCount ++;
2413    }
2414  }
2415  // early termination
2416  if (iCount == getSlice()->getMaxNumMergeCand())
2417  {
2418    return;
2419  }
2420
2421  if ( getSlice()->getEnableTMVPFlag() )
2422  {
2423    //>> MTK colocated-RightBottom
2424    UInt uiPartIdxRB;
2425
2426    deriveRightBottomIdx( uiPUIdx, uiPartIdxRB );
2427
2428    UInt uiAbsPartIdxTmp = g_auiZscanToRaster[uiPartIdxRB];
2429    const UInt numPartInCtuWidth  = m_pcPic->getNumPartInCtuWidth();
2430    const UInt numPartInCtuHeight = m_pcPic->getNumPartInCtuHeight();
2431
2432    TComMv cColMv;
2433    Int iRefIdx;
2434    Int ctuRsAddr = -1;
2435
2436#if SVC_EXTENSION
2437    if (   ( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxTmp] + m_pcPic->getMinCUWidth () ) < m_pcSlice->getPicWidthInLumaSamples() )  // image boundary check
2438        && ( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxTmp] + m_pcPic->getMinCUHeight() ) < m_pcSlice->getPicHeightInLumaSamples() ) )
2439#else
2440    if (   ( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxTmp] + m_pcPic->getMinCUWidth () ) < m_pcSlice->getSPS()->getPicWidthInLumaSamples () )  // image boundary check
2441        && ( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxTmp] + m_pcPic->getMinCUHeight() ) < m_pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2442#endif   
2443    {
2444      if ( ( uiAbsPartIdxTmp % numPartInCtuWidth < numPartInCtuWidth - 1 ) &&           // is not at the last column of CTU
2445        ( uiAbsPartIdxTmp / numPartInCtuWidth < numPartInCtuHeight - 1 ) )              // is not at the last row    of CTU
2446      {
2447        uiAbsPartAddr = g_auiRasterToZscan[ uiAbsPartIdxTmp + numPartInCtuWidth + 1 ];
2448        ctuRsAddr = getCtuRsAddr();
2449      }
2450      else if ( uiAbsPartIdxTmp % numPartInCtuWidth < numPartInCtuWidth - 1 )           // is not at the last column of CTU But is last row of CTU
2451      {
2452        uiAbsPartAddr = g_auiRasterToZscan[ (uiAbsPartIdxTmp + numPartInCtuWidth + 1) % m_pcPic->getNumPartitionsInCtu() ];
2453      }
2454      else if ( uiAbsPartIdxTmp / numPartInCtuWidth < numPartInCtuHeight - 1 )          // is not at the last row of CTU But is last column of CTU
2455      {
2456        uiAbsPartAddr = g_auiRasterToZscan[ uiAbsPartIdxTmp + 1 ];
2457        ctuRsAddr = getCtuRsAddr() + 1;
2458      }
2459      else //is the right bottom corner of CTU
2460      {
2461        uiAbsPartAddr = 0;
2462      }
2463    }
2464
2465    iRefIdx = 0;
2466
2467    Bool bExistMV = false;
2468    UInt uiPartIdxCenter;
2469    Int dir = 0;
2470    UInt uiArrayAddr = iCount;
2471    xDeriveCenterIdx( uiPUIdx, uiPartIdxCenter );
2472    bExistMV = ctuRsAddr >= 0 && xGetColMVP( REF_PIC_LIST_0, ctuRsAddr, uiAbsPartAddr, cColMv, iRefIdx );
2473    if( bExistMV == false )
2474    {
2475      bExistMV = xGetColMVP( REF_PIC_LIST_0, getCtuRsAddr(), uiPartIdxCenter,  cColMv, iRefIdx );
2476    }
2477    if( bExistMV )
2478    {
2479      dir |= 1;
2480      pcMvFieldNeighbours[ 2 * uiArrayAddr ].setMvField( cColMv, iRefIdx );
2481    }
2482
2483    if ( getSlice()->isInterB() )
2484    {
2485      bExistMV = ctuRsAddr >= 0 && xGetColMVP( REF_PIC_LIST_1, ctuRsAddr, uiAbsPartAddr, cColMv, iRefIdx);
2486      if( bExistMV == false )
2487      {
2488        bExistMV = xGetColMVP( REF_PIC_LIST_1, getCtuRsAddr(), uiPartIdxCenter, cColMv, iRefIdx );
2489      }
2490      if( bExistMV )
2491      {
2492        dir |= 2;
2493        pcMvFieldNeighbours[ 2 * uiArrayAddr + 1 ].setMvField( cColMv, iRefIdx );
2494      }
2495    }
2496
2497    if (dir != 0)
2498    {
2499      puhInterDirNeighbours[uiArrayAddr] = dir;
2500      abCandIsInter[uiArrayAddr] = true;
2501
2502      if ( mrgCandIdx == iCount )
2503      {
2504        return;
2505      }
2506      iCount++;
2507    }
2508  }
2509  // early termination
2510  if (iCount == getSlice()->getMaxNumMergeCand())
2511  {
2512    return;
2513  }
2514
2515  UInt uiArrayAddr = iCount;
2516  UInt uiCutoff = uiArrayAddr;
2517
2518  if ( getSlice()->isInterB() )
2519  {
2520    static const UInt NUM_PRIORITY_LIST=12;
2521    static const UInt uiPriorityList0[NUM_PRIORITY_LIST] = {0 , 1, 0, 2, 1, 2, 0, 3, 1, 3, 2, 3};
2522    static const UInt uiPriorityList1[NUM_PRIORITY_LIST] = {1 , 0, 2, 0, 2, 1, 3, 0, 3, 1, 3, 2};
2523
2524    for (Int idx=0; idx<uiCutoff*(uiCutoff-1) && uiArrayAddr!= getSlice()->getMaxNumMergeCand(); idx++)
2525    {
2526      assert(idx<NUM_PRIORITY_LIST);
2527      Int i = uiPriorityList0[idx];
2528      Int j = uiPriorityList1[idx];
2529      if (abCandIsInter[i] && abCandIsInter[j]&& (puhInterDirNeighbours[i]&0x1)&&(puhInterDirNeighbours[j]&0x2))
2530      {
2531        abCandIsInter[uiArrayAddr] = true;
2532        puhInterDirNeighbours[uiArrayAddr] = 3;
2533
2534        // get Mv from cand[i] and cand[j]
2535        pcMvFieldNeighbours[uiArrayAddr << 1].setMvField(pcMvFieldNeighbours[i<<1].getMv(), pcMvFieldNeighbours[i<<1].getRefIdx());
2536        pcMvFieldNeighbours[( uiArrayAddr << 1 ) + 1].setMvField(pcMvFieldNeighbours[(j<<1)+1].getMv(), pcMvFieldNeighbours[(j<<1)+1].getRefIdx());
2537
2538        Int iRefPOCL0 = m_pcSlice->getRefPOC( REF_PIC_LIST_0, pcMvFieldNeighbours[(uiArrayAddr<<1)].getRefIdx() );
2539        Int iRefPOCL1 = m_pcSlice->getRefPOC( REF_PIC_LIST_1, pcMvFieldNeighbours[(uiArrayAddr<<1)+1].getRefIdx() );
2540        if (iRefPOCL0 == iRefPOCL1 && pcMvFieldNeighbours[(uiArrayAddr<<1)].getMv() == pcMvFieldNeighbours[(uiArrayAddr<<1)+1].getMv())
2541        {
2542          abCandIsInter[uiArrayAddr] = false;
2543        }
2544        else
2545        {
2546          uiArrayAddr++;
2547        }
2548      }
2549    }
2550  }
2551  // early termination
2552  if (uiArrayAddr == getSlice()->getMaxNumMergeCand())
2553  {
2554    return;
2555  }
2556
2557  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);
2558
2559  Int r = 0;
2560  Int refcnt = 0;
2561  while (uiArrayAddr < getSlice()->getMaxNumMergeCand())
2562  {
2563    abCandIsInter[uiArrayAddr] = true;
2564    puhInterDirNeighbours[uiArrayAddr] = 1;
2565    pcMvFieldNeighbours[uiArrayAddr << 1].setMvField( TComMv(0, 0), r);
2566
2567    if ( getSlice()->isInterB() )
2568    {
2569      puhInterDirNeighbours[uiArrayAddr] = 3;
2570      pcMvFieldNeighbours[(uiArrayAddr << 1) + 1].setMvField(TComMv(0, 0), r);
2571    }
2572    uiArrayAddr++;
2573
2574    if ( refcnt == iNumRefIdx - 1 )
2575    {
2576      r = 0;
2577    }
2578    else
2579    {
2580      ++r;
2581      ++refcnt;
2582    }
2583  }
2584  numValidMergeCand = uiArrayAddr;
2585}
2586
2587/** Check whether the current PU and a spatial neighboring PU are in a same ME region.
2588 * \param xN, xN   location of the upper-left corner pixel of a neighboring PU
2589 * \param xP, yP   location of the upper-left corner pixel of the current PU
2590 * \returns Bool
2591 */
2592Bool TComDataCU::isDiffMER(Int xN, Int yN, Int xP, Int yP)
2593{
2594
2595  UInt plevel = this->getSlice()->getPPS()->getLog2ParallelMergeLevelMinus2() + 2;
2596  if ((xN>>plevel)!= (xP>>plevel))
2597  {
2598    return true;
2599  }
2600  if ((yN>>plevel)!= (yP>>plevel))
2601  {
2602    return true;
2603  }
2604  return false;
2605}
2606
2607/** calculate the location of upper-left corner pixel and size of the current PU.
2608 * \param partIdx  PU index within a CU
2609 * \param xP, yP   location of the upper-left corner pixel of the current PU
2610 * \param PSW, nPSH    size of the curren PU
2611 * \returns Void
2612 */
2613Void TComDataCU::getPartPosition( UInt partIdx, Int& xP, Int& yP, Int& nPSW, Int& nPSH)
2614{
2615  UInt col = m_uiCUPelX;
2616  UInt row = m_uiCUPelY;
2617
2618  switch ( m_pePartSize[0] )
2619  {
2620  case SIZE_2NxN:
2621    nPSW = getWidth(0);
2622    nPSH = getHeight(0) >> 1;
2623    xP   = col;
2624    yP   = (partIdx ==0)? row: row + nPSH;
2625    break;
2626  case SIZE_Nx2N:
2627    nPSW = getWidth(0) >> 1;
2628    nPSH = getHeight(0);
2629    xP   = (partIdx ==0)? col: col + nPSW;
2630    yP   = row;
2631    break;
2632  case SIZE_NxN:
2633    nPSW = getWidth(0) >> 1;
2634    nPSH = getHeight(0) >> 1;
2635    xP   = col + (partIdx&0x1)*nPSW;
2636    yP   = row + (partIdx>>1)*nPSH;
2637    break;
2638  case SIZE_2NxnU:
2639    nPSW = getWidth(0);
2640    nPSH = ( partIdx == 0 ) ?  getHeight(0) >> 2 : ( getHeight(0) >> 2 ) + ( getHeight(0) >> 1 );
2641    xP   = col;
2642    yP   = (partIdx ==0)? row: row + getHeight(0) - nPSH;
2643
2644    break;
2645  case SIZE_2NxnD:
2646    nPSW = getWidth(0);
2647    nPSH = ( partIdx == 0 ) ?  ( getHeight(0) >> 2 ) + ( getHeight(0) >> 1 ) : getHeight(0) >> 2;
2648    xP   = col;
2649    yP   = (partIdx ==0)? row: row + getHeight(0) - nPSH;
2650    break;
2651  case SIZE_nLx2N:
2652    nPSW = ( partIdx == 0 ) ? getWidth(0) >> 2 : ( getWidth(0) >> 2 ) + ( getWidth(0) >> 1 );
2653    nPSH = getHeight(0);
2654    xP   = (partIdx ==0)? col: col + getWidth(0) - nPSW;
2655    yP   = row;
2656    break;
2657  case SIZE_nRx2N:
2658    nPSW = ( partIdx == 0 ) ? ( getWidth(0) >> 2 ) + ( getWidth(0) >> 1 ) : getWidth(0) >> 2;
2659    nPSH = getHeight(0);
2660    xP   = (partIdx ==0)? col: col + getWidth(0) - nPSW;
2661    yP   = row;
2662    break;
2663  default:
2664    assert ( m_pePartSize[0] == SIZE_2Nx2N );
2665    nPSW = getWidth(0);
2666    nPSH = getHeight(0);
2667    xP   = col ;
2668    yP   = row ;
2669
2670    break;
2671  }
2672}
2673
2674/** Constructs a list of candidates for AMVP
2675 * \param uiPartIdx
2676 * \param uiPartAddr
2677 * \param eRefPicList
2678 * \param iRefIdx
2679 * \param pInfo
2680 */
2681Void TComDataCU::fillMvpCand ( UInt uiPartIdx, UInt uiPartAddr, RefPicList eRefPicList, Int iRefIdx, AMVPInfo* pInfo )
2682{
2683  TComMv cMvPred;
2684  Bool bAddedSmvp = false;
2685
2686  pInfo->iN = 0;
2687  if (iRefIdx < 0)
2688  {
2689    return;
2690  }
2691
2692  //-- Get Spatial MV
2693  UInt uiPartIdxLT, uiPartIdxRT, uiPartIdxLB;
2694  const UInt numPartInCtuWidth  = m_pcPic->getNumPartInCtuWidth();
2695  const UInt numPartInCtuHeight = m_pcPic->getNumPartInCtuHeight();
2696  Bool bAdded = false;
2697
2698  deriveLeftRightTopIdx( uiPartIdx, uiPartIdxLT, uiPartIdxRT );
2699  deriveLeftBottomIdx( uiPartIdx, uiPartIdxLB );
2700
2701  TComDataCU* tmpCU = NULL;
2702  UInt idx;
2703  tmpCU = getPUBelowLeft(idx, uiPartIdxLB);
2704  bAddedSmvp = (tmpCU != NULL) && (tmpCU->isInter(idx));
2705
2706  if (!bAddedSmvp)
2707  {
2708    tmpCU = getPULeft(idx, uiPartIdxLB);
2709    bAddedSmvp = (tmpCU != NULL) && (tmpCU->isInter(idx));
2710  }
2711
2712  // Left predictor search
2713  bAdded = xAddMVPCand( pInfo, eRefPicList, iRefIdx, uiPartIdxLB, MD_BELOW_LEFT);
2714  if (!bAdded)
2715  {
2716    bAdded = xAddMVPCand( pInfo, eRefPicList, iRefIdx, uiPartIdxLB, MD_LEFT );
2717  }
2718
2719  if(!bAdded)
2720  {
2721    bAdded = xAddMVPCandOrder( pInfo, eRefPicList, iRefIdx, uiPartIdxLB, MD_BELOW_LEFT);
2722    if (!bAdded)
2723    {
2724      xAddMVPCandOrder( pInfo, eRefPicList, iRefIdx, uiPartIdxLB, MD_LEFT );
2725    }
2726  }
2727
2728  // Above predictor search
2729  bAdded = xAddMVPCand( pInfo, eRefPicList, iRefIdx, uiPartIdxRT, MD_ABOVE_RIGHT);
2730
2731  if (!bAdded)
2732  {
2733    bAdded = xAddMVPCand( pInfo, eRefPicList, iRefIdx, uiPartIdxRT, MD_ABOVE);
2734  }
2735
2736  if(!bAdded)
2737  {
2738    xAddMVPCand( pInfo, eRefPicList, iRefIdx, uiPartIdxLT, MD_ABOVE_LEFT);
2739  }
2740
2741  if(!bAddedSmvp)
2742  {
2743    bAdded = xAddMVPCandOrder( pInfo, eRefPicList, iRefIdx, uiPartIdxRT, MD_ABOVE_RIGHT);
2744    if (!bAdded)
2745    {
2746      bAdded = xAddMVPCandOrder( pInfo, eRefPicList, iRefIdx, uiPartIdxRT, MD_ABOVE);
2747    }
2748
2749    if(!bAdded)
2750    {
2751      xAddMVPCandOrder( pInfo, eRefPicList, iRefIdx, uiPartIdxLT, MD_ABOVE_LEFT);
2752    }
2753  }
2754
2755  if ( pInfo->iN == 2 )
2756  {
2757    if ( pInfo->m_acMvCand[ 0 ] == pInfo->m_acMvCand[ 1 ] )
2758    {
2759      pInfo->iN = 1;
2760    }
2761  }
2762
2763  if ( getSlice()->getEnableTMVPFlag() )
2764  {
2765    // Get Temporal Motion Predictor
2766    Int iRefIdx_Col = iRefIdx;
2767    TComMv cColMv;
2768    UInt uiPartIdxRB;
2769    UInt uiAbsPartIdx;
2770    UInt uiAbsPartAddr;
2771
2772    deriveRightBottomIdx( uiPartIdx, uiPartIdxRB );
2773    uiAbsPartAddr = m_absZIdxInCtu + uiPartAddr;
2774
2775    //----  co-located RightBottom Temporal Predictor (H) ---//
2776    uiAbsPartIdx = g_auiZscanToRaster[uiPartIdxRB];
2777    Int ctuRsAddr = -1;
2778
2779#if SVC_EXTENSION
2780    if (  ( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdx] + m_pcPic->getMinCUWidth () ) < m_pcSlice->getPicWidthInLumaSamples())   // image boundary check
2781       && ( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdx] + m_pcPic->getMinCUHeight() ) < m_pcSlice->getPicHeightInLumaSamples() ) )   
2782#else
2783    if (  ( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdx] + m_pcPic->getMinCUWidth () ) < m_pcSlice->getSPS()->getPicWidthInLumaSamples () )  // image boundary check
2784       && ( ( m_pcPic->getCtu(m_ctuRsAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdx] + m_pcPic->getMinCUHeight() ) < m_pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2785#endif   
2786    {
2787      if ( ( uiAbsPartIdx % numPartInCtuWidth < numPartInCtuWidth - 1 ) &&  // is not at the last column of CTU
2788           ( uiAbsPartIdx / numPartInCtuWidth < numPartInCtuHeight - 1 ) )  // is not at the last row    of CTU
2789      {
2790        uiAbsPartAddr = g_auiRasterToZscan[ uiAbsPartIdx + numPartInCtuWidth + 1 ];
2791        ctuRsAddr = getCtuRsAddr();
2792      }
2793      else if ( uiAbsPartIdx % numPartInCtuWidth < numPartInCtuWidth - 1 )  // is not at the last column of CTU But is last row of CTU
2794      {
2795        uiAbsPartAddr = g_auiRasterToZscan[ (uiAbsPartIdx + numPartInCtuWidth + 1) % m_pcPic->getNumPartitionsInCtu() ];
2796      }
2797      else if ( uiAbsPartIdx / numPartInCtuWidth < numPartInCtuHeight - 1 ) // is not at the last row of CTU But is last column of CTU
2798      {
2799        uiAbsPartAddr = g_auiRasterToZscan[ uiAbsPartIdx + 1 ];
2800        ctuRsAddr = getCtuRsAddr() + 1;
2801      }
2802      else //is the right bottom corner of CTU
2803      {
2804        uiAbsPartAddr = 0;
2805      }
2806    }
2807    if ( ctuRsAddr >= 0 && xGetColMVP( eRefPicList, ctuRsAddr, uiAbsPartAddr, cColMv, iRefIdx_Col ) )
2808    {
2809      pInfo->m_acMvCand[pInfo->iN++] = cColMv;
2810    }
2811    else
2812    {
2813      UInt uiPartIdxCenter;
2814      xDeriveCenterIdx( uiPartIdx, uiPartIdxCenter );
2815      if (xGetColMVP( eRefPicList, getCtuRsAddr(), uiPartIdxCenter,  cColMv, iRefIdx_Col ))
2816      {
2817        pInfo->m_acMvCand[pInfo->iN++] = cColMv;
2818      }
2819    }
2820    //----  co-located RightBottom Temporal Predictor  ---//
2821  }
2822
2823  if (pInfo->iN > AMVP_MAX_NUM_CANDS)
2824  {
2825    pInfo->iN = AMVP_MAX_NUM_CANDS;
2826  }
2827
2828  while (pInfo->iN < AMVP_MAX_NUM_CANDS)
2829  {
2830    pInfo->m_acMvCand[pInfo->iN].set(0,0);
2831    pInfo->iN++;
2832  }
2833  return ;
2834}
2835
2836
2837Bool TComDataCU::isBipredRestriction(UInt puIdx)
2838{
2839  Int width = 0;
2840  Int height = 0;
2841  UInt partAddr;
2842
2843  getPartIndexAndSize( puIdx, partAddr, width, height );
2844  if ( getWidth(0) == 8 && (width < 8 || height < 8) )
2845  {
2846    return true;
2847  }
2848  return false;
2849}
2850
2851
2852Void TComDataCU::clipMv    (TComMv&  rcMv)
2853{
2854  Int  iMvShift = 2;
2855  Int iOffset = 8;
2856#if SVC_EXTENSION
2857  Int iHorMax = ( m_pcSlice->getPicWidthInLumaSamples() + iOffset - m_uiCUPelX - 1 ) << iMvShift;
2858#else
2859  Int iHorMax = ( m_pcSlice->getSPS()->getPicWidthInLumaSamples() + iOffset - m_uiCUPelX - 1 ) << iMvShift;
2860#endif
2861  Int iHorMin = (       -(Int)g_uiMaxCUWidth - iOffset - (Int)m_uiCUPelX + 1 ) << iMvShift;
2862 
2863#if SVC_EXTENSION
2864  Int iVerMax = ( m_pcSlice->getPicHeightInLumaSamples() + iOffset - m_uiCUPelY - 1 ) << iMvShift;
2865#else
2866  Int iVerMax = ( m_pcSlice->getSPS()->getPicHeightInLumaSamples() + iOffset - m_uiCUPelY - 1 ) << iMvShift;
2867#endif
2868  Int iVerMin = (       -(Int)g_uiMaxCUHeight - iOffset - (Int)m_uiCUPelY + 1 ) << iMvShift;
2869
2870  rcMv.setHor( min (iHorMax, max (iHorMin, rcMv.getHor())) );
2871  rcMv.setVer( min (iVerMax, max (iVerMin, rcMv.getVer())) );
2872}
2873
2874
2875UInt TComDataCU::getIntraSizeIdx(UInt uiAbsPartIdx)
2876{
2877  UInt uiShift = ( m_pePartSize[uiAbsPartIdx]==SIZE_NxN ? 1 : 0 );
2878
2879  UChar uiWidth = m_puhWidth[uiAbsPartIdx]>>uiShift;
2880  UInt  uiCnt = 0;
2881  while( uiWidth )
2882  {
2883    uiCnt++;
2884    uiWidth>>=1;
2885  }
2886  uiCnt-=2;
2887  return uiCnt > 6 ? 6 : uiCnt;
2888}
2889
2890Void TComDataCU::clearCbf( UInt uiIdx, ComponentID compID, UInt uiNumParts )
2891{
2892  memset( &m_puhCbf[compID][uiIdx], 0, sizeof(UChar)*uiNumParts);
2893}
2894
2895/** Set a I_PCM flag for all sub-partitions of a partition.
2896 * \param bIpcmFlag I_PCM flag
2897 * \param uiAbsPartIdx patition index
2898 * \param uiDepth CU depth
2899 * \returns Void
2900 */
2901Void TComDataCU::setIPCMFlagSubParts  (Bool bIpcmFlag, UInt uiAbsPartIdx, UInt uiDepth)
2902{
2903  UInt uiCurrPartNumb = m_pcPic->getNumPartitionsInCtu() >> (uiDepth << 1);
2904
2905  memset(m_pbIPCMFlag + uiAbsPartIdx, bIpcmFlag, sizeof(Bool)*uiCurrPartNumb );
2906}
2907
2908/** Test whether the current block is skipped
2909 * \param uiPartIdx Block index
2910 * \returns Flag indicating whether the block is skipped
2911 */
2912Bool TComDataCU::isSkipped( UInt uiPartIdx )
2913{
2914  return ( getSkipFlag( uiPartIdx ) );
2915}
2916
2917// ====================================================================================================================
2918// Protected member functions
2919// ====================================================================================================================
2920
2921Bool TComDataCU::xAddMVPCand( AMVPInfo* pInfo, RefPicList eRefPicList, Int iRefIdx, UInt uiPartUnitIdx, MVP_DIR eDir )
2922{
2923  TComDataCU* pcTmpCU = NULL;
2924  UInt uiIdx;
2925  switch( eDir )
2926  {
2927    case MD_LEFT:
2928    {
2929      pcTmpCU = getPULeft(uiIdx, uiPartUnitIdx);
2930      break;
2931    }
2932    case MD_ABOVE:
2933    {
2934      pcTmpCU = getPUAbove(uiIdx, uiPartUnitIdx);
2935      break;
2936    }
2937    case MD_ABOVE_RIGHT:
2938    {
2939      pcTmpCU = getPUAboveRight(uiIdx, uiPartUnitIdx);
2940      break;
2941    }
2942    case MD_BELOW_LEFT:
2943    {
2944      pcTmpCU = getPUBelowLeft(uiIdx, uiPartUnitIdx);
2945      break;
2946    }
2947    case MD_ABOVE_LEFT:
2948    {
2949      pcTmpCU = getPUAboveLeft(uiIdx, uiPartUnitIdx);
2950      break;
2951    }
2952    default:
2953    {
2954      break;
2955    }
2956  }
2957
2958  if ( pcTmpCU == NULL )
2959  {
2960    return false;
2961  }
2962
2963  if ( pcTmpCU->getCUMvField(eRefPicList)->getRefIdx(uiIdx) >= 0 && m_pcSlice->getRefPic( eRefPicList, iRefIdx)->getPOC() == pcTmpCU->getSlice()->getRefPOC( eRefPicList, pcTmpCU->getCUMvField(eRefPicList)->getRefIdx(uiIdx) ))
2964  {
2965    TComMv cMvPred = pcTmpCU->getCUMvField(eRefPicList)->getMv(uiIdx);
2966
2967    pInfo->m_acMvCand[ pInfo->iN++] = cMvPred;
2968    return true;
2969  }
2970
2971  RefPicList eRefPicList2nd = REF_PIC_LIST_0;
2972  if(       eRefPicList == REF_PIC_LIST_0 )
2973  {
2974    eRefPicList2nd = REF_PIC_LIST_1;
2975  }
2976  else if ( eRefPicList == REF_PIC_LIST_1)
2977  {
2978    eRefPicList2nd = REF_PIC_LIST_0;
2979  }
2980
2981
2982  Int iCurrRefPOC = m_pcSlice->getRefPic( eRefPicList, iRefIdx)->getPOC();
2983  Int iNeibRefPOC;
2984
2985
2986  if( pcTmpCU->getCUMvField(eRefPicList2nd)->getRefIdx(uiIdx) >= 0 )
2987  {
2988    iNeibRefPOC = pcTmpCU->getSlice()->getRefPOC( eRefPicList2nd, pcTmpCU->getCUMvField(eRefPicList2nd)->getRefIdx(uiIdx) );
2989    if( iNeibRefPOC == iCurrRefPOC ) // Same Reference Frame But Diff List//
2990    {
2991      TComMv cMvPred = pcTmpCU->getCUMvField(eRefPicList2nd)->getMv(uiIdx);
2992      pInfo->m_acMvCand[ pInfo->iN++] = cMvPred;
2993      return true;
2994    }
2995  }
2996  return false;
2997}
2998
2999/**
3000 * \param pInfo
3001 * \param eRefPicList
3002 * \param iRefIdx
3003 * \param uiPartUnitIdx
3004 * \param eDir
3005 * \returns Bool
3006 */
3007Bool TComDataCU::xAddMVPCandOrder( AMVPInfo* pInfo, RefPicList eRefPicList, Int iRefIdx, UInt uiPartUnitIdx, MVP_DIR eDir )
3008{
3009  TComDataCU* pcTmpCU = NULL;
3010  UInt uiIdx;
3011  switch( eDir )
3012  {
3013  case MD_LEFT:
3014    {
3015      pcTmpCU = getPULeft(uiIdx, uiPartUnitIdx);
3016      break;
3017    }
3018  case MD_ABOVE:
3019    {
3020      pcTmpCU = getPUAbove(uiIdx, uiPartUnitIdx);
3021      break;
3022    }
3023  case MD_ABOVE_RIGHT:
3024    {
3025      pcTmpCU = getPUAboveRight(uiIdx, uiPartUnitIdx);
3026      break;
3027    }
3028  case MD_BELOW_LEFT:
3029    {
3030      pcTmpCU = getPUBelowLeft(uiIdx, uiPartUnitIdx);
3031      break;
3032    }
3033  case MD_ABOVE_LEFT:
3034    {
3035      pcTmpCU = getPUAboveLeft(uiIdx, uiPartUnitIdx);
3036      break;
3037    }
3038  default:
3039    {
3040      break;
3041    }
3042  }
3043
3044  if ( pcTmpCU == NULL )
3045  {
3046    return false;
3047  }
3048
3049  RefPicList eRefPicList2nd = REF_PIC_LIST_0;
3050  if(       eRefPicList == REF_PIC_LIST_0 )
3051  {
3052    eRefPicList2nd = REF_PIC_LIST_1;
3053  }
3054  else if ( eRefPicList == REF_PIC_LIST_1)
3055  {
3056    eRefPicList2nd = REF_PIC_LIST_0;
3057  }
3058
3059  Int iCurrPOC = m_pcSlice->getPOC();
3060  Int iCurrRefPOC = m_pcSlice->getRefPic( eRefPicList, iRefIdx)->getPOC();
3061  Int iNeibPOC = iCurrPOC;
3062  Int iNeibRefPOC;
3063  Bool bIsCurrRefLongTerm = m_pcSlice->getRefPic( eRefPicList, iRefIdx)->getIsLongTerm();
3064  Bool bIsNeibRefLongTerm = false;
3065
3066  //---------------  V1 (END) ------------------//
3067  if( pcTmpCU->getCUMvField(eRefPicList)->getRefIdx(uiIdx) >= 0)
3068  {
3069    iNeibRefPOC = pcTmpCU->getSlice()->getRefPOC( eRefPicList, pcTmpCU->getCUMvField(eRefPicList)->getRefIdx(uiIdx) );
3070    TComMv cMvPred = pcTmpCU->getCUMvField(eRefPicList)->getMv(uiIdx);
3071    TComMv rcMv;
3072
3073    bIsNeibRefLongTerm = pcTmpCU->getSlice()->getRefPic( eRefPicList, pcTmpCU->getCUMvField(eRefPicList)->getRefIdx(uiIdx) )->getIsLongTerm();
3074    if ( bIsCurrRefLongTerm == bIsNeibRefLongTerm )
3075    {
3076      if ( bIsCurrRefLongTerm || bIsNeibRefLongTerm )
3077      {
3078        rcMv = cMvPred;
3079      }
3080      else
3081      {
3082        Int iScale = xGetDistScaleFactor( iCurrPOC, iCurrRefPOC, iNeibPOC, iNeibRefPOC );
3083        if ( iScale == 4096 )
3084        {
3085          rcMv = cMvPred;
3086        }
3087        else
3088        {
3089          rcMv = cMvPred.scaleMv( iScale );
3090        }
3091      }
3092
3093      pInfo->m_acMvCand[ pInfo->iN++] = rcMv;
3094      return true;
3095    }
3096  }
3097  //---------------------- V2(END) --------------------//
3098  if( pcTmpCU->getCUMvField(eRefPicList2nd)->getRefIdx(uiIdx) >= 0)
3099  {
3100    iNeibRefPOC = pcTmpCU->getSlice()->getRefPOC( eRefPicList2nd, pcTmpCU->getCUMvField(eRefPicList2nd)->getRefIdx(uiIdx) );
3101    TComMv cMvPred = pcTmpCU->getCUMvField(eRefPicList2nd)->getMv(uiIdx);
3102    TComMv rcMv;
3103
3104    bIsNeibRefLongTerm = pcTmpCU->getSlice()->getRefPic( eRefPicList2nd, pcTmpCU->getCUMvField(eRefPicList2nd)->getRefIdx(uiIdx) )->getIsLongTerm();
3105    if ( bIsCurrRefLongTerm == bIsNeibRefLongTerm )
3106    {
3107      if ( bIsCurrRefLongTerm || bIsNeibRefLongTerm )
3108      {
3109        rcMv = cMvPred;
3110      }
3111      else
3112      {
3113        Int iScale = xGetDistScaleFactor( iCurrPOC, iCurrRefPOC, iNeibPOC, iNeibRefPOC );
3114        if ( iScale == 4096 )
3115        {
3116          rcMv = cMvPred;
3117        }
3118        else
3119        {
3120          rcMv = cMvPred.scaleMv( iScale );
3121        }
3122      }
3123
3124      pInfo->m_acMvCand[ pInfo->iN++] = rcMv;
3125      return true;
3126    }
3127  }
3128  //---------------------- V3(END) --------------------//
3129  return false;
3130}
3131
3132/**
3133 * \param eRefPicList
3134 * \param uiCUAddr
3135 * \param uiPartUnitIdx
3136 * \param riRefIdx
3137 * \returns Bool
3138 */
3139Bool TComDataCU::xGetColMVP( RefPicList eRefPicList, Int ctuRsAddr, Int uiPartUnitIdx, TComMv& rcMv, Int& riRefIdx )
3140{
3141  UInt uiAbsPartAddr = uiPartUnitIdx;
3142
3143  RefPicList  eColRefPicList;
3144  Int iColPOC, iColRefPOC, iCurrPOC, iCurrRefPOC, iScale;
3145  TComMv cColMv;
3146
3147  // use coldir.
3148  TComPic *pColPic = getSlice()->getRefPic( RefPicList(getSlice()->isInterB() ? 1-getSlice()->getColFromL0Flag() : 0), getSlice()->getColRefIdx());
3149  TComDataCU *pColCtu = pColPic->getCtu( ctuRsAddr );
3150  if(pColCtu->getPic()==0||pColCtu->getPartitionSize(uiPartUnitIdx)==NUMBER_OF_PART_SIZES)
3151  {
3152    return false;
3153  }
3154  iCurrPOC = m_pcSlice->getPOC();
3155  iColPOC = pColCtu->getSlice()->getPOC();
3156
3157  if (!pColCtu->isInter(uiAbsPartAddr))
3158  {
3159    return false;
3160  }
3161
3162  eColRefPicList = getSlice()->getCheckLDC() ? eRefPicList : RefPicList(getSlice()->getColFromL0Flag());
3163
3164  Int iColRefIdx = pColCtu->getCUMvField(RefPicList(eColRefPicList))->getRefIdx(uiAbsPartAddr);
3165
3166  if (iColRefIdx < 0 )
3167  {
3168    eColRefPicList = RefPicList(1 - eColRefPicList);
3169    iColRefIdx = pColCtu->getCUMvField(RefPicList(eColRefPicList))->getRefIdx(uiAbsPartAddr);
3170
3171    if (iColRefIdx < 0 )
3172    {
3173      return false;
3174    }
3175  }
3176
3177  // Scale the vector.
3178  iColRefPOC = pColCtu->getSlice()->getRefPOC(eColRefPicList, iColRefIdx);
3179  cColMv = pColCtu->getCUMvField(eColRefPicList)->getMv(uiAbsPartAddr);
3180
3181  iCurrRefPOC = m_pcSlice->getRefPic(eRefPicList, riRefIdx)->getPOC();
3182
3183  Bool bIsCurrRefLongTerm = m_pcSlice->getRefPic(eRefPicList, riRefIdx)->getIsLongTerm();
3184  Bool bIsColRefLongTerm = pColCtu->getSlice()->getIsUsedAsLongTerm(eColRefPicList, iColRefIdx);
3185
3186  if ( bIsCurrRefLongTerm != bIsColRefLongTerm )
3187  {
3188    return false;
3189  }
3190
3191  if ( bIsCurrRefLongTerm || bIsColRefLongTerm )
3192  {
3193    rcMv = cColMv;
3194  }
3195  else
3196  {
3197    iScale = xGetDistScaleFactor(iCurrPOC, iCurrRefPOC, iColPOC, iColRefPOC);
3198    if ( iScale == 4096 )
3199    {
3200      rcMv = cColMv;
3201    }
3202    else
3203    {
3204      rcMv = cColMv.scaleMv( iScale );
3205    }
3206  }
3207
3208  return true;
3209}
3210
3211UInt TComDataCU::xGetMvdBits(TComMv cMvd)
3212{
3213  return ( xGetComponentBits(cMvd.getHor()) + xGetComponentBits(cMvd.getVer()) );
3214}
3215
3216UInt TComDataCU::xGetComponentBits(Int iVal)
3217{
3218  UInt uiLength = 1;
3219  UInt uiTemp   = ( iVal <= 0) ? (-iVal<<1)+1: (iVal<<1);
3220
3221  assert ( uiTemp );
3222
3223  while ( 1 != uiTemp )
3224  {
3225    uiTemp >>= 1;
3226    uiLength += 2;
3227  }
3228
3229  return uiLength;
3230}
3231
3232
3233Int TComDataCU::xGetDistScaleFactor(Int iCurrPOC, Int iCurrRefPOC, Int iColPOC, Int iColRefPOC)
3234{
3235  Int iDiffPocD = iColPOC - iColRefPOC;
3236  Int iDiffPocB = iCurrPOC - iCurrRefPOC;
3237
3238  if( iDiffPocD == iDiffPocB )
3239  {
3240    return 4096;
3241  }
3242  else
3243  {
3244    Int iTDB      = Clip3( -128, 127, iDiffPocB );
3245    Int iTDD      = Clip3( -128, 127, iDiffPocD );
3246    Int iX        = (0x4000 + abs(iTDD/2)) / iTDD;
3247    Int iScale    = Clip3( -4096, 4095, (iTDB * iX + 32) >> 6 );
3248    return iScale;
3249  }
3250}
3251
3252/**
3253 * \param eCUMode
3254 * \param uiPartIdx
3255 * \param ruiPartIdxCenter
3256 * \returns Void
3257 */
3258Void TComDataCU::xDeriveCenterIdx( UInt uiPartIdx, UInt& ruiPartIdxCenter )
3259{
3260  UInt uiPartAddr;
3261  Int  iPartWidth;
3262  Int  iPartHeight;
3263  getPartIndexAndSize( uiPartIdx, uiPartAddr, iPartWidth, iPartHeight);
3264
3265  ruiPartIdxCenter = m_absZIdxInCtu+uiPartAddr; // partition origin.
3266  ruiPartIdxCenter = g_auiRasterToZscan[ g_auiZscanToRaster[ ruiPartIdxCenter ]
3267                                        + ( iPartHeight/m_pcPic->getMinCUHeight()  )/2*m_pcPic->getNumPartInCtuWidth()
3268                                        + ( iPartWidth/m_pcPic->getMinCUWidth()  )/2];
3269}
3270
3271Void TComDataCU::compressMV()
3272{
3273  Int scaleFactor = 4 * AMVP_DECIMATION_FACTOR / m_unitSize;
3274  if (scaleFactor > 0)
3275  {
3276    for(UInt i=0; i<NUM_REF_PIC_LIST_01; i++)
3277    {
3278      m_acCUMvField[i].compress(m_pePredMode, scaleFactor);
3279    }
3280  }
3281}
3282
3283UInt TComDataCU::getCoefScanIdx(const UInt uiAbsPartIdx, const UInt uiWidth, const UInt uiHeight, const ComponentID compID) const
3284{
3285  //------------------------------------------------
3286
3287  //this mechanism is available for intra only
3288
3289  if (!isIntra(uiAbsPartIdx)) return SCAN_DIAG;
3290
3291  //------------------------------------------------
3292
3293  //check that MDCS can be used for this TU
3294
3295  const ChromaFormat format = getPic()->getChromaFormat();
3296
3297  const UInt maximumWidth  = MDCS_MAXIMUM_WIDTH  >> getComponentScaleX(compID, format);
3298  const UInt maximumHeight = MDCS_MAXIMUM_HEIGHT >> getComponentScaleY(compID, format);
3299
3300  if ((uiWidth > maximumWidth) || (uiHeight > maximumHeight)) return SCAN_DIAG;
3301
3302  //------------------------------------------------
3303
3304  //otherwise, select the appropriate mode
3305
3306  UInt uiDirMode  = getIntraDir(toChannelType(compID), uiAbsPartIdx);
3307
3308  if (uiDirMode==DM_CHROMA_IDX)
3309  {
3310    uiDirMode = getIntraDir(CHANNEL_TYPE_LUMA, getChromasCorrespondingPULumaIdx(uiAbsPartIdx, getPic()->getChromaFormat()));
3311  }
3312
3313  if (isChroma(compID) && (format == CHROMA_422))
3314  {
3315    uiDirMode = g_chroma422IntraAngleMappingTable[uiDirMode];
3316  }
3317
3318  //------------------
3319
3320  if      (abs((Int)uiDirMode - VER_IDX) <= MDCS_ANGLE_LIMIT) return SCAN_HOR;
3321  else if (abs((Int)uiDirMode - HOR_IDX) <= MDCS_ANGLE_LIMIT) return SCAN_VER;
3322  else return SCAN_DIAG;
3323}
3324
3325#if SVC_EXTENSION
3326TComDataCU* TComDataCU::getBaseColCU( UInt refLayerIdc, UInt uiCuAbsPartIdx, UInt &uiCUAddrBase, UInt &uiAbsPartIdxBase, Bool motionMapping )
3327{
3328  UInt uiPelX = getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiCuAbsPartIdx] ];
3329  UInt uiPelY = getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiCuAbsPartIdx] ];
3330
3331  return getBaseColCU( refLayerIdc, uiPelX, uiPelY, uiCUAddrBase, uiAbsPartIdxBase, motionMapping );
3332}
3333
3334TComDataCU* TComDataCU::getBaseColCU( UInt refLayerIdc, UInt pelX, UInt pelY, UInt &uiCUAddrBase, UInt &uiAbsPartIdxBase, Bool motionMapping )
3335{
3336  TComPic* baseColPic = m_pcSlice->getBaseColPic(refLayerIdc);
3337
3338  Int iPelX = Clip3<Int>(0, m_pcPic->getPicYuvRec()->getWidth(COMPONENT_Y)  - 1, pelX);
3339  Int iPelY = Clip3<Int>(0, m_pcPic->getPicYuvRec()->getHeight(COMPONENT_Y) - 1, pelY);
3340
3341  // centre of the collocated 16x16 block for motion mapping
3342  if( motionMapping )
3343  {
3344    iPelX = pelX + 8;
3345    iPelY = pelY + 8;
3346  }
3347
3348#if !LAYER_CTB
3349  UInt uiMinUnitSize = m_pcPic->getMinCUWidth();
3350#endif
3351
3352  Int leftStartL = m_pcSlice->getPPS()->getScaledRefLayerWindowForLayer(baseColPic->getSlice(0)->getVPS()->getRefLayerId(getSlice()->getLayerId(), refLayerIdc)).getWindowLeftOffset();
3353  Int topStartL  = m_pcSlice->getPPS()->getScaledRefLayerWindowForLayer(baseColPic->getSlice(0)->getVPS()->getRefLayerId(getSlice()->getLayerId(), refLayerIdc)).getWindowTopOffset();
3354
3355  const Window &windowRL = m_pcSlice->getPPS()->getRefLayerWindowForLayer(baseColPic->getSlice(0)->getVPS()->getRefLayerId(getSlice()->getLayerId(), refLayerIdc));
3356  Int iBX = (((iPelX - leftStartL)*g_posScalingFactor[refLayerIdc][0] + (1<<15)) >> 16) + windowRL.getWindowLeftOffset();
3357  Int iBY = (((iPelY - topStartL )*g_posScalingFactor[refLayerIdc][1] + (1<<15)) >> 16) + windowRL.getWindowTopOffset();
3358
3359  // offset for collocated block in the motion mapping
3360  if( motionMapping )
3361  {
3362    if( m_pcPic->equalPictureSizeAndOffsetFlag(refLayerIdc) )
3363    {
3364      // copy motion field from the same sample position for the case of 1x scaling ratio and same offset value between the current and reference layers
3365      iBX = pelX;
3366      iBY = pelY;
3367    }
3368    else
3369    {
3370      // actually, motion field compression is performed in the Void TComPic::compressMotion() function, but with (+4) the rounding may have effect on the picture boundary check.
3371      iBX = ( ( iBX + 4 ) >> 4 ) << 4;
3372      iBY = ( ( iBY + 4 ) >> 4 ) << 4;
3373    }
3374  }
3375
3376  if ( iBX < 0 || iBX >= baseColPic->getPicYuvRec()->getWidth(COMPONENT_Y) || iBY < 0 || iBY >= baseColPic->getPicYuvRec()->getHeight(COMPONENT_Y) )
3377  {
3378    return NULL;
3379  }
3380
3381#if LAYER_CTB
3382  UInt baseMaxCUHeight = baseColPic->getPicSym()->getMaxCUHeight();
3383  UInt baseMaxCUWidth  = baseColPic->getPicSym()->getMaxCUWidth();
3384  UInt baseMinUnitSize = baseColPic->getMinCUWidth();
3385 
3386  uiCUAddrBase = ( iBY / baseMaxCUHeight ) * baseColPic->getFrameWidthInCtus() + ( iBX / baseMaxCUWidth );
3387#else
3388  uiCUAddrBase = (iBY/g_uiMaxCUHeight)*baseColPic->getFrameWidthInCtus() + (iBX/g_uiMaxCUWidth);
3389#endif
3390
3391  assert(uiCUAddrBase < baseColPic->getNumberOfCtusInFrame());
3392
3393#if LAYER_CTB
3394  UInt uiRasterAddrBase = ( iBY - (iBY/baseMaxCUHeight)*baseMaxCUHeight ) / baseMinUnitSize * baseColPic->getNumPartInCtuWidth() + ( iBX - (iBX/baseMaxCUWidth)*baseMaxCUWidth ) / baseMinUnitSize;
3395 
3396  uiAbsPartIdxBase = g_auiLayerRasterToZscan[baseColPic->getLayerId()][uiRasterAddrBase];
3397#else
3398  UInt uiRasterAddrBase = (iBY - (iBY/g_uiMaxCUHeight)*g_uiMaxCUHeight)/uiMinUnitSize*baseColPic->getNumPartInCtuWidth() + (iBX - (iBX/g_uiMaxCUWidth)*g_uiMaxCUWidth)/uiMinUnitSize;
3399
3400  uiAbsPartIdxBase = g_auiRasterToZscan[uiRasterAddrBase];
3401#endif
3402
3403  return baseColPic->getCtu(uiCUAddrBase);
3404}
3405
3406Void TComDataCU::scaleBaseMV( UInt refLayerIdc, TComMvField& rcMvFieldEnhance, TComMvField& rcMvFieldBase )
3407{
3408  TComMvField cMvFieldBase;
3409  TComMv cMv;
3410
3411  cMv = rcMvFieldBase.getMv().scaleMv( g_mvScalingFactor[refLayerIdc][0], g_mvScalingFactor[refLayerIdc][1] );
3412
3413  rcMvFieldEnhance.setMvField( cMv, rcMvFieldBase.getRefIdx() );
3414}
3415
3416#if FAST_INTRA_SHVC
3417/** generate limited set of remaining modes
3418*\param   uiAbsPartIdx
3419*\param   uiIntraDirPred  pointer to the array for MPM storage
3420*\returns Number of intra coding modes (nb of remaining modes + 3 MPMs)
3421*/
3422Int TComDataCU::reduceSetOfIntraModes( UInt uiAbsPartIdx, Int* uiIntraDirPred, Int &fullSetOfModes )
3423{
3424  // check BL mode
3425  UInt uiCUAddrBase = 0, uiAbsPartAddrBase = 0;
3426  // the right reference layerIdc should be specified, currently it is set to m_layerId-1
3427  TComDataCU* pcTempCU = getBaseColCU(m_layerId - 1, uiAbsPartIdx, uiCUAddrBase, uiAbsPartAddrBase, false );
3428
3429  if( pcTempCU->getPredictionMode( uiAbsPartAddrBase ) != MODE_INTRA )
3430  {
3431    return( NUM_INTRA_MODE-1 );
3432  }
3433
3434  // compute set of enabled modes g_reducedSetIntraModes[...]
3435  Int authorizedMode[NUM_INTRA_MODE-1]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
3436  Int nbModes;
3437  for (nbModes=0; nbModes<3; nbModes++)  // add 3 MPMs 1st
3438  {
3439    g_reducedSetIntraModes[nbModes] = uiIntraDirPred[nbModes];
3440    authorizedMode[ uiIntraDirPred[nbModes] ] = 0;
3441  }
3442
3443  Int iColBaseDir = pcTempCU->getIntraDir( CHANNEL_TYPE_LUMA, uiAbsPartAddrBase );
3444  if ( authorizedMode[iColBaseDir] )  //possibly add BL mode
3445  {
3446    g_reducedSetIntraModes[nbModes++] = iColBaseDir;
3447    authorizedMode[ iColBaseDir ] = 0;
3448  }
3449
3450  Int iRefMode = ( iColBaseDir > 1 ) ? iColBaseDir : uiIntraDirPred[0];
3451  if ( iRefMode > 1 )    //add neighboring modes of refMode
3452  {
3453    UInt Left  = iRefMode;
3454    UInt Right = iRefMode;
3455    while ( nbModes < NB_REMAIN_MODES+3 )
3456    {
3457      Left = ((Left + 29) % 32) + 2;
3458      Right = ((Right - 1 ) % 32) + 2;
3459      if ( authorizedMode[Left] )   g_reducedSetIntraModes[nbModes++] = Left;
3460      if ( authorizedMode[Right] )  g_reducedSetIntraModes[nbModes++] = Right;
3461    }
3462  }
3463  else      //add pre-defined modes
3464  {
3465    Int  idx = 0;
3466    while ( nbModes < NB_REMAIN_MODES+3 )
3467    {
3468      UInt mode = g_predefSetIntraModes[idx++];
3469      if ( authorizedMode[mode] )   g_reducedSetIntraModes[nbModes++] = mode;
3470    }
3471  }
3472
3473  fullSetOfModes = 0;
3474  return ( nbModes );
3475}
3476#endif
3477
3478#if REF_IDX_ME_ZEROMV
3479Bool TComDataCU::xCheckZeroMVILRMerge(UChar uhInterDir, TComMvField& cMvFieldL0, TComMvField& cMvFieldL1)
3480{
3481  Bool checkZeroMVILR = true;
3482
3483  if(uhInterDir&0x1)  //list0
3484  {
3485    Int refIdxL0 = cMvFieldL0.getRefIdx();
3486    TComPic* refPic = m_pcSlice->getRefPic(REF_PIC_LIST_0, refIdxL0);
3487
3488    if(refPic->isILR(m_layerId))
3489    {
3490      checkZeroMVILR &= (cMvFieldL0.getHor() == 0 && cMvFieldL0.getVer() == 0);
3491
3492      // It is a requirement of bitstream conformance that when the reference picture represented by the variable refIdxLX is an inter-layer reference picture,
3493      // VpsInterLayerSamplePredictionEnabled[ LayerIdxInVps[ currLayerId ] ][ LayerIdxInVps[ rLId ] ] shall be equal to 1, where rLId is set equal to nuh_layer_id of the inter-layer picture
3494      checkZeroMVILR &= m_pcSlice->getVPS()->isSamplePredictionType( getLayerIdx(), refPic->getLayerIdx() );
3495    }
3496  }
3497  if(uhInterDir&0x2)  //list1
3498  {
3499    Int refIdxL1  = cMvFieldL1.getRefIdx();
3500    TComPic* refPic = m_pcSlice->getRefPic(REF_PIC_LIST_1, refIdxL1);
3501
3502    if(refPic->isILR(m_layerId))
3503    {
3504      checkZeroMVILR &= (cMvFieldL1.getHor() == 0 && cMvFieldL1.getVer() == 0);
3505
3506      // It is a requirement of bitstream conformance that when the reference picture represented by the variable refIdxLX is an inter-layer reference picture,
3507      // VpsInterLayerSamplePredictionEnabled[ LayerIdxInVps[ currLayerId ] ][ LayerIdxInVps[ rLId ] ] shall be equal to 1, where rLId is set equal to nuh_layer_id of the inter-layer picture
3508      checkZeroMVILR &= m_pcSlice->getVPS()->isSamplePredictionType( getLayerIdx(), refPic->getLayerIdx() );
3509    }
3510  }
3511
3512  return checkZeroMVILR;
3513}
3514
3515Bool TComDataCU::xCheckZeroMVILRMvdL1Zero(Int iRefList, Int iRefIdx, Int MvpIdx)
3516{
3517  RefPicList eRefPicList = iRefList > 0? REF_PIC_LIST_1: REF_PIC_LIST_0;
3518  assert(eRefPicList == REF_PIC_LIST_1);
3519
3520  Bool checkZeroMVILR = true;
3521
3522  if(getSlice()->getRefPic(eRefPicList, iRefIdx)->isILR(m_layerId))
3523  {
3524    AMVPInfo* pcAMVPInfo = getCUMvField(eRefPicList)->getAMVPInfo();
3525    TComMv    cMv        = pcAMVPInfo->m_acMvCand[MvpIdx];
3526    checkZeroMVILR &= (cMv.getHor() == 0 && cMv.getVer() == 0);
3527  }
3528
3529  return checkZeroMVILR;
3530}
3531#endif
3532
3533#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
3534Bool TComDataCU::isInterLayerReference(UChar uhInterDir, TComMvField& cMvFieldL0, TComMvField& cMvFieldL1)
3535{
3536  Bool checkILR = false;
3537
3538  if(uhInterDir&0x1)  //list0
3539  {
3540    Int refIdxL0 = cMvFieldL0.getRefIdx();
3541    checkILR = getSlice()->getRefPic(REF_PIC_LIST_0, refIdxL0)->isILR(m_layerId);
3542  }
3543  if(uhInterDir&0x2)  //list1
3544  {
3545    Int refIdxL1  = cMvFieldL1.getRefIdx();
3546    checkILR = checkILR || getSlice()->getRefPic(REF_PIC_LIST_1, refIdxL1)->isILR(m_layerId);
3547  }
3548
3549  return checkILR;
3550}
3551#endif
3552
3553#endif //SVC_EXTENSION
3554//! \}
Note: See TracBrowser for help on using the repository browser.