source: 3DVCSoftware/branches/HTM-8.2-dev0-Cleanup/source/Lib/TLibEncoder/TEncCu.cpp @ 647

Last change on this file since 647 was 647, checked in by tech, 12 years ago

Macro removal part 2.

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