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

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

Fixed wrongly merged parts.

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