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

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