source: SHVCSoftware/branches/SHM-4.0-dev/source/Lib/TLibDecoder/TDecSlice.cpp @ 582

Last change on this file since 582 was 345, checked in by seregin, 11 years ago

merge SHM-3.0-dev branch

  • Property svn:eol-style set to native
File size: 16.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-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     TDecSlice.cpp
35    \brief    slice decoder class
36*/
37
38#include "TDecSlice.h"
39
40//! \ingroup TLibDecoder
41//! \{
42
43#if SVC_EXTENSION
44  ParameterSetMap<TComVPS> ParameterSetManagerDecoder::m_vpsBuffer(MAX_NUM_VPS);
45#endif
46
47//////////////////////////////////////////////////////////////////////
48// Construction/Destruction
49//////////////////////////////////////////////////////////////////////
50
51TDecSlice::TDecSlice()
52{
53  m_pcBufferSbacDecoders = NULL;
54  m_pcBufferBinCABACs    = NULL;
55  m_pcBufferLowLatSbacDecoders = NULL;
56  m_pcBufferLowLatBinCABACs    = NULL;
57}
58
59TDecSlice::~TDecSlice()
60{
61  for (std::vector<TDecSbac*>::iterator i = CTXMem.begin(); i != CTXMem.end(); i++)
62  {
63    delete (*i);
64  }
65  CTXMem.clear();
66}
67
68Void TDecSlice::initCtxMem(  UInt i )               
69{   
70  for (std::vector<TDecSbac*>::iterator j = CTXMem.begin(); j != CTXMem.end(); j++)
71  {
72    delete (*j);
73  }
74  CTXMem.clear(); 
75  CTXMem.resize(i); 
76}
77
78Void TDecSlice::create()
79{
80}
81
82Void TDecSlice::destroy()
83{
84  if ( m_pcBufferSbacDecoders )
85  {
86    delete[] m_pcBufferSbacDecoders;
87    m_pcBufferSbacDecoders = NULL;
88  }
89  if ( m_pcBufferBinCABACs )
90  {
91    delete[] m_pcBufferBinCABACs;
92    m_pcBufferBinCABACs = NULL;
93  }
94  if ( m_pcBufferLowLatSbacDecoders )
95  {
96    delete[] m_pcBufferLowLatSbacDecoders;
97    m_pcBufferLowLatSbacDecoders = NULL;
98  }
99  if ( m_pcBufferLowLatBinCABACs )
100  {
101    delete[] m_pcBufferLowLatBinCABACs;
102    m_pcBufferLowLatBinCABACs = NULL;
103  }
104}
105
106#if SVC_EXTENSION
107Void TDecSlice::init(TDecTop** ppcDecTop,TDecEntropy* pcEntropyDecoder, TDecCu* pcCuDecoder)
108{
109  m_pcEntropyDecoder  = pcEntropyDecoder;
110  m_pcCuDecoder       = pcCuDecoder;
111  m_ppcTDecTop        = ppcDecTop;
112}
113#else
114Void TDecSlice::init(TDecEntropy* pcEntropyDecoder, TDecCu* pcCuDecoder)
115{
116  m_pcEntropyDecoder  = pcEntropyDecoder;
117  m_pcCuDecoder       = pcCuDecoder;
118}
119#endif
120
121Void TDecSlice::decompressSlice(TComInputBitstream** ppcSubstreams, TComPic*& rpcPic, TDecSbac* pcSbacDecoder, TDecSbac* pcSbacDecoders)
122{
123  TComDataCU* pcCU;
124  UInt        uiIsLast = 0;
125  Int   iStartCUEncOrder = max(rpcPic->getSlice(rpcPic->getCurrSliceIdx())->getSliceCurStartCUAddr()/rpcPic->getNumPartInCU(), rpcPic->getSlice(rpcPic->getCurrSliceIdx())->getSliceSegmentCurStartCUAddr()/rpcPic->getNumPartInCU());
126  Int   iStartCUAddr = rpcPic->getPicSym()->getCUOrderMap(iStartCUEncOrder);
127
128  // decoder don't need prediction & residual frame buffer
129  rpcPic->setPicYuvPred( 0 );
130  rpcPic->setPicYuvResi( 0 );
131 
132#if ENC_DEC_TRACE
133  g_bJustDoIt = g_bEncDecTraceEnable;
134#endif
135  DTRACE_CABAC_VL( g_nSymbolCounter++ );
136  DTRACE_CABAC_T( "\tPOC: " );
137  DTRACE_CABAC_V( rpcPic->getPOC() );
138  DTRACE_CABAC_T( "\n" );
139
140#if ENC_DEC_TRACE
141  g_bJustDoIt = g_bEncDecTraceDisable;
142#endif
143
144  UInt uiTilesAcross   = rpcPic->getPicSym()->getNumColumnsMinus1()+1;
145  TComSlice*  pcSlice = rpcPic->getSlice(rpcPic->getCurrSliceIdx());
146  Int  iNumSubstreams = pcSlice->getPPS()->getNumSubstreams();
147
148  // delete decoders if already allocated in previous slice
149  if (m_pcBufferSbacDecoders)
150  {
151    delete [] m_pcBufferSbacDecoders;
152  }
153  if (m_pcBufferBinCABACs) 
154  {
155    delete [] m_pcBufferBinCABACs;
156  }
157  // allocate new decoders based on tile numbaer
158  m_pcBufferSbacDecoders = new TDecSbac    [uiTilesAcross]; 
159  m_pcBufferBinCABACs    = new TDecBinCABAC[uiTilesAcross];
160  for (UInt ui = 0; ui < uiTilesAcross; ui++)
161  {
162    m_pcBufferSbacDecoders[ui].init(&m_pcBufferBinCABACs[ui]);
163  }
164  //save init. state
165  for (UInt ui = 0; ui < uiTilesAcross; ui++)
166  {
167    m_pcBufferSbacDecoders[ui].load(pcSbacDecoder);
168  }
169
170  // free memory if already allocated in previous call
171  if (m_pcBufferLowLatSbacDecoders)
172  {
173    delete [] m_pcBufferLowLatSbacDecoders;
174  }
175  if (m_pcBufferLowLatBinCABACs)
176  {
177    delete [] m_pcBufferLowLatBinCABACs;
178  }
179  m_pcBufferLowLatSbacDecoders = new TDecSbac    [uiTilesAcross]; 
180  m_pcBufferLowLatBinCABACs    = new TDecBinCABAC[uiTilesAcross];
181  for (UInt ui = 0; ui < uiTilesAcross; ui++)
182  {
183    m_pcBufferLowLatSbacDecoders[ui].init(&m_pcBufferLowLatBinCABACs[ui]);
184  }
185  //save init. state
186  for (UInt ui = 0; ui < uiTilesAcross; ui++)
187  {
188    m_pcBufferLowLatSbacDecoders[ui].load(pcSbacDecoder);
189  }
190
191  UInt uiWidthInLCUs  = rpcPic->getPicSym()->getFrameWidthInCU();
192  //UInt uiHeightInLCUs = rpcPic->getPicSym()->getFrameHeightInCU();
193  UInt uiCol=0, uiLin=0, uiSubStrm=0;
194
195  UInt uiTileCol;
196  UInt uiTileStartLCU;
197  UInt uiTileLCUX;
198  Int iNumSubstreamsPerTile = 1; // if independent.
199
200  Bool depSliceSegmentsEnabled = rpcPic->getSlice(rpcPic->getCurrSliceIdx())->getPPS()->getDependentSliceSegmentsEnabledFlag();
201  uiTileStartLCU = rpcPic->getPicSym()->getTComTile(rpcPic->getPicSym()->getTileIdxMap(iStartCUAddr))->getFirstCUAddr();
202  if( depSliceSegmentsEnabled )
203  {
204    if( (!rpcPic->getSlice(rpcPic->getCurrSliceIdx())->isNextSlice()) &&
205       iStartCUAddr != rpcPic->getPicSym()->getTComTile(rpcPic->getPicSym()->getTileIdxMap(iStartCUAddr))->getFirstCUAddr())
206    {
207      if(pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag())
208      {
209        uiTileCol = rpcPic->getPicSym()->getTileIdxMap(iStartCUAddr) % (rpcPic->getPicSym()->getNumColumnsMinus1()+1);
210        m_pcBufferSbacDecoders[uiTileCol].loadContexts( CTXMem[1]  );//2.LCU
211        if ( (iStartCUAddr%uiWidthInLCUs+1) >= uiWidthInLCUs  )
212        {
213          uiTileLCUX = uiTileStartLCU % uiWidthInLCUs;
214          uiCol     = iStartCUAddr % uiWidthInLCUs;
215          if(uiCol==uiTileLCUX)
216          {
217            CTXMem[0]->loadContexts(pcSbacDecoder);
218          }
219        }
220      }
221      pcSbacDecoder->loadContexts(CTXMem[0] ); //end of depSlice-1
222      pcSbacDecoders[uiSubStrm].loadContexts(pcSbacDecoder);
223    }
224    else
225    {
226      if(pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag())
227      {
228        CTXMem[1]->loadContexts(pcSbacDecoder);
229      }
230      CTXMem[0]->loadContexts(pcSbacDecoder);
231    }
232  }
233  for( Int iCUAddr = iStartCUAddr; !uiIsLast && iCUAddr < rpcPic->getNumCUsInFrame(); iCUAddr = rpcPic->getPicSym()->xCalculateNxtCUAddr(iCUAddr) )
234  {
235    pcCU = rpcPic->getCU( iCUAddr );
236    pcCU->initCU( rpcPic, iCUAddr );
237    uiTileCol = rpcPic->getPicSym()->getTileIdxMap(iCUAddr) % (rpcPic->getPicSym()->getNumColumnsMinus1()+1); // what column of tiles are we in?
238    uiTileStartLCU = rpcPic->getPicSym()->getTComTile(rpcPic->getPicSym()->getTileIdxMap(iCUAddr))->getFirstCUAddr();
239    uiTileLCUX = uiTileStartLCU % uiWidthInLCUs;
240    uiCol     = iCUAddr % uiWidthInLCUs;
241    // The 'line' is now relative to the 1st line in the slice, not the 1st line in the picture.
242    uiLin     = (iCUAddr/uiWidthInLCUs)-(iStartCUAddr/uiWidthInLCUs);
243    // inherit from TR if necessary, select substream to use.
244    if( (pcSlice->getPPS()->getNumSubstreams() > 1) || ( depSliceSegmentsEnabled  && (uiCol == uiTileLCUX)&&(pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag()) ))
245    {
246      // independent tiles => substreams are "per tile".  iNumSubstreams has already been multiplied.
247      iNumSubstreamsPerTile = iNumSubstreams/rpcPic->getPicSym()->getNumTiles();
248      uiSubStrm = rpcPic->getPicSym()->getTileIdxMap(iCUAddr)*iNumSubstreamsPerTile
249                  + uiLin%iNumSubstreamsPerTile;
250      m_pcEntropyDecoder->setBitstream( ppcSubstreams[uiSubStrm] );
251      // Synchronize cabac probabilities with upper-right LCU if it's available and we're at the start of a line.
252      if (((pcSlice->getPPS()->getNumSubstreams() > 1) || depSliceSegmentsEnabled ) && (uiCol == uiTileLCUX)&&(pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag()))
253      {
254        // We'll sync if the TR is available.
255        TComDataCU *pcCUUp = pcCU->getCUAbove();
256        UInt uiWidthInCU = rpcPic->getFrameWidthInCU();
257        TComDataCU *pcCUTR = NULL;
258        if ( pcCUUp && ((iCUAddr%uiWidthInCU+1) < uiWidthInCU)  )
259        {
260          pcCUTR = rpcPic->getCU( iCUAddr - uiWidthInCU + 1 );
261        }
262        UInt uiMaxParts = 1<<(pcSlice->getSPS()->getMaxCUDepth()<<1);
263
264        if ( (true/*bEnforceSliceRestriction*/ &&
265             ((pcCUTR==NULL) || (pcCUTR->getSlice()==NULL) || 
266             ((pcCUTR->getSCUAddr()+uiMaxParts-1) < pcSlice->getSliceCurStartCUAddr()) ||
267             ((rpcPic->getPicSym()->getTileIdxMap( pcCUTR->getAddr() ) != rpcPic->getPicSym()->getTileIdxMap(iCUAddr)))
268             ))
269           )
270        {
271          // TR not available.
272        }
273        else
274        {
275          // TR is available, we use it.
276          pcSbacDecoders[uiSubStrm].loadContexts( &m_pcBufferSbacDecoders[uiTileCol] );
277        }
278      }
279      pcSbacDecoder->load(&pcSbacDecoders[uiSubStrm]);  //this load is used to simplify the code (avoid to change all the call to pcSbacDecoders)
280    }
281    else if ( pcSlice->getPPS()->getNumSubstreams() <= 1 )
282    {
283      // Set variables to appropriate values to avoid later code change.
284      iNumSubstreamsPerTile = 1;
285    }
286
287    if ( (iCUAddr == rpcPic->getPicSym()->getTComTile(rpcPic->getPicSym()->getTileIdxMap(iCUAddr))->getFirstCUAddr()) && // 1st in tile.
288         (iCUAddr!=0) && (iCUAddr!=rpcPic->getPicSym()->getPicSCUAddr(rpcPic->getSlice(rpcPic->getCurrSliceIdx())->getSliceCurStartCUAddr())/rpcPic->getNumPartInCU())
289         && (iCUAddr!=rpcPic->getPicSym()->getPicSCUAddr(rpcPic->getSlice(rpcPic->getCurrSliceIdx())->getSliceSegmentCurStartCUAddr())/rpcPic->getNumPartInCU())
290         ) // !1st in frame && !1st in slice
291    {
292      if (pcSlice->getPPS()->getNumSubstreams() > 1)
293      {
294        // We're crossing into another tile, tiles are independent.
295        // When tiles are independent, we have "substreams per tile".  Each substream has already been terminated, and we no longer
296        // have to perform it here.
297        // For TILES_DECODER, there can be a header at the start of the 1st substream in a tile.  These are read when the substreams
298        // are extracted, not here.
299      }
300      else
301      {
302        SliceType sliceType  = pcSlice->getSliceType();
303        if (pcSlice->getCabacInitFlag())
304        {
305          switch (sliceType)
306          {
307          case P_SLICE:           // change initialization table to B_SLICE intialization
308            sliceType = B_SLICE; 
309            break;
310          case B_SLICE:           // change initialization table to P_SLICE intialization
311            sliceType = P_SLICE; 
312            break;
313          default     :           // should not occur
314            assert(0);
315          }
316        }
317        m_pcEntropyDecoder->updateContextTables( sliceType, pcSlice->getSliceQp() );
318      }
319     
320    }
321
322#if ENC_DEC_TRACE
323    g_bJustDoIt = g_bEncDecTraceEnable;
324#endif
325    if ( pcSlice->getSPS()->getUseSAO() && (pcSlice->getSaoEnabledFlag()||pcSlice->getSaoEnabledFlagChroma()) )
326    {
327      SAOParam *saoParam = rpcPic->getPicSym()->getSaoParam();
328      saoParam->bSaoFlag[0] = pcSlice->getSaoEnabledFlag();
329      if (iCUAddr == iStartCUAddr)
330      {
331        saoParam->bSaoFlag[1] = pcSlice->getSaoEnabledFlagChroma();
332      }
333      Int numCuInWidth     = saoParam->numCuInWidth;
334      Int cuAddrInSlice = iCUAddr - rpcPic->getPicSym()->getCUOrderMap(pcSlice->getSliceCurStartCUAddr()/rpcPic->getNumPartInCU());
335      Int cuAddrUpInSlice  = cuAddrInSlice - numCuInWidth;
336      Int rx = iCUAddr % numCuInWidth;
337      Int ry = iCUAddr / numCuInWidth;
338      Int allowMergeLeft = 1;
339      Int allowMergeUp   = 1;
340      if (rx!=0)
341      {
342        if (rpcPic->getPicSym()->getTileIdxMap(iCUAddr-1) != rpcPic->getPicSym()->getTileIdxMap(iCUAddr))
343        {
344          allowMergeLeft = 0;
345        }
346      }
347      if (ry!=0)
348      {
349        if (rpcPic->getPicSym()->getTileIdxMap(iCUAddr-numCuInWidth) != rpcPic->getPicSym()->getTileIdxMap(iCUAddr))
350        {
351          allowMergeUp = 0;
352        }
353      }
354      pcSbacDecoder->parseSaoOneLcuInterleaving(rx, ry, saoParam,pcCU, cuAddrInSlice, cuAddrUpInSlice, allowMergeLeft, allowMergeUp);
355    }
356    else if ( pcSlice->getSPS()->getUseSAO() )
357    {
358      Int addr = pcCU->getAddr();
359      SAOParam *saoParam = rpcPic->getPicSym()->getSaoParam();
360      for (Int cIdx=0; cIdx<3; cIdx++)
361      {
362        SaoLcuParam *saoLcuParam = &(saoParam->saoLcuParam[cIdx][addr]);
363        if ( ((cIdx == 0) && !pcSlice->getSaoEnabledFlag()) || ((cIdx == 1 || cIdx == 2) && !pcSlice->getSaoEnabledFlagChroma()))
364        {
365          saoLcuParam->mergeUpFlag   = 0;
366          saoLcuParam->mergeLeftFlag = 0;
367          saoLcuParam->subTypeIdx    = 0;
368          saoLcuParam->typeIdx       = -1;
369          saoLcuParam->offset[0]     = 0;
370          saoLcuParam->offset[1]     = 0;
371          saoLcuParam->offset[2]     = 0;
372          saoLcuParam->offset[3]     = 0;
373        }
374      }
375    }
376    m_pcCuDecoder->decodeCU     ( pcCU, uiIsLast );
377    m_pcCuDecoder->decompressCU ( pcCU );
378   
379#if ENC_DEC_TRACE
380    g_bJustDoIt = g_bEncDecTraceDisable;
381#endif
382    pcSbacDecoders[uiSubStrm].load(pcSbacDecoder);
383
384    if ( uiCol == rpcPic->getPicSym()->getTComTile(rpcPic->getPicSym()->getTileIdxMap(iCUAddr))->getRightEdgePosInCU()
385        && pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag()
386        && !uiIsLast )
387    {
388      // Parse end_of_substream_one_bit for WPP case
389      UInt binVal;
390      pcSbacDecoder->parseTerminatingBit( binVal );
391      assert( binVal );
392    }
393
394    //Store probabilities of second LCU in line into buffer
395    if ( (uiCol == uiTileLCUX+1)&& (depSliceSegmentsEnabled || (pcSlice->getPPS()->getNumSubstreams() > 1)) && (pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag()) )
396    {
397      m_pcBufferSbacDecoders[uiTileCol].loadContexts( &pcSbacDecoders[uiSubStrm] );
398    }
399    if( uiIsLast && depSliceSegmentsEnabled )
400    {
401      if (pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag())
402       {
403         CTXMem[1]->loadContexts( &m_pcBufferSbacDecoders[uiTileCol] );//ctx 2.LCU
404       }
405      CTXMem[0]->loadContexts( pcSbacDecoder );//ctx end of dep.slice
406      return;
407    }
408  }
409}
410
411ParameterSetManagerDecoder::ParameterSetManagerDecoder()
412#if SVC_EXTENSION
413: m_spsBuffer(MAX_NUM_SPS)
414, m_ppsBuffer(MAX_NUM_PPS)
415#else
416: m_vpsBuffer(MAX_NUM_VPS)
417, m_spsBuffer(MAX_NUM_SPS)
418, m_ppsBuffer(MAX_NUM_PPS)
419#endif
420{
421}
422
423ParameterSetManagerDecoder::~ParameterSetManagerDecoder()
424{
425
426}
427
428TComVPS* ParameterSetManagerDecoder::getPrefetchedVPS  (Int vpsId)
429{
430  if (m_vpsBuffer.getPS(vpsId) != NULL )
431  {
432    return m_vpsBuffer.getPS(vpsId);
433  }
434  else
435  {
436    return getVPS(vpsId);
437  }
438}
439
440
441TComSPS* ParameterSetManagerDecoder::getPrefetchedSPS  (Int spsId)
442{
443  if (m_spsBuffer.getPS(spsId) != NULL )
444  {
445    return m_spsBuffer.getPS(spsId);
446  }
447  else
448  {
449    return getSPS(spsId);
450  }
451}
452
453TComPPS* ParameterSetManagerDecoder::getPrefetchedPPS  (Int ppsId)
454{
455  if (m_ppsBuffer.getPS(ppsId) != NULL )
456  {
457    return m_ppsBuffer.getPS(ppsId);
458  }
459  else
460  {
461    return getPPS(ppsId);
462  }
463}
464
465Void     ParameterSetManagerDecoder::applyPrefetchedPS()
466{
467  m_vpsMap.mergePSList(m_vpsBuffer);
468  m_ppsMap.mergePSList(m_ppsBuffer);
469  m_spsMap.mergePSList(m_spsBuffer);
470}
471
472//! \}
Note: See TracBrowser for help on using the repository browser.