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

Last change on this file since 541 was 541, checked in by tech, 11 years ago
  • Changed HM Version number
  • Minor cleanups
  • Property svn:eol-style set to native
File size: 79.4 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  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag, numValidMergeCand );
1618#else
1619  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1620#endif
1621
1622#if H_3D_IV_MERGE
1623  Int mergeCandBuffer[MRG_MAX_NUM_CANDS_MEM];
1624#else
1625  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1626#endif
1627for( UInt ui = 0; ui < numValidMergeCand; ++ui )
1628  {
1629    mergeCandBuffer[ui] = 0;
1630  }
1631
1632  Bool bestIsSkip = false;
1633
1634  UInt iteration;
1635  if ( rpcTempCU->isLosslessCoded(0))
1636  {
1637    iteration = 1;
1638  }
1639  else 
1640  {
1641    iteration = 2;
1642  }
1643
1644#if H_3D_ARP
1645  Int nARPWMax = rpcTempCU->getSlice()->getARPStepNum() - 1;
1646  if( nARPWMax < 0 || !rpcTempCU->getDvInfo(0).bDV )
1647  {
1648    nARPWMax = 0;
1649  }
1650  for( Int nARPW=nARPWMax; nARPW >= 0 ; nARPW-- )
1651  {
1652    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS*sizeof(Int) );
1653#endif
1654  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1655  {
1656    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1657    {     
1658#if H_3D_IC
1659        if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
1660        {
1661          if( bICFlag && uiMergeCand == 0 ) 
1662          {
1663            continue;
1664          }
1665        }
1666#endif
1667        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1668        {
1669        if( !(bestIsSkip && uiNoResidual == 0) )
1670        {
1671          // set MC parameters
1672          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1673          rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(),     0, uhDepth );
1674          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1675#if H_3D_IC
1676          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1677#endif
1678#if H_3D_ARP
1679          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1680#endif
1681          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1682          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1683#if H_3D_VSP
1684          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
1685#endif
1686          rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1687          rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1688          rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1689
1690#if H_3D_ARP
1691          if( nARPW )
1692          {
1693            Bool bSignalflag[2] = { true, true };
1694            for( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1695            {
1696              Int iRefIdx = cMvFieldNeighbours[uiRefListIdx + 2*uiMergeCand].getRefIdx();
1697              RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1698              if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1699              {
1700                bSignalflag[uiRefListIdx] = false;
1701              }
1702            }
1703            if( !bSignalflag[0] && !bSignalflag[1] )
1704            {
1705              rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1706            }
1707          }
1708#endif
1709       // do MC
1710       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1711       // estimate residual and encode everything
1712#if H_3D_VSO //M2
1713       if( m_pcRdCost->getUseRenModel() )
1714       { //Reset
1715         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1716         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1717         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1718         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1719         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1720       }
1721#endif
1722       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1723         m_ppcOrigYuv    [uhDepth],
1724         m_ppcPredYuvTemp[uhDepth],
1725         m_ppcResiYuvTemp[uhDepth],
1726         m_ppcResiYuvBest[uhDepth],
1727         m_ppcRecoYuvTemp[uhDepth],
1728         (uiNoResidual? true:false));
1729
1730
1731          if ( uiNoResidual == 0 && rpcTempCU->getQtRootCbf(0) == 0 )
1732         {
1733            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
1734           mergeCandBuffer[uiMergeCand] = 1;
1735         }
1736
1737          rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
1738          Int orgQP = rpcTempCU->getQP( 0 );
1739          xCheckDQP( rpcTempCU );
1740          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1741          rpcTempCU->initEstData( uhDepth, orgQP );
1742
1743      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
1744      {
1745        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
1746      }
1747    }
1748   }
1749  }
1750
1751  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
1752  {
1753    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
1754    {
1755      if( rpcBestCU->getMergeFlag( 0 ))
1756      {
1757        *earlyDetectionSkipMode = true;
1758      }
1759      else
1760      {
1761        Int absoulte_MV=0;
1762        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1763        {
1764          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1765          {
1766            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
1767            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
1768            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
1769            absoulte_MV+=iHor+iVer;
1770          }
1771        }
1772
1773        if(absoulte_MV == 0)
1774        {
1775          *earlyDetectionSkipMode = true;
1776        }
1777      }
1778    }
1779  }
1780 }
1781#if H_3D_ARP
1782 }
1783#endif
1784}
1785
1786
1787#if AMP_MRG
1788Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
1789#else
1790Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
1791#endif
1792{
1793  UChar uhDepth = rpcTempCU->getDepth( 0 );
1794#if H_3D_ARP
1795  Int iLayerId    = rpcTempCU->getSlice()->getLayerId();
1796  Bool bFirstTime = true;
1797  Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
1798
1799  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || !rpcTempCU->getDvInfo(0).bDV  )
1800  {
1801    nARPWMax = 0;
1802  }
1803
1804  for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
1805  {
1806    if( bFirstTime == false && rpcTempCU->getSlice()->getVPS()->getUseAdvRP( iLayerId ) )
1807    {
1808      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0) );
1809    }
1810#endif
1811#if H_3D_VSO // M3
1812  if( m_pcRdCost->getUseRenModel() )
1813  {
1814    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1815    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1816    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1817    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1818    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1819  }
1820#endif
1821
1822  rpcTempCU->setDepthSubParts( uhDepth, 0 );
1823 
1824  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
1825
1826  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
1827  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
1828  rpcTempCU->setCUTransquantBypassSubParts  ( m_pcEncCfg->getCUTransquantBypassFlagValue(),      0, uhDepth );
1829 
1830#if H_3D_ARP
1831  rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1832#endif
1833
1834#if H_3D_ARP
1835  if( bFirstTime == false && nARPWMax )
1836  {
1837    rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
1838    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1839
1840    m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
1841
1842    if(rpcTempCU->getPartitionSize(0)==SIZE_2Nx2N)
1843    {
1844      Bool bSignalflag[2] = { true, true };
1845      for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1846      {
1847        RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1848        Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
1849        if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1850        {
1851          bSignalflag[uiRefListIdx] = false;
1852        }
1853      }
1854      if( !bSignalflag[0] && !bSignalflag[1] )
1855      {
1856        rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1857      }
1858    }
1859  }
1860  else
1861  {
1862    bFirstTime = false;
1863#endif
1864#if AMP_MRG
1865  rpcTempCU->setMergeAMP (true);
1866  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
1867#else 
1868  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1869#endif
1870#if H_3D_ARP
1871   if( nARPWMax )
1872   {
1873     m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
1874
1875     Bool bSignalflag[2] = { true, true };
1876     for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1877     {
1878       RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1879       Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
1880       if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1881       {
1882         bSignalflag[uiRefListIdx] = false;
1883       }
1884     }
1885     if( !bSignalflag[0] && !bSignalflag[1])
1886     {
1887       rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1888     }
1889   }
1890  }
1891#endif
1892
1893#if AMP_MRG
1894  if ( !rpcTempCU->getMergeAMP() )
1895  {
1896#if H_3D_ARP
1897    if( nARPWMax )
1898    {
1899      continue;
1900    }
1901    else
1902#endif
1903    return;
1904  }
1905#endif
1906
1907#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
1908  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
1909  {
1910    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
1911      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
1912      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
1913    m_temporalSAD = (Int)SAD;
1914  }
1915#endif
1916
1917  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
1918
1919
1920#if H_3D_VSO // M4
1921  if( m_pcRdCost->getUseLambdaScaleVSO() )
1922    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1923  else
1924#endif
1925  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1926
1927  xCheckDQP( rpcTempCU );
1928  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1929#if H_3D_ARP
1930  }
1931#endif
1932}
1933
1934Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
1935{
1936  UInt uiDepth = rpcTempCU->getDepth( 0 );
1937 
1938#if H_3D_VSO // M5
1939  if( m_pcRdCost->getUseRenModel() )
1940  {
1941    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
1942    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
1943    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
1944    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
1945    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1946  }
1947#endif
1948
1949  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1950
1951  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
1952  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1953  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
1954 
1955  Bool bSeparateLumaChroma = true; // choose estimation mode
1956  UInt uiPreCalcDistC      = 0;
1957  if( !bSeparateLumaChroma )
1958  {
1959    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
1960  }
1961  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
1962
1963  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
1964 
1965#if H_3D_DIM_SDC
1966  if( !rpcTempCU->getSDCFlag( 0 ) )
1967#endif
1968  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
1969 
1970  m_pcEntropyCoder->resetBits();
1971  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1972  {
1973    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1974  }
1975  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1976  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
1977  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
1978  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
1979  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
1980
1981  // Encode Coefficients
1982  Bool bCodeDQP = getdQPFlag();
1983  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
1984  setdQPFlag( bCodeDQP );
1985 
1986  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1987 
1988  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1989  if(m_pcEncCfg->getUseSBACRD())
1990  {
1991    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1992  }
1993
1994#if H_3D_VSO // M6
1995  if( m_pcRdCost->getUseLambdaScaleVSO()) 
1996    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
1997  else
1998#endif
1999  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2000 
2001  xCheckDQP( rpcTempCU );
2002  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2003}
2004
2005/** Check R-D costs for a CU with PCM mode.
2006 * \param rpcBestCU pointer to best mode CU data structure
2007 * \param rpcTempCU pointer to testing mode CU data structure
2008 * \returns Void
2009 *
2010 * \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.
2011 */
2012Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
2013{
2014  UInt uiDepth = rpcTempCU->getDepth( 0 );
2015
2016  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2017
2018  rpcTempCU->setIPCMFlag(0, true);
2019  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
2020  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2021  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2022  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
2023  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
2024
2025  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
2026
2027  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
2028
2029  m_pcEntropyCoder->resetBits();
2030  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2031  {
2032    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2033  }
2034  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2035  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
2036  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
2037  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
2038
2039  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2040
2041  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2042  if(m_pcEncCfg->getUseSBACRD())
2043  {
2044    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2045  }
2046#if H_3D_VSO // M44
2047  if ( m_pcRdCost->getUseVSO() )
2048    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2049  else
2050#endif
2051  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2052
2053  xCheckDQP( rpcTempCU );
2054  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
2055}
2056
2057/** check whether current try is the best with identifying the depth of current try
2058 * \param rpcBestCU
2059 * \param rpcTempCU
2060 * \returns Void
2061 */
2062Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
2063{
2064  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
2065  {
2066    TComYuv* pcYuv;
2067    // Change Information data
2068    TComDataCU* pcCU = rpcBestCU;
2069    rpcBestCU = rpcTempCU;
2070    rpcTempCU = pcCU;
2071
2072    // Change Prediction data
2073    pcYuv = m_ppcPredYuvBest[uiDepth];
2074    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
2075    m_ppcPredYuvTemp[uiDepth] = pcYuv;
2076
2077    // Change Reconstruction data
2078    pcYuv = m_ppcRecoYuvBest[uiDepth];
2079    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
2080    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
2081
2082    pcYuv = NULL;
2083    pcCU  = NULL;
2084
2085    if( m_bUseSBACRD )  // store temp best CI for next CU coding
2086      m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
2087  }
2088}
2089
2090Void TEncCu::xCheckDQP( TComDataCU* pcCU )
2091{
2092  UInt uiDepth = pcCU->getDepth( 0 );
2093
2094  if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() )
2095  {
2096    if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) )
2097    {
2098#if !RDO_WITHOUT_DQP_BITS
2099      m_pcEntropyCoder->resetBits();
2100      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
2101      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
2102      if(m_pcEncCfg->getUseSBACRD())
2103      {
2104        pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2105      }
2106#if H_3D_VSO // M45
2107      if ( m_pcRdCost->getUseVSO() )     
2108        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
2109      else
2110#endif
2111      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
2112#endif
2113    }
2114    else
2115    {
2116      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
2117    }
2118  }
2119}
2120
2121Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
2122{
2123  pDst->iN = pSrc->iN;
2124  for (Int i = 0; i < pSrc->iN; i++)
2125  {
2126    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
2127  }
2128}
2129Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY )
2130{
2131  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
2132  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
2133  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
2134  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2135    pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2136  Bool bSliceEnd   = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2137    pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2138  if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2139  {
2140    UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
2141    UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth);
2142    UInt uiBlkWidth    = rpcPic->getNumPartInWidth() >> (uiDepth);
2143    UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2144    UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2145    UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
2146    m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
2147  }
2148  else
2149  {
2150    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2;
2151
2152    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
2153    {
2154      UInt uiSubCULPelX   = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx &  1 );
2155      UInt uiSubCUTPelY   = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 );
2156
2157      Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && 
2158        rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr();
2159      if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2160      {
2161        xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY );   // Copy Yuv data to picture Yuv
2162      }
2163    }
2164  }
2165}
2166
2167Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
2168{
2169  UInt uiCurrDepth = uiNextDepth - 1;
2170  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
2171}
2172
2173/** Function for filling the PCM buffer of a CU using its original sample array
2174 * \param pcCU pointer to current CU
2175 * \param pcOrgYuv pointer to original sample array
2176 * \returns Void
2177 */
2178Void TEncCu::xFillPCMBuffer     ( TComDataCU*& pCU, TComYuv* pOrgYuv )
2179{
2180
2181  UInt   width        = pCU->getWidth(0);
2182  UInt   height       = pCU->getHeight(0);
2183
2184  Pel*   pSrcY = pOrgYuv->getLumaAddr(0, width); 
2185  Pel*   pDstY = pCU->getPCMSampleY();
2186  UInt   srcStride = pOrgYuv->getStride();
2187
2188  for(Int y = 0; y < height; y++ )
2189  {
2190    for(Int x = 0; x < width; x++ )
2191    {
2192      pDstY[x] = pSrcY[x];
2193    }
2194    pDstY += width;
2195    pSrcY += srcStride;
2196  }
2197
2198  Pel* pSrcCb       = pOrgYuv->getCbAddr();
2199  Pel* pSrcCr       = pOrgYuv->getCrAddr();;
2200
2201  Pel* pDstCb       = pCU->getPCMSampleCb();
2202  Pel* pDstCr       = pCU->getPCMSampleCr();;
2203
2204  UInt srcStrideC = pOrgYuv->getCStride();
2205  UInt heightC   = height >> 1;
2206  UInt widthC    = width  >> 1;
2207
2208  for(Int y = 0; y < heightC; y++ )
2209  {
2210    for(Int x = 0; x < widthC; x++ )
2211    {
2212      pDstCb[x] = pSrcCb[x];
2213      pDstCr[x] = pSrcCr[x];
2214    }
2215    pDstCb += widthC;
2216    pDstCr += widthC;
2217    pSrcCb += srcStrideC;
2218    pSrcCr += srcStrideC;
2219  }
2220}
2221
2222#if ADAPTIVE_QP_SELECTION
2223/** Collect ARL statistics from one block
2224  */
2225Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
2226{
2227  for( Int n = 0; n < NumCoeffInCU; n++ )
2228  {
2229    Int u = abs( rpcCoeff[ n ] );
2230    Int absc = rpcArlCoeff[ n ];
2231
2232    if( u != 0 )
2233    {
2234      if( u < LEVEL_RANGE )
2235      {
2236        cSum[ u ] += ( Double )absc;
2237        numSamples[ u ]++;
2238      }
2239      else 
2240      {
2241        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
2242        numSamples[ LEVEL_RANGE ]++;
2243      }
2244    }
2245  }
2246
2247  return 0;
2248}
2249
2250/** Collect ARL statistics from one LCU
2251 * \param pcCU
2252 */
2253Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU )
2254{
2255  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to datatype and quantization output
2256  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output
2257
2258  TCoeff* pCoeffY = rpcCU->getCoeffY();
2259  Int* pArlCoeffY = rpcCU->getArlCoeffY();
2260
2261  UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth;
2262  UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;
2263
2264  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
2265  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
2266
2267  // Collect stats to cSum[][] and numSamples[][]
2268  for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ )
2269  {
2270    UInt uiTrIdx = rpcCU->getTransformIdx(i);
2271
2272    if(rpcCU->getPredictionMode(i) == MODE_INTER)
2273    if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) )
2274    {
2275      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
2276    }//Note that only InterY is processed. QP rounding is based on InterY data only.
2277   
2278    pCoeffY  += uiMinNumCoeffInCU;
2279    pArlCoeffY  += uiMinNumCoeffInCU;
2280  }
2281
2282  for(Int u=1; u<LEVEL_RANGE;u++)
2283  {
2284    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
2285    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
2286  }
2287  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
2288  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
2289}
2290#endif
2291//! \}
Note: See TracBrowser for help on using the repository browser.