source: 3DVCSoftware/branches/HTM-3.0-Vidyo/source/App/TAppDecoder/TAppDecTop.cpp @ 70

Last change on this file since 70 was 70, checked in by vidyo, 12 years ago
  • Property svn:eol-style set to native
File size: 15.8 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-2012, 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  ::memset (m_abDecFlag, 0, sizeof (m_abDecFlag));
58  m_useDepth = false;
59  m_pScaleOffsetFile  = 0;
60}
61
62Void TAppDecTop::create()
63{
64}
65
66Void TAppDecTop::destroy()
67{
68}
69
70// ====================================================================================================================
71// Public member functions
72// ====================================================================================================================
73
74/**
75 - create internal class
76 - initialize internal class
77 - until the end of the bitstream, call decoding function in TDecTop class
78 - delete allocated buffers
79 - destroy internal class
80 .
81 */
82Void TAppDecTop::decode()
83{
84  increaseNumberOfViews( 1 );
85#if SONY_COLPIC_AVAILABILITY
86  m_tDecTop[0]->setViewOrderIdx(0);
87#endif
88  Int                 viewDepthId = 0;
89  Int                 previousViewDepthId  = 0;
90  UInt                uiPOC[MAX_VIEW_NUM*2];
91  TComList<TComPic*>* pcListPic[MAX_VIEW_NUM*2];
92  Bool                newPicture[MAX_VIEW_NUM*2];
93  Bool                previousPictureDecoded = false;
94  for( Int i = 0; i < MAX_VIEW_NUM*2; i++ )
95  {
96    uiPOC[i] = 0;
97    pcListPic[i] = NULL;
98    newPicture[i] = false;
99  }
100
101  ifstream bitstreamFile(m_pchBitstreamFile, ifstream::in | ifstream::binary);
102  if (!bitstreamFile)
103  {
104    fprintf(stderr, "\nfailed to open bitstream file `%s' for reading\n", m_pchBitstreamFile);
105    exit(EXIT_FAILURE);
106  }
107
108  if( m_pchScaleOffsetFile ) 
109  { 
110    m_pScaleOffsetFile = ::fopen( m_pchScaleOffsetFile, "wt" ); 
111    AOF( m_pScaleOffsetFile ); 
112  }
113  m_cCamParsCollector.init( m_pScaleOffsetFile );
114
115  InputByteStream bytestream(bitstreamFile);
116
117  while (!!bitstreamFile)
118  {
119    /* location serves to work around a design fault in the decoder, whereby
120     * the process of reading a new slice that is the first slice of a new frame
121     * requires the TDecTop::decode() method to be called again with the same
122     * nal unit. */
123    streampos location = bitstreamFile.tellg();
124    AnnexBStats stats = AnnexBStats();
125    vector<uint8_t> nalUnit;
126    InputNALUnit nalu;
127    byteStreamNALUnit(bytestream, nalUnit, stats);
128
129    // call actual decoding function
130    if (nalUnit.empty())
131    {
132      /* this can happen if the following occur:
133       *  - empty input file
134       *  - two back-to-back start_code_prefixes
135       *  - start_code_prefix immediately followed by EOF
136       */
137      fprintf(stderr, "Warning: Attempt to decode an empty NAL unit\n");
138    }
139    else
140    {
141      read(nalu, nalUnit);
142#if VIDYO_VPS_INTEGRATION
143      viewDepthId = nalu.m_layerId;   // coding order T0D0T1D1T2D2
144#else
145      Int viewId = nalu.m_viewId;
146      Int depth = nalu.m_isDepth ? 1 : 0;
147      viewDepthId = viewId * 2 + depth;   // coding order T0D0T1D1T2D2
148#endif
149     
150      newPicture[viewDepthId] = false;
151      if( viewDepthId >= m_tDecTop.size() )     
152      {
153        increaseNumberOfViews( viewDepthId +1 );
154      }
155      if(m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer)
156      {
157        previousPictureDecoded = false; 
158      }
159      if(m_tDecTop.size() > 1 && (viewDepthId != previousViewDepthId) && previousPictureDecoded )
160      {
161        m_tDecTop[previousViewDepthId]->executeDeblockAndAlf(uiPOC[previousViewDepthId], pcListPic[previousViewDepthId], m_iSkipFrame, m_pocLastDisplay[previousViewDepthId]);
162      } 
163      if( ( viewDepthId == 0 && (viewDepthId != previousViewDepthId) ) || m_tDecTop.size() == 1 )
164      {
165#if HHI_INTER_VIEW_RESIDUAL_PRED
166        for( Int i = 0; i < m_tDecTop.size(); i++ )
167        {
168          m_tDecTop[i]->deleteExtraPicBuffers( uiPOC[i] );
169        }
170#endif
171        for( Int i = 0; i < m_tDecTop.size(); i++ )
172        {
173          m_tDecTop[i]->compressMotion( uiPOC[i] );
174        }
175      }   
176      if( !(m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) )
177      {
178        newPicture[viewDepthId] = m_tDecTop[viewDepthId]->decode(nalu, m_iSkipFrame, m_pocLastDisplay[viewDepthId]);
179        if (newPicture[viewDepthId])
180        {
181          bitstreamFile.clear();
182          /* location points to the current nalunit payload[1] due to the
183           * need for the annexB parser to read three extra bytes.
184           * [1] except for the first NAL unit in the file
185           *     (but bNewPicture doesn't happen then) */
186          bitstreamFile.seekg(location-streamoff(3));
187          bytestream.reset();
188        }
189        if( nalu.isSlice() )
190        {
191          previousPictureDecoded = true;
192        }
193      }
194    }
195    if( ( (newPicture[viewDepthId] || !bitstreamFile) && m_tDecTop.size() == 1) || (!bitstreamFile && previousPictureDecoded == true) ) 
196    {
197      m_tDecTop[viewDepthId]->executeDeblockAndAlf(uiPOC[viewDepthId], pcListPic[viewDepthId], m_iSkipFrame, m_pocLastDisplay[viewDepthId]);
198    }
199    if( pcListPic[viewDepthId] )
200    {
201      if( newPicture[viewDepthId] && (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR || (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDV && m_tDecTop[viewDepthId]->getNalUnitTypeBaseView() == NAL_UNIT_CODED_SLICE_IDR)) )
202      {
203        xFlushOutput( pcListPic[viewDepthId], viewDepthId );
204      }
205      // write reconstruction to file
206      if(newPicture[viewDepthId])
207      {
208#if H0567_DPB_PARAMETERS_PER_TEMPORAL_LAYER
209        xWriteOutput( pcListPic[viewDepthId], viewDepthId, nalu.m_temporalId );
210#else
211        xWriteOutput( pcListPic[viewDepthId], viewDepthId );
212#endif
213      }
214    }
215    previousViewDepthId = viewDepthId;
216  } 
217  if( m_cCamParsCollector.isInitialized() )
218  {
219    m_cCamParsCollector.setSlice( 0 );
220  }
221  // last frame
222  for( Int viewDepthIdx = 0; viewDepthIdx < m_tDecTop.size(); viewDepthIdx++ )
223  {
224    xFlushOutput( pcListPic[viewDepthIdx], viewDepthIdx );
225  } 
226  xDestroyDecLib();
227}
228
229// ====================================================================================================================
230// Protected member functions
231// ====================================================================================================================
232
233Void TAppDecTop::xDestroyDecLib()
234{
235
236  for(Int viewDepthIdx=0; viewDepthIdx<m_tVideoIOYuvReconFile.size() ; viewDepthIdx++)
237  {
238    if( m_tVideoIOYuvReconFile[viewDepthIdx] )
239    {
240      m_tVideoIOYuvReconFile[viewDepthIdx]->close();
241      delete m_tVideoIOYuvReconFile[viewDepthIdx]; 
242      m_tVideoIOYuvReconFile[viewDepthIdx] = NULL ;
243    }
244  }
245
246  for(Int viewDepthIdx=0; viewDepthIdx<m_tDecTop.size() ; viewDepthIdx++)
247  {
248    if( m_tDecTop[viewDepthIdx] )
249    {
250      if( !m_useDepth && (viewDepthIdx % 2 == 1) )
251      {
252      }
253      else
254      {
255        m_tDecTop[viewDepthIdx]->deletePicBuffer();
256        m_tDecTop[viewDepthIdx]->destroy() ;
257      }
258      delete m_tDecTop[viewDepthIdx] ; 
259      m_tDecTop[viewDepthIdx] = NULL ;
260    }
261  }
262
263  m_cCamParsCollector.uninit();
264  if( m_pScaleOffsetFile ) 
265  { 
266    ::fclose( m_pScaleOffsetFile ); 
267  }
268}
269
270#if H0567_DPB_PARAMETERS_PER_TEMPORAL_LAYER
271Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, Int viewDepthId, UInt tId )
272#else
273Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, Int viewDepthId )
274#endif
275{
276  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
277  Int not_displayed = 0;
278
279  while (iterPic != pcListPic->end())
280  {
281    TComPic* pcPic = *(iterPic);
282    if(pcPic->getOutputMark() && pcPic->getPOC() > m_pocLastDisplay[viewDepthId])
283    {
284       not_displayed++;
285    }
286    iterPic++;
287  }
288  iterPic   = pcListPic->begin();
289 
290  while (iterPic != pcListPic->end())
291  {
292    TComPic* pcPic = *(iterPic);
293#if PIC_CROPPING
294    TComSPS *sps = pcPic->getSlice(0)->getSPS();
295#endif
296   
297#if H0567_DPB_PARAMETERS_PER_TEMPORAL_LAYER
298    if ( pcPic->getOutputMark() && (not_displayed >  pcPic->getSlice(0)->getSPS()->getNumReorderPics(tId) && pcPic->getPOC() > m_pocLastDisplay[viewDepthId]))
299#else
300    if ( pcPic->getOutputMark() && (not_displayed >  pcPic->getSlice(0)->getSPS()->getNumReorderFrames() && pcPic->getPOC() > m_pocLastDisplay[viewDepthId]))
301#endif
302    {
303      // write to file
304       not_displayed--;
305      if ( m_pchReconFile )
306      {
307#if PIC_CROPPING
308        m_tVideoIOYuvReconFile[viewDepthId]->write( pcPic->getPicYuvRec(), sps->getPicCropLeftOffset(), sps->getPicCropRightOffset(), sps->getPicCropTopOffset(), sps->getPicCropBottomOffset() );
309#else
310        m_tVideoIOYuvReconFile[viewDepthId]->write( pcPic->getPicYuvRec(), pcPic->getSlice(0)->getSPS()->getPad() );
311#endif
312      }
313     
314      // update POC of display order
315      m_pocLastDisplay[viewDepthId] = pcPic->getPOC();
316     
317      // erase non-referenced picture in the reference picture list after display
318      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
319      {
320#if !DYN_REF_FREE
321        pcPic->setReconMark(false);
322       
323        // mark it should be extended later
324        pcPic->getPicYuvRec()->setBorderExtension( false );
325       
326#else
327        pcPic->destroy();
328        pcListPic->erase( iterPic );
329        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
330        continue;
331#endif
332      }
333      pcPic->setOutputMark(false);
334    }
335   
336    iterPic++;
337  }
338}
339
340/** \param pcListPic list of pictures to be written to file
341    \todo            DYN_REF_FREE should be revised
342 */
343Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic, Int viewDepthId )
344{
345  if(!pcListPic)
346  {
347    return;
348  } 
349  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
350
351  iterPic   = pcListPic->begin();
352 
353  while (iterPic != pcListPic->end())
354  {
355    TComPic* pcPic = *(iterPic);
356#if PIC_CROPPING
357    TComSPS *sps = pcPic->getSlice(0)->getSPS();
358#endif
359
360    if ( pcPic->getOutputMark() )
361    {
362      // write to file
363      if ( m_pchReconFile )
364      {
365#if PIC_CROPPING
366        m_tVideoIOYuvReconFile[viewDepthId]->write( pcPic->getPicYuvRec(), sps->getPicCropLeftOffset(), sps->getPicCropRightOffset(), sps->getPicCropTopOffset(), sps->getPicCropBottomOffset() );
367#else
368        m_tVideoIOYuvReconFile[viewDepthId]->write( pcPic->getPicYuvRec(), pcPic->getSlice(0)->getSPS()->getPad() );
369#endif
370      }
371     
372      // update POC of display order
373      m_pocLastDisplay[viewDepthId] = pcPic->getPOC();
374     
375      // erase non-referenced picture in the reference picture list after display
376      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
377      {
378#if !DYN_REF_FREE
379        pcPic->setReconMark(false);
380       
381        // mark it should be extended later
382        pcPic->getPicYuvRec()->setBorderExtension( false );
383       
384#else
385        pcPic->destroy();
386        pcListPic->erase( iterPic );
387        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
388        continue;
389#endif
390      }
391      pcPic->setOutputMark(false);
392    }
393   
394    iterPic++;
395  }
396  pcListPic->clear();
397  m_pocLastDisplay[viewDepthId] = -MAX_INT;
398}
399
400Void  TAppDecTop::increaseNumberOfViews  ( Int newNumberOfViewDepth )
401{
402  if ( m_outputBitDepth == 0 )
403  {
404    m_outputBitDepth = g_uiBitDepth + g_uiBitIncrement;
405  }
406  Int viewId = (newNumberOfViewDepth-1)>>1;   // coding order T0D0T1D1T2D2
407  Bool isDepth = ((newNumberOfViewDepth % 2) == 0);  // coding order T0D0T1D1T2D2
408  if( isDepth )
409    m_useDepth = true;
410  while( m_tVideoIOYuvReconFile.size() < newNumberOfViewDepth)
411  {
412    m_tVideoIOYuvReconFile.push_back(new TVideoIOYuv);
413    Char buffer[4];
414    sprintf(buffer,"_%i", (Int)(m_tVideoIOYuvReconFile.size()-1) / 2 );
415    Char* nextFilename = NULL;
416    if( (m_tVideoIOYuvReconFile.size() % 2) == 0 )
417    {
418      Char* pchTempFilename = NULL;
419      xAppendToFileNameEnd( m_pchReconFile, "_depth", pchTempFilename);
420      xAppendToFileNameEnd( pchTempFilename, buffer, nextFilename);
421      free ( pchTempFilename );
422    }
423    else
424    {
425      xAppendToFileNameEnd( m_pchReconFile, buffer, nextFilename);
426    }
427    if( isDepth || ( !isDepth && (m_tVideoIOYuvReconFile.size() % 2) == 1 ) )
428    {
429      m_tVideoIOYuvReconFile.back()->open( nextFilename, true, m_outputBitDepth, g_uiBitDepth + g_uiBitIncrement );
430    }
431      free ( nextFilename );
432  }
433  while( m_pocLastDisplay.size() < newNumberOfViewDepth )
434  {
435    m_pocLastDisplay.push_back(-MAX_INT+m_iSkipFrame);
436  }
437  while( m_tDecTop.size() < newNumberOfViewDepth)
438  {
439    m_tDecTop.push_back(new TDecTop);
440    if( isDepth || ( !isDepth && (m_tVideoIOYuvReconFile.size() % 2) == 1 ) )
441    {
442      m_tDecTop.back()->create();
443      m_tDecTop.back()->init( this, newNumberOfViewDepth == 1);
444      m_tDecTop.back()->setViewId( viewId );
445      m_tDecTop.back()->setIsDepth( isDepth );
446      m_tDecTop.back()->setPictureDigestEnabled(m_pictureDigestEnabled);
447      m_tDecTop.back()->setCamParsCollector( &m_cCamParsCollector );
448    }
449  }
450}
451
452TDecTop* TAppDecTop::getTDecTop( Int viewId, Bool isDepth )
453{ 
454  return m_tDecTop[(isDepth ? 1 : 0) + viewId * 2];  // coding order T0D0T1D1T2D2
455} 
456
457std::vector<TComPic*> TAppDecTop::getInterViewRefPics( Int viewId, Int poc, Bool isDepth, TComSPS* sps )
458{
459  std::vector<TComPic*> apcRefPics( sps->getNumberOfUsableInterViewRefs(), (TComPic*)NULL );
460  for( Int k = 0; k < sps->getNumberOfUsableInterViewRefs(); k++ )
461  {
462    TComPic* pcRefPic = xGetPicFromView( sps->getUsableInterViewRef( k ) + viewId, poc, isDepth );
463    assert( pcRefPic != NULL );
464    apcRefPics[k] = pcRefPic;
465  }
466  return apcRefPics;
467}
468
469TComPic* TAppDecTop::xGetPicFromView( Int viewId, Int poc, Bool isDepth )
470{
471  assert( ( viewId >= 0 ) );
472
473  TComList<TComPic*>* apcListPic = getTDecTop( viewId, isDepth )->getListPic();
474  TComPic* pcPic = NULL;
475  for( TComList<TComPic*>::iterator it=apcListPic->begin(); it!=apcListPic->end(); it++ )
476  {
477    if( (*it)->getPOC() == poc )
478    {
479      pcPic = *it;
480      break;
481    }
482  }
483  return pcPic;
484}
485//! \}
Note: See TracBrowser for help on using the repository browser.