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

Last change on this file since 253 was 253, checked in by lg, 12 years ago

Integration of JCT3V-C0046

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