source: 3DVCSoftware/branches/HTM-5.1-dev1-Hisilicon/source/Lib/TLibEncoder/TEncCu.cpp @ 1352

Last change on this file since 1352 was 229, checked in by hhi-htm, 12 years ago

bug fix

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