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

Last change on this file since 1005 was 999, checked in by seregin, 10 years ago

use layerIdx in repformat

  • Property svn:eol-style set to native
File size: 60.7 KB
RevLine 
[313]1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
[595]6 * Copyright (c) 2010-2014, ITU/ISO/IEC
[313]7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TEncTop.cpp
35    \brief    encoder class
36*/
37
38#include "TLibCommon/CommonDef.h"
39#include "TEncTop.h"
40#include "TEncPic.h"
41#if FAST_BIT_EST
42#include "TLibCommon/ContextModel.h"
43#endif
44
45//! \ingroup TLibEncoder
46//! \{
47#if SVC_EXTENSION 
48Int TEncTop::m_iSPSIdCnt = 0;
49Int TEncTop::m_iPPSIdCnt = 0;
50TComVPS TEncCfg::m_cVPS;
51#endif
52
53// ====================================================================================================================
54// Constructor / destructor / create / destroy
55// ====================================================================================================================
56
57TEncTop::TEncTop()
58{
59  m_iPOCLast          = -1;
60  m_iNumPicRcvd       =  0;
61  m_uiNumAllPicCoded  =  0;
62  m_pppcRDSbacCoder   =  NULL;
63  m_pppcBinCoderCABAC =  NULL;
64  m_cRDGoOnSbacCoder.init( &m_cRDGoOnBinCoderCABAC );
65#if ENC_DEC_TRACE
66  g_hTrace = fopen( "TraceEnc.txt", "wb" );
67  g_bJustDoIt = g_bEncDecTraceDisable;
68  g_nSymbolCounter = 0;
69#endif
70
71  m_iMaxRefPicNum     = 0;
72
73#if FAST_BIT_EST
74  ContextModel::buildNextStateTable();
75#endif
76
77  m_pcSbacCoders           = NULL;
78  m_pcBinCoderCABACs       = NULL;
79  m_ppppcRDSbacCoders      = NULL;
80  m_ppppcBinCodersCABAC    = NULL;
81  m_pcRDGoOnSbacCoders     = NULL;
82  m_pcRDGoOnBinCodersCABAC = NULL;
83  m_pcBitCounters          = NULL;
84  m_pcRdCosts              = NULL;
[442]85#if SVC_EXTENSION
[313]86  memset(m_cIlpPic, 0, sizeof(m_cIlpPic));
87#if REF_IDX_MFM
88  m_bMFMEnabledFlag = false;
89#endif
90  m_numScaledRefLayerOffsets = 0;
[815]91#if POC_RESET_FLAG || POC_RESET_IDC_ENCODER
[442]92  m_pocAdjustmentValue     = 0;
93#endif
[540]94#if NO_CLRAS_OUTPUT_FLAG
95  m_noClrasOutputFlag          = false;
96  m_layerInitializedFlag       = false;
97  m_firstPicInLayerDecodedFlag = false;
98  m_noOutputOfPriorPicsFlags   = false;
99#endif
[903]100#if P0297_VPS_POC_LSB_ALIGNED_FLAG
101  m_pocDecrementedInDPBFlag    = false;
102#endif
[494]103#endif //SVC_EXTENSION
[313]104}
105
106TEncTop::~TEncTop()
107{
108#if ENC_DEC_TRACE
109  fclose( g_hTrace );
110#endif
111}
112
113Void TEncTop::create ()
114{
115#if !SVC_EXTENSION
116  // initialize global variables
117  initROM();
118#endif
[442]119
[313]120  // create processing unit classes
121#if SVC_EXTENSION
122  m_cGOPEncoder.        create( m_layerId );
123#else
124  m_cGOPEncoder.        create();
125#endif
[494]126#if AUXILIARY_PICTURES
127  m_cSliceEncoder.      create( getSourceWidth(), getSourceHeight(), m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
128#else
[313]129  m_cSliceEncoder.      create( getSourceWidth(), getSourceHeight(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
[494]130#endif
[313]131  m_cCuEncoder.         create( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight );
132  if (m_bUseSAO)
133  {
[540]134#if AUXILIARY_PICTURES
135    m_cEncSAO.create( getSourceWidth(), getSourceHeight(), m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
136#else
137    m_cEncSAO.create( getSourceWidth(), getSourceHeight(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
138#endif
139#if SAO_ENCODE_ALLOW_USE_PREDEBLOCK
140    m_cEncSAO.createEncData(getSaoLcuBoundary());
141#else
142    m_cEncSAO.createEncData();
143#endif
[313]144  }
145#if ADAPTIVE_QP_SELECTION
146  if (m_bUseAdaptQpSelect)
147  {
148    m_cTrQuant.initSliceQpDelta();
149  }
150#endif
151  m_cLoopFilter.        create( g_uiMaxCUDepth );
152 
153  if ( m_RCEnableRateControl )
154  {
155    m_cRateCtrl.init( m_framesToBeEncoded, m_RCTargetBitrate, m_iFrameRate, m_iGOPSize, m_iSourceWidth, m_iSourceHeight,
156                      g_uiMaxCUWidth, g_uiMaxCUHeight, m_RCKeepHierarchicalBit, m_RCUseLCUSeparateModel, m_GOPList );
157  }
[595]158
159  m_pppcRDSbacCoder = new TEncSbac** [g_uiMaxCUDepth+1];
[313]160#if FAST_BIT_EST
[595]161  m_pppcBinCoderCABAC = new TEncBinCABACCounter** [g_uiMaxCUDepth+1];
[313]162#else
[595]163  m_pppcBinCoderCABAC = new TEncBinCABAC** [g_uiMaxCUDepth+1];
[313]164#endif
[595]165
166  for ( Int iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
167  {
168    m_pppcRDSbacCoder[iDepth] = new TEncSbac* [CI_NUM];
[313]169#if FAST_BIT_EST
[595]170    m_pppcBinCoderCABAC[iDepth] = new TEncBinCABACCounter* [CI_NUM];
[313]171#else
[595]172    m_pppcBinCoderCABAC[iDepth] = new TEncBinCABAC* [CI_NUM];
[313]173#endif
[595]174
175    for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
176    {
177      m_pppcRDSbacCoder[iDepth][iCIIdx] = new TEncSbac;
[313]178#if FAST_BIT_EST
[595]179      m_pppcBinCoderCABAC [iDepth][iCIIdx] = new TEncBinCABACCounter;
[313]180#else
[595]181      m_pppcBinCoderCABAC [iDepth][iCIIdx] = new TEncBinCABAC;
[313]182#endif
[595]183      m_pppcRDSbacCoder   [iDepth][iCIIdx]->init( m_pppcBinCoderCABAC [iDepth][iCIIdx] );
[313]184    }
185  }
[494]186
187#if LAYER_CTB
188  memcpy(g_auiLayerZscanToRaster[m_layerId], g_auiZscanToRaster, sizeof( g_auiZscanToRaster ) );
189  memcpy(g_auiLayerRasterToZscan[m_layerId], g_auiRasterToZscan, sizeof( g_auiRasterToZscan ) );
190  memcpy(g_auiLayerRasterToPelX[m_layerId],  g_auiRasterToPelX,  sizeof( g_auiRasterToPelX ) );
191  memcpy(g_auiLayerRasterToPelY[m_layerId],  g_auiRasterToPelY,  sizeof( g_auiRasterToPelY ) );
192#endif
[313]193}
194
195/**
196 - Allocate coders required for wavefront for the nominated number of substreams.
197 .
198 \param iNumSubstreams Determines how much information to allocate.
199 */
200Void TEncTop::createWPPCoders(Int iNumSubstreams)
201{
202  if (m_pcSbacCoders != NULL)
203  {
204    return; // already generated.
205  }
206
207  m_iNumSubstreams         = iNumSubstreams;
208  m_pcSbacCoders           = new TEncSbac       [iNumSubstreams];
209  m_pcBinCoderCABACs       = new TEncBinCABAC   [iNumSubstreams];
210  m_pcRDGoOnSbacCoders     = new TEncSbac       [iNumSubstreams];
211  m_pcRDGoOnBinCodersCABAC = new TEncBinCABAC   [iNumSubstreams];
212  m_pcBitCounters          = new TComBitCounter [iNumSubstreams];
213  m_pcRdCosts              = new TComRdCost     [iNumSubstreams];
214
215  for ( UInt ui = 0 ; ui < iNumSubstreams; ui++ )
216  {
217    m_pcRDGoOnSbacCoders[ui].init( &m_pcRDGoOnBinCodersCABAC[ui] );
218    m_pcSbacCoders[ui].init( &m_pcBinCoderCABACs[ui] );
219  }
[595]220
221  m_ppppcRDSbacCoders      = new TEncSbac***    [iNumSubstreams];
222  m_ppppcBinCodersCABAC    = new TEncBinCABAC***[iNumSubstreams];
223  for ( UInt ui = 0 ; ui < iNumSubstreams ; ui++ )
[313]224  {
[595]225    m_ppppcRDSbacCoders[ui]  = new TEncSbac** [g_uiMaxCUDepth+1];
226    m_ppppcBinCodersCABAC[ui]= new TEncBinCABAC** [g_uiMaxCUDepth+1];
227
228    for ( Int iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
[313]229    {
[595]230      m_ppppcRDSbacCoders[ui][iDepth]  = new TEncSbac*     [CI_NUM];
231      m_ppppcBinCodersCABAC[ui][iDepth]= new TEncBinCABAC* [CI_NUM];
232
233      for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
[313]234      {
[595]235        m_ppppcRDSbacCoders  [ui][iDepth][iCIIdx] = new TEncSbac;
236        m_ppppcBinCodersCABAC[ui][iDepth][iCIIdx] = new TEncBinCABAC;
237        m_ppppcRDSbacCoders  [ui][iDepth][iCIIdx]->init( m_ppppcBinCodersCABAC[ui][iDepth][iCIIdx] );
[313]238      }
239    }
240  }
241}
242
243Void TEncTop::destroy ()
244{
245  // destroy processing unit classes
246  m_cGOPEncoder.        destroy();
247  m_cSliceEncoder.      destroy();
248  m_cCuEncoder.         destroy();
249  if (m_cSPS.getUseSAO())
250  {
[540]251    m_cEncSAO.destroyEncData();
[313]252    m_cEncSAO.destroy();
253  }
254  m_cLoopFilter.        destroy();
255  m_cRateCtrl.          destroy();
[595]256
257  Int iDepth;
258  for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
[313]259  {
[595]260    for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
261    {
262      delete m_pppcRDSbacCoder[iDepth][iCIIdx];
263      delete m_pppcBinCoderCABAC[iDepth][iCIIdx];
264    }
265  }
266
267  for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
268  {
269    delete [] m_pppcRDSbacCoder[iDepth];
270    delete [] m_pppcBinCoderCABAC[iDepth];
271  }
272
273  delete [] m_pppcRDSbacCoder;
274  delete [] m_pppcBinCoderCABAC;
275
276  for ( UInt ui = 0; ui < m_iNumSubstreams; ui++ )
277  {
[313]278    for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
279    {
280      for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
281      {
[595]282        delete m_ppppcRDSbacCoders  [ui][iDepth][iCIIdx];
283        delete m_ppppcBinCodersCABAC[ui][iDepth][iCIIdx];
[313]284      }
285    }
[595]286
[313]287    for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
288    {
[595]289      delete [] m_ppppcRDSbacCoders  [ui][iDepth];
290      delete [] m_ppppcBinCodersCABAC[ui][iDepth];
[313]291    }
[595]292    delete[] m_ppppcRDSbacCoders  [ui];
293    delete[] m_ppppcBinCodersCABAC[ui];
[313]294  }
[595]295  delete[] m_ppppcRDSbacCoders;
296  delete[] m_ppppcBinCodersCABAC;
[313]297  delete[] m_pcSbacCoders;
298  delete[] m_pcBinCoderCABACs;
299  delete[] m_pcRDGoOnSbacCoders; 
300  delete[] m_pcRDGoOnBinCodersCABAC;
301  delete[] m_pcBitCounters;
302  delete[] m_pcRdCosts;
303 
[442]304#if SVC_EXTENSION
[313]305  for(Int i=0; i<MAX_NUM_REF; i++)
306  {
307    if(m_cIlpPic[i])
308    {
309      m_cIlpPic[i]->destroy();
310      delete m_cIlpPic[i];
311      m_cIlpPic[i] = NULL;
312    }
[442]313  }
314#else
315  // destroy ROM
316  destroyROM();
[313]317#endif
318  return;
319}
320
[442]321Void TEncTop::init(Bool isFieldCoding)
[313]322{
323  // initialize SPS
324  xInitSPS();
325 
326  /* set the VPS profile information */
327  *m_cVPS.getPTL() = *m_cSPS.getPTL();
[894]328#if VPS_VUI_BSP_HRD_PARAMS
329  m_cVPS.getTimingInfo()->setTimingInfoPresentFlag       ( true );
330#else
[313]331  m_cVPS.getTimingInfo()->setTimingInfoPresentFlag       ( false );
[894]332#endif
[313]333  // initialize PPS
334  m_cPPS.setSPS(&m_cSPS);
335  xInitPPS();
[442]336  xInitRPS(isFieldCoding);
[313]337
338  xInitPPSforTiles();
339
340  // initialize processing unit classes
341  m_cGOPEncoder.  init( this );
342  m_cSliceEncoder.init( this );
343  m_cCuEncoder.   init( this );
344 
345  // initialize transform & quantization class
346  m_pcCavlcCoder = getCavlcCoder();
347 
348  m_cTrQuant.init( 1 << m_uiQuadtreeTULog2MaxSize,
349                  m_useRDOQ, 
350                  m_useRDOQTS,
351                  true 
352                  ,m_useTransformSkipFast
353#if ADAPTIVE_QP_SELECTION                 
354                  , m_bUseAdaptQpSelect
355#endif
356                  );
357 
358  // initialize encoder search class
359  m_cSearch.init( this, &m_cTrQuant, m_iSearchRange, m_bipredSearchRange, m_iFastSearch, 0, &m_cEntropyCoder, &m_cRdCost, getRDSbacCoder(), getRDGoOnSbacCoder() );
360
361  m_iMaxRefPicNum = 0;
362#if SVC_EXTENSION
363  m_iSPSIdCnt ++;
364  m_iPPSIdCnt ++;
365  xInitILRP();
366#endif
367}
368
369// ====================================================================================================================
370// Public member functions
371// ====================================================================================================================
372
373Void TEncTop::deletePicBuffer()
374{
375  TComList<TComPic*>::iterator iterPic = m_cListPic.begin();
376  Int iSize = Int( m_cListPic.size() );
377 
378  for ( Int i = 0; i < iSize; i++ )
379  {
380    TComPic* pcPic = *(iterPic++);
381   
382    pcPic->destroy();
383    delete pcPic;
384    pcPic = NULL;
385  }
386}
387
388/**
389 - Application has picture buffer list with size of GOP + 1
390 - Picture buffer list acts like as ring buffer
391 - End of the list has the latest picture
392 .
393 \param   flush               cause encoder to encode a partial GOP
394 \param   pcPicYuvOrg         original YUV picture
395 \retval  rcListPicYuvRecOut  list of reconstruction YUV pictures
396 \retval  rcListBitstreamOut  list of output bitstreams
397 \retval  iNumEncoded         number of encoded pictures
398 */
399#if SVC_EXTENSION
400Void TEncTop::encode( TComPicYuv* pcPicYuvOrg, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int iPicIdInGOP )
401{
402  // compress GOP
403#if !RC_SHVC_HARMONIZATION
404  if ( m_RCEnableRateControl )
405  {
406    m_cRateCtrl.initRCGOP( m_iNumPicRcvd );
407  }
408#endif
409
410  // compress GOP
[442]411  m_cGOPEncoder.compressGOP(iPicIdInGOP, m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, false, false);
[313]412
413#if !RC_SHVC_HARMONIZATION
414  if ( m_RCEnableRateControl )
415  {
416    m_cRateCtrl.destroyRCGOP();
417  }
418#endif
419 
420  m_uiNumAllPicCoded ++;
421}
422
423Void TEncTop::encodePrep( TComPicYuv* pcPicYuvOrg )
424{
425  if (pcPicYuvOrg) 
426  {
427    // get original YUV
428    TComPic* pcPicCurr = NULL;
429    xGetNewPicBuffer( pcPicCurr );
430    pcPicYuvOrg->copyToPic( pcPicCurr->getPicYuvOrg() );
431
432    // compute image characteristics
433    if ( getUseAdaptiveQP() )
434    {
435      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcPicCurr ) );
436    }
437  }
438}
439#else
440Void TEncTop::encode(Bool flush, TComPicYuv* pcPicYuvOrg, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int& iNumEncoded )
441{
442  if (pcPicYuvOrg) {
443    // get original YUV
444    TComPic* pcPicCurr = NULL;
445    xGetNewPicBuffer( pcPicCurr );
446    pcPicYuvOrg->copyToPic( pcPicCurr->getPicYuvOrg() );
447
448    // compute image characteristics
449    if ( getUseAdaptiveQP() )
450    {
451      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcPicCurr ) );
452    }
453  }
454 
455  if (!m_iNumPicRcvd || (!flush && m_iPOCLast != 0 && m_iNumPicRcvd != m_iGOPSize && m_iGOPSize))
456  {
457    iNumEncoded = 0;
458    return;
459  }
460 
461  if ( m_RCEnableRateControl )
462  {
463    m_cRateCtrl.initRCGOP( m_iNumPicRcvd );
464  }
465
466  // compress GOP
467
[442]468  m_cGOPEncoder.compressGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, false, false);
469
[313]470  if ( m_RCEnableRateControl )
471  {
472    m_cRateCtrl.destroyRCGOP();
473  }
474 
475  iNumEncoded         = m_iNumPicRcvd;
476  m_iNumPicRcvd       = 0;
477  m_uiNumAllPicCoded += iNumEncoded;
478}
479#endif
480
[442]481/**------------------------------------------------
482 Separate interlaced frame into two fields
483 -------------------------------------------------**/
484void separateFields(Pel* org, Pel* dstField, UInt stride, UInt width, UInt height, Bool isTop)
485{
486  if (!isTop)
487  {
488    org += stride;
489  }
490  for (Int y = 0; y < height>>1; y++)
491  {
492    for (Int x = 0; x < width; x++)
493    {
494      dstField[x] = org[x];
495    }
496   
497    dstField += stride;
498    org += stride*2;
499  }
500 
501}
502
503#if SVC_EXTENSION
504Void TEncTop::encodePrep( TComPicYuv* pcPicYuvOrg, Bool isTff )
505{
506  if (pcPicYuvOrg)
507  {
508    /* -- TOP FIELD -- */
509    /* -- Top field initialization -- */
510   
511    TComPic *pcTopField;
512    xGetNewPicBuffer( pcTopField );
513    pcTopField->setReconMark (false);
514   
515    pcTopField->getSlice(0)->setPOC( m_iPOCLast );
516    pcTopField->getPicYuvRec()->setBorderExtension(false);
517    pcTopField->setTopField(isTff);
518   
519    Int nHeight = pcPicYuvOrg->getHeight();
520    Int nWidth = pcPicYuvOrg->getWidth();
521    Int nStride = pcPicYuvOrg->getStride();
522    Int nPadLuma = pcPicYuvOrg->getLumaMargin();
523    Int nPadChroma = pcPicYuvOrg->getChromaMargin();
524   
525    // Get pointers
526    Pel * PicBufY = pcPicYuvOrg->getBufY();
527    Pel * PicBufU = pcPicYuvOrg->getBufU();
528    Pel * PicBufV = pcPicYuvOrg->getBufV();
529   
530    Pel * pcTopFieldY =  pcTopField->getPicYuvOrg()->getLumaAddr();
531    Pel * pcTopFieldU =  pcTopField->getPicYuvOrg()->getCbAddr();
532    Pel * pcTopFieldV =  pcTopField->getPicYuvOrg()->getCrAddr();
[540]533
[442]534    // compute image characteristics
535    if ( getUseAdaptiveQP() )
536    {
537      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcTopField ) );
[540]538    }   
539
[442]540    /* -- Defield -- */
541   
542    Bool isTop = isTff;
543   
544    separateFields(PicBufY + nPadLuma + nStride*nPadLuma, pcTopFieldY, nStride, nWidth, nHeight, isTop);
545    separateFields(PicBufU + nPadChroma + (nStride >> 1)*nPadChroma, pcTopFieldU, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
546    separateFields(PicBufV + nPadChroma + (nStride >> 1)*nPadChroma, pcTopFieldV, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
547
548    /* -- BOTTOM FIELD -- */
549    /* -- Bottom field initialization -- */
550   
551    TComPic* pcBottomField;
552    xGetNewPicBuffer( pcBottomField );
553    pcBottomField->setReconMark (false);
554   
555    pcBottomField->getSlice(0)->setPOC( m_iPOCLast);
556    pcBottomField->getPicYuvRec()->setBorderExtension(false);
557    pcBottomField->setTopField(!isTff);
558   
559    nHeight = pcPicYuvOrg->getHeight();
560    nWidth = pcPicYuvOrg->getWidth();
561    nStride = pcPicYuvOrg->getStride();
562    nPadLuma = pcPicYuvOrg->getLumaMargin();
563    nPadChroma = pcPicYuvOrg->getChromaMargin();
564   
565    // Get pointers
566    PicBufY = pcPicYuvOrg->getBufY();
567    PicBufU = pcPicYuvOrg->getBufU();
568    PicBufV = pcPicYuvOrg->getBufV();
569   
570    Pel * pcBottomFieldY =  pcBottomField->getPicYuvOrg()->getLumaAddr();
571    Pel * pcBottomFieldU =  pcBottomField->getPicYuvOrg()->getCbAddr();
572    Pel * pcBottomFieldV =  pcBottomField->getPicYuvOrg()->getCrAddr();
[540]573
574    // compute image characteristics
[442]575    if ( getUseAdaptiveQP() )
576    {
577      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcBottomField ) );
[540]578    }       
579
[442]580    /* -- Defield -- */
581   
582    isTop = !isTff;
583   
584    separateFields(PicBufY + nPadLuma + nStride*nPadLuma, pcBottomFieldY, nStride, nWidth, nHeight, isTop);
585    separateFields(PicBufU + nPadChroma + (nStride >> 1)*nPadChroma, pcBottomFieldU, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
586    separateFields(PicBufV + nPadChroma + (nStride >> 1)*nPadChroma, pcBottomFieldV, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
587   
588  }
589}
590
591Void TEncTop::encode( TComPicYuv* pcPicYuvOrg, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int iPicIdInGOP, Bool isTff )
592{
593  // compress GOP
594  if (m_iPOCLast == 0) // compress field 0
595  {
596    m_cGOPEncoder.compressGOP(iPicIdInGOP, m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, true, isTff);
597  }
598
599  if (pcPicYuvOrg)
600  {
601    TComPicYuv* rpcPicYuvRec = new TComPicYuv;
602    if ( rcListPicYuvRecOut.size() == (UInt)m_iGOPSize )
603    {
604      rpcPicYuvRec = rcListPicYuvRecOut.popFront();
605    }
606    else
607    {
[494]608#if AUXILIARY_PICTURES
609      rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
610#else
[442]611      rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
[494]612#endif
[442]613    }
614    rcListPicYuvRecOut.pushBack( rpcPicYuvRec );
615  }
616 
617  // compress GOP
618  m_cGOPEncoder.compressGOP(iPicIdInGOP, m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, true, isTff);
619
620  m_uiNumAllPicCoded ++;
621}
622#else
623Void TEncTop::encode(Bool flush, TComPicYuv* pcPicYuvOrg, TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsOut, Int& iNumEncoded, Bool isTff)
624{
625  /* -- TOP FIELD -- */
626 
627  if (pcPicYuvOrg)
628  {
629   
630    /* -- Top field initialization -- */
631   
632    TComPic *pcTopField;
633    xGetNewPicBuffer( pcTopField );
634    pcTopField->setReconMark (false);
635   
636    pcTopField->getSlice(0)->setPOC( m_iPOCLast );
637    pcTopField->getPicYuvRec()->setBorderExtension(false);
638    pcTopField->setTopField(isTff);
639   
640    Int nHeight = pcPicYuvOrg->getHeight();
641    Int nWidth = pcPicYuvOrg->getWidth();
642    Int nStride = pcPicYuvOrg->getStride();
643    Int nPadLuma = pcPicYuvOrg->getLumaMargin();
644    Int nPadChroma = pcPicYuvOrg->getChromaMargin();
645   
646    // Get pointers
647    Pel * PicBufY = pcPicYuvOrg->getBufY();
648    Pel * PicBufU = pcPicYuvOrg->getBufU();
649    Pel * PicBufV = pcPicYuvOrg->getBufV();
650   
651    Pel * pcTopFieldY =  pcTopField->getPicYuvOrg()->getLumaAddr();
652    Pel * pcTopFieldU =  pcTopField->getPicYuvOrg()->getCbAddr();
653    Pel * pcTopFieldV =  pcTopField->getPicYuvOrg()->getCrAddr();
654   
655    // compute image characteristics
656    if ( getUseAdaptiveQP() )
657    {
658      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcTopField ) );
659    }
660   
661    /* -- Defield -- */
662   
663    Bool isTop = isTff;
664   
665    separateFields(PicBufY + nPadLuma + nStride*nPadLuma, pcTopFieldY, nStride, nWidth, nHeight, isTop);
666    separateFields(PicBufU + nPadChroma + (nStride >> 1)*nPadChroma, pcTopFieldU, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
667    separateFields(PicBufV + nPadChroma + (nStride >> 1)*nPadChroma, pcTopFieldV, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
668   
669  }
670 
671  if (m_iPOCLast == 0) // compress field 0
672  {
673    m_cGOPEncoder.compressGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, true, isTff);
674  }
675 
676  /* -- BOTTOM FIELD -- */
677 
678  if (pcPicYuvOrg)
679  {
680   
681    /* -- Bottom field initialization -- */
682   
683    TComPic* pcBottomField;
684    xGetNewPicBuffer( pcBottomField );
685    pcBottomField->setReconMark (false);
686   
[540]687    TComPicYuv* rpcPicYuvRec;
[442]688    if ( rcListPicYuvRecOut.size() == (UInt)m_iGOPSize )
689    {
690      rpcPicYuvRec = rcListPicYuvRecOut.popFront();
691    }
692    else
693    {
[540]694      rpcPicYuvRec = new TComPicYuv;
[442]695      rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
696    }
697    rcListPicYuvRecOut.pushBack( rpcPicYuvRec );
698   
699    pcBottomField->getSlice(0)->setPOC( m_iPOCLast);
700    pcBottomField->getPicYuvRec()->setBorderExtension(false);
701    pcBottomField->setTopField(!isTff);
702   
703    int nHeight = pcPicYuvOrg->getHeight();
704    int nWidth = pcPicYuvOrg->getWidth();
705    int nStride = pcPicYuvOrg->getStride();
706    int nPadLuma = pcPicYuvOrg->getLumaMargin();
707    int nPadChroma = pcPicYuvOrg->getChromaMargin();
708   
709    // Get pointers
710    Pel * PicBufY = pcPicYuvOrg->getBufY();
711    Pel * PicBufU = pcPicYuvOrg->getBufU();
712    Pel * PicBufV = pcPicYuvOrg->getBufV();
713   
714    Pel * pcBottomFieldY =  pcBottomField->getPicYuvOrg()->getLumaAddr();
715    Pel * pcBottomFieldU =  pcBottomField->getPicYuvOrg()->getCbAddr();
716    Pel * pcBottomFieldV =  pcBottomField->getPicYuvOrg()->getCrAddr();
717   
718    // Compute image characteristics
719    if ( getUseAdaptiveQP() )
720    {
721      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcBottomField ) );
722    }
723   
724    /* -- Defield -- */
725   
726    Bool isTop = !isTff;
727   
728    separateFields(PicBufY + nPadLuma + nStride*nPadLuma, pcBottomFieldY, nStride, nWidth, nHeight, isTop);
729    separateFields(PicBufU + nPadChroma + (nStride >> 1)*nPadChroma, pcBottomFieldU, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
730    separateFields(PicBufV + nPadChroma + (nStride >> 1)*nPadChroma, pcBottomFieldV, nStride >> 1, nWidth >> 1, nHeight >> 1, isTop);
731   
732  }
733 
734  if ( ( !(m_iNumPicRcvd) || (!flush && m_iPOCLast != 1 && m_iNumPicRcvd != m_iGOPSize && m_iGOPSize)) )
735  {
736    iNumEncoded = 0;
737    return;
738  }
739 
740  // compress GOP
741  m_cGOPEncoder.compressGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, true, isTff);
742 
743  iNumEncoded = m_iNumPicRcvd;
744  m_iNumPicRcvd = 0;
745  m_uiNumAllPicCoded += iNumEncoded;
746}
747#endif
748
[313]749// ====================================================================================================================
750// Protected member functions
751// ====================================================================================================================
752
753/**
754 - Application has picture buffer list with size of GOP + 1
755 - Picture buffer list acts like as ring buffer
756 - End of the list has the latest picture
757 .
758 \retval rpcPic obtained picture buffer
759 */
760Void TEncTop::xGetNewPicBuffer ( TComPic*& rpcPic )
761{
762  TComSlice::sortPicList(m_cListPic);
763 
764  if (m_cListPic.size() >= (UInt)(m_iGOPSize + getMaxDecPicBuffering(MAX_TLAYER-1) + 2) )
765  {
766    TComList<TComPic*>::iterator iterPic  = m_cListPic.begin();
767    Int iSize = Int( m_cListPic.size() );
768    for ( Int i = 0; i < iSize; i++ )
769    {
770      rpcPic = *(iterPic++);
771      if(rpcPic->getSlice(0)->isReferenced() == false)
772      {
773        break;
774      }
775    }
776  }
777  else
778  {
779    if ( getUseAdaptiveQP() )
780    {
781      TEncPic* pcEPic = new TEncPic;
782
783#if SVC_EXTENSION //Temporal solution, should be modified
784      if(m_layerId > 0)
785      {
786        for(UInt i = 0; i < m_cVPS.getNumDirectRefLayers( m_layerId ); i++ )
787        {
[849]788#if MOVE_SCALED_OFFSET_TO_PPS
[540]789#if O0098_SCALED_REF_LAYER_ID
[849]790          const Window scalEL = getPPS()->getScaledRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
791#else
792          const Window scalEL = getPPS()->getScaledRefLayerWindow(i);
793#endif
794#else
795#if O0098_SCALED_REF_LAYER_ID
[540]796          const Window scalEL = getSPS()->getScaledRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
797#else
[313]798          const Window scalEL = getSPS()->getScaledRefLayerWindow(i);
[540]799#endif
[849]800#endif
801#if REF_REGION_OFFSET
802          const Window altRL  = getPPS()->getRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
[873]803#if RESAMPLING_FIX
804          Bool equalOffsets = scalEL.hasEqualOffset(altRL);
805#if R0209_GENERIC_PHASE
806          Bool zeroPhase = getPPS()->hasZeroResamplingPhase(m_cVPS.getRefLayerId(m_layerId, i));
807#endif
808#else
[849]809          Bool zeroOffsets = ( scalEL.getWindowLeftOffset() == 0 && scalEL.getWindowRightOffset() == 0 && scalEL.getWindowTopOffset() == 0 && scalEL.getWindowBottomOffset() == 0
810                               && altRL.getWindowLeftOffset() == 0 && altRL.getWindowRightOffset() == 0 && altRL.getWindowTopOffset() == 0 && altRL.getWindowBottomOffset() == 0);
[873]811#endif
[849]812#else
[313]813          Bool zeroOffsets = ( scalEL.getWindowLeftOffset() == 0 && scalEL.getWindowRightOffset() == 0 && scalEL.getWindowTopOffset() == 0 && scalEL.getWindowBottomOffset() == 0 );
[849]814#endif
[313]815
816#if VPS_EXTN_DIRECT_REF_LAYERS
817          TEncTop *pcEncTopBase = (TEncTop *)getRefLayerEnc( i );
818#else
819          TEncTop *pcEncTopBase = (TEncTop *)getLayerEnc( m_layerId-1 );
820#endif
[644]821#if O0194_DIFFERENT_BITDEPTH_EL_BL
822          UInt refLayerId = m_cVPS.getRefLayerId(m_layerId, i);
823          Bool sameBitDepths = ( g_bitDepthYLayer[m_layerId] == g_bitDepthYLayer[refLayerId] ) && ( g_bitDepthCLayer[m_layerId] == g_bitDepthCLayer[refLayerId] );
824
[901]825#if REF_IDX_MFM
826          if( m_iSourceWidth == pcEncTopBase->getSourceWidth() && m_iSourceHeight == pcEncTopBase->getSourceHeight() && equalOffsets && zeroPhase )
827          {
828            pcEPic->setEqualPictureSizeAndOffsetFlag( i, true );
829          }
830
831          if( !pcEPic->equalPictureSizeAndOffsetFlag(i) || !sameBitDepths
832#else
[873]833          if( m_iSourceWidth != pcEncTopBase->getSourceWidth() || m_iSourceHeight != pcEncTopBase->getSourceHeight() || !sameBitDepths
834#if REF_REGION_OFFSET && RESAMPLING_FIX
835            || !equalOffsets
836#if R0209_GENERIC_PHASE
837            || !zeroPhase
838#endif
839#else
840            || !zeroOffsets
841#endif
[901]842#endif
[713]843#if Q0048_CGS_3D_ASYMLUT
844            || m_cPPS.getCGSFlag() > 0
845#endif
846#if LAYER_CTB
847            || pcEncTopBase->getSPS()->getMaxCUWidth() != m_cSPS.getMaxCUWidth() || pcEncTopBase->getSPS()->getMaxCUHeight() != m_cSPS.getMaxCUHeight() || pcEncTopBase->getSPS()->getMaxCUDepth() != m_cSPS.getMaxCUDepth()
848#endif
849            )
[644]850#else
[873]851          if(m_iSourceWidth != pcEncTopBase->getSourceWidth() || m_iSourceHeight != pcEncTopBase->getSourceHeight()
852#if REF_REGION_OFFSET && RESAMPLING_FIX
853            || !equalOffsets
854#if R0209_GENERIC_PHASE
855            || !zeroPhase
[644]856#endif
[873]857#else
858            || !zeroOffsets
859#endif
860          )
861#endif
[313]862          {
863            pcEPic->setSpatialEnhLayerFlag( i, true );
864
865            //only for scalable extension
[815]866            assert( m_cVPS.getScalabilityMask( SCALABILITY_ID ) == true );
[313]867          }
868        }
869      }
870#endif
871
[815]872#if SVC_EXTENSION
[494]873#if AUXILIARY_PICTURES
874      pcEPic->create( m_iSourceWidth, m_iSourceHeight, m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, m_cPPS.getMaxCuDQPDepth()+1 ,
875                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics, &m_cSPS);
876#else
[313]877      pcEPic->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, m_cPPS.getMaxCuDQPDepth()+1 ,
878                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics, &m_cSPS);
[815]879#endif
880#else  //SVC_EXTENSION
[313]881      pcEPic->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, m_cPPS.getMaxCuDQPDepth()+1 ,
882                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics);
[815]883#endif //SVC_EXTENSION
[313]884      rpcPic = pcEPic;
885    }
886    else
887    {
888      rpcPic = new TComPic;
889
890#if SVC_EXTENSION //Temporal solution, should be modified
891      if(m_layerId > 0)
892      {
893        for(UInt i = 0; i < m_cVPS.getNumDirectRefLayers( m_layerId ); i++ )
894        {
[849]895#if MOVE_SCALED_OFFSET_TO_PPS
[540]896#if O0098_SCALED_REF_LAYER_ID
[849]897          const Window scalEL = getPPS()->getScaledRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
898#else
899          const Window scalEL = getPPS()->getScaledRefLayerWindow(i);
900#endif
901#else
902#if O0098_SCALED_REF_LAYER_ID
[540]903          const Window scalEL = getSPS()->getScaledRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
904#else
[313]905          const Window scalEL = getSPS()->getScaledRefLayerWindow(i);
[540]906#endif
[849]907#endif
908#if REF_REGION_OFFSET
909          const Window altRL  = getPPS()->getRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
[873]910#if RESAMPLING_FIX
911          Bool equalOffsets = scalEL.hasEqualOffset(altRL);
912#if R0209_GENERIC_PHASE
913          Bool zeroPhase = getPPS()->hasZeroResamplingPhase(m_cVPS.getRefLayerId(m_layerId, i));
914#endif
915#else
[849]916          Bool zeroOffsets = ( scalEL.getWindowLeftOffset() == 0 && scalEL.getWindowRightOffset() == 0 && scalEL.getWindowTopOffset() == 0 && scalEL.getWindowBottomOffset() == 0
917                               && altRL.getWindowLeftOffset() == 0 && altRL.getWindowRightOffset() == 0 && altRL.getWindowTopOffset() == 0 && altRL.getWindowBottomOffset() == 0);
[873]918#endif
[849]919#else
[313]920          Bool zeroOffsets = ( scalEL.getWindowLeftOffset() == 0 && scalEL.getWindowRightOffset() == 0 && scalEL.getWindowTopOffset() == 0 && scalEL.getWindowBottomOffset() == 0 );
[849]921#endif
[313]922
923#if VPS_EXTN_DIRECT_REF_LAYERS
924          TEncTop *pcEncTopBase = (TEncTop *)getRefLayerEnc( i );
925#else
926          TEncTop *pcEncTopBase = (TEncTop *)getLayerEnc( m_layerId-1 );
927#endif
[644]928#if O0194_DIFFERENT_BITDEPTH_EL_BL
929          UInt refLayerId = m_cVPS.getRefLayerId(m_layerId, i);
930          Bool sameBitDepths = ( g_bitDepthYLayer[m_layerId] == g_bitDepthYLayer[refLayerId] ) && ( g_bitDepthCLayer[m_layerId] == g_bitDepthCLayer[refLayerId] );
931
[873]932          if( m_iSourceWidth != pcEncTopBase->getSourceWidth() || m_iSourceHeight != pcEncTopBase->getSourceHeight() || !sameBitDepths
933#if REF_REGION_OFFSET && RESAMPLING_FIX
934            || !equalOffsets
935#if R0209_GENERIC_PHASE
936            || !zeroPhase
937#endif
938#else
939            || !zeroOffsets
940#endif
[713]941#if Q0048_CGS_3D_ASYMLUT
942            || m_cPPS.getCGSFlag() > 0
943#endif
944#if LAYER_CTB
945            || pcEncTopBase->getSPS()->getMaxCUWidth() != m_cSPS.getMaxCUWidth() || pcEncTopBase->getSPS()->getMaxCUHeight() != m_cSPS.getMaxCUHeight() || pcEncTopBase->getSPS()->getMaxCUDepth() != m_cSPS.getMaxCUDepth()
946#endif
947)
[644]948#else
[873]949          if(m_iSourceWidth != pcEncTopBase->getSourceWidth() || m_iSourceHeight != pcEncTopBase->getSourceHeight()
950#if REF_REGION_OFFSET && RESAMPLING_FIX
951            || !equalOffsets
952#if R0209_GENERIC_PHASE
953            || !zeroPhase
[644]954#endif
[873]955#else
956            || !zeroOffsets
957#endif
958          )
959#endif
[313]960          {
961            rpcPic->setSpatialEnhLayerFlag( i, true );
962
963            //only for scalable extension
[815]964            assert( m_cVPS.getScalabilityMask( SCALABILITY_ID ) == true );
[313]965          }
966        }
967      }
968#endif
969
[815]970#if SVC_EXTENSION
[494]971#if AUXILIARY_PICTURES
972      rpcPic->create( m_iSourceWidth, m_iSourceHeight, m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
973                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics, &m_cSPS);
974#else
[313]975      rpcPic->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
976                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics, &m_cSPS);
[815]977#endif
978#else  //SVC_EXTENSION
[313]979      rpcPic->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
980                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics);
[815]981#endif //SVC_EXTENSION
[313]982    }
983    m_cListPic.pushBack( rpcPic );
984  }
985  rpcPic->setReconMark (false);
986 
987  m_iPOCLast++;
988  m_iNumPicRcvd++;
989 
990  rpcPic->getSlice(0)->setPOC( m_iPOCLast );
991  // mark it should be extended
992  rpcPic->getPicYuvRec()->setBorderExtension(false);
993}
994
995Void TEncTop::xInitSPS()
996{
997#if SVC_EXTENSION
[815]998  m_cSPS.setExtensionFlag( m_layerId > 0 ? true : false );
[898]999#if R0042_PROFILE_INDICATION
[988]1000  m_cSPS.setNumDirectRefLayers(m_numDirectRefLayers);
[898]1001#endif
[815]1002#if Q0078_ADD_LAYER_SETS
[830]1003  if( !m_numDirectRefLayers && m_numAddLayerSets )
[815]1004  {
1005    m_cSPS.setLayerId(0); // layer ID 0 for independent layers
1006  }
1007  else
1008  {
1009    m_cSPS.setLayerId(m_layerId);
1010  }
1011#else
[313]1012  m_cSPS.setLayerId(m_layerId);
[815]1013#endif
[849]1014#if !MOVE_SCALED_OFFSET_TO_PPS
[313]1015  m_cSPS.setNumScaledRefLayerOffsets(m_numScaledRefLayerOffsets);
1016  for(Int i = 0; i < m_cSPS.getNumScaledRefLayerOffsets(); i++)
1017  {
[540]1018#if O0098_SCALED_REF_LAYER_ID
1019    m_cSPS.setScaledRefLayerId(i, m_scaledRefLayerId[i]);
1020#endif
[313]1021    m_cSPS.getScaledRefLayerWindow(i) = m_scaledRefLayerWindow[i];
[644]1022#if P0312_VERT_PHASE_ADJ
1023    m_cSPS.setVertPhasePositionEnableFlag( m_scaledRefLayerId[i], m_scaledRefLayerWindow[i].getVertPhasePositionEnableFlag() );
1024#endif
[313]1025  }
[849]1026#endif
[494]1027#endif //SVC_EXTENSION
[313]1028  ProfileTierLevel& profileTierLevel = *m_cSPS.getPTL()->getGeneralPTL();
1029  profileTierLevel.setLevelIdc(m_level);
1030  profileTierLevel.setTierFlag(m_levelTier);
1031  profileTierLevel.setProfileIdc(m_profile);
1032  profileTierLevel.setProfileCompatibilityFlag(m_profile, 1);
1033  profileTierLevel.setProgressiveSourceFlag(m_progressiveSourceFlag);
1034  profileTierLevel.setInterlacedSourceFlag(m_interlacedSourceFlag);
1035  profileTierLevel.setNonPackedConstraintFlag(m_nonPackedConstraintFlag);
1036  profileTierLevel.setFrameOnlyConstraintFlag(m_frameOnlyConstraintFlag);
1037 
1038  if (m_profile == Profile::MAIN10 && g_bitDepthY == 8 && g_bitDepthC == 8)
1039  {
1040    /* The above constraint is equal to Profile::MAIN */
1041    profileTierLevel.setProfileCompatibilityFlag(Profile::MAIN, 1);
1042  }
1043  if (m_profile == Profile::MAIN)
1044  {
1045    /* A Profile::MAIN10 decoder can always decode Profile::MAIN */
1046    profileTierLevel.setProfileCompatibilityFlag(Profile::MAIN10, 1);
1047  }
1048  /* XXX: should Main be marked as compatible with still picture? */
1049  /* XXX: may be a good idea to refactor the above into a function
1050   * that chooses the actual compatibility based upon options */
1051
1052  m_cSPS.setPicWidthInLumaSamples         ( m_iSourceWidth      );
1053  m_cSPS.setPicHeightInLumaSamples        ( m_iSourceHeight     );
1054  m_cSPS.setConformanceWindow             ( m_conformanceWindow );
1055  m_cSPS.setMaxCUWidth    ( g_uiMaxCUWidth      );
1056  m_cSPS.setMaxCUHeight   ( g_uiMaxCUHeight     );
1057  m_cSPS.setMaxCUDepth    ( g_uiMaxCUDepth      );
[494]1058#if AUXILIARY_PICTURES
1059  m_cSPS.setChromaFormatIdc( m_chromaFormatIDC);
1060#endif
[313]1061
1062  Int minCUSize = m_cSPS.getMaxCUWidth() >> ( m_cSPS.getMaxCUDepth()-g_uiAddCUDepth );
1063  Int log2MinCUSize = 0;
1064  while(minCUSize > 1)
1065  {
1066    minCUSize >>= 1;
1067    log2MinCUSize++;
1068  }
1069
1070  m_cSPS.setLog2MinCodingBlockSize(log2MinCUSize);
1071  m_cSPS.setLog2DiffMaxMinCodingBlockSize(m_cSPS.getMaxCUDepth()-g_uiAddCUDepth);
1072#if SVC_EXTENSION
1073  m_cSPS.setSPSId         ( m_iSPSIdCnt       );
1074#endif
1075 
1076  m_cSPS.setPCMLog2MinSize (m_uiPCMLog2MinSize);
1077  m_cSPS.setUsePCM        ( m_usePCM           );
1078  m_cSPS.setPCMLog2MaxSize( m_pcmLog2MaxSize  );
1079
1080  m_cSPS.setQuadtreeTULog2MaxSize( m_uiQuadtreeTULog2MaxSize );
1081  m_cSPS.setQuadtreeTULog2MinSize( m_uiQuadtreeTULog2MinSize );
1082  m_cSPS.setQuadtreeTUMaxDepthInter( m_uiQuadtreeTUMaxDepthInter    );
1083  m_cSPS.setQuadtreeTUMaxDepthIntra( m_uiQuadtreeTUMaxDepthIntra    );
1084 
1085  m_cSPS.setTMVPFlagsPresent(false);
1086
1087  m_cSPS.setMaxTrSize   ( 1 << m_uiQuadtreeTULog2MaxSize );
1088 
1089  Int i;
1090 
1091  for (i = 0; i < g_uiMaxCUDepth-g_uiAddCUDepth; i++ )
1092  {
1093    m_cSPS.setAMPAcc( i, m_useAMP );
1094    //m_cSPS.setAMPAcc( i, 1 );
1095  }
1096
1097  m_cSPS.setUseAMP ( m_useAMP );
1098
1099  for (i = g_uiMaxCUDepth-g_uiAddCUDepth; i < g_uiMaxCUDepth; i++ )
1100  {
1101    m_cSPS.setAMPAcc(i, 0);
1102  }
1103
[442]1104#if REPN_FORMAT_IN_VPS
[999]1105  UInt layerIdx = m_cVPS.getLayerIdInVps( m_layerId );
[442]1106
[999]1107  m_cSPS.setBitDepthY( m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( layerIdx ) )->getBitDepthVpsLuma() );
1108  m_cSPS.setBitDepthC( m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( layerIdx ) )->getBitDepthVpsChroma()  );
1109
1110  m_cSPS.setQpBDOffsetY ( 6*(m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( layerIdx ) )->getBitDepthVpsLuma() - 8) );
1111  m_cSPS.setQpBDOffsetC ( 6*(m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( layerIdx ) )->getBitDepthVpsChroma() - 8) );
[442]1112#else
[313]1113  m_cSPS.setBitDepthY( g_bitDepthY );
1114  m_cSPS.setBitDepthC( g_bitDepthC );
1115
1116  m_cSPS.setQpBDOffsetY ( 6*(g_bitDepthY - 8) );
1117  m_cSPS.setQpBDOffsetC ( 6*(g_bitDepthC - 8) );
[442]1118#endif
[313]1119
1120  m_cSPS.setUseSAO( m_bUseSAO );
1121
1122  m_cSPS.setMaxTLayers( m_maxTempLayer );
1123  m_cSPS.setTemporalIdNestingFlag( ( m_maxTempLayer == 1 ) ? true : false );
[595]1124  for ( i = 0; i < min(m_cSPS.getMaxTLayers(),(UInt) MAX_TLAYER); i++ )
[313]1125  {
[930]1126#if SVC_EXTENSION
1127    assert(i < MAX_TLAYER);
1128#endif
[313]1129    m_cSPS.setMaxDecPicBuffering(m_maxDecPicBuffering[i], i);
1130    m_cSPS.setNumReorderPics(m_numReorderPics[i], i);
1131  }
1132  m_cSPS.setPCMBitDepthLuma (g_uiPCMBitDepthLuma);
1133  m_cSPS.setPCMBitDepthChroma (g_uiPCMBitDepthChroma);
1134  m_cSPS.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag );
1135
1136  m_cSPS.setScalingListFlag ( (m_useScalingListId == 0) ? 0 : 1 );
1137
1138  m_cSPS.setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
1139
1140  m_cSPS.setVuiParametersPresentFlag(getVuiParametersPresentFlag());
1141  if (m_cSPS.getVuiParametersPresentFlag())
1142  {
1143    TComVUI* pcVUI = m_cSPS.getVuiParameters();
[823]1144    pcVUI->setAspectRatioInfoPresentFlag(getAspectRatioInfoPresentFlag());
[313]1145    pcVUI->setAspectRatioIdc(getAspectRatioIdc());
1146    pcVUI->setSarWidth(getSarWidth());
1147    pcVUI->setSarHeight(getSarHeight());
1148    pcVUI->setOverscanInfoPresentFlag(getOverscanInfoPresentFlag());
1149    pcVUI->setOverscanAppropriateFlag(getOverscanAppropriateFlag());
1150    pcVUI->setVideoSignalTypePresentFlag(getVideoSignalTypePresentFlag());
1151    pcVUI->setVideoFormat(getVideoFormat());
1152    pcVUI->setVideoFullRangeFlag(getVideoFullRangeFlag());
1153    pcVUI->setColourDescriptionPresentFlag(getColourDescriptionPresentFlag());
1154    pcVUI->setColourPrimaries(getColourPrimaries());
1155    pcVUI->setTransferCharacteristics(getTransferCharacteristics());
1156    pcVUI->setMatrixCoefficients(getMatrixCoefficients());
1157    pcVUI->setChromaLocInfoPresentFlag(getChromaLocInfoPresentFlag());
1158    pcVUI->setChromaSampleLocTypeTopField(getChromaSampleLocTypeTopField());
1159    pcVUI->setChromaSampleLocTypeBottomField(getChromaSampleLocTypeBottomField());
1160    pcVUI->setNeutralChromaIndicationFlag(getNeutralChromaIndicationFlag());
1161    pcVUI->setDefaultDisplayWindow(getDefaultDisplayWindow());
1162    pcVUI->setFrameFieldInfoPresentFlag(getFrameFieldInfoPresentFlag());
1163    pcVUI->setFieldSeqFlag(false);
1164    pcVUI->setHrdParametersPresentFlag(false);
1165    pcVUI->getTimingInfo()->setPocProportionalToTimingFlag(getPocProportionalToTimingFlag());
1166    pcVUI->getTimingInfo()->setNumTicksPocDiffOneMinus1   (getNumTicksPocDiffOneMinus1()   );
1167    pcVUI->setBitstreamRestrictionFlag(getBitstreamRestrictionFlag());
1168    pcVUI->setTilesFixedStructureFlag(getTilesFixedStructureFlag());
1169    pcVUI->setMotionVectorsOverPicBoundariesFlag(getMotionVectorsOverPicBoundariesFlag());
1170    pcVUI->setMinSpatialSegmentationIdc(getMinSpatialSegmentationIdc());
1171    pcVUI->setMaxBytesPerPicDenom(getMaxBytesPerPicDenom());
1172    pcVUI->setMaxBitsPerMinCuDenom(getMaxBitsPerMinCuDenom());
1173    pcVUI->setLog2MaxMvLengthHorizontal(getLog2MaxMvLengthHorizontal());
1174    pcVUI->setLog2MaxMvLengthVertical(getLog2MaxMvLengthVertical());
1175  }
1176}
1177
1178Void TEncTop::xInitPPS()
1179{
1180  m_cPPS.setConstrainedIntraPred( m_bUseConstrainedIntraPred );
1181  Bool bUseDQP = (getMaxCuDQPDepth() > 0)? true : false;
1182
[595]1183  if((getMaxDeltaQP() != 0 )|| getUseAdaptiveQP())
[540]1184  {
[595]1185    bUseDQP = true;
[540]1186  }
[313]1187
1188  if(bUseDQP)
1189  {
1190    m_cPPS.setUseDQP(true);
1191    m_cPPS.setMaxCuDQPDepth( m_iMaxCuDQPDepth );
1192    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
1193  }
1194  else
1195  {
1196    m_cPPS.setUseDQP(false);
1197    m_cPPS.setMaxCuDQPDepth( 0 );
1198    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
1199  }
1200
1201  if ( m_RCEnableRateControl )
1202  {
1203    m_cPPS.setUseDQP(true);
1204    m_cPPS.setMaxCuDQPDepth( 0 );
1205    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
1206  } 
1207
1208  m_cPPS.setChromaCbQpOffset( m_chromaCbQpOffset );
1209  m_cPPS.setChromaCrQpOffset( m_chromaCrQpOffset );
1210
1211  m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams);
1212  m_cPPS.setEntropyCodingSyncEnabledFlag( m_iWaveFrontSynchro > 0 );
1213  m_cPPS.setTilesEnabledFlag( (m_iNumColumnsMinus1 > 0 || m_iNumRowsMinus1 > 0) );
1214  m_cPPS.setUseWP( m_useWeightedPred );
1215  m_cPPS.setWPBiPred( m_useWeightedBiPred );
1216  m_cPPS.setOutputFlagPresentFlag( false );
1217  m_cPPS.setSignHideFlag(getSignHideFlag());
1218  if ( getDeblockingFilterMetric() )
1219  {
1220    m_cPPS.setDeblockingFilterControlPresentFlag (true);
1221    m_cPPS.setDeblockingFilterOverrideEnabledFlag(true);
1222    m_cPPS.setPicDisableDeblockingFilterFlag(false);
1223    m_cPPS.setDeblockingFilterBetaOffsetDiv2(0);
1224    m_cPPS.setDeblockingFilterTcOffsetDiv2(0);
1225  } 
1226  else
1227  {
1228  m_cPPS.setDeblockingFilterControlPresentFlag (m_DeblockingFilterControlPresent );
1229  }
1230  m_cPPS.setLog2ParallelMergeLevelMinus2   (m_log2ParallelMergeLevelMinus2 );
1231  m_cPPS.setCabacInitPresentFlag(CABAC_INIT_PRESENT_FLAG);
1232  m_cPPS.setLoopFilterAcrossSlicesEnabledFlag( m_bLFCrossSliceBoundaryFlag );
1233  Int histogram[MAX_NUM_REF + 1];
1234  for( Int i = 0; i <= MAX_NUM_REF; i++ )
1235  {
1236    histogram[i]=0;
1237  }
1238  for( Int i = 0; i < getGOPSize(); i++ )
1239  {
1240    assert(getGOPEntry(i).m_numRefPicsActive >= 0 && getGOPEntry(i).m_numRefPicsActive <= MAX_NUM_REF);
1241    histogram[getGOPEntry(i).m_numRefPicsActive]++;
1242  }
1243  Int maxHist=-1;
1244  Int bestPos=0;
1245  for( Int i = 0; i <= MAX_NUM_REF; i++ )
1246  {
1247    if(histogram[i]>maxHist)
1248    {
1249      maxHist=histogram[i];
1250      bestPos=i;
1251    }
1252  }
1253  assert(bestPos <= 15);
1254  m_cPPS.setNumRefIdxL0DefaultActive(bestPos);
1255  m_cPPS.setNumRefIdxL1DefaultActive(bestPos);
1256  m_cPPS.setTransquantBypassEnableFlag(getTransquantBypassEnableFlag());
1257  m_cPPS.setUseTransformSkip( m_useTransformSkip );
1258  if (m_sliceSegmentMode)
1259  {
1260    m_cPPS.setDependentSliceSegmentsEnabledFlag( true );
1261  }
1262  if( m_cPPS.getDependentSliceSegmentsEnabledFlag() )
1263  {
1264    Int NumCtx = m_cPPS.getEntropyCodingSyncEnabledFlag()?2:1;
1265    m_cSliceEncoder.initCtxMem( NumCtx );
1266    for ( UInt st = 0; st < NumCtx; st++ )
1267    {
1268      TEncSbac* ctx = NULL;
1269      ctx = new TEncSbac;
1270      ctx->init( &m_cBinCoderCABAC );
1271      m_cSliceEncoder.setCtxMem( ctx, st );
1272    }
1273  }
[442]1274#if SVC_EXTENSION
[815]1275#if SCALINGLIST_INFERRING
1276  m_cPPS.setLayerId( m_layerId );
1277#endif
1278
1279#if Q0078_ADD_LAYER_SETS
[830]1280  if( !m_numDirectRefLayers && m_numAddLayerSets )
[313]1281  {
[815]1282    m_cPPS.setLayerId(0); // layer ID 0 for independent layers
[313]1283  }
[815]1284#endif
1285
1286  if( m_layerId > 0 )
[313]1287  {
1288    m_cPPS.setListsModificationPresentFlag(true);
[815]1289    m_cPPS.setExtensionFlag(true);
[313]1290  }
[815]1291  else
1292  {
1293    m_cPPS.setListsModificationPresentFlag(false);
1294    m_cPPS.setExtensionFlag(false);
1295  }
[442]1296
[815]1297  m_cPPS.setPPSId( m_iPPSIdCnt );
1298  m_cPPS.setSPSId( m_iSPSIdCnt );
[442]1299#if POC_RESET_FLAG
1300  m_cPPS.setNumExtraSliceHeaderBits( 2 );
1301#endif
[540]1302#if O0149_CROSS_LAYER_BLA_FLAG
1303  if (m_crossLayerBLAFlag)
1304  {
1305    m_cPPS.setNumExtraSliceHeaderBits( 3 );
1306  }
1307#endif
[849]1308#if MOVE_SCALED_OFFSET_TO_PPS
1309  m_cPPS.setNumScaledRefLayerOffsets(m_numScaledRefLayerOffsets);
1310  for(Int i = 0; i < m_cPPS.getNumScaledRefLayerOffsets(); i++)
1311  {
1312#if O0098_SCALED_REF_LAYER_ID
1313    m_cPPS.setScaledRefLayerId(i, m_scaledRefLayerId[i]);
1314#endif
1315    m_cPPS.getScaledRefLayerWindow(i) = m_scaledRefLayerWindow[i];
1316#if REF_REGION_OFFSET
1317    m_cPPS.getRefLayerWindow(i) = m_refLayerWindow[i];
[934]1318    m_cPPS.setScaledRefLayerOffsetPresentFlag( i, m_scaledRefLayerOffsetPresentFlag[i] );
1319    m_cPPS.setRefRegionOffsetPresentFlag( i, m_refRegionOffsetPresentFlag[i] );
[849]1320#endif
1321#if R0209_GENERIC_PHASE
[934]1322    m_cPPS.setResamplePhaseSetPresentFlag( i, m_resamplePhaseSetPresentFlag[i] );
[849]1323    m_cPPS.setPhaseHorLuma( m_scaledRefLayerId[i], m_phaseHorLuma[i] );
1324    m_cPPS.setPhaseVerLuma( m_scaledRefLayerId[i], m_phaseVerLuma[i] );
1325    m_cPPS.setPhaseHorChroma( m_scaledRefLayerId[i], m_phaseHorChroma[i] );
1326    m_cPPS.setPhaseVerChroma( m_scaledRefLayerId[i], m_phaseVerChroma[i] );
1327#endif
1328#if P0312_VERT_PHASE_ADJ
1329    m_cPPS.setVertPhasePositionEnableFlag( m_scaledRefLayerId[i], m_scaledRefLayerWindow[i].getVertPhasePositionEnableFlag() );
1330#endif
1331  }
1332#endif
[713]1333#if Q0048_CGS_3D_ASYMLUT
1334  m_cPPS.setCGSFlag( m_nCGSFlag );
1335#endif
[815]1336#if POC_RESET_IDC_ENCODER
1337  m_cPPS.setPocResetInfoPresentFlag( true );
1338  m_cPPS.setExtensionFlag( true );
1339  m_cPPS.setSliceHeaderExtensionPresentFlag( true );
1340#endif
1341#endif //SVC_EXTENSION
[313]1342}
1343
1344//Function for initializing m_RPSList, a list of TComReferencePictureSet, based on the GOPEntry objects read from the config file.
[442]1345Void TEncTop::xInitRPS(Bool isFieldCoding)
[313]1346{
1347  TComReferencePictureSet*      rps;
1348 
[442]1349  m_cSPS.createRPSList(getGOPSize()+m_extraRPSs+1);
[313]1350  TComRPSList* rpsList = m_cSPS.getRPSList();
1351
1352  for( Int i = 0; i < getGOPSize()+m_extraRPSs; i++) 
1353  {
1354    GOPEntry ge = getGOPEntry(i);
1355    rps = rpsList->getReferencePictureSet(i);
1356    rps->setNumberOfPictures(ge.m_numRefPics);
1357    rps->setNumRefIdc(ge.m_numRefIdc);
1358    Int numNeg = 0;
1359    Int numPos = 0;
1360    for( Int j = 0; j < ge.m_numRefPics; j++)
1361    {
1362      rps->setDeltaPOC(j,ge.m_referencePics[j]);
1363      rps->setUsed(j,ge.m_usedByCurrPic[j]);
1364      if(ge.m_referencePics[j]>0)
1365      {
1366        numPos++;
1367      }
1368      else
1369      {
1370        numNeg++;
1371      }
1372    }
1373    rps->setNumberOfNegativePictures(numNeg);
1374    rps->setNumberOfPositivePictures(numPos);
1375
1376    // handle inter RPS intialization from the config file.
1377#if AUTO_INTER_RPS
1378    rps->setInterRPSPrediction(ge.m_interRPSPrediction > 0);  // not very clean, converting anything > 0 to true.
1379    rps->setDeltaRIdxMinus1(0);                               // index to the Reference RPS is always the previous one.
1380    TComReferencePictureSet*     RPSRef = rpsList->getReferencePictureSet(i-1);  // get the reference RPS
1381
1382    if (ge.m_interRPSPrediction == 2)  // Automatic generation of the inter RPS idc based on the RIdx provided.
1383    {
1384      Int deltaRPS = getGOPEntry(i-1).m_POC - ge.m_POC;  // the ref POC - current POC
1385      Int numRefDeltaPOC = RPSRef->getNumberOfPictures();
1386
1387      rps->setDeltaRPS(deltaRPS);           // set delta RPS
1388      rps->setNumRefIdc(numRefDeltaPOC+1);  // set the numRefIdc to the number of pictures in the reference RPS + 1.
1389      Int count=0;
1390      for (Int j = 0; j <= numRefDeltaPOC; j++ ) // cycle through pics in reference RPS.
1391      {
1392        Int RefDeltaPOC = (j<numRefDeltaPOC)? RPSRef->getDeltaPOC(j): 0;  // if it is the last decoded picture, set RefDeltaPOC = 0
1393        rps->setRefIdc(j, 0);
1394        for (Int k = 0; k < rps->getNumberOfPictures(); k++ )  // cycle through pics in current RPS.
1395        {
1396          if (rps->getDeltaPOC(k) == ( RefDeltaPOC + deltaRPS))  // if the current RPS has a same picture as the reference RPS.
1397          {
1398              rps->setRefIdc(j, (rps->getUsed(k)?1:2));
1399              count++;
1400              break;
1401          }
1402        }
1403      }
1404      if (count != rps->getNumberOfPictures())
1405      {
1406        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");
1407        rps->setInterRPSPrediction(0);
1408      }
1409    }
1410    else if (ge.m_interRPSPrediction == 1)  // inter RPS idc based on the RefIdc values provided in config file.
1411    {
1412      rps->setDeltaRPS(ge.m_deltaRPS);
1413      rps->setNumRefIdc(ge.m_numRefIdc);
1414      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1415      {
1416        rps->setRefIdc(j, ge.m_refIdc[j]);
1417      }
1418#if WRITE_BACK
1419      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1420      // computed from the RefIdc.  A warning is printed if they are not identical.
1421      numNeg = 0;
1422      numPos = 0;
1423      TComReferencePictureSet      RPSTemp;  // temporary variable
1424
1425      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1426      {
1427        if (ge.m_refIdc[j])
1428        {
1429          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1430          RPSTemp.setDeltaPOC((numNeg+numPos),deltaPOC);
1431          RPSTemp.setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1432          if (deltaPOC<0)
1433          {
1434            numNeg++;
1435          }
1436          else
1437          {
1438            numPos++;
1439          }
1440        }
1441      }
1442      if (numNeg != rps->getNumberOfNegativePictures())
1443      {
1444        printf("Warning: number of negative pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1445        rps->setNumberOfNegativePictures(numNeg);
[442]1446        rps->setNumberOfPictures(numNeg+numPos);
[313]1447      }
1448      if (numPos != rps->getNumberOfPositivePictures())
1449      {
1450        printf("Warning: number of positive pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1451        rps->setNumberOfPositivePictures(numPos);
[442]1452        rps->setNumberOfPictures(numNeg+numPos);
[313]1453      }
1454      RPSTemp.setNumberOfPictures(numNeg+numPos);
1455      RPSTemp.setNumberOfNegativePictures(numNeg);
1456      RPSTemp.sortDeltaPOC();     // sort the created delta POC before comparing
1457      // check if Delta POC and Used are the same
1458      // print warning if they are not.
1459      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1460      {
1461        if (RPSTemp.getDeltaPOC(j) != rps->getDeltaPOC(j))
1462        {
1463          printf("Warning: delta POC is different between intra RPS and inter RPS specified in the config file.\n");
1464          rps->setDeltaPOC(j,RPSTemp.getDeltaPOC(j));
1465        }
1466        if (RPSTemp.getUsed(j) != rps->getUsed(j))
1467        {
1468          printf("Warning: Used by Current in RPS is different between intra and inter RPS specified in the config file.\n");
1469          rps->setUsed(j,RPSTemp.getUsed(j));
1470        }
1471      }
1472#endif
1473    }
1474#else
1475    rps->setInterRPSPrediction(ge.m_interRPSPrediction);
1476    if (ge.m_interRPSPrediction)
1477    {
1478      rps->setDeltaRIdxMinus1(0);
1479      rps->setDeltaRPS(ge.m_deltaRPS);
1480      rps->setNumRefIdc(ge.m_numRefIdc);
1481      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1482      {
1483        rps->setRefIdc(j, ge.m_refIdc[j]);
1484      }
1485#if WRITE_BACK
1486      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1487      // computed from the RefIdc.  This is not necessary if both are identical. Currently there is no check to see if they are identical.
1488      numNeg = 0;
1489      numPos = 0;
1490      TComReferencePictureSet*     RPSRef = m_RPSList.getReferencePictureSet(i-1);
1491
1492      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1493      {
1494        if (ge.m_refIdc[j])
1495        {
1496          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1497          rps->setDeltaPOC((numNeg+numPos),deltaPOC);
1498          rps->setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1499          if (deltaPOC<0)
1500          {
1501            numNeg++;
1502          }
1503          else
1504          {
1505            numPos++;
1506          }
1507        }
1508      }
1509      rps->setNumberOfNegativePictures(numNeg);
1510      rps->setNumberOfPositivePictures(numPos);
1511      rps->sortDeltaPOC();
1512#endif
1513    }
1514#endif //INTER_RPS_AUTO
1515  }
[442]1516  //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.
1517  //The position = GOPSize + extraRPSs which is (a priori) unused is reserved for this field in the RPS.
1518  if (isFieldCoding) 
1519  {
1520    rps = rpsList->getReferencePictureSet(getGOPSize()+m_extraRPSs);
1521    rps->setNumberOfPictures(1);
1522    rps->setNumberOfNegativePictures(1);
1523    rps->setNumberOfPositivePictures(0);
1524    rps->setNumberOfLongtermPictures(0);
1525    rps->setDeltaPOC(0,-1);
1526    rps->setPOC(0,0);
1527    rps->setUsed(0,true);
1528    rps->setInterRPSPrediction(false);
1529    rps->setDeltaRIdxMinus1(0);
1530    rps->setDeltaRPS(0);
1531    rps->setNumRefIdc(0);
[313]1532}
[442]1533}
[313]1534
1535   // This is a function that
1536   // determines what Reference Picture Set to use
1537   // for a specific slice (with POC = POCCurr)
1538Void TEncTop::selectReferencePictureSet(TComSlice* slice, Int POCCurr, Int GOPid )
1539{
1540  slice->setRPSidx(GOPid);
1541  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1542  {   
1543    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1544    {
1545      Int POCIndex = POCCurr%m_uiIntraPeriod;
1546      if(POCIndex == 0)
1547      {
1548        POCIndex = m_uiIntraPeriod;
1549      }
1550      if(POCIndex == m_GOPList[extraNum].m_POC)
1551      {
1552        slice->setRPSidx(extraNum);
1553      }
1554    }
1555    else
1556    {
1557      if(POCCurr==m_GOPList[extraNum].m_POC)
1558      {
1559        slice->setRPSidx(extraNum);
1560      }
1561    }
1562  }
1563
[442]1564  if(POCCurr == 1 && slice->getPic()->isField())
1565  {
1566    slice->setRPSidx(m_iGOPSize+m_extraRPSs);
1567  }
1568
[313]1569  slice->setRPS(getSPS()->getRPSList()->getReferencePictureSet(slice->getRPSidx()));
1570  slice->getRPS()->setNumberOfPictures(slice->getRPS()->getNumberOfNegativePictures()+slice->getRPS()->getNumberOfPositivePictures());
1571}
1572
1573Int TEncTop::getReferencePictureSetIdxForSOP(TComSlice* slice, Int POCCurr, Int GOPid )
1574{
1575  int rpsIdx = GOPid;
1576
1577  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1578  {   
1579    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1580    {
1581      Int POCIndex = POCCurr%m_uiIntraPeriod;
1582      if(POCIndex == 0)
1583      {
1584        POCIndex = m_uiIntraPeriod;
1585      }
1586      if(POCIndex == m_GOPList[extraNum].m_POC)
1587      {
1588        rpsIdx = extraNum;
1589      }
1590    }
1591    else
1592    {
1593      if(POCCurr==m_GOPList[extraNum].m_POC)
1594      {
1595        rpsIdx = extraNum;
1596      }
1597    }
1598  }
1599
1600  return rpsIdx;
1601}
1602
1603Void  TEncTop::xInitPPSforTiles()
1604{
[823]1605  m_cPPS.setTileUniformSpacingFlag( m_tileUniformSpacingFlag );
1606  m_cPPS.setNumTileColumnsMinus1( m_iNumColumnsMinus1 );
1607  m_cPPS.setNumTileRowsMinus1( m_iNumRowsMinus1 );
1608  if( !m_tileUniformSpacingFlag )
[313]1609  {
[823]1610    m_cPPS.setTileColumnWidth( m_tileColumnWidth );
1611    m_cPPS.setTileRowHeight( m_tileRowHeight );
[313]1612  }
1613  m_cPPS.setLoopFilterAcrossTilesEnabledFlag( m_loopFilterAcrossTilesEnabledFlag );
1614
1615  // # substreams is "per tile" when tiles are independent.
[823]1616  if (m_iWaveFrontSynchro )
[313]1617  {
1618    m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams * (m_iNumColumnsMinus1+1));
1619  }
1620}
1621
1622Void  TEncCfg::xCheckGSParameters()
1623{
1624  Int   iWidthInCU = ( m_iSourceWidth%g_uiMaxCUWidth ) ? m_iSourceWidth/g_uiMaxCUWidth + 1 : m_iSourceWidth/g_uiMaxCUWidth;
1625  Int   iHeightInCU = ( m_iSourceHeight%g_uiMaxCUHeight ) ? m_iSourceHeight/g_uiMaxCUHeight + 1 : m_iSourceHeight/g_uiMaxCUHeight;
1626  UInt  uiCummulativeColumnWidth = 0;
1627  UInt  uiCummulativeRowHeight = 0;
1628
1629  //check the column relative parameters
1630  if( m_iNumColumnsMinus1 >= (1<<(LOG2_MAX_NUM_COLUMNS_MINUS1+1)) )
1631  {
1632    printf( "The number of columns is larger than the maximum allowed number of columns.\n" );
1633    exit( EXIT_FAILURE );
1634  }
1635
1636  if( m_iNumColumnsMinus1 >= iWidthInCU )
1637  {
1638    printf( "The current picture can not have so many columns.\n" );
1639    exit( EXIT_FAILURE );
1640  }
1641
[823]1642  if( m_iNumColumnsMinus1 && !m_tileUniformSpacingFlag )
[313]1643  {
1644    for(Int i=0; i<m_iNumColumnsMinus1; i++)
1645    {
[823]1646      uiCummulativeColumnWidth += m_tileColumnWidth[i];
[313]1647    }
1648
1649    if( uiCummulativeColumnWidth >= iWidthInCU )
1650    {
1651      printf( "The width of the column is too large.\n" );
1652      exit( EXIT_FAILURE );
1653    }
1654  }
1655
1656  //check the row relative parameters
1657  if( m_iNumRowsMinus1 >= (1<<(LOG2_MAX_NUM_ROWS_MINUS1+1)) )
1658  {
1659    printf( "The number of rows is larger than the maximum allowed number of rows.\n" );
1660    exit( EXIT_FAILURE );
1661  }
1662
1663  if( m_iNumRowsMinus1 >= iHeightInCU )
1664  {
1665    printf( "The current picture can not have so many rows.\n" );
1666    exit( EXIT_FAILURE );
1667  }
1668
[823]1669  if( m_iNumRowsMinus1 && !m_tileUniformSpacingFlag )
[313]1670  {
1671    for(Int i=0; i<m_iNumRowsMinus1; i++)
[823]1672      uiCummulativeRowHeight += m_tileRowHeight[i];
[313]1673
1674    if( uiCummulativeRowHeight >= iHeightInCU )
1675    {
1676      printf( "The height of the row is too large.\n" );
1677      exit( EXIT_FAILURE );
1678    }
1679  }
1680}
1681
1682#if SVC_EXTENSION
1683#if VPS_EXTN_DIRECT_REF_LAYERS
1684TEncTop* TEncTop::getRefLayerEnc( UInt refLayerIdc )
1685{
1686  if( m_ppcTEncTop[m_layerId]->getNumDirectRefLayers() <= 0 )
1687  {
1688    return (TEncTop *)getLayerEnc( 0 );
1689  }
1690
1691  return (TEncTop *)getLayerEnc( m_cVPS.getRefLayerId( m_layerId, refLayerIdc ) );
1692}
1693#endif
1694
[442]1695#if !REPN_FORMAT_IN_VPS
[313]1696Void TEncTop::xInitILRP()
1697{
1698  if(m_layerId>0)
1699  {
1700    g_bitDepthY     = m_cSPS.getBitDepthY();
1701    g_bitDepthC     = m_cSPS.getBitDepthC();
1702    g_uiMaxCUWidth  = m_cSPS.getMaxCUWidth();
1703    g_uiMaxCUHeight = m_cSPS.getMaxCUHeight();
1704    g_uiMaxCUDepth  = m_cSPS.getMaxCUDepth();
1705    g_uiAddCUDepth  = max (0, m_cSPS.getLog2MinCodingBlockSize() - (Int)m_cSPS.getQuadtreeTULog2MinSize() );
1706
1707    Int  numReorderPics[MAX_TLAYER];
1708    Window &conformanceWindow = m_cSPS.getConformanceWindow();
1709    Window defaultDisplayWindow = m_cSPS.getVuiParametersPresentFlag() ? m_cSPS.getVuiParameters()->getDefaultDisplayWindow() : Window();
1710
1711    if (m_cIlpPic[0] == NULL)
1712    {
[588]1713      for (Int j=0; j < m_numLayer; j++) // consider to set to NumDirectRefLayers[LayerIdInVps[nuh_layer_id]]
[313]1714      {
1715        m_cIlpPic[j] = new  TComPic;
[494]1716#if AUXILIARY_PICTURES
1717        m_cIlpPic[j]->create(m_iSourceWidth, m_iSourceHeight, m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
1718#else
[313]1719        m_cIlpPic[j]->create(m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
[494]1720#endif
[313]1721        for (Int i=0; i<m_cIlpPic[j]->getPicSym()->getNumberOfCUsInFrame(); i++)
1722        {
1723          m_cIlpPic[j]->getPicSym()->getCU(i)->initCU(m_cIlpPic[j], i);
1724        }
1725      }
1726    }
1727  }
1728}
[442]1729#else
1730Void TEncTop::xInitILRP()
1731{
[540]1732#if O0096_REP_FORMAT_INDEX
[999]1733  RepFormat *repFormat = m_cVPS.getVpsRepFormat( m_cSPS.getUpdateRepFormatFlag() ? m_cSPS.getUpdateRepFormatIndex() : m_cVPS.getVpsRepFormatIdx( m_cVPS.getLayerIdInVps(m_layerId) ) );
[540]1734#else
[999]1735  RepFormat *repFormat = m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( m_cVPS.getLayerIdInVps(m_layerId) ) );
[540]1736#endif
[442]1737  Int bitDepthY,bitDepthC,picWidth,picHeight;
[313]1738
[540]1739#if O0096_REP_FORMAT_INDEX
1740  bitDepthY   = repFormat->getBitDepthVpsLuma();
1741  bitDepthC   = repFormat->getBitDepthVpsChroma();
1742  picWidth    = repFormat->getPicWidthVpsInLumaSamples();
1743  picHeight   = repFormat->getPicHeightVpsInLumaSamples();
1744#else
[442]1745  if( m_cSPS.getUpdateRepFormatFlag() )
1746  {
1747    bitDepthY   = m_cSPS.getBitDepthY();
1748    bitDepthC   = m_cSPS.getBitDepthC();
1749    picWidth    = m_cSPS.getPicWidthInLumaSamples();
1750    picHeight   = m_cSPS.getPicHeightInLumaSamples();
1751  }
1752  else
1753  {
1754    bitDepthY   = repFormat->getBitDepthVpsLuma();
1755    bitDepthC   = repFormat->getBitDepthVpsChroma();
1756    picWidth    = repFormat->getPicWidthVpsInLumaSamples();
1757    picHeight   = repFormat->getPicHeightVpsInLumaSamples();
1758  }
[540]1759#endif
[442]1760 
1761  if(m_layerId > 0)
1762  {
1763    g_bitDepthY     = bitDepthY;
1764    g_bitDepthC     = bitDepthC;
1765    g_uiMaxCUWidth  = m_cSPS.getMaxCUWidth();
1766    g_uiMaxCUHeight = m_cSPS.getMaxCUHeight();
1767    g_uiMaxCUDepth  = m_cSPS.getMaxCUDepth();
1768    g_uiAddCUDepth  = max (0, m_cSPS.getLog2MinCodingBlockSize() - (Int)m_cSPS.getQuadtreeTULog2MinSize() );
1769
1770    Int  numReorderPics[MAX_TLAYER];
[869]1771#if R0156_CONF_WINDOW_IN_REP_FORMAT
1772    Window &conformanceWindow = repFormat->getConformanceWindowVps();
1773#else
[442]1774    Window &conformanceWindow = m_cSPS.getConformanceWindow();
[869]1775#endif
[442]1776    Window defaultDisplayWindow = m_cSPS.getVuiParametersPresentFlag() ? m_cSPS.getVuiParameters()->getDefaultDisplayWindow() : Window();
1777
1778    if (m_cIlpPic[0] == NULL)
1779    {
[588]1780      for (Int j=0; j < m_numDirectRefLayers; j++)
[442]1781      {
1782        m_cIlpPic[j] = new  TComPic;
[494]1783#if AUXILIARY_PICTURES
1784        m_cIlpPic[j]->create(picWidth, picHeight, m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
1785#else
[442]1786        m_cIlpPic[j]->create(picWidth, picHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
[494]1787#endif
[442]1788        for (Int i=0; i<m_cIlpPic[j]->getPicSym()->getNumberOfCUsInFrame(); i++)
1789        {
1790          m_cIlpPic[j]->getPicSym()->getCU(i)->initCU(m_cIlpPic[j], i);
1791        }
1792      }
1793    }
1794  }
1795}
1796#endif
[540]1797
1798#if O0098_SCALED_REF_LAYER_ID
1799Window& TEncTop::getScaledRefLayerWindowForLayer(Int layerId)
1800{
1801  static Window win;
1802
1803  for (Int i = 0; i < m_numScaledRefLayerOffsets; i++)
1804  {
1805    if (layerId == m_scaledRefLayerId[i])
1806    {
1807      return m_scaledRefLayerWindow[i];
1808    }
1809  }
1810
1811  win.resetWindow();  // scaled reference layer offsets are inferred to be zero when not present
1812  return win;
1813}
[849]1814#if REF_REGION_OFFSET
1815Window& TEncTop::getRefLayerWindowForLayer(Int layerId)
1816{
1817  static Window win;
1818
1819  for (Int i = 0; i < m_numScaledRefLayerOffsets; i++)
1820  {
1821    if (layerId == m_refLayerId[i])
1822    {
1823      return m_refLayerWindow[i];
1824    }
1825  }
1826
1827  win.resetWindow();  // reference offsets are inferred to be zero when not present
1828  return win;
1829}
[540]1830#endif
[849]1831#endif
[540]1832
[494]1833#endif //SVC_EXTENSION
[313]1834//! \}
Note: See TracBrowser for help on using the repository browser.