source: 3DVCSoftware/branches/HTM-DEV-0.1-dev/source/Lib/TLibEncoder/TEncTop.cpp @ 367

Last change on this file since 367 was 367, checked in by tech, 11 years ago

Further minor cleanups.

  • Property svn:eol-style set to native
File size: 33.6 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-2013, 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#if FAST_BIT_EST
42#include "TLibCommon/ContextModel.h"
43#endif
44
45//! \ingroup TLibEncoder
46//! \{
47
48// ====================================================================================================================
49// Constructor / destructor / create / destroy
50// ====================================================================================================================
51
52TEncTop::TEncTop()
53{
54  m_iPOCLast          = -1;
55  m_iNumPicRcvd       =  0;
56  m_uiNumAllPicCoded  =  0;
57  m_pppcRDSbacCoder   =  NULL;
58  m_pppcBinCoderCABAC =  NULL;
59  m_cRDGoOnSbacCoder.init( &m_cRDGoOnBinCoderCABAC );
60#if ENC_DEC_TRACE
61  g_hTrace = fopen( "TraceEnc.txt", "wb" );
62  g_bJustDoIt = g_bEncDecTraceDisable;
63  g_nSymbolCounter = 0;
64#endif
65
66  m_iMaxRefPicNum     = 0;
67
68#if FAST_BIT_EST
69  ContextModel::buildNextStateTable();
70#endif
71
72  m_pcSbacCoders           = NULL;
73  m_pcBinCoderCABACs       = NULL;
74  m_ppppcRDSbacCoders      = NULL;
75  m_ppppcBinCodersCABAC    = NULL;
76  m_pcRDGoOnSbacCoders     = NULL;
77  m_pcRDGoOnBinCodersCABAC = NULL;
78  m_pcBitCounters          = NULL;
79  m_pcRdCosts              = NULL;
80}
81
82TEncTop::~TEncTop()
83{
84#if ENC_DEC_TRACE
85  fclose( g_hTrace );
86#endif
87}
88
89Void TEncTop::create ()
90{
91  // initialize global variables
92  initROM();
93 
94  // create processing unit classes
95  m_cGOPEncoder.        create();
96  m_cSliceEncoder.      create( getSourceWidth(), getSourceHeight(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
97  m_cCuEncoder.         create( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight );
98  if (m_bUseSAO)
99  {
100    m_cEncSAO.setSaoLcuBoundary(getSaoLcuBoundary());
101    m_cEncSAO.setSaoLcuBasedOptimization(getSaoLcuBasedOptimization());
102    m_cEncSAO.setMaxNumOffsetsPerPic(getMaxNumOffsetsPerPic());
103    m_cEncSAO.create( getSourceWidth(), getSourceHeight(), g_uiMaxCUWidth, g_uiMaxCUHeight );
104    m_cEncSAO.createEncBuffer();
105  }
106#if ADAPTIVE_QP_SELECTION
107  if (m_bUseAdaptQpSelect)
108  {
109    m_cTrQuant.initSliceQpDelta();
110  }
111#endif
112  m_cLoopFilter.        create( g_uiMaxCUDepth );
113 
114#if RATE_CONTROL_LAMBDA_DOMAIN
115  if ( m_RCEnableRateControl )
116  {
117    m_cRateCtrl.init( m_framesToBeEncoded, m_RCTargetBitrate, m_iFrameRate, m_iGOPSize, m_iSourceWidth, m_iSourceHeight,
118                      g_uiMaxCUWidth, g_uiMaxCUHeight, m_RCKeepHierarchicalBit, m_RCUseLCUSeparateModel, m_GOPList );
119  }
120#else
121  m_cRateCtrl.create(getIntraPeriod(), getGOPSize(), getFrameRate(), getTargetBitrate(), getQP(), getNumLCUInUnit(), getSourceWidth(), getSourceHeight(), g_uiMaxCUWidth, g_uiMaxCUHeight);
122#endif
123  // if SBAC-based RD optimization is used
124  if( m_bUseSBACRD )
125  {
126    m_pppcRDSbacCoder = new TEncSbac** [g_uiMaxCUDepth+1];
127#if FAST_BIT_EST
128    m_pppcBinCoderCABAC = new TEncBinCABACCounter** [g_uiMaxCUDepth+1];
129#else
130    m_pppcBinCoderCABAC = new TEncBinCABAC** [g_uiMaxCUDepth+1];
131#endif
132   
133    for ( Int iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
134    {
135      m_pppcRDSbacCoder[iDepth] = new TEncSbac* [CI_NUM];
136#if FAST_BIT_EST
137      m_pppcBinCoderCABAC[iDepth] = new TEncBinCABACCounter* [CI_NUM];
138#else
139      m_pppcBinCoderCABAC[iDepth] = new TEncBinCABAC* [CI_NUM];
140#endif
141     
142      for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
143      {
144        m_pppcRDSbacCoder[iDepth][iCIIdx] = new TEncSbac;
145#if FAST_BIT_EST
146        m_pppcBinCoderCABAC [iDepth][iCIIdx] = new TEncBinCABACCounter;
147#else
148        m_pppcBinCoderCABAC [iDepth][iCIIdx] = new TEncBinCABAC;
149#endif
150        m_pppcRDSbacCoder   [iDepth][iCIIdx]->init( m_pppcBinCoderCABAC [iDepth][iCIIdx] );
151      }
152    }
153  }
154}
155
156/**
157 - Allocate coders required for wavefront for the nominated number of substreams.
158 .
159 \param iNumSubstreams Determines how much information to allocate.
160 */
161Void TEncTop::createWPPCoders(Int iNumSubstreams)
162{
163  if (m_pcSbacCoders != NULL)
164  {
165    return; // already generated.
166  }
167
168  m_iNumSubstreams         = iNumSubstreams;
169  m_pcSbacCoders           = new TEncSbac       [iNumSubstreams];
170  m_pcBinCoderCABACs       = new TEncBinCABAC   [iNumSubstreams];
171  m_pcRDGoOnSbacCoders     = new TEncSbac       [iNumSubstreams];
172  m_pcRDGoOnBinCodersCABAC = new TEncBinCABAC   [iNumSubstreams];
173  m_pcBitCounters          = new TComBitCounter [iNumSubstreams];
174  m_pcRdCosts              = new TComRdCost     [iNumSubstreams];
175
176  for ( UInt ui = 0 ; ui < iNumSubstreams; ui++ )
177  {
178    m_pcRDGoOnSbacCoders[ui].init( &m_pcRDGoOnBinCodersCABAC[ui] );
179    m_pcSbacCoders[ui].init( &m_pcBinCoderCABACs[ui] );
180  }
181  if( m_bUseSBACRD )
182  {
183    m_ppppcRDSbacCoders      = new TEncSbac***    [iNumSubstreams];
184    m_ppppcBinCodersCABAC    = new TEncBinCABAC***[iNumSubstreams];
185    for ( UInt ui = 0 ; ui < iNumSubstreams ; ui++ )
186    {
187      m_ppppcRDSbacCoders[ui]  = new TEncSbac** [g_uiMaxCUDepth+1];
188      m_ppppcBinCodersCABAC[ui]= new TEncBinCABAC** [g_uiMaxCUDepth+1];
189     
190      for ( Int iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
191      {
192        m_ppppcRDSbacCoders[ui][iDepth]  = new TEncSbac*     [CI_NUM];
193        m_ppppcBinCodersCABAC[ui][iDepth]= new TEncBinCABAC* [CI_NUM];
194
195        for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
196        {
197          m_ppppcRDSbacCoders  [ui][iDepth][iCIIdx] = new TEncSbac;
198          m_ppppcBinCodersCABAC[ui][iDepth][iCIIdx] = new TEncBinCABAC;
199          m_ppppcRDSbacCoders  [ui][iDepth][iCIIdx]->init( m_ppppcBinCodersCABAC[ui][iDepth][iCIIdx] );
200        }
201      }
202    }
203  }
204}
205
206Void TEncTop::destroy ()
207{
208  // destroy processing unit classes
209  m_cGOPEncoder.        destroy();
210  m_cSliceEncoder.      destroy();
211  m_cCuEncoder.         destroy();
212  if (m_cSPS.getUseSAO())
213  {
214    m_cEncSAO.destroy();
215    m_cEncSAO.destroyEncBuffer();
216  }
217  m_cLoopFilter.        destroy();
218  m_cRateCtrl.          destroy();
219  // SBAC RD
220  if( m_bUseSBACRD )
221  {
222    Int iDepth;
223    for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
224    {
225      for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
226      {
227        delete m_pppcRDSbacCoder[iDepth][iCIIdx];
228        delete m_pppcBinCoderCABAC[iDepth][iCIIdx];
229      }
230    }
231   
232    for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
233    {
234      delete [] m_pppcRDSbacCoder[iDepth];
235      delete [] m_pppcBinCoderCABAC[iDepth];
236    }
237   
238    delete [] m_pppcRDSbacCoder;
239    delete [] m_pppcBinCoderCABAC;
240
241    for ( UInt ui = 0; ui < m_iNumSubstreams; ui++ )
242    {
243      for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
244      {
245        for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
246        {
247          delete m_ppppcRDSbacCoders  [ui][iDepth][iCIIdx];
248          delete m_ppppcBinCodersCABAC[ui][iDepth][iCIIdx];
249        }
250      }
251
252      for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
253      {
254        delete [] m_ppppcRDSbacCoders  [ui][iDepth];
255        delete [] m_ppppcBinCodersCABAC[ui][iDepth];
256      }
257      delete[] m_ppppcRDSbacCoders  [ui];
258      delete[] m_ppppcBinCodersCABAC[ui];
259    }
260    delete[] m_ppppcRDSbacCoders;
261    delete[] m_ppppcBinCodersCABAC;
262  }
263  delete[] m_pcSbacCoders;
264  delete[] m_pcBinCoderCABACs;
265  delete[] m_pcRDGoOnSbacCoders; 
266  delete[] m_pcRDGoOnBinCodersCABAC;
267  delete[] m_pcBitCounters;
268  delete[] m_pcRdCosts;
269 
270    // destroy ROM
271  destroyROM();
272
273  return;
274}
275
276Void TEncTop::init()
277{
278  // initialize SPS
279  xInitSPS();
280 
281  /* set the VPS profile information */
282  *m_cVPS.getPTL() = *m_cSPS.getPTL();
283#if L0043_TIMING_INFO
284  m_cVPS.getTimingInfo()->setTimingInfoPresentFlag       ( false );
285#endif
286  // initialize PPS
287  m_cPPS.setSPS(&m_cSPS);
288  xInitPPS();
289  xInitRPS();
290
291  xInitPPSforTiles();
292
293  // initialize processing unit classes
294  m_cGOPEncoder.  init( this );
295  m_cSliceEncoder.init( this );
296  m_cCuEncoder.   init( this );
297 
298  // initialize transform & quantization class
299  m_pcCavlcCoder = getCavlcCoder();
300 
301  m_cTrQuant.init( 1 << m_uiQuadtreeTULog2MaxSize,
302                  m_useRDOQ, 
303                  m_useRDOQTS,
304                  true 
305                  ,m_useTransformSkipFast
306#if ADAPTIVE_QP_SELECTION                 
307                  , m_bUseAdaptQpSelect
308#endif
309                  );
310 
311  // initialize encoder search class
312  m_cSearch.init( this, &m_cTrQuant, m_iSearchRange, m_bipredSearchRange, m_iFastSearch, 0, &m_cEntropyCoder, &m_cRdCost, getRDSbacCoder(), getRDGoOnSbacCoder() );
313
314  m_iMaxRefPicNum = 0;
315}
316
317// ====================================================================================================================
318// Public member functions
319// ====================================================================================================================
320
321Void TEncTop::deletePicBuffer()
322{
323  TComList<TComPic*>::iterator iterPic = m_cListPic.begin();
324  Int iSize = Int( m_cListPic.size() );
325 
326  for ( Int i = 0; i < iSize; i++ )
327  {
328    TComPic* pcPic = *(iterPic++);
329   
330    pcPic->destroy();
331    delete pcPic;
332    pcPic = NULL;
333  }
334}
335
336/**
337 - Application has picture buffer list with size of GOP + 1
338 - Picture buffer list acts like as ring buffer
339 - End of the list has the latest picture
340 .
341 \param   flush               cause encoder to encode a partial GOP
342 \param   pcPicYuvOrg         original YUV picture
343 \retval  rcListPicYuvRecOut  list of reconstruction YUV pictures
344 \retval  rcListBitstreamOut  list of output bitstreams
345 \retval  iNumEncoded         number of encoded pictures
346 */
347Void TEncTop::encode(Bool flush, TComPicYuv* pcPicYuvOrg, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int& iNumEncoded )
348{
349  if (pcPicYuvOrg) {
350    // get original YUV
351    TComPic* pcPicCurr = NULL;
352    xGetNewPicBuffer( pcPicCurr );
353    pcPicYuvOrg->copyToPic( pcPicCurr->getPicYuvOrg() );
354
355    // compute image characteristics
356    if ( getUseAdaptiveQP() )
357    {
358      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcPicCurr ) );
359    }
360  }
361 
362  if (!m_iNumPicRcvd || (!flush && m_iPOCLast != 0 && m_iNumPicRcvd != m_iGOPSize && m_iGOPSize))
363  {
364    iNumEncoded = 0;
365    return;
366  }
367 
368#if RATE_CONTROL_LAMBDA_DOMAIN
369  if ( m_RCEnableRateControl )
370  {
371    m_cRateCtrl.initRCGOP( m_iNumPicRcvd );
372  }
373#endif
374
375  // compress GOP
376  m_cGOPEncoder.compressGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut);
377
378#if RATE_CONTROL_LAMBDA_DOMAIN
379  if ( m_RCEnableRateControl )
380  {
381    m_cRateCtrl.destroyRCGOP();
382  }
383#endif
384 
385  iNumEncoded         = m_iNumPicRcvd;
386  m_iNumPicRcvd       = 0;
387  m_uiNumAllPicCoded += iNumEncoded;
388}
389
390// ====================================================================================================================
391// Protected member functions
392// ====================================================================================================================
393
394/**
395 - Application has picture buffer list with size of GOP + 1
396 - Picture buffer list acts like as ring buffer
397 - End of the list has the latest picture
398 .
399 \retval rpcPic obtained picture buffer
400 */
401Void TEncTop::xGetNewPicBuffer ( TComPic*& rpcPic )
402{
403  TComSlice::sortPicList(m_cListPic);
404 
405  if (m_cListPic.size() >= (UInt)(m_iGOPSize + getMaxDecPicBuffering(MAX_TLAYER-1) + 2) )
406  {
407    TComList<TComPic*>::iterator iterPic  = m_cListPic.begin();
408    Int iSize = Int( m_cListPic.size() );
409    for ( Int i = 0; i < iSize; i++ )
410    {
411      rpcPic = *(iterPic++);
412      if(rpcPic->getSlice(0)->isReferenced() == false)
413      {
414        break;
415      }
416    }
417  }
418  else
419  {
420    if ( getUseAdaptiveQP() )
421    {
422      TEncPic* pcEPic = new TEncPic;
423      pcEPic->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, m_cPPS.getMaxCuDQPDepth()+1 ,
424                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics);
425      rpcPic = pcEPic;
426    }
427    else
428    {
429      rpcPic = new TComPic;
430
431      rpcPic->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
432                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics);
433    }
434    if (getUseSAO())
435    {
436      rpcPic->getPicSym()->allocSaoParam(&m_cEncSAO);
437    }
438    m_cListPic.pushBack( rpcPic );
439  }
440  rpcPic->setReconMark (false);
441 
442  m_iPOCLast++;
443  m_iNumPicRcvd++;
444 
445  rpcPic->getSlice(0)->setPOC( m_iPOCLast );
446  // mark it should be extended
447  rpcPic->getPicYuvRec()->setBorderExtension(false);
448}
449
450Void TEncTop::xInitSPS()
451{
452  ProfileTierLevel& profileTierLevel = *m_cSPS.getPTL()->getGeneralPTL();
453  profileTierLevel.setLevelIdc(m_level);
454  profileTierLevel.setTierFlag(m_levelTier);
455  profileTierLevel.setProfileIdc(m_profile);
456  profileTierLevel.setProfileCompatibilityFlag(m_profile, 1);
457#if L0046_CONSTRAINT_FLAGS
458  profileTierLevel.setProgressiveSourceFlag(m_progressiveSourceFlag);
459  profileTierLevel.setInterlacedSourceFlag(m_interlacedSourceFlag);
460  profileTierLevel.setNonPackedConstraintFlag(m_nonPackedConstraintFlag);
461  profileTierLevel.setFrameOnlyConstraintFlag(m_frameOnlyConstraintFlag);
462#endif
463 
464  if (m_profile == Profile::MAIN10 && g_bitDepthY == 8 && g_bitDepthC == 8)
465  {
466    /* The above constraint is equal to Profile::MAIN */
467    profileTierLevel.setProfileCompatibilityFlag(Profile::MAIN, 1);
468  }
469  if (m_profile == Profile::MAIN)
470  {
471    /* A Profile::MAIN10 decoder can always decode Profile::MAIN */
472    profileTierLevel.setProfileCompatibilityFlag(Profile::MAIN10, 1);
473  }
474  /* XXX: should Main be marked as compatible with still picture? */
475  /* XXX: may be a good idea to refactor the above into a function
476   * that chooses the actual compatibility based upon options */
477
478  m_cSPS.setPicWidthInLumaSamples         ( m_iSourceWidth      );
479  m_cSPS.setPicHeightInLumaSamples        ( m_iSourceHeight     );
480  m_cSPS.setConformanceWindow             ( m_conformanceWindow );
481  m_cSPS.setMaxCUWidth    ( g_uiMaxCUWidth      );
482  m_cSPS.setMaxCUHeight   ( g_uiMaxCUHeight     );
483  m_cSPS.setMaxCUDepth    ( g_uiMaxCUDepth      );
484
485  Int minCUSize = m_cSPS.getMaxCUWidth() >> ( m_cSPS.getMaxCUDepth()-g_uiAddCUDepth );
486  Int log2MinCUSize = 0;
487  while(minCUSize > 1)
488  {
489    minCUSize >>= 1;
490    log2MinCUSize++;
491  }
492
493  m_cSPS.setLog2MinCodingBlockSize(log2MinCUSize);
494  m_cSPS.setLog2DiffMaxMinCodingBlockSize(m_cSPS.getMaxCUDepth()-g_uiAddCUDepth);
495 
496  m_cSPS.setPCMLog2MinSize (m_uiPCMLog2MinSize);
497  m_cSPS.setUsePCM        ( m_usePCM           );
498  m_cSPS.setPCMLog2MaxSize( m_pcmLog2MaxSize  );
499
500  m_cSPS.setQuadtreeTULog2MaxSize( m_uiQuadtreeTULog2MaxSize );
501  m_cSPS.setQuadtreeTULog2MinSize( m_uiQuadtreeTULog2MinSize );
502  m_cSPS.setQuadtreeTUMaxDepthInter( m_uiQuadtreeTUMaxDepthInter    );
503  m_cSPS.setQuadtreeTUMaxDepthIntra( m_uiQuadtreeTUMaxDepthIntra    );
504 
505  m_cSPS.setTMVPFlagsPresent(false);
506  m_cSPS.setUseLossless   ( m_useLossless  );
507
508  m_cSPS.setMaxTrSize   ( 1 << m_uiQuadtreeTULog2MaxSize );
509#if !L0034_COMBINED_LIST_CLEANUP
510  m_cSPS.setUseLComb    ( m_bUseLComb           );
511#endif
512 
513  Int i;
514 
515  for (i = 0; i < g_uiMaxCUDepth-g_uiAddCUDepth; i++ )
516  {
517    m_cSPS.setAMPAcc( i, m_useAMP );
518    //m_cSPS.setAMPAcc( i, 1 );
519  }
520
521  m_cSPS.setUseAMP ( m_useAMP );
522
523  for (i = g_uiMaxCUDepth-g_uiAddCUDepth; i < g_uiMaxCUDepth; i++ )
524  {
525    m_cSPS.setAMPAcc(i, 0);
526  }
527
528  m_cSPS.setBitDepthY( g_bitDepthY );
529  m_cSPS.setBitDepthC( g_bitDepthC );
530
531  m_cSPS.setQpBDOffsetY ( 6*(g_bitDepthY - 8) );
532  m_cSPS.setQpBDOffsetC ( 6*(g_bitDepthC - 8) );
533
534  m_cSPS.setUseSAO( m_bUseSAO );
535
536  m_cSPS.setMaxTLayers( m_maxTempLayer );
537  m_cSPS.setTemporalIdNestingFlag( ( m_maxTempLayer == 1 ) ? true : false );
538  for ( i = 0; i < m_cSPS.getMaxTLayers(); i++ )
539  {
540    m_cSPS.setMaxDecPicBuffering(m_maxDecPicBuffering[i], i);
541    m_cSPS.setNumReorderPics(m_numReorderPics[i], i);
542  }
543  m_cSPS.setPCMBitDepthLuma (g_uiPCMBitDepthLuma);
544  m_cSPS.setPCMBitDepthChroma (g_uiPCMBitDepthChroma);
545  m_cSPS.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag );
546
547  m_cSPS.setScalingListFlag ( (m_useScalingListId == 0) ? 0 : 1 );
548
549  m_cSPS.setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
550
551  m_cSPS.setVuiParametersPresentFlag(getVuiParametersPresentFlag());
552  if (m_cSPS.getVuiParametersPresentFlag())
553  {
554    TComVUI* pcVUI = m_cSPS.getVuiParameters();
555    pcVUI->setAspectRatioInfoPresentFlag(getAspectRatioIdc() != -1);
556    pcVUI->setAspectRatioIdc(getAspectRatioIdc());
557    pcVUI->setSarWidth(getSarWidth());
558    pcVUI->setSarHeight(getSarHeight());
559    pcVUI->setOverscanInfoPresentFlag(getOverscanInfoPresentFlag());
560    pcVUI->setOverscanAppropriateFlag(getOverscanAppropriateFlag());
561    pcVUI->setVideoSignalTypePresentFlag(getVideoSignalTypePresentFlag());
562    pcVUI->setVideoFormat(getVideoFormat());
563    pcVUI->setVideoFullRangeFlag(getVideoFullRangeFlag());
564    pcVUI->setColourDescriptionPresentFlag(getColourDescriptionPresentFlag());
565    pcVUI->setColourPrimaries(getColourPrimaries());
566    pcVUI->setTransferCharacteristics(getTransferCharacteristics());
567    pcVUI->setMatrixCoefficients(getMatrixCoefficients());
568    pcVUI->setChromaLocInfoPresentFlag(getChromaLocInfoPresentFlag());
569    pcVUI->setChromaSampleLocTypeTopField(getChromaSampleLocTypeTopField());
570    pcVUI->setChromaSampleLocTypeBottomField(getChromaSampleLocTypeBottomField());
571    pcVUI->setNeutralChromaIndicationFlag(getNeutralChromaIndicationFlag());
572    pcVUI->setDefaultDisplayWindow(getDefaultDisplayWindow());
573    pcVUI->setFrameFieldInfoPresentFlag(getFrameFieldInfoPresentFlag());
574    pcVUI->setFieldSeqFlag(false);
575    pcVUI->setHrdParametersPresentFlag(false);
576#if L0043_TIMING_INFO
577    pcVUI->getTimingInfo()->setPocProportionalToTimingFlag(getPocProportionalToTimingFlag());
578    pcVUI->getTimingInfo()->setNumTicksPocDiffOneMinus1   (getNumTicksPocDiffOneMinus1()   );
579#else
580    pcVUI->setPocProportionalToTimingFlag(getPocProportionalToTimingFlag());
581    pcVUI->setNumTicksPocDiffOneMinus1   (getNumTicksPocDiffOneMinus1()   );
582#endif
583    pcVUI->setBitstreamRestrictionFlag(getBitstreamRestrictionFlag());
584    pcVUI->setTilesFixedStructureFlag(getTilesFixedStructureFlag());
585    pcVUI->setMotionVectorsOverPicBoundariesFlag(getMotionVectorsOverPicBoundariesFlag());
586    pcVUI->setMinSpatialSegmentationIdc(getMinSpatialSegmentationIdc());
587    pcVUI->setMaxBytesPerPicDenom(getMaxBytesPerPicDenom());
588    pcVUI->setMaxBitsPerMinCuDenom(getMaxBitsPerMinCuDenom());
589    pcVUI->setLog2MaxMvLengthHorizontal(getLog2MaxMvLengthHorizontal());
590    pcVUI->setLog2MaxMvLengthVertical(getLog2MaxMvLengthVertical());
591  }
592}
593
594Void TEncTop::xInitPPS()
595{
596  m_cPPS.setConstrainedIntraPred( m_bUseConstrainedIntraPred );
597  Bool bUseDQP = (getMaxCuDQPDepth() > 0)? true : false;
598
599  Int lowestQP = - m_cSPS.getQpBDOffsetY();
600
601  if(getUseLossless())
602  {
603    if ((getMaxCuDQPDepth() == 0) && (getMaxDeltaQP() == 0 ) && (getQP() == lowestQP) )
604    {
605      bUseDQP = false;
606    }
607    else
608    {
609      bUseDQP = true;
610    }
611  }
612  else
613  {
614    if(bUseDQP == false)
615    {
616      if((getMaxDeltaQP() != 0 )|| getUseAdaptiveQP())
617      {
618        bUseDQP = true;
619      }
620    }
621  }
622
623  if(bUseDQP)
624  {
625    m_cPPS.setUseDQP(true);
626    m_cPPS.setMaxCuDQPDepth( m_iMaxCuDQPDepth );
627    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
628  }
629  else
630  {
631    m_cPPS.setUseDQP(false);
632    m_cPPS.setMaxCuDQPDepth( 0 );
633    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
634  }
635
636#if RATE_CONTROL_LAMBDA_DOMAIN
637  if ( m_RCEnableRateControl )
638  {
639    m_cPPS.setUseDQP(true);
640    m_cPPS.setMaxCuDQPDepth( 0 );
641    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
642  } 
643#endif
644
645  m_cPPS.setChromaCbQpOffset( m_chromaCbQpOffset );
646  m_cPPS.setChromaCrQpOffset( m_chromaCrQpOffset );
647
648  m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams);
649  m_cPPS.setEntropyCodingSyncEnabledFlag( m_iWaveFrontSynchro > 0 );
650  m_cPPS.setTilesEnabledFlag( (m_iNumColumnsMinus1 > 0 || m_iNumRowsMinus1 > 0) );
651  m_cPPS.setUseWP( m_useWeightedPred );
652  m_cPPS.setWPBiPred( m_useWeightedBiPred );
653  m_cPPS.setOutputFlagPresentFlag( false );
654  m_cPPS.setSignHideFlag(getSignHideFlag());
655#if L0386_DB_METRIC
656  if ( getDeblockingFilterMetric() )
657  {
658    m_cPPS.setDeblockingFilterControlPresentFlag (true);
659    m_cPPS.setDeblockingFilterOverrideEnabledFlag(true);
660    m_cPPS.setPicDisableDeblockingFilterFlag(false);
661    m_cPPS.setDeblockingFilterBetaOffsetDiv2(0);
662    m_cPPS.setDeblockingFilterTcOffsetDiv2(0);
663  } 
664  else
665  {
666  m_cPPS.setDeblockingFilterControlPresentFlag (m_DeblockingFilterControlPresent );
667  }
668#else
669  m_cPPS.setDeblockingFilterControlPresentFlag (m_DeblockingFilterControlPresent );
670#endif
671  m_cPPS.setLog2ParallelMergeLevelMinus2   (m_log2ParallelMergeLevelMinus2 );
672  m_cPPS.setCabacInitPresentFlag(CABAC_INIT_PRESENT_FLAG);
673  m_cPPS.setLoopFilterAcrossSlicesEnabledFlag( m_bLFCrossSliceBoundaryFlag );
674  Int histogram[MAX_NUM_REF + 1];
675  for( Int i = 0; i <= MAX_NUM_REF; i++ )
676  {
677    histogram[i]=0;
678  }
679  for( Int i = 0; i < getGOPSize(); i++ )
680  {
681    assert(getGOPEntry(i).m_numRefPicsActive >= 0 && getGOPEntry(i).m_numRefPicsActive <= MAX_NUM_REF);
682    histogram[getGOPEntry(i).m_numRefPicsActive]++;
683  }
684  Int maxHist=-1;
685  Int bestPos=0;
686  for( Int i = 0; i <= MAX_NUM_REF; i++ )
687  {
688    if(histogram[i]>maxHist)
689    {
690      maxHist=histogram[i];
691      bestPos=i;
692    }
693  }
694#if L0323_LIMIT_DEFAULT_LIST_SIZE
695  assert(bestPos <= 15);
696#endif
697  m_cPPS.setNumRefIdxL0DefaultActive(bestPos);
698  m_cPPS.setNumRefIdxL1DefaultActive(bestPos);
699  m_cPPS.setTransquantBypassEnableFlag(getTransquantBypassEnableFlag());
700  m_cPPS.setUseTransformSkip( m_useTransformSkip );
701  if (m_sliceSegmentMode)
702  {
703    m_cPPS.setDependentSliceSegmentsEnabledFlag( true );
704  }
705  if( m_cPPS.getDependentSliceSegmentsEnabledFlag() )
706  {
707    Int NumCtx = m_cPPS.getEntropyCodingSyncEnabledFlag()?2:1;
708    m_cSliceEncoder.initCtxMem( NumCtx );
709    for ( UInt st = 0; st < NumCtx; st++ )
710    {
711      TEncSbac* ctx = NULL;
712      ctx = new TEncSbac;
713      ctx->init( &m_cBinCoderCABAC );
714      m_cSliceEncoder.setCtxMem( ctx, st );
715    }
716  }
717}
718
719//Function for initializing m_RPSList, a list of TComReferencePictureSet, based on the GOPEntry objects read from the config file.
720Void TEncTop::xInitRPS()
721{
722  TComReferencePictureSet*      rps;
723 
724  m_cSPS.createRPSList(getGOPSize()+m_extraRPSs);
725  TComRPSList* rpsList = m_cSPS.getRPSList();
726
727  for( Int i = 0; i < getGOPSize()+m_extraRPSs; i++) 
728  {
729    GOPEntry ge = getGOPEntry(i);
730    rps = rpsList->getReferencePictureSet(i);
731    rps->setNumberOfPictures(ge.m_numRefPics);
732    rps->setNumRefIdc(ge.m_numRefIdc);
733    Int numNeg = 0;
734    Int numPos = 0;
735    for( Int j = 0; j < ge.m_numRefPics; j++)
736    {
737      rps->setDeltaPOC(j,ge.m_referencePics[j]);
738      rps->setUsed(j,ge.m_usedByCurrPic[j]);
739      if(ge.m_referencePics[j]>0)
740      {
741        numPos++;
742      }
743      else
744      {
745        numNeg++;
746      }
747    }
748    rps->setNumberOfNegativePictures(numNeg);
749    rps->setNumberOfPositivePictures(numPos);
750
751    // handle inter RPS intialization from the config file.
752#if AUTO_INTER_RPS
753    rps->setInterRPSPrediction(ge.m_interRPSPrediction > 0);  // not very clean, converting anything > 0 to true.
754    rps->setDeltaRIdxMinus1(0);                               // index to the Reference RPS is always the previous one.
755    TComReferencePictureSet*     RPSRef = rpsList->getReferencePictureSet(i-1);  // get the reference RPS
756
757    if (ge.m_interRPSPrediction == 2)  // Automatic generation of the inter RPS idc based on the RIdx provided.
758    {
759      Int deltaRPS = getGOPEntry(i-1).m_POC - ge.m_POC;  // the ref POC - current POC
760      Int numRefDeltaPOC = RPSRef->getNumberOfPictures();
761
762      rps->setDeltaRPS(deltaRPS);           // set delta RPS
763      rps->setNumRefIdc(numRefDeltaPOC+1);  // set the numRefIdc to the number of pictures in the reference RPS + 1.
764      Int count=0;
765      for (Int j = 0; j <= numRefDeltaPOC; j++ ) // cycle through pics in reference RPS.
766      {
767        Int RefDeltaPOC = (j<numRefDeltaPOC)? RPSRef->getDeltaPOC(j): 0;  // if it is the last decoded picture, set RefDeltaPOC = 0
768        rps->setRefIdc(j, 0);
769        for (Int k = 0; k < rps->getNumberOfPictures(); k++ )  // cycle through pics in current RPS.
770        {
771          if (rps->getDeltaPOC(k) == ( RefDeltaPOC + deltaRPS))  // if the current RPS has a same picture as the reference RPS.
772          {
773              rps->setRefIdc(j, (rps->getUsed(k)?1:2));
774              count++;
775              break;
776          }
777        }
778      }
779      if (count != rps->getNumberOfPictures())
780      {
781        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");
782        rps->setInterRPSPrediction(0);
783      }
784    }
785    else if (ge.m_interRPSPrediction == 1)  // inter RPS idc based on the RefIdc values provided in config file.
786    {
787      rps->setDeltaRPS(ge.m_deltaRPS);
788      rps->setNumRefIdc(ge.m_numRefIdc);
789      for (Int j = 0; j < ge.m_numRefIdc; j++ )
790      {
791        rps->setRefIdc(j, ge.m_refIdc[j]);
792      }
793#if WRITE_BACK
794      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
795      // computed from the RefIdc.  A warning is printed if they are not identical.
796      numNeg = 0;
797      numPos = 0;
798      TComReferencePictureSet      RPSTemp;  // temporary variable
799
800      for (Int j = 0; j < ge.m_numRefIdc; j++ )
801      {
802        if (ge.m_refIdc[j])
803        {
804          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
805          RPSTemp.setDeltaPOC((numNeg+numPos),deltaPOC);
806          RPSTemp.setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
807          if (deltaPOC<0)
808          {
809            numNeg++;
810          }
811          else
812          {
813            numPos++;
814          }
815        }
816      }
817      if (numNeg != rps->getNumberOfNegativePictures())
818      {
819        printf("Warning: number of negative pictures in RPS is different between intra and inter RPS specified in the config file.\n");
820        rps->setNumberOfNegativePictures(numNeg);
821        rps->setNumberOfPositivePictures(numNeg+numPos);
822      }
823      if (numPos != rps->getNumberOfPositivePictures())
824      {
825        printf("Warning: number of positive pictures in RPS is different between intra and inter RPS specified in the config file.\n");
826        rps->setNumberOfPositivePictures(numPos);
827        rps->setNumberOfPositivePictures(numNeg+numPos);
828      }
829      RPSTemp.setNumberOfPictures(numNeg+numPos);
830      RPSTemp.setNumberOfNegativePictures(numNeg);
831      RPSTemp.sortDeltaPOC();     // sort the created delta POC before comparing
832      // check if Delta POC and Used are the same
833      // print warning if they are not.
834      for (Int j = 0; j < ge.m_numRefIdc; j++ )
835      {
836        if (RPSTemp.getDeltaPOC(j) != rps->getDeltaPOC(j))
837        {
838          printf("Warning: delta POC is different between intra RPS and inter RPS specified in the config file.\n");
839          rps->setDeltaPOC(j,RPSTemp.getDeltaPOC(j));
840        }
841        if (RPSTemp.getUsed(j) != rps->getUsed(j))
842        {
843          printf("Warning: Used by Current in RPS is different between intra and inter RPS specified in the config file.\n");
844          rps->setUsed(j,RPSTemp.getUsed(j));
845        }
846      }
847#endif
848    }
849#else
850    rps->setInterRPSPrediction(ge.m_interRPSPrediction);
851    if (ge.m_interRPSPrediction)
852    {
853      rps->setDeltaRIdxMinus1(0);
854      rps->setDeltaRPS(ge.m_deltaRPS);
855      rps->setNumRefIdc(ge.m_numRefIdc);
856      for (Int j = 0; j < ge.m_numRefIdc; j++ )
857      {
858        rps->setRefIdc(j, ge.m_refIdc[j]);
859      }
860#if WRITE_BACK
861      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
862      // computed from the RefIdc.  This is not necessary if both are identical. Currently there is no check to see if they are identical.
863      numNeg = 0;
864      numPos = 0;
865      TComReferencePictureSet*     RPSRef = m_RPSList.getReferencePictureSet(i-1);
866
867      for (Int j = 0; j < ge.m_numRefIdc; j++ )
868      {
869        if (ge.m_refIdc[j])
870        {
871          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
872          rps->setDeltaPOC((numNeg+numPos),deltaPOC);
873          rps->setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
874          if (deltaPOC<0)
875          {
876            numNeg++;
877          }
878          else
879          {
880            numPos++;
881          }
882        }
883      }
884      rps->setNumberOfNegativePictures(numNeg);
885      rps->setNumberOfPositivePictures(numPos);
886      rps->sortDeltaPOC();
887#endif
888    }
889#endif //INTER_RPS_AUTO
890  }
891 
892}
893
894   // This is a function that
895   // determines what Reference Picture Set to use
896   // for a specific slice (with POC = POCCurr)
897Void TEncTop::selectReferencePictureSet(TComSlice* slice, Int POCCurr, Int GOPid )
898{
899  slice->setRPSidx(GOPid);
900
901  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
902  {   
903    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
904    {
905      Int POCIndex = POCCurr%m_uiIntraPeriod;
906      if(POCIndex == 0)
907      {
908        POCIndex = m_uiIntraPeriod;
909      }
910      if(POCIndex == m_GOPList[extraNum].m_POC)
911      {
912        slice->setRPSidx(extraNum);
913      }
914    }
915    else
916    {
917      if(POCCurr==m_GOPList[extraNum].m_POC)
918      {
919        slice->setRPSidx(extraNum);
920      }
921    }
922  }
923
924  slice->setRPS(getSPS()->getRPSList()->getReferencePictureSet(slice->getRPSidx()));
925  slice->getRPS()->setNumberOfPictures(slice->getRPS()->getNumberOfNegativePictures()+slice->getRPS()->getNumberOfPositivePictures());
926
927}
928
929#if L0208_SOP_DESCRIPTION_SEI
930Int TEncTop::getReferencePictureSetIdxForSOP(TComSlice* slice, Int POCCurr, Int GOPid )
931{
932  int rpsIdx = GOPid;
933
934  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
935  {   
936    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
937    {
938      Int POCIndex = POCCurr%m_uiIntraPeriod;
939      if(POCIndex == 0)
940      {
941        POCIndex = m_uiIntraPeriod;
942      }
943      if(POCIndex == m_GOPList[extraNum].m_POC)
944      {
945        rpsIdx = extraNum;
946      }
947    }
948    else
949    {
950      if(POCCurr==m_GOPList[extraNum].m_POC)
951      {
952        rpsIdx = extraNum;
953      }
954    }
955  }
956
957  return rpsIdx;
958}
959#endif
960
961Void  TEncTop::xInitPPSforTiles()
962{
963  m_cPPS.setUniformSpacingFlag( m_iUniformSpacingIdr );
964  m_cPPS.setNumColumnsMinus1( m_iNumColumnsMinus1 );
965  m_cPPS.setNumRowsMinus1( m_iNumRowsMinus1 );
966  if( m_iUniformSpacingIdr == 0 )
967  {
968    m_cPPS.setColumnWidth( m_puiColumnWidth );
969    m_cPPS.setRowHeight( m_puiRowHeight );
970  }
971  m_cPPS.setLoopFilterAcrossTilesEnabledFlag( m_loopFilterAcrossTilesEnabledFlag );
972
973  // # substreams is "per tile" when tiles are independent.
974  if (m_iWaveFrontSynchro
975    )
976  {
977    m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams * (m_iNumColumnsMinus1+1));
978  }
979}
980
981Void  TEncCfg::xCheckGSParameters()
982{
983  Int   iWidthInCU = ( m_iSourceWidth%g_uiMaxCUWidth ) ? m_iSourceWidth/g_uiMaxCUWidth + 1 : m_iSourceWidth/g_uiMaxCUWidth;
984  Int   iHeightInCU = ( m_iSourceHeight%g_uiMaxCUHeight ) ? m_iSourceHeight/g_uiMaxCUHeight + 1 : m_iSourceHeight/g_uiMaxCUHeight;
985  UInt  uiCummulativeColumnWidth = 0;
986  UInt  uiCummulativeRowHeight = 0;
987
988  //check the column relative parameters
989  if( m_iNumColumnsMinus1 >= (1<<(LOG2_MAX_NUM_COLUMNS_MINUS1+1)) )
990  {
991    printf( "The number of columns is larger than the maximum allowed number of columns.\n" );
992    exit( EXIT_FAILURE );
993  }
994
995  if( m_iNumColumnsMinus1 >= iWidthInCU )
996  {
997    printf( "The current picture can not have so many columns.\n" );
998    exit( EXIT_FAILURE );
999  }
1000
1001  if( m_iNumColumnsMinus1 && m_iUniformSpacingIdr==0 )
1002  {
1003    for(Int i=0; i<m_iNumColumnsMinus1; i++)
1004    {
1005      uiCummulativeColumnWidth += m_puiColumnWidth[i];
1006    }
1007
1008    if( uiCummulativeColumnWidth >= iWidthInCU )
1009    {
1010      printf( "The width of the column is too large.\n" );
1011      exit( EXIT_FAILURE );
1012    }
1013  }
1014
1015  //check the row relative parameters
1016  if( m_iNumRowsMinus1 >= (1<<(LOG2_MAX_NUM_ROWS_MINUS1+1)) )
1017  {
1018    printf( "The number of rows is larger than the maximum allowed number of rows.\n" );
1019    exit( EXIT_FAILURE );
1020  }
1021
1022  if( m_iNumRowsMinus1 >= iHeightInCU )
1023  {
1024    printf( "The current picture can not have so many rows.\n" );
1025    exit( EXIT_FAILURE );
1026  }
1027
1028  if( m_iNumRowsMinus1 && m_iUniformSpacingIdr==0 )
1029  {
1030    for(Int i=0; i<m_iNumRowsMinus1; i++)
1031      uiCummulativeRowHeight += m_puiRowHeight[i];
1032
1033    if( uiCummulativeRowHeight >= iHeightInCU )
1034    {
1035      printf( "The height of the row is too large.\n" );
1036      exit( EXIT_FAILURE );
1037    }
1038  }
1039}
1040//! \}
Note: See TracBrowser for help on using the repository browser.