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

Last change on this file since 848 was 848, checked in by qualcomm, 10 years ago

Update implementation of rep format for independent non-base layer such that it uses the rep format that is signalled in SPS, instead of in VPS.

Adoption of JCTVC-R0279. (Macro: R0279_REP_FORMAT_INBL)

From: Hendry <fhendry@…>

  • Property svn:eol-style set to native
File size: 125.1 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2014, 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);
49ParameterSetMap<TComSPS> ParameterSetManager::m_spsMap(MAX_NUM_SPS);
50ParameterSetMap<TComPPS> ParameterSetManager::m_ppsMap(MAX_NUM_PPS);
51Int ParameterSetManager::m_activeVPSId = -1;
52#endif
53
54TComSlice::TComSlice()
55: m_iPPSId                        ( -1 )
56, m_iPOC                          ( 0 )
57, m_iLastIDR                      ( 0 )
58, m_eNalUnitType                  ( NAL_UNIT_CODED_SLICE_IDR_W_RADL )
59, m_eSliceType                    ( I_SLICE )
60, m_iSliceQp                      ( 0 )
61, m_dependentSliceSegmentFlag            ( false )
62#if ADAPTIVE_QP_SELECTION
63, m_iSliceQpBase                  ( 0 )
64#endif
65, m_deblockingFilterDisable        ( false )
66, m_deblockingFilterOverrideFlag   ( false )
67, m_deblockingFilterBetaOffsetDiv2 ( 0 )
68, m_deblockingFilterTcOffsetDiv2   ( 0 )
69, m_bCheckLDC                     ( false )
70, m_iSliceQpDelta                 ( 0 )
71, m_iSliceQpDeltaCb               ( 0 )
72, m_iSliceQpDeltaCr               ( 0 )
73, m_iDepth                        ( 0 )
74, m_bRefenced                     ( false )
75, m_pcSPS                         ( NULL )
76, m_pcPPS                         ( NULL )
77, m_pcPic                         ( NULL )
78, m_colFromL0Flag                 ( 1 )
79#if SETTING_NO_OUT_PIC_PRIOR
80, m_noOutputPriorPicsFlag         ( false )
81, m_noRaslOutputFlag              ( false )
82, m_handleCraAsBlaFlag            ( false )
83#endif
84, m_colRefIdx                     ( 0 )
85, m_uiTLayer                      ( 0 )
86, m_bTLayerSwitchingFlag          ( false )
87, m_sliceMode                   ( 0 )
88, m_sliceArgument               ( 0 )
89, m_sliceCurStartCUAddr         ( 0 )
90, m_sliceCurEndCUAddr           ( 0 )
91, m_sliceIdx                    ( 0 )
92, m_sliceSegmentMode            ( 0 )
93, m_sliceSegmentArgument        ( 0 )
94, m_sliceSegmentCurStartCUAddr  ( 0 )
95, m_sliceSegmentCurEndCUAddr    ( 0 )
96, m_nextSlice                    ( false )
97, m_nextSliceSegment             ( false )
98, m_sliceBits                   ( 0 )
99, m_sliceSegmentBits         ( 0 )
100, m_bFinalized                    ( false )
101, m_uiTileOffstForMultES          ( 0 )
102, m_puiSubstreamSizes             ( NULL )
103, m_cabacInitFlag                 ( false )
104, m_bLMvdL1Zero                   ( false )
105, m_numEntryPointOffsets          ( 0 )
106, m_temporalLayerNonReferenceFlag ( false )
107, m_enableTMVPFlag                ( true )
108#if SVC_EXTENSION
109, m_layerId                     ( 0 )
110#if REF_IDX_MFM
111, m_bMFMEnabledFlag               ( false )
112#endif
113#if POC_RESET_FLAG
114, m_bPocResetFlag                 ( false )
115#endif
116, m_bDiscardableFlag              ( false )
117#if O0149_CROSS_LAYER_BLA_FLAG
118, m_bCrossLayerBLAFlag            ( false )
119#endif
120#if POC_RESET_IDC_SIGNALLING
121, m_pocResetIdc                   ( 0 )
122, m_pocResetPeriodId              ( 0 )
123, m_fullPocResetFlag              ( false )
124, m_pocLsbVal                     ( 0 )
125, m_pocMsbVal                     ( 0 )
126, m_pocMsbValRequiredFlag         ( false )
127, m_pocMsbValPresentFlag          ( false )
128#endif
129#if POC_RESET_IDC_DECODER || POC_RESET_IDC_ENCODER
130, m_picOrderCntLsb (0)
131#endif
132#endif //SVC_EXTENSION
133{
134  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = 0;
135
136#if SVC_EXTENSION
137  memset( m_pcBaseColPic, 0, sizeof( m_pcBaseColPic ) );
138  m_activeNumILRRefIdx        = 0; 
139  m_interLayerPredEnabledFlag = 0;
140  ::memset( m_interLayerPredLayerIdc, 0, sizeof(m_interLayerPredLayerIdc) );
141#if P0312_VERT_PHASE_ADJ
142  ::memset( m_vertPhasePositionFlag, 0, sizeof(m_vertPhasePositionFlag) );
143#endif
144#endif //SVC_EXTENSION
145
146  initEqualRef();
147 
148  for (Int component = 0; component < 3; component++)
149  {
150    m_lambdas[component] = 0.0;
151  }
152 
153  for ( Int idx = 0; idx < MAX_NUM_REF; idx++ )
154  {
155    m_list1IdxToList0Idx[idx] = -1;
156  }
157  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF; iNumCount++)
158  {
159    m_apcRefPicList [0][iNumCount] = NULL;
160    m_apcRefPicList [1][iNumCount] = NULL;
161    m_aiRefPOCList  [0][iNumCount] = 0;
162    m_aiRefPOCList  [1][iNumCount] = 0;
163  }
164  resetWpScaling();
165  initWpAcDcParam();
166  m_saoEnabledFlag = false;
167  m_saoEnabledFlagChroma = false;
168}
169
170TComSlice::~TComSlice()
171{
172  delete[] m_puiSubstreamSizes;
173  m_puiSubstreamSizes = NULL;
174}
175
176
177#if SVC_EXTENSION
178Void TComSlice::initSlice( UInt layerId )
179#else
180Void TComSlice::initSlice()
181#endif
182{
183#if SVC_EXTENSION
184  m_layerId = layerId;
185  m_activeNumILRRefIdx        = 0;
186  m_interLayerPredEnabledFlag = 0;
187#endif
188  m_aiNumRefIdx[0]      = 0;
189  m_aiNumRefIdx[1]      = 0;
190 
191  m_colFromL0Flag = 1;
192 
193  m_colRefIdx = 0;
194  initEqualRef();
195  m_bCheckLDC = false;
196  m_iSliceQpDeltaCb = 0;
197  m_iSliceQpDeltaCr = 0;
198
199  m_maxNumMergeCand = MRG_MAX_NUM_CANDS;
200
201  m_bFinalized=false;
202
203  m_tileByteLocation.clear();
204  m_cabacInitFlag        = false;
205  m_numEntryPointOffsets = 0;
206  m_enableTMVPFlag = true;
207#if POC_RESET_IDC_SIGNALLING
208  m_pocResetIdc                   = 0;
209  m_pocResetPeriodId              = 0;
210  m_fullPocResetFlag              = false;
211  m_pocLsbVal                     = 0;
212  m_pocMsbVal                     = 0;
213  m_pocMsbValRequiredFlag         = false;
214  m_pocMsbValPresentFlag          = false;
215#endif
216#if POC_RESET_IDC_DECODER || POC_RESET_IDC_ENCODER
217  m_picOrderCntLsb = 0;
218#endif
219}
220
221Bool TComSlice::getRapPicFlag()
222{
223  return getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
224      || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
225      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
226      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
227      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
228      || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA;
229}
230#if NO_OUTPUT_OF_PRIOR_PICS
231Bool TComSlice::getBlaPicFlag       ()
232{
233    return  getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
234    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
235    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP;
236}
237Bool TComSlice::getCraPicFlag       ()
238{
239    return getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA;
240}
241#endif
242#if POC_RESET_IDC_DECODER
243Bool TComSlice::getRaslPicFlag      ()
244{
245  return  getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_R
246  || getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N;
247}
248Bool TComSlice::getRadlPicFlag      ()
249{
250  return  getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_R
251  || getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N;
252}
253#endif
254#if POC_RESET_IDC_ENCODER
255Void TComSlice::decrementRefPocValues(Int const decrementValue)
256{
257  for(Int listNum = 0; listNum < 2; listNum++)
258  {
259    RefPicList dpbPicSliceList = (listNum == 1) ? REF_PIC_LIST_1 : REF_PIC_LIST_0;
260    for(Int listIdx = 0; listIdx < getNumRefIdx( dpbPicSliceList ); listIdx++)
261    {
262      setRefPOC( getRefPOC(dpbPicSliceList, listIdx) - decrementValue, 
263                  dpbPicSliceList,
264                  listIdx
265                  );
266    }
267  }
268}
269Int TComSlice::getCurrMsb( Int currLsb, Int prevLsb, Int prevMsb, Int maxLsbVal )
270{
271  if( prevLsb - currLsb >= (maxLsbVal >> 1) )
272  {
273    return prevMsb + maxLsbVal;
274  }
275  else if( currLsb - prevLsb > (maxLsbVal >> 1) )
276  {
277    return prevMsb - maxLsbVal;
278  }
279  else
280  {
281    return prevMsb;
282  }
283}
284#endif
285/**
286 - allocate table to contain substream sizes to be written to the slice header.
287 .
288 \param uiNumSubstreams Number of substreams -- the allocation will be this value - 1.
289 */
290Void  TComSlice::allocSubstreamSizes(UInt uiNumSubstreams)
291{
292  delete[] m_puiSubstreamSizes;
293  m_puiSubstreamSizes = new UInt[uiNumSubstreams > 0 ? uiNumSubstreams-1 : 0];
294}
295
296Void  TComSlice::sortPicList(TComList<TComPic*>& rcListPic)
297{
298  TComPic*    pcPicExtract;
299  TComPic*    pcPicInsert;
300 
301  TComList<TComPic*>::iterator    iterPicExtract;
302  TComList<TComPic*>::iterator    iterPicExtract_1;
303  TComList<TComPic*>::iterator    iterPicInsert;
304 
305  for (Int i = 1; i < (Int)(rcListPic.size()); i++)
306  {
307    iterPicExtract = rcListPic.begin();
308    for (Int j = 0; j < i; j++) iterPicExtract++;
309    pcPicExtract = *(iterPicExtract);
310    pcPicExtract->setCurrSliceIdx(0);
311   
312    iterPicInsert = rcListPic.begin();
313    while (iterPicInsert != iterPicExtract)
314    {
315      pcPicInsert = *(iterPicInsert);
316      pcPicInsert->setCurrSliceIdx(0);
317      if (pcPicInsert->getPOC() >= pcPicExtract->getPOC())
318      {
319        break;
320      }
321     
322      iterPicInsert++;
323    }
324   
325    iterPicExtract_1 = iterPicExtract;    iterPicExtract_1++;
326   
327    //  swap iterPicExtract and iterPicInsert, iterPicExtract = curr. / iterPicInsert = insertion position
328    rcListPic.insert (iterPicInsert, iterPicExtract, iterPicExtract_1);
329    rcListPic.erase  (iterPicExtract);
330  }
331}
332
333TComPic* TComSlice::xGetRefPic (TComList<TComPic*>& rcListPic,
334                                Int                 poc)
335{
336  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
337  TComPic*                      pcPic = *(iterPic);
338  while ( iterPic != rcListPic.end() )
339  {
340#if POC_RESET_IDC_ENCODER
341    if( (pcPic->getPOC() == poc) && (pcPic->getSlice(0)->isReferenced()) )
342#else
343    if(pcPic->getPOC() == poc)
344#endif
345    {
346      break;
347    }
348    iterPic++;
349#if SVC_EXTENSION
350    // return NULL, if picture with requested POC is not in the list, otherwise iterator goes outside of the list
351    if( iterPic == rcListPic.end() )
352    {
353      return NULL;
354    }
355#endif
356    pcPic = *(iterPic);
357  }
358#if POC_RESET_FLAG || POC_RESET_IDC_DECODER
359  assert( pcPic->getSlice(0)->isReferenced() );
360#endif
361  return  pcPic;
362}
363
364
365TComPic* TComSlice::xGetLongTermRefPic(TComList<TComPic*>& rcListPic, Int poc, Bool pocHasMsb)
366{
367  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
368  TComPic*                      pcPic = *(iterPic);
369  TComPic*                      pcStPic = pcPic;
370 
371  Int pocCycle = 1 << getSPS()->getBitsForPOC();
372  if (!pocHasMsb)
373  {
374    poc = poc & (pocCycle - 1);
375  }
376 
377  while ( iterPic != rcListPic.end() )
378  {
379    pcPic = *(iterPic);
380    if (pcPic && pcPic->getPOC()!=this->getPOC() && pcPic->getSlice( 0 )->isReferenced())
381    {
382      Int picPoc = pcPic->getPOC();
383      if (!pocHasMsb)
384      {
385        picPoc = picPoc & (pocCycle - 1);
386      }
387     
388#if POC_RESET_RPS
389      if( ((!pocHasMsb) && ((poc & (pocCycle-1)) == picPoc)) || ( pocHasMsb && (poc == picPoc)) )
390#else
391      if (poc == picPoc)
392#endif
393      {
394       if (pcPic->getIsLongTerm())
395      {
396        return pcPic;
397      }
398      else
399      {
400        pcStPic = pcPic;
401      }
402      break;
403    }
404    }
405
406    iterPic++;
407  }
408 
409  return  pcStPic;
410}
411
412Void TComSlice::setRefPOCList()
413{
414  for (Int iDir = 0; iDir < 2; iDir++)
415  {
416    for (Int iNumRefIdx = 0; iNumRefIdx < m_aiNumRefIdx[iDir]; iNumRefIdx++)
417    {
418      m_aiRefPOCList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getPOC();
419    }
420  }
421
422}
423
424Void TComSlice::setList1IdxToList0Idx()
425{
426  Int idxL0, idxL1;
427  for ( idxL1 = 0; idxL1 < getNumRefIdx( REF_PIC_LIST_1 ); idxL1++ )
428  {
429    m_list1IdxToList0Idx[idxL1] = -1;
430    for ( idxL0 = 0; idxL0 < getNumRefIdx( REF_PIC_LIST_0 ); idxL0++ )
431    {
432      if ( m_apcRefPicList[REF_PIC_LIST_0][idxL0]->getPOC() == m_apcRefPicList[REF_PIC_LIST_1][idxL1]->getPOC() )
433      {
434        m_list1IdxToList0Idx[idxL1] = idxL0;
435        break;
436      }
437    }
438  }
439}
440
441#if SVC_EXTENSION
442Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr, TComPic** ilpPic)
443#else
444Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr )
445#endif
446{
447  if (!checkNumPocTotalCurr)
448  {
449    if (m_eSliceType == I_SLICE)
450    {
451      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
452      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
453     
454      return;
455    }
456 
457    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
458    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
459  }
460
461  TComPic*  pcRefPic= NULL;
462  TComPic*  RefPicSetStCurr0[16];
463  TComPic*  RefPicSetStCurr1[16];
464  TComPic*  RefPicSetLtCurr[16];
465  UInt NumPocStCurr0 = 0;
466  UInt NumPocStCurr1 = 0;
467  UInt NumPocLtCurr = 0;
468  Int i;
469
470#if SVC_EXTENSION
471  if( m_layerId == 0 || ( m_layerId > 0 && ( m_activeNumILRRefIdx == 0 || !((getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP) && (getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA)) ) ) )
472  {
473#endif
474  for(i=0; i < m_pcRPS->getNumberOfNegativePictures(); i++)
475  {
476    if(m_pcRPS->getUsed(i))
477    {
478      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
479      pcRefPic->setIsLongTerm(0);
480      pcRefPic->getPicYuvRec()->extendPicBorder();
481      RefPicSetStCurr0[NumPocStCurr0] = pcRefPic;
482      NumPocStCurr0++;
483      pcRefPic->setCheckLTMSBPresent(false); 
484    }
485  }
486 
487  for(; i < m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures(); i++)
488  {
489    if(m_pcRPS->getUsed(i))
490    {
491      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
492      pcRefPic->setIsLongTerm(0);
493      pcRefPic->getPicYuvRec()->extendPicBorder();
494      RefPicSetStCurr1[NumPocStCurr1] = pcRefPic;
495      NumPocStCurr1++;
496      pcRefPic->setCheckLTMSBPresent(false); 
497    }
498  }
499 
500  for(i = m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()+m_pcRPS->getNumberOfLongtermPictures()-1; i > m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()-1 ; i--)
501  {
502    if(m_pcRPS->getUsed(i))
503    {
504      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
505      pcRefPic->setIsLongTerm(1);
506      pcRefPic->getPicYuvRec()->extendPicBorder();
507      RefPicSetLtCurr[NumPocLtCurr] = pcRefPic;
508      NumPocLtCurr++;
509    }
510    if(pcRefPic==NULL) 
511    {
512      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
513    }
514    pcRefPic->setCheckLTMSBPresent(m_pcRPS->getCheckLTMSBPresent(i)); 
515  }
516#if SVC_EXTENSION
517  }
518#endif
519
520  // ref_pic_list_init
521  TComPic*  rpsCurrList0[MAX_NUM_REF+1];
522  TComPic*  rpsCurrList1[MAX_NUM_REF+1];
523#if SVC_EXTENSION
524  Int numInterLayerRPSPics = 0;
525#if M0040_ADAPTIVE_RESOLUTION_CHANGE
526  if( m_layerId > 0 && m_activeNumILRRefIdx > 0 )
527#else
528  if( m_layerId > 0 )
529#endif
530  {
531    for( i=0; i < m_pcVPS->getNumDirectRefLayers( m_layerId ); i++ )
532    {
533#if O0225_MAX_TID_FOR_REF_LAYERS
534      Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[i]->getSlice(0)->getLayerId(),m_layerId);
535#else
536      Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[i]->getSlice(0)->getLayerId());
537#endif
538      if( ((Int)(ilpPic[i]->getSlice(0)->getTLayer())<= maxTidIlRefPicsPlus1-1) || (maxTidIlRefPicsPlus1==0 && ilpPic[i]->getSlice(0)->getRapPicFlag() ) )
539      {
540        numInterLayerRPSPics++;
541#if DISCARDABLE_PIC_RPS
542        assert( ilpPic[i]->getSlice(0)->getDiscardableFlag() == 0 );    // Inter-layer RPS shall not contain picture with discardable_flag = 1.
543#endif
544      }
545    }
546    if (numInterLayerRPSPics < m_activeNumILRRefIdx)
547    {
548      m_activeNumILRRefIdx = numInterLayerRPSPics;
549    }
550#if MAX_ONE_RESAMPLING_DIRECT_LAYERS
551    if( m_pcVPS->getScalabilityMask( SCALABILITY_ID ) )
552    {
553      Int numResampler = 0;
554#if MOTION_RESAMPLING_CONSTRAINT
555      Int numMotionResamplers = 0;
556      Int refResamplingLayer[MAX_LAYERS];
557      memset( refResamplingLayer, 0, sizeof( refResamplingLayer ) );
558#endif
559#if !RESAMPLING_CONSTRAINT_BUG_FIX
560      const Window &scalEL = getSPS()->getScaledRefLayerWindow(m_interLayerPredLayerIdc[i]);
561      Int scalingOffset = ((scalEL.getWindowLeftOffset()   == 0 ) && 
562                           (scalEL.getWindowRightOffset()  == 0 ) && 
563                           (scalEL.getWindowTopOffset()    == 0 ) && 
564                           (scalEL.getWindowBottomOffset() == 0 ) 
565                          );
566#endif
567
568      for( i=0; i < m_activeNumILRRefIdx; i++ )
569      {
570        UInt refLayerIdc = m_interLayerPredLayerIdc[i];
571        UInt refLayerId = m_pcVPS->getRefLayerId( m_layerId, refLayerIdc );
572#if RESAMPLING_CONSTRAINT_BUG_FIX
573#if O0098_SCALED_REF_LAYER_ID
574        const Window &scalEL = getSPS()->getScaledRefLayerWindowForLayer(refLayerId);
575#else
576        const Window &scalEL = getSPS()->getScaledRefLayerWindow(m_interLayerPredLayerIdc[i]);
577#endif
578        Int scalingOffset = ((scalEL.getWindowLeftOffset()   == 0 ) && 
579                             (scalEL.getWindowRightOffset()  == 0 ) && 
580                             (scalEL.getWindowTopOffset()    == 0 ) && 
581                             (scalEL.getWindowBottomOffset() == 0 ) 
582                            );
583#endif
584#if O0194_DIFFERENT_BITDEPTH_EL_BL
585        Bool sameBitDepths = ( g_bitDepthYLayer[m_layerId] == g_bitDepthYLayer[refLayerId] ) && ( g_bitDepthCLayer[m_layerId] == g_bitDepthCLayer[refLayerId] );
586
587        if( !( g_posScalingFactor[refLayerIdc][0] == 65536 && g_posScalingFactor[refLayerIdc][1] == 65536 ) || !scalingOffset || !sameBitDepths
588#if Q0048_CGS_3D_ASYMLUT
589          || getPPS()->getCGSFlag()
590#endif
591          ) // ratio 1x
592#else
593        if(!( g_posScalingFactor[refLayerIdc][0] == 65536 && g_posScalingFactor[refLayerIdc][1] == 65536 ) || (!scalingOffset)) // ratio 1x
594#endif
595        {
596#if MOTION_RESAMPLING_CONSTRAINT
597          UInt predType = m_pcVPS->getDirectDependencyType( m_layerId, refLayerId ) + 1;
598
599          if( predType & 0x1 )
600          {
601            numResampler++;
602            refResamplingLayer[i] = refLayerIdc + 1;
603          }
604
605          if( predType & 0x2 )
606          {
607            numMotionResamplers++;
608            refResamplingLayer[i] -= refLayerIdc + 1;
609          }
610#else
611          numResampler++;
612#endif
613        }
614      }
615     
616      // 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.
617      if( m_activeNumILRRefIdx > 1 && numResampler > 0 )
618      {
619        for( i=0; i < m_activeNumILRRefIdx; i++ )
620        {
621          assert( refResamplingLayer[i] >= 0 && "Motion and sample inter-layer prediction shall be from the same layer" );
622        }
623      }
624
625      // 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.
626      assert(numResampler <= 1);
627#if MOTION_RESAMPLING_CONSTRAINT
628      assert( numMotionResamplers <= 1  && "Up to 1 motion resampling is allowed" );
629#endif
630    }
631#endif
632  }
633  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr + m_activeNumILRRefIdx;
634#else //SVC_EXTENSION
635  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr;
636#endif //SVC_EXTENSION
637
638  if (checkNumPocTotalCurr)
639  {
640    // 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:
641#if SVC_EXTENSION    // inter-layer prediction is allowed for BLA, CRA pictures of nuh_layer_id>0
642    // - 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.
643    // - Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
644    if (getRapPicFlag() && getLayerId()==0)
645#else
646    // - If the current picture is a BLA or CRA picture, the value of NumPocTotalCurr shall be equal to 0.
647    // - Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
648    if (getRapPicFlag())
649#endif
650    {
651      assert(numPocTotalCurr == 0);
652    }
653
654    if (m_eSliceType == I_SLICE)
655    {
656      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
657      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
658
659      return;
660    }
661
662    assert(numPocTotalCurr > 0);
663
664    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
665    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
666  }
667
668    Int cIdx = 0;
669    for ( i=0; i<NumPocStCurr0; i++, cIdx++)
670    {
671      rpsCurrList0[cIdx] = RefPicSetStCurr0[i];
672    }
673#if SVC_EXTENSION
674#if RPL_INIT_N0316_N0082
675    if( m_layerId > 0 )
676    {     
677      for( i = 0; i < m_activeNumILRRefIdx && cIdx < numPocTotalCurr; cIdx ++, i ++)     
678      {
679        Int refLayerIdc = m_interLayerPredLayerIdc[i];
680#if O0225_MAX_TID_FOR_REF_LAYERS
681        Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[refLayerIdc]->getSlice(0)->getLayerId(),m_layerId);
682#else
683        Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[refLayerIdc]->getSlice(0)->getLayerId());
684#endif
685        if( ((Int)(ilpPic[refLayerIdc]->getSlice(0)->getTLayer())<=maxTidIlRefPicsPlus1-1) || (maxTidIlRefPicsPlus1==0 && ilpPic[refLayerIdc]->getSlice(0)->getRapPicFlag()) )
686        {
687          rpsCurrList0[cIdx] = ilpPic[refLayerIdc];
688        }
689      }
690    }
691#endif
692#endif //SVC_EXTENSION
693    for ( i=0; i<NumPocStCurr1; i++, cIdx++)
694    {
695      rpsCurrList0[cIdx] = RefPicSetStCurr1[i];
696    }
697    for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
698    {
699      rpsCurrList0[cIdx] = RefPicSetLtCurr[i];
700    }   
701#if !RPL_INIT_N0316_N0082
702#if SVC_EXTENSION
703    if( m_layerId > 0 )
704    {
705      for( i = 0; i < m_activeNumILRRefIdx && cIdx < numPocTotalCurr; cIdx ++, i ++)     
706      {
707        Int refLayerIdc = m_interLayerPredLayerIdc[i];
708#if O0225_MAX_TID_FOR_REF_LAYERS
709        Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[refLayerIdc]->getSlice(0)->getLayerId(),m_layerId);
710#else
711        Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[refLayerIdc]->getSlice(0)->getLayerId());
712#endif
713        if( ((Int)(ilpPic[refLayerIdc]->getSlice(0)->getTLayer())<=maxTidIlRefPicsPlus1-1) || (maxTidIlRefPicsPlus1==0 && ilpPic[refLayerIdc]->getSlice(0)->getRapPicFlag()) )
714        {
715          rpsCurrList0[cIdx] = ilpPic[refLayerIdc];
716        }
717      }
718    }
719#endif
720#endif
721  assert(cIdx == numPocTotalCurr);
722
723  if (m_eSliceType==B_SLICE)
724  {
725    cIdx = 0;
726    for ( i=0; i<NumPocStCurr1; i++, cIdx++)
727    {
728      rpsCurrList1[cIdx] = RefPicSetStCurr1[i];
729    }
730    for ( i=0; i<NumPocStCurr0; i++, cIdx++)
731    {
732      rpsCurrList1[cIdx] = RefPicSetStCurr0[i];
733    }
734    for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
735    {
736      rpsCurrList1[cIdx] = RefPicSetLtCurr[i];
737    }   
738
739#if SVC_EXTENSION
740    if( m_layerId > 0 )
741    {
742      for( i = 0; i < m_activeNumILRRefIdx && cIdx < numPocTotalCurr; cIdx ++, i ++)
743      {
744        Int refLayerIdc = m_interLayerPredLayerIdc[i];
745#if O0225_MAX_TID_FOR_REF_LAYERS
746        Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[refLayerIdc]->getSlice(0)->getLayerId(),m_layerId);
747#else
748        Int maxTidIlRefPicsPlus1 = getVPS()->getMaxTidIlRefPicsPlus1(ilpPic[refLayerIdc]->getSlice(0)->getLayerId());
749#endif
750        if( ((Int)(ilpPic[refLayerIdc]->getSlice(0)->getTLayer())<=maxTidIlRefPicsPlus1-1) || (maxTidIlRefPicsPlus1==0 && ilpPic[refLayerIdc]->getSlice(0)->getRapPicFlag()) )
751        {
752          rpsCurrList1[cIdx] = ilpPic[refLayerIdc];
753        }
754      }
755    }
756#endif //SVC_EXTENSION
757
758    assert(cIdx == numPocTotalCurr);
759  }
760
761  ::memset(m_bIsUsedAsLongTerm, 0, sizeof(m_bIsUsedAsLongTerm));
762
763  for (Int rIdx = 0; rIdx < m_aiNumRefIdx[0]; rIdx ++)
764  {
765    cIdx = m_RefPicListModification.getRefPicListModificationFlagL0() ? m_RefPicListModification.getRefPicSetIdxL0(rIdx) : rIdx % numPocTotalCurr;
766    assert(cIdx >= 0 && cIdx < numPocTotalCurr);
767    m_apcRefPicList[0][rIdx] = rpsCurrList0[ cIdx ];
768#if RPL_INIT_N0316_N0082
769    m_bIsUsedAsLongTerm[0][rIdx] = ( cIdx >= NumPocStCurr0 && cIdx < NumPocStCurr0 + m_activeNumILRRefIdx ) || ( cIdx >= NumPocStCurr0 + NumPocStCurr1 + m_activeNumILRRefIdx );
770#else
771    m_bIsUsedAsLongTerm[0][rIdx] = ( cIdx >= NumPocStCurr0 + NumPocStCurr1 );
772#endif
773  }
774  if ( m_eSliceType != B_SLICE )
775  {
776    m_aiNumRefIdx[1] = 0;
777    ::memset( m_apcRefPicList[1], 0, sizeof(m_apcRefPicList[1]));
778  }
779  else
780  {
781    for (Int rIdx = 0; rIdx < m_aiNumRefIdx[1]; rIdx ++)
782    {
783      cIdx = m_RefPicListModification.getRefPicListModificationFlagL1() ? m_RefPicListModification.getRefPicSetIdxL1(rIdx) : rIdx % numPocTotalCurr;
784      assert(cIdx >= 0 && cIdx < numPocTotalCurr);
785      m_apcRefPicList[1][rIdx] = rpsCurrList1[ cIdx ];
786      m_bIsUsedAsLongTerm[1][rIdx] = ( cIdx >= NumPocStCurr0 + NumPocStCurr1 );
787    }
788  }
789}
790
791#if SVC_EXTENSION
792Void TComSlice::setRefPicListModificationSvc()
793{
794  if( !m_pcPPS->getListsModificationPresentFlag()) 
795  {
796    return;
797  }
798
799  if(m_eNalUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP && m_eNalUnitType <= NAL_UNIT_CODED_SLICE_CRA)
800  {
801    return;
802  }
803
804  TComRefPicListModification* refPicListModification = &m_RefPicListModification;
805  Int numberOfRpsCurrTempList = this->getNumRpsCurrTempList();  // total number of ref pics in listTemp0 including inter-layer ref pics
806#if RPL_INIT_N0316_N0082
807  Int numberOfPocBeforeCurr = this->getNumNegativeRpsCurrTempList();  // number of negative temporal ref pics
808#endif
809
810  assert(m_aiNumRefIdx[REF_PIC_LIST_0] > 0);
811  assert(m_aiNumRefIdx[REF_PIC_LIST_1] > 0);
812
813  //set L0 inter-layer reference picture modification
814#if RPL_INIT_N0316_N0082
815  Bool hasModification = (m_aiNumRefIdx[REF_PIC_LIST_0] == (numberOfPocBeforeCurr + m_activeNumILRRefIdx)) ? false : true;
816
817  if( m_activeNumILRRefIdx > 1 )
818  {
819    hasModification = (m_aiNumRefIdx[REF_PIC_LIST_0] >= (numberOfPocBeforeCurr + m_activeNumILRRefIdx)) ? false : true;
820  }
821#else
822  Bool hasModification = (m_aiNumRefIdx[REF_PIC_LIST_0] == numberOfRpsCurrTempList) ? false : true;
823#endif
824  hasModification = hasModification && ( m_aiNumRefIdx[REF_PIC_LIST_0] > 1 );
825  refPicListModification->setRefPicListModificationFlagL0(hasModification);
826  if(hasModification)
827  { 
828    for(Int i = 0; i < min(m_aiNumRefIdx[REF_PIC_LIST_0], numberOfRpsCurrTempList); i++)
829    {
830      refPicListModification->setRefPicSetIdxL0(i, i);
831    }
832
833    if(m_aiNumRefIdx[REF_PIC_LIST_0] > numberOfRpsCurrTempList)
834    {
835      // repeat last ref pic when the number of active ref idx are more than RPS entries
836      for (Int i = numberOfRpsCurrTempList; i < m_aiNumRefIdx[REF_PIC_LIST_0]; i ++)
837      {
838        refPicListModification->setRefPicSetIdxL0(i, numberOfRpsCurrTempList - 1);
839      }
840    }
841    else
842    {
843      // number of ILRPs included into the reference picture list with the list modification
844      Int includeNumILRP = min( max(1, m_aiNumRefIdx[REF_PIC_LIST_0]-numberOfPocBeforeCurr), m_activeNumILRRefIdx);
845
846      for(Int i = includeNumILRP; i > 0; i-- )
847      {
848#if RPL_INIT_N0316_N0082
849        if((numberOfPocBeforeCurr) >= m_aiNumRefIdx[REF_PIC_LIST_0])
850        {
851          refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfPocBeforeCurr + includeNumILRP - i);
852        }
853        else
854        {
855          refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfPocBeforeCurr + includeNumILRP - i);
856          for (Int j = numberOfPocBeforeCurr; j < (m_aiNumRefIdx[REF_PIC_LIST_0] - i); j++)
857          {
858            assert( j + includeNumILRP < numberOfRpsCurrTempList );
859            refPicListModification->setRefPicSetIdxL0(j, j + includeNumILRP);
860          }
861        }
862#else
863        refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfRpsCurrTempList - i);
864#endif
865      }
866    }
867  }
868
869  //set L1 inter-layer reference picture modification
870  hasModification = (m_aiNumRefIdx[REF_PIC_LIST_1] >= numberOfRpsCurrTempList) ? false : true;
871  hasModification = hasModification && ( m_aiNumRefIdx[REF_PIC_LIST_1] > 1 );
872
873  refPicListModification->setRefPicListModificationFlagL1(hasModification);
874  if(hasModification)
875  { 
876    for(Int i = 0; i < min(m_aiNumRefIdx[REF_PIC_LIST_1], numberOfRpsCurrTempList); i++)
877    {
878      refPicListModification->setRefPicSetIdxL1(i, i);
879    }
880    if(m_aiNumRefIdx[REF_PIC_LIST_1] > numberOfRpsCurrTempList)
881    {
882      for (Int i = numberOfRpsCurrTempList; i < m_aiNumRefIdx[REF_PIC_LIST_1]; i ++)
883      {
884        // repeat last ref pic when the number of active ref idx are more than RPS entries
885        refPicListModification->setRefPicSetIdxL1(i, numberOfRpsCurrTempList - 1); 
886      }
887    }
888    else
889    {
890      Int includeNumILRP = min(m_aiNumRefIdx[REF_PIC_LIST_1], m_activeNumILRRefIdx);
891
892      for(Int i = includeNumILRP; i > 0; i-- )
893      {
894        refPicListModification->setRefPicSetIdxL1(m_aiNumRefIdx[REF_PIC_LIST_1] - i, numberOfRpsCurrTempList - i);
895      }
896    }
897  }
898  return;
899}
900#endif
901#if RPL_INIT_N0316_N0082
902Int TComSlice::getNumNegativeRpsCurrTempList()
903{
904  if( m_eSliceType == I_SLICE ) 
905  {
906    return 0;
907  }
908
909  Int numPocBeforeCurr = 0;
910  for( UInt i = 0; i < m_pcRPS->getNumberOfNegativePictures(); i++ )
911  {
912    if(m_pcRPS->getUsed(i))
913    {
914      numPocBeforeCurr++;
915    }
916  }
917
918  return numPocBeforeCurr;
919}
920#endif
921Int TComSlice::getNumRpsCurrTempList()
922{
923  Int numRpsCurrTempList = 0;
924
925#if SVC_EXTENSION
926  if( m_eSliceType == I_SLICE || ( m_layerId && 
927    (m_eNalUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP) &&
928    (m_eNalUnitType <= NAL_UNIT_CODED_SLICE_CRA) ) )
929#else
930  if (m_eSliceType == I_SLICE) 
931#endif
932  {
933#if SVC_EXTENSION
934    return m_activeNumILRRefIdx;
935#else
936    return 0;
937#endif
938  }
939  for(UInt i=0; i < m_pcRPS->getNumberOfNegativePictures()+ m_pcRPS->getNumberOfPositivePictures() + m_pcRPS->getNumberOfLongtermPictures(); i++)
940  {
941    if(m_pcRPS->getUsed(i))
942    {
943      numRpsCurrTempList++;
944    }
945  }
946#if SVC_EXTENSION
947  if( m_layerId > 0 )
948  {
949    numRpsCurrTempList += m_activeNumILRRefIdx;
950  }
951#endif
952
953  return numRpsCurrTempList;
954}
955
956Void TComSlice::initEqualRef()
957{
958  for (Int iDir = 0; iDir < 2; iDir++)
959  {
960    for (Int iRefIdx1 = 0; iRefIdx1 < MAX_NUM_REF; iRefIdx1++)
961    {
962      for (Int iRefIdx2 = iRefIdx1; iRefIdx2 < MAX_NUM_REF; iRefIdx2++)
963      {
964        m_abEqualRef[iDir][iRefIdx1][iRefIdx2] = m_abEqualRef[iDir][iRefIdx2][iRefIdx1] = (iRefIdx1 == iRefIdx2? true : false);
965      }
966    }
967  }
968}
969
970Void TComSlice::checkColRefIdx(UInt curSliceIdx, TComPic* pic)
971{
972  Int i;
973  TComSlice* curSlice = pic->getSlice(curSliceIdx);
974  Int currColRefPOC =  curSlice->getRefPOC( RefPicList(1-curSlice->getColFromL0Flag()), curSlice->getColRefIdx());
975  TComSlice* preSlice;
976  Int preColRefPOC;
977  for(i=curSliceIdx-1; i>=0; i--)
978  {
979    preSlice = pic->getSlice(i);
980    if(preSlice->getSliceType() != I_SLICE)
981    {
982      preColRefPOC  = preSlice->getRefPOC( RefPicList(1-preSlice->getColFromL0Flag()), preSlice->getColRefIdx());
983      if(currColRefPOC != preColRefPOC)
984      {
985        printf("Collocated_ref_idx shall always be the same for all slices of a coded picture!\n");
986        exit(EXIT_FAILURE);
987      }
988      else
989      {
990        break;
991      }
992    }
993  }
994}
995
996Void TComSlice::checkCRA(TComReferencePictureSet *pReferencePictureSet, Int& pocCRA, NalUnitType& associatedIRAPType, TComList<TComPic *>& rcListPic)
997{
998  for(Int i = 0; i < pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i++)
999  {
1000    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
1001    {
1002      assert(getPOC()+pReferencePictureSet->getDeltaPOC(i) >= pocCRA);
1003    }
1004  }
1005  for(Int i = pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i < pReferencePictureSet->getNumberOfPictures(); i++)
1006  {
1007    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
1008    {
1009      if (!pReferencePictureSet->getCheckLTMSBPresent(i))
1010      {
1011        assert(xGetLongTermRefPic(rcListPic, pReferencePictureSet->getPOC(i), false)->getPOC() >= pocCRA);
1012      }
1013      else
1014      {
1015      assert(pReferencePictureSet->getPOC(i) >= pocCRA);
1016    }
1017  }
1018  }
1019  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP ) // IDR picture found
1020  {
1021    pocCRA = getPOC();
1022    associatedIRAPType = getNalUnitType();
1023  }
1024  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
1025  {
1026    pocCRA = getPOC();
1027    associatedIRAPType = getNalUnitType();
1028  }
1029  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1030         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1031         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP ) // BLA picture found
1032  {
1033    pocCRA = getPOC();
1034    associatedIRAPType = getNalUnitType();
1035  }
1036}
1037
1038/** Function for marking the reference pictures when an IDR/CRA/CRANT/BLA/BLANT is encountered.
1039 * \param pocCRA POC of the CRA/CRANT/BLA/BLANT picture
1040 * \param bRefreshPending flag indicating if a deferred decoding refresh is pending
1041 * \param rcListPic reference to the reference picture list
1042 * This function marks the reference pictures as "unused for reference" in the following conditions.
1043 * If the nal_unit_type is IDR/BLA/BLANT, all pictures in the reference picture list 
1044 * are marked as "unused for reference"
1045 *    If the nal_unit_type is BLA/BLANT, set the pocCRA to the temporal reference of the current picture.
1046 * Otherwise
1047 *    If the bRefreshPending flag is true (a deferred decoding refresh is pending) and the current
1048 *    temporal reference is greater than the temporal reference of the latest CRA/CRANT/BLA/BLANT picture (pocCRA),
1049 *    mark all reference pictures except the latest CRA/CRANT/BLA/BLANT picture as "unused for reference" and set
1050 *    the bRefreshPending flag to false.
1051 *    If the nal_unit_type is CRA/CRANT, set the bRefreshPending flag to true and pocCRA to the temporal
1052 *    reference of the current picture.
1053 * Note that the current picture is already placed in the reference list and its marking is not changed.
1054 * If the current picture has a nal_ref_idc that is not 0, it will remain marked as "used for reference".
1055 */
1056#if NO_CLRAS_OUTPUT_FLAG
1057Void TComSlice::decodingRefreshMarking( TComList<TComPic*>& rcListPic, Bool noClrasOutputFlag )
1058{
1059  if( !isIRAP() )
1060  {
1061    return;
1062  }
1063
1064  Int pocCurr = getPOC();
1065  TComPic* rpcPic = NULL;
1066
1067  // When the current picture is an IRAP picture with nuh_layer_id equal to 0 and NoClrasOutputFlag is equal to 1,
1068  // all reference pictures with any value of nuh_layer_id currently in the DPB (if any) are marked as "unused for reference".
1069  if( m_layerId == 0 && noClrasOutputFlag )
1070  {
1071    // mark all pictures for all layers as not used for reference
1072    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1073    while( iterPic != rcListPic.end() )
1074    {
1075      rpcPic = *(iterPic);
1076      if( rpcPic->getPOC() != pocCurr )
1077      {
1078        rpcPic->getSlice(0)->setReferenced(false);
1079      }
1080      iterPic++;
1081    }
1082  }
1083
1084  // When the current picture is an IRAP picture with NoRaslOutputFlag equal to 1,
1085  // all reference pictures with nuh_layer_id equal to currPicLayerId currently in the DPB (if any) are marked as "unused for reference".
1086  if( m_noRaslOutputFlag )
1087  {
1088    // mark all pictures of a current layer as not used for reference
1089    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1090    while( iterPic != rcListPic.end() )
1091    {
1092      rpcPic = *(iterPic);
1093      if( rpcPic->getPOC() != pocCurr && rpcPic->getLayerId() == m_layerId )
1094      {
1095        rpcPic->getSlice(0)->setReferenced(false);
1096      }
1097      iterPic++;
1098    }
1099  }
1100}
1101
1102Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic, Bool noClrasOutputFlag)
1103#else
1104Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic)
1105#endif
1106{
1107  TComPic*                 rpcPic;
1108#if !FIX1172
1109  setAssociatedIRAPPOC(pocCRA);
1110#endif
1111  Int pocCurr = getPOC(); 
1112
1113  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1114    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1115    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
1116    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
1117    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )  // IDR or BLA picture
1118  {
1119    // mark all pictures as not used for reference
1120    TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
1121    while (iterPic != rcListPic.end())
1122    {
1123      rpcPic = *(iterPic);
1124      rpcPic->setCurrSliceIdx(0);
1125#if NO_CLRAS_OUTPUT_FLAG
1126#if POC_RESET_IDC_ENCODER
1127      if (noClrasOutputFlag)
1128      {
1129        rpcPic->getSlice(0)->setReferenced(false);  // all layers // TODO. This does not mark all layers
1130      }
1131      else
1132      {
1133        if (rpcPic->getLayerId() == m_layerId) rpcPic->getSlice(0)->setReferenced(false);  // only current layer
1134      }
1135#else
1136      if (noClrasOutputFlag)
1137      {
1138        if (rpcPic->getPOC() != pocCurr) rpcPic->getSlice(0)->setReferenced(false);  // all layers
1139      }
1140      else
1141      {
1142        if (rpcPic->getPOC() != pocCurr && rpcPic->getLayerId() == m_layerId) rpcPic->getSlice(0)->setReferenced(false);  // only current layer
1143      }
1144#endif
1145#else
1146      if (rpcPic->getPOC() != pocCurr) rpcPic->getSlice(0)->setReferenced(false);
1147#endif
1148      iterPic++;
1149    }
1150#if POC_RESET_IDC_ENCODER
1151    this->getPic()->getSlice(0)->setReferenced(true);   // Mark the current picture back as refererced.
1152#endif
1153    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
1154      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
1155      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
1156    {
1157      pocCRA = pocCurr;
1158    }
1159#if EFFICIENT_FIELD_IRAP
1160    bRefreshPending = true;
1161#endif
1162  }
1163  else // CRA or No DR
1164  {
1165#if EFFICIENT_FIELD_IRAP
1166    if(getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_IDR_N_LP || getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL)
1167    {
1168      if (bRefreshPending==true && pocCurr > m_iLastIDR) // IDR reference marking pending
1169      {
1170        TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
1171        while (iterPic != rcListPic.end())
1172        {
1173          rpcPic = *(iterPic);
1174          if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != m_iLastIDR)
1175          {
1176            rpcPic->getSlice(0)->setReferenced(false);
1177          }
1178          iterPic++;
1179        }
1180        bRefreshPending = false; 
1181      }
1182    }
1183    else
1184    {
1185#endif
1186    if (bRefreshPending==true && pocCurr > pocCRA) // CRA reference marking pending
1187    {
1188      TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
1189      while (iterPic != rcListPic.end())
1190      {
1191        rpcPic = *(iterPic);
1192        if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != pocCRA)
1193        {
1194          rpcPic->getSlice(0)->setReferenced(false);
1195        }
1196        iterPic++;
1197      }
1198      bRefreshPending = false; 
1199    }
1200#if EFFICIENT_FIELD_IRAP
1201    }
1202#endif
1203    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
1204    {
1205      bRefreshPending = true; 
1206      pocCRA = pocCurr;
1207    }
1208  }
1209}
1210
1211Void TComSlice::copySliceInfo(TComSlice *pSrc)
1212{
1213  assert( pSrc != NULL );
1214
1215  Int i, j, k;
1216
1217  m_iPOC                 = pSrc->m_iPOC;
1218  m_eNalUnitType         = pSrc->m_eNalUnitType;
1219  m_eSliceType           = pSrc->m_eSliceType;
1220  m_iSliceQp             = pSrc->m_iSliceQp;
1221#if ADAPTIVE_QP_SELECTION
1222  m_iSliceQpBase         = pSrc->m_iSliceQpBase;
1223#endif
1224  m_deblockingFilterDisable   = pSrc->m_deblockingFilterDisable;
1225  m_deblockingFilterOverrideFlag = pSrc->m_deblockingFilterOverrideFlag;
1226  m_deblockingFilterBetaOffsetDiv2 = pSrc->m_deblockingFilterBetaOffsetDiv2;
1227  m_deblockingFilterTcOffsetDiv2 = pSrc->m_deblockingFilterTcOffsetDiv2;
1228 
1229  for (i = 0; i < 2; i++)
1230  {
1231    m_aiNumRefIdx[i]     = pSrc->m_aiNumRefIdx[i];
1232  }
1233
1234  for (i = 0; i < MAX_NUM_REF; i++)
1235  {
1236    m_list1IdxToList0Idx[i] = pSrc->m_list1IdxToList0Idx[i];
1237  } 
1238  m_bCheckLDC             = pSrc->m_bCheckLDC;
1239  m_iSliceQpDelta        = pSrc->m_iSliceQpDelta;
1240  m_iSliceQpDeltaCb      = pSrc->m_iSliceQpDeltaCb;
1241  m_iSliceQpDeltaCr      = pSrc->m_iSliceQpDeltaCr;
1242  for (i = 0; i < 2; i++)
1243  {
1244    for (j = 0; j < MAX_NUM_REF; j++)
1245    {
1246      m_apcRefPicList[i][j]  = pSrc->m_apcRefPicList[i][j];
1247      m_aiRefPOCList[i][j]   = pSrc->m_aiRefPOCList[i][j];
1248    }
1249  }
1250  for (i = 0; i < 2; i++)
1251  {
1252    for (j = 0; j < MAX_NUM_REF + 1; j++)
1253    {
1254      m_bIsUsedAsLongTerm[i][j] = pSrc->m_bIsUsedAsLongTerm[i][j];
1255    }
1256  }
1257  m_iDepth               = pSrc->m_iDepth;
1258
1259  // referenced slice
1260  m_bRefenced            = pSrc->m_bRefenced;
1261
1262  // access channel
1263#if SVC_EXTENSION
1264  m_pcVPS                = pSrc->m_pcVPS;
1265  m_layerId              = pSrc->m_layerId;
1266  m_activeNumILRRefIdx         = pSrc->m_activeNumILRRefIdx;
1267  m_interLayerPredEnabledFlag  = pSrc->m_interLayerPredEnabledFlag;
1268  memcpy( m_interLayerPredLayerIdc, pSrc->m_interLayerPredLayerIdc, sizeof( m_interLayerPredLayerIdc ) );
1269#if P0312_VERT_PHASE_ADJ
1270  memcpy( m_vertPhasePositionFlag, pSrc->m_vertPhasePositionFlag, sizeof( m_vertPhasePositionFlag ) );
1271#endif
1272#endif
1273  m_pcSPS                = pSrc->m_pcSPS;
1274  m_pcPPS                = pSrc->m_pcPPS;
1275  m_pcRPS                = pSrc->m_pcRPS;
1276  m_iLastIDR             = pSrc->m_iLastIDR;
1277
1278  m_pcPic                = pSrc->m_pcPic;
1279
1280  m_colFromL0Flag        = pSrc->m_colFromL0Flag;
1281  m_colRefIdx            = pSrc->m_colRefIdx;
1282  setLambdas(pSrc->getLambdas());
1283  for (i = 0; i < 2; i++)
1284  {
1285    for (j = 0; j < MAX_NUM_REF; j++)
1286    {
1287      for (k =0; k < MAX_NUM_REF; k++)
1288      {
1289        m_abEqualRef[i][j][k] = pSrc->m_abEqualRef[i][j][k];
1290      }
1291    }
1292  }
1293
1294  m_uiTLayer                      = pSrc->m_uiTLayer;
1295  m_bTLayerSwitchingFlag          = pSrc->m_bTLayerSwitchingFlag;
1296
1297  m_sliceMode                   = pSrc->m_sliceMode;
1298  m_sliceArgument               = pSrc->m_sliceArgument;
1299  m_sliceCurStartCUAddr         = pSrc->m_sliceCurStartCUAddr;
1300  m_sliceCurEndCUAddr           = pSrc->m_sliceCurEndCUAddr;
1301  m_sliceIdx                    = pSrc->m_sliceIdx;
1302  m_sliceSegmentMode            = pSrc->m_sliceSegmentMode;
1303  m_sliceSegmentArgument        = pSrc->m_sliceSegmentArgument; 
1304  m_sliceSegmentCurStartCUAddr  = pSrc->m_sliceSegmentCurStartCUAddr;
1305  m_sliceSegmentCurEndCUAddr    = pSrc->m_sliceSegmentCurEndCUAddr;
1306  m_nextSlice                    = pSrc->m_nextSlice;
1307  m_nextSliceSegment             = pSrc->m_nextSliceSegment;
1308  for ( Int e=0 ; e<2 ; e++ )
1309  {
1310    for ( Int n=0 ; n<MAX_NUM_REF ; n++ )
1311    {
1312      memcpy(m_weightPredTable[e][n], pSrc->m_weightPredTable[e][n], sizeof(wpScalingParam)*3 );
1313    }
1314  }
1315  m_saoEnabledFlag = pSrc->m_saoEnabledFlag; 
1316  m_saoEnabledFlagChroma = pSrc->m_saoEnabledFlagChroma;
1317  m_cabacInitFlag                = pSrc->m_cabacInitFlag;
1318  m_numEntryPointOffsets  = pSrc->m_numEntryPointOffsets;
1319
1320  m_bLMvdL1Zero = pSrc->m_bLMvdL1Zero;
1321  m_LFCrossSliceBoundaryFlag = pSrc->m_LFCrossSliceBoundaryFlag;
1322  m_enableTMVPFlag                = pSrc->m_enableTMVPFlag;
1323  m_maxNumMergeCand               = pSrc->m_maxNumMergeCand;
1324}
1325
1326Int TComSlice::m_prevTid0POC = 0;
1327
1328/** Function for setting the slice's temporal layer ID and corresponding temporal_layer_switching_point_flag.
1329 * \param uiTLayer Temporal layer ID of the current slice
1330 * The decoder calls this function to set temporal_layer_switching_point_flag for each temporal layer based on
1331 * the SPS's temporal_id_nesting_flag and the parsed PPS.  Then, current slice's temporal layer ID and
1332 * temporal_layer_switching_point_flag is set accordingly.
1333 */
1334Void TComSlice::setTLayerInfo( UInt uiTLayer )
1335{
1336  m_uiTLayer = uiTLayer;
1337}
1338
1339/** Function for checking if this is a switching-point
1340*/
1341Bool TComSlice::isTemporalLayerSwitchingPoint( TComList<TComPic*>& rcListPic )
1342{
1343  TComPic* rpcPic;
1344  // loop through all pictures in the reference picture buffer
1345  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1346  while ( iterPic != rcListPic.end())
1347  {
1348    rpcPic = *(iterPic++);
1349    if(rpcPic->getSlice(0)->isReferenced() && rpcPic->getPOC() != getPOC())
1350    {
1351      if(rpcPic->getTLayer() >= getTLayer())
1352      {
1353        return false;
1354      }
1355    }
1356  }
1357  return true;
1358}
1359
1360/** Function for checking if this is a STSA candidate
1361 */
1362Bool TComSlice::isStepwiseTemporalLayerSwitchingPointCandidate( TComList<TComPic*>& rcListPic )
1363{
1364    TComPic* rpcPic;
1365   
1366    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1367    while ( iterPic != rcListPic.end())
1368    {
1369        rpcPic = *(iterPic++);
1370        if(rpcPic->getSlice(0)->isReferenced() &&  (rpcPic->getUsedByCurr()==true) && rpcPic->getPOC() != getPOC())
1371        {
1372            if(rpcPic->getTLayer() >= getTLayer())
1373            {
1374                return false;
1375            }
1376        }
1377    }
1378    return true;
1379}
1380
1381#if POC_RESET_IDC_ENCODER
1382Void TComSlice::checkLeadingPictureRestrictions(TComList<TComPic*>& rcListPic, Bool usePocBeforeReset)
1383#else
1384Void TComSlice::checkLeadingPictureRestrictions(TComList<TComPic*>& rcListPic)
1385#endif
1386{
1387  TComPic* rpcPic;
1388
1389  Int nalUnitType = this->getNalUnitType();
1390
1391  // When a picture is a leading picture, it shall be a RADL or RASL picture.
1392  if(this->getAssociatedIRAPPOC() > this->getPOC())
1393  {
1394    // Do not check IRAP pictures since they may get a POC lower than their associated IRAP
1395    if(nalUnitType < NAL_UNIT_CODED_SLICE_BLA_W_LP ||
1396       nalUnitType > NAL_UNIT_RESERVED_IRAP_VCL23)
1397    {
1398      assert(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1399             nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R ||
1400             nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1401             nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R);
1402    }
1403  }
1404
1405  // When a picture is a trailing picture, it shall not be a RADL or RASL picture.
1406  if(this->getAssociatedIRAPPOC() < this->getPOC())
1407  {
1408    assert(nalUnitType != NAL_UNIT_CODED_SLICE_RASL_N &&
1409           nalUnitType != NAL_UNIT_CODED_SLICE_RASL_R &&
1410           nalUnitType != NAL_UNIT_CODED_SLICE_RADL_N &&
1411           nalUnitType != NAL_UNIT_CODED_SLICE_RADL_R);
1412  }
1413
1414  // No RASL pictures shall be present in the bitstream that are associated
1415  // with a BLA picture having nal_unit_type equal to BLA_W_RADL or BLA_N_LP.
1416  if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1417     nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1418  {
1419    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_W_RADL &&
1420           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_N_LP);
1421  }
1422
1423  // No RASL pictures shall be present in the bitstream that are associated with
1424  // an IDR picture.
1425  if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1426     nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1427  {
1428    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_N_LP   &&
1429           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_W_RADL);
1430  }
1431
1432  // No RADL pictures shall be present in the bitstream that are associated with
1433  // a BLA picture having nal_unit_type equal to BLA_N_LP or that are associated
1434  // with an IDR picture having nal_unit_type equal to IDR_N_LP.
1435  if(nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1436     nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R)
1437  {
1438    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_N_LP   &&
1439           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_N_LP);
1440  }
1441
1442  // loop through all pictures in the reference picture buffer
1443  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1444  while ( iterPic != rcListPic.end())
1445  {
1446    rpcPic = *(iterPic++);
1447#if BUGFIX_INTRAPERIOD
1448    if(!rpcPic->getReconMark())
1449    {
1450      continue;
1451    }
1452#endif
1453    if (rpcPic->getPOC() == this->getPOC())
1454    {
1455      continue;
1456    }
1457
1458    // Any picture that has PicOutputFlag equal to 1 that precedes an IRAP picture
1459    // in decoding order shall precede the IRAP picture in output order.
1460    // (Note that any picture following in output order would be present in the DPB)
1461#if !SETTING_NO_OUT_PIC_PRIOR
1462    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1)
1463#else
1464    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1 && !this->getNoOutputPriorPicsFlag())
1465#endif
1466    {
1467      if(nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP    ||
1468         nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP    ||
1469         nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL  ||
1470         nalUnitType == NAL_UNIT_CODED_SLICE_CRA         ||
1471         nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP    ||
1472         nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL)
1473      {
1474#if POC_RESET_IDC_ENCODER
1475        if( usePocBeforeReset )
1476        {
1477          assert(rpcPic->getSlice(0)->getPocValueBeforeReset() < this->getPocValueBeforeReset());
1478        }
1479        else
1480        {
1481          assert(rpcPic->getPOC() < this->getPOC());
1482        }
1483#else
1484        assert(rpcPic->getPOC() < this->getPOC());
1485#endif
1486      }
1487    }
1488
1489    // Any picture that has PicOutputFlag equal to 1 that precedes an IRAP picture
1490    // in decoding order shall precede any RADL picture associated with the IRAP
1491    // picture in output order.
1492    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1)
1493    {
1494      if((nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1495          nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R))
1496      {
1497        // rpcPic precedes the IRAP in decoding order
1498        if(this->getAssociatedIRAPPOC() > rpcPic->getSlice(0)->getAssociatedIRAPPOC())
1499        {
1500          // rpcPic must not be the IRAP picture
1501          if(this->getAssociatedIRAPPOC() != rpcPic->getPOC())
1502          {
1503#if POC_RESET_IDC_ENCODER
1504            if( usePocBeforeReset )
1505            {
1506              assert(rpcPic->getSlice(0)->getPocValueBeforeReset() < this->getPocValueBeforeReset());
1507            }
1508            else
1509            {
1510              assert(rpcPic->getPOC() < this->getPOC());
1511            }
1512#else
1513            assert(rpcPic->getPOC() < this->getPOC());
1514#endif
1515          }
1516        }
1517      }
1518    }
1519
1520    // When a picture is a leading picture, it shall precede, in decoding order,
1521    // all trailing pictures that are associated with the same IRAP picture.
1522    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1523       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R ||
1524       nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1525       nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R)
1526    {
1527      if(rpcPic->getSlice(0)->getAssociatedIRAPPOC() == this->getAssociatedIRAPPOC())
1528      {
1529        // rpcPic is a picture that preceded the leading in decoding order since it exist in the DPB
1530        // rpcPic would violate the constraint if it was a trailing picture
1531#if POC_RESET_IDC_ENCODER
1532        if( usePocBeforeReset )
1533        {
1534          assert(rpcPic->getPOC() <= this->getAssociatedIrapPocBeforeReset());
1535        }
1536        else
1537        {
1538          assert(rpcPic->getPOC() <= this->getAssociatedIRAPPOC());
1539        }
1540#else
1541        assert(rpcPic->getPOC() <= this->getAssociatedIRAPPOC());
1542#endif
1543      }
1544    }
1545
1546    // Any RASL picture associated with a CRA or BLA picture shall precede any
1547    // RADL picture associated with the CRA or BLA picture in output order
1548    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1549       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1550    { 
1551      if((this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ||
1552          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ||
1553          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ||
1554          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_CRA)       &&
1555          this->getAssociatedIRAPPOC() == rpcPic->getSlice(0)->getAssociatedIRAPPOC())
1556      {
1557        if(rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N ||
1558           rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_R)
1559        {
1560          assert(rpcPic->getPOC() > this->getPOC());
1561        }
1562      }
1563    }
1564
1565    // Any RASL picture associated with a CRA picture shall follow, in output
1566    // order, any IRAP picture that precedes the CRA picture in decoding order.
1567    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1568       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1569    {
1570      if(this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_CRA)
1571      {
1572        if(rpcPic->getSlice(0)->getPOC() < this->getAssociatedIRAPPOC() &&
1573           (rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ||
1574            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ||
1575            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ||
1576            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP   ||
1577            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL ||
1578            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA))
1579        {
1580          assert(this->getPOC() > rpcPic->getSlice(0)->getPOC());
1581        }
1582      }
1583    }
1584  }
1585}
1586
1587
1588
1589/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1590*/
1591Void TComSlice::applyReferencePictureSet( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1592{
1593  TComPic* rpcPic;
1594  Int i, isReference;
1595
1596#if !ALIGNED_BUMPING
1597  checkLeadingPictureRestrictions(rcListPic);
1598#endif
1599
1600  // loop through all pictures in the reference picture buffer
1601  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1602  while ( iterPic != rcListPic.end())
1603  {
1604    rpcPic = *(iterPic++);
1605
1606    if(!rpcPic->getSlice( 0 )->isReferenced())
1607    {
1608      continue;
1609    }
1610
1611    isReference = 0;
1612    // loop through all pictures in the Reference Picture Set
1613    // to see if the picture should be kept as reference picture
1614    for(i=0;i<pReferencePictureSet->getNumberOfPositivePictures()+pReferencePictureSet->getNumberOfNegativePictures();i++)
1615    {
1616      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i))
1617      {
1618        isReference = 1;
1619        rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1620        rpcPic->setIsLongTerm(0);
1621      }
1622    }
1623    for(;i<pReferencePictureSet->getNumberOfPictures();i++)
1624    {
1625      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1626      {
1627        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i))
1628        {
1629          isReference = 1;
1630          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1631        }
1632      }
1633      else 
1634      {
1635        Int pocCycle = 1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1636        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC() & (pocCycle-1);
1637        Int refPoc = pReferencePictureSet->getPOC(i) & (pocCycle-1);
1638        if(rpcPic->getIsLongTerm() && curPoc == refPoc)
1639        {
1640          isReference = 1;
1641          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1642        }
1643      }
1644
1645    }
1646#if DISCARDABLE_PIC_RPS
1647    if( isReference ) // Current picture is in the temporal RPS
1648    {
1649      assert( rpcPic->getSlice(0)->getDiscardableFlag() == 0 ); // Temporal RPS shall not contain picture with discardable_flag equal to 1
1650    }
1651#endif
1652    // mark the picture as "unused for reference" if it is not in
1653    // the Reference Picture Set
1654    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && isReference == 0)   
1655    {           
1656      rpcPic->getSlice( 0 )->setReferenced( false );   
1657      rpcPic->setUsedByCurr(0);
1658      rpcPic->setIsLongTerm(0);
1659    }
1660    //check that pictures of higher temporal layers are not used
1661    assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
1662    //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
1663    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
1664    {
1665      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
1666    }
1667    //check that pictures marked as temporal layer non-reference pictures are not used for reference
1668    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && rpcPic->getTLayer()==this->getTLayer())
1669    {
1670      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getSlice( 0 )->getTemporalLayerNonReferenceFlag()==false);
1671    }
1672  }
1673}
1674
1675/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1676*/
1677#if ALLOW_RECOVERY_POINT_AS_RAP
1678Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess, Bool bUseRecoveryPoint)
1679#else
1680Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess)
1681#endif
1682{
1683#if ALLOW_RECOVERY_POINT_AS_RAP
1684  Int atLeastOneUnabledByRecoveryPoint = 0;
1685  Int atLeastOneFlushedByPreviousIDR = 0;
1686#endif
1687  TComPic* rpcPic;
1688  Int i, isAvailable;
1689  Int atLeastOneLost = 0;
1690  Int atLeastOneRemoved = 0;
1691  Int iPocLost = 0;
1692
1693  // loop through all long-term pictures in the Reference Picture Set
1694  // to see if the picture should be kept as reference picture
1695  for(i=pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i<pReferencePictureSet->getNumberOfPictures();i++)
1696  {
1697    isAvailable = 0;
1698    // loop through all pictures in the reference picture buffer
1699    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1700    while ( iterPic != rcListPic.end())
1701    {
1702      rpcPic = *(iterPic++);
1703      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1704      {
1705        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i) && rpcPic->getSlice(0)->isReferenced())
1706        {
1707#if ALLOW_RECOVERY_POINT_AS_RAP
1708          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1709          {
1710            isAvailable = 0;
1711          }
1712          else
1713          {
1714          isAvailable = 1;
1715        }
1716#else
1717          isAvailable = 1;
1718#endif
1719        }
1720      }
1721      else 
1722      {
1723        Int pocCycle = 1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1724        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC() & (pocCycle-1);
1725        Int refPoc = pReferencePictureSet->getPOC(i) & (pocCycle-1);
1726        if(rpcPic->getIsLongTerm() && curPoc == refPoc && rpcPic->getSlice(0)->isReferenced())
1727        {
1728#if ALLOW_RECOVERY_POINT_AS_RAP
1729          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1730          {
1731            isAvailable = 0;
1732          }
1733          else
1734          {
1735          isAvailable = 1;
1736        }
1737#else
1738          isAvailable = 1;
1739#endif
1740        }
1741      }
1742    }
1743    // if there was no such long-term check the short terms
1744    if(!isAvailable)
1745    {
1746      iterPic = rcListPic.begin();
1747      while ( iterPic != rcListPic.end())
1748      {
1749        rpcPic = *(iterPic++);
1750
1751        Int pocCycle = 1 << rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1752        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC();
1753        Int refPoc = pReferencePictureSet->getPOC(i);
1754        if (!pReferencePictureSet->getCheckLTMSBPresent(i))
1755        {
1756          curPoc = curPoc & (pocCycle - 1);
1757          refPoc = refPoc & (pocCycle - 1);
1758        }
1759       
1760        if (rpcPic->getSlice(0)->isReferenced() && curPoc == refPoc)
1761        {
1762#if ALLOW_RECOVERY_POINT_AS_RAP
1763          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1764          {
1765            isAvailable = 0;
1766          }
1767          else
1768          {
1769          isAvailable = 1;
1770          rpcPic->setIsLongTerm(1);
1771          break;
1772        }
1773#else
1774          isAvailable = 1;
1775          rpcPic->setIsLongTerm(1);
1776          break;
1777#endif
1778        }
1779      }
1780    }
1781    // report that a picture is lost if it is in the Reference Picture Set
1782    // but not available as reference picture
1783    if(isAvailable == 0)   
1784    {           
1785      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1786      {
1787        if(!pReferencePictureSet->getUsed(i) )
1788        {
1789          if(printErrors)
1790          {
1791            printf("\nLong-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1792          }
1793          atLeastOneRemoved = 1;
1794        }
1795        else
1796        {
1797          if(printErrors)
1798          {
1799            printf("\nLong-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1800          }
1801          atLeastOneLost = 1;
1802          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1803        }
1804      }
1805#if ALLOW_RECOVERY_POINT_AS_RAP
1806      else if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess)
1807      {
1808        atLeastOneUnabledByRecoveryPoint = 1;
1809      }
1810      else if(bUseRecoveryPoint && (this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_N_LP || this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_W_RADL))
1811      {
1812        atLeastOneFlushedByPreviousIDR = 1;
1813      }
1814#endif
1815    }
1816  } 
1817  // loop through all short-term pictures in the Reference Picture Set
1818  // to see if the picture should be kept as reference picture
1819  for(i=0;i<pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i++)
1820  {
1821    isAvailable = 0;
1822    // loop through all pictures in the reference picture buffer
1823    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1824    while ( iterPic != rcListPic.end())
1825    {
1826      rpcPic = *(iterPic++);
1827
1828      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1829      {
1830#if ALLOW_RECOVERY_POINT_AS_RAP
1831        if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1832        {
1833          isAvailable = 0;
1834        }
1835        else
1836        {
1837        isAvailable = 1;
1838      }
1839#else
1840        isAvailable = 1;
1841#endif
1842      }
1843    }
1844    // report that a picture is lost if it is in the Reference Picture Set
1845    // but not available as reference picture
1846    if(isAvailable == 0)   
1847    {           
1848#if !UNAVAILABLE_PIC_BUGFIX
1849      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1850#endif
1851      {
1852        if(!pReferencePictureSet->getUsed(i) )
1853        {
1854          if(printErrors)
1855          {
1856            printf("\nShort-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1857          }
1858          atLeastOneRemoved = 1;
1859        }
1860        else
1861        {
1862          if(printErrors)
1863          {
1864            printf("\nShort-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1865          }
1866          atLeastOneLost = 1;
1867          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1868        }
1869      }
1870#if ALLOW_RECOVERY_POINT_AS_RAP
1871#if UNAVAILABLE_PIC_BUGFIX
1872      if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess)
1873#else
1874      else if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess)
1875#endif
1876      {
1877        atLeastOneUnabledByRecoveryPoint = 1;
1878      }
1879      else if(bUseRecoveryPoint && (this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_N_LP || this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_W_RADL))
1880      {
1881        atLeastOneFlushedByPreviousIDR = 1;
1882      }
1883#endif
1884    }
1885    }
1886#if ALLOW_RECOVERY_POINT_AS_RAP
1887  if(atLeastOneUnabledByRecoveryPoint || atLeastOneFlushedByPreviousIDR)
1888  {
1889    return -1;
1890  }   
1891#endif
1892  if(atLeastOneLost)
1893  {
1894    return iPocLost+1;
1895  }
1896  if(atLeastOneRemoved)
1897  {
1898    return -2;
1899  }
1900  else
1901  {
1902    return 0;
1903  }
1904}
1905
1906/** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
1907*/
1908#if ALLOW_RECOVERY_POINT_AS_RAP
1909Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool isRAP, Int pocRandomAccess, Bool bUseRecoveryPoint)
1910#else
1911Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool isRAP)
1912#endif
1913{
1914  TComPic* rpcPic;
1915  Int i, j;
1916  Int k = 0;
1917  Int nrOfNegativePictures = 0;
1918  Int nrOfPositivePictures = 0;
1919  TComReferencePictureSet* pcRPS = this->getLocalRPS();
1920
1921  // loop through all pictures in the Reference Picture Set
1922  for(i=0;i<pReferencePictureSet->getNumberOfPictures();i++)
1923  {
1924    j = 0;
1925    // loop through all pictures in the reference picture buffer
1926    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1927    while ( iterPic != rcListPic.end())
1928    {
1929      j++;
1930      rpcPic = *(iterPic++);
1931
1932      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1933      {
1934        // This picture exists as a reference picture
1935        // and should be added to the explicit Reference Picture Set
1936        pcRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
1937        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i) && (!isRAP));
1938#if ALLOW_RECOVERY_POINT_AS_RAP
1939        pcRPS->setUsed(k, pcRPS->getUsed(k) && !(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess) ); 
1940#endif
1941        if(pcRPS->getDeltaPOC(k) < 0)
1942        {
1943          nrOfNegativePictures++;
1944        }
1945        else
1946        {
1947          nrOfPositivePictures++;
1948        }
1949        k++;
1950      }
1951    }
1952  }
1953#if EFFICIENT_FIELD_IRAP
1954  Bool useNewRPS = false;
1955  // if current picture is complimentary field associated to IRAP, add the IRAP to its RPS.
1956  if(m_pcPic->isField())
1957  {
1958    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1959    while ( iterPic != rcListPic.end())
1960    {
1961      rpcPic = *(iterPic++);
1962      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getAssociatedIRAPPOC() && this->getAssociatedIRAPPOC() == this->getPOC()+1)
1963      {
1964        pcRPS->setDeltaPOC(k, 1);
1965        pcRPS->setUsed(k, true);
1966        nrOfPositivePictures++;
1967        k ++;
1968        useNewRPS = true;
1969      }
1970    }
1971  }
1972#endif
1973  pcRPS->setNumberOfNegativePictures(nrOfNegativePictures);
1974  pcRPS->setNumberOfPositivePictures(nrOfPositivePictures);
1975  pcRPS->setNumberOfPictures(nrOfNegativePictures+nrOfPositivePictures);
1976  // This is a simplistic inter rps example. A smarter encoder will look for a better reference RPS to do the
1977  // inter RPS prediction with.  Here we just use the reference used by pReferencePictureSet.
1978  // If pReferencePictureSet is not inter_RPS_predicted, then inter_RPS_prediction is for the current RPS also disabled.
1979  if (!pReferencePictureSet->getInterRPSPrediction()
1980#if EFFICIENT_FIELD_IRAP
1981    || useNewRPS
1982#endif
1983    )
1984  {
1985    pcRPS->setInterRPSPrediction(false);
1986    pcRPS->setNumRefIdc(0);
1987  }
1988  else
1989  {
1990    Int rIdx =  this->getRPSidx() - pReferencePictureSet->getDeltaRIdxMinus1() - 1;
1991    Int deltaRPS = pReferencePictureSet->getDeltaRPS();
1992    TComReferencePictureSet* pcRefRPS = this->getSPS()->getRPSList()->getReferencePictureSet(rIdx);
1993    Int iRefPics = pcRefRPS->getNumberOfPictures();
1994    Int iNewIdc=0;
1995    for(i=0; i<= iRefPics; i++) 
1996    {
1997      Int deltaPOC = ((i != iRefPics)? pcRefRPS->getDeltaPOC(i) : 0);  // check if the reference abs POC is >= 0
1998      Int iRefIdc = 0;
1999      for (j=0; j < pcRPS->getNumberOfPictures(); j++) // loop through the  pictures in the new RPS
2000      {
2001        if ( (deltaPOC + deltaRPS) == pcRPS->getDeltaPOC(j))
2002        {
2003          if (pcRPS->getUsed(j))
2004          {
2005            iRefIdc = 1;
2006          }
2007          else
2008          {
2009            iRefIdc = 2;
2010          }
2011        }
2012      }
2013      pcRPS->setRefIdc(i, iRefIdc);
2014      iNewIdc++;
2015    }
2016    pcRPS->setInterRPSPrediction(true);
2017    pcRPS->setNumRefIdc(iNewIdc);
2018    pcRPS->setDeltaRPS(deltaRPS); 
2019    pcRPS->setDeltaRIdxMinus1(pReferencePictureSet->getDeltaRIdxMinus1() + this->getSPS()->getRPSList()->getNumberOfReferencePictureSets() - this->getRPSidx());
2020  }
2021
2022  this->setRPS(pcRPS);
2023  this->setRPSidx(-1);
2024}
2025
2026/** get AC and DC values for weighted pred
2027 * \param *wp
2028 * \returns Void
2029 */
2030Void  TComSlice::getWpAcDcParam(wpACDCParam *&wp)
2031{
2032  wp = m_weightACDCParam;
2033}
2034
2035/** init AC and DC values for weighted pred
2036 * \returns Void
2037 */
2038Void  TComSlice::initWpAcDcParam()
2039{
2040  for(Int iComp = 0; iComp < 3; iComp++ )
2041  {
2042    m_weightACDCParam[iComp].iAC = 0;
2043    m_weightACDCParam[iComp].iDC = 0;
2044  }
2045}
2046
2047/** get WP tables for weighted pred
2048 * \param RefPicList
2049 * \param iRefIdx
2050 * \param *&wpScalingParam
2051 * \returns Void
2052 */
2053Void  TComSlice::getWpScaling( RefPicList e, Int iRefIdx, wpScalingParam *&wp )
2054{
2055  wp = m_weightPredTable[e][iRefIdx];
2056}
2057
2058/** reset Default WP tables settings : no weight.
2059 * \param wpScalingParam
2060 * \returns Void
2061 */
2062Void  TComSlice::resetWpScaling()
2063{
2064  for ( Int e=0 ; e<2 ; e++ )
2065  {
2066    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
2067    {
2068      for ( Int yuv=0 ; yuv<3 ; yuv++ )
2069      {
2070        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
2071        pwp->bPresentFlag      = false;
2072        pwp->uiLog2WeightDenom = 0;
2073        pwp->uiLog2WeightDenom = 0;
2074        pwp->iWeight           = 1;
2075        pwp->iOffset           = 0;
2076      }
2077    }
2078  }
2079}
2080
2081/** init WP table
2082 * \returns Void
2083 */
2084Void  TComSlice::initWpScaling()
2085{
2086  for ( Int e=0 ; e<2 ; e++ )
2087  {
2088    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
2089    {
2090      for ( Int yuv=0 ; yuv<3 ; yuv++ )
2091      {
2092        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
2093        if ( !pwp->bPresentFlag ) 
2094        {
2095          // Inferring values not present :
2096          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
2097          pwp->iOffset = 0;
2098        }
2099
2100        pwp->w      = pwp->iWeight;
2101        Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
2102        pwp->o      = pwp->iOffset << (bitDepth-8);
2103        pwp->shift  = pwp->uiLog2WeightDenom;
2104        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
2105      }
2106    }
2107  }
2108}
2109
2110#if REPN_FORMAT_IN_VPS
2111UInt TComSlice::getPicWidthInLumaSamples()
2112{
2113  TComSPS *sps = getSPS();
2114  TComVPS *vps = getVPS();
2115  UInt retVal, layerId = getLayerId();
2116#if O0096_REP_FORMAT_INDEX
2117#if R0279_REP_FORMAT_INBL
2118  if ( layerId == 0 || sps->getV1CompatibleSPSFlag() == 1 )
2119  {
2120    if( layerId == 0 && vps->getAvcBaseLayerFlag() )
2121#else
2122  if ( layerId == 0 )
2123  {
2124    if( vps->getAvcBaseLayerFlag() )
2125#endif
2126    {
2127      retVal = vps->getVpsRepFormat(layerId)->getPicWidthVpsInLumaSamples();
2128    }
2129    else
2130    {
2131      retVal = sps->getPicWidthInLumaSamples();
2132    }
2133  }
2134  else
2135  {
2136    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : layerId) )->getPicWidthVpsInLumaSamples();
2137  }
2138#else
2139  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
2140  {
2141    retVal = sps->getPicWidthInLumaSamples();
2142  }
2143  else
2144  {
2145    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getPicWidthVpsInLumaSamples();
2146  }
2147#endif
2148  return retVal;
2149}
2150UInt TComSlice::getPicHeightInLumaSamples()
2151{
2152  TComSPS *sps = getSPS();
2153  TComVPS *vps = getVPS();
2154  UInt retVal, layerId = getLayerId();
2155#if O0096_REP_FORMAT_INDEX
2156#if R0279_REP_FORMAT_INBL
2157  if ( layerId == 0 || sps->getV1CompatibleSPSFlag() == 1 )
2158  {
2159    if( layerId == 0 && vps->getAvcBaseLayerFlag() )
2160#else
2161  if ( layerId == 0 )
2162  {
2163    if( vps->getAvcBaseLayerFlag() )
2164#endif
2165    {
2166      retVal = vps->getVpsRepFormat(layerId)->getPicHeightVpsInLumaSamples();
2167    }
2168    else
2169    {
2170      retVal = sps->getPicHeightInLumaSamples();
2171    }
2172  }
2173  else
2174  {
2175    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : layerId) )->getPicHeightVpsInLumaSamples();
2176  }
2177#else
2178  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
2179  {
2180    retVal = sps->getPicHeightInLumaSamples();
2181  }
2182  else
2183  {
2184    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getPicHeightVpsInLumaSamples();
2185  }
2186#endif
2187  return retVal;
2188}
2189#if AUXILIARY_PICTURES
2190ChromaFormat TComSlice::getChromaFormatIdc()
2191#else
2192UInt TComSlice::getChromaFormatIdc()
2193#endif
2194{
2195  TComSPS *sps = getSPS();
2196  TComVPS *vps = getVPS();
2197#if AUXILIARY_PICTURES
2198  ChromaFormat retVal;
2199  UInt layerId = getLayerId();
2200#else
2201  UInt retVal, layerId = getLayerId();
2202#endif
2203#if O0096_REP_FORMAT_INDEX
2204#if R0279_REP_FORMAT_INBL
2205  if ( layerId == 0 || sps->getV1CompatibleSPSFlag() == 1 )
2206  {
2207    if( layerId == 0 && vps->getAvcBaseLayerFlag() )
2208#else
2209  if ( layerId == 0 )
2210  {
2211    if( vps->getAvcBaseLayerFlag() )
2212#endif
2213    {
2214      retVal = vps->getVpsRepFormat(layerId)->getChromaFormatVpsIdc();
2215    }
2216    else
2217    {
2218      retVal = sps->getChromaFormatIdc();
2219    }
2220  }
2221  else
2222  {
2223    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : layerId) )->getChromaFormatVpsIdc();
2224  }
2225#else
2226  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
2227  {
2228    retVal = sps->getChromaFormatIdc();
2229  }
2230  else
2231  {
2232    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getChromaFormatVpsIdc();
2233  }
2234#endif
2235  return retVal;
2236}
2237UInt TComSlice::getBitDepthY()
2238{
2239  TComSPS *sps = getSPS();
2240  TComVPS *vps = getVPS();
2241  UInt retVal, layerId = getLayerId();
2242#if O0096_REP_FORMAT_INDEX
2243#if R0279_REP_FORMAT_INBL
2244  if ( layerId == 0 || sps->getV1CompatibleSPSFlag() == 1 )
2245#else
2246  if ( layerId == 0 )
2247#endif
2248  {
2249    retVal = sps->getBitDepthY();
2250  }
2251  else
2252  {
2253    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : layerId) )->getBitDepthVpsLuma();
2254  }
2255#else
2256  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
2257  {
2258    retVal = sps->getBitDepthY();
2259  }
2260  else
2261  {
2262    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getBitDepthVpsLuma();
2263  }
2264#endif
2265  return retVal;
2266}
2267UInt TComSlice::getBitDepthC()
2268{
2269  TComSPS *sps = getSPS();
2270  TComVPS *vps = getVPS();
2271  UInt retVal, layerId = getLayerId();
2272#if O0096_REP_FORMAT_INDEX
2273#if R0279_REP_FORMAT_INBL
2274  if ( layerId == 0 || sps->getV1CompatibleSPSFlag() == 1 )
2275#else
2276  if ( layerId == 0 )
2277#endif
2278  {
2279    retVal = sps->getBitDepthC();
2280  }
2281  else
2282  {
2283    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : layerId) )->getBitDepthVpsChroma();
2284  }
2285#else
2286  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
2287  {
2288    retVal = sps->getBitDepthC();
2289  }
2290  else
2291  {
2292    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getBitDepthVpsChroma();
2293  }
2294#endif
2295  return retVal;
2296}
2297Int TComSlice::getQpBDOffsetY()
2298{
2299  return (getBitDepthY() - 8) * 6;
2300}
2301Int TComSlice::getQpBDOffsetC()
2302{
2303  return (getBitDepthC() - 8) * 6;
2304}
2305
2306RepFormat::RepFormat()
2307#if AUXILIARY_PICTURES
2308: m_chromaFormatVpsIdc          (CHROMA_420)
2309#else
2310: m_chromaFormatVpsIdc          (0)
2311#endif
2312, m_separateColourPlaneVpsFlag  (false)
2313, m_picWidthVpsInLumaSamples    (0)
2314, m_picHeightVpsInLumaSamples   (0)
2315, m_bitDepthVpsLuma             (0)
2316, m_bitDepthVpsChroma           (0)
2317{}
2318#if RESOLUTION_BASED_DPB
2319Void RepFormat::init()
2320{
2321  m_chromaFormatVpsIdc          = CHROMA_420;
2322  m_separateColourPlaneVpsFlag  = false;
2323  m_picWidthVpsInLumaSamples    = 0;
2324  m_picHeightVpsInLumaSamples   = 0;
2325  m_bitDepthVpsLuma             = 0;
2326  m_bitDepthVpsChroma           = 0;
2327}
2328#endif
2329#endif
2330
2331// ------------------------------------------------------------------------------------------------
2332// Video parameter set (VPS)
2333// ------------------------------------------------------------------------------------------------
2334#if SVC_EXTENSION
2335TComVPS::TComVPS()
2336: m_VPSId                     (  0)
2337#if VPS_RESERVED_FLAGS
2338, m_baseLayerInternalFlag     (true)
2339, m_baseLayerAvailableFlag    (true)
2340#endif
2341, m_uiMaxTLayers              (  1)
2342, m_uiMaxLayers               (  1)
2343, m_bTemporalIdNestingFlag    (false)
2344, m_numHrdParameters          (  0)
2345, m_hrdParameters             (NULL)
2346, m_hrdOpSetIdx               (NULL)
2347, m_cprmsPresentFlag          (NULL)
2348, m_maxLayerId                (0)
2349, m_numLayerSets              (0)
2350#if VPS_EXTN_OP_LAYER_SETS
2351, m_numOutputLayerSets        (0) 
2352#endif
2353, m_numProfileTierLevel       (0)
2354#if !VPS_EXTN_UEV_CODING
2355, m_moreOutputLayerSetsThanDefaultFlag (false)
2356#endif
2357, m_numAddOutputLayerSets     (0)
2358#if P0295_DEFAULT_OUT_LAYER_IDC
2359, m_defaultTargetOutputLayerIdc     (0)
2360#else
2361#if O0109_DEFAULT_ONE_OUT_LAYER_IDC
2362, m_defaultOneTargetOutputLayerIdc     (0)
2363#else
2364, m_defaultOneTargetOutputLayerFlag    (false)
2365#endif
2366#endif
2367, m_bitRatePresentVpsFlag     (false)
2368, m_picRatePresentVpsFlag     (false)
2369#if REPN_FORMAT_IN_VPS
2370#if Q0195_REP_FORMAT_CLEANUP
2371, m_repFormatIdxPresentFlag   (false)
2372#else
2373, m_repFormatIdxPresentFlag   (true)
2374#endif
2375, m_vpsNumRepFormats          (1)
2376#endif
2377#if VIEW_ID_RELATED_SIGNALING
2378#if O0109_VIEW_ID_LEN
2379, m_viewIdLen                (0)
2380#else
2381, m_viewIdLenMinus1           (0)
2382#endif
2383#endif
2384#if !P0307_REMOVE_VPS_VUI_OFFSET
2385#if VPS_VUI_OFFSET
2386, m_vpsVuiOffset (0)
2387#endif
2388#endif
2389#if P0307_VPS_NON_VUI_EXTENSION
2390, m_vpsNonVuiExtLength (0)
2391#endif
2392{
2393  for( Int i = 0; i < MAX_TLAYER; i++)
2394  {
2395    m_numReorderPics[i] = 0;
2396    m_uiMaxDecPicBuffering[i] = 1; 
2397    m_uiMaxLatencyIncrease[i] = 0;
2398  }
2399#if VPS_EXTN_MASK_AND_DIM_INFO
2400  m_avcBaseLayerFlag = false;
2401  m_splittingFlag = false;
2402  ::memset(m_scalabilityMask, 0, sizeof(m_scalabilityMask));
2403  ::memset(m_dimensionIdLen, 0, sizeof(m_dimensionIdLen));
2404  m_nuhLayerIdPresentFlag = false;
2405  ::memset(m_layerIdInNuh, 0, sizeof(m_layerIdInNuh));
2406  ::memset(m_dimensionId, 0, sizeof(m_dimensionId));
2407
2408  m_numScalabilityTypes = 0;
2409  ::memset(m_layerIdInVps, 0, sizeof(m_layerIdInVps));
2410#endif
2411#if VPS_EXTN_PROFILE_INFO
2412  ::memset(m_profilePresentFlag, 0, sizeof(m_profilePresentFlag));
2413#if !P0048_REMOVE_PROFILE_REF
2414  ::memset(m_profileLayerSetRef, 0, sizeof(m_profileLayerSetRef));
2415#endif
2416#endif
2417#if VPS_EXTN_OP_LAYER_SETS
2418  ::memset(m_layerIdIncludedFlag, 0, sizeof(m_layerIdIncludedFlag));
2419  // Consider dynamic allocation for outputLayerSetIdx and outputLayerFlag
2420  ::memset(m_outputLayerSetIdx, 0, sizeof(m_outputLayerSetIdx));
2421  ::memset(m_outputLayerFlag, 0, sizeof(m_outputLayerFlag));
2422#endif
2423#if VPS_EXTN_DIRECT_REF_LAYERS
2424  ::memset(m_directDependencyFlag, 0, sizeof(m_directDependencyFlag));
2425  ::memset(m_numDirectRefLayers,   0, sizeof(m_numDirectRefLayers  ));
2426  ::memset(m_refLayerId,           0, sizeof(m_refLayerId          ));
2427  m_directDepTypeLen = 2;
2428  ::memset(m_directDependencyType, 0, sizeof(m_directDependencyType));
2429#endif
2430#if DERIVE_LAYER_ID_LIST_VARIABLES
2431  ::memset(m_layerSetLayerIdList,  0, sizeof(m_layerSetLayerIdList));
2432  ::memset(m_numLayerInIdList,     0, sizeof(m_numLayerInIdList   )); 
2433#endif
2434  ::memset(m_profileLevelTierIdx,  0, sizeof(m_profileLevelTierIdx));
2435  m_maxOneActiveRefLayerFlag = true;
2436#if O0062_POC_LSB_NOT_PRESENT_FLAG
2437  ::memset(m_pocLsbNotPresentFlag, 0, sizeof(m_pocLsbNotPresentFlag));
2438#endif
2439#if O0223_PICTURE_TYPES_ALIGN_FLAG
2440  m_crossLayerPictureTypeAlignFlag = true;
2441#endif
2442  m_crossLayerIrapAlignFlag = true;
2443#if P0068_CROSS_LAYER_ALIGNED_IDR_ONLY_FOR_IRAP_FLAG
2444  m_crossLayerAlignedIdrOnlyFlag = false;
2445#endif
2446  m_maxTidRefPresentFlag = true;
2447  for( Int i = 0; i < MAX_VPS_LAYER_ID_PLUS1 - 1; i++)
2448  {
2449#if O0225_MAX_TID_FOR_REF_LAYERS
2450    for( Int j = 0; j < MAX_VPS_LAYER_ID_PLUS1; j++)
2451    {
2452      m_maxTidIlRefPicsPlus1[i][j] = m_uiMaxTLayers + 1;
2453    }
2454#else
2455    m_maxTidIlRefPicsPlus1[i] = m_uiMaxTLayers + 1;
2456#endif
2457  }
2458#if VPS_VUI_TILES_NOT_IN_USE__FLAG
2459  m_tilesNotInUseFlag = true;
2460  ::memset(m_tilesInUseFlag,  0, sizeof(m_tilesInUseFlag));
2461  ::memset(m_loopFilterNotAcrossTilesFlag,  0, sizeof(m_loopFilterNotAcrossTilesFlag));
2462#endif
2463  ::memset(m_tileBoundariesAlignedFlag,  0, sizeof(m_tileBoundariesAlignedFlag));
2464#if VPS_VUI_WPP_NOT_IN_USE__FLAG
2465  m_wppNotInUseFlag = true;
2466  ::memset(m_wppInUseFlag,  0, sizeof(m_wppInUseFlag));
2467#endif
2468#if N0160_VUI_EXT_ILP_REF
2469  m_ilpRestrictedRefLayersFlag = false;
2470  ::memset(m_minSpatialSegmentOffsetPlus1,  0, sizeof(m_minSpatialSegmentOffsetPlus1));
2471  ::memset(m_ctuBasedOffsetEnabledFlag,     0, sizeof(m_ctuBasedOffsetEnabledFlag));
2472  ::memset(m_minHorizontalCtuOffsetPlus1,   0, sizeof(m_minHorizontalCtuOffsetPlus1));
2473#endif
2474#if VPS_VUI_VIDEO_SIGNAL
2475  m_vidSigPresentVpsFlag=true;
2476  m_vpsVidSigInfo=1;
2477  ::memset( m_vpsVidSigIdx, 0, sizeof(m_vpsVidSigIdx) );
2478  m_vpsVidSigIdx[0]=0;
2479  for (Int i=0; i < 16; i++)
2480  {
2481    m_vpsVidFormat[i] = 5;
2482    m_vpsFullRangeFlag[i] = false;
2483    m_vpsColorPrimaries[i] = 2;
2484    m_vpsTransChar[i] = 2;
2485    m_vpsMatCoeff[i] = 2;
2486  }
2487#endif
2488  ::memset(m_bitRatePresentFlag, 0, sizeof(m_bitRatePresentFlag));
2489  ::memset(m_picRatePresentFlag, 0, sizeof(m_picRatePresentFlag));
2490  ::memset(m_avgBitRate        , 0, sizeof(m_avgBitRate)        );
2491  ::memset(m_maxBitRate        , 0, sizeof(m_maxBitRate)        );
2492  ::memset(m_constPicRateIdc   , 0, sizeof(m_constPicRateIdc)   );
2493  ::memset(m_avgPicRate        , 0, sizeof(m_avgPicRate)        );
2494#if REPN_FORMAT_IN_VPS
2495  ::memset( m_vpsRepFormatIdx, 0, sizeof(m_vpsRepFormatIdx) );
2496#endif
2497#if VIEW_ID_RELATED_SIGNALING
2498  ::memset(m_viewIdVal, 0, sizeof(m_viewIdVal));
2499#endif
2500#if O0092_0094_DEPENDENCY_CONSTRAINT
2501  for (Int i = 0; i < MAX_NUM_LAYER_IDS; i++)
2502  {
2503    m_numberRefLayers[i] = 0;
2504    for (Int j = 0; j < MAX_NUM_LAYER_IDS; j++)
2505    {
2506      m_recursiveRefLayerFlag[i][j] = 0;
2507    }
2508  }
2509#endif
2510#if VPS_DPB_SIZE_TABLE
2511  ::memset( m_subLayerFlagInfoPresentFlag,  0, sizeof(m_subLayerFlagInfoPresentFlag ) );
2512  ::memset( m_subLayerDpbInfoPresentFlag,   0, sizeof(m_subLayerDpbInfoPresentFlag )  );
2513  ::memset( m_maxVpsDecPicBufferingMinus1,  0, sizeof(m_maxVpsDecPicBufferingMinus1 ) );
2514#if RESOLUTION_BASED_DPB
2515  ::memset( m_maxVpsLayerDecPicBuffMinus1,  0, sizeof(m_maxVpsLayerDecPicBuffMinus1 ) );
2516#endif
2517  ::memset( m_maxVpsNumReorderPics,         0, sizeof(m_maxVpsNumReorderPics )        );
2518  ::memset( m_maxVpsLatencyIncreasePlus1,   0, sizeof(m_maxVpsLatencyIncreasePlus1 )  );
2519  ::memset( m_numSubDpbs                ,   0, sizeof(m_numSubDpbs)                   );
2520#endif
2521}
2522#else
2523TComVPS::TComVPS()
2524: m_VPSId                     (  0)
2525, m_uiMaxTLayers              (  1)
2526, m_uiMaxLayers               (  1)
2527, m_bTemporalIdNestingFlag    (false)
2528, m_numHrdParameters          (  0)
2529, m_maxNuhReservedZeroLayerId (  0)
2530, m_hrdParameters             (NULL)
2531, m_hrdOpSetIdx               (NULL)
2532, m_cprmsPresentFlag          (NULL)
2533{
2534  for( Int i = 0; i < MAX_TLAYER; i++)
2535  {
2536    m_numReorderPics[i] = 0;
2537    m_uiMaxDecPicBuffering[i] = 1; 
2538    m_uiMaxLatencyIncrease[i] = 0;
2539  }
2540}
2541#endif //SVC_EXTENSION
2542
2543TComVPS::~TComVPS()
2544{
2545  if( m_hrdParameters    != NULL )     delete[] m_hrdParameters;
2546  if( m_hrdOpSetIdx      != NULL )     delete[] m_hrdOpSetIdx;
2547  if( m_cprmsPresentFlag != NULL )     delete[] m_cprmsPresentFlag;
2548}
2549#if DERIVE_LAYER_ID_LIST_VARIABLES
2550Void TComVPS::deriveLayerIdListVariables()
2551{
2552  // For layer 0
2553  m_numLayerInIdList[0] = 1;
2554  m_layerSetLayerIdList[0][0] = 0;
2555 
2556  // For other layers
2557  Int i, m, n;
2558  for( i = 1; i < m_numLayerSets; i++ )
2559  {
2560    n = 0;
2561    for( m = 0; m <= m_maxLayerId; m++)
2562    {
2563      if( m_layerIdIncludedFlag[i][m] )
2564      {
2565        m_layerSetLayerIdList[i][n++] = m;
2566      }
2567    }
2568    m_numLayerInIdList[i] = n;
2569  }
2570}
2571#endif
2572#if !RESOLUTION_BASED_DPB
2573#if VPS_DPB_SIZE_TABLE
2574Void TComVPS::deriveNumberOfSubDpbs()
2575{
2576  // Derive number of sub-DPBs
2577#if CHANGE_NUMSUBDPB_IDX
2578  // For layer set 0
2579  setNumSubDpbs(0, 1);
2580  // For other layer sets
2581  for( Int i = 1; i < getNumLayerSets(); i++)
2582  {
2583    setNumSubDpbs( i, getNumLayersInIdList( i ) );
2584  }
2585#else
2586  // For output layer set 0
2587  setNumSubDpbs(0, 1);
2588  // For other output layer sets
2589  for( Int i = 1; i < getNumOutputLayerSets(); i++)
2590  {
2591    setNumSubDpbs( i, getNumLayersInIdList( getOutputLayerSetIdx(i)) );
2592  }
2593#endif
2594}
2595#endif
2596#endif
2597#if VPS_VUI_TILES_NOT_IN_USE__FLAG
2598Void TComVPS::setTilesNotInUseFlag(Bool x)
2599{
2600  m_tilesNotInUseFlag = x;
2601  if (m_tilesNotInUseFlag)
2602  {
2603    for (int i = 0; i < getMaxLayers(); i++)
2604    {
2605      m_tilesInUseFlag[i] = m_loopFilterNotAcrossTilesFlag[i] = m_tilesNotInUseFlag;
2606    }
2607  }
2608
2609  if (m_tilesNotInUseFlag)
2610  {
2611    for (int i = 1; i < getMaxLayers(); i++)
2612    {
2613      for(int j = 0; j < getNumDirectRefLayers(getLayerIdInNuh(i)); j++)
2614      {
2615        setTileBoundariesAlignedFlag(i, j, m_tilesNotInUseFlag);
2616      }
2617    }
2618  }
2619}
2620#endif
2621
2622#if VPS_VUI_WPP_NOT_IN_USE__FLAG
2623Void TComVPS::setWppNotInUseFlag(Bool x)
2624{
2625  m_wppNotInUseFlag = x;
2626  if (m_wppNotInUseFlag)
2627  {
2628    for (int i = 0; i < getMaxLayers(); i++)
2629    {
2630      m_wppInUseFlag[i] = m_wppNotInUseFlag;
2631    }
2632  }
2633}
2634#endif
2635#if O0092_0094_DEPENDENCY_CONSTRAINT
2636Void TComVPS::setRefLayersFlags(Int currLayerId)
2637{
2638  for (Int i = 0; i < m_numDirectRefLayers[currLayerId]; i++)
2639  {
2640    UInt refLayerId = getRefLayerId(currLayerId, i);
2641    m_recursiveRefLayerFlag[currLayerId][refLayerId] = true;
2642    for (Int k = 0; k < MAX_NUM_LAYER_IDS; k++)
2643    {
2644      m_recursiveRefLayerFlag[currLayerId][k] = m_recursiveRefLayerFlag[currLayerId][k] | m_recursiveRefLayerFlag[refLayerId][k];
2645    }
2646  }
2647}
2648
2649Void TComVPS::setNumRefLayers()
2650{
2651  memset( m_numberRefLayers, 0, sizeof( m_numberRefLayers ) );
2652
2653  for (Int i = 0; i < m_uiMaxLayers; i++)
2654  {
2655    UInt iNuhLId = m_layerIdInNuh[i];
2656    setRefLayersFlags(iNuhLId);
2657    for (UInt j = 0; j < MAX_NUM_LAYER_IDS; j++)
2658    {
2659      m_numberRefLayers[iNuhLId] += (m_recursiveRefLayerFlag[iNuhLId][j] == true ? 1 : 0);
2660    }
2661  }
2662}
2663#endif
2664
2665#if Q0078_ADD_LAYER_SETS
2666Void TComVPS::setPredictedLayerIds()
2667{
2668  for (UInt i = 0; i < getMaxLayers() - 1; i++)
2669  {
2670    UInt iNuhLId = getLayerIdInNuh(i);
2671    UInt predIdx = 0;
2672    for (UInt j = iNuhLId + 1; j < MAX_NUM_LAYER_IDS; j++)
2673    {
2674      if (getRecursiveRefLayerFlag(j, iNuhLId))
2675      {
2676        setPredictedLayerId(i, predIdx, j);
2677        predIdx++;
2678      }
2679    }
2680    setNumPredictedLayers(iNuhLId, predIdx);
2681  }
2682}
2683
2684Void TComVPS::setTreePartitionLayerIdList()
2685{
2686  Bool countedLayerIdxFlag[MAX_NUM_LAYER_IDS];
2687
2688  for (UInt i = 0; i <= getMaxLayers() - 1; i++)
2689  {
2690    countedLayerIdxFlag[i] = false;
2691  }
2692
2693  Int numIndependentLayers = 0;
2694
2695  for (UInt i = 0; i <= getMaxLayers() - 1; i++)
2696  {
2697    UInt iNuhLId = getLayerIdInNuh(i);
2698    if (getNumDirectRefLayers(iNuhLId) == 0)
2699    {
2700      setTreePartitionLayerId(numIndependentLayers, 0, iNuhLId);
2701      setNumLayersInTreePartition(numIndependentLayers, 1);
2702      for (UInt j = 0; j < getNumPredictedLayers(iNuhLId); j++)
2703      {
2704        if (!countedLayerIdxFlag[getLayerIdInVps(iNuhLId)])
2705        {
2706          setTreePartitionLayerId(numIndependentLayers, getNumLayersInTreePartition(numIndependentLayers), getPredictedLayerId(iNuhLId, j));
2707          setNumLayersInTreePartition(numIndependentLayers, getNumLayersInTreePartition(numIndependentLayers) + 1);
2708          countedLayerIdxFlag[getLayerIdInVps(getPredictedLayerId(iNuhLId, j))] = true;
2709        }
2710      }
2711      numIndependentLayers++;
2712    }
2713  }
2714
2715  setNumIndependentLayers(numIndependentLayers);
2716}
2717
2718void TComVPS::setLayerIdIncludedFlagsForAddLayerSets()
2719{
2720  for (UInt i = 0; i < getNumAddLayerSets(); i++)
2721  {
2722    for (UInt j = 1; j < getNumIndependentLayers(); j++)
2723    {
2724      Int layerNum = 0;
2725      Int lsIdx = getVpsNumLayerSetsMinus1() + 1 + i;
2726      for (Int layerId = 0; layerId < MAX_VPS_LAYER_ID_PLUS1; layerId++)
2727      {
2728        setLayerIdIncludedFlag(false, lsIdx, layerId);
2729      }
2730      for (Int treeIdx = 1; treeIdx < getNumIndependentLayers(); treeIdx++)
2731      {
2732        for (Int layerCnt = 0; layerCnt < getHighestLayerIdxPlus1(i, j); layerCnt++)
2733        {
2734          setLayerSetLayerIdList(lsIdx, layerNum, getTreePartitionLayerId(treeIdx, layerCnt));
2735          setLayerIdIncludedFlag(true, lsIdx, getTreePartitionLayerId(treeIdx, layerCnt));
2736          layerNum++;
2737        }
2738      }
2739      setNumLayersInIdList(lsIdx, layerNum);
2740    }
2741  }
2742}
2743
2744#endif
2745
2746#if VIEW_ID_RELATED_SIGNALING
2747Int TComVPS::getNumViews()
2748{
2749  Int numViews = 1; 
2750  for( Int i = 0; i <= getMaxLayers() - 1; i++ )
2751  {
2752    Int lId = getLayerIdInNuh( i ); 
2753    if ( i > 0 && ( getViewIndex( lId ) != getScalabilityId( i - 1, VIEW_ORDER_INDEX ) ) )
2754    {
2755      numViews++; 
2756    }   
2757  }
2758
2759  return numViews;
2760}
2761Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType )
2762{
2763  return getScalabilityMask( scalType ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
2764} 
2765Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType )
2766{
2767  assert( scalType >= 0 && scalType <= MAX_VPS_NUM_SCALABILITY_TYPES ); 
2768  assert( scalType == MAX_VPS_NUM_SCALABILITY_TYPES || getScalabilityMask( scalType ) );
2769  Int scalIdx = 0; 
2770  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
2771  {
2772    scalIdx += ( getScalabilityMask( curScalType ) ? 1 : 0 );
2773
2774  }
2775
2776  return scalIdx; 
2777}
2778#endif
2779#if VPS_DPB_SIZE_TABLE
2780Void TComVPS::determineSubDpbInfoFlags()
2781{
2782  for(Int i = 1; i < getNumOutputLayerSets(); i++)
2783  {
2784    Int layerSetIdxForOutputLayerSet = getOutputLayerSetIdx( i );
2785    // For each output layer set, set the DPB size for each layer and the reorder/latency value the maximum for all layers
2786    Bool checkFlagOuter = false;      // Used to calculate sub_layer_flag_info_present_flag
2787    Bool checkFlagInner[MAX_TLAYER];  // Used to calculate sub_layer_dpb_info_present_flag
2788
2789    for(Int j = 0; j < getMaxTLayers(); j++)
2790    {
2791      // --------------------------------------------------------
2792      // To determine value of m_subLayerDpbInfoPresentFlag
2793      // --------------------------------------------------------
2794      if( j == 0 )  // checkFlagInner[0] is always 1
2795      {
2796        checkFlagInner[j] = true;     // Always signal sub-layer DPB information for the first sub-layer
2797      }
2798      else
2799      {
2800        checkFlagInner[j] = false;    // Initialize to be false. If the values of the current sub-layers matches with the earlier sub-layer,
2801                                      // then will be continue to be false - i.e. the j-th sub-layer DPB info is not signaled
2802        checkFlagInner[j] |= ( getMaxVpsNumReorderPics(i, j) != getMaxVpsNumReorderPics(i, j - 1) );
2803#if CHANGE_NUMSUBDPB_IDX
2804        for(Int subDpbIdx = 0; subDpbIdx < getNumSubDpbs(layerSetIdxForOutputLayerSet) && !checkFlagInner[j]; subDpbIdx++)  // If checkFlagInner[j] is true, break and signal the values
2805#else
2806        for(Int k = 0; k < getNumSubDpbs(i) && !checkFlagInner[j]; k++)  // If checkFlagInner[j] is true, break and signal the values
2807#endif
2808        {
2809          checkFlagInner[j] |= ( getMaxVpsDecPicBufferingMinus1(i, subDpbIdx, j - 1) != getMaxVpsDecPicBufferingMinus1(i, subDpbIdx, j) );
2810        }
2811#if RESOLUTION_BASED_DPB
2812        for(Int layerIdx = 0; layerIdx < this->getNumLayersInIdList(layerSetIdxForOutputLayerSet) && !checkFlagInner[j]; layerIdx++)  // If checkFlagInner[j] is true, break and signal the values
2813        {
2814          checkFlagInner[j] |= ( getMaxVpsLayerDecPicBuffMinus1(i, layerIdx, j - 1) != getMaxVpsLayerDecPicBuffMinus1(i, layerIdx, j) );
2815        }
2816#endif
2817      }
2818      // If checkFlagInner[j] = true, then some value needs to be signalled for the j-th sub-layer
2819      setSubLayerDpbInfoPresentFlag( i, j, checkFlagInner[j] );
2820    }
2821
2822    // --------------------------------------------------------
2823    // To determine value of m_subLayerFlagInfoPresentFlag
2824    // --------------------------------------------------------
2825
2826    for(Int j = 1; j < getMaxTLayers(); j++) // Check if DPB info of any of non-zero sub-layers is signaled. If so set flag to one
2827    {
2828      if( getSubLayerDpbInfoPresentFlag(i, j) )
2829      {
2830        checkFlagOuter = true;
2831        break;
2832      }
2833    }
2834    setSubLayerFlagInfoPresentFlag( i, checkFlagOuter );
2835  }
2836}
2837#endif
2838#if RESOLUTION_BASED_DPB
2839Void TComVPS::assignSubDpbIndices()
2840{
2841  RepFormat layerRepFormat  [MAX_LAYERS];
2842  RepFormat subDpbRepFormat [MAX_LAYERS];
2843
2844  for(Int lsIdx = 0; lsIdx < this->getNumLayerSets(); lsIdx++)
2845  {
2846    for(Int j = 0; j < MAX_LAYERS; j++)
2847    {
2848      layerRepFormat [j].init();
2849      subDpbRepFormat[j].init();
2850    }
2851
2852    // Assign resolution, bit-depth, colour format for each layer in the layer set
2853    for(Int i = 0; i < this->getNumLayersInIdList( lsIdx ); i++)
2854    {
2855      Int layerIdxInVps = this->getLayerIdInVps( this->getLayerSetLayerIdList(lsIdx, i) );
2856      Int repFormatIdx  = this->getVpsRepFormatIdx( layerIdxInVps );
2857      RepFormat* repFormat = this->getVpsRepFormat( repFormatIdx );
2858
2859      // Assign the rep_format() to the layer
2860      layerRepFormat[i] = *repFormat;
2861    }
2862
2863    // ----------------------------------------
2864    // Sub-DPB assignment
2865    // ----------------------------------------
2866    // For the base layer
2867    m_subDpbAssigned[lsIdx][0] = 0;
2868    subDpbRepFormat[0] = layerRepFormat[0];
2869
2870    // Sub-DPB counter
2871    Int subDpbCtr = 1;
2872
2873    for(Int i = 1; i < this->getNumLayersInIdList( lsIdx ); i++)
2874    {
2875      Bool newSubDpbFlag = true;
2876      for(Int j = 0; (j < subDpbCtr) && (newSubDpbFlag); j++)
2877      {
2878        if( RepFormat::checkSameSubDpb( layerRepFormat[i], subDpbRepFormat[j] ) )
2879        {
2880          // Belong to i-th sub-DPB
2881          m_subDpbAssigned[lsIdx][i] = j;
2882          newSubDpbFlag = false;
2883        }
2884      }
2885      if( newSubDpbFlag )
2886      {
2887        // New sub-DPB
2888        subDpbRepFormat[subDpbCtr] = layerRepFormat[i];
2889        m_subDpbAssigned[lsIdx][i] = subDpbCtr;
2890        subDpbCtr++;                                    // Increment # subDpbs
2891      }
2892    }
2893    m_numSubDpbs[lsIdx] = subDpbCtr;
2894  }
2895}
2896Int  TComVPS::findLayerIdxInLayerSet ( Int lsIdx, Int nuhLayerId )
2897{
2898  for(Int i = 0; i < this->getNumLayersInIdList(lsIdx); i++)
2899  {
2900    if( this->getLayerSetLayerIdList( lsIdx, i) == nuhLayerId )
2901    {
2902      return i;
2903    }
2904  }
2905  return -1;  // Layer not found
2906}
2907#endif
2908#if O0164_MULTI_LAYER_HRD
2909Void TComVPS::setBspHrdParameters( UInt hrdIdx, UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
2910{
2911  if( !getVpsVuiBspHrdPresentFlag() )
2912  {
2913    return;
2914  }
2915
2916  TComHRD *hrd = getBspHrd(hrdIdx);
2917
2918  Bool rateCnt = ( bitRate > 0 );
2919  hrd->setNalHrdParametersPresentFlag( rateCnt );
2920  hrd->setVclHrdParametersPresentFlag( rateCnt );
2921
2922  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
2923
2924  if( hrd->getSubPicCpbParamsPresentFlag() )
2925  {
2926    hrd->setTickDivisorMinus2( 100 - 2 );                          //
2927    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
2928    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
2929    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
2930  }
2931  else
2932  {
2933    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
2934  }
2935
2936  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
2937  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2938  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2939
2940  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
2941  if( randomAccess )
2942  {
2943    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
2944    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
2945  }
2946  else
2947  {
2948    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
2949    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
2950  }
2951
2952  /*
2953  Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
2954  */
2955  Int i, j;
2956  UInt birateValue, cpbSizeValue;
2957  UInt ducpbSizeValue;
2958  UInt duBitRateValue = 0;
2959
2960  for( i = 0; i < MAX_TLAYER; i ++ )
2961  {
2962    hrd->setFixedPicRateFlag( i, 1 );
2963    hrd->setPicDurationInTcMinus1( i, 0 );
2964    hrd->setLowDelayHrdFlag( i, 0 );
2965    hrd->setCpbCntMinus1( i, 0 );
2966
2967    birateValue  = bitRate;
2968    cpbSizeValue = bitRate;                                     // 1 second
2969    ducpbSizeValue = bitRate/numDU;
2970    duBitRateValue = bitRate;
2971    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
2972    {
2973      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
2974      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
2975      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
2976      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
2977
2978      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
2979      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
2980      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
2981      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
2982      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
2983    }
2984  }
2985}
2986#endif
2987#if RESOLUTION_BASED_DPB
2988// RepFormat Assignment operator
2989RepFormat& RepFormat::operator= (const RepFormat &other)
2990{
2991  if( this != &other)
2992  {
2993    m_chromaAndBitDepthVpsPresentFlag = other.m_chromaAndBitDepthVpsPresentFlag;
2994    m_chromaFormatVpsIdc              = other.m_chromaFormatVpsIdc;
2995    m_separateColourPlaneVpsFlag      = other.m_separateColourPlaneVpsFlag;
2996    m_picWidthVpsInLumaSamples        = other.m_picWidthVpsInLumaSamples;
2997    m_picHeightVpsInLumaSamples       = other.m_picHeightVpsInLumaSamples;
2998    m_bitDepthVpsLuma                 = other.m_bitDepthVpsLuma;
2999    m_bitDepthVpsChroma               = other.m_bitDepthVpsChroma;
3000  }
3001  return *this;
3002}
3003
3004// Check whether x and y share the same resolution, chroma format and bit-depth.
3005Bool RepFormat::checkSameSubDpb(const RepFormat &x, const RepFormat &y)
3006{
3007  return (    (x.m_chromaFormatVpsIdc              == y.m_chromaFormatVpsIdc)
3008          &&  (x.m_picWidthVpsInLumaSamples        == y.m_picWidthVpsInLumaSamples)
3009          &&  (x.m_picHeightVpsInLumaSamples       == y.m_picHeightVpsInLumaSamples)
3010          &&  (x.m_bitDepthVpsLuma                 == y.m_bitDepthVpsLuma)
3011          &&  (x.m_bitDepthVpsChroma               == y.m_bitDepthVpsChroma)
3012          );
3013}
3014#endif
3015// ------------------------------------------------------------------------------------------------
3016// Sequence parameter set (SPS)
3017// ------------------------------------------------------------------------------------------------
3018
3019TComSPS::TComSPS()
3020: m_SPSId                     (  0)
3021, m_VPSId                     (  0)
3022, m_chromaFormatIdc           (CHROMA_420)
3023, m_uiMaxTLayers              (  1)
3024#if R0279_REP_FORMAT_INBL
3025, m_bV1CompatibleSPSFlag      (  0)
3026#endif
3027// Structure
3028, m_picWidthInLumaSamples     (352)
3029, m_picHeightInLumaSamples    (288)
3030, m_log2MinCodingBlockSize    (  0)
3031, m_log2DiffMaxMinCodingBlockSize (0)
3032, m_uiMaxCUWidth              ( 32)
3033, m_uiMaxCUHeight             ( 32)
3034, m_uiMaxCUDepth              (  3)
3035, m_bLongTermRefsPresent      (false)
3036, m_uiQuadtreeTULog2MaxSize   (  0)
3037, m_uiQuadtreeTULog2MinSize   (  0)
3038, m_uiQuadtreeTUMaxDepthInter (  0)
3039, m_uiQuadtreeTUMaxDepthIntra (  0)
3040// Tool list
3041, m_usePCM                   (false)
3042, m_pcmLog2MaxSize            (  5)
3043, m_uiPCMLog2MinSize          (  7)
3044, m_bitDepthY                 (  8)
3045, m_bitDepthC                 (  8)
3046, m_qpBDOffsetY               (  0)
3047, m_qpBDOffsetC               (  0)
3048, m_uiPCMBitDepthLuma         (  8)
3049, m_uiPCMBitDepthChroma       (  8)
3050, m_bPCMFilterDisableFlag     (false)
3051, m_uiBitsForPOC              (  8)
3052, m_numLongTermRefPicSPS    (  0) 
3053, m_uiMaxTrSize               ( 32)
3054, m_bUseSAO                   (false) 
3055, m_bTemporalIdNestingFlag    (false)
3056, m_scalingListEnabledFlag    (false)
3057, m_useStrongIntraSmoothing   (false)
3058, m_vuiParametersPresentFlag  (false)
3059, m_vuiParameters             ()
3060#if SVC_EXTENSION
3061, m_layerId                   ( 0 )
3062, m_extensionFlag             ( false )
3063, m_numScaledRefLayerOffsets  ( 0 )
3064#if REPN_FORMAT_IN_VPS
3065, m_updateRepFormatFlag       (false)
3066#if O0096_REP_FORMAT_INDEX
3067, m_updateRepFormatIndex      (0)
3068#endif
3069#endif
3070#if SCALINGLIST_INFERRING
3071, m_inferScalingListFlag ( false )
3072, m_scalingListRefLayerId ( 0 )
3073#endif
3074#endif //SVC_EXTENSION
3075{
3076  for ( Int i = 0; i < MAX_TLAYER; i++ )
3077  {
3078    m_uiMaxLatencyIncrease[i] = 0;
3079    m_uiMaxDecPicBuffering[i] = 1;
3080    m_numReorderPics[i]       = 0;
3081  }
3082  m_scalingList = new TComScalingList;
3083  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
3084  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
3085
3086#if P0312_VERT_PHASE_ADJ
3087  ::memset(m_vertPhasePositionEnableFlag, 0, sizeof(m_vertPhasePositionEnableFlag));
3088#endif
3089}
3090
3091TComSPS::~TComSPS()
3092{
3093#if SCALINGLIST_INFERRING
3094  if( !m_inferScalingListFlag )
3095#endif
3096  delete m_scalingList;
3097  m_RPSList.destroy();
3098}
3099
3100Void  TComSPS::createRPSList( Int numRPS )
3101{ 
3102  m_RPSList.destroy();
3103  m_RPSList.create(numRPS);
3104}
3105
3106Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
3107{
3108  if( !getVuiParametersPresentFlag() )
3109  {
3110    return;
3111  }
3112
3113  TComVUI *vui = getVuiParameters();
3114  TComHRD *hrd = vui->getHrdParameters();
3115
3116  TimingInfo *timingInfo = vui->getTimingInfo();
3117#if SVC_EXTENSION
3118  if( m_layerId > 0 )
3119  {
3120    timingInfo->setTimingInfoPresentFlag( false );
3121    return;
3122  }
3123#endif
3124
3125  timingInfo->setTimingInfoPresentFlag( true );
3126  switch( frameRate )
3127  {
3128  case 24:
3129    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
3130    break;
3131  case 25:
3132    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
3133    break;
3134  case 30:
3135    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
3136    break;
3137  case 50:
3138    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
3139    break;
3140  case 60:
3141    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
3142    break;
3143  default:
3144    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
3145    break;
3146  }
3147
3148  Bool rateCnt = ( bitRate > 0 );
3149  hrd->setNalHrdParametersPresentFlag( rateCnt );
3150  hrd->setVclHrdParametersPresentFlag( rateCnt );
3151
3152  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
3153
3154  if( hrd->getSubPicCpbParamsPresentFlag() )
3155  {
3156    hrd->setTickDivisorMinus2( 100 - 2 );                          //
3157    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
3158    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
3159    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
3160  }
3161  else
3162  {
3163    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
3164  }
3165
3166  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
3167  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
3168  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
3169
3170  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
3171  if( randomAccess )
3172  {
3173    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
3174    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
3175  }
3176  else
3177  {
3178    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
3179    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
3180  }
3181
3182  /*
3183  Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
3184  */
3185  Int i, j;
3186  UInt birateValue, cpbSizeValue;
3187  UInt ducpbSizeValue;
3188  UInt duBitRateValue = 0;
3189
3190  for( i = 0; i < MAX_TLAYER; i ++ )
3191  {
3192    hrd->setFixedPicRateFlag( i, 1 );
3193    hrd->setPicDurationInTcMinus1( i, 0 );
3194    hrd->setLowDelayHrdFlag( i, 0 );
3195    hrd->setCpbCntMinus1( i, 0 );
3196
3197    birateValue  = bitRate;
3198    cpbSizeValue = bitRate;                                     // 1 second
3199    ducpbSizeValue = bitRate/numDU;
3200    duBitRateValue = bitRate;
3201    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
3202    {
3203      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
3204      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
3205      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
3206      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
3207
3208      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
3209      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
3210      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
3211      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
3212      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
3213    }
3214  }
3215}
3216const Int TComSPS::m_winUnitX[]={1,2,2,1};
3217const Int TComSPS::m_winUnitY[]={1,2,1,1};
3218
3219#if O0098_SCALED_REF_LAYER_ID
3220Window& TComSPS::getScaledRefLayerWindowForLayer(Int layerId)
3221{
3222  static Window win;
3223
3224  for (Int i = 0; i < m_numScaledRefLayerOffsets; i++)
3225  {
3226    if (layerId == m_scaledRefLayerId[i])
3227    {
3228      return m_scaledRefLayerWindow[i];
3229    }
3230  }
3231
3232  win.resetWindow();  // scaled reference layer offsets are inferred to be zero when not present
3233  return win;
3234}
3235#endif
3236
3237TComPPS::TComPPS()
3238: m_PPSId                       (0)
3239, m_SPSId                       (0)
3240, m_picInitQPMinus26            (0)
3241, m_useDQP                      (false)
3242, m_bConstrainedIntraPred       (false)
3243, m_bSliceChromaQpFlag          (false)
3244, m_pcSPS                       (NULL)
3245, m_uiMaxCuDQPDepth             (0)
3246, m_uiMinCuDQPSize              (0)
3247, m_chromaCbQpOffset            (0)
3248, m_chromaCrQpOffset            (0)
3249, m_numRefIdxL0DefaultActive    (1)
3250, m_numRefIdxL1DefaultActive    (1)
3251, m_TransquantBypassEnableFlag  (false)
3252, m_useTransformSkip             (false)
3253, m_dependentSliceSegmentsEnabledFlag    (false)
3254, m_tilesEnabledFlag               (false)
3255, m_entropyCodingSyncEnabledFlag   (false)
3256, m_loopFilterAcrossTilesEnabledFlag  (true)
3257, m_uniformSpacingFlag           (false)
3258, m_numTileColumnsMinus1         (0)
3259, m_numTileRowsMinus1            (0)
3260, m_numSubstreams               (1)
3261, m_signHideFlag(0)
3262, m_cabacInitPresentFlag        (false)
3263, m_encCABACTableIdx            (I_SLICE)
3264, m_sliceHeaderExtensionPresentFlag    (false)
3265, m_loopFilterAcrossSlicesEnabledFlag (false)
3266, m_listsModificationPresentFlag(  0)
3267, m_numExtraSliceHeaderBits(0)
3268#if SVC_EXTENSION
3269, m_extensionFlag(false)
3270#if SCALINGLIST_INFERRING
3271, m_layerId(0)
3272, m_inferScalingListFlag ( false )
3273, m_scalingListRefLayerId ( 0 )
3274#endif
3275#if POC_RESET_IDC
3276, m_pocResetInfoPresentFlag   (false)
3277#endif
3278#if Q0048_CGS_3D_ASYMLUT
3279, m_nCGSFlag(0)
3280, m_nCGSOutputBitDepthY(0)
3281, m_nCGSOutputBitDepthC(0)
3282#endif
3283#endif //SVC_EXTENSION
3284{
3285  m_scalingList = new TComScalingList;
3286}
3287
3288TComPPS::~TComPPS()
3289{
3290#if SCALINGLIST_INFERRING
3291  if( !m_inferScalingListFlag )
3292#endif
3293  delete m_scalingList;
3294}
3295
3296TComReferencePictureSet::TComReferencePictureSet()
3297: m_numberOfPictures (0)
3298, m_numberOfNegativePictures (0)
3299, m_numberOfPositivePictures (0)
3300, m_numberOfLongtermPictures (0)
3301, m_interRPSPrediction (0) 
3302, m_deltaRIdxMinus1 (0)   
3303, m_deltaRPS (0) 
3304, m_numRefIdc (0) 
3305{
3306  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
3307  ::memset( m_POC, 0, sizeof(m_POC) );
3308  ::memset( m_used, 0, sizeof(m_used) );
3309  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
3310}
3311
3312TComReferencePictureSet::~TComReferencePictureSet()
3313{
3314}
3315
3316Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
3317{
3318  m_used[bufferNum] = used;
3319}
3320
3321Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
3322{
3323  m_deltaPOC[bufferNum] = deltaPOC;
3324}
3325
3326Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
3327{
3328  m_numberOfPictures = numberOfPictures;
3329}
3330
3331Int TComReferencePictureSet::getUsed(Int bufferNum)
3332{
3333  return m_used[bufferNum];
3334}
3335
3336Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
3337{
3338  return m_deltaPOC[bufferNum];
3339}
3340
3341Int TComReferencePictureSet::getNumberOfPictures()
3342{
3343  return m_numberOfPictures;
3344}
3345
3346Int TComReferencePictureSet::getPOC(Int bufferNum)
3347{
3348  return m_POC[bufferNum];
3349}
3350
3351Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
3352{
3353  m_POC[bufferNum] = POC;
3354}
3355
3356Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
3357{
3358  return m_bCheckLTMSB[bufferNum];
3359}
3360
3361Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
3362{
3363  m_bCheckLTMSB[bufferNum] = b;
3364}
3365
3366/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
3367 * \param uiBufferNum
3368 * \param iRefIdc
3369 * \returns Void
3370 */
3371Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
3372{
3373  m_refIdc[bufferNum] = refIdc;
3374}
3375
3376/** get the reference idc value at uiBufferNum
3377 * \param uiBufferNum
3378 * \returns Int
3379 */
3380Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
3381{
3382  return m_refIdc[bufferNum];
3383}
3384
3385/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
3386 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
3387 *  +ve values are in increasing order.
3388 * \returns Void
3389 */
3390Void TComReferencePictureSet::sortDeltaPOC()
3391{
3392  // sort in increasing order (smallest first)
3393  for(Int j=1; j < getNumberOfPictures(); j++)
3394  { 
3395    Int deltaPOC = getDeltaPOC(j);
3396    Bool used = getUsed(j);
3397    for (Int k=j-1; k >= 0; k--)
3398    {
3399      Int temp = getDeltaPOC(k);
3400      if (deltaPOC < temp)
3401      {
3402        setDeltaPOC(k+1, temp);
3403        setUsed(k+1, getUsed(k));
3404        setDeltaPOC(k, deltaPOC);
3405        setUsed(k, used);
3406      }
3407    }
3408  }
3409  // flip the negative values to largest first
3410  Int numNegPics = getNumberOfNegativePictures();
3411  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
3412  { 
3413    Int deltaPOC = getDeltaPOC(j);
3414    Bool used = getUsed(j);
3415    setDeltaPOC(j, getDeltaPOC(k));
3416    setUsed(j, getUsed(k));
3417    setDeltaPOC(k, deltaPOC);
3418    setUsed(k, used);
3419  }
3420}
3421
3422/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
3423 *  A "*" is added to the deltaPOC value if it is Used bu current.
3424 * \returns Void
3425 */
3426Void TComReferencePictureSet::printDeltaPOC()
3427{
3428  printf("DeltaPOC = { ");
3429  for(Int j=0; j < getNumberOfPictures(); j++)
3430  {
3431    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
3432  } 
3433  if (getInterRPSPrediction()) 
3434  {
3435    printf("}, RefIdc = { ");
3436    for(Int j=0; j < getNumRefIdc(); j++)
3437    {
3438      printf("%d ", getRefIdc(j));
3439    } 
3440  }
3441  printf("}\n");
3442}
3443
3444TComRPSList::TComRPSList()
3445:m_referencePictureSets (NULL)
3446{
3447}
3448
3449TComRPSList::~TComRPSList()
3450{
3451}
3452
3453Void TComRPSList::create( Int numberOfReferencePictureSets)
3454{
3455  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
3456  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
3457}
3458
3459Void TComRPSList::destroy()
3460{
3461  if (m_referencePictureSets)
3462  {
3463    delete [] m_referencePictureSets;
3464  }
3465  m_numberOfReferencePictureSets = 0;
3466  m_referencePictureSets = NULL;
3467}
3468
3469
3470
3471TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
3472{
3473  return &m_referencePictureSets[referencePictureSetNum];
3474}
3475
3476Int TComRPSList::getNumberOfReferencePictureSets()
3477{
3478  return m_numberOfReferencePictureSets;
3479}
3480
3481Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
3482{
3483  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
3484}
3485
3486TComRefPicListModification::TComRefPicListModification()
3487: m_bRefPicListModificationFlagL0 (false)
3488, m_bRefPicListModificationFlagL1 (false)
3489{
3490  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
3491  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
3492}
3493
3494TComRefPicListModification::~TComRefPicListModification()
3495{
3496}
3497
3498TComScalingList::TComScalingList()
3499{
3500  init();
3501}
3502
3503TComScalingList::~TComScalingList()
3504{
3505  destroy();
3506}
3507
3508/** set default quantization matrix to array
3509*/
3510Void TComSlice::setDefaultScalingList()
3511{
3512  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3513  {
3514    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
3515    {
3516      getScalingList()->processDefaultMatrix(sizeId, listId);
3517    }
3518  }
3519}
3520/** check if use default quantization matrix
3521 * \returns true if use default quantization matrix in all size
3522*/
3523Bool TComSlice::checkDefaultScalingList()
3524{
3525  UInt defaultCounter=0;
3526
3527  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3528  {
3529    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
3530    {
3531      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
3532     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
3533      {
3534        defaultCounter++;
3535      }
3536    }
3537  }
3538  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
3539}
3540
3541/** get scaling matrix from RefMatrixID
3542 * \param sizeId size index
3543 * \param Index of input matrix
3544 * \param Index of reference matrix
3545 */
3546Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
3547{
3548  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3549}
3550
3551/** parse syntax infomation
3552 *  \param pchFile syntax infomation
3553 *  \returns false if successful
3554 */
3555Bool TComScalingList::xParseScalingList(Char* pchFile)
3556{
3557  FILE *fp;
3558  Char line[1024];
3559  UInt sizeIdc,listIdc;
3560  UInt i,size = 0;
3561  Int *src=0,data;
3562  Char *ret;
3563  UInt  retval;
3564
3565  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
3566  {
3567    printf("can't open file %s :: set Default Matrix\n",pchFile);
3568    return true;
3569  }
3570
3571  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
3572  {
3573    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
3574    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
3575    {
3576      src = getScalingListAddress(sizeIdc, listIdc);
3577
3578      fseek(fp,0,0);
3579      do 
3580      {
3581        ret = fgets(line, 1024, fp);
3582        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
3583        {
3584          printf("Error: can't read Matrix :: set Default Matrix\n");
3585          return true;
3586        }
3587      }
3588      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
3589      for (i=0; i<size; i++)
3590      {
3591        retval = fscanf(fp, "%d,", &data);
3592        if (retval!=1)
3593        {
3594          printf("Error: can't read Matrix :: set Default Matrix\n");
3595          return true;
3596        }
3597        src[i] = data;
3598      }
3599      //set DC value for default matrix check
3600      setScalingListDC(sizeIdc,listIdc,src[0]);
3601
3602      if(sizeIdc > SCALING_LIST_8x8)
3603      {
3604        fseek(fp,0,0);
3605        do 
3606        {
3607          ret = fgets(line, 1024, fp);
3608          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
3609          {
3610            printf("Error: can't read DC :: set Default Matrix\n");
3611            return true;
3612          }
3613        }
3614        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
3615        retval = fscanf(fp, "%d,", &data);
3616        if (retval!=1)
3617        {
3618          printf("Error: can't read Matrix :: set Default Matrix\n");
3619          return true;
3620        }
3621        //overwrite DC value when size of matrix is larger than 16x16
3622        setScalingListDC(sizeIdc,listIdc,data);
3623      }
3624    }
3625  }
3626  fclose(fp);
3627  return false;
3628}
3629
3630/** initialization process of quantization matrix array
3631 */
3632Void TComScalingList::init()
3633{
3634  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3635  {
3636    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
3637    {
3638      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
3639    }
3640  }
3641  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
3642}
3643
3644/** destroy quantization matrix array
3645 */
3646Void TComScalingList::destroy()
3647{
3648  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3649  {
3650    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
3651    {
3652      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
3653    }
3654  }
3655}
3656
3657/** get default address of quantization matrix
3658 * \param sizeId size index
3659 * \param listId list index
3660 * \returns pointer of quantization matrix
3661 */
3662Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
3663{
3664  Int *src = 0;
3665  switch(sizeId)
3666  {
3667    case SCALING_LIST_4x4:
3668      src = g_quantTSDefault4x4;
3669      break;
3670    case SCALING_LIST_8x8:
3671      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
3672      break;
3673    case SCALING_LIST_16x16:
3674      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
3675      break;
3676    case SCALING_LIST_32x32:
3677      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
3678      break;
3679    default:
3680      assert(0);
3681      src = NULL;
3682      break;
3683  }
3684  return src;
3685}
3686
3687/** process of default matrix
3688 * \param sizeId size index
3689 * \param Index of input matrix
3690 */
3691Void TComScalingList::processDefaultMatrix(UInt sizeId, UInt listId)
3692{
3693  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3694  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
3695}
3696
3697/** check DC value of matrix for default matrix signaling
3698 */
3699Void TComScalingList::checkDcOfMatrix()
3700{
3701  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3702  {
3703    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
3704    {
3705      //check default matrix?
3706      if(getScalingListDC(sizeId,listId) == 0)
3707      {
3708        processDefaultMatrix(sizeId, listId);
3709      }
3710    }
3711  }
3712}
3713
3714ParameterSetManager::ParameterSetManager()
3715#if SVC_EXTENSION
3716: m_activeSPSId(-1)
3717, m_activePPSId(-1)
3718#else
3719: m_vpsMap(MAX_NUM_VPS)
3720, m_spsMap(MAX_NUM_SPS)
3721, m_ppsMap(MAX_NUM_PPS)
3722, m_activeVPSId(-1)
3723, m_activeSPSId(-1)
3724, m_activePPSId(-1)
3725#endif
3726{
3727}
3728
3729
3730ParameterSetManager::~ParameterSetManager()
3731{
3732}
3733
3734//! activate a SPS from a active parameter sets SEI message
3735//! \returns true, if activation is successful
3736Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
3737{
3738  TComSPS *sps = m_spsMap.getPS(spsId);
3739  if (sps)
3740  {
3741    Int vpsId = sps->getVPSId();
3742    if (m_vpsMap.getPS(vpsId))
3743    {
3744      m_activeVPSId = vpsId;
3745      m_activeSPSId = spsId;
3746      return true;
3747    }
3748    else
3749    {
3750      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
3751    }
3752  }
3753  else
3754  {
3755    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
3756  }
3757  return false;
3758}
3759
3760//! activate a PPS and depending on isIDR parameter also SPS and VPS
3761//! \returns true, if activation is successful
3762Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
3763{
3764  TComPPS *pps = m_ppsMap.getPS(ppsId);
3765  if (pps)
3766  {
3767    Int spsId = pps->getSPSId();
3768    if (!isIRAP && (spsId != m_activeSPSId))
3769    {
3770      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
3771      return false;
3772    }
3773    TComSPS *sps = m_spsMap.getPS(spsId);
3774    if (sps)
3775    {
3776      Int vpsId = sps->getVPSId();
3777      if (!isIRAP && (vpsId != m_activeVPSId))
3778      {
3779        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
3780        return false;
3781      }
3782      if (m_vpsMap.getPS(vpsId))
3783      {
3784        m_activePPSId = ppsId;
3785        m_activeVPSId = vpsId;
3786        m_activeSPSId = spsId;
3787
3788        return true;
3789      }
3790      else
3791      {
3792        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
3793      }
3794    }
3795    else
3796    {
3797      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
3798    }
3799  }
3800  else
3801  {
3802    printf("Warning: tried to activate non-existing PPS.");
3803  }
3804  return false;
3805}
3806
3807ProfileTierLevel::ProfileTierLevel()
3808  : m_profileSpace    (0)
3809  , m_tierFlag        (false)
3810  , m_profileIdc      (0)
3811  , m_levelIdc        (0)
3812, m_progressiveSourceFlag  (false)
3813, m_interlacedSourceFlag   (false)
3814, m_nonPackedConstraintFlag(false)
3815, m_frameOnlyConstraintFlag(false)
3816{
3817  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
3818}
3819#if VPS_EXTN_PROFILE_INFO
3820Void ProfileTierLevel::copyProfileInfo(ProfileTierLevel *ptl)
3821{
3822  this->setProfileSpace          ( ptl->getProfileSpace()      );
3823  this->setTierFlag              ( ptl->getTierFlag()          );
3824  this->setProfileIdc            ( ptl->getProfileIdc()        );
3825  for(Int j = 0; j < 32; j++)
3826  {
3827    this->setProfileCompatibilityFlag(j, ptl->getProfileCompatibilityFlag(j));
3828  }
3829  this->setProgressiveSourceFlag  ( ptl->getProgressiveSourceFlag()  );
3830  this->setInterlacedSourceFlag   ( ptl->getInterlacedSourceFlag()   );
3831  this->setNonPackedConstraintFlag( ptl->getNonPackedConstraintFlag());
3832  this->setFrameOnlyConstraintFlag( ptl->getFrameOnlyConstraintFlag()); 
3833}
3834#endif
3835
3836TComPTL::TComPTL()
3837{
3838  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
3839  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
3840}
3841#if VPS_EXTN_PROFILE_INFO
3842Void TComPTL::copyProfileInfo(TComPTL *ptl)
3843{
3844  // Copy all information related to general profile
3845  this->getGeneralPTL()->copyProfileInfo(ptl->getGeneralPTL());
3846}
3847#endif
3848
3849#if SVC_EXTENSION
3850Bool TComSlice::setBaseColPic(  TComList<TComPic*>& rcListPic, UInt refLayerIdc )
3851{ 
3852  if(m_layerId == 0)
3853  {
3854    memset( m_pcBaseColPic, 0, sizeof( m_pcBaseColPic ) );
3855    return false;
3856  }       
3857#if POC_RESET_FLAG || POC_RESET_IDC_DECODER
3858#if POC_RESET_IDC_DECODER
3859  TComPic* pic = xGetRefPic( rcListPic, getPOC() );
3860#else
3861  TComPic* pic = xGetRefPic( rcListPic, m_bPocResetFlag ? 0 : m_iPOC );
3862#endif
3863
3864  if( pic )
3865  {
3866    setBaseColPic(refLayerIdc, pic );
3867  }
3868  else
3869  {
3870    return false;
3871  }
3872 
3873  return true;
3874#else
3875  setBaseColPic(refLayerIdc, xGetRefPic(rcListPic, getPOC()));
3876  return true;
3877#endif
3878}
3879
3880#if MFM_ENCCONSTRAINT
3881TComPic* TComSlice::getBaseColPic(  TComList<TComPic*>& rcListPic )
3882{
3883#if POC_RESET_FLAG
3884  return xGetRefPic( rcListPic, m_bPocResetFlag ? 0 : m_iPOC );
3885#else
3886  return xGetRefPic( rcListPic, m_iPOC );
3887#endif
3888}
3889#endif
3890
3891Void TComSlice::setILRPic(TComPic **pcIlpPic)
3892{
3893  for( Int i = 0; i < m_activeNumILRRefIdx; i++ )
3894  {
3895    Int refLayerIdc = m_interLayerPredLayerIdc[i];
3896
3897    if( pcIlpPic[refLayerIdc] )
3898    {
3899      TComPic* pcRefPicBL = m_pcBaseColPic[refLayerIdc];
3900
3901      // copy scalability ratio, it is needed to get the corect location for the motion field of the corresponding reference layer block
3902      pcIlpPic[refLayerIdc]->setSpatialEnhLayerFlag( refLayerIdc, m_pcPic->isSpatialEnhLayer(refLayerIdc) );
3903
3904      pcIlpPic[refLayerIdc]->copyUpsampledPictureYuv( m_pcPic->getFullPelBaseRec( refLayerIdc ), pcIlpPic[refLayerIdc]->getPicYuvRec() );     
3905      pcIlpPic[refLayerIdc]->getSlice(0)->setBaseColPic( refLayerIdc, pcRefPicBL );
3906
3907      //set reference picture POC of each ILP reference
3908      pcIlpPic[refLayerIdc]->getSlice(0)->setPOC( m_iPOC );
3909
3910      //set temporal Id
3911      pcIlpPic[refLayerIdc]->getSlice(0)->setTLayer( m_uiTLayer );
3912
3913      //copy layer id from the reference layer
3914      pcIlpPic[refLayerIdc]->setLayerId( pcRefPicBL->getLayerId() );
3915
3916      pcIlpPic[refLayerIdc]->getPicYuvRec()->setBorderExtension( false );
3917      pcIlpPic[refLayerIdc]->getPicYuvRec()->extendPicBorder();
3918      for (Int j=0; j<pcIlpPic[refLayerIdc]->getPicSym()->getNumberOfCUsInFrame(); j++)    // set reference CU layerId
3919      {
3920        pcIlpPic[refLayerIdc]->getPicSym()->getCU(j)->setLayerId( pcIlpPic[refLayerIdc]->getLayerId() );
3921      }
3922      pcIlpPic[refLayerIdc]->setIsLongTerm(1);
3923
3924#if REF_IDX_MFM
3925      if( m_bMFMEnabledFlag && !(m_eNalUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP && m_eNalUnitType <= NAL_UNIT_CODED_SLICE_CRA) )
3926      {
3927        //set reference picture POC of each ILP reference
3928        assert( pcIlpPic[refLayerIdc]->getPOC() == pcRefPicBL->getPOC() );
3929
3930        //copy slice type from the reference layer
3931        pcIlpPic[refLayerIdc]->getSlice(0)->setSliceType( pcRefPicBL->getSlice(0)->getSliceType() );
3932
3933        //copy "used for reference"
3934        pcIlpPic[refLayerIdc]->getSlice(0)->setReferenced( pcRefPicBL->getSlice(0)->isReferenced() );
3935
3936        for( Int refList = 0; refList < 2; refList++ )
3937        {
3938          RefPicList refPicList = RefPicList( refList );
3939
3940          //set reference POC of ILP
3941          pcIlpPic[refLayerIdc]->getSlice(0)->setNumRefIdx(refPicList, pcRefPicBL->getSlice(0)->getNumRefIdx(refPicList));
3942          assert(pcIlpPic[refLayerIdc]->getSlice(0)->getNumRefIdx(refPicList) >= 0);
3943          assert(pcIlpPic[refLayerIdc]->getSlice(0)->getNumRefIdx(refPicList) <= MAX_NUM_REF);
3944
3945          //initialize reference POC of ILP
3946          for(Int refIdx = 0; refIdx < pcRefPicBL->getSlice(0)->getNumRefIdx(refPicList); refIdx++)
3947          {
3948            pcIlpPic[refLayerIdc]->getSlice(0)->setRefPOC(pcRefPicBL->getSlice(0)->getRefPOC(refPicList, refIdx), refPicList, refIdx);
3949            pcIlpPic[refLayerIdc]->getSlice(0)->setRefPic(pcRefPicBL->getSlice(0)->getRefPic(refPicList, refIdx), refPicList, refIdx);
3950          }
3951
3952          for(Int refIdx = pcRefPicBL->getSlice(0)->getNumRefIdx(refPicList); refIdx < MAX_NUM_REF; refIdx++) 
3953          { 
3954            pcIlpPic[refLayerIdc]->getSlice(0)->setRefPOC(0, refPicList, refIdx); 
3955            pcIlpPic[refLayerIdc]->getSlice(0)->setRefPic(NULL, refPicList, refIdx); 
3956          }
3957
3958          //copy reference pictures' marking from the reference layer
3959          for(Int j = 0; j < MAX_NUM_REF + 1; j++)
3960          {
3961            pcIlpPic[refLayerIdc]->getSlice(0)->setIsUsedAsLongTerm(refList, j, pcRefPicBL->getSlice(0)->getIsUsedAsLongTerm(refList, j));
3962          }
3963        }
3964
3965        pcIlpPic[refLayerIdc]->copyUpsampledMvField( refLayerIdc, m_pcBaseColPic[refLayerIdc] );
3966      }
3967      else
3968      {
3969        pcIlpPic[refLayerIdc]->initUpsampledMvField();
3970      }
3971#endif
3972
3973#if O0225_MAX_TID_FOR_REF_LAYERS
3974      Int maxTidIlRefPicsPlus1 = m_pcVPS->getMaxTidIlRefPicsPlus1( pcIlpPic[refLayerIdc]->getSlice(0)->getLayerId(), m_layerId );
3975#else
3976      Int maxTidIlRefPicsPlus1 = m_pcVPS->getMaxTidIlRefPicsPlus1( pcIlpPic[refLayerIdc]->getSlice(0)->getLayerId() );
3977#endif
3978      assert( (Int)pcIlpPic[refLayerIdc]->getSlice(0)->getTLayer() < maxTidIlRefPicsPlus1 || ( !maxTidIlRefPicsPlus1 && pcIlpPic[refLayerIdc]->getSlice(0)->getRapPicFlag() ) );
3979
3980    }
3981  }
3982}
3983#endif //SVC_EXTENSION
3984
3985//! \}
Note: See TracBrowser for help on using the repository browser.