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

Last change on this file since 1280 was 1280, checked in by tech, 9 years ago
  • Preliminary fix for pruning.
  • 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 DEBUG_STRING_PASS_INTO(sDebug), 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 H_3D_DIM_ENC
756    if( rpcBestCU->getSlice()->getIsDepth() && rpcBestCU->getSlice()->isIRAP() )
757    {
758      earlyDetectionSkipMode = false;
759    }
760#endif
761#if H_3D
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()) && (
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 // avoid very complex intra if it is unlikely
1102#if H_3D_DIM_ENC
1103            || rpcBestCU->getSlice()->getIsDepth()
1104#endif
1105            ))
1106        {
1107#if H_3D_DIM
1108            Bool bOnlyIVP = false;
1109            Bool bUseIVP = true;
1110            if( rpcBestCU->getSlice()->getIsDepth() && !(rpcBestCU->getSlice()->isIRAP()) && 
1111              rpcBestCU->getSlice()->getSliceType() != I_SLICE && 
1112              rpcBestCU->getCbf( 0, TEXT_LUMA     ) == 0 &&
1113              rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) == 0 &&
1114              rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) == 0 
1115              )
1116            { 
1117              bOnlyIVP = true;
1118              bUseIVP = rpcBestCU->getSlice()->getIntraContourFlag();
1119            }
1120            if( bUseIVP )
1121            {
1122              xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N, bOnlyIVP );
1123#else
1124          xCheckRDCostIntra( rpcBestCU, rpcTempCU, intraCost, SIZE_2Nx2N DEBUG_STRING_PASS_INTO(sDebug) );
1125#endif
1126#if KWU_RC_MADPRED_E0227
1127            if ( uiDepth <= m_addSADDepth )
1128            {
1129              m_LCUPredictionSAD += m_spatialSAD;
1130              m_addSADDepth = uiDepth;
1131            }
1132#endif
1133
1134          rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1135          if( uiDepth == sps.getLog2DiffMaxMinCodingBlockSize() )
1136          {
1137#if NH_3D_QTLPC //Try IntraNxN
1138              if(bTrySplit)
1139              {
1140#endif
1141            if( rpcTempCU->getWidth(0) > ( 1 << sps.getQuadtreeTULog2MinSize() ) )
1142            {
1143              Double tmpIntraCost;
1144#if H_3D_DIM
1145              xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN, bOnlyIVP );
1146#else
1147              xCheckRDCostIntra( rpcBestCU, rpcTempCU, tmpIntraCost, SIZE_NxN DEBUG_STRING_PASS_INTO(sDebug)   );
1148#endif
1149
1150              intraCost = std::min(intraCost, tmpIntraCost);
1151              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1152            }
1153#if NH_3D_QTLPC
1154              }
1155#endif
1156          }
1157#if H_3D
1158          }
1159#endif
1160        }
1161
1162        // test PCM
1163        if(sps.getUsePCM()
1164          && rpcTempCU->getWidth(0) <= (1<<sps.getPCMLog2MaxSize())
1165          && rpcTempCU->getWidth(0) >= (1<<sps.getPCMLog2MinSize()) )
1166        {
1167          UInt uiRawBits = getTotalBits(rpcBestCU->getWidth(0), rpcBestCU->getHeight(0), rpcBestCU->getPic()->getChromaFormat(), sps.getBitDepths().recon);
1168          UInt uiBestBits = rpcBestCU->getTotalBits();
1169#if NH_3D_VSO // M7
1170          Double dRDCostTemp = m_pcRdCost->getUseLambdaScaleVSO() ? m_pcRdCost->calcRdCostVSO(uiRawBits, 0) : m_pcRdCost->calcRdCost(uiRawBits, 0);
1171          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > dRDCostTemp ))
1172#else
1173          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
1174#endif
1175
1176          {
1177            xCheckIntraPCM (rpcBestCU, rpcTempCU);
1178            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1179          }
1180        }
1181#if  H_3D_FAST_TEXTURE_ENCODING
1182        }
1183#endif
1184        if (bIsLosslessMode) // Restore loop variable if lossless mode was searched.
1185        {
1186          iQP = iMinQP;
1187        }
1188      }
1189    }
1190
1191    m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
1192    m_pcEntropyCoder->resetBits();
1193    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
1194    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1195    rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1196#if NH_3D_VSO // M8
1197    if ( m_pcRdCost->getUseVSO() )   
1198    {
1199      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );   
1200    }
1201    else
1202#endif
1203      rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
1204    m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
1205
1206    // Early CU determination
1207    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
1208    {
1209      bSubBranch = false;
1210    }
1211    else
1212    {
1213      bSubBranch = true;
1214    }
1215#if  H_3D_FAST_TEXTURE_ENCODING
1216    if(rpcBestCU->getSlice()->getViewIndex() && !rpcBestCU->getSlice()->getIsDepth() && (uiDepth >=iIVFMaxD) && rpcBestCU->isSkipped(0))
1217    {
1218      bSubBranch = false;
1219    }
1220#endif
1221  }
1222  else
1223  {
1224    bBoundary = true;
1225  }
1226
1227  // copy orginal YUV samples to PCM buffer
1228  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
1229  {
1230    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
1231  }
1232
1233  if( uiDepth == pps.getMaxCuDQPDepth() )
1234  {
1235    Int idQP = m_pcEncCfg->getMaxDeltaQP();
1236    iMinQP = Clip3( -sps.getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP-idQP );
1237    iMaxQP = Clip3( -sps.getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP+idQP );
1238  }
1239  else if( uiDepth < pps.getMaxCuDQPDepth() )
1240  {
1241    iMinQP = iBaseQP;
1242    iMaxQP = iBaseQP;
1243  }
1244  else
1245  {
1246    const Int iStartQP = rpcTempCU->getQP(0);
1247    iMinQP = iStartQP;
1248    iMaxQP = iStartQP;
1249  }
1250
1251  if ( m_pcEncCfg->getUseRateCtrl() )
1252  {
1253    iMinQP = m_pcRateCtrl->getRCQP();
1254    iMaxQP = m_pcRateCtrl->getRCQP();
1255  }
1256
1257  if ( m_pcEncCfg->getCUTransquantBypassFlagForceValue() )
1258  {
1259    iMaxQP = iMinQP; // If all TUs are forced into using transquant bypass, do not loop here.
1260  }
1261
1262  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
1263  {
1264    const Bool bIsLosslessMode = false; // False at this level. Next level down may set it to true.
1265
1266    rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1267
1268    // further split
1269#if NH_3D_QTLPC
1270
1271    if( bSubBranch && bTrySplitDQP && uiDepth < sps.getLog2DiffMaxMinCodingBlockSize() )
1272#else
1273    if( bSubBranch && uiDepth < sps.getLog2DiffMaxMinCodingBlockSize() )
1274#endif
1275    {
1276#if NH_3D_VSO // M9
1277      // reset Model
1278      if( m_pcRdCost->getUseRenModel() )
1279      {
1280        UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth ( COMPONENT_Y );
1281        UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight( COMPONENT_Y );
1282        Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getAddr  ( COMPONENT_Y, 0 );
1283        UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride( COMPONENT_Y  );
1284        m_pcRdCost->setRenModelData( m_ppcBestCU[uiDepth], 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1285      }
1286#endif
1287      UChar       uhNextDepth         = uiDepth+1;
1288      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
1289      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
1290      DEBUG_STRING_NEW(sTempDebug)
1291
1292#if NH_3D_ARP
1293      m_ppcWeightedTempCU[uhNextDepth]->setSlice( m_ppcWeightedTempCU[ uiDepth]->getSlice()); 
1294      m_ppcWeightedTempCU[uhNextDepth]->setPic  ( m_ppcWeightedTempCU[ uiDepth] ); 
1295#endif
1296      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
1297      {
1298        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1299        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
1300
1301        if( ( pcSubBestPartCU->getCUPelX() < sps.getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < sps.getPicHeightInLumaSamples() ) )
1302        {
1303          if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
1304          {
1305            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1306          }
1307          else
1308          {
1309            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
1310          }
1311
1312#if AMP_ENC_SPEEDUP
1313          DEBUG_STRING_NEW(sChild)
1314          if ( !rpcBestCU->isInter(0) )
1315          {
1316            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth DEBUG_STRING_PASS_INTO(sChild), NUMBER_OF_PART_SIZES );
1317          }
1318          else
1319          {
1320
1321            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth DEBUG_STRING_PASS_INTO(sChild), rpcBestCU->getPartitionSize(0) );
1322          }
1323          DEBUG_STRING_APPEND(sTempDebug, sChild)
1324#else
1325          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
1326#endif
1327
1328          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
1329          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
1330        }
1331        else
1332        {
1333          pcSubBestPartCU->copyToPic( uhNextDepth );
1334          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
1335        }
1336      }
1337
1338      m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
1339      if( !bBoundary )
1340      {
1341        m_pcEntropyCoder->resetBits();
1342        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
1343
1344        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
1345        rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1346      }
1347#if NH_3D_VSO // M10
1348      if ( m_pcRdCost->getUseVSO() )
1349      {
1350        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1351      }
1352      else
1353#endif
1354        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1355
1356      if( uiDepth == pps.getMaxCuDQPDepth() && pps.getUseDQP())
1357      {
1358        Bool hasResidual = false;
1359        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
1360        {
1361          if( (     rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Y)
1362                || (rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Cb) && (numberValidComponents > COMPONENT_Cb))
1363                || (rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Cr) && (numberValidComponents > COMPONENT_Cr)) ) )
1364          {
1365            hasResidual = true;
1366            break;
1367          }
1368        }
1369
1370        UInt uiTargetPartIdx = 0;
1371        if ( hasResidual )
1372        {
1373          m_pcEntropyCoder->resetBits();
1374          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
1375          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1376          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1377#if NH_3D_VSO // M11
1378          if ( m_pcRdCost->getUseLambdaScaleVSO())         
1379          {
1380            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );         
1381          }
1382          else
1383#endif
1384            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1385
1386          Bool foundNonZeroCbf = false;
1387          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth, foundNonZeroCbf );
1388          assert( foundNonZeroCbf );
1389        }
1390        else
1391        {
1392          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
1393        }
1394      }
1395
1396      m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1397
1398      // If the configuration being tested exceeds the maximum number of bytes for a slice / slice-segment, then
1399      // a proper RD evaluation cannot be performed. Therefore, termination of the
1400      // slice/slice-segment must be made prior to this CTU.
1401      // This can be achieved by forcing the decision to be that of the rpcTempCU.
1402      // The exception is each slice / slice-segment must have at least one CTU.
1403      const Bool isEndOfSlice        =    pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES
1404                                       && ((pcSlice->getSliceBits()+rpcBestCU->getTotalBits())>pcSlice->getSliceArgument()<<3)
1405                                       && rpcBestCU->getCtuRsAddr() != pcPic->getPicSym()->getCtuTsToRsAddrMap(pcSlice->getSliceCurStartCtuTsAddr())
1406                                       && rpcBestCU->getCtuRsAddr() != pcPic->getPicSym()->getCtuTsToRsAddrMap(pcSlice->getSliceSegmentCurStartCtuTsAddr());
1407      const Bool isEndOfSliceSegment =    pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
1408                                       && ((pcSlice->getSliceSegmentBits()+rpcBestCU->getTotalBits()) > pcSlice->getSliceSegmentArgument()<<3)
1409                                       && rpcBestCU->getCtuRsAddr() != pcPic->getPicSym()->getCtuTsToRsAddrMap(pcSlice->getSliceSegmentCurStartCtuTsAddr());
1410                                           // Do not need to check slice condition for slice-segment since a slice-segment is a subset of a slice.
1411      if(isEndOfSlice||isEndOfSliceSegment)
1412      {
1413        rpcBestCU->getTotalCost()=MAX_DOUBLE;
1414      }
1415
1416      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTempDebug) DEBUG_STRING_PASS_INTO(false) ); // RD compare current larger prediction
1417                                                                                       // with sub partitioned prediction.
1418    }
1419  }
1420#if NH_3D_VSO // M12
1421  if( m_pcRdCost->getUseRenModel() )
1422  {
1423    UInt  uiWidth     = m_ppcRecoYuvBest[uiDepth]->getWidth   ( COMPONENT_Y );
1424    UInt  uiHeight    = m_ppcRecoYuvBest[uiDepth]->getHeight  ( COMPONENT_Y );
1425    Pel*  piSrc       = m_ppcRecoYuvBest[uiDepth]->getAddr    ( COMPONENT_Y,  0 );
1426    UInt  uiSrcStride = m_ppcRecoYuvBest[uiDepth]->getStride  ( COMPONENT_Y );
1427    m_pcRdCost->setRenModelData( rpcBestCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1428  }
1429#endif
1430
1431  DEBUG_STRING_APPEND(sDebug_, sDebug);
1432
1433  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
1434
1435  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getCtuRsAddr(), rpcBestCU->getZorderIdxInCtu(), uiDepth, uiDepth );   // Copy Yuv data to picture Yuv
1436  if (bBoundary)
1437  {
1438    return;
1439  }
1440
1441  // Assert if Best prediction mode is NONE
1442  // Selected mode's RD-cost must be not MAX_DOUBLE.
1443  assert( rpcBestCU->getPartitionSize ( 0 ) != NUMBER_OF_PART_SIZES       );
1444  assert( rpcBestCU->getPredictionMode( 0 ) != NUMBER_OF_PREDICTION_MODES );
1445  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE                 );
1446}
1447
1448/** finish encoding a cu and handle end-of-slice conditions
1449 * \param pcCU
1450 * \param uiAbsPartIdx
1451 * \param uiDepth
1452 * \returns Void
1453 */
1454Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx )
1455{
1456  TComPic* pcPic = pcCU->getPic();
1457  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
1458
1459  //Calculate end address
1460  const Int  currentCTUTsAddr = pcPic->getPicSym()->getCtuRsToTsAddrMap(pcCU->getCtuRsAddr());
1461  const Bool isLastSubCUOfCtu = pcCU->isLastSubCUOfCtu(uiAbsPartIdx);
1462  if ( isLastSubCUOfCtu )
1463  {
1464    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1465    // i.e. when the slice segment CurEnd CTU address is the current CTU address+1.
1466    if (pcSlice->getSliceSegmentCurEndCtuTsAddr() != currentCTUTsAddr+1)
1467    {
1468      m_pcEntropyCoder->encodeTerminatingBit( 0 );
1469    }
1470  }
1471}
1472
1473/** Compute QP for each CU
1474 * \param pcCU Target CU
1475 * \param uiDepth CU depth
1476 * \returns quantization parameter
1477 */
1478Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1479{
1480  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1481  Int iQpOffset = 0;
1482  if ( m_pcEncCfg->getUseAdaptiveQP() )
1483  {
1484    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1485    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1486    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1487    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1488    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1489    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1490    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1491
1492    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1493    Double dAvgAct = pcAQLayer->getAvgActivity();
1494    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1495    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1496    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1497    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1498  }
1499
1500  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQp+iQpOffset );
1501}
1502
1503/** encode a CU block recursively
1504 * \param pcCU
1505 * \param uiAbsPartIdx
1506 * \param uiDepth
1507 * \returns Void
1508 */
1509Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1510{
1511        TComPic   *const pcPic   = pcCU->getPic();
1512        TComSlice *const pcSlice = pcCU->getSlice();
1513  const TComSPS   &sps =*(pcSlice->getSPS());
1514  const TComPPS   &pps =*(pcSlice->getPPS());
1515
1516  const UInt maxCUWidth  = sps.getMaxCUWidth();
1517  const UInt maxCUHeight = sps.getMaxCUHeight();
1518
1519        Bool bBoundary = false;
1520        UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1521  const UInt uiRPelX   = uiLPelX + (maxCUWidth>>uiDepth)  - 1;
1522        UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1523  const UInt uiBPelY   = uiTPelY + (maxCUHeight>>uiDepth) - 1;
1524
1525#if H_MV_ENC_DEC_TRAC
1526  DTRACE_CU_S("=========== coding_quadtree ===========\n")
1527  DTRACE_CU("x0", uiLPelX)
1528  DTRACE_CU("x1", uiTPelY)
1529  DTRACE_CU("log2CbSize", maxCUWidth>>uiDepth )
1530  DTRACE_CU("cqtDepth"  , uiDepth)
1531#endif
1532
1533  if( ( uiRPelX < sps.getPicWidthInLumaSamples() ) && ( uiBPelY < sps.getPicHeightInLumaSamples() ) )
1534  {
1535    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1536  }
1537  else
1538  {
1539    bBoundary = true;
1540  }
1541
1542  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < sps.getLog2DiffMaxMinCodingBlockSize() ) ) || bBoundary )
1543  {
1544    UInt uiQNumParts = ( pcPic->getNumPartitionsInCtu() >> (uiDepth<<1) )>>2;
1545    if( uiDepth == pps.getMaxCuDQPDepth() && pps.getUseDQP())
1546    {
1547      setdQPFlag(true);
1548    }
1549
1550    if( uiDepth == pps.getPpsRangeExtension().getDiffCuChromaQpOffsetDepth() && pcSlice->getUseChromaQpAdj())
1551    {
1552      setCodeChromaQpAdjFlag(true);
1553    }
1554
1555    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1556    {
1557      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1558      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1559      if( ( uiLPelX < sps.getPicWidthInLumaSamples() ) && ( uiTPelY < sps.getPicHeightInLumaSamples() ) )
1560      {
1561        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1562      }
1563    }
1564    return;
1565  }
1566
1567#if H_MV_ENC_DEC_TRAC
1568  DTRACE_CU_S("=========== coding_unit ===========\n")
1569#endif
1570
1571
1572  if( uiDepth <= pps.getMaxCuDQPDepth() && pps.getUseDQP())
1573  {
1574    setdQPFlag(true);
1575  }
1576
1577  if( uiDepth <= pps.getPpsRangeExtension().getDiffCuChromaQpOffsetDepth() && pcSlice->getUseChromaQpAdj())
1578  {
1579    setCodeChromaQpAdjFlag(true);
1580  }
1581
1582  if (pps.getTransquantBypassEnableFlag())
1583  {
1584    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1585  }
1586
1587  if( !pcSlice->isIntra() )
1588  {
1589    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1590  }
1591
1592  if( pcCU->isSkipped( uiAbsPartIdx ) )
1593  {
1594#if H_MV_ENC_DEC_TRAC
1595    DTRACE_PU_S("=========== prediction_unit ===========\n")
1596    DTRACE_PU("x0", uiLPelX)
1597    DTRACE_PU("x1", uiTPelY)
1598#endif
1599
1600    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1601#if NH_3D_ARP
1602    m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1603#endif
1604#if NH_3D_IC
1605    m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1606#endif
1607
1608    finishCU(pcCU,uiAbsPartIdx);
1609    return;
1610  }
1611
1612#if H_3D
1613  m_pcEntropyCoder->encodeDIS( pcCU, uiAbsPartIdx );
1614  if(!pcCU->getDISFlag(uiAbsPartIdx))
1615  {
1616#endif
1617  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1618  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1619
1620  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1621  {
1622    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1623
1624    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1625    {
1626#if H_3D_DIM_SDC
1627      m_pcEntropyCoder->encodeSDCFlag( pcCU, uiAbsPartIdx, false );
1628#endif 
1629
1630      // Encode slice finish
1631      finishCU(pcCU,uiAbsPartIdx);
1632      return;
1633    }
1634  }
1635
1636  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1637  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1638#if NH_3D_DBBP
1639  m_pcEntropyCoder->encodeDBBPFlag( pcCU, uiAbsPartIdx );
1640#endif
1641#if H_3D_DIM_SDC
1642  m_pcEntropyCoder->encodeSDCFlag( pcCU, uiAbsPartIdx, false );
1643#endif 
1644#if NH_3D_ARP
1645  m_pcEntropyCoder->encodeARPW( pcCU , uiAbsPartIdx );
1646#endif
1647#if NH_3D_IC
1648  m_pcEntropyCoder->encodeICFlag  ( pcCU, uiAbsPartIdx );
1649#endif
1650
1651  // Encode Coefficients
1652  Bool bCodeDQP = getdQPFlag();
1653  Bool codeChromaQpAdj = getCodeChromaQpAdjFlag();
1654  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, bCodeDQP, codeChromaQpAdj );
1655  setCodeChromaQpAdjFlag( codeChromaQpAdj );
1656  setdQPFlag( bCodeDQP );
1657#if H_3D
1658  }
1659#endif
1660
1661
1662  // --- write terminating bit ---
1663  finishCU(pcCU,uiAbsPartIdx);
1664}
1665
1666Int xCalcHADs8x8_ISlice(Pel *piOrg, Int iStrideOrg)
1667{
1668  Int k, i, j, jj;
1669  Int diff[64], m1[8][8], m2[8][8], m3[8][8], iSumHad = 0;
1670
1671  for( k = 0; k < 64; k += 8 )
1672  {
1673    diff[k+0] = piOrg[0] ;
1674    diff[k+1] = piOrg[1] ;
1675    diff[k+2] = piOrg[2] ;
1676    diff[k+3] = piOrg[3] ;
1677    diff[k+4] = piOrg[4] ;
1678    diff[k+5] = piOrg[5] ;
1679    diff[k+6] = piOrg[6] ;
1680    diff[k+7] = piOrg[7] ;
1681
1682    piOrg += iStrideOrg;
1683  }
1684
1685  //horizontal
1686  for (j=0; j < 8; j++)
1687  {
1688    jj = j << 3;
1689    m2[j][0] = diff[jj  ] + diff[jj+4];
1690    m2[j][1] = diff[jj+1] + diff[jj+5];
1691    m2[j][2] = diff[jj+2] + diff[jj+6];
1692    m2[j][3] = diff[jj+3] + diff[jj+7];
1693    m2[j][4] = diff[jj  ] - diff[jj+4];
1694    m2[j][5] = diff[jj+1] - diff[jj+5];
1695    m2[j][6] = diff[jj+2] - diff[jj+6];
1696    m2[j][7] = diff[jj+3] - diff[jj+7];
1697
1698    m1[j][0] = m2[j][0] + m2[j][2];
1699    m1[j][1] = m2[j][1] + m2[j][3];
1700    m1[j][2] = m2[j][0] - m2[j][2];
1701    m1[j][3] = m2[j][1] - m2[j][3];
1702    m1[j][4] = m2[j][4] + m2[j][6];
1703    m1[j][5] = m2[j][5] + m2[j][7];
1704    m1[j][6] = m2[j][4] - m2[j][6];
1705    m1[j][7] = m2[j][5] - m2[j][7];
1706
1707    m2[j][0] = m1[j][0] + m1[j][1];
1708    m2[j][1] = m1[j][0] - m1[j][1];
1709    m2[j][2] = m1[j][2] + m1[j][3];
1710    m2[j][3] = m1[j][2] - m1[j][3];
1711    m2[j][4] = m1[j][4] + m1[j][5];
1712    m2[j][5] = m1[j][4] - m1[j][5];
1713    m2[j][6] = m1[j][6] + m1[j][7];
1714    m2[j][7] = m1[j][6] - m1[j][7];
1715  }
1716
1717  //vertical
1718  for (i=0; i < 8; i++)
1719  {
1720    m3[0][i] = m2[0][i] + m2[4][i];
1721    m3[1][i] = m2[1][i] + m2[5][i];
1722    m3[2][i] = m2[2][i] + m2[6][i];
1723    m3[3][i] = m2[3][i] + m2[7][i];
1724    m3[4][i] = m2[0][i] - m2[4][i];
1725    m3[5][i] = m2[1][i] - m2[5][i];
1726    m3[6][i] = m2[2][i] - m2[6][i];
1727    m3[7][i] = m2[3][i] - m2[7][i];
1728
1729    m1[0][i] = m3[0][i] + m3[2][i];
1730    m1[1][i] = m3[1][i] + m3[3][i];
1731    m1[2][i] = m3[0][i] - m3[2][i];
1732    m1[3][i] = m3[1][i] - m3[3][i];
1733    m1[4][i] = m3[4][i] + m3[6][i];
1734    m1[5][i] = m3[5][i] + m3[7][i];
1735    m1[6][i] = m3[4][i] - m3[6][i];
1736    m1[7][i] = m3[5][i] - m3[7][i];
1737
1738    m2[0][i] = m1[0][i] + m1[1][i];
1739    m2[1][i] = m1[0][i] - m1[1][i];
1740    m2[2][i] = m1[2][i] + m1[3][i];
1741    m2[3][i] = m1[2][i] - m1[3][i];
1742    m2[4][i] = m1[4][i] + m1[5][i];
1743    m2[5][i] = m1[4][i] - m1[5][i];
1744    m2[6][i] = m1[6][i] + m1[7][i];
1745    m2[7][i] = m1[6][i] - m1[7][i];
1746  }
1747
1748  for (i = 0; i < 8; i++)
1749  {
1750    for (j = 0; j < 8; j++)
1751    {
1752      iSumHad += abs(m2[i][j]);
1753    }
1754  }
1755  iSumHad -= abs(m2[0][0]);
1756  iSumHad =(iSumHad+2)>>2;
1757  return(iSumHad);
1758}
1759
1760Int  TEncCu::updateCtuDataISlice(TComDataCU* pCtu, Int width, Int height)
1761{
1762  Int  xBl, yBl;
1763  const Int iBlkSize = 8;
1764
1765  Pel* pOrgInit   = pCtu->getPic()->getPicYuvOrg()->getAddr(COMPONENT_Y, pCtu->getCtuRsAddr(), 0);
1766  Int  iStrideOrig = pCtu->getPic()->getPicYuvOrg()->getStride(COMPONENT_Y);
1767  Pel  *pOrg;
1768
1769  Int iSumHad = 0;
1770  for ( yBl=0; (yBl+iBlkSize)<=height; yBl+= iBlkSize)
1771  {
1772    for ( xBl=0; (xBl+iBlkSize)<=width; xBl+= iBlkSize)
1773    {
1774      pOrg = pOrgInit + iStrideOrig*yBl + xBl;
1775      iSumHad += xCalcHADs8x8_ISlice(pOrg, iStrideOrig);
1776    }
1777  }
1778  return(iSumHad);
1779}
1780
1781/** check RD costs for a CU block encoded with merge
1782 * \param rpcBestCU
1783 * \param rpcTempCU
1784 * \param earlyDetectionSkipMode
1785 */
1786Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU DEBUG_STRING_FN_DECLARE(sDebug), Bool *earlyDetectionSkipMode )
1787{
1788  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1789#if NH_3D_MLC
1790  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS_MEM << 1]; // double length for mv of both lists
1791  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS_MEM];
1792#else
1793  TComMvField  cMvFieldNeighbours[2 * MRG_MAX_NUM_CANDS]; // double length for mv of both lists
1794  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1795#endif
1796  Int numValidMergeCand = 0;
1797  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
1798
1799  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1800  {
1801    uhInterDirNeighbours[ui] = 0;
1802  }
1803  UChar uhDepth = rpcTempCU->getDepth( 0 );
1804#if NH_3D_IC
1805  Bool bICFlag = rpcTempCU->getICFlag( 0 );
1806#endif
1807#if NH_3D_VSO // M1  //necessary here?
1808  if( m_pcRdCost->getUseRenModel() )
1809  {
1810    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( COMPONENT_Y );
1811    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( COMPONENT_Y );
1812    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getAddr  ( COMPONENT_Y );
1813    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride( COMPONENT_Y );
1814    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
1815  }
1816#endif
1817
1818#if NH_3D_ARP
1819  DisInfo cOrigDisInfo = rpcTempCU->getDvInfo(0);
1820#else
1821#endif
1822
1823  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to CTU level
1824
1825#if NH_3D_SPIVMP
1826  Bool bSPIVMPFlag[MRG_MAX_NUM_CANDS_MEM];
1827  memset(bSPIVMPFlag, false, sizeof(Bool)*MRG_MAX_NUM_CANDS_MEM);
1828  TComMvField*  pcMvFieldSP;
1829  UChar* puhInterDirSP;
1830  pcMvFieldSP = new TComMvField[rpcTempCU->getPic()->getPicSym()->getNumPartitionsInCtu()*2]; 
1831  puhInterDirSP = new UChar[rpcTempCU->getPic()->getPicSym()->getNumPartitionsInCtu()]; 
1832#endif
1833
1834#if NH_3D_VSP
1835#if !NH_3D_ARP
1836  Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1837  memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1838#if NH_3D_MLC
1839  rpcTempCU->initAvailableFlags();
1840#endif
1841  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1842#if NH_3D_MLC
1843  rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours
1844#if NH_3D_SPIVMP
1845    , pcMvFieldSP, puhInterDirSP
1846#endif
1847    , numValidMergeCand
1848    );
1849
1850  rpcTempCU->buildMCL( cMvFieldNeighbours,uhInterDirNeighbours, vspFlag
1851#if NH_3D_SPIVMP
1852    , bSPIVMPFlag
1853#endif
1854    , numValidMergeCand
1855    );
1856#endif
1857#endif
1858#else
1859#if NH_3D_MLC
1860  rpcTempCU->initAvailableFlags();
1861#endif
1862  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1863#if NH_3D_MLC
1864  rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours
1865#if H_3D_SPIVMP
1866    , pcMvFieldSP, puhInterDirSP
1867#endif
1868    , numValidMergeCand
1869    );
1870#if NH_3D_MLC
1871  rpcTempCU->buildMCL( cMvFieldNeighbours,uhInterDirNeighbours
1872#if H_3D_SPIVMP
1873    , bSPIVMPFlag
1874#endif
1875    , numValidMergeCand
1876    );
1877#endif
1878#endif
1879#endif
1880
1881#if NH_3D_MLC
1882  Int mergeCandBuffer[MRG_MAX_NUM_CANDS_MEM];
1883#else
1884  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1885#endif
1886#if NH_3D_MLC
1887  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1888#else
1889  for( UInt ui = 0; ui < numValidMergeCand; ++ui )
1890#endif
1891  {
1892    mergeCandBuffer[ui] = 0;
1893  }
1894
1895  Bool bestIsSkip = false;
1896
1897  UInt iteration;
1898  if ( rpcTempCU->isLosslessCoded(0))
1899  {
1900    iteration = 1;
1901  }
1902  else
1903  {
1904    iteration = 2;
1905  }
1906  DEBUG_STRING_NEW(bestStr)
1907
1908#if NH_3D_ARP
1909  Int nARPWMax = rpcTempCU->getSlice()->getARPStepNum() - 1;
1910#if NH_3D_IC
1911  if( nARPWMax < 0 || bICFlag )
1912#else
1913  if( nARPWMax < 0 )
1914#endif
1915  {
1916    nARPWMax = 0;
1917  }
1918  for( Int nARPW=nARPWMax; nARPW >= 0 ; nARPW-- )
1919  {
1920#if NH_3D
1921#if DEBUG_STRING
1922    bestStr.clear(); 
1923#endif
1924#endif
1925#if NH_3D_IV_MERGE
1926    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS_MEM*sizeof(Int) );
1927#else
1928    memset( mergeCandBuffer, 0, MRG_MAX_NUM_CANDS * sizeof(Int) );
1929#endif
1930    rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
1931    rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
1932#if NH_3D_IC
1933    rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1934#endif
1935    rpcTempCU->getDvInfo(0) = cOrigDisInfo;
1936    rpcTempCU->setDvInfoSubParts(cOrigDisInfo, 0, uhDepth );
1937#if NH_3D_VSP
1938    Int vspFlag[MRG_MAX_NUM_CANDS_MEM];
1939    memset(vspFlag, 0, sizeof(Int)*MRG_MAX_NUM_CANDS_MEM);
1940#endif
1941#if NH_3D
1942#if NH_3D_MLC
1943    rpcTempCU->initAvailableFlags();
1944#endif
1945    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
1946    rpcTempCU->xGetInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours
1947#if NH_3D_SPIVMP
1948      , pcMvFieldSP, puhInterDirSP
1949#endif
1950      , numValidMergeCand
1951      );
1952
1953    rpcTempCU->buildMCL( cMvFieldNeighbours,uhInterDirNeighbours
1954#if NH_3D_VSP
1955      , vspFlag
1956#endif
1957#if NH_3D_SPIVMP
1958      , bSPIVMPFlag
1959#endif
1960      , numValidMergeCand
1961      );
1962
1963#else
1964    rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1965#endif
1966
1967
1968#endif
1969
1970  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1971  {
1972    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1973    {
1974#if NH_3D_IC
1975      if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
1976      {
1977        if( bICFlag && uiMergeCand == 0 ) 
1978        {
1979          continue;
1980        }
1981      }
1982#endif
1983
1984      if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1985      {
1986        if( !(bestIsSkip && uiNoResidual == 0) )
1987        {
1988          DEBUG_STRING_NEW(tmpStr)
1989          // set MC parameters
1990          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to CTU level
1991#if NH_3D_IC
1992          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
1993#endif
1994          rpcTempCU->setCUTransquantBypassSubParts( bTransquantBypassFlag, 0, uhDepth );
1995          rpcTempCU->setChromaQpAdjSubParts( bTransquantBypassFlag ? 0 : m_cuChromaQpOffsetIdxPlus1, 0, uhDepth );
1996          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to CTU level
1997          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to CTU level
1998          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to CTU level
1999#if NH_3D_ARP
2000          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2001#endif
2002
2003#if NH_3D_VSP
2004          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
2005#endif
2006#if NH_3D_SPIVMP
2007          rpcTempCU->setSPIVMPFlagSubParts(bSPIVMPFlag[uiMergeCand], 0, 0, uhDepth);
2008          if (bSPIVMPFlag[uiMergeCand])
2009          {
2010            UInt uiSPAddr;
2011            Int iWidth = rpcTempCU->getWidth(0);
2012            Int iHeight = rpcTempCU->getHeight(0);
2013            Int iNumSPInOneLine, iNumSP, iSPWidth, iSPHeight;
2014            rpcTempCU->getSPPara(iWidth, iHeight, iNumSP, iNumSPInOneLine, iSPWidth, iSPHeight);
2015            for (Int iPartitionIdx = 0; iPartitionIdx < iNumSP; iPartitionIdx++)
2016            {
2017              rpcTempCU->getSPAbsPartIdx(0, iSPWidth, iSPHeight, iPartitionIdx, iNumSPInOneLine, uiSPAddr);
2018              rpcTempCU->setInterDirSP(puhInterDirSP[iPartitionIdx], uiSPAddr, iSPWidth, iSPHeight);
2019              rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx], iSPWidth, iSPHeight);
2020              rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx + 1], iSPWidth, iSPHeight);
2021            }
2022          }
2023          else
2024#endif
2025          {
2026#if NH_3D_VSP
2027            if ( vspFlag[uiMergeCand] )
2028            {
2029              UInt partAddr;
2030              Int vspSize;
2031              Int width, height;
2032              rpcTempCU->getPartIndexAndSize( 0, partAddr, width, height );
2033              if( uhInterDirNeighbours[ uiMergeCand ] & 0x01 )
2034              {
2035                rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_0, cMvFieldNeighbours[ 2*uiMergeCand + 0 ].getRefIdx(), vspSize );
2036                rpcTempCU->setVSPFlag( partAddr, vspSize );
2037              }
2038              else
2039              {
2040                rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
2041              }
2042              if( uhInterDirNeighbours[ uiMergeCand ] & 0x02 )
2043              {
2044                rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_1 , cMvFieldNeighbours[ 2*uiMergeCand + 1 ].getRefIdx(), vspSize );
2045                rpcTempCU->setVSPFlag( partAddr, vspSize );
2046              }
2047              else
2048              {
2049                rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
2050              }
2051              rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
2052            }
2053            else
2054            {
2055#endif
2056            rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to CTU level
2057            rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
2058            rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
2059#if NH_3D_VSP
2060            }
2061#endif
2062          }
2063          // do MC
2064          m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
2065          // estimate residual and encode everything
2066#if NH_3D_VSO //M2
2067          if( m_pcRdCost->getUseRenModel() )
2068          { //Reset
2069            UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ( COMPONENT_Y );
2070            UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ( COMPONENT_Y );
2071            Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getAddr     ( COMPONENT_Y );
2072            UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ( COMPONENT_Y );
2073            m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2074          }
2075#endif
2076          m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
2077                                                     m_ppcOrigYuv    [uhDepth],
2078                                                     m_ppcPredYuvTemp[uhDepth],
2079                                                     m_ppcResiYuvTemp[uhDepth],
2080                                                     m_ppcResiYuvBest[uhDepth],
2081                                                     m_ppcRecoYuvTemp[uhDepth],
2082                                                     (uiNoResidual != 0) DEBUG_STRING_PASS_INTO(tmpStr) );
2083
2084#if DEBUG_STRING
2085          DebugInterPredResiReco(tmpStr, *(m_ppcPredYuvTemp[uhDepth]), *(m_ppcResiYuvBest[uhDepth]), *(m_ppcRecoYuvTemp[uhDepth]), DebugStringGetPredModeMask(rpcTempCU->getPredictionMode(0)));
2086#endif
2087
2088          if ((uiNoResidual == 0) && (rpcTempCU->getQtRootCbf(0) == 0))
2089          {
2090            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
2091            mergeCandBuffer[uiMergeCand] = 1;
2092          }
2093#if H_3D
2094          rpcTempCU->setDISFlagSubParts( false, 0, uhDepth );
2095#endif
2096#if NH_3D_VSP
2097          if( rpcTempCU->getSkipFlag(0) )
2098          {
2099            rpcTempCU->setTrIdxSubParts(0, 0, uhDepth);
2100          }
2101#endif
2102#if H_3D_INTER_SDC
2103          TComDataCU *rpcTempCUPre = rpcTempCU;
2104#endif
2105          Int orgQP = rpcTempCU->getQP( 0 );
2106          xCheckDQP( rpcTempCU );
2107          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth DEBUG_STRING_PASS_INTO(bestStr) DEBUG_STRING_PASS_INTO(tmpStr));
2108#if H_3D_INTER_SDC
2109          if( rpcTempCU->getSlice()->getInterSdcFlag() && !uiNoResidual )
2110          {
2111            Double dOffsetCost[3] = {MAX_DOUBLE,MAX_DOUBLE,MAX_DOUBLE};
2112            for( Int uiOffest = 1 ; uiOffest <= 5 ; uiOffest++ )
2113            {
2114              if( uiOffest > 3)
2115              {
2116                if ( dOffsetCost[0] < (0.9*dOffsetCost[1]) && dOffsetCost[0] < (0.9*dOffsetCost[2]) )
2117                {
2118                  continue;
2119                }
2120                if ( dOffsetCost[1] < dOffsetCost[0] && dOffsetCost[0] < dOffsetCost[2] &&  uiOffest == 5)
2121                {
2122                  continue;
2123                }
2124                if ( dOffsetCost[0] < dOffsetCost[1] && dOffsetCost[2] < dOffsetCost[0] &&  uiOffest == 4)
2125                {
2126                  continue;
2127                }
2128              }
2129              if( rpcTempCU != rpcTempCUPre )
2130              {
2131                rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag  );
2132                rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
2133              }
2134              rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2135#if H_3D
2136              rpcTempCU->setDISFlagSubParts( false, 0, uhDepth );
2137#endif
2138              rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
2139              rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
2140#if NH_3D_VSO //M2
2141              if( m_pcRdCost->getUseRenModel() )
2142              { //Reset
2143                UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ( COMPONENT_Y );
2144                UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ( COMPONENT_Y );
2145                Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getAddr     ( COMPONENT_Y );
2146                UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ( COMPONENT_Y );
2147                m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2148              }
2149#endif
2150              Int iSdcOffset = 0;
2151              if(uiOffest % 2 == 0)
2152              {
2153                iSdcOffset = uiOffest >> 1;
2154              }
2155              else
2156              {
2157                iSdcOffset = -1 * (uiOffest >> 1);
2158              }
2159              m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2160                m_ppcOrigYuv[uhDepth], 
2161                ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth], 
2162                m_ppcResiYuvTemp[uhDepth], 
2163                m_ppcRecoYuvTemp[uhDepth],
2164                iSdcOffset,
2165                uhDepth );
2166              if (uiOffest <= 3 )
2167              {
2168                dOffsetCost [uiOffest -1] = rpcTempCU->getTotalCost();
2169              }
2170
2171              xCheckDQP( rpcTempCU );
2172              xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
2173            }
2174          }
2175#endif
2176
2177          rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag );
2178
2179          if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
2180          {
2181#if H_3D_INTER_SDC
2182            if( rpcTempCU->getSlice()->getInterSdcFlag() )
2183            {
2184              bestIsSkip = !rpcBestCU->getSDCFlag( 0 ) && ( rpcBestCU->getQtRootCbf(0) == 0 );
2185            }
2186            else
2187            {
2188#endif
2189            bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
2190#if H_3D_INTER_SDC
2191            }
2192#endif
2193          }
2194        }
2195      }
2196    }
2197
2198    if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
2199    {
2200      if(rpcBestCU->getQtRootCbf( 0 ) == 0)
2201      {
2202        if( rpcBestCU->getMergeFlag( 0 ))
2203        {
2204          *earlyDetectionSkipMode = true;
2205        }
2206        else if(m_pcEncCfg->getFastSearch() != SELECTIVE)
2207        {
2208          Int absoulte_MV=0;
2209          for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2210          {
2211            if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
2212            {
2213              TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
2214              Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
2215              Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
2216              absoulte_MV+=iHor+iVer;
2217            }
2218          }
2219
2220          if(absoulte_MV == 0)
2221          {
2222            *earlyDetectionSkipMode = true;
2223          }
2224        }
2225      }
2226    }
2227  }
2228  DEBUG_STRING_APPEND(sDebug, bestStr)
2229#if NH_3D_ARP
2230 }
2231#endif
2232#if NH_3D_SPIVMP
2233 delete[] pcMvFieldSP;
2234 delete[] puhInterDirSP;
2235#endif
2236}
2237
2238
2239#if AMP_MRG
2240#if  H_3D_FAST_TEXTURE_ENCODING
2241Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bFMD, Bool bUseMRG)
2242#else
2243Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize DEBUG_STRING_FN_DECLARE(sDebug), Bool bUseMRG)
2244#endif
2245#else
2246Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
2247#endif
2248{
2249  DEBUG_STRING_NEW(sTest)
2250
2251  // prior to this, rpcTempCU will have just been reset using rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
2252#if H_3D || NH_3D_ARP
2253  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
2254#endif
2255#if  H_3D_FAST_TEXTURE_ENCODING
2256  if(!(bFMD && (ePartSize == SIZE_2Nx2N)))  //have  motion estimation or merge check
2257  {
2258#endif
2259  UChar uhDepth = rpcTempCU->getDepth( 0 );
2260#if NH_3D_ARP
2261    Bool bFirstTime = true;
2262    Int nARPWMax    = rpcTempCU->getSlice()->getARPStepNum() - 1;
2263#if NH_3D_IC
2264    if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N || rpcTempCU->getICFlag(0) )
2265#else
2266    if( nARPWMax < 0 || ePartSize != SIZE_2Nx2N )
2267#endif
2268    {
2269      nARPWMax = 0;
2270    }
2271
2272    for( Int nARPW = 0; nARPW <= nARPWMax; nARPW++ )
2273    {
2274      if( !bFirstTime && rpcTempCU->getSlice()->getIvResPredFlag() )
2275      {
2276        rpcTempCU->initEstData( rpcTempCU->getDepth(0), rpcTempCU->getQP(0),bTransquantBypassFlag );     
2277      }
2278#endif
2279#if NH_3D_VSO // M3
2280      if( m_pcRdCost->getUseRenModel() )
2281      {
2282        UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( COMPONENT_Y );
2283        UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( COMPONENT_Y );
2284        Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getAddr  ( COMPONENT_Y );
2285        UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride( COMPONENT_Y );
2286        m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2287      }
2288#endif
2289#if H_3D
2290      rpcTempCU->setDISFlagSubParts( false, 0, uhDepth );
2291#endif
2292  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
2293  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2294  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_cuChromaQpOffsetIdxPlus1, 0, uhDepth );
2295#if NH_3D_ARP
2296      rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2297#endif
2298#if NH_3D_ARP
2299      if( bFirstTime == false && nARPWMax )
2300      {
2301        rpcTempCU->copyPartFrom( m_ppcWeightedTempCU[uhDepth] , 0 , uhDepth );
2302        rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
2303
2304        m_pcPredSearch->motionCompensation( rpcTempCU , m_ppcPredYuvTemp[uhDepth] );
2305      }
2306      else
2307      {
2308        bFirstTime = false;
2309#endif
2310#if AMP_MRG
2311  rpcTempCU->setMergeAMP (true);
2312#if  H_3D_FAST_TEXTURE_ENCODING
2313        m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], bFMD, false, bUseMRG );
2314#else
2315  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] DEBUG_STRING_PASS_INTO(sTest), false, bUseMRG );
2316#endif
2317
2318#else
2319  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
2320#endif
2321#if NH_3D_ARP
2322        if( nARPWMax )
2323        {
2324          m_ppcWeightedTempCU[uhDepth]->copyPartFrom( rpcTempCU , 0 , uhDepth );
2325        }
2326      }
2327#endif
2328
2329#if AMP_MRG
2330  if ( !rpcTempCU->getMergeAMP() )
2331  {
2332#if NH_3D_ARP
2333        if( nARPWMax )
2334        {
2335          continue;
2336        }
2337        else
2338#endif
2339    return;
2340  }
2341#endif
2342#if KWU_RC_MADPRED_E0227
2343      if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth )
2344      {
2345        UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(),
2346          m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(),
2347          rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) );
2348        m_temporalSAD = (Int)SAD;
2349      }
2350#endif
2351
2352  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) );
2353#if NH_3D_VSP
2354  if( rpcTempCU->getQtRootCbf(0)==0 )
2355  {
2356    rpcTempCU->setTrIdxSubParts(0, 0, uhDepth);
2357  }
2358#endif
2359#if NH_3D_VSO // M4
2360  if( m_pcRdCost->getUseLambdaScaleVSO() )
2361  {
2362    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2363  }
2364  else           
2365#endif
2366    rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2367
2368#if DEBUG_STRING
2369  DebugInterPredResiReco(sTest, *(m_ppcPredYuvTemp[uhDepth]), *(m_ppcResiYuvBest[uhDepth]), *(m_ppcRecoYuvTemp[uhDepth]), DebugStringGetPredModeMask(rpcTempCU->getPredictionMode(0)));
2370#endif
2371#if H_3D_INTER_SDC
2372      TComDataCU *rpcTempCUPre = rpcTempCU;
2373#endif
2374
2375  xCheckDQP( rpcTempCU );
2376  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTest));
2377#if H_3D_INTER_SDC
2378      if( rpcTempCU->getSlice()->getInterSdcFlag() && ePartSize == SIZE_2Nx2N)
2379      {
2380        Double dOffsetCost[3] = {MAX_DOUBLE,MAX_DOUBLE,MAX_DOUBLE};
2381        for( Int uiOffest = 1 ; uiOffest <= 5 ; uiOffest++ )
2382        {
2383          if( uiOffest > 3)
2384          {
2385            if ( dOffsetCost[0] < (0.9*dOffsetCost[1]) && dOffsetCost[0] < (0.9*dOffsetCost[2]) )
2386            {
2387              continue;
2388            }
2389            if ( dOffsetCost[1] < dOffsetCost[0] && dOffsetCost[0] < dOffsetCost[2] &&  uiOffest == 5)
2390            {
2391              continue;
2392            }
2393            if ( dOffsetCost[0] < dOffsetCost[1] && dOffsetCost[2] < dOffsetCost[0] &&  uiOffest == 4)
2394            {
2395              continue;
2396            }
2397          }
2398
2399          if( rpcTempCU != rpcTempCUPre )
2400          {
2401            Int orgQP = rpcBestCU->getQP( 0 );
2402            rpcTempCU->initEstData( uhDepth, orgQP ,bTransquantBypassFlag );     
2403            rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
2404          }
2405          rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
2406#if H_3D
2407          rpcTempCU->setDISFlagSubParts( false, 0, uhDepth );
2408#endif
2409          rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
2410          rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
2411#if NH_3D_VSO // M3
2412          if( m_pcRdCost->getUseRenModel() )
2413          {
2414            UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( COMPONENT_Y );
2415            UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( COMPONENT_Y  );
2416            Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getAddr  ( COMPONENT_Y  );
2417            UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride( COMPONENT_Y  );
2418            m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2419          }
2420#endif
2421
2422          Int iSdcOffset = 0;
2423          if(uiOffest % 2 == 0)
2424          {
2425            iSdcOffset = uiOffest >> 1;
2426          }
2427          else
2428          {
2429            iSdcOffset = -1 * (uiOffest >> 1);
2430          }
2431          m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
2432            m_ppcOrigYuv[uhDepth],
2433            ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth],
2434            m_ppcResiYuvTemp[uhDepth],
2435            m_ppcRecoYuvTemp[uhDepth],
2436            iSdcOffset,
2437            uhDepth );
2438          if (uiOffest <= 3 )
2439          {
2440            dOffsetCost [uiOffest -1] = rpcTempCU->getTotalCost();
2441          }
2442
2443          xCheckDQP( rpcTempCU );
2444          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
2445        }
2446
2447      }
2448#endif
2449#if NH_3D_ARP
2450    }
2451#endif
2452#if  H_3D_FAST_TEXTURE_ENCODING
2453  }
2454#endif
2455}
2456
2457#if NH_3D_DBBP
2458Void TEncCu::xInvalidateOriginalSegments( TComYuv* pOrigYuv, TComYuv* pOrigYuvTemp, Bool* pMask, UInt uiValidSegment )
2459{
2460  UInt  uiWidth     = pOrigYuv->getWidth (COMPONENT_Y);
2461  UInt  uiHeight    = pOrigYuv->getHeight(COMPONENT_Y);
2462  Pel*  piSrc       = pOrigYuv->getAddr(COMPONENT_Y);
2463  UInt  uiSrcStride = pOrigYuv->getStride(COMPONENT_Y);
2464  Pel*  piDst       = pOrigYuvTemp->getAddr(COMPONENT_Y);
2465  UInt  uiDstStride = pOrigYuvTemp->getStride(COMPONENT_Y);
2466 
2467  UInt  uiMaskStride= MAX_CU_SIZE;
2468 
2469  AOF( uiWidth == uiHeight );
2470 
2471  // backup pointer
2472  Bool* pMaskStart = pMask;
2473 
2474  for (Int y=0; y<uiHeight; y++)
2475  {
2476    for (Int x=0; x<uiWidth; x++)
2477    {
2478      UChar ucSegment = (UChar)pMask[x];
2479      AOF( ucSegment < 2 );
2480     
2481      piDst[x] = (ucSegment==uiValidSegment)?piSrc[x]:DBBP_INVALID_SHORT;
2482    }
2483   
2484    piSrc  += uiSrcStride;
2485    piDst  += uiDstStride;
2486    pMask  += uiMaskStride;
2487  }
2488 
2489  // now invalidate chroma
2490  Pel*  piSrcU       = pOrigYuv->getAddr(COMPONENT_Cb);
2491  Pel*  piSrcV       = pOrigYuv->getAddr(COMPONENT_Cr);
2492  UInt  uiSrcStrideC = pOrigYuv->getStride(COMPONENT_Cb);
2493  Pel*  piDstU       = pOrigYuvTemp->getAddr(COMPONENT_Cb);
2494  Pel*  piDstV       = pOrigYuvTemp->getAddr(COMPONENT_Cr);
2495  UInt  uiDstStrideC = pOrigYuvTemp->getStride(COMPONENT_Cb);
2496  pMask = pMaskStart;
2497 
2498  for (Int y=0; y<uiHeight/2; y++)
2499  {
2500    for (Int x=0; x<uiWidth/2; x++)
2501    {
2502      UChar ucSegment = (UChar)pMask[x*2];
2503      AOF( ucSegment < 2 );
2504     
2505      piDstU[x] = (ucSegment==uiValidSegment)?piSrcU[x]:DBBP_INVALID_SHORT;
2506      piDstV[x] = (ucSegment==uiValidSegment)?piSrcV[x]:DBBP_INVALID_SHORT;
2507    }
2508   
2509    piSrcU  += uiSrcStrideC;
2510    piSrcV  += uiSrcStrideC;
2511    piDstU  += uiDstStrideC;
2512    piDstV  += uiDstStrideC;
2513    pMask   += 2*uiMaskStride;
2514  }
2515}
2516#endif
2517
2518#if H_3D_DIS
2519Void TEncCu::xCheckRDCostDIS( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize )
2520{
2521  UInt uiDepth = rpcTempCU->getDepth( 0 );
2522  if( !rpcBestCU->getSlice()->getIsDepth() || (eSize != SIZE_2Nx2N))
2523  {
2524    return;
2525  }
2526
2527#if NH_3D_VSO // M5
2528  if( m_pcRdCost->getUseRenModel() )
2529  {
2530    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ( COMPONENT_Y );
2531    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ( COMPONENT_Y );
2532    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getAddr    ( COMPONENT_Y );
2533    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ( COMPONENT_Y );
2534    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2535  }
2536#endif
2537
2538  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2539  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2540  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2541  rpcTempCU->setCUTransquantBypassSubParts( rpcTempCU->getCUTransquantBypass(0), 0, uiDepth );
2542
2543  rpcTempCU->setTrIdxSubParts(0, 0, uiDepth);
2544  rpcTempCU->setCbfSubParts(0, 1, 1, 0, uiDepth);
2545  rpcTempCU->setDISFlagSubParts(true, 0, uiDepth);
2546  rpcTempCU->setLumaIntraDirSubParts (DC_IDX, 0, uiDepth);
2547#if H_3D_DIM_SDC
2548  rpcTempCU->setSDCFlagSubParts( false, 0, uiDepth);
2549#endif
2550
2551  UInt uiPreCalcDistC;
2552  m_pcPredSearch  ->estIntraPredDIS      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, false );
2553
2554#if ENC_DEC_TRACE && H_MV_ENC_DEC_TRAC
2555  Int oldTraceCopyBack = g_traceCopyBack; 
2556  g_traceCopyBack = false; 
2557#endif
2558  m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() );
2559  #if ENC_DEC_TRACE && H_MV_ENC_DEC_TRAC 
2560    g_traceCopyBack = oldTraceCopyBack; 
2561  #endif
2562
2563
2564  m_pcEntropyCoder->resetBits();
2565  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2566  {
2567    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2568  }
2569  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2570  m_pcEntropyCoder->encodeDIS( rpcTempCU, 0,          true );
2571
2572
2573  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2574
2575  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2576  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2577
2578
2579#if NH_3D_VSO // M6
2580  if( m_pcRdCost->getUseLambdaScaleVSO())
2581  {
2582    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
2583  }
2584  else
2585#endif
2586    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2587
2588
2589  xCheckDQP( rpcTempCU );
2590  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth);
2591}
2592#endif
2593
2594#if NH_3D_DBBP
2595Void TEncCu::xCheckRDCostInterDBBP( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU  DEBUG_STRING_FN_DECLARE(sDebug), Bool bUseMRG )
2596{
2597  DEBUG_STRING_NEW(sTest)
2598  AOF( !rpcTempCU->getSlice()->getIsDepth() );
2599 
2600  UChar uhDepth = rpcTempCU->getDepth( 0 );
2601 
2602#if NH_3D_VSO
2603  if( m_pcRdCost->getUseRenModel() )
2604  {
2605    UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth ( COMPONENT_Y );
2606    UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight( COMPONENT_Y );
2607    Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getAddr  ( COMPONENT_Y );
2608    UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride( COMPONENT_Y );
2609    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2610  }
2611#endif
2612 
2613  UInt uiWidth  = rpcTempCU->getWidth(0);
2614  UInt uiHeight = rpcTempCU->getHeight(0);
2615  AOF( uiWidth == uiHeight );
2616 
2617#if NH_3D_DBBP
2618  if(uiWidth <= 8)
2619  {
2620    return;
2621  }
2622#endif
2623 
2624  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N,  0, uhDepth );
2625 
2626  // fetch virtual depth block
2627  UInt uiDepthStride = 0;
2628#if H_3D_FCO
2629  Pel* pDepthPels = rpcTempCU->getVirtualDepthBlock(rpcTempCU->getZorderIdxInCU(), uiWidth, uiHeight, uiDepthStride);
2630#else
2631  Pel* pDepthPels = rpcTempCU->getVirtualDepthBlock(0, uiWidth, uiHeight, uiDepthStride);
2632#endif
2633  AOF( pDepthPels != NULL );
2634  AOF( uiDepthStride != 0 );
2635 
2636  PartSize eVirtualPartSize = m_pcPredSearch->getPartitionSizeFromDepth(pDepthPels, uiDepthStride, uiWidth, rpcTempCU);
2637
2638  // derive partitioning from depth
2639  Bool pMask[MAX_CU_SIZE*MAX_CU_SIZE];
2640  Bool bValidMask = m_pcPredSearch->getSegmentMaskFromDepth(pDepthPels, uiDepthStride, uiWidth, uiHeight, pMask, rpcTempCU);
2641 
2642  if( !bValidMask )
2643  {
2644    return;
2645  }
2646 
2647  // find optimal motion/disparity vector for each segment
2648  DisInfo originalDvInfo = rpcTempCU->getDvInfo(0);
2649  DbbpTmpData* pDBBPTmpData = rpcTempCU->getDBBPTmpData();
2650  TComYuv* apPredYuv[2] = { m_ppcRecoYuvTemp[uhDepth], m_ppcPredYuvTemp[uhDepth] };
2651 
2652  // find optimal motion vector fields for both segments (as 2Nx2N)
2653  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2654  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N,  0, uhDepth );
2655  rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth );
2656  for( UInt uiSegment = 0; uiSegment < 2; uiSegment++ )
2657  {
2658    rpcTempCU->setDBBPFlagSubParts(true, 0, 0, uhDepth);
2659    rpcTempCU->setDvInfoSubParts(originalDvInfo, 0, uhDepth);
2660   
2661    // invalidate all other segments in original YUV
2662    xInvalidateOriginalSegments(m_ppcOrigYuv[uhDepth], m_ppcOrigYuvDBBP[uhDepth], pMask, uiSegment);
2663   
2664    // do motion estimation for this segment
2665    m_pcRdCost->setUseMask(true);
2666    rpcTempCU->getDBBPTmpData()->eVirtualPartSize = eVirtualPartSize;
2667    rpcTempCU->getDBBPTmpData()->uiVirtualPartIndex = uiSegment;
2668    m_pcPredSearch->predInterSearch( rpcTempCU, m_ppcOrigYuvDBBP[uhDepth], apPredYuv[uiSegment], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth] DEBUG_STRING_PASS_INTO(sTest), false, bUseMRG );
2669    m_pcRdCost->setUseMask(false);
2670   
2671    // extract motion parameters of full block for this segment
2672    pDBBPTmpData->auhInterDir[uiSegment] = rpcTempCU->getInterDir(0);
2673   
2674    pDBBPTmpData->abMergeFlag[uiSegment] = rpcTempCU->getMergeFlag(0);
2675    pDBBPTmpData->auhMergeIndex[uiSegment] = rpcTempCU->getMergeIndex(0);
2676   
2677#if NH_3D_VSP
2678    AOF( rpcTempCU->getSPIVMPFlag(0) == false );
2679    AOF( rpcTempCU->getVSPFlag(0) == 0 );
2680#endif
2681   
2682    for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2683    {
2684      RefPicList eRefList = (RefPicList)uiRefListIdx;
2685     
2686      pDBBPTmpData->acMvd[uiSegment][eRefList] = rpcTempCU->getCUMvField(eRefList)->getMvd(0);
2687      pDBBPTmpData->aiMvpNum[uiSegment][eRefList] = rpcTempCU->getMVPNum(eRefList, 0);
2688      pDBBPTmpData->aiMvpIdx[uiSegment][eRefList] = rpcTempCU->getMVPIdx(eRefList, 0);
2689     
2690      rpcTempCU->getMvField(rpcTempCU, 0, eRefList, pDBBPTmpData->acMvField[uiSegment][eRefList]);
2691    }
2692  }
2693 
2694  // store final motion/disparity information in each PU using derived partitioning
2695  rpcTempCU->setDepthSubParts( uhDepth, 0 );
2696  rpcTempCU->setPartSizeSubParts  ( eVirtualPartSize,  0, uhDepth );
2697  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
2698 
2699  UInt uiPUOffset = ( g_auiPUOffset[UInt( eVirtualPartSize )] << ( ( rpcTempCU->getSlice()->getSPS()->getMaxTotalCUDepth() - uhDepth ) << 1 ) ) >> 4;
2700  for( UInt uiSegment = 0; uiSegment < 2; uiSegment++ )
2701  {
2702    UInt uiPartAddr = uiSegment*uiPUOffset;
2703   
2704    rpcTempCU->setDBBPFlagSubParts(true, uiPartAddr, uiSegment, uhDepth);
2705   
2706    // now set stored information from 2Nx2N motion search to each partition
2707    rpcTempCU->setInterDirSubParts(pDBBPTmpData->auhInterDir[uiSegment], uiPartAddr, uiSegment, uhDepth); // interprets depth relative to LCU level
2708   
2709    rpcTempCU->setMergeFlagSubParts(pDBBPTmpData->abMergeFlag[uiSegment], uiPartAddr, uiSegment, uhDepth);
2710    rpcTempCU->setMergeIndexSubParts(pDBBPTmpData->auhMergeIndex[uiSegment], uiPartAddr, uiSegment, uhDepth);
2711       
2712    for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
2713    {
2714      RefPicList eRefList = (RefPicList)uiRefListIdx;
2715     
2716      rpcTempCU->getCUMvField( eRefList )->setAllMvd(pDBBPTmpData->acMvd[uiSegment][eRefList], eVirtualPartSize, uiPartAddr, 0, uiSegment);
2717      rpcTempCU->setMVPNum(eRefList, uiPartAddr, pDBBPTmpData->aiMvpNum[uiSegment][eRefList]);
2718      rpcTempCU->setMVPIdx(eRefList, uiPartAddr, pDBBPTmpData->aiMvpIdx[uiSegment][eRefList]);
2719     
2720      rpcTempCU->getCUMvField( eRefList )->setAllMvField( pDBBPTmpData->acMvField[uiSegment][eRefList], eVirtualPartSize, uiPartAddr, 0, uiSegment ); // interprets depth relative to rpcTempCU level
2721    }
2722  }
2723 
2724  // reconstruct final prediction signal by combining both segments
2725  Int bitDepthY = rpcTempCU->getSlice()->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA);
2726  m_pcPredSearch->combineSegmentsWithMask(apPredYuv, m_ppcPredYuvTemp[uhDepth], pMask, uiWidth, uiHeight, 0, eVirtualPartSize, bitDepthY);
2727  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) );
2728 
2729  xCheckDQP( rpcTempCU );
2730  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth  DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTest) );
2731}
2732#endif
2733
2734#if H_3D_DIM
2735Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize, Bool bOnlyIVP )
2736#else
2737Void TEncCu::xCheckRDCostIntra( TComDataCU *&rpcBestCU,
2738                                TComDataCU *&rpcTempCU,
2739                                Double      &cost,
2740                                PartSize     eSize
2741                                DEBUG_STRING_FN_DECLARE(sDebug) )
2742#endif
2743{
2744  DEBUG_STRING_NEW(sTest)
2745
2746  UInt uiDepth = rpcTempCU->getDepth( 0 );
2747#if NH_3D_VSO // M5
2748  if( m_pcRdCost->getUseRenModel() )
2749  {
2750    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ( COMPONENT_Y );
2751    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ( COMPONENT_Y );
2752    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getAddr    ( COMPONENT_Y );
2753    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ( COMPONENT_Y );
2754    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2755  }
2756#endif
2757
2758  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2759#if H_3D
2760  rpcTempCU->setDISFlagSubParts( false, 0, uiDepth );
2761#endif
2762
2763
2764  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
2765  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2766  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_cuChromaQpOffsetIdxPlus1, 0, uiDepth );
2767
2768  Pel resiLuma[NUMBER_OF_STORED_RESIDUAL_TYPES][MAX_CU_SIZE * MAX_CU_SIZE];
2769
2770  m_pcPredSearch->estIntraPredLumaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], resiLuma DEBUG_STRING_PASS_INTO(sTest) );
2771
2772  m_ppcRecoYuvTemp[uiDepth]->copyToPicComponent(COMPONENT_Y, rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getCtuRsAddr(), rpcTempCU->getZorderIdxInCtu() );
2773
2774  if (rpcBestCU->getPic()->getChromaFormat()!=CHROMA_400)
2775  {
2776#if H_3D_DIM
2777    m_pcPredSearch->estIntraPredQT      ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma, bOnlyIVP );
2778#else
2779    m_pcPredSearch->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], resiLuma DEBUG_STRING_PASS_INTO(sTest) );
2780#endif
2781  }
2782#if H_3D_DIM_SDC
2783  if( !rpcTempCU->getSDCFlag( 0 ) )
2784#endif
2785
2786  m_pcEntropyCoder->resetBits();
2787
2788  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2789  {
2790    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2791  }
2792#if H_3D
2793  m_pcEntropyCoder->encodeDIS( rpcTempCU, 0,          true );
2794  if(!rpcTempCU->getDISFlag(0))
2795  {
2796#endif
2797  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2798  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
2799  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
2800  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0 );
2801  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
2802#if H_3D_DIM_SDC
2803    m_pcEntropyCoder->encodeSDCFlag( rpcTempCU, 0, true );
2804#endif
2805
2806  // Encode Coefficients
2807  Bool bCodeDQP = getdQPFlag();
2808  Bool codeChromaQpAdjFlag = getCodeChromaQpAdjFlag();
2809  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, bCodeDQP, codeChromaQpAdjFlag );
2810  setCodeChromaQpAdjFlag( codeChromaQpAdjFlag );
2811  setdQPFlag( bCodeDQP );
2812#if H_3D
2813  }
2814#endif
2815  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2816
2817  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2818  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2819#if NH_3D_VSO // M6
2820  if( m_pcRdCost->getUseLambdaScaleVSO()) 
2821  {
2822    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); 
2823  }
2824  else
2825#endif
2826    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2827
2828  xCheckDQP( rpcTempCU );
2829
2830  cost = rpcTempCU->getTotalCost();
2831
2832  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTest));
2833}
2834
2835
2836/** Check R-D costs for a CU with PCM mode.
2837 * \param rpcBestCU pointer to best mode CU data structure
2838 * \param rpcTempCU pointer to testing mode CU data structure
2839 * \returns Void
2840 *
2841 * \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.
2842 */
2843Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
2844{
2845  UInt uiDepth = rpcTempCU->getDepth( 0 );
2846
2847#if NH_3D_VSO // VERY NEW
2848  if( m_pcRdCost->getUseRenModel() )
2849  {
2850    UInt  uiWidth     = m_ppcOrigYuv[uiDepth]->getWidth   ( COMPONENT_Y );
2851    UInt  uiHeight    = m_ppcOrigYuv[uiDepth]->getHeight  ( COMPONENT_Y );
2852    Pel*  piSrc       = m_ppcOrigYuv[uiDepth]->getAddr    ( COMPONENT_Y );
2853    UInt  uiSrcStride = m_ppcOrigYuv[uiDepth]->getStride  ( COMPONENT_Y );
2854    m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
2855  }
2856#endif
2857
2858  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
2859#if H_3D
2860  rpcTempCU->setDISFlagSubParts( false, 0, uiDepth );
2861#endif
2862  rpcTempCU->setIPCMFlag(0, true);
2863  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
2864  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
2865  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
2866  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
2867  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_cuChromaQpOffsetIdxPlus1, 0, uiDepth );
2868
2869  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
2870
2871  m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
2872
2873  m_pcEntropyCoder->resetBits();
2874
2875  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
2876  {
2877    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
2878  }
2879
2880  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
2881#if H_3D
2882  m_pcEntropyCoder->encodeDIS( rpcTempCU, 0,          true );
2883#endif
2884  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
2885  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
2886  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
2887#if H_3D_DIM_SDC
2888  m_pcEntropyCoder->encodeSDCFlag( rpcTempCU, 0, true );
2889#endif
2890  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
2891
2892  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
2893  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2894#if NH_3D_VSO // M44
2895  if ( m_pcRdCost->getUseVSO() )
2896  {
2897    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2898  }
2899  else
2900#endif
2901    rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
2902
2903  xCheckDQP( rpcTempCU );
2904  DEBUG_STRING_NEW(a)
2905  DEBUG_STRING_NEW(b)
2906  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(a) DEBUG_STRING_PASS_INTO(b));
2907}
2908
2909/** check whether current try is the best with identifying the depth of current try
2910 * \param rpcBestCU
2911 * \param rpcTempCU
2912 * \param uiDepth
2913 */
2914Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth DEBUG_STRING_FN_DECLARE(sParent) DEBUG_STRING_FN_DECLARE(sTest) DEBUG_STRING_PASS_INTO(Bool bAddSizeInfo) )
2915{
2916  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
2917  {
2918    TComYuv* pcYuv;
2919    // Change Information data
2920    TComDataCU* pcCU = rpcBestCU;
2921    rpcBestCU = rpcTempCU;
2922    rpcTempCU = pcCU;
2923
2924    // Change Prediction data
2925    pcYuv = m_ppcPredYuvBest[uiDepth];
2926    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
2927    m_ppcPredYuvTemp[uiDepth] = pcYuv;
2928
2929    // Change Reconstruction data
2930    pcYuv = m_ppcRecoYuvBest[uiDepth];
2931    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
2932    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
2933
2934    pcYuv = NULL;
2935    pcCU  = NULL;
2936
2937    // store temp best CI for next CU coding
2938    m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
2939
2940
2941#if DEBUG_STRING
2942    DEBUG_STRING_SWAP(sParent, sTest)
2943    const PredMode predMode=rpcBestCU->getPredictionMode(0);
2944    if ((DebugOptionList::DebugString_Structure.getInt()&DebugStringGetPredModeMask(predMode)) && bAddSizeInfo)
2945    {
2946      std::stringstream ss(stringstream::out);
2947      ss <<"###: " << (predMode==MODE_INTRA?"Intra   ":"Inter   ") << partSizeToString[rpcBestCU->getPartitionSize(0)] << " CU at " << rpcBestCU->getCUPelX() << ", " << rpcBestCU->getCUPelY() << " width=" << UInt(rpcBestCU->getWidth(0)) << std::endl;
2948      sParent+=ss.str();
2949    }
2950#endif
2951  }
2952}
2953
2954Void TEncCu::xCheckDQP( TComDataCU* pcCU )
2955{
2956  UInt uiDepth = pcCU->getDepth( 0 );
2957
2958  const TComPPS &pps = *(pcCU->getSlice()->getPPS());
2959  if ( pps.getUseDQP() && uiDepth <= pps.getMaxCuDQPDepth() )
2960  {
2961    if ( pcCU->getQtRootCbf( 0) )
2962    {
2963      m_pcEntropyCoder->resetBits();
2964      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
2965      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
2966      pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
2967#if NH_3D_VSO // M45
2968      if ( m_pcRdCost->getUseVSO() )     
2969      {
2970        pcCU->getTotalCost() = m_pcRdCost->calcRdCostVSO( pcCU->getTotalBits(), pcCU->getTotalDistortion() );     
2971      }
2972      else
2973#endif
2974        pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
2975    }
2976    else
2977    {
2978      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
2979    }
2980  }
2981}
2982
2983Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
2984{
2985  pDst->iN = pSrc->iN;
2986  for (Int i = 0; i < pSrc->iN; i++)
2987  {
2988    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
2989  }
2990}
2991Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth )
2992{
2993  UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
2994  UInt uiSrcBlkWidth = rpcPic->getNumPartInCtuWidth() >> (uiSrcDepth);
2995  UInt uiBlkWidth    = rpcPic->getNumPartInCtuWidth() >> (uiDepth);
2996  UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInCtuWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2997  UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInCtuWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
2998  UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
2999  m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
3000
3001  m_ppcPredYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvPred (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
3002}
3003
3004Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
3005{
3006  UInt uiCurrDepth = uiNextDepth - 1;
3007  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
3008  m_ppcPredYuvBest[uiNextDepth]->copyToPartYuv( m_ppcPredYuvBest[uiCurrDepth], uiPartUnitIdx);
3009}
3010
3011/** Function for filling the PCM buffer of a CU using its original sample array
3012 * \param pCU pointer to current CU
3013 * \param pOrgYuv pointer to original sample array
3014 */
3015Void TEncCu::xFillPCMBuffer     ( TComDataCU* pCU, TComYuv* pOrgYuv )
3016{
3017  const ChromaFormat format = pCU->getPic()->getChromaFormat();
3018  const UInt numberValidComponents = getNumberValidComponents(format);
3019  for (UInt componentIndex = 0; componentIndex < numberValidComponents; componentIndex++)
3020  {
3021    const ComponentID component = ComponentID(componentIndex);
3022
3023    const UInt width  = pCU->getWidth(0)  >> getComponentScaleX(component, format);
3024    const UInt height = pCU->getHeight(0) >> getComponentScaleY(component, format);
3025
3026    Pel *source      = pOrgYuv->getAddr(component, 0, width);
3027    Pel *destination = pCU->getPCMSample(component);
3028
3029    const UInt sourceStride = pOrgYuv->getStride(component);
3030
3031    for (Int line = 0; line < height; line++)
3032    {
3033      for (Int column = 0; column < width; column++)
3034      {
3035        destination[column] = source[column];
3036      }
3037
3038      source      += sourceStride;
3039      destination += width;
3040    }
3041  }
3042}
3043
3044#if ADAPTIVE_QP_SELECTION
3045/** Collect ARL statistics from one block
3046  */
3047Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, TCoeff* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
3048{
3049  for( Int n = 0; n < NumCoeffInCU; n++ )
3050  {
3051    TCoeff u = abs( rpcCoeff[ n ] );
3052    TCoeff absc = rpcArlCoeff[ n ];
3053
3054    if( u != 0 )
3055    {
3056      if( u < LEVEL_RANGE )
3057      {
3058        cSum[ u ] += ( Double )absc;
3059        numSamples[ u ]++;
3060      }
3061      else
3062      {
3063        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
3064        numSamples[ LEVEL_RANGE ]++;
3065      }
3066    }
3067  }
3068
3069  return 0;
3070}
3071
3072//! Collect ARL statistics from one CTU
3073Void TEncCu::xCtuCollectARLStats(TComDataCU* pCtu )
3074{
3075  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to data type and quantization output
3076  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to data type and quantization output
3077
3078  TCoeff* pCoeffY = pCtu->getCoeff(COMPONENT_Y);
3079  TCoeff* pArlCoeffY = pCtu->getArlCoeff(COMPONENT_Y);
3080  const TComSPS &sps = *(pCtu->getSlice()->getSPS());
3081
3082  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.
3083  const UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;                          // NOTE: ed - what is this?
3084
3085  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
3086  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
3087
3088  // Collect stats to cSum[][] and numSamples[][]
3089  for(Int i = 0; i < pCtu->getTotalNumPart(); i ++ )
3090  {
3091    UInt uiTrIdx = pCtu->getTransformIdx(i);
3092
3093    if(pCtu->isInter(i) && pCtu->getCbf( i, COMPONENT_Y, uiTrIdx ) )
3094    {
3095      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
3096    }//Note that only InterY is processed. QP rounding is based on InterY data only.
3097
3098    pCoeffY  += uiMinNumCoeffInCU;
3099    pArlCoeffY  += uiMinNumCoeffInCU;
3100  }
3101
3102  for(Int u=1; u<LEVEL_RANGE;u++)
3103  {
3104    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
3105    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
3106  }
3107  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
3108  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
3109}
3110#endif
3111
3112//! \}
Note: See TracBrowser for help on using the repository browser.