source: SHVCSoftware/trunk/source/Lib/TLibEncoder/TEncTop.cpp @ 1547

Last change on this file since 1547 was 906, checked in by seregin, 10 years ago

merge SHM-dev

  • Property svn:eol-style set to native
File size: 60.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-2014, 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#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;
85#if SVC_EXTENSION
86  memset(m_cIlpPic, 0, sizeof(m_cIlpPic));
87#if REF_IDX_MFM
88  m_bMFMEnabledFlag = false;
89#endif
90  m_numScaledRefLayerOffsets = 0;
91#if POC_RESET_FLAG || POC_RESET_IDC_ENCODER
92  m_pocAdjustmentValue     = 0;
93#endif
94#if NO_CLRAS_OUTPUT_FLAG
95  m_noClrasOutputFlag          = false;
96  m_layerInitializedFlag       = false;
97  m_firstPicInLayerDecodedFlag = false;
98  m_noOutputOfPriorPicsFlags   = false;
99#endif
100#if P0297_VPS_POC_LSB_ALIGNED_FLAG
101  m_pocDecrementedInDPBFlag    = false;
102#endif
103#endif //SVC_EXTENSION
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
119
120  // create processing unit classes
121#if SVC_EXTENSION
122  m_cGOPEncoder.        create( m_layerId );
123#else
124  m_cGOPEncoder.        create();
125#endif
126#if AUXILIARY_PICTURES
127  m_cSliceEncoder.      create( getSourceWidth(), getSourceHeight(), m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
128#else
129  m_cSliceEncoder.      create( getSourceWidth(), getSourceHeight(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
130#endif
131  m_cCuEncoder.         create( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight );
132  if (m_bUseSAO)
133  {
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
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  }
158
159  m_pppcRDSbacCoder = new TEncSbac** [g_uiMaxCUDepth+1];
160#if FAST_BIT_EST
161  m_pppcBinCoderCABAC = new TEncBinCABACCounter** [g_uiMaxCUDepth+1];
162#else
163  m_pppcBinCoderCABAC = new TEncBinCABAC** [g_uiMaxCUDepth+1];
164#endif
165
166  for ( Int iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
167  {
168    m_pppcRDSbacCoder[iDepth] = new TEncSbac* [CI_NUM];
169#if FAST_BIT_EST
170    m_pppcBinCoderCABAC[iDepth] = new TEncBinCABACCounter* [CI_NUM];
171#else
172    m_pppcBinCoderCABAC[iDepth] = new TEncBinCABAC* [CI_NUM];
173#endif
174
175    for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
176    {
177      m_pppcRDSbacCoder[iDepth][iCIIdx] = new TEncSbac;
178#if FAST_BIT_EST
179      m_pppcBinCoderCABAC [iDepth][iCIIdx] = new TEncBinCABACCounter;
180#else
181      m_pppcBinCoderCABAC [iDepth][iCIIdx] = new TEncBinCABAC;
182#endif
183      m_pppcRDSbacCoder   [iDepth][iCIIdx]->init( m_pppcBinCoderCABAC [iDepth][iCIIdx] );
184    }
185  }
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
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  }
220
221  m_ppppcRDSbacCoders      = new TEncSbac***    [iNumSubstreams];
222  m_ppppcBinCodersCABAC    = new TEncBinCABAC***[iNumSubstreams];
223  for ( UInt ui = 0 ; ui < iNumSubstreams ; ui++ )
224  {
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++ )
229    {
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 ++ )
234      {
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] );
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  {
251    m_cEncSAO.destroyEncData();
252    m_cEncSAO.destroy();
253  }
254  m_cLoopFilter.        destroy();
255  m_cRateCtrl.          destroy();
256
257  Int iDepth;
258  for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
259  {
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  {
278    for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
279    {
280      for (Int iCIIdx = 0; iCIIdx < CI_NUM; iCIIdx ++ )
281      {
282        delete m_ppppcRDSbacCoders  [ui][iDepth][iCIIdx];
283        delete m_ppppcBinCodersCABAC[ui][iDepth][iCIIdx];
284      }
285    }
286
287    for ( iDepth = 0; iDepth < g_uiMaxCUDepth+1; iDepth++ )
288    {
289      delete [] m_ppppcRDSbacCoders  [ui][iDepth];
290      delete [] m_ppppcBinCodersCABAC[ui][iDepth];
291    }
292    delete[] m_ppppcRDSbacCoders  [ui];
293    delete[] m_ppppcBinCodersCABAC[ui];
294  }
295  delete[] m_ppppcRDSbacCoders;
296  delete[] m_ppppcBinCodersCABAC;
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 
304#if SVC_EXTENSION
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    }
313  }
314#else
315  // destroy ROM
316  destroyROM();
317#endif
318  return;
319}
320
321Void TEncTop::init(Bool isFieldCoding)
322{
323  // initialize SPS
324  xInitSPS();
325 
326  /* set the VPS profile information */
327  *m_cVPS.getPTL() = *m_cSPS.getPTL();
328#if VPS_VUI_BSP_HRD_PARAMS
329  m_cVPS.getTimingInfo()->setTimingInfoPresentFlag       ( true );
330#else
331  m_cVPS.getTimingInfo()->setTimingInfoPresentFlag       ( false );
332#endif
333  // initialize PPS
334  m_cPPS.setSPS(&m_cSPS);
335  xInitPPS();
336  xInitRPS(isFieldCoding);
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
411  m_cGOPEncoder.compressGOP(iPicIdInGOP, m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, false, false);
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
468  m_cGOPEncoder.compressGOP(m_iPOCLast, m_iNumPicRcvd, m_cListPic, rcListPicYuvRecOut, accessUnitsOut, false, false);
469
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
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();
533
534    // compute image characteristics
535    if ( getUseAdaptiveQP() )
536    {
537      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcTopField ) );
538    }   
539
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();
573
574    // compute image characteristics
575    if ( getUseAdaptiveQP() )
576    {
577      m_cPreanalyzer.xPreanalyze( dynamic_cast<TEncPic*>( pcBottomField ) );
578    }       
579
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    {
608#if AUXILIARY_PICTURES
609      rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
610#else
611      rpcPicYuvRec->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
612#endif
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   
687    TComPicYuv* rpcPicYuvRec;
688    if ( rcListPicYuvRecOut.size() == (UInt)m_iGOPSize )
689    {
690      rpcPicYuvRec = rcListPicYuvRecOut.popFront();
691    }
692    else
693    {
694      rpcPicYuvRec = new TComPicYuv;
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
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        {
788#if MOVE_SCALED_OFFSET_TO_PPS
789#if O0098_SCALED_REF_LAYER_ID
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
796          const Window scalEL = getSPS()->getScaledRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
797#else
798          const Window scalEL = getSPS()->getScaledRefLayerWindow(i);
799#endif
800#endif
801#if REF_REGION_OFFSET
802          const Window altRL  = getPPS()->getRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
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
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);
811#endif
812#else
813          Bool zeroOffsets = ( scalEL.getWindowLeftOffset() == 0 && scalEL.getWindowRightOffset() == 0 && scalEL.getWindowTopOffset() == 0 && scalEL.getWindowBottomOffset() == 0 );
814#endif
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
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
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
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
842#endif
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            )
850#else
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
856#endif
857#else
858            || !zeroOffsets
859#endif
860          )
861#endif
862          {
863            pcEPic->setSpatialEnhLayerFlag( i, true );
864
865            //only for scalable extension
866            assert( m_cVPS.getScalabilityMask( SCALABILITY_ID ) == true );
867          }
868        }
869      }
870#endif
871
872#if SVC_EXTENSION
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
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);
879#endif
880#else  //SVC_EXTENSION
881      pcEPic->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, m_cPPS.getMaxCuDQPDepth()+1 ,
882                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics);
883#endif //SVC_EXTENSION
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        {
895#if MOVE_SCALED_OFFSET_TO_PPS
896#if O0098_SCALED_REF_LAYER_ID
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
903          const Window scalEL = getSPS()->getScaledRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
904#else
905          const Window scalEL = getSPS()->getScaledRefLayerWindow(i);
906#endif
907#endif
908#if REF_REGION_OFFSET
909          const Window altRL  = getPPS()->getRefLayerWindowForLayer(m_cVPS.getRefLayerId(m_layerId, i));
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
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);
918#endif
919#else
920          Bool zeroOffsets = ( scalEL.getWindowLeftOffset() == 0 && scalEL.getWindowRightOffset() == 0 && scalEL.getWindowTopOffset() == 0 && scalEL.getWindowBottomOffset() == 0 );
921#endif
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
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
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
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)
948#else
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
954#endif
955#else
956            || !zeroOffsets
957#endif
958          )
959#endif
960          {
961            rpcPic->setSpatialEnhLayerFlag( i, true );
962
963            //only for scalable extension
964            assert( m_cVPS.getScalabilityMask( SCALABILITY_ID ) == true );
965          }
966        }
967      }
968#endif
969
970#if SVC_EXTENSION
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
975      rpcPic->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
976                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics, &m_cSPS);
977#endif
978#else  //SVC_EXTENSION
979      rpcPic->create( m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
980                      m_conformanceWindow, m_defaultDisplayWindow, m_numReorderPics);
981#endif //SVC_EXTENSION
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
998  m_cSPS.setExtensionFlag( m_layerId > 0 ? true : false );
999#if R0042_PROFILE_INDICATION
1000  m_cSPS.setNumDirectRefLayers(m_numAddLayerSets);
1001#endif
1002#if Q0078_ADD_LAYER_SETS
1003  if( !m_numDirectRefLayers && m_numAddLayerSets )
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
1012  m_cSPS.setLayerId(m_layerId);
1013#endif
1014#if !MOVE_SCALED_OFFSET_TO_PPS
1015  m_cSPS.setNumScaledRefLayerOffsets(m_numScaledRefLayerOffsets);
1016  for(Int i = 0; i < m_cSPS.getNumScaledRefLayerOffsets(); i++)
1017  {
1018#if O0098_SCALED_REF_LAYER_ID
1019    m_cSPS.setScaledRefLayerId(i, m_scaledRefLayerId[i]);
1020#endif
1021    m_cSPS.getScaledRefLayerWindow(i) = m_scaledRefLayerWindow[i];
1022#if P0312_VERT_PHASE_ADJ
1023    m_cSPS.setVertPhasePositionEnableFlag( m_scaledRefLayerId[i], m_scaledRefLayerWindow[i].getVertPhasePositionEnableFlag() );
1024#endif
1025  }
1026#endif
1027#endif //SVC_EXTENSION
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      );
1058#if AUXILIARY_PICTURES
1059  m_cSPS.setChromaFormatIdc( m_chromaFormatIDC);
1060#endif
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
1104#if REPN_FORMAT_IN_VPS
1105  m_cSPS.setBitDepthY( m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( m_layerId ) )->getBitDepthVpsLuma() );
1106  m_cSPS.setBitDepthC( m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( m_layerId ) )->getBitDepthVpsChroma()  );
1107
1108  m_cSPS.setQpBDOffsetY ( 6*(m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( m_layerId ) )->getBitDepthVpsLuma()  - 8) );
1109  m_cSPS.setQpBDOffsetC ( 6*(m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( m_layerId ) )->getBitDepthVpsChroma()  - 8) );
1110#else
1111  m_cSPS.setBitDepthY( g_bitDepthY );
1112  m_cSPS.setBitDepthC( g_bitDepthC );
1113
1114  m_cSPS.setQpBDOffsetY ( 6*(g_bitDepthY - 8) );
1115  m_cSPS.setQpBDOffsetC ( 6*(g_bitDepthC - 8) );
1116#endif
1117
1118  m_cSPS.setUseSAO( m_bUseSAO );
1119
1120  m_cSPS.setMaxTLayers( m_maxTempLayer );
1121  m_cSPS.setTemporalIdNestingFlag( ( m_maxTempLayer == 1 ) ? true : false );
1122  for ( i = 0; i < min(m_cSPS.getMaxTLayers(),(UInt) MAX_TLAYER); i++ )
1123  {
1124    m_cSPS.setMaxDecPicBuffering(m_maxDecPicBuffering[i], i);
1125    m_cSPS.setNumReorderPics(m_numReorderPics[i], i);
1126  }
1127  m_cSPS.setPCMBitDepthLuma (g_uiPCMBitDepthLuma);
1128  m_cSPS.setPCMBitDepthChroma (g_uiPCMBitDepthChroma);
1129  m_cSPS.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag );
1130
1131  m_cSPS.setScalingListFlag ( (m_useScalingListId == 0) ? 0 : 1 );
1132
1133  m_cSPS.setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
1134
1135  m_cSPS.setVuiParametersPresentFlag(getVuiParametersPresentFlag());
1136  if (m_cSPS.getVuiParametersPresentFlag())
1137  {
1138    TComVUI* pcVUI = m_cSPS.getVuiParameters();
1139    pcVUI->setAspectRatioInfoPresentFlag(getAspectRatioInfoPresentFlag());
1140    pcVUI->setAspectRatioIdc(getAspectRatioIdc());
1141    pcVUI->setSarWidth(getSarWidth());
1142    pcVUI->setSarHeight(getSarHeight());
1143    pcVUI->setOverscanInfoPresentFlag(getOverscanInfoPresentFlag());
1144    pcVUI->setOverscanAppropriateFlag(getOverscanAppropriateFlag());
1145    pcVUI->setVideoSignalTypePresentFlag(getVideoSignalTypePresentFlag());
1146    pcVUI->setVideoFormat(getVideoFormat());
1147    pcVUI->setVideoFullRangeFlag(getVideoFullRangeFlag());
1148    pcVUI->setColourDescriptionPresentFlag(getColourDescriptionPresentFlag());
1149    pcVUI->setColourPrimaries(getColourPrimaries());
1150    pcVUI->setTransferCharacteristics(getTransferCharacteristics());
1151    pcVUI->setMatrixCoefficients(getMatrixCoefficients());
1152    pcVUI->setChromaLocInfoPresentFlag(getChromaLocInfoPresentFlag());
1153    pcVUI->setChromaSampleLocTypeTopField(getChromaSampleLocTypeTopField());
1154    pcVUI->setChromaSampleLocTypeBottomField(getChromaSampleLocTypeBottomField());
1155    pcVUI->setNeutralChromaIndicationFlag(getNeutralChromaIndicationFlag());
1156    pcVUI->setDefaultDisplayWindow(getDefaultDisplayWindow());
1157    pcVUI->setFrameFieldInfoPresentFlag(getFrameFieldInfoPresentFlag());
1158    pcVUI->setFieldSeqFlag(false);
1159    pcVUI->setHrdParametersPresentFlag(false);
1160    pcVUI->getTimingInfo()->setPocProportionalToTimingFlag(getPocProportionalToTimingFlag());
1161    pcVUI->getTimingInfo()->setNumTicksPocDiffOneMinus1   (getNumTicksPocDiffOneMinus1()   );
1162    pcVUI->setBitstreamRestrictionFlag(getBitstreamRestrictionFlag());
1163    pcVUI->setTilesFixedStructureFlag(getTilesFixedStructureFlag());
1164    pcVUI->setMotionVectorsOverPicBoundariesFlag(getMotionVectorsOverPicBoundariesFlag());
1165    pcVUI->setMinSpatialSegmentationIdc(getMinSpatialSegmentationIdc());
1166    pcVUI->setMaxBytesPerPicDenom(getMaxBytesPerPicDenom());
1167    pcVUI->setMaxBitsPerMinCuDenom(getMaxBitsPerMinCuDenom());
1168    pcVUI->setLog2MaxMvLengthHorizontal(getLog2MaxMvLengthHorizontal());
1169    pcVUI->setLog2MaxMvLengthVertical(getLog2MaxMvLengthVertical());
1170  }
1171}
1172
1173Void TEncTop::xInitPPS()
1174{
1175  m_cPPS.setConstrainedIntraPred( m_bUseConstrainedIntraPred );
1176  Bool bUseDQP = (getMaxCuDQPDepth() > 0)? true : false;
1177
1178  if((getMaxDeltaQP() != 0 )|| getUseAdaptiveQP())
1179  {
1180    bUseDQP = true;
1181  }
1182
1183  if(bUseDQP)
1184  {
1185    m_cPPS.setUseDQP(true);
1186    m_cPPS.setMaxCuDQPDepth( m_iMaxCuDQPDepth );
1187    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
1188  }
1189  else
1190  {
1191    m_cPPS.setUseDQP(false);
1192    m_cPPS.setMaxCuDQPDepth( 0 );
1193    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
1194  }
1195
1196  if ( m_RCEnableRateControl )
1197  {
1198    m_cPPS.setUseDQP(true);
1199    m_cPPS.setMaxCuDQPDepth( 0 );
1200    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
1201  } 
1202
1203  m_cPPS.setChromaCbQpOffset( m_chromaCbQpOffset );
1204  m_cPPS.setChromaCrQpOffset( m_chromaCrQpOffset );
1205
1206  m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams);
1207  m_cPPS.setEntropyCodingSyncEnabledFlag( m_iWaveFrontSynchro > 0 );
1208  m_cPPS.setTilesEnabledFlag( (m_iNumColumnsMinus1 > 0 || m_iNumRowsMinus1 > 0) );
1209  m_cPPS.setUseWP( m_useWeightedPred );
1210  m_cPPS.setWPBiPred( m_useWeightedBiPred );
1211  m_cPPS.setOutputFlagPresentFlag( false );
1212  m_cPPS.setSignHideFlag(getSignHideFlag());
1213  if ( getDeblockingFilterMetric() )
1214  {
1215    m_cPPS.setDeblockingFilterControlPresentFlag (true);
1216    m_cPPS.setDeblockingFilterOverrideEnabledFlag(true);
1217    m_cPPS.setPicDisableDeblockingFilterFlag(false);
1218    m_cPPS.setDeblockingFilterBetaOffsetDiv2(0);
1219    m_cPPS.setDeblockingFilterTcOffsetDiv2(0);
1220  } 
1221  else
1222  {
1223  m_cPPS.setDeblockingFilterControlPresentFlag (m_DeblockingFilterControlPresent );
1224  }
1225  m_cPPS.setLog2ParallelMergeLevelMinus2   (m_log2ParallelMergeLevelMinus2 );
1226  m_cPPS.setCabacInitPresentFlag(CABAC_INIT_PRESENT_FLAG);
1227  m_cPPS.setLoopFilterAcrossSlicesEnabledFlag( m_bLFCrossSliceBoundaryFlag );
1228  Int histogram[MAX_NUM_REF + 1];
1229  for( Int i = 0; i <= MAX_NUM_REF; i++ )
1230  {
1231    histogram[i]=0;
1232  }
1233  for( Int i = 0; i < getGOPSize(); i++ )
1234  {
1235    assert(getGOPEntry(i).m_numRefPicsActive >= 0 && getGOPEntry(i).m_numRefPicsActive <= MAX_NUM_REF);
1236    histogram[getGOPEntry(i).m_numRefPicsActive]++;
1237  }
1238  Int maxHist=-1;
1239  Int bestPos=0;
1240  for( Int i = 0; i <= MAX_NUM_REF; i++ )
1241  {
1242    if(histogram[i]>maxHist)
1243    {
1244      maxHist=histogram[i];
1245      bestPos=i;
1246    }
1247  }
1248  assert(bestPos <= 15);
1249  m_cPPS.setNumRefIdxL0DefaultActive(bestPos);
1250  m_cPPS.setNumRefIdxL1DefaultActive(bestPos);
1251  m_cPPS.setTransquantBypassEnableFlag(getTransquantBypassEnableFlag());
1252  m_cPPS.setUseTransformSkip( m_useTransformSkip );
1253  if (m_sliceSegmentMode)
1254  {
1255    m_cPPS.setDependentSliceSegmentsEnabledFlag( true );
1256  }
1257  if( m_cPPS.getDependentSliceSegmentsEnabledFlag() )
1258  {
1259    Int NumCtx = m_cPPS.getEntropyCodingSyncEnabledFlag()?2:1;
1260    m_cSliceEncoder.initCtxMem( NumCtx );
1261    for ( UInt st = 0; st < NumCtx; st++ )
1262    {
1263      TEncSbac* ctx = NULL;
1264      ctx = new TEncSbac;
1265      ctx->init( &m_cBinCoderCABAC );
1266      m_cSliceEncoder.setCtxMem( ctx, st );
1267    }
1268  }
1269#if SVC_EXTENSION
1270#if SCALINGLIST_INFERRING
1271  m_cPPS.setLayerId( m_layerId );
1272#endif
1273
1274#if Q0078_ADD_LAYER_SETS
1275  if( !m_numDirectRefLayers && m_numAddLayerSets )
1276  {
1277    m_cPPS.setLayerId(0); // layer ID 0 for independent layers
1278  }
1279#endif
1280
1281  if( m_layerId > 0 )
1282  {
1283    m_cPPS.setListsModificationPresentFlag(true);
1284    m_cPPS.setExtensionFlag(true);
1285  }
1286  else
1287  {
1288    m_cPPS.setListsModificationPresentFlag(false);
1289    m_cPPS.setExtensionFlag(false);
1290  }
1291
1292  m_cPPS.setPPSId( m_iPPSIdCnt );
1293  m_cPPS.setSPSId( m_iSPSIdCnt );
1294#if POC_RESET_FLAG
1295  m_cPPS.setNumExtraSliceHeaderBits( 2 );
1296#endif
1297#if O0149_CROSS_LAYER_BLA_FLAG
1298  if (m_crossLayerBLAFlag)
1299  {
1300    m_cPPS.setNumExtraSliceHeaderBits( 3 );
1301  }
1302#endif
1303#if MOVE_SCALED_OFFSET_TO_PPS
1304  m_cPPS.setNumScaledRefLayerOffsets(m_numScaledRefLayerOffsets);
1305  for(Int i = 0; i < m_cPPS.getNumScaledRefLayerOffsets(); i++)
1306  {
1307#if O0098_SCALED_REF_LAYER_ID
1308    m_cPPS.setScaledRefLayerId(i, m_scaledRefLayerId[i]);
1309#endif
1310    m_cPPS.getScaledRefLayerWindow(i) = m_scaledRefLayerWindow[i];
1311#if REF_REGION_OFFSET
1312    m_cPPS.getRefLayerWindow(i) = m_refLayerWindow[i];
1313    m_cPPS.setScaledRefLayerOffsetPresentFlag( m_scaledRefLayerId[i], m_scaledRefLayerOffsetPresentFlag[i] );
1314    m_cPPS.setRefRegionOffsetPresentFlag( m_scaledRefLayerId[i], m_refRegionOffsetPresentFlag[i] );
1315#endif
1316#if R0209_GENERIC_PHASE
1317    m_cPPS.setResamplePhaseSetPresentFlag( m_scaledRefLayerId[i], m_resamplePhaseSetPresentFlag[i] );
1318    m_cPPS.setPhaseHorLuma( m_scaledRefLayerId[i], m_phaseHorLuma[i] );
1319    m_cPPS.setPhaseVerLuma( m_scaledRefLayerId[i], m_phaseVerLuma[i] );
1320    m_cPPS.setPhaseHorChroma( m_scaledRefLayerId[i], m_phaseHorChroma[i] );
1321    m_cPPS.setPhaseVerChroma( m_scaledRefLayerId[i], m_phaseVerChroma[i] );
1322#endif
1323#if P0312_VERT_PHASE_ADJ
1324    m_cPPS.setVertPhasePositionEnableFlag( m_scaledRefLayerId[i], m_scaledRefLayerWindow[i].getVertPhasePositionEnableFlag() );
1325#endif
1326  }
1327#endif
1328#if Q0048_CGS_3D_ASYMLUT
1329  m_cPPS.setCGSFlag( m_nCGSFlag );
1330#endif
1331#if POC_RESET_IDC_ENCODER
1332  m_cPPS.setPocResetInfoPresentFlag( true );
1333  m_cPPS.setExtensionFlag( true );
1334  m_cPPS.setSliceHeaderExtensionPresentFlag( true );
1335#endif
1336#endif //SVC_EXTENSION
1337}
1338
1339//Function for initializing m_RPSList, a list of TComReferencePictureSet, based on the GOPEntry objects read from the config file.
1340Void TEncTop::xInitRPS(Bool isFieldCoding)
1341{
1342  TComReferencePictureSet*      rps;
1343 
1344  m_cSPS.createRPSList(getGOPSize()+m_extraRPSs+1);
1345  TComRPSList* rpsList = m_cSPS.getRPSList();
1346
1347  for( Int i = 0; i < getGOPSize()+m_extraRPSs; i++) 
1348  {
1349    GOPEntry ge = getGOPEntry(i);
1350    rps = rpsList->getReferencePictureSet(i);
1351    rps->setNumberOfPictures(ge.m_numRefPics);
1352    rps->setNumRefIdc(ge.m_numRefIdc);
1353    Int numNeg = 0;
1354    Int numPos = 0;
1355    for( Int j = 0; j < ge.m_numRefPics; j++)
1356    {
1357      rps->setDeltaPOC(j,ge.m_referencePics[j]);
1358      rps->setUsed(j,ge.m_usedByCurrPic[j]);
1359      if(ge.m_referencePics[j]>0)
1360      {
1361        numPos++;
1362      }
1363      else
1364      {
1365        numNeg++;
1366      }
1367    }
1368    rps->setNumberOfNegativePictures(numNeg);
1369    rps->setNumberOfPositivePictures(numPos);
1370
1371    // handle inter RPS intialization from the config file.
1372#if AUTO_INTER_RPS
1373    rps->setInterRPSPrediction(ge.m_interRPSPrediction > 0);  // not very clean, converting anything > 0 to true.
1374    rps->setDeltaRIdxMinus1(0);                               // index to the Reference RPS is always the previous one.
1375    TComReferencePictureSet*     RPSRef = rpsList->getReferencePictureSet(i-1);  // get the reference RPS
1376
1377    if (ge.m_interRPSPrediction == 2)  // Automatic generation of the inter RPS idc based on the RIdx provided.
1378    {
1379      Int deltaRPS = getGOPEntry(i-1).m_POC - ge.m_POC;  // the ref POC - current POC
1380      Int numRefDeltaPOC = RPSRef->getNumberOfPictures();
1381
1382      rps->setDeltaRPS(deltaRPS);           // set delta RPS
1383      rps->setNumRefIdc(numRefDeltaPOC+1);  // set the numRefIdc to the number of pictures in the reference RPS + 1.
1384      Int count=0;
1385      for (Int j = 0; j <= numRefDeltaPOC; j++ ) // cycle through pics in reference RPS.
1386      {
1387        Int RefDeltaPOC = (j<numRefDeltaPOC)? RPSRef->getDeltaPOC(j): 0;  // if it is the last decoded picture, set RefDeltaPOC = 0
1388        rps->setRefIdc(j, 0);
1389        for (Int k = 0; k < rps->getNumberOfPictures(); k++ )  // cycle through pics in current RPS.
1390        {
1391          if (rps->getDeltaPOC(k) == ( RefDeltaPOC + deltaRPS))  // if the current RPS has a same picture as the reference RPS.
1392          {
1393              rps->setRefIdc(j, (rps->getUsed(k)?1:2));
1394              count++;
1395              break;
1396          }
1397        }
1398      }
1399      if (count != rps->getNumberOfPictures())
1400      {
1401        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");
1402        rps->setInterRPSPrediction(0);
1403      }
1404    }
1405    else if (ge.m_interRPSPrediction == 1)  // inter RPS idc based on the RefIdc values provided in config file.
1406    {
1407      rps->setDeltaRPS(ge.m_deltaRPS);
1408      rps->setNumRefIdc(ge.m_numRefIdc);
1409      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1410      {
1411        rps->setRefIdc(j, ge.m_refIdc[j]);
1412      }
1413#if WRITE_BACK
1414      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1415      // computed from the RefIdc.  A warning is printed if they are not identical.
1416      numNeg = 0;
1417      numPos = 0;
1418      TComReferencePictureSet      RPSTemp;  // temporary variable
1419
1420      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1421      {
1422        if (ge.m_refIdc[j])
1423        {
1424          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1425          RPSTemp.setDeltaPOC((numNeg+numPos),deltaPOC);
1426          RPSTemp.setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1427          if (deltaPOC<0)
1428          {
1429            numNeg++;
1430          }
1431          else
1432          {
1433            numPos++;
1434          }
1435        }
1436      }
1437      if (numNeg != rps->getNumberOfNegativePictures())
1438      {
1439        printf("Warning: number of negative pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1440        rps->setNumberOfNegativePictures(numNeg);
1441        rps->setNumberOfPictures(numNeg+numPos);
1442      }
1443      if (numPos != rps->getNumberOfPositivePictures())
1444      {
1445        printf("Warning: number of positive pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1446        rps->setNumberOfPositivePictures(numPos);
1447        rps->setNumberOfPictures(numNeg+numPos);
1448      }
1449      RPSTemp.setNumberOfPictures(numNeg+numPos);
1450      RPSTemp.setNumberOfNegativePictures(numNeg);
1451      RPSTemp.sortDeltaPOC();     // sort the created delta POC before comparing
1452      // check if Delta POC and Used are the same
1453      // print warning if they are not.
1454      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1455      {
1456        if (RPSTemp.getDeltaPOC(j) != rps->getDeltaPOC(j))
1457        {
1458          printf("Warning: delta POC is different between intra RPS and inter RPS specified in the config file.\n");
1459          rps->setDeltaPOC(j,RPSTemp.getDeltaPOC(j));
1460        }
1461        if (RPSTemp.getUsed(j) != rps->getUsed(j))
1462        {
1463          printf("Warning: Used by Current in RPS is different between intra and inter RPS specified in the config file.\n");
1464          rps->setUsed(j,RPSTemp.getUsed(j));
1465        }
1466      }
1467#endif
1468    }
1469#else
1470    rps->setInterRPSPrediction(ge.m_interRPSPrediction);
1471    if (ge.m_interRPSPrediction)
1472    {
1473      rps->setDeltaRIdxMinus1(0);
1474      rps->setDeltaRPS(ge.m_deltaRPS);
1475      rps->setNumRefIdc(ge.m_numRefIdc);
1476      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1477      {
1478        rps->setRefIdc(j, ge.m_refIdc[j]);
1479      }
1480#if WRITE_BACK
1481      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1482      // computed from the RefIdc.  This is not necessary if both are identical. Currently there is no check to see if they are identical.
1483      numNeg = 0;
1484      numPos = 0;
1485      TComReferencePictureSet*     RPSRef = m_RPSList.getReferencePictureSet(i-1);
1486
1487      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1488      {
1489        if (ge.m_refIdc[j])
1490        {
1491          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1492          rps->setDeltaPOC((numNeg+numPos),deltaPOC);
1493          rps->setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1494          if (deltaPOC<0)
1495          {
1496            numNeg++;
1497          }
1498          else
1499          {
1500            numPos++;
1501          }
1502        }
1503      }
1504      rps->setNumberOfNegativePictures(numNeg);
1505      rps->setNumberOfPositivePictures(numPos);
1506      rps->sortDeltaPOC();
1507#endif
1508    }
1509#endif //INTER_RPS_AUTO
1510  }
1511  //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.
1512  //The position = GOPSize + extraRPSs which is (a priori) unused is reserved for this field in the RPS.
1513  if (isFieldCoding) 
1514  {
1515    rps = rpsList->getReferencePictureSet(getGOPSize()+m_extraRPSs);
1516    rps->setNumberOfPictures(1);
1517    rps->setNumberOfNegativePictures(1);
1518    rps->setNumberOfPositivePictures(0);
1519    rps->setNumberOfLongtermPictures(0);
1520    rps->setDeltaPOC(0,-1);
1521    rps->setPOC(0,0);
1522    rps->setUsed(0,true);
1523    rps->setInterRPSPrediction(false);
1524    rps->setDeltaRIdxMinus1(0);
1525    rps->setDeltaRPS(0);
1526    rps->setNumRefIdc(0);
1527}
1528}
1529
1530   // This is a function that
1531   // determines what Reference Picture Set to use
1532   // for a specific slice (with POC = POCCurr)
1533Void TEncTop::selectReferencePictureSet(TComSlice* slice, Int POCCurr, Int GOPid )
1534{
1535  slice->setRPSidx(GOPid);
1536  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1537  {   
1538    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1539    {
1540      Int POCIndex = POCCurr%m_uiIntraPeriod;
1541      if(POCIndex == 0)
1542      {
1543        POCIndex = m_uiIntraPeriod;
1544      }
1545      if(POCIndex == m_GOPList[extraNum].m_POC)
1546      {
1547        slice->setRPSidx(extraNum);
1548      }
1549    }
1550    else
1551    {
1552      if(POCCurr==m_GOPList[extraNum].m_POC)
1553      {
1554        slice->setRPSidx(extraNum);
1555      }
1556    }
1557  }
1558
1559  if(POCCurr == 1 && slice->getPic()->isField())
1560  {
1561    slice->setRPSidx(m_iGOPSize+m_extraRPSs);
1562  }
1563
1564  slice->setRPS(getSPS()->getRPSList()->getReferencePictureSet(slice->getRPSidx()));
1565  slice->getRPS()->setNumberOfPictures(slice->getRPS()->getNumberOfNegativePictures()+slice->getRPS()->getNumberOfPositivePictures());
1566}
1567
1568Int TEncTop::getReferencePictureSetIdxForSOP(TComSlice* slice, Int POCCurr, Int GOPid )
1569{
1570  int rpsIdx = GOPid;
1571
1572  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1573  {   
1574    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1575    {
1576      Int POCIndex = POCCurr%m_uiIntraPeriod;
1577      if(POCIndex == 0)
1578      {
1579        POCIndex = m_uiIntraPeriod;
1580      }
1581      if(POCIndex == m_GOPList[extraNum].m_POC)
1582      {
1583        rpsIdx = extraNum;
1584      }
1585    }
1586    else
1587    {
1588      if(POCCurr==m_GOPList[extraNum].m_POC)
1589      {
1590        rpsIdx = extraNum;
1591      }
1592    }
1593  }
1594
1595  return rpsIdx;
1596}
1597
1598Void  TEncTop::xInitPPSforTiles()
1599{
1600  m_cPPS.setTileUniformSpacingFlag( m_tileUniformSpacingFlag );
1601  m_cPPS.setNumTileColumnsMinus1( m_iNumColumnsMinus1 );
1602  m_cPPS.setNumTileRowsMinus1( m_iNumRowsMinus1 );
1603  if( !m_tileUniformSpacingFlag )
1604  {
1605    m_cPPS.setTileColumnWidth( m_tileColumnWidth );
1606    m_cPPS.setTileRowHeight( m_tileRowHeight );
1607  }
1608  m_cPPS.setLoopFilterAcrossTilesEnabledFlag( m_loopFilterAcrossTilesEnabledFlag );
1609
1610  // # substreams is "per tile" when tiles are independent.
1611  if (m_iWaveFrontSynchro )
1612  {
1613    m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams * (m_iNumColumnsMinus1+1));
1614  }
1615}
1616
1617Void  TEncCfg::xCheckGSParameters()
1618{
1619  Int   iWidthInCU = ( m_iSourceWidth%g_uiMaxCUWidth ) ? m_iSourceWidth/g_uiMaxCUWidth + 1 : m_iSourceWidth/g_uiMaxCUWidth;
1620  Int   iHeightInCU = ( m_iSourceHeight%g_uiMaxCUHeight ) ? m_iSourceHeight/g_uiMaxCUHeight + 1 : m_iSourceHeight/g_uiMaxCUHeight;
1621  UInt  uiCummulativeColumnWidth = 0;
1622  UInt  uiCummulativeRowHeight = 0;
1623
1624  //check the column relative parameters
1625  if( m_iNumColumnsMinus1 >= (1<<(LOG2_MAX_NUM_COLUMNS_MINUS1+1)) )
1626  {
1627    printf( "The number of columns is larger than the maximum allowed number of columns.\n" );
1628    exit( EXIT_FAILURE );
1629  }
1630
1631  if( m_iNumColumnsMinus1 >= iWidthInCU )
1632  {
1633    printf( "The current picture can not have so many columns.\n" );
1634    exit( EXIT_FAILURE );
1635  }
1636
1637  if( m_iNumColumnsMinus1 && !m_tileUniformSpacingFlag )
1638  {
1639    for(Int i=0; i<m_iNumColumnsMinus1; i++)
1640    {
1641      uiCummulativeColumnWidth += m_tileColumnWidth[i];
1642    }
1643
1644    if( uiCummulativeColumnWidth >= iWidthInCU )
1645    {
1646      printf( "The width of the column is too large.\n" );
1647      exit( EXIT_FAILURE );
1648    }
1649  }
1650
1651  //check the row relative parameters
1652  if( m_iNumRowsMinus1 >= (1<<(LOG2_MAX_NUM_ROWS_MINUS1+1)) )
1653  {
1654    printf( "The number of rows is larger than the maximum allowed number of rows.\n" );
1655    exit( EXIT_FAILURE );
1656  }
1657
1658  if( m_iNumRowsMinus1 >= iHeightInCU )
1659  {
1660    printf( "The current picture can not have so many rows.\n" );
1661    exit( EXIT_FAILURE );
1662  }
1663
1664  if( m_iNumRowsMinus1 && !m_tileUniformSpacingFlag )
1665  {
1666    for(Int i=0; i<m_iNumRowsMinus1; i++)
1667      uiCummulativeRowHeight += m_tileRowHeight[i];
1668
1669    if( uiCummulativeRowHeight >= iHeightInCU )
1670    {
1671      printf( "The height of the row is too large.\n" );
1672      exit( EXIT_FAILURE );
1673    }
1674  }
1675}
1676
1677#if SVC_EXTENSION
1678#if VPS_EXTN_DIRECT_REF_LAYERS
1679TEncTop* TEncTop::getRefLayerEnc( UInt refLayerIdc )
1680{
1681  if( m_ppcTEncTop[m_layerId]->getNumDirectRefLayers() <= 0 )
1682  {
1683    return (TEncTop *)getLayerEnc( 0 );
1684  }
1685
1686  return (TEncTop *)getLayerEnc( m_cVPS.getRefLayerId( m_layerId, refLayerIdc ) );
1687}
1688#endif
1689
1690#if !REPN_FORMAT_IN_VPS
1691Void TEncTop::xInitILRP()
1692{
1693  if(m_layerId>0)
1694  {
1695    g_bitDepthY     = m_cSPS.getBitDepthY();
1696    g_bitDepthC     = m_cSPS.getBitDepthC();
1697    g_uiMaxCUWidth  = m_cSPS.getMaxCUWidth();
1698    g_uiMaxCUHeight = m_cSPS.getMaxCUHeight();
1699    g_uiMaxCUDepth  = m_cSPS.getMaxCUDepth();
1700    g_uiAddCUDepth  = max (0, m_cSPS.getLog2MinCodingBlockSize() - (Int)m_cSPS.getQuadtreeTULog2MinSize() );
1701
1702    Int  numReorderPics[MAX_TLAYER];
1703    Window &conformanceWindow = m_cSPS.getConformanceWindow();
1704    Window defaultDisplayWindow = m_cSPS.getVuiParametersPresentFlag() ? m_cSPS.getVuiParameters()->getDefaultDisplayWindow() : Window();
1705
1706    if (m_cIlpPic[0] == NULL)
1707    {
1708      for (Int j=0; j < m_numLayer; j++) // consider to set to NumDirectRefLayers[LayerIdInVps[nuh_layer_id]]
1709      {
1710        m_cIlpPic[j] = new  TComPic;
1711#if AUXILIARY_PICTURES
1712        m_cIlpPic[j]->create(m_iSourceWidth, m_iSourceHeight, m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
1713#else
1714        m_cIlpPic[j]->create(m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
1715#endif
1716        for (Int i=0; i<m_cIlpPic[j]->getPicSym()->getNumberOfCUsInFrame(); i++)
1717        {
1718          m_cIlpPic[j]->getPicSym()->getCU(i)->initCU(m_cIlpPic[j], i);
1719        }
1720      }
1721    }
1722  }
1723}
1724#else
1725Void TEncTop::xInitILRP()
1726{
1727#if O0096_REP_FORMAT_INDEX
1728  RepFormat *repFormat = m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( m_cSPS.getUpdateRepFormatFlag() ? m_cSPS.getUpdateRepFormatIndex() : m_layerId ) );
1729#else
1730  RepFormat *repFormat = m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( m_layerId ) );
1731#endif
1732  Int bitDepthY,bitDepthC,picWidth,picHeight;
1733
1734#if O0096_REP_FORMAT_INDEX
1735  bitDepthY   = repFormat->getBitDepthVpsLuma();
1736  bitDepthC   = repFormat->getBitDepthVpsChroma();
1737  picWidth    = repFormat->getPicWidthVpsInLumaSamples();
1738  picHeight   = repFormat->getPicHeightVpsInLumaSamples();
1739#else
1740  if( m_cSPS.getUpdateRepFormatFlag() )
1741  {
1742    bitDepthY   = m_cSPS.getBitDepthY();
1743    bitDepthC   = m_cSPS.getBitDepthC();
1744    picWidth    = m_cSPS.getPicWidthInLumaSamples();
1745    picHeight   = m_cSPS.getPicHeightInLumaSamples();
1746  }
1747  else
1748  {
1749    bitDepthY   = repFormat->getBitDepthVpsLuma();
1750    bitDepthC   = repFormat->getBitDepthVpsChroma();
1751    picWidth    = repFormat->getPicWidthVpsInLumaSamples();
1752    picHeight   = repFormat->getPicHeightVpsInLumaSamples();
1753  }
1754#endif
1755 
1756  if(m_layerId > 0)
1757  {
1758    g_bitDepthY     = bitDepthY;
1759    g_bitDepthC     = bitDepthC;
1760    g_uiMaxCUWidth  = m_cSPS.getMaxCUWidth();
1761    g_uiMaxCUHeight = m_cSPS.getMaxCUHeight();
1762    g_uiMaxCUDepth  = m_cSPS.getMaxCUDepth();
1763    g_uiAddCUDepth  = max (0, m_cSPS.getLog2MinCodingBlockSize() - (Int)m_cSPS.getQuadtreeTULog2MinSize() );
1764
1765    Int  numReorderPics[MAX_TLAYER];
1766#if R0156_CONF_WINDOW_IN_REP_FORMAT
1767    Window &conformanceWindow = repFormat->getConformanceWindowVps();
1768#else
1769    Window &conformanceWindow = m_cSPS.getConformanceWindow();
1770#endif
1771    Window defaultDisplayWindow = m_cSPS.getVuiParametersPresentFlag() ? m_cSPS.getVuiParameters()->getDefaultDisplayWindow() : Window();
1772
1773    if (m_cIlpPic[0] == NULL)
1774    {
1775      for (Int j=0; j < m_numDirectRefLayers; j++)
1776      {
1777        m_cIlpPic[j] = new  TComPic;
1778#if AUXILIARY_PICTURES
1779        m_cIlpPic[j]->create(picWidth, picHeight, m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
1780#else
1781        m_cIlpPic[j]->create(picWidth, picHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
1782#endif
1783        for (Int i=0; i<m_cIlpPic[j]->getPicSym()->getNumberOfCUsInFrame(); i++)
1784        {
1785          m_cIlpPic[j]->getPicSym()->getCU(i)->initCU(m_cIlpPic[j], i);
1786        }
1787      }
1788    }
1789  }
1790}
1791#endif
1792
1793#if O0098_SCALED_REF_LAYER_ID
1794Window& TEncTop::getScaledRefLayerWindowForLayer(Int layerId)
1795{
1796  static Window win;
1797
1798  for (Int i = 0; i < m_numScaledRefLayerOffsets; i++)
1799  {
1800    if (layerId == m_scaledRefLayerId[i])
1801    {
1802      return m_scaledRefLayerWindow[i];
1803    }
1804  }
1805
1806  win.resetWindow();  // scaled reference layer offsets are inferred to be zero when not present
1807  return win;
1808}
1809#if REF_REGION_OFFSET
1810Window& TEncTop::getRefLayerWindowForLayer(Int layerId)
1811{
1812  static Window win;
1813
1814  for (Int i = 0; i < m_numScaledRefLayerOffsets; i++)
1815  {
1816    if (layerId == m_refLayerId[i])
1817    {
1818      return m_refLayerWindow[i];
1819    }
1820  }
1821
1822  win.resetWindow();  // reference offsets are inferred to be zero when not present
1823  return win;
1824}
1825#endif
1826#endif
1827
1828#endif //SVC_EXTENSION
1829//! \}
Note: See TracBrowser for help on using the repository browser.