source: 3DVCSoftware/branches/HTM-12.2-dev1-Mediatek/source/Lib/TLibEncoder/TEncCu.cpp

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

Integration of JCT3V-J0060. The MACRO is "MTK_SINGLE_DEPTH_VPS_FLAG_J0060".

By YiWen Chen (yiwen.chen@…)

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