source: 3DVCSoftware/branches/HTM-9.3-dev2-Samsung/source/Lib/TLibEncoder/TEncCu.cpp @ 785

Last change on this file since 785 was 785, checked in by samsung-htm, 10 years ago

Integration of JCT3V-G0072: IC and ARP Flags Signaling

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