source: SHVCSoftware/branches/0.1.1-bugfix/source/Lib/TLibDecoder/TDecGop.cpp @ 582

Last change on this file since 582 was 2, checked in by seregin, 12 years ago

Initial import by Vadim Seregin <vseregin@…>

File size: 14.3 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     TDecGop.cpp
35    \brief    GOP decoder class
36*/
37
38extern bool g_md5_mismatch; ///< top level flag to signal when there is a decode problem
39
40#include "TDecGop.h"
41#include "TDecCAVLC.h"
42#include "TDecSbac.h"
43#include "TDecBinCoder.h"
44#include "TDecBinCoderCABAC.h"
45#include "libmd5/MD5.h"
46#include "TLibCommon/SEI.h"
47#if SVC_EXTENSION
48#include "TDecTop.h"
49#endif
50
51#include <time.h>
52
53//! \ingroup TLibDecoder
54//! \{
55static void calcAndPrintHashStatus(TComPicYuv& pic, const SEImessages* seis);
56// ====================================================================================================================
57// Constructor / destructor / initialization / destroy
58// ====================================================================================================================
59
60TDecGop::TDecGop()
61{
62  m_iGopSize = 0;
63  m_dDecTime = 0;
64  m_pcSbacDecoders = NULL;
65  m_pcBinCABACs = NULL;
66}
67
68TDecGop::~TDecGop()
69{
70 
71}
72
73#if SVC_EXTENSION
74Void TDecGop::create(UInt layerId)
75{
76  m_layerId = layerId;
77}
78#else
79Void TDecGop::create()
80{
81 
82}
83#endif
84
85Void TDecGop::destroy()
86{
87}
88#if SVC_EXTENSION
89Void TDecGop::init(TDecTop**               ppcDecTop,
90                   TDecEntropy*            pcEntropyDecoder,
91#else
92Void TDecGop::init( TDecEntropy*            pcEntropyDecoder, 
93#endif
94                   TDecSbac*               pcSbacDecoder, 
95                   TDecBinCABAC*           pcBinCABAC,
96                   TDecCavlc*              pcCavlcDecoder, 
97                   TDecSlice*              pcSliceDecoder, 
98                   TComLoopFilter*         pcLoopFilter,
99#if !REMOVE_ALF
100                   TComAdaptiveLoopFilter* pcAdaptiveLoopFilter,
101#endif
102                   TComSampleAdaptiveOffset* pcSAO
103                   )
104{
105  m_pcEntropyDecoder      = pcEntropyDecoder;
106  m_pcSbacDecoder         = pcSbacDecoder;
107  m_pcBinCABAC            = pcBinCABAC;
108  m_pcCavlcDecoder        = pcCavlcDecoder;
109  m_pcSliceDecoder        = pcSliceDecoder;
110  m_pcLoopFilter          = pcLoopFilter;
111#if !REMOVE_ALF
112  m_pcAdaptiveLoopFilter  = pcAdaptiveLoopFilter;
113#endif
114  m_pcSAO  = pcSAO;
115#if SVC_EXTENSION   
116  m_ppcTDecTop            = ppcDecTop;
117#endif
118}
119
120
121// ====================================================================================================================
122// Private member functions
123// ====================================================================================================================
124// ====================================================================================================================
125// Public member functions
126// ====================================================================================================================
127
128Void TDecGop::decompressSlice(TComInputBitstream* pcBitstream, TComPic*& rpcPic)
129{
130  TComSlice*  pcSlice = rpcPic->getSlice(rpcPic->getCurrSliceIdx());
131  // Table of extracted substreams.
132  // These must be deallocated AND their internal fifos, too.
133  TComInputBitstream **ppcSubstreams = NULL;
134
135  //-- For time output for each slice
136  long iBeforeTime = clock();
137 
138  UInt uiStartCUAddr   = pcSlice->getDependentSliceCurStartCUAddr();
139
140  UInt uiSliceStartCuAddr = pcSlice->getSliceCurStartCUAddr();
141  if(uiSliceStartCuAddr == uiStartCUAddr)
142  {
143    m_sliceStartCUAddress.push_back(uiSliceStartCuAddr);
144  }
145
146  m_pcSbacDecoder->init( (TDecBinIf*)m_pcBinCABAC );
147  m_pcEntropyDecoder->setEntropyDecoder (m_pcSbacDecoder);
148
149#if TILES_WPP_ENTROPYSLICES_FLAGS
150  UInt uiNumSubstreams = pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag() ? pcSlice->getNumEntryPointOffsets()+1 : pcSlice->getPPS()->getNumSubstreams();
151#else
152  UInt uiNumSubstreams = pcSlice->getPPS()->getTilesOrEntropyCodingSyncIdc() == 2 ? pcSlice->getNumEntryPointOffsets()+1 : pcSlice->getPPS()->getNumSubstreams();
153#endif
154
155  // init each couple {EntropyDecoder, Substream}
156  UInt *puiSubstreamSizes = pcSlice->getSubstreamSizes();
157  ppcSubstreams    = new TComInputBitstream*[uiNumSubstreams];
158  m_pcSbacDecoders = new TDecSbac[uiNumSubstreams];
159  m_pcBinCABACs    = new TDecBinCABAC[uiNumSubstreams];
160  for ( UInt ui = 0 ; ui < uiNumSubstreams ; ui++ )
161  {
162    m_pcSbacDecoders[ui].init(&m_pcBinCABACs[ui]);
163    ppcSubstreams[ui] = pcBitstream->extractSubstream(ui+1 < uiNumSubstreams ? puiSubstreamSizes[ui] : pcBitstream->getNumBitsLeft());
164  }
165
166  for ( UInt ui = 0 ; ui+1 < uiNumSubstreams; ui++ )
167  {
168    m_pcEntropyDecoder->setEntropyDecoder ( &m_pcSbacDecoders[uiNumSubstreams - 1 - ui] );
169    m_pcEntropyDecoder->setBitstream      (  ppcSubstreams   [uiNumSubstreams - 1 - ui] );
170    m_pcEntropyDecoder->resetEntropy      (pcSlice);
171  }
172
173  m_pcEntropyDecoder->setEntropyDecoder ( m_pcSbacDecoder  );
174  m_pcEntropyDecoder->setBitstream      ( ppcSubstreams[0] );
175  m_pcEntropyDecoder->resetEntropy      (pcSlice);
176
177  if(uiSliceStartCuAddr == uiStartCUAddr)
178  {
179    m_LFCrossSliceBoundaryFlag.push_back( pcSlice->getLFCrossSliceBoundaryFlag());
180#if !REMOVE_ALF
181    if(pcSlice->getSPS()->getUseALF())
182    {
183      for(Int compIdx=0; compIdx < 3; compIdx++)
184      {
185        m_sliceAlfEnabled[compIdx].push_back(  pcSlice->getAlfEnabledFlag(compIdx) );
186      }
187    }
188#endif
189  }
190  m_pcSbacDecoders[0].load(m_pcSbacDecoder);
191  m_pcSliceDecoder->decompressSlice( pcBitstream, ppcSubstreams, rpcPic, m_pcSbacDecoder, m_pcSbacDecoders);
192  m_pcEntropyDecoder->setBitstream(  ppcSubstreams[uiNumSubstreams-1] );
193  // deallocate all created substreams, including internal buffers.
194  for (UInt ui = 0; ui < uiNumSubstreams; ui++)
195  {
196    ppcSubstreams[ui]->deleteFifo();
197    delete ppcSubstreams[ui];
198  }
199  delete[] ppcSubstreams;
200  delete[] m_pcSbacDecoders; m_pcSbacDecoders = NULL;
201  delete[] m_pcBinCABACs; m_pcBinCABACs = NULL;
202
203  m_dDecTime += (double)(clock()-iBeforeTime) / CLOCKS_PER_SEC;
204}
205
206Void TDecGop::filterPicture(TComPic*& rpcPic)
207{
208  TComSlice*  pcSlice = rpcPic->getSlice(rpcPic->getCurrSliceIdx());
209
210  //-- For time output for each slice
211  long iBeforeTime = clock();
212
213  // deblocking filter
214  Bool bLFCrossTileBoundary = pcSlice->getPPS()->getLoopFilterAcrossTilesEnabledFlag();
215  m_pcLoopFilter->setCfg(pcSlice->getPPS()->getDeblockingFilterControlPresentFlag(), pcSlice->getDeblockingFilterDisable(), pcSlice->getDeblockingFilterBetaOffsetDiv2(), pcSlice->getDeblockingFilterTcOffsetDiv2(), bLFCrossTileBoundary);
216  m_pcLoopFilter->loopFilterPic( rpcPic );
217
218  pcSlice = rpcPic->getSlice(0);
219#if REMOVE_ALF
220  if(pcSlice->getSPS()->getUseSAO())
221#else
222  if(pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
223#endif
224  {
225#if !REMOVE_FGS
226    Int sliceGranularity = pcSlice->getPPS()->getSliceGranularity();
227#endif
228    m_sliceStartCUAddress.push_back(rpcPic->getNumCUsInFrame()* rpcPic->getNumPartInCU());
229#if REMOVE_FGS
230    rpcPic->createNonDBFilterInfo(m_sliceStartCUAddress, 0, &m_LFCrossSliceBoundaryFlag, rpcPic->getPicSym()->getNumTiles(), bLFCrossTileBoundary);
231#else
232    rpcPic->createNonDBFilterInfo(m_sliceStartCUAddress, sliceGranularity, &m_LFCrossSliceBoundaryFlag, rpcPic->getPicSym()->getNumTiles(), bLFCrossTileBoundary);
233#endif
234  }
235
236  if( pcSlice->getSPS()->getUseSAO() )
237  {
238#if !SAO_LUM_CHROMA_ONOFF_FLAGS
239    if(pcSlice->getSaoEnabledFlag())
240#else
241    if(pcSlice->getSaoEnabledFlag()||pcSlice->getSaoEnabledFlagChroma())
242#endif
243    {
244#if REMOVE_APS
245      SAOParam *saoParam = rpcPic->getPicSym()->getSaoParam();
246#else
247      SAOParam *saoParam = pcSlice->getAPS()->getSaoParam();
248#endif
249      saoParam->bSaoFlag[0] = pcSlice->getSaoEnabledFlag();
250#if SAO_TYPE_SHARING
251      saoParam->bSaoFlag[1] = pcSlice->getSaoEnabledFlagChroma();
252#else
253      saoParam->bSaoFlag[1] = pcSlice->getSaoEnabledFlagCb();
254      saoParam->bSaoFlag[2] = pcSlice->getSaoEnabledFlagCr();
255#endif
256      m_pcSAO->setSaoLcuBasedOptimization(1);
257      m_pcSAO->createPicSaoInfo(rpcPic, (Int) m_sliceStartCUAddress.size() - 1);
258      m_pcSAO->SAOProcess(rpcPic, saoParam);
259#if !REMOVE_ALF
260      m_pcAdaptiveLoopFilter->PCMLFDisableProcess(rpcPic);
261#else
262      m_pcSAO->PCMLFDisableProcess(rpcPic);
263#endif
264      m_pcSAO->destroyPicSaoInfo();
265    }
266  }
267
268#if !REMOVE_ALF
269  // adaptive loop filter
270  if( pcSlice->getSPS()->getUseALF() )
271  {
272    m_pcAdaptiveLoopFilter->createPicAlfInfo(rpcPic, (Int) m_sliceStartCUAddress.size()-1);
273    m_pcAdaptiveLoopFilter->ALFProcess(rpcPic, pcSlice->getAPS()->getAlfParam(), m_sliceAlfEnabled);
274    m_pcAdaptiveLoopFilter->PCMLFDisableProcess(rpcPic);
275    m_pcAdaptiveLoopFilter->destroyPicAlfInfo();
276  }
277#endif
278
279#if REMOVE_ALF
280  if(pcSlice->getSPS()->getUseSAO())
281#else
282  if(pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
283#endif
284  {
285    rpcPic->destroyNonDBFilterInfo();
286  }
287
288  rpcPic->compressMotion(); 
289  Char c = (pcSlice->isIntra() ? 'I' : pcSlice->isInterP() ? 'P' : 'B');
290  if (!pcSlice->isReferenced()) c += 32;
291
292  //-- For time output for each slice
293#if SVC_EXTENSION
294  printf("\nPOC %4d LId: %1d TId: %1d ( %c-SLICE, QP%3d ) ", pcSlice->getPOC(),
295                                                    rpcPic->getLayerId(),
296                                                    pcSlice->getTLayer(),
297                                                    c,
298                                                    pcSlice->getSliceQp() );
299#else
300  printf("\nPOC %4d TId: %1d ( %c-SLICE, QP%3d ) ", pcSlice->getPOC(),
301                                                    pcSlice->getTLayer(),
302                                                    c,
303                                                    pcSlice->getSliceQp() );
304#endif
305  m_dDecTime += (double)(clock()-iBeforeTime) / CLOCKS_PER_SEC;
306  printf ("[DT %6.3f] ", m_dDecTime );
307  m_dDecTime  = 0;
308
309  for (Int iRefList = 0; iRefList < 2; iRefList++)
310  {
311    printf ("[L%d ", iRefList);
312    for (Int iRefIndex = 0; iRefIndex < pcSlice->getNumRefIdx(RefPicList(iRefList)); iRefIndex++)
313    {
314      printf ("%d ", pcSlice->getRefPOC(RefPicList(iRefList), iRefIndex));
315    }
316    printf ("] ");
317  }
318  if (m_decodedPictureHashSEIEnabled)
319  {
320    calcAndPrintHashStatus(*rpcPic->getPicYuvRec(), rpcPic->getSEIs());
321  }
322
323#if FIXED_ROUNDING_FRAME_MEMORY
324  rpcPic->getPicYuvRec()->xFixedRoundingPic();
325#endif
326
327  rpcPic->setOutputMark(true);
328  rpcPic->setReconMark(true);
329  m_sliceStartCUAddress.clear();
330#if !REMOVE_ALF
331  for(Int compIdx=0; compIdx < 3; compIdx++)
332  {
333    m_sliceAlfEnabled[compIdx].clear();
334  }
335#endif
336  m_LFCrossSliceBoundaryFlag.clear();
337}
338
339/**
340 * Calculate and print hash for pic, compare to picture_digest SEI if
341 * present in seis.  seis may be NULL.  Hash is printed to stdout, in
342 * a manner suitable for the status line. Theformat is:
343 *  [Hash_type:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,(yyy)]
344 * Where, x..x is the hash
345 *        yyy has the following meanings:
346 *            OK          - calculated hash matches the SEI message
347 *            ***ERROR*** - calculated hash does not match the SEI message
348 *            unk         - no SEI message was available for comparison
349 */
350static void calcAndPrintHashStatus(TComPicYuv& pic, const SEImessages* seis)
351{
352  /* calculate MD5sum for entire reconstructed picture */
353  unsigned char recon_digest[3][16];
354  int numChar=0;
355  const char* hashType = "\0";
356
357  if (seis && seis->picture_digest)
358  {
359    switch (seis->picture_digest->method)
360    {
361    case SEIDecodedPictureHash::MD5:
362      {
363        hashType = "MD5";
364        calcMD5(pic, recon_digest);
365        numChar = 16;
366        break;
367      }
368    case SEIDecodedPictureHash::CRC:
369      {
370        hashType = "CRC";
371        calcCRC(pic, recon_digest);
372        numChar = 2;
373        break;
374      }
375    case SEIDecodedPictureHash::CHECKSUM:
376      {
377        hashType = "Checksum";
378        calcChecksum(pic, recon_digest);
379        numChar = 4;
380        break;
381      }
382    default:
383      {
384        assert (!"unknown hash type");
385      }
386    }
387  }
388
389  /* compare digest against received version */
390  const char* ok = "(unk)";
391  bool mismatch = false;
392
393  if (seis && seis->picture_digest)
394  {
395    ok = "(OK)";
396    for(int yuvIdx = 0; yuvIdx < 3; yuvIdx++)
397    {
398      for (unsigned i = 0; i < numChar; i++)
399      {
400        if (recon_digest[yuvIdx][i] != seis->picture_digest->digest[yuvIdx][i])
401        {
402          ok = "(***ERROR***)";
403          mismatch = true;
404        }
405      }
406    }
407  }
408
409  printf("[%s:%s,%s] ", hashType, digestToString(recon_digest, numChar), ok);
410
411  if (mismatch)
412  {
413    g_md5_mismatch = true;
414    printf("[rx%s:%s] ", hashType, digestToString(seis->picture_digest->digest, numChar));
415  }
416}
417//! \}
Note: See TracBrowser for help on using the repository browser.