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

Last change on this file since 1368 was 1366, checked in by seregin, 9 years ago

port rev 4483 and fixes for previous revisions

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