source: SHVCSoftware/branches/SHM-6-dev/source/App/TAppDecoder/TAppDecTop.cpp @ 678

Last change on this file since 678 was 652, checked in by seregin, 11 years ago

update to HM-14.0

  • Property svn:eol-style set to native
File size: 50.2 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license.
5 *
6 * Copyright (c) 2010-2014, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TAppDecTop.cpp
35    \brief    Decoder application class
36*/
37
38#include <list>
39#include <vector>
40#include <stdio.h>
41#include <fcntl.h>
42#include <assert.h>
43#include <iostream>
44#include "TAppDecTop.h"
45#include "TLibDecoder/AnnexBread.h"
46#include "TLibDecoder/NALread.h"
47//! \ingroup TAppDecoder
48//! \{
49
50// ====================================================================================================================
51// Constructor / destructor / initialization / destroy
52// ====================================================================================================================
53
54#if SVC_EXTENSION
55TAppDecTop::TAppDecTop()
56{
57  for(UInt layer=0; layer < MAX_LAYERS; layer++)
58  {
59    m_aiPOCLastDisplay[layer]  = -MAX_INT;
60    m_apcTDecTop[layer] = &m_acTDecTop[layer];
61  }
62}
63#else
64TAppDecTop::TAppDecTop()
65: m_iPOCLastDisplay(-MAX_INT)
66{
67}
68#endif
69
70Void TAppDecTop::create()
71{
72}
73
74Void TAppDecTop::destroy()
75{
76  if (m_pchBitstreamFile)
77  {
78    free (m_pchBitstreamFile);
79    m_pchBitstreamFile = NULL;
80  }
81#if SVC_EXTENSION
82  for( Int i = 0; i <= m_tgtLayerId; i++ )
83  {
84    if( m_pchReconFile[i] )
85    {
86      free ( m_pchReconFile[i] );
87      m_pchReconFile[i] = NULL;
88    }
89  }
90#if AVC_BASE
91  if( m_pchBLReconFile )
92  {
93    free ( m_pchBLReconFile );
94    m_pchBLReconFile = NULL;
95  }
96#endif
97#else
98  if (m_pchReconFile)
99  {
100    free (m_pchReconFile);
101    m_pchReconFile = NULL;
102  }
103#endif
104#if AVC_SYNTAX || SYNTAX_OUTPUT
105  if( m_pchBLSyntaxFile )
106  {
107    free ( m_pchBLSyntaxFile );
108    m_pchBLSyntaxFile = NULL;
109  }
110#endif
111}
112
113// ====================================================================================================================
114// Public member functions
115// ====================================================================================================================
116
117/**
118 - create internal class
119 - initialize internal class
120 - until the end of the bitstream, call decoding function in TDecTop class
121 - delete allocated buffers
122 - destroy internal class
123 .
124 */
125#if SVC_EXTENSION
126Void TAppDecTop::decode()
127{
128  Int                poc;
129  TComList<TComPic*>* pcListPic = NULL;
130
131  ifstream bitstreamFile(m_pchBitstreamFile, ifstream::in | ifstream::binary);
132  if (!bitstreamFile)
133  {
134    fprintf(stderr, "\nfailed to open bitstream file `%s' for reading\n", m_pchBitstreamFile);
135    exit(EXIT_FAILURE);
136  }
137
138  InputByteStream bytestream(bitstreamFile);
139
140  // create & initialize internal classes
141  xCreateDecLib();
142  xInitDecLib  ();
143
144  // main decoder loop
145  Bool openedReconFile[MAX_LAYERS]; // reconstruction file not yet opened. (must be performed after SPS is seen)
146  Bool loopFiltered[MAX_LAYERS];
147  memset( loopFiltered, false, sizeof( loopFiltered ) );
148
149  for(UInt layer=0; layer<=m_tgtLayerId; layer++)
150  {
151    openedReconFile[layer] = false;
152    m_aiPOCLastDisplay[layer] += m_iSkipFrame;      // set the last displayed POC correctly for skip forward.
153  }
154
155  UInt curLayerId = 0;     // current layer to be reconstructed
156
157#if AVC_BASE
158  TComPic pcBLPic;
159  fstream streamYUV;
160  if( m_pchBLReconFile )
161  {
162    streamYUV.open( m_pchBLReconFile, fstream::in | fstream::binary );
163  }
164  TComList<TComPic*> *cListPic = m_acTDecTop[0].getListPic();
165  m_acTDecTop[0].setBLsize( m_iBLSourceWidth, m_iBLSourceHeight );
166  m_acTDecTop[0].setBLReconFile( &streamYUV );
167  pcBLPic.setLayerId( 0 );
168  cListPic->pushBack( &pcBLPic );
169#if AVC_SYNTAX
170  fstream streamSyntaxFile;
171  if( m_pchBLSyntaxFile )
172  {
173    streamSyntaxFile.open( m_pchBLSyntaxFile, fstream::in | fstream::binary );
174  }
175  m_acTDecTop[0].setBLSyntaxFile( &streamSyntaxFile );
176#endif
177#endif
178
179  while (!!bitstreamFile)
180  {
181    /* location serves to work around a design fault in the decoder, whereby
182     * the process of reading a new slice that is the first slice of a new frame
183     * requires the TDecTop::decode() method to be called again with the same
184     * nal unit. */
185    streampos location = bitstreamFile.tellg();
186    AnnexBStats stats = AnnexBStats();
187
188    vector<uint8_t> nalUnit;
189    InputNALUnit nalu;
190    byteStreamNALUnit(bytestream, nalUnit, stats);
191
192    // call actual decoding function
193    Bool bNewPicture = false;
194    Bool bNewPOC = false;
195    if (nalUnit.empty())
196    {
197      /* this can happen if the following occur:
198       *  - empty input file
199       *  - two back-to-back start_code_prefixes
200       *  - start_code_prefix immediately followed by EOF
201       */
202      fprintf(stderr, "Warning: Attempt to decode an empty NAL unit\n");
203    }
204    else
205    {
206      read(nalu, nalUnit);
207      if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) || !isNaluWithinTargetDecLayerIdSet(&nalu)  ||
208        (nalu.m_layerId > m_tgtLayerId) )
209      {
210        bNewPicture = false;
211      }
212      else
213      {
214        bNewPicture = m_acTDecTop[nalu.m_layerId].decode(nalu, m_iSkipFrame, m_aiPOCLastDisplay[nalu.m_layerId], curLayerId, bNewPOC);
215        if (bNewPicture)
216        {
217          bitstreamFile.clear();
218          /* location points to the current nalunit payload[1] due to the
219           * need for the annexB parser to read three extra bytes.
220           * [1] except for the first NAL unit in the file
221           *     (but bNewPicture doesn't happen then) */
222          bitstreamFile.seekg(location-streamoff(3));
223          bytestream.reset();
224        }
225      }
226    }
227
228    if (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS)
229    {
230#if O0194_DIFFERENT_BITDEPTH_EL_BL
231      //Bug fix: The bit depth was not set correctly for each layer when doing DBF
232      g_bitDepthY = g_bitDepthYLayer[curLayerId];
233      g_bitDepthC = g_bitDepthCLayer[curLayerId];
234#endif
235      if (!loopFiltered[curLayerId] || bitstreamFile)
236      {
237        m_acTDecTop[curLayerId].executeLoopFilters(poc, pcListPic);
238      }
239      loopFiltered[curLayerId] = (nalu.m_nalUnitType == NAL_UNIT_EOS);
240#if EARLY_REF_PIC_MARKING
241      m_acTDecTop[curLayerId].earlyPicMarking(m_iMaxTemporalLayer, m_targetDecLayerIdSet);
242#endif
243    }
244
245    if( pcListPic )
246    {
247      if ( m_pchReconFile[curLayerId] && !openedReconFile[curLayerId] )
248      {
249        if (!m_outputBitDepthY) { m_outputBitDepthY = g_bitDepthY; }
250        if (!m_outputBitDepthC) { m_outputBitDepthC = g_bitDepthC; }
251
252        m_acTVideoIOYuvReconFile[curLayerId].open( m_pchReconFile[curLayerId], true, m_outputBitDepthY, m_outputBitDepthC, g_bitDepthY, g_bitDepthC ); // write mode
253
254        openedReconFile[curLayerId] = true;
255      }
256#if ALIGNED_BUMPING
257      Bool outputPicturesFlag = true; 
258#if NO_OUTPUT_OF_PRIOR_PICS
259      if( m_acTDecTop[nalu.m_layerId].getNoOutputPriorPicsFlag() )
260      {
261        outputPicturesFlag = false;
262      }
263#endif
264
265      if (nalu.m_nalUnitType == NAL_UNIT_EOS) // End of sequence
266      {
267        flushAllPictures( nalu.m_layerId, outputPicturesFlag );       
268      }
269      if( bNewPicture ) // New picture, slice header parsed but picture not decoded
270      {
271#if NO_OUTPUT_OF_PRIOR_PICS
272        if( 
273#else
274        if ( bNewPOC &&
275#endif
276           (   nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
277            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
278            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP
279            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL
280            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP ) )
281        {
282          flushAllPictures( nalu.m_layerId, outputPicturesFlag );
283        }
284        else
285        {
286          this->checkOutputBeforeDecoding( nalu.m_layerId );
287        }
288      }
289
290      /* The following code has to be executed when the last DU of the picture is decoded
291         TODO: Need code to identify end of decoding a picture
292      {
293        this->checkOutputAfterDecoding( );
294      } */
295#else
296      if ( bNewPicture && bNewPOC &&
297           (   nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
298            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
299            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP
300            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL
301            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP ) )
302      {
303        xFlushOutput( pcListPic, curLayerId );
304      }
305      if (nalu.m_nalUnitType == NAL_UNIT_EOS)
306      {
307        xFlushOutput( pcListPic, curLayerId );       
308      }
309      // write reconstruction to file
310      if(bNewPicture)
311      {
312        xWriteOutput( pcListPic, curLayerId, nalu.m_temporalId );
313      }
314#endif
315    }
316  }
317#if ALIGNED_BUMPING
318   flushAllPictures( true );   
319#else
320  for(UInt layer = 0; layer <= m_tgtLayerId; layer++)
321  {
322    xFlushOutput( m_acTDecTop[layer].getListPic(), layer );
323  }
324#endif
325  // delete buffers
326#if AVC_BASE
327  UInt layerIdmin = m_acTDecTop[0].getBLReconFile()->is_open() ? 1 : 0;
328
329  if( streamYUV.is_open() )
330  {
331    streamYUV.close();
332  }
333#if AVC_SYNTAX
334  if( streamSyntaxFile.is_open() )
335  {
336    streamSyntaxFile.close();
337  }
338#endif
339  pcBLPic.destroy();
340
341  for(UInt layer = layerIdmin; layer <= m_tgtLayerId; layer++)
342#else
343  for(UInt layer = 0; layer <= m_tgtLayerId; layer++)
344#endif
345  {
346    m_acTDecTop[layer].deletePicBuffer();
347  }
348
349  // destroy internal classes
350  xDestroyDecLib();
351}
352#else
353Void TAppDecTop::decode()
354{
355  Int                 poc;
356  TComList<TComPic*>* pcListPic = NULL;
357
358  ifstream bitstreamFile(m_pchBitstreamFile, ifstream::in | ifstream::binary);
359  if (!bitstreamFile)
360  {
361    fprintf(stderr, "\nfailed to open bitstream file `%s' for reading\n", m_pchBitstreamFile);
362    exit(EXIT_FAILURE);
363  }
364
365  InputByteStream bytestream(bitstreamFile);
366
367  // create & initialize internal classes
368  xCreateDecLib();
369  xInitDecLib  ();
370  m_iPOCLastDisplay += m_iSkipFrame;      // set the last displayed POC correctly for skip forward.
371
372  // main decoder loop
373  Bool openedReconFile = false; // reconstruction file not yet opened. (must be performed after SPS is seen)
374  Bool loopFiltered = false;
375
376#if SYNTAX_OUTPUT
377  if( !m_pchBLSyntaxFile )
378  {
379    printf( "Wrong base layer syntax file\n" );
380    exit(EXIT_FAILURE);
381  }
382  fstream streamSyntaxFile( m_pchBLSyntaxFile, fstream::out | fstream::binary );
383  if( !streamSyntaxFile.good() )
384  {
385    printf( "Base layer syntax input reading error\n" );
386    exit(EXIT_FAILURE);
387  }
388  m_cTDecTop.setBLSyntaxFile( &streamSyntaxFile );
389
390  for( Int i = m_iBLFrames * m_iBLSourceWidth * m_iBLSourceHeight * SYNTAX_BYTES / 16; i >= 0; i-- )
391  {
392    streamSyntaxFile.put( 0 );
393  }
394  streamSyntaxFile.seekp( 0 );
395#endif
396
397  while (!!bitstreamFile)
398  {
399    /* location serves to work around a design fault in the decoder, whereby
400     * the process of reading a new slice that is the first slice of a new frame
401     * requires the TDecTop::decode() method to be called again with the same
402     * nal unit. */
403    streampos location = bitstreamFile.tellg();
404    AnnexBStats stats = AnnexBStats();
405
406    vector<uint8_t> nalUnit;
407    InputNALUnit nalu;
408    byteStreamNALUnit(bytestream, nalUnit, stats);
409
410    // call actual decoding function
411    Bool bNewPicture = false;
412    if (nalUnit.empty())
413    {
414      /* this can happen if the following occur:
415       *  - empty input file
416       *  - two back-to-back start_code_prefixes
417       *  - start_code_prefix immediately followed by EOF
418       */
419      fprintf(stderr, "Warning: Attempt to decode an empty NAL unit\n");
420    }
421    else
422    {
423      read(nalu, nalUnit);
424      if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) || !isNaluWithinTargetDecLayerIdSet(&nalu)  )
425      {
426          bNewPicture = false;
427        }
428      else
429      {
430        bNewPicture = m_cTDecTop.decode(nalu, m_iSkipFrame, m_iPOCLastDisplay);
431        if (bNewPicture)
432        {
433          bitstreamFile.clear();
434          /* location points to the current nalunit payload[1] due to the
435           * need for the annexB parser to read three extra bytes.
436           * [1] except for the first NAL unit in the file
437           *     (but bNewPicture doesn't happen then) */
438          bitstreamFile.seekg(location-streamoff(3));
439          bytestream.reset();
440        }
441      }
442    }
443    if (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS)
444    {
445      if (!loopFiltered || bitstreamFile)
446      {
447        m_cTDecTop.executeLoopFilters(poc, pcListPic);
448      }
449      loopFiltered = (nalu.m_nalUnitType == NAL_UNIT_EOS);
450    }
451#if !FIX_WRITING_OUTPUT
452#if SETTING_NO_OUT_PIC_PRIOR
453    if (bNewPicture && m_cTDecTop.getNoOutputPriorPicsFlag())
454    {
455      m_cTDecTop.checkNoOutputPriorPics( pcListPic );
456    }
457#endif
458#endif
459
460    if( pcListPic )
461    {
462      if ( m_pchReconFile && !openedReconFile )
463      {
464        if (!m_outputBitDepthY) { m_outputBitDepthY = g_bitDepthY; }
465        if (!m_outputBitDepthC) { m_outputBitDepthC = g_bitDepthC; }
466
467        m_cTVideoIOYuvReconFile.open( m_pchReconFile, true, m_outputBitDepthY, m_outputBitDepthC, g_bitDepthY, g_bitDepthC ); // write mode
468        openedReconFile = true;
469      }
470#if FIX_WRITING_OUTPUT
471      // write reconstruction to file
472      if( bNewPicture )
473      {
474        xWriteOutput( pcListPic, nalu.m_temporalId );
475      }
476#if SETTING_NO_OUT_PIC_PRIOR
477      if ( (bNewPicture || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA) && m_cTDecTop.getNoOutputPriorPicsFlag() )
478      {
479        m_cTDecTop.checkNoOutputPriorPics( pcListPic );
480        m_cTDecTop.setNoOutputPriorPicsFlag (false);
481      }
482#endif
483#endif
484      if ( bNewPicture &&
485           (   nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
486            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
487            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP
488            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL
489            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP ) )
490      {
491        xFlushOutput( pcListPic );
492      }
493      if (nalu.m_nalUnitType == NAL_UNIT_EOS)
494      {
495#if FIX_OUTPUT_EOS
496        xWriteOutput( pcListPic, nalu.m_temporalId );
497#else
498        xFlushOutput( pcListPic );       
499#endif
500      }
501      // write reconstruction to file -- for additional bumping as defined in C.5.2.3
502#if FIX_WRITING_OUTPUT
503      if(!bNewPicture && nalu.m_nalUnitType >= NAL_UNIT_CODED_SLICE_TRAIL_N && nalu.m_nalUnitType <= NAL_UNIT_RESERVED_VCL31)
504#else
505      if(bNewPicture)
506#endif
507      {
508        xWriteOutput( pcListPic, nalu.m_temporalId );
509      }
510    }
511  }
512
513#if SYNTAX_OUTPUT
514  if( streamSyntaxFile.is_open() )
515  {
516    streamSyntaxFile.close();
517  }
518#endif
519
520  xFlushOutput( pcListPic );
521  // delete buffers
522  m_cTDecTop.deletePicBuffer();
523
524  // destroy internal classes
525  xDestroyDecLib();
526}
527#endif
528
529// ====================================================================================================================
530// Protected member functions
531// ====================================================================================================================
532
533Void TAppDecTop::xCreateDecLib()
534{
535#if SVC_EXTENSION
536  // initialize global variables
537  initROM();
538
539  for(UInt layer = 0; layer <= m_tgtLayerId; layer++)
540  {
541    // set layer ID
542    m_acTDecTop[layer].setLayerId                      ( layer );
543
544    // create decoder class
545    m_acTDecTop[layer].create();
546
547    m_acTDecTop[layer].setLayerDec(m_apcTDecTop);
548  }
549#else
550  // create decoder class
551  m_cTDecTop.create();
552#endif
553}
554
555Void TAppDecTop::xDestroyDecLib()
556{
557#if SVC_EXTENSION
558  // destroy ROM
559  destroyROM();
560
561  for(UInt layer = 0; layer <= m_tgtLayerId; layer++)
562  {
563    if ( m_pchReconFile[layer] )
564    {
565      m_acTVideoIOYuvReconFile[layer].close();
566    }
567
568    // destroy decoder class
569    m_acTDecTop[layer].destroy();
570  }
571#else
572  if ( m_pchReconFile )
573  {
574    m_cTVideoIOYuvReconFile. close();
575  }
576
577  // destroy decoder class
578  m_cTDecTop.destroy();
579#endif
580}
581
582Void TAppDecTop::xInitDecLib()
583{
584  // initialize decoder class
585#if SVC_EXTENSION
586  for(UInt layer = 0; layer <= m_tgtLayerId; layer++)
587  {
588    m_acTDecTop[layer].init();
589    m_acTDecTop[layer].setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
590    m_acTDecTop[layer].setNumLayer( m_tgtLayerId + 1 );
591#if OUTPUT_LAYER_SET_INDEX
592    m_acTDecTop[layer].setCommonDecoderParams( this->getCommonDecoderParams() );
593#endif
594  }
595
596#else
597  m_cTDecTop.init();
598  m_cTDecTop.setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
599#endif
600}
601
602/** \param pcListPic list of pictures to be written to file
603    \todo            DYN_REF_FREE should be revised
604 */
605#if SVC_EXTENSION
606Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, UInt layerId, UInt tId )
607#else
608Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, UInt tId )
609#endif
610{
611  if (pcListPic->empty())
612  {
613    return;
614  }
615
616  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
617  Int numPicsNotYetDisplayed = 0;
618  Int dpbFullness = 0;
619#if SVC_EXTENSION
620TComSPS* activeSPS = m_acTDecTop[layerId].getActiveSPS();
621#else
622  TComSPS* activeSPS = m_cTDecTop.getActiveSPS();
623#endif
624  UInt numReorderPicsHighestTid;
625  UInt maxDecPicBufferingHighestTid;
626  UInt maxNrSublayers = activeSPS->getMaxTLayers();
627
628  if(m_iMaxTemporalLayer == -1 || m_iMaxTemporalLayer >= maxNrSublayers)
629  {
630    numReorderPicsHighestTid = activeSPS->getNumReorderPics(maxNrSublayers-1);
631    maxDecPicBufferingHighestTid =  activeSPS->getMaxDecPicBuffering(maxNrSublayers-1); 
632  }
633  else
634  {
635    numReorderPicsHighestTid = activeSPS->getNumReorderPics(m_iMaxTemporalLayer);
636    maxDecPicBufferingHighestTid = activeSPS->getMaxDecPicBuffering(m_iMaxTemporalLayer); 
637  }
638
639  while (iterPic != pcListPic->end())
640  {
641    TComPic* pcPic = *(iterPic);
642#if SVC_EXTENSION
643    if(pcPic->getOutputMark() && pcPic->getPOC() > m_aiPOCLastDisplay[layerId])
644#else
645    if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay)
646#endif
647    {
648      numPicsNotYetDisplayed++;
649      dpbFullness++;
650    }
651    else if(pcPic->getSlice( 0 )->isReferenced())
652    {
653      dpbFullness++;
654    }
655    iterPic++;
656  }
657  iterPic   = pcListPic->begin();
658  if (numPicsNotYetDisplayed>2)
659  {
660    iterPic++;
661  }
662
663  TComPic* pcPic = *(iterPic);
664  if (numPicsNotYetDisplayed>2 && pcPic->isField()) //Field Decoding
665  {
666    TComList<TComPic*>::iterator endPic   = pcListPic->end();
667    endPic--;
668    iterPic   = pcListPic->begin();
669    while (iterPic != endPic)
670    {
671      TComPic* pcPicTop = *(iterPic);
672      iterPic++;
673      TComPic* pcPicBottom = *(iterPic);
674
675#if SVC_EXTENSION
676      if( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() &&
677        (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid) &&
678        (!(pcPicTop->getPOC()%2) && pcPicBottom->getPOC() == pcPicTop->getPOC()+1) &&       
679        (pcPicTop->getPOC() == m_aiPOCLastDisplay[layerId]+1 || m_aiPOCLastDisplay[layerId]<0) )
680#else
681      if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() &&
682          (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid) &&
683          (!(pcPicTop->getPOC()%2) && pcPicBottom->getPOC() == pcPicTop->getPOC()+1) &&
684          (pcPicTop->getPOC() == m_iPOCLastDisplay+1 || m_iPOCLastDisplay < 0))
685#endif
686      {
687        // write to file
688        numPicsNotYetDisplayed = numPicsNotYetDisplayed-2;
689#if SVC_EXTENSION
690        if ( m_pchReconFile[layerId] )
691        {
692          const Window &conf = pcPicTop->getConformanceWindow();
693          const Window &defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
694          const Bool isTff = pcPicTop->isTopField();
695#if REPN_FORMAT_IN_VPS
696          UInt chromaFormatIdc = pcPic->getSlice(0)->getChromaFormatIdc();
697          Int xScal =  TComSPS::getWinUnitX( chromaFormatIdc ), yScal = TComSPS::getWinUnitY( chromaFormatIdc );
698          m_acTVideoIOYuvReconFile[layerId].write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
699            conf.getWindowLeftOffset()  * xScal + defDisp.getWindowLeftOffset(),
700            conf.getWindowRightOffset() * xScal + defDisp.getWindowRightOffset(),
701            conf.getWindowTopOffset()   * yScal + defDisp.getWindowTopOffset(),
702            conf.getWindowBottomOffset()* yScal + defDisp.getWindowBottomOffset(), isTff );
703
704#else
705#if O0194_DIFFERENT_BITDEPTH_EL_BL
706          // Compile time bug-fix
707          m_acTVideoIOYuvReconFile[layerId].write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
708#else
709          m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
710#endif
711            conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
712            conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
713            conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
714            conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), isTff );
715#endif
716        }
717
718        // update POC of display order
719        m_aiPOCLastDisplay[layerId] = pcPicBottom->getPOC();
720#else
721        if ( m_pchReconFile )
722        {
723          const Window &conf = pcPicTop->getConformanceWindow();
724          const Window &defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
725          const Bool isTff = pcPicTop->isTopField();
726          m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
727            conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
728            conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
729            conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
730            conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), isTff );
731        }
732
733        // update POC of display order
734        m_iPOCLastDisplay = pcPicBottom->getPOC();
735#endif
736
737        // erase non-referenced picture in the reference picture list after display
738        if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
739        {
740#if !DYN_REF_FREE
741          pcPicTop->setReconMark(false);
742
743          // mark it should be extended later
744          pcPicTop->getPicYuvRec()->setBorderExtension( false );
745
746#else
747          pcPicTop->destroy();
748          pcListPic->erase( iterPic );
749          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
750          continue;
751#endif
752        }
753        if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
754        {
755#if !DYN_REF_FREE
756          pcPicBottom->setReconMark(false);
757
758          // mark it should be extended later
759          pcPicBottom->getPicYuvRec()->setBorderExtension( false );
760
761#else
762          pcPicBottom->destroy();
763          pcListPic->erase( iterPic );
764          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
765          continue;
766#endif
767        }
768        pcPicTop->setOutputMark(false);
769        pcPicBottom->setOutputMark(false);
770      }
771    }
772  }
773  else if (!pcPic->isField()) //Frame Decoding
774  {
775    iterPic = pcListPic->begin();
776    while (iterPic != pcListPic->end())
777    {
778      pcPic = *(iterPic);
779
780#if SVC_EXTENSION
781      if( pcPic->getOutputMark() && pcPic->getPOC() > m_aiPOCLastDisplay[layerId] &&
782        (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid) )
783#else
784      if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay &&
785        (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid))
786#endif
787      {
788        // write to file
789        numPicsNotYetDisplayed--;
790        if(pcPic->getSlice(0)->isReferenced() == false)
791        {
792          dpbFullness--;
793        }
794#if SVC_EXTENSION
795        if( m_pchReconFile[layerId] )
796        {
797          const Window &conf = pcPic->getConformanceWindow();
798          const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
799#if REPN_FORMAT_IN_VPS
800          UInt chromaFormatIdc = pcPic->getSlice(0)->getChromaFormatIdc();
801          Int xScal =  TComSPS::getWinUnitX( chromaFormatIdc ), yScal = TComSPS::getWinUnitY( chromaFormatIdc );
802          m_acTVideoIOYuvReconFile[layerId].write( pcPic->getPicYuvRec(),
803            conf.getWindowLeftOffset()  * xScal + defDisp.getWindowLeftOffset(),
804            conf.getWindowRightOffset() * xScal + defDisp.getWindowRightOffset(),
805            conf.getWindowTopOffset()   * yScal + defDisp.getWindowTopOffset(),
806            conf.getWindowBottomOffset()* yScal + defDisp.getWindowBottomOffset() );
807
808#else
809          m_acTVideoIOYuvReconFile[layerId].write( pcPic->getPicYuvRec(),
810            conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
811            conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
812            conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
813            conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
814#endif
815        }
816
817        // update POC of display order
818        m_aiPOCLastDisplay[layerId] = pcPic->getPOC();
819#else
820        if ( m_pchReconFile )
821        {
822#if SYNTAX_OUTPUT
823          const Window &conf = pcPic->getConformanceWindow();
824          const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
825          m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
826            conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
827            conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
828            conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
829            conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
830#endif
831        }
832
833        // update POC of display order
834        m_iPOCLastDisplay = pcPic->getPOC();
835#endif
836
837        // erase non-referenced picture in the reference picture list after display
838        if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
839        {
840#if !DYN_REF_FREE
841          pcPic->setReconMark(false);
842
843          // mark it should be extended later
844          pcPic->getPicYuvRec()->setBorderExtension( false );
845
846#else
847          pcPic->destroy();
848          pcListPic->erase( iterPic );
849          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
850          continue;
851#endif
852        }
853        pcPic->setOutputMark(false);
854      }
855
856      iterPic++;
857    }
858  }
859}
860
861/** \param pcListPic list of pictures to be written to file
862    \todo            DYN_REF_FREE should be revised
863 */
864#if SVC_EXTENSION
865Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic, UInt layerId )
866#else
867Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic )
868#endif
869{
870  if(!pcListPic || pcListPic->empty())
871  {
872    return;
873  }
874  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
875
876  iterPic   = pcListPic->begin();
877  TComPic* pcPic = *(iterPic);
878
879  if (pcPic->isField()) //Field Decoding
880  {
881    TComList<TComPic*>::iterator endPic   = pcListPic->end();
882    endPic--;
883    TComPic *pcPicTop, *pcPicBottom = NULL;
884    while (iterPic != endPic)
885    {
886      pcPicTop = *(iterPic);
887      iterPic++;
888      pcPicBottom = *(iterPic);
889
890      if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() && !(pcPicTop->getPOC()%2) && (pcPicBottom->getPOC() == pcPicTop->getPOC()+1) )
891      {
892        // write to file
893#if SVC_EXTENSION
894        if ( m_pchReconFile[layerId] )
895        {
896          const Window &conf = pcPicTop->getConformanceWindow();
897          const Window &defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
898          const Bool isTff = pcPicTop->isTopField();
899#if REPN_FORMAT_IN_VPS
900          UInt chromaFormatIdc = pcPic->getSlice(0)->getChromaFormatIdc();
901          Int xScal =  TComSPS::getWinUnitX( chromaFormatIdc ), yScal = TComSPS::getWinUnitY( chromaFormatIdc );
902          m_acTVideoIOYuvReconFile[layerId].write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
903            conf.getWindowLeftOffset()  *xScal + defDisp.getWindowLeftOffset(),
904            conf.getWindowRightOffset() *xScal + defDisp.getWindowRightOffset(),
905            conf.getWindowTopOffset()   *yScal + defDisp.getWindowTopOffset(),
906            conf.getWindowBottomOffset()*yScal + defDisp.getWindowBottomOffset(), isTff );
907
908#else
909#if O0194_DIFFERENT_BITDEPTH_EL_BL
910          // Compile time bug-fix
911          m_acTVideoIOYuvReconFile[layerId].write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
912#else
913          m_cTVideoIOYuvReconFile[layerId].write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
914#endif
915            conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
916            conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
917            conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
918            conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), isTff );
919#endif
920        }
921
922        // update POC of display order
923        m_aiPOCLastDisplay[layerId] = pcPicBottom->getPOC();
924#else
925        if ( m_pchReconFile )
926        {
927          const Window &conf = pcPicTop->getConformanceWindow();
928          const Window &defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
929          const Bool isTff = pcPicTop->isTopField();
930          m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
931            conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
932            conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
933            conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
934            conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), isTff );
935        }
936
937        // update POC of display order
938        m_iPOCLastDisplay = pcPicBottom->getPOC();
939#endif
940
941        // erase non-referenced picture in the reference picture list after display
942        if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
943        {
944#if !DYN_REF_FREE
945          pcPicTop->setReconMark(false);
946
947          // mark it should be extended later
948          pcPicTop->getPicYuvRec()->setBorderExtension( false );
949
950#else
951          pcPicTop->destroy();
952          pcListPic->erase( iterPic );
953          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
954          continue;
955#endif
956        }
957        if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
958        {
959#if !DYN_REF_FREE
960          pcPicBottom->setReconMark(false);
961
962          // mark it should be extended later
963          pcPicBottom->getPicYuvRec()->setBorderExtension( false );
964
965#else
966          pcPicBottom->destroy();
967          pcListPic->erase( iterPic );
968          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
969          continue;
970#endif
971        }
972        pcPicTop->setOutputMark(false);
973        pcPicBottom->setOutputMark(false);
974
975#if !DYN_REF_FREE
976        if(pcPicTop)
977        {
978          pcPicTop->destroy();
979          delete pcPicTop;
980          pcPicTop = NULL;
981        }
982#endif
983      }
984    }
985    if(pcPicBottom)
986    {
987      pcPicBottom->destroy();
988      delete pcPicBottom;
989      pcPicBottom = NULL;
990    }
991  }
992  else //Frame decoding
993  {
994    while (iterPic != pcListPic->end())
995    {
996      pcPic = *(iterPic);
997
998      if ( pcPic->getOutputMark() )
999      {
1000        // write to file
1001#if SVC_EXTENSION
1002        if ( m_pchReconFile[layerId] )
1003        {
1004          const Window &conf = pcPic->getConformanceWindow();
1005          const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
1006#if REPN_FORMAT_IN_VPS
1007          UInt chromaFormatIdc = pcPic->getSlice(0)->getChromaFormatIdc();
1008          Int xScal =  TComSPS::getWinUnitX( chromaFormatIdc ), yScal = TComSPS::getWinUnitY( chromaFormatIdc );
1009          m_acTVideoIOYuvReconFile[layerId].write( pcPic->getPicYuvRec(),
1010            conf.getWindowLeftOffset()  *xScal + defDisp.getWindowLeftOffset(),
1011            conf.getWindowRightOffset() *xScal + defDisp.getWindowRightOffset(),
1012            conf.getWindowTopOffset()   *yScal + defDisp.getWindowTopOffset(),
1013            conf.getWindowBottomOffset()*yScal + defDisp.getWindowBottomOffset() );
1014
1015#else
1016          m_acTVideoIOYuvReconFile[layerId].write( pcPic->getPicYuvRec(),
1017            conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
1018            conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
1019            conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
1020            conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
1021#endif
1022        }
1023
1024        // update POC of display order
1025        m_aiPOCLastDisplay[layerId] = pcPic->getPOC();
1026#else
1027        if ( m_pchReconFile )
1028        {
1029          const Window &conf = pcPic->getConformanceWindow();
1030          const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
1031          m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
1032            conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
1033            conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
1034            conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
1035            conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
1036        }
1037
1038        // update POC of display order
1039        m_iPOCLastDisplay = pcPic->getPOC();
1040#endif
1041
1042        // erase non-referenced picture in the reference picture list after display
1043        if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
1044        {
1045#if !DYN_REF_FREE
1046          pcPic->setReconMark(false);
1047
1048          // mark it should be extended later
1049          pcPic->getPicYuvRec()->setBorderExtension( false );
1050
1051#else
1052          pcPic->destroy();
1053          pcListPic->erase( iterPic );
1054          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
1055          continue;
1056#endif
1057        }
1058        pcPic->setOutputMark(false);
1059      }
1060#if !SVC_EXTENSION
1061#if !DYN_REF_FREE
1062      if(pcPic)
1063      {
1064        pcPic->destroy();
1065        delete pcPic;
1066        pcPic = NULL;
1067      }
1068#endif
1069#endif
1070      iterPic++;
1071    }
1072  }
1073#if SVC_EXTENSION
1074  m_aiPOCLastDisplay[layerId] = -MAX_INT;
1075#else
1076  pcListPic->clear();
1077  m_iPOCLastDisplay = -MAX_INT;
1078#endif
1079}
1080
1081/** \param nalu Input nalu to check whether its LayerId is within targetDecLayerIdSet
1082 */
1083Bool TAppDecTop::isNaluWithinTargetDecLayerIdSet( InputNALUnit* nalu )
1084{
1085  if ( m_targetDecLayerIdSet.size() == 0 ) // By default, the set is empty, meaning all LayerIds are allowed
1086  {
1087    return true;
1088  }
1089  for (std::vector<Int>::iterator it = m_targetDecLayerIdSet.begin(); it != m_targetDecLayerIdSet.end(); it++)
1090  {
1091    if ( nalu->m_reservedZero6Bits == (*it) )
1092    {
1093      return true;
1094    }
1095  }
1096  return false;
1097}
1098#if ALIGNED_BUMPING
1099// Function outputs a picture, and marks it as not needed for output.
1100Void TAppDecTop::xOutputAndMarkPic( TComPic *pic, const Char *reconFile, const Int layerIdx, Int &pocLastDisplay, DpbStatus &dpbStatus )
1101{
1102  if ( reconFile )
1103  {
1104    const Window &conf = pic->getConformanceWindow();
1105    const Window &defDisp = m_respectDefDispWindow ? pic->getDefDisplayWindow() : Window();
1106    Int xScal =  1, yScal = 1;
1107#if REPN_FORMAT_IN_VPS
1108    UInt chromaFormatIdc = pic->getSlice(0)->getChromaFormatIdc();
1109    xScal = TComSPS::getWinUnitX( chromaFormatIdc );
1110    yScal = TComSPS::getWinUnitY( chromaFormatIdc );
1111#endif
1112    m_acTVideoIOYuvReconFile[layerIdx].write( pic->getPicYuvRec(),
1113      conf.getWindowLeftOffset()  * xScal + defDisp.getWindowLeftOffset(),
1114      conf.getWindowRightOffset() * xScal + defDisp.getWindowRightOffset(),
1115      conf.getWindowTopOffset()   * yScal + defDisp.getWindowTopOffset(),
1116      conf.getWindowBottomOffset()* yScal + defDisp.getWindowBottomOffset() );
1117  }
1118  // update POC of display order
1119  pocLastDisplay = pic->getPOC();
1120
1121  // Mark as not needed for output
1122  pic->setOutputMark(false);
1123
1124  // "erase" non-referenced picture in the reference picture list after display
1125  if ( !pic->getSlice(0)->isReferenced() && pic->getReconMark() == true )
1126  {
1127    pic->setReconMark(false);
1128
1129    // mark it should be extended later
1130    pic->getPicYuvRec()->setBorderExtension( false );
1131
1132    dpbStatus.m_numPicsInLayer[layerIdx]--;
1133  }
1134}
1135
1136Void TAppDecTop::flushAllPictures(Int layerId, Bool outputPictures)
1137{
1138  // First "empty" all pictures that are not used for reference and not needed for output
1139  emptyUnusedPicturesNotNeededForOutput();
1140
1141  if( outputPictures )  // All pictures in the DPB in that layer are to be output; this means other pictures would also be output
1142  {
1143    std::vector<Int>  listOfPocs;
1144    std::vector<Int>  listOfPocsInEachLayer[MAX_LAYERS];
1145    std::vector<Int>  listOfPocsPositionInEachLayer[MAX_LAYERS];
1146    DpbStatus dpbStatus;
1147
1148    // Find the status of the DPB
1149    xFindDPBStatus(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus);
1150
1151    if( listOfPocs.size() )
1152    {
1153      while( listOfPocsInEachLayer[layerId].size() )    // As long as there picture in the layer to be output
1154      {
1155        bumpingProcess( listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus );
1156      }
1157    }
1158  }
1159
1160  // Now remove all pictures from the layer DPB?
1161  markAllPicturesAsErased(layerId);
1162}
1163Void TAppDecTop::flushAllPictures(Bool outputPictures)
1164{
1165  // First "empty" all pictures that are not used for reference and not needed for output
1166  emptyUnusedPicturesNotNeededForOutput();
1167
1168  if( outputPictures )  // All pictures in the DPB are to be output
1169  {
1170    std::vector<Int>  listOfPocs;
1171    std::vector<Int>  listOfPocsInEachLayer[MAX_LAYERS];
1172    std::vector<Int>  listOfPocsPositionInEachLayer[MAX_LAYERS];
1173    DpbStatus dpbStatus;
1174
1175    // Find the status of the DPB
1176    xFindDPBStatus(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus);
1177
1178    while( dpbStatus.m_numAUsNotDisplayed )
1179    {
1180      bumpingProcess( listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus );
1181    }
1182  }
1183
1184  // Now remove all pictures from the DPB?
1185  markAllPicturesAsErased();
1186}
1187
1188Void TAppDecTop::markAllPicturesAsErased()
1189{
1190  for(Int i = 0; i < MAX_LAYERS; i++)
1191  {
1192    m_acTDecTop[i].getListPic()->clear();
1193  }
1194}
1195
1196Void TAppDecTop::markAllPicturesAsErased(Int layerIdx)
1197{
1198  m_acTDecTop[layerIdx].getListPic()->clear();
1199}
1200
1201Void TAppDecTop::checkOutputBeforeDecoding(Int layerIdx)
1202{
1203   
1204  std::vector<Int>  listOfPocs;
1205  std::vector<Int>  listOfPocsInEachLayer[MAX_LAYERS];
1206  std::vector<Int>  listOfPocsPositionInEachLayer[MAX_LAYERS];
1207  DpbStatus dpbStatus;
1208
1209  // First "empty" all pictures that are not used for reference and not needed for output
1210  emptyUnusedPicturesNotNeededForOutput();
1211
1212  // Find the status of the DPB
1213  xFindDPBStatus(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus);
1214
1215  // If not picture to be output, return
1216  if( listOfPocs.size() == 0 )
1217  {
1218    return;
1219  }
1220
1221  // Find DPB-information from the VPS
1222  DpbStatus maxDpbLimit;
1223  Int targetLsIdx, subDpbIdx;
1224  TComVPS *vps = findDpbParametersFromVps(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, maxDpbLimit);
1225  if( getCommonDecoderParams()->getTargetOutputLayerSetIdx() == 0 )
1226  {
1227    targetLsIdx = 0;
1228    subDpbIdx   = 0; 
1229  }
1230  else
1231  {
1232    targetLsIdx = vps->getOutputLayerSetIdx( getCommonDecoderParams()->getTargetOutputLayerSetIdx() );
1233    subDpbIdx   = vps->getSubDpbAssigned( targetLsIdx, layerIdx );
1234  }
1235  // Assume that listOfPocs is sorted in increasing order - if not have to sort it.
1236  while( ifInvokeBumpingBeforeDecoding(dpbStatus, maxDpbLimit, layerIdx, subDpbIdx) )
1237  {
1238    bumpingProcess( listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus );
1239  } 
1240}
1241
1242Void TAppDecTop::checkOutputAfterDecoding()
1243{   
1244  std::vector<Int>  listOfPocs;
1245  std::vector<Int>  listOfPocsInEachLayer[MAX_LAYERS];
1246  std::vector<Int>  listOfPocsPositionInEachLayer[MAX_LAYERS];
1247  DpbStatus dpbStatus;
1248
1249  // First "empty" all pictures that are not used for reference and not needed for output
1250  emptyUnusedPicturesNotNeededForOutput();
1251
1252  // Find the status of the DPB
1253  xFindDPBStatus(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus);
1254
1255  // If not picture to be output, return
1256  if( listOfPocs.size() == 0 )
1257  {
1258    return;
1259  }
1260
1261  // Find DPB-information from the VPS
1262  DpbStatus maxDpbLimit;
1263  findDpbParametersFromVps(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, maxDpbLimit);
1264
1265  // Assume that listOfPocs is sorted in increasing order - if not have to sort it.
1266  while( ifInvokeBumpingAfterDecoding(dpbStatus, maxDpbLimit) )
1267  {
1268    bumpingProcess( listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus );
1269  } 
1270}
1271
1272Void TAppDecTop::bumpingProcess(std::vector<Int> &listOfPocs, std::vector<Int> *listOfPocsInEachLayer, std::vector<Int> *listOfPocsPositionInEachLayer, DpbStatus &dpbStatus)
1273{
1274  // Choose the smallest POC value
1275  Int pocValue = *(listOfPocs.begin());
1276  std::vector<int>::iterator it;
1277  TComList<TComPic*>::iterator iterPic;
1278  for( Int layerIdx = 0; layerIdx < dpbStatus.m_numLayers; layerIdx++)
1279  {
1280    // Check if picture with pocValue is present.
1281    it = find( listOfPocsInEachLayer[layerIdx].begin(), listOfPocsInEachLayer[layerIdx].end(), pocValue );
1282    if( it != listOfPocsInEachLayer[layerIdx].end() )  // picture found.
1283    {
1284      Int picPosition = std::distance( listOfPocsInEachLayer[layerIdx].begin(), it );
1285      Int j;
1286      for(j = 0, iterPic = m_acTDecTop[layerIdx].getListPic()->begin(); j < listOfPocsPositionInEachLayer[layerIdx][picPosition]; j++) // Picture to be output
1287      {
1288        iterPic++;
1289      }
1290      TComPic *pic = *iterPic;
1291
1292      xOutputAndMarkPic( pic, m_pchReconFile[layerIdx], layerIdx, m_aiPOCLastDisplay[layerIdx], dpbStatus );
1293
1294      listOfPocsInEachLayer[layerIdx].erase( it );
1295      listOfPocsPositionInEachLayer[layerIdx].erase( listOfPocsPositionInEachLayer[layerIdx].begin() + picPosition );
1296    }
1297  }
1298  // Update sub-DPB status
1299  for( Int subDpbIdx = 0; subDpbIdx < dpbStatus.m_numSubDpbs; subDpbIdx++)
1300  {
1301    dpbStatus.m_numPicsInSubDpb[subDpbIdx]--;
1302  }
1303  dpbStatus.m_numAUsNotDisplayed--;   
1304
1305  // Remove the picture from the listOfPocs
1306  listOfPocs.erase( listOfPocs.begin() );
1307}
1308
1309TComVPS *TAppDecTop::findDpbParametersFromVps(std::vector<Int> const &listOfPocs, std::vector<Int> const *listOfPocsInEachLayer, std::vector<Int> const *listOfPocsPositionInEachLayer, DpbStatus &maxDpbLimit)
1310{
1311  Int targetOutputLsIdx = getCommonDecoderParams()->getTargetOutputLayerSetIdx();
1312  TComVPS *vps = NULL;
1313
1314  if( targetOutputLsIdx == 0 )   // Only base layer is output
1315  {
1316    TComSPS *sps = NULL;
1317    assert( listOfPocsInEachLayer[0].size() != 0 );
1318    TComList<TComPic*>::iterator iterPic;
1319    Int j;
1320    for(j = 0, iterPic = m_acTDecTop[0].getListPic()->begin(); j < listOfPocsPositionInEachLayer[0][0]; j++) // Picture to be output
1321    {
1322      iterPic++;
1323    }
1324    TComPic *pic = *iterPic;
1325    sps = pic->getSlice(0)->getSPS();   assert( sps->getLayerId() == 0 );
1326    vps = pic->getSlice(0)->getVPS();
1327    Int highestTId = sps->getMaxTLayers() - 1;
1328
1329    maxDpbLimit.m_numAUsNotDisplayed = sps->getNumReorderPics( highestTId ); // m_numAUsNotDisplayed is only variable name - stores reorderpics
1330    maxDpbLimit.m_maxLatencyIncrease = sps->getMaxLatencyIncrease( highestTId ) > 0;
1331    if( maxDpbLimit.m_maxLatencyIncrease )
1332    {
1333      maxDpbLimit.m_maxLatencyPictures = sps->getMaxLatencyIncrease( highestTId ) + sps->getNumReorderPics( highestTId ) - 1;
1334    }
1335    maxDpbLimit.m_numPicsInLayer[0] = sps->getMaxDecPicBuffering( highestTId );
1336    maxDpbLimit.m_numPicsInSubDpb[0] = sps->getMaxDecPicBuffering( highestTId );
1337  }
1338  else
1339  {
1340    // -------------------------------------
1341    // Find the VPS used for the pictures
1342    // -------------------------------------
1343    for(Int i = 0; i < MAX_LAYERS; i++)
1344    {
1345      if( m_acTDecTop[i].getListPic()->empty() )
1346      {
1347        assert( listOfPocsInEachLayer[i].size() == 0 );
1348        continue;
1349      }
1350      std::vector<Int>::const_iterator it;
1351      it = find( listOfPocsInEachLayer[i].begin(), listOfPocsInEachLayer[i].end(), listOfPocs[0] );
1352      TComList<TComPic*>::iterator iterPic;
1353      if( it != listOfPocsInEachLayer[i].end() )
1354      {
1355        Int picPosition = std::distance( listOfPocsInEachLayer[i].begin(), it );
1356        Int j;
1357        for(j = 0, iterPic = m_acTDecTop[i].getListPic()->begin(); j < listOfPocsPositionInEachLayer[i][picPosition]; j++) // Picture to be output
1358        {
1359          iterPic++;
1360        }
1361        TComPic *pic = *iterPic;
1362        vps = pic->getSlice(0)->getVPS();
1363        break;
1364      }
1365    }
1366
1367    Int targetLsIdx       = vps->getOutputLayerSetIdx( getCommonDecoderParams()->getTargetOutputLayerSetIdx() );
1368    Int highestTId = vps->getMaxTLayers() - 1;
1369
1370    maxDpbLimit.m_numAUsNotDisplayed = vps->getMaxVpsNumReorderPics( targetOutputLsIdx, highestTId ); // m_numAUsNotDisplayed is only variable name - stores reorderpics
1371    maxDpbLimit.m_maxLatencyIncrease  = vps->getMaxVpsLatencyIncreasePlus1(targetOutputLsIdx, highestTId ) > 0;
1372    if( maxDpbLimit.m_maxLatencyIncrease )
1373    {
1374      maxDpbLimit.m_maxLatencyPictures = vps->getMaxVpsNumReorderPics( targetOutputLsIdx, highestTId ) + vps->getMaxVpsLatencyIncreasePlus1(targetOutputLsIdx, highestTId ) - 1;
1375    }
1376    for(Int i = 0; i < vps->getNumLayersInIdList( targetLsIdx ); i++)
1377    {
1378      maxDpbLimit.m_numPicsInLayer[i] = vps->getMaxVpsLayerDecPicBuffMinus1( targetOutputLsIdx, i, highestTId ) + 1;
1379      maxDpbLimit.m_numPicsInSubDpb[vps->getSubDpbAssigned( targetLsIdx, i )] = vps->getMaxVpsDecPicBufferingMinus1( targetOutputLsIdx, vps->getSubDpbAssigned( targetLsIdx, i ), highestTId) + 1;
1380    }
1381    // -------------------------------------
1382  }
1383  return vps;
1384}
1385Void TAppDecTop::emptyUnusedPicturesNotNeededForOutput()
1386{
1387  for(Int layerIdx = 0; layerIdx < MAX_LAYERS; layerIdx++)
1388  {
1389    TComList <TComPic*> *pcListPic = m_acTDecTop[layerIdx].getListPic();
1390    TComList<TComPic*>::iterator iterPic = pcListPic->begin();
1391    while ( iterPic != pcListPic->end() )
1392    {
1393      TComPic *pic = *iterPic;
1394      if( !pic->getSlice(0)->isReferenced() && !pic->getOutputMark() )
1395      {
1396        // Emtpy the picture buffer
1397        pic->setReconMark( false );
1398      }
1399      iterPic++;
1400    }
1401  }
1402}
1403
1404Bool TAppDecTop::ifInvokeBumpingBeforeDecoding( const DpbStatus &dpbStatus, const DpbStatus &dpbLimit, const Int layerIdx, const Int subDpbIdx )
1405{
1406  Bool retVal = false;
1407  // Number of reorder picutres
1408  retVal |= ( dpbStatus.m_numAUsNotDisplayed > dpbLimit.m_numAUsNotDisplayed );
1409
1410  // Number of pictures in each sub-DPB
1411  retVal |= ( dpbStatus.m_numPicsInSubDpb[subDpbIdx] >= dpbLimit.m_numPicsInSubDpb[subDpbIdx] );
1412 
1413  // Number of pictures in each layer
1414  retVal |= ( dpbStatus.m_numPicsInLayer[layerIdx] >= dpbLimit.m_numPicsInLayer[layerIdx]);
1415
1416  return retVal;
1417}
1418
1419Bool TAppDecTop::ifInvokeBumpingAfterDecoding( const DpbStatus &dpbStatus, const DpbStatus &dpbLimit )
1420{
1421  Bool retVal = false;
1422
1423  // Number of reorder picutres
1424  retVal |= ( dpbStatus.m_numAUsNotDisplayed > dpbLimit.m_numAUsNotDisplayed );
1425
1426  return retVal;
1427}
1428
1429Void TAppDecTop::xFindDPBStatus( std::vector<Int> &listOfPocs
1430                            , std::vector<Int> *listOfPocsInEachLayer
1431                            , std::vector<Int> *listOfPocsPositionInEachLayer
1432                            , DpbStatus &dpbStatus
1433                            )
1434{
1435  TComVPS *vps = NULL;
1436  dpbStatus.init();
1437  for( Int i = 0; i < MAX_LAYERS; i++ )
1438  {
1439    if( m_acTDecTop[i].getListPic()->empty() )
1440    {
1441      continue;
1442    }
1443   
1444    // To check # AUs that have at least one picture not output,
1445    // For each layer, populate listOfPOcs if not already present
1446    TComList<TComPic*>::iterator iterPic = m_acTDecTop[i].getListPic()->begin();
1447    Int picPositionInList = 0;
1448    while (iterPic != m_acTDecTop[i].getListPic()->end())
1449    {
1450      TComPic* pic = *(iterPic);
1451      if( pic->getReconMark() )
1452      {
1453        if( vps == NULL )
1454        {
1455          vps = pic->getSlice(0)->getVPS();
1456        }
1457
1458        std::vector<Int>::iterator it;
1459        if( pic->getOutputMark() ) // && pic->getPOC() > m_aiPOCLastDisplay[i])
1460        {
1461          it = find( listOfPocs.begin(), listOfPocs.end(), pic->getPOC() ); // Check if already included
1462          if( it == listOfPocs.end() )  // New POC value - i.e. new AU - add to the list
1463          {
1464            listOfPocs.push_back( pic->getPOC() );
1465          }
1466          listOfPocsInEachLayer         [i].push_back( pic->getPOC()    );    // POC to be output in each layer
1467          listOfPocsPositionInEachLayer [i].push_back( picPositionInList  );  // For ease of access
1468        }
1469        if( pic->getSlice(0)->isReferenced() || pic->getOutputMark() )
1470        {
1471          dpbStatus.m_numPicsInLayer[i]++;  // Count pictures that are "used for reference" or "needed for output"
1472        }
1473      }
1474      iterPic++;
1475      picPositionInList++;
1476    }
1477  }
1478
1479  assert( vps != NULL );    // No picture in any DPB?
1480  std::sort( listOfPocs.begin(), listOfPocs.end() );    // Sort in increasing order of POC
1481  Int targetLsIdx = vps->getOutputLayerSetIdx( getCommonDecoderParams()->getTargetOutputLayerSetIdx() );
1482  // Update status
1483  dpbStatus.m_numAUsNotDisplayed = listOfPocs.size();   // Number of AUs not displayed
1484  dpbStatus.m_numLayers = vps->getNumLayersInIdList( targetLsIdx );
1485  dpbStatus.m_numSubDpbs = vps->getNumSubDpbs( vps->getOutputLayerSetIdx(
1486                                                      this->getCommonDecoderParams()->getTargetOutputLayerSetIdx() ) );
1487
1488  for(Int i = 0; i < dpbStatus.m_numLayers; i++)
1489  {
1490    dpbStatus.m_numPicsNotDisplayedInLayer[i] = listOfPocsInEachLayer[i].size();
1491    dpbStatus.m_numPicsInSubDpb[vps->getSubDpbAssigned(targetLsIdx,i)] += dpbStatus.m_numPicsInLayer[i];
1492  }
1493  assert( dpbStatus.m_numAUsNotDisplayed != -1 );
1494
1495
1496} 
1497#endif
1498//! \}
Note: See TracBrowser for help on using the repository browser.