source: 3DVCSoftware/tags/HTM-DEV-0.1/source/Lib/TLibDecoder/TDecTop.cpp

Last change on this file was 324, checked in by tech, 11 years ago

Initial development version for update to latest HM version.
Includes MV-HEVC and basic extensions for 3D-HEVC.

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