source: 3DVCSoftware/branches/HTM-9.3-dev1-RWTH/source/Lib/TLibEncoder/TEncCu.cpp @ 819

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