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

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

remove macro O0223_O0139_IRAP_ALIGN_NO_CONTRAINTS

  • 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#if ALIGN_TSA_STSA_PICS
1176  if( m_apcSlicePilot->getLayerId() > 0 )
1177  {
1178    // Check for TSA alignment
1179    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
1180        m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R
1181         )
1182    {
1183      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1184      {
1185        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1186        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() );
1187        if( refpicLayer )
1188        {
1189          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
1190                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R );    // TSA pictures should be aligned among depenedent layers
1191        } 
1192      }
1193    }
1194    // Check for STSA alignment
1195    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
1196         m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R
1197         )
1198    {
1199      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1200      {
1201        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1202        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() ); // STSA pictures should be aligned among dependent layers
1203        if( refpicLayer )
1204
1205        {
1206          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
1207                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R );
1208        }
1209      }
1210    }
1211  }
1212#endif
1213#else //SVC_EXTENSION
1214  //we should only get a different poc for a new picture (with CTU address==0)
1215  if (m_apcSlicePilot->isNextSlice() && m_apcSlicePilot->getPOC()!=m_prevPOC && !m_bFirstSliceInSequence && (m_apcSlicePilot->getSliceCurStartCUAddr()!=0))
1216  {
1217    printf ("Warning, the first slice of a picture might have been lost!\n");
1218  }
1219  // exit when a new picture is found
1220  if (m_apcSlicePilot->isNextSlice() && (m_apcSlicePilot->getSliceCurStartCUAddr() == 0 && !m_bFirstSliceInPicture) && !m_bFirstSliceInSequence )
1221  {
1222    if (m_prevPOC >= m_pocRandomAccess)
1223    {
1224      m_prevPOC = m_apcSlicePilot->getPOC();
1225      return true;
1226    }
1227    m_prevPOC = m_apcSlicePilot->getPOC();
1228  }
1229
1230  // actual decoding starts here
1231  xActivateParameterSets();
1232
1233  if (m_apcSlicePilot->isNextSlice()) 
1234  {
1235    m_prevPOC = m_apcSlicePilot->getPOC();
1236  }
1237  m_bFirstSliceInSequence = false;
1238#endif //SVC_EXTENSION
1239  //detect lost reference picture and insert copy of earlier frame.
1240  Int lostPoc;
1241  while((lostPoc=m_apcSlicePilot->checkThatAllRefPicsAreAvailable(m_cListPic, m_apcSlicePilot->getRPS(), true, m_pocRandomAccess)) > 0)
1242  {
1243    xCreateLostPicture(lostPoc-1);
1244  }
1245  if (m_bFirstSliceInPicture)
1246  {
1247#if AVC_BASE
1248    if( m_layerId == 1 && m_parameterSetManagerDecoder.getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
1249    {
1250      TComPic* pBLPic = (*m_ppcTDecTop[0]->getListPic()->begin());
1251      pBLPic->getSlice(0)->setReferenced(true);
1252      fstream* pFile  = m_ppcTDecTop[0]->getBLReconFile();
1253
1254      if( pFile->good() )
1255      {
1256        Bool is16bit  = g_bitDepthYLayer[0] > 8 || g_bitDepthCLayer[0] > 8;
1257        UInt uiWidth  = pBLPic->getPicYuvRec()->getWidth();
1258        UInt uiHeight = pBLPic->getPicYuvRec()->getHeight();
1259
1260        Int len = uiWidth * (is16bit ? 2 : 1);
1261        UChar *buf = new UChar[len];
1262
1263        UInt64 uiPos = (UInt64) m_apcSlicePilot->getPOC() * uiWidth * uiHeight * 3 / 2;
1264        if( is16bit )
1265        {
1266            uiPos <<= 1;
1267        }
1268
1269        pFile->seekg((UInt)uiPos, ios::beg );
1270
1271        // read Y component
1272        Pel* pPel = pBLPic->getPicYuvRec()->getLumaAddr();
1273        UInt uiStride = pBLPic->getPicYuvRec()->getStride();
1274        for( Int i = 0; i < uiHeight; i++ )
1275        {
1276          pFile->read(reinterpret_cast<Char*>(buf), len);
1277
1278          if( !is16bit )
1279          {
1280            for (Int x = 0; x < uiWidth; x++)
1281            {
1282              pPel[x] = buf[x];
1283            }
1284          }
1285          else
1286          {
1287            for (Int x = 0; x < uiWidth; x++)
1288            {
1289              pPel[x] = (buf[2*x+1] << 8) | buf[2*x];
1290            }
1291          }
1292     
1293          pPel += uiStride;
1294        }
1295
1296        len >>= 1;
1297        uiWidth >>= 1;
1298        uiHeight >>= 1;
1299
1300        // read Cb component
1301        pPel = pBLPic->getPicYuvRec()->getCbAddr();
1302        uiStride = pBLPic->getPicYuvRec()->getCStride();
1303        for( Int i = 0; i < uiHeight; i++ )
1304        {
1305          pFile->read(reinterpret_cast<Char*>(buf), len);
1306
1307          if( !is16bit )
1308          {
1309            for( Int x = 0; x < uiWidth; x++ )
1310            {
1311              pPel[x] = buf[x];
1312            }
1313          }
1314          else
1315          {
1316            for( Int x = 0; x < uiWidth; x++ )
1317            {
1318              pPel[x] = (buf[2*x+1] << 8) | buf[2*x];
1319            }
1320          }
1321     
1322          pPel += uiStride;
1323        }
1324
1325        // read Cr component
1326        pPel = pBLPic->getPicYuvRec()->getCrAddr();
1327        uiStride = pBLPic->getPicYuvRec()->getCStride();
1328        for( Int i = 0; i < uiHeight; i++ )
1329        {
1330          pFile->read(reinterpret_cast<Char*>(buf), len);
1331
1332          if( !is16bit )
1333          {
1334            for( Int x = 0; x < uiWidth; x++ )
1335            {
1336              pPel[x] = buf[x];
1337            }
1338          }
1339          else
1340          {
1341            for( Int x = 0; x < uiWidth; x++ )
1342            {
1343              pPel[x] = (buf[2*x+1] << 8) | buf[2*x];
1344            }
1345          }
1346     
1347          pPel += uiStride;
1348        }
1349
1350        delete[] buf;
1351      }
1352    }
1353#endif
1354
1355#if NO_OUTPUT_OF_PRIOR_PICS
1356    if ( m_layerId == 0 && m_apcSlicePilot->getRapPicFlag() && getNoClrasOutputFlag() )
1357    {
1358      for (UInt i = 0; i < m_apcSlicePilot->getVPS()->getMaxLayers(); i++)
1359      {
1360        m_ppcTDecTop[i]->setLayerInitializedFlag(false);
1361        m_ppcTDecTop[i]->setFirstPicInLayerDecodedFlag(false);
1362      }
1363    }
1364#endif
1365    // Buffer initialize for prediction.
1366    m_cPrediction.initTempBuff();
1367#if ALIGNED_BUMPING
1368    m_apcSlicePilot->checkLeadingPictureRestrictions(m_cListPic);
1369#else
1370    m_apcSlicePilot->applyReferencePictureSet(m_cListPic, m_apcSlicePilot->getRPS());
1371#endif
1372    //  Get a new picture buffer
1373    xGetNewPicBuffer (m_apcSlicePilot, pcPic);
1374
1375    Bool isField = false;
1376    Bool isTff = false;
1377   
1378    if(!m_SEIs.empty())
1379    {
1380      // Check if any new Picture Timing SEI has arrived
1381      SEIMessages pictureTimingSEIs = extractSeisByType (m_SEIs, SEI::PICTURE_TIMING);
1382      if (pictureTimingSEIs.size()>0)
1383      {
1384        SEIPictureTiming* pictureTiming = (SEIPictureTiming*) *(pictureTimingSEIs.begin());
1385        isField = (pictureTiming->m_picStruct == 1) || (pictureTiming->m_picStruct == 2);
1386        isTff =  (pictureTiming->m_picStruct == 1);
1387      }
1388    }
1389   
1390    //Set Field/Frame coding mode
1391    m_pcPic->setField(isField);
1392    m_pcPic->setTopField(isTff);
1393
1394    // transfer any SEI messages that have been received to the picture
1395    pcPic->setSEIs(m_SEIs);
1396    m_SEIs.clear();
1397
1398    // Recursive structure
1399    m_cCuDecoder.create ( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight );
1400#if SVC_EXTENSION
1401    m_cCuDecoder.init   ( m_ppcTDecTop,&m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction, curLayerId );
1402#else
1403    m_cCuDecoder.init   ( &m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction );
1404#endif
1405    m_cTrQuant.init     ( g_uiMaxCUWidth, g_uiMaxCUHeight, m_apcSlicePilot->getSPS()->getMaxTrSize());
1406
1407    m_cSliceDecoder.create();
1408  }
1409  else
1410  {
1411    // Check if any new SEI has arrived
1412    if(!m_SEIs.empty())
1413    {
1414      // Currently only decoding Unit SEI message occurring between VCL NALUs copied
1415      SEIMessages &picSEI = pcPic->getSEIs();
1416      SEIMessages decodingUnitInfos = extractSeisByType (m_SEIs, SEI::DECODING_UNIT_INFO);
1417      picSEI.insert(picSEI.end(), decodingUnitInfos.begin(), decodingUnitInfos.end());
1418      deleteSEIs(m_SEIs);
1419    }
1420  }
1421 
1422  //  Set picture slice pointer
1423  TComSlice*  pcSlice = m_apcSlicePilot;
1424  Bool bNextSlice     = pcSlice->isNextSlice();
1425
1426  UInt uiCummulativeTileWidth;
1427  UInt uiCummulativeTileHeight;
1428  UInt i, j, p;
1429
1430  //set NumColumnsMins1 and NumRowsMinus1
1431  pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getPPS()->getNumColumnsMinus1() );
1432  pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getPPS()->getNumRowsMinus1() );
1433
1434  //create the TComTileArray
1435  pcPic->getPicSym()->xCreateTComTileArray();
1436
1437  if( pcSlice->getPPS()->getUniformSpacingFlag() )
1438  {
1439    //set the width for each tile
1440    for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
1441    {
1442      for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++)
1443      {
1444        pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->
1445          setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1) 
1446          - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) );
1447      }
1448    }
1449
1450    //set the height for each tile
1451    for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
1452    {
1453      for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++)
1454      {
1455        pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->
1456          setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1) 
1457          - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) );   
1458      }
1459    }
1460  }
1461  else
1462  {
1463    //set the width for each tile
1464    for(j=0; j < pcSlice->getPPS()->getNumRowsMinus1()+1; j++)
1465    {
1466      uiCummulativeTileWidth = 0;
1467      for(i=0; i < pcSlice->getPPS()->getNumColumnsMinus1(); i++)
1468      {
1469        pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcSlice->getPPS()->getColumnWidth(i) );
1470        uiCummulativeTileWidth += pcSlice->getPPS()->getColumnWidth(i);
1471      }
1472      pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth );
1473    }
1474
1475    //set the height for each tile
1476    for(j=0; j < pcSlice->getPPS()->getNumColumnsMinus1()+1; j++)
1477    {
1478      uiCummulativeTileHeight = 0;
1479      for(i=0; i < pcSlice->getPPS()->getNumRowsMinus1(); i++)
1480      { 
1481        pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcSlice->getPPS()->getRowHeight(i) );
1482        uiCummulativeTileHeight += pcSlice->getPPS()->getRowHeight(i);
1483      }
1484      pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight );
1485    }
1486  }
1487
1488  pcPic->getPicSym()->xInitTiles();
1489
1490  //generate the Coding Order Map and Inverse Coding Order Map
1491  UInt uiEncCUAddr;
1492  for(i=0, uiEncCUAddr=0; i<pcPic->getPicSym()->getNumberOfCUsInFrame(); i++, uiEncCUAddr = pcPic->getPicSym()->xCalculateNxtCUAddr(uiEncCUAddr))
1493  {
1494    pcPic->getPicSym()->setCUOrderMap(i, uiEncCUAddr);
1495    pcPic->getPicSym()->setInverseCUOrderMap(uiEncCUAddr, i);
1496  }
1497  pcPic->getPicSym()->setCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
1498  pcPic->getPicSym()->setInverseCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
1499
1500  //convert the start and end CU addresses of the slice and dependent slice into encoding order
1501  pcSlice->setSliceSegmentCurStartCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurStartCUAddr()) );
1502  pcSlice->setSliceSegmentCurEndCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurEndCUAddr()) );
1503  if(pcSlice->isNextSlice())
1504  {
1505    pcSlice->setSliceCurStartCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurStartCUAddr()));
1506    pcSlice->setSliceCurEndCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurEndCUAddr()));
1507  }
1508
1509  if (m_bFirstSliceInPicture) 
1510  {
1511    if(pcPic->getNumAllocatedSlice() != 1)
1512    {
1513      pcPic->clearSliceBuffer();
1514    }
1515  }
1516  else
1517  {
1518    pcPic->allocateNewSlice();
1519  }
1520  assert(pcPic->getNumAllocatedSlice() == (m_uiSliceIdx + 1));
1521  m_apcSlicePilot = pcPic->getPicSym()->getSlice(m_uiSliceIdx); 
1522  pcPic->getPicSym()->setSlice(pcSlice, m_uiSliceIdx);
1523
1524  pcPic->setTLayer(nalu.m_temporalId);
1525
1526#if SVC_EXTENSION
1527  pcPic->setLayerId(nalu.m_layerId);
1528  pcSlice->setLayerId(nalu.m_layerId);
1529  pcSlice->setPic(pcPic);
1530#endif
1531
1532  if (bNextSlice)
1533  {
1534    pcSlice->checkCRA(pcSlice->getRPS(), m_pocCRA, m_associatedIRAPType, m_cListPic );
1535    // Set reference list
1536#if SVC_EXTENSION
1537    if (m_layerId == 0)
1538#endif
1539    pcSlice->setRefPicList( m_cListPic, true );
1540
1541#if SVC_EXTENSION
1542    // Create upsampling reference layer pictures for all possible dependent layers and do it only once for the first slice.
1543    // Other slices might choose which reference pictures to be used for inter-layer prediction
1544    if( m_layerId > 0 && m_uiSliceIdx == 0 )
1545    {     
1546#if M0040_ADAPTIVE_RESOLUTION_CHANGE
1547      if( !pcSlice->getVPS()->getSingleLayerForNonIrapFlag() || ( pcSlice->getVPS()->getSingleLayerForNonIrapFlag() && pcSlice->isIRAP() ) )
1548#endif
1549      for( i = 0; i < pcSlice->getNumILRRefIdx(); i++ )
1550      {
1551        UInt refLayerIdc = i;
1552        UInt refLayerId = pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc);
1553#if AVC_BASE
1554        if( refLayerId == 0 && m_parameterSetManagerDecoder.getActiveVPS()->getAvcBaseLayerFlag() )
1555        {         
1556          TComPic* pic = *m_ppcTDecTop[0]->getListPic()->begin();
1557
1558          if( pic )
1559          {
1560            pcSlice->setBaseColPic ( refLayerIdc, pic );
1561          }
1562          else
1563          {
1564            continue;
1565          }
1566#if AVC_SYNTAX
1567          TComPic* pBLPic = pcSlice->getBaseColPic(refLayerIdc);
1568          if( pcSlice->getPOC() == 0 )
1569          {
1570            // initialize partition order.
1571            UInt* piTmp = &g_auiZscanToRaster[0];
1572            initZscanToRaster( pBLPic->getPicSym()->getMaxDepth() + 1, 1, 0, piTmp );
1573            initRasterToZscan( pBLPic->getPicSym()->getMaxCUWidth(), pBLPic->getPicSym()->getMaxCUHeight(), pBLPic->getPicSym()->getMaxDepth() + 1 );
1574          }     
1575          pBLPic->getSlice( 0 )->initBaseLayerRPL( pcSlice );
1576          pBLPic->readBLSyntax( m_ppcTDecTop[0]->getBLSyntaxFile(), SYNTAX_BYTES );
1577#endif
1578        }
1579        else
1580        {
1581#if VPS_EXTN_DIRECT_REF_LAYERS
1582          TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1583#else
1584          TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1585#endif
1586          TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1587          if( !pcSlice->setBaseColPic ( *cListPic, refLayerIdc ) )
1588          {
1589            continue;
1590          }
1591        }
1592#else
1593#if VPS_EXTN_DIRECT_REF_LAYERS
1594        TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1595#else
1596        TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1597#endif
1598        TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1599        pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1600#endif
1601
1602#if O0098_SCALED_REF_LAYER_ID
1603        const Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindowForLayer(refLayerId);
1604#else
1605        const Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc);
1606#endif
1607
1608        Int widthBL   = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getWidth();
1609        Int heightBL  = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getHeight();
1610#if Q0200_CONFORMANCE_BL_SIZE
1611        Int chromaFormatIdc = pcSlice->getBaseColPic(refLayerIdc)->getSlice(0)->getChromaFormatIdc();
1612        const Window &confBL = pcSlice->getBaseColPic(refLayerIdc)->getConformanceWindow(); 
1613        widthBL  -= ( confBL.getWindowLeftOffset() + confBL.getWindowRightOffset() ) * TComSPS::getWinUnitX( chromaFormatIdc );
1614        heightBL -= ( confBL.getWindowTopOffset() + confBL.getWindowBottomOffset() ) * TComSPS::getWinUnitY( chromaFormatIdc );
1615#endif
1616        Int widthEL   = pcPic->getPicYuvRec()->getWidth()  - scalEL.getWindowLeftOffset() - scalEL.getWindowRightOffset();
1617        Int heightEL  = pcPic->getPicYuvRec()->getHeight() - scalEL.getWindowTopOffset()  - scalEL.getWindowBottomOffset();
1618
1619        g_mvScalingFactor[refLayerIdc][0] = widthEL  == widthBL  ? 4096 : Clip3(-4096, 4095, ((widthEL  << 8) + (widthBL  >> 1)) / widthBL);
1620        g_mvScalingFactor[refLayerIdc][1] = heightEL == heightBL ? 4096 : Clip3(-4096, 4095, ((heightEL << 8) + (heightBL >> 1)) / heightBL);
1621
1622        g_posScalingFactor[refLayerIdc][0] = ((widthBL  << 16) + (widthEL  >> 1)) / widthEL;
1623        g_posScalingFactor[refLayerIdc][1] = ((heightBL << 16) + (heightEL >> 1)) / heightEL;
1624
1625#if Q0048_CGS_3D_ASYMLUT
1626        TComPicYuv* pBaseColRec = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec();
1627        if( pcSlice->getPPS()->getCGSFlag() )
1628        {
1629          if(!m_pColorMappedPic)
1630          {
1631            initAsymLut(pcSlice->getBaseColPic(refLayerIdc)->getSlice(0));
1632          }
1633          m_c3DAsymLUTPPS.colorMapping( pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(),  m_pColorMappedPic );
1634          pBaseColRec = m_pColorMappedPic;
1635        }
1636#endif
1637#if SVC_EXTENSION
1638        if( pcPic->isSpatialEnhLayer(refLayerIdc) )
1639        {
1640          // check for the sample prediction picture type
1641          if( m_ppcTDecTop[m_layerId]->getSamplePredEnabledFlag(refLayerId) )
1642          {
1643#if O0215_PHASE_ALIGNMENT
1644#if O0194_JOINT_US_BITSHIFT
1645#if Q0048_CGS_3D_ASYMLUT
1646            m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pBaseColRec, pcPic->getPicYuvRec(), pcSlice->getVPS()->getPhaseAlignFlag() );
1647#else
1648            m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), pcSlice->getVPS()->getPhaseAlignFlag() );
1649#endif
1650#else
1651#if Q0048_CGS_3D_ASYMLUT
1652            m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pBaseColRec, pcPic->getPicYuvRec(), pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc), pcSlice->getVPS()->getPhaseAlignFlag() );
1653#else
1654            m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), scalEL, pcSlice->getVPS()->getPhaseAlignFlag() );
1655#endif
1656#endif
1657#else
1658#if O0194_JOINT_US_BITSHIFT
1659#if Q0048_CGS_3D_ASYMLUT
1660            m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pBaseColRec, pcPic->getPicYuvRec(), scalEL );
1661#else
1662            m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), scalEL );
1663#endif
1664#else
1665#if Q0048_CGS_3D_ASYMLUT
1666            m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pBaseColRec, pcPic->getPicYuvRec(), scalEL );
1667#else
1668            m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), scalEL );
1669#endif
1670#endif
1671#endif
1672          }
1673        }
1674        else
1675        {
1676          pcPic->setFullPelBaseRec( refLayerIdc, pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec() );
1677        }
1678        pcSlice->setFullPelBaseRec ( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc) );
1679#endif //SVC_EXTENSION
1680      }
1681    }
1682
1683    if( m_layerId > 0 && pcSlice->getActiveNumILRRefIdx() )
1684    {
1685      for( i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
1686      {
1687        UInt refLayerIdc = pcSlice->getInterLayerPredLayerIdc(i);
1688#if AVC_BASE
1689        if( pcSlice->getVPS()->getRefLayerId( m_layerId, refLayerIdc ) == 0 && m_parameterSetManagerDecoder.getActiveVPS()->getAvcBaseLayerFlag() )
1690        {
1691          pcSlice->setBaseColPic ( refLayerIdc, *m_ppcTDecTop[0]->getListPic()->begin() );
1692#if AVC_SYNTAX
1693          TComPic* pBLPic = pcSlice->getBaseColPic(refLayerIdc);
1694          if( pcSlice->getPOC() == 0 )
1695          {
1696            // initialize partition order.
1697            UInt* piTmp = &g_auiZscanToRaster[0];
1698            initZscanToRaster( pBLPic->getPicSym()->getMaxDepth() + 1, 1, 0, piTmp );
1699            initRasterToZscan( pBLPic->getPicSym()->getMaxCUWidth(), pBLPic->getPicSym()->getMaxCUHeight(), pBLPic->getPicSym()->getMaxDepth() + 1 );
1700          }     
1701          pBLPic->getSlice( 0 )->initBaseLayerRPL( pcSlice );
1702          pBLPic->readBLSyntax( m_ppcTDecTop[0]->getBLSyntaxFile(), SYNTAX_BYTES );
1703#endif
1704        }
1705        else
1706        {
1707#if VPS_EXTN_DIRECT_REF_LAYERS
1708          TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1709#else
1710          TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1711#endif
1712          TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1713          pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1714        }
1715#else
1716#if VPS_EXTN_DIRECT_REF_LAYERS
1717        TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1718#else
1719        TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1720#endif
1721        TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1722        pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1723#endif
1724
1725        pcSlice->setFullPelBaseRec ( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc) );
1726      }
1727
1728      pcSlice->setILRPic( m_cIlpPic );
1729
1730#if REF_IDX_MFM
1731      if( pcSlice->getMFMEnabledFlag() )
1732      {
1733        pcSlice->setRefPOCListILP(m_ppcTDecTop[m_layerId]->m_cIlpPic, pcSlice->getBaseColPic());
1734      }
1735      pcSlice->setRefPicList( m_cListPic, false, m_cIlpPic);
1736    }
1737#if M0040_ADAPTIVE_RESOLUTION_CHANGE
1738    else if ( m_layerId > 0 )
1739    {
1740      pcSlice->setRefPicList( m_cListPic, false, NULL);
1741    }
1742#endif
1743#if MFM_ENCCONSTRAINT
1744    if( pcSlice->getMFMEnabledFlag() )
1745    {
1746      TComPic* refPic = pcSlice->getRefPic( pcSlice->getSliceType() == B_SLICE ? ( RefPicList )( 1 - pcSlice->getColFromL0Flag() ) : REF_PIC_LIST_0 , pcSlice->getColRefIdx() );
1747
1748      assert( refPic );
1749
1750      Int refLayerId = refPic->getLayerId();
1751
1752      if( refLayerId != pcSlice->getLayerId() )
1753      {
1754        TComPic* pColBasePic = pcSlice->getBaseColPic( *m_ppcTDecTop[refLayerId]->getListPic() );
1755        assert( pColBasePic->checkSameRefInfo() == true );
1756      }
1757    }
1758#endif
1759#endif
1760   
1761    if( m_layerId > 0 && pcSlice->getVPS()->getCrossLayerIrapAlignFlag() )
1762    {
1763#if M0040_ADAPTIVE_RESOLUTION_CHANGE
1764      if( !pcSlice->getVPS()->getSingleLayerForNonIrapFlag() || ( pcSlice->getVPS()->getSingleLayerForNonIrapFlag() && pcSlice->isIRAP() ) )
1765#endif
1766      for(Int dependentLayerIdx = 0; dependentLayerIdx < pcSlice->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1767      {
1768        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1769        TComPic* refpicLayer = pcSlice->getRefPic(*cListPic, pcSlice->getPOC() );
1770        if(refpicLayer && pcSlice->isIRAP())
1771        {                 
1772          assert(pcSlice->getNalUnitType() == refpicLayer->getSlice(0)->getNalUnitType());
1773        }
1774      }
1775    }
1776
1777    if( m_layerId > 0 && !pcSlice->isIntra() && pcSlice->getEnableTMVPFlag() )
1778    {
1779      TComPic* refPic = pcSlice->getRefPic(RefPicList(1 - pcSlice->getColFromL0Flag()), pcSlice->getColRefIdx());
1780
1781      assert( refPic );
1782
1783      // It is a requirement of bitstream conformance when the collocated picture, used for temporal motion vector prediction, is an inter-layer reference picture,
1784      // VpsInterLayerMotionPredictionEnabled[ LayerIdxInVps[ currLayerId ] ][ LayerIdxInVps[ rLId ] ] shall be equal to 1, where rLId is set equal to nuh_layer_id of the inter-layer picture.
1785      if( refPic->isILR(pcSlice->getLayerId()) )
1786      {
1787        assert( m_ppcTDecTop[m_layerId]->getMotionPredEnabledFlag(refPic->getLayerId()) );
1788      }
1789    }
1790#endif //SVC_EXTENSION
1791   
1792    // For generalized B
1793    // note: maybe not existed case (always L0 is copied to L1 if L1 is empty)
1794    if (pcSlice->isInterB() && pcSlice->getNumRefIdx(REF_PIC_LIST_1) == 0)
1795    {
1796      Int iNumRefIdx = pcSlice->getNumRefIdx(REF_PIC_LIST_0);
1797      pcSlice->setNumRefIdx        ( REF_PIC_LIST_1, iNumRefIdx );
1798
1799      for (Int iRefIdx = 0; iRefIdx < iNumRefIdx; iRefIdx++)
1800      {
1801        pcSlice->setRefPic(pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx), REF_PIC_LIST_1, iRefIdx);
1802      }
1803    }
1804    if (!pcSlice->isIntra())
1805    {
1806      Bool bLowDelay = true;
1807      Int  iCurrPOC  = pcSlice->getPOC();
1808      Int iRefIdx = 0;
1809
1810      for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
1811      {
1812        if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
1813        {
1814          bLowDelay = false;
1815        }
1816      }
1817      if (pcSlice->isInterB())
1818      {
1819        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
1820        {
1821          if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
1822          {
1823            bLowDelay = false;
1824          }
1825        }       
1826      }
1827
1828      pcSlice->setCheckLDC(bLowDelay);           
1829    }
1830
1831    //---------------
1832    pcSlice->setRefPOCList();
1833  }
1834
1835  pcPic->setCurrSliceIdx(m_uiSliceIdx);
1836  if(pcSlice->getSPS()->getScalingListFlag())
1837  {
1838    pcSlice->setScalingList ( pcSlice->getSPS()->getScalingList()  );
1839    if(pcSlice->getPPS()->getScalingListPresentFlag())
1840    {
1841      pcSlice->setScalingList ( pcSlice->getPPS()->getScalingList()  );
1842    }
1843#if SCALINGLIST_INFERRING
1844    if( m_layerId == 0 || ( m_layerId > 0 && !pcSlice->getPPS()->getInferScalingListFlag() && !pcSlice->getSPS()->getInferScalingListFlag() ) )
1845#endif
1846    if(!pcSlice->getPPS()->getScalingListPresentFlag() && !pcSlice->getSPS()->getScalingListPresentFlag())
1847    {
1848      pcSlice->setDefaultScalingList();
1849    }
1850    m_cTrQuant.setScalingListDec(pcSlice->getScalingList());
1851    m_cTrQuant.setUseScalingList(true);
1852  }
1853  else
1854  {
1855    m_cTrQuant.setFlatScalingList();
1856    m_cTrQuant.setUseScalingList(false);
1857  }
1858
1859  //  Decode a picture
1860  m_cGopDecoder.decompressSlice(nalu.m_Bitstream, pcPic);
1861
1862  m_bFirstSliceInPicture = false;
1863  m_uiSliceIdx++;
1864
1865  return false;
1866}
1867
1868Void TDecTop::xDecodeVPS()
1869{
1870  TComVPS* vps = new TComVPS();
1871 
1872  m_cEntropyDecoder.decodeVPS( vps );
1873  m_parameterSetManagerDecoder.storePrefetchedVPS(vps); 
1874}
1875
1876Void TDecTop::xDecodeSPS()
1877{
1878  TComSPS* sps = new TComSPS();
1879#if SVC_EXTENSION
1880  sps->setLayerId(m_layerId);
1881#if SPS_DPB_PARAMS
1882  m_cEntropyDecoder.decodeSPS( sps ); // it should be removed after macro clean up
1883#else
1884  m_cEntropyDecoder.decodeSPS( sps, &m_parameterSetManagerDecoder );
1885#endif
1886  m_parameterSetManagerDecoder.storePrefetchedSPS(sps);
1887#if !REPN_FORMAT_IN_VPS   // ILRP can only be initialized at activation 
1888  if(m_numLayer>0)
1889  {
1890    xInitILRP(sps);
1891  }
1892#endif
1893#else //SVC_EXTENSION
1894  m_cEntropyDecoder.decodeSPS( sps );
1895  m_parameterSetManagerDecoder.storePrefetchedSPS(sps);
1896#endif //SVC_EXTENSION
1897}
1898
1899Void TDecTop::xDecodePPS(
1900#if Q0048_CGS_3D_ASYMLUT
1901  TCom3DAsymLUT * pc3DAsymLUT
1902#endif
1903  )
1904{
1905  TComPPS* pps = new TComPPS();
1906
1907#if SCALINGLIST_INFERRING
1908  pps->setLayerId( m_layerId );
1909#endif
1910
1911  m_cEntropyDecoder.decodePPS( pps
1912#if Q0048_CGS_3D_ASYMLUT
1913    , pc3DAsymLUT , m_layerId
1914#endif
1915    );
1916  m_parameterSetManagerDecoder.storePrefetchedPPS( pps );
1917
1918  if( pps->getDependentSliceSegmentsEnabledFlag() )
1919  {
1920    Int NumCtx = pps->getEntropyCodingSyncEnabledFlag()?2:1;
1921    m_cSliceDecoder.initCtxMem(NumCtx);
1922    for ( UInt st = 0; st < NumCtx; st++ )
1923    {
1924      TDecSbac* ctx = NULL;
1925      ctx = new TDecSbac;
1926      ctx->init( &m_cBinCABAC );
1927      m_cSliceDecoder.setCtxMem( ctx, st );
1928    }
1929  }
1930}
1931
1932Void TDecTop::xDecodeSEI( TComInputBitstream* bs, const NalUnitType nalUnitType )
1933{
1934#if SVC_EXTENSION
1935  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
1936  {
1937#if RANDOM_ACCESS_SEI_FIX
1938    if (m_prevSliceSkipped) // No need to decode SEI messages of a skipped access unit
1939    {
1940      return;
1941    }
1942#endif
1943#if LAYERS_NOT_PRESENT_SEI
1944    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
1945#else
1946    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1947#endif
1948  }
1949  else
1950  {
1951#if LAYERS_NOT_PRESENT_SEI
1952    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
1953#else
1954    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1955#endif
1956    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
1957    if (activeParamSets.size()>0)
1958    {
1959      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
1960      m_parameterSetManagerDecoder.applyPrefetchedPS();
1961      assert(seiAps->activeSeqParameterSetId.size()>0);
1962      if( !m_parameterSetManagerDecoder.activateSPSWithSEI( seiAps->activeSeqParameterSetId[0] ) )
1963      {
1964        printf ("Warning SPS activation with Active parameter set SEI failed");
1965      }
1966    }
1967#if Q0074_SEI_COLOR_MAPPING
1968    m_ColorMapping->setColorMapping( m_SEIs );
1969#endif
1970  }
1971#else
1972  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
1973  {
1974#if LAYERS_NOT_PRESENT_SEI
1975    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
1976#else
1977    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1978#endif
1979  }
1980  else
1981  {
1982#if LAYERS_NOT_PRESENT_SEI
1983    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
1984#else
1985    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1986#endif
1987    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
1988    if (activeParamSets.size()>0)
1989    {
1990      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
1991      m_parameterSetManagerDecoder.applyPrefetchedPS();
1992      assert(seiAps->activeSeqParameterSetId.size()>0);
1993      if (! m_parameterSetManagerDecoder.activateSPSWithSEI(seiAps->activeSeqParameterSetId[0] ))
1994      {
1995        printf ("Warning SPS activation with Active parameter set SEI failed");
1996      }
1997    }
1998  }
1999#endif
2000}
2001
2002#if SVC_EXTENSION
2003Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC)
2004#else
2005Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay)
2006#endif
2007{
2008  // Initialize entropy decoder
2009  m_cEntropyDecoder.setEntropyDecoder (&m_cCavlcDecoder);
2010  m_cEntropyDecoder.setBitstream      (nalu.m_Bitstream);
2011
2012#if O0137_MAX_LAYERID
2013  // ignore any NAL units with nuh_layer_id == 63
2014  if (nalu.m_layerId == 63 )
2015  { 
2016    return false;
2017  }
2018#endif
2019  switch (nalu.m_nalUnitType)
2020  {
2021    case NAL_UNIT_VPS:
2022#if VPS_NUH_LAYER_ID
2023      assert( nalu.m_layerId == 0 ); // Non-conforming bitstream. The value of nuh_layer_id of VPS NAL unit shall be equal to 0.
2024#endif
2025      xDecodeVPS();
2026#if Q0177_EOS_CHECKS
2027      m_isLastNALWasEos = false;
2028#endif
2029#if AVC_BASE
2030      if( m_parameterSetManagerDecoder.getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
2031      {
2032        if( !m_ppcTDecTop[0]->getBLReconFile()->good() )
2033        {
2034          printf( "Base layer YUV input reading error\n" );
2035          exit(EXIT_FAILURE);
2036        }       
2037#if AVC_SYNTAX
2038        if( !m_ppcTDecTop[0]->getBLSyntaxFile()->good() )
2039        {
2040          printf( "Base layer syntax input reading error\n" );
2041          exit(EXIT_FAILURE);
2042        }
2043#endif
2044      }
2045      else
2046      {
2047        TComList<TComPic*> *cListPic = m_ppcTDecTop[0]->getListPic();
2048        cListPic->clear();
2049      }
2050#endif
2051      return false;
2052     
2053    case NAL_UNIT_SPS:
2054      xDecodeSPS();
2055      return false;
2056
2057    case NAL_UNIT_PPS:
2058      xDecodePPS(
2059#if Q0048_CGS_3D_ASYMLUT
2060        &m_c3DAsymLUTPPS
2061#endif
2062        );
2063      return false;
2064     
2065    case NAL_UNIT_PREFIX_SEI:
2066    case NAL_UNIT_SUFFIX_SEI:
2067#if Q0177_EOS_CHECKS
2068      if ( nalu.m_nalUnitType == NAL_UNIT_SUFFIX_SEI )
2069      {
2070        assert( m_isLastNALWasEos == false );
2071      }
2072#endif
2073      xDecodeSEI( nalu.m_Bitstream, nalu.m_nalUnitType );
2074      return false;
2075
2076    case NAL_UNIT_CODED_SLICE_TRAIL_R:
2077    case NAL_UNIT_CODED_SLICE_TRAIL_N:
2078    case NAL_UNIT_CODED_SLICE_TSA_R:
2079    case NAL_UNIT_CODED_SLICE_TSA_N:
2080    case NAL_UNIT_CODED_SLICE_STSA_R:
2081    case NAL_UNIT_CODED_SLICE_STSA_N:
2082    case NAL_UNIT_CODED_SLICE_BLA_W_LP:
2083    case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
2084    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
2085    case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
2086    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
2087    case NAL_UNIT_CODED_SLICE_CRA:
2088    case NAL_UNIT_CODED_SLICE_RADL_N:
2089    case NAL_UNIT_CODED_SLICE_RADL_R:
2090    case NAL_UNIT_CODED_SLICE_RASL_N:
2091    case NAL_UNIT_CODED_SLICE_RASL_R:
2092#if Q0177_EOS_CHECKS
2093      if (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_TRAIL_R || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_TRAIL_N ||
2094          nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_TSA_R || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_TSA_N ||
2095          nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_STSA_R || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_STSA_N ||
2096          nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
2097          nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N )
2098      {
2099        assert( m_isLastNALWasEos == false );
2100      }
2101      else
2102      {
2103        m_isLastNALWasEos = false;
2104      }
2105#endif
2106#if SVC_EXTENSION
2107      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay, curLayerId, bNewPOC);
2108#else
2109      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay);
2110#endif
2111      break;
2112     
2113    case NAL_UNIT_EOS:
2114#if Q0177_EOS_CHECKS
2115      assert( m_isLastNALWasEos == false );
2116      //Check layer id of the nalu. if it is not 0, give a warning message and just return without doing anything.
2117      if (nalu.m_layerId > 0)
2118      {
2119        printf( "\nThis bitstream has EOS with non-zero layer id.\n" );
2120        return false;
2121      }
2122      m_isLastNALWasEos = true;
2123#endif
2124      m_associatedIRAPType = NAL_UNIT_INVALID;
2125      m_pocCRA = 0;
2126      m_pocRandomAccess = MAX_INT;
2127      m_prevPOC = MAX_INT;
2128      m_bFirstSliceInPicture = true;
2129      m_bFirstSliceInSequence = true;
2130      m_prevSliceSkipped = false;
2131      m_skippedPOC = 0;
2132      return false;
2133     
2134    case NAL_UNIT_ACCESS_UNIT_DELIMITER:
2135      // TODO: process AU delimiter
2136      return false;
2137     
2138    case NAL_UNIT_EOB:
2139#if P0130_EOB
2140      //Check layer id of the nalu. if it is not 0, give a warning message.
2141      if (nalu.m_layerId > 0)
2142      {
2143        printf( "\n\nThis bitstream is ended with EOB NALU that has layer id greater than 0\n" );
2144      }
2145#endif
2146      return false;
2147     
2148    case NAL_UNIT_FILLER_DATA:
2149#if Q0177_EOS_CHECKS
2150      assert( m_isLastNALWasEos == false );
2151#endif
2152      return false;
2153     
2154    case NAL_UNIT_RESERVED_VCL_N10:
2155    case NAL_UNIT_RESERVED_VCL_R11:
2156    case NAL_UNIT_RESERVED_VCL_N12:
2157    case NAL_UNIT_RESERVED_VCL_R13:
2158    case NAL_UNIT_RESERVED_VCL_N14:
2159    case NAL_UNIT_RESERVED_VCL_R15:
2160     
2161    case NAL_UNIT_RESERVED_IRAP_VCL22:
2162    case NAL_UNIT_RESERVED_IRAP_VCL23:
2163     
2164    case NAL_UNIT_RESERVED_VCL24:
2165    case NAL_UNIT_RESERVED_VCL25:
2166    case NAL_UNIT_RESERVED_VCL26:
2167    case NAL_UNIT_RESERVED_VCL27:
2168    case NAL_UNIT_RESERVED_VCL28:
2169    case NAL_UNIT_RESERVED_VCL29:
2170    case NAL_UNIT_RESERVED_VCL30:
2171    case NAL_UNIT_RESERVED_VCL31:
2172     
2173    case NAL_UNIT_RESERVED_NVCL41:
2174    case NAL_UNIT_RESERVED_NVCL42:
2175    case NAL_UNIT_RESERVED_NVCL43:
2176    case NAL_UNIT_RESERVED_NVCL44:
2177    case NAL_UNIT_RESERVED_NVCL45:
2178    case NAL_UNIT_RESERVED_NVCL46:
2179    case NAL_UNIT_RESERVED_NVCL47:
2180    case NAL_UNIT_UNSPECIFIED_48:
2181    case NAL_UNIT_UNSPECIFIED_49:
2182    case NAL_UNIT_UNSPECIFIED_50:
2183    case NAL_UNIT_UNSPECIFIED_51:
2184    case NAL_UNIT_UNSPECIFIED_52:
2185    case NAL_UNIT_UNSPECIFIED_53:
2186    case NAL_UNIT_UNSPECIFIED_54:
2187    case NAL_UNIT_UNSPECIFIED_55:
2188    case NAL_UNIT_UNSPECIFIED_56:
2189    case NAL_UNIT_UNSPECIFIED_57:
2190    case NAL_UNIT_UNSPECIFIED_58:
2191    case NAL_UNIT_UNSPECIFIED_59:
2192    case NAL_UNIT_UNSPECIFIED_60:
2193    case NAL_UNIT_UNSPECIFIED_61:
2194    case NAL_UNIT_UNSPECIFIED_62:
2195    case NAL_UNIT_UNSPECIFIED_63:
2196
2197    default:
2198      assert (0);
2199  }
2200
2201  return false;
2202}
2203
2204/** Function for checking if picture should be skipped because of association with a previous BLA picture
2205 * \param iPOCLastDisplay POC of last picture displayed
2206 * \returns true if the picture should be skipped
2207 * This function skips all TFD pictures that follow a BLA picture
2208 * in decoding order and precede it in output order.
2209 */
2210Bool TDecTop::isSkipPictureForBLA(Int& iPOCLastDisplay)
2211{
2212  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) && 
2213       m_apcSlicePilot->getPOC() < m_pocCRA && (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N))
2214  {
2215    iPOCLastDisplay++;
2216    return true;
2217  }
2218  return false;
2219}
2220
2221/** Function for checking if picture should be skipped because of random access
2222 * \param iSkipFrame skip frame counter
2223 * \param iPOCLastDisplay POC of last picture displayed
2224 * \returns true if the picture shold be skipped in the random access.
2225 * This function checks the skipping of pictures in the case of -s option random access.
2226 * All pictures prior to the random access point indicated by the counter iSkipFrame are skipped.
2227 * It also checks the type of Nal unit type at the random access point.
2228 * 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.
2229 * If the random access point is IDR all pictures after the random access point are decoded.
2230 * If the random access point is none of the above, a warning is issues, and decoding of pictures with POC
2231 * equal to or greater than the random access point POC is attempted. For non IDR/CRA/BLA random
2232 * access point there is no guarantee that the decoder will not crash.
2233 */
2234Bool TDecTop::isRandomAccessSkipPicture(Int& iSkipFrame,  Int& iPOCLastDisplay)
2235{
2236  if (iSkipFrame) 
2237  {
2238    iSkipFrame--;   // decrement the counter
2239    return true;
2240  }
2241  else if (m_pocRandomAccess == MAX_INT) // start of random access point, m_pocRandomAccess has not been set yet.
2242  {
2243    if (   m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA
2244        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
2245        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
2246        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL )
2247    {
2248      // set the POC random access since we need to skip the reordered pictures in the case of CRA/CRANT/BLA/BLANT.
2249      m_pocRandomAccess = m_apcSlicePilot->getPOC();
2250    }
2251    else if ( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
2252    {
2253      m_pocRandomAccess = -MAX_INT; // no need to skip the reordered pictures in IDR, they are decodable.
2254    }
2255    else 
2256    {
2257      static Bool warningMessage = false;
2258      if(!warningMessage)
2259      {
2260        printf("\nWarning: this is not a valid random access point and the data is discarded until the first CRA picture");
2261        warningMessage = true;
2262      }
2263      return true;
2264    }
2265  }
2266  // skip the reordered pictures, if necessary
2267  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))
2268  {
2269    iPOCLastDisplay++;
2270    return true;
2271  }
2272  // if we reach here, then the picture is not skipped.
2273  return false; 
2274}
2275
2276#if SVC_EXTENSION
2277#if VPS_EXTN_DIRECT_REF_LAYERS
2278TDecTop* TDecTop::getRefLayerDec( UInt refLayerIdc )
2279{
2280  TComVPS* vps = m_parameterSetManagerDecoder.getActiveVPS();
2281  if( vps->getNumDirectRefLayers( m_layerId ) <= 0 )
2282  {
2283    return (TDecTop *)getLayerDec( 0 );
2284  }
2285 
2286  return (TDecTop *)getLayerDec( vps->getRefLayerId( m_layerId, refLayerIdc ) );
2287}
2288#endif
2289
2290#if VPS_EXTN_DIRECT_REF_LAYERS
2291Void TDecTop::setRefLayerParams( TComVPS* vps )
2292{
2293  for(UInt layer = 0; layer < m_numLayer; layer++)
2294  {
2295    TDecTop *decTop = (TDecTop *)getLayerDec(layer);
2296    decTop->setNumSamplePredRefLayers(0);
2297    decTop->setNumMotionPredRefLayers(0);
2298    decTop->setNumDirectRefLayers(0);
2299    for(Int i = 0; i < MAX_VPS_LAYER_ID_PLUS1; i++)
2300    {
2301      decTop->setSamplePredEnabledFlag(i, false);
2302      decTop->setMotionPredEnabledFlag(i, false);
2303      decTop->setSamplePredRefLayerId(i, 0);
2304      decTop->setMotionPredRefLayerId(i, 0);
2305    }
2306    for(Int j = 0; j < layer; j++)
2307    {
2308      if (vps->getDirectDependencyFlag(layer, j))
2309      {
2310        decTop->setRefLayerId(decTop->getNumDirectRefLayers(), vps->getLayerIdInNuh(layer));
2311        decTop->setNumDirectRefLayers(decTop->getNumDirectRefLayers() + 1);
2312
2313        Int samplePredEnabledFlag = (vps->getDirectDependencyType(layer, j) + 1) & 1;
2314        decTop->setSamplePredEnabledFlag(j, samplePredEnabledFlag == 1 ? true : false);
2315        decTop->setNumSamplePredRefLayers(decTop->getNumSamplePredRefLayers() + samplePredEnabledFlag);
2316
2317        Int motionPredEnabledFlag = ((vps->getDirectDependencyType(layer, j) + 1) & 2) >> 1;
2318        decTop->setMotionPredEnabledFlag(j, motionPredEnabledFlag == 1 ? true : false);
2319        decTop->setNumMotionPredRefLayers(decTop->getNumMotionPredRefLayers() + motionPredEnabledFlag);
2320      }
2321    }
2322  }
2323  for( Int i = 1; i < m_numLayer; i++ )
2324  {
2325    Int mIdx = 0, sIdx = 0;
2326    Int iNuhLId = vps->getLayerIdInNuh(i);
2327    TDecTop *decTop = (TDecTop *)getLayerDec(iNuhLId);
2328    for ( Int j = 0; j < i; j++ )
2329    {
2330      if (decTop->getMotionPredEnabledFlag(j))
2331      {
2332        decTop->setMotionPredRefLayerId(mIdx++, vps->getLayerIdInNuh(j));
2333      }
2334      if (decTop->getSamplePredEnabledFlag(j))
2335      {
2336        decTop->setSamplePredRefLayerId(sIdx++, vps->getLayerIdInNuh(j));
2337      }
2338    }
2339  }
2340}
2341
2342#endif
2343
2344#if OUTPUT_LAYER_SET_INDEX
2345Void TDecTop::checkValueOfTargetOutputLayerSetIdx(TComVPS *vps)
2346{
2347  CommonDecoderParams* params = this->getCommonDecoderParams();
2348
2349  assert( params->getTargetLayerId() < vps->getMaxLayers() );
2350
2351  if( params->getValueCheckedFlag() )
2352  {
2353    return; // Already checked
2354  }
2355  if( params->getTargetOutputLayerSetIdx() == -1 )  // Output layer set index not specified
2356  {
2357    Bool layerSetMatchFound = false;
2358    // Output layer set index not assigned.
2359    // Based on the value of targetLayerId, check if any of the output layer matches
2360    // Currently, the target layer ID in the encoder assumes that all the layers are decoded   
2361    // Check if any of the output layer sets match this description
2362    for(Int i = 0; i < vps->getNumOutputLayerSets(); i++)
2363    {
2364      Bool layerSetMatchFlag = true;
2365      Int layerSetIdx = vps->getOutputLayerSetIdx( i );
2366      if( vps->getNumLayersInIdList( layerSetIdx ) == params->getTargetLayerId() + 1 )
2367      {
2368        for(Int j = 0; j < vps->getNumLayersInIdList( layerSetIdx ); j++)
2369        {
2370          if( vps->getLayerSetLayerIdList( layerSetIdx, j ) != j )
2371          {
2372            layerSetMatchFlag = false;
2373            break;
2374          }
2375        }
2376      }
2377      else
2378      {
2379        layerSetMatchFlag = false;
2380      }
2381     
2382      if( layerSetMatchFlag ) // Potential output layer set candidate found
2383      {
2384        // If target dec layer ID list is also included - check if they match
2385        if( params->getTargetDecLayerIdSet() )
2386        {
2387          if( params->getTargetDecLayerIdSet()->size() ) 
2388          {
2389            for(Int j = 0; j < vps->getNumLayersInIdList( layerSetIdx ); j++)
2390            {
2391              if( *(params->getTargetDecLayerIdSet()->begin() + j) != vps->getLayerIdInNuh(vps->getLayerSetLayerIdList( layerSetIdx, j )))
2392              {
2393                layerSetMatchFlag = false;
2394              }
2395            }
2396          }
2397        }
2398        if( layerSetMatchFlag ) // The target dec layer ID list also matches, if present
2399        {
2400          // Match found
2401          layerSetMatchFound = true;
2402          params->setTargetOutputLayerSetIdx( i );
2403          params->setValueCheckedFlag( true );
2404          break;
2405        }
2406      }
2407    }
2408    assert( layerSetMatchFound ); // No output layer set matched the value of either targetLayerId or targetdeclayerIdlist
2409  }   
2410  else // Output layer set index is assigned - check if the values match
2411  {
2412    // Check if the target decoded layer is the highest layer in the list
2413    assert( params->getTargetOutputLayerSetIdx() < vps->getNumLayerSets() );
2414    Int layerSetIdx = vps->getOutputLayerSetIdx( params->getTargetOutputLayerSetIdx() );  // Index to the layer set
2415    assert( params->getTargetLayerId() == vps->getNumLayersInIdList( layerSetIdx ) - 1);
2416
2417    Bool layerSetMatchFlag = true;
2418    for(Int j = 0; j < vps->getNumLayersInIdList( layerSetIdx ); j++)
2419    {
2420      if( vps->getLayerSetLayerIdList( layerSetIdx, j ) != j )
2421      {
2422        layerSetMatchFlag = false;
2423        break;
2424      }
2425    }
2426
2427    assert(layerSetMatchFlag);    // Signaled output layer set index does not match targetOutputLayerId.
2428   
2429    // Check if the targetdeclayerIdlist matches the output layer set
2430    if( params->getTargetDecLayerIdSet() )
2431    {
2432      if( params->getTargetDecLayerIdSet()->size() ) 
2433      {
2434        for(Int i = 0; i < vps->getNumLayersInIdList( layerSetIdx ); i++)
2435        {
2436          assert( *(params->getTargetDecLayerIdSet()->begin() + i) == vps->getLayerIdInNuh(vps->getLayerSetLayerIdList( layerSetIdx, i )));
2437        }
2438      }
2439    }
2440    params->setValueCheckedFlag( true );
2441
2442  }
2443}
2444#endif
2445#if RESOLUTION_BASED_DPB
2446Void TDecTop::assignSubDpbs(TComVPS *vps)
2447{
2448  if( m_subDpbIdx == -1 ) // Sub-DPB index is not already assigned
2449  {
2450    Int lsIdx = vps->getOutputLayerSetIdx( getCommonDecoderParams()->getTargetOutputLayerSetIdx() );
2451
2452    Int layerIdx = vps->findLayerIdxInLayerSet( lsIdx, getLayerId() );
2453    assert( layerIdx != -1 ); // Current layer should be found in the layer set.
2454
2455    // Copy from the active VPS based on the layer ID.
2456    m_subDpbIdx = vps->getSubDpbAssigned( lsIdx, layerIdx );
2457  }
2458}
2459#endif
2460
2461#if Q0048_CGS_3D_ASYMLUT
2462Void TDecTop::initAsymLut(TComSlice *pcSlice)
2463{
2464  if(m_layerId>0)
2465  {
2466    if(!m_pColorMappedPic)
2467    {
2468      Int picWidth    = pcSlice->getPicWidthInLumaSamples();
2469      Int picHeight   = pcSlice->getPicHeightInLumaSamples();
2470      m_pColorMappedPic = new TComPicYuv;
2471      m_pColorMappedPic->create( picWidth, picHeight, pcSlice->getChromaFormatIdc()/*CHROMA_420*/, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, NULL );
2472    }
2473  }
2474}
2475#endif
2476
2477#endif //SVC_EXTENSION
2478
2479#if Q0074_SEI_COLOR_MAPPING
2480TDecColorMapping::TDecColorMapping()
2481{
2482  m_pcColorMappingPic[0]   = NULL;
2483  m_pcColorMappingPic[1]   = NULL;
2484
2485  m_colorMapCancelFlag  = true;
2486
2487  for( Int i=0 ; i<3 ; i++ )
2488  {
2489    m_lut1d_computed[i]             = false;
2490    m_lut1d_input[i]                = NULL;
2491    m_coded_input_pivot_value[i]    = NULL;
2492    m_target_input_pivot_value[i]   = NULL;
2493  }
2494  for( Int i=0 ; i<3 ; i++ )
2495  {
2496    m_lut1d_output[i]               = NULL;
2497    m_coded_output_pivot_value[i]   = NULL;
2498    m_target_output_pivot_value[i]  = NULL;
2499  }
2500}
2501
2502TDecColorMapping::~TDecColorMapping()
2503{
2504  if ( m_pcColorMappingPic[0] )  delete m_pcColorMappingPic[0];
2505  if ( m_pcColorMappingPic[1] )  delete m_pcColorMappingPic[1];
2506
2507  for( Int i=0 ; i<3 ; i++ )
2508  {
2509    if ( m_lut1d_input[i] )               delete  m_lut1d_input[i];
2510    if ( m_coded_input_pivot_value[i] )   delete  m_coded_input_pivot_value[i];
2511    if ( m_target_input_pivot_value[i] )  delete  m_target_input_pivot_value[i];
2512  }
2513  for( Int i=0 ; i<3 ; i++ )
2514  {
2515    if ( m_lut1d_output[i] )               delete  m_lut1d_output[i];
2516    if ( m_coded_output_pivot_value[i] )   delete  m_coded_output_pivot_value[i];
2517    if ( m_target_output_pivot_value[i] )  delete  m_target_output_pivot_value[i];
2518  }
2519}
2520
2521Void  TDecColorMapping::setColorMapping( SEIMessages m_SEIs )
2522{
2523  SEIMessages colorMappingInfo = getSeisByType(m_SEIs, SEI::COLOR_MAPPING_INFO) ;
2524  SEIColorMappingInfo *seiColorMappingInfo = NULL;
2525  if (colorMappingInfo.size() !=0)
2526  {
2527    seiColorMappingInfo = (SEIColorMappingInfo*)(*colorMappingInfo.begin());
2528
2529    m_colorMapId                              = seiColorMappingInfo->m_colorMapId;
2530    m_colorMapCancelFlag                      = seiColorMappingInfo->m_colorMapCancelFlag;
2531    if( !m_colorMapCancelFlag )
2532    {
2533      m_colorMapPersistenceFlag                 = seiColorMappingInfo->m_colorMapPersistenceFlag;
2534      m_colorMap_video_signal_type_present_flag = seiColorMappingInfo->m_colorMap_video_signal_type_present_flag;
2535      m_colorMap_video_full_range_flag          = seiColorMappingInfo->m_colorMap_video_full_range_flag;
2536      m_colorMap_primaries                      = seiColorMappingInfo->m_colorMap_primaries;
2537      m_colorMap_transfer_characteristics       = seiColorMappingInfo->m_colorMap_transfer_characteristics;
2538      m_colorMap_matrix_coeffs                  = seiColorMappingInfo->m_colorMap_matrix_coeffs;
2539      m_colorMapModelId                         = seiColorMappingInfo->m_colorMapModelId;
2540
2541      m_colour_map_coded_data_bit_depth         = seiColorMappingInfo->m_colour_map_coded_data_bit_depth;
2542      m_colour_map_target_bit_depth             = seiColorMappingInfo->m_colour_map_target_bit_depth;
2543
2544      for( Int i=0 ; i<3 ; i++ )
2545      {
2546        m_num_input_pivots[i]                 = seiColorMappingInfo->m_num_input_pivots[i];
2547        if ( m_coded_input_pivot_value[i] )   delete  m_coded_input_pivot_value[i];
2548        if ( m_target_input_pivot_value[i] )  delete  m_target_input_pivot_value[i];
2549        m_coded_input_pivot_value[i]          = new Int[ m_num_input_pivots[i] ];
2550        m_target_input_pivot_value[i]         = new Int[ m_num_input_pivots[i] ];
2551        for( Int j=0 ; j<m_num_input_pivots[i] ; j++ )
2552        {
2553          m_coded_input_pivot_value[i][j]     = seiColorMappingInfo->m_coded_input_pivot_value[i][j];
2554          m_target_input_pivot_value[i][j]    = seiColorMappingInfo->m_target_input_pivot_value[i][j];
2555        }
2556      }
2557
2558      m_matrix_flag       = seiColorMappingInfo->m_matrix_flag;
2559      m_log2_matrix_denom = m_matrix_flag ? (seiColorMappingInfo->m_log2_matrix_denom) : (0) ;
2560      for( Int i=0 ; i<3 ; i++ )
2561      {
2562        for( Int j=0 ; j<3 ; j++ )
2563        {
2564          m_matrix_coef[i][j]  = seiColorMappingInfo->m_matrix_coef[i][j];
2565        }
2566      }
2567
2568      for( Int i=0 ; i<3 ; i++ )
2569      {
2570        m_num_output_pivots[i]                 = seiColorMappingInfo->m_num_output_pivots[i];
2571        if ( m_coded_output_pivot_value[i] )   delete  m_coded_output_pivot_value[i];
2572        if ( m_target_output_pivot_value[i] )  delete  m_target_output_pivot_value[i];
2573        m_coded_output_pivot_value[i]          = new Int[ m_num_output_pivots[i] ];
2574        m_target_output_pivot_value[i]         = new Int[ m_num_output_pivots[i] ];
2575        for( Int j=0 ; j<m_num_output_pivots[i] ; j++ )
2576        {
2577          m_coded_output_pivot_value[i][j]     = seiColorMappingInfo->m_coded_output_pivot_value[i][j];
2578          m_target_output_pivot_value[i][j]    = seiColorMappingInfo->m_target_output_pivot_value[i][j];
2579        }
2580      }
2581
2582      memset( m_lut1d_computed, 0, sizeof( m_lut1d_computed ) );
2583    }
2584  }
2585
2586}
2587
2588Void  TDecColorMapping::setColorMapping( Int bitDepth, Int iComp )
2589{
2590  if( !m_colorMapCancelFlag && !m_lut1d_computed[iComp] )
2591  {
2592
2593    if ( m_lut1d_input[iComp] )   delete m_lut1d_input[iComp];
2594    if ( m_lut1d_output[iComp] )  delete m_lut1d_output[iComp];
2595
2596    m_lut1d_input[iComp]  = new Int[ 1 << bitDepth ];
2597    m_lut1d_output[iComp] = new Int[ 1 << bitDepth ];
2598
2599    Int iShift      = (m_colour_map_coded_data_bit_depth >= bitDepth) ? (m_colour_map_coded_data_bit_depth - bitDepth) : (0);
2600    Int iShiftPivot = (m_colour_map_coded_data_bit_depth >= bitDepth) ? (0) : (bitDepth - m_colour_map_coded_data_bit_depth);
2601
2602    for( Int k=0 ; k<(1<<bitDepth) ; k++ )
2603    {
2604      Int iSample = k << iShift ;
2605      if( m_num_input_pivots[iComp] > 1 )
2606      {
2607        for( Int iPivot=0 ; iPivot<m_num_input_pivots[iComp] ; iPivot++ )
2608        {
2609          Int iCodedPrev  = m_coded_input_pivot_value[iComp][iPivot]     << iShiftPivot;
2610          Int iCodedNext  = m_coded_input_pivot_value[iComp][iPivot+1]   << iShiftPivot;
2611          Int iTargetPrev = m_target_input_pivot_value[iComp][iPivot]    << iShiftPivot;
2612          Int iTargetNext = m_target_input_pivot_value[iComp][iPivot+1]  << iShiftPivot;
2613          if ( iCodedPrev <= iSample && iSample < iCodedNext )
2614          {
2615            Float fInterpol = (Float)( (iCodedNext - iSample)*iTargetPrev + (iSample - iCodedPrev)*iTargetNext ) / (Float)(iCodedNext - iCodedPrev) ;
2616            m_lut1d_input[iComp][k]  = (Int)( 0.5 + fInterpol );
2617            iPivot = m_num_input_pivots[iComp]; // stop
2618          }
2619        }
2620      }
2621      else
2622      {
2623        m_lut1d_input[iComp][k]  = k;
2624      }
2625    }
2626
2627    iShift      = ( (m_colour_map_coded_data_bit_depth >= bitDepth) ? (m_colour_map_coded_data_bit_depth - bitDepth) : (0) );
2628    Int iOffset = iShift ? (1 << (iShift - 1)) : (0) ;
2629    iShiftPivot = (m_colour_map_target_bit_depth >= bitDepth) ? (0) : (bitDepth - m_colour_map_target_bit_depth) ;
2630    for( Int k=0 ; k<(1<<bitDepth) ; k++ )
2631    {
2632      Int iSample = k << iShift;
2633      if ( m_num_output_pivots[iComp]>1 )
2634      {
2635        for( Int iPivot=0 ; iPivot<m_num_output_pivots[iComp] ; iPivot++ )
2636        {
2637          Int iCodedPrev  = m_coded_output_pivot_value[iComp][iPivot]     << iShiftPivot;
2638          Int iCodedNext  = m_coded_output_pivot_value[iComp][iPivot+1]   << iShiftPivot;
2639          Int iTargetPrev = m_target_output_pivot_value[iComp][iPivot]    << iShiftPivot;
2640          Int iTargetNext = m_target_output_pivot_value[iComp][iPivot+1]  << iShiftPivot;
2641          if ( iCodedPrev <= iSample && iSample < iCodedNext )
2642          {
2643            Float fInterpol =  (Float)( (iCodedNext - iSample)*iTargetPrev + (iSample - iCodedPrev)*iTargetNext ) / (Float)(iCodedNext - iCodedPrev) ;
2644            m_lut1d_output[iComp][k]  = ( (Int)(0.5 + fInterpol) + iOffset ) >> iShift ;
2645            iPivot = m_num_output_pivots[iComp]; // stop
2646          }
2647        }
2648      }
2649      else
2650      {
2651        m_lut1d_output[iComp][k]  = k;
2652      }
2653    }
2654
2655    m_lut1d_computed[iComp] = true;
2656  }
2657
2658}
2659
2660TComPicYuv* TDecColorMapping::getColorMapping( TComPicYuv* pPicYuvRec, Int iTop, Int curlayerId )
2661{
2662  if( !m_colorMapCancelFlag )
2663  {
2664    if( !m_pcColorMappingPic[iTop] )
2665    {
2666      m_pcColorMappingPic[iTop] = new TComPicYuv;
2667#if SVC_EXTENSION
2668      m_pcColorMappingPic[iTop]->create( pPicYuvRec->getWidth(), pPicYuvRec->getHeight(), pPicYuvRec->getChromaFormat(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
2669#else
2670      m_pcColorMappingPic[iTop]->create( pPicYuvRec->getWidth(), pPicYuvRec->getHeight(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth );
2671#endif
2672    }
2673
2674    Int iHeight   = pPicYuvRec->getHeight();
2675    Int iWidth    = pPicYuvRec->getWidth();
2676    Int iStride   = pPicYuvRec->getStride();
2677    Int iCStride  = pPicYuvRec->getCStride();
2678
2679    Pel* Lum0   = pPicYuvRec->getLumaAddr();
2680    Pel* Cb0    = pPicYuvRec->getCbAddr();
2681    Pel* Cr0    = pPicYuvRec->getCrAddr();
2682    Pel* Lum1   = m_pcColorMappingPic[iTop]->getLumaAddr();
2683    Pel* Cb1    = m_pcColorMappingPic[iTop]->getCbAddr();
2684    Pel* Cr1    = m_pcColorMappingPic[iTop]->getCrAddr();
2685
2686#if SVC_EXTENSION
2687    Int bitDepthY = g_bitDepthYLayer[curlayerId];
2688    Int bitDepthC = g_bitDepthCLayer[curlayerId];
2689
2690    assert( g_bitDepthY == bitDepthY );
2691    assert( g_bitDepthC == bitDepthC );
2692#else
2693    Int bitDepthY = g_bitDepthY;
2694    Int bitDepthC = g_bitDepthC;
2695#endif
2696
2697    Int iYShift = (m_colour_map_target_bit_depth >= bitDepthY) ? (m_colour_map_target_bit_depth - bitDepthY) : (0) ;
2698    Int iCShift = (m_colour_map_target_bit_depth >= bitDepthC) ? (m_colour_map_target_bit_depth - bitDepthC) : (0) ;
2699    Int offsetY = (1 << (m_log2_matrix_denom+iYShift - 1));
2700    Int offsetC = (1 << (m_log2_matrix_denom+iCShift - 1));
2701
2702    Int   cShift  = 1 ;
2703
2704    //Pel*  LumPrev0 = Lum0;
2705    for( Int y = 0; y < iHeight ; y++ )
2706    {
2707      Bool  bDoChroma = (y % 2);
2708      for( Int x = 0; x < iWidth ; x++ )
2709      {
2710        Int s1Y = m_lut1d_input[0][ Lum0[x]   ];
2711        Int s1U = m_lut1d_input[1][ Cb0[x>>1] ];
2712        Int s1V = m_lut1d_input[2][ Cr0[x>>1] ];
2713
2714        Int s2Y, s2U, s2V;
2715        if( m_matrix_flag )
2716        {
2717          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 );
2718          //s2Y = ClipBD( s2Y , bitDepthY );
2719          s2Y = ClipY( s2Y );
2720          Lum1[x]   = m_lut1d_output[0][ s2Y ];
2721        }
2722        else
2723        {
2724          s1Y       = ( s1Y + offsetY ) >> iYShift ;
2725          //s1Y = ClipBD( s1Y , bitDepthY );
2726          s1Y = ClipY( s1Y );
2727          Lum1[x]   = m_lut1d_output[0][ s1Y ];
2728        }
2729
2730        if( bDoChroma && (x%2) )
2731        {
2732          if( m_matrix_flag )
2733          {
2734            //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 ;
2735            //s1Y = m_lut1d_input[0][ (Lum0[x] + Lum0[x+1] + LumPrev0[x] + LumPrev0[x+1] + 2)>>2 ] ;
2736            s1Y = m_lut1d_input[0][ Lum0[x] ];
2737
2738            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 ) ;
2739            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 ) ;
2740            //s2U = ClipBD( s2U , bitDepthC );
2741            //s2V = ClipBD( s2V , bitDepthC );
2742            s2U = ClipC( s2U );
2743            s2V = ClipC( s2V );
2744            Cb1[x>>cShift] = m_lut1d_output[1][ s2U ];
2745            Cr1[x>>cShift] = m_lut1d_output[2][ s2V ];
2746          }
2747          else
2748          {
2749            s1U       = ( s1U + offsetC ) >> iCShift ;
2750            s1V       = ( s1V + offsetC ) >> iCShift ;
2751            //s1U = ClipBD( s1U , bitDepthC );
2752            //s1V = ClipBD( s1V , bitDepthC );
2753            s1U = ClipC( s1U );
2754            s1V = ClipC( s1V );
2755            Cb1[x>>cShift] = m_lut1d_output[1][ s1U ];
2756            Cr1[x>>cShift] = m_lut1d_output[2][ s1V ];
2757          }
2758        }
2759
2760      }
2761
2762      //LumPrev0 = Lum0;
2763      Lum0  += iStride;
2764      Lum1  += iStride;
2765      if( bDoChroma )
2766      {
2767        Cb0 += iCStride;
2768        Cr0 += iCStride;
2769        Cb1 += iCStride;
2770        Cr1 += iCStride;
2771      }
2772    }
2773
2774    return m_pcColorMappingPic[iTop];
2775  }
2776
2777  return pPicYuvRec; 
2778}
2779#endif
2780
2781//! \}
Note: See TracBrowser for help on using the repository browser.