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

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

Added missing parts of merge.

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