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

Last change on this file since 1431 was 1406, checked in by seregin, 10 years ago

port rev 4559

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