source: 3DVCSoftware/branches/HTM-13.1-dev1-Samsung/source/Lib/TLibEncoder/TEncCu.cpp @ 1147

Last change on this file since 1147 was 1147, checked in by samsung-htm, 9 years ago

Integration of K0035: Removal of encoder restriction of ARP

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