source: SHVCSoftware/branches/SHM-2.1-multilayers-dev/source/Lib/TLibDecoder/TDecTop.cpp @ 503

Last change on this file since 503 was 267, checked in by interdigital, 12 years ago

fix linux compile warning

File size: 47.0 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2013, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TDecTop.cpp
35    \brief    decoder class
36*/
37
38#include "NALread.h"
39#include "TDecTop.h"
40
41#if SVC_EXTENSION
42UInt  TDecTop::m_prevPOC = MAX_UINT;
43UInt  TDecTop::m_uiPrevLayerId = MAX_UINT;
44Bool  TDecTop::m_bFirstSliceInSequence = true;
45#endif
46
47//! \ingroup TLibDecoder
48//! \{
49
50TDecTop::TDecTop()
51{
52  m_pcPic = 0;
53  m_iMaxRefPicNum = 0;
54#if ENC_DEC_TRACE
55  g_hTrace = fopen( "TraceDec.txt", "wb" );
56  g_bJustDoIt = g_bEncDecTraceDisable;
57  g_nSymbolCounter = 0;
58#endif
59  m_pocCRA = 0;
60  m_prevRAPisBLA = false;
61  m_pocRandomAccess = MAX_INT;         
62#if !SVC_EXTENSION
63  m_prevPOC                = MAX_INT;
64#endif
65  m_bFirstSliceInPicture    = true;
66#if !SVC_EXTENSION
67  m_bFirstSliceInSequence   = true;
68#endif
69#if SVC_EXTENSION
70  m_layerId = 0;
71#if AVC_BASE
72  m_pBLReconFile = NULL;
73#endif
74#endif
75#if REF_IDX_FRAMEWORK
76  memset(m_cIlpPic, 0, sizeof(m_cIlpPic));
77#endif
78#if AVC_SYNTAX || SYNTAX_OUTPUT
79  m_pBLSyntaxFile = NULL;
80#endif
81
82}
83
84TDecTop::~TDecTop()
85{
86#if ENC_DEC_TRACE
87  fclose( g_hTrace );
88#endif
89}
90
91Void TDecTop::create()
92{
93#if SVC_EXTENSION
94  m_cGopDecoder.create( m_layerId );
95#else
96  m_cGopDecoder.create();
97#endif
98  m_apcSlicePilot = new TComSlice;
99  m_uiSliceIdx = 0;
100}
101
102Void TDecTop::destroy()
103{
104  m_cGopDecoder.destroy();
105 
106  delete m_apcSlicePilot;
107  m_apcSlicePilot = NULL;
108 
109  m_cSliceDecoder.destroy();
110#if REF_IDX_FRAMEWORK
111  for(Int i=0; i<MAX_NUM_REF; i++)
112  {
113    if(m_cIlpPic[i])
114    {
115      m_cIlpPic[i]->destroy();
116      delete m_cIlpPic[i];
117      m_cIlpPic[i] = NULL;
118    }
119  }   
120#endif
121}
122
123Void TDecTop::init()
124{
125#if !SVC_EXTENSION
126  // initialize ROM
127  initROM();
128#endif
129#if SVC_EXTENSION
130  m_cGopDecoder.init( m_ppcTDecTop, &m_cEntropyDecoder, &m_cSbacDecoder, &m_cBinCABAC, &m_cCavlcDecoder, &m_cSliceDecoder, &m_cLoopFilter, &m_cSAO);
131  m_cSliceDecoder.init( m_ppcTDecTop, &m_cEntropyDecoder, &m_cCuDecoder );
132#else
133  m_cGopDecoder.init( &m_cEntropyDecoder, &m_cSbacDecoder, &m_cBinCABAC, &m_cCavlcDecoder, &m_cSliceDecoder, &m_cLoopFilter, &m_cSAO);
134  m_cSliceDecoder.init( &m_cEntropyDecoder, &m_cCuDecoder );
135#endif
136  m_cEntropyDecoder.init(&m_cPrediction);
137}
138
139#if REF_IDX_FRAMEWORK
140Void TDecTop::xInitILRP(TComSPS *pcSPS)
141{
142  if(m_layerId>0)
143  {
144    g_bitDepthY     = pcSPS->getBitDepthY();
145    g_bitDepthC     = pcSPS->getBitDepthC();
146    g_uiMaxCUWidth  = pcSPS->getMaxCUWidth();
147    g_uiMaxCUHeight = pcSPS->getMaxCUHeight();
148    g_uiMaxCUDepth  = pcSPS->getMaxCUDepth();
149    g_uiAddCUDepth  = max (0, pcSPS->getLog2MinCodingBlockSize() - (Int)pcSPS->getQuadtreeTULog2MinSize() );
150
151    Int  numReorderPics[MAX_TLAYER];
152    Window &conformanceWindow = pcSPS->getConformanceWindow();
153    Window defaultDisplayWindow = pcSPS->getVuiParametersPresentFlag() ? pcSPS->getVuiParameters()->getDefaultDisplayWindow() : Window();
154
155    for( Int temporalLayer=0; temporalLayer < MAX_TLAYER; temporalLayer++) 
156    {
157      numReorderPics[temporalLayer] = pcSPS->getNumReorderPics(temporalLayer);
158    }
159
160    if (m_cIlpPic[0] == NULL)
161    {
162      for (Int j=0; j < MAX_LAYERS /*MAX_NUM_REF*/; j++)  // consider to set to NumDirectRefLayers[LayerIdInVps[nuh_layer_id]]
163      {
164
165        m_cIlpPic[j] = new  TComPic;
166#if SVC_UPSAMPLING
167        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
168#else
169        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, true);
170#endif
171        for (Int i=0; i<m_cIlpPic[j]->getPicSym()->getNumberOfCUsInFrame(); i++)
172        {
173          m_cIlpPic[j]->getPicSym()->getCU(i)->initCU(m_cIlpPic[j], i);
174        }
175      }
176    }
177  }
178}
179
180Void TDecTop::setILRPic(TComPic *pcPic)
181{
182  for( Int i = 0; i < pcPic->getSlice(0)->getActiveNumILRRefIdx(); i++ )
183  {
184    Int refLayerIdc = pcPic->getSlice(0)->getInterLayerPredLayerIdc(i);
185
186    if(m_cIlpPic[refLayerIdc])
187    {
188      m_cIlpPic[refLayerIdc]->copyUpsampledPictureYuv(pcPic->getFullPelBaseRec(refLayerIdc), m_cIlpPic[refLayerIdc]->getPicYuvRec());
189      m_cIlpPic[refLayerIdc]->getSlice(0)->setPOC(pcPic->getPOC());
190      m_cIlpPic[refLayerIdc]->setLayerId(pcPic->getSlice(0)->getBaseColPic(refLayerIdc)->getLayerId()); //set reference layerId
191      m_cIlpPic[refLayerIdc]->getPicYuvRec()->setBorderExtension(false);
192      m_cIlpPic[refLayerIdc]->getPicYuvRec()->extendPicBorder();
193    }
194  }
195}
196#endif
197
198Void TDecTop::deletePicBuffer ( )
199{
200  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
201  Int iSize = Int( m_cListPic.size() );
202 
203  for (Int i = 0; i < iSize; i++ )
204  {
205    TComPic* pcPic = *(iterPic++);
206#if SVC_EXTENSION
207    if( pcPic )
208    {
209      pcPic->destroy();
210
211      delete pcPic;
212      pcPic = NULL;
213    }
214#else
215    pcPic->destroy();
216   
217    delete pcPic;
218    pcPic = NULL;
219#endif
220  }
221 
222  m_cSAO.destroy();
223 
224  m_cLoopFilter.        destroy();
225 
226#if !SVC_EXTENSION
227  // destroy ROM
228  destroyROM();
229#endif
230}
231
232Void TDecTop::xGetNewPicBuffer ( TComSlice* pcSlice, TComPic*& rpcPic )
233{
234  Int  numReorderPics[MAX_TLAYER];
235  Window &conformanceWindow = pcSlice->getSPS()->getConformanceWindow();
236  Window defaultDisplayWindow = pcSlice->getSPS()->getVuiParametersPresentFlag() ? pcSlice->getSPS()->getVuiParameters()->getDefaultDisplayWindow() : Window();
237
238  for( Int temporalLayer=0; temporalLayer < MAX_TLAYER; temporalLayer++) 
239  {
240    numReorderPics[temporalLayer] = pcSlice->getSPS()->getNumReorderPics(temporalLayer);
241  }
242
243#if L0323_DPB
244  m_iMaxRefPicNum = pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getTLayer())+pcSlice->getSPS()->getNumReorderPics(pcSlice->getTLayer());     // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
245#else
246  m_iMaxRefPicNum = pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getTLayer())+pcSlice->getSPS()->getNumReorderPics(pcSlice->getTLayer()) + 1; // +1 to have space for the picture currently being decoded
247#endif
248  if (m_cListPic.size() < (UInt)m_iMaxRefPicNum)
249  {
250    rpcPic = new TComPic();
251
252#if SVC_EXTENSION //Temporal solution, should be modified
253    if(m_layerId > 0)
254    {
255      for(UInt i = 0; i < pcSlice->getVPS()->getNumDirectRefLayers( m_layerId ); i++ )
256      {
257        const Window scalEL = pcSlice->getSPS()->getScaledRefLayerWindow();
258        Bool zeroOffsets = ( scalEL.getWindowLeftOffset() == 0 && scalEL.getWindowRightOffset() == 0 && scalEL.getWindowTopOffset() == 0 && scalEL.getWindowBottomOffset() == 0 );
259
260#if VPS_EXTN_DIRECT_REF_LAYERS
261        TDecTop *pcTDecTopBase = (TDecTop *)getRefLayerDec( i );
262#else
263        TDecTop *pcTDecTopBase = (TDecTop *)getLayerDec( m_layerId-1 );
264#endif
265        //TComPic*                      pcPic = *(pcTDecTopBase->getListPic()->begin());
266        TComPicYuv* pcPicYuvRecBase = (*(pcTDecTopBase->getListPic()->begin()))->getPicYuvRec(); 
267        if(pcPicYuvRecBase->getWidth() != pcSlice->getSPS()->getPicWidthInLumaSamples() || pcPicYuvRecBase->getHeight() != pcSlice->getSPS()->getPicHeightInLumaSamples() || !zeroOffsets )
268        {
269          rpcPic->setSpatialEnhLayerFlag( i, true );
270
271          //only for scalable extension
272          assert( pcSlice->getVPS()->getScalabilityMask(1) == true );
273        }
274#if MAX_ONE_RESAMPLING_DIRECT_LAYERS
275        if(pcSlice->getVPS()->getScalabilityMask(1))
276        {
277          pcSlice->setPic(rpcPic);
278        }
279#endif
280      }
281    }
282#endif
283   
284#if SVC_UPSAMPLING
285    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
286                     conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
287#else
288    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
289                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
290#endif
291    rpcPic->getPicSym()->allocSaoParam(&m_cSAO);
292    m_cListPic.pushBack( rpcPic );
293   
294    return;
295  }
296 
297  Bool bBufferIsAvailable = false;
298  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
299  while (iterPic != m_cListPic.end())
300  {
301    rpcPic = *(iterPic++);
302    if ( rpcPic->getReconMark() == false && rpcPic->getOutputMark() == false)
303    {
304      rpcPic->setOutputMark(false);
305      bBufferIsAvailable = true;
306      break;
307    }
308
309    if ( rpcPic->getSlice( 0 )->isReferenced() == false  && rpcPic->getOutputMark() == false)
310    {
311#if !SVC_EXTENSION
312      rpcPic->setOutputMark(false);
313#endif
314      rpcPic->setReconMark( false );
315      rpcPic->getPicYuvRec()->setBorderExtension( false );
316      bBufferIsAvailable = true;
317      break;
318    }
319  }
320 
321  if ( !bBufferIsAvailable )
322  {
323    //There is no room for this picture, either because of faulty encoder or dropped NAL. Extend the buffer.
324    m_iMaxRefPicNum++;
325    rpcPic = new TComPic();
326    m_cListPic.pushBack( rpcPic );
327  }
328  rpcPic->destroy();
329
330#if SVC_UPSAMPLING
331  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
332                   conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
333
334#else
335  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
336                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
337#endif
338  rpcPic->getPicSym()->allocSaoParam(&m_cSAO);
339}
340
341Void TDecTop::executeLoopFilters(Int& poc, TComList<TComPic*>*& rpcListPic)
342{
343  if (!m_pcPic)
344  {
345    /* nothing to deblock */
346    return;
347  }
348 
349  TComPic*&   pcPic         = m_pcPic;
350
351  // Execute Deblock + Cleanup
352
353  m_cGopDecoder.filterPicture(pcPic);
354
355#if SYNTAX_OUTPUT
356  pcPic->wrireBLSyntax( getBLSyntaxFile(), SYNTAX_BYTES );
357#endif
358  TComSlice::sortPicList( m_cListPic ); // sorting for application output
359  poc                 = pcPic->getSlice(m_uiSliceIdx-1)->getPOC();
360  rpcListPic          = &m_cListPic; 
361  m_cCuDecoder.destroy();       
362  m_bFirstSliceInPicture  = true;
363
364  return;
365}
366
367Void TDecTop::xCreateLostPicture(Int iLostPoc) 
368{
369  printf("\ninserting lost poc : %d\n",iLostPoc);
370  TComSlice cFillSlice;
371#if SVC_EXTENSION
372  cFillSlice.setSPS( m_parameterSetManagerDecoder[m_layerId].getFirstSPS() );
373  cFillSlice.setPPS( m_parameterSetManagerDecoder[m_layerId].getFirstPPS() );
374  cFillSlice.setVPS( m_parameterSetManagerDecoder[m_layerId].getFirstVPS() );
375  cFillSlice.initSlice( m_layerId );
376#else
377  cFillSlice.setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
378  cFillSlice.setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
379  cFillSlice.initSlice();
380#endif
381  TComPic *cFillPic;
382  xGetNewPicBuffer(&cFillSlice,cFillPic);
383#if SVC_EXTENSION
384  cFillPic->getSlice(0)->setSPS( m_parameterSetManagerDecoder[m_layerId].getFirstSPS() );
385  cFillPic->getSlice(0)->setPPS( m_parameterSetManagerDecoder[m_layerId].getFirstPPS() );
386  cFillPic->getSlice(0)->setVPS( m_parameterSetManagerDecoder[m_layerId].getFirstVPS() );
387  cFillPic->getSlice(0)->initSlice( m_layerId );
388#else
389  cFillPic->getSlice(0)->setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
390  cFillPic->getSlice(0)->setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
391  cFillPic->getSlice(0)->initSlice();
392#endif
393 
394  TComList<TComPic*>::iterator iterPic = m_cListPic.begin();
395  Int closestPoc = 1000000;
396  while ( iterPic != m_cListPic.end())
397  {
398    TComPic * rpcPic = *(iterPic++);
399    if(abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc)<closestPoc&&abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc)!=0&&rpcPic->getPicSym()->getSlice(0)->getPOC()!=m_apcSlicePilot->getPOC())
400    {
401      closestPoc=abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc);
402    }
403  }
404  iterPic = m_cListPic.begin();
405  while ( iterPic != m_cListPic.end())
406  {
407    TComPic *rpcPic = *(iterPic++);
408    if(abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc)==closestPoc&&rpcPic->getPicSym()->getSlice(0)->getPOC()!=m_apcSlicePilot->getPOC())
409    {
410      printf("copying picture %d to %d (%d)\n",rpcPic->getPicSym()->getSlice(0)->getPOC() ,iLostPoc,m_apcSlicePilot->getPOC());
411      rpcPic->getPicYuvRec()->copyToPic(cFillPic->getPicYuvRec());
412      break;
413    }
414  }
415  cFillPic->setCurrSliceIdx(0);
416  for(Int i=0; i<cFillPic->getNumCUsInFrame(); i++) 
417  {
418    cFillPic->getCU(i)->initCU(cFillPic,i);
419  }
420  cFillPic->getSlice(0)->setReferenced(true);
421  cFillPic->getSlice(0)->setPOC(iLostPoc);
422  cFillPic->setReconMark(true);
423  cFillPic->setOutputMark(true);
424  if(m_pocRandomAccess == MAX_INT)
425  {
426    m_pocRandomAccess = iLostPoc;
427  }
428}
429
430
431Void TDecTop::xActivateParameterSets()
432{
433#if SVC_EXTENSION
434  m_parameterSetManagerDecoder[m_layerId].applyPrefetchedPS();
435 
436  TComPPS *pps = m_parameterSetManagerDecoder[m_layerId].getPPS(m_apcSlicePilot->getPPSId());
437  assert (pps != 0);
438
439  TComSPS *sps = m_parameterSetManagerDecoder[m_layerId].getSPS(pps->getSPSId());
440  assert (sps != 0);
441
442  if( false == m_parameterSetManagerDecoder[m_layerId].activatePPS(m_apcSlicePilot->getPPSId(), m_apcSlicePilot->isIRAP()) )
443#else
444  m_parameterSetManagerDecoder.applyPrefetchedPS();
445 
446  TComPPS *pps = m_parameterSetManagerDecoder.getPPS(m_apcSlicePilot->getPPSId());
447  assert (pps != 0);
448
449  TComSPS *sps = m_parameterSetManagerDecoder.getSPS(pps->getSPSId());
450  assert (sps != 0);
451
452  if (false == m_parameterSetManagerDecoder.activatePPS(m_apcSlicePilot->getPPSId(),m_apcSlicePilot->isIRAP()))
453#endif
454  {
455    printf ("Parameter set activation failed!");
456    assert (0);
457  }
458
459  if( pps->getDependentSliceSegmentsEnabledFlag() )
460  {
461    Int NumCtx = pps->getEntropyCodingSyncEnabledFlag()?2:1;
462
463    if (m_cSliceDecoder.getCtxMemSize() != NumCtx)
464    {
465      m_cSliceDecoder.initCtxMem(NumCtx);
466      for ( UInt st = 0; st < NumCtx; st++ )
467      {
468        TDecSbac* ctx = NULL;
469        ctx = new TDecSbac;
470        ctx->init( &m_cBinCABAC );
471        m_cSliceDecoder.setCtxMem( ctx, st );
472      }
473    }
474  }
475
476  m_apcSlicePilot->setPPS(pps);
477  m_apcSlicePilot->setSPS(sps);
478
479  pps->setSPS(sps);
480  pps->setNumSubstreams(pps->getEntropyCodingSyncEnabledFlag() ? ((sps->getPicHeightInLumaSamples() + sps->getMaxCUHeight() - 1) / sps->getMaxCUHeight()) * (pps->getNumColumnsMinus1() + 1) : 1);
481  pps->setMinCuDQPSize( sps->getMaxCUWidth() >> ( pps->getMaxCuDQPDepth()) );
482
483  g_bitDepthY     = sps->getBitDepthY();
484  g_bitDepthC     = sps->getBitDepthC();
485  g_uiMaxCUWidth  = sps->getMaxCUWidth();
486  g_uiMaxCUHeight = sps->getMaxCUHeight();
487  g_uiMaxCUDepth  = sps->getMaxCUDepth();
488  g_uiAddCUDepth  = max (0, sps->getLog2MinCodingBlockSize() - (Int)sps->getQuadtreeTULog2MinSize() );
489
490  for (Int i = 0; i < sps->getLog2DiffMaxMinCodingBlockSize(); i++)
491  {
492    sps->setAMPAcc( i, sps->getUseAMP() );
493  }
494
495  for (Int i = sps->getLog2DiffMaxMinCodingBlockSize(); i < sps->getMaxCUDepth(); i++)
496  {
497    sps->setAMPAcc( i, 0 );
498  }
499
500  m_cSAO.destroy();
501  m_cSAO.create( sps->getPicWidthInLumaSamples(), sps->getPicHeightInLumaSamples(), sps->getMaxCUWidth(), sps->getMaxCUHeight() );
502  m_cLoopFilter.create( sps->getMaxCUDepth() );
503}
504
505#if SVC_EXTENSION
506Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC )
507#else
508Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay )
509#endif
510{
511  TComPic*&   pcPic         = m_pcPic;
512#if SVC_EXTENSION
513  m_apcSlicePilot->setVPS( m_parameterSetManagerDecoder[m_layerId].getPrefetchedVPS(0) );
514  m_apcSlicePilot->initSlice( nalu.m_layerId );
515#else
516  m_apcSlicePilot->initSlice();
517#endif
518
519  if (m_bFirstSliceInPicture)
520  {
521    m_uiSliceIdx     = 0;
522  }
523  m_apcSlicePilot->setSliceIdx(m_uiSliceIdx);
524  if (!m_bFirstSliceInPicture)
525  {
526    m_apcSlicePilot->copySliceInfo( pcPic->getPicSym()->getSlice(m_uiSliceIdx-1) );
527  }
528
529  m_apcSlicePilot->setNalUnitType(nalu.m_nalUnitType);
530  Bool nonReferenceFlag = (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TRAIL_N ||
531                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N   ||
532                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N  ||
533                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N  ||
534                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N);
535  m_apcSlicePilot->setTemporalLayerNonReferenceFlag(nonReferenceFlag);
536 
537  m_apcSlicePilot->setReferenced(true); // Putting this as true ensures that picture is referenced the first time it is in an RPS
538  m_apcSlicePilot->setTLayerInfo(nalu.m_temporalId);
539
540#if SVC_EXTENSION
541  m_cEntropyDecoder.decodeSliceHeader (m_apcSlicePilot, &m_parameterSetManagerDecoder[m_layerId]);
542#else
543  m_cEntropyDecoder.decodeSliceHeader (m_apcSlicePilot, &m_parameterSetManagerDecoder);
544#endif
545
546  // Skip pictures due to random access
547  if (isRandomAccessSkipPicture(iSkipFrame, iPOCLastDisplay))
548  {
549    return false;
550  }
551  // Skip TFD pictures associated with BLA/BLANT pictures
552  if (isSkipPictureForBLA(iPOCLastDisplay))
553  {
554    return false;
555  }
556
557  // exit when a new picture is found
558#if SVC_EXTENSION
559  bNewPOC = (m_apcSlicePilot->getPOC()!= m_prevPOC);
560  if (m_apcSlicePilot->isNextSlice() && (bNewPOC || m_layerId!=m_uiPrevLayerId) && !m_bFirstSliceInSequence )
561  {
562    m_prevPOC = m_apcSlicePilot->getPOC();
563    curLayerId = m_uiPrevLayerId; 
564    m_uiPrevLayerId = m_layerId;
565    return true;
566  }
567#else
568  //we should only get a different poc for a new picture (with CTU address==0)
569  if (m_apcSlicePilot->isNextSlice() && m_apcSlicePilot->getPOC()!=m_prevPOC && !m_bFirstSliceInSequence && (!m_apcSlicePilot->getSliceCurStartCUAddr()==0))
570  {
571    printf ("Warning, the first slice of a picture might have been lost!\n");
572  }
573  // exit when a new picture is found
574  if (m_apcSlicePilot->isNextSlice() && (m_apcSlicePilot->getSliceCurStartCUAddr() == 0 && !m_bFirstSliceInPicture) && !m_bFirstSliceInSequence )
575  {
576    if (m_prevPOC >= m_pocRandomAccess)
577    {
578      m_prevPOC = m_apcSlicePilot->getPOC();
579      return true;
580    }
581    m_prevPOC = m_apcSlicePilot->getPOC();
582  }
583#endif
584  // actual decoding starts here
585  xActivateParameterSets();
586
587  if (m_apcSlicePilot->isNextSlice()) 
588  {
589    m_prevPOC = m_apcSlicePilot->getPOC();
590#if SVC_EXTENSION
591    curLayerId = m_layerId;
592    m_uiPrevLayerId = m_layerId;
593#endif
594  }
595  m_bFirstSliceInSequence = false;
596  //detect lost reference picture and insert copy of earlier frame.
597  Int lostPoc;
598  while((lostPoc=m_apcSlicePilot->checkThatAllRefPicsAreAvailable(m_cListPic, m_apcSlicePilot->getRPS(), true, m_pocRandomAccess)) > 0)
599  {
600    xCreateLostPicture(lostPoc-1);
601  }
602  if (m_bFirstSliceInPicture)
603  {
604#if AVC_BASE
605    if( m_layerId == 1 && m_parameterSetManagerDecoder[0].getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
606    {
607      TComPic* pBLPic = (*m_ppcTDecTop[0]->getListPic()->begin());
608      fstream* pFile  = m_ppcTDecTop[0]->getBLReconFile();
609#if ILP_DECODED_PICTURE
610      UInt uiWidth    = pBLPic->getPicYuvRec()->getWidth();
611      UInt uiHeight   = pBLPic->getPicYuvRec()->getHeight();
612#else
613      const Window &conf = pBLPic->getConformanceWindow();
614      UInt uiWidth    = pBLPic->getPicYuvRec()->getWidth() - conf.getWindowLeftOffset() - conf.getWindowRightOffset();
615      UInt uiHeight   = pBLPic->getPicYuvRec()->getHeight() - conf.getWindowTopOffset() - conf.getWindowBottomOffset();
616#endif
617
618      if( pFile->good() )
619      {
620        UInt64 uiPos = (UInt64) m_apcSlicePilot->getPOC() * uiWidth * uiHeight * 3 / 2;
621
622        pFile->seekg((UInt)uiPos, ios::beg );
623
624        Pel* pPel = pBLPic->getPicYuvRec()->getLumaAddr();
625        UInt uiStride = pBLPic->getPicYuvRec()->getStride();
626        for( Int i = 0; i < uiHeight; i++ )
627        {
628          for( Int j = 0; j < uiWidth; j++ )
629          {
630            pPel[j] = pFile->get();
631          }
632          pPel += uiStride;
633        }
634
635        pPel = pBLPic->getPicYuvRec()->getCbAddr();
636        uiStride = pBLPic->getPicYuvRec()->getCStride();
637        for( Int i = 0; i < uiHeight/2; i++ )
638        {
639          for( Int j = 0; j < uiWidth/2; j++ )
640          {
641            pPel[j] = pFile->get();
642          }
643          pPel += uiStride;
644        }
645
646        pPel = pBLPic->getPicYuvRec()->getCrAddr();
647        uiStride = pBLPic->getPicYuvRec()->getCStride();
648        for( Int i = 0; i < uiHeight/2; i++ )
649        {
650          for( Int j = 0; j < uiWidth/2; j++ )
651          {
652            pPel[j] = pFile->get();
653          }
654          pPel += uiStride;
655        }
656      }
657    }
658#endif
659
660    // Buffer initialize for prediction.
661    m_cPrediction.initTempBuff();
662    m_apcSlicePilot->applyReferencePictureSet(m_cListPic, m_apcSlicePilot->getRPS());
663    //  Get a new picture buffer
664    xGetNewPicBuffer (m_apcSlicePilot, pcPic);
665
666    // transfer any SEI messages that have been received to the picture
667    pcPic->setSEIs(m_SEIs);
668    m_SEIs.clear();
669
670    // Recursive structure
671    m_cCuDecoder.create ( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight );
672#if SVC_EXTENSION
673    m_cCuDecoder.init   ( m_ppcTDecTop,&m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction, curLayerId );
674#else
675    m_cCuDecoder.init   ( &m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction );
676#endif
677    m_cTrQuant.init     ( g_uiMaxCUWidth, g_uiMaxCUHeight, m_apcSlicePilot->getSPS()->getMaxTrSize());
678
679    m_cSliceDecoder.create();
680  }
681  else
682  {
683    // Check if any new SEI has arrived
684    if(!m_SEIs.empty())
685    {
686      // Currently only decoding Unit SEI message occurring between VCL NALUs copied
687      SEIMessages &picSEI = pcPic->getSEIs();
688      SEIMessages decodingUnitInfos = extractSeisByType (m_SEIs, SEI::DECODING_UNIT_INFO);
689      picSEI.insert(picSEI.end(), decodingUnitInfos.begin(), decodingUnitInfos.end());
690      deleteSEIs(m_SEIs);
691    }
692  }
693 
694  //  Set picture slice pointer
695  TComSlice*  pcSlice = m_apcSlicePilot;
696  Bool bNextSlice     = pcSlice->isNextSlice();
697
698  UInt uiCummulativeTileWidth;
699  UInt uiCummulativeTileHeight;
700  UInt i, j, p;
701
702  //set NumColumnsMins1 and NumRowsMinus1
703  pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getPPS()->getNumColumnsMinus1() );
704  pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getPPS()->getNumRowsMinus1() );
705
706  //create the TComTileArray
707  pcPic->getPicSym()->xCreateTComTileArray();
708
709  if( pcSlice->getPPS()->getUniformSpacingFlag() )
710  {
711    //set the width for each tile
712    for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
713    {
714      for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++)
715      {
716        pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->
717          setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1) 
718          - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) );
719      }
720    }
721
722    //set the height for each tile
723    for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
724    {
725      for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++)
726      {
727        pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->
728          setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1) 
729          - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) );   
730      }
731    }
732  }
733  else
734  {
735    //set the width for each tile
736    for(j=0; j < pcSlice->getPPS()->getNumRowsMinus1()+1; j++)
737    {
738      uiCummulativeTileWidth = 0;
739      for(i=0; i < pcSlice->getPPS()->getNumColumnsMinus1(); i++)
740      {
741        pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcSlice->getPPS()->getColumnWidth(i) );
742        uiCummulativeTileWidth += pcSlice->getPPS()->getColumnWidth(i);
743      }
744      pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth );
745    }
746
747    //set the height for each tile
748    for(j=0; j < pcSlice->getPPS()->getNumColumnsMinus1()+1; j++)
749    {
750      uiCummulativeTileHeight = 0;
751      for(i=0; i < pcSlice->getPPS()->getNumRowsMinus1(); i++)
752      { 
753        pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcSlice->getPPS()->getRowHeight(i) );
754        uiCummulativeTileHeight += pcSlice->getPPS()->getRowHeight(i);
755      }
756      pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight );
757    }
758  }
759
760  pcPic->getPicSym()->xInitTiles();
761
762  //generate the Coding Order Map and Inverse Coding Order Map
763  UInt uiEncCUAddr;
764  for(i=0, uiEncCUAddr=0; i<pcPic->getPicSym()->getNumberOfCUsInFrame(); i++, uiEncCUAddr = pcPic->getPicSym()->xCalculateNxtCUAddr(uiEncCUAddr))
765  {
766    pcPic->getPicSym()->setCUOrderMap(i, uiEncCUAddr);
767    pcPic->getPicSym()->setInverseCUOrderMap(uiEncCUAddr, i);
768  }
769  pcPic->getPicSym()->setCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
770  pcPic->getPicSym()->setInverseCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
771
772  //convert the start and end CU addresses of the slice and dependent slice into encoding order
773  pcSlice->setSliceSegmentCurStartCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurStartCUAddr()) );
774  pcSlice->setSliceSegmentCurEndCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurEndCUAddr()) );
775  if(pcSlice->isNextSlice())
776  {
777    pcSlice->setSliceCurStartCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurStartCUAddr()));
778    pcSlice->setSliceCurEndCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurEndCUAddr()));
779  }
780
781  if (m_bFirstSliceInPicture) 
782  {
783    if(pcPic->getNumAllocatedSlice() != 1)
784    {
785      pcPic->clearSliceBuffer();
786    }
787  }
788  else
789  {
790    pcPic->allocateNewSlice();
791  }
792  assert(pcPic->getNumAllocatedSlice() == (m_uiSliceIdx + 1));
793  m_apcSlicePilot = pcPic->getPicSym()->getSlice(m_uiSliceIdx); 
794  pcPic->getPicSym()->setSlice(pcSlice, m_uiSliceIdx);
795
796  pcPic->setTLayer(nalu.m_temporalId);
797
798#if SVC_EXTENSION
799  pcPic->setLayerId(nalu.m_layerId);
800  pcSlice->setLayerId(nalu.m_layerId);
801#endif
802
803  if (bNextSlice)
804  {
805    pcSlice->checkCRA(pcSlice->getRPS(), m_pocCRA, m_prevRAPisBLA, m_cListPic );
806    // Set reference list
807#if REF_IDX_FRAMEWORK
808    if (m_layerId == 0)
809#elif INTRA_BL
810    if( m_layerId > 0 )
811    {
812      pcSlice->setRefPicList( m_cListPic );
813    }
814    else
815#endif
816#if FIX1071
817    pcSlice->setRefPicList( m_cListPic, true );
818#else
819    pcSlice->setRefPicList( m_cListPic );
820#endif
821
822#if SVC_EXTENSION   
823    if(m_layerId > 0)
824    {
825      for( i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
826      {
827        UInt refLayerIdc = pcSlice->getInterLayerPredLayerIdc(i);
828#if AVC_BASE
829        if( pcSlice->getVPS()->getRefLayerId( m_layerId, refLayerIdc ) == 0 && m_parameterSetManagerDecoder[0].getActiveVPS()->getAvcBaseLayerFlag() )
830        {
831          pcSlice->setBaseColPic ( refLayerIdc, *m_ppcTDecTop[0]->getListPic()->begin() );
832#if AVC_SYNTAX
833          TComPic* pBLPic = pcSlice->getBaseColPic(refLayerIdc);
834          if( pcSlice->getPOC() == 0 )
835          {
836            // initialize partition order.
837            UInt* piTmp = &g_auiZscanToRaster[0];
838            initZscanToRaster( pBLPic->getPicSym()->getMaxDepth() + 1, 1, 0, piTmp );
839            initRasterToZscan( pBLPic->getPicSym()->getMaxCUWidth(), pBLPic->getPicSym()->getMaxCUHeight(), pBLPic->getPicSym()->getMaxDepth() + 1 );
840          }     
841          pBLPic->getSlice( 0 )->initBaseLayerRPL( pcSlice );
842          pBLPic->readBLSyntax( m_ppcTDecTop[0]->getBLSyntaxFile(), SYNTAX_BYTES );
843#endif
844        }
845        else
846        {
847#if VPS_EXTN_DIRECT_REF_LAYERS
848          TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
849#else
850          TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
851#endif
852          TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
853          pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
854        }
855#else
856#if VPS_EXTN_DIRECT_REF_LAYERS
857        TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
858#else
859        TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
860#endif
861        TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
862        pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
863#endif
864
865#if SIMPLIFIED_MV_POS_SCALING
866#if SCALED_REF_LAYER_OFFSETS
867        const Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindow();
868
869        Int widthBL   = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getWidth();
870        Int heightBL  = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getHeight();
871
872        Int widthEL   = pcPic->getPicYuvRec()->getWidth()  - scalEL.getWindowLeftOffset() - scalEL.getWindowRightOffset();
873        Int heightEL  = pcPic->getPicYuvRec()->getHeight() - scalEL.getWindowTopOffset()  - scalEL.getWindowBottomOffset();
874#else
875        const Window &confBL = pcSlice->getBaseColPic()->getPicYuvRec()->getConformanceWindow();
876        const Window &confEL = pcPic->getPicYuvRec()->getConformanceWindow();
877
878        Int widthBL   = pcSlice->getBaseColPic()->getPicYuvRec()->getWidth () - confBL.getWindowLeftOffset() - confBL.getWindowRightOffset();
879        Int heightBL  = pcSlice->getBaseColPic()->getPicYuvRec()->getHeight() - confBL.getWindowTopOffset() - confBL.getWindowBottomOffset();
880
881        Int widthEL   = pcPic->getPicYuvRec()->getWidth() - confEL.getWindowLeftOffset() - confEL.getWindowRightOffset();
882        Int heightEL  = pcPic->getPicYuvRec()->getHeight() - confEL.getWindowTopOffset() - confEL.getWindowBottomOffset();
883#endif
884        g_mvScalingFactor[refLayerIdc][0] = widthEL  == widthBL  ? 4096 : Clip3(-4096, 4095, ((widthEL  << 8) + (widthBL  >> 1)) / widthBL);
885        g_mvScalingFactor[refLayerIdc][1] = heightEL == heightBL ? 4096 : Clip3(-4096, 4095, ((heightEL << 8) + (heightBL >> 1)) / heightBL);
886
887        g_posScalingFactor[refLayerIdc][0] = ((widthBL  << 16) + (widthEL  >> 1)) / widthEL;
888        g_posScalingFactor[refLayerIdc][1] = ((heightBL << 16) + (heightEL >> 1)) / heightEL;
889#endif
890
891#if SVC_UPSAMPLING
892        if( pcPic->isSpatialEnhLayer(refLayerIdc) )
893        {   
894#if SCALED_REF_LAYER_OFFSETS
895          m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), pcSlice->getSPS()->getScaledRefLayerWindow() );
896#else
897          m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec() );
898#endif
899        }
900        else
901        {
902          pcPic->setFullPelBaseRec( refLayerIdc, pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec() );
903        }
904        pcSlice->setFullPelBaseRec ( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc) );
905#endif
906      }
907    }
908
909#if REF_IDX_FRAMEWORK
910#if ZERO_NUM_DIRECT_LAYERS
911    if( m_layerId > 0 && pcSlice->getActiveNumILRRefIdx() )
912#else
913    if(m_layerId > 0)
914#endif
915    {
916      setILRPic(pcPic);
917#if REF_IDX_MFM
918      if( pcSlice->getSPS()->getMFMEnabledFlag() )
919      {
920        pcSlice->setRefPOCListILP(m_ppcTDecTop[m_layerId]->m_cIlpPic, pcSlice->getBaseColPic());
921      }
922#endif
923      pcSlice->setRefPicList( m_cListPic, false, m_cIlpPic);
924    }
925#endif
926
927#endif //SVC_EXTENSION
928
929    // For generalized B
930    // note: maybe not existed case (always L0 is copied to L1 if L1 is empty)
931    if (pcSlice->isInterB() && pcSlice->getNumRefIdx(REF_PIC_LIST_1) == 0)
932    {
933      Int iNumRefIdx = pcSlice->getNumRefIdx(REF_PIC_LIST_0);
934      pcSlice->setNumRefIdx        ( REF_PIC_LIST_1, iNumRefIdx );
935
936      for (Int iRefIdx = 0; iRefIdx < iNumRefIdx; iRefIdx++)
937      {
938        pcSlice->setRefPic(pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx), REF_PIC_LIST_1, iRefIdx);
939      }
940    }
941    if (!pcSlice->isIntra())
942    {
943      Bool bLowDelay = true;
944      Int  iCurrPOC  = pcSlice->getPOC();
945      Int iRefIdx = 0;
946
947      for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
948      {
949        if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
950        {
951          bLowDelay = false;
952        }
953      }
954      if (pcSlice->isInterB())
955      {
956        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
957        {
958          if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
959          {
960            bLowDelay = false;
961          }
962        }       
963      }
964
965      pcSlice->setCheckLDC(bLowDelay);           
966    }
967
968    //---------------
969    pcSlice->setRefPOCList();
970#if !L0034_COMBINED_LIST_CLEANUP
971    pcSlice->setNoBackPredFlag( false );
972    if ( pcSlice->getSliceType() == B_SLICE )
973    {
974      if ( pcSlice->getNumRefIdx(RefPicList( 0 ) ) == pcSlice->getNumRefIdx(RefPicList( 1 ) ) )
975      {
976        pcSlice->setNoBackPredFlag( true );
977        for ( i=0; i < pcSlice->getNumRefIdx(RefPicList( 1 ) ); i++ )
978        {
979          if ( pcSlice->getRefPOC(RefPicList(1), i) != pcSlice->getRefPOC(RefPicList(0), i) ) 
980          {
981            pcSlice->setNoBackPredFlag( false );
982            break;
983          }
984        }
985      }
986    }
987#endif
988  }
989
990  pcPic->setCurrSliceIdx(m_uiSliceIdx);
991  if(pcSlice->getSPS()->getScalingListFlag())
992  {
993    pcSlice->setScalingList ( pcSlice->getSPS()->getScalingList()  );
994    if(pcSlice->getPPS()->getScalingListPresentFlag())
995    {
996      pcSlice->setScalingList ( pcSlice->getPPS()->getScalingList()  );
997    }
998    pcSlice->getScalingList()->setUseTransformSkip(pcSlice->getPPS()->getUseTransformSkip());
999    if(!pcSlice->getPPS()->getScalingListPresentFlag() && !pcSlice->getSPS()->getScalingListPresentFlag())
1000    {
1001      pcSlice->setDefaultScalingList();
1002    }
1003    m_cTrQuant.setScalingListDec(pcSlice->getScalingList());
1004    m_cTrQuant.setUseScalingList(true);
1005  }
1006  else
1007  {
1008    m_cTrQuant.setFlatScalingList();
1009    m_cTrQuant.setUseScalingList(false);
1010  }
1011
1012  //  Decode a picture
1013  m_cGopDecoder.decompressSlice(nalu.m_Bitstream, pcPic);
1014
1015  m_bFirstSliceInPicture = false;
1016  m_uiSliceIdx++;
1017
1018  return false;
1019}
1020
1021Void TDecTop::xDecodeVPS()
1022{
1023  TComVPS* vps = new TComVPS();
1024 
1025  m_cEntropyDecoder.decodeVPS( vps );
1026#if SVC_EXTENSION
1027  m_parameterSetManagerDecoder[0].storePrefetchedVPS(vps);
1028#else
1029  m_parameterSetManagerDecoder.storePrefetchedVPS(vps); 
1030#endif
1031}
1032
1033Void TDecTop::xDecodeSPS()
1034{
1035  TComSPS* sps = new TComSPS();
1036#if SVC_EXTENSION
1037  sps->setLayerId(m_layerId);
1038#endif
1039#if SPS_SUB_LAYER_INFO
1040  m_cEntropyDecoder.decodeSPS( sps, &m_parameterSetManagerDecoder[0] );
1041#else
1042  m_cEntropyDecoder.decodeSPS( sps );
1043#endif
1044#if SVC_EXTENSION
1045  m_parameterSetManagerDecoder[m_layerId].storePrefetchedSPS(sps);
1046#else
1047  m_parameterSetManagerDecoder.storePrefetchedSPS(sps);
1048#endif
1049#if REF_IDX_FRAMEWORK
1050  if(m_numLayer>0)
1051  {
1052    xInitILRP(sps);
1053  }
1054#endif
1055}
1056
1057Void TDecTop::xDecodePPS()
1058{
1059  TComPPS* pps = new TComPPS();
1060  m_cEntropyDecoder.decodePPS( pps );
1061#if SVC_EXTENSION
1062  m_parameterSetManagerDecoder[m_layerId].storePrefetchedPPS( pps );
1063#else
1064  m_parameterSetManagerDecoder.storePrefetchedPPS( pps );
1065#endif
1066
1067  if( pps->getDependentSliceSegmentsEnabledFlag() )
1068  {
1069    Int NumCtx = pps->getEntropyCodingSyncEnabledFlag()?2:1;
1070    m_cSliceDecoder.initCtxMem(NumCtx);
1071    for ( UInt st = 0; st < NumCtx; st++ )
1072    {
1073      TDecSbac* ctx = NULL;
1074      ctx = new TDecSbac;
1075      ctx->init( &m_cBinCABAC );
1076      m_cSliceDecoder.setCtxMem( ctx, st );
1077    }
1078  }
1079}
1080
1081Void TDecTop::xDecodeSEI( TComInputBitstream* bs, const NalUnitType nalUnitType )
1082{
1083#if SVC_EXTENSION
1084  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
1085  {
1086    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder[m_layerId].getActiveSPS() );
1087  }
1088  else
1089  {
1090    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder[m_layerId].getActiveSPS() );
1091    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
1092    if (activeParamSets.size()>0)
1093    {
1094      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
1095      m_parameterSetManagerDecoder[m_layerId].applyPrefetchedPS();
1096      assert(seiAps->activeSeqParamSetId.size()>0);
1097      if (! m_parameterSetManagerDecoder[m_layerId].activateSPSWithSEI(seiAps->activeSeqParamSetId[0] ))
1098      {
1099        printf ("Warning SPS activation with Active parameter set SEI failed");
1100      }
1101    }
1102  }
1103#else
1104  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
1105  {
1106    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1107  }
1108  else
1109  {
1110    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1111    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
1112    if (activeParamSets.size()>0)
1113    {
1114      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
1115      m_parameterSetManagerDecoder.applyPrefetchedPS();
1116      assert(seiAps->activeSeqParamSetId.size()>0);
1117      if (! m_parameterSetManagerDecoder.activateSPSWithSEI(seiAps->activeSeqParamSetId[0] ))
1118      {
1119        printf ("Warning SPS activation with Active parameter set SEI failed");
1120      }
1121    }
1122  }
1123#endif
1124}
1125
1126#if SVC_EXTENSION
1127Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC)
1128#else
1129Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay)
1130#endif
1131{
1132  // Initialize entropy decoder
1133  m_cEntropyDecoder.setEntropyDecoder (&m_cCavlcDecoder);
1134  m_cEntropyDecoder.setBitstream      (nalu.m_Bitstream);
1135
1136  switch (nalu.m_nalUnitType)
1137  {
1138    case NAL_UNIT_VPS:
1139      xDecodeVPS();
1140#if AVC_BASE
1141      if( m_parameterSetManagerDecoder[0].getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
1142      {
1143        if( !m_ppcTDecTop[0]->getBLReconFile()->good() )
1144        {
1145          printf( "Base layer YUV input reading error\n" );
1146          exit(EXIT_FAILURE);
1147        }       
1148#if AVC_SYNTAX
1149        if( !m_ppcTDecTop[0]->getBLSyntaxFile()->good() )
1150        {
1151          printf( "Base layer syntax input reading error\n" );
1152          exit(EXIT_FAILURE);
1153        }
1154#endif
1155      }
1156      else
1157      {
1158        TComList<TComPic*> *cListPic = m_ppcTDecTop[0]->getListPic();
1159        cListPic->clear();
1160      }
1161#endif
1162      return false;
1163     
1164    case NAL_UNIT_SPS:
1165      xDecodeSPS();
1166#if AVC_BASE
1167      if( m_parameterSetManagerDecoder[0].getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
1168      {
1169        TComPic* pBLPic = (*m_ppcTDecTop[0]->getListPic()->begin());
1170        if( nalu.m_layerId == 1 && pBLPic->getPicYuvRec() == NULL )
1171        {
1172          // using EL SPS with spsId = 1
1173          TComSPS* sps = m_parameterSetManagerDecoder[nalu.m_layerId].getPrefetchedSPS(1);
1174          Int  numReorderPics[MAX_TLAYER];
1175          Window &conformanceWindow = sps->getConformanceWindow();
1176          Window defaultDisplayWindow = sps->getVuiParametersPresentFlag() ? sps->getVuiParameters()->getDefaultDisplayWindow() : Window();
1177#if SVC_UPSAMPLING
1178#if AVC_SYNTAX
1179
1180          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, sps, true);
1181#else
1182          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, NULL, true);
1183#endif
1184#else
1185          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), onformanceWindow, defaultDisplayWindow, numReorderPics, true);
1186#endif
1187        }
1188      }
1189#endif
1190      return false;
1191
1192    case NAL_UNIT_PPS:
1193      xDecodePPS();
1194      return false;
1195     
1196    case NAL_UNIT_PREFIX_SEI:
1197    case NAL_UNIT_SUFFIX_SEI:
1198      xDecodeSEI( nalu.m_Bitstream, nalu.m_nalUnitType );
1199      return false;
1200
1201    case NAL_UNIT_CODED_SLICE_TRAIL_R:
1202    case NAL_UNIT_CODED_SLICE_TRAIL_N:
1203    case NAL_UNIT_CODED_SLICE_TLA_R:
1204    case NAL_UNIT_CODED_SLICE_TSA_N:
1205    case NAL_UNIT_CODED_SLICE_STSA_R:
1206    case NAL_UNIT_CODED_SLICE_STSA_N:
1207    case NAL_UNIT_CODED_SLICE_BLA_W_LP:
1208    case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
1209    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
1210    case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
1211    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
1212    case NAL_UNIT_CODED_SLICE_CRA:
1213    case NAL_UNIT_CODED_SLICE_RADL_N:
1214    case NAL_UNIT_CODED_SLICE_RADL_R:
1215    case NAL_UNIT_CODED_SLICE_RASL_N:
1216    case NAL_UNIT_CODED_SLICE_RASL_R:
1217#if SVC_EXTENSION
1218      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay, curLayerId, bNewPOC);
1219#else
1220      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay);
1221#endif
1222      break;
1223    default:
1224      assert (1);
1225  }
1226
1227  return false;
1228}
1229
1230/** Function for checking if picture should be skipped because of association with a previous BLA picture
1231 * \param iPOCLastDisplay POC of last picture displayed
1232 * \returns true if the picture should be skipped
1233 * This function skips all TFD pictures that follow a BLA picture
1234 * in decoding order and precede it in output order.
1235 */
1236Bool TDecTop::isSkipPictureForBLA(Int& iPOCLastDisplay)
1237{
1238  if (m_prevRAPisBLA && m_apcSlicePilot->getPOC() < m_pocCRA && (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N))
1239  {
1240    iPOCLastDisplay++;
1241    return true;
1242  }
1243  return false;
1244}
1245
1246/** Function for checking if picture should be skipped because of random access
1247 * \param iSkipFrame skip frame counter
1248 * \param iPOCLastDisplay POC of last picture displayed
1249 * \returns true if the picture shold be skipped in the random access.
1250 * This function checks the skipping of pictures in the case of -s option random access.
1251 * All pictures prior to the random access point indicated by the counter iSkipFrame are skipped.
1252 * It also checks the type of Nal unit type at the random access point.
1253 * If the random access point is CRA/CRANT/BLA/BLANT, TFD pictures with POC less than the POC of the random access point are skipped.
1254 * If the random access point is IDR all pictures after the random access point are decoded.
1255 * If the random access point is none of the above, a warning is issues, and decoding of pictures with POC
1256 * equal to or greater than the random access point POC is attempted. For non IDR/CRA/BLA random
1257 * access point there is no guarantee that the decoder will not crash.
1258 */
1259Bool TDecTop::isRandomAccessSkipPicture(Int& iSkipFrame,  Int& iPOCLastDisplay)
1260{
1261  if (iSkipFrame) 
1262  {
1263    iSkipFrame--;   // decrement the counter
1264    return true;
1265  }
1266  else if (m_pocRandomAccess == MAX_INT) // start of random access point, m_pocRandomAccess has not been set yet.
1267  {
1268    if (   m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA
1269        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1270        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
1271        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL )
1272    {
1273      // set the POC random access since we need to skip the reordered pictures in the case of CRA/CRANT/BLA/BLANT.
1274      m_pocRandomAccess = m_apcSlicePilot->getPOC();
1275    }
1276    else if ( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
1277    {
1278      m_pocRandomAccess = -MAX_INT; // no need to skip the reordered pictures in IDR, they are decodable.
1279    }
1280    else 
1281    {
1282      static Bool warningMessage = false;
1283      if(!warningMessage)
1284      {
1285        printf("\nWarning: this is not a valid random access point and the data is discarded until the first CRA picture");
1286        warningMessage = true;
1287      }
1288      return true;
1289    }
1290  }
1291  // skip the reordered pictures, if necessary
1292  else if (m_apcSlicePilot->getPOC() < m_pocRandomAccess && (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N))
1293  {
1294    iPOCLastDisplay++;
1295    return true;
1296  }
1297  // if we reach here, then the picture is not skipped.
1298  return false; 
1299}
1300
1301#if VPS_EXTN_DIRECT_REF_LAYERS
1302TDecTop* TDecTop::getRefLayerDec( UInt refLayerIdc )
1303{
1304  TComVPS* vps = m_parameterSetManagerDecoder[0].getActiveVPS();
1305  if( vps->getNumDirectRefLayers( m_layerId ) <= 0 )
1306  {
1307#if ZERO_NUM_DIRECT_LAYERS
1308    return (TDecTop *)getLayerDec( 0 );
1309#else
1310    return NULL;
1311#endif
1312  }
1313 
1314  return (TDecTop *)getLayerDec( vps->getRefLayerId( m_layerId, refLayerIdc ) );
1315}
1316#endif
1317
1318//! \}
Note: See TracBrowser for help on using the repository browser.