source: 3DVCSoftware/branches/HTM-11.2-dev2-Samsung/source/Lib/TLibCommon/TComSlice.cpp @ 1025

Last change on this file since 1025 was 1025, checked in by samsung-htm, 10 years ago

Integration of JCT3V-I0100: vps_extension2 and slice_segment_header clean-up

  • Property svn:eol-style set to native
File size: 132.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-2014, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TComSlice.cpp
35    \brief    slice header and SPS class
36*/
37
38#include "CommonDef.h"
39#include "TComSlice.h"
40#include "TComPic.h"
41#include "TLibEncoder/TEncSbac.h"
42#include "TLibDecoder/TDecSbac.h"
43
44//! \ingroup TLibCommon
45//! \{
46
47
48TComSlice::TComSlice()
49: m_iPPSId                        ( -1 )
50#if H_MV
51, m_slicePicOrderCntLsb           ( 0 )
52#endif
53, m_iPOC                          ( 0 )
54, m_iLastIDR                      ( 0 )
55, m_eNalUnitType                  ( NAL_UNIT_CODED_SLICE_IDR_W_RADL )
56, m_eSliceType                    ( I_SLICE )
57, m_iSliceQp                      ( 0 )
58, m_dependentSliceSegmentFlag            ( false )
59#if ADAPTIVE_QP_SELECTION
60, m_iSliceQpBase                  ( 0 )
61#endif
62, m_deblockingFilterDisable        ( false )
63, m_deblockingFilterOverrideFlag   ( false )
64, m_deblockingFilterBetaOffsetDiv2 ( 0 )
65, m_deblockingFilterTcOffsetDiv2   ( 0 )
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#if SETTING_NO_OUT_PIC_PRIOR
77, m_noOutputPriorPicsFlag         ( false )
78, m_noRaslOutputFlag              ( false )
79, m_handleCraAsBlaFlag              ( false )
80#endif
81, m_colRefIdx                     ( 0 )
82, m_uiTLayer                      ( 0 )
83, m_bTLayerSwitchingFlag          ( false )
84, m_sliceMode                   ( 0 )
85, m_sliceArgument               ( 0 )
86, m_sliceCurStartCUAddr         ( 0 )
87, m_sliceCurEndCUAddr           ( 0 )
88, m_sliceIdx                    ( 0 )
89, m_sliceSegmentMode            ( 0 )
90, m_sliceSegmentArgument        ( 0 )
91, m_sliceSegmentCurStartCUAddr  ( 0 )
92, m_sliceSegmentCurEndCUAddr    ( 0 )
93, m_nextSlice                    ( false )
94, m_nextSliceSegment             ( false )
95, m_sliceBits                   ( 0 )
96, m_sliceSegmentBits         ( 0 )
97, m_bFinalized                    ( false )
98, m_uiTileOffstForMultES          ( 0 )
99, m_puiSubstreamSizes             ( NULL )
100, m_cabacInitFlag                 ( false )
101, m_bLMvdL1Zero                   ( false )
102, m_numEntryPointOffsets          ( 0 )
103, m_temporalLayerNonReferenceFlag ( false )
104, m_enableTMVPFlag                ( true )
105#if H_MV
106, m_refPicSetInterLayer0           ( NULL )
107, m_refPicSetInterLayer1           ( NULL )
108, m_layerId                       (0)
109, m_viewId                        (0)
110, m_viewIndex                     (0)
111#if H_3D
112, m_isDepth                       (false)
113#endif
114#if !H_MV_HLS7_GEN
115, m_pocResetFlag                  (false)
116#endif
117#if H_MV
118, m_crossLayerBlaFlag             (false)
119#endif
120, m_discardableFlag               (false)
121, m_interLayerPredEnabledFlag     (false)
122, m_numInterLayerRefPicsMinus1    (0)
123#if H_MV
124, m_sliceSegmentHeaderExtensionLength (0)
125, m_pocResetIdc                       (0)
126, m_pocResetPeriodId                  (0)
127, m_fullPocResetFlag                  (false)
128, m_pocLsbVal                         (0)
129, m_pocMsbValPresentFlag              (false)
130, m_pocMsbVal                         (0)
131, m_pocMsbValRequiredFlag         ( false )
132#endif
133#if H_3D_IC
134, m_bApplyIC                      ( false )
135, m_icSkipParseFlag               ( false )
136#endif
137#if H_3D
138, m_depthToDisparityB             ( NULL )
139, m_depthToDisparityF             ( NULL )
140#endif
141#endif
142{
143  m_aiNumRefIdx[0] = m_aiNumRefIdx[1] = 0;
144 
145  initEqualRef();
146 
147  for (Int component = 0; component < 3; component++)
148  {
149    m_lambdas[component] = 0.0;
150  }
151 
152  for ( Int idx = 0; idx < MAX_NUM_REF; idx++ )
153  {
154    m_list1IdxToList0Idx[idx] = -1;
155  }
156  for(Int iNumCount = 0; iNumCount < MAX_NUM_REF; iNumCount++)
157  {
158    m_apcRefPicList [0][iNumCount] = NULL;
159    m_apcRefPicList [1][iNumCount] = NULL;
160    m_aiRefPOCList  [0][iNumCount] = 0;
161    m_aiRefPOCList  [1][iNumCount] = 0;
162#if H_MV
163    m_aiRefLayerIdList[0][iNumCount] = 0;
164    m_aiRefLayerIdList[1][iNumCount] = 0;
165#endif
166  }
167  resetWpScaling();
168  initWpAcDcParam();
169  m_saoEnabledFlag = false;
170  m_saoEnabledFlagChroma = false;
171#if H_MV
172  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
173  {
174    m_interLayerPredLayerIdc[ i ] = -1;
175  }
176#endif
177}
178
179TComSlice::~TComSlice()
180{
181  delete[] m_puiSubstreamSizes;
182  m_puiSubstreamSizes = NULL;
183#if H_3D
184  for( UInt i = 0; i < getViewIndex(); i++ )
185  {
186    if ( m_depthToDisparityB && m_depthToDisparityB[ i ] )
187    {
188      delete[] m_depthToDisparityB [ i ];
189    }
190
191    if ( m_depthToDisparityF && m_depthToDisparityF[ i ] ) 
192    {
193      delete[] m_depthToDisparityF [ i ];
194  }
195  }
196
197  if ( m_depthToDisparityF )
198  {
199    delete[] m_depthToDisparityF; 
200  }
201
202  m_depthToDisparityF = NULL;
203
204  if ( m_depthToDisparityB )
205    delete[] m_depthToDisparityB; 
206
207  m_depthToDisparityB = NULL;
208#endif
209}
210
211
212Void TComSlice::initSlice()
213{
214  m_aiNumRefIdx[0]      = 0;
215  m_aiNumRefIdx[1]      = 0;
216 
217  m_colFromL0Flag = 1;
218 
219  m_colRefIdx = 0;
220  initEqualRef();
221  m_bCheckLDC = false;
222  m_iSliceQpDeltaCb = 0;
223  m_iSliceQpDeltaCr = 0;
224
225#if H_3D_IV_MERGE
226  m_maxNumMergeCand = MRG_MAX_NUM_CANDS_MEM;
227#else
228  m_maxNumMergeCand = MRG_MAX_NUM_CANDS;
229#endif
230
231  m_bFinalized=false;
232
233  m_tileByteLocation.clear();
234  m_cabacInitFlag        = false;
235  m_numEntryPointOffsets = 0;
236  m_enableTMVPFlag = true;
237#if H_3D_TMVP
238  m_aiAlterRefIdx[0]                  = -1;
239  m_aiAlterRefIdx[1]                  = -1;
240#endif
241}
242
243Bool TComSlice::getRapPicFlag()
244{
245  return getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
246      || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
247      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
248      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
249      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
250      || getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA;
251}
252
253/**
254 - allocate table to contain substream sizes to be written to the slice header.
255 .
256 \param uiNumSubstreams Number of substreams -- the allocation will be this value - 1.
257 */
258Void  TComSlice::allocSubstreamSizes(UInt uiNumSubstreams)
259{
260  delete[] m_puiSubstreamSizes;
261  m_puiSubstreamSizes = new UInt[uiNumSubstreams > 0 ? uiNumSubstreams-1 : 0];
262}
263
264Void  TComSlice::sortPicList        (TComList<TComPic*>& rcListPic)
265{
266  TComPic*    pcPicExtract;
267  TComPic*    pcPicInsert;
268 
269  TComList<TComPic*>::iterator    iterPicExtract;
270  TComList<TComPic*>::iterator    iterPicExtract_1;
271  TComList<TComPic*>::iterator    iterPicInsert;
272 
273  for (Int i = 1; i < (Int)(rcListPic.size()); i++)
274  {
275    iterPicExtract = rcListPic.begin();
276    for (Int j = 0; j < i; j++) iterPicExtract++;
277    pcPicExtract = *(iterPicExtract);
278    pcPicExtract->setCurrSliceIdx(0);
279   
280    iterPicInsert = rcListPic.begin();
281    while (iterPicInsert != iterPicExtract)
282    {
283      pcPicInsert = *(iterPicInsert);
284      pcPicInsert->setCurrSliceIdx(0);
285      if (pcPicInsert->getPOC() >= pcPicExtract->getPOC())
286      {
287        break;
288      }
289     
290      iterPicInsert++;
291    }
292   
293    iterPicExtract_1 = iterPicExtract;    iterPicExtract_1++;
294   
295    //  swap iterPicExtract and iterPicInsert, iterPicExtract = curr. / iterPicInsert = insertion position
296    rcListPic.insert (iterPicInsert, iterPicExtract, iterPicExtract_1);
297    rcListPic.erase  (iterPicExtract);
298  }
299}
300
301TComPic* TComSlice::xGetRefPic (TComList<TComPic*>& rcListPic,
302                                Int                 poc)
303{
304  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
305  TComPic*                      pcPic = *(iterPic);
306  while ( iterPic != rcListPic.end() )
307  {
308    if(pcPic->getPOC() == poc)
309    {
310      break;
311    }
312    iterPic++;
313    pcPic = *(iterPic);
314  }
315  return  pcPic;
316}
317
318
319TComPic* TComSlice::xGetLongTermRefPic(TComList<TComPic*>& rcListPic, Int poc, Bool pocHasMsb)
320{
321  TComList<TComPic*>::iterator  iterPic = rcListPic.begin(); 
322  TComPic*                      pcPic = *(iterPic);
323  TComPic*                      pcStPic = pcPic;
324 
325  Int pocCycle = 1 << getSPS()->getBitsForPOC();
326  if (!pocHasMsb)
327  {
328    poc = poc & (pocCycle - 1);
329  }
330 
331  while ( iterPic != rcListPic.end() )
332  {
333    pcPic = *(iterPic);
334    if (pcPic && pcPic->getPOC()!=this->getPOC() && pcPic->getSlice( 0 )->isReferenced())
335    {
336      Int picPoc = pcPic->getPOC();
337      if (!pocHasMsb)
338      {
339        picPoc = picPoc & (pocCycle - 1);
340      }
341     
342      if (poc == picPoc)
343    {
344      if(pcPic->getIsLongTerm())
345      {
346        return pcPic;
347      }
348      else
349      {
350        pcStPic = pcPic;
351      }
352      break;
353    }
354    }
355
356    iterPic++;
357  }
358 
359  return  pcStPic;
360}
361
362Void TComSlice::setRefPOCList       ()
363{
364  for (Int iDir = 0; iDir < 2; iDir++)
365  {
366    for (Int iNumRefIdx = 0; iNumRefIdx < m_aiNumRefIdx[iDir]; iNumRefIdx++)
367    {
368      m_aiRefPOCList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getPOC();
369#if H_MV
370      m_aiRefLayerIdList[iDir][iNumRefIdx] = m_apcRefPicList[iDir][iNumRefIdx]->getLayerId();
371#endif
372    }
373  }
374
375}
376
377Void TComSlice::setList1IdxToList0Idx()
378{
379  Int idxL0, idxL1;
380  for ( idxL1 = 0; idxL1 < getNumRefIdx( REF_PIC_LIST_1 ); idxL1++ )
381  {
382    m_list1IdxToList0Idx[idxL1] = -1;
383    for ( idxL0 = 0; idxL0 < getNumRefIdx( REF_PIC_LIST_0 ); idxL0++ )
384    {
385      if ( m_apcRefPicList[REF_PIC_LIST_0][idxL0]->getPOC() == m_apcRefPicList[REF_PIC_LIST_1][idxL1]->getPOC() )
386      {
387        m_list1IdxToList0Idx[idxL1] = idxL0;
388        break;
389      }
390    }
391  }
392}
393
394#if !H_MV
395Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr )
396{
397  if (!checkNumPocTotalCurr)
398  {
399    if (m_eSliceType == I_SLICE)
400    {
401      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
402      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
403
404      return;
405    }
406
407    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
408    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
409  }
410
411  TComPic*  pcRefPic= NULL;
412  TComPic*  RefPicSetStCurr0[16];
413  TComPic*  RefPicSetStCurr1[16];
414  TComPic*  RefPicSetLtCurr[16];
415  UInt NumPocStCurr0 = 0;
416  UInt NumPocStCurr1 = 0;
417  UInt NumPocLtCurr = 0;
418  Int i;
419
420  for(i=0; i < m_pcRPS->getNumberOfNegativePictures(); i++)
421  {
422    if(m_pcRPS->getUsed(i))
423    {
424      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
425      pcRefPic->setIsLongTerm(0);
426      pcRefPic->getPicYuvRec()->extendPicBorder();
427      RefPicSetStCurr0[NumPocStCurr0] = pcRefPic;
428      NumPocStCurr0++;
429      pcRefPic->setCheckLTMSBPresent(false); 
430    }
431  }
432
433  for(; i < m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures(); i++)
434  {
435    if(m_pcRPS->getUsed(i))
436    {
437      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
438      pcRefPic->setIsLongTerm(0);
439      pcRefPic->getPicYuvRec()->extendPicBorder();
440      RefPicSetStCurr1[NumPocStCurr1] = pcRefPic;
441      NumPocStCurr1++;
442      pcRefPic->setCheckLTMSBPresent(false); 
443    }
444  }
445
446  for(i = m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()+m_pcRPS->getNumberOfLongtermPictures()-1; i > m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()-1 ; i--)
447  {
448    if(m_pcRPS->getUsed(i))
449    {
450      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
451      pcRefPic->setIsLongTerm(1);
452      pcRefPic->getPicYuvRec()->extendPicBorder();
453      RefPicSetLtCurr[NumPocLtCurr] = pcRefPic;
454      NumPocLtCurr++;
455    }
456    if(pcRefPic==NULL) 
457    {
458      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
459    }
460    pcRefPic->setCheckLTMSBPresent(m_pcRPS->getCheckLTMSBPresent(i)); 
461  }
462
463  // ref_pic_list_init
464  TComPic*  rpsCurrList0[MAX_NUM_REF+1];
465  TComPic*  rpsCurrList1[MAX_NUM_REF+1];
466  Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr;
467  if (checkNumPocTotalCurr)
468  {
469    // 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:
470    // - If the current picture is a BLA or CRA picture, the value of NumPocTotalCurr shall be equal to 0.
471    // - Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
472    if (getRapPicFlag())
473    {
474      assert(numPocTotalCurr == 0);
475    }
476
477    if (m_eSliceType == I_SLICE)
478    {
479      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
480      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
481
482      return;
483    }
484
485    assert(numPocTotalCurr > 0);
486
487    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
488    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
489  }
490
491  Int cIdx = 0;
492  for ( i=0; i<NumPocStCurr0; i++, cIdx++)
493  {
494    rpsCurrList0[cIdx] = RefPicSetStCurr0[i];
495  }
496  for ( i=0; i<NumPocStCurr1; i++, cIdx++)
497  {
498    rpsCurrList0[cIdx] = RefPicSetStCurr1[i];
499  }
500  for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
501  {
502    rpsCurrList0[cIdx] = RefPicSetLtCurr[i];
503  }
504  assert(cIdx == numPocTotalCurr);
505
506  if (m_eSliceType==B_SLICE)
507  {
508    cIdx = 0;
509    for ( i=0; i<NumPocStCurr1; i++, cIdx++)
510    {
511      rpsCurrList1[cIdx] = RefPicSetStCurr1[i];
512    }
513    for ( i=0; i<NumPocStCurr0; i++, cIdx++)
514    {
515      rpsCurrList1[cIdx] = RefPicSetStCurr0[i];
516    }
517    for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
518    {
519      rpsCurrList1[cIdx] = RefPicSetLtCurr[i];
520    }
521    assert(cIdx == numPocTotalCurr);
522  }
523
524  ::memset(m_bIsUsedAsLongTerm, 0, sizeof(m_bIsUsedAsLongTerm));
525
526  for (Int rIdx = 0; rIdx < m_aiNumRefIdx[0]; rIdx ++)
527  {
528    cIdx = m_RefPicListModification.getRefPicListModificationFlagL0() ? m_RefPicListModification.getRefPicSetIdxL0(rIdx) : rIdx % numPocTotalCurr;
529    assert(cIdx >= 0 && cIdx < numPocTotalCurr);
530    m_apcRefPicList[0][rIdx] = rpsCurrList0[ cIdx ];
531    m_bIsUsedAsLongTerm[0][rIdx] = ( cIdx >= NumPocStCurr0 + NumPocStCurr1 );
532  }
533  if ( m_eSliceType != B_SLICE )
534  {
535    m_aiNumRefIdx[1] = 0;
536    ::memset( m_apcRefPicList[1], 0, sizeof(m_apcRefPicList[1]));
537  }
538  else
539  {
540    for (Int rIdx = 0; rIdx < m_aiNumRefIdx[1]; rIdx ++)
541    {
542      cIdx = m_RefPicListModification.getRefPicListModificationFlagL1() ? m_RefPicListModification.getRefPicSetIdxL1(rIdx) : rIdx % numPocTotalCurr;
543      assert(cIdx >= 0 && cIdx < numPocTotalCurr);
544      m_apcRefPicList[1][rIdx] = rpsCurrList1[ cIdx ];
545      m_bIsUsedAsLongTerm[1][rIdx] = ( cIdx >= NumPocStCurr0 + NumPocStCurr1 );
546    }
547  }
548}
549
550#else
551Void TComSlice::getTempRefPicLists( TComList<TComPic*>& rcListPic, std::vector<TComPic*>& refPicSetInterLayer0, std::vector<TComPic*>& refPicSetInterLayer1,                                     
552                                   std::vector<TComPic*> rpsCurrList[2], std::vector<Bool> usedAsLongTerm[2], Int& numPocTotalCurr, Bool checkNumPocTotalCurr )
553{
554  if (!checkNumPocTotalCurr)
555  {
556    if (m_eSliceType == I_SLICE)
557    {     
558      return;
559    }   
560  }
561
562  TComPic*  pcRefPic= NULL;
563  TComPic*  RefPicSetStCurr0[16];
564  TComPic*  RefPicSetStCurr1[16];
565  TComPic*  RefPicSetLtCurr[16];
566  UInt NumPocStCurr0 = 0;
567  UInt NumPocStCurr1 = 0;
568  UInt NumPocLtCurr = 0;
569  Int i;
570
571  for(i=0; i < m_pcRPS->getNumberOfNegativePictures(); i++)
572  {
573    if(m_pcRPS->getUsed(i))
574    {
575      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
576      pcRefPic->setIsLongTerm(0);
577      pcRefPic->getPicYuvRec()->extendPicBorder();
578      RefPicSetStCurr0[NumPocStCurr0] = pcRefPic;
579      NumPocStCurr0++;
580      pcRefPic->setCheckLTMSBPresent(false); 
581    }
582  }
583 
584  for(; i < m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures(); i++)
585  {
586    if(m_pcRPS->getUsed(i))
587    {
588      pcRefPic = xGetRefPic(rcListPic, getPOC()+m_pcRPS->getDeltaPOC(i));
589      pcRefPic->setIsLongTerm(0);
590      pcRefPic->getPicYuvRec()->extendPicBorder();
591      RefPicSetStCurr1[NumPocStCurr1] = pcRefPic;
592      NumPocStCurr1++;
593      pcRefPic->setCheckLTMSBPresent(false); 
594    }
595  }
596 
597  for(i = m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()+m_pcRPS->getNumberOfLongtermPictures()-1; i > m_pcRPS->getNumberOfNegativePictures()+m_pcRPS->getNumberOfPositivePictures()-1 ; i--)
598  {
599    if(m_pcRPS->getUsed(i))
600    {
601      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
602      pcRefPic->setIsLongTerm(1);
603      pcRefPic->getPicYuvRec()->extendPicBorder();
604      RefPicSetLtCurr[NumPocLtCurr] = pcRefPic;
605      NumPocLtCurr++;
606    }
607    if(pcRefPic==NULL) 
608    {
609      pcRefPic = xGetLongTermRefPic(rcListPic, m_pcRPS->getPOC(i), m_pcRPS->getCheckLTMSBPresent(i));
610    }
611    pcRefPic->setCheckLTMSBPresent(m_pcRPS->getCheckLTMSBPresent(i)); 
612  }
613
614  Int numPocInterCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr; 
615  numPocTotalCurr = numPocInterCurr + getNumActiveRefLayerPics( );
616  assert( numPocTotalCurr == getNumRpsCurrTempList() );
617
618  if (checkNumPocTotalCurr)
619  {
620    // 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:
621    // - 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.
622    // - Otherwise, when the current picture contains a P or B slice, the value of NumPocTotalCurr shall not be equal to 0.
623    if ( getRapPicFlag() && m_layerId == 0 )
624    {
625      assert(numPocTotalCurr == 0);
626    }
627
628    if (m_eSliceType == I_SLICE)
629    {
630      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
631      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
632     
633      return;
634    }
635   
636    assert(numPocTotalCurr > 0);
637   
638    m_aiNumRefIdx[0] = getNumRefIdx(REF_PIC_LIST_0);
639    m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
640  }
641
642  std::vector<TComPic*>* refPicSetInterLayer[2] = { &refPicSetInterLayer0, &refPicSetInterLayer1}; 
643  Int numPocInterLayer[2] = { getNumActiveRefLayerPics0( ), getNumActiveRefLayerPics1( ) }; 
644 
645  TComPic**             refPicSetStCurr    [2] = { RefPicSetStCurr0, RefPicSetStCurr1 };
646  Int numPocStCurr[2] = { NumPocStCurr0, NumPocStCurr1 }; 
647
648  for (Int li = 0; li < ((m_eSliceType==B_SLICE) ? 2 : 1); li++)
649  { 
650    rpsCurrList   [li].resize(MAX_NUM_REF+1,NULL ); 
651    usedAsLongTerm[li].resize(MAX_NUM_REF+1,false); 
652
653    Int cIdx = 0;
654    for ( i=0; i < numPocStCurr[li]; i++, cIdx++)
655    {
656      rpsCurrList[li][cIdx] = refPicSetStCurr[li][i];
657      usedAsLongTerm [li][cIdx] = false; 
658    }
659
660    for ( i=0; i < numPocInterLayer[li];  i++, cIdx++)
661    {   
662      rpsCurrList[li][cIdx] = (*refPicSetInterLayer[li])[i];
663      usedAsLongTerm [li][cIdx] = true; 
664    }
665
666    for ( i=0; i < numPocStCurr[1-li]; i++, cIdx++)
667    {
668      rpsCurrList[li][cIdx] = refPicSetStCurr[1-li][i];
669      usedAsLongTerm [li][cIdx] = false; 
670    }
671
672    for ( i=0; i<NumPocLtCurr;  i++, cIdx++)
673    {
674      rpsCurrList[li][cIdx] = RefPicSetLtCurr[i];
675      usedAsLongTerm [li][cIdx] = true; 
676    }
677
678    for ( i=0; i < numPocInterLayer[1-li];  i++, cIdx++)
679    {   
680      assert( cIdx < MAX_NUM_REF );   
681      rpsCurrList[li][cIdx] = (*refPicSetInterLayer[1-li])[i];
682      usedAsLongTerm [li][cIdx] = true; 
683    }
684
685    assert(cIdx == numPocTotalCurr);
686  }
687}
688
689Void TComSlice::setRefPicList( std::vector<TComPic*> rpsCurrList[2], std::vector<Bool> usedAsLongTerm[2], Int numPocTotalCurr, Bool checkNumPocTotalCurr )
690
691{
692  if (!checkNumPocTotalCurr)
693  {
694    if (m_eSliceType == I_SLICE)
695    {
696      ::memset( m_apcRefPicList, 0, sizeof (m_apcRefPicList));
697      ::memset( m_aiNumRefIdx,   0, sizeof ( m_aiNumRefIdx ));
698
699      return;
700    }   
701  }
702
703  ::memset(m_bIsUsedAsLongTerm, 0, sizeof(m_bIsUsedAsLongTerm));
704
705  for (Int li = 0; li < 2; li++)
706  {
707    if ( m_eSliceType == P_SLICE && li == 1 )
708    {
709      m_aiNumRefIdx[1] = 0;
710      ::memset( m_apcRefPicList[1], 0, sizeof(m_apcRefPicList[1]));
711    } 
712    else
713    {
714      for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[ li ] - 1 ); rIdx ++)
715      { 
716        Bool listModified             =                m_RefPicListModification.getRefPicListModificationFlagL( li ); 
717        Int orgIdx                    = listModified ? m_RefPicListModification.getRefPicSetIdxL(li, rIdx) : (rIdx % numPocTotalCurr); 
718
719        assert( rpsCurrList[li][ orgIdx ] != NULL ); 
720        m_apcRefPicList    [li][rIdx] = rpsCurrList    [li][ orgIdx ];
721        m_bIsUsedAsLongTerm[li][rIdx] = usedAsLongTerm [li][ orgIdx ] ; 
722      }
723    }
724  }
725}
726#endif
727Int TComSlice::getNumRpsCurrTempList()
728{
729  Int numRpsCurrTempList = 0;
730
731  if (m_eSliceType == I_SLICE) 
732  {
733    return 0;
734  }
735  for(UInt i=0; i < m_pcRPS->getNumberOfNegativePictures()+ m_pcRPS->getNumberOfPositivePictures() + m_pcRPS->getNumberOfLongtermPictures(); i++)
736  {
737    if(m_pcRPS->getUsed(i))
738    {
739      numRpsCurrTempList++;
740    }
741  }
742#if H_MV
743  numRpsCurrTempList = numRpsCurrTempList + getNumActiveRefLayerPics();
744#endif
745  return numRpsCurrTempList;
746}
747
748Void TComSlice::initEqualRef()
749{
750  for (Int iDir = 0; iDir < 2; iDir++)
751  {
752    for (Int iRefIdx1 = 0; iRefIdx1 < MAX_NUM_REF; iRefIdx1++)
753    {
754      for (Int iRefIdx2 = iRefIdx1; iRefIdx2 < MAX_NUM_REF; iRefIdx2++)
755      {
756        m_abEqualRef[iDir][iRefIdx1][iRefIdx2] = m_abEqualRef[iDir][iRefIdx2][iRefIdx1] = (iRefIdx1 == iRefIdx2? true : false);
757      }
758    }
759  }
760}
761#if H_3D
762#if H_3D_TMVP
763Void TComSlice::generateAlterRefforTMVP()
764{
765  for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
766  {       
767    if ( this->getNumRefIdx( RefPicList( uiRefListIdx ) ) == 0)
768    {
769        continue;
770    }
771    Bool bZeroIdxLtFlag = this->getRefPic(RefPicList(uiRefListIdx), 0)->getIsLongTerm();
772    for(Int i = 1; i < this->getNumRefIdx(RefPicList(uiRefListIdx)); i++ )
773    {
774      if ( ( bZeroIdxLtFlag && !this->getRefPic(RefPicList(uiRefListIdx), i)->getIsLongTerm() ) ||
775           (!bZeroIdxLtFlag &&  this->getRefPic(RefPicList(uiRefListIdx), i)->getIsLongTerm() ) )
776      {
777        this->setAlterRefIdx(RefPicList(uiRefListIdx),i);
778        break;
779      }
780    }
781  }
782}
783#endif
784Void TComSlice::setCamparaSlice( Int** aaiScale, Int** aaiOffset )
785{ 
786  if( m_pcVPS->hasCamParInSliceHeader( m_viewIndex ) )
787  {   
788    for( UInt uiBaseViewIndex = 0; uiBaseViewIndex < m_viewIndex; uiBaseViewIndex++ )
789    {
790      m_aaiCodedScale [ 0 ][ uiBaseViewIndex ] = aaiScale [ uiBaseViewIndex ][     m_viewIndex ];
791      m_aaiCodedScale [ 1 ][ uiBaseViewIndex ] = aaiScale [     m_viewIndex ][ uiBaseViewIndex ];
792      m_aaiCodedOffset[ 0 ][ uiBaseViewIndex ] = aaiOffset[ uiBaseViewIndex ][     m_viewIndex ];
793      m_aaiCodedOffset[ 1 ][ uiBaseViewIndex ] = aaiOffset[     m_viewIndex ][ uiBaseViewIndex ];
794    }
795  } 
796}
797#endif
798
799Void TComSlice::checkColRefIdx(UInt curSliceIdx, TComPic* pic)
800{
801  Int i;
802  TComSlice* curSlice = pic->getSlice(curSliceIdx);
803  Int currColRefPOC =  curSlice->getRefPOC( RefPicList(1-curSlice->getColFromL0Flag()), curSlice->getColRefIdx());
804  TComSlice* preSlice;
805  Int preColRefPOC;
806  for(i=curSliceIdx-1; i>=0; i--)
807  {
808    preSlice = pic->getSlice(i);
809    if(preSlice->getSliceType() != I_SLICE)
810    {
811      preColRefPOC  = preSlice->getRefPOC( RefPicList(1-preSlice->getColFromL0Flag()), preSlice->getColRefIdx());
812      if(currColRefPOC != preColRefPOC)
813      {
814        printf("Collocated_ref_idx shall always be the same for all slices of a coded picture!\n");
815        exit(EXIT_FAILURE);
816      }
817      else
818      {
819        break;
820      }
821    }
822  }
823}
824
825Void TComSlice::checkCRA(TComReferencePictureSet *pReferencePictureSet, Int& pocCRA, NalUnitType& associatedIRAPType, TComList<TComPic *>& rcListPic)
826{
827  for(Int i = 0; i < pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i++)
828  {
829    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
830    {
831      assert(getPOC()+pReferencePictureSet->getDeltaPOC(i) >= pocCRA);
832    }
833  }
834  for(Int i = pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures(); i < pReferencePictureSet->getNumberOfPictures(); i++)
835  {
836    if(pocCRA < MAX_UINT && getPOC() > pocCRA)
837    {
838      if (!pReferencePictureSet->getCheckLTMSBPresent(i))
839      {
840        assert(xGetLongTermRefPic(rcListPic, pReferencePictureSet->getPOC(i), false)->getPOC() >= pocCRA);
841      }
842      else
843      {
844      assert(pReferencePictureSet->getPOC(i) >= pocCRA);
845    }
846  }
847  }
848  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP ) // IDR picture found
849  {
850    pocCRA = getPOC();
851    associatedIRAPType = getNalUnitType();
852  }
853  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
854  {
855    pocCRA = getPOC();
856    associatedIRAPType = getNalUnitType();
857  }
858  else if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
859         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
860         || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP ) // BLA picture found
861  {
862    pocCRA = getPOC();
863    associatedIRAPType = getNalUnitType();
864  }
865}
866
867/** Function for marking the reference pictures when an IDR/CRA/CRANT/BLA/BLANT is encountered.
868 * \param pocCRA POC of the CRA/CRANT/BLA/BLANT picture
869 * \param bRefreshPending flag indicating if a deferred decoding refresh is pending
870 * \param rcListPic reference to the reference picture list
871 * This function marks the reference pictures as "unused for reference" in the following conditions.
872 * If the nal_unit_type is IDR/BLA/BLANT, all pictures in the reference picture list 
873 * are marked as "unused for reference"
874 *    If the nal_unit_type is BLA/BLANT, set the pocCRA to the temporal reference of the current picture.
875 * Otherwise
876 *    If the bRefreshPending flag is true (a deferred decoding refresh is pending) and the current
877 *    temporal reference is greater than the temporal reference of the latest CRA/CRANT/BLA/BLANT picture (pocCRA),
878 *    mark all reference pictures except the latest CRA/CRANT/BLA/BLANT picture as "unused for reference" and set
879 *    the bRefreshPending flag to false.
880 *    If the nal_unit_type is CRA/CRANT, set the bRefreshPending flag to true and pocCRA to the temporal
881 *    reference of the current picture.
882 * Note that the current picture is already placed in the reference list and its marking is not changed.
883 * If the current picture has a nal_ref_idc that is not 0, it will remain marked as "used for reference".
884 */
885Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic)
886{
887  TComPic*                 rpcPic;
888#if !FIX1172
889  setAssociatedIRAPPOC(pocCRA);
890#endif
891  Int pocCurr = getPOC(); 
892
893  if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
894    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
895    || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
896    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL
897    || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )  // IDR or BLA picture
898  {
899    // mark all pictures as not used for reference
900    TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
901    while (iterPic != rcListPic.end())
902    {
903      rpcPic = *(iterPic);
904      rpcPic->setCurrSliceIdx(0);
905      if (rpcPic->getPOC() != pocCurr) rpcPic->getSlice(0)->setReferenced(false);
906      iterPic++;
907    }
908    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
909      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
910      || getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
911    {
912      pocCRA = pocCurr;
913    }
914#if EFFICIENT_FIELD_IRAP
915    bRefreshPending = true;
916#endif
917  }
918  else // CRA or No DR
919  {
920#if EFFICIENT_FIELD_IRAP
921    if(getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_IDR_N_LP || getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL)
922    {
923      if (bRefreshPending==true && pocCurr > m_iLastIDR) // IDR reference marking pending
924      {
925        TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
926        while (iterPic != rcListPic.end())
927        {
928          rpcPic = *(iterPic);
929          if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != m_iLastIDR)
930          {
931            rpcPic->getSlice(0)->setReferenced(false);
932          }
933          iterPic++;
934        }
935        bRefreshPending = false; 
936      }
937    }
938    else
939    {
940#endif
941    if (bRefreshPending==true && pocCurr > pocCRA) // CRA reference marking pending
942    {
943      TComList<TComPic*>::iterator        iterPic       = rcListPic.begin();
944      while (iterPic != rcListPic.end())
945      {
946        rpcPic = *(iterPic);
947        if (rpcPic->getPOC() != pocCurr && rpcPic->getPOC() != pocCRA)
948        {
949          rpcPic->getSlice(0)->setReferenced(false);
950        }
951        iterPic++;
952      }
953      bRefreshPending = false; 
954    }
955#if EFFICIENT_FIELD_IRAP
956    }
957#endif
958    if ( getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA ) // CRA picture found
959    {
960      bRefreshPending = true; 
961      pocCRA = pocCurr;
962    }
963  }
964}
965
966Void TComSlice::copySliceInfo(TComSlice *pSrc)
967{
968  assert( pSrc != NULL );
969
970  Int i, j, k;
971
972  m_iPOC                 = pSrc->m_iPOC;
973  m_eNalUnitType         = pSrc->m_eNalUnitType;
974#if H_MV
975  m_layerId              = pSrc->m_layerId;
976  // GT: Copying of several other values might be be missing here, or is above not necessary?
977#endif
978  m_eSliceType           = pSrc->m_eSliceType;
979  m_iSliceQp             = pSrc->m_iSliceQp;
980#if ADAPTIVE_QP_SELECTION
981  m_iSliceQpBase         = pSrc->m_iSliceQpBase;
982#endif
983  m_deblockingFilterDisable   = pSrc->m_deblockingFilterDisable;
984  m_deblockingFilterOverrideFlag = pSrc->m_deblockingFilterOverrideFlag;
985  m_deblockingFilterBetaOffsetDiv2 = pSrc->m_deblockingFilterBetaOffsetDiv2;
986  m_deblockingFilterTcOffsetDiv2 = pSrc->m_deblockingFilterTcOffsetDiv2;
987 
988  for (i = 0; i < 2; i++)
989  {
990    m_aiNumRefIdx[i]     = pSrc->m_aiNumRefIdx[i];
991  }
992
993  for (i = 0; i < MAX_NUM_REF; i++)
994  {
995    m_list1IdxToList0Idx[i] = pSrc->m_list1IdxToList0Idx[i];
996  } 
997  m_bCheckLDC             = pSrc->m_bCheckLDC;
998  m_iSliceQpDelta        = pSrc->m_iSliceQpDelta;
999  m_iSliceQpDeltaCb      = pSrc->m_iSliceQpDeltaCb;
1000  m_iSliceQpDeltaCr      = pSrc->m_iSliceQpDeltaCr;
1001  for (i = 0; i < 2; i++)
1002  {
1003    for (j = 0; j < MAX_NUM_REF; j++)
1004    {
1005      m_apcRefPicList[i][j]  = pSrc->m_apcRefPicList[i][j];
1006      m_aiRefPOCList[i][j]   = pSrc->m_aiRefPOCList[i][j];
1007#if H_MV
1008      m_aiRefLayerIdList[i][j] = pSrc->m_aiRefLayerIdList[i][j];
1009#endif
1010    }
1011  }
1012  for (i = 0; i < 2; i++)
1013  {
1014    for (j = 0; j < MAX_NUM_REF + 1; j++)
1015    {
1016      m_bIsUsedAsLongTerm[i][j] = pSrc->m_bIsUsedAsLongTerm[i][j];
1017    }
1018  }
1019  m_iDepth               = pSrc->m_iDepth;
1020
1021  // referenced slice
1022  m_bRefenced            = pSrc->m_bRefenced;
1023
1024  // access channel
1025#if H_MV
1026  m_pcVPS                = pSrc->m_pcVPS;
1027#endif
1028  m_pcSPS                = pSrc->m_pcSPS;
1029  m_pcPPS                = pSrc->m_pcPPS;
1030  m_pcRPS                = pSrc->m_pcRPS;
1031  m_iLastIDR             = pSrc->m_iLastIDR;
1032
1033  m_pcPic                = pSrc->m_pcPic;
1034
1035  m_colFromL0Flag        = pSrc->m_colFromL0Flag;
1036  m_colRefIdx            = pSrc->m_colRefIdx;
1037  setLambdas(pSrc->getLambdas());
1038  for (i = 0; i < 2; i++)
1039  {
1040    for (j = 0; j < MAX_NUM_REF; j++)
1041    {
1042      for (k =0; k < MAX_NUM_REF; k++)
1043      {
1044        m_abEqualRef[i][j][k] = pSrc->m_abEqualRef[i][j][k];
1045      }
1046    }
1047  }
1048
1049  m_uiTLayer                      = pSrc->m_uiTLayer;
1050  m_bTLayerSwitchingFlag          = pSrc->m_bTLayerSwitchingFlag;
1051
1052  m_sliceMode                   = pSrc->m_sliceMode;
1053  m_sliceArgument               = pSrc->m_sliceArgument;
1054  m_sliceCurStartCUAddr         = pSrc->m_sliceCurStartCUAddr;
1055  m_sliceCurEndCUAddr           = pSrc->m_sliceCurEndCUAddr;
1056  m_sliceIdx                    = pSrc->m_sliceIdx;
1057  m_sliceSegmentMode            = pSrc->m_sliceSegmentMode;
1058  m_sliceSegmentArgument        = pSrc->m_sliceSegmentArgument; 
1059  m_sliceSegmentCurStartCUAddr  = pSrc->m_sliceSegmentCurStartCUAddr;
1060  m_sliceSegmentCurEndCUAddr    = pSrc->m_sliceSegmentCurEndCUAddr;
1061  m_nextSlice                    = pSrc->m_nextSlice;
1062  m_nextSliceSegment             = pSrc->m_nextSliceSegment;
1063  for ( Int e=0 ; e<2 ; e++ )
1064  {
1065    for ( Int n=0 ; n<MAX_NUM_REF ; n++ )
1066    {
1067      memcpy(m_weightPredTable[e][n], pSrc->m_weightPredTable[e][n], sizeof(wpScalingParam)*3 );
1068    }
1069  }
1070  m_saoEnabledFlag = pSrc->m_saoEnabledFlag; 
1071  m_saoEnabledFlagChroma = pSrc->m_saoEnabledFlagChroma;
1072  m_cabacInitFlag                = pSrc->m_cabacInitFlag;
1073  m_numEntryPointOffsets  = pSrc->m_numEntryPointOffsets;
1074
1075  m_bLMvdL1Zero = pSrc->m_bLMvdL1Zero;
1076  m_LFCrossSliceBoundaryFlag = pSrc->m_LFCrossSliceBoundaryFlag;
1077  m_enableTMVPFlag                = pSrc->m_enableTMVPFlag;
1078  m_maxNumMergeCand               = pSrc->m_maxNumMergeCand;
1079
1080#if H_MV
1081  // Additional slice header syntax elements
1082#if !H_MV_HLS7_GEN
1083  m_pocResetFlag               = pSrc->m_pocResetFlag; 
1084#endif
1085  m_discardableFlag            = pSrc->m_discardableFlag; 
1086  m_interLayerPredEnabledFlag  = pSrc->m_interLayerPredEnabledFlag; 
1087  m_numInterLayerRefPicsMinus1 = pSrc->m_numInterLayerRefPicsMinus1;
1088
1089  for (Int layer = 0; layer < MAX_NUM_LAYERS; layer++ )
1090  {
1091    m_interLayerPredLayerIdc[ layer ] = pSrc->m_interLayerPredLayerIdc[ layer ]; 
1092  }
1093#endif
1094#if H_3D_IC
1095  m_bApplyIC = pSrc->m_bApplyIC;
1096  m_icSkipParseFlag = pSrc->m_icSkipParseFlag;
1097#endif
1098}
1099
1100Int TComSlice::m_prevTid0POC = 0;
1101
1102/** Function for setting the slice's temporal layer ID and corresponding temporal_layer_switching_point_flag.
1103 * \param uiTLayer Temporal layer ID of the current slice
1104 * The decoder calls this function to set temporal_layer_switching_point_flag for each temporal layer based on
1105 * the SPS's temporal_id_nesting_flag and the parsed PPS.  Then, current slice's temporal layer ID and
1106 * temporal_layer_switching_point_flag is set accordingly.
1107 */
1108Void TComSlice::setTLayerInfo( UInt uiTLayer )
1109{
1110  m_uiTLayer = uiTLayer;
1111}
1112
1113/** Function for checking if this is a switching-point
1114*/
1115Bool TComSlice::isTemporalLayerSwitchingPoint( TComList<TComPic*>& rcListPic )
1116{
1117  TComPic* rpcPic;
1118  // loop through all pictures in the reference picture buffer
1119  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1120  while ( iterPic != rcListPic.end())
1121  {
1122    rpcPic = *(iterPic++);
1123    if(rpcPic->getSlice(0)->isReferenced() && rpcPic->getPOC() != getPOC())
1124    {
1125      if(rpcPic->getTLayer() >= getTLayer())
1126      {
1127        return false;
1128      }
1129    }
1130  }
1131  return true;
1132}
1133
1134/** Function for checking if this is a STSA candidate
1135 */
1136Bool TComSlice::isStepwiseTemporalLayerSwitchingPointCandidate( TComList<TComPic*>& rcListPic )
1137{
1138    TComPic* rpcPic;
1139   
1140    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1141    while ( iterPic != rcListPic.end())
1142    {
1143        rpcPic = *(iterPic++);
1144        if(rpcPic->getSlice(0)->isReferenced() &&  (rpcPic->getUsedByCurr()==true) && rpcPic->getPOC() != getPOC())
1145        {
1146            if(rpcPic->getTLayer() >= getTLayer())
1147            {
1148                return false;
1149            }
1150        }
1151    }
1152    return true;
1153}
1154
1155
1156Void TComSlice::checkLeadingPictureRestrictions(TComList<TComPic*>& rcListPic)
1157{
1158  TComPic* rpcPic;
1159
1160  Int nalUnitType = this->getNalUnitType();
1161
1162  // When a picture is a leading picture, it shall be a RADL or RASL picture.
1163  if(this->getAssociatedIRAPPOC() > this->getPOC())
1164  {
1165    // Do not check IRAP pictures since they may get a POC lower than their associated IRAP
1166    if(nalUnitType < NAL_UNIT_CODED_SLICE_BLA_W_LP ||
1167       nalUnitType > NAL_UNIT_RESERVED_IRAP_VCL23)
1168    {
1169      assert(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1170             nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R ||
1171             nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1172             nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R);
1173    }
1174  }
1175
1176  // When a picture is a trailing picture, it shall not be a RADL or RASL picture.
1177  if(this->getAssociatedIRAPPOC() < this->getPOC())
1178  {
1179    assert(nalUnitType != NAL_UNIT_CODED_SLICE_RASL_N &&
1180           nalUnitType != NAL_UNIT_CODED_SLICE_RASL_R &&
1181           nalUnitType != NAL_UNIT_CODED_SLICE_RADL_N &&
1182           nalUnitType != NAL_UNIT_CODED_SLICE_RADL_R);
1183  }
1184
1185  // No RASL pictures shall be present in the bitstream that are associated
1186  // with a BLA picture having nal_unit_type equal to BLA_W_RADL or BLA_N_LP.
1187  if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1188     nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1189  {
1190    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_W_RADL &&
1191           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_N_LP);
1192  }
1193
1194  // No RASL pictures shall be present in the bitstream that are associated with
1195  // an IDR picture.
1196  if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1197     nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1198  {
1199    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_N_LP   &&
1200           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_W_RADL);
1201  }
1202
1203  // No RADL pictures shall be present in the bitstream that are associated with
1204  // a BLA picture having nal_unit_type equal to BLA_N_LP or that are associated
1205  // with an IDR picture having nal_unit_type equal to IDR_N_LP.
1206  if(nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1207     nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R)
1208  {
1209    assert(this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_BLA_N_LP   &&
1210           this->getAssociatedIRAPType() != NAL_UNIT_CODED_SLICE_IDR_N_LP);
1211  }
1212
1213  // loop through all pictures in the reference picture buffer
1214  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1215  while ( iterPic != rcListPic.end())
1216  {
1217    rpcPic = *(iterPic++);
1218#if BUGFIX_INTRAPERIOD
1219    if(!rpcPic->getReconMark())
1220    {
1221      continue;
1222    }
1223#endif
1224    if (rpcPic->getPOC() == this->getPOC())
1225    {
1226      continue;
1227    }
1228
1229    // Any picture that has PicOutputFlag equal to 1 that precedes an IRAP picture
1230    // in decoding order shall precede the IRAP picture in output order.
1231    // (Note that any picture following in output order would be present in the DPB)
1232#if !SETTING_NO_OUT_PIC_PRIOR
1233    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1)
1234#else
1235    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1 && !this->getNoOutputPriorPicsFlag())
1236#endif
1237    {
1238      if(nalUnitType == NAL_UNIT_CODED_SLICE_BLA_N_LP    ||
1239         nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_LP    ||
1240         nalUnitType == NAL_UNIT_CODED_SLICE_BLA_W_RADL  ||
1241         nalUnitType == NAL_UNIT_CODED_SLICE_CRA         ||
1242         nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP    ||
1243         nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL)
1244      {
1245        assert(rpcPic->getPOC() < this->getPOC());
1246      }
1247    }
1248
1249    // Any picture that has PicOutputFlag equal to 1 that precedes an IRAP picture
1250    // in decoding order shall precede any RADL picture associated with the IRAP
1251    // picture in output order.
1252    if(rpcPic->getSlice(0)->getPicOutputFlag() == 1)
1253    {
1254      if((nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1255          nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R))
1256      {
1257        // rpcPic precedes the IRAP in decoding order
1258        if(this->getAssociatedIRAPPOC() > rpcPic->getSlice(0)->getAssociatedIRAPPOC())
1259        {
1260          // rpcPic must not be the IRAP picture
1261          if(this->getAssociatedIRAPPOC() != rpcPic->getPOC())
1262          {
1263            assert(rpcPic->getPOC() < this->getPOC());
1264          }
1265        }
1266      }
1267    }
1268
1269    // When a picture is a leading picture, it shall precede, in decoding order,
1270    // all trailing pictures that are associated with the same IRAP picture.
1271    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1272       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R ||
1273       nalUnitType == NAL_UNIT_CODED_SLICE_RADL_N ||
1274       nalUnitType == NAL_UNIT_CODED_SLICE_RADL_R)
1275    {
1276      if(rpcPic->getSlice(0)->getAssociatedIRAPPOC() == this->getAssociatedIRAPPOC())
1277      {
1278        // rpcPic is a picture that preceded the leading in decoding order since it exist in the DPB
1279        // rpcPic would violate the constraint if it was a trailing picture
1280        assert(rpcPic->getPOC() <= this->getAssociatedIRAPPOC());
1281      }
1282    }
1283
1284    // Any RASL picture associated with a CRA or BLA picture shall precede any
1285    // RADL picture associated with the CRA or BLA picture in output order
1286    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1287       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1288    { 
1289      if((this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ||
1290          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ||
1291          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ||
1292          this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_CRA)       &&
1293          this->getAssociatedIRAPPOC() == rpcPic->getSlice(0)->getAssociatedIRAPPOC())
1294      {
1295        if(rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N ||
1296           rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_R)
1297        {
1298          assert(rpcPic->getPOC() > this->getPOC());
1299        }
1300      }
1301    }
1302
1303    // Any RASL picture associated with a CRA picture shall follow, in output
1304    // order, any IRAP picture that precedes the CRA picture in decoding order.
1305    if(nalUnitType == NAL_UNIT_CODED_SLICE_RASL_N ||
1306       nalUnitType == NAL_UNIT_CODED_SLICE_RASL_R)
1307    {
1308      if(this->getAssociatedIRAPType() == NAL_UNIT_CODED_SLICE_CRA)
1309      {
1310        if(rpcPic->getSlice(0)->getPOC() < this->getAssociatedIRAPPOC() &&
1311           (rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ||
1312            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ||
1313            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ||
1314            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP   ||
1315            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL ||
1316            rpcPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA))
1317        {
1318          assert(this->getPOC() > rpcPic->getSlice(0)->getPOC());
1319        }
1320      }
1321    }
1322  }
1323}
1324
1325
1326/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1327*/
1328Void TComSlice::applyReferencePictureSet( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
1329{
1330  TComPic* rpcPic;
1331  Int i, isReference;
1332
1333  checkLeadingPictureRestrictions(rcListPic);
1334
1335  // loop through all pictures in the reference picture buffer
1336  TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1337  while ( iterPic != rcListPic.end())
1338  {
1339    rpcPic = *(iterPic++);
1340
1341    if(!rpcPic->getSlice( 0 )->isReferenced())
1342    {
1343      continue;
1344    }
1345
1346    isReference = 0;
1347    // loop through all pictures in the Reference Picture Set
1348    // to see if the picture should be kept as reference picture
1349    for(i=0;i<pReferencePictureSet->getNumberOfPositivePictures()+pReferencePictureSet->getNumberOfNegativePictures();i++)
1350    {
1351      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i))
1352      {
1353        isReference = 1;
1354        rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1355        rpcPic->setIsLongTerm(0);
1356      }
1357    }
1358    for(;i<pReferencePictureSet->getNumberOfPictures();i++)
1359    {
1360      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1361      {
1362        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i))
1363        {
1364          isReference = 1;
1365          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1366        }
1367      }
1368      else 
1369      {
1370        Int pocCycle = 1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1371        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC() & (pocCycle-1);
1372        Int refPoc = pReferencePictureSet->getPOC(i) & (pocCycle-1);
1373        if(rpcPic->getIsLongTerm() && curPoc == refPoc)
1374        {
1375          isReference = 1;
1376          rpcPic->setUsedByCurr(pReferencePictureSet->getUsed(i));
1377        }
1378      }
1379
1380    }
1381#if H_MV
1382    if( isReference ) // Current picture is in the temporal RPS
1383    {
1384      assert( rpcPic->getSlice(0)->getDiscardableFlag() == 0 ); // Temporal RPS shall not contain picture with discardable_flag equal to 1
1385    }
1386#endif
1387    // mark the picture as "unused for reference" if it is not in
1388    // the Reference Picture Set
1389    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && isReference == 0)   
1390    {           
1391      rpcPic->getSlice( 0 )->setReferenced( false );   
1392      rpcPic->setUsedByCurr(0);
1393      rpcPic->setIsLongTerm(0);
1394    }
1395    //check that pictures of higher temporal layers are not used
1396    assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
1397    //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
1398    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
1399    {
1400      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
1401    }
1402    //check that pictures marked as temporal layer non-reference pictures are not used for reference
1403    if(rpcPic->getPicSym()->getSlice(0)->getPOC() != this->getPOC() && rpcPic->getTLayer()==this->getTLayer())
1404    {
1405      assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getSlice( 0 )->getTemporalLayerNonReferenceFlag()==false);
1406    }
1407  }
1408}
1409
1410/** Function for applying picture marking based on the Reference Picture Set in pReferencePictureSet.
1411*/
1412#if ALLOW_RECOVERY_POINT_AS_RAP
1413Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess, Bool bUseRecoveryPoint)
1414#else
1415Int TComSlice::checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess)
1416#endif
1417{
1418#if ALLOW_RECOVERY_POINT_AS_RAP
1419  Int atLeastOneUnabledByRecoveryPoint = 0;
1420  Int atLeastOneFlushedByPreviousIDR = 0;
1421#endif
1422  TComPic* rpcPic;
1423  Int i, isAvailable;
1424  Int atLeastOneLost = 0;
1425  Int atLeastOneRemoved = 0;
1426  Int iPocLost = 0;
1427
1428  // loop through all long-term pictures in the Reference Picture Set
1429  // to see if the picture should be kept as reference picture
1430  for(i=pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i<pReferencePictureSet->getNumberOfPictures();i++)
1431  {
1432    isAvailable = 0;
1433    // loop through all pictures in the reference picture buffer
1434    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1435    while ( iterPic != rcListPic.end())
1436    {
1437      rpcPic = *(iterPic++);
1438      if(pReferencePictureSet->getCheckLTMSBPresent(i)==true)
1439      {
1440        if(rpcPic->getIsLongTerm() && (rpcPic->getPicSym()->getSlice(0)->getPOC()) == pReferencePictureSet->getPOC(i) && rpcPic->getSlice(0)->isReferenced())
1441        {
1442#if ALLOW_RECOVERY_POINT_AS_RAP
1443          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1444          {
1445            isAvailable = 0;
1446          }
1447          else
1448          {
1449          isAvailable = 1;
1450        }
1451#else
1452          isAvailable = 1;
1453#endif
1454        }
1455      }
1456      else 
1457      {
1458        Int pocCycle = 1<<rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1459        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC() & (pocCycle-1);
1460        Int refPoc = pReferencePictureSet->getPOC(i) & (pocCycle-1);
1461        if(rpcPic->getIsLongTerm() && curPoc == refPoc && rpcPic->getSlice(0)->isReferenced())
1462        {
1463#if ALLOW_RECOVERY_POINT_AS_RAP
1464          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1465          {
1466            isAvailable = 0;
1467          }
1468          else
1469          {
1470          isAvailable = 1;
1471        }
1472#else
1473          isAvailable = 1;
1474#endif
1475        }
1476      }
1477    }
1478    // if there was no such long-term check the short terms
1479    if(!isAvailable)
1480    {
1481      iterPic = rcListPic.begin();
1482      while ( iterPic != rcListPic.end())
1483      {
1484        rpcPic = *(iterPic++);
1485
1486        Int pocCycle = 1 << rpcPic->getPicSym()->getSlice(0)->getSPS()->getBitsForPOC();
1487        Int curPoc = rpcPic->getPicSym()->getSlice(0)->getPOC();
1488        Int refPoc = pReferencePictureSet->getPOC(i);
1489        if (!pReferencePictureSet->getCheckLTMSBPresent(i))
1490        {
1491          curPoc = curPoc & (pocCycle - 1);
1492          refPoc = refPoc & (pocCycle - 1);
1493        }
1494       
1495        if (rpcPic->getSlice(0)->isReferenced() && curPoc == refPoc)
1496        {
1497#if ALLOW_RECOVERY_POINT_AS_RAP
1498          if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1499          {
1500            isAvailable = 0;
1501          }
1502          else
1503          {
1504          isAvailable = 1;
1505          rpcPic->setIsLongTerm(1);
1506          break;
1507        }
1508#else
1509          isAvailable = 1;
1510          rpcPic->setIsLongTerm(1);
1511          break;
1512#endif
1513        }
1514      }
1515    }
1516    // report that a picture is lost if it is in the Reference Picture Set
1517    // but not available as reference picture
1518    if(isAvailable == 0)   
1519    {           
1520      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1521      {
1522        if(!pReferencePictureSet->getUsed(i) )
1523        {
1524          if(printErrors)
1525          {
1526            printf("\nLong-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1527          }
1528          atLeastOneRemoved = 1;
1529        }
1530        else
1531        {
1532          if(printErrors)
1533          {
1534            printf("\nLong-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1535          }
1536          atLeastOneLost = 1;
1537          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1538        }
1539      }
1540#if ALLOW_RECOVERY_POINT_AS_RAP
1541      else if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess)
1542      {
1543        atLeastOneUnabledByRecoveryPoint = 1;
1544      }
1545      else if(bUseRecoveryPoint && (this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_N_LP || this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_W_RADL))
1546      {
1547        atLeastOneFlushedByPreviousIDR = 1;
1548      }
1549#endif
1550    }
1551  } 
1552  // loop through all short-term pictures in the Reference Picture Set
1553  // to see if the picture should be kept as reference picture
1554  for(i=0;i<pReferencePictureSet->getNumberOfNegativePictures()+pReferencePictureSet->getNumberOfPositivePictures();i++)
1555  {
1556    isAvailable = 0;
1557    // loop through all pictures in the reference picture buffer
1558    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1559    while ( iterPic != rcListPic.end())
1560    {
1561      rpcPic = *(iterPic++);
1562
1563      if(!rpcPic->getIsLongTerm() && rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1564      {
1565#if ALLOW_RECOVERY_POINT_AS_RAP
1566        if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess)
1567        {
1568          isAvailable = 0;
1569        }
1570        else
1571        {
1572        isAvailable = 1;
1573      }
1574#else
1575        isAvailable = 1;
1576#endif
1577      }
1578    }
1579    // report that a picture is lost if it is in the Reference Picture Set
1580    // but not available as reference picture
1581    if(isAvailable == 0)   
1582    {           
1583      if (this->getPOC() + pReferencePictureSet->getDeltaPOC(i) >= pocRandomAccess)
1584      {
1585        if(!pReferencePictureSet->getUsed(i) )
1586        {
1587          if(printErrors)
1588          {
1589            printf("\nShort-term reference picture with POC = %3d seems to have been removed or not correctly decoded.", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1590          }
1591          atLeastOneRemoved = 1;
1592        }
1593        else
1594        {
1595          if(printErrors)
1596          {
1597            printf("\nShort-term reference picture with POC = %3d is lost or not correctly decoded!", this->getPOC() + pReferencePictureSet->getDeltaPOC(i));
1598          }
1599          atLeastOneLost = 1;
1600          iPocLost=this->getPOC() + pReferencePictureSet->getDeltaPOC(i);
1601        }
1602      }
1603#if ALLOW_RECOVERY_POINT_AS_RAP
1604      else if(bUseRecoveryPoint && this->getPOC() > pocRandomAccess)
1605      {
1606        atLeastOneUnabledByRecoveryPoint = 1;
1607    }
1608      else if(bUseRecoveryPoint && (this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_N_LP || this->getAssociatedIRAPType()==NAL_UNIT_CODED_SLICE_IDR_W_RADL))
1609      {
1610        atLeastOneFlushedByPreviousIDR = 1;
1611  }   
1612#endif
1613    }
1614    }
1615#if ALLOW_RECOVERY_POINT_AS_RAP
1616  if(atLeastOneUnabledByRecoveryPoint || atLeastOneFlushedByPreviousIDR)
1617  {
1618    return -1;
1619  }   
1620#endif
1621  if(atLeastOneLost)
1622  {
1623    return iPocLost+1;
1624  }
1625  if(atLeastOneRemoved)
1626  {
1627    return -2;
1628  }
1629  else
1630  {
1631    return 0;
1632  }
1633}
1634
1635/** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
1636*/
1637#if ALLOW_RECOVERY_POINT_AS_RAP
1638Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool isRAP, Int pocRandomAccess, Bool bUseRecoveryPoint)
1639#else
1640Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool isRAP)
1641#endif
1642{
1643  TComPic* rpcPic;
1644  Int i, j;
1645  Int k = 0;
1646  Int nrOfNegativePictures = 0;
1647  Int nrOfPositivePictures = 0;
1648  TComReferencePictureSet* pcRPS = this->getLocalRPS();
1649
1650  // loop through all pictures in the Reference Picture Set
1651  for(i=0;i<pReferencePictureSet->getNumberOfPictures();i++)
1652  {
1653    j = 0;
1654    // loop through all pictures in the reference picture buffer
1655    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1656    while ( iterPic != rcListPic.end())
1657    {
1658      j++;
1659      rpcPic = *(iterPic++);
1660
1661      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getPOC() + pReferencePictureSet->getDeltaPOC(i) && rpcPic->getSlice(0)->isReferenced())
1662      {
1663        // This picture exists as a reference picture
1664        // and should be added to the explicit Reference Picture Set
1665        pcRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
1666        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i) && (!isRAP));
1667#if ALLOW_RECOVERY_POINT_AS_RAP
1668        pcRPS->setUsed(k, pcRPS->getUsed(k) && !(bUseRecoveryPoint && this->getPOC() > pocRandomAccess && this->getPOC() + pReferencePictureSet->getDeltaPOC(i) < pocRandomAccess) ); 
1669#endif
1670        if(pcRPS->getDeltaPOC(k) < 0)
1671        {
1672          nrOfNegativePictures++;
1673        }
1674        else
1675        {
1676          nrOfPositivePictures++;
1677        }
1678        k++;
1679      }
1680    }
1681  }
1682#if EFFICIENT_FIELD_IRAP
1683  Bool useNewRPS = false;
1684  // if current picture is complimentary field associated to IRAP, add the IRAP to its RPS.
1685  if(m_pcPic->isField())
1686  {
1687    TComList<TComPic*>::iterator iterPic = rcListPic.begin();
1688    while ( iterPic != rcListPic.end())
1689    {
1690      rpcPic = *(iterPic++);
1691      if(rpcPic->getPicSym()->getSlice(0)->getPOC() == this->getAssociatedIRAPPOC() && this->getAssociatedIRAPPOC() == this->getPOC()+1)
1692      {
1693        pcRPS->setDeltaPOC(k, 1);
1694        pcRPS->setUsed(k, true);
1695        nrOfPositivePictures++;
1696        k ++;
1697        useNewRPS = true;
1698      }
1699    }
1700  }
1701#endif
1702  pcRPS->setNumberOfNegativePictures(nrOfNegativePictures);
1703  pcRPS->setNumberOfPositivePictures(nrOfPositivePictures);
1704  pcRPS->setNumberOfPictures(nrOfNegativePictures+nrOfPositivePictures);
1705  // This is a simplistic inter rps example. A smarter encoder will look for a better reference RPS to do the
1706  // inter RPS prediction with.  Here we just use the reference used by pReferencePictureSet.
1707  // If pReferencePictureSet is not inter_RPS_predicted, then inter_RPS_prediction is for the current RPS also disabled.
1708  if (!pReferencePictureSet->getInterRPSPrediction()
1709#if EFFICIENT_FIELD_IRAP
1710    || useNewRPS
1711#endif
1712    )
1713  {
1714    pcRPS->setInterRPSPrediction(false);
1715    pcRPS->setNumRefIdc(0);
1716  }
1717  else
1718  {
1719    Int rIdx =  this->getRPSidx() - pReferencePictureSet->getDeltaRIdxMinus1() - 1;
1720    Int deltaRPS = pReferencePictureSet->getDeltaRPS();
1721    TComReferencePictureSet* pcRefRPS = this->getSPS()->getRPSList()->getReferencePictureSet(rIdx);
1722    Int iRefPics = pcRefRPS->getNumberOfPictures();
1723    Int iNewIdc=0;
1724    for(i=0; i<= iRefPics; i++) 
1725    {
1726      Int deltaPOC = ((i != iRefPics)? pcRefRPS->getDeltaPOC(i) : 0);  // check if the reference abs POC is >= 0
1727      Int iRefIdc = 0;
1728      for (j=0; j < pcRPS->getNumberOfPictures(); j++) // loop through the  pictures in the new RPS
1729      {
1730        if ( (deltaPOC + deltaRPS) == pcRPS->getDeltaPOC(j))
1731        {
1732          if (pcRPS->getUsed(j))
1733          {
1734            iRefIdc = 1;
1735          }
1736          else
1737          {
1738            iRefIdc = 2;
1739          }
1740        }
1741      }
1742      pcRPS->setRefIdc(i, iRefIdc);
1743      iNewIdc++;
1744    }
1745    pcRPS->setInterRPSPrediction(true);
1746    pcRPS->setNumRefIdc(iNewIdc);
1747    pcRPS->setDeltaRPS(deltaRPS); 
1748    pcRPS->setDeltaRIdxMinus1(pReferencePictureSet->getDeltaRIdxMinus1() + this->getSPS()->getRPSList()->getNumberOfReferencePictureSets() - this->getRPSidx());
1749  }
1750
1751  this->setRPS(pcRPS);
1752  this->setRPSidx(-1);
1753}
1754
1755/** get AC and DC values for weighted pred
1756 * \param *wp
1757 * \returns Void
1758 */
1759Void  TComSlice::getWpAcDcParam(wpACDCParam *&wp)
1760{
1761  wp = m_weightACDCParam;
1762}
1763
1764/** init AC and DC values for weighted pred
1765 * \returns Void
1766 */
1767Void  TComSlice::initWpAcDcParam()
1768{
1769  for(Int iComp = 0; iComp < 3; iComp++ )
1770  {
1771    m_weightACDCParam[iComp].iAC = 0;
1772    m_weightACDCParam[iComp].iDC = 0;
1773  }
1774}
1775
1776/** get WP tables for weighted pred
1777 * \param RefPicList
1778 * \param iRefIdx
1779 * \param *&wpScalingParam
1780 * \returns Void
1781 */
1782Void  TComSlice::getWpScaling( RefPicList e, Int iRefIdx, wpScalingParam *&wp )
1783{
1784  wp = m_weightPredTable[e][iRefIdx];
1785}
1786
1787/** reset Default WP tables settings : no weight.
1788 * \param wpScalingParam
1789 * \returns Void
1790 */
1791Void  TComSlice::resetWpScaling()
1792{
1793  for ( Int e=0 ; e<2 ; e++ )
1794  {
1795    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1796    {
1797      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1798      {
1799        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1800        pwp->bPresentFlag      = false;
1801        pwp->uiLog2WeightDenom = 0;
1802        pwp->uiLog2WeightDenom = 0;
1803        pwp->iWeight           = 1;
1804        pwp->iOffset           = 0;
1805      }
1806    }
1807  }
1808}
1809
1810/** init WP table
1811 * \returns Void
1812 */
1813Void  TComSlice::initWpScaling()
1814{
1815  for ( Int e=0 ; e<2 ; e++ )
1816  {
1817    for ( Int i=0 ; i<MAX_NUM_REF ; i++ )
1818    {
1819      for ( Int yuv=0 ; yuv<3 ; yuv++ )
1820      {
1821        wpScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
1822        if ( !pwp->bPresentFlag ) 
1823        {
1824          // Inferring values not present :
1825          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
1826          pwp->iOffset = 0;
1827        }
1828
1829        pwp->w      = pwp->iWeight;
1830        Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
1831        pwp->o      = pwp->iOffset << (bitDepth-8);
1832        pwp->shift  = pwp->uiLog2WeightDenom;
1833        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
1834      }
1835    }
1836  }
1837}
1838
1839// ------------------------------------------------------------------------------------------------
1840// Video parameter set (VPS)
1841// ------------------------------------------------------------------------------------------------
1842TComVPS::TComVPS()
1843: m_VPSId                     (  0)
1844, m_uiMaxTLayers              (  1)
1845#if H_MV
1846, m_uiMaxLayersMinus1         (  0)
1847#else
1848, m_uiMaxLayers               (  1)
1849#endif
1850, m_bTemporalIdNestingFlag    (false)
1851, m_numHrdParameters          (  0)
1852#if H_MV
1853, m_maxLayerId             (  0)
1854#else
1855, m_maxNuhReservedZeroLayerId (  0)
1856#endif
1857, m_hrdParameters             (NULL)
1858, m_hrdOpSetIdx               (NULL)
1859, m_cprmsPresentFlag          (NULL)
1860#if H_MV
1861, m_dpbSize                   (NULL)
1862, m_vpsVUI                 (  NULL )
1863#endif
1864{
1865#if H_MV
1866  m_vpsBaseLayerInternalFlag = true; 
1867#endif
1868
1869  for( Int i = 0; i < MAX_TLAYER; i++)
1870  {
1871    m_numReorderPics[i] = 0;
1872    m_uiMaxDecPicBuffering[i] = 1; 
1873    m_uiMaxLatencyIncrease[i] = 0;
1874  }
1875#if H_MV
1876  for (Int lsIdx = 0; lsIdx < MAX_VPS_OP_SETS_PLUS1; lsIdx++ )
1877  { 
1878    for( Int layerId = 0; layerId < MAX_VPS_NUH_LAYER_ID_PLUS1; layerId++ )
1879    {
1880      m_layerIdIncludedFlag[lsIdx][layerId] = (( lsIdx == 0 ) && ( layerId == 0 )) ; 
1881    }
1882  } 
1883  m_vpsNumProfileTierLevelMinus1 = -1; 
1884   
1885  m_numAddLayerSets              = 0;   
1886  m_numAddOlss                   = 0; 
1887  m_defaultOutputLayerIdc     = 0; 
1888 
1889  for ( Int i = 0; i < MAX_VPS_OUTPUTLAYER_SETS; i++)
1890  {
1891    m_layerSetIdxForOlsMinus1[i]  = -1; 
1892    m_profileLevelTierIdx[i]      = 0; 
1893    for ( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++)
1894    {
1895      m_outputLayerFlag[i][j] = false; 
1896    }
1897    m_altOutputLayerFlag[ i ]       = false; 
1898  }
1899
1900  m_repFormatIdxPresentFlag = false; 
1901  m_maxOneActiveRefLayerFlag = false; 
1902  m_directDepTypeLenMinus2   = 0;         
1903 
1904
1905  m_vpsExtensionFlag = true; 
1906  m_vpsNonVuiExtensionLength = 0;
1907  m_splittingFlag    = false;
1908 
1909  for( Int i = 0; i < MAX_NUM_SCALABILITY_TYPES; i++ )
1910  {
1911    m_scalabilityMaskFlag[i] = false;
1912    m_dimensionIdLen [i]  = -1; 
1913  }
1914
1915  m_vpsNuhLayerIdPresentFlag = false;
1916
1917  for( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++ )
1918  {
1919    m_vpsProfilePresentFlag   [i] = false;
1920    m_layerSetIdxForOlsMinus1       [i] = 0;
1921    for( Int j = 0; j < MAX_VPS_NUH_LAYER_ID_PLUS1; j++ )
1922    {
1923      m_outputLayerFlag[i][j] = false;
1924    }
1925  }
1926
1927  for( Int i = 0; i < MAX_NUM_LAYER_IDS; i++ )
1928  {
1929    m_layerIdInVps[i] =  (i == 0 ) ? 0 : -1;         
1930  }
1931
1932  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1933  {
1934    m_layerIdInNuh      [i] = ( i == 0 ) ? 0 : -1; 
1935    m_numDirectRefLayers[i] = 0; 
1936    m_vpsRepFormatIdx    [i] = 0; 
1937    m_pocLsbNotPresentFlag[i] = 0;
1938    m_repFormat          [i] = NULL; 
1939    m_viewIdVal          [i] = 0; 
1940
1941#if H_3D
1942    m_viewIndex         [i] = -1; 
1943    m_vpsDepthModesFlag [i] = false;
1944#if SEC_HLS_CLEANUP_I0100
1945    m_ivMvScalingFlag[i] = true; 
1946#else
1947    m_ivMvScalingFlag = true; 
1948#endif
1949#endif
1950
1951    for( Int j = 0; j < MAX_NUM_LAYERS; j++ )
1952    {
1953      m_directDependencyFlag[i][j] = false;
1954      m_directDependencyType[i][j] = -1; 
1955      m_refLayerId[i][j]           = -1; 
1956      m_maxTidIlRefPicsPlus1[i][j]  = 7;
1957    }
1958
1959    for( Int j = 0; j < MAX_NUM_SCALABILITY_TYPES; j++ )
1960    {
1961      m_dimensionId[i][j] = 0;
1962    }
1963#if H_3D_ARP
1964    m_uiUseAdvResPred[i]  = 0;
1965    m_uiARPStepNum[i]     = 1;
1966#endif
1967  }
1968  m_vpsVUI = new TComVPSVUI; 
1969  m_dpbSize = new TComDpbSize; 
1970
1971#if H_3D
1972  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
1973  {
1974#if H_3D_IV_MERGE
1975    m_ivMvPredFlag         [ i ] = false;
1976#if H_3D_SPIVMP
1977    m_iSubPULog2Size       [ i ] = 0;
1978#endif
1979#endif
1980#if MTK_I0099_VPS_EX2
1981    m_bLimQtPredFlag       [ i ] = false;
1982#endif
1983#if H_3D_VSP
1984    m_viewSynthesisPredFlag[ i ] = false;
1985#endif
1986#if H_3D_NBDV_REF
1987    m_depthRefinementFlag  [ i ] = false;
1988#endif
1989#if H_3D_INTER_SDC
1990    m_bInterSDCFlag        [ i ] = false;
1991#endif
1992#if H_3D_DBBP
1993    m_dbbpFlag             [ i ] = false;
1994#endif
1995#if H_3D_IV_MERGE
1996    m_bMPIFlag             [ i ] = false;
1997#endif
1998  } 
1999#endif
2000#endif
2001}
2002
2003TComVPS::~TComVPS()
2004{
2005  if( m_hrdParameters    != NULL )     delete[] m_hrdParameters;
2006  if( m_hrdOpSetIdx      != NULL )     delete[] m_hrdOpSetIdx;
2007  if( m_cprmsPresentFlag != NULL )     delete[] m_cprmsPresentFlag;
2008#if H_MV
2009  if ( m_vpsVUI          != NULL )     delete m_vpsVUI; 
2010  if ( m_dpbSize         != NULL )     delete m_dpbSize; 
2011
2012  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
2013  {
2014    if (m_repFormat[ i ] != NULL )      delete m_repFormat[ i ];   
2015  }
2016#endif
2017#if H_3D
2018  deleteCamPars();
2019#endif
2020}
2021
2022#if H_MV
2023
2024Bool TComVPS::checkVPSExtensionSyntax()
2025{
2026  for( Int layer = 1; layer <= getMaxLayersMinus1(); layer++ )
2027  {
2028    // check layer_id_in_nuh constraint
2029    assert( getLayerIdInNuh( layer ) > getLayerIdInNuh( layer -1 ) );
2030  }
2031
2032  //The value of vps_num_rep_formats_minus1 shall be in the range of 0 to 255, inclusive.
2033  assert( getVpsNumRepFormatsMinus1() >= 0 ); 
2034  assert( getVpsNumRepFormatsMinus1() <= 255 ); 
2035
2036  return true; 
2037}
2038
2039Int TComVPS::getNumScalabilityTypes()
2040{
2041  return scalTypeToScalIdx( ScalabilityType(MAX_NUM_SCALABILITY_TYPES) );
2042}
2043
2044Int TComVPS::scalTypeToScalIdx( ScalabilityType scalType )
2045{
2046  assert( scalType >= 0 && scalType <= MAX_NUM_SCALABILITY_TYPES ); 
2047  assert( scalType == MAX_NUM_SCALABILITY_TYPES || getScalabilityMaskFlag( scalType ) );
2048  Int scalIdx = 0; 
2049  for( Int curScalType = 0; curScalType < scalType; curScalType++ )
2050  {
2051    scalIdx += ( getScalabilityMaskFlag( curScalType ) ? 1 : 0 );
2052
2053  }
2054
2055  return scalIdx; 
2056}
2057Void TComVPS::setScalabilityMaskFlag( UInt val )
2058{
2059  for ( Int scalType = 0; scalType < MAX_NUM_SCALABILITY_TYPES; scalType++ ) 
2060  {
2061    setScalabilityMaskFlag( scalType, ( val & (1 << scalType ) ) != 0 );
2062  }
2063}
2064
2065Void TComVPS::setRefLayers()
2066{
2067  for( Int i = 0; i  <= getMaxLayersMinus1(); i++ )
2068  {
2069    Int iNuhLId = getLayerIdInNuh( i ); 
2070    m_numDirectRefLayers[ iNuhLId ] = 0; 
2071    for( Int j = 0; j < i; j++ )
2072    {
2073      if( getDirectDependencyFlag(i , j) )
2074      {
2075        m_refLayerId[ iNuhLId ][m_numDirectRefLayers[ iNuhLId ]++ ] = getLayerIdInNuh( j );
2076      }
2077    }
2078  }
2079
2080  for (Int i = 0 ; i < MAX_NUM_LAYER_IDS; i++ )
2081  {
2082    m_numRefLayers[i] = 0; 
2083  }
2084
2085  for (Int currLayerId = 0; currLayerId <= 62; currLayerId++ ) 
2086  {
2087    for (Int i = 0 ; i < MAX_NUM_LAYER_IDS; i++ )
2088    {
2089      m_recursiveRefLayerFlag[currLayerId][i] = 0; 
2090    }
2091  }
2092
2093  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
2094  {
2095    Int iNuhLId = getLayerIdInNuh( i );
2096    xSetRefLayerFlags( iNuhLId );
2097    for( Int j = 0; j < 63; j++ )
2098    {
2099      m_numRefLayers[ iNuhLId ]  +=  m_recursiveRefLayerFlag[ iNuhLId ][ j ];
2100    }
2101  }
2102
2103  for( Int i = 0; i <= getMaxLayersMinus1(); i++ )  // Bug in spec "<" instead of "<="
2104  {
2105    Int iNuhLId = getLayerIdInNuh( i );
2106    Int predIdx = 0;
2107    for( Int j = iNuhLId + 1; j < 63; j++ )
2108    {
2109      if( m_recursiveRefLayerFlag[ j ][ iNuhLId ] )
2110      {
2111        m_predictedLayerId[ iNuhLId ][ predIdx++ ] = j;
2112      }
2113    }
2114    m_numPredictedLayers[ iNuhLId ] = predIdx;
2115  }
2116 
2117  Bool countedLayerIdxFlag[ MAX_NUM_LAYERS ]; 
2118  for( Int i = 0; i  <=  getMaxLayersMinus1(); i++ )
2119  {
2120    countedLayerIdxFlag[ i ] = 0;
2121  }
2122 
2123  for( Int i = 0, k = 0; i  <=  getMaxLayersMinus1(); i++ )
2124  {
2125    Int iNuhLId = getLayerIdInNuh( i );
2126    if( m_numDirectRefLayers[ iNuhLId ]  ==  0 )
2127    {
2128      m_treePartitionLayerIdList[ k ][ 0 ] = iNuhLId;
2129      m_numLayersInTreePartition[ k ]      = 1;
2130      for( Int j = 0; j < m_numPredictedLayers[ iNuhLId ]; j++ ) 
2131      {
2132        if( !countedLayerIdxFlag[ getLayerIdInVps( m_predictedLayerId[ iNuhLId ][ j ] ) ] )   
2133        {
2134          m_treePartitionLayerIdList[ k ][ m_numLayersInTreePartition[ k ] ] = m_predictedLayerId[ iNuhLId ][ j ];
2135          m_numLayersInTreePartition[ k ]++;
2136          countedLayerIdxFlag[ getLayerIdInVps( m_predictedLayerId[ iNuhLId ][ j ] ) ] = 1;
2137        }
2138      }
2139      k++;
2140    }
2141    m_numIndependentLayers = k;
2142  }
2143}
2144
2145Int TComVPS::getRefLayerId( Int layerIdInNuh, Int idx )
2146{
2147  assert( idx >= 0 && idx < m_numDirectRefLayers[layerIdInNuh] );     
2148  Int refLayerIdInNuh = m_refLayerId[ layerIdInNuh ][ idx ];   
2149  assert ( refLayerIdInNuh >= 0 ); 
2150  return refLayerIdInNuh;
2151}
2152
2153Int TComVPS::getScalabilityId( Int layerIdInVps, ScalabilityType scalType )
2154{
2155  return getScalabilityMaskFlag( scalType ) ? getDimensionId( layerIdInVps, scalTypeToScalIdx( scalType ) ) : 0;
2156}
2157
2158#if H_3D
2159Int TComVPS::getLayerIdInNuh( Int viewIndex, Bool depthFlag )
2160{
2161  Int foundLayerIdinNuh = -1; 
2162
2163  for (Int layerIdInVps = 0 ; layerIdInVps <= getMaxLayersMinus1(); layerIdInVps++ )
2164  {
2165    Int layerIdInNuh = getLayerIdInNuh( layerIdInVps ); 
2166    if( ( getViewIndex( layerIdInNuh ) == viewIndex ) && ( getDepthId( layerIdInNuh ) == ( depthFlag ? 1 : 0 ) )  )
2167    {
2168      foundLayerIdinNuh = layerIdInNuh; 
2169      break; 
2170    }
2171  }
2172#if !BUG_FIX_TK65
2173  assert( foundLayerIdinNuh != -1 ); 
2174#endif
2175  return foundLayerIdinNuh;
2176}
2177
2178Void TComVPS::createCamPars(Int iNumViews)
2179{
2180  Int i = 0, j = 0;
2181
2182  m_bCamParPresent = new Bool[ iNumViews ];
2183  m_bCamParInSliceHeader = new Bool[ iNumViews ];
2184
2185  m_aaaiCodedScale = new Int**[ iNumViews ];
2186  m_aaaiCodedOffset = new Int**[ iNumViews ];
2187  for ( i = 0; i < iNumViews ; i++ )
2188  {
2189    m_bCamParInSliceHeader[i] = false; 
2190    m_aaaiCodedScale[i] = new Int*[ 2 ];
2191    m_aaaiCodedOffset[i] = new Int*[ 2 ];
2192    for ( j = 0; j < 2; j++ )
2193    {
2194      m_aaaiCodedScale[i][j] = new Int[ MAX_NUM_LAYERS ];
2195      m_aaaiCodedOffset[i][j] = new Int[ MAX_NUM_LAYERS ];
2196      for ( Int k = 0; k < MAX_NUM_LAYERS; k++ )
2197      {
2198        m_aaaiCodedScale[i][j][k] = 0;
2199        m_aaaiCodedOffset[i][j][k] = 0;
2200      }
2201    }
2202  }
2203}
2204
2205Void TComVPS::deleteCamPars()
2206{
2207  Int iNumViews = getNumViews();
2208  Int i = 0, j = 0;
2209
2210  if ( m_bCamParPresent != NULL )
2211  {
2212    delete [] m_bCamParPresent;
2213  }
2214  if ( m_bCamParInSliceHeader != NULL )
2215  {
2216    delete [] m_bCamParInSliceHeader;
2217  }
2218
2219  if ( m_aaaiCodedScale != NULL )
2220  {
2221    for ( i = 0; i < iNumViews ; i++ )
2222    {
2223      for ( j = 0; j < 2; j++ )
2224      {
2225        delete [] m_aaaiCodedScale[i][j];
2226      }
2227      delete [] m_aaaiCodedScale[i];
2228    }
2229    delete [] m_aaaiCodedScale;
2230  }
2231
2232  if ( m_aaaiCodedOffset != NULL )
2233  {
2234    for ( i = 0; i < iNumViews ; i++ )
2235    {
2236      for ( j = 0; j < 2; j++ )
2237      {
2238        delete [] m_aaaiCodedOffset[i][j];
2239      }
2240      delete [] m_aaaiCodedOffset[i];
2241    }
2242    delete [] m_aaaiCodedOffset;
2243  }
2244}
2245
2246
2247Void
2248  TComVPS::initCamParaVPS( UInt uiViewIndex, Bool bCamParPresent, UInt uiCamParPrecision, Bool bCamParSlice, Int** aaiScale, Int** aaiOffset )
2249{
2250  AOT( uiViewIndex != 0 && !bCamParSlice && ( aaiScale == 0 || aaiOffset == 0 ) ); 
2251
2252  m_uiCamParPrecision = ( ( uiViewIndex != 0 )? uiCamParPrecision : 0 );
2253  m_bCamParPresent[ uiViewIndex ] = (( uiViewIndex != 0 )? bCamParPresent  : false );
2254  m_bCamParInSliceHeader[ uiViewIndex ]  = ( (uiViewIndex != 0)? bCamParSlice  : false );
2255
2256  if( !m_bCamParInSliceHeader[ uiViewIndex ] )
2257  {
2258    for( UInt uiBaseViewIndex = 0; uiBaseViewIndex < uiViewIndex; uiBaseViewIndex++ )
2259    {
2260      m_aaaiCodedScale [ uiViewIndex ][ 0 ][ uiBaseViewIndex ] = aaiScale [ uiBaseViewIndex ][     uiViewIndex ];
2261      m_aaaiCodedScale [ uiViewIndex ][ 1 ][ uiBaseViewIndex ] = aaiScale [     uiViewIndex ][ uiBaseViewIndex ];
2262      m_aaaiCodedOffset[ uiViewIndex ][ 0 ][ uiBaseViewIndex ] = aaiOffset[ uiBaseViewIndex ][     uiViewIndex ];
2263      m_aaaiCodedOffset[ uiViewIndex ][ 1 ][ uiBaseViewIndex ] = aaiOffset[     uiViewIndex ][ uiBaseViewIndex ];
2264    }
2265  }
2266}
2267
2268#endif // H_3D
2269
2270
2271Int TComVPS::xGetDimBitOffset( Int j )
2272{
2273  Int dimBitOffset = 0; 
2274  if ( getSplittingFlag() && j == getNumScalabilityTypes() )
2275  {
2276     dimBitOffset = 6; 
2277  }
2278  else
2279  {
2280    for (Int dimIdx = 0; dimIdx <= j-1; dimIdx++)
2281    {
2282      dimBitOffset += getDimensionIdLen( dimIdx ); 
2283    }
2284  }
2285  return dimBitOffset; 
2286}
2287
2288Int TComVPS::inferDimensionId( Int i, Int j )
2289{
2290    return ( ( getLayerIdInNuh( i ) & ( (1 << xGetDimBitOffset( j + 1 ) ) - 1) ) >> xGetDimBitOffset( j ) ); 
2291}
2292
2293Int TComVPS::inferLastDimsionIdLenMinus1()
2294{
2295  return ( 5 - xGetDimBitOffset( getNumScalabilityTypes() - 1 ) ); 
2296}
2297
2298Int TComVPS::getNumLayersInIdList( Int lsIdx )
2299{
2300  assert( lsIdx >= 0 ); 
2301  assert( lsIdx <= getVpsNumLayerSetsMinus1() ); 
2302  return (Int) m_layerSetLayerIdList[ lsIdx ].size(); 
2303}
2304
2305Int    TComVPS::getNumOutputLayerSets() 
2306{
2307  return getNumAddOlss() + getNumLayerSets(); 
2308}
2309
2310Int TComVPS::getNumViews()
2311{
2312  Int numViews = 1; 
2313  for( Int i = 0; i <=  getMaxLayersMinus1(); i++ )
2314  {
2315    Int lId = getLayerIdInNuh( i ); 
2316    if ( i > 0 && ( getViewIndex( lId ) != getScalabilityId( i - 1, VIEW_ORDER_INDEX ) ) )
2317    {
2318      numViews++; 
2319    }   
2320  }
2321
2322  return numViews;
2323}
2324
2325Bool TComVPS::getInDirectDependencyFlag( Int depLayeridInVps, Int refLayeridInVps, Int depth /*= 0 */ )
2326{
2327  assert( depth < 65 ); 
2328  Bool dependentFlag = getDirectDependencyFlag( depLayeridInVps, refLayeridInVps ); 
2329
2330  for( Int i = 0; i < depLayeridInVps && !dependentFlag; i++ )
2331  {
2332    if ( getDirectDependencyFlag( depLayeridInVps, i ) )
2333    {
2334      dependentFlag = getInDirectDependencyFlag( i, refLayeridInVps, depth++ ); 
2335    }
2336  }
2337  return dependentFlag;
2338}
2339
2340Void TComVPS::deriveLayerSetLayerIdList()
2341{
2342  m_layerSetLayerIdList.resize( getVpsNumLayerSetsMinus1() + 1 ); 
2343  for (Int i = 0; i <= getVpsNumLayerSetsMinus1(); i++ )
2344  {
2345    for( Int m = 0; m  <= getVpsMaxLayerId(); m++ )
2346    {
2347      if( getLayerIdIncludedFlag( i, m) ) 
2348      {
2349        m_layerSetLayerIdList[ i ].push_back( m );       
2350      }
2351    }
2352  }
2353}
2354
2355Void TComVPS::initTargetLayerIdLists()
2356{
2357  m_targetDecLayerIdLists.resize( getNumOutputLayerSets() ); 
2358  m_targetOptLayerIdLists.resize( getNumOutputLayerSets() ); 
2359}
2360
2361Void TComVPS::deriveTargetLayerIdList( Int i )
2362{ 
2363  Int lsIdx = olsIdxToLsIdx( i );     
2364 
2365  for( Int j = 0; j < getNumLayersInIdList( lsIdx ); j++ )
2366  {
2367    m_targetDecLayerIdLists[i].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] ); 
2368    if( getOutputLayerFlag( i, j  ))
2369    {
2370      m_targetOptLayerIdLists[i].push_back( m_layerSetLayerIdList[ lsIdx ][ j ] );
2371    }
2372  } 
2373  assert( getNumOutputLayersInOutputLayerSet( i ) > 0 ); 
2374}
2375
2376Bool TComVPS::inferOutputLayerFlag( Int i, Int j )
2377{
2378  Bool outputLayerFlag; 
2379  switch ( getDefaultOutputLayerIdc( ) )
2380  {
2381  case 0:
2382    outputLayerFlag = true; 
2383    break; 
2384  case 1:
2385    outputLayerFlag = ( j == m_layerSetLayerIdList[ olsIdxToLsIdx( i ) ].size() - 1 ); 
2386    break;
2387  case 2:
2388    if ( i == 0 && j == 0)
2389    {     
2390      outputLayerFlag = true;  // This is a software only fix for a bug in the spec. In spec outputLayerFlag is neither present nor inferred.
2391    }
2392    else
2393    {
2394      assert( 0 ); 
2395    }
2396    break; 
2397  default:     
2398    assert( 0 );
2399    break; 
2400  }
2401  return outputLayerFlag;
2402}
2403
2404Int TComVPS::getMaxSubLayersInLayerSetMinus1( Int i )
2405{
2406  Int maxSLMinus1 = 0; 
2407  for( Int k = 0; k < getNumLayersInIdList( i ); k++ )
2408  {
2409    Int lId = m_layerSetLayerIdList[i][k];
2410    maxSLMinus1 = std::max( maxSLMinus1, getSubLayersVpsMaxMinus1( getLayerIdInVps( lId ) ));
2411  }
2412  return maxSLMinus1;
2413}
2414
2415Void TComVPS::inferDbpSizeLayerSetZero( TComSPS* sps, Bool encoder )
2416{
2417  for( Int j = 0; j <= getMaxSubLayersInLayerSetMinus1( 0 ); j++ )
2418  {
2419    Int maxDecPicBufferingMinus1 = sps->getMaxDecPicBuffering( j ) - 1; 
2420    Int numReorderPics           = sps->getNumReorderPics    ( j ); 
2421    Int maxLatencyIncreasePlus1  = sps->getMaxLatencyIncrease( j ); 
2422
2423    if ( encoder )
2424    {
2425      assert( getDpbSize()->getMaxVpsDecPicBufferingMinus1(0, 0, j ) == maxDecPicBufferingMinus1 );
2426      assert( getDpbSize()->getMaxVpsNumReorderPics       (0,    j ) == numReorderPics           );
2427      assert( getDpbSize()->getMaxVpsLatencyIncreasePlus1 (0,    j ) == maxLatencyIncreasePlus1  );
2428    }
2429    else
2430    {
2431      getDpbSize()->setMaxVpsDecPicBufferingMinus1(0, 0, j, maxDecPicBufferingMinus1 );
2432      getDpbSize()->setMaxVpsNumReorderPics       (0,    j, numReorderPics           );
2433      getDpbSize()->setMaxVpsLatencyIncreasePlus1 (0,    j, maxLatencyIncreasePlus1  );       
2434    }     
2435  }
2436}
2437
2438Bool TComVPS::getAltOutputLayerFlagVar( Int i )
2439{
2440  // Semantics variable not syntax element !
2441
2442  Bool altOptLayerFlag = false;     
2443  if ( i > 0 && getNumOutputLayersInOutputLayerSet( i ) == 1 && 
2444    getNumDirectRefLayers( getOlsHighestOutputLayerId( i ) ) > 0 )
2445  {
2446    altOptLayerFlag = getAltOutputLayerFlag( i ); 
2447  }
2448  return altOptLayerFlag;
2449}
2450
2451Int TComVPS::getMaxNumPics( Int layerId )
2452{
2453  Int maxNumPics = MAX_INT; 
2454  for( Int olsIdx = 0; olsIdx < getNumOutputLayerSets(); olsIdx++)
2455  {
2456    Int lsIdx = olsIdxToLsIdx( olsIdx ); 
2457    for( Int j = 0; j < getNumLayersInIdList( lsIdx ); j++ )
2458    {
2459      if( getLayerSetLayerIdList(lsIdx, j ) ==  layerId )
2460      {
2461        Int maxSL = getMaxSubLayersInLayerSetMinus1( lsIdx ); 
2462        maxNumPics = std::min( maxNumPics, getDpbSize()->getMaxVpsDecPicBufferingMinus1( olsIdx , j,  maxSL ) );
2463      }
2464    }
2465  }
2466  assert( maxNumPics != MAX_INT ); 
2467  return maxNumPics;
2468}
2469
2470Void TComVPS::xSetRefLayerFlags( Int currLayerId )
2471{
2472  for( Int j = 0; j < getNumDirectRefLayers( currLayerId ); j++ )
2473  {
2474    Int refLayerId = m_refLayerId[ currLayerId ][ j ];
2475    m_recursiveRefLayerFlag[ currLayerId ][ refLayerId ] = 1;
2476    for( Int k = 0; k < MAX_NUM_LAYER_IDS; k++ )
2477    {
2478      m_recursiveRefLayerFlag[ currLayerId ][ k ] = m_recursiveRefLayerFlag[ currLayerId ][ k ]  ||  m_recursiveRefLayerFlag[ refLayerId ][ k ];
2479    }
2480  }
2481}
2482
2483#endif // H_MV
2484
2485// ------------------------------------------------------------------------------------------------
2486// Sequence parameter set (SPS)
2487// ------------------------------------------------------------------------------------------------
2488
2489TComSPS::TComSPS()
2490: m_SPSId                     (  0)
2491, m_VPSId                     (  0)
2492, m_chromaFormatIdc           (CHROMA_420)
2493, m_uiMaxTLayers              (  1)
2494// Structure
2495, m_picWidthInLumaSamples     (352)
2496, m_picHeightInLumaSamples    (288)
2497, m_log2MinCodingBlockSize    (  0)
2498, m_log2DiffMaxMinCodingBlockSize (0)
2499, m_uiMaxCUWidth              ( 32)
2500, m_uiMaxCUHeight             ( 32)
2501, m_uiMaxCUDepth              (  3)
2502, m_bLongTermRefsPresent      (false)
2503, m_uiQuadtreeTULog2MaxSize   (  0)
2504, m_uiQuadtreeTULog2MinSize   (  0)
2505, m_uiQuadtreeTUMaxDepthInter (  0)
2506, m_uiQuadtreeTUMaxDepthIntra (  0)
2507// Tool list
2508, m_usePCM                   (false)
2509, m_pcmLog2MaxSize            (  5)
2510, m_uiPCMLog2MinSize          (  7)
2511#if !MTK_I0099_VPS_EX2
2512#if H_3D_QTLPC
2513, m_bUseQTL                   (false)
2514, m_bUsePC                    (false)
2515#endif
2516#endif
2517, m_bitDepthY                 (  8)
2518, m_bitDepthC                 (  8)
2519, m_qpBDOffsetY               (  0)
2520, m_qpBDOffsetC               (  0)
2521, m_uiPCMBitDepthLuma         (  8)
2522, m_uiPCMBitDepthChroma       (  8)
2523, m_bPCMFilterDisableFlag     (false)
2524, m_uiBitsForPOC              (  8)
2525, m_numLongTermRefPicSPS    (  0) 
2526, m_uiMaxTrSize               ( 32)
2527, m_bUseSAO                   (false) 
2528, m_bTemporalIdNestingFlag    (false)
2529, m_scalingListEnabledFlag    (false)
2530, m_useStrongIntraSmoothing   (false)
2531, m_vuiParametersPresentFlag  (false)
2532, m_vuiParameters             ()
2533#if H_MV
2534, m_pcVPS                     ( NULL )
2535, m_spsInferScalingListFlag   ( false )
2536, m_spsScalingListRefLayerId  ( 0 )
2537
2538, m_updateRepFormatFlag       ( false ) 
2539, m_spsRepFormatIdx           ( 0 )
2540, m_interViewMvVertConstraintFlag (false)
2541#endif
2542#if H_3D
2543, m_bCamParInSliceHeader      (false)
2544#endif
2545{
2546  for ( Int i = 0; i < MAX_TLAYER; i++ )
2547  {
2548    m_uiMaxLatencyIncrease[i] = 0;
2549    m_uiMaxDecPicBuffering[i] = 1;
2550    m_numReorderPics[i]       = 0;
2551  }
2552  m_scalingList = new TComScalingList;
2553  ::memset(m_ltRefPicPocLsbSps, 0, sizeof(m_ltRefPicPocLsbSps));
2554  ::memset(m_usedByCurrPicLtSPSFlag, 0, sizeof(m_usedByCurrPicLtSPSFlag));
2555#if H_MV
2556  m_spsRangeExtensionsFlag     = false;
2557  m_spsMultilayerExtensionFlag = false;
2558#if H_3D
2559  m_spsExtension5bits          = 0;
2560  m_sps3dExtensionFlag         = false; 
2561#else
2562  m_spsExtension6bits          = 0;
2563#endif
2564
2565  m_numScaledRefLayerOffsets = 0; 
2566
2567  for (Int i = 0; i < MAX_NUM_SCALED_REF_LAYERS; i++ )
2568  {
2569    m_scaledRefLayerId             [i] = -1;
2570  }
2571
2572  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
2573  {
2574    m_scaledRefLayerLeftOffset     [i] = 0;
2575    m_scaledRefLayerTopOffset      [i] = 0;
2576    m_scaledRefLayerRightOffset    [i] = 0;
2577    m_scaledRefLayerBottomOffset   [i] = 0;
2578  }
2579#endif
2580}
2581
2582TComSPS::~TComSPS()
2583{
2584  delete m_scalingList;
2585  m_RPSList.destroy();
2586}
2587
2588Void  TComSPS::createRPSList( Int numRPS )
2589{ 
2590  m_RPSList.destroy();
2591  m_RPSList.create(numRPS);
2592}
2593
2594Void TComSPS::setHrdParameters( UInt frameRate, UInt numDU, UInt bitRate, Bool randomAccess )
2595{
2596  if( !getVuiParametersPresentFlag() )
2597  {
2598    return;
2599  }
2600
2601  TComVUI *vui = getVuiParameters();
2602  TComHRD *hrd = vui->getHrdParameters();
2603
2604  TimingInfo *timingInfo = vui->getTimingInfo();
2605  timingInfo->setTimingInfoPresentFlag( true );
2606  switch( frameRate )
2607  {
2608  case 24:
2609    timingInfo->setNumUnitsInTick( 1125000 );    timingInfo->setTimeScale    ( 27000000 );
2610    break;
2611  case 25:
2612    timingInfo->setNumUnitsInTick( 1080000 );    timingInfo->setTimeScale    ( 27000000 );
2613    break;
2614  case 30:
2615    timingInfo->setNumUnitsInTick( 900900 );     timingInfo->setTimeScale    ( 27000000 );
2616    break;
2617  case 50:
2618    timingInfo->setNumUnitsInTick( 540000 );     timingInfo->setTimeScale    ( 27000000 );
2619    break;
2620  case 60:
2621    timingInfo->setNumUnitsInTick( 450450 );     timingInfo->setTimeScale    ( 27000000 );
2622    break;
2623  default:
2624    timingInfo->setNumUnitsInTick( 1001 );       timingInfo->setTimeScale    ( 60000 );
2625    break;
2626  }
2627
2628  Bool rateCnt = ( bitRate > 0 );
2629  hrd->setNalHrdParametersPresentFlag( rateCnt );
2630  hrd->setVclHrdParametersPresentFlag( rateCnt );
2631
2632  hrd->setSubPicCpbParamsPresentFlag( ( numDU > 1 ) );
2633
2634  if( hrd->getSubPicCpbParamsPresentFlag() )
2635  {
2636    hrd->setTickDivisorMinus2( 100 - 2 );                          //
2637    hrd->setDuCpbRemovalDelayLengthMinus1( 7 );                    // 8-bit precision ( plus 1 for last DU in AU )
2638    hrd->setSubPicCpbParamsInPicTimingSEIFlag( true );
2639    hrd->setDpbOutputDelayDuLengthMinus1( 5 + 7 );                 // With sub-clock tick factor of 100, at least 7 bits to have the same value as AU dpb delay
2640  }
2641  else
2642  {
2643    hrd->setSubPicCpbParamsInPicTimingSEIFlag( false ); 
2644  }
2645
2646  hrd->setBitRateScale( 4 );                                       // in units of 2~( 6 + 4 ) = 1,024 bps
2647  hrd->setCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2648  hrd->setDuCpbSizeScale( 6 );                                       // in units of 2~( 4 + 4 ) = 1,024 bit
2649 
2650  hrd->setInitialCpbRemovalDelayLengthMinus1(15);                  // assuming 0.5 sec, log2( 90,000 * 0.5 ) = 16-bit
2651  if( randomAccess )
2652  {
2653    hrd->setCpbRemovalDelayLengthMinus1(5);                        // 32 = 2^5 (plus 1)
2654    hrd->setDpbOutputDelayLengthMinus1 (5);                        // 32 + 3 = 2^6
2655  }
2656  else
2657  {
2658    hrd->setCpbRemovalDelayLengthMinus1(9);                        // max. 2^10
2659    hrd->setDpbOutputDelayLengthMinus1 (9);                        // max. 2^10
2660  }
2661
2662/*
2663   Note: only the case of "vps_max_temporal_layers_minus1 = 0" is supported.
2664*/
2665  Int i, j;
2666  UInt birateValue, cpbSizeValue;
2667  UInt ducpbSizeValue;
2668  UInt duBitRateValue = 0;
2669
2670  for( i = 0; i < MAX_TLAYER; i ++ )
2671  {
2672    hrd->setFixedPicRateFlag( i, 1 );
2673    hrd->setPicDurationInTcMinus1( i, 0 );
2674    hrd->setLowDelayHrdFlag( i, 0 );
2675    hrd->setCpbCntMinus1( i, 0 );
2676
2677    birateValue  = bitRate;
2678    cpbSizeValue = bitRate;                                     // 1 second
2679    ducpbSizeValue = bitRate/numDU;
2680    duBitRateValue = bitRate;
2681    for( j = 0; j < ( hrd->getCpbCntMinus1( i ) + 1 ); j ++ )
2682    {
2683      hrd->setBitRateValueMinus1( i, j, 0, ( birateValue  - 1 ) );
2684      hrd->setCpbSizeValueMinus1( i, j, 0, ( cpbSizeValue - 1 ) );
2685      hrd->setDuCpbSizeValueMinus1( i, j, 0, ( ducpbSizeValue - 1 ) );
2686      hrd->setCbrFlag( i, j, 0, ( j == 0 ) );
2687
2688      hrd->setBitRateValueMinus1( i, j, 1, ( birateValue  - 1) );
2689      hrd->setCpbSizeValueMinus1( i, j, 1, ( cpbSizeValue - 1 ) );
2690      hrd->setDuCpbSizeValueMinus1( i, j, 1, ( ducpbSizeValue - 1 ) );
2691      hrd->setDuBitRateValueMinus1( i, j, 1, ( duBitRateValue - 1 ) );
2692      hrd->setCbrFlag( i, j, 1, ( j == 0 ) );
2693    }
2694  }
2695}
2696const Int TComSPS::m_winUnitX[]={1,2,2,1};
2697const Int TComSPS::m_winUnitY[]={1,2,1,1};
2698
2699TComPPS::TComPPS()
2700: m_PPSId                       (0)
2701, m_SPSId                       (0)
2702, m_picInitQPMinus26            (0)
2703, m_useDQP                      (false)
2704, m_bConstrainedIntraPred       (false)
2705, m_bSliceChromaQpFlag          (false)
2706, m_pcSPS                       (NULL)
2707, m_uiMaxCuDQPDepth             (0)
2708, m_uiMinCuDQPSize              (0)
2709, m_chromaCbQpOffset            (0)
2710, m_chromaCrQpOffset            (0)
2711, m_numRefIdxL0DefaultActive    (1)
2712, m_numRefIdxL1DefaultActive    (1)
2713, m_TransquantBypassEnableFlag  (false)
2714, m_useTransformSkip             (false)
2715, m_dependentSliceSegmentsEnabledFlag    (false)
2716, m_tilesEnabledFlag               (false)
2717, m_entropyCodingSyncEnabledFlag   (false)
2718, m_loopFilterAcrossTilesEnabledFlag  (true)
2719, m_uniformSpacingFlag           (0)
2720, m_iNumColumnsMinus1            (0)
2721, m_puiColumnWidth               (NULL)
2722, m_iNumRowsMinus1               (0)
2723, m_puiRowHeight                 (NULL)
2724, m_iNumSubstreams             (1)
2725, m_signHideFlag(0)
2726, m_cabacInitPresentFlag        (false)
2727, m_encCABACTableIdx            (I_SLICE)
2728, m_sliceHeaderExtensionPresentFlag    (false)
2729, m_loopFilterAcrossSlicesEnabledFlag (false)
2730, m_listsModificationPresentFlag(  0)
2731, m_numExtraSliceHeaderBits(0)
2732#if H_MV
2733, m_ppsInferScalingListFlag(false)
2734, m_ppsScalingListRefLayerId(0)
2735, m_pocResetInfoPresentFlag(false)
2736#if H_3D
2737, m_pcDLT(NULL)
2738#endif
2739#endif
2740{
2741  m_scalingList = new TComScalingList;
2742
2743#if H_MV
2744  m_ppsRangeExtensionsFlag     = false;
2745  m_ppsMultilayerExtensionFlag = false;
2746#if !H_3D
2747  m_ppsExtension6bits          = 0;
2748#else
2749  m_pps3dExtensionFlag         = false;
2750  m_ppsExtension5bits          = 0;
2751#endif
2752#endif
2753}
2754
2755TComPPS::~TComPPS()
2756{
2757  if( m_iNumColumnsMinus1 > 0 && m_uniformSpacingFlag == 0 )
2758  {
2759    if (m_puiColumnWidth) delete [] m_puiColumnWidth; 
2760    m_puiColumnWidth = NULL;
2761  }
2762  if( m_iNumRowsMinus1 > 0 && m_uniformSpacingFlag == 0 )
2763  {
2764    if (m_puiRowHeight) delete [] m_puiRowHeight;
2765    m_puiRowHeight = NULL;
2766  }
2767  delete m_scalingList;
2768}
2769
2770#if H_3D
2771TComDLT::TComDLT()
2772: m_bDltPresentFlag(false)
2773, m_iNumDepthViews(0)
2774, m_uiDepthViewBitDepth(8)
2775{
2776  m_uiDepthViewBitDepth = g_bitDepthY; 
2777
2778  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
2779  {
2780    m_bUseDLTFlag                 [i] = false;
2781    m_bInterViewDltPredEnableFlag [i] = false;
2782
2783    // allocate some memory and initialize with default mapping
2784    m_iNumDepthmapValues[i] = ((1 << m_uiDepthViewBitDepth)-1)+1;
2785    m_iBitsPerDepthValue[i] = numBitsForValue(m_iNumDepthmapValues[i]);
2786
2787    m_iDepthValue2Idx[i]    = (Int*) xMalloc(Int, m_iNumDepthmapValues[i]);
2788    m_iIdx2DepthValue[i]    = (Int*) xMalloc(Int, m_iNumDepthmapValues[i]);
2789
2790    //default mapping
2791    for (Int d=0; d<m_iNumDepthmapValues[i]; d++)
2792    {
2793      m_iDepthValue2Idx[i][d] = d;
2794      m_iIdx2DepthValue[i][d] = d;
2795    }
2796  }
2797}
2798
2799TComDLT::~TComDLT()
2800{
2801  for( Int i = 0; i < MAX_NUM_LAYERS; i++ )
2802  {
2803    if ( m_iDepthValue2Idx[i] != NULL ) 
2804    {
2805      xFree( m_iDepthValue2Idx[i] );
2806      m_iDepthValue2Idx[i] = NULL; 
2807    }
2808
2809    if ( m_iIdx2DepthValue[i] != NULL ) 
2810    {
2811      xFree( m_iIdx2DepthValue[i] );
2812      m_iIdx2DepthValue[i] = NULL; 
2813    }
2814  }
2815}
2816
2817Void TComDLT::setDepthLUTs(Int layerIdInVps, Int* idxToDepthValueTable, Int iNumDepthValues)
2818{
2819  if( idxToDepthValueTable == NULL || iNumDepthValues == 0 ) // default mapping only
2820    return;
2821
2822  // copy idx2DepthValue to internal array
2823  memcpy(m_iIdx2DepthValue[layerIdInVps], idxToDepthValueTable, iNumDepthValues*sizeof(UInt));
2824
2825  UInt uiMaxDepthValue = ((1 << g_bitDepthY)-1);
2826  for(Int p=0; p<=uiMaxDepthValue; p++)
2827  {
2828    Int iIdxDown    = 0;
2829    Int iIdxUp      = iNumDepthValues-1;
2830    Bool bFound     = false;
2831
2832    // iterate over indices to find lower closest depth
2833    Int i = 1;
2834    while(!bFound && i<iNumDepthValues)
2835    {
2836      if( m_iIdx2DepthValue[layerIdInVps][i] > p )
2837      {
2838        iIdxDown  = i-1;
2839        bFound    = true;
2840      }
2841
2842      i++;
2843    }
2844    // iterate over indices to find upper closest depth
2845    i = iNumDepthValues-2;
2846    bFound = false;
2847    while(!bFound && i>=0)
2848    {
2849      if( m_iIdx2DepthValue[layerIdInVps][i] < p )
2850      {
2851        iIdxUp  = i+1;
2852        bFound    = true;
2853      }
2854
2855      i--;
2856    }
2857
2858    // assert monotony
2859    assert(iIdxDown<=iIdxUp);
2860
2861    // assign closer depth value/idx
2862    if( abs(p-m_iIdx2DepthValue[layerIdInVps][iIdxDown]) < abs(p-m_iIdx2DepthValue[layerIdInVps][iIdxUp]) )
2863    {
2864      m_iDepthValue2Idx[layerIdInVps][p] = iIdxDown;
2865    }
2866    else
2867    {
2868      m_iDepthValue2Idx[layerIdInVps][p] = iIdxUp;
2869    }
2870
2871  }
2872
2873  // update DLT variables
2874  m_iNumDepthmapValues[layerIdInVps] = iNumDepthValues;
2875  m_iBitsPerDepthValue[layerIdInVps] = numBitsForValue(m_iNumDepthmapValues[layerIdInVps]);
2876}
2877
2878#if H_3D_DELTA_DLT
2879Void TComDLT::getDeltaDLT( Int layerIdInVps, Int* piDLTInRef, UInt uiDLTInRefNum, Int* piDeltaDLTOut, UInt *puiDeltaDLTOutNum )
2880{
2881  Bool abBM0[ 256 ];
2882  Bool abBM1[ 256 ];
2883 
2884  memset( abBM0, 0, sizeof( abBM0 ));
2885  memset( abBM1, 0, sizeof( abBM1 ));
2886 
2887  // convert reference DLT to bit string
2888  for( Int i = 0; i < uiDLTInRefNum; i++ )
2889  {
2890    abBM0[ piDLTInRef[ i ] ] = true;
2891  }
2892  // convert internal DLT to bit string
2893  for( Int i = 0; i < m_iNumDepthmapValues[ layerIdInVps ]; i++ )
2894  {
2895    abBM1[ m_iIdx2DepthValue[ layerIdInVps ][ i ] ] = true;
2896  }
2897 
2898  *puiDeltaDLTOutNum = 0;
2899  for( Int i = 0; i < 256; i++ )
2900  {
2901    if( abBM0[ i ] ^ abBM1[ i ] )
2902    {
2903      piDeltaDLTOut[ *puiDeltaDLTOutNum ] = i;
2904      *puiDeltaDLTOutNum = *puiDeltaDLTOutNum + 1;
2905    }
2906  }
2907}
2908
2909Void TComDLT::setDeltaDLT( Int layerIdInVps, Int* piDLTInRef, UInt uiDLTInRefNum, Int* piDeltaDLTIn, UInt uiDeltaDLTInNum )
2910{
2911  Bool abBM0[ 256 ];
2912  Bool abBM1[ 256 ];
2913 
2914  memset( abBM0, 0, sizeof( abBM0 ));
2915  memset( abBM1, 0, sizeof( abBM1 ));
2916 
2917  // convert reference DLT to bit string
2918  for( Int i = 0; i < uiDLTInRefNum; i++ )
2919  {
2920    abBM0[ piDLTInRef[ i ] ] = true;
2921  }
2922  // convert delta DLT to bit string
2923  for( Int i = 0; i < uiDeltaDLTInNum; i++ )
2924  {
2925    abBM1[ piDeltaDLTIn[ i ] ] = true;
2926  }
2927 
2928  Int aiIdx2DepthValue[256];
2929  UInt uiNumDepthValues = 0;
2930  memset( aiIdx2DepthValue, 0, sizeof( aiIdx2DepthValue ));
2931 
2932  for( Int i = 0; i < 256; i++ )
2933  {
2934    if( abBM0[ i ] ^ abBM1[ i ] )
2935    {
2936      aiIdx2DepthValue[ uiNumDepthValues++ ] = i;
2937    }
2938  }
2939 
2940  // update internal tables
2941  setDepthLUTs(layerIdInVps, aiIdx2DepthValue, uiNumDepthValues);
2942}
2943#endif
2944
2945#endif
2946
2947#if H_MV
2948Void TComSPS::inferRepFormat( TComVPS* vps, Int layerIdCurr )
2949{
2950  if ( layerIdCurr > 0 )
2951  { 
2952    Int            repFormatIdx = getUpdateRepFormatFlag() ?  getSpsRepFormatIdx() : vps->getVpsRepFormatIdx( vps->getLayerIdInVps( layerIdCurr ) ) ;
2953    TComRepFormat* repFormat    = vps->getRepFormat( repFormatIdx ); 
2954      setChromaFormatIdc( repFormat->getChromaFormatVpsIdc() );         
2955      //// ToDo: add when supported:
2956      // setSeperateColourPlaneFlag( repFormat->getSeparateColourPlaneVpsFlag() ) ;
2957
2958      setPicWidthInLumaSamples ( repFormat->getPicWidthVpsInLumaSamples()  ); 
2959      setPicHeightInLumaSamples( repFormat->getPicHeightVpsInLumaSamples() ); 
2960
2961      setBitDepthY             ( repFormat->getBitDepthVpsLumaMinus8()   + 8 ); 
2962      setQpBDOffsetY           ( (Int) (6*( getBitDepthY() - 8 )) );
2963
2964      setBitDepthC             ( repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
2965      setQpBDOffsetC           ( (Int) (6* ( getBitDepthC() -8 ) ) );
2966    if ( getLayerId() > 0 && getUpdateRepFormatFlag() )
2967    {
2968      assert( getChromaFormatIdc()      <=  repFormat->getChromaFormatVpsIdc()         ); 
2969      //// ToDo: add when supported:
2970      // assert( getSeperateColourPlaneFlag() <=  repFormat->getSeparateColourPlaneVpsFlag() ) ;
2971
2972      assert( getPicWidthInLumaSamples()  <= repFormat->getPicWidthVpsInLumaSamples()    ); 
2973      assert( getPicHeightInLumaSamples() <= repFormat->getPicHeightVpsInLumaSamples()   ); 
2974
2975      assert( getBitDepthY()              <= repFormat->getBitDepthVpsLumaMinus8()   + 8 );         
2976      assert( getBitDepthC()              <= repFormat->getBitDepthVpsChromaMinus8() + 8 ); 
2977    }
2978  }
2979
2980  // Set conformance window
2981  Int scal = TComSPS::getWinUnitX( getChromaFormatIdc() ) ;
2982  getConformanceWindow().scaleOffsets( scal );
2983  getVuiParameters()->getDefaultDisplayWindow().scaleOffsets( scal );
2984}
2985
2986Void TComSPS::inferScalingList( TComSPS* spsSrc )
2987{
2988  if ( getSpsInferScalingListFlag() ) 
2989  {
2990    assert( spsSrc != NULL ); 
2991    assert( !spsSrc->getSpsInferScalingListFlag() );             
2992    getScalingList()->inferFrom( spsSrc->getScalingList() ); 
2993  }
2994}
2995
2996Void TComSPS::inferSpsMaxDecPicBufferingMinus1( TComVPS* vps, Int targetOptLayerSetIdx, Int currLayerId, Bool encoder )
2997{
2998  const std::vector<Int>& targetDecLayerIdList = vps->getTargetDecLayerIdList( vps->olsIdxToLsIdx( targetOptLayerSetIdx )); 
2999
3000  if (getLayerId() > 0 )
3001  {
3002    Int layerIdx = 0;         
3003    while (layerIdx < (Int) targetDecLayerIdList.size() )
3004    {
3005      if ( targetDecLayerIdList[layerIdx] == currLayerId )
3006      { 
3007        break; 
3008      }
3009      layerIdx++; 
3010    }
3011
3012    assert( layerIdx < (Int) targetDecLayerIdList.size() ); 
3013
3014    for (Int i = 0; i <= getSpsMaxSubLayersMinus1(); i++ ) 
3015    {
3016      Int maxDecPicBufferingMinus1 = vps->getDpbSize()->getMaxVpsDecPicBufferingMinus1( targetOptLayerSetIdx, layerIdx, i ) ; 
3017
3018      if ( encoder )
3019      {
3020        assert( getMaxDecPicBuffering( i ) - 1 == maxDecPicBufferingMinus1 ); 
3021      }
3022      else
3023      {
3024        setMaxDecPicBuffering(i, maxDecPicBufferingMinus1 + 1 ); 
3025      }
3026    }
3027  }
3028}
3029
3030Void TComSPS::checkRpsMaxNumPics( TComVPS* vps, Int currLayerId )
3031{
3032  // In spec, when rps is in SPS, nuh_layer_id of SPS is used instead
3033  // of nuh_layer_id of slice (currLayerId), this seems to be a bug.
3034
3035  for (Int i = 0; i < getRPSList()->getNumberOfReferencePictureSets(); i++ )
3036  {
3037    TComReferencePictureSet* rps = getRPSList()->getReferencePictureSet( i ); 
3038    if ( !rps->getInterRPSPrediction() )
3039    {
3040      rps->checkMaxNumPics( vps->getVpsExtensionFlag(), vps->getMaxNumPics( currLayerId ), 
3041        getLayerId(), getMaxDecPicBuffering( getSpsMaxSubLayersMinus1() ) - 1 ); 
3042    }
3043  }
3044}
3045
3046#endif
3047
3048TComReferencePictureSet::TComReferencePictureSet()
3049: m_numberOfPictures (0)
3050, m_numberOfNegativePictures (0)
3051, m_numberOfPositivePictures (0)
3052, m_numberOfLongtermPictures (0)
3053, m_interRPSPrediction (0) 
3054, m_deltaRIdxMinus1 (0)   
3055, m_deltaRPS (0) 
3056, m_numRefIdc (0) 
3057{
3058  ::memset( m_deltaPOC, 0, sizeof(m_deltaPOC) );
3059  ::memset( m_POC, 0, sizeof(m_POC) );
3060  ::memset( m_used, 0, sizeof(m_used) );
3061  ::memset( m_refIdc, 0, sizeof(m_refIdc) );
3062}
3063
3064TComReferencePictureSet::~TComReferencePictureSet()
3065{
3066}
3067
3068Void TComReferencePictureSet::setUsed(Int bufferNum, Bool used)
3069{
3070  m_used[bufferNum] = used;
3071}
3072
3073Void TComReferencePictureSet::setDeltaPOC(Int bufferNum, Int deltaPOC)
3074{
3075  m_deltaPOC[bufferNum] = deltaPOC;
3076}
3077
3078Void TComReferencePictureSet::setNumberOfPictures(Int numberOfPictures)
3079{
3080  m_numberOfPictures = numberOfPictures;
3081}
3082
3083Int TComReferencePictureSet::getUsed(Int bufferNum)
3084{
3085  return m_used[bufferNum];
3086}
3087
3088Int TComReferencePictureSet::getDeltaPOC(Int bufferNum)
3089{
3090  return m_deltaPOC[bufferNum];
3091}
3092
3093Int TComReferencePictureSet::getNumberOfPictures()
3094{
3095  return m_numberOfPictures;
3096}
3097
3098Int TComReferencePictureSet::getPOC(Int bufferNum)
3099{
3100  return m_POC[bufferNum];
3101}
3102
3103Void TComReferencePictureSet::setPOC(Int bufferNum, Int POC)
3104{
3105  m_POC[bufferNum] = POC;
3106}
3107
3108Bool TComReferencePictureSet::getCheckLTMSBPresent(Int bufferNum)
3109{
3110  return m_bCheckLTMSB[bufferNum];
3111}
3112
3113Void TComReferencePictureSet::setCheckLTMSBPresent(Int bufferNum, Bool b)
3114{
3115  m_bCheckLTMSB[bufferNum] = b;
3116}
3117
3118/** set the reference idc value at uiBufferNum entry to the value of iRefIdc
3119 * \param uiBufferNum
3120 * \param iRefIdc
3121 * \returns Void
3122 */
3123Void TComReferencePictureSet::setRefIdc(Int bufferNum, Int refIdc)
3124{
3125  m_refIdc[bufferNum] = refIdc;
3126}
3127
3128/** get the reference idc value at uiBufferNum
3129 * \param uiBufferNum
3130 * \returns Int
3131 */
3132Int  TComReferencePictureSet::getRefIdc(Int bufferNum)
3133{
3134  return m_refIdc[bufferNum];
3135}
3136
3137/** Sorts the deltaPOC and Used by current values in the RPS based on the deltaPOC values.
3138 *  deltaPOC values are sorted with -ve values before the +ve values.  -ve values are in decreasing order.
3139 *  +ve values are in increasing order.
3140 * \returns Void
3141 */
3142Void TComReferencePictureSet::sortDeltaPOC()
3143{
3144  // sort in increasing order (smallest first)
3145  for(Int j=1; j < getNumberOfPictures(); j++)
3146  { 
3147    Int deltaPOC = getDeltaPOC(j);
3148    Bool used = getUsed(j);
3149    for (Int k=j-1; k >= 0; k--)
3150    {
3151      Int temp = getDeltaPOC(k);
3152      if (deltaPOC < temp)
3153      {
3154        setDeltaPOC(k+1, temp);
3155        setUsed(k+1, getUsed(k));
3156        setDeltaPOC(k, deltaPOC);
3157        setUsed(k, used);
3158      }
3159    }
3160  }
3161  // flip the negative values to largest first
3162  Int numNegPics = getNumberOfNegativePictures();
3163  for(Int j=0, k=numNegPics-1; j < numNegPics>>1; j++, k--)
3164  { 
3165    Int deltaPOC = getDeltaPOC(j);
3166    Bool used = getUsed(j);
3167    setDeltaPOC(j, getDeltaPOC(k));
3168    setUsed(j, getUsed(k));
3169    setDeltaPOC(k, deltaPOC);
3170    setUsed(k, used);
3171  }
3172}
3173
3174/** Prints the deltaPOC and RefIdc (if available) values in the RPS.
3175 *  A "*" is added to the deltaPOC value if it is Used bu current.
3176 * \returns Void
3177 */
3178Void TComReferencePictureSet::printDeltaPOC()
3179{
3180  printf("DeltaPOC = { ");
3181  for(Int j=0; j < getNumberOfPictures(); j++)
3182  {
3183    printf("%d%s ", getDeltaPOC(j), (getUsed(j)==1)?"*":"");
3184  } 
3185  if (getInterRPSPrediction()) 
3186  {
3187    printf("}, RefIdc = { ");
3188    for(Int j=0; j < getNumRefIdc(); j++)
3189    {
3190      printf("%d ", getRefIdc(j));
3191    } 
3192  }
3193  printf("}\n");
3194}
3195#if H_MV
3196Void TComReferencePictureSet::checkMaxNumPics( Bool vpsExtensionFlag, Int maxNumPics, Int nuhLayerId, Int spsMaxDecPicBufferingMinus1 )
3197{
3198  assert( getNumberOfPictures() >= 0 ); 
3199  if ( nuhLayerId == 0 )
3200  {
3201    assert( getNumberOfPictures() <= spsMaxDecPicBufferingMinus1 ); 
3202  }
3203
3204  if ( vpsExtensionFlag )
3205  {
3206    assert( getNumberOfPictures() <= maxNumPics );
3207  }
3208}
3209#endif
3210
3211TComRPSList::TComRPSList()
3212:m_referencePictureSets (NULL)
3213{
3214}
3215
3216TComRPSList::~TComRPSList()
3217{
3218}
3219
3220Void TComRPSList::create( Int numberOfReferencePictureSets)
3221{
3222  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
3223  m_referencePictureSets = new TComReferencePictureSet[numberOfReferencePictureSets];
3224}
3225
3226Void TComRPSList::destroy()
3227{
3228  if (m_referencePictureSets)
3229  {
3230    delete [] m_referencePictureSets;
3231  }
3232  m_numberOfReferencePictureSets = 0;
3233  m_referencePictureSets = NULL;
3234}
3235
3236
3237
3238TComReferencePictureSet* TComRPSList::getReferencePictureSet(Int referencePictureSetNum)
3239{
3240  return &m_referencePictureSets[referencePictureSetNum];
3241}
3242
3243Int TComRPSList::getNumberOfReferencePictureSets()
3244{
3245  return m_numberOfReferencePictureSets;
3246}
3247
3248Void TComRPSList::setNumberOfReferencePictureSets(Int numberOfReferencePictureSets)
3249{
3250  m_numberOfReferencePictureSets = numberOfReferencePictureSets;
3251}
3252
3253TComRefPicListModification::TComRefPicListModification()
3254: m_bRefPicListModificationFlagL0 (false)
3255, m_bRefPicListModificationFlagL1 (false)
3256{
3257  ::memset( m_RefPicSetIdxL0, 0, sizeof(m_RefPicSetIdxL0) );
3258  ::memset( m_RefPicSetIdxL1, 0, sizeof(m_RefPicSetIdxL1) );
3259}
3260
3261TComRefPicListModification::~TComRefPicListModification()
3262{
3263}
3264
3265TComScalingList::TComScalingList()
3266{
3267  init();
3268}
3269TComScalingList::~TComScalingList()
3270{
3271  destroy();
3272}
3273
3274/** set default quantization matrix to array
3275*/
3276Void TComSlice::setDefaultScalingList()
3277{
3278  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3279  {
3280    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
3281    {
3282      getScalingList()->processDefaultMatrix(sizeId, listId);
3283    }
3284  }
3285}
3286/** check if use default quantization matrix
3287 * \returns true if use default quantization matrix in all size
3288*/
3289Bool TComSlice::checkDefaultScalingList()
3290{
3291  UInt defaultCounter=0;
3292
3293  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3294  {
3295    for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
3296    {
3297      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
3298     && ((sizeId < SCALING_LIST_16x16) || (getScalingList()->getScalingListDC(sizeId,listId) == 16))) // check DC value
3299      {
3300        defaultCounter++;
3301      }
3302    }
3303  }
3304  return (defaultCounter == (SCALING_LIST_NUM * SCALING_LIST_SIZE_NUM - 4)) ? false : true; // -4 for 32x32
3305}
3306
3307#if H_MV
3308Void TComSlice::createInterLayerReferencePictureSet( TComPicLists* ivPicLists, std::vector<TComPic*>& refPicSetInterLayer0, std::vector<TComPic*>& refPicSetInterLayer1 )
3309{
3310  refPicSetInterLayer0.clear(); 
3311  refPicSetInterLayer1.clear(); 
3312
3313  for( Int i = 0; i < getNumActiveRefLayerPics(); i++ ) 
3314  {
3315    Int layerIdRef = getRefPicLayerId( i ); 
3316    TComPic* picRef = ivPicLists->getPic( layerIdRef, getPOC() ) ; 
3317    assert ( picRef != 0 ); // There shall be no entry equal to "no reference picture" in RefPicSetInterLayer0 or RefPicSetInterLayer1.
3318
3319    picRef->getPicYuvRec()->extendPicBorder(); 
3320    picRef->setIsLongTerm( true );       
3321    picRef->getSlice(0)->setReferenced( true );       
3322
3323    Int viewIdCur  = getVPS()->getViewId( getLayerId() ); 
3324    Int viewIdZero = getVPS()->getViewId( 0 );
3325    Int viewIdRef  = getVPS()->getViewId( layerIdRef ); 
3326
3327    if (  ( viewIdCur <= viewIdZero && viewIdCur <= viewIdRef ) || ( viewIdCur >= viewIdZero && viewIdCur >= viewIdRef ) )
3328    {
3329      refPicSetInterLayer0.push_back( picRef ); 
3330    }
3331    else
3332    {
3333      refPicSetInterLayer1.push_back( picRef ); 
3334    }
3335    // Consider to check here:
3336    // "If the current picture is a RADL picture, there shall be no entry in the RefPicSetInterLayer0 and RefPicSetInterLayer1 that is a RASL picture. "   
3337    assert( picRef->getSlice(0)->getDiscardableFlag() == false ); // "There shall be no picture that has discardable_flag equal to 1 in RefPicSetInterLayer0 or RefPicSetInterLayer1".       
3338  }
3339}
3340
3341Void TComSlice::markIvRefPicsAsShortTerm( std::vector<TComPic*> refPicSetInterLayer0, std::vector<TComPic*> refPicSetInterLayer1 )
3342{
3343  // Mark as short-term
3344  for ( Int i = 0; i < refPicSetInterLayer0.size(); i++ ) 
3345  {
3346    refPicSetInterLayer0[i]->setIsLongTerm( false ); 
3347  }
3348
3349  for ( Int i = 0; i < refPicSetInterLayer1.size(); i++ ) 
3350  {
3351    refPicSetInterLayer1[i]->setIsLongTerm( false ); 
3352  }
3353
3354}
3355
3356Void TComSlice::printRefPicList()
3357{ 
3358  for ( Int li = 0; li < 2; li++)
3359  {   
3360    std::cout << std::endl << "RefPicListL" <<  li << ":" << std::endl; 
3361    for (Int rIdx = 0; rIdx <= (m_aiNumRefIdx[li]-1); rIdx ++)
3362    {     
3363      if (rIdx == 0 && li == 0) m_apcRefPicList[li][rIdx]->print( true );
3364       
3365      m_apcRefPicList[li][rIdx]->print( false );
3366    }
3367  }
3368}
3369
3370Void TComSlice::markCurrPic( TComPic* currPic )
3371{
3372  currPic->getSlice(0)->setReferenced( true ) ; 
3373  currPic->setIsLongTerm( false ); 
3374
3375  currPic->setReconMark( true );
3376  currPic->setPicOutputFlag( currPic->getSlice(0)->getPicOutputFlag() );
3377}
3378
3379Void TComSlice::setRefPicSetInterLayer( std::vector<TComPic*>* refPicSetInterLayer0, std::vector<TComPic*>* refPicSetInterLayer1 )
3380{
3381  m_refPicSetInterLayer0 = refPicSetInterLayer0; 
3382  m_refPicSetInterLayer1 = refPicSetInterLayer1; 
3383}
3384
3385TComPic* TComSlice::getPicFromRefPicSetInterLayer(Int setIdc, Int layerId )
3386{
3387  assert ( setIdc == 0 || setIdc == 1);   
3388  std::vector<TComPic*>* refPicSetInterLayer = ( setIdc == 0 ? m_refPicSetInterLayer0 : m_refPicSetInterLayer1);   
3389  assert( refPicSetInterLayer != 0 ); 
3390 
3391  TComPic* pcPic = NULL; 
3392  for ( Int i = 0; i < (*refPicSetInterLayer).size(); i++ )
3393  {
3394    if ((*refPicSetInterLayer)[ i ]->getLayerId() == layerId )
3395    {
3396      pcPic = (*refPicSetInterLayer)[ i ]; 
3397    }
3398  }
3399
3400  assert(pcPic != NULL); 
3401  return pcPic;
3402}
3403
3404
3405Int  TComSlice::getRefLayerPicFlag( Int i ) 
3406{
3407  TComVPS* vps = getVPS(); 
3408  Int refLayerIdx = vps->getLayerIdInVps( vps->getRefLayerId( getLayerId(), i ) ); 
3409
3410  Bool refLayerPicFlag = ( vps->getSubLayersVpsMaxMinus1( refLayerIdx ) >=  getTLayer() )  && ( getTLayer() == 0  ) &&
3411    ( vps->getMaxTidIlRefPicsPlus1( refLayerIdx, vps->getLayerIdInVps( getLayerId() )) > getTLayer() ); 
3412
3413  return refLayerPicFlag;       
3414}   
3415
3416Int TComSlice::getRefLayerPicIdc( Int j ) 
3417{ 
3418  Int refLayerPicIdc = -1; 
3419  Int curj = 0; 
3420  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3421  {
3422    if( getRefLayerPicFlag( i ) )
3423    {
3424      if ( curj == j ) 
3425      {
3426        refLayerPicIdc = i;         
3427        break;
3428      }
3429      curj++; 
3430    }
3431  }
3432
3433  assert( curj == j ); 
3434  assert( refLayerPicIdc != -1 ); 
3435  return refLayerPicIdc; 
3436}
3437
3438Int  TComSlice::getNumRefLayerPics( )
3439{ 
3440  Int numRefLayerPics = 0; 
3441  for( Int i = 0;  i < getVPS()->getNumDirectRefLayers( getLayerId()) ; i++ )
3442  {
3443    numRefLayerPics += getRefLayerPicFlag( i ); 
3444  }
3445  return numRefLayerPics; 
3446}
3447
3448
3449
3450Int TComSlice::getNumActiveRefLayerPics()
3451{
3452  Int numActiveRefLayerPics; 
3453
3454  if( getLayerId() == 0 || getNumRefLayerPics() ==  0 )
3455  {
3456    numActiveRefLayerPics = 0; 
3457  }
3458  else if (getVPS()->getAllRefLayersActiveFlag() )
3459  {
3460    numActiveRefLayerPics = getNumRefLayerPics(); 
3461  }
3462  else if ( !getInterLayerPredEnabledFlag() )
3463  {
3464    numActiveRefLayerPics = 0; 
3465  }
3466  else if( getVPS()->getMaxOneActiveRefLayerFlag() || getVPS()->getNumDirectRefLayers( getLayerId() ) == 1 )
3467  {
3468    numActiveRefLayerPics = 1; 
3469  }
3470  else
3471  {
3472    numActiveRefLayerPics = getNumInterLayerRefPicsMinus1() + 1; 
3473  }
3474  return numActiveRefLayerPics;
3475}
3476
3477Int TComSlice::getRefPicLayerId( Int i )
3478{
3479  return getVPS()->getRefLayerId( getLayerId(), getInterLayerPredLayerIdc( i ) );
3480}
3481
3482#if H_3D_ARP
3483Void TComSlice::setARPStepNum( TComPicLists*ivPicLists )
3484{
3485  Bool tempRefPicInListsFlag = false;
3486  if(!getVPS()->getUseAdvRP(getLayerId()) || this->isIRAP())
3487  {
3488    m_nARPStepNum = 0;
3489  }
3490  else
3491  {
3492    setFirstTRefIdx (REF_PIC_LIST_0, -1);
3493    setFirstTRefIdx (REF_PIC_LIST_1, -1);
3494    for ( Int refListIdx = 0; refListIdx < ((m_eSliceType==B_SLICE) ? 2 : 1); refListIdx++ )
3495    {
3496      Int diffPOC=MAX_INT;
3497      Int idx=-1;
3498      for(Int i = 0; i < getNumRefIdx(RefPicList(refListIdx)); i++ )
3499      {
3500        if ( getRefPic(RefPicList(refListIdx), i)->getPOC() != getPOC() )
3501        {
3502          if( abs(getRefPic(RefPicList(refListIdx), i)->getPOC() - getPOC()) < diffPOC)
3503          {
3504            diffPOC=abs(getRefPic(RefPicList(refListIdx), i)->getPOC() - getPOC());
3505            idx=i;
3506          }
3507        }
3508        if(idx>=0)
3509        {
3510          setFirstTRefIdx (RefPicList(refListIdx), idx);
3511        }
3512      }
3513    }
3514    tempRefPicInListsFlag = getFirstTRefIdx(REF_PIC_LIST_0) >= 0 || getFirstTRefIdx(REF_PIC_LIST_1) >= 0;
3515    m_nARPStepNum = tempRefPicInListsFlag ? getVPS()->getARPStepNum(getLayerId()) : 0;
3516  }
3517
3518  if (tempRefPicInListsFlag)
3519  {
3520    for ( Int refListIdx = 0; refListIdx < ((m_eSliceType==B_SLICE) ? 2 : 1); refListIdx++ )
3521    {
3522      RefPicList eRefPicList = RefPicList( refListIdx );
3523      Int prevPOC = getRefPic(eRefPicList, getFirstTRefIdx(eRefPicList) )->getPOC();
3524      for( Int i = 0; i < getNumActiveRefLayerPics(); i++ )
3525      {
3526        Int layerIdInNuh = getRefPicLayerId( i );
3527        Int viewIdx = getVPS()->getViewId( layerIdInNuh );
3528        TComPic*pcPicPrev = ivPicLists->getPic(viewIdx, 0, prevPOC);
3529        if (getFirstTRefIdx(eRefPicList) >= 0 && pcPicPrev && pcPicPrev->getSlice( 0 )->isReferenced())
3530        {
3531          m_arpRefPicAvailable[eRefPicList][layerIdInNuh] = true;
3532        }
3533        else
3534        {
3535          m_arpRefPicAvailable[eRefPicList][layerIdInNuh] = false;
3536        }
3537      }
3538    }
3539  }
3540}
3541#endif
3542#if H_3D_IC
3543Void TComSlice::xSetApplyIC(Bool bUseLowLatencyICEnc)
3544{
3545  if(bUseLowLatencyICEnc)
3546  {
3547    Bool existInterViewRef=false;
3548    TComPic* pcCurrPic = getPic();
3549    TComPic* pcRefPic = NULL;
3550    for ( Int i = 0; (i < getNumRefIdx( REF_PIC_LIST_0 )) && !existInterViewRef; i++ )
3551    {
3552      pcRefPic = getRefPic( REF_PIC_LIST_0, i );
3553      if ( pcRefPic != NULL )
3554      {
3555        if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3556        {
3557          existInterViewRef = true;       
3558        }
3559      }
3560    }
3561
3562    for ( Int i = 0; (i < getNumRefIdx( REF_PIC_LIST_1 )) && !existInterViewRef; i++ )
3563    {
3564      pcRefPic = getRefPic( REF_PIC_LIST_1, i );
3565      if ( pcRefPic != NULL )
3566      {
3567        if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3568        {
3569          existInterViewRef = true;       
3570        }
3571      }
3572    }
3573
3574    if(!existInterViewRef)
3575    {
3576      m_bApplyIC = false;
3577    }
3578    else
3579    {
3580      Int curLayer=getDepth();
3581      if( curLayer>9) curLayer=9; // Max layer is 10
3582
3583      m_bApplyIC = true;
3584      Int refLayer = curLayer-1;
3585      if( (refLayer>=0) && (g_aICEnableCANDIDATE[refLayer]>0) )
3586      {   
3587        Double ratio=Double(g_aICEnableNUM[refLayer])/Double(g_aICEnableCANDIDATE[refLayer]);
3588
3589        if( ratio > IC_LOW_LATENCY_ENCODING_THRESHOLD)
3590{
3591          m_bApplyIC=true;
3592        }
3593        else
3594        {
3595          m_bApplyIC=false;
3596        }
3597      }
3598      g_aICEnableNUM[curLayer]=0;
3599      g_aICEnableCANDIDATE[curLayer]=0;
3600      g_lastlayer=getDepth();
3601    }
3602  }
3603  else
3604  {
3605  Int iMaxPelValue = ( 1 << g_bitDepthY ); 
3606  Int *aiRefOrgHist;
3607  Int *aiCurrHist;
3608  aiRefOrgHist = (Int *) xMalloc( Int,iMaxPelValue );
3609  aiCurrHist   = (Int *) xMalloc( Int,iMaxPelValue );
3610  memset( aiRefOrgHist, 0, iMaxPelValue*sizeof(Int) );
3611  memset( aiCurrHist, 0, iMaxPelValue*sizeof(Int) );
3612  // Reference Idx Number
3613  Int iNumRefIdx = getNumRefIdx( REF_PIC_LIST_0 );
3614  TComPic* pcCurrPic = NULL;
3615  TComPic* pcRefPic = NULL;
3616  TComPicYuv* pcCurrPicYuv = NULL;
3617  TComPicYuv* pcRefPicYuvOrg = NULL;
3618  pcCurrPic = getPic();
3619  pcCurrPicYuv = pcCurrPic->getPicYuvOrg();
3620  Int iWidth = pcCurrPicYuv->getWidth();
3621  Int iHeight = pcCurrPicYuv->getHeight();
3622
3623
3624  // Get InterView Reference picture
3625  // !!!!! Assume only one Interview Reference Picture in L0
3626  for ( Int i = 0; i < iNumRefIdx; i++ )
3627  {
3628    pcRefPic = getRefPic( REF_PIC_LIST_0, i );
3629    if ( pcRefPic != NULL )
3630    {
3631      if ( pcCurrPic->getViewIndex() != pcRefPic->getViewIndex() )
3632      {
3633        pcRefPicYuvOrg = pcRefPic->getPicYuvOrg();
3634      }
3635    }
3636  }
3637
3638  if ( pcRefPicYuvOrg != NULL )
3639  {
3640    Pel* pCurrY = pcCurrPicYuv ->getLumaAddr();
3641    Pel* pRefOrgY = pcRefPicYuvOrg  ->getLumaAddr();
3642    Int iCurrStride = pcCurrPicYuv->getStride();
3643    Int iRefStride = pcRefPicYuvOrg->getStride();
3644    Int iSumOrgSAD = 0;
3645    Double dThresholdOrgSAD = getIsDepth() ? 0.1 : 0.05;
3646
3647    // Histogram building - luminance
3648    for ( Int y = 0; y < iHeight; y++ )
3649    {
3650      for ( Int x = 0; x < iWidth; x++ )
3651      {
3652        aiCurrHist[pCurrY[x]]++;
3653        aiRefOrgHist[pRefOrgY[x]]++;
3654      }
3655      pCurrY += iCurrStride;
3656      pRefOrgY += iRefStride;
3657    }
3658    // Histogram SAD
3659    for ( Int i = 0; i < iMaxPelValue; i++ )
3660    {
3661      iSumOrgSAD += abs( aiCurrHist[i] - aiRefOrgHist[i] );
3662    }
3663    // Setting
3664    if ( iSumOrgSAD > Int( dThresholdOrgSAD * iWidth * iHeight ) )
3665    {
3666      m_bApplyIC = true;
3667    }
3668    else
3669    {
3670      m_bApplyIC = false;
3671    }
3672  }
3673
3674  xFree( aiCurrHist   );
3675  xFree( aiRefOrgHist );
3676  aiCurrHist = NULL;
3677  aiRefOrgHist = NULL;
3678  }//if(bUseLowLatencyICEnc)
3679}
3680#endif
3681#if H_3D
3682Void TComSlice::setIvPicLists( TComPicLists* m_ivPicLists )
3683{
3684  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
3685  {     
3686    for ( Int depthId = 0; depthId < 2; depthId++ )
3687    {
3688      m_ivPicsCurrPoc[ depthId ][ i ] = ( i <= m_viewIndex ) ? m_ivPicLists->getPic( i, ( depthId == 1) , getPOC() ) : NULL;
3689    }       
3690  } 
3691}
3692Void TComSlice::setDepthToDisparityLUTs()
3693{ 
3694  Bool setupLUT = false; 
3695  Int layerIdInVPS = getVPS()->getLayerIdInNuh( m_layerId ); 
3696
3697#if H_3D_VSP
3698  setupLUT = setupLUT || getVPS()->getViewSynthesisPredFlag( layerIdInVPS); 
3699#endif
3700
3701#if H_3D_NBDV_REF
3702  setupLUT = setupLUT || getVPS()->getDepthRefinementFlag( layerIdInVPS );
3703#endif
3704
3705#if H_3D_IV_MERGE
3706  setupLUT = setupLUT || ( getVPS()->getIvMvPredFlag(layerIdInVPS ) && getIsDepth() );
3707#endif
3708
3709#if H_3D_DDD
3710  if( getIsDepth() && getViewIndex() > 0 )
3711  {
3712      TComSlice *pcTextSlice = getTexturePic()->getSlice( 0 );
3713      memcpy( m_aiDDDInvScale, pcTextSlice->m_aiDDDInvScale, sizeof( Int ) * getViewIndex() );
3714      memcpy( m_aiDDDInvOffset, pcTextSlice->m_aiDDDInvOffset, sizeof( Int ) * getViewIndex() );
3715      memcpy( m_aiDDDShift, pcTextSlice->m_aiDDDShift, sizeof( Int ) * getViewIndex() );             
3716  } 
3717#endif
3718
3719  if( !setupLUT )
3720    return; 
3721
3722  /// GT: Allocation should be moved to a better place later;
3723  if ( m_depthToDisparityB == NULL )
3724  {
3725    m_depthToDisparityB = new Int*[ getViewIndex() ];
3726    for ( Int i = 0; i < getViewIndex(); i++ )
3727    {
3728      m_depthToDisparityB[ i ] = new Int[ Int(1 << g_bitDepthY) ]; 
3729    }
3730  }
3731
3732  if ( m_depthToDisparityF == NULL )
3733  {
3734    m_depthToDisparityF= new Int*[ getViewIndex() ];
3735    for ( Int i = 0; i < getViewIndex(); i++ )
3736    {
3737      m_depthToDisparityF[ i ] = new Int[ Int(1 << g_bitDepthY) ]; 
3738    }
3739  }
3740
3741  assert( m_depthToDisparityB != NULL ); 
3742  assert( m_depthToDisparityF != NULL ); 
3743
3744  TComVPS* vps = getVPS(); 
3745
3746  Int log2Div = g_bitDepthY - 1 + vps->getCamParPrecision();
3747  Int viewIndex = getViewIndex();
3748
3749  Bool camParaSH = vps->hasCamParInSliceHeader( viewIndex );
3750
3751  Int* codScale     = camParaSH ? m_aaiCodedScale [ 0 ] : vps->getCodedScale    ( viewIndex ); 
3752  Int* codOffset    = camParaSH ? m_aaiCodedOffset[ 0 ] : vps->getCodedOffset   ( viewIndex ); 
3753  Int* invCodScale  = camParaSH ? m_aaiCodedScale [ 1 ] : vps->getInvCodedScale ( viewIndex ); 
3754  Int* invCodOffset = camParaSH ? m_aaiCodedOffset[ 1 ] : vps->getInvCodedOffset( viewIndex ); 
3755
3756  for (Int i = 0; i <= ( getViewIndex() - 1); i++)
3757  {
3758    for ( Int d = 0; d <= ( ( 1 << g_bitDepthY ) - 1 ); d++ )
3759    {
3760      Int offset =    ( codOffset  [ i ] << g_bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
3761      m_depthToDisparityB[ i ][ d ] = ( codScale [ i ] * d + offset ) >> log2Div; 
3762
3763      Int invOffset = ( invCodOffset[ i ] << g_bitDepthY ) + ( ( 1 << log2Div ) >> 1 );         
3764      m_depthToDisparityF[ i ][ d ] = ( invCodScale[ i ] * d + invOffset ) >> log2Div; 
3765    }
3766
3767#if H_3D_DDD
3768    initializeDDDPara( vps->getCamParPrecision(), codScale[ i ], codOffset[ i ], i );
3769#endif
3770  }
3771}
3772#endif
3773#endif
3774
3775#if H_3D_DDD
3776Void TComSlice::initializeDDDPara( UInt uiCamParsCodedPrecision, Int  iCodedScale,Int  iCodedOffset, Int iBaseViewIdx )
3777{
3778    UInt uiViewId     = getViewIndex();
3779
3780    if( uiViewId == 0 )
3781    {
3782        m_aiDDDInvScale[ iBaseViewIdx ] = m_aiDDDInvOffset[ iBaseViewIdx ] = m_aiDDDShift[ iBaseViewIdx ] = 0;
3783        return;
3784    }
3785
3786
3787    Int iSign = iCodedScale >= 0 ? 1 : -1;
3788    iCodedScale = abs( iCodedScale );
3789
3790    Int iBitWidth = 0;
3791
3792    const Int iInvPres = 9;
3793
3794    while( ((( 1 << iBitWidth ) << 1 ) <= iCodedScale ) )
3795    {
3796        iBitWidth ++;
3797    }
3798    iBitWidth += iInvPres;
3799    Int iTargetValue =  1 << iBitWidth;
3800
3801    Int iMinError = MAX_INT;
3802    Int iBestD = 1 << ( iInvPres - 1 );
3803    for( Int d = 1 << ( iInvPres - 1 ); d < ( 1 << iInvPres ); d++ )
3804    {
3805        Int iError = abs( iCodedScale * d - iTargetValue );
3806        if( iError < iMinError )
3807        {
3808            iMinError = iError;
3809            iBestD = d;
3810        }
3811        if( iMinError == 0 )
3812        {
3813            break;
3814        }
3815    }
3816    Int iRoundingDir = 0;
3817    if( iCodedScale * iBestD > iTargetValue )
3818    {
3819        iRoundingDir = -1;
3820    }
3821    else if( iCodedScale * iBestD < iTargetValue )
3822    {
3823        iRoundingDir = 1;
3824    }
3825    Int iCamPres = uiCamParsCodedPrecision - 1;
3826    m_aiDDDInvScale [ iBaseViewIdx ] = ( iBestD << ( iCamPres + g_bitDepthY )) * iSign;
3827    m_aiDDDInvOffset[ iBaseViewIdx ] = -iSign * iBestD * ( iCodedOffset << g_bitDepthY );
3828    m_aiDDDShift    [ iBaseViewIdx ] = iBitWidth;
3829    m_aiDDDInvOffset[ iBaseViewIdx ] += 1 << ( m_aiDDDShift[ iBaseViewIdx ] - 1 );
3830    m_aiDDDInvOffset[ iBaseViewIdx ] += ( 1 << ( m_aiDDDShift[ iBaseViewIdx ] - 4 ) ) * iRoundingDir;
3831
3832    return;
3833}
3834
3835
3836#endif
3837
3838#if H_MV
3839Void TComSlice::checkCrossLayerBlaFlag()
3840{
3841  // cross_layer_bla_flag shall be equal to 0 for pictures with nal_unit_type not equal to IDR_W_RADL or IDR_N_LP or with nuh_layer_id not equal to 0.
3842  if ( getLayerId() != 0 || getNalUnitType() != NAL_UNIT_CODED_SLICE_IDR_W_RADL || getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP )
3843  {
3844    assert( m_crossLayerBlaFlag == 0 ); 
3845  }
3846}
3847
3848Bool TComSlice::inferPocMsbValPresentFlag()
3849{
3850  Bool pocMsbValPresentFlag; 
3851  if( getSliceSegmentHeaderExtensionLength() == 0 ) 
3852  {
3853    pocMsbValPresentFlag = false; 
3854  }
3855  else if ( getPocMsbValRequiredFlag() )
3856  {
3857    pocMsbValPresentFlag = true; 
3858  }
3859  else
3860  {
3861    pocMsbValPresentFlag = false; 
3862  }
3863
3864  return pocMsbValPresentFlag;
3865}
3866
3867
3868#endif
3869
3870#if H_3D_DBBP
3871Int TComSlice::getDepthFromDV( Int iDV, Int iBaseViewIdx )
3872{
3873  return ClipY(( iDV * m_aiDDDInvScale[ iBaseViewIdx ] + m_aiDDDInvOffset[ iBaseViewIdx ] ) >> m_aiDDDShift[ iBaseViewIdx ]);
3874}
3875#endif
3876
3877/** get scaling matrix from RefMatrixID
3878 * \param sizeId size index
3879 * \param Index of input matrix
3880 * \param Index of reference matrix
3881 */
3882Void TComScalingList::processRefMatrix( UInt sizeId, UInt listId , UInt refListId )
3883{
3884  ::memcpy(getScalingListAddress(sizeId, listId),((listId == refListId)? getScalingListDefaultAddress(sizeId, refListId): getScalingListAddress(sizeId, refListId)),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3885}
3886
3887/** parse syntax infomation
3888 *  \param pchFile syntax infomation
3889 *  \returns false if successful
3890 */
3891Bool TComScalingList::xParseScalingList(Char* pchFile)
3892{
3893  FILE *fp;
3894  Char line[1024];
3895  UInt sizeIdc,listIdc;
3896  UInt i,size = 0;
3897  Int *src=0,data;
3898  Char *ret;
3899  UInt  retval;
3900
3901  if((fp = fopen(pchFile,"r")) == (FILE*)NULL)
3902  {
3903    printf("can't open file %s :: set Default Matrix\n",pchFile);
3904    return true;
3905  }
3906
3907  for(sizeIdc = 0; sizeIdc < SCALING_LIST_SIZE_NUM; sizeIdc++)
3908  {
3909    size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeIdc]);
3910    for(listIdc = 0; listIdc < g_scalingListNum[sizeIdc]; listIdc++)
3911    {
3912      src = getScalingListAddress(sizeIdc, listIdc);
3913
3914      fseek(fp,0,0);
3915      do 
3916      {
3917        ret = fgets(line, 1024, fp);
3918        if ((ret==NULL)||(strstr(line, MatrixType[sizeIdc][listIdc])==NULL && feof(fp)))
3919        {
3920          printf("Error: can't read Matrix :: set Default Matrix\n");
3921          return true;
3922        }
3923      }
3924      while (strstr(line, MatrixType[sizeIdc][listIdc]) == NULL);
3925      for (i=0; i<size; i++)
3926      {
3927        retval = fscanf(fp, "%d,", &data);
3928        if (retval!=1)
3929        {
3930          printf("Error: can't read Matrix :: set Default Matrix\n");
3931          return true;
3932        }
3933        src[i] = data;
3934      }
3935      //set DC value for default matrix check
3936      setScalingListDC(sizeIdc,listIdc,src[0]);
3937
3938      if(sizeIdc > SCALING_LIST_8x8)
3939      {
3940        fseek(fp,0,0);
3941        do 
3942        {
3943          ret = fgets(line, 1024, fp);
3944          if ((ret==NULL)||(strstr(line, MatrixType_DC[sizeIdc][listIdc])==NULL && feof(fp)))
3945          {
3946            printf("Error: can't read DC :: set Default Matrix\n");
3947            return true;
3948          }
3949        }
3950        while (strstr(line, MatrixType_DC[sizeIdc][listIdc]) == NULL);
3951        retval = fscanf(fp, "%d,", &data);
3952        if (retval!=1)
3953        {
3954          printf("Error: can't read Matrix :: set Default Matrix\n");
3955          return true;
3956        }
3957        //overwrite DC value when size of matrix is larger than 16x16
3958        setScalingListDC(sizeIdc,listIdc,data);
3959      }
3960    }
3961  }
3962  fclose(fp);
3963  return false;
3964}
3965
3966#if H_MV
3967Void TComScalingList::inferFrom( TComScalingList* srcScLi )
3968{
3969  for(Int sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3970  {
3971    for(Int listId = 0; listId <  g_scalingListNum[sizeId]; listId++)
3972    {
3973      setRefMatrixId  (sizeId,listId, srcScLi->getRefMatrixId  (sizeId,listId));
3974      setScalingListDC(sizeId,listId, srcScLi->getScalingListDC(sizeId,listId));         
3975      ::memcpy(getScalingListAddress(sizeId, listId),srcScLi->getScalingListAddress(sizeId, listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
3976    }
3977  }
3978}
3979#endif
3980/** initialization process of quantization matrix array
3981 */
3982Void TComScalingList::init()
3983{
3984  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3985  {
3986    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
3987    {
3988      m_scalingListCoef[sizeId][listId] = new Int [min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId])];
3989    }
3990  }
3991  m_scalingListCoef[SCALING_LIST_32x32][3] = m_scalingListCoef[SCALING_LIST_32x32][1]; // copy address for 32x32
3992}
3993
3994/** destroy quantization matrix array
3995 */
3996Void TComScalingList::destroy()
3997{
3998  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3999  {
4000    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
4001    {
4002      if(m_scalingListCoef[sizeId][listId]) delete [] m_scalingListCoef[sizeId][listId];
4003    }
4004  }
4005}
4006
4007/** get default address of quantization matrix
4008 * \param sizeId size index
4009 * \param listId list index
4010 * \returns pointer of quantization matrix
4011 */
4012Int* TComScalingList::getScalingListDefaultAddress(UInt sizeId, UInt listId)
4013{
4014  Int *src = 0;
4015  switch(sizeId)
4016  {
4017    case SCALING_LIST_4x4:
4018      src = g_quantTSDefault4x4;
4019      break;
4020    case SCALING_LIST_8x8:
4021      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
4022      break;
4023    case SCALING_LIST_16x16:
4024      src = (listId<3) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
4025      break;
4026    case SCALING_LIST_32x32:
4027      src = (listId<1) ? g_quantIntraDefault8x8 : g_quantInterDefault8x8;
4028      break;
4029    default:
4030      assert(0);
4031      src = NULL;
4032      break;
4033  }
4034  return src;
4035}
4036
4037/** process of default matrix
4038 * \param sizeId size index
4039 * \param Index of input matrix
4040 */
4041Void TComScalingList::processDefaultMatrix(UInt sizeId, UInt listId)
4042{
4043  ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
4044  setScalingListDC(sizeId,listId,SCALING_LIST_DC);
4045}
4046
4047/** check DC value of matrix for default matrix signaling
4048 */
4049Void TComScalingList::checkDcOfMatrix()
4050{
4051  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
4052  {
4053    for(UInt listId = 0; listId < g_scalingListNum[sizeId]; listId++)
4054    {
4055      //check default matrix?
4056      if(getScalingListDC(sizeId,listId) == 0)
4057      {
4058        processDefaultMatrix(sizeId, listId);
4059      }
4060    }
4061  }
4062}
4063
4064ParameterSetManager::ParameterSetManager()
4065: m_vpsMap(MAX_NUM_VPS)
4066, m_spsMap(MAX_NUM_SPS)
4067, m_ppsMap(MAX_NUM_PPS)
4068, m_activeVPSId(-1)
4069#if !H_MV
4070, m_activeSPSId(-1)
4071, m_activePPSId(-1)
4072{
4073#else
4074{
4075  for (Int i = 0; i < MAX_NUM_LAYERS; i++ )
4076  {
4077    m_activeSPSId[ i ] = -1; 
4078    m_activePPSId[ i ] = -1; 
4079  }
4080#endif
4081}
4082
4083
4084ParameterSetManager::~ParameterSetManager()
4085{
4086}
4087
4088//! activate a SPS from a active parameter sets SEI message
4089//! \returns true, if activation is successful
4090#if H_MV
4091Bool ParameterSetManager::activateSPSWithSEI(Int spsId, Int layerId )
4092#else
4093Bool ParameterSetManager::activateSPSWithSEI(Int spsId)
4094#endif
4095{
4096  TComSPS *sps = m_spsMap.getPS(spsId);
4097  if (sps)
4098  {
4099    Int vpsId = sps->getVPSId();
4100    if (m_vpsMap.getPS(vpsId))
4101    {
4102      m_activeVPSId = vpsId;
4103#if H_MV
4104      m_activeSPSId[ layerId ] = spsId;
4105#else
4106      m_activeSPSId = spsId;
4107#endif
4108      return true;
4109    }
4110    else
4111    {
4112      printf("Warning: tried to activate SPS using an Active parameter sets SEI message. Referenced VPS does not exist.");
4113    }
4114  }
4115  else
4116  {
4117    printf("Warning: tried to activate non-existing SPS using an Active parameter sets SEI message.");
4118  }
4119  return false;
4120}
4121
4122//! activate a PPS and depending on isIDR parameter also SPS and VPS
4123//! \returns true, if activation is successful
4124#if H_MV
4125Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP, Int layerId )
4126#else
4127Bool ParameterSetManager::activatePPS(Int ppsId, Bool isIRAP)
4128#endif
4129{
4130  TComPPS *pps = m_ppsMap.getPS(ppsId);
4131  if (pps)
4132  {
4133    Int spsId = pps->getSPSId();
4134#if H_MV
4135    if (!isIRAP && (spsId != m_activeSPSId[ layerId ]))
4136#else
4137    if (!isIRAP && (spsId != m_activeSPSId))
4138#endif
4139    {
4140      printf("Warning: tried to activate PPS referring to a inactive SPS at non-IRAP.");
4141      return false;
4142    }
4143
4144    TComSPS *sps = m_spsMap.getPS(spsId);
4145    if (sps)
4146    {
4147      Int vpsId = sps->getVPSId();
4148      if (!isIRAP && (vpsId != m_activeVPSId))
4149      {
4150        printf("Warning: tried to activate PPS referring to a inactive VPS at non-IRAP.");
4151        return false;
4152      }
4153      if (m_vpsMap.getPS(vpsId))
4154      {
4155#if H_MV
4156        m_activePPSId[ layerId ] = ppsId;
4157        m_activeVPSId = vpsId;
4158        m_activeSPSId[ layerId ] = spsId;
4159#else
4160        m_activePPSId = ppsId;
4161        m_activeVPSId = vpsId;
4162        m_activeSPSId = spsId;
4163#endif
4164        return true;
4165      }
4166      else
4167      {
4168        printf("Warning: tried to activate PPS that refers to a non-existing VPS.");
4169      }
4170    }
4171    else
4172    {
4173      printf("Warning: tried to activate a PPS that refers to a non-existing SPS.");
4174    }
4175  }
4176  else
4177  {
4178    printf("Warning: tried to activate non-existing PPS.");
4179  }
4180  return false;
4181}
4182
4183ProfileTierLevel::ProfileTierLevel()
4184  : m_profileSpace    (0)
4185  , m_tierFlag        (false)
4186  , m_profileIdc      (0)
4187  , m_levelIdc        (0)
4188, m_progressiveSourceFlag  (false)
4189, m_interlacedSourceFlag   (false)
4190, m_nonPackedConstraintFlag(false)
4191, m_frameOnlyConstraintFlag(false)
4192{
4193  ::memset(m_profileCompatibilityFlag, 0, sizeof(m_profileCompatibilityFlag));
4194}
4195
4196TComPTL::TComPTL()
4197{
4198  ::memset(m_subLayerProfilePresentFlag, 0, sizeof(m_subLayerProfilePresentFlag));
4199  ::memset(m_subLayerLevelPresentFlag,   0, sizeof(m_subLayerLevelPresentFlag  ));
4200}
4201
4202#if H_MV
4203Void TComPTL::copyLevelFrom( TComPTL* source )
4204{
4205  getGeneralPTL()->setLevelIdc( source->getGeneralPTL()->getLevelIdc() );
4206  for( Int subLayer = 0; subLayer < 6; subLayer++ )
4207  {
4208    setSubLayerLevelPresentFlag( subLayer, source->getSubLayerLevelPresentFlag( subLayer ) );
4209    getSubLayerPTL( subLayer )->setLevelIdc( source->getSubLayerPTL( subLayer )->getLevelIdc() );
4210  }
4211}
4212#endif
4213//! \}
4214
4215#if H_MV
4216TComVPSVUI::TComVPSVUI()
4217{
4218  m_crossLayerIrapAlignedFlag = true; 
4219  m_allLayersIdrAlignedFlag   = false; 
4220  m_bitRatePresentVpsFlag = false;
4221  m_picRatePresentVpsFlag = false;
4222  for ( Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++)
4223  {   
4224    for ( Int j = 0; j < MAX_TLAYER; j++)
4225    {   
4226      m_bitRatePresentFlag          [i][j] = false;
4227      m_picRatePresentFlag          [i][j] = false;
4228      m_avgBitRate                  [i][j] = -1;
4229      m_maxBitRate                  [i][j] = -1;
4230      m_constantPicRateIdc          [i][j] = -1;
4231      m_avgPicRate                  [i][j] = -1;
4232    }
4233  }
4234
4235  m_ilpRestrictedRefLayersFlag = false;
4236
4237  for ( Int i = 0; i < MAX_NUM_LAYERS; i++)
4238  {         
4239    for ( Int j = 0; j < MAX_NUM_LAYERS; j++)
4240    {   
4241      m_tileBoundariesAlignedFlag   [i][j] = false;
4242      m_minSpatialSegmentOffsetPlus1[i][j] = 0;
4243      m_ctuBasedOffsetEnabledFlag   [i][j] = false;
4244      m_minHorizontalCtuOffsetPlus1 [i][j] = -1;
4245    }
4246    m_baseLayerParameterSetCompatibilityFlag[i] = false;
4247  }
4248  for ( Int i = 0; i < MAX_NUM_VIDEO_SIGNAL_INFO; i++ )
4249  {
4250    m_videoSignalInfo          [i] = NULL;     
4251  }
4252
4253  m_vpsVuiBspHrdPresentFlag = false; 
4254  m_vpsVuiBspHrdParameters  = new TComVpsVuiBspHrdParameters();
4255}
4256
4257TComVPSVUI::~TComVPSVUI()
4258{
4259  for ( Int i = 0; i < MAX_NUM_VIDEO_SIGNAL_INFO; i++ )
4260  {
4261    if (m_videoSignalInfo[ i ] != NULL )      delete m_videoSignalInfo[ i ];   
4262    m_videoSignalInfo    [ i ] = NULL; 
4263  }
4264
4265  if ( m_vpsVuiBspHrdParameters ) delete m_vpsVuiBspHrdParameters;
4266  m_vpsVuiBspHrdParameters = NULL; 
4267}
4268
4269Void TComVPSVUI::inferVpsVui( Bool encoderFlag )
4270{
4271  // inference of syntax elements that differ from default inference (as done in constructor), when VPS VUI is not present
4272  if (!encoderFlag )
4273  {
4274    setCrossLayerIrapAlignedFlag( false ); 
4275  }
4276  else
4277  {
4278    assert( !getCrossLayerIrapAlignedFlag() ); 
4279  }
4280}
4281
4282Void TComRepFormat::inferChromaAndBitDepth( TComRepFormat* prevRepFormat, Bool encoderFlag )
4283{
4284  if ( !encoderFlag )
4285  {
4286    setChromaAndBitDepthVpsPresentFlag( prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
4287    setSeparateColourPlaneVpsFlag     ( prevRepFormat->getSeparateColourPlaneVpsFlag     () );
4288    setBitDepthVpsLumaMinus8          ( prevRepFormat->getBitDepthVpsLumaMinus8          () );
4289    setBitDepthVpsChromaMinus8        ( prevRepFormat->getBitDepthVpsChromaMinus8        () );
4290  }
4291  else
4292  {
4293    assert( getChromaAndBitDepthVpsPresentFlag() == prevRepFormat->getChromaAndBitDepthVpsPresentFlag() );
4294    assert( getSeparateColourPlaneVpsFlag     () == prevRepFormat->getSeparateColourPlaneVpsFlag     () );
4295    assert( getBitDepthVpsLumaMinus8          () == prevRepFormat->getBitDepthVpsLumaMinus8          () );
4296    assert( getBitDepthVpsChromaMinus8        () == prevRepFormat->getBitDepthVpsChromaMinus8        () );
4297}
4298}
4299
4300Void TComVpsVuiBspHrdParameters::checkLayerInBspFlag( TComVPS* vps, Int h )
4301{
4302  // It is a requirement of bitstream conformance that bitstream partition with index j shall not include
4303  // direct or indirect reference layers of any layers in bitstream partition i for any values of i and j
4304  // in the range of 0 to num_bitstream_partitions[ h ] ?1, inclusive, such that i is less than j.
4305
4306  for ( Int partJ = 0; partJ < getNumBitstreamPartitions( h ); partJ++ )
4307  {       
4308    for ( Int partI = 0; partI < partJ; partI++ )
4309    {
4310      for ( Int layerJ = 0; layerJ < vps->getMaxLayersMinus1(); layerJ++ )
4311      {
4312        if ( m_layerInBspFlag[ h ][partJ][layerJ ] )
4313        {
4314          for ( Int layerI = 0; layerI < vps->getMaxLayersMinus1(); layerI++ )
4315          {
4316            if ( m_layerInBspFlag[ h ][partI][layerI] )
4317            {
4318              assert( !vps->getInDirectDependencyFlag( layerI, layerJ ) ); 
4319            }
4320          }
4321        }
4322      }
4323    }
4324  }
4325
4326  // ---------------
4327  // To be added:
4328  // When vps_base_layer_internal_flag is equal to 0 and layer_in_bsp_flag[ h ][ i ][ 0 ] is equal to 1 for any value of h in the
4329  // range of 1 to vps_num_layer_sets_minus1, inclusive, and any value of i in the range of 0 to num_bitstream_partitions[ h ] - 1,
4330  // inclusive, the value of layer_in_bsp_flag[ h ][ i ][ j ] for at least one value of j in the range of 1 to
4331  // NumLayersInIdList[ h ] - 1, inclusive, shall be equal to 1.
4332  // ---------------
4333
4334
4335  // When num_bitstream_partitions[ h ] is equal to 1 for any value of h in the range 1 to vps_num_layer_set_minus1, inclusive,
4336  // the value of layer_in_bsp_flag[ h ][ 0 ][ j ] should be equal to 0 for at least one value of j in the range 0 to
4337  // NumLayersInIdList[ h ] − 1, inclusive.
4338
4339
4340  if ( getNumBitstreamPartitions( h ) == 1 )
4341  {
4342    Bool atLeastOneZero = false; 
4343    for ( Int j = 0; j <= vps->getNumLayersInIdList( h ) - 1; j++ )
4344    {
4345      atLeastOneZero = atLeastOneZero || !getLayerInBspFlag( h, 0, j ); 
4346    }   
4347    assert( atLeastOneZero ); 
4348  }
4349
4350 
4351  // For any value of h in the range 1 to vps_num_layer_set_minus1, inclusive, the value of layer_in_bsp_flag[ h ][ i ][ j ]
4352  // shall be equal to 1 for at most one value of i in the range of 0 to num_bitstream_partitions[ h ] − 1, inclusive.
4353
4354  for ( Int j = 0; j <= vps->getNumLayersInIdList( h ) - 1; j++ )
4355  {
4356    Int numLayerInBsp = 0; 
4357    for ( Int i = 0; i <= getNumBitstreamPartitions( h ) - 1; i++ )
4358    {
4359      numLayerInBsp += ( getLayerInBspFlag( h, i, j ) ? 1 : 0 ); 
4360    }   
4361    assert( numLayerInBsp <= 1 ); 
4362  }
4363
4364}
4365
4366Void TComVpsVuiBspHrdParameters::checkBspCombHrdAndShedIdx( TComVPS* vps, Int h, Int i, Int j )
4367{
4368  // bsp_comb_hrd_idx
4369  assert( getBspCombSchedIdx(h, i, j ) >= 0 );
4370  assert( getBspCombSchedIdx(h, i, j ) <= getVpsNumBspHrdParametersMinus1() );
4371
4372  // bsp_comb_sched_idx
4373  assert( getBspCombSchedIdx(h, i, j ) >= 0 );
4374
4375  //* This check needs to activated,  when HighestTid is available here   
4376  //  assert(  getBspCombSchedIdx(h, i, j ) <= vps->getHrdParameters( getBspCombHrdIdx( h, i, j ) )->getCpbCntMinus1( highestTid ) );
4377}
4378
4379Void TComVUI::inferVideoSignalInfo( TComVPS* vps, Int layerIdCurr )
4380{
4381  if ( layerIdCurr == 0 || !vps->getVpsVuiPresentFlag() ) 
4382  {
4383    return; 
4384  }
4385
4386  TComVPSVUI* vpsVUI = vps->getVPSVUI(); 
4387  assert( vpsVUI != NULL ); 
4388
4389  TComVideoSignalInfo* videoSignalInfo = vpsVUI->getVideoSignalInfo( vpsVUI->getVpsVideoSignalInfoIdx( vps->getLayerIdInVps( layerIdCurr ) ) ); 
4390  assert( videoSignalInfo != NULL );
4391
4392  setVideoFormat            ( videoSignalInfo->getVideoVpsFormat            () ); 
4393  setVideoFullRangeFlag     ( videoSignalInfo->getVideoFullRangeVpsFlag     () );
4394  setColourPrimaries        ( videoSignalInfo->getColourPrimariesVps        () );
4395  setTransferCharacteristics( videoSignalInfo->getTransferCharacteristicsVps() );
4396  setMatrixCoefficients     ( videoSignalInfo->getMatrixCoeffsVps           () );
4397}
4398
4399TComDpbSize::TComDpbSize()
4400{
4401  for (Int i = 0; i < MAX_VPS_OUTPUTLAYER_SETS; i++ )
4402  {     
4403    m_subLayerFlagInfoPresentFlag[i]  = false;
4404
4405    for (Int j = 0; j < MAX_TLAYER; j++  )
4406    {       
4407      m_subLayerDpbInfoPresentFlag [i][j] = ( j == 0) ;
4408      m_maxVpsNumReorderPics       [i][j] = 0;
4409      m_maxVpsLatencyIncreasePlus1 [i][j] = 0;
4410
4411      for (Int k = 0; k < MAX_NUM_LAYER_IDS; k++ )
4412      {
4413        m_maxVpsDecPicBufferingMinus1[i][k][j] = 0; 
4414      }
4415    }
4416  }
4417}
4418
4419Void Window::scaleOffsets( Int scal )
4420{
4421  if (! m_scaledFlag )
4422  {
4423    m_scaledFlag         = true; 
4424    m_winLeftOffset     *= scal; 
4425    m_winRightOffset    *= scal; 
4426    m_winTopOffset      *= scal; 
4427    m_winBottomOffset   *= scal; 
4428  }
4429}
4430#endif
4431
Note: See TracBrowser for help on using the repository browser.