source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncCu.cpp @ 833

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

Merged 9.3-dev0@831.

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