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

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