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

Last change on this file since 490 was 490, checked in by tech, 12 years ago

Integrated all new syntax elements and variables in semantics.

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