source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncCu.cpp @ 2

Last change on this file since 2 was 2, checked in by hhi, 13 years ago

inital import

  • Property svn:eol-style set to native
File size: 56.9 KB
Line 
1
2
3/** \file     TEncCU.cpp
4    \brief    CU encoder class
5*/
6
7#include <stdio.h>
8#include "TEncTop.h"
9#include "TEncCu.h"
10#include "TEncAnalyze.h"
11
12// ====================================================================================================================
13// Constructor / destructor / create / destroy
14// ====================================================================================================================
15
16/**
17 \param    uiTotalDepth  total number of allowable depth
18 \param    uiMaxWidth    largest CU width
19 \param    uiMaxHeight   largest CU height
20 */
21Void TEncCu::create(UChar uhTotalDepth, UInt uiMaxWidth, UInt uiMaxHeight)
22{
23  Int i;
24
25  m_uhTotalDepth   = uhTotalDepth + 1;
26  m_ppcBestCU      = new TComDataCU*[m_uhTotalDepth-1];
27  m_ppcTempCU      = new TComDataCU*[m_uhTotalDepth-1];
28
29  m_ppcPredYuvBest = new TComYuv*[m_uhTotalDepth-1];
30  m_ppcResiYuvBest = new TComYuv*[m_uhTotalDepth-1];
31  m_ppcRecoYuvBest = new TComYuv*[m_uhTotalDepth-1];
32  m_ppcPredYuvTemp = new TComYuv*[m_uhTotalDepth-1];
33  m_ppcResiYuvTemp = new TComYuv*[m_uhTotalDepth-1];
34  m_ppcRecoYuvTemp = new TComYuv*[m_uhTotalDepth-1];
35  m_ppcOrigYuv     = new TComYuv*[m_uhTotalDepth-1];
36  m_ppcResPredTmp  = new TComYuv*[m_uhTotalDepth-1];
37
38#if MW_MVI_SIGNALLING_MODE == 1
39  m_puhDepthSaved  = new UChar[1ll<<( ( m_uhTotalDepth - 1 )<<1 )];
40  m_puhWidthSaved  = new UChar[1ll<<( ( m_uhTotalDepth - 1 )<<1 )];
41  m_puhHeightSaved = new UChar[1ll<<( ( m_uhTotalDepth - 1 )<<1 )];
42#endif
43  UInt uiNumPartitions;
44  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
45  {
46    uiNumPartitions = 1<<( ( m_uhTotalDepth - i - 1 )<<1 );
47    UInt uiWidth  = uiMaxWidth  >> i;
48    UInt uiHeight = uiMaxHeight >> i;
49
50    m_ppcBestCU[i] = new TComDataCU; m_ppcBestCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false );
51    m_ppcTempCU[i] = new TComDataCU; m_ppcTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false );
52
53    m_ppcPredYuvBest[i] = new TComYuv; m_ppcPredYuvBest[i]->create(uiWidth, uiHeight);
54    m_ppcResiYuvBest[i] = new TComYuv; m_ppcResiYuvBest[i]->create(uiWidth, uiHeight);
55    m_ppcRecoYuvBest[i] = new TComYuv; m_ppcRecoYuvBest[i]->create(uiWidth, uiHeight);
56
57    m_ppcPredYuvTemp[i] = new TComYuv; m_ppcPredYuvTemp[i]->create(uiWidth, uiHeight);
58    m_ppcResiYuvTemp[i] = new TComYuv; m_ppcResiYuvTemp[i]->create(uiWidth, uiHeight);
59    m_ppcRecoYuvTemp[i] = new TComYuv; m_ppcRecoYuvTemp[i]->create(uiWidth, uiHeight);
60
61    m_ppcOrigYuv    [i] = new TComYuv; m_ppcOrigYuv    [i]->create(uiWidth, uiHeight);
62
63    m_ppcResPredTmp [i] = new TComYuv; m_ppcResPredTmp [i]->create(uiWidth, uiHeight);
64  }
65
66  // initialize partition order.
67  UInt* piTmp = &g_auiZscanToRaster[0];
68  initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp);
69  initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
70
71  // initialize conversion matrix from partition index to pel
72  initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
73}
74
75Void TEncCu::destroy()
76{
77  Int i;
78
79#if MW_MVI_SIGNALLING_MODE == 1
80  delete[] m_puhDepthSaved;  m_puhDepthSaved  = NULL;
81  delete[] m_puhWidthSaved;  m_puhWidthSaved  = NULL;
82  delete[] m_puhHeightSaved; m_puhHeightSaved = NULL;
83#endif
84  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
85  {
86    if(m_ppcBestCU[i])
87    {
88      m_ppcBestCU[i]->destroy();      delete m_ppcBestCU[i];      m_ppcBestCU[i] = NULL;
89    }
90    if(m_ppcTempCU[i])
91    {
92      m_ppcTempCU[i]->destroy();      delete m_ppcTempCU[i];      m_ppcTempCU[i] = NULL;
93    }
94    if(m_ppcPredYuvBest[i])
95    {
96      m_ppcPredYuvBest[i]->destroy(); delete m_ppcPredYuvBest[i]; m_ppcPredYuvBest[i] = NULL;
97    }
98    if(m_ppcResiYuvBest[i])
99    {
100      m_ppcResiYuvBest[i]->destroy(); delete m_ppcResiYuvBest[i]; m_ppcResiYuvBest[i] = NULL;
101    }
102    if(m_ppcRecoYuvBest[i])
103    {
104      m_ppcRecoYuvBest[i]->destroy(); delete m_ppcRecoYuvBest[i]; m_ppcRecoYuvBest[i] = NULL;
105    }
106    if(m_ppcPredYuvTemp[i])
107    {
108      m_ppcPredYuvTemp[i]->destroy(); delete m_ppcPredYuvTemp[i]; m_ppcPredYuvTemp[i] = NULL;
109    }
110    if(m_ppcResiYuvTemp[i])
111    {
112      m_ppcResiYuvTemp[i]->destroy(); delete m_ppcResiYuvTemp[i]; m_ppcResiYuvTemp[i] = NULL;
113    }
114    if(m_ppcRecoYuvTemp[i])
115    {
116      m_ppcRecoYuvTemp[i]->destroy(); delete m_ppcRecoYuvTemp[i]; m_ppcRecoYuvTemp[i] = NULL;
117    }
118    if(m_ppcOrigYuv[i])
119    {
120      m_ppcOrigYuv[i]->destroy();     delete m_ppcOrigYuv[i];     m_ppcOrigYuv[i] = NULL;
121    }
122    if(m_ppcResPredTmp[i])
123    {
124      m_ppcResPredTmp [i]->destroy(); delete m_ppcResPredTmp[i];  m_ppcResPredTmp[i] = NULL;
125    }
126  }
127  if(m_ppcBestCU)
128  {
129    delete [] m_ppcBestCU;
130    m_ppcBestCU = NULL;
131  }
132  if(m_ppcTempCU)
133  {
134    delete [] m_ppcTempCU;
135    m_ppcTempCU = NULL;
136  }
137
138  if(m_ppcPredYuvBest)
139  {
140    delete [] m_ppcPredYuvBest;
141    m_ppcPredYuvBest = NULL;
142  }
143  if(m_ppcResiYuvBest)
144  {
145    delete [] m_ppcResiYuvBest;
146    m_ppcResiYuvBest = NULL;
147  }
148  if(m_ppcRecoYuvBest)
149  {
150    delete [] m_ppcRecoYuvBest;
151    m_ppcRecoYuvBest = NULL;
152  }
153  if(m_ppcPredYuvTemp)
154  {
155    delete [] m_ppcPredYuvTemp;
156    m_ppcPredYuvTemp = NULL;
157  }
158  if(m_ppcResiYuvTemp)
159  {
160    delete [] m_ppcResiYuvTemp;
161    m_ppcResiYuvTemp = NULL;
162  }
163  if(m_ppcRecoYuvTemp)
164  {
165    delete [] m_ppcRecoYuvTemp;
166    m_ppcRecoYuvTemp = NULL;
167  }
168  if(m_ppcOrigYuv)
169  {
170    delete [] m_ppcOrigYuv;
171    m_ppcOrigYuv = NULL;
172  }
173  if(m_ppcResPredTmp)
174  {
175    delete [] m_ppcResPredTmp;
176    m_ppcResPredTmp = NULL;
177  }
178}
179
180/** \param    pcEncTop      pointer of encoder class
181 */
182Void TEncCu::init( TEncTop* pcEncTop )
183{
184  m_pcEncCfg           = pcEncTop;
185  m_pcEncTop           = pcEncTop;
186  m_pcPredSearch       = pcEncTop->getPredSearch();
187  m_pcTrQuant          = pcEncTop->getTrQuant();
188  m_pcBitCounter       = pcEncTop->getBitCounter();
189  m_pcRdCost           = pcEncTop->getRdCost();
190
191  m_pcEntropyCoder     = pcEncTop->getEntropyCoder();
192  m_pcCavlcCoder       = pcEncTop->getCavlcCoder();
193  m_pcSbacCoder       = pcEncTop->getSbacCoder();
194  m_pcBinCABAC         = pcEncTop->getBinCABAC();
195
196  m_pppcRDSbacCoder   = pcEncTop->getRDSbacCoder();
197  m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder();
198
199  m_bUseSBACRD        = pcEncTop->getUseSBACRD();
200}
201
202// ====================================================================================================================
203// Public member functions
204// ====================================================================================================================
205
206/** \param  rpcCU pointer of CU data class
207 */
208Void TEncCu::compressCU( TComDataCU*& rpcCU )
209{
210  // single-QP coding mode
211  if ( rpcCU->getSlice()->getSPS()->getUseDQP() == false )
212  {
213    // initialize CU data
214    m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
215    m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
216
217    // analysis of CU
218    xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
219  }
220  // multiple-QP coding mode
221  else
222  {
223    Int iQP  = rpcCU->getSlice()->getSliceQp();
224    Int idQP = m_pcEncCfg->getMaxDeltaQP();
225    Int i;
226    Int iBestQP = iQP;
227    Double fBestCost = MAX_DOUBLE;
228
229    rpcCU->getSlice()->setSliceQp( iQP );
230    m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
231    m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
232    m_ppcBestCU[0]->setQPSubParts( iQP, 0, 0 );
233    m_ppcTempCU[0]->setQPSubParts( iQP, 0, 0 );
234
235    // first try
236    xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
237
238    // for non-zero residual case
239    if ( !( m_ppcBestCU[0]->isSkipped( 0 ) && m_ppcBestCU[0]->getDepth( 0 ) == 0 ) )
240    {
241      // add dQP bits
242      m_pcEntropyCoder->resetBits();
243      m_pcEntropyCoder->encodeQP( m_ppcBestCU[0], 0, false );
244      m_ppcBestCU[0]->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
245
246
247//GT VSO
248      if ( m_pcRdCost->getUseLambdaScaleVSO() )
249      {
250        m_ppcBestCU[0]->getTotalCost() = m_pcRdCost->calcRdCostVSO( m_ppcBestCU[0]->getTotalBits(), m_ppcBestCU[0]->getTotalDistortion() );
251      }
252      else
253      {
254        m_ppcBestCU[0]->getTotalCost()  = m_pcRdCost->calcRdCost( m_ppcBestCU[0]->getTotalBits(), m_ppcBestCU[0]->getTotalDistortion() );
255      }
256//GT VSO end
257      fBestCost = m_ppcBestCU[0]->getTotalCost();
258
259      // try every case
260      for ( i=iQP-idQP; i<=iQP+idQP; i++ )
261      {
262        if ( i == iQP ) continue;
263
264        rpcCU->getSlice()->setSliceQp( i );
265        m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
266        m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
267        m_ppcBestCU[0]->setQPSubParts( i, 0, 0 );
268        m_ppcTempCU[0]->setQPSubParts( i, 0, 0 );
269
270        xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
271
272        // add dQP bits
273        rpcCU->getSlice()->setSliceQp( iQP );
274        m_pcEntropyCoder->resetBits();
275        m_pcEntropyCoder->encodeQP( m_ppcBestCU[0], 0, false );
276        m_ppcBestCU[0]->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
277
278        //GT VSO
279        if (m_pcRdCost->getUseLambdaScaleVSO())
280        {
281          m_ppcBestCU[0]->getTotalCost()  = m_pcRdCost->calcRdCostVSO( m_ppcBestCU[0]->getTotalBits(), m_ppcBestCU[0]->getTotalDistortion() );
282        }
283        else
284        {
285          m_ppcBestCU[0]->getTotalCost()  = m_pcRdCost->calcRdCost( m_ppcBestCU[0]->getTotalBits(), m_ppcBestCU[0]->getTotalDistortion() );
286        }
287        //GT VSO end
288
289        if ( fBestCost > m_ppcBestCU[0]->getTotalCost() )
290        {
291          fBestCost = m_ppcBestCU[0]->getTotalCost();
292          iBestQP   = i;
293        }
294      }
295
296      // perform best case
297      rpcCU->getSlice()->setSliceQp( iBestQP );
298      m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
299      m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
300      m_ppcBestCU[0]->setQPSubParts( iBestQP, 0, 0 );
301      m_ppcTempCU[0]->setQPSubParts( iBestQP, 0, 0 );
302
303      xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
304
305      // add dQP bits
306      rpcCU->getSlice()->setSliceQp( iQP );
307      m_pcEntropyCoder->resetBits();
308      m_pcEntropyCoder->encodeQP( m_ppcBestCU[0], 0, false );
309      m_ppcBestCU[0]->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
310
311      //GT VSO
312      if (m_pcRdCost->getUseLambdaScaleVSO())
313      {
314        m_ppcBestCU[0]->getTotalCost()  = m_pcRdCost->calcRdCostVSO( m_ppcBestCU[0]->getTotalBits(), m_ppcBestCU[0]->getTotalDistortion() );
315      }
316      else
317      {
318        m_ppcBestCU[0]->getTotalCost()  = m_pcRdCost->calcRdCost( m_ppcBestCU[0]->getTotalBits(), m_ppcBestCU[0]->getTotalDistortion() );
319      }
320     //GT VSO end
321    }
322  }
323}
324
325/** \param  pcCU  pointer of CU data class, bForceTerminate when set to true terminates slice (default is false).
326 */
327Void TEncCu::encodeCU ( TComDataCU* pcCU, Bool bForceTerminate )
328{
329#if SNY_DQP
330  if ( pcCU->getSlice()->getSPS()->getUseDQP() )
331  {
332    pcCU->setdQPFlag(true);
333  }
334#endif//SNY_DQP
335  // encode CU data
336  xEncodeCU( pcCU, 0, 0 );
337
338#if SNY_DQP
339  // dQP: only for LCU
340  if ( pcCU->getSlice()->getSPS()->getUseDQP() )
341  {
342    if ( pcCU->isSkipped( 0 ) && pcCU->getDepth( 0 ) == 0 )
343    {
344    }
345    else if ( pcCU->getdQPFlag())// non-skip
346    {
347
348      m_pcEntropyCoder->encodeQP( pcCU, 0 );
349      pcCU->setdQPFlag(false);
350    }
351  }
352#else
353  // dQP: only for LCU
354  if ( pcCU->getSlice()->getSPS()->getUseDQP() )
355  {
356    if ( pcCU->isSkipped( 0 ) && pcCU->getDepth( 0 ) == 0 )
357    {
358    }
359    else
360    {
361      m_pcEntropyCoder->encodeQP( pcCU, 0 );
362    }
363  }
364#endif//SNY_DQP
365
366  //--- write terminating bit ---
367  Bool bTerminateSlice = bForceTerminate;
368  UInt uiCUAddr = pcCU->getAddr();
369
370  if (uiCUAddr == (pcCU->getPic()->getNumCUsInFrame()-1) )
371    bTerminateSlice = true;
372
373  if (uiCUAddr == (pcCU->getSlice()->getSliceCurEndCUAddr()-1))
374    bTerminateSlice = true;
375
376  m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 );
377
378  // Encode slice finish
379  if ( bTerminateSlice )
380  {
381    m_pcEntropyCoder->encodeSliceFinish();
382  }
383}
384
385// ====================================================================================================================
386// Protected member functions
387// ====================================================================================================================
388
389Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
390{
391  TComPic* pcPic = rpcBestCU->getPic();
392
393  // get Original YUV data from picture
394  m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
395
396  // variables for fast encoder decision
397  TComDataCU* pcTempCU;
398  Bool    bEarlySkip  = false;
399  Bool    bTrySplit    = true;
400  Double  fRD_Skip    = MAX_DOUBLE;
401
402  static  Double  afCost[ MAX_CU_DEPTH ];
403  static  Int      aiNum [ MAX_CU_DEPTH ];
404
405  if ( rpcBestCU->getAddr() == 0 )
406  {
407    ::memset( afCost, 0, sizeof( afCost ) );
408    ::memset( aiNum,  0, sizeof( aiNum  ) );
409  }
410
411  Bool bBoundary = false;
412  UInt uiLPelX   = rpcBestCU->getCUPelX();
413  UInt uiRPelX   = uiLPelX + rpcBestCU->getWidth(0)  - 1;
414  UInt uiTPelY   = rpcBestCU->getCUPelY();
415  UInt uiBPelY   = uiTPelY + rpcBestCU->getHeight(0) - 1;
416
417#if ( SB_INTERVIEW_SKIP)
418  Bool bFullyRenderedSec = true ;
419  if( m_pcEncCfg->getInterViewSkip() )
420  {
421    Pel* pUsedSamples ;
422    UInt uiStride ;
423    pUsedSamples =  pcPic->getUsedPelsMap()->getLumaAddr( rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
424    uiStride = pcPic->getUsedPelsMap()->getStride();
425
426    for ( Int y=0; y<m_ppcOrigYuv[uiDepth]->getHeight(); y++)
427    {
428      for ( Int x=0; x<m_ppcOrigYuv[uiDepth]->getWidth(); x++)
429      {
430        if( pUsedSamples[x] !=0 )
431        {
432          bFullyRenderedSec = false ;
433          break ;
434        }
435      }
436      if ( !bFullyRenderedSec )
437      {
438        break;
439      }
440      pUsedSamples += uiStride ;
441    }
442  }
443  else
444  {
445    bFullyRenderedSec = false ;
446  }
447#if SB_INTERVIEW_SKIP_LAMBDA_SCALE
448  if( bFullyRenderedSec )
449  {
450    m_pcRdCost->setLambdaScale( m_pcEncCfg->getInterViewSkipLambdaScale() );
451  }
452  else
453  {
454    m_pcRdCost->setLambdaScale( 1 );
455  }
456  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
457#endif
458
459#endif
460  if( ( uiRPelX < rpcBestCU->getSlice()->getSPS()->getWidth() ) && ( uiBPelY < rpcBestCU->getSlice()->getSPS()->getHeight() ) )
461  {
462    // do inter modes
463    if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
464    {
465      // check availability of residual prediction
466      Bool  bResPredAvailable   = false;
467      Bool  bResPredAllowed     =                    (!rpcBestCU->getSlice()->getSPS()->isDepth                () );
468      bResPredAllowed           = bResPredAllowed && ( rpcBestCU->getSlice()->getSPS()->getViewId              () );
469      bResPredAllowed           = bResPredAllowed && ( rpcBestCU->getSlice()->getSPS()->getMultiviewResPredMode() );
470      if( bResPredAllowed )
471      {
472        bResPredAvailable       = rpcBestCU->getResidualSamples( 0, m_ppcResPredTmp[uiDepth] );
473      }
474
475      for( UInt uiResPrdId = 0; uiResPrdId < ( bResPredAvailable ? 2 : 1 ); uiResPrdId++ )
476      {
477        Bool bResPredFlag  = ( uiResPrdId > 0 );
478
479      // SKIP
480      pcTempCU = rpcTempCU;
481
482      if( pcPic->getSlice(0)->getSPS()->getUseMRG() )
483      {
484#if !HHI_MRG_SKIP
485        rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
486        xCheckRDCostAMVPSkip ( rpcBestCU, rpcTempCU );        rpcTempCU->initEstData();
487#endif
488        rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
489#if SB_INTERVIEW_SKIP
490        xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, bFullyRenderedSec );            rpcTempCU->initEstData();
491#else
492        xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU );            rpcTempCU->initEstData();
493#endif
494      }
495      else
496      {
497        rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
498        xCheckRDCostAMVPSkip ( rpcBestCU, rpcTempCU );        rpcTempCU->initEstData();
499      }
500
501      // fast encoder decision for early skip
502      if ( m_pcEncCfg->getUseFastEnc() )
503      {
504        Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
505        if ( aiNum [ iIdx ] > 5 && fRD_Skip < EARLY_SKIP_THRES*afCost[ iIdx ]/aiNum[ iIdx ] )
506        {
507          bEarlySkip = true;
508          bTrySplit  = false;
509        }
510      }
511
512      // 2Nx2N, NxN
513      if ( !bEarlySkip )
514      {
515#if HHI_DISABLE_INTER_NxN_SPLIT
516        rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
517#if SB_INTERVIEW_SKIP
518        xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFullyRenderedSec );  rpcTempCU->initEstData();
519#else
520        xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData();
521#endif
522        if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
523        {
524          rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
525#if SB_INTERVIEW_SKIP
526          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFullyRenderedSec );  rpcTempCU->initEstData();
527#else
528          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN   );  rpcTempCU->initEstData();
529#endif
530        }
531#else
532        rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
533#if SB_INTERVIEW_SKIP
534        xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFullyRenderedSec );  rpcTempCU->initEstData();
535#else
536        xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData();
537#endif
538        rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
539#if SB_INTERVIEW_SKIP
540        xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN, bFullyRenderedSec   );  rpcTempCU->initEstData();
541#else
542        xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN   );  rpcTempCU->initEstData();
543#endif
544#endif
545      }
546
547#if HHI_RMP_SWITCH
548      if( pcPic->getSlice(0)->getSPS()->getUseRMP() )
549#endif
550      { // 2NxN, Nx2N
551        rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
552#if SB_INTERVIEW_SKIP
553        xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N, bFullyRenderedSec  );  rpcTempCU->initEstData();
554#else
555        xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N  );  rpcTempCU->initEstData();
556#endif
557        rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
558#if SB_INTERVIEW_SKIP
559        xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN, bFullyRenderedSec  );  rpcTempCU->initEstData();
560#else
561        xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN  );  rpcTempCU->initEstData();
562#endif
563      }
564
565    } // uiResPrdId
566    }
567
568    // do normal intra modes
569    if ( !bEarlySkip )
570    {
571      // speedup for inter frames
572#if SB_INTERVIEW_SKIP
573      if( ( rpcBestCU->getSlice()->getSliceType() == I_SLICE ||
574               rpcBestCU->getCbf( 0, TEXT_LUMA     ) != 0   ||
575               rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0   ||
576               rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0 ) && !bFullyRenderedSec ) // avoid very complex intra if it is unlikely
577#else
578      if( rpcBestCU->getSlice()->getSliceType() == I_SLICE ||
579         rpcBestCU->getCbf( 0, TEXT_LUMA     ) != 0   ||
580         rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0   ||
581         rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0     ) // avoid very complex intra if it is unlikely
582#endif
583      {
584        xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); rpcTempCU->initEstData();
585#if MTK_DISABLE_INTRA_NxN_SPLIT
586        if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
587#endif
588        {
589          if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) )
590          {
591            xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN   ); rpcTempCU->initEstData();
592          }
593        }
594      }
595    }
596
597    m_pcEntropyCoder->resetBits();
598#if MW_MVI_SIGNALLING_MODE == 0
599    if( rpcBestCU->getSlice()->getSPS()->getUseMVI() && rpcBestCU->getSlice()->getSliceType() != I_SLICE )
600      m_pcEntropyCoder->encodeMvInheritanceFlag( rpcBestCU, 0, uiDepth, true );
601#endif
602    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
603    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
604
605    //GT VSO
606    if (m_pcRdCost->getUseLambdaScaleVSO())
607    {
608      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
609    }
610    else
611    {
612#if SB_INTERVIEW_SKIP
613  if(  m_pcEncCfg->getInterViewSkip())
614      {
615        TComYuv*  pRec    = m_ppcRecoYuvBest[ uiDepth ];
616        TComYuv*  pOrg    = m_ppcOrigYuv    [ uiDepth ];
617        Pel*      pUsedY  = pcPic->getUsedPelsMap()->getLumaAddr( rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
618        Pel*      pUsedU  = pcPic->getUsedPelsMap()->getCbAddr  ( rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
619        Pel*      pUsedV  = pcPic->getUsedPelsMap()->getCrAddr  ( rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
620        Int       iUStrdY = pcPic->getUsedPelsMap()->getStride  ();
621        Int       iUStrdC = pcPic->getUsedPelsMap()->getCStride ();
622        UInt      uiWdt   = rpcBestCU->getWidth ( 0 );
623        UInt      uiHgt   = rpcBestCU->getHeight( 0 );
624        UInt      uiDist  = ( m_pcRdCost->getDistPart( pRec->getLumaAddr(), pRec->getStride(),  pOrg->getLumaAddr(), pOrg->getStride(),  pUsedY, iUStrdY, uiWdt,      uiHgt      )
625                            + m_pcRdCost->getDistPart( pRec->getCbAddr(),   pRec->getCStride(), pOrg->getCbAddr(),   pOrg->getCStride(), pUsedU, iUStrdC, uiWdt >> 1, uiHgt >> 1 )
626                            + m_pcRdCost->getDistPart( pRec->getCrAddr(),   pRec->getCStride(), pOrg->getCrAddr(),   pOrg->getCStride(), pUsedV, iUStrdC, uiWdt >> 1, uiHgt >> 1 ) );
627//        printf("\nD(as is) = %d,   D(new) = %d,  diff = %d", rpcBestCU->getTotalDistortion(), uiDist, Int(rpcBestCU->getTotalDistortion()-uiDist) );
628        rpcBestCU->getTotalDistortion() = uiDist;
629      }
630#endif
631      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
632    }
633    //GT VSO end
634
635    // accumulate statistics for early skip
636    if ( m_pcEncCfg->getUseFastEnc() )
637    {
638      if ( rpcBestCU->isSkipped(0) )
639      {
640        Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
641        afCost[ iIdx ] += rpcBestCU->getTotalCost();
642        aiNum [ iIdx ] ++;
643      }
644    }
645#if MW_MVI_SIGNALLING_MODE == 0
646    if( rpcBestCU->getSlice()->getSPS()->getUseMVI() && rpcBestCU->getSlice()->getSliceType() != I_SLICE )
647    {
648      xCheckRDCostMvInheritance( rpcBestCU, rpcTempCU, uiDepth, false );  rpcTempCU->initEstData();
649    }
650#elif MW_MVI_SIGNALLING_MODE == 1
651    if( rpcBestCU->getSlice()->getSPS()->getUseMVI() && rpcBestCU->getSlice()->getSliceType() != I_SLICE )
652    {
653      xCheckRDCostMvInheritance( rpcBestCU, rpcTempCU, uiDepth, false, false ); rpcTempCU->initEstData();
654      rpcTempCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, 0, uiDepth );
655      rpcTempCU->setDepthSubParts( uiDepth, 0 );
656      xCheckRDCostMvInheritance( rpcBestCU, rpcTempCU, uiDepth, true, false );  rpcTempCU->initEstData();
657    }
658#endif
659  }
660  else
661  {
662    bBoundary = true;
663  }
664
665  // further split
666  if( bTrySplit && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
667  {
668    // reset Model
669    if( m_pcRdCost->getUseRenModel() )
670    {
671      UInt  uiWidth     = m_ppcBestCU[uiDepth]->getWidth ( 0 );
672      UInt  uiHeight    = m_ppcBestCU[uiDepth]->getHeight( 0 );
673      Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr( 0 );
674      UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride();
675      m_pcRdCost->setRenModelData( m_ppcBestCU[uiDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
676    }
677
678    UChar       uhNextDepth         = uiDepth+1;
679    TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
680    TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
681
682    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
683    {
684      pcSubBestPartCU->initSubCU( rpcBestCU, uiPartUnitIdx, uhNextDepth );           // clear sub partition datas or init.
685      pcSubTempPartCU->initSubCU( rpcBestCU, uiPartUnitIdx, uhNextDepth );           // clear sub partition datas or init.
686
687      if( ( pcSubBestPartCU->getCUPelX() < pcSubBestPartCU->getSlice()->getSPS()->getWidth() ) && ( pcSubBestPartCU->getCUPelY() < pcSubBestPartCU->getSlice()->getSPS()->getHeight() ) )
688      {
689        if( m_bUseSBACRD )
690        {
691          if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
692          {
693            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
694          }
695          else
696          {
697            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
698          }
699        }
700
701        xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
702
703        if( m_pcRdCost->getUseRenModel() )
704        {
705          UInt  uiWidth     = pcSubBestPartCU->getWidth ( 0 );
706          UInt  uiHeight    = pcSubBestPartCU->getHeight( 0 );
707          Pel*  piSrc       = m_ppcRecoYuvBest[pcSubBestPartCU->getDepth(0)]->getLumaAddr( 0 );
708          UInt  uiSrcStride = m_ppcRecoYuvBest[pcSubBestPartCU->getDepth(0)]->getStride();
709          m_pcRdCost->setRenModelData( pcSubBestPartCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
710        }
711        rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
712        xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
713      }
714    }
715
716    if( !bBoundary )
717    {
718      m_pcEntropyCoder->resetBits();
719      m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
720#if MW_MVI_SIGNALLING_MODE == 0
721      if( rpcBestCU->getSlice()->getSPS()->getUseMVI() && rpcBestCU->getSlice()->getSliceType() != I_SLICE )
722        m_pcEntropyCoder->encodeMvInheritanceFlag( rpcTempCU, 0, uiDepth, true );
723#endif
724
725      rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
726    }
727
728#if SB_INTERVIEW_SKIP_LAMBDA_SCALE
729    if( bFullyRenderedSec )
730    {
731      m_pcRdCost->setLambdaScale( m_pcEncCfg->getInterViewSkipLambdaScale() );
732    }
733    else
734    {
735      m_pcRdCost->setLambdaScale( 1 );
736    }
737#endif
738//GT VSO
739    if ( m_pcRdCost->getUseLambdaScaleVSO())
740    {
741      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
742    }
743    else
744    {
745      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
746    }
747//GT VSO end
748
749    if( m_bUseSBACRD )
750    {
751      m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
752    }
753    xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );                                          // RD compare current larger prediction
754
755    if( m_pcRdCost->getUseRenModel() )
756    {
757      UInt  uiWidth     = rpcBestCU->getWidth ( 0 );
758      UInt  uiHeight    = rpcBestCU->getHeight( 0 );
759      Pel*  piSrc       = m_ppcRecoYuvBest[uiDepth]->getLumaAddr( 0 );
760      UInt  uiSrcStride = m_ppcRecoYuvBest[uiDepth]->getStride();
761      m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
762    }
763  }                                                                                  // with sub partitioned prediction.
764
765  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
766
767  if( bBoundary )
768    return;
769
770  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth );   // Copy Yuv data to picture Yuv
771
772  // Assert if Best prediction mode is NONE
773  // Selected mode's RD-cost must be not MAX_DOUBLE.
774  assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE  );
775  assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE  );
776  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE );
777}
778
779/** encode a CU block recursively
780 * \param pcCU
781 * \param uiAbsPartIdx
782 * \param uiDepth
783 * \returns Void
784 */
785Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
786{
787  TComPic* pcPic = pcCU->getPic();
788
789  Bool bBoundary = false;
790  UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
791  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
792  UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
793  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
794
795#if MW_MVI_SIGNALLING_MODE == 0
796  if( pcCU->getSlice()->getSPS()->getUseMVI() && pcCU->getSlice()->getSliceType() != I_SLICE )
797  {
798    m_pcEntropyCoder->encodeMvInheritanceFlag( pcCU, uiAbsPartIdx, uiDepth );
799  }
800#endif
801  if( ( uiRPelX < pcCU->getSlice()->getSPS()->getWidth() ) && ( uiBPelY < pcCU->getSlice()->getSPS()->getHeight() ) )
802  {
803    if( pcCU->getTextureModeDepth( uiAbsPartIdx ) == -1 || uiDepth < pcCU->getTextureModeDepth( uiAbsPartIdx ) )
804      m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
805  }
806  else
807  {
808    bBoundary = true;
809  }
810
811#if MW_MVI_SIGNALLING_MODE == 1
812  if( uiDepth == pcCU->getTextureModeDepth( uiAbsPartIdx ) )
813  {
814    xSaveDepthWidthHeight( pcCU );
815    pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
816    pcCU->setDepthSubParts( uiDepth, uiAbsPartIdx );
817
818    if( ( uiRPelX < pcCU->getSlice()->getSPS()->getWidth() ) && ( uiBPelY < pcCU->getSlice()->getSPS()->getHeight() ) )
819      m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
820    if( !pcCU->getSlice()->isIntra() )
821    {
822      m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
823    }
824
825    if( pcCU->isSkipped( uiAbsPartIdx ) )
826    {
827#if HHI_MRG_SKIP
828      m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx, 0 );
829#else
830      if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 ) //if ( ref. frame list0 has at least 1 entry )
831      {
832        m_pcEntropyCoder->encodeMVPIdx( pcCU, uiAbsPartIdx, REF_PIC_LIST_0);
833      }
834      if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) > 0 ) //if ( ref. frame list1 has at least 1 entry )
835      {
836        m_pcEntropyCoder->encodeMVPIdx( pcCU, uiAbsPartIdx, REF_PIC_LIST_1);
837      }
838#endif
839      xRestoreDepthWidthHeight( pcCU );
840      return;
841    }
842
843    m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
844
845    m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
846
847    // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
848    m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
849    xRestoreDepthWidthHeight( pcCU );
850  }
851#endif
852
853  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary )
854  {
855    UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2;
856    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
857    {
858      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
859      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
860
861      if( ( uiLPelX < pcCU->getSlice()->getSPS()->getWidth() ) && ( uiTPelY < pcCU->getSlice()->getSPS()->getHeight() ) )
862        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
863    }
864    return;
865  }
866
867#if TSB_ALF_HEADER
868#else
869  m_pcEntropyCoder->encodeAlfCtrlFlag( pcCU, uiAbsPartIdx );
870#endif
871
872  if( !pcCU->getSlice()->isIntra() && pcCU->getTextureModeDepth( uiAbsPartIdx ) == -1 )
873  {
874    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
875  }
876
877  if( pcCU->isSkipped( uiAbsPartIdx ) )
878  {
879#if HHI_MRG_SKIP
880    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx, 0 );
881#else
882    if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 ) //if ( ref. frame list0 has at least 1 entry )
883    {
884      m_pcEntropyCoder->encodeMVPIdx( pcCU, uiAbsPartIdx, REF_PIC_LIST_0);
885    }
886    if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) > 0 ) //if ( ref. frame list1 has at least 1 entry )
887    {
888      m_pcEntropyCoder->encodeMVPIdx( pcCU, uiAbsPartIdx, REF_PIC_LIST_1);
889    }
890#endif
891    m_pcEntropyCoder->encodeResPredFlag( pcCU, uiAbsPartIdx, 0 );
892    return;
893  }
894  if( pcCU->getTextureModeDepth( uiAbsPartIdx ) == -1 )
895  {
896    m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
897
898    m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
899
900    // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
901    m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
902
903    if( !pcCU->isIntra( uiAbsPartIdx ) )
904    {
905      m_pcEntropyCoder->encodeResPredFlag( pcCU, uiAbsPartIdx, 0 );
906    }
907  }
908
909  // Encode Coefficients
910  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx) );
911}
912
913Void TEncCu::xCheckRDCostSkip( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool bSkipRes )
914{
915  UChar uhDepth = rpcTempCU->getDepth( 0 );
916
917  if( m_pcRdCost->getUseRenModel() )
918  {
919    UInt  uiWidth     = rpcTempCU->getWidth ( 0 );
920    UInt  uiHeight    = rpcTempCU->getHeight( 0 );
921    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
922    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
923    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
924  }
925
926  rpcTempCU->setPredModeSubParts( MODE_SKIP,   0, uhDepth );
927  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N,  0, uhDepth );
928
929  m_pcPredSearch->predInterSkipSearch       ( rpcTempCU,
930                                             m_ppcOrigYuv    [uhDepth],
931                                             m_ppcPredYuvTemp[uhDepth],
932                                             m_ppcResiYuvTemp[uhDepth],
933                                             m_ppcRecoYuvTemp[uhDepth] );
934
935  m_pcPredSearch->encodeResAndCalcRdInterCU ( rpcTempCU,
936                                             m_ppcOrigYuv    [uhDepth],
937                                             m_ppcPredYuvTemp[uhDepth],
938                                             m_ppcResiYuvTemp[uhDepth],
939                                             m_ppcResiYuvBest[uhDepth],
940                                             m_ppcRecoYuvTemp[uhDepth],
941                                             m_ppcResPredTmp [uhDepth],
942                                             bSkipRes );
943
944  xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
945}
946
947/** check RD costs for a CU block encoded with merge
948 * \param rpcBestCU
949 * \param rpcTempCU
950 * \returns Void
951 */
952#if SB_INTERVIEW_SKIP
953Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool bSkipRes )
954#else
955Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
956#endif
957{
958  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
959  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists
960  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
961  UInt uiNeighbourCandIdx[MRG_MAX_NUM_CANDS]; //MVs with same idx => same cand
962
963  Bool  bResPrdAvail  = rpcTempCU->getResPredAvail( 0 );
964  Bool  bResPrdFlag   = rpcTempCU->getResPredFlag ( 0 );
965
966  for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS; ++ui )
967  {
968    uhInterDirNeighbours[ui] = 0;
969    uiNeighbourCandIdx[ui] = 0;
970  }
971  UChar uhDepth = rpcTempCU->getDepth( 0 );
972
973  if( m_pcRdCost->getUseRenModel() )
974  {
975    UInt  uiWidth     = rpcTempCU->getWidth ( 0 );
976    UInt  uiHeight    = rpcTempCU->getHeight( 0 );
977    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
978    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
979    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
980  }
981
982  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
983  rpcTempCU->getInterMergeCandidates( 0, 0, uhDepth, cMvFieldNeighbours,uhInterDirNeighbours, uiNeighbourCandIdx );
984
985  Bool bValidCands = false;
986  for( UInt uiMergeCand = 0; uiMergeCand < MRG_MAX_NUM_CANDS; ++uiMergeCand )
987  {
988    if( uiNeighbourCandIdx[uiMergeCand] == ( uiMergeCand + 1 ) )
989    {
990#if HHI_MRG_SKIP
991      TComYuv* pcPredYuvTemp = NULL;
992#if SB_INTERVIEW_SKIP
993      for( UInt uiNoResidual = (bSkipRes ? 1:0); uiNoResidual < 2; ++uiNoResidual )
994#else
995      for( UInt uiNoResidual = 0; uiNoResidual < 2; ++uiNoResidual )
996#endif
997      {
998#endif
999      bValidCands = true;
1000      // set MC parameters
1001#if HHI_MRG_SKIP
1002      rpcTempCU->setPredModeSubParts( MODE_SKIP, 0, uhDepth ); // interprets depth relative to LCU level
1003#else
1004      rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1005#endif
1006      rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1007      rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1008      rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1009      rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1010      for( UInt uiInner = 0; uiInner < MRG_MAX_NUM_CANDS; uiInner++ )
1011      {
1012        rpcTempCU->setNeighbourCandIdxSubParts( uiInner, uiNeighbourCandIdx[uiInner], 0, 0,uhDepth );
1013      }
1014      rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand].getMv(), cMvFieldNeighbours[0 + 2*uiMergeCand].getRefIdx(), SIZE_2Nx2N, 0, 0, 0 ); // interprets depth relative to rpcTempCU level
1015      rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand].getMv(), cMvFieldNeighbours[1 + 2*uiMergeCand].getRefIdx(), SIZE_2Nx2N, 0, 0, 0 ); // interprets depth relative to rpcTempCU level
1016
1017      rpcTempCU->setResPredAvailSubParts( bResPrdAvail, 0, 0, uhDepth );
1018      rpcTempCU->setResPredFlagSubParts ( bResPrdFlag,  0, 0, uhDepth );
1019
1020#if HHI_MRG_SKIP
1021      // do MC
1022#if SB_INTERVIEW_SKIP
1023      if ( (uiNoResidual == 0) || bSkipRes ){
1024#else
1025      if ( uiNoResidual == 0 ){
1026#endif
1027        m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1028        // save pred adress
1029        pcPredYuvTemp = m_ppcPredYuvTemp[uhDepth];
1030
1031      }
1032      else {
1033        if ( pcPredYuvTemp != m_ppcPredYuvTemp[uhDepth]) {
1034          //adress changes take best (old temp)
1035          pcPredYuvTemp = m_ppcPredYuvBest[uhDepth];
1036        }
1037      }
1038
1039      if( m_pcRdCost->getUseRenModel() )
1040      { //Reset
1041        UInt  uiWidth     = rpcTempCU->getWidth ( 0 );
1042        UInt  uiHeight    = rpcTempCU->getHeight( 0 );
1043        Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1044        UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1045        m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1046      }
1047
1048      // estimate residual and encode everything
1049      m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1050                                                m_ppcOrigYuv    [uhDepth],
1051                                                pcPredYuvTemp,
1052                                                m_ppcResiYuvTemp[uhDepth],
1053                                                m_ppcResiYuvBest[uhDepth],
1054                                                m_ppcRecoYuvTemp[uhDepth],
1055                                                m_ppcResPredTmp [uhDepth],
1056                                                (uiNoResidual? true:false) );
1057      Bool bQtRootCbf = rpcTempCU->getQtRootCbf(0) == 1;
1058#else
1059      // do MC
1060      m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1061
1062      // estimate residual and encode everything
1063      m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1064                                                 m_ppcOrigYuv    [uhDepth],
1065                                                 m_ppcPredYuvTemp[uhDepth],
1066                                                 m_ppcResiYuvTemp[uhDepth],
1067                                                 m_ppcResiYuvBest[uhDepth],
1068                                                 m_ppcRecoYuvTemp[uhDepth],
1069                                                 m_ppcResPredTmp [uhDepth],
1070                                                 false );
1071#endif
1072      xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth );
1073
1074      rpcTempCU->initEstData();
1075#if HHI_MRG_SKIP
1076      if (!bQtRootCbf)
1077        break;
1078      }
1079#endif
1080    }
1081  }
1082}
1083
1084#if SB_INTERVIEW_SKIP
1085Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bSkipRes)
1086#else
1087Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
1088#endif
1089{
1090  UChar uhDepth = rpcTempCU->getDepth( 0 );
1091
1092  if( m_pcRdCost->getUseRenModel() )
1093  {
1094    UInt  uiWidth     = rpcTempCU->getWidth ( 0 );
1095    UInt  uiHeight    = rpcTempCU->getHeight( 0 );
1096    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1097    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1098    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1099  }
1100
1101  rpcTempCU->setDepthSubParts( uhDepth, 0 );
1102
1103  Bool  bResPrdAvail  = rpcTempCU->getResPredAvail( 0 );
1104  Bool  bResPrdFlag   = rpcTempCU->getResPredFlag ( 0 );
1105  rpcTempCU->setPartSizeSubParts    ( SIZE_2Nx2N,   0,    uhDepth );
1106  rpcTempCU->setResPredAvailSubParts( bResPrdAvail, 0, 0, uhDepth );
1107  rpcTempCU->setResPredFlagSubParts ( bResPrdFlag,  0, 0, uhDepth );
1108  rpcTempCU->setPartSizeSubParts    ( ePartSize,    0,    uhDepth );
1109  rpcTempCU->setPredModeSubParts    ( MODE_INTER,   0,    uhDepth );
1110
1111  if( rpcTempCU->getResPredFlag( 0 ) )
1112  { // subtract residual prediction from original in motion search
1113    m_ppcOrigYuv[uhDepth]->add( m_ppcResPredTmp [uhDepth], rpcTempCU->getWidth( 0 ), rpcTempCU->getHeight( 0 ), true );
1114  }
1115  #if SB_INTERVIEW_SKIP
1116  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bSkipRes );
1117#else
1118  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1119#endif
1120  if( rpcTempCU->getResPredFlag( 0 ) )
1121  { // add residual prediction to original again
1122    m_ppcOrigYuv[uhDepth]->add( m_ppcResPredTmp [uhDepth], rpcTempCU->getWidth( 0 ), rpcTempCU->getHeight( 0 ) );
1123  }
1124
1125#if PART_MRG
1126  if (rpcTempCU->getWidth(0) > 8 && !rpcTempCU->getMergeFlag(0) && (ePartSize != SIZE_2Nx2N && ePartSize != SIZE_NxN))
1127  {
1128    return;
1129  }
1130#endif
1131#if SB_INTERVIEW_SKIP
1132  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], m_ppcResPredTmp [uhDepth],bSkipRes );
1133#else
1134  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], m_ppcResPredTmp [uhDepth], false );
1135#endif
1136//GT VSO
1137  if( m_pcRdCost->getUseLambdaScaleVSO() )
1138  {
1139    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1140  }
1141  else
1142  {
1143    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1144  }
1145//GT VSO end
1146
1147  xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
1148}
1149
1150Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
1151{
1152  UInt uiDepth = rpcTempCU->getDepth( 0 );
1153
1154  // reset Model
1155  if( m_pcRdCost->getUseRenModel() )
1156  {
1157    UInt  uiWidth     = rpcTempCU->getWidth ( 0 );
1158    UInt  uiHeight    = rpcTempCU->getHeight( 0 );
1159    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr( );
1160    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride();
1161    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1162  }
1163
1164  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
1165  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1166
1167  Bool bSeparateLumaChroma = true; // choose estimation mode
1168  Dist uiPreCalcDistC      = 0;
1169  if( !bSeparateLumaChroma )
1170  {
1171    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
1172  }
1173  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
1174
1175#if LM_CHROMA
1176  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
1177#endif
1178
1179  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
1180
1181  m_pcEntropyCoder->resetBits();
1182  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1183  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
1184  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
1185  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
1186
1187  // Encode Coefficients
1188  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0) );
1189
1190  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1191
1192  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1193//GT VSO
1194  if( m_pcRdCost->getUseLambdaScaleVSO())
1195  {
1196    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1197  }
1198  else
1199  {
1200    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1201  }
1202//GT VSO end
1203
1204  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
1205}
1206
1207// check whether current try is the best
1208Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UChar uhDepth )
1209{
1210  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
1211  {
1212    TComYuv* pcYuv;
1213
1214    // Change Information data
1215    TComDataCU* pcCU = rpcBestCU;
1216    rpcBestCU = rpcTempCU;
1217    rpcTempCU = pcCU;
1218
1219    // Change Prediction data
1220    pcYuv = m_ppcPredYuvBest[uhDepth];
1221    m_ppcPredYuvBest[uhDepth] = m_ppcPredYuvTemp[uhDepth];
1222    m_ppcPredYuvTemp[uhDepth] = pcYuv;
1223
1224    // Change Reconstruction data
1225    pcYuv = m_ppcRecoYuvBest[uhDepth];
1226    m_ppcRecoYuvBest[uhDepth] = m_ppcRecoYuvTemp[uhDepth];
1227    m_ppcRecoYuvTemp[uhDepth] = pcYuv;
1228
1229    pcYuv = NULL;
1230    pcCU  = NULL;
1231
1232    if( m_bUseSBACRD )  // store temp best CI for next CU coding
1233      m_pppcRDSbacCoder[uhDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uhDepth][CI_NEXT_BEST]);
1234  }
1235}
1236
1237Void TEncCu::xCheckRDCostAMVPSkip           ( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
1238{
1239  UChar uhDepth = rpcTempCU->getDepth(0);
1240
1241  Bool  bResPrdAvail  = rpcTempCU->getResPredAvail( 0 );
1242  Bool  bResPrdFlag   = rpcTempCU->getResPredFlag ( 0 );
1243
1244  AMVPInfo cAMVPInfo0;
1245  cAMVPInfo0.iN = 0;
1246
1247  AMVPInfo cAMVPInfo1;
1248  cAMVPInfo1.iN = 0;
1249
1250  if (rpcTempCU->getAMVPMode(0) == AM_EXPL)
1251  {
1252    rpcTempCU->setPredModeSubParts( MODE_SKIP, 0, uhDepth );
1253    rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N,  0, uhDepth );
1254
1255    if ( rpcTempCU->getSlice()->isInterP() && rpcTempCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 )
1256    {
1257      rpcTempCU->fillMvpCand(0, 0, REF_PIC_LIST_0, 0, &cAMVPInfo0);
1258    }
1259    else if ( rpcTempCU->getSlice()->isInterB() &&
1260             rpcTempCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 &&
1261             rpcTempCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) > 0  )
1262    {
1263      rpcTempCU->fillMvpCand(0, 0, REF_PIC_LIST_0, 0, &cAMVPInfo0);
1264      rpcTempCU->fillMvpCand(0, 0, REF_PIC_LIST_1, 0, &cAMVPInfo1);
1265    }
1266    else
1267    {
1268      assert( 0 );
1269    }
1270  }
1271
1272  Int iMVP0, iMVP1;
1273
1274  for (iMVP0 = (cAMVPInfo0.iN > 0? 0:-1); iMVP0 < cAMVPInfo0.iN; iMVP0++)
1275  {
1276    for (iMVP1 = (cAMVPInfo1.iN > 0? 0:-1); iMVP1 < cAMVPInfo1.iN; iMVP1++)
1277    {
1278      rpcTempCU->setPredModeSubParts( MODE_SKIP, 0, uhDepth );
1279      rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N,  0, uhDepth );
1280      rpcTempCU->setResPredAvailSubParts( bResPrdAvail, 0, 0, uhDepth );
1281      rpcTempCU->setResPredFlagSubParts ( bResPrdFlag,  0, 0, uhDepth );
1282
1283      if (rpcTempCU->getSlice()->isInterB())
1284        rpcTempCU->setInterDirSubParts( 3, 0, 0, uhDepth );
1285
1286      rpcTempCU->setMVPIdxSubParts( iMVP0, REF_PIC_LIST_0, 0, 0, uhDepth );
1287      rpcTempCU->setMVPIdxSubParts( iMVP1, REF_PIC_LIST_1, 0, 0, uhDepth );
1288
1289      rpcTempCU->setMVPNumSubParts( cAMVPInfo0.iN, REF_PIC_LIST_0, 0, 0, uhDepth );
1290      rpcTempCU->setMVPNumSubParts( cAMVPInfo1.iN, REF_PIC_LIST_1, 0, 0, uhDepth );
1291
1292      xCopyAMVPInfo(&cAMVPInfo0, rpcTempCU->getCUMvField(REF_PIC_LIST_0)->getAMVPInfo());
1293      xCopyAMVPInfo(&cAMVPInfo1, rpcTempCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo());
1294      xCheckRDCostSkip ( rpcBestCU, rpcTempCU, true );      rpcTempCU->initEstData();
1295    }
1296  }
1297}
1298
1299Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
1300{
1301  pDst->iN = pSrc->iN;
1302  for (Int i = 0; i < pSrc->iN; i++)
1303  {
1304    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
1305  }
1306}
1307
1308Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsZorderIdx, UInt uiDepth)
1309{
1310  m_ppcRecoYuvBest[uiDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsZorderIdx );
1311}
1312
1313Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
1314{
1315  UInt uiCurrDepth = uiNextDepth - 1;
1316  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
1317}
1318
1319#if MW_MVI_SIGNALLING_MODE == 0
1320Void TEncCu::xCheckRDCostMvInheritance( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UChar uhTextureModeDepth, Bool bRecursiveCall )
1321#elif MW_MVI_SIGNALLING_MODE == 1
1322Void TEncCu::xCheckRDCostMvInheritance( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UChar uhTextureModeDepth, Bool bSkipResidual, Bool bRecursiveCall )
1323#else
1324#error
1325#endif
1326{
1327  assert( rpcTempCU->getSlice()->getSPS()->isDepth() );
1328  TComDataCU *pcTextureCU = rpcTempCU->getSlice()->getTexturePic()->getCU( rpcTempCU->getAddr() );
1329
1330  const UChar uhDepth  = rpcTempCU->getDepth( 0 );
1331  assert( bRecursiveCall == ( uhDepth != uhTextureModeDepth ) );
1332
1333  if( uhDepth == uhTextureModeDepth )
1334  {
1335    for( UInt ui = 0; ui < rpcTempCU->getTotalNumPart(); ui++ )
1336    {
1337      if( pcTextureCU->isIntra( rpcTempCU->getZorderIdxInCU() + ui ) )
1338      {
1339        return;
1340      }
1341    }
1342  }
1343
1344  if( m_pcRdCost->getUseRenModel() && !bRecursiveCall)
1345  {
1346    UInt  uiWidth     = m_ppcTempCU [uhDepth]->getWidth ( 0 );
1347    UInt  uiHeight    = m_ppcTempCU [uhDepth]->getHeight( 0 );
1348    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( 0 );
1349    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1350    m_pcRdCost->setRenModelData( m_ppcTempCU[uhDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1351  }
1352
1353  Bool bSplit = uhDepth < pcTextureCU->getDepth( rpcTempCU->getZorderIdxInCU() );
1354  if( bSplit )
1355  {
1356    const UChar       uhNextDepth   = uhDepth+1;
1357    TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
1358    TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
1359
1360    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
1361    {
1362      pcSubBestPartCU->initSubCU( rpcBestCU, uiPartUnitIdx, uhNextDepth );           // clear sub partition datas or init.
1363      pcSubTempPartCU->initSubCU( rpcBestCU, uiPartUnitIdx, uhNextDepth );           // clear sub partition datas or init.
1364
1365      if( ( pcSubBestPartCU->getCUPelX() < pcSubBestPartCU->getSlice()->getSPS()->getWidth() ) && ( pcSubBestPartCU->getCUPelY() < pcSubBestPartCU->getSlice()->getSPS()->getHeight() ) )
1366      {
1367        if( m_bUseSBACRD )
1368        {
1369          if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
1370          {
1371            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhDepth][CI_CURR_BEST]);
1372          }
1373          else
1374          {
1375            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
1376          }
1377        }
1378
1379#if MW_MVI_SIGNALLING_MODE == 0
1380        xCheckRDCostMvInheritance( pcSubBestPartCU, pcSubTempPartCU, uhTextureModeDepth, true );
1381#elif MW_MVI_SIGNALLING_MODE == 1
1382        xCheckRDCostMvInheritance( pcSubBestPartCU, pcSubTempPartCU, uhTextureModeDepth, bSkipResidual, true );
1383#endif
1384
1385        rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
1386        xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
1387      }
1388    }
1389
1390    if( uhDepth == uhTextureModeDepth )
1391    {
1392      xAddMVISignallingBits( rpcTempCU );
1393    }
1394
1395    if( m_bUseSBACRD )
1396    {
1397      m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uhDepth][CI_TEMP_BEST]);
1398    }
1399  }
1400  else
1401  {
1402    rpcTempCU->setTextureModeDepthSubParts( uhTextureModeDepth, 0, uhDepth );
1403    rpcTempCU->copyTextureMotionDataFrom( pcTextureCU, uhDepth, rpcTempCU->getZorderIdxInCU() );
1404    rpcTempCU->setPartSizeSubParts( SIZE_NxN, 0, uhDepth );
1405    for( UInt ui = 0; ui < rpcTempCU->getTotalNumPart(); ui++ )
1406    {
1407      assert( rpcTempCU->getInterDir( ui ) != 0 );
1408      assert( rpcTempCU->getPredictionMode( ui ) != MODE_NONE );
1409    }
1410#if MW_MVI_SIGNALLING_MODE == 0
1411    if( rpcTempCU->getPredictionMode( 0 ) == MODE_SKIP )
1412    {
1413      rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth );
1414    }
1415#elif MW_MVI_SIGNALLING_MODE == 1
1416    rpcTempCU->setPredModeSubParts( bSkipResidual ? MODE_SKIP : MODE_INTER, 0, uhDepth );
1417#endif
1418    m_pcPredSearch->motionCompensation( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1419
1420    // get Original YUV data from picture
1421    m_ppcOrigYuv[uhDepth]->copyFromPicYuv( rpcBestCU->getPic()->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
1422#if MW_MVI_SIGNALLING_MODE == 0
1423    m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], m_ppcResPredTmp [uhDepth], false );
1424#elif MW_MVI_SIGNALLING_MODE == 1
1425    m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], m_ppcResPredTmp [uhDepth], bSkipResidual );
1426#endif
1427
1428    if( uhDepth == uhTextureModeDepth )
1429    {
1430      xAddMVISignallingBits( rpcTempCU );
1431    }
1432  }
1433  //GT VSO
1434  if( m_pcRdCost->getUseLambdaScaleVSO() )
1435  {
1436    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1437  }
1438  else
1439  {
1440    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1441  }
1442  //GT VSO end
1443
1444  xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
1445
1446  if( !bSplit && bRecursiveCall && m_pcRdCost->getUseRenModel() )
1447  {
1448    UInt  uiWidth     = rpcBestCU->getWidth ( 0 );
1449    UInt  uiHeight    = rpcBestCU->getHeight( 0 );
1450    Pel*  piSrc       = m_ppcRecoYuvBest[uhDepth]->getLumaAddr( 0 );
1451    UInt  uiSrcStride = m_ppcRecoYuvBest[uhDepth]->getStride();
1452    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1453  }
1454}
1455
1456Void TEncCu::xAddMVISignallingBits( TComDataCU* pcCU )
1457{
1458  const UChar uhDepth = pcCU->getTextureModeDepth( 0 );
1459  m_pcEntropyCoder->resetBits();
1460#if MW_MVI_SIGNALLING_MODE == 0
1461  m_pcEntropyCoder->encodeMvInheritanceFlag( pcCU, 0, uhDepth, true );
1462#elif MW_MVI_SIGNALLING_MODE == 1
1463  xSaveDepthWidthHeight( pcCU );
1464  pcCU->setSizeSubParts( g_uiMaxCUWidth>>uhDepth, g_uiMaxCUHeight>>uhDepth, 0, uhDepth );
1465  pcCU->setDepthSubParts( uhDepth, 0 );
1466  pcCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth );
1467  pcCU->setMergeFlagSubParts( true, 0, 0, uhDepth );
1468  pcCU->setMergeIndexSubParts( 0, 0, 0, uhDepth );
1469
1470  {
1471    TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists
1472    UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1473    UInt uiNeighbourCandIdx[MRG_MAX_NUM_CANDS]; //MVs with same idx => same cand
1474
1475    for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS; ++ui )
1476    {
1477      uhInterDirNeighbours[ui] = 0;
1478      uiNeighbourCandIdx[ui] = 0;
1479    }
1480    pcCU->getInterMergeCandidates( 0, 0, uhDepth, cMvFieldNeighbours,uhInterDirNeighbours, uiNeighbourCandIdx );
1481    for( UInt uiMergeCand = 0; uiMergeCand < MRG_MAX_NUM_CANDS; uiMergeCand++ )
1482    {
1483      pcCU->setNeighbourCandIdxSubParts( uiMergeCand, uiNeighbourCandIdx[uiMergeCand], 0, 0,uhDepth );
1484    }
1485  }
1486
1487  // check for skip mode
1488  {
1489    Bool bAllZero = true;
1490    for( UInt ui = 0; ui < pcCU->getTotalNumPart(); ui++ )
1491    {
1492      if( pcCU->getCbf( ui, TEXT_LUMA ) || pcCU->getCbf( ui, TEXT_CHROMA_U ) || pcCU->getCbf( ui, TEXT_CHROMA_V ) )
1493      {
1494        bAllZero = false;
1495        break;
1496      }
1497    }
1498    if( bAllZero )
1499      pcCU->setPredModeSubParts( MODE_SKIP, 0, uhDepth );
1500  }
1501
1502
1503  m_pcEntropyCoder->encodeSplitFlag( pcCU, 0, uhDepth, true );
1504  m_pcEntropyCoder->encodeSkipFlag( pcCU, 0, true );
1505
1506  if( pcCU->isSkipped( 0 ) )
1507  {
1508#if HHI_MRG_SKIP
1509    m_pcEntropyCoder->encodeMergeIndex( pcCU, 0, 0, true );
1510#else
1511    if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 ) //if ( ref. frame list0 has at least 1 entry )
1512    {
1513      m_pcEntropyCoder->encodeMVPIdx( pcCU, 0, REF_PIC_LIST_0, true );
1514    }
1515    if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) > 0 ) //if ( ref. frame list1 has at least 1 entry )
1516    {
1517      m_pcEntropyCoder->encodeMVPIdx( pcCU, 0, REF_PIC_LIST_1, true );
1518    }
1519#endif
1520  }
1521  else
1522  {
1523    m_pcEntropyCoder->encodePredMode( pcCU, 0, true );
1524    m_pcEntropyCoder->encodePartSize( pcCU, 0, uhDepth, true );
1525    // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1526    m_pcEntropyCoder->encodePredInfo( pcCU, 0, true );
1527  }
1528  xRestoreDepthWidthHeight( pcCU );
1529#endif
1530
1531  pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits();
1532}
1533
1534#if MW_MVI_SIGNALLING_MODE == 1
1535Void TEncCu::xSaveDepthWidthHeight( TComDataCU* pcCU )
1536{
1537  const Int iSizeInUchar  = sizeof( UChar ) * pcCU->getTotalNumPart();
1538  memcpy( m_puhDepthSaved, pcCU->getDepth(), iSizeInUchar );
1539  memcpy( m_puhWidthSaved, pcCU->getWidth(), iSizeInUchar );
1540  memcpy( m_puhHeightSaved, pcCU->getHeight(), iSizeInUchar );
1541}
1542
1543Void TEncCu::xRestoreDepthWidthHeight( TComDataCU* pcCU )
1544{
1545  const Int iSizeInUchar  = sizeof( UChar ) * pcCU->getTotalNumPart();
1546  memcpy( pcCU->getDepth(), m_puhDepthSaved, iSizeInUchar );
1547  memcpy( pcCU->getWidth(), m_puhWidthSaved, iSizeInUchar );
1548  memcpy( pcCU->getHeight(), m_puhHeightSaved, iSizeInUchar );
1549}
1550#endif
Note: See TracBrowser for help on using the repository browser.