source: 3DVCSoftware/branches/HTM-3.0-Qualcomm/source/App/TAppDecoder/TAppDecTop.cpp @ 1417

Last change on this file since 1417 was 57, checked in by hschwarz, 13 years ago

corrected output of decoded camera parameter files

  • Property svn:eol-style set to native
File size: 15.7 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      Int viewId = nalu.m_viewId;
143      Int depth = nalu.m_isDepth ? 1 : 0;
144      viewDepthId = viewId * 2 + depth;   // coding order T0D0T1D1T2D2
145      newPicture[viewDepthId] = false;
146      if( viewDepthId >= m_tDecTop.size() )     
147      {
148        increaseNumberOfViews( viewDepthId +1 );
149      }
150      if(m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer)
151      {
152        previousPictureDecoded = false; 
153      }
154      if(m_tDecTop.size() > 1 && (viewDepthId != previousViewDepthId) && previousPictureDecoded )
155      {
156        m_tDecTop[previousViewDepthId]->executeDeblockAndAlf(uiPOC[previousViewDepthId], pcListPic[previousViewDepthId], m_iSkipFrame, m_pocLastDisplay[previousViewDepthId]);
157      } 
158      if( ( viewDepthId == 0 && (viewDepthId != previousViewDepthId) ) || m_tDecTop.size() == 1 )
159      {
160#if HHI_INTER_VIEW_RESIDUAL_PRED
161        for( Int i = 0; i < m_tDecTop.size(); i++ )
162        {
163          m_tDecTop[i]->deleteExtraPicBuffers( uiPOC[i] );
164        }
165#endif
166        for( Int i = 0; i < m_tDecTop.size(); i++ )
167        {
168          m_tDecTop[i]->compressMotion( uiPOC[i] );
169        }
170      }   
171      if( !(m_iMaxTemporalLayer >= 0 && nalu.m_temporalId > m_iMaxTemporalLayer) )
172      {
173        newPicture[viewDepthId] = m_tDecTop[viewDepthId]->decode(nalu, m_iSkipFrame, m_pocLastDisplay[viewDepthId]);
174        if (newPicture[viewDepthId])
175        {
176          bitstreamFile.clear();
177          /* location points to the current nalunit payload[1] due to the
178           * need for the annexB parser to read three extra bytes.
179           * [1] except for the first NAL unit in the file
180           *     (but bNewPicture doesn't happen then) */
181          bitstreamFile.seekg(location-streamoff(3));
182          bytestream.reset();
183        }
184        if( nalu.isSlice() )
185        {
186          previousPictureDecoded = true;
187        }
188      }
189    }
190    if( ( (newPicture[viewDepthId] || !bitstreamFile) && m_tDecTop.size() == 1) || (!bitstreamFile && previousPictureDecoded == true) ) 
191    {
192      m_tDecTop[viewDepthId]->executeDeblockAndAlf(uiPOC[viewDepthId], pcListPic[viewDepthId], m_iSkipFrame, m_pocLastDisplay[viewDepthId]);
193    }
194    if( pcListPic[viewDepthId] )
195    {
196      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)) )
197      {
198        xFlushOutput( pcListPic[viewDepthId], viewDepthId );
199      }
200      // write reconstruction to file
201      if(newPicture[viewDepthId])
202      {
203#if H0567_DPB_PARAMETERS_PER_TEMPORAL_LAYER
204        xWriteOutput( pcListPic[viewDepthId], viewDepthId, nalu.m_temporalId );
205#else
206        xWriteOutput( pcListPic[viewDepthId], viewDepthId );
207#endif
208      }
209    }
210    previousViewDepthId = viewDepthId;
211  } 
212  if( m_cCamParsCollector.isInitialized() )
213  {
214    m_cCamParsCollector.setSlice( 0 );
215  }
216  // last frame
217  for( Int viewDepthIdx = 0; viewDepthIdx < m_tDecTop.size(); viewDepthIdx++ )
218  {
219    xFlushOutput( pcListPic[viewDepthIdx], viewDepthIdx );
220  } 
221  xDestroyDecLib();
222}
223
224// ====================================================================================================================
225// Protected member functions
226// ====================================================================================================================
227
228Void TAppDecTop::xDestroyDecLib()
229{
230
231  for(Int viewDepthIdx=0; viewDepthIdx<m_tVideoIOYuvReconFile.size() ; viewDepthIdx++)
232  {
233    if( m_tVideoIOYuvReconFile[viewDepthIdx] )
234    {
235      m_tVideoIOYuvReconFile[viewDepthIdx]->close();
236      delete m_tVideoIOYuvReconFile[viewDepthIdx]; 
237      m_tVideoIOYuvReconFile[viewDepthIdx] = NULL ;
238    }
239  }
240
241  for(Int viewDepthIdx=0; viewDepthIdx<m_tDecTop.size() ; viewDepthIdx++)
242  {
243    if( m_tDecTop[viewDepthIdx] )
244    {
245      if( !m_useDepth && (viewDepthIdx % 2 == 1) )
246      {
247      }
248      else
249      {
250        m_tDecTop[viewDepthIdx]->deletePicBuffer();
251        m_tDecTop[viewDepthIdx]->destroy() ;
252      }
253      delete m_tDecTop[viewDepthIdx] ; 
254      m_tDecTop[viewDepthIdx] = NULL ;
255    }
256  }
257
258  m_cCamParsCollector.uninit();
259  if( m_pScaleOffsetFile ) 
260  { 
261    ::fclose( m_pScaleOffsetFile ); 
262  }
263}
264
265#if H0567_DPB_PARAMETERS_PER_TEMPORAL_LAYER
266Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, Int viewDepthId, UInt tId )
267#else
268Void TAppDecTop::xWriteOutput( TComList<TComPic*>* pcListPic, Int viewDepthId )
269#endif
270{
271  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
272  Int not_displayed = 0;
273
274  while (iterPic != pcListPic->end())
275  {
276    TComPic* pcPic = *(iterPic);
277    if(pcPic->getOutputMark() && pcPic->getPOC() > m_pocLastDisplay[viewDepthId])
278    {
279       not_displayed++;
280    }
281    iterPic++;
282  }
283  iterPic   = pcListPic->begin();
284 
285  while (iterPic != pcListPic->end())
286  {
287    TComPic* pcPic = *(iterPic);
288#if PIC_CROPPING
289    TComSPS *sps = pcPic->getSlice(0)->getSPS();
290#endif
291   
292#if H0567_DPB_PARAMETERS_PER_TEMPORAL_LAYER
293    if ( pcPic->getOutputMark() && (not_displayed >  pcPic->getSlice(0)->getSPS()->getNumReorderPics(tId) && pcPic->getPOC() > m_pocLastDisplay[viewDepthId]))
294#else
295    if ( pcPic->getOutputMark() && (not_displayed >  pcPic->getSlice(0)->getSPS()->getNumReorderFrames() && pcPic->getPOC() > m_pocLastDisplay[viewDepthId]))
296#endif
297    {
298      // write to file
299       not_displayed--;
300      if ( m_pchReconFile )
301      {
302#if PIC_CROPPING
303        m_tVideoIOYuvReconFile[viewDepthId]->write( pcPic->getPicYuvRec(), sps->getPicCropLeftOffset(), sps->getPicCropRightOffset(), sps->getPicCropTopOffset(), sps->getPicCropBottomOffset() );
304#else
305        m_tVideoIOYuvReconFile[viewDepthId]->write( pcPic->getPicYuvRec(), pcPic->getSlice(0)->getSPS()->getPad() );
306#endif
307      }
308     
309      // update POC of display order
310      m_pocLastDisplay[viewDepthId] = pcPic->getPOC();
311     
312      // erase non-referenced picture in the reference picture list after display
313      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
314      {
315#if !DYN_REF_FREE
316        pcPic->setReconMark(false);
317       
318        // mark it should be extended later
319        pcPic->getPicYuvRec()->setBorderExtension( false );
320       
321#else
322        pcPic->destroy();
323        pcListPic->erase( iterPic );
324        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
325        continue;
326#endif
327      }
328      pcPic->setOutputMark(false);
329    }
330   
331    iterPic++;
332  }
333}
334
335/** \param pcListPic list of pictures to be written to file
336    \todo            DYN_REF_FREE should be revised
337 */
338Void TAppDecTop::xFlushOutput( TComList<TComPic*>* pcListPic, Int viewDepthId )
339{
340  if(!pcListPic)
341  {
342    return;
343  } 
344  TComList<TComPic*>::iterator iterPic   = pcListPic->begin();
345
346  iterPic   = pcListPic->begin();
347 
348  while (iterPic != pcListPic->end())
349  {
350    TComPic* pcPic = *(iterPic);
351#if PIC_CROPPING
352    TComSPS *sps = pcPic->getSlice(0)->getSPS();
353#endif
354
355    if ( pcPic->getOutputMark() )
356    {
357      // write to file
358      if ( m_pchReconFile )
359      {
360#if PIC_CROPPING
361        m_tVideoIOYuvReconFile[viewDepthId]->write( pcPic->getPicYuvRec(), sps->getPicCropLeftOffset(), sps->getPicCropRightOffset(), sps->getPicCropTopOffset(), sps->getPicCropBottomOffset() );
362#else
363        m_tVideoIOYuvReconFile[viewDepthId]->write( pcPic->getPicYuvRec(), pcPic->getSlice(0)->getSPS()->getPad() );
364#endif
365      }
366     
367      // update POC of display order
368      m_pocLastDisplay[viewDepthId] = pcPic->getPOC();
369     
370      // erase non-referenced picture in the reference picture list after display
371      if ( !pcPic->getSlice(0)->isReferenced() && pcPic->getReconMark() == true )
372      {
373#if !DYN_REF_FREE
374        pcPic->setReconMark(false);
375       
376        // mark it should be extended later
377        pcPic->getPicYuvRec()->setBorderExtension( false );
378       
379#else
380        pcPic->destroy();
381        pcListPic->erase( iterPic );
382        iterPic = pcListPic->begin(); // to the beginning, non-efficient way, have to be revised!
383        continue;
384#endif
385      }
386      pcPic->setOutputMark(false);
387    }
388   
389    iterPic++;
390  }
391  pcListPic->clear();
392  m_pocLastDisplay[viewDepthId] = -MAX_INT;
393}
394
395Void  TAppDecTop::increaseNumberOfViews  ( Int newNumberOfViewDepth )
396{
397  if ( m_outputBitDepth == 0 )
398  {
399    m_outputBitDepth = g_uiBitDepth + g_uiBitIncrement;
400  }
401  Int viewId = (newNumberOfViewDepth-1)>>1;   // coding order T0D0T1D1T2D2
402  Bool isDepth = ((newNumberOfViewDepth % 2) == 0);  // coding order T0D0T1D1T2D2
403  if( isDepth )
404    m_useDepth = true;
405  while( m_tVideoIOYuvReconFile.size() < newNumberOfViewDepth)
406  {
407    m_tVideoIOYuvReconFile.push_back(new TVideoIOYuv);
408    Char buffer[4];
409    sprintf(buffer,"_%i", (Int)(m_tVideoIOYuvReconFile.size()-1) / 2 );
410    Char* nextFilename = NULL;
411    if( (m_tVideoIOYuvReconFile.size() % 2) == 0 )
412    {
413      Char* pchTempFilename = NULL;
414      xAppendToFileNameEnd( m_pchReconFile, "_depth", pchTempFilename);
415      xAppendToFileNameEnd( pchTempFilename, buffer, nextFilename);
416      free ( pchTempFilename );
417    }
418    else
419    {
420      xAppendToFileNameEnd( m_pchReconFile, buffer, nextFilename);
421    }
422    if( isDepth || ( !isDepth && (m_tVideoIOYuvReconFile.size() % 2) == 1 ) )
423    {
424      m_tVideoIOYuvReconFile.back()->open( nextFilename, true, m_outputBitDepth, g_uiBitDepth + g_uiBitIncrement );
425    }
426      free ( nextFilename );
427  }
428  while( m_pocLastDisplay.size() < newNumberOfViewDepth )
429  {
430    m_pocLastDisplay.push_back(-MAX_INT+m_iSkipFrame);
431  }
432  while( m_tDecTop.size() < newNumberOfViewDepth)
433  {
434    m_tDecTop.push_back(new TDecTop);
435    if( isDepth || ( !isDepth && (m_tVideoIOYuvReconFile.size() % 2) == 1 ) )
436    {
437      m_tDecTop.back()->create();
438      m_tDecTop.back()->init( this, newNumberOfViewDepth == 1);
439      m_tDecTop.back()->setViewId( viewId );
440      m_tDecTop.back()->setIsDepth( isDepth );
441      m_tDecTop.back()->setPictureDigestEnabled(m_pictureDigestEnabled);
442      m_tDecTop.back()->setCamParsCollector( &m_cCamParsCollector );
443    }
444  }
445}
446
447TDecTop* TAppDecTop::getTDecTop( Int viewId, Bool isDepth )
448{ 
449  return m_tDecTop[(isDepth ? 1 : 0) + viewId * 2];  // coding order T0D0T1D1T2D2
450} 
451
452std::vector<TComPic*> TAppDecTop::getInterViewRefPics( Int viewId, Int poc, Bool isDepth, TComSPS* sps )
453{
454  std::vector<TComPic*> apcRefPics( sps->getNumberOfUsableInterViewRefs(), (TComPic*)NULL );
455  for( Int k = 0; k < sps->getNumberOfUsableInterViewRefs(); k++ )
456  {
457    TComPic* pcRefPic = xGetPicFromView( sps->getUsableInterViewRef( k ) + viewId, poc, isDepth );
458    assert( pcRefPic != NULL );
459    apcRefPics[k] = pcRefPic;
460  }
461  return apcRefPics;
462}
463
464TComPic* TAppDecTop::xGetPicFromView( Int viewId, Int poc, Bool isDepth )
465{
466  assert( ( viewId >= 0 ) );
467
468  TComList<TComPic*>* apcListPic = getTDecTop( viewId, isDepth )->getListPic();
469  TComPic* pcPic = NULL;
470  for( TComList<TComPic*>::iterator it=apcListPic->begin(); it!=apcListPic->end(); it++ )
471  {
472    if( (*it)->getPOC() == poc )
473    {
474      pcPic = *it;
475      break;
476    }
477  }
478  return pcPic;
479}
480//! \}
Note: See TracBrowser for help on using the repository browser.