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

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

Minor clean ups.

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