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

Last change on this file since 226 was 216, checked in by sharp, 12 years ago

JCTVC-M0203 <hendry.hendry@…> and JCTVC-M0209 - Sachin Deshpande <sdeshpande@…>
Inter-layer prediction indication signaling and decoding

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