source: 3DVCSoftware/branches/HTM-11.2-dev0/source/Lib/TLibEncoder/TEncCu.cpp @ 1038

Last change on this file since 1038 was 1038, checked in by sharpjp-htm, 10 years ago

Fix tickets #75 Bi-pred restriction bug in VSP
Fix tickets #79 Unused VSP code

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