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

Last change on this file since 742 was 740, checked in by qualcomm, 12 years ago

Implementation of JCTVC_Q0177 -- Inference of sps_temporal_id_nesting_flag when it is not present.

Submitted by Hendry (fhendry@…)

  • Property svn:eol-style set to native
File size: 102.6 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2014, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TDecTop.cpp
35    \brief    decoder class
36*/
37
38#include "NALread.h"
39#include "TDecTop.h"
40
41#if SVC_EXTENSION
42UInt  TDecTop::m_prevPOC = MAX_UINT;
43UInt  TDecTop::m_uiPrevLayerId = MAX_UINT;
44Bool  TDecTop::m_bFirstSliceInSequence = true;
45#endif
46
47//! \ingroup TLibDecoder
48//! \{
49
50TDecTop::TDecTop()
51{
52  m_pcPic = 0;
53  m_iMaxRefPicNum = 0;
54#if ENC_DEC_TRACE
55  g_hTrace = fopen( "TraceDec.txt", "wb" );
56  g_bJustDoIt = g_bEncDecTraceDisable;
57  g_nSymbolCounter = 0;
58#endif
59  m_associatedIRAPType = NAL_UNIT_INVALID;
60  m_pocCRA = 0;
61  m_pocRandomAccess = MAX_INT;         
62#if !SVC_EXTENSION
63  m_prevPOC                = MAX_INT;
64#endif
65  m_bFirstSliceInPicture    = true;
66#if !SVC_EXTENSION
67  m_bFirstSliceInSequence   = true;
68#endif
69#if SVC_EXTENSION
70  m_layerId = 0;
71#if AVC_BASE
72  m_pBLReconFile = NULL;
73#endif
74  memset(m_cIlpPic, 0, sizeof(m_cIlpPic));
75#endif
76#if AVC_SYNTAX || SYNTAX_OUTPUT
77  m_pBLSyntaxFile = NULL;
78#endif
79  m_prevSliceSkipped = false;
80  m_skippedPOC = 0;
81#if 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 AVC_BASE
782  if( activeVPS->getAvcBaseLayerFlag() )
783  {
784    TComPic* pBLPic = (*m_ppcTDecTop[0]->getListPic()->begin());
785    if( m_layerId == 1 && pBLPic->getPicYuvRec() == NULL )
786    {
787      UInt refLayerId = 0;
788      RepFormat* repFormat = activeVPS->getVpsRepFormat( activeVPS->getVpsRepFormatIdx(refLayerId) );
789
790      Int  numReorderPics[MAX_TLAYER];
791      Window conformanceWindow;
792      Window defaultDisplayWindow;
793
794#if AUXILIARY_PICTURES
795#if SVC_UPSAMPLING
796#if AVC_SYNTAX
797      pBLPic->create( repFormat->getPicWidthVpsInLumaSamples(), repFormat->getPicHeightVpsInLumaSamples(), repFormat->getChromaFormatVpsIdc(), activeSPS->getMaxCUWidth(), activeSPS->getMaxCUHeight(), activeSPS->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, activeSPS, true);
798#else
799      pBLPic->create( repFormat->getPicWidthVpsInLumaSamples(), repFormat->getPicHeightVpsInLumaSamples(), repFormat->getChromaFormatVpsIdc(), activeSPS->getMaxCUWidth(), activeSPS->getMaxCUHeight(), activeSPS->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, NULL, true);
800#endif
801#else
802      pBLPic->create( repFormat->getPicWidthVpsInLumaSamples(), repFormat->getPicHeightVpsInLumaSamples(), repFormat->getChromaFormatVpsIdc(), activeSPS->getMaxCUWidth(), activeSPS->getMaxCUHeight(), activeSPS->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, true);
803#endif
804#else
805#if SVC_UPSAMPLING
806#if AVC_SYNTAX
807      pBLPic->create( repFormat->getPicWidthVpsInLumaSamples(), repFormat->getPicHeightVpsInLumaSamples(), activeSPS->getMaxCUWidth(), activeSPS->getMaxCUHeight(), activeSPS->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, activeSPS, true);
808#else
809      pBLPic->create( repFormat->getPicWidthVpsInLumaSamples(), repFormat->getPicHeightVpsInLumaSamples(), activeSPS->getMaxCUWidth(), activeSPS->getMaxCUHeight(), activeSPS->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, NULL, true);
810#endif
811#else
812      pBLPic->create( repFormat->getPicWidthVpsInLumaSamples(), repFormat->getPicHeightVpsInLumaSamples(), activeSPS->getMaxCUWidth(), activeSPS->getMaxCUHeight(), activeSPS->getMaxCUDepth(), onformanceWindow, defaultDisplayWindow, numReorderPics, true);
813#endif
814#endif
815      // it is needed where the VPS is accessed through the slice
816      pBLPic->getSlice(0)->setVPS( activeVPS );
817
818#if O0194_DIFFERENT_BITDEPTH_EL_BL
819      g_bitDepthYLayer[0] = repFormat->getBitDepthVpsLuma();
820      g_bitDepthCLayer[0] = repFormat->getBitDepthVpsChroma();
821#endif
822    }
823  }
824#endif
825
826#if P0312_VERT_PHASE_ADJ
827  if( activeVPS->getVpsVuiVertPhaseInUseFlag() == 0 )
828  {   
829    for(Int i = 0; i < activeSPS->getNumScaledRefLayerOffsets(); i++)
830    {
831      UInt scaledRefLayerId = activeSPS->getScaledRefLayerId(i);
832      if( activeSPS->getVertPhasePositionEnableFlag( scaledRefLayerId ) )
833      {
834        printf("\nWarning: LayerId = %d: vert_phase_position_enable_flag[%d] = 1, however indication vert_phase_position_in_use_flag = 0\n", m_layerId, scaledRefLayerId );
835        break;
836      }
837    }
838  }
839#endif
840
841#if SPS_DPB_PARAMS
842  if( m_layerId > 0 )
843  {
844    // When not present sps_max_sub_layers_minus1 is inferred to be equal to vps_max_sub_layers_minus1.
845    sps->setMaxTLayers( activeVPS->getMaxTLayers() );
846
847    // When not present sps_temporal_id_nesting_flag is inferred to be equal to vps_temporal_id_nesting_flag
848#if Q0177_SPS_TEMP_NESTING_FIX
849    sps->setTemporalIdNestingFlag( (sps->getMaxTLayers() > 1) ? activeVPS->getTemporalNestingFlag() : true );
850#else
851    sps->setTemporalIdNestingFlag( activeVPS->getTemporalNestingFlag() );
852#endif
853
854    // 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,
855    // 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.
856    for(UInt i=0; i < sps->getMaxTLayers(); i++)
857    {
858      // to avoid compiler warning "array subscript is above array bounds"
859      assert( i < MAX_TLAYER );
860
861      sps->setMaxDecPicBuffering( activeVPS->getMaxVpsDecPicBufferingMinus1( getCommonDecoderParams()->getTargetOutputLayerSetIdx(), sps->getLayerId(), i) + 1, i);
862    }
863  }
864#endif
865
866  if( pps->getDependentSliceSegmentsEnabledFlag() )
867  {
868    Int NumCtx = pps->getEntropyCodingSyncEnabledFlag()?2:1;
869
870    if (m_cSliceDecoder.getCtxMemSize() != NumCtx)
871    {
872      m_cSliceDecoder.initCtxMem(NumCtx);
873      for ( UInt st = 0; st < NumCtx; st++ )
874      {
875        TDecSbac* ctx = NULL;
876        ctx = new TDecSbac;
877        ctx->init( &m_cBinCABAC );
878        m_cSliceDecoder.setCtxMem( ctx, st );
879      }
880    }
881  }
882
883  m_apcSlicePilot->setPPS(pps);
884  m_apcSlicePilot->setSPS(sps);
885  pps->setSPS(sps);
886#if REPN_FORMAT_IN_VPS
887  pps->setNumSubstreams(pps->getEntropyCodingSyncEnabledFlag() ? ((sps->getPicHeightInLumaSamples() + sps->getMaxCUHeight() - 1) / sps->getMaxCUHeight()) * (pps->getNumColumnsMinus1() + 1) : 1);
888#else
889  pps->setNumSubstreams(pps->getEntropyCodingSyncEnabledFlag() ? ((sps->getPicHeightInLumaSamples() + sps->getMaxCUHeight() - 1) / sps->getMaxCUHeight()) * (pps->getNumColumnsMinus1() + 1) : 1);
890#endif
891  pps->setMinCuDQPSize( sps->getMaxCUWidth() >> ( pps->getMaxCuDQPDepth()) );
892
893#if REPN_FORMAT_IN_VPS
894  g_bitDepthY     = m_apcSlicePilot->getBitDepthY();
895  g_bitDepthC     = m_apcSlicePilot->getBitDepthC();
896#else
897  g_bitDepthY     = sps->getBitDepthY();
898  g_bitDepthC     = sps->getBitDepthC();
899#endif
900  g_uiMaxCUWidth  = sps->getMaxCUWidth();
901  g_uiMaxCUHeight = sps->getMaxCUHeight();
902  g_uiMaxCUDepth  = sps->getMaxCUDepth();
903  g_uiAddCUDepth  = max (0, sps->getLog2MinCodingBlockSize() - (Int)sps->getQuadtreeTULog2MinSize() );
904
905#if Q0074_SEI_COLOR_MAPPING
906  for(Int compID=0; compID<3; compID++)
907  {
908    m_ColorMapping->setColorMapping( compID ? g_bitDepthC : g_bitDepthY, compID );
909  }
910#endif
911
912  for (Int i = 0; i < sps->getLog2DiffMaxMinCodingBlockSize(); i++)
913  {
914    sps->setAMPAcc( i, sps->getUseAMP() );
915  }
916
917  for (Int i = sps->getLog2DiffMaxMinCodingBlockSize(); i < sps->getMaxCUDepth(); i++)
918  {
919    sps->setAMPAcc( i, 0 );
920  }
921
922  m_cSAO.destroy();
923#if REPN_FORMAT_IN_VPS
924#if AUXILIARY_PICTURES
925  m_cSAO.create( m_apcSlicePilot->getPicWidthInLumaSamples(), m_apcSlicePilot->getPicHeightInLumaSamples(), sps->getChromaFormatIdc(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth() );
926#else
927  m_cSAO.create( m_apcSlicePilot->getPicWidthInLumaSamples(), m_apcSlicePilot->getPicHeightInLumaSamples(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth() );
928#endif
929#else
930  m_cSAO.create( sps->getPicWidthInLumaSamples(), sps->getPicHeightInLumaSamples(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth() );
931#endif
932  m_cLoopFilter.create( sps->getMaxCUDepth() );
933}
934
935#if SVC_EXTENSION
936#if POC_RESET_FLAG
937Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int &iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC )
938#else
939Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC )
940#endif
941#else
942Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay )
943#endif
944{
945  TComPic*&   pcPic         = m_pcPic;
946#if SVC_EXTENSION
947  m_apcSlicePilot->setVPS( m_parameterSetManagerDecoder.getPrefetchedVPS(0) );
948#if OUTPUT_LAYER_SET_INDEX
949  // Following check should go wherever the VPS is activated
950  checkValueOfTargetOutputLayerSetIdx( m_apcSlicePilot->getVPS());
951#endif
952#if RESOLUTION_BASED_DPB
953  // Following assignment should go wherever a new VPS is activated
954  assignSubDpbs(m_apcSlicePilot->getVPS());
955#endif
956  m_apcSlicePilot->initSlice( nalu.m_layerId );
957#else //SVC_EXTENSION
958  m_apcSlicePilot->initSlice();
959#endif
960
961  if (m_bFirstSliceInPicture)
962  {
963    m_uiSliceIdx     = 0;
964  }
965  else
966  {
967    m_apcSlicePilot->copySliceInfo( pcPic->getPicSym()->getSlice(m_uiSliceIdx-1) );
968  }
969  m_apcSlicePilot->setSliceIdx(m_uiSliceIdx);
970
971  m_apcSlicePilot->setNalUnitType(nalu.m_nalUnitType);
972  Bool nonReferenceFlag = (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TRAIL_N ||
973                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N   ||
974                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N  ||
975                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N  ||
976                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N);
977  m_apcSlicePilot->setTemporalLayerNonReferenceFlag(nonReferenceFlag);
978 
979  m_apcSlicePilot->setReferenced(true); // Putting this as true ensures that picture is referenced the first time it is in an RPS
980  m_apcSlicePilot->setTLayerInfo(nalu.m_temporalId);
981
982#if SVC_EXTENSION
983#if VPS_EXTN_DIRECT_REF_LAYERS
984  setRefLayerParams(m_apcSlicePilot->getVPS());
985#endif
986  m_apcSlicePilot->setNumMotionPredRefLayers(m_numMotionPredRefLayers);
987#endif
988  m_cEntropyDecoder.decodeSliceHeader (m_apcSlicePilot, &m_parameterSetManagerDecoder);
989
990  // set POC for dependent slices in skipped pictures
991  if(m_apcSlicePilot->getDependentSliceSegmentFlag() && m_prevSliceSkipped) 
992  {
993    m_apcSlicePilot->setPOC(m_skippedPOC);
994  }
995
996  m_apcSlicePilot->setAssociatedIRAPPOC(m_pocCRA);
997  m_apcSlicePilot->setAssociatedIRAPType(m_associatedIRAPType);
998
999#if SETTING_NO_OUT_PIC_PRIOR
1000  //For inference of NoOutputOfPriorPicsFlag
1001  if (m_apcSlicePilot->getRapPicFlag())
1002  {
1003    if ((m_apcSlicePilot->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && m_apcSlicePilot->getNalUnitType() <= NAL_UNIT_CODED_SLICE_IDR_N_LP) || 
1004        (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA && m_bFirstSliceInSequence) ||
1005        (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA && m_apcSlicePilot->getHandleCraAsBlaFlag()))
1006    {
1007      m_apcSlicePilot->setNoRaslOutputFlag(true);
1008    }
1009    //the inference for NoOutputPriorPicsFlag
1010    if (!m_bFirstSliceInBitstream && m_apcSlicePilot->getRapPicFlag() && m_apcSlicePilot->getNoRaslOutputFlag())
1011    {
1012      if (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA)
1013      {
1014        m_apcSlicePilot->setNoOutputPriorPicsFlag(true);
1015      }
1016    }
1017    else
1018    {
1019      m_apcSlicePilot->setNoOutputPriorPicsFlag(false);
1020    }
1021
1022    if(m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA)
1023    {
1024      m_craNoRaslOutputFlag = m_apcSlicePilot->getNoRaslOutputFlag();
1025    }
1026  }
1027  if (m_apcSlicePilot->getRapPicFlag() && m_apcSlicePilot->getNoOutputPriorPicsFlag())
1028  {
1029    m_lastPOCNoOutputPriorPics = m_apcSlicePilot->getPOC();
1030    m_isNoOutputPriorPics = true;
1031  }
1032  else
1033  {
1034    m_isNoOutputPriorPics = false;
1035  }
1036
1037  //For inference of PicOutputFlag
1038  if (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R)
1039  {
1040    if ( m_craNoRaslOutputFlag )
1041    {
1042      m_apcSlicePilot->setPicOutputFlag(false);
1043    }
1044  }
1045#endif
1046
1047#if FIX_POC_CRA_NORASL_OUTPUT
1048  if (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA && m_craNoRaslOutputFlag) //Reset POC MSB when CRA has NoRaslOutputFlag equal to 1
1049  {
1050    Int iMaxPOClsb = 1 << m_apcSlicePilot->getSPS()->getBitsForPOC();
1051    m_apcSlicePilot->setPOC( m_apcSlicePilot->getPOC() & (iMaxPOClsb - 1) );
1052  }
1053#endif
1054
1055  // Skip pictures due to random access
1056  if (isRandomAccessSkipPicture(iSkipFrame, iPOCLastDisplay))
1057  {
1058    m_prevSliceSkipped = true;
1059    m_skippedPOC = m_apcSlicePilot->getPOC();
1060    return false;
1061  }
1062  // Skip TFD pictures associated with BLA/BLANT pictures
1063  if (isSkipPictureForBLA(iPOCLastDisplay))
1064  {
1065    m_prevSliceSkipped = true;
1066    m_skippedPOC = m_apcSlicePilot->getPOC();
1067    return false;
1068  }
1069
1070  // clear previous slice skipped flag
1071  m_prevSliceSkipped = false;
1072
1073  // exit when a new picture is found
1074#if SVC_EXTENSION
1075  bNewPOC = (m_apcSlicePilot->getPOC()!= m_prevPOC);
1076
1077#if NO_OUTPUT_OF_PRIOR_PICS
1078#if NO_CLRAS_OUTPUT_FLAG
1079  if (m_layerId == 0 && m_apcSlicePilot->getRapPicFlag() )
1080  {
1081    if (m_bFirstSliceInSequence)
1082    {
1083      setNoClrasOutputFlag(true);
1084    }
1085    else if ( m_apcSlicePilot->getBlaPicFlag() )
1086    {
1087      setNoClrasOutputFlag(true);
1088    }
1089#if O0149_CROSS_LAYER_BLA_FLAG
1090    else if (m_apcSlicePilot->getIdrPicFlag() && m_apcSlicePilot->getCrossLayerBLAFlag())
1091    {
1092      setNoClrasOutputFlag(true);
1093    }
1094#endif
1095    else
1096    {
1097      setNoClrasOutputFlag(false);
1098    }     
1099  }
1100  else
1101  {
1102    setNoClrasOutputFlag(false);
1103  }
1104
1105  m_apcSlicePilot->decodingRefreshMarking( m_cListPic, m_noClrasOutputFlag );
1106#endif
1107
1108  // Derive the value of NoOutputOfPriorPicsFlag
1109  if( bNewPOC || m_layerId!=m_uiPrevLayerId )   // i.e. new coded picture
1110  {
1111    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA && m_apcSlicePilot->getNoRaslOutputFlag() )
1112    {
1113      this->setNoOutputPriorPicsFlag( true );
1114    }
1115    else if( m_apcSlicePilot->getRapPicFlag() && m_apcSlicePilot->getNoRaslOutputFlag() )
1116    {
1117      this->setNoOutputPriorPicsFlag( m_apcSlicePilot->getNoOutputPriorPicsFlag() );
1118    }
1119    else
1120    {
1121      if( this->m_ppcTDecTop[0]->getNoClrasOutputFlag() )
1122      {
1123        this->setNoOutputPriorPicsFlag( true );
1124      }
1125    }
1126  }
1127#endif
1128
1129#if ALIGNED_BUMPING
1130  if (bNewPOC || m_layerId!=m_uiPrevLayerId)
1131  {
1132    m_apcSlicePilot->applyReferencePictureSet(m_cListPic, m_apcSlicePilot->getRPS());
1133  }
1134#endif
1135  if (m_apcSlicePilot->isNextSlice() && (bNewPOC || m_layerId!=m_uiPrevLayerId) && !m_bFirstSliceInSequence )
1136  {
1137    m_prevPOC = m_apcSlicePilot->getPOC();
1138    curLayerId = m_uiPrevLayerId; 
1139    m_uiPrevLayerId = m_layerId;
1140    return true;
1141  }
1142#else
1143  //we should only get a different poc for a new picture (with CTU address==0)
1144  if (m_apcSlicePilot->isNextSlice() && m_apcSlicePilot->getPOC()!=m_prevPOC && !m_bFirstSliceInSequence && (m_apcSlicePilot->getSliceCurStartCUAddr()!=0))
1145  {
1146    printf ("Warning, the first slice of a picture might have been lost!\n");
1147  }
1148  // exit when a new picture is found
1149  if (m_apcSlicePilot->isNextSlice() && (m_apcSlicePilot->getSliceCurStartCUAddr() == 0 && !m_bFirstSliceInPicture) && !m_bFirstSliceInSequence )
1150  {
1151    if (m_prevPOC >= m_pocRandomAccess)
1152    {
1153      m_prevPOC = m_apcSlicePilot->getPOC();
1154      return true;
1155    }
1156    m_prevPOC = m_apcSlicePilot->getPOC();
1157  }
1158#endif
1159  // actual decoding starts here
1160  xActivateParameterSets();
1161#if !O0223_O0139_IRAP_ALIGN_NO_CONTRAINTS && N0147_IRAP_ALIGN_FLAG
1162  //Note setting O0223_O0139_IRAP_ALIGN_NO_CONTRAINTS to 0 may cause decoder to crash.
1163  //When cross_layer_irap_aligned_flag is equal to 0, num_extra_slice_header_bits >=1
1164  if(!m_apcSlicePilot->getVPS()->getCrossLayerIrapAlignFlag() )
1165  {
1166    assert( m_apcSlicePilot->getPPS()->getNumExtraSliceHeaderBits() > 0);
1167  }
1168  //When cross_layer_irap_aligned_flag is equal to 1, the value of poc_reset_flag shall be equal to 0 
1169  if( m_apcSlicePilot->getVPS()->getCrossLayerIrapAlignFlag() )
1170  {
1171    assert( m_apcSlicePilot->getPocResetFlag() == 0);
1172  }
1173#endif
1174#if REPN_FORMAT_IN_VPS
1175  // Initialize ILRP if needed, only for the current layer 
1176  // ILRP intialization should go along with activation of parameters sets,
1177  // although activation of parameter sets itself need not be done for each and every slice!!!
1178  xInitILRP(m_apcSlicePilot);
1179#endif
1180  if (m_apcSlicePilot->isNextSlice()) 
1181  {
1182    m_prevPOC = m_apcSlicePilot->getPOC();
1183#if SVC_EXTENSION
1184    curLayerId = m_layerId;
1185    m_uiPrevLayerId = m_layerId;
1186#endif
1187  }
1188  m_bFirstSliceInSequence = false;
1189#if SETTING_NO_OUT_PIC_PRIOR 
1190  m_bFirstSliceInBitstream  = false;
1191#endif
1192#if POC_RESET_FLAG
1193  // This operation would do the following:
1194  // 1. Update the other picture in the DPB. This should be done only for the first slice of the picture.
1195  // 2. Update the value of m_pocCRA.
1196  // 3. Reset the POC values at the decoder for the current picture to be zero.
1197  // 4. update value of POCLastDisplay
1198  if( m_apcSlicePilot->getPocResetFlag() )
1199  {
1200    if( m_apcSlicePilot->getSliceIdx() == 0 )
1201    {
1202      Int pocAdjustValue = m_apcSlicePilot->getPOC();
1203
1204#if PREVTID0_POC_RESET
1205      m_apcSlicePilot->adjustPrevTid0POC(pocAdjustValue);
1206#endif
1207      // If poc reset flag is set to 1, reset all POC for DPB -> basically do it for each slice in the picutre
1208      TComList<TComPic*>::iterator  iterPic = m_cListPic.begin(); 
1209
1210      // Iterate through all picture in DPB
1211      while( iterPic != m_cListPic.end() )
1212      {
1213        TComPic *dpbPic = *iterPic;
1214        // Check if the picture pointed to by iterPic is either used for reference or
1215        // needed for output, are in the same layer, and not the current picture.
1216        if( /*  ( ( dpbPic->getSlice(0)->isReferenced() ) || ( dpbPic->getOutputMark() ) )
1217            &&*/ ( dpbPic->getLayerId() == m_apcSlicePilot->getLayerId() )
1218              && ( dpbPic->getReconMark() ) 
1219          )
1220        {
1221          for(Int i = dpbPic->getNumAllocatedSlice()-1; i >= 0; i--)
1222          {
1223
1224            TComSlice *slice = dpbPic->getSlice(i);
1225            TComReferencePictureSet *rps = slice->getRPS();
1226            slice->setPOC( slice->getPOC() - pocAdjustValue );
1227
1228            // Also adjust the POC value stored in the RPS of each such slice
1229            for(Int j = rps->getNumberOfPictures(); j >= 0; j--)
1230            {
1231              rps->setPOC( j, rps->getPOC(j) - pocAdjustValue );
1232            }
1233            // Also adjust the value of refPOC
1234            for(Int k = 0; k < 2; k++)  // For List 0 and List 1
1235            {
1236              RefPicList list = (k == 1) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
1237              for(Int j = 0; j < slice->getNumRefIdx(list); j++)
1238              {
1239                slice->setRefPOC( slice->getRefPOC(list, j) - pocAdjustValue, list, j);
1240              }
1241            }
1242          }
1243        }
1244        iterPic++;
1245      }
1246      // Update the value of pocCRA
1247      m_pocCRA -= pocAdjustValue;
1248      // Update value of POCLastDisplay
1249      iPOCLastDisplay -= pocAdjustValue;
1250    }
1251    // Reset current poc for current slice and RPS
1252    m_apcSlicePilot->setPOC( 0 );
1253  }
1254#endif
1255#if ALIGN_TSA_STSA_PICS
1256  if( m_apcSlicePilot->getLayerId() > 0 )
1257  {
1258    // Check for TSA alignment
1259    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
1260        m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R
1261         )
1262    {
1263      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1264      {
1265        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1266        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() );
1267        if( refpicLayer )
1268        {
1269          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
1270                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R );    // TSA pictures should be aligned among depenedent layers
1271        } 
1272      }
1273    }
1274    // Check for STSA alignment
1275    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
1276         m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R
1277         )
1278    {
1279      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1280      {
1281        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1282        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() ); // STSA pictures should be aligned among dependent layers
1283        if( refpicLayer )
1284
1285        {
1286          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
1287                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R );
1288        }
1289      }
1290    }
1291  }
1292#endif
1293  //detect lost reference picture and insert copy of earlier frame.
1294  Int lostPoc;
1295  while((lostPoc=m_apcSlicePilot->checkThatAllRefPicsAreAvailable(m_cListPic, m_apcSlicePilot->getRPS(), true, m_pocRandomAccess)) > 0)
1296  {
1297    xCreateLostPicture(lostPoc-1);
1298  }
1299  if (m_bFirstSliceInPicture)
1300  {
1301#if AVC_BASE
1302    if( m_layerId == 1 && m_parameterSetManagerDecoder.getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
1303    {
1304      TComPic* pBLPic = (*m_ppcTDecTop[0]->getListPic()->begin());
1305      pBLPic->getSlice(0)->setReferenced(true);
1306      fstream* pFile  = m_ppcTDecTop[0]->getBLReconFile();
1307
1308      if( pFile->good() )
1309      {
1310        Bool is16bit  = g_bitDepthYLayer[0] > 8 || g_bitDepthCLayer[0] > 8;
1311        UInt uiWidth  = pBLPic->getPicYuvRec()->getWidth();
1312        UInt uiHeight = pBLPic->getPicYuvRec()->getHeight();
1313
1314        Int len = uiWidth * (is16bit ? 2 : 1);
1315        UChar *buf = new UChar[len];
1316
1317        UInt64 uiPos = (UInt64) m_apcSlicePilot->getPOC() * uiWidth * uiHeight * 3 / 2;
1318
1319        pFile->seekg((UInt)uiPos, ios::beg );
1320
1321        // read Y component
1322        Pel* pPel = pBLPic->getPicYuvRec()->getLumaAddr();
1323        UInt uiStride = pBLPic->getPicYuvRec()->getStride();
1324        for( Int i = 0; i < uiHeight; i++ )
1325        {
1326          pFile->read(reinterpret_cast<Char*>(buf), len);
1327
1328          if( !is16bit )
1329          {
1330            for (Int x = 0; x < uiWidth; x++)
1331            {
1332              pPel[x] = buf[x];
1333            }
1334          }
1335          else
1336          {
1337            for (Int x = 0; x < uiWidth; x++)
1338            {
1339              pPel[x] = (buf[2*x+1] << 8) | buf[2*x];
1340            }
1341          }
1342     
1343          pPel += uiStride;
1344        }
1345
1346        len >>= 1;
1347        uiWidth >>= 1;
1348        uiHeight >>= 1;
1349
1350        // read Cb component
1351        pPel = pBLPic->getPicYuvRec()->getCbAddr();
1352        uiStride = pBLPic->getPicYuvRec()->getCStride();
1353        for( Int i = 0; i < uiHeight; i++ )
1354        {
1355          pFile->read(reinterpret_cast<Char*>(buf), len);
1356
1357          if( !is16bit )
1358          {
1359            for( Int x = 0; x < uiWidth; x++ )
1360            {
1361              pPel[x] = buf[x];
1362            }
1363          }
1364          else
1365          {
1366            for( Int x = 0; x < uiWidth; x++ )
1367            {
1368              pPel[x] = (buf[2*x+1] << 8) | buf[2*x];
1369            }
1370          }
1371     
1372          pPel += uiStride;
1373        }
1374
1375        // read Cr component
1376        pPel = pBLPic->getPicYuvRec()->getCrAddr();
1377        uiStride = pBLPic->getPicYuvRec()->getCStride();
1378        for( Int i = 0; i < uiHeight; i++ )
1379        {
1380          pFile->read(reinterpret_cast<Char*>(buf), len);
1381
1382          if( !is16bit )
1383          {
1384            for( Int x = 0; x < uiWidth; x++ )
1385            {
1386              pPel[x] = buf[x];
1387            }
1388          }
1389          else
1390          {
1391            for( Int x = 0; x < uiWidth; x++ )
1392            {
1393              pPel[x] = (buf[2*x+1] << 8) | buf[2*x];
1394            }
1395          }
1396     
1397          pPel += uiStride;
1398        }
1399
1400        delete[] buf;
1401      }
1402    }
1403#endif
1404
1405#if NO_OUTPUT_OF_PRIOR_PICS
1406    if ( m_layerId == 0 && m_apcSlicePilot->getRapPicFlag() && getNoClrasOutputFlag() )
1407    {
1408      for (UInt i = 0; i < m_apcSlicePilot->getVPS()->getMaxLayers(); i++)
1409      {
1410        m_ppcTDecTop[i]->setLayerInitializedFlag(false);
1411        m_ppcTDecTop[i]->setFirstPicInLayerDecodedFlag(false);
1412      }
1413    }
1414#endif
1415    // Buffer initialize for prediction.
1416    m_cPrediction.initTempBuff();
1417#if ALIGNED_BUMPING
1418    m_apcSlicePilot->checkLeadingPictureRestrictions(m_cListPic);
1419#else
1420    m_apcSlicePilot->applyReferencePictureSet(m_cListPic, m_apcSlicePilot->getRPS());
1421#endif
1422    //  Get a new picture buffer
1423    xGetNewPicBuffer (m_apcSlicePilot, pcPic);
1424
1425    Bool isField = false;
1426    Bool isTff = false;
1427   
1428    if(!m_SEIs.empty())
1429    {
1430      // Check if any new Picture Timing SEI has arrived
1431      SEIMessages pictureTimingSEIs = extractSeisByType (m_SEIs, SEI::PICTURE_TIMING);
1432      if (pictureTimingSEIs.size()>0)
1433      {
1434        SEIPictureTiming* pictureTiming = (SEIPictureTiming*) *(pictureTimingSEIs.begin());
1435        isField = (pictureTiming->m_picStruct == 1) || (pictureTiming->m_picStruct == 2);
1436        isTff =  (pictureTiming->m_picStruct == 1);
1437      }
1438    }
1439   
1440    //Set Field/Frame coding mode
1441    m_pcPic->setField(isField);
1442    m_pcPic->setTopField(isTff);
1443
1444    // transfer any SEI messages that have been received to the picture
1445    pcPic->setSEIs(m_SEIs);
1446    m_SEIs.clear();
1447
1448    // Recursive structure
1449    m_cCuDecoder.create ( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight );
1450#if SVC_EXTENSION
1451    m_cCuDecoder.init   ( m_ppcTDecTop,&m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction, curLayerId );
1452#else
1453    m_cCuDecoder.init   ( &m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction );
1454#endif
1455    m_cTrQuant.init     ( g_uiMaxCUWidth, g_uiMaxCUHeight, m_apcSlicePilot->getSPS()->getMaxTrSize());
1456
1457    m_cSliceDecoder.create();
1458  }
1459  else
1460  {
1461    // Check if any new SEI has arrived
1462    if(!m_SEIs.empty())
1463    {
1464      // Currently only decoding Unit SEI message occurring between VCL NALUs copied
1465      SEIMessages &picSEI = pcPic->getSEIs();
1466      SEIMessages decodingUnitInfos = extractSeisByType (m_SEIs, SEI::DECODING_UNIT_INFO);
1467      picSEI.insert(picSEI.end(), decodingUnitInfos.begin(), decodingUnitInfos.end());
1468      deleteSEIs(m_SEIs);
1469    }
1470  }
1471 
1472  //  Set picture slice pointer
1473  TComSlice*  pcSlice = m_apcSlicePilot;
1474  Bool bNextSlice     = pcSlice->isNextSlice();
1475
1476  UInt uiCummulativeTileWidth;
1477  UInt uiCummulativeTileHeight;
1478  UInt i, j, p;
1479
1480  //set NumColumnsMins1 and NumRowsMinus1
1481  pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getPPS()->getNumColumnsMinus1() );
1482  pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getPPS()->getNumRowsMinus1() );
1483
1484  //create the TComTileArray
1485  pcPic->getPicSym()->xCreateTComTileArray();
1486
1487  if( pcSlice->getPPS()->getUniformSpacingFlag() )
1488  {
1489    //set the width for each tile
1490    for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
1491    {
1492      for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++)
1493      {
1494        pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->
1495          setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1) 
1496          - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) );
1497      }
1498    }
1499
1500    //set the height for each tile
1501    for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
1502    {
1503      for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++)
1504      {
1505        pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->
1506          setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1) 
1507          - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) );   
1508      }
1509    }
1510  }
1511  else
1512  {
1513    //set the width for each tile
1514    for(j=0; j < pcSlice->getPPS()->getNumRowsMinus1()+1; j++)
1515    {
1516      uiCummulativeTileWidth = 0;
1517      for(i=0; i < pcSlice->getPPS()->getNumColumnsMinus1(); i++)
1518      {
1519        pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcSlice->getPPS()->getColumnWidth(i) );
1520        uiCummulativeTileWidth += pcSlice->getPPS()->getColumnWidth(i);
1521      }
1522      pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth );
1523    }
1524
1525    //set the height for each tile
1526    for(j=0; j < pcSlice->getPPS()->getNumColumnsMinus1()+1; j++)
1527    {
1528      uiCummulativeTileHeight = 0;
1529      for(i=0; i < pcSlice->getPPS()->getNumRowsMinus1(); i++)
1530      { 
1531        pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcSlice->getPPS()->getRowHeight(i) );
1532        uiCummulativeTileHeight += pcSlice->getPPS()->getRowHeight(i);
1533      }
1534      pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight );
1535    }
1536  }
1537
1538  pcPic->getPicSym()->xInitTiles();
1539
1540  //generate the Coding Order Map and Inverse Coding Order Map
1541  UInt uiEncCUAddr;
1542  for(i=0, uiEncCUAddr=0; i<pcPic->getPicSym()->getNumberOfCUsInFrame(); i++, uiEncCUAddr = pcPic->getPicSym()->xCalculateNxtCUAddr(uiEncCUAddr))
1543  {
1544    pcPic->getPicSym()->setCUOrderMap(i, uiEncCUAddr);
1545    pcPic->getPicSym()->setInverseCUOrderMap(uiEncCUAddr, i);
1546  }
1547  pcPic->getPicSym()->setCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
1548  pcPic->getPicSym()->setInverseCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
1549
1550  //convert the start and end CU addresses of the slice and dependent slice into encoding order
1551  pcSlice->setSliceSegmentCurStartCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurStartCUAddr()) );
1552  pcSlice->setSliceSegmentCurEndCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurEndCUAddr()) );
1553  if(pcSlice->isNextSlice())
1554  {
1555    pcSlice->setSliceCurStartCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurStartCUAddr()));
1556    pcSlice->setSliceCurEndCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurEndCUAddr()));
1557  }
1558
1559  if (m_bFirstSliceInPicture) 
1560  {
1561    if(pcPic->getNumAllocatedSlice() != 1)
1562    {
1563      pcPic->clearSliceBuffer();
1564    }
1565  }
1566  else
1567  {
1568    pcPic->allocateNewSlice();
1569  }
1570  assert(pcPic->getNumAllocatedSlice() == (m_uiSliceIdx + 1));
1571  m_apcSlicePilot = pcPic->getPicSym()->getSlice(m_uiSliceIdx); 
1572  pcPic->getPicSym()->setSlice(pcSlice, m_uiSliceIdx);
1573
1574  pcPic->setTLayer(nalu.m_temporalId);
1575
1576#if SVC_EXTENSION
1577  pcPic->setLayerId(nalu.m_layerId);
1578  pcSlice->setLayerId(nalu.m_layerId);
1579  pcSlice->setPic(pcPic);
1580#endif
1581
1582  if (bNextSlice)
1583  {
1584    pcSlice->checkCRA(pcSlice->getRPS(), m_pocCRA, m_associatedIRAPType, m_cListPic );
1585    // Set reference list
1586#if SVC_EXTENSION
1587    if (m_layerId == 0)
1588#endif
1589    pcSlice->setRefPicList( m_cListPic, true );
1590
1591#if SVC_EXTENSION
1592    // Create upsampling reference layer pictures for all possible dependent layers and do it only once for the first slice.
1593    // Other slices might choose which reference pictures to be used for inter-layer prediction
1594    if( m_layerId > 0 && m_uiSliceIdx == 0 )
1595    {     
1596#if M0040_ADAPTIVE_RESOLUTION_CHANGE
1597      if( !pcSlice->getVPS()->getSingleLayerForNonIrapFlag() || ( pcSlice->getVPS()->getSingleLayerForNonIrapFlag() && pcSlice->isIRAP() ) )
1598#endif
1599      for( i = 0; i < pcSlice->getNumILRRefIdx(); i++ )
1600      {
1601        UInt refLayerIdc = i;
1602        UInt refLayerId = pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc);
1603#if AVC_BASE
1604        if( refLayerId == 0 && m_parameterSetManagerDecoder.getActiveVPS()->getAvcBaseLayerFlag() )
1605        {         
1606          TComPic* pic = *m_ppcTDecTop[0]->getListPic()->begin();
1607
1608          if( pic )
1609          {
1610            pcSlice->setBaseColPic ( refLayerIdc, pic );
1611          }
1612          else
1613          {
1614            continue;
1615          }
1616#if AVC_SYNTAX
1617          TComPic* pBLPic = pcSlice->getBaseColPic(refLayerIdc);
1618          if( pcSlice->getPOC() == 0 )
1619          {
1620            // initialize partition order.
1621            UInt* piTmp = &g_auiZscanToRaster[0];
1622            initZscanToRaster( pBLPic->getPicSym()->getMaxDepth() + 1, 1, 0, piTmp );
1623            initRasterToZscan( pBLPic->getPicSym()->getMaxCUWidth(), pBLPic->getPicSym()->getMaxCUHeight(), pBLPic->getPicSym()->getMaxDepth() + 1 );
1624          }     
1625          pBLPic->getSlice( 0 )->initBaseLayerRPL( pcSlice );
1626          pBLPic->readBLSyntax( m_ppcTDecTop[0]->getBLSyntaxFile(), SYNTAX_BYTES );
1627#endif
1628        }
1629        else
1630        {
1631#if VPS_EXTN_DIRECT_REF_LAYERS
1632          TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1633#else
1634          TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1635#endif
1636          TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1637          if( !pcSlice->setBaseColPic ( *cListPic, refLayerIdc ) )
1638          {
1639            continue;
1640          }
1641        }
1642#else
1643#if VPS_EXTN_DIRECT_REF_LAYERS
1644        TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1645#else
1646        TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1647#endif
1648        TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1649        pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1650#endif
1651
1652#if O0098_SCALED_REF_LAYER_ID
1653        const Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindowForLayer(refLayerId);
1654#else
1655        const Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc);
1656#endif
1657
1658        Int widthBL   = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getWidth();
1659        Int heightBL  = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getHeight();
1660#if Q0200_CONFORMANCE_BL_SIZE
1661        Int chromaFormatIdc = pcSlice->getBaseColPic(refLayerIdc)->getSlice(0)->getChromaFormatIdc();
1662        const Window &confBL = pcSlice->getBaseColPic(refLayerIdc)->getConformanceWindow(); 
1663        widthBL  -= ( confBL.getWindowLeftOffset() + confBL.getWindowRightOffset() ) * TComSPS::getWinUnitX( chromaFormatIdc );
1664        heightBL -= ( confBL.getWindowTopOffset() + confBL.getWindowBottomOffset() ) * TComSPS::getWinUnitY( chromaFormatIdc );
1665#endif
1666        Int widthEL   = pcPic->getPicYuvRec()->getWidth()  - scalEL.getWindowLeftOffset() - scalEL.getWindowRightOffset();
1667        Int heightEL  = pcPic->getPicYuvRec()->getHeight() - scalEL.getWindowTopOffset()  - scalEL.getWindowBottomOffset();
1668
1669        g_mvScalingFactor[refLayerIdc][0] = widthEL  == widthBL  ? 4096 : Clip3(-4096, 4095, ((widthEL  << 8) + (widthBL  >> 1)) / widthBL);
1670        g_mvScalingFactor[refLayerIdc][1] = heightEL == heightBL ? 4096 : Clip3(-4096, 4095, ((heightEL << 8) + (heightBL >> 1)) / heightBL);
1671
1672        g_posScalingFactor[refLayerIdc][0] = ((widthBL  << 16) + (widthEL  >> 1)) / widthEL;
1673        g_posScalingFactor[refLayerIdc][1] = ((heightBL << 16) + (heightEL >> 1)) / heightEL;
1674
1675#if Q0048_CGS_3D_ASYMLUT
1676        TComPicYuv* pBaseColRec = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec();
1677        if( pcSlice->getPPS()->getCGSFlag() )
1678        {
1679          if(!m_pColorMappedPic)
1680          {
1681            initAsymLut(pcSlice->getBaseColPic(refLayerIdc)->getSlice(0));
1682          }
1683          m_c3DAsymLUTPPS.colorMapping( pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(),  m_pColorMappedPic );
1684          pBaseColRec = m_pColorMappedPic;
1685        }
1686#endif
1687#if SVC_UPSAMPLING
1688        if( pcPic->isSpatialEnhLayer(refLayerIdc) )
1689        {
1690          // check for the sample prediction picture type
1691          if( m_ppcTDecTop[m_layerId]->getSamplePredEnabledFlag(refLayerId) )
1692          {
1693#if O0215_PHASE_ALIGNMENT
1694#if O0194_JOINT_US_BITSHIFT
1695#if Q0048_CGS_3D_ASYMLUT
1696            m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pBaseColRec, pcPic->getPicYuvRec(), pcSlice->getVPS()->getPhaseAlignFlag() );
1697#else
1698            m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), pcSlice->getVPS()->getPhaseAlignFlag() );
1699#endif
1700#else
1701#if Q0048_CGS_3D_ASYMLUT
1702            m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pBaseColRec, pcPic->getPicYuvRec(), pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc), pcSlice->getVPS()->getPhaseAlignFlag() );
1703#else
1704            m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), scalEL, pcSlice->getVPS()->getPhaseAlignFlag() );
1705#endif
1706#endif
1707#else
1708#if O0194_JOINT_US_BITSHIFT
1709#if Q0048_CGS_3D_ASYMLUT
1710            m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pBaseColRec, pcPic->getPicYuvRec(), scalEL );
1711#else
1712            m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), scalEL );
1713#endif
1714#else
1715#if Q0048_CGS_3D_ASYMLUT
1716            m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pBaseColRec, pcPic->getPicYuvRec(), scalEL );
1717#else
1718            m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), scalEL );
1719#endif
1720#endif
1721#endif
1722          }
1723        }
1724        else
1725        {
1726          pcPic->setFullPelBaseRec( refLayerIdc, pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec() );
1727        }
1728        pcSlice->setFullPelBaseRec ( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc) );
1729#endif
1730      }
1731    }
1732
1733    if( m_layerId > 0 && pcSlice->getActiveNumILRRefIdx() )
1734    {
1735      for( i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
1736      {
1737        UInt refLayerIdc = pcSlice->getInterLayerPredLayerIdc(i);
1738#if AVC_BASE
1739        if( pcSlice->getVPS()->getRefLayerId( m_layerId, refLayerIdc ) == 0 && m_parameterSetManagerDecoder.getActiveVPS()->getAvcBaseLayerFlag() )
1740        {
1741          pcSlice->setBaseColPic ( refLayerIdc, *m_ppcTDecTop[0]->getListPic()->begin() );
1742#if AVC_SYNTAX
1743          TComPic* pBLPic = pcSlice->getBaseColPic(refLayerIdc);
1744          if( pcSlice->getPOC() == 0 )
1745          {
1746            // initialize partition order.
1747            UInt* piTmp = &g_auiZscanToRaster[0];
1748            initZscanToRaster( pBLPic->getPicSym()->getMaxDepth() + 1, 1, 0, piTmp );
1749            initRasterToZscan( pBLPic->getPicSym()->getMaxCUWidth(), pBLPic->getPicSym()->getMaxCUHeight(), pBLPic->getPicSym()->getMaxDepth() + 1 );
1750          }     
1751          pBLPic->getSlice( 0 )->initBaseLayerRPL( pcSlice );
1752          pBLPic->readBLSyntax( m_ppcTDecTop[0]->getBLSyntaxFile(), SYNTAX_BYTES );
1753#endif
1754        }
1755        else
1756        {
1757#if VPS_EXTN_DIRECT_REF_LAYERS
1758          TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1759#else
1760          TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1761#endif
1762          TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1763          pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1764        }
1765#else
1766#if VPS_EXTN_DIRECT_REF_LAYERS
1767        TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1768#else
1769        TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1770#endif
1771        TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1772        pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1773#endif
1774
1775        pcSlice->setFullPelBaseRec ( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc) );
1776      }
1777
1778      pcSlice->setILRPic( m_cIlpPic );
1779
1780#if REF_IDX_MFM
1781      if( pcSlice->getMFMEnabledFlag() )
1782      {
1783        pcSlice->setRefPOCListILP(m_ppcTDecTop[m_layerId]->m_cIlpPic, pcSlice->getBaseColPic());
1784      }
1785      pcSlice->setRefPicList( m_cListPic, false, m_cIlpPic);
1786    }
1787#if M0040_ADAPTIVE_RESOLUTION_CHANGE
1788    else if ( m_layerId > 0 )
1789    {
1790      pcSlice->setRefPicList( m_cListPic, false, NULL);
1791    }
1792#endif
1793#if MFM_ENCCONSTRAINT
1794    if( pcSlice->getMFMEnabledFlag() )
1795    {
1796      TComPic* refPic = pcSlice->getRefPic( pcSlice->getSliceType() == B_SLICE ? ( RefPicList )( 1 - pcSlice->getColFromL0Flag() ) : REF_PIC_LIST_0 , pcSlice->getColRefIdx() );
1797
1798      assert( refPic );
1799
1800      Int refLayerId = refPic->getLayerId();
1801
1802      if( refLayerId != pcSlice->getLayerId() )
1803      {
1804        TComPic* pColBasePic = pcSlice->getBaseColPic( *m_ppcTDecTop[refLayerId]->getListPic() );
1805        assert( pColBasePic->checkSameRefInfo() == true );
1806      }
1807    }
1808#endif
1809#endif
1810   
1811#if N0147_IRAP_ALIGN_FLAG
1812    if( m_layerId > 0 && pcSlice->getVPS()->getCrossLayerIrapAlignFlag() )
1813    {
1814#if M0040_ADAPTIVE_RESOLUTION_CHANGE
1815      if( !pcSlice->getVPS()->getSingleLayerForNonIrapFlag() || ( pcSlice->getVPS()->getSingleLayerForNonIrapFlag() && pcSlice->isIRAP() ) )
1816#endif
1817      for(Int dependentLayerIdx = 0; dependentLayerIdx < pcSlice->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1818      {
1819        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1820        TComPic* refpicLayer = pcSlice->getRefPic(*cListPic, pcSlice->getPOC() );
1821        if(refpicLayer && pcSlice->isIRAP())
1822        {                 
1823          assert(pcSlice->getNalUnitType() == refpicLayer->getSlice(0)->getNalUnitType());
1824        }
1825      }
1826    }
1827#endif
1828
1829    if( m_layerId > 0 && !pcSlice->isIntra() && pcSlice->getEnableTMVPFlag() )
1830    {
1831      TComPic* refPic = pcSlice->getRefPic(RefPicList(1 - pcSlice->getColFromL0Flag()), pcSlice->getColRefIdx());
1832
1833      assert( refPic );
1834
1835      // It is a requirement of bitstream conformance when the collocated picture, used for temporal motion vector prediction, is an inter-layer reference picture,
1836      // VpsInterLayerMotionPredictionEnabled[ LayerIdxInVps[ currLayerId ] ][ LayerIdxInVps[ rLId ] ] shall be equal to 1, where rLId is set equal to nuh_layer_id of the inter-layer picture.
1837      if( refPic->isILR(pcSlice->getLayerId()) )
1838      {
1839        assert( m_ppcTDecTop[m_layerId]->getMotionPredEnabledFlag(refPic->getLayerId()) );
1840      }
1841    }
1842#endif //SVC_EXTENSION
1843   
1844    // For generalized B
1845    // note: maybe not existed case (always L0 is copied to L1 if L1 is empty)
1846    if (pcSlice->isInterB() && pcSlice->getNumRefIdx(REF_PIC_LIST_1) == 0)
1847    {
1848      Int iNumRefIdx = pcSlice->getNumRefIdx(REF_PIC_LIST_0);
1849      pcSlice->setNumRefIdx        ( REF_PIC_LIST_1, iNumRefIdx );
1850
1851      for (Int iRefIdx = 0; iRefIdx < iNumRefIdx; iRefIdx++)
1852      {
1853        pcSlice->setRefPic(pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx), REF_PIC_LIST_1, iRefIdx);
1854      }
1855    }
1856    if (!pcSlice->isIntra())
1857    {
1858      Bool bLowDelay = true;
1859      Int  iCurrPOC  = pcSlice->getPOC();
1860      Int iRefIdx = 0;
1861
1862      for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
1863      {
1864        if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
1865        {
1866          bLowDelay = false;
1867        }
1868      }
1869      if (pcSlice->isInterB())
1870      {
1871        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
1872        {
1873          if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
1874          {
1875            bLowDelay = false;
1876          }
1877        }       
1878      }
1879
1880      pcSlice->setCheckLDC(bLowDelay);           
1881    }
1882
1883    //---------------
1884    pcSlice->setRefPOCList();
1885  }
1886
1887  pcPic->setCurrSliceIdx(m_uiSliceIdx);
1888  if(pcSlice->getSPS()->getScalingListFlag())
1889  {
1890    pcSlice->setScalingList ( pcSlice->getSPS()->getScalingList()  );
1891    if(pcSlice->getPPS()->getScalingListPresentFlag())
1892    {
1893      pcSlice->setScalingList ( pcSlice->getPPS()->getScalingList()  );
1894    }
1895#if SCALINGLIST_INFERRING
1896    if( m_layerId == 0 || ( m_layerId > 0 && !pcSlice->getPPS()->getInferScalingListFlag() && !pcSlice->getSPS()->getInferScalingListFlag() ) )
1897#endif
1898    if(!pcSlice->getPPS()->getScalingListPresentFlag() && !pcSlice->getSPS()->getScalingListPresentFlag())
1899    {
1900      pcSlice->setDefaultScalingList();
1901    }
1902    m_cTrQuant.setScalingListDec(pcSlice->getScalingList());
1903    m_cTrQuant.setUseScalingList(true);
1904  }
1905  else
1906  {
1907    m_cTrQuant.setFlatScalingList();
1908    m_cTrQuant.setUseScalingList(false);
1909  }
1910
1911  //  Decode a picture
1912  m_cGopDecoder.decompressSlice(nalu.m_Bitstream, pcPic);
1913
1914  m_bFirstSliceInPicture = false;
1915  m_uiSliceIdx++;
1916
1917  return false;
1918}
1919
1920Void TDecTop::xDecodeVPS()
1921{
1922  TComVPS* vps = new TComVPS();
1923 
1924  m_cEntropyDecoder.decodeVPS( vps );
1925  m_parameterSetManagerDecoder.storePrefetchedVPS(vps); 
1926}
1927
1928Void TDecTop::xDecodeSPS()
1929{
1930  TComSPS* sps = new TComSPS();
1931#if SVC_EXTENSION
1932  sps->setLayerId(m_layerId);
1933#if SPS_DPB_PARAMS
1934  m_cEntropyDecoder.decodeSPS( sps ); // it should be removed after macro clean up
1935#else
1936  m_cEntropyDecoder.decodeSPS( sps, &m_parameterSetManagerDecoder );
1937#endif
1938  m_parameterSetManagerDecoder.storePrefetchedSPS(sps);
1939#if !REPN_FORMAT_IN_VPS   // ILRP can only be initialized at activation 
1940  if(m_numLayer>0)
1941  {
1942    xInitILRP(sps);
1943  }
1944#endif
1945#else //SVC_EXTENSION
1946  m_cEntropyDecoder.decodeSPS( sps );
1947  m_parameterSetManagerDecoder.storePrefetchedSPS(sps);
1948#endif //SVC_EXTENSION
1949}
1950
1951Void TDecTop::xDecodePPS(
1952#if Q0048_CGS_3D_ASYMLUT
1953  TCom3DAsymLUT * pc3DAsymLUT
1954#endif
1955  )
1956{
1957  TComPPS* pps = new TComPPS();
1958
1959#if SCALINGLIST_INFERRING
1960  pps->setLayerId( m_layerId );
1961#endif
1962
1963  m_cEntropyDecoder.decodePPS( pps
1964#if Q0048_CGS_3D_ASYMLUT
1965    , pc3DAsymLUT , m_layerId
1966#endif
1967    );
1968  m_parameterSetManagerDecoder.storePrefetchedPPS( pps );
1969
1970  if( pps->getDependentSliceSegmentsEnabledFlag() )
1971  {
1972    Int NumCtx = pps->getEntropyCodingSyncEnabledFlag()?2:1;
1973    m_cSliceDecoder.initCtxMem(NumCtx);
1974    for ( UInt st = 0; st < NumCtx; st++ )
1975    {
1976      TDecSbac* ctx = NULL;
1977      ctx = new TDecSbac;
1978      ctx->init( &m_cBinCABAC );
1979      m_cSliceDecoder.setCtxMem( ctx, st );
1980    }
1981  }
1982}
1983
1984Void TDecTop::xDecodeSEI( TComInputBitstream* bs, const NalUnitType nalUnitType )
1985{
1986#if SVC_EXTENSION
1987  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
1988  {
1989#if RANDOM_ACCESS_SEI_FIX
1990    if (m_prevSliceSkipped) // No need to decode SEI messages of a skipped access unit
1991    {
1992      return;
1993    }
1994#endif
1995#if LAYERS_NOT_PRESENT_SEI
1996    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
1997#else
1998    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1999#endif
2000  }
2001  else
2002  {
2003#if LAYERS_NOT_PRESENT_SEI
2004    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
2005#else
2006    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
2007#endif
2008    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
2009    if (activeParamSets.size()>0)
2010    {
2011      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
2012      m_parameterSetManagerDecoder.applyPrefetchedPS();
2013      assert(seiAps->activeSeqParameterSetId.size()>0);
2014      if( !m_parameterSetManagerDecoder.activateSPSWithSEI( seiAps->activeSeqParameterSetId[0] ) )
2015      {
2016        printf ("Warning SPS activation with Active parameter set SEI failed");
2017      }
2018    }
2019#if Q0074_SEI_COLOR_MAPPING
2020    m_ColorMapping->setColorMapping( m_SEIs );
2021#endif
2022  }
2023#else
2024  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
2025  {
2026#if LAYERS_NOT_PRESENT_SEI
2027    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
2028#else
2029    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
2030#endif
2031  }
2032  else
2033  {
2034#if LAYERS_NOT_PRESENT_SEI
2035    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
2036#else
2037    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
2038#endif
2039    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
2040    if (activeParamSets.size()>0)
2041    {
2042      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
2043      m_parameterSetManagerDecoder.applyPrefetchedPS();
2044      assert(seiAps->activeSeqParameterSetId.size()>0);
2045      if (! m_parameterSetManagerDecoder.activateSPSWithSEI(seiAps->activeSeqParameterSetId[0] ))
2046      {
2047        printf ("Warning SPS activation with Active parameter set SEI failed");
2048      }
2049    }
2050  }
2051#endif
2052}
2053
2054#if SVC_EXTENSION
2055Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC)
2056#else
2057Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay)
2058#endif
2059{
2060  // Initialize entropy decoder
2061  m_cEntropyDecoder.setEntropyDecoder (&m_cCavlcDecoder);
2062  m_cEntropyDecoder.setBitstream      (nalu.m_Bitstream);
2063
2064#if O0137_MAX_LAYERID
2065  // ignore any NAL units with nuh_layer_id == 63
2066  if (nalu.m_layerId == 63 )
2067  { 
2068    return false;
2069  }
2070#endif
2071  switch (nalu.m_nalUnitType)
2072  {
2073    case NAL_UNIT_VPS:
2074#if VPS_NUH_LAYER_ID
2075      assert( nalu.m_layerId == 0 ); // Non-conforming bitstream. The value of nuh_layer_id of VPS NAL unit shall be equal to 0.
2076#endif
2077      xDecodeVPS();
2078#if AVC_BASE
2079      if( m_parameterSetManagerDecoder.getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
2080      {
2081        if( !m_ppcTDecTop[0]->getBLReconFile()->good() )
2082        {
2083          printf( "Base layer YUV input reading error\n" );
2084          exit(EXIT_FAILURE);
2085        }       
2086#if AVC_SYNTAX
2087        if( !m_ppcTDecTop[0]->getBLSyntaxFile()->good() )
2088        {
2089          printf( "Base layer syntax input reading error\n" );
2090          exit(EXIT_FAILURE);
2091        }
2092#endif
2093      }
2094      else
2095      {
2096        TComList<TComPic*> *cListPic = m_ppcTDecTop[0]->getListPic();
2097        cListPic->clear();
2098      }
2099#endif
2100      return false;
2101     
2102    case NAL_UNIT_SPS:
2103      xDecodeSPS();
2104      return false;
2105
2106    case NAL_UNIT_PPS:
2107      xDecodePPS(
2108#if Q0048_CGS_3D_ASYMLUT
2109        &m_c3DAsymLUTPPS
2110#endif
2111        );
2112      return false;
2113     
2114    case NAL_UNIT_PREFIX_SEI:
2115    case NAL_UNIT_SUFFIX_SEI:
2116      xDecodeSEI( nalu.m_Bitstream, nalu.m_nalUnitType );
2117      return false;
2118
2119    case NAL_UNIT_CODED_SLICE_TRAIL_R:
2120    case NAL_UNIT_CODED_SLICE_TRAIL_N:
2121    case NAL_UNIT_CODED_SLICE_TSA_R:
2122    case NAL_UNIT_CODED_SLICE_TSA_N:
2123    case NAL_UNIT_CODED_SLICE_STSA_R:
2124    case NAL_UNIT_CODED_SLICE_STSA_N:
2125    case NAL_UNIT_CODED_SLICE_BLA_W_LP:
2126    case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
2127    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
2128    case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
2129    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
2130    case NAL_UNIT_CODED_SLICE_CRA:
2131    case NAL_UNIT_CODED_SLICE_RADL_N:
2132    case NAL_UNIT_CODED_SLICE_RADL_R:
2133    case NAL_UNIT_CODED_SLICE_RASL_N:
2134    case NAL_UNIT_CODED_SLICE_RASL_R:
2135#if SVC_EXTENSION
2136      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay, curLayerId, bNewPOC);
2137#else
2138      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay);
2139#endif
2140      break;
2141     
2142    case NAL_UNIT_EOS:
2143      m_associatedIRAPType = NAL_UNIT_INVALID;
2144      m_pocCRA = 0;
2145      m_pocRandomAccess = MAX_INT;
2146      m_prevPOC = MAX_INT;
2147      m_bFirstSliceInPicture = true;
2148      m_bFirstSliceInSequence = true;
2149      m_prevSliceSkipped = false;
2150      m_skippedPOC = 0;
2151      return false;
2152     
2153    case NAL_UNIT_ACCESS_UNIT_DELIMITER:
2154      // TODO: process AU delimiter
2155      return false;
2156     
2157    case NAL_UNIT_EOB:
2158#if P0130_EOB
2159      //Check layer id of the nalu. if it is not 0, give a warning message.
2160      if (nalu.m_layerId > 0)
2161      {
2162        printf( "\n\nThis bitstream is ended with EOB NALU that has layer id greater than 0\n" );
2163      }
2164#endif
2165      return false;
2166     
2167    case NAL_UNIT_FILLER_DATA:
2168      return false;
2169     
2170    case NAL_UNIT_RESERVED_VCL_N10:
2171    case NAL_UNIT_RESERVED_VCL_R11:
2172    case NAL_UNIT_RESERVED_VCL_N12:
2173    case NAL_UNIT_RESERVED_VCL_R13:
2174    case NAL_UNIT_RESERVED_VCL_N14:
2175    case NAL_UNIT_RESERVED_VCL_R15:
2176     
2177    case NAL_UNIT_RESERVED_IRAP_VCL22:
2178    case NAL_UNIT_RESERVED_IRAP_VCL23:
2179     
2180    case NAL_UNIT_RESERVED_VCL24:
2181    case NAL_UNIT_RESERVED_VCL25:
2182    case NAL_UNIT_RESERVED_VCL26:
2183    case NAL_UNIT_RESERVED_VCL27:
2184    case NAL_UNIT_RESERVED_VCL28:
2185    case NAL_UNIT_RESERVED_VCL29:
2186    case NAL_UNIT_RESERVED_VCL30:
2187    case NAL_UNIT_RESERVED_VCL31:
2188     
2189    case NAL_UNIT_RESERVED_NVCL41:
2190    case NAL_UNIT_RESERVED_NVCL42:
2191    case NAL_UNIT_RESERVED_NVCL43:
2192    case NAL_UNIT_RESERVED_NVCL44:
2193    case NAL_UNIT_RESERVED_NVCL45:
2194    case NAL_UNIT_RESERVED_NVCL46:
2195    case NAL_UNIT_RESERVED_NVCL47:
2196    case NAL_UNIT_UNSPECIFIED_48:
2197    case NAL_UNIT_UNSPECIFIED_49:
2198    case NAL_UNIT_UNSPECIFIED_50:
2199    case NAL_UNIT_UNSPECIFIED_51:
2200    case NAL_UNIT_UNSPECIFIED_52:
2201    case NAL_UNIT_UNSPECIFIED_53:
2202    case NAL_UNIT_UNSPECIFIED_54:
2203    case NAL_UNIT_UNSPECIFIED_55:
2204    case NAL_UNIT_UNSPECIFIED_56:
2205    case NAL_UNIT_UNSPECIFIED_57:
2206    case NAL_UNIT_UNSPECIFIED_58:
2207    case NAL_UNIT_UNSPECIFIED_59:
2208    case NAL_UNIT_UNSPECIFIED_60:
2209    case NAL_UNIT_UNSPECIFIED_61:
2210    case NAL_UNIT_UNSPECIFIED_62:
2211    case NAL_UNIT_UNSPECIFIED_63:
2212
2213    default:
2214      assert (0);
2215  }
2216
2217  return false;
2218}
2219
2220/** Function for checking if picture should be skipped because of association with a previous BLA picture
2221 * \param iPOCLastDisplay POC of last picture displayed
2222 * \returns true if the picture should be skipped
2223 * This function skips all TFD pictures that follow a BLA picture
2224 * in decoding order and precede it in output order.
2225 */
2226Bool TDecTop::isSkipPictureForBLA(Int& iPOCLastDisplay)
2227{
2228  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) && 
2229       m_apcSlicePilot->getPOC() < m_pocCRA && (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N))
2230  {
2231    iPOCLastDisplay++;
2232    return true;
2233  }
2234  return false;
2235}
2236
2237/** Function for checking if picture should be skipped because of random access
2238 * \param iSkipFrame skip frame counter
2239 * \param iPOCLastDisplay POC of last picture displayed
2240 * \returns true if the picture shold be skipped in the random access.
2241 * This function checks the skipping of pictures in the case of -s option random access.
2242 * All pictures prior to the random access point indicated by the counter iSkipFrame are skipped.
2243 * It also checks the type of Nal unit type at the random access point.
2244 * 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.
2245 * If the random access point is IDR all pictures after the random access point are decoded.
2246 * If the random access point is none of the above, a warning is issues, and decoding of pictures with POC
2247 * equal to or greater than the random access point POC is attempted. For non IDR/CRA/BLA random
2248 * access point there is no guarantee that the decoder will not crash.
2249 */
2250Bool TDecTop::isRandomAccessSkipPicture(Int& iSkipFrame,  Int& iPOCLastDisplay)
2251{
2252  if (iSkipFrame) 
2253  {
2254    iSkipFrame--;   // decrement the counter
2255    return true;
2256  }
2257  else if (m_pocRandomAccess == MAX_INT) // start of random access point, m_pocRandomAccess has not been set yet.
2258  {
2259    if (   m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA
2260        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
2261        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
2262        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL )
2263    {
2264      // set the POC random access since we need to skip the reordered pictures in the case of CRA/CRANT/BLA/BLANT.
2265      m_pocRandomAccess = m_apcSlicePilot->getPOC();
2266    }
2267    else if ( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
2268    {
2269      m_pocRandomAccess = -MAX_INT; // no need to skip the reordered pictures in IDR, they are decodable.
2270    }
2271    else 
2272    {
2273      static Bool warningMessage = false;
2274      if(!warningMessage)
2275      {
2276        printf("\nWarning: this is not a valid random access point and the data is discarded until the first CRA picture");
2277        warningMessage = true;
2278      }
2279      return true;
2280    }
2281  }
2282  // skip the reordered pictures, if necessary
2283  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))
2284  {
2285    iPOCLastDisplay++;
2286    return true;
2287  }
2288  // if we reach here, then the picture is not skipped.
2289  return false; 
2290}
2291
2292#if SVC_EXTENSION
2293#if VPS_EXTN_DIRECT_REF_LAYERS
2294TDecTop* TDecTop::getRefLayerDec( UInt refLayerIdc )
2295{
2296  TComVPS* vps = m_parameterSetManagerDecoder.getActiveVPS();
2297  if( vps->getNumDirectRefLayers( m_layerId ) <= 0 )
2298  {
2299    return (TDecTop *)getLayerDec( 0 );
2300  }
2301 
2302  return (TDecTop *)getLayerDec( vps->getRefLayerId( m_layerId, refLayerIdc ) );
2303}
2304#endif
2305
2306#if VPS_EXTN_DIRECT_REF_LAYERS
2307Void TDecTop::setRefLayerParams( TComVPS* vps )
2308{
2309  for(UInt layer = 0; layer < m_numLayer; layer++)
2310  {
2311    TDecTop *decTop = (TDecTop *)getLayerDec(layer);
2312    decTop->setNumSamplePredRefLayers(0);
2313    decTop->setNumMotionPredRefLayers(0);
2314    decTop->setNumDirectRefLayers(0);
2315    for(Int i = 0; i < MAX_VPS_LAYER_ID_PLUS1; i++)
2316    {
2317      decTop->setSamplePredEnabledFlag(i, false);
2318      decTop->setMotionPredEnabledFlag(i, false);
2319      decTop->setSamplePredRefLayerId(i, 0);
2320      decTop->setMotionPredRefLayerId(i, 0);
2321    }
2322    for(Int j = 0; j < layer; j++)
2323    {
2324      if (vps->getDirectDependencyFlag(layer, j))
2325      {
2326        decTop->setRefLayerId(decTop->getNumDirectRefLayers(), vps->getLayerIdInNuh(layer));
2327        decTop->setNumDirectRefLayers(decTop->getNumDirectRefLayers() + 1);
2328
2329        Int samplePredEnabledFlag = (vps->getDirectDependencyType(layer, j) + 1) & 1;
2330        decTop->setSamplePredEnabledFlag(j, samplePredEnabledFlag == 1 ? true : false);
2331        decTop->setNumSamplePredRefLayers(decTop->getNumSamplePredRefLayers() + samplePredEnabledFlag);
2332
2333        Int motionPredEnabledFlag = ((vps->getDirectDependencyType(layer, j) + 1) & 2) >> 1;
2334        decTop->setMotionPredEnabledFlag(j, motionPredEnabledFlag == 1 ? true : false);
2335        decTop->setNumMotionPredRefLayers(decTop->getNumMotionPredRefLayers() + motionPredEnabledFlag);
2336      }
2337    }
2338  }
2339  for( Int i = 1; i < m_numLayer; i++ )
2340  {
2341    Int mIdx = 0, sIdx = 0;
2342    Int iNuhLId = vps->getLayerIdInNuh(i);
2343    TDecTop *decTop = (TDecTop *)getLayerDec(iNuhLId);
2344    for ( Int j = 0; j < i; j++ )
2345    {
2346      if (decTop->getMotionPredEnabledFlag(j))
2347      {
2348        decTop->setMotionPredRefLayerId(mIdx++, vps->getLayerIdInNuh(j));
2349      }
2350      if (decTop->getSamplePredEnabledFlag(j))
2351      {
2352        decTop->setSamplePredRefLayerId(sIdx++, vps->getLayerIdInNuh(j));
2353      }
2354    }
2355  }
2356}
2357
2358#endif
2359
2360#if OUTPUT_LAYER_SET_INDEX
2361Void TDecTop::checkValueOfTargetOutputLayerSetIdx(TComVPS *vps)
2362{
2363  CommonDecoderParams* params = this->getCommonDecoderParams();
2364
2365  assert( params->getTargetLayerId() < vps->getMaxLayers() );
2366
2367  if( params->getValueCheckedFlag() )
2368  {
2369    return; // Already checked
2370  }
2371  if( params->getTargetOutputLayerSetIdx() == -1 )  // Output layer set index not specified
2372  {
2373    Bool layerSetMatchFound = false;
2374    // Output layer set index not assigned.
2375    // Based on the value of targetLayerId, check if any of the output layer matches
2376    // Currently, the target layer ID in the encoder assumes that all the layers are decoded   
2377    // Check if any of the output layer sets match this description
2378    for(Int i = 0; i < vps->getNumOutputLayerSets(); i++)
2379    {
2380      Bool layerSetMatchFlag = true;
2381      Int layerSetIdx = vps->getOutputLayerSetIdx( i );
2382      if( vps->getNumLayersInIdList( layerSetIdx ) == params->getTargetLayerId() + 1 )
2383      {
2384        for(Int j = 0; j < vps->getNumLayersInIdList( layerSetIdx ); j++)
2385        {
2386          if( vps->getLayerSetLayerIdList( layerSetIdx, j ) != j )
2387          {
2388            layerSetMatchFlag = false;
2389            break;
2390          }
2391        }
2392      }
2393      else
2394      {
2395        layerSetMatchFlag = false;
2396      }
2397     
2398      if( layerSetMatchFlag ) // Potential output layer set candidate found
2399      {
2400        // If target dec layer ID list is also included - check if they match
2401        if( params->getTargetDecLayerIdSet() )
2402        {
2403          if( params->getTargetDecLayerIdSet()->size() ) 
2404          {
2405            for(Int j = 0; j < vps->getNumLayersInIdList( layerSetIdx ); j++)
2406            {
2407              if( *(params->getTargetDecLayerIdSet()->begin() + j) != vps->getLayerIdInNuh(vps->getLayerSetLayerIdList( layerSetIdx, j )))
2408              {
2409                layerSetMatchFlag = false;
2410              }
2411            }
2412          }
2413        }
2414        if( layerSetMatchFlag ) // The target dec layer ID list also matches, if present
2415        {
2416          // Match found
2417          layerSetMatchFound = true;
2418          params->setTargetOutputLayerSetIdx( i );
2419          params->setValueCheckedFlag( true );
2420          break;
2421        }
2422      }
2423    }
2424    assert( layerSetMatchFound ); // No output layer set matched the value of either targetLayerId or targetdeclayerIdlist
2425  }   
2426  else // Output layer set index is assigned - check if the values match
2427  {
2428    // Check if the target decoded layer is the highest layer in the list
2429    assert( params->getTargetOutputLayerSetIdx() < vps->getNumLayerSets() );
2430    Int layerSetIdx = vps->getOutputLayerSetIdx( params->getTargetOutputLayerSetIdx() );  // Index to the layer set
2431    assert( params->getTargetLayerId() == vps->getNumLayersInIdList( layerSetIdx ) - 1);
2432
2433    Bool layerSetMatchFlag = true;
2434    for(Int j = 0; j < vps->getNumLayersInIdList( layerSetIdx ); j++)
2435    {
2436      if( vps->getLayerSetLayerIdList( layerSetIdx, j ) != j )
2437      {
2438        layerSetMatchFlag = false;
2439        break;
2440      }
2441    }
2442
2443    assert(layerSetMatchFlag);    // Signaled output layer set index does not match targetOutputLayerId.
2444   
2445    // Check if the targetdeclayerIdlist matches the output layer set
2446    if( params->getTargetDecLayerIdSet() )
2447    {
2448      if( params->getTargetDecLayerIdSet()->size() ) 
2449      {
2450        for(Int i = 0; i < vps->getNumLayersInIdList( layerSetIdx ); i++)
2451        {
2452          assert( *(params->getTargetDecLayerIdSet()->begin() + i) == vps->getLayerIdInNuh(vps->getLayerSetLayerIdList( layerSetIdx, i )));
2453        }
2454      }
2455    }
2456    params->setValueCheckedFlag( true );
2457
2458  }
2459}
2460#endif
2461#if RESOLUTION_BASED_DPB
2462Void TDecTop::assignSubDpbs(TComVPS *vps)
2463{
2464  if( m_subDpbIdx == -1 ) // Sub-DPB index is not already assigned
2465  {
2466    Int lsIdx = vps->getOutputLayerSetIdx( getCommonDecoderParams()->getTargetOutputLayerSetIdx() );
2467
2468    Int layerIdx = vps->findLayerIdxInLayerSet( lsIdx, getLayerId() );
2469    assert( layerIdx != -1 ); // Current layer should be found in the layer set.
2470
2471    // Copy from the active VPS based on the layer ID.
2472    m_subDpbIdx = vps->getSubDpbAssigned( lsIdx, layerIdx );
2473  }
2474}
2475#endif
2476
2477#if Q0048_CGS_3D_ASYMLUT
2478Void TDecTop::initAsymLut(TComSlice *pcSlice)
2479{
2480  if(m_layerId>0)
2481  {
2482    if(!m_pColorMappedPic)
2483    {
2484      Int picWidth    = pcSlice->getPicWidthInLumaSamples();
2485      Int picHeight   = pcSlice->getPicHeightInLumaSamples();
2486      m_pColorMappedPic = new TComPicYuv;
2487      m_pColorMappedPic->create( picWidth, picHeight, pcSlice->getChromaFormatIdc()/*CHROMA_420*/, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, NULL );
2488    }
2489  }
2490}
2491#endif
2492
2493#endif //SVC_EXTENSION
2494
2495#if Q0074_SEI_COLOR_MAPPING
2496TDecColorMapping::TDecColorMapping()
2497{
2498  m_pcColorMappingPic[0]   = NULL;
2499  m_pcColorMappingPic[1]   = NULL;
2500
2501  m_colorMapCancelFlag  = true;
2502
2503  for( Int i=0 ; i<3 ; i++ )
2504  {
2505    m_lut1d_computed[i]             = false;
2506    m_lut1d_input[i]                = NULL;
2507    m_coded_input_pivot_value[i]    = NULL;
2508    m_target_input_pivot_value[i]   = NULL;
2509  }
2510  for( Int i=0 ; i<3 ; i++ )
2511  {
2512    m_lut1d_output[i]               = NULL;
2513    m_coded_output_pivot_value[i]   = NULL;
2514    m_target_output_pivot_value[i]  = NULL;
2515  }
2516}
2517
2518TDecColorMapping::~TDecColorMapping()
2519{
2520  if ( m_pcColorMappingPic[0] )  delete m_pcColorMappingPic[0];
2521  if ( m_pcColorMappingPic[1] )  delete m_pcColorMappingPic[1];
2522
2523  for( Int i=0 ; i<3 ; i++ )
2524  {
2525    if ( m_lut1d_input[i] )               delete  m_lut1d_input[i];
2526    if ( m_coded_input_pivot_value[i] )   delete  m_coded_input_pivot_value[i];
2527    if ( m_target_input_pivot_value[i] )  delete  m_target_input_pivot_value[i];
2528  }
2529  for( Int i=0 ; i<3 ; i++ )
2530  {
2531    if ( m_lut1d_output[i] )               delete  m_lut1d_output[i];
2532    if ( m_coded_output_pivot_value[i] )   delete  m_coded_output_pivot_value[i];
2533    if ( m_target_output_pivot_value[i] )  delete  m_target_output_pivot_value[i];
2534  }
2535}
2536
2537Void  TDecColorMapping::setColorMapping( SEIMessages m_SEIs )
2538{
2539  SEIMessages colorMappingInfo = getSeisByType(m_SEIs, SEI::COLOR_MAPPING_INFO) ;
2540  SEIColorMappingInfo *seiColorMappingInfo = NULL;
2541  if (colorMappingInfo.size() !=0)
2542  {
2543    seiColorMappingInfo = (SEIColorMappingInfo*)(*colorMappingInfo.begin());
2544
2545    m_colorMapId                              = seiColorMappingInfo->m_colorMapId;
2546    m_colorMapCancelFlag                      = seiColorMappingInfo->m_colorMapCancelFlag;
2547    if( !m_colorMapCancelFlag )
2548    {
2549      m_colorMapPersistenceFlag                 = seiColorMappingInfo->m_colorMapPersistenceFlag;
2550      m_colorMap_video_signal_type_present_flag = seiColorMappingInfo->m_colorMap_video_signal_type_present_flag;
2551      m_colorMap_video_full_range_flag          = seiColorMappingInfo->m_colorMap_video_full_range_flag;
2552      m_colorMap_primaries                      = seiColorMappingInfo->m_colorMap_primaries;
2553      m_colorMap_transfer_characteristics       = seiColorMappingInfo->m_colorMap_transfer_characteristics;
2554      m_colorMap_matrix_coeffs                  = seiColorMappingInfo->m_colorMap_matrix_coeffs;
2555      m_colorMapModelId                         = seiColorMappingInfo->m_colorMapModelId;
2556
2557      m_colour_map_coded_data_bit_depth         = seiColorMappingInfo->m_colour_map_coded_data_bit_depth;
2558      m_colour_map_target_bit_depth             = seiColorMappingInfo->m_colour_map_target_bit_depth;
2559
2560      for( Int i=0 ; i<3 ; i++ )
2561      {
2562        m_num_input_pivots[i]                 = seiColorMappingInfo->m_num_input_pivots[i];
2563        if ( m_coded_input_pivot_value[i] )   delete  m_coded_input_pivot_value[i];
2564        if ( m_target_input_pivot_value[i] )  delete  m_target_input_pivot_value[i];
2565        m_coded_input_pivot_value[i]          = new Int[ m_num_input_pivots[i] ];
2566        m_target_input_pivot_value[i]         = new Int[ m_num_input_pivots[i] ];
2567        for( Int j=0 ; j<m_num_input_pivots[i] ; j++ )
2568        {
2569          m_coded_input_pivot_value[i][j]     = seiColorMappingInfo->m_coded_input_pivot_value[i][j];
2570          m_target_input_pivot_value[i][j]    = seiColorMappingInfo->m_target_input_pivot_value[i][j];
2571        }
2572      }
2573
2574      m_matrix_flag       = seiColorMappingInfo->m_matrix_flag;
2575      m_log2_matrix_denom = m_matrix_flag ? (seiColorMappingInfo->m_log2_matrix_denom) : (0) ;
2576      for( Int i=0 ; i<3 ; i++ )
2577      {
2578        for( Int j=0 ; j<3 ; j++ )
2579        {
2580          m_matrix_coef[i][j]  = seiColorMappingInfo->m_matrix_coef[i][j];
2581        }
2582      }
2583
2584      for( Int i=0 ; i<3 ; i++ )
2585      {
2586        m_num_output_pivots[i]                 = seiColorMappingInfo->m_num_output_pivots[i];
2587        if ( m_coded_output_pivot_value[i] )   delete  m_coded_output_pivot_value[i];
2588        if ( m_target_output_pivot_value[i] )  delete  m_target_output_pivot_value[i];
2589        m_coded_output_pivot_value[i]          = new Int[ m_num_output_pivots[i] ];
2590        m_target_output_pivot_value[i]         = new Int[ m_num_output_pivots[i] ];
2591        for( Int j=0 ; j<m_num_output_pivots[i] ; j++ )
2592        {
2593          m_coded_output_pivot_value[i][j]     = seiColorMappingInfo->m_coded_output_pivot_value[i][j];
2594          m_target_output_pivot_value[i][j]    = seiColorMappingInfo->m_target_output_pivot_value[i][j];
2595        }
2596      }
2597
2598      memset( m_lut1d_computed, 0, sizeof( m_lut1d_computed ) );
2599    }
2600  }
2601
2602}
2603
2604Void  TDecColorMapping::setColorMapping( Int bitDepth, Int iComp )
2605{
2606  if( !m_colorMapCancelFlag && !m_lut1d_computed[iComp] )
2607  {
2608
2609    if ( m_lut1d_input[iComp] )   delete m_lut1d_input[iComp];
2610    if ( m_lut1d_output[iComp] )  delete m_lut1d_output[iComp];
2611
2612    m_lut1d_input[iComp]  = new Int[ 1 << bitDepth ];
2613    m_lut1d_output[iComp] = new Int[ 1 << bitDepth ];
2614
2615    Int iShift      = (m_colour_map_coded_data_bit_depth >= bitDepth) ? (m_colour_map_coded_data_bit_depth - bitDepth) : (0);
2616    Int iShiftPivot = (m_colour_map_coded_data_bit_depth >= bitDepth) ? (0) : (bitDepth - m_colour_map_coded_data_bit_depth);
2617
2618    for( Int k=0 ; k<(1<<bitDepth) ; k++ )
2619    {
2620      Int iSample = k << iShift ;
2621      if( m_num_input_pivots[iComp] > 1 )
2622      {
2623        for( Int iPivot=0 ; iPivot<m_num_input_pivots[iComp] ; iPivot++ )
2624        {
2625          Int iCodedPrev  = m_coded_input_pivot_value[iComp][iPivot]     << iShiftPivot;
2626          Int iCodedNext  = m_coded_input_pivot_value[iComp][iPivot+1]   << iShiftPivot;
2627          Int iTargetPrev = m_target_input_pivot_value[iComp][iPivot]    << iShiftPivot;
2628          Int iTargetNext = m_target_input_pivot_value[iComp][iPivot+1]  << iShiftPivot;
2629          if ( iCodedPrev <= iSample && iSample < iCodedNext )
2630          {
2631            Float fInterpol = (Float)( (iCodedNext - iSample)*iTargetPrev + (iSample - iCodedPrev)*iTargetNext ) / (Float)(iCodedNext - iCodedPrev) ;
2632            m_lut1d_input[iComp][k]  = (Int)( 0.5 + fInterpol );
2633            iPivot = m_num_input_pivots[iComp]; // stop
2634          }
2635        }
2636      }
2637      else
2638      {
2639        m_lut1d_input[iComp][k]  = k;
2640      }
2641    }
2642
2643    iShift      = ( (m_colour_map_coded_data_bit_depth >= bitDepth) ? (m_colour_map_coded_data_bit_depth - bitDepth) : (0) );
2644    Int iOffset = iShift ? (1 << (iShift - 1)) : (0) ;
2645    iShiftPivot = (m_colour_map_target_bit_depth >= bitDepth) ? (0) : (bitDepth - m_colour_map_target_bit_depth) ;
2646    for( Int k=0 ; k<(1<<bitDepth) ; k++ )
2647    {
2648      Int iSample = k << iShift;
2649      if ( m_num_output_pivots[iComp]>1 )
2650      {
2651        for( Int iPivot=0 ; iPivot<m_num_output_pivots[iComp] ; iPivot++ )
2652        {
2653          Int iCodedPrev  = m_coded_output_pivot_value[iComp][iPivot]     << iShiftPivot;
2654          Int iCodedNext  = m_coded_output_pivot_value[iComp][iPivot+1]   << iShiftPivot;
2655          Int iTargetPrev = m_target_output_pivot_value[iComp][iPivot]    << iShiftPivot;
2656          Int iTargetNext = m_target_output_pivot_value[iComp][iPivot+1]  << iShiftPivot;
2657          if ( iCodedPrev <= iSample && iSample < iCodedNext )
2658          {
2659            Float fInterpol =  (Float)( (iCodedNext - iSample)*iTargetPrev + (iSample - iCodedPrev)*iTargetNext ) / (Float)(iCodedNext - iCodedPrev) ;
2660            m_lut1d_output[iComp][k]  = ( (Int)(0.5 + fInterpol) + iOffset ) >> iShift ;
2661            iPivot = m_num_output_pivots[iComp]; // stop
2662          }
2663        }
2664      }
2665      else
2666      {
2667        m_lut1d_output[iComp][k]  = k;
2668      }
2669    }
2670
2671    m_lut1d_computed[iComp] = true;
2672  }
2673
2674}
2675
2676TComPicYuv* TDecColorMapping::getColorMapping( TComPicYuv* pPicYuvRec, Int iTop, Int curlayerId )
2677{
2678  if( !m_colorMapCancelFlag )
2679  {
2680    if( !m_pcColorMappingPic[iTop] )
2681    {
2682      m_pcColorMappingPic[iTop] = new TComPicYuv;
2683#if SVC_EXTENSION
2684      m_pcColorMappingPic[iTop]->create( pPicYuvRec->getWidth(), pPicYuvRec->getHeight(), pPicYuvRec->getChromaFormat(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
2685#else
2686      m_pcColorMappingPic[iTop]->create( pPicYuvRec->getWidth(), pPicYuvRec->getHeight(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
2687#endif
2688    }
2689
2690    Int iHeight   = pPicYuvRec->getHeight();
2691    Int iWidth    = pPicYuvRec->getWidth();
2692    Int iStride   = pPicYuvRec->getStride();
2693    Int iCStride  = pPicYuvRec->getCStride();
2694
2695    Pel* Lum0   = pPicYuvRec->getLumaAddr();
2696    Pel* Cb0    = pPicYuvRec->getCbAddr();
2697    Pel* Cr0    = pPicYuvRec->getCrAddr();
2698    Pel* Lum1   = m_pcColorMappingPic[iTop]->getLumaAddr();
2699    Pel* Cb1    = m_pcColorMappingPic[iTop]->getCbAddr();
2700    Pel* Cr1    = m_pcColorMappingPic[iTop]->getCrAddr();
2701
2702#if SVC_EXTENSION
2703    Int bitDepthY = g_bitDepthYLayer[curlayerId];
2704    Int bitDepthC = g_bitDepthCLayer[curlayerId];
2705
2706    assert( g_bitDepthY == bitDepthY );
2707    assert( g_bitDepthC == bitDepthC );
2708#else
2709    Int bitDepthY = g_bitDepthY;
2710    Int bitDepthC = g_bitDepthC;
2711#endif
2712
2713    Int iYShift = (m_colour_map_target_bit_depth >= bitDepthY) ? (m_colour_map_target_bit_depth - bitDepthY) : (0) ;
2714    Int iCShift = (m_colour_map_target_bit_depth >= bitDepthC) ? (m_colour_map_target_bit_depth - bitDepthC) : (0) ;
2715    Int offsetY = (1 << (m_log2_matrix_denom+iYShift - 1));
2716    Int offsetC = (1 << (m_log2_matrix_denom+iCShift - 1));
2717
2718    Int   cShift  = 1 ;
2719
2720    //Pel*  LumPrev0 = Lum0;
2721    for( Int y = 0; y < iHeight ; y++ )
2722    {
2723      Bool  bDoChroma = (y % 2);
2724      for( Int x = 0; x < iWidth ; x++ )
2725      {
2726        Int s1Y = m_lut1d_input[0][ Lum0[x]   ];
2727        Int s1U = m_lut1d_input[1][ Cb0[x>>1] ];
2728        Int s1V = m_lut1d_input[2][ Cr0[x>>1] ];
2729
2730        Int s2Y, s2U, s2V;
2731        if( m_matrix_flag )
2732        {
2733          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 );
2734          //s2Y = ClipBD( s2Y , bitDepthY );
2735          s2Y = ClipY( s2Y );
2736          Lum1[x]   = m_lut1d_output[0][ s2Y ];
2737        }
2738        else
2739        {
2740          s1Y       = ( s1Y + offsetY ) >> iYShift ;
2741          //s1Y = ClipBD( s1Y , bitDepthY );
2742          s1Y = ClipY( s1Y );
2743          Lum1[x]   = m_lut1d_output[0][ s1Y ];
2744        }
2745
2746        if( bDoChroma && (x%2) )
2747        {
2748          if( m_matrix_flag )
2749          {
2750            //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 ;
2751            //s1Y = m_lut1d_input[0][ (Lum0[x] + Lum0[x+1] + LumPrev0[x] + LumPrev0[x+1] + 2)>>2 ] ;
2752            s1Y = m_lut1d_input[0][ Lum0[x] ];
2753
2754            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 ) ;
2755            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 ) ;
2756            //s2U = ClipBD( s2U , bitDepthC );
2757            //s2V = ClipBD( s2V , bitDepthC );
2758            s2U = ClipC( s2U );
2759            s2V = ClipC( s2V );
2760            Cb1[x>>cShift] = m_lut1d_output[1][ s2U ];
2761            Cr1[x>>cShift] = m_lut1d_output[2][ s2V ];
2762          }
2763          else
2764          {
2765            s1U       = ( s1U + offsetC ) >> iCShift ;
2766            s1V       = ( s1V + offsetC ) >> iCShift ;
2767            //s1U = ClipBD( s1U , bitDepthC );
2768            //s1V = ClipBD( s1V , bitDepthC );
2769            s1U = ClipC( s1U );
2770            s1V = ClipC( s1V );
2771            Cb1[x>>cShift] = m_lut1d_output[1][ s1U ];
2772            Cr1[x>>cShift] = m_lut1d_output[2][ s1V ];
2773          }
2774        }
2775
2776      }
2777
2778      //LumPrev0 = Lum0;
2779      Lum0  += iStride;
2780      Lum1  += iStride;
2781      if( bDoChroma )
2782      {
2783        Cb0 += iCStride;
2784        Cr0 += iCStride;
2785        Cb1 += iCStride;
2786        Cr1 += iCStride;
2787      }
2788    }
2789
2790    return m_pcColorMappingPic[iTop];
2791  }
2792
2793  return pPicYuvRec; 
2794}
2795#endif
2796
2797//! \}
Note: See TracBrowser for help on using the repository browser.