source: SHVCSoftware/branches/SHM-4.0-dev/source/Lib/TLibCommon/TComSlice.cpp @ 464

Last change on this file since 464 was 462, checked in by seregin, 12 years ago

use layerIdc for accessing the reference layer picture for MFM constraint

  • Property svn:eol-style set to native
File size: 98.6 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
47#if SVC_EXTENSION
48ParameterSetMap<TComVPS> ParameterSetManager::m_vpsMap(MAX_NUM_VPS);
49Int ParameterSetManager::m_activeVPSId = -1;
50#if IL_SL_SIGNALLING_N0371
51TComSPS* TComSPS::m_pcSPS[MAX_LAYERS];
52TComPPS* TComPPS::m_pcPPS[MAX_LAYERS];
53#endif
54#endif
55
56TComSlice::TComSlice()
57: m_iPPSId                        ( -1 )
58, m_iPOC                          ( 0 )
59, m_iLastIDR                      ( 0 )
60, m_eNalUnitType                  ( NAL_UNIT_CODED_SLICE_IDR_W_RADL )
61, m_eSliceType                    ( I_SLICE )
62, m_iSliceQp                      ( 0 )
63, m_dependentSliceSegmentFlag            ( false )
64#if ADAPTIVE_QP_SELECTION
65, m_iSliceQpBase                  ( 0 )
66#endif
67, m_deblockingFilterDisable        ( false )
68, m_deblockingFilterOverrideFlag   ( false )
69, m_deblockingFilterBetaOffsetDiv2 ( 0 )
70, m_deblockingFilterTcOffsetDiv2   ( 0 )
71, m_bCheckLDC                     ( false )
72, m_iSliceQpDelta                 ( 0 )
73, m_iSliceQpDeltaCb               ( 0 )
74, m_iSliceQpDeltaCr               ( 0 )
75, m_iDepth                        ( 0 )
76, m_bRefenced                     ( false )
77, m_pcSPS                         ( NULL )
78, m_pcPPS                         ( NULL )
79, m_pcPic                         ( NULL )
80, m_colFromL0Flag                 ( 1 )
81, m_colRefIdx                     ( 0 )
82#if SAO_CHROMA_LAMBDA
83, m_dLambdaLuma( 0.0 )
84, m_dLambdaChroma( 0.0 )
85#else
86, m_dLambda                       ( 0.0 )
87#endif
88, m_uiTLayer                      ( 0 )
89, m_bTLayerSwitchingFlag          ( false )
90, m_sliceMode                   ( 0 )
91, m_sliceArgument               ( 0 )
92, m_sliceCurStartCUAddr         ( 0 )
93, m_sliceCurEndCUAddr           ( 0 )
94, m_sliceIdx                    ( 0 )
95, m_sliceSegmentMode            ( 0 )
96, m_sliceSegmentArgument        ( 0 )
97, m_sliceSegmentCurStartCUAddr  ( 0 )
98, m_sliceSegmentCurEndCUAddr    ( 0 )
99, m_nextSlice                    ( false )
100, m_nextSliceSegment             ( false )
101, m_sliceBits                   ( 0 )
102, m_sliceSegmentBits         ( 0 )
103, m_bFinalized                    ( false )
104, m_uiTileOffstForMultES          ( 0 )
105, m_puiSubstreamSizes             ( NULL )
106, m_cabacInitFlag                 ( false )
107, m_bLMvdL1Zero                   ( false )
108, m_numEntryPointOffsets          ( 0 )
109, m_temporalLayerNonReferenceFlag ( false )
110, m_enableTMVPFlag                ( true )
111#if SVC_EXTENSION
112, m_layerId                     ( 0 )
113#if REF_IDX_MFM
114, m_bMFMEnabledFlag               ( false )
115#endif
116#if POC_RESET_FLAG
117, m_bPocResetFlag                 ( false )
118#endif
119, m_bDiscardableFlag              ( false )
120#endif //SVC_EXTENSION
121{
122  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = 0;
123
124#if SVC_EXTENSION
125  memset( m_pcBaseColPic, 0, sizeof( m_pcBaseColPic ) );
126#if JCTVC_M0458_INTERLAYER_RPS_SIG
127  m_activeNumILRRefIdx        = 0; 
128  m_interLayerPredEnabledFlag = 0;
129  ::memset( m_interLayerPredLayerIdc, 0, sizeof(m_interLayerPredLayerIdc) );
130#else
131  m_numILRRefIdx = 0;
132#endif
133#if M0457_COL_PICTURE_SIGNALING && !REMOVE_COL_PICTURE_SIGNALING
134  m_altColIndicationFlag = false;
135  m_colRefLayerIdx       = 0;
136#endif
137#if M0457_IL_SAMPLE_PRED_ONLY_FLAG
138  m_numSamplePredRefLayers       = 0;
139  m_interLayerSamplePredOnlyFlag = false;
140#endif
141#endif //SVC_EXTENSION
142
143  initEqualRef();
144 
145  for ( Int idx = 0; idx < MAX_NUM_REF; idx++ )
146  {
147    m_list1IdxToList0Idx[idx] = -1;
148  }
149  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF; iNumCount++)
150  {
151    m_apcRefPicList [0][iNumCount] = NULL;
152    m_apcRefPicList [1][iNumCount] = NULL;
153    m_aiRefPOCList  [0][iNumCount] = 0;
154    m_aiRefPOCList  [1][iNumCount] = 0;
155  }
156  resetWpScaling();
157  initWpAcDcParam();
158  m_saoEnabledFlag = false;
159}
160
161TComSlice::~TComSlice()
162{
163  delete[] m_puiSubstreamSizes;
164  m_puiSubstreamSizes = NULL;
165}
166
167
168#if SVC_EXTENSION
169Void TComSlice::initSlice( UInt layerId )
170#else
171Void TComSlice::initSlice()
172#endif
173{
174#if SVC_EXTENSION
175  m_layerId = layerId;
176#if JCTVC_M0458_INTERLAYER_RPS_SIG
177  m_activeNumILRRefIdx        = 0;
178  m_interLayerPredEnabledFlag = 0;
179#else
180  m_numILRRefIdx              = 0;
181#endif
182#if M0457_IL_SAMPLE_PRED_ONLY_FLAG
183  m_numSamplePredRefLayers       = 0;
184  m_interLayerSamplePredOnlyFlag = false;
185#endif
186#endif
187  m_aiNumRefIdx[0]      = 0;
188  m_aiNumRefIdx[1]      = 0;
189 
190  m_colFromL0Flag = 1;
191 
192  m_colRefIdx = 0;
193  initEqualRef();
194  m_bCheckLDC = false;
195  m_iSliceQpDeltaCb = 0;
196  m_iSliceQpDeltaCr = 0;
197
198  m_maxNumMergeCand = MRG_MAX_NUM_CANDS;
199
200  m_bFinalized=false;
201
202  m_tileByteLocation.clear();
203  m_cabacInitFlag        = false;
204  m_numEntryPointOffsets = 0;
205  m_enableTMVPFlag = true;
206}
207
208Bool TComSlice::getRapPicFlag()
209{
210  return getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
211      || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
212      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
213      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
214      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
215      || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA;
216}
217
218/**
219 - allocate table to contain substream sizes to be written to the slice header.
220 .
221 \param uiNumSubstreams Number of substreams -- the allocation will be this value - 1.
222 */
223Void  TComSlice::allocSubstreamSizes(UInt uiNumSubstreams)
224{
225  delete[] m_puiSubstreamSizes;
226  m_puiSubstreamSizes = new UInt[uiNumSubstreams > 0 ? uiNumSubstreams-1 : 0];
227}
228
229Void  TComSlice::sortPicList(TComList<TComPic*>& rcListPic)
230{
231  TComPic*    pcPicExtract;
232  TComPic*    pcPicInsert;
233 
234  TComList<TComPic*>::iterator    iterPicExtract;
235  TComList<TComPic*>::iterator    iterPicExtract_1;
236  TComList<TComPic*>::iterator    iterPicInsert;
237 
238  for (Int i = 1; i < (Int)(rcListPic.size()); i++)
239  {
240    iterPicExtract = rcListPic.begin();
241    for (Int j = 0; j < i; j++) iterPicExtract++;
242    pcPicExtract = *(iterPicExtract);
243    pcPicExtract->setCurrSliceIdx(0);
244   
245    iterPicInsert = rcListPic.begin();
246    while (iterPicInsert != iterPicExtract)
247    {
248      pcPicInsert = *(iterPicInsert);
249      pcPicInsert->setCurrSliceIdx(0);
250      if (pcPicInsert->getPOC() >= pcPicExtract->getPOC())
251      {
252        break;
253      }
254     
255      iterPicInsert++;
256    }
257   
258    iterPicExtract_1 = iterPicExtract;    iterPicExtract_1++;
259   
260    //  swap iterPicExtract and iterPicInsert, iterPicExtract = curr. / iterPicInsert = insertion position
261    rcListPic.insert (iterPicInsert, iterPicExtract, iterPicExtract_1);
262    rcListPic.erase  (iterPicExtract);
263  }
264}
265
266TComPic* TComSlice::xGetRefPic (TComList<TComPic*>& rcListPic,
267                                Int                 poc)
268{
269  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
270  TComPic*                      pcPic = *(iterPic);
271  while ( iterPic != rcListPic.end() )
272  {
273    if(pcPic->getPOC() == poc)
274    {
275      break;
276    }
277    iterPic++;
278    pcPic = *(iterPic);
279  }
280#if POC_RESET_FLAG
281  assert( pcPic->getSlice(0)->isReferenced() );
282#endif
283  return  pcPic;
284}
285
286
287TComPic* TComSlice::xGetLongTermRefPic(TComList<TComPic*>& rcListPic, Int poc, Bool pocHasMsb)
288{
289  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
290  TComPic*                      pcPic = *(iterPic);
291  TComPic*                      pcStPic = pcPic;
292 
293  Int pocCycle = 1 << getSPS()->getBitsForPOC();
294  if (!pocHasMsb)
295  {
296    poc = poc & (pocCycle - 1);
297  }
298 
299  while ( iterPic != rcListPic.end() )
300  {
301    pcPic = *(iterPic);
302    if (pcPic && pcPic->getPOC()!=this->getPOC() && pcPic->getSlice( 0 )->isReferenced())
303    {
304      Int picPoc = pcPic->getPOC();
305      if (!pocHasMsb)
306      {
307        picPoc = picPoc & (pocCycle - 1);
308      }
309     
310      if (poc == picPoc)
311      {
312       if (pcPic->getIsLongTerm())
313      {
314        return pcPic;
315      }
316      else
317      {
318        pcStPic = pcPic;
319      }
320      break;
321    }
322    }
323
324    iterPic++;
325  }
326 
327  return  pcStPic;
328}
329
330Void TComSlice::setRefPOCList()
331{
332  for (Int iDir = 0; iDir < 2; iDir++)
333  {
334    for (Int iNumRefIdx = 0; iNumRefIdx < m_aiNumRefIdx[iDir]; iNumRefIdx++)
335    {
336      m_aiRefPOCList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getPOC();
337    }
338  }
339
340}
341
342Void TComSlice::setList1IdxToList0Idx()
343{
344  Int idxL0, idxL1;
345  for ( idxL1 = 0; idxL1 < getNumRefIdx( REF_PIC_LIST_1 ); idxL1++ )
346  {
347    m_list1IdxToList0Idx[idxL1] = -1;
348    for ( idxL0 = 0; idxL0 < getNumRefIdx( REF_PIC_LIST_0 ); idxL0++ )
349    {
350      if ( m_apcRefPicList[REF_PIC_LIST_0][idxL0]->getPOC() == m_apcRefPicList[REF_PIC_LIST_1][idxL1]->getPOC() )
351      {
352        m_list1IdxToList0Idx[idxL1] = idxL0;
353        break;
354      }
355    }
356  }
357}
358
359#if FIX1071
360#if SVC_EXTENSION
361Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr, TComPic** ilpPic)
362#else
363Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr )
364#endif
365#else
366Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic )
367#endif
368{
369#if FIX1071
370  if (!checkNumPocTotalCurr)
371#endif
372  {
373    if (m_eSliceType == I_SLICE)
374    {
375      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
376      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
377     
378      return;
379    }
380 
381    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
382    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
383  }
384
385  TComPic*  pcRefPic= NULL;
386  TComPic*  RefPicSetStCurr0[16];
387  TComPic*  RefPicSetStCurr1[16];
388  TComPic*  RefPicSetLtCurr[16];
389  UInt NumPocStCurr0 = 0;
390  UInt NumPocStCurr1 = 0;
391  UInt NumPocLtCurr = 0;
392  Int i;
393
394#if SVC_EXTENSION
395  if( m_layerId == 0 || ( m_layerId > 0 && ( m_activeNumILRRefIdx == 0 || !((getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP) && (getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA)) ) ) )
396  {
397#endif
398  for(i=0; i < m_pcRPS->getNumberOfNegativePictures(); i++)
399  {
400    if(m_pcRPS->getUsed(i))
401    {
402      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
403      pcRefPic->setIsLongTerm(0);
404      pcRefPic->getPicYuvRec()->extendPicBorder();
405      RefPicSetStCurr0[NumPocStCurr0] = pcRefPic;
406#if M0457_IL_SAMPLE_PRED_ONLY_FLAG
407      if( !m_interLayerSamplePredOnlyFlag || pcRefPic->getLayerId() < getLayerId()) 
408#endif
409      NumPocStCurr0++;
410      pcRefPic->setCheckLTMSBPresent(false); 
411    }
412  }
413 
414  for(; i < m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures(); i++)
415  {
416    if(m_pcRPS->getUsed(i))
417    {
418      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
419      pcRefPic->setIsLongTerm(0);
420      pcRefPic->getPicYuvRec()->extendPicBorder();
421      RefPicSetStCurr1[NumPocStCurr1] = pcRefPic;
422#if M0457_IL_SAMPLE_PRED_ONLY_FLAG
423      if( !m_interLayerSamplePredOnlyFlag || pcRefPic->getLayerId() < getLayerId()) 
424#endif
425      NumPocStCurr1++;
426      pcRefPic->setCheckLTMSBPresent(false); 
427    }
428  }
429 
430  for(i = m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()+m_pcRPS->getNumberOfLongtermPictures()-1; i > m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()-1 ; i--)
431  {
432    if(m_pcRPS->getUsed(i))
433    {
434      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
435      pcRefPic->setIsLongTerm(1);
436      pcRefPic->getPicYuvRec()->extendPicBorder();
437      RefPicSetLtCurr[NumPocLtCurr] = pcRefPic;
438#if M0457_IL_SAMPLE_PRED_ONLY_FLAG
439      if( !m_interLayerSamplePredOnlyFlag || pcRefPic->getLayerId() < getLayerId()) 
440#endif
441      NumPocLtCurr++;
442    }
443    if(pcRefPic==NULL) 
444    {
445      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
446    }
447    pcRefPic->setCheckLTMSBPresent(m_pcRPS->getCheckLTMSBPresent(i)); 
448  }
449#if SVC_EXTENSION
450  }
451#endif
452
453#if SVC_EXTENSION
454  for( i = 0; i < m_activeNumILRRefIdx; i++ )
455  {
456    UInt refLayerIdc = m_interLayerPredLayerIdc[i];
457    //inter-layer reference picture
458
459    Int maxTidIlRefPicsPlus1 = ( m_layerId > 0 && m_activeNumILRRefIdx > 0)? m_pcVPS->getMaxTidIlRefPicsPlus1(ilpPic[refLayerIdc]->getSlice(0)->getLayerId()) : 0;
460    if( m_layerId > 0 && m_activeNumILRRefIdx > 0 && ( ( (Int)(ilpPic[refLayerIdc]->getSlice(0)->getTLayer())<=  maxTidIlRefPicsPlus1-1) || (maxTidIlRefPicsPlus1==0 && ilpPic[refLayerIdc]->getSlice(0)->getRapPicFlag()) )  ) 
461    {
462#if REF_IDX_MFM
463#if M0457_COL_PICTURE_SIGNALING
464      if(!(m_eNalUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP && m_eNalUnitType <= NAL_UNIT_CODED_SLICE_CRA) && getMFMEnabledFlag())
465#else
466      if(!(m_eNalUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP && m_eNalUnitType <= NAL_UNIT_CODED_SLICE_CRA) && m_pcSPS->getMFMEnabledFlag())
467#endif
468      { 
469        ilpPic[refLayerIdc]->copyUpsampledMvField( refLayerIdc, m_pcBaseColPic[refLayerIdc] );
470      }
471      else
472      {
473        ilpPic[refLayerIdc]->initUpsampledMvField();
474      }
475#endif
476      ilpPic[refLayerIdc]->setIsLongTerm(1);
477    }
478  }
479#endif
480  // ref_pic_list_init
481  TComPic*  rpsCurrList0[MAX_NUM_REF+1];
482  TComPic*  rpsCurrList1[MAX_NUM_REF+1];
483#if SVC_EXTENSION
484  Int numInterLayerRPSPics = 0;
485#if M0040_ADAPTIVE_RESOLUTION_CHANGE
486  if( m_layerId > 0 && m_activeNumILRRefIdx > 0 )
487#else
488  if( m_layerId > 0 )
489#endif
490  {
491    for( i=0; i < m_pcVPS->getNumDirectRefLayers( m_layerId ); i++ )
492    {
493      Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[i]->getSlice(0)->getLayerId());
494      if( ((Int)(ilpPic[i]->getSlice(0)->getTLayer())<= maxTidIlRefPicsPlus1-1) || (maxTidIlRefPicsPlus1==0 && ilpPic[i]->getSlice(0)->getRapPicFlag() ) )
495      {
496        numInterLayerRPSPics++;
497      }
498    }
499#if JCTVC_M0458_INTERLAYER_RPS_SIG
500    if (numInterLayerRPSPics < m_activeNumILRRefIdx)
501    {
502      m_activeNumILRRefIdx = numInterLayerRPSPics;
503    }
504#if MAX_ONE_RESAMPLING_DIRECT_LAYERS
505#if SCALABILITY_MASK_E0104
506    if( m_pcVPS->getScalabilityMask(2) )
507#else
508    if( m_pcVPS->getScalabilityMask(1) )
509#endif
510    {
511      Int numResampler = 0;
512#if MOTION_RESAMPLING_CONSTRAINT
513      Int numMotionResamplers = 0;
514      Int refResamplingLayer[MAX_LAYERS];
515      memset( refResamplingLayer, 0, sizeof( refResamplingLayer ) );
516#endif
517      const Window &scalEL = getSPS()->getScaledRefLayerWindow(m_interLayerPredLayerIdc[i]);
518      Int scalingOffset = ((scalEL.getWindowLeftOffset()   == 0 ) && 
519                           (scalEL.getWindowRightOffset()  == 0 ) && 
520                           (scalEL.getWindowTopOffset()    == 0 ) && 
521                           (scalEL.getWindowBottomOffset() == 0 ) 
522                          );
523
524      for( i=0; i < m_activeNumILRRefIdx; i++ )
525      {
526        UInt refLayerIdc = m_interLayerPredLayerIdc[i];
527        if(!( g_posScalingFactor[refLayerIdc][0] == 65536 && g_posScalingFactor[refLayerIdc][1] == 65536 ) || (!scalingOffset)) // ratio 1x
528        {
529#if MOTION_RESAMPLING_CONSTRAINT
530          UInt predType = m_pcVPS->getDirectDependencyType( m_layerId, m_pcVPS->getRefLayerId( m_layerId, refLayerIdc ) ) + 1;
531
532          if( predType & 0x1 )
533          {
534            numResampler++;
535            refResamplingLayer[i] = refLayerIdc + 1;
536          }
537
538          if( predType & 0x2 )
539          {
540            numMotionResamplers++;
541            refResamplingLayer[i] -= refLayerIdc + 1;
542          }
543#else
544          numResampler++;
545#endif
546        }
547      }
548     
549      // When both picture sample values and picture motion field resampling processes are invoked for decoding of a particular picture, they shall be applied to the same reference layer picture.
550      if( m_activeNumILRRefIdx > 1 && numResampler > 0 )
551      {
552        for( i=0; i < m_activeNumILRRefIdx; i++ )
553        {
554          assert( refResamplingLayer[i] >= 0 && "Motion and sample inter-layer prediction shall be from the same layer" );
555        }
556      }
557
558      // Bitstream constraint for SHVC: The picture resampling process as specified in subclause G.8.1.4.1 shall not be invoked more than once for decoding of each particular picture.
559      assert(numResampler <= 1);
560#if MOTION_RESAMPLING_CONSTRAINT
561      assert( numMotionResamplers <= 1  && "Up to 1 motion resampling is allowed" );
562#endif
563    }
564#endif
565#else
566    if (numInterLayerRPSPics < m_numILRRefIdx)
567    {
568      m_numILRRefIdx = numInterLayerRPSPics;
569    }
570#endif
571  }
572#if JCTVC_M0458_INTERLAYER_RPS_SIG
573  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr + m_activeNumILRRefIdx;
574#else
575  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr + m_numILRRefIdx;
576#endif
577#else //SVC_EXTENSION
578  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr;
579#endif //SVC_EXTENSION
580#if FIX1071
581  if (checkNumPocTotalCurr)
582  {
583    // 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:
584#if ILP_RAP    // inter-layer prediction is allowed for BLA, CRA pictures of nuh_layer_id>0
585    // – If the current picture is a BLA or CRA picture with nuh_layer_id equal to 0, the value of NumPocTotalCurr shall be equal to 0.
586    // – Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
587    if (getRapPicFlag() && getLayerId()==0)
588#else
589    // – If the current picture is a BLA or CRA picture, the value of NumPocTotalCurr shall be equal to 0.
590    // – Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
591    if (getRapPicFlag())
592#endif
593    {
594      assert(numPocTotalCurr == 0);
595    }
596
597    if (m_eSliceType == I_SLICE)
598  {
599      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
600      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
601     
602      return;
603    }
604   
605    assert(numPocTotalCurr > 0);
606   
607    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
608    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
609  }
610#endif
611
612#if M0457_IL_SAMPLE_PRED_ONLY_FLAG
613    if( m_interLayerSamplePredOnlyFlag && getLayerId() ) 
614    {
615      m_aiNumRefIdx[0] = m_aiNumRefIdx[0] > m_activeNumILRRefIdx ? m_activeNumILRRefIdx : m_aiNumRefIdx[0];
616      m_aiNumRefIdx[1] = m_aiNumRefIdx[1] > m_activeNumILRRefIdx ? m_activeNumILRRefIdx : m_aiNumRefIdx[1];
617    }
618#endif
619 
620    Int cIdx = 0;
621    for ( i=0; i<NumPocStCurr0; i++, cIdx++)
622    {
623      rpsCurrList0[cIdx] = RefPicSetStCurr0[i];
624    }
625#if SVC_EXTENSION
626#if RPL_INIT_N0316_N0082
627    if( m_layerId > 0 )
628    {     
629#if JCTVC_M0458_INTERLAYER_RPS_SIG
630      for( i = 0; i < m_activeNumILRRefIdx && cIdx < numPocTotalCurr; cIdx ++, i ++)     
631#else
632      for( i = 0; i < m_numILRRefIdx && cIdx < numPocTotalCurr; cIdx ++, i ++)
633#endif
634      {
635        Int refLayerIdc = m_interLayerPredLayerIdc[i];
636        Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[refLayerIdc]->getSlice(0)->getLayerId());
637        if( ((Int)(ilpPic[refLayerIdc]->getSlice(0)->getTLayer())<=maxTidIlRefPicsPlus1-1) || (maxTidIlRefPicsPlus1==0 && ilpPic[refLayerIdc]->getSlice(0)->getRapPicFlag()) )
638        {
639          rpsCurrList0[cIdx] = ilpPic[refLayerIdc];
640        }
641      }
642    }
643#endif
644#endif //SVC_EXTENSION
645    for ( i=0; i<NumPocStCurr1; i++, cIdx++)
646    {
647      rpsCurrList0[cIdx] = RefPicSetStCurr1[i];
648    }
649    for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
650    {
651      rpsCurrList0[cIdx] = RefPicSetLtCurr[i];
652    }   
653#if !RPL_INIT_N0316_N0082
654#if SVC_EXTENSION
655    if( m_layerId > 0 )
656    {
657#if JCTVC_M0458_INTERLAYER_RPS_SIG
658      for( i = 0; i < m_activeNumILRRefIdx && cIdx < numPocTotalCurr; cIdx ++, i ++)     
659#else
660      for( i = 0; i < m_numILRRefIdx && cIdx < numPocTotalCurr; cIdx ++, i ++)
661#endif
662      {
663        Int refLayerIdc = m_interLayerPredLayerIdc[i];
664        Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[refLayerIdc]->getSlice(0)->getLayerId());
665        if( ((Int)(ilpPic[refLayerIdc]->getSlice(0)->getTLayer())<=maxTidIlRefPicsPlus1-1) || (maxTidIlRefPicsPlus1==0 && ilpPic[refLayerIdc]->getSlice(0)->getRapPicFlag()) )
666        {
667          rpsCurrList0[cIdx] = ilpPic[refLayerIdc];
668        }
669      }
670    }
671#endif
672#endif
673  assert(cIdx == numPocTotalCurr);
674
675  if (m_eSliceType==B_SLICE)
676  {
677    cIdx = 0;
678    for ( i=0; i<NumPocStCurr1; i++, cIdx++)
679    {
680      rpsCurrList1[cIdx] = RefPicSetStCurr1[i];
681    }
682    for ( i=0; i<NumPocStCurr0; i++, cIdx++)
683    {
684      rpsCurrList1[cIdx] = RefPicSetStCurr0[i];
685    }
686    for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
687    {
688      rpsCurrList1[cIdx] = RefPicSetLtCurr[i];
689    }   
690
691#if SVC_EXTENSION
692    if( m_layerId > 0 )
693    {
694#if JCTVC_M0458_INTERLAYER_RPS_SIG
695      for( i = 0; i < m_activeNumILRRefIdx && cIdx < numPocTotalCurr; cIdx ++, i ++)
696#else
697      for( i = 0; i < m_numILRRefIdx && cIdx < numPocTotalCurr; cIdx ++, i ++)
698#endif
699      {
700        Int refLayerIdc = m_interLayerPredLayerIdc[i];
701        Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[refLayerIdc]->getSlice(0)->getLayerId());
702        if( ((Int)(ilpPic[refLayerIdc]->getSlice(0)->getTLayer())<=maxTidIlRefPicsPlus1-1) || (maxTidIlRefPicsPlus1==0 && ilpPic[refLayerIdc]->getSlice(0)->getRapPicFlag()) )
703        {
704          rpsCurrList1[cIdx] = ilpPic[refLayerIdc];
705        }
706      }
707    }
708#endif //SVC_EXTENSION
709
710    assert(cIdx == numPocTotalCurr);
711  }
712
713  ::memset(m_bIsUsedAsLongTerm, 0, sizeof(m_bIsUsedAsLongTerm));
714
715  for (Int rIdx = 0; rIdx < m_aiNumRefIdx[0]; rIdx ++)
716  {
717    cIdx = m_RefPicListModification.getRefPicListModificationFlagL0() ? m_RefPicListModification.getRefPicSetIdxL0(rIdx) : rIdx % numPocTotalCurr;
718    assert(cIdx >= 0 && cIdx < numPocTotalCurr);
719    m_apcRefPicList[0][rIdx] = rpsCurrList0[ cIdx ];
720#if RPL_INIT_N0316_N0082
721    m_bIsUsedAsLongTerm[0][rIdx] = ( cIdx >= NumPocStCurr0 && cIdx < NumPocStCurr0 + m_activeNumILRRefIdx ) || ( cIdx >= NumPocStCurr0 + NumPocStCurr1 + m_activeNumILRRefIdx );
722#else
723    m_bIsUsedAsLongTerm[0][rIdx] = ( cIdx >= NumPocStCurr0 + NumPocStCurr1 );
724#endif
725  }
726  if ( m_eSliceType != B_SLICE )
727  {
728    m_aiNumRefIdx[1] = 0;
729    ::memset( m_apcRefPicList[1], 0, sizeof(m_apcRefPicList[1]));
730  }
731  else
732  {
733    for (Int rIdx = 0; rIdx < m_aiNumRefIdx[1]; rIdx ++)
734    {
735      cIdx = m_RefPicListModification.getRefPicListModificationFlagL1() ? m_RefPicListModification.getRefPicSetIdxL1(rIdx) : rIdx % numPocTotalCurr;
736      assert(cIdx >= 0 && cIdx < numPocTotalCurr);
737      m_apcRefPicList[1][rIdx] = rpsCurrList1[ cIdx ];
738      m_bIsUsedAsLongTerm[1][rIdx] = ( cIdx >= NumPocStCurr0 + NumPocStCurr1 );
739    }
740  }
741}
742
743#if SVC_EXTENSION
744Void TComSlice::setRefPicListModificationSvc()
745{
746  if( !m_pcPPS->getListsModificationPresentFlag()) 
747  {
748    return;
749  }
750
751  if(m_eNalUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP && m_eNalUnitType <= NAL_UNIT_CODED_SLICE_CRA)
752  {
753    return;
754  }
755
756  TComRefPicListModification* refPicListModification = &m_RefPicListModification;
757  Int numberOfRpsCurrTempList = this->getNumRpsCurrTempList();  // total number of ref pics in listTemp0 including inter-layer ref pics
758#if RPL_INIT_N0316_N0082
759  Int numberOfPocBeforeCurr = this->getNumNegativeRpsCurrTempList();  // number of negative temporal ref pics
760#endif
761
762#if N0147_IRAP_ALIGN_FLAG
763  assert(m_aiNumRefIdx[REF_PIC_LIST_0] > 0);
764  assert(m_aiNumRefIdx[REF_PIC_LIST_1] > 0);
765#else
766  assert(m_aiNumRefIdx[REF_PIC_LIST_0] > 1);
767  assert(m_aiNumRefIdx[REF_PIC_LIST_1] > 1);
768#endif
769
770  //set L0 inter-layer reference picture modification
771#if RPL_INIT_N0316_N0082
772  Bool hasModification = (m_aiNumRefIdx[REF_PIC_LIST_0] == (numberOfPocBeforeCurr + m_activeNumILRRefIdx)) ? false : true;
773#else
774  Bool hasModification = (m_aiNumRefIdx[REF_PIC_LIST_0] == numberOfRpsCurrTempList) ? false : true;
775#endif
776#if N0147_IRAP_ALIGN_FLAG
777  hasModification = hasModification && ( m_aiNumRefIdx[REF_PIC_LIST_0] > 1 );
778#endif
779#if FINAL_RPL_CHANGE_N0082
780  hasModification = false; //modification is not necessary
781#endif
782  refPicListModification->setRefPicListModificationFlagL0(hasModification);
783  if(hasModification)
784  { 
785    for(Int i = 0; i < min(m_aiNumRefIdx[REF_PIC_LIST_0], numberOfRpsCurrTempList); i++)
786    {
787      refPicListModification->setRefPicSetIdxL0(i, i);
788    }
789    if(m_aiNumRefIdx[REF_PIC_LIST_0] > numberOfRpsCurrTempList)
790    {
791        // repeat last ref pic when the number of active ref idx are more than RPS entries
792      for (Int i = numberOfRpsCurrTempList; i < m_aiNumRefIdx[REF_PIC_LIST_0]; i ++)
793      {
794        refPicListModification->setRefPicSetIdxL0(i, numberOfRpsCurrTempList - 1);
795      }
796    }
797    else
798    {
799#if JCTVC_M0458_INTERLAYER_RPS_SIG
800      for(Int i = m_activeNumILRRefIdx; i > 0; i-- )
801#else
802      for(Int i = m_numILRRefIdx; i > 0; i-- )
803#endif
804      {
805#if RPL_INIT_N0316_N0082
806        if((numberOfPocBeforeCurr) >= m_aiNumRefIdx[REF_PIC_LIST_0])
807        {
808          refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfPocBeforeCurr);
809        }
810        else
811        {
812          refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfPocBeforeCurr);
813          for (Int j = numberOfPocBeforeCurr; j < (m_aiNumRefIdx[REF_PIC_LIST_0] - i); j++)
814          {
815            refPicListModification->setRefPicSetIdxL0(j, j + m_activeNumILRRefIdx);
816          }
817        }
818#else
819        refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfRpsCurrTempList - i);
820#endif
821      }
822    }
823  }
824
825  //set L1 inter-layer reference picture modification
826  hasModification = (m_aiNumRefIdx[REF_PIC_LIST_1] == numberOfRpsCurrTempList) ? false : true;
827#if N0147_IRAP_ALIGN_FLAG
828  hasModification = hasModification && ( m_aiNumRefIdx[REF_PIC_LIST_1] > 1 );
829#endif
830
831  refPicListModification->setRefPicListModificationFlagL1(hasModification);
832  if(hasModification)
833  { 
834    for(Int i = 0; i < min(m_aiNumRefIdx[REF_PIC_LIST_1], numberOfRpsCurrTempList); i++)
835    {
836      refPicListModification->setRefPicSetIdxL1(i, i);
837    }
838    if(m_aiNumRefIdx[REF_PIC_LIST_1] > numberOfRpsCurrTempList)
839    {
840      for (Int i = numberOfRpsCurrTempList; i < m_aiNumRefIdx[REF_PIC_LIST_1]; i ++)
841      {
842        // repeat last ref pic when the number of active ref idx are more than RPS entries
843        refPicListModification->setRefPicSetIdxL1(i, numberOfRpsCurrTempList - 1); 
844      }
845    }
846    else
847    {
848#if JCTVC_M0458_INTERLAYER_RPS_SIG
849      for(Int i = m_activeNumILRRefIdx; i > 0; i-- )
850#else
851      for(Int i = m_numILRRefIdx; i > 0; i-- )
852#endif
853      {
854        refPicListModification->setRefPicSetIdxL1(m_aiNumRefIdx[REF_PIC_LIST_1] - i, numberOfRpsCurrTempList - i);
855      }
856    }
857  }
858  return;
859}
860#endif
861#if RPL_INIT_N0316_N0082
862Int TComSlice::getNumNegativeRpsCurrTempList()
863{
864  if( m_eSliceType == I_SLICE ) 
865  {
866    return 0;
867  }
868
869  Int numPocBeforeCurr = 0;
870  for( UInt i = 0; i < m_pcRPS->getNumberOfNegativePictures(); i++ )
871  {
872    if(m_pcRPS->getUsed(i))
873    {
874      numPocBeforeCurr++;
875    }
876  }
877
878  return numPocBeforeCurr;
879}
880#endif
881Int TComSlice::getNumRpsCurrTempList()
882{
883  Int numRpsCurrTempList = 0;
884
885#if SVC_EXTENSION
886  if( m_eSliceType == I_SLICE || ( m_pcSPS->getLayerId() && 
887    (m_eNalUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP) &&
888    (m_eNalUnitType <= NAL_UNIT_CODED_SLICE_CRA) ) )
889#else
890  if (m_eSliceType == I_SLICE) 
891#endif
892  {
893#if SVC_EXTENSION
894#if JCTVC_M0458_INTERLAYER_RPS_SIG
895    return m_activeNumILRRefIdx;
896#else
897    return getNumILRRefIdx();
898#endif
899#else
900    return 0;
901#endif
902  }
903  for(UInt i=0; i < m_pcRPS->getNumberOfNegativePictures()+ m_pcRPS->getNumberOfPositivePictures() + m_pcRPS->getNumberOfLongtermPictures(); i++)
904  {
905    if(m_pcRPS->getUsed(i))
906    {
907      numRpsCurrTempList++;
908    }
909  }
910#if SVC_EXTENSION
911  if( m_layerId > 0 )
912  {
913#if JCTVC_M0458_INTERLAYER_RPS_SIG
914    numRpsCurrTempList += m_activeNumILRRefIdx;
915#else
916    numRpsCurrTempList += getNumILRRefIdx();
917#endif
918  }
919#endif
920
921  return numRpsCurrTempList;
922}
923
924Void TComSlice::initEqualRef()
925{
926  for (Int iDir = 0; iDir < 2; iDir++)
927  {
928    for (Int iRefIdx1 = 0; iRefIdx1 < MAX_NUM_REF; iRefIdx1++)
929    {
930      for (Int iRefIdx2 = iRefIdx1; iRefIdx2 < MAX_NUM_REF; iRefIdx2++)
931      {
932        m_abEqualRef[iDir][iRefIdx1][iRefIdx2] = m_abEqualRef[iDir][iRefIdx2][iRefIdx1] = (iRefIdx1 == iRefIdx2? true : false);
933      }
934    }
935  }
936}
937
938Void TComSlice::checkColRefIdx(UInt curSliceIdx, TComPic* pic)
939{
940  Int i;
941  TComSlice* curSlice = pic->getSlice(curSliceIdx);
942  Int currColRefPOC =  curSlice->getRefPOC( RefPicList(1-curSlice->getColFromL0Flag()), curSlice->getColRefIdx());
943  TComSlice* preSlice;
944  Int preColRefPOC;
945  for(i=curSliceIdx-1; i>=0; i--)
946  {
947    preSlice = pic->getSlice(i);
948    if(preSlice->getSliceType() != I_SLICE)
949    {
950      preColRefPOC  = preSlice->getRefPOC( RefPicList(1-preSlice->getColFromL0Flag()), preSlice->getColRefIdx());
951      if(currColRefPOC != preColRefPOC)
952      {
953        printf("Collocated_ref_idx shall always be the same for all slices of a coded picture!\n");
954        exit(EXIT_FAILURE);
955      }
956      else
957      {
958        break;
959      }
960    }
961  }
962}
963
964Void TComSlice::checkCRA(TComReferencePictureSet *pReferencePictureSet, Int& pocCRA, NalUnitType& associatedIRAPType, TComList<TComPic *>& rcListPic)
965{
966  for(Int i = 0; i < pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i++)
967  {
968    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
969    {
970      assert(getPOC()+pReferencePictureSet->getDeltaPOC(i) >= pocCRA);
971    }
972  }
973  for(Int i = pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i < pReferencePictureSet->getNumberOfPictures(); i++)
974  {
975    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
976    {
977      if (!pReferencePictureSet->getCheckLTMSBPresent(i))
978      {
979        assert(xGetLongTermRefPic(rcListPic, pReferencePictureSet->getPOC(i), false)->getPOC() >= pocCRA);
980      }
981      else
982      {
983      assert(pReferencePictureSet->getPOC(i) >= pocCRA);
984    }
985  }
986  }
987  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP ) // IDR picture found
988  {
989    pocCRA = getPOC();
990    associatedIRAPType = getNalUnitType();
991  }
992  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
993  {
994    pocCRA = getPOC();
995    associatedIRAPType = getNalUnitType();
996  }
997  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
998         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
999         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP ) // BLA picture found
1000  {
1001    pocCRA = getPOC();
1002    associatedIRAPType = getNalUnitType();
1003  }
1004}
1005
1006/** Function for marking the reference pictures when an IDR/CRA/CRANT/BLA/BLANT is encountered.
1007 * \param pocCRA POC of the CRA/CRANT/BLA/BLANT picture
1008 * \param bRefreshPending flag indicating if a deferred decoding refresh is pending
1009 * \param rcListPic reference to the reference picture list
1010 * This function marks the reference pictures as "unused for reference" in the following conditions.
1011 * If the nal_unit_type is IDR/BLA/BLANT, all pictures in the reference picture list 
1012 * are marked as "unused for reference"
1013 *    If the nal_unit_type is BLA/BLANT, set the pocCRA to the temporal reference of the current picture.
1014 * Otherwise
1015 *    If the bRefreshPending flag is true (a deferred decoding refresh is pending) and the current
1016 *    temporal reference is greater than the temporal reference of the latest CRA/CRANT/BLA/BLANT picture (pocCRA),
1017 *    mark all reference pictures except the latest CRA/CRANT/BLA/BLANT picture as "unused for reference" and set
1018 *    the bRefreshPending flag to false.
1019 *    If the nal_unit_type is CRA/CRANT, set the bRefreshPending flag to true and pocCRA to the temporal
1020 *    reference of the current picture.
1021 * Note that the current picture is already placed in the reference list and its marking is not changed.
1022 * If the current picture has a nal_ref_idc that is not 0, it will remain marked as "used for reference".
1023 */
1024Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic)
1025{
1026  TComPic*                 rpcPic;
1027  setAssociatedIRAPPOC(pocCRA);
1028  Int pocCurr = getPOC(); 
1029
1030  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1031    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1032    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
1033    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
1034    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )  // IDR or BLA picture
1035  {
1036    // mark all pictures as not used for reference
1037    TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
1038    while (iterPic != rcListPic.end())
1039    {
1040      rpcPic = *(iterPic);
1041      rpcPic->setCurrSliceIdx(0);
1042      if (rpcPic->getPOC() != pocCurr) rpcPic->getSlice(0)->setReferenced(false);
1043      iterPic++;
1044    }
1045    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1046      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1047      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
1048    {
1049      pocCRA = pocCurr;
1050    }
1051  }
1052  else // CRA or No DR
1053  {
1054    if (bRefreshPending==true && pocCurr > pocCRA) // CRA reference marking pending
1055    {
1056      TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
1057      while (iterPic != rcListPic.end())
1058      {
1059        rpcPic = *(iterPic);
1060        if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != pocCRA)
1061        {
1062          rpcPic->getSlice(0)->setReferenced(false);
1063        }
1064        iterPic++;
1065      }
1066      bRefreshPending = false; 
1067    }
1068    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
1069    {
1070      bRefreshPending = true; 
1071      pocCRA = pocCurr;
1072    }
1073  }
1074}
1075
1076Void TComSlice::copySliceInfo(TComSlice *pSrc)
1077{
1078  assert( pSrc != NULL );
1079
1080  Int i, j, k;
1081
1082  m_iPOC                 = pSrc->m_iPOC;
1083  m_eNalUnitType         = pSrc->m_eNalUnitType;
1084  m_eSliceType           = pSrc->m_eSliceType;
1085  m_iSliceQp             = pSrc->m_iSliceQp;
1086#if ADAPTIVE_QP_SELECTION
1087  m_iSliceQpBase         = pSrc->m_iSliceQpBase;
1088#endif
1089  m_deblockingFilterDisable   = pSrc->m_deblockingFilterDisable;
1090  m_deblockingFilterOverrideFlag = pSrc->m_deblockingFilterOverrideFlag;
1091  m_deblockingFilterBetaOffsetDiv2 = pSrc->m_deblockingFilterBetaOffsetDiv2;
1092  m_deblockingFilterTcOffsetDiv2 = pSrc->m_deblockingFilterTcOffsetDiv2;
1093 
1094  for (i = 0; i < 2; i++)
1095  {
1096    m_aiNumRefIdx[i]     = pSrc->m_aiNumRefIdx[i];
1097  }
1098
1099  for (i = 0; i < MAX_NUM_REF; i++)
1100  {
1101    m_list1IdxToList0Idx[i] = pSrc->m_list1IdxToList0Idx[i];
1102  } 
1103  m_bCheckLDC             = pSrc->m_bCheckLDC;
1104  m_iSliceQpDelta        = pSrc->m_iSliceQpDelta;
1105  m_iSliceQpDeltaCb      = pSrc->m_iSliceQpDeltaCb;
1106  m_iSliceQpDeltaCr      = pSrc->m_iSliceQpDeltaCr;
1107  for (i = 0; i < 2; i++)
1108  {
1109    for (j = 0; j < MAX_NUM_REF; j++)
1110    {
1111      m_apcRefPicList[i][j]  = pSrc->m_apcRefPicList[i][j];
1112      m_aiRefPOCList[i][j]   = pSrc->m_aiRefPOCList[i][j];
1113    }
1114  }
1115  for (i = 0; i < 2; i++)
1116  {
1117    for (j = 0; j < MAX_NUM_REF + 1; j++)
1118    {
1119      m_bIsUsedAsLongTerm[i][j] = pSrc->m_bIsUsedAsLongTerm[i][j];
1120    }
1121  }
1122  m_iDepth               = pSrc->m_iDepth;
1123
1124  // referenced slice
1125  m_bRefenced            = pSrc->m_bRefenced;
1126
1127  // access channel
1128#if SVC_EXTENSION
1129  m_pcVPS                = pSrc->m_pcVPS;
1130  m_layerId              = pSrc->m_layerId;
1131#if JCTVC_M0458_INTERLAYER_RPS_SIG
1132  m_activeNumILRRefIdx         = pSrc->m_activeNumILRRefIdx;
1133  m_interLayerPredEnabledFlag  = pSrc->m_interLayerPredEnabledFlag;
1134  memcpy( m_interLayerPredLayerIdc, pSrc->m_interLayerPredLayerIdc, sizeof( m_interLayerPredLayerIdc ) );
1135#endif
1136#endif
1137  m_pcSPS                = pSrc->m_pcSPS;
1138  m_pcPPS                = pSrc->m_pcPPS;
1139  m_pcRPS                = pSrc->m_pcRPS;
1140  m_iLastIDR             = pSrc->m_iLastIDR;
1141
1142  m_pcPic                = pSrc->m_pcPic;
1143
1144  m_colFromL0Flag        = pSrc->m_colFromL0Flag;
1145  m_colRefIdx            = pSrc->m_colRefIdx;
1146#if SAO_CHROMA_LAMBDA
1147  m_dLambdaLuma          = pSrc->m_dLambdaLuma;
1148  m_dLambdaChroma        = pSrc->m_dLambdaChroma;
1149#else
1150  m_dLambda              = pSrc->m_dLambda;
1151#endif
1152  for (i = 0; i < 2; i++)
1153  {
1154    for (j = 0; j < MAX_NUM_REF; j++)
1155    {
1156      for (k =0; k < MAX_NUM_REF; k++)
1157      {
1158        m_abEqualRef[i][j][k] = pSrc->m_abEqualRef[i][j][k];
1159      }
1160    }
1161  }
1162
1163  m_uiTLayer                      = pSrc->m_uiTLayer;
1164  m_bTLayerSwitchingFlag          = pSrc->m_bTLayerSwitchingFlag;
1165
1166  m_sliceMode                   = pSrc->m_sliceMode;
1167  m_sliceArgument               = pSrc->m_sliceArgument;
1168  m_sliceCurStartCUAddr         = pSrc->m_sliceCurStartCUAddr;
1169  m_sliceCurEndCUAddr           = pSrc->m_sliceCurEndCUAddr;
1170  m_sliceIdx                    = pSrc->m_sliceIdx;
1171  m_sliceSegmentMode            = pSrc->m_sliceSegmentMode;
1172  m_sliceSegmentArgument        = pSrc->m_sliceSegmentArgument; 
1173  m_sliceSegmentCurStartCUAddr  = pSrc->m_sliceSegmentCurStartCUAddr;
1174  m_sliceSegmentCurEndCUAddr    = pSrc->m_sliceSegmentCurEndCUAddr;
1175  m_nextSlice                    = pSrc->m_nextSlice;
1176  m_nextSliceSegment             = pSrc->m_nextSliceSegment;
1177  for ( Int e=0 ; e<2 ; e++ )
1178  {
1179    for ( Int n=0 ; n<MAX_NUM_REF ; n++ )
1180    {
1181      memcpy(m_weightPredTable[e][n], pSrc->m_weightPredTable[e][n], sizeof(wpScalingParam)*3 );
1182    }
1183  }
1184  m_saoEnabledFlag = pSrc->m_saoEnabledFlag; 
1185  m_saoEnabledFlagChroma = pSrc->m_saoEnabledFlagChroma;
1186  m_cabacInitFlag                = pSrc->m_cabacInitFlag;
1187  m_numEntryPointOffsets  = pSrc->m_numEntryPointOffsets;
1188
1189  m_bLMvdL1Zero = pSrc->m_bLMvdL1Zero;
1190  m_LFCrossSliceBoundaryFlag = pSrc->m_LFCrossSliceBoundaryFlag;
1191  m_enableTMVPFlag                = pSrc->m_enableTMVPFlag;
1192  m_maxNumMergeCand               = pSrc->m_maxNumMergeCand;
1193}
1194
1195Int TComSlice::m_prevTid0POC = 0;
1196
1197/** Function for setting the slice's temporal layer ID and corresponding temporal_layer_switching_point_flag.
1198 * \param uiTLayer Temporal layer ID of the current slice
1199 * The decoder calls this function to set temporal_layer_switching_point_flag for each temporal layer based on
1200 * the SPS's temporal_id_nesting_flag and the parsed PPS.  Then, current slice's temporal layer ID and
1201 * temporal_layer_switching_point_flag is set accordingly.
1202 */
1203Void TComSlice::setTLayerInfo( UInt uiTLayer )
1204{
1205  m_uiTLayer = uiTLayer;
1206}
1207
1208/** Function for checking if this is a switching-point
1209*/
1210Bool TComSlice::isTemporalLayerSwitchingPoint( TComList<TComPic*>& rcListPic )
1211{
1212  TComPic* rpcPic;
1213  // loop through all pictures in the reference picture buffer
1214  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1215  while ( iterPic != rcListPic.end())
1216  {
1217    rpcPic = *(iterPic++);
1218    if(rpcPic->getSlice(0)->isReferenced() && rpcPic->getPOC() != getPOC())
1219    {
1220      if(rpcPic->getTLayer() >= getTLayer())
1221      {
1222        return false;
1223      }
1224    }
1225  }
1226  return true;
1227}
1228
1229/** Function for checking if this is a STSA candidate
1230 */
1231Bool TComSlice::isStepwiseTemporalLayerSwitchingPointCandidate( TComList<TComPic*>& rcListPic )
1232{
1233    TComPic* rpcPic;
1234   
1235    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1236    while ( iterPic != rcListPic.end())
1237    {
1238        rpcPic = *(iterPic++);
1239        if(rpcPic->getSlice(0)->isReferenced() &&  (rpcPic->getUsedByCurr()==true) && rpcPic->getPOC() != getPOC())
1240        {
1241            if(rpcPic->getTLayer() >= getTLayer())
1242            {
1243                return false;
1244            }
1245        }
1246    }
1247    return true;
1248}
1249
1250
1251Void TComSlice::checkLeadingPictureRestrictions(TComList<TComPic*>& rcListPic)
1252{
1253  TComPic* rpcPic;
1254
1255  Int nalUnitType = this->getNalUnitType();
1256
1257  // When a picture is a leading picture, it shall be a RADL or RASL picture.
1258  if(this->getAssociatedIRAPPOC() > this->getPOC())
1259  {
1260    // Do not check IRAP pictures since they may get a POC lower than their associated IRAP
1261    if(nalUnitType < NAL_UNIT_CODED_SLICE_BLA_W_LP ||
1262       nalUnitType > NAL_UNIT_RESERVED_IRAP_VCL23)
1263    {
1264      assert(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1265             nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R ||
1266             nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1267             nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R);
1268    }
1269  }
1270
1271  // When a picture is a trailing picture, it shall not be a RADL or RASL picture.
1272  if(this->getAssociatedIRAPPOC() < this->getPOC())
1273  {
1274    assert(nalUnitType != NAL_UNIT_CODED_SLICE_RASL_N &&
1275           nalUnitType != NAL_UNIT_CODED_SLICE_RASL_R &&
1276           nalUnitType != NAL_UNIT_CODED_SLICE_RADL_N &&
1277           nalUnitType != NAL_UNIT_CODED_SLICE_RADL_R);
1278  }
1279
1280  // No RASL pictures shall be present in the bitstream that are associated
1281  // with a BLA picture having nal_unit_type equal to BLA_W_RADL or BLA_N_LP.
1282  if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1283     nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1284  {
1285    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_W_RADL &&
1286           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_N_LP);
1287  }
1288
1289  // No RASL pictures shall be present in the bitstream that are associated with
1290  // an IDR picture.
1291  if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1292     nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1293  {
1294    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_N_LP   &&
1295           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_W_RADL);
1296  }
1297
1298  // No RADL pictures shall be present in the bitstream that are associated with
1299  // a BLA picture having nal_unit_type equal to BLA_N_LP or that are associated
1300  // with an IDR picture having nal_unit_type equal to IDR_N_LP.
1301  if(nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1302     nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R)
1303  {
1304    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_N_LP   &&
1305           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_N_LP);
1306  }
1307
1308  // loop through all pictures in the reference picture buffer
1309  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1310  while ( iterPic != rcListPic.end())
1311  {
1312    rpcPic = *(iterPic++);
1313    if (rpcPic->getPOC() == this->getPOC())
1314    {
1315      continue;
1316    }
1317
1318    // Any picture that has PicOutputFlag equal to 1 that precedes an IRAP picture
1319    // in decoding order shall precede the IRAP picture in output order.
1320    // (Note that any picture following in output order would be present in the DPB)
1321    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1)
1322    {
1323      if(nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP    ||
1324         nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP    ||
1325         nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL  ||
1326         nalUnitType == NAL_UNIT_CODED_SLICE_CRA         ||
1327         nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP    ||
1328         nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL)
1329      {
1330        assert(rpcPic->getPOC() < this->getPOC());
1331      }
1332    }
1333
1334    // Any picture that has PicOutputFlag equal to 1 that precedes an IRAP picture
1335    // in decoding order shall precede any RADL picture associated with the IRAP
1336    // picture in output order.
1337    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1)
1338    {
1339      if((nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1340          nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R))
1341      {
1342        // rpcPic precedes the IRAP in decoding order
1343        if(this->getAssociatedIRAPPOC() > rpcPic->getSlice(0)->getAssociatedIRAPPOC())
1344        {
1345          // rpcPic must not be the IRAP picture
1346          if(this->getAssociatedIRAPPOC() != rpcPic->getPOC())
1347          {
1348            assert(rpcPic->getPOC() < this->getPOC());
1349          }
1350        }
1351      }
1352    }
1353
1354    // When a picture is a leading picture, it shall precede, in decoding order,
1355    // all trailing pictures that are associated with the same IRAP picture.
1356    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1357       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R ||
1358       nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1359       nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R)
1360    {
1361      if(rpcPic->getSlice(0)->getAssociatedIRAPPOC() == this->getAssociatedIRAPPOC())
1362      {
1363        // rpcPic is a picture that preceded the leading in decoding order since it exist in the DPB
1364        // rpcPic would violate the constraint if it was a trailing picture
1365        assert(rpcPic->getPOC() <= this->getAssociatedIRAPPOC());
1366      }
1367    }
1368
1369    // Any RASL picture associated with a CRA or BLA picture shall precede any
1370    // RADL picture associated with the CRA or BLA picture in output order
1371    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1372       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1373    { 
1374      if((this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ||
1375          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ||
1376          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ||
1377          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_CRA)       &&
1378          this->getAssociatedIRAPPOC() == rpcPic->getSlice(0)->getAssociatedIRAPPOC())
1379      {
1380        if(rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N ||
1381           rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_R)
1382        {
1383          assert(rpcPic->getPOC() > this->getPOC());
1384        }
1385      }
1386    }
1387
1388    // Any RASL picture associated with a CRA picture shall follow, in output
1389    // order, any IRAP picture that precedes the CRA picture in decoding order.
1390    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1391       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1392    {
1393      if(this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_CRA)
1394      {
1395        if(rpcPic->getSlice(0)->getPOC() < this->getAssociatedIRAPPOC() &&
1396           (rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ||
1397            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ||
1398            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ||
1399            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP   ||
1400            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL ||
1401            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA))
1402        {
1403          assert(this->getPOC() > rpcPic->getSlice(0)->getPOC());
1404        }
1405      }
1406    }
1407  }
1408}
1409
1410/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1411*/
1412Void TComSlice::applyReferencePictureSet( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1413{
1414  TComPic* rpcPic;
1415  Int i, isReference;
1416
1417  checkLeadingPictureRestrictions(rcListPic);
1418
1419  // loop through all pictures in the reference picture buffer
1420  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1421  while ( iterPic != rcListPic.end())
1422  {
1423    rpcPic = *(iterPic++);
1424
1425    if(!rpcPic->getSlice( 0 )->isReferenced())
1426    {
1427      continue;
1428    }
1429
1430    isReference = 0;
1431    // loop through all pictures in the Reference Picture Set
1432    // to see if the picture should be kept as reference picture
1433    for(i=0;i<pReferencePictureSet->getNumberOfPositivePictures()+pReferencePictureSet->getNumberOfNegativePictures();i++)
1434    {
1435      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i))
1436      {
1437        isReference = 1;
1438        rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1439        rpcPic->setIsLongTerm(0);
1440      }
1441    }
1442    for(;i<pReferencePictureSet->getNumberOfPictures();i++)
1443    {
1444      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1445      {
1446        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i))
1447        {
1448          isReference = 1;
1449          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1450        }
1451      }
1452      else 
1453      {
1454        Int pocCycle = 1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1455        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC() & (pocCycle-1);
1456        Int refPoc = pReferencePictureSet->getPOC(i) & (pocCycle-1);
1457        if(rpcPic->getIsLongTerm() && curPoc == refPoc)
1458        {
1459          isReference = 1;
1460          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1461        }
1462      }
1463
1464    }
1465    // mark the picture as "unused for reference" if it is not in
1466    // the Reference Picture Set
1467    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && isReference == 0)   
1468    {           
1469      rpcPic->getSlice( 0 )->setReferenced( false );   
1470      rpcPic->setUsedByCurr(0);
1471      rpcPic->setIsLongTerm(0);
1472    }
1473    //check that pictures of higher temporal layers are not used
1474    assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
1475    //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
1476    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
1477    {
1478      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
1479    }
1480    //check that pictures marked as temporal layer non-reference pictures are not used for reference
1481    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && rpcPic->getTLayer()==this->getTLayer())
1482    {
1483      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getSlice( 0 )->getTemporalLayerNonReferenceFlag()==false);
1484    }
1485  }
1486}
1487
1488/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1489*/
1490Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess)
1491{
1492  TComPic* rpcPic;
1493  Int i, isAvailable;
1494  Int atLeastOneLost = 0;
1495  Int atLeastOneRemoved = 0;
1496  Int iPocLost = 0;
1497
1498  // loop through all long-term pictures in the Reference Picture Set
1499  // to see if the picture should be kept as reference picture
1500  for(i=pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i<pReferencePictureSet->getNumberOfPictures();i++)
1501  {
1502    isAvailable = 0;
1503    // loop through all pictures in the reference picture buffer
1504    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1505    while ( iterPic != rcListPic.end())
1506    {
1507      rpcPic = *(iterPic++);
1508      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1509      {
1510        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i) && rpcPic->getSlice(0)->isReferenced())
1511        {
1512          isAvailable = 1;
1513        }
1514      }
1515      else 
1516      {
1517        Int pocCycle = 1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1518        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC() & (pocCycle-1);
1519        Int refPoc = pReferencePictureSet->getPOC(i) & (pocCycle-1);
1520        if(rpcPic->getIsLongTerm() && curPoc == refPoc && rpcPic->getSlice(0)->isReferenced())
1521        {
1522          isAvailable = 1;
1523        }
1524      }
1525    }
1526    // if there was no such long-term check the short terms
1527    if(!isAvailable)
1528    {
1529      iterPic = rcListPic.begin();
1530      while ( iterPic != rcListPic.end())
1531      {
1532        rpcPic = *(iterPic++);
1533
1534        Int pocCycle = 1 << rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1535        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC();
1536        Int refPoc = pReferencePictureSet->getPOC(i);
1537        if (!pReferencePictureSet->getCheckLTMSBPresent(i))
1538        {
1539          curPoc = curPoc & (pocCycle - 1);
1540          refPoc = refPoc & (pocCycle - 1);
1541        }
1542       
1543        if (rpcPic->getSlice(0)->isReferenced() && curPoc == refPoc)
1544        {
1545          isAvailable = 1;
1546          rpcPic->setIsLongTerm(1);
1547          break;
1548        }
1549      }
1550    }
1551    // report that a picture is lost if it is in the Reference Picture Set
1552    // but not available as reference picture
1553    if(isAvailable == 0)   
1554    {           
1555      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1556      {
1557        if(!pReferencePictureSet->getUsed(i) )
1558        {
1559          if(printErrors)
1560          {
1561            printf("\nLong-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1562          }
1563          atLeastOneRemoved = 1;
1564        }
1565        else
1566        {
1567          if(printErrors)
1568          {
1569            printf("\nLong-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1570          }
1571          atLeastOneLost = 1;
1572          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1573        }
1574      }
1575    }
1576  } 
1577  // loop through all short-term pictures in the Reference Picture Set
1578  // to see if the picture should be kept as reference picture
1579  for(i=0;i<pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i++)
1580  {
1581    isAvailable = 0;
1582    // loop through all pictures in the reference picture buffer
1583    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1584    while ( iterPic != rcListPic.end())
1585    {
1586      rpcPic = *(iterPic++);
1587
1588      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1589      {
1590        isAvailable = 1;
1591      }
1592    }
1593    // report that a picture is lost if it is in the Reference Picture Set
1594    // but not available as reference picture
1595    if(isAvailable == 0)   
1596    {           
1597      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1598      {
1599        if(!pReferencePictureSet->getUsed(i) )
1600        {
1601          if(printErrors)
1602          {
1603            printf("\nShort-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1604          }
1605          atLeastOneRemoved = 1;
1606        }
1607        else
1608        {
1609          if(printErrors)
1610          {
1611            printf("\nShort-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1612          }
1613          atLeastOneLost = 1;
1614          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1615        }
1616      }
1617    }
1618  }   
1619  if(atLeastOneLost)
1620  {
1621    return iPocLost+1;
1622  }
1623  if(atLeastOneRemoved)
1624  {
1625    return -2;
1626  }
1627  else
1628  {
1629    return 0;
1630  }
1631}
1632
1633/** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
1634*/
1635#if FIX1071
1636Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool isRAP)
1637#else
1638Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1639#endif
1640{
1641  TComPic* rpcPic;
1642  Int i, j;
1643  Int k = 0;
1644  Int nrOfNegativePictures = 0;
1645  Int nrOfPositivePictures = 0;
1646  TComReferencePictureSet* pcRPS = this->getLocalRPS();
1647
1648  // loop through all pictures in the Reference Picture Set
1649  for(i=0;i<pReferencePictureSet->getNumberOfPictures();i++)
1650  {
1651    j = 0;
1652    // loop through all pictures in the reference picture buffer
1653    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1654    while ( iterPic != rcListPic.end())
1655    {
1656      j++;
1657      rpcPic = *(iterPic++);
1658
1659      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1660      {
1661        // This picture exists as a reference picture
1662        // and should be added to the explicit Reference Picture Set
1663        pcRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
1664#if FIX1071
1665        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i) && (!isRAP));
1666#else
1667        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i));
1668#endif
1669        if(pcRPS->getDeltaPOC(k) < 0)
1670        {
1671          nrOfNegativePictures++;
1672        }
1673        else
1674        {
1675          nrOfPositivePictures++;
1676        }
1677        k++;
1678      }
1679    }
1680  }
1681  pcRPS->setNumberOfNegativePictures(nrOfNegativePictures);
1682  pcRPS->setNumberOfPositivePictures(nrOfPositivePictures);
1683  pcRPS->setNumberOfPictures(nrOfNegativePictures+nrOfPositivePictures);
1684  // This is a simplistic inter rps example. A smarter encoder will look for a better reference RPS to do the
1685  // inter RPS prediction with.  Here we just use the reference used by pReferencePictureSet.
1686  // If pReferencePictureSet is not inter_RPS_predicted, then inter_RPS_prediction is for the current RPS also disabled.
1687  if (!pReferencePictureSet->getInterRPSPrediction())
1688  {
1689    pcRPS->setInterRPSPrediction(false);
1690    pcRPS->setNumRefIdc(0);
1691  }
1692  else
1693  {
1694    Int rIdx =  this->getRPSidx() - pReferencePictureSet->getDeltaRIdxMinus1() - 1;
1695    Int deltaRPS = pReferencePictureSet->getDeltaRPS();
1696    TComReferencePictureSet* pcRefRPS = this->getSPS()->getRPSList()->getReferencePictureSet(rIdx);
1697    Int iRefPics = pcRefRPS->getNumberOfPictures();
1698    Int iNewIdc=0;
1699    for(i=0; i<= iRefPics; i++) 
1700    {
1701      Int deltaPOC = ((i != iRefPics)? pcRefRPS->getDeltaPOC(i) : 0);  // check if the reference abs POC is >= 0
1702      Int iRefIdc = 0;
1703      for (j=0; j < pcRPS->getNumberOfPictures(); j++) // loop through the  pictures in the new RPS
1704      {
1705        if ( (deltaPOC + deltaRPS) == pcRPS->getDeltaPOC(j))
1706        {
1707          if (pcRPS->getUsed(j))
1708          {
1709            iRefIdc = 1;
1710          }
1711          else
1712          {
1713            iRefIdc = 2;
1714          }
1715        }
1716      }
1717      pcRPS->setRefIdc(i, iRefIdc);
1718      iNewIdc++;
1719    }
1720    pcRPS->setInterRPSPrediction(true);
1721    pcRPS->setNumRefIdc(iNewIdc);
1722    pcRPS->setDeltaRPS(deltaRPS); 
1723    pcRPS->setDeltaRIdxMinus1(pReferencePictureSet->getDeltaRIdxMinus1() + this->getSPS()->getRPSList()->getNumberOfReferencePictureSets() - this->getRPSidx());
1724  }
1725
1726  this->setRPS(pcRPS);
1727  this->setRPSidx(-1);
1728}
1729
1730/** get AC and DC values for weighted pred
1731 * \param *wp
1732 * \returns Void
1733 */
1734Void  TComSlice::getWpAcDcParam(wpACDCParam *&wp)
1735{
1736  wp = m_weightACDCParam;
1737}
1738
1739/** init AC and DC values for weighted pred
1740 * \returns Void
1741 */
1742Void  TComSlice::initWpAcDcParam()
1743{
1744  for(Int iComp = 0; iComp < 3; iComp++ )
1745  {
1746    m_weightACDCParam[iComp].iAC = 0;
1747    m_weightACDCParam[iComp].iDC = 0;
1748  }
1749}
1750
1751/** get WP tables for weighted pred
1752 * \param RefPicList
1753 * \param iRefIdx
1754 * \param *&wpScalingParam
1755 * \returns Void
1756 */
1757Void  TComSlice::getWpScaling( RefPicList e, Int iRefIdx, wpScalingParam *&wp )
1758{
1759  wp = m_weightPredTable[e][iRefIdx];
1760}
1761
1762/** reset Default WP tables settings : no weight.
1763 * \param wpScalingParam
1764 * \returns Void
1765 */
1766Void  TComSlice::resetWpScaling()
1767{
1768  for ( Int e=0 ; e<2 ; e++ )
1769  {
1770    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1771    {
1772      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1773      {
1774        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1775        pwp->bPresentFlag      = false;
1776        pwp->uiLog2WeightDenom = 0;
1777        pwp->uiLog2WeightDenom = 0;
1778        pwp->iWeight           = 1;
1779        pwp->iOffset           = 0;
1780      }
1781    }
1782  }
1783}
1784
1785/** init WP table
1786 * \returns Void
1787 */
1788Void  TComSlice::initWpScaling()
1789{
1790  for ( Int e=0 ; e<2 ; e++ )
1791  {
1792    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1793    {
1794      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1795      {
1796        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1797        if ( !pwp->bPresentFlag ) 
1798        {
1799          // Inferring values not present :
1800          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
1801          pwp->iOffset = 0;
1802        }
1803
1804        pwp->w      = pwp->iWeight;
1805        Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
1806        pwp->o      = pwp->iOffset << (bitDepth-8);
1807        pwp->shift  = pwp->uiLog2WeightDenom;
1808        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
1809      }
1810    }
1811  }
1812}
1813
1814#if REPN_FORMAT_IN_VPS
1815UInt TComSlice::getPicWidthInLumaSamples()
1816{
1817  TComSPS *sps = getSPS();
1818  TComVPS *vps = getVPS();
1819  UInt retVal, layerId = getLayerId();
1820  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
1821  {
1822    retVal = sps->getPicWidthInLumaSamples();
1823  }
1824  else
1825  {
1826    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getPicWidthVpsInLumaSamples();
1827  }
1828  return retVal;
1829}
1830UInt TComSlice::getPicHeightInLumaSamples()
1831{
1832  TComSPS *sps = getSPS();
1833  TComVPS *vps = getVPS();
1834  UInt retVal, layerId = getLayerId();
1835  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
1836  {
1837    retVal = sps->getPicHeightInLumaSamples();
1838  }
1839  else
1840  {
1841    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getPicHeightVpsInLumaSamples();
1842  }
1843  return retVal;
1844}
1845UInt TComSlice::getChromaFormatIdc()
1846{
1847  TComSPS *sps = getSPS();
1848  TComVPS *vps = getVPS();
1849  UInt retVal, layerId = getLayerId();
1850  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
1851  {
1852    retVal = sps->getChromaFormatIdc();
1853  }
1854  else
1855  {
1856    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getChromaFormatVpsIdc();
1857  }
1858  return retVal;
1859}
1860UInt TComSlice::getBitDepthY()
1861{
1862  TComSPS *sps = getSPS();
1863  TComVPS *vps = getVPS();
1864  UInt retVal, layerId = getLayerId();
1865  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
1866  {
1867    retVal = sps->getBitDepthY();
1868  }
1869  else
1870  {
1871    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getBitDepthVpsLuma();
1872  }
1873  return retVal;
1874}
1875UInt TComSlice::getBitDepthC()
1876{
1877  TComSPS *sps = getSPS();
1878  TComVPS *vps = getVPS();
1879  UInt retVal, layerId = getLayerId();
1880  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
1881  {
1882    retVal = sps->getBitDepthC();
1883  }
1884  else
1885  {
1886    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getBitDepthVpsChroma();
1887  }
1888  return retVal;
1889}
1890Int TComSlice::getQpBDOffsetY()
1891{
1892  return (getBitDepthY() - 8) * 6;
1893}
1894Int TComSlice::getQpBDOffsetC()
1895{
1896  return (getBitDepthC() - 8) * 6;
1897}
1898
1899RepFormat::RepFormat()
1900: m_chromaFormatVpsIdc          (0)
1901, m_separateColourPlaneVpsFlag  (false)
1902, m_picWidthVpsInLumaSamples    (0)
1903, m_picHeightVpsInLumaSamples   (0)
1904, m_bitDepthVpsLuma             (0)
1905, m_bitDepthVpsChroma           (0)
1906{}
1907#endif
1908
1909// ------------------------------------------------------------------------------------------------
1910// Video parameter set (VPS)
1911// ------------------------------------------------------------------------------------------------
1912#if SVC_EXTENSION
1913TComVPS::TComVPS()
1914: m_VPSId                     (  0)
1915, m_uiMaxTLayers              (  1)
1916, m_uiMaxLayers               (  1)
1917, m_bTemporalIdNestingFlag    (false)
1918, m_numHrdParameters          (  0)
1919#if !VPS_RENAME
1920, m_maxNuhReservedZeroLayerId (  0)
1921#endif
1922, m_hrdParameters             (NULL)
1923, m_hrdOpSetIdx               (NULL)
1924, m_cprmsPresentFlag          (NULL)
1925#if VPS_RENAME
1926, m_maxLayerId                (0)
1927, m_numLayerSets              (0)
1928#endif
1929#if VPS_EXTN_OP_LAYER_SETS
1930, m_numOutputLayerSets        (0) 
1931#endif
1932, m_numProfileTierLevel       (0)
1933, m_moreOutputLayerSetsThanDefaultFlag (false)
1934, m_numAddOutputLayerSets     (0)
1935, m_defaultOneTargetOutputLayerFlag    (false)
1936#if VPS_VUI_BITRATE_PICRATE
1937, m_bitRatePresentVpsFlag     (false)
1938, m_picRatePresentVpsFlag     (false)
1939#endif
1940#if REPN_FORMAT_IN_VPS
1941, m_repFormatIdxPresentFlag   (true)
1942, m_vpsNumRepFormats          (1)
1943#endif
1944#if VIEW_ID_RELATED_SIGNALING
1945, m_viewIdLenMinus1           (0)
1946#endif
1947{
1948  for( Int i = 0; i < MAX_TLAYER; i++)
1949  {
1950    m_numReorderPics[i] = 0;
1951    m_uiMaxDecPicBuffering[i] = 1; 
1952    m_uiMaxLatencyIncrease[i] = 0;
1953  }
1954#if VPS_EXTN_MASK_AND_DIM_INFO
1955  m_avcBaseLayerFlag = false;
1956  m_splittingFlag = false;
1957  ::memset(m_scalabilityMask, 0, sizeof(m_scalabilityMask));
1958  ::memset(m_dimensionIdLen, 0, sizeof(m_dimensionIdLen));
1959  m_nuhLayerIdPresentFlag = false;
1960  ::memset(m_layerIdInNuh, 0, sizeof(m_layerIdInNuh));
1961  ::memset(m_dimensionId, 0, sizeof(m_dimensionId));
1962
1963  m_numScalabilityTypes = 0;
1964  ::memset(m_layerIdInVps, 0, sizeof(m_layerIdInVps));
1965#endif
1966#if VPS_EXTN_PROFILE_INFO
1967  ::memset(m_profilePresentFlag, 0, sizeof(m_profilePresentFlag));
1968  ::memset(m_profileLayerSetRef, 0, sizeof(m_profileLayerSetRef));
1969#endif
1970#if VPS_EXTN_OP_LAYER_SETS
1971  ::memset(m_layerIdIncludedFlag, 0, sizeof(m_layerIdIncludedFlag));
1972  // Consider dynamic allocation for outputLayerSetIdx and outputLayerFlag
1973  ::memset(m_outputLayerSetIdx, 0, sizeof(m_outputLayerSetIdx));
1974  ::memset(m_outputLayerFlag, 0, sizeof(m_outputLayerFlag));
1975#endif
1976#if VPS_EXTN_DIRECT_REF_LAYERS
1977  ::memset(m_directDependencyFlag, 0, sizeof(m_directDependencyFlag));
1978  ::memset(m_numDirectRefLayers,   0, sizeof(m_numDirectRefLayers  ));
1979  ::memset(m_refLayerId,           0, sizeof(m_refLayerId          ));
1980#if M0457_PREDICTION_INDICATIONS
1981  m_directDepTypeLen = 2;
1982  ::memset(m_directDependencyType, 0, sizeof(m_directDependencyType));
1983#endif
1984#endif
1985#if DERIVE_LAYER_ID_LIST_VARIABLES
1986  ::memset(m_layerSetLayerIdList,  0, sizeof(m_layerSetLayerIdList));
1987  ::memset(m_numLayerInIdList,     0, sizeof(m_numLayerInIdList   )); 
1988#endif
1989  ::memset(m_profileLevelTierIdx,  0, sizeof(m_profileLevelTierIdx));
1990#if JCTVC_M0458_INTERLAYER_RPS_SIG
1991  m_maxOneActiveRefLayerFlag = true;
1992#endif
1993#if N0147_IRAP_ALIGN_FLAG
1994  m_crossLayerIrapAlignFlag = true;
1995#endif
1996#if JCTVC_M0203_INTERLAYER_PRED_IDC
1997#if N0120_MAX_TID_REF_PRESENT_FLAG
1998  m_maxTidRefPresentFlag = true;
1999#endif
2000  for( Int i = 0; i < MAX_VPS_LAYER_ID_PLUS1 - 1; i++)
2001  {
2002    m_maxTidIlRefPicsPlus1[i] = m_uiMaxTLayers + 1;
2003  }
2004#endif
2005#if TILE_BOUNDARY_ALIGNED_FLAG
2006   ::memset(m_tileBoundariesAlignedFlag,  0, sizeof(m_tileBoundariesAlignedFlag));
2007#endif
2008#if N0160_VUI_EXT_ILP_REF
2009   m_numIlpRestrictedRefLayers = false;
2010   ::memset(m_minSpatialSegmentOffsetPlus1,  0, sizeof(m_minSpatialSegmentOffsetPlus1));
2011   ::memset(m_ctuBasedOffsetEnabledFlag,     0, sizeof(m_ctuBasedOffsetEnabledFlag));
2012   ::memset(m_minHorizontalCtuOffsetPlus1,   0, sizeof(m_minHorizontalCtuOffsetPlus1));
2013#endif
2014#if VPS_VUI_BITRATE_PICRATE 
2015  ::memset(m_bitRatePresentFlag, 0, sizeof(m_bitRatePresentFlag));
2016  ::memset(m_picRatePresentFlag, 0, sizeof(m_picRatePresentFlag));
2017  ::memset(m_avgBitRate        , 0, sizeof(m_avgBitRate)        );
2018  ::memset(m_maxBitRate        , 0, sizeof(m_maxBitRate)        );
2019  ::memset(m_constPicRateIdc   , 0, sizeof(m_constPicRateIdc)   );
2020  ::memset(m_avgPicRate        , 0, sizeof(m_avgPicRate)        );
2021#endif
2022#if REPN_FORMAT_IN_VPS
2023  ::memset( m_vpsRepFormatIdx, 0, sizeof(m_vpsRepFormatIdx) );
2024#endif
2025#if VIEW_ID_RELATED_SIGNALING
2026  ::memset(m_viewIdVal, 0, sizeof(m_viewIdVal));
2027#endif
2028}
2029#else
2030TComVPS::TComVPS()
2031: m_VPSId                     (  0)
2032, m_uiMaxTLayers              (  1)
2033, m_uiMaxLayers               (  1)
2034, m_bTemporalIdNestingFlag    (false)
2035, m_numHrdParameters          (  0)
2036, m_maxNuhReservedZeroLayerId (  0)
2037, m_hrdParameters             (NULL)
2038, m_hrdOpSetIdx               (NULL)
2039, m_cprmsPresentFlag          (NULL)
2040{
2041  for( Int i = 0; i < MAX_TLAYER; i++)
2042  {
2043    m_numReorderPics[i] = 0;
2044    m_uiMaxDecPicBuffering[i] = 1; 
2045    m_uiMaxLatencyIncrease[i] = 0;
2046  }
2047}
2048#endif //SVC_EXTENSION
2049
2050TComVPS::~TComVPS()
2051{
2052  if( m_hrdParameters    != NULL )     delete[] m_hrdParameters;
2053  if( m_hrdOpSetIdx      != NULL )     delete[] m_hrdOpSetIdx;
2054  if( m_cprmsPresentFlag != NULL )     delete[] m_cprmsPresentFlag;
2055}
2056#if DERIVE_LAYER_ID_LIST_VARIABLES
2057Void TComVPS::deriveLayerIdListVariables()
2058{
2059  // For layer 0
2060  setNumLayersInIdList(0, 1);
2061  setLayerSetLayerIdList(0, 0, 0);
2062
2063  // For other layers
2064  Int i, m, n;
2065  for( i = 1; i <= getNumLayerSets() - 1; i++ )
2066  {
2067    n = 0;
2068    for( m = 0; m <= this->getMaxLayerId(); m++)
2069    {
2070      if(this->getLayerIdIncludedFlag(i, m))
2071      {
2072        setLayerSetLayerIdList(i, n, m);
2073        n++;
2074      }
2075    }
2076    setNumLayersInIdList(i, n);
2077  }
2078}
2079#endif
2080
2081#if IL_SL_SIGNALLING_N0371
2082Bool TComVPS::checkLayerDependency(UInt i, UInt j)
2083{
2084  if( this->getDirectDependencyFlag(i, j) == true )
2085  {                                                                                                                                   
2086    return true;                                                                                                                       
2087  }                                                                                                                                   
2088  else                                                                                                                                   
2089  {                                                                                                                                   
2090    for(UInt k=i-1; k>j; k--)
2091    {                                                                                                                               
2092      if( this->getDirectDependencyFlag(i, k) == true )
2093      {                                                                                                                           
2094        checkLayerDependency( k,j );                                                                                           
2095      }
2096    }
2097  }
2098  return false;       
2099}
2100#endif
2101
2102#if VIEW_ID_RELATED_SIGNALING
2103Int TComVPS::getNumViews()
2104{
2105  Int numViews = 1; 
2106  for( Int i = 0; i <= getMaxLayers() - 1; i++ )
2107  {
2108    Int lId = getLayerIdInNuh( i ); 
2109    if ( i > 0 && ( getViewIndex( lId ) != getScalabilityId( i - 1, VIEW_ORDER_INDEX ) ) )
2110    {
2111      numViews++; 
2112    }   
2113  }
2114
2115  return numViews;
2116}
2117Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType )
2118{
2119  return getScalabilityMask( scalType ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
2120} 
2121Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType )
2122{
2123  assert( scalType >= 0 && scalType <= MAX_VPS_NUM_SCALABILITY_TYPES ); 
2124  assert( scalType == MAX_VPS_NUM_SCALABILITY_TYPES || getScalabilityMask( scalType ) );
2125  Int scalIdx = 0; 
2126  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
2127  {
2128    scalIdx += ( getScalabilityMask( curScalType ) ? 1 : 0 );
2129
2130  }
2131
2132  return scalIdx; 
2133}
2134#endif
2135// ------------------------------------------------------------------------------------------------
2136// Sequence parameter set (SPS)
2137// ------------------------------------------------------------------------------------------------
2138
2139TComSPS::TComSPS()
2140: m_SPSId                     (  0)
2141, m_VPSId                     (  0)
2142, m_chromaFormatIdc           (CHROMA_420)
2143, m_uiMaxTLayers              (  1)
2144// Structure
2145, m_picWidthInLumaSamples     (352)
2146, m_picHeightInLumaSamples    (288)
2147, m_log2MinCodingBlockSize    (  0)
2148, m_log2DiffMaxMinCodingBlockSize (0)
2149, m_uiMaxCUWidth              ( 32)
2150, m_uiMaxCUHeight             ( 32)
2151, m_uiMaxCUDepth              (  3)
2152, m_bLongTermRefsPresent      (false)
2153, m_uiQuadtreeTULog2MaxSize   (  0)
2154, m_uiQuadtreeTULog2MinSize   (  0)
2155, m_uiQuadtreeTUMaxDepthInter (  0)
2156, m_uiQuadtreeTUMaxDepthIntra (  0)
2157// Tool list
2158, m_usePCM                   (false)
2159, m_pcmLog2MaxSize            (  5)
2160, m_uiPCMLog2MinSize          (  7)
2161, m_bitDepthY                 (  8)
2162, m_bitDepthC                 (  8)
2163, m_qpBDOffsetY               (  0)
2164, m_qpBDOffsetC               (  0)
2165, m_useLossless               (false)
2166, m_uiPCMBitDepthLuma         (  8)
2167, m_uiPCMBitDepthChroma       (  8)
2168, m_bPCMFilterDisableFlag     (false)
2169, m_uiBitsForPOC              (  8)
2170, m_numLongTermRefPicSPS    (  0) 
2171, m_uiMaxTrSize               ( 32)
2172, m_bUseSAO                   (false) 
2173, m_bTemporalIdNestingFlag    (false)
2174, m_scalingListEnabledFlag    (false)
2175, m_useStrongIntraSmoothing   (false)
2176, m_vuiParametersPresentFlag  (false)
2177, m_vuiParameters             ()
2178#if SVC_EXTENSION
2179#if M0463_VUI_EXT_ILP_REF
2180, m_interViewMvVertConstraintFlag (false)
2181, m_numIlpRestrictedRefLayers ( 0 )
2182#endif
2183, m_layerId                   ( 0 )
2184, m_numScaledRefLayerOffsets  ( 0 )
2185#if REPN_FORMAT_IN_VPS
2186, m_updateRepFormatFlag       (false)
2187#endif
2188#endif //SVC_EXTENSION
2189{
2190  for ( Int i = 0; i < MAX_TLAYER; i++ )
2191  {
2192    m_uiMaxLatencyIncrease[i] = 0;
2193    m_uiMaxDecPicBuffering[i] = 1;
2194    m_numReorderPics[i]       = 0;
2195  }
2196  m_scalingList = new TComScalingList;
2197  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
2198  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
2199#if M0463_VUI_EXT_ILP_REF
2200  for (Int i = 0; i < MAX_LAYERS; i++ )
2201  {
2202    m_minSpatialSegmentOffsetPlus1[ i ] = 0;
2203    m_ctuBasedOffsetEnabledFlag   [ i ] = false;
2204    m_minHorizontalCtuOffsetPlus1 [ i ] = 0;
2205  }
2206#endif
2207}
2208
2209TComSPS::~TComSPS()
2210{
2211  delete m_scalingList;
2212  m_RPSList.destroy();
2213}
2214
2215Void  TComSPS::createRPSList( Int numRPS )
2216{ 
2217  m_RPSList.destroy();
2218  m_RPSList.create(numRPS);
2219}
2220
2221Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
2222{
2223  if( !getVuiParametersPresentFlag() )
2224  {
2225    return;
2226  }
2227
2228  TComVUI *vui = getVuiParameters();
2229  TComHRD *hrd = vui->getHrdParameters();
2230
2231  TimingInfo *timingInfo = vui->getTimingInfo();
2232#if TIMING_INFO_NONZERO_LAYERID_SPS
2233  if( getLayerId() > 0 )
2234  {
2235    timingInfo->setTimingInfoPresentFlag( false );
2236  }
2237  else
2238  {
2239#endif
2240    timingInfo->setTimingInfoPresentFlag( true );
2241    switch( frameRate )
2242    {
2243    case 24:
2244      timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
2245      break;
2246    case 25:
2247      timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
2248      break;
2249    case 30:
2250      timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
2251      break;
2252    case 50:
2253      timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
2254      break;
2255    case 60:
2256      timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
2257      break;
2258    default:
2259      timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
2260      break;
2261    }
2262
2263    Bool rateCnt = ( bitRate > 0 );
2264    hrd->setNalHrdParametersPresentFlag( rateCnt );
2265    hrd->setVclHrdParametersPresentFlag( rateCnt );
2266
2267    hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
2268
2269    if( hrd->getSubPicCpbParamsPresentFlag() )
2270    {
2271      hrd->setTickDivisorMinus2( 100 - 2 );                          //
2272      hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
2273      hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
2274      hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
2275    }
2276    else
2277    {
2278      hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
2279    }
2280
2281    hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
2282    hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2283    hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2284 
2285    hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
2286    if( randomAccess )
2287    {
2288      hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
2289      hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
2290    }
2291    else
2292    {
2293      hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
2294      hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
2295    }
2296
2297/*
2298     Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
2299*/
2300    Int i, j;
2301    UInt birateValue, cpbSizeValue;
2302    UInt ducpbSizeValue;
2303    UInt duBitRateValue = 0;
2304
2305    for( i = 0; i < MAX_TLAYER; i ++ )
2306    {
2307      hrd->setFixedPicRateFlag( i, 1 );
2308      hrd->setPicDurationInTcMinus1( i, 0 );
2309      hrd->setLowDelayHrdFlag( i, 0 );
2310      hrd->setCpbCntMinus1( i, 0 );
2311
2312      birateValue  = bitRate;
2313      cpbSizeValue = bitRate;                                     // 1 second
2314      ducpbSizeValue = bitRate/numDU;
2315      duBitRateValue = bitRate;
2316      for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
2317      {
2318        hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
2319        hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
2320        hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
2321        hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
2322
2323        hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
2324        hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
2325        hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
2326        hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
2327        hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
2328      }
2329    }
2330#if TIMING_INFO_NONZERO_LAYERID_SPS
2331  }
2332#endif
2333}
2334const Int TComSPS::m_winUnitX[]={1,2,2,1};
2335const Int TComSPS::m_winUnitY[]={1,2,1,1};
2336
2337TComPPS::TComPPS()
2338: m_PPSId                       (0)
2339, m_SPSId                       (0)
2340, m_picInitQPMinus26            (0)
2341, m_useDQP                      (false)
2342, m_bConstrainedIntraPred       (false)
2343, m_bSliceChromaQpFlag          (false)
2344, m_pcSPS                       (NULL)
2345, m_uiMaxCuDQPDepth             (0)
2346, m_uiMinCuDQPSize              (0)
2347, m_chromaCbQpOffset            (0)
2348, m_chromaCrQpOffset            (0)
2349, m_numRefIdxL0DefaultActive    (1)
2350, m_numRefIdxL1DefaultActive    (1)
2351, m_TransquantBypassEnableFlag  (false)
2352, m_useTransformSkip             (false)
2353, m_dependentSliceSegmentsEnabledFlag    (false)
2354, m_tilesEnabledFlag               (false)
2355, m_entropyCodingSyncEnabledFlag   (false)
2356, m_loopFilterAcrossTilesEnabledFlag  (true)
2357, m_uniformSpacingFlag           (0)
2358, m_iNumColumnsMinus1            (0)
2359, m_puiColumnWidth               (NULL)
2360, m_iNumRowsMinus1               (0)
2361, m_puiRowHeight                 (NULL)
2362, m_iNumSubstreams             (1)
2363, m_signHideFlag(0)
2364, m_cabacInitPresentFlag        (false)
2365, m_encCABACTableIdx            (I_SLICE)
2366, m_sliceHeaderExtensionPresentFlag    (false)
2367, m_loopFilterAcrossSlicesEnabledFlag (false)
2368, m_listsModificationPresentFlag(  0)
2369, m_numExtraSliceHeaderBits(0)
2370{
2371  m_scalingList = new TComScalingList;
2372}
2373
2374TComPPS::~TComPPS()
2375{
2376  if( m_iNumColumnsMinus1 > 0 && m_uniformSpacingFlag == 0 )
2377  {
2378    if (m_puiColumnWidth) delete [] m_puiColumnWidth; 
2379    m_puiColumnWidth = NULL;
2380  }
2381  if( m_iNumRowsMinus1 > 0 && m_uniformSpacingFlag == 0 )
2382  {
2383    if (m_puiRowHeight) delete [] m_puiRowHeight;
2384    m_puiRowHeight = NULL;
2385  }
2386  delete m_scalingList;
2387}
2388
2389TComReferencePictureSet::TComReferencePictureSet()
2390: m_numberOfPictures (0)
2391, m_numberOfNegativePictures (0)
2392, m_numberOfPositivePictures (0)
2393, m_numberOfLongtermPictures (0)
2394, m_interRPSPrediction (0) 
2395, m_deltaRIdxMinus1 (0)   
2396, m_deltaRPS (0) 
2397, m_numRefIdc (0) 
2398{
2399  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
2400  ::memset( m_POC, 0, sizeof(m_POC) );
2401  ::memset( m_used, 0, sizeof(m_used) );
2402  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
2403}
2404
2405TComReferencePictureSet::~TComReferencePictureSet()
2406{
2407}
2408
2409Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
2410{
2411  m_used[bufferNum] = used;
2412}
2413
2414Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
2415{
2416  m_deltaPOC[bufferNum] = deltaPOC;
2417}
2418
2419Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
2420{
2421  m_numberOfPictures = numberOfPictures;
2422}
2423
2424Int TComReferencePictureSet::getUsed(Int bufferNum)
2425{
2426  return m_used[bufferNum];
2427}
2428
2429Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
2430{
2431  return m_deltaPOC[bufferNum];
2432}
2433
2434Int TComReferencePictureSet::getNumberOfPictures()
2435{
2436  return m_numberOfPictures;
2437}
2438
2439Int TComReferencePictureSet::getPOC(Int bufferNum)
2440{
2441  return m_POC[bufferNum];
2442}
2443
2444Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
2445{
2446  m_POC[bufferNum] = POC;
2447}
2448
2449Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
2450{
2451  return m_bCheckLTMSB[bufferNum];
2452}
2453
2454Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
2455{
2456  m_bCheckLTMSB[bufferNum] = b;
2457}
2458
2459/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
2460 * \param uiBufferNum
2461 * \param iRefIdc
2462 * \returns Void
2463 */
2464Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
2465{
2466  m_refIdc[bufferNum] = refIdc;
2467}
2468
2469/** get the reference idc value at uiBufferNum
2470 * \param uiBufferNum
2471 * \returns Int
2472 */
2473Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
2474{
2475  return m_refIdc[bufferNum];
2476}
2477
2478/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
2479 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
2480 *  +ve values are in increasing order.
2481 * \returns Void
2482 */
2483Void TComReferencePictureSet::sortDeltaPOC()
2484{
2485  // sort in increasing order (smallest first)
2486  for(Int j=1; j < getNumberOfPictures(); j++)
2487  { 
2488    Int deltaPOC = getDeltaPOC(j);
2489    Bool used = getUsed(j);
2490    for (Int k=j-1; k >= 0; k--)
2491    {
2492      Int temp = getDeltaPOC(k);
2493      if (deltaPOC < temp)
2494      {
2495        setDeltaPOC(k+1, temp);
2496        setUsed(k+1, getUsed(k));
2497        setDeltaPOC(k, deltaPOC);
2498        setUsed(k, used);
2499      }
2500    }
2501  }
2502  // flip the negative values to largest first
2503  Int numNegPics = getNumberOfNegativePictures();
2504  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
2505  { 
2506    Int deltaPOC = getDeltaPOC(j);
2507    Bool used = getUsed(j);
2508    setDeltaPOC(j, getDeltaPOC(k));
2509    setUsed(j, getUsed(k));
2510    setDeltaPOC(k, deltaPOC);
2511    setUsed(k, used);
2512  }
2513}
2514
2515/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
2516 *  A "*" is added to the deltaPOC value if it is Used bu current.
2517 * \returns Void
2518 */
2519Void TComReferencePictureSet::printDeltaPOC()
2520{
2521  printf("DeltaPOC = { ");
2522  for(Int j=0; j < getNumberOfPictures(); j++)
2523  {
2524    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
2525  } 
2526  if (getInterRPSPrediction()) 
2527  {
2528    printf("}, RefIdc = { ");
2529    for(Int j=0; j < getNumRefIdc(); j++)
2530    {
2531      printf("%d ", getRefIdc(j));
2532    } 
2533  }
2534  printf("}\n");
2535}
2536
2537TComRPSList::TComRPSList()
2538:m_referencePictureSets (NULL)
2539{
2540}
2541
2542TComRPSList::~TComRPSList()
2543{
2544}
2545
2546Void TComRPSList::create( Int numberOfReferencePictureSets)
2547{
2548  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2549  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
2550}
2551
2552Void TComRPSList::destroy()
2553{
2554  if (m_referencePictureSets)
2555  {
2556    delete [] m_referencePictureSets;
2557  }
2558  m_numberOfReferencePictureSets = 0;
2559  m_referencePictureSets = NULL;
2560}
2561
2562
2563
2564TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
2565{
2566  return &m_referencePictureSets[referencePictureSetNum];
2567}
2568
2569Int TComRPSList::getNumberOfReferencePictureSets()
2570{
2571  return m_numberOfReferencePictureSets;
2572}
2573
2574Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
2575{
2576  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2577}
2578
2579TComRefPicListModification::TComRefPicListModification()
2580: m_bRefPicListModificationFlagL0 (false)
2581, m_bRefPicListModificationFlagL1 (false)
2582{
2583  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
2584  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
2585}
2586
2587TComRefPicListModification::~TComRefPicListModification()
2588{
2589}
2590
2591TComScalingList::TComScalingList()
2592{
2593  init();
2594}
2595
2596TComScalingList::~TComScalingList()
2597{
2598  destroy();
2599}
2600
2601/** set default quantization matrix to array
2602*/
2603#if IL_SL_SIGNALLING_N0371
2604Void TComSlice::setDefaultScalingList( UInt layerId )
2605#else
2606Void TComSlice::setDefaultScalingList()
2607#endif
2608{
2609  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2610  {
2611    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2612    {
2613#if IL_SL_SIGNALLING_N0371
2614      getScalingList()->processDefaultMarix(sizeId, listId, layerId);
2615#else
2616      getScalingList()->processDefaultMarix(sizeId, listId);
2617#endif
2618    }
2619  }
2620}
2621/** check if use default quantization matrix
2622 * \returns true if use default quantization matrix in all size
2623*/
2624Bool TComSlice::checkDefaultScalingList()
2625{
2626  UInt defaultCounter=0;
2627
2628  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2629  {
2630    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2631    {
2632      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
2633     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
2634      {
2635        defaultCounter++;
2636      }
2637    }
2638  }
2639  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
2640}
2641
2642/** get scaling matrix from RefMatrixID
2643 * \param sizeId size index
2644 * \param Index of input matrix
2645 * \param Index of reference matrix
2646 */
2647Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
2648{
2649  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2650}
2651
2652/** parse syntax infomation
2653 *  \param pchFile syntax infomation
2654 *  \returns false if successful
2655 */
2656Bool TComScalingList::xParseScalingList(Char* pchFile)
2657{
2658  FILE *fp;
2659  Char line[1024];
2660  UInt sizeIdc,listIdc;
2661  UInt i,size = 0;
2662  Int *src=0,data;
2663  Char *ret;
2664  UInt  retval;
2665
2666  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
2667  {
2668    printf("can't open file %s :: set Default Matrix\n",pchFile);
2669    return true;
2670  }
2671
2672  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
2673  {
2674    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
2675    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
2676    {
2677      src = getScalingListAddress(sizeIdc, listIdc);
2678
2679      fseek(fp,0,0);
2680      do 
2681      {
2682        ret = fgets(line, 1024, fp);
2683        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
2684        {
2685          printf("Error: can't read Matrix :: set Default Matrix\n");
2686          return true;
2687        }
2688      }
2689      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
2690      for (i=0; i<size; i++)
2691      {
2692        retval = fscanf(fp, "%d,", &data);
2693        if (retval!=1)
2694        {
2695          printf("Error: can't read Matrix :: set Default Matrix\n");
2696          return true;
2697        }
2698        src[i] = data;
2699      }
2700      //set DC value for default matrix check
2701      setScalingListDC(sizeIdc,listIdc,src[0]);
2702
2703      if(sizeIdc > SCALING_LIST_8x8)
2704      {
2705        fseek(fp,0,0);
2706        do 
2707        {
2708          ret = fgets(line, 1024, fp);
2709          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
2710          {
2711            printf("Error: can't read DC :: set Default Matrix\n");
2712            return true;
2713          }
2714        }
2715        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
2716        retval = fscanf(fp, "%d,", &data);
2717        if (retval!=1)
2718        {
2719          printf("Error: can't read Matrix :: set Default Matrix\n");
2720          return true;
2721        }
2722        //overwrite DC value when size of matrix is larger than 16x16
2723        setScalingListDC(sizeIdc,listIdc,data);
2724      }
2725    }
2726  }
2727  fclose(fp);
2728  return false;
2729}
2730
2731/** initialization process of quantization matrix array
2732 */
2733Void TComScalingList::init()
2734{
2735  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2736  {
2737    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2738    {
2739      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
2740    }
2741  }
2742  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
2743}
2744
2745/** destroy quantization matrix array
2746 */
2747Void TComScalingList::destroy()
2748{
2749  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2750  {
2751    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2752    {
2753      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
2754    }
2755  }
2756}
2757
2758/** get default address of quantization matrix
2759 * \param sizeId size index
2760 * \param listId list index
2761 * \returns pointer of quantization matrix
2762 */
2763Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
2764{
2765  Int *src = 0;
2766  switch(sizeId)
2767  {
2768    case SCALING_LIST_4x4:
2769      src = g_quantTSDefault4x4;
2770      break;
2771    case SCALING_LIST_8x8:
2772      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2773      break;
2774    case SCALING_LIST_16x16:
2775      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2776      break;
2777    case SCALING_LIST_32x32:
2778      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2779      break;
2780    default:
2781      assert(0);
2782      src = NULL;
2783      break;
2784  }
2785  return src;
2786}
2787
2788/** process of default matrix
2789 * \param sizeId size index
2790 * \param Index of input matrix
2791 */
2792#if IL_SL_SIGNALLING_N0371
2793Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId, UInt layerId)
2794#else
2795Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId)
2796#endif
2797{
2798#if IL_SL_SIGNALLING_N0371
2799  Int i,coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
2800  UInt* scan  = (sizeId == 0) ? g_auiSigLastScan [ SCAN_DIAG ] [ 1 ] :  g_sigLastScanCG32x32; 
2801  Int *src = getScalingListDefaultAddress(sizeId, listId);   
2802#endif
2803
2804  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2805
2806#if IL_SL_SIGNALLING_N0371
2807  for(i = 0; i < coefNum; i++) 
2808  {
2809    ref_scalingListCoef[layerId][sizeId][listId][i] = src[scan[i]];
2810  }
2811#endif
2812
2813  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
2814#if IL_SL_SIGNALLING_N0371
2815  ref_scalingListDC[layerId][sizeId][listId] = SCALING_LIST_DC;
2816#endif
2817}
2818
2819/** check DC value of matrix for default matrix signaling
2820 */
2821#if IL_SL_SIGNALLING_N0371
2822Void TComScalingList::checkDcOfMatrix( UInt layerId )
2823#else
2824Void TComScalingList::checkDcOfMatrix()
2825#endif
2826{
2827  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2828  {
2829    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2830    {
2831      //check default matrix?
2832      if(getScalingListDC(sizeId,listId) == 0)
2833      {
2834#if IL_SL_SIGNALLING_N0371
2835        processDefaultMarix(sizeId, listId, layerId);
2836#else
2837        processDefaultMarix(sizeId, listId);
2838#endif
2839      }
2840    }
2841  }
2842}
2843
2844ParameterSetManager::ParameterSetManager()
2845#if SVC_EXTENSION
2846: m_spsMap(MAX_NUM_SPS)
2847, m_ppsMap(MAX_NUM_PPS)
2848, m_activeSPSId(-1)
2849, m_activePPSId(-1)
2850#else
2851: m_vpsMap(MAX_NUM_VPS)
2852, m_spsMap(MAX_NUM_SPS)
2853, m_ppsMap(MAX_NUM_PPS)
2854, m_activeVPSId(-1)
2855, m_activeSPSId(-1)
2856, m_activePPSId(-1)
2857#endif
2858{
2859}
2860
2861
2862ParameterSetManager::~ParameterSetManager()
2863{
2864}
2865
2866//! activate a SPS from a active parameter sets SEI message
2867//! \returns true, if activation is successful
2868Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
2869{
2870  TComSPS *sps = m_spsMap.getPS(spsId);
2871  if (sps)
2872  {
2873    Int vpsId = sps->getVPSId();
2874    if (m_vpsMap.getPS(vpsId))
2875    {
2876      m_activeVPSId = vpsId;
2877      m_activeSPSId = spsId;
2878      return true;
2879    }
2880    else
2881    {
2882      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
2883    }
2884  }
2885  else
2886  {
2887    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
2888  }
2889  return false;
2890}
2891
2892//! activate a PPS and depending on isIDR parameter also SPS and VPS
2893//! \returns true, if activation is successful
2894Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
2895{
2896  TComPPS *pps = m_ppsMap.getPS(ppsId);
2897  if (pps)
2898  {
2899    Int spsId = pps->getSPSId();
2900    if (!isIRAP && (spsId != m_activeSPSId))
2901    {
2902      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
2903      return false;
2904    }
2905    TComSPS *sps = m_spsMap.getPS(spsId);
2906    if (sps)
2907    {
2908      Int vpsId = sps->getVPSId();
2909      if (!isIRAP && (vpsId != m_activeVPSId))
2910      {
2911        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
2912        return false;
2913      }
2914      if (m_vpsMap.getPS(vpsId))
2915      {
2916        m_activePPSId = ppsId;
2917        m_activeVPSId = vpsId;
2918        m_activeSPSId = spsId;
2919
2920#if IL_SL_SIGNALLING_N0371
2921        activeRefPPSId[ sps->getLayerId() ] = ppsId;
2922        activeRefSPSId[ sps->getLayerId() ] = spsId;
2923#endif
2924
2925        return true;
2926      }
2927      else
2928      {
2929        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
2930      }
2931    }
2932    else
2933    {
2934      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
2935    }
2936  }
2937  else
2938  {
2939    printf("Warning: tried to activate non-existing PPS.");
2940  }
2941  return false;
2942}
2943
2944ProfileTierLevel::ProfileTierLevel()
2945  : m_profileSpace    (0)
2946  , m_tierFlag        (false)
2947  , m_profileIdc      (0)
2948  , m_levelIdc        (0)
2949, m_progressiveSourceFlag  (false)
2950, m_interlacedSourceFlag   (false)
2951, m_nonPackedConstraintFlag(false)
2952, m_frameOnlyConstraintFlag(false)
2953{
2954  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
2955}
2956#if VPS_EXTN_PROFILE_INFO
2957Void ProfileTierLevel::copyProfileInfo(ProfileTierLevel *ptl)
2958{
2959  this->setProfileSpace          ( ptl->getProfileSpace()      );
2960  this->setTierFlag              ( ptl->getTierFlag()          );
2961  this->setProfileIdc            ( ptl->getProfileIdc()        );
2962  for(Int j = 0; j < 32; j++)
2963  {
2964    this->setProfileCompatibilityFlag(j, ptl->getProfileCompatibilityFlag(j));
2965  }
2966  this->setProgressiveSourceFlag  ( ptl->getProgressiveSourceFlag()  );
2967  this->setInterlacedSourceFlag   ( ptl->getInterlacedSourceFlag()   );
2968  this->setNonPackedConstraintFlag( ptl->getNonPackedConstraintFlag());
2969  this->setFrameOnlyConstraintFlag( ptl->getFrameOnlyConstraintFlag()); 
2970}
2971#endif
2972
2973TComPTL::TComPTL()
2974{
2975  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
2976  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
2977}
2978#if VPS_EXTN_PROFILE_INFO
2979Void TComPTL::copyProfileInfo(TComPTL *ptl)
2980{
2981  // Copy all information related to general profile
2982  this->getGeneralPTL()->copyProfileInfo(ptl->getGeneralPTL());
2983}
2984#endif
2985
2986#if SVC_EXTENSION
2987#if AVC_SYNTAX
2988Void TComSlice::initBaseLayerRPL( TComSlice *pcSlice )
2989{
2990// Assumed that RPL of the base layer is same to the EL, otherwise this information should be also dumped and read from the metadata file
2991  setPOC( pcSlice->getPOC() );
2992  if( pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pcSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA )
2993  {
2994    setSliceType( I_SLICE );
2995  }
2996  else
2997  {
2998    setSliceType( pcSlice->getSliceType() );
2999  }
3000
3001  if( this->isIntra() )
3002  {
3003    return;
3004  }
3005
3006  //initialize reference POC of BL
3007  for( Int iRefPicList = 0; iRefPicList < 2; iRefPicList++ )
3008  {
3009    RefPicList eRefPicList = RefPicList( iRefPicList );
3010
3011    assert( pcSlice->getNumRefIdx( eRefPicList) >= 0 );
3012    setNumRefIdx( eRefPicList, pcSlice->getNumRefIdx( eRefPicList ) - 1 );
3013    assert( getNumRefIdx( eRefPicList) <= MAX_NUM_REF);
3014
3015    for(Int refIdx = 0; refIdx < getNumRefIdx( eRefPicList ); refIdx++) 
3016    {
3017      setRefPOC( pcSlice->getRefPic( eRefPicList, refIdx )->getPOC(), eRefPicList, refIdx );
3018      setRefPic( pcSlice->getRefPic( eRefPicList, refIdx ), eRefPicList, refIdx );
3019      /*
3020      // should be set if the base layer has its own instance of the reference picture lists, currently EL RPL is reused.
3021      getRefPic( eRefPicList, refIdx )->setLayerId( 0 );
3022      getRefPic( eRefPicList, refIdx )->setIsLongTerm( pcSlice->getRefPic( eRefPicList, refIdx )->getIsLongTerm() );     
3023      */
3024
3025    }
3026  } 
3027  return;
3028}
3029#endif
3030
3031Void TComSlice::setBaseColPic(  TComList<TComPic*>& rcListPic, UInt refLayerIdc )
3032{ 
3033  if(m_layerId == 0)
3034  {
3035    memset( m_pcBaseColPic, 0, sizeof( m_pcBaseColPic ) );
3036    return;
3037  }       
3038#if POC_RESET_FLAG
3039  if( this->getPocResetFlag() )
3040  {
3041    setBaseColPic(refLayerIdc, xGetRefPic(rcListPic, 0)); 
3042  }
3043  else
3044  {
3045    setBaseColPic(refLayerIdc, xGetRefPic(rcListPic, getPOC())); 
3046  }
3047#else
3048  setBaseColPic(refLayerIdc, xGetRefPic(rcListPic, getPOC())); 
3049#endif
3050}
3051
3052#if MFM_ENCCONSTRAINT
3053TComPic* TComSlice::getBaseColPic(  TComList<TComPic*>& rcListPic )
3054{
3055#if POC_RESET_FLAG
3056  return xGetRefPic( rcListPic, m_bPocResetFlag ? 0 : m_iPOC );
3057#else
3058  return xGetRefPic( rcListPic, m_iPOC );
3059#endif
3060}
3061#endif
3062
3063#if REF_IDX_MFM
3064Void TComSlice::setRefPOCListILP( TComPic** ilpPic, TComPic** pcRefPicRL )
3065{
3066  for( UInt i = 0; i < m_activeNumILRRefIdx; i++ )
3067  {
3068    UInt refLayerIdc = m_interLayerPredLayerIdc[i];
3069
3070    TComPic* pcRefPicBL = pcRefPicRL[refLayerIdc];
3071    //set reference picture POC of each ILP reference
3072    Int thePoc = ilpPic[refLayerIdc]->getPOC(); 
3073    assert(thePoc >= 0);
3074    assert(thePoc == pcRefPicBL->getPOC());
3075
3076    ilpPic[refLayerIdc]->getSlice(0)->setBaseColPic( refLayerIdc, pcRefPicBL );
3077
3078    //copy reference pictures marking from the reference layer
3079    ilpPic[refLayerIdc]->getSlice(0)->copySliceInfo(pcRefPicBL->getSlice(0));
3080
3081    for( Int refList = 0; refList < 2; refList++ )
3082    {
3083      RefPicList refPicList = RefPicList( refList );
3084
3085      //set reference POC of ILP
3086      ilpPic[refLayerIdc]->getSlice(0)->setNumRefIdx(refPicList, pcRefPicBL->getSlice(0)->getNumRefIdx(refPicList));
3087      assert(ilpPic[refLayerIdc]->getSlice(0)->getNumRefIdx(refPicList) >= 0);
3088      assert(ilpPic[refLayerIdc]->getSlice(0)->getNumRefIdx(refPicList) <= MAX_NUM_REF);
3089
3090      //initialize reference POC of ILP
3091      for(Int refIdx = 0; refIdx < pcRefPicBL->getSlice(0)->getNumRefIdx(refPicList); refIdx++)
3092      {
3093        ilpPic[refLayerIdc]->getSlice(0)->setRefPOC(pcRefPicBL->getSlice(0)->getRefPOC(refPicList, refIdx), refPicList, refIdx);
3094        ilpPic[refLayerIdc]->getSlice(0)->setRefPic(pcRefPicBL->getSlice(0)->getRefPic(refPicList, refIdx), refPicList, refIdx);
3095      }
3096
3097      for(Int refIdx = pcRefPicBL->getSlice(0)->getNumRefIdx(refPicList); refIdx < MAX_NUM_REF; refIdx++) 
3098      { 
3099        ilpPic[refLayerIdc]->getSlice(0)->setRefPOC(0, refPicList, refIdx); 
3100        ilpPic[refLayerIdc]->getSlice(0)->setRefPic(NULL, refPicList, refIdx); 
3101      }
3102    }
3103  }
3104  return;
3105}
3106#endif
3107
3108Void TComSlice::setILRPic(TComPic **pcIlpPic)
3109{
3110  for( Int i = 0; i < m_activeNumILRRefIdx; i++ )
3111  {
3112    Int refLayerIdc = m_interLayerPredLayerIdc[i];
3113
3114    if( pcIlpPic[refLayerIdc] )
3115    {
3116      pcIlpPic[refLayerIdc]->copyUpsampledPictureYuv( m_pcPic->getFullPelBaseRec( refLayerIdc ), pcIlpPic[refLayerIdc]->getPicYuvRec() );
3117      pcIlpPic[refLayerIdc]->getSlice(0)->setPOC( m_iPOC );
3118      pcIlpPic[refLayerIdc]->setLayerId( m_layerId ); //set reference layerId
3119      pcIlpPic[refLayerIdc]->getPicYuvRec()->setBorderExtension( false );
3120      pcIlpPic[refLayerIdc]->getPicYuvRec()->extendPicBorder();
3121      for (Int j=0; j<pcIlpPic[refLayerIdc]->getPicSym()->getNumberOfCUsInFrame(); j++)    // set reference CU layerId
3122      {
3123        pcIlpPic[refLayerIdc]->getPicSym()->getCU(j)->setLayerId( pcIlpPic[refLayerIdc]->getLayerId() );
3124      }
3125    }
3126  }
3127}
3128
3129#endif //SVC_EXTENSION
3130
3131//! \}
Note: See TracBrowser for help on using the repository browser.