source: 3DVCSoftware/tags/HTM-DEV-0.1/source/Lib/TLibDecoder/TDecSlice.cpp @ 324

Last change on this file since 324 was 324, checked in by tech, 11 years ago

Initial development version for update to latest HM version.
Includes MV-HEVC and basic extensions for 3D-HEVC.

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