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

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

port rev 4600, remove macro Q0074_COLOUR_REMAPPING_SEI

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