source: 3DVCSoftware/branches/HTM-12.2-dev2-HHI/source/Lib/TLibEncoder/TEncCu.cpp @ 1106

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