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

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

Alignment of J0060 and J0107 (ALIGN_J0060_J0107)

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