source: 3DVCSoftware/branches/HTM-8.2-dev2-ETRI/source/Lib/TLibEncoder/TEncCu.cpp

Last change on this file was 708, checked in by etri-htm, 11 years ago

Integration of "Results on simple merge candidate list construction for 3DV"
JCT3V-F0093
MACRO "ETRIKHU_MERGE_REUSE_F0093"

By Gun Bang (gbang@…)

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