source: 3DVCSoftware/trunk/source/App/TAppDecoder/TAppDecTop.cpp @ 1179

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

Merged branch 13.1-dev0@1178.

  • Property svn:eol-style set to native
File size: 36.3 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
48//! \ingroup TAppDecoder
49//! \{
50
51// ====================================================================================================================
52// Constructor / destructor / initialization / destroy
53// ====================================================================================================================
54
55TAppDecTop::TAppDecTop()
56#if !H_MV
57: m_iPOCLastDisplay(-MAX_INT)
58#else
59: m_numDecoders( 0 )
60#endif
61{
62#if H_MV
63  for (Int i = 0; i < MAX_NUM_LAYER_IDS; i++) 
64  {
65    m_layerIdToDecIdx[i] = -1; 
66    m_layerInitilizedFlags[i] = false; 
67  }
68#endif
69#if H_3D
70    m_pScaleOffsetFile  = 0;
71#endif
72
73#if H_MV
74    m_markedForOutput = false; 
75#endif
76
77}
78
79Void TAppDecTop::create()
80{
81}
82
83Void TAppDecTop::destroy()
84{
85  if (m_pchBitstreamFile)
86  {
87    free (m_pchBitstreamFile);
88    m_pchBitstreamFile = NULL;
89  }
90#if H_MV
91  for (Int decIdx = 0; decIdx < m_numDecoders; decIdx++)
92  {
93    if (m_pchReconFiles[decIdx])
94    {
95      free (m_pchReconFiles[decIdx]);
96      m_pchReconFiles[decIdx] = NULL;
97    }
98  }
99#endif
100  if (m_pchReconFile)
101  {
102    free (m_pchReconFile);
103    m_pchReconFile = NULL;
104  }
105#if H_3D
106  if (m_pchScaleOffsetFile)
107  {
108    free (m_pchScaleOffsetFile);
109    m_pchScaleOffsetFile = NULL; 
110  }
111#endif
112}
113
114// ====================================================================================================================
115// Public member functions
116// ====================================================================================================================
117
118/**
119 - create internal class
120 - initialize internal class
121 - until the end of the bitstream, call decoding function in TDecTop class
122 - delete allocated buffers
123 - destroy internal class
124 .
125 */
126Void TAppDecTop::decode()
127{
128  Int                 poc;
129#if H_MV
130  poc = -1; 
131#endif
132  TComList<TComPic*>* pcListPic = NULL;
133
134  ifstream bitstreamFile(m_pchBitstreamFile, ifstream::in | ifstream::binary);
135  if (!bitstreamFile)
136  {
137    fprintf(stderr, "\nfailed to open bitstream file `%s' for reading\n", m_pchBitstreamFile);
138    exit(EXIT_FAILURE);
139  }
140
141#if H_3D
142  if( m_pchScaleOffsetFile ) 
143  { 
144    m_pScaleOffsetFile = ::fopen( m_pchScaleOffsetFile, "wt" ); 
145    AOF( m_pScaleOffsetFile ); 
146  }
147#endif
148  InputByteStream bytestream(bitstreamFile);
149
150  // create & initialize internal classes
151  xCreateDecLib();
152  xInitDecLib  ();
153#if !H_MV
154  m_iPOCLastDisplay += m_iSkipFrame;      // set the last displayed POC correctly for skip forward.
155
156  // main decoder loop
157  Bool openedReconFile = false; // reconstruction file not yet opened. (must be performed after SPS is seen)
158#else
159
160  Int  pocCurrPic        = -MAX_INT;     
161  Int  pocLastPic        = -MAX_INT;   
162
163  Int  layerIdLastPic    = -MAX_INT; 
164  Int  layerIdCurrPic    = 0; 
165
166  Int  decIdxLastPic     = 0; 
167  Int  decIdxCurrPic     = 0; 
168
169  Bool firstSlice        = true; 
170#endif
171  Bool loopFiltered      = false;
172
173  while (!!bitstreamFile)
174  {
175    /* location serves to work around a design fault in the decoder, whereby
176     * the process of reading a new slice that is the first slice of a new frame
177     * requires the TDecTop::decode() method to be called again with the same
178     * nal unit. */
179    streampos location = bitstreamFile.tellg();
180#if H_MV
181#if ENC_DEC_TRACE
182    Int64 symCount = g_nSymbolCounter;
183#endif
184#endif
185    AnnexBStats stats = AnnexBStats();
186    vector<uint8_t> nalUnit;
187    InputNALUnit nalu;
188    byteStreamNALUnit(bytestream, nalUnit, stats);
189
190    // call actual decoding function
191    Bool bNewPicture = false;
192#if H_MV
193    Bool newSliceDiffPoc   = false;
194    Bool newSliceDiffLayer = false;
195    Bool sliceSkippedFlag  = false; 
196    Bool allLayersDecoded  = false;     
197#endif
198    if (nalUnit.empty())
199    {
200      /* this can happen if the following occur:
201       *  - empty input file
202       *  - two back-to-back start_code_prefixes
203       *  - start_code_prefix immediately followed by EOF
204       */
205      fprintf(stderr, "Warning: Attempt to decode an empty NAL unit\n");
206    }
207    else
208    {
209      read(nalu, nalUnit);
210#if H_MV     
211      if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) 
212          || !isNaluWithinTargetDecLayerIdSet(&nalu)
213          || nalu.m_layerId > MAX_NUM_LAYER_IDS-1
214          || (nalu.m_nalUnitType == NAL_UNIT_VPS && nalu.m_layerId > 0)           
215          || (nalu.m_nalUnitType == NAL_UNIT_EOB && nalu.m_layerId > 0)   
216          || (nalu.m_nalUnitType == NAL_UNIT_EOS && nalu.m_layerId > 0)   
217         ) 
218      {
219        bNewPicture = false;
220        if ( !bitstreamFile )
221        {
222          decIdxLastPic     = decIdxCurrPic; 
223        }
224      }
225      else
226      { 
227        Int decIdx     = xGetDecoderIdx( nalu.m_layerId , true );     
228        newSliceDiffLayer = nalu.isSlice() && ( nalu.m_layerId != layerIdCurrPic ) && !firstSlice;
229        newSliceDiffPoc   = m_tDecTop[decIdx]->decode(nalu, m_iSkipFrame, m_pocLastDisplay[decIdx], newSliceDiffLayer, sliceSkippedFlag );
230        // decode function only returns true when all of the following conditions are true
231        // - poc in particular layer changes
232        // - nalu does not belong to first slice in layer
233        // - nalu.isSlice() == true     
234
235        if ( nalu.m_nalUnitType == NAL_UNIT_VPS )
236        {
237          m_vps = m_tDecTop[decIdx]->getPrefetchedVPS(); 
238          if ( m_targetDecLayerIdSetFileEmpty )
239          {
240            TComVPS* vps = m_vps; 
241            if ( m_targetOptLayerSetIdx == -1 )
242            {
243              // Not normative! Corresponds to specification by "External Means". (Should be set equal to 0, when no external means available. )
244              m_targetOptLayerSetIdx = vps->getVpsNumLayerSetsMinus1(); 
245            }
246
247            for (Int dI = 0; dI < m_numDecoders; dI++ )
248            {
249              m_tDecTop[decIdx]->setTargetOptLayerSetIdx( m_targetOptLayerSetIdx ); 
250#if H_3D_ANNEX_SELECTION_FIX
251              m_tDecTop[decIdx]->setProfileIdc( ); 
252#endif
253            }
254
255            if ( m_targetOptLayerSetIdx < 0 || m_targetOptLayerSetIdx >= vps->getNumOutputLayerSets() )
256            {
257              fprintf(stderr, "\ntarget output layer set index must be in the range of 0 to %d, inclusive \n", vps->getNumOutputLayerSets() - 1 );           
258              exit(EXIT_FAILURE);
259            }
260            m_targetDecLayerIdSet = vps->getTargetDecLayerIdList( m_targetOptLayerSetIdx ); 
261          }
262          if (m_outputVpsInfo )
263          {
264            m_vps->printScalabilityId();
265            m_vps->printLayerDependencies();
266            m_vps->printLayerSets();
267            m_vps->printPTL(); 
268          }
269        }
270#if H_3D
271        if (nalu.m_nalUnitType == NAL_UNIT_VPS )
272        {                 
273          m_cCamParsCollector.init( m_pScaleOffsetFile, m_tDecTop[decIdx]->getPrefetchedVPS() );
274        }       
275#endif
276        bNewPicture       = ( newSliceDiffLayer || newSliceDiffPoc ) && !sliceSkippedFlag; 
277        if ( nalu.isSlice() && firstSlice && !sliceSkippedFlag )       
278        {
279          layerIdCurrPic = nalu.m_layerId; 
280          pocCurrPic     = m_tDecTop[decIdx]->getCurrPoc(); 
281          decIdxCurrPic  = decIdx; 
282          firstSlice     = false; 
283        }
284
285        if ( bNewPicture || !bitstreamFile )
286        { 
287          layerIdLastPic    = layerIdCurrPic; 
288          layerIdCurrPic    = nalu.m_layerId; 
289          pocLastPic        = pocCurrPic; 
290          pocCurrPic        = m_tDecTop[decIdx]->getCurrPoc(); 
291          decIdxLastPic     = decIdxCurrPic; 
292          decIdxCurrPic     = decIdx; 
293          allLayersDecoded = ( pocCurrPic != pocLastPic );
294        }
295#else
296      if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) || !isNaluWithinTargetDecLayerIdSet(&nalu)  )
297      {
298        bNewPicture = false;
299      }
300      else
301      {
302        bNewPicture = m_cTDecTop.decode(nalu, m_iSkipFrame, m_iPOCLastDisplay);
303#endif
304        if (bNewPicture)
305        {
306          bitstreamFile.clear();
307          /* location points to the current nalunit payload[1] due to the
308           * need for the annexB parser to read three extra bytes.
309           * [1] except for the first NAL unit in the file
310           *     (but bNewPicture doesn't happen then) */
311          bitstreamFile.seekg(location-streamoff(3));
312          bytestream.reset();
313#if H_MV_ENC_DEC_TRAC
314#if ENC_DEC_TRACE
315          const Bool resetCounter = false; 
316          if ( resetCounter )
317          {
318            g_nSymbolCounter  = symCount; // Only reset counter SH becomes traced twice
319          }
320          else
321          {
322            g_disableHLSTrace = true;     // Tracing of second parsing of SH is not carried out
323          }     
324#endif
325#endif
326        }
327      }
328    }
329    if (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS )
330    {
331      if (!loopFiltered || bitstreamFile)
332      {
333#if H_MV
334        assert( decIdxLastPic != -1 ); 
335        m_tDecTop[decIdxLastPic]->endPicDecoding(poc, pcListPic, m_targetDecLayerIdSet );
336        xMarkForOutput( allLayersDecoded, poc, layerIdLastPic ); 
337#else
338        m_cTDecTop.executeLoopFilters(poc, pcListPic);
339#endif
340      }
341      loopFiltered = (nalu.m_nalUnitType == NAL_UNIT_EOS);
342    }
343#if !FIX_WRITING_OUTPUT
344#if SETTING_NO_OUT_PIC_PRIOR
345    if (bNewPicture && m_cTDecTop.getIsNoOutputPriorPics())
346    {
347      m_cTDecTop.checkNoOutputPriorPics( pcListPic );
348    }
349#endif
350#endif
351#if H_3D
352    if ( allLayersDecoded || !bitstreamFile )
353    {
354      for( Int dI = 0; dI < m_numDecoders; dI++ )
355      {
356        TComPic* picLastCoded = m_ivPicLists.getPic( m_tDecTop[dI]->getLayerId(), pocLastPic );
357        assert( picLastCoded != NULL );       
358        picLastCoded->compressMotion(1);
359      }
360    }
361#endif
362
363    if( pcListPic )
364    {
365#if H_MV
366      if ( m_pchReconFiles[decIdxLastPic] && !m_reconOpen[decIdxLastPic] )
367#else
368      if ( m_pchReconFile && !openedReconFile  )
369#endif
370      {
371        if (!m_outputBitDepthY) { m_outputBitDepthY = g_bitDepthY; }
372        if (!m_outputBitDepthC) { m_outputBitDepthC = g_bitDepthC; }
373
374#if H_MV
375        m_tVideoIOYuvReconFile[decIdxLastPic]->open( m_pchReconFiles[decIdxLastPic], true, m_outputBitDepthY, m_outputBitDepthC, g_bitDepthY, g_bitDepthC ); // write mode
376        m_reconOpen[decIdxLastPic] = true;
377      }
378#if FIX_WRITING_OUTPUT
379      // write reconstruction to file
380      if( bNewPicture )
381      {
382        // Bumping after picture has been decoded
383#if ENC_DEC_TRACE
384        g_bJustDoIt = true; 
385        writeToTraceFile( "Bumping after decoding \n", g_decTracePicOutput  );         
386        g_bJustDoIt = false; 
387#endif
388        xWriteOutput( pcListPic, decIdxLastPic, nalu.m_temporalId );
389      }
390#if SETTING_NO_OUT_PIC_PRIOR
391      if ( (bNewPicture || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA) && m_tDecTop[decIdxLastPic]->getNoOutputPriorPicsFlag() )
392      {
393        m_tDecTop[decIdxLastPic]->checkNoOutputPriorPics( pcListPic );
394        m_tDecTop[decIdxLastPic]->setNoOutputPriorPicsFlag (false);
395      }
396#endif
397#endif
398      if ( bNewPicture && newSliceDiffPoc && 
399#else
400        m_cTVideoIOYuvReconFile.open( m_pchReconFile, true, m_outputBitDepthY, m_outputBitDepthC, g_bitDepthY, g_bitDepthC ); // write mode
401        openedReconFile  = true;
402      }
403#if FIX_WRITING_OUTPUT
404      // write reconstruction to file
405      if( bNewPicture )
406      {
407        xWriteOutput( pcListPic, nalu.m_temporalId );
408      }
409#if SETTING_NO_OUT_PIC_PRIOR
410      if ( (bNewPicture || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA) && m_cTDecTop.getNoOutputPriorPicsFlag() )
411      {
412        m_cTDecTop.checkNoOutputPriorPics( pcListPic );
413        m_cTDecTop.setNoOutputPriorPicsFlag (false);
414      }
415#endif
416#endif
417      if ( bNewPicture && 
418#endif
419           (   nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
420            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
421            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP
422            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL
423            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP ) )
424      {
425#if H_MV
426        xFlushOutput( pcListPic, decIdxLastPic );
427#else
428        xFlushOutput( pcListPic );
429#endif
430      }
431      if (nalu.m_nalUnitType == NAL_UNIT_EOS)
432      {
433#if H_MV
434#if FIX_OUTPUT_EOS
435        xWriteOutput( pcListPic, decIdxLastPic, nalu.m_temporalId );
436#else
437        xFlushOutput( pcListPic, decIdxLastPic );
438#endif
439#else
440#if FIX_OUTPUT_EOS
441        xWriteOutput( pcListPic, nalu.m_temporalId );
442#else
443        xFlushOutput( pcListPic );
444#endif
445
446#endif
447      }
448      // write reconstruction to file -- for additional bumping as defined in C.5.2.3
449#if H_MV
450      // Above comment seems to be wrong
451#endif
452#if FIX_WRITING_OUTPUT
453      if(!bNewPicture && nalu.m_nalUnitType >= NAL_UNIT_CODED_SLICE_TRAIL_N && nalu.m_nalUnitType <= NAL_UNIT_RESERVED_VCL31)
454#else
455      if(bNewPicture)
456#endif
457      {
458#if H_MV       
459        // Bumping after reference picture set has been applied (here after first vcl nalu.
460#if ENC_DEC_TRACE
461        g_bJustDoIt = true; 
462        writeToTraceFile( "Bumping after reference picture set has been applied \n", g_decTracePicOutput  );         
463        g_bJustDoIt = false; 
464#endif
465
466        xWriteOutput( m_tDecTop[decIdxCurrPic]->getListPic(), decIdxCurrPic, nalu.m_temporalId );
467#else
468        xWriteOutput( pcListPic, nalu.m_temporalId );
469#endif
470      }
471    }
472  }
473#if H_MV
474#if H_3D
475  if( m_cCamParsCollector.isInitialized() )
476  {
477    m_cCamParsCollector.setSlice( 0 );
478  }
479#endif
480  for(UInt decIdx = 0; decIdx < m_numDecoders; decIdx++)
481  {
482    xFlushOutput( m_tDecTop[decIdx]->getListPic(), decIdx );
483  }
484#else 
485  xFlushOutput( pcListPic );
486  // delete buffers
487  m_cTDecTop.deletePicBuffer();
488#endif
489     
490  // destroy internal classes
491  xDestroyDecLib();
492}
493
494// ====================================================================================================================
495// Protected member functions
496// ====================================================================================================================
497
498Void TAppDecTop::xCreateDecLib()
499{
500#if H_MV
501  // initialize global variables
502  initROM(); 
503#if H_3D_DIM_DMM
504  initWedgeLists();
505#endif
506#else
507  // create decoder class
508  m_cTDecTop.create();
509#endif
510}
511
512Void TAppDecTop::xDestroyDecLib()
513{
514#if H_MV
515  // destroy ROM
516  destroyROM();
517
518  for(Int decIdx = 0; decIdx < m_numDecoders ; decIdx++)
519  {
520    if( m_tVideoIOYuvReconFile[decIdx] )
521    {
522      m_tVideoIOYuvReconFile[decIdx]->close();
523      delete m_tVideoIOYuvReconFile[decIdx]; 
524      m_tVideoIOYuvReconFile[decIdx] = NULL ;
525    }
526
527    if( m_tDecTop[decIdx] )
528    {
529      m_tDecTop[decIdx]->deletePicBuffer();
530      m_tDecTop[decIdx]->destroy() ;
531    }
532    delete m_tDecTop[decIdx] ; 
533    m_tDecTop[decIdx] = NULL ;
534  }
535#else
536  if ( m_pchReconFile )
537  {
538    m_cTVideoIOYuvReconFile. close();
539  }
540 
541  // destroy decoder class
542  m_cTDecTop.destroy();
543#endif
544#if H_3D
545  m_cCamParsCollector.uninit();
546  if( m_pScaleOffsetFile ) 
547  { 
548    ::fclose( m_pScaleOffsetFile ); 
549  }
550#endif
551}
552
553Void TAppDecTop::xInitDecLib()
554{
555#if !H_MV
556  // initialize decoder class
557  m_cTDecTop.init();
558  m_cTDecTop.setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
559#endif
560}
561
562/** \param pcListPic list of pictures to be written to file
563    \todo            DYN_REF_FREE should be revised
564 */
565#if H_MV
566Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, Int decIdx, Int tId )
567#else
568Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, UInt tId )
569#endif
570{
571
572  if (pcListPic->empty())
573  {
574    return;
575  }
576
577  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
578  Int numPicsNotYetDisplayed = 0;
579  Int dpbFullness = 0;
580#if H_MV
581  TComSPS* activeSPS = m_tDecTop[ decIdx ]->getActiveSPS();
582#else
583  TComSPS* activeSPS = m_cTDecTop.getActiveSPS();
584#endif
585  UInt numReorderPicsHighestTid;
586  UInt maxDecPicBufferingHighestTid;
587  UInt maxNrSublayers = activeSPS->getMaxTLayers();
588
589  if(m_iMaxTemporalLayer == -1 || m_iMaxTemporalLayer >= maxNrSublayers)
590  {
591    numReorderPicsHighestTid = activeSPS->getNumReorderPics(maxNrSublayers-1);
592    maxDecPicBufferingHighestTid =  activeSPS->getMaxDecPicBuffering(maxNrSublayers-1); 
593  }
594  else
595  {
596    numReorderPicsHighestTid = activeSPS->getNumReorderPics(m_iMaxTemporalLayer);
597    maxDecPicBufferingHighestTid = activeSPS->getMaxDecPicBuffering(m_iMaxTemporalLayer); 
598  }
599 
600  while (iterPic != pcListPic->end())
601  {
602    TComPic* pcPic = *(iterPic);
603#if H_MV
604    if(pcPic->getOutputMark() && pcPic->getPOC() > m_pocLastDisplay[decIdx])
605#else
606    if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay)
607#endif
608    {
609      numPicsNotYetDisplayed++;
610      dpbFullness++;
611    }
612    else if(pcPic->getSlice( 0 )->isReferenced())
613    {
614      dpbFullness++;
615    }
616    iterPic++;
617  }
618  iterPic   = pcListPic->begin();
619  if (numPicsNotYetDisplayed>2)
620  {
621    iterPic++;
622  }
623 
624  TComPic* pcPic = *(iterPic);
625  if (numPicsNotYetDisplayed>2 && pcPic->isField()) //Field Decoding
626  {
627    TComList<TComPic*>::iterator endPic   = pcListPic->end();
628    endPic--;
629    iterPic   = pcListPic->begin();
630    while (iterPic != endPic)
631    {
632      TComPic* pcPicTop = *(iterPic);
633      iterPic++;
634      TComPic* pcPicBottom = *(iterPic);
635     
636#if H_MV
637      if ( pcPicTop->getOutputMark() && (numPicsNotYetDisplayed >  pcPicTop->getNumReorderPics(tId) && !(pcPicTop->getPOC()%2) && pcPicBottom->getPOC() == pcPicTop->getPOC()+1)
638          && pcPicBottom->getOutputMark() && (numPicsNotYetDisplayed >  pcPicBottom->getNumReorderPics(tId) && (pcPicTop->getPOC() == m_pocLastDisplay[decIdx]+1 || m_pocLastDisplay[decIdx]<0)))
639#else
640      if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() &&
641          (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid) &&
642          (!(pcPicTop->getPOC()%2) && pcPicBottom->getPOC() == pcPicTop->getPOC()+1) &&
643          (pcPicTop->getPOC() == m_iPOCLastDisplay+1 || m_iPOCLastDisplay < 0))
644#endif
645      {
646        // write to file
647        numPicsNotYetDisplayed = numPicsNotYetDisplayed-2;
648#if H_MV
649      if ( m_pchReconFiles[decIdx] )
650#else
651        if ( m_pchReconFile )
652#endif
653        {
654          const Window &conf = pcPicTop->getConformanceWindow();
655          const Window &defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
656
657          const Bool isTff = pcPicTop->isTopField();
658#if H_MV
659        assert( conf   .getScaledFlag() );
660        assert( defDisp.getScaledFlag() );
661#if ENC_DEC_TRACE
662        g_bJustDoIt = true; 
663        writeToTraceFile( "OutputPic Poc"   , pcPic->getPOC    (), g_decTracePicOutput  ); 
664        writeToTraceFile( "OutputPic LayerId", pcPic->getLayerId(), g_decTracePicOutput );         
665        g_bJustDoIt = false; 
666#endif
667        m_tVideoIOYuvReconFile[decIdx]->write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
668#else
669          m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
670#endif
671                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
672                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
673                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
674                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), isTff );
675        }
676       
677        // update POC of display order
678#if H_MV
679        m_pocLastDisplay[decIdx] = pcPic->getPOC();
680#else
681        m_iPOCLastDisplay = pcPicBottom->getPOC();
682#endif
683       
684        // erase non-referenced picture in the reference picture list after display
685        if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
686        {
687#if !DYN_REF_FREE
688          pcPicTop->setReconMark(false);
689         
690          // mark it should be extended later
691          pcPicTop->getPicYuvRec()->setBorderExtension( false );
692         
693#else
694          pcPicTop->destroy();
695          pcListPic->erase( iterPic );
696          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
697          continue;
698#endif
699        }
700        if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
701        {
702#if !DYN_REF_FREE
703          pcPicBottom->setReconMark(false);
704         
705          // mark it should be extended later
706          pcPicBottom->getPicYuvRec()->setBorderExtension( false );
707         
708#else
709          pcPicBottom->destroy();
710          pcListPic->erase( iterPic );
711          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
712          continue;
713#endif
714        }
715        pcPicTop->setOutputMark(false);
716        pcPicBottom->setOutputMark(false);
717      }
718    }
719  }
720  else if (!pcPic->isField()) //Frame Decoding
721  {
722    iterPic = pcListPic->begin();
723    while (iterPic != pcListPic->end())
724    {
725      pcPic = *(iterPic);
726
727#if H_MV
728      if(pcPic->getOutputMark() && pcPic->getPOC() > m_pocLastDisplay[decIdx] &&
729        (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid))
730#else     
731      if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay &&
732        (numPicsNotYetDisplayed >  numReorderPicsHighestTid || dpbFullness > maxDecPicBufferingHighestTid))
733#endif
734      {
735        // write to file
736        numPicsNotYetDisplayed--;
737        if(pcPic->getSlice(0)->isReferenced() == false)
738        {
739          dpbFullness--;
740        }
741#if H_MV
742      if ( m_pchReconFiles[decIdx] )
743#else
744        if ( m_pchReconFile )
745#endif
746        {
747          const Window &conf = pcPic->getConformanceWindow();
748          const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
749#if H_MV
750        assert( conf   .getScaledFlag() );
751        assert( defDisp.getScaledFlag() );
752#if ENC_DEC_TRACE
753        g_bJustDoIt = true; 
754        writeToTraceFile( "OutputPic Poc"   , pcPic->getPOC    (), g_decTracePicOutput  ); 
755        writeToTraceFile( "OutputPic LayerId", pcPic->getLayerId(), g_decTracePicOutput );         
756        g_bJustDoIt = false; 
757#endif
758        m_tVideoIOYuvReconFile[decIdx]->write( pcPic->getPicYuvRec(),
759#else
760          m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
761#endif
762                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
763                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
764                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
765                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
766        }
767       
768        // update POC of display order
769#if H_MV
770        m_pocLastDisplay[decIdx] = pcPic->getPOC();
771#else
772        m_iPOCLastDisplay = pcPic->getPOC();
773#endif
774       
775        // erase non-referenced picture in the reference picture list after display
776        if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
777        {
778#if !DYN_REF_FREE
779          pcPic->setReconMark(false);
780         
781          // mark it should be extended later
782          pcPic->getPicYuvRec()->setBorderExtension( false );
783         
784#else
785          pcPic->destroy();
786          pcListPic->erase( iterPic );
787          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
788          continue;
789#endif
790        }
791        pcPic->setOutputMark(false);
792#if H_MV
793        pcPic->setPicOutputFlag(false);
794#endif
795      }
796     
797      iterPic++;
798    }
799  }
800}
801/** \param pcListPic list of pictures to be written to file
802    \todo            DYN_REF_FREE should be revised
803 */
804#if H_MV
805Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic, Int decIdx )
806#else
807Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic )
808#endif
809{
810  if(!pcListPic || pcListPic->empty())
811  {
812    return;
813  }
814  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
815 
816  iterPic   = pcListPic->begin();
817  TComPic* pcPic = *(iterPic);
818 
819  if (pcPic->isField()) //Field Decoding
820  {
821    TComList<TComPic*>::iterator endPic   = pcListPic->end();
822    endPic--;
823    TComPic *pcPicTop, *pcPicBottom = NULL;
824    while (iterPic != endPic)
825    {
826      pcPicTop = *(iterPic);
827      iterPic++;
828      pcPicBottom = *(iterPic);
829     
830      if ( pcPicTop->getOutputMark() && pcPicBottom->getOutputMark() && !(pcPicTop->getPOC()%2) && (pcPicBottom->getPOC() == pcPicTop->getPOC()+1) )
831      {
832        // write to file
833#if H_MV
834      if ( m_pchReconFiles[decIdx] )
835#else
836        if ( m_pchReconFile )
837#endif
838        {
839          const Window &conf = pcPicTop->getConformanceWindow();
840          const Window &defDisp = m_respectDefDispWindow ? pcPicTop->getDefDisplayWindow() : Window();
841          const Bool isTff = pcPicTop->isTopField();
842#if H_MV
843        assert( conf   .getScaledFlag() );
844        assert( defDisp.getScaledFlag() );
845#if ENC_DEC_TRACE
846        g_bJustDoIt = true; 
847        writeToTraceFile( "OutputPic Poc"   , pcPic->getPOC    (), g_decTracePicOutput  ); 
848        writeToTraceFile( "OutputPic LayerId", pcPic->getLayerId(), g_decTracePicOutput );         
849        g_bJustDoIt = false; 
850#endif
851        m_tVideoIOYuvReconFile[decIdx]->write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
852#else
853          m_cTVideoIOYuvReconFile.write( pcPicTop->getPicYuvRec(), pcPicBottom->getPicYuvRec(),
854#endif
855                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
856                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
857                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
858                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset(), isTff );
859        }
860       
861        // update POC of display order
862#if H_MV
863      m_pocLastDisplay[decIdx] = pcPic->getPOC();
864#else
865      m_iPOCLastDisplay = pcPicBottom->getPOC();
866#endif       
867        // erase non-referenced picture in the reference picture list after display
868        if ( !pcPicTop->getSlice(0)->isReferenced() && pcPicTop->getReconMark() == true )
869        {
870#if !DYN_REF_FREE
871          pcPicTop->setReconMark(false);
872         
873          // mark it should be extended later
874          pcPicTop->getPicYuvRec()->setBorderExtension( false );
875         
876#else
877          pcPicTop->destroy();
878          pcListPic->erase( iterPic );
879          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
880          continue;
881#endif
882        }
883        if ( !pcPicBottom->getSlice(0)->isReferenced() && pcPicBottom->getReconMark() == true )
884        {
885#if !DYN_REF_FREE
886          pcPicBottom->setReconMark(false);
887         
888          // mark it should be extended later
889          pcPicBottom->getPicYuvRec()->setBorderExtension( false );
890         
891#else
892          pcPicBottom->destroy();
893          pcListPic->erase( iterPic );
894          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
895          continue;
896#endif
897        }
898        pcPicTop->setOutputMark(false);
899        pcPicBottom->setOutputMark(false);
900       
901#if !DYN_REF_FREE
902        if(pcPicTop)
903        {
904          pcPicTop->destroy();
905          delete pcPicTop;
906          pcPicTop = NULL;
907        }
908#endif
909      }
910    }
911    if(pcPicBottom)
912    {
913      pcPicBottom->destroy();
914      delete pcPicBottom;
915      pcPicBottom = NULL;
916    }
917  }
918  else //Frame decoding
919  {
920    while (iterPic != pcListPic->end())
921    {
922      pcPic = *(iterPic);
923     
924      if ( pcPic->getOutputMark() )
925      {
926        // write to file
927#if H_MV
928      if ( m_pchReconFiles[decIdx] )
929#else
930        if ( m_pchReconFile )
931#endif
932        {
933          const Window &conf = pcPic->getConformanceWindow();
934          const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
935#if H_MV
936        assert( conf   .getScaledFlag() );
937        assert( defDisp.getScaledFlag() );
938#if ENC_DEC_TRACE
939        g_bJustDoIt = true; 
940        writeToTraceFile( "OutputPic Poc"   , pcPic->getPOC    (), g_decTracePicOutput  ); 
941        writeToTraceFile( "OutputPic LayerId", pcPic->getLayerId(), g_decTracePicOutput );         
942        g_bJustDoIt = false; 
943#endif
944        m_tVideoIOYuvReconFile[decIdx]->write( pcPic->getPicYuvRec(),
945#else
946          m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
947#endif
948                                        conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
949                                        conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
950                                        conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
951                                        conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
952        }
953       
954        // update POC of display order
955#if H_MV
956      m_pocLastDisplay[decIdx] = pcPic->getPOC();
957#else
958        m_iPOCLastDisplay = pcPic->getPOC();
959#endif
960       
961        // erase non-referenced picture in the reference picture list after display
962        if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
963        {
964#if !DYN_REF_FREE
965          pcPic->setReconMark(false);
966         
967          // mark it should be extended later
968          pcPic->getPicYuvRec()->setBorderExtension( false );
969         
970#else
971          pcPic->destroy();
972          pcListPic->erase( iterPic );
973          iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
974          continue;
975#endif
976        }
977        pcPic->setOutputMark(false);
978#if H_MV
979        pcPic->setPicOutputFlag(false);
980#endif
981      }
982#if !H_MV
983#if !DYN_REF_FREE
984      if(pcPic)
985      {
986        pcPic->destroy();
987        delete pcPic;
988        pcPic = NULL;
989      }
990#endif   
991#endif
992      iterPic++;
993    }
994  }
995#if H_MV
996  m_pocLastDisplay[decIdx] = -MAX_INT;
997#else
998  pcListPic->clear();
999  m_iPOCLastDisplay = -MAX_INT;
1000#endif
1001}
1002
1003/** \param nalu Input nalu to check whether its LayerId is within targetDecLayerIdSet
1004 */
1005Bool TAppDecTop::isNaluWithinTargetDecLayerIdSet( InputNALUnit* nalu )
1006{
1007  if ( m_targetDecLayerIdSet.size() == 0 ) // By default, the set is empty, meaning all LayerIds are allowed
1008  {
1009    return true;
1010  }
1011  for (std::vector<Int>::iterator it = m_targetDecLayerIdSet.begin(); it != m_targetDecLayerIdSet.end(); it++)
1012  {
1013#if H_MV
1014    if ( nalu->m_layerId == (*it) )
1015#else
1016    if ( nalu->m_reservedZero6Bits == (*it) )
1017#endif
1018    {
1019      return true;
1020    }
1021  }
1022  return false;
1023}
1024
1025#if H_MV
1026Int TAppDecTop::xGetDecoderIdx( Int layerId, Bool createFlag /*= false */ )
1027{
1028  Int decIdx = -1; 
1029
1030  if ( layerId > MAX_NUM_LAYER_IDS-1 ) 
1031  {
1032    return decIdx; 
1033  }
1034
1035  if ( m_layerIdToDecIdx[ layerId ] != -1 ) 
1036  {     
1037    decIdx = m_layerIdToDecIdx[ layerId ]; 
1038  }
1039  else
1040  {     
1041    assert ( createFlag ); 
1042    assert( m_numDecoders < MAX_NUM_LAYERS ); 
1043
1044    decIdx = m_numDecoders; 
1045
1046    // Init decoder
1047    m_tDecTop[ decIdx ] =  new TDecTop;
1048    m_tDecTop[ decIdx ]->create();
1049    m_tDecTop[ decIdx ]->init( );
1050    m_tDecTop[ decIdx ]->setLayerId( layerId );
1051    m_tDecTop[ decIdx ]->setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
1052    m_tDecTop[ decIdx ]->setIvPicLists( &m_ivPicLists ); 
1053    m_tDecTop[ decIdx ]->setLayerInitilizedFlags( m_layerInitilizedFlags );
1054    m_tDecTop[ decIdx ]->setTargetOptLayerSetIdx( m_targetOptLayerSetIdx );   
1055#if H_3D_ANNEX_SELECTION_FIX
1056    m_tDecTop[ decIdx ]->setProfileIdc           ( );   
1057#endif
1058
1059#if H_3D
1060   m_tDecTop[ decIdx ]->setCamParsCollector( &m_cCamParsCollector );
1061#endif
1062
1063    // append pic list of new decoder to PicLists
1064    assert( m_ivPicLists.size() == m_numDecoders );
1065    m_ivPicLists.push_back( m_tDecTop[ decIdx ]->getListPic() );
1066
1067    // create recon file related stuff     
1068    Char* pchTempFilename = NULL;
1069    if ( m_pchReconFile )
1070    {     
1071      Char buffer[4];     
1072      sprintf(buffer,"_%i", layerId );
1073      assert ( m_pchReconFile ); 
1074      xAppendToFileNameEnd( m_pchReconFile , buffer, pchTempFilename );
1075      assert( m_pchReconFiles.size() == m_numDecoders );
1076    }
1077
1078    m_pchReconFiles.push_back( pchTempFilename );   
1079
1080    m_tVideoIOYuvReconFile[ decIdx ] = new TVideoIOYuv;
1081    m_reconOpen           [ decIdx ] = false;
1082
1083    // set others
1084    m_pocLastDisplay      [ decIdx ] = -MAX_INT;
1085    m_layerIdToDecIdx     [ layerId ] = decIdx; 
1086
1087    m_numDecoders++; 
1088  };
1089  return decIdx;
1090
1091}
1092
1093Void TAppDecTop::xMarkForOutput( Bool allLayersDecoded, Int pocLastPic, Int layerIdLastPic )
1094{ 
1095  vector<Int> targetOptLayerIdList = m_vps->getTargetOptLayerIdList( m_targetOptLayerSetIdx );
1096
1097  if (m_vps->getAltOutputLayerFlagVar( m_targetOptLayerSetIdx ) )
1098  {
1099    assert( targetOptLayerIdList.size() == 1 ); 
1100    Int targetLayerId = targetOptLayerIdList[0];     
1101
1102    TComPic* curPic = m_ivPicLists.getPic( layerIdLastPic, pocLastPic );
1103    assert( curPic != NULL );
1104
1105    if ( layerIdLastPic == targetLayerId )
1106    {
1107      if ( curPic->getPicOutputFlag() )
1108      {
1109        curPic->setOutputMark( true );
1110      }
1111      else
1112      {       
1113        xMarkAltOutPic( targetLayerId, pocLastPic ); 
1114      }
1115      m_markedForOutput = true; 
1116    }
1117    else if ( ( layerIdLastPic > targetLayerId || allLayersDecoded ) && !m_markedForOutput )
1118    {
1119      xMarkAltOutPic( targetLayerId, pocLastPic );
1120    }
1121
1122    if ( allLayersDecoded )
1123    {
1124      m_markedForOutput = false; 
1125    }
1126  }
1127  else
1128  { 
1129    for( Int dI = 0; dI < m_numDecoders; dI++ )
1130    {     
1131      Int layerId = m_tDecTop[dI]->getLayerId(); 
1132      TComPic* curPic = m_ivPicLists.getPic( layerId, pocLastPic );
1133      if ( curPic != NULL )
1134      {
1135        if ( curPic->getReconMark() )
1136        {
1137          Bool isTargetOptLayer = std::find(targetOptLayerIdList.begin(), targetOptLayerIdList.end(), layerId) != targetOptLayerIdList.end();
1138          curPic->setOutputMark( isTargetOptLayer ? curPic->getPicOutputFlag() : false ); 
1139        }
1140      }
1141    }
1142  }
1143}
1144
1145Void TAppDecTop::xMarkAltOutPic( Int targetOutputLayer, Int pocLastPic )
1146{
1147  Int optLayerIdxInVps = m_vps->getLayerIdInNuh( targetOutputLayer ); 
1148  Int highestNuhLayerId = -1; 
1149  TComPic* picWithHighestNuhLayerId = NULL; 
1150  for (Int dIdx = 0; dIdx < m_numDecoders; dIdx++)
1151  {
1152    Int curLayerId = m_tDecTop[dIdx]->getLayerId();
1153    Int curLayerIdxInVps = m_vps->getLayerIdInNuh( curLayerId  ); 
1154    if ( m_vps->getDependencyFlag(optLayerIdxInVps, curLayerIdxInVps ) )
1155    {
1156      TComPic* curPic = m_ivPicLists.getPic( curLayerId, pocLastPic ); 
1157      if (curPic != NULL)
1158      {
1159        if (curPic->getReconMark() && curPic->getPicOutputFlag() )
1160        {
1161          curPic->setOutputMark   ( false ); 
1162          curPic->setPicOutputFlag( false ); 
1163          if ( curLayerId > highestNuhLayerId)
1164          {
1165            highestNuhLayerId = curLayerId ; 
1166            picWithHighestNuhLayerId = curPic; 
1167          }           
1168        }
1169      }
1170    }
1171  }
1172  if ( picWithHighestNuhLayerId != NULL )
1173  {
1174    picWithHighestNuhLayerId->setPicOutputFlag(true); 
1175    picWithHighestNuhLayerId->setOutputMark   (true); 
1176  }
1177}
1178
1179#endif
1180//! \}
Note: See TracBrowser for help on using the repository browser.