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

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

Further minor cleanups.

  • Property svn:eol-style set to native
File size: 12.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: m_iPOCLastDisplay(-MAX_INT)
57{
58  ::memset (m_abDecFlag, 0, sizeof (m_abDecFlag));
59}
60
61Void TAppDecTop::create()
62{
63}
64
65Void TAppDecTop::destroy()
66{
67  if (m_pchBitstreamFile)
68  {
69    free (m_pchBitstreamFile);
70    m_pchBitstreamFile = NULL;
71  }
72  if (m_pchReconFile)
73  {
74    free (m_pchReconFile);
75    m_pchReconFile = NULL;
76  }
77}
78
79// ====================================================================================================================
80// Public member functions
81// ====================================================================================================================
82
83/**
84 - create internal class
85 - initialize internal class
86 - until the end of the bitstream, call decoding function in TDecTop class
87 - delete allocated buffers
88 - destroy internal class
89 .
90 */
91Void TAppDecTop::decode()
92{
93  Int                 poc;
94  TComList<TComPic*>* pcListPic = NULL;
95
96  ifstream bitstreamFile(m_pchBitstreamFile, ifstream::in | ifstream::binary);
97  if (!bitstreamFile)
98  {
99    fprintf(stderr, "\nfailed to open bitstream file `%s' for reading\n", m_pchBitstreamFile);
100    exit(EXIT_FAILURE);
101  }
102
103  InputByteStream bytestream(bitstreamFile);
104
105  // create & initialize internal classes
106  xCreateDecLib();
107  xInitDecLib  ();
108  m_iPOCLastDisplay += m_iSkipFrame;      // set the last displayed POC correctly for skip forward.
109
110  // main decoder loop
111  Bool recon_opened = false; // reconstruction file not yet opened. (must be performed after SPS is seen)
112 
113  while (!!bitstreamFile)
114  {
115    /* location serves to work around a design fault in the decoder, whereby
116     * the process of reading a new slice that is the first slice of a new frame
117     * requires the TDecTop::decode() method to be called again with the same
118     * nal unit. */
119    streampos location = bitstreamFile.tellg();
120    AnnexBStats stats = AnnexBStats();
121    Bool bPreviousPictureDecoded = false;
122
123    vector<uint8_t> nalUnit;
124    InputNALUnit nalu;
125    byteStreamNALUnit(bytestream, nalUnit, stats);
126
127    // call actual decoding function
128    Bool bNewPicture = false;
129    if (nalUnit.empty())
130    {
131      /* this can happen if the following occur:
132       *  - empty input file
133       *  - two back-to-back start_code_prefixes
134       *  - start_code_prefix immediately followed by EOF
135       */
136      fprintf(stderr, "Warning: Attempt to decode an empty NAL unit\n");
137    }
138    else
139    {
140      read(nalu, nalUnit);
141      if( (m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) || !isNaluWithinTargetDecLayerIdSet(&nalu)  )
142      {
143        if(bPreviousPictureDecoded)
144        {
145          bNewPicture = true;
146          bPreviousPictureDecoded = false;
147        }
148        else
149        {
150          bNewPicture = false;
151        }
152      }
153      else
154      {
155        bNewPicture = m_cTDecTop.decode(nalu, m_iSkipFrame, m_iPOCLastDisplay);
156        if (bNewPicture)
157        {
158          bitstreamFile.clear();
159          /* location points to the current nalunit payload[1] due to the
160           * need for the annexB parser to read three extra bytes.
161           * [1] except for the first NAL unit in the file
162           *     (but bNewPicture doesn't happen then) */
163          bitstreamFile.seekg(location-streamoff(3));
164          bytestream.reset();
165        }
166        bPreviousPictureDecoded = true; 
167      }
168    }
169    if (bNewPicture || !bitstreamFile)
170    {
171      m_cTDecTop.executeLoopFilters(poc, pcListPic);
172    }
173
174    if( pcListPic )
175    {
176      if ( m_pchReconFile && !recon_opened )
177      {
178        if (!m_outputBitDepthY) { m_outputBitDepthY = g_bitDepthY; }
179        if (!m_outputBitDepthC) { m_outputBitDepthC = g_bitDepthC; }
180
181        m_cTVideoIOYuvReconFile.open( m_pchReconFile, true, m_outputBitDepthY, m_outputBitDepthC, g_bitDepthY, g_bitDepthC ); // write mode
182        recon_opened = true;
183      }
184      if ( bNewPicture && 
185           (   nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
186            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
187            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP
188            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL
189            || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP ) )
190      {
191        xFlushOutput( pcListPic );
192      }
193      // write reconstruction to file
194      if(bNewPicture)
195      {
196        xWriteOutput( pcListPic, nalu.m_temporalId );
197      }
198    }
199  }
200 
201  xFlushOutput( pcListPic );
202  // delete buffers
203  m_cTDecTop.deletePicBuffer();
204     
205  // destroy internal classes
206  xDestroyDecLib();
207}
208
209// ====================================================================================================================
210// Protected member functions
211// ====================================================================================================================
212
213Void TAppDecTop::xCreateDecLib()
214{
215  // create decoder class
216  m_cTDecTop.create();
217}
218
219Void TAppDecTop::xDestroyDecLib()
220{
221  if ( m_pchReconFile )
222  {
223    m_cTVideoIOYuvReconFile. close();
224  }
225 
226  // destroy decoder class
227  m_cTDecTop.destroy();
228}
229
230Void TAppDecTop::xInitDecLib()
231{
232  // initialize decoder class
233  m_cTDecTop.init();
234  m_cTDecTop.setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
235}
236
237/** \param pcListPic list of pictures to be written to file
238    \todo            DYN_REF_FREE should be revised
239 */
240Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, UInt tId )
241{
242  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
243  Int not_displayed = 0;
244
245  while (iterPic != pcListPic->end())
246  {
247    TComPic* pcPic = *(iterPic);
248    if(pcPic->getOutputMark() && pcPic->getPOC() > m_iPOCLastDisplay)
249    {
250       not_displayed++;
251    }
252    iterPic++;
253  }
254  iterPic   = pcListPic->begin();
255 
256  while (iterPic != pcListPic->end())
257  {
258    TComPic* pcPic = *(iterPic);
259   
260    if ( pcPic->getOutputMark() && (not_displayed >  pcPic->getNumReorderPics(tId) && pcPic->getPOC() > m_iPOCLastDisplay))
261    {
262      // write to file
263       not_displayed--;
264      if ( m_pchReconFile )
265      {
266        const Window &conf = pcPic->getConformanceWindow();
267        const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
268        m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
269                                       conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
270                                       conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
271                                       conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
272                                       conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
273      }
274     
275      // update POC of display order
276      m_iPOCLastDisplay = pcPic->getPOC();
277     
278      // erase non-referenced picture in the reference picture list after display
279      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
280      {
281#if !DYN_REF_FREE
282        pcPic->setReconMark(false);
283       
284        // mark it should be extended later
285        pcPic->getPicYuvRec()->setBorderExtension( false );
286       
287#else
288        pcPic->destroy();
289        pcListPic->erase( iterPic );
290        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
291        continue;
292#endif
293      }
294      pcPic->setOutputMark(false);
295    }
296   
297    iterPic++;
298  }
299}
300
301/** \param pcListPic list of pictures to be written to file
302    \todo            DYN_REF_FREE should be revised
303 */
304Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic )
305{
306  if(!pcListPic)
307  {
308    return;
309  } 
310  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
311
312  iterPic   = pcListPic->begin();
313 
314  while (iterPic != pcListPic->end())
315  {
316    TComPic* pcPic = *(iterPic);
317
318    if ( pcPic->getOutputMark() )
319    {
320      // write to file
321      if ( m_pchReconFile )
322      {
323        const Window &conf = pcPic->getConformanceWindow();
324        const Window &defDisp = m_respectDefDispWindow ? pcPic->getDefDisplayWindow() : Window();
325        m_cTVideoIOYuvReconFile.write( pcPic->getPicYuvRec(),
326                                       conf.getWindowLeftOffset() + defDisp.getWindowLeftOffset(),
327                                       conf.getWindowRightOffset() + defDisp.getWindowRightOffset(),
328                                       conf.getWindowTopOffset() + defDisp.getWindowTopOffset(),
329                                       conf.getWindowBottomOffset() + defDisp.getWindowBottomOffset() );
330      }
331     
332      // update POC of display order
333      m_iPOCLastDisplay = pcPic->getPOC();
334     
335      // erase non-referenced picture in the reference picture list after display
336      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
337      {
338#if !DYN_REF_FREE
339        pcPic->setReconMark(false);
340       
341        // mark it should be extended later
342        pcPic->getPicYuvRec()->setBorderExtension( false );
343       
344#else
345        pcPic->destroy();
346        pcListPic->erase( iterPic );
347        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
348        continue;
349#endif
350      }
351      pcPic->setOutputMark(false);
352    }
353#if !DYN_REF_FREE
354    if(pcPic)
355    {
356      pcPic->destroy();
357      delete pcPic;
358      pcPic = NULL;
359    }
360#endif
361    iterPic++;
362  }
363  pcListPic->clear();
364  m_iPOCLastDisplay = -MAX_INT;
365}
366
367/** \param nalu Input nalu to check whether its LayerId is within targetDecLayerIdSet
368 */
369Bool TAppDecTop::isNaluWithinTargetDecLayerIdSet( InputNALUnit* nalu )
370{
371  if ( m_targetDecLayerIdSet.size() == 0 ) // By default, the set is empty, meaning all LayerIds are allowed
372  {
373    return true;
374  }
375  for (std::vector<Int>::iterator it = m_targetDecLayerIdSet.begin(); it != m_targetDecLayerIdSet.end(); it++)
376  {
377    if ( nalu->m_reservedZero6Bits == (*it) )
378    {
379      return true;
380    }
381  }
382  return false;
383}
384
385//! \}
Note: See TracBrowser for help on using the repository browser.