source: 3DVCSoftware/branches/HTM-8.2-dev0-Cleanup/source/Lib/TLibCommon/TComSlice.cpp @ 648

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

Macro removal part H_MV5.

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