source: 3DVCSoftware/branches/HTM-6.2-dev1-Sharp/source/Lib/TLibEncoder/TEncCu.cpp @ 374

Last change on this file since 374 was 374, checked in by sharpjp-htm, 12 years ago

D0060: Removal of IC's parsing dependency

  • Property svn:eol-style set to native
File size: 101.5 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-2012, 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     TEncCu.cpp
35    \brief    Coding Unit (CU) encoder class
36*/
37
38#include <stdio.h>
39#include "TEncTop.h"
40#include "TEncCu.h"
41#include "TEncAnalyze.h"
42
43#include <cmath>
44#include <algorithm>
45using namespace std;
46
47//! \ingroup TLibEncoder
48//! \{
49
50// ====================================================================================================================
51// Constructor / destructor / create / destroy
52// ====================================================================================================================
53
54/**
55 \param    uiTotalDepth  total number of allowable depth
56 \param    uiMaxWidth    largest CU width
57 \param    uiMaxHeight   largest CU height
58 */
59Void TEncCu::create(UChar uhTotalDepth, UInt uiMaxWidth, UInt uiMaxHeight)
60{
61  Int i;
62 
63  m_uhTotalDepth   = uhTotalDepth + 1;
64  m_ppcBestCU      = new TComDataCU*[m_uhTotalDepth-1];
65  m_ppcTempCU      = new TComDataCU*[m_uhTotalDepth-1];
66 
67  m_ppcPredYuvBest = new TComYuv*[m_uhTotalDepth-1];
68  m_ppcResiYuvBest = new TComYuv*[m_uhTotalDepth-1];
69  m_ppcRecoYuvBest = new TComYuv*[m_uhTotalDepth-1];
70  m_ppcPredYuvTemp = new TComYuv*[m_uhTotalDepth-1];
71  m_ppcResiYuvTemp = new TComYuv*[m_uhTotalDepth-1];
72  m_ppcRecoYuvTemp = new TComYuv*[m_uhTotalDepth-1];
73  m_ppcOrigYuv     = new TComYuv*[m_uhTotalDepth-1];
74#if H3D_IVRP
75  m_ppcResPredTmp  = new TComYuv*[m_uhTotalDepth-1];
76#endif
77 
78#if HHI_MPI
79  m_puhDepthSaved  = new UChar[1ll<<( ( m_uhTotalDepth - 1 )<<1 )];
80  m_puhWidthSaved  = new UChar[1ll<<( ( m_uhTotalDepth - 1 )<<1 )];
81  m_puhHeightSaved = new UChar[1ll<<( ( m_uhTotalDepth - 1 )<<1 )];
82#endif
83
84  UInt uiNumPartitions;
85  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
86  {
87    uiNumPartitions = 1<<( ( m_uhTotalDepth - i - 1 )<<1 );
88    UInt uiWidth  = uiMaxWidth  >> i;
89    UInt uiHeight = uiMaxHeight >> i;
90   
91    m_ppcBestCU[i] = new TComDataCU; m_ppcBestCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
92    m_ppcTempCU[i] = new TComDataCU; m_ppcTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
93   
94    m_ppcPredYuvBest[i] = new TComYuv; m_ppcPredYuvBest[i]->create(uiWidth, uiHeight);
95    m_ppcResiYuvBest[i] = new TComYuv; m_ppcResiYuvBest[i]->create(uiWidth, uiHeight);
96    m_ppcRecoYuvBest[i] = new TComYuv; m_ppcRecoYuvBest[i]->create(uiWidth, uiHeight);
97   
98    m_ppcPredYuvTemp[i] = new TComYuv; m_ppcPredYuvTemp[i]->create(uiWidth, uiHeight);
99    m_ppcResiYuvTemp[i] = new TComYuv; m_ppcResiYuvTemp[i]->create(uiWidth, uiHeight);
100    m_ppcRecoYuvTemp[i] = new TComYuv; m_ppcRecoYuvTemp[i]->create(uiWidth, uiHeight);
101   
102    m_ppcOrigYuv    [i] = new TComYuv; m_ppcOrigYuv    [i]->create(uiWidth, uiHeight);
103#if H3D_IVRP
104    m_ppcResPredTmp [i] = new TComYuv; m_ppcResPredTmp [i]->create(uiWidth, uiHeight);
105#endif
106  }
107 
108  m_bEncodeDQP = false;
109  m_checkBurstIPCMFlag = false;
110
111  // initialize partition order.
112  UInt* piTmp = &g_auiZscanToRaster[0];
113  initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp);
114  initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
115 
116  // initialize conversion matrix from partition index to pel
117  initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
118  initMotionReferIdx ( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
119}
120
121Void TEncCu::destroy()
122{
123  Int i;
124 
125#if HHI_MPI
126  delete[] m_puhDepthSaved;  m_puhDepthSaved  = NULL;
127  delete[] m_puhWidthSaved;  m_puhWidthSaved  = NULL;
128  delete[] m_puhHeightSaved; m_puhHeightSaved = NULL;
129#endif
130  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
131  {
132    if(m_ppcBestCU[i])
133    {
134      m_ppcBestCU[i]->destroy();      delete m_ppcBestCU[i];      m_ppcBestCU[i] = NULL;
135    }
136    if(m_ppcTempCU[i])
137    {
138      m_ppcTempCU[i]->destroy();      delete m_ppcTempCU[i];      m_ppcTempCU[i] = NULL;
139    }
140    if(m_ppcPredYuvBest[i])
141    {
142      m_ppcPredYuvBest[i]->destroy(); delete m_ppcPredYuvBest[i]; m_ppcPredYuvBest[i] = NULL;
143    }
144    if(m_ppcResiYuvBest[i])
145    {
146      m_ppcResiYuvBest[i]->destroy(); delete m_ppcResiYuvBest[i]; m_ppcResiYuvBest[i] = NULL;
147    }
148    if(m_ppcRecoYuvBest[i])
149    {
150      m_ppcRecoYuvBest[i]->destroy(); delete m_ppcRecoYuvBest[i]; m_ppcRecoYuvBest[i] = NULL;
151    }
152    if(m_ppcPredYuvTemp[i])
153    {
154      m_ppcPredYuvTemp[i]->destroy(); delete m_ppcPredYuvTemp[i]; m_ppcPredYuvTemp[i] = NULL;
155    }
156    if(m_ppcResiYuvTemp[i])
157    {
158      m_ppcResiYuvTemp[i]->destroy(); delete m_ppcResiYuvTemp[i]; m_ppcResiYuvTemp[i] = NULL;
159    }
160    if(m_ppcRecoYuvTemp[i])
161    {
162      m_ppcRecoYuvTemp[i]->destroy(); delete m_ppcRecoYuvTemp[i]; m_ppcRecoYuvTemp[i] = NULL;
163    }
164    if(m_ppcOrigYuv[i])
165    {
166      m_ppcOrigYuv[i]->destroy();     delete m_ppcOrigYuv[i];     m_ppcOrigYuv[i] = NULL;
167    }
168#if H3D_IVRP
169    if(m_ppcResPredTmp[i])
170    {
171      m_ppcResPredTmp [i]->destroy(); delete m_ppcResPredTmp[i];  m_ppcResPredTmp[i] = NULL;
172    }
173#endif
174  }
175  if(m_ppcBestCU)
176  {
177    delete [] m_ppcBestCU;
178    m_ppcBestCU = NULL;
179  }
180  if(m_ppcTempCU)
181  {
182    delete [] m_ppcTempCU;
183    m_ppcTempCU = NULL;
184  }
185 
186  if(m_ppcPredYuvBest)
187  {
188    delete [] m_ppcPredYuvBest;
189    m_ppcPredYuvBest = NULL;
190  }
191  if(m_ppcResiYuvBest)
192  {
193    delete [] m_ppcResiYuvBest;
194    m_ppcResiYuvBest = NULL;
195  }
196  if(m_ppcRecoYuvBest)
197  {
198    delete [] m_ppcRecoYuvBest;
199    m_ppcRecoYuvBest = NULL;
200  }
201  if(m_ppcPredYuvTemp)
202  {
203    delete [] m_ppcPredYuvTemp;
204    m_ppcPredYuvTemp = NULL;
205  }
206  if(m_ppcResiYuvTemp)
207  {
208    delete [] m_ppcResiYuvTemp;
209    m_ppcResiYuvTemp = NULL;
210  }
211  if(m_ppcRecoYuvTemp)
212  {
213    delete [] m_ppcRecoYuvTemp;
214    m_ppcRecoYuvTemp = NULL;
215  }
216  if(m_ppcOrigYuv)
217  {
218    delete [] m_ppcOrigYuv;
219    m_ppcOrigYuv = NULL;
220  }
221#if H3D_IVRP
222  if(m_ppcResPredTmp)
223  {
224    delete [] m_ppcResPredTmp;
225    m_ppcResPredTmp = NULL;
226  }
227#endif
228}
229
230/** \param    pcEncTop      pointer of encoder class
231 */
232Void TEncCu::init( TEncTop* pcEncTop )
233{
234  m_pcEncCfg           = pcEncTop;
235  m_pcPredSearch       = pcEncTop->getPredSearch();
236  m_pcTrQuant          = pcEncTop->getTrQuant();
237  m_pcBitCounter       = pcEncTop->getBitCounter();
238  m_pcRdCost           = pcEncTop->getRdCost();
239 
240  m_pcEntropyCoder     = pcEncTop->getEntropyCoder();
241  m_pcCavlcCoder       = pcEncTop->getCavlcCoder();
242  m_pcSbacCoder       = pcEncTop->getSbacCoder();
243  m_pcBinCABAC         = pcEncTop->getBinCABAC();
244 
245  m_pppcRDSbacCoder   = pcEncTop->getRDSbacCoder();
246  m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder();
247 
248  m_bUseSBACRD        = pcEncTop->getUseSBACRD();
249}
250
251// ====================================================================================================================
252// Public member functions
253// ====================================================================================================================
254
255/** \param  rpcCU pointer of CU data class
256 */
257Void TEncCu::compressCU( TComDataCU*& rpcCU )
258{
259  // initialize CU data
260  m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
261  m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
262
263  // analysis of CU
264  xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
265
266#if ADAPTIVE_QP_SELECTION
267  if( m_pcEncCfg->getUseAdaptQpSelect() )
268  {
269    if(rpcCU->getSlice()->getSliceType()!=I_SLICE) //IIII
270    {
271      xLcuCollectARLStats( rpcCU);
272    }
273  }
274#endif
275}
276/** \param  pcCU  pointer of CU data class, bForceTerminate when set to true terminates slice (default is false).
277 */
278Void TEncCu::encodeCU ( TComDataCU* pcCU, Bool bForceTerminate )
279{
280  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
281  {
282    setdQPFlag(true);
283  }
284
285  TComPic* pcPic = pcCU->getPic();
286  Bool checkBurstIPCMFlag = (pcPic->getSlice(0)->getSPS()->getUsePCM())? true : false;
287
288  setCheckBurstIPCMFlag( checkBurstIPCMFlag );
289
290  pcCU->setNumSucIPCM(0);
291  pcCU->setLastCUSucIPCMFlag(false);
292
293  // Encode CU data
294  xEncodeCU( pcCU, 0, 0 );
295 
296  bool bTerminateSlice = bForceTerminate;
297  UInt uiCUAddr = pcCU->getAddr();
298    /* If at the end of an LCU line but not at the end of a substream, perform CABAC flush */
299    if (!bTerminateSlice && pcCU->getSlice()->getPPS()->getNumSubstreams() > 1)
300    {
301      Int iNumSubstreams = pcCU->getSlice()->getPPS()->getNumSubstreams();
302      UInt uiWidthInLCUs = pcCU->getPic()->getPicSym()->getFrameWidthInCU();
303      UInt uiCol     = uiCUAddr % uiWidthInLCUs;
304      UInt uiLin     = uiCUAddr / uiWidthInLCUs;
305      UInt uiTileStartLCU = pcCU->getPic()->getPicSym()->getTComTile(pcCU->getPic()->getPicSym()->getTileIdxMap(uiCUAddr))->getFirstCUAddr();
306      UInt uiTileLCUX = uiTileStartLCU % uiWidthInLCUs;
307      UInt uiTileLCUY = uiTileStartLCU / uiWidthInLCUs;
308      UInt uiTileWidth = pcCU->getPic()->getPicSym()->getTComTile(pcCU->getPic()->getPicSym()->getTileIdxMap(uiCUAddr))->getTileWidth();
309      UInt uiTileHeight = pcCU->getPic()->getPicSym()->getTComTile(pcCU->getPic()->getPicSym()->getTileIdxMap(uiCUAddr))->getTileHeight();
310      Int iNumSubstreamsPerTile = iNumSubstreams;
311      if (pcCU->getSlice()->getPPS()->getNumSubstreams() > 1)
312      {
313        iNumSubstreamsPerTile /= pcCU->getPic()->getPicSym()->getNumTiles();
314      }
315      if ((uiCol == uiTileLCUX+uiTileWidth-1) && (uiLin+iNumSubstreamsPerTile < uiTileLCUY+uiTileHeight))
316      {
317        m_pcEntropyCoder->encodeFlush();
318      }
319    }
320}
321
322// ====================================================================================================================
323// Protected member functions
324// ====================================================================================================================
325/** Derive small set of test modes for AMP encoder speed-up
326 *\param   rpcBestCU
327 *\param   eParentPartSize
328 *\param   bTestAMP_Hor
329 *\param   bTestAMP_Ver
330 *\param   bTestMergeAMP_Hor
331 *\param   bTestMergeAMP_Ver
332 *\returns Void
333*/
334#if AMP_ENC_SPEEDUP
335#if AMP_MRG
336Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver, Bool &bTestMergeAMP_Hor, Bool &bTestMergeAMP_Ver)
337#else
338Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver)
339#endif
340{
341  if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
342  {
343    bTestAMP_Hor = true;
344  }
345  else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
346  {
347    bTestAMP_Ver = true;
348  }
349  else if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->getMergeFlag(0) == false && rpcBestCU->isSkipped(0) == false )
350  {
351    bTestAMP_Hor = true;         
352    bTestAMP_Ver = true;         
353  }
354
355#if AMP_MRG
356  //! Utilizing the partition size of parent PU   
357  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
358  { 
359    bTestMergeAMP_Hor = true;
360    bTestMergeAMP_Ver = true;
361  }
362
363  if ( eParentPartSize == SIZE_NONE ) //! if parent is intra
364  {
365    if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
366    {
367      bTestMergeAMP_Hor = true;
368    }
369    else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
370    {
371      bTestMergeAMP_Ver = true;
372    }
373  }
374
375  if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->isSkipped(0) == false )
376  {
377    bTestMergeAMP_Hor = true;         
378    bTestMergeAMP_Ver = true;         
379  }
380
381  if ( rpcBestCU->getWidth(0) == 64 )
382  { 
383    bTestAMP_Hor = false;
384    bTestAMP_Ver = false;
385  }   
386#else
387  //! Utilizing the partition size of parent PU       
388  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
389  { 
390    bTestAMP_Hor = true;
391    bTestAMP_Ver = true;
392  }
393
394  if ( eParentPartSize == SIZE_2Nx2N )
395  { 
396    bTestAMP_Hor = false;
397    bTestAMP_Ver = false;
398  }     
399#endif
400}
401#endif
402
403// ====================================================================================================================
404// Protected member functions
405// ====================================================================================================================
406/** Compress a CU block recursively with enabling sub-LCU-level delta QP
407 *\param   rpcBestCU
408 *\param   rpcTempCU
409 *\param   uiDepth
410 *\returns Void
411 *
412 *- for loop of QP value to compress the current CU with all possible QP
413*/
414#if AMP_ENC_SPEEDUP
415Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth, PartSize eParentPartSize )
416#else
417Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
418#endif
419{
420  TComPic* pcPic = rpcBestCU->getPic();
421
422#if H3D_QTL
423  TComSPS *sps         = pcPic->getSlice(0)->getSPS();
424  TComPic *pcTexture   = rpcBestCU->getSlice()->getTexturePic();
425
426  Bool  depthMapDetect    = (pcTexture != NULL);
427  Bool  bIntraSliceDetect = (rpcBestCU->getSlice()->getSliceType() == I_SLICE);
428
429  Bool rapPic     = (rpcBestCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR || rpcBestCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA);
430
431  Bool bTry2NxN = true;
432  Bool bTryNx2N = true;
433#endif
434
435  // get Original YUV data from picture
436  m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
437
438  // variables for fast encoder decision
439  Bool    bEarlySkip  = false;
440  Bool    bTrySplit   = true;
441  Double  fRD_Skip    = MAX_DOUBLE;
442
443  // variable for Early CU determination
444  Bool    bSubBranch = true;
445
446  // variable for Cbf fast mode PU decision
447  Bool    doNotBlockPu = true;
448
449  Bool    bTrySplitDQP  = true;
450
451  static  Double  afCost [ MAX_CU_DEPTH ];
452  static  Int      aiNum [ MAX_CU_DEPTH ];
453
454  if ( rpcBestCU->getAddr() == 0 )
455  {
456    ::memset( afCost, 0, sizeof( afCost ) );
457    ::memset( aiNum,  0, sizeof( aiNum  ) );
458  }
459
460  Bool bBoundary = false;
461  UInt uiLPelX   = rpcBestCU->getCUPelX();
462  UInt uiRPelX   = uiLPelX + rpcBestCU->getWidth(0)  - 1;
463  UInt uiTPelY   = rpcBestCU->getCUPelY();
464  UInt uiBPelY   = uiTPelY + rpcBestCU->getHeight(0) - 1;
465
466#if LGE_ILLUCOMP_B0045
467  Bool bICEnabled = (
468#if !LGE_ILLUCOMP_DEPTH_C0046
469      !rpcTempCU->getSlice()->getIsDepth() && 
470#endif
471      rpcTempCU->getSlice()->getViewId());
472
473  bICEnabled = bICEnabled && rpcTempCU->getSlice()->getApplyIC();
474#endif
475
476#if HHI_INTERVIEW_SKIP
477  Bool bFullyRenderedSec = true ;
478  if( m_pcEncCfg->getInterViewSkip() )
479  {
480    Pel* pUsedSamples ;
481    UInt uiStride ;
482    pUsedSamples =  pcPic->getUsedPelsMap()->getLumaAddr( rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
483    uiStride = pcPic->getUsedPelsMap()->getStride();
484
485    for ( Int y=0; y<m_ppcOrigYuv[uiDepth]->getHeight(); y++)
486    {
487      for ( Int x=0; x<m_ppcOrigYuv[uiDepth]->getWidth(); x++)
488      {
489        if( pUsedSamples[x] !=0 )
490        {
491          bFullyRenderedSec = false ;
492          break ;
493        }
494      }
495      if ( !bFullyRenderedSec )
496      {
497        break;
498      }
499      pUsedSamples += uiStride ;
500    }
501  }
502  else
503  {
504    bFullyRenderedSec = false ;
505  }
506
507#if HHI_INTERVIEW_SKIP_LAMBDA_SCALE
508  if( bFullyRenderedSec )
509  {
510    m_pcRdCost->setLambdaScale( m_pcEncCfg->getInterViewSkipLambdaScale() );
511  }
512  else
513  {
514    m_pcRdCost->setLambdaScale( 1 );
515  }
516  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
517#endif
518
519#endif
520  Int iBaseQP = xComputeQP( rpcBestCU, uiDepth );
521  Int iMinQP;
522  Int iMaxQP;
523#if LOSSLESS_CODING
524  Bool isAddLowestQP = false;
525  Int lowestQP = -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY();
526#endif
527
528  if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
529  {
530    Int idQP = m_pcEncCfg->getMaxDeltaQP();
531    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
532    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
533#if LOSSLESS_CODING
534    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
535    {
536      isAddLowestQP = true; 
537      iMinQP = iMinQP - 1;
538
539    }
540#endif
541  }
542  else
543  {
544    iMinQP = rpcTempCU->getQP(0);
545    iMaxQP = rpcTempCU->getQP(0);
546  }
547
548  // If slice start or slice end is within this cu...
549  TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx());
550  Bool bSliceStart = pcSlice->getEntropySliceCurStartCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getEntropySliceCurStartCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart();
551  Bool bSliceEnd = (pcSlice->getEntropySliceCurEndCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getEntropySliceCurEndCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart());
552  Bool bInsidePicture = ( uiRPelX < rpcBestCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < rpcBestCU->getSlice()->getSPS()->getPicHeightInLumaSamples() );
553  // We need to split, so don't try these modes.
554  if(!bSliceEnd && !bSliceStart && bInsidePicture )
555  {
556    for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
557    {
558#if LOSSLESS_CODING
559      if (isAddLowestQP && (iQP == iMinQP))
560      {
561        iQP = lowestQP;
562      }
563#endif
564      // variables for fast encoder decision
565      bEarlySkip  = false;
566      bTrySplit   = true;
567      fRD_Skip    = MAX_DOUBLE;
568
569      rpcTempCU->initEstData( uiDepth, iQP );
570
571#if H3D_QTL
572      //logic for setting bTrySplit using the partition information that is stored of the texture colocated CU
573
574      if(depthMapDetect && !bIntraSliceDetect && !rapPic && sps->getUseQTLPC())
575      {
576        TComDataCU* pcTextureCU = pcTexture->getCU( rpcBestCU->getAddr() ); //Corresponding texture LCU
577        UInt uiCUIdx            = rpcBestCU->getZorderIdxInCU();
578        assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth); //Depth cannot be more partitionned than the texture.
579        if (pcTextureCU->getDepth(uiCUIdx) > uiDepth || pcTextureCU->getPartitionSize(uiCUIdx) == SIZE_NxN) //Texture was split.
580        {
581          bTrySplit = true;
582          bTryNx2N  = true;
583          bTry2NxN  = true;
584        }
585        else
586        {
587          bTrySplit = false;
588          bTryNx2N  = false;
589          bTry2NxN  = false;
590        }
591      }
592#endif
593
594      // do inter modes, SKIP and 2Nx2N
595      if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
596      {
597#if H3D_IVRP
598        Bool  bResPredAvailable   = false;
599        UInt uiResPrdId = 0; 
600        {
601          Bool bResPredFlag  = ( uiResPrdId > 0 );
602#if LGE_ILLUCOMP_B0045
603          for(UInt uiICId = 0; uiICId < (bICEnabled ? 2 : 1); uiICId++)
604          {
605            Bool bICFlag = (uiICId ? true : false);
606            rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
607#endif
608#endif
609#if H3D_IVRP
610          rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
611#endif
612          // SKIP
613#if HHI_INTERVIEW_SKIP
614          xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, bFullyRenderedSec );
615#else
616          xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU );
617#endif
618          rpcTempCU->initEstData( uiDepth, iQP );
619
620          // fast encoder decision for early skip
621          if ( m_pcEncCfg->getUseFastEnc() )
622          {
623            Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
624            if ( aiNum [ iIdx ] > 5 && fRD_Skip < EARLY_SKIP_THRES*afCost[ iIdx ]/aiNum[ iIdx ] )
625            {
626              bEarlySkip = true;
627              bTrySplit  = false;
628            }
629          }
630#if LGE_ILLUCOMP_B0045
631          if(bICFlag && rpcBestCU->getMergeFlag(0) && !rpcBestCU->getICFlag(0))
632          {
633             bICEnabled = false;
634             break;
635          }
636#endif
637          // 2Nx2N, NxN
638          if ( !bEarlySkip )
639          {
640#if H3D_IVRP
641            rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
642#endif
643#if LGE_ILLUCOMP_B0045
644            rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
645#endif
646#if HHI_INTERVIEW_SKIP
647            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFullyRenderedSec );
648
649#else
650            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
651#endif
652            rpcTempCU->initEstData( uiDepth, iQP );
653            if(m_pcEncCfg->getUseCbfFastMode())
654            {
655              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
656            }
657          }
658#if LGE_ILLUCOMP_B0045
659         }
660#endif
661#if H3D_IVRP
662        } // uiResPrdId
663#endif
664      } // != I_SLICE
665
666#if LGE_ILLUCOMP_B0045
667#if SHARP_ILLUCOMP_PARSE_D0060
668    bICEnabled = false;
669#else
670    bICEnabled = rpcBestCU->getICFlag(0);
671#endif
672#endif
673
674#if H3D_QTL
675
676      if(depthMapDetect && !bIntraSliceDetect && !rapPic && sps->getUseQTLPC())
677      {
678        bTrySplitDQP = bTrySplit;
679      }
680      else
681      {
682#endif
683        if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
684        {
685          if(iQP == iBaseQP)
686          {
687            bTrySplitDQP = bTrySplit;
688          }
689        }
690        else
691        {
692          bTrySplitDQP = bTrySplit;
693        }
694#if H3D_QTL
695      }
696#endif
697
698#if LOSSLESS_CODING
699      if (isAddLowestQP && (iQP == lowestQP))
700      {
701        iQP = iMinQP;
702      }
703#endif
704    }  // end for iMinQP to iMaxQP
705
706
707    for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
708    {
709#if LOSSLESS_CODING
710      if (isAddLowestQP && (iQP == iMinQP))
711      {
712        iQP = lowestQP;
713      }
714#endif
715      rpcTempCU->initEstData( uiDepth, iQP );
716
717      // do inter modes, NxN, 2NxN, and Nx2N
718      if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
719      {
720#if H3D_IVRP
721        Bool  bResPredAvailable   = false;
722        UInt uiResPrdId = 0; 
723        {
724          Bool bResPredFlag  = ( uiResPrdId > 0 );
725#if LGE_ILLUCOMP_B0045
726#if SHARP_ILLUCOMP_PARSE_D0060
727          {
728            Bool bICFlag = false;
729            rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
730#else
731          for(UInt uiICId = 0; uiICId < (bICEnabled ? 2 : 1); uiICId++)
732          {
733            Bool bICFlag = (uiICId ? true : false);
734            rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
735#endif
736#endif
737#endif
738          // 2Nx2N, NxN
739          if ( !bEarlySkip )
740          {
741
742            if(!( rpcBestCU->getSlice()->getSPS()->getDisInter4x4()  && (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) ))
743            {
744              if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu)
745              {
746#if H3D_QTL //try InterNxN
747                if(bTrySplit)
748                {
749#endif
750#if H3D_IVRP
751                  rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
752#endif
753#if HHI_INTERVIEW_SKIP
754                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN, bFullyRenderedSec   );
755#else
756                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN   );
757#endif
758                  rpcTempCU->initEstData( uiDepth, iQP );
759#if H3D_QTL
760                }
761#endif
762              } 
763            }
764          }
765
766          { // 2NxN, Nx2N
767#if H3D_QTL //try Nx2N
768            if(bTryNx2N)
769            {
770#endif
771              if(doNotBlockPu)
772              {
773#if H3D_IVRP
774                rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
775#endif
776#if LGE_ILLUCOMP_B0045
777                rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
778#endif
779#if HHI_INTERVIEW_SKIP
780                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N, bFullyRenderedSec   );
781#else
782                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N  );
783#endif
784                rpcTempCU->initEstData( uiDepth, iQP );
785                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
786                {
787                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
788                }
789              }
790#if H3D_QTL
791            }
792#endif
793
794#if H3D_QTL //try 2NxN
795            if(bTry2NxN)
796            {
797#endif
798              if(doNotBlockPu)
799              {
800#if H3D_IVRP
801                rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
802#endif
803#if LGE_ILLUCOMP_B0045
804                rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
805#endif
806#if HHI_INTERVIEW_SKIP
807                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxN, bFullyRenderedSec   );
808#else
809                xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN  );
810#endif
811                rpcTempCU->initEstData( uiDepth, iQP );
812                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN)
813                {
814                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
815                }
816              }
817#if H3D_QTL
818            }
819#endif
820          }
821
822#if 1
823          //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N)
824          if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) )
825          {
826#if AMP_ENC_SPEEDUP       
827            Bool bTestAMP_Hor = false, bTestAMP_Ver = false;
828
829#if AMP_MRG
830            Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false;
831
832            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver);
833#else
834            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver);
835#endif
836
837            //! Do horizontal AMP
838            if ( bTestAMP_Hor )
839            {
840#if H3D_QTL //try 2NxnU & 2NxnD
841              if(bTry2NxN)
842              {
843#endif
844                if(doNotBlockPu)
845                {
846#if H3D_IVRP
847                  rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
848#endif
849#if LGE_ILLUCOMP_B0045
850                  rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
851#endif
852#if HHI_INTERVIEW_SKIP
853                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFullyRenderedSec );
854#else
855                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
856#endif
857                  rpcTempCU->initEstData( uiDepth, iQP );
858                  if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
859                  {
860                    doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
861                  }
862                }
863                if(doNotBlockPu)
864                {
865#if H3D_IVRP
866                  rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
867#endif
868#if LGE_ILLUCOMP_B0045
869                  rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
870#endif
871#if HHI_INTERVIEW_SKIP
872                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFullyRenderedSec );
873#else
874                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
875#endif
876                  rpcTempCU->initEstData( uiDepth, iQP );
877                  if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
878                  {
879                    doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
880                  }
881                }
882#if H3D_QTL
883              }
884#endif
885            }
886#if AMP_MRG
887            else if ( bTestMergeAMP_Hor ) 
888            {
889#if H3D_QTL //try 2NxnU & 2NxnD Merge
890              if(bTry2NxN)
891              {
892#endif
893                if(doNotBlockPu)
894                {
895#if H3D_IVRP
896                  rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
897#endif
898#if LGE_ILLUCOMP_B0045
899                  rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
900#endif
901#if HHI_INTERVIEW_SKIP
902                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFullyRenderedSec, true );
903#else
904                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true );
905#endif
906                  rpcTempCU->initEstData( uiDepth, iQP );
907                  if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
908                  {
909                    doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
910                  }
911                }
912                if(doNotBlockPu)
913                {
914#if H3D_IVRP
915                  rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
916#endif
917#if LGE_ILLUCOMP_B0045
918                  rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
919#endif
920#if HHI_INTERVIEW_SKIP
921                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFullyRenderedSec, true );
922#else
923                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true );
924#endif
925                  rpcTempCU->initEstData( uiDepth, iQP );
926                  if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
927                  {
928                    doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
929                  }
930                }
931#if H3D_QTL
932              }
933#endif
934            }
935#endif
936
937            //! Do horizontal AMP
938            if ( bTestAMP_Ver )
939            {
940#if H3D_QTL //try nLx2N & nRx2N
941              if(bTryNx2N)
942              {
943#endif
944                if(doNotBlockPu)
945                {
946#if H3D_IVRP
947                  rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
948#endif
949#if LGE_ILLUCOMP_B0045
950                  rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
951#endif
952#if HHI_INTERVIEW_SKIP
953                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFullyRenderedSec );
954#else
955                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
956#endif
957                  rpcTempCU->initEstData( uiDepth, iQP );
958                  if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
959                  {
960                    doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
961                  }
962                }
963                if(doNotBlockPu)
964                {
965#if H3D_IVRP
966                  rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
967#endif
968#if LGE_ILLUCOMP_B0045
969                  rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
970#endif
971#if HHI_INTERVIEW_SKIP
972                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFullyRenderedSec );
973#else
974                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
975#endif
976                  rpcTempCU->initEstData( uiDepth, iQP );
977                }
978#if H3D_QTL
979              }
980#endif
981            }
982#if AMP_MRG
983            else if ( bTestMergeAMP_Ver )
984            {
985#if H3D_QTL //try nLx2N & nRx2N (Merge)
986              if(bTryNx2N)
987              {
988#endif
989                if(doNotBlockPu)
990                {
991#if H3D_IVRP
992                  rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
993#endif
994#if LGE_ILLUCOMP_B0045
995                  rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
996#endif
997#if HHI_INTERVIEW_SKIP
998                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFullyRenderedSec, true );
999#else
1000                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true );
1001#endif
1002                  rpcTempCU->initEstData( uiDepth, iQP );
1003                  if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
1004                  {
1005                    doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
1006                  }
1007                }
1008                if(doNotBlockPu)
1009                {
1010#if H3D_IVRP
1011                  rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
1012#endif
1013#if LGE_ILLUCOMP_B0045
1014                  rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
1015#endif
1016#if HHI_INTERVIEW_SKIP
1017                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFullyRenderedSec, true );
1018#else
1019                  xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true );
1020#endif
1021                  rpcTempCU->initEstData( uiDepth, iQP );
1022                }
1023#if H3D_QTL
1024              }
1025#endif
1026            }
1027#endif
1028
1029#else
1030#if H3D_IVRP
1031            rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
1032#endif
1033#if HHI_INTERVIEW_SKIP
1034            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFullyRenderedSec );
1035#else
1036            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
1037#endif
1038            rpcTempCU->initEstData( uiDepth, iQP );
1039#if H3D_IVRP
1040            rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
1041#endif
1042#if HHI_INTERVIEW_SKIP
1043            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFullyRenderedSec );
1044#else
1045            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
1046#endif
1047            rpcTempCU->initEstData( uiDepth, iQP );
1048#if H3D_IVRP
1049            rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
1050#endif
1051#if HHI_INTERVIEW_SKIP
1052            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFullyRenderedSec );
1053#else
1054            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
1055#endif
1056            rpcTempCU->initEstData( uiDepth, iQP );
1057#if H3D_IVRP
1058            rpcTempCU->setResPredIndicator( bResPredAvailable, bResPredFlag );
1059#endif
1060#if HHI_INTERVIEW_SKIP
1061            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFullyRenderedSec );
1062#else
1063            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
1064#endif
1065            rpcTempCU->initEstData( uiDepth, iQP );
1066
1067#endif
1068          } //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N)
1069#endif
1070#if LGE_ILLUCOMP_B0045
1071         }
1072#endif
1073#if H3D_IVRP
1074        } // uiResPrdId
1075#endif
1076      } // != I_SLICE
1077
1078      // initialize PCM flag
1079      rpcTempCU->setIPCMFlag( 0, false);
1080      rpcTempCU->setIPCMFlagSubParts ( false, 0, uiDepth); //SUB_LCU_DQP
1081
1082      // do normal intra modes
1083      if ( !bEarlySkip
1084#if HHI_DEPTH_INTRA_SEARCH_RAU_C0160
1085        || ((rpcBestCU->getSlice()->getIsDepth() == true) && (rpcBestCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR || rpcBestCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA))
1086#endif
1087        )
1088      {
1089        // speedup for inter frames
1090#if HHI_INTERVIEW_SKIP
1091        if( ( rpcBestCU->getSlice()->getSliceType() == I_SLICE ||
1092          rpcBestCU->getCbf( 0, TEXT_LUMA     ) != 0   ||
1093          rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0   ||
1094          rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0 
1095#if HHI_DEPTH_INTRA_SEARCH_RAU_C0160
1096          || ((rpcBestCU->getSlice()->getIsDepth() == true) && (rpcBestCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR || rpcBestCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA))
1097#endif           
1098          ) && !bFullyRenderedSec ) // avoid very complex intra if it is unlikely
1099#else
1100        if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || 
1101          rpcBestCU->getCbf( 0, TEXT_LUMA     ) != 0   ||
1102          rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0   ||
1103          rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0     
1104#if HHI_DEPTH_INTRA_SEARCH_RAU_C0160
1105          || ((rpcBestCU->getSlice()->getIsDepth() == true) && (rpcBestCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR || rpcBestCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA))
1106#endif     
1107          ) // avoid very complex intra if it is unlikely
1108#endif
1109        {
1110#if LGE_ILLUCOMP_B0045
1111          rpcTempCU->setICFlagSubParts(false, 0, 0, uiDepth);
1112#endif
1113          xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
1114          rpcTempCU->initEstData( uiDepth, iQP );
1115          if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
1116          {
1117#if H3D_QTL //Try IntraNxN
1118            if(bTrySplit)
1119            {
1120#endif
1121              if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) )
1122              {
1123#if LGE_ILLUCOMP_B0045
1124                rpcTempCU->setICFlagSubParts(false, 0, 0, uiDepth);
1125#endif
1126                xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN   );
1127                rpcTempCU->initEstData( uiDepth, iQP );
1128              }
1129#if H3D_QTL
1130            }
1131#endif
1132          }
1133        }
1134      }
1135
1136      // test PCM
1137      if(pcPic->getSlice(0)->getSPS()->getUsePCM()
1138        && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize())
1139        && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) )
1140      {
1141        UInt uiRawBits = (g_uiBitDepth * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) * 3 / 2);
1142        UInt uiBestBits = rpcBestCU->getTotalBits();
1143#if HHI_VSO
1144        Double dRDCostTemp = m_pcRdCost->getUseVSO() ? m_pcRdCost->calcRdCostVSO(uiRawBits, 0) : m_pcRdCost->calcRdCost(uiRawBits, 0);
1145        if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > dRDCostTemp ))
1146#else
1147        if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
1148#endif
1149        {
1150#if LGE_ILLUCOMP_B0045
1151          rpcTempCU->setICFlagSubParts(false, 0, 0, uiDepth);
1152#endif
1153          xCheckIntraPCM (rpcBestCU, rpcTempCU);
1154          rpcTempCU->initEstData( uiDepth, iQP );
1155        }
1156      }
1157#if HHI_MPI
1158      if( rpcBestCU->getSlice()->getSPS()->getUseMVI() && rpcBestCU->getSlice()->getSliceType() != I_SLICE )
1159      {
1160#if LGE_ILLUCOMP_DEPTH_C0046
1161        for(UInt uiICId = 0; uiICId < (bICEnabled ? 2 : 1); uiICId++)
1162        {
1163          Bool bICFlag = (uiICId ? true : false);
1164          rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
1165#endif
1166        xCheckRDCostMvInheritance( rpcBestCU, rpcTempCU, uiDepth, false, false );
1167        rpcTempCU->initEstData( uiDepth, iQP );
1168#if FIX_ILLUCOMP_DEPTH
1169        rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
1170#endif
1171        xCheckRDCostMvInheritance( rpcBestCU, rpcTempCU, uiDepth, true, false );
1172        rpcTempCU->initEstData( uiDepth, iQP );
1173#if LGE_ILLUCOMP_DEPTH_C0046
1174        }
1175#endif
1176      }
1177#endif
1178#if LOSSLESS_CODING
1179      if (isAddLowestQP && (iQP == lowestQP))
1180      {
1181        iQP = iMinQP;
1182      }
1183#endif
1184    }
1185
1186    m_pcEntropyCoder->resetBits();
1187    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
1188    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1189    if(m_pcEncCfg->getUseSBACRD())
1190    {
1191      rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1192    }
1193
1194#if HHI_VSO   
1195    if ( m_pcRdCost->getUseVSO() )
1196    {
1197      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
1198    }
1199    else
1200#endif
1201    {
1202      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
1203    }
1204
1205    // accumulate statistics for early skip
1206    if ( m_pcEncCfg->getUseFastEnc() )
1207    {
1208      if ( rpcBestCU->isSkipped(0) )
1209      {
1210        Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
1211        afCost[ iIdx ] += rpcBestCU->getTotalCost();
1212        aiNum [ iIdx ] ++;
1213      }
1214    }
1215
1216    // Early CU determination
1217    if( m_pcEncCfg->getUseEarlyCU() && ((*rpcBestCU->getPredictionMode()) == 0) )
1218    {
1219      bSubBranch = false;
1220    }
1221    else
1222    {
1223      bSubBranch = true;
1224    }
1225#if HHI_INTERVIEW_SKIP
1226    rpcBestCU->setRenderableSubParts(bFullyRenderedSec,0,rpcBestCU->getDepth( 0 )) ;
1227#endif
1228  }
1229  else if(!(bSliceEnd && bInsidePicture))
1230  {
1231    bBoundary = true;
1232  }
1233
1234#if LOSSLESS_CODING
1235  // copy orginal YUV samples to PCM buffer
1236  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
1237  {
1238    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
1239  }
1240#endif
1241  if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1242  {
1243    Int idQP = m_pcEncCfg->getMaxDeltaQP();
1244    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
1245    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
1246#if LOSSLESS_CODING
1247    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
1248    {
1249      isAddLowestQP = true;
1250      iMinQP = iMinQP - 1;     
1251    }
1252#endif
1253  }
1254  else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1255  {
1256    iMinQP = iBaseQP;
1257    iMaxQP = iBaseQP;
1258  }
1259  else
1260  {
1261    Int iStartQP;
1262    if( pcPic->getCU( rpcTempCU->getAddr() )->getEntropySliceStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getEntropySliceCurStartCUAddr())
1263    {
1264      iStartQP = rpcTempCU->getQP(0);
1265    }
1266    else
1267    {
1268      UInt uiCurSliceStartPartIdx = pcSlice->getEntropySliceCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1269      iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx);
1270    }
1271    iMinQP = iStartQP;
1272    iMaxQP = iStartQP;
1273  }
1274
1275  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
1276  {
1277#if LOSSLESS_CODING
1278    if (isAddLowestQP && (iQP == iMinQP))
1279    {
1280      iQP = lowestQP;
1281    }
1282#endif
1283    rpcTempCU->initEstData( uiDepth, iQP );
1284
1285    // further split
1286    if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
1287    {
1288#if HHI_VSO
1289      // reset Model
1290      if( m_pcRdCost->getUseRenModel() )
1291      {
1292        UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth ( );
1293        UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight( );
1294        Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr( 0 );
1295        UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride();
1296        m_pcRdCost->setRenModelData( m_ppcBestCU[uiDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1297      }
1298#endif
1299      UChar       uhNextDepth         = uiDepth+1;
1300      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
1301      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
1302
1303      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
1304      {
1305        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1306        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1307
1308        Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getEntropySliceCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getEntropySliceCurEndCUAddr();
1309        if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1310        {
1311          if( m_bUseSBACRD )
1312          {
1313            if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
1314            {
1315              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1316            }
1317            else
1318            {
1319              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
1320            }
1321          }
1322
1323#if AMP_ENC_SPEEDUP
1324          if ( rpcBestCU->isIntra(0) )
1325          {
1326            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE );
1327          }
1328          else
1329          {
1330            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) );
1331          }
1332#else
1333          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
1334#endif
1335
1336          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
1337          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
1338
1339#if HHI_VSO
1340#endif
1341        }
1342        else if (bInSlice)
1343        {
1344          pcSubBestPartCU->copyToPic( uhNextDepth );
1345          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
1346        }
1347      }
1348
1349      if( !bBoundary )
1350      {
1351        m_pcEntropyCoder->resetBits();
1352        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
1353
1354        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1355        if(m_pcEncCfg->getUseSBACRD())
1356        {
1357          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1358        }
1359      }
1360#if HHI_INTERVIEW_SKIP_LAMBDA_SCALE
1361      if( bFullyRenderedSec )
1362      {
1363        m_pcRdCost->setLambdaScale( m_pcEncCfg->getInterViewSkipLambdaScale() );
1364      }
1365      else
1366      {
1367        m_pcRdCost->setLambdaScale( 1 );
1368      }
1369#endif
1370
1371#if HHI_VSO
1372      if ( m_pcRdCost->getUseVSO() )
1373      {
1374        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1375      }
1376      else
1377#endif
1378      {           
1379        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1380      }
1381
1382      if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
1383      {
1384        Bool bHasRedisual = false;
1385        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
1386        {
1387          if( ( pcPic->getCU( rpcTempCU->getAddr() )->getEntropySliceStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getEntropySliceCurStartCUAddr() ) && 
1388            ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
1389          {
1390            bHasRedisual = true;
1391            break;
1392          }
1393        }
1394
1395        UInt uiTargetPartIdx;
1396        if ( pcPic->getCU( rpcTempCU->getAddr() )->getEntropySliceStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getEntropySliceCurStartCUAddr() )
1397        {
1398          uiTargetPartIdx = pcSlice->getEntropySliceCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1399        }
1400        else
1401        {
1402          uiTargetPartIdx = 0;
1403        }
1404        if ( bHasRedisual )
1405        {
1406#if !RDO_WITHOUT_DQP_BITS
1407          m_pcEntropyCoder->resetBits();
1408          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
1409          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1410          if(m_pcEncCfg->getUseSBACRD())
1411          {
1412            rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1413          }
1414#if HHI_VSO
1415          if ( m_pcRdCost->getUseLambdaScaleVSO())
1416          {
1417            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1418          }
1419          else
1420#endif
1421          {
1422            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1423          }
1424#endif
1425        }
1426        else
1427        {
1428#if LOSSLESS_CODING
1429          if (((rpcTempCU->getQP(uiTargetPartIdx) != rpcTempCU->getRefQP(uiTargetPartIdx)) ) && (rpcTempCU->getSlice()->getSPS()->getUseLossless()))
1430          {
1431            rpcTempCU->getTotalCost() = MAX_DOUBLE;
1432          }
1433#endif
1434          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
1435        }
1436      }
1437
1438      if( m_bUseSBACRD )
1439      {
1440        m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1441      }
1442      Bool bEntropyLimit=false;
1443      Bool bSliceLimit=false;
1444      bSliceLimit=rpcBestCU->getSlice()->getSliceMode()==AD_HOC_SLICES_FIXED_NUMBER_OF_BYTES_IN_SLICE&&(rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
1445      if(rpcBestCU->getSlice()->getEntropySliceMode()==SHARP_MULTIPLE_CONSTRAINT_BASED_ENTROPY_SLICE&&m_pcEncCfg->getUseSBACRD())
1446      {
1447        if(rpcBestCU->getTotalBins()>rpcBestCU->getSlice()->getEntropySliceArgument())
1448        {
1449          bEntropyLimit=true;
1450        }
1451      }
1452      else if(rpcBestCU->getSlice()->getEntropySliceMode()==SHARP_MULTIPLE_CONSTRAINT_BASED_ENTROPY_SLICE)
1453      {
1454        if(rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getEntropySliceArgument())
1455        {
1456          bEntropyLimit=true;
1457        }
1458      }
1459      if(rpcBestCU->getDepth(0)>=rpcBestCU->getSlice()->getPPS()->getSliceGranularity())
1460      {
1461        bSliceLimit=false;
1462        bEntropyLimit=false;
1463      }
1464      if(bSliceLimit||bEntropyLimit)
1465      {
1466        rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
1467      }
1468      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth);                                  // RD compare current larger prediction
1469    }                                                                                  // with sub partitioned prediction.
1470#if LOSSLESS_CODING
1471    if (isAddLowestQP && (iQP == lowestQP))
1472    {
1473      iQP = iMinQP;
1474    }
1475#endif
1476  } // SPLIT- QP Loop
1477
1478#if HHI_VSO
1479  if( m_pcRdCost->getUseRenModel() )
1480  {
1481    UInt  uiWidth     = m_ppcRecoYuvBest[uiDepth]->getWidth   ( );
1482    UInt  uiHeight    = m_ppcRecoYuvBest[uiDepth]->getHeight  ( );
1483    Pel*  piSrc       = m_ppcRecoYuvBest[uiDepth]->getLumaAddr( 0 );
1484    UInt  uiSrcStride = m_ppcRecoYuvBest[uiDepth]->getStride  ( );
1485    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1486  }
1487#endif
1488
1489  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
1490
1491  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY );   // Copy Yuv data to picture Yuv
1492  if( bBoundary ||(bSliceEnd && bInsidePicture))
1493  {
1494    return;
1495  }
1496
1497  // Assert if Best prediction mode is NONE
1498  // Selected mode's RD-cost must be not MAX_DOUBLE.
1499  assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE  );
1500  assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE  );
1501  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE );
1502}
1503
1504/** finish encoding a cu and handle end-of-slice conditions
1505 * \param pcCU
1506 * \param uiAbsPartIdx
1507 * \param uiDepth
1508 * \returns Void
1509 */
1510Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1511{
1512  TComPic* pcPic = pcCU->getPic();
1513  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1514
1515  //Calculate end address
1516  UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx;
1517
1518  UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getEntropySliceCurEndCUAddr()-1) % pcPic->getNumPartInCU();
1519  UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getEntropySliceCurEndCUAddr()-1) / pcPic->getNumPartInCU();
1520  UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1521  UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1522  UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
1523  UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
1524  while(uiPosX>=uiWidth||uiPosY>=uiHeight)
1525  {
1526    uiInternalAddress--;
1527    uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1528    uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1529  }
1530  uiInternalAddress++;
1531  if(uiInternalAddress==pcCU->getPic()->getNumPartInCU())
1532  {
1533    uiInternalAddress = 0;
1534    uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1);
1535  }
1536  UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress);
1537
1538  // Encode slice finish
1539  Bool bTerminateSlice = false;
1540  if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress)
1541  {
1542    bTerminateSlice = true;
1543  }
1544  UInt uiGranularityWidth = g_uiMaxCUWidth>>(pcSlice->getPPS()->getSliceGranularity());
1545  uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1546  uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1547  Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth))
1548    &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight));
1549 
1550  if(granularityBoundary && (!(pcCU->getIPCMFlag(uiAbsPartIdx) && ( pcCU->getNumSucIPCM() > 1 ))))
1551  {
1552    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1553    if (!bTerminateSlice)
1554      m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 );
1555  }
1556 
1557  Int numberOfWrittenBits = 0;
1558  if (m_pcBitCounter)
1559  {
1560    numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits();
1561  }
1562 
1563  // Calculate slice end IF this CU puts us over slice bit size.
1564  unsigned iGranularitySize = pcCU->getPic()->getNumPartInCU()>>(pcSlice->getPPS()->getSliceGranularity()<<1);
1565  int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize;
1566  if(iGranularityEnd<=pcSlice->getEntropySliceCurStartCUAddr()) 
1567  {
1568    iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)));
1569  }
1570  // Set slice end parameter
1571  if(pcSlice->getSliceMode()==AD_HOC_SLICES_FIXED_NUMBER_OF_BYTES_IN_SLICE&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) 
1572  {
1573    pcSlice->setEntropySliceCurEndCUAddr(iGranularityEnd);
1574    pcSlice->setSliceCurEndCUAddr(iGranularityEnd);
1575    return;
1576  }
1577  // Set entropy slice end parameter
1578  if(m_pcEncCfg->getUseSBACRD()) 
1579  {
1580    TEncBinCABAC *pppcRDSbacCoder = (TEncBinCABAC *) m_pppcRDSbacCoder[0][CI_CURR_BEST]->getEncBinIf();
1581    UInt uiBinsCoded = pppcRDSbacCoder->getBinsCoded();
1582    if(pcSlice->getEntropySliceMode()==SHARP_MULTIPLE_CONSTRAINT_BASED_ENTROPY_SLICE&&!pcSlice->getFinalized()&&pcSlice->getEntropySliceCounter()+uiBinsCoded>pcSlice->getEntropySliceArgument())
1583    {
1584      pcSlice->setEntropySliceCurEndCUAddr(iGranularityEnd);
1585      return;
1586    }
1587  }
1588  else
1589  {
1590    if(pcSlice->getEntropySliceMode()==SHARP_MULTIPLE_CONSTRAINT_BASED_ENTROPY_SLICE&&!pcSlice->getFinalized()&&pcSlice->getEntropySliceCounter()+numberOfWrittenBits>pcSlice->getEntropySliceArgument()) 
1591    {
1592      pcSlice->setEntropySliceCurEndCUAddr(iGranularityEnd);
1593      return;
1594    }
1595  }
1596  if(granularityBoundary)
1597  {
1598    pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) );
1599    if(m_pcEncCfg->getUseSBACRD()) 
1600    {
1601      TEncBinCABAC *pppcRDSbacCoder = (TEncBinCABAC *) m_pppcRDSbacCoder[0][CI_CURR_BEST]->getEncBinIf();
1602      pcSlice->setEntropySliceCounter(pcSlice->getEntropySliceCounter()+pppcRDSbacCoder->getBinsCoded());
1603      pppcRDSbacCoder->setBinsCoded( 0 );
1604    }
1605    else 
1606    {
1607      pcSlice->setEntropySliceCounter(pcSlice->getEntropySliceCounter()+numberOfWrittenBits);
1608    }
1609    if (m_pcBitCounter)
1610    {
1611      m_pcEntropyCoder->resetBits();     
1612    }
1613  }
1614}
1615
1616/** Compute QP for each CU
1617 * \param pcCU Target CU
1618 * \param uiDepth CU depth
1619 * \returns quantization parameter
1620 */
1621Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1622{
1623  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1624  Int iQpOffset = 0;
1625  if ( m_pcEncCfg->getUseAdaptiveQP() )
1626  {
1627    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1628    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1629    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1630    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1631    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1632    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1633    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1634
1635    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1636    Double dAvgAct = pcAQLayer->getAvgActivity();
1637    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1638    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1639    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1640    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1641  }
1642  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset );
1643}
1644
1645/** encode a CU block recursively
1646 * \param pcCU
1647 * \param uiAbsPartIdx
1648 * \param uiDepth
1649 * \returns Void
1650 */
1651Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1652{
1653  TComPic* pcPic = pcCU->getPic();
1654 
1655  Bool bBoundary = false;
1656  UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1657  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1658  UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1659  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1660 
1661  if( getCheckBurstIPCMFlag() )
1662  {
1663    pcCU->setLastCUSucIPCMFlag( checkLastCUSucIPCM( pcCU, uiAbsPartIdx ));
1664    pcCU->setNumSucIPCM( countNumSucIPCM ( pcCU, uiAbsPartIdx ) );
1665  }
1666
1667  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1668  // If slice start is within this cu...
1669  Bool bSliceStart = pcSlice->getEntropySliceCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1670    pcSlice->getEntropySliceCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) );
1671  // We need to split, so don't try these modes.
1672  if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1673  {
1674#if HHI_MPI
1675    if( pcCU->getTextureModeDepth( uiAbsPartIdx ) == -1 || uiDepth < pcCU->getTextureModeDepth( uiAbsPartIdx ) )
1676    {
1677#endif
1678
1679    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1680
1681#if HHI_MPI
1682    }
1683#endif
1684  }
1685  else
1686  {
1687    bBoundary = true;
1688  }
1689 
1690#if HHI_MPI
1691  if( uiDepth == pcCU->getTextureModeDepth( uiAbsPartIdx ) )
1692  {
1693    xSaveDepthWidthHeight( pcCU );
1694    pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
1695    pcCU->setDepthSubParts( uiDepth, uiAbsPartIdx );
1696
1697    if( ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1698    {
1699      m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1700    }
1701
1702    if( !pcCU->getSlice()->isIntra() )
1703    {
1704      m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1705    }
1706
1707    if( pcCU->isSkipped( uiAbsPartIdx ) )
1708    {
1709      m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx, 0 );
1710#if LGE_ILLUCOMP_DEPTH_C0046
1711      m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx, false, uiDepth );
1712#endif
1713      finishCU(pcCU,uiAbsPartIdx,uiDepth);
1714      xRestoreDepthWidthHeight( pcCU );
1715      return;
1716    }
1717
1718    m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1719
1720    m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1721
1722    // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1723    m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1724#if LGE_ILLUCOMP_DEPTH_C0046
1725    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx, false, uiDepth );
1726#endif
1727    xRestoreDepthWidthHeight( pcCU );
1728  }
1729#endif
1730
1731  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary )
1732  {
1733    UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2;
1734    if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1735    {
1736      setdQPFlag(true);
1737    }
1738    pcCU->setNumSucIPCM(0);
1739    pcCU->setLastCUSucIPCMFlag(false);
1740    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1741    {
1742      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1743      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1744      Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getEntropySliceCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getEntropySliceCurEndCUAddr();
1745      if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1746      {
1747        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1748      }
1749    }
1750    return;
1751  }
1752 
1753  if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1754  {
1755    setdQPFlag(true);
1756  }
1757#if HHI_MPI
1758  if( pcCU->getTextureModeDepth( uiAbsPartIdx ) == -1 )
1759{
1760#endif
1761  if( !pcCU->getSlice()->isIntra() )
1762  {
1763    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1764  }
1765#if HHI_MPI
1766}
1767#endif
1768 
1769  if( pcCU->isSkipped( uiAbsPartIdx ) )
1770  {
1771    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx, 0 );
1772#if LGE_ILLUCOMP_B0045
1773    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx
1774#if LGE_ILLUCOMP_DEPTH_C0046
1775        , false, uiDepth
1776#endif
1777        );
1778#endif
1779    finishCU(pcCU,uiAbsPartIdx,uiDepth);
1780    return;
1781  }
1782#if HHI_MPI
1783  if( pcCU->getTextureModeDepth( uiAbsPartIdx ) == -1 )
1784  {
1785#endif
1786  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1787 
1788  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1789 
1790  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1791  {
1792    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1793
1794    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1795    {
1796      // Encode slice finish
1797      finishCU(pcCU,uiAbsPartIdx,uiDepth);
1798      return;
1799    }
1800  }
1801
1802  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1803  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1804#if LGE_ILLUCOMP_B0045
1805    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx
1806#if LGE_ILLUCOMP_DEPTH_C0046
1807        ,false, uiDepth
1808#endif
1809        );
1810#endif
1811#if HHI_MPI
1812  }
1813#endif
1814 
1815  // Encode Coefficients
1816  Bool bCodeDQP = getdQPFlag();
1817  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP );
1818  setdQPFlag( bCodeDQP );
1819
1820  // --- write terminating bit ---
1821  finishCU(pcCU,uiAbsPartIdx,uiDepth);
1822}
1823
1824/** check RD costs for a CU block encoded with merge
1825 * \param rpcBestCU
1826 * \param rpcTempCU
1827 * \returns Void
1828 */
1829#if HHI_INTERVIEW_SKIP
1830Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool bSkipRes )
1831#else
1832Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
1833#endif
1834{
1835  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1836#if H3D_IVMP
1837  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS_MEM << 1]; // double length for mv of both lists
1838  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS_MEM];
1839#else
1840  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists
1841  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1842#endif
1843  Int numValidMergeCand = 0;
1844
1845#if LGE_ILLUCOMP_B0045
1846  Bool  bICFlag = rpcTempCU->getICFlag(0);
1847#endif
1848
1849#if H3D_IVMP
1850  for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS_MEM; ++ui )
1851#else
1852  for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS; ++ui )
1853#endif
1854  {
1855    uhInterDirNeighbours[ui] = 0;
1856  }
1857  UChar uhDepth = rpcTempCU->getDepth( 0 );
1858
1859#if HHI_VSO
1860  if( m_pcRdCost->getUseRenModel() )
1861  {
1862    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1863    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1864    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1865    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1866    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1867  }
1868#endif
1869
1870  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1871#if MERL_VSP_C0152
1872  Int iVSPIndexTrue[3] = {-1, -1, -1};
1873  rpcTempCU->getInterMergeCandidates( 0, 0, uhDepth, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand, iVSPIndexTrue );
1874#else
1875  rpcTempCU->getInterMergeCandidates( 0, 0, uhDepth, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1876#endif
1877#if H3D_IVRP
1878  Bool bResPredAvail = rpcTempCU->getResPredAvail(0);
1879#endif
1880
1881  Bool bestIsSkip = false;
1882 
1883  for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1884  {
1885    {
1886      TComYuv* pcPredYuvTemp = NULL;
1887#if SHARP_ILLUCOMP_PARSE_D0060
1888      if (rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag())
1889      {
1890        if (bICFlag && uiMergeCand == 0) 
1891        {
1892          continue;
1893        }
1894      }
1895#endif
1896#if LOSSLESS_CODING
1897      UInt iteration;
1898      if ( rpcTempCU->isLosslessCoded(0))
1899      {
1900        iteration = 1;
1901      }
1902      else 
1903      {
1904        iteration = 2;
1905      }
1906
1907#if HHI_INTERVIEW_SKIP
1908    for( UInt uiNoResidual = (bSkipRes ? 1:0); uiNoResidual < iteration; ++uiNoResidual )
1909#else
1910      for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1911#endif
1912#else
1913#if HHI_INTERVIEW_SKIP
1914    for( UInt uiNoResidual = (bSkipRes ? 1:0); uiNoResidual < 2; ++uiNoResidual )
1915#else
1916      for( UInt uiNoResidual = 0; uiNoResidual < 2; ++uiNoResidual )
1917#endif
1918#endif
1919      {
1920        if( !(bestIsSkip && uiNoResidual == 0) )
1921        {
1922          // set MC parameters
1923          rpcTempCU->setPredModeSubParts( MODE_SKIP, 0, uhDepth ); // interprets depth relative to LCU level
1924          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1925          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1926          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1927#if MERL_VSP_C0152
1928          {
1929            Int iVSPIdx = 0;
1930            Int numVSPIdx;
1931            numVSPIdx = 3;
1932            for (Int i = 0; i < numVSPIdx; i++)
1933            {
1934              if (iVSPIndexTrue[i] == uiMergeCand)
1935                {
1936                  iVSPIdx = i+1;
1937                  break;
1938                }
1939            }
1940            rpcTempCU->setVSPIndexSubParts( iVSPIdx, 0, 0, uhDepth );
1941#if QC_BVSP_CleanUP_D0191
1942           if(iVSPIdx != 0)
1943           {
1944             Int iIVCIdx = rpcTempCU->getSlice()->getRefPic(REF_PIC_LIST_0, 0)->getPOC()==rpcTempCU->getSlice()->getPOC() ? 0: rpcTempCU->getSlice()->getNewRefIdx(REF_PIC_LIST_0);
1945             cMvFieldNeighbours[ 2*uiMergeCand].setRefIdx(iIVCIdx);
1946           }
1947#endif
1948          }
1949#endif
1950          rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1951          rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1952          rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1953
1954#if H3D_IVRP
1955          rpcTempCU->setResPredAvailSubParts(bResPredAvail, 0, 0, uhDepth);
1956#endif
1957#if LGE_ILLUCOMP_B0045
1958          rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uhDepth);
1959#endif
1960
1961          // do MC
1962#if HHI_INTERVIEW_SKIP
1963      if ( (uiNoResidual == 0) || bSkipRes )
1964#else
1965      if ( uiNoResidual == 0 )
1966#endif
1967        {
1968#if MERL_VSP_C0152
1969            m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth],  rpcTempCU->getZorderIdxInCU() );
1970#else
1971            m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1972#endif
1973#if H3D_IVRP
1974            if (uiMergeCand == 0 && rpcTempCU->getResPredAvail(0))
1975            {
1976              m_pcPredSearch->residualPrediction(rpcTempCU, m_ppcPredYuvTemp[uhDepth], m_ppcResPredTmp [uhDepth]);
1977            }
1978#endif
1979            // save pred adress
1980            pcPredYuvTemp = m_ppcPredYuvTemp[uhDepth];
1981
1982          }
1983          else
1984          {
1985            if( bestIsSkip)
1986            {
1987#if MERL_VSP_C0152
1988              m_pcPredSearch->motionCompensation( rpcTempCU, m_ppcPredYuvTemp[uhDepth], rpcTempCU->getZorderIdxInCU() );
1989#else
1990              m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1991#endif
1992#if H3D_IVRP
1993              if (uiMergeCand == 0 && rpcTempCU->getResPredAvail(0))
1994              {
1995                m_pcPredSearch->residualPrediction(rpcTempCU, m_ppcPredYuvTemp[uhDepth], m_ppcResPredTmp [uhDepth]);
1996              }
1997#endif
1998              // save pred adress
1999              pcPredYuvTemp = m_ppcPredYuvTemp[uhDepth];
2000            }
2001            else
2002            {
2003              if ( pcPredYuvTemp != m_ppcPredYuvTemp[uhDepth])
2004              {
2005                //adress changes take best (old temp)
2006                pcPredYuvTemp = m_ppcPredYuvBest[uhDepth];
2007              }
2008            }
2009          }
2010#if HHI_VSO
2011          if( m_pcRdCost->getUseRenModel() )
2012          { //Reset
2013            UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
2014            UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
2015            Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
2016            UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
2017            m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2018          }
2019#endif
2020          // estimate residual and encode everything
2021          m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
2022            m_ppcOrigYuv    [uhDepth],
2023            pcPredYuvTemp,
2024            m_ppcResiYuvTemp[uhDepth],
2025            m_ppcResiYuvBest[uhDepth],
2026            m_ppcRecoYuvTemp[uhDepth],
2027#if H3D_IVRP
2028                                                     m_ppcResPredTmp [uhDepth],
2029#endif
2030            (uiNoResidual? true:false) );     
2031          Bool bQtRootCbf = rpcTempCU->getQtRootCbf(0) == 1;
2032
2033          Int orgQP = rpcTempCU->getQP( 0 );
2034          xCheckDQP( rpcTempCU );
2035          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2036          rpcTempCU->initEstData( uhDepth, orgQP );
2037
2038          if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
2039          {
2040            bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
2041          }
2042
2043          if (!bQtRootCbf)
2044            break;
2045        }
2046      }
2047    }
2048  }
2049}
2050
2051#if AMP_MRG
2052#if HHI_INTERVIEW_SKIP
2053Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bSkipRes, Bool bUseMRG)
2054#else
2055Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
2056#endif
2057#else
2058#if HHI_INTERVIEW_SKIP
2059Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bSkipRes)
2060#else
2061Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
2062#endif
2063#endif
2064{
2065  UChar uhDepth = rpcTempCU->getDepth( 0 );
2066 
2067#if HHI_VSO
2068  if( m_pcRdCost->getUseRenModel() )
2069  {
2070    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2071    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2072    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2073    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2074    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2075  }
2076#endif 
2077
2078  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2079 
2080#if H3D_IVRP
2081  Bool  bResPrdAvail  = rpcTempCU->getResPredAvail( 0 );
2082  Bool  bResPrdFlag   = rpcTempCU->getResPredFlag ( 0 );
2083#endif
2084 
2085  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
2086
2087#if H3D_IVRP
2088  rpcTempCU->setResPredAvailSubParts( bResPrdAvail, 0, 0, uhDepth );
2089  rpcTempCU->setResPredFlagSubParts ( bResPrdFlag,  0, 0, uhDepth );
2090#endif
2091  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2092
2093#if AMP_MRG
2094  rpcTempCU->setMergeAMP (true);
2095  #if HHI_INTERVIEW_SKIP
2096  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bSkipRes, bUseMRG  );
2097#else
2098  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
2099#endif
2100#else
2101  #if HHI_INTERVIEW_SKIP
2102  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bSkipRes );
2103#else 
2104  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
2105#endif
2106#endif
2107
2108#if AMP_MRG
2109  if ( !rpcTempCU->getMergeAMP() )
2110  {
2111    return;
2112  }
2113#endif
2114
2115#if HHI_INTERVIEW_SKIP
2116  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
2117                                             m_ppcOrigYuv[uhDepth],
2118                                             m_ppcPredYuvTemp[uhDepth],
2119                                             m_ppcResiYuvTemp[uhDepth],
2120                                             m_ppcResiYuvBest[uhDepth],
2121                                             m_ppcRecoYuvTemp[uhDepth],
2122#if H3D_IVRP
2123                                             m_ppcResPredTmp [uhDepth],
2124#endif
2125                                             bSkipRes );
2126#else
2127  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
2128                                             m_ppcOrigYuv[uhDepth],
2129                                             m_ppcPredYuvTemp[uhDepth],
2130                                             m_ppcResiYuvTemp[uhDepth],
2131                                             m_ppcResiYuvBest[uhDepth],
2132                                             m_ppcRecoYuvTemp[uhDepth],
2133#if H3D_IVRP
2134                                             m_ppcResPredTmp [uhDepth],
2135#endif
2136                                             false );
2137#endif
2138#if HHI_VSO
2139  if( m_pcRdCost->getUseLambdaScaleVSO() )
2140  {
2141    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2142  }
2143  else
2144#endif
2145  {
2146  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2147  }
2148
2149  xCheckDQP( rpcTempCU );
2150  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2151}
2152
2153Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2154{
2155  UInt uiDepth = rpcTempCU->getDepth( 0 );
2156 
2157#if HHI_VSO
2158  if( m_pcRdCost->getUseRenModel() )
2159  {
2160    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
2161    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
2162    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
2163    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
2164    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2165  }
2166#endif
2167
2168  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
2169  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2170 
2171  Bool bSeparateLumaChroma = true; // choose estimation mode
2172  Dist uiPreCalcDistC      = 0;
2173  if( !bSeparateLumaChroma )
2174  {
2175    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
2176  }
2177  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
2178
2179  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
2180 
2181#if RWTH_SDC_DLT_B0036
2182  if( !rpcTempCU->getSDCFlag( 0 ) )
2183#endif
2184  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
2185 
2186  m_pcEntropyCoder->resetBits();
2187  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2188  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
2189  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
2190  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
2191  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
2192
2193  // Encode Coefficients
2194  Bool bCodeDQP = getdQPFlag();
2195  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
2196  setdQPFlag( bCodeDQP );
2197 
2198  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2199 
2200  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2201  if(m_pcEncCfg->getUseSBACRD())
2202  {
2203    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2204  }
2205#if HHI_VSO
2206  if( m_pcRdCost->getUseLambdaScaleVSO())
2207  {
2208    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2209  }
2210  else
2211#endif
2212  {
2213  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2214  }
2215 
2216  xCheckDQP( rpcTempCU );
2217  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2218}
2219
2220/** Check R-D costs for a CU with PCM mode.
2221 * \param rpcBestCU pointer to best mode CU data structure
2222 * \param rpcTempCU pointer to testing mode CU data structure
2223 * \returns Void
2224 *
2225 * \note Current PCM implementation encodes sample values in a lossless way. The distortion of PCM mode CUs are zero. PCM mode is selected if the best mode yields bits greater than that of PCM mode.
2226 */
2227Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
2228{
2229  UInt uiDepth = rpcTempCU->getDepth( 0 );
2230
2231  rpcTempCU->setIPCMFlag(0, true);
2232  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
2233  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2234  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2235
2236  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
2237
2238  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
2239
2240  m_pcEntropyCoder->resetBits();
2241  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2242  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
2243  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
2244  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
2245
2246  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2247
2248  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2249  if(m_pcEncCfg->getUseSBACRD())
2250  {
2251    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2252  }
2253#if HHI_VSO
2254  if ( m_pcRdCost->getUseVSO() )
2255  {
2256    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2257  }
2258  else
2259#endif
2260  { 
2261  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2262  }
2263
2264  xCheckDQP( rpcTempCU );
2265  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
2266}
2267
2268// check whether current try is the best
2269Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
2270{
2271  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
2272  {
2273    TComYuv* pcYuv;
2274    UChar uhDepth = rpcBestCU->getDepth(0);
2275
2276    // Change Information data
2277    TComDataCU* pcCU = rpcBestCU;
2278    rpcBestCU = rpcTempCU;
2279    rpcTempCU = pcCU;
2280   
2281    // Change Prediction data
2282    pcYuv = m_ppcPredYuvBest[uhDepth];
2283    m_ppcPredYuvBest[uhDepth] = m_ppcPredYuvTemp[uhDepth];
2284    m_ppcPredYuvTemp[uhDepth] = pcYuv;
2285   
2286    // Change Reconstruction data
2287    pcYuv = m_ppcRecoYuvBest[uhDepth];
2288    m_ppcRecoYuvBest[uhDepth] = m_ppcRecoYuvTemp[uhDepth];
2289    m_ppcRecoYuvTemp[uhDepth] = pcYuv;
2290   
2291    pcYuv = NULL;
2292    pcCU  = NULL;
2293   
2294    if( m_bUseSBACRD )  // store temp best CI for next CU coding
2295      m_pppcRDSbacCoder[uhDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uhDepth][CI_NEXT_BEST]);
2296  }
2297}
2298
2299/** check whether current try is the best with identifying the depth of current try
2300 * \param rpcBestCU
2301 * \param rpcTempCU
2302 * \returns Void
2303 */
2304Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
2305{
2306  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
2307  {
2308    TComYuv* pcYuv;
2309    // Change Information data
2310    TComDataCU* pcCU = rpcBestCU;
2311    rpcBestCU = rpcTempCU;
2312    rpcTempCU = pcCU;
2313
2314    // Change Prediction data
2315    pcYuv = m_ppcPredYuvBest[uiDepth];
2316    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
2317    m_ppcPredYuvTemp[uiDepth] = pcYuv;
2318
2319    // Change Reconstruction data
2320    pcYuv = m_ppcRecoYuvBest[uiDepth];
2321    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
2322    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
2323
2324    pcYuv = NULL;
2325    pcCU  = NULL;
2326
2327    if( m_bUseSBACRD )  // store temp best CI for next CU coding
2328      m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
2329  }
2330}
2331
2332Void TEncCu::xCheckDQP( TComDataCU* pcCU )
2333{
2334  UInt uiDepth = pcCU->getDepth( 0 );
2335
2336  if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() )
2337  {
2338    if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) )
2339    {
2340#if !RDO_WITHOUT_DQP_BITS
2341      m_pcEntropyCoder->resetBits();
2342      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
2343      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
2344      if(m_pcEncCfg->getUseSBACRD())
2345      {
2346        pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2347      }
2348
2349      // GT: Change here??
2350#if HHI_VSO
2351      if ( m_pcRdCost->getUseVSO() )
2352      {
2353        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
2354      }
2355      else
2356#endif
2357      {
2358      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
2359      }   
2360#endif
2361    }
2362    else
2363    {
2364#if LOSSLESS_CODING
2365      if ((  ( pcCU->getRefQP( 0 ) != pcCU->getQP( 0 )) ) && (pcCU->getSlice()->getSPS()->getUseLossless()))
2366      {
2367        pcCU->getTotalCost() = MAX_DOUBLE;
2368      }
2369#endif
2370      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
2371    }
2372  }
2373}
2374
2375/** Check whether the last CU shares the same root as the current CU and is IPCM or not. 
2376 * \param pcCU
2377 * \param uiCurAbsPartIdx
2378 * \returns Bool
2379 */
2380Bool TEncCu::checkLastCUSucIPCM( TComDataCU* pcCU, UInt uiCurAbsPartIdx )
2381{
2382  Bool lastCUSucIPCMFlag = false;
2383
2384  UInt curDepth = pcCU->getDepth(uiCurAbsPartIdx);
2385  UInt shift = ((pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx())->getSPS()->getMaxCUDepth() - curDepth)<<1);
2386  UInt startPartUnitIdx = ((uiCurAbsPartIdx&(0x03<<shift))>>shift);
2387
2388  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
2389  if( pcSlice->getEntropySliceCurStartCUAddr() == ( pcCU->getSCUAddr() + uiCurAbsPartIdx ) )
2390  {
2391    return false;
2392  }
2393
2394  if(curDepth > 0 && startPartUnitIdx > 0)
2395  {
2396    Int lastValidPartIdx = pcCU->getLastValidPartIdx((Int) uiCurAbsPartIdx );
2397
2398    if( lastValidPartIdx >= 0 )
2399    {
2400      if(( pcCU->getSliceStartCU( uiCurAbsPartIdx ) == pcCU->getSliceStartCU( (UInt) lastValidPartIdx ))
2401        && 
2402        ( pcCU->getDepth( uiCurAbsPartIdx ) == pcCU->getDepth( (UInt) lastValidPartIdx )) 
2403        && 
2404        pcCU->getIPCMFlag( (UInt) lastValidPartIdx ) )
2405      {
2406        lastCUSucIPCMFlag = true;
2407      }
2408    }
2409  }
2410
2411  return  lastCUSucIPCMFlag;
2412}
2413
2414/** Count the number of successive IPCM CUs sharing the same root.
2415 * \param pcCU
2416 * \param uiCurAbsPartIdx
2417 * \returns Int
2418 */
2419Int TEncCu::countNumSucIPCM ( TComDataCU* pcCU, UInt uiCurAbsPartIdx )
2420{
2421  Int numSucIPCM = 0;
2422  UInt CurDepth = pcCU->getDepth(uiCurAbsPartIdx);
2423
2424  if( pcCU->getIPCMFlag(uiCurAbsPartIdx) )
2425  {
2426    if(CurDepth == 0)
2427    {
2428       numSucIPCM = 1;
2429    }
2430    else 
2431    {
2432      TComPic* pcPic = pcCU->getPic();
2433      TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
2434      UInt qNumParts = ( pcPic->getNumPartInCU() >> ((CurDepth-1)<<1) )>>2;
2435
2436      Bool continueFlag = true;
2437      UInt absPartIdx = uiCurAbsPartIdx;
2438      UInt shift = ((pcSlice->getSPS()->getMaxCUDepth() - CurDepth)<<1);
2439      UInt startPartUnitIdx = ((uiCurAbsPartIdx&(0x03<<shift))>>shift);
2440
2441      for ( UInt partUnitIdx = startPartUnitIdx; partUnitIdx < 4 && continueFlag; partUnitIdx++, absPartIdx+=qNumParts )
2442      {
2443        UInt lPelX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[absPartIdx] ];
2444        UInt tPelY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[absPartIdx] ];
2445        Bool inSliceFlag = ( pcCU->getSCUAddr()+absPartIdx+qNumParts>pcSlice->getEntropySliceCurStartCUAddr() ) && ( pcCU->getSCUAddr()+absPartIdx < pcSlice->getEntropySliceCurEndCUAddr());
2446
2447        if( inSliceFlag && ( lPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( tPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2448        {
2449          UInt uiDepth = pcCU->getDepth(absPartIdx);
2450
2451          if( ( CurDepth == uiDepth) && pcCU->getIPCMFlag( absPartIdx ) )
2452          {
2453            numSucIPCM++;
2454          }
2455          else
2456          {
2457            continueFlag = false;
2458          }
2459        }
2460      }
2461    }
2462  }
2463
2464  return numSucIPCM;
2465}
2466
2467Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
2468{
2469  pDst->iN = pSrc->iN;
2470  for (Int i = 0; i < pSrc->iN; i++)
2471  {
2472    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
2473  }
2474}
2475Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY )
2476{
2477  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
2478  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
2479  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
2480  Bool bSliceStart = pcSlice->getEntropySliceCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2481    pcSlice->getEntropySliceCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2482  Bool bSliceEnd   = pcSlice->getEntropySliceCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2483    pcSlice->getEntropySliceCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2484  if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2485  {
2486    UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
2487    UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth);
2488    UInt uiBlkWidth    = rpcPic->getNumPartInWidth() >> (uiDepth);
2489    UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2490    UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2491    UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
2492    m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
2493  }
2494  else
2495  {
2496    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2;
2497
2498    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
2499    {
2500      UInt uiSubCULPelX   = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx &  1 );
2501      UInt uiSubCUTPelY   = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 );
2502
2503      Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getEntropySliceCurStartCUAddr() && 
2504        rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getEntropySliceCurEndCUAddr();
2505      if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2506      {
2507        xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY );   // Copy Yuv data to picture Yuv
2508      }
2509    }
2510  }
2511}
2512
2513Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
2514{
2515  UInt uiCurrDepth = uiNextDepth - 1;
2516  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
2517}
2518
2519#if LOSSLESS_CODING
2520/** Function for filling the PCM buffer of a CU using its original sample array
2521 * \param pcCU pointer to current CU
2522 * \param pcOrgYuv pointer to original sample array
2523 * \returns Void
2524 */
2525Void TEncCu::xFillPCMBuffer     ( TComDataCU*& pCU, TComYuv* pOrgYuv )
2526{
2527
2528  UInt   width        = pCU->getWidth(0);
2529  UInt   height       = pCU->getHeight(0);
2530
2531  Pel*   pSrcY = pOrgYuv->getLumaAddr(0, width); 
2532  Pel*   pDstY = pCU->getPCMSampleY();
2533  UInt   srcStride = pOrgYuv->getStride();
2534
2535  for(Int y = 0; y < height; y++ )
2536  {
2537    for(Int x = 0; x < width; x++ )
2538    {
2539      pDstY[x] = pSrcY[x];
2540    }
2541    pDstY += width;
2542    pSrcY += srcStride;
2543  }
2544
2545  Pel* pSrcCb       = pOrgYuv->getCbAddr();
2546  Pel* pSrcCr       = pOrgYuv->getCrAddr();;
2547
2548  Pel* pDstCb       = pCU->getPCMSampleCb();
2549  Pel* pDstCr       = pCU->getPCMSampleCr();;
2550
2551  UInt srcStrideC = pOrgYuv->getCStride();
2552  UInt heightC   = height >> 1;
2553  UInt widthC    = width  >> 1;
2554
2555  for(Int y = 0; y < heightC; y++ )
2556  {
2557    for(Int x = 0; x < widthC; x++ )
2558    {
2559      pDstCb[x] = pSrcCb[x];
2560      pDstCr[x] = pSrcCr[x];
2561    }
2562    pDstCb += widthC;
2563    pDstCr += widthC;
2564    pSrcCb += srcStrideC;
2565    pSrcCr += srcStrideC;
2566  }
2567}
2568#endif
2569
2570#if ADAPTIVE_QP_SELECTION
2571/** Collect ARL statistics from one block
2572  */
2573Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
2574{
2575  for( Int n = 0; n < NumCoeffInCU; n++ )
2576  {
2577    Int u = abs( rpcCoeff[ n ] );
2578    Int absc = rpcArlCoeff[ n ];
2579
2580    if( u != 0 )
2581    {
2582      if( u < LEVEL_RANGE )
2583      {
2584        cSum[ u ] += ( Double )absc;
2585        numSamples[ u ]++;
2586      }
2587      else 
2588      {
2589        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
2590        numSamples[ LEVEL_RANGE ]++;
2591      }
2592    }
2593  }
2594
2595  return 0;
2596}
2597
2598/** Collect ARL statistics from one LCU
2599 * \param pcCU
2600 */
2601Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU )
2602{
2603  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to datatype and quantization output
2604  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output
2605
2606  TCoeff* pCoeffY = rpcCU->getCoeffY();
2607  Int* pArlCoeffY = rpcCU->getArlCoeffY();
2608
2609  UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth;
2610  UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;
2611
2612  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
2613  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
2614
2615  // Collect stats to cSum[][] and numSamples[][]
2616  for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ )
2617  {
2618    UInt uiTrIdx = rpcCU->getTransformIdx(i);
2619
2620    if(rpcCU->getPredictionMode(i) == MODE_INTER)
2621    if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) )
2622    {
2623      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
2624    }//Note that only InterY is processed. QP rounding is based on InterY data only.
2625   
2626    pCoeffY  += uiMinNumCoeffInCU;
2627    pArlCoeffY  += uiMinNumCoeffInCU;
2628  }
2629
2630  for(Int u=1; u<LEVEL_RANGE;u++)
2631  {
2632    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
2633    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
2634  }
2635  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
2636  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
2637}
2638#endif
2639
2640#if HHI_MPI
2641Void TEncCu::xCheckRDCostMvInheritance( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UChar uhTextureModeDepth, Bool bSkipResidual, Bool bRecursiveCall )
2642{
2643  assert( rpcTempCU->getSlice()->getIsDepth() );
2644  TComDataCU *pcTextureCU = rpcTempCU->getSlice()->getTexturePic()->getCU( rpcTempCU->getAddr() );
2645
2646  const UChar uhDepth  = rpcTempCU->getDepth( 0 );
2647  const Int   iQP      = rpcTempCU->getQP( 0 );
2648  assert( bRecursiveCall == ( uhDepth != uhTextureModeDepth ) );
2649
2650#if !MTK_UNCONSTRAINED_MVI_B0083
2651  if( uhDepth == uhTextureModeDepth )
2652  {
2653    for( UInt ui = 0; ui < rpcTempCU->getTotalNumPart(); ui++ )
2654    {
2655      if( pcTextureCU->isIntra( rpcTempCU->getZorderIdxInCU() + ui ) )
2656      {
2657        return;
2658      }
2659    }
2660  }
2661#endif
2662
2663#if HHI_VSO
2664  if( m_pcRdCost->getUseRenModel() && !bRecursiveCall)
2665  {
2666    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth   ();
2667    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight  ();
2668    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr();
2669    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride  ();
2670    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2671  }
2672#endif
2673
2674  Bool bSplit = uhDepth < pcTextureCU->getDepth( rpcTempCU->getZorderIdxInCU() );
2675  if( bSplit )
2676  {
2677    const UChar       uhNextDepth   = uhDepth+1;
2678    TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
2679    TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
2680
2681    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
2682    {
2683      pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
2684      pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
2685
2686      TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx());
2687      Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getEntropySliceCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getEntropySliceCurEndCUAddr();
2688      if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2689      {
2690        if( m_bUseSBACRD )
2691        {
2692          if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
2693          {
2694            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhDepth][CI_CURR_BEST]);
2695          }
2696          else
2697          {
2698            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
2699          }
2700        }
2701
2702        xCheckRDCostMvInheritance( pcSubBestPartCU, pcSubTempPartCU, uhTextureModeDepth, bSkipResidual, true );
2703
2704        rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
2705        xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
2706      }
2707      else if (bInSlice)
2708      {
2709        pcSubBestPartCU->copyToPic( uhNextDepth );
2710        rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
2711      }
2712    }
2713
2714    if( uhDepth == uhTextureModeDepth )
2715    {
2716      xAddMVISignallingBits( rpcTempCU );
2717    }
2718
2719    // DQP stuff
2720    {
2721      if( (g_uiMaxCUWidth>>uhDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
2722      {
2723        TComPic *pcPic = rpcTempCU->getPic();
2724        TComSlice *pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx());
2725        Bool bHasRedisual = false;
2726        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
2727        {
2728          if( ( pcPic->getCU( rpcTempCU->getAddr() )->getEntropySliceStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getEntropySliceCurStartCUAddr() ) &&
2729              ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
2730          {
2731            bHasRedisual = true;
2732            break;
2733          }
2734        }
2735
2736        UInt uiTargetPartIdx;
2737        if ( pcPic->getCU( rpcTempCU->getAddr() )->getEntropySliceStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getEntropySliceCurStartCUAddr() )
2738        {
2739          uiTargetPartIdx = pcSlice->getEntropySliceCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
2740        }
2741        else
2742        {
2743          uiTargetPartIdx = 0;
2744        }
2745        if ( ! bHasRedisual )
2746        {
2747  #if LOSSLESS_CODING
2748          if (((rpcTempCU->getQP(uiTargetPartIdx) != rpcTempCU->getRefQP(uiTargetPartIdx)) ) && (rpcTempCU->getSlice()->getSPS()->getUseLossless()))
2749          {
2750            rpcTempCU->getTotalCost() = MAX_DOUBLE;
2751          }
2752  #endif
2753          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uhDepth ); // set QP to default QP
2754        }
2755      }
2756    }
2757
2758    if( m_bUseSBACRD )
2759    {
2760      m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uhDepth][CI_TEMP_BEST]);
2761    }
2762  }
2763  else
2764  {
2765    rpcTempCU->setTextureModeDepthSubParts( uhTextureModeDepth, 0, uhDepth );
2766    rpcTempCU->copyTextureMotionDataFrom( pcTextureCU, uhDepth, rpcTempCU->getZorderIdxInCU() );
2767#if FIX_MPI_B0065
2768    UInt uiAbsPartIdx = rpcTempCU->getZorderIdxInCU();
2769    if( rpcTempCU->getDepth(0) > pcTextureCU->getDepth(uiAbsPartIdx))
2770    {
2771      rpcTempCU->setPartSizeSubParts( SIZE_NxN, 0, uhDepth );
2772    }
2773    else
2774    {
2775      PartSize partSize = pcTextureCU->getPartitionSize(uiAbsPartIdx);
2776      rpcTempCU->setPartSizeSubParts( partSize, 0, uhDepth );
2777    }
2778#else
2779    rpcTempCU->setPartSizeSubParts( SIZE_NxN, 0, uhDepth );
2780#endif
2781    for( UInt ui = 0; ui < rpcTempCU->getTotalNumPart(); ui++ )
2782    {
2783      assert( rpcTempCU->getInterDir( ui ) != 0 );
2784      assert( rpcTempCU->getPredictionMode( ui ) != MODE_NONE );
2785#if MERL_VSP_C0152
2786      Int vspIdx = pcTextureCU->getVSPIndex( rpcTempCU->getZorderIdxInCU() + ui);
2787      rpcTempCU->setVSPIndex( ui , vspIdx);
2788#endif
2789    }
2790
2791    rpcTempCU->setPredModeSubParts( bSkipResidual ? MODE_SKIP : MODE_INTER, 0, uhDepth );
2792#if MERL_VSP_C0152
2793    m_pcPredSearch->motionCompensation( rpcTempCU, m_ppcPredYuvTemp[uhDepth], rpcTempCU->getZorderIdxInCU()  );
2794#else
2795    m_pcPredSearch->motionCompensation( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
2796#endif
2797    // get Original YUV data from picture
2798    m_ppcOrigYuv[uhDepth]->copyFromPicYuv( rpcBestCU->getPic()->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
2799    m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
2800                                               m_ppcOrigYuv[uhDepth],
2801                                               m_ppcPredYuvTemp[uhDepth],
2802                                               m_ppcResiYuvTemp[uhDepth],
2803                                               m_ppcResiYuvBest[uhDepth],
2804                                               m_ppcRecoYuvTemp[uhDepth],
2805#if H3D_IVRP
2806                                               m_ppcResPredTmp [uhDepth],
2807#endif
2808                                               bSkipResidual );
2809
2810    if( uhDepth == uhTextureModeDepth )
2811    {
2812      xAddMVISignallingBits( rpcTempCU );
2813    }
2814    xCheckDQP( rpcTempCU );
2815  }
2816
2817#if HHI_VSO
2818  if( m_pcRdCost->getUseLambdaScaleVSO() )
2819  {
2820    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2821  }
2822  else
2823#endif
2824  {
2825    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2826  }
2827
2828  if( rpcTempCU->getPredictionMode( 0 ) == MODE_SKIP && uhDepth == uhTextureModeDepth )
2829  {
2830    if( rpcTempCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uhDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
2831      rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( 0 ), 0, uhDepth ); // set QP to default QP
2832  }
2833  xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
2834  rpcBestCU->copyToPic(uhDepth);                                                     // Copy Best data to Picture for next partition prediction.
2835
2836#if HHI_VSO
2837  if( !bSplit && bRecursiveCall && m_pcRdCost->getUseRenModel() )
2838  {
2839    UInt  uiWidth     = m_ppcRecoYuvBest[uhDepth]->getWidth   (   );
2840    UInt  uiHeight    = m_ppcRecoYuvBest[uhDepth]->getHeight  (   );
2841    UInt  uiSrcStride = m_ppcRecoYuvBest[uhDepth]->getStride  (   );
2842    Pel*  piSrc       = m_ppcRecoYuvBest[uhDepth]->getLumaAddr( 0 );
2843    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2844  }
2845#endif
2846}
2847
2848Void TEncCu::xAddMVISignallingBits( TComDataCU* pcCU )
2849{
2850  const UChar uhDepth = pcCU->getTextureModeDepth( 0 );
2851  m_pcEntropyCoder->resetBits();
2852  xSaveDepthWidthHeight( pcCU );
2853  pcCU->setSizeSubParts( g_uiMaxCUWidth>>uhDepth, g_uiMaxCUHeight>>uhDepth, 0, uhDepth );
2854  pcCU->setDepthSubParts( uhDepth, 0 );
2855  pcCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth );
2856  pcCU->setMergeFlagSubParts( true, 0, 0, uhDepth );
2857  pcCU->setMergeIndexSubParts( HHI_MPI_MERGE_POS, 0, 0, uhDepth );
2858
2859  // check for skip mode
2860  {
2861    Bool bAllZero = true;
2862    for( UInt ui = 0; ui < pcCU->getTotalNumPart(); ui++ )
2863    {
2864      if( pcCU->getCbf( ui, TEXT_LUMA ) || pcCU->getCbf( ui, TEXT_CHROMA_U ) || pcCU->getCbf( ui, TEXT_CHROMA_V ) )
2865      {
2866        bAllZero = false;
2867        break;
2868      }
2869    }
2870    if( bAllZero )
2871      pcCU->setPredModeSubParts( MODE_SKIP, 0, uhDepth );
2872  }
2873
2874
2875  m_pcEntropyCoder->encodeSplitFlag( pcCU, 0, uhDepth, true );
2876  m_pcEntropyCoder->encodeSkipFlag( pcCU, 0, true );
2877
2878  if( pcCU->isSkipped( 0 ) )
2879  {
2880    m_pcEntropyCoder->encodeMergeIndex( pcCU, 0, 0, true );
2881#if LGE_ILLUCOMP_B0045
2882    m_pcEntropyCoder->encodeICFlag( pcCU, 0, true
2883#if LGE_ILLUCOMP_DEPTH_C0046
2884        , uhDepth
2885#endif
2886        );
2887#endif
2888  }
2889  else
2890  {
2891    m_pcEntropyCoder->encodePredMode( pcCU, 0, true );
2892    m_pcEntropyCoder->encodePartSize( pcCU, 0, uhDepth, true );
2893    // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
2894    m_pcEntropyCoder->encodePredInfo( pcCU, 0, true );
2895#if LGE_ILLUCOMP_B0045
2896    m_pcEntropyCoder->encodeICFlag( pcCU, 0,          true 
2897#if LGE_ILLUCOMP_DEPTH_C0046
2898        , uhDepth
2899#endif
2900        );
2901#endif
2902  }
2903  xRestoreDepthWidthHeight( pcCU );
2904
2905  pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits();
2906}
2907
2908Void TEncCu::xSaveDepthWidthHeight( TComDataCU* pcCU )
2909{
2910  const Int iSizeInUchar  = sizeof( UChar ) * pcCU->getTotalNumPart();
2911  memcpy( m_puhDepthSaved, pcCU->getDepth(), iSizeInUchar );
2912  memcpy( m_puhWidthSaved, pcCU->getWidth(), iSizeInUchar );
2913  memcpy( m_puhHeightSaved, pcCU->getHeight(), iSizeInUchar );
2914}
2915
2916Void TEncCu::xRestoreDepthWidthHeight( TComDataCU* pcCU )
2917{
2918  const Int iSizeInUchar  = sizeof( UChar ) * pcCU->getTotalNumPart();
2919  memcpy( pcCU->getDepth(), m_puhDepthSaved, iSizeInUchar );
2920  memcpy( pcCU->getWidth(), m_puhWidthSaved, iSizeInUchar );
2921  memcpy( pcCU->getHeight(), m_puhHeightSaved, iSizeInUchar );
2922}
2923#endif
2924
2925//! \}
Note: See TracBrowser for help on using the repository browser.