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

Last change on this file since 1444 was 1442, checked in by seregin, 10 years ago

port rev 4590

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