source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncCu.cpp @ 773

Last change on this file since 773 was 773, checked in by tech, 10 years ago

Merged branch/9.2-dev0@722.

  • Property svn:eol-style set to native
File size: 91.9 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 ) && !rpcTempCU->getSlice()->getIsDepth();
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 H_3D_IV_MERGE
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 H_3D_IV_MERGE
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#if H_3D_ARP
1778  DisInfo cOrigDisInfo = rpcTempCU->getDvInfo(0);
1779#else
1780  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1781  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth );
1782#endif
1783
1784#if H_3D_VSP
1785#if !H_3D_ARP
1786  Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1787  memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1788  InheritedVSPDisInfo inheritedVSPDisInfo[MRG_MAX_NUM_CANDS_MEM];
1789  rpcTempCU->m_bAvailableFlagA1 = 0;
1790  rpcTempCU->m_bAvailableFlagB1 = 0;
1791  rpcTempCU->m_bAvailableFlagB0 = 0;
1792  rpcTempCU->m_bAvailableFlagA0 = 0;
1793  rpcTempCU->m_bAvailableFlagB2 = 0;
1794  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1795  rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag,inheritedVSPDisInfo, numValidMergeCand );
1796#endif
1797#else
1798#if H_3D
1799  rpcTempCU->m_bAvailableFlagA1 = 0;
1800  rpcTempCU->m_bAvailableFlagB1 = 0;
1801  rpcTempCU->m_bAvailableFlagB0 = 0;
1802  rpcTempCU->m_bAvailableFlagA0 = 0;
1803  rpcTempCU->m_bAvailableFlagB2 = 0;
1804  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1805  rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1806#else
1807  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1808#endif
1809#endif
1810
1811#if H_3D_IV_MERGE
1812  Int mergeCandBuffer[MRG_MAX_NUM_CANDS_MEM];
1813#else
1814  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1815#endif
1816#if H_3D_ARP
1817for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1818#else
1819for( UInt ui = 0; ui < numValidMergeCand; ++ui )
1820#endif
1821  {
1822    mergeCandBuffer[ui] = 0;
1823  }
1824
1825  Bool bestIsSkip = false;
1826
1827  UInt iteration;
1828  if ( rpcTempCU->isLosslessCoded(0))
1829  {
1830    iteration = 1;
1831  }
1832  else 
1833  {
1834    iteration = 2;
1835  }
1836
1837#if H_3D_ARP
1838  Int nARPWMax = rpcTempCU->getSlice()->getARPStepNum() - 1;
1839  if( nARPWMax < 0 || !rpcTempCU->getDvInfo(0).bDV )
1840  {
1841    nARPWMax = 0;
1842  }
1843  for( Int nARPW=nARPWMax; nARPW >= 0 ; nARPW-- )
1844  {
1845    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS_MEM*sizeof(Int) );
1846    rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1847    rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth );
1848    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1849#if H_3D_IC
1850    rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1851#endif
1852    rpcTempCU->getDvInfo(0) = cOrigDisInfo;
1853    rpcTempCU->setDvInfoSubParts(cOrigDisInfo, 0, 0, uhDepth );
1854    Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1855    memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1856    InheritedVSPDisInfo inheritedVSPDisInfo[MRG_MAX_NUM_CANDS_MEM];
1857#if H_3D_SPIVMP
1858    Bool bSPIVMPFlag[MRG_MAX_NUM_CANDS_MEM];
1859    memset(bSPIVMPFlag, false, sizeof(Bool)*MRG_MAX_NUM_CANDS_MEM);
1860    TComMvField*  pcMvFieldSP;
1861    UChar* puhInterDirSP;
1862    pcMvFieldSP = new TComMvField[rpcTempCU->getPic()->getPicSym()->getNumPartition()*2]; 
1863    puhInterDirSP = new UChar[rpcTempCU->getPic()->getPicSym()->getNumPartition()]; 
1864#endif
1865#if H_3D
1866    rpcTempCU->initAvailableFlags();
1867    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1868    rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag,inheritedVSPDisInfo
1869#if H_3D_SPIVMP
1870      , bSPIVMPFlag, pcMvFieldSP, puhInterDirSP
1871#endif
1872      , numValidMergeCand
1873      );
1874#else
1875    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag, inheritedVSPDisInfo, numValidMergeCand );
1876#endif
1877
1878#endif
1879  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1880  {
1881    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1882    {     
1883#if H_3D_IC
1884        if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
1885        {
1886          if( bICFlag && uiMergeCand == 0 ) 
1887          {
1888            continue;
1889          }
1890        }
1891#endif
1892        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1893        {
1894        if( !(bestIsSkip && uiNoResidual == 0) )
1895        {
1896          // set MC parameters
1897          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1898          rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(),     0, uhDepth );
1899          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1900#if H_3D_IC
1901          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1902#endif
1903#if H_3D_ARP
1904          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1905#endif
1906          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1907          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1908#if H_3D_VSP
1909          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
1910          rpcTempCU->setDvInfoSubParts(inheritedVSPDisInfo[uiMergeCand].m_acDvInfo, 0, 0, uhDepth );
1911#endif
1912#if H_3D_SPIVMP
1913          rpcTempCU->setSPIVMPFlagSubParts(bSPIVMPFlag[uiMergeCand], 0, 0, uhDepth);
1914          if (bSPIVMPFlag[uiMergeCand])
1915          {
1916            UInt uiSPAddr;
1917            Int iWidth = rpcTempCU->getWidth(0);
1918            Int iHeight = rpcTempCU->getHeight(0);
1919
1920            Int iNumSPInOneLine, iNumSP, iSPWidth, iSPHeight;
1921
1922            rpcTempCU->getSPPara(iWidth, iHeight, iNumSP, iNumSPInOneLine, iSPWidth, iSPHeight);
1923
1924            for (Int iPartitionIdx = 0; iPartitionIdx < iNumSP; iPartitionIdx++)
1925            {
1926              rpcTempCU->getSPAbsPartIdx(0, iSPWidth, iSPHeight, iPartitionIdx, iNumSPInOneLine, uiSPAddr);
1927              rpcTempCU->setInterDirSP(puhInterDirSP[iPartitionIdx], uiSPAddr, iSPWidth, iSPHeight);
1928              rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx], iSPWidth, iSPHeight);
1929              rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx + 1], iSPWidth, iSPHeight);
1930            }
1931          }
1932          else
1933          {
1934#endif
1935            rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1936            rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1937            rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1938#if H_3D_SPIVMP
1939          }
1940#endif
1941       // do MC
1942       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1943       // estimate residual and encode everything
1944#if H_3D_VSO //M2
1945       if( m_pcRdCost->getUseRenModel() )
1946       { //Reset
1947         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1948         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1949         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1950         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1951         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1952       }
1953#endif
1954       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1955         m_ppcOrigYuv    [uhDepth],
1956         m_ppcPredYuvTemp[uhDepth],
1957         m_ppcResiYuvTemp[uhDepth],
1958         m_ppcResiYuvBest[uhDepth],
1959         m_ppcRecoYuvTemp[uhDepth],
1960         (uiNoResidual? true:false));
1961
1962
1963          if ( uiNoResidual == 0 && rpcTempCU->getQtRootCbf(0) == 0 )
1964         {
1965            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
1966           mergeCandBuffer[uiMergeCand] = 1;
1967         }
1968
1969          rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
1970#if H_3D_INTER_SDC
1971          TComDataCU *rpcTempCUPre = rpcTempCU;
1972#endif
1973          Int orgQP = rpcTempCU->getQP( 0 );
1974          xCheckDQP( rpcTempCU );
1975          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1976#if H_3D_INTER_SDC
1977          if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() && !uiNoResidual )
1978          {
1979            if( rpcTempCU != rpcTempCUPre )
1980            {
1981              rpcTempCU->initEstData( uhDepth, orgQP );
1982              rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
1983            }
1984            rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
1985            rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
1986            rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
1987#if H_3D_VSO //M2
1988            if( m_pcRdCost->getUseRenModel() )
1989            { //Reset
1990              UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1991              UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1992              Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1993              UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1994              m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1995            }
1996#endif
1997            m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
1998              m_ppcOrigYuv[uhDepth], 
1999              ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth], 
2000              m_ppcResiYuvTemp[uhDepth], 
2001              m_ppcRecoYuvTemp[uhDepth], 
2002              uhDepth );
2003
2004            xCheckDQP( rpcTempCU );
2005            xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
2006          }
2007#endif
2008          rpcTempCU->initEstData( uhDepth, orgQP );
2009
2010      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
2011      {
2012#if H_3D_INTER_SDC
2013        if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) )
2014        {
2015          bestIsSkip = !rpcBestCU->getSDCFlag( 0 ) && ( rpcBestCU->getQtRootCbf(0) == 0 );
2016        }
2017        else
2018        {
2019#endif
2020        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
2021#if H_3D_INTER_SDC
2022        }
2023#endif
2024      }
2025    }
2026   }
2027  }
2028
2029  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
2030  {
2031    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
2032    {
2033      if( rpcBestCU->getMergeFlag( 0 ))
2034      {
2035        *earlyDetectionSkipMode = true;
2036      }
2037      else
2038      {
2039        Int absoulte_MV=0;
2040        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2041        {
2042          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
2043          {
2044            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
2045            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
2046            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
2047            absoulte_MV+=iHor+iVer;
2048          }
2049        }
2050
2051        if(absoulte_MV == 0)
2052        {
2053          *earlyDetectionSkipMode = true;
2054        }
2055      }
2056    }
2057  }
2058 }
2059#if H_3D_SPIVMP
2060 delete[] pcMvFieldSP;
2061 delete[] puhInterDirSP;
2062#endif
2063#if H_3D_ARP
2064 }
2065#endif
2066}
2067
2068
2069#if AMP_MRG
2070#if  H_3D_FAST_TEXTURE_ENCODING
2071Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bFMD, Bool bUseMRG)
2072#else
2073Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
2074#endif
2075#else
2076Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
2077#endif
2078{
2079#if  H_3D_FAST_TEXTURE_ENCODING
2080  if(!(bFMD && (ePartSize == SIZE_2Nx2N)))  //have  motion estimation or merge check
2081  {
2082#endif
2083  UChar uhDepth = rpcTempCU->getDepth( 0 );
2084#if H_3D_ARP
2085  Int iLayerId    = rpcTempCU->getSlice()->getLayerId();
2086  Bool bFirstTime = true;
2087  Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
2088
2089  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || !rpcTempCU->getDvInfo(0).bDV  )
2090  {
2091    nARPWMax = 0;
2092  }
2093
2094  for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
2095  {
2096    if( bFirstTime == false && rpcTempCU->getSlice()->getVPS()->getUseAdvRP( iLayerId ) )
2097    {
2098      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0) );
2099    }
2100#endif
2101#if H_3D_VSO // M3
2102  if( m_pcRdCost->getUseRenModel() )
2103  {
2104    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2105    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2106    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2107    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2108    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2109  }
2110#endif
2111
2112  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2113 
2114  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2115
2116  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
2117  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2118  rpcTempCU->setCUTransquantBypassSubParts  ( m_pcEncCfg->getCUTransquantBypassFlagValue(),      0, uhDepth );
2119 
2120#if H_3D_ARP
2121  rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2122#endif
2123
2124#if H_3D_ARP
2125  if( bFirstTime == false && nARPWMax )
2126  {
2127    rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
2128    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2129
2130    m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
2131  }
2132  else
2133  {
2134    bFirstTime = false;
2135#endif
2136#if AMP_MRG
2137  rpcTempCU->setMergeAMP (true);
2138#if  H_3D_FAST_TEXTURE_ENCODING
2139  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bFMD, false, bUseMRG );
2140#else
2141  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
2142#endif
2143#else 
2144  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
2145#endif
2146#if H_3D_ARP
2147   if( nARPWMax )
2148   {
2149     m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
2150   }
2151  }
2152#endif
2153
2154#if AMP_MRG
2155  if ( !rpcTempCU->getMergeAMP() )
2156  {
2157#if H_3D_ARP
2158    if( nARPWMax )
2159    {
2160      continue;
2161    }
2162    else
2163#endif
2164    return;
2165  }
2166#endif
2167
2168#if RATE_CONTROL_LAMBDA_DOMAIN  && (!M0036_RC_IMPROVEMENT || KWU_RC_MADPRED_E0227)
2169  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2170  {
2171    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2172      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2173      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2174    m_temporalSAD = (Int)SAD;
2175  }
2176#endif
2177#if !RATE_CONTROL_LAMBDA_DOMAIN  && KWU_RC_MADPRED_E0227
2178  if ( m_pcEncCfg->getUseRateCtrl() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2179  {
2180    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2181      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2182      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2183    m_temporalSAD = (Int)SAD;
2184  }
2185#endif
2186
2187  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
2188
2189
2190#if H_3D_VSO // M4
2191  if( m_pcRdCost->getUseLambdaScaleVSO() )
2192    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2193  else
2194#endif
2195  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2196#if H_3D_INTER_SDC
2197  TComDataCU *rpcTempCUPre = rpcTempCU;
2198#endif
2199  xCheckDQP( rpcTempCU );
2200  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2201#if H_3D_INTER_SDC
2202  if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() )
2203  {
2204    if( rpcTempCU != rpcTempCUPre )
2205    {
2206      Int orgQP = rpcBestCU->getQP( 0 );
2207      rpcTempCU->initEstData( uhDepth, orgQP );
2208      rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
2209    }
2210    rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2211    rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
2212    rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
2213#if H_3D_VSO // M3
2214    if( m_pcRdCost->getUseRenModel() )
2215    {
2216      UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2217      UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2218      Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2219      UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2220      m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2221    }
2222#endif
2223
2224    m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2225      m_ppcOrigYuv[uhDepth],
2226      ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth],
2227      m_ppcResiYuvTemp[uhDepth],
2228      m_ppcRecoYuvTemp[uhDepth],
2229      uhDepth );
2230
2231  xCheckDQP( rpcTempCU );
2232  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2233  }
2234#endif
2235#if H_3D_ARP
2236  }
2237#endif
2238#if  H_3D_FAST_TEXTURE_ENCODING
2239  }
2240#endif
2241}
2242
2243Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2244{
2245  UInt uiDepth = rpcTempCU->getDepth( 0 );
2246 
2247#if H_3D_VSO // M5
2248  if( m_pcRdCost->getUseRenModel() )
2249  {
2250    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
2251    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
2252    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
2253    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
2254    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2255  }
2256#endif
2257
2258  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2259
2260  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
2261  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2262  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
2263 
2264  Bool bSeparateLumaChroma = true; // choose estimation mode
2265  UInt uiPreCalcDistC      = 0;
2266  if( !bSeparateLumaChroma )
2267  {
2268    m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] );
2269  }
2270  m_pcPredSearch  ->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma );
2271
2272  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
2273 
2274#if H_3D_DIM_SDC
2275  if( !rpcTempCU->getSDCFlag( 0 ) )
2276#endif
2277  m_pcPredSearch  ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC );
2278 
2279  m_pcEntropyCoder->resetBits();
2280  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2281  {
2282    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2283  }
2284  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2285  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
2286  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
2287  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0,          true );
2288  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
2289
2290  // Encode Coefficients
2291  Bool bCodeDQP = getdQPFlag();
2292  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP );
2293  setdQPFlag( bCodeDQP );
2294 
2295  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2296 
2297  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2298  if(m_pcEncCfg->getUseSBACRD())
2299  {
2300    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2301  }
2302
2303#if H_3D_VSO // M6
2304  if( m_pcRdCost->getUseLambdaScaleVSO()) 
2305    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
2306  else
2307#endif
2308  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2309 
2310#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT && KWU_RC_MADPRED_E0227
2311  UChar uhDepth = rpcTempCU->getDepth( 0 );
2312  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && eSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2313  {
2314    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2315      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2316      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2317    m_spatialSAD = (Int)SAD;
2318  }
2319#endif
2320#if RATE_CONTROL_LAMBDA_DOMAIN && M0036_RC_IMPROVEMENT && KWU_RC_MADPRED_E0227
2321  UChar uhDepth = rpcTempCU->getDepth( 0 );
2322  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && eSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2323  {
2324    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2325      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2326      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2327    m_spatialSAD = (Int)SAD;
2328  }
2329#endif
2330#if !RATE_CONTROL_LAMBDA_DOMAIN && KWU_RC_MADPRED_E0227
2331  UChar uhDepth = rpcTempCU->getDepth( 0 );
2332  if ( m_pcEncCfg->getUseRateCtrl() && eSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2333  {
2334    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2335      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2336      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2337    m_spatialSAD = (Int)SAD;
2338  }
2339#endif
2340  xCheckDQP( rpcTempCU );
2341  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2342}
2343
2344/** Check R-D costs for a CU with PCM mode.
2345 * \param rpcBestCU pointer to best mode CU data structure
2346 * \param rpcTempCU pointer to testing mode CU data structure
2347 * \returns Void
2348 *
2349 * \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.
2350 */
2351Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
2352{
2353  UInt uiDepth = rpcTempCU->getDepth( 0 );
2354
2355  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2356
2357  rpcTempCU->setIPCMFlag(0, true);
2358  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
2359  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2360  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2361  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
2362  rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth );
2363
2364  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
2365
2366  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
2367
2368  m_pcEntropyCoder->resetBits();
2369  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2370  {
2371    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2372  }
2373  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2374  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
2375  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
2376  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
2377
2378  if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2379
2380  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2381  if(m_pcEncCfg->getUseSBACRD())
2382  {
2383    rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2384  }
2385#if H_3D_VSO // M44
2386  if ( m_pcRdCost->getUseVSO() )
2387    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2388  else
2389#endif
2390  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2391
2392  xCheckDQP( rpcTempCU );
2393  xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth );
2394}
2395
2396/** check whether current try is the best with identifying the depth of current try
2397 * \param rpcBestCU
2398 * \param rpcTempCU
2399 * \returns Void
2400 */
2401Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
2402{
2403  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
2404  {
2405    TComYuv* pcYuv;
2406    // Change Information data
2407    TComDataCU* pcCU = rpcBestCU;
2408    rpcBestCU = rpcTempCU;
2409    rpcTempCU = pcCU;
2410
2411    // Change Prediction data
2412    pcYuv = m_ppcPredYuvBest[uiDepth];
2413    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
2414    m_ppcPredYuvTemp[uiDepth] = pcYuv;
2415
2416    // Change Reconstruction data
2417    pcYuv = m_ppcRecoYuvBest[uiDepth];
2418    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
2419    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
2420
2421    pcYuv = NULL;
2422    pcCU  = NULL;
2423
2424    if( m_bUseSBACRD )  // store temp best CI for next CU coding
2425      m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
2426  }
2427}
2428
2429Void TEncCu::xCheckDQP( TComDataCU* pcCU )
2430{
2431  UInt uiDepth = pcCU->getDepth( 0 );
2432
2433  if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() )
2434  {
2435    if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) )
2436    {
2437#if !RDO_WITHOUT_DQP_BITS
2438      m_pcEntropyCoder->resetBits();
2439      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
2440      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
2441      if(m_pcEncCfg->getUseSBACRD())
2442      {
2443        pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2444      }
2445#if H_3D_VSO // M45
2446      if ( m_pcRdCost->getUseVSO() )     
2447        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
2448      else
2449#endif
2450      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
2451#endif
2452    }
2453    else
2454    {
2455      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
2456    }
2457  }
2458}
2459
2460Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
2461{
2462  pDst->iN = pSrc->iN;
2463  for (Int i = 0; i < pSrc->iN; i++)
2464  {
2465    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
2466  }
2467}
2468Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY )
2469{
2470  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
2471  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
2472  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
2473  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2474    pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2475  Bool bSliceEnd   = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
2476    pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) );
2477  if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2478  {
2479    UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
2480    UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth);
2481    UInt uiBlkWidth    = rpcPic->getNumPartInWidth() >> (uiDepth);
2482    UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2483    UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2484    UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
2485    m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
2486  }
2487  else
2488  {
2489    UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2;
2490
2491    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
2492    {
2493      UInt uiSubCULPelX   = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx &  1 );
2494      UInt uiSubCUTPelY   = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 );
2495
2496      Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && 
2497        rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr();
2498      if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
2499      {
2500        xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY );   // Copy Yuv data to picture Yuv
2501      }
2502    }
2503  }
2504}
2505
2506Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
2507{
2508  UInt uiCurrDepth = uiNextDepth - 1;
2509  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
2510}
2511
2512/** Function for filling the PCM buffer of a CU using its original sample array
2513 * \param pcCU pointer to current CU
2514 * \param pcOrgYuv pointer to original sample array
2515 * \returns Void
2516 */
2517Void TEncCu::xFillPCMBuffer     ( TComDataCU*& pCU, TComYuv* pOrgYuv )
2518{
2519
2520  UInt   width        = pCU->getWidth(0);
2521  UInt   height       = pCU->getHeight(0);
2522
2523  Pel*   pSrcY = pOrgYuv->getLumaAddr(0, width); 
2524  Pel*   pDstY = pCU->getPCMSampleY();
2525  UInt   srcStride = pOrgYuv->getStride();
2526
2527  for(Int y = 0; y < height; y++ )
2528  {
2529    for(Int x = 0; x < width; x++ )
2530    {
2531      pDstY[x] = pSrcY[x];
2532    }
2533    pDstY += width;
2534    pSrcY += srcStride;
2535  }
2536
2537  Pel* pSrcCb       = pOrgYuv->getCbAddr();
2538  Pel* pSrcCr       = pOrgYuv->getCrAddr();;
2539
2540  Pel* pDstCb       = pCU->getPCMSampleCb();
2541  Pel* pDstCr       = pCU->getPCMSampleCr();;
2542
2543  UInt srcStrideC = pOrgYuv->getCStride();
2544  UInt heightC   = height >> 1;
2545  UInt widthC    = width  >> 1;
2546
2547  for(Int y = 0; y < heightC; y++ )
2548  {
2549    for(Int x = 0; x < widthC; x++ )
2550    {
2551      pDstCb[x] = pSrcCb[x];
2552      pDstCr[x] = pSrcCr[x];
2553    }
2554    pDstCb += widthC;
2555    pDstCr += widthC;
2556    pSrcCb += srcStrideC;
2557    pSrcCr += srcStrideC;
2558  }
2559}
2560
2561#if ADAPTIVE_QP_SELECTION
2562/** Collect ARL statistics from one block
2563  */
2564Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
2565{
2566  for( Int n = 0; n < NumCoeffInCU; n++ )
2567  {
2568    Int u = abs( rpcCoeff[ n ] );
2569    Int absc = rpcArlCoeff[ n ];
2570
2571    if( u != 0 )
2572    {
2573      if( u < LEVEL_RANGE )
2574      {
2575        cSum[ u ] += ( Double )absc;
2576        numSamples[ u ]++;
2577      }
2578      else 
2579      {
2580        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
2581        numSamples[ LEVEL_RANGE ]++;
2582      }
2583    }
2584  }
2585
2586  return 0;
2587}
2588
2589/** Collect ARL statistics from one LCU
2590 * \param pcCU
2591 */
2592Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU )
2593{
2594  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to datatype and quantization output
2595  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output
2596
2597  TCoeff* pCoeffY = rpcCU->getCoeffY();
2598  Int* pArlCoeffY = rpcCU->getArlCoeffY();
2599
2600  UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth;
2601  UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;
2602
2603  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
2604  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
2605
2606  // Collect stats to cSum[][] and numSamples[][]
2607  for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ )
2608  {
2609    UInt uiTrIdx = rpcCU->getTransformIdx(i);
2610
2611    if(rpcCU->getPredictionMode(i) == MODE_INTER)
2612    if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) )
2613    {
2614      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
2615    }//Note that only InterY is processed. QP rounding is based on InterY data only.
2616   
2617    pCoeffY  += uiMinNumCoeffInCU;
2618    pArlCoeffY  += uiMinNumCoeffInCU;
2619  }
2620
2621  for(Int u=1; u<LEVEL_RANGE;u++)
2622  {
2623    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
2624    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
2625  }
2626  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
2627  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
2628}
2629#endif
2630//! \}
Note: See TracBrowser for help on using the repository browser.