source: 3DVCSoftware/branches/HTM-9.3-dev1-MediaTek/source/Lib/TLibEncoder/TEncCu.cpp @ 795

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

Integration of DDD (JCT3V-G0063)

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