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

Last change on this file since 805 was 790, checked in by mediatek-htm, 11 years ago

JCT3V-G0055 Integrated

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