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

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

Update to HM 11.0.

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