source: 3DVCSoftware/branches/HTM-11.2-dev2-MediaTek/source/Lib/TLibEncoder/TEncCu.cpp @ 1007

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

JCT3V-I0099. Changes of sub-PU size signaling and lim_qt_pred_flag signaling in VPS extension 2. Remove use_qtl_flag and use_pc_flag.

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