source: SHVCSoftware/trunk/source/App/TAppDecoder/TAppDecTop.cpp @ 757

Last change on this file since 757 was 713, checked in by seregin, 11 years ago

merge with SHM-6-dev

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