source: 3DVCSoftware/branches/HTM-15.2-dev/source/Lib/TLibEncoder/TEncCu.cpp @ 1373

Last change on this file since 1373 was 1373, checked in by tech, 8 years ago

Macro fixes.

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