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

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

Fixed assertion on NumPocTotalCurr.

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