source: 3DVCSoftware/branches/HTM-11.2-dev3-MediaTek/source/Lib/TLibEncoder/TEncCu.cpp @ 983

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

Integration of Single Depth Mode proposed in JCT3V-I0095.
The MACRO is "MTK_SINGLE_DEPTH_MODE_I0095".

By Yi-Wen Chen (yiwen.chen@…)

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