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

Last change on this file since 364 was 364, checked in by tech, 12 years ago

Further cleanups

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