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

Last change on this file since 1233 was 1233, checked in by etri-htm, 9 years ago

Reactive_MLC (the MACRO is "NH_3D_MLC")

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