source: SHVCSoftware/branches/SHM-dev/source/Lib/TLibEncoder/TEncTop.cpp @ 1497

Last change on this file since 1497 was 1459, checked in by seregin, 9 years ago

port rev 4593

  • Property svn:eol-style set to native
File size: 60.2 KB
RevLine 
[313]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
[1029]4 * granted under this license.
[313]5 *
[1259]6 * Copyright (c) 2010-2015, ITU/ISO/IEC
[313]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     TEncTop.cpp
35    \brief    encoder class
36*/
37
38#include "TLibCommon/CommonDef.h"
39#include "TEncTop.h"
40#include "TEncPic.h"
[1029]41#include "TLibCommon/TComChromaFormat.h"
[313]42#if FAST_BIT_EST
43#include "TLibCommon/ContextModel.h"
44#endif
45
46//! \ingroup TLibEncoder
47//! \{
48#if SVC_EXTENSION 
49Int TEncTop::m_iSPSIdCnt = 0;
50Int TEncTop::m_iPPSIdCnt = 0;
51TComVPS TEncCfg::m_cVPS;
52#endif
53
54// ====================================================================================================================
55// Constructor / destructor / create / destroy
56// ====================================================================================================================
57
58TEncTop::TEncTop()
59{
60  m_iPOCLast          = -1;
61  m_iNumPicRcvd       =  0;
62  m_uiNumAllPicCoded  =  0;
63  m_pppcRDSbacCoder   =  NULL;
64  m_pppcBinCoderCABAC =  NULL;
65  m_cRDGoOnSbacCoder.init( &m_cRDGoOnBinCoderCABAC );
66#if ENC_DEC_TRACE
[1029]67  if (g_hTrace == NULL)
68  {
69    g_hTrace = fopen( "TraceEnc.txt", "wb" );
70  }
[313]71  g_bJustDoIt = g_bEncDecTraceDisable;
72  g_nSymbolCounter = 0;
73#endif
74
75  m_iMaxRefPicNum     = 0;
76
77#if FAST_BIT_EST
78  ContextModel::buildNextStateTable();
79#endif
80
[442]81#if SVC_EXTENSION
[1226]82  memset( m_cIlpPic, 0, sizeof(m_cIlpPic) );
[313]83  m_bMFMEnabledFlag = false;
[1030]84  m_numRefLayerLocationOffsets = 0;
[442]85  m_pocAdjustmentValue     = 0;
[540]86#if NO_CLRAS_OUTPUT_FLAG
87  m_noClrasOutputFlag          = false;
88  m_layerInitializedFlag       = false;
89  m_firstPicInLayerDecodedFlag = false;
90  m_noOutputOfPriorPicsFlags   = false;
91#endif
[903]92  m_pocDecrementedInDPBFlag    = false;
[494]93#endif //SVC_EXTENSION
[313]94}
95
96TEncTop::~TEncTop()
97{
98#if ENC_DEC_TRACE
[1029]99  if (g_hTrace != stdout)
100  {
101    fclose( g_hTrace );
102  }
[313]103#endif
104}
105
106Void TEncTop::create ()
107{
108#if !SVC_EXTENSION
109  // initialize global variables
110  initROM();
111#endif
[442]112
[313]113  // create processing unit classes
114#if SVC_EXTENSION
115  m_cGOPEncoder.        create( m_layerId );
116#else
[1029]117  m_cGOPEncoder.        create( );
[313]118#endif
[1290]119  m_cSliceEncoder.      create( getSourceWidth(), getSourceHeight(), m_chromaFormatIDC, m_maxCUWidth, m_maxCUHeight, m_maxTotalCUDepth );
120  m_cCuEncoder.         create( m_maxTotalCUDepth, m_maxCUWidth, m_maxCUHeight, m_chromaFormatIDC );
[313]121  if (m_bUseSAO)
122  {
[1316]123    m_cEncSAO.create( getSourceWidth(), getSourceHeight(), m_chromaFormatIDC, m_maxCUWidth, m_maxCUHeight, m_maxTotalCUDepth, m_log2SaoOffsetScale[CHANNEL_TYPE_LUMA], m_log2SaoOffsetScale[CHANNEL_TYPE_CHROMA] );
[1029]124    m_cEncSAO.createEncData(getSaoCtuBoundary());
[313]125  }
126#if ADAPTIVE_QP_SELECTION
127  if (m_bUseAdaptQpSelect)
128  {
129    m_cTrQuant.initSliceQpDelta();
130  }
131#endif
[1029]132
[1290]133  m_cLoopFilter.create( m_maxTotalCUDepth );
[1029]134
[313]135  if ( m_RCEnableRateControl )
136  {
137    m_cRateCtrl.init( m_framesToBeEncoded, m_RCTargetBitrate, m_iFrameRate, m_iGOPSize, m_iSourceWidth, m_iSourceHeight,
[1289]138        m_maxCUWidth, m_maxCUHeight, m_RCKeepHierarchicalBit, m_RCUseLCUSeparateModel, m_GOPList );
[313]139  }
[595]140
[1290]141  m_pppcRDSbacCoder = new TEncSbac** [m_maxTotalCUDepth+1];
[313]142#if FAST_BIT_EST
[1290]143  m_pppcBinCoderCABAC = new TEncBinCABACCounter** [m_maxTotalCUDepth+1];
[313]144#else
[1290]145  m_pppcBinCoderCABAC = new TEncBinCABAC** [m_maxTotalCUDepth+1];
[313]146#endif
[595]147
[1290]148  for ( Int iDepth = 0; iDepth < m_maxTotalCUDepth+1; iDepth++ )
[595]149  {
150    m_pppcRDSbacCoder[iDepth] = new TEncSbac* [CI_NUM];
[313]151#if FAST_BIT_EST
[595]152    m_pppcBinCoderCABAC[iDepth] = new TEncBinCABACCounter* [CI_NUM];
[313]153#else
[595]154    m_pppcBinCoderCABAC[iDepth] = new TEncBinCABAC* [CI_NUM];
[313]155#endif
[595]156
157    for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
158    {
159      m_pppcRDSbacCoder[iDepth][iCIIdx] = new TEncSbac;
[313]160#if FAST_BIT_EST
[595]161      m_pppcBinCoderCABAC [iDepth][iCIIdx] = new TEncBinCABACCounter;
[313]162#else
[595]163      m_pppcBinCoderCABAC [iDepth][iCIIdx] = new TEncBinCABAC;
[313]164#endif
[595]165      m_pppcRDSbacCoder   [iDepth][iCIIdx]->init( m_pppcBinCoderCABAC [iDepth][iCIIdx] );
[313]166    }
167  }
[494]168
169#if LAYER_CTB
170  memcpy(g_auiLayerZscanToRaster[m_layerId], g_auiZscanToRaster, sizeof( g_auiZscanToRaster ) );
171  memcpy(g_auiLayerRasterToZscan[m_layerId], g_auiRasterToZscan, sizeof( g_auiRasterToZscan ) );
172  memcpy(g_auiLayerRasterToPelX[m_layerId],  g_auiRasterToPelX,  sizeof( g_auiRasterToPelX ) );
173  memcpy(g_auiLayerRasterToPelY[m_layerId],  g_auiRasterToPelY,  sizeof( g_auiRasterToPelY ) );
174#endif
[313]175}
176
177Void TEncTop::destroy ()
178{
179  // destroy processing unit classes
180  m_cGOPEncoder.        destroy();
181  m_cSliceEncoder.      destroy();
182  m_cCuEncoder.         destroy();
[1235]183  m_cEncSAO.            destroyEncData();
184  m_cEncSAO.            destroy();
[313]185  m_cLoopFilter.        destroy();
186  m_cRateCtrl.          destroy();
[1372]187  m_cSearch.            destroy();
[595]188  Int iDepth;
[1290]189  for ( iDepth = 0; iDepth < m_maxTotalCUDepth+1; iDepth++ )
[313]190  {
[595]191    for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
192    {
193      delete m_pppcRDSbacCoder[iDepth][iCIIdx];
194      delete m_pppcBinCoderCABAC[iDepth][iCIIdx];
195    }
196  }
197
[1290]198  for ( iDepth = 0; iDepth < m_maxTotalCUDepth+1; iDepth++ )
[595]199  {
200    delete [] m_pppcRDSbacCoder[iDepth];
201    delete [] m_pppcBinCoderCABAC[iDepth];
202  }
203
204  delete [] m_pppcRDSbacCoder;
205  delete [] m_pppcBinCoderCABAC;
[1459]206
[442]207#if SVC_EXTENSION
[313]208  for(Int i=0; i<MAX_NUM_REF; i++)
209  {
210    if(m_cIlpPic[i])
211    {
212      m_cIlpPic[i]->destroy();
213      delete m_cIlpPic[i];
214      m_cIlpPic[i] = NULL;
215    }
[442]216  }
217#else
218  // destroy ROM
219  destroyROM();
[313]220#endif
221  return;
222}
223
[442]224Void TEncTop::init(Bool isFieldCoding)
[313]225{
226  // initialize SPS
227  xInitSPS();
[1235]228  xInitVPS();
[1029]229
[1433]230#if U0132_TARGET_BITS_SATURATION
231  if (m_RCCpbSaturationEnabled)
232  {
233    m_cRateCtrl.initHrdParam(m_cSPS.getVuiParameters()->getHrdParameters(), m_iFrameRate, m_RCInitialCpbFullness);
234  }
235#endif
[1029]236  m_cRdCost.setCostMode(m_costMode);
237
[313]238  // initialize PPS
239  xInitPPS();
[442]240  xInitRPS(isFieldCoding);
[313]241
242  xInitPPSforTiles();
243
244  // initialize processing unit classes
245  m_cGOPEncoder.  init( this );
246  m_cSliceEncoder.init( this );
247  m_cCuEncoder.   init( this );
[1029]248
[313]249  // initialize transform & quantization class
250  m_pcCavlcCoder = getCavlcCoder();
[1029]251
[313]252  m_cTrQuant.init( 1 << m_uiQuadtreeTULog2MaxSize,
[1029]253                   m_useRDOQ,
254                   m_useRDOQTS,
[1298]255#if T0196_SELECTIVE_RDOQ
256                   m_useSelectiveRDOQ,
257#endif
[1029]258                   true
[313]259                  ,m_useTransformSkipFast
[1029]260#if ADAPTIVE_QP_SELECTION
261                  ,m_bUseAdaptQpSelect
[313]262#endif
263                  );
[1029]264
[313]265  // initialize encoder search class
[1406]266  m_cSearch.init( this, &m_cTrQuant, m_iSearchRange, m_bipredSearchRange, m_motionEstimationSearchMethod, m_maxCUWidth, m_maxCUHeight, m_maxTotalCUDepth, &m_cEntropyCoder, &m_cRdCost, getRDSbacCoder(), getRDGoOnSbacCoder() );
[313]267
268  m_iMaxRefPicNum = 0;
[1235]269
270  xInitScalingLists();
271
[313]272#if SVC_EXTENSION
273  m_iSPSIdCnt ++;
274  m_iPPSIdCnt ++;
275  xInitILRP();
276#endif
277}
278
[1235]279Void TEncTop::xInitScalingLists()
280{
281  // Initialise scaling lists
282  // The encoder will only use the SPS scaling lists. The PPS will never be marked present.
[1285]283  const Int maxLog2TrDynamicRange[MAX_NUM_CHANNEL_TYPE] =
284  {
285      m_cSPS.getMaxLog2TrDynamicRange(CHANNEL_TYPE_LUMA),
286      m_cSPS.getMaxLog2TrDynamicRange(CHANNEL_TYPE_CHROMA)
287  };
[1235]288  if(getUseScalingListId() == SCALING_LIST_OFF)
289  {
290#if SVC_EXTENSION
[1307]291    getTrQuant()->setFlatScalingList( maxLog2TrDynamicRange, m_cVPS.getBitDepths(&m_cSPS, m_layerId) );
[1235]292#else
[1307]293    getTrQuant()->setFlatScalingList(maxLog2TrDynamicRange, m_cSPS.getBitDepths());
[1235]294#endif
295    getTrQuant()->setUseScalingList(false);
296    m_cSPS.setScalingListPresentFlag(false);
297    m_cPPS.setScalingListPresentFlag(false);
298  }
299  else if(getUseScalingListId() == SCALING_LIST_DEFAULT)
300  {
301#if SVC_EXTENSION
302    // inferring of the scaling list can be moved to the config file
303    UInt refLayerId = 0;
304    if( m_layerId > 0 && !m_cVPS.getNonHEVCBaseLayerFlag() && m_cVPS.getRecursiveRefLayerFlag( m_layerId, refLayerId ) )
305    {
306      m_cSPS.setInferScalingListFlag( true );
307      m_cSPS.setScalingListRefLayerId( refLayerId );
308      m_cSPS.setScalingListPresentFlag( false );
309      m_cPPS.setInferScalingListFlag( false );
310      m_cPPS.setScalingListPresentFlag( false );
311
312      // infer the scaling list from the reference layer
[1307]313      getTrQuant()->setScalingList( &m_ppcTEncTop[m_cVPS.getLayerIdxInVps(refLayerId)]->getSPS()->getScalingList(), maxLog2TrDynamicRange, m_cVPS.getBitDepths(&m_cSPS, m_layerId) );
[1235]314    }
315    else
316    {
317#endif
318    m_cSPS.getScalingList().setDefaultScalingList ();
319    m_cSPS.setScalingListPresentFlag(false);
320    m_cPPS.setScalingListPresentFlag(false);
[1285]321
[1235]322#if SVC_EXTENSION
[1307]323    getTrQuant()->setScalingList(&(m_cSPS.getScalingList()), maxLog2TrDynamicRange, m_cVPS.getBitDepths(&m_cSPS, m_layerId));
324#else
325    getTrQuant()->setScalingList(&(m_cSPS.getScalingList()), maxLog2TrDynamicRange, m_cSPS.getBitDepths());
326#endif
327#if SVC_EXTENSION
[1235]328    }
329#endif
330    getTrQuant()->setUseScalingList(true);
331  }
332  else if(getUseScalingListId() == SCALING_LIST_FILE_READ)
333  {
334#if SVC_EXTENSION
335    // inferring of the scaling list can be moved to the config file
336    UInt refLayerId = 0;
337    if( m_layerId > 0 && !m_cVPS.getNonHEVCBaseLayerFlag() && m_cVPS.getRecursiveRefLayerFlag( m_layerId, refLayerId ) )
338    {
339      m_cSPS.setInferScalingListFlag( true );
340      m_cSPS.setScalingListRefLayerId( refLayerId );
341      m_cSPS.setScalingListPresentFlag( false );
342      m_cPPS.setInferScalingListFlag( false );
343      m_cPPS.setScalingListPresentFlag( false );
344
345      // infer the scaling list from the reference layer
[1307]346      getTrQuant()->setScalingList( &m_ppcTEncTop[m_cVPS.getLayerIdxInVps(refLayerId)]->getSPS()->getScalingList(), maxLog2TrDynamicRange, m_cVPS.getBitDepths(&m_cSPS, m_layerId) );
[1235]347    }
348    else
349    {
350#endif
351    m_cSPS.getScalingList().setDefaultScalingList ();
[1442]352    if(m_cSPS.getScalingList().xParseScalingList(getScalingListFileName()))
[1235]353    {
354      Bool bParsedScalingList=false; // Use of boolean so that assertion outputs useful string
355      assert(bParsedScalingList);
356      exit(1);
357    }
358    m_cSPS.getScalingList().checkDcOfMatrix();
359    m_cSPS.setScalingListPresentFlag(m_cSPS.getScalingList().checkDefaultScalingList());
360    m_cPPS.setScalingListPresentFlag(false);
[1307]361    getTrQuant()->setScalingList(&(m_cSPS.getScalingList()), maxLog2TrDynamicRange, m_cSPS.getBitDepths());
[1235]362#if SVC_EXTENSION
363    }
364#endif
365    getTrQuant()->setUseScalingList(true);
366  }
367  else
368  {
369    printf("error : ScalingList == %d not supported\n",getUseScalingListId());
370    assert(0);
371  }
372
[1309]373  if (getUseScalingListId() != SCALING_LIST_OFF)
[1235]374  {
[1309]375    // Prepare delta's:
376    for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
377    {
378      const Int predListStep = (sizeId == SCALING_LIST_32x32? (SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) : 1); // if 32x32, skip over chroma entries.
[1235]379
[1309]380      for(UInt listId = 0; listId < SCALING_LIST_NUM; listId+=predListStep)
381      {
382        m_cSPS.getScalingList().checkPredMode( sizeId, listId );
383      }
[1235]384    }
385  }
386}
387
[313]388// ====================================================================================================================
389// Public member functions
390// ====================================================================================================================
391
392Void TEncTop::deletePicBuffer()
393{
394  TComList<TComPic*>::iterator iterPic = m_cListPic.begin();
395  Int iSize = Int( m_cListPic.size() );
[1029]396
[313]397  for ( Int i = 0; i < iSize; i++ )
398  {
399    TComPic* pcPic = *(iterPic++);
[1029]400
[313]401    pcPic->destroy();
402    delete pcPic;
403    pcPic = NULL;
404  }
405}
406
407/**
408 - Application has picture buffer list with size of GOP + 1
409 - Picture buffer list acts like as ring buffer
410 - End of the list has the latest picture
411 .
412 \param   flush               cause encoder to encode a partial GOP
413 \param   pcPicYuvOrg         original YUV picture
[1260]414 \param   pcPicYuvTrueOrg     
415 \param   snrCSC
[313]416 \retval  rcListPicYuvRecOut  list of reconstruction YUV pictures
[1260]417 \retval  accessUnitsOut      list of output access units
[313]418 \retval  iNumEncoded         number of encoded pictures
419 */
420#if SVC_EXTENSION
[1029]421Void TEncTop::encode( TComPicYuv* pcPicYuvOrg, const InputColourSpaceConversion snrCSC, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int iPicIdInGOP )
[313]422{
423  // compress GOP
424#if !RC_SHVC_HARMONIZATION
425  if ( m_RCEnableRateControl )
426  {
427    m_cRateCtrl.initRCGOP( m_iNumPicRcvd );
428  }
429#endif
430
431  // compress GOP
[1029]432  m_cGOPEncoder.compressGOP(iPicIdInGOP, m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, false, false, snrCSC, m_printFrameMSE);
[313]433
434#if !RC_SHVC_HARMONIZATION
435  if ( m_RCEnableRateControl )
436  {
437    m_cRateCtrl.destroyRCGOP();
438  }
439#endif
440 
441  m_uiNumAllPicCoded ++;
442}
443
[1029]444Void TEncTop::encodePrep( TComPicYuv* pcPicYuvOrg, TComPicYuv* pcPicYuvTrueOrg )
[313]445{
[1029]446  if (pcPicYuvOrg != NULL)
[313]447  {
448    // get original YUV
449    TComPic* pcPicCurr = NULL;
450    xGetNewPicBuffer( pcPicCurr );
451    pcPicYuvOrg->copyToPic( pcPicCurr->getPicYuvOrg() );
[1029]452    pcPicYuvTrueOrg->copyToPic( pcPicCurr->getPicYuvTrueOrg() );
[313]453
454    // compute image characteristics
455    if ( getUseAdaptiveQP() )
456    {
457      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcPicCurr ) );
458    }
459  }
460}
461#else
[1029]462Void TEncTop::encode( Bool flush, TComPicYuv* pcPicYuvOrg, TComPicYuv* pcPicYuvTrueOrg, const InputColourSpaceConversion snrCSC, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int& iNumEncoded )
[313]463{
[1029]464  if (pcPicYuvOrg != NULL)
465  {
[313]466    // get original YUV
467    TComPic* pcPicCurr = NULL;
[1029]468
[313]469    xGetNewPicBuffer( pcPicCurr );
470    pcPicYuvOrg->copyToPic( pcPicCurr->getPicYuvOrg() );
[1029]471    pcPicYuvTrueOrg->copyToPic( pcPicCurr->getPicYuvTrueOrg() );
[313]472
473    // compute image characteristics
474    if ( getUseAdaptiveQP() )
475    {
476      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcPicCurr ) );
477    }
478  }
[1029]479
480  if ((m_iNumPicRcvd == 0) || (!flush && (m_iPOCLast != 0) && (m_iNumPicRcvd != m_iGOPSize) && (m_iGOPSize != 0)))
[313]481  {
482    iNumEncoded = 0;
483    return;
484  }
[1029]485
[313]486  if ( m_RCEnableRateControl )
487  {
488    m_cRateCtrl.initRCGOP( m_iNumPicRcvd );
489  }
490
491  // compress GOP
[1029]492  m_cGOPEncoder.compressGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, false, false, snrCSC, m_printFrameMSE);
[313]493
494  if ( m_RCEnableRateControl )
495  {
496    m_cRateCtrl.destroyRCGOP();
497  }
[1029]498
[313]499  iNumEncoded         = m_iNumPicRcvd;
500  m_iNumPicRcvd       = 0;
501  m_uiNumAllPicCoded += iNumEncoded;
502}
503#endif
504
[442]505/**------------------------------------------------
506 Separate interlaced frame into two fields
507 -------------------------------------------------**/
[1029]508Void separateFields(Pel* org, Pel* dstField, UInt stride, UInt width, UInt height, Bool isTop)
[442]509{
510  if (!isTop)
511  {
512    org += stride;
513  }
514  for (Int y = 0; y < height>>1; y++)
515  {
516    for (Int x = 0; x < width; x++)
517    {
518      dstField[x] = org[x];
519    }
[1029]520
[442]521    dstField += stride;
522    org += stride*2;
523  }
[1029]524
[442]525}
526
527#if SVC_EXTENSION
[1029]528Void TEncTop::encodePrep( TComPicYuv* pcPicYuvOrg, TComPicYuv* pcPicYuvTrueOrg, Bool isTff )
[442]529{
[1029]530  for (Int fieldNum=0; fieldNum<2; fieldNum++)
[442]531  {
[1029]532    if (pcPicYuvOrg)
[442]533    {
[1029]534      /* -- field initialization -- */
535      const Bool isTopField=isTff==(fieldNum==0);
[540]536
[1029]537      TComPic *pcField;
538      xGetNewPicBuffer( pcField );
539      pcField->setReconMark (false);                     // where is this normally?
540           
541      pcField->getSlice(0)->setPOC( m_iPOCLast );        // superfluous?
542      pcField->getPicYuvRec()->setBorderExtension(false);// where is this normally?
[442]543
[1029]544      pcField->setTopField(isTopField);                  // interlaced requirement
[540]545
[1029]546      for (UInt componentIndex = 0; componentIndex < pcPicYuvOrg->getNumberValidComponents(); componentIndex++)
547      {
548        const ComponentID component = ComponentID(componentIndex);
549        const UInt stride = pcPicYuvOrg->getStride(component);
[540]550
[1029]551        separateFields((pcPicYuvOrg->getBuf(component) + pcPicYuvOrg->getMarginX(component) + (pcPicYuvOrg->getMarginY(component) * stride)),
552                       pcField->getPicYuvOrg()->getAddr(component),
553                       pcPicYuvOrg->getStride(component),
554                       pcPicYuvOrg->getWidth(component),
555                       pcPicYuvOrg->getHeight(component),
556                       isTopField);
557
558        separateFields((pcPicYuvTrueOrg->getBuf(component) + pcPicYuvTrueOrg->getMarginX(component) + (pcPicYuvTrueOrg->getMarginY(component) * stride)),
559                       pcField->getPicYuvTrueOrg()->getAddr(component),
560                       pcPicYuvTrueOrg->getStride(component),
561                       pcPicYuvTrueOrg->getWidth(component),
562                       pcPicYuvTrueOrg->getHeight(component),
563                       isTopField);
564      }
565
566      // compute image characteristics
567      if ( getUseAdaptiveQP() )
568      {
569        m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcField ) );
570      }
571    }
[442]572  }
573}
574
[1029]575Void TEncTop::encode( TComPicYuv* pcPicYuvOrg, const InputColourSpaceConversion snrCSC, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int iPicIdInGOP, Bool isTff )
[442]576{
[1029]577  for (Int fieldNum=0; fieldNum<2; fieldNum++)
[442]578  {
[1029]579    if (pcPicYuvOrg)
580    {
581      if (fieldNum==1)                                   // where is this normally?
582      {
583        TComPicYuv* rpcPicYuvRec;
[442]584
[1029]585        // org. buffer
586        if ( rcListPicYuvRecOut.size() >= (UInt)m_iGOPSize+1 ) // need to maintain field 0 in list of RecOuts while processing field 1. Hence +1 on m_iGOPSize.
587        {
588          rpcPicYuvRec = rcListPicYuvRecOut.popFront();
589        }
590        else
591        {
592          rpcPicYuvRec = new TComPicYuv;
[1290]593          rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, m_chromaFormatIDC, m_maxCUWidth, m_maxCUHeight, m_maxTotalCUDepth, true);
[1029]594        }
595        rcListPicYuvRecOut.pushBack( rpcPicYuvRec );
596      }
[442]597    }
[1029]598
599    // compress GOP
600    m_cGOPEncoder.compressGOP(iPicIdInGOP, m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, true, isTff, snrCSC, m_printFrameMSE);
[442]601  }
602
603  m_uiNumAllPicCoded ++;
604}
605#else
[1029]606Void TEncTop::encode(Bool flush, TComPicYuv* pcPicYuvOrg, TComPicYuv* pcPicYuvTrueOrg, const InputColourSpaceConversion snrCSC, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int& iNumEncoded, Bool isTff)
[442]607{
[1029]608  iNumEncoded = 0;
609
610  for (Int fieldNum=0; fieldNum<2; fieldNum++)
[442]611  {
[1029]612    if (pcPicYuvOrg)
[442]613    {
[1029]614
615      /* -- field initialization -- */
616      const Bool isTopField=isTff==(fieldNum==0);
617
618      TComPic *pcField;
619      xGetNewPicBuffer( pcField );
620      pcField->setReconMark (false);                     // where is this normally?
621
622      if (fieldNum==1)                                   // where is this normally?
623      {
624        TComPicYuv* rpcPicYuvRec;
625
626        // org. buffer
627        if ( rcListPicYuvRecOut.size() >= (UInt)m_iGOPSize+1 ) // need to maintain field 0 in list of RecOuts while processing field 1. Hence +1 on m_iGOPSize.
628        {
629          rpcPicYuvRec = rcListPicYuvRecOut.popFront();
630        }
631        else
632        {
633          rpcPicYuvRec = new TComPicYuv;
[1290]634          rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, m_chromaFormatIDC, m_maxCUWidth, m_maxCUHeight, m_maxTotalCUDepth, true);
[1029]635        }
636        rcListPicYuvRecOut.pushBack( rpcPicYuvRec );
637      }
638
639      pcField->getSlice(0)->setPOC( m_iPOCLast );        // superfluous?
640      pcField->getPicYuvRec()->setBorderExtension(false);// where is this normally?
641
642      pcField->setTopField(isTopField);                  // interlaced requirement
643
644      for (UInt componentIndex = 0; componentIndex < pcPicYuvOrg->getNumberValidComponents(); componentIndex++)
645      {
646        const ComponentID component = ComponentID(componentIndex);
647        const UInt stride = pcPicYuvOrg->getStride(component);
648
649        separateFields((pcPicYuvOrg->getBuf(component) + pcPicYuvOrg->getMarginX(component) + (pcPicYuvOrg->getMarginY(component) * stride)),
650                       pcField->getPicYuvOrg()->getAddr(component),
651                       pcPicYuvOrg->getStride(component),
652                       pcPicYuvOrg->getWidth(component),
653                       pcPicYuvOrg->getHeight(component),
654                       isTopField);
655
656        separateFields((pcPicYuvTrueOrg->getBuf(component) + pcPicYuvTrueOrg->getMarginX(component) + (pcPicYuvTrueOrg->getMarginY(component) * stride)),
657                       pcField->getPicYuvTrueOrg()->getAddr(component),
658                       pcPicYuvTrueOrg->getStride(component),
659                       pcPicYuvTrueOrg->getWidth(component),
660                       pcPicYuvTrueOrg->getHeight(component),
661                       isTopField);
662      }
663
664      // compute image characteristics
665      if ( getUseAdaptiveQP() )
666      {
667        m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcField ) );
668      }
[442]669    }
[1029]670
671    if ( m_iNumPicRcvd && ((flush&&fieldNum==1) || (m_iPOCLast/2)==0 || m_iNumPicRcvd==m_iGOPSize ) )
[442]672    {
[1029]673      // compress GOP
674      m_cGOPEncoder.compressGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, true, isTff, snrCSC, m_printFrameMSE);
675
676      iNumEncoded += m_iNumPicRcvd;
677      m_uiNumAllPicCoded += m_iNumPicRcvd;
678      m_iNumPicRcvd = 0;
[442]679    }
680  }
681}
682#endif
683
[313]684// ====================================================================================================================
685// Protected member functions
686// ====================================================================================================================
687
688/**
689 - Application has picture buffer list with size of GOP + 1
690 - Picture buffer list acts like as ring buffer
691 - End of the list has the latest picture
692 .
693 \retval rpcPic obtained picture buffer
694 */
695Void TEncTop::xGetNewPicBuffer ( TComPic*& rpcPic )
696{
[1235]697  // At this point, the SPS and PPS can be considered activated - they are copied to the new TComPic.
698
[313]699  TComSlice::sortPicList(m_cListPic);
[1029]700
[1249]701
[313]702  if (m_cListPic.size() >= (UInt)(m_iGOPSize + getMaxDecPicBuffering(MAX_TLAYER-1) + 2) )
703  {
704    TComList<TComPic*>::iterator iterPic  = m_cListPic.begin();
705    Int iSize = Int( m_cListPic.size() );
706    for ( Int i = 0; i < iSize; i++ )
707    {
708      rpcPic = *(iterPic++);
709      if(rpcPic->getSlice(0)->isReferenced() == false)
710      {
711        break;
712      }
713    }
714  }
715  else
716  {
717    if ( getUseAdaptiveQP() )
718    {
719      TEncPic* pcEPic = new TEncPic;
720
721#if SVC_EXTENSION //Temporal solution, should be modified
[1200]722      if( m_layerId > 0 )
[313]723      {
[1200]724        for( UInt i = 0; i < m_cVPS.getNumDirectRefLayers( m_layerId ); i++ )
[313]725        {
[1235]726          const Window scalEL = m_cPPS.getScaledRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
727          const Window altRL = m_cPPS.getRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
[873]728          Bool equalOffsets = scalEL.hasEqualOffset(altRL);
[1235]729          Bool zeroPhase = m_cPPS.hasZeroResamplingPhase(m_cVPS.getRefLayerId(m_layerId, i));
[313]730
731          TEncTop *pcEncTopBase = (TEncTop *)getRefLayerEnc( i );
[1200]732
[644]733          UInt refLayerId = m_cVPS.getRefLayerId(m_layerId, i);
734
[1287]735          const Int bitDepthLuma = m_cVPS.getBitDepth(CHANNEL_TYPE_LUMA, &m_cSPS, m_layerId);
736          const Int bitDepthChroma = m_cVPS.getBitDepth(CHANNEL_TYPE_CHROMA, &m_cSPS, m_layerId);
737          const Int refBitDepthLuma = m_cVPS.getBitDepth(CHANNEL_TYPE_LUMA, pcEncTopBase->getSPS(), refLayerId);
738          const Int refBitDepthChroma = m_cVPS.getBitDepth(CHANNEL_TYPE_CHROMA, pcEncTopBase->getSPS(), refLayerId);
739
740          Bool sameBitDepths = ( bitDepthLuma == refBitDepthLuma ) && ( bitDepthChroma == refBitDepthChroma );
741
[901]742          if( m_iSourceWidth == pcEncTopBase->getSourceWidth() && m_iSourceHeight == pcEncTopBase->getSourceHeight() && equalOffsets && zeroPhase )
743          {
744            pcEPic->setEqualPictureSizeAndOffsetFlag( i, true );
745          }
746
747          if( !pcEPic->equalPictureSizeAndOffsetFlag(i) || !sameBitDepths
[1212]748#if CGS_3D_ASYMLUT
[713]749            || m_cPPS.getCGSFlag() > 0
750#endif
751            )
[313]752          {
753            pcEPic->setSpatialEnhLayerFlag( i, true );
754
755            //only for scalable extension
[815]756            assert( m_cVPS.getScalabilityMask( SCALABILITY_ID ) == true );
[313]757          }
758        }
759      }
760
[1290]761      pcEPic->create( m_cVPS, m_cSPS, m_cPPS, m_cPPS.getMaxCuDQPDepth()+1, false, m_layerId);
[815]762#else  //SVC_EXTENSION
[1290]763      pcEPic->create( m_cSPS, m_cPPS, m_cPPS.getMaxCuDQPDepth()+1, false);
[815]764#endif //SVC_EXTENSION
[313]765      rpcPic = pcEPic;
766    }
767    else
768    {
769      rpcPic = new TComPic;
770
771#if SVC_EXTENSION //Temporal solution, should be modified
[1200]772      if( m_layerId > 0 )
[313]773      {
[1200]774        for( UInt i = 0; i < m_cVPS.getNumDirectRefLayers( m_layerId ); i++ )
[313]775        {
[1235]776          const Window scalEL = m_cPPS.getScaledRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
777          const Window altRL = m_cPPS.getRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
[873]778          Bool equalOffsets = scalEL.hasEqualOffset(altRL);
[1235]779          Bool zeroPhase = m_cPPS.hasZeroResamplingPhase(m_cVPS.getRefLayerId(m_layerId, i));
[313]780
781          TEncTop *pcEncTopBase = (TEncTop *)getRefLayerEnc( i );
[1200]782
[644]783          UInt refLayerId = m_cVPS.getRefLayerId(m_layerId, i);
784
[1287]785          const Int bitDepthLuma = m_cVPS.getBitDepth(CHANNEL_TYPE_LUMA, &m_cSPS, m_layerId);
786          const Int bitDepthChroma = m_cVPS.getBitDepth(CHANNEL_TYPE_CHROMA, &m_cSPS, m_layerId);
787          const Int refBitDepthLuma = m_cVPS.getBitDepth(CHANNEL_TYPE_LUMA, pcEncTopBase->getSPS(), refLayerId);
788          const Int refBitDepthChroma = m_cVPS.getBitDepth(CHANNEL_TYPE_CHROMA, pcEncTopBase->getSPS(), refLayerId);
789
790          Bool sameBitDepths = ( bitDepthLuma == refBitDepthLuma ) && ( bitDepthChroma == refBitDepthChroma );
791
[873]792          if( m_iSourceWidth != pcEncTopBase->getSourceWidth() || m_iSourceHeight != pcEncTopBase->getSourceHeight() || !sameBitDepths
793            || !equalOffsets
794            || !zeroPhase
[1212]795#if CGS_3D_ASYMLUT
[713]796            || m_cPPS.getCGSFlag() > 0
797#endif
[1200]798            )
[313]799          {
800            rpcPic->setSpatialEnhLayerFlag( i, true );
801
802            //only for scalable extension
[815]803            assert( m_cVPS.getScalabilityMask( SCALABILITY_ID ) == true );
[313]804          }
805        }
806      }
807
[1290]808      rpcPic->create( m_cVPS, m_cSPS, m_cPPS, false, m_layerId );
[815]809#else  //SVC_EXTENSION
[1290]810      rpcPic->create( m_cSPS, m_cPPS, false );
[815]811#endif //SVC_EXTENSION
[313]812    }
[1029]813
[313]814    m_cListPic.pushBack( rpcPic );
815  }
816  rpcPic->setReconMark (false);
[1029]817
[313]818  m_iPOCLast++;
819  m_iNumPicRcvd++;
[1029]820
[313]821  rpcPic->getSlice(0)->setPOC( m_iPOCLast );
822  // mark it should be extended
823  rpcPic->getPicYuvRec()->setBorderExtension(false);
824}
825
[1235]826Void TEncTop::xInitVPS()
827{
828  // The SPS must have already been set up.
829  // set the VPS profile information.
830#if SVC_EXTENSION
831  m_cVPS.setVpsVuiPresentFlag( true );
832#else
833  *m_cVPS.getPTL() = *m_cSPS.getPTL();
834  m_cVPS.setMaxOpSets(1);
835#endif
836  m_cVPS.getTimingInfo()->setTimingInfoPresentFlag       ( false );
837  m_cVPS.setNumHrdParameters( 0 );
838
839  m_cVPS.createHrdParamBuffer();
840  for( UInt i = 0; i < m_cVPS.getNumHrdParameters(); i ++ )
841  {
842    m_cVPS.setHrdOpSetIdx( 0, i );
843    m_cVPS.setCprmsPresentFlag( false, i );
844    // Set up HrdParameters here.
845  }
846}
847
[313]848Void TEncTop::xInitSPS()
849{
850#if SVC_EXTENSION
[815]851  m_cSPS.setExtensionFlag( m_layerId > 0 ? true : false );
[988]852  m_cSPS.setNumDirectRefLayers(m_numDirectRefLayers);
[1172]853
[830]854  if( !m_numDirectRefLayers && m_numAddLayerSets )
[815]855  {
856    m_cSPS.setLayerId(0); // layer ID 0 for independent layers
857  }
858  else
859  {
860    m_cSPS.setLayerId(m_layerId);
861  }
[494]862#endif //SVC_EXTENSION
[1172]863
[313]864  ProfileTierLevel& profileTierLevel = *m_cSPS.getPTL()->getGeneralPTL();
865  profileTierLevel.setLevelIdc(m_level);
866  profileTierLevel.setTierFlag(m_levelTier);
867  profileTierLevel.setProfileIdc(m_profile);
868  profileTierLevel.setProfileCompatibilityFlag(m_profile, 1);
869  profileTierLevel.setProgressiveSourceFlag(m_progressiveSourceFlag);
870  profileTierLevel.setInterlacedSourceFlag(m_interlacedSourceFlag);
871  profileTierLevel.setNonPackedConstraintFlag(m_nonPackedConstraintFlag);
872  profileTierLevel.setFrameOnlyConstraintFlag(m_frameOnlyConstraintFlag);
[1029]873  profileTierLevel.setBitDepthConstraint(m_bitDepthConstraintValue);
874  profileTierLevel.setChromaFormatConstraint(m_chromaFormatConstraintValue);
875  profileTierLevel.setIntraConstraintFlag(m_intraConstraintFlag);
[1356]876  profileTierLevel.setOnePictureOnlyConstraintFlag(m_onePictureOnlyConstraintFlag);
[1029]877  profileTierLevel.setLowerBitRateConstraintFlag(m_lowerBitRateConstraintFlag);
878
[1287]879  if ((m_profile == Profile::MAIN10) && (m_bitDepth[CHANNEL_TYPE_LUMA] == 8) && (m_bitDepth[CHANNEL_TYPE_CHROMA] == 8))
[313]880  {
881    /* The above constraint is equal to Profile::MAIN */
882    profileTierLevel.setProfileCompatibilityFlag(Profile::MAIN, 1);
883  }
884  if (m_profile == Profile::MAIN)
885  {
886    /* A Profile::MAIN10 decoder can always decode Profile::MAIN */
887    profileTierLevel.setProfileCompatibilityFlag(Profile::MAIN10, 1);
888  }
889  /* XXX: should Main be marked as compatible with still picture? */
890  /* XXX: may be a good idea to refactor the above into a function
891   * that chooses the actual compatibility based upon options */
892
[1290]893  m_cSPS.setPicWidthInLumaSamples  ( m_iSourceWidth      );
894  m_cSPS.setPicHeightInLumaSamples ( m_iSourceHeight     );
895  m_cSPS.setConformanceWindow      ( m_conformanceWindow );
896  m_cSPS.setMaxCUWidth             ( m_maxCUWidth        );
897  m_cSPS.setMaxCUHeight            ( m_maxCUHeight       );
898  m_cSPS.setMaxTotalCUDepth        ( m_maxTotalCUDepth   );
[494]899  m_cSPS.setChromaFormatIdc( m_chromaFormatIDC);
[1290]900  m_cSPS.setLog2DiffMaxMinCodingBlockSize(m_log2DiffMaxMinCodingBlockSize);
[313]901
[1290]902  Int minCUSize = m_cSPS.getMaxCUWidth() >> ( m_cSPS.getLog2DiffMaxMinCodingBlockSize() );
[313]903  Int log2MinCUSize = 0;
904  while(minCUSize > 1)
905  {
906    minCUSize >>= 1;
907    log2MinCUSize++;
908  }
909
910  m_cSPS.setLog2MinCodingBlockSize(log2MinCUSize);
[1459]911
[313]912#if SVC_EXTENSION
913  m_cSPS.setSPSId         ( m_iSPSIdCnt       );
914#endif
[1459]915
[313]916  m_cSPS.setPCMLog2MinSize (m_uiPCMLog2MinSize);
917  m_cSPS.setUsePCM        ( m_usePCM           );
918  m_cSPS.setPCMLog2MaxSize( m_pcmLog2MaxSize  );
919
920  m_cSPS.setQuadtreeTULog2MaxSize( m_uiQuadtreeTULog2MaxSize );
921  m_cSPS.setQuadtreeTULog2MinSize( m_uiQuadtreeTULog2MinSize );
922  m_cSPS.setQuadtreeTUMaxDepthInter( m_uiQuadtreeTUMaxDepthInter    );
923  m_cSPS.setQuadtreeTUMaxDepthIntra( m_uiQuadtreeTUMaxDepthIntra    );
[1029]924
[1235]925  m_cSPS.setTMVPFlagsPresent((getTMVPModeId() == 2 || getTMVPModeId() == 1));
926
[313]927  m_cSPS.setMaxTrSize   ( 1 << m_uiQuadtreeTULog2MaxSize );
[1284]928
[313]929  m_cSPS.setUseAMP ( m_useAMP );
930
[1029]931  for (UInt channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
932  {
[1203]933#if SVC_EXTENSION
[1290]934    m_cSPS.setBitDepth    (ChannelType(channelType), m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( m_cVPS.getLayerIdxInVps( m_layerId ) ) )->getBitDepthVps(ChannelType(channelType))            );   
[1287]935#if O0043_BEST_EFFORT_DECODING
[1290]936    m_cSPS.setStreamBitDepth(ChannelType(channelType), m_bitDepth[channelType] );
[1287]937#endif
[1290]938    m_cSPS.setQpBDOffset  (ChannelType(channelType), (6 * (m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( m_cVPS.getLayerIdxInVps( m_layerId ) ) )->getBitDepthVps(ChannelType(channelType)) - 8)));
[442]939#else
[1290]940    m_cSPS.setBitDepth      (ChannelType(channelType), m_bitDepth[channelType] );
[1287]941#if O0043_BEST_EFFORT_DECODING
942    m_cSPS.setStreamBitDepth(ChannelType(channelType), m_bitDepth[channelType] );
[442]943#endif
[1287]944    m_cSPS.setQpBDOffset  (ChannelType(channelType), (6 * (m_bitDepth[channelType] - 8)));
945#endif
[1284]946    m_cSPS.setPCMBitDepth (ChannelType(channelType), m_PCMBitDepth[channelType]         );
[1029]947  }
[313]948
949  m_cSPS.setUseSAO( m_bUseSAO );
950
951  m_cSPS.setMaxTLayers( m_maxTempLayer );
952  m_cSPS.setTemporalIdNestingFlag( ( m_maxTempLayer == 1 ) ? true : false );
[1029]953
[1252]954  for (Int i = 0; i < min(m_cSPS.getMaxTLayers(),(UInt) MAX_TLAYER); i++ )
[313]955  {
[1288]956#if SVC_EXTENSION
957    assert( i < MAX_TLAYER );
958#endif
[313]959    m_cSPS.setMaxDecPicBuffering(m_maxDecPicBuffering[i], i);
960    m_cSPS.setNumReorderPics(m_numReorderPics[i], i);
961  }
[1029]962
[313]963  m_cSPS.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag );
[1235]964  m_cSPS.setScalingListFlag ( (m_useScalingListId == SCALING_LIST_OFF) ? 0 : 1 );
[313]965  m_cSPS.setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
[1029]966  m_cSPS.setVuiParametersPresentFlag(getVuiParametersPresentFlag());
[313]967
968  if (m_cSPS.getVuiParametersPresentFlag())
969  {
970    TComVUI* pcVUI = m_cSPS.getVuiParameters();
[823]971    pcVUI->setAspectRatioInfoPresentFlag(getAspectRatioInfoPresentFlag());
[313]972    pcVUI->setAspectRatioIdc(getAspectRatioIdc());
973    pcVUI->setSarWidth(getSarWidth());
974    pcVUI->setSarHeight(getSarHeight());
975    pcVUI->setOverscanInfoPresentFlag(getOverscanInfoPresentFlag());
976    pcVUI->setOverscanAppropriateFlag(getOverscanAppropriateFlag());
977    pcVUI->setVideoSignalTypePresentFlag(getVideoSignalTypePresentFlag());
978    pcVUI->setVideoFormat(getVideoFormat());
979    pcVUI->setVideoFullRangeFlag(getVideoFullRangeFlag());
980    pcVUI->setColourDescriptionPresentFlag(getColourDescriptionPresentFlag());
981    pcVUI->setColourPrimaries(getColourPrimaries());
982    pcVUI->setTransferCharacteristics(getTransferCharacteristics());
983    pcVUI->setMatrixCoefficients(getMatrixCoefficients());
984    pcVUI->setChromaLocInfoPresentFlag(getChromaLocInfoPresentFlag());
985    pcVUI->setChromaSampleLocTypeTopField(getChromaSampleLocTypeTopField());
986    pcVUI->setChromaSampleLocTypeBottomField(getChromaSampleLocTypeBottomField());
987    pcVUI->setNeutralChromaIndicationFlag(getNeutralChromaIndicationFlag());
988    pcVUI->setDefaultDisplayWindow(getDefaultDisplayWindow());
989    pcVUI->setFrameFieldInfoPresentFlag(getFrameFieldInfoPresentFlag());
990    pcVUI->setFieldSeqFlag(false);
991    pcVUI->setHrdParametersPresentFlag(false);
992    pcVUI->getTimingInfo()->setPocProportionalToTimingFlag(getPocProportionalToTimingFlag());
993    pcVUI->getTimingInfo()->setNumTicksPocDiffOneMinus1   (getNumTicksPocDiffOneMinus1()   );
994    pcVUI->setBitstreamRestrictionFlag(getBitstreamRestrictionFlag());
995    pcVUI->setTilesFixedStructureFlag(getTilesFixedStructureFlag());
996    pcVUI->setMotionVectorsOverPicBoundariesFlag(getMotionVectorsOverPicBoundariesFlag());
997    pcVUI->setMinSpatialSegmentationIdc(getMinSpatialSegmentationIdc());
998    pcVUI->setMaxBytesPerPicDenom(getMaxBytesPerPicDenom());
999    pcVUI->setMaxBitsPerMinCuDenom(getMaxBitsPerMinCuDenom());
1000    pcVUI->setLog2MaxMvLengthHorizontal(getLog2MaxMvLengthHorizontal());
1001    pcVUI->setLog2MaxMvLengthVertical(getLog2MaxMvLengthVertical());
1002  }
[1235]1003  m_cSPS.setNumLongTermRefPicSPS(NUM_LONG_TERM_REF_PIC_SPS);
1004  assert (NUM_LONG_TERM_REF_PIC_SPS <= MAX_NUM_LONG_TERM_REF_PICS);
1005  for (Int k = 0; k < NUM_LONG_TERM_REF_PIC_SPS; k++)
1006  {
1007    m_cSPS.setLtRefPicPocLsbSps(k, 0);
1008    m_cSPS.setUsedByCurrPicLtSPSFlag(k, 0);
1009  }
[1433]1010
1011#if U0132_TARGET_BITS_SATURATION
1012  if( getPictureTimingSEIEnabled() || getDecodingUnitInfoSEIEnabled() || getCpbSaturationEnabled() )
1013#else
[1235]1014  if( getPictureTimingSEIEnabled() || getDecodingUnitInfoSEIEnabled() )
[1433]1015#endif
[1235]1016  {
[1348]1017    xInitHrdParameters();
[1235]1018  }
1019  if( getBufferingPeriodSEIEnabled() || getPictureTimingSEIEnabled() || getDecodingUnitInfoSEIEnabled() )
1020  {
1021    m_cSPS.getVuiParameters()->setHrdParametersPresentFlag( true );
1022  }
[1316]1023
1024  // Set up SPS range extension settings
1025  m_cSPS.getSpsRangeExtension().setTransformSkipRotationEnabledFlag(m_transformSkipRotationEnabledFlag);
1026  m_cSPS.getSpsRangeExtension().setTransformSkipContextEnabledFlag(m_transformSkipContextEnabledFlag);
1027  for (UInt signallingModeIndex = 0; signallingModeIndex < NUMBER_OF_RDPCM_SIGNALLING_MODES; signallingModeIndex++)
1028  {
1029    m_cSPS.getSpsRangeExtension().setRdpcmEnabledFlag(RDPCMSignallingMode(signallingModeIndex), m_rdpcmEnabledFlag[signallingModeIndex]);
1030  }
1031  m_cSPS.getSpsRangeExtension().setExtendedPrecisionProcessingFlag(m_extendedPrecisionProcessingFlag);
1032  m_cSPS.getSpsRangeExtension().setIntraSmoothingDisabledFlag( m_intraSmoothingDisabledFlag );
1033  m_cSPS.getSpsRangeExtension().setHighPrecisionOffsetsEnabledFlag(m_highPrecisionOffsetsEnabledFlag);
1034  m_cSPS.getSpsRangeExtension().setPersistentRiceAdaptationEnabledFlag(m_persistentRiceAdaptationEnabledFlag);
1035  m_cSPS.getSpsRangeExtension().setCabacBypassAlignmentEnabledFlag(m_cabacBypassAlignmentEnabledFlag);
[313]1036}
1037
[1433]1038#if U0132_TARGET_BITS_SATURATION
1039// calculate scale value of bitrate and initial delay
1040Int calcScale(Int x)
1041{
1042  UInt iMask = 0xffffffff;
1043  Int ScaleValue = 32;
1044
1045  while ((x&iMask) != 0)
1046  {
1047    ScaleValue--;
1048    iMask = (iMask >> 1);
1049  }
1050
1051  return ScaleValue;
1052}
1053#endif
[1348]1054Void TEncTop::xInitHrdParameters()
1055{
1056  Bool useSubCpbParams = (getSliceMode() > 0) || (getSliceSegmentMode() > 0);
1057  Int  bitRate         = getTargetBitrate();
1058  Bool isRandomAccess  = getIntraPeriod() > 0;
[1433]1059# if U0132_TARGET_BITS_SATURATION
1060  Int cpbSize          = getCpbSize();
[1348]1061
[1433]1062  if( !getVuiParametersPresentFlag() && !getCpbSaturationEnabled() )
1063#else
[1348]1064  if( !getVuiParametersPresentFlag() )
[1433]1065#endif
[1348]1066  {
1067    return;
1068  }
1069
1070  TComVUI *vui = m_cSPS.getVuiParameters();
1071  TComHRD *hrd = vui->getHrdParameters();
1072
1073  TimingInfo *timingInfo = vui->getTimingInfo();
1074
1075#if SVC_EXTENSION
1076  if( m_layerId > 0 )
1077  {
1078    timingInfo->setTimingInfoPresentFlag( false );
1079    return;
1080  }
1081#endif
1082
1083  timingInfo->setTimingInfoPresentFlag( true );
1084  switch( getFrameRate() )
1085  {
1086  case 24:
1087    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
1088    break;
1089  case 25:
1090    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
1091    break;
1092  case 30:
1093    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
1094    break;
1095  case 50:
1096    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
1097    break;
1098  case 60:
1099    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
1100    break;
1101  default:
1102    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
1103    break;
1104  }
1105
1106  Bool rateCnt = ( bitRate > 0 );
1107  hrd->setNalHrdParametersPresentFlag( rateCnt );
1108  hrd->setVclHrdParametersPresentFlag( rateCnt );
1109  hrd->setSubPicCpbParamsPresentFlag( useSubCpbParams );
1110
1111  if( hrd->getSubPicCpbParamsPresentFlag() )
1112  {
1113    hrd->setTickDivisorMinus2( 100 - 2 );                          //
1114    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
1115    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
1116    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
1117  }
1118  else
1119  {
1120    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false );
1121  }
1122
[1433]1123#if U0132_TARGET_BITS_SATURATION
1124  if (calcScale(bitRate) <= 6)
1125  {
1126    hrd->setBitRateScale(0);
1127  }
1128  else
1129  {
1130    hrd->setBitRateScale(calcScale(bitRate) - 6);
1131  }
1132
1133  if (calcScale(cpbSize) <= 4)
1134  {
1135    hrd->setCpbSizeScale(0);
1136  }
1137  else
1138  {
1139    hrd->setCpbSizeScale(calcScale(cpbSize) - 4);
1140  }
1141#else
[1349]1142  hrd->setBitRateScale( 4 );                                       // in units of 2^( 6 + 4 ) = 1,024 bps
1143  hrd->setCpbSizeScale( 6 );                                       // in units of 2^( 4 + 6 ) = 1,024 bit
[1433]1144#endif
1145
[1349]1146  hrd->setDuCpbSizeScale( 6 );                                     // in units of 2^( 4 + 6 ) = 1,024 bit
[1348]1147
1148  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
1149  if( isRandomAccess )
1150  {
1151    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
1152    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
1153  }
1154  else
1155  {
1156    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
1157    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
1158  }
1159
1160  // Note: parameters for all temporal layers are initialized with the same values
1161  Int i, j;
1162  UInt bitrateValue, cpbSizeValue;
1163  UInt duCpbSizeValue;
1164  UInt duBitRateValue = 0;
1165
1166  for( i = 0; i < MAX_TLAYER; i ++ )
1167  {
1168    hrd->setFixedPicRateFlag( i, 1 );
1169    hrd->setPicDurationInTcMinus1( i, 0 );
1170    hrd->setLowDelayHrdFlag( i, 0 );
1171    hrd->setCpbCntMinus1( i, 0 );
1172
[1349]1173    //! \todo check for possible PTL violations
1174    // BitRate[ i ] = ( bit_rate_value_minus1[ i ] + 1 ) * 2^( 6 + bit_rate_scale )
1175    bitrateValue = bitRate / (1 << (6 + hrd->getBitRateScale()) );      // bitRate is in bits, so it needs to be scaled down
1176    // CpbSize[ i ] = ( cpb_size_value_minus1[ i ] + 1 ) * 2^( 4 + cpb_size_scale )
[1433]1177#if U0132_TARGET_BITS_SATURATION
1178    cpbSizeValue = cpbSize / (1 << (4 + hrd->getCpbSizeScale()) );      // using bitRate results in 1 second CPB size
1179#else
[1349]1180    cpbSizeValue = bitRate / (1 << (4 + hrd->getCpbSizeScale()) );      // using bitRate results in 1 second CPB size
[1433]1181#endif
[1349]1182
[1433]1183
[1349]1184    // DU CPB size could be smaller (i.e. bitrateValue / number of DUs), but we don't know
[1348]1185    // in how many DUs the slice segment settings will result
[1349]1186    duCpbSizeValue = bitrateValue;
1187    duBitRateValue = cpbSizeValue;
[1348]1188
1189    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
1190    {
1191      hrd->setBitRateValueMinus1( i, j, 0, ( bitrateValue - 1 ) );
1192      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
1193      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( duCpbSizeValue - 1 ) );
[1360]1194      hrd->setDuBitRateValueMinus1( i, j, 0, ( duBitRateValue - 1 ) );
[1350]1195      hrd->setCbrFlag( i, j, 0, false );
[1348]1196
1197      hrd->setBitRateValueMinus1( i, j, 1, ( bitrateValue - 1) );
1198      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
1199      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( duCpbSizeValue - 1 ) );
1200      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
[1350]1201      hrd->setCbrFlag( i, j, 1, false );
[1348]1202    }
1203  }
1204}
1205
[313]1206Void TEncTop::xInitPPS()
1207{
1208  m_cPPS.setConstrainedIntraPred( m_bUseConstrainedIntraPred );
1209  Bool bUseDQP = (getMaxCuDQPDepth() > 0)? true : false;
1210
[595]1211  if((getMaxDeltaQP() != 0 )|| getUseAdaptiveQP())
[540]1212  {
[595]1213    bUseDQP = true;
[540]1214  }
[313]1215
[1235]1216  if (m_costMode==COST_SEQUENCE_LEVEL_LOSSLESS || m_costMode==COST_LOSSLESS_CODING)
1217  {
1218    bUseDQP=false;
1219  }
[1029]1220
[1235]1221
1222  if ( m_RCEnableRateControl )
[313]1223  {
1224    m_cPPS.setUseDQP(true);
[1235]1225    m_cPPS.setMaxCuDQPDepth( 0 );
1226  }
1227  else if(bUseDQP)
1228  {
1229    m_cPPS.setUseDQP(true);
[313]1230    m_cPPS.setMaxCuDQPDepth( m_iMaxCuDQPDepth );
1231  }
1232  else
1233  {
1234    m_cPPS.setUseDQP(false);
1235    m_cPPS.setMaxCuDQPDepth( 0 );
1236  }
1237
[1316]1238  if ( m_diffCuChromaQpOffsetDepth >= 0 )
[1029]1239  {
[1316]1240    m_cPPS.getPpsRangeExtension().setDiffCuChromaQpOffsetDepth(m_diffCuChromaQpOffsetDepth);
1241    m_cPPS.getPpsRangeExtension().clearChromaQpOffsetList();
1242    m_cPPS.getPpsRangeExtension().setChromaQpOffsetListEntry(1, 6, 6);
[1029]1243    /* todo, insert table entries from command line (NB, 0 should not be touched) */
1244  }
1245  else
1246  {
[1316]1247    m_cPPS.getPpsRangeExtension().setDiffCuChromaQpOffsetDepth(0);
1248    m_cPPS.getPpsRangeExtension().clearChromaQpOffsetList();
[1029]1249  }
[1316]1250  m_cPPS.getPpsRangeExtension().setCrossComponentPredictionEnabledFlag(m_crossComponentPredictionEnabledFlag);
1251  m_cPPS.getPpsRangeExtension().setLog2SaoOffsetScale(CHANNEL_TYPE_LUMA,   m_log2SaoOffsetScale[CHANNEL_TYPE_LUMA  ]);
1252  m_cPPS.getPpsRangeExtension().setLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA, m_log2SaoOffsetScale[CHANNEL_TYPE_CHROMA]);
[1029]1253
1254  m_cPPS.setQpOffset(COMPONENT_Cb, m_chromaCbQpOffset );
1255  m_cPPS.setQpOffset(COMPONENT_Cr, m_chromaCrQpOffset );
1256
[1459]1257  m_cPPS.setEntropyCodingSyncEnabledFlag( m_entropyCodingSyncEnabledFlag );
[313]1258  m_cPPS.setTilesEnabledFlag( (m_iNumColumnsMinus1 > 0 || m_iNumRowsMinus1 > 0) );
1259  m_cPPS.setUseWP( m_useWeightedPred );
1260  m_cPPS.setWPBiPred( m_useWeightedBiPred );
1261  m_cPPS.setOutputFlagPresentFlag( false );
1262  m_cPPS.setSignHideFlag(getSignHideFlag());
[1322]1263
[313]1264  if ( getDeblockingFilterMetric() )
1265  {
1266    m_cPPS.setDeblockingFilterOverrideEnabledFlag(true);
1267    m_cPPS.setPicDisableDeblockingFilterFlag(false);
[1029]1268  }
[313]1269  else
1270  {
[1322]1271    m_cPPS.setDeblockingFilterOverrideEnabledFlag( !getLoopFilterOffsetInPPS() );
1272    m_cPPS.setPicDisableDeblockingFilterFlag( getLoopFilterDisable() );
[313]1273  }
[1235]1274
[1322]1275  if (! m_cPPS.getPicDisableDeblockingFilterFlag())
[1235]1276  {
1277    m_cPPS.setDeblockingFilterBetaOffsetDiv2( getLoopFilterBetaOffset() );
1278    m_cPPS.setDeblockingFilterTcOffsetDiv2( getLoopFilterTcOffset() );
1279  }
[1322]1280  else
1281  {
1282    m_cPPS.setDeblockingFilterBetaOffsetDiv2(0);
1283    m_cPPS.setDeblockingFilterTcOffsetDiv2(0);
1284  }
1285
1286  // deblockingFilterControlPresentFlag is true if any of the settings differ from the inferred values:
1287  const Bool deblockingFilterControlPresentFlag = m_cPPS.getDeblockingFilterOverrideEnabledFlag() ||
1288                                                  m_cPPS.getPicDisableDeblockingFilterFlag()      ||
1289                                                  m_cPPS.getDeblockingFilterBetaOffsetDiv2() != 0 ||
1290                                                  m_cPPS.getDeblockingFilterTcOffsetDiv2() != 0;
1291
1292  m_cPPS.setDeblockingFilterControlPresentFlag(deblockingFilterControlPresentFlag);
1293
[313]1294  m_cPPS.setLog2ParallelMergeLevelMinus2   (m_log2ParallelMergeLevelMinus2 );
1295  m_cPPS.setCabacInitPresentFlag(CABAC_INIT_PRESENT_FLAG);
1296  m_cPPS.setLoopFilterAcrossSlicesEnabledFlag( m_bLFCrossSliceBoundaryFlag );
[1029]1297
[1249]1298
[313]1299  Int histogram[MAX_NUM_REF + 1];
1300  for( Int i = 0; i <= MAX_NUM_REF; i++ )
1301  {
1302    histogram[i]=0;
1303  }
[1029]1304  for( Int i = 0; i < getGOPSize(); i++)
[313]1305  {
1306    assert(getGOPEntry(i).m_numRefPicsActive >= 0 && getGOPEntry(i).m_numRefPicsActive <= MAX_NUM_REF);
1307    histogram[getGOPEntry(i).m_numRefPicsActive]++;
1308  }
[1029]1309
[313]1310  Int maxHist=-1;
1311  Int bestPos=0;
1312  for( Int i = 0; i <= MAX_NUM_REF; i++ )
1313  {
1314    if(histogram[i]>maxHist)
1315    {
1316      maxHist=histogram[i];
1317      bestPos=i;
1318    }
1319  }
1320  assert(bestPos <= 15);
1321  m_cPPS.setNumRefIdxL0DefaultActive(bestPos);
1322  m_cPPS.setNumRefIdxL1DefaultActive(bestPos);
1323  m_cPPS.setTransquantBypassEnableFlag(getTransquantBypassEnableFlag());
1324  m_cPPS.setUseTransformSkip( m_useTransformSkip );
[1316]1325  m_cPPS.getPpsRangeExtension().setLog2MaxTransformSkipBlockSize( m_log2MaxTransformSkipBlockSize  );
[1029]1326
1327  if (m_sliceSegmentMode != NO_SLICES)
[313]1328  {
1329    m_cPPS.setDependentSliceSegmentsEnabledFlag( true );
1330  }
[1029]1331 
[442]1332#if SVC_EXTENSION
[815]1333  m_cPPS.setLayerId( m_layerId );
1334
[830]1335  if( !m_numDirectRefLayers && m_numAddLayerSets )
[313]1336  {
[815]1337    m_cPPS.setLayerId(0); // layer ID 0 for independent layers
[313]1338  }
[815]1339
1340  if( m_layerId > 0 )
[313]1341  {
1342    m_cPPS.setListsModificationPresentFlag(true);
[815]1343    m_cPPS.setExtensionFlag(true);
[313]1344  }
[815]1345  else
1346  {
1347    m_cPPS.setListsModificationPresentFlag(false);
1348    m_cPPS.setExtensionFlag(false);
1349  }
[442]1350
[815]1351  m_cPPS.setPPSId( m_iPPSIdCnt );
1352  m_cPPS.setSPSId( m_iSPSIdCnt );
[1150]1353
1354  if( m_crossLayerBLAFlag )
[540]1355  {
1356    m_cPPS.setNumExtraSliceHeaderBits( 3 );
1357  }
[1150]1358
[1030]1359  m_cPPS.setNumRefLayerLocationOffsets(m_numRefLayerLocationOffsets);
1360  for(Int i = 0; i < m_cPPS.getNumRefLayerLocationOffsets(); i++)
[849]1361  {
[1030]1362    m_cPPS.setRefLocationOffsetLayerId(i, m_refLocationOffsetLayerId[i]);
[849]1363    m_cPPS.getScaledRefLayerWindow(i) = m_scaledRefLayerWindow[i];
1364    m_cPPS.getRefLayerWindow(i) = m_refLayerWindow[i];
[934]1365    m_cPPS.setScaledRefLayerOffsetPresentFlag( i, m_scaledRefLayerOffsetPresentFlag[i] );
1366    m_cPPS.setRefRegionOffsetPresentFlag( i, m_refRegionOffsetPresentFlag[i] );
1367    m_cPPS.setResamplePhaseSetPresentFlag( i, m_resamplePhaseSetPresentFlag[i] );
[1030]1368    m_cPPS.setPhaseHorLuma( m_refLocationOffsetLayerId[i], m_phaseHorLuma[i] );
1369    m_cPPS.setPhaseVerLuma( m_refLocationOffsetLayerId[i], m_phaseVerLuma[i] );
1370    m_cPPS.setPhaseHorChroma( m_refLocationOffsetLayerId[i], m_phaseHorChroma[i] );
1371    m_cPPS.setPhaseVerChroma( m_refLocationOffsetLayerId[i], m_phaseVerChroma[i] );
[849]1372  }
[1212]1373#if CGS_3D_ASYMLUT
[713]1374  m_cPPS.setCGSFlag( m_nCGSFlag );
1375#endif
[815]1376  m_cPPS.setPocResetInfoPresentFlag( true );
1377  m_cPPS.setExtensionFlag( true );
1378  m_cPPS.setSliceHeaderExtensionPresentFlag( true );
1379#endif //SVC_EXTENSION
[313]1380}
1381
1382//Function for initializing m_RPSList, a list of TComReferencePictureSet, based on the GOPEntry objects read from the config file.
[442]1383Void TEncTop::xInitRPS(Bool isFieldCoding)
[313]1384{
1385  TComReferencePictureSet*      rps;
[1029]1386
1387  m_cSPS.createRPSList(getGOPSize() + m_extraRPSs + 1);
[313]1388  TComRPSList* rpsList = m_cSPS.getRPSList();
1389
[1029]1390  for( Int i = 0; i < getGOPSize()+m_extraRPSs; i++)
[313]1391  {
1392    GOPEntry ge = getGOPEntry(i);
1393    rps = rpsList->getReferencePictureSet(i);
1394    rps->setNumberOfPictures(ge.m_numRefPics);
1395    rps->setNumRefIdc(ge.m_numRefIdc);
1396    Int numNeg = 0;
1397    Int numPos = 0;
1398    for( Int j = 0; j < ge.m_numRefPics; j++)
1399    {
1400      rps->setDeltaPOC(j,ge.m_referencePics[j]);
1401      rps->setUsed(j,ge.m_usedByCurrPic[j]);
1402      if(ge.m_referencePics[j]>0)
1403      {
1404        numPos++;
1405      }
1406      else
1407      {
1408        numNeg++;
1409      }
1410    }
1411    rps->setNumberOfNegativePictures(numNeg);
1412    rps->setNumberOfPositivePictures(numPos);
1413
1414    // handle inter RPS intialization from the config file.
1415    rps->setInterRPSPrediction(ge.m_interRPSPrediction > 0);  // not very clean, converting anything > 0 to true.
1416    rps->setDeltaRIdxMinus1(0);                               // index to the Reference RPS is always the previous one.
[1235]1417    TComReferencePictureSet*     RPSRef = i>0 ? rpsList->getReferencePictureSet(i-1): NULL;  // get the reference RPS
[313]1418
1419    if (ge.m_interRPSPrediction == 2)  // Automatic generation of the inter RPS idc based on the RIdx provided.
1420    {
[1235]1421      assert (RPSRef!=NULL);
[313]1422      Int deltaRPS = getGOPEntry(i-1).m_POC - ge.m_POC;  // the ref POC - current POC
1423      Int numRefDeltaPOC = RPSRef->getNumberOfPictures();
1424
1425      rps->setDeltaRPS(deltaRPS);           // set delta RPS
1426      rps->setNumRefIdc(numRefDeltaPOC+1);  // set the numRefIdc to the number of pictures in the reference RPS + 1.
1427      Int count=0;
1428      for (Int j = 0; j <= numRefDeltaPOC; j++ ) // cycle through pics in reference RPS.
1429      {
1430        Int RefDeltaPOC = (j<numRefDeltaPOC)? RPSRef->getDeltaPOC(j): 0;  // if it is the last decoded picture, set RefDeltaPOC = 0
1431        rps->setRefIdc(j, 0);
1432        for (Int k = 0; k < rps->getNumberOfPictures(); k++ )  // cycle through pics in current RPS.
1433        {
[1029]1434          if (rps->getDeltaPOC(k) == ( RefDeltaPOC + deltaRPS))  // if the current RPS has a same picture as the reference RPS.
[313]1435          {
1436              rps->setRefIdc(j, (rps->getUsed(k)?1:2));
1437              count++;
1438              break;
1439          }
1440        }
1441      }
1442      if (count != rps->getNumberOfPictures())
1443      {
1444        printf("Warning: Unable fully predict all delta POCs using the reference RPS index given in the config file.  Setting Inter RPS to false for this RPS.\n");
1445        rps->setInterRPSPrediction(0);
1446      }
1447    }
1448    else if (ge.m_interRPSPrediction == 1)  // inter RPS idc based on the RefIdc values provided in config file.
1449    {
[1235]1450      assert (RPSRef!=NULL);
[313]1451      rps->setDeltaRPS(ge.m_deltaRPS);
1452      rps->setNumRefIdc(ge.m_numRefIdc);
1453      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1454      {
1455        rps->setRefIdc(j, ge.m_refIdc[j]);
1456      }
[1252]1457      // the following code overwrite the deltaPOC and Used by current values read from the config file with the ones
[313]1458      // computed from the RefIdc.  A warning is printed if they are not identical.
1459      numNeg = 0;
1460      numPos = 0;
1461      TComReferencePictureSet      RPSTemp;  // temporary variable
1462
1463      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1464      {
1465        if (ge.m_refIdc[j])
1466        {
1467          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1468          RPSTemp.setDeltaPOC((numNeg+numPos),deltaPOC);
1469          RPSTemp.setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1470          if (deltaPOC<0)
1471          {
1472            numNeg++;
1473          }
1474          else
1475          {
1476            numPos++;
1477          }
1478        }
1479      }
1480      if (numNeg != rps->getNumberOfNegativePictures())
1481      {
1482        printf("Warning: number of negative pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1483        rps->setNumberOfNegativePictures(numNeg);
[442]1484        rps->setNumberOfPictures(numNeg+numPos);
[313]1485      }
1486      if (numPos != rps->getNumberOfPositivePictures())
1487      {
1488        printf("Warning: number of positive pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1489        rps->setNumberOfPositivePictures(numPos);
[442]1490        rps->setNumberOfPictures(numNeg+numPos);
[313]1491      }
1492      RPSTemp.setNumberOfPictures(numNeg+numPos);
1493      RPSTemp.setNumberOfNegativePictures(numNeg);
1494      RPSTemp.sortDeltaPOC();     // sort the created delta POC before comparing
[1029]1495      // check if Delta POC and Used are the same
[313]1496      // print warning if they are not.
1497      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1498      {
1499        if (RPSTemp.getDeltaPOC(j) != rps->getDeltaPOC(j))
1500        {
1501          printf("Warning: delta POC is different between intra RPS and inter RPS specified in the config file.\n");
1502          rps->setDeltaPOC(j,RPSTemp.getDeltaPOC(j));
1503        }
1504        if (RPSTemp.getUsed(j) != rps->getUsed(j))
1505        {
1506          printf("Warning: Used by Current in RPS is different between intra and inter RPS specified in the config file.\n");
1507          rps->setUsed(j,RPSTemp.getUsed(j));
1508        }
1509      }
1510    }
1511  }
[1029]1512  //In case of field coding, we need to set special parameters for the first bottom field of the sequence, since it is not specified in the cfg file.
1513  //The position = GOPSize + extraRPSs which is (a priori) unused is reserved for this field in the RPS.
1514  if (isFieldCoding)
[442]1515  {
1516    rps = rpsList->getReferencePictureSet(getGOPSize()+m_extraRPSs);
1517    rps->setNumberOfPictures(1);
1518    rps->setNumberOfNegativePictures(1);
1519    rps->setNumberOfPositivePictures(0);
1520    rps->setNumberOfLongtermPictures(0);
1521    rps->setDeltaPOC(0,-1);
1522    rps->setPOC(0,0);
1523    rps->setUsed(0,true);
1524    rps->setInterRPSPrediction(false);
1525    rps->setDeltaRIdxMinus1(0);
1526    rps->setDeltaRPS(0);
1527    rps->setNumRefIdc(0);
[1029]1528  }
[313]1529}
1530
[1029]1531   // This is a function that
1532   // determines what Reference Picture Set to use
[313]1533   // for a specific slice (with POC = POCCurr)
1534Void TEncTop::selectReferencePictureSet(TComSlice* slice, Int POCCurr, Int GOPid )
1535{
1536  slice->setRPSidx(GOPid);
[1029]1537
[313]1538  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
[1029]1539  {
[313]1540    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1541    {
1542      Int POCIndex = POCCurr%m_uiIntraPeriod;
1543      if(POCIndex == 0)
1544      {
1545        POCIndex = m_uiIntraPeriod;
1546      }
1547      if(POCIndex == m_GOPList[extraNum].m_POC)
1548      {
1549        slice->setRPSidx(extraNum);
1550      }
1551    }
1552    else
1553    {
1554      if(POCCurr==m_GOPList[extraNum].m_POC)
1555      {
1556        slice->setRPSidx(extraNum);
1557      }
1558    }
1559  }
1560
[442]1561  if(POCCurr == 1 && slice->getPic()->isField())
1562  {
1563    slice->setRPSidx(m_iGOPSize+m_extraRPSs);
1564  }
1565
[1366]1566  const TComReferencePictureSet *rps = (slice->getSPS()->getRPSList()->getReferencePictureSet(slice->getRPSidx()));
[1235]1567  slice->setRPS(rps);
[313]1568}
1569
[1307]1570Int TEncTop::getReferencePictureSetIdxForSOP(Int POCCurr, Int GOPid )
[313]1571{
[1029]1572  Int rpsIdx = GOPid;
[313]1573
1574  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
[1029]1575  {
[313]1576    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1577    {
1578      Int POCIndex = POCCurr%m_uiIntraPeriod;
1579      if(POCIndex == 0)
1580      {
1581        POCIndex = m_uiIntraPeriod;
1582      }
1583      if(POCIndex == m_GOPList[extraNum].m_POC)
1584      {
1585        rpsIdx = extraNum;
1586      }
1587    }
1588    else
1589    {
1590      if(POCCurr==m_GOPList[extraNum].m_POC)
1591      {
1592        rpsIdx = extraNum;
1593      }
1594    }
1595  }
1596
1597  return rpsIdx;
1598}
1599
1600Void  TEncTop::xInitPPSforTiles()
1601{
[823]1602  m_cPPS.setTileUniformSpacingFlag( m_tileUniformSpacingFlag );
1603  m_cPPS.setNumTileColumnsMinus1( m_iNumColumnsMinus1 );
1604  m_cPPS.setNumTileRowsMinus1( m_iNumRowsMinus1 );
1605  if( !m_tileUniformSpacingFlag )
[313]1606  {
[823]1607    m_cPPS.setTileColumnWidth( m_tileColumnWidth );
1608    m_cPPS.setTileRowHeight( m_tileRowHeight );
[313]1609  }
1610  m_cPPS.setLoopFilterAcrossTilesEnabledFlag( m_loopFilterAcrossTilesEnabledFlag );
1611
1612  // # substreams is "per tile" when tiles are independent.
1613}
1614
1615Void  TEncCfg::xCheckGSParameters()
1616{
[1289]1617  Int   iWidthInCU = ( m_iSourceWidth%m_maxCUWidth ) ? m_iSourceWidth/m_maxCUWidth + 1 : m_iSourceWidth/m_maxCUWidth;
1618  Int   iHeightInCU = ( m_iSourceHeight%m_maxCUHeight ) ? m_iSourceHeight/m_maxCUHeight + 1 : m_iSourceHeight/m_maxCUHeight;
[313]1619  UInt  uiCummulativeColumnWidth = 0;
1620  UInt  uiCummulativeRowHeight = 0;
1621
1622  //check the column relative parameters
1623  if( m_iNumColumnsMinus1 >= (1<<(LOG2_MAX_NUM_COLUMNS_MINUS1+1)) )
1624  {
1625    printf( "The number of columns is larger than the maximum allowed number of columns.\n" );
1626    exit( EXIT_FAILURE );
1627  }
1628
1629  if( m_iNumColumnsMinus1 >= iWidthInCU )
1630  {
1631    printf( "The current picture can not have so many columns.\n" );
1632    exit( EXIT_FAILURE );
1633  }
1634
[823]1635  if( m_iNumColumnsMinus1 && !m_tileUniformSpacingFlag )
[313]1636  {
1637    for(Int i=0; i<m_iNumColumnsMinus1; i++)
1638    {
[823]1639      uiCummulativeColumnWidth += m_tileColumnWidth[i];
[313]1640    }
1641
1642    if( uiCummulativeColumnWidth >= iWidthInCU )
1643    {
1644      printf( "The width of the column is too large.\n" );
1645      exit( EXIT_FAILURE );
1646    }
1647  }
1648
1649  //check the row relative parameters
1650  if( m_iNumRowsMinus1 >= (1<<(LOG2_MAX_NUM_ROWS_MINUS1+1)) )
1651  {
1652    printf( "The number of rows is larger than the maximum allowed number of rows.\n" );
1653    exit( EXIT_FAILURE );
1654  }
1655
1656  if( m_iNumRowsMinus1 >= iHeightInCU )
1657  {
1658    printf( "The current picture can not have so many rows.\n" );
1659    exit( EXIT_FAILURE );
1660  }
1661
[823]1662  if( m_iNumRowsMinus1 && !m_tileUniformSpacingFlag )
[313]1663  {
1664    for(Int i=0; i<m_iNumRowsMinus1; i++)
[1246]1665    {
[823]1666      uiCummulativeRowHeight += m_tileRowHeight[i];
[1246]1667    }
[313]1668
1669    if( uiCummulativeRowHeight >= iHeightInCU )
1670    {
1671      printf( "The height of the row is too large.\n" );
1672      exit( EXIT_FAILURE );
1673    }
1674  }
1675}
1676
1677#if SVC_EXTENSION
[1048]1678TEncTop* TEncTop::getRefLayerEnc( UInt refLayerIdx )
[313]1679{
[1057]1680  if( m_ppcTEncTop[m_cVPS.getLayerIdxInVps(m_layerId)]->getNumDirectRefLayers() <= 0 )
[313]1681  {
1682    return (TEncTop *)getLayerEnc( 0 );
1683  }
1684
[1057]1685  return (TEncTop *)getLayerEnc( m_cVPS.getLayerIdxInVps(m_cVPS.getRefLayerId( m_layerId, refLayerIdx )) );
[313]1686}
1687
1688Void TEncTop::xInitILRP()
1689{
[1235]1690  if( m_layerId > 0 )
[442]1691  {
[1290]1692    if( m_cIlpPic[0] == NULL )
[442]1693    {
[588]1694      for (Int j=0; j < m_numDirectRefLayers; j++)
[442]1695      {
[1029]1696        m_cIlpPic[j] = new TComPic;
[1290]1697        m_cIlpPic[j]->create(m_cVPS, m_cSPS, m_cPPS, true, m_layerId);
[1029]1698        for (Int i=0; i<m_cIlpPic[j]->getPicSym()->getNumberOfCtusInFrame(); i++)
[442]1699        {
[1029]1700          m_cIlpPic[j]->getPicSym()->getCtu(i)->initCtu(m_cIlpPic[j], i);
[442]1701        }
1702      }
1703    }
[1009]1704
1705    if( m_cVPS.getNumRefLayers( m_layerId ) == 0 )
1706    {
[1043]1707      UInt layerIdx = m_cVPS.getLayerIdxInVps( m_layerId );
[1020]1708      RepFormat* repFmt = m_cVPS.getVpsRepFormat(m_cVPS.getVpsRepFormatIdx(layerIdx));
[1009]1709     
1710      if( m_cPPS.getLayerId() == 0 && 
1711          m_cSPS.getLayerId() == 0 &&
[1020]1712          repFmt->getChromaFormatVpsIdc() == m_cSPS.getChromaFormatIdc() &&
1713          repFmt->getSeparateColourPlaneVpsFlag() == 0 &&
1714          repFmt->getPicHeightVpsInLumaSamples() == m_cSPS.getPicHeightInLumaSamples() &&
1715          repFmt->getPicWidthVpsInLumaSamples()  == m_cSPS.getPicWidthInLumaSamples() &&
[1029]1716          repFmt->getBitDepthVpsLuma()   == m_cSPS.getBitDepth(CHANNEL_TYPE_LUMA) &&
1717          repFmt->getBitDepthVpsChroma() == m_cSPS.getBitDepth(CHANNEL_TYPE_CHROMA) &&
[1020]1718          repFmt->getConformanceWindowVps().getWindowLeftOffset()   == m_cSPS.getConformanceWindow().getWindowLeftOffset() &&
1719          repFmt->getConformanceWindowVps().getWindowRightOffset()  == m_cSPS.getConformanceWindow().getWindowRightOffset() &&
1720          repFmt->getConformanceWindowVps().getWindowTopOffset()    == m_cSPS.getConformanceWindow().getWindowTopOffset() &&
1721          repFmt->getConformanceWindowVps().getWindowBottomOffset() == m_cSPS.getConformanceWindow().getWindowBottomOffset() )
[1009]1722      {
1723        m_cVPS.setBaseLayerPSCompatibilityFlag(layerIdx, 1);
1724      }
1725      else
1726      {
1727        m_cVPS.setBaseLayerPSCompatibilityFlag(layerIdx, 0);
1728      }
1729    }
[442]1730  }
1731}
[540]1732
[1235]1733Window TEncTop::getScaledRefLayerWindowForLayer(Int layerId)
[540]1734{
[1030]1735  for (Int i = 0; i < m_numRefLayerLocationOffsets; i++)
[540]1736  {
[1030]1737    if (layerId == m_refLocationOffsetLayerId[i])
[540]1738    {
1739      return m_scaledRefLayerWindow[i];
1740    }
1741  }
1742
[1235]1743  return Window();
[540]1744}
[1090]1745
[1235]1746Window TEncTop::getRefLayerWindowForLayer(Int layerId)
[849]1747{
[1030]1748  for (Int i = 0; i < m_numRefLayerLocationOffsets; i++)
[849]1749  {
1750    if (layerId == m_refLayerId[i])
1751    {
1752      return m_refLayerWindow[i];
1753    }
1754  }
1755
[1235]1756  return Window();
[849]1757}
[494]1758#endif //SVC_EXTENSION
[313]1759//! \}
Note: See TracBrowser for help on using the repository browser.