source: 3DVCSoftware/branches/HTM-10.1-dev0/source/Lib/TLibEncoder/TEncCu.cpp @ 876

Last change on this file since 876 was 876, checked in by tech, 10 years ago

Cleanups part 1.

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