source: 3DVCSoftware/branches/HTM-12.2-dev0/source/Lib/TLibEncoder/TEncCu.cpp @ 1120

Last change on this file since 1120 was 1120, checked in by tech, 9 years ago

Preliminary disabled getSingleDepthModeFlag

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