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

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