source: 3DVCSoftware/branches/HTM-DEV-0.3-dev2/source/Lib/TLibEncoder/TEncCu.cpp @ 521

Last change on this file since 521 was 521, checked in by tech, 11 years ago

Integrated following changes:

  • H_MV_FIX1071, fix of encoder side reference list construction on IRAP pictures, same as in HM11.
  • H_3D_VSO_FIX_BORDRE_EXTENSION, fixed uninitialized borders for org-yuvs in VSO
  • H_MV_ENC_DEC_TRAC, added cu/pu level trace ( only enabled when ENC_DEC_TRACE is enabled)
  • Property svn:eol-style set to native
File size: 73.1 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2013, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TEncCu.cpp
35    \brief    Coding Unit (CU) encoder class
36*/
37
38#include <stdio.h>
39#include "TEncTop.h"
40#include "TEncCu.h"
41#include "TEncAnalyze.h"
42
43#include <cmath>
44#include <algorithm>
45using namespace std;
46
47//! \ingroup TLibEncoder
48//! \{
49
50// ====================================================================================================================
51// Constructor / destructor / create / destroy
52// ====================================================================================================================
53
54/**
55 \param    uiTotalDepth  total number of allowable depth
56 \param    uiMaxWidth    largest CU width
57 \param    uiMaxHeight   largest CU height
58 */
59Void TEncCu::create(UChar uhTotalDepth, UInt uiMaxWidth, UInt uiMaxHeight)
60{
61  Int i;
62 
63  m_uhTotalDepth   = uhTotalDepth + 1;
64  m_ppcBestCU      = new TComDataCU*[m_uhTotalDepth-1];
65  m_ppcTempCU      = new TComDataCU*[m_uhTotalDepth-1];
66   
67#if H_3D_ARP
68  m_ppcWeightedTempCU = new TComDataCU*[m_uhTotalDepth-1];
69#endif
70
71  m_ppcPredYuvBest = new TComYuv*[m_uhTotalDepth-1];
72  m_ppcResiYuvBest = new TComYuv*[m_uhTotalDepth-1];
73  m_ppcRecoYuvBest = new TComYuv*[m_uhTotalDepth-1];
74  m_ppcPredYuvTemp = new TComYuv*[m_uhTotalDepth-1];
75  m_ppcResiYuvTemp = new TComYuv*[m_uhTotalDepth-1];
76  m_ppcRecoYuvTemp = new TComYuv*[m_uhTotalDepth-1];
77  m_ppcOrigYuv     = new TComYuv*[m_uhTotalDepth-1];
78 
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;
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) );
88   
89#if H_3D_ARP
90    m_ppcWeightedTempCU[i] = new TComDataCU; m_ppcWeightedTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
91#endif 
92
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);
96   
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);
100   
101    m_ppcOrigYuv    [i] = new TComYuv; m_ppcOrigYuv    [i]->create(uiWidth, uiHeight);
102  }
103 
104  m_bEncodeDQP = false;
105#if RATE_CONTROL_LAMBDA_DOMAIN
106  m_LCUPredictionSAD = 0;
107  m_addSADDepth      = 0;
108  m_temporalSAD      = 0;
109#endif
110
111  // initialize partition order.
112  UInt* piTmp = &g_auiZscanToRaster[0];
113  initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp);
114  initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
115 
116  // initialize conversion matrix from partition index to pel
117  initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
118}
119
120Void TEncCu::destroy()
121{
122  Int i;
123 
124  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
125  {
126    if(m_ppcBestCU[i])
127    {
128      m_ppcBestCU[i]->destroy();      delete m_ppcBestCU[i];      m_ppcBestCU[i] = NULL;
129    }
130    if(m_ppcTempCU[i])
131    {
132      m_ppcTempCU[i]->destroy();      delete m_ppcTempCU[i];      m_ppcTempCU[i] = NULL;
133    }
134#if H_3D_ARP
135    if(m_ppcWeightedTempCU[i])
136    {
137      m_ppcWeightedTempCU[i]->destroy(); delete m_ppcWeightedTempCU[i]; m_ppcWeightedTempCU[i] = NULL;
138    }
139#endif
140    if(m_ppcPredYuvBest[i])
141    {
142      m_ppcPredYuvBest[i]->destroy(); delete m_ppcPredYuvBest[i]; m_ppcPredYuvBest[i] = NULL;
143    }
144    if(m_ppcResiYuvBest[i])
145    {
146      m_ppcResiYuvBest[i]->destroy(); delete m_ppcResiYuvBest[i]; m_ppcResiYuvBest[i] = NULL;
147    }
148    if(m_ppcRecoYuvBest[i])
149    {
150      m_ppcRecoYuvBest[i]->destroy(); delete m_ppcRecoYuvBest[i]; m_ppcRecoYuvBest[i] = NULL;
151    }
152    if(m_ppcPredYuvTemp[i])
153    {
154      m_ppcPredYuvTemp[i]->destroy(); delete m_ppcPredYuvTemp[i]; m_ppcPredYuvTemp[i] = NULL;
155    }
156    if(m_ppcResiYuvTemp[i])
157    {
158      m_ppcResiYuvTemp[i]->destroy(); delete m_ppcResiYuvTemp[i]; m_ppcResiYuvTemp[i] = NULL;
159    }
160    if(m_ppcRecoYuvTemp[i])
161    {
162      m_ppcRecoYuvTemp[i]->destroy(); delete m_ppcRecoYuvTemp[i]; m_ppcRecoYuvTemp[i] = NULL;
163    }
164    if(m_ppcOrigYuv[i])
165    {
166      m_ppcOrigYuv[i]->destroy();     delete m_ppcOrigYuv[i];     m_ppcOrigYuv[i] = NULL;
167    }
168  }
169  if(m_ppcBestCU)
170  {
171    delete [] m_ppcBestCU;
172    m_ppcBestCU = NULL;
173  }
174  if(m_ppcTempCU)
175  {
176    delete [] m_ppcTempCU;
177    m_ppcTempCU = NULL;
178  }
179
180#if H_3D_ARP
181  if(m_ppcWeightedTempCU)
182  {
183    delete [] m_ppcWeightedTempCU; 
184    m_ppcWeightedTempCU = NULL; 
185  }
186#endif
187  if(m_ppcPredYuvBest)
188  {
189    delete [] m_ppcPredYuvBest;
190    m_ppcPredYuvBest = NULL;
191  }
192  if(m_ppcResiYuvBest)
193  {
194    delete [] m_ppcResiYuvBest;
195    m_ppcResiYuvBest = NULL;
196  }
197  if(m_ppcRecoYuvBest)
198  {
199    delete [] m_ppcRecoYuvBest;
200    m_ppcRecoYuvBest = NULL;
201  }
202  if(m_ppcPredYuvTemp)
203  {
204    delete [] m_ppcPredYuvTemp;
205    m_ppcPredYuvTemp = NULL;
206  }
207  if(m_ppcResiYuvTemp)
208  {
209    delete [] m_ppcResiYuvTemp;
210    m_ppcResiYuvTemp = NULL;
211  }
212  if(m_ppcRecoYuvTemp)
213  {
214    delete [] m_ppcRecoYuvTemp;
215    m_ppcRecoYuvTemp = NULL;
216  }
217  if(m_ppcOrigYuv)
218  {
219    delete [] m_ppcOrigYuv;
220    m_ppcOrigYuv = NULL;
221  }
222}
223
224/** \param    pcEncTop      pointer of encoder class
225 */
226Void TEncCu::init( TEncTop* pcEncTop )
227{
228  m_pcEncCfg           = pcEncTop;
229  m_pcPredSearch       = pcEncTop->getPredSearch();
230  m_pcTrQuant          = pcEncTop->getTrQuant();
231  m_pcBitCounter       = pcEncTop->getBitCounter();
232  m_pcRdCost           = pcEncTop->getRdCost();
233 
234  m_pcEntropyCoder     = pcEncTop->getEntropyCoder();
235  m_pcCavlcCoder       = pcEncTop->getCavlcCoder();
236  m_pcSbacCoder       = pcEncTop->getSbacCoder();
237  m_pcBinCABAC         = pcEncTop->getBinCABAC();
238 
239  m_pppcRDSbacCoder   = pcEncTop->getRDSbacCoder();
240  m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder();
241 
242  m_bUseSBACRD        = pcEncTop->getUseSBACRD();
243  m_pcRateCtrl        = pcEncTop->getRateCtrl();
244}
245
246// ====================================================================================================================
247// Public member functions
248// ====================================================================================================================
249
250/** \param  rpcCU pointer of CU data class
251 */
252Void TEncCu::compressCU( TComDataCU*& rpcCU )
253{
254  // initialize CU data
255  m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
256  m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
257
258#if RATE_CONTROL_LAMBDA_DOMAIN
259  m_addSADDepth      = 0;
260  m_LCUPredictionSAD = 0;
261  m_temporalSAD      = 0;
262#endif
263
264  // analysis of CU
265  xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
266
267#if ADAPTIVE_QP_SELECTION
268  if( m_pcEncCfg->getUseAdaptQpSelect() )
269  {
270    if(rpcCU->getSlice()->getSliceType()!=I_SLICE) //IIII
271    {
272      xLcuCollectARLStats( rpcCU);
273    }
274  }
275#endif
276}
277/** \param  pcCU  pointer of CU data class
278 */
279Void TEncCu::encodeCU ( TComDataCU* pcCU )
280{
281  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
282  {
283    setdQPFlag(true);
284  }
285
286  // Encode CU data
287  xEncodeCU( pcCU, 0, 0 );
288}
289
290// ====================================================================================================================
291// Protected member functions
292// ====================================================================================================================
293/** Derive small set of test modes for AMP encoder speed-up
294 *\param   rpcBestCU
295 *\param   eParentPartSize
296 *\param   bTestAMP_Hor
297 *\param   bTestAMP_Ver
298 *\param   bTestMergeAMP_Hor
299 *\param   bTestMergeAMP_Ver
300 *\returns Void
301*/
302#if AMP_ENC_SPEEDUP
303#if AMP_MRG
304Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver, Bool &bTestMergeAMP_Hor, Bool &bTestMergeAMP_Ver)
305#else
306Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver)
307#endif
308{
309  if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
310  {
311    bTestAMP_Hor = true;
312  }
313  else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
314  {
315    bTestAMP_Ver = true;
316  }
317  else if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->getMergeFlag(0) == false && rpcBestCU->isSkipped(0) == false )
318  {
319    bTestAMP_Hor = true;         
320    bTestAMP_Ver = true;         
321  }
322
323#if AMP_MRG
324  //! Utilizing the partition size of parent PU   
325  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
326  { 
327    bTestMergeAMP_Hor = true;
328    bTestMergeAMP_Ver = true;
329  }
330
331  if ( eParentPartSize == SIZE_NONE ) //! if parent is intra
332  {
333    if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
334    {
335      bTestMergeAMP_Hor = true;
336    }
337    else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
338    {
339      bTestMergeAMP_Ver = true;
340    }
341  }
342
343  if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->isSkipped(0) == false )
344  {
345    bTestMergeAMP_Hor = true;         
346    bTestMergeAMP_Ver = true;         
347  }
348
349  if ( rpcBestCU->getWidth(0) == 64 )
350  { 
351    bTestAMP_Hor = false;
352    bTestAMP_Ver = false;
353  }   
354#else
355  //! Utilizing the partition size of parent PU       
356  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
357  { 
358    bTestAMP_Hor = true;
359    bTestAMP_Ver = true;
360  }
361
362  if ( eParentPartSize == SIZE_2Nx2N )
363  { 
364    bTestAMP_Hor = false;
365    bTestAMP_Ver = false;
366  }     
367#endif
368}
369#endif
370
371// ====================================================================================================================
372// Protected member functions
373// ====================================================================================================================
374/** Compress a CU block recursively with enabling sub-LCU-level delta QP
375 *\param   rpcBestCU
376 *\param   rpcTempCU
377 *\param   uiDepth
378 *\returns Void
379 *
380 *- for loop of QP value to compress the current CU with all possible QP
381*/
382#if AMP_ENC_SPEEDUP
383Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth, PartSize eParentPartSize )
384#else
385Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
386#endif
387{
388  TComPic* pcPic = rpcBestCU->getPic();
389
390  // get Original YUV data from picture
391  m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
392
393  // variables for fast encoder decision
394  Bool    bEarlySkip  = false;
395  Bool    bTrySplit    = true;
396  Double  fRD_Skip    = MAX_DOUBLE;
397
398  // variable for Early CU determination
399  Bool    bSubBranch = true;
400
401  // variable for Cbf fast mode PU decision
402  Bool    doNotBlockPu = true;
403  Bool earlyDetectionSkipMode = false;
404
405  Bool    bTrySplitDQP  = true;
406
407  static  Double  afCost[ MAX_CU_DEPTH ];
408  static  Int      aiNum [ MAX_CU_DEPTH ];
409
410  if ( rpcBestCU->getAddr() == 0 )
411  {
412    ::memset( afCost, 0, sizeof( afCost ) );
413    ::memset( aiNum,  0, sizeof( aiNum  ) );
414  }
415
416  Bool bBoundary = false;
417  UInt uiLPelX   = rpcBestCU->getCUPelX();
418  UInt uiRPelX   = uiLPelX + rpcBestCU->getWidth(0)  - 1;
419  UInt uiTPelY   = rpcBestCU->getCUPelY();
420  UInt uiBPelY   = uiTPelY + rpcBestCU->getHeight(0) - 1;
421
422  Int iBaseQP = xComputeQP( rpcBestCU, uiDepth );
423  Int iMinQP;
424  Int iMaxQP;
425  Bool isAddLowestQP = false;
426  Int lowestQP = -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY();
427
428  if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
429  {
430    Int idQP = m_pcEncCfg->getMaxDeltaQP();
431    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
432    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
433    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
434    {
435      isAddLowestQP = true; 
436      iMinQP = iMinQP - 1;
437    }
438  }
439  else
440  {
441    iMinQP = rpcTempCU->getQP(0);
442    iMaxQP = rpcTempCU->getQP(0);
443  }
444
445#if RATE_CONTROL_LAMBDA_DOMAIN
446  if ( m_pcEncCfg->getUseRateCtrl() )
447  {
448    iMinQP = m_pcRateCtrl->getRCQP();
449    iMaxQP = m_pcRateCtrl->getRCQP();
450  }
451#else
452  if(m_pcEncCfg->getUseRateCtrl())
453  {
454    Int qp = m_pcRateCtrl->getUnitQP();
455    iMinQP  = Clip3( MIN_QP, MAX_QP, qp);
456    iMaxQP  = Clip3( MIN_QP, MAX_QP, qp);
457  }
458#endif
459#if H_3D_IC
460  Bool bICEnabled = rpcTempCU->getSlice()->getViewIndex() && ( rpcTempCU->getSlice()->getSliceType() == P_SLICE || rpcTempCU->getSlice()->getSliceType() == B_SLICE );
461  bICEnabled = bICEnabled && rpcTempCU->getSlice()->getApplyIC();
462#endif
463  // If slice start or slice end is within this cu...
464  TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx());
465  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurStartCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart();
466  Bool bSliceEnd = (pcSlice->getSliceSegmentCurEndCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurEndCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart());
467  Bool bInsidePicture = ( uiRPelX < rpcBestCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < rpcBestCU->getSlice()->getSPS()->getPicHeightInLumaSamples() );
468  // We need to split, so don't try these modes.
469  if(!bSliceEnd && !bSliceStart && bInsidePicture )
470  {
471    for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
472    {
473      if (isAddLowestQP && (iQP == iMinQP))
474      {
475        iQP = lowestQP;
476      }
477      // variables for fast encoder decision
478      bEarlySkip  = false;
479      bTrySplit    = true;
480      fRD_Skip    = MAX_DOUBLE;
481
482      rpcTempCU->initEstData( uiDepth, iQP );
483#if H_3D_NBDV
484      DisInfo DvInfo; 
485      DvInfo.bDV = false;
486      DvInfo.m_acNBDV.setZero();
487      DvInfo.m_aVIdxCan = 0;
488#if H_3D_NBDV_REF
489      DvInfo.m_acDoNBDV.setZero();
490#endif
491
492      if( rpcTempCU->getSlice()->getSliceType() != I_SLICE )
493      {
494#if H_3D_ARP && H_3D_IV_MERGE
495        if( rpcTempCU->getSlice()->getVPS()->getUseAdvRP(rpcTempCU->getSlice()->getLayerId()) || rpcTempCU->getSlice()->getVPS()->getIvMvPredFlag(rpcTempCU->getSlice()->getLayerId()) )
496#else
497#if H_3D_ARP
498        if( rpcTempCU->getSlice()->getVPS()->getUseAdvRP(rpcTempCU->getSlice()->getLayerId()) )
499#else
500#if H_3D_IV_MERGE
501        if( rpcTempCU->getSlice()->getVPS()->getIvMvPredFlag(rpcTempCU->getSlice()->getLayerId()) )
502#else
503        if (0)
504#endif
505#endif
506#endif
507        {
508          PartSize ePartTemp = rpcTempCU->getPartitionSize(0);
509          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );     
510#if H_3D_NBDV_REF
511          if(rpcTempCU->getSlice()->getVPS()->getDepthRefinementFlag( rpcTempCU->getSlice()->getLayerIdInVps()))
512            DvInfo.bDV = rpcTempCU->getDisMvpCandNBDV(&DvInfo, true);
513          else
514#endif
515            DvInfo.bDV = rpcTempCU->getDisMvpCandNBDV(&DvInfo);
516
517          rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
518          rpcBestCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
519          rpcTempCU->setPartSizeSubParts( ePartTemp, 0, uiDepth );
520        }
521      }
522#endif
523      // do inter modes, SKIP and 2Nx2N
524      if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
525      {
526#if H_3D_IC
527        for( UInt uiICId = 0; uiICId < ( bICEnabled ? 2 : 1 ); uiICId++ )
528        {
529          Bool bICFlag = uiICId ? true : false;
530#endif
531        // 2Nx2N
532        if(m_pcEncCfg->getUseEarlySkipDetection())
533        {
534#if H_3D_IC
535          rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
536#endif
537          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData( uiDepth, iQP );//by Competition for inter_2Nx2N
538        }
539        // SKIP
540#if H_3D_IC
541        rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
542#endif
543        xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode );//by Merge for inter_2Nx2N
544        rpcTempCU->initEstData( uiDepth, iQP );
545
546        // fast encoder decision for early skip
547        if ( m_pcEncCfg->getUseFastEnc() )
548        {
549          Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
550          if ( aiNum [ iIdx ] > 5 && fRD_Skip < EARLY_SKIP_THRES*afCost[ iIdx ]/aiNum[ iIdx ] )
551          {
552            bEarlySkip = true;
553            bTrySplit  = false;
554          }
555        }
556
557        if(!m_pcEncCfg->getUseEarlySkipDetection())
558        {
559          // 2Nx2N, NxN
560          if ( !bEarlySkip )
561          {
562#if H_3D_IC
563            rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
564#endif
565            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData( uiDepth, iQP );
566            if(m_pcEncCfg->getUseCbfFastMode())
567            {
568              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
569            }
570          }
571        }
572#if H_3D_IC
573        }
574#endif
575      }
576
577      if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
578      {
579        if(iQP == iBaseQP)
580        {
581          bTrySplitDQP = bTrySplit;
582        }
583      }
584      else
585      {
586        bTrySplitDQP = bTrySplit;
587      }
588      if (isAddLowestQP && (iQP == lowestQP))
589      {
590        iQP = iMinQP;
591      }
592    }
593
594#if RATE_CONTROL_LAMBDA_DOMAIN
595    if ( uiDepth <= m_addSADDepth )
596    {
597      m_LCUPredictionSAD += m_temporalSAD;
598      m_addSADDepth = uiDepth;
599    }
600#endif
601
602    if(!earlyDetectionSkipMode)
603    {
604      for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
605      {
606        if (isAddLowestQP && (iQP == iMinQP))
607        {
608          iQP = lowestQP;
609        }
610        rpcTempCU->initEstData( uiDepth, iQP );
611
612        // do inter modes, NxN, 2NxN, and Nx2N
613        if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
614        {
615          // 2Nx2N, NxN
616          if ( !bEarlySkip )
617          {
618            if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) ))
619            {
620              if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu)
621              {
622                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN   );
623                rpcTempCU->initEstData( uiDepth, iQP );
624              }
625            }
626          }
627
628          // 2NxN, Nx2N
629          if(doNotBlockPu)
630          {
631            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N  );
632            rpcTempCU->initEstData( uiDepth, iQP );
633            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
634            {
635              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
636            }
637          }
638          if(doNotBlockPu)
639          {
640            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN  );
641            rpcTempCU->initEstData( uiDepth, iQP );
642            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN)
643            {
644              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
645            }
646          }
647
648#if 1
649          //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N)
650          if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) )
651          {
652#if AMP_ENC_SPEEDUP       
653            Bool bTestAMP_Hor = false, bTestAMP_Ver = false;
654
655#if AMP_MRG
656            Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false;
657
658            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver);
659#else
660            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver);
661#endif
662
663            //! Do horizontal AMP
664            if ( bTestAMP_Hor )
665            {
666              if(doNotBlockPu)
667              {
668                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
669                rpcTempCU->initEstData( uiDepth, iQP );
670                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
671                {
672                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
673                }
674              }
675              if(doNotBlockPu)
676              {
677                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
678                rpcTempCU->initEstData( uiDepth, iQP );
679                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
680                {
681                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
682                }
683              }
684            }
685#if AMP_MRG
686            else if ( bTestMergeAMP_Hor ) 
687            {
688              if(doNotBlockPu)
689              {
690                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true );
691                rpcTempCU->initEstData( uiDepth, iQP );
692                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
693                {
694                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
695                }
696              }
697              if(doNotBlockPu)
698              {
699                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true );
700                rpcTempCU->initEstData( uiDepth, iQP );
701                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
702                {
703                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
704                }
705              }
706            }
707#endif
708
709            //! Do horizontal AMP
710            if ( bTestAMP_Ver )
711            {
712              if(doNotBlockPu)
713              {
714                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
715                rpcTempCU->initEstData( uiDepth, iQP );
716                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
717                {
718                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
719                }
720              }
721              if(doNotBlockPu)
722              {
723                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
724                rpcTempCU->initEstData( uiDepth, iQP );
725              }
726            }
727#if AMP_MRG
728            else if ( bTestMergeAMP_Ver )
729            {
730              if(doNotBlockPu)
731              {
732                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true );
733                rpcTempCU->initEstData( uiDepth, iQP );
734                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
735                {
736                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
737                }
738              }
739              if(doNotBlockPu)
740              {
741                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true );
742                rpcTempCU->initEstData( uiDepth, iQP );
743              }
744            }
745#endif
746
747#else
748            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
749            rpcTempCU->initEstData( uiDepth, iQP );
750            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
751            rpcTempCU->initEstData( uiDepth, iQP );
752            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
753            rpcTempCU->initEstData( uiDepth, iQP );
754
755            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
756            rpcTempCU->initEstData( uiDepth, iQP );
757
758#endif
759          }   
760#endif
761        }
762
763        // do normal intra modes
764        if ( !bEarlySkip )
765        {
766          // speedup for inter frames
767          if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || 
768            rpcBestCU->getCbf( 0, TEXT_LUMA     ) != 0   ||
769            rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0   ||
770            rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0     ) // avoid very complex intra if it is unlikely
771          {
772            xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
773            rpcTempCU->initEstData( uiDepth, iQP );
774            if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
775            {
776              if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) )
777              {
778                xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN   );
779                rpcTempCU->initEstData( uiDepth, iQP );
780              }
781            }
782          }
783        }
784
785        // test PCM
786        if(pcPic->getSlice(0)->getSPS()->getUsePCM()
787          && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize())
788          && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) )
789        {
790          UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2;
791          UInt uiBestBits = rpcBestCU->getTotalBits();
792#if H_3D_VSO // M7
793          Double dRDCostTemp = m_pcRdCost->getUseVSO() ? m_pcRdCost->calcRdCostVSO(uiRawBits, 0) : m_pcRdCost->calcRdCost(uiRawBits, 0);
794          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > dRDCostTemp ))
795#else
796          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
797#endif
798          {
799            xCheckIntraPCM (rpcBestCU, rpcTempCU);
800            rpcTempCU->initEstData( uiDepth, iQP );
801          }
802        }
803        if (isAddLowestQP && (iQP == lowestQP))
804        {
805          iQP = iMinQP;
806        }
807      }
808    }
809
810    m_pcEntropyCoder->resetBits();
811    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
812    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
813    if(m_pcEncCfg->getUseSBACRD())
814    {
815      rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
816    }
817
818#if H_3D_VSO // M8
819    if ( m_pcRdCost->getUseVSO() )   
820      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );   
821    else
822#endif
823    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
824
825    // accumulate statistics for early skip
826    if ( m_pcEncCfg->getUseFastEnc() )
827    {
828      if ( rpcBestCU->isSkipped(0) )
829      {
830        Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
831        afCost[ iIdx ] += rpcBestCU->getTotalCost();
832        aiNum [ iIdx ] ++;
833      }
834    }
835
836    // Early CU determination
837    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
838    {
839      bSubBranch = false;
840    }
841    else
842    {
843      bSubBranch = true;
844    }
845  }
846  else if(!(bSliceEnd && bInsidePicture))
847  {
848    bBoundary = true;
849#if RATE_CONTROL_LAMBDA_DOMAIN
850    m_addSADDepth++;
851#endif
852  }
853
854  // copy orginal YUV samples to PCM buffer
855  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
856  {
857    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
858  }
859  if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
860  {
861    Int idQP = m_pcEncCfg->getMaxDeltaQP();
862    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
863    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
864    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
865    {
866      isAddLowestQP = true;
867      iMinQP = iMinQP - 1;     
868    }
869  }
870  else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
871  {
872    iMinQP = iBaseQP;
873    iMaxQP = iBaseQP;
874  }
875  else
876  {
877    Int iStartQP;
878    if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr())
879    {
880      iStartQP = rpcTempCU->getQP(0);
881    }
882    else
883    {
884      UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
885      iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx);
886    }
887    iMinQP = iStartQP;
888    iMaxQP = iStartQP;
889  }
890#if RATE_CONTROL_LAMBDA_DOMAIN
891  if ( m_pcEncCfg->getUseRateCtrl() )
892  {
893    iMinQP = m_pcRateCtrl->getRCQP();
894    iMaxQP = m_pcRateCtrl->getRCQP();
895  }
896#else
897  if(m_pcEncCfg->getUseRateCtrl())
898  {
899    Int qp = m_pcRateCtrl->getUnitQP();
900    iMinQP  = Clip3( MIN_QP, MAX_QP, qp);
901    iMaxQP  = Clip3( MIN_QP, MAX_QP, qp);
902  }
903#endif
904  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
905  {
906    if (isAddLowestQP && (iQP == iMinQP))
907    {
908      iQP = lowestQP;
909    }
910    rpcTempCU->initEstData( uiDepth, iQP );
911
912    // further split
913    if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
914    {
915#if H_3D_VSO // M9
916      // reset Model
917      if( m_pcRdCost->getUseRenModel() )
918      {
919        UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth ( );
920        UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight( );
921        Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr( 0 );
922        UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride();
923        m_pcRdCost->setRenModelData( m_ppcBestCU[uiDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
924      }
925#endif
926
927      UChar       uhNextDepth         = uiDepth+1;
928      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
929      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
930
931      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
932      {
933        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
934        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
935
936        Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr();
937        if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
938        {
939          if( m_bUseSBACRD )
940          {
941            if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
942            {
943              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
944            }
945            else
946            {
947              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
948            }
949          }
950
951#if AMP_ENC_SPEEDUP
952          if ( rpcBestCU->isIntra(0) )
953          {
954            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE );
955          }
956          else
957          {
958            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) );
959          }
960#else
961          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
962#endif
963
964          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
965          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
966        }
967        else if (bInSlice)
968        {
969          pcSubBestPartCU->copyToPic( uhNextDepth );
970          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
971        }
972      }
973
974      if( !bBoundary )
975      {
976        m_pcEntropyCoder->resetBits();
977        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
978
979        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
980        if(m_pcEncCfg->getUseSBACRD())
981        {
982          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
983        }
984      }
985
986#if H_3D_VSO // M10
987      if ( m_pcRdCost->getUseVSO() )
988        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
989      else
990#endif
991      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
992
993      if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
994      {
995        Bool hasResidual = false;
996        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
997        {
998          if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && 
999              ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
1000          {
1001            hasResidual = true;
1002            break;
1003          }
1004        }
1005
1006        UInt uiTargetPartIdx;
1007        if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() )
1008        {
1009          uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1010        }
1011        else
1012        {
1013          uiTargetPartIdx = 0;
1014        }
1015        if ( hasResidual )
1016        {
1017#if !RDO_WITHOUT_DQP_BITS
1018          m_pcEntropyCoder->resetBits();
1019          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
1020          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1021          if(m_pcEncCfg->getUseSBACRD())
1022          {
1023            rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1024          }
1025#if H_3D_VSO // M11
1026          if ( m_pcRdCost->getUseLambdaScaleVSO())         
1027            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );         
1028          else
1029#endif
1030          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1031#endif
1032
1033          Bool foundNonZeroCbf = false;
1034          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf );
1035          assert( foundNonZeroCbf );
1036        }
1037        else
1038        {
1039          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
1040        }
1041      }
1042
1043      if( m_bUseSBACRD )
1044      {
1045        m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1046      }
1047      Bool isEndOfSlice        = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES
1048                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
1049      Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
1050                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
1051      if(isEndOfSlice||isEndOfSliceSegment)
1052      {
1053        rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
1054      }
1055      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth);                                  // RD compare current larger prediction
1056    }                                                                                  // with sub partitioned prediction.
1057    if (isAddLowestQP && (iQP == lowestQP))
1058    {
1059      iQP = iMinQP;
1060    }
1061  }
1062
1063
1064#if H_3D_VSO // M12
1065  if( m_pcRdCost->getUseRenModel() )
1066  {
1067    UInt  uiWidth     = m_ppcRecoYuvBest[uiDepth]->getWidth   ( );
1068    UInt  uiHeight    = m_ppcRecoYuvBest[uiDepth]->getHeight  ( );
1069    Pel*  piSrc       = m_ppcRecoYuvBest[uiDepth]->getLumaAddr( 0 );
1070    UInt  uiSrcStride = m_ppcRecoYuvBest[uiDepth]->getStride  ( );
1071    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1072  }
1073#endif
1074
1075  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
1076
1077  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY );   // Copy Yuv data to picture Yuv
1078  if( bBoundary ||(bSliceEnd && bInsidePicture))
1079  {
1080    return;
1081  }
1082
1083  // Assert if Best prediction mode is NONE
1084  // Selected mode's RD-cost must be not MAX_DOUBLE.
1085  assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE  );
1086  assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE  );
1087  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE );
1088}
1089
1090/** finish encoding a cu and handle end-of-slice conditions
1091 * \param pcCU
1092 * \param uiAbsPartIdx
1093 * \param uiDepth
1094 * \returns Void
1095 */
1096Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1097{
1098  TComPic* pcPic = pcCU->getPic();
1099  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1100
1101  //Calculate end address
1102  UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx;
1103
1104  UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU();
1105  UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU();
1106  UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1107  UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1108  UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
1109  UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
1110  while(uiPosX>=uiWidth||uiPosY>=uiHeight)
1111  {
1112    uiInternalAddress--;
1113    uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1114    uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1115  }
1116  uiInternalAddress++;
1117  if(uiInternalAddress==pcCU->getPic()->getNumPartInCU())
1118  {
1119    uiInternalAddress = 0;
1120    uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1);
1121  }
1122  UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress);
1123
1124  // Encode slice finish
1125  Bool bTerminateSlice = false;
1126  if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress)
1127  {
1128    bTerminateSlice = true;
1129  }
1130  UInt uiGranularityWidth = g_uiMaxCUWidth;
1131  uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1132  uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1133  Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth))
1134    &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight));
1135 
1136  if(granularityBoundary)
1137  {
1138    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1139    if (!bTerminateSlice)
1140      m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 );
1141  }
1142 
1143  Int numberOfWrittenBits = 0;
1144  if (m_pcBitCounter)
1145  {
1146    numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits();
1147  }
1148 
1149  // Calculate slice end IF this CU puts us over slice bit size.
1150  UInt iGranularitySize = pcCU->getPic()->getNumPartInCU();
1151  Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize;
1152  if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) 
1153  {
1154    iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)));
1155  }
1156  // Set slice end parameter
1157  if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) 
1158  {
1159    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1160    pcSlice->setSliceCurEndCUAddr(iGranularityEnd);
1161    return;
1162  }
1163  // Set dependent slice end parameter
1164  if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) 
1165  {
1166    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1167    return;
1168  }
1169  if(granularityBoundary)
1170  {
1171    pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) );
1172    pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits);
1173    if (m_pcBitCounter)
1174    {
1175      m_pcEntropyCoder->resetBits();     
1176    }
1177  }
1178}
1179
1180/** Compute QP for each CU
1181 * \param pcCU Target CU
1182 * \param uiDepth CU depth
1183 * \returns quantization parameter
1184 */
1185Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1186{
1187  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1188  Int iQpOffset = 0;
1189  if ( m_pcEncCfg->getUseAdaptiveQP() )
1190  {
1191    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1192    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1193    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1194    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1195    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1196    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1197    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1198
1199    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1200    Double dAvgAct = pcAQLayer->getAvgActivity();
1201    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1202    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1203    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1204    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1205  }
1206  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset );
1207}
1208
1209/** encode a CU block recursively
1210 * \param pcCU
1211 * \param uiAbsPartIdx
1212 * \param uiDepth
1213 * \returns Void
1214 */
1215Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1216{
1217  TComPic* pcPic = pcCU->getPic();
1218 
1219  Bool bBoundary = false;
1220  UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1221  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1222  UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1223  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1224 
1225#if H_MV_ENC_DEC_TRAC
1226  DTRACE_CU_S("=========== coding_quadtree ===========\n")
1227  DTRACE_CU("x0", uiLPelX)
1228  DTRACE_CU("x1", uiTPelY)
1229  DTRACE_CU("log2CbSize", g_uiMaxCUWidth>>uiDepth)
1230  DTRACE_CU("cqtDepth"  , uiDepth)
1231#endif
1232
1233  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1234  // If slice start is within this cu...
1235  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1236    pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) );
1237  // We need to split, so don't try these modes.
1238  if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1239  {
1240    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1241  }
1242  else
1243  {
1244    bBoundary = true;
1245  }
1246 
1247  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary )
1248  {
1249    UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2;
1250    if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1251    {
1252      setdQPFlag(true);
1253    }
1254    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1255    {
1256      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1257      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1258      Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr();
1259      if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1260      {
1261        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1262      }
1263    }
1264    return;
1265  }
1266 
1267#if H_MV_ENC_DEC_TRAC
1268  DTRACE_CU_S("=========== coding_unit ===========\n")
1269#endif
1270
1271  if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1272  {
1273    setdQPFlag(true);
1274  }
1275  if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1276  {
1277    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1278  }
1279  if( !pcCU->getSlice()->isIntra() )
1280  {
1281    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1282  }
1283 
1284  if( pcCU->isSkipped( uiAbsPartIdx ) )
1285  {
1286#if H_MV_ENC_DEC_TRAC
1287    DTRACE_PU_S("=========== prediction_unit ===========\n")
1288    DTRACE_PU("x0", uiLPelX)
1289    DTRACE_PU("x1", uiTPelY)
1290#endif
1291    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1292#if H_3D_IC
1293    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1294#endif
1295#if H_3D_ARP
1296    m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1297#endif
1298    finishCU(pcCU,uiAbsPartIdx,uiDepth);
1299    return;
1300  }
1301  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1302 
1303  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1304 
1305  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1306  {
1307    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1308
1309    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1310    {
1311      // Encode slice finish
1312      finishCU(pcCU,uiAbsPartIdx,uiDepth);
1313      return;
1314    }
1315  }
1316
1317  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1318  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1319#if H_3D_IC
1320  m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1321#endif
1322#if H_3D_ARP
1323  m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1324#endif
1325
1326  // Encode Coefficients
1327  Bool bCodeDQP = getdQPFlag();
1328  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP );
1329  setdQPFlag( bCodeDQP );
1330
1331  // --- write terminating bit ---
1332  finishCU(pcCU,uiAbsPartIdx,uiDepth);
1333}
1334
1335/** check RD costs for a CU block encoded with merge
1336 * \param rpcBestCU
1337 * \param rpcTempCU
1338 * \returns Void
1339 */
1340Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode )
1341{
1342  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1343#if H_3D_IV_MERGE
1344  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS_MEM << 1]; // double length for mv of both lists
1345  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS_MEM];
1346#else
1347  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists
1348  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1349#endif
1350  Int numValidMergeCand = 0;
1351
1352  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1353  {
1354    uhInterDirNeighbours[ui] = 0;
1355  }
1356  UChar uhDepth = rpcTempCU->getDepth( 0 );
1357#if H_3D_IC
1358  Bool bICFlag = rpcTempCU->getICFlag( 0 );
1359#endif
1360#if H_3D_VSO // M1  //nececcary here?
1361  if( m_pcRdCost->getUseRenModel() )
1362  {
1363    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1364    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1365    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1366    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1367    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1368  }
1369#endif
1370
1371  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1372  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth );
1373
1374#if H_3D_VSP
1375  Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1376  memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1377  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag, numValidMergeCand );
1378#else
1379  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1380#endif
1381
1382#if H_3D_IV_MERGE
1383  Int mergeCandBuffer[MRG_MAX_NUM_CANDS_MEM];
1384#else
1385  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1386#endif
1387  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1388  {
1389    mergeCandBuffer[ui] = 0;
1390  }
1391
1392  Bool bestIsSkip = false;
1393
1394  UInt iteration;
1395  if ( rpcTempCU->isLosslessCoded(0))
1396  {
1397    iteration = 1;
1398  }
1399  else 
1400  {
1401    iteration = 2;
1402  }
1403
1404#if H_3D_ARP
1405  Int nARPWMax = rpcTempCU->getSlice()->getARPStepNum() - 1;
1406  if( nARPWMax < 0 || !rpcTempCU->getDvInfo(0).bDV )
1407  {
1408    nARPWMax = 0;
1409  }
1410  for( Int nARPW=nARPWMax; nARPW >= 0 ; nARPW-- )
1411  {
1412    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS*sizeof(Int) );
1413#endif
1414  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1415  {
1416    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1417    {
1418      {
1419#if H_3D_IC
1420        if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
1421        {
1422          if( bICFlag && uiMergeCand == 0 ) 
1423          {
1424            continue;
1425          }
1426        }
1427#endif
1428        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1429        {
1430
1431        if( !(bestIsSkip && uiNoResidual == 0) )
1432        {
1433          // set MC parameters
1434          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1435          rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(),     0, uhDepth );
1436          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1437#if H_3D_IC
1438          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1439#endif
1440#if H_3D_ARP
1441          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1442#endif
1443          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1444          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1445#if H_3D_VSP
1446          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
1447#endif
1448          rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1449          rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1450          rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1451
1452#if H_3D_ARP
1453          if( nARPW )
1454          {
1455            Bool bSignalflag[2] = { true, true };
1456            for( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1457            {
1458              Int iRefIdx = cMvFieldNeighbours[uiRefListIdx + 2*uiMergeCand].getRefIdx();
1459              RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1460              if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1461              {
1462                bSignalflag[uiRefListIdx] = false;
1463              }
1464            }
1465            if( !bSignalflag[0] && !bSignalflag[1] )
1466            {
1467              rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1468            }
1469          }
1470#endif
1471       // do MC
1472       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1473       // estimate residual and encode everything
1474
1475#if H_3D_VSO //M2
1476       if( m_pcRdCost->getUseRenModel() )
1477       { //Reset
1478         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1479         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1480         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1481         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1482         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1483       }
1484#endif
1485
1486       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1487         m_ppcOrigYuv    [uhDepth],
1488         m_ppcPredYuvTemp[uhDepth],
1489         m_ppcResiYuvTemp[uhDepth],
1490         m_ppcResiYuvBest[uhDepth],
1491         m_ppcRecoYuvTemp[uhDepth],
1492         (uiNoResidual? true:false));
1493
1494
1495       if(uiNoResidual==0)
1496       {
1497         if(rpcTempCU->getQtRootCbf(0) == 0)
1498         {
1499           mergeCandBuffer[uiMergeCand] = 1;
1500         }
1501       }
1502
1503       rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
1504          Int orgQP = rpcTempCU->getQP( 0 );
1505          xCheckDQP( rpcTempCU );
1506          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1507          rpcTempCU->initEstData( uhDepth, orgQP );
1508
1509
1510      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
1511      {
1512        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
1513      }
1514
1515    }
1516    }
1517   }
1518  }
1519
1520  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
1521  {
1522    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
1523    {
1524      if( rpcBestCU->getMergeFlag( 0 ))
1525      {
1526        *earlyDetectionSkipMode = true;
1527      }
1528      else
1529      {
1530        Int absoulte_MV=0;
1531        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1532        {
1533          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1534          {
1535            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
1536            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
1537            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
1538            absoulte_MV+=iHor+iVer;
1539          }
1540        }
1541
1542        if(absoulte_MV == 0)
1543        {
1544          *earlyDetectionSkipMode = true;
1545        }
1546      }
1547    }
1548  }
1549 }
1550#if H_3D_ARP
1551 }
1552#endif
1553}
1554
1555
1556#if AMP_MRG
1557Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
1558#else
1559Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
1560#endif
1561{
1562  UChar uhDepth = rpcTempCU->getDepth( 0 );
1563#if H_3D_ARP
1564  Int iLayerId    = rpcTempCU->getSlice()->getLayerId();
1565  Bool bFirstTime = true;
1566  Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
1567
1568  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || !rpcTempCU->getDvInfo(0).bDV  )
1569  {
1570    nARPWMax = 0;
1571  }
1572
1573  for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
1574  {
1575    if( bFirstTime == false && rpcTempCU->getSlice()->getVPS()->getUseAdvRP( iLayerId ) )
1576    {
1577      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0) );
1578    }
1579#endif
1580#if H_3D_VSO // M3
1581  if( m_pcRdCost->getUseRenModel() )
1582  {
1583    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1584    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1585    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1586    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1587    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1588  }
1589#endif
1590
1591  rpcTempCU->setDepthSubParts( uhDepth, 0 );
1592 
1593  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
1594
1595  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
1596  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
1597  rpcTempCU->setCUTransquantBypassSubParts  ( m_pcEncCfg->getCUTransquantBypassFlagValue(),      0, uhDepth );
1598 
1599#if H_3D_ARP
1600  rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1601#endif
1602
1603#if H_3D_ARP
1604  if( bFirstTime == false && nARPWMax )
1605  {
1606    rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
1607    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1608
1609    m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
1610
1611    if(rpcTempCU->getPartitionSize(0)==SIZE_2Nx2N)
1612    {
1613      Bool bSignalflag[2] = { true, true };
1614      for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1615      {
1616        RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1617        Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
1618        if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1619        {
1620          bSignalflag[uiRefListIdx] = false;
1621        }
1622      }
1623      if( !bSignalflag[0] && !bSignalflag[1] )
1624      {
1625        rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1626      }
1627    }
1628  }
1629  else
1630  {
1631    bFirstTime = false;
1632#endif
1633#if AMP_MRG
1634  rpcTempCU->setMergeAMP (true);
1635  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
1636#else 
1637  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1638#endif
1639#if H_3D_ARP
1640   if( nARPWMax )
1641   {
1642     m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
1643
1644     Bool bSignalflag[2] = { true, true };
1645     for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1646     {
1647       RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1648       Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
1649       if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1650       {
1651         bSignalflag[uiRefListIdx] = false;
1652       }
1653     }
1654     if( !bSignalflag[0] && !bSignalflag[1])
1655     {
1656       rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1657     }
1658   }
1659  }
1660#endif
1661
1662#if AMP_MRG
1663  if ( !rpcTempCU->getMergeAMP() )
1664  {
1665#if H_3D_ARP
1666    if( nARPWMax )
1667    {
1668      continue;
1669    }
1670    else
1671#endif
1672    return;
1673  }
1674#endif
1675
1676#if RATE_CONTROL_LAMBDA_DOMAIN
1677  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
1678  {
1679    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
1680      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
1681      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
1682    m_temporalSAD = (Int)SAD;
1683  }
1684#endif
1685
1686  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
1687
1688
1689#if H_3D_VSO // M4
1690  if( m_pcRdCost->getUseLambdaScaleVSO() )
1691    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1692  else
1693#endif
1694  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1695
1696  xCheckDQP( rpcTempCU );
1697  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1698#if H_3D_ARP
1699  }
1700#endif
1701}
1702
1703Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
1704{
1705  UInt uiDepth = rpcTempCU->getDepth( 0 );
1706 
1707#if H_3D_VSO // M5
1708  if( m_pcRdCost->getUseRenModel() )
1709  {
1710    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
1711    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
1712    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
1713    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
1714    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1715  }
1716#endif
1717
1718  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1719
1720  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
1721  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1722  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
1723 
1724  Bool bSeparateLumaChroma = true; // choose estimation mode
1725  UInt uiPreCalcDistC      = 0;
1726  if( !bSeparateLumaChroma )
1727  {
1728    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
1729  }
1730  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
1731
1732  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
1733 
1734  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
1735 
1736  m_pcEntropyCoder->resetBits();
1737  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1738  {
1739    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1740  }
1741  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1742  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
1743  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
1744  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
1745  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
1746
1747  // Encode Coefficients
1748  Bool bCodeDQP = getdQPFlag();
1749  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
1750  setdQPFlag( bCodeDQP );
1751 
1752  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1753 
1754  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1755  if(m_pcEncCfg->getUseSBACRD())
1756  {
1757    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1758  }
1759
1760#if H_3D_VSO // M6
1761  if( m_pcRdCost->getUseLambdaScaleVSO()) 
1762    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
1763  else
1764#endif
1765  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1766 
1767  xCheckDQP( rpcTempCU );
1768  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
1769}
1770
1771/** Check R-D costs for a CU with PCM mode.
1772 * \param rpcBestCU pointer to best mode CU data structure
1773 * \param rpcTempCU pointer to testing mode CU data structure
1774 * \returns Void
1775 *
1776 * \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.
1777 */
1778Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
1779{
1780  UInt uiDepth = rpcTempCU->getDepth( 0 );
1781
1782  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1783
1784  rpcTempCU->setIPCMFlag(0, true);
1785  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
1786  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
1787  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1788  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
1789  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
1790
1791  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
1792
1793  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1794
1795  m_pcEntropyCoder->resetBits();
1796  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1797  {
1798    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1799  }
1800  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1801  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
1802  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
1803  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
1804
1805  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1806
1807  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1808  if(m_pcEncCfg->getUseSBACRD())
1809  {
1810    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1811  }
1812#if H_3D_VSO // M44
1813  if ( m_pcRdCost->getUseVSO() )
1814    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1815  else
1816#endif
1817  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1818
1819  xCheckDQP( rpcTempCU );
1820  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
1821}
1822
1823/** check whether current try is the best with identifying the depth of current try
1824 * \param rpcBestCU
1825 * \param rpcTempCU
1826 * \returns Void
1827 */
1828Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
1829{
1830  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
1831  {
1832    TComYuv* pcYuv;
1833    // Change Information data
1834    TComDataCU* pcCU = rpcBestCU;
1835    rpcBestCU = rpcTempCU;
1836    rpcTempCU = pcCU;
1837
1838    // Change Prediction data
1839    pcYuv = m_ppcPredYuvBest[uiDepth];
1840    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
1841    m_ppcPredYuvTemp[uiDepth] = pcYuv;
1842
1843    // Change Reconstruction data
1844    pcYuv = m_ppcRecoYuvBest[uiDepth];
1845    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
1846    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
1847
1848    pcYuv = NULL;
1849    pcCU  = NULL;
1850
1851    if( m_bUseSBACRD )  // store temp best CI for next CU coding
1852      m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
1853  }
1854}
1855
1856Void TEncCu::xCheckDQP( TComDataCU* pcCU )
1857{
1858  UInt uiDepth = pcCU->getDepth( 0 );
1859
1860  if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() )
1861  {
1862    if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) )
1863    {
1864#if !RDO_WITHOUT_DQP_BITS
1865      m_pcEntropyCoder->resetBits();
1866      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
1867      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1868      if(m_pcEncCfg->getUseSBACRD())
1869      {
1870        pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1871      }
1872#if H_3D_VSO // M45
1873      if ( m_pcRdCost->getUseVSO() )     
1874        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
1875      else
1876#endif
1877      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
1878#endif
1879    }
1880    else
1881    {
1882      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
1883    }
1884  }
1885}
1886
1887Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
1888{
1889  pDst->iN = pSrc->iN;
1890  for (Int i = 0; i < pSrc->iN; i++)
1891  {
1892    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
1893  }
1894}
1895Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY )
1896{
1897  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1898  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1899  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1900  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1901    pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
1902  Bool bSliceEnd   = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1903    pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
1904  if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1905  {
1906    UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
1907    UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth);
1908    UInt uiBlkWidth    = rpcPic->getNumPartInWidth() >> (uiDepth);
1909    UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
1910    UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
1911    UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
1912    m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
1913  }
1914  else
1915  {
1916    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2;
1917
1918    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1919    {
1920      UInt uiSubCULPelX   = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx &  1 );
1921      UInt uiSubCUTPelY   = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 );
1922
1923      Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && 
1924        rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr();
1925      if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1926      {
1927        xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY );   // Copy Yuv data to picture Yuv
1928      }
1929    }
1930  }
1931}
1932
1933Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
1934{
1935  UInt uiCurrDepth = uiNextDepth - 1;
1936  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
1937}
1938
1939/** Function for filling the PCM buffer of a CU using its original sample array
1940 * \param pcCU pointer to current CU
1941 * \param pcOrgYuv pointer to original sample array
1942 * \returns Void
1943 */
1944Void TEncCu::xFillPCMBuffer     ( TComDataCU*& pCU, TComYuv* pOrgYuv )
1945{
1946
1947  UInt   width        = pCU->getWidth(0);
1948  UInt   height       = pCU->getHeight(0);
1949
1950  Pel*   pSrcY = pOrgYuv->getLumaAddr(0, width); 
1951  Pel*   pDstY = pCU->getPCMSampleY();
1952  UInt   srcStride = pOrgYuv->getStride();
1953
1954  for(Int y = 0; y < height; y++ )
1955  {
1956    for(Int x = 0; x < width; x++ )
1957    {
1958      pDstY[x] = pSrcY[x];
1959    }
1960    pDstY += width;
1961    pSrcY += srcStride;
1962  }
1963
1964  Pel* pSrcCb       = pOrgYuv->getCbAddr();
1965  Pel* pSrcCr       = pOrgYuv->getCrAddr();;
1966
1967  Pel* pDstCb       = pCU->getPCMSampleCb();
1968  Pel* pDstCr       = pCU->getPCMSampleCr();;
1969
1970  UInt srcStrideC = pOrgYuv->getCStride();
1971  UInt heightC   = height >> 1;
1972  UInt widthC    = width  >> 1;
1973
1974  for(Int y = 0; y < heightC; y++ )
1975  {
1976    for(Int x = 0; x < widthC; x++ )
1977    {
1978      pDstCb[x] = pSrcCb[x];
1979      pDstCr[x] = pSrcCr[x];
1980    }
1981    pDstCb += widthC;
1982    pDstCr += widthC;
1983    pSrcCb += srcStrideC;
1984    pSrcCr += srcStrideC;
1985  }
1986}
1987
1988#if ADAPTIVE_QP_SELECTION
1989/** Collect ARL statistics from one block
1990  */
1991Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
1992{
1993  for( Int n = 0; n < NumCoeffInCU; n++ )
1994  {
1995    Int u = abs( rpcCoeff[ n ] );
1996    Int absc = rpcArlCoeff[ n ];
1997
1998    if( u != 0 )
1999    {
2000      if( u < LEVEL_RANGE )
2001      {
2002        cSum[ u ] += ( Double )absc;
2003        numSamples[ u ]++;
2004      }
2005      else 
2006      {
2007        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
2008        numSamples[ LEVEL_RANGE ]++;
2009      }
2010    }
2011  }
2012
2013  return 0;
2014}
2015
2016/** Collect ARL statistics from one LCU
2017 * \param pcCU
2018 */
2019Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU )
2020{
2021  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to datatype and quantization output
2022  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output
2023
2024  TCoeff* pCoeffY = rpcCU->getCoeffY();
2025  Int* pArlCoeffY = rpcCU->getArlCoeffY();
2026
2027  UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth;
2028  UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;
2029
2030  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
2031  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
2032
2033  // Collect stats to cSum[][] and numSamples[][]
2034  for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ )
2035  {
2036    UInt uiTrIdx = rpcCU->getTransformIdx(i);
2037
2038    if(rpcCU->getPredictionMode(i) == MODE_INTER)
2039    if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) )
2040    {
2041      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
2042    }//Note that only InterY is processed. QP rounding is based on InterY data only.
2043   
2044    pCoeffY  += uiMinNumCoeffInCU;
2045    pArlCoeffY  += uiMinNumCoeffInCU;
2046  }
2047
2048  for(Int u=1; u<LEVEL_RANGE;u++)
2049  {
2050    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
2051    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
2052  }
2053  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
2054  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
2055}
2056#endif
2057//! \}
Note: See TracBrowser for help on using the repository browser.