source: SHVCSoftware/branches/SHM-dev/source/Lib/TLibEncoder/TEncCu.cpp @ 1363

Last change on this file since 1363 was 1346, checked in by seregin, 10 years ago

port rev 4424

  • Property svn:eol-style set to native
File size: 70.1 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  m_ppcPredYuvBest = new TComYuv*[m_uhTotalDepth-1];
71  m_ppcResiYuvBest = new TComYuv*[m_uhTotalDepth-1];
72  m_ppcRecoYuvBest = new TComYuv*[m_uhTotalDepth-1];
73  m_ppcPredYuvTemp = new TComYuv*[m_uhTotalDepth-1];
74  m_ppcResiYuvTemp = new TComYuv*[m_uhTotalDepth-1];
75  m_ppcRecoYuvTemp = new TComYuv*[m_uhTotalDepth-1];
76  m_ppcOrigYuv     = new TComYuv*[m_uhTotalDepth-1];
77
78  UInt uiNumPartitions;
79  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
80  {
81    uiNumPartitions = 1<<( ( m_uhTotalDepth - i - 1 )<<1 );
82    UInt uiWidth  = uiMaxWidth  >> i;
83    UInt uiHeight = uiMaxHeight >> i;
84
85    m_ppcBestCU[i] = new TComDataCU; m_ppcBestCU[i]->create( chromaFormat, uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
86    m_ppcTempCU[i] = new TComDataCU; m_ppcTempCU[i]->create( chromaFormat, uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) );
87
88    m_ppcPredYuvBest[i] = new TComYuv; m_ppcPredYuvBest[i]->create(uiWidth, uiHeight, chromaFormat);
89    m_ppcResiYuvBest[i] = new TComYuv; m_ppcResiYuvBest[i]->create(uiWidth, uiHeight, chromaFormat);
90    m_ppcRecoYuvBest[i] = new TComYuv; m_ppcRecoYuvBest[i]->create(uiWidth, uiHeight, chromaFormat);
91
92    m_ppcPredYuvTemp[i] = new TComYuv; m_ppcPredYuvTemp[i]->create(uiWidth, uiHeight, chromaFormat);
93    m_ppcResiYuvTemp[i] = new TComYuv; m_ppcResiYuvTemp[i]->create(uiWidth, uiHeight, chromaFormat);
94    m_ppcRecoYuvTemp[i] = new TComYuv; m_ppcRecoYuvTemp[i]->create(uiWidth, uiHeight, chromaFormat);
95
96    m_ppcOrigYuv    [i] = new TComYuv; m_ppcOrigYuv    [i]->create(uiWidth, uiHeight, chromaFormat);
97  }
98
99  m_bEncodeDQP                     = false;
100  m_stillToCodeChromaQpOffsetFlag  = false;
101  m_cuChromaQpOffsetIdxPlus1       = 0;
102
103  // initialize partition order.
104  UInt* piTmp = &g_auiZscanToRaster[0];
105  initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp);
106  initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
107
108  // initialize conversion matrix from partition index to pel
109  initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth );
110}
111
112Void TEncCu::destroy()
113{
114  Int i;
115
116  for( i=0 ; i<m_uhTotalDepth-1 ; i++)
117  {
118    if(m_ppcBestCU[i])
119    {
120      m_ppcBestCU[i]->destroy();      delete m_ppcBestCU[i];      m_ppcBestCU[i] = NULL;
121    }
122    if(m_ppcTempCU[i])
123    {
124      m_ppcTempCU[i]->destroy();      delete m_ppcTempCU[i];      m_ppcTempCU[i] = NULL;
125    }
126    if(m_ppcPredYuvBest[i])
127    {
128      m_ppcPredYuvBest[i]->destroy(); delete m_ppcPredYuvBest[i]; m_ppcPredYuvBest[i] = NULL;
129    }
130    if(m_ppcResiYuvBest[i])
131    {
132      m_ppcResiYuvBest[i]->destroy(); delete m_ppcResiYuvBest[i]; m_ppcResiYuvBest[i] = NULL;
133    }
134    if(m_ppcRecoYuvBest[i])
135    {
136      m_ppcRecoYuvBest[i]->destroy(); delete m_ppcRecoYuvBest[i]; m_ppcRecoYuvBest[i] = NULL;
137    }
138    if(m_ppcPredYuvTemp[i])
139    {
140      m_ppcPredYuvTemp[i]->destroy(); delete m_ppcPredYuvTemp[i]; m_ppcPredYuvTemp[i] = NULL;
141    }
142    if(m_ppcResiYuvTemp[i])
143    {
144      m_ppcResiYuvTemp[i]->destroy(); delete m_ppcResiYuvTemp[i]; m_ppcResiYuvTemp[i] = NULL;
145    }
146    if(m_ppcRecoYuvTemp[i])
147    {
148      m_ppcRecoYuvTemp[i]->destroy(); delete m_ppcRecoYuvTemp[i]; m_ppcRecoYuvTemp[i] = NULL;
149    }
150    if(m_ppcOrigYuv[i])
151    {
152      m_ppcOrigYuv[i]->destroy();     delete m_ppcOrigYuv[i];     m_ppcOrigYuv[i] = NULL;
153    }
154  }
155  if(m_ppcBestCU)
156  {
157    delete [] m_ppcBestCU;
158    m_ppcBestCU = NULL;
159  }
160  if(m_ppcTempCU)
161  {
162    delete [] m_ppcTempCU;
163    m_ppcTempCU = NULL;
164  }
165
166  if(m_ppcPredYuvBest)
167  {
168    delete [] m_ppcPredYuvBest;
169    m_ppcPredYuvBest = NULL;
170  }
171  if(m_ppcResiYuvBest)
172  {
173    delete [] m_ppcResiYuvBest;
174    m_ppcResiYuvBest = NULL;
175  }
176  if(m_ppcRecoYuvBest)
177  {
178    delete [] m_ppcRecoYuvBest;
179    m_ppcRecoYuvBest = NULL;
180  }
181  if(m_ppcPredYuvTemp)
182  {
183    delete [] m_ppcPredYuvTemp;
184    m_ppcPredYuvTemp = NULL;
185  }
186  if(m_ppcResiYuvTemp)
187  {
188    delete [] m_ppcResiYuvTemp;
189    m_ppcResiYuvTemp = NULL;
190  }
191  if(m_ppcRecoYuvTemp)
192  {
193    delete [] m_ppcRecoYuvTemp;
194    m_ppcRecoYuvTemp = NULL;
195  }
196  if(m_ppcOrigYuv)
197  {
198    delete [] m_ppcOrigYuv;
199    m_ppcOrigYuv = NULL;
200  }
201}
202
203/** \param    pcEncTop      pointer of encoder class
204 */
205Void TEncCu::init( TEncTop* pcEncTop )
206{
207  m_pcEncCfg           = pcEncTop;
208  m_pcPredSearch       = pcEncTop->getPredSearch();
209  m_pcTrQuant          = pcEncTop->getTrQuant();
210  m_pcRdCost           = pcEncTop->getRdCost();
211 
212#if SVC_EXTENSION
213  m_ppcTEncTop         = pcEncTop->getLayerEnc();
214  for(UInt i=0 ; i< m_uhTotalDepth-1 ; i++)
215  {   
216    m_ppcBestCU[i]->setLayerId(pcEncTop->getLayerId());
217    m_ppcTempCU[i]->setLayerId(pcEncTop->getLayerId());
218  }
219#endif
220 
221  m_pcEntropyCoder     = pcEncTop->getEntropyCoder();
222  m_pcBinCABAC         = pcEncTop->getBinCABAC();
223
224  m_pppcRDSbacCoder    = pcEncTop->getRDSbacCoder();
225  m_pcRDGoOnSbacCoder  = pcEncTop->getRDGoOnSbacCoder();
226
227  m_pcRateCtrl         = pcEncTop->getRateCtrl();
228}
229
230// ====================================================================================================================
231// Public member functions
232// ====================================================================================================================
233
234/**
235 \param  pCtu pointer of CU data class
236 */
237Void TEncCu::compressCtu( TComDataCU* pCtu )
238{
239  // initialize CU data
240  m_ppcBestCU[0]->initCtu( pCtu->getPic(), pCtu->getCtuRsAddr() );
241  m_ppcTempCU[0]->initCtu( pCtu->getPic(), pCtu->getCtuRsAddr() );
242
243#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
244  m_disableILP = xCheckTileSetConstraint(pCtu);
245  m_pcPredSearch->setDisableILP(m_disableILP);
246#endif
247
248  // analysis of CU
249  DEBUG_STRING_NEW(sDebug)
250
251  xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 DEBUG_STRING_PASS_INTO(sDebug) );
252  DEBUG_STRING_OUTPUT(std::cout, sDebug)
253
254#if ADAPTIVE_QP_SELECTION
255  if( m_pcEncCfg->getUseAdaptQpSelect() )
256  {
257    if(pCtu->getSlice()->getSliceType()!=I_SLICE) //IIII
258    {
259      xCtuCollectARLStats( pCtu );
260    }
261  }
262#endif
263
264#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
265  xVerifyTileSetConstraint(pCtu);
266#endif
267}
268/** \param  pCtu  pointer of CU data class
269 */
270Void TEncCu::encodeCtu ( TComDataCU* pCtu )
271{
272  if ( pCtu->getSlice()->getPPS()->getUseDQP() )
273  {
274    setdQPFlag(true);
275  }
276
277  if ( pCtu->getSlice()->getUseChromaQpAdj() )
278  {
279    setCodeChromaQpAdjFlag(true);
280  }
281
282  // Encode CU data
283  xEncodeCU( pCtu, 0, 0 );
284}
285
286// ====================================================================================================================
287// Protected member functions
288// ====================================================================================================================
289//! Derive small set of test modes for AMP encoder speed-up
290#if AMP_ENC_SPEEDUP
291#if AMP_MRG
292Void TEncCu::deriveTestModeAMP (TComDataCU *pcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver, Bool &bTestMergeAMP_Hor, Bool &bTestMergeAMP_Ver)
293#else
294Void TEncCu::deriveTestModeAMP (TComDataCU *pcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver)
295#endif
296{
297  if ( pcBestCU->getPartitionSize(0) == SIZE_2NxN )
298  {
299    bTestAMP_Hor = true;
300  }
301  else if ( pcBestCU->getPartitionSize(0) == SIZE_Nx2N )
302  {
303    bTestAMP_Ver = true;
304  }
305  else if ( pcBestCU->getPartitionSize(0) == SIZE_2Nx2N && pcBestCU->getMergeFlag(0) == false && pcBestCU->isSkipped(0) == false )
306  {
307    bTestAMP_Hor = true;
308    bTestAMP_Ver = true;
309  }
310
311#if AMP_MRG
312  //! Utilizing the partition size of parent PU
313  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
314  {
315    bTestMergeAMP_Hor = true;
316    bTestMergeAMP_Ver = true;
317  }
318
319  if ( eParentPartSize == NUMBER_OF_PART_SIZES ) //! if parent is intra
320  {
321    if ( pcBestCU->getPartitionSize(0) == SIZE_2NxN )
322    {
323      bTestMergeAMP_Hor = true;
324    }
325    else if ( pcBestCU->getPartitionSize(0) == SIZE_Nx2N )
326    {
327      bTestMergeAMP_Ver = true;
328    }
329  }
330
331  if ( pcBestCU->getPartitionSize(0) == SIZE_2Nx2N && pcBestCU->isSkipped(0) == false )
332  {
333    bTestMergeAMP_Hor = true;
334    bTestMergeAMP_Ver = true;
335  }
336
337  if ( pcBestCU->getWidth(0) == 64 )
338  {
339    bTestAMP_Hor = false;
340    bTestAMP_Ver = false;
341  }
342#else
343  //! Utilizing the partition size of parent PU
344  if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N )
345  {
346    bTestAMP_Hor = true;
347    bTestAMP_Ver = true;
348  }
349
350  if ( eParentPartSize == SIZE_2Nx2N )
351  {
352    bTestAMP_Hor = false;
353    bTestAMP_Ver = false;
354  }
355#endif
356}
357#endif
358
359
360// ====================================================================================================================
361// Protected member functions
362// ====================================================================================================================
363/** Compress a CU block recursively with enabling sub-CTU-level delta QP
364 *  - for loop of QP value to compress the current CU with all possible QP
365*/
366#if AMP_ENC_SPEEDUP
367Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth DEBUG_STRING_FN_DECLARE(sDebug_), PartSize eParentPartSize )
368#else
369Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth )
370#endif
371{
372  TComPic* pcPic = rpcBestCU->getPic();
373  DEBUG_STRING_NEW(sDebug)
374  const TComPPS &pps=*(rpcTempCU->getSlice()->getPPS());
375  const TComSPS &sps=*(rpcTempCU->getSlice()->getSPS());
376
377  // get Original YUV data from picture
378  m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getCtuRsAddr(), rpcBestCU->getZorderIdxInCtu() );
379
380    // variable for Early CU determination
381  Bool    bSubBranch = true;
382
383  // variable for Cbf fast mode PU decision
384  Bool    doNotBlockPu = true;
385  Bool    earlyDetectionSkipMode = false;
386
387  Bool bBoundary = false;
388  UInt uiLPelX   = rpcBestCU->getCUPelX();
389  UInt uiRPelX   = uiLPelX + rpcBestCU->getWidth(0)  - 1;
390  UInt uiTPelY   = rpcBestCU->getCUPelY();
391  UInt uiBPelY   = uiTPelY + rpcBestCU->getHeight(0) - 1;
392
393  Int iBaseQP = xComputeQP( rpcBestCU, uiDepth );
394  Int iMinQP;
395  Int iMaxQP;
396  Bool isAddLowestQP = false;
397
398  const UInt numberValidComponents = rpcBestCU->getPic()->getNumberValidComponents();
399
400  if( uiDepth <= pps.getMaxCuDQPDepth() )
401  {
402    Int idQP = m_pcEncCfg->getMaxDeltaQP();
403#if SVC_EXTENSION
404    iMinQP = Clip3( -rpcTempCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP-idQP );
405    iMaxQP = Clip3( -rpcTempCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP+idQP );
406#else
407    iMinQP = Clip3( -sps.getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP-idQP );
408    iMaxQP = Clip3( -sps.getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP+idQP );
409#endif
410  }
411  else
412  {
413    iMinQP = rpcTempCU->getQP(0);
414    iMaxQP = rpcTempCU->getQP(0);
415  }
416
417  if ( m_pcEncCfg->getUseRateCtrl() )
418  {
419    iMinQP = m_pcRateCtrl->getRCQP();
420    iMaxQP = m_pcRateCtrl->getRCQP();
421  }
422
423  // transquant-bypass (TQB) processing loop variable initialisation ---
424
425  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.
426
427  if ( (pps.getTransquantBypassEnableFlag()) )
428  {
429    isAddLowestQP = true; // mark that the first iteration is to cost TQB mode.
430    iMinQP = iMinQP - 1;  // increase loop variable range by 1, to allow testing of TQB mode along with other QPs
431    if ( m_pcEncCfg->getCUTransquantBypassFlagForceValue() )
432    {
433      iMaxQP = iMinQP;
434    }
435  }
436
437  TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx());
438  // We need to split, so don't try these modes.
439#if SVC_EXTENSION
440  if ( ( uiRPelX < rpcBestCU->getSlice()->getPicWidthInLumaSamples() ) &&
441       ( uiBPelY < rpcBestCU->getSlice()->getPicHeightInLumaSamples() ) )
442#else
443  if ( ( uiRPelX < sps.getPicWidthInLumaSamples() ) &&
444       ( uiBPelY < sps.getPicHeightInLumaSamples() ) )
445#endif
446  {
447#if HIGHER_LAYER_IRAP_SKIP_FLAG
448    if (m_pcEncCfg->getSkipPictureAtArcSwitch() && m_pcEncCfg->getAdaptiveResolutionChange() > 0 && pcSlice->getLayerId() == 1 && pcSlice->getPOC() == m_pcEncCfg->getAdaptiveResolutionChange())
449    {
450      Int iQP = iBaseQP;
451      const Bool bIsLosslessMode = isAddLowestQP && (iQP == iMinQP);
452
453      if( bIsLosslessMode )
454      {
455        iQP = lowestQP;
456      }
457
458      rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
459     
460      xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode, true );
461    }
462    else
463    {
464#endif
465#if ENCODER_FAST_MODE
466    Bool testInter = true;
467    if( rpcBestCU->getLayerId() > 0 )
468    {
469      if(pcSlice->getSliceType() == P_SLICE && pcSlice->getNumRefIdx(REF_PIC_LIST_0) == pcSlice->getActiveNumILRRefIdx())
470      {
471        testInter = false;
472      }
473      if(pcSlice->getSliceType() == B_SLICE && pcSlice->getNumRefIdx(REF_PIC_LIST_0) == pcSlice->getActiveNumILRRefIdx() && pcSlice->getNumRefIdx(REF_PIC_LIST_1) == pcSlice->getActiveNumILRRefIdx()) 
474      {
475        testInter = false;
476      }
477    }
478#endif
479    for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
480    {
481      const Bool bIsLosslessMode = isAddLowestQP && (iQP == iMinQP);
482
483      if (bIsLosslessMode)
484      {
485        iQP = lowestQP;
486      }
487
488      m_cuChromaQpOffsetIdxPlus1 = 0;
489      if (pcSlice->getUseChromaQpAdj())
490      {
491        /* Pre-estimation of chroma QP based on input block activity may be performed
492         * here, using for example m_ppcOrigYuv[uiDepth] */
493        /* To exercise the current code, the index used for adjustment is based on
494         * block position
495         */
496        Int lgMinCuSize = sps.getLog2MinCodingBlockSize() +
497                          std::max<Int>(0, sps.getLog2DiffMaxMinCodingBlockSize()-Int(pps.getPpsRangeExtension().getDiffCuChromaQpOffsetDepth()));
498        m_cuChromaQpOffsetIdxPlus1 = ((uiLPelX >> lgMinCuSize) + (uiTPelY >> lgMinCuSize)) % (pps.getPpsRangeExtension().getChromaQpOffsetListLen() + 1);
499      }
500
501      rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
502
503      // do inter modes, SKIP and 2Nx2N
504#if ENCODER_FAST_MODE == 1
505      if( rpcBestCU->getSlice()->getSliceType() != I_SLICE && testInter )
506#else
507      if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
508#endif
509      {
510        // 2Nx2N
511        if(m_pcEncCfg->getUseEarlySkipDetection())
512        {
513          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N DEBUG_STRING_PASS_INTO(sDebug) );
514          rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );//by Competition for inter_2Nx2N
515        }
516        // SKIP
517        xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU DEBUG_STRING_PASS_INTO(sDebug), &earlyDetectionSkipMode );//by Merge for inter_2Nx2N
518        rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
519       
520#if ENCODER_FAST_MODE == 2
521        if (testInter)
522        {
523#endif
524        if(!m_pcEncCfg->getUseEarlySkipDetection())
525        {
526          // 2Nx2N, NxN
527          xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N DEBUG_STRING_PASS_INTO(sDebug) );
528          rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
529          if(m_pcEncCfg->getUseCbfFastMode())
530          {
531            doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
532          }
533        }
534#if ENCODER_FAST_MODE == 2
535        }
536#endif
537      }
538
539      if (bIsLosslessMode) // Restore loop variable if lossless mode was searched.
540      {
541        iQP = iMinQP;
542      }
543    }
544
545    if(!earlyDetectionSkipMode)
546    {
547      for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
548      {
549        const Bool bIsLosslessMode = isAddLowestQP && (iQP == iMinQP); // If lossless, then iQP is irrelevant for subsequent modules.
550
551        if (bIsLosslessMode)
552        {
553          iQP = lowestQP;
554        }
555
556        rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
557
558        // do inter modes, NxN, 2NxN, and Nx2N
559#if ENCODER_FAST_MODE
560        if( rpcBestCU->getSlice()->getSliceType() != I_SLICE && testInter )
561#else
562        if( rpcBestCU->getSlice()->getSliceType() != I_SLICE )
563#endif
564        {
565          // 2Nx2N, NxN
566
567          if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) ))
568          {
569            if( uiDepth == sps.getLog2DiffMaxMinCodingBlockSize() && doNotBlockPu)
570            {
571              xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN DEBUG_STRING_PASS_INTO(sDebug)   );
572              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
573            }
574          }
575
576          if(doNotBlockPu)
577          {
578            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N DEBUG_STRING_PASS_INTO(sDebug)  );
579            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
580            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N )
581            {
582              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
583            }
584          }
585          if(doNotBlockPu)
586          {
587            xCheckRDCostInter      ( rpcBestCU, rpcTempCU, SIZE_2NxN DEBUG_STRING_PASS_INTO(sDebug)  );
588            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
589            if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN)
590            {
591              doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
592            }
593          }
594
595          //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N)
596          if(sps.getUseAMP() && uiDepth < sps.getLog2DiffMaxMinCodingBlockSize() )
597          {
598#if AMP_ENC_SPEEDUP
599            Bool bTestAMP_Hor = false, bTestAMP_Ver = false;
600
601#if AMP_MRG
602            Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false;
603
604            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver);
605#else
606            deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver);
607#endif
608
609            //! Do horizontal AMP
610            if ( bTestAMP_Hor )
611            {
612              if(doNotBlockPu)
613              {
614                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU DEBUG_STRING_PASS_INTO(sDebug) );
615                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
616                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
617                {
618                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
619                }
620              }
621              if(doNotBlockPu)
622              {
623                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD DEBUG_STRING_PASS_INTO(sDebug) );
624                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
625                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
626                {
627                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
628                }
629              }
630            }
631#if AMP_MRG
632            else if ( bTestMergeAMP_Hor )
633            {
634              if(doNotBlockPu)
635              {
636                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU DEBUG_STRING_PASS_INTO(sDebug), true );
637                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
638                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU )
639                {
640                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
641                }
642              }
643              if(doNotBlockPu)
644              {
645                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD DEBUG_STRING_PASS_INTO(sDebug), true );
646                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
647                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD )
648                {
649                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
650                }
651              }
652            }
653#endif
654
655            //! Do horizontal AMP
656            if ( bTestAMP_Ver )
657            {
658              if(doNotBlockPu)
659              {
660                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N DEBUG_STRING_PASS_INTO(sDebug) );
661                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
662                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
663                {
664                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
665                }
666              }
667              if(doNotBlockPu)
668              {
669                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N DEBUG_STRING_PASS_INTO(sDebug) );
670                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
671              }
672            }
673#if AMP_MRG
674            else if ( bTestMergeAMP_Ver )
675            {
676              if(doNotBlockPu)
677              {
678                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N DEBUG_STRING_PASS_INTO(sDebug), true );
679                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
680                if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N )
681                {
682                  doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0;
683                }
684              }
685              if(doNotBlockPu)
686              {
687                xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N DEBUG_STRING_PASS_INTO(sDebug), true );
688                rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
689              }
690            }
691#endif
692
693#else
694            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU );
695            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
696            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD );
697            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
698            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N );
699            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
700
701            xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N );
702            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
703
704#endif
705          }
706        }
707
708        // do normal intra modes
709        // speedup for inter frames
710        Double intraCost = 0.0;
711
712        if((rpcBestCU->getSlice()->getSliceType() == I_SLICE)                                        ||
713#if ENCODER_FAST_MODE
714               rpcBestCU->getPredictionMode(0) == NUMBER_OF_PREDICTION_MODES                           ||  // if there is no valid inter prediction
715              !testInter                                                                              ||
716#endif
717            ((!m_pcEncCfg->getDisableIntraPUsInInterSlices()) && (
718              (rpcBestCU->getCbf( 0, COMPONENT_Y  ) != 0)                                            ||
719             ((rpcBestCU->getCbf( 0, COMPONENT_Cb ) != 0) && (numberValidComponents > COMPONENT_Cb)) ||
720             ((rpcBestCU->getCbf( 0, COMPONENT_Cr ) != 0) && (numberValidComponents > COMPONENT_Cr))  // avoid very complex intra if it is unlikely
721            )))
722        {
723          xCheckRDCostIntra( rpcBestCU, rpcTempCU, intraCost, SIZE_2Nx2N DEBUG_STRING_PASS_INTO(sDebug) );
724          rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
725          if( uiDepth == sps.getLog2DiffMaxMinCodingBlockSize() )
726          {
727            if( rpcTempCU->getWidth(0) > ( 1 << sps.getQuadtreeTULog2MinSize() ) )
728            {
729              Double tmpIntraCost;
730              xCheckRDCostIntra( rpcBestCU, rpcTempCU, tmpIntraCost, SIZE_NxN DEBUG_STRING_PASS_INTO(sDebug)   );
731              intraCost = std::min(intraCost, tmpIntraCost);
732              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
733            }
734          }
735        }
736
737        // test PCM
738        if(sps.getUsePCM()
739          && rpcTempCU->getWidth(0) <= (1<<sps.getPCMLog2MaxSize())
740          && rpcTempCU->getWidth(0) >= (1<<sps.getPCMLog2MinSize()) )
741        {
742#if SVC_EXTENSION
743          UInt uiRawBits = getTotalBits(rpcBestCU->getWidth(0), rpcBestCU->getHeight(0), rpcBestCU->getPic()->getChromaFormat(), pcPic->getSlice(0)->getBitDepths().recon);
744#else
745          UInt uiRawBits = getTotalBits(rpcBestCU->getWidth(0), rpcBestCU->getHeight(0), rpcBestCU->getPic()->getChromaFormat(), sps.getBitDepths().recon);
746#endif
747          UInt uiBestBits = rpcBestCU->getTotalBits();
748          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
749          {
750            xCheckIntraPCM (rpcBestCU, rpcTempCU);
751            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
752          }
753        }
754#if ENCODER_FAST_MODE
755#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
756        if(pcPic->getLayerId() > 0 && !m_disableILP)
757#else
758        if(pcPic->getLayerId() > 0)
759#endif
760        {
761          for(Int refLayer = 0; refLayer < pcSlice->getActiveNumILRRefIdx(); refLayer++)
762          { 
763            xCheckRDCostILRUni( rpcBestCU, rpcTempCU, pcSlice->getVPS()->getRefLayerId( pcSlice->getLayerId(), pcSlice->getInterLayerPredLayerIdc(refLayer) ) );
764            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
765          }
766        }
767#endif
768
769        if (bIsLosslessMode) // Restore loop variable if lossless mode was searched.
770        {
771          iQP = iMinQP;
772        }
773      }
774    }
775
776    m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
777    m_pcEntropyCoder->resetBits();
778    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
779    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
780    rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
781    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
782    m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
783
784    // Early CU determination
785    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
786    {
787      bSubBranch = false;
788    }
789    else
790    {
791      bSubBranch = true;
792    }
793#if HIGHER_LAYER_IRAP_SKIP_FLAG
794    }
795#endif
796  }
797  else
798  {
799    bBoundary = true;
800  }
801
802  // copy orginal YUV samples to PCM buffer
803  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
804  {
805    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
806  }
807
808  if( uiDepth == pps.getMaxCuDQPDepth() )
809  {
810    Int idQP = m_pcEncCfg->getMaxDeltaQP();
811#if SVC_EXTENSION
812    iMinQP = Clip3( -rpcTempCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP-idQP );
813    iMaxQP = Clip3( -rpcTempCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP+idQP );
814#else
815    iMinQP = Clip3( -sps.getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP-idQP );
816    iMaxQP = Clip3( -sps.getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP+idQP );
817#endif   
818  }
819  else if( uiDepth < pps.getMaxCuDQPDepth() )
820  {
821    iMinQP = iBaseQP;
822    iMaxQP = iBaseQP;
823  }
824  else
825  {
826    const Int iStartQP = rpcTempCU->getQP(0);
827    iMinQP = iStartQP;
828    iMaxQP = iStartQP;
829  }
830
831  if ( m_pcEncCfg->getUseRateCtrl() )
832  {
833    iMinQP = m_pcRateCtrl->getRCQP();
834    iMaxQP = m_pcRateCtrl->getRCQP();
835  }
836
837  if ( m_pcEncCfg->getCUTransquantBypassFlagForceValue() )
838  {
839    iMaxQP = iMinQP; // If all TUs are forced into using transquant bypass, do not loop here.
840  }
841
842  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
843  {
844    const Bool bIsLosslessMode = false; // False at this level. Next level down may set it to true.
845
846    rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
847
848    // further split
849    if( bSubBranch && uiDepth < sps.getLog2DiffMaxMinCodingBlockSize() )
850    {
851      UChar       uhNextDepth         = uiDepth+1;
852      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
853      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
854      DEBUG_STRING_NEW(sTempDebug)
855
856      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
857      {
858        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
859        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
860
861#if SVC_EXTENSION
862        if( ( pcSubBestPartCU->getCUPelX() < pcSlice->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getPicHeightInLumaSamples() ) )
863#else
864        if( ( pcSubBestPartCU->getCUPelX() < sps.getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < sps.getPicHeightInLumaSamples() ) )
865#endif
866        {
867          if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
868          {
869            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
870          }
871          else
872          {
873            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
874          }
875
876#if AMP_ENC_SPEEDUP
877          DEBUG_STRING_NEW(sChild)
878          if ( !rpcBestCU->isInter(0) )
879          {
880            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth DEBUG_STRING_PASS_INTO(sChild), NUMBER_OF_PART_SIZES );
881          }
882          else
883          {
884
885            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth DEBUG_STRING_PASS_INTO(sChild), rpcBestCU->getPartitionSize(0) );
886          }
887          DEBUG_STRING_APPEND(sTempDebug, sChild)
888#else
889          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
890#endif
891
892          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
893          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
894        }
895        else
896        {
897          pcSubBestPartCU->copyToPic( uhNextDepth );
898          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
899        }
900      }
901
902      m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
903      if( !bBoundary )
904      {
905        m_pcEntropyCoder->resetBits();
906        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
907
908        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
909        rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
910      }
911      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
912
913      if( uiDepth == pps.getMaxCuDQPDepth() && pps.getUseDQP())
914      {
915        Bool hasResidual = false;
916        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
917        {
918          if( (     rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Y)
919                || (rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Cb) && (numberValidComponents > COMPONENT_Cb))
920                || (rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Cr) && (numberValidComponents > COMPONENT_Cr)) ) )
921          {
922            hasResidual = true;
923            break;
924          }
925        }
926
927        UInt uiTargetPartIdx = 0;
928        if ( hasResidual )
929        {
930          m_pcEntropyCoder->resetBits();
931          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
932          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
933          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
934          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
935
936          Bool foundNonZeroCbf = false;
937          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth, foundNonZeroCbf );
938          assert( foundNonZeroCbf );
939        }
940        else
941        {
942          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
943        }
944      }
945
946      m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
947
948      // If the configuration being tested exceeds the maximum number of bytes for a slice / slice-segment, then
949      // a proper RD evaluation cannot be performed. Therefore, termination of the
950      // slice/slice-segment must be made prior to this CTU.
951      // This can be achieved by forcing the decision to be that of the rpcTempCU.
952      // The exception is each slice / slice-segment must have at least one CTU.
953      const Bool isEndOfSlice        =    pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES
954                                       && ((pcSlice->getSliceBits()+rpcBestCU->getTotalBits())>pcSlice->getSliceArgument()<<3)
955                                       && rpcBestCU->getCtuRsAddr() != pcPic->getPicSym()->getCtuTsToRsAddrMap(pcSlice->getSliceCurStartCtuTsAddr())
956                                       && rpcBestCU->getCtuRsAddr() != pcPic->getPicSym()->getCtuTsToRsAddrMap(pcSlice->getSliceSegmentCurStartCtuTsAddr());
957      const Bool isEndOfSliceSegment =    pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
958                                       && ((pcSlice->getSliceSegmentBits()+rpcBestCU->getTotalBits()) > pcSlice->getSliceSegmentArgument()<<3)
959                                       && rpcBestCU->getCtuRsAddr() != pcPic->getPicSym()->getCtuTsToRsAddrMap(pcSlice->getSliceSegmentCurStartCtuTsAddr());
960                                           // Do not need to check slice condition for slice-segment since a slice-segment is a subset of a slice.
961      if(isEndOfSlice||isEndOfSliceSegment)
962      {
963        rpcBestCU->getTotalCost()=MAX_DOUBLE;
964      }
965
966      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTempDebug) DEBUG_STRING_PASS_INTO(false) ); // RD compare current larger prediction
967                                                                                       // with sub partitioned prediction.
968    }
969  }
970
971  DEBUG_STRING_APPEND(sDebug_, sDebug);
972
973  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
974
975  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getCtuRsAddr(), rpcBestCU->getZorderIdxInCtu(), uiDepth, uiDepth );   // Copy Yuv data to picture Yuv
976  if (bBoundary)
977  {
978    return;
979  }
980
981  // Assert if Best prediction mode is NONE
982  // Selected mode's RD-cost must be not MAX_DOUBLE.
983  assert( rpcBestCU->getPartitionSize ( 0 ) != NUMBER_OF_PART_SIZES       );
984  assert( rpcBestCU->getPredictionMode( 0 ) != NUMBER_OF_PREDICTION_MODES );
985  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE                 );
986}
987
988/** finish encoding a cu and handle end-of-slice conditions
989 * \param pcCU
990 * \param uiAbsPartIdx
991 * \param uiDepth
992 * \returns Void
993 */
994Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx )
995{
996  TComPic* pcPic = pcCU->getPic();
997  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
998
999  //Calculate end address
1000  const Int  currentCTUTsAddr = pcPic->getPicSym()->getCtuRsToTsAddrMap(pcCU->getCtuRsAddr());
1001  const Bool isLastSubCUOfCtu = pcCU->isLastSubCUOfCtu(uiAbsPartIdx);
1002  if ( isLastSubCUOfCtu )
1003  {
1004    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1005    // i.e. when the slice segment CurEnd CTU address is the current CTU address+1.
1006    if (pcSlice->getSliceSegmentCurEndCtuTsAddr() != currentCTUTsAddr+1)
1007    {
1008      m_pcEntropyCoder->encodeTerminatingBit( 0 );
1009    }
1010  }
1011}
1012
1013/** Compute QP for each CU
1014 * \param pcCU Target CU
1015 * \param uiDepth CU depth
1016 * \returns quantization parameter
1017 */
1018Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1019{
1020  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1021  Int iQpOffset = 0;
1022  if ( m_pcEncCfg->getUseAdaptiveQP() )
1023  {
1024    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1025    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1026    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1027    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1028    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1029    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1030    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1031
1032    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1033    Double dAvgAct = pcAQLayer->getAvgActivity();
1034    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1035    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1036    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1037    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1038  }
1039#if SVC_EXTENSION
1040  return Clip3(-pcCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQp+iQpOffset );
1041#else
1042  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQp+iQpOffset );
1043#endif
1044}
1045
1046/** encode a CU block recursively
1047 * \param pcCU
1048 * \param uiAbsPartIdx
1049 * \param uiDepth
1050 * \returns Void
1051 */
1052Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1053{
1054        TComPic   *const pcPic   = pcCU->getPic();
1055        TComSlice *const pcSlice = pcCU->getSlice();
1056  const TComSPS   &sps =*(pcSlice->getSPS());
1057  const TComPPS   &pps =*(pcSlice->getPPS());
1058
1059  const UInt maxCUWidth  = sps.getMaxCUWidth();
1060  const UInt maxCUHeight = sps.getMaxCUHeight();
1061
1062        Bool bBoundary = false;
1063        UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1064  const UInt uiRPelX   = uiLPelX + (maxCUWidth>>uiDepth)  - 1;
1065        UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1066  const UInt uiBPelY   = uiTPelY + (maxCUHeight>>uiDepth) - 1;
1067
1068#if HIGHER_LAYER_IRAP_SKIP_FLAG
1069  if (m_pcEncCfg->getSkipPictureAtArcSwitch() && m_pcEncCfg->getAdaptiveResolutionChange() > 0 && pcSlice->getLayerId() == 1 && pcSlice->getPOC() == m_pcEncCfg->getAdaptiveResolutionChange())
1070  {
1071    pcCU->setSkipFlagSubParts(true, uiAbsPartIdx, uiDepth);
1072  }
1073#endif
1074
1075#if SVC_EXTENSION
1076  if( ( uiRPelX < pcSlice->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getPicHeightInLumaSamples() ) )
1077#else
1078  if( ( uiRPelX < sps.getPicWidthInLumaSamples() ) && ( uiBPelY < sps.getPicHeightInLumaSamples() ) )
1079#endif
1080  {
1081    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1082  }
1083  else
1084  {
1085    bBoundary = true;
1086  }
1087
1088  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < sps.getLog2DiffMaxMinCodingBlockSize() ) ) || bBoundary )
1089  {
1090    UInt uiQNumParts = ( pcPic->getNumPartitionsInCtu() >> (uiDepth<<1) )>>2;
1091    if( uiDepth == pps.getMaxCuDQPDepth() && pps.getUseDQP())
1092    {
1093      setdQPFlag(true);
1094    }
1095
1096    if( uiDepth == pps.getPpsRangeExtension().getDiffCuChromaQpOffsetDepth() && pcSlice->getUseChromaQpAdj())
1097    {
1098      setCodeChromaQpAdjFlag(true);
1099    }
1100
1101    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1102    {
1103      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1104      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1105
1106#if SVC_EXTENSION
1107      if( ( uiLPelX < pcSlice->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getPicHeightInLumaSamples() ) )
1108#else
1109      if( ( uiLPelX < sps.getPicWidthInLumaSamples() ) && ( uiTPelY < sps.getPicHeightInLumaSamples() ) )
1110#endif
1111      {
1112        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1113      }
1114    }
1115    return;
1116  }
1117
1118  if( uiDepth <= pps.getMaxCuDQPDepth() && pps.getUseDQP())
1119  {
1120    setdQPFlag(true);
1121  }
1122
1123  if( uiDepth <= pps.getPpsRangeExtension().getDiffCuChromaQpOffsetDepth() && pcSlice->getUseChromaQpAdj())
1124  {
1125    setCodeChromaQpAdjFlag(true);
1126  }
1127
1128  if (pps.getTransquantBypassEnableFlag())
1129  {
1130    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1131  }
1132
1133  if( !pcSlice->isIntra() )
1134  {
1135    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1136  }
1137
1138  if( pcCU->isSkipped( uiAbsPartIdx ) )
1139  {
1140    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1141    finishCU(pcCU,uiAbsPartIdx);
1142    return;
1143  }
1144
1145  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1146  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1147
1148  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1149  {
1150    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1151
1152    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1153    {
1154      // Encode slice finish
1155      finishCU(pcCU,uiAbsPartIdx);
1156      return;
1157    }
1158  }
1159
1160  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1161  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1162
1163  // Encode Coefficients
1164  Bool bCodeDQP = getdQPFlag();
1165  Bool codeChromaQpAdj = getCodeChromaQpAdjFlag();
1166  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, bCodeDQP, codeChromaQpAdj );
1167  setCodeChromaQpAdjFlag( codeChromaQpAdj );
1168  setdQPFlag( bCodeDQP );
1169
1170  // --- write terminating bit ---
1171  finishCU(pcCU,uiAbsPartIdx);
1172}
1173
1174Int xCalcHADs8x8_ISlice(Pel *piOrg, Int iStrideOrg)
1175{
1176  Int k, i, j, jj;
1177  Int diff[64], m1[8][8], m2[8][8], m3[8][8], iSumHad = 0;
1178
1179  for( k = 0; k < 64; k += 8 )
1180  {
1181    diff[k+0] = piOrg[0] ;
1182    diff[k+1] = piOrg[1] ;
1183    diff[k+2] = piOrg[2] ;
1184    diff[k+3] = piOrg[3] ;
1185    diff[k+4] = piOrg[4] ;
1186    diff[k+5] = piOrg[5] ;
1187    diff[k+6] = piOrg[6] ;
1188    diff[k+7] = piOrg[7] ;
1189
1190    piOrg += iStrideOrg;
1191  }
1192
1193  //horizontal
1194  for (j=0; j < 8; j++)
1195  {
1196    jj = j << 3;
1197    m2[j][0] = diff[jj  ] + diff[jj+4];
1198    m2[j][1] = diff[jj+1] + diff[jj+5];
1199    m2[j][2] = diff[jj+2] + diff[jj+6];
1200    m2[j][3] = diff[jj+3] + diff[jj+7];
1201    m2[j][4] = diff[jj  ] - diff[jj+4];
1202    m2[j][5] = diff[jj+1] - diff[jj+5];
1203    m2[j][6] = diff[jj+2] - diff[jj+6];
1204    m2[j][7] = diff[jj+3] - diff[jj+7];
1205
1206    m1[j][0] = m2[j][0] + m2[j][2];
1207    m1[j][1] = m2[j][1] + m2[j][3];
1208    m1[j][2] = m2[j][0] - m2[j][2];
1209    m1[j][3] = m2[j][1] - m2[j][3];
1210    m1[j][4] = m2[j][4] + m2[j][6];
1211    m1[j][5] = m2[j][5] + m2[j][7];
1212    m1[j][6] = m2[j][4] - m2[j][6];
1213    m1[j][7] = m2[j][5] - m2[j][7];
1214
1215    m2[j][0] = m1[j][0] + m1[j][1];
1216    m2[j][1] = m1[j][0] - m1[j][1];
1217    m2[j][2] = m1[j][2] + m1[j][3];
1218    m2[j][3] = m1[j][2] - m1[j][3];
1219    m2[j][4] = m1[j][4] + m1[j][5];
1220    m2[j][5] = m1[j][4] - m1[j][5];
1221    m2[j][6] = m1[j][6] + m1[j][7];
1222    m2[j][7] = m1[j][6] - m1[j][7];
1223  }
1224
1225  //vertical
1226  for (i=0; i < 8; i++)
1227  {
1228    m3[0][i] = m2[0][i] + m2[4][i];
1229    m3[1][i] = m2[1][i] + m2[5][i];
1230    m3[2][i] = m2[2][i] + m2[6][i];
1231    m3[3][i] = m2[3][i] + m2[7][i];
1232    m3[4][i] = m2[0][i] - m2[4][i];
1233    m3[5][i] = m2[1][i] - m2[5][i];
1234    m3[6][i] = m2[2][i] - m2[6][i];
1235    m3[7][i] = m2[3][i] - m2[7][i];
1236
1237    m1[0][i] = m3[0][i] + m3[2][i];
1238    m1[1][i] = m3[1][i] + m3[3][i];
1239    m1[2][i] = m3[0][i] - m3[2][i];
1240    m1[3][i] = m3[1][i] - m3[3][i];
1241    m1[4][i] = m3[4][i] + m3[6][i];
1242    m1[5][i] = m3[5][i] + m3[7][i];
1243    m1[6][i] = m3[4][i] - m3[6][i];
1244    m1[7][i] = m3[5][i] - m3[7][i];
1245
1246    m2[0][i] = m1[0][i] + m1[1][i];
1247    m2[1][i] = m1[0][i] - m1[1][i];
1248    m2[2][i] = m1[2][i] + m1[3][i];
1249    m2[3][i] = m1[2][i] - m1[3][i];
1250    m2[4][i] = m1[4][i] + m1[5][i];
1251    m2[5][i] = m1[4][i] - m1[5][i];
1252    m2[6][i] = m1[6][i] + m1[7][i];
1253    m2[7][i] = m1[6][i] - m1[7][i];
1254  }
1255
1256  for (i = 0; i < 8; i++)
1257  {
1258    for (j = 0; j < 8; j++)
1259    {
1260      iSumHad += abs(m2[i][j]);
1261    }
1262  }
1263  iSumHad -= abs(m2[0][0]);
1264  iSumHad =(iSumHad+2)>>2;
1265  return(iSumHad);
1266}
1267
1268Int  TEncCu::updateCtuDataISlice(TComDataCU* pCtu, Int width, Int height)
1269{
1270  Int  xBl, yBl;
1271  const Int iBlkSize = 8;
1272
1273  Pel* pOrgInit   = pCtu->getPic()->getPicYuvOrg()->getAddr(COMPONENT_Y, pCtu->getCtuRsAddr(), 0);
1274  Int  iStrideOrig = pCtu->getPic()->getPicYuvOrg()->getStride(COMPONENT_Y);
1275  Pel  *pOrg;
1276
1277  Int iSumHad = 0;
1278  for ( yBl=0; (yBl+iBlkSize)<=height; yBl+= iBlkSize)
1279  {
1280    for ( xBl=0; (xBl+iBlkSize)<=width; xBl+= iBlkSize)
1281    {
1282      pOrg = pOrgInit + iStrideOrig*yBl + xBl;
1283      iSumHad += xCalcHADs8x8_ISlice(pOrg, iStrideOrig);
1284    }
1285  }
1286  return(iSumHad);
1287}
1288
1289/** check RD costs for a CU block encoded with merge
1290 * \param rpcBestCU
1291 * \param rpcTempCU
1292 * \param earlyDetectionSkipMode
1293 */
1294#if HIGHER_LAYER_IRAP_SKIP_FLAG
1295Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU DEBUG_STRING_FN_DECLARE(sDebug), Bool *earlyDetectionSkipMode, Bool bUseSkip )
1296#else
1297Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU DEBUG_STRING_FN_DECLARE(sDebug), Bool *earlyDetectionSkipMode )
1298#endif
1299{
1300  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1301  TComMvField  cMvFieldNeighbours[2 * MRG_MAX_NUM_CANDS]; // double length for mv of both lists
1302  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1303  Int numValidMergeCand = 0;
1304  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
1305
1306  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1307  {
1308    uhInterDirNeighbours[ui] = 0;
1309  }
1310  UChar uhDepth = rpcTempCU->getDepth( 0 );
1311  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to CTU level
1312  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1313
1314  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1315  for( UInt ui = 0; ui < numValidMergeCand; ++ui )
1316  {
1317    mergeCandBuffer[ui] = 0;
1318  }
1319
1320  Bool bestIsSkip = false;
1321
1322  UInt iteration;
1323  if ( rpcTempCU->isLosslessCoded(0))
1324  {
1325    iteration = 1;
1326  }
1327  else
1328  {
1329    iteration = 2;
1330  }
1331  DEBUG_STRING_NEW(bestStr)
1332
1333#if HIGHER_LAYER_IRAP_SKIP_FLAG
1334  for( UInt uiNoResidual = bUseSkip?1:0; uiNoResidual < iteration; ++uiNoResidual )
1335#else
1336  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1337#endif
1338  {
1339    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1340    {
1341#if REF_IDX_ME_ZEROMV
1342      Bool bZeroMVILR = rpcTempCU->xCheckZeroMVILRMerge(uhInterDirNeighbours[uiMergeCand], cMvFieldNeighbours[0 + 2*uiMergeCand], cMvFieldNeighbours[1 + 2*uiMergeCand]);
1343      if(bZeroMVILR)
1344      {
1345#endif
1346#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
1347      if (!(rpcTempCU->isInterLayerReference(uhInterDirNeighbours[uiMergeCand], cMvFieldNeighbours[0 + 2*uiMergeCand], cMvFieldNeighbours[1 + 2*uiMergeCand]) && m_disableILP))
1348      {
1349#endif
1350      if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1351      {
1352        if( !(bestIsSkip && uiNoResidual == 0) )
1353        {
1354          DEBUG_STRING_NEW(tmpStr)
1355          // set MC parameters
1356          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to CTU level
1357          rpcTempCU->setCUTransquantBypassSubParts( bTransquantBypassFlag, 0, uhDepth );
1358          rpcTempCU->setChromaQpAdjSubParts( bTransquantBypassFlag ? 0 : m_cuChromaQpOffsetIdxPlus1, 0, uhDepth );
1359          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to CTU level
1360          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to CTU level
1361          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to CTU level
1362          rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to CTU level
1363          rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1364          rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1365
1366          // do MC
1367          m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1368          // estimate residual and encode everything
1369          m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1370                                                     m_ppcOrigYuv    [uhDepth],
1371                                                     m_ppcPredYuvTemp[uhDepth],
1372                                                     m_ppcResiYuvTemp[uhDepth],
1373                                                     m_ppcResiYuvBest[uhDepth],
1374                                                     m_ppcRecoYuvTemp[uhDepth],
1375                                                     (uiNoResidual != 0) DEBUG_STRING_PASS_INTO(tmpStr) );
1376
1377#if DEBUG_STRING
1378          DebugInterPredResiReco(tmpStr, *(m_ppcPredYuvTemp[uhDepth]), *(m_ppcResiYuvBest[uhDepth]), *(m_ppcRecoYuvTemp[uhDepth]), DebugStringGetPredModeMask(rpcTempCU->getPredictionMode(0)));
1379#endif
1380
1381          if ((uiNoResidual == 0) && (rpcTempCU->getQtRootCbf(0) == 0))
1382          {
1383            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
1384            mergeCandBuffer[uiMergeCand] = 1;
1385          }
1386
1387          Int orgQP = rpcTempCU->getQP( 0 );
1388          xCheckDQP( rpcTempCU );
1389          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth DEBUG_STRING_PASS_INTO(bestStr) DEBUG_STRING_PASS_INTO(tmpStr));
1390
1391          rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag );
1392
1393          if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
1394          {
1395            bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
1396          }
1397        }
1398      }
1399#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
1400      }
1401#endif
1402#if REF_IDX_ME_ZEROMV
1403      }
1404#endif
1405    }
1406
1407    if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
1408    {
1409      if(rpcBestCU->getQtRootCbf( 0 ) == 0)
1410      {
1411        if( rpcBestCU->getMergeFlag( 0 ))
1412        {
1413          *earlyDetectionSkipMode = true;
1414        }
1415        else if(m_pcEncCfg->getFastSearch() != SELECTIVE)
1416        {
1417          Int absoulte_MV=0;
1418          for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1419          {
1420            if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1421            {
1422              TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
1423              Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
1424              Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
1425              absoulte_MV+=iHor+iVer;
1426            }
1427          }
1428
1429          if(absoulte_MV == 0)
1430          {
1431            *earlyDetectionSkipMode = true;
1432          }
1433        }
1434      }
1435    }
1436  }
1437  DEBUG_STRING_APPEND(sDebug, bestStr)
1438}
1439
1440
1441#if AMP_MRG
1442Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize DEBUG_STRING_FN_DECLARE(sDebug), Bool bUseMRG)
1443#else
1444Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
1445#endif
1446{
1447  DEBUG_STRING_NEW(sTest)
1448
1449  // prior to this, rpcTempCU will have just been reset using rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1450  UChar uhDepth = rpcTempCU->getDepth( 0 );
1451
1452  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
1453  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
1454  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_cuChromaQpOffsetIdxPlus1, 0, uhDepth );
1455 
1456#if SVC_EXTENSION
1457#if AMP_MRG
1458  rpcTempCU->setMergeAMP (true);
1459  Bool ret = m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] DEBUG_STRING_PASS_INTO(sTest), false, bUseMRG );
1460#else 
1461  Bool ret = m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1462#endif
1463
1464  if( !ret )
1465  {
1466    return;
1467  }
1468#else
1469#if AMP_MRG
1470  rpcTempCU->setMergeAMP (true);
1471  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] DEBUG_STRING_PASS_INTO(sTest), false, bUseMRG );
1472#else
1473  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1474#endif
1475#endif
1476
1477#if AMP_MRG
1478  if ( !rpcTempCU->getMergeAMP() )
1479  {
1480    return;
1481  }
1482#endif
1483
1484  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) );
1485  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1486
1487#if DEBUG_STRING
1488  DebugInterPredResiReco(sTest, *(m_ppcPredYuvTemp[uhDepth]), *(m_ppcResiYuvBest[uhDepth]), *(m_ppcRecoYuvTemp[uhDepth]), DebugStringGetPredModeMask(rpcTempCU->getPredictionMode(0)));
1489#endif
1490
1491  xCheckDQP( rpcTempCU );
1492  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTest));
1493}
1494
1495Void TEncCu::xCheckRDCostIntra( TComDataCU *&rpcBestCU,
1496                                TComDataCU *&rpcTempCU,
1497                                Double      &cost,
1498                                PartSize     eSize
1499                                DEBUG_STRING_FN_DECLARE(sDebug) )
1500{
1501  DEBUG_STRING_NEW(sTest)
1502
1503  UInt uiDepth = rpcTempCU->getDepth( 0 );
1504
1505  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1506
1507  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
1508  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1509  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_cuChromaQpOffsetIdxPlus1, 0, uiDepth );
1510
1511  Pel resiLuma[NUMBER_OF_STORED_RESIDUAL_TYPES][MAX_CU_SIZE * MAX_CU_SIZE];
1512
1513  m_pcPredSearch->estIntraPredLumaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], resiLuma DEBUG_STRING_PASS_INTO(sTest) );
1514
1515  m_ppcRecoYuvTemp[uiDepth]->copyToPicComponent(COMPONENT_Y, rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getCtuRsAddr(), rpcTempCU->getZorderIdxInCtu() );
1516
1517  if (rpcBestCU->getPic()->getChromaFormat()!=CHROMA_400)
1518  {
1519    m_pcPredSearch->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], resiLuma DEBUG_STRING_PASS_INTO(sTest) );
1520  }
1521
1522  m_pcEntropyCoder->resetBits();
1523
1524  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1525  {
1526    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1527  }
1528
1529  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1530  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
1531  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
1532  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0 );
1533  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
1534
1535  // Encode Coefficients
1536  Bool bCodeDQP = getdQPFlag();
1537  Bool codeChromaQpAdjFlag = getCodeChromaQpAdjFlag();
1538  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, bCodeDQP, codeChromaQpAdjFlag );
1539  setCodeChromaQpAdjFlag( codeChromaQpAdjFlag );
1540  setdQPFlag( bCodeDQP );
1541
1542  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1543
1544  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1545  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1546  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1547
1548  xCheckDQP( rpcTempCU );
1549
1550  cost = rpcTempCU->getTotalCost();
1551
1552  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTest));
1553}
1554
1555
1556/** Check R-D costs for a CU with PCM mode.
1557 * \param rpcBestCU pointer to best mode CU data structure
1558 * \param rpcTempCU pointer to testing mode CU data structure
1559 * \returns Void
1560 *
1561 * \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.
1562 */
1563Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
1564{
1565  UInt uiDepth = rpcTempCU->getDepth( 0 );
1566
1567  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1568
1569  rpcTempCU->setIPCMFlag(0, true);
1570  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
1571  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
1572  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1573  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
1574  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_cuChromaQpOffsetIdxPlus1, 0, uiDepth );
1575
1576  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
1577
1578  m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1579
1580  m_pcEntropyCoder->resetBits();
1581
1582  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1583  {
1584    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1585  }
1586
1587  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1588  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
1589  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
1590  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
1591
1592  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1593
1594  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1595  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1596  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1597
1598  xCheckDQP( rpcTempCU );
1599  DEBUG_STRING_NEW(a)
1600  DEBUG_STRING_NEW(b)
1601  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(a) DEBUG_STRING_PASS_INTO(b));
1602}
1603
1604/** check whether current try is the best with identifying the depth of current try
1605 * \param rpcBestCU
1606 * \param rpcTempCU
1607 * \param uiDepth
1608 */
1609Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth DEBUG_STRING_FN_DECLARE(sParent) DEBUG_STRING_FN_DECLARE(sTest) DEBUG_STRING_PASS_INTO(Bool bAddSizeInfo) )
1610{
1611  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
1612  {
1613    TComYuv* pcYuv;
1614    // Change Information data
1615    TComDataCU* pcCU = rpcBestCU;
1616    rpcBestCU = rpcTempCU;
1617    rpcTempCU = pcCU;
1618
1619    // Change Prediction data
1620    pcYuv = m_ppcPredYuvBest[uiDepth];
1621    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
1622    m_ppcPredYuvTemp[uiDepth] = pcYuv;
1623
1624    // Change Reconstruction data
1625    pcYuv = m_ppcRecoYuvBest[uiDepth];
1626    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
1627    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
1628
1629    pcYuv = NULL;
1630    pcCU  = NULL;
1631
1632    // store temp best CI for next CU coding
1633    m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
1634
1635
1636#if DEBUG_STRING
1637    DEBUG_STRING_SWAP(sParent, sTest)
1638    const PredMode predMode=rpcBestCU->getPredictionMode(0);
1639    if ((DebugOptionList::DebugString_Structure.getInt()&DebugStringGetPredModeMask(predMode)) && bAddSizeInfo)
1640    {
1641      std::stringstream ss(stringstream::out);
1642      ss <<"###: " << (predMode==MODE_INTRA?"Intra   ":"Inter   ") << partSizeToString[rpcBestCU->getPartitionSize(0)] << " CU at " << rpcBestCU->getCUPelX() << ", " << rpcBestCU->getCUPelY() << " width=" << UInt(rpcBestCU->getWidth(0)) << std::endl;
1643      sParent+=ss.str();
1644    }
1645#endif
1646  }
1647}
1648
1649Void TEncCu::xCheckDQP( TComDataCU* pcCU )
1650{
1651  UInt uiDepth = pcCU->getDepth( 0 );
1652
1653  const TComPPS &pps = *(pcCU->getSlice()->getPPS());
1654  if ( pps.getUseDQP() && uiDepth <= pps.getMaxCuDQPDepth() )
1655  {
1656    if ( pcCU->getQtRootCbf( 0) )
1657    {
1658      m_pcEntropyCoder->resetBits();
1659      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
1660      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1661      pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1662      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
1663    }
1664    else
1665    {
1666      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
1667    }
1668  }
1669}
1670
1671Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
1672{
1673  pDst->iN = pSrc->iN;
1674  for (Int i = 0; i < pSrc->iN; i++)
1675  {
1676    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
1677  }
1678}
1679Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth )
1680{
1681  UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
1682  UInt uiSrcBlkWidth = rpcPic->getNumPartInCtuWidth() >> (uiSrcDepth);
1683  UInt uiBlkWidth    = rpcPic->getNumPartInCtuWidth() >> (uiDepth);
1684  UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInCtuWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
1685  UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInCtuWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
1686  UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
1687  m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
1688
1689  m_ppcPredYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvPred (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
1690}
1691
1692Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
1693{
1694  UInt uiCurrDepth = uiNextDepth - 1;
1695  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
1696  m_ppcPredYuvBest[uiNextDepth]->copyToPartYuv( m_ppcPredYuvBest[uiCurrDepth], uiPartUnitIdx);
1697}
1698
1699/** Function for filling the PCM buffer of a CU using its original sample array
1700 * \param pCU pointer to current CU
1701 * \param pOrgYuv pointer to original sample array
1702 */
1703Void TEncCu::xFillPCMBuffer     ( TComDataCU* pCU, TComYuv* pOrgYuv )
1704{
1705  const ChromaFormat format = pCU->getPic()->getChromaFormat();
1706  const UInt numberValidComponents = getNumberValidComponents(format);
1707  for (UInt componentIndex = 0; componentIndex < numberValidComponents; componentIndex++)
1708  {
1709    const ComponentID component = ComponentID(componentIndex);
1710
1711    const UInt width  = pCU->getWidth(0)  >> getComponentScaleX(component, format);
1712    const UInt height = pCU->getHeight(0) >> getComponentScaleY(component, format);
1713
1714    Pel *source      = pOrgYuv->getAddr(component, 0, width);
1715    Pel *destination = pCU->getPCMSample(component);
1716
1717    const UInt sourceStride = pOrgYuv->getStride(component);
1718
1719    for (Int line = 0; line < height; line++)
1720    {
1721      for (Int column = 0; column < width; column++)
1722      {
1723        destination[column] = source[column];
1724      }
1725
1726      source      += sourceStride;
1727      destination += width;
1728    }
1729  }
1730}
1731
1732#if ADAPTIVE_QP_SELECTION
1733/** Collect ARL statistics from one block
1734  */
1735Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, TCoeff* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
1736{
1737  for( Int n = 0; n < NumCoeffInCU; n++ )
1738  {
1739    TCoeff u = abs( rpcCoeff[ n ] );
1740    TCoeff absc = rpcArlCoeff[ n ];
1741
1742    if( u != 0 )
1743    {
1744      if( u < LEVEL_RANGE )
1745      {
1746        cSum[ u ] += ( Double )absc;
1747        numSamples[ u ]++;
1748      }
1749      else
1750      {
1751        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
1752        numSamples[ LEVEL_RANGE ]++;
1753      }
1754    }
1755  }
1756
1757  return 0;
1758}
1759
1760//! Collect ARL statistics from one CTU
1761Void TEncCu::xCtuCollectARLStats(TComDataCU* pCtu )
1762{
1763  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to data type and quantization output
1764  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to data type and quantization output
1765
1766  TCoeff* pCoeffY = pCtu->getCoeff(COMPONENT_Y);
1767  TCoeff* pArlCoeffY = pCtu->getArlCoeff(COMPONENT_Y);
1768  const TComSPS &sps = *(pCtu->getSlice()->getSPS());
1769
1770  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.
1771  const UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;                          // NOTE: ed - what is this?
1772
1773  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
1774  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
1775
1776  // Collect stats to cSum[][] and numSamples[][]
1777  for(Int i = 0; i < pCtu->getTotalNumPart(); i ++ )
1778  {
1779    UInt uiTrIdx = pCtu->getTransformIdx(i);
1780
1781    if(pCtu->isInter(i) && pCtu->getCbf( i, COMPONENT_Y, uiTrIdx ) )
1782    {
1783      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
1784    }//Note that only InterY is processed. QP rounding is based on InterY data only.
1785
1786    pCoeffY  += uiMinNumCoeffInCU;
1787    pArlCoeffY  += uiMinNumCoeffInCU;
1788  }
1789
1790  for(Int u=1; u<LEVEL_RANGE;u++)
1791  {
1792    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
1793    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
1794  }
1795  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
1796  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
1797}
1798#endif
1799
1800#if SVC_EXTENSION
1801#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
1802Bool TEncCu::xCheckTileSetConstraint( TComDataCU*& rpcCU )
1803{
1804  Bool disableILP = false;
1805
1806  if (rpcCU->getLayerId() == (m_pcEncCfg->getNumLayer() - 1)  && m_pcEncCfg->getInterLayerConstrainedTileSetsSEIEnabled() && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(rpcCU->getCtuRsAddr()) >= 0)
1807  {
1808    if (rpcCU->getPic()->getPicSym()->getTileSetType(rpcCU->getCtuRsAddr()) == 2)
1809    {
1810      disableILP = true;
1811    }
1812    if (rpcCU->getPic()->getPicSym()->getTileSetType(rpcCU->getCtuRsAddr()) == 1)
1813    {
1814      Int currCUaddr = rpcCU->getCtuRsAddr();
1815      Int frameWitdhInCU  = rpcCU->getPic()->getPicSym()->getFrameWidthInCtus();
1816      Int frameHeightInCU = rpcCU->getPic()->getPicSym()->getFrameHeightInCtus();
1817      Bool leftCUExists   = (currCUaddr % frameWitdhInCU) > 0;
1818      Bool aboveCUExists  = (currCUaddr / frameWitdhInCU) > 0;
1819      Bool rightCUExists  = (currCUaddr % frameWitdhInCU) < (frameWitdhInCU - 1);
1820      Bool belowCUExists  = (currCUaddr / frameWitdhInCU) < (frameHeightInCU - 1);
1821      Int currTileSetIdx  = rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr);
1822      // Check if CU is at tile set boundary
1823      if ( (leftCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr-1) != currTileSetIdx) ||
1824           (leftCUExists && aboveCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr-frameWitdhInCU-1) != currTileSetIdx) ||
1825           (aboveCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr-frameWitdhInCU) != currTileSetIdx) ||
1826           (aboveCUExists && rightCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr-frameWitdhInCU+1) != currTileSetIdx) ||
1827           (rightCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr+1) != currTileSetIdx) ||
1828           (rightCUExists && belowCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr+frameWitdhInCU+1) != currTileSetIdx) ||
1829           (belowCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr+frameWitdhInCU) != currTileSetIdx) ||
1830           (belowCUExists && leftCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr+frameWitdhInCU-1) != currTileSetIdx) )
1831      {
1832        disableILP = true;  // Disable ILP in tile set boundary CU
1833      }
1834    }
1835  }
1836
1837  return disableILP;
1838}
1839
1840Void TEncCu::xVerifyTileSetConstraint( TComDataCU*& rpcCU )
1841{
1842  if (rpcCU->getLayerId() == (m_pcEncCfg->getNumLayer() - 1)  && m_pcEncCfg->getInterLayerConstrainedTileSetsSEIEnabled() && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(rpcCU->getCtuRsAddr()) >= 0 &&
1843      m_disableILP)
1844  {
1845    UInt numPartitions = rpcCU->getPic()->getNumPartitionsInCtu();
1846    for (UInt i = 0; i < numPartitions; i++)
1847    {
1848      if (!rpcCU->isIntra(i))
1849      {
1850        for (UInt refList = 0; refList < 2; refList++)
1851        {
1852          if (rpcCU->getInterDir(i) & (1<<refList))
1853          {
1854            TComCUMvField *mvField = rpcCU->getCUMvField(RefPicList(refList));
1855            if (mvField->getRefIdx(i) >= 0)
1856            {
1857              assert(!(rpcCU->getSlice()->getRefPic(RefPicList(refList), mvField->getRefIdx(i))->isILR(rpcCU->getLayerId())));
1858            }
1859          }
1860        }
1861      }
1862    }
1863  }
1864}
1865#endif
1866
1867#if ENCODER_FAST_MODE
1868Void TEncCu::xCheckRDCostILRUni(TComDataCU *&rpcBestCU, TComDataCU *&rpcTempCU, UInt refLayerId)
1869{
1870  UChar uhDepth = rpcTempCU->getDepth( 0 );
1871  rpcTempCU->setDepthSubParts( uhDepth, 0 );
1872#if SKIP_FLAG
1873  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
1874#endif
1875  rpcTempCU->setPartSizeSubParts  ( SIZE_2Nx2N,  0, uhDepth );  //2Nx2N
1876  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
1877  rpcTempCU->setCUTransquantBypassSubParts  ( m_pcEncCfg->getCUTransquantBypassFlagForceValue(), 0, uhDepth );
1878  Bool exitILR = m_pcPredSearch->predInterSearchILRUni( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], refLayerId );
1879  if(!exitILR)
1880  {
1881     return;
1882  }
1883  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
1884  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1885  xCheckDQP( rpcTempCU );
1886  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1887  return;
1888}
1889#endif
1890#endif //SVC_EXTENSION
1891//! \}
Note: See TracBrowser for help on using the repository browser.