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

Last change on this file since 1502 was 1502, checked in by seregin, 8 years ago

infer parameters in SPS after activation, fixing chroma scaling for non 4:2:0

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