source: 3DVCSoftware/branches/HTM-10.1-dev0/source/Lib/TLibCommon/TComSlice.cpp @ 882

Last change on this file since 882 was 882, checked in by tech, 10 years ago

Cleanups part 7.

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