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

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

fix the bitdepth for writing picture out in the conformance mode

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