source: 3DVCSoftware/branches/HTM-DEV-2.0-dev1-Mediatek/source/Lib/TLibEncoder/TEncCu.cpp @ 566

Last change on this file since 566 was 566, checked in by mediatek-htm, 11 years ago

Integration of JCT3V-E0172. The MACRO is "MTK_RVS_BUGFIX_E0172".

By Na Zhang (na.zhang@…)

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