source: 3DVCSoftware/branches/HTM-8.2-dev3-Samsung/source/Lib/TLibEncoder/TEncCu.cpp @ 930

Last change on this file since 930 was 699, checked in by samsung-htm, 11 years ago

Integration of F0147 and F0151
F0147: DMM simplification and signalling
F0151: Removal of IC in depth coding

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