source: 3DVCSoftware/branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncCu.cpp @ 839

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

Further fixes.

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