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

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

port rev 4732, update copyright notice to include 2016

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