source: 3DVCSoftware/branches/HTM-DEV-0.3-dev0/source/Lib/TLibCommon/TComSlice.cpp @ 497

Last change on this file since 497 was 497, checked in by tech, 11 years ago

Fixes to RefPicList modification.

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