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

Last change on this file since 242 was 242, checked in by qualcomm, 12 years ago

M0269 : Assert when more than one resampling for reference layers is used.

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