source: 3DVCSoftware/branches/HTM-8.2-dev0-KWU/source/Lib/TLibEncoder/TEncCu.cpp @ 639

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

Macro cleaned up.

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