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

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

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