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

Last change on this file since 851 was 851, checked in by qualcomm, 10 years ago

R0223: Restrictions on POC reset-related syntax elements (Macro: POC_RESET_RESTRICTIONS)

Allow mixing of CRA and BLA pictures without requiring POC resetting (adoption) and other restrictions that were not implemented before.

From: Adarsh K. Ramasubramonian <aramasub@…>

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