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

Last change on this file since 1030 was 1030, checked in by tech, 10 years ago

Merged 11.2-dev2-Samsung@1026.

  • Property svn:eol-style set to native
File size: 108.0 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    InheritedVSPDisInfo inheritedVSPDisInfo[MRG_MAX_NUM_CANDS_MEM];
1862#if H_3D_SPIVMP
1863    Bool bSPIVMPFlag[MRG_MAX_NUM_CANDS_MEM];
1864    memset(bSPIVMPFlag, false, sizeof(Bool)*MRG_MAX_NUM_CANDS_MEM);
1865    TComMvField*  pcMvFieldSP;
1866    UChar* puhInterDirSP;
1867    pcMvFieldSP = new TComMvField[rpcTempCU->getPic()->getPicSym()->getNumPartition()*2]; 
1868    puhInterDirSP = new UChar[rpcTempCU->getPic()->getPicSym()->getNumPartition()]; 
1869#endif
1870#if H_3D
1871    rpcTempCU->initAvailableFlags();
1872    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1873    rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours
1874      , inheritedVSPDisInfo
1875#if H_3D_SPIVMP
1876      , pcMvFieldSP, puhInterDirSP
1877#endif
1878      , numValidMergeCand
1879      );
1880
1881    rpcTempCU->buildMCL( cMvFieldNeighbours,uhInterDirNeighbours, vspFlag
1882#if H_3D_SPIVMP
1883      , bSPIVMPFlag
1884#endif
1885      , numValidMergeCand
1886      );
1887
1888#else
1889    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag, inheritedVSPDisInfo, numValidMergeCand );
1890#endif
1891
1892#endif
1893
1894#if H_3D_DDD
1895    Int iDDDCand = rpcTempCU->getUseDDDCandIdx(); 
1896    UChar ucDDDepth = rpcTempCU->getDDTmpDepth();
1897    rpcTempCU->setUseDDD( false, 0, uhDepth );
1898#endif
1899
1900  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1901  {
1902    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1903    {     
1904#if H_3D_IC
1905        if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
1906        {
1907          if( bICFlag && uiMergeCand == 0 ) 
1908          {
1909            continue;
1910          }
1911        }
1912#endif
1913        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1914        {
1915        if( !(bestIsSkip && uiNoResidual == 0) )
1916        {
1917          // set MC parameters
1918          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1919          rpcTempCU->setCUTransquantBypassSubParts( bTransquantBypassFlag,     0, uhDepth );
1920          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1921#if H_3D_IC
1922          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1923#endif
1924#if H_3D_ARP
1925          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1926#endif
1927          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1928          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1929#if H_3D_VSP
1930          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
1931          rpcTempCU->setDvInfoSubParts(inheritedVSPDisInfo[uiMergeCand].m_acDvInfo, 0, 0, uhDepth );
1932#endif
1933#if H_3D_DDD
1934          if( rpcTempCU->getSlice()->getIsDepth() && rpcTempCU->getSlice()->getViewIndex() != 0 && iDDDCand == uiMergeCand )
1935          {
1936              rpcTempCU->setUseDDD( true, 0, 0, uhDepth );
1937              rpcTempCU->setDDDepthSubParts( ucDDDepth, 0, 0, uhDepth );
1938          }
1939          else
1940          {
1941              rpcTempCU->setUseDDD( false, 0, 0, uhDepth );
1942          }
1943#endif
1944#if H_3D_SPIVMP
1945          rpcTempCU->setSPIVMPFlagSubParts(bSPIVMPFlag[uiMergeCand], 0, 0, uhDepth);
1946          if (bSPIVMPFlag[uiMergeCand])
1947          {
1948            UInt uiSPAddr;
1949            Int iWidth = rpcTempCU->getWidth(0);
1950            Int iHeight = rpcTempCU->getHeight(0);
1951            Int iNumSPInOneLine, iNumSP, iSPWidth, iSPHeight;
1952            rpcTempCU->getSPPara(iWidth, iHeight, iNumSP, iNumSPInOneLine, iSPWidth, iSPHeight);
1953            for (Int iPartitionIdx = 0; iPartitionIdx < iNumSP; iPartitionIdx++)
1954            {
1955              rpcTempCU->getSPAbsPartIdx(0, iSPWidth, iSPHeight, iPartitionIdx, iNumSPInOneLine, uiSPAddr);
1956              rpcTempCU->setInterDirSP(puhInterDirSP[iPartitionIdx], uiSPAddr, iSPWidth, iSPHeight);
1957              rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx], iSPWidth, iSPHeight);
1958              rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx + 1], iSPWidth, iSPHeight);
1959            }
1960          }
1961          else
1962#endif
1963#if H_3D_VSP
1964          {
1965          if ( vspFlag[uiMergeCand] )
1966          {
1967            UInt partAddr;
1968            Int vspSize;
1969            Int width, height;
1970            rpcTempCU->getPartIndexAndSize( 0, partAddr, width, height );
1971            if( uhInterDirNeighbours[ uiMergeCand ] & 0x01 )
1972            {
1973              rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_0, cMvFieldNeighbours[ 2*uiMergeCand + 0 ].getRefIdx(), vspSize );
1974              rpcTempCU->setVSPFlag( partAddr, vspSize );
1975            }
1976            else
1977            {
1978              rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1979            }
1980            if( uhInterDirNeighbours[ uiMergeCand ] & 0x02 )
1981            {
1982              rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_1 , cMvFieldNeighbours[ 2*uiMergeCand + 1 ].getRefIdx(), vspSize );
1983              rpcTempCU->setVSPFlag( partAddr, vspSize );
1984            }
1985            else
1986            {
1987              rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1988            }
1989            rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1990          }
1991          else
1992          {
1993#endif
1994            rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1995            rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1996            rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1997#if H_3D_VSP
1998          }
1999        }
2000#endif
2001       // do MC
2002       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
2003       // estimate residual and encode everything
2004#if H_3D_VSO //M2
2005       if( m_pcRdCost->getUseRenModel() )
2006       { //Reset
2007         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
2008         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
2009         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
2010         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
2011         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2012       }
2013#endif
2014       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
2015         m_ppcOrigYuv    [uhDepth],
2016         m_ppcPredYuvTemp[uhDepth],
2017         m_ppcResiYuvTemp[uhDepth],
2018         m_ppcResiYuvBest[uhDepth],
2019         m_ppcRecoYuvTemp[uhDepth],
2020         (uiNoResidual? true:false));
2021
2022
2023          if ( uiNoResidual == 0 && rpcTempCU->getQtRootCbf(0) == 0 )
2024         {
2025            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
2026           mergeCandBuffer[uiMergeCand] = 1;
2027         }
2028
2029          rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
2030#if MTK_SINGLE_DEPTH_MODE_I0095
2031          rpcTempCU->setSingleDepthFlagSubParts( false, 0, uhDepth );
2032#endif
2033#if H_3D_VSP // possible bug fix
2034          if( rpcTempCU->getSkipFlag(0) )
2035          {
2036            rpcTempCU->setTrIdxSubParts(0, 0, uhDepth);
2037          }
2038#endif
2039#if H_3D_INTER_SDC
2040          TComDataCU *rpcTempCUPre = rpcTempCU;
2041#endif
2042          Int orgQP = rpcTempCU->getQP( 0 );
2043          xCheckDQP( rpcTempCU );
2044          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2045#if H_3D_INTER_SDC
2046          if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() && !uiNoResidual )
2047          {
2048#if FAST_SDC_OFFSET_DECISION_I0084
2049            Double dOffsetCost[3] = {MAX_DOUBLE,MAX_DOUBLE,MAX_DOUBLE};
2050            for( Int uiOffest = 1 ; uiOffest <= 5 ; uiOffest++ )
2051#else
2052            for( Int uiOffest = -2 ; uiOffest <= 2 ; uiOffest++ )
2053#endif
2054            {
2055#if FAST_SDC_OFFSET_DECISION_I0084
2056              if( uiOffest > 3)
2057              {
2058                if ( dOffsetCost[0] < (0.9*dOffsetCost[1]) && dOffsetCost[0] < (0.9*dOffsetCost[2]) )
2059                {
2060                  continue;
2061                }
2062                if ( dOffsetCost[1] < dOffsetCost[0] && dOffsetCost[0] < dOffsetCost[2] &&  uiOffest == 5)
2063                {
2064                  continue;
2065                }
2066                if ( dOffsetCost[0] < dOffsetCost[1] && dOffsetCost[2] < dOffsetCost[0] &&  uiOffest == 4)
2067                {
2068                  continue;
2069                }
2070              }
2071#endif
2072              if( rpcTempCU != rpcTempCUPre )
2073              {
2074                rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag  );
2075                rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
2076              }
2077              rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2078#if MTK_SINGLE_DEPTH_MODE_I0095
2079              rpcTempCU->setSingleDepthFlagSubParts( false, 0, uhDepth );
2080#endif
2081              rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
2082              rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
2083#if H_3D_VSO //M2
2084              if( m_pcRdCost->getUseRenModel() )
2085              { //Reset
2086                UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
2087                UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
2088                Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
2089                UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
2090                m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2091              }
2092#endif
2093#if FAST_SDC_OFFSET_DECISION_I0084
2094              Int iSdcOffset = 0;
2095              if(uiOffest % 2 == 0)
2096              {
2097                iSdcOffset = uiOffest >> 1;
2098              }
2099              else
2100              {
2101                iSdcOffset = -1 * (uiOffest >> 1);
2102              }
2103              m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2104                m_ppcOrigYuv[uhDepth], 
2105                ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth], 
2106                m_ppcResiYuvTemp[uhDepth], 
2107                m_ppcRecoYuvTemp[uhDepth],
2108                iSdcOffset,
2109                uhDepth );
2110              if (uiOffest <= 3 )
2111              {
2112                dOffsetCost [uiOffest -1] = rpcTempCU->getTotalCost();
2113              }
2114#else
2115              m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2116                m_ppcOrigYuv[uhDepth], 
2117                ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth], 
2118                m_ppcResiYuvTemp[uhDepth], 
2119                m_ppcRecoYuvTemp[uhDepth],
2120                uiOffest,
2121                uhDepth );
2122#endif
2123
2124              xCheckDQP( rpcTempCU );
2125              xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
2126            }
2127          }
2128#endif
2129          rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag );
2130
2131      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
2132      {
2133#if H_3D_INTER_SDC
2134        if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) )
2135        {
2136          bestIsSkip = !rpcBestCU->getSDCFlag( 0 ) && ( rpcBestCU->getQtRootCbf(0) == 0 );
2137        }
2138        else
2139        {
2140#endif
2141        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
2142#if H_3D_INTER_SDC
2143        }
2144#endif
2145      }
2146    }
2147   }
2148  }
2149
2150  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
2151  {
2152    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
2153    {
2154      if( rpcBestCU->getMergeFlag( 0 ))
2155      {
2156        *earlyDetectionSkipMode = true;
2157      }
2158      else
2159      {
2160        Int absoulte_MV=0;
2161        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2162        {
2163          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
2164          {
2165            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
2166            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
2167            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
2168            absoulte_MV+=iHor+iVer;
2169          }
2170        }
2171
2172        if(absoulte_MV == 0)
2173        {
2174          *earlyDetectionSkipMode = true;
2175        }
2176      }
2177    }
2178  }
2179 }
2180#if H_3D_SPIVMP
2181 delete[] pcMvFieldSP;
2182 delete[] puhInterDirSP;
2183#endif
2184#if H_3D_ARP
2185 }
2186#endif
2187}
2188
2189
2190#if AMP_MRG
2191#if  H_3D_FAST_TEXTURE_ENCODING
2192Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bFMD, Bool bUseMRG)
2193#else
2194Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
2195#endif
2196#else
2197Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
2198#endif
2199{
2200
2201#if H_3D
2202  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
2203#endif
2204#if  H_3D_FAST_TEXTURE_ENCODING
2205  if(!(bFMD && (ePartSize == SIZE_2Nx2N)))  //have  motion estimation or merge check
2206  {
2207#endif
2208  UChar uhDepth = rpcTempCU->getDepth( 0 );
2209#if H_3D_ARP
2210  Int iLayerId    = rpcTempCU->getSlice()->getLayerId();
2211  Bool bFirstTime = true;
2212  Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
2213
2214  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || !rpcTempCU->getDvInfo(0).bDV || rpcTempCU->getICFlag(0) )
2215  {
2216    nARPWMax = 0;
2217  }
2218
2219  for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
2220  {
2221    if( bFirstTime == false && rpcTempCU->getSlice()->getVPS()->getUseAdvRP( iLayerId ) )
2222    {
2223      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0),bTransquantBypassFlag );     
2224    }
2225#endif
2226#if H_3D_VSO // M3
2227  if( m_pcRdCost->getUseRenModel() )
2228  {
2229    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2230    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2231    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2232    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2233    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2234  }
2235#endif
2236
2237  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2238 
2239  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2240#if MTK_SINGLE_DEPTH_MODE_I0095
2241  rpcTempCU->setSingleDepthFlagSubParts( false, 0, uhDepth );
2242#endif
2243  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
2244  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2245#if H_3D_DDD
2246  rpcTempCU->setUseDDD( false, 0, uhDepth );
2247#endif
2248
2249#if H_3D_ARP
2250  rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2251#endif
2252
2253#if H_3D_ARP
2254  if( bFirstTime == false && nARPWMax )
2255  {
2256    rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
2257    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2258
2259    m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
2260  }
2261  else
2262  {
2263    bFirstTime = false;
2264#endif
2265#if AMP_MRG
2266  rpcTempCU->setMergeAMP (true);
2267#if  H_3D_FAST_TEXTURE_ENCODING
2268  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bFMD, false, bUseMRG );
2269#else
2270  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
2271#endif
2272#else 
2273  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
2274#endif
2275#if H_3D_ARP
2276   if( nARPWMax )
2277   {
2278     m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
2279   }
2280  }
2281#endif
2282
2283#if AMP_MRG
2284  if ( !rpcTempCU->getMergeAMP() )
2285  {
2286#if H_3D_ARP
2287    if( nARPWMax )
2288    {
2289      continue;
2290    }
2291    else
2292#endif
2293    return;
2294  }
2295#endif
2296
2297#if KWU_RC_MADPRED_E0227
2298  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2299  {
2300    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2301      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2302      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2303    m_temporalSAD = (Int)SAD;
2304  }
2305#endif
2306  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
2307#if H_3D_VSP // possible bug fix
2308  if( rpcTempCU->getQtRootCbf(0)==0 )
2309  {
2310    rpcTempCU->setTrIdxSubParts(0, 0, uhDepth);
2311  }
2312#endif
2313
2314#if H_3D_VSO // M4
2315  if( m_pcRdCost->getUseLambdaScaleVSO() )
2316    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2317  else
2318#endif
2319
2320  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2321#if H_3D_INTER_SDC
2322  TComDataCU *rpcTempCUPre = rpcTempCU;
2323#endif
2324  xCheckDQP( rpcTempCU );
2325  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2326#if H_3D_INTER_SDC
2327  if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() && ePartSize == SIZE_2Nx2N)
2328  {
2329#if FAST_SDC_OFFSET_DECISION_I0084
2330    Double dOffsetCost[3] = {MAX_DOUBLE,MAX_DOUBLE,MAX_DOUBLE};
2331    for( Int uiOffest = 1 ; uiOffest <= 5 ; uiOffest++ )
2332#else
2333    for( Int uiOffest = -2 ; uiOffest <= 2 ; uiOffest++ )
2334#endif
2335    {
2336#if FAST_SDC_OFFSET_DECISION_I0084
2337      if( uiOffest > 3)
2338      {
2339        if ( dOffsetCost[0] < (0.9*dOffsetCost[1]) && dOffsetCost[0] < (0.9*dOffsetCost[2]) )
2340        {
2341          continue;
2342        }
2343        if ( dOffsetCost[1] < dOffsetCost[0] && dOffsetCost[0] < dOffsetCost[2] &&  uiOffest == 5)
2344        {
2345          continue;
2346        }
2347        if ( dOffsetCost[0] < dOffsetCost[1] && dOffsetCost[2] < dOffsetCost[0] &&  uiOffest == 4)
2348        {
2349          continue;
2350        }
2351      }
2352#endif
2353      if( rpcTempCU != rpcTempCUPre )
2354      {
2355        Int orgQP = rpcBestCU->getQP( 0 );
2356        rpcTempCU->initEstData( uhDepth, orgQP ,bTransquantBypassFlag );     
2357        rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
2358      }
2359      rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2360#if MTK_SINGLE_DEPTH_MODE_I0095
2361      rpcTempCU->setSingleDepthFlagSubParts( false, 0, uhDepth );
2362#endif
2363      rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
2364      rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
2365#if H_3D_VSO // M3
2366      if( m_pcRdCost->getUseRenModel() )
2367      {
2368        UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2369        UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2370        Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2371        UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2372        m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2373      }
2374#endif
2375
2376#if FAST_SDC_OFFSET_DECISION_I0084
2377      Int iSdcOffset = 0;
2378      if(uiOffest % 2 == 0)
2379      {
2380        iSdcOffset = uiOffest >> 1;
2381      }
2382      else
2383      {
2384        iSdcOffset = -1 * (uiOffest >> 1);
2385      }
2386      m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2387        m_ppcOrigYuv[uhDepth],
2388        ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth],
2389        m_ppcResiYuvTemp[uhDepth],
2390        m_ppcRecoYuvTemp[uhDepth],
2391        iSdcOffset,
2392        uhDepth );
2393      if (uiOffest <= 3 )
2394      {
2395        dOffsetCost [uiOffest -1] = rpcTempCU->getTotalCost();
2396      }
2397#else
2398      m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2399        m_ppcOrigYuv[uhDepth],
2400        ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth],
2401        m_ppcResiYuvTemp[uhDepth],
2402        m_ppcRecoYuvTemp[uhDepth],
2403        uiOffest,
2404        uhDepth );
2405#endif
2406
2407      xCheckDQP( rpcTempCU );
2408      xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2409    }
2410
2411  }
2412#endif
2413#if H_3D_ARP
2414  }
2415#endif
2416#if  H_3D_FAST_TEXTURE_ENCODING
2417  }
2418#endif
2419}
2420
2421#if H_3D_DBBP
2422Void TEncCu::xInvalidateOriginalSegments( TComYuv* pOrigYuv, TComYuv* pOrigYuvTemp, Bool* pMask, UInt uiValidSegment )
2423{
2424  UInt  uiWidth     = pOrigYuv->getWidth ( );
2425  UInt  uiHeight    = pOrigYuv->getHeight( );
2426  Pel*  piSrc       = pOrigYuv->getLumaAddr( );
2427  UInt  uiSrcStride = pOrigYuv->getStride();
2428  Pel*  piDst       = pOrigYuvTemp->getLumaAddr( );
2429  UInt  uiDstStride = pOrigYuvTemp->getStride();
2430 
2431  UInt  uiMaskStride= MAX_CU_SIZE;
2432 
2433  AOF( uiWidth == uiHeight );
2434 
2435  // backup pointer
2436  Bool* pMaskStart = pMask;
2437 
2438  for (Int y=0; y<uiHeight; y++)
2439  {
2440    for (Int x=0; x<uiWidth; x++)
2441    {
2442      UChar ucSegment = (UChar)pMask[x];
2443      AOF( ucSegment < 2 );
2444     
2445      piDst[x] = (ucSegment==uiValidSegment)?piSrc[x]:DBBP_INVALID_SHORT;
2446    }
2447   
2448    piSrc  += uiSrcStride;
2449    piDst  += uiDstStride;
2450    pMask  += uiMaskStride;
2451  }
2452 
2453  // now invalidate chroma
2454  Pel*  piSrcU       = pOrigYuv->getCbAddr();
2455  Pel*  piSrcV       = pOrigYuv->getCrAddr();
2456  UInt  uiSrcStrideC = pOrigYuv->getCStride();
2457  Pel*  piDstU       = pOrigYuvTemp->getCbAddr( );
2458  Pel*  piDstV       = pOrigYuvTemp->getCrAddr( );
2459  UInt  uiDstStrideC = pOrigYuvTemp->getCStride();
2460  pMask = pMaskStart;
2461 
2462  for (Int y=0; y<uiHeight/2; y++)
2463  {
2464    for (Int x=0; x<uiWidth/2; x++)
2465    {
2466      UChar ucSegment = (UChar)pMask[x*2];
2467      AOF( ucSegment < 2 );
2468     
2469      piDstU[x] = (ucSegment==uiValidSegment)?piSrcU[x]:DBBP_INVALID_SHORT;
2470      piDstV[x] = (ucSegment==uiValidSegment)?piSrcV[x]:DBBP_INVALID_SHORT;
2471    }
2472   
2473    piSrcU  += uiSrcStrideC;
2474    piSrcV  += uiSrcStrideC;
2475    piDstU  += uiDstStrideC;
2476    piDstV  += uiDstStrideC;
2477    pMask   += 2*uiMaskStride;
2478  }
2479}
2480#if MTK_SINGLE_DEPTH_MODE_I0095
2481Void TEncCu::xCheckRDCostSingleDepth( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2482{
2483  UInt uiDepth = rpcTempCU->getDepth( 0 );
2484  if( !rpcBestCU->getSlice()->getIsDepth() || (eSize != SIZE_2Nx2N))
2485  {
2486    return;
2487  }
2488 
2489#if H_3D_VSO // M5
2490  if( m_pcRdCost->getUseRenModel() )
2491  {
2492    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
2493    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
2494    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
2495    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
2496    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2497  }
2498#endif
2499
2500  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2501  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2502  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2503  rpcTempCU->setCUTransquantBypassSubParts( rpcTempCU->getCUTransquantBypass(0), 0, uiDepth );
2504
2505  rpcTempCU->setTrIdxSubParts(0, 0, uiDepth);
2506  rpcTempCU->setCbfSubParts(0, 1, 1, 0, uiDepth);
2507  rpcTempCU->setSingleDepthFlagSubParts(true, 0, uiDepth);
2508  rpcTempCU->setLumaIntraDirSubParts (DC_IDX, 0, uiDepth);
2509#if H_3D_DIM_SDC
2510  rpcTempCU->setSDCFlagSubParts( false, 0, uiDepth);
2511#endif
2512
2513  UInt uiPreCalcDistC;
2514  m_pcPredSearch  ->estIntraPredSingleDepth      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, false );
2515
2516
2517  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
2518 
2519 
2520  m_pcEntropyCoder->resetBits();
2521  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2522  {
2523    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2524  }
2525  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2526  m_pcEntropyCoder->encodeSingleDepthMode( rpcTempCU, 0,          true );
2527 
2528
2529  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2530 
2531  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2532  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2533
2534
2535#if H_3D_VSO // M6
2536  if( m_pcRdCost->getUseLambdaScaleVSO()) 
2537    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
2538  else
2539#endif
2540  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2541 
2542
2543  xCheckDQP( rpcTempCU );
2544  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2545}
2546#endif
2547Void TEncCu::xCheckRDCostInterDBBP( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool bUseMRG )
2548{
2549  AOF( !rpcTempCU->getSlice()->getIsDepth() );
2550 
2551  UChar uhDepth = rpcTempCU->getDepth( 0 );
2552 
2553#if H_3D_VSO
2554  if( m_pcRdCost->getUseRenModel() )
2555  {
2556    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2557    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2558    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2559    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2560    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2561  }
2562#endif
2563 
2564  UInt uiWidth  = rpcTempCU->getWidth(0);
2565  UInt uiHeight = rpcTempCU->getHeight(0);
2566  AOF( uiWidth == uiHeight );
2567 
2568#if SEC_DBBP_DISALLOW_8x8_I0078
2569  if(uiWidth <= 8)
2570  {
2571    return;
2572  }
2573#endif
2574 
2575  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N,  0, uhDepth );
2576 
2577  // fetch virtual depth block
2578  UInt uiDepthStride = 0;
2579  Pel* pDepthPels = rpcTempCU->getVirtualDepthBlock(0, uiWidth, uiHeight, uiDepthStride);
2580  AOF( pDepthPels != NULL );
2581  AOF( uiDepthStride != 0 );
2582 
2583  // derive partitioning from depth
2584  PartSize eVirtualPartSize = m_pcPredSearch->getPartitionSizeFromDepth(pDepthPels, uiDepthStride, uiWidth);
2585 
2586  // derive segmentation mask from depth
2587  Bool pMask[MAX_CU_SIZE*MAX_CU_SIZE];
2588  Bool bValidMask = m_pcPredSearch->getSegmentMaskFromDepth(pDepthPels, uiDepthStride, uiWidth, uiHeight, pMask);
2589 
2590  if( !bValidMask )
2591  {
2592    return;
2593  }
2594 
2595  // find optimal motion/disparity vector for each segment
2596  DisInfo originalDvInfo = rpcTempCU->getDvInfo(0);
2597  DBBPTmpData* pDBBPTmpData = rpcTempCU->getDBBPTmpData();
2598  TComYuv* apPredYuv[2] = { m_ppcRecoYuvTemp[uhDepth], m_ppcPredYuvTemp[uhDepth] };
2599 
2600  // find optimal motion vector fields for both segments (as 2Nx2N)
2601  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2602  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N,  0, uhDepth );
2603  rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth );
2604  for( UInt uiSegment = 0; uiSegment < 2; uiSegment++ )
2605  {
2606    rpcTempCU->setDBBPFlagSubParts(true, 0, 0, uhDepth);
2607    rpcTempCU->setDvInfoSubParts(originalDvInfo, 0, uhDepth);
2608   
2609    // invalidate all other segments in original YUV
2610    xInvalidateOriginalSegments(m_ppcOrigYuv[uhDepth], m_ppcOrigYuvDBBP[uhDepth], pMask, uiSegment);
2611   
2612    // do motion estimation for this segment
2613    m_pcRdCost->setUseMask(true);
2614    rpcTempCU->getDBBPTmpData()->eVirtualPartSize = eVirtualPartSize;
2615    rpcTempCU->getDBBPTmpData()->uiVirtualPartIndex = uiSegment;
2616    m_pcPredSearch->predInterSearch( rpcTempCU, m_ppcOrigYuvDBBP[uhDepth], apPredYuv[uiSegment], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], false, false, bUseMRG );
2617    m_pcRdCost->setUseMask(false);
2618   
2619    // extract motion parameters of full block for this segment
2620    pDBBPTmpData->auhInterDir[uiSegment] = rpcTempCU->getInterDir(0);
2621   
2622    pDBBPTmpData->abMergeFlag[uiSegment] = rpcTempCU->getMergeFlag(0);
2623    pDBBPTmpData->auhMergeIndex[uiSegment] = rpcTempCU->getMergeIndex(0);
2624   
2625    AOF( rpcTempCU->getSPIVMPFlag(0) == false );
2626    AOF( rpcTempCU->getVSPFlag(0) == 0 );
2627   
2628    for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2629    {
2630      RefPicList eRefList = (RefPicList)uiRefListIdx;
2631     
2632      pDBBPTmpData->acMvd[uiSegment][eRefList] = rpcTempCU->getCUMvField(eRefList)->getMvd(0);
2633      pDBBPTmpData->aiMvpNum[uiSegment][eRefList] = rpcTempCU->getMVPNum(eRefList, 0);
2634      pDBBPTmpData->aiMvpIdx[uiSegment][eRefList] = rpcTempCU->getMVPIdx(eRefList, 0);
2635     
2636      rpcTempCU->getMvField(rpcTempCU, 0, eRefList, pDBBPTmpData->acMvField[uiSegment][eRefList]);
2637    }
2638  }
2639 
2640  // store final motion/disparity information in each PU using derived partitioning
2641  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2642  rpcTempCU->setPartSizeSubParts  ( eVirtualPartSize,  0, uhDepth );
2643  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2644 
2645  UInt uiPUOffset = ( g_auiPUOffset[UInt( eVirtualPartSize )] << ( ( rpcTempCU->getSlice()->getSPS()->getMaxCUDepth() - uhDepth ) << 1 ) ) >> 4;
2646  for( UInt uiSegment = 0; uiSegment < 2; uiSegment++ )
2647  {
2648    UInt uiPartAddr = uiSegment*uiPUOffset;
2649   
2650    rpcTempCU->setDBBPFlagSubParts(true, uiPartAddr, uiSegment, uhDepth);
2651   
2652    // now set stored information from 2Nx2N motion search to each partition
2653    rpcTempCU->setInterDirSubParts(pDBBPTmpData->auhInterDir[uiSegment], uiPartAddr, uiSegment, uhDepth); // interprets depth relative to LCU level
2654   
2655    rpcTempCU->setMergeFlagSubParts(pDBBPTmpData->abMergeFlag[uiSegment], uiPartAddr, uiSegment, uhDepth);
2656    rpcTempCU->setMergeIndexSubParts(pDBBPTmpData->auhMergeIndex[uiSegment], uiPartAddr, uiSegment, uhDepth);
2657       
2658    for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2659    {
2660      RefPicList eRefList = (RefPicList)uiRefListIdx;
2661     
2662      rpcTempCU->getCUMvField( eRefList )->setAllMvd(pDBBPTmpData->acMvd[uiSegment][eRefList], eVirtualPartSize, uiPartAddr, 0, uiSegment);
2663      rpcTempCU->setMVPNum(eRefList, uiPartAddr, pDBBPTmpData->aiMvpNum[uiSegment][eRefList]);
2664      rpcTempCU->setMVPIdx(eRefList, uiPartAddr, pDBBPTmpData->aiMvpIdx[uiSegment][eRefList]);
2665     
2666      rpcTempCU->getCUMvField( eRefList )->setAllMvField( pDBBPTmpData->acMvField[uiSegment][eRefList], eVirtualPartSize, uiPartAddr, 0, uiSegment ); // interprets depth relative to rpcTempCU level
2667    }
2668  }
2669 
2670  // reconstruct final prediction signal by combining both segments
2671#if SHARP_DBBP_SIMPLE_FLTER_I0109
2672  m_pcPredSearch->combineSegmentsWithMask(apPredYuv, m_ppcPredYuvTemp[uhDepth], pMask, uiWidth, uiHeight, 0, eVirtualPartSize);
2673#else
2674  m_pcPredSearch->combineSegmentsWithMask(apPredYuv, m_ppcPredYuvTemp[uhDepth], pMask, uiWidth, uiHeight);
2675#endif
2676 
2677  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
2678 
2679  xCheckDQP( rpcTempCU );
2680  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2681}
2682#endif
2683
2684#if HHI_DMM4_ENC_I0066
2685Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize, Bool bOnlyIVP )
2686#else
2687Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2688#endif
2689{
2690  UInt uiDepth = rpcTempCU->getDepth( 0 );
2691 
2692#if H_3D_VSO // M5
2693  if( m_pcRdCost->getUseRenModel() )
2694  {
2695    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
2696    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
2697    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
2698    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
2699    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2700  }
2701#endif
2702
2703  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2704#if MTK_SINGLE_DEPTH_MODE_I0095
2705  rpcTempCU->setSingleDepthFlagSubParts( false, 0, uiDepth );
2706#endif
2707  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
2708  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2709 
2710  Bool bSeparateLumaChroma = true; // choose estimation mode
2711  UInt uiPreCalcDistC      = 0;
2712  if( !bSeparateLumaChroma )
2713  {
2714    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
2715  }
2716#if HHI_DMM4_ENC_I0066
2717  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma, bOnlyIVP );
2718#else
2719  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
2720#endif
2721  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
2722 
2723#if H_3D_DIM_SDC
2724  if( !rpcTempCU->getSDCFlag( 0 ) )
2725#endif
2726  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
2727 
2728  m_pcEntropyCoder->resetBits();
2729  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2730  {
2731    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2732  }
2733  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2734#if MTK_SINGLE_DEPTH_MODE_I0095
2735  m_pcEntropyCoder->encodeSingleDepthMode( rpcTempCU, 0,          true );
2736  if(!rpcTempCU->getSingleDepthFlag(0))
2737  {
2738#endif
2739  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
2740  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
2741#if H_3D_DIM_SDC
2742  m_pcEntropyCoder->encodeSDCFlag( rpcTempCU, 0, true );
2743#endif
2744  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
2745  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
2746
2747  // Encode Coefficients
2748  Bool bCodeDQP = getdQPFlag();
2749  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
2750  setdQPFlag( bCodeDQP );
2751#if MTK_SINGLE_DEPTH_MODE_I0095
2752  }
2753#endif       
2754  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2755 
2756  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2757    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2758#if H_3D_VSO // M6
2759  if( m_pcRdCost->getUseLambdaScaleVSO()) 
2760    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
2761  else
2762#endif
2763  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2764 
2765  xCheckDQP( rpcTempCU );
2766  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2767}
2768
2769/** Check R-D costs for a CU with PCM mode.
2770 * \param rpcBestCU pointer to best mode CU data structure
2771 * \param rpcTempCU pointer to testing mode CU data structure
2772 * \returns Void
2773 *
2774 * \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.
2775 */
2776Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
2777{
2778  UInt uiDepth = rpcTempCU->getDepth( 0 );
2779
2780  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2781#if MTK_SINGLE_DEPTH_MODE_I0095
2782  rpcTempCU->setSingleDepthFlagSubParts( false, 0, uiDepth );
2783#endif
2784  rpcTempCU->setIPCMFlag(0, true);
2785  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
2786  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2787  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2788  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
2789
2790  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
2791
2792  m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
2793
2794  m_pcEntropyCoder->resetBits();
2795  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2796  {
2797    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2798  }
2799  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2800#if MTK_SINGLE_DEPTH_MODE_I0095
2801  m_pcEntropyCoder->encodeSingleDepthMode( rpcTempCU, 0,          true );
2802#endif
2803  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
2804  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
2805#if H_3D_DIM_SDC
2806  m_pcEntropyCoder->encodeSDCFlag( rpcTempCU, 0, true );
2807#endif
2808  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
2809
2810  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2811
2812  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2813    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2814#if H_3D_VSO // M44
2815  if ( m_pcRdCost->getUseVSO() )
2816    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2817  else
2818#endif
2819  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2820
2821  xCheckDQP( rpcTempCU );
2822  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
2823}
2824
2825/** check whether current try is the best with identifying the depth of current try
2826 * \param rpcBestCU
2827 * \param rpcTempCU
2828 * \returns Void
2829 */
2830Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
2831{
2832  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
2833  {
2834    TComYuv* pcYuv;
2835    // Change Information data
2836    TComDataCU* pcCU = rpcBestCU;
2837    rpcBestCU = rpcTempCU;
2838    rpcTempCU = pcCU;
2839
2840    // Change Prediction data
2841    pcYuv = m_ppcPredYuvBest[uiDepth];
2842    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
2843    m_ppcPredYuvTemp[uiDepth] = pcYuv;
2844
2845    // Change Reconstruction data
2846    pcYuv = m_ppcRecoYuvBest[uiDepth];
2847    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
2848    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
2849
2850    pcYuv = NULL;
2851    pcCU  = NULL;
2852
2853    // store temp best CI for next CU coding
2854      m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
2855  }
2856}
2857
2858Void TEncCu::xCheckDQP( TComDataCU* pcCU )
2859{
2860  UInt uiDepth = pcCU->getDepth( 0 );
2861
2862  if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() )
2863  {
2864    if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) )
2865    {
2866#if !RDO_WITHOUT_DQP_BITS
2867      m_pcEntropyCoder->resetBits();
2868      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
2869      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
2870        pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2871#if H_3D_VSO // M45
2872      if ( m_pcRdCost->getUseVSO() )     
2873        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
2874      else
2875#endif
2876      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
2877#endif
2878    }
2879    else
2880    {
2881      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
2882    }
2883  }
2884}
2885
2886Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
2887{
2888  pDst->iN = pSrc->iN;
2889  for (Int i = 0; i < pSrc->iN; i++)
2890  {
2891    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
2892  }
2893}
2894Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY )
2895{
2896  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
2897  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
2898  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
2899  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2900    pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2901  Bool bSliceEnd   = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2902    pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2903  if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2904  {
2905    UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
2906    UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth);
2907    UInt uiBlkWidth    = rpcPic->getNumPartInWidth() >> (uiDepth);
2908    UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2909    UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2910    UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
2911    m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
2912  }
2913  else
2914  {
2915    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2;
2916
2917    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
2918    {
2919      UInt uiSubCULPelX   = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx &  1 );
2920      UInt uiSubCUTPelY   = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 );
2921
2922      Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && 
2923        rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr();
2924      if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2925      {
2926        xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY );   // Copy Yuv data to picture Yuv
2927      }
2928    }
2929  }
2930}
2931
2932Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
2933{
2934  UInt uiCurrDepth = uiNextDepth - 1;
2935  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
2936}
2937
2938/** Function for filling the PCM buffer of a CU using its original sample array
2939 * \param pcCU pointer to current CU
2940 * \param pcOrgYuv pointer to original sample array
2941 * \returns Void
2942 */
2943Void TEncCu::xFillPCMBuffer     ( TComDataCU*& pCU, TComYuv* pOrgYuv )
2944{
2945
2946  UInt   width        = pCU->getWidth(0);
2947  UInt   height       = pCU->getHeight(0);
2948
2949  Pel*   pSrcY = pOrgYuv->getLumaAddr(0, width); 
2950  Pel*   pDstY = pCU->getPCMSampleY();
2951  UInt   srcStride = pOrgYuv->getStride();
2952
2953  for(Int y = 0; y < height; y++ )
2954  {
2955    for(Int x = 0; x < width; x++ )
2956    {
2957      pDstY[x] = pSrcY[x];
2958    }
2959    pDstY += width;
2960    pSrcY += srcStride;
2961  }
2962
2963  Pel* pSrcCb       = pOrgYuv->getCbAddr();
2964  Pel* pSrcCr       = pOrgYuv->getCrAddr();;
2965
2966  Pel* pDstCb       = pCU->getPCMSampleCb();
2967  Pel* pDstCr       = pCU->getPCMSampleCr();;
2968
2969  UInt srcStrideC = pOrgYuv->getCStride();
2970  UInt heightC   = height >> 1;
2971  UInt widthC    = width  >> 1;
2972
2973  for(Int y = 0; y < heightC; y++ )
2974  {
2975    for(Int x = 0; x < widthC; x++ )
2976    {
2977      pDstCb[x] = pSrcCb[x];
2978      pDstCr[x] = pSrcCr[x];
2979    }
2980    pDstCb += widthC;
2981    pDstCr += widthC;
2982    pSrcCb += srcStrideC;
2983    pSrcCr += srcStrideC;
2984  }
2985}
2986
2987#if ADAPTIVE_QP_SELECTION
2988/** Collect ARL statistics from one block
2989  */
2990Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
2991{
2992  for( Int n = 0; n < NumCoeffInCU; n++ )
2993  {
2994    Int u = abs( rpcCoeff[ n ] );
2995    Int absc = rpcArlCoeff[ n ];
2996
2997    if( u != 0 )
2998    {
2999      if( u < LEVEL_RANGE )
3000      {
3001        cSum[ u ] += ( Double )absc;
3002        numSamples[ u ]++;
3003      }
3004      else 
3005      {
3006        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
3007        numSamples[ LEVEL_RANGE ]++;
3008      }
3009    }
3010  }
3011
3012  return 0;
3013}
3014
3015/** Collect ARL statistics from one LCU
3016 * \param pcCU
3017 */
3018Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU )
3019{
3020  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to datatype and quantization output
3021  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output
3022
3023  TCoeff* pCoeffY = rpcCU->getCoeffY();
3024  Int* pArlCoeffY = rpcCU->getArlCoeffY();
3025
3026  UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth;
3027  UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;
3028
3029  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
3030  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
3031
3032  // Collect stats to cSum[][] and numSamples[][]
3033  for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ )
3034  {
3035    UInt uiTrIdx = rpcCU->getTransformIdx(i);
3036
3037    if(rpcCU->getPredictionMode(i) == MODE_INTER)
3038    if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) )
3039    {
3040      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
3041    }//Note that only InterY is processed. QP rounding is based on InterY data only.
3042   
3043    pCoeffY  += uiMinNumCoeffInCU;
3044    pArlCoeffY  += uiMinNumCoeffInCU;
3045  }
3046
3047  for(Int u=1; u<LEVEL_RANGE;u++)
3048  {
3049    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
3050    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
3051  }
3052  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
3053  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
3054}
3055#endif
3056//! \}
Note: See TracBrowser for help on using the repository browser.