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

Last change on this file since 252 was 250, checked in by seregin, 12 years ago

use refLayerIdc for getting reference layer encoder and decoder

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