source: 3DVCSoftware/branches/HTM-5.1-dev0/source/Lib/TLibEncoder/TEncCu.cpp @ 290

Last change on this file since 290 was 290, checked in by tech, 13 years ago

Further macro clean ups.

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