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

Last change on this file since 963 was 934, checked in by seregin, 10 years ago

fix for the ticket #41

  • 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#if SVC_EXTENSION
1125    assert(i < MAX_TLAYER);
1126#endif
1127    m_cSPS.setMaxDecPicBuffering(m_maxDecPicBuffering[i], i);
1128    m_cSPS.setNumReorderPics(m_numReorderPics[i], i);
1129  }
1130  m_cSPS.setPCMBitDepthLuma (g_uiPCMBitDepthLuma);
1131  m_cSPS.setPCMBitDepthChroma (g_uiPCMBitDepthChroma);
1132  m_cSPS.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag );
1133
1134  m_cSPS.setScalingListFlag ( (m_useScalingListId == 0) ? 0 : 1 );
1135
1136  m_cSPS.setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
1137
1138  m_cSPS.setVuiParametersPresentFlag(getVuiParametersPresentFlag());
1139  if (m_cSPS.getVuiParametersPresentFlag())
1140  {
1141    TComVUI* pcVUI = m_cSPS.getVuiParameters();
1142    pcVUI->setAspectRatioInfoPresentFlag(getAspectRatioInfoPresentFlag());
1143    pcVUI->setAspectRatioIdc(getAspectRatioIdc());
1144    pcVUI->setSarWidth(getSarWidth());
1145    pcVUI->setSarHeight(getSarHeight());
1146    pcVUI->setOverscanInfoPresentFlag(getOverscanInfoPresentFlag());
1147    pcVUI->setOverscanAppropriateFlag(getOverscanAppropriateFlag());
1148    pcVUI->setVideoSignalTypePresentFlag(getVideoSignalTypePresentFlag());
1149    pcVUI->setVideoFormat(getVideoFormat());
1150    pcVUI->setVideoFullRangeFlag(getVideoFullRangeFlag());
1151    pcVUI->setColourDescriptionPresentFlag(getColourDescriptionPresentFlag());
1152    pcVUI->setColourPrimaries(getColourPrimaries());
1153    pcVUI->setTransferCharacteristics(getTransferCharacteristics());
1154    pcVUI->setMatrixCoefficients(getMatrixCoefficients());
1155    pcVUI->setChromaLocInfoPresentFlag(getChromaLocInfoPresentFlag());
1156    pcVUI->setChromaSampleLocTypeTopField(getChromaSampleLocTypeTopField());
1157    pcVUI->setChromaSampleLocTypeBottomField(getChromaSampleLocTypeBottomField());
1158    pcVUI->setNeutralChromaIndicationFlag(getNeutralChromaIndicationFlag());
1159    pcVUI->setDefaultDisplayWindow(getDefaultDisplayWindow());
1160    pcVUI->setFrameFieldInfoPresentFlag(getFrameFieldInfoPresentFlag());
1161    pcVUI->setFieldSeqFlag(false);
1162    pcVUI->setHrdParametersPresentFlag(false);
1163    pcVUI->getTimingInfo()->setPocProportionalToTimingFlag(getPocProportionalToTimingFlag());
1164    pcVUI->getTimingInfo()->setNumTicksPocDiffOneMinus1   (getNumTicksPocDiffOneMinus1()   );
1165    pcVUI->setBitstreamRestrictionFlag(getBitstreamRestrictionFlag());
1166    pcVUI->setTilesFixedStructureFlag(getTilesFixedStructureFlag());
1167    pcVUI->setMotionVectorsOverPicBoundariesFlag(getMotionVectorsOverPicBoundariesFlag());
1168    pcVUI->setMinSpatialSegmentationIdc(getMinSpatialSegmentationIdc());
1169    pcVUI->setMaxBytesPerPicDenom(getMaxBytesPerPicDenom());
1170    pcVUI->setMaxBitsPerMinCuDenom(getMaxBitsPerMinCuDenom());
1171    pcVUI->setLog2MaxMvLengthHorizontal(getLog2MaxMvLengthHorizontal());
1172    pcVUI->setLog2MaxMvLengthVertical(getLog2MaxMvLengthVertical());
1173  }
1174}
1175
1176Void TEncTop::xInitPPS()
1177{
1178  m_cPPS.setConstrainedIntraPred( m_bUseConstrainedIntraPred );
1179  Bool bUseDQP = (getMaxCuDQPDepth() > 0)? true : false;
1180
1181  if((getMaxDeltaQP() != 0 )|| getUseAdaptiveQP())
1182  {
1183    bUseDQP = true;
1184  }
1185
1186  if(bUseDQP)
1187  {
1188    m_cPPS.setUseDQP(true);
1189    m_cPPS.setMaxCuDQPDepth( m_iMaxCuDQPDepth );
1190    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
1191  }
1192  else
1193  {
1194    m_cPPS.setUseDQP(false);
1195    m_cPPS.setMaxCuDQPDepth( 0 );
1196    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
1197  }
1198
1199  if ( m_RCEnableRateControl )
1200  {
1201    m_cPPS.setUseDQP(true);
1202    m_cPPS.setMaxCuDQPDepth( 0 );
1203    m_cPPS.setMinCuDQPSize( m_cPPS.getSPS()->getMaxCUWidth() >> ( m_cPPS.getMaxCuDQPDepth()) );
1204  } 
1205
1206  m_cPPS.setChromaCbQpOffset( m_chromaCbQpOffset );
1207  m_cPPS.setChromaCrQpOffset( m_chromaCrQpOffset );
1208
1209  m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams);
1210  m_cPPS.setEntropyCodingSyncEnabledFlag( m_iWaveFrontSynchro > 0 );
1211  m_cPPS.setTilesEnabledFlag( (m_iNumColumnsMinus1 > 0 || m_iNumRowsMinus1 > 0) );
1212  m_cPPS.setUseWP( m_useWeightedPred );
1213  m_cPPS.setWPBiPred( m_useWeightedBiPred );
1214  m_cPPS.setOutputFlagPresentFlag( false );
1215  m_cPPS.setSignHideFlag(getSignHideFlag());
1216  if ( getDeblockingFilterMetric() )
1217  {
1218    m_cPPS.setDeblockingFilterControlPresentFlag (true);
1219    m_cPPS.setDeblockingFilterOverrideEnabledFlag(true);
1220    m_cPPS.setPicDisableDeblockingFilterFlag(false);
1221    m_cPPS.setDeblockingFilterBetaOffsetDiv2(0);
1222    m_cPPS.setDeblockingFilterTcOffsetDiv2(0);
1223  } 
1224  else
1225  {
1226  m_cPPS.setDeblockingFilterControlPresentFlag (m_DeblockingFilterControlPresent );
1227  }
1228  m_cPPS.setLog2ParallelMergeLevelMinus2   (m_log2ParallelMergeLevelMinus2 );
1229  m_cPPS.setCabacInitPresentFlag(CABAC_INIT_PRESENT_FLAG);
1230  m_cPPS.setLoopFilterAcrossSlicesEnabledFlag( m_bLFCrossSliceBoundaryFlag );
1231  Int histogram[MAX_NUM_REF + 1];
1232  for( Int i = 0; i <= MAX_NUM_REF; i++ )
1233  {
1234    histogram[i]=0;
1235  }
1236  for( Int i = 0; i < getGOPSize(); i++ )
1237  {
1238    assert(getGOPEntry(i).m_numRefPicsActive >= 0 && getGOPEntry(i).m_numRefPicsActive <= MAX_NUM_REF);
1239    histogram[getGOPEntry(i).m_numRefPicsActive]++;
1240  }
1241  Int maxHist=-1;
1242  Int bestPos=0;
1243  for( Int i = 0; i <= MAX_NUM_REF; i++ )
1244  {
1245    if(histogram[i]>maxHist)
1246    {
1247      maxHist=histogram[i];
1248      bestPos=i;
1249    }
1250  }
1251  assert(bestPos <= 15);
1252  m_cPPS.setNumRefIdxL0DefaultActive(bestPos);
1253  m_cPPS.setNumRefIdxL1DefaultActive(bestPos);
1254  m_cPPS.setTransquantBypassEnableFlag(getTransquantBypassEnableFlag());
1255  m_cPPS.setUseTransformSkip( m_useTransformSkip );
1256  if (m_sliceSegmentMode)
1257  {
1258    m_cPPS.setDependentSliceSegmentsEnabledFlag( true );
1259  }
1260  if( m_cPPS.getDependentSliceSegmentsEnabledFlag() )
1261  {
1262    Int NumCtx = m_cPPS.getEntropyCodingSyncEnabledFlag()?2:1;
1263    m_cSliceEncoder.initCtxMem( NumCtx );
1264    for ( UInt st = 0; st < NumCtx; st++ )
1265    {
1266      TEncSbac* ctx = NULL;
1267      ctx = new TEncSbac;
1268      ctx->init( &m_cBinCoderCABAC );
1269      m_cSliceEncoder.setCtxMem( ctx, st );
1270    }
1271  }
1272#if SVC_EXTENSION
1273#if SCALINGLIST_INFERRING
1274  m_cPPS.setLayerId( m_layerId );
1275#endif
1276
1277#if Q0078_ADD_LAYER_SETS
1278  if( !m_numDirectRefLayers && m_numAddLayerSets )
1279  {
1280    m_cPPS.setLayerId(0); // layer ID 0 for independent layers
1281  }
1282#endif
1283
1284  if( m_layerId > 0 )
1285  {
1286    m_cPPS.setListsModificationPresentFlag(true);
1287    m_cPPS.setExtensionFlag(true);
1288  }
1289  else
1290  {
1291    m_cPPS.setListsModificationPresentFlag(false);
1292    m_cPPS.setExtensionFlag(false);
1293  }
1294
1295  m_cPPS.setPPSId( m_iPPSIdCnt );
1296  m_cPPS.setSPSId( m_iSPSIdCnt );
1297#if POC_RESET_FLAG
1298  m_cPPS.setNumExtraSliceHeaderBits( 2 );
1299#endif
1300#if O0149_CROSS_LAYER_BLA_FLAG
1301  if (m_crossLayerBLAFlag)
1302  {
1303    m_cPPS.setNumExtraSliceHeaderBits( 3 );
1304  }
1305#endif
1306#if MOVE_SCALED_OFFSET_TO_PPS
1307  m_cPPS.setNumScaledRefLayerOffsets(m_numScaledRefLayerOffsets);
1308  for(Int i = 0; i < m_cPPS.getNumScaledRefLayerOffsets(); i++)
1309  {
1310#if O0098_SCALED_REF_LAYER_ID
1311    m_cPPS.setScaledRefLayerId(i, m_scaledRefLayerId[i]);
1312#endif
1313    m_cPPS.getScaledRefLayerWindow(i) = m_scaledRefLayerWindow[i];
1314#if REF_REGION_OFFSET
1315    m_cPPS.getRefLayerWindow(i) = m_refLayerWindow[i];
1316    m_cPPS.setScaledRefLayerOffsetPresentFlag( i, m_scaledRefLayerOffsetPresentFlag[i] );
1317    m_cPPS.setRefRegionOffsetPresentFlag( i, m_refRegionOffsetPresentFlag[i] );
1318#endif
1319#if R0209_GENERIC_PHASE
1320    m_cPPS.setResamplePhaseSetPresentFlag( i, m_resamplePhaseSetPresentFlag[i] );
1321    m_cPPS.setPhaseHorLuma( m_scaledRefLayerId[i], m_phaseHorLuma[i] );
1322    m_cPPS.setPhaseVerLuma( m_scaledRefLayerId[i], m_phaseVerLuma[i] );
1323    m_cPPS.setPhaseHorChroma( m_scaledRefLayerId[i], m_phaseHorChroma[i] );
1324    m_cPPS.setPhaseVerChroma( m_scaledRefLayerId[i], m_phaseVerChroma[i] );
1325#endif
1326#if P0312_VERT_PHASE_ADJ
1327    m_cPPS.setVertPhasePositionEnableFlag( m_scaledRefLayerId[i], m_scaledRefLayerWindow[i].getVertPhasePositionEnableFlag() );
1328#endif
1329  }
1330#endif
1331#if Q0048_CGS_3D_ASYMLUT
1332  m_cPPS.setCGSFlag( m_nCGSFlag );
1333#endif
1334#if POC_RESET_IDC_ENCODER
1335  m_cPPS.setPocResetInfoPresentFlag( true );
1336  m_cPPS.setExtensionFlag( true );
1337  m_cPPS.setSliceHeaderExtensionPresentFlag( true );
1338#endif
1339#endif //SVC_EXTENSION
1340}
1341
1342//Function for initializing m_RPSList, a list of TComReferencePictureSet, based on the GOPEntry objects read from the config file.
1343Void TEncTop::xInitRPS(Bool isFieldCoding)
1344{
1345  TComReferencePictureSet*      rps;
1346 
1347  m_cSPS.createRPSList(getGOPSize()+m_extraRPSs+1);
1348  TComRPSList* rpsList = m_cSPS.getRPSList();
1349
1350  for( Int i = 0; i < getGOPSize()+m_extraRPSs; i++) 
1351  {
1352    GOPEntry ge = getGOPEntry(i);
1353    rps = rpsList->getReferencePictureSet(i);
1354    rps->setNumberOfPictures(ge.m_numRefPics);
1355    rps->setNumRefIdc(ge.m_numRefIdc);
1356    Int numNeg = 0;
1357    Int numPos = 0;
1358    for( Int j = 0; j < ge.m_numRefPics; j++)
1359    {
1360      rps->setDeltaPOC(j,ge.m_referencePics[j]);
1361      rps->setUsed(j,ge.m_usedByCurrPic[j]);
1362      if(ge.m_referencePics[j]>0)
1363      {
1364        numPos++;
1365      }
1366      else
1367      {
1368        numNeg++;
1369      }
1370    }
1371    rps->setNumberOfNegativePictures(numNeg);
1372    rps->setNumberOfPositivePictures(numPos);
1373
1374    // handle inter RPS intialization from the config file.
1375#if AUTO_INTER_RPS
1376    rps->setInterRPSPrediction(ge.m_interRPSPrediction > 0);  // not very clean, converting anything > 0 to true.
1377    rps->setDeltaRIdxMinus1(0);                               // index to the Reference RPS is always the previous one.
1378    TComReferencePictureSet*     RPSRef = rpsList->getReferencePictureSet(i-1);  // get the reference RPS
1379
1380    if (ge.m_interRPSPrediction == 2)  // Automatic generation of the inter RPS idc based on the RIdx provided.
1381    {
1382      Int deltaRPS = getGOPEntry(i-1).m_POC - ge.m_POC;  // the ref POC - current POC
1383      Int numRefDeltaPOC = RPSRef->getNumberOfPictures();
1384
1385      rps->setDeltaRPS(deltaRPS);           // set delta RPS
1386      rps->setNumRefIdc(numRefDeltaPOC+1);  // set the numRefIdc to the number of pictures in the reference RPS + 1.
1387      Int count=0;
1388      for (Int j = 0; j <= numRefDeltaPOC; j++ ) // cycle through pics in reference RPS.
1389      {
1390        Int RefDeltaPOC = (j<numRefDeltaPOC)? RPSRef->getDeltaPOC(j): 0;  // if it is the last decoded picture, set RefDeltaPOC = 0
1391        rps->setRefIdc(j, 0);
1392        for (Int k = 0; k < rps->getNumberOfPictures(); k++ )  // cycle through pics in current RPS.
1393        {
1394          if (rps->getDeltaPOC(k) == ( RefDeltaPOC + deltaRPS))  // if the current RPS has a same picture as the reference RPS.
1395          {
1396              rps->setRefIdc(j, (rps->getUsed(k)?1:2));
1397              count++;
1398              break;
1399          }
1400        }
1401      }
1402      if (count != rps->getNumberOfPictures())
1403      {
1404        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");
1405        rps->setInterRPSPrediction(0);
1406      }
1407    }
1408    else if (ge.m_interRPSPrediction == 1)  // inter RPS idc based on the RefIdc values provided in config file.
1409    {
1410      rps->setDeltaRPS(ge.m_deltaRPS);
1411      rps->setNumRefIdc(ge.m_numRefIdc);
1412      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1413      {
1414        rps->setRefIdc(j, ge.m_refIdc[j]);
1415      }
1416#if WRITE_BACK
1417      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1418      // computed from the RefIdc.  A warning is printed if they are not identical.
1419      numNeg = 0;
1420      numPos = 0;
1421      TComReferencePictureSet      RPSTemp;  // temporary variable
1422
1423      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1424      {
1425        if (ge.m_refIdc[j])
1426        {
1427          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1428          RPSTemp.setDeltaPOC((numNeg+numPos),deltaPOC);
1429          RPSTemp.setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1430          if (deltaPOC<0)
1431          {
1432            numNeg++;
1433          }
1434          else
1435          {
1436            numPos++;
1437          }
1438        }
1439      }
1440      if (numNeg != rps->getNumberOfNegativePictures())
1441      {
1442        printf("Warning: number of negative pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1443        rps->setNumberOfNegativePictures(numNeg);
1444        rps->setNumberOfPictures(numNeg+numPos);
1445      }
1446      if (numPos != rps->getNumberOfPositivePictures())
1447      {
1448        printf("Warning: number of positive pictures in RPS is different between intra and inter RPS specified in the config file.\n");
1449        rps->setNumberOfPositivePictures(numPos);
1450        rps->setNumberOfPictures(numNeg+numPos);
1451      }
1452      RPSTemp.setNumberOfPictures(numNeg+numPos);
1453      RPSTemp.setNumberOfNegativePictures(numNeg);
1454      RPSTemp.sortDeltaPOC();     // sort the created delta POC before comparing
1455      // check if Delta POC and Used are the same
1456      // print warning if they are not.
1457      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1458      {
1459        if (RPSTemp.getDeltaPOC(j) != rps->getDeltaPOC(j))
1460        {
1461          printf("Warning: delta POC is different between intra RPS and inter RPS specified in the config file.\n");
1462          rps->setDeltaPOC(j,RPSTemp.getDeltaPOC(j));
1463        }
1464        if (RPSTemp.getUsed(j) != rps->getUsed(j))
1465        {
1466          printf("Warning: Used by Current in RPS is different between intra and inter RPS specified in the config file.\n");
1467          rps->setUsed(j,RPSTemp.getUsed(j));
1468        }
1469      }
1470#endif
1471    }
1472#else
1473    rps->setInterRPSPrediction(ge.m_interRPSPrediction);
1474    if (ge.m_interRPSPrediction)
1475    {
1476      rps->setDeltaRIdxMinus1(0);
1477      rps->setDeltaRPS(ge.m_deltaRPS);
1478      rps->setNumRefIdc(ge.m_numRefIdc);
1479      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1480      {
1481        rps->setRefIdc(j, ge.m_refIdc[j]);
1482      }
1483#if WRITE_BACK
1484      // the folowing code overwrite the deltaPOC and Used by current values read from the config file with the ones
1485      // computed from the RefIdc.  This is not necessary if both are identical. Currently there is no check to see if they are identical.
1486      numNeg = 0;
1487      numPos = 0;
1488      TComReferencePictureSet*     RPSRef = m_RPSList.getReferencePictureSet(i-1);
1489
1490      for (Int j = 0; j < ge.m_numRefIdc; j++ )
1491      {
1492        if (ge.m_refIdc[j])
1493        {
1494          Int deltaPOC = ge.m_deltaRPS + ((j < RPSRef->getNumberOfPictures())? RPSRef->getDeltaPOC(j) : 0);
1495          rps->setDeltaPOC((numNeg+numPos),deltaPOC);
1496          rps->setUsed((numNeg+numPos),ge.m_refIdc[j]==1?1:0);
1497          if (deltaPOC<0)
1498          {
1499            numNeg++;
1500          }
1501          else
1502          {
1503            numPos++;
1504          }
1505        }
1506      }
1507      rps->setNumberOfNegativePictures(numNeg);
1508      rps->setNumberOfPositivePictures(numPos);
1509      rps->sortDeltaPOC();
1510#endif
1511    }
1512#endif //INTER_RPS_AUTO
1513  }
1514  //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.
1515  //The position = GOPSize + extraRPSs which is (a priori) unused is reserved for this field in the RPS.
1516  if (isFieldCoding) 
1517  {
1518    rps = rpsList->getReferencePictureSet(getGOPSize()+m_extraRPSs);
1519    rps->setNumberOfPictures(1);
1520    rps->setNumberOfNegativePictures(1);
1521    rps->setNumberOfPositivePictures(0);
1522    rps->setNumberOfLongtermPictures(0);
1523    rps->setDeltaPOC(0,-1);
1524    rps->setPOC(0,0);
1525    rps->setUsed(0,true);
1526    rps->setInterRPSPrediction(false);
1527    rps->setDeltaRIdxMinus1(0);
1528    rps->setDeltaRPS(0);
1529    rps->setNumRefIdc(0);
1530}
1531}
1532
1533   // This is a function that
1534   // determines what Reference Picture Set to use
1535   // for a specific slice (with POC = POCCurr)
1536Void TEncTop::selectReferencePictureSet(TComSlice* slice, Int POCCurr, Int GOPid )
1537{
1538  slice->setRPSidx(GOPid);
1539  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1540  {   
1541    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1542    {
1543      Int POCIndex = POCCurr%m_uiIntraPeriod;
1544      if(POCIndex == 0)
1545      {
1546        POCIndex = m_uiIntraPeriod;
1547      }
1548      if(POCIndex == m_GOPList[extraNum].m_POC)
1549      {
1550        slice->setRPSidx(extraNum);
1551      }
1552    }
1553    else
1554    {
1555      if(POCCurr==m_GOPList[extraNum].m_POC)
1556      {
1557        slice->setRPSidx(extraNum);
1558      }
1559    }
1560  }
1561
1562  if(POCCurr == 1 && slice->getPic()->isField())
1563  {
1564    slice->setRPSidx(m_iGOPSize+m_extraRPSs);
1565  }
1566
1567  slice->setRPS(getSPS()->getRPSList()->getReferencePictureSet(slice->getRPSidx()));
1568  slice->getRPS()->setNumberOfPictures(slice->getRPS()->getNumberOfNegativePictures()+slice->getRPS()->getNumberOfPositivePictures());
1569}
1570
1571Int TEncTop::getReferencePictureSetIdxForSOP(TComSlice* slice, Int POCCurr, Int GOPid )
1572{
1573  int rpsIdx = GOPid;
1574
1575  for(Int extraNum=m_iGOPSize; extraNum<m_extraRPSs+m_iGOPSize; extraNum++)
1576  {   
1577    if(m_uiIntraPeriod > 0 && getDecodingRefreshType() > 0)
1578    {
1579      Int POCIndex = POCCurr%m_uiIntraPeriod;
1580      if(POCIndex == 0)
1581      {
1582        POCIndex = m_uiIntraPeriod;
1583      }
1584      if(POCIndex == m_GOPList[extraNum].m_POC)
1585      {
1586        rpsIdx = extraNum;
1587      }
1588    }
1589    else
1590    {
1591      if(POCCurr==m_GOPList[extraNum].m_POC)
1592      {
1593        rpsIdx = extraNum;
1594      }
1595    }
1596  }
1597
1598  return rpsIdx;
1599}
1600
1601Void  TEncTop::xInitPPSforTiles()
1602{
1603  m_cPPS.setTileUniformSpacingFlag( m_tileUniformSpacingFlag );
1604  m_cPPS.setNumTileColumnsMinus1( m_iNumColumnsMinus1 );
1605  m_cPPS.setNumTileRowsMinus1( m_iNumRowsMinus1 );
1606  if( !m_tileUniformSpacingFlag )
1607  {
1608    m_cPPS.setTileColumnWidth( m_tileColumnWidth );
1609    m_cPPS.setTileRowHeight( m_tileRowHeight );
1610  }
1611  m_cPPS.setLoopFilterAcrossTilesEnabledFlag( m_loopFilterAcrossTilesEnabledFlag );
1612
1613  // # substreams is "per tile" when tiles are independent.
1614  if (m_iWaveFrontSynchro )
1615  {
1616    m_cPPS.setNumSubstreams(m_iWaveFrontSubstreams * (m_iNumColumnsMinus1+1));
1617  }
1618}
1619
1620Void  TEncCfg::xCheckGSParameters()
1621{
1622  Int   iWidthInCU = ( m_iSourceWidth%g_uiMaxCUWidth ) ? m_iSourceWidth/g_uiMaxCUWidth + 1 : m_iSourceWidth/g_uiMaxCUWidth;
1623  Int   iHeightInCU = ( m_iSourceHeight%g_uiMaxCUHeight ) ? m_iSourceHeight/g_uiMaxCUHeight + 1 : m_iSourceHeight/g_uiMaxCUHeight;
1624  UInt  uiCummulativeColumnWidth = 0;
1625  UInt  uiCummulativeRowHeight = 0;
1626
1627  //check the column relative parameters
1628  if( m_iNumColumnsMinus1 >= (1<<(LOG2_MAX_NUM_COLUMNS_MINUS1+1)) )
1629  {
1630    printf( "The number of columns is larger than the maximum allowed number of columns.\n" );
1631    exit( EXIT_FAILURE );
1632  }
1633
1634  if( m_iNumColumnsMinus1 >= iWidthInCU )
1635  {
1636    printf( "The current picture can not have so many columns.\n" );
1637    exit( EXIT_FAILURE );
1638  }
1639
1640  if( m_iNumColumnsMinus1 && !m_tileUniformSpacingFlag )
1641  {
1642    for(Int i=0; i<m_iNumColumnsMinus1; i++)
1643    {
1644      uiCummulativeColumnWidth += m_tileColumnWidth[i];
1645    }
1646
1647    if( uiCummulativeColumnWidth >= iWidthInCU )
1648    {
1649      printf( "The width of the column is too large.\n" );
1650      exit( EXIT_FAILURE );
1651    }
1652  }
1653
1654  //check the row relative parameters
1655  if( m_iNumRowsMinus1 >= (1<<(LOG2_MAX_NUM_ROWS_MINUS1+1)) )
1656  {
1657    printf( "The number of rows is larger than the maximum allowed number of rows.\n" );
1658    exit( EXIT_FAILURE );
1659  }
1660
1661  if( m_iNumRowsMinus1 >= iHeightInCU )
1662  {
1663    printf( "The current picture can not have so many rows.\n" );
1664    exit( EXIT_FAILURE );
1665  }
1666
1667  if( m_iNumRowsMinus1 && !m_tileUniformSpacingFlag )
1668  {
1669    for(Int i=0; i<m_iNumRowsMinus1; i++)
1670      uiCummulativeRowHeight += m_tileRowHeight[i];
1671
1672    if( uiCummulativeRowHeight >= iHeightInCU )
1673    {
1674      printf( "The height of the row is too large.\n" );
1675      exit( EXIT_FAILURE );
1676    }
1677  }
1678}
1679
1680#if SVC_EXTENSION
1681#if VPS_EXTN_DIRECT_REF_LAYERS
1682TEncTop* TEncTop::getRefLayerEnc( UInt refLayerIdc )
1683{
1684  if( m_ppcTEncTop[m_layerId]->getNumDirectRefLayers() <= 0 )
1685  {
1686    return (TEncTop *)getLayerEnc( 0 );
1687  }
1688
1689  return (TEncTop *)getLayerEnc( m_cVPS.getRefLayerId( m_layerId, refLayerIdc ) );
1690}
1691#endif
1692
1693#if !REPN_FORMAT_IN_VPS
1694Void TEncTop::xInitILRP()
1695{
1696  if(m_layerId>0)
1697  {
1698    g_bitDepthY     = m_cSPS.getBitDepthY();
1699    g_bitDepthC     = m_cSPS.getBitDepthC();
1700    g_uiMaxCUWidth  = m_cSPS.getMaxCUWidth();
1701    g_uiMaxCUHeight = m_cSPS.getMaxCUHeight();
1702    g_uiMaxCUDepth  = m_cSPS.getMaxCUDepth();
1703    g_uiAddCUDepth  = max (0, m_cSPS.getLog2MinCodingBlockSize() - (Int)m_cSPS.getQuadtreeTULog2MinSize() );
1704
1705    Int  numReorderPics[MAX_TLAYER];
1706    Window &conformanceWindow = m_cSPS.getConformanceWindow();
1707    Window defaultDisplayWindow = m_cSPS.getVuiParametersPresentFlag() ? m_cSPS.getVuiParameters()->getDefaultDisplayWindow() : Window();
1708
1709    if (m_cIlpPic[0] == NULL)
1710    {
1711      for (Int j=0; j < m_numLayer; j++) // consider to set to NumDirectRefLayers[LayerIdInVps[nuh_layer_id]]
1712      {
1713        m_cIlpPic[j] = new  TComPic;
1714#if AUXILIARY_PICTURES
1715        m_cIlpPic[j]->create(m_iSourceWidth, m_iSourceHeight, m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
1716#else
1717        m_cIlpPic[j]->create(m_iSourceWidth, m_iSourceHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
1718#endif
1719        for (Int i=0; i<m_cIlpPic[j]->getPicSym()->getNumberOfCUsInFrame(); i++)
1720        {
1721          m_cIlpPic[j]->getPicSym()->getCU(i)->initCU(m_cIlpPic[j], i);
1722        }
1723      }
1724    }
1725  }
1726}
1727#else
1728Void TEncTop::xInitILRP()
1729{
1730#if O0096_REP_FORMAT_INDEX
1731  RepFormat *repFormat = m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( m_cSPS.getUpdateRepFormatFlag() ? m_cSPS.getUpdateRepFormatIndex() : m_layerId ) );
1732#else
1733  RepFormat *repFormat = m_cVPS.getVpsRepFormat( m_cVPS.getVpsRepFormatIdx( m_layerId ) );
1734#endif
1735  Int bitDepthY,bitDepthC,picWidth,picHeight;
1736
1737#if O0096_REP_FORMAT_INDEX
1738  bitDepthY   = repFormat->getBitDepthVpsLuma();
1739  bitDepthC   = repFormat->getBitDepthVpsChroma();
1740  picWidth    = repFormat->getPicWidthVpsInLumaSamples();
1741  picHeight   = repFormat->getPicHeightVpsInLumaSamples();
1742#else
1743  if( m_cSPS.getUpdateRepFormatFlag() )
1744  {
1745    bitDepthY   = m_cSPS.getBitDepthY();
1746    bitDepthC   = m_cSPS.getBitDepthC();
1747    picWidth    = m_cSPS.getPicWidthInLumaSamples();
1748    picHeight   = m_cSPS.getPicHeightInLumaSamples();
1749  }
1750  else
1751  {
1752    bitDepthY   = repFormat->getBitDepthVpsLuma();
1753    bitDepthC   = repFormat->getBitDepthVpsChroma();
1754    picWidth    = repFormat->getPicWidthVpsInLumaSamples();
1755    picHeight   = repFormat->getPicHeightVpsInLumaSamples();
1756  }
1757#endif
1758 
1759  if(m_layerId > 0)
1760  {
1761    g_bitDepthY     = bitDepthY;
1762    g_bitDepthC     = bitDepthC;
1763    g_uiMaxCUWidth  = m_cSPS.getMaxCUWidth();
1764    g_uiMaxCUHeight = m_cSPS.getMaxCUHeight();
1765    g_uiMaxCUDepth  = m_cSPS.getMaxCUDepth();
1766    g_uiAddCUDepth  = max (0, m_cSPS.getLog2MinCodingBlockSize() - (Int)m_cSPS.getQuadtreeTULog2MinSize() );
1767
1768    Int  numReorderPics[MAX_TLAYER];
1769#if R0156_CONF_WINDOW_IN_REP_FORMAT
1770    Window &conformanceWindow = repFormat->getConformanceWindowVps();
1771#else
1772    Window &conformanceWindow = m_cSPS.getConformanceWindow();
1773#endif
1774    Window defaultDisplayWindow = m_cSPS.getVuiParametersPresentFlag() ? m_cSPS.getVuiParameters()->getDefaultDisplayWindow() : Window();
1775
1776    if (m_cIlpPic[0] == NULL)
1777    {
1778      for (Int j=0; j < m_numDirectRefLayers; j++)
1779      {
1780        m_cIlpPic[j] = new  TComPic;
1781#if AUXILIARY_PICTURES
1782        m_cIlpPic[j]->create(picWidth, picHeight, m_chromaFormatIDC, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
1783#else
1784        m_cIlpPic[j]->create(picWidth, picHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, &m_cSPS, true);
1785#endif
1786        for (Int i=0; i<m_cIlpPic[j]->getPicSym()->getNumberOfCUsInFrame(); i++)
1787        {
1788          m_cIlpPic[j]->getPicSym()->getCU(i)->initCU(m_cIlpPic[j], i);
1789        }
1790      }
1791    }
1792  }
1793}
1794#endif
1795
1796#if O0098_SCALED_REF_LAYER_ID
1797Window& TEncTop::getScaledRefLayerWindowForLayer(Int layerId)
1798{
1799  static Window win;
1800
1801  for (Int i = 0; i < m_numScaledRefLayerOffsets; i++)
1802  {
1803    if (layerId == m_scaledRefLayerId[i])
1804    {
1805      return m_scaledRefLayerWindow[i];
1806    }
1807  }
1808
1809  win.resetWindow();  // scaled reference layer offsets are inferred to be zero when not present
1810  return win;
1811}
1812#if REF_REGION_OFFSET
1813Window& TEncTop::getRefLayerWindowForLayer(Int layerId)
1814{
1815  static Window win;
1816
1817  for (Int i = 0; i < m_numScaledRefLayerOffsets; i++)
1818  {
1819    if (layerId == m_refLayerId[i])
1820    {
1821      return m_refLayerWindow[i];
1822    }
1823  }
1824
1825  win.resetWindow();  // reference offsets are inferred to be zero when not present
1826  return win;
1827}
1828#endif
1829#endif
1830
1831#endif //SVC_EXTENSION
1832//! \}
Note: See TracBrowser for help on using the repository browser.