source: 3DVCSoftware/branches/HTM-11.2-dev0/source/Lib/TLibEncoder/TEncCu.cpp @ 1029

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

Merged 11.2-dev1-MediaTek@1004.

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