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

Last change on this file since 1272 was 1272, checked in by qualcomm, 9 years ago

1) Fix issues of Sub-PU based inter view motion prediction when ARP is enabled; 2) minor code cleanups for VSP

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