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

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

Fixed linux compiler warnings.

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