source: SHVCSoftware/trunk/source/Lib/TLibDecoder/TDecSlice.cpp @ 824

Last change on this file since 824 was 644, checked in by seregin, 11 years ago

merge with SHM-5.1-dev branch

  • 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-2014, 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(TDecEntropy* pcEntropyDecoder, TDecCu* pcCuDecoder, UInt* saoMaxOffsetQVal)
108{
109  m_pcEntropyDecoder  = pcEntropyDecoder;
110  m_pcCuDecoder       = pcCuDecoder;
111  m_saoMaxOffsetQVal  = saoMaxOffsetQVal;
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  Bool depSliceSegmentsEnabled = rpcPic->getSlice(rpcPic->getCurrSliceIdx())->getPPS()->getDependentSliceSegmentsEnabledFlag();
200  uiTileStartLCU = rpcPic->getPicSym()->getTComTile(rpcPic->getPicSym()->getTileIdxMap(iStartCUAddr))->getFirstCUAddr();
201  if( depSliceSegmentsEnabled )
202  {
203    if( (!rpcPic->getSlice(rpcPic->getCurrSliceIdx())->isNextSlice()) &&
204       iStartCUAddr != rpcPic->getPicSym()->getTComTile(rpcPic->getPicSym()->getTileIdxMap(iStartCUAddr))->getFirstCUAddr())
205    {
206      if(pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag())
207      {
208        uiTileCol = rpcPic->getPicSym()->getTileIdxMap(iStartCUAddr) % (rpcPic->getPicSym()->getNumColumnsMinus1()+1);
209        m_pcBufferSbacDecoders[uiTileCol].loadContexts( CTXMem[1]  );//2.LCU
210        if ( (iStartCUAddr%uiWidthInLCUs+1) >= uiWidthInLCUs  )
211        {
212          uiTileLCUX = uiTileStartLCU % uiWidthInLCUs;
213          uiCol     = iStartCUAddr % uiWidthInLCUs;
214          if(uiCol==uiTileLCUX)
215          {
216            CTXMem[0]->loadContexts(pcSbacDecoder);
217          }
218        }
219      }
220      pcSbacDecoder->loadContexts(CTXMem[0] ); //end of depSlice-1
221      pcSbacDecoders[uiSubStrm].loadContexts(pcSbacDecoder);
222    }
223    else
224    {
225      if(pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag())
226      {
227        CTXMem[1]->loadContexts(pcSbacDecoder);
228      }
229      CTXMem[0]->loadContexts(pcSbacDecoder);
230    }
231  }
232  for( Int iCUAddr = iStartCUAddr; !uiIsLast && iCUAddr < rpcPic->getNumCUsInFrame(); iCUAddr = rpcPic->getPicSym()->xCalculateNxtCUAddr(iCUAddr) )
233  {
234    pcCU = rpcPic->getCU( iCUAddr );
235    pcCU->initCU( rpcPic, iCUAddr );
236    uiTileCol = rpcPic->getPicSym()->getTileIdxMap(iCUAddr) % (rpcPic->getPicSym()->getNumColumnsMinus1()+1); // what column of tiles are we in?
237    uiTileStartLCU = rpcPic->getPicSym()->getTComTile(rpcPic->getPicSym()->getTileIdxMap(iCUAddr))->getFirstCUAddr();
238    uiTileLCUX = uiTileStartLCU % uiWidthInLCUs;
239    uiCol     = iCUAddr % uiWidthInLCUs;
240    // The 'line' is now relative to the 1st line in the slice, not the 1st line in the picture.
241    uiLin     = (iCUAddr/uiWidthInLCUs)-(iStartCUAddr/uiWidthInLCUs);
242    // inherit from TR if necessary, select substream to use.
243    if( (pcSlice->getPPS()->getNumSubstreams() > 1) || ( depSliceSegmentsEnabled  && (uiCol == uiTileLCUX)&&(pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag()) ))
244    {
245      // independent tiles => substreams are "per tile".  iNumSubstreams has already been multiplied.
246      iNumSubstreamsPerTile = iNumSubstreams/rpcPic->getPicSym()->getNumTiles();
247      uiSubStrm = rpcPic->getPicSym()->getTileIdxMap(iCUAddr)*iNumSubstreamsPerTile
248                  + uiLin%iNumSubstreamsPerTile;
249      m_pcEntropyDecoder->setBitstream( ppcSubstreams[uiSubStrm] );
250      // Synchronize cabac probabilities with upper-right LCU if it's available and we're at the start of a line.
251      if (((pcSlice->getPPS()->getNumSubstreams() > 1) || depSliceSegmentsEnabled ) && (uiCol == uiTileLCUX)&&(pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag()))
252      {
253        // We'll sync if the TR is available.
254        TComDataCU *pcCUUp = pcCU->getCUAbove();
255        UInt uiWidthInCU = rpcPic->getFrameWidthInCU();
256        TComDataCU *pcCUTR = NULL;
257        if ( pcCUUp && ((iCUAddr%uiWidthInCU+1) < uiWidthInCU)  )
258        {
259          pcCUTR = rpcPic->getCU( iCUAddr - uiWidthInCU + 1 );
260        }
261        UInt uiMaxParts = 1<<(pcSlice->getSPS()->getMaxCUDepth()<<1);
262
263        if ( (true/*bEnforceSliceRestriction*/ &&
264             ((pcCUTR==NULL) || (pcCUTR->getSlice()==NULL) || 
265             ((pcCUTR->getSCUAddr()+uiMaxParts-1) < pcSlice->getSliceCurStartCUAddr()) ||
266             ((rpcPic->getPicSym()->getTileIdxMap( pcCUTR->getAddr() ) != rpcPic->getPicSym()->getTileIdxMap(iCUAddr)))
267             ))
268           )
269        {
270          // TR not available.
271        }
272        else
273        {
274          // TR is available, we use it.
275          pcSbacDecoders[uiSubStrm].loadContexts( &m_pcBufferSbacDecoders[uiTileCol] );
276        }
277      }
278      pcSbacDecoder->load(&pcSbacDecoders[uiSubStrm]);  //this load is used to simplify the code (avoid to change all the call to pcSbacDecoders)
279    }
280    else if ( pcSlice->getPPS()->getNumSubstreams() <= 1 )
281    {
282      // Set variables to appropriate values to avoid later code change.
283      iNumSubstreamsPerTile = 1;
284    }
285
286    if ( (iCUAddr == rpcPic->getPicSym()->getTComTile(rpcPic->getPicSym()->getTileIdxMap(iCUAddr))->getFirstCUAddr()) && // 1st in tile.
287         (iCUAddr!=0) && (iCUAddr!=rpcPic->getPicSym()->getPicSCUAddr(rpcPic->getSlice(rpcPic->getCurrSliceIdx())->getSliceCurStartCUAddr())/rpcPic->getNumPartInCU())
288         && (iCUAddr!=rpcPic->getPicSym()->getPicSCUAddr(rpcPic->getSlice(rpcPic->getCurrSliceIdx())->getSliceSegmentCurStartCUAddr())/rpcPic->getNumPartInCU())
289         ) // !1st in frame && !1st in slice
290    {
291      if (pcSlice->getPPS()->getNumSubstreams() > 1)
292      {
293        // We're crossing into another tile, tiles are independent.
294        // When tiles are independent, we have "substreams per tile".  Each substream has already been terminated, and we no longer
295        // have to perform it here.
296        // For TILES_DECODER, there can be a header at the start of the 1st substream in a tile.  These are read when the substreams
297        // are extracted, not here.
298      }
299      else
300      {
301        SliceType sliceType  = pcSlice->getSliceType();
302        if (pcSlice->getCabacInitFlag())
303        {
304          switch (sliceType)
305          {
306          case P_SLICE:           // change initialization table to B_SLICE intialization
307            sliceType = B_SLICE; 
308            break;
309          case B_SLICE:           // change initialization table to P_SLICE intialization
310            sliceType = P_SLICE; 
311            break;
312          default     :           // should not occur
313            assert(0);
314          }
315        }
316        m_pcEntropyDecoder->updateContextTables( sliceType, pcSlice->getSliceQp() );
317      }
318     
319    }
320
321#if ENC_DEC_TRACE
322    g_bJustDoIt = g_bEncDecTraceEnable;
323#endif
324
325    if ( pcSlice->getSPS()->getUseSAO() )
326    {
327      SAOBlkParam& saoblkParam = (rpcPic->getPicSym()->getSAOBlkParam())[iCUAddr];
328      if (pcSlice->getSaoEnabledFlag()||pcSlice->getSaoEnabledFlagChroma())
329      {
330        Bool sliceEnabled[NUM_SAO_COMPONENTS];
331        sliceEnabled[SAO_Y] = pcSlice->getSaoEnabledFlag();
332        sliceEnabled[SAO_Cb]= sliceEnabled[SAO_Cr]= pcSlice->getSaoEnabledFlagChroma();
333
334        Bool leftMergeAvail = false;
335        Bool aboveMergeAvail= false;
336
337        //merge left condition
338        Int rx = (iCUAddr % uiWidthInLCUs);
339        if(rx > 0)
340        {
341          leftMergeAvail = rpcPic->getSAOMergeAvailability(iCUAddr, iCUAddr-1);
342        }
343        //merge up condition
344        Int ry = (iCUAddr / uiWidthInLCUs);
345        if(ry > 0)
346        {
347          aboveMergeAvail = rpcPic->getSAOMergeAvailability(iCUAddr, iCUAddr-uiWidthInLCUs);
348        }
349#if SVC_EXTENSION
350        pcSbacDecoder->parseSAOBlkParam( saoblkParam, m_saoMaxOffsetQVal, sliceEnabled, leftMergeAvail, aboveMergeAvail);
351#else
352        pcSbacDecoder->parseSAOBlkParam( saoblkParam, sliceEnabled, leftMergeAvail, aboveMergeAvail);
353#endif
354      }
355      else 
356      {
357        saoblkParam[SAO_Y ].modeIdc = SAO_MODE_OFF;
358        saoblkParam[SAO_Cb].modeIdc = SAO_MODE_OFF;
359        saoblkParam[SAO_Cr].modeIdc = SAO_MODE_OFF;
360      }
361    }
362
363    m_pcCuDecoder->decodeCU     ( pcCU, uiIsLast );
364    m_pcCuDecoder->decompressCU ( pcCU );
365   
366#if ENC_DEC_TRACE
367    g_bJustDoIt = g_bEncDecTraceDisable;
368#endif
369    pcSbacDecoders[uiSubStrm].load(pcSbacDecoder);
370
371    if ( uiCol == rpcPic->getPicSym()->getTComTile(rpcPic->getPicSym()->getTileIdxMap(iCUAddr))->getRightEdgePosInCU()
372        && pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag()
373        && !uiIsLast )
374    {
375      // Parse end_of_substream_one_bit for WPP case
376      UInt binVal;
377      pcSbacDecoder->parseTerminatingBit( binVal );
378      assert( binVal );
379    }
380
381    //Store probabilities of second LCU in line into buffer
382    if ( (uiCol == uiTileLCUX+1)&& (depSliceSegmentsEnabled || (pcSlice->getPPS()->getNumSubstreams() > 1)) && (pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag()) )
383    {
384      m_pcBufferSbacDecoders[uiTileCol].loadContexts( &pcSbacDecoders[uiSubStrm] );
385    }
386    if( uiIsLast && depSliceSegmentsEnabled )
387    {
388      if (pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag())
389       {
390         CTXMem[1]->loadContexts( &m_pcBufferSbacDecoders[uiTileCol] );//ctx 2.LCU
391       }
392      CTXMem[0]->loadContexts( pcSbacDecoder );//ctx end of dep.slice
393      return;
394    }
395  }
396}
397
398ParameterSetManagerDecoder::ParameterSetManagerDecoder()
399#if SVC_EXTENSION
400: m_spsBuffer(MAX_NUM_SPS)
401, m_ppsBuffer(MAX_NUM_PPS)
402#else
403: m_vpsBuffer(MAX_NUM_VPS)
404, m_spsBuffer(MAX_NUM_SPS)
405, m_ppsBuffer(MAX_NUM_PPS)
406#endif
407{
408}
409
410ParameterSetManagerDecoder::~ParameterSetManagerDecoder()
411{
412
413}
414
415TComVPS* ParameterSetManagerDecoder::getPrefetchedVPS  (Int vpsId)
416{
417  if (m_vpsBuffer.getPS(vpsId) != NULL )
418  {
419    return m_vpsBuffer.getPS(vpsId);
420  }
421  else
422  {
423    return getVPS(vpsId);
424  }
425}
426
427
428TComSPS* ParameterSetManagerDecoder::getPrefetchedSPS  (Int spsId)
429{
430  if (m_spsBuffer.getPS(spsId) != NULL )
431  {
432    return m_spsBuffer.getPS(spsId);
433  }
434  else
435  {
436    return getSPS(spsId);
437  }
438}
439
440TComPPS* ParameterSetManagerDecoder::getPrefetchedPPS  (Int ppsId)
441{
442  if (m_ppsBuffer.getPS(ppsId) != NULL )
443  {
444    return m_ppsBuffer.getPS(ppsId);
445  }
446  else
447  {
448    return getPPS(ppsId);
449  }
450}
451
452Void     ParameterSetManagerDecoder::applyPrefetchedPS()
453{
454  m_vpsMap.mergePSList(m_vpsBuffer);
455  m_ppsMap.mergePSList(m_ppsBuffer);
456  m_spsMap.mergePSList(m_spsBuffer);
457}
458
459//! \}
Note: See TracBrowser for help on using the repository browser.