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

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

Update to HM-10.1.

  • 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, Bool checkNumPocTotalCurr,std::vector<TComPic*>& refPicSetInterLayer )
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// ------------------------------------------------------------------------------------------------
1547// Sequence parameter set (SPS)
1548// ------------------------------------------------------------------------------------------------
1549
1550TComSPS::TComSPS()
1551: m_SPSId                     (  0)
1552, m_VPSId                     (  0)
1553, m_chromaFormatIdc           (CHROMA_420)
1554, m_uiMaxTLayers              (  1)
1555// Structure
1556, m_picWidthInLumaSamples     (352)
1557, m_picHeightInLumaSamples    (288)
1558, m_log2MinCodingBlockSize    (  0)
1559, m_log2DiffMaxMinCodingBlockSize (0)
1560, m_uiMaxCUWidth              ( 32)
1561, m_uiMaxCUHeight             ( 32)
1562, m_uiMaxCUDepth              (  3)
1563, m_bLongTermRefsPresent      (false)
1564, m_uiQuadtreeTULog2MaxSize   (  0)
1565, m_uiQuadtreeTULog2MinSize   (  0)
1566, m_uiQuadtreeTUMaxDepthInter (  0)
1567, m_uiQuadtreeTUMaxDepthIntra (  0)
1568// Tool list
1569, m_usePCM                   (false)
1570, m_pcmLog2MaxSize            (  5)
1571, m_uiPCMLog2MinSize          (  7)
1572#if !L0034_COMBINED_LIST_CLEANUP
1573, m_bUseLComb                 (false)
1574#endif
1575, m_bitDepthY                 (  8)
1576, m_bitDepthC                 (  8)
1577, m_qpBDOffsetY               (  0)
1578, m_qpBDOffsetC               (  0)
1579, m_useLossless               (false)
1580, m_uiPCMBitDepthLuma         (  8)
1581, m_uiPCMBitDepthChroma       (  8)
1582, m_bPCMFilterDisableFlag     (false)
1583, m_uiBitsForPOC              (  8)
1584, m_numLongTermRefPicSPS    (  0) 
1585, m_uiMaxTrSize               ( 32)
1586, m_bUseSAO                   (false) 
1587, m_bTemporalIdNestingFlag    (false)
1588, m_scalingListEnabledFlag    (false)
1589, m_useStrongIntraSmoothing   (false)
1590, m_vuiParametersPresentFlag  (false)
1591, m_vuiParameters             ()
1592#if H_MV
1593, m_interViewMvVertConstraintFlag (false)
1594#endif
1595{
1596  for ( Int i = 0; i < MAX_TLAYER; i++ )
1597  {
1598    m_uiMaxLatencyIncrease[i] = 0;
1599#if L0323_DPB
1600    m_uiMaxDecPicBuffering[i] = 1;
1601#else
1602    m_uiMaxDecPicBuffering[i] = 0;
1603#endif
1604    m_numReorderPics[i]       = 0;
1605  }
1606  m_scalingList = new TComScalingList;
1607  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
1608  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
1609}
1610
1611TComSPS::~TComSPS()
1612{
1613  delete m_scalingList;
1614  m_RPSList.destroy();
1615}
1616
1617Void  TComSPS::createRPSList( Int numRPS )
1618{ 
1619  m_RPSList.destroy();
1620  m_RPSList.create(numRPS);
1621}
1622
1623Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
1624{
1625  if( !getVuiParametersPresentFlag() )
1626  {
1627    return;
1628  }
1629
1630  TComVUI *vui = getVuiParameters();
1631  TComHRD *hrd = vui->getHrdParameters();
1632
1633#if L0043_TIMING_INFO
1634  TimingInfo *timingInfo = vui->getTimingInfo();
1635  timingInfo->setTimingInfoPresentFlag( true );
1636  switch( frameRate )
1637  {
1638  case 24:
1639    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
1640    break;
1641  case 25:
1642    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
1643    break;
1644  case 30:
1645    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
1646    break;
1647  case 50:
1648    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
1649    break;
1650  case 60:
1651    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
1652    break;
1653  default:
1654    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
1655    break;
1656  }
1657#else
1658  hrd->setTimingInfoPresentFlag( true );
1659  switch( frameRate )
1660  {
1661  case 24:
1662    hrd->setNumUnitsInTick( 1125000 );    hrd->setTimeScale    ( 27000000 );
1663    break;
1664  case 25:
1665    hrd->setNumUnitsInTick( 1080000 );    hrd->setTimeScale    ( 27000000 );
1666    break;
1667  case 30:
1668    hrd->setNumUnitsInTick( 900900 );     hrd->setTimeScale    ( 27000000 );
1669    break;
1670  case 50:
1671    hrd->setNumUnitsInTick( 540000 );     hrd->setTimeScale    ( 27000000 );
1672    break;
1673  case 60:
1674    hrd->setNumUnitsInTick( 450450 );     hrd->setTimeScale    ( 27000000 );
1675    break;
1676  default:
1677    hrd->setNumUnitsInTick( 1001 );       hrd->setTimeScale    ( 60000 );
1678    break;
1679  }
1680#endif
1681
1682  Bool rateCnt = ( bitRate > 0 );
1683  hrd->setNalHrdParametersPresentFlag( rateCnt );
1684  hrd->setVclHrdParametersPresentFlag( rateCnt );
1685
1686  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
1687
1688  if( hrd->getSubPicCpbParamsPresentFlag() )
1689  {
1690    hrd->setTickDivisorMinus2( 100 - 2 );                          //
1691    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
1692    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
1693#if L0044_DU_DPB_OUTPUT_DELAY_HRD
1694    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
1695#endif
1696  }
1697  else
1698  {
1699    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
1700  }
1701
1702  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
1703  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1704  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1705 
1706  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
1707  if( randomAccess )
1708  {
1709    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
1710    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
1711  }
1712  else
1713  {
1714    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
1715    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
1716  }
1717
1718/*
1719   Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
1720*/
1721  Int i, j;
1722  UInt birateValue, cpbSizeValue;
1723  UInt ducpbSizeValue;
1724#if L0363_DU_BIT_RATE
1725  UInt duBitRateValue = 0;
1726#endif
1727
1728  for( i = 0; i < MAX_TLAYER; i ++ )
1729  {
1730    hrd->setFixedPicRateFlag( i, 1 );
1731    hrd->setPicDurationInTcMinus1( i, 0 );
1732    hrd->setLowDelayHrdFlag( i, 0 );
1733    hrd->setCpbCntMinus1( i, 0 );
1734
1735    birateValue  = bitRate;
1736    cpbSizeValue = bitRate;                                     // 1 second
1737    ducpbSizeValue = bitRate/numDU;
1738#if L0363_DU_BIT_RATE
1739    duBitRateValue = bitRate;
1740#endif
1741    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
1742    {
1743      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
1744      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
1745      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
1746      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
1747
1748      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
1749      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
1750      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
1751#if L0363_DU_BIT_RATE
1752      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
1753#endif
1754      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
1755    }
1756  }
1757}
1758const Int TComSPS::m_winUnitX[]={1,2,2,1};
1759const Int TComSPS::m_winUnitY[]={1,2,1,1};
1760
1761TComPPS::TComPPS()
1762: m_PPSId                       (0)
1763, m_SPSId                       (0)
1764, m_picInitQPMinus26            (0)
1765, m_useDQP                      (false)
1766, m_bConstrainedIntraPred       (false)
1767, m_bSliceChromaQpFlag          (false)
1768, m_pcSPS                       (NULL)
1769, m_uiMaxCuDQPDepth             (0)
1770, m_uiMinCuDQPSize              (0)
1771, m_chromaCbQpOffset            (0)
1772, m_chromaCrQpOffset            (0)
1773, m_numRefIdxL0DefaultActive    (1)
1774, m_numRefIdxL1DefaultActive    (1)
1775, m_TransquantBypassEnableFlag  (false)
1776, m_useTransformSkip             (false)
1777, m_dependentSliceSegmentsEnabledFlag    (false)
1778, m_tilesEnabledFlag               (false)
1779, m_entropyCodingSyncEnabledFlag   (false)
1780, m_loopFilterAcrossTilesEnabledFlag  (true)
1781, m_uniformSpacingFlag           (0)
1782, m_iNumColumnsMinus1            (0)
1783, m_puiColumnWidth               (NULL)
1784, m_iNumRowsMinus1               (0)
1785, m_puiRowHeight                 (NULL)
1786, m_iNumSubstreams             (1)
1787, m_signHideFlag(0)
1788, m_cabacInitPresentFlag        (false)
1789, m_encCABACTableIdx            (I_SLICE)
1790, m_sliceHeaderExtensionPresentFlag    (false)
1791, m_loopFilterAcrossSlicesEnabledFlag (false)
1792, m_listsModificationPresentFlag(  0)
1793, m_numExtraSliceHeaderBits(0)
1794{
1795  m_scalingList = new TComScalingList;
1796}
1797
1798TComPPS::~TComPPS()
1799{
1800  if( m_iNumColumnsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1801  {
1802    if (m_puiColumnWidth) delete [] m_puiColumnWidth; 
1803    m_puiColumnWidth = NULL;
1804  }
1805  if( m_iNumRowsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1806  {
1807    if (m_puiRowHeight) delete [] m_puiRowHeight;
1808    m_puiRowHeight = NULL;
1809  }
1810  delete m_scalingList;
1811}
1812
1813TComReferencePictureSet::TComReferencePictureSet()
1814: m_numberOfPictures (0)
1815, m_numberOfNegativePictures (0)
1816, m_numberOfPositivePictures (0)
1817, m_numberOfLongtermPictures (0)
1818, m_interRPSPrediction (0) 
1819, m_deltaRIdxMinus1 (0)   
1820, m_deltaRPS (0) 
1821, m_numRefIdc (0) 
1822{
1823  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
1824  ::memset( m_POC, 0, sizeof(m_POC) );
1825  ::memset( m_used, 0, sizeof(m_used) );
1826  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
1827}
1828
1829TComReferencePictureSet::~TComReferencePictureSet()
1830{
1831}
1832
1833Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
1834{
1835  m_used[bufferNum] = used;
1836}
1837
1838Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
1839{
1840  m_deltaPOC[bufferNum] = deltaPOC;
1841}
1842
1843Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
1844{
1845  m_numberOfPictures = numberOfPictures;
1846}
1847
1848Int TComReferencePictureSet::getUsed(Int bufferNum)
1849{
1850  return m_used[bufferNum];
1851}
1852
1853Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
1854{
1855  return m_deltaPOC[bufferNum];
1856}
1857
1858Int TComReferencePictureSet::getNumberOfPictures()
1859{
1860  return m_numberOfPictures;
1861}
1862
1863Int TComReferencePictureSet::getPOC(Int bufferNum)
1864{
1865  return m_POC[bufferNum];
1866}
1867
1868Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
1869{
1870  m_POC[bufferNum] = POC;
1871}
1872
1873Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
1874{
1875  return m_bCheckLTMSB[bufferNum];
1876}
1877
1878Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
1879{
1880  m_bCheckLTMSB[bufferNum] = b;
1881}
1882
1883/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
1884 * \param uiBufferNum
1885 * \param iRefIdc
1886 * \returns Void
1887 */
1888Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
1889{
1890  m_refIdc[bufferNum] = refIdc;
1891}
1892
1893/** get the reference idc value at uiBufferNum
1894 * \param uiBufferNum
1895 * \returns Int
1896 */
1897Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
1898{
1899  return m_refIdc[bufferNum];
1900}
1901
1902/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
1903 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
1904 *  +ve values are in increasing order.
1905 * \returns Void
1906 */
1907Void TComReferencePictureSet::sortDeltaPOC()
1908{
1909  // sort in increasing order (smallest first)
1910  for(Int j=1; j < getNumberOfPictures(); j++)
1911  { 
1912    Int deltaPOC = getDeltaPOC(j);
1913    Bool used = getUsed(j);
1914    for (Int k=j-1; k >= 0; k--)
1915    {
1916      Int temp = getDeltaPOC(k);
1917      if (deltaPOC < temp)
1918      {
1919        setDeltaPOC(k+1, temp);
1920        setUsed(k+1, getUsed(k));
1921        setDeltaPOC(k, deltaPOC);
1922        setUsed(k, used);
1923      }
1924    }
1925  }
1926  // flip the negative values to largest first
1927  Int numNegPics = getNumberOfNegativePictures();
1928  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
1929  { 
1930    Int deltaPOC = getDeltaPOC(j);
1931    Bool used = getUsed(j);
1932    setDeltaPOC(j, getDeltaPOC(k));
1933    setUsed(j, getUsed(k));
1934    setDeltaPOC(k, deltaPOC);
1935    setUsed(k, used);
1936  }
1937}
1938
1939/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
1940 *  A "*" is added to the deltaPOC value if it is Used bu current.
1941 * \returns Void
1942 */
1943Void TComReferencePictureSet::printDeltaPOC()
1944{
1945  printf("DeltaPOC = { ");
1946  for(Int j=0; j < getNumberOfPictures(); j++)
1947  {
1948    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
1949  } 
1950  if (getInterRPSPrediction()) 
1951  {
1952    printf("}, RefIdc = { ");
1953    for(Int j=0; j < getNumRefIdc(); j++)
1954    {
1955      printf("%d ", getRefIdc(j));
1956    } 
1957  }
1958  printf("}\n");
1959}
1960
1961TComRPSList::TComRPSList()
1962:m_referencePictureSets (NULL)
1963{
1964}
1965
1966TComRPSList::~TComRPSList()
1967{
1968}
1969
1970Void TComRPSList::create( Int numberOfReferencePictureSets)
1971{
1972  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
1973  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
1974}
1975
1976Void TComRPSList::destroy()
1977{
1978  if (m_referencePictureSets)
1979  {
1980    delete [] m_referencePictureSets;
1981  }
1982  m_numberOfReferencePictureSets = 0;
1983  m_referencePictureSets = NULL;
1984}
1985
1986
1987
1988TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
1989{
1990  return &m_referencePictureSets[referencePictureSetNum];
1991}
1992
1993Int TComRPSList::getNumberOfReferencePictureSets()
1994{
1995  return m_numberOfReferencePictureSets;
1996}
1997
1998Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
1999{
2000  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2001}
2002
2003TComRefPicListModification::TComRefPicListModification()
2004: m_bRefPicListModificationFlagL0 (false)
2005, m_bRefPicListModificationFlagL1 (false)
2006{
2007  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
2008  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
2009}
2010
2011TComRefPicListModification::~TComRefPicListModification()
2012{
2013}
2014
2015TComScalingList::TComScalingList()
2016{
2017  m_useTransformSkip = false;
2018  init();
2019}
2020TComScalingList::~TComScalingList()
2021{
2022  destroy();
2023}
2024
2025/** set default quantization matrix to array
2026*/
2027Void TComSlice::setDefaultScalingList()
2028{
2029  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2030  {
2031    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2032    {
2033      getScalingList()->processDefaultMarix(sizeId, listId);
2034    }
2035  }
2036}
2037/** check if use default quantization matrix
2038 * \returns true if use default quantization matrix in all size
2039*/
2040Bool TComSlice::checkDefaultScalingList()
2041{
2042  UInt defaultCounter=0;
2043
2044  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2045  {
2046    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2047    {
2048      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
2049     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
2050      {
2051        defaultCounter++;
2052      }
2053    }
2054  }
2055  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
2056}
2057
2058#if H_MV
2059Void TComSlice::createAndApplyIvReferencePictureSet( TComPicLists* ivPicLists, std::vector<TComPic*>& refPicSetInterLayer )
2060{
2061  refPicSetInterLayer.clear(); 
2062
2063  for( Int i = 0; i < getVPS()->getNumDirectRefLayers( getLayerIdInVps() ); i++ ) 
2064  {
2065    Int layerIdRef = getVPS()->getRefLayerId( getLayerIdInVps(), i ); 
2066    TComPic* picRef = ivPicLists->getPic( layerIdRef, getPOC() ) ; 
2067    assert ( picRef != 0 ); 
2068
2069    picRef->getPicYuvRec()->extendPicBorder(); 
2070    picRef->setIsLongTerm( true );       
2071    picRef->getSlice(0)->setReferenced( true );       
2072
2073    refPicSetInterLayer.push_back( picRef ); 
2074  }
2075}
2076
2077Void TComSlice::markIvRefPicsAsShortTerm( std::vector<TComPic*> refPicSetInterLayer )
2078{
2079  // Mark as shortterm
2080  for ( Int i = 0; i < refPicSetInterLayer.size(); i++ ) 
2081  {
2082    refPicSetInterLayer[i]->setIsLongTerm( false ); 
2083  }
2084}
2085
2086Void TComSlice::markIvRefPicsAsUnused( TComPicLists* ivPicLists, std::vector<Int> targetDecLayerIdSet, TComVPS* vps, Int curLayerId, Int curPoc )
2087{
2088  // Fill targetDecLayerIdSet with all layers if empty.
2089  if (targetDecLayerIdSet.size() == 0 )   
2090  {
2091    for ( Int layerIdInVps = 0; layerIdInVps < vps->getMaxLayers(); layerIdInVps++ )
2092    {
2093      targetDecLayerIdSet.push_back( vps->getLayerIdInNuh( layerIdInVps ) ); 
2094    }
2095  }     
2096
2097  Int numTargetDecLayers = (Int) targetDecLayerIdSet.size(); 
2098  Int latestDecIdx; 
2099  for ( latestDecIdx = 0; latestDecIdx < numTargetDecLayers; latestDecIdx++)
2100  {
2101    if ( targetDecLayerIdSet[ latestDecIdx ] == curLayerId )
2102      break; 
2103  }       
2104
2105  for( Int i = 0; i <= latestDecIdx; i++ ) 
2106  {
2107    if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ i ] ) )
2108    {
2109      TComPic* pcPic = ivPicLists->getPic( targetDecLayerIdSet[ i ], curPoc ); 
2110      if( pcPic->getSlice(0)->isReferenced() && pcPic->getSlice(0)->getTemporalLayerNonReferenceFlag() ) 
2111      { 
2112        Bool remainingInterLayerReferencesFlag = false; 
2113        for( Int j = latestDecIdx + 1; j < numTargetDecLayers; j++ )
2114        { 
2115          TComVPS* vpsSlice = pcPic->getSlice(0)->getVPS(); 
2116          if ( vps->nuhLayerIdIncluded( targetDecLayerIdSet[ j ] ) )
2117          {
2118            Int targetDecLayerIdinVPS = vpsSlice->getLayerIdInVps( targetDecLayerIdSet[ j ] ); 
2119            for( Int k = 0; k < vpsSlice->getNumDirectRefLayers( targetDecLayerIdinVPS ); k++ )
2120              if ( targetDecLayerIdSet[ i ] == vpsSlice->getRefLayerId( targetDecLayerIdinVPS,  k  ) )
2121                remainingInterLayerReferencesFlag = true;
2122          }
2123        }
2124        if( !remainingInterLayerReferencesFlag )
2125          pcPic->getSlice(0)->setReferenced( false );                   
2126      }
2127    }
2128  }
2129}
2130
2131Void TComSlice::xPrintRefPicList()
2132{ 
2133  for ( Int li = 0; li < 2; li++)
2134  {   
2135    std::cout << std::endl << "RefPicListL" <<  li << ":" << std::endl; 
2136    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[li]-1); rIdx ++)
2137    {     
2138      if (rIdx == 0 && li == 0) m_apcRefPicList[li][rIdx]->print( true );
2139       
2140      m_apcRefPicList[li][rIdx]->print( false );
2141    }
2142  }
2143}
2144#endif
2145
2146/** get scaling matrix from RefMatrixID
2147 * \param sizeId size index
2148 * \param Index of input matrix
2149 * \param Index of reference matrix
2150 */
2151Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
2152{
2153  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2154}
2155
2156/** parse syntax infomation
2157 *  \param pchFile syntax infomation
2158 *  \returns false if successful
2159 */
2160Bool TComScalingList::xParseScalingList(Char* pchFile)
2161{
2162  FILE *fp;
2163  Char line[1024];
2164  UInt sizeIdc,listIdc;
2165  UInt i,size = 0;
2166  Int *src=0,data;
2167  Char *ret;
2168  UInt  retval;
2169
2170  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
2171  {
2172    printf("can't open file %s :: set Default Matrix\n",pchFile);
2173    return true;
2174  }
2175
2176  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
2177  {
2178    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
2179    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
2180    {
2181      src = getScalingListAddress(sizeIdc, listIdc);
2182
2183      fseek(fp,0,0);
2184      do 
2185      {
2186        ret = fgets(line, 1024, fp);
2187        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
2188        {
2189          printf("Error: can't read Matrix :: set Default Matrix\n");
2190          return true;
2191        }
2192      }
2193      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
2194      for (i=0; i<size; i++)
2195      {
2196        retval = fscanf(fp, "%d,", &data);
2197        if (retval!=1)
2198        {
2199          printf("Error: can't read Matrix :: set Default Matrix\n");
2200          return true;
2201        }
2202        src[i] = data;
2203      }
2204      //set DC value for default matrix check
2205      setScalingListDC(sizeIdc,listIdc,src[0]);
2206
2207      if(sizeIdc > SCALING_LIST_8x8)
2208      {
2209        fseek(fp,0,0);
2210        do 
2211        {
2212          ret = fgets(line, 1024, fp);
2213          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
2214          {
2215            printf("Error: can't read DC :: set Default Matrix\n");
2216            return true;
2217          }
2218        }
2219        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
2220        retval = fscanf(fp, "%d,", &data);
2221        if (retval!=1)
2222        {
2223          printf("Error: can't read Matrix :: set Default Matrix\n");
2224          return true;
2225        }
2226        //overwrite DC value when size of matrix is larger than 16x16
2227        setScalingListDC(sizeIdc,listIdc,data);
2228      }
2229    }
2230  }
2231  fclose(fp);
2232  return false;
2233}
2234
2235/** initialization process of quantization matrix array
2236 */
2237Void TComScalingList::init()
2238{
2239  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2240  {
2241    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2242    {
2243      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
2244    }
2245  }
2246  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
2247}
2248
2249/** destroy quantization matrix array
2250 */
2251Void TComScalingList::destroy()
2252{
2253  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2254  {
2255    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2256    {
2257      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
2258    }
2259  }
2260}
2261
2262/** get default address of quantization matrix
2263 * \param sizeId size index
2264 * \param listId list index
2265 * \returns pointer of quantization matrix
2266 */
2267Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
2268{
2269  Int *src = 0;
2270  switch(sizeId)
2271  {
2272    case SCALING_LIST_4x4:
2273      src = g_quantTSDefault4x4;
2274      break;
2275    case SCALING_LIST_8x8:
2276      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2277      break;
2278    case SCALING_LIST_16x16:
2279      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2280      break;
2281    case SCALING_LIST_32x32:
2282      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2283      break;
2284    default:
2285      assert(0);
2286      src = NULL;
2287      break;
2288  }
2289  return src;
2290}
2291
2292/** process of default matrix
2293 * \param sizeId size index
2294 * \param Index of input matrix
2295 */
2296Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId)
2297{
2298  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2299  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
2300}
2301
2302/** check DC value of matrix for default matrix signaling
2303 */
2304Void TComScalingList::checkDcOfMatrix()
2305{
2306  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2307  {
2308    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2309    {
2310      //check default matrix?
2311      if(getScalingListDC(sizeId,listId) == 0)
2312      {
2313        processDefaultMarix(sizeId, listId);
2314      }
2315    }
2316  }
2317}
2318
2319ParameterSetManager::ParameterSetManager()
2320: m_vpsMap(MAX_NUM_VPS)
2321, m_spsMap(MAX_NUM_SPS)
2322, m_ppsMap(MAX_NUM_PPS)
2323, m_activeVPSId(-1)
2324, m_activeSPSId(-1)
2325, m_activePPSId(-1)
2326{
2327}
2328
2329
2330ParameterSetManager::~ParameterSetManager()
2331{
2332}
2333
2334//! activate a SPS from a active parameter sets SEI message
2335//! \returns true, if activation is successful
2336Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
2337{
2338  TComSPS *sps = m_spsMap.getPS(spsId);
2339  if (sps)
2340  {
2341    Int vpsId = sps->getVPSId();
2342    if (m_vpsMap.getPS(vpsId))
2343    {
2344      m_activeVPSId = vpsId;
2345      m_activeSPSId = spsId;
2346      return true;
2347    }
2348    else
2349    {
2350      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
2351    }
2352  }
2353  else
2354  {
2355    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
2356  }
2357  return false;
2358}
2359
2360//! activate a PPS and depending on isIDR parameter also SPS and VPS
2361//! \returns true, if activation is successful
2362Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
2363{
2364  TComPPS *pps = m_ppsMap.getPS(ppsId);
2365  if (pps)
2366  {
2367    Int spsId = pps->getSPSId();
2368#if H_MV
2369    // active parameter sets per layer should be used here
2370#else
2371    if (!isIRAP && (spsId != m_activeSPSId))
2372    {
2373      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
2374      return false;
2375    }
2376#endif
2377    TComSPS *sps = m_spsMap.getPS(spsId);
2378    if (sps)
2379    {
2380      Int vpsId = sps->getVPSId();
2381      if (!isIRAP && (vpsId != m_activeVPSId))
2382      {
2383        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
2384        return false;
2385      }
2386      if (m_vpsMap.getPS(vpsId))
2387      {
2388        m_activePPSId = ppsId;
2389        m_activeVPSId = vpsId;
2390        m_activeSPSId = spsId;
2391        return true;
2392      }
2393      else
2394      {
2395        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
2396      }
2397    }
2398    else
2399    {
2400      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
2401    }
2402  }
2403  else
2404  {
2405    printf("Warning: tried to activate non-existing PPS.");
2406  }
2407  return false;
2408}
2409
2410ProfileTierLevel::ProfileTierLevel()
2411  : m_profileSpace    (0)
2412  , m_tierFlag        (false)
2413  , m_profileIdc      (0)
2414  , m_levelIdc        (0)
2415#if L0046_CONSTRAINT_FLAGS
2416, m_progressiveSourceFlag  (false)
2417, m_interlacedSourceFlag   (false)
2418, m_nonPackedConstraintFlag(false)
2419, m_frameOnlyConstraintFlag(false)
2420#endif
2421{
2422  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
2423}
2424
2425TComPTL::TComPTL()
2426{
2427  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
2428  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
2429}
2430
2431#if H_MV
2432Void TComPTL::copyLevelFrom( TComPTL* source )
2433{
2434  getGeneralPTL()->setLevelIdc( source->getGeneralPTL()->getLevelIdc() );
2435  for( Int subLayer = 0; subLayer < 6; subLayer++ )
2436  {
2437    setSubLayerLevelPresentFlag( subLayer, source->getSubLayerLevelPresentFlag( subLayer ) );
2438    getSubLayerPTL( subLayer )->setLevelIdc( source->getSubLayerPTL( subLayer )->getLevelIdc() );
2439  }
2440}
2441#endif
2442#if SIGNAL_BITRATE_PICRATE_IN_VPS
2443TComBitRatePicRateInfo::TComBitRatePicRateInfo()
2444{
2445  ::memset(m_bitRateInfoPresentFlag, 0, sizeof(m_bitRateInfoPresentFlag));
2446  ::memset(m_picRateInfoPresentFlag, 0, sizeof(m_picRateInfoPresentFlag));
2447  ::memset(m_avgBitRate,             0, sizeof(m_avgBitRate));
2448  ::memset(m_maxBitRate,             0, sizeof(m_maxBitRate));
2449  ::memset(m_constantPicRateIdc,     0, sizeof(m_constantPicRateIdc));
2450  ::memset(m_avgPicRate,             0, sizeof(m_avgPicRate));
2451}
2452#endif
2453//! \}
Note: See TracBrowser for help on using the repository browser.