source: SHVCSoftware/branches/HM-10.0-dev-SHM/source/Lib/TLibDecoder/TDecSlice.cpp @ 59

Last change on this file since 59 was 54, checked in by seregin, 12 years ago

port simulcast

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