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

Last change on this file since 1502 was 1502, checked in by seregin, 8 years ago

infer parameters in SPS after activation, fixing chroma scaling for non 4:2:0

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