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

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

fix compiler warnings

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