source: 3DVCSoftware/branches/HTM-15.1-dev0/source/App/TAppDecoder/TAppDecTop.cpp

Last change on this file was 1321, checked in by tech, 10 years ago

Merged 15.0-dev0@1320.

  • Property svn:eol-style set to native
File size: 111.5 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
51//! \ingroup TAppDecoder
52//! \{
53
54// ====================================================================================================================
55// Constructor / destructor / initialization / destroy
56// ====================================================================================================================
57
58TAppDecTop::TAppDecTop()
59#if !NH_MV
60: m_iPOCLastDisplay(-MAX_INT)
61#else
62: m_numDecoders( 0 )
63#endif
64{
65#if NH_MV
66  for (Int i = 0; i < MAX_NUM_LAYER_IDS; i++)
67  {
68    m_layerIdToDecIdx                     [i] = -1;
69    m_layerInitilizedFlag                 [i] = false;
70    m_eosInLayer                          [i] = false;
71    m_firstPicInLayerDecodedFlag          [i] = false;
72    m_pocDecrementedInDpbFlag             [i] = false;
73    m_decodingOrder                       [i] = 0;
74    m_lastPresentPocResetIdc              [i] = MIN_INT;
75    m_firstPicInPocResettingPeriodReceived[i] = true;
76    m_noRaslOutputFlagAssocIrap           [i] = false;
77  }
78
79  m_curPic                          = NULL;
80  m_vps                             = NULL;
81  m_sps                             = NULL;
82  m_pps                             = NULL;
83  m_initilizedFromVPS               = false;
84  m_firstSliceInBitstream           = true;
85  m_newVpsActivatedbyCurAu          = false;
86  m_newVpsActivatedbyCurPic         = false;
87  m_handleCraAsBlaFlag              = false;
88  m_handleCraAsBlaFlagSetByExtMeans = false;
89  m_noClrasOutputFlag               = false;
90  m_noClrasOutputFlagSetByExtMeans  = false;
91  m_layerResetFlag                  = false;
92  m_totalNumofPicsReceived          = 0;
93  m_cvsStartFound                   = false;
94#endif
95#if NH_3D
96    m_pScaleOffsetFile              = 0;
97#endif
98}
99
100Void TAppDecTop::create()
101{
102#if NH_MV
103#if ENC_DEC_TRACE
104  if ( g_hTrace == NULL )
105  {
106    g_hTrace = fopen( "TraceDec.txt", "wb" );
107    g_bJustDoIt = g_bEncDecTraceDisable;
108    g_nSymbolCounter = 0;
109  }
110#endif
111#endif
112}
113
114Void TAppDecTop::destroy()
115{
116#if NH_MV
117  // destroy internal classes
118  xDestroyDecLib();
119#endif
120
121  if (m_pchBitstreamFile)
122  {
123    free (m_pchBitstreamFile);
124    m_pchBitstreamFile = NULL;
125  }
126#if NH_MV
127  for (Int decIdx = 0; decIdx < m_numDecoders; decIdx++)
128  {
129    if (m_pchReconFiles[decIdx])
130    {
131      free (m_pchReconFiles[decIdx]);
132      m_pchReconFiles[decIdx] = NULL;
133    }
134  }
135#endif
136  if (m_pchReconFile)
137  {
138    free (m_pchReconFile);
139    m_pchReconFile = NULL;
140  }
141#if NH_3D
142  if (m_pchScaleOffsetFile)
143  {
144    free (m_pchScaleOffsetFile);
145    m_pchScaleOffsetFile = NULL;
146  }
147#endif
148}
149
150// ====================================================================================================================
151// Public member functions
152// ====================================================================================================================
153
154/**
155 - create internal class
156 - initialize internal class
157 - until the end of the bitstream, call decoding function in TDecTop class
158 - delete allocated buffers
159 - destroy internal class
160 .
161 */
162
163#if NH_MV
164Void TAppDecTop::decode()
165{
166  // create & initialize internal classes
167  xInitFileIO  ();
168  xCreateDecLib();
169  xInitDecLib  ();
170
171  InputByteStream bytestream(m_bitstreamFile);
172
173  while ( m_bitstreamFile )
174  {
175    AnnexBStats stats = AnnexBStats();
176    InputNALUnit nalu;
177
178    byteStreamNALUnit(bytestream, nalu.getBitstream().getFifo(), stats);
179
180    if (nalu.getBitstream().getFifo().empty())
181    {
182      /* this can happen if the following occur:
183       *  - empty input file
184       *  - two back-to-back start_code_prefixes
185       *  - start_code_prefix immediately followed by EOF
186       */
187      fprintf(stderr, "Warning: Attempt to decode an empty NAL unit\n");
188    }
189    else
190    {
191      read(nalu);
192
193      if ( m_printReceivedNalus )
194      {
195        std::cout << "Received NAL unit: ";
196        nalu.print();
197        std::cout << std::endl;
198      }
199
200      if ( xExtractAndRewrite( &nalu )
201          && nalu.m_nuhLayerId <= MAX_NUM_LAYER_IDS-1
202          && !(nalu.m_nalUnitType == NAL_UNIT_VPS && nalu.m_nuhLayerId > 0)
203          && !(nalu.m_nalUnitType == NAL_UNIT_EOB && nalu.m_nuhLayerId > 0)
204         )
205      {
206        xGetDecoderIdx( nalu.m_nuhLayerId , true );
207        if( nalu.isSlice() )
208        {
209          xProcessVclNalu   ( nalu );
210        }
211        else
212        {
213          xProcessNonVclNalu( nalu );
214        }
215      }
216    }
217  }
218 xTerminateDecoding();
219}
220#endif
221
222#if !NH_MV
223Void TAppDecTop::decode()
224{
225  Int                 poc;
226  TComList<TComPic*>* pcListPic = NULL;
227
228  ifstream bitstreamFile(m_pchBitstreamFile, ifstream::in | ifstream::binary);
229  if (!bitstreamFile)
230  {
231    fprintf(stderr, "\nfailed to open bitstream file `%s' for reading\n", m_pchBitstreamFile);
232    exit(EXIT_FAILURE);
233  }
234
235  InputByteStream bytestream(bitstreamFile);
236
237  if (!m_outputDecodedSEIMessagesFilename.empty() && m_outputDecodedSEIMessagesFilename!="-")
238  {
239    m_seiMessageFileStream.open(m_outputDecodedSEIMessagesFilename.c_str(), std::ios::out);
240    if (!m_seiMessageFileStream.is_open() || !m_seiMessageFileStream.good())
241    {
242      fprintf(stderr, "\nUnable to open file `%s' for writing decoded SEI messages\n", m_outputDecodedSEIMessagesFilename.c_str());
243      exit(EXIT_FAILURE);
244    }
245  }
246
247  // create & initialize internal classes
248  xCreateDecLib();
249  xInitDecLib  ();
250
251  m_iPOCLastDisplay += m_iSkipFrame;      // set the last displayed POC correctly for skip forward.
252
253  // main decoder loop
254  Bool openedReconFile = false; // reconstruction file not yet opened. (must be performed after SPS is seen)
255  Bool loopFiltered = false;
256
257  while (!!bitstreamFile)
258  {
259    /* location serves to work around a design fault in the decoder, whereby
260     * the process of reading a new slice that is the first slice of a new frame
261     * requires the TDecTop::decode() method to be called again with the same
262     * nal unit. */
263#if RExt__DECODER_DEBUG_BIT_STATISTICS
264    TComCodingStatistics::TComCodingStatisticsData backupStats(TComCodingStatistics::GetStatistics());
265    streampos location = bitstreamFile.tellg() - streampos(bytestream.GetNumBufferedBytes());
266#else
267    streampos location = bitstreamFile.tellg();
268#endif
269    AnnexBStats stats = AnnexBStats();
270
271    InputNALUnit nalu;
272    byteStreamNALUnit(bytestream, nalu.getBitstream().getFifo(), stats);
273
274    // call actual decoding function
275    Bool bNewPicture = false;
276    if (nalu.getBitstream().getFifo().empty())
277    {
278      /* this can happen if the following occur:
279       *  - empty input file
280       *  - two back-to-back start_code_prefixes
281       *  - start_code_prefix immediately followed by EOF
282       */
283      fprintf(stderr, "Warning: Attempt to decode an empty NAL unit\n");
284    }
285    else
286    {
287      read(nalu);
288
289      if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) || !isNaluWithinTargetDecLayerIdSet(&nalu)  )
290      {
291        bNewPicture = false;
292      }
293      else
294      {
295        bNewPicture = m_cTDecTop.decode(nalu, m_iSkipFrame, m_iPOCLastDisplay);
296        if (bNewPicture)
297        {
298          bitstreamFile.clear();
299          /* location points to the current nalunit payload[1] due to the
300           * need for the annexB parser to read three extra bytes.
301           * [1] except for the first NAL unit in the file
302           *     (but bNewPicture doesn't happen then) */
303#if RExt__DECODER_DEBUG_BIT_STATISTICS
304          bitstreamFile.seekg(location);
305          bytestream.reset();
306          TComCodingStatistics::SetStatistics(backupStats);
307#else
308          bitstreamFile.seekg(location-streamoff(3));
309          bytestream.reset();
310#endif
311        }
312      }
313    }
314
315    if ( (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS) &&
316
317      !m_cTDecTop.getFirstSliceInSequence () )
318
319    {
320      if (!loopFiltered || bitstreamFile)
321      {
322        m_cTDecTop.executeLoopFilters(poc, pcListPic);
323      }
324      loopFiltered = (nalu.m_nalUnitType == NAL_UNIT_EOS);
325      if (nalu.m_nalUnitType == NAL_UNIT_EOS)
326      {
327        m_cTDecTop.setFirstSliceInSequence(true);
328      }
329    }
330    else if ( (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS ) &&
331              m_cTDecTop.getFirstSliceInSequence () )
332    {
333      m_cTDecTop.setFirstSliceInPicture (true);
334   }
335
336    if( pcListPic )
337    {
338      if ( m_pchReconFile && !openedReconFile )
339      {
340        const BitDepths &bitDepths=pcListPic->front()->getPicSym()->getSPS().getBitDepths(); // use bit depths of first reconstructed picture.
341        for (UInt channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
342        {
343          if (m_outputBitDepth[channelType] == 0)
344          {
345            m_outputBitDepth[channelType] = bitDepths.recon[channelType];
346          }
347        }
348        m_cTVideoIOYuvReconFile.open( m_pchReconFile, true, m_outputBitDepth, m_outputBitDepth, bitDepths.recon ); // write mode
349        openedReconFile = true;
350      }
351      // write reconstruction to file
352      if( bNewPicture )
353      {
354        xWriteOutput( pcListPic, nalu.m_temporalId );
355      }
356      if ( (bNewPicture || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA) && m_cTDecTop.getNoOutputPriorPicsFlag() )
357      {
358        m_cTDecTop.checkNoOutputPriorPics( pcListPic );
359        m_cTDecTop.setNoOutputPriorPicsFlag (false);
360      }
361
362      if ( bNewPicture &&
363           (   nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
364            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
365            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP
366            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL
367            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP ) )
368      {
369
370        xFlushOutput( pcListPic );
371      }
372      if (nalu.m_nalUnitType == NAL_UNIT_EOS)
373      {
374
375        xWriteOutput( pcListPic, nalu.m_temporalId );
376        m_cTDecTop.setFirstSliceInPicture (false);
377      }
378      // write reconstruction to file -- for additional bumping as defined in C.5.2.3
379      if(!bNewPicture && nalu.m_nalUnitType >= NAL_UNIT_CODED_SLICE_TRAIL_N && nalu.m_nalUnitType <= NAL_UNIT_RESERVED_VCL31)
380      {
381        xWriteOutput( pcListPic, nalu.m_temporalId );
382      }
383    }
384  }
385
386  xFlushOutput( pcListPic );
387  // delete buffers
388  m_cTDecTop.deletePicBuffer();
389  // destroy internal classes
390  xDestroyDecLib();
391}
392#endif
393
394// ====================================================================================================================
395// Protected member functions
396// ====================================================================================================================
397
398Void TAppDecTop::xCreateDecLib()
399{
400#if NH_MV
401  // initialize global variables
402  initROM();
403#if NH_3D_DMM
404  initWedgeLists();
405#endif
406#else
407  // create decoder class
408  m_cTDecTop.create();
409#endif
410}
411
412Void TAppDecTop::xDestroyDecLib()
413{
414#if NH_MV
415  // destroy ROM
416  destroyROM();
417
418  for(Int decIdx = 0; decIdx < m_numDecoders ; decIdx++)
419  {
420    if( m_tVideoIOYuvReconFile[decIdx] )
421    {
422      m_tVideoIOYuvReconFile[decIdx]->close();
423      delete m_tVideoIOYuvReconFile[decIdx];
424      m_tVideoIOYuvReconFile[decIdx] = NULL ;
425    }
426
427    if( m_tDecTop[decIdx] )
428    {
429#if !NH_MV
430      m_tDecTop[decIdx]->deletePicBuffer();
431#endif
432      m_tDecTop[decIdx]->destroy() ;
433    }
434    delete m_tDecTop[decIdx] ;
435    m_tDecTop[decIdx] = NULL ;
436  }
437#else
438  if ( m_pchReconFile )
439  {
440    m_cTVideoIOYuvReconFile. close();
441  }
442
443  // destroy decoder class
444  m_cTDecTop.destroy();
445#endif
446#if NH_3D
447  m_cCamParsCollector.uninit();
448  if( m_pScaleOffsetFile )
449  {
450    ::fclose( m_pScaleOffsetFile );
451  }
452#endif
453}
454
455Void TAppDecTop::xInitDecLib()
456{
457
458#if NH_3D
459  m_cCamParsCollector.setCodeScaleOffsetFile( m_pScaleOffsetFile );
460#endif
461#if NH_MV
462  m_dpb.setPrintPicOutput(m_printPicOutput);
463#else
464  // initialize decoder class
465  m_cTDecTop.init();
466  m_cTDecTop.setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
467#if O0043_BEST_EFFORT_DECODING
468  m_cTDecTop.setForceDecodeBitDepth(m_forceDecodeBitDepth);
469#endif
470  if (!m_outputDecodedSEIMessagesFilename.empty())
471  {
472    std::ostream &os=m_seiMessageFileStream.is_open() ? m_seiMessageFileStream : std::cout;
473    m_cTDecTop.setDecodedSEIMessageOutputStream(&os);
474  }
475#endif
476}
477
478
479#if !NH_MV
480/** \param pcListPic list of pictures to be written to file
481    \param tId       temporal sub-layer ID
482 */
483Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, UInt tId )
484{
485  if (pcListPic->empty())
486  {
487    return;
488  }
489
490  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
491  Int numPicsNotYetDisplayed = 0;
492  Int dpbFullness = 0;
493  const TComSPS* activeSPS = &(pcListPic->front()->getPicSym()->getSPS());
494
495  UInt numReorderPicsHighestTid;
496  UInt maxDecPicBufferingHighestTid;
497  UInt maxNrSublayers = activeSPS->getMaxTLayers();
498
499  if(m_iMaxTemporalLayer == -1 || m_iMaxTemporalLayer >= maxNrSublayers)
500  {
501    numReorderPicsHighestTid = activeSPS->getNumReorderPics(maxNrSublayers-1);
502    maxDecPicBufferingHighestTid =  activeSPS->getMaxDecPicBuffering(maxNrSublayers-1);
503  }
504  else
505  {
506    numReorderPicsHighestTid = activeSPS->getNumReorderPics(m_iMaxTemporalLayer);
507    maxDecPicBufferingHighestTid = activeSPS->getMaxDecPicBuffering(m_iMaxTemporalLayer);
508  }
509
510  while (iterPic != pcListPic->end())
511  {
512    TComPic* pcPic = *(iterPic);
513    if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay)
514    {
515       numPicsNotYetDisplayed++;
516      dpbFullness++;
517    }
518    else if(pcPic->getSlice( 0 )->isReferenced())
519    {
520      dpbFullness++;
521    }
522    iterPic++;
523  }
524
525  iterPic = pcListPic->begin();
526
527  if (numPicsNotYetDisplayed>2)
528  {
529    iterPic++;
530  }
531
532  TComPic* pcPic = *(iterPic);
533  if (numPicsNotYetDisplayed>2 && pcPic->isField()) //Field Decoding
534  {
535    TComList<TComPic*>::iterator endPic   = pcListPic->end();
536    endPic--;
537    iterPic   = pcListPic->begin();
538    while (iterPic != endPic)
539    {
540      TComPic* pcPicTop = *(iterPic);
541      iterPic++;
542      TComPic* pcPicBottom = *(iterPic);
543
544      if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() &&
545          (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid) &&
546          (!(pcPicTop->getPOC()%2) && pcPicBottom->getPOC() == pcPicTop->getPOC()+1) &&
547          (pcPicTop->getPOC() == m_iPOCLastDisplay+1 || m_iPOCLastDisplay < 0))
548      {
549        // write to file
550        numPicsNotYetDisplayed = numPicsNotYetDisplayed-2;
551        if ( m_pchReconFile )
552        {
553          const Window &conf = pcPicTop->getConformanceWindow();
554          const Window  defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
555          const Bool isTff = pcPicTop->isTopField();
556
557          Bool display = true;
558          if( m_decodedNoDisplaySEIEnabled )
559          {
560            SEIMessages noDisplay = getSeisByType(pcPic->getSEIs(), SEI::NO_DISPLAY );
561            const SEINoDisplay *nd = ( noDisplay.size() > 0 ) ? (SEINoDisplay*) *(noDisplay.begin()) : NULL;
562            if( (nd != NULL) && nd->m_noDisplay )
563            {
564              display = false;
565            }
566          }
567
568          if (display)
569          {
570        m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
571                                           m_outputColourSpaceConvert,
572                                           conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
573                                           conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
574                                           conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
575                                           conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), NUM_CHROMA_FORMAT, isTff );
576          }
577        }
578
579        // update POC of display order
580        m_iPOCLastDisplay = pcPicBottom->getPOC();
581
582        // erase non-referenced picture in the reference picture list after display
583        if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
584        {
585          pcPicTop->setReconMark(false);
586
587          // mark it should be extended later
588          pcPicTop->getPicYuvRec()->setBorderExtension( false );
589        }
590        if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
591        {
592          pcPicBottom->setReconMark(false);
593
594          // mark it should be extended later
595          pcPicBottom->getPicYuvRec()->setBorderExtension( false );
596        }
597        pcPicTop->setOutputMark(false);
598        pcPicBottom->setOutputMark(false);
599      }
600    }
601  }
602  else if (!pcPic->isField()) //Frame Decoding
603  {
604    iterPic = pcListPic->begin();
605
606    while (iterPic != pcListPic->end())
607    {
608      pcPic = *(iterPic);
609
610      if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay &&
611        (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid))
612      {
613        // write to file
614         numPicsNotYetDisplayed--;
615        if(pcPic->getSlice(0)->isReferenced() == false)
616        {
617          dpbFullness--;
618        }
619        if ( m_pchReconFile )
620        {
621          const Window &conf    = pcPic->getConformanceWindow();
622          const Window defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
623
624          m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
625                                         m_outputColourSpaceConvert,
626                                         conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
627                                         conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
628                                         conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
629                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(),
630                                         NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range  );
631        }
632
633        // update POC of display order
634        m_iPOCLastDisplay = pcPic->getPOC();
635
636        // erase non-referenced picture in the reference picture list after display
637        if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
638        {
639          pcPic->setReconMark(false);
640
641          // mark it should be extended later
642          pcPic->getPicYuvRec()->setBorderExtension( false );
643        }
644        pcPic->setOutputMark(false);
645      }
646
647      iterPic++;
648    }
649  }
650}
651
652/** \param pcListPic list of pictures to be written to file
653 */
654Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic )
655{
656  if(!pcListPic || pcListPic->empty())
657  {
658    return;
659  }
660  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
661
662  iterPic   = pcListPic->begin();
663  TComPic* pcPic = *(iterPic);
664
665  if (pcPic->isField()) //Field Decoding
666  {
667    TComList<TComPic*>::iterator endPic   = pcListPic->end();
668    endPic--;
669    TComPic *pcPicTop, *pcPicBottom = NULL;
670    while (iterPic != endPic)
671    {
672      pcPicTop = *(iterPic);
673      iterPic++;
674      pcPicBottom = *(iterPic);
675
676      if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() && !(pcPicTop->getPOC()%2) && (pcPicBottom->getPOC() == pcPicTop->getPOC()+1) )
677      {
678        // write to file
679
680        if ( m_pchReconFile )
681        {
682          const Window &conf = pcPicTop->getConformanceWindow();
683          const Window  defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
684          const Bool isTff = pcPicTop->isTopField();
685          m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
686                                         m_outputColourSpaceConvert,
687                                         conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
688                                         conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
689                                         conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
690                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), NUM_CHROMA_FORMAT, isTff );
691        }
692
693        // update POC of display order
694        m_iPOCLastDisplay = pcPicBottom->getPOC();
695
696        // erase non-referenced picture in the reference picture list after display
697        if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
698        {
699          pcPicTop->setReconMark(false);
700
701          // mark it should be extended later
702          pcPicTop->getPicYuvRec()->setBorderExtension( false );
703        }
704        if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
705        {
706          pcPicBottom->setReconMark(false);
707
708          // mark it should be extended later
709          pcPicBottom->getPicYuvRec()->setBorderExtension( false );
710        }
711        pcPicTop->setOutputMark(false);
712        pcPicBottom->setOutputMark(false);
713
714        if(pcPicTop)
715        {
716          pcPicTop->destroy();
717          delete pcPicTop;
718          pcPicTop = NULL;
719        }
720      }
721    }
722    if(pcPicBottom)
723    {
724      pcPicBottom->destroy();
725      delete pcPicBottom;
726      pcPicBottom = NULL;
727    }
728  }
729  else //Frame decoding
730  {
731    while (iterPic != pcListPic->end())
732    {
733      pcPic = *(iterPic);
734
735      if ( pcPic->getOutputMark() )
736      {
737        // write to file
738        if ( m_pchReconFile )
739        {
740          const Window &conf    = pcPic->getConformanceWindow();
741          const Window  defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
742          m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
743                                         m_outputColourSpaceConvert,
744                                         conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
745                                         conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
746                                         conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
747                                         conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(),
748                                         NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range );
749        }
750
751        // update POC of display order
752        m_iPOCLastDisplay = pcPic->getPOC();
753        // erase non-referenced picture in the reference picture list after display
754        if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
755        {
756          pcPic->setReconMark(false);
757
758          // mark it should be extended later
759          pcPic->getPicYuvRec()->setBorderExtension( false );
760        }
761        pcPic->setOutputMark(false);
762      }
763      if(pcPic != NULL)
764      {
765        pcPic->destroy();
766        delete pcPic;
767        pcPic = NULL;
768      }
769      iterPic++;
770    }
771  }
772  pcListPic->clear();
773  m_iPOCLastDisplay = -MAX_INT;
774}
775#endif
776/** \param nalu Input nalu to check whether its LayerId is within targetDecLayerIdSet
777 */
778#if NH_MV
779Bool TAppDecTop::xIsNaluInTargetDecLayerIdSet( InputNALUnit* nalu )
780#else
781Bool TAppDecTop::isNaluWithinTargetDecLayerIdSet( InputNALUnit* nalu )
782#endif
783{
784  if ( m_targetDecLayerIdSet.size() == 0 ) // By default, the set is empty, meaning all LayerIds are allowed
785  {
786    return true;
787  }
788  for (std::vector<Int>::iterator it = m_targetDecLayerIdSet.begin(); it != m_targetDecLayerIdSet.end(); it++)
789  {
790    if ( nalu->m_nuhLayerId == (*it) )
791    {
792      return true;
793    }
794  }
795  return false;
796}
797
798#if NH_MV
799
800Bool TAppDecTop::xExtractAndRewrite( InputNALUnit* nalu )
801{
802  Bool naluInSubStream;
803  if ( !m_initilizedFromVPS )
804  {
805    naluInSubStream = true; // No active VPS yet. Wait for slice activating one.
806  }
807  else
808  {
809    if ( m_decProcCvsg == CLAUSE_8 )
810    {
811      // 8.1.2->clause 10
812
813      // sub-bitstream extraction process as specified in clause 10
814      naluInSubStream = true;
815      if ( nalu->m_temporalId > m_highestTid || !xIsNaluInTargetDecLayerIdSet(nalu) )
816      {
817        naluInSubStream = false;
818      }
819    }
820    else
821    {
822      // F.8.1.2
823      Int targetDecLayerSetIdx = m_vps->olsIdxToLsIdx( m_targetOptLayerSetIdx );
824      if ( targetDecLayerSetIdx <= m_vps->getVpsNumLayerSetsMinus1() && m_vps->getVpsBaseLayerInternalFlag() )
825      {
826        // - If TargetDecLayerSetIdx is less than or equal to vps_num_layer_sets_minus1 and
827        //   vps_base_layer_internal_flag is equal to 1, the following applies:
828        //   - The sub-bitstream extraction process as specified in clause 10 is applied with the CVSG, HighestTid and
829        //     TargetDecLayerIdList as inputs, and the output is assigned to a bitstream referred to as BitstreamToDecode.
830
831        naluInSubStream = true;
832        if ( nalu->m_temporalId > m_highestTid || !xIsNaluInTargetDecLayerIdSet(nalu) )
833        {
834          naluInSubStream = false;
835        }
836
837      }
838      else if ( targetDecLayerSetIdx <= m_vps->getVpsNumLayerSetsMinus1() && !m_vps->getVpsBaseLayerInternalFlag() )
839      {
840        // - Otherwise, if TargetDecLayerSetIdx is less than or equal to vps_num_layer_sets_minus1 and vps_base_layer_internal_flag
841        //   is equal to 0, the following applies:
842        //   - The sub-bitstream extraction process as specified in clause F.10.1 is applied with the CVSG, HighestTid and
843        //     TargetDecLayerIdList as inputs, and the output is assigned to a bitstream referred to as BitstreamToDecode.
844
845        naluInSubStream = true;
846        if ( nalu->m_temporalId > m_highestTid || !xIsNaluInTargetDecLayerIdSet(nalu) )
847        {
848          naluInSubStream = false;
849        }
850      }
851      else if ( targetDecLayerSetIdx > m_vps->getVpsNumLayerSetsMinus1() && m_vps->getNumLayersInIdList( targetDecLayerSetIdx ) == 1 )
852      {
853        // - Otherwise, if TargetDecLayerSetIdx is greater than vps_num_layer_sets_minus1 and
854        //   NumLayersInIdList[ TargetDecLayerSetIdx ] is equal to 1, the following applies:
855        //   - The independent non-base layer rewriting process of clause F.10.2 is applied with the CVSG, HighestTid and
856        //     TargetDecLayerIdList[ 0 ] as inputs, and the output is assigned to a bitstream referred to as BitstreamToDecode.
857
858        Int assingedBaseLayerId = m_targetDecLayerIdSet[0];
859        naluInSubStream = true;
860
861        if ( nalu->m_nalUnitType != NAL_UNIT_SPS &&
862          nalu->m_nalUnitType != NAL_UNIT_PPS &&
863          nalu->m_nalUnitType != NAL_UNIT_EOB &&
864          nalu->m_nuhLayerId != assingedBaseLayerId )
865        {
866          naluInSubStream = false;
867        }
868
869        if ( ( nalu->m_nalUnitType == NAL_UNIT_SPS || nalu->m_nalUnitType != NAL_UNIT_PPS ) &&
870          !( nalu->m_nuhLayerId == 0 || nalu->m_nuhLayerId == assingedBaseLayerId ) )
871        {
872          naluInSubStream = false;
873        }
874
875        if ( nalu->m_nalUnitType == NAL_UNIT_VPS )
876        {
877          naluInSubStream = false;
878        }
879
880        if ( nalu->m_temporalId > m_highestTid )
881        {
882          naluInSubStream = false;
883        }
884               
885        // For now, don't do the layer id change here, but change smallest layer id.
886        // To be verified.         
887        //if ( naluInSubStream )
888        //{
889        //  nalu->m_nuhLayerId = 0;
890        //}
891      }
892      else
893      {
894        // - Otherwise, the following applies:
895        //   - The sub-bitstream extraction process as specified in clause F.10.3 is applied with the CVSG, HighestTid and
896        //     TargetDecLayerIdList as inputs, and the output is assigned to a bitstream referred to as BitstreamToDecode.
897
898        naluInSubStream = true;
899
900        if ( nalu->m_nalUnitType != NAL_UNIT_VPS &&
901          nalu->m_nalUnitType != NAL_UNIT_SPS &&
902          nalu->m_nalUnitType != NAL_UNIT_PPS &&
903          nalu->m_nalUnitType != NAL_UNIT_EOS &&
904          nalu->m_nalUnitType != NAL_UNIT_EOB &&
905          !xIsNaluInTargetDecLayerIdSet(nalu) )
906        {
907          naluInSubStream = false;
908        }
909
910        if ( ( nalu->m_nalUnitType == NAL_UNIT_VPS ||
911          nalu->m_nalUnitType == NAL_UNIT_SPS ||
912          nalu->m_nalUnitType == NAL_UNIT_PPS ||
913          nalu->m_nalUnitType == NAL_UNIT_EOS    ) &&
914          !( nalu->m_nuhLayerId == 0 || xIsNaluInTargetDecLayerIdSet(nalu) )
915          )
916        {
917          naluInSubStream = false;
918        }
919
920        if ( nalu->m_temporalId > m_highestTid )
921        {
922          naluInSubStream = false;
923        }
924        // TBD: vps_base_layer_available_flag in each VPS is set equal to 0.
925      }
926    }
927  }
928
929  return naluInSubStream;
930}
931
932Void TAppDecTop::xProcessVclNalu( InputNALUnit nalu )
933{
934  TDecTop* dec  = xGetDecoder( nalu );
935
936  // Decode slice header of slice of current or new picture.
937  dec->decodeSliceHeader( nalu );
938
939  // Check if slice belongs to current or new picture
940  Bool sliceIsFirstOfNewPicture = dec->getFirstSliceSegementInPicFlag();
941
942  if ( !xIsSkipVclNalu( nalu, sliceIsFirstOfNewPicture ) )
943  {
944    if ( sliceIsFirstOfNewPicture )
945    {
946      xDetectNewPocResettingPeriod( nalu );
947      Bool sliceIsFirstOfNewAU = xDetectNewAu( nalu );
948      xFinalizePreviousPictures ( sliceIsFirstOfNewAU );
949      xDecodeFirstSliceOfPicture( nalu, sliceIsFirstOfNewAU );
950    }
951    else
952    {
953      xDecodeFollowSliceOfPicture( nalu );
954    }
955  }
956}
957
958Bool TAppDecTop::xIsSkipVclNalu( InputNALUnit& nalu, Bool isFirstSliceOfPic )
959{
960
961  TDecTop* dec = xGetDecoder( nalu ); 
962
963  m_handleCraAsBlaFlagSetByExtMeans = false;
964  Bool skipNalu = false;
965
966  if ( isFirstSliceOfPic )
967  {
968    m_totalNumofPicsReceived++;
969  }
970
971  if (!m_cvsStartFound )
972  {
973    // Skip as specified by decoder option. (Not normative!)
974    if ( m_totalNumofPicsReceived <= m_iSkipFrame )
975    {
976      skipNalu = true;
977      if ( isFirstSliceOfPic )
978      {
979        std::cout << "Layer " << std::setfill(' ') << std::setw(2) << nalu.m_nuhLayerId
980          << "   POC    ? Skipping picture." << std::setw(5) << m_totalNumofPicsReceived - 1 << std::endl;
981      }
982    }
983    else
984    {     
985      if ( dec->getIsInOwnTargetDecLayerIdList() )
986      {
987        // Search for initial IRAP access unit
988        Bool canBeSliceOfInitialIrapAu = nalu.isIrap() && ( dec->decProcClause8() || nalu.m_nuhLayerId == dec->getSmallestLayerId() );
989
990        if ( !canBeSliceOfInitialIrapAu )
991        {
992          skipNalu = true;
993          if ( isFirstSliceOfPic )
994          {
995            std::cout << "Layer " << std::setfill(' ') << std::setw(2) << nalu.m_nuhLayerId
996              << "   POC    ? Not an initial IRAP AU. Skipping picture." << std::setw(5) << m_totalNumofPicsReceived - 1 << std::endl;
997          }
998        }
999        else
1000        {
1001          m_cvsStartFound = true;
1002          if( nalu.isCra() )
1003          {
1004            // Ensure that NoRaslOutputFlag of picture is equal to 1.
1005            m_handleCraAsBlaFlagSetByExtMeans = true;
1006            m_handleCraAsBlaFlag = true;
1007          }
1008        }
1009      }
1010      else
1011      {
1012        skipNalu = true; 
1013      }
1014    }
1015  }
1016  else
1017  {
1018    assert( dec->getIsInOwnTargetDecLayerIdList() ); 
1019  }
1020  return skipNalu;
1021}
1022
1023Void TAppDecTop::xProcessNonVclNalu( InputNALUnit nalu )
1024{
1025  xGetDecoder(nalu)->decodeNonVclNalu( nalu );
1026
1027  if (  nalu.m_nalUnitType == NAL_UNIT_EOS )
1028  {
1029    m_eosInLayer[ nalu.m_nuhLayerId ] = true;
1030  }
1031}
1032Void TAppDecTop::xTerminateDecoding()
1033{
1034    xFinalizePic( true );
1035    xFinalizeAU ( );
1036
1037#if NH_3D
1038  if( m_cCamParsCollector.isInitialized() )
1039  {
1040    m_cCamParsCollector.setSlice( 0 );
1041  }
1042#endif
1043   xFlushOutput(); 
1044   m_dpb.emptyAllSubDpbs();
1045}
1046
1047
1048//////////////////////////////
1049/// Process slices
1050//////////////////////////////
1051
1052Void TAppDecTop::xDecodeFirstSliceOfPicture( InputNALUnit nalu, Bool sliceIsFirstOfNewAu )
1053{
1054  TDecTop* dec = xGetDecoder(nalu);
1055  // Initialize from VPS of first slice
1056
1057  // Get current SPS and PPS
1058  TComSlice* slicePilot = dec->getSlicePilot(); 
1059  m_vps = slicePilot->getVPS(); 
1060  m_sps = slicePilot->getSPS(); 
1061  m_pps = slicePilot->getPPS(); 
1062
1063  /// Use VPS activated by the first slice to initialized decoding
1064  if( !m_initilizedFromVPS )
1065  {
1066    xF811GeneralDecProc( nalu );
1067    m_initilizedFromVPS = true;
1068    m_newVpsActivatedbyCurAu  = true; //TBD
1069    m_newVpsActivatedbyCurPic = true;
1070#if NH_3D
1071    m_dpb.setVPS( m_vps ); 
1072#endif
1073  }
1074
1075  // Create new sub-DBP if not existing
1076  m_dpb.getSubDpb( nalu.m_nuhLayerId, true );
1077
1078  // Create a new picture initialize and make it the current picture
1079  assert( m_curPic == NULL );
1080  m_curPic = new TComPic;
1081
1082  m_curPic->create(*m_sps, *m_pps, true);
1083
1084  m_curPic->setLayerId                      ( nalu.m_nuhLayerId );
1085  m_curPic->setDecodingOrder                ( m_decodingOrder[ nalu.m_nuhLayerId ]);
1086  m_curPic->setIsFstPicOfAllLayOfPocResetPer( m_newPicIsFstPicOfAllLayOfPocResetPer );
1087  m_curPic->setIsPocResettingPic            ( m_newPicIsPocResettingPic );
1088  m_curPic->setActivatesNewVps              ( m_newVpsActivatedbyCurPic );
1089
1090  dec     ->activatePSsAndInitPicOrSlice( m_curPic );
1091
1092  m_decodingOrder[ nalu.m_nuhLayerId ]++;
1093
1094  // Insert pic to current AU
1095  // ( There is also a "current AU in the DBP", however the DBP AU will include the current
1096  //   picture only after it is inserted to the DBP )
1097  m_curAu.addPic(  m_curPic, true );
1098
1099  // Invoke Claus 8 and Annex F decoding process for a picture (only parts before POC derivation ).
1100  xPicDecoding( START_PIC, sliceIsFirstOfNewAu );
1101
1102  if (m_decProcCvsg == ANNEX_F )
1103  {
1104    // Do output before POC derivation
1105    xF13522OutputAndRemOfPicsFromDpb( true );
1106  }
1107
1108  // Decode POC and apply reference picture set
1109  dec->setDecProcPocAndRps( m_decProcPocAndRps );
1110  dec->decodePocAndRps( );
1111
1112  // Do output after POC and RPS derivation
1113  if (m_decProcCvsg == CLAUSE_8 )
1114  {
1115    xC522OutputAndRemOfPicsFromDpb( );
1116  }
1117  else if (m_decProcCvsg == ANNEX_F )
1118  {
1119    xF13522OutputAndRemOfPicsFromDpb( false );
1120  }
1121  else
1122  {
1123    assert(false);
1124  }
1125
1126  // Generate unavailable reference pictures
1127  dec->genUnavailableRefPics( );
1128
1129  // decode first slice segment
1130  dec->decodeSliceSegment( nalu );
1131
1132  m_firstSliceInBitstream = false;
1133}
1134
1135Void TAppDecTop::xDecodeFollowSliceOfPicture( InputNALUnit nalu )
1136{
1137  // decode following segment
1138    TDecTop* dec = xGetDecoder( nalu ); 
1139    dec->activatePSsAndInitPicOrSlice( NULL );
1140    dec->decodeSliceSegment          ( nalu );
1141}
1142
1143Void TAppDecTop::xFinalizePreviousPictures( Bool sliceIsFirstOfNewAU )
1144{
1145  Bool curPicIsLastInAu = sliceIsFirstOfNewAU && (m_curPic != NULL);
1146  // When slice belongs to new picture, finalize current picture.
1147  if( m_curPic != NULL )
1148  {
1149    xFinalizePic( curPicIsLastInAu );
1150
1151    if (m_curPic->isIrap() )
1152    {
1153      m_noRaslOutputFlagAssocIrap[ m_curPic->getLayerId() ] = m_curPic->getNoRaslOutputFlag();
1154    }
1155
1156    m_tDecTop[ xGetDecoderIdx( m_curPic->getLayerId() )]->finalizePic();
1157    m_curPic->getPicYuvRec()->extendPicBorder(); 
1158    m_newVpsActivatedbyCurPic = false;
1159  }
1160
1161  // When slice belongs to new AU, finalize current AU.
1162  if ( curPicIsLastInAu && !m_curAu.empty() )
1163  {
1164    xFinalizeAU( );
1165    m_newVpsActivatedbyCurAu = false;
1166    m_curAu.clear();
1167  }
1168  m_curPic = NULL;
1169}
1170
1171
1172Void TAppDecTop::xFinalizePic(Bool curPicIsLastInAu )
1173{
1174  if ( m_curPic != NULL )
1175  {
1176    m_tDecTop[ xGetDecoderIdx(m_curPic->getLayerId() ) ]->executeLoopFilters( ); // 8.7
1177
1178    // Invoke Claus 8 and F.8 decoding process for a picture (only parts after POC derivation )
1179    xPicDecoding(FINALIZE_PIC, curPicIsLastInAu );
1180
1181    if( m_decProcCvsg == CLAUSE_8 )
1182    {
1183      xC523PicDecMarkAddBumpAndStor    ( );
1184    }
1185    else if ( m_decProcCvsg == ANNEX_F )
1186    {
1187      xF13523PicDecMarkAddBumpAndStor  ( curPicIsLastInAu );
1188    }
1189  }
1190}
1191
1192Void TAppDecTop::xFinalizeAU()
1193{
1194#if NH_3D
1195  if ( !m_curAu.empty())
1196  {
1197    for (TComList<TComPic*>::iterator it = m_curAu.begin(); it != m_curAu.end(); it++)
1198    {
1199      TComPic* pic = (*it);
1200      if ( !pic->getHasGeneratedRefPics() )
1201      {
1202        pic->compressMotion(1);
1203      }
1204    }
1205  }
1206#endif
1207}
1208
1209
1210Void TAppDecTop::xF811GeneralDecProc( InputNALUnit nalu )
1211{
1212  ////////////////////////////////////////////////////////////////////////////////
1213  // F.8.1 General decoding process
1214  ////////////////////////////////////////////////////////////////////////////////
1215
1216  // The following applies at the beginning of decoding a CVSG, after activating the VPS RBSP that is active for
1217  // the entire CVSG and before decoding any VCL NAL units of the CVSG:
1218
1219  if ( !m_vps->getVpsExtensionFlag() )
1220  {
1221    //-   If vps_extension( ) is not present in the active VPS or a decoding process specified in this annex is not in use,
1222    //   clause 8.1.2 is invoked with the CVSG as input.
1223    x812CvsgDecodingProcess( xGetDecoderIdx( nalu.m_nuhLayerId ) );
1224    m_decProcCvsg = CLAUSE_8;
1225  }
1226  else
1227  {
1228    // - Otherwise (vps_extension( ) is present in the active VPS and a decoding process specified in this annex is in use),
1229    xF812CvsgDecodingProcess( xGetDecoderIdx( nalu.m_nuhLayerId ) );
1230    xF13521InitDpb();
1231    m_decProcCvsg = ANNEX_F;
1232  }
1233
1234  if ( m_printVpsInfo  && ( m_decProcCvsg == ANNEX_F ) )
1235  {
1236    m_vps->printScalabilityId();
1237    m_vps->printLayerDependencies();
1238    m_vps->printLayerSets();
1239    m_vps->printPTL();
1240  }
1241}
1242
1243Void   TAppDecTop::xPicDecoding( DecProcPart curPart, Bool picPosInAuIndication )
1244{
1245  if ( m_decProcCvsg == CLAUSE_8 )
1246  {
1247    // F.8.1.1 -> 8.1.2 -> 8.1.3
1248    x813decProcForCodPicWithLIdZero  ( curPart );
1249  }
1250  else if ( m_decProcCvsg == ANNEX_F )
1251  {
1252    if ( m_targetOptLayerSetIdx == 0  )
1253    {
1254      // F.8.1.1 -> F.8.1.2 -> 8.1.3
1255      x813decProcForCodPicWithLIdZero  ( curPart );
1256    }
1257    else
1258    {
1259      // F.8.1.1 -> F.8.1.2 -> F.8.1.3
1260      xF813ComDecProcForACodedPic( curPart, picPosInAuIndication );
1261    }
1262  }
1263  else
1264  {
1265    assert( false );
1266  }
1267}
1268
1269Void TAppDecTop::x812CvsgDecodingProcess( Int decIdx )
1270{
1271  ///////////////////////////////////////////////////////////////////////////////////////
1272  //  8.1.2 CVSG decoding process
1273  ///////////////////////////////////////////////////////////////////////////////////////
1274
1275  // The layer identifier list TargetDecLayerIdList, which specifies the list of nuh_layer_id values,
1276  // in increasing order of nuh_layer_id values, of the NAL units to be decoded, is specified as follows:
1277
1278  Bool externalMeansToSetTargetDecLayerIdList = !m_targetDecLayerIdSetFileEmpty;
1279
1280  if ( externalMeansToSetTargetDecLayerIdList )
1281  {
1282    // - If some external means, not specified in this Specification, is available to set TargetDecLayerIdList,
1283    //   TargetDecLayerIdList is set by the external means.
1284    assert( !m_targetDecLayerIdSet.empty() ); // Already done when parsing cfg ile
1285  }
1286  else
1287  {
1288    //-  Otherwise, TargetDecLayerIdList contains only one nuh_layer_id value that is equal to 0.
1289    m_targetDecLayerIdSet.clear();
1290    m_targetDecLayerIdSet.push_back( 0 );
1291  }
1292
1293  // The variable HighestTid, which identifies the highest temporal sub-layer to be decoded, is specified as follows:
1294  Bool externalMeansSetHighestTid = ( m_iMaxTemporalLayer != -1 );
1295  if ( externalMeansSetHighestTid )
1296  {
1297    //- If some external means, not specified in this Specification, is available to set HighestTid,
1298    //  HighestTid is set by the external means.
1299    m_highestTid = m_iMaxTemporalLayer;
1300  }
1301  else
1302  {
1303    //-  Otherwise, HighestTid is set equal to sps_max_sub_layers_minus1.
1304    m_highestTid = m_sps->getSpsMaxSubLayersMinus1();
1305  }
1306
1307  //The variable SubPicHrdFlag is specified as follows:
1308  //- If the decoding process is invoked in a bitstream conformance test as specified in clause C.1, SubPicHrdFlag is set as specified in clause C.1.
1309  //- Otherwise, SubPicHrdFlag is set equal to ( SubPicHrdPreferredFlag  &&  sub_pic_hrd_params_present_flag ).
1310
1311  // The sub-bitstream extraction process as specified in clause 10 is applied with the CVSG, HighestTid and TargetDecLayerIdList as inputs, and the output is assigned to a bitstream referred to as BitstreamToDecode.
1312}
1313
1314Void TAppDecTop::x813decProcForCodPicWithLIdZero( DecProcPart curPart )
1315{
1316  ////////////////////////////////////////////////////////////////////////////////
1317  // 8.1.3 Decoding process for a coded picture with nuh_layer_id equal to 0.
1318  ////////////////////////////////////////////////////////////////////////////////
1319
1320  if ( curPart == START_PIC )
1321  {
1322    Int nuhLayerId = m_curPic->getLayerId();
1323
1324    if ( ( m_curPic->isBla() && m_curPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP ) || m_curPic->isCra() )
1325    {
1326      // Not needed.
1327      // When the current picture is a BLA picture that has nal_unit_type equal to BLA_W_LP or is a CRA picture, the following applies:
1328      // -  If some external means not specified in this Specification is available to set the variable UseAltCpbParamsFlag to a value, UseAltCpbParamsFlag is set equal to the value provided by the external means.
1329      // -  Otherwise, the value of UseAltCpbParamsFlag is set equal to 0.
1330    }
1331
1332    if ( m_curPic->isIrap() )
1333    {
1334      // When the current picture is an IRAP picture, the following applies:
1335      if ( m_curPic->isIdr() || m_curPic->isBla() || m_curPic->getDecodingOrder() == 0 || m_eosInLayer[ nuhLayerId ] )
1336      {
1337        // -  If the current picture is an IDR picture, a BLA picture, the first picture in the bitstream in decoding order,
1338        //    or the first picture that follows an end of sequence NAL unit in decoding order, the variable NoRaslOutputFlag
1339        //    is set equal to 1.
1340
1341        m_curPic->setNoRaslOutputFlag( true );
1342      }
1343      else if ( m_handleCraAsBlaFlagSetByExtMeans )
1344      {
1345        // -  Otherwise, if some external means not specified in this Specification is available to set the variable HandleCraAsBlaFlag
1346        //    to a value for the current picture, the variable HandleCraAsBlaFlag is set equal to the value provided by
1347        //    the external means and the variable NoRaslOutputFlag is set equal to HandleCraAsBlaFlag.
1348
1349        m_curPic->setNoRaslOutputFlag( m_handleCraAsBlaFlag );
1350      }
1351      else
1352      {
1353        // -  Otherwise, the variable HandleCraAsBlaFlag is set equal to 0 and the variable NoRaslOutputFlag is set equal to 0.
1354        m_handleCraAsBlaFlag = false;
1355        m_curPic->setNoRaslOutputFlag( false );
1356      }
1357    }
1358
1359    m_decProcPocAndRps = CLAUSE_8;
1360  }
1361  else if ( curPart == FINALIZE_PIC )
1362  {
1363    // -  PicOutputFlag is set as follows:
1364    if (m_curPic->isRasl() && m_noRaslOutputFlagAssocIrap[ m_curPic->getLayerId() ] )
1365    {
1366      // -  If the current picture is a RASL picture and NoRaslOutputFlag of the associated IRAP picture is equal to 1,
1367      //    PicOutputFlag is set equal to 0.
1368      m_curPic->setPicOutputFlag( false );
1369    }
1370    else
1371    {
1372      // -  Otherwise, PicOutputFlag is set equal to pic_output_flag.
1373      m_curPic->setPicOutputFlag( m_curPic->getSlice(0)->getPicOutputFlag() );
1374    }
1375
1376    // 4.  After all slices of the current picture have been decoded, the decoded picture is marked as "used for short-term reference".
1377    m_curPic->markAsUsedForShortTermReference();
1378  }
1379}
1380
1381// F.8.1.2
1382Void TAppDecTop::xF812CvsgDecodingProcess( Int decIdx )
1383{
1384  ///////////////////////////////////////////////////////////////////////////////////////
1385  //  F.8.1.2 CVSG decoding process
1386 ///////////////////////////////////////////////////////////////////////////////////////
1387
1388  // The variable TargetOlsIdx, which specifies the index to the list of the OLSs
1389  // specified by the VPS, of the target OLS, is specified as follows:
1390
1391
1392  // If some external means, not specified in this Specification, is available to set
1393  // TargetOlsIdx, TargetOlsIdx is set by the external means.
1394
1395  // For this decoder the TargetOlsIdx is always set by external means:
1396  // When m_targetOptLayerSetIdx is equal to -1,  TargetOlsIdx is set to getVpsNumLayerSetsMinus1 (which has already been done in TDecTop, since needed there for parsing)
1397  // Otherwise m_targetOptLayerSetIdx is used directly.
1398
1399  if ( m_targetOptLayerSetIdx == -1 )
1400  {
1401    m_targetOptLayerSetIdx = m_tDecTop[decIdx]->getTargetOlsIdx();
1402  }
1403  else
1404  {
1405    assert( m_tDecTop[decIdx]->getTargetOlsIdx() == m_targetOptLayerSetIdx );
1406  }
1407
1408  m_targetDecLayerSetIdx = m_vps->olsIdxToLsIdx( m_targetOptLayerSetIdx );
1409  m_targetDecLayerIdSet  = m_vps->getTargetDecLayerIdList( m_targetOptLayerSetIdx );
1410
1411
1412  if ( m_targetOptLayerSetIdx < 0 || m_targetOptLayerSetIdx >= m_vps->getNumOutputLayerSets() )
1413  {
1414    fprintf(stderr, "\ntarget output layer set index must be in the range of 0 to %d, inclusive \n", m_vps->getNumOutputLayerSets() - 1 );
1415    exit(EXIT_FAILURE);
1416  }
1417
1418  if ( !m_vps->getVpsBaseLayerAvailableFlag() )
1419  {
1420    if(  m_targetDecLayerSetIdx < m_vps->getFirstAddLayerSetIdx() || m_targetDecLayerSetIdx > m_vps->getLastAddLayerSetIdx() )
1421    {
1422      // When vps_base_layer_available_flag is equal to 0, OlsIdxToLsIdx[ TargetOlsIdx ] shall be in the range of FirstAddLayerSetIdx to LastAddLayerSetIdx, inclusive.
1423      fprintf(stderr, "\nvps_base_layer_available_flag is equal to 0, OlsIdxToLsIdx[ TargetOlsIdx ] shall be in the range of %d to %d, inclusive \n", m_vps->getFirstAddLayerSetIdx(), m_vps->getLastAddLayerSetIdx() );
1424      exit(EXIT_FAILURE);
1425    }
1426  }
1427
1428  if ( !m_vps->getVpsBaseLayerInternalFlag() && m_targetOptLayerSetIdx <= 0 )
1429  {
1430    // When vps_base_layer_internal_flag is equal to 0, TargetOlsIdx shall be greater than 0.
1431    fprintf(stderr, "\nvps_base_layer_internal_flag is equal to 0, TargetOlsIdx shall be greater than 0.\n" );
1432    exit(EXIT_FAILURE);
1433  }
1434
1435  // The variable HighestTid, which identifies the highest temporal sub-layer to be decoded,
1436  // is specified as follows:
1437
1438  Bool externalMeansSetHighestTid = ( m_iMaxTemporalLayer != -1 );
1439  if ( externalMeansSetHighestTid )
1440  {
1441    m_highestTid = m_iMaxTemporalLayer;
1442  }
1443  else
1444  {
1445    m_highestTid = m_sps->getSpsMaxSubLayersMinus1();
1446  }
1447
1448  // The variable SubPicHrdPreferredFlag is either specified by external means, or when not specified by external means, set equal to 0.
1449  // The variable SubPicHrdFlag is specified as follows:
1450  // -  If the decoding process is invoked in a bitstream conformance test as specified in clause F.13.1, SubPicHrdFlag is set as specified in clause F.13.1.
1451  // -  Otherwise, SubPicHrdFlag is set equal to ( SubPicHrdPreferredFlag  &&  sub_pic_hrd_params_present_flag ), where sub_pic_hrd_params_present_flag is found in any hrd_parameters( ) syntax structure that applies to at least one bitstream partition of the output layer set idenified by TargetOlsIdx.
1452  // -  TBD in case that needed some when.
1453
1454  // A bitstream to be decoded, BitstreamToDecode, is specified as follows:
1455  //   - Extraction is done in xExtractAndRewrite();
1456
1457  //   - SmallestLayerId is already derived in  TDecTop:: initFromActiveVps, since required for SH parsing.
1458  m_smallestLayerId = m_tDecTop[decIdx]->getSmallestLayerId();
1459
1460  // When vps_base_layer_internal_flag is equal to 0, vps_base_layer_available_flag is equal to 1,
1461  // and TargetDecLayerSetIdx is in the range of 0 to vps_num_layer_sets_minus1, inclusive,
1462  // the following applies:
1463  if ( !m_vps->getVpsBaseLayerInternalFlag() && m_vps->getVpsBaseLayerAvailableFlag() &&
1464       ( m_targetDecLayerSetIdx >= 0 && m_targetDecLayerSetIdx <= m_vps->getVpsNumLayerSetsMinus1() ) )
1465  {
1466
1467    // TBD: The size of the sub-DPB for the layer with nuh_layer_id equal to 0 is set equal to 1.
1468
1469    // TBD: The values of pic_width_in_luma_samples, pic_height_in_luma_samples, chroma_format_idc,
1470    // separate_colour_plane_flag, bit_depth_luma_minus8, bit_depth_chroma_minus8, conf_win_left_offset,
1471    // conf_win_right_offset, conf_win_top_offset, and conf_win_bottom_offset for decoded pictures
1472    // with nuh_layer_id equal to 0 are set equal to the values of pic_width_vps_in_luma_samples,
1473    // pic_height_vps_in_luma_samples, chroma_format_vps_idc, separate_colour_plane_vps_flag,
1474    // bit_depth_vps_luma_minus8, bit_depth_vps_chroma_minus8, conf_win_vps_left_offset,
1475    // conf_win_vps_right_offset, conf_win_vps_top_offset, and conf_win_vps_bottom_offset respectively,
1476    // of the vps_rep_format_idx[ 0 ]-th rep_format( ) syntax structure in the active VPS.
1477
1478    // The variable BaseLayerOutputFlag is set equal to ( TargetOptLayerIdList[ 0 ]  = =  0 ).
1479
1480    m_baseLayerOutputFlag = ( m_vps->getTargetOptLayerIdList( m_targetOptLayerSetIdx )[ 0 ] == 0 );
1481
1482    // NOTE - The BaseLayerOutputFlag for each access unit is to be sent by an external means to
1483    // the base layer decoder for controlling the output of base layer decoded pictures.
1484    // BaseLayerOutputFlag equal to 1 indicates that the base layer is an output layer.
1485    // BaseLayerOutputFlag equal to 0 indicates that the base layer is not an output layer.
1486
1487    // The variable LayerInitializedFlag[ i ] is set equal to 0 for all values of i from 0
1488    // to vps_max_layer_id, inclusive, and the variable FirstPicInLayerDecodedFlag[ i ] is set
1489    // equal to 0 for all values of i from 0 to vps_max_layer_id, inclusive.
1490
1491    for (Int i = 0; i <= m_vps->getVpsMaxLayerId(); i++ )
1492    {
1493      m_layerInitilizedFlag       [ i ] = false;
1494      m_firstPicInLayerDecodedFlag[ i ] = false;
1495    }
1496  }
1497}
1498
1499Void TAppDecTop::xC522OutputAndRemOfPicsFromDpb( )
1500{
1501  ////////////////////////////////////////////////////////////////////////////////
1502  // C.5.2.2 Output and removal of pictures from the DPB
1503  ////////////////////////////////////////////////////////////////////////////////
1504
1505//  The output and removal of pictures from the DPB before the decoding of the current picture
1506//  (but after parsing the slice header of the first slice of the current picture) happens instantaneously
1507//  when the first decoding unit of the access unit containing the current picture is removed from the CPB and proceeds as follows:
1508//  - The decoding process for RPS as specified in clause 8.3.2 is invoked.
1509
1510  Int nuhLayerId = m_curPic->getLayerId();
1511  const TComSPS* sps = m_curPic->getSlice(0)->getSPS();
1512  assert( nuhLayerId == 0 );
1513
1514  if( ( m_curPic->isIrap() && m_curPic->getNoRaslOutputFlag() == 1) && m_curPic->getDecodingOrder() != 0 )
1515  {
1516    // - If the current picture is an IRAP picture with NoRaslOutputFlag equal to 1 that is not picture 0,
1517    //   the following ordered steps are applied:
1518
1519    // 1.  The variable NoOutputOfPriorPicsFlag is derived for the decoder under test as follows:
1520    Int noOutputOfPriorPicsFlag;
1521    if( m_curPic->isCra() )
1522    {
1523      //-  If the current picture is a CRA picture, NoOutputOfPriorPicsFlag is set equal to 1
1524      // (regardless of the value of no_output_of_prior_pics_flag).
1525      noOutputOfPriorPicsFlag = true;
1526    }
1527    else if ( 0  )
1528    {
1529      // TBD
1530      //- Otherwise, if the value of pic_width_in_luma_samples, pic_height_in_luma_samples, chroma_format_idc,
1531      //  separate_colour_plane_flag, bit_depth_luma_minus8, bit_depth_chroma_minus8 or sps_max_dec_pic_buffering_minus1[ HighestTid ]
1532      //  derived from the active SPS is different from the value of pic_width_in_luma_samples, pic_height_in_luma_samples,
1533      //  chroma_format_idc, separate_colour_plane_flag, bit_depth_luma_minus8, bit_depth_chroma_minus8 or
1534      //  sps_max_dec_pic_buffering_minus1[ HighestTid ], respectively, derived from the SPS active for the preceding picture,
1535      //  NoOutputOfPriorPicsFlag may (but should not) be set to 1 by the decoder under test,
1536      //  regardless of the value of no_output_of_prior_pics_flag.
1537      //     NOTE - Although setting NoOutputOfPriorPicsFlag equal to no_output_of_prior_pics_flag is preferred under these conditions,
1538      //            the decoder under test is allowed to set NoOutputOfPriorPicsFlag to 1 in this case.
1539    }
1540    else
1541    {
1542      noOutputOfPriorPicsFlag = m_curPic->getSlice(0)->getNoOutputPriorPicsFlag();
1543    }
1544    //  2.  The value of NoOutputOfPriorPicsFlag derived for the decoder under test is applied for the HRD as follows:
1545    if (noOutputOfPriorPicsFlag)
1546    {
1547      //-  If NoOutputOfPriorPicsFlag is equal to 1, all picture storage buffers in the DPB are emptied
1548      //   without output of the pictures they contain and the DPB fullness is set equal to 0.
1549      m_dpb.emptySubDpb( nuhLayerId );
1550    }
1551    else if (!noOutputOfPriorPicsFlag)
1552    {
1553      //  -  Otherwise (NoOutputOfPriorPicsFlag is equal to 0), all picture storage buffers containing a picture that
1554      //     is marked as "not needed for output" and "unused for reference" are emptied (without output) and all non-empty
1555      //     picture storage buffers in the DPB are emptied by repeatedly invoking the "bumping" process specified in clause C.5.2.4
1556      //     and the DPB fullness is set equal to 0.
1557
1558      m_dpb.emptySubDpbNotNeedForOutputAndUnusedForRef( nuhLayerId );
1559      while( m_dpb.getSubDpb( nuhLayerId, false  )->size() != 0 )
1560      {
1561        xC524Bumping();
1562      }
1563    }
1564  }
1565  else if ( !( m_curPic->isIrap() && m_curPic->getNoRaslOutputFlag() == 0 ) )
1566  {
1567    //  -  Otherwise (the current picture is not an IRAP picture with NoRaslOutputFlag equal to 1),
1568    //     all picture storage buffers containing a picture which are marked as "not needed for output" and "unused for reference"
1569    //     are emptied (without output). For each picture storage buffer that is emptied, the DPB fullness is decremented by one.
1570
1571    m_dpb.emptySubDpbNotNeedForOutputAndUnusedForRef( nuhLayerId );
1572
1573    Bool repeat = true;
1574
1575    while( repeat )
1576    {
1577      TComSubDpb* dpb = m_dpb.getSubDpb( nuhLayerId, false );
1578      TComList<TComPic*> picsMarkedForOutput = dpb->getPicsMarkedNeedForOutput();
1579      // When one or more of the following conditions are true, the "bumping" process specified in clause C.5.2.4
1580      // is invoked repeatedly while further decrementing the DPB fullness by one for each additional picture storage buffer that
1581      // is emptied, until none of the following conditions are true:
1582
1583      // -  The number of pictures in the DPB that are marked as "needed for output" is greater
1584      //    than sps_max_num_reorder_pics[ HighestTid ].
1585      Bool cond1 = ( picsMarkedForOutput.size() > sps->getSpsMaxNumReorderPics( m_highestTid ) );
1586
1587      //  -  sps_max_latency_increase_plus1[ HighestTid ] is not equal to 0 and there is at least one picture in the DPB
1588      //     that is marked as "needed for output" for which the associated variable PicLatencyCount is greater than or equal
1589      //     to SpsMaxLatencyPictures[ HighestTid ].
1590      Bool anyPicWithGtOrEqLatencyCnt = false;
1591      for (TComList<TComPic*>::iterator itP = picsMarkedForOutput.begin(); itP != picsMarkedForOutput.end() && !anyPicWithGtOrEqLatencyCnt; itP++ )
1592      {
1593        anyPicWithGtOrEqLatencyCnt = anyPicWithGtOrEqLatencyCnt || ( (*itP)->getPicLatencyCount() >= sps->getSpsMaxLatencyPictures( m_highestTid ));
1594      }
1595      Bool cond2 = ( sps->getSpsMaxLatencyIncreasePlus1( m_highestTid ) != 0 ) && anyPicWithGtOrEqLatencyCnt;
1596
1597      //  -  The number of pictures in the DPB is greater than or equal to sps_max_dec_pic_buffering_minus1[ HighestTid ] + 1.
1598      Bool cond3 = ( dpb->size() >= ( sps->getSpsMaxDecPicBufferingMinus1( m_highestTid ) + 1 ));
1599
1600      if ( cond1 || cond2 || cond3 )
1601      {
1602        xC524Bumping();
1603      }
1604      else
1605      {
1606        repeat = false;
1607      }
1608    }
1609  }
1610}
1611
1612Void TAppDecTop::xC523PicDecMarkAddBumpAndStor()
1613{
1614  ///////////////////////////////////////////////////////////////////////////////////////
1615  // C.5.2.3 Picture decoding, marking, additional bumping and storage 
1616  ///////////////////////////////////////////////////////////////////////////////////////
1617 
1618  Int nuhLayerId = m_curPic->getLayerId();
1619  const TComSPS* sps = m_curPic->getSlice(0)->getSPS();
1620  // The processes specified in this clause happen instantaneously when the last decoding unit of access unit n containing the
1621  // current picture is removed from the CPB.
1622  if (m_curPic->getPicOutputFlag() )
1623  {
1624    // When the current picture has PicOutputFlag equal to 1, for each picture in the DPB that is marked as "needed for output"
1625    // and follows the current picture in output order, the associated variable PicLatencyCount is set equal to PicLatencyCount + 1.
1626    TComSubDpb* dpb = m_dpb.getSubDpb( nuhLayerId, false);
1627
1628    for (TComSubDpb::iterator itP = dpb->begin(); itP != dpb->end(); itP++)
1629    {
1630      TComPic* pic = (*itP);
1631      if ( pic->getOutputMark() && pic->getPOC() > m_curPic->getPOC() )
1632      {
1633        pic->setPicLatencyCount( pic->getPicLatencyCount() + 1 );
1634      }
1635    }
1636  }
1637
1638  // The current picture is considered as decoded after the last decoding unit of the picture is decoded.
1639  // The current decoded picture is stored in an empty picture storage buffer in the DPB and the following applies:
1640  m_dpb.addNewPic( m_curPic );
1641
1642
1643  if (m_curPic->getPicOutputFlag())
1644  {
1645    // - If the current decoded picture has PicOutputFlag equal to 1, it is marked as "needed for output" and its associated
1646    //   variable PicLatencyCount is set equal to 0.
1647    m_curPic->setOutputMark( true );
1648    m_curPic->setPicLatencyCount( 0 );
1649  }
1650  else if (!m_curPic->getPicOutputFlag() )
1651  {
1652    // - Otherwise (the current decoded picture has PicOutputFlag equal to 0), it is marked as "not needed for output".
1653    m_curPic->setOutputMark( false );
1654  }
1655
1656  // The current decoded picture is marked as "used for short-term reference".
1657  m_curPic->markAsUsedForShortTermReference();
1658
1659  Bool repeat = true;
1660
1661  while( repeat )
1662  {
1663    TComSubDpb* dpb = m_dpb.getSubDpb( nuhLayerId, false );
1664    TComList<TComPic*> picsMarkedForOutput = dpb->getPicsMarkedNeedForOutput();
1665
1666    // When one or more of the following conditions are true, the "bumping" process specified in clause C.5.2.4
1667    // is invoked repeatedly until none of the following conditions are true:
1668
1669    // - The number of pictures in the DPB that are marked as "needed for output" is greater than sps_max_num_reorder_pics[ HighestTid ].
1670    Bool cond1 = ( picsMarkedForOutput.size() > sps->getSpsMaxNumReorderPics( m_highestTid ) );
1671
1672    // - sps_max_latency_increase_plus1[ HighestTid ] is not equal to 0 and there is at least one picture in the DPB that is marked
1673    //   as "needed for output" for which the associated variable PicLatencyCount that is greater than or equal to
1674    //   SpsMaxLatencyPictures[ HighestTid ].
1675    Bool anyPicWithGtOrEqLatencyCnt = false;
1676    for (TComList<TComPic*>::iterator itP = picsMarkedForOutput.begin(); itP != picsMarkedForOutput.end() && !anyPicWithGtOrEqLatencyCnt; itP++ )
1677    {
1678      anyPicWithGtOrEqLatencyCnt = anyPicWithGtOrEqLatencyCnt || ( (*itP)->getPicLatencyCount() >= sps->getSpsMaxLatencyPictures( m_highestTid ));
1679    }
1680    Bool cond2 = ( sps->getSpsMaxLatencyIncreasePlus1( m_highestTid ) != 0 ) && anyPicWithGtOrEqLatencyCnt;
1681
1682    if ( cond1 || cond2 )
1683    {
1684      xC524Bumping();
1685    }
1686    else
1687    {
1688      repeat = false;
1689    }
1690  }
1691}
1692
1693Void TAppDecTop::xC524Bumping( )
1694{
1695  ////////////////////////////////////////////////////////////////////////////////
1696  // C.5.2.4 "Bumping" process
1697  ////////////////////////////////////////////////////////////////////////////////
1698
1699  // The "bumping" process consists of the following ordered steps:
1700
1701  // 1.  The picture that is first for output is selected as the one having the smallest value of PicOrderCntVal of all pictures
1702  //     in the DPB marked as "needed for output".
1703  TComPic* pic = *(m_dpb.getSubDpb( 0, false)->getPicsMarkedNeedForOutput().begin()); // pics are sorted, so take first
1704
1705  // 2.  The picture is cropped, using the conformance cropping window specified in the active SPS for the picture,
1706  //     the cropped picture is output, and the picture is marked as "not needed for output".
1707  xCropAndOutput( pic );
1708  pic->setOutputMark( false );
1709
1710  //3.  When the picture storage buffer that included the picture that was cropped and output contains a picture marked
1711  //    as "unused for reference", the picture storage buffer is emptied.
1712  if (pic->getMarkedUnUsedForReference() )
1713  {
1714    m_dpb.removePic( pic );
1715  }
1716
1717  // NOTE - For any two pictures picA and picB that belong to the same CVS and are output by the "bumping process", when picA
1718  //        is output earlier than picB, the value of PicOrderCntVal of picA is less than the value of PicOrderCntVal of picB.
1719}
1720
1721
1722Void TAppDecTop::xF813ComDecProcForACodedPic( DecProcPart curPart, Bool picPosInAuIndication )
1723{
1724  ////////////////////////////////////////////////////////////////////////////////
1725  // F.8.1.3  Common decoding process for a coded picture
1726  ////////////////////////////////////////////////////////////////////////////////
1727
1728  // PicPosInAuIndication is interpreted based on curPart.
1729  // - If curPart is equal to START_PIC               , it indicates whether the current pic is the first in the current AU.
1730  // - Otherwise (if curPart is equal to FINALIZE_PIC), it indicates whether the current pic is the last  in the current AU.
1731
1732  if (curPart == START_PIC )
1733  {
1734    Bool curPicIsFirstInAu = picPosInAuIndication;
1735
1736    Int nuhLayerId = m_curPic->getLayerId();
1737
1738    if ( !m_vps->getVpsBaseLayerInternalFlag() && m_vps->getVpsBaseLayerAvailableFlag() &&
1739      ( m_targetDecLayerSetIdx >= 0 && m_targetDecLayerSetIdx <= m_vps->getVpsNumLayerSetsMinus1() ) &&
1740      m_curPic->getSlice(0)->getTemporalId() <= m_vps->getSubLayersVpsMaxMinus1( 0 ) && curPicIsFirstInAu )
1741    {
1742      // When vps_base_layer_internal_flag is equal to 0, vps_base_layer_available_flag is equal to 1,
1743      // TargetDecLayerSetIdx is in the range of 0 to vps_num_layer_sets_minus1, inclusive,
1744      // TemporalId is less than or equal to sub_layers_vps_max_minus1[ 0 ], and the current picture
1745      // is the first coded picture of an access unit, clause F.8.1.8 is invoked prior to decoding the current picture.
1746
1747      assert( false ); //TBD
1748    }
1749
1750    //  When the current picture is an IRAP picture, the variable HandleCraAsBlaFlag is derived as specified in the following:
1751    if ( m_curPic->isIrap() )
1752    {
1753      if ( m_handleCraAsBlaFlagSetByExtMeans )
1754      {
1755        // If some external means not specified in this Specification is available to set the variable HandleCraAsBlaFlag to
1756        // a value for the current picture, the variable HandleCraAsBlaFlag is set equal to the value provided by the external means.
1757      }
1758      else
1759      {
1760        // - Otherwise, the variable HandleCraAsBlaFlag is set equal to 0.
1761        m_handleCraAsBlaFlag = false;
1762      }
1763    }
1764
1765    if ( m_curPic->isIrap() && nuhLayerId == m_smallestLayerId )
1766    {
1767      // When the current picture is an IRAP picture and has nuh_layer_id equal to SmallestLayerId, the following applies:
1768      // The variable NoClrasOutputFlag is specified as follows:
1769
1770      if( m_firstSliceInBitstream )
1771      {
1772        //  - If the current picture is the first picture in the bitstream, NoClrasOutputFlag is set equal to 1.
1773        m_curPic->setNoClrasOutputFlag(true );
1774      }
1775      else if( m_eosInLayer[ 0 ] || m_eosInLayer[ nuhLayerId ] )
1776      {
1777        //  - Otherwise, if the current picture is included in the first access unit that follows an access unit
1778        //    including an end of sequence NAL unit with nuh_layer_id equal to SmallestLayerId or 0 in decoding order,
1779        //    NoClrasOutputFlag is set equal to 1.
1780
1781        m_curPic->setNoClrasOutputFlag(true );
1782      }
1783      else if ( m_curPic->isBla() || (m_curPic->isCra() && m_handleCraAsBlaFlag ))
1784      {
1785        //  - Otherwise, if the current picture is a BLA picture or a CRA picture with HandleCraAsBlaFlag equal to 1,
1786        //    NoClrasOutputFlag is set equal to 1.
1787        m_curPic->setNoClrasOutputFlag(true );
1788      }
1789      else if ( m_curPic->isIdr() && m_curPic->getSlice(0)->getCrossLayerBlaFlag() )
1790      {
1791        //  - Otherwise, if the current picture is an IDR picture with cross_layer_bla_flag is equal to 1,
1792        //    NoClrasOutputFlag is set equal to 1.
1793        m_curPic->setNoClrasOutputFlag(true );
1794      }
1795      else if ( m_noClrasOutputFlagSetByExtMeans )
1796      {
1797        m_curPic->setNoClrasOutputFlag( m_noClrasOutputFlag );
1798        //  - Otherwise, if some external means, not specified in this Specification, is available to set NoClrasOutputFlag,
1799        //    NoClrasOutputFlag is set by the external means.
1800      }
1801      else
1802      {
1803        //  - Otherwise, NoClrasOutputFlag is set equal to 0.
1804        m_curPic->setNoClrasOutputFlag(false );
1805      }
1806
1807      //-  When NoClrasOutputFlag is equal to 1, the variable LayerInitializedFlag[ i ] is set equal to 0 for all values
1808      //  of i from 0 to vps_max_layer_id, inclusive, and the variable FirstPicInLayerDecodedFlag[ i ] is set equal to 0
1809      //  for all values of i from 0 to vps_max_layer_id, inclusive.
1810      if ( m_curPic->getNoClrasOutputFlag( ) )
1811      {
1812        for (Int i = 0; i <= m_vps->getVpsMaxLayerId(); i++)
1813        {
1814          m_layerInitilizedFlag       [i] = false;
1815          m_firstPicInLayerDecodedFlag[i] = false;
1816        }
1817      }
1818    }
1819
1820    // The variables LayerResetFlag and dolLayerId are derived as follows:
1821    Int dolLayerId = -1;
1822    if ( m_curPic->isIrap() && nuhLayerId > m_smallestLayerId )
1823    {
1824      // - If the current picture is an IRAP picture and has nuh_layer_id nuhLayerId greater than SmallestLayerId,
1825      //   the following applies:
1826
1827      if( m_eosInLayer[ nuhLayerId ] )
1828      {
1829        // -  If the current picture is the first picture, in decoding order, that follows an end of sequence NAL unit with
1830        //    nuh_layer_id equal to nuhLayerId, LayerResetFlag is set equal to 1 and dolLayerId is set equal to the nuh_layer_id
1831        //    value of the current NAL unit.
1832
1833        m_layerResetFlag = true;
1834        dolLayerId = nuhLayerId;
1835      }
1836      else  if( ( m_curPic->isCra() && m_handleCraAsBlaFlag ) ||
1837        (m_curPic->isIdr() && m_curPic->getSlice(0)->getCrossLayerBlaFlag() ) || m_curPic->isBla() )
1838      {
1839        // - Otherwise, if the current picture is a CRA picture with HandleCraAsBlaFlag equal to 1, an IDR picture with
1840        //   cross_layer_bla_flag is equal to 1 or a BLA picture, LayerResetFlag is set equal to 1 and dolLayerId is set
1841        //   equal to the nuh_layer_id value of the current NAL unit.
1842        m_layerResetFlag = true;
1843        dolLayerId = nuhLayerId;
1844      }
1845      else
1846      {
1847        // -   Otherwise, LayerResetFlag is set equal to 0.
1848        m_layerResetFlag = false;
1849      }
1850
1851      // NOTE 1 - An end of sequence NAL unit, a CRA picture with HandleCraAsBlaFlag equal to 1, an IDR picture with
1852      // cross_layer_bla_flag equal to 1, or a BLA picture, each with nuh_layer_id nuhLayerId greater than SmallestLayerId,
1853      // may be present to indicate a discontinuity of the layer with nuh_layer_id equal to nuhLayerId and its predicted layers.
1854
1855      if (m_layerResetFlag )
1856      {
1857        //When LayerResetFlag is equal to 1, the following applies:
1858        //  - The values of LayerInitializedFlag and FirstPicInLayerDecodedFlag are updated as follows:
1859
1860        for( Int i = 0; i < m_vps->getNumPredictedLayers( dolLayerId ); i++ )
1861        {
1862          Int iLayerId = m_vps->getIdPredictedLayer(  dolLayerId , i );
1863          m_layerInitilizedFlag       [ iLayerId ] = false;
1864          m_firstPicInLayerDecodedFlag[ iLayerId ] = false;
1865        }
1866
1867        //  - Each picture that is in the DPB and has nuh_layer_id equal to dolLayerId is marked as "unused for reference".
1868        m_dpb.markSubDpbAsUnusedForReference( dolLayerId );
1869
1870        //  - When NumPredictedLayers[ dolLayerId ] is greater than 0, each picture that is in the DPB and has nuh_layer_id
1871        //    equal to any value of IdPredictedLayer[ dolLayerId ][ i ] for the values of i in the range of 0 to
1872        //    NumPredictedLayers[ dolLayerId ] - 1, inclusive, is marked as "unused for reference".
1873
1874        for( Int i = 0; i <= m_vps->getNumPredictedLayers( dolLayerId )- 1; i++  )
1875        {
1876          m_dpb.markSubDpbAsUnusedForReference( m_vps->getIdPredictedLayer( dolLayerId, i ) );
1877        }
1878      }
1879    }
1880    else
1881    {
1882      // - Otherwise, LayerResetFlag is set equal to 0.
1883      m_layerResetFlag = false;
1884    }
1885
1886    if ( m_curPic->isIrap() )
1887    {
1888      // When the current picture is an IRAP picture, the following applies:
1889
1890      if ( m_curPic->isIdr() || m_curPic->isBla() || m_curPic->getDecodingOrder() == 0 || m_eosInLayer[nuhLayerId] )
1891      {
1892        // - If the current picture with a particular value of nuh_layer_id is an IDR picture, a BLA picture, the first picture
1893        //   with that particular value of nuh_layer_id in the bitstream in decoding order or the first picture with that
1894        //   particular value of nuh_layer_id that follows an end of sequence NAL unit with that particular value of nuh_layer_id
1895        //   in decoding order, the variable NoRaslOutputFlag is set equal to 1.
1896
1897        m_curPic->setNoRaslOutputFlag(  true );
1898      }
1899      else if ( m_layerInitilizedFlag[ nuhLayerId ] == 0 && xAllRefLayersInitilized( nuhLayerId ) )
1900      {
1901        // Otherwise, if LayerInitializedFlag[ nuh_layer_id ] is equal to 0 and LayerInitializedFlag[ refLayerId ] is equal to 1
1902        // for all values of refLayerId equal to IdDirectRefLayer[ nuh_layer_id ][ j ], where j is in the range of 0 to
1903        // NumDirectRefLayers[ nuh_layer_id ] - 1, inclusive, the variable NoRaslOutputFlag is set equal to 1.
1904
1905        m_curPic->setNoRaslOutputFlag(  true );
1906      }
1907      else
1908      {
1909        // - Otherwise, the variable NoRaslOutputFlag is set equal to HandleCraAsBlaFlag.
1910        m_curPic->setNoRaslOutputFlag(  m_handleCraAsBlaFlag );
1911      }
1912    }
1913
1914    // The following applies for the decoding of the current picture:
1915    if ( m_curPic->isIrap() && m_curPic->getNoRaslOutputFlag() && (
1916      ( nuhLayerId == 0 ) ||
1917      ( m_layerInitilizedFlag[ nuhLayerId ] == 0   && m_vps->getNumDirectRefLayers( nuhLayerId ) == 0  ) ||
1918      ( m_layerInitilizedFlag[ nuhLayerId ] == 0 && xAllRefLayersInitilized( nuhLayerId ) )
1919      ) )
1920    {
1921      // When the current picture is an IRAP picture with NoRaslOutputFlag equal to 1 and one of the following conditions is true,
1922      // LayerInitializedFlag[ nuh_layer_id ] is set equal to 1:
1923      // - nuh_layer_id is equal to 0.
1924      // - LayerInitializedFlag[ nuh_layer_id ] is equal to 0 and NumDirectRefLayers[ nuh_layer_id ] is equal to 0.
1925      // - LayerInitializedFlag[ nuh_layer_id ] is equal to 0 and LayerInitializedFlag[ refLayerId ] is equal to 1 for all values of
1926      //   refLayerId equal to IdDirectRefLayer[ nuh_layer_id ][ j ], where j is in the range of 0 to NumDirectRefLayers[ nuh_layer_id ] - 1, inclusive.
1927
1928      m_layerInitilizedFlag[ nuhLayerId ] = true;
1929    }
1930
1931    // The following applies for the decoding of the current picture:
1932
1933    if ( nuhLayerId == 0 )
1934    {
1935      //  If the current picture has nuh_layer_id equal to 0, the decoding process for the current picture takes as inputs the syntax elements
1936      //  and upper-case variables from clause F.7 and the decoding process for a coded picture with nuh_layer_id equal to 0 as specified in clause F.8.1.4
1937      //  is invoked.
1938
1939      xF814decProcForCodPicWithLIdZero( curPart );
1940    }
1941    else
1942    {
1943      Bool decodedPicWithLayerIdZeroProvided = false;
1944      if ( !m_vps->getVpsBaseLayerInternalFlag() && m_vps->getVpsBaseLayerAvailableFlag() &&
1945        ( m_targetDecLayerSetIdx >= 0 && m_targetDecLayerSetIdx <= m_vps->getVpsNumLayerSetsMinus1() ) &&
1946        m_curPic->getSlice(0)->getTemporalId() <= m_vps->getSubLayersVpsMaxMinus1( 0 ) && curPicIsFirstInAu
1947        && decodedPicWithLayerIdZeroProvided )
1948      {
1949        assert( false ); //TBD
1950        //TBD: Hybrid scalability
1951        //  When vps_base_layer_internal_flag is equal to 0, vps_base_layer_available_flag is equal to 1,
1952        //  TargetDecLayerSetIdx is in the range of 0 to vps_num_layer_sets_minus1, inclusive,
1953        //  TemporalId is less than or equal to sub_layers_vps_max_minus1[ 0 ], the current picture
1954        //  is the first coded picture of an access unit, and a decoded picture with nuh_layer_id equal
1955        //  to 0 is provided by external means for the current access unit, clause F.8.1.9 is invoked
1956        //  after the decoding of the slice segment header of the first slice segment, in decoding order,
1957        //  of the current picture, but prior to decoding any slice segment of the first
1958        //  coded picture of the access unit.
1959      }
1960
1961      m_decProcPocAndRps = ANNEX_F;
1962      // For the decoding of the slice segment header of the first slice segment, in decoding order, of the current picture,
1963      // the decoding process for starting the decoding of a coded picture with nuh_layer_id greater than 0 specified in
1964      // clause F.8.1.5 is invoked.  --> This is done in the loop when receiving slices.
1965
1966      // The decoding process is already selected before.
1967    }
1968  }
1969  else if( curPart == FINALIZE_PIC )
1970  {
1971    Bool curPicIsLastInAu = picPosInAuIndication;
1972
1973    if (m_curPic->getLayerId() == 0 )
1974    {
1975      xF814decProcForCodPicWithLIdZero( curPart );
1976    }
1977    else
1978    {
1979      // - After all slices of the current picture have been decoded, the decoding process for ending the decoding of a
1980      //   coded picture with nuh_layer_id greater than 0 specified in clause F.8.1.6 is invoked.
1981      xF816decProcEndDecOfCodPicLIdGrtZero( );
1982    }
1983
1984    TComSlice* slice = m_curPic->getSlice(0);
1985    const TComVPS* vps = slice->getVPS();
1986
1987    if ( curPicIsLastInAu )
1988    {
1989      //When the current picture is the last coded picture in an access unit in BitstreamToDecode, the following steps apply after
1990      // the decoding of the current picture, prior to the decoding of the next picture:
1991
1992      //-  PicOutputFlag is updated as follows:
1993      TComPic* picAtOutputLayer = NULL;
1994      Int outputLayerId = -1;
1995
1996      // Try to find pic at output layer.
1997      if( vps->getAltOutputLayerFlag( m_targetOptLayerSetIdx ) )
1998      {
1999        // When alt_output_layer_flag is equal to 1, the target output layer set can only contain one output layer.
2000        assert( vps->getNumOutputLayersInOutputLayerSet( m_targetOptLayerSetIdx ) == 1 );
2001        outputLayerId = vps->getTargetOptLayerIdList(m_targetOptLayerSetIdx)[0];
2002        picAtOutputLayer = m_curAu.getPic( outputLayerId );
2003      }
2004
2005      if ( vps->getAltOutputLayerFlag( m_targetOptLayerSetIdx ) &&
2006        ( picAtOutputLayer == NULL || (picAtOutputLayer != NULL && !picAtOutputLayer->getPicOutputFlag() ) ) )
2007      {
2008        // If alt_output_layer_flag[ TargetOlsIdx ] is equal to 1 and the current access unit either does
2009        // not contain a picture at the output layer or contains a picture at the output layer that has PicOutputFlag equal to 0:
2010
2011        // - The list nonOutputLayerPictures is set to be the list of the pictures of the access unit with PicOutputFlag equal to 1 and
2012        //   with nuh_layer_id values among the nuh_layer_id values of the reference layers of the output layer.
2013        TComList<TComPic*> nonOutputLayerPictures;
2014        for( TComList<TComPic*>::iterator itP= m_curAu.begin();  itP != m_curAu.end() ; itP++ )
2015        {
2016          TComPic* pic = (*itP);
2017          Bool isRefernceLayerOfOutputLayer = false;
2018          for ( Int i = 0; i < vps->getNumRefLayers( outputLayerId ) && !isRefernceLayerOfOutputLayer; i++ )
2019          {
2020            if ( pic->getLayerId() == vps->getIdRefLayer(outputLayerId, i) )
2021            {
2022              isRefernceLayerOfOutputLayer = true;
2023            }
2024          }
2025
2026          if ( pic->getPicOutputFlag() && isRefernceLayerOfOutputLayer )
2027          {
2028            nonOutputLayerPictures.pushBack( pic );
2029          }
2030        }
2031
2032        if (nonOutputLayerPictures.size() != 0 )
2033        {
2034          // -  When the list nonOutputLayerPictures is not empty,
2035          //    The picture with the highest nuh_layer_id value among the list nonOutputLayerPictures is removed from the list nonOutputLayerPictures.
2036          //    As in AU the picture with the highest layer id is the last
2037          nonOutputLayerPictures.pop_back();
2038        }
2039
2040        //  -  PicOutputFlag for each picture that is included in the list nonOutputLayerPictures is set equal to 0.
2041        for( TComList<TComPic*>::iterator itP= nonOutputLayerPictures.begin();  itP != nonOutputLayerPictures.end() ; itP++ )
2042        {
2043          (*itP)->setPicOutputFlag( false );
2044        }
2045      }
2046      else
2047      {
2048        //Otherwise, PicOutputFlag for pictures that are not included in an output layer is set equal to 0.
2049        for( TComList<TComPic*>::iterator itP= m_curAu.begin();  itP != m_curAu.end() ; itP++ )
2050        {
2051          TComPic* pic = (*itP);
2052
2053          Bool includedInOutputLayer = false;
2054          for (Int i = 0 ; i < vps->getNumOutputLayersInOutputLayerSet( m_targetOptLayerSetIdx ) && !includedInOutputLayer; i++)
2055          {
2056            includedInOutputLayer = ( pic->getLayerId() ==  vps->getTargetOptLayerIdList(m_targetOptLayerSetIdx)[i]);
2057          }
2058
2059          if ( !includedInOutputLayer )
2060          {
2061            pic->setPicOutputFlag( false );
2062          }
2063        }
2064      }
2065
2066      // When vps_base_layer_internal_flag is equal to 0, vps_base_layer_available_flag is equal to 1 and
2067      // TargetDecLayerSetIdx is in the range of 0 to vps_num_layer_sets_minus1, inclusive
2068
2069      if( !vps->getVpsBaseLayerInternalFlag() && vps->getVpsBaseLayerAvailableFlag() && ( m_targetDecLayerSetIdx >= 0 && m_targetDecLayerSetIdx <= vps->getVpsNumLayerSetsMinus1()   ) )
2070      {
2071        if( !m_baseLayerOutputFlag )
2072        {
2073          // Check if base layer is a reference layer of the output layer
2074          // and if the access unit does not contain a picture at any other reference layer of the output layer
2075          Bool baseLayerIsRefOfOutputLayer = false;
2076          Bool auDoesNotContainPicAtAnyOtherRefLayerOfOptLayer = true;
2077          if ( vps->getAltOutputLayerFlag( m_targetOptLayerSetIdx ))
2078          {
2079            assert( outputLayerId >= 0 );
2080
2081            for (Int i = 0; i < vps->getNumRefLayers( outputLayerId ); i++ )
2082            {
2083              Int refLayerId = vps->getIdRefLayer(outputLayerId, i);
2084              if( refLayerId == 0 )
2085              {
2086                baseLayerIsRefOfOutputLayer = true;
2087              }
2088              else
2089              {
2090                if ( m_curAu.getPic( refLayerId ) != NULL )
2091                {
2092                  auDoesNotContainPicAtAnyOtherRefLayerOfOptLayer = false;
2093                }
2094              }
2095            }
2096          }
2097
2098          // If alt_output_layer_flag[ TargetOlsIdx ] is equal to 1, the base layer is a reference layer of the output layer,
2099          // the access unit does not contain a picture at the output layer or contains a picture at the output layer that has
2100          // PicOutputFlag equal to 0, and the access unit does not contain a picture at any other reference layer of the output layer,
2101          // BaseLayerPicOutputFlag is set equal to 1
2102
2103          if ( vps->getAltOutputLayerFlag( m_targetOptLayerSetIdx ) && baseLayerIsRefOfOutputLayer
2104            && ( ( picAtOutputLayer == NULL ) || ( picAtOutputLayer != NULL && !picAtOutputLayer->getPicOutputFlag()  ) )
2105            &&  auDoesNotContainPicAtAnyOtherRefLayerOfOptLayer )
2106          {
2107            m_baseLayerPicOutputFlag = true;
2108          }
2109          else
2110          {
2111            m_baseLayerPicOutputFlag = false;
2112          }
2113        }
2114        else
2115        {
2116          m_baseLayerPicOutputFlag = true;
2117        }
2118
2119        // NOTE 3 - The BaseLayerPicOutputFlag for each access unit is to be sent by an external means
2120        // to the base layer decoder for controlling the output of base layer decoded pictures.
2121        // BaseLayerPicOutputFlag equal to 1 for an access unit specifies that the base layer picture of the access unit is to be output.
2122        // BaseLayerPicOutputFlag equal to 0 for an access unit specifies that the base layer picture of the access unit is not to be output.
2123
2124        //  The sub-DPB for the layer with nuh_layer_id equal to 0 is set to be empty.
2125        m_dpb.emptySubDpb( 0 );
2126      }
2127
2128      // The variable AuOutputFlag that is associated with the current access unit is derived as follows:
2129
2130      // Derive if at least one picture in the current access unit has PicOutputFlag equal to 1
2131      Bool atLeastOnePicInAuWithPicOptFlagOne = false;
2132      for( TComList<TComPic*>::iterator itP= m_curAu.begin();  itP != m_curAu.end() ; itP++ )
2133      {
2134        if ( (*itP)->getPicOutputFlag() )
2135        {
2136          atLeastOnePicInAuWithPicOptFlagOne = true;
2137        }
2138      }
2139
2140      //If at least one picture in the current access unit has PicOutputFlag equal to 1
2141      if ( atLeastOnePicInAuWithPicOptFlagOne )
2142      {
2143        m_auOutputFlag = true;
2144      }
2145      else
2146      {
2147        m_auOutputFlag = false;
2148      }
2149
2150      // The variable PicLatencyCount that is associated with the current access unit is set equal to 0.
2151      m_curAu.setPicLatencyCount( 0 );
2152
2153      if ( m_auOutputFlag )
2154      {
2155        // for each access unit in the DPB
2156        // that has at least one picture marked as "needed for output" and
2157        // follows the current access unit in output order, the associated
2158        // variable PicLatencyCount is set equal to PicLatencyCount + 1.
2159
2160        TComList<TComAu*> aus = m_dpb.getAusHavingPicsMarkedForOutput(); // <-- current AU is actually not yet in DPB, but this does not matter here.
2161
2162        for(TComList<TComAu*>::iterator itA= aus.begin(); ( itA!=aus.end()); itA++)
2163        {
2164          if( m_curAu.getPoc() < (*itA)->getPoc())
2165          {
2166            (*itA)->setPicLatencyCount( (*itA)->getPicLatencyCount() + 1 );
2167          }
2168        }
2169      }
2170    }
2171  }
2172}
2173
2174Void TAppDecTop::xF814decProcForCodPicWithLIdZero( DecProcPart curPart )
2175{
2176  ////////////////////////////////////////////////////////////////////////////////
2177  // F.8.1.4 Decoding process for a coded picture with nuh_layer_id equal to 0
2178  ////////////////////////////////////////////////////////////////////////////////
2179
2180  x813decProcForCodPicWithLIdZero( curPart );
2181
2182  if (curPart == START_PIC )
2183  {
2184    m_decProcPocAndRps = ANNEX_F;
2185  }
2186  else if (curPart == FINALIZE_PIC )
2187  {
2188    if ( !m_firstPicInLayerDecodedFlag[0] )
2189    {
2190      m_firstPicInLayerDecodedFlag[0] = true;
2191    }
2192  }
2193}
2194
2195Void TAppDecTop::xF13521InitDpb()
2196{
2197  ////////////////////////////////////////////////////////////////////////////////
2198  // F.13.5.2.1 General
2199  ////////////////////////////////////////////////////////////////////////////////
2200
2201  const TComDpbSize* dpbSize = m_vps->getDpbSize();
2202  m_maxNumReorderPics       = dpbSize->getMaxVpsNumReorderPics      ( m_targetOptLayerSetIdx, m_highestTid );
2203  m_maxLatencyIncreasePlus1 = dpbSize->getMaxVpsLatencyIncreasePlus1( m_targetOptLayerSetIdx, m_highestTid );
2204  m_maxLatencyValue         = dpbSize->getVpsMaxLatencyPictures     ( m_targetOptLayerSetIdx, m_highestTid );
2205
2206  for(Int i = 0; i < MAX_NUM_LAYER_IDS; i++)
2207  {
2208    m_maxDecPicBufferingMinus1[ i ] = MIN_INT;
2209  }
2210  for( Int i = 0; i <= m_vps->getMaxLayersMinus1(); i++ )
2211  {
2212    Int currLayerId = m_vps->getLayerIdInNuh( i );
2213    for(Int layerIdx = 0 ; layerIdx < m_vps->getNumLayersInIdList( m_targetDecLayerSetIdx); layerIdx++ )
2214    {
2215      if( m_vps->getLayerSetLayerIdList( m_targetDecLayerSetIdx, layerIdx ) == currLayerId )
2216      {
2217        m_maxDecPicBufferingMinus1[currLayerId] = dpbSize->getMaxVpsDecPicBufferingMinus1( m_targetDecLayerSetIdx, layerIdx, m_highestTid );
2218      }
2219    }
2220  }
2221}
2222
2223Void TAppDecTop::xF13522OutputAndRemOfPicsFromDpb( Bool beforePocDerivation )
2224{
2225  ////////////////////////////////////////////////////////////////////////////////
2226  // F.13.5.2.2 Output and removal of pictures from the DPB
2227  ////////////////////////////////////////////////////////////////////////////////
2228
2229  if( beforePocDerivation )
2230  {
2231    // F.13.5.2.2
2232    // Part before POC and RPS derivation.
2233
2234    if( m_curPic->getDecodingOrder() != 0 )
2235    {
2236      // When the current picture is not picture 0 in the current layer,
2237      // the output and removal of pictures in the current layer, with nuh_layer_id equal to currLayerId,
2238      // from the DPB before the decoding of the current picture, i.e., picture n, but after parsing the
2239      // slice header of the first slice of the current picture and before the invocation of the decoding
2240      // process for picture order count, happens instantaneously when the first decoding unit of the current
2241      // picture is removed from the CPB and proceeds as follows:
2242
2243      if( m_curPic->getIsPocResettingPic() )
2244      {
2245        // When the current picture is a POC resetting picture, all pictures in the DPB that do
2246        // not belong to the current access unit and that are marked as "needed for output" are
2247        // output, starting with pictures with the smallest value of PicOrderCntVal of all pictures
2248        // excluding those in the current access unit in the DPB, in ascending order of the PicOrderCntVal
2249        // values, and pictures with the same value of PicOrderCntVal are output in ascending order
2250        // of the nuh_layer_id values. When a picture is output, it is cropped using the conformance cropping
2251        // window specified in the active SPS for the picture, the cropped picture is output, and
2252        // the picture is marked as "not needed for output"
2253
2254        TComList<TComAu*>* aus = m_dpb.getAus(); // Theses are sorted by POC.
2255        for (TComList<TComAu*>::iterator itA = aus->begin(); itA != aus->end(); itA++ )
2256        {
2257          //Pictures in AUs are sorted by nuh_layer_id;
2258          for (TComAu::iterator itP = (*itA)->begin(); itP != (*itA)->end(); itP++ )
2259          {
2260            TComPic* pic = (*itP);
2261            if ( !m_curAu.containsPic( pic ) )
2262            {
2263              xCropAndOutput( pic );
2264              pic->setOutputMark( false );
2265            }
2266          }
2267        }
2268      }
2269    }
2270  }
2271  else if ( !beforePocDerivation )
2272  {
2273    //  The variable listOfSubDpbsToEmpty is derived as follows:
2274
2275    Int nuhLayerId = m_curPic->getLayerId();
2276    TComList<TComSubDpb*> listOfSubDpbsToEmpty;
2277
2278    if ( m_newVpsActivatedbyCurAu && ( m_curPic->isIrap()&& nuhLayerId == m_smallestLayerId
2279      && m_curPic->getNoRaslOutputFlag() && m_curPic->getNoClrasOutputFlag() ) )
2280    {
2281      // If a new VPS is activated by the current access unit or the current picture is IRAP picture
2282      // with nuh_layer_id equal to SmallestLayerId,   NoRaslOutputFlag equal to 1, and NoClrasOutputFlag equal to 1,
2283      // listOfSubDpbsToEmpty is set equal to all the sub-DPBs.
2284      listOfSubDpbsToEmpty = (*m_dpb.getSubDpbs());
2285    }
2286    else if (m_curPic->isIrap() && m_vps->getNumDirectRefLayers( nuhLayerId ) == 0 &&
2287      nuhLayerId > m_smallestLayerId && m_curPic->getNoRaslOutputFlag() && m_layerResetFlag  )
2288    {
2289      // Otherwise, if the current picture is an IRAP picture with any nuh_layer_id value indepLayerId
2290      // such that NumDirectRefLayers[ indepLayerId ] is equal to 0 and indepLayerId is greater than
2291      // SmallestLayerId, and with NoRaslOutputFlag equal to 1, and LayerResetFlag is equal to 1,
2292
2293      // listOfSubDpbsToEmpty is set equal to the sub-DPBs containing the current layer and the sub-DPBs
2294      // containing the predicted layers of the current layer.
2295
2296      listOfSubDpbsToEmpty.pushBack( m_dpb.getSubDpb( nuhLayerId, false  ) );
2297      for( Int i = 0; i < m_vps->getNumPredictedLayers( nuhLayerId ); i++  )
2298      {
2299        listOfSubDpbsToEmpty.pushBack( m_dpb.getSubDpb( m_vps->getIdPredictedLayer( nuhLayerId, i), false  ) );
2300      }
2301    }
2302    else
2303    {
2304      // Otherwise, crossLayerBufferEmptyFlag is set equal to 0.
2305
2306      // The SPEC seems to have an issue here. Use current subDpb as in form F.13.3.2
2307      listOfSubDpbsToEmpty.pushBack( m_dpb.getSubDpb( nuhLayerId, false  ) );
2308    }
2309
2310    // If the current picture is an IRAP picture with NoRaslOutputFlag equal to 1 and any of
2311    // the following conditions is true:
2312    //   - nuh_layer_id equal to SmallestLayerId,
2313    //   - nuh_layer_id of the current layer is greater than SmallestLayerId, and NumDirectRefLayers[ nuh_layer_id ]
2314    //     is equal to 0,
2315
2316    if ( m_curPic->isIrap() && m_curPic->getNoRaslOutputFlag() && (
2317      ( nuhLayerId == m_smallestLayerId ) ||
2318      ( ( nuhLayerId > m_smallestLayerId ) && m_vps->getNumDirectRefLayers( nuhLayerId ) == 0 ) )
2319      )
2320    {
2321      // 1. The variable NoOutputOfPriorPicsFlag is derived for the decoder under test as follows:
2322      Bool noOutputOfPriorPicsFlag;
2323      if( m_curPic->isCra() )
2324      {
2325        noOutputOfPriorPicsFlag = true;
2326        // - If the current picture is a CRA picture, NoOutputOfPriorPicsFlag is set equal to 1
2327        // (regardless of the value of no_output_of_prior_pics_flag).
2328      }
2329      else if ( false )
2330      {
2331        // TBD
2332        // - Otherwise, if the value of pic_width_in_luma_samples, pic_height_in_luma_samples,
2333        //   chroma_format_idc, bit_depth_luma_minus8, bit_depth_chroma_minus8, separate_colour_plane_flag,
2334        //   or sps_max_dec_pic_buffering_minus1[ HighestTid ] derived from the active SPS for the current
2335        //   layer is different from the value of pic_width_in_luma_samples, pic_height_in_luma_samples,
2336        //   chroma_format_idc, bit_depth_luma_minus8, bit_depth_chroma_minus8, separate_colour_plane_flag,
2337        //   or sps_max_dec_pic_buffering_minus1[ HighestTid ], respectively, derived from the SPS that
2338        //   was active for the current layer when decoding the preceding picture in the current layer,
2339        //   NoOutputOfPriorPicsFlag may (but should not) be set equal to 1 by the decoder under test,
2340        //   regardless of the value of no_output_of_prior_pics_flag.
2341        //    NOTE - Although setting NoOutputOfPriorPicsFlag equal to no_output_of_prior_pics_flag is preferred
2342        //    under these conditions, the decoder under test is allowed to set NoOutputOfPriorPicsFlag to 1 in this case.
2343        // - Otherwise, NoOutputOfPriorPicsFlag is set equal to no_output_of_prior_pics_flag.
2344
2345        // assert( 1 );
2346      }
2347      else
2348      {
2349        // - Otherwise, NoOutputOfPriorPicsFlag is set equal to no_output_of_prior_pics_flag.
2350        noOutputOfPriorPicsFlag = m_curPic->getSlice(0)->getNoOutputPriorPicsFlag();
2351      }
2352
2353      // 2. The value of NoOutputOfPriorPicsFlag derived for the decoder under test is applied for the HRD as follows:
2354      if ( !noOutputOfPriorPicsFlag )
2355      {
2356        // - If NoOutputOfPriorPicsFlag is equal to 0, all non-empty picture storage buffers in all the sub-DPBs included
2357        //   in listOfSubDpbsToEmpty are output by repeatedly invoking the "bumping" process specified in clause
2358        //   F.13.5.2.4 until all these pictures are marked as "not needed for output".
2359
2360        Bool repeat = true;
2361        while (repeat )
2362        {
2363          Bool allPicsMarkedNotNeedForOutput = true;
2364          for (TComList<TComSubDpb*>::iterator itS = listOfSubDpbsToEmpty.begin(); itS != listOfSubDpbsToEmpty.end() && allPicsMarkedNotNeedForOutput; itS++ )
2365          {
2366            allPicsMarkedNotNeedForOutput = allPicsMarkedNotNeedForOutput && ( (*itS)->areAllPicsMarkedNotNeedForOutput() );
2367          }
2368
2369          if ( !allPicsMarkedNotNeedForOutput )
2370          {
2371            xF13524Bumping( m_dpb.getAusHavingPicsMarkedForOutput() );
2372          }
2373          else
2374          {
2375            repeat = false;
2376          }
2377        }
2378      }
2379      else
2380      {
2381        // - Otherwise (NoOutputOfPriorPicsFlag is equal to 1), all picture storage buffers containing a picture
2382        //   that is marked as "not needed for output" and "unused for reference" are emptied (without output),
2383        //   all pictures that are contained in a sub-DPB included in listOfSubDpbsToEmpty are emptied, and the sub-DPB
2384        //   fullness of each sub-DPB is decremented by the number of picture storage buffers emptied in that sub-DPB.
2385        m_dpb.emptyNotNeedForOutputAndUnusedForRef();
2386
2387        for( TComList<TComSubDpb*>::iterator iS = listOfSubDpbsToEmpty.begin(); iS != listOfSubDpbsToEmpty.end(); iS++)
2388        {
2389          m_dpb.emptySubDpbs( &listOfSubDpbsToEmpty );
2390        }
2391      }
2392    }
2393    else
2394    {
2395      // -  Otherwise, all picture storage buffers that contain a picture in the current layer and that are marked as
2396      //   "not needed for output" and "unused for reference" are emptied (without output). For each picture storage buffer that is emptied,
2397      //   the sub-DPB fullness is decremented by one.
2398
2399      m_dpb.emptySubDpbNotNeedForOutputAndUnusedForRef( nuhLayerId );
2400
2401      //    When one or more of the following conditions are true, the "bumping" process specified in clause F.13.5.2.4
2402      //    is invoked repeatedly until none of the following conditions are true:
2403
2404      Bool repeat = true;
2405      while ( repeat )
2406      {
2407        TComList<TComAu*> aus = m_dpb.getAusHavingPicsMarkedForOutput();
2408
2409        // The number of access units that contain at least one decoded picture in the DPB marked
2410        // as "needed for output" is greater than MaxNumReorderPics.
2411        Bool cond1 = ( aus.size() > m_maxNumReorderPics );
2412
2413        // MaxLatencyIncreasePlus1 is not equal to 0 and there is at least one access unit
2414        // that contains at least one decoded picture in the DPB marked as "needed for output"
2415        // for which the associated variable PicLatencyCount is greater than or equal to MaxLatencyValue.
2416        Bool auWithGreaterLatencyCount = false;
2417        for(TComList<TComAu*>::iterator itA= aus.begin(); ( itA!=aus.end()) && !auWithGreaterLatencyCount ; itA++)
2418        {
2419          if ( (*itA)->getPicLatencyCount() > m_maxLatencyValue )
2420          {
2421            auWithGreaterLatencyCount = true;
2422          }
2423        }
2424
2425        Bool cond2 = (m_maxLatencyIncreasePlus1 != 0 ) && auWithGreaterLatencyCount;
2426
2427        // The number of pictures in the sub-DPB is greater than or equal to MaxDecPicBufferingMinus1 + 1.
2428        Bool cond3 = ( m_dpb.getSubDpb( nuhLayerId, false )->size() >= m_maxDecPicBufferingMinus1[ nuhLayerId ] + 1 );
2429
2430        if ( cond1  || cond2 || cond3 )
2431        {
2432          xF13524Bumping( aus );
2433        }
2434        else
2435        {
2436          repeat = false;
2437        }
2438      }
2439    }
2440  }
2441}
2442
2443Void TAppDecTop::xF13523PicDecMarkAddBumpAndStor( Bool curPicIsLastInAu )
2444{
2445  ////////////////////////////////////////////////////////////////////////////////
2446  // F.13.5.2.3 Picture decoding, marking, additional bumping and storage
2447  ////////////////////////////////////////////////////////////////////////////////
2448
2449  const TComVPS* vps = m_curPic->getSlice(0)->getVPS();
2450
2451  // The current picture is considered as decoded after the last decoding unit of
2452  // the picture is decoded. The current decoded picture is stored in an empty picture
2453  // storage buffer in the sub-DPB.
2454
2455  m_dpb.addNewPic( m_curPic );
2456
2457  if ( curPicIsLastInAu )
2458  {
2459    // When the current picture is the last picture in an access unit, the following applies
2460    // for each decoded picture with nuh_layer_id greater than or
2461    // equal to ( vps_base_layer_internal_flag ? 0 : 1 ) of the access unit:
2462
2463    for( TComList<TComPic*>::iterator itP = m_curAu.begin(); itP != m_curAu.end(); itP++ )
2464    {
2465      TComPic* pic = (*itP);
2466      if( pic->getLayerId() >= ( vps->getVpsBaseLayerInternalFlag() ? 0 : 1 ) )
2467      {
2468        if ( pic->getPicOutputFlag() )
2469        {
2470          //If the decoded picture has PicOutputFlag equal to 1, it is marked as "needed for output".
2471          pic->setOutputMark( true );
2472        }
2473        else
2474        {
2475          // Otherwise it is marked as "not needed for output".
2476          pic->setOutputMark( false );
2477        }
2478        // NOTE - Prior to investigating the conditions above, PicOutputFlag
2479        // of each picture of the access unit is updated as specified in clause F.8.1.2.
2480      }
2481    }
2482  }
2483
2484  // The current decoded picture is marked as "used for short-term reference".
2485  m_curPic->markAsUsedForShortTermReference();
2486
2487
2488  Bool repeat = true;
2489  while ( repeat )
2490  {
2491    TComList<TComAu*> aus = m_dpb.getAusHavingPicsMarkedForOutput();
2492
2493    // When one or more of the following conditions are true,
2494    // the "bumping" process specified in clause F.13.5.2.4 is invoked
2495    // repeatedly until none of the following conditions are true:
2496
2497    // - The number of access units that contain at least one decoded picture in the DPB marked
2498    //   as "needed for output" is greater than MaxNumReorderPics.
2499    Bool cond1 = ( aus.size() > m_maxNumReorderPics );
2500
2501    // - MaxLatencyIncreasePlus1 is not equal to 0 and there is at least one access unit
2502    //   that contains at least one decoded picture in the DPB marked as "needed for output"
2503    //   for which the associated variable PicLatencyCount is greater than or equal to MaxLatencyValue.
2504    Bool auWithGreaterLatencyCount = false;
2505    for(TComList<TComAu*>::iterator itA= aus.begin(); ( itA!=aus.end()) && !auWithGreaterLatencyCount ; itA++)
2506    {
2507      if ( (*itA)->getPicLatencyCount() > m_maxLatencyValue )
2508      {
2509        auWithGreaterLatencyCount = true;
2510      }
2511    }
2512
2513    Bool cond2 = (m_maxLatencyIncreasePlus1 != 0 ) && auWithGreaterLatencyCount;
2514
2515    if ( cond1  || cond2 )
2516    {
2517      xF13524Bumping( aus );
2518    }
2519    else
2520    {
2521      repeat = false;
2522    }
2523  }
2524}
2525
2526Void TAppDecTop::xF13524Bumping( TComList<TComAu*> aus )
2527{
2528  ////////////////////////////////////////////////////////////////////////////////
2529  // F.13.5.2.4 "Bumping" process
2530  ////////////////////////////////////////////////////////////////////////////////
2531
2532  // The picture or pictures that are first for output are selected as the ones having the
2533  // smallest value of PicOrderCntVal of all pictures in the DPB marked as "needed for output".
2534
2535  assert( !aus.empty() );
2536
2537  // Create copy, since original AU from DBP is modified when removing pic.
2538  TComAu auWithSmallestPoc = *((*aus.begin())); // List is sorted, hence the AU with smallest POC is the first.
2539
2540  for(TComAu::iterator itP= auWithSmallestPoc.begin(); ( itP!=auWithSmallestPoc.end() ); itP++)
2541  {
2542    TComPic* pic = (*itP);
2543
2544    if (pic->getOutputMark() )
2545    {
2546      // Each of these pictures is, in ascending nuh_layer_id order, cropped,
2547      // using the conformance cropping window specified in the active SPS for the picture,
2548      // the cropped picture is output, and the picture is marked as "not needed for output".
2549
2550      // pictures are sorted in the AU in ascending nuh_layer_id order.
2551
2552
2553      xCropAndOutput( pic );
2554      pic->setOutputMark( false );
2555
2556      // Each picture storage buffer that contains a picture marked as "unused for reference"
2557      // and that was one of the pictures cropped and output is emptied and the fullness of
2558      // the associated sub-DPB is decremented by one.
2559
2560      if (pic->getMarkedUnUsedForReference() )
2561      {
2562        m_dpb.removePic( pic );
2563      }
2564    }
2565  }
2566}
2567
2568Void TAppDecTop::xF816decProcEndDecOfCodPicLIdGrtZero()
2569{
2570  ////////////////////////////////////////////////////////////////////////////////
2571  // F.8.1.6  Decoding process for ending the decoding of a coded picture with nuh_layer_id greater than 0
2572  ////////////////////////////////////////////////////////////////////////////////
2573
2574  const TComSlice* slice = m_curPic->getSlice( 0 );
2575  const Int nuhLayerId   = m_curPic->getLayerId();
2576
2577  assert(  nuhLayerId != 0 );
2578
2579  //The marking of decoded pictures is modified as specified in the following:
2580  for (Int i = 0; i < m_curPic->getDecodedRps()->m_numActiveRefLayerPics0;i++ )
2581  {
2582    m_curPic->getDecodedRps()->m_refPicSetInterLayer0[i]->markAsUsedForShortTermReference();
2583  }
2584
2585  for (Int i = 0; i < m_curPic->getDecodedRps()->m_numActiveRefLayerPics1;i++ )
2586  {
2587    m_curPic->getDecodedRps()->m_refPicSetInterLayer1[i]->markAsUsedForShortTermReference();
2588  }
2589
2590  // PicOutputFlag is set as follows:
2591  if ( !m_layerInitilizedFlag[ nuhLayerId ] )
2592  {
2593    // - If LayerInitializedFlag[ nuh_layer_id ] is equal to 0, PicOutputFlag is set equal to 0.
2594    m_curPic->setPicOutputFlag( false );
2595  }
2596  else if (m_curPic->isRasl( ) && m_noRaslOutputFlagAssocIrap[ nuhLayerId] )
2597  {
2598    // - Otherwise, if the current picture is a RASL picture and NoRaslOutputFlag of the associated IRAP picture is equal to 1,
2599    //   PicOutputFlag is set equal to 0.
2600
2601    m_curPic->setPicOutputFlag( false );
2602  }
2603  else
2604  {
2605    // - Otherwise, PicOutputFlag is set equal to pic_output_flag.
2606    m_curPic->setPicOutputFlag( slice->getPicOutputFlag() );
2607  }
2608
2609  // The decoded picture is marked as "used for short-term reference".
2610  m_curPic->markAsUsedForShortTermReference();
2611
2612  if ( !m_firstPicInLayerDecodedFlag[ nuhLayerId ] )
2613  {
2614    // When FirstPicInLayerDecodedFlag[ nuh_layer_id ] is equal to 0, FirstPicInLayerDecodedFlag[ nuh_layer_id ] is set equal to 1.
2615    m_firstPicInLayerDecodedFlag[ nuhLayerId ] = true;
2616  }
2617}
2618
2619TDecTop* TAppDecTop::xGetDecoder( InputNALUnit& nalu )
2620{
2621  return m_tDecTop[ xGetDecoderIdx( nalu.m_nuhLayerId )];
2622}
2623
2624
2625Int TAppDecTop::xGetDecoderIdx( Int layerId, Bool createFlag /*= false */ )
2626{
2627  Int decIdx = -1;
2628
2629  if ( layerId > MAX_NUM_LAYER_IDS-1 )
2630  {
2631    return decIdx;
2632  }
2633
2634  if ( m_layerIdToDecIdx[ layerId ] != -1 )
2635  {
2636    decIdx = m_layerIdToDecIdx[ layerId ];
2637  }
2638  else
2639  {
2640    assert ( createFlag );
2641    assert( m_numDecoders < MAX_NUM_LAYERS );
2642
2643    decIdx = m_numDecoders;
2644
2645    // Init decoder
2646    m_tDecTop[ decIdx ] =  new TDecTop;
2647    m_tDecTop[ decIdx ]->create();
2648    m_tDecTop[ decIdx ]->init( );
2649    m_tDecTop[ decIdx ]->setLayerId( layerId );
2650    m_tDecTop[ decIdx ]->setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
2651    m_tDecTop[ decIdx ]->setDpb( &m_dpb );
2652    m_tDecTop[ decIdx ]->setTargetOlsIdx( m_targetOptLayerSetIdx );
2653    m_tDecTop[ decIdx ]->setFirstPicInLayerDecodedFlag( m_firstPicInLayerDecodedFlag );
2654    m_tDecTop[ decIdx ]->setPocDecrementedInDPBFlag   ( m_pocDecrementedInDpbFlag    );
2655    m_tDecTop[ decIdx ]->setLastPresentPocResetIdc    ( m_lastPresentPocResetIdc );
2656
2657
2658#if O0043_BEST_EFFORT_DECODING
2659    m_cTDecTop[ decIdx ]->setForceDecodeBitDepth(m_forceDecodeBitDepth);
2660#endif
2661    if (!m_outputDecodedSEIMessagesFilename.empty())
2662    {
2663      std::ostream &os=m_seiMessageFileStream.is_open() ? m_seiMessageFileStream : std::cout;
2664      m_tDecTop[ decIdx ]->setDecodedSEIMessageOutputStream(&os);
2665    }
2666#if NH_3D
2667   m_tDecTop[ decIdx ]->setCamParsCollector( &m_cCamParsCollector );
2668#endif
2669
2670    // append pic list of new decoder to PicLists
2671
2672    // create recon file related stuff
2673    Char* pchTempFilename = NULL;
2674    if ( m_pchReconFile )
2675    {
2676      Char buffer[4];
2677      sprintf(buffer,"_%i", layerId );
2678      assert ( m_pchReconFile );
2679      xAppendToFileNameEnd( m_pchReconFile , buffer, pchTempFilename );
2680      assert( m_pchReconFiles.size() == m_numDecoders );
2681    }
2682
2683    m_pchReconFiles.push_back( pchTempFilename );
2684
2685    m_tVideoIOYuvReconFile[ decIdx ] = new TVideoIOYuv;
2686    m_reconOpen           [ decIdx ] = false;
2687
2688    // set others
2689    m_layerIdToDecIdx     [ layerId ] = decIdx;
2690
2691    m_numDecoders++;
2692  };
2693  return decIdx;
2694
2695}
2696
2697Int TAppDecTop::xPreDecodePoc( InputNALUnit& nalu )
2698{ 
2699  // - According to F.7.4.2.4.4, the POC of the current picture is required to detect whether it is the first in a new AU
2700  // - F.8.1.3 needs to know if the current picture is the first of an AU
2701  //   actually before its POC has been decoded.
2702
2703  // Thus, in the software implementation the processes can not be invoked in the same order as in the Spec.
2704  // For this, this function decodes the POC before invoking F.8.1.3. However, this is done without
2705  // altering member variables
2706
2707  // Do some stuff from F.8.1.3 required for POC derivation, which is not depending on AU detection.
2708  TDecTop* dec          = xGetDecoder( nalu ); 
2709  TComSlice* slicePilot = dec->getSlicePilot(); 
2710  Int nuhLayerId        = nalu.m_nuhLayerId; 
2711  Int smallestLayerId   = dec->getSmallestLayerId();
2712
2713  Int handleCraAsBlaFlag; 
2714  if ( nalu.isIrap() )
2715  {
2716    if ( !m_handleCraAsBlaFlagSetByExtMeans )
2717    {
2718      handleCraAsBlaFlag = false;
2719    }
2720  }
2721
2722  Bool firstPicInLayerDecodedFlag = m_firstPicInLayerDecodedFlag[ nalu.m_nuhLayerId ];
2723 
2724  if ( nalu.isIrap() && nuhLayerId == smallestLayerId )
2725  {
2726    Int noClrasOutputFlag;
2727    if( m_firstSliceInBitstream )
2728    {
2729      noClrasOutputFlag = true; 
2730    }
2731    else if( m_eosInLayer[ 0 ] || m_eosInLayer[ nuhLayerId ] )
2732    {
2733      noClrasOutputFlag = true;
2734    }
2735    else if ( nalu.isBla() || (nalu.isCra() && handleCraAsBlaFlag ))
2736    {
2737      noClrasOutputFlag = true; 
2738    }
2739    else if ( nalu.isIdr() && slicePilot->getCrossLayerBlaFlag() )
2740    {
2741      noClrasOutputFlag = true; 
2742    }
2743    else if ( m_noClrasOutputFlagSetByExtMeans )
2744    {
2745      noClrasOutputFlag = m_noClrasOutputFlag; 
2746    }
2747    else
2748    {     
2749      noClrasOutputFlag  = false;
2750    }
2751
2752    if( noClrasOutputFlag )
2753    {
2754      firstPicInLayerDecodedFlag = false; 
2755    }   
2756  }
2757
2758  // Derive POC
2759  return dec->preDecodePoc(firstPicInLayerDecodedFlag, m_newPicIsFstPicOfAllLayOfPocResetPer, m_newPicIsPocResettingPic );
2760}
2761
2762Bool TAppDecTop::xDetectNewAu( InputNALUnit& nalu )
2763{
2764  TDecTop*        dec        = xGetDecoder( nalu );
2765  TComSlice*      slicePilot = dec->getSlicePilot();
2766  const TComVPS*  vps        = slicePilot->getVPS();
2767
2768  Bool firstVclNaluOfAu;
2769
2770  if (m_curPic == NULL )
2771  {
2772    // No picture decoded yet, so we have a new AU.
2773    firstVclNaluOfAu = true; 
2774  }
2775  else
2776  {
2777    if ( !vps->getVpsExtensionFlag() )
2778    {
2779      // Decoding according to clause 8, hence one pic per AU.
2780      firstVclNaluOfAu = slicePilot->getFirstSliceSegementInPicFlag();
2781    }
2782    else
2783    {
2784      if ( dec->getTargetOlsIdx() == 0 )
2785      {
2786        // Only the base layer is decoded, hence one pic per AU.
2787        firstVclNaluOfAu = slicePilot->getFirstSliceSegementInPicFlag();
2788      }
2789      else
2790      {
2791        // F.7.4.2.4.4  Order of NAL units and coded pictures and association to access units   
2792        //  An access unit consists of one or more coded pictures, each with a distinct value of
2793        //  nuh_layer_id, and zero or more non-VCL NAL units.
2794
2795        //  A VCL NAL unit is the first VCL NAL unit of an access unit, when all of the following conditions are true:
2796        //  -  first_slice_segment_in_pic_flag is equal to 1.
2797        //  -  At least one of the following conditions is true:
2798        //     - The previous picture in decoding order belongs to a different POC resetting period
2799        //       than the picture containing the VCL NAL unit.
2800        Bool prevPicDiffPocResetPeriod = m_newPicIsFstPicOfAllLayOfPocResetPer; 
2801
2802        //     - PicOrderCntVal derived for the VCL NAL unit differs from the PicOrderCntVal of the
2803        //       previous picture in decoding order.
2804        Bool prevPicDiffPoc = ( xPreDecodePoc( nalu ) != m_curPic->getPOC() );
2805
2806        if( slicePilot->getFirstSliceSegementInPicFlag() && ( prevPicDiffPocResetPeriod || prevPicDiffPoc ) )
2807        {
2808          firstVclNaluOfAu = true;
2809        }
2810        else
2811        {
2812          firstVclNaluOfAu = false;
2813        }
2814      }
2815    }
2816  }
2817  return firstVclNaluOfAu;
2818}
2819
2820Void TAppDecTop::xDetectNewPocResettingPeriod( InputNALUnit& nalu )
2821{
2822  TDecTop* dec  = xGetDecoder( nalu );
2823  dec->inferPocResetPeriodId();
2824
2825
2826  Int pocResetIdc         = dec->getSlicePilot()->getPocResetIdc();
2827  Int newPocResetPeriodId = dec->getSlicePilot()->getPocResetPeriodId();
2828
2829  Int curPocResetPeriodId = ( m_curPic != NULL)  ? m_curPic->getPocResetPeriodId() : MIN_INT; 
2830
2831  // Check if new picture starts a new poc resetting period.
2832  if(  ( pocResetIdc == 1 || pocResetIdc == 2 ) &&  ( curPocResetPeriodId != newPocResetPeriodId ) )
2833  {
2834    for (Int i = 0; i < MAX_NUM_LAYER_IDS; i++ )
2835    {
2836      m_firstPicInPocResettingPeriodReceived[ i ] = false;
2837    }
2838    m_newPicIsFstPicOfAllLayOfPocResetPer = true;
2839  }
2840  else
2841  {
2842    m_newPicIsFstPicOfAllLayOfPocResetPer = false;
2843  }
2844
2845  // Check if current picture is a poc resetting picture (thus the first picture of this layer within the POC resetting period.
2846  if ( !m_firstPicInPocResettingPeriodReceived[ nalu.m_nuhLayerId ] )
2847  {
2848    m_newPicIsPocResettingPic = true;
2849    m_firstPicInPocResettingPeriodReceived[ nalu.m_nuhLayerId ] = true;
2850  }
2851  else
2852  {
2853    m_newPicIsPocResettingPic = false;
2854  }
2855
2856}
2857
2858Bool TAppDecTop::xAllRefLayersInitilized( Int curLayerId )
2859{
2860  Bool allRefLayersInitilizedFlag = true;
2861  for (Int i = 0; i < m_vps->getNumDirectRefLayers( curLayerId  ); i++ )
2862  {
2863    Int refLayerId = m_vps->getIdDirectRefLayer( curLayerId, i );
2864    allRefLayersInitilizedFlag = allRefLayersInitilizedFlag && m_layerInitilizedFlag[ refLayerId ];
2865  }
2866
2867  return allRefLayersInitilizedFlag;
2868}
2869
2870Void TAppDecTop::xInitFileIO()
2871{
2872  m_bitstreamFile.open(m_pchBitstreamFile, ifstream::in | ifstream::binary);
2873
2874  if ( !m_bitstreamFile)
2875  {
2876    fprintf(stderr, "\nUnable to open bitstream file `%s' for reading\n", m_pchBitstreamFile);
2877    exit(EXIT_FAILURE);
2878  }
2879
2880  if (!m_outputDecodedSEIMessagesFilename.empty() && m_outputDecodedSEIMessagesFilename!="-")
2881  {
2882    m_seiMessageFileStream.open(m_outputDecodedSEIMessagesFilename.c_str(), std::ios::out);
2883    if (!m_seiMessageFileStream.is_open() || !m_seiMessageFileStream.good())
2884    {
2885      fprintf(stderr, "\nUnable to open file `%s' for writing decoded SEI messages\n", m_outputDecodedSEIMessagesFilename.c_str());
2886      exit(EXIT_FAILURE);
2887    }
2888  }
2889
2890#if NH_3D
2891  if( m_pchScaleOffsetFile )
2892  {
2893    m_pScaleOffsetFile = ::fopen( m_pchScaleOffsetFile, "wt" );
2894    if (!m_pScaleOffsetFile)
2895    {
2896      fprintf(stderr, "\nUnable to open file `%s' for writing decoded Camera Parameters messages\n", m_pchScaleOffsetFile);
2897      exit(EXIT_FAILURE);
2898    }
2899  }
2900#endif
2901}
2902
2903Void TAppDecTop::xOpenReconFile( TComPic* curPic )
2904{
2905  Int decIdx = xGetDecoderIdx( curPic->getLayerId() );
2906
2907  if ( m_pchReconFile && !m_reconOpen[decIdx] )
2908  {
2909    const BitDepths &bitDepths= curPic->getPicSym()->getSPS().getBitDepths(); // use bit depths of first reconstructed picture.
2910    for (UInt channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
2911    {
2912      if (m_outputBitDepth[channelType] == 0)
2913      {
2914        m_outputBitDepth[channelType] = bitDepths.recon[channelType];
2915      }
2916    }
2917
2918    m_tVideoIOYuvReconFile[decIdx]->open( m_pchReconFiles[decIdx], true, m_outputBitDepth, m_outputBitDepth, bitDepths.recon ); // write mode
2919    m_reconOpen[decIdx] = true;
2920  }
2921}
2922
2923Void TAppDecTop::xFlushOutput()
2924{
2925  Bool repeat = true;
2926  while ( repeat )
2927  {
2928    if( m_decProcCvsg == ANNEX_F )
2929    {
2930      TComList<TComAu*> aus = m_dpb.getAusHavingPicsMarkedForOutput();
2931      if ( !aus.empty() )
2932      {
2933        xF13524Bumping( aus );
2934      }     
2935      else
2936      {
2937        repeat = false;
2938      }
2939    }     
2940    else if( m_decProcCvsg == CLAUSE_8 )
2941    {
2942      TComList<TComPic*> picsMarkedForOutput = m_dpb.getSubDpb( 0, false )->getPicsMarkedNeedForOutput();
2943      if (!picsMarkedForOutput.empty())
2944      {
2945        xC524Bumping();
2946      }
2947      else
2948      {
2949        repeat = false; 
2950      }
2951    }
2952  }
2953}
2954
2955Void TAppDecTop::xCropAndOutput( TComPic* curPic )
2956{
2957
2958  if ( m_printPicOutput )
2959  {
2960    std::cout << "  Output picture: ";
2961    curPic->print( 2 );
2962    std::cout << std::endl;
2963  }
2964
2965  assert( !curPic->getHasGeneratedRefPics() );
2966  assert( !curPic->getIsGenerated()         );
2967
2968  Int decIdx = xGetDecoderIdx( curPic->getLayerId() );
2969
2970  if (!m_reconOpen[ decIdx ])
2971  {
2972    xOpenReconFile( curPic );
2973  }
2974
2975  if ( m_pchReconFiles[ decIdx ] )
2976  {
2977    const Window &conf    = curPic->getConformanceWindow();
2978    const Window  defDisp = m_respectDefDispWindow ? curPic->getDefDisplayWindow() : Window();
2979
2980    assert( conf   .getScaledFlag() );
2981    assert( defDisp.getScaledFlag() );
2982
2983    m_tVideoIOYuvReconFile[decIdx]->write( curPic->getPicYuvRec(),
2984      m_outputColourSpaceConvert,
2985      conf.getWindowLeftOffset()   + defDisp.getWindowLeftOffset(),
2986      conf.getWindowRightOffset()  + defDisp.getWindowRightOffset(),
2987      conf.getWindowTopOffset()    + defDisp.getWindowTopOffset(),
2988      conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(),
2989#if NH_3D
2990      m_depth420OutputFlag && curPic->getIsDepth() ? CHROMA_420 : NUM_CHROMA_FORMAT
2991#else
2992      NUM_CHROMA_FORMAT
2993#endif
2994      , m_bClipOutputVideoToRec709Range);
2995  }
2996}
2997
2998UInt TAppDecTop::getNumberOfChecksumErrorsDetected() const
2999{
3000  UInt numOfChecksumErrors = 0;
3001  for (Int i = 0; i < m_numDecoders; i++ )
3002  {
3003    numOfChecksumErrors += getNumberOfChecksumErrorsDetected( i );
3004  }
3005  return numOfChecksumErrors;
3006}
3007
3008#endif
3009//! \}
Note: See TracBrowser for help on using the repository browser.