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

Last change on this file since 899 was 898, checked in by nokia, 11 years ago

implementation for proposal JCTVC-R0042

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