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

Last change on this file since 1313 was 1311, checked in by seregin, 10 years ago

port rev 4385

  • Property svn:eol-style set to native
File size: 69.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_CodeChromaQpAdjFlag = false;
101  m_ChromaQpAdjIdc      = 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_ChromaQpAdjIdc = 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.getMaxCuChromaQpAdjDepth()));
498        m_ChromaQpAdjIdc = ((uiLPelX >> lgMinCuSize) + (uiTPelY >> lgMinCuSize)) % (pps.getChromaQpAdjTableSize() + 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_pcEntropyCoder->resetBits();
777    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
778    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
779    rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
780    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
781
782    // Early CU determination
783    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
784    {
785      bSubBranch = false;
786    }
787    else
788    {
789      bSubBranch = true;
790    }
791#if HIGHER_LAYER_IRAP_SKIP_FLAG
792    }
793#endif
794  }
795  else
796  {
797    bBoundary = true;
798  }
799
800  // copy orginal YUV samples to PCM buffer
801  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
802  {
803    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
804  }
805
806  if( uiDepth == pps.getMaxCuDQPDepth() )
807  {
808    Int idQP = m_pcEncCfg->getMaxDeltaQP();
809#if SVC_EXTENSION
810    iMinQP = Clip3( -rpcTempCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP-idQP );
811    iMaxQP = Clip3( -rpcTempCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP+idQP );
812#else
813    iMinQP = Clip3( -sps.getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP-idQP );
814    iMaxQP = Clip3( -sps.getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP+idQP );
815#endif   
816  }
817  else if( uiDepth < pps.getMaxCuDQPDepth() )
818  {
819    iMinQP = iBaseQP;
820    iMaxQP = iBaseQP;
821  }
822  else
823  {
824    const Int iStartQP = rpcTempCU->getQP(0);
825    iMinQP = iStartQP;
826    iMaxQP = iStartQP;
827  }
828
829  if ( m_pcEncCfg->getUseRateCtrl() )
830  {
831    iMinQP = m_pcRateCtrl->getRCQP();
832    iMaxQP = m_pcRateCtrl->getRCQP();
833  }
834
835  if ( m_pcEncCfg->getCUTransquantBypassFlagForceValue() )
836  {
837    iMaxQP = iMinQP; // If all TUs are forced into using transquant bypass, do not loop here.
838  }
839
840  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
841  {
842    const Bool bIsLosslessMode = false; // False at this level. Next level down may set it to true.
843
844    rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
845
846    // further split
847    if( bSubBranch && uiDepth < sps.getLog2DiffMaxMinCodingBlockSize() )
848    {
849      UChar       uhNextDepth         = uiDepth+1;
850      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
851      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
852      DEBUG_STRING_NEW(sTempDebug)
853
854      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
855      {
856        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
857        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
858
859#if SVC_EXTENSION
860        if( ( pcSubBestPartCU->getCUPelX() < pcSlice->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getPicHeightInLumaSamples() ) )
861#else
862        if( ( pcSubBestPartCU->getCUPelX() < sps.getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < sps.getPicHeightInLumaSamples() ) )
863#endif
864        {
865          if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
866          {
867            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
868          }
869          else
870          {
871            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
872          }
873
874#if AMP_ENC_SPEEDUP
875          DEBUG_STRING_NEW(sChild)
876          if ( !rpcBestCU->isInter(0) )
877          {
878            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth DEBUG_STRING_PASS_INTO(sChild), NUMBER_OF_PART_SIZES );
879          }
880          else
881          {
882
883            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth DEBUG_STRING_PASS_INTO(sChild), rpcBestCU->getPartitionSize(0) );
884          }
885          DEBUG_STRING_APPEND(sTempDebug, sChild)
886#else
887          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
888#endif
889
890          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
891          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
892        }
893        else
894        {
895          pcSubBestPartCU->copyToPic( uhNextDepth );
896          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
897        }
898      }
899
900      if( !bBoundary )
901      {
902        m_pcEntropyCoder->resetBits();
903        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
904
905        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
906        rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
907      }
908      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
909
910      if( uiDepth == pps.getMaxCuDQPDepth() && pps.getUseDQP())
911      {
912        Bool hasResidual = false;
913        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
914        {
915          if( (     rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Y)
916                || (rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Cb) && (numberValidComponents > COMPONENT_Cb))
917                || (rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Cr) && (numberValidComponents > COMPONENT_Cr)) ) )
918          {
919            hasResidual = true;
920            break;
921          }
922        }
923
924        UInt uiTargetPartIdx = 0;
925        if ( hasResidual )
926        {
927#if !RDO_WITHOUT_DQP_BITS
928          m_pcEntropyCoder->resetBits();
929          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
930          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
931          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
932          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
933#endif
934
935          Bool foundNonZeroCbf = false;
936          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth, foundNonZeroCbf );
937          assert( foundNonZeroCbf );
938        }
939        else
940        {
941          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
942        }
943      }
944
945      m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
946
947      // TODO: this does not account for the slice bytes already written. See other instances of FIXED_NUMBER_OF_BYTES
948      Bool isEndOfSlice        = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES
949                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
950      Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
951                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
952      if(isEndOfSlice||isEndOfSliceSegment)
953      {
954        if (m_pcEncCfg->getCostMode()==COST_MIXED_LOSSLESS_LOSSY_CODING)
955        {
956          rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost() + (1.0 / m_pcRdCost->getLambda());
957        }
958        else
959        {
960          rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
961        }
962      }
963
964      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTempDebug) DEBUG_STRING_PASS_INTO(false) ); // RD compare current larger prediction
965                                                                                       // with sub partitioned prediction.
966    }
967  }
968
969  DEBUG_STRING_APPEND(sDebug_, sDebug);
970
971  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
972
973  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getCtuRsAddr(), rpcBestCU->getZorderIdxInCtu(), uiDepth, uiDepth );   // Copy Yuv data to picture Yuv
974  if (bBoundary)
975  {
976    return;
977  }
978
979  // Assert if Best prediction mode is NONE
980  // Selected mode's RD-cost must be not MAX_DOUBLE.
981  assert( rpcBestCU->getPartitionSize ( 0 ) != NUMBER_OF_PART_SIZES       );
982  assert( rpcBestCU->getPredictionMode( 0 ) != NUMBER_OF_PREDICTION_MODES );
983  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE                 );
984}
985
986/** finish encoding a cu and handle end-of-slice conditions
987 * \param pcCU
988 * \param uiAbsPartIdx
989 * \param uiDepth
990 * \returns Void
991 */
992Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx )
993{
994  TComPic* pcPic = pcCU->getPic();
995  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
996
997  //Calculate end address
998  const Int  currentCTUTsAddr = pcPic->getPicSym()->getCtuRsToTsAddrMap(pcCU->getCtuRsAddr());
999  const Bool isLastSubCUOfCtu = pcCU->isLastSubCUOfCtu(uiAbsPartIdx);
1000  if ( isLastSubCUOfCtu )
1001  {
1002    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1003    // i.e. when the slice segment CurEnd CTU address is the current CTU address+1.
1004    if (pcSlice->getSliceSegmentCurEndCtuTsAddr() != currentCTUTsAddr+1)
1005    {
1006      m_pcEntropyCoder->encodeTerminatingBit( 0 );
1007    }
1008  }
1009}
1010
1011/** Compute QP for each CU
1012 * \param pcCU Target CU
1013 * \param uiDepth CU depth
1014 * \returns quantization parameter
1015 */
1016Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1017{
1018  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1019  Int iQpOffset = 0;
1020  if ( m_pcEncCfg->getUseAdaptiveQP() )
1021  {
1022    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1023    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1024    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1025    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1026    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1027    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1028    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1029
1030    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1031    Double dAvgAct = pcAQLayer->getAvgActivity();
1032    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1033    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1034    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1035    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1036  }
1037#if SVC_EXTENSION
1038  return Clip3(-pcCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQp+iQpOffset );
1039#else
1040  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQp+iQpOffset );
1041#endif
1042}
1043
1044/** encode a CU block recursively
1045 * \param pcCU
1046 * \param uiAbsPartIdx
1047 * \param uiDepth
1048 * \returns Void
1049 */
1050Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1051{
1052        TComPic   *const pcPic   = pcCU->getPic();
1053        TComSlice *const pcSlice = pcCU->getSlice();
1054  const TComSPS   &sps =*(pcSlice->getSPS());
1055  const TComPPS   &pps =*(pcSlice->getPPS());
1056
1057  const UInt maxCUWidth  = sps.getMaxCUWidth();
1058  const UInt maxCUHeight = sps.getMaxCUHeight();
1059
1060        Bool bBoundary = false;
1061        UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1062  const UInt uiRPelX   = uiLPelX + (maxCUWidth>>uiDepth)  - 1;
1063        UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1064  const UInt uiBPelY   = uiTPelY + (maxCUHeight>>uiDepth) - 1;
1065
1066#if HIGHER_LAYER_IRAP_SKIP_FLAG
1067  if (m_pcEncCfg->getSkipPictureAtArcSwitch() && m_pcEncCfg->getAdaptiveResolutionChange() > 0 && pcSlice->getLayerId() == 1 && pcSlice->getPOC() == m_pcEncCfg->getAdaptiveResolutionChange())
1068  {
1069    pcCU->setSkipFlagSubParts(true, uiAbsPartIdx, uiDepth);
1070  }
1071#endif
1072
1073#if SVC_EXTENSION
1074  if( ( uiRPelX < pcSlice->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getPicHeightInLumaSamples() ) )
1075#else
1076  if( ( uiRPelX < sps.getPicWidthInLumaSamples() ) && ( uiBPelY < sps.getPicHeightInLumaSamples() ) )
1077#endif
1078  {
1079    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1080  }
1081  else
1082  {
1083    bBoundary = true;
1084  }
1085
1086  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < sps.getLog2DiffMaxMinCodingBlockSize() ) ) || bBoundary )
1087  {
1088    UInt uiQNumParts = ( pcPic->getNumPartitionsInCtu() >> (uiDepth<<1) )>>2;
1089    if( uiDepth == pps.getMaxCuDQPDepth() && pps.getUseDQP())
1090    {
1091      setdQPFlag(true);
1092    }
1093
1094    if( uiDepth == pps.getMaxCuChromaQpAdjDepth() && pcSlice->getUseChromaQpAdj())
1095    {
1096      setCodeChromaQpAdjFlag(true);
1097    }
1098
1099    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1100    {
1101      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1102      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1103
1104#if SVC_EXTENSION
1105      if( ( uiLPelX < pcSlice->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getPicHeightInLumaSamples() ) )
1106#else
1107      if( ( uiLPelX < sps.getPicWidthInLumaSamples() ) && ( uiTPelY < sps.getPicHeightInLumaSamples() ) )
1108#endif
1109      {
1110        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1111      }
1112    }
1113    return;
1114  }
1115
1116  if( uiDepth <= pps.getMaxCuDQPDepth() && pps.getUseDQP())
1117  {
1118    setdQPFlag(true);
1119  }
1120
1121  if( uiDepth <= pps.getMaxCuChromaQpAdjDepth() && pcSlice->getUseChromaQpAdj())
1122  {
1123    setCodeChromaQpAdjFlag(true);
1124  }
1125
1126  if (pps.getTransquantBypassEnableFlag())
1127  {
1128    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1129  }
1130
1131  if( !pcSlice->isIntra() )
1132  {
1133    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1134  }
1135
1136  if( pcCU->isSkipped( uiAbsPartIdx ) )
1137  {
1138    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1139    finishCU(pcCU,uiAbsPartIdx);
1140    return;
1141  }
1142
1143  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1144  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1145
1146  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1147  {
1148    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1149
1150    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1151    {
1152      // Encode slice finish
1153      finishCU(pcCU,uiAbsPartIdx);
1154      return;
1155    }
1156  }
1157
1158  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1159  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1160
1161  // Encode Coefficients
1162  Bool bCodeDQP = getdQPFlag();
1163  Bool codeChromaQpAdj = getCodeChromaQpAdjFlag();
1164  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, bCodeDQP, codeChromaQpAdj );
1165  setCodeChromaQpAdjFlag( codeChromaQpAdj );
1166  setdQPFlag( bCodeDQP );
1167
1168  // --- write terminating bit ---
1169  finishCU(pcCU,uiAbsPartIdx);
1170}
1171
1172Int xCalcHADs8x8_ISlice(Pel *piOrg, Int iStrideOrg)
1173{
1174  Int k, i, j, jj;
1175  Int diff[64], m1[8][8], m2[8][8], m3[8][8], iSumHad = 0;
1176
1177  for( k = 0; k < 64; k += 8 )
1178  {
1179    diff[k+0] = piOrg[0] ;
1180    diff[k+1] = piOrg[1] ;
1181    diff[k+2] = piOrg[2] ;
1182    diff[k+3] = piOrg[3] ;
1183    diff[k+4] = piOrg[4] ;
1184    diff[k+5] = piOrg[5] ;
1185    diff[k+6] = piOrg[6] ;
1186    diff[k+7] = piOrg[7] ;
1187
1188    piOrg += iStrideOrg;
1189  }
1190
1191  //horizontal
1192  for (j=0; j < 8; j++)
1193  {
1194    jj = j << 3;
1195    m2[j][0] = diff[jj  ] + diff[jj+4];
1196    m2[j][1] = diff[jj+1] + diff[jj+5];
1197    m2[j][2] = diff[jj+2] + diff[jj+6];
1198    m2[j][3] = diff[jj+3] + diff[jj+7];
1199    m2[j][4] = diff[jj  ] - diff[jj+4];
1200    m2[j][5] = diff[jj+1] - diff[jj+5];
1201    m2[j][6] = diff[jj+2] - diff[jj+6];
1202    m2[j][7] = diff[jj+3] - diff[jj+7];
1203
1204    m1[j][0] = m2[j][0] + m2[j][2];
1205    m1[j][1] = m2[j][1] + m2[j][3];
1206    m1[j][2] = m2[j][0] - m2[j][2];
1207    m1[j][3] = m2[j][1] - m2[j][3];
1208    m1[j][4] = m2[j][4] + m2[j][6];
1209    m1[j][5] = m2[j][5] + m2[j][7];
1210    m1[j][6] = m2[j][4] - m2[j][6];
1211    m1[j][7] = m2[j][5] - m2[j][7];
1212
1213    m2[j][0] = m1[j][0] + m1[j][1];
1214    m2[j][1] = m1[j][0] - m1[j][1];
1215    m2[j][2] = m1[j][2] + m1[j][3];
1216    m2[j][3] = m1[j][2] - m1[j][3];
1217    m2[j][4] = m1[j][4] + m1[j][5];
1218    m2[j][5] = m1[j][4] - m1[j][5];
1219    m2[j][6] = m1[j][6] + m1[j][7];
1220    m2[j][7] = m1[j][6] - m1[j][7];
1221  }
1222
1223  //vertical
1224  for (i=0; i < 8; i++)
1225  {
1226    m3[0][i] = m2[0][i] + m2[4][i];
1227    m3[1][i] = m2[1][i] + m2[5][i];
1228    m3[2][i] = m2[2][i] + m2[6][i];
1229    m3[3][i] = m2[3][i] + m2[7][i];
1230    m3[4][i] = m2[0][i] - m2[4][i];
1231    m3[5][i] = m2[1][i] - m2[5][i];
1232    m3[6][i] = m2[2][i] - m2[6][i];
1233    m3[7][i] = m2[3][i] - m2[7][i];
1234
1235    m1[0][i] = m3[0][i] + m3[2][i];
1236    m1[1][i] = m3[1][i] + m3[3][i];
1237    m1[2][i] = m3[0][i] - m3[2][i];
1238    m1[3][i] = m3[1][i] - m3[3][i];
1239    m1[4][i] = m3[4][i] + m3[6][i];
1240    m1[5][i] = m3[5][i] + m3[7][i];
1241    m1[6][i] = m3[4][i] - m3[6][i];
1242    m1[7][i] = m3[5][i] - m3[7][i];
1243
1244    m2[0][i] = m1[0][i] + m1[1][i];
1245    m2[1][i] = m1[0][i] - m1[1][i];
1246    m2[2][i] = m1[2][i] + m1[3][i];
1247    m2[3][i] = m1[2][i] - m1[3][i];
1248    m2[4][i] = m1[4][i] + m1[5][i];
1249    m2[5][i] = m1[4][i] - m1[5][i];
1250    m2[6][i] = m1[6][i] + m1[7][i];
1251    m2[7][i] = m1[6][i] - m1[7][i];
1252  }
1253
1254  for (i = 0; i < 8; i++)
1255  {
1256    for (j = 0; j < 8; j++)
1257    {
1258      iSumHad += abs(m2[i][j]);
1259    }
1260  }
1261  iSumHad -= abs(m2[0][0]);
1262  iSumHad =(iSumHad+2)>>2;
1263  return(iSumHad);
1264}
1265
1266Int  TEncCu::updateCtuDataISlice(TComDataCU* pCtu, Int width, Int height)
1267{
1268  Int  xBl, yBl;
1269  const Int iBlkSize = 8;
1270
1271  Pel* pOrgInit   = pCtu->getPic()->getPicYuvOrg()->getAddr(COMPONENT_Y, pCtu->getCtuRsAddr(), 0);
1272  Int  iStrideOrig = pCtu->getPic()->getPicYuvOrg()->getStride(COMPONENT_Y);
1273  Pel  *pOrg;
1274
1275  Int iSumHad = 0;
1276  for ( yBl=0; (yBl+iBlkSize)<=height; yBl+= iBlkSize)
1277  {
1278    for ( xBl=0; (xBl+iBlkSize)<=width; xBl+= iBlkSize)
1279    {
1280      pOrg = pOrgInit + iStrideOrig*yBl + xBl;
1281      iSumHad += xCalcHADs8x8_ISlice(pOrg, iStrideOrig);
1282    }
1283  }
1284  return(iSumHad);
1285}
1286
1287/** check RD costs for a CU block encoded with merge
1288 * \param rpcBestCU
1289 * \param rpcTempCU
1290 * \param earlyDetectionSkipMode
1291 */
1292#if HIGHER_LAYER_IRAP_SKIP_FLAG
1293Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU DEBUG_STRING_FN_DECLARE(sDebug), Bool *earlyDetectionSkipMode, Bool bUseSkip )
1294#else
1295Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU DEBUG_STRING_FN_DECLARE(sDebug), Bool *earlyDetectionSkipMode )
1296#endif
1297{
1298  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1299  TComMvField  cMvFieldNeighbours[2 * MRG_MAX_NUM_CANDS]; // double length for mv of both lists
1300  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1301  Int numValidMergeCand = 0;
1302  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
1303
1304  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1305  {
1306    uhInterDirNeighbours[ui] = 0;
1307  }
1308  UChar uhDepth = rpcTempCU->getDepth( 0 );
1309  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to CTU level
1310  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1311
1312  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1313  for( UInt ui = 0; ui < numValidMergeCand; ++ui )
1314  {
1315    mergeCandBuffer[ui] = 0;
1316  }
1317
1318  Bool bestIsSkip = false;
1319
1320  UInt iteration;
1321  if ( rpcTempCU->isLosslessCoded(0))
1322  {
1323    iteration = 1;
1324  }
1325  else
1326  {
1327    iteration = 2;
1328  }
1329  DEBUG_STRING_NEW(bestStr)
1330
1331#if HIGHER_LAYER_IRAP_SKIP_FLAG
1332  for( UInt uiNoResidual = bUseSkip?1:0; uiNoResidual < iteration; ++uiNoResidual )
1333#else
1334  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1335#endif
1336  {
1337    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1338    {
1339#if REF_IDX_ME_ZEROMV
1340      Bool bZeroMVILR = rpcTempCU->xCheckZeroMVILRMerge(uhInterDirNeighbours[uiMergeCand], cMvFieldNeighbours[0 + 2*uiMergeCand], cMvFieldNeighbours[1 + 2*uiMergeCand]);
1341      if(bZeroMVILR)
1342      {
1343#endif
1344#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
1345      if (!(rpcTempCU->isInterLayerReference(uhInterDirNeighbours[uiMergeCand], cMvFieldNeighbours[0 + 2*uiMergeCand], cMvFieldNeighbours[1 + 2*uiMergeCand]) && m_disableILP))
1346      {
1347#endif
1348      if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1349      {
1350        if( !(bestIsSkip && uiNoResidual == 0) )
1351        {
1352          DEBUG_STRING_NEW(tmpStr)
1353          // set MC parameters
1354          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to CTU level
1355          rpcTempCU->setCUTransquantBypassSubParts( bTransquantBypassFlag, 0, uhDepth );
1356          rpcTempCU->setChromaQpAdjSubParts( bTransquantBypassFlag ? 0 : m_ChromaQpAdjIdc, 0, uhDepth );
1357          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to CTU level
1358          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to CTU level
1359          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to CTU level
1360          rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to CTU level
1361          rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1362          rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1363
1364          // do MC
1365          m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1366          // estimate residual and encode everything
1367          m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1368                                                     m_ppcOrigYuv    [uhDepth],
1369                                                     m_ppcPredYuvTemp[uhDepth],
1370                                                     m_ppcResiYuvTemp[uhDepth],
1371                                                     m_ppcResiYuvBest[uhDepth],
1372                                                     m_ppcRecoYuvTemp[uhDepth],
1373                                                     (uiNoResidual != 0) DEBUG_STRING_PASS_INTO(tmpStr) );
1374
1375#ifdef DEBUG_STRING
1376          DebugInterPredResiReco(tmpStr, *(m_ppcPredYuvTemp[uhDepth]), *(m_ppcResiYuvBest[uhDepth]), *(m_ppcRecoYuvTemp[uhDepth]), DebugStringGetPredModeMask(rpcTempCU->getPredictionMode(0)));
1377#endif
1378
1379          if ((uiNoResidual == 0) && (rpcTempCU->getQtRootCbf(0) == 0))
1380          {
1381            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
1382            mergeCandBuffer[uiMergeCand] = 1;
1383          }
1384
1385          Int orgQP = rpcTempCU->getQP( 0 );
1386          xCheckDQP( rpcTempCU );
1387          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth DEBUG_STRING_PASS_INTO(bestStr) DEBUG_STRING_PASS_INTO(tmpStr));
1388
1389          rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag );
1390
1391          if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
1392          {
1393            bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
1394          }
1395        }
1396      }
1397#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
1398      }
1399#endif
1400#if REF_IDX_ME_ZEROMV
1401      }
1402#endif
1403    }
1404
1405    if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
1406    {
1407      if(rpcBestCU->getQtRootCbf( 0 ) == 0)
1408      {
1409        if( rpcBestCU->getMergeFlag( 0 ))
1410        {
1411          *earlyDetectionSkipMode = true;
1412        }
1413        else if(m_pcEncCfg->getFastSearch() != SELECTIVE)
1414        {
1415          Int absoulte_MV=0;
1416          for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1417          {
1418            if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1419            {
1420              TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
1421              Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
1422              Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
1423              absoulte_MV+=iHor+iVer;
1424            }
1425          }
1426
1427          if(absoulte_MV == 0)
1428          {
1429            *earlyDetectionSkipMode = true;
1430          }
1431        }
1432      }
1433    }
1434  }
1435  DEBUG_STRING_APPEND(sDebug, bestStr)
1436}
1437
1438
1439#if AMP_MRG
1440Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize DEBUG_STRING_FN_DECLARE(sDebug), Bool bUseMRG)
1441#else
1442Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
1443#endif
1444{
1445  DEBUG_STRING_NEW(sTest)
1446
1447  // prior to this, rpcTempCU will have just been reset using rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1448  UChar uhDepth = rpcTempCU->getDepth( 0 );
1449
1450  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
1451  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
1452  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_ChromaQpAdjIdc, 0, uhDepth );
1453 
1454#if SVC_EXTENSION
1455#if AMP_MRG
1456  rpcTempCU->setMergeAMP (true);
1457  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 );
1458#else 
1459  Bool ret = m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1460#endif
1461
1462  if( !ret )
1463  {
1464    return;
1465  }
1466#else
1467#if AMP_MRG
1468  rpcTempCU->setMergeAMP (true);
1469  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] DEBUG_STRING_PASS_INTO(sTest), false, bUseMRG );
1470#else
1471  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1472#endif
1473#endif
1474
1475#if AMP_MRG
1476  if ( !rpcTempCU->getMergeAMP() )
1477  {
1478    return;
1479  }
1480#endif
1481
1482  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) );
1483  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1484
1485#ifdef DEBUG_STRING
1486  DebugInterPredResiReco(sTest, *(m_ppcPredYuvTemp[uhDepth]), *(m_ppcResiYuvBest[uhDepth]), *(m_ppcRecoYuvTemp[uhDepth]), DebugStringGetPredModeMask(rpcTempCU->getPredictionMode(0)));
1487#endif
1488
1489  xCheckDQP( rpcTempCU );
1490  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTest));
1491}
1492
1493Void TEncCu::xCheckRDCostIntra( TComDataCU *&rpcBestCU,
1494                                TComDataCU *&rpcTempCU,
1495                                Double      &cost,
1496                                PartSize     eSize
1497                                DEBUG_STRING_FN_DECLARE(sDebug) )
1498{
1499  DEBUG_STRING_NEW(sTest)
1500
1501  UInt uiDepth = rpcTempCU->getDepth( 0 );
1502
1503  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1504
1505  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
1506  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1507  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_ChromaQpAdjIdc, 0, uiDepth );
1508
1509  Pel resiLuma[NUMBER_OF_STORED_RESIDUAL_TYPES][MAX_CU_SIZE * MAX_CU_SIZE];
1510
1511  m_pcPredSearch->estIntraPredLumaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], resiLuma DEBUG_STRING_PASS_INTO(sTest) );
1512
1513  m_ppcRecoYuvTemp[uiDepth]->copyToPicComponent(COMPONENT_Y, rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getCtuRsAddr(), rpcTempCU->getZorderIdxInCtu() );
1514
1515  if (rpcBestCU->getPic()->getChromaFormat()!=CHROMA_400)
1516  {
1517    m_pcPredSearch->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], resiLuma DEBUG_STRING_PASS_INTO(sTest) );
1518  }
1519
1520  m_pcEntropyCoder->resetBits();
1521
1522  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1523  {
1524    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1525  }
1526
1527  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1528  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
1529  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
1530  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0 );
1531  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
1532
1533  // Encode Coefficients
1534  Bool bCodeDQP = getdQPFlag();
1535  Bool codeChromaQpAdjFlag = getCodeChromaQpAdjFlag();
1536  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, bCodeDQP, codeChromaQpAdjFlag );
1537  setCodeChromaQpAdjFlag( codeChromaQpAdjFlag );
1538  setdQPFlag( bCodeDQP );
1539
1540  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1541
1542  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1543  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1544  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1545
1546  xCheckDQP( rpcTempCU );
1547
1548  cost = rpcTempCU->getTotalCost();
1549
1550  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTest));
1551}
1552
1553
1554/** Check R-D costs for a CU with PCM mode.
1555 * \param rpcBestCU pointer to best mode CU data structure
1556 * \param rpcTempCU pointer to testing mode CU data structure
1557 * \returns Void
1558 *
1559 * \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.
1560 */
1561Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
1562{
1563  UInt uiDepth = rpcTempCU->getDepth( 0 );
1564
1565  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1566
1567  rpcTempCU->setIPCMFlag(0, true);
1568  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
1569  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
1570  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1571  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
1572  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_ChromaQpAdjIdc, 0, uiDepth );
1573
1574  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
1575
1576  m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1577
1578  m_pcEntropyCoder->resetBits();
1579
1580  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1581  {
1582    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1583  }
1584
1585  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1586  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
1587  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
1588  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
1589
1590  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1591
1592  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1593  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1594  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1595
1596  xCheckDQP( rpcTempCU );
1597  DEBUG_STRING_NEW(a)
1598  DEBUG_STRING_NEW(b)
1599  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(a) DEBUG_STRING_PASS_INTO(b));
1600}
1601
1602/** check whether current try is the best with identifying the depth of current try
1603 * \param rpcBestCU
1604 * \param rpcTempCU
1605 * \param uiDepth
1606 */
1607Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth DEBUG_STRING_FN_DECLARE(sParent) DEBUG_STRING_FN_DECLARE(sTest) DEBUG_STRING_PASS_INTO(Bool bAddSizeInfo) )
1608{
1609  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
1610  {
1611    TComYuv* pcYuv;
1612    // Change Information data
1613    TComDataCU* pcCU = rpcBestCU;
1614    rpcBestCU = rpcTempCU;
1615    rpcTempCU = pcCU;
1616
1617    // Change Prediction data
1618    pcYuv = m_ppcPredYuvBest[uiDepth];
1619    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
1620    m_ppcPredYuvTemp[uiDepth] = pcYuv;
1621
1622    // Change Reconstruction data
1623    pcYuv = m_ppcRecoYuvBest[uiDepth];
1624    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
1625    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
1626
1627    pcYuv = NULL;
1628    pcCU  = NULL;
1629
1630    // store temp best CI for next CU coding
1631    m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
1632
1633
1634#ifdef DEBUG_STRING
1635    DEBUG_STRING_SWAP(sParent, sTest)
1636    const PredMode predMode=rpcBestCU->getPredictionMode(0);
1637    if ((DebugOptionList::DebugString_Structure.getInt()&DebugStringGetPredModeMask(predMode)) && bAddSizeInfo)
1638    {
1639      std::stringstream ss(stringstream::out);
1640      ss <<"###: " << (predMode==MODE_INTRA?"Intra   ":"Inter   ") << partSizeToString[rpcBestCU->getPartitionSize(0)] << " CU at " << rpcBestCU->getCUPelX() << ", " << rpcBestCU->getCUPelY() << " width=" << UInt(rpcBestCU->getWidth(0)) << std::endl;
1641      sParent+=ss.str();
1642    }
1643#endif
1644  }
1645}
1646
1647Void TEncCu::xCheckDQP( TComDataCU* pcCU )
1648{
1649  UInt uiDepth = pcCU->getDepth( 0 );
1650
1651  const TComPPS &pps = *(pcCU->getSlice()->getPPS());
1652  if ( pps.getUseDQP() && uiDepth <= pps.getMaxCuDQPDepth() )
1653  {
1654    if ( pcCU->getQtRootCbf( 0) )
1655    {
1656#if !RDO_WITHOUT_DQP_BITS
1657      m_pcEntropyCoder->resetBits();
1658      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
1659      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1660      pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1661      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
1662#endif
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.