source: 3DVCSoftware/branches/HTM-12.1-dev0/source/Lib/TLibEncoder/TEncCu.cpp @ 1074

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

Removed 3D-HEVC related macros.

  • Property svn:eol-style set to native
File size: 106.2 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6* Copyright (c) 2010-2014, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TEncCu.cpp
35    \brief    Coding Unit (CU) encoder class
36*/
37
38#include <stdio.h>
39#include "TEncTop.h"
40#include "TEncCu.h"
41#include "TEncAnalyze.h"
42
43#include <cmath>
44#include <algorithm>
45using namespace std;
46
47//! \ingroup TLibEncoder
48//! \{
49
50// ====================================================================================================================
51// Constructor / destructor / create / destroy
52// ====================================================================================================================
53
54/**
55 \param    uiTotalDepth  total number of allowable depth
56 \param    uiMaxWidth    largest CU width
57 \param    uiMaxHeight   largest CU height
58 */
59Void TEncCu::create(UChar uhTotalDepth, UInt uiMaxWidth, UInt uiMaxHeight)
60{
61  Int i;
62 
63  m_uhTotalDepth   = uhTotalDepth + 1;
64  m_ppcBestCU      = new TComDataCU*[m_uhTotalDepth-1];
65  m_ppcTempCU      = new TComDataCU*[m_uhTotalDepth-1];
66   
67#if H_3D_ARP
68  m_ppcWeightedTempCU = new TComDataCU*[m_uhTotalDepth-1];
69#endif
70
71  m_ppcPredYuvBest = new TComYuv*[m_uhTotalDepth-1];
72  m_ppcResiYuvBest = new TComYuv*[m_uhTotalDepth-1];
73  m_ppcRecoYuvBest = new TComYuv*[m_uhTotalDepth-1];
74  m_ppcPredYuvTemp = new TComYuv*[m_uhTotalDepth-1];
75  m_ppcResiYuvTemp = new TComYuv*[m_uhTotalDepth-1];
76  m_ppcRecoYuvTemp = new TComYuv*[m_uhTotalDepth-1];
77  m_ppcOrigYuv     = new TComYuv*[m_uhTotalDepth-1];
78#if H_3D_DBBP
79  m_ppcOrigYuvDBBP = new TComYuv*[m_uhTotalDepth-1];
80#endif
81 
82  UInt uiNumPartitions;
83  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
84  {
85    uiNumPartitions = 1<<( ( m_uhTotalDepth - i - 1 )<<1 );
86    UInt uiWidth  = uiMaxWidth  >> i;
87    UInt uiHeight = uiMaxHeight >> i;
88   
89    m_ppcBestCU[i] = new TComDataCU; m_ppcBestCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
90    m_ppcTempCU[i] = new TComDataCU; m_ppcTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
91   
92#if H_3D_ARP
93    m_ppcWeightedTempCU[i] = new TComDataCU; m_ppcWeightedTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
94#endif 
95
96    m_ppcPredYuvBest[i] = new TComYuv; m_ppcPredYuvBest[i]->create(uiWidth, uiHeight);
97    m_ppcResiYuvBest[i] = new TComYuv; m_ppcResiYuvBest[i]->create(uiWidth, uiHeight);
98    m_ppcRecoYuvBest[i] = new TComYuv; m_ppcRecoYuvBest[i]->create(uiWidth, uiHeight);
99   
100    m_ppcPredYuvTemp[i] = new TComYuv; m_ppcPredYuvTemp[i]->create(uiWidth, uiHeight);
101    m_ppcResiYuvTemp[i] = new TComYuv; m_ppcResiYuvTemp[i]->create(uiWidth, uiHeight);
102    m_ppcRecoYuvTemp[i] = new TComYuv; m_ppcRecoYuvTemp[i]->create(uiWidth, uiHeight);
103   
104    m_ppcOrigYuv    [i] = new TComYuv; m_ppcOrigYuv    [i]->create(uiWidth, uiHeight);
105#if H_3D_DBBP
106    m_ppcOrigYuvDBBP[i] = new TComYuv; m_ppcOrigYuvDBBP[i]->create(uiWidth, uiHeight);
107#endif
108  }
109 
110  m_bEncodeDQP = false;
111#if KWU_RC_MADPRED_E0227
112  m_LCUPredictionSAD = 0;
113  m_addSADDepth      = 0;
114  m_temporalSAD      = 0;
115  m_spatialSAD       = 0;
116#endif
117
118  // initialize partition order.
119  UInt* piTmp = &g_auiZscanToRaster[0];
120  initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp);
121  initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
122 
123  // initialize conversion matrix from partition index to pel
124  initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
125}
126
127Void TEncCu::destroy()
128{
129  Int i;
130 
131  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
132  {
133    if(m_ppcBestCU[i])
134    {
135      m_ppcBestCU[i]->destroy();      delete m_ppcBestCU[i];      m_ppcBestCU[i] = NULL;
136    }
137    if(m_ppcTempCU[i])
138    {
139      m_ppcTempCU[i]->destroy();      delete m_ppcTempCU[i];      m_ppcTempCU[i] = NULL;
140    }
141#if H_3D_ARP
142    if(m_ppcWeightedTempCU[i])
143    {
144      m_ppcWeightedTempCU[i]->destroy(); delete m_ppcWeightedTempCU[i]; m_ppcWeightedTempCU[i] = NULL;
145    }
146#endif
147    if(m_ppcPredYuvBest[i])
148    {
149      m_ppcPredYuvBest[i]->destroy(); delete m_ppcPredYuvBest[i]; m_ppcPredYuvBest[i] = NULL;
150    }
151    if(m_ppcResiYuvBest[i])
152    {
153      m_ppcResiYuvBest[i]->destroy(); delete m_ppcResiYuvBest[i]; m_ppcResiYuvBest[i] = NULL;
154    }
155    if(m_ppcRecoYuvBest[i])
156    {
157      m_ppcRecoYuvBest[i]->destroy(); delete m_ppcRecoYuvBest[i]; m_ppcRecoYuvBest[i] = NULL;
158    }
159    if(m_ppcPredYuvTemp[i])
160    {
161      m_ppcPredYuvTemp[i]->destroy(); delete m_ppcPredYuvTemp[i]; m_ppcPredYuvTemp[i] = NULL;
162    }
163    if(m_ppcResiYuvTemp[i])
164    {
165      m_ppcResiYuvTemp[i]->destroy(); delete m_ppcResiYuvTemp[i]; m_ppcResiYuvTemp[i] = NULL;
166    }
167    if(m_ppcRecoYuvTemp[i])
168    {
169      m_ppcRecoYuvTemp[i]->destroy(); delete m_ppcRecoYuvTemp[i]; m_ppcRecoYuvTemp[i] = NULL;
170    }
171    if(m_ppcOrigYuv[i])
172    {
173      m_ppcOrigYuv[i]->destroy();     delete m_ppcOrigYuv[i];     m_ppcOrigYuv[i] = NULL;
174    }
175#if H_3D_DBBP
176    if(m_ppcOrigYuvDBBP[i])
177    {
178      m_ppcOrigYuvDBBP[i]->destroy(); delete m_ppcOrigYuvDBBP[i]; m_ppcOrigYuvDBBP[i] = NULL;
179    }
180#endif
181  }
182  if(m_ppcBestCU)
183  {
184    delete [] m_ppcBestCU;
185    m_ppcBestCU = NULL;
186  }
187  if(m_ppcTempCU)
188  {
189    delete [] m_ppcTempCU;
190    m_ppcTempCU = NULL;
191  }
192
193#if H_3D_ARP
194  if(m_ppcWeightedTempCU)
195  {
196    delete [] m_ppcWeightedTempCU; 
197    m_ppcWeightedTempCU = NULL; 
198  }
199#endif
200  if(m_ppcPredYuvBest)
201  {
202    delete [] m_ppcPredYuvBest;
203    m_ppcPredYuvBest = NULL;
204  }
205  if(m_ppcResiYuvBest)
206  {
207    delete [] m_ppcResiYuvBest;
208    m_ppcResiYuvBest = NULL;
209  }
210  if(m_ppcRecoYuvBest)
211  {
212    delete [] m_ppcRecoYuvBest;
213    m_ppcRecoYuvBest = NULL;
214  }
215  if(m_ppcPredYuvTemp)
216  {
217    delete [] m_ppcPredYuvTemp;
218    m_ppcPredYuvTemp = NULL;
219  }
220  if(m_ppcResiYuvTemp)
221  {
222    delete [] m_ppcResiYuvTemp;
223    m_ppcResiYuvTemp = NULL;
224  }
225  if(m_ppcRecoYuvTemp)
226  {
227    delete [] m_ppcRecoYuvTemp;
228    m_ppcRecoYuvTemp = NULL;
229  }
230  if(m_ppcOrigYuv)
231  {
232    delete [] m_ppcOrigYuv;
233    m_ppcOrigYuv = NULL;
234  }
235#if H_3D_DBBP
236  if(m_ppcOrigYuvDBBP)
237  {
238    delete [] m_ppcOrigYuvDBBP;
239    m_ppcOrigYuvDBBP = NULL;
240  }
241#endif
242}
243
244/** \param    pcEncTop      pointer of encoder class
245 */
246Void TEncCu::init( TEncTop* pcEncTop )
247{
248  m_pcEncCfg           = pcEncTop;
249  m_pcPredSearch       = pcEncTop->getPredSearch();
250  m_pcTrQuant          = pcEncTop->getTrQuant();
251  m_pcBitCounter       = pcEncTop->getBitCounter();
252  m_pcRdCost           = pcEncTop->getRdCost();
253 
254  m_pcEntropyCoder     = pcEncTop->getEntropyCoder();
255  m_pcCavlcCoder       = pcEncTop->getCavlcCoder();
256  m_pcSbacCoder       = pcEncTop->getSbacCoder();
257  m_pcBinCABAC         = pcEncTop->getBinCABAC();
258 
259  m_pppcRDSbacCoder   = pcEncTop->getRDSbacCoder();
260  m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder();
261 
262  m_pcRateCtrl        = pcEncTop->getRateCtrl();
263}
264
265// ====================================================================================================================
266// Public member functions
267// ====================================================================================================================
268
269/** \param  rpcCU pointer of CU data class
270 */
271Void TEncCu::compressCU( TComDataCU*& rpcCU )
272{
273  // initialize CU data
274  m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
275  m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() );
276
277#if KWU_RC_MADPRED_E0227
278  m_LCUPredictionSAD = 0;
279  m_addSADDepth      = 0;
280  m_temporalSAD      = 0;
281  m_spatialSAD       = 0;
282#endif
283
284  // analysis of CU
285  xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 );
286
287#if ADAPTIVE_QP_SELECTION
288  if( m_pcEncCfg->getUseAdaptQpSelect() )
289  {
290    if(rpcCU->getSlice()->getSliceType()!=I_SLICE) //IIII
291    {
292      xLcuCollectARLStats( rpcCU);
293    }
294  }
295#endif
296}
297/** \param  pcCU  pointer of CU data class
298 */
299Void TEncCu::encodeCU ( TComDataCU* pcCU )
300{
301  if ( pcCU->getSlice()->getPPS()->getUseDQP() )
302  {
303    setdQPFlag(true);
304  }
305
306  // Encode CU data
307  xEncodeCU( pcCU, 0, 0 );
308}
309
310// ====================================================================================================================
311// Protected member functions
312// ====================================================================================================================
313/** Derive small set of test modes for AMP encoder speed-up
314 *\param   rpcBestCU
315 *\param   eParentPartSize
316 *\param   bTestAMP_Hor
317 *\param   bTestAMP_Ver
318 *\param   bTestMergeAMP_Hor
319 *\param   bTestMergeAMP_Ver
320 *\returns Void
321*/
322#if AMP_ENC_SPEEDUP
323#if AMP_MRG
324Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver, Bool &bTestMergeAMP_Hor, Bool &bTestMergeAMP_Ver)
325#else
326Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver)
327#endif
328{
329  if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
330  {
331    bTestAMP_Hor = true;
332  }
333  else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
334  {
335    bTestAMP_Ver = true;
336  }
337  else if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->getMergeFlag(0) == false && rpcBestCU->isSkipped(0) == false )
338  {
339    bTestAMP_Hor = true;         
340    bTestAMP_Ver = true;         
341  }
342
343#if AMP_MRG
344  //! Utilizing the partition size of parent PU   
345  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
346  { 
347    bTestMergeAMP_Hor = true;
348    bTestMergeAMP_Ver = true;
349  }
350
351  if ( eParentPartSize == SIZE_NONE ) //! if parent is intra
352  {
353    if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN )
354    {
355      bTestMergeAMP_Hor = true;
356    }
357    else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
358    {
359      bTestMergeAMP_Ver = true;
360    }
361  }
362
363  if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->isSkipped(0) == false )
364  {
365    bTestMergeAMP_Hor = true;         
366    bTestMergeAMP_Ver = true;         
367  }
368
369  if ( rpcBestCU->getWidth(0) == 64 )
370  { 
371    bTestAMP_Hor = false;
372    bTestAMP_Ver = false;
373  }   
374#else
375  //! Utilizing the partition size of parent PU       
376  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
377  { 
378    bTestAMP_Hor = true;
379    bTestAMP_Ver = true;
380  }
381
382  if ( eParentPartSize == SIZE_2Nx2N )
383  { 
384    bTestAMP_Hor = false;
385    bTestAMP_Ver = false;
386  }     
387#endif
388}
389#endif
390
391// ====================================================================================================================
392// Protected member functions
393// ====================================================================================================================
394/** Compress a CU block recursively with enabling sub-LCU-level delta QP
395 *\param   rpcBestCU
396 *\param   rpcTempCU
397 *\param   uiDepth
398 *\returns Void
399 *
400 *- for loop of QP value to compress the current CU with all possible QP
401*/
402#if AMP_ENC_SPEEDUP
403Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth, PartSize eParentPartSize )
404#else
405Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
406#endif
407{
408  TComPic* pcPic = rpcBestCU->getPic();
409
410#if H_3D_QTLPC
411  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(rpcBestCU->getSlice()->getApplySingleDepthMode())
718    {
719      xCheckRDCostSingleDepth( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
720      rpcTempCU->initEstData( uiDepth, iMinQP, isAddLowestQP  );
721    }
722#endif
723    if(!earlyDetectionSkipMode)
724    {
725      for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
726      {
727        const Bool bIsLosslessMode = isAddLowestQP && (iQP == iMinQP);
728
729        if (bIsLosslessMode)
730        {
731          iQP = lowestQP;
732        }
733        rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
734
735        // do inter modes, NxN, 2NxN, and Nx2N
736        if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
737        {
738          // 2Nx2N, NxN
739            if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) ))
740            {
741              if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu
742#if H_3D_QTLPC
743                && bTrySplit
744#endif
745                )
746              {
747#if  H_3D_FAST_TEXTURE_ENCODING
748                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN, bFMD  );
749#else
750                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN   );
751#endif
752                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
753#if H_3D_VSP
754                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
755#endif
756              }
757            }
758
759          // 2NxN, Nx2N
760          if(doNotBlockPu
761#if H_3D_QTLPC
762            && bTryNx2N
763#endif
764            )
765          {
766#if  H_3D_FAST_TEXTURE_ENCODING
767            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N, bFMD  );
768#else
769            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N  );
770#endif
771            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
772#if H_3D_VSP
773            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
774#endif
775            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
776            {
777              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
778            }
779          }
780          if(doNotBlockPu
781#if H_3D_QTLPC
782            && bTry2NxN
783#endif
784            )
785          {
786#if  H_3D_FAST_TEXTURE_ENCODING
787            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN, bFMD  );
788#else
789            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN  );
790#endif
791            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
792#if H_3D_VSP
793            rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
794#endif
795            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN)
796            {
797              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
798            }
799          }
800
801#if 1
802          //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N)
803          if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) )
804          {
805#if AMP_ENC_SPEEDUP       
806            Bool bTestAMP_Hor = false, bTestAMP_Ver = false;
807
808#if AMP_MRG
809            Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false;
810
811            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver);
812#else
813            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver);
814#endif
815
816            //! Do horizontal AMP
817            if ( bTestAMP_Hor )
818            {
819              if(doNotBlockPu
820#if H_3D_QTLPC
821                && bTry2NxN
822#endif
823                )
824              {
825#if  H_3D_FAST_TEXTURE_ENCODING
826                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFMD );
827#else
828                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
829#endif
830                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
831#if H_3D_VSP
832                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
833#endif
834                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
835                {
836                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
837                }
838              }
839              if(doNotBlockPu
840#if H_3D_QTLPC
841                && bTry2NxN
842#endif
843                )
844              {
845#if  H_3D_FAST_TEXTURE_ENCODING
846                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFMD );
847#else
848                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
849#endif
850                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
851#if H_3D_VSP
852                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
853#endif
854                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
855                {
856                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
857                }
858              }
859            }
860#if AMP_MRG
861            else if ( bTestMergeAMP_Hor ) 
862            {
863              if(doNotBlockPu
864#if H_3D_QTLPC
865                && bTry2NxN
866#endif
867                )
868              {
869#if  H_3D_FAST_TEXTURE_ENCODING
870                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, bFMD, true );
871#else
872                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true );
873#endif
874                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
875#if H_3D_VSP
876                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
877#endif
878                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
879                {
880                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
881                }
882              }
883              if(doNotBlockPu
884#if H_3D_QTLPC
885                && bTry2NxN
886#endif
887                )
888              {
889#if  H_3D_FAST_TEXTURE_ENCODING
890                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, bFMD, true );
891#else
892                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true );
893#endif
894                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
895#if H_3D_VSP
896                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
897#endif
898                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
899                {
900                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
901                }
902              }
903            }
904#endif
905
906            //! Do horizontal AMP
907            if ( bTestAMP_Ver )
908            {
909              if(doNotBlockPu
910#if H_3D_QTLPC
911                && bTryNx2N
912#endif
913                )
914              {
915#if  H_3D_FAST_TEXTURE_ENCODING
916                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFMD );
917#else
918                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
919#endif
920                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
921#if H_3D_VSP
922                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
923#endif
924                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
925                {
926                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
927                }
928              }
929              if(doNotBlockPu
930#if H_3D_QTLPC
931                && bTryNx2N
932#endif
933                )
934              {
935#if  H_3D_FAST_TEXTURE_ENCODING
936                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFMD );
937#else
938                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
939#endif
940                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
941#if H_3D_VSP
942                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
943#endif
944              }
945            }
946#if AMP_MRG
947            else if ( bTestMergeAMP_Ver )
948            {
949              if(doNotBlockPu
950#if H_3D_QTLPC
951                && bTryNx2N
952#endif
953                )
954              {
955#if  H_3D_FAST_TEXTURE_ENCODING
956                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, bFMD, true );
957#else
958                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true );
959#endif
960                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
961#if H_3D_VSP
962                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
963#endif
964                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
965                {
966                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
967                }
968              }
969              if(doNotBlockPu
970#if H_3D_QTLPC
971                && bTryNx2N
972#endif
973                )
974              {
975#if  H_3D_FAST_TEXTURE_ENCODING
976                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, bFMD, true );
977#else
978                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true );
979#endif
980                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
981#if H_3D_VSP
982                rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
983#endif
984              }
985            }
986#endif
987
988#else
989#if H_3D_QTLPC
990            if (bTry2NxN)
991            {
992#endif
993              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
994              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
995#if H_3D_VSP
996              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
997#endif
998              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
999              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1000#if H_3D_VSP
1001              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
1002#endif
1003#if H_3D_QTLPC
1004            }
1005            if (bTryNx2N)
1006            {
1007#endif
1008              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
1009              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1010#if H_3D_VSP
1011              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
1012#endif
1013              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
1014              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1015#if H_3D_VSP
1016              rpcTempCU->setDvInfoSubParts(DvInfo, 0, uiDepth);
1017#endif
1018#if H_3D_QTLPC
1019            }
1020#endif
1021
1022#endif
1023          }   
1024#endif
1025        }
1026#if  H_3D_FAST_TEXTURE_ENCODING
1027        if(!bFMD)
1028        {
1029#endif
1030        // do normal intra modes
1031       
1032          // speedup for inter frames
1033          if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || 
1034              rpcBestCU->getCbf( 0, TEXT_LUMA     ) != 0   ||
1035              rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0   ||
1036              rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0     
1037#if H_3D_DIM_ENC
1038              || rpcBestCU->getSlice()->getIsDepth()
1039#endif
1040            ) // avoid very complex intra if it is unlikely
1041          {
1042#if H_3D_DIM
1043            Bool bOnlyIVP = false;
1044            if( rpcBestCU->getSlice()->getIsDepth() && !(rpcBestCU->getSlice()->isIRAP()) && 
1045                rpcBestCU->getSlice()->getSliceType() != I_SLICE && 
1046                rpcBestCU->getCbf( 0, TEXT_LUMA     ) == 0 &&
1047                rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) == 0 &&
1048                rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) == 0 
1049              )
1050            { 
1051              bOnlyIVP = true;
1052            }
1053            xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bOnlyIVP );
1054#else
1055            xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N );
1056#endif
1057
1058#if KWU_RC_MADPRED_E0227
1059            if ( uiDepth <= m_addSADDepth )
1060            {
1061              m_LCUPredictionSAD += m_spatialSAD;
1062              m_addSADDepth = uiDepth;
1063            }
1064#endif
1065
1066            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1067            if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
1068            {
1069#if H_3D_QTLPC //Try IntraNxN
1070              if(bTrySplit)
1071              {
1072#endif
1073                if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) )
1074                {
1075#if H_3D_DIM
1076                  xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN, bOnlyIVP );
1077#else
1078                  xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN   );
1079#endif
1080                  rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1081                }
1082#if H_3D_QTLPC
1083              }
1084#endif
1085            }
1086          }
1087        // test PCM
1088        if(pcPic->getSlice(0)->getSPS()->getUsePCM()
1089          && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize())
1090          && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) )
1091        {
1092          UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2;
1093          UInt uiBestBits = rpcBestCU->getTotalBits();
1094#if H_3D_VSO // M7
1095          Double dRDCostTemp = m_pcRdCost->getUseVSO() ? m_pcRdCost->calcRdCostVSO(uiRawBits, 0) : m_pcRdCost->calcRdCost(uiRawBits, 0);
1096          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > dRDCostTemp ))
1097#else
1098          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
1099#endif
1100          {
1101            xCheckIntraPCM (rpcBestCU, rpcTempCU);
1102            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1103          }
1104        }
1105#if  H_3D_FAST_TEXTURE_ENCODING
1106        }
1107#endif
1108        if (bIsLosslessMode)
1109        {
1110          iQP = iMinQP;
1111        }
1112      }
1113    }
1114
1115    m_pcEntropyCoder->resetBits();
1116    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
1117    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1118      rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1119    #if H_3D_VSO // M8
1120    if ( m_pcRdCost->getUseVSO() )   
1121      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );   
1122    else
1123#endif
1124    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
1125
1126    // Early CU determination
1127    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
1128    {
1129      bSubBranch = false;
1130    }
1131    else
1132    {
1133      bSubBranch = true;
1134    }
1135#if  H_3D_FAST_TEXTURE_ENCODING
1136    if(rpcBestCU->getSlice()->getViewIndex() && !rpcBestCU->getSlice()->getIsDepth() && (uiDepth >=iIVFMaxD) && rpcBestCU->isSkipped(0))
1137    {
1138      bSubBranch = false;
1139    }
1140#endif
1141  }
1142  else if(!(bSliceEnd && bInsidePicture))
1143  {
1144    bBoundary = true;
1145  }
1146
1147  // copy orginal YUV samples to PCM buffer
1148  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
1149  {
1150    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
1151  }
1152  if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1153  {
1154    Int idQP = m_pcEncCfg->getMaxDeltaQP();
1155    iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP );
1156    iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP );
1157  }
1158  else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() )
1159  {
1160    iMinQP = iBaseQP;
1161    iMaxQP = iBaseQP;
1162  }
1163  else
1164  {
1165    Int iStartQP;
1166    if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr())
1167    {
1168      iStartQP = rpcTempCU->getQP(0);
1169    }
1170    else
1171    {
1172      UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1173      iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx);
1174    }
1175    iMinQP = iStartQP;
1176    iMaxQP = iStartQP;
1177  }
1178  if ( m_pcEncCfg->getUseRateCtrl() )
1179  {
1180    iMinQP = m_pcRateCtrl->getRCQP();
1181    iMaxQP = m_pcRateCtrl->getRCQP();
1182  }
1183
1184  if ( m_pcEncCfg->getCUTransquantBypassFlagForceValue() )
1185  {
1186    iMaxQP = iMinQP; // If all blocks are forced into using transquant bypass, do not loop here.
1187  }
1188  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
1189  {
1190    const Bool bIsLosslessMode = false; // False at this level. Next level down may set it to true.
1191    rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1192
1193    // further split
1194#if H_3D_QTLPC
1195    if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
1196#else
1197    if( bSubBranch && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth )
1198#endif
1199    {
1200#if H_3D_VSO // M9
1201      // reset Model
1202      if( m_pcRdCost->getUseRenModel() )
1203      {
1204        UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth ( );
1205        UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight( );
1206        Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr( 0 );
1207        UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride();
1208        m_pcRdCost->setRenModelData( m_ppcBestCU[uiDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1209      }
1210#endif
1211
1212      UChar       uhNextDepth         = uiDepth+1;
1213      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
1214      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
1215
1216      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
1217      {
1218        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1219        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1220
1221        Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr();
1222        if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1223        {
1224            if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
1225            {
1226              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1227            }
1228            else
1229            {
1230              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
1231            }
1232
1233#if AMP_ENC_SPEEDUP
1234          if ( rpcBestCU->isIntra(0) )
1235          {
1236            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE );
1237          }
1238          else
1239          {
1240            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) );
1241          }
1242#else
1243          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
1244#endif
1245
1246          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
1247          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
1248        }
1249        else if (bInSlice)
1250        {
1251          pcSubBestPartCU->copyToPic( uhNextDepth );
1252          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
1253        }
1254      }
1255
1256      if( !bBoundary )
1257      {
1258        m_pcEntropyCoder->resetBits();
1259        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
1260
1261        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1262          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1263        }
1264#if H_3D_VSO // M10
1265      if ( m_pcRdCost->getUseVSO() )
1266        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1267      else
1268#endif
1269      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1270
1271      if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
1272      {
1273        Bool hasResidual = false;
1274        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
1275        {
1276          if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && 
1277              ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
1278          {
1279            hasResidual = true;
1280            break;
1281          }
1282        }
1283
1284        UInt uiTargetPartIdx;
1285        if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() )
1286        {
1287          uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU();
1288        }
1289        else
1290        {
1291          uiTargetPartIdx = 0;
1292        }
1293        if ( hasResidual )
1294        {
1295#if !RDO_WITHOUT_DQP_BITS
1296          m_pcEntropyCoder->resetBits();
1297          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
1298          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1299            rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1300#if H_3D_VSO // M11
1301          if ( m_pcRdCost->getUseLambdaScaleVSO())         
1302            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );         
1303          else
1304#endif
1305          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1306#endif
1307
1308          Bool foundNonZeroCbf = false;
1309          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf );
1310          assert( foundNonZeroCbf );
1311        }
1312        else
1313        {
1314          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
1315        }
1316      }
1317
1318        m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1319      Bool isEndOfSlice        = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES
1320                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
1321      Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
1322                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
1323      if(isEndOfSlice||isEndOfSliceSegment)
1324      {
1325        rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
1326      }
1327      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth);                                  // RD compare current larger prediction
1328    }                                                                                  // with sub partitioned prediction.
1329    }
1330
1331#if H_3D_VSO // M12
1332  if( m_pcRdCost->getUseRenModel() )
1333  {
1334    UInt  uiWidth     = m_ppcRecoYuvBest[uiDepth]->getWidth   ( );
1335    UInt  uiHeight    = m_ppcRecoYuvBest[uiDepth]->getHeight  ( );
1336    Pel*  piSrc       = m_ppcRecoYuvBest[uiDepth]->getLumaAddr( 0 );
1337    UInt  uiSrcStride = m_ppcRecoYuvBest[uiDepth]->getStride  ( );
1338    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1339  }
1340#endif
1341  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
1342
1343  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY );   // Copy Yuv data to picture Yuv
1344  if( bBoundary ||(bSliceEnd && bInsidePicture))
1345  {
1346    return;
1347  }
1348
1349  // Assert if Best prediction mode is NONE
1350  // Selected mode's RD-cost must be not MAX_DOUBLE.
1351  assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE  );
1352  assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE  );
1353  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE );
1354}
1355
1356/** finish encoding a cu and handle end-of-slice conditions
1357 * \param pcCU
1358 * \param uiAbsPartIdx
1359 * \param uiDepth
1360 * \returns Void
1361 */
1362Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1363{
1364  TComPic* pcPic = pcCU->getPic();
1365  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1366
1367  //Calculate end address
1368  UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx;
1369
1370  UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU();
1371  UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU();
1372  UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1373  UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1374  UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples();
1375  UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples();
1376  while(uiPosX>=uiWidth||uiPosY>=uiHeight)
1377  {
1378    uiInternalAddress--;
1379    uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ];
1380    uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ];
1381  }
1382  uiInternalAddress++;
1383  if(uiInternalAddress==pcCU->getPic()->getNumPartInCU())
1384  {
1385    uiInternalAddress = 0;
1386    uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1);
1387  }
1388  UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress);
1389
1390  // Encode slice finish
1391  Bool bTerminateSlice = false;
1392  if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress)
1393  {
1394    bTerminateSlice = true;
1395  }
1396  UInt uiGranularityWidth = g_uiMaxCUWidth;
1397  uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1398  uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1399  Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth))
1400    &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight));
1401 
1402  if(granularityBoundary)
1403  {
1404    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1405    if (!bTerminateSlice)
1406      m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 );
1407  }
1408 
1409  Int numberOfWrittenBits = 0;
1410  if (m_pcBitCounter)
1411  {
1412    numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits();
1413  }
1414 
1415  // Calculate slice end IF this CU puts us over slice bit size.
1416  UInt iGranularitySize = pcCU->getPic()->getNumPartInCU();
1417  Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize;
1418  if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) 
1419  {
1420    iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)));
1421  }
1422  // Set slice end parameter
1423  if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) 
1424  {
1425    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1426    pcSlice->setSliceCurEndCUAddr(iGranularityEnd);
1427    return;
1428  }
1429  // Set dependent slice end parameter
1430  if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) 
1431  {
1432    pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd);
1433    return;
1434  }
1435  if(granularityBoundary)
1436  {
1437    pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) );
1438    pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits);
1439    if (m_pcBitCounter)
1440    {
1441      m_pcEntropyCoder->resetBits();     
1442    }
1443  }
1444}
1445
1446/** Compute QP for each CU
1447 * \param pcCU Target CU
1448 * \param uiDepth CU depth
1449 * \returns quantization parameter
1450 */
1451Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1452{
1453  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1454  Int iQpOffset = 0;
1455  if ( m_pcEncCfg->getUseAdaptiveQP() )
1456  {
1457    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1458    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1459    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1460    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1461    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1462    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1463    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1464
1465    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1466    Double dAvgAct = pcAQLayer->getAvgActivity();
1467    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1468    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1469    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1470    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1471  }
1472  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset );
1473}
1474
1475/** encode a CU block recursively
1476 * \param pcCU
1477 * \param uiAbsPartIdx
1478 * \param uiDepth
1479 * \returns Void
1480 */
1481Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1482{
1483  TComPic* pcPic = pcCU->getPic();
1484 
1485  Bool bBoundary = false;
1486  UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1487  UInt uiRPelX   = uiLPelX + (g_uiMaxCUWidth>>uiDepth)  - 1;
1488  UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1489  UInt uiBPelY   = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1;
1490 
1491#if H_MV_ENC_DEC_TRAC
1492  DTRACE_CU_S("=========== coding_quadtree ===========\n")
1493  DTRACE_CU("x0", uiLPelX)
1494  DTRACE_CU("x1", uiTPelY)
1495  DTRACE_CU("log2CbSize", g_uiMaxCUWidth>>uiDepth)
1496  DTRACE_CU("cqtDepth"  , uiDepth)
1497#endif
1498
1499  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1500  // If slice start is within this cu...
1501  Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && 
1502    pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) );
1503  // We need to split, so don't try these modes.
1504  if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1505  {
1506    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1507  }
1508  else
1509  {
1510    bBoundary = true;
1511  }
1512 
1513  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary )
1514  {
1515    UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2;
1516    if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1517    {
1518      setdQPFlag(true);
1519    }
1520    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1521    {
1522      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1523      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1524      Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr();
1525      if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
1526      {
1527        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1528      }
1529    }
1530    return;
1531  }
1532 
1533#if H_MV_ENC_DEC_TRAC
1534  DTRACE_CU_S("=========== coding_unit ===========\n")
1535#endif
1536
1537  if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP())
1538  {
1539    setdQPFlag(true);
1540  }
1541  if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1542  {
1543    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1544  }
1545  if( !pcCU->getSlice()->isIntra() )
1546  {
1547    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1548  }
1549 
1550  if( pcCU->isSkipped( uiAbsPartIdx ) )
1551  {
1552#if H_MV_ENC_DEC_TRAC
1553    DTRACE_PU_S("=========== prediction_unit ===========\n")
1554    DTRACE_PU("x0", uiLPelX)
1555    DTRACE_PU("x1", uiTPelY)
1556#endif
1557    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1558#if H_3D_ARP
1559    m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1560#endif
1561#if H_3D_IC
1562    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1563#endif
1564    finishCU(pcCU,uiAbsPartIdx,uiDepth);
1565    return;
1566  }
1567#if H_3D_SINGLE_DEPTH
1568  m_pcEntropyCoder->encodeSingleDepthMode( pcCU, uiAbsPartIdx );
1569  if(!pcCU->getSingleDepthFlag(uiAbsPartIdx))
1570  {
1571#endif
1572  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1573 
1574  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1575 
1576#if H_3D_DIM_SDC
1577  m_pcEntropyCoder->encodeSDCFlag( pcCU, uiAbsPartIdx, false );
1578#endif
1579  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1580  {
1581    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1582
1583    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1584    {
1585      // Encode slice finish
1586      finishCU(pcCU,uiAbsPartIdx,uiDepth);
1587      return;
1588    }
1589  }
1590
1591  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1592  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1593
1594#if H_3D_ARP
1595  m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1596#endif
1597#if H_3D_IC
1598  m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1599#endif
1600  // Encode Coefficients
1601  Bool bCodeDQP = getdQPFlag();
1602  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP );
1603  setdQPFlag( bCodeDQP );
1604#if H_3D_SINGLE_DEPTH
1605  }
1606#endif
1607  // --- write terminating bit ---
1608  finishCU(pcCU,uiAbsPartIdx,uiDepth);
1609}
1610
1611Int xCalcHADs8x8_ISlice(Pel *piOrg, Int iStrideOrg) 
1612{
1613  Int k, i, j, jj;
1614  Int diff[64], m1[8][8], m2[8][8], m3[8][8], iSumHad = 0;
1615
1616  for( k = 0; k < 64; k += 8 )
1617  {
1618    diff[k+0] = piOrg[0] ;
1619    diff[k+1] = piOrg[1] ;
1620    diff[k+2] = piOrg[2] ;
1621    diff[k+3] = piOrg[3] ;
1622    diff[k+4] = piOrg[4] ;
1623    diff[k+5] = piOrg[5] ;
1624    diff[k+6] = piOrg[6] ;
1625    diff[k+7] = piOrg[7] ;
1626 
1627    piOrg += iStrideOrg;
1628  }
1629 
1630  //horizontal
1631  for (j=0; j < 8; j++)
1632  {
1633    jj = j << 3;
1634    m2[j][0] = diff[jj  ] + diff[jj+4];
1635    m2[j][1] = diff[jj+1] + diff[jj+5];
1636    m2[j][2] = diff[jj+2] + diff[jj+6];
1637    m2[j][3] = diff[jj+3] + diff[jj+7];
1638    m2[j][4] = diff[jj  ] - diff[jj+4];
1639    m2[j][5] = diff[jj+1] - diff[jj+5];
1640    m2[j][6] = diff[jj+2] - diff[jj+6];
1641    m2[j][7] = diff[jj+3] - diff[jj+7];
1642   
1643    m1[j][0] = m2[j][0] + m2[j][2];
1644    m1[j][1] = m2[j][1] + m2[j][3];
1645    m1[j][2] = m2[j][0] - m2[j][2];
1646    m1[j][3] = m2[j][1] - m2[j][3];
1647    m1[j][4] = m2[j][4] + m2[j][6];
1648    m1[j][5] = m2[j][5] + m2[j][7];
1649    m1[j][6] = m2[j][4] - m2[j][6];
1650    m1[j][7] = m2[j][5] - m2[j][7];
1651   
1652    m2[j][0] = m1[j][0] + m1[j][1];
1653    m2[j][1] = m1[j][0] - m1[j][1];
1654    m2[j][2] = m1[j][2] + m1[j][3];
1655    m2[j][3] = m1[j][2] - m1[j][3];
1656    m2[j][4] = m1[j][4] + m1[j][5];
1657    m2[j][5] = m1[j][4] - m1[j][5];
1658    m2[j][6] = m1[j][6] + m1[j][7];
1659    m2[j][7] = m1[j][6] - m1[j][7];
1660  }
1661 
1662  //vertical
1663  for (i=0; i < 8; i++)
1664  {
1665    m3[0][i] = m2[0][i] + m2[4][i];
1666    m3[1][i] = m2[1][i] + m2[5][i];
1667    m3[2][i] = m2[2][i] + m2[6][i];
1668    m3[3][i] = m2[3][i] + m2[7][i];
1669    m3[4][i] = m2[0][i] - m2[4][i];
1670    m3[5][i] = m2[1][i] - m2[5][i];
1671    m3[6][i] = m2[2][i] - m2[6][i];
1672    m3[7][i] = m2[3][i] - m2[7][i];
1673   
1674    m1[0][i] = m3[0][i] + m3[2][i];
1675    m1[1][i] = m3[1][i] + m3[3][i];
1676    m1[2][i] = m3[0][i] - m3[2][i];
1677    m1[3][i] = m3[1][i] - m3[3][i];
1678    m1[4][i] = m3[4][i] + m3[6][i];
1679    m1[5][i] = m3[5][i] + m3[7][i];
1680    m1[6][i] = m3[4][i] - m3[6][i];
1681    m1[7][i] = m3[5][i] - m3[7][i];
1682   
1683    m2[0][i] = m1[0][i] + m1[1][i];
1684    m2[1][i] = m1[0][i] - m1[1][i];
1685    m2[2][i] = m1[2][i] + m1[3][i];
1686    m2[3][i] = m1[2][i] - m1[3][i];
1687    m2[4][i] = m1[4][i] + m1[5][i];
1688    m2[5][i] = m1[4][i] - m1[5][i];
1689    m2[6][i] = m1[6][i] + m1[7][i];
1690    m2[7][i] = m1[6][i] - m1[7][i];
1691  }
1692 
1693  for (i = 0; i < 8; i++)
1694  {
1695    for (j = 0; j < 8; j++)
1696    {
1697      iSumHad += abs(m2[i][j]);
1698    }
1699  }
1700  iSumHad -= abs(m2[0][0]);
1701  iSumHad =(iSumHad+2)>>2;
1702  return(iSumHad);
1703}
1704
1705Int  TEncCu::updateLCUDataISlice(TComDataCU* pcCU, Int LCUIdx, Int width, Int height)
1706{
1707  Int  xBl, yBl; 
1708  const Int iBlkSize = 8;
1709
1710  Pel* pOrgInit   = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
1711  Int  iStrideOrig = pcCU->getPic()->getPicYuvOrg()->getStride();
1712  Pel  *pOrg;
1713
1714  Int iSumHad = 0;
1715  for ( yBl=0; (yBl+iBlkSize)<=height; yBl+= iBlkSize)
1716  {
1717    for ( xBl=0; (xBl+iBlkSize)<=width; xBl+= iBlkSize)
1718    {
1719      pOrg = pOrgInit + iStrideOrig*yBl + xBl; 
1720      iSumHad += xCalcHADs8x8_ISlice(pOrg, iStrideOrig);
1721    }
1722  }
1723  return(iSumHad);
1724}
1725
1726/** check RD costs for a CU block encoded with merge
1727 * \param rpcBestCU
1728 * \param rpcTempCU
1729 * \returns Void
1730 */
1731Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode )
1732{
1733  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1734#if H_3D_IV_MERGE
1735  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS_MEM << 1]; // double length for mv of both lists
1736  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS_MEM];
1737#else
1738  TComMvField  cMvFieldNeighbours[2 * MRG_MAX_NUM_CANDS]; // double length for mv of both lists
1739  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1740#endif
1741  Int numValidMergeCand = 0;
1742  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
1743
1744  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1745  {
1746    uhInterDirNeighbours[ui] = 0;
1747  }
1748  UChar uhDepth = rpcTempCU->getDepth( 0 );
1749#if H_3D_IC
1750  Bool bICFlag = rpcTempCU->getICFlag( 0 );
1751#endif
1752#if H_3D_VSO // M1  //nececcary here?
1753  if( m_pcRdCost->getUseRenModel() )
1754  {
1755    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
1756    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
1757    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
1758    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
1759    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1760  }
1761#endif
1762
1763#if H_3D_ARP
1764  DisInfo cOrigDisInfo = rpcTempCU->getDvInfo(0);
1765#else
1766  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1767#endif
1768
1769#if H_3D_VSP
1770#if !H_3D_ARP
1771  Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1772  memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1773  InheritedVSPDisInfo inheritedVSPDisInfo[MRG_MAX_NUM_CANDS_MEM];
1774  rpcTempCU->m_bAvailableFlagA1 = 0;
1775  rpcTempCU->m_bAvailableFlagB1 = 0;
1776  rpcTempCU->m_bAvailableFlagB0 = 0;
1777  rpcTempCU->m_bAvailableFlagA0 = 0;
1778  rpcTempCU->m_bAvailableFlagB2 = 0;
1779  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1780  rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag,inheritedVSPDisInfo, numValidMergeCand );
1781#endif
1782#else
1783#if H_3D
1784  rpcTempCU->m_bAvailableFlagA1 = 0;
1785  rpcTempCU->m_bAvailableFlagB1 = 0;
1786  rpcTempCU->m_bAvailableFlagB0 = 0;
1787  rpcTempCU->m_bAvailableFlagA0 = 0;
1788  rpcTempCU->m_bAvailableFlagB2 = 0;
1789  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1790  rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1791#else
1792  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1793#endif
1794#endif
1795
1796#if H_3D_IV_MERGE
1797  Int mergeCandBuffer[MRG_MAX_NUM_CANDS_MEM];
1798#else
1799  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1800#endif
1801#if H_3D_ARP
1802for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1803#else
1804for( UInt ui = 0; ui < numValidMergeCand; ++ui )
1805#endif
1806  {
1807    mergeCandBuffer[ui] = 0;
1808  }
1809
1810  Bool bestIsSkip = false;
1811
1812  UInt iteration;
1813  if ( rpcTempCU->isLosslessCoded(0))
1814  {
1815    iteration = 1;
1816  }
1817  else 
1818  {
1819    iteration = 2;
1820  }
1821
1822#if H_3D_ARP
1823  Int nARPWMax = rpcTempCU->getSlice()->getARPStepNum() - 1;
1824  if( nARPWMax < 0 || !rpcTempCU->getDvInfo(0).bDV || bICFlag )
1825  {
1826    nARPWMax = 0;
1827  }
1828  for( Int nARPW=nARPWMax; nARPW >= 0 ; nARPW-- )
1829  {
1830    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS_MEM*sizeof(Int) );
1831    rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1832    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1833#if H_3D_IC
1834    rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1835#endif
1836    rpcTempCU->getDvInfo(0) = cOrigDisInfo;
1837    rpcTempCU->setDvInfoSubParts(cOrigDisInfo, 0, 0, uhDepth );
1838    Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1839    memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1840#if H_3D_SPIVMP
1841    Bool bSPIVMPFlag[MRG_MAX_NUM_CANDS_MEM];
1842    memset(bSPIVMPFlag, false, sizeof(Bool)*MRG_MAX_NUM_CANDS_MEM);
1843    TComMvField*  pcMvFieldSP;
1844    UChar* puhInterDirSP;
1845    pcMvFieldSP = new TComMvField[rpcTempCU->getPic()->getPicSym()->getNumPartition()*2]; 
1846    puhInterDirSP = new UChar[rpcTempCU->getPic()->getPicSym()->getNumPartition()]; 
1847#endif
1848#if H_3D
1849    rpcTempCU->initAvailableFlags();
1850    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1851    rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours
1852#if H_3D_SPIVMP
1853      , pcMvFieldSP, puhInterDirSP
1854#endif
1855      , numValidMergeCand
1856      );
1857
1858    rpcTempCU->buildMCL( cMvFieldNeighbours,uhInterDirNeighbours, vspFlag
1859#if H_3D_SPIVMP
1860      , bSPIVMPFlag
1861#endif
1862      , numValidMergeCand
1863      );
1864
1865#else
1866    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, vspFlag, numValidMergeCand );
1867#endif
1868
1869
1870#endif
1871
1872#if H_3D_DDD
1873    Int iDDDCand = rpcTempCU->getUseDDDCandIdx(); 
1874    UChar ucDDDepth = rpcTempCU->getDDTmpDepth();
1875    rpcTempCU->setUseDDD( false, 0, uhDepth );
1876#endif
1877
1878  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1879  {
1880    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1881    {     
1882#if H_3D_IC
1883        if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
1884        {
1885          if( bICFlag && uiMergeCand == 0 ) 
1886          {
1887            continue;
1888          }
1889        }
1890#endif
1891        if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1892        {
1893        if( !(bestIsSkip && uiNoResidual == 0) )
1894        {
1895          // set MC parameters
1896          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
1897          rpcTempCU->setCUTransquantBypassSubParts( bTransquantBypassFlag,     0, uhDepth );
1898          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1899#if H_3D_IC
1900          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1901#endif
1902#if H_3D_ARP
1903          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1904#endif
1905          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
1906          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
1907#if H_3D_VSP
1908          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
1909#endif
1910#if H_3D_DDD
1911          if( rpcTempCU->getSlice()->getIsDepth() && rpcTempCU->getSlice()->getViewIndex() != 0 && iDDDCand == uiMergeCand )
1912          {
1913              rpcTempCU->setUseDDD( true, 0, 0, uhDepth );
1914              rpcTempCU->setDDDepthSubParts( ucDDDepth, 0, 0, uhDepth );
1915          }
1916          else
1917          {
1918              rpcTempCU->setUseDDD( false, 0, 0, uhDepth );
1919          }
1920#endif
1921#if H_3D_SPIVMP
1922          rpcTempCU->setSPIVMPFlagSubParts(bSPIVMPFlag[uiMergeCand], 0, 0, uhDepth);
1923          if (bSPIVMPFlag[uiMergeCand])
1924          {
1925            UInt uiSPAddr;
1926            Int iWidth = rpcTempCU->getWidth(0);
1927            Int iHeight = rpcTempCU->getHeight(0);
1928            Int iNumSPInOneLine, iNumSP, iSPWidth, iSPHeight;
1929            rpcTempCU->getSPPara(iWidth, iHeight, iNumSP, iNumSPInOneLine, iSPWidth, iSPHeight);
1930            for (Int iPartitionIdx = 0; iPartitionIdx < iNumSP; iPartitionIdx++)
1931            {
1932              rpcTempCU->getSPAbsPartIdx(0, iSPWidth, iSPHeight, iPartitionIdx, iNumSPInOneLine, uiSPAddr);
1933              rpcTempCU->setInterDirSP(puhInterDirSP[iPartitionIdx], uiSPAddr, iSPWidth, iSPHeight);
1934              rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx], iSPWidth, iSPHeight);
1935              rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx + 1], iSPWidth, iSPHeight);
1936            }
1937          }
1938          else
1939#endif
1940#if H_3D_VSP
1941          {
1942          if ( vspFlag[uiMergeCand] )
1943          {
1944            UInt partAddr;
1945            Int vspSize;
1946            Int width, height;
1947            rpcTempCU->getPartIndexAndSize( 0, partAddr, width, height );
1948            if( uhInterDirNeighbours[ uiMergeCand ] & 0x01 )
1949            {
1950              rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_0, cMvFieldNeighbours[ 2*uiMergeCand + 0 ].getRefIdx(), vspSize );
1951              rpcTempCU->setVSPFlag( partAddr, vspSize );
1952            }
1953            else
1954            {
1955              rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1956            }
1957            if( uhInterDirNeighbours[ uiMergeCand ] & 0x02 )
1958            {
1959              rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_1 , cMvFieldNeighbours[ 2*uiMergeCand + 1 ].getRefIdx(), vspSize );
1960              rpcTempCU->setVSPFlag( partAddr, vspSize );
1961            }
1962            else
1963            {
1964              rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1965            }
1966            rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1967          }
1968          else
1969          {
1970#endif
1971            rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
1972            rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1973            rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1974#if H_3D_VSP
1975          }
1976        }
1977#endif
1978       // do MC
1979       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1980       // estimate residual and encode everything
1981#if H_3D_VSO //M2
1982       if( m_pcRdCost->getUseRenModel() )
1983       { //Reset
1984         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
1985         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
1986         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
1987         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
1988         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1989       }
1990#endif
1991       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1992         m_ppcOrigYuv    [uhDepth],
1993         m_ppcPredYuvTemp[uhDepth],
1994         m_ppcResiYuvTemp[uhDepth],
1995         m_ppcResiYuvBest[uhDepth],
1996         m_ppcRecoYuvTemp[uhDepth],
1997         (uiNoResidual? true:false));
1998
1999
2000          if ( uiNoResidual == 0 && rpcTempCU->getQtRootCbf(0) == 0 )
2001         {
2002            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
2003           mergeCandBuffer[uiMergeCand] = 1;
2004         }
2005
2006          rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
2007#if H_3D_SINGLE_DEPTH
2008          rpcTempCU->setSingleDepthFlagSubParts( false, 0, uhDepth );
2009#endif
2010#if H_3D_VSP // possible bug fix
2011          if( rpcTempCU->getSkipFlag(0) )
2012          {
2013            rpcTempCU->setTrIdxSubParts(0, 0, uhDepth);
2014          }
2015#endif
2016#if H_3D_INTER_SDC
2017          TComDataCU *rpcTempCUPre = rpcTempCU;
2018#endif
2019          Int orgQP = rpcTempCU->getQP( 0 );
2020          xCheckDQP( rpcTempCU );
2021          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2022#if H_3D_INTER_SDC
2023          if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() && !uiNoResidual )
2024          {
2025            Double dOffsetCost[3] = {MAX_DOUBLE,MAX_DOUBLE,MAX_DOUBLE};
2026            for( Int uiOffest = 1 ; uiOffest <= 5 ; uiOffest++ )
2027            {
2028              if( uiOffest > 3)
2029              {
2030                if ( dOffsetCost[0] < (0.9*dOffsetCost[1]) && dOffsetCost[0] < (0.9*dOffsetCost[2]) )
2031                {
2032                  continue;
2033                }
2034                if ( dOffsetCost[1] < dOffsetCost[0] && dOffsetCost[0] < dOffsetCost[2] &&  uiOffest == 5)
2035                {
2036                  continue;
2037                }
2038                if ( dOffsetCost[0] < dOffsetCost[1] && dOffsetCost[2] < dOffsetCost[0] &&  uiOffest == 4)
2039                {
2040                  continue;
2041                }
2042              }
2043              if( rpcTempCU != rpcTempCUPre )
2044              {
2045                rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag  );
2046                rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
2047              }
2048              rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2049#if H_3D_SINGLE_DEPTH
2050              rpcTempCU->setSingleDepthFlagSubParts( false, 0, uhDepth );
2051#endif
2052              rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
2053              rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
2054#if H_3D_VSO //M2
2055              if( m_pcRdCost->getUseRenModel() )
2056              { //Reset
2057                UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
2058                UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
2059                Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
2060                UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
2061                m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2062              }
2063#endif
2064              Int iSdcOffset = 0;
2065              if(uiOffest % 2 == 0)
2066              {
2067                iSdcOffset = uiOffest >> 1;
2068              }
2069              else
2070              {
2071                iSdcOffset = -1 * (uiOffest >> 1);
2072              }
2073              m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2074                m_ppcOrigYuv[uhDepth], 
2075                ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth], 
2076                m_ppcResiYuvTemp[uhDepth], 
2077                m_ppcRecoYuvTemp[uhDepth],
2078                iSdcOffset,
2079                uhDepth );
2080              if (uiOffest <= 3 )
2081              {
2082                dOffsetCost [uiOffest -1] = rpcTempCU->getTotalCost();
2083              }
2084
2085              xCheckDQP( rpcTempCU );
2086              xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
2087            }
2088          }
2089#endif
2090          rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag );
2091
2092      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
2093      {
2094#if H_3D_INTER_SDC
2095        if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) )
2096        {
2097          bestIsSkip = !rpcBestCU->getSDCFlag( 0 ) && ( rpcBestCU->getQtRootCbf(0) == 0 );
2098        }
2099        else
2100        {
2101#endif
2102        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
2103#if H_3D_INTER_SDC
2104        }
2105#endif
2106      }
2107    }
2108   }
2109  }
2110
2111  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
2112  {
2113    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
2114    {
2115      if( rpcBestCU->getMergeFlag( 0 ))
2116      {
2117        *earlyDetectionSkipMode = true;
2118      }
2119      else
2120      {
2121        Int absoulte_MV=0;
2122        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2123        {
2124          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
2125          {
2126            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
2127            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
2128            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
2129            absoulte_MV+=iHor+iVer;
2130          }
2131        }
2132
2133        if(absoulte_MV == 0)
2134        {
2135          *earlyDetectionSkipMode = true;
2136        }
2137      }
2138    }
2139  }
2140 }
2141#if H_3D_SPIVMP
2142 delete[] pcMvFieldSP;
2143 delete[] puhInterDirSP;
2144#endif
2145#if H_3D_ARP
2146 }
2147#endif
2148}
2149
2150
2151#if AMP_MRG
2152#if  H_3D_FAST_TEXTURE_ENCODING
2153Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bFMD, Bool bUseMRG)
2154#else
2155Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG)
2156#endif
2157#else
2158Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
2159#endif
2160{
2161
2162#if H_3D
2163  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
2164#endif
2165#if  H_3D_FAST_TEXTURE_ENCODING
2166  if(!(bFMD && (ePartSize == SIZE_2Nx2N)))  //have  motion estimation or merge check
2167  {
2168#endif
2169  UChar uhDepth = rpcTempCU->getDepth( 0 );
2170#if H_3D_ARP
2171  Int iLayerId    = rpcTempCU->getSlice()->getLayerId();
2172  Bool bFirstTime = true;
2173  Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
2174
2175  if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || !rpcTempCU->getDvInfo(0).bDV || rpcTempCU->getICFlag(0) )
2176  {
2177    nARPWMax = 0;
2178  }
2179
2180  for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
2181  {
2182    if( bFirstTime == false && rpcTempCU->getSlice()->getVPS()->getUseAdvRP( iLayerId ) )
2183    {
2184      rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0),bTransquantBypassFlag );     
2185    }
2186#endif
2187#if H_3D_VSO // M3
2188  if( m_pcRdCost->getUseRenModel() )
2189  {
2190    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2191    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2192    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2193    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2194    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2195  }
2196#endif
2197
2198  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2199 
2200  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2201#if H_3D_SINGLE_DEPTH
2202  rpcTempCU->setSingleDepthFlagSubParts( false, 0, uhDepth );
2203#endif
2204  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
2205  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2206#if H_3D_DDD
2207  rpcTempCU->setUseDDD( false, 0, uhDepth );
2208#endif
2209
2210#if H_3D_ARP
2211  rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2212#endif
2213
2214#if H_3D_ARP
2215  if( bFirstTime == false && nARPWMax )
2216  {
2217    rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
2218    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2219
2220    m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
2221  }
2222  else
2223  {
2224    bFirstTime = false;
2225#endif
2226#if AMP_MRG
2227  rpcTempCU->setMergeAMP (true);
2228#if  H_3D_FAST_TEXTURE_ENCODING
2229  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bFMD, false, bUseMRG );
2230#else
2231  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG );
2232#endif
2233#else 
2234  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
2235#endif
2236#if H_3D_ARP
2237   if( nARPWMax )
2238   {
2239     m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
2240   }
2241  }
2242#endif
2243
2244#if AMP_MRG
2245  if ( !rpcTempCU->getMergeAMP() )
2246  {
2247#if H_3D_ARP
2248    if( nARPWMax )
2249    {
2250      continue;
2251    }
2252    else
2253#endif
2254    return;
2255  }
2256#endif
2257
2258#if KWU_RC_MADPRED_E0227
2259  if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2260  {
2261    UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2262      m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2263      rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2264    m_temporalSAD = (Int)SAD;
2265  }
2266#endif
2267  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
2268#if H_3D_VSP // possible bug fix
2269  if( rpcTempCU->getQtRootCbf(0)==0 )
2270  {
2271    rpcTempCU->setTrIdxSubParts(0, 0, uhDepth);
2272  }
2273#endif
2274
2275#if H_3D_VSO // M4
2276  if( m_pcRdCost->getUseLambdaScaleVSO() )
2277    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2278  else
2279#endif
2280
2281  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2282#if H_3D_INTER_SDC
2283  TComDataCU *rpcTempCUPre = rpcTempCU;
2284#endif
2285  xCheckDQP( rpcTempCU );
2286  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2287#if H_3D_INTER_SDC
2288  if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() && ePartSize == SIZE_2Nx2N)
2289  {
2290    Double dOffsetCost[3] = {MAX_DOUBLE,MAX_DOUBLE,MAX_DOUBLE};
2291    for( Int uiOffest = 1 ; uiOffest <= 5 ; uiOffest++ )
2292    {
2293      if( uiOffest > 3)
2294      {
2295        if ( dOffsetCost[0] < (0.9*dOffsetCost[1]) && dOffsetCost[0] < (0.9*dOffsetCost[2]) )
2296        {
2297          continue;
2298        }
2299        if ( dOffsetCost[1] < dOffsetCost[0] && dOffsetCost[0] < dOffsetCost[2] &&  uiOffest == 5)
2300        {
2301          continue;
2302        }
2303        if ( dOffsetCost[0] < dOffsetCost[1] && dOffsetCost[2] < dOffsetCost[0] &&  uiOffest == 4)
2304        {
2305          continue;
2306        }
2307      }
2308
2309      if( rpcTempCU != rpcTempCUPre )
2310      {
2311        Int orgQP = rpcBestCU->getQP( 0 );
2312        rpcTempCU->initEstData( uhDepth, orgQP ,bTransquantBypassFlag );     
2313        rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
2314      }
2315      rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2316#if H_3D_SINGLE_DEPTH
2317      rpcTempCU->setSingleDepthFlagSubParts( false, 0, uhDepth );
2318#endif
2319      rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
2320      rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
2321#if H_3D_VSO // M3
2322      if( m_pcRdCost->getUseRenModel() )
2323      {
2324        UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2325        UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2326        Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2327        UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2328        m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2329      }
2330#endif
2331
2332      Int iSdcOffset = 0;
2333      if(uiOffest % 2 == 0)
2334      {
2335        iSdcOffset = uiOffest >> 1;
2336      }
2337      else
2338      {
2339        iSdcOffset = -1 * (uiOffest >> 1);
2340      }
2341      m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2342        m_ppcOrigYuv[uhDepth],
2343        ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth],
2344        m_ppcResiYuvTemp[uhDepth],
2345        m_ppcRecoYuvTemp[uhDepth],
2346        iSdcOffset,
2347        uhDepth );
2348      if (uiOffest <= 3 )
2349      {
2350        dOffsetCost [uiOffest -1] = rpcTempCU->getTotalCost();
2351      }
2352
2353      xCheckDQP( rpcTempCU );
2354      xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2355    }
2356
2357  }
2358#endif
2359#if H_3D_ARP
2360  }
2361#endif
2362#if  H_3D_FAST_TEXTURE_ENCODING
2363  }
2364#endif
2365}
2366
2367#if H_3D_DBBP
2368Void TEncCu::xInvalidateOriginalSegments( TComYuv* pOrigYuv, TComYuv* pOrigYuvTemp, Bool* pMask, UInt uiValidSegment )
2369{
2370  UInt  uiWidth     = pOrigYuv->getWidth ( );
2371  UInt  uiHeight    = pOrigYuv->getHeight( );
2372  Pel*  piSrc       = pOrigYuv->getLumaAddr( );
2373  UInt  uiSrcStride = pOrigYuv->getStride();
2374  Pel*  piDst       = pOrigYuvTemp->getLumaAddr( );
2375  UInt  uiDstStride = pOrigYuvTemp->getStride();
2376 
2377  UInt  uiMaskStride= MAX_CU_SIZE;
2378 
2379  AOF( uiWidth == uiHeight );
2380 
2381  // backup pointer
2382  Bool* pMaskStart = pMask;
2383 
2384  for (Int y=0; y<uiHeight; y++)
2385  {
2386    for (Int x=0; x<uiWidth; x++)
2387    {
2388      UChar ucSegment = (UChar)pMask[x];
2389      AOF( ucSegment < 2 );
2390     
2391      piDst[x] = (ucSegment==uiValidSegment)?piSrc[x]:DBBP_INVALID_SHORT;
2392    }
2393   
2394    piSrc  += uiSrcStride;
2395    piDst  += uiDstStride;
2396    pMask  += uiMaskStride;
2397  }
2398 
2399  // now invalidate chroma
2400  Pel*  piSrcU       = pOrigYuv->getCbAddr();
2401  Pel*  piSrcV       = pOrigYuv->getCrAddr();
2402  UInt  uiSrcStrideC = pOrigYuv->getCStride();
2403  Pel*  piDstU       = pOrigYuvTemp->getCbAddr( );
2404  Pel*  piDstV       = pOrigYuvTemp->getCrAddr( );
2405  UInt  uiDstStrideC = pOrigYuvTemp->getCStride();
2406  pMask = pMaskStart;
2407 
2408  for (Int y=0; y<uiHeight/2; y++)
2409  {
2410    for (Int x=0; x<uiWidth/2; x++)
2411    {
2412      UChar ucSegment = (UChar)pMask[x*2];
2413      AOF( ucSegment < 2 );
2414     
2415      piDstU[x] = (ucSegment==uiValidSegment)?piSrcU[x]:DBBP_INVALID_SHORT;
2416      piDstV[x] = (ucSegment==uiValidSegment)?piSrcV[x]:DBBP_INVALID_SHORT;
2417    }
2418   
2419    piSrcU  += uiSrcStrideC;
2420    piSrcV  += uiSrcStrideC;
2421    piDstU  += uiDstStrideC;
2422    piDstV  += uiDstStrideC;
2423    pMask   += 2*uiMaskStride;
2424  }
2425}
2426#if H_3D_SINGLE_DEPTH
2427Void TEncCu::xCheckRDCostSingleDepth( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2428{
2429  UInt uiDepth = rpcTempCU->getDepth( 0 );
2430  if( !rpcBestCU->getSlice()->getIsDepth() || (eSize != SIZE_2Nx2N))
2431  {
2432    return;
2433  }
2434 
2435#if H_3D_VSO // M5
2436  if( m_pcRdCost->getUseRenModel() )
2437  {
2438    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ();
2439    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ();
2440    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getLumaAddr();
2441    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ();
2442    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2443  }
2444#endif
2445
2446  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2447  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2448  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2449  rpcTempCU->setCUTransquantBypassSubParts( rpcTempCU->getCUTransquantBypass(0), 0, uiDepth );
2450
2451  rpcTempCU->setTrIdxSubParts(0, 0, uiDepth);
2452  rpcTempCU->setCbfSubParts(0, 1, 1, 0, uiDepth);
2453  rpcTempCU->setSingleDepthFlagSubParts(true, 0, uiDepth);
2454  rpcTempCU->setLumaIntraDirSubParts (DC_IDX, 0, uiDepth);
2455#if H_3D_DIM_SDC
2456  rpcTempCU->setSDCFlagSubParts( false, 0, uiDepth);
2457#endif
2458
2459  UInt uiPreCalcDistC;
2460  m_pcPredSearch  ->estIntraPredSingleDepth      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, false );
2461
2462
2463  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
2464 
2465 
2466  m_pcEntropyCoder->resetBits();
2467  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2468  {
2469    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2470  }
2471  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2472  m_pcEntropyCoder->encodeSingleDepthMode( rpcTempCU, 0,          true );
2473 
2474
2475  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2476 
2477  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2478  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2479
2480
2481#if H_3D_VSO // M6
2482  if( m_pcRdCost->getUseLambdaScaleVSO()) 
2483    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
2484  else
2485#endif
2486  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2487 
2488
2489  xCheckDQP( rpcTempCU );
2490  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2491}
2492#endif
2493
2494Void TEncCu::xCheckRDCostInterDBBP( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool bUseMRG )
2495{
2496  AOF( !rpcTempCU->getSlice()->getIsDepth() );
2497 
2498  UChar uhDepth = rpcTempCU->getDepth( 0 );
2499 
2500#if H_3D_VSO
2501  if( m_pcRdCost->getUseRenModel() )
2502  {
2503    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( );
2504    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( );
2505    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr( );
2506    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride();
2507    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2508  }
2509#endif
2510 
2511  UInt uiWidth  = rpcTempCU->getWidth(0);
2512  UInt uiHeight = rpcTempCU->getHeight(0);
2513  AOF( uiWidth == uiHeight );
2514 
2515#if H_3D_DBBP
2516  // Is this correct here, was under the macro SEC_DBBP_DISALLOW_8x8_I0078, however the function is related to Single Depth Mode
2517  if(uiWidth <= 8)
2518  {
2519    return;
2520  }
2521#endif
2522 
2523  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N,  0, uhDepth );
2524 
2525  // fetch virtual depth block
2526  UInt uiDepthStride = 0;
2527#if H_3D_FCO
2528  Pel* pDepthPels = rpcTempCU->getVirtualDepthBlock(rpcTempCU->getZorderIdxInCU(), uiWidth, uiHeight, uiDepthStride);
2529#else
2530  Pel* pDepthPels = rpcTempCU->getVirtualDepthBlock(0, uiWidth, uiHeight, uiDepthStride);
2531#endif
2532  AOF( pDepthPels != NULL );
2533  AOF( uiDepthStride != 0 );
2534 
2535  // derive partitioning from depth
2536  PartSize eVirtualPartSize = m_pcPredSearch->getPartitionSizeFromDepth(pDepthPels, uiDepthStride, uiWidth);
2537 
2538  // derive segmentation mask from depth
2539  Bool pMask[MAX_CU_SIZE*MAX_CU_SIZE];
2540  Bool bValidMask = m_pcPredSearch->getSegmentMaskFromDepth(pDepthPels, uiDepthStride, uiWidth, uiHeight, pMask);
2541 
2542  if( !bValidMask )
2543  {
2544    return;
2545  }
2546 
2547  // find optimal motion/disparity vector for each segment
2548  DisInfo originalDvInfo = rpcTempCU->getDvInfo(0);
2549  DBBPTmpData* pDBBPTmpData = rpcTempCU->getDBBPTmpData();
2550  TComYuv* apPredYuv[2] = { m_ppcRecoYuvTemp[uhDepth], m_ppcPredYuvTemp[uhDepth] };
2551 
2552  // find optimal motion vector fields for both segments (as 2Nx2N)
2553  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2554  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N,  0, uhDepth );
2555  rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth );
2556  for( UInt uiSegment = 0; uiSegment < 2; uiSegment++ )
2557  {
2558    rpcTempCU->setDBBPFlagSubParts(true, 0, 0, uhDepth);
2559    rpcTempCU->setDvInfoSubParts(originalDvInfo, 0, uhDepth);
2560   
2561    // invalidate all other segments in original YUV
2562    xInvalidateOriginalSegments(m_ppcOrigYuv[uhDepth], m_ppcOrigYuvDBBP[uhDepth], pMask, uiSegment);
2563   
2564    // do motion estimation for this segment
2565    m_pcRdCost->setUseMask(true);
2566    rpcTempCU->getDBBPTmpData()->eVirtualPartSize = eVirtualPartSize;
2567    rpcTempCU->getDBBPTmpData()->uiVirtualPartIndex = uiSegment;
2568    m_pcPredSearch->predInterSearch( rpcTempCU, m_ppcOrigYuvDBBP[uhDepth], apPredYuv[uiSegment], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], false, false, bUseMRG );
2569    m_pcRdCost->setUseMask(false);
2570   
2571    // extract motion parameters of full block for this segment
2572    pDBBPTmpData->auhInterDir[uiSegment] = rpcTempCU->getInterDir(0);
2573   
2574    pDBBPTmpData->abMergeFlag[uiSegment] = rpcTempCU->getMergeFlag(0);
2575    pDBBPTmpData->auhMergeIndex[uiSegment] = rpcTempCU->getMergeIndex(0);
2576   
2577    AOF( rpcTempCU->getSPIVMPFlag(0) == false );
2578    AOF( rpcTempCU->getVSPFlag(0) == 0 );
2579   
2580    for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2581    {
2582      RefPicList eRefList = (RefPicList)uiRefListIdx;
2583     
2584      pDBBPTmpData->acMvd[uiSegment][eRefList] = rpcTempCU->getCUMvField(eRefList)->getMvd(0);
2585      pDBBPTmpData->aiMvpNum[uiSegment][eRefList] = rpcTempCU->getMVPNum(eRefList, 0);
2586      pDBBPTmpData->aiMvpIdx[uiSegment][eRefList] = rpcTempCU->getMVPIdx(eRefList, 0);
2587     
2588      rpcTempCU->getMvField(rpcTempCU, 0, eRefList, pDBBPTmpData->acMvField[uiSegment][eRefList]);
2589    }
2590  }
2591 
2592  // store final motion/disparity information in each PU using derived partitioning
2593  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2594  rpcTempCU->setPartSizeSubParts  ( eVirtualPartSize,  0, uhDepth );
2595  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2596 
2597  UInt uiPUOffset = ( g_auiPUOffset[UInt( eVirtualPartSize )] << ( ( rpcTempCU->getSlice()->getSPS()->getMaxCUDepth() - uhDepth ) << 1 ) ) >> 4;
2598  for( UInt uiSegment = 0; uiSegment < 2; uiSegment++ )
2599  {
2600    UInt uiPartAddr = uiSegment*uiPUOffset;
2601   
2602    rpcTempCU->setDBBPFlagSubParts(true, uiPartAddr, uiSegment, uhDepth);
2603   
2604    // now set stored information from 2Nx2N motion search to each partition
2605    rpcTempCU->setInterDirSubParts(pDBBPTmpData->auhInterDir[uiSegment], uiPartAddr, uiSegment, uhDepth); // interprets depth relative to LCU level
2606   
2607    rpcTempCU->setMergeFlagSubParts(pDBBPTmpData->abMergeFlag[uiSegment], uiPartAddr, uiSegment, uhDepth);
2608    rpcTempCU->setMergeIndexSubParts(pDBBPTmpData->auhMergeIndex[uiSegment], uiPartAddr, uiSegment, uhDepth);
2609       
2610    for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2611    {
2612      RefPicList eRefList = (RefPicList)uiRefListIdx;
2613     
2614      rpcTempCU->getCUMvField( eRefList )->setAllMvd(pDBBPTmpData->acMvd[uiSegment][eRefList], eVirtualPartSize, uiPartAddr, 0, uiSegment);
2615      rpcTempCU->setMVPNum(eRefList, uiPartAddr, pDBBPTmpData->aiMvpNum[uiSegment][eRefList]);
2616      rpcTempCU->setMVPIdx(eRefList, uiPartAddr, pDBBPTmpData->aiMvpIdx[uiSegment][eRefList]);
2617     
2618      rpcTempCU->getCUMvField( eRefList )->setAllMvField( pDBBPTmpData->acMvField[uiSegment][eRefList], eVirtualPartSize, uiPartAddr, 0, uiSegment ); // interprets depth relative to rpcTempCU level
2619    }
2620  }
2621 
2622  // reconstruct final prediction signal by combining both segments
2623  m_pcPredSearch->combineSegmentsWithMask(apPredYuv, m_ppcPredYuvTemp[uhDepth], pMask, uiWidth, uiHeight, 0, eVirtualPartSize);
2624  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
2625 
2626  xCheckDQP( rpcTempCU );
2627  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2628}
2629#endif
2630
2631#if H_3D_DIM
2632Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize, Bool bOnlyIVP )
2633#else
2634Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2635#endif
2636{
2637  UInt uiDepth = rpcTempCU->getDepth( 0 );
2638 
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.