source: 3DVCSoftware/branches/HTM-13.0-dev0/source/Lib/TLibEncoder/TEncCu.cpp @ 1131

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

Cleanup.

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