source: 3DVCSoftware/branches/HTM-10.0-dev0/source/Lib/TLibEncoder/TEncCu.cpp @ 852

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

Update HM-12.0 -> HM-13.0.

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