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

Last change on this file since 1483 was 1483, checked in by seregin, 9 years ago

remove m_layerId from TComDataCU

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