source: 3DVCSoftware/branches/HTM-8.2-dev3-Samsung/source/Lib/TLibEncoder/TEncCu.cpp

Last change on this file was 699, checked in by samsung-htm, 11 years ago

Integration of F0147 and F0151
F0147: DMM simplification and signalling
F0151: Removal of IC in depth coding

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