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
RevLine 
[5]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
[56]4 * granted under this license. 
[5]5 *
[837]6* Copyright (c) 2010-2014, ITU/ISO/IEC
[5]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.
[56]17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
[5]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 */
[2]33
[56]34/** \file     TEncCu.cpp
35    \brief    Coding Unit (CU) encoder class
[2]36*/
37
38#include <stdio.h>
39#include "TEncTop.h"
40#include "TEncCu.h"
41#include "TEncAnalyze.h"
42
[56]43#include <cmath>
44#include <algorithm>
45using namespace std;
46
47//! \ingroup TLibEncoder
48//! \{
49
[2]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;
[56]62 
[2]63  m_uhTotalDepth   = uhTotalDepth + 1;
64  m_ppcBestCU      = new TComDataCU*[m_uhTotalDepth-1];
65  m_ppcTempCU      = new TComDataCU*[m_uhTotalDepth-1];
[608]66   
67#if H_3D_ARP
[443]68  m_ppcWeightedTempCU = new TComDataCU*[m_uhTotalDepth-1];
69#endif
[608]70
[2]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];
[833]78#if H_3D_DBBP
79  m_ppcOrigYuvDBBP = new TComYuv*[m_uhTotalDepth-1];
80#endif
[56]81 
[2]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;
[56]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) );
[608]91   
92#if H_3D_ARP
[443]93    m_ppcWeightedTempCU[i] = new TComDataCU; m_ppcWeightedTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
[608]94#endif 
95
[2]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);
[56]99   
[2]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);
[56]103   
[2]104    m_ppcOrigYuv    [i] = new TComYuv; m_ppcOrigYuv    [i]->create(uiWidth, uiHeight);
[833]105#if H_3D_DBBP
106    m_ppcOrigYuvDBBP[i] = new TComYuv; m_ppcOrigYuvDBBP[i]->create(uiWidth, uiHeight);
107#endif
[2]108  }
[56]109 
110  m_bEncodeDQP = false;
[837]111#if KWU_RC_MADPRED_E0227
[608]112  m_LCUPredictionSAD = 0;
113  m_addSADDepth      = 0;
114  m_temporalSAD      = 0;
[655]115  m_spatialSAD       = 0;
116#endif
[2]117
118  // initialize partition order.
119  UInt* piTmp = &g_auiZscanToRaster[0];
120  initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp);
121  initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
[56]122 
[2]123  // initialize conversion matrix from partition index to pel
124  initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
125}
126
127Void TEncCu::destroy()
128{
129  Int i;
[56]130 
[2]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    }
[608]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
[2]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    }
[833]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
[2]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  }
[608]192
193#if H_3D_ARP
194  if(m_ppcWeightedTempCU)
195  {
196    delete [] m_ppcWeightedTempCU; 
197    m_ppcWeightedTempCU = NULL; 
198  }
199#endif
[2]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  }
[833]235#if H_3D_DBBP
236  if(m_ppcOrigYuvDBBP)
237  {
238    delete [] m_ppcOrigYuvDBBP;
239    m_ppcOrigYuvDBBP = NULL;
240  }
241#endif
[2]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();
[56]253 
[2]254  m_pcEntropyCoder     = pcEncTop->getEntropyCoder();
255  m_pcCavlcCoder       = pcEncTop->getCavlcCoder();
256  m_pcSbacCoder       = pcEncTop->getSbacCoder();
257  m_pcBinCABAC         = pcEncTop->getBinCABAC();
[56]258 
[2]259  m_pppcRDSbacCoder   = pcEncTop->getRDSbacCoder();
260  m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder();
[56]261 
[608]262  m_pcRateCtrl        = pcEncTop->getRateCtrl();
[2]263}
264
265// ====================================================================================================================
266// Public member functions
267// ====================================================================================================================
268
269/** \param  rpcCU pointer of CU data class
270 */
271Void TEncCu::compressCU( TComDataCU*& rpcCU )
272{
[56]273  // initialize CU data
274  m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
275  m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
276
[655]277#if KWU_RC_MADPRED_E0227
278  m_LCUPredictionSAD = 0;
279  m_addSADDepth      = 0;
280  m_temporalSAD      = 0;
281  m_spatialSAD       = 0;
282#endif
[608]283
[56]284  // analysis of CU
285  xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
286
287#if ADAPTIVE_QP_SELECTION
288  if( m_pcEncCfg->getUseAdaptQpSelect() )
[2]289  {
[56]290    if(rpcCU->getSlice()->getSliceType()!=I_SLICE) //IIII
291    {
292      xLcuCollectARLStats( rpcCU);
293    }
[2]294  }
[56]295#endif
296}
[608]297/** \param  pcCU  pointer of CU data class
[56]298 */
[608]299Void TEncCu::encodeCU ( TComDataCU* pcCU )
[56]300{
301  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
[2]302  {
[56]303    setdQPFlag(true);
304  }
[2]305
[56]306  // Encode CU data
307  xEncodeCU( pcCU, 0, 0 );
[2]308}
309
[56]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
[2]328{
[56]329  if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
[2]330  {
[56]331    bTestAMP_Hor = true;
[2]332  }
[56]333  else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
[2]334  {
[56]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  }
[2]342
[56]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;
[2]349  }
[56]350
351  if ( eParentPartSize == SIZE_NONE ) //! if parent is intra
[2]352  {
[56]353    if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
[2]354    {
[56]355      bTestMergeAMP_Hor = true;
[2]356    }
[56]357    else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
[2]358    {
[56]359      bTestMergeAMP_Ver = true;
[2]360    }
361  }
362
[56]363  if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->isSkipped(0) == false )
364  {
365    bTestMergeAMP_Hor = true;         
366    bTestMergeAMP_Ver = true;         
367  }
[2]368
[56]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  }
[2]381
[56]382  if ( eParentPartSize == SIZE_2Nx2N )
383  { 
384    bTestAMP_Hor = false;
385    bTestAMP_Ver = false;
386  }     
387#endif
[2]388}
[56]389#endif
[2]390
391// ====================================================================================================================
392// Protected member functions
393// ====================================================================================================================
[56]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
[2]405Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
[56]406#endif
[2]407{
408  TComPic* pcPic = rpcBestCU->getPic();
409
[608]410#if H_3D_QTLPC
411  TComSPS *sps            = pcPic->getSlice(0)->getSPS();
412  TComPic *pcTexture      = rpcBestCU->getSlice()->getTexturePic();
[189]413
414  Bool  depthMapDetect    = (pcTexture != NULL);
415  Bool  bIntraSliceDetect = (rpcBestCU->getSlice()->getSliceType() == I_SLICE);
416
[608]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);
[296]418
[608]419  Bool bTry2NxN           = true;
420  Bool bTryNx2N           = true;
[115]421#endif
[2]422  // get Original YUV data from picture
423  m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
424
[655]425#if H_3D_QTLPC 
426  Bool    bTrySplit     = true;
427  Bool    bTrySplitDQP  = true;
428#endif
[2]429
[56]430  // variable for Early CU determination
431  Bool    bSubBranch = true;
432
433  // variable for Cbf fast mode PU decision
434  Bool    doNotBlockPu = true;
[608]435  Bool earlyDetectionSkipMode = false;
[56]436
[622]437#if H_3D_VSP
[608]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
[2]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
[56]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() )
[2]458  {
[56]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
[608]469  if ( m_pcEncCfg->getUseRateCtrl() )
470  {
471    iMinQP = m_pcRateCtrl->getRCQP();
472    iMaxQP = m_pcRateCtrl->getRCQP();
473  }
[837]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()) )
[608]479  {
[837]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    }
[608]486  }
[837]487
[608]488#if H_3D_IC
[724]489  Bool bICEnabled = rpcTempCU->getSlice()->getViewIndex() && ( rpcTempCU->getSlice()->getSliceType() == P_SLICE || rpcTempCU->getSlice()->getSliceType() == B_SLICE ) && !rpcTempCU->getSlice()->getIsDepth();
[608]490  bICEnabled = bICEnabled && rpcTempCU->getSlice()->getApplyIC();
491#endif
[56]492  // If slice start or slice end is within this cu...
493  TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx());
[608]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());
[56]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  {
[655]500#if  H_3D_FAST_TEXTURE_ENCODING
[608]501    Bool bIVFMerge = false;
502    Int  iIVFMaxD = 0;
503    Bool bFMD = false;
504#endif
[56]505    for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
506    {
[837]507      const Bool bIsLosslessMode = isAddLowestQP && (iQP == iMinQP);
508
509      if (bIsLosslessMode)
[2]510      {
[56]511        iQP = lowestQP;
[2]512      }
[837]513
[655]514#if H_3D_QTLPC
[608]515      bTrySplit    = true;
[655]516#endif
[2]517
[837]518      rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[608]519#if H_3D_QTLPC
[116]520      //logic for setting bTrySplit using the partition information that is stored of the texture colocated CU
[296]521
[608]522      if(depthMapDetect && !bIntraSliceDetect && !rapPic && sps->getUseQTL())
[116]523      {
[189]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.
[116]528        {
529          bTrySplit = true;
[189]530          bTryNx2N  = true;
531          bTry2NxN  = true;
[116]532        }
[189]533        else
[116]534        {
535          bTrySplit = false;
[189]536          bTryNx2N  = false;
537          bTry2NxN  = false;
[833]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
[116]547        }
548      }
[115]549#endif
[608]550
551#if H_3D_NBDV
[443]552      if( rpcTempCU->getSlice()->getSliceType() != I_SLICE )
553      {
[608]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()) )
[443]559#else
[608]560#if H_3D_IV_MERGE
561        if( rpcTempCU->getSlice()->getVPS()->getIvMvPredFlag(rpcTempCU->getSlice()->getLayerId()) )
562#else
563        if (0)
[443]564#endif
[608]565#endif
566#endif
567        {
[443]568          PartSize ePartTemp = rpcTempCU->getPartitionSize(0);
569          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );     
[773]570#if H_3D_IV_MERGE
[724]571          if (rpcTempCU->getSlice()->getIsDepth() )
572          {
[833]573#if SEC_DEPTH_DV_DERIVAITON_G0074
574            DvInfo.bDV = rpcTempCU->getDispforDepth(0, 0, &DvInfo);
575#else
[724]576            DvInfo.bDV = rpcTempCU->getDispNeighBlocks(0, 0, &DvInfo);
[833]577#endif
[724]578          }
579          else
580          {
581#endif
[608]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
[773]589#if H_3D_IV_MERGE
[724]590          }
591#endif
[443]592          rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
593          rpcBestCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
594          rpcTempCU->setPartSizeSubParts( ePartTemp, 0, uiDepth );
595        }
[608]596      }
[655]597#if  H_3D_FAST_TEXTURE_ENCODING
[608]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      }
[443]605#endif
606#endif
[56]607      // do inter modes, SKIP and 2Nx2N
608      if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
[2]609      {
[608]610#if H_3D_IC
611        for( UInt uiICId = 0; uiICId < ( bICEnabled ? 2 : 1 ); uiICId++ )
[443]612        {
[608]613          Bool bICFlag = uiICId ? true : false;
[443]614#endif
[608]615        // 2Nx2N
616        if(m_pcEncCfg->getUseEarlySkipDetection())
[56]617        {
[608]618#if H_3D_IC
619          rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
[56]620#endif
[655]621#if  H_3D_FAST_TEXTURE_ENCODING
[837]622          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFMD );  rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode  );//by Competition for inter_2Nx2N
[608]623#else
[838]624          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
625          rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );//by Competition for inter_2Nx2N
[189]626#endif
[622]627#if H_3D_VSP
[608]628          rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
[5]629#endif
[608]630        }
631        // SKIP
632#if H_3D_IC
633        rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
[2]634#endif
[608]635        xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode );//by Merge for inter_2Nx2N
[655]636#if  H_3D_FAST_TEXTURE_ENCODING
[608]637        bFMD = bIVFMerge && rpcBestCU->isSkipped(0);
638#endif
[837]639        rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]640#if H_3D_VSP
[608]641        rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
642#endif
643
644        if(!m_pcEncCfg->getUseEarlySkipDetection())
645        {
[116]646          // 2Nx2N, NxN
[608]647#if H_3D_IC
[296]648            rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
649#endif
[655]650#if  H_3D_FAST_TEXTURE_ENCODING
[837]651            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFMD );  rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[2]652#else
[838]653          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
654          rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[2]655#endif
[622]656#if H_3D_VSP
[608]657            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
658#endif
[833]659         
660#if H_3D_DBBP
661          if( m_pcEncCfg->getUseDBBP() )
662          {
663            xCheckRDCostInterDBBP( rpcBestCU, rpcTempCU, false );
[837]664            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode  );
[833]665#if H_3D_VSP
666            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
667#endif
668          }
669#endif
670         
[116]671            if(m_pcEncCfg->getUseCbfFastMode())
672            {
673              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
674            }
[608]675        }
676#if H_3D_IC
677        }
[189]678#endif
[608]679      }
[56]680
[655]681#if H_3D_QTLPC     
[608]682      if(depthMapDetect && !bIntraSliceDetect && !rapPic && sps->getUseQTL())
[116]683      {
684        bTrySplitDQP = bTrySplit;
685      }
[189]686#endif
[837]687      if ( bIsLosslessMode )
[56]688      {
689        iQP = iMinQP;
690      }
[608]691    }
692
[837]693#if KWU_RC_MADPRED_E0227
[608]694    if ( uiDepth <= m_addSADDepth )
695    {
696      m_LCUPredictionSAD += m_temporalSAD;
697      m_addSADDepth = uiDepth;
698    }
[5]699#endif
[608]700#if H_3D_DIM_ENC
701    if( rpcBestCU->getSlice()->getIsDepth() && rpcBestCU->getSlice()->isIRAP() )
[56]702    {
[608]703      earlyDetectionSkipMode = false;
704    }
[56]705#endif
[2]706
[608]707    if(!earlyDetectionSkipMode)
708    {
709      for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
[2]710      {
[837]711        const Bool bIsLosslessMode = isAddLowestQP && (iQP == iMinQP);
712
713        if (bIsLosslessMode)
[443]714        {
[608]715          iQP = lowestQP;
[443]716        }
[837]717        rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[608]718
719        // do inter modes, NxN, 2NxN, and Nx2N
720        if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
[2]721        {
[116]722          // 2Nx2N, NxN
[608]723            if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) ))
[116]724            {
[608]725              if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu
726#if H_3D_QTLPC
727                && bTrySplit
728#endif
729                )
[116]730              {
[655]731#if  H_3D_FAST_TEXTURE_ENCODING
[608]732                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN, bFMD  );
[115]733#else
[608]734                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN   );
[115]735#endif
[837]736                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]737#if H_3D_VSP
[608]738                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
[116]739#endif
[608]740              }
[116]741            }
[56]742
[608]743          // 2NxN, Nx2N
744          if(doNotBlockPu
745#if H_3D_QTLPC
746            && bTryNx2N
[115]747#endif
[608]748            )
749          {
[655]750#if  H_3D_FAST_TEXTURE_ENCODING
[608]751            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N, bFMD  );
[115]752#else
[608]753            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N  );
[115]754#endif
[837]755            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]756#if H_3D_VSP
[608]757            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
[115]758#endif
[608]759            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
[116]760            {
[608]761              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
762            }
763          }
764          if(doNotBlockPu
765#if H_3D_QTLPC
766            && bTry2NxN
[115]767#endif
[608]768            )
769          {
[655]770#if  H_3D_FAST_TEXTURE_ENCODING
[608]771            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN, bFMD  );
772#else
773            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN  );
[115]774#endif
[837]775            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]776#if H_3D_VSP
[608]777            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
[296]778#endif
[608]779            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN)
780            {
781              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
[115]782            }
783          }
[56]784
785#if 1
[116]786          //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N)
787          if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) )
788          {
[56]789#if AMP_ENC_SPEEDUP       
[116]790            Bool bTestAMP_Hor = false, bTestAMP_Ver = false;
[56]791
792#if AMP_MRG
[116]793            Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false;
[56]794
[116]795            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver);
[2]796#else
[116]797            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver);
[56]798#endif
799
[116]800            //! Do horizontal AMP
801            if ( bTestAMP_Hor )
802            {
[608]803              if(doNotBlockPu
804#if H_3D_QTLPC
805                && bTry2NxN
806#endif
807                )
[116]808              {
[655]809#if  H_3D_FAST_TEXTURE_ENCODING
[608]810                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFMD );
811#else
812                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
[115]813#endif
[837]814                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]815#if H_3D_VSP
[608]816                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
817#endif
818                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
[116]819                {
[608]820                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
821                }
822              }
823              if(doNotBlockPu
824#if H_3D_QTLPC
825                && bTry2NxN
[115]826#endif
[608]827                )
828              {
[655]829#if  H_3D_FAST_TEXTURE_ENCODING
[608]830                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFMD );
[115]831#else
[608]832                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
[115]833#endif
[837]834                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]835#if H_3D_VSP
[608]836                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
837#endif
838                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
[116]839                {
[608]840                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
[116]841                }
[115]842              }
843            }
[56]844#if AMP_MRG
[116]845            else if ( bTestMergeAMP_Hor ) 
846            {
[608]847              if(doNotBlockPu
848#if H_3D_QTLPC
849                && bTry2NxN
850#endif
851                )
[116]852              {
[655]853#if  H_3D_FAST_TEXTURE_ENCODING
[608]854                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFMD, true );
855#else
856                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true );
[115]857#endif
[837]858                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]859#if H_3D_VSP
[608]860                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
861#endif
862                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
[116]863                {
[608]864                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
865                }
866              }
867              if(doNotBlockPu
868#if H_3D_QTLPC
869                && bTry2NxN
[115]870#endif
[608]871                )
872              {
[655]873#if  H_3D_FAST_TEXTURE_ENCODING
[608]874                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFMD, true );
[115]875#else
[608]876                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true );
[115]877#endif
[837]878                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]879#if H_3D_VSP
[608]880                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
881#endif
882                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
[116]883                {
[608]884                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
[116]885                }
[115]886              }
887            }
888#endif
[2]889
[116]890            //! Do horizontal AMP
891            if ( bTestAMP_Ver )
892            {
[608]893              if(doNotBlockPu
894#if H_3D_QTLPC
895                && bTryNx2N
896#endif
897                )
[116]898              {
[655]899#if  H_3D_FAST_TEXTURE_ENCODING
[608]900                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFMD );
901#else
902                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
[115]903#endif
[837]904                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]905#if H_3D_VSP
[608]906                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
907#endif
908                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
[116]909                {
[608]910                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
911                }
912              }
913              if(doNotBlockPu
914#if H_3D_QTLPC
915                && bTryNx2N
[115]916#endif
[608]917                )
918              {
[655]919#if  H_3D_FAST_TEXTURE_ENCODING
[608]920                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFMD );
[115]921#else
[608]922                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
[115]923#endif
[837]924                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]925#if H_3D_VSP
[608]926                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
[115]927#endif
[116]928              }
[115]929            }
[56]930#if AMP_MRG
[116]931            else if ( bTestMergeAMP_Ver )
932            {
[608]933              if(doNotBlockPu
934#if H_3D_QTLPC
935                && bTryNx2N
936#endif
937                )
[116]938              {
[655]939#if  H_3D_FAST_TEXTURE_ENCODING
[608]940                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFMD, true );
941#else
942                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true );
[115]943#endif
[837]944                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]945#if H_3D_VSP
[608]946                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
947#endif
948                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
[116]949                {
[608]950                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
951                }
952              }
953              if(doNotBlockPu
954#if H_3D_QTLPC
955                && bTryNx2N
[115]956#endif
[608]957                )
958              {
[655]959#if  H_3D_FAST_TEXTURE_ENCODING
[608]960                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFMD, true );
[115]961#else
[608]962                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true );
[115]963#endif
[837]964                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]965#if H_3D_VSP
[608]966                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
[115]967#endif
[116]968              }
[115]969            }
[56]970#endif
[2]971
[56]972#else
[608]973#if H_3D_QTLPC
974            if (bTry2NxN)
975            {
[5]976#endif
[608]977              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
[837]978              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]979#if H_3D_VSP
[608]980              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
[56]981#endif
[608]982              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
[837]983              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]984#if H_3D_VSP
[608]985              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
[56]986#endif
[608]987#if H_3D_QTLPC
988            }
989            if (bTryNx2N)
990            {
[56]991#endif
[608]992              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
[837]993              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]994#if H_3D_VSP
[608]995              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
[56]996#endif
[608]997              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
[837]998              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[622]999#if H_3D_VSP
[608]1000              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
[56]1001#endif
[608]1002#if H_3D_QTLPC
1003            }
[56]1004#endif
[2]1005
[56]1006#endif
[608]1007          }   
[56]1008#endif
[608]1009        }
[655]1010#if  H_3D_FAST_TEXTURE_ENCODING
[608]1011        if(!bFMD)
1012        {
[189]1013#endif
[608]1014        // do normal intra modes
[655]1015       
[608]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() )
[189]1023#endif
[608]1024            ) // avoid very complex intra if it is unlikely
[2]1025          {
[608]1026            xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
[655]1027
[837]1028#if KWU_RC_MADPRED_E0227
[655]1029            if ( uiDepth <= m_addSADDepth )
1030            {
1031              m_LCUPredictionSAD += m_spatialSAD;
1032              m_addSADDepth = uiDepth;
1033            }
1034#endif
1035
[837]1036            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[608]1037            if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
[116]1038            {
[608]1039#if H_3D_QTLPC //Try IntraNxN
1040              if(bTrySplit)
[116]1041              {
[189]1042#endif
[608]1043                if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) )
1044                {
1045                  xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN   );
[837]1046                  rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[608]1047                }
1048#if H_3D_QTLPC
[116]1049              }
[608]1050#endif
[56]1051            }
[2]1052          }
[608]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 ))
[56]1063#else
[608]1064          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
[56]1065#endif
[608]1066          {
1067            xCheckIntraPCM (rpcBestCU, rpcTempCU);
[837]1068            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[608]1069          }
1070        }
[655]1071#if  H_3D_FAST_TEXTURE_ENCODING
[608]1072        }
[189]1073#endif
[837]1074        if (bIsLosslessMode)
[296]1075        {
[608]1076          iQP = iMinQP;
[296]1077        }
[56]1078      }
[2]1079    }
1080
1081    m_pcEntropyCoder->resetBits();
1082    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
1083    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
[56]1084      rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
[608]1085    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
[2]1086
[56]1087    // Early CU determination
[608]1088    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
[2]1089    {
[56]1090      bSubBranch = false;
[2]1091    }
[56]1092    else
1093    {
1094      bSubBranch = true;
1095    }
[655]1096#if  H_3D_FAST_TEXTURE_ENCODING
[608]1097    if(rpcBestCU->getSlice()->getViewIndex() && !rpcBestCU->getSlice()->getIsDepth() && (uiDepth >=iIVFMaxD) && rpcBestCU->isSkipped(0))
1098    {
1099      bSubBranch = false;
1100    }
[2]1101#endif
1102  }
[56]1103  else if(!(bSliceEnd && bInsidePicture))
[2]1104  {
1105    bBoundary = true;
1106  }
1107
[56]1108  // copy orginal YUV samples to PCM buffer
1109  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
[2]1110  {
[56]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;
[608]1127    if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr())
[56]1128    {
1129      iStartQP = rpcTempCU->getQP(0);
1130    }
1131    else
1132    {
[608]1133      UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
[56]1134      iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx);
1135    }
1136    iMinQP = iStartQP;
1137    iMaxQP = iStartQP;
1138  }
[608]1139  if ( m_pcEncCfg->getUseRateCtrl() )
1140  {
1141    iMinQP = m_pcRateCtrl->getRCQP();
1142    iMaxQP = m_pcRateCtrl->getRCQP();
1143  }
[837]1144
1145  if ( m_pcEncCfg->getCUTransquantBypassFlagForceValue() )
[608]1146  {
[837]1147    iMaxQP = iMinQP; // If all blocks are forced into using transquant bypass, do not loop here.
[608]1148  }
[56]1149  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
1150  {
[837]1151    const Bool bIsLosslessMode = false; // False at this level. Next level down may set it to true.
1152    rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
[2]1153
[56]1154    // further split
[655]1155#if H_3D_QTLPC
[56]1156    if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
[655]1157#else
1158    if( bSubBranch && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
1159#endif
[2]1160    {
[608]1161#if H_3D_VSO // M9
[56]1162      // reset Model
1163      if( m_pcRdCost->getUseRenModel() )
1164      {
[81]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 );
[56]1170      }
1171#endif
[608]1172
[56]1173      UChar       uhNextDepth         = uiDepth+1;
1174      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
1175      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
[2]1176
[56]1177      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
[2]1178      {
[56]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
[608]1182        Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr();
[56]1183        if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
[2]1184        {
[56]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          }
[2]1199          else
1200          {
[56]1201            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) );
[2]1202          }
[56]1203#else
1204          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
1205#endif
[2]1206
[56]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)
[2]1211        {
[56]1212          pcSubBestPartCU->copyToPic( uhNextDepth );
1213          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
[2]1214        }
1215      }
1216
[56]1217      if( !bBoundary )
1218      {
1219        m_pcEntropyCoder->resetBits();
1220        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
[2]1221
[56]1222        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1223          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1224        }
[608]1225#if H_3D_VSO // M10
[56]1226      if ( m_pcRdCost->getUseVSO() )
1227        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1228      else
[5]1229#endif
[608]1230      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
[2]1231
[56]1232      if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
1233      {
[608]1234        Bool hasResidual = false;
[56]1235        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
1236        {
[608]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 ) ) )
[56]1239          {
[608]1240            hasResidual = true;
[56]1241            break;
1242          }
1243        }
[2]1244
[56]1245        UInt uiTargetPartIdx;
[608]1246        if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() )
[56]1247        {
[608]1248          uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
[56]1249        }
1250        else
1251        {
1252          uiTargetPartIdx = 0;
1253        }
[608]1254        if ( hasResidual )
[56]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();
[608]1261#if H_3D_VSO // M11
1262          if ( m_pcRdCost->getUseLambdaScaleVSO())         
1263            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );         
[56]1264          else
[5]1265#endif
[608]1266          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
[56]1267#endif
[608]1268
1269          Bool foundNonZeroCbf = false;
1270          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf );
1271          assert( foundNonZeroCbf );
[56]1272        }
1273        else
1274        {
1275          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
1276        }
1277      }
[2]1278
[56]1279        m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
[608]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)
[56]1285      {
1286        rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
1287      }
1288      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth);                                  // RD compare current larger prediction
1289    }                                                                                  // with sub partitioned prediction.
[116]1290    }
[56]1291
[608]1292#if H_3D_VSO // M12
[56]1293  if( m_pcRdCost->getUseRenModel() )
1294  {
[116]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 );
[56]1300  }
1301#endif
[2]1302  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
1303
[56]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  {
[2]1307    return;
[56]1308  }
[2]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
[56]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
[608]1331  UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU();
1332  UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU();
[56]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  }
[608]1357  UInt uiGranularityWidth = g_uiMaxCUWidth;
[56]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 
[608]1363  if(granularityBoundary)
[56]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.
[608]1377  UInt iGranularitySize = pcCU->getPic()->getNumPartInCU();
1378  Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize;
1379  if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) 
[56]1380  {
1381    iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)));
1382  }
1383  // Set slice end parameter
[608]1384  if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) 
[56]1385  {
[608]1386    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
[56]1387    pcSlice->setSliceCurEndCUAddr(iGranularityEnd);
1388    return;
1389  }
[608]1390  // Set dependent slice end parameter
1391  if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) 
[56]1392  {
[608]1393    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1394    return;
[56]1395  }
1396  if(granularityBoundary)
1397  {
1398    pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) );
[608]1399    pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits);
[56]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
[2]1436/** encode a CU block recursively
1437 * \param pcCU
1438 * \param uiAbsPartIdx
[56]1439 * \param uiDepth
[2]1440 * \returns Void
1441 */
1442Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1443{
1444  TComPic* pcPic = pcCU->getPic();
[56]1445 
[2]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;
[608]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
[443]1459
[56]1460  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1461  // If slice start is within this cu...
[608]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) );
[56]1464  // We need to split, so don't try these modes.
1465  if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
[2]1466  {
[56]1467    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
[2]1468  }
1469  else
1470  {
1471    bBoundary = true;
1472  }
[56]1473 
[2]1474  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary )
1475  {
1476    UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2;
[56]1477    if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1478    {
1479      setdQPFlag(true);
1480    }
[2]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] ];
[608]1485      Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr();
[56]1486      if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1487      {
[2]1488        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
[56]1489      }
[2]1490    }
1491    return;
1492  }
[56]1493 
[608]1494#if H_MV_ENC_DEC_TRAC
1495  DTRACE_CU_S("=========== coding_unit ===========\n")
1496#endif
1497
[56]1498  if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1499  {
1500    setdQPFlag(true);
1501  }
[608]1502  if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1503  {
1504    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1505  }
[5]1506  if( !pcCU->getSlice()->isIntra() )
[2]1507  {
1508    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1509  }
[56]1510 
[2]1511  if( pcCU->isSkipped( uiAbsPartIdx ) )
1512  {
[608]1513#if H_MV_ENC_DEC_TRAC
1514    DTRACE_PU_S("=========== prediction_unit ===========\n")
1515    DTRACE_PU("x0", uiLPelX)
1516    DTRACE_PU("x1", uiTPelY)
[115]1517#endif
[608]1518    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
[833]1519#if !SEC_IC_ARP_SIG_G0072
[608]1520#if H_3D_IC
1521    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
[296]1522#endif
[833]1523#endif
[608]1524#if H_3D_ARP
1525    m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
[443]1526#endif
[833]1527#if SEC_IC_ARP_SIG_G0072
1528#if H_3D_IC
1529    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1530#endif
1531#endif
[56]1532    finishCU(pcCU,uiAbsPartIdx,uiDepth);
[2]1533    return;
1534  }
[56]1535  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
[608]1536 
[56]1537  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
[608]1538 
[833]1539#if QC_SDC_UNIFY_G0130
1540  m_pcEntropyCoder->encodeSDCFlag( pcCU, uiAbsPartIdx, false );
1541#endif
[56]1542  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1543  {
1544    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
[2]1545
[56]1546    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1547    {
1548      // Encode slice finish
1549      finishCU(pcCU,uiAbsPartIdx,uiDepth);
1550      return;
1551    }
1552  }
[2]1553
[56]1554  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1555  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
[833]1556#if !SEC_IC_ARP_SIG_G0072
[608]1557#if H_3D_IC
1558  m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
[189]1559#endif
[833]1560#endif
[608]1561#if H_3D_ARP
1562  m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
[296]1563#endif
[833]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
[608]1570  m_pcEntropyCoder->encodeInterSDCFlag( pcCU, uiAbsPartIdx, false );
[5]1571#endif
[608]1572
[2]1573  // Encode Coefficients
[56]1574  Bool bCodeDQP = getdQPFlag();
1575  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP );
1576  setdQPFlag( bCodeDQP );
[2]1577
[56]1578  // --- write terminating bit ---
1579  finishCU(pcCU,uiAbsPartIdx,uiDepth);
[2]1580}
1581
[608]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
[2]1697/** check RD costs for a CU block encoded with merge
1698 * \param rpcBestCU
1699 * \param rpcTempCU
1700 * \returns Void
1701 */
[608]1702Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode )
[2]1703{
1704  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
[608]1705#if H_3D_IV_MERGE
[56]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
[608]1709  TComMvField  cMvFieldNeighbours[2 * MRG_MAX_NUM_CANDS]; // double length for mv of both lists
[2]1710  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
[56]1711#endif
1712  Int numValidMergeCand = 0;
[837]1713  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
[2]1714
[608]1715  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
[2]1716  {
1717    uhInterDirNeighbours[ui] = 0;
1718  }
1719  UChar uhDepth = rpcTempCU->getDepth( 0 );
[608]1720#if H_3D_IC
1721  Bool bICFlag = rpcTempCU->getICFlag( 0 );
1722#endif
1723#if H_3D_VSO // M1  //nececcary here?
[2]1724  if( m_pcRdCost->getUseRenModel() )
1725  {
[81]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 );
[2]1731  }
[5]1732#endif
[2]1733
[773]1734#if H_3D_ARP
[724]1735  DisInfo cOrigDisInfo = rpcTempCU->getDvInfo(0);
1736#else
[2]1737  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
[724]1738#endif
[608]1739
1740#if H_3D_VSP
[773]1741#if !H_3D_ARP
[608]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];
[724]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
[773]1754#if H_3D
[724]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
[608]1763  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
[443]1764#endif
[724]1765#endif
[608]1766
1767#if H_3D_IV_MERGE
1768  Int mergeCandBuffer[MRG_MAX_NUM_CANDS_MEM];
[296]1769#else
[608]1770  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
[296]1771#endif
[773]1772#if H_3D_ARP
[724]1773for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1774#else
[608]1775for( UInt ui = 0; ui < numValidMergeCand; ++ui )
[724]1776#endif
[608]1777  {
1778    mergeCandBuffer[ui] = 0;
1779  }
[2]1780
[56]1781  Bool bestIsSkip = false;
[608]1782
1783  UInt iteration;
1784  if ( rpcTempCU->isLosslessCoded(0))
[443]1785  {
[608]1786    iteration = 1;
1787  }
1788  else 
[2]1789  {
[608]1790    iteration = 2;
1791  }
1792
1793#if H_3D_ARP
1794  Int nARPWMax = rpcTempCU->getSlice()->getARPStepNum() - 1;
[833]1795#if SEC_IC_ARP_SIG_G0072
1796  if( nARPWMax < 0 || !rpcTempCU->getDvInfo(0).bDV || bICFlag )
1797#else
[608]1798  if( nARPWMax < 0 || !rpcTempCU->getDvInfo(0).bDV )
[833]1799#endif
[608]1800  {
1801    nARPWMax = 0;
1802  }
1803  for( Int nARPW=nARPWMax; nARPW >= 0 ; nARPW-- )
1804  {
[724]1805    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS_MEM*sizeof(Int) );
1806    rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
[840]1807#if !UPDATE_HM13
[724]1808    rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth );
[840]1809#endif
[724]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];
[773]1819#if H_3D_SPIVMP
[724]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
[773]1827#if H_3D
[724]1828    rpcTempCU->initAvailableFlags();
1829    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1830    rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag,inheritedVSPDisInfo
[773]1831#if H_3D_SPIVMP
[724]1832      , bSPIVMPFlag, pcMvFieldSP, puhInterDirSP
1833#endif
1834      , numValidMergeCand
1835      );
1836#else
1837    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag, inheritedVSPDisInfo, numValidMergeCand );
1838#endif
[773]1839
[724]1840#endif
[833]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
[608]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() )
[443]1854        {
[608]1855          if( bICFlag && uiMergeCand == 0 ) 
1856          {
1857            continue;
1858          }
[443]1859        }
1860#endif
[608]1861        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1862        {
[56]1863        if( !(bestIsSkip && uiNoResidual == 0) )
1864        {
1865          // set MC parameters
[608]1866          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
[837]1867          rpcTempCU->setCUTransquantBypassSubParts( bTransquantBypassFlag,     0, uhDepth );
[56]1868          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
[608]1869#if H_3D_IC
1870          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
[443]1871#endif
[608]1872#if H_3D_ARP
1873          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1874#endif
[56]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
[608]1877#if H_3D_VSP
1878          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
1879          rpcTempCU->setDvInfoSubParts(inheritedVSPDisInfo[uiMergeCand].m_acDvInfo, 0, 0, uhDepth );
[443]1880#endif
[833]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
[773]1892#if H_3D_SPIVMP
[724]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
[833]1910#endif
1911#if NTT_STORE_SPDV_VSP_G0148
1912          if ( vspFlag[uiMergeCand] )
[724]1913          {
[833]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
[838]1939          {
[724]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
[838]1944#if NTT_STORE_SPDV_VSP_G0148
[724]1945          }
[838]1946#endif
[608]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       }
[2]1959#endif
[608]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 );
[833]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
[655]1982#if H_3D_INTER_SDC
[608]1983          TComDataCU *rpcTempCUPre = rpcTempCU;
[296]1984#endif
[608]1985          Int orgQP = rpcTempCU->getQP( 0 );
1986          xCheckDQP( rpcTempCU );
1987          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
[655]1988#if H_3D_INTER_SDC
[608]1989          if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() && !uiNoResidual )
1990          {
[833]1991#if SEC_INTER_SDC_G0101
1992            for( Int uiOffest = -2 ; uiOffest <= 2 ; uiOffest++ )
1993            {
1994              if( rpcTempCU != rpcTempCUPre )
1995              {
[840]1996                rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag  );
[833]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
[608]2024            if( rpcTempCU != rpcTempCUPre )
[296]2025            {
[840]2026              rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag  );
[608]2027              rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
[296]2028            }
[608]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            }
[296]2041#endif
[608]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 );
[2]2048
[608]2049            xCheckDQP( rpcTempCU );
2050            xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
[833]2051#endif
[56]2052          }
[296]2053#endif
[837]2054          rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag );
[608]2055
2056      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
2057      {
[655]2058#if H_3D_INTER_SDC
[608]2059        if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) )
2060        {
2061          bestIsSkip = !rpcBestCU->getSDCFlag( 0 ) && ( rpcBestCU->getQtRootCbf(0) == 0 );
2062        }
2063        else
2064        {
[296]2065#endif
[608]2066        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
[655]2067#if H_3D_INTER_SDC
[608]2068        }
[5]2069#endif
[608]2070      }
2071    }
2072   }
2073  }
[2]2074
[608]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 )
[56]2089          {
[608]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;
[56]2094          }
[608]2095        }
[2]2096
[608]2097        if(absoulte_MV == 0)
2098        {
2099          *earlyDetectionSkipMode = true;
[56]2100        }
[2]2101      }
2102    }
2103  }
[608]2104 }
[773]2105#if H_3D_SPIVMP
[735]2106 delete[] pcMvFieldSP;
2107 delete[] puhInterDirSP;
[724]2108#endif
[608]2109#if H_3D_ARP
2110 }
[443]2111#endif
[2]2112}
2113
[608]2114
[56]2115#if AMP_MRG
[655]2116#if  H_3D_FAST_TEXTURE_ENCODING
[608]2117Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bFMD, Bool bUseMRG)
[56]2118#else
2119Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
2120#endif
2121#else
[2]2122Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
2123#endif
[608]2124{
[840]2125
2126#if UPDATE_HM13
2127#if H_3D
2128  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
2129#endif
2130#endif
[655]2131#if  H_3D_FAST_TEXTURE_ENCODING
[608]2132  if(!(bFMD && (ePartSize == SIZE_2Nx2N)))  //have  motion estimation or merge check
2133  {
[56]2134#endif
[2]2135  UChar uhDepth = rpcTempCU->getDepth( 0 );
[608]2136#if H_3D_ARP
2137  Int iLayerId    = rpcTempCU->getSlice()->getLayerId();
[443]2138  Bool bFirstTime = true;
[608]2139  Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
2140
[833]2141#if SEC_IC_ARP_SIG_G0072
2142  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || !rpcTempCU->getDvInfo(0).bDV || rpcTempCU->getICFlag(0) )
2143#else
[608]2144  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || !rpcTempCU->getDvInfo(0).bDV  )
[833]2145#endif
[608]2146  {
[443]2147    nARPWMax = 0;
[608]2148  }
2149
2150  for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
[443]2151  {
[608]2152    if( bFirstTime == false && rpcTempCU->getSlice()->getVPS()->getUseAdvRP( iLayerId ) )
2153    {
[840]2154#if UPDATE_HM13
2155      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0),bTransquantBypassFlag );     
2156#else
[443]2157      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0) );
[840]2158#endif
[608]2159    }
2160#endif
2161#if H_3D_VSO // M3
[2]2162  if( m_pcRdCost->getUseRenModel() )
2163  {
[81]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 );
[2]2169  }
[608]2170#endif
[2]2171
2172  rpcTempCU->setDepthSubParts( uhDepth, 0 );
[56]2173 
[608]2174  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2175
2176  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
2177  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
[833]2178#if MTK_DDD_G0063
2179  rpcTempCU->setUseDDD( false, 0, uhDepth );
2180#endif
2181
[608]2182#if H_3D_ARP
2183  rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
[5]2184#endif
[56]2185
[608]2186#if H_3D_ARP
2187  if( bFirstTime == false && nARPWMax )
[443]2188  {
[608]2189    rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
2190    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2191
[443]2192    m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
2193  }
2194  else
2195  {
2196    bFirstTime = false;
2197#endif
[56]2198#if AMP_MRG
2199  rpcTempCU->setMergeAMP (true);
[655]2200#if  H_3D_FAST_TEXTURE_ENCODING
[608]2201  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bFMD, false, bUseMRG );
[56]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 
[2]2206  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
2207#endif
[608]2208#if H_3D_ARP
2209   if( nARPWMax )
[443]2210   {
2211     m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
2212   }
2213  }
2214#endif
[56]2215
2216#if AMP_MRG
2217  if ( !rpcTempCU->getMergeAMP() )
[2]2218  {
[608]2219#if H_3D_ARP
2220    if( nARPWMax )
2221    {
[443]2222      continue;
[608]2223    }
[443]2224    else
2225#endif
[2]2226    return;
2227  }
2228#endif
[56]2229
[837]2230#if KWU_RC_MADPRED_E0227
[608]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  }
[56]2238#endif
[608]2239  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
[833]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
[608]2246
2247#if H_3D_VSO // M4
[2]2248  if( m_pcRdCost->getUseLambdaScaleVSO() )
2249    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2250  else
[5]2251#endif
[839]2252
[608]2253  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
[655]2254#if H_3D_INTER_SDC
[608]2255  TComDataCU *rpcTempCUPre = rpcTempCU;
2256#endif
2257  xCheckDQP( rpcTempCU );
2258  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
[655]2259#if H_3D_INTER_SDC
[833]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
[608]2263  if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() )
[833]2264#endif
[2]2265  {
[833]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 );
[840]2272#if UPDATE_HM13
2273        rpcTempCU->initEstData( uhDepth, orgQP ,bTransquantBypassFlag );     
2274#else
[833]2275        rpcTempCU->initEstData( uhDepth, orgQP );
[840]2276#endif
[833]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
[608]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
[2]2324
[608]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
[56]2332  xCheckDQP( rpcTempCU );
2333  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
[833]2334#endif
[443]2335  }
2336#endif
[608]2337#if H_3D_ARP
2338  }
2339#endif
[655]2340#if  H_3D_FAST_TEXTURE_ENCODING
[608]2341  }
2342#endif
[2]2343}
2344
[833]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
[2]2534Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2535{
2536  UInt uiDepth = rpcTempCU->getDepth( 0 );
[56]2537 
[608]2538#if H_3D_VSO // M5
[2]2539  if( m_pcRdCost->getUseRenModel() )
2540  {
[81]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 );
[2]2546  }
[5]2547#endif
[2]2548
[608]2549  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2550
[2]2551  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
2552  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
[56]2553 
[2]2554  Bool bSeparateLumaChroma = true; // choose estimation mode
[608]2555  UInt uiPreCalcDistC      = 0;
[2]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() );
[56]2563 
[608]2564#if H_3D_DIM_SDC
[189]2565  if( !rpcTempCU->getSDCFlag( 0 ) )
2566#endif
[2]2567  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
[56]2568 
[2]2569  m_pcEntropyCoder->resetBits();
[608]2570  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2571  {
2572    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2573  }
[2]2574  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2575  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
2576  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
[833]2577#if QC_SDC_UNIFY_G0130
2578  m_pcEntropyCoder->encodeSDCFlag( rpcTempCU, 0, true );
2579#endif
[2]2580  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
[56]2581  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
[2]2582
2583  // Encode Coefficients
[56]2584  Bool bCodeDQP = getdQPFlag();
2585  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
2586  setdQPFlag( bCodeDQP );
2587 
[837]2588  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
[56]2589 
[2]2590  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
[56]2591    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
[608]2592#if H_3D_VSO // M6
2593  if( m_pcRdCost->getUseLambdaScaleVSO()) 
2594    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
[2]2595  else
[5]2596#endif
[56]2597  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2598 
2599  xCheckDQP( rpcTempCU );
2600  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2601}
[2]2602
[56]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
[608]2614  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2615
[56]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 );
[608]2620  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
[56]2621
2622  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
2623
[837]2624  m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
[56]2625
2626  m_pcEntropyCoder->resetBits();
[608]2627  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2628  {
2629    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2630  }
[56]2631  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2632  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
2633  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
[833]2634#if QC_SDC_UNIFY_G0130
2635  m_pcEntropyCoder->encodeSDCFlag( rpcTempCU, 0, true );
2636#endif
[56]2637  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
2638
[837]2639  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
[56]2640
2641  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2642    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
[608]2643#if H_3D_VSO // M44
[56]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 );
[2]2651  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
2652}
2653
[56]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 )
[2]2660{
[56]2661  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
2662  {
2663    TComYuv* pcYuv;
2664    // Change Information data
2665    TComDataCU* pcCU = rpcBestCU;
2666    rpcBestCU = rpcTempCU;
2667    rpcTempCU = pcCU;
[2]2668
[56]2669    // Change Prediction data
2670    pcYuv = m_ppcPredYuvBest[uiDepth];
2671    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
2672    m_ppcPredYuvTemp[uiDepth] = pcYuv;
[2]2673
[56]2674    // Change Reconstruction data
2675    pcYuv = m_ppcRecoYuvBest[uiDepth];
2676    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
2677    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
[2]2678
[56]2679    pcYuv = NULL;
2680    pcCU  = NULL;
[2]2681
[837]2682    // store temp best CI for next CU coding
[56]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() )
[2]2692  {
[56]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();
[608]2700#if H_3D_VSO // M45
2701      if ( m_pcRdCost->getUseVSO() )     
2702        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
[56]2703      else
2704#endif
2705      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
2706#endif
[2]2707    }
[56]2708    else
[2]2709    {
[56]2710      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
[2]2711    }
[56]2712  }
2713}
2714
[2]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}
[56]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());
[608]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) );
[56]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;
[2]2745
[56]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
[608]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();
[56]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  }
[2]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
[56]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.