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

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

remove macro ALIGN_TSA_STSA_PICS

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