source: 3DVCSoftware/branches/HTM-DEV-0.1-dev/source/App/TAppDecoder/TAppDecTop.cpp @ 547

Last change on this file since 547 was 401, checked in by tech, 12 years ago
  • Fixed trace files for MV-HEVC.
  • Fixed assertion mismatch due to NumPocTotalCurr.
  • Property svn:eol-style set to native
File size: 18.6 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2013, 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  ::memset (m_abDecFlag, 0, sizeof (m_abDecFlag));
63#if H_MV
64  for (Int i = 0; i < MAX_NUM_LAYER_IDS; i++) m_layerIdToDecIdx[i] = -1; 
65#endif
66}
67
68Void TAppDecTop::create()
69{
70}
71
72Void TAppDecTop::destroy()
73{
74  if (m_pchBitstreamFile)
75  {
76    free (m_pchBitstreamFile);
77    m_pchBitstreamFile = NULL;
78  }
79#if H_MV
80  for (Int decIdx = 0; decIdx < m_numDecoders; decIdx++)
81  {
82    if (m_pchReconFiles[decIdx])
83    {
84      free (m_pchReconFiles[decIdx]);
85      m_pchReconFiles[decIdx] = NULL;
86    }
87  }
88#endif
89  if (m_pchReconFile)
90  {
91    free (m_pchReconFile);
92    m_pchReconFile = NULL;
93  }
94}
95
96// ====================================================================================================================
97// Public member functions
98// ====================================================================================================================
99
100/**
101 - create internal class
102 - initialize internal class
103 - until the end of the bitstream, call decoding function in TDecTop class
104 - delete allocated buffers
105 - destroy internal class
106 .
107 */
108Void TAppDecTop::decode()
109{
110  Int                 poc;
111#if H_MV
112  poc = -1; 
113#endif
114  TComList<TComPic*>* pcListPic = NULL;
115
116  ifstream bitstreamFile(m_pchBitstreamFile, ifstream::in | ifstream::binary);
117  if (!bitstreamFile)
118  {
119    fprintf(stderr, "\nfailed to open bitstream file `%s' for reading\n", m_pchBitstreamFile);
120    exit(EXIT_FAILURE);
121  }
122
123  InputByteStream bytestream(bitstreamFile);
124
125  // create & initialize internal classes
126  xCreateDecLib();
127  xInitDecLib  ();
128#if !H_MV
129  m_iPOCLastDisplay += m_iSkipFrame;      // set the last displayed POC correctly for skip forward.
130
131  // main decoder loop
132  Bool recon_opened = false; // reconstruction file not yet opened. (must be performed after SPS is seen)
133#else
134  Int  pocCurrPic        = -MAX_INT;     
135  Int  pocLastPic        = -MAX_INT;   
136 
137  Int  layerIdLastPic    = 0; 
138  Int  layerIdCurrPic    = 0; 
139
140  Int  decIdxLastPic     = 0; 
141  Int  decIdxCurrPic     = 0; 
142
143  Bool firstSlice        = true; 
144#endif
145 
146  while (!!bitstreamFile)
147  {
148    /* location serves to work around a design fault in the decoder, whereby
149     * the process of reading a new slice that is the first slice of a new frame
150     * requires the TDecTop::decode() method to be called again with the same
151     * nal unit. */
152    streampos location = bitstreamFile.tellg();
153#if H_MV
154#if ENC_DEC_TRACE
155    Int64 symCount = g_nSymbolCounter;
156#endif
157#endif
158    AnnexBStats stats = AnnexBStats();
159#if !H_MV
160    Bool bPreviousPictureDecoded = false;
161#endif
162
163    vector<uint8_t> nalUnit;
164    InputNALUnit nalu;
165    byteStreamNALUnit(bytestream, nalUnit, stats);
166
167    // call actual decoding function
168    Bool bNewPicture = false;
169#if H_MV
170    Bool newSliceDiffPoc   = false;
171    Bool newSliceDiffLayer = false;
172    Bool allLayersDecoded  = false;     
173#endif
174    if (nalUnit.empty())
175    {
176      /* this can happen if the following occur:
177       *  - empty input file
178       *  - two back-to-back start_code_prefixes
179       *  - start_code_prefix immediately followed by EOF
180       */
181      fprintf(stderr, "Warning: Attempt to decode an empty NAL unit\n");
182    }
183    else
184    {
185      read(nalu, nalUnit);
186#if H_MV     
187      Int decIdx     = xGetDecoderIdx( nalu.m_layerId , true );
188     
189      if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) || !isNaluWithinTargetDecLayerIdSet(&nalu) )
190      {
191        bNewPicture = false;
192      }
193      else
194      { 
195        newSliceDiffLayer = nalu.isSlice() && ( nalu.m_layerId != layerIdCurrPic ) && !firstSlice;
196        newSliceDiffPoc   = m_tDecTop[decIdx]->decode(nalu, m_iSkipFrame, m_pocLastDisplay[decIdx], newSliceDiffLayer );
197        // decode function only returns true when all of the following conditions are true
198        // - poc in particular layer changes
199        // - nalu does not belong to first slice in layer
200        // - nalu.isSlice() == true     
201
202        bNewPicture       = newSliceDiffLayer || newSliceDiffPoc; 
203
204        if ( nalu.isSlice() && firstSlice )
205        {
206          layerIdCurrPic = nalu.m_layerId; 
207          pocCurrPic     = m_tDecTop[decIdx]->getCurrPoc(); 
208          decIdxCurrPic  = decIdx; 
209          firstSlice     = false; 
210        }
211
212        if ( bNewPicture || !bitstreamFile )
213        { 
214          layerIdLastPic    = layerIdCurrPic; 
215          layerIdCurrPic    = nalu.m_layerId; 
216         
217          pocLastPic        = pocCurrPic; 
218          pocCurrPic        = m_tDecTop[decIdx]->getCurrPoc(); 
219         
220          decIdxLastPic     = decIdxCurrPic; 
221          decIdxCurrPic     = decIdx; 
222
223          allLayersDecoded = ( pocCurrPic != pocLastPic );
224        }
225
226       
227#else
228      if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) || !isNaluWithinTargetDecLayerIdSet(&nalu)  )
229      {
230        if(bPreviousPictureDecoded)
231        {
232          bNewPicture = true;
233          bPreviousPictureDecoded = false;
234        }
235        else
236        {
237          bNewPicture = false;
238        }
239      }
240      else
241      {
242        bNewPicture = m_cTDecTop.decode(nalu, m_iSkipFrame, m_iPOCLastDisplay);
243#endif
244        if (bNewPicture)
245        {
246          bitstreamFile.clear();
247          /* location points to the current nalunit payload[1] due to the
248           * need for the annexB parser to read three extra bytes.
249           * [1] except for the first NAL unit in the file
250           *     (but bNewPicture doesn't happen then) */
251          bitstreamFile.seekg(location-streamoff(3));
252          bytestream.reset();
253#if H_MV
254#if ENC_DEC_TRACE
255          g_nSymbolCounter = symCount;
256#endif
257#endif
258
259        }
260#if !H_MV
261        bPreviousPictureDecoded = true; 
262#endif
263      }
264    }
265    if (bNewPicture || !bitstreamFile)
266    {
267#if H_MV
268      assert( decIdxLastPic != -1 ); 
269      m_tDecTop[decIdxLastPic]->endPicDecoding(poc, pcListPic, m_targetDecLayerIdSet );
270#else
271      m_cTDecTop.executeLoopFilters(poc, pcListPic);
272#endif
273    }
274#if H_3D
275    if ( allLayersDecoded || !bitstreamFile )
276    {
277      for( Int dI = 0; dI < m_numDecoders; dI++ )
278      {
279        TComPic* picLastCoded = m_ivPicLists.getPic( m_tDecTop[dI]->getLayerId(), pocLastPic );
280        assert( picLastCoded != NULL );       
281        picLastCoded->compressMotion();         
282      }
283    }
284#endif
285
286    if( pcListPic )
287    {
288#if H_MV
289      if ( m_pchReconFiles[decIdxLastPic] && !m_reconOpen[decIdxLastPic] )
290#else
291      if ( m_pchReconFile && !recon_opened )
292#endif
293      {
294        if (!m_outputBitDepthY) { m_outputBitDepthY = g_bitDepthY; }
295        if (!m_outputBitDepthC) { m_outputBitDepthC = g_bitDepthC; }
296
297#if H_MV
298        m_tVideoIOYuvReconFile[decIdxLastPic]->open( m_pchReconFiles[decIdxLastPic], true, m_outputBitDepthY, m_outputBitDepthC, g_bitDepthY, g_bitDepthC ); // write mode
299        m_reconOpen[decIdxLastPic] = true;
300      }
301      if ( bNewPicture && newSliceDiffPoc && 
302#else
303        m_cTVideoIOYuvReconFile.open( m_pchReconFile, true, m_outputBitDepthY, m_outputBitDepthC, g_bitDepthY, g_bitDepthC ); // write mode
304        recon_opened = true;
305      }
306      if ( bNewPicture && 
307#endif
308           (   nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
309            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
310            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP
311            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL
312            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP ) )
313      {
314#if H_MV
315        xFlushOutput( pcListPic, decIdxLastPic );
316#else
317        xFlushOutput( pcListPic );
318#endif
319      }
320      // write reconstruction to file
321      if(bNewPicture)
322      {
323#if H_MV
324        xWriteOutput( pcListPic, decIdxLastPic, nalu.m_temporalId );
325      }
326    }
327  }
328
329  for(UInt decIdx = 0; decIdx < m_numDecoders; decIdx++)
330  {
331    xFlushOutput( m_tDecTop[decIdx]->getListPic(), decIdx );
332  }
333#else
334        xWriteOutput( pcListPic, nalu.m_temporalId );
335      }
336    }
337  }
338 
339  xFlushOutput( pcListPic );
340  // delete buffers
341  m_cTDecTop.deletePicBuffer();
342#endif
343     
344  // destroy internal classes
345  xDestroyDecLib();
346}
347
348// ====================================================================================================================
349// Protected member functions
350// ====================================================================================================================
351
352Void TAppDecTop::xCreateDecLib()
353{
354#if H_MV
355  // initialize global variables
356  initROM(); 
357#else
358  // create decoder class
359  m_cTDecTop.create();
360#endif
361}
362
363Void TAppDecTop::xDestroyDecLib()
364{
365#if H_MV
366  // destroy ROM
367  destroyROM();
368
369  for(Int decIdx = 0; decIdx < m_numDecoders ; decIdx++)
370  {
371    if( m_tVideoIOYuvReconFile[decIdx] )
372    {
373      m_tVideoIOYuvReconFile[decIdx]->close();
374      delete m_tVideoIOYuvReconFile[decIdx]; 
375      m_tVideoIOYuvReconFile[decIdx] = NULL ;
376    }
377
378    if( m_tDecTop[decIdx] )
379    {
380      m_tDecTop[decIdx]->deletePicBuffer();
381      m_tDecTop[decIdx]->destroy() ;
382    }
383    delete m_tDecTop[decIdx] ; 
384    m_tDecTop[decIdx] = NULL ;
385  }
386#else
387  if ( m_pchReconFile )
388  {
389    m_cTVideoIOYuvReconFile. close();
390  }
391 
392  // destroy decoder class
393  m_cTDecTop.destroy();
394#endif
395}
396
397Void TAppDecTop::xInitDecLib()
398{
399#if !H_MV
400  // initialize decoder class
401  m_cTDecTop.init();
402  m_cTDecTop.setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
403#endif
404}
405
406/** \param pcListPic list of pictures to be written to file
407    \todo            DYN_REF_FREE should be revised
408 */
409#if H_MV
410Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, Int decIdx, Int tId )
411#else
412Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, UInt tId )
413#endif
414{
415  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
416  Int not_displayed = 0;
417
418  while (iterPic != pcListPic->end())
419  {
420    TComPic* pcPic = *(iterPic);
421#if H_MV
422    if(pcPic->getOutputMark() && pcPic->getPOC() > m_pocLastDisplay[decIdx])
423#else
424    if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay)
425#endif
426    {
427       not_displayed++;
428    }
429    iterPic++;
430  }
431  iterPic   = pcListPic->begin();
432 
433  while (iterPic != pcListPic->end())
434  {
435    TComPic* pcPic = *(iterPic);
436   
437#if H_MV
438    if ( pcPic->getOutputMark() && (not_displayed >  pcPic->getNumReorderPics(tId) && pcPic->getPOC() > m_pocLastDisplay[decIdx]))
439#else
440    if ( pcPic->getOutputMark() && (not_displayed >  pcPic->getNumReorderPics(tId) && pcPic->getPOC() > m_iPOCLastDisplay))
441#endif
442    {
443      // write to file
444       not_displayed--;
445#if H_MV
446      if ( m_pchReconFiles[decIdx] )
447#else
448      if ( m_pchReconFile )
449#endif
450      {
451        const Window &conf = pcPic->getConformanceWindow();
452        const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
453#if H_MV
454        m_tVideoIOYuvReconFile[decIdx]->write( pcPic->getPicYuvRec(),
455#else
456        m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
457#endif
458                                       conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
459                                       conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
460                                       conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
461                                       conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
462      }
463     
464      // update POC of display order
465#if H_MV
466      m_pocLastDisplay[decIdx] = pcPic->getPOC();
467#else
468      m_iPOCLastDisplay = pcPic->getPOC();
469#endif
470     
471      // erase non-referenced picture in the reference picture list after display
472      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
473      {
474#if !DYN_REF_FREE
475        pcPic->setReconMark(false);
476       
477        // mark it should be extended later
478        pcPic->getPicYuvRec()->setBorderExtension( false );
479       
480#else
481        pcPic->destroy();
482        pcListPic->erase( iterPic );
483        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
484        continue;
485#endif
486      }
487      pcPic->setOutputMark(false);
488    }
489   
490    iterPic++;
491  }
492}
493
494/** \param pcListPic list of pictures to be written to file
495    \todo            DYN_REF_FREE should be revised
496 */
497#if H_MV
498Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic, Int decIdx )
499#else
500Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic )
501#endif
502{
503  if(!pcListPic)
504  {
505    return;
506  } 
507  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
508
509  iterPic   = pcListPic->begin();
510 
511  while (iterPic != pcListPic->end())
512  {
513    TComPic* pcPic = *(iterPic);
514
515    if ( pcPic->getOutputMark() )
516    {
517      // write to file
518#if H_MV
519      if ( m_pchReconFiles[decIdx] )
520#else
521      if ( m_pchReconFile )
522#endif
523      {
524        const Window &conf = pcPic->getConformanceWindow();
525        const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
526#if H_MV
527        m_tVideoIOYuvReconFile[decIdx]->write( pcPic->getPicYuvRec(),
528#else
529        m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
530#endif
531                                       conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
532                                       conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
533                                       conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
534                                       conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
535      }
536     
537      // update POC of display order
538#if H_MV
539      m_pocLastDisplay[decIdx] = pcPic->getPOC();
540#else
541      m_iPOCLastDisplay = pcPic->getPOC();
542#endif
543     
544      // erase non-referenced picture in the reference picture list after display
545      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
546      {
547#if !DYN_REF_FREE
548        pcPic->setReconMark(false);
549       
550        // mark it should be extended later
551        pcPic->getPicYuvRec()->setBorderExtension( false );
552       
553#else
554        pcPic->destroy();
555        pcListPic->erase( iterPic );
556        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
557        continue;
558#endif
559      }
560      pcPic->setOutputMark(false);
561    }
562#if !H_MV
563#if !DYN_REF_FREE
564    if(pcPic)
565    {
566      pcPic->destroy();
567      delete pcPic;
568      pcPic = NULL;
569    }
570#endif
571#endif
572    iterPic++;
573  }
574#if H_MV
575  m_pocLastDisplay[decIdx] = -MAX_INT;
576#else
577  pcListPic->clear();
578  m_iPOCLastDisplay = -MAX_INT;
579#endif
580}
581
582/** \param nalu Input nalu to check whether its LayerId is within targetDecLayerIdSet
583 */
584Bool TAppDecTop::isNaluWithinTargetDecLayerIdSet( InputNALUnit* nalu )
585{
586  if ( m_targetDecLayerIdSet.size() == 0 ) // By default, the set is empty, meaning all LayerIds are allowed
587  {
588    return true;
589  }
590  for (std::vector<Int>::iterator it = m_targetDecLayerIdSet.begin(); it != m_targetDecLayerIdSet.end(); it++)
591  {
592#if H_MV
593    if ( nalu->m_layerId == (*it) )
594#else
595    if ( nalu->m_reservedZero6Bits == (*it) )
596#endif
597    {
598      return true;
599    }
600  }
601  return false;
602}
603
604#if H_MV
605Int TAppDecTop::xGetDecoderIdx( Int layerId, Bool createFlag /*= false */ )
606{
607  Int decIdx = -1; 
608  if ( m_layerIdToDecIdx[ layerId ] != -1 ) 
609  {     
610    decIdx = m_layerIdToDecIdx[ layerId ]; 
611  }
612  else
613  {     
614    assert ( createFlag ); 
615    assert( m_numDecoders < MAX_NUM_LAYERS ); 
616
617    decIdx = m_numDecoders; 
618
619    // Init decoder
620    m_tDecTop[ decIdx ] =  new TDecTop;
621    m_tDecTop[ decIdx ]->create();
622    m_tDecTop[ decIdx ]->init( );
623    m_tDecTop[ decIdx ]->setLayerId( layerId );
624    m_tDecTop[ decIdx ]->setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
625    m_tDecTop[ decIdx ]->setIvPicLists( &m_ivPicLists ); 
626
627    // append pic list of new decoder to PicLists
628    assert( m_ivPicLists.size() == m_numDecoders );
629    m_ivPicLists.push_back( m_tDecTop[ decIdx ]->getListPic() );
630
631    // create recon file related stuff     
632    Char* pchTempFilename = NULL;
633    if ( m_pchReconFile )
634    {     
635      Char buffer[4];     
636      sprintf(buffer,"_%i", layerId );
637      assert ( m_pchReconFile ); 
638      xAppendToFileNameEnd( m_pchReconFile , buffer, pchTempFilename );
639      assert( m_pchReconFiles.size() == m_numDecoders );
640    }
641
642    m_pchReconFiles.push_back( pchTempFilename );   
643
644    m_tVideoIOYuvReconFile[ decIdx ] = new TVideoIOYuv;
645    m_reconOpen           [ decIdx ] = false;
646
647    // set others
648    m_pocLastDisplay      [ decIdx ] = -MAX_INT;
649    m_layerIdToDecIdx     [ layerId ] = decIdx; 
650
651    m_numDecoders++; 
652  };
653  return decIdx;
654}
655#endif
656//! \}
Note: See TracBrowser for help on using the repository browser.