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

Last change on this file since 340 was 337, checked in by etri, 12 years ago

N0120 max_tid_il_ref_pics_plus1_present_flag

  • Property svn:eol-style set to native
File size: 80.7 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    m_bIsUsedAsLongTerm[0][rIdx] = ( cIdx >= NumPocStCurr0 + NumPocStCurr1 );
682  }
683  if ( m_eSliceType != B_SLICE )
684  {
685    m_aiNumRefIdx[1] = 0;
686    ::memset( m_apcRefPicList[1], 0, sizeof(m_apcRefPicList[1]));
687  }
688  else
689  {
690    for (Int rIdx = 0; rIdx < m_aiNumRefIdx[1]; rIdx ++)
691    {
692      cIdx = m_RefPicListModification.getRefPicListModificationFlagL1() ? m_RefPicListModification.getRefPicSetIdxL1(rIdx) : rIdx % numPocTotalCurr;
693      assert(cIdx >= 0 && cIdx < numPocTotalCurr);
694      m_apcRefPicList[1][rIdx] = rpsCurrList1[ cIdx ];
695      m_bIsUsedAsLongTerm[1][rIdx] = ( cIdx >= NumPocStCurr0 + NumPocStCurr1 );
696    }
697  }
698}
699
700#if REF_IDX_FRAMEWORK
701Void TComSlice::setRefPicListModificationSvc()
702{
703  if( !m_pcPPS->getListsModificationPresentFlag()) 
704  {
705    return;
706  }
707
708  if(m_eNalUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP && m_eNalUnitType <= NAL_UNIT_CODED_SLICE_CRA)
709  {
710    return;
711  }
712
713  TComRefPicListModification* refPicListModification = &m_RefPicListModification;
714  Int numberOfRpsCurrTempList = this->getNumRpsCurrTempList();  // total number of ref pics in listTemp0 including inter-layer ref pics
715#if RPL_INIT_N0316_N0082
716  Int numberOfPocBeforeCurr = this->getNumNegativeRpsCurrTempList();  // number of negative temporal ref pics
717#endif
718
719  assert(m_aiNumRefIdx[REF_PIC_LIST_0] > 1);
720  assert(m_aiNumRefIdx[REF_PIC_LIST_1] > 1);
721
722  //set L0 inter-layer reference picture modification
723#if RPL_INIT_N0316_N0082
724  Bool hasModification = (m_aiNumRefIdx[REF_PIC_LIST_0] == (numberOfPocBeforeCurr + m_activeNumILRRefIdx)) ? false : true;
725#else
726  Bool hasModification = (m_aiNumRefIdx[REF_PIC_LIST_0] == numberOfRpsCurrTempList) ? false : true;
727#endif
728#if FINAL_RPL_CHANGE_N0082
729  hasModification = false; //modification is not necessary
730#endif
731  refPicListModification->setRefPicListModificationFlagL0(hasModification);
732  if(hasModification)
733  { 
734    for(Int i = 0; i < min(m_aiNumRefIdx[REF_PIC_LIST_0], numberOfRpsCurrTempList); i++)
735    {
736      refPicListModification->setRefPicSetIdxL0(i, i);
737    }
738    if(m_aiNumRefIdx[REF_PIC_LIST_0] > numberOfRpsCurrTempList)
739    {
740        // repeat last ref pic when the number of active ref idx are more than RPS entries
741      for (Int i = numberOfRpsCurrTempList; i < m_aiNumRefIdx[REF_PIC_LIST_0]; i ++)
742      {
743        refPicListModification->setRefPicSetIdxL0(i, numberOfRpsCurrTempList - 1);
744      }
745    }
746    else
747    {
748#if JCTVC_M0458_INTERLAYER_RPS_SIG
749      for(Int i = m_activeNumILRRefIdx; i > 0; i-- )
750#else
751      for(Int i = m_numILRRefIdx; i > 0; i-- )
752#endif
753      {
754#if RPL_INIT_N0316_N0082
755        if((numberOfPocBeforeCurr) >= m_aiNumRefIdx[REF_PIC_LIST_0])
756          refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfPocBeforeCurr);
757        else
758        {
759          refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfPocBeforeCurr);
760          for (Int j = numberOfPocBeforeCurr; j < (m_aiNumRefIdx[REF_PIC_LIST_0] - i); j++)
761            refPicListModification->setRefPicSetIdxL0(j, j + m_activeNumILRRefIdx);
762        }
763#else
764        refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfRpsCurrTempList - i);
765#endif
766      }
767    }
768  }
769
770  //set L1 inter-layer reference picture modification
771  hasModification = (m_aiNumRefIdx[REF_PIC_LIST_1] == numberOfRpsCurrTempList) ? false : true;
772  refPicListModification->setRefPicListModificationFlagL1(hasModification);
773  if(hasModification)
774  { 
775    for(Int i = 0; i < min(m_aiNumRefIdx[REF_PIC_LIST_1], numberOfRpsCurrTempList); i++)
776    {
777      refPicListModification->setRefPicSetIdxL1(i, i);
778    }
779    if(m_aiNumRefIdx[REF_PIC_LIST_1] > numberOfRpsCurrTempList)
780    {
781      for (Int i = numberOfRpsCurrTempList; i < m_aiNumRefIdx[REF_PIC_LIST_1]; i ++)
782      {
783        // repeat last ref pic when the number of active ref idx are more than RPS entries
784        refPicListModification->setRefPicSetIdxL1(i, numberOfRpsCurrTempList - 1); 
785      }
786    }
787    else
788    {
789#if JCTVC_M0458_INTERLAYER_RPS_SIG
790      for(Int i = m_activeNumILRRefIdx; i > 0; i-- )
791#else
792      for(Int i = m_numILRRefIdx; i > 0; i-- )
793#endif
794      {
795        refPicListModification->setRefPicSetIdxL1(m_aiNumRefIdx[REF_PIC_LIST_1] - i, numberOfRpsCurrTempList - i);
796      }
797    }
798  }
799  return;
800}
801#endif
802#if RPL_INIT_N0316_N0082
803Int TComSlice::getNumNegativeRpsCurrTempList()
804{
805  if( m_eSliceType == I_SLICE ) 
806  {
807    return 0;
808  }
809
810  Int numPocBeforeCurr = 0;
811  for( UInt i = 0; i < m_pcRPS->getNumberOfNegativePictures(); i++ )
812  {
813    if(m_pcRPS->getUsed(i))
814    {
815      numPocBeforeCurr++;
816    }
817  }
818
819  return numPocBeforeCurr;
820}
821#endif
822Int TComSlice::getNumRpsCurrTempList()
823{
824  Int numRpsCurrTempList = 0;
825
826#if REF_IDX_FRAMEWORK
827  if( m_eSliceType == I_SLICE || ( m_pcSPS->getLayerId() && 
828    (m_eNalUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP) &&
829    (m_eNalUnitType <= NAL_UNIT_CODED_SLICE_CRA) ) )
830#else
831  if (m_eSliceType == I_SLICE) 
832#endif
833  {
834#if REF_IDX_FRAMEWORK
835#if JCTVC_M0458_INTERLAYER_RPS_SIG
836    return m_activeNumILRRefIdx;
837#else
838    return getNumILRRefIdx();
839#endif
840#else
841    return 0;
842#endif
843  }
844  for(UInt i=0; i < m_pcRPS->getNumberOfNegativePictures()+ m_pcRPS->getNumberOfPositivePictures() + m_pcRPS->getNumberOfLongtermPictures(); i++)
845  {
846    if(m_pcRPS->getUsed(i))
847    {
848      numRpsCurrTempList++;
849    }
850  }
851#if REF_IDX_FRAMEWORK
852  if( m_layerId > 0 )
853  {
854#if JCTVC_M0458_INTERLAYER_RPS_SIG
855    numRpsCurrTempList += m_activeNumILRRefIdx;
856#else
857    numRpsCurrTempList += getNumILRRefIdx();
858#endif
859  }
860#endif
861
862  return numRpsCurrTempList;
863}
864
865Void TComSlice::initEqualRef()
866{
867  for (Int iDir = 0; iDir < 2; iDir++)
868  {
869    for (Int iRefIdx1 = 0; iRefIdx1 < MAX_NUM_REF; iRefIdx1++)
870    {
871      for (Int iRefIdx2 = iRefIdx1; iRefIdx2 < MAX_NUM_REF; iRefIdx2++)
872      {
873        m_abEqualRef[iDir][iRefIdx1][iRefIdx2] = m_abEqualRef[iDir][iRefIdx2][iRefIdx1] = (iRefIdx1 == iRefIdx2? true : false);
874      }
875    }
876  }
877}
878
879Void TComSlice::checkColRefIdx(UInt curSliceIdx, TComPic* pic)
880{
881  Int i;
882  TComSlice* curSlice = pic->getSlice(curSliceIdx);
883  Int currColRefPOC =  curSlice->getRefPOC( RefPicList(1-curSlice->getColFromL0Flag()), curSlice->getColRefIdx());
884  TComSlice* preSlice;
885  Int preColRefPOC;
886  for(i=curSliceIdx-1; i>=0; i--)
887  {
888    preSlice = pic->getSlice(i);
889    if(preSlice->getSliceType() != I_SLICE)
890    {
891      preColRefPOC  = preSlice->getRefPOC( RefPicList(1-preSlice->getColFromL0Flag()), preSlice->getColRefIdx());
892      if(currColRefPOC != preColRefPOC)
893      {
894        printf("Collocated_ref_idx shall always be the same for all slices of a coded picture!\n");
895        exit(EXIT_FAILURE);
896      }
897      else
898      {
899        break;
900      }
901    }
902  }
903}
904
905Void TComSlice::checkCRA(TComReferencePictureSet *pReferencePictureSet, Int& pocCRA, Bool& prevRAPisBLA, TComList<TComPic *>& rcListPic)
906{
907  for(Int i = 0; i < pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i++)
908  {
909    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
910    {
911      assert(getPOC()+pReferencePictureSet->getDeltaPOC(i) >= pocCRA);
912    }
913  }
914  for(Int i = pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i < pReferencePictureSet->getNumberOfPictures(); i++)
915  {
916    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
917    {
918      if (!pReferencePictureSet->getCheckLTMSBPresent(i))
919      {
920        assert(xGetLongTermRefPic(rcListPic, pReferencePictureSet->getPOC(i), false)->getPOC() >= pocCRA);
921      }
922      else
923      {
924      assert(pReferencePictureSet->getPOC(i) >= pocCRA);
925    }
926  }
927  }
928  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP ) // IDR picture found
929  {
930    pocCRA = getPOC();
931    prevRAPisBLA = false;
932  }
933  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
934  {
935    pocCRA = getPOC();
936    prevRAPisBLA = false;
937  }
938  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
939         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
940         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP ) // BLA picture found
941  {
942    pocCRA = getPOC();
943    prevRAPisBLA = true;
944  }
945}
946
947/** Function for marking the reference pictures when an IDR/CRA/CRANT/BLA/BLANT is encountered.
948 * \param pocCRA POC of the CRA/CRANT/BLA/BLANT picture
949 * \param bRefreshPending flag indicating if a deferred decoding refresh is pending
950 * \param rcListPic reference to the reference picture list
951 * This function marks the reference pictures as "unused for reference" in the following conditions.
952 * If the nal_unit_type is IDR/BLA/BLANT, all pictures in the reference picture list 
953 * are marked as "unused for reference"
954 *    If the nal_unit_type is BLA/BLANT, set the pocCRA to the temporal reference of the current picture.
955 * Otherwise
956 *    If the bRefreshPending flag is true (a deferred decoding refresh is pending) and the current
957 *    temporal reference is greater than the temporal reference of the latest CRA/CRANT/BLA/BLANT picture (pocCRA),
958 *    mark all reference pictures except the latest CRA/CRANT/BLA/BLANT picture as "unused for reference" and set
959 *    the bRefreshPending flag to false.
960 *    If the nal_unit_type is CRA/CRANT, set the bRefreshPending flag to true and pocCRA to the temporal
961 *    reference of the current picture.
962 * Note that the current picture is already placed in the reference list and its marking is not changed.
963 * If the current picture has a nal_ref_idc that is not 0, it will remain marked as "used for reference".
964 */
965Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic)
966{
967  TComPic*                 rpcPic;
968  Int pocCurr = getPOC(); 
969
970  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
971    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
972    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
973    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
974    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )  // IDR or BLA picture
975  {
976    // mark all pictures as not used for reference
977    TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
978    while (iterPic != rcListPic.end())
979    {
980      rpcPic = *(iterPic);
981      rpcPic->setCurrSliceIdx(0);
982      if (rpcPic->getPOC() != pocCurr) rpcPic->getSlice(0)->setReferenced(false);
983      iterPic++;
984    }
985    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
986      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
987      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
988    {
989      pocCRA = pocCurr;
990    }
991  }
992  else // CRA or No DR
993  {
994    if (bRefreshPending==true && pocCurr > pocCRA) // CRA reference marking pending
995    {
996      TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
997      while (iterPic != rcListPic.end())
998      {
999        rpcPic = *(iterPic);
1000        if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != pocCRA)
1001        {
1002          rpcPic->getSlice(0)->setReferenced(false);
1003        }
1004        iterPic++;
1005      }
1006      bRefreshPending = false; 
1007    }
1008    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
1009    {
1010      bRefreshPending = true; 
1011      pocCRA = pocCurr;
1012    }
1013  }
1014}
1015
1016Void TComSlice::copySliceInfo(TComSlice *pSrc)
1017{
1018  assert( pSrc != NULL );
1019
1020  Int i, j, k;
1021
1022  m_iPOC                 = pSrc->m_iPOC;
1023  m_eNalUnitType         = pSrc->m_eNalUnitType;
1024  m_eSliceType           = pSrc->m_eSliceType;
1025  m_iSliceQp             = pSrc->m_iSliceQp;
1026#if ADAPTIVE_QP_SELECTION
1027  m_iSliceQpBase         = pSrc->m_iSliceQpBase;
1028#endif
1029  m_deblockingFilterDisable   = pSrc->m_deblockingFilterDisable;
1030  m_deblockingFilterOverrideFlag = pSrc->m_deblockingFilterOverrideFlag;
1031  m_deblockingFilterBetaOffsetDiv2 = pSrc->m_deblockingFilterBetaOffsetDiv2;
1032  m_deblockingFilterTcOffsetDiv2 = pSrc->m_deblockingFilterTcOffsetDiv2;
1033 
1034  for (i = 0; i < 2; i++)
1035  {
1036    m_aiNumRefIdx[i]     = pSrc->m_aiNumRefIdx[i];
1037  }
1038
1039  for (i = 0; i < MAX_NUM_REF; i++)
1040  {
1041    m_list1IdxToList0Idx[i] = pSrc->m_list1IdxToList0Idx[i];
1042  } 
1043  m_bCheckLDC             = pSrc->m_bCheckLDC;
1044  m_iSliceQpDelta        = pSrc->m_iSliceQpDelta;
1045  m_iSliceQpDeltaCb      = pSrc->m_iSliceQpDeltaCb;
1046  m_iSliceQpDeltaCr      = pSrc->m_iSliceQpDeltaCr;
1047  for (i = 0; i < 2; i++)
1048  {
1049    for (j = 0; j < MAX_NUM_REF; j++)
1050    {
1051      m_apcRefPicList[i][j]  = pSrc->m_apcRefPicList[i][j];
1052      m_aiRefPOCList[i][j]   = pSrc->m_aiRefPOCList[i][j];
1053    }
1054  }
1055  for (i = 0; i < 2; i++)
1056  {
1057    for (j = 0; j < MAX_NUM_REF + 1; j++)
1058    {
1059      m_bIsUsedAsLongTerm[i][j] = pSrc->m_bIsUsedAsLongTerm[i][j];
1060    }
1061  }
1062  m_iDepth               = pSrc->m_iDepth;
1063
1064  // referenced slice
1065  m_bRefenced            = pSrc->m_bRefenced;
1066
1067  // access channel
1068#if SVC_EXTENSION
1069  m_pcVPS                = pSrc->m_pcVPS;
1070#if JCTVC_M0458_INTERLAYER_RPS_SIG
1071  m_activeNumILRRefIdx         = pSrc->m_activeNumILRRefIdx;
1072  m_interLayerPredEnabledFlag  = pSrc->m_interLayerPredEnabledFlag;
1073  memcpy( m_interLayerPredLayerIdc, pSrc->m_interLayerPredLayerIdc, sizeof( m_interLayerPredLayerIdc ) );
1074#elif REF_IDX_FRAMEWORK
1075  m_numILRRefIdx               = pSrc->m_numILRRefIdx;
1076#endif
1077#endif
1078  m_pcSPS                = pSrc->m_pcSPS;
1079  m_pcPPS                = pSrc->m_pcPPS;
1080  m_pcRPS                = pSrc->m_pcRPS;
1081  m_iLastIDR             = pSrc->m_iLastIDR;
1082
1083  m_pcPic                = pSrc->m_pcPic;
1084
1085  m_colFromL0Flag        = pSrc->m_colFromL0Flag;
1086  m_colRefIdx            = pSrc->m_colRefIdx;
1087#if SAO_CHROMA_LAMBDA
1088  m_dLambdaLuma          = pSrc->m_dLambdaLuma;
1089  m_dLambdaChroma        = pSrc->m_dLambdaChroma;
1090#else
1091  m_dLambda              = pSrc->m_dLambda;
1092#endif
1093  for (i = 0; i < 2; i++)
1094  {
1095    for (j = 0; j < MAX_NUM_REF; j++)
1096    {
1097      for (k =0; k < MAX_NUM_REF; k++)
1098      {
1099        m_abEqualRef[i][j][k] = pSrc->m_abEqualRef[i][j][k];
1100      }
1101    }
1102  }
1103
1104  m_uiTLayer                      = pSrc->m_uiTLayer;
1105  m_bTLayerSwitchingFlag          = pSrc->m_bTLayerSwitchingFlag;
1106
1107  m_sliceMode                   = pSrc->m_sliceMode;
1108  m_sliceArgument               = pSrc->m_sliceArgument;
1109  m_sliceCurStartCUAddr         = pSrc->m_sliceCurStartCUAddr;
1110  m_sliceCurEndCUAddr           = pSrc->m_sliceCurEndCUAddr;
1111  m_sliceIdx                    = pSrc->m_sliceIdx;
1112  m_sliceSegmentMode            = pSrc->m_sliceSegmentMode;
1113  m_sliceSegmentArgument        = pSrc->m_sliceSegmentArgument; 
1114  m_sliceSegmentCurStartCUAddr  = pSrc->m_sliceSegmentCurStartCUAddr;
1115  m_sliceSegmentCurEndCUAddr    = pSrc->m_sliceSegmentCurEndCUAddr;
1116  m_nextSlice                    = pSrc->m_nextSlice;
1117  m_nextSliceSegment             = pSrc->m_nextSliceSegment;
1118  for ( Int e=0 ; e<2 ; e++ )
1119  {
1120    for ( Int n=0 ; n<MAX_NUM_REF ; n++ )
1121    {
1122      memcpy(m_weightPredTable[e][n], pSrc->m_weightPredTable[e][n], sizeof(wpScalingParam)*3 );
1123    }
1124  }
1125  m_saoEnabledFlag = pSrc->m_saoEnabledFlag; 
1126  m_saoEnabledFlagChroma = pSrc->m_saoEnabledFlagChroma;
1127  m_cabacInitFlag                = pSrc->m_cabacInitFlag;
1128  m_numEntryPointOffsets  = pSrc->m_numEntryPointOffsets;
1129
1130  m_bLMvdL1Zero = pSrc->m_bLMvdL1Zero;
1131  m_LFCrossSliceBoundaryFlag = pSrc->m_LFCrossSliceBoundaryFlag;
1132  m_enableTMVPFlag                = pSrc->m_enableTMVPFlag;
1133  m_maxNumMergeCand               = pSrc->m_maxNumMergeCand;
1134}
1135
1136Int TComSlice::m_prevPOC = 0;
1137
1138/** Function for setting the slice's temporal layer ID and corresponding temporal_layer_switching_point_flag.
1139 * \param uiTLayer Temporal layer ID of the current slice
1140 * The decoder calls this function to set temporal_layer_switching_point_flag for each temporal layer based on
1141 * the SPS's temporal_id_nesting_flag and the parsed PPS.  Then, current slice's temporal layer ID and
1142 * temporal_layer_switching_point_flag is set accordingly.
1143 */
1144Void TComSlice::setTLayerInfo( UInt uiTLayer )
1145{
1146  m_uiTLayer = uiTLayer;
1147}
1148
1149/** Function for checking if this is a switching-point
1150*/
1151Bool TComSlice::isTemporalLayerSwitchingPoint( TComList<TComPic*>& rcListPic )
1152{
1153  TComPic* rpcPic;
1154  // loop through all pictures in the reference picture buffer
1155  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1156  while ( iterPic != rcListPic.end())
1157  {
1158    rpcPic = *(iterPic++);
1159    if(rpcPic->getSlice(0)->isReferenced() && rpcPic->getPOC() != getPOC())
1160    {
1161      if(rpcPic->getTLayer() >= getTLayer())
1162      {
1163        return false;
1164      }
1165    }
1166  }
1167  return true;
1168}
1169
1170/** Function for checking if this is a STSA candidate
1171 */
1172Bool TComSlice::isStepwiseTemporalLayerSwitchingPointCandidate( TComList<TComPic*>& rcListPic )
1173{
1174    TComPic* rpcPic;
1175   
1176    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1177    while ( iterPic != rcListPic.end())
1178    {
1179        rpcPic = *(iterPic++);
1180        if(rpcPic->getSlice(0)->isReferenced() &&  (rpcPic->getUsedByCurr()==true) && rpcPic->getPOC() != getPOC())
1181        {
1182            if(rpcPic->getTLayer() >= getTLayer())
1183            {
1184                return false;
1185            }
1186        }
1187    }
1188    return true;
1189}
1190
1191/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1192*/
1193Void TComSlice::applyReferencePictureSet( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1194{
1195  TComPic* rpcPic;
1196  Int i, isReference;
1197
1198  // loop through all pictures in the reference picture buffer
1199  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1200  while ( iterPic != rcListPic.end())
1201  {
1202    rpcPic = *(iterPic++);
1203
1204    if(!rpcPic->getSlice( 0 )->isReferenced())
1205    {
1206      continue;
1207    }
1208
1209    isReference = 0;
1210    // loop through all pictures in the Reference Picture Set
1211    // to see if the picture should be kept as reference picture
1212    for(i=0;i<pReferencePictureSet->getNumberOfPositivePictures()+pReferencePictureSet->getNumberOfNegativePictures();i++)
1213    {
1214      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i))
1215      {
1216        isReference = 1;
1217        rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1218        rpcPic->setIsLongTerm(0);
1219      }
1220    }
1221    for(;i<pReferencePictureSet->getNumberOfPictures();i++)
1222    {
1223      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1224      {
1225        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i))
1226        {
1227          isReference = 1;
1228          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1229        }
1230      }
1231      else 
1232      {
1233        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()))
1234        {
1235          isReference = 1;
1236          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1237        }
1238      }
1239
1240    }
1241    // mark the picture as "unused for reference" if it is not in
1242    // the Reference Picture Set
1243    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && isReference == 0)   
1244    {           
1245      rpcPic->getSlice( 0 )->setReferenced( false );   
1246      rpcPic->setUsedByCurr(0);
1247      rpcPic->setIsLongTerm(0);
1248    }
1249    //check that pictures of higher temporal layers are not used
1250    assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
1251    //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
1252    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
1253    {
1254      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
1255    }
1256    //check that pictures marked as temporal layer non-reference pictures are not used for reference
1257    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && rpcPic->getTLayer()==this->getTLayer())
1258    {
1259      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getSlice( 0 )->getTemporalLayerNonReferenceFlag()==false);
1260    }
1261  }
1262}
1263
1264/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1265*/
1266Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess)
1267{
1268  TComPic* rpcPic;
1269  Int i, isAvailable;
1270  Int atLeastOneLost = 0;
1271  Int atLeastOneRemoved = 0;
1272  Int iPocLost = 0;
1273
1274  // loop through all long-term pictures in the Reference Picture Set
1275  // to see if the picture should be kept as reference picture
1276  for(i=pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i<pReferencePictureSet->getNumberOfPictures();i++)
1277  {
1278    isAvailable = 0;
1279    // loop through all pictures in the reference picture buffer
1280    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1281    while ( iterPic != rcListPic.end())
1282    {
1283      rpcPic = *(iterPic++);
1284      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1285      {
1286        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i) && rpcPic->getSlice(0)->isReferenced())
1287        {
1288          isAvailable = 1;
1289        }
1290      }
1291      else 
1292      {
1293        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())
1294        {
1295          isAvailable = 1;
1296        }
1297      }
1298    }
1299    // if there was no such long-term check the short terms
1300    if(!isAvailable)
1301    {
1302      iterPic = rcListPic.begin();
1303      while ( iterPic != rcListPic.end())
1304      {
1305        rpcPic = *(iterPic++);
1306
1307        Int pocCycle = 1 << rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1308        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC();
1309        Int refPoc = pReferencePictureSet->getPOC(i);
1310        if (!pReferencePictureSet->getCheckLTMSBPresent(i))
1311        {
1312          curPoc = curPoc % pocCycle;
1313          refPoc = refPoc % pocCycle;
1314        }
1315       
1316        if (rpcPic->getSlice(0)->isReferenced() && curPoc == refPoc)
1317        {
1318          isAvailable = 1;
1319          rpcPic->setIsLongTerm(1);
1320          break;
1321        }
1322      }
1323    }
1324    // report that a picture is lost if it is in the Reference Picture Set
1325    // but not available as reference picture
1326    if(isAvailable == 0)   
1327    {           
1328      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1329      {
1330        if(!pReferencePictureSet->getUsed(i) )
1331        {
1332          if(printErrors)
1333          {
1334            printf("\nLong-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1335          }
1336          atLeastOneRemoved = 1;
1337        }
1338        else
1339        {
1340          if(printErrors)
1341          {
1342            printf("\nLong-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1343          }
1344          atLeastOneLost = 1;
1345          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1346        }
1347      }
1348    }
1349  } 
1350  // loop through all short-term pictures in the Reference Picture Set
1351  // to see if the picture should be kept as reference picture
1352  for(i=0;i<pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i++)
1353  {
1354    isAvailable = 0;
1355    // loop through all pictures in the reference picture buffer
1356    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1357    while ( iterPic != rcListPic.end())
1358    {
1359      rpcPic = *(iterPic++);
1360
1361      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1362      {
1363        isAvailable = 1;
1364      }
1365    }
1366    // report that a picture is lost if it is in the Reference Picture Set
1367    // but not available as reference picture
1368    if(isAvailable == 0)   
1369    {           
1370      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1371      {
1372        if(!pReferencePictureSet->getUsed(i) )
1373        {
1374          if(printErrors)
1375          {
1376            printf("\nShort-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1377          }
1378          atLeastOneRemoved = 1;
1379        }
1380        else
1381        {
1382          if(printErrors)
1383          {
1384            printf("\nShort-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1385          }
1386          atLeastOneLost = 1;
1387          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1388        }
1389      }
1390    }
1391  }   
1392  if(atLeastOneLost)
1393  {
1394    return iPocLost+1;
1395  }
1396  if(atLeastOneRemoved)
1397  {
1398    return -2;
1399  }
1400  else
1401  {
1402    return 0;
1403  }
1404}
1405
1406/** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
1407*/
1408#if FIX1071
1409Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool isRAP)
1410#else
1411Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1412#endif
1413{
1414  TComPic* rpcPic;
1415  Int i, j;
1416  Int k = 0;
1417  Int nrOfNegativePictures = 0;
1418  Int nrOfPositivePictures = 0;
1419  TComReferencePictureSet* pcRPS = this->getLocalRPS();
1420
1421  // loop through all pictures in the Reference Picture Set
1422  for(i=0;i<pReferencePictureSet->getNumberOfPictures();i++)
1423  {
1424    j = 0;
1425    // loop through all pictures in the reference picture buffer
1426    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1427    while ( iterPic != rcListPic.end())
1428    {
1429      j++;
1430      rpcPic = *(iterPic++);
1431
1432      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1433      {
1434        // This picture exists as a reference picture
1435        // and should be added to the explicit Reference Picture Set
1436        pcRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
1437#if FIX1071
1438        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i) && (!isRAP));
1439#else
1440        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i));
1441#endif
1442        if(pcRPS->getDeltaPOC(k) < 0)
1443        {
1444          nrOfNegativePictures++;
1445        }
1446        else
1447        {
1448          nrOfPositivePictures++;
1449        }
1450        k++;
1451      }
1452    }
1453  }
1454  pcRPS->setNumberOfNegativePictures(nrOfNegativePictures);
1455  pcRPS->setNumberOfPositivePictures(nrOfPositivePictures);
1456  pcRPS->setNumberOfPictures(nrOfNegativePictures+nrOfPositivePictures);
1457  // This is a simplistic inter rps example. A smarter encoder will look for a better reference RPS to do the
1458  // inter RPS prediction with.  Here we just use the reference used by pReferencePictureSet.
1459  // If pReferencePictureSet is not inter_RPS_predicted, then inter_RPS_prediction is for the current RPS also disabled.
1460  if (!pReferencePictureSet->getInterRPSPrediction())
1461  {
1462    pcRPS->setInterRPSPrediction(false);
1463    pcRPS->setNumRefIdc(0);
1464  }
1465  else
1466  {
1467    Int rIdx =  this->getRPSidx() - pReferencePictureSet->getDeltaRIdxMinus1() - 1;
1468    Int deltaRPS = pReferencePictureSet->getDeltaRPS();
1469    TComReferencePictureSet* pcRefRPS = this->getSPS()->getRPSList()->getReferencePictureSet(rIdx);
1470    Int iRefPics = pcRefRPS->getNumberOfPictures();
1471    Int iNewIdc=0;
1472    for(i=0; i<= iRefPics; i++) 
1473    {
1474      Int deltaPOC = ((i != iRefPics)? pcRefRPS->getDeltaPOC(i) : 0);  // check if the reference abs POC is >= 0
1475      Int iRefIdc = 0;
1476      for (j=0; j < pcRPS->getNumberOfPictures(); j++) // loop through the  pictures in the new RPS
1477      {
1478        if ( (deltaPOC + deltaRPS) == pcRPS->getDeltaPOC(j))
1479        {
1480          if (pcRPS->getUsed(j))
1481          {
1482            iRefIdc = 1;
1483          }
1484          else
1485          {
1486            iRefIdc = 2;
1487          }
1488        }
1489      }
1490      pcRPS->setRefIdc(i, iRefIdc);
1491      iNewIdc++;
1492    }
1493    pcRPS->setInterRPSPrediction(true);
1494    pcRPS->setNumRefIdc(iNewIdc);
1495    pcRPS->setDeltaRPS(deltaRPS); 
1496    pcRPS->setDeltaRIdxMinus1(pReferencePictureSet->getDeltaRIdxMinus1() + this->getSPS()->getRPSList()->getNumberOfReferencePictureSets() - this->getRPSidx());
1497  }
1498
1499  this->setRPS(pcRPS);
1500  this->setRPSidx(-1);
1501}
1502
1503/** get AC and DC values for weighted pred
1504 * \param *wp
1505 * \returns Void
1506 */
1507Void  TComSlice::getWpAcDcParam(wpACDCParam *&wp)
1508{
1509  wp = m_weightACDCParam;
1510}
1511
1512/** init AC and DC values for weighted pred
1513 * \returns Void
1514 */
1515Void  TComSlice::initWpAcDcParam()
1516{
1517  for(Int iComp = 0; iComp < 3; iComp++ )
1518  {
1519    m_weightACDCParam[iComp].iAC = 0;
1520    m_weightACDCParam[iComp].iDC = 0;
1521  }
1522}
1523
1524/** get WP tables for weighted pred
1525 * \param RefPicList
1526 * \param iRefIdx
1527 * \param *&wpScalingParam
1528 * \returns Void
1529 */
1530Void  TComSlice::getWpScaling( RefPicList e, Int iRefIdx, wpScalingParam *&wp )
1531{
1532  wp = m_weightPredTable[e][iRefIdx];
1533}
1534
1535/** reset Default WP tables settings : no weight.
1536 * \param wpScalingParam
1537 * \returns Void
1538 */
1539Void  TComSlice::resetWpScaling()
1540{
1541  for ( Int e=0 ; e<2 ; e++ )
1542  {
1543    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1544    {
1545      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1546      {
1547        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1548        pwp->bPresentFlag      = false;
1549        pwp->uiLog2WeightDenom = 0;
1550        pwp->uiLog2WeightDenom = 0;
1551        pwp->iWeight           = 1;
1552        pwp->iOffset           = 0;
1553      }
1554    }
1555  }
1556}
1557
1558/** init WP table
1559 * \returns Void
1560 */
1561Void  TComSlice::initWpScaling()
1562{
1563  for ( Int e=0 ; e<2 ; e++ )
1564  {
1565    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1566    {
1567      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1568      {
1569        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1570        if ( !pwp->bPresentFlag ) 
1571        {
1572          // Inferring values not present :
1573          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
1574          pwp->iOffset = 0;
1575        }
1576
1577        pwp->w      = pwp->iWeight;
1578        Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
1579        pwp->o      = pwp->iOffset << (bitDepth-8);
1580        pwp->shift  = pwp->uiLog2WeightDenom;
1581        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
1582      }
1583    }
1584  }
1585}
1586
1587// ------------------------------------------------------------------------------------------------
1588// Video parameter set (VPS)
1589// ------------------------------------------------------------------------------------------------
1590TComVPS::TComVPS()
1591: m_VPSId                     (  0)
1592, m_uiMaxTLayers              (  1)
1593, m_uiMaxLayers               (  1)
1594, m_bTemporalIdNestingFlag    (false)
1595, m_numHrdParameters          (  0)
1596#if !VPS_RENAME
1597, m_maxNuhReservedZeroLayerId (  0)
1598#endif
1599, m_hrdParameters             (NULL)
1600, m_hrdOpSetIdx               (NULL)
1601, m_cprmsPresentFlag          (NULL)
1602#if VPS_RENAME
1603, m_maxLayerId                (0)
1604, m_numLayerSets              (0)
1605#endif
1606#if VPS_EXTN_OP_LAYER_SETS
1607, m_numOutputLayerSets        (0) 
1608#endif
1609#if VPS_PROFILE_OUTPUT_LAYERS
1610, m_numProfileTierLevel       (0)
1611, m_moreOutputLayerSetsThanDefaultFlag (false)
1612, m_numAddOutputLayerSets     (0)
1613, m_defaultOneTargetOutputLayerFlag    (false)
1614#endif
1615{
1616  for( Int i = 0; i < MAX_TLAYER; i++)
1617  {
1618    m_numReorderPics[i] = 0;
1619    m_uiMaxDecPicBuffering[i] = 1; 
1620    m_uiMaxLatencyIncrease[i] = 0;
1621  }
1622#if VPS_EXTN_MASK_AND_DIM_INFO
1623  m_avcBaseLayerFlag = false;
1624  m_splittingFlag = false;
1625  ::memset(m_scalabilityMask, 0, sizeof(m_scalabilityMask));
1626  ::memset(m_dimensionIdLen, 0, sizeof(m_dimensionIdLen));
1627  m_nuhLayerIdPresentFlag = false;
1628  ::memset(m_layerIdInNuh, 0, sizeof(m_layerIdInNuh));
1629  ::memset(m_dimensionId, 0, sizeof(m_dimensionId));
1630
1631  m_numScalabilityTypes = 0;
1632  ::memset(m_layerIdInVps, 0, sizeof(m_layerIdInVps));
1633#endif
1634#if VPS_EXTN_PROFILE_INFO
1635  ::memset(m_profilePresentFlag, 0, sizeof(m_profilePresentFlag));
1636  ::memset(m_profileLayerSetRef, 0, sizeof(m_profileLayerSetRef));
1637#endif
1638#if VPS_EXTN_OP_LAYER_SETS
1639  ::memset(m_layerIdIncludedFlag, 0, sizeof(m_layerIdIncludedFlag));
1640  // Consider dynamic allocation for outputLayerSetIdx and outputLayerFlag
1641  ::memset(m_outputLayerSetIdx, 0, sizeof(m_outputLayerSetIdx));
1642  ::memset(m_outputLayerFlag, 0, sizeof(m_outputLayerFlag));
1643#endif
1644#if VPS_EXTN_DIRECT_REF_LAYERS
1645  ::memset(m_directDependencyFlag, 0, sizeof(m_directDependencyFlag));
1646  ::memset(m_numDirectRefLayers,   0, sizeof(m_numDirectRefLayers  ));
1647  ::memset(m_refLayerId,           0, sizeof(m_refLayerId          ));
1648#if M0457_PREDICTION_INDICATIONS
1649  m_directDepTypeLen = 2;
1650  ::memset(m_directDependencyType, 0, sizeof(m_directDependencyType));
1651#endif
1652#endif
1653#if DERIVE_LAYER_ID_LIST_VARIABLES
1654  ::memset(m_layerSetLayerIdList,  0, sizeof(m_layerSetLayerIdList));
1655  ::memset(m_numLayerInIdList,     0, sizeof(m_numLayerInIdList   )); 
1656#endif
1657#if VPS_PROFILE_OUTPUT_LAYERS
1658  ::memset(m_profileLevelTierIdx,  0, sizeof(m_profileLevelTierIdx));
1659#endif
1660#if JCTVC_M0458_INTERLAYER_RPS_SIG
1661  m_maxOneActiveRefLayerFlag = true;
1662#endif
1663#if JCTVC_M0203_INTERLAYER_PRED_IDC
1664#if N0120_MAX_TID_REF_PRESENT_FLAG
1665  m_maxTidIlRefPicsPlus1PresentFlag = true;
1666#endif
1667  for( Int i = 0; i < MAX_VPS_LAYER_ID_PLUS1 - 1; i++)
1668  {
1669    m_maxSublayerForIlpPlus1[i] = m_uiMaxTLayers + 1;
1670  }
1671#endif
1672}
1673
1674TComVPS::~TComVPS()
1675{
1676  if( m_hrdParameters    != NULL )     delete[] m_hrdParameters;
1677  if( m_hrdOpSetIdx      != NULL )     delete[] m_hrdOpSetIdx;
1678  if( m_cprmsPresentFlag != NULL )     delete[] m_cprmsPresentFlag;
1679}
1680#if DERIVE_LAYER_ID_LIST_VARIABLES
1681Void TComVPS::deriveLayerIdListVariables()
1682{
1683  // For layer 0
1684  setNumLayersInIdList(0, 1);
1685  setLayerSetLayerIdList(0, 0, 0);
1686
1687  // For other layers
1688  Int i, m, n;
1689  for( i = 1; i <= getNumLayerSets() - 1; i++ )
1690  {
1691    n = 0;
1692    for( m = 0; m <= this->getMaxLayerId(); m++)
1693    {
1694      if(this->getLayerIdIncludedFlag(i, m))
1695      {
1696        setLayerSetLayerIdList(i, n, m);
1697        n++;
1698      }
1699    }
1700    setNumLayersInIdList(i, n);
1701  }
1702}
1703#endif
1704// ------------------------------------------------------------------------------------------------
1705// Sequence parameter set (SPS)
1706// ------------------------------------------------------------------------------------------------
1707
1708TComSPS::TComSPS()
1709: m_SPSId                     (  0)
1710, m_VPSId                     (  0)
1711, m_chromaFormatIdc           (CHROMA_420)
1712, m_uiMaxTLayers              (  1)
1713// Structure
1714, m_picWidthInLumaSamples     (352)
1715, m_picHeightInLumaSamples    (288)
1716, m_log2MinCodingBlockSize    (  0)
1717, m_log2DiffMaxMinCodingBlockSize (0)
1718, m_uiMaxCUWidth              ( 32)
1719, m_uiMaxCUHeight             ( 32)
1720, m_uiMaxCUDepth              (  3)
1721, m_bLongTermRefsPresent      (false)
1722, m_uiQuadtreeTULog2MaxSize   (  0)
1723, m_uiQuadtreeTULog2MinSize   (  0)
1724, m_uiQuadtreeTUMaxDepthInter (  0)
1725, m_uiQuadtreeTUMaxDepthIntra (  0)
1726// Tool list
1727, m_usePCM                   (false)
1728, m_pcmLog2MaxSize            (  5)
1729, m_uiPCMLog2MinSize          (  7)
1730, m_bitDepthY                 (  8)
1731, m_bitDepthC                 (  8)
1732, m_qpBDOffsetY               (  0)
1733, m_qpBDOffsetC               (  0)
1734, m_useLossless               (false)
1735, m_uiPCMBitDepthLuma         (  8)
1736, m_uiPCMBitDepthChroma       (  8)
1737, m_bPCMFilterDisableFlag     (false)
1738, m_uiBitsForPOC              (  8)
1739, m_numLongTermRefPicSPS    (  0) 
1740, m_uiMaxTrSize               ( 32)
1741, m_bUseSAO                   (false) 
1742, m_bTemporalIdNestingFlag    (false)
1743, m_scalingListEnabledFlag    (false)
1744, m_useStrongIntraSmoothing   (false)
1745, m_vuiParametersPresentFlag  (false)
1746, m_vuiParameters             ()
1747#if M0463_VUI_EXT_ILP_REF
1748, m_interViewMvVertConstraintFlag (false)
1749, m_numIlpRestrictedRefLayers ( 0 )
1750#endif
1751#if SVC_EXTENSION
1752, m_layerId(0)
1753#endif
1754#if SCALED_REF_LAYER_OFFSETS
1755, m_numScaledRefLayerOffsets  (0)
1756#endif
1757{
1758  for ( Int i = 0; i < MAX_TLAYER; i++ )
1759  {
1760    m_uiMaxLatencyIncrease[i] = 0;
1761    m_uiMaxDecPicBuffering[i] = 1;
1762    m_numReorderPics[i]       = 0;
1763  }
1764  m_scalingList = new TComScalingList;
1765  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
1766  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
1767#if M0463_VUI_EXT_ILP_REF
1768  for (Int i = 0; i < MAX_LAYERS; i++ )
1769  {
1770    m_minSpatialSegmentOffsetPlus1[ i ] = 0;
1771    m_ctuBasedOffsetEnabledFlag   [ i ] = false;
1772    m_minHorizontalCtuOffsetPlus1 [ i ] = 0;
1773  }
1774#endif
1775}
1776
1777TComSPS::~TComSPS()
1778{
1779  delete m_scalingList;
1780  m_RPSList.destroy();
1781}
1782
1783Void  TComSPS::createRPSList( Int numRPS )
1784{ 
1785  m_RPSList.destroy();
1786  m_RPSList.create(numRPS);
1787}
1788
1789Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
1790{
1791  if( !getVuiParametersPresentFlag() )
1792  {
1793    return;
1794  }
1795
1796  TComVUI *vui = getVuiParameters();
1797  TComHRD *hrd = vui->getHrdParameters();
1798
1799  TimingInfo *timingInfo = vui->getTimingInfo();
1800  timingInfo->setTimingInfoPresentFlag( true );
1801  switch( frameRate )
1802  {
1803  case 24:
1804    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
1805    break;
1806  case 25:
1807    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
1808    break;
1809  case 30:
1810    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
1811    break;
1812  case 50:
1813    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
1814    break;
1815  case 60:
1816    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
1817    break;
1818  default:
1819    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
1820    break;
1821  }
1822
1823  Bool rateCnt = ( bitRate > 0 );
1824  hrd->setNalHrdParametersPresentFlag( rateCnt );
1825  hrd->setVclHrdParametersPresentFlag( rateCnt );
1826
1827  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
1828
1829  if( hrd->getSubPicCpbParamsPresentFlag() )
1830  {
1831    hrd->setTickDivisorMinus2( 100 - 2 );                          //
1832    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
1833    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
1834    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
1835  }
1836  else
1837  {
1838    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
1839  }
1840
1841  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
1842  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1843  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
1844 
1845  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
1846  if( randomAccess )
1847  {
1848    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
1849    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
1850  }
1851  else
1852  {
1853    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
1854    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
1855  }
1856
1857/*
1858   Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
1859*/
1860  Int i, j;
1861  UInt birateValue, cpbSizeValue;
1862  UInt ducpbSizeValue;
1863  UInt duBitRateValue = 0;
1864
1865  for( i = 0; i < MAX_TLAYER; i ++ )
1866  {
1867    hrd->setFixedPicRateFlag( i, 1 );
1868    hrd->setPicDurationInTcMinus1( i, 0 );
1869    hrd->setLowDelayHrdFlag( i, 0 );
1870    hrd->setCpbCntMinus1( i, 0 );
1871
1872    birateValue  = bitRate;
1873    cpbSizeValue = bitRate;                                     // 1 second
1874    ducpbSizeValue = bitRate/numDU;
1875    duBitRateValue = bitRate;
1876    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
1877    {
1878      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
1879      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
1880      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
1881      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
1882
1883      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
1884      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
1885      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
1886      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
1887      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
1888    }
1889  }
1890}
1891const Int TComSPS::m_winUnitX[]={1,2,2,1};
1892const Int TComSPS::m_winUnitY[]={1,2,1,1};
1893
1894TComPPS::TComPPS()
1895: m_PPSId                       (0)
1896, m_SPSId                       (0)
1897, m_picInitQPMinus26            (0)
1898, m_useDQP                      (false)
1899, m_bConstrainedIntraPred       (false)
1900, m_bSliceChromaQpFlag          (false)
1901, m_pcSPS                       (NULL)
1902, m_uiMaxCuDQPDepth             (0)
1903, m_uiMinCuDQPSize              (0)
1904, m_chromaCbQpOffset            (0)
1905, m_chromaCrQpOffset            (0)
1906, m_numRefIdxL0DefaultActive    (1)
1907, m_numRefIdxL1DefaultActive    (1)
1908, m_TransquantBypassEnableFlag  (false)
1909, m_useTransformSkip             (false)
1910, m_dependentSliceSegmentsEnabledFlag    (false)
1911, m_tilesEnabledFlag               (false)
1912, m_entropyCodingSyncEnabledFlag   (false)
1913, m_loopFilterAcrossTilesEnabledFlag  (true)
1914, m_uniformSpacingFlag           (0)
1915, m_iNumColumnsMinus1            (0)
1916, m_puiColumnWidth               (NULL)
1917, m_iNumRowsMinus1               (0)
1918, m_puiRowHeight                 (NULL)
1919, m_iNumSubstreams             (1)
1920, m_signHideFlag(0)
1921, m_cabacInitPresentFlag        (false)
1922, m_encCABACTableIdx            (I_SLICE)
1923, m_sliceHeaderExtensionPresentFlag    (false)
1924, m_loopFilterAcrossSlicesEnabledFlag (false)
1925, m_listsModificationPresentFlag(  0)
1926, m_numExtraSliceHeaderBits(0)
1927{
1928  m_scalingList = new TComScalingList;
1929}
1930
1931TComPPS::~TComPPS()
1932{
1933  if( m_iNumColumnsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1934  {
1935    if (m_puiColumnWidth) delete [] m_puiColumnWidth; 
1936    m_puiColumnWidth = NULL;
1937  }
1938  if( m_iNumRowsMinus1 > 0 && m_uniformSpacingFlag == 0 )
1939  {
1940    if (m_puiRowHeight) delete [] m_puiRowHeight;
1941    m_puiRowHeight = NULL;
1942  }
1943  delete m_scalingList;
1944}
1945
1946TComReferencePictureSet::TComReferencePictureSet()
1947: m_numberOfPictures (0)
1948, m_numberOfNegativePictures (0)
1949, m_numberOfPositivePictures (0)
1950, m_numberOfLongtermPictures (0)
1951, m_interRPSPrediction (0) 
1952, m_deltaRIdxMinus1 (0)   
1953, m_deltaRPS (0) 
1954, m_numRefIdc (0) 
1955{
1956  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
1957  ::memset( m_POC, 0, sizeof(m_POC) );
1958  ::memset( m_used, 0, sizeof(m_used) );
1959  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
1960}
1961
1962TComReferencePictureSet::~TComReferencePictureSet()
1963{
1964}
1965
1966Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
1967{
1968  m_used[bufferNum] = used;
1969}
1970
1971Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
1972{
1973  m_deltaPOC[bufferNum] = deltaPOC;
1974}
1975
1976Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
1977{
1978  m_numberOfPictures = numberOfPictures;
1979}
1980
1981Int TComReferencePictureSet::getUsed(Int bufferNum)
1982{
1983  return m_used[bufferNum];
1984}
1985
1986Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
1987{
1988  return m_deltaPOC[bufferNum];
1989}
1990
1991Int TComReferencePictureSet::getNumberOfPictures()
1992{
1993  return m_numberOfPictures;
1994}
1995
1996Int TComReferencePictureSet::getPOC(Int bufferNum)
1997{
1998  return m_POC[bufferNum];
1999}
2000
2001Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
2002{
2003  m_POC[bufferNum] = POC;
2004}
2005
2006Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
2007{
2008  return m_bCheckLTMSB[bufferNum];
2009}
2010
2011Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
2012{
2013  m_bCheckLTMSB[bufferNum] = b;
2014}
2015
2016/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
2017 * \param uiBufferNum
2018 * \param iRefIdc
2019 * \returns Void
2020 */
2021Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
2022{
2023  m_refIdc[bufferNum] = refIdc;
2024}
2025
2026/** get the reference idc value at uiBufferNum
2027 * \param uiBufferNum
2028 * \returns Int
2029 */
2030Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
2031{
2032  return m_refIdc[bufferNum];
2033}
2034
2035/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
2036 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
2037 *  +ve values are in increasing order.
2038 * \returns Void
2039 */
2040Void TComReferencePictureSet::sortDeltaPOC()
2041{
2042  // sort in increasing order (smallest first)
2043  for(Int j=1; j < getNumberOfPictures(); j++)
2044  { 
2045    Int deltaPOC = getDeltaPOC(j);
2046    Bool used = getUsed(j);
2047    for (Int k=j-1; k >= 0; k--)
2048    {
2049      Int temp = getDeltaPOC(k);
2050      if (deltaPOC < temp)
2051      {
2052        setDeltaPOC(k+1, temp);
2053        setUsed(k+1, getUsed(k));
2054        setDeltaPOC(k, deltaPOC);
2055        setUsed(k, used);
2056      }
2057    }
2058  }
2059  // flip the negative values to largest first
2060  Int numNegPics = getNumberOfNegativePictures();
2061  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
2062  { 
2063    Int deltaPOC = getDeltaPOC(j);
2064    Bool used = getUsed(j);
2065    setDeltaPOC(j, getDeltaPOC(k));
2066    setUsed(j, getUsed(k));
2067    setDeltaPOC(k, deltaPOC);
2068    setUsed(k, used);
2069  }
2070}
2071
2072/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
2073 *  A "*" is added to the deltaPOC value if it is Used bu current.
2074 * \returns Void
2075 */
2076Void TComReferencePictureSet::printDeltaPOC()
2077{
2078  printf("DeltaPOC = { ");
2079  for(Int j=0; j < getNumberOfPictures(); j++)
2080  {
2081    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
2082  } 
2083  if (getInterRPSPrediction()) 
2084  {
2085    printf("}, RefIdc = { ");
2086    for(Int j=0; j < getNumRefIdc(); j++)
2087    {
2088      printf("%d ", getRefIdc(j));
2089    } 
2090  }
2091  printf("}\n");
2092}
2093
2094TComRPSList::TComRPSList()
2095:m_referencePictureSets (NULL)
2096{
2097}
2098
2099TComRPSList::~TComRPSList()
2100{
2101}
2102
2103Void TComRPSList::create( Int numberOfReferencePictureSets)
2104{
2105  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2106  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
2107}
2108
2109Void TComRPSList::destroy()
2110{
2111  if (m_referencePictureSets)
2112  {
2113    delete [] m_referencePictureSets;
2114  }
2115  m_numberOfReferencePictureSets = 0;
2116  m_referencePictureSets = NULL;
2117}
2118
2119
2120
2121TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
2122{
2123  return &m_referencePictureSets[referencePictureSetNum];
2124}
2125
2126Int TComRPSList::getNumberOfReferencePictureSets()
2127{
2128  return m_numberOfReferencePictureSets;
2129}
2130
2131Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
2132{
2133  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
2134}
2135
2136TComRefPicListModification::TComRefPicListModification()
2137: m_bRefPicListModificationFlagL0 (false)
2138, m_bRefPicListModificationFlagL1 (false)
2139{
2140  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
2141  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
2142}
2143
2144TComRefPicListModification::~TComRefPicListModification()
2145{
2146}
2147
2148TComScalingList::TComScalingList()
2149{
2150  m_useTransformSkip = false;
2151  init();
2152}
2153TComScalingList::~TComScalingList()
2154{
2155  destroy();
2156}
2157
2158/** set default quantization matrix to array
2159*/
2160Void TComSlice::setDefaultScalingList()
2161{
2162  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2163  {
2164    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2165    {
2166      getScalingList()->processDefaultMarix(sizeId, listId);
2167    }
2168  }
2169}
2170/** check if use default quantization matrix
2171 * \returns true if use default quantization matrix in all size
2172*/
2173Bool TComSlice::checkDefaultScalingList()
2174{
2175  UInt defaultCounter=0;
2176
2177  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2178  {
2179    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
2180    {
2181      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
2182     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
2183      {
2184        defaultCounter++;
2185      }
2186    }
2187  }
2188  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
2189}
2190
2191/** get scaling matrix from RefMatrixID
2192 * \param sizeId size index
2193 * \param Index of input matrix
2194 * \param Index of reference matrix
2195 */
2196Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
2197{
2198  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2199}
2200
2201/** parse syntax infomation
2202 *  \param pchFile syntax infomation
2203 *  \returns false if successful
2204 */
2205Bool TComScalingList::xParseScalingList(Char* pchFile)
2206{
2207  FILE *fp;
2208  Char line[1024];
2209  UInt sizeIdc,listIdc;
2210  UInt i,size = 0;
2211  Int *src=0,data;
2212  Char *ret;
2213  UInt  retval;
2214
2215  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
2216  {
2217    printf("can't open file %s :: set Default Matrix\n",pchFile);
2218    return true;
2219  }
2220
2221  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
2222  {
2223    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
2224    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
2225    {
2226      src = getScalingListAddress(sizeIdc, listIdc);
2227
2228      fseek(fp,0,0);
2229      do 
2230      {
2231        ret = fgets(line, 1024, fp);
2232        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
2233        {
2234          printf("Error: can't read Matrix :: set Default Matrix\n");
2235          return true;
2236        }
2237      }
2238      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
2239      for (i=0; i<size; i++)
2240      {
2241        retval = fscanf(fp, "%d,", &data);
2242        if (retval!=1)
2243        {
2244          printf("Error: can't read Matrix :: set Default Matrix\n");
2245          return true;
2246        }
2247        src[i] = data;
2248      }
2249      //set DC value for default matrix check
2250      setScalingListDC(sizeIdc,listIdc,src[0]);
2251
2252      if(sizeIdc > SCALING_LIST_8x8)
2253      {
2254        fseek(fp,0,0);
2255        do 
2256        {
2257          ret = fgets(line, 1024, fp);
2258          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
2259          {
2260            printf("Error: can't read DC :: set Default Matrix\n");
2261            return true;
2262          }
2263        }
2264        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
2265        retval = fscanf(fp, "%d,", &data);
2266        if (retval!=1)
2267        {
2268          printf("Error: can't read Matrix :: set Default Matrix\n");
2269          return true;
2270        }
2271        //overwrite DC value when size of matrix is larger than 16x16
2272        setScalingListDC(sizeIdc,listIdc,data);
2273      }
2274    }
2275  }
2276  fclose(fp);
2277  return false;
2278}
2279
2280/** initialization process of quantization matrix array
2281 */
2282Void TComScalingList::init()
2283{
2284  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2285  {
2286    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2287    {
2288      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
2289    }
2290  }
2291  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
2292}
2293
2294/** destroy quantization matrix array
2295 */
2296Void TComScalingList::destroy()
2297{
2298  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2299  {
2300    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2301    {
2302      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
2303    }
2304  }
2305}
2306
2307/** get default address of quantization matrix
2308 * \param sizeId size index
2309 * \param listId list index
2310 * \returns pointer of quantization matrix
2311 */
2312Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
2313{
2314  Int *src = 0;
2315  switch(sizeId)
2316  {
2317    case SCALING_LIST_4x4:
2318      src = g_quantTSDefault4x4;
2319      break;
2320    case SCALING_LIST_8x8:
2321      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2322      break;
2323    case SCALING_LIST_16x16:
2324      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2325      break;
2326    case SCALING_LIST_32x32:
2327      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
2328      break;
2329    default:
2330      assert(0);
2331      src = NULL;
2332      break;
2333  }
2334  return src;
2335}
2336
2337/** process of default matrix
2338 * \param sizeId size index
2339 * \param Index of input matrix
2340 */
2341Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId)
2342{
2343  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
2344  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
2345}
2346
2347/** check DC value of matrix for default matrix signaling
2348 */
2349Void TComScalingList::checkDcOfMatrix()
2350{
2351  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
2352  {
2353    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
2354    {
2355      //check default matrix?
2356      if(getScalingListDC(sizeId,listId) == 0)
2357      {
2358        processDefaultMarix(sizeId, listId);
2359      }
2360    }
2361  }
2362}
2363
2364ParameterSetManager::ParameterSetManager()
2365#if SVC_EXTENSION
2366: m_spsMap(MAX_NUM_SPS)
2367, m_ppsMap(MAX_NUM_PPS)
2368, m_activeSPSId(-1)
2369, m_activePPSId(-1)
2370#else
2371: m_vpsMap(MAX_NUM_VPS)
2372, m_spsMap(MAX_NUM_SPS)
2373, m_ppsMap(MAX_NUM_PPS)
2374, m_activeVPSId(-1)
2375, m_activeSPSId(-1)
2376, m_activePPSId(-1)
2377#endif
2378{
2379}
2380
2381
2382ParameterSetManager::~ParameterSetManager()
2383{
2384}
2385
2386//! activate a SPS from a active parameter sets SEI message
2387//! \returns true, if activation is successful
2388Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
2389{
2390  TComSPS *sps = m_spsMap.getPS(spsId);
2391  if (sps)
2392  {
2393    Int vpsId = sps->getVPSId();
2394    if (m_vpsMap.getPS(vpsId))
2395    {
2396      m_activeVPSId = vpsId;
2397      m_activeSPSId = spsId;
2398      return true;
2399    }
2400    else
2401    {
2402      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
2403    }
2404  }
2405  else
2406  {
2407    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
2408  }
2409  return false;
2410}
2411
2412//! activate a PPS and depending on isIDR parameter also SPS and VPS
2413//! \returns true, if activation is successful
2414Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
2415{
2416  TComPPS *pps = m_ppsMap.getPS(ppsId);
2417  if (pps)
2418  {
2419    Int spsId = pps->getSPSId();
2420    if (!isIRAP && (spsId != m_activeSPSId))
2421    {
2422      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
2423      return false;
2424    }
2425    TComSPS *sps = m_spsMap.getPS(spsId);
2426    if (sps)
2427    {
2428      Int vpsId = sps->getVPSId();
2429      if (!isIRAP && (vpsId != m_activeVPSId))
2430      {
2431        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
2432        return false;
2433      }
2434      if (m_vpsMap.getPS(vpsId))
2435      {
2436        m_activePPSId = ppsId;
2437        m_activeVPSId = vpsId;
2438        m_activeSPSId = spsId;
2439        return true;
2440      }
2441      else
2442      {
2443        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
2444      }
2445    }
2446    else
2447    {
2448      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
2449    }
2450  }
2451  else
2452  {
2453    printf("Warning: tried to activate non-existing PPS.");
2454  }
2455  return false;
2456}
2457
2458ProfileTierLevel::ProfileTierLevel()
2459  : m_profileSpace    (0)
2460  , m_tierFlag        (false)
2461  , m_profileIdc      (0)
2462  , m_levelIdc        (0)
2463, m_progressiveSourceFlag  (false)
2464, m_interlacedSourceFlag   (false)
2465, m_nonPackedConstraintFlag(false)
2466, m_frameOnlyConstraintFlag(false)
2467{
2468  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
2469}
2470#if VPS_EXTN_PROFILE_INFO
2471Void ProfileTierLevel::copyProfileInfo(ProfileTierLevel *ptl)
2472{
2473  this->setProfileSpace          ( ptl->getProfileSpace()      );
2474  this->setTierFlag              ( ptl->getTierFlag()          );
2475  this->setProfileIdc            ( ptl->getProfileIdc()        );
2476  for(Int j = 0; j < 32; j++)
2477  {
2478    this->setProfileCompatibilityFlag(j, ptl->getProfileCompatibilityFlag(j));
2479  }
2480  this->setProgressiveSourceFlag  ( ptl->getProgressiveSourceFlag()  );
2481  this->setInterlacedSourceFlag   ( ptl->getInterlacedSourceFlag()   );
2482  this->setNonPackedConstraintFlag( ptl->getNonPackedConstraintFlag());
2483  this->setFrameOnlyConstraintFlag( ptl->getFrameOnlyConstraintFlag()); 
2484}
2485#endif
2486
2487TComPTL::TComPTL()
2488{
2489  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
2490  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
2491}
2492#if VPS_EXTN_PROFILE_INFO
2493Void TComPTL::copyProfileInfo(TComPTL *ptl)
2494{
2495  // Copy all information related to general profile
2496  this->getGeneralPTL()->copyProfileInfo(ptl->getGeneralPTL());
2497}
2498#endif
2499
2500#if SVC_EXTENSION
2501#if AVC_SYNTAX
2502Void TComSlice::initBaseLayerRPL( TComSlice *pcSlice )
2503{
2504// 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
2505  setPOC( pcSlice->getPOC() );
2506  if( pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pcSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA )
2507  {
2508    setSliceType( I_SLICE );
2509  }
2510  else
2511  {
2512    setSliceType( pcSlice->getSliceType() );
2513  }
2514
2515  if( this->isIntra() )
2516  {
2517    return;
2518  }
2519
2520  //initialize reference POC of BL
2521  for( Int iRefPicList = 0; iRefPicList < 2; iRefPicList++ )
2522  {
2523    RefPicList eRefPicList = RefPicList( iRefPicList );
2524
2525    assert( pcSlice->getNumRefIdx( eRefPicList) >= 0 );
2526    setNumRefIdx( eRefPicList, pcSlice->getNumRefIdx( eRefPicList ) - 1 );
2527    assert( getNumRefIdx( eRefPicList) <= MAX_NUM_REF);
2528
2529    for(Int refIdx = 0; refIdx < getNumRefIdx( eRefPicList ); refIdx++) 
2530    {
2531      setRefPOC( pcSlice->getRefPic( eRefPicList, refIdx )->getPOC(), eRefPicList, refIdx );
2532      setRefPic( pcSlice->getRefPic( eRefPicList, refIdx ), eRefPicList, refIdx );
2533      /*
2534      // should be set if the base layer has its own instance of the reference picture lists, currently EL RPL is reused.
2535      getRefPic( eRefPicList, refIdx )->setLayerId( 0 );
2536      getRefPic( eRefPicList, refIdx )->setIsLongTerm( pcSlice->getRefPic( eRefPicList, refIdx )->getIsLongTerm() );     
2537      */
2538
2539    }
2540  } 
2541  return;
2542}
2543#endif
2544
2545Void TComSlice::setBaseColPic(  TComList<TComPic*>& rcListPic, UInt refLayerIdc )
2546{ 
2547  if(m_layerId == 0)
2548  {
2549    memset( m_pcBaseColPic, 0, sizeof( m_pcBaseColPic ) );
2550    return;
2551  }       
2552  setBaseColPic(refLayerIdc, xGetRefPic(rcListPic, getPOC())); 
2553}
2554#endif
2555
2556#if REF_IDX_MFM
2557Void TComSlice::setRefPOCListILP( TComPic** ilpPic, TComPic** pcRefPicRL )
2558{
2559  for( UInt i = 0; i < m_activeNumILRRefIdx; i++ )
2560  {
2561    UInt refLayerIdc = m_interLayerPredLayerIdc[i];
2562
2563    TComPic* pcRefPicBL = pcRefPicRL[refLayerIdc];
2564    //set reference picture POC of each ILP reference
2565    Int thePoc = ilpPic[refLayerIdc]->getPOC(); 
2566    assert(thePoc >= 0); 
2567    assert(thePoc == pcRefPicBL->getPOC());
2568
2569    ilpPic[refLayerIdc]->getSlice(0)->setBaseColPic( refLayerIdc, pcRefPicBL );
2570
2571    //copy reference pictures marking from the reference layer
2572    ilpPic[refLayerIdc]->getSlice(0)->copySliceInfo(pcRefPicBL->getSlice(0));
2573
2574    for( Int refList = 0; refList < 2; refList++ )
2575    {
2576      RefPicList refPicList = RefPicList( refList );
2577
2578      //set reference POC of ILP
2579      ilpPic[refLayerIdc]->getSlice(0)->setNumRefIdx(refPicList, pcRefPicBL->getSlice(0)->getNumRefIdx(refPicList));
2580      assert(ilpPic[refLayerIdc]->getSlice(0)->getNumRefIdx(refPicList) >= 0);
2581      assert(ilpPic[refLayerIdc]->getSlice(0)->getNumRefIdx(refPicList) <= MAX_NUM_REF);
2582
2583      //initialize reference POC of ILP
2584      for(Int refIdx = 0; refIdx < pcRefPicBL->getSlice(0)->getNumRefIdx(refPicList); refIdx++)
2585      {
2586        ilpPic[refLayerIdc]->getSlice(0)->setRefPOC(pcRefPicBL->getSlice(0)->getRefPOC(refPicList, refIdx), refPicList, refIdx);
2587        ilpPic[refLayerIdc]->getSlice(0)->setRefPic(pcRefPicBL->getSlice(0)->getRefPic(refPicList, refIdx), refPicList, refIdx);
2588      }
2589
2590      for(Int refIdx = pcRefPicBL->getSlice(0)->getNumRefIdx(refPicList); refIdx < MAX_NUM_REF; refIdx++) 
2591      { 
2592        ilpPic[refLayerIdc]->getSlice(0)->setRefPOC(0, refPicList, refIdx); 
2593        ilpPic[refLayerIdc]->getSlice(0)->setRefPic(NULL, refPicList, refIdx); 
2594      }
2595    }
2596  }
2597  return;
2598}
2599#endif
2600
2601//! \}
Note: See TracBrowser for help on using the repository browser.