source: 3DVCSoftware/branches/HTM-14.1-update-dev1/source/Lib/TLibEncoder/TEncCu.cpp @ 1279

Last change on this file since 1279 was 1279, checked in by tech, 9 years ago

Merged 14.1-update-dev2@1277.

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