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

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

Macro-related fixed.

  • 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#endif
1071#if RATE_CONTROL_LAMBDA_DOMAIN && M0036_RC_IMPROVEMENT && KWU_RC_MADPRED_E0227
1072            if ( uiDepth <= m_addSADDepth )
1073            {
1074              m_LCUPredictionSAD += m_spatialSAD;
1075              m_addSADDepth = uiDepth;
1076            }
1077#endif
1078
1079#if !RATE_CONTROL_LAMBDA_DOMAIN && KWU_RC_MADPRED_E0227
1080            if ( uiDepth <= m_addSADDepth )
1081            {
1082              m_LCUPredictionSAD += m_spatialSAD;
1083              m_addSADDepth = uiDepth;
1084            }
1085#endif
1086            rpcTempCU->initEstData( uiDepth, iQP );
1087            if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
1088            {
1089#if H_3D_QTLPC //Try IntraNxN
1090              if(bTrySplit)
1091              {
1092#endif
1093                if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) )
1094                {
1095                  xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN   );
1096                  rpcTempCU->initEstData( uiDepth, iQP );
1097                }
1098#if H_3D_QTLPC
1099              }
1100#endif
1101            }
1102          }
1103        }
1104
1105        // test PCM
1106        if(pcPic->getSlice(0)->getSPS()->getUsePCM()
1107          && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize())
1108          && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) )
1109        {
1110          UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2;
1111          UInt uiBestBits = rpcBestCU->getTotalBits();
1112#if H_3D_VSO // M7
1113          Double dRDCostTemp = m_pcRdCost->getUseVSO() ? m_pcRdCost->calcRdCostVSO(uiRawBits, 0) : m_pcRdCost->calcRdCost(uiRawBits, 0);
1114          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > dRDCostTemp ))
1115#else
1116          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
1117#endif
1118          {
1119            xCheckIntraPCM (rpcBestCU, rpcTempCU);
1120            rpcTempCU->initEstData( uiDepth, iQP );
1121          }
1122        }
1123#if  MTK_FAST_TEXTURE_ENCODING_E0173
1124        }
1125#endif
1126        if (isAddLowestQP && (iQP == lowestQP))
1127        {
1128          iQP = iMinQP;
1129        }
1130      }
1131    }
1132
1133    m_pcEntropyCoder->resetBits();
1134    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
1135    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1136    if(m_pcEncCfg->getUseSBACRD())
1137    {
1138      rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1139    }
1140
1141#if H_3D_VSO // M8
1142    if ( m_pcRdCost->getUseVSO() )   
1143      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );   
1144    else
1145#endif
1146    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
1147
1148    // accumulate statistics for early skip
1149    if ( m_pcEncCfg->getUseFastEnc() )
1150    {
1151      if ( rpcBestCU->isSkipped(0) )
1152      {
1153        Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
1154        afCost[ iIdx ] += rpcBestCU->getTotalCost();
1155        aiNum [ iIdx ] ++;
1156      }
1157    }
1158
1159    // Early CU determination
1160    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
1161    {
1162      bSubBranch = false;
1163    }
1164    else
1165    {
1166      bSubBranch = true;
1167    }
1168#if  MTK_FAST_TEXTURE_ENCODING_E0173
1169    if(rpcBestCU->getSlice()->getViewIndex() && !rpcBestCU->getSlice()->getIsDepth() && (uiDepth >=iIVFMaxD) && rpcBestCU->isSkipped(0))
1170    {
1171      bSubBranch = false;
1172    }
1173#endif
1174  }
1175  else if(!(bSliceEnd && bInsidePicture))
1176  {
1177    bBoundary = true;
1178#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
1179    m_addSADDepth++;
1180#endif
1181  }
1182
1183  // copy orginal YUV samples to PCM buffer
1184  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
1185  {
1186    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
1187  }
1188  if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1189  {
1190    Int idQP = m_pcEncCfg->getMaxDeltaQP();
1191    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
1192    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
1193    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
1194    {
1195      isAddLowestQP = true;
1196      iMinQP = iMinQP - 1;     
1197    }
1198  }
1199  else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1200  {
1201    iMinQP = iBaseQP;
1202    iMaxQP = iBaseQP;
1203  }
1204  else
1205  {
1206    Int iStartQP;
1207    if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr())
1208    {
1209      iStartQP = rpcTempCU->getQP(0);
1210    }
1211    else
1212    {
1213      UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1214      iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx);
1215    }
1216    iMinQP = iStartQP;
1217    iMaxQP = iStartQP;
1218  }
1219#if RATE_CONTROL_LAMBDA_DOMAIN
1220  if ( m_pcEncCfg->getUseRateCtrl() )
1221  {
1222    iMinQP = m_pcRateCtrl->getRCQP();
1223    iMaxQP = m_pcRateCtrl->getRCQP();
1224  }
1225#else
1226  if(m_pcEncCfg->getUseRateCtrl())
1227  {
1228    Int qp = m_pcRateCtrl->getUnitQP();
1229    iMinQP  = Clip3( MIN_QP, MAX_QP, qp);
1230    iMaxQP  = Clip3( MIN_QP, MAX_QP, qp);
1231  }
1232#endif
1233  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
1234  {
1235    if (isAddLowestQP && (iQP == iMinQP))
1236    {
1237      iQP = lowestQP;
1238    }
1239    rpcTempCU->initEstData( uiDepth, iQP );
1240
1241    // further split
1242    if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
1243    {
1244#if H_3D_VSO // M9
1245      // reset Model
1246      if( m_pcRdCost->getUseRenModel() )
1247      {
1248        UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth ( );
1249        UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight( );
1250        Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr( 0 );
1251        UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride();
1252        m_pcRdCost->setRenModelData( m_ppcBestCU[uiDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1253      }
1254#endif
1255
1256      UChar       uhNextDepth         = uiDepth+1;
1257      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
1258      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
1259
1260      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
1261      {
1262        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1263        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1264
1265        Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr();
1266        if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1267        {
1268          if( m_bUseSBACRD )
1269          {
1270            if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
1271            {
1272              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1273            }
1274            else
1275            {
1276              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
1277            }
1278          }
1279
1280#if AMP_ENC_SPEEDUP
1281          if ( rpcBestCU->isIntra(0) )
1282          {
1283            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE );
1284          }
1285          else
1286          {
1287            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) );
1288          }
1289#else
1290          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
1291#endif
1292
1293          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
1294          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
1295        }
1296        else if (bInSlice)
1297        {
1298          pcSubBestPartCU->copyToPic( uhNextDepth );
1299          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
1300        }
1301      }
1302
1303      if( !bBoundary )
1304      {
1305        m_pcEntropyCoder->resetBits();
1306        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
1307
1308        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1309        if(m_pcEncCfg->getUseSBACRD())
1310        {
1311          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1312        }
1313      }
1314
1315#if H_3D_VSO // M10
1316      if ( m_pcRdCost->getUseVSO() )
1317        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1318      else
1319#endif
1320      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1321
1322      if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
1323      {
1324        Bool hasResidual = false;
1325        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
1326        {
1327          if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && 
1328              ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
1329          {
1330            hasResidual = true;
1331            break;
1332          }
1333        }
1334
1335        UInt uiTargetPartIdx;
1336        if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() )
1337        {
1338          uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1339        }
1340        else
1341        {
1342          uiTargetPartIdx = 0;
1343        }
1344        if ( hasResidual )
1345        {
1346#if !RDO_WITHOUT_DQP_BITS
1347          m_pcEntropyCoder->resetBits();
1348          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
1349          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1350          if(m_pcEncCfg->getUseSBACRD())
1351          {
1352            rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1353          }
1354#if H_3D_VSO // M11
1355          if ( m_pcRdCost->getUseLambdaScaleVSO())         
1356            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );         
1357          else
1358#endif
1359          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1360#endif
1361
1362          Bool foundNonZeroCbf = false;
1363          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf );
1364          assert( foundNonZeroCbf );
1365        }
1366        else
1367        {
1368          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
1369        }
1370      }
1371
1372      if( m_bUseSBACRD )
1373      {
1374        m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1375      }
1376      Bool isEndOfSlice        = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES
1377                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
1378      Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
1379                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
1380      if(isEndOfSlice||isEndOfSliceSegment)
1381      {
1382        rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
1383      }
1384      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth);                                  // RD compare current larger prediction
1385    }                                                                                  // with sub partitioned prediction.
1386    if (isAddLowestQP && (iQP == lowestQP))
1387    {
1388      iQP = iMinQP;
1389    }
1390  }
1391
1392
1393#if H_3D_VSO // M12
1394  if( m_pcRdCost->getUseRenModel() )
1395  {
1396    UInt  uiWidth     = m_ppcRecoYuvBest[uiDepth]->getWidth   ( );
1397    UInt  uiHeight    = m_ppcRecoYuvBest[uiDepth]->getHeight  ( );
1398    Pel*  piSrc       = m_ppcRecoYuvBest[uiDepth]->getLumaAddr( 0 );
1399    UInt  uiSrcStride = m_ppcRecoYuvBest[uiDepth]->getStride  ( );
1400    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1401  }
1402#endif
1403
1404  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
1405
1406  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY );   // Copy Yuv data to picture Yuv
1407  if( bBoundary ||(bSliceEnd && bInsidePicture))
1408  {
1409    return;
1410  }
1411
1412  // Assert if Best prediction mode is NONE
1413  // Selected mode's RD-cost must be not MAX_DOUBLE.
1414  assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE  );
1415  assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE  );
1416  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE );
1417}
1418
1419/** finish encoding a cu and handle end-of-slice conditions
1420 * \param pcCU
1421 * \param uiAbsPartIdx
1422 * \param uiDepth
1423 * \returns Void
1424 */
1425Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1426{
1427  TComPic* pcPic = pcCU->getPic();
1428  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1429
1430  //Calculate end address
1431  UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx;
1432
1433  UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU();
1434  UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU();
1435  UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1436  UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1437  UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
1438  UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
1439  while(uiPosX>=uiWidth||uiPosY>=uiHeight)
1440  {
1441    uiInternalAddress--;
1442    uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1443    uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1444  }
1445  uiInternalAddress++;
1446  if(uiInternalAddress==pcCU->getPic()->getNumPartInCU())
1447  {
1448    uiInternalAddress = 0;
1449    uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1);
1450  }
1451  UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress);
1452
1453  // Encode slice finish
1454  Bool bTerminateSlice = false;
1455  if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress)
1456  {
1457    bTerminateSlice = true;
1458  }
1459  UInt uiGranularityWidth = g_uiMaxCUWidth;
1460  uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1461  uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1462  Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth))
1463    &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight));
1464 
1465  if(granularityBoundary)
1466  {
1467    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1468    if (!bTerminateSlice)
1469      m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 );
1470  }
1471 
1472  Int numberOfWrittenBits = 0;
1473  if (m_pcBitCounter)
1474  {
1475    numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits();
1476  }
1477 
1478  // Calculate slice end IF this CU puts us over slice bit size.
1479  UInt iGranularitySize = pcCU->getPic()->getNumPartInCU();
1480  Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize;
1481  if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) 
1482  {
1483    iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)));
1484  }
1485  // Set slice end parameter
1486  if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) 
1487  {
1488    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1489    pcSlice->setSliceCurEndCUAddr(iGranularityEnd);
1490    return;
1491  }
1492  // Set dependent slice end parameter
1493  if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) 
1494  {
1495    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1496    return;
1497  }
1498  if(granularityBoundary)
1499  {
1500    pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) );
1501    pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits);
1502    if (m_pcBitCounter)
1503    {
1504      m_pcEntropyCoder->resetBits();     
1505    }
1506  }
1507}
1508
1509/** Compute QP for each CU
1510 * \param pcCU Target CU
1511 * \param uiDepth CU depth
1512 * \returns quantization parameter
1513 */
1514Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1515{
1516  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1517  Int iQpOffset = 0;
1518  if ( m_pcEncCfg->getUseAdaptiveQP() )
1519  {
1520    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1521    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1522    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1523    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1524    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1525    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1526    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1527
1528    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1529    Double dAvgAct = pcAQLayer->getAvgActivity();
1530    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1531    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1532    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1533    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1534  }
1535  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset );
1536}
1537
1538/** encode a CU block recursively
1539 * \param pcCU
1540 * \param uiAbsPartIdx
1541 * \param uiDepth
1542 * \returns Void
1543 */
1544Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1545{
1546  TComPic* pcPic = pcCU->getPic();
1547 
1548  Bool bBoundary = false;
1549  UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1550  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1551  UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1552  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1553 
1554#if H_MV_ENC_DEC_TRAC
1555  DTRACE_CU_S("=========== coding_quadtree ===========\n")
1556  DTRACE_CU("x0", uiLPelX)
1557  DTRACE_CU("x1", uiTPelY)
1558  DTRACE_CU("log2CbSize", g_uiMaxCUWidth>>uiDepth)
1559  DTRACE_CU("cqtDepth"  , uiDepth)
1560#endif
1561
1562  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1563  // If slice start is within this cu...
1564  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1565    pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) );
1566  // We need to split, so don't try these modes.
1567  if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1568  {
1569    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1570  }
1571  else
1572  {
1573    bBoundary = true;
1574  }
1575 
1576  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary )
1577  {
1578    UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2;
1579    if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1580    {
1581      setdQPFlag(true);
1582    }
1583    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1584    {
1585      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1586      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1587      Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr();
1588      if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1589      {
1590        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1591      }
1592    }
1593    return;
1594  }
1595 
1596#if H_MV_ENC_DEC_TRAC
1597  DTRACE_CU_S("=========== coding_unit ===========\n")
1598#endif
1599
1600  if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1601  {
1602    setdQPFlag(true);
1603  }
1604  if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1605  {
1606    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1607  }
1608  if( !pcCU->getSlice()->isIntra() )
1609  {
1610    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1611  }
1612 
1613  if( pcCU->isSkipped( uiAbsPartIdx ) )
1614  {
1615#if H_MV_ENC_DEC_TRAC
1616    DTRACE_PU_S("=========== prediction_unit ===========\n")
1617    DTRACE_PU("x0", uiLPelX)
1618    DTRACE_PU("x1", uiTPelY)
1619#endif
1620    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1621#if H_3D_IC
1622    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1623#endif
1624#if H_3D_ARP
1625    m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1626#endif
1627    finishCU(pcCU,uiAbsPartIdx,uiDepth);
1628    return;
1629  }
1630  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1631 
1632  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1633 
1634  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1635  {
1636    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1637
1638    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1639    {
1640      // Encode slice finish
1641      finishCU(pcCU,uiAbsPartIdx,uiDepth);
1642      return;
1643    }
1644  }
1645
1646  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1647  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1648#if H_3D_IC
1649  m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1650#endif
1651#if H_3D_ARP
1652  m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1653#endif
1654#if LGE_INTER_SDC_E0156
1655  m_pcEntropyCoder->encodeInterSDCFlag( pcCU, uiAbsPartIdx, false );
1656#endif
1657
1658  // Encode Coefficients
1659  Bool bCodeDQP = getdQPFlag();
1660  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP );
1661  setdQPFlag( bCodeDQP );
1662
1663  // --- write terminating bit ---
1664  finishCU(pcCU,uiAbsPartIdx,uiDepth);
1665}
1666
1667#if RATE_CONTROL_INTRA
1668Int xCalcHADs8x8_ISlice(Pel *piOrg, Int iStrideOrg) 
1669{
1670  Int k, i, j, jj;
1671  Int diff[64], m1[8][8], m2[8][8], m3[8][8], iSumHad = 0;
1672
1673  for( k = 0; k < 64; k += 8 )
1674  {
1675    diff[k+0] = piOrg[0] ;
1676    diff[k+1] = piOrg[1] ;
1677    diff[k+2] = piOrg[2] ;
1678    diff[k+3] = piOrg[3] ;
1679    diff[k+4] = piOrg[4] ;
1680    diff[k+5] = piOrg[5] ;
1681    diff[k+6] = piOrg[6] ;
1682    diff[k+7] = piOrg[7] ;
1683 
1684    piOrg += iStrideOrg;
1685  }
1686 
1687  //horizontal
1688  for (j=0; j < 8; j++)
1689  {
1690    jj = j << 3;
1691    m2[j][0] = diff[jj  ] + diff[jj+4];
1692    m2[j][1] = diff[jj+1] + diff[jj+5];
1693    m2[j][2] = diff[jj+2] + diff[jj+6];
1694    m2[j][3] = diff[jj+3] + diff[jj+7];
1695    m2[j][4] = diff[jj  ] - diff[jj+4];
1696    m2[j][5] = diff[jj+1] - diff[jj+5];
1697    m2[j][6] = diff[jj+2] - diff[jj+6];
1698    m2[j][7] = diff[jj+3] - diff[jj+7];
1699   
1700    m1[j][0] = m2[j][0] + m2[j][2];
1701    m1[j][1] = m2[j][1] + m2[j][3];
1702    m1[j][2] = m2[j][0] - m2[j][2];
1703    m1[j][3] = m2[j][1] - m2[j][3];
1704    m1[j][4] = m2[j][4] + m2[j][6];
1705    m1[j][5] = m2[j][5] + m2[j][7];
1706    m1[j][6] = m2[j][4] - m2[j][6];
1707    m1[j][7] = m2[j][5] - m2[j][7];
1708   
1709    m2[j][0] = m1[j][0] + m1[j][1];
1710    m2[j][1] = m1[j][0] - m1[j][1];
1711    m2[j][2] = m1[j][2] + m1[j][3];
1712    m2[j][3] = m1[j][2] - m1[j][3];
1713    m2[j][4] = m1[j][4] + m1[j][5];
1714    m2[j][5] = m1[j][4] - m1[j][5];
1715    m2[j][6] = m1[j][6] + m1[j][7];
1716    m2[j][7] = m1[j][6] - m1[j][7];
1717  }
1718 
1719  //vertical
1720  for (i=0; i < 8; i++)
1721  {
1722    m3[0][i] = m2[0][i] + m2[4][i];
1723    m3[1][i] = m2[1][i] + m2[5][i];
1724    m3[2][i] = m2[2][i] + m2[6][i];
1725    m3[3][i] = m2[3][i] + m2[7][i];
1726    m3[4][i] = m2[0][i] - m2[4][i];
1727    m3[5][i] = m2[1][i] - m2[5][i];
1728    m3[6][i] = m2[2][i] - m2[6][i];
1729    m3[7][i] = m2[3][i] - m2[7][i];
1730   
1731    m1[0][i] = m3[0][i] + m3[2][i];
1732    m1[1][i] = m3[1][i] + m3[3][i];
1733    m1[2][i] = m3[0][i] - m3[2][i];
1734    m1[3][i] = m3[1][i] - m3[3][i];
1735    m1[4][i] = m3[4][i] + m3[6][i];
1736    m1[5][i] = m3[5][i] + m3[7][i];
1737    m1[6][i] = m3[4][i] - m3[6][i];
1738    m1[7][i] = m3[5][i] - m3[7][i];
1739   
1740    m2[0][i] = m1[0][i] + m1[1][i];
1741    m2[1][i] = m1[0][i] - m1[1][i];
1742    m2[2][i] = m1[2][i] + m1[3][i];
1743    m2[3][i] = m1[2][i] - m1[3][i];
1744    m2[4][i] = m1[4][i] + m1[5][i];
1745    m2[5][i] = m1[4][i] - m1[5][i];
1746    m2[6][i] = m1[6][i] + m1[7][i];
1747    m2[7][i] = m1[6][i] - m1[7][i];
1748  }
1749 
1750  for (i = 0; i < 8; i++)
1751  {
1752    for (j = 0; j < 8; j++)
1753    {
1754      iSumHad += abs(m2[i][j]);
1755    }
1756  }
1757  iSumHad -= abs(m2[0][0]);
1758  iSumHad =(iSumHad+2)>>2;
1759  return(iSumHad);
1760}
1761
1762Int  TEncCu::updateLCUDataISlice(TComDataCU* pcCU, Int LCUIdx, Int width, Int height)
1763{
1764  Int  xBl, yBl; 
1765  const Int iBlkSize = 8;
1766
1767  Pel* pOrgInit   = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
1768  Int  iStrideOrig = pcCU->getPic()->getPicYuvOrg()->getStride();
1769  Pel  *pOrg;
1770
1771  Int iSumHad = 0;
1772  for ( yBl=0; (yBl+iBlkSize)<=height; yBl+= iBlkSize)
1773  {
1774    for ( xBl=0; (xBl+iBlkSize)<=width; xBl+= iBlkSize)
1775    {
1776      pOrg = pOrgInit + iStrideOrig*yBl + xBl; 
1777      iSumHad += xCalcHADs8x8_ISlice(pOrg, iStrideOrig);
1778    }
1779  }
1780  return(iSumHad);
1781}
1782#endif
1783
1784/** check RD costs for a CU block encoded with merge
1785 * \param rpcBestCU
1786 * \param rpcTempCU
1787 * \returns Void
1788 */
1789Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode )
1790{
1791  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1792#if H_3D_IV_MERGE
1793  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS_MEM << 1]; // double length for mv of both lists
1794  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS_MEM];
1795#else
1796  TComMvField  cMvFieldNeighbours[2 * MRG_MAX_NUM_CANDS]; // double length for mv of both lists
1797  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1798#endif
1799  Int numValidMergeCand = 0;
1800
1801  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1802  {
1803    uhInterDirNeighbours[ui] = 0;
1804  }
1805  UChar uhDepth = rpcTempCU->getDepth( 0 );
1806#if H_3D_IC
1807  Bool bICFlag = rpcTempCU->getICFlag( 0 );
1808#endif
1809#if H_3D_VSO // M1  //nececcary here?
1810  if( m_pcRdCost->getUseRenModel() )
1811  {
1812    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1813    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1814    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1815    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1816    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1817  }
1818#endif
1819
1820  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1821  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth );
1822
1823#if H_3D_VSP
1824  Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1825  memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1826  InheritedVSPDisInfo inheritedVSPDisInfo[MRG_MAX_NUM_CANDS_MEM];
1827  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag,inheritedVSPDisInfo, numValidMergeCand );
1828#else
1829  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1830#endif
1831
1832#if H_3D_IV_MERGE
1833  Int mergeCandBuffer[MRG_MAX_NUM_CANDS_MEM];
1834#else
1835  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1836#endif
1837for( UInt ui = 0; ui < numValidMergeCand; ++ui )
1838  {
1839    mergeCandBuffer[ui] = 0;
1840  }
1841
1842  Bool bestIsSkip = false;
1843
1844  UInt iteration;
1845  if ( rpcTempCU->isLosslessCoded(0))
1846  {
1847    iteration = 1;
1848  }
1849  else 
1850  {
1851    iteration = 2;
1852  }
1853
1854#if H_3D_ARP
1855  Int nARPWMax = rpcTempCU->getSlice()->getARPStepNum() - 1;
1856  if( nARPWMax < 0 || !rpcTempCU->getDvInfo(0).bDV )
1857  {
1858    nARPWMax = 0;
1859  }
1860  for( Int nARPW=nARPWMax; nARPW >= 0 ; nARPW-- )
1861  {
1862    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS*sizeof(Int) );
1863#endif
1864  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1865  {
1866    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1867    {     
1868#if H_3D_IC
1869        if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
1870        {
1871          if( bICFlag && uiMergeCand == 0 ) 
1872          {
1873            continue;
1874          }
1875        }
1876#endif
1877        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1878        {
1879        if( !(bestIsSkip && uiNoResidual == 0) )
1880        {
1881          // set MC parameters
1882          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1883          rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(),     0, uhDepth );
1884          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1885#if H_3D_IC
1886          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1887#endif
1888#if H_3D_ARP
1889          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1890#endif
1891          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1892          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1893#if H_3D_VSP
1894          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
1895          rpcTempCU->setDvInfoSubParts(inheritedVSPDisInfo[uiMergeCand].m_acDvInfo, 0, 0, uhDepth );
1896#endif
1897          rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1898          rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1899          rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1900
1901#if H_3D_ARP
1902          if( nARPW )
1903          {
1904            Bool bSignalflag[2] = { true, true };
1905            for( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1906            {
1907              Int iRefIdx = cMvFieldNeighbours[uiRefListIdx + 2*uiMergeCand].getRefIdx();
1908              RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1909              if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1910              {
1911                bSignalflag[uiRefListIdx] = false;
1912              }
1913            }
1914            if( !bSignalflag[0] && !bSignalflag[1] )
1915            {
1916              rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1917            }
1918          }
1919#endif
1920       // do MC
1921       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1922       // estimate residual and encode everything
1923#if H_3D_VSO //M2
1924       if( m_pcRdCost->getUseRenModel() )
1925       { //Reset
1926         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1927         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1928         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1929         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1930         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1931       }
1932#endif
1933       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1934         m_ppcOrigYuv    [uhDepth],
1935         m_ppcPredYuvTemp[uhDepth],
1936         m_ppcResiYuvTemp[uhDepth],
1937         m_ppcResiYuvBest[uhDepth],
1938         m_ppcRecoYuvTemp[uhDepth],
1939         (uiNoResidual? true:false));
1940
1941
1942          if ( uiNoResidual == 0 && rpcTempCU->getQtRootCbf(0) == 0 )
1943         {
1944            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
1945           mergeCandBuffer[uiMergeCand] = 1;
1946         }
1947
1948          rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
1949#if LGE_INTER_SDC_E0156
1950          TComDataCU *rpcTempCUPre = rpcTempCU;
1951#endif
1952          Int orgQP = rpcTempCU->getQP( 0 );
1953          xCheckDQP( rpcTempCU );
1954          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1955#if LGE_INTER_SDC_E0156
1956          if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() && !uiNoResidual )
1957          {
1958            if( rpcTempCU != rpcTempCUPre )
1959            {
1960              rpcTempCU->initEstData( uhDepth, orgQP );
1961              rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
1962            }
1963            rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
1964            rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
1965            rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
1966#if H_3D_VSO //M2
1967            if( m_pcRdCost->getUseRenModel() )
1968            { //Reset
1969              UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1970              UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1971              Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1972              UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1973              m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1974            }
1975#endif
1976            m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
1977              m_ppcOrigYuv[uhDepth], 
1978              ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth], 
1979              m_ppcResiYuvTemp[uhDepth], 
1980              m_ppcRecoYuvTemp[uhDepth], 
1981              uhDepth );
1982
1983            xCheckDQP( rpcTempCU );
1984            xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
1985          }
1986#endif
1987          rpcTempCU->initEstData( uhDepth, orgQP );
1988
1989      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
1990      {
1991#if LGE_INTER_SDC_E0156
1992        if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) )
1993        {
1994          bestIsSkip = !rpcBestCU->getSDCFlag( 0 ) && ( rpcBestCU->getQtRootCbf(0) == 0 );
1995        }
1996        else
1997        {
1998#endif
1999        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
2000#if LGE_INTER_SDC_E0156
2001        }
2002#endif
2003      }
2004    }
2005   }
2006  }
2007
2008  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
2009  {
2010    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
2011    {
2012      if( rpcBestCU->getMergeFlag( 0 ))
2013      {
2014        *earlyDetectionSkipMode = true;
2015      }
2016      else
2017      {
2018        Int absoulte_MV=0;
2019        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2020        {
2021          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
2022          {
2023            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
2024            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
2025            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
2026            absoulte_MV+=iHor+iVer;
2027          }
2028        }
2029
2030        if(absoulte_MV == 0)
2031        {
2032          *earlyDetectionSkipMode = true;
2033        }
2034      }
2035    }
2036  }
2037 }
2038#if H_3D_ARP
2039 }
2040#endif
2041}
2042
2043
2044#if AMP_MRG
2045#if  MTK_FAST_TEXTURE_ENCODING_E0173
2046Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bFMD, Bool bUseMRG)
2047#else
2048Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
2049#endif
2050#else
2051Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
2052#endif
2053{
2054#if  MTK_FAST_TEXTURE_ENCODING_E0173
2055  if(!(bFMD && (ePartSize == SIZE_2Nx2N)))  //have  motion estimation or merge check
2056  {
2057#endif
2058  UChar uhDepth = rpcTempCU->getDepth( 0 );
2059#if H_3D_ARP
2060  Int iLayerId    = rpcTempCU->getSlice()->getLayerId();
2061  Bool bFirstTime = true;
2062  Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
2063
2064  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || !rpcTempCU->getDvInfo(0).bDV  )
2065  {
2066    nARPWMax = 0;
2067  }
2068
2069  for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
2070  {
2071    if( bFirstTime == false && rpcTempCU->getSlice()->getVPS()->getUseAdvRP( iLayerId ) )
2072    {
2073      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0) );
2074    }
2075#endif
2076#if H_3D_VSO // M3
2077  if( m_pcRdCost->getUseRenModel() )
2078  {
2079    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2080    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2081    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2082    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2083    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2084  }
2085#endif
2086
2087  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2088 
2089  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2090
2091  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
2092  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2093  rpcTempCU->setCUTransquantBypassSubParts  ( m_pcEncCfg->getCUTransquantBypassFlagValue(),      0, uhDepth );
2094 
2095#if H_3D_ARP
2096  rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2097#endif
2098
2099#if H_3D_ARP
2100  if( bFirstTime == false && nARPWMax )
2101  {
2102    rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
2103    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2104
2105    m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
2106
2107    if(rpcTempCU->getPartitionSize(0)==SIZE_2Nx2N)
2108    {
2109      Bool bSignalflag[2] = { true, true };
2110      for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
2111      {
2112        RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
2113        Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
2114        if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
2115        {
2116          bSignalflag[uiRefListIdx] = false;
2117        }
2118      }
2119      if( !bSignalflag[0] && !bSignalflag[1] )
2120      {
2121        rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
2122      }
2123    }
2124  }
2125  else
2126  {
2127    bFirstTime = false;
2128#endif
2129#if AMP_MRG
2130  rpcTempCU->setMergeAMP (true);
2131#if  MTK_FAST_TEXTURE_ENCODING_E0173
2132  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bFMD, false, bUseMRG );
2133#else
2134  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
2135#endif
2136#else 
2137  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
2138#endif
2139#if H_3D_ARP
2140   if( nARPWMax )
2141   {
2142     m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
2143
2144     Bool bSignalflag[2] = { true, true };
2145     for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
2146     {
2147       RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
2148       Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
2149       if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
2150       {
2151         bSignalflag[uiRefListIdx] = false;
2152       }
2153     }
2154     if( !bSignalflag[0] && !bSignalflag[1])
2155     {
2156       rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
2157     }
2158   }
2159  }
2160#endif
2161
2162#if AMP_MRG
2163  if ( !rpcTempCU->getMergeAMP() )
2164  {
2165#if H_3D_ARP
2166    if( nARPWMax )
2167    {
2168      continue;
2169    }
2170    else
2171#endif
2172    return;
2173  }
2174#endif
2175
2176#if RATE_CONTROL_LAMBDA_DOMAIN  && (!M0036_RC_IMPROVEMENT || KWU_RC_MADPRED_E0227)
2177  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2178  {
2179    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2180      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2181      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2182    m_temporalSAD = (Int)SAD;
2183  }
2184#endif
2185#if !RATE_CONTROL_LAMBDA_DOMAIN  && KWU_RC_MADPRED_E0227
2186  if ( m_pcEncCfg->getUseRateCtrl() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2187  {
2188    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2189      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2190      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2191    m_temporalSAD = (Int)SAD;
2192  }
2193#endif
2194
2195  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
2196
2197
2198#if H_3D_VSO // M4
2199  if( m_pcRdCost->getUseLambdaScaleVSO() )
2200    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2201  else
2202#endif
2203  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2204#if LGE_INTER_SDC_E0156
2205  TComDataCU *rpcTempCUPre = rpcTempCU;
2206#endif
2207  xCheckDQP( rpcTempCU );
2208  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2209#if LGE_INTER_SDC_E0156
2210  if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() )
2211  {
2212    if( rpcTempCU != rpcTempCUPre )
2213    {
2214      Int orgQP = rpcBestCU->getQP( 0 );
2215      rpcTempCU->initEstData( uhDepth, orgQP );
2216      rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
2217    }
2218    rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2219    rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
2220    rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
2221#if H_3D_VSO // M3
2222    if( m_pcRdCost->getUseRenModel() )
2223    {
2224      UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2225      UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2226      Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2227      UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2228      m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2229    }
2230#endif
2231
2232    m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2233      m_ppcOrigYuv[uhDepth],
2234      ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth],
2235      m_ppcResiYuvTemp[uhDepth],
2236      m_ppcRecoYuvTemp[uhDepth],
2237      uhDepth );
2238
2239  xCheckDQP( rpcTempCU );
2240  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2241  }
2242#endif
2243#if H_3D_ARP
2244  }
2245#endif
2246#if  MTK_FAST_TEXTURE_ENCODING_E0173
2247  }
2248#endif
2249}
2250
2251Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2252{
2253  UInt uiDepth = rpcTempCU->getDepth( 0 );
2254 
2255#if H_3D_VSO // M5
2256  if( m_pcRdCost->getUseRenModel() )
2257  {
2258    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
2259    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
2260    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
2261    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
2262    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2263  }
2264#endif
2265
2266  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2267
2268  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
2269  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2270  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
2271 
2272  Bool bSeparateLumaChroma = true; // choose estimation mode
2273  UInt uiPreCalcDistC      = 0;
2274  if( !bSeparateLumaChroma )
2275  {
2276    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
2277  }
2278  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
2279
2280  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
2281 
2282#if H_3D_DIM_SDC
2283  if( !rpcTempCU->getSDCFlag( 0 ) )
2284#endif
2285  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
2286 
2287  m_pcEntropyCoder->resetBits();
2288  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2289  {
2290    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2291  }
2292  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2293  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
2294  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
2295  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
2296  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
2297
2298  // Encode Coefficients
2299  Bool bCodeDQP = getdQPFlag();
2300  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
2301  setdQPFlag( bCodeDQP );
2302 
2303  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2304 
2305  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2306  if(m_pcEncCfg->getUseSBACRD())
2307  {
2308    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2309  }
2310
2311#if H_3D_VSO // M6
2312  if( m_pcRdCost->getUseLambdaScaleVSO()) 
2313    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
2314  else
2315#endif
2316  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2317 
2318#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
2319  UChar uhDepth = rpcTempCU->getDepth( 0 );
2320  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && eSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2321  {
2322    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2323      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2324      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2325    m_spatialSAD = (Int)SAD;
2326  }
2327#endif
2328#if RATE_CONTROL_LAMBDA_DOMAIN && M0036_RC_IMPROVEMENT && KWU_RC_MADPRED_E0227
2329  UChar uhDepth = rpcTempCU->getDepth( 0 );
2330  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && eSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2331  {
2332    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2333      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2334      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2335    m_spatialSAD = (Int)SAD;
2336  }
2337#endif
2338#if !RATE_CONTROL_LAMBDA_DOMAIN && KWU_RC_MADPRED_E0227
2339  UChar uhDepth = rpcTempCU->getDepth( 0 );
2340  if ( m_pcEncCfg->getUseRateCtrl() && eSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2341  {
2342    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2343      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2344      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2345    m_spatialSAD = (Int)SAD;
2346  }
2347#endif
2348  xCheckDQP( rpcTempCU );
2349  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2350}
2351
2352/** Check R-D costs for a CU with PCM mode.
2353 * \param rpcBestCU pointer to best mode CU data structure
2354 * \param rpcTempCU pointer to testing mode CU data structure
2355 * \returns Void
2356 *
2357 * \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.
2358 */
2359Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
2360{
2361  UInt uiDepth = rpcTempCU->getDepth( 0 );
2362
2363  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2364
2365  rpcTempCU->setIPCMFlag(0, true);
2366  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
2367  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2368  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2369  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
2370  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
2371
2372  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
2373
2374  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
2375
2376  m_pcEntropyCoder->resetBits();
2377  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2378  {
2379    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2380  }
2381  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2382  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
2383  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
2384  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
2385
2386  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2387
2388  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2389  if(m_pcEncCfg->getUseSBACRD())
2390  {
2391    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2392  }
2393#if H_3D_VSO // M44
2394  if ( m_pcRdCost->getUseVSO() )
2395    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2396  else
2397#endif
2398  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2399
2400  xCheckDQP( rpcTempCU );
2401  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
2402}
2403
2404/** check whether current try is the best with identifying the depth of current try
2405 * \param rpcBestCU
2406 * \param rpcTempCU
2407 * \returns Void
2408 */
2409Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
2410{
2411  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
2412  {
2413    TComYuv* pcYuv;
2414    // Change Information data
2415    TComDataCU* pcCU = rpcBestCU;
2416    rpcBestCU = rpcTempCU;
2417    rpcTempCU = pcCU;
2418
2419    // Change Prediction data
2420    pcYuv = m_ppcPredYuvBest[uiDepth];
2421    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
2422    m_ppcPredYuvTemp[uiDepth] = pcYuv;
2423
2424    // Change Reconstruction data
2425    pcYuv = m_ppcRecoYuvBest[uiDepth];
2426    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
2427    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
2428
2429    pcYuv = NULL;
2430    pcCU  = NULL;
2431
2432    if( m_bUseSBACRD )  // store temp best CI for next CU coding
2433      m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
2434  }
2435}
2436
2437Void TEncCu::xCheckDQP( TComDataCU* pcCU )
2438{
2439  UInt uiDepth = pcCU->getDepth( 0 );
2440
2441  if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() )
2442  {
2443    if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) )
2444    {
2445#if !RDO_WITHOUT_DQP_BITS
2446      m_pcEntropyCoder->resetBits();
2447      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
2448      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
2449      if(m_pcEncCfg->getUseSBACRD())
2450      {
2451        pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2452      }
2453#if H_3D_VSO // M45
2454      if ( m_pcRdCost->getUseVSO() )     
2455        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
2456      else
2457#endif
2458      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
2459#endif
2460    }
2461    else
2462    {
2463      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
2464    }
2465  }
2466}
2467
2468Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
2469{
2470  pDst->iN = pSrc->iN;
2471  for (Int i = 0; i < pSrc->iN; i++)
2472  {
2473    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
2474  }
2475}
2476Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY )
2477{
2478  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
2479  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
2480  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
2481  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2482    pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2483  Bool bSliceEnd   = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2484    pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2485  if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2486  {
2487    UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
2488    UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth);
2489    UInt uiBlkWidth    = rpcPic->getNumPartInWidth() >> (uiDepth);
2490    UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2491    UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2492    UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
2493    m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
2494  }
2495  else
2496  {
2497    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2;
2498
2499    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
2500    {
2501      UInt uiSubCULPelX   = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx &  1 );
2502      UInt uiSubCUTPelY   = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 );
2503
2504      Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && 
2505        rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr();
2506      if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2507      {
2508        xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY );   // Copy Yuv data to picture Yuv
2509      }
2510    }
2511  }
2512}
2513
2514Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
2515{
2516  UInt uiCurrDepth = uiNextDepth - 1;
2517  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
2518}
2519
2520/** Function for filling the PCM buffer of a CU using its original sample array
2521 * \param pcCU pointer to current CU
2522 * \param pcOrgYuv pointer to original sample array
2523 * \returns Void
2524 */
2525Void TEncCu::xFillPCMBuffer     ( TComDataCU*& pCU, TComYuv* pOrgYuv )
2526{
2527
2528  UInt   width        = pCU->getWidth(0);
2529  UInt   height       = pCU->getHeight(0);
2530
2531  Pel*   pSrcY = pOrgYuv->getLumaAddr(0, width); 
2532  Pel*   pDstY = pCU->getPCMSampleY();
2533  UInt   srcStride = pOrgYuv->getStride();
2534
2535  for(Int y = 0; y < height; y++ )
2536  {
2537    for(Int x = 0; x < width; x++ )
2538    {
2539      pDstY[x] = pSrcY[x];
2540    }
2541    pDstY += width;
2542    pSrcY += srcStride;
2543  }
2544
2545  Pel* pSrcCb       = pOrgYuv->getCbAddr();
2546  Pel* pSrcCr       = pOrgYuv->getCrAddr();;
2547
2548  Pel* pDstCb       = pCU->getPCMSampleCb();
2549  Pel* pDstCr       = pCU->getPCMSampleCr();;
2550
2551  UInt srcStrideC = pOrgYuv->getCStride();
2552  UInt heightC   = height >> 1;
2553  UInt widthC    = width  >> 1;
2554
2555  for(Int y = 0; y < heightC; y++ )
2556  {
2557    for(Int x = 0; x < widthC; x++ )
2558    {
2559      pDstCb[x] = pSrcCb[x];
2560      pDstCr[x] = pSrcCr[x];
2561    }
2562    pDstCb += widthC;
2563    pDstCr += widthC;
2564    pSrcCb += srcStrideC;
2565    pSrcCr += srcStrideC;
2566  }
2567}
2568
2569#if ADAPTIVE_QP_SELECTION
2570/** Collect ARL statistics from one block
2571  */
2572Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
2573{
2574  for( Int n = 0; n < NumCoeffInCU; n++ )
2575  {
2576    Int u = abs( rpcCoeff[ n ] );
2577    Int absc = rpcArlCoeff[ n ];
2578
2579    if( u != 0 )
2580    {
2581      if( u < LEVEL_RANGE )
2582      {
2583        cSum[ u ] += ( Double )absc;
2584        numSamples[ u ]++;
2585      }
2586      else 
2587      {
2588        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
2589        numSamples[ LEVEL_RANGE ]++;
2590      }
2591    }
2592  }
2593
2594  return 0;
2595}
2596
2597/** Collect ARL statistics from one LCU
2598 * \param pcCU
2599 */
2600Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU )
2601{
2602  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to datatype and quantization output
2603  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output
2604
2605  TCoeff* pCoeffY = rpcCU->getCoeffY();
2606  Int* pArlCoeffY = rpcCU->getArlCoeffY();
2607
2608  UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth;
2609  UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;
2610
2611  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
2612  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
2613
2614  // Collect stats to cSum[][] and numSamples[][]
2615  for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ )
2616  {
2617    UInt uiTrIdx = rpcCU->getTransformIdx(i);
2618
2619    if(rpcCU->getPredictionMode(i) == MODE_INTER)
2620    if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) )
2621    {
2622      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
2623    }//Note that only InterY is processed. QP rounding is based on InterY data only.
2624   
2625    pCoeffY  += uiMinNumCoeffInCU;
2626    pArlCoeffY  += uiMinNumCoeffInCU;
2627  }
2628
2629  for(Int u=1; u<LEVEL_RANGE;u++)
2630  {
2631    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
2632    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
2633  }
2634  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
2635  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
2636}
2637#endif
2638//! \}
Note: See TracBrowser for help on using the repository browser.