source: 3DVCSoftware/branches/HTM-9.2-dev0/source/Lib/TLibDecoder/TDecTop.cpp @ 766

Last change on this file since 766 was 766, checked in by tech, 11 years ago

Cleanup part 3

  • Property svn:eol-style set to native
File size: 49.8 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 H_MV
42ParameterSetManagerDecoder TDecTop::m_parameterSetManagerDecoder;
43#endif
44//! \ingroup TLibDecoder
45//! \{
46
47#if H_3D
48CamParsCollector::CamParsCollector()
49: m_bInitialized( false )
50{
51  m_aaiCodedOffset         = new Int* [ MAX_NUM_LAYERS ];
52  m_aaiCodedScale          = new Int* [ MAX_NUM_LAYERS ];
53  m_aiViewId               = new Int  [ MAX_NUM_LAYERS ];
54
55  m_bViewReceived          = new Bool [ MAX_NUM_LAYERS ];
56  for( UInt uiId = 0; uiId < MAX_NUM_LAYERS; uiId++ )
57  {
58    m_aaiCodedOffset      [ uiId ] = new Int [ MAX_NUM_LAYERS ];
59    m_aaiCodedScale       [ uiId ] = new Int [ MAX_NUM_LAYERS ];
60  }
61
62  xCreateLUTs( (UInt)MAX_NUM_LAYERS, (UInt)MAX_NUM_LAYERS, m_adBaseViewShiftLUT, m_aiBaseViewShiftLUT );
63  m_iLog2Precision   = LOG2_DISP_PREC_LUT;
64  m_uiBitDepthForLUT = 8; // fixed
65}
66
67CamParsCollector::~CamParsCollector()
68{
69  for( UInt uiId = 0; uiId < MAX_NUM_LAYERS; uiId++ )
70  {
71    delete [] m_aaiCodedOffset      [ uiId ];
72    delete [] m_aaiCodedScale       [ uiId ];
73  }
74  delete [] m_aaiCodedOffset;
75  delete [] m_aaiCodedScale;
76  delete [] m_aiViewId; 
77  delete [] m_bViewReceived;
78
79  xDeleteArray( m_adBaseViewShiftLUT, MAX_NUM_LAYERS, MAX_NUM_LAYERS, 2 );
80  xDeleteArray( m_aiBaseViewShiftLUT, MAX_NUM_LAYERS, MAX_NUM_LAYERS, 2 );
81}
82
83Void
84CamParsCollector::init( FILE* pCodedScaleOffsetFile )
85{
86  m_bInitialized            = true;
87  m_pCodedScaleOffsetFile   = pCodedScaleOffsetFile;
88  m_uiCamParsCodedPrecision = 0;
89  m_bCamParsVaryOverTime    = false;
90  m_iLastViewIndex             = -1;
91  m_iLastPOC                = -1;
92  m_uiMaxViewIndex             = 0;
93}
94
95Void
96CamParsCollector::xCreateLUTs( UInt uiNumberSourceViews, UInt uiNumberTargetViews, Double****& radLUT, Int****& raiLUT)
97{
98
99  uiNumberSourceViews = std::max( (UInt) 1, uiNumberSourceViews );
100  uiNumberTargetViews = std::max( (UInt) 1, uiNumberTargetViews );
101
102  radLUT         = new Double***[ uiNumberSourceViews ];
103  raiLUT         = new Int   ***[ uiNumberSourceViews ];
104
105  for( UInt uiSourceView = 0; uiSourceView < uiNumberSourceViews; uiSourceView++ )
106  {
107    radLUT        [ uiSourceView ] = new Double**[ uiNumberTargetViews ];
108    raiLUT        [ uiSourceView ] = new Int   **[ uiNumberTargetViews ];
109
110    for( UInt uiTargetView = 0; uiTargetView < uiNumberTargetViews; uiTargetView++ )
111    {
112      radLUT        [ uiSourceView ][ uiTargetView ]      = new Double*[ 2 ];
113      radLUT        [ uiSourceView ][ uiTargetView ][ 0 ] = new Double [ 257 ];
114      radLUT        [ uiSourceView ][ uiTargetView ][ 1 ] = new Double [ 257 ];
115
116      raiLUT        [ uiSourceView ][ uiTargetView ]      = new Int*   [ 2 ];
117      raiLUT        [ uiSourceView ][ uiTargetView ][ 0 ] = new Int    [ 257 ];
118      raiLUT        [ uiSourceView ][ uiTargetView ][ 1 ] = new Int    [ 257 ];
119    }
120  }
121}
122
123Void
124  CamParsCollector::xInitLUTs( UInt uiSourceView, UInt uiTargetView, Int iScale, Int iOffset, Double****& radLUT, Int****& raiLUT)
125{
126  Int     iLog2DivLuma   = m_uiBitDepthForLUT + m_uiCamParsCodedPrecision + 1 - m_iLog2Precision;   AOF( iLog2DivLuma > 0 );
127  Int     iLog2DivChroma = iLog2DivLuma + 1;
128
129  iOffset <<= m_uiBitDepthForLUT;
130
131  Double dScale  = (Double) iScale  / (( Double ) ( 1 << iLog2DivLuma ));
132  Double dOffset = (Double) iOffset / (( Double ) ( 1 << iLog2DivLuma ));
133
134  // offsets including rounding offsets
135  Int64 iOffsetLuma   = iOffset + ( ( 1 << iLog2DivLuma   ) >> 1 );
136  Int64 iOffsetChroma = iOffset + ( ( 1 << iLog2DivChroma ) >> 1 );
137
138
139  for( UInt uiDepthValue = 0; uiDepthValue < 256; uiDepthValue++ )
140  {
141
142    // real-valued look-up tables
143    Double  dShiftLuma      = ( (Double)uiDepthValue * dScale + dOffset ) * Double( 1 << m_iLog2Precision );
144    Double  dShiftChroma    = dShiftLuma / 2;
145    radLUT[ uiSourceView ][ uiTargetView ][ 0 ][ uiDepthValue ] = dShiftLuma;
146    radLUT[ uiSourceView ][ uiTargetView ][ 1 ][ uiDepthValue ] = dShiftChroma;
147
148    // integer-valued look-up tables
149    Int64   iTempScale      = (Int64)uiDepthValue * iScale;
150    Int64   iShiftLuma      = ( iTempScale + iOffsetLuma   ) >> iLog2DivLuma;
151    Int64   iShiftChroma    = ( iTempScale + iOffsetChroma ) >> iLog2DivChroma;
152    raiLUT[ uiSourceView ][ uiTargetView ][ 0 ][ uiDepthValue ] = (Int)iShiftLuma;
153    raiLUT[ uiSourceView ][ uiTargetView ][ 1 ][ uiDepthValue ] = (Int)iShiftChroma;
154  }
155
156  radLUT[ uiSourceView ][ uiTargetView ][ 0 ][ 256 ] = radLUT[ uiSourceView ][ uiTargetView ][ 0 ][ 255 ];
157  radLUT[ uiSourceView ][ uiTargetView ][ 1 ][ 256 ] = radLUT[ uiSourceView ][ uiTargetView ][ 1 ][ 255 ];
158  raiLUT[ uiSourceView ][ uiTargetView ][ 0 ][ 256 ] = raiLUT[ uiSourceView ][ uiTargetView ][ 0 ][ 255 ];
159  raiLUT[ uiSourceView ][ uiTargetView ][ 1 ][ 256 ] = raiLUT[ uiSourceView ][ uiTargetView ][ 1 ][ 255 ];
160}
161
162Void
163CamParsCollector::uninit()
164{
165  m_bInitialized = false;
166}
167
168Void
169CamParsCollector::setSlice( TComSlice* pcSlice )
170{
171
172  if( pcSlice == 0 )
173  {
174    AOF( xIsComplete() );
175    if( m_bCamParsVaryOverTime || m_iLastPOC == 0 )
176    {
177      xOutput( m_iLastPOC );
178    }
179    return;
180  }
181 
182  if ( pcSlice->getIsDepth())
183  {
184    return;
185  }
186
187  Bool  bFirstAU          = ( pcSlice->getPOC()     == 0 );
188  Bool  bFirstSliceInAU   = ( pcSlice->getPOC()     != Int ( m_iLastPOC ) );
189  Bool  bFirstSliceInView = ( pcSlice->getViewIndex()  != UInt( m_iLastViewIndex ) || bFirstSliceInAU );
190
191  AOT(  bFirstSliceInAU  &&   pcSlice->getViewIndex()  != 0 );
192  AOT( !bFirstSliceInAU  &&   pcSlice->getViewIndex()   < UInt( m_iLastViewIndex ) );
193 
194  AOT( !bFirstSliceInAU  &&   pcSlice->getViewIndex()   > UInt( m_iLastViewIndex + 1 ) );
195 
196  AOT( !bFirstAU         &&   pcSlice->getViewIndex()   > m_uiMaxViewIndex );
197
198  if ( !bFirstSliceInView )
199  {
200    if( m_bCamParsVaryOverTime ) // check consistency of slice parameters here
201    {
202      UInt uiViewIndex = pcSlice->getViewIndex();
203      for( UInt uiBaseViewIndex = 0; uiBaseViewIndex < uiViewIndex; uiBaseViewIndex++ )
204      {
205        AOF( m_aaiCodedScale [ uiBaseViewIndex ][ uiViewIndex ] == pcSlice->getCodedScale    () [ uiBaseViewIndex ] );
206        AOF( m_aaiCodedOffset[ uiBaseViewIndex ][ uiViewIndex ] == pcSlice->getCodedOffset   () [ uiBaseViewIndex ] );
207        AOF( m_aaiCodedScale [ uiViewIndex ][ uiBaseViewIndex ] == pcSlice->getInvCodedScale () [ uiBaseViewIndex ] );
208        AOF( m_aaiCodedOffset[ uiViewIndex ][ uiBaseViewIndex ] == pcSlice->getInvCodedOffset() [ uiBaseViewIndex ] );
209      }
210    }
211    return;
212  }
213
214  if( bFirstSliceInAU )
215  {
216    if( !bFirstAU )
217    {
218      AOF( xIsComplete() );
219      xOutput( m_iLastPOC );
220    }
221    ::memset( m_bViewReceived, false, MAX_NUM_LAYERS * sizeof( Bool ) );
222  }
223
224  UInt uiViewIndex                       = pcSlice->getViewIndex();
225  m_bViewReceived[ uiViewIndex ]         = true;
226  if( bFirstAU )
227  {
228    m_uiMaxViewIndex                     = std::max( m_uiMaxViewIndex, uiViewIndex );
229    m_aiViewId[ uiViewIndex ]            = pcSlice->getViewId();
230#if CAM_HLS_F0136_F0045_F0082
231    if( uiViewIndex == 1 )
232    {
233      m_uiCamParsCodedPrecision       = pcSlice->getVPS()->getCamParPrecision     ();
234      m_bCamParsVaryOverTime          = pcSlice->getVPS()->hasCamParInSliceHeader ( uiViewIndex );
235    }
236    else if( uiViewIndex > 1 )
237    {
238      AOF( m_uiCamParsCodedPrecision == pcSlice->getVPS()->getCamParPrecision     () );
239      AOF( m_bCamParsVaryOverTime    == pcSlice->getVPS()->hasCamParInSliceHeader ( uiViewIndex ) );
240    }
241#else
242    if( uiViewIndex == 1 )
243    {
244      m_uiCamParsCodedPrecision       = pcSlice->getSPS()->getCamParPrecision     ();
245      m_bCamParsVaryOverTime          = pcSlice->getSPS()->hasCamParInSliceHeader ();
246    }
247    else if( uiViewIndex > 1 )
248    {
249      AOF( m_uiCamParsCodedPrecision == pcSlice->getSPS()->getCamParPrecision     () );
250      AOF( m_bCamParsVaryOverTime    == pcSlice->getSPS()->hasCamParInSliceHeader () );
251    }
252#endif
253    for( UInt uiBaseIndex = 0; uiBaseIndex < uiViewIndex; uiBaseIndex++ )
254    {
255      if( m_bCamParsVaryOverTime )
256      {
257        m_aaiCodedScale [ uiBaseIndex ][ uiViewIndex ]  = pcSlice->getCodedScale    () [ uiBaseIndex ];
258        m_aaiCodedOffset[ uiBaseIndex ][ uiViewIndex ]  = pcSlice->getCodedOffset   () [ uiBaseIndex ];
259        m_aaiCodedScale [ uiViewIndex ][ uiBaseIndex ]  = pcSlice->getInvCodedScale () [ uiBaseIndex ];
260        m_aaiCodedOffset[ uiViewIndex ][ uiBaseIndex ]  = pcSlice->getInvCodedOffset() [ uiBaseIndex ];
261        xInitLUTs( uiBaseIndex, uiViewIndex, m_aaiCodedScale[ uiBaseIndex ][ uiViewIndex ], m_aaiCodedOffset[ uiBaseIndex ][ uiViewIndex ], m_adBaseViewShiftLUT, m_aiBaseViewShiftLUT);
262        xInitLUTs( uiViewIndex, uiBaseIndex, m_aaiCodedScale[ uiViewIndex ][ uiBaseIndex ], m_aaiCodedOffset[ uiViewIndex ][ uiBaseIndex ], m_adBaseViewShiftLUT, m_aiBaseViewShiftLUT);
263      }
264      else
265      {
266#if CAM_HLS_F0136_F0045_F0082
267        m_aaiCodedScale [ uiBaseIndex ][ uiViewIndex ]  = pcSlice->getVPS()->getCodedScale    (uiViewIndex) [ uiBaseIndex ];
268        m_aaiCodedOffset[ uiBaseIndex ][ uiViewIndex ]  = pcSlice->getVPS()->getCodedOffset   (uiViewIndex) [ uiBaseIndex ];
269        m_aaiCodedScale [ uiViewIndex ][ uiBaseIndex ]  = pcSlice->getVPS()->getInvCodedScale (uiViewIndex) [ uiBaseIndex ];
270        m_aaiCodedOffset[ uiViewIndex ][ uiBaseIndex ]  = pcSlice->getVPS()->getInvCodedOffset(uiViewIndex) [ uiBaseIndex ];
271#else
272        m_aaiCodedScale [ uiBaseIndex ][ uiViewIndex ]  = pcSlice->getSPS()->getCodedScale    () [ uiBaseIndex ];
273        m_aaiCodedOffset[ uiBaseIndex ][ uiViewIndex ]  = pcSlice->getSPS()->getCodedOffset   () [ uiBaseIndex ];
274        m_aaiCodedScale [ uiViewIndex ][ uiBaseIndex ]  = pcSlice->getSPS()->getInvCodedScale () [ uiBaseIndex ];
275        m_aaiCodedOffset[ uiViewIndex ][ uiBaseIndex ]  = pcSlice->getSPS()->getInvCodedOffset() [ uiBaseIndex ];
276#endif
277        xInitLUTs( uiBaseIndex, uiViewIndex, m_aaiCodedScale[ uiBaseIndex ][ uiViewIndex ], m_aaiCodedOffset[ uiBaseIndex ][ uiViewIndex ], m_adBaseViewShiftLUT, m_aiBaseViewShiftLUT );
278        xInitLUTs( uiViewIndex, uiBaseIndex, m_aaiCodedScale[ uiViewIndex ][ uiBaseIndex ], m_aaiCodedOffset[ uiViewIndex ][ uiBaseIndex ], m_adBaseViewShiftLUT, m_aiBaseViewShiftLUT );
279      }
280    }
281  }
282  else
283  {
284    AOF( m_aiViewId[ uiViewIndex ] == pcSlice->getViewId() );
285    if( m_bCamParsVaryOverTime )
286    {
287      for( UInt uiBaseIndex = 0; uiBaseIndex < uiViewIndex; uiBaseIndex++ )
288      {
289        m_aaiCodedScale [ uiBaseIndex ][ uiViewIndex ]  = pcSlice->getCodedScale    () [ uiBaseIndex ];
290        m_aaiCodedOffset[ uiBaseIndex ][ uiViewIndex ]  = pcSlice->getCodedOffset   () [ uiBaseIndex ];
291        m_aaiCodedScale [ uiViewIndex ][ uiBaseIndex ]  = pcSlice->getInvCodedScale () [ uiBaseIndex ];
292        m_aaiCodedOffset[ uiViewIndex ][ uiBaseIndex ]  = pcSlice->getInvCodedOffset() [ uiBaseIndex ];
293
294        xInitLUTs( uiBaseIndex, uiViewIndex, m_aaiCodedScale[ uiBaseIndex ][ uiViewIndex ], m_aaiCodedOffset[ uiBaseIndex ][ uiViewIndex ], m_adBaseViewShiftLUT, m_aiBaseViewShiftLUT );
295        xInitLUTs( uiViewIndex, uiBaseIndex, m_aaiCodedScale[ uiViewIndex ][ uiBaseIndex ], m_aaiCodedOffset[ uiViewIndex ][ uiBaseIndex ], m_adBaseViewShiftLUT, m_aiBaseViewShiftLUT );
296      }
297    }
298  }
299 
300  m_iLastViewIndex = (Int)pcSlice->getViewIndex(); 
301  m_iLastPOC       = (Int)pcSlice->getPOC();
302}
303
304#if H_3D_IV_MERGE
305Void
306CamParsCollector::copyCamParamForSlice( TComSlice* pcSlice )
307{
308#if !CAM_HLS_F0136_F0045_F0082
309  UInt uiViewIndex = pcSlice->getViewIndex();
310
311  pcSlice->getSPS()->initCamParaSPS( uiViewIndex, m_uiCamParsCodedPrecision, m_bCamParsVaryOverTime, m_aaiCodedScale, m_aaiCodedOffset );
312#endif
313  if( m_bCamParsVaryOverTime )
314  {
315    pcSlice->setCamparaSlice( m_aaiCodedScale, m_aaiCodedOffset );
316  }
317}
318#endif
319
320Bool
321CamParsCollector::xIsComplete()
322{
323  for( UInt uiView = 0; uiView <= m_uiMaxViewIndex; uiView++ )
324  {
325    if( m_bViewReceived[ uiView ] == 0 )
326    {
327      return false;
328    }
329  }
330  return true;
331}
332
333Void
334CamParsCollector::xOutput( Int iPOC )
335{
336  if( m_pCodedScaleOffsetFile )
337  {
338    if( iPOC == 0 )
339    {
340      fprintf( m_pCodedScaleOffsetFile, "#  ViewIndex       ViewId\n" );
341      fprintf( m_pCodedScaleOffsetFile, "#----------- ------------\n" );
342      for( UInt uiViewIndex = 0; uiViewIndex <= m_uiMaxViewIndex; uiViewIndex++ )
343      {
344        fprintf( m_pCodedScaleOffsetFile, "%12d %12d\n", uiViewIndex, m_aiViewId[ uiViewIndex ] );
345      }
346      fprintf( m_pCodedScaleOffsetFile, "\n\n");
347      fprintf( m_pCodedScaleOffsetFile, "# StartFrame     EndFrame   TargetView     BaseView   CodedScale  CodedOffset    Precision\n" );
348      fprintf( m_pCodedScaleOffsetFile, "#----------- ------------ ------------ ------------ ------------ ------------ ------------\n" );
349    }
350    if( iPOC == 0 || m_bCamParsVaryOverTime )
351    {
352      Int iS = iPOC;
353      Int iE = ( m_bCamParsVaryOverTime ? iPOC : ~( 1 << 31 ) );
354      for( UInt uiViewIndex = 0; uiViewIndex <= m_uiMaxViewIndex; uiViewIndex++ )
355      {
356        for( UInt uiBaseIndex = 0; uiBaseIndex <= m_uiMaxViewIndex; uiBaseIndex++ )
357        {
358          if( uiViewIndex != uiBaseIndex )
359          {
360            fprintf( m_pCodedScaleOffsetFile, "%12d %12d %12d %12d %12d %12d %12d\n",
361              iS, iE, uiViewIndex, uiBaseIndex, m_aaiCodedScale[ uiBaseIndex ][ uiViewIndex ], m_aaiCodedOffset[ uiBaseIndex ][ uiViewIndex ], m_uiCamParsCodedPrecision );
362          }
363        }
364      }
365    }
366  }
367}
368#endif
369TDecTop::TDecTop()
370{
371  m_pcPic = 0;
372  m_iMaxRefPicNum = 0;
373#if ENC_DEC_TRACE
374#if H_MV
375  if ( g_hTrace == NULL )
376  {
377#endif
378  g_hTrace = fopen( "TraceDec.txt", "wb" );
379  g_bJustDoIt = g_bEncDecTraceDisable;
380  g_nSymbolCounter = 0;
381#if H_MV
382  }
383#endif
384#endif
385  m_associatedIRAPType = NAL_UNIT_INVALID;
386  m_pocCRA = 0;
387  m_pocRandomAccess = MAX_INT; 
388  m_prevPOC                = MAX_INT;
389  m_bFirstSliceInPicture    = true;
390  m_bFirstSliceInSequence   = true;
391  m_prevSliceSkipped = false;
392  m_skippedPOC = 0;
393#if H_MV
394  m_layerId = 0;
395  m_viewId = 0;
396#if H_3D
397  m_viewIndex = 0; 
398  m_isDepth = false;
399  m_pcCamParsCollector = 0;
400#endif
401#endif
402}
403
404TDecTop::~TDecTop()
405{
406#if ENC_DEC_TRACE
407  fclose( g_hTrace );
408#endif
409}
410
411Void TDecTop::create()
412{
413  m_cGopDecoder.create();
414  m_apcSlicePilot = new TComSlice;
415  m_uiSliceIdx = 0;
416}
417
418Void TDecTop::destroy()
419{
420  m_cGopDecoder.destroy();
421 
422  delete m_apcSlicePilot;
423  m_apcSlicePilot = NULL;
424 
425  m_cSliceDecoder.destroy();
426}
427
428Void TDecTop::init()
429{
430  // initialize ROM
431#if !H_MV
432  initROM();
433#endif
434  m_cGopDecoder.init( &m_cEntropyDecoder, &m_cSbacDecoder, &m_cBinCABAC, &m_cCavlcDecoder, &m_cSliceDecoder, &m_cLoopFilter, &m_cSAO );
435  m_cSliceDecoder.init( &m_cEntropyDecoder, &m_cCuDecoder );
436  m_cEntropyDecoder.init(&m_cPrediction);
437}
438
439Void TDecTop::deletePicBuffer ( )
440{
441  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
442  Int iSize = Int( m_cListPic.size() );
443 
444  for (Int i = 0; i < iSize; i++ )
445  {
446    TComPic* pcPic = *(iterPic++);
447#if H_MV
448    if( pcPic )
449    {
450#endif
451    pcPic->destroy();
452   
453    delete pcPic;
454    pcPic = NULL;
455#if H_MV
456    }
457#endif
458  }
459 
460  m_cSAO.destroy();
461 
462  m_cLoopFilter.        destroy();
463 
464#if !H_MV
465  // destroy ROM
466  destroyROM();
467#endif
468}
469
470Void TDecTop::xGetNewPicBuffer ( TComSlice* pcSlice, TComPic*& rpcPic )
471{
472  Int  numReorderPics[MAX_TLAYER];
473  Window &conformanceWindow = pcSlice->getSPS()->getConformanceWindow();
474  Window defaultDisplayWindow = pcSlice->getSPS()->getVuiParametersPresentFlag() ? pcSlice->getSPS()->getVuiParameters()->getDefaultDisplayWindow() : Window();
475
476#if H_MV
477    assert( conformanceWindow   .getScaledFlag() ); 
478    assert( defaultDisplayWindow.getScaledFlag() );
479#endif
480  for( Int temporalLayer=0; temporalLayer < MAX_TLAYER; temporalLayer++) 
481  {
482    numReorderPics[temporalLayer] = pcSlice->getSPS()->getNumReorderPics(temporalLayer);
483  }
484
485  m_iMaxRefPicNum = pcSlice->getSPS()->getMaxDecPicBuffering(pcSlice->getTLayer());     // m_uiMaxDecPicBuffering has the space for the picture currently being decoded
486  if (m_cListPic.size() < (UInt)m_iMaxRefPicNum)
487  {
488    rpcPic = new TComPic();
489   
490    rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth, 
491                     conformanceWindow, defaultDisplayWindow, numReorderPics, true);
492    rpcPic->getPicSym()->allocSaoParam(&m_cSAO);
493    m_cListPic.pushBack( rpcPic );
494   
495    return;
496  }
497 
498  Bool bBufferIsAvailable = false;
499  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
500  while (iterPic != m_cListPic.end())
501  {
502    rpcPic = *(iterPic++);
503    if ( rpcPic->getReconMark() == false && rpcPic->getOutputMark() == false)
504    {
505      rpcPic->setOutputMark(false);
506      bBufferIsAvailable = true;
507      break;
508    }
509
510    if ( rpcPic->getSlice( 0 )->isReferenced() == false  && rpcPic->getOutputMark() == false)
511    {
512      rpcPic->setOutputMark(false);
513      rpcPic->setReconMark( false );
514      rpcPic->getPicYuvRec()->setBorderExtension( false );
515      bBufferIsAvailable = true;
516      break;
517    }
518  }
519 
520  if ( !bBufferIsAvailable )
521  {
522    //There is no room for this picture, either because of faulty encoder or dropped NAL. Extend the buffer.
523    m_iMaxRefPicNum++;
524    rpcPic = new TComPic();
525    m_cListPic.pushBack( rpcPic );
526  }
527  rpcPic->destroy();
528  rpcPic->create ( pcSlice->getSPS()->getPicWidthInLumaSamples(), pcSlice->getSPS()->getPicHeightInLumaSamples(), g_uiMaxCUWidth, g_uiMaxCUHeight, g_uiMaxCUDepth,
529                   conformanceWindow, defaultDisplayWindow, numReorderPics, true);
530  rpcPic->getPicSym()->allocSaoParam(&m_cSAO);
531}
532
533#if H_MV
534Void TDecTop::endPicDecoding(Int& poc, TComList<TComPic*>*& rpcListPic, std::vector<Int>& targetDecLayerIdSet )
535#else
536Void TDecTop::executeLoopFilters(Int& poc, TComList<TComPic*>*& rpcListPic)
537#endif
538{
539  if (!m_pcPic)
540  {
541    /* nothing to deblock */
542    return;
543  }
544 
545  TComPic*&   pcPic         = m_pcPic;
546
547  // Execute Deblock + Cleanup
548
549  m_cGopDecoder.filterPicture(pcPic);
550
551  TComSlice::sortPicList( m_cListPic ); // sorting for application output
552  poc                 = pcPic->getSlice(m_uiSliceIdx-1)->getPOC();
553  rpcListPic          = &m_cListPic; 
554  m_cCuDecoder.destroy();       
555#if H_MV
556  TComSlice::markIvRefPicsAsShortTerm( m_refPicSetInterLayer0, m_refPicSetInterLayer1 ); 
557  TComSlice::markCurrPic( pcPic ); 
558  TComSlice::markIvRefPicsAsUnused   ( m_ivPicLists, targetDecLayerIdSet, m_parameterSetManagerDecoder.getActiveVPS(), m_layerId, poc ); 
559#endif
560  m_bFirstSliceInPicture  = true;
561
562  return;
563}
564
565Void TDecTop::xCreateLostPicture(Int iLostPoc) 
566{
567  printf("\ninserting lost poc : %d\n",iLostPoc);
568  TComSlice cFillSlice;
569  cFillSlice.setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
570  cFillSlice.setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
571  cFillSlice.initSlice();
572  TComPic *cFillPic;
573  xGetNewPicBuffer(&cFillSlice,cFillPic);
574  cFillPic->getSlice(0)->setSPS( m_parameterSetManagerDecoder.getFirstSPS() );
575  cFillPic->getSlice(0)->setPPS( m_parameterSetManagerDecoder.getFirstPPS() );
576  cFillPic->getSlice(0)->initSlice();
577 
578  TComList<TComPic*>::iterator iterPic = m_cListPic.begin();
579  Int closestPoc = 1000000;
580  while ( iterPic != m_cListPic.end())
581  {
582    TComPic * rpcPic = *(iterPic++);
583    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())
584    {
585      closestPoc=abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc);
586    }
587  }
588  iterPic = m_cListPic.begin();
589  while ( iterPic != m_cListPic.end())
590  {
591    TComPic *rpcPic = *(iterPic++);
592    if(abs(rpcPic->getPicSym()->getSlice(0)->getPOC() -iLostPoc)==closestPoc&&rpcPic->getPicSym()->getSlice(0)->getPOC()!=m_apcSlicePilot->getPOC())
593    {
594      printf("copying picture %d to %d (%d)\n",rpcPic->getPicSym()->getSlice(0)->getPOC() ,iLostPoc,m_apcSlicePilot->getPOC());
595      rpcPic->getPicYuvRec()->copyToPic(cFillPic->getPicYuvRec());
596      break;
597    }
598  }
599  cFillPic->setCurrSliceIdx(0);
600  for(Int i=0; i<cFillPic->getNumCUsInFrame(); i++) 
601  {
602    cFillPic->getCU(i)->initCU(cFillPic,i);
603  }
604  cFillPic->getSlice(0)->setReferenced(true);
605  cFillPic->getSlice(0)->setPOC(iLostPoc);
606  cFillPic->setReconMark(true);
607  cFillPic->setOutputMark(true);
608  if(m_pocRandomAccess == MAX_INT)
609  {
610    m_pocRandomAccess = iLostPoc;
611  }
612}
613
614
615Void TDecTop::xActivateParameterSets()
616{
617  m_parameterSetManagerDecoder.applyPrefetchedPS();
618 
619  TComPPS *pps = m_parameterSetManagerDecoder.getPPS(m_apcSlicePilot->getPPSId());
620  assert (pps != 0);
621
622  TComSPS *sps = m_parameterSetManagerDecoder.getSPS(pps->getSPSId());
623  assert (sps != 0);
624
625#if H_MV
626  TComVPS* vps = m_parameterSetManagerDecoder.getVPS(sps->getVPSId());
627  assert (vps != 0); 
628  if (false == m_parameterSetManagerDecoder.activatePPS(m_apcSlicePilot->getPPSId(),m_apcSlicePilot->isIRAP(), m_layerId ) )
629#else
630  if (false == m_parameterSetManagerDecoder.activatePPS(m_apcSlicePilot->getPPSId(),m_apcSlicePilot->isIRAP()))
631#endif
632  {
633    printf ("Parameter set activation failed!");
634    assert (0);
635  }
636
637  if( pps->getDependentSliceSegmentsEnabledFlag() )
638  {
639    Int NumCtx = pps->getEntropyCodingSyncEnabledFlag()?2:1;
640
641    if (m_cSliceDecoder.getCtxMemSize() != NumCtx)
642    {
643      m_cSliceDecoder.initCtxMem(NumCtx);
644      for ( UInt st = 0; st < NumCtx; st++ )
645      {
646        TDecSbac* ctx = NULL;
647        ctx = new TDecSbac;
648        ctx->init( &m_cBinCABAC );
649        m_cSliceDecoder.setCtxMem( ctx, st );
650      }
651    }
652  }
653
654  m_apcSlicePilot->setPPS(pps);
655  m_apcSlicePilot->setSPS(sps);
656#if H_MV
657  m_apcSlicePilot->setVPS(vps); 
658  // The nuh_layer_id value of the NAL unit containing the PPS that is activated for a layer layerA with nuh_layer_id equal to nuhLayerIdA shall be equal to 0, or nuhLayerIdA, or the nuh_layer_id of a direct or indirect reference layer of layerA.
659  assert( pps->getLayerId() == m_layerId || pps->getLayerId( ) == 0 || vps->getInDirectDependencyFlag( m_layerId, pps->getLayerId() ) );   
660  // The nuh_layer_id value of the NAL unit containing the SPS that is activated for a layer layerA with nuh_layer_id equal to nuhLayerIdA shall be equal to 0, or nuhLayerIdA, or the nuh_layer_id of a direct or indirect reference layer of layerA.
661  assert( sps->getLayerId() == m_layerId || sps->getLayerId( ) == 0 || vps->getInDirectDependencyFlag( m_layerId, sps->getLayerId() ) );
662  sps->inferRepFormat  ( vps , m_layerId ); 
663  sps->inferScalingList( m_parameterSetManagerDecoder.getActiveSPS( sps->getSpsScalingListRefLayerId() ) ); 
664#endif
665  pps->setSPS(sps);
666  pps->setNumSubstreams(pps->getEntropyCodingSyncEnabledFlag() ? ((sps->getPicHeightInLumaSamples() + sps->getMaxCUHeight() - 1) / sps->getMaxCUHeight()) * (pps->getNumColumnsMinus1() + 1) : 1);
667  pps->setMinCuDQPSize( sps->getMaxCUWidth() >> ( pps->getMaxCuDQPDepth()) );
668
669  g_bitDepthY     = sps->getBitDepthY();
670  g_bitDepthC     = sps->getBitDepthC();
671  g_uiMaxCUWidth  = sps->getMaxCUWidth();
672  g_uiMaxCUHeight = sps->getMaxCUHeight();
673  g_uiMaxCUDepth  = sps->getMaxCUDepth();
674  g_uiAddCUDepth  = max (0, sps->getLog2MinCodingBlockSize() - (Int)sps->getQuadtreeTULog2MinSize() );
675
676  for (Int i = 0; i < sps->getLog2DiffMaxMinCodingBlockSize(); i++)
677  {
678    sps->setAMPAcc( i, sps->getUseAMP() );
679  }
680
681  for (Int i = sps->getLog2DiffMaxMinCodingBlockSize(); i < sps->getMaxCUDepth(); i++)
682  {
683    sps->setAMPAcc( i, 0 );
684  }
685
686  m_cSAO.destroy();
687  m_cSAO.create( sps->getPicWidthInLumaSamples(), sps->getPicHeightInLumaSamples(), sps->getMaxCUWidth(), sps->getMaxCUHeight() );
688  m_cLoopFilter.create( sps->getMaxCUDepth() );
689}
690
691#if H_MV
692Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay, Bool newLayerFlag, Bool& sliceSkippedFlag  )
693{
694  assert( nalu.m_layerId == m_layerId ); 
695
696#else
697Bool TDecTop::xDecodeSlice(InputNALUnit &nalu, Int &iSkipFrame, Int iPOCLastDisplay )
698{
699#endif
700  TComPic*&   pcPic         = m_pcPic;
701  m_apcSlicePilot->initSlice();
702
703  if (m_bFirstSliceInPicture)
704  {
705    m_uiSliceIdx     = 0;
706  }
707  m_apcSlicePilot->setSliceIdx(m_uiSliceIdx);
708  if (!m_bFirstSliceInPicture)
709  {
710    m_apcSlicePilot->copySliceInfo( pcPic->getPicSym()->getSlice(m_uiSliceIdx-1) );
711  }
712
713  m_apcSlicePilot->setNalUnitType(nalu.m_nalUnitType);
714  Bool nonReferenceFlag = (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TRAIL_N ||
715                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N   ||
716                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N  ||
717                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N  ||
718                           m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N);
719  m_apcSlicePilot->setTemporalLayerNonReferenceFlag(nonReferenceFlag);
720 
721  m_apcSlicePilot->setReferenced(true); // Putting this as true ensures that picture is referenced the first time it is in an RPS
722  m_apcSlicePilot->setTLayerInfo(nalu.m_temporalId);
723
724#if H_MV
725  m_apcSlicePilot->setRefPicSetInterLayer( & m_refPicSetInterLayer0, &m_refPicSetInterLayer1 ); 
726  m_apcSlicePilot->setLayerId( nalu.m_layerId );
727#endif
728  m_cEntropyDecoder.decodeSliceHeader (m_apcSlicePilot, &m_parameterSetManagerDecoder);
729
730  // set POC for dependent slices in skipped pictures
731  if(m_apcSlicePilot->getDependentSliceSegmentFlag() && m_prevSliceSkipped) 
732  {
733    m_apcSlicePilot->setPOC(m_skippedPOC);
734  }
735
736  m_apcSlicePilot->setAssociatedIRAPPOC(m_pocCRA);
737  m_apcSlicePilot->setAssociatedIRAPType(m_associatedIRAPType);
738
739#if H_MV 
740  TComVPS* vps     = m_apcSlicePilot->getVPS();
741  Int layerId  = nalu.m_layerId;   
742  setViewId   ( vps->getViewId   ( layerId )      ); 
743#if H_3D
744  setViewIndex( vps->getViewIndex( layerId )      ); 
745  setIsDepth  ( vps->getDepthId  ( layerId ) == 1 ); 
746  m_ivPicLists->setVPS( vps ); 
747#endif
748#endif
749
750    xCeckNoClrasOutput();
751    // Skip pictures due to random access
752    if (isRandomAccessSkipPicture(iSkipFrame, iPOCLastDisplay))
753    {
754    m_prevSliceSkipped = true;
755    m_skippedPOC = m_apcSlicePilot->getPOC();
756      sliceSkippedFlag = true; 
757      return false;
758    }
759    // Skip TFD pictures associated with BLA/BLANT pictures
760    if (isSkipPictureForBLA(iPOCLastDisplay))
761    {
762    m_prevSliceSkipped = true;
763    m_skippedPOC = m_apcSlicePilot->getPOC();
764      sliceSkippedFlag = true; 
765      return false;
766    }
767
768  // clear previous slice skipped flag
769  m_prevSliceSkipped = false;
770
771  //we should only get a different poc for a new picture (with CTU address==0)
772  if (m_apcSlicePilot->isNextSlice() && m_apcSlicePilot->getPOC()!=m_prevPOC && !m_bFirstSliceInSequence && (!m_apcSlicePilot->getSliceCurStartCUAddr()==0))
773  {
774    printf ("Warning, the first slice of a picture might have been lost!\n");
775  }
776  // exit when a new picture is found
777  if (m_apcSlicePilot->isNextSlice() && (m_apcSlicePilot->getSliceCurStartCUAddr() == 0 && !m_bFirstSliceInPicture) && !m_bFirstSliceInSequence )
778  {
779    if (m_prevPOC >= m_pocRandomAccess)
780    {
781      m_prevPOC = m_apcSlicePilot->getPOC();
782      return true;
783    }
784    m_prevPOC = m_apcSlicePilot->getPOC();
785  }
786#if H_MV
787  if ( newLayerFlag )
788  {
789    return false; 
790  }
791#if ENC_DEC_TRACE
792#if H_MV_ENC_DEC_TRAC
793  // parse remainder of SH
794   g_disableHLSTrace = false; 
795#endif
796#endif
797#endif
798  // actual decoding starts here
799#if H_MV
800   // This part needs further testing !
801   if ( m_apcSlicePilot->getPocResetFlag() )
802   {   
803     xResetPocInPicBuffer();
804   }
805#endif
806  xActivateParameterSets();
807
808  if (m_apcSlicePilot->isNextSlice()) 
809  {
810    m_prevPOC = m_apcSlicePilot->getPOC();
811  }
812  m_bFirstSliceInSequence = false;
813  //detect lost reference picture and insert copy of earlier frame.
814  Int lostPoc;
815  while((lostPoc=m_apcSlicePilot->checkThatAllRefPicsAreAvailable(m_cListPic, m_apcSlicePilot->getRPS(), true, m_pocRandomAccess)) > 0)
816  {
817    xCreateLostPicture(lostPoc-1);
818  }
819  if (m_bFirstSliceInPicture)
820  {
821    // Buffer initialize for prediction.
822    m_cPrediction.initTempBuff();
823    m_apcSlicePilot->applyReferencePictureSet(m_cListPic, m_apcSlicePilot->getRPS());
824#if H_MV
825    m_apcSlicePilot->createInterLayerReferencePictureSet( m_ivPicLists, m_refPicSetInterLayer0, m_refPicSetInterLayer1 ); 
826#endif
827    //  Get a new picture buffer
828    xGetNewPicBuffer (m_apcSlicePilot, pcPic);
829
830    Bool isField = false;
831    Bool isTff = false;
832   
833    if(!m_SEIs.empty())
834    {
835      // Check if any new Picture Timing SEI has arrived
836      SEIMessages pictureTimingSEIs = extractSeisByType (m_SEIs, SEI::PICTURE_TIMING);
837      if (pictureTimingSEIs.size()>0)
838      {
839        SEIPictureTiming* pictureTiming = (SEIPictureTiming*) *(pictureTimingSEIs.begin());
840        isField = (pictureTiming->m_picStruct == 1) || (pictureTiming->m_picStruct == 2);
841        isTff =  (pictureTiming->m_picStruct == 1);
842      }
843    }
844   
845    //Set Field/Frame coding mode
846    m_pcPic->setField(isField);
847    m_pcPic->setTopField(isTff);
848   
849    // transfer any SEI messages that have been received to the picture
850    pcPic->setSEIs(m_SEIs);
851    m_SEIs.clear();
852
853    // Recursive structure
854    m_cCuDecoder.create ( g_uiMaxCUDepth, g_uiMaxCUWidth, g_uiMaxCUHeight );
855    m_cCuDecoder.init   ( &m_cEntropyDecoder, &m_cTrQuant, &m_cPrediction );
856    m_cTrQuant.init     ( g_uiMaxCUWidth, g_uiMaxCUHeight, m_apcSlicePilot->getSPS()->getMaxTrSize());
857
858    m_cSliceDecoder.create();
859  }
860  else
861  {
862    // Check if any new SEI has arrived
863    if(!m_SEIs.empty())
864    {
865      // Currently only decoding Unit SEI message occurring between VCL NALUs copied
866      SEIMessages &picSEI = pcPic->getSEIs();
867      SEIMessages decodingUnitInfos = extractSeisByType (m_SEIs, SEI::DECODING_UNIT_INFO);
868      picSEI.insert(picSEI.end(), decodingUnitInfos.begin(), decodingUnitInfos.end());
869      deleteSEIs(m_SEIs);
870    }
871  }
872 
873  //  Set picture slice pointer
874  TComSlice*  pcSlice = m_apcSlicePilot;
875  Bool bNextSlice     = pcSlice->isNextSlice();
876
877  UInt uiCummulativeTileWidth;
878  UInt uiCummulativeTileHeight;
879  UInt i, j, p;
880
881  //set NumColumnsMins1 and NumRowsMinus1
882  pcPic->getPicSym()->setNumColumnsMinus1( pcSlice->getPPS()->getNumColumnsMinus1() );
883  pcPic->getPicSym()->setNumRowsMinus1( pcSlice->getPPS()->getNumRowsMinus1() );
884
885  //create the TComTileArray
886  pcPic->getPicSym()->xCreateTComTileArray();
887
888  if( pcSlice->getPPS()->getUniformSpacingFlag() )
889  {
890    //set the width for each tile
891    for(j=0; j < pcPic->getPicSym()->getNumRowsMinus1()+1; j++)
892    {
893      for(p=0; p < pcPic->getPicSym()->getNumColumnsMinus1()+1; p++)
894      {
895        pcPic->getPicSym()->getTComTile( j * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + p )->
896          setTileWidth( (p+1)*pcPic->getPicSym()->getFrameWidthInCU()/(pcPic->getPicSym()->getNumColumnsMinus1()+1) 
897          - (p*pcPic->getPicSym()->getFrameWidthInCU())/(pcPic->getPicSym()->getNumColumnsMinus1()+1) );
898      }
899    }
900
901    //set the height for each tile
902    for(j=0; j < pcPic->getPicSym()->getNumColumnsMinus1()+1; j++)
903    {
904      for(p=0; p < pcPic->getPicSym()->getNumRowsMinus1()+1; p++)
905      {
906        pcPic->getPicSym()->getTComTile( p * (pcPic->getPicSym()->getNumColumnsMinus1()+1) + j )->
907          setTileHeight( (p+1)*pcPic->getPicSym()->getFrameHeightInCU()/(pcPic->getPicSym()->getNumRowsMinus1()+1) 
908          - (p*pcPic->getPicSym()->getFrameHeightInCU())/(pcPic->getPicSym()->getNumRowsMinus1()+1) );   
909      }
910    }
911  }
912  else
913  {
914    //set the width for each tile
915    for(j=0; j < pcSlice->getPPS()->getNumRowsMinus1()+1; j++)
916    {
917      uiCummulativeTileWidth = 0;
918      for(i=0; i < pcSlice->getPPS()->getNumColumnsMinus1(); i++)
919      {
920        pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcSlice->getPPS()->getColumnWidth(i) );
921        uiCummulativeTileWidth += pcSlice->getPPS()->getColumnWidth(i);
922      }
923      pcPic->getPicSym()->getTComTile(j * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + i)->setTileWidth( pcPic->getPicSym()->getFrameWidthInCU()-uiCummulativeTileWidth );
924    }
925
926    //set the height for each tile
927    for(j=0; j < pcSlice->getPPS()->getNumColumnsMinus1()+1; j++)
928    {
929      uiCummulativeTileHeight = 0;
930      for(i=0; i < pcSlice->getPPS()->getNumRowsMinus1(); i++)
931      { 
932        pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcSlice->getPPS()->getRowHeight(i) );
933        uiCummulativeTileHeight += pcSlice->getPPS()->getRowHeight(i);
934      }
935      pcPic->getPicSym()->getTComTile(i * (pcSlice->getPPS()->getNumColumnsMinus1()+1) + j)->setTileHeight( pcPic->getPicSym()->getFrameHeightInCU()-uiCummulativeTileHeight );
936    }
937  }
938
939  pcPic->getPicSym()->xInitTiles();
940
941  //generate the Coding Order Map and Inverse Coding Order Map
942  UInt uiEncCUAddr;
943  for(i=0, uiEncCUAddr=0; i<pcPic->getPicSym()->getNumberOfCUsInFrame(); i++, uiEncCUAddr = pcPic->getPicSym()->xCalculateNxtCUAddr(uiEncCUAddr))
944  {
945    pcPic->getPicSym()->setCUOrderMap(i, uiEncCUAddr);
946    pcPic->getPicSym()->setInverseCUOrderMap(uiEncCUAddr, i);
947  }
948  pcPic->getPicSym()->setCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
949  pcPic->getPicSym()->setInverseCUOrderMap(pcPic->getPicSym()->getNumberOfCUsInFrame(), pcPic->getPicSym()->getNumberOfCUsInFrame());
950
951  //convert the start and end CU addresses of the slice and dependent slice into encoding order
952  pcSlice->setSliceSegmentCurStartCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurStartCUAddr()) );
953  pcSlice->setSliceSegmentCurEndCUAddr( pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceSegmentCurEndCUAddr()) );
954  if(pcSlice->isNextSlice())
955  {
956    pcSlice->setSliceCurStartCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurStartCUAddr()));
957    pcSlice->setSliceCurEndCUAddr(pcPic->getPicSym()->getPicSCUEncOrder(pcSlice->getSliceCurEndCUAddr()));
958  }
959
960  if (m_bFirstSliceInPicture) 
961  {
962    if(pcPic->getNumAllocatedSlice() != 1)
963    {
964      pcPic->clearSliceBuffer();
965    }
966  }
967  else
968  {
969    pcPic->allocateNewSlice();
970  }
971  assert(pcPic->getNumAllocatedSlice() == (m_uiSliceIdx + 1));
972  m_apcSlicePilot = pcPic->getPicSym()->getSlice(m_uiSliceIdx); 
973  pcPic->getPicSym()->setSlice(pcSlice, m_uiSliceIdx);
974
975  pcPic->setTLayer(nalu.m_temporalId);
976
977#if H_MV
978  pcPic->setLayerId( nalu.m_layerId );
979  pcPic->setViewId ( getViewId() );
980#if H_3D
981  pcPic->setViewIndex( getViewIndex() );
982  pcPic->setIsDepth  ( getIsDepth  () );
983#endif
984#endif
985  if (bNextSlice)
986  {
987    pcSlice->checkCRA(pcSlice->getRPS(), m_pocCRA, m_associatedIRAPType, m_cListPic );
988    // Set reference list
989#if H_MV   
990    std::vector< TComPic* > tempRefPicLists[2];
991    std::vector< Bool     > usedAsLongTerm [2];
992    Int       numPocTotalCurr;
993
994    pcSlice->getTempRefPicLists( m_cListPic, m_refPicSetInterLayer0, m_refPicSetInterLayer1, tempRefPicLists, usedAsLongTerm, numPocTotalCurr);
995    pcSlice->setRefPicList     ( tempRefPicLists, usedAsLongTerm, numPocTotalCurr, true ); 
996#if H_3D_ARP
997    pcSlice->setARPStepNum(m_ivPicLists);
998    if( pcSlice->getARPStepNum() > 1 )
999    {
1000      // GT: This seems to be broken, not all nuh_layer_ids are necessarily present
1001      for(Int iLayerId = 0; iLayerId < nalu.m_layerId; iLayerId ++ )
1002      {
1003        Int  iViewIdx =   pcSlice->getVPS()->getViewIndex(iLayerId);
1004        Bool bIsDepth = ( pcSlice->getVPS()->getDepthId  ( iLayerId ) == 1 );
1005        if( iViewIdx<getViewIndex() && !bIsDepth )
1006        {
1007          pcSlice->setBaseViewRefPicList( m_ivPicLists->getPicList( iLayerId ), iViewIdx );
1008        }
1009      }
1010    }
1011#endif
1012#else
1013#if FIX1071
1014    pcSlice->setRefPicList( m_cListPic, true );
1015#else
1016    pcSlice->setRefPicList( m_cListPic );
1017#endif
1018
1019#endif
1020
1021#if H_3D
1022    pcSlice->setIvPicLists( m_ivPicLists );         
1023#if H_3D_IV_MERGE
1024#if H_3D_FCO
1025    //assert( !getIsDepth() );
1026#else
1027    assert( !getIsDepth() || ( pcSlice->getTexturePic() != 0 ) );
1028#endif
1029#endif   
1030#endif
1031    // For generalized B
1032    // note: maybe not existed case (always L0 is copied to L1 if L1 is empty)
1033    if (pcSlice->isInterB() && pcSlice->getNumRefIdx(REF_PIC_LIST_1) == 0)
1034    {
1035      Int iNumRefIdx = pcSlice->getNumRefIdx(REF_PIC_LIST_0);
1036      pcSlice->setNumRefIdx        ( REF_PIC_LIST_1, iNumRefIdx );
1037
1038      for (Int iRefIdx = 0; iRefIdx < iNumRefIdx; iRefIdx++)
1039      {
1040        pcSlice->setRefPic(pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx), REF_PIC_LIST_1, iRefIdx);
1041      }
1042    }
1043    if (!pcSlice->isIntra())
1044    {
1045      Bool bLowDelay = true;
1046      Int  iCurrPOC  = pcSlice->getPOC();
1047      Int iRefIdx = 0;
1048
1049      for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_0) && bLowDelay; iRefIdx++)
1050      {
1051        if ( pcSlice->getRefPic(REF_PIC_LIST_0, iRefIdx)->getPOC() > iCurrPOC )
1052        {
1053          bLowDelay = false;
1054        }
1055      }
1056      if (pcSlice->isInterB())
1057      {
1058        for (iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(REF_PIC_LIST_1) && bLowDelay; iRefIdx++)
1059        {
1060          if ( pcSlice->getRefPic(REF_PIC_LIST_1, iRefIdx)->getPOC() > iCurrPOC )
1061          {
1062            bLowDelay = false;
1063          }
1064        }       
1065      }
1066
1067      pcSlice->setCheckLDC(bLowDelay);           
1068    }
1069
1070    //---------------
1071    pcSlice->setRefPOCList();
1072#if  H_3D_TMVP
1073    if(pcSlice->getLayerId())
1074      pcSlice->generateAlterRefforTMVP();
1075#endif
1076  }
1077
1078  pcPic->setCurrSliceIdx(m_uiSliceIdx);
1079  if(pcSlice->getSPS()->getScalingListFlag())
1080  {
1081    pcSlice->setScalingList ( pcSlice->getSPS()->getScalingList()  );
1082    if(pcSlice->getPPS()->getScalingListPresentFlag())
1083    {
1084      pcSlice->setScalingList ( pcSlice->getPPS()->getScalingList()  );
1085    }
1086    if(!pcSlice->getPPS()->getScalingListPresentFlag() && !pcSlice->getSPS()->getScalingListPresentFlag())
1087    {
1088      pcSlice->setDefaultScalingList();
1089    }
1090    m_cTrQuant.setScalingListDec(pcSlice->getScalingList());
1091    m_cTrQuant.setUseScalingList(true);
1092  }
1093  else
1094  {
1095    m_cTrQuant.setFlatScalingList();
1096    m_cTrQuant.setUseScalingList(false);
1097  }
1098
1099#if H_3D_IV_MERGE
1100  if( pcSlice->getIsDepth() && m_pcCamParsCollector )
1101  {
1102    m_pcCamParsCollector->copyCamParamForSlice( pcSlice );
1103  }
1104#endif
1105
1106  //  Decode a picture
1107  m_cGopDecoder.decompressSlice(nalu.m_Bitstream, pcPic);
1108#if H_3D
1109  if( m_pcCamParsCollector )
1110  {
1111    m_pcCamParsCollector->setSlice( pcSlice );
1112  }
1113#if H_3D_IV_MERGE
1114  if( pcSlice->getIsDepth() )
1115  {
1116#if !CAM_HLS_F0136_F0045_F0082
1117    pcSlice->getSPS()->setHasCamParInSliceHeader( false );
1118#endif
1119  }
1120#endif
1121#endif
1122  m_bFirstSliceInPicture = false;
1123  m_uiSliceIdx++;
1124
1125  return false;
1126}
1127
1128Void TDecTop::xDecodeVPS()
1129{
1130  TComVPS* vps = new TComVPS();
1131 
1132  m_cEntropyDecoder.decodeVPS( vps );
1133  m_parameterSetManagerDecoder.storePrefetchedVPS(vps); 
1134}
1135
1136Void TDecTop::xDecodeSPS()
1137{
1138  TComSPS* sps = new TComSPS();
1139#if H_MV
1140  sps->setLayerId( getLayerId() ); 
1141#endif
1142#if H_3D
1143  // Preliminary fix. assuming that all sps refer to the same SPS.
1144  // Parsing dependency should be resolved!
1145  TComVPS* vps = m_parameterSetManagerDecoder.getPrefetchedVPS( 0 ); 
1146  assert( vps != 0 ); 
1147  m_cEntropyDecoder.decodeSPS( sps, vps->getViewIndex( m_layerId ), ( vps->getDepthId( m_layerId ) == 1 ) );
1148#else
1149  m_cEntropyDecoder.decodeSPS( sps );
1150#endif
1151  m_parameterSetManagerDecoder.storePrefetchedSPS(sps);
1152}
1153
1154Void TDecTop::xDecodePPS()
1155{
1156  TComPPS* pps = new TComPPS();
1157#if H_MV
1158  pps->setLayerId( getLayerId() ); 
1159#endif
1160#if DLT_DIFF_CODING_IN_PPS
1161  // Assuming that all PPS indirectly refer to the same VPS via different SPS
1162  // There is no parsing dependency in decoding DLT in PPS.
1163  // The VPS information passed to decodePPS() is used to arrange the decoded DLT tables to their corresponding layers.
1164  // This is equivalent to the process of
1165  //   Step 1) decoding DLT tables based on the number of depth layers, and
1166  //   Step 2) mapping DLT tables to the depth layers
1167  // as descripted in the 3D-HEVC WD.
1168  TComVPS* vps = m_parameterSetManagerDecoder.getPrefetchedVPS( 0 );
1169  m_cEntropyDecoder.decodePPS( pps, vps );
1170#else
1171  m_cEntropyDecoder.decodePPS( pps );
1172#endif
1173  m_parameterSetManagerDecoder.storePrefetchedPPS( pps );
1174}
1175
1176Void TDecTop::xDecodeSEI( TComInputBitstream* bs, const NalUnitType nalUnitType )
1177{
1178  if(nalUnitType == NAL_UNIT_SUFFIX_SEI)
1179  {
1180#if H_MV
1181    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS( m_layerId ) );
1182#else
1183    m_seiReader.parseSEImessage( bs, m_pcPic->getSEIs(), nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1184#endif
1185  }
1186  else
1187  {
1188#if H_MV
1189    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS( m_layerId ) );
1190#else
1191    m_seiReader.parseSEImessage( bs, m_SEIs, nalUnitType, m_parameterSetManagerDecoder.getActiveSPS() );
1192#endif
1193    SEIMessages activeParamSets = getSeisByType(m_SEIs, SEI::ACTIVE_PARAMETER_SETS);
1194    if (activeParamSets.size()>0)
1195    {
1196      SEIActiveParameterSets *seiAps = (SEIActiveParameterSets*)(*activeParamSets.begin());
1197      m_parameterSetManagerDecoder.applyPrefetchedPS();
1198      assert(seiAps->activeSeqParamSetId.size()>0);
1199#if H_MV
1200      if (! m_parameterSetManagerDecoder.activateSPSWithSEI(seiAps->activeSeqParamSetId[0], m_layerId ))
1201#else
1202      if (! m_parameterSetManagerDecoder.activateSPSWithSEI(seiAps->activeSeqParamSetId[0] ))
1203#endif
1204      {
1205        printf ("Warning SPS activation with Active parameter set SEI failed");
1206      }
1207    }
1208  }
1209}
1210
1211#if H_MV
1212Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay, Bool newLayerFlag, Bool& sliceSkippedFlag )
1213#else
1214Bool TDecTop::decode(InputNALUnit& nalu, Int& iSkipFrame, Int& iPOCLastDisplay)
1215#endif
1216{
1217  // Initialize entropy decoder
1218  m_cEntropyDecoder.setEntropyDecoder (&m_cCavlcDecoder);
1219  m_cEntropyDecoder.setBitstream      (nalu.m_Bitstream);
1220
1221  switch (nalu.m_nalUnitType)
1222  {
1223    case NAL_UNIT_VPS:
1224      xDecodeVPS();
1225      return false;
1226     
1227    case NAL_UNIT_SPS:
1228      xDecodeSPS();
1229      return false;
1230
1231    case NAL_UNIT_PPS:
1232      xDecodePPS();
1233      return false;
1234     
1235    case NAL_UNIT_PREFIX_SEI:
1236    case NAL_UNIT_SUFFIX_SEI:
1237      xDecodeSEI( nalu.m_Bitstream, nalu.m_nalUnitType );
1238      return false;
1239
1240    case NAL_UNIT_CODED_SLICE_TRAIL_R:
1241    case NAL_UNIT_CODED_SLICE_TRAIL_N:
1242    case NAL_UNIT_CODED_SLICE_TLA_R:
1243    case NAL_UNIT_CODED_SLICE_TSA_N:
1244    case NAL_UNIT_CODED_SLICE_STSA_R:
1245    case NAL_UNIT_CODED_SLICE_STSA_N:
1246    case NAL_UNIT_CODED_SLICE_BLA_W_LP:
1247    case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
1248    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
1249    case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
1250    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
1251    case NAL_UNIT_CODED_SLICE_CRA:
1252    case NAL_UNIT_CODED_SLICE_RADL_N:
1253    case NAL_UNIT_CODED_SLICE_RADL_R:
1254    case NAL_UNIT_CODED_SLICE_RASL_N:
1255    case NAL_UNIT_CODED_SLICE_RASL_R:
1256#if H_MV
1257      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay, newLayerFlag, sliceSkippedFlag );
1258#else
1259      return xDecodeSlice(nalu, iSkipFrame, iPOCLastDisplay);
1260#endif
1261      break;
1262    default:
1263      assert (1);
1264  }
1265
1266  return false;
1267}
1268
1269/** Function for checking if picture should be skipped because of association with a previous BLA picture
1270 * \param iPOCLastDisplay POC of last picture displayed
1271 * \returns true if the picture should be skipped
1272 * This function skips all TFD pictures that follow a BLA picture
1273 * in decoding order and precede it in output order.
1274 */
1275Bool TDecTop::isSkipPictureForBLA(Int& iPOCLastDisplay)
1276{
1277  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) && 
1278       m_apcSlicePilot->getPOC() < m_pocCRA && (m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N))
1279  {
1280    iPOCLastDisplay++;
1281    return true;
1282  }
1283  return false;
1284}
1285
1286/** Function for checking if picture should be skipped because of random access
1287 * \param iSkipFrame skip frame counter
1288 * \param iPOCLastDisplay POC of last picture displayed
1289 * \returns true if the picture shold be skipped in the random access.
1290 * This function checks the skipping of pictures in the case of -s option random access.
1291 * All pictures prior to the random access point indicated by the counter iSkipFrame are skipped.
1292 * It also checks the type of Nal unit type at the random access point.
1293 * 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.
1294 * If the random access point is IDR all pictures after the random access point are decoded.
1295 * If the random access point is none of the above, a warning is issues, and decoding of pictures with POC
1296 * equal to or greater than the random access point POC is attempted. For non IDR/CRA/BLA random
1297 * access point there is no guarantee that the decoder will not crash.
1298 */
1299Bool TDecTop::isRandomAccessSkipPicture(Int& iSkipFrame,  Int& iPOCLastDisplay)
1300{
1301  if (iSkipFrame) 
1302  {
1303    iSkipFrame--;   // decrement the counter
1304    return true;
1305  }
1306  else if ( !m_layerInitilizedFlag[ m_layerId ] ) // start of random access point, m_pocRandomAccess has not been set yet.
1307  {
1308    if (   m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA
1309        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1310        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
1311        || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL )
1312    {
1313
1314      if ( xAllRefLayersInitilized() )
1315      {
1316        m_layerInitilizedFlag[ m_layerId ] = true; 
1317        m_pocRandomAccess = m_apcSlicePilot->getPOC();
1318      }
1319      else
1320      {
1321        return true; 
1322      }
1323    }
1324    else if ( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
1325    {
1326      if ( xAllRefLayersInitilized() )
1327      {
1328        m_layerInitilizedFlag[ m_layerId ] = true; 
1329      m_pocRandomAccess = -MAX_INT; // no need to skip the reordered pictures in IDR, they are decodable.
1330    }
1331    else 
1332    {
1333        return true; 
1334      }
1335    }
1336    else 
1337    {
1338      static Bool warningMessage[MAX_NUM_LAYERS];
1339      static Bool warningInitFlag = false;
1340     
1341      if (!warningInitFlag)
1342      {
1343        for ( Int i = 0; i < MAX_NUM_LAYERS; i++)
1344        {
1345          warningMessage[i] = true; 
1346        }
1347        warningInitFlag = true; 
1348      }
1349
1350      if ( warningMessage[getLayerId()] )
1351      {
1352        printf("\nLayer%3d   No valid random access point. VCL NAL units of this layer are discarded until next layer initialization picture. ", getLayerId() ); 
1353        warningMessage[m_layerId] = false; 
1354      }
1355      return true;
1356    }
1357  }
1358  // skip the reordered pictures, if necessary
1359  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))
1360  {
1361    iPOCLastDisplay++;
1362    return true;
1363  }
1364  return !m_layerInitilizedFlag[ getLayerId() ]; 
1365}
1366
1367#if H_MV
1368TComPic* TDecTop::getPic( Int poc )
1369{
1370  xGetPic( m_layerId, poc ); 
1371  TComList<TComPic*>* listPic = getListPic();
1372  TComPic* pcPic = NULL;
1373  for(TComList<TComPic*>::iterator it=listPic->begin(); it!=listPic->end(); it++)
1374  {
1375    if( (*it)->getPOC() == poc )
1376    {
1377      pcPic = *it ;
1378      break ;
1379    }
1380  }
1381  return pcPic;
1382}
1383
1384TComPic* TDecTop::xGetPic( Int layerId, Int poc )
1385{ 
1386  return m_ivPicLists->getPic( layerId, poc ) ;
1387}
1388
1389Void TDecTop::xResetPocInPicBuffer()
1390{
1391  TComList<TComPic*>::iterator  iterPic   = m_cListPic.begin();
1392  while (iterPic != m_cListPic.end())
1393  {
1394    TComPic* pic = *(iterPic++);
1395    if ( pic->getReconMark() )
1396    {
1397      for( Int i = 0; i < pic->getNumAllocatedSlice(); i++)
1398      {
1399        TComSlice* slice = pic->getSlice( i ); 
1400        slice->setPOC ( slice->getPOC() - m_apcSlicePilot->getPocBeforeReset() );           
1401      }         
1402    }     
1403  }
1404}
1405
1406Void TDecTop::xCeckNoClrasOutput()
1407{
1408  // This part needs further testing!
1409  if ( getLayerId() == 0 )
1410  {   
1411    NalUnitType nut = m_apcSlicePilot->getNalUnitType(); 
1412
1413    Bool isBLA =  ( nut == NAL_UNIT_CODED_SLICE_BLA_W_LP  )  || ( nut == NAL_UNIT_CODED_SLICE_BLA_N_LP ) || ( nut == NAL_UNIT_CODED_SLICE_BLA_W_RADL ); 
1414    Bool isIDR  = ( nut == NAL_UNIT_CODED_SLICE_IDR_W_RADL ) || ( nut == NAL_UNIT_CODED_SLICE_IDR_N_LP ); 
1415    Bool noClrasOutputFlag  = isBLA || ( isIDR  &&  m_apcSlicePilot->getCrossLayerBlaFlag() ); 
1416
1417    if ( noClrasOutputFlag ) 
1418    {
1419      for (Int i = 0; i < MAX_NUM_LAYER_IDS; i++)
1420      {
1421        m_layerInitilizedFlag[i] = false; 
1422      } 
1423    }
1424  }
1425}
1426
1427Bool TDecTop::xAllRefLayersInitilized()
1428{
1429  Bool allRefLayersInitilizedFlag = true; 
1430  TComVPS* vps = m_parameterSetManagerDecoder.getPrefetchedVPS( 0 ); 
1431  for (Int i = 0; i < vps->getNumDirectRefLayers( getLayerId()  ); i++ )
1432  {
1433    Int refLayerId = vps->getRefLayerId( m_layerId, i ); 
1434    allRefLayersInitilizedFlag = allRefLayersInitilizedFlag && m_layerInitilizedFlag[ refLayerId ]; 
1435  }
1436
1437  return allRefLayersInitilizedFlag;
1438}
1439#endif
1440//! \}
Note: See TracBrowser for help on using the repository browser.