source: SHVCSoftware/trunk/source/Lib/TLibCommon/TComSlice.cpp @ 21

Last change on this file since 21 was 11, checked in by seregin, 12 years ago

SET_SLICE_LAYER_ID: set layerId to the slice

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