source: 3DVCSoftware/branches/HTM-8.2-dev2-Qualcomm/source/Lib/TLibEncoder/TEncCu.cpp @ 795

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

JCT3V-F0125

  • Property svn:eol-style set to native
File size: 90.2 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2013, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TEncCu.cpp
35    \brief    Coding Unit (CU) encoder class
36*/
37
38#include <stdio.h>
39#include "TEncTop.h"
40#include "TEncCu.h"
41#include "TEncAnalyze.h"
42
43#include <cmath>
44#include <algorithm>
45using namespace std;
46
47//! \ingroup TLibEncoder
48//! \{
49
50// ====================================================================================================================
51// Constructor / destructor / create / destroy
52// ====================================================================================================================
53
54/**
55 \param    uiTotalDepth  total number of allowable depth
56 \param    uiMaxWidth    largest CU width
57 \param    uiMaxHeight   largest CU height
58 */
59Void TEncCu::create(UChar uhTotalDepth, UInt uiMaxWidth, UInt uiMaxHeight)
60{
61  Int i;
62 
63  m_uhTotalDepth   = uhTotalDepth + 1;
64  m_ppcBestCU      = new TComDataCU*[m_uhTotalDepth-1];
65  m_ppcTempCU      = new TComDataCU*[m_uhTotalDepth-1];
66   
67#if H_3D_ARP
68  m_ppcWeightedTempCU = new TComDataCU*[m_uhTotalDepth-1];
69#endif
70
71  m_ppcPredYuvBest = new TComYuv*[m_uhTotalDepth-1];
72  m_ppcResiYuvBest = new TComYuv*[m_uhTotalDepth-1];
73  m_ppcRecoYuvBest = new TComYuv*[m_uhTotalDepth-1];
74  m_ppcPredYuvTemp = new TComYuv*[m_uhTotalDepth-1];
75  m_ppcResiYuvTemp = new TComYuv*[m_uhTotalDepth-1];
76  m_ppcRecoYuvTemp = new TComYuv*[m_uhTotalDepth-1];
77  m_ppcOrigYuv     = new TComYuv*[m_uhTotalDepth-1];
78 
79  UInt uiNumPartitions;
80  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
81  {
82    uiNumPartitions = 1<<( ( m_uhTotalDepth - i - 1 )<<1 );
83    UInt uiWidth  = uiMaxWidth  >> i;
84    UInt uiHeight = uiMaxHeight >> i;
85   
86    m_ppcBestCU[i] = new TComDataCU; m_ppcBestCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
87    m_ppcTempCU[i] = new TComDataCU; m_ppcTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
88   
89#if H_3D_ARP
90    m_ppcWeightedTempCU[i] = new TComDataCU; m_ppcWeightedTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
91#endif 
92
93    m_ppcPredYuvBest[i] = new TComYuv; m_ppcPredYuvBest[i]->create(uiWidth, uiHeight);
94    m_ppcResiYuvBest[i] = new TComYuv; m_ppcResiYuvBest[i]->create(uiWidth, uiHeight);
95    m_ppcRecoYuvBest[i] = new TComYuv; m_ppcRecoYuvBest[i]->create(uiWidth, uiHeight);
96   
97    m_ppcPredYuvTemp[i] = new TComYuv; m_ppcPredYuvTemp[i]->create(uiWidth, uiHeight);
98    m_ppcResiYuvTemp[i] = new TComYuv; m_ppcResiYuvTemp[i]->create(uiWidth, uiHeight);
99    m_ppcRecoYuvTemp[i] = new TComYuv; m_ppcRecoYuvTemp[i]->create(uiWidth, uiHeight);
100   
101    m_ppcOrigYuv    [i] = new TComYuv; m_ppcOrigYuv    [i]->create(uiWidth, uiHeight);
102  }
103 
104  m_bEncodeDQP = false;
105#if RATE_CONTROL_LAMBDA_DOMAIN
106#if !M0036_RC_IMPROVEMENT
107  m_LCUPredictionSAD = 0;
108  m_addSADDepth      = 0;
109  m_temporalSAD      = 0;
110#endif
111#if M0036_RC_IMPROVEMENT && KWU_RC_MADPRED_E0227
112  m_LCUPredictionSAD = 0;
113  m_addSADDepth      = 0;
114  m_temporalSAD      = 0;
115  m_spatialSAD       = 0;
116#endif
117#endif
118#if !RATE_CONTROL_LAMBDA_DOMAIN && KWU_RC_MADPRED_E0227
119  m_LCUPredictionSAD = 0;
120  m_addSADDepth      = 0;
121  m_temporalSAD      = 0;
122  m_spatialSAD       = 0;
123#endif
124
125  // initialize partition order.
126  UInt* piTmp = &g_auiZscanToRaster[0];
127  initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp);
128  initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
129 
130  // initialize conversion matrix from partition index to pel
131  initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
132}
133
134Void TEncCu::destroy()
135{
136  Int i;
137 
138  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
139  {
140    if(m_ppcBestCU[i])
141    {
142      m_ppcBestCU[i]->destroy();      delete m_ppcBestCU[i];      m_ppcBestCU[i] = NULL;
143    }
144    if(m_ppcTempCU[i])
145    {
146      m_ppcTempCU[i]->destroy();      delete m_ppcTempCU[i];      m_ppcTempCU[i] = NULL;
147    }
148#if H_3D_ARP
149    if(m_ppcWeightedTempCU[i])
150    {
151      m_ppcWeightedTempCU[i]->destroy(); delete m_ppcWeightedTempCU[i]; m_ppcWeightedTempCU[i] = NULL;
152    }
153#endif
154    if(m_ppcPredYuvBest[i])
155    {
156      m_ppcPredYuvBest[i]->destroy(); delete m_ppcPredYuvBest[i]; m_ppcPredYuvBest[i] = NULL;
157    }
158    if(m_ppcResiYuvBest[i])
159    {
160      m_ppcResiYuvBest[i]->destroy(); delete m_ppcResiYuvBest[i]; m_ppcResiYuvBest[i] = NULL;
161    }
162    if(m_ppcRecoYuvBest[i])
163    {
164      m_ppcRecoYuvBest[i]->destroy(); delete m_ppcRecoYuvBest[i]; m_ppcRecoYuvBest[i] = NULL;
165    }
166    if(m_ppcPredYuvTemp[i])
167    {
168      m_ppcPredYuvTemp[i]->destroy(); delete m_ppcPredYuvTemp[i]; m_ppcPredYuvTemp[i] = NULL;
169    }
170    if(m_ppcResiYuvTemp[i])
171    {
172      m_ppcResiYuvTemp[i]->destroy(); delete m_ppcResiYuvTemp[i]; m_ppcResiYuvTemp[i] = NULL;
173    }
174    if(m_ppcRecoYuvTemp[i])
175    {
176      m_ppcRecoYuvTemp[i]->destroy(); delete m_ppcRecoYuvTemp[i]; m_ppcRecoYuvTemp[i] = NULL;
177    }
178    if(m_ppcOrigYuv[i])
179    {
180      m_ppcOrigYuv[i]->destroy();     delete m_ppcOrigYuv[i];     m_ppcOrigYuv[i] = NULL;
181    }
182  }
183  if(m_ppcBestCU)
184  {
185    delete [] m_ppcBestCU;
186    m_ppcBestCU = NULL;
187  }
188  if(m_ppcTempCU)
189  {
190    delete [] m_ppcTempCU;
191    m_ppcTempCU = NULL;
192  }
193
194#if H_3D_ARP
195  if(m_ppcWeightedTempCU)
196  {
197    delete [] m_ppcWeightedTempCU; 
198    m_ppcWeightedTempCU = NULL; 
199  }
200#endif
201  if(m_ppcPredYuvBest)
202  {
203    delete [] m_ppcPredYuvBest;
204    m_ppcPredYuvBest = NULL;
205  }
206  if(m_ppcResiYuvBest)
207  {
208    delete [] m_ppcResiYuvBest;
209    m_ppcResiYuvBest = NULL;
210  }
211  if(m_ppcRecoYuvBest)
212  {
213    delete [] m_ppcRecoYuvBest;
214    m_ppcRecoYuvBest = NULL;
215  }
216  if(m_ppcPredYuvTemp)
217  {
218    delete [] m_ppcPredYuvTemp;
219    m_ppcPredYuvTemp = NULL;
220  }
221  if(m_ppcResiYuvTemp)
222  {
223    delete [] m_ppcResiYuvTemp;
224    m_ppcResiYuvTemp = NULL;
225  }
226  if(m_ppcRecoYuvTemp)
227  {
228    delete [] m_ppcRecoYuvTemp;
229    m_ppcRecoYuvTemp = NULL;
230  }
231  if(m_ppcOrigYuv)
232  {
233    delete [] m_ppcOrigYuv;
234    m_ppcOrigYuv = NULL;
235  }
236}
237
238/** \param    pcEncTop      pointer of encoder class
239 */
240Void TEncCu::init( TEncTop* pcEncTop )
241{
242  m_pcEncCfg           = pcEncTop;
243  m_pcPredSearch       = pcEncTop->getPredSearch();
244  m_pcTrQuant          = pcEncTop->getTrQuant();
245  m_pcBitCounter       = pcEncTop->getBitCounter();
246  m_pcRdCost           = pcEncTop->getRdCost();
247 
248  m_pcEntropyCoder     = pcEncTop->getEntropyCoder();
249  m_pcCavlcCoder       = pcEncTop->getCavlcCoder();
250  m_pcSbacCoder       = pcEncTop->getSbacCoder();
251  m_pcBinCABAC         = pcEncTop->getBinCABAC();
252 
253  m_pppcRDSbacCoder   = pcEncTop->getRDSbacCoder();
254  m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder();
255 
256  m_bUseSBACRD        = pcEncTop->getUseSBACRD();
257  m_pcRateCtrl        = pcEncTop->getRateCtrl();
258}
259
260// ====================================================================================================================
261// Public member functions
262// ====================================================================================================================
263
264/** \param  rpcCU pointer of CU data class
265 */
266Void TEncCu::compressCU( TComDataCU*& rpcCU )
267{
268  // initialize CU data
269  m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
270  m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
271
272#if RATE_CONTROL_LAMBDA_DOMAIN
273#if !M0036_RC_IMPROVEMENT
274  m_addSADDepth      = 0;
275  m_LCUPredictionSAD = 0;
276  m_temporalSAD      = 0;
277#endif
278#if M0036_RC_IMPROVEMENT && KWU_RC_MADPRED_E0227
279  m_LCUPredictionSAD = 0;
280  m_addSADDepth      = 0;
281  m_temporalSAD      = 0;
282  m_spatialSAD       = 0;
283#endif
284#endif
285#if KWU_RC_MADPRED_E0227
286  m_LCUPredictionSAD = 0;
287  m_addSADDepth      = 0;
288  m_temporalSAD      = 0;
289  m_spatialSAD       = 0;
290#endif
291
292  // analysis of CU
293  xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
294
295#if ADAPTIVE_QP_SELECTION
296  if( m_pcEncCfg->getUseAdaptQpSelect() )
297  {
298    if(rpcCU->getSlice()->getSliceType()!=I_SLICE) //IIII
299    {
300      xLcuCollectARLStats( rpcCU);
301    }
302  }
303#endif
304}
305/** \param  pcCU  pointer of CU data class
306 */
307Void TEncCu::encodeCU ( TComDataCU* pcCU )
308{
309  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
310  {
311    setdQPFlag(true);
312  }
313
314  // Encode CU data
315  xEncodeCU( pcCU, 0, 0 );
316}
317
318// ====================================================================================================================
319// Protected member functions
320// ====================================================================================================================
321/** Derive small set of test modes for AMP encoder speed-up
322 *\param   rpcBestCU
323 *\param   eParentPartSize
324 *\param   bTestAMP_Hor
325 *\param   bTestAMP_Ver
326 *\param   bTestMergeAMP_Hor
327 *\param   bTestMergeAMP_Ver
328 *\returns Void
329*/
330#if AMP_ENC_SPEEDUP
331#if AMP_MRG
332Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver, Bool &bTestMergeAMP_Hor, Bool &bTestMergeAMP_Ver)
333#else
334Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver)
335#endif
336{
337  if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
338  {
339    bTestAMP_Hor = true;
340  }
341  else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
342  {
343    bTestAMP_Ver = true;
344  }
345  else if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->getMergeFlag(0) == false && rpcBestCU->isSkipped(0) == false )
346  {
347    bTestAMP_Hor = true;         
348    bTestAMP_Ver = true;         
349  }
350
351#if AMP_MRG
352  //! Utilizing the partition size of parent PU   
353  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
354  { 
355    bTestMergeAMP_Hor = true;
356    bTestMergeAMP_Ver = true;
357  }
358
359  if ( eParentPartSize == SIZE_NONE ) //! if parent is intra
360  {
361    if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
362    {
363      bTestMergeAMP_Hor = true;
364    }
365    else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
366    {
367      bTestMergeAMP_Ver = true;
368    }
369  }
370
371  if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->isSkipped(0) == false )
372  {
373    bTestMergeAMP_Hor = true;         
374    bTestMergeAMP_Ver = true;         
375  }
376
377  if ( rpcBestCU->getWidth(0) == 64 )
378  { 
379    bTestAMP_Hor = false;
380    bTestAMP_Ver = false;
381  }   
382#else
383  //! Utilizing the partition size of parent PU       
384  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
385  { 
386    bTestAMP_Hor = true;
387    bTestAMP_Ver = true;
388  }
389
390  if ( eParentPartSize == SIZE_2Nx2N )
391  { 
392    bTestAMP_Hor = false;
393    bTestAMP_Ver = false;
394  }     
395#endif
396}
397#endif
398
399// ====================================================================================================================
400// Protected member functions
401// ====================================================================================================================
402/** Compress a CU block recursively with enabling sub-LCU-level delta QP
403 *\param   rpcBestCU
404 *\param   rpcTempCU
405 *\param   uiDepth
406 *\returns Void
407 *
408 *- for loop of QP value to compress the current CU with all possible QP
409*/
410#if AMP_ENC_SPEEDUP
411Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth, PartSize eParentPartSize )
412#else
413Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
414#endif
415{
416  TComPic* pcPic = rpcBestCU->getPic();
417
418#if H_3D_QTLPC
419  TComSPS *sps            = pcPic->getSlice(0)->getSPS();
420  TComPic *pcTexture      = rpcBestCU->getSlice()->getTexturePic();
421
422  Bool  depthMapDetect    = (pcTexture != NULL);
423  Bool  bIntraSliceDetect = (rpcBestCU->getSlice()->getSliceType() == I_SLICE);
424
425  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);
426
427  Bool bTry2NxN           = true;
428  Bool bTryNx2N           = true;
429#endif
430  // get Original YUV data from picture
431  m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
432
433  // variables for fast encoder decision
434#if H_3D_QTLPC 
435  Bool    bTrySplit     = true;
436  Bool    bTrySplitDQP  = true;
437#endif
438
439  // variable for Early CU determination
440  Bool    bSubBranch = true;
441
442  // variable for Cbf fast mode PU decision
443  Bool    doNotBlockPu = true;
444  Bool earlyDetectionSkipMode = false;
445
446#if H_3D_VSP
447  DisInfo DvInfo; 
448  DvInfo.bDV = false;
449  DvInfo.m_acNBDV.setZero();
450  DvInfo.m_aVIdxCan = 0;
451#if H_3D_NBDV_REF
452  DvInfo.m_acDoNBDV.setZero();
453#endif
454#endif
455  Bool bBoundary = false;
456  UInt uiLPelX   = rpcBestCU->getCUPelX();
457  UInt uiRPelX   = uiLPelX + rpcBestCU->getWidth(0)  - 1;
458  UInt uiTPelY   = rpcBestCU->getCUPelY();
459  UInt uiBPelY   = uiTPelY + rpcBestCU->getHeight(0) - 1;
460
461  Int iBaseQP = xComputeQP( rpcBestCU, uiDepth );
462  Int iMinQP;
463  Int iMaxQP;
464  Bool isAddLowestQP = false;
465  Int lowestQP = -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY();
466
467  if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
468  {
469    Int idQP = m_pcEncCfg->getMaxDeltaQP();
470    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
471    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
472    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
473    {
474      isAddLowestQP = true; 
475      iMinQP = iMinQP - 1;
476    }
477  }
478  else
479  {
480    iMinQP = rpcTempCU->getQP(0);
481    iMaxQP = rpcTempCU->getQP(0);
482  }
483
484#if RATE_CONTROL_LAMBDA_DOMAIN
485  if ( m_pcEncCfg->getUseRateCtrl() )
486  {
487    iMinQP = m_pcRateCtrl->getRCQP();
488    iMaxQP = m_pcRateCtrl->getRCQP();
489  }
490#else
491  if(m_pcEncCfg->getUseRateCtrl())
492  {
493    Int qp = m_pcRateCtrl->getUnitQP();
494    iMinQP  = Clip3( MIN_QP, MAX_QP, qp);
495    iMaxQP  = Clip3( MIN_QP, MAX_QP, qp);
496  }
497#endif
498#if H_3D_IC
499  Bool bICEnabled = rpcTempCU->getSlice()->getViewIndex() && ( rpcTempCU->getSlice()->getSliceType() == P_SLICE || rpcTempCU->getSlice()->getSliceType() == B_SLICE );
500  bICEnabled = bICEnabled && rpcTempCU->getSlice()->getApplyIC();
501#endif
502  // If slice start or slice end is within this cu...
503  TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx());
504  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurStartCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart();
505  Bool bSliceEnd = (pcSlice->getSliceSegmentCurEndCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurEndCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart());
506  Bool bInsidePicture = ( uiRPelX < rpcBestCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < rpcBestCU->getSlice()->getSPS()->getPicHeightInLumaSamples() );
507  // We need to split, so don't try these modes.
508  if(!bSliceEnd && !bSliceStart && bInsidePicture )
509  {
510#if  H_3D_FAST_TEXTURE_ENCODING
511    Bool bIVFMerge = false;
512    Int  iIVFMaxD = 0;
513    Bool bFMD = false;
514#endif
515    for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
516    {
517      if (isAddLowestQP && (iQP == iMinQP))
518      {
519        iQP = lowestQP;
520      }
521      // variables for fast encoder decision
522#if H_3D_QTLPC
523      bTrySplit    = true;
524#endif
525
526      rpcTempCU->initEstData( uiDepth, iQP );
527#if H_3D_QTLPC
528      //logic for setting bTrySplit using the partition information that is stored of the texture colocated CU
529
530      if(depthMapDetect && !bIntraSliceDetect && !rapPic && sps->getUseQTL())
531      {
532        TComDataCU* pcTextureCU = pcTexture->getCU( rpcBestCU->getAddr() ); //Corresponding texture LCU
533        UInt uiCUIdx            = rpcBestCU->getZorderIdxInCU();
534        assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth); //Depth cannot be more partitionned than the texture.
535        if (pcTextureCU->getDepth(uiCUIdx) > uiDepth || pcTextureCU->getPartitionSize(uiCUIdx) == SIZE_NxN) //Texture was split.
536        {
537          bTrySplit = true;
538          bTryNx2N  = true;
539          bTry2NxN  = true;
540        }
541        else
542        {
543          bTrySplit = false;
544          bTryNx2N  = false;
545          bTry2NxN  = false;
546        }
547      }
548#endif
549
550#if H_3D_NBDV
551      if( rpcTempCU->getSlice()->getSliceType() != I_SLICE )
552      {
553#if H_3D_ARP && H_3D_IV_MERGE
554        if( rpcTempCU->getSlice()->getVPS()->getUseAdvRP(rpcTempCU->getSlice()->getLayerId()) || rpcTempCU->getSlice()->getVPS()->getIvMvPredFlag(rpcTempCU->getSlice()->getLayerId()) )
555#else
556#if H_3D_ARP
557        if( rpcTempCU->getSlice()->getVPS()->getUseAdvRP(rpcTempCU->getSlice()->getLayerId()) )
558#else
559#if H_3D_IV_MERGE
560        if( rpcTempCU->getSlice()->getVPS()->getIvMvPredFlag(rpcTempCU->getSlice()->getLayerId()) )
561#else
562        if (0)
563#endif
564#endif
565#endif
566        {
567          PartSize ePartTemp = rpcTempCU->getPartitionSize(0);
568          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );     
569#if QC_DEPTH_IV_MRG_F0125
570          if (rpcTempCU->getSlice()->getIsDepth() )
571          {
572            DvInfo.bDV = rpcTempCU->getDispNeighBlocks(0, 0, &DvInfo);
573          }
574          else
575          {
576#endif
577#if H_3D_NBDV_REF
578          if(rpcTempCU->getSlice()->getVPS()->getDepthRefinementFlag( rpcTempCU->getSlice()->getLayerIdInVps()))
579            DvInfo.bDV = rpcTempCU->getDisMvpCandNBDV(&DvInfo, true);
580          else
581#endif
582            DvInfo.bDV = rpcTempCU->getDisMvpCandNBDV(&DvInfo);
583
584#if QC_DEPTH_IV_MRG_F0125
585          }
586#endif
587          rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
588          rpcBestCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
589          rpcTempCU->setPartSizeSubParts( ePartTemp, 0, uiDepth );
590        }
591      }
592#if  H_3D_FAST_TEXTURE_ENCODING
593      if(rpcTempCU->getSlice()->getViewIndex() && !rpcTempCU->getSlice()->getIsDepth())
594      {
595        PartSize ePartTemp = rpcTempCU->getPartitionSize(0);
596        rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth ); 
597        rpcTempCU->getIVNStatus( 0, &DvInfo,  bIVFMerge, iIVFMaxD);
598        rpcTempCU->setPartSizeSubParts( ePartTemp, 0, uiDepth );
599      }
600#endif
601#endif
602      // do inter modes, SKIP and 2Nx2N
603      if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
604      {
605#if H_3D_IC
606        for( UInt uiICId = 0; uiICId < ( bICEnabled ? 2 : 1 ); uiICId++ )
607        {
608          Bool bICFlag = uiICId ? true : false;
609#endif
610        // 2Nx2N
611        if(m_pcEncCfg->getUseEarlySkipDetection())
612        {
613#if H_3D_IC
614          rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
615#endif
616#if  H_3D_FAST_TEXTURE_ENCODING
617          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFMD );  rpcTempCU->initEstData( uiDepth, iQP );//by Competition for inter_2Nx2N
618#else
619          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData( uiDepth, iQP );//by Competition for inter_2Nx2N
620#endif
621#if H_3D_VSP
622          rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
623#endif
624        }
625        // SKIP
626#if H_3D_IC
627        rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
628#endif
629        xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode );//by Merge for inter_2Nx2N
630#if  H_3D_FAST_TEXTURE_ENCODING
631        bFMD = bIVFMerge && rpcBestCU->isSkipped(0);
632#endif
633        rpcTempCU->initEstData( uiDepth, iQP );
634#if H_3D_VSP
635        rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
636#endif
637
638        if(!m_pcEncCfg->getUseEarlySkipDetection())
639        {
640          // 2Nx2N, NxN
641#if H_3D_IC
642            rpcTempCU->setICFlagSubParts(bICFlag, 0, 0, uiDepth);
643#endif
644#if  H_3D_FAST_TEXTURE_ENCODING
645            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bFMD );  rpcTempCU->initEstData( uiDepth, iQP );
646#else
647            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N );  rpcTempCU->initEstData( uiDepth, iQP );
648#endif
649#if H_3D_VSP
650            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
651#endif
652            if(m_pcEncCfg->getUseCbfFastMode())
653            {
654              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
655            }
656        }
657#if H_3D_IC
658        }
659#endif
660      }
661
662#if H_3D_QTLPC     
663      if(depthMapDetect && !bIntraSliceDetect && !rapPic && sps->getUseQTL())
664      {
665        bTrySplitDQP = bTrySplit;
666      }
667#endif
668      if (isAddLowestQP && (iQP == lowestQP))
669      {
670        iQP = iMinQP;
671      }
672    }
673
674#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
675    if ( uiDepth <= m_addSADDepth )
676    {
677      m_LCUPredictionSAD += m_temporalSAD;
678      m_addSADDepth = uiDepth;
679    }
680#endif
681#if RATE_CONTROL_LAMBDA_DOMAIN && M0036_RC_IMPROVEMENT && KWU_RC_MADPRED_E0227
682    if ( uiDepth <= m_addSADDepth )
683    {
684      m_LCUPredictionSAD += m_temporalSAD;
685      m_addSADDepth = uiDepth;
686    }
687#endif
688#if !RATE_CONTROL_LAMBDA_DOMAIN && KWU_RC_MADPRED_E0227
689if ( uiDepth <= m_addSADDepth )
690{
691  m_LCUPredictionSAD += m_temporalSAD;
692  m_addSADDepth = uiDepth;
693}
694#endif
695#if !RATE_CONTROL_LAMBDA_DOMAIN && KWU_FIX_URQ
696    if(m_pcEncCfg->getUseRateCtrl())
697    {
698      Int qp = m_pcRateCtrl->getUnitQP();
699      iMinQP  = Clip3( MIN_QP, MAX_QP, qp);
700      iMaxQP  = Clip3( MIN_QP, MAX_QP, qp);
701    }
702#endif
703
704#if H_3D_DIM_ENC
705    if( rpcBestCU->getSlice()->getIsDepth() && rpcBestCU->getSlice()->isIRAP() )
706    {
707      earlyDetectionSkipMode = false;
708    }
709#endif
710
711    if(!earlyDetectionSkipMode)
712    {
713      for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
714      {
715        if (isAddLowestQP && (iQP == iMinQP))
716        {
717          iQP = lowestQP;
718        }
719        rpcTempCU->initEstData( uiDepth, iQP );
720
721        // do inter modes, NxN, 2NxN, and Nx2N
722        if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
723        {
724          // 2Nx2N, NxN
725            if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) ))
726            {
727              if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu
728#if H_3D_QTLPC
729                && bTrySplit
730#endif
731                )
732              {
733#if  H_3D_FAST_TEXTURE_ENCODING
734                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN, bFMD  );
735#else
736                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN   );
737#endif
738                rpcTempCU->initEstData( uiDepth, iQP );
739#if H_3D_VSP
740                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
741#endif
742              }
743            }
744
745          // 2NxN, Nx2N
746          if(doNotBlockPu
747#if H_3D_QTLPC
748            && bTryNx2N
749#endif
750            )
751          {
752#if  H_3D_FAST_TEXTURE_ENCODING
753            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N, bFMD  );
754#else
755            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N  );
756#endif
757            rpcTempCU->initEstData( uiDepth, iQP );
758#if H_3D_VSP
759            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
760#endif
761            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
762            {
763              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
764            }
765          }
766          if(doNotBlockPu
767#if H_3D_QTLPC
768            && bTry2NxN
769#endif
770            )
771          {
772#if  H_3D_FAST_TEXTURE_ENCODING
773            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN, bFMD  );
774#else
775            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN  );
776#endif
777            rpcTempCU->initEstData( uiDepth, iQP );
778#if H_3D_VSP
779            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
780#endif
781            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN)
782            {
783              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
784            }
785          }
786
787#if 1
788          //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N)
789          if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) )
790          {
791#if AMP_ENC_SPEEDUP       
792            Bool bTestAMP_Hor = false, bTestAMP_Ver = false;
793
794#if AMP_MRG
795            Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false;
796
797            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver);
798#else
799            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver);
800#endif
801
802            //! Do horizontal AMP
803            if ( bTestAMP_Hor )
804            {
805              if(doNotBlockPu
806#if H_3D_QTLPC
807                && bTry2NxN
808#endif
809                )
810              {
811#if  H_3D_FAST_TEXTURE_ENCODING
812                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFMD );
813#else
814                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
815#endif
816                rpcTempCU->initEstData( uiDepth, iQP );
817#if H_3D_VSP
818                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
819#endif
820                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
821                {
822                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
823                }
824              }
825              if(doNotBlockPu
826#if H_3D_QTLPC
827                && bTry2NxN
828#endif
829                )
830              {
831#if  H_3D_FAST_TEXTURE_ENCODING
832                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFMD );
833#else
834                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
835#endif
836                rpcTempCU->initEstData( uiDepth, iQP );
837#if H_3D_VSP
838                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
839#endif
840                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
841                {
842                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
843                }
844              }
845            }
846#if AMP_MRG
847            else if ( bTestMergeAMP_Hor ) 
848            {
849              if(doNotBlockPu
850#if H_3D_QTLPC
851                && bTry2NxN
852#endif
853                )
854              {
855#if  H_3D_FAST_TEXTURE_ENCODING
856                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFMD, true );
857#else
858                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true );
859#endif
860                rpcTempCU->initEstData( uiDepth, iQP );
861#if H_3D_VSP
862                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
863#endif
864                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
865                {
866                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
867                }
868              }
869              if(doNotBlockPu
870#if H_3D_QTLPC
871                && bTry2NxN
872#endif
873                )
874              {
875#if  H_3D_FAST_TEXTURE_ENCODING
876                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFMD, true );
877#else
878                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true );
879#endif
880                rpcTempCU->initEstData( uiDepth, iQP );
881#if H_3D_VSP
882                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
883#endif
884                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
885                {
886                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
887                }
888              }
889            }
890#endif
891
892            //! Do horizontal AMP
893            if ( bTestAMP_Ver )
894            {
895              if(doNotBlockPu
896#if H_3D_QTLPC
897                && bTryNx2N
898#endif
899                )
900              {
901#if  H_3D_FAST_TEXTURE_ENCODING
902                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFMD );
903#else
904                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
905#endif
906                rpcTempCU->initEstData( uiDepth, iQP );
907#if H_3D_VSP
908                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
909#endif
910                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
911                {
912                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
913                }
914              }
915              if(doNotBlockPu
916#if H_3D_QTLPC
917                && bTryNx2N
918#endif
919                )
920              {
921#if  H_3D_FAST_TEXTURE_ENCODING
922                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFMD );
923#else
924                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
925#endif
926                rpcTempCU->initEstData( uiDepth, iQP );
927#if H_3D_VSP
928                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
929#endif
930              }
931            }
932#if AMP_MRG
933            else if ( bTestMergeAMP_Ver )
934            {
935              if(doNotBlockPu
936#if H_3D_QTLPC
937                && bTryNx2N
938#endif
939                )
940              {
941#if  H_3D_FAST_TEXTURE_ENCODING
942                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFMD, true );
943#else
944                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true );
945#endif
946                rpcTempCU->initEstData( uiDepth, iQP );
947#if H_3D_VSP
948                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
949#endif
950                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
951                {
952                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
953                }
954              }
955              if(doNotBlockPu
956#if H_3D_QTLPC
957                && bTryNx2N
958#endif
959                )
960              {
961#if  H_3D_FAST_TEXTURE_ENCODING
962                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFMD, true );
963#else
964                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true );
965#endif
966                rpcTempCU->initEstData( uiDepth, iQP );
967#if H_3D_VSP
968                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
969#endif
970              }
971            }
972#endif
973
974#else
975#if H_3D_QTLPC
976            if (bTry2NxN)
977            {
978#endif
979              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
980              rpcTempCU->initEstData( uiDepth, iQP );
981#if H_3D_VSP
982              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
983#endif
984              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
985              rpcTempCU->initEstData( uiDepth, iQP );
986#if H_3D_VSP
987              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
988#endif
989#if H_3D_QTLPC
990            }
991            if (bTryNx2N)
992            {
993#endif
994              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
995              rpcTempCU->initEstData( uiDepth, iQP );
996#if H_3D_VSP
997              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
998#endif
999              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
1000              rpcTempCU->initEstData( uiDepth, iQP );
1001#if H_3D_VSP
1002              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
1003#endif
1004#if H_3D_QTLPC
1005            }
1006#endif
1007
1008#endif
1009          }   
1010#endif
1011        }
1012#if  H_3D_FAST_TEXTURE_ENCODING
1013        if(!bFMD)
1014        {
1015#endif
1016        // do normal intra modes
1017       
1018          // speedup for inter frames
1019          if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || 
1020            rpcBestCU->getCbf( 0, TEXT_LUMA     ) != 0   ||
1021            rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0   ||
1022              rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0     
1023#if H_3D_DIM_ENC
1024            || ( rpcBestCU->getSlice()->getIsDepth() && rpcBestCU->getSlice()->isIRAP() )
1025#endif
1026            ) // avoid very complex intra if it is unlikely
1027          {
1028            xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
1029
1030#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT && KWU_RC_MADPRED_E0227
1031            if ( uiDepth <= m_addSADDepth )
1032            {
1033              m_LCUPredictionSAD += m_spatialSAD;
1034              m_addSADDepth = uiDepth;
1035            }
1036#endif
1037#if RATE_CONTROL_LAMBDA_DOMAIN && M0036_RC_IMPROVEMENT && KWU_RC_MADPRED_E0227
1038            if ( uiDepth <= m_addSADDepth )
1039            {
1040              m_LCUPredictionSAD += m_spatialSAD;
1041              m_addSADDepth = uiDepth;
1042            }
1043#endif
1044
1045#if !RATE_CONTROL_LAMBDA_DOMAIN && KWU_RC_MADPRED_E0227
1046            if ( uiDepth <= m_addSADDepth )
1047            {
1048              m_LCUPredictionSAD += m_spatialSAD;
1049              m_addSADDepth = uiDepth;
1050            }
1051#endif
1052            rpcTempCU->initEstData( uiDepth, iQP );
1053            if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
1054            {
1055#if H_3D_QTLPC //Try IntraNxN
1056              if(bTrySplit)
1057              {
1058#endif
1059                if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) )
1060                {
1061                  xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN   );
1062                  rpcTempCU->initEstData( uiDepth, iQP );
1063                }
1064#if H_3D_QTLPC
1065              }
1066#endif
1067            }
1068          }
1069        // test PCM
1070        if(pcPic->getSlice(0)->getSPS()->getUsePCM()
1071          && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize())
1072          && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) )
1073        {
1074          UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2;
1075          UInt uiBestBits = rpcBestCU->getTotalBits();
1076#if H_3D_VSO // M7
1077          Double dRDCostTemp = m_pcRdCost->getUseVSO() ? m_pcRdCost->calcRdCostVSO(uiRawBits, 0) : m_pcRdCost->calcRdCost(uiRawBits, 0);
1078          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > dRDCostTemp ))
1079#else
1080          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
1081#endif
1082          {
1083            xCheckIntraPCM (rpcBestCU, rpcTempCU);
1084            rpcTempCU->initEstData( uiDepth, iQP );
1085          }
1086        }
1087#if  H_3D_FAST_TEXTURE_ENCODING
1088        }
1089#endif
1090        if (isAddLowestQP && (iQP == lowestQP))
1091        {
1092          iQP = iMinQP;
1093        }
1094      }
1095    }
1096
1097    m_pcEntropyCoder->resetBits();
1098    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
1099    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1100    if(m_pcEncCfg->getUseSBACRD())
1101    {
1102      rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1103    }
1104
1105#if H_3D_VSO // M8
1106    if ( m_pcRdCost->getUseVSO() )   
1107      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );   
1108    else
1109#endif
1110    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
1111
1112    // Early CU determination
1113    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
1114    {
1115      bSubBranch = false;
1116    }
1117    else
1118    {
1119      bSubBranch = true;
1120    }
1121#if  H_3D_FAST_TEXTURE_ENCODING
1122    if(rpcBestCU->getSlice()->getViewIndex() && !rpcBestCU->getSlice()->getIsDepth() && (uiDepth >=iIVFMaxD) && rpcBestCU->isSkipped(0))
1123    {
1124      bSubBranch = false;
1125    }
1126#endif
1127  }
1128  else if(!(bSliceEnd && bInsidePicture))
1129  {
1130    bBoundary = true;
1131#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
1132    m_addSADDepth++;
1133#endif
1134  }
1135
1136  // copy orginal YUV samples to PCM buffer
1137  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
1138  {
1139    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
1140  }
1141  if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1142  {
1143    Int idQP = m_pcEncCfg->getMaxDeltaQP();
1144    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
1145    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
1146    if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() )
1147    {
1148      isAddLowestQP = true;
1149      iMinQP = iMinQP - 1;     
1150    }
1151  }
1152  else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1153  {
1154    iMinQP = iBaseQP;
1155    iMaxQP = iBaseQP;
1156  }
1157  else
1158  {
1159    Int iStartQP;
1160    if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr())
1161    {
1162      iStartQP = rpcTempCU->getQP(0);
1163    }
1164    else
1165    {
1166      UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1167      iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx);
1168    }
1169    iMinQP = iStartQP;
1170    iMaxQP = iStartQP;
1171  }
1172#if RATE_CONTROL_LAMBDA_DOMAIN
1173  if ( m_pcEncCfg->getUseRateCtrl() )
1174  {
1175    iMinQP = m_pcRateCtrl->getRCQP();
1176    iMaxQP = m_pcRateCtrl->getRCQP();
1177  }
1178#else
1179  if(m_pcEncCfg->getUseRateCtrl())
1180  {
1181    Int qp = m_pcRateCtrl->getUnitQP();
1182    iMinQP  = Clip3( MIN_QP, MAX_QP, qp);
1183    iMaxQP  = Clip3( MIN_QP, MAX_QP, qp);
1184  }
1185#endif
1186  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
1187  {
1188    if (isAddLowestQP && (iQP == iMinQP))
1189    {
1190      iQP = lowestQP;
1191    }
1192    rpcTempCU->initEstData( uiDepth, iQP );
1193
1194    // further split
1195#if H_3D_QTLPC
1196    if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
1197#else
1198    if( bSubBranch && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
1199#endif
1200    {
1201#if H_3D_VSO // M9
1202      // reset Model
1203      if( m_pcRdCost->getUseRenModel() )
1204      {
1205        UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth ( );
1206        UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight( );
1207        Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr( 0 );
1208        UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride();
1209        m_pcRdCost->setRenModelData( m_ppcBestCU[uiDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1210      }
1211#endif
1212
1213      UChar       uhNextDepth         = uiDepth+1;
1214      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
1215      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
1216
1217      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
1218      {
1219        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1220        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1221
1222        Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr();
1223        if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1224        {
1225          if( m_bUseSBACRD )
1226          {
1227            if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
1228            {
1229              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1230            }
1231            else
1232            {
1233              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
1234            }
1235          }
1236
1237#if AMP_ENC_SPEEDUP
1238          if ( rpcBestCU->isIntra(0) )
1239          {
1240            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE );
1241          }
1242          else
1243          {
1244            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) );
1245          }
1246#else
1247          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
1248#endif
1249
1250          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
1251          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
1252        }
1253        else if (bInSlice)
1254        {
1255          pcSubBestPartCU->copyToPic( uhNextDepth );
1256          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
1257        }
1258      }
1259
1260      if( !bBoundary )
1261      {
1262        m_pcEntropyCoder->resetBits();
1263        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
1264
1265        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1266        if(m_pcEncCfg->getUseSBACRD())
1267        {
1268          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1269        }
1270      }
1271
1272#if H_3D_VSO // M10
1273      if ( m_pcRdCost->getUseVSO() )
1274        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1275      else
1276#endif
1277      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1278
1279      if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
1280      {
1281        Bool hasResidual = false;
1282        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
1283        {
1284          if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && 
1285              ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
1286          {
1287            hasResidual = true;
1288            break;
1289          }
1290        }
1291
1292        UInt uiTargetPartIdx;
1293        if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() )
1294        {
1295          uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1296        }
1297        else
1298        {
1299          uiTargetPartIdx = 0;
1300        }
1301        if ( hasResidual )
1302        {
1303#if !RDO_WITHOUT_DQP_BITS
1304          m_pcEntropyCoder->resetBits();
1305          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
1306          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1307          if(m_pcEncCfg->getUseSBACRD())
1308          {
1309            rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1310          }
1311#if H_3D_VSO // M11
1312          if ( m_pcRdCost->getUseLambdaScaleVSO())         
1313            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );         
1314          else
1315#endif
1316          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1317#endif
1318
1319          Bool foundNonZeroCbf = false;
1320          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf );
1321          assert( foundNonZeroCbf );
1322        }
1323        else
1324        {
1325          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
1326        }
1327      }
1328
1329      if( m_bUseSBACRD )
1330      {
1331        m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1332      }
1333      Bool isEndOfSlice        = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES
1334                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
1335      Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
1336                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
1337      if(isEndOfSlice||isEndOfSliceSegment)
1338      {
1339        rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
1340      }
1341      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth);                                  // RD compare current larger prediction
1342    }                                                                                  // with sub partitioned prediction.
1343    if (isAddLowestQP && (iQP == lowestQP))
1344    {
1345      iQP = iMinQP;
1346    }
1347  }
1348
1349
1350#if H_3D_VSO // M12
1351  if( m_pcRdCost->getUseRenModel() )
1352  {
1353    UInt  uiWidth     = m_ppcRecoYuvBest[uiDepth]->getWidth   ( );
1354    UInt  uiHeight    = m_ppcRecoYuvBest[uiDepth]->getHeight  ( );
1355    Pel*  piSrc       = m_ppcRecoYuvBest[uiDepth]->getLumaAddr( 0 );
1356    UInt  uiSrcStride = m_ppcRecoYuvBest[uiDepth]->getStride  ( );
1357    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1358  }
1359#endif
1360
1361  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
1362
1363  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY );   // Copy Yuv data to picture Yuv
1364  if( bBoundary ||(bSliceEnd && bInsidePicture))
1365  {
1366    return;
1367  }
1368
1369  // Assert if Best prediction mode is NONE
1370  // Selected mode's RD-cost must be not MAX_DOUBLE.
1371  assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE  );
1372  assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE  );
1373  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE );
1374}
1375
1376/** finish encoding a cu and handle end-of-slice conditions
1377 * \param pcCU
1378 * \param uiAbsPartIdx
1379 * \param uiDepth
1380 * \returns Void
1381 */
1382Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1383{
1384  TComPic* pcPic = pcCU->getPic();
1385  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1386
1387  //Calculate end address
1388  UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx;
1389
1390  UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU();
1391  UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU();
1392  UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1393  UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1394  UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
1395  UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
1396  while(uiPosX>=uiWidth||uiPosY>=uiHeight)
1397  {
1398    uiInternalAddress--;
1399    uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1400    uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1401  }
1402  uiInternalAddress++;
1403  if(uiInternalAddress==pcCU->getPic()->getNumPartInCU())
1404  {
1405    uiInternalAddress = 0;
1406    uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1);
1407  }
1408  UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress);
1409
1410  // Encode slice finish
1411  Bool bTerminateSlice = false;
1412  if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress)
1413  {
1414    bTerminateSlice = true;
1415  }
1416  UInt uiGranularityWidth = g_uiMaxCUWidth;
1417  uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1418  uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1419  Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth))
1420    &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight));
1421 
1422  if(granularityBoundary)
1423  {
1424    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1425    if (!bTerminateSlice)
1426      m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 );
1427  }
1428 
1429  Int numberOfWrittenBits = 0;
1430  if (m_pcBitCounter)
1431  {
1432    numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits();
1433  }
1434 
1435  // Calculate slice end IF this CU puts us over slice bit size.
1436  UInt iGranularitySize = pcCU->getPic()->getNumPartInCU();
1437  Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize;
1438  if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) 
1439  {
1440    iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)));
1441  }
1442  // Set slice end parameter
1443  if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) 
1444  {
1445    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1446    pcSlice->setSliceCurEndCUAddr(iGranularityEnd);
1447    return;
1448  }
1449  // Set dependent slice end parameter
1450  if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) 
1451  {
1452    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1453    return;
1454  }
1455  if(granularityBoundary)
1456  {
1457    pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) );
1458    pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits);
1459    if (m_pcBitCounter)
1460    {
1461      m_pcEntropyCoder->resetBits();     
1462    }
1463  }
1464}
1465
1466/** Compute QP for each CU
1467 * \param pcCU Target CU
1468 * \param uiDepth CU depth
1469 * \returns quantization parameter
1470 */
1471Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1472{
1473  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1474  Int iQpOffset = 0;
1475  if ( m_pcEncCfg->getUseAdaptiveQP() )
1476  {
1477    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1478    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1479    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1480    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1481    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1482    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1483    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1484
1485    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1486    Double dAvgAct = pcAQLayer->getAvgActivity();
1487    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1488    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1489    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1490    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1491  }
1492  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset );
1493}
1494
1495/** encode a CU block recursively
1496 * \param pcCU
1497 * \param uiAbsPartIdx
1498 * \param uiDepth
1499 * \returns Void
1500 */
1501Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1502{
1503  TComPic* pcPic = pcCU->getPic();
1504 
1505  Bool bBoundary = false;
1506  UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1507  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1508  UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1509  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1510 
1511#if H_MV_ENC_DEC_TRAC
1512  DTRACE_CU_S("=========== coding_quadtree ===========\n")
1513  DTRACE_CU("x0", uiLPelX)
1514  DTRACE_CU("x1", uiTPelY)
1515  DTRACE_CU("log2CbSize", g_uiMaxCUWidth>>uiDepth)
1516  DTRACE_CU("cqtDepth"  , uiDepth)
1517#endif
1518
1519  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1520  // If slice start is within this cu...
1521  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1522    pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) );
1523  // We need to split, so don't try these modes.
1524  if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1525  {
1526    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1527  }
1528  else
1529  {
1530    bBoundary = true;
1531  }
1532 
1533  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary )
1534  {
1535    UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2;
1536    if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1537    {
1538      setdQPFlag(true);
1539    }
1540    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1541    {
1542      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1543      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1544      Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr();
1545      if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1546      {
1547        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1548      }
1549    }
1550    return;
1551  }
1552 
1553#if H_MV_ENC_DEC_TRAC
1554  DTRACE_CU_S("=========== coding_unit ===========\n")
1555#endif
1556
1557  if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1558  {
1559    setdQPFlag(true);
1560  }
1561  if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1562  {
1563    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1564  }
1565  if( !pcCU->getSlice()->isIntra() )
1566  {
1567    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1568  }
1569 
1570  if( pcCU->isSkipped( uiAbsPartIdx ) )
1571  {
1572#if H_MV_ENC_DEC_TRAC
1573    DTRACE_PU_S("=========== prediction_unit ===========\n")
1574    DTRACE_PU("x0", uiLPelX)
1575    DTRACE_PU("x1", uiTPelY)
1576#endif
1577    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1578#if H_3D_IC
1579    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1580#endif
1581#if H_3D_ARP
1582    m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1583#endif
1584    finishCU(pcCU,uiAbsPartIdx,uiDepth);
1585    return;
1586  }
1587  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1588 
1589  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1590 
1591  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1592  {
1593    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1594
1595    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1596    {
1597      // Encode slice finish
1598      finishCU(pcCU,uiAbsPartIdx,uiDepth);
1599      return;
1600    }
1601  }
1602
1603  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1604  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1605#if H_3D_IC
1606  m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1607#endif
1608#if H_3D_ARP
1609  m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1610#endif
1611#if H_3D_INTER_SDC
1612  m_pcEntropyCoder->encodeInterSDCFlag( pcCU, uiAbsPartIdx, false );
1613#endif
1614
1615  // Encode Coefficients
1616  Bool bCodeDQP = getdQPFlag();
1617  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP );
1618  setdQPFlag( bCodeDQP );
1619
1620  // --- write terminating bit ---
1621  finishCU(pcCU,uiAbsPartIdx,uiDepth);
1622}
1623
1624#if RATE_CONTROL_INTRA
1625Int xCalcHADs8x8_ISlice(Pel *piOrg, Int iStrideOrg) 
1626{
1627  Int k, i, j, jj;
1628  Int diff[64], m1[8][8], m2[8][8], m3[8][8], iSumHad = 0;
1629
1630  for( k = 0; k < 64; k += 8 )
1631  {
1632    diff[k+0] = piOrg[0] ;
1633    diff[k+1] = piOrg[1] ;
1634    diff[k+2] = piOrg[2] ;
1635    diff[k+3] = piOrg[3] ;
1636    diff[k+4] = piOrg[4] ;
1637    diff[k+5] = piOrg[5] ;
1638    diff[k+6] = piOrg[6] ;
1639    diff[k+7] = piOrg[7] ;
1640 
1641    piOrg += iStrideOrg;
1642  }
1643 
1644  //horizontal
1645  for (j=0; j < 8; j++)
1646  {
1647    jj = j << 3;
1648    m2[j][0] = diff[jj  ] + diff[jj+4];
1649    m2[j][1] = diff[jj+1] + diff[jj+5];
1650    m2[j][2] = diff[jj+2] + diff[jj+6];
1651    m2[j][3] = diff[jj+3] + diff[jj+7];
1652    m2[j][4] = diff[jj  ] - diff[jj+4];
1653    m2[j][5] = diff[jj+1] - diff[jj+5];
1654    m2[j][6] = diff[jj+2] - diff[jj+6];
1655    m2[j][7] = diff[jj+3] - diff[jj+7];
1656   
1657    m1[j][0] = m2[j][0] + m2[j][2];
1658    m1[j][1] = m2[j][1] + m2[j][3];
1659    m1[j][2] = m2[j][0] - m2[j][2];
1660    m1[j][3] = m2[j][1] - m2[j][3];
1661    m1[j][4] = m2[j][4] + m2[j][6];
1662    m1[j][5] = m2[j][5] + m2[j][7];
1663    m1[j][6] = m2[j][4] - m2[j][6];
1664    m1[j][7] = m2[j][5] - m2[j][7];
1665   
1666    m2[j][0] = m1[j][0] + m1[j][1];
1667    m2[j][1] = m1[j][0] - m1[j][1];
1668    m2[j][2] = m1[j][2] + m1[j][3];
1669    m2[j][3] = m1[j][2] - m1[j][3];
1670    m2[j][4] = m1[j][4] + m1[j][5];
1671    m2[j][5] = m1[j][4] - m1[j][5];
1672    m2[j][6] = m1[j][6] + m1[j][7];
1673    m2[j][7] = m1[j][6] - m1[j][7];
1674  }
1675 
1676  //vertical
1677  for (i=0; i < 8; i++)
1678  {
1679    m3[0][i] = m2[0][i] + m2[4][i];
1680    m3[1][i] = m2[1][i] + m2[5][i];
1681    m3[2][i] = m2[2][i] + m2[6][i];
1682    m3[3][i] = m2[3][i] + m2[7][i];
1683    m3[4][i] = m2[0][i] - m2[4][i];
1684    m3[5][i] = m2[1][i] - m2[5][i];
1685    m3[6][i] = m2[2][i] - m2[6][i];
1686    m3[7][i] = m2[3][i] - m2[7][i];
1687   
1688    m1[0][i] = m3[0][i] + m3[2][i];
1689    m1[1][i] = m3[1][i] + m3[3][i];
1690    m1[2][i] = m3[0][i] - m3[2][i];
1691    m1[3][i] = m3[1][i] - m3[3][i];
1692    m1[4][i] = m3[4][i] + m3[6][i];
1693    m1[5][i] = m3[5][i] + m3[7][i];
1694    m1[6][i] = m3[4][i] - m3[6][i];
1695    m1[7][i] = m3[5][i] - m3[7][i];
1696   
1697    m2[0][i] = m1[0][i] + m1[1][i];
1698    m2[1][i] = m1[0][i] - m1[1][i];
1699    m2[2][i] = m1[2][i] + m1[3][i];
1700    m2[3][i] = m1[2][i] - m1[3][i];
1701    m2[4][i] = m1[4][i] + m1[5][i];
1702    m2[5][i] = m1[4][i] - m1[5][i];
1703    m2[6][i] = m1[6][i] + m1[7][i];
1704    m2[7][i] = m1[6][i] - m1[7][i];
1705  }
1706 
1707  for (i = 0; i < 8; i++)
1708  {
1709    for (j = 0; j < 8; j++)
1710    {
1711      iSumHad += abs(m2[i][j]);
1712    }
1713  }
1714  iSumHad -= abs(m2[0][0]);
1715  iSumHad =(iSumHad+2)>>2;
1716  return(iSumHad);
1717}
1718
1719Int  TEncCu::updateLCUDataISlice(TComDataCU* pcCU, Int LCUIdx, Int width, Int height)
1720{
1721  Int  xBl, yBl; 
1722  const Int iBlkSize = 8;
1723
1724  Pel* pOrgInit   = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
1725  Int  iStrideOrig = pcCU->getPic()->getPicYuvOrg()->getStride();
1726  Pel  *pOrg;
1727
1728  Int iSumHad = 0;
1729  for ( yBl=0; (yBl+iBlkSize)<=height; yBl+= iBlkSize)
1730  {
1731    for ( xBl=0; (xBl+iBlkSize)<=width; xBl+= iBlkSize)
1732    {
1733      pOrg = pOrgInit + iStrideOrig*yBl + xBl; 
1734      iSumHad += xCalcHADs8x8_ISlice(pOrg, iStrideOrig);
1735    }
1736  }
1737  return(iSumHad);
1738}
1739#endif
1740
1741/** check RD costs for a CU block encoded with merge
1742 * \param rpcBestCU
1743 * \param rpcTempCU
1744 * \returns Void
1745 */
1746Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode )
1747{
1748  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1749#if H_3D_IV_MERGE
1750  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS_MEM << 1]; // double length for mv of both lists
1751  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS_MEM];
1752#else
1753  TComMvField  cMvFieldNeighbours[2 * MRG_MAX_NUM_CANDS]; // double length for mv of both lists
1754  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1755#endif
1756  Int numValidMergeCand = 0;
1757
1758  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1759  {
1760    uhInterDirNeighbours[ui] = 0;
1761  }
1762  UChar uhDepth = rpcTempCU->getDepth( 0 );
1763#if H_3D_IC
1764  Bool bICFlag = rpcTempCU->getICFlag( 0 );
1765#endif
1766#if H_3D_VSO // M1  //nececcary here?
1767  if( m_pcRdCost->getUseRenModel() )
1768  {
1769    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1770    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1771    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1772    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1773    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1774  }
1775#endif
1776
1777  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1778  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth );
1779
1780#if H_3D_VSP
1781  Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1782  memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1783  InheritedVSPDisInfo inheritedVSPDisInfo[MRG_MAX_NUM_CANDS_MEM];
1784  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag,inheritedVSPDisInfo, numValidMergeCand );
1785#else
1786  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1787#endif
1788
1789#if H_3D_IV_MERGE
1790  Int mergeCandBuffer[MRG_MAX_NUM_CANDS_MEM];
1791#else
1792  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1793#endif
1794for( UInt ui = 0; ui < numValidMergeCand; ++ui )
1795  {
1796    mergeCandBuffer[ui] = 0;
1797  }
1798
1799  Bool bestIsSkip = false;
1800
1801  UInt iteration;
1802  if ( rpcTempCU->isLosslessCoded(0))
1803  {
1804    iteration = 1;
1805  }
1806  else 
1807  {
1808    iteration = 2;
1809  }
1810
1811#if H_3D_ARP
1812  Int nARPWMax = rpcTempCU->getSlice()->getARPStepNum() - 1;
1813  if( nARPWMax < 0 || !rpcTempCU->getDvInfo(0).bDV )
1814  {
1815    nARPWMax = 0;
1816  }
1817  for( Int nARPW=nARPWMax; nARPW >= 0 ; nARPW-- )
1818  {
1819    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS*sizeof(Int) );
1820#endif
1821  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1822  {
1823    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1824    {     
1825#if H_3D_IC
1826        if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
1827        {
1828          if( bICFlag && uiMergeCand == 0 ) 
1829          {
1830            continue;
1831          }
1832        }
1833#endif
1834        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1835        {
1836        if( !(bestIsSkip && uiNoResidual == 0) )
1837        {
1838          // set MC parameters
1839          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1840          rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(),     0, uhDepth );
1841          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1842#if H_3D_IC
1843          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1844#endif
1845#if H_3D_ARP
1846          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1847#endif
1848          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1849          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1850#if H_3D_VSP
1851          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
1852          rpcTempCU->setDvInfoSubParts(inheritedVSPDisInfo[uiMergeCand].m_acDvInfo, 0, 0, uhDepth );
1853#endif
1854          rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1855          rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1856          rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1857
1858#if H_3D_ARP
1859          if( nARPW )
1860          {
1861            Bool bSignalflag[2] = { true, true };
1862            for( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
1863            {
1864              Int iRefIdx = cMvFieldNeighbours[uiRefListIdx + 2*uiMergeCand].getRefIdx();
1865              RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1866              if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
1867              {
1868                bSignalflag[uiRefListIdx] = false;
1869              }
1870            }
1871            if( !bSignalflag[0] && !bSignalflag[1] )
1872            {
1873              rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
1874            }
1875          }
1876#endif
1877       // do MC
1878       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1879       // estimate residual and encode everything
1880#if H_3D_VSO //M2
1881       if( m_pcRdCost->getUseRenModel() )
1882       { //Reset
1883         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1884         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1885         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1886         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1887         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1888       }
1889#endif
1890       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1891         m_ppcOrigYuv    [uhDepth],
1892         m_ppcPredYuvTemp[uhDepth],
1893         m_ppcResiYuvTemp[uhDepth],
1894         m_ppcResiYuvBest[uhDepth],
1895         m_ppcRecoYuvTemp[uhDepth],
1896         (uiNoResidual? true:false));
1897
1898
1899          if ( uiNoResidual == 0 && rpcTempCU->getQtRootCbf(0) == 0 )
1900         {
1901            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
1902           mergeCandBuffer[uiMergeCand] = 1;
1903         }
1904
1905          rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
1906#if H_3D_INTER_SDC
1907          TComDataCU *rpcTempCUPre = rpcTempCU;
1908#endif
1909          Int orgQP = rpcTempCU->getQP( 0 );
1910          xCheckDQP( rpcTempCU );
1911          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1912#if H_3D_INTER_SDC
1913          if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() && !uiNoResidual )
1914          {
1915            if( rpcTempCU != rpcTempCUPre )
1916            {
1917              rpcTempCU->initEstData( uhDepth, orgQP );
1918              rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
1919            }
1920            rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
1921            rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
1922            rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
1923#if H_3D_VSO //M2
1924            if( m_pcRdCost->getUseRenModel() )
1925            { //Reset
1926              UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1927              UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1928              Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1929              UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1930              m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1931            }
1932#endif
1933            m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
1934              m_ppcOrigYuv[uhDepth], 
1935              ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth], 
1936              m_ppcResiYuvTemp[uhDepth], 
1937              m_ppcRecoYuvTemp[uhDepth], 
1938              uhDepth );
1939
1940            xCheckDQP( rpcTempCU );
1941            xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
1942          }
1943#endif
1944          rpcTempCU->initEstData( uhDepth, orgQP );
1945
1946      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
1947      {
1948#if H_3D_INTER_SDC
1949        if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) )
1950        {
1951          bestIsSkip = !rpcBestCU->getSDCFlag( 0 ) && ( rpcBestCU->getQtRootCbf(0) == 0 );
1952        }
1953        else
1954        {
1955#endif
1956        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
1957#if H_3D_INTER_SDC
1958        }
1959#endif
1960      }
1961    }
1962   }
1963  }
1964
1965  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
1966  {
1967    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
1968    {
1969      if( rpcBestCU->getMergeFlag( 0 ))
1970      {
1971        *earlyDetectionSkipMode = true;
1972      }
1973      else
1974      {
1975        Int absoulte_MV=0;
1976        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1977        {
1978          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1979          {
1980            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
1981            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
1982            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
1983            absoulte_MV+=iHor+iVer;
1984          }
1985        }
1986
1987        if(absoulte_MV == 0)
1988        {
1989          *earlyDetectionSkipMode = true;
1990        }
1991      }
1992    }
1993  }
1994 }
1995#if H_3D_ARP
1996 }
1997#endif
1998}
1999
2000
2001#if AMP_MRG
2002#if  H_3D_FAST_TEXTURE_ENCODING
2003Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bFMD, Bool bUseMRG)
2004#else
2005Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
2006#endif
2007#else
2008Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
2009#endif
2010{
2011#if  H_3D_FAST_TEXTURE_ENCODING
2012  if(!(bFMD && (ePartSize == SIZE_2Nx2N)))  //have  motion estimation or merge check
2013  {
2014#endif
2015  UChar uhDepth = rpcTempCU->getDepth( 0 );
2016#if H_3D_ARP
2017  Int iLayerId    = rpcTempCU->getSlice()->getLayerId();
2018  Bool bFirstTime = true;
2019  Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
2020
2021  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || !rpcTempCU->getDvInfo(0).bDV  )
2022  {
2023    nARPWMax = 0;
2024  }
2025
2026  for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
2027  {
2028    if( bFirstTime == false && rpcTempCU->getSlice()->getVPS()->getUseAdvRP( iLayerId ) )
2029    {
2030      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0) );
2031    }
2032#endif
2033#if H_3D_VSO // M3
2034  if( m_pcRdCost->getUseRenModel() )
2035  {
2036    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2037    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2038    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2039    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2040    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2041  }
2042#endif
2043
2044  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2045 
2046  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2047
2048  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
2049  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2050  rpcTempCU->setCUTransquantBypassSubParts  ( m_pcEncCfg->getCUTransquantBypassFlagValue(),      0, uhDepth );
2051 
2052#if H_3D_ARP
2053  rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2054#endif
2055
2056#if H_3D_ARP
2057  if( bFirstTime == false && nARPWMax )
2058  {
2059    rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
2060    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2061
2062    m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
2063
2064    if(rpcTempCU->getPartitionSize(0)==SIZE_2Nx2N)
2065    {
2066      Bool bSignalflag[2] = { true, true };
2067      for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
2068      {
2069        RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
2070        Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
2071        if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
2072        {
2073          bSignalflag[uiRefListIdx] = false;
2074        }
2075      }
2076      if( !bSignalflag[0] && !bSignalflag[1] )
2077      {
2078        rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
2079      }
2080    }
2081  }
2082  else
2083  {
2084    bFirstTime = false;
2085#endif
2086#if AMP_MRG
2087  rpcTempCU->setMergeAMP (true);
2088#if  H_3D_FAST_TEXTURE_ENCODING
2089  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bFMD, false, bUseMRG );
2090#else
2091  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
2092#endif
2093#else 
2094  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
2095#endif
2096#if H_3D_ARP
2097   if( nARPWMax )
2098   {
2099     m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
2100
2101     Bool bSignalflag[2] = { true, true };
2102     for(UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx ++ )
2103     {
2104       RefPicList eRefList = uiRefListIdx ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
2105       Int iRefIdx = rpcTempCU->getCUMvField(eRefList)->getRefIdx(0);
2106       if( iRefIdx < 0 || rpcTempCU->getSlice()->getPOC() == rpcTempCU->getSlice()->getRefPOC(eRefList, iRefIdx) )
2107       {
2108         bSignalflag[uiRefListIdx] = false;
2109       }
2110     }
2111     if( !bSignalflag[0] && !bSignalflag[1])
2112     {
2113       rpcTempCU->setARPWSubParts( 0 , 0 , uhDepth );
2114     }
2115   }
2116  }
2117#endif
2118
2119#if AMP_MRG
2120  if ( !rpcTempCU->getMergeAMP() )
2121  {
2122#if H_3D_ARP
2123    if( nARPWMax )
2124    {
2125      continue;
2126    }
2127    else
2128#endif
2129    return;
2130  }
2131#endif
2132
2133#if RATE_CONTROL_LAMBDA_DOMAIN  && (!M0036_RC_IMPROVEMENT || KWU_RC_MADPRED_E0227)
2134  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2135  {
2136    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2137      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2138      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2139    m_temporalSAD = (Int)SAD;
2140  }
2141#endif
2142#if !RATE_CONTROL_LAMBDA_DOMAIN  && KWU_RC_MADPRED_E0227
2143  if ( m_pcEncCfg->getUseRateCtrl() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2144  {
2145    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2146      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2147      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2148    m_temporalSAD = (Int)SAD;
2149  }
2150#endif
2151
2152  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
2153
2154
2155#if H_3D_VSO // M4
2156  if( m_pcRdCost->getUseLambdaScaleVSO() )
2157    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2158  else
2159#endif
2160  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2161#if H_3D_INTER_SDC
2162  TComDataCU *rpcTempCUPre = rpcTempCU;
2163#endif
2164  xCheckDQP( rpcTempCU );
2165  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2166#if H_3D_INTER_SDC
2167  if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() )
2168  {
2169    if( rpcTempCU != rpcTempCUPre )
2170    {
2171      Int orgQP = rpcBestCU->getQP( 0 );
2172      rpcTempCU->initEstData( uhDepth, orgQP );
2173      rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
2174    }
2175    rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2176    rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
2177    rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
2178#if H_3D_VSO // M3
2179    if( m_pcRdCost->getUseRenModel() )
2180    {
2181      UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2182      UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2183      Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2184      UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2185      m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2186    }
2187#endif
2188
2189    m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2190      m_ppcOrigYuv[uhDepth],
2191      ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth],
2192      m_ppcResiYuvTemp[uhDepth],
2193      m_ppcRecoYuvTemp[uhDepth],
2194      uhDepth );
2195
2196  xCheckDQP( rpcTempCU );
2197  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2198  }
2199#endif
2200#if H_3D_ARP
2201  }
2202#endif
2203#if  H_3D_FAST_TEXTURE_ENCODING
2204  }
2205#endif
2206}
2207
2208Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2209{
2210  UInt uiDepth = rpcTempCU->getDepth( 0 );
2211 
2212#if H_3D_VSO // M5
2213  if( m_pcRdCost->getUseRenModel() )
2214  {
2215    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
2216    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
2217    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
2218    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
2219    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2220  }
2221#endif
2222
2223  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2224
2225  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
2226  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2227  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
2228 
2229  Bool bSeparateLumaChroma = true; // choose estimation mode
2230  UInt uiPreCalcDistC      = 0;
2231  if( !bSeparateLumaChroma )
2232  {
2233    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
2234  }
2235  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
2236
2237  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
2238 
2239#if H_3D_DIM_SDC
2240  if( !rpcTempCU->getSDCFlag( 0 ) )
2241#endif
2242  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
2243 
2244  m_pcEntropyCoder->resetBits();
2245  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2246  {
2247    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2248  }
2249  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2250  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
2251  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
2252  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
2253  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
2254
2255  // Encode Coefficients
2256  Bool bCodeDQP = getdQPFlag();
2257  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
2258  setdQPFlag( bCodeDQP );
2259 
2260  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2261 
2262  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2263  if(m_pcEncCfg->getUseSBACRD())
2264  {
2265    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2266  }
2267
2268#if H_3D_VSO // M6
2269  if( m_pcRdCost->getUseLambdaScaleVSO()) 
2270    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
2271  else
2272#endif
2273  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2274 
2275#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT && KWU_RC_MADPRED_E0227
2276  UChar uhDepth = rpcTempCU->getDepth( 0 );
2277  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && eSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2278  {
2279    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2280      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2281      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2282    m_spatialSAD = (Int)SAD;
2283  }
2284#endif
2285#if RATE_CONTROL_LAMBDA_DOMAIN && M0036_RC_IMPROVEMENT && KWU_RC_MADPRED_E0227
2286  UChar uhDepth = rpcTempCU->getDepth( 0 );
2287  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && eSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2288  {
2289    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2290      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2291      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2292    m_spatialSAD = (Int)SAD;
2293  }
2294#endif
2295#if !RATE_CONTROL_LAMBDA_DOMAIN && KWU_RC_MADPRED_E0227
2296  UChar uhDepth = rpcTempCU->getDepth( 0 );
2297  if ( m_pcEncCfg->getUseRateCtrl() && eSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2298  {
2299    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2300      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2301      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2302    m_spatialSAD = (Int)SAD;
2303  }
2304#endif
2305  xCheckDQP( rpcTempCU );
2306  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2307}
2308
2309/** Check R-D costs for a CU with PCM mode.
2310 * \param rpcBestCU pointer to best mode CU data structure
2311 * \param rpcTempCU pointer to testing mode CU data structure
2312 * \returns Void
2313 *
2314 * \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.
2315 */
2316Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
2317{
2318  UInt uiDepth = rpcTempCU->getDepth( 0 );
2319
2320  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2321
2322  rpcTempCU->setIPCMFlag(0, true);
2323  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
2324  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2325  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2326  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
2327  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
2328
2329  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
2330
2331  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
2332
2333  m_pcEntropyCoder->resetBits();
2334  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2335  {
2336    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2337  }
2338  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2339  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
2340  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
2341  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
2342
2343  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2344
2345  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2346  if(m_pcEncCfg->getUseSBACRD())
2347  {
2348    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2349  }
2350#if H_3D_VSO // M44
2351  if ( m_pcRdCost->getUseVSO() )
2352    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2353  else
2354#endif
2355  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2356
2357  xCheckDQP( rpcTempCU );
2358  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
2359}
2360
2361/** check whether current try is the best with identifying the depth of current try
2362 * \param rpcBestCU
2363 * \param rpcTempCU
2364 * \returns Void
2365 */
2366Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
2367{
2368  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
2369  {
2370    TComYuv* pcYuv;
2371    // Change Information data
2372    TComDataCU* pcCU = rpcBestCU;
2373    rpcBestCU = rpcTempCU;
2374    rpcTempCU = pcCU;
2375
2376    // Change Prediction data
2377    pcYuv = m_ppcPredYuvBest[uiDepth];
2378    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
2379    m_ppcPredYuvTemp[uiDepth] = pcYuv;
2380
2381    // Change Reconstruction data
2382    pcYuv = m_ppcRecoYuvBest[uiDepth];
2383    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
2384    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
2385
2386    pcYuv = NULL;
2387    pcCU  = NULL;
2388
2389    if( m_bUseSBACRD )  // store temp best CI for next CU coding
2390      m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
2391  }
2392}
2393
2394Void TEncCu::xCheckDQP( TComDataCU* pcCU )
2395{
2396  UInt uiDepth = pcCU->getDepth( 0 );
2397
2398  if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() )
2399  {
2400    if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) )
2401    {
2402#if !RDO_WITHOUT_DQP_BITS
2403      m_pcEntropyCoder->resetBits();
2404      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
2405      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
2406      if(m_pcEncCfg->getUseSBACRD())
2407      {
2408        pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2409      }
2410#if H_3D_VSO // M45
2411      if ( m_pcRdCost->getUseVSO() )     
2412        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
2413      else
2414#endif
2415      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
2416#endif
2417    }
2418    else
2419    {
2420      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
2421    }
2422  }
2423}
2424
2425Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
2426{
2427  pDst->iN = pSrc->iN;
2428  for (Int i = 0; i < pSrc->iN; i++)
2429  {
2430    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
2431  }
2432}
2433Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY )
2434{
2435  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
2436  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
2437  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
2438  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2439    pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2440  Bool bSliceEnd   = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2441    pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2442  if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2443  {
2444    UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
2445    UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth);
2446    UInt uiBlkWidth    = rpcPic->getNumPartInWidth() >> (uiDepth);
2447    UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2448    UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2449    UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
2450    m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
2451  }
2452  else
2453  {
2454    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2;
2455
2456    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
2457    {
2458      UInt uiSubCULPelX   = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx &  1 );
2459      UInt uiSubCUTPelY   = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 );
2460
2461      Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && 
2462        rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr();
2463      if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2464      {
2465        xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY );   // Copy Yuv data to picture Yuv
2466      }
2467    }
2468  }
2469}
2470
2471Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
2472{
2473  UInt uiCurrDepth = uiNextDepth - 1;
2474  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
2475}
2476
2477/** Function for filling the PCM buffer of a CU using its original sample array
2478 * \param pcCU pointer to current CU
2479 * \param pcOrgYuv pointer to original sample array
2480 * \returns Void
2481 */
2482Void TEncCu::xFillPCMBuffer     ( TComDataCU*& pCU, TComYuv* pOrgYuv )
2483{
2484
2485  UInt   width        = pCU->getWidth(0);
2486  UInt   height       = pCU->getHeight(0);
2487
2488  Pel*   pSrcY = pOrgYuv->getLumaAddr(0, width); 
2489  Pel*   pDstY = pCU->getPCMSampleY();
2490  UInt   srcStride = pOrgYuv->getStride();
2491
2492  for(Int y = 0; y < height; y++ )
2493  {
2494    for(Int x = 0; x < width; x++ )
2495    {
2496      pDstY[x] = pSrcY[x];
2497    }
2498    pDstY += width;
2499    pSrcY += srcStride;
2500  }
2501
2502  Pel* pSrcCb       = pOrgYuv->getCbAddr();
2503  Pel* pSrcCr       = pOrgYuv->getCrAddr();;
2504
2505  Pel* pDstCb       = pCU->getPCMSampleCb();
2506  Pel* pDstCr       = pCU->getPCMSampleCr();;
2507
2508  UInt srcStrideC = pOrgYuv->getCStride();
2509  UInt heightC   = height >> 1;
2510  UInt widthC    = width  >> 1;
2511
2512  for(Int y = 0; y < heightC; y++ )
2513  {
2514    for(Int x = 0; x < widthC; x++ )
2515    {
2516      pDstCb[x] = pSrcCb[x];
2517      pDstCr[x] = pSrcCr[x];
2518    }
2519    pDstCb += widthC;
2520    pDstCr += widthC;
2521    pSrcCb += srcStrideC;
2522    pSrcCr += srcStrideC;
2523  }
2524}
2525
2526#if ADAPTIVE_QP_SELECTION
2527/** Collect ARL statistics from one block
2528  */
2529Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
2530{
2531  for( Int n = 0; n < NumCoeffInCU; n++ )
2532  {
2533    Int u = abs( rpcCoeff[ n ] );
2534    Int absc = rpcArlCoeff[ n ];
2535
2536    if( u != 0 )
2537    {
2538      if( u < LEVEL_RANGE )
2539      {
2540        cSum[ u ] += ( Double )absc;
2541        numSamples[ u ]++;
2542      }
2543      else 
2544      {
2545        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
2546        numSamples[ LEVEL_RANGE ]++;
2547      }
2548    }
2549  }
2550
2551  return 0;
2552}
2553
2554/** Collect ARL statistics from one LCU
2555 * \param pcCU
2556 */
2557Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU )
2558{
2559  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to datatype and quantization output
2560  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output
2561
2562  TCoeff* pCoeffY = rpcCU->getCoeffY();
2563  Int* pArlCoeffY = rpcCU->getArlCoeffY();
2564
2565  UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth;
2566  UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;
2567
2568  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
2569  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
2570
2571  // Collect stats to cSum[][] and numSamples[][]
2572  for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ )
2573  {
2574    UInt uiTrIdx = rpcCU->getTransformIdx(i);
2575
2576    if(rpcCU->getPredictionMode(i) == MODE_INTER)
2577    if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) )
2578    {
2579      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
2580    }//Note that only InterY is processed. QP rounding is based on InterY data only.
2581   
2582    pCoeffY  += uiMinNumCoeffInCU;
2583    pArlCoeffY  += uiMinNumCoeffInCU;
2584  }
2585
2586  for(Int u=1; u<LEVEL_RANGE;u++)
2587  {
2588    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
2589    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
2590  }
2591  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
2592  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
2593}
2594#endif
2595//! \}
Note: See TracBrowser for help on using the repository browser.