source: SHVCSoftware/branches/SHM-2.0-dev/source/Lib/TLibDecoder/TDecSlice.cpp @ 154

Last change on this file since 154 was 125, checked in by seregin, 12 years ago

copy from HM-10.0-dev-SHM

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