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

Last change on this file since 1194 was 1191, checked in by seregin, 10 years ago

macro cleanup: R0226_SLICE_TMVP

  • Property svn:eol-style set to native
File size: 102.3 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#if RExt__DECODER_DEBUG_BIT_STATISTICS
41#include "TLibCommon/TComCodingStatistics.h"
42#endif
43
44#if SVC_EXTENSION
45UInt  TDecTop::m_prevPOC = MAX_UINT;
46UInt  TDecTop::m_uiPrevLayerId = MAX_UINT;
47Bool  TDecTop::m_bFirstSliceInSequence = true;
48#if POC_RESET_RESTRICTIONS
49Bool  TDecTop::m_checkPocRestrictionsForCurrAu       = false;
50Int   TDecTop::m_pocResetIdcOrCurrAu                 = -1;
51Bool  TDecTop::m_baseLayerIdrFlag                    = false;
52Bool  TDecTop::m_baseLayerPicPresentFlag             = false;
53Bool  TDecTop::m_baseLayerIrapFlag                   = false;
54Bool  TDecTop::m_nonBaseIdrPresentFlag               = false;
55Int   TDecTop::m_nonBaseIdrType                      = -1;
56Bool  TDecTop::m_picNonIdrWithRadlPresentFlag        = false;
57Bool  TDecTop::m_picNonIdrNoLpPresentFlag            = false;
58#endif
59#if POC_RESET_VALUE_RESTRICTION
60Int   TDecTop::m_crossLayerPocResetPeriodId          = -1;
61Int   TDecTop::m_crossLayerPocResetIdc               = -1;
62#endif
63#endif
64
65//! \ingroup TLibDecoder
66//! \{
67
68TDecTop::TDecTop()
69  : m_pDecodedSEIOutputStream(NULL)
70{
71  m_pcPic = 0;
72  m_iMaxRefPicNum = 0;
73#if ENC_DEC_TRACE
74  if (g_hTrace == NULL)
75  {
76    g_hTrace = fopen( "TraceDec.txt", "wb" );
77  }
78  g_bJustDoIt = g_bEncDecTraceDisable;
79  g_nSymbolCounter = 0;
80#endif
81  m_associatedIRAPType = NAL_UNIT_INVALID;
82  m_pocCRA = 0;
83  m_pocRandomAccess = MAX_INT;
84#if !SVC_EXTENSION
85  m_prevPOC                = MAX_INT;
86#endif
87  m_bFirstSliceInPicture    = true;
88#if !SVC_EXTENSION
89  m_bFirstSliceInSequence   = true;
90#endif
91  m_prevSliceSkipped = false;
92  m_skippedPOC = 0;
93  m_bFirstSliceInBitstream  = true;
94  m_lastPOCNoOutputPriorPics = -1;
95  m_craNoRaslOutputFlag = false;
96  m_isNoOutputPriorPics = false;
97
98#if SVC_EXTENSION
99  m_layerId = 0;
100  m_smallestLayerId = 0;
101#if AVC_BASE
102  m_pBLReconFile = NULL;
103#endif
104  memset(m_cIlpPic, 0, sizeof(m_cIlpPic));
105#if Q0177_EOS_CHECKS
106  m_isLastNALWasEos = false;
107#endif
108  m_lastPicHasEos = false;
109#if NO_CLRAS_OUTPUT_FLAG
110  m_noClrasOutputFlag          = false;
111  m_layerInitializedFlag       = false;
112  m_firstPicInLayerDecodedFlag = false; 
113#endif
114#if POC_RESET_IDC_DECODER
115  m_parseIdc = -1;
116  m_lastPocPeriodId = -1;
117  m_prevPicOrderCnt = 0;
118#endif
119#if Q0048_CGS_3D_ASYMLUT
120  m_pColorMappedPic = NULL;
121#endif
122
123#if POC_RESET_RESTRICTIONS
124  resetPocRestrictionCheckParameters();
125#endif
126#if P0297_VPS_POC_LSB_ALIGNED_FLAG
127  m_pocResettingFlag        = false;
128  m_pocDecrementedInDPBFlag = false;
129#endif
130#if CONFORMANCE_BITSTREAM_MODE
131  m_confModeFlag = false;
132#endif
133  m_isOutputLayerFlag = false;
134#endif //SVC_EXTENSION
135}
136
137TDecTop::~TDecTop()
138{
139#if ENC_DEC_TRACE
140  if (g_hTrace != stdout)
141  {
142    fclose( g_hTrace );
143  }
144#endif
145#if Q0048_CGS_3D_ASYMLUT
146  if(m_pColorMappedPic)
147  {
148    m_pColorMappedPic->destroy();
149    delete m_pColorMappedPic;
150    m_pColorMappedPic = NULL;
151  }
152#endif
153}
154
155Void TDecTop::create()
156{
157#if SVC_EXTENSION
158  m_cGopDecoder.create( m_layerId );
159#else
160  m_cGopDecoder.create();
161#endif
162  m_apcSlicePilot = new TComSlice;
163  m_uiSliceIdx = 0;
164}
165
166Void TDecTop::destroy()
167{
168  m_cGopDecoder.destroy();
169
170  delete m_apcSlicePilot;
171  m_apcSlicePilot = NULL;
172
173  m_cSliceDecoder.destroy();
174#if SVC_EXTENSION
175  for(Int i=0; i<MAX_NUM_REF; i++)
176  {
177    if(m_cIlpPic[i])
178    {
179      m_cIlpPic[i]->destroy();
180      delete m_cIlpPic[i];
181      m_cIlpPic[i] = NULL;
182    }
183  }
184#endif
185}
186
187Void TDecTop::init()
188{
189#if !SVC_EXTENSION
190  // initialize ROM
191  initROM();
192#endif
193#if SVC_EXTENSION
194  m_cGopDecoder.init( m_ppcTDecTop, &m_cEntropyDecoder, &m_cSbacDecoder, &m_cBinCABAC, &m_cCavlcDecoder, &m_cSliceDecoder, &m_cLoopFilter, &m_cSAO);
195  m_cSliceDecoder.init( &m_cEntropyDecoder, &m_cCuDecoder, m_cSAO.getSaoMaxOffsetQVal() );
196#else
197  m_cGopDecoder.init( &m_cEntropyDecoder, &m_cSbacDecoder, &m_cBinCABAC, &m_cCavlcDecoder, &m_cSliceDecoder, &m_cLoopFilter, &m_cSAO);
198  m_cSliceDecoder.init( &m_cEntropyDecoder, &m_cCuDecoder );
199#endif
200  m_cEntropyDecoder.init(&m_cPrediction);
201}
202
203Void TDecTop::deletePicBuffer ( )
204{
205  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
206  Int iSize = Int( m_cListPic.size() );
207
208  for (Int i = 0; i < iSize; i++ )
209  {
210    TComPic* pcPic = *(iterPic++);
211#if SVC_EXTENSION
212    if( pcPic )
213    {
214      pcPic->destroy();
215
216      delete pcPic;
217      pcPic = NULL;
218    }
219#else
220    pcPic->destroy();
221
222    delete pcPic;
223    pcPic = NULL;
224#endif
225  }
226
227  m_cSAO.destroy();
228
229  m_cLoopFilter.        destroy();
230 
231#if !SVC_EXTENSION
232  // destroy ROM
233  destroyROM();
234#endif
235}
236
237Void TDecTop::xGetNewPicBuffer ( TComSlice* pcSlice, TComPic*& rpcPic )
238{
239  Int  numReorderPics[MAX_TLAYER];
240#if SVC_EXTENSION
241  Window &conformanceWindow = pcSlice->getConformanceWindow();
242#else
243  Window &conformanceWindow = pcSlice->getSPS()->getConformanceWindow();
244#endif
245  Window defaultDisplayWindow = pcSlice->getSPS()->getVuiParametersPresentFlag() ? pcSlice->getSPS()->getVuiParameters()->getDefaultDisplayWindow() : Window();
246
247  for( Int temporalLayer=0; temporalLayer < MAX_TLAYER; temporalLayer++)
248  {
249#if SVC_EXTENSION
250    if( m_commonDecoderParams->getTargetOutputLayerSetIdx() == 0 )
251    {
252      assert( this->getLayerId() == 0 );
253      numReorderPics[temporalLayer] = pcSlice->getSPS()->getNumReorderPics(temporalLayer);
254    }
255    else
256    {
257      TComVPS *vps = pcSlice->getVPS();
258      // SHM decoders will use DPB size table in the VPS to determine the number of reorder pictures.
259      numReorderPics[temporalLayer] = vps->getMaxVpsNumReorderPics( getCommonDecoderParams()->getTargetOutputLayerSetIdx() , temporalLayer);
260    }
261#else
262    numReorderPics[temporalLayer] = pcSlice->getSPS()->getNumReorderPics(temporalLayer);
263#endif
264  }
265
266#if SVC_EXTENSION
267  if( m_commonDecoderParams->getTargetOutputLayerSetIdx() == 0 )
268  {
269    assert( this->getLayerId() == 0 );
270    m_iMaxRefPicNum = pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getTLayer());     // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
271  }
272  else
273  {
274    m_iMaxRefPicNum = pcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1( m_commonDecoderParams->getTargetOutputLayerSetIdx(), pcSlice->getVPS()->getLayerIdcForOls( pcSlice->getVPS()->getOutputLayerSetIdx( m_commonDecoderParams->getTargetOutputLayerSetIdx()), pcSlice->getLayerId() ), pcSlice->getTLayer() ) + 1; // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
275  }
276
277  m_iMaxRefPicNum += 1; // it should be updated if more than 1 resampling picture is used
278#else
279  m_iMaxRefPicNum = pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getTLayer());     // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
280#endif
281 
282  if (m_cListPic.size() < (UInt)m_iMaxRefPicNum)
283  {
284    rpcPic = new TComPic();
285
286#if SVC_EXTENSION
287    if(m_layerId > 0)
288    {
289      xSetSpatialEnhLayerFlag( pcSlice, rpcPic );
290    }
291
292#if REPN_FORMAT_IN_VPS
293    rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), pcSlice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
294                     conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
295#else
296    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
297                     conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
298#endif
299
300#else //SVC_EXTENSION
301    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
302                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
303#endif //SVC_EXTENSION
304   
305    m_cListPic.pushBack( rpcPic );
306
307    return;
308  }
309
310  Bool bBufferIsAvailable = false;
311  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
312  while (iterPic != m_cListPic.end())
313  {
314    rpcPic = *(iterPic++);
315    if ( rpcPic->getReconMark() == false && rpcPic->getOutputMark() == false)
316    {
317      rpcPic->setOutputMark(false);
318      bBufferIsAvailable = true;
319      break;
320    }
321
322    if ( rpcPic->getSlice( 0 )->isReferenced() == false  && rpcPic->getOutputMark() == false)
323    {
324#if !SVC_EXTENSION
325      rpcPic->setOutputMark(false);
326#endif
327      rpcPic->setReconMark( false );
328      rpcPic->getPicYuvRec()->setBorderExtension( false );
329      bBufferIsAvailable = true;
330      break;
331    }
332  }
333
334  if ( !bBufferIsAvailable )
335  {
336    //There is no room for this picture, either because of faulty encoder or dropped NAL. Extend the buffer.
337    m_iMaxRefPicNum++;
338    rpcPic = new TComPic();
339    m_cListPic.pushBack( rpcPic );
340  }
341  rpcPic->destroy();
342
343#if SVC_EXTENSION
344  if( m_layerId > 0 )
345  {
346    xSetSpatialEnhLayerFlag( pcSlice, rpcPic );
347  }
348
349#if REPN_FORMAT_IN_VPS
350  rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), pcSlice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
351                   conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
352#else
353  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
354                   conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
355#endif
356#else  //SVC_EXTENSION
357  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
358                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
359#endif //SVC_EXTENSION
360}
361
362Void TDecTop::executeLoopFilters(Int& poc, TComList<TComPic*>*& rpcListPic)
363{
364  if (!m_pcPic)
365  {
366    /* nothing to deblock */
367    return;
368  }
369
370  TComPic*   pcPic         = m_pcPic;
371
372  // Execute Deblock + Cleanup
373
374  m_cGopDecoder.filterPicture(pcPic);
375
376#if SVC_EXTENSION
377  if( this->getLayerDec(pcPic->getLayerId())->m_isOutputLayerFlag == false )
378  {
379    pcPic->setOutputMark( false );
380  }
381#endif
382
383  TComSlice::sortPicList( m_cListPic ); // sorting for application output
384  poc                 = pcPic->getSlice(m_uiSliceIdx-1)->getPOC();
385  rpcListPic          = &m_cListPic;
386  m_cCuDecoder.destroy();
387  m_bFirstSliceInPicture  = true;
388
389  return;
390}
391
392Void TDecTop::checkNoOutputPriorPics (TComList<TComPic*>* pcListPic)
393{
394  if (!pcListPic || !m_isNoOutputPriorPics) return;
395
396  TComList<TComPic*>::iterator  iterPic   = pcListPic->begin();
397
398  while (iterPic != pcListPic->end())
399  {
400    TComPic* pcPicTmp = *(iterPic++);
401    if (m_lastPOCNoOutputPriorPics != pcPicTmp->getPOC())
402    {
403      pcPicTmp->setOutputMark(false);
404    }
405  }
406}
407
408Void TDecTop::xCreateLostPicture(Int iLostPoc)
409{
410  printf("\ninserting lost poc : %d\n",iLostPoc);
411  TComSlice cFillSlice;
412  cFillSlice.setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
413  cFillSlice.setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
414#if SVC_EXTENSION
415  cFillSlice.setVPS( m_parameterSetManagerDecoder.getFirstVPS() );
416  cFillSlice.initSlice( m_layerId );
417#else
418  cFillSlice.initSlice();
419#endif
420  TComPic *cFillPic;
421  xGetNewPicBuffer(&cFillSlice,cFillPic);
422  cFillPic->getSlice(0)->setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
423  cFillPic->getSlice(0)->setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
424#if SVC_EXTENSION
425  cFillPic->getSlice(0)->setVPS( m_parameterSetManagerDecoder.getFirstVPS() );
426  cFillPic->getSlice(0)->initSlice( m_layerId );
427#else
428  cFillPic->getSlice(0)->initSlice();
429#endif
430 
431  TComList<TComPic*>::iterator iterPic = m_cListPic.begin();
432  Int closestPoc = 1000000;
433  while ( iterPic != m_cListPic.end())
434  {
435    TComPic * rpcPic = *(iterPic++);
436    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())
437    {
438      closestPoc=abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc);
439    }
440  }
441  iterPic = m_cListPic.begin();
442  while ( iterPic != m_cListPic.end())
443  {
444    TComPic *rpcPic = *(iterPic++);
445    if(abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc)==closestPoc&&rpcPic->getPicSym()->getSlice(0)->getPOC()!=m_apcSlicePilot->getPOC())
446    {
447      printf("copying picture %d to %d (%d)\n",rpcPic->getPicSym()->getSlice(0)->getPOC() ,iLostPoc,m_apcSlicePilot->getPOC());
448      rpcPic->getPicYuvRec()->copyToPic(cFillPic->getPicYuvRec());
449      break;
450    }
451  }
452  cFillPic->setCurrSliceIdx(0);
453  for(Int ctuRsAddr=0; ctuRsAddr<cFillPic->getNumberOfCtusInFrame(); ctuRsAddr++)
454  {
455    cFillPic->getCtu(ctuRsAddr)->initCtu(cFillPic, ctuRsAddr);
456  }
457  cFillPic->getSlice(0)->setReferenced(true);
458  cFillPic->getSlice(0)->setPOC(iLostPoc);
459  cFillPic->setReconMark(true);
460  cFillPic->setOutputMark(true);
461  if(m_pocRandomAccess == MAX_INT)
462  {
463    m_pocRandomAccess = iLostPoc;
464  }
465}
466
467
468Void TDecTop::xActivateParameterSets()
469{
470  m_parameterSetManagerDecoder.applyPrefetchedPS();
471
472  TComPPS *pps = m_parameterSetManagerDecoder.getPPS(m_apcSlicePilot->getPPSId());
473  assert (pps != 0);
474
475  TComSPS *sps = m_parameterSetManagerDecoder.getSPS(pps->getSPSId());
476  assert (sps != 0);
477
478  if (false == m_parameterSetManagerDecoder.activatePPS(m_apcSlicePilot->getPPSId(),m_apcSlicePilot->isIRAP()))
479  {
480    printf ("Parameter set activation failed!");
481    assert (0);
482  }
483
484#if SVC_EXTENSION
485#if SCALINGLIST_INFERRING
486  // scaling list settings and checks
487  TComVPS *activeVPS = m_parameterSetManagerDecoder.getActiveVPS();
488  TComSPS *activeSPS = m_parameterSetManagerDecoder.getActiveSPS();
489  TComPPS *activePPS = m_parameterSetManagerDecoder.getActivePPS();
490
491  if( activeSPS->getInferScalingListFlag() )
492  {
493    UInt refLayerId = activeSPS->getScalingListRefLayerId();
494    TComSPS *refSps = m_ppcTDecTop[refLayerId]->getParameterSetManager()->getActiveSPS(); assert( refSps != NULL );
495
496    // 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
497    if( activeVPS->getNonHEVCBaseLayerFlag() )
498    {
499      assert( refLayerId > 0 );
500    }
501
502    // 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
503    // 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
504    assert( refSps->getInferScalingListFlag() == false );
505
506    // 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,
507    // 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
508    assert( activeVPS->getRecursiveRefLayerFlag( activeSPS->getLayerId(), refLayerId ) == true );
509   
510    if( activeSPS->getScalingList() != refSps->getScalingList() )
511    {
512      // delete created instance of scaling list since it will be inferred
513      delete activeSPS->getScalingList();
514
515      // infer scaling list
516      activeSPS->setScalingList( refSps->getScalingList() );
517    }
518  }
519
520  if( activePPS->getInferScalingListFlag() )
521  {
522    UInt refLayerId = activePPS->getScalingListRefLayerId();
523    TComPPS *refPps = m_ppcTDecTop[refLayerId]->getParameterSetManager()->getActivePPS(); assert( refPps != NULL );
524
525    // 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
526    if( activeVPS->getNonHEVCBaseLayerFlag() )
527    {
528      assert( refLayerId > 0 );
529    }
530
531    // 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
532    // 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
533    assert( refPps->getInferScalingListFlag() == false );
534
535    // 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,
536    // 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
537    assert( activeVPS->getRecursiveRefLayerFlag( activePPS->getLayerId(), refLayerId ) == true );
538   
539    if( activePPS->getScalingList() != refPps->getScalingList() )
540    {
541      // delete created instance of scaling list since it will be inferred
542      delete activePPS->getScalingList();
543
544      // infer scaling list
545      activePPS->setScalingList( refPps->getScalingList() );
546    }
547
548  }
549#endif
550
551#if AVC_BASE
552  if( activeVPS->getNonHEVCBaseLayerFlag() )
553  {
554    TComPic* pBLPic = (*m_ppcTDecTop[0]->getListPic()->begin());
555    if( m_layerId > 0 && pBLPic->getPicYuvRec() == NULL )
556    {
557      UInt refLayerId = 0;
558      RepFormat* repFormat = activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(refLayerId) );
559
560      Int  numReorderPics[MAX_TLAYER];
561      Window defaultDisplayWindow;
562
563#if AUXILIARY_PICTURES
564      pBLPic->create( repFormat->getPicWidthVpsInLumaSamples(), repFormat->getPicHeightVpsInLumaSamples(), repFormat->getChromaFormatVpsIdc(), activeSPS->getMaxCUWidth(), activeSPS->getMaxCUHeight(), activeSPS->getMaxCUDepth(), repFormat->getConformanceWindowVps(), defaultDisplayWindow, numReorderPics, NULL, true);
565#else
566      pBLPic->create( repFormat->getPicWidthVpsInLumaSamples(), repFormat->getPicHeightVpsInLumaSamples(), activeSPS->getMaxCUWidth(), activeSPS->getMaxCUHeight(), activeSPS->getMaxCUDepth(), repFormat->getConformanceWindowVps(), defaultDisplayWindow, numReorderPics, NULL, true);
567#endif
568      // it is needed where the VPS is accessed through the slice
569      pBLPic->getSlice(0)->setVPS( activeVPS );
570
571#if O0194_DIFFERENT_BITDEPTH_EL_BL
572      g_bitDepthLayer[CHANNEL_TYPE_LUMA][0] = repFormat->getBitDepthVpsLuma();
573      g_bitDepthLayer[CHANNEL_TYPE_CHROMA][0] = repFormat->getBitDepthVpsChroma();
574#endif
575    }
576  }
577#endif
578
579  if( m_layerId > 0 )
580  {
581    // When not present sps_max_sub_layers_minus1 is inferred to be equal to vps_max_sub_layers_minus1.
582    sps->setMaxTLayers( activeVPS->getMaxTLayers() );
583
584    // When not present sps_temporal_id_nesting_flag is inferred to be equal to vps_temporal_id_nesting_flag
585    sps->setTemporalIdNestingFlag( (sps->getMaxTLayers() > 1) ? activeVPS->getTemporalNestingFlag() : true );
586
587    // 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,
588    // 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.
589    for(UInt i=0; i < sps->getMaxTLayers(); i++)
590    {
591      // to avoid compiler warning "array subscript is above array bounds"
592      assert( i < MAX_TLAYER );
593
594      // 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,
595      // due to MultiLayerExtSpsFlag being equal to 1, for a layer that refers to the SPS and has nuh_layer_id equal to currLayerId,
596      // the value of sps_max_dec_pic_buffering_minus1[ i ] is inferred to be equal to max_vps_dec_pic_buffering_minus1[ TargetOlsIdx ][ layerIdx ][ i ] of the active VPS,
597      // where layerIdx is equal to the value such that LayerSetLayerIdList[ TargetDecLayerSetIdx ][ layerIdx ] is equal to currLayerId.
598      if( sps->getMultiLayerExtSpsFlag() )
599      {
600        sps->setMaxDecPicBuffering( activeVPS->getMaxVpsDecPicBufferingMinus1( m_commonDecoderParams->getTargetOutputLayerSetIdx(), activeVPS->getLayerIdcForOls( activeVPS->getOutputLayerSetIdx( m_commonDecoderParams->getTargetOutputLayerSetIdx() ), m_layerId ), i) + 1, i);
601      }
602
603      // The value of sps_max_dec_pic_buffering_minus1[ i ] shall be less than or equal to vps_max_dec_pic_buffering_minus1[ i ] for each value of i.
604      assert( sps->getMaxDecPicBuffering(i) <= activeVPS->getMaxDecPicBuffering(i) );     
605    }
606
607    UInt layerIdx = activeVPS->getLayerIdxInVps( m_layerId );
608
609    if( activeVPS->getBaseLayerPSCompatibilityFlag(layerIdx) )
610    {
611      RepFormat* repFormat = activeVPS->getVpsRepFormat(activeVPS->getVpsRepFormatIdx(layerIdx));
612
613      assert( pps->getLayerId() == 0 );
614      assert( sps->getLayerId() == 0 );
615      assert( repFormat->getChromaFormatVpsIdc() == sps->getChromaFormatIdc() );
616      assert( repFormat->getSeparateColourPlaneVpsFlag() == 0 );
617      assert( repFormat->getPicHeightVpsInLumaSamples() == sps->getPicHeightInLumaSamples() );
618      assert( repFormat->getPicWidthVpsInLumaSamples()  == sps->getPicWidthInLumaSamples() );
619      assert( repFormat->getBitDepthVpsLuma()   == sps->getBitDepth(CHANNEL_TYPE_LUMA) );
620      assert( repFormat->getBitDepthVpsChroma() == sps->getBitDepth(CHANNEL_TYPE_CHROMA) );
621      assert( repFormat->getConformanceWindowVps().getWindowLeftOffset()   == sps->getConformanceWindow().getWindowLeftOffset() );
622      assert( repFormat->getConformanceWindowVps().getWindowRightOffset()  == sps->getConformanceWindow().getWindowRightOffset() );
623      assert( repFormat->getConformanceWindowVps().getWindowTopOffset()    == sps->getConformanceWindow().getWindowTopOffset() );
624      assert( repFormat->getConformanceWindowVps().getWindowBottomOffset() == sps->getConformanceWindow().getWindowBottomOffset() );
625    }   
626  }
627
628  //Conformance checking for rep format -- rep format of current picture of current layer shall never be greater rep format defined in VPS for the current layer
629  UInt layerIdx = activeVPS->getLayerIdxInVps(m_apcSlicePilot->getLayerId());
630
631  if ( activeVPS->getVpsExtensionFlag() == 1 && (m_apcSlicePilot->getLayerId() == 0 || sps->getV1CompatibleSPSFlag() == 1) )
632  {
633    assert( sps->getPicWidthInLumaSamples()        <= activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(layerIdx) )->getPicWidthVpsInLumaSamples() );
634    assert( sps->getPicHeightInLumaSamples()       <= activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(layerIdx) )->getPicHeightVpsInLumaSamples() );
635    assert( sps->getChromaFormatIdc()              <= activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(layerIdx) )->getChromaFormatVpsIdc() );
636    assert( sps->getBitDepth(CHANNEL_TYPE_LUMA)    <= activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(layerIdx) )->getBitDepthVpsLuma() );
637    assert( sps->getBitDepth(CHANNEL_TYPE_CHROMA)  <= activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(layerIdx) )->getBitDepthVpsChroma() );
638  }
639  else if ( activeVPS->getVpsExtensionFlag() == 1 )
640  {
641    assert( activeVPS->getVpsRepFormat( sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : activeVPS->getVpsRepFormatIdx(layerIdx))->getPicWidthVpsInLumaSamples()  <= activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(layerIdx))->getPicWidthVpsInLumaSamples());
642    assert( activeVPS->getVpsRepFormat( sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : activeVPS->getVpsRepFormatIdx(layerIdx))->getPicHeightVpsInLumaSamples() <= activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(layerIdx))->getPicHeightVpsInLumaSamples());
643    assert( activeVPS->getVpsRepFormat( sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : activeVPS->getVpsRepFormatIdx(layerIdx))->getChromaFormatVpsIdc()        <= activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(layerIdx))->getChromaFormatVpsIdc());
644    assert( activeVPS->getVpsRepFormat( sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : activeVPS->getVpsRepFormatIdx(layerIdx))->getBitDepthVpsLuma()           <= activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(layerIdx))->getBitDepthVpsLuma());
645    assert( activeVPS->getVpsRepFormat( sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : activeVPS->getVpsRepFormatIdx(layerIdx))->getBitDepthVpsChroma()         <= activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(layerIdx))->getBitDepthVpsChroma());
646  }
647#endif //SVC_EXTENSION
648
649  m_apcSlicePilot->setPPS(pps);
650  m_apcSlicePilot->setSPS(sps);
651  pps->setSPS(sps);
652  pps->setNumSubstreams(pps->getEntropyCodingSyncEnabledFlag() ? ((sps->getPicHeightInLumaSamples() + sps->getMaxCUHeight() - 1) / sps->getMaxCUHeight()) * (pps->getNumTileColumnsMinus1() + 1) : ((pps->getNumTileRowsMinus1() + 1)*(pps->getNumTileColumnsMinus1() + 1)));
653  pps->setMinCuDQPSize( sps->getMaxCUWidth() >> ( pps->getMaxCuDQPDepth()) );
654  pps->setMinCuChromaQpAdjSize( sps->getMaxCUWidth() >> ( pps->getMaxCuChromaQpAdjDepth()) );
655
656  for (UInt channel = 0; channel < MAX_NUM_CHANNEL_TYPE; channel++)
657  {
658#if REPN_FORMAT_IN_VPS
659    g_bitDepth[channel] = isLuma(ChannelType(channel)) ? m_apcSlicePilot->getBitDepthY() : m_apcSlicePilot->getBitDepthC();
660#else
661    g_bitDepth[channel] = sps->getBitDepth(ChannelType(channel));
662#endif
663
664    if (sps->getUseExtendedPrecision()) g_maxTrDynamicRange[channel] = std::max<Int>(15, (g_bitDepth[channel] + 6));
665    else                                g_maxTrDynamicRange[channel] = 15;
666  }
667  g_uiMaxCUWidth  = sps->getMaxCUWidth();
668  g_uiMaxCUHeight = sps->getMaxCUHeight();
669  g_uiMaxCUDepth  = sps->getMaxCUDepth();
670  g_uiAddCUDepth  = max (0, sps->getLog2MinCodingBlockSize() - (Int)sps->getQuadtreeTULog2MinSize() + (Int)getMaxCUDepthOffset(sps->getChromaFormatIdc(), sps->getQuadtreeTULog2MinSize()));
671
672  for (Int i = 0; i < sps->getLog2DiffMaxMinCodingBlockSize(); i++)
673  {
674    sps->setAMPAcc( i, sps->getUseAMP() );
675  }
676
677  for (Int i = sps->getLog2DiffMaxMinCodingBlockSize(); i < sps->getMaxCUDepth(); i++)
678  {
679    sps->setAMPAcc( i, 0 );
680  }
681
682  m_cSAO.destroy();
683#if REPN_FORMAT_IN_VPS
684  m_cSAO.create( m_apcSlicePilot->getPicWidthInLumaSamples(), m_apcSlicePilot->getPicHeightInLumaSamples(), sps->getChromaFormatIdc(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), pps->getSaoOffsetBitShift(CHANNEL_TYPE_LUMA), pps->getSaoOffsetBitShift(CHANNEL_TYPE_CHROMA) );
685#else
686  m_cSAO.create( sps->getPicWidthInLumaSamples(), sps->getPicHeightInLumaSamples(), sps->getChromaFormatIdc(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), pps->getSaoOffsetBitShift(CHANNEL_TYPE_LUMA), pps->getSaoOffsetBitShift(CHANNEL_TYPE_CHROMA) );
687#endif
688  m_cLoopFilter.create( sps->getMaxCUDepth() );
689}
690
691#if SVC_EXTENSION
692Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC )
693#else
694Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay )
695#endif
696{
697#if SVC_EXTENSION
698  m_apcSlicePilot->setVPS( m_parameterSetManagerDecoder.getPrefetchedVPS(0) );
699
700  // Following check should go wherever the VPS is activated
701  if( !m_apcSlicePilot->getVPS()->getBaseLayerAvailableFlag() )
702  {
703    assert(nalu.m_layerId != 0);
704    assert(m_apcSlicePilot->getVPS()->getNumAddLayerSets() > 0);
705
706    if( m_commonDecoderParams->getTargetOutputLayerSetIdx() >= 0 )
707    {
708      UInt layerIdx = m_apcSlicePilot->getVPS()->getOutputLayerSetIdx(getCommonDecoderParams()->getTargetOutputLayerSetIdx());
709      assert(layerIdx > m_apcSlicePilot->getVPS()->getVpsNumLayerSetsMinus1());
710    }
711  } 
712
713  m_apcSlicePilot->initSlice( nalu.m_layerId );
714#else //SVC_EXTENSION
715  m_apcSlicePilot->initSlice();
716#endif
717
718  if (m_bFirstSliceInPicture)
719  {
720    m_uiSliceIdx = 0;
721  }
722  else
723  {
724    m_apcSlicePilot->copySliceInfo( m_pcPic->getPicSym()->getSlice(m_uiSliceIdx-1) );
725  }
726  m_apcSlicePilot->setSliceIdx(m_uiSliceIdx);
727
728  m_apcSlicePilot->setNalUnitType(nalu.m_nalUnitType);
729#if POC_RESET_RESTRICTIONS
730  m_apcSlicePilot->setTLayer( nalu.m_temporalId );
731#endif
732  Bool nonReferenceFlag = (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TRAIL_N ||
733                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N   ||
734                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N  ||
735                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N  ||
736                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N);
737  m_apcSlicePilot->setTemporalLayerNonReferenceFlag(nonReferenceFlag);
738  m_apcSlicePilot->setReferenced(true); // Putting this as true ensures that picture is referenced the first time it is in an RPS
739  m_apcSlicePilot->setTLayerInfo(nalu.m_temporalId);
740
741#if ENC_DEC_TRACE
742  const UInt64 originalSymbolCount = g_nSymbolCounter;
743#endif
744
745#if SVC_EXTENSION
746  setRefLayerParams(m_apcSlicePilot->getVPS());
747  m_apcSlicePilot->setNumMotionPredRefLayers(m_numMotionPredRefLayers);
748#endif
749  m_cEntropyDecoder.decodeSliceHeader (m_apcSlicePilot, &m_parameterSetManagerDecoder);
750
751  // set POC for dependent slices in skipped pictures
752  if(m_apcSlicePilot->getDependentSliceSegmentFlag() && m_prevSliceSkipped)
753  {
754    m_apcSlicePilot->setPOC(m_skippedPOC);
755  }
756
757  m_apcSlicePilot->setAssociatedIRAPPOC(m_pocCRA);
758  m_apcSlicePilot->setAssociatedIRAPType(m_associatedIRAPType);
759
760  //For inference of NoOutputOfPriorPicsFlag
761  if (m_apcSlicePilot->getRapPicFlag())
762  {
763    if ((m_apcSlicePilot->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && m_apcSlicePilot->getNalUnitType() <= NAL_UNIT_CODED_SLICE_IDR_N_LP) || 
764        (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA && m_bFirstSliceInSequence) ||
765        (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA && m_apcSlicePilot->getHandleCraAsBlaFlag()))
766    {
767      m_apcSlicePilot->setNoRaslOutputFlag(true);
768    }
769    //the inference for NoOutputPriorPicsFlag
770    if (!m_bFirstSliceInBitstream && m_apcSlicePilot->getRapPicFlag() && m_apcSlicePilot->getNoRaslOutputFlag())
771    {
772      if (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA)
773      {
774        m_apcSlicePilot->setNoOutputPriorPicsFlag(true);
775      }
776    }
777    else
778    {
779      m_apcSlicePilot->setNoOutputPriorPicsFlag(false);
780    }
781
782    if(m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA)
783    {
784      m_craNoRaslOutputFlag = m_apcSlicePilot->getNoRaslOutputFlag();
785    }
786  }
787  if (m_apcSlicePilot->getRapPicFlag() && m_apcSlicePilot->getNoOutputPriorPicsFlag())
788  {
789    m_lastPOCNoOutputPriorPics = m_apcSlicePilot->getPOC();
790    m_isNoOutputPriorPics = true;
791  }
792  else
793  {
794    m_isNoOutputPriorPics = false;
795  }
796
797  //For inference of PicOutputFlag
798  if (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R)
799  {
800    if ( m_craNoRaslOutputFlag )
801    {
802      m_apcSlicePilot->setPicOutputFlag(false);
803    }
804  }
805
806  if (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA && m_craNoRaslOutputFlag) //Reset POC MSB when CRA has NoRaslOutputFlag equal to 1
807  {
808    Int iMaxPOClsb = 1 << m_apcSlicePilot->getSPS()->getBitsForPOC();
809    m_apcSlicePilot->setPOC( m_apcSlicePilot->getPOC() & (iMaxPOClsb - 1) );
810  }
811
812  // Skip pictures due to random access
813  if (isRandomAccessSkipPicture(iSkipFrame, iPOCLastDisplay))
814  {
815    m_prevSliceSkipped = true;
816    m_skippedPOC = m_apcSlicePilot->getPOC();
817    return false;
818  }
819  // Skip TFD pictures associated with BLA/BLANT pictures
820  if (isSkipPictureForBLA(iPOCLastDisplay))
821  {
822    m_prevSliceSkipped = true;
823    m_skippedPOC = m_apcSlicePilot->getPOC();
824    return false;
825  }
826
827  // clear previous slice skipped flag
828  m_prevSliceSkipped = false;
829
830  // exit when a new picture is found
831#if SVC_EXTENSION
832  bNewPOC = m_apcSlicePilot->getPOC() != m_prevPOC || ( m_apcSlicePilot->getFirstSliceInPic() && m_parseIdc == -1 );
833
834#if NO_CLRAS_OUTPUT_FLAG
835  if( m_layerId == m_smallestLayerId && m_apcSlicePilot->getRapPicFlag() )
836  {
837    if( m_bFirstSliceInSequence )
838    {
839      setNoClrasOutputFlag(true);
840    }
841    else if( m_lastPicHasEos )
842    {
843      setNoClrasOutputFlag(true);
844    }
845    else if( m_apcSlicePilot->getBlaPicFlag() )
846    {
847      setNoClrasOutputFlag(true);
848    }
849    else if( m_apcSlicePilot->getIdrPicFlag() && m_apcSlicePilot->getCrossLayerBLAFlag() )
850    {
851      setNoClrasOutputFlag(true);
852    }
853    else
854    {
855      setNoClrasOutputFlag(false);
856    }     
857  }
858  else
859  {
860    setNoClrasOutputFlag(false);
861  }
862
863  m_apcSlicePilot->decodingRefreshMarking( m_cListPic, m_noClrasOutputFlag, m_smallestLayerId );
864#endif
865
866  // Derive the value of NoOutputOfPriorPicsFlag
867  if( bNewPOC || m_layerId!=m_uiPrevLayerId )   // i.e. new coded picture
868  {
869    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA && m_apcSlicePilot->getNoRaslOutputFlag() )
870    {
871      this->setNoOutputPriorPicsFlag( true );
872    }
873    else if( m_apcSlicePilot->getRapPicFlag() && m_apcSlicePilot->getNoRaslOutputFlag() )
874    {
875      this->setNoOutputPriorPicsFlag( m_apcSlicePilot->getNoOutputPriorPicsFlag() );
876    }
877    else
878    {
879      if( this->m_ppcTDecTop[0]->getNoClrasOutputFlag() )
880      {
881        this->setNoOutputPriorPicsFlag( true );
882      }
883    }
884  }
885
886#if POC_RESET_IDC_DECODER
887  if( m_parseIdc != -1 ) // Second pass for a POC resetting picture
888  {
889    m_parseIdc++; // Proceed to POC decoding and RPS derivation
890  }
891 
892  if( m_parseIdc == 2 )
893  {
894    bNewPOC = false;
895  }
896
897  if( (bNewPOC || m_layerId!=m_uiPrevLayerId) && (m_parseIdc == -1) ) // Will be true at the first pass
898  {
899  //if (bNewPOC || m_layerId!=m_uiPrevLayerId)
900  // Check if new reset period has started - this is needed just so that the SHM decoder which calls slice header decoding twice
901  // does not invoke the output twice
902  //if( m_lastPocPeriodId[m_apcSlicePilot->getLayerId()] == m_apcSlicePilot->getPocResetPeriodId() )
903    // Update CurrAU marking
904    if(( m_layerId < m_uiPrevLayerId) ||( ( m_layerId == m_uiPrevLayerId) && bNewPOC)) // Decoding a lower layer than or same layer as previous - mark all earlier pictures as not in current AU
905    {
906#if POC_RESET_RESTRICTIONS
907      // New access unit; reset all variables related to POC reset restrictions
908      resetPocRestrictionCheckParameters();
909#endif
910      markAllPicsAsNoCurrAu(m_apcSlicePilot->getVPS());
911#if P0297_VPS_POC_LSB_ALIGNED_FLAG
912      for (UInt i = 0; i < MAX_LAYERS; i++)
913      {
914        m_ppcTDecTop[m_apcSlicePilot->getVPS()->getLayerIdInNuh(i)]->m_pocDecrementedInDPBFlag = false;
915      }
916#endif
917    }
918
919
920#if P0297_VPS_POC_LSB_ALIGNED_FLAG
921    m_pocResettingFlag = false;
922
923    if (m_apcSlicePilot->getPocResetIdc() != 0)
924    {
925      if (m_apcSlicePilot->getVPS()->getVpsPocLsbAlignedFlag())
926      {
927        m_pocResettingFlag = true;
928      }
929      else if (m_pocDecrementedInDPBFlag)
930      {
931        m_pocResettingFlag = false;
932      }
933      else
934      {
935        m_pocResettingFlag = true;
936      }
937    }
938#endif
939
940    if( m_apcSlicePilot->getPocResetIdc() && m_apcSlicePilot->getSliceIdx() == 0 )
941    {
942      Int pocResetPeriodId = m_apcSlicePilot->getPocResetPeriodId();
943      if ( m_apcSlicePilot->getPocResetIdc() == 1 || m_apcSlicePilot->getPocResetIdc() == 2 ||
944        ( m_apcSlicePilot->getPocResetIdc() == 3 && pocResetPeriodId != getLastPocPeriodId() ) )
945      {
946        setLastPocPeriodId(pocResetPeriodId);
947        m_parseIdc = 0;
948      }
949#if POC_RESET_VALUE_RESTRICTION
950      // Check if the POC Reset period ID matches with the Reset Period ID
951      if( pocResetPeriodId == m_crossLayerPocResetPeriodId )
952      {
953        // If matching, and current poc_reset_idc = 3, then the values should match
954        if( m_apcSlicePilot->getPocResetIdc() == 3 )
955        {
956          assert( ( m_apcSlicePilot->getFullPocResetFlag() == false && m_crossLayerPocResetIdc == 1 ) ||
957                  ( m_apcSlicePilot->getFullPocResetFlag() == true  && m_crossLayerPocResetIdc == 2 ) );
958        }
959      }
960      else
961      {
962        // This is the first picture of a POC resetting access unit
963        m_crossLayerPocResetPeriodId = pocResetPeriodId;
964        if( m_apcSlicePilot->getPocResetIdc() == 1 || m_apcSlicePilot->getPocResetIdc() == 2 )
965        {
966          m_crossLayerPocResetIdc = m_apcSlicePilot->getPocResetIdc();
967        }
968        else
969        { // poc_reset_idc = 3
970          // In this case, the entire POC resetting access unit has been lost.
971          // Need more checking to ensure poc_reset_idc = 3 works.
972          assert ( 0 );
973        }
974      }
975#endif
976    }
977    else
978    {
979      m_parseIdc = 3; // Proceed to decoding POC and RPS
980    }
981  }
982#endif
983
984#if ALIGNED_BUMPING
985#if POC_RESET_IDC_DECODER
986
987#if P0297_VPS_POC_LSB_ALIGNED_FLAG
988  UInt affectedLayerList[MAX_LAYERS];
989  Int  numAffectedLayers;
990
991  affectedLayerList[0] = m_apcSlicePilot->getLayerId();
992  numAffectedLayers = 1;
993
994  if (m_apcSlicePilot->getVPS()->getVpsPocLsbAlignedFlag())
995  {
996    for (UInt j = 0; j < m_apcSlicePilot->getVPS()->getNumPredictedLayers(m_apcSlicePilot->getLayerId()); j++)
997    {
998      affectedLayerList[j + 1] = m_apcSlicePilot->getVPS()->getPredictedLayerId(m_apcSlicePilot->getLayerId(), j);
999    }
1000    numAffectedLayers = m_apcSlicePilot->getVPS()->getNumPredictedLayers(m_apcSlicePilot->getLayerId()) + 1;
1001  }
1002#endif
1003
1004  //if(  (bNewPOC || m_layerId != m_uiPrevLayerId) && ( m_parseIdc != 1) )
1005#if P0297_VPS_POC_LSB_ALIGNED_FLAG
1006  if (m_parseIdc == 1 && m_pocResettingFlag)
1007#else
1008  if (m_parseIdc == 1)
1009#endif
1010  {
1011    // Invoke output of pictures if the current picture is a POC reset picture
1012    bNewPOC = true;
1013    /* Include reset of all POCs in the layer */
1014
1015  // This operation would do the following:
1016  // 1. Update the other picture in the DPB. This should be done only for the first slice of the picture.
1017  // 2. Update the value of m_pocCRA.
1018  // 3. Reset the POC values at the decoder for the current picture to be zero - will be done later
1019  // 4. update value of POCLastDisplay
1020     
1021  //Do the reset stuff here
1022    Int maxPocLsb = 1 << m_apcSlicePilot->getSPS()->getBitsForPOC();
1023    Int pocLsbVal;
1024    if( m_apcSlicePilot->getPocResetIdc() == 3 )
1025    {
1026      pocLsbVal = m_apcSlicePilot->getPocLsbVal() ;
1027    }
1028    else
1029    {
1030      pocLsbVal = (m_apcSlicePilot->getPOC() % maxPocLsb);
1031    }
1032
1033    Int pocMsbDelta = 0;
1034    if ( m_apcSlicePilot->getPocMsbValPresentFlag() ) 
1035    {
1036      pocMsbDelta = m_apcSlicePilot->getPocMsbVal() * maxPocLsb;
1037    }
1038    else
1039    {
1040      //This MSB derivation can be made into one function. Item to do next.
1041      Int prevPoc     = this->getPrevPicOrderCnt();
1042      Int prevPocLsb  = prevPoc & (maxPocLsb - 1);
1043      Int prevPocMsb  = prevPoc - prevPocLsb;
1044
1045      pocMsbDelta = m_apcSlicePilot->getCurrMsb( pocLsbVal, prevPocLsb, prevPocMsb, maxPocLsb );
1046    }
1047
1048    Int pocLsbDelta;
1049    if( m_apcSlicePilot->getPocResetIdc() == 2 ||  ( m_apcSlicePilot->getPocResetIdc() == 3 && m_apcSlicePilot->getFullPocResetFlag() ))
1050    {
1051      pocLsbDelta = pocLsbVal;
1052    }
1053    else
1054    {
1055      pocLsbDelta = 0; 
1056    }
1057
1058    Int deltaPocVal  =  pocMsbDelta + pocLsbDelta;
1059
1060#if P0297_VPS_POC_LSB_ALIGNED_FLAG
1061    for (UInt layerIdx = 0; layerIdx < numAffectedLayers; layerIdx++)
1062    {
1063      if (!m_ppcTDecTop[affectedLayerList[layerIdx]]->m_pocDecrementedInDPBFlag)
1064      {
1065        m_ppcTDecTop[affectedLayerList[layerIdx]]->m_pocDecrementedInDPBFlag = true;
1066        TComList<TComPic*>::iterator  iterPic = m_ppcTDecTop[affectedLayerList[layerIdx]]->getListPic()->begin();
1067        while (iterPic != m_ppcTDecTop[affectedLayerList[layerIdx]]->getListPic()->end())
1068#else
1069    //Reset all POC for DPB -> basically do it for each slice in the picutre
1070    TComList<TComPic*>::iterator  iterPic = m_cListPic.begin(); 
1071
1072    // Iterate through all picture in DPB
1073    while( iterPic != m_cListPic.end() )
1074#endif
1075    {
1076      TComPic *dpbPic = *iterPic;
1077      // Check if the picture pointed to by iterPic is either used for reference or
1078      // needed for output, are in the same layer, and not the current picture.
1079#if P0297_VPS_POC_LSB_ALIGNED_FLAG
1080      assert(dpbPic->getLayerId() == affectedLayerList[layerIdx]);
1081      if ( (dpbPic->getReconMark()) && (dpbPic->getPicSym()->getSlice(0)->getPicOutputFlag()) )
1082#else
1083      if ( /*  ( ( dpbPic->getSlice(0)->isReferenced() ) || ( dpbPic->getOutputMark() ) )
1084          &&*/ ( dpbPic->getLayerId() == m_apcSlicePilot->getLayerId() )
1085            && ( dpbPic->getReconMark() ) && ( dpbPic->getPicSym()->getSlice(0)->getPicOutputFlag() ))
1086#endif
1087      {
1088        for(Int i = dpbPic->getNumAllocatedSlice()-1; i >= 0; i--)
1089        {
1090
1091          TComSlice *slice = dpbPic->getSlice(i);
1092          TComReferencePictureSet *rps = slice->getRPS();
1093          slice->setPOC( slice->getPOC() - deltaPocVal );
1094
1095          // Also adjust the POC value stored in the RPS of each such slice
1096          for(Int j = rps->getNumberOfPictures(); j >= 0; j--)
1097          {
1098            rps->setPOC( j, rps->getPOC(j) - deltaPocVal );
1099          }
1100          // Also adjust the value of refPOC
1101          for(Int k = 0; k < 2; k++)  // For List 0 and List 1
1102          {
1103            RefPicList list = (k == 1) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1104            for(Int j = 0; j < slice->getNumRefIdx(list); j++)
1105            {
1106              slice->setRefPOC( slice->getRefPOC(list, j) - deltaPocVal, list, j);
1107            }
1108          }
1109        }
1110      }
1111      iterPic++;
1112    }
1113#if P0297_VPS_POC_LSB_ALIGNED_FLAG
1114        // Update the value of pocCRA
1115        m_ppcTDecTop[affectedLayerList[layerIdx]]->m_pocCRA -= deltaPocVal;
1116      }
1117    }
1118#else
1119    // Update the value of pocCRA
1120    m_pocCRA -= deltaPocVal;
1121#endif
1122
1123    // Update value of POCLastDisplay
1124    iPOCLastDisplay -= deltaPocVal;
1125  }
1126  Int maxPocLsb = 1 << m_apcSlicePilot->getSPS()->getBitsForPOC();
1127  Int slicePicOrderCntLsb = m_apcSlicePilot->getPicOrderCntLsb();
1128
1129#if P0297_VPS_POC_LSB_ALIGNED_FLAG
1130  if (m_pocResettingFlag && (m_parseIdc == 1 || m_parseIdc == 2))
1131#else
1132  if (m_parseIdc == 1 || m_parseIdc == 2) // TODO This should be replaced by pocResettingFlag.
1133#endif
1134  {
1135    // Set poc for current slice
1136    if( m_apcSlicePilot->getPocResetIdc() == 1 )
1137    {       
1138      m_apcSlicePilot->setPOC( slicePicOrderCntLsb );
1139    }
1140    else if( m_apcSlicePilot->getPocResetIdc() == 2 )
1141    {
1142      m_apcSlicePilot->setPOC( 0 );
1143    }
1144    else 
1145    {
1146      Int picOrderCntMsb = m_apcSlicePilot->getCurrMsb( slicePicOrderCntLsb, m_apcSlicePilot->getFullPocResetFlag() ? 0 : m_apcSlicePilot->getPocLsbVal(), 0 , maxPocLsb );
1147      m_apcSlicePilot->setPOC( picOrderCntMsb + slicePicOrderCntLsb );
1148    }
1149  }
1150  else if (m_parseIdc == 3)
1151  {
1152    Int picOrderCntMsb = 0;
1153    if( m_apcSlicePilot->getPocMsbValPresentFlag() )
1154    {
1155      picOrderCntMsb = m_apcSlicePilot->getPocMsbVal() * maxPocLsb;
1156    }
1157    else if( m_apcSlicePilot->getIdrPicFlag() )
1158    {
1159      picOrderCntMsb = 0;
1160    }
1161    else
1162    {
1163      Int prevPicOrderCntLsb = this->getPrevPicOrderCnt() & ( maxPocLsb - 1);
1164      Int prevPicOrderCntMsb  = this->getPrevPicOrderCnt() - prevPicOrderCntLsb;
1165      picOrderCntMsb = m_apcSlicePilot->getCurrMsb(slicePicOrderCntLsb, prevPicOrderCntLsb, prevPicOrderCntMsb, maxPocLsb );
1166    }
1167    m_apcSlicePilot->setPOC( picOrderCntMsb + slicePicOrderCntLsb );
1168  }
1169
1170  if( m_parseIdc == 1 || m_parseIdc == 3)
1171  {
1172    // Adjust prevPicOrderCnt
1173    if(    !m_apcSlicePilot->getRaslPicFlag() 
1174        && !m_apcSlicePilot->getRadlPicFlag()
1175        && (m_apcSlicePilot->getNalUnitType() % 2 == 1)
1176        && ( nalu.m_temporalId == 0 )
1177        && !m_apcSlicePilot->getDiscardableFlag() )
1178    {
1179#if P0297_VPS_POC_LSB_ALIGNED_FLAG
1180      for (UInt i = 0; i < numAffectedLayers; i++)
1181      {
1182        m_ppcTDecTop[affectedLayerList[i]]->setPrevPicOrderCnt(m_apcSlicePilot->getPOC());
1183      }
1184#else
1185      this->setPrevPicOrderCnt( m_apcSlicePilot->getPOC() );
1186#endif
1187    }
1188    else if ( m_apcSlicePilot->getPocResetIdc() == 3 )
1189    {
1190#if P0297_VPS_POC_LSB_ALIGNED_FLAG
1191      if (!m_firstPicInLayerDecodedFlag || (m_firstPicInLayerDecodedFlag && m_pocResettingFlag))
1192      {
1193        for (UInt i = 0; i < numAffectedLayers; i++)
1194        {
1195          m_ppcTDecTop[affectedLayerList[i]]->setPrevPicOrderCnt( m_apcSlicePilot->getFullPocResetFlag() 
1196                                                                  ? 0 : m_apcSlicePilot->getPocLsbVal() );
1197        }
1198      }
1199#else
1200      this->setPrevPicOrderCnt( m_apcSlicePilot->getFullPocResetFlag() 
1201                                            ? 0 : m_apcSlicePilot->getPocLsbVal() );
1202#endif
1203    }
1204#else
1205  if (bNewPOC || m_layerId!=m_uiPrevLayerId)
1206  {
1207#endif
1208    m_apcSlicePilot->applyReferencePictureSet(m_cListPic, m_apcSlicePilot->getRPS());
1209  }
1210#endif
1211#if POC_RESET_IDC_DECODER
1212  if (!m_apcSlicePilot->getDependentSliceSegmentFlag() && (bNewPOC || m_layerId!=m_uiPrevLayerId || m_parseIdc == 1) && !m_bFirstSliceInSequence )
1213#else
1214  if (!m_apcSlicePilot->getDependentSliceSegmentFlag() && (bNewPOC || m_layerId!=m_uiPrevLayerId) && !m_bFirstSliceInSequence )
1215#endif
1216  {
1217    m_prevPOC = m_apcSlicePilot->getPOC();
1218    curLayerId = m_uiPrevLayerId; 
1219    m_uiPrevLayerId = m_layerId;
1220    return true;
1221  }
1222
1223#if SVC_EXTENSION
1224#if POC_RESET_IDC_DECODER
1225  m_parseIdc = -1;
1226#endif
1227
1228  if( m_apcSlicePilot->getTLayer() == 0 && m_apcSlicePilot->getEnableTMVPFlag() == 0 )
1229  {
1230    //update all pics in the DPB such that they cannot be used for TMPV ref
1231    TComList<TComPic*>::iterator  iterRefPic = m_cListPic.begin();
1232
1233    while( iterRefPic != m_cListPic.end() )
1234    {
1235      TComPic *refPic = *iterRefPic;
1236      if( ( refPic->getLayerId() == m_apcSlicePilot->getLayerId() ) && refPic->getReconMark() )
1237      {
1238        for( Int i = refPic->getNumAllocatedSlice()-1; i >= 0; i-- )
1239        {
1240
1241          TComSlice *refSlice = refPic->getSlice(i);
1242          refSlice->setAvailableForTMVPRefFlag( false );
1243        }
1244      }
1245      iterRefPic++;
1246    }
1247  }
1248  m_apcSlicePilot->setAvailableForTMVPRefFlag( true );
1249#endif
1250
1251  // actual decoding starts here
1252  xActivateParameterSets();
1253
1254#if REPN_FORMAT_IN_VPS
1255  // Initialize ILRP if needed, only for the current layer 
1256  // ILRP intialization should go along with activation of parameters sets,
1257  // although activation of parameter sets itself need not be done for each and every slice!!!
1258  xInitILRP(m_apcSlicePilot);
1259#endif
1260  if (!m_apcSlicePilot->getDependentSliceSegmentFlag()) 
1261  {
1262    m_prevPOC = m_apcSlicePilot->getPOC();
1263    curLayerId = m_layerId;
1264    m_uiPrevLayerId = m_layerId;
1265  }
1266  m_bFirstSliceInSequence = false;
1267  m_bFirstSliceInBitstream  = false;
1268
1269  // Alignment of TSA and STSA pictures across AU
1270#if !Q0108_TSA_STSA
1271  if( m_apcSlicePilot->getLayerId() > 0 )
1272  {
1273    // Check for TSA alignment
1274    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
1275        m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R
1276         )
1277    {
1278      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1279      {
1280        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1281        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() );
1282        if( refpicLayer )
1283        {
1284          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
1285                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R );    // TSA pictures should be aligned among depenedent layers
1286        } 
1287      }
1288    }
1289    // Check for STSA alignment
1290    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
1291         m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R
1292         )
1293    {
1294      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1295      {
1296        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1297        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() ); // STSA pictures should be aligned among dependent layers
1298        if( refpicLayer )
1299
1300        {
1301          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
1302                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R );
1303        }
1304      }
1305    }
1306  }
1307#endif
1308
1309#else //SVC_EXTENSION
1310  //we should only get a different poc for a new picture (with CTU address==0)
1311  if (!m_apcSlicePilot->getDependentSliceSegmentFlag() && m_apcSlicePilot->getPOC()!=m_prevPOC && !m_bFirstSliceInSequence && (m_apcSlicePilot->getSliceCurStartCtuTsAddr() != 0)) 
1312  {
1313    printf ("Warning, the first slice of a picture might have been lost!\n");
1314  }
1315
1316  // exit when a new picture is found
1317  if (!m_apcSlicePilot->getDependentSliceSegmentFlag() && (m_apcSlicePilot->getSliceCurStartCtuTsAddr() == 0 && !m_bFirstSliceInPicture) )
1318  {
1319    if (m_prevPOC >= m_pocRandomAccess)
1320    {
1321      m_prevPOC = m_apcSlicePilot->getPOC();
1322#if ENC_DEC_TRACE
1323      //rewind the trace counter since we didn't actually decode the slice
1324      g_nSymbolCounter = originalSymbolCount;
1325#endif
1326      return true;
1327    }
1328    m_prevPOC = m_apcSlicePilot->getPOC();
1329  }
1330
1331  // actual decoding starts here
1332  xActivateParameterSets();
1333
1334  if (!m_apcSlicePilot->getDependentSliceSegmentFlag())
1335  {
1336    m_prevPOC = m_apcSlicePilot->getPOC();
1337  }
1338  m_bFirstSliceInSequence = false;
1339  m_bFirstSliceInBitstream  = false;
1340#endif //SVC_EXTENSION
1341  //detect lost reference picture and insert copy of earlier frame.
1342  Int lostPoc;
1343  while((lostPoc=m_apcSlicePilot->checkThatAllRefPicsAreAvailable(m_cListPic, m_apcSlicePilot->getRPS(), true, m_pocRandomAccess)) > 0)
1344  {
1345    xCreateLostPicture(lostPoc-1);
1346  }
1347  if (m_bFirstSliceInPicture)
1348  {
1349#if SVC_EXTENSION
1350#if AVC_BASE
1351    if( m_layerId > 0 && m_parameterSetManagerDecoder.getPrefetchedVPS(0)->getNonHEVCBaseLayerFlag() )
1352    {
1353      TComPic* pBLPic = (*m_ppcTDecTop[0]->getListPic()->begin());
1354      pBLPic->getSlice(0)->setReferenced(true);
1355      fstream* pFile  = m_ppcTDecTop[0]->getBLReconFile();
1356
1357      if( pFile->good() )
1358      {
1359        Bool is16bit  = g_bitDepthLayer[CHANNEL_TYPE_LUMA][0] > 8 || g_bitDepthLayer[CHANNEL_TYPE_CHROMA][0] > 8;
1360        UInt uiWidth  = pBLPic->getPicYuvRec()->getWidth(COMPONENT_Y);
1361        UInt uiHeight = pBLPic->getPicYuvRec()->getHeight(COMPONENT_Y);
1362
1363        Int len = uiWidth * (is16bit ? 2 : 1);
1364        UChar *buf = new UChar[len];
1365
1366        UInt64 uiPos = (UInt64) m_apcSlicePilot->getPOC() * uiWidth * uiHeight * 3 / 2;
1367        if( is16bit )
1368        {
1369            uiPos <<= 1;
1370        }
1371
1372        pFile->seekg((UInt)uiPos, ios::beg );
1373
1374        // read Y component
1375        Pel* pPel = pBLPic->getPicYuvRec()->getAddr(COMPONENT_Y);
1376        UInt uiStride = pBLPic->getPicYuvRec()->getStride(COMPONENT_Y);
1377        for( Int i = 0; i < uiHeight; i++ )
1378        {
1379          pFile->read(reinterpret_cast<Char*>(buf), len);
1380
1381          if( !is16bit )
1382          {
1383            for (Int x = 0; x < uiWidth; x++)
1384            {
1385              pPel[x] = buf[x];
1386            }
1387          }
1388          else
1389          {
1390            for (Int x = 0; x < uiWidth; x++)
1391            {
1392              pPel[x] = (buf[2*x+1] << 8) | buf[2*x];
1393            }
1394          }
1395     
1396          pPel += uiStride;
1397        }
1398
1399        len >>= 1;
1400        uiWidth >>= 1;
1401        uiHeight >>= 1;
1402
1403        // read Cb component
1404        pPel = pBLPic->getPicYuvRec()->getAddr(COMPONENT_Cb);
1405        uiStride = pBLPic->getPicYuvRec()->getStride(COMPONENT_Cb);
1406        for( Int i = 0; i < uiHeight; i++ )
1407        {
1408          pFile->read(reinterpret_cast<Char*>(buf), len);
1409
1410          if( !is16bit )
1411          {
1412            for( Int x = 0; x < uiWidth; x++ )
1413            {
1414              pPel[x] = buf[x];
1415            }
1416          }
1417          else
1418          {
1419            for( Int x = 0; x < uiWidth; x++ )
1420            {
1421              pPel[x] = (buf[2*x+1] << 8) | buf[2*x];
1422            }
1423          }
1424     
1425          pPel += uiStride;
1426        }
1427
1428        // read Cr component
1429        pPel = pBLPic->getPicYuvRec()->getAddr(COMPONENT_Cr);
1430        uiStride = pBLPic->getPicYuvRec()->getStride(COMPONENT_Cr);
1431        for( Int i = 0; i < uiHeight; i++ )
1432        {
1433          pFile->read(reinterpret_cast<Char*>(buf), len);
1434
1435          if( !is16bit )
1436          {
1437            for( Int x = 0; x < uiWidth; x++ )
1438            {
1439              pPel[x] = buf[x];
1440            }
1441          }
1442          else
1443          {
1444            for( Int x = 0; x < uiWidth; x++ )
1445            {
1446              pPel[x] = (buf[2*x+1] << 8) | buf[2*x];
1447            }
1448          }
1449     
1450          pPel += uiStride;
1451        }
1452
1453        delete[] buf;
1454      }
1455    }
1456#endif
1457
1458    if ( m_layerId == 0 && m_apcSlicePilot->getRapPicFlag() && getNoClrasOutputFlag() )
1459    {
1460      for (UInt i = 0; i < m_apcSlicePilot->getVPS()->getMaxLayers(); i++)
1461      {
1462        m_ppcTDecTop[i]->setLayerInitializedFlag(false);
1463        m_ppcTDecTop[i]->setFirstPicInLayerDecodedFlag(false);
1464      }
1465    }
1466
1467    xCheckLayerReset();
1468    xSetLayerInitializedFlag();
1469#endif
1470
1471    // Buffer initialize for prediction.
1472    m_cPrediction.initTempBuff(m_apcSlicePilot->getSPS()->getChromaFormatIdc());
1473#if ALIGNED_BUMPING
1474    m_apcSlicePilot->checkLeadingPictureRestrictions(m_cListPic);
1475#else
1476    m_apcSlicePilot->applyReferencePictureSet(m_cListPic, m_apcSlicePilot->getRPS());
1477#endif
1478    //  Get a new picture buffer
1479    xGetNewPicBuffer (m_apcSlicePilot, m_pcPic);
1480
1481#if POC_RESET_IDC_DECODER
1482    m_pcPic->setCurrAuFlag( true );
1483#if POC_RESET_RESTRICTIONS
1484    if( m_pcPic->getLayerId() > 0 && m_apcSlicePilot->isIDR() && !m_nonBaseIdrPresentFlag )
1485    {
1486      // IDR picture with nuh_layer_id > 0 present
1487      m_nonBaseIdrPresentFlag = true;
1488      m_nonBaseIdrType = (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL);
1489    }
1490    else
1491    {
1492      if( m_apcSlicePilot->getNalUnitType() != NAL_UNIT_CODED_SLICE_IDR_W_RADL )
1493      {
1494        // Picture with nal_unit_type not equal IDR_W_RADL present
1495        m_picNonIdrWithRadlPresentFlag = true;
1496      }
1497      if( m_apcSlicePilot->getNalUnitType() != NAL_UNIT_CODED_SLICE_IDR_N_LP )
1498      {
1499        // Picture with nal_unit_type not equal IDR_N_LP present
1500        m_picNonIdrNoLpPresentFlag = true;
1501      }
1502    }
1503    if( !m_checkPocRestrictionsForCurrAu )  // Will be true for the first slice/picture of the AU
1504    {
1505      m_checkPocRestrictionsForCurrAu = true;
1506      m_pocResetIdcOrCurrAu = m_apcSlicePilot->getPocResetIdc();
1507      if( m_pcPic->getLayerId() == 0 )
1508      {
1509        // Base layer picture is present
1510        m_baseLayerPicPresentFlag = true;
1511        if( m_apcSlicePilot->isIRAP() )
1512        {
1513          // Base layer picture is IRAP
1514          m_baseLayerIrapFlag = true;
1515        }
1516        if( m_apcSlicePilot->isIDR() )
1517        {
1518          // Base layer picture is IDR
1519          m_baseLayerIdrFlag = true;
1520        }
1521        else
1522        {
1523          if( m_apcSlicePilot->getVPS()->getBaseLayerInternalFlag())
1524          {
1525            /* When the picture with nuh_layer_id equal to 0 in an access unit is not an IDR picture
1526            and vps_base_layer_internal_flag is equal to 1, the value of poc_reset_idc shall not be equal to 2
1527            for any picture in the access unit. */
1528            assert( m_apcSlicePilot->getPocResetIdc() != 2 );
1529          }
1530        }
1531      }
1532    }
1533    else
1534    {
1535      // The value of poc_reset_idc of all coded pictures that are present in the bitstream in an access unit shall be the same.
1536      assert( m_pocResetIdcOrCurrAu == m_apcSlicePilot->getPocResetIdc() );
1537
1538      /* When the picture in an access unit with nuh_layer_id equal to 0 is an IRAP picture and vps_base_layer_internal_flag is equal to 1
1539      and there is at least one other picture in the same access unit that is not an IRAP picture,
1540      the value of poc_reset_idc shall be equal to 1 or 2 for all pictures in the access unit. */
1541      if( m_baseLayerPicPresentFlag && m_baseLayerIrapFlag && !m_apcSlicePilot->isIRAP() && m_apcSlicePilot->getVPS()->getBaseLayerInternalFlag() )
1542      {
1543        assert( m_apcSlicePilot->getPocResetIdc() == 1 || m_apcSlicePilot->getPocResetIdc() == 2 );
1544      }
1545
1546      /* When the picture with nuh_layer_id equal to 0 in an access unit is an IDR picture and
1547      vps_base_layer_internal_flag is equal to 1 and there is at least one non-IDR picture in the same access unit,
1548      the value of poc_reset_idc shall be equal to 2 for all pictures in the access unit. */
1549      if( m_baseLayerPicPresentFlag && m_baseLayerIdrFlag && !m_apcSlicePilot->isIDR() && m_apcSlicePilot->getVPS()->getBaseLayerInternalFlag() )
1550      {
1551        assert( m_apcSlicePilot->getPocResetIdc() == 2 );
1552      }
1553
1554      /* When there is at least one picture that has nuh_layer_id greater than 0 and that is an IDR picture
1555      with a particular value of nal_unit_type in an access unit and there is at least one other coded picture
1556      that is present in the bitstream in the same access unit with a different value of nal_unit_type,
1557      the value of poc_reset_idc shall be equal to 1 or 2 for all pictures in the access unit. */
1558      if( m_nonBaseIdrPresentFlag && (
1559            ( m_nonBaseIdrType == 1 && m_picNonIdrWithRadlPresentFlag ) ||
1560            ( m_nonBaseIdrType == 0 && m_picNonIdrNoLpPresentFlag )
1561        ))
1562      {
1563        assert( m_apcSlicePilot->getPocResetIdc() == 1 || m_apcSlicePilot->getPocResetIdc() == 2 );
1564      }
1565    }
1566#endif
1567#endif
1568
1569    Bool isField = false;
1570    Bool isTff = false;
1571   
1572    if(!m_SEIs.empty())
1573    {
1574      // Check if any new Picture Timing SEI has arrived
1575      SEIMessages pictureTimingSEIs = extractSeisByType (m_SEIs, SEI::PICTURE_TIMING);
1576      if (pictureTimingSEIs.size()>0)
1577      {
1578        SEIPictureTiming* pictureTiming = (SEIPictureTiming*) *(pictureTimingSEIs.begin());
1579        isField = (pictureTiming->m_picStruct == 1) || (pictureTiming->m_picStruct == 2);
1580        isTff =  (pictureTiming->m_picStruct == 1);
1581      }
1582
1583      // Check if any new temporal motion vector prediction constraints SEI has arrived
1584      SEIMessages seiTMVPConstrainsList = extractSeisByType (m_SEIs, SEI::TMVP_CONSTRAINTS);
1585      if (seiTMVPConstrainsList.size() > 0)
1586      {
1587        assert ( m_pcPic->getTLayer() == 0 );  //this SEI can present only for AU with Tid equal to 0
1588        SEITMVPConstrains* tmvpConstraintSEI = (SEITMVPConstrains*) *(seiTMVPConstrainsList.begin());
1589        if ( tmvpConstraintSEI->prev_pics_not_used_flag == 1 )
1590        {
1591          //update all pics in the DPB such that they cannot be used for TMPV ref
1592          TComList<TComPic*>::iterator  iterRefPic = m_cListPic.begin(); 
1593          while( iterRefPic != m_cListPic.end() )
1594          {
1595            TComPic *refPic = *iterRefPic;
1596            if( ( refPic->getLayerId() == m_pcPic->getLayerId() ) && refPic->getReconMark() )
1597            {
1598              for(Int i = refPic->getNumAllocatedSlice()-1; i >= 0; i--)
1599              {
1600                TComSlice *refSlice = refPic->getSlice(i);
1601                refSlice->setAvailableForTMVPRefFlag( false );
1602              }
1603            }
1604            iterRefPic++;
1605          }
1606        }
1607      }
1608    }
1609   
1610    //Set Field/Frame coding mode
1611    m_pcPic->setField(isField);
1612    m_pcPic->setTopField(isTff);
1613
1614    // transfer any SEI messages that have been received to the picture
1615    m_pcPic->setSEIs(m_SEIs);
1616    m_SEIs.clear();
1617
1618    // Recursive structure
1619    m_cCuDecoder.create ( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight, m_apcSlicePilot->getSPS()->getChromaFormatIdc() );
1620#if SVC_EXTENSION
1621    m_cCuDecoder.init   ( m_ppcTDecTop,&m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction, curLayerId );
1622#else
1623    m_cCuDecoder.init   ( &m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction );
1624#endif
1625    m_cTrQuant.init     ( g_uiMaxCUWidth, g_uiMaxCUHeight, m_apcSlicePilot->getSPS()->getMaxTrSize());
1626
1627    m_cSliceDecoder.create();
1628  }
1629  else
1630  {
1631    // Check if any new SEI has arrived
1632    if(!m_SEIs.empty())
1633    {
1634      // Currently only decoding Unit SEI message occurring between VCL NALUs copied
1635      SEIMessages &picSEI = m_pcPic->getSEIs();
1636      SEIMessages decodingUnitInfos = extractSeisByType (m_SEIs, SEI::DECODING_UNIT_INFO);
1637      picSEI.insert(picSEI.end(), decodingUnitInfos.begin(), decodingUnitInfos.end());
1638      deleteSEIs(m_SEIs);
1639    }
1640  }
1641
1642  //  Set picture slice pointer
1643  TComSlice*  pcSlice = m_apcSlicePilot;
1644
1645  m_pcPic->getPicSym()->initTiles(pcSlice->getPPS());
1646  m_pcPic->getPicSym()->initCtuTsRsAddrMaps();
1647
1648  // When decoding the slice header, the stored start and end addresses were actually RS addresses, not TS addresses.
1649  // Now, having set up the maps, convert them to the correct form.
1650  pcSlice->setSliceSegmentCurStartCtuTsAddr( m_pcPic->getPicSym()->getCtuRsToTsAddrMap(pcSlice->getSliceSegmentCurStartCtuTsAddr()) );
1651  pcSlice->setSliceSegmentCurEndCtuTsAddr( m_pcPic->getPicSym()->getCtuRsToTsAddrMap(pcSlice->getSliceSegmentCurEndCtuTsAddr()) );
1652  if(!pcSlice->getDependentSliceSegmentFlag())
1653  {
1654    pcSlice->setSliceCurStartCtuTsAddr(m_pcPic->getPicSym()->getCtuRsToTsAddrMap(pcSlice->getSliceCurStartCtuTsAddr()));
1655    pcSlice->setSliceCurEndCtuTsAddr(m_pcPic->getPicSym()->getCtuRsToTsAddrMap(pcSlice->getSliceCurEndCtuTsAddr()));
1656  }
1657
1658  if (m_bFirstSliceInPicture)
1659  {
1660    if(m_pcPic->getNumAllocatedSlice() != 1)
1661    {
1662      m_pcPic->clearSliceBuffer();
1663    }
1664  }
1665  else
1666  {
1667    m_pcPic->allocateNewSlice();
1668  }
1669  assert(m_pcPic->getNumAllocatedSlice() == (m_uiSliceIdx + 1));
1670  m_apcSlicePilot = m_pcPic->getPicSym()->getSlice(m_uiSliceIdx);
1671  m_pcPic->getPicSym()->setSlice(pcSlice, m_uiSliceIdx);
1672
1673  m_pcPic->setTLayer(nalu.m_temporalId);
1674
1675#if SVC_EXTENSION
1676  m_pcPic->setLayerId(nalu.m_layerId);
1677  pcSlice->setLayerId(nalu.m_layerId);
1678  pcSlice->setPic(m_pcPic);
1679#endif
1680
1681  if (!pcSlice->getDependentSliceSegmentFlag())
1682  {
1683    pcSlice->checkCRA(pcSlice->getRPS(), m_pocCRA, m_associatedIRAPType, m_cListPic );
1684    // Set reference list
1685#if SVC_EXTENSION
1686    if (m_layerId == 0)
1687#endif
1688    pcSlice->setRefPicList( m_cListPic, true );
1689
1690#if SVC_EXTENSION
1691    // Create upsampling reference layer pictures for all possible dependent layers and do it only once for the first slice.
1692    // Other slices might choose which reference pictures to be used for inter-layer prediction
1693    if( m_layerId > 0 && m_uiSliceIdx == 0 && ( !pcSlice->getVPS()->getSingleLayerForNonIrapFlag() || pcSlice->isIRAP() ) )
1694    {
1695      for( Int i = 0; i < pcSlice->getNumILRRefIdx(); i++ )
1696      {
1697        UInt refLayerIdc = i;
1698        UInt refLayerId = pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc);
1699#if AVC_BASE
1700        if( refLayerId == 0 && m_parameterSetManagerDecoder.getActiveVPS()->getNonHEVCBaseLayerFlag() )
1701        {         
1702          TComPic* pic = *m_ppcTDecTop[0]->getListPic()->begin();
1703
1704          if( pic )
1705          {
1706            pcSlice->setBaseColPic ( refLayerIdc, pic );
1707          }
1708          else
1709          {
1710            continue;
1711          }
1712        }
1713        else
1714        {
1715          TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1716          TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1717          if( !pcSlice->setBaseColPic ( *cListPic, refLayerIdc ) )
1718          {
1719            continue;
1720          }
1721        }
1722#else
1723        TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1724        TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1725        pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1726#endif
1727
1728        const Window &scalEL = pcSlice->getPPS()->getScaledRefLayerWindowForLayer(refLayerId);
1729        const Window &windowRL = pcSlice->getPPS()->getRefLayerWindowForLayer(pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc));
1730        Int widthBL   = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getWidth(COMPONENT_Y) - windowRL.getWindowLeftOffset() - windowRL.getWindowRightOffset();
1731        Int heightBL  = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getHeight(COMPONENT_Y) - windowRL.getWindowTopOffset() - windowRL.getWindowBottomOffset();
1732        Int widthEL   = m_pcPic->getPicYuvRec()->getWidth(COMPONENT_Y)  - scalEL.getWindowLeftOffset() - scalEL.getWindowRightOffset();
1733        Int heightEL  = m_pcPic->getPicYuvRec()->getHeight(COMPONENT_Y) - scalEL.getWindowTopOffset()  - scalEL.getWindowBottomOffset();
1734
1735        // conformance check: the values of RefLayerRegionWidthInSamplesY, RefLayerRegionHeightInSamplesY, ScaledRefRegionWidthInSamplesY and ScaledRefRegionHeightInSamplesY shall be greater than 0
1736        assert(widthEL > 0 && heightEL > 0 && widthBL > 0 && widthEL > 0);
1737
1738        // conformance check: ScaledRefRegionWidthInSamplesY shall be greater or equal to RefLayerRegionWidthInSamplesY and ScaledRefRegionHeightInSamplesY shall be greater or equal to RefLayerRegionHeightInSamplesY
1739        assert(widthEL >= widthBL && heightEL >= heightBL);
1740
1741        // conformance check: when ScaledRefRegionWidthInSamplesY is equal to RefLayerRegionWidthInSamplesY, PhaseHorY shall be equal to 0, when ScaledRefRegionWidthInSamplesC is equal to RefLayerRegionWidthInSamplesC, PhaseHorC shall be equal to 0, when ScaledRefRegionHeightInSamplesY is equal to RefLayerRegionHeightInSamplesY, PhaseVerY shall be equal to 0, and when ScaledRefRegionHeightInSamplesC is equal to RefLayerRegionHeightInSamplesC, PhaseVerC shall be equal to 0.
1742        Bool phaseSetPresentFlag;
1743        Int phaseHorLuma, phaseVerLuma, phaseHorChroma, phaseVerChroma;
1744        pcSlice->getPPS()->getResamplingPhase( refLayerId, phaseSetPresentFlag, phaseHorLuma, phaseVerLuma, phaseHorChroma, phaseVerChroma );
1745
1746        assert( ( (widthEL  != widthBL)  || (phaseHorLuma == 0 && phaseHorChroma == 0) )
1747             && ( (heightEL != heightBL) || (phaseVerLuma == 0 && phaseVerChroma == 0) ) );
1748
1749        g_mvScalingFactor[refLayerIdc][0] = widthEL  == widthBL  ? 4096 : Clip3(-4096, 4095, ((widthEL  << 8) + (widthBL  >> 1)) / widthBL);
1750        g_mvScalingFactor[refLayerIdc][1] = heightEL == heightBL ? 4096 : Clip3(-4096, 4095, ((heightEL << 8) + (heightBL >> 1)) / heightBL);
1751
1752        g_posScalingFactor[refLayerIdc][0] = ((widthBL  << 16) + (widthEL  >> 1)) / widthEL;
1753        g_posScalingFactor[refLayerIdc][1] = ((heightBL << 16) + (heightEL >> 1)) / heightEL;
1754
1755#if Q0048_CGS_3D_ASYMLUT
1756        TComPicYuv* pBaseColRec = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec();
1757        if( pcSlice->getPPS()->getCGSFlag() 
1758#if R0150_CGS_SIGNAL_CONSTRAINTS
1759          && m_c3DAsymLUTPPS.isRefLayer( pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc) )
1760#endif
1761          )
1762        {
1763#if R0150_CGS_SIGNAL_CONSTRAINTS
1764          assert( pcSlice->getBaseColPic( refLayerIdc )->getSlice( 0 )->getBitDepthY() == m_c3DAsymLUTPPS.getInputBitDepthY() );
1765          assert( pcSlice->getBaseColPic( refLayerIdc )->getSlice( 0 )->getBitDepthC() == m_c3DAsymLUTPPS.getInputBitDepthC() );
1766          assert( pcSlice->getBitDepthY() >= m_c3DAsymLUTPPS.getOutputBitDepthY() );
1767          assert( pcSlice->getBitDepthY() >= m_c3DAsymLUTPPS.getOutputBitDepthC() );
1768#endif
1769          if(!m_pColorMappedPic)
1770          {
1771            initAsymLut(pcSlice->getBaseColPic(refLayerIdc)->getSlice(0));
1772          }
1773          m_c3DAsymLUTPPS.colorMapping( pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(),  m_pColorMappedPic );
1774          pBaseColRec = m_pColorMappedPic;
1775        }
1776#endif
1777        if( m_pcPic->isSpatialEnhLayer(refLayerIdc) )
1778        {
1779          // check for the sample prediction picture type
1780          if( pcSlice->getVPS()->isSamplePredictionType( pcSlice->getVPS()->getLayerIdxInVps(m_layerId), pcSlice->getVPS()->getLayerIdxInVps(refLayerId) ) )
1781          {
1782            m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, m_pcPic->getFullPelBaseRec(refLayerIdc), pBaseColRec, m_pcPic->getPicYuvRec());
1783          }
1784        }
1785        else
1786        {
1787          m_pcPic->setFullPelBaseRec( refLayerIdc, pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec() );
1788        }
1789        pcSlice->setFullPelBaseRec ( refLayerIdc, m_pcPic->getFullPelBaseRec(refLayerIdc) );
1790      }
1791    }
1792
1793    if( m_layerId > 0 && pcSlice->getActiveNumILRRefIdx() )
1794    {
1795      for( Int i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
1796      {
1797        UInt refLayerIdc = pcSlice->getInterLayerPredLayerIdc(i);
1798#if AVC_BASE
1799        if( pcSlice->getVPS()->getRefLayerId( m_layerId, refLayerIdc ) == 0 && m_parameterSetManagerDecoder.getActiveVPS()->getNonHEVCBaseLayerFlag() )
1800        {
1801          pcSlice->setBaseColPic ( refLayerIdc, *m_ppcTDecTop[0]->getListPic()->begin() );
1802        }
1803        else
1804        {
1805          TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1806          TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1807          pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1808        }
1809#else
1810        TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1811        TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1812        pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1813#endif
1814
1815        pcSlice->setFullPelBaseRec ( refLayerIdc, m_pcPic->getFullPelBaseRec(refLayerIdc) );
1816      }
1817
1818      pcSlice->setILRPic( m_cIlpPic );
1819
1820#if REF_IDX_MFM
1821      pcSlice->setRefPicList( m_cListPic, false, m_cIlpPic);
1822    }
1823    else if ( m_layerId > 0 )
1824    {
1825      pcSlice->setRefPicList( m_cListPic, false, NULL);
1826    }
1827#if MFM_ENCCONSTRAINT
1828    if( pcSlice->getMFMEnabledFlag() )
1829    {
1830      TComPic* refPic = pcSlice->getRefPic( pcSlice->getSliceType() == B_SLICE ? ( RefPicList )( 1 - pcSlice->getColFromL0Flag() ) : REF_PIC_LIST_0 , pcSlice->getColRefIdx() );
1831
1832      assert( refPic );
1833
1834      Int refLayerId = refPic->getLayerId();
1835
1836      if( refLayerId != pcSlice->getLayerId() )
1837      {
1838        TComPic* pColBasePic = pcSlice->getBaseColPic( *m_ppcTDecTop[refLayerId]->getListPic() );
1839        assert( pColBasePic->checkSameRefInfo() == true );
1840      }
1841    }
1842#endif
1843#endif
1844   
1845    if( m_layerId > 0 && pcSlice->getVPS()->getCrossLayerIrapAlignFlag() && ( !pcSlice->getVPS()->getSingleLayerForNonIrapFlag() || pcSlice->isIRAP() ) )
1846    {
1847      for(Int dependentLayerIdx = 0; dependentLayerIdx < pcSlice->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1848      {
1849        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1850        TComPic* refpicLayer = pcSlice->getRefPic(*cListPic, pcSlice->getPOC() );
1851        if(refpicLayer && pcSlice->isIRAP())
1852        {                 
1853          assert(pcSlice->getNalUnitType() == refpicLayer->getSlice(0)->getNalUnitType());
1854        }
1855      }
1856    }
1857   
1858    if( m_layerId > 0 && !pcSlice->isIntra() && pcSlice->getEnableTMVPFlag() )
1859    {
1860      TComPic* refPic = pcSlice->getRefPic(RefPicList(1 - pcSlice->getColFromL0Flag()), pcSlice->getColRefIdx());
1861
1862      assert( refPic );
1863      assert ( refPic->getPicSym()->getSlice(0)->getAvailableForTMVPRefFlag() == true );
1864
1865      // It is a requirement of bitstream conformance when the collocated picture, used for temporal motion vector prediction, is an inter-layer reference picture,
1866      // VpsInterLayerMotionPredictionEnabled[ LayerIdxInVps[ currLayerId ] ][ LayerIdxInVps[ rLId ] ] shall be equal to 1, where rLId is set equal to nuh_layer_id of the inter-layer picture.
1867      if( refPic->isILR(pcSlice->getLayerId()) )
1868      {
1869        assert( pcSlice->getVPS()->isMotionPredictionType( pcSlice->getVPS()->getLayerIdxInVps(m_layerId), refPic->getLayerIdx() ) );
1870      }
1871    }
1872#endif //SVC_EXTENSION
1873   
1874    // For generalized B
1875    // note: maybe not existed case (always L0 is copied to L1 if L1 is empty)
1876    if (pcSlice->isInterB() && pcSlice->getNumRefIdx(REF_PIC_LIST_1) == 0)
1877    {
1878      Int iNumRefIdx = pcSlice->getNumRefIdx(REF_PIC_LIST_0);
1879      pcSlice->setNumRefIdx        ( REF_PIC_LIST_1, iNumRefIdx );
1880
1881      for (Int iRefIdx = 0; iRefIdx < iNumRefIdx; iRefIdx++)
1882      {
1883        pcSlice->setRefPic(pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx), REF_PIC_LIST_1, iRefIdx);
1884      }
1885    }
1886    if (!pcSlice->isIntra())
1887    {
1888      Bool bLowDelay = true;
1889      Int  iCurrPOC  = pcSlice->getPOC();
1890      Int iRefIdx = 0;
1891
1892      for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
1893      {
1894        if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
1895        {
1896          bLowDelay = false;
1897        }
1898      }
1899      if (pcSlice->isInterB())
1900      {
1901        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
1902        {
1903          if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
1904          {
1905            bLowDelay = false;
1906          }
1907        }
1908      }
1909
1910      pcSlice->setCheckLDC(bLowDelay);
1911    }
1912
1913    //---------------
1914    pcSlice->setRefPOCList();
1915  }
1916
1917  m_pcPic->setCurrSliceIdx(m_uiSliceIdx);
1918  if(pcSlice->getSPS()->getScalingListFlag())
1919  {
1920    pcSlice->setScalingList ( pcSlice->getSPS()->getScalingList()  );
1921
1922#if SCALINGLIST_INFERRING
1923    if( pcSlice->getPPS()->getScalingListPresentFlag() || pcSlice->getPPS()->getInferScalingListFlag() )
1924#else
1925    if(pcSlice->getPPS()->getScalingListPresentFlag())
1926#endif
1927    {
1928      pcSlice->setScalingList ( pcSlice->getPPS()->getScalingList()  );
1929    }
1930#if SCALINGLIST_INFERRING
1931    if( m_layerId == 0 || ( m_layerId > 0 && !pcSlice->getPPS()->getInferScalingListFlag() && !pcSlice->getSPS()->getInferScalingListFlag() ) )
1932#endif
1933    if(!pcSlice->getPPS()->getScalingListPresentFlag() && !pcSlice->getSPS()->getScalingListPresentFlag())
1934    {
1935      pcSlice->setDefaultScalingList();
1936    }
1937    m_cTrQuant.setScalingListDec(pcSlice->getScalingList(), pcSlice->getSPS()->getChromaFormatIdc());
1938    m_cTrQuant.setUseScalingList(true);
1939  }
1940  else
1941  {
1942    m_cTrQuant.setFlatScalingList(pcSlice->getSPS()->getChromaFormatIdc());
1943    m_cTrQuant.setUseScalingList(false);
1944  }
1945
1946  //  Decode a picture
1947  m_cGopDecoder.decompressSlice(nalu.m_Bitstream, m_pcPic);
1948
1949#if P0297_VPS_POC_LSB_ALIGNED_FLAG
1950  setFirstPicInLayerDecodedFlag(true);
1951#endif
1952#if SVC_EXTENSION
1953  m_lastPicHasEos = false;
1954#endif
1955
1956  m_bFirstSliceInPicture = false;
1957  m_uiSliceIdx++;
1958
1959  return false;
1960}
1961
1962Void TDecTop::xDecodeVPS()
1963{
1964  TComVPS* vps = new TComVPS();
1965
1966  m_cEntropyDecoder.decodeVPS( vps );
1967  m_parameterSetManagerDecoder.storePrefetchedVPS(vps);
1968#if SVC_EXTENSION
1969  checkValueOfTargetOutputLayerSetIdx(vps);
1970#endif
1971}
1972
1973Void TDecTop::xDecodeSPS()
1974{
1975  TComSPS* sps = new TComSPS();
1976#if O0043_BEST_EFFORT_DECODING
1977  sps->setForceDecodeBitDepth(m_forceDecodeBitDepth);
1978#endif
1979#if SVC_EXTENSION
1980  sps->setLayerId(m_layerId);
1981#endif
1982  m_cEntropyDecoder.decodeSPS( sps );
1983  m_parameterSetManagerDecoder.storePrefetchedSPS(sps);
1984}
1985
1986#if Q0048_CGS_3D_ASYMLUT
1987Void TDecTop::xDecodePPS( TCom3DAsymLUT * pc3DAsymLUT )
1988#else
1989Void TDecTop::xDecodePPS()
1990#endif
1991{
1992  TComPPS* pps = new TComPPS();
1993
1994#if SCALINGLIST_INFERRING
1995  pps->setLayerId( m_layerId );
1996#endif
1997#if Q0048_CGS_3D_ASYMLUT
1998  m_cEntropyDecoder.decodePPS( pps, pc3DAsymLUT, m_layerId );
1999#else
2000  m_cEntropyDecoder.decodePPS( pps );
2001#endif
2002  m_parameterSetManagerDecoder.storePrefetchedPPS( pps );
2003}
2004
2005Void TDecTop::xDecodeSEI( TComInputBitstream* bs, const NalUnitType nalUnitType )
2006{
2007  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
2008  {
2009#if SVC_EXTENSION
2010    if (m_prevSliceSkipped) // No need to decode SEI messages of a skipped access unit
2011    {
2012      return;
2013    }
2014#endif
2015#if LAYERS_NOT_PRESENT_SEI
2016    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS(), m_pDecodedSEIOutputStream  );
2017#else
2018    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS(), m_pDecodedSEIOutputStream );
2019#endif
2020  }
2021  else
2022  {
2023#if LAYERS_NOT_PRESENT_SEI
2024    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS(), m_pDecodedSEIOutputStream  );
2025#else
2026    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS(), m_pDecodedSEIOutputStream );
2027#endif
2028    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
2029    if (activeParamSets.size()>0)
2030    {
2031      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
2032#if R0247_SEI_ACTIVE
2033      getLayerDec(0)->m_parameterSetManagerDecoder.applyPrefetchedPS();
2034      assert(seiAps->activeSeqParameterSetId.size()>0);
2035      if( !getLayerDec(0)->m_parameterSetManagerDecoder.activateSPSWithSEI( seiAps->activeSeqParameterSetId[0] ) )
2036      {
2037        printf ("Warning SPS activation with Active parameter set SEI failed");
2038      }
2039      for (Int c=1 ; c <= seiAps->numSpsIdsMinus1; c++)
2040      {
2041        Int layerIdx = seiAps->layerSpsIdx[c];
2042        getLayerDec(layerIdx)->m_parameterSetManagerDecoder.applyPrefetchedPS();
2043        if( !getLayerDec(layerIdx)->m_parameterSetManagerDecoder.activateSPSWithSEI( seiAps->activeSeqParameterSetId[layerIdx] ) )
2044        {
2045          printf ("Warning SPS activation with Active parameter set SEI failed");
2046        }
2047      }
2048#else
2049      m_parameterSetManagerDecoder.applyPrefetchedPS();
2050      assert(seiAps->activeSeqParameterSetId.size()>0);
2051      if (! m_parameterSetManagerDecoder.activateSPSWithSEI(seiAps->activeSeqParameterSetId[0] ))
2052      {
2053        printf ("Warning SPS activation with Active parameter set SEI failed");
2054      }
2055#endif
2056    }
2057  }
2058}
2059
2060#if SVC_EXTENSION
2061Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC)
2062#else
2063Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay)
2064#endif
2065{
2066  // Initialize entropy decoder
2067  m_cEntropyDecoder.setEntropyDecoder (&m_cCavlcDecoder);
2068  m_cEntropyDecoder.setBitstream      (nalu.m_Bitstream);
2069
2070#if SVC_EXTENSION
2071  // ignore any NAL units with nuh_layer_id == 63
2072  if( nalu.m_layerId == 63 )
2073  { 
2074    return false;
2075  }
2076#endif
2077  switch (nalu.m_nalUnitType)
2078  {
2079    case NAL_UNIT_VPS:
2080#if SVC_EXTENSION
2081      assert( nalu.m_layerId == 0 ); // Non-conforming bitstream. The value of nuh_layer_id of VPS NAL unit shall be equal to 0.
2082#endif
2083      xDecodeVPS();
2084#if RExt__DECODER_DEBUG_BIT_STATISTICS
2085      TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS,nalu.m_Bitstream->readByteAlignment(),0);
2086#endif
2087#if SVC_EXTENSION
2088#if Q0177_EOS_CHECKS
2089      m_isLastNALWasEos = false;
2090#endif
2091#if AVC_BASE
2092      if( m_parameterSetManagerDecoder.getPrefetchedVPS(0)->getNonHEVCBaseLayerFlag() )
2093      {
2094        if( !m_ppcTDecTop[0]->getBLReconFile()->good() )
2095        {
2096          printf( "Base layer YUV input reading error\n" );
2097          exit(EXIT_FAILURE);
2098        }       
2099      }
2100      else
2101      {
2102        TComList<TComPic*> *cListPic = m_ppcTDecTop[0]->getListPic();
2103        cListPic->clear();
2104      }
2105#endif
2106      xDeriveSmallestLayerId(m_parameterSetManagerDecoder.getPrefetchedVPS(0));
2107#endif
2108      return false;
2109
2110    case NAL_UNIT_SPS:
2111      xDecodeSPS();
2112#if RExt__DECODER_DEBUG_BIT_STATISTICS
2113      TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS,nalu.m_Bitstream->readByteAlignment(),0);
2114#endif
2115      return false;
2116
2117    case NAL_UNIT_PPS:
2118      xDecodePPS(
2119#if Q0048_CGS_3D_ASYMLUT
2120        &m_c3DAsymLUTPPS
2121#endif
2122        );
2123#if RExt__DECODER_DEBUG_BIT_STATISTICS
2124      TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS,nalu.m_Bitstream->readByteAlignment(),0);
2125#endif
2126      return false;
2127
2128    case NAL_UNIT_PREFIX_SEI:
2129    case NAL_UNIT_SUFFIX_SEI:
2130#if Q0177_EOS_CHECKS
2131      if ( nalu.m_nalUnitType == NAL_UNIT_SUFFIX_SEI )
2132      {
2133        assert( m_isLastNALWasEos == false );
2134      }
2135#endif
2136      xDecodeSEI( nalu.m_Bitstream, nalu.m_nalUnitType );
2137      return false;
2138
2139    case NAL_UNIT_CODED_SLICE_TRAIL_R:
2140    case NAL_UNIT_CODED_SLICE_TRAIL_N:
2141    case NAL_UNIT_CODED_SLICE_TSA_R:
2142    case NAL_UNIT_CODED_SLICE_TSA_N:
2143    case NAL_UNIT_CODED_SLICE_STSA_R:
2144    case NAL_UNIT_CODED_SLICE_STSA_N:
2145    case NAL_UNIT_CODED_SLICE_BLA_W_LP:
2146    case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
2147    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
2148    case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
2149    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
2150    case NAL_UNIT_CODED_SLICE_CRA:
2151    case NAL_UNIT_CODED_SLICE_RADL_N:
2152    case NAL_UNIT_CODED_SLICE_RADL_R:
2153    case NAL_UNIT_CODED_SLICE_RASL_N:
2154    case NAL_UNIT_CODED_SLICE_RASL_R:
2155#if Q0177_EOS_CHECKS
2156      if (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_TRAIL_R || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_TRAIL_N ||
2157          nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_TSA_R || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_TSA_N ||
2158          nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_STSA_R || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_STSA_N ||
2159          nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
2160          nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N )
2161      {
2162        assert( m_isLastNALWasEos == false );
2163      }
2164      else
2165      {
2166        m_isLastNALWasEos = false;
2167      }
2168#endif
2169#if SVC_EXTENSION
2170      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay, curLayerId, bNewPOC);
2171#else
2172      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay);
2173#endif
2174      break;
2175
2176    case NAL_UNIT_EOS:
2177#if Q0177_EOS_CHECKS
2178      assert( m_isLastNALWasEos == false );
2179
2180      m_isLastNALWasEos = true;
2181      m_lastPicHasEos = true;
2182#endif
2183      m_associatedIRAPType = NAL_UNIT_INVALID;
2184      m_pocCRA = 0;
2185      m_pocRandomAccess = MAX_INT;
2186      m_prevPOC = MAX_INT;
2187      m_prevSliceSkipped = false;
2188      m_skippedPOC = 0;
2189      return false;
2190
2191    case NAL_UNIT_ACCESS_UNIT_DELIMITER:
2192      // TODO: process AU delimiter
2193      return false;
2194
2195    case NAL_UNIT_EOB:
2196#if P0130_EOB
2197      //Check layer id of the nalu. if it is not 0, give a warning message.
2198      if (nalu.m_layerId > 0)
2199      {
2200        printf( "\n\nThis bitstream is ended with EOB NALU that has layer id greater than 0\n" );
2201      }
2202#endif
2203      return false;
2204
2205    case NAL_UNIT_FILLER_DATA:
2206#if Q0177_EOS_CHECKS
2207      assert( m_isLastNALWasEos == false );
2208#endif
2209      return false;
2210
2211    case NAL_UNIT_RESERVED_VCL_N10:
2212    case NAL_UNIT_RESERVED_VCL_R11:
2213    case NAL_UNIT_RESERVED_VCL_N12:
2214    case NAL_UNIT_RESERVED_VCL_R13:
2215    case NAL_UNIT_RESERVED_VCL_N14:
2216    case NAL_UNIT_RESERVED_VCL_R15:
2217
2218    case NAL_UNIT_RESERVED_IRAP_VCL22:
2219    case NAL_UNIT_RESERVED_IRAP_VCL23:
2220
2221    case NAL_UNIT_RESERVED_VCL24:
2222    case NAL_UNIT_RESERVED_VCL25:
2223    case NAL_UNIT_RESERVED_VCL26:
2224    case NAL_UNIT_RESERVED_VCL27:
2225    case NAL_UNIT_RESERVED_VCL28:
2226    case NAL_UNIT_RESERVED_VCL29:
2227    case NAL_UNIT_RESERVED_VCL30:
2228    case NAL_UNIT_RESERVED_VCL31:
2229
2230    case NAL_UNIT_RESERVED_NVCL41:
2231    case NAL_UNIT_RESERVED_NVCL42:
2232    case NAL_UNIT_RESERVED_NVCL43:
2233    case NAL_UNIT_RESERVED_NVCL44:
2234    case NAL_UNIT_RESERVED_NVCL45:
2235    case NAL_UNIT_RESERVED_NVCL46:
2236    case NAL_UNIT_RESERVED_NVCL47:
2237    case NAL_UNIT_UNSPECIFIED_48:
2238    case NAL_UNIT_UNSPECIFIED_49:
2239    case NAL_UNIT_UNSPECIFIED_50:
2240    case NAL_UNIT_UNSPECIFIED_51:
2241    case NAL_UNIT_UNSPECIFIED_52:
2242    case NAL_UNIT_UNSPECIFIED_53:
2243    case NAL_UNIT_UNSPECIFIED_54:
2244    case NAL_UNIT_UNSPECIFIED_55:
2245    case NAL_UNIT_UNSPECIFIED_56:
2246    case NAL_UNIT_UNSPECIFIED_57:
2247    case NAL_UNIT_UNSPECIFIED_58:
2248    case NAL_UNIT_UNSPECIFIED_59:
2249    case NAL_UNIT_UNSPECIFIED_60:
2250    case NAL_UNIT_UNSPECIFIED_61:
2251    case NAL_UNIT_UNSPECIFIED_62:
2252    case NAL_UNIT_UNSPECIFIED_63:
2253
2254    default:
2255      assert (0);
2256      break;
2257  }
2258
2259  return false;
2260}
2261
2262/** Function for checking if picture should be skipped because of association with a previous BLA picture
2263 * \param iPOCLastDisplay POC of last picture displayed
2264 * \returns true if the picture should be skipped
2265 * This function skips all TFD pictures that follow a BLA picture
2266 * in decoding order and precede it in output order.
2267 */
2268Bool TDecTop::isSkipPictureForBLA(Int& iPOCLastDisplay)
2269{
2270  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) &&
2271       m_apcSlicePilot->getPOC() < m_pocCRA && (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N))
2272  {
2273    iPOCLastDisplay++;
2274    return true;
2275  }
2276  return false;
2277}
2278
2279/** Function for checking if picture should be skipped because of random access
2280 * \param iSkipFrame skip frame counter
2281 * \param iPOCLastDisplay POC of last picture displayed
2282 * \returns true if the picture shold be skipped in the random access.
2283 * This function checks the skipping of pictures in the case of -s option random access.
2284 * All pictures prior to the random access point indicated by the counter iSkipFrame are skipped.
2285 * It also checks the type of Nal unit type at the random access point.
2286 * 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.
2287 * If the random access point is IDR all pictures after the random access point are decoded.
2288 * If the random access point is none of the above, a warning is issues, and decoding of pictures with POC
2289 * equal to or greater than the random access point POC is attempted. For non IDR/CRA/BLA random
2290 * access point there is no guarantee that the decoder will not crash.
2291 */
2292Bool TDecTop::isRandomAccessSkipPicture(Int& iSkipFrame,  Int& iPOCLastDisplay)
2293{
2294  if (iSkipFrame)
2295  {
2296    iSkipFrame--;   // decrement the counter
2297    return true;
2298  }
2299  else if (m_pocRandomAccess == MAX_INT) // start of random access point, m_pocRandomAccess has not been set yet.
2300  {
2301    if (   m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA
2302        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
2303        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
2304        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL )
2305    {
2306      // set the POC random access since we need to skip the reordered pictures in the case of CRA/CRANT/BLA/BLANT.
2307      m_pocRandomAccess = m_apcSlicePilot->getPOC();
2308    }
2309    else if ( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
2310    {
2311      m_pocRandomAccess = -MAX_INT; // no need to skip the reordered pictures in IDR, they are decodable.
2312    }
2313    else
2314    {
2315      static Bool warningMessage = false;
2316      if(!warningMessage)
2317      {
2318        printf("\nWarning: this is not a valid random access point and the data is discarded until the first CRA picture");
2319        warningMessage = true;
2320      }
2321      return true;
2322    }
2323  }
2324  // skip the reordered pictures, if necessary
2325  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))
2326  {
2327    iPOCLastDisplay++;
2328    return true;
2329  }
2330  // if we reach here, then the picture is not skipped.
2331  return false;
2332}
2333
2334#if SVC_EXTENSION
2335#if !REPN_FORMAT_IN_VPS
2336Void TDecTop::xInitILRP(TComSPS *pcSPS)
2337#else
2338Void TDecTop::xInitILRP(TComSlice *slice)
2339#endif
2340{
2341#if REPN_FORMAT_IN_VPS
2342  TComSPS* pcSPS = slice->getSPS();
2343  Int bitDepthY   = slice->getBitDepthY();
2344  Int bitDepthC   = slice->getBitDepthC();
2345  Int picWidth    = slice->getPicWidthInLumaSamples();
2346  Int picHeight   = slice->getPicHeightInLumaSamples();
2347#endif
2348  if(m_layerId>0)
2349  {
2350#if REPN_FORMAT_IN_VPS
2351    g_bitDepth[CHANNEL_TYPE_LUMA]     = bitDepthY;
2352    g_bitDepth[CHANNEL_TYPE_CHROMA]   = bitDepthC;
2353#else
2354    g_bitDepth[CHANNEL_TYPE_LUMA]     = pcSPS->getBitDepthY();
2355    g_bitDepth[CHANNEL_TYPE_CHROMA]   = pcSPS->getBitDepthC();
2356#endif
2357    g_uiMaxCUWidth  = pcSPS->getMaxCUWidth();
2358    g_uiMaxCUHeight = pcSPS->getMaxCUHeight();
2359    g_uiMaxCUDepth  = pcSPS->getMaxCUDepth();
2360    g_uiAddCUDepth  = max (0, pcSPS->getLog2MinCodingBlockSize() - (Int)pcSPS->getQuadtreeTULog2MinSize() );
2361
2362    Int  numReorderPics[MAX_TLAYER];
2363    Window &conformanceWindow = slice->getConformanceWindow();
2364    Window defaultDisplayWindow = pcSPS->getVuiParametersPresentFlag() ? pcSPS->getVuiParameters()->getDefaultDisplayWindow() : Window();
2365
2366    for( Int temporalLayer=0; temporalLayer < MAX_TLAYER; temporalLayer++ ) 
2367    {
2368      if( m_commonDecoderParams->getTargetOutputLayerSetIdx() == 0 )
2369      {
2370        assert( this->getLayerId() == 0 );
2371        numReorderPics[temporalLayer] = pcSPS->getNumReorderPics(temporalLayer);
2372      }
2373      else
2374      {
2375        TComVPS *vps = slice->getVPS();
2376        // SHM decoders will use DPB size table in the VPS to determine the number of reorder pictures.
2377        numReorderPics[temporalLayer] = vps->getMaxVpsNumReorderPics( getCommonDecoderParams()->getTargetOutputLayerSetIdx() , temporalLayer);
2378      }
2379    }
2380
2381    if (m_cIlpPic[0] == NULL)
2382    {
2383      for (Int j=0; j < m_numDirectRefLayers; j++)
2384      {
2385
2386        m_cIlpPic[j] = new  TComPic;
2387
2388#if AUXILIARY_PICTURES
2389#if REPN_FORMAT_IN_VPS
2390        m_cIlpPic[j]->create(picWidth, picHeight, slice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
2391#else
2392        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), pcSPS->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
2393#endif
2394#else
2395#if REPN_FORMAT_IN_VPS
2396        m_cIlpPic[j]->create(picWidth, picHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
2397#else
2398        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
2399#endif
2400#endif
2401        for (Int i=0; i<m_cIlpPic[j]->getPicSym()->getNumberOfCtusInFrame(); i++)
2402        {
2403          m_cIlpPic[j]->getPicSym()->getCtu(i)->initCtu(m_cIlpPic[j], i);
2404        }
2405      }
2406    }
2407  }
2408}
2409
2410TDecTop* TDecTop::getRefLayerDec( UInt refLayerIdx )
2411{
2412  TComVPS* vps = m_parameterSetManagerDecoder.getActiveVPS();
2413  if( vps->getNumDirectRefLayers( m_layerId ) <= 0 )
2414  {
2415    return (TDecTop *)getLayerDec( 0 );
2416  }
2417 
2418  return (TDecTop *)getLayerDec( vps->getRefLayerId( m_layerId, refLayerIdx ) );
2419}
2420
2421Void TDecTop::setRefLayerParams( TComVPS* vps )
2422{
2423  for(UInt layerIdx = 0; layerIdx < m_numLayer; layerIdx++)
2424  {
2425    TDecTop *decTop = (TDecTop *)getLayerDec(vps->getLayerIdInNuh(layerIdx));
2426    decTop->setNumSamplePredRefLayers(0);
2427    decTop->setNumMotionPredRefLayers(0);
2428    decTop->setNumDirectRefLayers(0);
2429
2430    for(Int j = 0; j < layerIdx; j++)
2431    {
2432      if (vps->getDirectDependencyFlag(layerIdx, j))
2433      {
2434        decTop->setRefLayerId(decTop->getNumDirectRefLayers(), vps->getLayerIdInNuh(layerIdx));
2435        decTop->setNumDirectRefLayers(decTop->getNumDirectRefLayers() + 1);
2436
2437        Int samplePredEnabledFlag = (vps->getDirectDependencyType(layerIdx, j) + 1) & 1;
2438        decTop->setNumSamplePredRefLayers(decTop->getNumSamplePredRefLayers() + samplePredEnabledFlag);
2439
2440        Int motionPredEnabledFlag = ((vps->getDirectDependencyType(layerIdx, j) + 1) & 2) >> 1;
2441        decTop->setNumMotionPredRefLayers(decTop->getNumMotionPredRefLayers() + motionPredEnabledFlag);
2442      }
2443    }
2444  }
2445}
2446
2447Void TDecTop::checkValueOfTargetOutputLayerSetIdx(TComVPS *vps)
2448{
2449  CommonDecoderParams* params = this->getCommonDecoderParams();
2450
2451  if( params->getValueCheckedFlag() )
2452  {
2453    return; // Already checked
2454  }
2455
2456  if( params->getTargetOutputLayerSetIdx() == -1 )  // Output layer set index not specified
2457  {
2458    if( params->getTargetLayerId() > vps->getMaxLayerId() )
2459    {
2460      printf( "Warning: specified target layerId %d is greater than max layerId %d. Target layerId is set equal to max layerId %d.\n", params->getTargetLayerId(), vps->getMaxLayerId(), vps->getMaxLayerId() );
2461      params->setTargetLayerId( vps->getMaxLayerId() );
2462    }
2463
2464    Bool layerSetMatchFound = false;
2465    // Output layer set index not assigned.
2466    // Based on the value of targetLayerId, check if any of the output layer matches
2467    // Currently, the target layer ID in the encoder assumes that all the layers are decoded   
2468    // Check if any of the output layer sets match this description
2469    for( Int i = 0; i < vps->getNumOutputLayerSets(); i++ )
2470    {
2471      Bool layerSetMatchFlag = false;
2472      Int layerSetIdx = vps->getOutputLayerSetIdx( i );
2473
2474      for( Int j = 0; j < vps->getNumLayersInIdList( layerSetIdx ); j++ )
2475      {
2476        if( vps->getLayerSetLayerIdList( layerSetIdx, j ) == params->getTargetLayerId() )
2477        {
2478          layerSetMatchFlag = true;
2479          break;
2480        }
2481      }
2482     
2483      if( layerSetMatchFlag ) // Potential output layer set candidate found
2484      {
2485        // If target dec layer ID list is also included - check if they match
2486        if( params->getTargetDecLayerIdSet() )
2487        {
2488          if( params->getTargetDecLayerIdSet()->size() ) 
2489          {
2490            for( Int j = 0; j < vps->getNumLayersInIdList( layerSetIdx ); j++ )
2491            {
2492              if( *(params->getTargetDecLayerIdSet()->begin() + j) != vps->getLayerIdInNuh(vps->getLayerSetLayerIdList( layerSetIdx, j )))
2493              {
2494                layerSetMatchFlag = false;
2495              }
2496            }
2497          }
2498        }
2499        if( layerSetMatchFlag ) // The target dec layer ID list also matches, if present
2500        {
2501          // Match found
2502          layerSetMatchFound = true;
2503          params->setTargetOutputLayerSetIdx( i );
2504          params->setValueCheckedFlag( true );
2505          break;
2506        }
2507      }
2508    }
2509    assert( layerSetMatchFound ); // No output layer set matched the value of either targetLayerId or targetdeclayerIdlist
2510  }   
2511  else // Output layer set index is assigned - check if the values match
2512  {
2513    // Check if the target decoded layer is the highest layer in the list
2514    assert( params->getTargetOutputLayerSetIdx() < vps->getNumOutputLayerSets() );
2515#if !CONFORMANCE_BITSTREAM_MODE
2516    assert( params->getTargetOutputLayerSetIdx() < vps->getNumLayerSets() );
2517#endif
2518    Int layerSetIdx = vps->getOutputLayerSetIdx( params->getTargetOutputLayerSetIdx() );  // Index to the layer set
2519#if !CONFORMANCE_BITSTREAM_MODE
2520    assert( params->getTargetLayerId() == vps->getNumLayersInIdList( layerSetIdx ) - 1);
2521#endif
2522   
2523    // Check if the targetdeclayerIdlist matches the output layer set
2524    if( params->getTargetDecLayerIdSet() )
2525    {
2526      if( params->getTargetDecLayerIdSet()->size() ) 
2527      {
2528        for(Int i = 0; i < vps->getNumLayersInIdList( layerSetIdx ); i++)
2529        {
2530          assert( *(params->getTargetDecLayerIdSet()->begin() + i) == vps->getLayerIdInNuh(vps->getLayerSetLayerIdList( layerSetIdx, i )));
2531        }
2532      }
2533    }
2534    params->setValueCheckedFlag( true );
2535
2536  }
2537
2538  // Set correct value of targetLayerId
2539  Int targetOlsIdx = params->getTargetOutputLayerSetIdx();
2540  Int targetLsIdx = vps->getOutputLayerSetIdx( targetOlsIdx );
2541  params->setTargetLayerId( vps->getLayerSetLayerIdList( targetLsIdx, vps->getNumLayersInIdList(targetLsIdx)-1 ) );
2542
2543  // Check if the current layer is an output layer
2544  for( Int i = 0; i < vps->getNumLayersInIdList( targetLsIdx ); i++ )
2545  {
2546    if( vps->getOutputLayerFlag( targetOlsIdx, i ) )
2547    {
2548      this->getLayerDec( vps->getLayerSetLayerIdList( targetLsIdx, i ) )->m_isOutputLayerFlag = true;
2549    }
2550  }
2551}
2552
2553#if POC_RESET_IDC_DECODER
2554Void TDecTop::markAllPicsAsNoCurrAu(TComVPS *vps)
2555{
2556  for(Int i = 0; i < MAX_LAYERS; i++)
2557  {
2558    TComList<TComPic*>* listPic = this->getLayerDec(vps->getLayerIdInNuh(i))->getListPic();
2559    TComList<TComPic*>::iterator  iterPic = listPic->begin();
2560    while ( iterPic != listPic->end() )
2561    {
2562      TComPic *pcPic = *(iterPic);
2563      pcPic->setCurrAuFlag( false );
2564      iterPic++;
2565    }
2566  }
2567}
2568#endif
2569#if Q0048_CGS_3D_ASYMLUT
2570Void TDecTop::initAsymLut(TComSlice *pcSlice)
2571{
2572  if(m_layerId>0)
2573  {
2574    if(!m_pColorMappedPic)
2575    {
2576      Int picWidth    = pcSlice->getPicWidthInLumaSamples();
2577      Int picHeight   = pcSlice->getPicHeightInLumaSamples();
2578      m_pColorMappedPic = new TComPicYuv;
2579      m_pColorMappedPic->create( picWidth, picHeight, pcSlice->getChromaFormatIdc()/*CHROMA_420*/, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, NULL );
2580    }
2581  }
2582}
2583#endif
2584#if POC_RESET_RESTRICTIONS
2585Void TDecTop::resetPocRestrictionCheckParameters()
2586{
2587  TDecTop::m_checkPocRestrictionsForCurrAu       = false;
2588  TDecTop::m_pocResetIdcOrCurrAu                 = -1;
2589  TDecTop::m_baseLayerIdrFlag                    = false;
2590  TDecTop::m_baseLayerPicPresentFlag             = false;
2591  TDecTop::m_baseLayerIrapFlag                   = false;
2592  TDecTop::m_nonBaseIdrPresentFlag               = false;
2593  TDecTop::m_nonBaseIdrType                      = -1;
2594  TDecTop::m_picNonIdrWithRadlPresentFlag        = false;
2595  TDecTop::m_picNonIdrNoLpPresentFlag            = false;
2596}
2597#endif
2598
2599Void TDecTop::xCheckLayerReset()
2600{
2601  if (m_apcSlicePilot->isIRAP() && m_layerId > m_smallestLayerId)
2602  {
2603    Bool layerResetFlag;
2604    UInt dolLayerId;
2605    if (m_lastPicHasEos)
2606    {
2607      layerResetFlag = true;
2608      dolLayerId = m_layerId;
2609    }
2610    else if ((m_apcSlicePilot->isCRA() && m_apcSlicePilot->getHandleCraAsBlaFlag()) ||
2611      (m_apcSlicePilot->isIDR() && m_apcSlicePilot->getCrossLayerBLAFlag()) || m_apcSlicePilot->isBLA())
2612    {
2613      layerResetFlag = true;
2614      dolLayerId = m_layerId;
2615    }
2616    else
2617    {
2618      layerResetFlag = false;
2619    }
2620
2621    if (layerResetFlag)
2622    {
2623      for (Int i = 0; i < m_apcSlicePilot->getVPS()->getNumPredictedLayers(dolLayerId); i++)
2624      {
2625        UInt iLayerId = m_apcSlicePilot->getVPS()->getPredictedLayerId(dolLayerId, i);
2626        m_ppcTDecTop[iLayerId]->m_layerInitializedFlag = false;
2627        m_ppcTDecTop[iLayerId]->m_firstPicInLayerDecodedFlag = false;
2628      }
2629
2630      for (TComList<TComPic*>::iterator i = m_cListPic.begin(); i != m_cListPic.end(); i++)
2631      {
2632        if ((*i)->getPOC() != m_apcSlicePilot->getPOC())
2633        {
2634          (*i)->getSlice(0)->setReferenced(false);
2635        }
2636      }
2637
2638      for (UInt i = 0; i < m_apcSlicePilot->getVPS()->getNumPredictedLayers(dolLayerId); i++)
2639      {
2640        UInt predLId = m_apcSlicePilot->getVPS()->getPredictedLayerId(dolLayerId, i);
2641        for (TComList<TComPic*>::iterator pic = m_ppcTDecTop[predLId]->getListPic()->begin(); pic != m_ppcTDecTop[predLId]->getListPic()->end(); pic++)
2642        {
2643          if ((*pic)->getSlice(0)->getPOC() != m_apcSlicePilot->getPOC())
2644          {
2645            (*pic)->getSlice(0)->setReferenced(false);
2646          }
2647        }
2648      }
2649    }
2650  }
2651}
2652
2653Void TDecTop::xSetLayerInitializedFlag()
2654{
2655  if (m_apcSlicePilot->isIRAP() && m_apcSlicePilot->getNoRaslOutputFlag())
2656  {
2657    if (m_layerId == 0)
2658    {
2659      m_ppcTDecTop[m_layerId]->setLayerInitializedFlag(true);
2660    }
2661    else if (!m_ppcTDecTop[m_layerId]->getLayerInitializedFlag() && m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId) == 0)
2662    {
2663      m_ppcTDecTop[m_layerId]->setLayerInitializedFlag(true);
2664    }
2665    else if (!m_ppcTDecTop[m_layerId]->getLayerInitializedFlag())
2666    {
2667      Bool refLayersInitialized = true;
2668      for (UInt j = 0; j < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); j++)
2669      {
2670        UInt refLayerId = m_apcSlicePilot->getVPS()->getRefLayerId(m_layerId, j);
2671        if (!m_ppcTDecTop[refLayerId]->getLayerInitializedFlag())
2672        {
2673          refLayersInitialized = false;
2674        }
2675      }
2676      if (refLayersInitialized)
2677      {
2678        m_ppcTDecTop[m_layerId]->setLayerInitializedFlag(true);
2679      }
2680    }
2681  }
2682}
2683
2684Void TDecTop::xDeriveSmallestLayerId(TComVPS* vps)
2685{
2686  UInt smallestLayerId;
2687  Int  targetOlsIdx = m_commonDecoderParams->getTargetOutputLayerSetIdx();
2688  assert( targetOlsIdx >= 0 );
2689
2690  UInt targetDecLayerSetIdx = vps->getOutputLayerSetIdx(targetOlsIdx);
2691  UInt lsIdx = targetDecLayerSetIdx;
2692  UInt targetDecLayerIdList[MAX_LAYERS] = {0};
2693
2694  for (UInt i = 0, j = 0; i < vps->getNumLayersInIdList(lsIdx); i++)
2695  {
2696    if (vps->getNecessaryLayerFlag(targetOlsIdx, i))
2697    {
2698      targetDecLayerIdList[j++] = vps->getLayerSetLayerIdList(lsIdx, i);
2699    }
2700  }
2701
2702  if (targetDecLayerSetIdx <= vps->getVpsNumLayerSetsMinus1())
2703  {
2704    smallestLayerId = 0;
2705  }
2706  else if (vps->getNumLayersInIdList(targetDecLayerSetIdx) == 1)
2707  {
2708    smallestLayerId = 0;
2709  }
2710  else
2711  {
2712    smallestLayerId = targetDecLayerIdList[0];
2713  }
2714
2715  for( UInt layerId = 0; layerId < MAX_VPS_LAYER_IDX_PLUS1; layerId++ )
2716  {
2717    m_ppcTDecTop[layerId]->m_smallestLayerId = smallestLayerId;
2718  }
2719}
2720
2721Void TDecTop::xSetSpatialEnhLayerFlag(TComSlice* slice, TComPic* pic)
2722{
2723  for(UInt i = 0; i < slice->getVPS()->getNumDirectRefLayers( m_layerId ); i++ )
2724  {
2725    const Window scalEL = slice->getPPS()->getScaledRefLayerWindowForLayer(slice->getVPS()->getRefLayerId(m_layerId, i));
2726    const Window refEL = slice->getPPS()->getRefLayerWindowForLayer(slice->getVPS()->getRefLayerId(m_layerId, i));
2727    Bool equalOffsets = scalEL.hasEqualOffset(refEL);
2728    Bool zeroPhase = slice->getPPS()->hasZeroResamplingPhase(slice->getVPS()->getRefLayerId(m_layerId, i));
2729
2730    TDecTop *pcTDecTopBase = (TDecTop *)getRefLayerDec( i );
2731    //TComPic*                      pcPic = *(pcTDecTopBase->getListPic()->begin());
2732    TComPicYuv* pcPicYuvRecBase = (*(pcTDecTopBase->getListPic()->begin()))->getPicYuvRec(); 
2733
2734    UInt refLayerId = slice->getVPS()->getRefLayerId(m_layerId, i);
2735    Bool sameBitDepths = ( g_bitDepthLayer[CHANNEL_TYPE_LUMA][m_layerId] == g_bitDepthLayer[CHANNEL_TYPE_LUMA][refLayerId] ) && ( g_bitDepthLayer[CHANNEL_TYPE_CHROMA][m_layerId] == g_bitDepthLayer[CHANNEL_TYPE_CHROMA][refLayerId] );
2736
2737    if( pcPicYuvRecBase->getWidth(COMPONENT_Y) == slice->getPicWidthInLumaSamples() && pcPicYuvRecBase->getHeight(COMPONENT_Y) == slice->getPicHeightInLumaSamples() && equalOffsets && zeroPhase )
2738    {
2739      pic->setEqualPictureSizeAndOffsetFlag( i, true );
2740    }
2741
2742    if( !pic->equalPictureSizeAndOffsetFlag(i) || !sameBitDepths
2743#if Q0048_CGS_3D_ASYMLUT
2744      || slice->getPPS()->getCGSFlag() > 0
2745#endif
2746#if LAYER_CTB
2747      || pcTDecTopBase->getActiveSPS()->getMaxCUWidth() != m_ppcTDecTop[m_layerId]->getActiveSPS()->getMaxCUWidth() || pcTDecTopBase->getActiveSPS()->getMaxCUHeight() != m_ppcTDecTop[m_layerId]->getActiveSPS()->getMaxCUHeight() || pcTDecTopBase->getActiveSPS()->getMaxCUDepth() != m_ppcTDecTop[m_layerId]->getActiveSPS()->getMaxCUDepth()
2748#endif
2749      )
2750    {
2751      pic->setSpatialEnhLayerFlag( i, true );
2752
2753      //only for scalable extension
2754      assert( slice->getVPS()->getScalabilityMask( SCALABILITY_ID ) == true );
2755    }
2756  }
2757}
2758
2759#endif //SVC_EXTENSION
2760
2761
2762//! \}
Note: See TracBrowser for help on using the repository browser.