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

Last change on this file since 501 was 494, checked in by seregin, 11 years ago

reintegrate branch SHM-4.0-dev

  • Property svn:eol-style set to native
File size: 67.6 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2013, 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}
82
83TDecTop::~TDecTop()
84{
85#if ENC_DEC_TRACE
86  fclose( g_hTrace );
87#endif
88}
89
90Void TDecTop::create()
91{
92#if SVC_EXTENSION
93  m_cGopDecoder.create( m_layerId );
94#else
95  m_cGopDecoder.create();
96#endif
97  m_apcSlicePilot = new TComSlice;
98  m_uiSliceIdx = 0;
99}
100
101Void TDecTop::destroy()
102{
103  m_cGopDecoder.destroy();
104 
105  delete m_apcSlicePilot;
106  m_apcSlicePilot = NULL;
107 
108  m_cSliceDecoder.destroy();
109#if SVC_EXTENSION
110  for(Int i=0; i<MAX_NUM_REF; i++)
111  {
112    if(m_cIlpPic[i])
113    {
114      m_cIlpPic[i]->destroy();
115      delete m_cIlpPic[i];
116      m_cIlpPic[i] = NULL;
117    }
118  }   
119#endif
120}
121
122Void TDecTop::init()
123{
124#if !SVC_EXTENSION
125  // initialize ROM
126  initROM();
127#endif
128#if SVC_EXTENSION
129  m_cGopDecoder.init( m_ppcTDecTop, &m_cEntropyDecoder, &m_cSbacDecoder, &m_cBinCABAC, &m_cCavlcDecoder, &m_cSliceDecoder, &m_cLoopFilter, &m_cSAO);
130  m_cSliceDecoder.init( m_ppcTDecTop, &m_cEntropyDecoder, &m_cCuDecoder );
131#else
132  m_cGopDecoder.init( &m_cEntropyDecoder, &m_cSbacDecoder, &m_cBinCABAC, &m_cCavlcDecoder, &m_cSliceDecoder, &m_cLoopFilter, &m_cSAO);
133  m_cSliceDecoder.init( &m_cEntropyDecoder, &m_cCuDecoder );
134#endif
135  m_cEntropyDecoder.init(&m_cPrediction);
136}
137
138#if SVC_EXTENSION
139#if !REPN_FORMAT_IN_VPS
140Void TDecTop::xInitILRP(TComSPS *pcSPS)
141#else
142Void TDecTop::xInitILRP(TComSlice *slice)
143#endif
144{
145#if REPN_FORMAT_IN_VPS
146  TComSPS* pcSPS = slice->getSPS();
147  Int bitDepthY   = slice->getBitDepthY();
148  Int bitDepthC   = slice->getBitDepthC();
149  Int picWidth    = slice->getPicWidthInLumaSamples();
150  Int picHeight   = slice->getPicHeightInLumaSamples();
151#endif
152  if(m_layerId>0)
153  {
154#if REPN_FORMAT_IN_VPS
155    g_bitDepthY     = bitDepthY;
156    g_bitDepthC     = bitDepthC;
157#else
158    g_bitDepthY     = pcSPS->getBitDepthY();
159    g_bitDepthC     = pcSPS->getBitDepthC();
160#endif
161    g_uiMaxCUWidth  = pcSPS->getMaxCUWidth();
162    g_uiMaxCUHeight = pcSPS->getMaxCUHeight();
163    g_uiMaxCUDepth  = pcSPS->getMaxCUDepth();
164    g_uiAddCUDepth  = max (0, pcSPS->getLog2MinCodingBlockSize() - (Int)pcSPS->getQuadtreeTULog2MinSize() );
165
166    Int  numReorderPics[MAX_TLAYER];
167    Window &conformanceWindow = pcSPS->getConformanceWindow();
168    Window defaultDisplayWindow = pcSPS->getVuiParametersPresentFlag() ? pcSPS->getVuiParameters()->getDefaultDisplayWindow() : Window();
169
170    for( Int temporalLayer=0; temporalLayer < MAX_TLAYER; temporalLayer++) 
171    {
172      numReorderPics[temporalLayer] = pcSPS->getNumReorderPics(temporalLayer);
173    }
174
175    if (m_cIlpPic[0] == NULL)
176    {
177      for (Int j=0; j < MAX_LAYERS /*MAX_NUM_REF*/; j++)  // consider to set to NumDirectRefLayers[LayerIdInVps[nuh_layer_id]]
178      {
179
180        m_cIlpPic[j] = new  TComPic;
181#if AUXILIARY_PICTURES
182#if REPN_FORMAT_IN_VPS
183#if SVC_UPSAMPLING
184        m_cIlpPic[j]->create(picWidth, picHeight, slice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
185#else
186        m_cIlpPic[j]->create(picWidth, picHeight, slice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, true);
187#endif
188#else
189#if SVC_UPSAMPLING
190        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), pcSPS->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
191#else
192        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), pcSPS->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, true);
193#endif
194#endif
195#else
196#if REPN_FORMAT_IN_VPS
197#if SVC_UPSAMPLING
198        m_cIlpPic[j]->create(picWidth, picHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
199#else
200        m_cIlpPic[j]->create(picWidth, picHeight, g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, true);
201#endif
202#else
203#if SVC_UPSAMPLING
204        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, pcSPS, true);
205#else
206        m_cIlpPic[j]->create(pcSPS->getPicWidthInLumaSamples(), pcSPS->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, conformanceWindow, defaultDisplayWindow, numReorderPics, true);
207#endif
208#endif
209#endif
210        for (Int i=0; i<m_cIlpPic[j]->getPicSym()->getNumberOfCUsInFrame(); i++)
211        {
212          m_cIlpPic[j]->getPicSym()->getCU(i)->initCU(m_cIlpPic[j], i);
213        }
214      }
215    }
216  }
217}
218#endif
219
220Void TDecTop::deletePicBuffer ( )
221{
222  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
223  Int iSize = Int( m_cListPic.size() );
224 
225  for (Int i = 0; i < iSize; i++ )
226  {
227    TComPic* pcPic = *(iterPic++);
228#if SVC_EXTENSION
229    if( pcPic )
230    {
231      pcPic->destroy();
232
233      delete pcPic;
234      pcPic = NULL;
235    }
236#else
237    pcPic->destroy();
238   
239    delete pcPic;
240    pcPic = NULL;
241#endif
242  }
243 
244  m_cSAO.destroy();
245 
246  m_cLoopFilter.        destroy();
247 
248#if !SVC_EXTENSION
249  // destroy ROM
250  destroyROM();
251#endif
252}
253
254Void TDecTop::xGetNewPicBuffer ( TComSlice* pcSlice, TComPic*& rpcPic )
255{
256  Int  numReorderPics[MAX_TLAYER];
257  Window &conformanceWindow = pcSlice->getSPS()->getConformanceWindow();
258  Window defaultDisplayWindow = pcSlice->getSPS()->getVuiParametersPresentFlag() ? pcSlice->getSPS()->getVuiParameters()->getDefaultDisplayWindow() : Window();
259
260  for( Int temporalLayer=0; temporalLayer < MAX_TLAYER; temporalLayer++) 
261  {
262    numReorderPics[temporalLayer] = pcSlice->getSPS()->getNumReorderPics(temporalLayer);
263  }
264
265  m_iMaxRefPicNum = pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getTLayer());     // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
266
267#if SVC_EXTENSION
268  m_iMaxRefPicNum += 1; // it should be updated if more than 1 resampling picture is used
269#endif
270
271  if (m_cListPic.size() < (UInt)m_iMaxRefPicNum)
272  {
273    rpcPic = new TComPic();
274
275#if SVC_EXTENSION //Temporal solution, should be modified
276    if(m_layerId > 0)
277    {
278      for(UInt i = 0; i < pcSlice->getVPS()->getNumDirectRefLayers( m_layerId ); i++ )
279      {
280        const Window scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(i);
281        Bool zeroOffsets = ( scalEL.getWindowLeftOffset() == 0 && scalEL.getWindowRightOffset() == 0 && scalEL.getWindowTopOffset() == 0 && scalEL.getWindowBottomOffset() == 0 );
282
283#if VPS_EXTN_DIRECT_REF_LAYERS
284        TDecTop *pcTDecTopBase = (TDecTop *)getRefLayerDec( i );
285#else
286        TDecTop *pcTDecTopBase = (TDecTop *)getLayerDec( m_layerId-1 );
287#endif
288        //TComPic*                      pcPic = *(pcTDecTopBase->getListPic()->begin());
289        TComPicYuv* pcPicYuvRecBase = (*(pcTDecTopBase->getListPic()->begin()))->getPicYuvRec(); 
290#if REPN_FORMAT_IN_VPS
291        if(pcPicYuvRecBase->getWidth() != pcSlice->getPicWidthInLumaSamples() || pcPicYuvRecBase->getHeight() != pcSlice->getPicHeightInLumaSamples() || !zeroOffsets )
292#else
293        if(pcPicYuvRecBase->getWidth() != pcSlice->getSPS()->getPicWidthInLumaSamples() || pcPicYuvRecBase->getHeight() != pcSlice->getSPS()->getPicHeightInLumaSamples() || !zeroOffsets )
294#endif
295        {
296          rpcPic->setSpatialEnhLayerFlag( i, true );
297
298          //only for scalable extension
299#if SCALABILITY_MASK_E0104
300          assert( pcSlice->getVPS()->getScalabilityMask(2) == true );
301#else
302          assert( pcSlice->getVPS()->getScalabilityMask(1) == true );
303#endif
304        }
305      }
306    }
307#endif
308   
309#if AUXILIARY_PICTURES
310#if REPN_FORMAT_IN_VPS
311#if SVC_UPSAMPLING
312    rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), pcSlice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
313                     conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
314#else
315    rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), pcSlice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
316                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
317#endif
318#else
319#if SVC_UPSAMPLING
320    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
321                     conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
322#else
323    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
324                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
325#endif
326#endif
327#else
328#if REPN_FORMAT_IN_VPS
329#if SVC_UPSAMPLING
330    rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
331                     conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
332#else
333    rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
334                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
335#endif
336#else
337#if SVC_UPSAMPLING
338    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
339                     conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
340#else
341    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
342                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
343#endif
344#endif
345#endif
346
347    rpcPic->getPicSym()->allocSaoParam(&m_cSAO);
348    m_cListPic.pushBack( rpcPic );
349   
350    return;
351  }
352 
353  Bool bBufferIsAvailable = false;
354  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
355  while (iterPic != m_cListPic.end())
356  {
357    rpcPic = *(iterPic++);
358    if ( rpcPic->getReconMark() == false && rpcPic->getOutputMark() == false)
359    {
360      rpcPic->setOutputMark(false);
361      bBufferIsAvailable = true;
362      break;
363    }
364
365    if ( rpcPic->getSlice( 0 )->isReferenced() == false  && rpcPic->getOutputMark() == false)
366    {
367#if !SVC_EXTENSION
368      rpcPic->setOutputMark(false);
369#endif
370      rpcPic->setReconMark( false );
371      rpcPic->getPicYuvRec()->setBorderExtension( false );
372      bBufferIsAvailable = true;
373      break;
374    }
375  }
376 
377  if ( !bBufferIsAvailable )
378  {
379    //There is no room for this picture, either because of faulty encoder or dropped NAL. Extend the buffer.
380    m_iMaxRefPicNum++;
381    rpcPic = new TComPic();
382    m_cListPic.pushBack( rpcPic );
383  }
384  rpcPic->destroy();
385
386#if AUXILIARY_PICTURES
387#if REPN_FORMAT_IN_VPS
388#if SVC_UPSAMPLING
389  rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), pcSlice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
390                   conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
391
392#else
393  rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), pcSlice->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
394                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
395#endif
396#else
397#if SVC_UPSAMPLING
398  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
399                   conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
400
401#else
402  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), pcSlice->getSPS()->getChromaFormatIdc(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
403                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
404#endif
405#endif
406#else
407#if REPN_FORMAT_IN_VPS
408#if SVC_UPSAMPLING
409  rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
410                   conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
411
412#else
413  rpcPic->create ( pcSlice->getPicWidthInLumaSamples(), pcSlice->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
414                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
415#endif
416#else
417#if SVC_UPSAMPLING
418  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
419                   conformanceWindow, defaultDisplayWindow, numReorderPics, pcSlice->getSPS(), true);
420
421#else
422  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
423                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
424#endif
425#endif
426#endif
427  rpcPic->getPicSym()->allocSaoParam(&m_cSAO);
428}
429
430Void TDecTop::executeLoopFilters(Int& poc, TComList<TComPic*>*& rpcListPic)
431{
432  if (!m_pcPic)
433  {
434    /* nothing to deblock */
435    return;
436  }
437 
438  TComPic*&   pcPic         = m_pcPic;
439
440  // Execute Deblock + Cleanup
441
442  m_cGopDecoder.filterPicture(pcPic);
443
444#if SYNTAX_OUTPUT
445  pcPic->wrireBLSyntax( getBLSyntaxFile(), SYNTAX_BYTES );
446#endif
447  TComSlice::sortPicList( m_cListPic ); // sorting for application output
448  poc                 = pcPic->getSlice(m_uiSliceIdx-1)->getPOC();
449  rpcListPic          = &m_cListPic; 
450  m_cCuDecoder.destroy();       
451  m_bFirstSliceInPicture  = true;
452
453  return;
454}
455
456#if EARLY_REF_PIC_MARKING
457Void TDecTop::earlyPicMarking(Int maxTemporalLayer, std::vector<Int>& targetDecLayerIdSet)
458{
459  UInt currTid = m_pcPic->getTLayer();
460  UInt highestTid = (maxTemporalLayer >= 0) ? maxTemporalLayer : (m_pcPic->getSlice(0)->getSPS()->getMaxTLayers() - 1);
461  UInt latestDecLayerId = m_layerId;
462  UInt numTargetDecLayers = 0;
463  Int targetDecLayerIdList[MAX_LAYERS];
464  UInt latestDecIdx = 0;
465  TComSlice* pcSlice = m_pcPic->getSlice(0);
466
467  if ( currTid != highestTid )  // Marking  process is only applicaple for highest decoded TLayer
468  {
469    return;
470  }
471
472  // currPic must be marked as "used for reference" and must be a sub-layer non-reference picture
473  if ( !((pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_TRAIL_N  ||
474          pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N    ||
475          pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N   ||
476          pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N   ||
477          pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N   ||
478          pcSlice->getNalUnitType() == NAL_UNIT_RESERVED_VCL_N10     ||
479          pcSlice->getNalUnitType() == NAL_UNIT_RESERVED_VCL_N12     ||
480          pcSlice->getNalUnitType() == NAL_UNIT_RESERVED_VCL_N14) && pcSlice->isReferenced()))
481  {
482    return;
483  }
484
485  if ( targetDecLayerIdSet.size() == 0 ) // Cannot mark if we don't know the number of scalable layers
486  {
487    return;
488  }
489
490  for (std::vector<Int>::iterator it = targetDecLayerIdSet.begin(); it != targetDecLayerIdSet.end(); it++)
491  {
492    if ( latestDecLayerId == (*it) )
493    {
494      latestDecIdx = numTargetDecLayers;
495    }
496    targetDecLayerIdList[numTargetDecLayers++] = (*it);
497  }
498
499  Int remainingInterLayerReferencesFlag = 0;
500#if O0225_MAX_TID_FOR_REF_LAYERS
501  for ( Int j = latestDecIdx + 1; j < numTargetDecLayers; j++ )
502  {
503    Int jLidx = pcSlice->getVPS()->getLayerIdInVps(targetDecLayerIdList[j]);
504    if ( currTid <= pcSlice->getVPS()->getMaxTidIlRefPicsPlus1(latestDecLayerId,jLidx) - 1 )
505    {
506#else
507  if ( currTid <= pcSlice->getVPS()->getMaxTidIlRefPicsPlus1(latestDecLayerId) - 1 )
508  {
509    for ( Int j = latestDecIdx + 1; j < numTargetDecLayers; j++ )
510    {
511#endif
512      for ( Int k = 0; k < m_ppcTDecTop[targetDecLayerIdList[j]]->getNumDirectRefLayers(); k++ )
513      {
514        if ( latestDecIdx == m_ppcTDecTop[targetDecLayerIdList[j]]->getRefLayerId(k) )
515        {
516          remainingInterLayerReferencesFlag = 1;
517        }
518      }
519    }
520  }
521
522  if ( remainingInterLayerReferencesFlag == 0 )
523  {
524    pcSlice->setReferenced(false);
525  }
526}
527#endif
528
529Void TDecTop::xCreateLostPicture(Int iLostPoc) 
530{
531  printf("\ninserting lost poc : %d\n",iLostPoc);
532  TComSlice cFillSlice;
533#if SVC_EXTENSION
534  cFillSlice.setSPS( m_parameterSetManagerDecoder[m_layerId].getFirstSPS() );
535  cFillSlice.setPPS( m_parameterSetManagerDecoder[m_layerId].getFirstPPS() );
536  cFillSlice.setVPS( m_parameterSetManagerDecoder[m_layerId].getFirstVPS() );
537  cFillSlice.initSlice( m_layerId );
538#else
539  cFillSlice.setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
540  cFillSlice.setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
541  cFillSlice.initSlice();
542#endif
543  TComPic *cFillPic;
544  xGetNewPicBuffer(&cFillSlice,cFillPic);
545#if SVC_EXTENSION
546  cFillPic->getSlice(0)->setSPS( m_parameterSetManagerDecoder[m_layerId].getFirstSPS() );
547  cFillPic->getSlice(0)->setPPS( m_parameterSetManagerDecoder[m_layerId].getFirstPPS() );
548  cFillPic->getSlice(0)->setVPS( m_parameterSetManagerDecoder[m_layerId].getFirstVPS() );
549  cFillPic->getSlice(0)->initSlice( m_layerId );
550#else
551  cFillPic->getSlice(0)->setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
552  cFillPic->getSlice(0)->setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
553  cFillPic->getSlice(0)->initSlice();
554#endif
555 
556  TComList<TComPic*>::iterator iterPic = m_cListPic.begin();
557  Int closestPoc = 1000000;
558  while ( iterPic != m_cListPic.end())
559  {
560    TComPic * rpcPic = *(iterPic++);
561    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())
562    {
563      closestPoc=abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc);
564    }
565  }
566  iterPic = m_cListPic.begin();
567  while ( iterPic != m_cListPic.end())
568  {
569    TComPic *rpcPic = *(iterPic++);
570    if(abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc)==closestPoc&&rpcPic->getPicSym()->getSlice(0)->getPOC()!=m_apcSlicePilot->getPOC())
571    {
572      printf("copying picture %d to %d (%d)\n",rpcPic->getPicSym()->getSlice(0)->getPOC() ,iLostPoc,m_apcSlicePilot->getPOC());
573      rpcPic->getPicYuvRec()->copyToPic(cFillPic->getPicYuvRec());
574      break;
575    }
576  }
577  cFillPic->setCurrSliceIdx(0);
578  for(Int i=0; i<cFillPic->getNumCUsInFrame(); i++) 
579  {
580    cFillPic->getCU(i)->initCU(cFillPic,i);
581  }
582  cFillPic->getSlice(0)->setReferenced(true);
583  cFillPic->getSlice(0)->setPOC(iLostPoc);
584  cFillPic->setReconMark(true);
585  cFillPic->setOutputMark(true);
586  if(m_pocRandomAccess == MAX_INT)
587  {
588    m_pocRandomAccess = iLostPoc;
589  }
590}
591
592
593Void TDecTop::xActivateParameterSets()
594{
595#if SVC_EXTENSION
596  m_parameterSetManagerDecoder[m_layerId].applyPrefetchedPS();
597 
598  TComPPS *pps = m_parameterSetManagerDecoder[m_layerId].getPPS(m_apcSlicePilot->getPPSId());
599  assert (pps != 0);
600
601  TComSPS *sps = m_parameterSetManagerDecoder[m_layerId].getSPS(pps->getSPSId());
602  assert (sps != 0);
603
604  if( false == m_parameterSetManagerDecoder[m_layerId].activatePPS(m_apcSlicePilot->getPPSId(), m_apcSlicePilot->isIRAP()) )
605#else
606  m_parameterSetManagerDecoder.applyPrefetchedPS();
607 
608  TComPPS *pps = m_parameterSetManagerDecoder.getPPS(m_apcSlicePilot->getPPSId());
609  assert (pps != 0);
610
611  TComSPS *sps = m_parameterSetManagerDecoder.getSPS(pps->getSPSId());
612  assert (sps != 0);
613
614  if (false == m_parameterSetManagerDecoder.activatePPS(m_apcSlicePilot->getPPSId(),m_apcSlicePilot->isIRAP()))
615#endif
616  {
617    printf ("Parameter set activation failed!");
618    assert (0);
619  }
620
621  if( pps->getDependentSliceSegmentsEnabledFlag() )
622  {
623    Int NumCtx = pps->getEntropyCodingSyncEnabledFlag()?2:1;
624
625    if (m_cSliceDecoder.getCtxMemSize() != NumCtx)
626    {
627      m_cSliceDecoder.initCtxMem(NumCtx);
628      for ( UInt st = 0; st < NumCtx; st++ )
629      {
630        TDecSbac* ctx = NULL;
631        ctx = new TDecSbac;
632        ctx->init( &m_cBinCABAC );
633        m_cSliceDecoder.setCtxMem( ctx, st );
634      }
635    }
636  }
637
638  m_apcSlicePilot->setPPS(pps);
639  m_apcSlicePilot->setSPS(sps);
640  pps->setSPS(sps);
641#if REPN_FORMAT_IN_VPS
642  pps->setNumSubstreams(pps->getEntropyCodingSyncEnabledFlag() ? ((sps->getPicHeightInLumaSamples() + sps->getMaxCUHeight() - 1) / sps->getMaxCUHeight()) * (pps->getNumColumnsMinus1() + 1) : 1);
643#else
644  pps->setNumSubstreams(pps->getEntropyCodingSyncEnabledFlag() ? ((sps->getPicHeightInLumaSamples() + sps->getMaxCUHeight() - 1) / sps->getMaxCUHeight()) * (pps->getNumColumnsMinus1() + 1) : 1);
645#endif
646  pps->setMinCuDQPSize( sps->getMaxCUWidth() >> ( pps->getMaxCuDQPDepth()) );
647
648#if REPN_FORMAT_IN_VPS
649  g_bitDepthY     = m_apcSlicePilot->getBitDepthY();
650  g_bitDepthC     = m_apcSlicePilot->getBitDepthC();
651#else
652  g_bitDepthY     = sps->getBitDepthY();
653  g_bitDepthC     = sps->getBitDepthC();
654#endif
655  g_uiMaxCUWidth  = sps->getMaxCUWidth();
656  g_uiMaxCUHeight = sps->getMaxCUHeight();
657  g_uiMaxCUDepth  = sps->getMaxCUDepth();
658  g_uiAddCUDepth  = max (0, sps->getLog2MinCodingBlockSize() - (Int)sps->getQuadtreeTULog2MinSize() );
659
660  for (Int i = 0; i < sps->getLog2DiffMaxMinCodingBlockSize(); i++)
661  {
662    sps->setAMPAcc( i, sps->getUseAMP() );
663  }
664
665  for (Int i = sps->getLog2DiffMaxMinCodingBlockSize(); i < sps->getMaxCUDepth(); i++)
666  {
667    sps->setAMPAcc( i, 0 );
668  }
669
670  m_cSAO.destroy();
671#if REPN_FORMAT_IN_VPS
672  m_cSAO.create( m_apcSlicePilot->getPicWidthInLumaSamples(), m_apcSlicePilot->getPicHeightInLumaSamples(), sps->getMaxCUWidth(), sps->getMaxCUHeight() );
673#else
674  m_cSAO.create( sps->getPicWidthInLumaSamples(), sps->getPicHeightInLumaSamples(), sps->getMaxCUWidth(), sps->getMaxCUHeight() );
675#endif
676  m_cLoopFilter.create( sps->getMaxCUDepth() );
677}
678
679#if SVC_EXTENSION
680#if POC_RESET_FLAG
681Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int &iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC )
682#else
683Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC )
684#endif
685#else
686Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay )
687#endif
688{
689  TComPic*&   pcPic         = m_pcPic;
690#if SVC_EXTENSION
691  m_apcSlicePilot->setVPS( m_parameterSetManagerDecoder[m_layerId].getPrefetchedVPS(0) );
692  m_apcSlicePilot->initSlice( nalu.m_layerId );
693#else
694  m_apcSlicePilot->initSlice();
695#endif
696
697  if (m_bFirstSliceInPicture)
698  {
699    m_uiSliceIdx     = 0;
700  }
701  m_apcSlicePilot->setSliceIdx(m_uiSliceIdx);
702  if (!m_bFirstSliceInPicture)
703  {
704    m_apcSlicePilot->copySliceInfo( pcPic->getPicSym()->getSlice(m_uiSliceIdx-1) );
705  }
706
707  m_apcSlicePilot->setNalUnitType(nalu.m_nalUnitType);
708  Bool nonReferenceFlag = (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TRAIL_N ||
709                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N   ||
710                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N  ||
711                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N  ||
712                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N);
713  m_apcSlicePilot->setTemporalLayerNonReferenceFlag(nonReferenceFlag);
714 
715  m_apcSlicePilot->setReferenced(true); // Putting this as true ensures that picture is referenced the first time it is in an RPS
716  m_apcSlicePilot->setTLayerInfo(nalu.m_temporalId);
717
718#if SVC_EXTENSION
719  m_apcSlicePilot->setSliceIdx( m_uiSliceIdx ); // it should be removed if HM will reflect it in above
720#if VPS_EXTN_DIRECT_REF_LAYERS && M0457_PREDICTION_INDICATIONS
721  setRefLayerParams(m_apcSlicePilot->getVPS());
722#endif
723#if M0457_COL_PICTURE_SIGNALING
724  m_apcSlicePilot->setNumMotionPredRefLayers(m_numMotionPredRefLayers);
725#endif
726#if M0457_IL_SAMPLE_PRED_ONLY_FLAG
727  m_apcSlicePilot->setNumSamplePredRefLayers( getNumSamplePredRefLayers() );
728#endif
729  m_cEntropyDecoder.decodeSliceHeader (m_apcSlicePilot, &m_parameterSetManagerDecoder[m_layerId]);
730#else
731  m_cEntropyDecoder.decodeSliceHeader (m_apcSlicePilot, &m_parameterSetManagerDecoder);
732#endif
733
734  // set POC for dependent slices in skipped pictures
735  if(m_apcSlicePilot->getDependentSliceSegmentFlag() && m_prevSliceSkipped) 
736  {
737    m_apcSlicePilot->setPOC(m_skippedPOC);
738  }
739
740  m_apcSlicePilot->setAssociatedIRAPPOC(m_pocCRA);
741  m_apcSlicePilot->setAssociatedIRAPType(m_associatedIRAPType);
742
743  // Skip pictures due to random access
744  if (isRandomAccessSkipPicture(iSkipFrame, iPOCLastDisplay))
745  {
746    m_prevSliceSkipped = true;
747    m_skippedPOC = m_apcSlicePilot->getPOC();
748    return false;
749  }
750  // Skip TFD pictures associated with BLA/BLANT pictures
751  if (isSkipPictureForBLA(iPOCLastDisplay))
752  {
753    m_prevSliceSkipped = true;
754    m_skippedPOC = m_apcSlicePilot->getPOC();
755    return false;
756  }
757
758  // clear previous slice skipped flag
759  m_prevSliceSkipped = false;
760
761  // exit when a new picture is found
762#if SVC_EXTENSION
763  bNewPOC = (m_apcSlicePilot->getPOC()!= m_prevPOC);
764  if (m_apcSlicePilot->isNextSlice() && (bNewPOC || m_layerId!=m_uiPrevLayerId) && !m_bFirstSliceInSequence )
765  {
766    m_prevPOC = m_apcSlicePilot->getPOC();
767    curLayerId = m_uiPrevLayerId; 
768    m_uiPrevLayerId = m_layerId;
769    return true;
770  }
771#else
772  //we should only get a different poc for a new picture (with CTU address==0)
773  if (m_apcSlicePilot->isNextSlice() && m_apcSlicePilot->getPOC()!=m_prevPOC && !m_bFirstSliceInSequence && (!m_apcSlicePilot->getSliceCurStartCUAddr()==0))
774  {
775    printf ("Warning, the first slice of a picture might have been lost!\n");
776  }
777  // exit when a new picture is found
778  if (m_apcSlicePilot->isNextSlice() && (m_apcSlicePilot->getSliceCurStartCUAddr() == 0 && !m_bFirstSliceInPicture) && !m_bFirstSliceInSequence )
779  {
780    if (m_prevPOC >= m_pocRandomAccess)
781    {
782      m_prevPOC = m_apcSlicePilot->getPOC();
783      return true;
784    }
785    m_prevPOC = m_apcSlicePilot->getPOC();
786  }
787#endif
788  // actual decoding starts here
789  xActivateParameterSets();
790#if !O0223_O0139_IRAP_ALIGN_NO_CONTRAINTS
791  //Note setting O0223_O0139_IRAP_ALIGN_NO_CONTRAINTS to 0 may cause decoder to crash.
792  //When cross_layer_irap_aligned_flag is equal to 0, num_extra_slice_header_bits >=1
793  if(!m_apcSlicePilot->getVPS()->getCrossLayerIrapAlignFlag() )
794  {
795    assert( m_apcSlicePilot->getPPS()->getNumExtraSliceHeaderBits() > 0);
796  }
797  //When cross_layer_irap_aligned_flag is equal to 1, the value of poc_reset_flag shall be equal to 0 
798  if( m_apcSlicePilot->getVPS()->getCrossLayerIrapAlignFlag() )
799  {
800    assert( m_apcSlicePilot->getPocResetFlag() == 0);
801  }
802#endif
803
804#if REPN_FORMAT_IN_VPS
805  // Initialize ILRP if needed, only for the current layer 
806  // ILRP intialization should go along with activation of parameters sets,
807  // although activation of parameter sets itself need not be done for each and every slice!!!
808  xInitILRP(m_apcSlicePilot);
809#endif
810  if (m_apcSlicePilot->isNextSlice()) 
811  {
812    m_prevPOC = m_apcSlicePilot->getPOC();
813#if SVC_EXTENSION
814    curLayerId = m_layerId;
815    m_uiPrevLayerId = m_layerId;
816#endif
817  }
818  m_bFirstSliceInSequence = false;
819#if POC_RESET_FLAG
820  // This operation would do the following:
821  // 1. Update the other picture in the DPB. This should be done only for the first slice of the picture.
822  // 2. Update the value of m_pocCRA.
823  // 3. Reset the POC values at the decoder for the current picture to be zero.
824  // 4. update value of POCLastDisplay
825  if( m_apcSlicePilot->getPocResetFlag() )
826  {
827    if( m_apcSlicePilot->getSliceIdx() == 0 )
828    {
829      Int pocAdjustValue = m_apcSlicePilot->getPOC();
830
831      // If poc reset flag is set to 1, reset all POC for DPB -> basically do it for each slice in the picutre
832      TComList<TComPic*>::iterator  iterPic = m_cListPic.begin(); 
833
834      // Iterate through all picture in DPB
835      while( iterPic != m_cListPic.end() )
836      {
837        TComPic *dpbPic = *iterPic;
838        // Check if the picture pointed to by iterPic is either used for reference or
839        // needed for output, are in the same layer, and not the current picture.
840        if( /*  ( ( dpbPic->getSlice(0)->isReferenced() ) || ( dpbPic->getOutputMark() ) )
841            &&*/ ( dpbPic->getLayerId() == m_apcSlicePilot->getLayerId() )
842              && ( dpbPic->getReconMark() ) 
843          )
844        {
845          for(Int i = dpbPic->getNumAllocatedSlice()-1; i >= 0; i--)
846          {
847
848            TComSlice *slice = dpbPic->getSlice(i);
849            TComReferencePictureSet *rps = slice->getRPS();
850            slice->setPOC( slice->getPOC() - pocAdjustValue );
851
852            // Also adjust the POC value stored in the RPS of each such slice
853            for(Int j = rps->getNumberOfPictures(); j >= 0; j--)
854            {
855              rps->setPOC( j, rps->getPOC(j) - pocAdjustValue );
856            }
857            // Also adjust the value of refPOC
858            for(Int k = 0; k < 2; k++)  // For List 0 and List 1
859            {
860              RefPicList list = (k == 1) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
861              for(Int j = 0; j < slice->getNumRefIdx(list); j++)
862              {
863                slice->setRefPOC( slice->getRefPOC(list, j) - pocAdjustValue, list, j);
864              }
865            }
866          }
867        }
868        iterPic++;
869      }
870      // Update the value of pocCRA
871      m_pocCRA -= pocAdjustValue;
872      // Update value of POCLastDisplay
873      iPOCLastDisplay -= pocAdjustValue;
874    }
875    // Reset current poc for current slice and RPS
876    m_apcSlicePilot->setPOC( 0 );
877  }
878#endif
879#if ALIGN_TSA_STSA_PICS
880  if( m_apcSlicePilot->getLayerId() > 0 )
881  {
882    // Check for TSA alignment
883    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
884        m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R
885         )
886    {
887      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
888      {
889        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
890        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() );
891        if( refpicLayer )
892        {
893          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
894                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R );    // TSA pictures should be aligned among depenedent layers
895        } 
896      }
897    }
898    // Check for STSA alignment
899    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
900         m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R
901         )
902    {
903      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
904      {
905        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
906        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() ); // STSA pictures should be aligned among dependent layers
907        if( refpicLayer )
908
909        {
910          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
911                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R );
912        }
913      }
914    }
915  }
916#endif
917  //detect lost reference picture and insert copy of earlier frame.
918  Int lostPoc;
919  while((lostPoc=m_apcSlicePilot->checkThatAllRefPicsAreAvailable(m_cListPic, m_apcSlicePilot->getRPS(), true, m_pocRandomAccess)) > 0)
920  {
921    xCreateLostPicture(lostPoc-1);
922  }
923  if (m_bFirstSliceInPicture)
924  {
925#if AVC_BASE
926    if( m_layerId == 1 && m_parameterSetManagerDecoder[0].getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
927    {
928      TComPic* pBLPic = (*m_ppcTDecTop[0]->getListPic()->begin());
929      fstream* pFile  = m_ppcTDecTop[0]->getBLReconFile();
930      UInt uiWidth    = pBLPic->getPicYuvRec()->getWidth();
931      UInt uiHeight   = pBLPic->getPicYuvRec()->getHeight();
932
933      if( pFile->good() )
934      {
935        UInt64 uiPos = (UInt64) m_apcSlicePilot->getPOC() * uiWidth * uiHeight * 3 / 2;
936
937        pFile->seekg((UInt)uiPos, ios::beg );
938
939        Pel* pPel = pBLPic->getPicYuvRec()->getLumaAddr();
940        UInt uiStride = pBLPic->getPicYuvRec()->getStride();
941        for( Int i = 0; i < uiHeight; i++ )
942        {
943          for( Int j = 0; j < uiWidth; j++ )
944          {
945            pPel[j] = pFile->get();
946          }
947          pPel += uiStride;
948        }
949
950        pPel = pBLPic->getPicYuvRec()->getCbAddr();
951        uiStride = pBLPic->getPicYuvRec()->getCStride();
952        for( Int i = 0; i < uiHeight/2; i++ )
953        {
954          for( Int j = 0; j < uiWidth/2; j++ )
955          {
956            pPel[j] = pFile->get();
957          }
958          pPel += uiStride;
959        }
960
961        pPel = pBLPic->getPicYuvRec()->getCrAddr();
962        uiStride = pBLPic->getPicYuvRec()->getCStride();
963        for( Int i = 0; i < uiHeight/2; i++ )
964        {
965          for( Int j = 0; j < uiWidth/2; j++ )
966          {
967            pPel[j] = pFile->get();
968          }
969          pPel += uiStride;
970        }
971      }
972    }
973#endif
974
975    // Buffer initialize for prediction.
976    m_cPrediction.initTempBuff();
977    m_apcSlicePilot->applyReferencePictureSet(m_cListPic, m_apcSlicePilot->getRPS());
978    //  Get a new picture buffer
979    xGetNewPicBuffer (m_apcSlicePilot, pcPic);
980
981    Bool isField = false;
982    Bool isTff = false;
983   
984    if(!m_SEIs.empty())
985    {
986      // Check if any new Picture Timing SEI has arrived
987      SEIMessages pictureTimingSEIs = extractSeisByType (m_SEIs, SEI::PICTURE_TIMING);
988      if (pictureTimingSEIs.size()>0)
989      {
990        SEIPictureTiming* pictureTiming = (SEIPictureTiming*) *(pictureTimingSEIs.begin());
991        isField = (pictureTiming->m_picStruct == 1) || (pictureTiming->m_picStruct == 2);
992        isTff =  (pictureTiming->m_picStruct == 1);
993      }
994    }
995   
996    //Set Field/Frame coding mode
997    m_pcPic->setField(isField);
998    m_pcPic->setTopField(isTff);
999
1000    // transfer any SEI messages that have been received to the picture
1001    pcPic->setSEIs(m_SEIs);
1002    m_SEIs.clear();
1003
1004    // Recursive structure
1005    m_cCuDecoder.create ( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight );
1006#if SVC_EXTENSION
1007    m_cCuDecoder.init   ( m_ppcTDecTop,&m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction, curLayerId );
1008#else
1009    m_cCuDecoder.init   ( &m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction );
1010#endif
1011    m_cTrQuant.init     ( g_uiMaxCUWidth, g_uiMaxCUHeight, m_apcSlicePilot->getSPS()->getMaxTrSize());
1012
1013    m_cSliceDecoder.create();
1014  }
1015  else
1016  {
1017    // Check if any new SEI has arrived
1018    if(!m_SEIs.empty())
1019    {
1020      // Currently only decoding Unit SEI message occurring between VCL NALUs copied
1021      SEIMessages &picSEI = pcPic->getSEIs();
1022      SEIMessages decodingUnitInfos = extractSeisByType (m_SEIs, SEI::DECODING_UNIT_INFO);
1023      picSEI.insert(picSEI.end(), decodingUnitInfos.begin(), decodingUnitInfos.end());
1024      deleteSEIs(m_SEIs);
1025    }
1026  }
1027 
1028  //  Set picture slice pointer
1029  TComSlice*  pcSlice = m_apcSlicePilot;
1030  Bool bNextSlice     = pcSlice->isNextSlice();
1031
1032  UInt uiCummulativeTileWidth;
1033  UInt uiCummulativeTileHeight;
1034  UInt i, j, p;
1035
1036  //set NumColumnsMins1 and NumRowsMinus1
1037  pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getPPS()->getNumColumnsMinus1() );
1038  pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getPPS()->getNumRowsMinus1() );
1039
1040  //create the TComTileArray
1041  pcPic->getPicSym()->xCreateTComTileArray();
1042
1043  if( pcSlice->getPPS()->getUniformSpacingFlag() )
1044  {
1045    //set the width for each tile
1046    for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
1047    {
1048      for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++)
1049      {
1050        pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->
1051          setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1) 
1052          - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) );
1053      }
1054    }
1055
1056    //set the height for each tile
1057    for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
1058    {
1059      for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++)
1060      {
1061        pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->
1062          setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1) 
1063          - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) );   
1064      }
1065    }
1066  }
1067  else
1068  {
1069    //set the width for each tile
1070    for(j=0; j < pcSlice->getPPS()->getNumRowsMinus1()+1; j++)
1071    {
1072      uiCummulativeTileWidth = 0;
1073      for(i=0; i < pcSlice->getPPS()->getNumColumnsMinus1(); i++)
1074      {
1075        pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcSlice->getPPS()->getColumnWidth(i) );
1076        uiCummulativeTileWidth += pcSlice->getPPS()->getColumnWidth(i);
1077      }
1078      pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth );
1079    }
1080
1081    //set the height for each tile
1082    for(j=0; j < pcSlice->getPPS()->getNumColumnsMinus1()+1; j++)
1083    {
1084      uiCummulativeTileHeight = 0;
1085      for(i=0; i < pcSlice->getPPS()->getNumRowsMinus1(); i++)
1086      { 
1087        pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcSlice->getPPS()->getRowHeight(i) );
1088        uiCummulativeTileHeight += pcSlice->getPPS()->getRowHeight(i);
1089      }
1090      pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight );
1091    }
1092  }
1093
1094  pcPic->getPicSym()->xInitTiles();
1095
1096  //generate the Coding Order Map and Inverse Coding Order Map
1097  UInt uiEncCUAddr;
1098  for(i=0, uiEncCUAddr=0; i<pcPic->getPicSym()->getNumberOfCUsInFrame(); i++, uiEncCUAddr = pcPic->getPicSym()->xCalculateNxtCUAddr(uiEncCUAddr))
1099  {
1100    pcPic->getPicSym()->setCUOrderMap(i, uiEncCUAddr);
1101    pcPic->getPicSym()->setInverseCUOrderMap(uiEncCUAddr, i);
1102  }
1103  pcPic->getPicSym()->setCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
1104  pcPic->getPicSym()->setInverseCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
1105
1106  //convert the start and end CU addresses of the slice and dependent slice into encoding order
1107  pcSlice->setSliceSegmentCurStartCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurStartCUAddr()) );
1108  pcSlice->setSliceSegmentCurEndCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurEndCUAddr()) );
1109  if(pcSlice->isNextSlice())
1110  {
1111    pcSlice->setSliceCurStartCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurStartCUAddr()));
1112    pcSlice->setSliceCurEndCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurEndCUAddr()));
1113  }
1114
1115  if (m_bFirstSliceInPicture) 
1116  {
1117    if(pcPic->getNumAllocatedSlice() != 1)
1118    {
1119      pcPic->clearSliceBuffer();
1120    }
1121  }
1122  else
1123  {
1124    pcPic->allocateNewSlice();
1125  }
1126  assert(pcPic->getNumAllocatedSlice() == (m_uiSliceIdx + 1));
1127  m_apcSlicePilot = pcPic->getPicSym()->getSlice(m_uiSliceIdx); 
1128  pcPic->getPicSym()->setSlice(pcSlice, m_uiSliceIdx);
1129
1130  pcPic->setTLayer(nalu.m_temporalId);
1131
1132#if SVC_EXTENSION
1133  pcPic->setLayerId(nalu.m_layerId);
1134  pcSlice->setLayerId(nalu.m_layerId);
1135  pcSlice->setPic(pcPic);
1136#endif
1137
1138  if (bNextSlice)
1139  {
1140    pcSlice->checkCRA(pcSlice->getRPS(), m_pocCRA, m_associatedIRAPType, m_cListPic );
1141    // Set reference list
1142#if SVC_EXTENSION
1143    if (m_layerId == 0)
1144#endif
1145#if FIX1071
1146    pcSlice->setRefPicList( m_cListPic, true );
1147#else
1148    pcSlice->setRefPicList( m_cListPic );
1149#endif
1150
1151#if SVC_EXTENSION
1152    // Create upsampling reference layer pictures for all possible dependent layers and do it only once for the first slice.
1153    // Other slices might choose which reference pictures to be used for inter-layer prediction
1154    if( m_layerId > 0 && m_uiSliceIdx == 0 )
1155    {     
1156      for( i = 0; i < pcSlice->getNumILRRefIdx(); i++ )
1157      {
1158        UInt refLayerIdc = i;
1159#if AVC_BASE
1160        if( pcSlice->getVPS()->getRefLayerId( m_layerId, refLayerIdc ) == 0 && m_parameterSetManagerDecoder[0].getActiveVPS()->getAvcBaseLayerFlag() )
1161        {
1162          pcSlice->setBaseColPic ( refLayerIdc, *m_ppcTDecTop[0]->getListPic()->begin() );
1163#if AVC_SYNTAX
1164          TComPic* pBLPic = pcSlice->getBaseColPic(refLayerIdc);
1165          if( pcSlice->getPOC() == 0 )
1166          {
1167            // initialize partition order.
1168            UInt* piTmp = &g_auiZscanToRaster[0];
1169            initZscanToRaster( pBLPic->getPicSym()->getMaxDepth() + 1, 1, 0, piTmp );
1170            initRasterToZscan( pBLPic->getPicSym()->getMaxCUWidth(), pBLPic->getPicSym()->getMaxCUHeight(), pBLPic->getPicSym()->getMaxDepth() + 1 );
1171          }     
1172          pBLPic->getSlice( 0 )->initBaseLayerRPL( pcSlice );
1173          pBLPic->readBLSyntax( m_ppcTDecTop[0]->getBLSyntaxFile(), SYNTAX_BYTES );
1174#endif
1175        }
1176        else
1177        {
1178#if VPS_EXTN_DIRECT_REF_LAYERS
1179          TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1180#else
1181          TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1182#endif
1183          TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1184          pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1185        }
1186#else
1187#if VPS_EXTN_DIRECT_REF_LAYERS
1188        TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1189#else
1190        TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1191#endif
1192        TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1193        pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1194#endif
1195
1196        const Window &scalEL = pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc);
1197
1198        Int widthBL   = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getWidth();
1199        Int heightBL  = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec()->getHeight();
1200
1201        Int widthEL   = pcPic->getPicYuvRec()->getWidth()  - scalEL.getWindowLeftOffset() - scalEL.getWindowRightOffset();
1202        Int heightEL  = pcPic->getPicYuvRec()->getHeight() - scalEL.getWindowTopOffset()  - scalEL.getWindowBottomOffset();
1203
1204        g_mvScalingFactor[refLayerIdc][0] = widthEL  == widthBL  ? 4096 : Clip3(-4096, 4095, ((widthEL  << 8) + (widthBL  >> 1)) / widthBL);
1205        g_mvScalingFactor[refLayerIdc][1] = heightEL == heightBL ? 4096 : Clip3(-4096, 4095, ((heightEL << 8) + (heightBL >> 1)) / heightBL);
1206
1207        g_posScalingFactor[refLayerIdc][0] = ((widthBL  << 16) + (widthEL  >> 1)) / widthEL;
1208        g_posScalingFactor[refLayerIdc][1] = ((heightBL << 16) + (heightEL >> 1)) / heightEL;
1209
1210#if SVC_UPSAMPLING
1211        if( pcPic->isSpatialEnhLayer(refLayerIdc) )
1212        {   
1213#if O0215_PHASE_ALIGNMENT
1214#if O0194_JOINT_US_BITSHIFT
1215          m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc), pcSlice->getVPS()->getPhaseAlignFlag() );
1216#else
1217          m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc), pcSlice->getVPS()->getPhaseAlignFlag() );
1218#endif
1219#else
1220#if O0194_JOINT_US_BITSHIFT
1221          m_cPrediction.upsampleBasePic( pcSlice, refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc) );
1222#else
1223          m_cPrediction.upsampleBasePic( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc), pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec(), pcPic->getPicYuvRec(), pcSlice->getSPS()->getScaledRefLayerWindow(refLayerIdc) );
1224#endif
1225#endif
1226        }
1227        else
1228        {
1229          pcPic->setFullPelBaseRec( refLayerIdc, pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec() );
1230        }
1231        pcSlice->setFullPelBaseRec ( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc) );
1232#endif
1233      }
1234    }
1235
1236    if( m_layerId > 0 && pcSlice->getActiveNumILRRefIdx() )
1237    {
1238      for( i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
1239      {
1240        UInt refLayerIdc = pcSlice->getInterLayerPredLayerIdc(i);
1241#if AVC_BASE
1242        if( pcSlice->getVPS()->getRefLayerId( m_layerId, refLayerIdc ) == 0 && m_parameterSetManagerDecoder[0].getActiveVPS()->getAvcBaseLayerFlag() )
1243        {
1244          pcSlice->setBaseColPic ( refLayerIdc, *m_ppcTDecTop[0]->getListPic()->begin() );
1245#if AVC_SYNTAX
1246          TComPic* pBLPic = pcSlice->getBaseColPic(refLayerIdc);
1247          if( pcSlice->getPOC() == 0 )
1248          {
1249            // initialize partition order.
1250            UInt* piTmp = &g_auiZscanToRaster[0];
1251            initZscanToRaster( pBLPic->getPicSym()->getMaxDepth() + 1, 1, 0, piTmp );
1252            initRasterToZscan( pBLPic->getPicSym()->getMaxCUWidth(), pBLPic->getPicSym()->getMaxCUHeight(), pBLPic->getPicSym()->getMaxDepth() + 1 );
1253          }     
1254          pBLPic->getSlice( 0 )->initBaseLayerRPL( pcSlice );
1255          pBLPic->readBLSyntax( m_ppcTDecTop[0]->getBLSyntaxFile(), SYNTAX_BYTES );
1256#endif
1257        }
1258        else
1259        {
1260#if VPS_EXTN_DIRECT_REF_LAYERS
1261          TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1262#else
1263          TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1264#endif
1265          TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1266          pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1267        }
1268#else
1269#if VPS_EXTN_DIRECT_REF_LAYERS
1270        TDecTop *pcTDecTop = (TDecTop *)getRefLayerDec( refLayerIdc );
1271#else
1272        TDecTop *pcTDecTop = (TDecTop *)getLayerDec( m_layerId-1 );
1273#endif
1274        TComList<TComPic*> *cListPic = pcTDecTop->getListPic();
1275        pcSlice->setBaseColPic ( *cListPic, refLayerIdc );
1276#endif
1277
1278        pcSlice->setFullPelBaseRec ( refLayerIdc, pcPic->getFullPelBaseRec(refLayerIdc) );
1279      }
1280
1281      pcSlice->setILRPic( m_cIlpPic );
1282
1283#if REF_IDX_MFM
1284#if M0457_COL_PICTURE_SIGNALING
1285      if( pcSlice->getMFMEnabledFlag() )
1286#else
1287      if( pcSlice->getSPS()->getMFMEnabledFlag() )
1288#endif
1289      {
1290        pcSlice->setRefPOCListILP(m_ppcTDecTop[m_layerId]->m_cIlpPic, pcSlice->getBaseColPic());
1291#if M0457_COL_PICTURE_SIGNALING && !REMOVE_COL_PICTURE_SIGNALING
1292        pcSlice->setMotionPredIlp(getMotionPredIlp(pcSlice));
1293#endif
1294      }
1295      pcSlice->setRefPicList( m_cListPic, false, m_cIlpPic);
1296    }
1297#if M0040_ADAPTIVE_RESOLUTION_CHANGE
1298    else if ( m_layerId > 0 )
1299    {
1300      pcSlice->setRefPicList( m_cListPic, false, NULL);
1301    }
1302#endif
1303#if MFM_ENCCONSTRAINT
1304    if( pcSlice->getMFMEnabledFlag() )
1305    {
1306      Int refLayerId = pcSlice->getRefPic( pcSlice->getSliceType() == B_SLICE ? ( RefPicList )( 1 - pcSlice->getColFromL0Flag() ) : REF_PIC_LIST_0 , pcSlice->getColRefIdx() )->getLayerId();
1307      if( refLayerId != pcSlice->getLayerId() )
1308      {
1309        TComPic* pColBasePic = pcSlice->getBaseColPic( *m_ppcTDecTop[refLayerId]->getListPic() );
1310        assert( pColBasePic->checkSameRefInfo() == true );
1311      }
1312    }
1313#endif
1314#endif
1315   
1316#if N0147_IRAP_ALIGN_FLAG
1317    if(  m_layerId > 0 && pcSlice->getVPS()->getCrossLayerIrapAlignFlag())
1318    {
1319      for(Int dependentLayerIdx = 0; dependentLayerIdx < pcSlice->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
1320      {
1321        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
1322        TComPic* refpicLayer = pcSlice->getRefPic(*cListPic, pcSlice->getPOC() );
1323        if(refpicLayer && pcSlice->isIRAP())
1324        {                 
1325          assert(pcSlice->getNalUnitType() == refpicLayer->getSlice(0)->getNalUnitType());
1326        }
1327      }
1328    }
1329#endif
1330#endif //SVC_EXTENSION
1331   
1332    // For generalized B
1333    // note: maybe not existed case (always L0 is copied to L1 if L1 is empty)
1334    if (pcSlice->isInterB() && pcSlice->getNumRefIdx(REF_PIC_LIST_1) == 0)
1335    {
1336      Int iNumRefIdx = pcSlice->getNumRefIdx(REF_PIC_LIST_0);
1337      pcSlice->setNumRefIdx        ( REF_PIC_LIST_1, iNumRefIdx );
1338
1339      for (Int iRefIdx = 0; iRefIdx < iNumRefIdx; iRefIdx++)
1340      {
1341        pcSlice->setRefPic(pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx), REF_PIC_LIST_1, iRefIdx);
1342      }
1343    }
1344    if (!pcSlice->isIntra())
1345    {
1346      Bool bLowDelay = true;
1347      Int  iCurrPOC  = pcSlice->getPOC();
1348      Int iRefIdx = 0;
1349
1350      for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
1351      {
1352        if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
1353        {
1354          bLowDelay = false;
1355        }
1356      }
1357      if (pcSlice->isInterB())
1358      {
1359        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
1360        {
1361          if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
1362          {
1363            bLowDelay = false;
1364          }
1365        }       
1366      }
1367
1368      pcSlice->setCheckLDC(bLowDelay);           
1369    }
1370
1371    //---------------
1372    pcSlice->setRefPOCList();
1373  }
1374
1375  pcPic->setCurrSliceIdx(m_uiSliceIdx);
1376  if(pcSlice->getSPS()->getScalingListFlag())
1377  {
1378#if IL_SL_SIGNALLING_N0371
1379    pcSlice->getSPS()->getScalingList()->setLayerId( m_layerId );
1380#endif
1381
1382    pcSlice->setScalingList ( pcSlice->getSPS()->getScalingList()  );
1383
1384    if(pcSlice->getPPS()->getScalingListPresentFlag())
1385    {
1386#if IL_SL_SIGNALLING_N0371
1387      pcSlice->getPPS()->getScalingList()->setLayerId( m_layerId );
1388#endif
1389
1390      pcSlice->setScalingList ( pcSlice->getPPS()->getScalingList()  );
1391    }
1392    if(!pcSlice->getPPS()->getScalingListPresentFlag() && !pcSlice->getSPS()->getScalingListPresentFlag())
1393    {
1394#if IL_SL_SIGNALLING_N0371
1395      pcSlice->setDefaultScalingList( m_layerId );
1396#else
1397      pcSlice->setDefaultScalingList();
1398#endif
1399    }
1400    m_cTrQuant.setScalingListDec(pcSlice->getScalingList());
1401    m_cTrQuant.setUseScalingList(true);
1402  }
1403  else
1404  {
1405#if IL_SL_SIGNALLING_N0371
1406    m_cTrQuant.setFlatScalingList( m_layerId );
1407#else
1408    m_cTrQuant.setFlatScalingList();
1409#endif
1410    m_cTrQuant.setUseScalingList(false);
1411  }
1412
1413  //  Decode a picture
1414  m_cGopDecoder.decompressSlice(nalu.m_Bitstream, pcPic);
1415
1416  m_bFirstSliceInPicture = false;
1417  m_uiSliceIdx++;
1418
1419  return false;
1420}
1421
1422Void TDecTop::xDecodeVPS()
1423{
1424  TComVPS* vps = new TComVPS();
1425 
1426  m_cEntropyDecoder.decodeVPS( vps );
1427#if SVC_EXTENSION
1428  m_parameterSetManagerDecoder[0].storePrefetchedVPS(vps);
1429#else
1430  m_parameterSetManagerDecoder.storePrefetchedVPS(vps); 
1431#endif
1432}
1433
1434Void TDecTop::xDecodeSPS()
1435{
1436  TComSPS* sps = new TComSPS();
1437#if SVC_EXTENSION
1438  sps->setLayerId(m_layerId);
1439  m_cEntropyDecoder.decodeSPS( sps, &m_parameterSetManagerDecoder[0] );
1440  m_parameterSetManagerDecoder[m_layerId].storePrefetchedSPS(sps);
1441#if !REPN_FORMAT_IN_VPS   // ILRP can only be initialized at activation 
1442  if(m_numLayer>0)
1443  {
1444    xInitILRP(sps);
1445  }
1446#endif
1447#else //SVC_EXTENSION
1448  m_cEntropyDecoder.decodeSPS( sps );
1449  m_parameterSetManagerDecoder.storePrefetchedSPS(sps);
1450#endif //SVC_EXTENSION
1451}
1452
1453Void TDecTop::xDecodePPS()
1454{
1455  TComPPS* pps = new TComPPS();
1456
1457#if IL_SL_SIGNALLING_N0371
1458  pps->setLayerId(m_layerId);
1459#endif
1460
1461  m_cEntropyDecoder.decodePPS( pps );
1462#if SVC_EXTENSION
1463  m_parameterSetManagerDecoder[m_layerId].storePrefetchedPPS( pps );
1464#else
1465  m_parameterSetManagerDecoder.storePrefetchedPPS( pps );
1466#endif
1467
1468  if( pps->getDependentSliceSegmentsEnabledFlag() )
1469  {
1470    Int NumCtx = pps->getEntropyCodingSyncEnabledFlag()?2:1;
1471    m_cSliceDecoder.initCtxMem(NumCtx);
1472    for ( UInt st = 0; st < NumCtx; st++ )
1473    {
1474      TDecSbac* ctx = NULL;
1475      ctx = new TDecSbac;
1476      ctx->init( &m_cBinCABAC );
1477      m_cSliceDecoder.setCtxMem( ctx, st );
1478    }
1479  }
1480}
1481
1482Void TDecTop::xDecodeSEI( TComInputBitstream* bs, const NalUnitType nalUnitType )
1483{
1484#if SVC_EXTENSION
1485  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
1486  {
1487#if RANDOM_ACCESS_SEI_FIX
1488    if (m_prevSliceSkipped) // No need to decode SEI messages of a skipped access unit
1489    {
1490      return;
1491    }
1492#endif
1493#if M0043_LAYERS_PRESENT_SEI
1494    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder[m_layerId].getActiveVPS(), m_parameterSetManagerDecoder[m_layerId].getActiveSPS() );
1495#else
1496    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder[m_layerId].getActiveSPS() );
1497#endif
1498  }
1499  else
1500  {
1501#if M0043_LAYERS_PRESENT_SEI
1502    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder[m_layerId].getActiveVPS(), m_parameterSetManagerDecoder[m_layerId].getActiveSPS() );
1503#else
1504    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder[m_layerId].getActiveSPS() );
1505#endif
1506    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
1507    if (activeParamSets.size()>0)
1508    {
1509      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
1510      m_parameterSetManagerDecoder[m_layerId].applyPrefetchedPS();
1511      assert(seiAps->activeSeqParamSetId.size()>0);
1512      if (! m_parameterSetManagerDecoder[m_layerId].activateSPSWithSEI(seiAps->activeSeqParamSetId[0] ))
1513      {
1514        printf ("Warning SPS activation with Active parameter set SEI failed");
1515      }
1516    }
1517  }
1518#else
1519  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
1520  {
1521#if M0043_LAYERS_PRESENT_SEI
1522    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
1523#else
1524    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1525#endif
1526  }
1527  else
1528  {
1529#if M0043_LAYERS_PRESENT_SEI
1530    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveVPS(), m_parameterSetManagerDecoder.getActiveSPS() );
1531#else
1532    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1533#endif
1534    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
1535    if (activeParamSets.size()>0)
1536    {
1537      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
1538      m_parameterSetManagerDecoder.applyPrefetchedPS();
1539      assert(seiAps->activeSeqParamSetId.size()>0);
1540      if (! m_parameterSetManagerDecoder.activateSPSWithSEI(seiAps->activeSeqParamSetId[0] ))
1541      {
1542        printf ("Warning SPS activation with Active parameter set SEI failed");
1543      }
1544    }
1545  }
1546#endif
1547}
1548
1549#if SVC_EXTENSION
1550Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay, UInt& curLayerId, Bool& bNewPOC)
1551#else
1552Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay)
1553#endif
1554{
1555  // Initialize entropy decoder
1556  m_cEntropyDecoder.setEntropyDecoder (&m_cCavlcDecoder);
1557  m_cEntropyDecoder.setBitstream      (nalu.m_Bitstream);
1558
1559  switch (nalu.m_nalUnitType)
1560  {
1561    case NAL_UNIT_VPS:
1562#if VPS_NUH_LAYER_ID
1563      assert( nalu.m_layerId == 0 ); // Non-conforming bitstream. The value of nuh_layer_id of VPS NAL unit shall be equal to 0.
1564#endif
1565      xDecodeVPS();
1566#if AVC_BASE
1567      if( m_parameterSetManagerDecoder[0].getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
1568      {
1569        if( !m_ppcTDecTop[0]->getBLReconFile()->good() )
1570        {
1571          printf( "Base layer YUV input reading error\n" );
1572          exit(EXIT_FAILURE);
1573        }       
1574#if AVC_SYNTAX
1575        if( !m_ppcTDecTop[0]->getBLSyntaxFile()->good() )
1576        {
1577          printf( "Base layer syntax input reading error\n" );
1578          exit(EXIT_FAILURE);
1579        }
1580#endif
1581      }
1582      else
1583      {
1584        TComList<TComPic*> *cListPic = m_ppcTDecTop[0]->getListPic();
1585        cListPic->clear();
1586      }
1587#endif
1588      return false;
1589     
1590    case NAL_UNIT_SPS:
1591      xDecodeSPS();
1592#if AVC_BASE
1593      if( m_parameterSetManagerDecoder[0].getPrefetchedVPS(0)->getAvcBaseLayerFlag() )
1594      {
1595        TComPic* pBLPic = (*m_ppcTDecTop[0]->getListPic()->begin());
1596        if( nalu.m_layerId == 1 && pBLPic->getPicYuvRec() == NULL )
1597        {
1598          // using EL SPS with spsId = 1
1599          TComSPS* sps = m_parameterSetManagerDecoder[nalu.m_layerId].getPrefetchedSPS(1);
1600          Int  numReorderPics[MAX_TLAYER];
1601          Window &conformanceWindow = sps->getConformanceWindow();
1602          Window defaultDisplayWindow = sps->getVuiParametersPresentFlag() ? sps->getVuiParameters()->getDefaultDisplayWindow() : Window();
1603#if AUXILIARY_PICTURES
1604#if SVC_UPSAMPLING
1605#if AVC_SYNTAX
1606          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getChromaFormatIdc(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, sps, true);
1607#else
1608          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getChromaFormatIdc(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, NULL, true);
1609#endif
1610#else
1611          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getChromaFormatIdc(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), onformanceWindow, defaultDisplayWindow, numReorderPics, true);
1612#endif
1613#else
1614#if SVC_UPSAMPLING
1615#if AVC_SYNTAX
1616          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, sps, true);
1617#else
1618          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), conformanceWindow, defaultDisplayWindow, numReorderPics, NULL, true);
1619#endif
1620#else
1621          pBLPic->create( m_ppcTDecTop[0]->getBLWidth(), m_ppcTDecTop[0]->getBLHeight(), sps->getMaxCUWidth(), sps->getMaxCUHeight(), sps->getMaxCUDepth(), onformanceWindow, defaultDisplayWindow, numReorderPics, true);
1622#endif
1623#endif
1624
1625#if O0194_DIFFERENT_BITDEPTH_EL_BL
1626          // set AVC BL bit depth, can be an input parameter from the command line
1627          g_bitDepthYLayer[0] = 8;
1628          g_bitDepthCLayer[0] = 8;
1629#endif
1630        }
1631      }
1632#endif
1633      return false;
1634
1635    case NAL_UNIT_PPS:
1636      xDecodePPS();
1637      return false;
1638     
1639    case NAL_UNIT_PREFIX_SEI:
1640    case NAL_UNIT_SUFFIX_SEI:
1641      xDecodeSEI( nalu.m_Bitstream, nalu.m_nalUnitType );
1642      return false;
1643
1644    case NAL_UNIT_CODED_SLICE_TRAIL_R:
1645    case NAL_UNIT_CODED_SLICE_TRAIL_N:
1646    case NAL_UNIT_CODED_SLICE_TLA_R:
1647    case NAL_UNIT_CODED_SLICE_TSA_N:
1648    case NAL_UNIT_CODED_SLICE_STSA_R:
1649    case NAL_UNIT_CODED_SLICE_STSA_N:
1650    case NAL_UNIT_CODED_SLICE_BLA_W_LP:
1651    case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
1652    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
1653    case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
1654    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
1655    case NAL_UNIT_CODED_SLICE_CRA:
1656    case NAL_UNIT_CODED_SLICE_RADL_N:
1657    case NAL_UNIT_CODED_SLICE_RADL_R:
1658    case NAL_UNIT_CODED_SLICE_RASL_N:
1659    case NAL_UNIT_CODED_SLICE_RASL_R:
1660#if SVC_EXTENSION
1661      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay, curLayerId, bNewPOC);
1662#else
1663      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay);
1664#endif
1665      break;
1666    default:
1667      assert (1);
1668  }
1669
1670  return false;
1671}
1672
1673/** Function for checking if picture should be skipped because of association with a previous BLA picture
1674 * \param iPOCLastDisplay POC of last picture displayed
1675 * \returns true if the picture should be skipped
1676 * This function skips all TFD pictures that follow a BLA picture
1677 * in decoding order and precede it in output order.
1678 */
1679Bool TDecTop::isSkipPictureForBLA(Int& iPOCLastDisplay)
1680{
1681  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) && 
1682       m_apcSlicePilot->getPOC() < m_pocCRA && (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N))
1683  {
1684    iPOCLastDisplay++;
1685    return true;
1686  }
1687  return false;
1688}
1689
1690/** Function for checking if picture should be skipped because of random access
1691 * \param iSkipFrame skip frame counter
1692 * \param iPOCLastDisplay POC of last picture displayed
1693 * \returns true if the picture shold be skipped in the random access.
1694 * This function checks the skipping of pictures in the case of -s option random access.
1695 * All pictures prior to the random access point indicated by the counter iSkipFrame are skipped.
1696 * It also checks the type of Nal unit type at the random access point.
1697 * 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.
1698 * If the random access point is IDR all pictures after the random access point are decoded.
1699 * If the random access point is none of the above, a warning is issues, and decoding of pictures with POC
1700 * equal to or greater than the random access point POC is attempted. For non IDR/CRA/BLA random
1701 * access point there is no guarantee that the decoder will not crash.
1702 */
1703Bool TDecTop::isRandomAccessSkipPicture(Int& iSkipFrame,  Int& iPOCLastDisplay)
1704{
1705  if (iSkipFrame) 
1706  {
1707    iSkipFrame--;   // decrement the counter
1708    return true;
1709  }
1710  else if (m_pocRandomAccess == MAX_INT) // start of random access point, m_pocRandomAccess has not been set yet.
1711  {
1712    if (   m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA
1713        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1714        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
1715        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL )
1716    {
1717      // set the POC random access since we need to skip the reordered pictures in the case of CRA/CRANT/BLA/BLANT.
1718      m_pocRandomAccess = m_apcSlicePilot->getPOC();
1719    }
1720    else if ( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
1721    {
1722      m_pocRandomAccess = -MAX_INT; // no need to skip the reordered pictures in IDR, they are decodable.
1723    }
1724    else 
1725    {
1726      static Bool warningMessage = false;
1727      if(!warningMessage)
1728      {
1729        printf("\nWarning: this is not a valid random access point and the data is discarded until the first CRA picture");
1730        warningMessage = true;
1731      }
1732      return true;
1733    }
1734  }
1735  // skip the reordered pictures, if necessary
1736  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))
1737  {
1738    iPOCLastDisplay++;
1739    return true;
1740  }
1741  // if we reach here, then the picture is not skipped.
1742  return false; 
1743}
1744
1745#if VPS_EXTN_DIRECT_REF_LAYERS
1746TDecTop* TDecTop::getRefLayerDec( UInt refLayerIdc )
1747{
1748  TComVPS* vps = m_parameterSetManagerDecoder[0].getActiveVPS();
1749  if( vps->getNumDirectRefLayers( m_layerId ) <= 0 )
1750  {
1751    return (TDecTop *)getLayerDec( 0 );
1752  }
1753 
1754  return (TDecTop *)getLayerDec( vps->getRefLayerId( m_layerId, refLayerIdc ) );
1755}
1756#endif
1757
1758#if VPS_EXTN_DIRECT_REF_LAYERS && M0457_PREDICTION_INDICATIONS
1759
1760Void TDecTop::setRefLayerParams( TComVPS* vps )
1761{
1762  for(UInt layer = 0; layer < m_numLayer; layer++)
1763  {
1764    TDecTop *decTop = (TDecTop *)getLayerDec(layer);
1765    decTop->setNumSamplePredRefLayers(0);
1766    decTop->setNumMotionPredRefLayers(0);
1767    decTop->setNumDirectRefLayers(0);
1768    for(Int i = 0; i < MAX_VPS_LAYER_ID_PLUS1; i++)
1769    {
1770      decTop->setSamplePredEnabledFlag(i, false);
1771      decTop->setMotionPredEnabledFlag(i, false);
1772      decTop->setSamplePredRefLayerId(i, 0);
1773      decTop->setMotionPredRefLayerId(i, 0);
1774    }
1775    for(Int j = 0; j < layer; j++)
1776    {
1777      if (vps->getDirectDependencyFlag(layer, j))
1778      {
1779        decTop->setRefLayerId(decTop->getNumDirectRefLayers(), vps->getLayerIdInNuh(layer));
1780        decTop->setNumDirectRefLayers(decTop->getNumDirectRefLayers() + 1);
1781
1782        Int samplePredEnabledFlag = (vps->getDirectDependencyType(layer, j) + 1) & 1;
1783        decTop->setSamplePredEnabledFlag(j, samplePredEnabledFlag == 1 ? true : false);
1784        decTop->setNumSamplePredRefLayers(decTop->getNumSamplePredRefLayers() + samplePredEnabledFlag);
1785
1786        Int motionPredEnabledFlag = ((vps->getDirectDependencyType(layer, j) + 1) & 2) >> 1;
1787        decTop->setMotionPredEnabledFlag(j, motionPredEnabledFlag == 1 ? true : false);
1788        decTop->setNumMotionPredRefLayers(decTop->getNumMotionPredRefLayers() + motionPredEnabledFlag);
1789      }
1790    }
1791  }
1792  for ( Int i = 1, mIdx = 0, sIdx = 0; i < m_numLayer; i++ )
1793  {
1794    Int iNuhLId = vps->getLayerIdInNuh(i);
1795    TDecTop *decTop = (TDecTop *)getLayerDec(iNuhLId);
1796    for ( Int j = 0; j < i; j++ )
1797    {
1798      if (decTop->getMotionPredEnabledFlag(j))
1799      {
1800        decTop->setMotionPredRefLayerId(mIdx++, vps->getLayerIdInNuh(j));
1801      }
1802      if (decTop->getSamplePredEnabledFlag(j))
1803      {
1804        decTop->setSamplePredRefLayerId(sIdx++, vps->getLayerIdInNuh(j));
1805      }
1806    }
1807  }
1808}
1809
1810#endif
1811
1812#if M0457_COL_PICTURE_SIGNALING && !REMOVE_COL_PICTURE_SIGNALING
1813TComPic* TDecTop::getMotionPredIlp(TComSlice* pcSlice)
1814{
1815  TComPic* ilpPic = NULL;
1816  Int activeMotionPredReflayerIdx = 0;
1817
1818  for( Int i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
1819  {
1820    UInt refLayerIdc = pcSlice->getInterLayerPredLayerIdc(i);
1821    if( getMotionPredEnabledFlag( pcSlice->getVPS()->getRefLayerId( m_layerId, refLayerIdc ) ) )
1822    {
1823      if (activeMotionPredReflayerIdx == pcSlice->getColRefLayerIdx())
1824      {
1825        ilpPic = m_cIlpPic[refLayerIdc];
1826        break;
1827      }
1828      else
1829      {
1830        activeMotionPredReflayerIdx++;
1831      }
1832    }
1833  }
1834
1835  assert(ilpPic != NULL);
1836
1837  return ilpPic;
1838}
1839#endif
1840
1841//! \}
Note: See TracBrowser for help on using the repository browser.