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

Last change on this file since 1255 was 1255, checked in by ntt, 9 years ago

Reactivation of VSP

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