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

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