source: 3DVCSoftware/tags/HTM-DEV-0.1/source/App/TAppDecoder/TAppDecTop.cpp @ 324

Last change on this file since 324 was 324, checked in by tech, 11 years ago

Initial development version for update to latest HM version.
Includes MV-HEVC and basic extensions for 3D-HEVC.

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