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

Last change on this file since 368 was 361, checked in by seregin, 12 years ago

LTR marking for temporal pictures

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