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

Last change on this file since 1472 was 1472, checked in by seregin, 9 years ago

fix writing bitdepth

  • Property svn:eol-style set to native
File size: 73.0 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-2015, 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
44#include "TAppDecTop.h"
45#include "TLibDecoder/AnnexBread.h"
46#include "TLibDecoder/NALread.h"
47#if RExt__DECODER_DEBUG_BIT_STATISTICS
48#include "TLibCommon/TComCodingStatistics.h"
49#endif
50#if CONFORMANCE_BITSTREAM_MODE
51#include "TLibCommon/TComPicYuv.h"
52#include "libmd5/MD5.h"
53#endif
54
55//! \ingroup TAppDecoder
56//! \{
57
58// ====================================================================================================================
59// Constructor / destructor / initialization / destroy
60// ====================================================================================================================
61
62#if SVC_EXTENSION
63TAppDecTop::TAppDecTop()
64#if Q0074_COLOUR_REMAPPING_SEI
65: m_pcSeiColourRemappingInfoPrevious(NULL)
66#endif
67{
68  memset( m_apcTDecTop, 0, sizeof(m_apcTDecTop) );
69  memset( m_apcTVideoIOYuvReconFile, 0, sizeof(m_apcTVideoIOYuvReconFile) );
70
71  for(UInt layer=0; layer < MAX_LAYERS; layer++)
72  {
73    m_aiPOCLastDisplay[layer]  = -MAX_INT;
74  }
75}
76#else
77TAppDecTop::TAppDecTop()
78: m_iPOCLastDisplay(-MAX_INT)
79#if Q0074_COLOUR_REMAPPING_SEI
80 ,m_pcSeiColourRemappingInfoPrevious(NULL)
81#endif
82{
83}
84#endif
85
86Void TAppDecTop::create()
87{
88}
89
90Void TAppDecTop::destroy()
91{
92  m_bitstreamFileName.clear();
93#if SVC_EXTENSION
94#if CONFORMANCE_BITSTREAM_MODE
95  for(Int i = 0; i < MAX_VPS_LAYER_IDX_PLUS1; i++ )
96#else
97  for( Int i = 0; i <= m_tgtLayerId; i++ )
98#endif
99  {
100    m_reconFileName[i].clear();
101
102    if( m_apcTDecTop[i] )
103    {
104      delete m_apcTDecTop[i];
105      m_apcTDecTop[i] = NULL;
106    }
107
108    if( m_apcTVideoIOYuvReconFile[i] )
109    {
110      delete m_apcTVideoIOYuvReconFile[i];
111      m_apcTVideoIOYuvReconFile[i] = NULL;
112    }
113  }
114#if AVC_BASE
115  m_reconFileNameBL.clear();
116#endif
117#else
118  m_reconFileName.clear();
119#endif
120}
121
122// ====================================================================================================================
123// Public member functions
124// ====================================================================================================================
125
126/**
127 - create internal class
128 - initialize internal class
129 - until the end of the bitstream, call decoding function in TDecTop class
130 - delete allocated buffers
131 - destroy internal class
132 .
133 */
134Void TAppDecTop::decode()
135{
136  Int                 poc;
137  TComList<TComPic*>* pcListPic = NULL;
138
139  ifstream bitstreamFile(m_bitstreamFileName.c_str(), ifstream::in | ifstream::binary);
140  if (!bitstreamFile)
141  {
142    fprintf(stderr, "\nfailed to open bitstream file `%s' for reading\n", m_bitstreamFileName.c_str());
143    exit(EXIT_FAILURE);
144  }
145
146  InputByteStream bytestream(bitstreamFile);
147
148  if (!m_outputDecodedSEIMessagesFilename.empty() && m_outputDecodedSEIMessagesFilename!="-")
149  {
150    m_seiMessageFileStream.open(m_outputDecodedSEIMessagesFilename.c_str(), std::ios::out);
151    if (!m_seiMessageFileStream.is_open() || !m_seiMessageFileStream.good())
152    {
153      fprintf(stderr, "\nUnable to open file `%s' for writing decoded SEI messages\n", m_outputDecodedSEIMessagesFilename.c_str());
154      exit(EXIT_FAILURE);
155    }
156  }
157
158  // create & initialize internal classes
159  xCreateDecLib();
160  xInitDecLib  ();
161#if !SVC_EXTENSION
162  m_iPOCLastDisplay += m_iSkipFrame;      // set the last displayed POC correctly for skip forward.
163#endif
164
165#if Q0074_COLOUR_REMAPPING_SEI
166  // clear contents of colour-remap-information-SEI output file
167  if (!m_colourRemapSEIFileName.empty())
168  {
169    std::ofstream ofile(m_colourRemapSEIFileName.c_str());
170    if (!ofile.good() || !ofile.is_open())
171    {
172      fprintf(stderr, "\nUnable to open file '%s' for writing colour-remap-information-SEI video\n", m_colourRemapSEIFileName.c_str());
173      exit(EXIT_FAILURE);
174    }
175  }
176#endif
177
178  // main decoder loop
179#if SVC_EXTENSION
180  Bool openedReconFile[MAX_LAYERS]; // reconstruction file not yet opened. (must be performed after SPS is seen)
181  Bool loopFiltered[MAX_LAYERS];
182  memset( loopFiltered, false, sizeof( loopFiltered ) );
183
184#if CONFORMANCE_BITSTREAM_MODE
185  for(UInt layer = 0; layer < MAX_VPS_LAYER_IDX_PLUS1; layer++)
186#else
187  for(UInt layer=0; layer<=m_tgtLayerId; layer++)
188#endif
189  {
190    openedReconFile[layer] = false;
191    m_aiPOCLastDisplay[layer] += m_iSkipFrame;      // set the last displayed POC correctly for skip forward.
192  }
193
194  UInt curLayerId = 0;     // current layer to be reconstructed
195
196#if AVC_BASE
197  TComPic pcBLPic;
198  fstream streamYUV;
199  if( !m_reconFileNameBL.empty() )
200  {
201    streamYUV.open( m_reconFileNameBL.c_str(), fstream::in | fstream::binary );
202  }
203  TComList<TComPic*> *cListPic = m_apcTDecTop[0]->getListPic();
204  m_apcTDecTop[0]->setBLReconFile( &streamYUV );
205  pcBLPic.setLayerId( 0 );
206  cListPic->pushBack( &pcBLPic );
207#endif
208
209  while (!!bitstreamFile)
210  {
211    /* location serves to work around a design fault in the decoder, whereby
212     * the process of reading a new slice that is the first slice of a new frame
213     * requires the TDecTop::decode() method to be called again with the same
214     * nal unit. */
215#if RExt__DECODER_DEBUG_BIT_STATISTICS
216    TComCodingStatistics::TComCodingStatisticsData backupStats(TComCodingStatistics::GetStatistics());
217    streampos location = bitstreamFile.tellg() - streampos(bytestream.GetNumBufferedBytes());
218#else
219    streampos location = bitstreamFile.tellg();
220#endif
221    AnnexBStats stats = AnnexBStats();
222
223    InputNALUnit nalu;
224    byteStreamNALUnit(bytestream, nalu.getBitstream().getFifo(), stats);
225
226    // call actual decoding function
227    Bool bNewPicture = false;
228    Bool bNewPOC = false;
229
230    if (nalu.getBitstream().getFifo().empty())
231    {
232      /* this can happen if the following occur:
233       *  - empty input file
234       *  - two back-to-back start_code_prefixes
235       *  - start_code_prefix immediately followed by EOF
236       */
237      fprintf(stderr, "Warning: Attempt to decode an empty NAL unit\n");
238    }
239    else
240    {
241      read(nalu);
242      if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) || !isNaluWithinTargetDecLayerIdSet(&nalu)  ||
243#if CONFORMANCE_BITSTREAM_MODE
244        (nalu.m_nuhLayerId > m_commonDecoderParams.getTargetLayerId()) )
245#else
246        (nalu.m_nuhLayerId > m_tgtLayerId) )
247#endif
248      {
249        bNewPicture = false;
250      }
251      else
252      {
253        bNewPicture = m_apcTDecTop[nalu.m_nuhLayerId]->decode(nalu, m_iSkipFrame, m_aiPOCLastDisplay[nalu.m_nuhLayerId], curLayerId, bNewPOC);
254
255#if SVC_POC
256        if( (bNewPicture && m_apcTDecTop[nalu.m_nuhLayerId]->getParseIdc() == 3) || (m_apcTDecTop[nalu.m_nuhLayerId]->getParseIdc() == 0) )
257#else
258        if (bNewPicture)
259#endif
260        {
261          bitstreamFile.clear();
262          /* location points to the current nalunit payload[1] due to the
263           * need for the annexB parser to read three extra bytes.
264           * [1] except for the first NAL unit in the file
265           *     (but bNewPicture doesn't happen then) */
266#if RExt__DECODER_DEBUG_BIT_STATISTICS
267          bitstreamFile.seekg(location);
268          bytestream.reset();
269          TComCodingStatistics::SetStatistics(backupStats);
270#else
271          bitstreamFile.seekg(location-streamoff(3));
272          bytestream.reset();
273#endif
274        }
275#if SVC_POC
276        else if(m_apcTDecTop[nalu.m_nuhLayerId]->getParseIdc() == 1) 
277        {
278          bitstreamFile.clear();
279          // This is before third parse of the NAL unit, and
280          // location points to correct beginning of the NALU
281          bitstreamFile.seekg(location);
282          bytestream.reset();
283#if RExt__DECODER_DEBUG_BIT_STATISTICS
284          TComCodingStatistics::SetStatistics(backupStats);
285#endif
286        }
287#endif
288      }
289    }
290
291#if SVC_POC
292    if( ( (bNewPicture && m_apcTDecTop[nalu.m_nuhLayerId]->getParseIdc() == 3) || m_apcTDecTop[nalu.m_nuhLayerId]->getParseIdc() == 0 || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS ) && 
293        !m_apcTDecTop[nalu.m_nuhLayerId]->getFirstSliceInSequence() )
294#else
295    if ( (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS) &&
296        !m_apcTDecTop[nalu.m_nuhLayerId]->getFirstSliceInSequence() )
297#endif
298    {
299      if (!loopFiltered[curLayerId] || bitstreamFile)
300      {
301        m_apcTDecTop[curLayerId]->executeLoopFilters(poc, pcListPic);
302      }
303      loopFiltered[curLayerId] = (nalu.m_nalUnitType == NAL_UNIT_EOS);
304
305      if (nalu.m_nalUnitType == NAL_UNIT_EOS)
306      {
307        m_apcTDecTop[nalu.m_nuhLayerId]->setFirstSliceInSequence(true);
308      }
309    }
310    else if ( (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS ) &&
311              m_apcTDecTop[nalu.m_nuhLayerId]->getFirstSliceInSequence () ) 
312    {
313      m_apcTDecTop[nalu.m_nuhLayerId]->setFirstSliceInPicture (true);
314    }
315
316#if SVC_POC
317    if( bNewPicture && m_apcTDecTop[nalu.m_nuhLayerId]->getParseIdc() == 0 )
318    {
319      outputAllPictures( nalu.m_nuhLayerId, true );
320    }
321#endif
322
323    if( pcListPic )
324    {
325      if ( !m_reconFileName[curLayerId].empty() && !openedReconFile[curLayerId] )
326      {
327        const BitDepths& bitDepths = m_apcTDecTop[curLayerId]->getParameterSetManager()->getActiveVPS()->getBitDepths( m_apcTDecTop[curLayerId]->getParameterSetManager()->getActiveSPS(), curLayerId );
328
329        for( UInt channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
330        {
331          if( m_outputBitDepth[curLayerId][channelType] == 0 )
332          {
333            m_outputBitDepth[curLayerId][channelType] = bitDepths.recon[channelType];
334          }
335        }
336        m_apcTVideoIOYuvReconFile[curLayerId]->open( m_reconFileName[curLayerId], true, m_outputBitDepth[curLayerId], m_outputBitDepth[curLayerId], bitDepths.recon ); // write mode
337
338        openedReconFile[curLayerId] = true;
339      }
340#if ALIGNED_BUMPING
341      Bool outputPicturesFlag = true; 
342
343      if( m_apcTDecTop[nalu.m_nuhLayerId]->getNoOutputPriorPicsFlag() )
344      {
345        outputPicturesFlag = false;
346      }
347
348      if (nalu.m_nalUnitType == NAL_UNIT_EOS) // End of sequence
349      {
350        flushAllPictures( nalu.m_nuhLayerId, outputPicturesFlag );       
351      }
352
353#if SVC_POC
354      if( bNewPicture && m_apcTDecTop[nalu.m_nuhLayerId]->getParseIdc() != 0 )
355      // New picture, slice header parsed but picture not decoded
356#else
357      if( bNewPicture ) // New picture, slice header parsed but picture not decoded
358#endif
359      {
360         if(   nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
361            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
362            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP
363            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL
364            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP   )
365        {
366          flushAllPictures( nalu.m_nuhLayerId, outputPicturesFlag );
367        }
368        else
369        {
370          this->checkOutputBeforeDecoding( nalu.m_nuhLayerId );
371        }
372      }
373
374      /* The following code has to be executed when the last DU of the picture is decoded
375         TODO: Need code to identify end of decoding a picture
376      {
377        this->checkOutputAfterDecoding( );
378      } */
379#else
380      if ( bNewPicture && bNewPOC &&
381           (   nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
382            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
383            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP
384            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL
385            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP ) )
386      {
387        xFlushOutput( pcListPic, curLayerId );
388      }
389      if (nalu.m_nalUnitType == NAL_UNIT_EOS)
390      {
391        xFlushOutput( pcListPic, curLayerId );       
392      }
393      // write reconstruction to file
394      if(bNewPicture)
395      {
396        xWriteOutput( pcListPic, curLayerId, nalu.m_temporalId );
397      }
398#endif
399    }
400  }
401#if ALIGNED_BUMPING
402   flushAllPictures( true );   
403#else
404  for(UInt layer = 0; layer <= m_tgtLayerId; layer++)
405  {
406    xFlushOutput( m_apcTDecTop[layer]->getListPic(), layer );
407  }
408#endif
409  // delete buffers
410#if AVC_BASE
411  UInt layerIdxmin = m_apcTDecTop[0]->getBLReconFile()->is_open() ? 1 : 0;
412
413  if( streamYUV.is_open() )
414  {
415    streamYUV.close();
416  }
417
418#if CONFORMANCE_BITSTREAM_MODE
419  for(UInt layer = layerIdxmin; layer < MAX_VPS_LAYER_IDX_PLUS1; layer++)
420#else
421  for(UInt layer = layerIdxmin; layer <= m_tgtLayerId; layer++)
422#endif
423#else
424  for(UInt layer = 0; layer <= m_tgtLayerId; layer++)
425#endif
426  {
427    m_apcTDecTop[layer]->deletePicBuffer();
428  }
429
430  // destroy internal classes
431  xDestroyDecLib();
432#else
433  Bool openedReconFile = false; // reconstruction file not yet opened. (must be performed after SPS is seen)
434  Bool loopFiltered = false;
435
436  while (!!bitstreamFile)
437  {
438    /* location serves to work around a design fault in the decoder, whereby
439     * the process of reading a new slice that is the first slice of a new frame
440     * requires the TDecTop::decode() method to be called again with the same
441     * nal unit. */
442#if RExt__DECODER_DEBUG_BIT_STATISTICS
443    TComCodingStatistics::TComCodingStatisticsData backupStats(TComCodingStatistics::GetStatistics());
444    streampos location = bitstreamFile.tellg() - streampos(bytestream.GetNumBufferedBytes());
445#else
446    streampos location = bitstreamFile.tellg();
447#endif
448    AnnexBStats stats = AnnexBStats();
449
450    InputNALUnit nalu;
451    byteStreamNALUnit(bytestream, nalu.getBitstream().getFifo(), stats);
452
453    // call actual decoding function
454    Bool bNewPicture = false;
455    if (nalu.getBitstream().getFifo().empty())
456    {
457      /* this can happen if the following occur:
458       *  - empty input file
459       *  - two back-to-back start_code_prefixes
460       *  - start_code_prefix immediately followed by EOF
461       */
462      fprintf(stderr, "Warning: Attempt to decode an empty NAL unit\n");
463    }
464    else
465    {
466      read(nalu);
467      if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) || !isNaluWithinTargetDecLayerIdSet(&nalu)  )
468      {
469        bNewPicture = false;
470      }
471      else
472      {
473        bNewPicture = m_cTDecTop.decode(nalu, m_iSkipFrame, m_iPOCLastDisplay);
474        if (bNewPicture)
475        {
476          bitstreamFile.clear();
477          /* location points to the current nalunit payload[1] due to the
478           * need for the annexB parser to read three extra bytes.
479           * [1] except for the first NAL unit in the file
480           *     (but bNewPicture doesn't happen then) */
481#if RExt__DECODER_DEBUG_BIT_STATISTICS
482          bitstreamFile.seekg(location);
483          bytestream.reset();
484          TComCodingStatistics::SetStatistics(backupStats);
485#else
486          bitstreamFile.seekg(location-streamoff(3));
487          bytestream.reset();
488#endif
489        }
490      }
491    }
492
493    if ( (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS) &&
494        !m_cTDecTop.getFirstSliceInSequence () )
495    {
496      if (!loopFiltered || bitstreamFile)
497      {
498        m_cTDecTop.executeLoopFilters(poc, pcListPic);
499      }
500      loopFiltered = (nalu.m_nalUnitType == NAL_UNIT_EOS);
501      if (nalu.m_nalUnitType == NAL_UNIT_EOS)
502      {
503        m_cTDecTop.setFirstSliceInSequence(true);
504      }
505    }
506    else if ( (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS ) &&
507              m_cTDecTop.getFirstSliceInSequence () ) 
508    {
509      m_cTDecTop.setFirstSliceInPicture (true);
510    }
511
512    if( pcListPic )
513    {
514      if ( (!m_reconFileName.empty()) && (!openedReconFile) )
515      {
516        const BitDepths &bitDepths=pcListPic->front()->getPicSym()->getSPS().getBitDepths(); // use bit depths of first reconstructed picture.
517        for (UInt channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
518        {
519          if (m_outputBitDepth[channelType] == 0)
520          {
521            m_outputBitDepth[channelType] = bitDepths.recon[channelType];
522          }
523        }
524
525        m_cTVideoIOYuvReconFile.open( m_reconFileName, true, m_outputBitDepth, m_outputBitDepth, bitDepths.recon ); // write mode
526        openedReconFile = true;
527      }
528      // write reconstruction to file
529      if( bNewPicture )
530      {
531        xWriteOutput( pcListPic, nalu.m_temporalId );
532      }
533      if ( (bNewPicture || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA) && m_cTDecTop.getNoOutputPriorPicsFlag() )
534      {
535        m_cTDecTop.checkNoOutputPriorPics( pcListPic );
536        m_cTDecTop.setNoOutputPriorPicsFlag (false);
537      }
538      if ( bNewPicture &&
539           (   nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
540            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
541            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP
542            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL
543            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP ) )
544      {
545        xFlushOutput( pcListPic );
546      }
547      if (nalu.m_nalUnitType == NAL_UNIT_EOS)
548      {
549        xWriteOutput( pcListPic, nalu.m_temporalId );
550        m_cTDecTop.setFirstSliceInPicture (false);
551      }
552      // write reconstruction to file -- for additional bumping as defined in C.5.2.3
553      if(!bNewPicture && nalu.m_nalUnitType >= NAL_UNIT_CODED_SLICE_TRAIL_N && nalu.m_nalUnitType <= NAL_UNIT_RESERVED_VCL31)
554      {
555        xWriteOutput( pcListPic, nalu.m_temporalId );
556      }
557    }
558  }
559
560  xFlushOutput( pcListPic );
561  // delete buffers
562  m_cTDecTop.deletePicBuffer();
563
564  // destroy internal classes
565  xDestroyDecLib();
566#endif
567}
568
569// ====================================================================================================================
570// Protected member functions
571// ====================================================================================================================
572
573Void TAppDecTop::xCreateDecLib()
574{
575#if SVC_EXTENSION
576  // initialize global variables
577  initROM();
578
579#if CONFORMANCE_BITSTREAM_MODE
580  for(UInt layer = 0; layer < MAX_VPS_LAYER_IDX_PLUS1; layer++)
581#else
582  for(UInt layer = 0; layer <= m_tgtLayerId; layer++)
583#endif
584  {
585    m_apcTDecTop[layer] = new TDecTop;
586    m_apcTVideoIOYuvReconFile[layer] = new TVideoIOYuv;
587
588    // set layer ID
589    m_apcTDecTop[layer]->setLayerId                      ( layer );
590
591    // create decoder class
592    m_apcTDecTop[layer]->create();
593
594    m_apcTDecTop[layer]->setLayerDec(m_apcTDecTop);
595  }
596#else
597  // create decoder class
598  m_cTDecTop.create();
599#endif
600}
601
602Void TAppDecTop::xDestroyDecLib()
603{
604#if SVC_EXTENSION
605  // destroy ROM
606  destroyROM();
607
608#if CONFORMANCE_BITSTREAM_MODE
609  for(UInt layer = 0; layer < MAX_VPS_LAYER_IDX_PLUS1; layer++)
610#else
611  for(UInt layer = 0; layer <= m_tgtLayerId; layer++)
612#endif
613  {
614    if ( !m_reconFileName[layer].empty() )
615    {
616      m_apcTVideoIOYuvReconFile[layer]->close();
617    }
618
619    // destroy decoder class
620    m_apcTDecTop[layer]->destroy();
621  }
622#else
623  if ( !m_reconFileName.empty() )
624  {
625    m_cTVideoIOYuvReconFile.close();
626  }
627
628  // destroy decoder class
629  m_cTDecTop.destroy();
630#endif
631#if Q0074_COLOUR_REMAPPING_SEI
632  if (m_pcSeiColourRemappingInfoPrevious != NULL)
633  {
634    delete m_pcSeiColourRemappingInfoPrevious;
635    m_pcSeiColourRemappingInfoPrevious = NULL;
636  }
637#endif
638}
639
640Void TAppDecTop::xInitDecLib()
641{
642  // initialize decoder class
643#if SVC_EXTENSION
644#if CONFORMANCE_BITSTREAM_MODE
645  for(UInt layer = 0; layer < MAX_VPS_LAYER_IDX_PLUS1; layer++)
646#else
647  for(UInt layer = 0; layer <= m_tgtLayerId; layer++)
648#endif
649  {
650    TDecTop& m_cTDecTop = *m_apcTDecTop[layer];
651#endif
652
653  m_cTDecTop.init();
654  m_cTDecTop.setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
655#if O0043_BEST_EFFORT_DECODING
656  m_cTDecTop.setForceDecodeBitDepth(m_forceDecodeBitDepth);
657#endif
658
659  if (!m_outputDecodedSEIMessagesFilename.empty())
660  {
661    std::ostream &os=m_seiMessageFileStream.is_open() ? m_seiMessageFileStream : std::cout;
662    m_cTDecTop.setDecodedSEIMessageOutputStream(&os);
663  }
664
665#if SVC_EXTENSION 
666#if CONFORMANCE_BITSTREAM_MODE
667    m_cTDecTop.setNumLayer( MAX_LAYERS );
668#else
669    m_cTDecTop.setNumLayer( m_tgtLayerId + 1 );
670#endif
671    m_cTDecTop.setCommonDecoderParams( &m_commonDecoderParams );
672    m_cTDecTop.setConfModeFlag( m_confModeFlag );
673  }
674#endif
675
676#if Q0074_COLOUR_REMAPPING_SEI
677  if (m_pcSeiColourRemappingInfoPrevious != NULL)
678  {
679    delete m_pcSeiColourRemappingInfoPrevious;
680    m_pcSeiColourRemappingInfoPrevious = NULL;
681  }
682#endif
683}
684
685/** \param pcListPic list of pictures to be written to file
686    \param tId       temporal sub-layer ID
687 */
688#if SVC_EXTENSION
689Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, UInt layerId, UInt tId )
690#else
691Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, UInt tId )
692#endif
693{
694  if (pcListPic->empty())
695  {
696    return;
697  }
698
699  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
700  Int numPicsNotYetDisplayed = 0;
701  Int dpbFullness = 0;
702  const TComSPS* activeSPS = &(pcListPic->front()->getPicSym()->getSPS());
703  UInt numReorderPicsHighestTid;
704  UInt maxDecPicBufferingHighestTid;
705  UInt maxNrSublayers = activeSPS->getMaxTLayers();
706
707  if(m_iMaxTemporalLayer == -1 || m_iMaxTemporalLayer >= maxNrSublayers)
708  {
709    numReorderPicsHighestTid = activeSPS->getNumReorderPics(maxNrSublayers-1);
710    maxDecPicBufferingHighestTid =  activeSPS->getMaxDecPicBuffering(maxNrSublayers-1); 
711  }
712  else
713  {
714    numReorderPicsHighestTid = activeSPS->getNumReorderPics(m_iMaxTemporalLayer);
715    maxDecPicBufferingHighestTid = activeSPS->getMaxDecPicBuffering(m_iMaxTemporalLayer); 
716  }
717
718  while (iterPic != pcListPic->end())
719  {
720    TComPic* pcPic = *(iterPic);
721#if SVC_EXTENSION
722    if(pcPic->getOutputMark() && pcPic->getPOC() > m_aiPOCLastDisplay[layerId])
723#else
724    if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay)
725#endif
726    {
727       numPicsNotYetDisplayed++;
728      dpbFullness++;
729    }
730    else if(pcPic->getSlice( 0 )->isReferenced())
731    {
732      dpbFullness++;
733    }
734    iterPic++;
735  }
736
737  iterPic = pcListPic->begin();
738
739  if (numPicsNotYetDisplayed>2)
740  {
741    iterPic++;
742  }
743
744  TComPic* pcPic = *(iterPic);
745  if (numPicsNotYetDisplayed>2 && pcPic->isField()) //Field Decoding
746  {
747    TComList<TComPic*>::iterator endPic   = pcListPic->end();
748    endPic--;
749    iterPic   = pcListPic->begin();
750    while (iterPic != endPic)
751    {
752      TComPic* pcPicTop = *(iterPic);
753      iterPic++;
754      TComPic* pcPicBottom = *(iterPic);
755
756#if SVC_EXTENSION
757      if( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() &&
758        (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid) &&
759        (!(pcPicTop->getPOC()%2) && pcPicBottom->getPOC() == pcPicTop->getPOC()+1) &&       
760        (pcPicTop->getPOC() == m_aiPOCLastDisplay[layerId]+1 || m_aiPOCLastDisplay[layerId]<0) )
761#else
762      if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() &&
763          (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid) &&
764          (!(pcPicTop->getPOC()%2) && pcPicBottom->getPOC() == pcPicTop->getPOC()+1) &&
765          (pcPicTop->getPOC() == m_iPOCLastDisplay+1 || m_iPOCLastDisplay < 0))
766#endif
767      {
768        // write to file
769        numPicsNotYetDisplayed = numPicsNotYetDisplayed-2;
770#if SVC_EXTENSION
771        if ( !m_reconFileName[layerId].empty() )
772        {
773          const Window &conf = pcPicTop->getConformanceWindow();
774          const Window  defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
775          const Bool isTff = pcPicTop->isTopField();
776
777          Bool display = true;
778          if( m_decodedNoDisplaySEIEnabled )
779          {
780            SEIMessages noDisplay = getSeisByType(pcPic->getSEIs(), SEI::NO_DISPLAY );
781            const SEINoDisplay *nd = ( noDisplay.size() > 0 ) ? (SEINoDisplay*) *(noDisplay.begin()) : NULL;
782            if( (nd != NULL) && nd->m_noDisplay )
783            {
784              display = false;
785            }
786          }
787
788          if (display)
789          {           
790            UInt chromaFormatIdc = pcPic->getSlice(0)->getChromaFormatIdc();
791            Int xScal =  TComSPS::getWinUnitX( chromaFormatIdc ), yScal = TComSPS::getWinUnitY( chromaFormatIdc );
792
793            m_apcTVideoIOYuvReconFile[layerId]->write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
794              m_outputColourSpaceConvert,
795              conf.getWindowLeftOffset()  * xScal + defDisp.getWindowLeftOffset(),
796              conf.getWindowRightOffset() * xScal + defDisp.getWindowRightOffset(),
797              conf.getWindowTopOffset()   * yScal + defDisp.getWindowTopOffset(),
798              conf.getWindowBottomOffset()* yScal + defDisp.getWindowBottomOffset(), NUM_CHROMA_FORMAT, isTff );
799          }
800        }
801
802        // update POC of display order
803        m_aiPOCLastDisplay[layerId] = pcPicBottom->getPOC();
804#else
805        if ( !m_reconFileName.empty() )
806        {
807          const Window &conf = pcPicTop->getConformanceWindow();
808          const Window  defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
809          const Bool isTff = pcPicTop->isTopField();
810
811          Bool display = true;
812          if( m_decodedNoDisplaySEIEnabled )
813          {
814            SEIMessages noDisplay = getSeisByType(pcPic->getSEIs(), SEI::NO_DISPLAY );
815            const SEINoDisplay *nd = ( noDisplay.size() > 0 ) ? (SEINoDisplay*) *(noDisplay.begin()) : NULL;
816            if( (nd != NULL) && nd->m_noDisplay )
817            {
818              display = false;
819            }
820          }
821
822          if (display)
823          {
824            m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
825                                           m_outputColourSpaceConvert,
826                                           conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
827                                           conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
828                                           conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
829                                           conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), NUM_CHROMA_FORMAT, isTff );
830          }
831        }
832
833        // update POC of display order
834        m_iPOCLastDisplay = pcPicBottom->getPOC();
835#endif
836
837        // erase non-referenced picture in the reference picture list after display
838        if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
839        {
840          pcPicTop->setReconMark(false);
841
842          // mark it should be extended later
843          pcPicTop->getPicYuvRec()->setBorderExtension( false );
844        }
845        if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
846        {
847          pcPicBottom->setReconMark(false);
848
849          // mark it should be extended later
850          pcPicBottom->getPicYuvRec()->setBorderExtension( false );
851        }
852        pcPicTop->setOutputMark(false);
853        pcPicBottom->setOutputMark(false);
854      }
855    }
856  }
857  else if (!pcPic->isField()) //Frame Decoding
858  {
859    iterPic = pcListPic->begin();
860
861    while (iterPic != pcListPic->end())
862    {
863      pcPic = *(iterPic);
864
865#if SVC_EXTENSION
866      if( pcPic->getOutputMark() && pcPic->getPOC() > m_aiPOCLastDisplay[layerId] &&
867        (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid) )
868#else
869      if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay &&
870        (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid))
871#endif
872      {
873        // write to file
874         numPicsNotYetDisplayed--;
875        if(pcPic->getSlice(0)->isReferenced() == false)
876        {
877          dpbFullness--;
878        }
879#if SVC_EXTENSION
880        if ( !m_reconFileName[layerId].empty() )
881        {
882          const Window &conf = pcPic->getConformanceWindow();
883          const Window  defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();         
884
885          UInt chromaFormatIdc = pcPic->getSlice(0)->getChromaFormatIdc();
886          Int xScal =  TComSPS::getWinUnitX( chromaFormatIdc ), yScal = TComSPS::getWinUnitY( chromaFormatIdc );
887
888          m_apcTVideoIOYuvReconFile[layerId]->write( pcPic->getPicYuvRec(), m_outputColourSpaceConvert,
889            conf.getWindowLeftOffset()  * xScal + defDisp.getWindowLeftOffset(),
890            conf.getWindowRightOffset() * xScal + defDisp.getWindowRightOffset(),
891            conf.getWindowTopOffset()   * yScal + defDisp.getWindowTopOffset(),
892            conf.getWindowBottomOffset()* yScal + defDisp.getWindowBottomOffset(),
893            NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range  );
894        }
895
896        // update POC of display order
897        m_aiPOCLastDisplay[layerId] = pcPic->getPOC();
898#else
899        if ( !m_reconFileName.empty() )
900        {
901          const Window &conf    = pcPic->getConformanceWindow();
902          const Window  defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
903
904          m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
905                                         m_outputColourSpaceConvert,
906                                         conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
907                                         conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
908                                         conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
909                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(),
910                                         NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range  );
911        }
912
913#if Q0074_COLOUR_REMAPPING_SEI
914        if (!m_colourRemapSEIFileName.empty())
915        {
916          xOutputColourRemapPic(pcPic);
917        }
918#endif
919
920        // update POC of display order
921        m_iPOCLastDisplay = pcPic->getPOC();
922#endif
923
924        // erase non-referenced picture in the reference picture list after display
925        if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
926        {
927          pcPic->setReconMark(false);
928
929          // mark it should be extended later
930          pcPic->getPicYuvRec()->setBorderExtension( false );
931        }
932        pcPic->setOutputMark(false);
933      }
934
935      iterPic++;
936    }
937  }
938}
939
940/** \param pcListPic list of pictures to be written to file
941 */
942#if SVC_EXTENSION
943Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic, UInt layerId )
944#else
945Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic )
946#endif
947{
948  if(!pcListPic || pcListPic->empty())
949  {
950    return;
951  }
952  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
953
954  iterPic   = pcListPic->begin();
955  TComPic* pcPic = *(iterPic);
956
957  if (pcPic->isField()) //Field Decoding
958  {
959    TComList<TComPic*>::iterator endPic   = pcListPic->end();
960    endPic--;
961    TComPic *pcPicTop, *pcPicBottom = NULL;
962    while (iterPic != endPic)
963    {
964      pcPicTop = *(iterPic);
965      iterPic++;
966      pcPicBottom = *(iterPic);
967
968      if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() && !(pcPicTop->getPOC()%2) && (pcPicBottom->getPOC() == pcPicTop->getPOC()+1) )
969      {
970        // write to file
971#if SVC_EXTENSION
972        if ( !m_reconFileName[layerId].empty() )
973        {
974          const Window &conf = pcPicTop->getConformanceWindow();
975          const Window  defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
976          const Bool isTff = pcPicTop->isTopField();         
977
978          UInt chromaFormatIdc = pcPic->getSlice(0)->getChromaFormatIdc();
979          Int xScal =  TComSPS::getWinUnitX( chromaFormatIdc ), yScal = TComSPS::getWinUnitY( chromaFormatIdc );
980
981          m_apcTVideoIOYuvReconFile[layerId]->write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(), m_outputColourSpaceConvert,
982            conf.getWindowLeftOffset()  *xScal + defDisp.getWindowLeftOffset(),
983            conf.getWindowRightOffset() *xScal + defDisp.getWindowRightOffset(),
984            conf.getWindowTopOffset()   *yScal + defDisp.getWindowTopOffset(),
985            conf.getWindowBottomOffset()*yScal + defDisp.getWindowBottomOffset(), NUM_CHROMA_FORMAT, isTff );
986        }
987
988        // update POC of display order
989        m_aiPOCLastDisplay[layerId] = pcPicBottom->getPOC();
990#else
991        if ( !m_reconFileName.empty() )
992        {
993          const Window &conf = pcPicTop->getConformanceWindow();
994          const Window  defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
995          const Bool isTff = pcPicTop->isTopField();
996          m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
997                                         m_outputColourSpaceConvert,
998                                         conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
999                                         conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
1000                                         conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
1001                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), NUM_CHROMA_FORMAT, isTff );
1002        }
1003
1004        // update POC of display order
1005        m_iPOCLastDisplay = pcPicBottom->getPOC();
1006#endif
1007
1008        // erase non-referenced picture in the reference picture list after display
1009        if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
1010        {
1011          pcPicTop->setReconMark(false);
1012
1013          // mark it should be extended later
1014          pcPicTop->getPicYuvRec()->setBorderExtension( false );
1015        }
1016        if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
1017        {
1018          pcPicBottom->setReconMark(false);
1019
1020          // mark it should be extended later
1021          pcPicBottom->getPicYuvRec()->setBorderExtension( false );
1022        }
1023        pcPicTop->setOutputMark(false);
1024        pcPicBottom->setOutputMark(false);
1025
1026        if(pcPicTop)
1027        {
1028          pcPicTop->destroy();
1029          delete pcPicTop;
1030          pcPicTop = NULL;
1031        }
1032      }
1033    }
1034    if(pcPicBottom)
1035    {
1036      pcPicBottom->destroy();
1037      delete pcPicBottom;
1038      pcPicBottom = NULL;
1039    }
1040  }
1041  else //Frame decoding
1042  {
1043    while (iterPic != pcListPic->end())
1044    {
1045      pcPic = *(iterPic);
1046
1047      if ( pcPic->getOutputMark() )
1048      {
1049        // write to file
1050#if SVC_EXTENSION
1051        if ( !m_reconFileName[layerId].empty() )
1052        {
1053          const Window &conf = pcPic->getConformanceWindow();
1054          const Window  defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();         
1055
1056          UInt chromaFormatIdc = pcPic->getSlice(0)->getChromaFormatIdc();
1057          Int xScal =  TComSPS::getWinUnitX( chromaFormatIdc ), yScal = TComSPS::getWinUnitY( chromaFormatIdc );
1058
1059          m_apcTVideoIOYuvReconFile[layerId]->write( pcPic->getPicYuvRec(),
1060                                                   m_outputColourSpaceConvert,
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                                                   NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range );
1066        }
1067
1068        // update POC of display order
1069        m_aiPOCLastDisplay[layerId] = pcPic->getPOC();
1070#else
1071        if ( !m_reconFileName.empty() )
1072        {
1073          const Window &conf    = pcPic->getConformanceWindow();
1074          const Window  defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
1075
1076          m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
1077                                         m_outputColourSpaceConvert,
1078                                         conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
1079                                         conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
1080                                         conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
1081                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(),
1082                                         NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range );
1083        }
1084
1085#if Q0074_COLOUR_REMAPPING_SEI
1086        if (!m_colourRemapSEIFileName.empty())
1087        {
1088          xOutputColourRemapPic(pcPic);
1089        }
1090#endif
1091
1092        // update POC of display order
1093        m_iPOCLastDisplay = pcPic->getPOC();
1094#endif
1095
1096        // erase non-referenced picture in the reference picture list after display
1097        if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
1098        {
1099          pcPic->setReconMark(false);
1100
1101          // mark it should be extended later
1102          pcPic->getPicYuvRec()->setBorderExtension( false );
1103        }
1104        pcPic->setOutputMark(false);
1105      }
1106#if !SVC_EXTENSION
1107      if(pcPic != NULL)
1108      {
1109        pcPic->destroy();
1110        delete pcPic;
1111        pcPic = NULL;
1112      }
1113#endif
1114      iterPic++;
1115    }
1116  }
1117#if SVC_EXTENSION
1118  m_aiPOCLastDisplay[layerId] = -MAX_INT;
1119#else
1120  pcListPic->clear();
1121  m_iPOCLastDisplay = -MAX_INT;
1122#endif
1123}
1124
1125/** \param nalu Input nalu to check whether its LayerId is within targetDecLayerIdSet
1126 */
1127Bool TAppDecTop::isNaluWithinTargetDecLayerIdSet( InputNALUnit* nalu )
1128{
1129  if ( m_targetDecLayerIdSet.size() == 0 ) // By default, the set is empty, meaning all LayerIds are allowed
1130  {
1131    return true;
1132  }
1133#if SVC_EXTENSION
1134  if (nalu->m_nuhLayerId == 0 && (nalu->m_nalUnitType == NAL_UNIT_VPS || nalu->m_nalUnitType == NAL_UNIT_SPS || nalu->m_nalUnitType == NAL_UNIT_PPS || nalu->m_nalUnitType == NAL_UNIT_EOS))
1135  {
1136    return true;
1137  }
1138#endif
1139  for (std::vector<Int>::iterator it = m_targetDecLayerIdSet.begin(); it != m_targetDecLayerIdSet.end(); it++)
1140  {
1141    if ( nalu->m_nuhLayerId == (*it) )
1142    {
1143      return true;
1144    }
1145  }
1146  return false;
1147}
1148
1149#if Q0074_COLOUR_REMAPPING_SEI
1150
1151Void TAppDecTop::xOutputColourRemapPic(TComPic* pcPic)
1152{
1153  const TComSPS &sps=pcPic->getPicSym()->getSPS();
1154  SEIMessages colourRemappingInfo = getSeisByType(pcPic->getSEIs(), SEI::COLOUR_REMAPPING_INFO );
1155  SEIColourRemappingInfo *seiColourRemappingInfo = ( colourRemappingInfo.size() > 0 ) ? (SEIColourRemappingInfo*) *(colourRemappingInfo.begin()) : NULL;
1156
1157  if (colourRemappingInfo.size() > 1)
1158  {
1159    printf ("Warning: Got multiple Colour Remapping Information SEI messages. Using first.");
1160  }
1161  if (seiColourRemappingInfo)
1162  {
1163    applyColourRemapping(*pcPic->getPicYuvRec(), *seiColourRemappingInfo, sps);
1164
1165    // save the last CRI SEI received
1166    if (m_pcSeiColourRemappingInfoPrevious == NULL)
1167    {
1168      m_pcSeiColourRemappingInfoPrevious = new SEIColourRemappingInfo();
1169    }
1170    m_pcSeiColourRemappingInfoPrevious->copyFrom(*seiColourRemappingInfo);
1171  }
1172  else  // using the last CRI SEI received
1173  {
1174    // TODO: prevent persistence of CRI SEI across C(L)VS.
1175    if (m_pcSeiColourRemappingInfoPrevious != NULL)
1176    {
1177      if (m_pcSeiColourRemappingInfoPrevious->m_colourRemapPersistenceFlag == false)
1178      {
1179        printf("Warning No SEI-CRI message is present for the current picture, persistence of the CRI is not managed\n");
1180      }
1181      applyColourRemapping(*pcPic->getPicYuvRec(), *m_pcSeiColourRemappingInfoPrevious, sps);
1182    }
1183  }
1184}
1185
1186// compute lut from SEI
1187// use at lutPoints points aligned on a power of 2 value
1188// SEI Lut must be in ascending values of coded Values
1189static std::vector<Int>
1190initColourRemappingInfoLut(const Int                                          bitDepth_in,     // bit-depth of the input values of the LUT
1191                           const Int                                          nbDecimalValues, // Position of the fixed point
1192                           const std::vector<SEIColourRemappingInfo::CRIlut> &lut,
1193                           const Int                                          maxValue, // maximum output value
1194                           const Int                                          lutOffset)
1195{
1196  const Int lutPoints = (1 << bitDepth_in) + 1 ;
1197  std::vector<Int> retLut(lutPoints);
1198
1199  // missing values: need to define default values before first definition (check codedValue[0] == 0)
1200  Int iTargetPrev = (lut.size() && lut[0].codedValue == 0) ? lut[0].targetValue: 0;
1201  Int startPivot = (lut.size())? ((lut[0].codedValue == 0)? 1: 0): 1;
1202  Int iCodedPrev  = 0;
1203  // set max value with the coded bit-depth
1204  // + ((1 << nbDecimalValues) - 1) is for the added bits
1205  const Int maxValueFixedPoint = (maxValue << nbDecimalValues) + ((1 << nbDecimalValues) - 1);
1206
1207  Int iValue = 0;
1208
1209  for ( Int iPivot=startPivot ; iPivot < (Int)lut.size(); iPivot++ )
1210  {
1211    Int iCodedNext  = lut[iPivot].codedValue;
1212    Int iTargetNext = lut[iPivot].targetValue;
1213
1214    // ensure correct bit depth and avoid overflow in lut address
1215    Int iCodedNext_bitDepth = std::min(iCodedNext, (1 << bitDepth_in));
1216
1217    const Int divValue =  (iCodedNext - iCodedPrev > 0)? (iCodedNext - iCodedPrev): 1;
1218    const Int lutValInit = (lutOffset + iTargetPrev) << nbDecimalValues;
1219    const Int roundValue = divValue / 2;
1220    for ( ; iValue<iCodedNext_bitDepth; iValue++ )
1221    {
1222      Int value = iValue;
1223      Int interpol = ((((value-iCodedPrev) * (iTargetNext - iTargetPrev)) << nbDecimalValues) + roundValue) / divValue;               
1224      retLut[iValue]  = std::min(lutValInit + interpol , maxValueFixedPoint);
1225    }
1226    iCodedPrev  = iCodedNext;
1227    iTargetPrev = iTargetNext;
1228  }
1229  // fill missing values if necessary
1230  if(iCodedPrev < (1 << bitDepth_in)+1)
1231  {
1232    Int iCodedNext  = (1 << bitDepth_in);
1233    Int iTargetNext = (1 << bitDepth_in) - 1;
1234
1235    const Int divValue =  (iCodedNext - iCodedPrev > 0)? (iCodedNext - iCodedPrev): 1;
1236    const Int lutValInit = (lutOffset + iTargetPrev) << nbDecimalValues;
1237    const Int roundValue = divValue / 2;
1238
1239    for ( ; iValue<=iCodedNext; iValue++ )
1240    {
1241      Int value = iValue;
1242      Int interpol = ((((value-iCodedPrev) * (iTargetNext - iTargetPrev)) << nbDecimalValues) + roundValue) / divValue; 
1243      retLut[iValue]  = std::min(lutValInit + interpol , maxValueFixedPoint);
1244    }
1245  }
1246  return retLut;
1247}
1248
1249static Void
1250initColourRemappingInfoLuts(std::vector<Int>      (&preLut)[3],
1251                            std::vector<Int>      (&postLut)[3],
1252                            SEIColourRemappingInfo &pCriSEI,
1253                            const Int               maxBitDepth)
1254{
1255  Int internalBitDepth = pCriSEI.m_colourRemapBitDepth;
1256  for ( Int c=0 ; c<3 ; c++ )
1257  {
1258    std::sort(pCriSEI.m_preLut[c].begin(), pCriSEI.m_preLut[c].end()); // ensure preLut is ordered in ascending values of codedValues   
1259    preLut[c] = initColourRemappingInfoLut(pCriSEI.m_colourRemapInputBitDepth, maxBitDepth - pCriSEI.m_colourRemapInputBitDepth, pCriSEI.m_preLut[c], ((1 << internalBitDepth) - 1), 0); //Fill preLut
1260
1261    std::sort(pCriSEI.m_postLut[c].begin(), pCriSEI.m_postLut[c].end()); // ensure postLut is ordered in ascending values of codedValues       
1262    postLut[c] = initColourRemappingInfoLut(pCriSEI.m_colourRemapBitDepth, maxBitDepth - pCriSEI.m_colourRemapBitDepth, pCriSEI.m_postLut[c], (1 << internalBitDepth) - 1, 0); //Fill postLut
1263  }
1264}
1265
1266// apply lut.
1267// Input lut values are aligned on power of 2 boundaries
1268static Int
1269applyColourRemappingInfoLut1D(Int inVal, const std::vector<Int> &lut, const Int inValPrecisionBits)
1270{
1271  const Int roundValue = (inValPrecisionBits)? 1 << (inValPrecisionBits - 1): 0;
1272  inVal = std::min(std::max(0, inVal), (Int)(((lut.size()-1) << inValPrecisionBits)));
1273  Int index  = (Int) std::min((inVal >> inValPrecisionBits), (Int)(lut.size()-2));
1274  Int outVal = (( inVal - (index<<inValPrecisionBits) ) * (lut[index+1] - lut[index]) + roundValue) >> inValPrecisionBits;
1275  outVal +=  lut[index] ;
1276
1277  return outVal;
1278} 
1279
1280static Int
1281applyColourRemappingInfoMatrix(const Int (&colourRemapCoeffs)[3], const Int postOffsetShift, const Int p0, const Int p1, const Int p2, const Int offset)
1282{
1283  Int YUVMat = (colourRemapCoeffs[0]* p0 + colourRemapCoeffs[1]* p1 + colourRemapCoeffs[2]* p2  + offset) >> postOffsetShift;
1284  return YUVMat;
1285}
1286
1287static Void
1288setColourRemappingInfoMatrixOffset(Int (&matrixOffset)[3], Int offset0, Int offset1, Int offset2)
1289{
1290  matrixOffset[0] = offset0;
1291  matrixOffset[1] = offset1;
1292  matrixOffset[2] = offset2;
1293}
1294
1295static Void
1296setColourRemappingInfoMatrixOffsets(      Int  (&matrixInputOffset)[3],
1297                                          Int  (&matrixOutputOffset)[3],
1298                                    const Int  bitDepth,
1299                                    const Bool crInputFullRangeFlag,
1300                                    const Int  crInputMatrixCoefficients,
1301                                    const Bool crFullRangeFlag,
1302                                    const Int  crMatrixCoefficients)
1303{
1304  // set static matrix offsets
1305  Int crInputOffsetLuma = (crInputFullRangeFlag)? 0:-(16 << (bitDepth-8));
1306  Int crOffsetLuma = (crFullRangeFlag)? 0:(16 << (bitDepth-8));
1307  Int crInputOffsetChroma = 0;
1308  Int crOffsetChroma = 0;
1309
1310  switch(crInputMatrixCoefficients)
1311  {
1312    case MATRIX_COEFFICIENTS_RGB:
1313      crInputOffsetChroma = 0;
1314      if(!crInputFullRangeFlag)
1315      {
1316        fprintf(stderr, "WARNING: crInputMatrixCoefficients set to MATRIX_COEFFICIENTS_RGB and crInputFullRangeFlag not set\n");
1317        crInputOffsetLuma = 0;
1318      }
1319      break;
1320    case MATRIX_COEFFICIENTS_UNSPECIFIED:
1321    case MATRIX_COEFFICIENTS_BT709:
1322    case MATRIX_COEFFICIENTS_BT2020_NON_CONSTANT_LUMINANCE:
1323      crInputOffsetChroma = -(1 << (bitDepth-1));
1324      break;
1325    default:
1326      fprintf(stderr, "WARNING: crInputMatrixCoefficients set to undefined value: %d\n", crInputMatrixCoefficients);
1327  }
1328
1329  switch(crMatrixCoefficients)
1330  {
1331    case MATRIX_COEFFICIENTS_RGB:
1332      crOffsetChroma = 0;
1333      if(!crFullRangeFlag)
1334      {
1335        fprintf(stderr, "WARNING: crMatrixCoefficients set to MATRIX_COEFFICIENTS_RGB and crInputFullRangeFlag not set\n");
1336        crOffsetLuma = 0;
1337      }
1338      break;
1339    case MATRIX_COEFFICIENTS_UNSPECIFIED:
1340    case MATRIX_COEFFICIENTS_BT709:
1341    case MATRIX_COEFFICIENTS_BT2020_NON_CONSTANT_LUMINANCE:
1342      crOffsetChroma = (1 << (bitDepth-1));
1343      break;
1344    default:
1345      fprintf(stderr, "WARNING: crMatrixCoefficients set to undefined value: %d\n", crMatrixCoefficients);
1346  }
1347
1348  setColourRemappingInfoMatrixOffset(matrixInputOffset, crInputOffsetLuma, crInputOffsetChroma, crInputOffsetChroma);
1349  setColourRemappingInfoMatrixOffset(matrixOutputOffset, crOffsetLuma, crOffsetChroma, crOffsetChroma);
1350}
1351
1352Void TAppDecTop::applyColourRemapping(const TComPicYuv& pic, SEIColourRemappingInfo& criSEI, const TComSPS &activeSPS)
1353{ 
1354  const Int maxBitDepth = 16;
1355
1356  // create colour remapped picture
1357  if( !criSEI.m_colourRemapCancelFlag && pic.getChromaFormat()!=CHROMA_400) // 4:0:0 not supported.
1358  {
1359    const Int          iHeight         = pic.getHeight(COMPONENT_Y);
1360    const Int          iWidth          = pic.getWidth(COMPONENT_Y);
1361    const ChromaFormat chromaFormatIDC = pic.getChromaFormat();
1362
1363    TComPicYuv picYuvColourRemapped;
1364    picYuvColourRemapped.createWithoutCUInfo( iWidth, iHeight, chromaFormatIDC );
1365
1366    const Int  iStrideIn   = pic.getStride(COMPONENT_Y);
1367    const Int  iCStrideIn  = pic.getStride(COMPONENT_Cb);
1368    const Int  iStrideOut  = picYuvColourRemapped.getStride(COMPONENT_Y);
1369    const Int  iCStrideOut = picYuvColourRemapped.getStride(COMPONENT_Cb);
1370    const Bool b444        = ( pic.getChromaFormat() == CHROMA_444 );
1371    const Bool b422        = ( pic.getChromaFormat() == CHROMA_422 );
1372    const Bool b420        = ( pic.getChromaFormat() == CHROMA_420 );
1373
1374    std::vector<Int> preLut[3];
1375    std::vector<Int> postLut[3];
1376    Int matrixInputOffset[3];
1377    Int matrixOutputOffset[3];
1378    const Pel *YUVIn[MAX_NUM_COMPONENT];
1379    Pel *YUVOut[MAX_NUM_COMPONENT];
1380    YUVIn[COMPONENT_Y]  = pic.getAddr(COMPONENT_Y);
1381    YUVIn[COMPONENT_Cb] = pic.getAddr(COMPONENT_Cb);
1382    YUVIn[COMPONENT_Cr] = pic.getAddr(COMPONENT_Cr);
1383    YUVOut[COMPONENT_Y]  = picYuvColourRemapped.getAddr(COMPONENT_Y);
1384    YUVOut[COMPONENT_Cb] = picYuvColourRemapped.getAddr(COMPONENT_Cb);
1385    YUVOut[COMPONENT_Cr] = picYuvColourRemapped.getAddr(COMPONENT_Cr);
1386
1387    const Int bitDepth = criSEI.m_colourRemapBitDepth;
1388    BitDepths        bitDepthsCriFile;
1389    bitDepthsCriFile.recon[CHANNEL_TYPE_LUMA]   = bitDepth;
1390    bitDepthsCriFile.recon[CHANNEL_TYPE_CHROMA] = bitDepth; // Different bitdepth is not implemented
1391
1392    const Int postOffsetShift = criSEI.m_log2MatrixDenom;
1393    const Int matrixRound = 1 << (postOffsetShift - 1);
1394    const Int postLutInputPrecision = (maxBitDepth - criSEI.m_colourRemapBitDepth);
1395
1396    if ( ! criSEI.m_colourRemapVideoSignalInfoPresentFlag ) // setting default
1397    {
1398      setColourRemappingInfoMatrixOffsets(matrixInputOffset, matrixOutputOffset, maxBitDepth,
1399          activeSPS.getVuiParameters()->getVideoFullRangeFlag(), activeSPS.getVuiParameters()->getMatrixCoefficients(),
1400          activeSPS.getVuiParameters()->getVideoFullRangeFlag(), activeSPS.getVuiParameters()->getMatrixCoefficients());
1401    }
1402    else
1403    {
1404      setColourRemappingInfoMatrixOffsets(matrixInputOffset, matrixOutputOffset, maxBitDepth,
1405          activeSPS.getVuiParameters()->getVideoFullRangeFlag(), activeSPS.getVuiParameters()->getMatrixCoefficients(),
1406          criSEI.m_colourRemapFullRangeFlag, criSEI.m_colourRemapMatrixCoefficients);
1407    }
1408
1409    // add matrix rounding to output matrix offsets
1410    matrixOutputOffset[0] = (matrixOutputOffset[0] << postOffsetShift) + matrixRound;
1411    matrixOutputOffset[1] = (matrixOutputOffset[1] << postOffsetShift) + matrixRound;
1412    matrixOutputOffset[2] = (matrixOutputOffset[2] << postOffsetShift) + matrixRound;
1413
1414    // Merge   matrixInputOffset and matrixOutputOffset to matrixOutputOffset
1415    matrixOutputOffset[0] += applyColourRemappingInfoMatrix(criSEI.m_colourRemapCoeffs[0], 0, matrixInputOffset[0], matrixInputOffset[1], matrixInputOffset[2], 0);
1416    matrixOutputOffset[1] += applyColourRemappingInfoMatrix(criSEI.m_colourRemapCoeffs[1], 0, matrixInputOffset[0], matrixInputOffset[1], matrixInputOffset[2], 0);
1417    matrixOutputOffset[2] += applyColourRemappingInfoMatrix(criSEI.m_colourRemapCoeffs[2], 0, matrixInputOffset[0], matrixInputOffset[1], matrixInputOffset[2], 0);
1418
1419    // rescaling output: include CRI/output frame difference
1420    const Int scaleShiftOut_neg = abs(bitDepth - maxBitDepth);
1421    const Int scaleOut_round = 1 << (scaleShiftOut_neg-1);
1422
1423    initColourRemappingInfoLuts(preLut, postLut, criSEI, maxBitDepth);
1424
1425    assert(pic.getChromaFormat() != CHROMA_400);
1426    const Int hs = pic.getComponentScaleX(ComponentID(COMPONENT_Cb));
1427
1428    for( Int y = 0; y < iHeight; y++ )
1429    {
1430      for( Int x = 0; x < iWidth; x++ )
1431      {
1432        const Int xc = (x>>hs);
1433        Bool computeChroma = b444 || ((b422 || !(y&1)) && !(x&1));
1434
1435        Int YUVPre_0 = applyColourRemappingInfoLut1D(YUVIn[COMPONENT_Y][x], preLut[0], 0);
1436        Int YUVPre_1 = applyColourRemappingInfoLut1D(YUVIn[COMPONENT_Cb][xc], preLut[1], 0);
1437        Int YUVPre_2 = applyColourRemappingInfoLut1D(YUVIn[COMPONENT_Cr][xc], preLut[2], 0);
1438
1439        Int YUVMat_0 = applyColourRemappingInfoMatrix(criSEI.m_colourRemapCoeffs[0], postOffsetShift, YUVPre_0, YUVPre_1, YUVPre_2, matrixOutputOffset[0]);
1440        Int YUVLutB_0 = applyColourRemappingInfoLut1D(YUVMat_0, postLut[0], postLutInputPrecision);
1441        YUVOut[COMPONENT_Y][x] = (YUVLutB_0 + scaleOut_round) >> scaleShiftOut_neg; // scaling output
1442
1443        if( computeChroma )
1444        {
1445          Int YUVMat_1 = applyColourRemappingInfoMatrix(criSEI.m_colourRemapCoeffs[1], postOffsetShift, YUVPre_0, YUVPre_1, YUVPre_2, matrixOutputOffset[1]);
1446          Int YUVLutB_1 = applyColourRemappingInfoLut1D(YUVMat_1, postLut[1], postLutInputPrecision);
1447          YUVOut[COMPONENT_Cb][xc] = (YUVLutB_1 + scaleOut_round) >> scaleShiftOut_neg; // scaling output
1448
1449          Int YUVMat_2 = applyColourRemappingInfoMatrix(criSEI.m_colourRemapCoeffs[2], postOffsetShift, YUVPre_0, YUVPre_1, YUVPre_2, matrixOutputOffset[2]);
1450          Int YUVLutB_2 = applyColourRemappingInfoLut1D(YUVMat_2, postLut[2], postLutInputPrecision);
1451          YUVOut[COMPONENT_Cr][xc] = (YUVLutB_2 + scaleOut_round) >> scaleShiftOut_neg; // scaling output
1452        }
1453      }
1454
1455      YUVIn[COMPONENT_Y]  += iStrideIn;
1456      YUVOut[COMPONENT_Y] += iStrideOut;
1457      if( !(b420 && !(y&1)) )
1458      {
1459         YUVIn[COMPONENT_Cb]  += iCStrideIn;
1460         YUVIn[COMPONENT_Cr]  += iCStrideIn;
1461         YUVOut[COMPONENT_Cb] += iCStrideOut;
1462         YUVOut[COMPONENT_Cr] += iCStrideOut;
1463      }
1464    }
1465    //Write remapped picture in display order
1466    picYuvColourRemapped.dump( m_colourRemapSEIFileName, bitDepthsCriFile, true );
1467    picYuvColourRemapped.destroy();
1468  }
1469}
1470#endif
1471
1472#if ALIGNED_BUMPING
1473// Function outputs a picture, and marks it as not needed for output.
1474Void TAppDecTop::xOutputAndMarkPic( TComPic *pic, std::string& reconFileName, const Int layerId, Int &pocLastDisplay, DpbStatus &dpbStatus )
1475{
1476  if( !reconFileName.empty() )
1477  {
1478    const Window &conf = pic->getConformanceWindow();
1479    const Window &defDisp = m_respectDefDispWindow ? pic->getDefDisplayWindow() : Window();
1480    Int xScal =  1, yScal = 1;
1481
1482    UInt chromaFormatIdc = pic->getSlice(0)->getChromaFormatIdc();
1483    xScal = TComSPS::getWinUnitX( chromaFormatIdc );
1484    yScal = TComSPS::getWinUnitY( chromaFormatIdc );
1485
1486    TComPicYuv* pPicCYuvRec = pic->getPicYuvRec();
1487    m_apcTVideoIOYuvReconFile[layerId]->write( pPicCYuvRec, m_outputColourSpaceConvert,
1488      conf.getWindowLeftOffset()  * xScal + defDisp.getWindowLeftOffset(),
1489      conf.getWindowRightOffset() * xScal + defDisp.getWindowRightOffset(),
1490      conf.getWindowTopOffset()   * yScal + defDisp.getWindowTopOffset(),
1491      conf.getWindowBottomOffset()* yScal + defDisp.getWindowBottomOffset() );
1492
1493#if Q0074_COLOUR_REMAPPING_SEI
1494    if( !m_colourRemapSEIFileName.empty() )
1495    {
1496      xOutputColourRemapPic(pic);
1497    }
1498#endif
1499  }
1500  // update POC of display order
1501  pocLastDisplay = pic->getPOC();
1502
1503  // Mark as not needed for output
1504  pic->setOutputMark(false);
1505
1506  // "erase" non-referenced picture in the reference picture list after display
1507  if ( !pic->getSlice(0)->isReferenced() && pic->getReconMark() == true )
1508  {
1509    pic->setReconMark(false);
1510
1511    // mark it should be extended later
1512    pic->getPicYuvRec()->setBorderExtension( false );
1513
1514    dpbStatus.m_numPicsInSubDpb[dpbStatus.m_layerIdToSubDpbIdMap[layerId]]--;
1515  }
1516}
1517
1518Void TAppDecTop::flushAllPictures(Int layerId, Bool outputPictures)
1519{
1520  // First "empty" all pictures that are not used for reference and not needed for output
1521  emptyUnusedPicturesNotNeededForOutput();
1522
1523  if( outputPictures )  // All pictures in the DPB in that layer are to be output; this means other pictures would also be output
1524  {
1525    std::vector<Int>  listOfPocs;
1526    std::vector<Int>  listOfPocsInEachLayer[MAX_VPS_LAYER_IDX_PLUS1];
1527    std::vector<Int>  listOfPocsPositionInEachLayer[MAX_VPS_LAYER_IDX_PLUS1];
1528
1529    DpbStatus dpbStatus;
1530
1531    // Find the status of the DPB
1532    xFindDPBStatus(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus);
1533
1534    if( listOfPocs.size() )
1535    {
1536      while( listOfPocsInEachLayer[layerId].size() )    // As long as there picture in the layer to be output
1537      {
1538        bumpingProcess( listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus );
1539      }
1540    }
1541  }
1542
1543  // Now remove all pictures from the layer DPB?
1544  markAllPicturesAsErased(layerId);
1545}
1546
1547Void TAppDecTop::flushAllPictures(Bool outputPictures)
1548{
1549  // First "empty" all pictures that are not used for reference and not needed for output
1550  emptyUnusedPicturesNotNeededForOutput();
1551
1552  if( outputPictures )  // All pictures in the DPB are to be output
1553  {
1554    std::vector<Int>  listOfPocs;
1555    std::vector<Int>  listOfPocsInEachLayer[MAX_VPS_LAYER_IDX_PLUS1];
1556    std::vector<Int>  listOfPocsPositionInEachLayer[MAX_VPS_LAYER_IDX_PLUS1];
1557
1558    DpbStatus dpbStatus;
1559
1560    // Find the status of the DPB
1561    xFindDPBStatus(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus, false);
1562
1563    while( dpbStatus.m_numAUsNotDisplayed )
1564    {
1565      bumpingProcess( listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus );
1566    }
1567  }
1568
1569  // Now remove all pictures from the DPB?
1570  markAllPicturesAsErased();
1571}
1572
1573Void TAppDecTop::markAllPicturesAsErased()
1574{
1575  for(Int i = 0; i < MAX_VPS_LAYER_IDX_PLUS1; i++)
1576  {
1577    markAllPicturesAsErased(i);
1578  }
1579}
1580
1581Void TAppDecTop::markAllPicturesAsErased(Int layerIdx)
1582{
1583  TComList<TComPic*>::iterator  iterPic = m_apcTDecTop[layerIdx]->getListPic()->begin();
1584  Int iSize = Int( m_apcTDecTop[layerIdx]->getListPic()->size() );
1585 
1586  for (Int i = 0; i < iSize; i++ )
1587  {
1588    TComPic* pcPic = *(iterPic++);
1589
1590    if( pcPic )
1591    {
1592      pcPic->destroy();
1593
1594      // pcPic is statically created for the external (AVC) base layer, no need to delete it
1595      if( !m_apcTDecTop[layerIdx]->getParameterSetManager()->getActiveVPS()->getNonHEVCBaseLayerFlag() || layerIdx )
1596      {
1597        delete pcPic;
1598        pcPic = NULL;
1599      }
1600    }
1601  }
1602
1603  m_apcTDecTop[layerIdx]->getListPic()->clear();
1604}
1605
1606Void TAppDecTop::checkOutputBeforeDecoding(Int layerIdx)
1607{   
1608  std::vector<Int>  listOfPocs;
1609  std::vector<Int>  listOfPocsInEachLayer[MAX_VPS_LAYER_IDX_PLUS1];
1610  std::vector<Int>  listOfPocsPositionInEachLayer[MAX_VPS_LAYER_IDX_PLUS1];
1611
1612  DpbStatus dpbStatus;
1613
1614  // First "empty" all pictures that are not used for reference and not needed for output
1615  emptyUnusedPicturesNotNeededForOutput();
1616
1617  // Find the status of the DPB
1618  xFindDPBStatus(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus);
1619
1620  // If not picture to be output, return
1621  if( listOfPocs.size() == 0 )
1622  {
1623    return;
1624  }
1625
1626  // Find DPB-information from the VPS
1627  DpbStatus maxDpbLimit;
1628
1629  Int subDpbIdx = getCommonDecoderParams()->getTargetOutputLayerSetIdx() == 0 ? dpbStatus.m_layerIdToSubDpbIdMap[0] : dpbStatus.m_layerIdToSubDpbIdMap[layerIdx];
1630
1631  findDpbParametersFromVps(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, maxDpbLimit);
1632
1633  // Assume that listOfPocs is sorted in increasing order - if not have to sort it.
1634  while( ifInvokeBumpingBeforeDecoding(dpbStatus, maxDpbLimit, layerIdx, subDpbIdx) )
1635  {
1636    bumpingProcess( listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus );
1637  } 
1638}
1639
1640Void TAppDecTop::checkOutputAfterDecoding()
1641{   
1642  std::vector<Int>  listOfPocs;
1643  std::vector<Int>  listOfPocsInEachLayer[MAX_VPS_LAYER_IDX_PLUS1];
1644  std::vector<Int>  listOfPocsPositionInEachLayer[MAX_VPS_LAYER_IDX_PLUS1];
1645
1646  DpbStatus dpbStatus;
1647
1648  // First "empty" all pictures that are not used for reference and not needed for output
1649  emptyUnusedPicturesNotNeededForOutput();
1650
1651  // Find the status of the DPB
1652  xFindDPBStatus(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus);
1653
1654  // If not picture to be output, return
1655  if( listOfPocs.size() == 0 )
1656  {
1657    return;
1658  }
1659
1660  // Find DPB-information from the VPS
1661  DpbStatus maxDpbLimit;
1662  findDpbParametersFromVps(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, maxDpbLimit);
1663
1664  // Assume that listOfPocs is sorted in increasing order - if not have to sort it.
1665  while( ifInvokeBumpingAfterDecoding(dpbStatus, maxDpbLimit) )
1666  {
1667    bumpingProcess( listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus );
1668  } 
1669}
1670
1671Void TAppDecTop::bumpingProcess(std::vector<Int> &listOfPocs, std::vector<Int> *listOfPocsInEachLayer, std::vector<Int> *listOfPocsPositionInEachLayer, DpbStatus &dpbStatus)
1672{
1673  // Choose the smallest POC value
1674  Int pocValue = *(listOfPocs.begin());
1675  std::vector<int>::iterator it;
1676  TComList<TComPic*>::iterator iterPic;
1677
1678  for( Int dpbLayerCtr = 0; dpbLayerCtr < dpbStatus.m_numLayers; dpbLayerCtr++)
1679  {
1680    Int layerId  = dpbStatus.m_targetDecLayerIdList[dpbLayerCtr];
1681
1682    // Check if picture with pocValue is present.
1683    it = find( listOfPocsInEachLayer[layerId].begin(), listOfPocsInEachLayer[layerId].end(), pocValue );
1684    if( it != listOfPocsInEachLayer[layerId].end() )  // picture found.
1685    {
1686      Int picPosition = (Int)std::distance( listOfPocsInEachLayer[layerId].begin(), it );
1687      Int j;
1688      for(j = 0, iterPic = m_apcTDecTop[layerId]->getListPic()->begin(); j < listOfPocsPositionInEachLayer[layerId][picPosition]; j++) // Picture to be output
1689      {
1690        iterPic++;
1691      }
1692      TComPic *pic = *iterPic;
1693
1694      xOutputAndMarkPic( pic, m_reconFileName[layerId], layerId, m_aiPOCLastDisplay[layerId], dpbStatus );
1695
1696#if CONFORMANCE_BITSTREAM_MODE
1697      FILE *fptr;
1698      if( m_confModeFlag )
1699      {
1700        if( m_metadataFileRefresh )
1701        {
1702          fptr = fopen( this->getMetadataFileName().c_str(), "w" );
1703          fprintf(fptr, " LayerId      POC    MD5\n");
1704          fprintf(fptr, "------------------------\n");
1705        }
1706        else
1707        {
1708          fptr = fopen( this->getMetadataFileName().c_str(), "a+" );
1709        }
1710        this->setMetadataFileRefresh(false);
1711
1712        TComPictureHash recon_digest;
1713        Int numChar = calcMD5(*pic->getPicYuvRec(), recon_digest, pic->getSlice(0)->getBitDepths());
1714        fprintf(fptr, "%8d%9d    MD5:%s\n", pic->getLayerId(), pic->getSlice(0)->getPOC(), hashToString(recon_digest, numChar).c_str());
1715        fclose(fptr);
1716      }
1717#endif
1718
1719      listOfPocsInEachLayer[layerId].erase( it );
1720      listOfPocsPositionInEachLayer[layerId].erase( listOfPocsPositionInEachLayer[layerId].begin() + picPosition );
1721      dpbStatus.m_numPicsInSubDpb[dpbStatus.m_layerIdToSubDpbIdMap[layerId]]--;
1722    }
1723  }
1724
1725  dpbStatus.m_numAUsNotDisplayed--;
1726
1727#if CONFORMANCE_BITSTREAM_MODE
1728  if( m_confModeFlag )
1729  {
1730    for( Int dpbLayerCtr = 0; dpbLayerCtr < dpbStatus.m_numLayers; dpbLayerCtr++)
1731    {
1732      Int layerId = dpbStatus.m_targetDecLayerIdList[dpbLayerCtr];
1733      // Output all picutres "decoded" in that layer that have POC less than the current picture
1734      std::vector<TComPic> *layerBuffer = m_apcTDecTop[layerId]->getConfListPic();
1735      // Write all pictures to the file.
1736      if( this->getDecodedYuvLayerRefresh(layerId) )
1737      {
1738        char tempFileName[256];
1739        strcpy(tempFileName, this->getDecodedYuvLayerFileName( layerId ).c_str());
1740
1741        const TComVPS *vps = m_apcTDecTop[layerId]->getParameterSetManager()->getActiveVPS();
1742        const TComSPS *sps = m_apcTDecTop[layerId]->getParameterSetManager()->getActiveSPS();
1743        const BitDepths &bitDpeths = vps->getBitDepths(sps, layerId);
1744        Int bitDepth[] = {bitDpeths.recon[CHANNEL_TYPE_LUMA], bitDpeths.recon[CHANNEL_TYPE_CHROMA]};
1745
1746        m_confReconFile[layerId].open(tempFileName, true, bitDepth, bitDepth, bitDepth ); // write mode
1747        this->setDecodedYuvLayerRefresh( layerId, false );
1748      }
1749
1750      std::vector<TComPic>::iterator itPic;
1751      for(itPic = layerBuffer->begin(); itPic != layerBuffer->end(); itPic++)
1752      {
1753        TComPic checkPic = *itPic;
1754        const Window &conf = checkPic.getConformanceWindow();
1755        const Window &defDisp = m_respectDefDispWindow ? checkPic.getDefDisplayWindow() : Window();
1756        Int xScal = 1, yScal = 1;
1757 
1758        UInt chromaFormatIdc = checkPic.getSlice(0)->getChromaFormatIdc();
1759        xScal = TComSPS::getWinUnitX( chromaFormatIdc );
1760        yScal = TComSPS::getWinUnitY( chromaFormatIdc );
1761 
1762        if( checkPic.getPOC() <= pocValue )
1763        {
1764          TComPicYuv* pPicCYuvRec = checkPic.getPicYuvRec();
1765          m_confReconFile[layerId].write( pPicCYuvRec, m_outputColourSpaceConvert,
1766            conf.getWindowLeftOffset()  * xScal + defDisp.getWindowLeftOffset(),
1767            conf.getWindowRightOffset() * xScal + defDisp.getWindowRightOffset(),
1768            conf.getWindowTopOffset()   * yScal + defDisp.getWindowTopOffset(),
1769            conf.getWindowBottomOffset()* yScal + defDisp.getWindowBottomOffset(),
1770            NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range );
1771
1772          layerBuffer->erase(itPic);
1773          itPic = layerBuffer->begin();  // Ensure doesn't go to infinite loop
1774          if(layerBuffer->size() == 0)
1775          {
1776            break;
1777          }
1778        }
1779      }
1780    }
1781  }
1782#endif
1783
1784  // Remove the picture from the listOfPocs
1785  listOfPocs.erase( listOfPocs.begin() );
1786}
1787
1788const TComVPS *TAppDecTop::findDpbParametersFromVps(std::vector<Int> const &listOfPocs, std::vector<Int> const *listOfPocsInEachLayer, std::vector<Int> const *listOfPocsPositionInEachLayer, DpbStatus &maxDpbLimit)
1789{
1790  Int targetOutputLsIdx = getCommonDecoderParams()->getTargetOutputLayerSetIdx();
1791  const TComVPS *vps = NULL;
1792
1793  if( targetOutputLsIdx == 0 )   // Only base layer is output
1794  {
1795    const TComSPS *sps = NULL;
1796    assert( listOfPocsInEachLayer[0].size() != 0 );
1797    TComList<TComPic*>::iterator iterPic;
1798    Int j;
1799    for(j = 0, iterPic = m_apcTDecTop[0]->getListPic()->begin(); j < listOfPocsPositionInEachLayer[0][0]; j++) // Picture to be output
1800    {
1801      iterPic++;
1802    }
1803    TComPic *pic = *iterPic;
1804    sps = pic->getSlice(0)->getSPS();   assert( sps->getLayerId() == 0 );
1805    vps = pic->getSlice(0)->getVPS();
1806    Int highestTId = sps->getMaxTLayers() - 1;
1807
1808    maxDpbLimit.m_numAUsNotDisplayed = sps->getNumReorderPics( highestTId ); // m_numAUsNotDisplayed is only variable name - stores reorderpics
1809    maxDpbLimit.m_maxLatencyIncrease = sps->getMaxLatencyIncreasePlus1( highestTId ) > 0;
1810    if( maxDpbLimit.m_maxLatencyIncrease )
1811    {
1812      maxDpbLimit.m_maxLatencyPictures = sps->getMaxLatencyIncreasePlus1( highestTId ) + sps->getNumReorderPics( highestTId ) - 1;
1813    }
1814    maxDpbLimit.m_numPicsInSubDpb[0] = sps->getMaxDecPicBuffering( highestTId );
1815  }
1816  else
1817  {
1818    // -------------------------------------
1819    // Find the VPS used for the pictures
1820    // -------------------------------------
1821    for( Int i = 0; i < MAX_VPS_LAYER_IDX_PLUS1; i++ )
1822    {
1823      if( m_apcTDecTop[i]->getListPic()->empty() )
1824      {
1825        assert( listOfPocsInEachLayer[i].size() == 0 );
1826        continue;
1827      }
1828
1829      std::vector<Int>::const_iterator it;
1830      it = find( listOfPocsInEachLayer[i].begin(), listOfPocsInEachLayer[i].end(), listOfPocs[0] );
1831      TComList<TComPic*>::iterator iterPic;
1832
1833      if( it != listOfPocsInEachLayer[i].end() )
1834      {
1835        Int picPosition = (Int)std::distance( listOfPocsInEachLayer[i].begin(), it );
1836        Int j;
1837
1838        // Picture to be output
1839        for( j = 0, iterPic = m_apcTDecTop[i]->getListPic()->begin(); j < listOfPocsPositionInEachLayer[i][picPosition]; j++ )
1840        {
1841          iterPic++;
1842        }
1843
1844        TComPic *pic = *iterPic;
1845        vps = pic->getSlice(0)->getVPS();
1846        break;
1847      }
1848    }
1849
1850    Int targetLsIdx       = vps->getOutputLayerSetIdx( getCommonDecoderParams()->getTargetOutputLayerSetIdx() );
1851    Int highestTId = vps->getMaxTLayers() - 1;
1852
1853    maxDpbLimit.m_numAUsNotDisplayed = vps->getMaxVpsNumReorderPics( targetOutputLsIdx, highestTId ); // m_numAUsNotDisplayed is only variable name - stores reorderpics
1854    maxDpbLimit.m_maxLatencyIncrease  = vps->getMaxVpsLatencyIncreasePlus1(targetOutputLsIdx, highestTId ) > 0;
1855
1856    if( maxDpbLimit.m_maxLatencyIncrease )
1857    {
1858      maxDpbLimit.m_maxLatencyPictures = vps->getMaxVpsNumReorderPics( targetOutputLsIdx, highestTId ) + vps->getMaxVpsLatencyIncreasePlus1(targetOutputLsIdx, highestTId ) - 1;
1859    }
1860
1861    for(Int i = 0; i < vps->getNumLayersInIdList( targetLsIdx ); i++)
1862    {
1863      maxDpbLimit.m_numPicsInSubDpb[i] = vps->getMaxVpsDecPicBufferingMinus1( targetOutputLsIdx, i, highestTId) + 1;
1864    }
1865    // -------------------------------------
1866  }
1867  return vps;
1868}
1869
1870Void TAppDecTop::emptyUnusedPicturesNotNeededForOutput()
1871{
1872  for( Int layerIdx = 0; layerIdx < MAX_VPS_LAYER_IDX_PLUS1; layerIdx++ )
1873  {
1874    TComList <TComPic*> *pcListPic = m_apcTDecTop[layerIdx]->getListPic();
1875    TComList<TComPic*>::iterator iterPic = pcListPic->begin();
1876    while ( iterPic != pcListPic->end() )
1877    {
1878      TComPic *pic = *iterPic;
1879
1880      assert( pic->getPicSym() );
1881
1882      if( !pic->getSlice(0)->isReferenced() && !pic->getOutputMark() )
1883      {
1884        // Emtpy the picture buffer
1885        pic->setReconMark( false );
1886      }
1887      iterPic++;
1888    }
1889  }
1890}
1891
1892Bool TAppDecTop::ifInvokeBumpingBeforeDecoding( const DpbStatus &dpbStatus, const DpbStatus &dpbLimit, const Int layerIdx, const Int subDpbIdx )
1893{
1894  Bool retVal = false;
1895  // Number of reorder picutres
1896  retVal |= ( dpbStatus.m_numAUsNotDisplayed > dpbLimit.m_numAUsNotDisplayed );
1897
1898  // Number of pictures in each sub-DPB
1899  retVal |= ( dpbStatus.m_numPicsInSubDpb[subDpbIdx] >= dpbLimit.m_numPicsInSubDpb[subDpbIdx] );
1900
1901  return retVal;
1902}
1903
1904Bool TAppDecTop::ifInvokeBumpingAfterDecoding( const DpbStatus &dpbStatus, const DpbStatus &dpbLimit )
1905{
1906  Bool retVal = false;
1907
1908  // Number of reorder picutres
1909  retVal |= ( dpbStatus.m_numAUsNotDisplayed > dpbLimit.m_numAUsNotDisplayed );
1910
1911  return retVal;
1912}
1913
1914Void TAppDecTop::xFindDPBStatus( std::vector<Int> &listOfPocs
1915                            , std::vector<Int> *listOfPocsInEachLayer
1916                            , std::vector<Int> *listOfPocsPositionInEachLayer
1917                            , DpbStatus &dpbStatus
1918                            , Bool notOutputCurrAu
1919                            )
1920{
1921  const TComVPS *vps = NULL;
1922  dpbStatus.init();
1923
1924  for( Int i = 0; i < MAX_VPS_LAYER_IDX_PLUS1; i++ )
1925  {
1926    if( m_apcTDecTop[i]->getListPic()->empty() )
1927    {
1928      continue;
1929    }
1930   
1931    // To check # AUs that have at least one picture not output,
1932    // For each layer, populate listOfPOcs if not already present
1933    TComList<TComPic*>::iterator iterPic = m_apcTDecTop[i]->getListPic()->begin();
1934    Int picPositionInList = 0;
1935    while (iterPic != m_apcTDecTop[i]->getListPic()->end())
1936    {
1937      TComPic* pic = *(iterPic);
1938      if( pic->getReconMark() )
1939      {
1940        if( vps == NULL )
1941        {
1942          vps = pic->getSlice(0)->getVPS();
1943        }
1944
1945        if( !(pic->isCurrAu() && notOutputCurrAu ) )
1946        {
1947          std::vector<Int>::iterator it;
1948          if( pic->getOutputMark() ) // && pic->getPOC() > m_aiPOCLastDisplay[i])
1949          {
1950            it = find( listOfPocs.begin(), listOfPocs.end(), pic->getPOC() ); // Check if already included
1951
1952            if( it == listOfPocs.end() )  // New POC value - i.e. new AU - add to the list
1953            {
1954              listOfPocs.push_back( pic->getPOC() );
1955            }
1956            listOfPocsInEachLayer         [i].push_back( pic->getPOC()    );    // POC to be output in each layer
1957            listOfPocsPositionInEachLayer [i].push_back( picPositionInList  );  // For ease of access
1958          }
1959
1960          if( pic->getSlice(0)->isReferenced() || pic->getOutputMark() )
1961          {
1962            dpbStatus.m_numPicsInSubDpb[i]++;  // Count pictures that are "used for reference" or "needed for output"
1963          }
1964        }
1965      }
1966
1967      iterPic++;
1968      picPositionInList++;
1969    }
1970  }
1971
1972  assert( vps != NULL );    // No picture in any DPB?
1973  std::sort( listOfPocs.begin(), listOfPocs.end() );    // Sort in increasing order of POC
1974  Int targetLsIdx = vps->getOutputLayerSetIdx( getCommonDecoderParams()->getTargetOutputLayerSetIdx() );
1975
1976  // Update status
1977  dpbStatus.m_numAUsNotDisplayed = (Int)listOfPocs.size();   // Number of AUs not displayed
1978  dpbStatus.m_numLayers = vps->getNumLayersInIdList( targetLsIdx );
1979
1980  for( Int i = 0; i < dpbStatus.m_numLayers; i++ )
1981  {
1982    dpbStatus.m_layerIdToSubDpbIdMap[vps->getLayerSetLayerIdList(targetLsIdx, i)] = i;
1983    dpbStatus.m_targetDecLayerIdList[i] = vps->getLayerSetLayerIdList(targetLsIdx, i);  // Layer Id stored in a particular sub-DPB
1984  }
1985  dpbStatus.m_numSubDpbs = vps->getNumSubDpbs( targetLsIdx ); 
1986
1987  for( Int i = 0; i < MAX_VPS_LAYER_IDX_PLUS1; i++ )
1988  {
1989    dpbStatus.m_numPicsNotDisplayedInLayer[i] = (Int)listOfPocsInEachLayer[i].size();
1990  }
1991  assert( dpbStatus.m_numAUsNotDisplayed != -1 );
1992} 
1993
1994Void TAppDecTop::outputAllPictures(Int layerId, Bool notOutputCurrPic)
1995{
1996  { // All pictures in the DPB in that layer are to be output; this means other pictures would also be output
1997    std::vector<Int>  listOfPocs;
1998    std::vector<Int>  listOfPocsInEachLayer[MAX_VPS_LAYER_IDX_PLUS1];
1999    std::vector<Int>  listOfPocsPositionInEachLayer[MAX_VPS_LAYER_IDX_PLUS1];
2000
2001    DpbStatus dpbStatus;
2002
2003    // Find the status of the DPB
2004    xFindDPBStatus(listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus, notOutputCurrPic);
2005
2006    if( listOfPocs.size() )
2007    {
2008      while( listOfPocsInEachLayer[layerId].size() )    // As long as there picture in the layer to be output
2009      {
2010        bumpingProcess( listOfPocs, listOfPocsInEachLayer, listOfPocsPositionInEachLayer, dpbStatus );
2011      }
2012    }
2013  }
2014}
2015#endif //ALIGNED_BUMPING
2016
2017//! \}
Note: See TracBrowser for help on using the repository browser.