source: SHVCSoftware/branches/SHM-5.1-dev/source/Lib/TLibDecoder/TDecTop.cpp @ 636

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

remove parsing dependency in SPS, ticket #17

  • Property svn:eol-style set to native
File size: 84.6 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2014, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     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_associatedIRAPType = NAL_UNIT_INVALID;
60  m_pocCRA = 0;
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  memset(m_cIlpPic, 0, sizeof(m_cIlpPic));
75#endif
76#if AVC_SYNTAX || SYNTAX_OUTPUT
77  m_pBLSyntaxFile = NULL;
78#endif
79  m_prevSliceSkipped = false;
80  m_skippedPOC = 0;
81#if NO_CLRAS_OUTPUT_FLAG
82  m_noClrasOutputFlag          = false;
83  m_layerInitializedFlag       = false;
84  m_firstPicInLayerDecodedFlag = false;
85  m_noOutputOfPriorPicsFlags   = false;
86  m_bRefreshPending            = false;
87#endif
88#if RESOLUTION_BASED_DPB
89  m_subDpbIdx = -1;
90#endif
91}
92
93TDecTop::~TDecTop()
94{
95#if ENC_DEC_TRACE
96  fclose( g_hTrace );
97#endif
98}
99
100Void TDecTop::create()
101{
102#if SVC_EXTENSION
103  m_cGopDecoder.create( m_layerId );
104#else
105  m_cGopDecoder.create();
106#endif
107  m_apcSlicePilot = new TComSlice;
108  m_uiSliceIdx = 0;
109}
110
111Void TDecTop::destroy()
112{
113  m_cGopDecoder.destroy();
114 
115  delete m_apcSlicePilot;
116  m_apcSlicePilot = NULL;
117 
118  m_cSliceDecoder.destroy();
119#if SVC_EXTENSION
120  for(Int i=0; i<MAX_NUM_REF; i++)
121  {
122    if(m_cIlpPic[i])
123    {
124      m_cIlpPic[i]->destroy();
125      delete m_cIlpPic[i];
126      m_cIlpPic[i] = NULL;
127    }
128  }   
129#endif
130}
131
132Void TDecTop::init()
133{
134#if !SVC_EXTENSION
135  // initialize ROM
136  initROM();
137#endif
138#if SVC_EXTENSION
139  m_cGopDecoder.init( m_ppcTDecTop, &m_cEntropyDecoder, &m_cSbacDecoder, &m_cBinCABAC, &m_cCavlcDecoder, &m_cSliceDecoder, &m_cLoopFilter, &m_cSAO);
140  m_cSliceDecoder.init( &m_cEntropyDecoder, &m_cCuDecoder, m_cSAO.getSaoMaxOffsetQVal() );
141#else
142  m_cGopDecoder.init( &m_cEntropyDecoder, &m_cSbacDecoder, &m_cBinCABAC, &m_cCavlcDecoder, &m_cSliceDecoder, &m_cLoopFilter, &m_cSAO);
143  m_cSliceDecoder.init( &m_cEntropyDecoder, &m_cCuDecoder );
144#endif
145  m_cEntropyDecoder.init(&m_cPrediction);
146}
147
148#if SVC_EXTENSION
149#if !REPN_FORMAT_IN_VPS
150Void TDecTop::xInitILRP(TComSPS *pcSPS)
151#else
152Void TDecTop::xInitILRP(TComSlice *slice)
153#endif
154{
155#if REPN_FORMAT_IN_VPS
156  TComSPS* pcSPS = slice->getSPS();
157  Int bitDepthY   = slice->getBitDepthY();
158  Int bitDepthC   = slice->getBitDepthC();
159  Int picWidth    = slice->getPicWidthInLumaSamples();
160  Int picHeight   = slice->getPicHeightInLumaSamples();
161#endif
162  if(m_layerId>0)
163  {
164#if REPN_FORMAT_IN_VPS
165    g_bitDepthY     = bitDepthY;
166    g_bitDepthC     = bitDepthC;
167#else
168    g_bitDepthY     = pcSPS->getBitDepthY();
169    g_bitDepthC     = pcSPS->getBitDepthC();
170#endif
171    g_uiMaxCUWidth  = pcSPS->getMaxCUWidth();
172    g_uiMaxCUHeight = pcSPS->getMaxCUHeight();
173    g_uiMaxCUDepth  = pcSPS->getMaxCUDepth();
174    g_uiAddCUDepth  = max (0, pcSPS->getLog2MinCodingBlockSize() - (Int)pcSPS->getQuadtreeTULog2MinSize() );
175
176    Int  numReorderPics[MAX_TLAYER];
177    Window &conformanceWindow = pcSPS->getConformanceWindow();
178    Window defaultDisplayWindow = pcSPS->getVuiParametersPresentFlag() ? pcSPS->getVuiParameters()->getDefaultDisplayWindow() : Window();
179
180    for( Int temporalLayer=0; temporalLayer < MAX_TLAYER; temporalLayer++) 
181    {
182#if USE_DPB_SIZE_TABLE
183      if( getCommonDecoderParams()->getTargetOutputLayerSetIdx() == 0 )
184      {
185        assert( this->getLayerId() == 0 );
186        numReorderPics[temporalLayer] = pcSPS->getNumReorderPics(temporalLayer);
187      }
188      else
189      {
190        TComVPS *vps = slice->getVPS();
191        // SHM decoders will use DPB size table in the VPS to determine the number of reorder pictures.
192        numReorderPics[temporalLayer] = vps->getMaxVpsNumReorderPics( getCommonDecoderParams()->getTargetOutputLayerSetIdx() , temporalLayer);
193      }
194#else
195      numReorderPics[temporalLayer] = pcSPS->getNumReorderPics(temporalLayer);
196#endif
197    }
198
199    if (m_cIlpPic[0] == NULL)
200    {
201      for (Int j=0; j < m_numDirectRefLayers; j++)
202      {
203
204        m_cIlpPic[j] = new  TComPic;
205#if AUXILIARY_PICTURES
206#if REPN_FORMAT_IN_VPS
207#if SVC_UPSAMPLING
208        m_cIlpPic[j]->create(picWidth, picHeight, slice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
209#else
210        m_cIlpPic[j]->create(picWidth, picHeight, slice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, true);
211#endif
212#else
213#if SVC_UPSAMPLING
214        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), pcSPS->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
215#else
216        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), pcSPS->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, true);
217#endif
218#endif
219#else
220#if REPN_FORMAT_IN_VPS
221#if SVC_UPSAMPLING
222        m_cIlpPic[j]->create(picWidth, picHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
223#else
224        m_cIlpPic[j]->create(picWidth, picHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, true);
225#endif
226#else
227#if SVC_UPSAMPLING
228        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
229#else
230        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, true);
231#endif
232#endif
233#endif
234        for (Int i=0; i<m_cIlpPic[j]->getPicSym()->getNumberOfCUsInFrame(); i++)
235        {
236          m_cIlpPic[j]->getPicSym()->getCU(i)->initCU(m_cIlpPic[j], i);
237        }
238      }
239    }
240  }
241}
242#endif
243
244Void TDecTop::deletePicBuffer ( )
245{
246  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
247  Int iSize = Int( m_cListPic.size() );
248 
249  for (Int i = 0; i < iSize; i++ )
250  {
251    TComPic* pcPic = *(iterPic++);
252#if SVC_EXTENSION
253    if( pcPic )
254    {
255      pcPic->destroy();
256
257      delete pcPic;
258      pcPic = NULL;
259    }
260#else
261    pcPic->destroy();
262   
263    delete pcPic;
264    pcPic = NULL;
265#endif
266  }
267 
268  m_cSAO.destroy();
269 
270  m_cLoopFilter.        destroy();
271 
272#if !SVC_EXTENSION
273  // destroy ROM
274  destroyROM();
275#endif
276}
277
278Void TDecTop::xGetNewPicBuffer ( TComSlice* pcSlice, TComPic*& rpcPic )
279{
280  Int  numReorderPics[MAX_TLAYER];
281  Window &conformanceWindow = pcSlice->getSPS()->getConformanceWindow();
282  Window defaultDisplayWindow = pcSlice->getSPS()->getVuiParametersPresentFlag() ? pcSlice->getSPS()->getVuiParameters()->getDefaultDisplayWindow() : Window();
283
284  for( Int temporalLayer=0; temporalLayer < MAX_TLAYER; temporalLayer++) 
285  {
286#if USE_DPB_SIZE_TABLE
287    if( getCommonDecoderParams()->getTargetOutputLayerSetIdx() == 0 )
288    {
289      assert( this->getLayerId() == 0 );
290      numReorderPics[temporalLayer] = pcSlice->getSPS()->getNumReorderPics(temporalLayer);
291    }
292    else
293    {
294      TComVPS *vps = pcSlice->getVPS();
295      // SHM decoders will use DPB size table in the VPS to determine the number of reorder pictures.
296      numReorderPics[temporalLayer] = vps->getMaxVpsNumReorderPics( getCommonDecoderParams()->getTargetOutputLayerSetIdx() , temporalLayer);
297    }
298#else
299    numReorderPics[temporalLayer] = pcSlice->getSPS()->getNumReorderPics(temporalLayer);
300#endif
301  }
302
303#if USE_DPB_SIZE_TABLE
304  if( getCommonDecoderParams()->getTargetOutputLayerSetIdx() == 0 )
305  {
306    assert( this->getLayerId() == 0 );
307    m_iMaxRefPicNum = pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getTLayer());     // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
308  }
309  else
310  {
311#if RESOLUTION_BASED_DPB
312    Int layerSetIdxForOutputLayerSet = pcSlice->getVPS()->getOutputLayerSetIdx( getCommonDecoderParams()->getTargetOutputLayerSetIdx() );
313    Int layerIdx = pcSlice->getVPS()->findLayerIdxInLayerSet( layerSetIdxForOutputLayerSet, pcSlice->getLayerId() );  assert( layerIdx != -1 );
314    m_iMaxRefPicNum = pcSlice->getVPS()->getMaxVpsLayerDecPicBuffMinus1( getCommonDecoderParams()->getTargetOutputLayerSetIdx(), layerIdx, pcSlice->getTLayer() ) + 1; // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
315#else
316    m_iMaxRefPicNum = pcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1( getCommonDecoderParams()->getTargetOutputLayerSetIdx(), pcSlice->getLayerId(), pcSlice->getTLayer() ) + 1; // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
317#endif
318  }
319#else
320  m_iMaxRefPicNum = pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getTLayer());     // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
321#endif
322
323#if SVC_EXTENSION
324  m_iMaxRefPicNum += 1; // it should be updated if more than 1 resampling picture is used
325#endif
326
327  if (m_cListPic.size() < (UInt)m_iMaxRefPicNum)
328  {
329    rpcPic = new TComPic();
330
331#if SVC_EXTENSION //Temporal solution, should be modified
332    if(m_layerId > 0)
333    {
334      for(UInt i = 0; i < pcSlice->getVPS()->getNumDirectRefLayers( m_layerId ); i++ )
335      {
336#if O0098_SCALED_REF_LAYER_ID
337        const Window scalEL = pcSlice->getSPS()->getScaledRefLayerWindowForLayer(pcSlice->getVPS()->getRefLayerId(m_layerId, i));
338#else
339        const Window scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(i);
340#endif
341        Bool zeroOffsets = ( scalEL.getWindowLeftOffset() == 0 && scalEL.getWindowRightOffset() == 0 && scalEL.getWindowTopOffset() == 0 && scalEL.getWindowBottomOffset() == 0 );
342
343#if VPS_EXTN_DIRECT_REF_LAYERS
344        TDecTop *pcTDecTopBase = (TDecTop *)getRefLayerDec( i );
345#else
346        TDecTop *pcTDecTopBase = (TDecTop *)getLayerDec( m_layerId-1 );
347#endif
348        //TComPic*                      pcPic = *(pcTDecTopBase->getListPic()->begin());
349        TComPicYuv* pcPicYuvRecBase = (*(pcTDecTopBase->getListPic()->begin()))->getPicYuvRec(); 
350#if REPN_FORMAT_IN_VPS
351        if(pcPicYuvRecBase->getWidth() != pcSlice->getPicWidthInLumaSamples() || pcPicYuvRecBase->getHeight() != pcSlice->getPicHeightInLumaSamples() || !zeroOffsets )
352#else
353        if(pcPicYuvRecBase->getWidth() != pcSlice->getSPS()->getPicWidthInLumaSamples() || pcPicYuvRecBase->getHeight() != pcSlice->getSPS()->getPicHeightInLumaSamples() || !zeroOffsets )
354#endif
355        {
356          rpcPic->setSpatialEnhLayerFlag( i, true );
357
358          //only for scalable extension
359#if SCALABILITY_MASK_E0104
360          assert( pcSlice->getVPS()->getScalabilityMask(2) == true );
361#else
362          assert( pcSlice->getVPS()->getScalabilityMask(1) == true );
363#endif
364        }
365      }
366    }
367#endif
368   
369#if AUXILIARY_PICTURES
370#if REPN_FORMAT_IN_VPS
371#if SVC_UPSAMPLING
372    rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), pcSlice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
373                     conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
374#else
375    rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), pcSlice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
376                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
377#endif
378#else
379#if SVC_UPSAMPLING
380    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
381                     conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
382#else
383    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
384                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
385#endif
386#endif
387#else
388#if REPN_FORMAT_IN_VPS
389#if SVC_UPSAMPLING
390    rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
391                     conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
392#else
393    rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
394                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
395#endif
396#else
397#if SVC_UPSAMPLING
398    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
399                     conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
400#else
401    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
402                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
403#endif
404#endif
405#endif
406    m_cListPic.pushBack( rpcPic );
407   
408    return;
409  }
410 
411  Bool bBufferIsAvailable = false;
412  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
413  while (iterPic != m_cListPic.end())
414  {
415    rpcPic = *(iterPic++);
416    if ( rpcPic->getReconMark() == false && rpcPic->getOutputMark() == false)
417    {
418      rpcPic->setOutputMark(false);
419      bBufferIsAvailable = true;
420      break;
421    }
422
423    if ( rpcPic->getSlice( 0 )->isReferenced() == false  && rpcPic->getOutputMark() == false)
424    {
425#if !SVC_EXTENSION
426      rpcPic->setOutputMark(false);
427#endif
428      rpcPic->setReconMark( false );
429      rpcPic->getPicYuvRec()->setBorderExtension( false );
430      bBufferIsAvailable = true;
431      break;
432    }
433  }
434 
435  if ( !bBufferIsAvailable )
436  {
437    //There is no room for this picture, either because of faulty encoder or dropped NAL. Extend the buffer.
438    m_iMaxRefPicNum++;
439    rpcPic = new TComPic();
440    m_cListPic.pushBack( rpcPic );
441  }
442  rpcPic->destroy();
443
444#if AUXILIARY_PICTURES
445#if REPN_FORMAT_IN_VPS
446#if SVC_UPSAMPLING
447  rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), pcSlice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
448                   conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
449
450#else
451  rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), pcSlice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
452                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
453#endif
454#else
455#if SVC_UPSAMPLING
456  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
457                   conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
458
459#else
460  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
461                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
462#endif
463#endif
464#else
465#if REPN_FORMAT_IN_VPS
466#if SVC_UPSAMPLING
467  rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
468                   conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
469
470#else
471  rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
472                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
473#endif
474#else
475#if SVC_UPSAMPLING
476  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
477                   conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
478
479#else
480  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
481                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
482#endif
483#endif
484#endif
485}
486
487Void TDecTop::executeLoopFilters(Int& poc, TComList<TComPic*>*& rpcListPic)
488{
489  if (!m_pcPic)
490  {
491    /* nothing to deblock */
492    return;
493  }
494 
495  TComPic*&   pcPic         = m_pcPic;
496
497  // Execute Deblock + Cleanup
498
499  m_cGopDecoder.filterPicture(pcPic);
500
501#if SYNTAX_OUTPUT
502  pcPic->wrireBLSyntax( getBLSyntaxFile(), SYNTAX_BYTES );
503#endif
504  TComSlice::sortPicList( m_cListPic ); // sorting for application output
505  poc                 = pcPic->getSlice(m_uiSliceIdx-1)->getPOC();
506  rpcListPic          = &m_cListPic; 
507  m_cCuDecoder.destroy();       
508  m_bFirstSliceInPicture  = true;
509
510  return;
511}
512
513#if EARLY_REF_PIC_MARKING
514Void TDecTop::earlyPicMarking(Int maxTemporalLayer, std::vector<Int>& targetDecLayerIdSet)
515{
516  UInt currTid = m_pcPic->getTLayer();
517  UInt highestTid = (maxTemporalLayer >= 0) ? maxTemporalLayer : (m_pcPic->getSlice(0)->getSPS()->getMaxTLayers() - 1);
518  UInt latestDecLayerId = m_layerId;
519  UInt numTargetDecLayers = 0;
520  Int targetDecLayerIdList[MAX_LAYERS];
521  UInt latestDecIdx = 0;
522  TComSlice* pcSlice = m_pcPic->getSlice(0);
523
524  if ( currTid != highestTid )  // Marking  process is only applicaple for highest decoded TLayer
525  {
526    return;
527  }
528
529  // currPic must be marked as "used for reference" and must be a sub-layer non-reference picture
530  if ( !((pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_TRAIL_N  ||
531          pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N    ||
532          pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N   ||
533          pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N   ||
534          pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N   ||
535          pcSlice->getNalUnitType() == NAL_UNIT_RESERVED_VCL_N10     ||
536          pcSlice->getNalUnitType() == NAL_UNIT_RESERVED_VCL_N12     ||
537          pcSlice->getNalUnitType() == NAL_UNIT_RESERVED_VCL_N14) && pcSlice->isReferenced()))
538  {
539    return;
540  }
541
542  if ( targetDecLayerIdSet.size() == 0 ) // Cannot mark if we don't know the number of scalable layers
543  {
544    return;
545  }
546
547  for (std::vector<Int>::iterator it = targetDecLayerIdSet.begin(); it != targetDecLayerIdSet.end(); it++)
548  {
549    if ( latestDecLayerId == (*it) )
550    {
551      latestDecIdx = numTargetDecLayers;
552    }
553    targetDecLayerIdList[numTargetDecLayers++] = (*it);
554  }
555
556  Int remainingInterLayerReferencesFlag = 0;
557#if O0225_MAX_TID_FOR_REF_LAYERS
558  for ( Int j = latestDecIdx + 1; j < numTargetDecLayers; j++ )
559  {
560    Int jLidx = pcSlice->getVPS()->getLayerIdInVps(targetDecLayerIdList[j]);
561    if ( currTid <= pcSlice->getVPS()->getMaxTidIlRefPicsPlus1(latestDecLayerId,jLidx) - 1 )
562    {
563#else
564  if ( currTid <= pcSlice->getVPS()->getMaxTidIlRefPicsPlus1(latestDecLayerId) - 1 )
565  {
566    for ( Int j = latestDecIdx + 1; j < numTargetDecLayers; j++ )
567    {
568#endif
569      for ( Int k = 0; k < m_ppcTDecTop[targetDecLayerIdList[j]]->getNumDirectRefLayers(); k++ )
570      {
571        if ( latestDecIdx == m_ppcTDecTop[targetDecLayerIdList[j]]->getRefLayerId(k) )
572        {
573          remainingInterLayerReferencesFlag = 1;
574        }
575      }
576    }
577  }
578
579  if ( remainingInterLayerReferencesFlag == 0 )
580  {
581    pcSlice->setReferenced(false);
582  }
583}
584#endif
585
586Void TDecTop::xCreateLostPicture(Int iLostPoc) 
587{
588  printf("\ninserting lost poc : %d\n",iLostPoc);
589  TComSlice cFillSlice;
590  cFillSlice.setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
591  cFillSlice.setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
592#if SVC_EXTENSION
593  cFillSlice.setVPS( m_parameterSetManagerDecoder.getFirstVPS() );
594  cFillSlice.initSlice( m_layerId );
595#else
596  cFillSlice.initSlice();
597#endif
598  TComPic *cFillPic;
599  xGetNewPicBuffer(&cFillSlice,cFillPic);
600  cFillPic->getSlice(0)->setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
601  cFillPic->getSlice(0)->setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
602#if SVC_EXTENSION
603  cFillPic->getSlice(0)->setVPS( m_parameterSetManagerDecoder.getFirstVPS() );
604  cFillPic->getSlice(0)->initSlice( m_layerId );
605#else
606  cFillPic->getSlice(0)->initSlice();
607#endif
608 
609  TComList<TComPic*>::iterator iterPic = m_cListPic.begin();
610  Int closestPoc = 1000000;
611  while ( iterPic != m_cListPic.end())
612  {
613    TComPic * rpcPic = *(iterPic++);
614    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())
615    {
616      closestPoc=abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc);
617    }
618  }
619  iterPic = m_cListPic.begin();
620  while ( iterPic != m_cListPic.end())
621  {
622    TComPic *rpcPic = *(iterPic++);
623    if(abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc)==closestPoc&&rpcPic->getPicSym()->getSlice(0)->getPOC()!=m_apcSlicePilot->getPOC())
624    {
625      printf("copying picture %d to %d (%d)\n",rpcPic->getPicSym()->getSlice(0)->getPOC() ,iLostPoc,m_apcSlicePilot->getPOC());
626      rpcPic->getPicYuvRec()->copyToPic(cFillPic->getPicYuvRec());
627      break;
628    }
629  }
630  cFillPic->setCurrSliceIdx(0);
631  for(Int i=0; i<cFillPic->getNumCUsInFrame(); i++) 
632  {
633    cFillPic->getCU(i)->initCU(cFillPic,i);
634  }
635  cFillPic->getSlice(0)->setReferenced(true);
636  cFillPic->getSlice(0)->setPOC(iLostPoc);
637  cFillPic->setReconMark(true);
638  cFillPic->setOutputMark(true);
639  if(m_pocRandomAccess == MAX_INT)
640  {
641    m_pocRandomAccess = iLostPoc;
642  }
643}
644
645
646Void TDecTop::xActivateParameterSets()
647{
648  m_parameterSetManagerDecoder.applyPrefetchedPS();
649 
650  TComPPS *pps = m_parameterSetManagerDecoder.getPPS(m_apcSlicePilot->getPPSId());
651  assert (pps != 0);
652
653  TComSPS *sps = m_parameterSetManagerDecoder.getSPS(pps->getSPSId());
654  assert (sps != 0);
655
656  if (false == m_parameterSetManagerDecoder.activatePPS(m_apcSlicePilot->getPPSId(),m_apcSlicePilot->isIRAP()))
657  {
658    printf ("Parameter set activation failed!");
659    assert (0);
660  }
661
662#if SCALINGLIST_INFERRING
663  // scaling list settings and checks
664  TComVPS *activeVPS = m_parameterSetManagerDecoder.getActiveVPS();
665  TComSPS *activeSPS = m_parameterSetManagerDecoder.getActiveSPS();
666  TComPPS *activePPS = m_parameterSetManagerDecoder.getActivePPS();
667
668  if( activeSPS->getInferScalingListFlag() )
669  {
670    UInt refLayerId = activeSPS->getScalingListRefLayerId();
671    TComSPS *refSps = m_ppcTDecTop[refLayerId]->getParameterSetManager()->getActiveSPS(); assert( refSps != NULL );
672
673    // When avc_base_layer_flag is equal to 1, it is a requirement of bitstream conformance that the value of sps_scaling_list_ref_layer_id shall be greater than 0
674    if( activeVPS->getAvcBaseLayerFlag() )
675    {
676      assert( refLayerId > 0 );
677    }
678
679    // It is a requirement of bitstream conformance that, when an SPS with nuh_layer_id equal to nuhLayerIdA is active for a layer with nuh_layer_id equal to nuhLayerIdB and
680    // sps_infer_scaling_list_flag in the SPS is equal to 1, sps_infer_scaling_list_flag shall be equal to 0 for the SPS that is active for the layer with nuh_layer_id equal to sps_scaling_list_ref_layer_id
681    assert( refSps->getInferScalingListFlag() == false );
682
683    // It is a requirement of bitstream conformance that, when an SPS with nuh_layer_id equal to nuhLayerIdA is active for a layer with nuh_layer_id equal to nuhLayerIdB,
684    // the layer with nuh_layer_id equal to sps_scaling_list_ref_layer_id shall be a direct or indirect reference layer of the layer with nuh_layer_id equal to nuhLayerIdB
685    assert( activeVPS->getRecursiveRefLayerFlag( activeSPS->getLayerId(), refLayerId ) == true );
686   
687    if( activeSPS->getScalingList() != refSps->getScalingList() )
688    {
689      // delete created instance of scaling list since it will be inferred
690      delete activeSPS->getScalingList();
691
692      // infer scaling list
693      activeSPS->setScalingList( refSps->getScalingList() );
694    }
695  }
696
697  if( activePPS->getInferScalingListFlag() )
698  {
699    UInt refLayerId = activePPS->getScalingListRefLayerId();
700    TComPPS *refPps = m_ppcTDecTop[refLayerId]->getParameterSetManager()->getActivePPS(); assert( refPps != NULL );
701
702    // When avc_base_layer_flag is equal to 1, it is a requirement of bitstream conformance that the value of sps_scaling_list_ref_layer_id shall be greater than 0
703    if( activeVPS->getAvcBaseLayerFlag() )
704    {
705      assert( refLayerId > 0 );
706    }
707
708    // It is a requirement of bitstream conformance that, when an PPS with nuh_layer_id equal to nuhLayerIdA is active for a layer with nuh_layer_id equal to nuhLayerIdB and
709    // pps_infer_scaling_list_flag in the PPS is equal to 1, pps_infer_scaling_list_flag shall be equal to 0 for the PPS that is active for the layer with nuh_layer_id equal to pps_scaling_list_ref_layer_id
710    assert( refPps->getInferScalingListFlag() == false );
711
712    // It is a requirement of bitstream conformance that, when an PPS with nuh_layer_id equal to nuhLayerIdA is active for a layer with nuh_layer_id equal to nuhLayerIdB,
713    // the layer with nuh_layer_id equal to pps_scaling_list_ref_layer_id shall be a direct or indirect reference layer of the layer with nuh_layer_id equal to nuhLayerIdB
714    assert( activeVPS->getRecursiveRefLayerFlag( activePPS->getLayerId(), refLayerId ) == true );
715   
716    if( activePPS->getScalingList() != refPps->getScalingList() )
717    {
718      // delete created instance of scaling list since it will be inferred
719      delete activePPS->getScalingList();
720
721      // infer scaling list
722      activePPS->setScalingList( refPps->getScalingList() );
723    }
724
725  }
726#endif
727
728#if P0312_VERT_PHASE_ADJ
729  if( activeVPS->getVpsVuiVertPhaseInUseFlag() == 0 )
730  {   
731    for(Int i = 0; i < activeSPS->getNumScaledRefLayerOffsets(); i++)
732    {
733      UInt scaledRefLayerId = activeSPS->getScaledRefLayerId(i);
734      if( activeSPS->getVertPhasePositionEnableFlag( scaledRefLayerId ) )
735      {
736        printf("\nWarning: LayerId = %d: vert_phase_position_enable_flag[%d] = 1, however indication vert_phase_position_in_use_flag = 0\n", m_layerId, scaledRefLayerId );
737        break;
738      }
739    }
740  }
741#endif
742
743#if SPS_DPB_PARAMS
744  if( m_layerId > 0 )
745  {
746    // When not present sps_max_sub_layers_minus1 is inferred to be equal to vps_max_sub_layers_minus1.
747    sps->setMaxTLayers( activeVPS->getMaxTLayers() );
748
749    // When not present sps_temporal_id_nesting_flag is inferred to be equal to vps_temporal_id_nesting_flag
750    sps->setTemporalIdNestingFlag( activeVPS->getTemporalNestingFlag() );
751
752    // When sps_max_dec_pic_buffering_minus1[ i ] is not present for i in the range of 0 to sps_max_sub_layers_minus1, inclusive, due to nuh_layer_id being greater than 0,
753    // it is inferred to be equal to max_vps_dec_pic_buffering_minus1[ TargetOptLayerSetIdx ][ currLayerId ][ i ] of the active VPS, where currLayerId is the nuh_layer_id of the layer that refers to the SPS.
754    for(UInt i=0; i < sps->getMaxTLayers(); i++)
755    {
756      sps->setMaxDecPicBuffering( activeVPS->getMaxVpsDecPicBufferingMinus1( getCommonDecoderParams()->getTargetOutputLayerSetIdx(), sps->getLayerId(), i) + 1, i);
757    }
758  }
759#endif
760
761  if( pps->getDependentSliceSegmentsEnabledFlag() )
762  {
763    Int NumCtx = pps->getEntropyCodingSyncEnabledFlag()?2:1;
764
765    if (m_cSliceDecoder.getCtxMemSize() != NumCtx)
766    {
767      m_cSliceDecoder.initCtxMem(NumCtx);
768      for ( UInt st = 0; st < NumCtx; st++ )
769      {
770        TDecSbac* ctx = NULL;
771        ctx = new TDecSbac;
772        ctx->init( &m_cBinCABAC );
773        m_cSliceDecoder.setCtxMem( ctx, st );
774      }
775    }
776  }
777
778  m_apcSlicePilot->setPPS(pps);
779  m_apcSlicePilot->setSPS(sps);
780  pps->setSPS(sps);
781#if REPN_FORMAT_IN_VPS
782  pps->setNumSubstreams(pps->getEntropyCodingSyncEnabledFlag() ? ((sps->getPicHeightInLumaSamples() + sps->getMaxCUHeight() - 1) / sps->getMaxCUHeight()) * (pps->getNumColumnsMinus1() + 1) : 1);
783#else
784  pps->setNumSubstreams(pps->getEntropyCodingSyncEnabledFlag() ? ((sps->getPicHeightInLumaSamples() + sps->getMaxCUHeight() - 1) / sps->getMaxCUHeight()) * (pps->getNumColumnsMinus1() + 1) : 1);
785#endif
786  pps->setMinCuDQPSize( sps->getMaxCUWidth() >> ( pps->getMaxCuDQPDepth()) );
787
788#if REPN_FORMAT_IN_VPS
789  g_bitDepthY     = m_apcSlicePilot->getBitDepthY();
790  g_bitDepthC     = m_apcSlicePilot->getBitDepthC();
791#else
792  g_bitDepthY     = sps->getBitDepthY();
793  g_bitDepthC     = sps->getBitDepthC();
794#endif
795  g_uiMaxCUWidth  = sps->getMaxCUWidth();
796  g_uiMaxCUHeight = sps->getMaxCUHeight();
797  g_uiMaxCUDepth  = sps->getMaxCUDepth();
798  g_uiAddCUDepth  = max (0, sps->getLog2MinCodingBlockSize() - (Int)sps->getQuadtreeTULog2MinSize() );
799
800  for (Int i = 0; i < sps->getLog2DiffMaxMinCodingBlockSize(); i++)
801  {
802    sps->setAMPAcc( i, sps->getUseAMP() );
803  }
804
805  for (Int i = sps->getLog2DiffMaxMinCodingBlockSize(); i < sps->getMaxCUDepth(); i++)
806  {
807    sps->setAMPAcc( i, 0 );
808  }
809
810  m_cSAO.destroy();
811#if REPN_FORMAT_IN_VPS
812#if AUXILIARY_PICTURES
813  m_cSAO.create( m_apcSlicePilot->getPicWidthInLumaSamples(), m_apcSlicePilot->getPicHeightInLumaSamples(), sps->getChromaFormatIdc(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth() );
814#else
815  m_cSAO.create( m_apcSlicePilot->getPicWidthInLumaSamples(), m_apcSlicePilot->getPicHeightInLumaSamples(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth() );
816#endif
817#else
818  m_cSAO.create( sps->getPicWidthInLumaSamples(), sps->getPicHeightInLumaSamples(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth() );
819#endif
820  m_cLoopFilter.create( sps->getMaxCUDepth() );
821}
822
823#if SVC_EXTENSION
824#if POC_RESET_FLAG
825Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int &iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC )
826#else
827Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC )
828#endif
829#else
830Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay )
831#endif
832{
833  TComPic*&   pcPic         = m_pcPic;
834#if SVC_EXTENSION
835#if !NO_OUTPUT_OF_PRIOR_PICS
836#if NO_CLRAS_OUTPUT_FLAG
837  Bool bFirstSliceInSeq;
838#endif
839#endif
840  m_apcSlicePilot->setVPS( m_parameterSetManagerDecoder.getPrefetchedVPS(0) );
841#if OUTPUT_LAYER_SET_INDEX
842  // Following check should go wherever the VPS is activated
843  checkValueOfTargetOutputLayerSetIdx( m_apcSlicePilot->getVPS());
844#endif
845#if RESOLUTION_BASED_DPB
846  // Following assignment should go wherever a new VPS is activated
847  assignSubDpbs(m_apcSlicePilot->getVPS());
848#endif
849  m_apcSlicePilot->initSlice( nalu.m_layerId );
850#else //SVC_EXTENSION
851  m_apcSlicePilot->initSlice();
852#endif
853
854  if (m_bFirstSliceInPicture)
855  {
856    m_uiSliceIdx     = 0;
857  }
858  else
859  {
860    m_apcSlicePilot->copySliceInfo( pcPic->getPicSym()->getSlice(m_uiSliceIdx-1) );
861  }
862  m_apcSlicePilot->setSliceIdx(m_uiSliceIdx);
863
864  m_apcSlicePilot->setNalUnitType(nalu.m_nalUnitType);
865  Bool nonReferenceFlag = (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TRAIL_N ||
866                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N   ||
867                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N  ||
868                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N  ||
869                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N);
870  m_apcSlicePilot->setTemporalLayerNonReferenceFlag(nonReferenceFlag);
871 
872  m_apcSlicePilot->setReferenced(true); // Putting this as true ensures that picture is referenced the first time it is in an RPS
873  m_apcSlicePilot->setTLayerInfo(nalu.m_temporalId);
874
875#if SVC_EXTENSION
876#if VPS_EXTN_DIRECT_REF_LAYERS
877  setRefLayerParams(m_apcSlicePilot->getVPS());
878#endif
879  m_apcSlicePilot->setNumMotionPredRefLayers(m_numMotionPredRefLayers);
880#endif
881  m_cEntropyDecoder.decodeSliceHeader (m_apcSlicePilot, &m_parameterSetManagerDecoder);
882
883  // set POC for dependent slices in skipped pictures
884  if(m_apcSlicePilot->getDependentSliceSegmentFlag() && m_prevSliceSkipped) 
885  {
886    m_apcSlicePilot->setPOC(m_skippedPOC);
887  }
888
889  m_apcSlicePilot->setAssociatedIRAPPOC(m_pocCRA);
890  m_apcSlicePilot->setAssociatedIRAPType(m_associatedIRAPType);
891
892#if NO_OUTPUT_OF_PRIOR_PICS
893  // Infer the value of NoOutputOfPriorPicsFlag
894  if( m_apcSlicePilot->getRapPicFlag() )
895  {
896    if ( m_apcSlicePilot->getBlaPicFlag() || m_apcSlicePilot->getIdrPicFlag()  || 
897        (m_apcSlicePilot->getCraPicFlag() && m_bFirstSliceInSequence) ||
898        (m_apcSlicePilot->getCraPicFlag() && m_apcSlicePilot->getHandleCraAsBlaFlag()))
899    {
900      m_apcSlicePilot->setNoRaslOutputFlag( true );
901    }
902    else
903    {
904      m_apcSlicePilot->setNoRaslOutputFlag( false );
905    }
906  }
907#endif
908
909  // Skip pictures due to random access
910  if (isRandomAccessSkipPicture(iSkipFrame, iPOCLastDisplay))
911  {
912    m_prevSliceSkipped = true;
913    m_skippedPOC = m_apcSlicePilot->getPOC();
914    return false;
915  }
916  // Skip TFD pictures associated with BLA/BLANT pictures
917  if (isSkipPictureForBLA(iPOCLastDisplay))
918  {
919    m_prevSliceSkipped = true;
920    m_skippedPOC = m_apcSlicePilot->getPOC();
921    return false;
922  }
923
924  // clear previous slice skipped flag
925  m_prevSliceSkipped = false;
926
927  // exit when a new picture is found
928#if SVC_EXTENSION
929  bNewPOC = (m_apcSlicePilot->getPOC()!= m_prevPOC);
930
931#if NO_OUTPUT_OF_PRIOR_PICS
932#if NO_CLRAS_OUTPUT_FLAG
933  if (m_layerId == 0 && m_apcSlicePilot->getRapPicFlag() )
934  {
935    if (m_bFirstSliceInSequence)
936    {
937      setNoClrasOutputFlag(true);
938    }
939    else if ( m_apcSlicePilot->getBlaPicFlag() )
940    {
941      setNoClrasOutputFlag(true);
942    }
943#if O0149_CROSS_LAYER_BLA_FLAG
944    else if (m_apcSlicePilot->getIdrPicFlag() && m_apcSlicePilot->getCrossLayerBLAFlag())
945    {
946      setNoClrasOutputFlag(true);
947    }
948#endif
949    else
950    {
951      setNoClrasOutputFlag(false);
952    }     
953  }
954  else
955  {
956    setNoClrasOutputFlag(false);
957  }
958
959  m_apcSlicePilot->decodingRefreshMarking(m_pocCRA, m_bRefreshPending, m_cListPic, getNoClrasOutputFlag());
960#endif
961
962  // Derive the value of NoOutputOfPriorPicsFlag
963  if( bNewPOC || m_layerId!=m_uiPrevLayerId )   // i.e. new coded picture
964  {
965    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA && m_apcSlicePilot->getNoRaslOutputFlag() )
966    {
967      this->setNoOutputOfPriorPicsFlags( true );
968    }
969    else if( m_apcSlicePilot->getRapPicFlag() && m_apcSlicePilot->getNoRaslOutputFlag() )
970    {
971      this->setNoOutputOfPriorPicsFlags( m_apcSlicePilot->getNoOutputOfPriorPicsFlag() );
972    }
973    else
974    {
975      if( this->m_ppcTDecTop[0]->getNoClrasOutputFlag() )
976      {
977        this->setNoOutputOfPriorPicsFlags( true );
978      }
979    }
980  }
981#endif
982
983#if ALIGNED_BUMPING
984  if (bNewPOC || m_layerId!=m_uiPrevLayerId)
985  {
986    m_apcSlicePilot->applyReferencePictureSet(m_cListPic, m_apcSlicePilot->getRPS());
987  }
988#endif
989  if (m_apcSlicePilot->isNextSlice() && (bNewPOC || m_layerId!=m_uiPrevLayerId) && !m_bFirstSliceInSequence )
990  {
991    m_prevPOC = m_apcSlicePilot->getPOC();
992    curLayerId = m_uiPrevLayerId; 
993    m_uiPrevLayerId = m_layerId;
994    return true;
995  }
996#else
997  //we should only get a different poc for a new picture (with CTU address==0)
998  if (m_apcSlicePilot->isNextSlice() && m_apcSlicePilot->getPOC()!=m_prevPOC && !m_bFirstSliceInSequence && (!m_apcSlicePilot->getSliceCurStartCUAddr()==0))
999  {
1000    printf ("Warning, the first slice of a picture might have been lost!\n");
1001  }
1002  // exit when a new picture is found
1003  if (m_apcSlicePilot->isNextSlice() && (m_apcSlicePilot->getSliceCurStartCUAddr() == 0 && !m_bFirstSliceInPicture) && !m_bFirstSliceInSequence )
1004  {
1005    if (m_prevPOC >= m_pocRandomAccess)
1006    {
1007      m_prevPOC = m_apcSlicePilot->getPOC();
1008      return true;
1009    }
1010    m_prevPOC = m_apcSlicePilot->getPOC();
1011  }
1012#endif
1013  // actual decoding starts here
1014  xActivateParameterSets();
1015#if !O0223_O0139_IRAP_ALIGN_NO_CONTRAINTS && N0147_IRAP_ALIGN_FLAG
1016  //Note setting O0223_O0139_IRAP_ALIGN_NO_CONTRAINTS to 0 may cause decoder to crash.
1017  //When cross_layer_irap_aligned_flag is equal to 0, num_extra_slice_header_bits >=1
1018  if(!m_apcSlicePilot->getVPS()->getCrossLayerIrapAlignFlag() )
1019  {
1020    assert( m_apcSlicePilot->getPPS()->getNumExtraSliceHeaderBits() > 0);
1021  }
1022  //When cross_layer_irap_aligned_flag is equal to 1, the value of poc_reset_flag shall be equal to 0 
1023  if( m_apcSlicePilot->getVPS()->getCrossLayerIrapAlignFlag() )
1024  {
1025    assert( m_apcSlicePilot->getPocResetFlag() == 0);
1026  }
1027#endif
1028#if REPN_FORMAT_IN_VPS
1029  // Initialize ILRP if needed, only for the current layer 
1030  // ILRP intialization should go along with activation of parameters sets,
1031  // although activation of parameter sets itself need not be done for each and every slice!!!
1032  xInitILRP(m_apcSlicePilot);
1033#endif
1034  if (m_apcSlicePilot->isNextSlice()) 
1035  {
1036    m_prevPOC = m_apcSlicePilot->getPOC();
1037#if SVC_EXTENSION
1038    curLayerId = m_layerId;
1039    m_uiPrevLayerId = m_layerId;
1040#endif
1041  }
1042#if !NO_OUTPUT_OF_PRIOR_PICS
1043#if NO_CLRAS_OUTPUT_FLAG
1044  bFirstSliceInSeq = m_bFirstSliceInSequence;
1045#endif
1046#endif
1047  m_bFirstSliceInSequence = false;
1048#if POC_RESET_FLAG
1049  // This operation would do the following:
1050  // 1. Update the other picture in the DPB. This should be done only for the first slice of the picture.
1051  // 2. Update the value of m_pocCRA.
1052  // 3. Reset the POC values at the decoder for the current picture to be zero.
1053  // 4. update value of POCLastDisplay
1054  if( m_apcSlicePilot->getPocResetFlag() )
1055  {
1056    if( m_apcSlicePilot->getSliceIdx() == 0 )
1057    {
1058      Int pocAdjustValue = m_apcSlicePilot->getPOC();
1059
1060#if PREVTID0_POC_RESET
1061      m_apcSlicePilot->adjustPrevTid0POC(pocAdjustValue);
1062#endif
1063      // If poc reset flag is set to 1, reset all POC for DPB -> basically do it for each slice in the picutre
1064      TComList<TComPic*>::iterator  iterPic = m_cListPic.begin(); 
1065
1066      // Iterate through all picture in DPB
1067      while( iterPic != m_cListPic.end() )
1068      {
1069        TComPic *dpbPic = *iterPic;
1070        // Check if the picture pointed to by iterPic is either used for reference or
1071        // needed for output, are in the same layer, and not the current picture.
1072        if( /*  ( ( dpbPic->getSlice(0)->isReferenced() ) || ( dpbPic->getOutputMark() ) )
1073            &&*/ ( dpbPic->getLayerId() == m_apcSlicePilot->getLayerId() )
1074              && ( dpbPic->getReconMark() ) 
1075          )
1076        {
1077          for(Int i = dpbPic->getNumAllocatedSlice()-1; i >= 0; i--)
1078          {
1079
1080            TComSlice *slice = dpbPic->getSlice(i);
1081            TComReferencePictureSet *rps = slice->getRPS();
1082            slice->setPOC( slice->getPOC() - pocAdjustValue );
1083
1084            // Also adjust the POC value stored in the RPS of each such slice
1085            for(Int j = rps->getNumberOfPictures(); j >= 0; j--)
1086            {
1087              rps->setPOC( j, rps->getPOC(j) - pocAdjustValue );
1088            }
1089            // Also adjust the value of refPOC
1090            for(Int k = 0; k < 2; k++)  // For List 0 and List 1
1091            {
1092              RefPicList list = (k == 1) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1093              for(Int j = 0; j < slice->getNumRefIdx(list); j++)
1094              {
1095                slice->setRefPOC( slice->getRefPOC(list, j) - pocAdjustValue, list, j);
1096              }
1097            }
1098          }
1099        }
1100        iterPic++;
1101      }
1102      // Update the value of pocCRA
1103      m_pocCRA -= pocAdjustValue;
1104      // Update value of POCLastDisplay
1105      iPOCLastDisplay -= pocAdjustValue;
1106    }
1107    // Reset current poc for current slice and RPS
1108    m_apcSlicePilot->setPOC( 0 );
1109  }
1110#endif
1111#if ALIGN_TSA_STSA_PICS
1112  if( m_apcSlicePilot->getLayerId() > 0 )
1113  {
1114    // Check for TSA alignment
1115    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
1116        m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R
1117         )
1118    {
1119      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1120      {
1121        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1122        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() );
1123        if( refpicLayer )
1124        {
1125          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
1126                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R );    // TSA pictures should be aligned among depenedent layers
1127        } 
1128      }
1129    }
1130    // Check for STSA alignment
1131    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
1132         m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R
1133         )
1134    {
1135      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1136      {
1137        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1138        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() ); // STSA pictures should be aligned among dependent layers
1139        if( refpicLayer )
1140
1141        {
1142          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
1143                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R );
1144        }
1145      }
1146    }
1147  }
1148#endif
1149  //detect lost reference picture and insert copy of earlier frame.
1150  Int lostPoc;
1151  while((lostPoc=m_apcSlicePilot->checkThatAllRefPicsAreAvailable(m_cListPic, m_apcSlicePilot->getRPS(), true, m_pocRandomAccess)) > 0)
1152  {
1153    xCreateLostPicture(lostPoc-1);
1154  }
1155  if (m_bFirstSliceInPicture)
1156  {
1157#if AVC_BASE
1158    if( m_layerId == 1 && m_parameterSetManagerDecoder.getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
1159    {
1160      TComPic* pBLPic = (*m_ppcTDecTop[0]->getListPic()->begin());
1161      pBLPic->getSlice(0)->setReferenced(true);
1162      fstream* pFile  = m_ppcTDecTop[0]->getBLReconFile();
1163      UInt uiWidth    = pBLPic->getPicYuvRec()->getWidth();
1164      UInt uiHeight   = pBLPic->getPicYuvRec()->getHeight();
1165
1166      if( pFile->good() )
1167      {
1168        UInt64 uiPos = (UInt64) m_apcSlicePilot->getPOC() * uiWidth * uiHeight * 3 / 2;
1169
1170        pFile->seekg((UInt)uiPos, ios::beg );
1171
1172        Pel* pPel = pBLPic->getPicYuvRec()->getLumaAddr();
1173        UInt uiStride = pBLPic->getPicYuvRec()->getStride();
1174        for( Int i = 0; i < uiHeight; i++ )
1175        {
1176          for( Int j = 0; j < uiWidth; j++ )
1177          {
1178            pPel[j] = pFile->get();
1179          }
1180          pPel += uiStride;
1181        }
1182
1183        pPel = pBLPic->getPicYuvRec()->getCbAddr();
1184        uiStride = pBLPic->getPicYuvRec()->getCStride();
1185        for( Int i = 0; i < uiHeight/2; i++ )
1186        {
1187          for( Int j = 0; j < uiWidth/2; j++ )
1188          {
1189            pPel[j] = pFile->get();
1190          }
1191          pPel += uiStride;
1192        }
1193
1194        pPel = pBLPic->getPicYuvRec()->getCrAddr();
1195        uiStride = pBLPic->getPicYuvRec()->getCStride();
1196        for( Int i = 0; i < uiHeight/2; i++ )
1197        {
1198          for( Int j = 0; j < uiWidth/2; j++ )
1199          {
1200            pPel[j] = pFile->get();
1201          }
1202          pPel += uiStride;
1203        }
1204      }
1205    }
1206#endif
1207
1208#if !NO_OUTPUT_OF_PRIOR_PICS
1209#if NO_CLRAS_OUTPUT_FLAG
1210    if (m_layerId == 0 &&
1211        (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1212      || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1213      || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
1214      || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
1215      || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
1216      || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA))
1217    {
1218      if (bFirstSliceInSeq)
1219      {
1220        setNoClrasOutputFlag(true);
1221      }
1222      else if (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1223            || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1224            || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP)
1225      {
1226        setNoClrasOutputFlag(true);
1227      }
1228#if O0149_CROSS_LAYER_BLA_FLAG
1229      else if ((m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP) &&
1230               m_apcSlicePilot->getCrossLayerBLAFlag())
1231      {
1232        setNoClrasOutputFlag(true);
1233      }
1234#endif
1235      else
1236      {
1237        setNoClrasOutputFlag(false);
1238      }
1239      if (getNoClrasOutputFlag())
1240      {
1241        for (UInt i = 0; i < m_apcSlicePilot->getVPS()->getMaxLayers(); i++)
1242        {
1243          m_ppcTDecTop[i]->setLayerInitializedFlag(false);
1244          m_ppcTDecTop[i]->setFirstPicInLayerDecodedFlag(false);
1245        }
1246      }
1247    }
1248#endif
1249
1250#if NO_CLRAS_OUTPUT_FLAG
1251    m_apcSlicePilot->decodingRefreshMarking(m_pocCRA, m_bRefreshPending, m_cListPic, getNoClrasOutputFlag());
1252#endif
1253#else
1254    if ( m_layerId == 0 && m_apcSlicePilot->getRapPicFlag() && getNoClrasOutputFlag() )
1255    {
1256      for (UInt i = 0; i < m_apcSlicePilot->getVPS()->getMaxLayers(); i++)
1257      {
1258        m_ppcTDecTop[i]->setLayerInitializedFlag(false);
1259        m_ppcTDecTop[i]->setFirstPicInLayerDecodedFlag(false);
1260      }
1261    }
1262#endif
1263    // Buffer initialize for prediction.
1264    m_cPrediction.initTempBuff();
1265#if !ALIGNED_BUMPING
1266    m_apcSlicePilot->applyReferencePictureSet(m_cListPic, m_apcSlicePilot->getRPS());
1267#else
1268    m_apcSlicePilot->checkLeadingPictureRestrictions(m_cListPic);
1269#endif
1270    //  Get a new picture buffer
1271    xGetNewPicBuffer (m_apcSlicePilot, pcPic);
1272
1273    Bool isField = false;
1274    Bool isTff = false;
1275   
1276    if(!m_SEIs.empty())
1277    {
1278      // Check if any new Picture Timing SEI has arrived
1279      SEIMessages pictureTimingSEIs = extractSeisByType (m_SEIs, SEI::PICTURE_TIMING);
1280      if (pictureTimingSEIs.size()>0)
1281      {
1282        SEIPictureTiming* pictureTiming = (SEIPictureTiming*) *(pictureTimingSEIs.begin());
1283        isField = (pictureTiming->m_picStruct == 1) || (pictureTiming->m_picStruct == 2);
1284        isTff =  (pictureTiming->m_picStruct == 1);
1285      }
1286    }
1287   
1288    //Set Field/Frame coding mode
1289    m_pcPic->setField(isField);
1290    m_pcPic->setTopField(isTff);
1291
1292    // transfer any SEI messages that have been received to the picture
1293    pcPic->setSEIs(m_SEIs);
1294    m_SEIs.clear();
1295
1296    // Recursive structure
1297    m_cCuDecoder.create ( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight );
1298#if SVC_EXTENSION
1299    m_cCuDecoder.init   ( m_ppcTDecTop,&m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction, curLayerId );
1300#else
1301    m_cCuDecoder.init   ( &m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction );
1302#endif
1303    m_cTrQuant.init     ( g_uiMaxCUWidth, g_uiMaxCUHeight, m_apcSlicePilot->getSPS()->getMaxTrSize());
1304
1305    m_cSliceDecoder.create();
1306  }
1307  else
1308  {
1309    // Check if any new SEI has arrived
1310    if(!m_SEIs.empty())
1311    {
1312      // Currently only decoding Unit SEI message occurring between VCL NALUs copied
1313      SEIMessages &picSEI = pcPic->getSEIs();
1314      SEIMessages decodingUnitInfos = extractSeisByType (m_SEIs, SEI::DECODING_UNIT_INFO);
1315      picSEI.insert(picSEI.end(), decodingUnitInfos.begin(), decodingUnitInfos.end());
1316      deleteSEIs(m_SEIs);
1317    }
1318  }
1319 
1320  //  Set picture slice pointer
1321  TComSlice*  pcSlice = m_apcSlicePilot;
1322  Bool bNextSlice     = pcSlice->isNextSlice();
1323
1324  UInt uiCummulativeTileWidth;
1325  UInt uiCummulativeTileHeight;
1326  UInt i, j, p;
1327
1328  //set NumColumnsMins1 and NumRowsMinus1
1329  pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getPPS()->getNumColumnsMinus1() );
1330  pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getPPS()->getNumRowsMinus1() );
1331
1332  //create the TComTileArray
1333  pcPic->getPicSym()->xCreateTComTileArray();
1334
1335  if( pcSlice->getPPS()->getUniformSpacingFlag() )
1336  {
1337    //set the width for each tile
1338    for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
1339    {
1340      for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++)
1341      {
1342        pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->
1343          setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1) 
1344          - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) );
1345      }
1346    }
1347
1348    //set the height for each tile
1349    for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
1350    {
1351      for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++)
1352      {
1353        pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->
1354          setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1) 
1355          - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) );   
1356      }
1357    }
1358  }
1359  else
1360  {
1361    //set the width for each tile
1362    for(j=0; j < pcSlice->getPPS()->getNumRowsMinus1()+1; j++)
1363    {
1364      uiCummulativeTileWidth = 0;
1365      for(i=0; i < pcSlice->getPPS()->getNumColumnsMinus1(); i++)
1366      {
1367        pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcSlice->getPPS()->getColumnWidth(i) );
1368        uiCummulativeTileWidth += pcSlice->getPPS()->getColumnWidth(i);
1369      }
1370      pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth );
1371    }
1372
1373    //set the height for each tile
1374    for(j=0; j < pcSlice->getPPS()->getNumColumnsMinus1()+1; j++)
1375    {
1376      uiCummulativeTileHeight = 0;
1377      for(i=0; i < pcSlice->getPPS()->getNumRowsMinus1(); i++)
1378      { 
1379        pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcSlice->getPPS()->getRowHeight(i) );
1380        uiCummulativeTileHeight += pcSlice->getPPS()->getRowHeight(i);
1381      }
1382      pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight );
1383    }
1384  }
1385
1386  pcPic->getPicSym()->xInitTiles();
1387
1388  //generate the Coding Order Map and Inverse Coding Order Map
1389  UInt uiEncCUAddr;
1390  for(i=0, uiEncCUAddr=0; i<pcPic->getPicSym()->getNumberOfCUsInFrame(); i++, uiEncCUAddr = pcPic->getPicSym()->xCalculateNxtCUAddr(uiEncCUAddr))
1391  {
1392    pcPic->getPicSym()->setCUOrderMap(i, uiEncCUAddr);
1393    pcPic->getPicSym()->setInverseCUOrderMap(uiEncCUAddr, i);
1394  }
1395  pcPic->getPicSym()->setCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
1396  pcPic->getPicSym()->setInverseCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
1397
1398  //convert the start and end CU addresses of the slice and dependent slice into encoding order
1399  pcSlice->setSliceSegmentCurStartCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurStartCUAddr()) );
1400  pcSlice->setSliceSegmentCurEndCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurEndCUAddr()) );
1401  if(pcSlice->isNextSlice())
1402  {
1403    pcSlice->setSliceCurStartCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurStartCUAddr()));
1404    pcSlice->setSliceCurEndCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurEndCUAddr()));
1405  }
1406
1407  if (m_bFirstSliceInPicture) 
1408  {
1409    if(pcPic->getNumAllocatedSlice() != 1)
1410    {
1411      pcPic->clearSliceBuffer();
1412    }
1413  }
1414  else
1415  {
1416    pcPic->allocateNewSlice();
1417  }
1418  assert(pcPic->getNumAllocatedSlice() == (m_uiSliceIdx + 1));
1419  m_apcSlicePilot = pcPic->getPicSym()->getSlice(m_uiSliceIdx); 
1420  pcPic->getPicSym()->setSlice(pcSlice, m_uiSliceIdx);
1421
1422  pcPic->setTLayer(nalu.m_temporalId);
1423
1424#if SVC_EXTENSION
1425  pcPic->setLayerId(nalu.m_layerId);
1426  pcSlice->setLayerId(nalu.m_layerId);
1427  pcSlice->setPic(pcPic);
1428#endif
1429
1430  if (bNextSlice)
1431  {
1432    pcSlice->checkCRA(pcSlice->getRPS(), m_pocCRA, m_associatedIRAPType, m_cListPic );
1433    // Set reference list
1434#if SVC_EXTENSION
1435    if (m_layerId == 0)
1436#endif
1437    pcSlice->setRefPicList( m_cListPic, true );
1438
1439#if SVC_EXTENSION
1440    // Create upsampling reference layer pictures for all possible dependent layers and do it only once for the first slice.
1441    // Other slices might choose which reference pictures to be used for inter-layer prediction
1442    if( m_layerId > 0 && m_uiSliceIdx == 0 )
1443    {     
1444#if M0040_ADAPTIVE_RESOLUTION_CHANGE
1445      if( !pcSlice->getVPS()->getSingleLayerForNonIrapFlag() || ( pcSlice->getVPS()->getSingleLayerForNonIrapFlag() && pcSlice->isIRAP() ) )
1446#endif
1447      for( i = 0; i < pcSlice->getNumILRRefIdx(); i++ )
1448      {
1449        UInt refLayerIdc = i;
1450#if AVC_BASE
1451        if( pcSlice->getVPS()->getRefLayerId( m_layerId, refLayerIdc ) == 0 && m_parameterSetManagerDecoder.getActiveVPS()->getAvcBaseLayerFlag() )
1452        {         
1453          TComPic* pic = *m_ppcTDecTop[0]->getListPic()->begin();
1454
1455          if( pic )
1456          {
1457            pcSlice->setBaseColPic ( refLayerIdc, pic );
1458          }
1459          else
1460          {
1461            continue;
1462          }
1463#if AVC_SYNTAX
1464          TComPic* pBLPic = pcSlice->getBaseColPic(refLayerIdc);
1465          if( pcSlice->getPOC() == 0 )
1466          {
1467            // initialize partition order.
1468            UInt* piTmp = &g_auiZscanToRaster[0];
1469            initZscanToRaster( pBLPic->getPicSym()->getMaxDepth() + 1, 1, 0, piTmp );
1470            initRasterToZscan( pBLPic->getPicSym()->getMaxCUWidth(), pBLPic->getPicSym()->getMaxCUHeight(), pBLPic->getPicSym()->getMaxDepth() + 1 );
1471          }     
1472          pBLPic->getSlice( 0 )->initBaseLayerRPL( pcSlice );
1473          pBLPic->readBLSyntax( m_ppcTDecTop[0]->getBLSyntaxFile(), SYNTAX_BYTES );
1474#endif
1475        }
1476        else
1477        {
1478#if VPS_EXTN_DIRECT_REF_LAYERS
1479          TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1480#else
1481          TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1482#endif
1483          TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1484          if( !pcSlice->setBaseColPic ( *cListPic, refLayerIdc ) )
1485          {
1486            continue;
1487          }
1488        }
1489#else
1490#if VPS_EXTN_DIRECT_REF_LAYERS
1491        TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1492#else
1493        TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1494#endif
1495        TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1496        pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1497#endif
1498
1499#if O0098_SCALED_REF_LAYER_ID
1500        const Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindowForLayer(pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc));
1501#else
1502        const Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc);
1503#endif
1504
1505        Int widthBL   = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getWidth();
1506        Int heightBL  = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getHeight();
1507
1508        Int widthEL   = pcPic->getPicYuvRec()->getWidth()  - scalEL.getWindowLeftOffset() - scalEL.getWindowRightOffset();
1509        Int heightEL  = pcPic->getPicYuvRec()->getHeight() - scalEL.getWindowTopOffset()  - scalEL.getWindowBottomOffset();
1510
1511        g_mvScalingFactor[refLayerIdc][0] = widthEL  == widthBL  ? 4096 : Clip3(-4096, 4095, ((widthEL  << 8) + (widthBL  >> 1)) / widthBL);
1512        g_mvScalingFactor[refLayerIdc][1] = heightEL == heightBL ? 4096 : Clip3(-4096, 4095, ((heightEL << 8) + (heightBL >> 1)) / heightBL);
1513
1514        g_posScalingFactor[refLayerIdc][0] = ((widthBL  << 16) + (widthEL  >> 1)) / widthEL;
1515        g_posScalingFactor[refLayerIdc][1] = ((heightBL << 16) + (heightEL >> 1)) / heightEL;
1516
1517#if SVC_UPSAMPLING
1518        if( pcPic->isSpatialEnhLayer(refLayerIdc) )
1519        {   
1520/*#if O0098_SCALED_REF_LAYER_ID
1521          Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindowForLayer(pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc));
1522#else
1523          Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc);
1524#endif*/
1525#if O0215_PHASE_ALIGNMENT
1526#if O0194_JOINT_US_BITSHIFT
1527          m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), scalEL, pcSlice->getVPS()->getPhaseAlignFlag() );
1528#else
1529          m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), scalEL, pcSlice->getVPS()->getPhaseAlignFlag() );
1530#endif
1531#else
1532#if O0194_JOINT_US_BITSHIFT
1533          m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), scalEL );
1534#else
1535          m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), scalEL );
1536#endif
1537#endif
1538        }
1539        else
1540        {
1541          pcPic->setFullPelBaseRec( refLayerIdc, pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec() );
1542        }
1543        pcSlice->setFullPelBaseRec ( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc) );
1544#endif
1545      }
1546    }
1547
1548    if( m_layerId > 0 && pcSlice->getActiveNumILRRefIdx() )
1549    {
1550      for( i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
1551      {
1552        UInt refLayerIdc = pcSlice->getInterLayerPredLayerIdc(i);
1553#if AVC_BASE
1554        if( pcSlice->getVPS()->getRefLayerId( m_layerId, refLayerIdc ) == 0 && m_parameterSetManagerDecoder.getActiveVPS()->getAvcBaseLayerFlag() )
1555        {
1556          pcSlice->setBaseColPic ( refLayerIdc, *m_ppcTDecTop[0]->getListPic()->begin() );
1557#if AVC_SYNTAX
1558          TComPic* pBLPic = pcSlice->getBaseColPic(refLayerIdc);
1559          if( pcSlice->getPOC() == 0 )
1560          {
1561            // initialize partition order.
1562            UInt* piTmp = &g_auiZscanToRaster[0];
1563            initZscanToRaster( pBLPic->getPicSym()->getMaxDepth() + 1, 1, 0, piTmp );
1564            initRasterToZscan( pBLPic->getPicSym()->getMaxCUWidth(), pBLPic->getPicSym()->getMaxCUHeight(), pBLPic->getPicSym()->getMaxDepth() + 1 );
1565          }     
1566          pBLPic->getSlice( 0 )->initBaseLayerRPL( pcSlice );
1567          pBLPic->readBLSyntax( m_ppcTDecTop[0]->getBLSyntaxFile(), SYNTAX_BYTES );
1568#endif
1569        }
1570        else
1571        {
1572#if VPS_EXTN_DIRECT_REF_LAYERS
1573          TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1574#else
1575          TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1576#endif
1577          TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1578          pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1579        }
1580#else
1581#if VPS_EXTN_DIRECT_REF_LAYERS
1582        TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1583#else
1584        TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1585#endif
1586        TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1587        pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1588#endif
1589
1590        pcSlice->setFullPelBaseRec ( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc) );
1591      }
1592
1593      pcSlice->setILRPic( m_cIlpPic );
1594
1595#if REF_IDX_MFM
1596      if( pcSlice->getMFMEnabledFlag() )
1597      {
1598        pcSlice->setRefPOCListILP(m_ppcTDecTop[m_layerId]->m_cIlpPic, pcSlice->getBaseColPic());
1599      }
1600      pcSlice->setRefPicList( m_cListPic, false, m_cIlpPic);
1601    }
1602#if M0040_ADAPTIVE_RESOLUTION_CHANGE
1603    else if ( m_layerId > 0 )
1604    {
1605      pcSlice->setRefPicList( m_cListPic, false, NULL);
1606    }
1607#endif
1608#if MFM_ENCCONSTRAINT
1609    if( pcSlice->getMFMEnabledFlag() )
1610    {
1611      Int refLayerId = pcSlice->getRefPic( pcSlice->getSliceType() == B_SLICE ? ( RefPicList )( 1 - pcSlice->getColFromL0Flag() ) : REF_PIC_LIST_0 , pcSlice->getColRefIdx() )->getLayerId();
1612      if( refLayerId != pcSlice->getLayerId() )
1613      {
1614        TComPic* pColBasePic = pcSlice->getBaseColPic( *m_ppcTDecTop[refLayerId]->getListPic() );
1615        assert( pColBasePic->checkSameRefInfo() == true );
1616      }
1617    }
1618#endif
1619#endif
1620   
1621#if N0147_IRAP_ALIGN_FLAG
1622    if( m_layerId > 0 && pcSlice->getVPS()->getCrossLayerIrapAlignFlag() )
1623    {
1624#if M0040_ADAPTIVE_RESOLUTION_CHANGE
1625      if( !pcSlice->getVPS()->getSingleLayerForNonIrapFlag() || ( pcSlice->getVPS()->getSingleLayerForNonIrapFlag() && pcSlice->isIRAP() ) )
1626#endif
1627      for(Int dependentLayerIdx = 0; dependentLayerIdx < pcSlice->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1628      {
1629        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1630        TComPic* refpicLayer = pcSlice->getRefPic(*cListPic, pcSlice->getPOC() );
1631        if(refpicLayer && pcSlice->isIRAP())
1632        {                 
1633          assert(pcSlice->getNalUnitType() == refpicLayer->getSlice(0)->getNalUnitType());
1634        }
1635      }
1636    }
1637#endif
1638#endif //SVC_EXTENSION
1639   
1640    // For generalized B
1641    // note: maybe not existed case (always L0 is copied to L1 if L1 is empty)
1642    if (pcSlice->isInterB() && pcSlice->getNumRefIdx(REF_PIC_LIST_1) == 0)
1643    {
1644      Int iNumRefIdx = pcSlice->getNumRefIdx(REF_PIC_LIST_0);
1645      pcSlice->setNumRefIdx        ( REF_PIC_LIST_1, iNumRefIdx );
1646
1647      for (Int iRefIdx = 0; iRefIdx < iNumRefIdx; iRefIdx++)
1648      {
1649        pcSlice->setRefPic(pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx), REF_PIC_LIST_1, iRefIdx);
1650      }
1651    }
1652    if (!pcSlice->isIntra())
1653    {
1654      Bool bLowDelay = true;
1655      Int  iCurrPOC  = pcSlice->getPOC();
1656      Int iRefIdx = 0;
1657
1658      for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
1659      {
1660        if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
1661        {
1662          bLowDelay = false;
1663        }
1664      }
1665      if (pcSlice->isInterB())
1666      {
1667        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
1668        {
1669          if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
1670          {
1671            bLowDelay = false;
1672          }
1673        }       
1674      }
1675
1676      pcSlice->setCheckLDC(bLowDelay);           
1677    }
1678
1679    //---------------
1680    pcSlice->setRefPOCList();
1681  }
1682
1683  pcPic->setCurrSliceIdx(m_uiSliceIdx);
1684  if(pcSlice->getSPS()->getScalingListFlag())
1685  {
1686    pcSlice->setScalingList ( pcSlice->getSPS()->getScalingList()  );
1687    if(pcSlice->getPPS()->getScalingListPresentFlag())
1688    {
1689      pcSlice->setScalingList ( pcSlice->getPPS()->getScalingList()  );
1690    }
1691#if SCALINGLIST_INFERRING
1692    if( m_layerId == 0 || ( m_layerId > 0 && !pcSlice->getPPS()->getInferScalingListFlag() && !pcSlice->getSPS()->getInferScalingListFlag() ) )
1693#endif
1694    if(!pcSlice->getPPS()->getScalingListPresentFlag() && !pcSlice->getSPS()->getScalingListPresentFlag())
1695    {
1696      pcSlice->setDefaultScalingList();
1697    }
1698    m_cTrQuant.setScalingListDec(pcSlice->getScalingList());
1699    m_cTrQuant.setUseScalingList(true);
1700  }
1701  else
1702  {
1703    m_cTrQuant.setFlatScalingList();
1704    m_cTrQuant.setUseScalingList(false);
1705  }
1706
1707  //  Decode a picture
1708  m_cGopDecoder.decompressSlice(nalu.m_Bitstream, pcPic);
1709
1710  m_bFirstSliceInPicture = false;
1711  m_uiSliceIdx++;
1712
1713  return false;
1714}
1715
1716Void TDecTop::xDecodeVPS()
1717{
1718  TComVPS* vps = new TComVPS();
1719 
1720  m_cEntropyDecoder.decodeVPS( vps );
1721  m_parameterSetManagerDecoder.storePrefetchedVPS(vps); 
1722}
1723
1724Void TDecTop::xDecodeSPS()
1725{
1726  TComSPS* sps = new TComSPS();
1727#if SVC_EXTENSION
1728  sps->setLayerId(m_layerId);
1729#if SPS_DPB_PARAMS
1730  m_cEntropyDecoder.decodeSPS( sps ); // it should be removed after macro clean up
1731#else
1732  m_cEntropyDecoder.decodeSPS( sps, &m_parameterSetManagerDecoder );
1733#endif
1734  m_parameterSetManagerDecoder.storePrefetchedSPS(sps);
1735#if !REPN_FORMAT_IN_VPS   // ILRP can only be initialized at activation 
1736  if(m_numLayer>0)
1737  {
1738    xInitILRP(sps);
1739  }
1740#endif
1741#else //SVC_EXTENSION
1742  m_cEntropyDecoder.decodeSPS( sps );
1743  m_parameterSetManagerDecoder.storePrefetchedSPS(sps);
1744#endif //SVC_EXTENSION
1745}
1746
1747Void TDecTop::xDecodePPS()
1748{
1749  TComPPS* pps = new TComPPS();
1750
1751#if SCALINGLIST_INFERRING
1752  pps->setLayerId( m_layerId );
1753#endif
1754
1755  m_cEntropyDecoder.decodePPS( pps );
1756  m_parameterSetManagerDecoder.storePrefetchedPPS( pps );
1757
1758  if( pps->getDependentSliceSegmentsEnabledFlag() )
1759  {
1760    Int NumCtx = pps->getEntropyCodingSyncEnabledFlag()?2:1;
1761    m_cSliceDecoder.initCtxMem(NumCtx);
1762    for ( UInt st = 0; st < NumCtx; st++ )
1763    {
1764      TDecSbac* ctx = NULL;
1765      ctx = new TDecSbac;
1766      ctx->init( &m_cBinCABAC );
1767      m_cSliceDecoder.setCtxMem( ctx, st );
1768    }
1769  }
1770}
1771
1772Void TDecTop::xDecodeSEI( TComInputBitstream* bs, const NalUnitType nalUnitType )
1773{
1774#if SVC_EXTENSION
1775  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
1776  {
1777#if RANDOM_ACCESS_SEI_FIX
1778    if (m_prevSliceSkipped) // No need to decode SEI messages of a skipped access unit
1779    {
1780      return;
1781    }
1782#endif
1783#if LAYERS_NOT_PRESENT_SEI
1784    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
1785#else
1786    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1787#endif
1788  }
1789  else
1790  {
1791#if LAYERS_NOT_PRESENT_SEI
1792    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
1793#else
1794    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1795#endif
1796    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
1797    if (activeParamSets.size()>0)
1798    {
1799      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
1800      m_parameterSetManagerDecoder.applyPrefetchedPS();
1801      assert(seiAps->activeSeqParamSetId.size()>0);
1802      if( !m_parameterSetManagerDecoder.activateSPSWithSEI( seiAps->activeSeqParamSetId[0] ) )
1803      {
1804        printf ("Warning SPS activation with Active parameter set SEI failed");
1805      }
1806    }
1807  }
1808#else
1809  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
1810  {
1811#if LAYERS_NOT_PRESENT_SEI
1812    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
1813#else
1814    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1815#endif
1816  }
1817  else
1818  {
1819#if LAYERS_NOT_PRESENT_SEI
1820    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
1821#else
1822    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1823#endif
1824    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
1825    if (activeParamSets.size()>0)
1826    {
1827      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
1828      m_parameterSetManagerDecoder.applyPrefetchedPS();
1829      assert(seiAps->activeSeqParamSetId.size()>0);
1830      if (! m_parameterSetManagerDecoder.activateSPSWithSEI(seiAps->activeSeqParamSetId[0] ))
1831      {
1832        printf ("Warning SPS activation with Active parameter set SEI failed");
1833      }
1834    }
1835  }
1836#endif
1837}
1838
1839#if SVC_EXTENSION
1840Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC)
1841#else
1842Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay)
1843#endif
1844{
1845  // Initialize entropy decoder
1846  m_cEntropyDecoder.setEntropyDecoder (&m_cCavlcDecoder);
1847  m_cEntropyDecoder.setBitstream      (nalu.m_Bitstream);
1848
1849#if O0137_MAX_LAYERID
1850  // ignore any NAL units with nuh_layer_id == 63
1851  if (nalu.m_layerId == 63 )
1852  { 
1853    return false;
1854  }
1855#endif
1856  switch (nalu.m_nalUnitType)
1857  {
1858    case NAL_UNIT_VPS:
1859#if VPS_NUH_LAYER_ID
1860      assert( nalu.m_layerId == 0 ); // Non-conforming bitstream. The value of nuh_layer_id of VPS NAL unit shall be equal to 0.
1861#endif
1862      xDecodeVPS();
1863#if AVC_BASE
1864      if( m_parameterSetManagerDecoder.getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
1865      {
1866        if( !m_ppcTDecTop[0]->getBLReconFile()->good() )
1867        {
1868          printf( "Base layer YUV input reading error\n" );
1869          exit(EXIT_FAILURE);
1870        }       
1871#if AVC_SYNTAX
1872        if( !m_ppcTDecTop[0]->getBLSyntaxFile()->good() )
1873        {
1874          printf( "Base layer syntax input reading error\n" );
1875          exit(EXIT_FAILURE);
1876        }
1877#endif
1878      }
1879      else
1880      {
1881        TComList<TComPic*> *cListPic = m_ppcTDecTop[0]->getListPic();
1882        cListPic->clear();
1883      }
1884#endif
1885      return false;
1886     
1887    case NAL_UNIT_SPS:
1888      xDecodeSPS();
1889#if AVC_BASE
1890      if( m_parameterSetManagerDecoder.getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
1891      {
1892        TComPic* pBLPic = (*m_ppcTDecTop[0]->getListPic()->begin());
1893        if( nalu.m_layerId == 1 && pBLPic->getPicYuvRec() == NULL )
1894        {
1895          // using EL SPS with spsId = 1
1896          TComSPS* sps = m_parameterSetManagerDecoder.getPrefetchedSPS(1);
1897          Int  numReorderPics[MAX_TLAYER];
1898          Window &conformanceWindow = sps->getConformanceWindow();
1899          Window defaultDisplayWindow = sps->getVuiParametersPresentFlag() ? sps->getVuiParameters()->getDefaultDisplayWindow() : Window();
1900#if AUXILIARY_PICTURES
1901#if SVC_UPSAMPLING
1902#if AVC_SYNTAX
1903          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getChromaFormatIdc(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, sps, true);
1904#else
1905          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getChromaFormatIdc(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, NULL, true);
1906#endif
1907#else
1908          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getChromaFormatIdc(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), onformanceWindow, defaultDisplayWindow, numReorderPics, true);
1909#endif
1910#else
1911#if SVC_UPSAMPLING
1912#if AVC_SYNTAX
1913          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, sps, true);
1914#else
1915          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, NULL, true);
1916#endif
1917#else
1918          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), onformanceWindow, defaultDisplayWindow, numReorderPics, true);
1919#endif
1920#endif
1921
1922#if O0194_DIFFERENT_BITDEPTH_EL_BL
1923          // set AVC BL bit depth, can be an input parameter from the command line
1924          g_bitDepthYLayer[0] = 8;
1925          g_bitDepthCLayer[0] = 8;
1926#endif
1927        }
1928      }
1929#endif
1930      return false;
1931
1932    case NAL_UNIT_PPS:
1933      xDecodePPS();
1934      return false;
1935     
1936    case NAL_UNIT_PREFIX_SEI:
1937    case NAL_UNIT_SUFFIX_SEI:
1938      xDecodeSEI( nalu.m_Bitstream, nalu.m_nalUnitType );
1939      return false;
1940
1941    case NAL_UNIT_CODED_SLICE_TRAIL_R:
1942    case NAL_UNIT_CODED_SLICE_TRAIL_N:
1943    case NAL_UNIT_CODED_SLICE_TSA_R:
1944    case NAL_UNIT_CODED_SLICE_TSA_N:
1945    case NAL_UNIT_CODED_SLICE_STSA_R:
1946    case NAL_UNIT_CODED_SLICE_STSA_N:
1947    case NAL_UNIT_CODED_SLICE_BLA_W_LP:
1948    case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
1949    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
1950    case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
1951    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
1952    case NAL_UNIT_CODED_SLICE_CRA:
1953    case NAL_UNIT_CODED_SLICE_RADL_N:
1954    case NAL_UNIT_CODED_SLICE_RADL_R:
1955    case NAL_UNIT_CODED_SLICE_RASL_N:
1956    case NAL_UNIT_CODED_SLICE_RASL_R:
1957#if SVC_EXTENSION
1958      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay, curLayerId, bNewPOC);
1959#else
1960      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay);
1961#endif
1962      break;
1963     
1964    case NAL_UNIT_EOS:
1965      m_associatedIRAPType = NAL_UNIT_INVALID;
1966      m_pocCRA = 0;
1967      m_pocRandomAccess = MAX_INT;
1968      m_prevPOC = MAX_INT;
1969      m_bFirstSliceInPicture = true;
1970      m_bFirstSliceInSequence = true;
1971      m_prevSliceSkipped = false;
1972      m_skippedPOC = 0;
1973      return false;
1974     
1975    case NAL_UNIT_ACCESS_UNIT_DELIMITER:
1976      // TODO: process AU delimiter
1977      return false;
1978     
1979    case NAL_UNIT_EOB:
1980#if P0130_EOB
1981      //Check layer id of the nalu. if it is not 0, give a warning message.
1982      if (nalu.m_layerId > 0)
1983      {
1984        printf( "\n\nThis bitstream is ended with EOB NALU that has layer id greater than 0\n" );
1985      }
1986#endif
1987      return false;
1988     
1989     
1990    case NAL_UNIT_RESERVED_VCL_N10:
1991    case NAL_UNIT_RESERVED_VCL_R11:
1992    case NAL_UNIT_RESERVED_VCL_N12:
1993    case NAL_UNIT_RESERVED_VCL_R13:
1994    case NAL_UNIT_RESERVED_VCL_N14:
1995    case NAL_UNIT_RESERVED_VCL_R15:
1996     
1997    case NAL_UNIT_RESERVED_IRAP_VCL22:
1998    case NAL_UNIT_RESERVED_IRAP_VCL23:
1999     
2000    case NAL_UNIT_RESERVED_VCL24:
2001    case NAL_UNIT_RESERVED_VCL25:
2002    case NAL_UNIT_RESERVED_VCL26:
2003    case NAL_UNIT_RESERVED_VCL27:
2004    case NAL_UNIT_RESERVED_VCL28:
2005    case NAL_UNIT_RESERVED_VCL29:
2006    case NAL_UNIT_RESERVED_VCL30:
2007    case NAL_UNIT_RESERVED_VCL31:
2008     
2009    case NAL_UNIT_FILLER_DATA:
2010    case NAL_UNIT_RESERVED_NVCL41:
2011    case NAL_UNIT_RESERVED_NVCL42:
2012    case NAL_UNIT_RESERVED_NVCL43:
2013    case NAL_UNIT_RESERVED_NVCL44:
2014    case NAL_UNIT_RESERVED_NVCL45:
2015    case NAL_UNIT_RESERVED_NVCL46:
2016    case NAL_UNIT_RESERVED_NVCL47:
2017    case NAL_UNIT_UNSPECIFIED_48:
2018    case NAL_UNIT_UNSPECIFIED_49:
2019    case NAL_UNIT_UNSPECIFIED_50:
2020    case NAL_UNIT_UNSPECIFIED_51:
2021    case NAL_UNIT_UNSPECIFIED_52:
2022    case NAL_UNIT_UNSPECIFIED_53:
2023    case NAL_UNIT_UNSPECIFIED_54:
2024    case NAL_UNIT_UNSPECIFIED_55:
2025    case NAL_UNIT_UNSPECIFIED_56:
2026    case NAL_UNIT_UNSPECIFIED_57:
2027    case NAL_UNIT_UNSPECIFIED_58:
2028    case NAL_UNIT_UNSPECIFIED_59:
2029    case NAL_UNIT_UNSPECIFIED_60:
2030    case NAL_UNIT_UNSPECIFIED_61:
2031    case NAL_UNIT_UNSPECIFIED_62:
2032    case NAL_UNIT_UNSPECIFIED_63:
2033
2034    default:
2035      assert (0);
2036  }
2037
2038  return false;
2039}
2040
2041/** Function for checking if picture should be skipped because of association with a previous BLA picture
2042 * \param iPOCLastDisplay POC of last picture displayed
2043 * \returns true if the picture should be skipped
2044 * This function skips all TFD pictures that follow a BLA picture
2045 * in decoding order and precede it in output order.
2046 */
2047Bool TDecTop::isSkipPictureForBLA(Int& iPOCLastDisplay)
2048{
2049  if ((m_associatedIRAPType == NAL_UNIT_CODED_SLICE_BLA_N_LP || m_associatedIRAPType == NAL_UNIT_CODED_SLICE_BLA_W_LP || m_associatedIRAPType == NAL_UNIT_CODED_SLICE_BLA_W_RADL) && 
2050       m_apcSlicePilot->getPOC() < m_pocCRA && (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N))
2051  {
2052    iPOCLastDisplay++;
2053    return true;
2054  }
2055  return false;
2056}
2057
2058/** Function for checking if picture should be skipped because of random access
2059 * \param iSkipFrame skip frame counter
2060 * \param iPOCLastDisplay POC of last picture displayed
2061 * \returns true if the picture shold be skipped in the random access.
2062 * This function checks the skipping of pictures in the case of -s option random access.
2063 * All pictures prior to the random access point indicated by the counter iSkipFrame are skipped.
2064 * It also checks the type of Nal unit type at the random access point.
2065 * 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.
2066 * If the random access point is IDR all pictures after the random access point are decoded.
2067 * If the random access point is none of the above, a warning is issues, and decoding of pictures with POC
2068 * equal to or greater than the random access point POC is attempted. For non IDR/CRA/BLA random
2069 * access point there is no guarantee that the decoder will not crash.
2070 */
2071Bool TDecTop::isRandomAccessSkipPicture(Int& iSkipFrame,  Int& iPOCLastDisplay)
2072{
2073  if (iSkipFrame) 
2074  {
2075    iSkipFrame--;   // decrement the counter
2076    return true;
2077  }
2078  else if (m_pocRandomAccess == MAX_INT) // start of random access point, m_pocRandomAccess has not been set yet.
2079  {
2080    if (   m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA
2081        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
2082        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
2083        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL )
2084    {
2085      // set the POC random access since we need to skip the reordered pictures in the case of CRA/CRANT/BLA/BLANT.
2086      m_pocRandomAccess = m_apcSlicePilot->getPOC();
2087    }
2088    else if ( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
2089    {
2090      m_pocRandomAccess = -MAX_INT; // no need to skip the reordered pictures in IDR, they are decodable.
2091    }
2092    else 
2093    {
2094      static Bool warningMessage = false;
2095      if(!warningMessage)
2096      {
2097        printf("\nWarning: this is not a valid random access point and the data is discarded until the first CRA picture");
2098        warningMessage = true;
2099      }
2100      return true;
2101    }
2102  }
2103  // skip the reordered pictures, if necessary
2104  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))
2105  {
2106    iPOCLastDisplay++;
2107    return true;
2108  }
2109  // if we reach here, then the picture is not skipped.
2110  return false; 
2111}
2112
2113#if SVC_EXTENSION
2114#if VPS_EXTN_DIRECT_REF_LAYERS
2115TDecTop* TDecTop::getRefLayerDec( UInt refLayerIdc )
2116{
2117  TComVPS* vps = m_parameterSetManagerDecoder.getActiveVPS();
2118  if( vps->getNumDirectRefLayers( m_layerId ) <= 0 )
2119  {
2120    return (TDecTop *)getLayerDec( 0 );
2121  }
2122 
2123  return (TDecTop *)getLayerDec( vps->getRefLayerId( m_layerId, refLayerIdc ) );
2124}
2125#endif
2126
2127#if VPS_EXTN_DIRECT_REF_LAYERS
2128Void TDecTop::setRefLayerParams( TComVPS* vps )
2129{
2130  for(UInt layer = 0; layer < m_numLayer; layer++)
2131  {
2132    TDecTop *decTop = (TDecTop *)getLayerDec(layer);
2133    decTop->setNumSamplePredRefLayers(0);
2134    decTop->setNumMotionPredRefLayers(0);
2135    decTop->setNumDirectRefLayers(0);
2136    for(Int i = 0; i < MAX_VPS_LAYER_ID_PLUS1; i++)
2137    {
2138      decTop->setSamplePredEnabledFlag(i, false);
2139      decTop->setMotionPredEnabledFlag(i, false);
2140      decTop->setSamplePredRefLayerId(i, 0);
2141      decTop->setMotionPredRefLayerId(i, 0);
2142    }
2143    for(Int j = 0; j < layer; j++)
2144    {
2145      if (vps->getDirectDependencyFlag(layer, j))
2146      {
2147        decTop->setRefLayerId(decTop->getNumDirectRefLayers(), vps->getLayerIdInNuh(layer));
2148        decTop->setNumDirectRefLayers(decTop->getNumDirectRefLayers() + 1);
2149
2150        Int samplePredEnabledFlag = (vps->getDirectDependencyType(layer, j) + 1) & 1;
2151        decTop->setSamplePredEnabledFlag(j, samplePredEnabledFlag == 1 ? true : false);
2152        decTop->setNumSamplePredRefLayers(decTop->getNumSamplePredRefLayers() + samplePredEnabledFlag);
2153
2154        Int motionPredEnabledFlag = ((vps->getDirectDependencyType(layer, j) + 1) & 2) >> 1;
2155        decTop->setMotionPredEnabledFlag(j, motionPredEnabledFlag == 1 ? true : false);
2156        decTop->setNumMotionPredRefLayers(decTop->getNumMotionPredRefLayers() + motionPredEnabledFlag);
2157      }
2158    }
2159  }
2160  for ( Int i = 1, mIdx = 0, sIdx = 0; i < m_numLayer; i++ )
2161  {
2162    Int iNuhLId = vps->getLayerIdInNuh(i);
2163    TDecTop *decTop = (TDecTop *)getLayerDec(iNuhLId);
2164    for ( Int j = 0; j < i; j++ )
2165    {
2166      if (decTop->getMotionPredEnabledFlag(j))
2167      {
2168        decTop->setMotionPredRefLayerId(mIdx++, vps->getLayerIdInNuh(j));
2169      }
2170      if (decTop->getSamplePredEnabledFlag(j))
2171      {
2172        decTop->setSamplePredRefLayerId(sIdx++, vps->getLayerIdInNuh(j));
2173      }
2174    }
2175  }
2176}
2177
2178#endif
2179
2180#if OUTPUT_LAYER_SET_INDEX
2181Void TDecTop::checkValueOfTargetOutputLayerSetIdx(TComVPS *vps)
2182{
2183  CommonDecoderParams* params = this->getCommonDecoderParams();
2184
2185  assert( params->getTargetLayerId() < vps->getMaxLayers() );
2186
2187  if( params->getValueCheckedFlag() )
2188  {
2189    return; // Already checked
2190  }
2191  if( params->getTargetOutputLayerSetIdx() == -1 )  // Output layer set index not specified
2192  {
2193    Bool layerSetMatchFound = false;
2194    // Output layer set index not assigned.
2195    // Based on the value of targetLayerId, check if any of the output layer matches
2196    // Currently, the target layer ID in the encoder assumes that all the layers are decoded   
2197    // Check if any of the output layer sets match this description
2198    for(Int i = 0; i < vps->getNumOutputLayerSets(); i++)
2199    {
2200      Bool layerSetMatchFlag = true;
2201      Int layerSetIdx = vps->getOutputLayerSetIdx( i );
2202      if( vps->getNumLayersInIdList( layerSetIdx ) == params->getTargetLayerId() + 1 )
2203      {
2204        for(Int j = 0; j < vps->getNumLayersInIdList( layerSetIdx ); j++)
2205        {
2206          if( vps->getLayerSetLayerIdList( layerSetIdx, j ) != j )
2207          {
2208            layerSetMatchFlag = false;
2209            break;
2210          }
2211        }
2212      }
2213      else
2214      {
2215        layerSetMatchFlag = false;
2216      }
2217     
2218      if( layerSetMatchFlag ) // Potential output layer set candidate found
2219      {
2220        // If target dec layer ID list is also included - check if they match
2221        if( params->getTargetDecLayerIdSet() )
2222        {
2223          if( params->getTargetDecLayerIdSet()->size() ) 
2224          {
2225            for(Int j = 0; j < vps->getNumLayersInIdList( layerSetIdx ); j++)
2226            {
2227              if( *(params->getTargetDecLayerIdSet()->begin() + j) != vps->getLayerIdInNuh(vps->getLayerSetLayerIdList( layerSetIdx, j )))
2228              {
2229                layerSetMatchFlag = false;
2230              }
2231            }
2232          }
2233        }
2234        if( layerSetMatchFlag ) // The target dec layer ID list also matches, if present
2235        {
2236          // Match found
2237          layerSetMatchFound = true;
2238          params->setTargetOutputLayerSetIdx( i );
2239          params->setValueCheckedFlag( true );
2240          break;
2241        }
2242      }
2243    }
2244    assert( layerSetMatchFound ); // No output layer set matched the value of either targetLayerId or targetdeclayerIdlist
2245  }   
2246  else // Output layer set index is assigned - check if the values match
2247  {
2248    // Check if the target decoded layer is the highest layer in the list
2249    assert( params->getTargetOutputLayerSetIdx() < vps->getNumLayerSets() );
2250    Int layerSetIdx = vps->getOutputLayerSetIdx( params->getTargetOutputLayerSetIdx() );  // Index to the layer set
2251    assert( params->getTargetLayerId() == vps->getNumLayersInIdList( layerSetIdx ) - 1);
2252
2253    Bool layerSetMatchFlag = true;
2254    for(Int j = 0; j < vps->getNumLayersInIdList( layerSetIdx ); j++)
2255    {
2256      if( vps->getLayerSetLayerIdList( layerSetIdx, j ) != j )
2257      {
2258        layerSetMatchFlag = false;
2259        break;
2260      }
2261    }
2262
2263    assert(layerSetMatchFlag);    // Signaled output layer set index does not match targetOutputLayerId.
2264   
2265    // Check if the targetdeclayerIdlist matches the output layer set
2266    if( params->getTargetDecLayerIdSet() )
2267    {
2268      if( params->getTargetDecLayerIdSet()->size() ) 
2269      {
2270        for(Int i = 0; i < vps->getNumLayersInIdList( layerSetIdx ); i++)
2271        {
2272          assert( *(params->getTargetDecLayerIdSet()->begin() + i) == vps->getLayerIdInNuh(vps->getLayerSetLayerIdList( layerSetIdx, i )));
2273        }
2274      }
2275    }
2276    params->setValueCheckedFlag( true );
2277
2278  }
2279}
2280#endif
2281#if RESOLUTION_BASED_DPB
2282Void TDecTop::assignSubDpbs(TComVPS *vps)
2283{
2284  if( m_subDpbIdx == -1 ) // Sub-DPB index is not already assigned
2285  {
2286    Int lsIdx = vps->getOutputLayerSetIdx( getCommonDecoderParams()->getTargetOutputLayerSetIdx() );
2287
2288    Int layerIdx = vps->findLayerIdxInLayerSet( lsIdx, getLayerId() );
2289    assert( layerIdx != -1 ); // Current layer should be found in the layer set.
2290
2291    // Copy from the active VPS based on the layer ID.
2292    m_subDpbIdx = vps->getSubDpbAssigned( lsIdx, layerIdx );
2293  }
2294}
2295#endif
2296#endif //SVC_EXTENSION
2297//! \}
Note: See TracBrowser for help on using the repository browser.