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

Last change on this file since 468 was 468, checked in by lg, 11 years ago

1.IC and full pel depth coding are integrated and is guarded by Macro H_3D_IC.

  • Property svn:eol-style set to native
File size: 72.0 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
495        if( rpcTempCU->getSlice()->getVPS()->getUseAdvRP(rpcTempCU->getSlice()->getLayerId()) )
496#else
497        if(rpcTempCU->getSlice()->getViewIndex() && !rpcTempCU->getSlice()->getIsDepth()) //Notes from QC: this condition shall be changed once the configuration is completed, e.g. in pcSlice->getSPS()->getMultiviewMvPredMode() || ARP in prev. HTM. Remove this comment once it is done. Remove this comment once it is done.
498#endif
499        {
500          PartSize ePartTemp = rpcTempCU->getPartitionSize(0);
501          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );     
502#if H_3D_NBDV_REF
503          if(m_pcSlice->getSPS()->getUseDVPRefine())  //Notes from QC: please check the condition for DoNBDV. Remove this comment once it is done.
504            DvInfo.bDV = rpcTempCU->getDisMvpCandNBDV(&DvInfo, true);
505          else
506#endif
507            DvInfo.bDV = rpcTempCU->getDisMvpCandNBDV(&DvInfo);
508
509          rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
510          rpcBestCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
511          rpcTempCU->setPartSizeSubParts( ePartTemp, 0, uiDepth );
512        }
513      }
514#endif
515      // do inter modes, SKIP and 2Nx2N
516      if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
517      {
518#if H_3D_IC
519        for( UInt uiICId = 0; uiICId < ( bICEnabled ? 2 : 1 ); uiICId++ )
520        {
521          Bool bICFlag = uiICId ? true : false;
522#endif
523        // 2Nx2N
524        if(m_pcEncCfg->getUseEarlySkipDetection())
525        {
526#if H_3D_IC
527          rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
528#endif
529          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData( uiDepth, iQP );//by Competition for inter_2Nx2N
530        }
531        // SKIP
532#if H_3D_IC
533        rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
534#endif
535        xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode );//by Merge for inter_2Nx2N
536        rpcTempCU->initEstData( uiDepth, iQP );
537
538        // fast encoder decision for early skip
539        if ( m_pcEncCfg->getUseFastEnc() )
540        {
541          Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
542          if ( aiNum [ iIdx ] > 5 && fRD_Skip < EARLY_SKIP_THRES*afCost[ iIdx ]/aiNum[ iIdx ] )
543          {
544            bEarlySkip = true;
545            bTrySplit  = false;
546          }
547        }
548
549        if(!m_pcEncCfg->getUseEarlySkipDetection())
550        {
551          // 2Nx2N, NxN
552          if ( !bEarlySkip )
553          {
554#if H_3D_IC
555            rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
556#endif
557            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData( uiDepth, iQP );
558            if(m_pcEncCfg->getUseCbfFastMode())
559            {
560              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
561            }
562          }
563        }
564#if H_3D_IC
565        }
566#endif
567      }
568
569      if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
570      {
571        if(iQP == iBaseQP)
572        {
573          bTrySplitDQP = bTrySplit;
574        }
575      }
576      else
577      {
578        bTrySplitDQP = bTrySplit;
579      }
580      if (isAddLowestQP && (iQP == lowestQP))
581      {
582        iQP = iMinQP;
583      }
584    }
585
586#if RATE_CONTROL_LAMBDA_DOMAIN
587    if ( uiDepth <= m_addSADDepth )
588    {
589      m_LCUPredictionSAD += m_temporalSAD;
590      m_addSADDepth = uiDepth;
591    }
592#endif
593
594    if(!earlyDetectionSkipMode)
595    {
596      for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
597      {
598        if (isAddLowestQP && (iQP == iMinQP))
599        {
600          iQP = lowestQP;
601        }
602        rpcTempCU->initEstData( uiDepth, iQP );
603
604        // do inter modes, NxN, 2NxN, and Nx2N
605        if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
606        {
607          // 2Nx2N, NxN
608          if ( !bEarlySkip )
609          {
610            if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) ))
611            {
612              if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu)
613              {
614                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN   );
615                rpcTempCU->initEstData( uiDepth, iQP );
616              }
617            }
618          }
619
620          // 2NxN, Nx2N
621          if(doNotBlockPu)
622          {
623            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N  );
624            rpcTempCU->initEstData( uiDepth, iQP );
625            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
626            {
627              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
628            }
629          }
630          if(doNotBlockPu)
631          {
632            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN  );
633            rpcTempCU->initEstData( uiDepth, iQP );
634            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN)
635            {
636              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
637            }
638          }
639
640#if 1
641          //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N)
642          if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) )
643          {
644#if AMP_ENC_SPEEDUP       
645            Bool bTestAMP_Hor = false, bTestAMP_Ver = false;
646
647#if AMP_MRG
648            Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false;
649
650            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver);
651#else
652            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver);
653#endif
654
655            //! Do horizontal AMP
656            if ( bTestAMP_Hor )
657            {
658              if(doNotBlockPu)
659              {
660                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
661                rpcTempCU->initEstData( uiDepth, iQP );
662                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
663                {
664                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
665                }
666              }
667              if(doNotBlockPu)
668              {
669                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
670                rpcTempCU->initEstData( uiDepth, iQP );
671                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
672                {
673                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
674                }
675              }
676            }
677#if AMP_MRG
678            else if ( bTestMergeAMP_Hor ) 
679            {
680              if(doNotBlockPu)
681              {
682                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true );
683                rpcTempCU->initEstData( uiDepth, iQP );
684                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
685                {
686                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
687                }
688              }
689              if(doNotBlockPu)
690              {
691                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true );
692                rpcTempCU->initEstData( uiDepth, iQP );
693                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
694                {
695                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
696                }
697              }
698            }
699#endif
700
701            //! Do horizontal AMP
702            if ( bTestAMP_Ver )
703            {
704              if(doNotBlockPu)
705              {
706                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
707                rpcTempCU->initEstData( uiDepth, iQP );
708                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
709                {
710                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
711                }
712              }
713              if(doNotBlockPu)
714              {
715                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
716                rpcTempCU->initEstData( uiDepth, iQP );
717              }
718            }
719#if AMP_MRG
720            else if ( bTestMergeAMP_Ver )
721            {
722              if(doNotBlockPu)
723              {
724                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true );
725                rpcTempCU->initEstData( uiDepth, iQP );
726                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
727                {
728                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
729                }
730              }
731              if(doNotBlockPu)
732              {
733                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true );
734                rpcTempCU->initEstData( uiDepth, iQP );
735              }
736            }
737#endif
738
739#else
740            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
741            rpcTempCU->initEstData( uiDepth, iQP );
742            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
743            rpcTempCU->initEstData( uiDepth, iQP );
744            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
745            rpcTempCU->initEstData( uiDepth, iQP );
746
747            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
748            rpcTempCU->initEstData( uiDepth, iQP );
749
750#endif
751          }   
752#endif
753        }
754
755        // do normal intra modes
756        if ( !bEarlySkip )
757        {
758          // speedup for inter frames
759          if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || 
760            rpcBestCU->getCbf( 0, TEXT_LUMA     ) != 0   ||
761            rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0   ||
762            rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0     ) // avoid very complex intra if it is unlikely
763          {
764            xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
765            rpcTempCU->initEstData( uiDepth, iQP );
766            if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
767            {
768              if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) )
769              {
770                xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN   );
771                rpcTempCU->initEstData( uiDepth, iQP );
772              }
773            }
774          }
775        }
776
777        // test PCM
778        if(pcPic->getSlice(0)->getSPS()->getUsePCM()
779          && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize())
780          && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) )
781        {
782          UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2;
783          UInt uiBestBits = rpcBestCU->getTotalBits();
784#if H_3D_VSO // M7
785          Double dRDCostTemp = m_pcRdCost->getUseVSO() ? m_pcRdCost->calcRdCostVSO(uiRawBits, 0) : m_pcRdCost->calcRdCost(uiRawBits, 0);
786          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > dRDCostTemp ))
787#else
788          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
789#endif
790          {
791            xCheckIntraPCM (rpcBestCU, rpcTempCU);
792            rpcTempCU->initEstData( uiDepth, iQP );
793          }
794        }
795        if (isAddLowestQP && (iQP == lowestQP))
796        {
797          iQP = iMinQP;
798        }
799      }
800    }
801
802    m_pcEntropyCoder->resetBits();
803    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
804    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
805    if(m_pcEncCfg->getUseSBACRD())
806    {
807      rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
808    }
809
810#if H_3D_VSO // M8
811    if ( m_pcRdCost->getUseVSO() )   
812      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );   
813    else
814#endif
815    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
816
817    // accumulate statistics for early skip
818    if ( m_pcEncCfg->getUseFastEnc() )
819    {
820      if ( rpcBestCU->isSkipped(0) )
821      {
822        Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ];
823        afCost[ iIdx ] += rpcBestCU->getTotalCost();
824        aiNum [ iIdx ] ++;
825      }
826    }
827
828    // Early CU determination
829    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
830    {
831      bSubBranch = false;
832    }
833    else
834    {
835      bSubBranch = true;
836    }
837  }
838  else if(!(bSliceEnd && bInsidePicture))
839  {
840    bBoundary = true;
841#if RATE_CONTROL_LAMBDA_DOMAIN
842    m_addSADDepth++;
843#endif
844  }
845
846  // copy orginal YUV samples to PCM buffer
847  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
848  {
849    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
850  }
851  if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
852  {
853    Int idQP = m_pcEncCfg->getMaxDeltaQP();
854    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
855    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
856    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
857    {
858      isAddLowestQP = true;
859      iMinQP = iMinQP - 1;     
860    }
861  }
862  else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
863  {
864    iMinQP = iBaseQP;
865    iMaxQP = iBaseQP;
866  }
867  else
868  {
869    Int iStartQP;
870    if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr())
871    {
872      iStartQP = rpcTempCU->getQP(0);
873    }
874    else
875    {
876      UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
877      iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx);
878    }
879    iMinQP = iStartQP;
880    iMaxQP = iStartQP;
881  }
882#if RATE_CONTROL_LAMBDA_DOMAIN
883  if ( m_pcEncCfg->getUseRateCtrl() )
884  {
885    iMinQP = m_pcRateCtrl->getRCQP();
886    iMaxQP = m_pcRateCtrl->getRCQP();
887  }
888#else
889  if(m_pcEncCfg->getUseRateCtrl())
890  {
891    Int qp = m_pcRateCtrl->getUnitQP();
892    iMinQP  = Clip3( MIN_QP, MAX_QP, qp);
893    iMaxQP  = Clip3( MIN_QP, MAX_QP, qp);
894  }
895#endif
896  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
897  {
898    if (isAddLowestQP && (iQP == iMinQP))
899    {
900      iQP = lowestQP;
901    }
902    rpcTempCU->initEstData( uiDepth, iQP );
903
904    // further split
905    if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
906    {
907#if H_3D_VSO // M9
908      // reset Model
909      if( m_pcRdCost->getUseRenModel() )
910      {
911        UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth ( );
912        UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight( );
913        Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr( 0 );
914        UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride();
915        m_pcRdCost->setRenModelData( m_ppcBestCU[uiDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
916      }
917#endif
918
919      UChar       uhNextDepth         = uiDepth+1;
920      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
921      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
922
923      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
924      {
925        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
926        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
927
928        Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr();
929        if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
930        {
931          if( m_bUseSBACRD )
932          {
933            if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
934            {
935              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
936            }
937            else
938            {
939              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
940            }
941          }
942
943#if AMP_ENC_SPEEDUP
944          if ( rpcBestCU->isIntra(0) )
945          {
946            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE );
947          }
948          else
949          {
950            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) );
951          }
952#else
953          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
954#endif
955
956          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
957          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
958        }
959        else if (bInSlice)
960        {
961          pcSubBestPartCU->copyToPic( uhNextDepth );
962          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
963        }
964      }
965
966      if( !bBoundary )
967      {
968        m_pcEntropyCoder->resetBits();
969        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
970
971        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
972        if(m_pcEncCfg->getUseSBACRD())
973        {
974          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
975        }
976      }
977
978#if H_3D_VSO // M10
979      if ( m_pcRdCost->getUseVSO() )
980        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
981      else
982#endif
983      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
984
985      if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
986      {
987        Bool hasResidual = false;
988        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
989        {
990          if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && 
991              ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
992          {
993            hasResidual = true;
994            break;
995          }
996        }
997
998        UInt uiTargetPartIdx;
999        if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() )
1000        {
1001          uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1002        }
1003        else
1004        {
1005          uiTargetPartIdx = 0;
1006        }
1007        if ( hasResidual )
1008        {
1009#if !RDO_WITHOUT_DQP_BITS
1010          m_pcEntropyCoder->resetBits();
1011          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
1012          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1013          if(m_pcEncCfg->getUseSBACRD())
1014          {
1015            rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1016          }
1017#if H_3D_VSO // M11
1018          if ( m_pcRdCost->getUseLambdaScaleVSO())         
1019            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );         
1020          else
1021#endif
1022          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1023#endif
1024
1025          Bool foundNonZeroCbf = false;
1026          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf );
1027          assert( foundNonZeroCbf );
1028        }
1029        else
1030        {
1031          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
1032        }
1033      }
1034
1035      if( m_bUseSBACRD )
1036      {
1037        m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1038      }
1039      Bool isEndOfSlice        = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES
1040                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
1041      Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
1042                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
1043      if(isEndOfSlice||isEndOfSliceSegment)
1044      {
1045        rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
1046      }
1047      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth);                                  // RD compare current larger prediction
1048    }                                                                                  // with sub partitioned prediction.
1049    if (isAddLowestQP && (iQP == lowestQP))
1050    {
1051      iQP = iMinQP;
1052    }
1053  }
1054
1055
1056#if H_3D_VSO // M12
1057  if( m_pcRdCost->getUseRenModel() )
1058  {
1059    UInt  uiWidth     = m_ppcRecoYuvBest[uiDepth]->getWidth   ( );
1060    UInt  uiHeight    = m_ppcRecoYuvBest[uiDepth]->getHeight  ( );
1061    Pel*  piSrc       = m_ppcRecoYuvBest[uiDepth]->getLumaAddr( 0 );
1062    UInt  uiSrcStride = m_ppcRecoYuvBest[uiDepth]->getStride  ( );
1063    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1064  }
1065#endif
1066
1067  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
1068
1069  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY );   // Copy Yuv data to picture Yuv
1070  if( bBoundary ||(bSliceEnd && bInsidePicture))
1071  {
1072    return;
1073  }
1074
1075  // Assert if Best prediction mode is NONE
1076  // Selected mode's RD-cost must be not MAX_DOUBLE.
1077  assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE  );
1078  assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE  );
1079  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE );
1080}
1081
1082/** finish encoding a cu and handle end-of-slice conditions
1083 * \param pcCU
1084 * \param uiAbsPartIdx
1085 * \param uiDepth
1086 * \returns Void
1087 */
1088Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1089{
1090  TComPic* pcPic = pcCU->getPic();
1091  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1092
1093  //Calculate end address
1094  UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx;
1095
1096  UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU();
1097  UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU();
1098  UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1099  UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1100  UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
1101  UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
1102  while(uiPosX>=uiWidth||uiPosY>=uiHeight)
1103  {
1104    uiInternalAddress--;
1105    uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1106    uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1107  }
1108  uiInternalAddress++;
1109  if(uiInternalAddress==pcCU->getPic()->getNumPartInCU())
1110  {
1111    uiInternalAddress = 0;
1112    uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1);
1113  }
1114  UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress);
1115
1116  // Encode slice finish
1117  Bool bTerminateSlice = false;
1118  if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress)
1119  {
1120    bTerminateSlice = true;
1121  }
1122  UInt uiGranularityWidth = g_uiMaxCUWidth;
1123  uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1124  uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1125  Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth))
1126    &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight));
1127 
1128  if(granularityBoundary)
1129  {
1130    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1131    if (!bTerminateSlice)
1132      m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 );
1133  }
1134 
1135  Int numberOfWrittenBits = 0;
1136  if (m_pcBitCounter)
1137  {
1138    numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits();
1139  }
1140 
1141  // Calculate slice end IF this CU puts us over slice bit size.
1142  UInt iGranularitySize = pcCU->getPic()->getNumPartInCU();
1143  Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize;
1144  if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) 
1145  {
1146    iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)));
1147  }
1148  // Set slice end parameter
1149  if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) 
1150  {
1151    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1152    pcSlice->setSliceCurEndCUAddr(iGranularityEnd);
1153    return;
1154  }
1155  // Set dependent slice end parameter
1156  if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) 
1157  {
1158    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1159    return;
1160  }
1161  if(granularityBoundary)
1162  {
1163    pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) );
1164    pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits);
1165    if (m_pcBitCounter)
1166    {
1167      m_pcEntropyCoder->resetBits();     
1168    }
1169  }
1170}
1171
1172/** Compute QP for each CU
1173 * \param pcCU Target CU
1174 * \param uiDepth CU depth
1175 * \returns quantization parameter
1176 */
1177Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1178{
1179  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1180  Int iQpOffset = 0;
1181  if ( m_pcEncCfg->getUseAdaptiveQP() )
1182  {
1183    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1184    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1185    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1186    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1187    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1188    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1189    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1190
1191    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1192    Double dAvgAct = pcAQLayer->getAvgActivity();
1193    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1194    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1195    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1196    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1197  }
1198  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset );
1199}
1200
1201/** encode a CU block recursively
1202 * \param pcCU
1203 * \param uiAbsPartIdx
1204 * \param uiDepth
1205 * \returns Void
1206 */
1207Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1208{
1209  TComPic* pcPic = pcCU->getPic();
1210 
1211  Bool bBoundary = false;
1212  UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1213  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1214  UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1215  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1216 
1217  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1218  // If slice start is within this cu...
1219  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1220    pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) );
1221  // We need to split, so don't try these modes.
1222  if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1223  {
1224    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1225  }
1226  else
1227  {
1228    bBoundary = true;
1229  }
1230 
1231  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary )
1232  {
1233    UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2;
1234    if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1235    {
1236      setdQPFlag(true);
1237    }
1238    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1239    {
1240      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1241      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1242      Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr();
1243      if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1244      {
1245        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1246      }
1247    }
1248    return;
1249  }
1250 
1251  if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1252  {
1253    setdQPFlag(true);
1254  }
1255  if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1256  {
1257    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1258  }
1259  if( !pcCU->getSlice()->isIntra() )
1260  {
1261    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1262  }
1263 
1264  if( pcCU->isSkipped( uiAbsPartIdx ) )
1265  {
1266    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1267#if H_3D_IC
1268    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1269#endif
1270#if H_3D_ARP
1271    m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1272#endif
1273    finishCU(pcCU,uiAbsPartIdx,uiDepth);
1274    return;
1275  }
1276  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1277 
1278  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1279 
1280  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1281  {
1282    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1283
1284    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1285    {
1286      // Encode slice finish
1287      finishCU(pcCU,uiAbsPartIdx,uiDepth);
1288      return;
1289    }
1290  }
1291
1292  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1293  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1294#if H_3D_IC
1295  m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1296#endif
1297#if H_3D_ARP
1298  m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1299#endif
1300
1301  // Encode Coefficients
1302  Bool bCodeDQP = getdQPFlag();
1303  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP );
1304  setdQPFlag( bCodeDQP );
1305
1306  // --- write terminating bit ---
1307  finishCU(pcCU,uiAbsPartIdx,uiDepth);
1308}
1309
1310/** check RD costs for a CU block encoded with merge
1311 * \param rpcBestCU
1312 * \param rpcTempCU
1313 * \returns Void
1314 */
1315Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode )
1316{
1317  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1318  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists
1319  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1320  Int numValidMergeCand = 0;
1321
1322  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1323  {
1324    uhInterDirNeighbours[ui] = 0;
1325  }
1326  UChar uhDepth = rpcTempCU->getDepth( 0 );
1327#if H_3D_IC
1328  Bool bICFlag = rpcTempCU->getICFlag( 0 );
1329#endif
1330#if H_3D_VSO // M1  //nececcary here?
1331  if( m_pcRdCost->getUseRenModel() )
1332  {
1333    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1334    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1335    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1336    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1337    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1338  }
1339#endif
1340
1341  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1342  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth );
1343  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1344
1345  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1346  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1347  {
1348    mergeCandBuffer[ui] = 0;
1349  }
1350
1351  Bool bestIsSkip = false;
1352
1353  UInt iteration;
1354  if ( rpcTempCU->isLosslessCoded(0))
1355  {
1356    iteration = 1;
1357  }
1358  else 
1359  {
1360    iteration = 2;
1361  }
1362
1363#if H_3D_ARP
1364  Int nARPWMax = rpcTempCU->getSlice()->getARPStepNum() - 1;
1365  if( nARPWMax < 0 || !rpcTempCU->getDvInfo(0).bDV )
1366  {
1367    nARPWMax = 0;
1368  }
1369  for( Int nARPW=nARPWMax; nARPW >= 0 ; nARPW-- )
1370  {
1371    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS*sizeof(Int) );
1372#endif
1373  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1374  {
1375    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1376    {
1377      {
1378#if H_3D_IC
1379        if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
1380        {
1381          if( bICFlag && uiMergeCand == 0 ) 
1382          {
1383            continue;
1384          }
1385        }
1386#endif
1387        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1388        {
1389
1390        if( !(bestIsSkip && uiNoResidual == 0) )
1391        {
1392          // set MC parameters
1393          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1394          rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(),     0, uhDepth );
1395          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1396#if H_3D_IC
1397          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1398#endif
1399#if H_3D_ARP
1400          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1401#endif
1402          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1403          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1404          rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1405          rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1406          rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1407
1408#if H_3D_ARP
1409          if( nARPW )
1410          {
1411            Bool bSignalflag[2] = { true, true };
1412            for( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1413            {
1414              Int iRefIdx = cMvFieldNeighbours[uiRefListIdx + 2*uiMergeCand].getRefIdx();
1415              RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1416              if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1417              {
1418                bSignalflag[uiRefListIdx] = false;
1419              }
1420            }
1421            if( !bSignalflag[0] && !bSignalflag[1] )
1422            {
1423              rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1424            }
1425          }
1426#endif
1427       // do MC
1428       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1429       // estimate residual and encode everything
1430
1431#if H_3D_VSO //M2
1432       if( m_pcRdCost->getUseRenModel() )
1433       { //Reset
1434         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1435         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1436         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1437         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1438         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1439       }
1440#endif
1441
1442       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1443         m_ppcOrigYuv    [uhDepth],
1444         m_ppcPredYuvTemp[uhDepth],
1445         m_ppcResiYuvTemp[uhDepth],
1446         m_ppcResiYuvBest[uhDepth],
1447         m_ppcRecoYuvTemp[uhDepth],
1448         (uiNoResidual? true:false));
1449
1450
1451       if(uiNoResidual==0)
1452       {
1453         if(rpcTempCU->getQtRootCbf(0) == 0)
1454         {
1455           mergeCandBuffer[uiMergeCand] = 1;
1456         }
1457       }
1458
1459       rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
1460          Int orgQP = rpcTempCU->getQP( 0 );
1461          xCheckDQP( rpcTempCU );
1462          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1463          rpcTempCU->initEstData( uhDepth, orgQP );
1464
1465
1466      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
1467      {
1468        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
1469      }
1470
1471    }
1472    }
1473   }
1474  }
1475
1476  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
1477  {
1478    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
1479    {
1480      if( rpcBestCU->getMergeFlag( 0 ))
1481      {
1482        *earlyDetectionSkipMode = true;
1483      }
1484      else
1485      {
1486        Int absoulte_MV=0;
1487        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1488        {
1489          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1490          {
1491            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
1492            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
1493            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
1494            absoulte_MV+=iHor+iVer;
1495          }
1496        }
1497
1498        if(absoulte_MV == 0)
1499        {
1500          *earlyDetectionSkipMode = true;
1501        }
1502      }
1503    }
1504  }
1505 }
1506#if H_3D_ARP
1507 }
1508#endif
1509}
1510
1511
1512#if AMP_MRG
1513Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
1514#else
1515Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
1516#endif
1517{
1518  UChar uhDepth = rpcTempCU->getDepth( 0 );
1519#if H_3D_ARP
1520  Int iLayerId    = rpcTempCU->getSlice()->getLayerId();
1521  Bool bFirstTime = true;
1522  Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
1523
1524  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || !rpcTempCU->getDvInfo(0).bDV  )
1525  {
1526    nARPWMax = 0;
1527  }
1528
1529  for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
1530  {
1531    if( bFirstTime == false && rpcTempCU->getSlice()->getVPS()->getUseAdvRP( iLayerId ) )
1532    {
1533      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0) );
1534    }
1535#endif
1536#if H_3D_VSO // M3
1537  if( m_pcRdCost->getUseRenModel() )
1538  {
1539    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1540    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1541    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1542    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1543    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1544  }
1545#endif
1546
1547  rpcTempCU->setDepthSubParts( uhDepth, 0 );
1548 
1549  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
1550
1551  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
1552  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
1553  rpcTempCU->setCUTransquantBypassSubParts  ( m_pcEncCfg->getCUTransquantBypassFlagValue(),      0, uhDepth );
1554 
1555#if H_3D_ARP
1556  rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1557#endif
1558
1559#if H_3D_ARP
1560  if( bFirstTime == false && nARPWMax )
1561  {
1562    rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
1563    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1564
1565    m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
1566
1567    if(rpcTempCU->getPartitionSize(0)==SIZE_2Nx2N)
1568    {
1569      Bool bSignalflag[2] = { true, true };
1570      for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1571      {
1572        RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1573        Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
1574        if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1575        {
1576          bSignalflag[uiRefListIdx] = false;
1577        }
1578      }
1579      if( !bSignalflag[0] && !bSignalflag[1] )
1580      {
1581        rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1582      }
1583    }
1584  }
1585  else
1586  {
1587    bFirstTime = false;
1588#endif
1589#if AMP_MRG
1590  rpcTempCU->setMergeAMP (true);
1591  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
1592#else 
1593  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1594#endif
1595#if H_3D_ARP
1596   if( nARPWMax )
1597   {
1598     m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
1599
1600     Bool bSignalflag[2] = { true, true };
1601     for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1602     {
1603       RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1604       Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
1605       if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1606       {
1607         bSignalflag[uiRefListIdx] = false;
1608       }
1609     }
1610     if( !bSignalflag[0] && !bSignalflag[1])
1611     {
1612       rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1613     }
1614   }
1615  }
1616#endif
1617
1618#if AMP_MRG
1619  if ( !rpcTempCU->getMergeAMP() )
1620  {
1621#if H_3D_ARP
1622    if( nARPWMax )
1623    {
1624      continue;
1625    }
1626    else
1627#endif
1628    return;
1629  }
1630#endif
1631
1632#if RATE_CONTROL_LAMBDA_DOMAIN
1633  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
1634  {
1635    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
1636      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
1637      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
1638    m_temporalSAD = (Int)SAD;
1639  }
1640#endif
1641
1642  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
1643
1644
1645#if H_3D_VSO // M4
1646  if( m_pcRdCost->getUseLambdaScaleVSO() )
1647    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1648  else
1649#endif
1650  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1651
1652  xCheckDQP( rpcTempCU );
1653  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1654#if H_3D_ARP
1655  }
1656#endif
1657}
1658
1659Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
1660{
1661  UInt uiDepth = rpcTempCU->getDepth( 0 );
1662 
1663#if H_3D_VSO // M5
1664  if( m_pcRdCost->getUseRenModel() )
1665  {
1666    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
1667    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
1668    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
1669    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
1670    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1671  }
1672#endif
1673
1674  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1675
1676  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
1677  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1678  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
1679 
1680  Bool bSeparateLumaChroma = true; // choose estimation mode
1681  UInt uiPreCalcDistC      = 0;
1682  if( !bSeparateLumaChroma )
1683  {
1684    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
1685  }
1686  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
1687
1688  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
1689 
1690  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
1691 
1692  m_pcEntropyCoder->resetBits();
1693  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1694  {
1695    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1696  }
1697  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1698  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
1699  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
1700  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
1701  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
1702
1703  // Encode Coefficients
1704  Bool bCodeDQP = getdQPFlag();
1705  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
1706  setdQPFlag( bCodeDQP );
1707 
1708  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1709 
1710  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1711  if(m_pcEncCfg->getUseSBACRD())
1712  {
1713    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1714  }
1715
1716#if H_3D_VSO // M6
1717  if( m_pcRdCost->getUseLambdaScaleVSO()) 
1718    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
1719  else
1720#endif
1721  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1722 
1723  xCheckDQP( rpcTempCU );
1724  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
1725}
1726
1727/** Check R-D costs for a CU with PCM mode.
1728 * \param rpcBestCU pointer to best mode CU data structure
1729 * \param rpcTempCU pointer to testing mode CU data structure
1730 * \returns Void
1731 *
1732 * \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.
1733 */
1734Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
1735{
1736  UInt uiDepth = rpcTempCU->getDepth( 0 );
1737
1738  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1739
1740  rpcTempCU->setIPCMFlag(0, true);
1741  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
1742  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
1743  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1744  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
1745  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
1746
1747  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
1748
1749  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1750
1751  m_pcEntropyCoder->resetBits();
1752  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1753  {
1754    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1755  }
1756  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1757  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
1758  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
1759  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
1760
1761  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1762
1763  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1764  if(m_pcEncCfg->getUseSBACRD())
1765  {
1766    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1767  }
1768#if H_3D_VSO // M44
1769  if ( m_pcRdCost->getUseVSO() )
1770    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1771  else
1772#endif
1773  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1774
1775  xCheckDQP( rpcTempCU );
1776  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
1777}
1778
1779/** check whether current try is the best with identifying the depth of current try
1780 * \param rpcBestCU
1781 * \param rpcTempCU
1782 * \returns Void
1783 */
1784Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
1785{
1786  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
1787  {
1788    TComYuv* pcYuv;
1789    // Change Information data
1790    TComDataCU* pcCU = rpcBestCU;
1791    rpcBestCU = rpcTempCU;
1792    rpcTempCU = pcCU;
1793
1794    // Change Prediction data
1795    pcYuv = m_ppcPredYuvBest[uiDepth];
1796    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
1797    m_ppcPredYuvTemp[uiDepth] = pcYuv;
1798
1799    // Change Reconstruction data
1800    pcYuv = m_ppcRecoYuvBest[uiDepth];
1801    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
1802    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
1803
1804    pcYuv = NULL;
1805    pcCU  = NULL;
1806
1807    if( m_bUseSBACRD )  // store temp best CI for next CU coding
1808      m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
1809  }
1810}
1811
1812Void TEncCu::xCheckDQP( TComDataCU* pcCU )
1813{
1814  UInt uiDepth = pcCU->getDepth( 0 );
1815
1816  if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() )
1817  {
1818    if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) )
1819    {
1820#if !RDO_WITHOUT_DQP_BITS
1821      m_pcEntropyCoder->resetBits();
1822      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
1823      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1824      if(m_pcEncCfg->getUseSBACRD())
1825      {
1826        pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1827      }
1828#if H_3D_VSO // M45
1829      if ( m_pcRdCost->getUseVSO() )     
1830        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
1831      else
1832#endif
1833      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
1834#endif
1835    }
1836    else
1837    {
1838      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
1839    }
1840  }
1841}
1842
1843Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
1844{
1845  pDst->iN = pSrc->iN;
1846  for (Int i = 0; i < pSrc->iN; i++)
1847  {
1848    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
1849  }
1850}
1851Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY )
1852{
1853  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1854  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1855  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1856  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1857    pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
1858  Bool bSliceEnd   = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1859    pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
1860  if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1861  {
1862    UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
1863    UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth);
1864    UInt uiBlkWidth    = rpcPic->getNumPartInWidth() >> (uiDepth);
1865    UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
1866    UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
1867    UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
1868    m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
1869  }
1870  else
1871  {
1872    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2;
1873
1874    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1875    {
1876      UInt uiSubCULPelX   = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx &  1 );
1877      UInt uiSubCUTPelY   = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 );
1878
1879      Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && 
1880        rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr();
1881      if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1882      {
1883        xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY );   // Copy Yuv data to picture Yuv
1884      }
1885    }
1886  }
1887}
1888
1889Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
1890{
1891  UInt uiCurrDepth = uiNextDepth - 1;
1892  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
1893}
1894
1895/** Function for filling the PCM buffer of a CU using its original sample array
1896 * \param pcCU pointer to current CU
1897 * \param pcOrgYuv pointer to original sample array
1898 * \returns Void
1899 */
1900Void TEncCu::xFillPCMBuffer     ( TComDataCU*& pCU, TComYuv* pOrgYuv )
1901{
1902
1903  UInt   width        = pCU->getWidth(0);
1904  UInt   height       = pCU->getHeight(0);
1905
1906  Pel*   pSrcY = pOrgYuv->getLumaAddr(0, width); 
1907  Pel*   pDstY = pCU->getPCMSampleY();
1908  UInt   srcStride = pOrgYuv->getStride();
1909
1910  for(Int y = 0; y < height; y++ )
1911  {
1912    for(Int x = 0; x < width; x++ )
1913    {
1914      pDstY[x] = pSrcY[x];
1915    }
1916    pDstY += width;
1917    pSrcY += srcStride;
1918  }
1919
1920  Pel* pSrcCb       = pOrgYuv->getCbAddr();
1921  Pel* pSrcCr       = pOrgYuv->getCrAddr();;
1922
1923  Pel* pDstCb       = pCU->getPCMSampleCb();
1924  Pel* pDstCr       = pCU->getPCMSampleCr();;
1925
1926  UInt srcStrideC = pOrgYuv->getCStride();
1927  UInt heightC   = height >> 1;
1928  UInt widthC    = width  >> 1;
1929
1930  for(Int y = 0; y < heightC; y++ )
1931  {
1932    for(Int x = 0; x < widthC; x++ )
1933    {
1934      pDstCb[x] = pSrcCb[x];
1935      pDstCr[x] = pSrcCr[x];
1936    }
1937    pDstCb += widthC;
1938    pDstCr += widthC;
1939    pSrcCb += srcStrideC;
1940    pSrcCr += srcStrideC;
1941  }
1942}
1943
1944#if ADAPTIVE_QP_SELECTION
1945/** Collect ARL statistics from one block
1946  */
1947Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
1948{
1949  for( Int n = 0; n < NumCoeffInCU; n++ )
1950  {
1951    Int u = abs( rpcCoeff[ n ] );
1952    Int absc = rpcArlCoeff[ n ];
1953
1954    if( u != 0 )
1955    {
1956      if( u < LEVEL_RANGE )
1957      {
1958        cSum[ u ] += ( Double )absc;
1959        numSamples[ u ]++;
1960      }
1961      else 
1962      {
1963        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
1964        numSamples[ LEVEL_RANGE ]++;
1965      }
1966    }
1967  }
1968
1969  return 0;
1970}
1971
1972/** Collect ARL statistics from one LCU
1973 * \param pcCU
1974 */
1975Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU )
1976{
1977  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to datatype and quantization output
1978  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output
1979
1980  TCoeff* pCoeffY = rpcCU->getCoeffY();
1981  Int* pArlCoeffY = rpcCU->getArlCoeffY();
1982
1983  UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth;
1984  UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;
1985
1986  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
1987  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
1988
1989  // Collect stats to cSum[][] and numSamples[][]
1990  for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ )
1991  {
1992    UInt uiTrIdx = rpcCU->getTransformIdx(i);
1993
1994    if(rpcCU->getPredictionMode(i) == MODE_INTER)
1995    if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) )
1996    {
1997      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
1998    }//Note that only InterY is processed. QP rounding is based on InterY data only.
1999   
2000    pCoeffY  += uiMinNumCoeffInCU;
2001    pArlCoeffY  += uiMinNumCoeffInCU;
2002  }
2003
2004  for(Int u=1; u<LEVEL_RANGE;u++)
2005  {
2006    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
2007    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
2008  }
2009  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
2010  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
2011}
2012#endif
2013//! \}
Note: See TracBrowser for help on using the repository browser.