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

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

MergeMerged tags/HTM-DEV-1.0.

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