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

Last change on this file since 1417 was 1259, checked in by mediatek-htm, 10 years ago

Reactive IVMV by Mediatek

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