source: 3DVCSoftware/branches/HTM-11.2-dev3-HiSilicon/source/Lib/TLibCommon/TComSlice.cpp @ 984

Last change on this file since 984 was 983, checked in by mediatek-htm, 11 years ago

Integration of Single Depth Mode proposed in JCT3V-I0095.
The MACRO is "MTK_SINGLE_DEPTH_MODE_I0095".

By Yi-Wen Chen (yiwen.chen@…)

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