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

Last change on this file since 1309 was 1307, checked in by seregin, 10 years ago

port rev 4363

  • Property svn:eol-style set to native
File size: 69.0 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license.
5 *
6 * Copyright (c) 2010-2015, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TEncCu.cpp
35    \brief    Coding Unit (CU) encoder class
36*/
37
38#include <stdio.h>
39#include "TEncTop.h"
40#include "TEncCu.h"
41#include "TEncAnalyze.h"
42#include "TLibCommon/Debug.h"
43
44#include <cmath>
45#include <algorithm>
46using namespace std;
47
48
49//! \ingroup TLibEncoder
50//! \{
51
52// ====================================================================================================================
53// Constructor / destructor / create / destroy
54// ====================================================================================================================
55
56/**
57 \param    uhTotalDepth  total number of allowable depth
58 \param    uiMaxWidth    largest CU width
59 \param    uiMaxHeight   largest CU height
60 \param    chromaFormat  chroma format
61 */
62Void TEncCu::create(UChar uhTotalDepth, UInt uiMaxWidth, UInt uiMaxHeight, ChromaFormat chromaFormat)
63{
64  Int i;
65
66  m_uhTotalDepth   = uhTotalDepth + 1;
67  m_ppcBestCU      = new TComDataCU*[m_uhTotalDepth-1];
68  m_ppcTempCU      = new TComDataCU*[m_uhTotalDepth-1];
69
70  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           (rpcBestCU->getCbf( 0, COMPONENT_Y  ) != 0)                                            ||
718          ((rpcBestCU->getCbf( 0, COMPONENT_Cb ) != 0) && (numberValidComponents > COMPONENT_Cb)) ||
719          ((rpcBestCU->getCbf( 0, COMPONENT_Cr ) != 0) && (numberValidComponents > COMPONENT_Cr))  ) // avoid very complex intra if it is unlikely
720        {
721          xCheckRDCostIntra( rpcBestCU, rpcTempCU, intraCost, SIZE_2Nx2N DEBUG_STRING_PASS_INTO(sDebug) );
722          rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
723          if( uiDepth == sps.getLog2DiffMaxMinCodingBlockSize() )
724          {
725            if( rpcTempCU->getWidth(0) > ( 1 << sps.getQuadtreeTULog2MinSize() ) )
726            {
727              Double tmpIntraCost;
728              xCheckRDCostIntra( rpcBestCU, rpcTempCU, tmpIntraCost, SIZE_NxN DEBUG_STRING_PASS_INTO(sDebug)   );
729              intraCost = std::min(intraCost, tmpIntraCost);
730              rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
731            }
732          }
733        }
734
735        // test PCM
736        if(sps.getUsePCM()
737          && rpcTempCU->getWidth(0) <= (1<<sps.getPCMLog2MaxSize())
738          && rpcTempCU->getWidth(0) >= (1<<sps.getPCMLog2MinSize()) )
739        {
740#if SVC_EXTENSION
741          UInt uiRawBits = getTotalBits(rpcBestCU->getWidth(0), rpcBestCU->getHeight(0), rpcBestCU->getPic()->getChromaFormat(), pcPic->getSlice(0)->getBitDepths().recon);
742#else
743          UInt uiRawBits = getTotalBits(rpcBestCU->getWidth(0), rpcBestCU->getHeight(0), rpcBestCU->getPic()->getChromaFormat(), sps.getBitDepths().recon);
744#endif
745          UInt uiBestBits = rpcBestCU->getTotalBits();
746          if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0)))
747          {
748            xCheckIntraPCM (rpcBestCU, rpcTempCU);
749            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
750          }
751        }
752#if ENCODER_FAST_MODE
753#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
754        if(pcPic->getLayerId() > 0 && !m_disableILP)
755#else
756        if(pcPic->getLayerId() > 0)
757#endif
758        {
759          for(Int refLayer = 0; refLayer < pcSlice->getActiveNumILRRefIdx(); refLayer++)
760          { 
761            xCheckRDCostILRUni( rpcBestCU, rpcTempCU, pcSlice->getVPS()->getRefLayerId( pcSlice->getLayerId(), pcSlice->getInterLayerPredLayerIdc(refLayer) ) );
762            rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
763          }
764        }
765#endif
766
767        if (bIsLosslessMode) // Restore loop variable if lossless mode was searched.
768        {
769          iQP = iMinQP;
770        }
771      }
772    }
773
774    m_pcEntropyCoder->resetBits();
775    m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true );
776    rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
777    rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
778    rpcBestCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() );
779
780    // Early CU determination
781    if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) )
782    {
783      bSubBranch = false;
784    }
785    else
786    {
787      bSubBranch = true;
788    }
789#if HIGHER_LAYER_IRAP_SKIP_FLAG
790    }
791#endif
792  }
793  else
794  {
795    bBoundary = true;
796  }
797
798  // copy orginal YUV samples to PCM buffer
799  if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false))
800  {
801    xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]);
802  }
803
804  if( uiDepth == pps.getMaxCuDQPDepth() )
805  {
806    Int idQP = m_pcEncCfg->getMaxDeltaQP();
807#if SVC_EXTENSION
808    iMinQP = Clip3( -rpcTempCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP-idQP );
809    iMaxQP = Clip3( -rpcTempCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP+idQP );
810#else
811    iMinQP = Clip3( -sps.getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP-idQP );
812    iMaxQP = Clip3( -sps.getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQP+idQP );
813#endif   
814  }
815  else if( uiDepth < pps.getMaxCuDQPDepth() )
816  {
817    iMinQP = iBaseQP;
818    iMaxQP = iBaseQP;
819  }
820  else
821  {
822    const Int iStartQP = rpcTempCU->getQP(0);
823    iMinQP = iStartQP;
824    iMaxQP = iStartQP;
825  }
826
827  if ( m_pcEncCfg->getUseRateCtrl() )
828  {
829    iMinQP = m_pcRateCtrl->getRCQP();
830    iMaxQP = m_pcRateCtrl->getRCQP();
831  }
832
833  if ( m_pcEncCfg->getCUTransquantBypassFlagForceValue() )
834  {
835    iMaxQP = iMinQP; // If all TUs are forced into using transquant bypass, do not loop here.
836  }
837
838  for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++)
839  {
840    const Bool bIsLosslessMode = false; // False at this level. Next level down may set it to true.
841
842    rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
843
844    // further split
845    if( bSubBranch && uiDepth < sps.getLog2DiffMaxMinCodingBlockSize() )
846    {
847      UChar       uhNextDepth         = uiDepth+1;
848      TComDataCU* pcSubBestPartCU     = m_ppcBestCU[uhNextDepth];
849      TComDataCU* pcSubTempPartCU     = m_ppcTempCU[uhNextDepth];
850      DEBUG_STRING_NEW(sTempDebug)
851
852      for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ )
853      {
854        pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
855        pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP );           // clear sub partition datas or init.
856
857#if SVC_EXTENSION
858        if( ( pcSubBestPartCU->getCUPelX() < pcSlice->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getPicHeightInLumaSamples() ) )
859#else
860        if( ( pcSubBestPartCU->getCUPelX() < sps.getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < sps.getPicHeightInLumaSamples() ) )
861#endif
862        {
863          if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
864          {
865            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
866          }
867          else
868          {
869            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
870          }
871
872#if AMP_ENC_SPEEDUP
873          DEBUG_STRING_NEW(sChild)
874          if ( !rpcBestCU->isInter(0) )
875          {
876            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth DEBUG_STRING_PASS_INTO(sChild), NUMBER_OF_PART_SIZES );
877          }
878          else
879          {
880
881            xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth DEBUG_STRING_PASS_INTO(sChild), rpcBestCU->getPartitionSize(0) );
882          }
883          DEBUG_STRING_APPEND(sTempDebug, sChild)
884#else
885          xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth );
886#endif
887
888          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );         // Keep best part data to current temporary data.
889          xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth );
890        }
891        else
892        {
893          pcSubBestPartCU->copyToPic( uhNextDepth );
894          rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth );
895        }
896      }
897
898      if( !bBoundary )
899      {
900        m_pcEntropyCoder->resetBits();
901        m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true );
902
903        rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
904        rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
905      }
906      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
907
908      if( uiDepth == pps.getMaxCuDQPDepth() && pps.getUseDQP())
909      {
910        Bool hasResidual = false;
911        for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++)
912        {
913          if( (     rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Y)
914                || (rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Cb) && (numberValidComponents > COMPONENT_Cb))
915                || (rpcTempCU->getCbf(uiBlkIdx, COMPONENT_Cr) && (numberValidComponents > COMPONENT_Cr)) ) )
916          {
917            hasResidual = true;
918            break;
919          }
920        }
921
922        UInt uiTargetPartIdx = 0;
923        if ( hasResidual )
924        {
925#if !RDO_WITHOUT_DQP_BITS
926          m_pcEntropyCoder->resetBits();
927          m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
928          rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
929          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
930          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
931#endif
932
933          Bool foundNonZeroCbf = false;
934          rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth, foundNonZeroCbf );
935          assert( foundNonZeroCbf );
936        }
937        else
938        {
939          rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP
940        }
941      }
942
943      m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
944
945      // TODO: this does not account for the slice bytes already written. See other instances of FIXED_NUMBER_OF_BYTES
946      Bool isEndOfSlice        = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES
947                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
948      Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
949                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
950      if(isEndOfSlice||isEndOfSliceSegment)
951      {
952        if (m_pcEncCfg->getCostMode()==COST_MIXED_LOSSLESS_LOSSY_CODING)
953        {
954          rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost() + (1.0 / m_pcRdCost->getLambda());
955        }
956        else
957        {
958          rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1;
959        }
960      }
961
962      xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTempDebug) DEBUG_STRING_PASS_INTO(false) ); // RD compare current larger prediction
963                                                                                       // with sub partitioned prediction.
964    }
965  }
966
967  DEBUG_STRING_APPEND(sDebug_, sDebug);
968
969  rpcBestCU->copyToPic(uiDepth);                                                     // Copy Best data to Picture for next partition prediction.
970
971  xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getCtuRsAddr(), rpcBestCU->getZorderIdxInCtu(), uiDepth, uiDepth );   // Copy Yuv data to picture Yuv
972  if (bBoundary)
973  {
974    return;
975  }
976
977  // Assert if Best prediction mode is NONE
978  // Selected mode's RD-cost must be not MAX_DOUBLE.
979  assert( rpcBestCU->getPartitionSize ( 0 ) != NUMBER_OF_PART_SIZES       );
980  assert( rpcBestCU->getPredictionMode( 0 ) != NUMBER_OF_PREDICTION_MODES );
981  assert( rpcBestCU->getTotalCost     (   ) != MAX_DOUBLE                 );
982}
983
984/** finish encoding a cu and handle end-of-slice conditions
985 * \param pcCU
986 * \param uiAbsPartIdx
987 * \param uiDepth
988 * \returns Void
989 */
990Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx )
991{
992  TComPic* pcPic = pcCU->getPic();
993  TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx());
994
995  //Calculate end address
996  const Int  currentCTUTsAddr = pcPic->getPicSym()->getCtuRsToTsAddrMap(pcCU->getCtuRsAddr());
997  const Bool isLastSubCUOfCtu = pcCU->isLastSubCUOfCtu(uiAbsPartIdx);
998  if ( isLastSubCUOfCtu )
999  {
1000    // The 1-terminating bit is added to all streams, so don't add it here when it's 1.
1001    // i.e. when the slice segment CurEnd CTU address is the current CTU address+1.
1002    if (pcSlice->getSliceSegmentCurEndCtuTsAddr() != currentCTUTsAddr+1)
1003    {
1004      m_pcEntropyCoder->encodeTerminatingBit( 0 );
1005    }
1006  }
1007}
1008
1009/** Compute QP for each CU
1010 * \param pcCU Target CU
1011 * \param uiDepth CU depth
1012 * \returns quantization parameter
1013 */
1014Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth )
1015{
1016  Int iBaseQp = pcCU->getSlice()->getSliceQp();
1017  Int iQpOffset = 0;
1018  if ( m_pcEncCfg->getUseAdaptiveQP() )
1019  {
1020    TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() );
1021    UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 );
1022    TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth );
1023    UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth();
1024    UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight();
1025    UInt uiAQUStride = pcAQLayer->getAQPartStride();
1026    TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit();
1027
1028    Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0);
1029    Double dAvgAct = pcAQLayer->getAvgActivity();
1030    Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity();
1031    Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct);
1032    Double dQpOffset = log(dNormAct) / log(2.0) * 6.0;
1033    iQpOffset = Int(floor( dQpOffset + 0.49999 ));
1034  }
1035#if SVC_EXTENSION
1036  return Clip3(-pcCU->getSlice()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQp+iQpOffset );
1037#else
1038  return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, iBaseQp+iQpOffset );
1039#endif
1040}
1041
1042/** encode a CU block recursively
1043 * \param pcCU
1044 * \param uiAbsPartIdx
1045 * \param uiDepth
1046 * \returns Void
1047 */
1048Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1049{
1050        TComPic   *const pcPic   = pcCU->getPic();
1051        TComSlice *const pcSlice = pcCU->getSlice();
1052  const TComSPS   &sps =*(pcSlice->getSPS());
1053  const TComPPS   &pps =*(pcSlice->getPPS());
1054
1055  const UInt maxCUWidth  = sps.getMaxCUWidth();
1056  const UInt maxCUHeight = sps.getMaxCUHeight();
1057
1058        Bool bBoundary = false;
1059        UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1060  const UInt uiRPelX   = uiLPelX + (maxCUWidth>>uiDepth)  - 1;
1061        UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1062  const UInt uiBPelY   = uiTPelY + (maxCUHeight>>uiDepth) - 1;
1063
1064#if HIGHER_LAYER_IRAP_SKIP_FLAG
1065  if (m_pcEncCfg->getSkipPictureAtArcSwitch() && m_pcEncCfg->getAdaptiveResolutionChange() > 0 && pcSlice->getLayerId() == 1 && pcSlice->getPOC() == m_pcEncCfg->getAdaptiveResolutionChange())
1066  {
1067    pcCU->setSkipFlagSubParts(true, uiAbsPartIdx, uiDepth);
1068  }
1069#endif
1070
1071#if SVC_EXTENSION
1072  if( ( uiRPelX < pcSlice->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getPicHeightInLumaSamples() ) )
1073#else
1074  if( ( uiRPelX < sps.getPicWidthInLumaSamples() ) && ( uiBPelY < sps.getPicHeightInLumaSamples() ) )
1075#endif
1076  {
1077    m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth );
1078  }
1079  else
1080  {
1081    bBoundary = true;
1082  }
1083
1084  if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < sps.getLog2DiffMaxMinCodingBlockSize() ) ) || bBoundary )
1085  {
1086    UInt uiQNumParts = ( pcPic->getNumPartitionsInCtu() >> (uiDepth<<1) )>>2;
1087    if( uiDepth == pps.getMaxCuDQPDepth() && pps.getUseDQP())
1088    {
1089      setdQPFlag(true);
1090    }
1091
1092    if( uiDepth == pps.getMaxCuChromaQpAdjDepth() && pcSlice->getUseChromaQpAdj())
1093    {
1094      setCodeChromaQpAdjFlag(true);
1095    }
1096
1097    for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts )
1098    {
1099      uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ];
1100      uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ];
1101
1102#if SVC_EXTENSION
1103      if( ( uiLPelX < pcSlice->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getPicHeightInLumaSamples() ) )
1104#else
1105      if( ( uiLPelX < sps.getPicWidthInLumaSamples() ) && ( uiTPelY < sps.getPicHeightInLumaSamples() ) )
1106#endif
1107      {
1108        xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 );
1109      }
1110    }
1111    return;
1112  }
1113
1114  if( uiDepth <= pps.getMaxCuDQPDepth() && pps.getUseDQP())
1115  {
1116    setdQPFlag(true);
1117  }
1118
1119  if( uiDepth <= pps.getMaxCuChromaQpAdjDepth() && pcSlice->getUseChromaQpAdj())
1120  {
1121    setCodeChromaQpAdjFlag(true);
1122  }
1123
1124  if (pps.getTransquantBypassEnableFlag())
1125  {
1126    m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx );
1127  }
1128
1129  if( !pcSlice->isIntra() )
1130  {
1131    m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx );
1132  }
1133
1134  if( pcCU->isSkipped( uiAbsPartIdx ) )
1135  {
1136    m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx );
1137    finishCU(pcCU,uiAbsPartIdx);
1138    return;
1139  }
1140
1141  m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx );
1142  m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth );
1143
1144  if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
1145  {
1146    m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx );
1147
1148    if(pcCU->getIPCMFlag(uiAbsPartIdx))
1149    {
1150      // Encode slice finish
1151      finishCU(pcCU,uiAbsPartIdx);
1152      return;
1153    }
1154  }
1155
1156  // prediction Info ( Intra : direction mode, Inter : Mv, reference idx )
1157  m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx );
1158
1159  // Encode Coefficients
1160  Bool bCodeDQP = getdQPFlag();
1161  Bool codeChromaQpAdj = getCodeChromaQpAdjFlag();
1162  m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, bCodeDQP, codeChromaQpAdj );
1163  setCodeChromaQpAdjFlag( codeChromaQpAdj );
1164  setdQPFlag( bCodeDQP );
1165
1166  // --- write terminating bit ---
1167  finishCU(pcCU,uiAbsPartIdx);
1168}
1169
1170Int xCalcHADs8x8_ISlice(Pel *piOrg, Int iStrideOrg)
1171{
1172  Int k, i, j, jj;
1173  Int diff[64], m1[8][8], m2[8][8], m3[8][8], iSumHad = 0;
1174
1175  for( k = 0; k < 64; k += 8 )
1176  {
1177    diff[k+0] = piOrg[0] ;
1178    diff[k+1] = piOrg[1] ;
1179    diff[k+2] = piOrg[2] ;
1180    diff[k+3] = piOrg[3] ;
1181    diff[k+4] = piOrg[4] ;
1182    diff[k+5] = piOrg[5] ;
1183    diff[k+6] = piOrg[6] ;
1184    diff[k+7] = piOrg[7] ;
1185
1186    piOrg += iStrideOrg;
1187  }
1188
1189  //horizontal
1190  for (j=0; j < 8; j++)
1191  {
1192    jj = j << 3;
1193    m2[j][0] = diff[jj  ] + diff[jj+4];
1194    m2[j][1] = diff[jj+1] + diff[jj+5];
1195    m2[j][2] = diff[jj+2] + diff[jj+6];
1196    m2[j][3] = diff[jj+3] + diff[jj+7];
1197    m2[j][4] = diff[jj  ] - diff[jj+4];
1198    m2[j][5] = diff[jj+1] - diff[jj+5];
1199    m2[j][6] = diff[jj+2] - diff[jj+6];
1200    m2[j][7] = diff[jj+3] - diff[jj+7];
1201
1202    m1[j][0] = m2[j][0] + m2[j][2];
1203    m1[j][1] = m2[j][1] + m2[j][3];
1204    m1[j][2] = m2[j][0] - m2[j][2];
1205    m1[j][3] = m2[j][1] - m2[j][3];
1206    m1[j][4] = m2[j][4] + m2[j][6];
1207    m1[j][5] = m2[j][5] + m2[j][7];
1208    m1[j][6] = m2[j][4] - m2[j][6];
1209    m1[j][7] = m2[j][5] - m2[j][7];
1210
1211    m2[j][0] = m1[j][0] + m1[j][1];
1212    m2[j][1] = m1[j][0] - m1[j][1];
1213    m2[j][2] = m1[j][2] + m1[j][3];
1214    m2[j][3] = m1[j][2] - m1[j][3];
1215    m2[j][4] = m1[j][4] + m1[j][5];
1216    m2[j][5] = m1[j][4] - m1[j][5];
1217    m2[j][6] = m1[j][6] + m1[j][7];
1218    m2[j][7] = m1[j][6] - m1[j][7];
1219  }
1220
1221  //vertical
1222  for (i=0; i < 8; i++)
1223  {
1224    m3[0][i] = m2[0][i] + m2[4][i];
1225    m3[1][i] = m2[1][i] + m2[5][i];
1226    m3[2][i] = m2[2][i] + m2[6][i];
1227    m3[3][i] = m2[3][i] + m2[7][i];
1228    m3[4][i] = m2[0][i] - m2[4][i];
1229    m3[5][i] = m2[1][i] - m2[5][i];
1230    m3[6][i] = m2[2][i] - m2[6][i];
1231    m3[7][i] = m2[3][i] - m2[7][i];
1232
1233    m1[0][i] = m3[0][i] + m3[2][i];
1234    m1[1][i] = m3[1][i] + m3[3][i];
1235    m1[2][i] = m3[0][i] - m3[2][i];
1236    m1[3][i] = m3[1][i] - m3[3][i];
1237    m1[4][i] = m3[4][i] + m3[6][i];
1238    m1[5][i] = m3[5][i] + m3[7][i];
1239    m1[6][i] = m3[4][i] - m3[6][i];
1240    m1[7][i] = m3[5][i] - m3[7][i];
1241
1242    m2[0][i] = m1[0][i] + m1[1][i];
1243    m2[1][i] = m1[0][i] - m1[1][i];
1244    m2[2][i] = m1[2][i] + m1[3][i];
1245    m2[3][i] = m1[2][i] - m1[3][i];
1246    m2[4][i] = m1[4][i] + m1[5][i];
1247    m2[5][i] = m1[4][i] - m1[5][i];
1248    m2[6][i] = m1[6][i] + m1[7][i];
1249    m2[7][i] = m1[6][i] - m1[7][i];
1250  }
1251
1252  for (i = 0; i < 8; i++)
1253  {
1254    for (j = 0; j < 8; j++)
1255    {
1256      iSumHad += abs(m2[i][j]);
1257    }
1258  }
1259  iSumHad -= abs(m2[0][0]);
1260  iSumHad =(iSumHad+2)>>2;
1261  return(iSumHad);
1262}
1263
1264Int  TEncCu::updateCtuDataISlice(TComDataCU* pCtu, Int width, Int height)
1265{
1266  Int  xBl, yBl;
1267  const Int iBlkSize = 8;
1268
1269  Pel* pOrgInit   = pCtu->getPic()->getPicYuvOrg()->getAddr(COMPONENT_Y, pCtu->getCtuRsAddr(), 0);
1270  Int  iStrideOrig = pCtu->getPic()->getPicYuvOrg()->getStride(COMPONENT_Y);
1271  Pel  *pOrg;
1272
1273  Int iSumHad = 0;
1274  for ( yBl=0; (yBl+iBlkSize)<=height; yBl+= iBlkSize)
1275  {
1276    for ( xBl=0; (xBl+iBlkSize)<=width; xBl+= iBlkSize)
1277    {
1278      pOrg = pOrgInit + iStrideOrig*yBl + xBl;
1279      iSumHad += xCalcHADs8x8_ISlice(pOrg, iStrideOrig);
1280    }
1281  }
1282  return(iSumHad);
1283}
1284
1285/** check RD costs for a CU block encoded with merge
1286 * \param rpcBestCU
1287 * \param rpcTempCU
1288 * \param earlyDetectionSkipMode
1289 */
1290#if HIGHER_LAYER_IRAP_SKIP_FLAG
1291Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU DEBUG_STRING_FN_DECLARE(sDebug), Bool *earlyDetectionSkipMode, Bool bUseSkip )
1292#else
1293Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU DEBUG_STRING_FN_DECLARE(sDebug), Bool *earlyDetectionSkipMode )
1294#endif
1295{
1296  assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE );
1297  TComMvField  cMvFieldNeighbours[2 * MRG_MAX_NUM_CANDS]; // double length for mv of both lists
1298  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
1299  Int numValidMergeCand = 0;
1300  const Bool bTransquantBypassFlag = rpcTempCU->getCUTransquantBypass(0);
1301
1302  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
1303  {
1304    uhInterDirNeighbours[ui] = 0;
1305  }
1306  UChar uhDepth = rpcTempCU->getDepth( 0 );
1307  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to CTU level
1308  rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
1309
1310  Int mergeCandBuffer[MRG_MAX_NUM_CANDS];
1311  for( UInt ui = 0; ui < numValidMergeCand; ++ui )
1312  {
1313    mergeCandBuffer[ui] = 0;
1314  }
1315
1316  Bool bestIsSkip = false;
1317
1318  UInt iteration;
1319  if ( rpcTempCU->isLosslessCoded(0))
1320  {
1321    iteration = 1;
1322  }
1323  else
1324  {
1325    iteration = 2;
1326  }
1327  DEBUG_STRING_NEW(bestStr)
1328
1329#if HIGHER_LAYER_IRAP_SKIP_FLAG
1330  for( UInt uiNoResidual = bUseSkip?1:0; uiNoResidual < iteration; ++uiNoResidual )
1331#else
1332  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
1333#endif
1334  {
1335    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
1336    {
1337#if REF_IDX_ME_ZEROMV
1338      Bool bZeroMVILR = rpcTempCU->xCheckZeroMVILRMerge(uhInterDirNeighbours[uiMergeCand], cMvFieldNeighbours[0 + 2*uiMergeCand], cMvFieldNeighbours[1 + 2*uiMergeCand]);
1339      if(bZeroMVILR)
1340      {
1341#endif
1342#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
1343      if (!(rpcTempCU->isInterLayerReference(uhInterDirNeighbours[uiMergeCand], cMvFieldNeighbours[0 + 2*uiMergeCand], cMvFieldNeighbours[1 + 2*uiMergeCand]) && m_disableILP))
1344      {
1345#endif
1346      if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
1347      {
1348        if( !(bestIsSkip && uiNoResidual == 0) )
1349        {
1350          DEBUG_STRING_NEW(tmpStr)
1351          // set MC parameters
1352          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to CTU level
1353          rpcTempCU->setCUTransquantBypassSubParts( bTransquantBypassFlag, 0, uhDepth );
1354          rpcTempCU->setChromaQpAdjSubParts( bTransquantBypassFlag ? 0 : m_ChromaQpAdjIdc, 0, uhDepth );
1355          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to CTU level
1356          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to CTU level
1357          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to CTU level
1358          rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to CTU level
1359          rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1360          rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
1361
1362          // do MC
1363          m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
1364          // estimate residual and encode everything
1365          m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
1366                                                     m_ppcOrigYuv    [uhDepth],
1367                                                     m_ppcPredYuvTemp[uhDepth],
1368                                                     m_ppcResiYuvTemp[uhDepth],
1369                                                     m_ppcResiYuvBest[uhDepth],
1370                                                     m_ppcRecoYuvTemp[uhDepth],
1371                                                     (uiNoResidual != 0) DEBUG_STRING_PASS_INTO(tmpStr) );
1372
1373#ifdef DEBUG_STRING
1374          DebugInterPredResiReco(tmpStr, *(m_ppcPredYuvTemp[uhDepth]), *(m_ppcResiYuvBest[uhDepth]), *(m_ppcRecoYuvTemp[uhDepth]), DebugStringGetPredModeMask(rpcTempCU->getPredictionMode(0)));
1375#endif
1376
1377          if ((uiNoResidual == 0) && (rpcTempCU->getQtRootCbf(0) == 0))
1378          {
1379            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
1380            mergeCandBuffer[uiMergeCand] = 1;
1381          }
1382
1383          Int orgQP = rpcTempCU->getQP( 0 );
1384          xCheckDQP( rpcTempCU );
1385          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth DEBUG_STRING_PASS_INTO(bestStr) DEBUG_STRING_PASS_INTO(tmpStr));
1386
1387          rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag );
1388
1389          if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
1390          {
1391            bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
1392          }
1393        }
1394      }
1395#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
1396      }
1397#endif
1398#if REF_IDX_ME_ZEROMV
1399      }
1400#endif
1401    }
1402
1403    if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
1404    {
1405      if(rpcBestCU->getQtRootCbf( 0 ) == 0)
1406      {
1407        if( rpcBestCU->getMergeFlag( 0 ))
1408        {
1409          *earlyDetectionSkipMode = true;
1410        }
1411        else if(m_pcEncCfg->getFastSearch() != SELECTIVE)
1412        {
1413          Int absoulte_MV=0;
1414          for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
1415          {
1416            if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
1417            {
1418              TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
1419              Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
1420              Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
1421              absoulte_MV+=iHor+iVer;
1422            }
1423          }
1424
1425          if(absoulte_MV == 0)
1426          {
1427            *earlyDetectionSkipMode = true;
1428          }
1429        }
1430      }
1431    }
1432  }
1433  DEBUG_STRING_APPEND(sDebug, bestStr)
1434}
1435
1436
1437#if AMP_MRG
1438Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize DEBUG_STRING_FN_DECLARE(sDebug), Bool bUseMRG)
1439#else
1440Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize )
1441#endif
1442{
1443  DEBUG_STRING_NEW(sTest)
1444
1445  // prior to this, rpcTempCU will have just been reset using rpcTempCU->initEstData( uiDepth, iQP, bIsLosslessMode );
1446  UChar uhDepth = rpcTempCU->getDepth( 0 );
1447
1448  rpcTempCU->setPartSizeSubParts  ( ePartSize,  0, uhDepth );
1449  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
1450  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_ChromaQpAdjIdc, 0, uhDepth );
1451 
1452#if SVC_EXTENSION
1453#if AMP_MRG
1454  rpcTempCU->setMergeAMP (true);
1455  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 );
1456#else 
1457  Bool ret = m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1458#endif
1459
1460  if( !ret )
1461  {
1462    return;
1463  }
1464#else
1465#if AMP_MRG
1466  rpcTempCU->setMergeAMP (true);
1467  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] DEBUG_STRING_PASS_INTO(sTest), false, bUseMRG );
1468#else
1469  m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] );
1470#endif
1471#endif
1472
1473#if AMP_MRG
1474  if ( !rpcTempCU->getMergeAMP() )
1475  {
1476    return;
1477  }
1478#endif
1479
1480  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) );
1481  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1482
1483#ifdef DEBUG_STRING
1484  DebugInterPredResiReco(sTest, *(m_ppcPredYuvTemp[uhDepth]), *(m_ppcResiYuvBest[uhDepth]), *(m_ppcRecoYuvTemp[uhDepth]), DebugStringGetPredModeMask(rpcTempCU->getPredictionMode(0)));
1485#endif
1486
1487  xCheckDQP( rpcTempCU );
1488  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTest));
1489}
1490
1491Void TEncCu::xCheckRDCostIntra( TComDataCU *&rpcBestCU,
1492                                TComDataCU *&rpcTempCU,
1493                                Double      &cost,
1494                                PartSize     eSize
1495                                DEBUG_STRING_FN_DECLARE(sDebug) )
1496{
1497  DEBUG_STRING_NEW(sTest)
1498
1499  UInt uiDepth = rpcTempCU->getDepth( 0 );
1500
1501  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1502
1503  rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth );
1504  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1505  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_ChromaQpAdjIdc, 0, uiDepth );
1506
1507  Pel resiLuma[NUMBER_OF_STORED_RESIDUAL_TYPES][MAX_CU_SIZE * MAX_CU_SIZE];
1508
1509  m_pcPredSearch->estIntraPredLumaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], resiLuma DEBUG_STRING_PASS_INTO(sTest) );
1510
1511  m_ppcRecoYuvTemp[uiDepth]->copyToPicComponent(COMPONENT_Y, rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getCtuRsAddr(), rpcTempCU->getZorderIdxInCtu() );
1512
1513  if (rpcBestCU->getPic()->getChromaFormat()!=CHROMA_400)
1514  {
1515    m_pcPredSearch->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], resiLuma DEBUG_STRING_PASS_INTO(sTest) );
1516  }
1517
1518  m_pcEntropyCoder->resetBits();
1519
1520  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1521  {
1522    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1523  }
1524
1525  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1526  m_pcEntropyCoder->encodePredMode( rpcTempCU, 0,          true );
1527  m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true );
1528  m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0 );
1529  m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true );
1530
1531  // Encode Coefficients
1532  Bool bCodeDQP = getdQPFlag();
1533  Bool codeChromaQpAdjFlag = getCodeChromaQpAdjFlag();
1534  m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, bCodeDQP, codeChromaQpAdjFlag );
1535  setCodeChromaQpAdjFlag( codeChromaQpAdjFlag );
1536  setdQPFlag( bCodeDQP );
1537
1538  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1539
1540  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1541  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1542  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1543
1544  xCheckDQP( rpcTempCU );
1545
1546  cost = rpcTempCU->getTotalCost();
1547
1548  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(sDebug) DEBUG_STRING_PASS_INTO(sTest));
1549}
1550
1551
1552/** Check R-D costs for a CU with PCM mode.
1553 * \param rpcBestCU pointer to best mode CU data structure
1554 * \param rpcTempCU pointer to testing mode CU data structure
1555 * \returns Void
1556 *
1557 * \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.
1558 */
1559Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU )
1560{
1561  UInt uiDepth = rpcTempCU->getDepth( 0 );
1562
1563  rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth );
1564
1565  rpcTempCU->setIPCMFlag(0, true);
1566  rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0));
1567  rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
1568  rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth );
1569  rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth );
1570  rpcTempCU->setChromaQpAdjSubParts( rpcTempCU->getCUTransquantBypass(0) ? 0 : m_ChromaQpAdjIdc, 0, uiDepth );
1571
1572  m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]);
1573
1574  m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
1575
1576  m_pcEntropyCoder->resetBits();
1577
1578  if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
1579  {
1580    m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0,          true );
1581  }
1582
1583  m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0,          true );
1584  m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0,          true );
1585  m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true );
1586  m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true );
1587
1588  m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
1589
1590  rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits();
1591  rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1592  rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1593
1594  xCheckDQP( rpcTempCU );
1595  DEBUG_STRING_NEW(a)
1596  DEBUG_STRING_NEW(b)
1597  xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth DEBUG_STRING_PASS_INTO(a) DEBUG_STRING_PASS_INTO(b));
1598}
1599
1600/** check whether current try is the best with identifying the depth of current try
1601 * \param rpcBestCU
1602 * \param rpcTempCU
1603 * \param uiDepth
1604 */
1605Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth DEBUG_STRING_FN_DECLARE(sParent) DEBUG_STRING_FN_DECLARE(sTest) DEBUG_STRING_PASS_INTO(Bool bAddSizeInfo) )
1606{
1607  if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() )
1608  {
1609    TComYuv* pcYuv;
1610    // Change Information data
1611    TComDataCU* pcCU = rpcBestCU;
1612    rpcBestCU = rpcTempCU;
1613    rpcTempCU = pcCU;
1614
1615    // Change Prediction data
1616    pcYuv = m_ppcPredYuvBest[uiDepth];
1617    m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth];
1618    m_ppcPredYuvTemp[uiDepth] = pcYuv;
1619
1620    // Change Reconstruction data
1621    pcYuv = m_ppcRecoYuvBest[uiDepth];
1622    m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth];
1623    m_ppcRecoYuvTemp[uiDepth] = pcYuv;
1624
1625    pcYuv = NULL;
1626    pcCU  = NULL;
1627
1628    // store temp best CI for next CU coding
1629    m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]);
1630
1631
1632#ifdef DEBUG_STRING
1633    DEBUG_STRING_SWAP(sParent, sTest)
1634    const PredMode predMode=rpcBestCU->getPredictionMode(0);
1635    if ((DebugOptionList::DebugString_Structure.getInt()&DebugStringGetPredModeMask(predMode)) && bAddSizeInfo)
1636    {
1637      std::stringstream ss(stringstream::out);
1638      ss <<"###: " << (predMode==MODE_INTRA?"Intra   ":"Inter   ") << partSizeToString[rpcBestCU->getPartitionSize(0)] << " CU at " << rpcBestCU->getCUPelX() << ", " << rpcBestCU->getCUPelY() << " width=" << UInt(rpcBestCU->getWidth(0)) << std::endl;
1639      sParent+=ss.str();
1640    }
1641#endif
1642  }
1643}
1644
1645Void TEncCu::xCheckDQP( TComDataCU* pcCU )
1646{
1647  UInt uiDepth = pcCU->getDepth( 0 );
1648
1649  const TComPPS &pps = *(pcCU->getSlice()->getPPS());
1650  if ( pps.getUseDQP() && uiDepth <= pps.getMaxCuDQPDepth() )
1651  {
1652    if ( pcCU->getQtRootCbf( 0) )
1653    {
1654#if !RDO_WITHOUT_DQP_BITS
1655      m_pcEntropyCoder->resetBits();
1656      m_pcEntropyCoder->encodeQP( pcCU, 0, false );
1657      pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
1658      pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
1659      pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() );
1660#endif
1661    }
1662    else
1663    {
1664      pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP
1665    }
1666  }
1667}
1668
1669Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst)
1670{
1671  pDst->iN = pSrc->iN;
1672  for (Int i = 0; i < pSrc->iN; i++)
1673  {
1674    pDst->m_acMvCand[i] = pSrc->m_acMvCand[i];
1675  }
1676}
1677Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth )
1678{
1679  UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx];
1680  UInt uiSrcBlkWidth = rpcPic->getNumPartInCtuWidth() >> (uiSrcDepth);
1681  UInt uiBlkWidth    = rpcPic->getNumPartInCtuWidth() >> (uiDepth);
1682  UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInCtuWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
1683  UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInCtuWidth() ) % uiSrcBlkWidth) / uiBlkWidth;
1684  UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX;
1685  m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
1686
1687  m_ppcPredYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvPred (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx);
1688}
1689
1690Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth )
1691{
1692  UInt uiCurrDepth = uiNextDepth - 1;
1693  m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx );
1694  m_ppcPredYuvBest[uiNextDepth]->copyToPartYuv( m_ppcPredYuvBest[uiCurrDepth], uiPartUnitIdx);
1695}
1696
1697/** Function for filling the PCM buffer of a CU using its original sample array
1698 * \param pCU pointer to current CU
1699 * \param pOrgYuv pointer to original sample array
1700 */
1701Void TEncCu::xFillPCMBuffer     ( TComDataCU* pCU, TComYuv* pOrgYuv )
1702{
1703  const ChromaFormat format = pCU->getPic()->getChromaFormat();
1704  const UInt numberValidComponents = getNumberValidComponents(format);
1705  for (UInt componentIndex = 0; componentIndex < numberValidComponents; componentIndex++)
1706  {
1707    const ComponentID component = ComponentID(componentIndex);
1708
1709    const UInt width  = pCU->getWidth(0)  >> getComponentScaleX(component, format);
1710    const UInt height = pCU->getHeight(0) >> getComponentScaleY(component, format);
1711
1712    Pel *source      = pOrgYuv->getAddr(component, 0, width);
1713    Pel *destination = pCU->getPCMSample(component);
1714
1715    const UInt sourceStride = pOrgYuv->getStride(component);
1716
1717    for (Int line = 0; line < height; line++)
1718    {
1719      for (Int column = 0; column < width; column++)
1720      {
1721        destination[column] = source[column];
1722      }
1723
1724      source      += sourceStride;
1725      destination += width;
1726    }
1727  }
1728}
1729
1730#if ADAPTIVE_QP_SELECTION
1731/** Collect ARL statistics from one block
1732  */
1733Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, TCoeff* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples )
1734{
1735  for( Int n = 0; n < NumCoeffInCU; n++ )
1736  {
1737    TCoeff u = abs( rpcCoeff[ n ] );
1738    TCoeff absc = rpcArlCoeff[ n ];
1739
1740    if( u != 0 )
1741    {
1742      if( u < LEVEL_RANGE )
1743      {
1744        cSum[ u ] += ( Double )absc;
1745        numSamples[ u ]++;
1746      }
1747      else
1748      {
1749        cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION );
1750        numSamples[ LEVEL_RANGE ]++;
1751      }
1752    }
1753  }
1754
1755  return 0;
1756}
1757
1758//! Collect ARL statistics from one CTU
1759Void TEncCu::xCtuCollectARLStats(TComDataCU* pCtu )
1760{
1761  Double cSum[ LEVEL_RANGE + 1 ];     //: the sum of DCT coefficients corresponding to data type and quantization output
1762  UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to data type and quantization output
1763
1764  TCoeff* pCoeffY = pCtu->getCoeff(COMPONENT_Y);
1765  TCoeff* pArlCoeffY = pCtu->getArlCoeff(COMPONENT_Y);
1766  const TComSPS &sps = *(pCtu->getSlice()->getSPS());
1767
1768  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.
1769  const UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth;                          // NOTE: ed - what is this?
1770
1771  memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) );
1772  memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) );
1773
1774  // Collect stats to cSum[][] and numSamples[][]
1775  for(Int i = 0; i < pCtu->getTotalNumPart(); i ++ )
1776  {
1777    UInt uiTrIdx = pCtu->getTransformIdx(i);
1778
1779    if(pCtu->isInter(i) && pCtu->getCbf( i, COMPONENT_Y, uiTrIdx ) )
1780    {
1781      xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples);
1782    }//Note that only InterY is processed. QP rounding is based on InterY data only.
1783
1784    pCoeffY  += uiMinNumCoeffInCU;
1785    pArlCoeffY  += uiMinNumCoeffInCU;
1786  }
1787
1788  for(Int u=1; u<LEVEL_RANGE;u++)
1789  {
1790    m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ;
1791    m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ;
1792  }
1793  m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ;
1794  m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ;
1795}
1796#endif
1797
1798#if SVC_EXTENSION
1799#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
1800Bool TEncCu::xCheckTileSetConstraint( TComDataCU*& rpcCU )
1801{
1802  Bool disableILP = false;
1803
1804  if (rpcCU->getLayerId() == (m_pcEncCfg->getNumLayer() - 1)  && m_pcEncCfg->getInterLayerConstrainedTileSetsSEIEnabled() && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(rpcCU->getCtuRsAddr()) >= 0)
1805  {
1806    if (rpcCU->getPic()->getPicSym()->getTileSetType(rpcCU->getCtuRsAddr()) == 2)
1807    {
1808      disableILP = true;
1809    }
1810    if (rpcCU->getPic()->getPicSym()->getTileSetType(rpcCU->getCtuRsAddr()) == 1)
1811    {
1812      Int currCUaddr = rpcCU->getCtuRsAddr();
1813      Int frameWitdhInCU  = rpcCU->getPic()->getPicSym()->getFrameWidthInCtus();
1814      Int frameHeightInCU = rpcCU->getPic()->getPicSym()->getFrameHeightInCtus();
1815      Bool leftCUExists   = (currCUaddr % frameWitdhInCU) > 0;
1816      Bool aboveCUExists  = (currCUaddr / frameWitdhInCU) > 0;
1817      Bool rightCUExists  = (currCUaddr % frameWitdhInCU) < (frameWitdhInCU - 1);
1818      Bool belowCUExists  = (currCUaddr / frameWitdhInCU) < (frameHeightInCU - 1);
1819      Int currTileSetIdx  = rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr);
1820      // Check if CU is at tile set boundary
1821      if ( (leftCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr-1) != currTileSetIdx) ||
1822           (leftCUExists && aboveCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr-frameWitdhInCU-1) != currTileSetIdx) ||
1823           (aboveCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr-frameWitdhInCU) != currTileSetIdx) ||
1824           (aboveCUExists && rightCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr-frameWitdhInCU+1) != currTileSetIdx) ||
1825           (rightCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr+1) != currTileSetIdx) ||
1826           (rightCUExists && belowCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr+frameWitdhInCU+1) != currTileSetIdx) ||
1827           (belowCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr+frameWitdhInCU) != currTileSetIdx) ||
1828           (belowCUExists && leftCUExists && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(currCUaddr+frameWitdhInCU-1) != currTileSetIdx) )
1829      {
1830        disableILP = true;  // Disable ILP in tile set boundary CU
1831      }
1832    }
1833  }
1834
1835  return disableILP;
1836}
1837
1838Void TEncCu::xVerifyTileSetConstraint( TComDataCU*& rpcCU )
1839{
1840  if (rpcCU->getLayerId() == (m_pcEncCfg->getNumLayer() - 1)  && m_pcEncCfg->getInterLayerConstrainedTileSetsSEIEnabled() && rpcCU->getPic()->getPicSym()->getTileSetIdxMap(rpcCU->getCtuRsAddr()) >= 0 &&
1841      m_disableILP)
1842  {
1843    UInt numPartitions = rpcCU->getPic()->getNumPartitionsInCtu();
1844    for (UInt i = 0; i < numPartitions; i++)
1845    {
1846      if (!rpcCU->isIntra(i))
1847      {
1848        for (UInt refList = 0; refList < 2; refList++)
1849        {
1850          if (rpcCU->getInterDir(i) & (1<<refList))
1851          {
1852            TComCUMvField *mvField = rpcCU->getCUMvField(RefPicList(refList));
1853            if (mvField->getRefIdx(i) >= 0)
1854            {
1855              assert(!(rpcCU->getSlice()->getRefPic(RefPicList(refList), mvField->getRefIdx(i))->isILR(rpcCU->getLayerId())));
1856            }
1857          }
1858        }
1859      }
1860    }
1861  }
1862}
1863#endif
1864
1865#if ENCODER_FAST_MODE
1866Void TEncCu::xCheckRDCostILRUni(TComDataCU *&rpcBestCU, TComDataCU *&rpcTempCU, UInt refLayerId)
1867{
1868  UChar uhDepth = rpcTempCU->getDepth( 0 );
1869  rpcTempCU->setDepthSubParts( uhDepth, 0 );
1870#if SKIP_FLAG
1871  rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
1872#endif
1873  rpcTempCU->setPartSizeSubParts  ( SIZE_2Nx2N,  0, uhDepth );  //2Nx2N
1874  rpcTempCU->setPredModeSubParts  ( MODE_INTER, 0, uhDepth );
1875  rpcTempCU->setCUTransquantBypassSubParts  ( m_pcEncCfg->getCUTransquantBypassFlagForceValue(), 0, uhDepth );
1876  Bool exitILR = m_pcPredSearch->predInterSearchILRUni( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], refLayerId );
1877  if(!exitILR)
1878  {
1879     return;
1880  }
1881  m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false );
1882  rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
1883  xCheckDQP( rpcTempCU );
1884  xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
1885  return;
1886}
1887#endif
1888#endif //SVC_EXTENSION
1889//! \}
Note: See TracBrowser for help on using the repository browser.