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

Last change on this file since 464 was 464, checked in by zhang, 11 years ago

Implementation of ARP from QC

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