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

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

Clean up and revised according to the guideline.

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