source: 3DVCSoftware/branches/HTM-DEV-0.1-dev/source/Lib/TLibDecoder/TDecTop.cpp @ 367

Last change on this file since 367 was 367, checked in by tech, 12 years ago

Further minor cleanups.

  • Property svn:eol-style set to native
File size: 27.2 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-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//! \ingroup TLibDecoder
42//! \{
43
44TDecTop::TDecTop()
45{
46  m_pcPic = 0;
47  m_iMaxRefPicNum = 0;
48#if ENC_DEC_TRACE
49  g_hTrace = fopen( "TraceDec.txt", "wb" );
50  g_bJustDoIt = g_bEncDecTraceDisable;
51  g_nSymbolCounter = 0;
52#endif
53  m_pocCRA = 0;
54  m_prevRAPisBLA = false;
55  m_pocRandomAccess = MAX_INT; 
56  m_prevPOC                = MAX_INT;
57  m_bFirstSliceInPicture    = true;
58  m_bFirstSliceInSequence   = true;
59}
60
61TDecTop::~TDecTop()
62{
63#if ENC_DEC_TRACE
64  fclose( g_hTrace );
65#endif
66}
67
68Void TDecTop::create()
69{
70  m_cGopDecoder.create();
71  m_apcSlicePilot = new TComSlice;
72  m_uiSliceIdx = 0;
73}
74
75Void TDecTop::destroy()
76{
77  m_cGopDecoder.destroy();
78 
79  delete m_apcSlicePilot;
80  m_apcSlicePilot = NULL;
81 
82  m_cSliceDecoder.destroy();
83}
84
85Void TDecTop::init()
86{
87  // initialize ROM
88  initROM();
89  m_cGopDecoder.init( &m_cEntropyDecoder, &m_cSbacDecoder, &m_cBinCABAC, &m_cCavlcDecoder, &m_cSliceDecoder, &m_cLoopFilter, &m_cSAO);
90  m_cSliceDecoder.init( &m_cEntropyDecoder, &m_cCuDecoder );
91  m_cEntropyDecoder.init(&m_cPrediction);
92}
93
94Void TDecTop::deletePicBuffer ( )
95{
96  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
97  Int iSize = Int( m_cListPic.size() );
98 
99  for (Int i = 0; i < iSize; i++ )
100  {
101    TComPic* pcPic = *(iterPic++);
102    pcPic->destroy();
103   
104    delete pcPic;
105    pcPic = NULL;
106  }
107 
108  m_cSAO.destroy();
109 
110  m_cLoopFilter.        destroy();
111 
112  // destroy ROM
113  destroyROM();
114}
115
116Void TDecTop::xGetNewPicBuffer ( TComSlice* pcSlice, TComPic*& rpcPic )
117{
118  Int  numReorderPics[MAX_TLAYER];
119  Window &conformanceWindow = pcSlice->getSPS()->getConformanceWindow();
120  Window defaultDisplayWindow = pcSlice->getSPS()->getVuiParametersPresentFlag() ? pcSlice->getSPS()->getVuiParameters()->getDefaultDisplayWindow() : Window();
121
122  for( Int temporalLayer=0; temporalLayer < MAX_TLAYER; temporalLayer++) 
123  {
124    numReorderPics[temporalLayer] = pcSlice->getSPS()->getNumReorderPics(temporalLayer);
125  }
126
127#if L0323_DPB
128  m_iMaxRefPicNum = pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getTLayer())+pcSlice->getSPS()->getNumReorderPics(pcSlice->getTLayer());     // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
129#else
130  m_iMaxRefPicNum = pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getTLayer())+pcSlice->getSPS()->getNumReorderPics(pcSlice->getTLayer()) + 1; // +1 to have space for the picture currently being decoded
131#endif
132  if (m_cListPic.size() < (UInt)m_iMaxRefPicNum)
133  {
134    rpcPic = new TComPic();
135   
136    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
137                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
138    rpcPic->getPicSym()->allocSaoParam(&m_cSAO);
139    m_cListPic.pushBack( rpcPic );
140   
141    return;
142  }
143 
144  Bool bBufferIsAvailable = false;
145  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
146  while (iterPic != m_cListPic.end())
147  {
148    rpcPic = *(iterPic++);
149    if ( rpcPic->getReconMark() == false && rpcPic->getOutputMark() == false)
150    {
151      rpcPic->setOutputMark(false);
152      bBufferIsAvailable = true;
153      break;
154    }
155
156    if ( rpcPic->getSlice( 0 )->isReferenced() == false  && rpcPic->getOutputMark() == false)
157    {
158      rpcPic->setOutputMark(false);
159      rpcPic->setReconMark( false );
160      rpcPic->getPicYuvRec()->setBorderExtension( false );
161      bBufferIsAvailable = true;
162      break;
163    }
164  }
165 
166  if ( !bBufferIsAvailable )
167  {
168    //There is no room for this picture, either because of faulty encoder or dropped NAL. Extend the buffer.
169    m_iMaxRefPicNum++;
170    rpcPic = new TComPic();
171    m_cListPic.pushBack( rpcPic );
172  }
173  rpcPic->destroy();
174  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
175                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
176  rpcPic->getPicSym()->allocSaoParam(&m_cSAO);
177}
178
179Void TDecTop::executeLoopFilters(Int& poc, TComList<TComPic*>*& rpcListPic)
180{
181  if (!m_pcPic)
182  {
183    /* nothing to deblock */
184    return;
185  }
186 
187  TComPic*&   pcPic         = m_pcPic;
188
189  // Execute Deblock + Cleanup
190
191  m_cGopDecoder.filterPicture(pcPic);
192
193  TComSlice::sortPicList( m_cListPic ); // sorting for application output
194  poc                 = pcPic->getSlice(m_uiSliceIdx-1)->getPOC();
195  rpcListPic          = &m_cListPic; 
196  m_cCuDecoder.destroy();       
197  m_bFirstSliceInPicture  = true;
198
199  return;
200}
201
202Void TDecTop::xCreateLostPicture(Int iLostPoc) 
203{
204  printf("\ninserting lost poc : %d\n",iLostPoc);
205  TComSlice cFillSlice;
206  cFillSlice.setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
207  cFillSlice.setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
208  cFillSlice.initSlice();
209  TComPic *cFillPic;
210  xGetNewPicBuffer(&cFillSlice,cFillPic);
211  cFillPic->getSlice(0)->setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
212  cFillPic->getSlice(0)->setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
213  cFillPic->getSlice(0)->initSlice();
214 
215  TComList<TComPic*>::iterator iterPic = m_cListPic.begin();
216  Int closestPoc = 1000000;
217  while ( iterPic != m_cListPic.end())
218  {
219    TComPic * rpcPic = *(iterPic++);
220    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())
221    {
222      closestPoc=abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc);
223    }
224  }
225  iterPic = m_cListPic.begin();
226  while ( iterPic != m_cListPic.end())
227  {
228    TComPic *rpcPic = *(iterPic++);
229    if(abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc)==closestPoc&&rpcPic->getPicSym()->getSlice(0)->getPOC()!=m_apcSlicePilot->getPOC())
230    {
231      printf("copying picture %d to %d (%d)\n",rpcPic->getPicSym()->getSlice(0)->getPOC() ,iLostPoc,m_apcSlicePilot->getPOC());
232      rpcPic->getPicYuvRec()->copyToPic(cFillPic->getPicYuvRec());
233      break;
234    }
235  }
236  cFillPic->setCurrSliceIdx(0);
237  for(Int i=0; i<cFillPic->getNumCUsInFrame(); i++) 
238  {
239    cFillPic->getCU(i)->initCU(cFillPic,i);
240  }
241  cFillPic->getSlice(0)->setReferenced(true);
242  cFillPic->getSlice(0)->setPOC(iLostPoc);
243  cFillPic->setReconMark(true);
244  cFillPic->setOutputMark(true);
245  if(m_pocRandomAccess == MAX_INT)
246  {
247    m_pocRandomAccess = iLostPoc;
248  }
249}
250
251
252Void TDecTop::xActivateParameterSets()
253{
254  m_parameterSetManagerDecoder.applyPrefetchedPS();
255 
256  TComPPS *pps = m_parameterSetManagerDecoder.getPPS(m_apcSlicePilot->getPPSId());
257  assert (pps != 0);
258
259  TComSPS *sps = m_parameterSetManagerDecoder.getSPS(pps->getSPSId());
260  assert (sps != 0);
261
262  if (false == m_parameterSetManagerDecoder.activatePPS(m_apcSlicePilot->getPPSId(),m_apcSlicePilot->isIRAP()))
263  {
264    printf ("Parameter set activation failed!");
265    assert (0);
266  }
267
268  if( pps->getDependentSliceSegmentsEnabledFlag() )
269  {
270    Int NumCtx = pps->getEntropyCodingSyncEnabledFlag()?2:1;
271
272    if (m_cSliceDecoder.getCtxMemSize() != NumCtx)
273    {
274      m_cSliceDecoder.initCtxMem(NumCtx);
275      for ( UInt st = 0; st < NumCtx; st++ )
276      {
277        TDecSbac* ctx = NULL;
278        ctx = new TDecSbac;
279        ctx->init( &m_cBinCABAC );
280        m_cSliceDecoder.setCtxMem( ctx, st );
281      }
282    }
283  }
284
285  m_apcSlicePilot->setPPS(pps);
286  m_apcSlicePilot->setSPS(sps);
287  pps->setSPS(sps);
288  pps->setNumSubstreams(pps->getEntropyCodingSyncEnabledFlag() ? ((sps->getPicHeightInLumaSamples() + sps->getMaxCUHeight() - 1) / sps->getMaxCUHeight()) * (pps->getNumColumnsMinus1() + 1) : 1);
289  pps->setMinCuDQPSize( sps->getMaxCUWidth() >> ( pps->getMaxCuDQPDepth()) );
290
291  g_bitDepthY     = sps->getBitDepthY();
292  g_bitDepthC     = sps->getBitDepthC();
293  g_uiMaxCUWidth  = sps->getMaxCUWidth();
294  g_uiMaxCUHeight = sps->getMaxCUHeight();
295  g_uiMaxCUDepth  = sps->getMaxCUDepth();
296  g_uiAddCUDepth  = max (0, sps->getLog2MinCodingBlockSize() - (Int)sps->getQuadtreeTULog2MinSize() );
297
298  for (Int i = 0; i < sps->getLog2DiffMaxMinCodingBlockSize(); i++)
299  {
300    sps->setAMPAcc( i, sps->getUseAMP() );
301  }
302
303  for (Int i = sps->getLog2DiffMaxMinCodingBlockSize(); i < sps->getMaxCUDepth(); i++)
304  {
305    sps->setAMPAcc( i, 0 );
306  }
307
308  m_cSAO.destroy();
309  m_cSAO.create( sps->getPicWidthInLumaSamples(), sps->getPicHeightInLumaSamples(), sps->getMaxCUWidth(), sps->getMaxCUHeight() );
310  m_cLoopFilter.create( sps->getMaxCUDepth() );
311}
312
313Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay )
314{
315  TComPic*&   pcPic         = m_pcPic;
316  m_apcSlicePilot->initSlice();
317
318  if (m_bFirstSliceInPicture)
319  {
320    m_uiSliceIdx     = 0;
321  }
322  m_apcSlicePilot->setSliceIdx(m_uiSliceIdx);
323  if (!m_bFirstSliceInPicture)
324  {
325    m_apcSlicePilot->copySliceInfo( pcPic->getPicSym()->getSlice(m_uiSliceIdx-1) );
326  }
327
328  m_apcSlicePilot->setNalUnitType(nalu.m_nalUnitType);
329  Bool nonReferenceFlag = (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TRAIL_N ||
330                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N   ||
331                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N  ||
332                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N  ||
333                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N);
334  m_apcSlicePilot->setTemporalLayerNonReferenceFlag(nonReferenceFlag);
335 
336  m_apcSlicePilot->setReferenced(true); // Putting this as true ensures that picture is referenced the first time it is in an RPS
337  m_apcSlicePilot->setTLayerInfo(nalu.m_temporalId);
338
339  m_cEntropyDecoder.decodeSliceHeader (m_apcSlicePilot, &m_parameterSetManagerDecoder);
340
341    // Skip pictures due to random access
342    if (isRandomAccessSkipPicture(iSkipFrame, iPOCLastDisplay))
343    {
344      return false;
345    }
346    // Skip TFD pictures associated with BLA/BLANT pictures
347    if (isSkipPictureForBLA(iPOCLastDisplay))
348    {
349      return false;
350    }
351
352  //we should only get a different poc for a new picture (with CTU address==0)
353  if (m_apcSlicePilot->isNextSlice() && m_apcSlicePilot->getPOC()!=m_prevPOC && !m_bFirstSliceInSequence && (!m_apcSlicePilot->getSliceCurStartCUAddr()==0))
354  {
355    printf ("Warning, the first slice of a picture might have been lost!\n");
356  }
357  // exit when a new picture is found
358  if (m_apcSlicePilot->isNextSlice() && (m_apcSlicePilot->getSliceCurStartCUAddr() == 0 && !m_bFirstSliceInPicture) && !m_bFirstSliceInSequence )
359  {
360    if (m_prevPOC >= m_pocRandomAccess)
361    {
362      m_prevPOC = m_apcSlicePilot->getPOC();
363      return true;
364    }
365    m_prevPOC = m_apcSlicePilot->getPOC();
366  }
367  // actual decoding starts here
368  xActivateParameterSets();
369
370  if (m_apcSlicePilot->isNextSlice()) 
371  {
372    m_prevPOC = m_apcSlicePilot->getPOC();
373  }
374  m_bFirstSliceInSequence = false;
375  //detect lost reference picture and insert copy of earlier frame.
376  Int lostPoc;
377  while((lostPoc=m_apcSlicePilot->checkThatAllRefPicsAreAvailable(m_cListPic, m_apcSlicePilot->getRPS(), true, m_pocRandomAccess)) > 0)
378  {
379    xCreateLostPicture(lostPoc-1);
380  }
381  if (m_bFirstSliceInPicture)
382  {
383    // Buffer initialize for prediction.
384    m_cPrediction.initTempBuff();
385    m_apcSlicePilot->applyReferencePictureSet(m_cListPic, m_apcSlicePilot->getRPS());
386    //  Get a new picture buffer
387    xGetNewPicBuffer (m_apcSlicePilot, pcPic);
388
389    // transfer any SEI messages that have been received to the picture
390    pcPic->setSEIs(m_SEIs);
391    m_SEIs.clear();
392
393    // Recursive structure
394    m_cCuDecoder.create ( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight );
395    m_cCuDecoder.init   ( &m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction );
396    m_cTrQuant.init     ( g_uiMaxCUWidth, g_uiMaxCUHeight, m_apcSlicePilot->getSPS()->getMaxTrSize());
397
398    m_cSliceDecoder.create();
399  }
400  else
401  {
402    // Check if any new SEI has arrived
403    if(!m_SEIs.empty())
404    {
405      // Currently only decoding Unit SEI message occurring between VCL NALUs copied
406      SEIMessages &picSEI = pcPic->getSEIs();
407      SEIMessages decodingUnitInfos = extractSeisByType (m_SEIs, SEI::DECODING_UNIT_INFO);
408      picSEI.insert(picSEI.end(), decodingUnitInfos.begin(), decodingUnitInfos.end());
409      deleteSEIs(m_SEIs);
410    }
411  }
412 
413  //  Set picture slice pointer
414  TComSlice*  pcSlice = m_apcSlicePilot;
415  Bool bNextSlice     = pcSlice->isNextSlice();
416
417  UInt uiCummulativeTileWidth;
418  UInt uiCummulativeTileHeight;
419  UInt i, j, p;
420
421  //set NumColumnsMins1 and NumRowsMinus1
422  pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getPPS()->getNumColumnsMinus1() );
423  pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getPPS()->getNumRowsMinus1() );
424
425  //create the TComTileArray
426  pcPic->getPicSym()->xCreateTComTileArray();
427
428  if( pcSlice->getPPS()->getUniformSpacingFlag() )
429  {
430    //set the width for each tile
431    for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
432    {
433      for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++)
434      {
435        pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->
436          setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1) 
437          - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) );
438      }
439    }
440
441    //set the height for each tile
442    for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
443    {
444      for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++)
445      {
446        pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->
447          setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1) 
448          - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) );   
449      }
450    }
451  }
452  else
453  {
454    //set the width for each tile
455    for(j=0; j < pcSlice->getPPS()->getNumRowsMinus1()+1; j++)
456    {
457      uiCummulativeTileWidth = 0;
458      for(i=0; i < pcSlice->getPPS()->getNumColumnsMinus1(); i++)
459      {
460        pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcSlice->getPPS()->getColumnWidth(i) );
461        uiCummulativeTileWidth += pcSlice->getPPS()->getColumnWidth(i);
462      }
463      pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth );
464    }
465
466    //set the height for each tile
467    for(j=0; j < pcSlice->getPPS()->getNumColumnsMinus1()+1; j++)
468    {
469      uiCummulativeTileHeight = 0;
470      for(i=0; i < pcSlice->getPPS()->getNumRowsMinus1(); i++)
471      { 
472        pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcSlice->getPPS()->getRowHeight(i) );
473        uiCummulativeTileHeight += pcSlice->getPPS()->getRowHeight(i);
474      }
475      pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight );
476    }
477  }
478
479  pcPic->getPicSym()->xInitTiles();
480
481  //generate the Coding Order Map and Inverse Coding Order Map
482  UInt uiEncCUAddr;
483  for(i=0, uiEncCUAddr=0; i<pcPic->getPicSym()->getNumberOfCUsInFrame(); i++, uiEncCUAddr = pcPic->getPicSym()->xCalculateNxtCUAddr(uiEncCUAddr))
484  {
485    pcPic->getPicSym()->setCUOrderMap(i, uiEncCUAddr);
486    pcPic->getPicSym()->setInverseCUOrderMap(uiEncCUAddr, i);
487  }
488  pcPic->getPicSym()->setCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
489  pcPic->getPicSym()->setInverseCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
490
491  //convert the start and end CU addresses of the slice and dependent slice into encoding order
492  pcSlice->setSliceSegmentCurStartCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurStartCUAddr()) );
493  pcSlice->setSliceSegmentCurEndCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurEndCUAddr()) );
494  if(pcSlice->isNextSlice())
495  {
496    pcSlice->setSliceCurStartCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurStartCUAddr()));
497    pcSlice->setSliceCurEndCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurEndCUAddr()));
498  }
499
500  if (m_bFirstSliceInPicture) 
501  {
502    if(pcPic->getNumAllocatedSlice() != 1)
503    {
504      pcPic->clearSliceBuffer();
505    }
506  }
507  else
508  {
509    pcPic->allocateNewSlice();
510  }
511  assert(pcPic->getNumAllocatedSlice() == (m_uiSliceIdx + 1));
512  m_apcSlicePilot = pcPic->getPicSym()->getSlice(m_uiSliceIdx); 
513  pcPic->getPicSym()->setSlice(pcSlice, m_uiSliceIdx);
514
515  pcPic->setTLayer(nalu.m_temporalId);
516
517  if (bNextSlice)
518  {
519    pcSlice->checkCRA(pcSlice->getRPS(), m_pocCRA, m_prevRAPisBLA, m_cListPic );
520    // Set reference list
521#if FIX1071
522    pcSlice->setRefPicList( m_cListPic, true );
523#else
524    pcSlice->setRefPicList( m_cListPic );
525#endif
526
527    // For generalized B
528    // note: maybe not existed case (always L0 is copied to L1 if L1 is empty)
529    if (pcSlice->isInterB() && pcSlice->getNumRefIdx(REF_PIC_LIST_1) == 0)
530    {
531      Int iNumRefIdx = pcSlice->getNumRefIdx(REF_PIC_LIST_0);
532      pcSlice->setNumRefIdx        ( REF_PIC_LIST_1, iNumRefIdx );
533
534      for (Int iRefIdx = 0; iRefIdx < iNumRefIdx; iRefIdx++)
535      {
536        pcSlice->setRefPic(pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx), REF_PIC_LIST_1, iRefIdx);
537      }
538    }
539    if (!pcSlice->isIntra())
540    {
541      Bool bLowDelay = true;
542      Int  iCurrPOC  = pcSlice->getPOC();
543      Int iRefIdx = 0;
544
545      for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
546      {
547        if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
548        {
549          bLowDelay = false;
550        }
551      }
552      if (pcSlice->isInterB())
553      {
554        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
555        {
556          if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
557          {
558            bLowDelay = false;
559          }
560        }       
561      }
562
563      pcSlice->setCheckLDC(bLowDelay);           
564    }
565
566    //---------------
567    pcSlice->setRefPOCList();
568#if !L0034_COMBINED_LIST_CLEANUP
569    pcSlice->setNoBackPredFlag( false );
570    if ( pcSlice->getSliceType() == B_SLICE )
571    {
572      if ( pcSlice->getNumRefIdx(RefPicList( 0 ) ) == pcSlice->getNumRefIdx(RefPicList( 1 ) ) )
573      {
574        pcSlice->setNoBackPredFlag( true );
575        for ( i=0; i < pcSlice->getNumRefIdx(RefPicList( 1 ) ); i++ )
576        {
577          if ( pcSlice->getRefPOC(RefPicList(1), i) != pcSlice->getRefPOC(RefPicList(0), i) ) 
578          {
579            pcSlice->setNoBackPredFlag( false );
580            break;
581          }
582        }
583      }
584    }
585#endif
586  }
587
588  pcPic->setCurrSliceIdx(m_uiSliceIdx);
589  if(pcSlice->getSPS()->getScalingListFlag())
590  {
591    pcSlice->setScalingList ( pcSlice->getSPS()->getScalingList()  );
592    if(pcSlice->getPPS()->getScalingListPresentFlag())
593    {
594      pcSlice->setScalingList ( pcSlice->getPPS()->getScalingList()  );
595    }
596    pcSlice->getScalingList()->setUseTransformSkip(pcSlice->getPPS()->getUseTransformSkip());
597    if(!pcSlice->getPPS()->getScalingListPresentFlag() && !pcSlice->getSPS()->getScalingListPresentFlag())
598    {
599      pcSlice->setDefaultScalingList();
600    }
601    m_cTrQuant.setScalingListDec(pcSlice->getScalingList());
602    m_cTrQuant.setUseScalingList(true);
603  }
604  else
605  {
606    m_cTrQuant.setFlatScalingList();
607    m_cTrQuant.setUseScalingList(false);
608  }
609
610  //  Decode a picture
611  m_cGopDecoder.decompressSlice(nalu.m_Bitstream, pcPic);
612
613  m_bFirstSliceInPicture = false;
614  m_uiSliceIdx++;
615
616  return false;
617}
618
619Void TDecTop::xDecodeVPS()
620{
621  TComVPS* vps = new TComVPS();
622 
623  m_cEntropyDecoder.decodeVPS( vps );
624  m_parameterSetManagerDecoder.storePrefetchedVPS(vps); 
625}
626
627Void TDecTop::xDecodeSPS()
628{
629  TComSPS* sps = new TComSPS();
630  m_cEntropyDecoder.decodeSPS( sps );
631  m_parameterSetManagerDecoder.storePrefetchedSPS(sps);
632}
633
634Void TDecTop::xDecodePPS()
635{
636  TComPPS* pps = new TComPPS();
637  m_cEntropyDecoder.decodePPS( pps );
638  m_parameterSetManagerDecoder.storePrefetchedPPS( pps );
639}
640
641Void TDecTop::xDecodeSEI( TComInputBitstream* bs, const NalUnitType nalUnitType )
642{
643  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
644  {
645    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
646  }
647  else
648  {
649    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
650    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
651    if (activeParamSets.size()>0)
652    {
653      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
654      m_parameterSetManagerDecoder.applyPrefetchedPS();
655      assert(seiAps->activeSeqParamSetId.size()>0);
656      if (! m_parameterSetManagerDecoder.activateSPSWithSEI(seiAps->activeSeqParamSetId[0] ))
657      {
658        printf ("Warning SPS activation with Active parameter set SEI failed");
659      }
660    }
661  }
662}
663
664Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay)
665{
666  // Initialize entropy decoder
667  m_cEntropyDecoder.setEntropyDecoder (&m_cCavlcDecoder);
668  m_cEntropyDecoder.setBitstream      (nalu.m_Bitstream);
669
670  switch (nalu.m_nalUnitType)
671  {
672    case NAL_UNIT_VPS:
673      xDecodeVPS();
674      return false;
675     
676    case NAL_UNIT_SPS:
677      xDecodeSPS();
678      return false;
679
680    case NAL_UNIT_PPS:
681      xDecodePPS();
682      return false;
683     
684    case NAL_UNIT_PREFIX_SEI:
685    case NAL_UNIT_SUFFIX_SEI:
686      xDecodeSEI( nalu.m_Bitstream, nalu.m_nalUnitType );
687      return false;
688
689    case NAL_UNIT_CODED_SLICE_TRAIL_R:
690    case NAL_UNIT_CODED_SLICE_TRAIL_N:
691    case NAL_UNIT_CODED_SLICE_TLA_R:
692    case NAL_UNIT_CODED_SLICE_TSA_N:
693    case NAL_UNIT_CODED_SLICE_STSA_R:
694    case NAL_UNIT_CODED_SLICE_STSA_N:
695    case NAL_UNIT_CODED_SLICE_BLA_W_LP:
696    case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
697    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
698    case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
699    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
700    case NAL_UNIT_CODED_SLICE_CRA:
701    case NAL_UNIT_CODED_SLICE_RADL_N:
702    case NAL_UNIT_CODED_SLICE_RADL_R:
703    case NAL_UNIT_CODED_SLICE_RASL_N:
704    case NAL_UNIT_CODED_SLICE_RASL_R:
705      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay);
706      break;
707    default:
708      assert (1);
709  }
710
711  return false;
712}
713
714/** Function for checking if picture should be skipped because of association with a previous BLA picture
715 * \param iPOCLastDisplay POC of last picture displayed
716 * \returns true if the picture should be skipped
717 * This function skips all TFD pictures that follow a BLA picture
718 * in decoding order and precede it in output order.
719 */
720Bool TDecTop::isSkipPictureForBLA(Int& iPOCLastDisplay)
721{
722  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))
723  {
724    iPOCLastDisplay++;
725    return true;
726  }
727  return false;
728}
729
730/** Function for checking if picture should be skipped because of random access
731 * \param iSkipFrame skip frame counter
732 * \param iPOCLastDisplay POC of last picture displayed
733 * \returns true if the picture shold be skipped in the random access.
734 * This function checks the skipping of pictures in the case of -s option random access.
735 * All pictures prior to the random access point indicated by the counter iSkipFrame are skipped.
736 * It also checks the type of Nal unit type at the random access point.
737 * 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.
738 * If the random access point is IDR all pictures after the random access point are decoded.
739 * If the random access point is none of the above, a warning is issues, and decoding of pictures with POC
740 * equal to or greater than the random access point POC is attempted. For non IDR/CRA/BLA random
741 * access point there is no guarantee that the decoder will not crash.
742 */
743Bool TDecTop::isRandomAccessSkipPicture(Int& iSkipFrame,  Int& iPOCLastDisplay)
744{
745  if (iSkipFrame) 
746  {
747    iSkipFrame--;   // decrement the counter
748    return true;
749  }
750  else if (m_pocRandomAccess == MAX_INT) // start of random access point, m_pocRandomAccess has not been set yet.
751  {
752    if (   m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA
753        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
754        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
755        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL )
756    {
757      // set the POC random access since we need to skip the reordered pictures in the case of CRA/CRANT/BLA/BLANT.
758      m_pocRandomAccess = m_apcSlicePilot->getPOC();
759    }
760    else if ( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
761    {
762      m_pocRandomAccess = -MAX_INT; // no need to skip the reordered pictures in IDR, they are decodable.
763    }
764    else 
765    {
766      static Bool warningMessage = false;
767      if(!warningMessage)
768      {
769        printf("\nWarning: this is not a valid random access point and the data is discarded until the first CRA picture");
770        warningMessage = true;
771      }
772      return true;
773    }
774  }
775  // skip the reordered pictures, if necessary
776  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))
777  {
778    iPOCLastDisplay++;
779    return true;
780  }
781  // if we reach here, then the picture is not skipped.
782  return false; 
783}
784
785//! \}
Note: See TracBrowser for help on using the repository browser.