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

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

Further fixes.

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