source: 3DVCSoftware/branches/HTM-DEV-0.1-dev/source/Lib/TLibCommon/TComSlice.cpp @ 366

Last change on this file since 366 was 366, checked in by tech, 12 years ago

Further minor cleanups.

  • Property svn:eol-style set to native
File size: 74.8 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     TComSlice.cpp
35    \brief    slice header and SPS class
36*/
37
38#include "CommonDef.h"
39#include "TComSlice.h"
40#include "TComPic.h"
41#include "TLibEncoder/TEncSbac.h"
42#include "TLibDecoder/TDecSbac.h"
43
44//! \ingroup TLibCommon
45//! \{
46
47TComSlice::TComSlice()
48: m_iPPSId                        ( -1 )
49, m_iPOC                          ( 0 )
50, m_iLastIDR                      ( 0 )
51, m_eNalUnitType                  ( NAL_UNIT_CODED_SLICE_IDR_W_RADL )
52, m_eSliceType                    ( I_SLICE )
53, m_iSliceQp                      ( 0 )
54, m_dependentSliceSegmentFlag            ( false )
55#if ADAPTIVE_QP_SELECTION
56, m_iSliceQpBase                  ( 0 )
57#endif
58, m_deblockingFilterDisable        ( false )
59, m_deblockingFilterOverrideFlag   ( false )
60, m_deblockingFilterBetaOffsetDiv2 ( 0 )
61, m_deblockingFilterTcOffsetDiv2   ( 0 )
62#if !L0034_COMBINED_LIST_CLEANUP
63, m_bRefPicListModificationFlagLC ( false )
64, m_bRefPicListCombinationFlag    ( false )
65#endif
66, m_bCheckLDC                     ( false )
67, m_iSliceQpDelta                 ( 0 )
68, m_iSliceQpDeltaCb               ( 0 )
69, m_iSliceQpDeltaCr               ( 0 )
70, m_iDepth                        ( 0 )
71, m_bRefenced                     ( false )
72, m_pcSPS                         ( NULL )
73, m_pcPPS                         ( NULL )
74, m_pcPic                         ( NULL )
75, m_colFromL0Flag                 ( 1 )
76, m_colRefIdx                     ( 0 )
77#if SAO_CHROMA_LAMBDA
78, m_dLambdaLuma( 0.0 )
79, m_dLambdaChroma( 0.0 )
80#else
81, m_dLambda                       ( 0.0 )
82#endif
83#if !L0034_COMBINED_LIST_CLEANUP
84, m_bNoBackPredFlag               ( false )
85#endif
86, m_uiTLayer                      ( 0 )
87, m_bTLayerSwitchingFlag          ( false )
88, m_sliceMode                   ( 0 )
89, m_sliceArgument               ( 0 )
90, m_sliceCurStartCUAddr         ( 0 )
91, m_sliceCurEndCUAddr           ( 0 )
92, m_sliceIdx                    ( 0 )
93, m_sliceSegmentMode            ( 0 )
94, m_sliceSegmentArgument        ( 0 )
95, m_sliceSegmentCurStartCUAddr  ( 0 )
96, m_sliceSegmentCurEndCUAddr    ( 0 )
97, m_nextSlice                    ( false )
98, m_nextSliceSegment             ( false )
99, m_sliceBits                   ( 0 )
100, m_sliceSegmentBits         ( 0 )
101, m_bFinalized                    ( false )
102, m_uiTileOffstForMultES          ( 0 )
103, m_puiSubstreamSizes             ( NULL )
104, m_cabacInitFlag                 ( false )
105, m_bLMvdL1Zero                   ( false )
106, m_numEntryPointOffsets          ( 0 )
107, m_temporalLayerNonReferenceFlag ( false )
108, m_enableTMVPFlag                ( true )
109#if H_MV
110, m_layerId                       (0)
111, m_viewId                        (0)
112#if H_3D
113, m_isDepth                       (false)
114#endif
115#endif
116{
117#if L0034_COMBINED_LIST_CLEANUP
118  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = 0;
119#else
120  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = m_aiNumRefIdx[2] = 0;
121#endif
122 
123  initEqualRef();
124 
125#if L0034_COMBINED_LIST_CLEANUP
126  for ( Int idx = 0; idx < MAX_NUM_REF; idx++ )
127  {
128    m_list1IdxToList0Idx[idx] = -1;
129  }
130#else
131  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF_LC; iNumCount++)
132  {
133    m_iRefIdxOfLC[REF_PIC_LIST_0][iNumCount]=-1;
134    m_iRefIdxOfLC[REF_PIC_LIST_1][iNumCount]=-1;
135    m_eListIdFromIdxOfLC[iNumCount]=0;
136    m_iRefIdxFromIdxOfLC[iNumCount]=0;
137    m_iRefIdxOfL0FromRefIdxOfL1[iNumCount] = -1;
138    m_iRefIdxOfL1FromRefIdxOfL0[iNumCount] = -1;
139  }   
140#endif
141  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF; iNumCount++)
142  {
143    m_apcRefPicList [0][iNumCount] = NULL;
144    m_apcRefPicList [1][iNumCount] = NULL;
145    m_aiRefPOCList  [0][iNumCount] = 0;
146    m_aiRefPOCList  [1][iNumCount] = 0;
147#if H_MV
148    m_aiRefLayerIdList[0][iNumCount] = 0;
149    m_aiRefLayerIdList[1][iNumCount] = 0;
150#endif
151  }
152  resetWpScaling();
153  initWpAcDcParam();
154  m_saoEnabledFlag = false;
155}
156
157TComSlice::~TComSlice()
158{
159  delete[] m_puiSubstreamSizes;
160  m_puiSubstreamSizes = NULL;
161}
162
163
164Void TComSlice::initSlice()
165{
166  m_aiNumRefIdx[0]      = 0;
167  m_aiNumRefIdx[1]      = 0;
168 
169  m_colFromL0Flag = 1;
170 
171  m_colRefIdx = 0;
172  initEqualRef();
173#if !L0034_COMBINED_LIST_CLEANUP
174  m_bNoBackPredFlag = false;
175  m_bRefPicListCombinationFlag = false;
176  m_bRefPicListModificationFlagLC = false;
177#endif
178  m_bCheckLDC = false;
179  m_iSliceQpDeltaCb = 0;
180  m_iSliceQpDeltaCr = 0;
181
182#if !L0034_COMBINED_LIST_CLEANUP
183  m_aiNumRefIdx[REF_PIC_LIST_C]      = 0;
184#endif
185
186  m_maxNumMergeCand = MRG_MAX_NUM_CANDS;
187
188  m_bFinalized=false;
189
190  m_tileByteLocation.clear();
191  m_cabacInitFlag        = false;
192  m_numEntryPointOffsets = 0;
193  m_enableTMVPFlag = true;
194}
195
196Bool TComSlice::getRapPicFlag()
197{
198  return getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
199      || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
200      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
201      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
202      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
203      || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA;
204}
205
206/**
207 - allocate table to contain substream sizes to be written to the slice header.
208 .
209 \param uiNumSubstreams Number of substreams -- the allocation will be this value - 1.
210 */
211Void  TComSlice::allocSubstreamSizes(UInt uiNumSubstreams)
212{
213  delete[] m_puiSubstreamSizes;
214  m_puiSubstreamSizes = new UInt[uiNumSubstreams > 0 ? uiNumSubstreams-1 : 0];
215}
216
217Void  TComSlice::sortPicList        (TComList<TComPic*>& rcListPic)
218{
219  TComPic*    pcPicExtract;
220  TComPic*    pcPicInsert;
221 
222  TComList<TComPic*>::iterator    iterPicExtract;
223  TComList<TComPic*>::iterator    iterPicExtract_1;
224  TComList<TComPic*>::iterator    iterPicInsert;
225 
226  for (Int i = 1; i < (Int)(rcListPic.size()); i++)
227  {
228    iterPicExtract = rcListPic.begin();
229    for (Int j = 0; j < i; j++) iterPicExtract++;
230    pcPicExtract = *(iterPicExtract);
231    pcPicExtract->setCurrSliceIdx(0);
232   
233    iterPicInsert = rcListPic.begin();
234    while (iterPicInsert != iterPicExtract)
235    {
236      pcPicInsert = *(iterPicInsert);
237      pcPicInsert->setCurrSliceIdx(0);
238      if (pcPicInsert->getPOC() >= pcPicExtract->getPOC())
239      {
240        break;
241      }
242     
243      iterPicInsert++;
244    }
245   
246    iterPicExtract_1 = iterPicExtract;    iterPicExtract_1++;
247   
248    //  swap iterPicExtract and iterPicInsert, iterPicExtract = curr. / iterPicInsert = insertion position
249    rcListPic.insert (iterPicInsert, iterPicExtract, iterPicExtract_1);
250    rcListPic.erase  (iterPicExtract);
251  }
252}
253
254TComPic* TComSlice::xGetRefPic (TComList<TComPic*>& rcListPic,
255                                Int                 poc)
256{
257  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
258  TComPic*                      pcPic = *(iterPic);
259  while ( iterPic != rcListPic.end() )
260  {
261    if(pcPic->getPOC() == poc)
262    {
263      break;
264    }
265    iterPic++;
266    pcPic = *(iterPic);
267  }
268  return  pcPic;
269}
270
271
272TComPic* TComSlice::xGetLongTermRefPic(TComList<TComPic*>& rcListPic, Int poc, Bool pocHasMsb)
273{
274  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
275  TComPic*                      pcPic = *(iterPic);
276  TComPic*                      pcStPic = pcPic;
277 
278  Int pocCycle = 1 << getSPS()->getBitsForPOC();
279  if (!pocHasMsb)
280  {
281    poc = poc % pocCycle;
282  }
283 
284  while ( iterPic != rcListPic.end() )
285  {
286    pcPic = *(iterPic);
287    if (pcPic && pcPic->getPOC()!=this->getPOC() && pcPic->getSlice( 0 )->isReferenced())
288    {
289      Int picPoc = pcPic->getPOC();
290      if (!pocHasMsb)
291      {
292        picPoc = picPoc % pocCycle;
293      }
294     
295      if (poc == picPoc)
296    {
297      if(pcPic->getIsLongTerm())
298      {
299        return pcPic;
300      }
301      else
302      {
303        pcStPic = pcPic;
304      }
305      break;
306    }
307    }
308
309    iterPic++;
310  }
311 
312  return  pcStPic;
313}
314
315Void TComSlice::setRefPOCList       ()
316{
317  for (Int iDir = 0; iDir < 2; iDir++)
318  {
319    for (Int iNumRefIdx = 0; iNumRefIdx < m_aiNumRefIdx[iDir]; iNumRefIdx++)
320    {
321      m_aiRefPOCList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getPOC();
322#if H_MV
323      m_aiRefLayerIdList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getLayerId();
324#endif
325    }
326  }
327
328}
329
330#if L0034_COMBINED_LIST_CLEANUP
331Void TComSlice::setList1IdxToList0Idx()
332{
333  Int idxL0, idxL1;
334  for ( idxL1 = 0; idxL1 < getNumRefIdx( REF_PIC_LIST_1 ); idxL1++ )
335  {
336    m_list1IdxToList0Idx[idxL1] = -1;
337    for ( idxL0 = 0; idxL0 < getNumRefIdx( REF_PIC_LIST_0 ); idxL0++ )
338    {
339      if ( m_apcRefPicList[REF_PIC_LIST_0][idxL0]->getPOC() == m_apcRefPicList[REF_PIC_LIST_1][idxL1]->getPOC() )
340      {
341        m_list1IdxToList0Idx[idxL1] = idxL0;
342        break;
343      }
344    }
345  }
346}
347#else
348Void TComSlice::generateCombinedList()
349{
350  if(m_aiNumRefIdx[REF_PIC_LIST_C] > 0)
351  {
352    m_aiNumRefIdx[REF_PIC_LIST_C]=0;
353    for(Int iNumCount = 0; iNumCount < MAX_NUM_REF_LC; iNumCount++)
354    {
355      m_iRefIdxOfLC[REF_PIC_LIST_0][iNumCount]=-1;
356      m_iRefIdxOfLC[REF_PIC_LIST_1][iNumCount]=-1;
357      m_eListIdFromIdxOfLC[iNumCount]=0;
358      m_iRefIdxFromIdxOfLC[iNumCount]=0;
359      m_iRefIdxOfL0FromRefIdxOfL1[iNumCount] = -1;
360      m_iRefIdxOfL1FromRefIdxOfL0[iNumCount] = -1;
361    }
362
363    for (Int iNumRefIdx = 0; iNumRefIdx < MAX_NUM_REF; iNumRefIdx++)
364    {
365      if(iNumRefIdx < m_aiNumRefIdx[REF_PIC_LIST_0])
366      {
367        Bool bTempRefIdxInL2 = true;
368        for ( Int iRefIdxLC = 0; iRefIdxLC < m_aiNumRefIdx[REF_PIC_LIST_C]; iRefIdxLC++ )
369        {
370#if H_MV
371          if ( m_apcRefPicList[REF_PIC_LIST_0][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() &&
372               m_apcRefPicList[REF_PIC_LIST_0][iNumRefIdx]->getLayerId() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getLayerId() )
373#else
374          if ( m_apcRefPicList[REF_PIC_LIST_0][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() )
375#endif
376          {
377            m_iRefIdxOfL1FromRefIdxOfL0[iNumRefIdx] = m_iRefIdxFromIdxOfLC[iRefIdxLC];
378            m_iRefIdxOfL0FromRefIdxOfL1[m_iRefIdxFromIdxOfLC[iRefIdxLC]] = iNumRefIdx;
379            bTempRefIdxInL2 = false;
380            break;
381          }
382        }
383
384        if(bTempRefIdxInL2 == true)
385        { 
386          m_eListIdFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = REF_PIC_LIST_0;
387          m_iRefIdxFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = iNumRefIdx;
388          m_iRefIdxOfLC[REF_PIC_LIST_0][iNumRefIdx] = m_aiNumRefIdx[REF_PIC_LIST_C]++;
389        }
390      }
391
392      if(iNumRefIdx < m_aiNumRefIdx[REF_PIC_LIST_1])
393      {
394        Bool bTempRefIdxInL2 = true;
395        for ( Int iRefIdxLC = 0; iRefIdxLC < m_aiNumRefIdx[REF_PIC_LIST_C]; iRefIdxLC++ )
396        {
397#if H_MV
398          if ( m_apcRefPicList[REF_PIC_LIST_1][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() &&
399               m_apcRefPicList[REF_PIC_LIST_1][iNumRefIdx]->getLayerId() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getLayerId() )
400#else
401          if ( m_apcRefPicList[REF_PIC_LIST_1][iNumRefIdx]->getPOC() == m_apcRefPicList[m_eListIdFromIdxOfLC[iRefIdxLC]][m_iRefIdxFromIdxOfLC[iRefIdxLC]]->getPOC() )
402#endif
403          {
404            m_iRefIdxOfL0FromRefIdxOfL1[iNumRefIdx] = m_iRefIdxFromIdxOfLC[iRefIdxLC];
405            m_iRefIdxOfL1FromRefIdxOfL0[m_iRefIdxFromIdxOfLC[iRefIdxLC]] = iNumRefIdx;
406            bTempRefIdxInL2 = false;
407            break;
408          }
409        }
410        if(bTempRefIdxInL2 == true)
411        {
412          m_eListIdFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = REF_PIC_LIST_1;
413          m_iRefIdxFromIdxOfLC[m_aiNumRefIdx[REF_PIC_LIST_C]] = iNumRefIdx;
414          m_iRefIdxOfLC[REF_PIC_LIST_1][iNumRefIdx] = m_aiNumRefIdx[REF_PIC_LIST_C]++;
415        }
416      }
417    }
418  }
419}
420#endif
421
422#if H_MV
423Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, std::vector<TComPic*>& refPicSetInterLayer , Bool checkNumPocTotalCurr)
424#else
425#if FIX1071
426Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr )
427#else
428Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic )
429#endif
430#endif
431{
432#if FIX1071
433  if (!checkNumPocTotalCurr)
434#endif
435  {
436    if (m_eSliceType == I_SLICE)
437    {
438      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
439      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
440     
441      return;
442    }
443   
444#if !H_MV
445    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
446    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
447#endif
448  }
449
450  TComPic*  pcRefPic= NULL;
451  TComPic*  RefPicSetStCurr0[16];
452  TComPic*  RefPicSetStCurr1[16];
453  TComPic*  RefPicSetLtCurr[16];
454  UInt NumPocStCurr0 = 0;
455  UInt NumPocStCurr1 = 0;
456  UInt NumPocLtCurr = 0;
457#if H_MV
458  Int numDirectRefLayers  = getVPS()->getNumDirectRefLayers( getLayerIdInVps() );
459  assert( numDirectRefLayers == refPicSetInterLayer.size() ); 
460#endif
461  Int i;
462
463  for(i=0; i < m_pcRPS->getNumberOfNegativePictures(); i++)
464  {
465    if(m_pcRPS->getUsed(i))
466    {
467      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
468      pcRefPic->setIsLongTerm(0);
469      pcRefPic->getPicYuvRec()->extendPicBorder();
470      RefPicSetStCurr0[NumPocStCurr0] = pcRefPic;
471      NumPocStCurr0++;
472      pcRefPic->setCheckLTMSBPresent(false); 
473    }
474  }
475 
476  for(; i < m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures(); i++)
477  {
478    if(m_pcRPS->getUsed(i))
479    {
480      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
481      pcRefPic->setIsLongTerm(0);
482      pcRefPic->getPicYuvRec()->extendPicBorder();
483      RefPicSetStCurr1[NumPocStCurr1] = pcRefPic;
484      NumPocStCurr1++;
485      pcRefPic->setCheckLTMSBPresent(false); 
486    }
487  }
488 
489  for(i = m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()+m_pcRPS->getNumberOfLongtermPictures()-1; i > m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()-1 ; i--)
490  {
491    if(m_pcRPS->getUsed(i))
492    {
493      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
494      pcRefPic->setIsLongTerm(1);
495      pcRefPic->getPicYuvRec()->extendPicBorder();
496      RefPicSetLtCurr[NumPocLtCurr] = pcRefPic;
497      NumPocLtCurr++;
498    }
499    if(pcRefPic==NULL) 
500    {
501      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
502    }
503    pcRefPic->setCheckLTMSBPresent(m_pcRPS->getCheckLTMSBPresent(i)); 
504  }
505
506  // ref_pic_list_init
507  TComPic*  rpsCurrList0[MAX_NUM_REF+1];
508  TComPic*  rpsCurrList1[MAX_NUM_REF+1];
509#if H_MV
510  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr + numDirectRefLayers;
511  assert( numPocTotalCurr == getNumRpsCurrTempList() );
512#else
513  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr;
514#endif
515#if FIX1071
516  if (checkNumPocTotalCurr)
517  {
518    // The variable NumPocTotalCurr is derived as specified in subclause 7.4.7.2. It is a requirement of bitstream conformance that the following applies to the value of NumPocTotalCurr:
519    // – If the current picture is a BLA or CRA picture, the value of NumPocTotalCurr shall be equal to 0.
520    // – Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
521    if (getRapPicFlag())
522    {
523      assert(numPocTotalCurr == 0);
524    }
525
526    if (m_eSliceType == I_SLICE)
527    {
528      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
529      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
530     
531      return;
532    }
533   
534    assert(numPocTotalCurr != 0);
535   
536    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
537    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
538  }
539#endif
540
541  Int cIdx = 0;
542  for ( i=0; i<NumPocStCurr0; i++, cIdx++)
543  {
544    rpsCurrList0[cIdx] = RefPicSetStCurr0[i];
545  }
546  for ( i=0; i<NumPocStCurr1; i++, cIdx++)
547  {
548    rpsCurrList0[cIdx] = RefPicSetStCurr1[i];
549  }
550  for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
551  {
552    rpsCurrList0[cIdx] = RefPicSetLtCurr[i];
553  }
554#if H_MV
555  for ( i=0; i<numDirectRefLayers;  i++, cIdx++)
556  {
557    if( cIdx <= MAX_NUM_REF )
558    {
559      rpsCurrList0[cIdx] = refPicSetInterLayer[i];
560    }
561  }
562#endif
563
564  if (m_eSliceType==B_SLICE)
565  {
566    cIdx = 0;
567    for ( i=0; i<NumPocStCurr1; i++, cIdx++)
568    {
569      rpsCurrList1[cIdx] = RefPicSetStCurr1[i];
570    }
571    for ( i=0; i<NumPocStCurr0; i++, cIdx++)
572    {
573      rpsCurrList1[cIdx] = RefPicSetStCurr0[i];
574    }
575    for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
576    {
577      rpsCurrList1[cIdx] = RefPicSetLtCurr[i];
578    }
579#if H_MV
580    for ( i=0; i<numDirectRefLayers;  i++, cIdx++)
581    {
582      if( cIdx <= MAX_NUM_REF )
583      {
584        rpsCurrList1[cIdx] = refPicSetInterLayer[i];
585      }
586    }
587#endif
588  }
589
590  ::memset(m_bIsUsedAsLongTerm, 0, sizeof(m_bIsUsedAsLongTerm));
591
592  for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[0]-1); rIdx ++)
593  {
594    m_apcRefPicList[0][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL0() ? rpsCurrList0[ m_RefPicListModification.getRefPicSetIdxL0(rIdx) ] : rpsCurrList0[rIdx % numPocTotalCurr];
595    m_bIsUsedAsLongTerm[0][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL0() ? (m_RefPicListModification.getRefPicSetIdxL0(rIdx) >= (NumPocStCurr0 + NumPocStCurr1))
596                                  : ((rIdx % numPocTotalCurr) >= (NumPocStCurr0 + NumPocStCurr1));
597  }
598  if ( m_eSliceType == P_SLICE )
599  {
600    m_aiNumRefIdx[1] = 0;
601    ::memset( m_apcRefPicList[1], 0, sizeof(m_apcRefPicList[1]));
602  }
603  else
604  {
605    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[1]-1); rIdx ++)
606    {
607      m_apcRefPicList[1][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL1() ? rpsCurrList1[ m_RefPicListModification.getRefPicSetIdxL1(rIdx) ] : rpsCurrList1[rIdx % numPocTotalCurr];
608      m_bIsUsedAsLongTerm[1][rIdx] = m_RefPicListModification.getRefPicListModificationFlagL1() ?
609                                  (m_RefPicListModification.getRefPicSetIdxL1(rIdx) >= (NumPocStCurr0 + NumPocStCurr1)): ((rIdx % numPocTotalCurr) >= (NumPocStCurr0 + NumPocStCurr1));
610    }
611  }
612}
613
614Int TComSlice::getNumRpsCurrTempList()
615{
616  Int numRpsCurrTempList = 0;
617
618  if (m_eSliceType == I_SLICE) 
619  {
620    return 0;
621  }
622  for(UInt i=0; i < m_pcRPS->getNumberOfNegativePictures()+ m_pcRPS->getNumberOfPositivePictures() + m_pcRPS->getNumberOfLongtermPictures(); i++)
623  {
624    if(m_pcRPS->getUsed(i))
625    {
626      numRpsCurrTempList++;
627    }
628  }
629#if H_MV
630  numRpsCurrTempList = numRpsCurrTempList + getVPS()->getNumDirectRefLayers( getLayerIdInVps() );
631#endif
632  return numRpsCurrTempList;
633}
634
635Void TComSlice::initEqualRef()
636{
637  for (Int iDir = 0; iDir < 2; iDir++)
638  {
639    for (Int iRefIdx1 = 0; iRefIdx1 < MAX_NUM_REF; iRefIdx1++)
640    {
641      for (Int iRefIdx2 = iRefIdx1; iRefIdx2 < MAX_NUM_REF; iRefIdx2++)
642      {
643        m_abEqualRef[iDir][iRefIdx1][iRefIdx2] = m_abEqualRef[iDir][iRefIdx2][iRefIdx1] = (iRefIdx1 == iRefIdx2? true : false);
644      }
645    }
646  }
647}
648
649Void TComSlice::checkColRefIdx(UInt curSliceIdx, TComPic* pic)
650{
651  Int i;
652  TComSlice* curSlice = pic->getSlice(curSliceIdx);
653  Int currColRefPOC =  curSlice->getRefPOC( RefPicList(1-curSlice->getColFromL0Flag()), curSlice->getColRefIdx());
654  TComSlice* preSlice;
655  Int preColRefPOC;
656  for(i=curSliceIdx-1; i>=0; i--)
657  {
658    preSlice = pic->getSlice(i);
659    if(preSlice->getSliceType() != I_SLICE)
660    {
661      preColRefPOC  = preSlice->getRefPOC( RefPicList(1-preSlice->getColFromL0Flag()), preSlice->getColRefIdx());
662      if(currColRefPOC != preColRefPOC)
663      {
664        printf("Collocated_ref_idx shall always be the same for all slices of a coded picture!\n");
665        exit(EXIT_FAILURE);
666      }
667      else
668      {
669        break;
670      }
671    }
672  }
673}
674
675Void TComSlice::checkCRA(TComReferencePictureSet *pReferencePictureSet, Int& pocCRA, Bool& prevRAPisBLA, TComList<TComPic *>& rcListPic)
676{
677  for(Int i = 0; i < pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i++)
678  {
679    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
680    {
681      assert(getPOC()+pReferencePictureSet->getDeltaPOC(i) >= pocCRA);
682    }
683  }
684  for(Int i = pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i < pReferencePictureSet->getNumberOfPictures(); i++)
685  {
686    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
687    {
688      if (!pReferencePictureSet->getCheckLTMSBPresent(i))
689      {
690        assert(xGetLongTermRefPic(rcListPic, pReferencePictureSet->getPOC(i), false)->getPOC() >= pocCRA);
691      }
692      else
693      {
694      assert(pReferencePictureSet->getPOC(i) >= pocCRA);
695    }
696  }
697  }
698  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP ) // IDR picture found
699  {
700    pocCRA = getPOC();
701    prevRAPisBLA = false;
702  }
703  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
704  {
705    pocCRA = getPOC();
706    prevRAPisBLA = false;
707  }
708  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
709         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
710         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP ) // BLA picture found
711  {
712    pocCRA = getPOC();
713    prevRAPisBLA = true;
714  }
715}
716
717/** Function for marking the reference pictures when an IDR/CRA/CRANT/BLA/BLANT is encountered.
718 * \param pocCRA POC of the CRA/CRANT/BLA/BLANT picture
719 * \param bRefreshPending flag indicating if a deferred decoding refresh is pending
720 * \param rcListPic reference to the reference picture list
721 * This function marks the reference pictures as "unused for reference" in the following conditions.
722 * If the nal_unit_type is IDR/BLA/BLANT, all pictures in the reference picture list 
723 * are marked as "unused for reference"
724 *    If the nal_unit_type is BLA/BLANT, set the pocCRA to the temporal reference of the current picture.
725 * Otherwise
726 *    If the bRefreshPending flag is true (a deferred decoding refresh is pending) and the current
727 *    temporal reference is greater than the temporal reference of the latest CRA/CRANT/BLA/BLANT picture (pocCRA),
728 *    mark all reference pictures except the latest CRA/CRANT/BLA/BLANT picture as "unused for reference" and set
729 *    the bRefreshPending flag to false.
730 *    If the nal_unit_type is CRA/CRANT, set the bRefreshPending flag to true and pocCRA to the temporal
731 *    reference of the current picture.
732 * Note that the current picture is already placed in the reference list and its marking is not changed.
733 * If the current picture has a nal_ref_idc that is not 0, it will remain marked as "used for reference".
734 */
735Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic)
736{
737  TComPic*                 rpcPic;
738  Int pocCurr = getPOC(); 
739
740  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
741    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
742    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
743    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
744    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )  // IDR or BLA picture
745  {
746    // mark all pictures as not used for reference
747    TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
748    while (iterPic != rcListPic.end())
749    {
750      rpcPic = *(iterPic);
751      rpcPic->setCurrSliceIdx(0);
752      if (rpcPic->getPOC() != pocCurr) rpcPic->getSlice(0)->setReferenced(false);
753      iterPic++;
754    }
755    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
756      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
757      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
758    {
759      pocCRA = pocCurr;
760    }
761  }
762  else // CRA or No DR
763  {
764    if (bRefreshPending==true && pocCurr > pocCRA) // CRA reference marking pending
765    {
766      TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
767      while (iterPic != rcListPic.end())
768      {
769        rpcPic = *(iterPic);
770        if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != pocCRA)
771        {
772          rpcPic->getSlice(0)->setReferenced(false);
773        }
774        iterPic++;
775      }
776      bRefreshPending = false; 
777    }
778    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
779    {
780      bRefreshPending = true; 
781      pocCRA = pocCurr;
782    }
783  }
784}
785
786Void TComSlice::copySliceInfo(TComSlice *pSrc)
787{
788  assert( pSrc != NULL );
789
790  Int i, j, k;
791
792  m_iPOC                 = pSrc->m_iPOC;
793  m_eNalUnitType         = pSrc->m_eNalUnitType;
794#if H_MV
795  m_layerId              = pSrc->m_layerId;
796#endif
797  m_eSliceType           = pSrc->m_eSliceType;
798  m_iSliceQp             = pSrc->m_iSliceQp;
799#if ADAPTIVE_QP_SELECTION
800  m_iSliceQpBase         = pSrc->m_iSliceQpBase;
801#endif
802  m_deblockingFilterDisable   = pSrc->m_deblockingFilterDisable;
803  m_deblockingFilterOverrideFlag = pSrc->m_deblockingFilterOverrideFlag;
804  m_deblockingFilterBetaOffsetDiv2 = pSrc->m_deblockingFilterBetaOffsetDiv2;
805  m_deblockingFilterTcOffsetDiv2 = pSrc->m_deblockingFilterTcOffsetDiv2;
806 
807#if L0034_COMBINED_LIST_CLEANUP
808  for (i = 0; i < 2; i++)
809#else
810  for (i = 0; i < 3; i++)
811#endif
812  {
813    m_aiNumRefIdx[i]     = pSrc->m_aiNumRefIdx[i];
814  }
815
816#if L0034_COMBINED_LIST_CLEANUP
817  for (i = 0; i < MAX_NUM_REF; i++)
818  {
819    m_list1IdxToList0Idx[i] = pSrc->m_list1IdxToList0Idx[i];
820  } 
821#else
822  for (i = 0; i < 2; i++)
823  {
824    for (j = 0; j < MAX_NUM_REF_LC; j++)
825    {
826       m_iRefIdxOfLC[i][j]  = pSrc->m_iRefIdxOfLC[i][j];
827    }
828  }
829  for (i = 0; i < MAX_NUM_REF_LC; i++)
830  {
831    m_eListIdFromIdxOfLC[i] = pSrc->m_eListIdFromIdxOfLC[i];
832    m_iRefIdxFromIdxOfLC[i] = pSrc->m_iRefIdxFromIdxOfLC[i];
833    m_iRefIdxOfL1FromRefIdxOfL0[i] = pSrc->m_iRefIdxOfL1FromRefIdxOfL0[i];
834    m_iRefIdxOfL0FromRefIdxOfL1[i] = pSrc->m_iRefIdxOfL0FromRefIdxOfL1[i];
835  }
836  m_bRefPicListModificationFlagLC = pSrc->m_bRefPicListModificationFlagLC;
837  m_bRefPicListCombinationFlag    = pSrc->m_bRefPicListCombinationFlag;
838#endif
839  m_bCheckLDC             = pSrc->m_bCheckLDC;
840  m_iSliceQpDelta        = pSrc->m_iSliceQpDelta;
841  m_iSliceQpDeltaCb      = pSrc->m_iSliceQpDeltaCb;
842  m_iSliceQpDeltaCr      = pSrc->m_iSliceQpDeltaCr;
843  for (i = 0; i < 2; i++)
844  {
845    for (j = 0; j < MAX_NUM_REF; j++)
846    {
847      m_apcRefPicList[i][j]  = pSrc->m_apcRefPicList[i][j];
848      m_aiRefPOCList[i][j]   = pSrc->m_aiRefPOCList[i][j];
849#if H_MV
850      m_aiRefLayerIdList[i][j] = pSrc->m_aiRefLayerIdList[i][j];
851#endif
852    }
853  }
854  for (i = 0; i < 2; i++)
855  {
856    for (j = 0; j < MAX_NUM_REF + 1; j++)
857    {
858      m_bIsUsedAsLongTerm[i][j] = pSrc->m_bIsUsedAsLongTerm[i][j];
859    }
860  }
861  m_iDepth               = pSrc->m_iDepth;
862
863  // referenced slice
864  m_bRefenced            = pSrc->m_bRefenced;
865
866  // access channel
867#if H_MV
868  m_pcVPS                = pSrc->m_pcVPS;
869#endif
870  m_pcSPS                = pSrc->m_pcSPS;
871  m_pcPPS                = pSrc->m_pcPPS;
872  m_pcRPS                = pSrc->m_pcRPS;
873  m_iLastIDR             = pSrc->m_iLastIDR;
874
875  m_pcPic                = pSrc->m_pcPic;
876
877  m_colFromL0Flag        = pSrc->m_colFromL0Flag;
878  m_colRefIdx            = pSrc->m_colRefIdx;
879#if SAO_CHROMA_LAMBDA
880  m_dLambdaLuma          = pSrc->m_dLambdaLuma;
881  m_dLambdaChroma        = pSrc->m_dLambdaChroma;
882#else
883  m_dLambda              = pSrc->m_dLambda;
884#endif
885  for (i = 0; i < 2; i++)
886  {
887    for (j = 0; j < MAX_NUM_REF; j++)
888    {
889      for (k =0; k < MAX_NUM_REF; k++)
890      {
891        m_abEqualRef[i][j][k] = pSrc->m_abEqualRef[i][j][k];
892      }
893    }
894  }
895
896#if !L0034_COMBINED_LIST_CLEANUP
897  m_bNoBackPredFlag      = pSrc->m_bNoBackPredFlag;
898#endif
899  m_uiTLayer                      = pSrc->m_uiTLayer;
900  m_bTLayerSwitchingFlag          = pSrc->m_bTLayerSwitchingFlag;
901
902  m_sliceMode                   = pSrc->m_sliceMode;
903  m_sliceArgument               = pSrc->m_sliceArgument;
904  m_sliceCurStartCUAddr         = pSrc->m_sliceCurStartCUAddr;
905  m_sliceCurEndCUAddr           = pSrc->m_sliceCurEndCUAddr;
906  m_sliceIdx                    = pSrc->m_sliceIdx;
907  m_sliceSegmentMode            = pSrc->m_sliceSegmentMode;
908  m_sliceSegmentArgument        = pSrc->m_sliceSegmentArgument; 
909  m_sliceSegmentCurStartCUAddr  = pSrc->m_sliceSegmentCurStartCUAddr;
910  m_sliceSegmentCurEndCUAddr    = pSrc->m_sliceSegmentCurEndCUAddr;
911  m_nextSlice                    = pSrc->m_nextSlice;
912  m_nextSliceSegment             = pSrc->m_nextSliceSegment;
913  for ( Int e=0 ; e<2 ; e++ )
914  {
915    for ( Int n=0 ; n<MAX_NUM_REF ; n++ )
916    {
917      memcpy(m_weightPredTable[e][n], pSrc->m_weightPredTable[e][n], sizeof(wpScalingParam)*3 );
918    }
919  }
920  m_saoEnabledFlag = pSrc->m_saoEnabledFlag; 
921  m_saoEnabledFlagChroma = pSrc->m_saoEnabledFlagChroma;
922  m_cabacInitFlag                = pSrc->m_cabacInitFlag;
923  m_numEntryPointOffsets  = pSrc->m_numEntryPointOffsets;
924
925  m_bLMvdL1Zero = pSrc->m_bLMvdL1Zero;
926  m_LFCrossSliceBoundaryFlag = pSrc->m_LFCrossSliceBoundaryFlag;
927  m_enableTMVPFlag                = pSrc->m_enableTMVPFlag;
928  m_maxNumMergeCand               = pSrc->m_maxNumMergeCand;
929}
930
931Int TComSlice::m_prevPOC = 0;
932
933/** Function for setting the slice's temporal layer ID and corresponding temporal_layer_switching_point_flag.
934 * \param uiTLayer Temporal layer ID of the current slice
935 * The decoder calls this function to set temporal_layer_switching_point_flag for each temporal layer based on
936 * the SPS's temporal_id_nesting_flag and the parsed PPS.  Then, current slice's temporal layer ID and
937 * temporal_layer_switching_point_flag is set accordingly.
938 */
939Void TComSlice::setTLayerInfo( UInt uiTLayer )
940{
941  m_uiTLayer = uiTLayer;
942}
943
944/** Function for checking if this is a switching-point
945*/
946Bool TComSlice::isTemporalLayerSwitchingPoint( TComList<TComPic*>& rcListPic )
947{
948  TComPic* rpcPic;
949  // loop through all pictures in the reference picture buffer
950  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
951  while ( iterPic != rcListPic.end())
952  {
953    rpcPic = *(iterPic++);
954    if(rpcPic->getSlice(0)->isReferenced() && rpcPic->getPOC() != getPOC())
955    {
956      if(rpcPic->getTLayer() >= getTLayer())
957      {
958        return false;
959      }
960    }
961  }
962  return true;
963}
964
965/** Function for checking if this is a STSA candidate
966 */
967Bool TComSlice::isStepwiseTemporalLayerSwitchingPointCandidate( TComList<TComPic*>& rcListPic )
968{
969    TComPic* rpcPic;
970   
971    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
972    while ( iterPic != rcListPic.end())
973    {
974        rpcPic = *(iterPic++);
975        if(rpcPic->getSlice(0)->isReferenced() &&  (rpcPic->getUsedByCurr()==true) && rpcPic->getPOC() != getPOC())
976        {
977            if(rpcPic->getTLayer() >= getTLayer())
978            {
979                return false;
980            }
981        }
982    }
983    return true;
984}
985
986/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
987*/
988Void TComSlice::applyReferencePictureSet( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
989{
990  TComPic* rpcPic;
991  Int i, isReference;
992
993  // loop through all pictures in the reference picture buffer
994  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
995  while ( iterPic != rcListPic.end())
996  {
997    rpcPic = *(iterPic++);
998
999    if(!rpcPic->getSlice( 0 )->isReferenced())
1000    {
1001      continue;
1002    }
1003
1004    isReference = 0;
1005    // loop through all pictures in the Reference Picture Set
1006    // to see if the picture should be kept as reference picture
1007    for(i=0;i<pReferencePictureSet->getNumberOfPositivePictures()+pReferencePictureSet->getNumberOfNegativePictures();i++)
1008    {
1009      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i))
1010      {
1011        isReference = 1;
1012        rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1013        rpcPic->setIsLongTerm(0);
1014      }
1015    }
1016    for(;i<pReferencePictureSet->getNumberOfPictures();i++)
1017    {
1018      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1019      {
1020        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i))
1021        {
1022          isReference = 1;
1023          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1024        }
1025      }
1026      else 
1027      {
1028        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC())) == pReferencePictureSet->getPOC(i)%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC()))
1029        {
1030          isReference = 1;
1031          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1032        }
1033      }
1034
1035    }
1036    // mark the picture as "unused for reference" if it is not in
1037    // the Reference Picture Set
1038    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && isReference == 0)   
1039    {           
1040      rpcPic->getSlice( 0 )->setReferenced( false );   
1041      rpcPic->setUsedByCurr(0);
1042      rpcPic->setIsLongTerm(0);
1043    }
1044    //check that pictures of higher temporal layers are not used
1045    assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
1046    //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
1047    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
1048    {
1049      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
1050    }
1051    //check that pictures marked as temporal layer non-reference pictures are not used for reference
1052    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && rpcPic->getTLayer()==this->getTLayer())
1053    {
1054      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getSlice( 0 )->getTemporalLayerNonReferenceFlag()==false);
1055    }
1056  }
1057}
1058
1059/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1060*/
1061Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess)
1062{
1063  TComPic* rpcPic;
1064  Int i, isAvailable;
1065  Int atLeastOneLost = 0;
1066  Int atLeastOneRemoved = 0;
1067  Int iPocLost = 0;
1068
1069  // loop through all long-term pictures in the Reference Picture Set
1070  // to see if the picture should be kept as reference picture
1071  for(i=pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i<pReferencePictureSet->getNumberOfPictures();i++)
1072  {
1073    isAvailable = 0;
1074    // loop through all pictures in the reference picture buffer
1075    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1076    while ( iterPic != rcListPic.end())
1077    {
1078      rpcPic = *(iterPic++);
1079      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1080      {
1081        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i) && rpcPic->getSlice(0)->isReferenced())
1082        {
1083          isAvailable = 1;
1084        }
1085      }
1086      else 
1087      {
1088        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC())) == pReferencePictureSet->getPOC(i)%(1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC()) && rpcPic->getSlice(0)->isReferenced())
1089        {
1090          isAvailable = 1;
1091        }
1092      }
1093    }
1094    // if there was no such long-term check the short terms
1095    if(!isAvailable)
1096    {
1097      iterPic = rcListPic.begin();
1098      while ( iterPic != rcListPic.end())
1099      {
1100        rpcPic = *(iterPic++);
1101
1102        Int pocCycle = 1 << rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1103        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC();
1104        Int refPoc = pReferencePictureSet->getPOC(i);
1105        if (!pReferencePictureSet->getCheckLTMSBPresent(i))
1106        {
1107          curPoc = curPoc % pocCycle;
1108          refPoc = refPoc % pocCycle;
1109        }
1110       
1111        if (rpcPic->getSlice(0)->isReferenced() && curPoc == refPoc)
1112        {
1113          isAvailable = 1;
1114          rpcPic->setIsLongTerm(1);
1115          break;
1116        }
1117      }
1118    }
1119    // report that a picture is lost if it is in the Reference Picture Set
1120    // but not available as reference picture
1121    if(isAvailable == 0)   
1122    {           
1123      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1124      {
1125        if(!pReferencePictureSet->getUsed(i) )
1126        {
1127          if(printErrors)
1128          {
1129            printf("\nLong-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1130          }
1131          atLeastOneRemoved = 1;
1132        }
1133        else
1134        {
1135          if(printErrors)
1136          {
1137            printf("\nLong-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1138          }
1139          atLeastOneLost = 1;
1140          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1141        }
1142      }
1143    }
1144  } 
1145  // loop through all short-term pictures in the Reference Picture Set
1146  // to see if the picture should be kept as reference picture
1147  for(i=0;i<pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i++)
1148  {
1149    isAvailable = 0;
1150    // loop through all pictures in the reference picture buffer
1151    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1152    while ( iterPic != rcListPic.end())
1153    {
1154      rpcPic = *(iterPic++);
1155
1156      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1157      {
1158        isAvailable = 1;
1159      }
1160    }
1161    // report that a picture is lost if it is in the Reference Picture Set
1162    // but not available as reference picture
1163    if(isAvailable == 0)   
1164    {           
1165      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1166      {
1167        if(!pReferencePictureSet->getUsed(i) )
1168        {
1169          if(printErrors)
1170          {
1171            printf("\nShort-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1172          }
1173          atLeastOneRemoved = 1;
1174        }
1175        else
1176        {
1177          if(printErrors)
1178          {
1179            printf("\nShort-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1180          }
1181          atLeastOneLost = 1;
1182          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1183        }
1184      }
1185    }
1186  }   
1187  if(atLeastOneLost)
1188  {
1189    return iPocLost+1;
1190  }
1191  if(atLeastOneRemoved)
1192  {
1193    return -2;
1194  }
1195  else
1196  {
1197    return 0;
1198  }
1199}
1200
1201/** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
1202*/
1203Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1204{
1205  TComPic* rpcPic;
1206  Int i, j;
1207  Int k = 0;
1208  Int nrOfNegativePictures = 0;
1209  Int nrOfPositivePictures = 0;
1210  TComReferencePictureSet* pcRPS = this->getLocalRPS();
1211
1212  // loop through all pictures in the Reference Picture Set
1213  for(i=0;i<pReferencePictureSet->getNumberOfPictures();i++)
1214  {
1215    j = 0;
1216    // loop through all pictures in the reference picture buffer
1217    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1218    while ( iterPic != rcListPic.end())
1219    {
1220      j++;
1221      rpcPic = *(iterPic++);
1222
1223      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1224      {
1225        // This picture exists as a reference picture
1226        // and should be added to the explicit Reference Picture Set
1227        pcRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
1228        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i));
1229        if(pcRPS->getDeltaPOC(k) < 0)
1230        {
1231          nrOfNegativePictures++;
1232        }
1233        else
1234        {
1235          nrOfPositivePictures++;
1236        }
1237        k++;
1238      }
1239    }
1240  }
1241  pcRPS->setNumberOfNegativePictures(nrOfNegativePictures);
1242  pcRPS->setNumberOfPositivePictures(nrOfPositivePictures);
1243  pcRPS->setNumberOfPictures(nrOfNegativePictures+nrOfPositivePictures);
1244  // This is a simplistic inter rps example. A smarter encoder will look for a better reference RPS to do the
1245  // inter RPS prediction with.  Here we just use the reference used by pReferencePictureSet.
1246  // If pReferencePictureSet is not inter_RPS_predicted, then inter_RPS_prediction is for the current RPS also disabled.
1247  if (!pReferencePictureSet->getInterRPSPrediction())
1248  {
1249    pcRPS->setInterRPSPrediction(false);
1250    pcRPS->setNumRefIdc(0);
1251  }
1252  else
1253  {
1254    Int rIdx =  this->getRPSidx() - pReferencePictureSet->getDeltaRIdxMinus1() - 1;
1255    Int deltaRPS = pReferencePictureSet->getDeltaRPS();
1256    TComReferencePictureSet* pcRefRPS = this->getSPS()->getRPSList()->getReferencePictureSet(rIdx);
1257    Int iRefPics = pcRefRPS->getNumberOfPictures();
1258    Int iNewIdc=0;
1259    for(i=0; i<= iRefPics; i++) 
1260    {
1261      Int deltaPOC = ((i != iRefPics)? pcRefRPS->getDeltaPOC(i) : 0);  // check if the reference abs POC is >= 0
1262      Int iRefIdc = 0;
1263      for (j=0; j < pcRPS->getNumberOfPictures(); j++) // loop through the  pictures in the new RPS
1264      {
1265        if ( (deltaPOC + deltaRPS) == pcRPS->getDeltaPOC(j))
1266        {
1267          if (pcRPS->getUsed(j))
1268          {
1269            iRefIdc = 1;
1270          }
1271          else
1272          {
1273            iRefIdc = 2;
1274          }
1275        }
1276      }
1277      pcRPS->setRefIdc(i, iRefIdc);
1278      iNewIdc++;
1279    }
1280    pcRPS->setInterRPSPrediction(true);
1281    pcRPS->setNumRefIdc(iNewIdc);
1282    pcRPS->setDeltaRPS(deltaRPS); 
1283    pcRPS->setDeltaRIdxMinus1(pReferencePictureSet->getDeltaRIdxMinus1() + this->getSPS()->getRPSList()->getNumberOfReferencePictureSets() - this->getRPSidx());
1284  }
1285
1286  this->setRPS(pcRPS);
1287  this->setRPSidx(-1);
1288}
1289
1290/** get AC and DC values for weighted pred
1291 * \param *wp
1292 * \returns Void
1293 */
1294Void  TComSlice::getWpAcDcParam(wpACDCParam *&wp)
1295{
1296  wp = m_weightACDCParam;
1297}
1298
1299/** init AC and DC values for weighted pred
1300 * \returns Void
1301 */
1302Void  TComSlice::initWpAcDcParam()
1303{
1304  for(Int iComp = 0; iComp < 3; iComp++ )
1305  {
1306    m_weightACDCParam[iComp].iAC = 0;
1307    m_weightACDCParam[iComp].iDC = 0;
1308  }
1309}
1310
1311/** get WP tables for weighted pred
1312 * \param RefPicList
1313 * \param iRefIdx
1314 * \param *&wpScalingParam
1315 * \returns Void
1316 */
1317Void  TComSlice::getWpScaling( RefPicList e, Int iRefIdx, wpScalingParam *&wp )
1318{
1319  wp = m_weightPredTable[e][iRefIdx];
1320}
1321
1322/** reset Default WP tables settings : no weight.
1323 * \param wpScalingParam
1324 * \returns Void
1325 */
1326Void  TComSlice::resetWpScaling()
1327{
1328  for ( Int e=0 ; e<2 ; e++ )
1329  {
1330    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1331    {
1332      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1333      {
1334        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1335        pwp->bPresentFlag      = false;
1336        pwp->uiLog2WeightDenom = 0;
1337        pwp->uiLog2WeightDenom = 0;
1338        pwp->iWeight           = 1;
1339        pwp->iOffset           = 0;
1340      }
1341    }
1342  }
1343}
1344
1345/** init WP table
1346 * \returns Void
1347 */
1348Void  TComSlice::initWpScaling()
1349{
1350  for ( Int e=0 ; e<2 ; e++ )
1351  {
1352    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1353    {
1354      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1355      {
1356        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1357        if ( !pwp->bPresentFlag ) 
1358        {
1359          // Inferring values not present :
1360          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
1361          pwp->iOffset = 0;
1362        }
1363
1364        pwp->w      = pwp->iWeight;
1365        Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
1366        pwp->o      = pwp->iOffset << (bitDepth-8);
1367        pwp->shift  = pwp->uiLog2WeightDenom;
1368        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
1369      }
1370    }
1371  }
1372}
1373
1374// ------------------------------------------------------------------------------------------------
1375// Video parameter set (VPS)
1376// ------------------------------------------------------------------------------------------------
1377TComVPS::TComVPS()
1378: m_VPSId                     (  0)
1379, m_uiMaxTLayers              (  1)
1380, m_uiMaxLayers               (  1)
1381, m_bTemporalIdNestingFlag    (false)
1382, m_numHrdParameters          (  0)
1383#if H_MV
1384, m_maxNuhLayerId             (  0)
1385#else
1386, m_maxNuhReservedZeroLayerId (  0)
1387#endif
1388, m_hrdParameters             (NULL)
1389, m_hrdOpSetIdx               (NULL)
1390, m_cprmsPresentFlag          (NULL)
1391{
1392  for( Int i = 0; i < MAX_TLAYER; i++)
1393  {
1394    m_numReorderPics[i] = 0;
1395#if L0323_DPB
1396    m_uiMaxDecPicBuffering[i] = 1; 
1397#else
1398    m_uiMaxDecPicBuffering[i] = 0; 
1399#endif
1400    m_uiMaxLatencyIncrease[i] = 0;
1401  }
1402#if H_MV
1403  m_avcBaseLayerFlag = false;
1404  m_splittingFlag    = false;
1405 
1406  for( Int i = 0; i < MAX_NUM_SCALABILITY_TYPES; i++ )
1407  {
1408    m_scalabilityMask[i] = false;
1409    m_dimensionIdLen [i]  = -1; 
1410  }
1411
1412  m_vpsNuhLayerIdPresentFlag = false;
1413  m_numOutputLayerSets       = 0;
1414
1415  for( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++ )
1416  {
1417    m_vpsProfilePresentFlag   [i] = false;
1418    m_profileLayerSetRefMinus1[i] = 0;
1419    m_outputLayerSetIdx       [i] = 0;
1420    for( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++ )
1421    {
1422      m_outputLayerFlag[i][j] = false;
1423    }
1424  }
1425
1426  for( Int i = 0; i < MAX_NUM_LAYER_IDS; i++ )
1427  {
1428    m_layerIdInVps[i] =  (i == 0 ) ? 0 : -1;         
1429  }
1430
1431  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1432  {
1433    m_layerIdInNuh      [i] = ( i == 0 ) ? 0 : -1; 
1434    m_numDirectRefLayers[i] = 0; 
1435
1436    for( Int j = 0; j < MAX_NUM_LAYERS; j++ )
1437    {
1438      m_directDependencyFlag[i][j] = false;
1439      m_refLayerId[i][j]           = -1; 
1440    }
1441
1442    for( Int j = 0; j < MAX_NUM_SCALABILITY_TYPES; j++ )
1443    {
1444      m_dimensionId[i][j] = 0;
1445    }
1446
1447  }
1448#endif
1449}
1450
1451TComVPS::~TComVPS()
1452{
1453if( m_hrdParameters    != NULL )     delete[] m_hrdParameters;
1454  if( m_hrdOpSetIdx      != NULL )     delete[] m_hrdOpSetIdx;
1455  if( m_cprmsPresentFlag != NULL )     delete[] m_cprmsPresentFlag;
1456}
1457
1458#if H_MV
1459
1460Bool TComVPS::checkVPSExtensionSyntax()
1461{
1462  // check splitting flag constraint
1463  if ( getSplittingFlag() )
1464  {
1465    // Derive dimBitOffset[j]
1466    Int dimBitOffset[MAX_NUM_SCALABILITY_TYPES+1];
1467    Int numScalabilityTypes = getNumScalabilityTypes();
1468    dimBitOffset[0] = 0;
1469
1470    for (Int type = 1; type <= numScalabilityTypes; type++ )
1471    {
1472      dimBitOffset[ type ] = 0;
1473      for (Int dimIdx = 0; dimIdx <= type - 1; dimIdx++)
1474        dimBitOffset[ type ] += ( getDimensionIdLen( dimIdx ) );
1475    }
1476
1477    for (Int type = 0; type < getNumScalabilityTypes(); type++ )
1478    {
1479      for( Int layer = 1; layer < getMaxLayers(); layer++ )
1480      {
1481        assert( getDimensionId( layer, type ) == ( ( getLayerIdInNuh( layer ) & ( (1 << dimBitOffset[ type + 1 ] ) - 1) ) >> dimBitOffset[ type ] ) );
1482      }; 
1483  };
1484  }
1485
1486  for( Int layer = 1; layer < getMaxLayers(); layer++ )
1487  {
1488    // check layer_id_in_nuh constraint
1489    assert( getLayerIdInNuh( layer ) > getLayerIdInNuh( layer -1 ) );
1490  }
1491  return true; 
1492}
1493
1494Int TComVPS::getNumScalabilityTypes()
1495{
1496  return scalTypeToScalIdx( ScalabilityType(MAX_NUM_SCALABILITY_TYPES) );
1497}
1498
1499Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType )
1500{
1501  assert( scalType >= 0 && scalType <= MAX_NUM_SCALABILITY_TYPES ); 
1502  assert( scalType == MAX_NUM_SCALABILITY_TYPES || getScalabilityMask( scalType ) );
1503
1504  Int scalIdx = 0; 
1505  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
1506  {
1507    scalIdx += ( getScalabilityMask( curScalType ) ? 1 : 0 );
1508  }
1509
1510  return scalIdx; 
1511}
1512
1513
1514
1515Void TComVPS::setScalabilityMask( UInt val )
1516{
1517  for ( Int scalType = 0; scalType < MAX_NUM_SCALABILITY_TYPES; scalType++ ) 
1518    setScalabilityMask( scalType, ( val & (1 << scalType ) ) == 1 );
1519}
1520
1521Void TComVPS::calcIvRefLayers()
1522{
1523  for( Int i = 1; i <= getMaxLayers(); i++ )
1524  { 
1525    m_numDirectRefLayers[ i ] = 0; 
1526    for( Int j = 0 ; j < i; j++ )
1527      if( m_directDependencyFlag[ i ][ j ])
1528        m_refLayerId[ i ][ m_numDirectRefLayers[ i ]++ ] = m_layerIdInNuh[ j ];   
1529  }
1530}
1531
1532Int TComVPS::getRefLayerId( Int layerIdInVps, Int idx )
1533{
1534  assert( idx >= 0 && idx < m_numDirectRefLayers[layerIdInVps] );     
1535  Int layerIdInNuh = m_refLayerId[ layerIdInVps ][ idx ];   
1536  assert ( layerIdInNuh >= 0 ); 
1537  return layerIdInNuh;
1538}
1539
1540Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType )
1541{
1542  return ( ( layerIdInVps != 0 )&& getScalabilityMask( scalType ) ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
1543}
1544#endif
1545// ------------------------------------------------------------------------------------------------
1546// Sequence parameter set (SPS)
1547// ------------------------------------------------------------------------------------------------
1548
1549TComSPS::TComSPS()
1550: m_SPSId                     (  0)
1551, m_VPSId                     (  0)
1552, m_chromaFormatIdc           (CHROMA_420)
1553, m_uiMaxTLayers              (  1)
1554// Structure
1555, m_picWidthInLumaSamples     (352)
1556, m_picHeightInLumaSamples    (288)
1557, m_log2MinCodingBlockSize    (  0)
1558, m_log2DiffMaxMinCodingBlockSize (0)
1559, m_uiMaxCUWidth              ( 32)
1560, m_uiMaxCUHeight             ( 32)
1561, m_uiMaxCUDepth              (  3)
1562, m_bLongTermRefsPresent      (false)
1563, m_uiQuadtreeTULog2MaxSize   (  0)
1564, m_uiQuadtreeTULog2MinSize   (  0)
1565, m_uiQuadtreeTUMaxDepthInter (  0)
1566, m_uiQuadtreeTUMaxDepthIntra (  0)
1567// Tool list
1568, m_usePCM                   (false)
1569, m_pcmLog2MaxSize            (  5)
1570, m_uiPCMLog2MinSize          (  7)
1571#if !L0034_COMBINED_LIST_CLEANUP
1572, m_bUseLComb                 (false)
1573#endif
1574, m_bitDepthY                 (  8)
1575, m_bitDepthC                 (  8)
1576, m_qpBDOffsetY               (  0)
1577, m_qpBDOffsetC               (  0)
1578, m_useLossless               (false)
1579, m_uiPCMBitDepthLuma         (  8)
1580, m_uiPCMBitDepthChroma       (  8)
1581, m_bPCMFilterDisableFlag     (false)
1582, m_uiBitsForPOC              (  8)
1583, m_numLongTermRefPicSPS    (  0) 
1584, m_uiMaxTrSize               ( 32)
1585, m_bUseSAO                   (false) 
1586, m_bTemporalIdNestingFlag    (false)
1587, m_scalingListEnabledFlag    (false)
1588, m_useStrongIntraSmoothing   (false)
1589, m_vuiParametersPresentFlag  (false)
1590, m_vuiParameters             ()
1591#if H_MV
1592, m_interViewMvVertConstraintFlag (false)
1593#endif
1594{
1595  for ( Int i = 0; i < MAX_TLAYER; i++ )
1596  {
1597    m_uiMaxLatencyIncrease[i] = 0;
1598#if L0323_DPB
1599    m_uiMaxDecPicBuffering[i] = 1;
1600#else
1601    m_uiMaxDecPicBuffering[i] = 0;
1602#endif
1603    m_numReorderPics[i]       = 0;
1604  }
1605  m_scalingList = new TComScalingList;
1606  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
1607  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
1608}
1609
1610TComSPS::~TComSPS()
1611{
1612  delete m_scalingList;
1613  m_RPSList.destroy();
1614}
1615
1616Void  TComSPS::createRPSList( Int numRPS )
1617{ 
1618  m_RPSList.destroy();
1619  m_RPSList.create(numRPS);
1620}
1621
1622Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
1623{
1624  if( !getVuiParametersPresentFlag() )
1625  {
1626    return;
1627  }
1628
1629  TComVUI *vui = getVuiParameters();
1630  TComHRD *hrd = vui->getHrdParameters();
1631
1632#if L0043_TIMING_INFO
1633  TimingInfo *timingInfo = vui->getTimingInfo();
1634  timingInfo->setTimingInfoPresentFlag( true );
1635  switch( frameRate )
1636  {
1637  case 24:
1638    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
1639    break;
1640  case 25:
1641    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
1642    break;
1643  case 30:
1644    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
1645    break;
1646  case 50:
1647    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
1648    break;
1649  case 60:
1650    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
1651    break;
1652  default:
1653    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
1654    break;
1655  }
1656#else
1657  hrd->setTimingInfoPresentFlag( true );
1658  switch( frameRate )
1659  {
1660  case 24:
1661    hrd->setNumUnitsInTick( 1125000 );    hrd->setTimeScale    ( 27000000 );
1662    break;
1663  case 25:
1664    hrd->setNumUnitsInTick( 1080000 );    hrd->setTimeScale    ( 27000000 );
1665    break;
1666  case 30:
1667    hrd->setNumUnitsInTick( 900900 );     hrd->setTimeScale    ( 27000000 );
1668    break;
1669  case 50:
1670    hrd->setNumUnitsInTick( 540000 );     hrd->setTimeScale    ( 27000000 );
1671    break;
1672  case 60:
1673    hrd->setNumUnitsInTick( 450450 );     hrd->setTimeScale    ( 27000000 );
1674    break;
1675  default:
1676    hrd->setNumUnitsInTick( 1001 );       hrd->setTimeScale    ( 60000 );
1677    break;
1678  }
1679#endif
1680
1681  Bool rateCnt = ( bitRate > 0 );
1682  hrd->setNalHrdParametersPresentFlag( rateCnt );
1683  hrd->setVclHrdParametersPresentFlag( rateCnt );
1684
1685  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
1686
1687  if( hrd->getSubPicCpbParamsPresentFlag() )
1688  {
1689    hrd->setTickDivisorMinus2( 100 - 2 );                          //
1690    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
1691    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
1692#if L0044_DU_DPB_OUTPUT_DELAY_HRD
1693    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
1694#endif
1695  }
1696  else
1697  {
1698    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
1699  }
1700
1701  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
1702  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1703  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1704 
1705  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
1706  if( randomAccess )
1707  {
1708    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
1709    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
1710  }
1711  else
1712  {
1713    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
1714    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
1715  }
1716
1717/*
1718   Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
1719*/
1720  Int i, j;
1721  UInt birateValue, cpbSizeValue;
1722  UInt ducpbSizeValue;
1723#if L0363_DU_BIT_RATE
1724  UInt duBitRateValue = 0;
1725#endif
1726
1727  for( i = 0; i < MAX_TLAYER; i ++ )
1728  {
1729    hrd->setFixedPicRateFlag( i, 1 );
1730    hrd->setPicDurationInTcMinus1( i, 0 );
1731    hrd->setLowDelayHrdFlag( i, 0 );
1732    hrd->setCpbCntMinus1( i, 0 );
1733
1734    birateValue  = bitRate;
1735    cpbSizeValue = bitRate;                                     // 1 second
1736    ducpbSizeValue = bitRate/numDU;
1737#if L0363_DU_BIT_RATE
1738    duBitRateValue = bitRate;
1739#endif
1740    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
1741    {
1742      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
1743      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
1744      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
1745      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
1746
1747      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
1748      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
1749      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
1750#if L0363_DU_BIT_RATE
1751      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
1752#endif
1753      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
1754    }
1755  }
1756}
1757const Int TComSPS::m_winUnitX[]={1,2,2,1};
1758const Int TComSPS::m_winUnitY[]={1,2,1,1};
1759
1760TComPPS::TComPPS()
1761: m_PPSId                       (0)
1762, m_SPSId                       (0)
1763, m_picInitQPMinus26            (0)
1764, m_useDQP                      (false)
1765, m_bConstrainedIntraPred       (false)
1766, m_bSliceChromaQpFlag          (false)
1767, m_pcSPS                       (NULL)
1768, m_uiMaxCuDQPDepth             (0)
1769, m_uiMinCuDQPSize              (0)
1770, m_chromaCbQpOffset            (0)
1771, m_chromaCrQpOffset            (0)
1772, m_numRefIdxL0DefaultActive    (1)
1773, m_numRefIdxL1DefaultActive    (1)
1774, m_TransquantBypassEnableFlag  (false)
1775, m_useTransformSkip             (false)
1776, m_dependentSliceSegmentsEnabledFlag    (false)
1777, m_tilesEnabledFlag               (false)
1778, m_entropyCodingSyncEnabledFlag   (false)
1779, m_loopFilterAcrossTilesEnabledFlag  (true)
1780, m_uniformSpacingFlag           (0)
1781, m_iNumColumnsMinus1            (0)
1782, m_puiColumnWidth               (NULL)
1783, m_iNumRowsMinus1               (0)
1784, m_puiRowHeight                 (NULL)
1785, m_iNumSubstreams             (1)
1786, m_signHideFlag(0)
1787, m_cabacInitPresentFlag        (false)
1788, m_encCABACTableIdx            (I_SLICE)
1789, m_sliceHeaderExtensionPresentFlag    (false)
1790, m_loopFilterAcrossSlicesEnabledFlag (false)
1791, m_listsModificationPresentFlag(  0)
1792, m_numExtraSliceHeaderBits(0)
1793{
1794  m_scalingList = new TComScalingList;
1795}
1796
1797TComPPS::~TComPPS()
1798{
1799  if( m_iNumColumnsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1800  {
1801    if (m_puiColumnWidth) delete [] m_puiColumnWidth; 
1802    m_puiColumnWidth = NULL;
1803  }
1804  if( m_iNumRowsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1805  {
1806    if (m_puiRowHeight) delete [] m_puiRowHeight;
1807    m_puiRowHeight = NULL;
1808  }
1809  delete m_scalingList;
1810}
1811
1812TComReferencePictureSet::TComReferencePictureSet()
1813: m_numberOfPictures (0)
1814, m_numberOfNegativePictures (0)
1815, m_numberOfPositivePictures (0)
1816, m_numberOfLongtermPictures (0)
1817, m_interRPSPrediction (0) 
1818, m_deltaRIdxMinus1 (0)   
1819, m_deltaRPS (0) 
1820, m_numRefIdc (0) 
1821{
1822  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
1823  ::memset( m_POC, 0, sizeof(m_POC) );
1824  ::memset( m_used, 0, sizeof(m_used) );
1825  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
1826}
1827
1828TComReferencePictureSet::~TComReferencePictureSet()
1829{
1830}
1831
1832Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
1833{
1834  m_used[bufferNum] = used;
1835}
1836
1837Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
1838{
1839  m_deltaPOC[bufferNum] = deltaPOC;
1840}
1841
1842Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
1843{
1844  m_numberOfPictures = numberOfPictures;
1845}
1846
1847Int TComReferencePictureSet::getUsed(Int bufferNum)
1848{
1849  return m_used[bufferNum];
1850}
1851
1852Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
1853{
1854  return m_deltaPOC[bufferNum];
1855}
1856
1857Int TComReferencePictureSet::getNumberOfPictures()
1858{
1859  return m_numberOfPictures;
1860}
1861
1862Int TComReferencePictureSet::getPOC(Int bufferNum)
1863{
1864  return m_POC[bufferNum];
1865}
1866
1867Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
1868{
1869  m_POC[bufferNum] = POC;
1870}
1871
1872Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
1873{
1874  return m_bCheckLTMSB[bufferNum];
1875}
1876
1877Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
1878{
1879  m_bCheckLTMSB[bufferNum] = b;
1880}
1881
1882/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
1883 * \param uiBufferNum
1884 * \param iRefIdc
1885 * \returns Void
1886 */
1887Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
1888{
1889  m_refIdc[bufferNum] = refIdc;
1890}
1891
1892/** get the reference idc value at uiBufferNum
1893 * \param uiBufferNum
1894 * \returns Int
1895 */
1896Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
1897{
1898  return m_refIdc[bufferNum];
1899}
1900
1901/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
1902 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
1903 *  +ve values are in increasing order.
1904 * \returns Void
1905 */
1906Void TComReferencePictureSet::sortDeltaPOC()
1907{
1908  // sort in increasing order (smallest first)
1909  for(Int j=1; j < getNumberOfPictures(); j++)
1910  { 
1911    Int deltaPOC = getDeltaPOC(j);
1912    Bool used = getUsed(j);
1913    for (Int k=j-1; k >= 0; k--)
1914    {
1915      Int temp = getDeltaPOC(k);
1916      if (deltaPOC < temp)
1917      {
1918        setDeltaPOC(k+1, temp);
1919        setUsed(k+1, getUsed(k));
1920        setDeltaPOC(k, deltaPOC);
1921        setUsed(k, used);
1922      }
1923    }
1924  }
1925  // flip the negative values to largest first
1926  Int numNegPics = getNumberOfNegativePictures();
1927  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
1928  { 
1929    Int deltaPOC = getDeltaPOC(j);
1930    Bool used = getUsed(j);
1931    setDeltaPOC(j, getDeltaPOC(k));
1932    setUsed(j, getUsed(k));
1933    setDeltaPOC(k, deltaPOC);
1934    setUsed(k, used);
1935  }
1936}
1937
1938/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
1939 *  A "*" is added to the deltaPOC value if it is Used bu current.
1940 * \returns Void
1941 */
1942Void TComReferencePictureSet::printDeltaPOC()
1943{
1944  printf("DeltaPOC = { ");
1945  for(Int j=0; j < getNumberOfPictures(); j++)
1946  {
1947    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
1948  } 
1949  if (getInterRPSPrediction()) 
1950  {
1951    printf("}, RefIdc = { ");
1952    for(Int j=0; j < getNumRefIdc(); j++)
1953    {
1954      printf("%d ", getRefIdc(j));
1955    } 
1956  }
1957  printf("}\n");
1958}
1959
1960TComRPSList::TComRPSList()
1961:m_referencePictureSets (NULL)
1962{
1963}
1964
1965TComRPSList::~TComRPSList()
1966{
1967}
1968
1969Void TComRPSList::create( Int numberOfReferencePictureSets)
1970{
1971  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
1972  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
1973}
1974
1975Void TComRPSList::destroy()
1976{
1977  if (m_referencePictureSets)
1978  {
1979    delete [] m_referencePictureSets;
1980  }
1981  m_numberOfReferencePictureSets = 0;
1982  m_referencePictureSets = NULL;
1983}
1984
1985
1986
1987TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
1988{
1989  return &m_referencePictureSets[referencePictureSetNum];
1990}
1991
1992Int TComRPSList::getNumberOfReferencePictureSets()
1993{
1994  return m_numberOfReferencePictureSets;
1995}
1996
1997Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
1998{
1999  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2000}
2001
2002TComRefPicListModification::TComRefPicListModification()
2003: m_bRefPicListModificationFlagL0 (false)
2004, m_bRefPicListModificationFlagL1 (false)
2005{
2006  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
2007  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
2008}
2009
2010TComRefPicListModification::~TComRefPicListModification()
2011{
2012}
2013
2014TComScalingList::TComScalingList()
2015{
2016  m_useTransformSkip = false;
2017  init();
2018}
2019TComScalingList::~TComScalingList()
2020{
2021  destroy();
2022}
2023
2024/** set default quantization matrix to array
2025*/
2026Void TComSlice::setDefaultScalingList()
2027{
2028  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2029  {
2030    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2031    {
2032      getScalingList()->processDefaultMarix(sizeId, listId);
2033    }
2034  }
2035}
2036/** check if use default quantization matrix
2037 * \returns true if use default quantization matrix in all size
2038*/
2039Bool TComSlice::checkDefaultScalingList()
2040{
2041  UInt defaultCounter=0;
2042
2043  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2044  {
2045    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2046    {
2047      if( !memcmp(getScalingList()->getScalingListAddress(sizeId,listId), getScalingList()->getScalingListDefaultAddress(sizeId, listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])) // check value of matrix
2048     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
2049      {
2050        defaultCounter++;
2051      }
2052    }
2053  }
2054  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
2055}
2056
2057#if H_MV
2058Void TComSlice::createAndApplyIvReferencePictureSet( TComPicLists* ivPicLists, std::vector<TComPic*>& refPicSetInterLayer )
2059{
2060  refPicSetInterLayer.clear(); 
2061
2062  for( Int i = 0; i < getVPS()->getNumDirectRefLayers( getLayerIdInVps() ); i++ ) 
2063  {
2064    Int layerIdRef = getVPS()->getRefLayerId( getLayerIdInVps(), i ); 
2065    TComPic* picRef = ivPicLists->getPic( layerIdRef, getPOC() ) ; 
2066    assert ( picRef != 0 ); 
2067
2068    picRef->getPicYuvRec()->extendPicBorder(); 
2069    picRef->setIsLongTerm( true );       
2070    picRef->getSlice(0)->setReferenced( true );       
2071
2072    refPicSetInterLayer.push_back( picRef ); 
2073  }
2074}
2075
2076Void TComSlice::markIvRefPicsAsShortTerm( std::vector<TComPic*> refPicSetInterLayer )
2077{
2078  // Mark as shortterm
2079  for ( Int i = 0; i < refPicSetInterLayer.size(); i++ ) 
2080  {
2081    refPicSetInterLayer[i]->setIsLongTerm( false ); 
2082  }
2083}
2084
2085Void TComSlice::markIvRefPicsAsUnused( TComPicLists* ivPicLists, std::vector<Int> targetDecLayerIdSet, TComVPS* vps, Int curLayerId, Int curPoc )
2086{
2087  // Fill targetDecLayerIdSet with all layers if empty.
2088  if (targetDecLayerIdSet.size() == 0 )   
2089  {
2090    for ( Int layerIdInVps = 0; layerIdInVps < vps->getMaxLayers(); layerIdInVps++ )
2091    {
2092      targetDecLayerIdSet.push_back( vps->getLayerIdInNuh( layerIdInVps ) ); 
2093    }
2094  }     
2095
2096  Int numTargetDecLayers = (Int) targetDecLayerIdSet.size(); 
2097  Int latestDecIdx; 
2098  for ( latestDecIdx = 0; latestDecIdx < numTargetDecLayers; latestDecIdx++)
2099  {
2100    if ( targetDecLayerIdSet[ latestDecIdx ] == curLayerId )
2101      break; 
2102  }       
2103
2104  for( Int i = 0; i <= latestDecIdx; i++ ) 
2105  {
2106    if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ i ] ) )
2107    {
2108      TComPic* pcPic = ivPicLists->getPic( targetDecLayerIdSet[ i ], curPoc ); 
2109      if( pcPic->getSlice(0)->isReferenced() && pcPic->getSlice(0)->getTemporalLayerNonReferenceFlag() ) 
2110      { 
2111        Bool remainingInterLayerReferencesFlag = false; 
2112        for( Int j = latestDecIdx + 1; j < numTargetDecLayers; j++ )
2113        { 
2114          TComVPS* vpsSlice = pcPic->getSlice(0)->getVPS(); 
2115          if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ j ] ) )
2116          {
2117            Int targetDecLayerIdinVPS = vpsSlice->getLayerIdInVps( targetDecLayerIdSet[ j ] ); 
2118            for( Int k = 0; k < vpsSlice->getNumDirectRefLayers( targetDecLayerIdinVPS ); k++ )
2119              if ( targetDecLayerIdSet[ i ] == vpsSlice->getRefLayerId( targetDecLayerIdinVPS,  k  ) )
2120                remainingInterLayerReferencesFlag = true;
2121          }
2122        }
2123        if( !remainingInterLayerReferencesFlag )
2124          pcPic->getSlice(0)->setReferenced( false );                   
2125      }
2126    }
2127  }
2128}
2129
2130Void TComSlice::xPrintRefPicList()
2131{ 
2132  for ( Int li = 0; li < 2; li++)
2133  {   
2134    std::cout << std::endl << "RefPicListL" <<  li << ":" << std::endl; 
2135    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[li]-1); rIdx ++)
2136    {     
2137      if (rIdx == 0 && li == 0) m_apcRefPicList[li][rIdx]->print( true );
2138       
2139      m_apcRefPicList[li][rIdx]->print( false );
2140    }
2141  }
2142}
2143#endif
2144/** get scaling matrix from RefMatrixID
2145 * \param sizeId size index
2146 * \param Index of input matrix
2147 * \param Index of reference matrix
2148 */
2149Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
2150{
2151  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2152}
2153
2154/** parse syntax infomation
2155 *  \param pchFile syntax infomation
2156 *  \returns false if successful
2157 */
2158Bool TComScalingList::xParseScalingList(Char* pchFile)
2159{
2160  FILE *fp;
2161  Char line[1024];
2162  UInt sizeIdc,listIdc;
2163  UInt i,size = 0;
2164  Int *src=0,data;
2165  Char *ret;
2166  UInt  retval;
2167
2168  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
2169  {
2170    printf("can't open file %s :: set Default Matrix\n",pchFile);
2171    return true;
2172  }
2173
2174  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
2175  {
2176    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
2177    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
2178    {
2179      src = getScalingListAddress(sizeIdc, listIdc);
2180
2181      fseek(fp,0,0);
2182      do 
2183      {
2184        ret = fgets(line, 1024, fp);
2185        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
2186        {
2187          printf("Error: can't read Matrix :: set Default Matrix\n");
2188          return true;
2189        }
2190      }
2191      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
2192      for (i=0; i<size; i++)
2193      {
2194        retval = fscanf(fp, "%d,", &data);
2195        if (retval!=1)
2196        {
2197          printf("Error: can't read Matrix :: set Default Matrix\n");
2198          return true;
2199        }
2200        src[i] = data;
2201      }
2202      //set DC value for default matrix check
2203      setScalingListDC(sizeIdc,listIdc,src[0]);
2204
2205      if(sizeIdc > SCALING_LIST_8x8)
2206      {
2207        fseek(fp,0,0);
2208        do 
2209        {
2210          ret = fgets(line, 1024, fp);
2211          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
2212          {
2213            printf("Error: can't read DC :: set Default Matrix\n");
2214            return true;
2215          }
2216        }
2217        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
2218        retval = fscanf(fp, "%d,", &data);
2219        if (retval!=1)
2220        {
2221          printf("Error: can't read Matrix :: set Default Matrix\n");
2222          return true;
2223        }
2224        //overwrite DC value when size of matrix is larger than 16x16
2225        setScalingListDC(sizeIdc,listIdc,data);
2226      }
2227    }
2228  }
2229  fclose(fp);
2230  return false;
2231}
2232
2233/** initialization process of quantization matrix array
2234 */
2235Void TComScalingList::init()
2236{
2237  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2238  {
2239    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2240    {
2241      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
2242    }
2243  }
2244  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
2245}
2246
2247/** destroy quantization matrix array
2248 */
2249Void TComScalingList::destroy()
2250{
2251  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2252  {
2253    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2254    {
2255      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
2256    }
2257  }
2258}
2259
2260/** get default address of quantization matrix
2261 * \param sizeId size index
2262 * \param listId list index
2263 * \returns pointer of quantization matrix
2264 */
2265Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
2266{
2267  Int *src = 0;
2268  switch(sizeId)
2269  {
2270    case SCALING_LIST_4x4:
2271      src = g_quantTSDefault4x4;
2272      break;
2273    case SCALING_LIST_8x8:
2274      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2275      break;
2276    case SCALING_LIST_16x16:
2277      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2278      break;
2279    case SCALING_LIST_32x32:
2280      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2281      break;
2282    default:
2283      assert(0);
2284      src = NULL;
2285      break;
2286  }
2287  return src;
2288}
2289
2290/** process of default matrix
2291 * \param sizeId size index
2292 * \param Index of input matrix
2293 */
2294Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId)
2295{
2296  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2297  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
2298}
2299
2300/** check DC value of matrix for default matrix signaling
2301 */
2302Void TComScalingList::checkDcOfMatrix()
2303{
2304  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2305  {
2306    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2307    {
2308      //check default matrix?
2309      if(getScalingListDC(sizeId,listId) == 0)
2310      {
2311        processDefaultMarix(sizeId, listId);
2312      }
2313    }
2314  }
2315}
2316
2317ParameterSetManager::ParameterSetManager()
2318: m_vpsMap(MAX_NUM_VPS)
2319, m_spsMap(MAX_NUM_SPS)
2320, m_ppsMap(MAX_NUM_PPS)
2321, m_activeVPSId(-1)
2322, m_activeSPSId(-1)
2323, m_activePPSId(-1)
2324{
2325}
2326
2327
2328ParameterSetManager::~ParameterSetManager()
2329{
2330}
2331
2332//! activate a SPS from a active parameter sets SEI message
2333//! \returns true, if activation is successful
2334Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
2335{
2336  TComSPS *sps = m_spsMap.getPS(spsId);
2337  if (sps)
2338  {
2339    Int vpsId = sps->getVPSId();
2340    if (m_vpsMap.getPS(vpsId))
2341    {
2342      m_activeVPSId = vpsId;
2343      m_activeSPSId = spsId;
2344      return true;
2345    }
2346    else
2347    {
2348      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
2349    }
2350  }
2351  else
2352  {
2353    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
2354  }
2355  return false;
2356}
2357
2358//! activate a PPS and depending on isIDR parameter also SPS and VPS
2359//! \returns true, if activation is successful
2360Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
2361{
2362  TComPPS *pps = m_ppsMap.getPS(ppsId);
2363  if (pps)
2364  {
2365    Int spsId = pps->getSPSId();
2366#if H_MV
2367    // active parameter sets per layer should be used here
2368#else
2369    if (!isIRAP && (spsId != m_activeSPSId))
2370    {
2371      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
2372      return false;
2373    }
2374#endif
2375    TComSPS *sps = m_spsMap.getPS(spsId);
2376    if (sps)
2377    {
2378      Int vpsId = sps->getVPSId();
2379      if (!isIRAP && (vpsId != m_activeVPSId))
2380      {
2381        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
2382        return false;
2383      }
2384      if (m_vpsMap.getPS(vpsId))
2385      {
2386        m_activePPSId = ppsId;
2387        m_activeVPSId = vpsId;
2388        m_activeSPSId = spsId;
2389        return true;
2390      }
2391      else
2392      {
2393        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
2394      }
2395    }
2396    else
2397    {
2398      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
2399    }
2400  }
2401  else
2402  {
2403    printf("Warning: tried to activate non-existing PPS.");
2404  }
2405  return false;
2406}
2407
2408ProfileTierLevel::ProfileTierLevel()
2409  : m_profileSpace    (0)
2410  , m_tierFlag        (false)
2411  , m_profileIdc      (0)
2412  , m_levelIdc        (0)
2413#if L0046_CONSTRAINT_FLAGS
2414, m_progressiveSourceFlag  (false)
2415, m_interlacedSourceFlag   (false)
2416, m_nonPackedConstraintFlag(false)
2417, m_frameOnlyConstraintFlag(false)
2418#endif
2419{
2420  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
2421}
2422
2423TComPTL::TComPTL()
2424{
2425  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
2426  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
2427}
2428
2429#if H_MV
2430Void TComPTL::copyLevelFrom( TComPTL* source )
2431{
2432  getGeneralPTL()->setLevelIdc( source->getGeneralPTL()->getLevelIdc() );
2433  for( Int subLayer = 0; subLayer < 6; subLayer++ )
2434  {
2435    setSubLayerLevelPresentFlag( subLayer, source->getSubLayerLevelPresentFlag( subLayer ) );
2436    getSubLayerPTL( subLayer )->setLevelIdc( source->getSubLayerPTL( subLayer )->getLevelIdc() );
2437  }
2438}
2439#endif
2440#if SIGNAL_BITRATE_PICRATE_IN_VPS
2441TComBitRatePicRateInfo::TComBitRatePicRateInfo()
2442{
2443  ::memset(m_bitRateInfoPresentFlag, 0, sizeof(m_bitRateInfoPresentFlag));
2444  ::memset(m_picRateInfoPresentFlag, 0, sizeof(m_picRateInfoPresentFlag));
2445  ::memset(m_avgBitRate,             0, sizeof(m_avgBitRate));
2446  ::memset(m_maxBitRate,             0, sizeof(m_maxBitRate));
2447  ::memset(m_constantPicRateIdc,     0, sizeof(m_constantPicRateIdc));
2448  ::memset(m_avgPicRate,             0, sizeof(m_avgPicRate));
2449}
2450#endif
2451//! \}
Note: See TracBrowser for help on using the repository browser.