source: 3DVCSoftware/trunk/source/Lib/TLibDecoder/TDecCAVLC.cpp

Last change on this file was 1413, checked in by tech, 7 years ago

Merged HTM-16.2-dev@1412

  • Property svn:eol-style set to native
File size: 143.7 KB
Line 
1/* The copyright in this software is being made available under the BSD
2* License, included below. This software may be subject to other third party
3* and contributor rights, including patent rights, and no such rights are
4* granted under this license.
5*
6* Copyright (c) 2010-2017, 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     TDecCAVLC.cpp
35\brief    CAVLC decoder class
36*/
37
38#include "TDecCAVLC.h"
39#include "SEIread.h"
40#include "TDecSlice.h"
41#include "TLibCommon/TComChromaFormat.h"
42#if RExt__DECODER_DEBUG_BIT_STATISTICS
43#include "TLibCommon/TComCodingStatistics.h"
44#endif
45#include "TDecConformance.h"
46
47#if NH_MV
48#include "TDecTop.h"
49#endif
50
51//! \ingroup TLibDecoder
52//! \{
53
54#if ENC_DEC_TRACE
55#if !NH_MV_ENC_DEC_TRAC
56Void  xTraceVPSHeader ()
57{
58  fprintf( g_hTrace, "=========== Video Parameter Set     ===========\n" );
59}
60
61Void  xTraceSPSHeader ()
62{
63  fprintf( g_hTrace, "=========== Sequence Parameter Set  ===========\n" );
64}
65
66Void  xTracePPSHeader ()
67{
68  fprintf( g_hTrace, "=========== Picture Parameter Set  ===========\n");
69}
70
71Void  xTraceSliceHeader ()
72{
73  fprintf( g_hTrace, "=========== Slice ===========\n");
74}
75
76#endif
77#endif
78// ====================================================================================================================
79// Constructor / destructor / create / destroy
80// ====================================================================================================================
81
82TDecCavlc::TDecCavlc()
83{
84}
85
86TDecCavlc::~TDecCavlc()
87{
88
89}
90
91// ====================================================================================================================
92// Public member functions
93// ====================================================================================================================
94
95#if NH_MV
96Void TDecCavlc::parseShortTermRefPicSet( TComSPS* sps, TComStRefPicSet* stRps, Int stRpsIdx )
97{
98  UInt uiCode; 
99  if( stRpsIdx  !=  0 )
100  {
101    READ_FLAG( uiCode, "inter_ref_pic_set_prediction_flag" ); stRps->setInterRefPicSetPredictionFlag( uiCode == 1 );
102  }
103  if( stRps->getInterRefPicSetPredictionFlag() )
104  {
105    if( stRpsIdx  ==  sps->getNumShortTermRefPicSets() )
106    {
107      READ_UVLC( uiCode, "delta_idx_minus1" ); stRps->setDeltaIdxMinus1( uiCode );
108    }
109    READ_FLAG( uiCode, "delta_rps_sign" ); stRps->setDeltaRpsSign( uiCode == 1 );
110    READ_UVLC( uiCode, "abs_delta_rps_minus1" ); stRps->setAbsDeltaRpsMinus1( uiCode );
111    for( Int j = 0; j  <=  sps->getStRefPicSet( stRps->getRefRpsIdx( stRpsIdx ) )->getNumDeltaPocs() ; j++ )
112    {
113      READ_FLAG( uiCode, "used_by_curr_pic_flag" ); stRps->setUsedByCurrPicFlag( j, uiCode == 1 );
114      if( !stRps->getUsedByCurrPicFlag( j ) )
115      {
116        READ_FLAG( uiCode, "use_delta_flag" ); stRps->setUseDeltaFlag( j, uiCode == 1 );
117      }
118    }
119  }
120  else
121  {
122    READ_UVLC( uiCode, "num_negative_pics" ); stRps->setNumNegativePics( uiCode );
123    READ_UVLC( uiCode, "num_positive_pics" ); stRps->setNumPositivePics( uiCode );
124    for( Int i = 0; i < stRps->getNumNegativePics( ); i++ )
125    {
126      READ_UVLC( uiCode, "delta_poc_s0_minus1" ); stRps->setDeltaPocS0Minus1( i, uiCode );
127      READ_FLAG( uiCode, "used_by_curr_pic_s0_flag" ); stRps->setUsedByCurrPicS0Flag( i, uiCode == 1 );
128    }
129    for( Int i = 0; i < stRps->getNumPositivePics( ); i++ )
130    {
131      READ_UVLC( uiCode, "delta_poc_s1_minus1" ); stRps->setDeltaPocS1Minus1( i, uiCode );
132      READ_FLAG( uiCode, "used_by_curr_pic_s1_flag" ); stRps->setUsedByCurrPicS1Flag( i, uiCode == 1 );
133    }
134  }
135  stRps->inferRps( stRpsIdx, sps, false );
136}
137#else
138Void TDecCavlc::parseShortTermRefPicSet( TComSPS* sps, TComReferencePictureSet* rps, Int idx )
139{
140  UInt code;
141  UInt interRPSPred;
142  if (idx > 0)
143  {
144    READ_FLAG(interRPSPred, "inter_ref_pic_set_prediction_flag");  rps->setInterRPSPrediction(interRPSPred);
145  }
146  else
147  {
148    interRPSPred = false;
149    rps->setInterRPSPrediction(false);
150  }
151
152  if (interRPSPred)
153  {
154    UInt bit;
155    if(idx == sps->getRPSList()->getNumberOfReferencePictureSets())
156    {
157      READ_UVLC(code, "delta_idx_minus1" ); // delta index of the Reference Picture Set used for prediction minus 1
158    }
159    else
160    {
161      code = 0;
162    }
163    assert(code <= idx-1); // delta_idx_minus1 shall not be larger than idx-1, otherwise we will predict from a negative row position that does not exist. When idx equals 0 there is no legal value and interRPSPred must be zero. See J0185-r2
164    Int rIdx =  idx - 1 - code;
165    assert (rIdx <= idx-1 && rIdx >= 0); // Made assert tighter; if rIdx = idx then prediction is done from itself. rIdx must belong to range 0, idx-1, inclusive, see J0185-r2
166    TComReferencePictureSet*   rpsRef = sps->getRPSList()->getReferencePictureSet(rIdx);
167    Int k = 0, k0 = 0, k1 = 0;
168    READ_CODE(1, bit, "delta_rps_sign"); // delta_RPS_sign
169    READ_UVLC(code, "abs_delta_rps_minus1");  // absolute delta RPS minus 1
170    Int deltaRPS = (1 - 2 * bit) * (code + 1); // delta_RPS
171    for(Int j=0 ; j <= rpsRef->getNumberOfPictures(); j++)
172    {
173      READ_CODE(1, bit, "used_by_curr_pic_flag" ); //first bit is "1" if Idc is 1
174      Int refIdc = bit;
175      if (refIdc == 0)
176      {
177        READ_CODE(1, bit, "use_delta_flag" ); //second bit is "1" if Idc is 2, "0" otherwise.
178        refIdc = bit<<1; //second bit is "1" if refIdc is 2, "0" if refIdc = 0.
179      }
180      if (refIdc == 1 || refIdc == 2)
181      {
182        Int deltaPOC = deltaRPS + ((j < rpsRef->getNumberOfPictures())? rpsRef->getDeltaPOC(j) : 0);
183        rps->setDeltaPOC(k, deltaPOC);
184        rps->setUsed(k, (refIdc == 1));
185
186        if (deltaPOC < 0)
187        {
188          k0++;
189        }
190        else
191        {
192          k1++;
193        }
194        k++;
195      }
196      rps->setRefIdc(j,refIdc);
197    }
198    rps->setNumRefIdc(rpsRef->getNumberOfPictures()+1);
199    rps->setNumberOfPictures(k);
200    rps->setNumberOfNegativePictures(k0);
201    rps->setNumberOfPositivePictures(k1);
202    rps->sortDeltaPOC();
203  }
204  else
205  {
206    READ_UVLC(code, "num_negative_pics");           rps->setNumberOfNegativePictures(code);
207    READ_UVLC(code, "num_positive_pics");           rps->setNumberOfPositivePictures(code);
208    Int prev = 0;
209    Int poc;
210    for(Int j=0 ; j < rps->getNumberOfNegativePictures(); j++)
211    {
212      READ_UVLC(code, "delta_poc_s0_minus1");
213      poc = prev-code-1;
214      prev = poc;
215      rps->setDeltaPOC(j,poc);
216      READ_FLAG(code, "used_by_curr_pic_s0_flag");  rps->setUsed(j,code);
217    }
218    prev = 0;
219    for(Int j=rps->getNumberOfNegativePictures(); j < rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures(); j++)
220    {
221      READ_UVLC(code, "delta_poc_s1_minus1");
222      poc = prev+code+1;
223      prev = poc;
224      rps->setDeltaPOC(j,poc);
225      READ_FLAG(code, "used_by_curr_pic_s1_flag");  rps->setUsed(j,code);
226    }
227    rps->setNumberOfPictures(rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures());
228  }
229#if PRINT_RPS_INFO
230  rps->printDeltaPOC();
231#endif
232}
233#endif
234
235Void TDecCavlc::parsePPS(TComPPS* pcPPS)
236{
237#if ENC_DEC_TRACE
238#if NH_MV_ENC_DEC_TRAC
239  tracePSHeader( "PPS", pcPPS->getLayerId() ); 
240#else
241  xTracePPSHeader ();
242#endif
243#endif
244  UInt  uiCode;
245
246  Int   iCode;
247
248  READ_UVLC_CHK( uiCode, "pps_pic_parameter_set_id", 0, 63);
249  assert(uiCode <= 63);
250  pcPPS->setPPSId (uiCode);
251
252  READ_UVLC_CHK( uiCode, "pps_seq_parameter_set_id", 0, 15);
253  assert(uiCode <= 15);
254  pcPPS->setSPSId (uiCode);
255
256  READ_FLAG( uiCode, "dependent_slice_segments_enabled_flag"    );    pcPPS->setDependentSliceSegmentsEnabledFlag   ( uiCode == 1 );
257
258  READ_FLAG( uiCode, "output_flag_present_flag" );                    pcPPS->setOutputFlagPresentFlag( uiCode==1 );
259
260  READ_CODE(3, uiCode, "num_extra_slice_header_bits");                pcPPS->setNumExtraSliceHeaderBits(uiCode);
261
262  READ_FLAG ( uiCode, "sign_data_hiding_enabled_flag" );              pcPPS->setSignDataHidingEnabledFlag( uiCode );
263
264  READ_FLAG( uiCode,   "cabac_init_present_flag" );            pcPPS->setCabacInitPresentFlag( uiCode ? true : false );
265
266  READ_UVLC_CHK(uiCode, "num_ref_idx_l0_default_active_minus1", 0, 14);
267  assert(uiCode <= 14);
268  pcPPS->setNumRefIdxL0DefaultActive(uiCode+1);
269
270  READ_UVLC_CHK(uiCode, "num_ref_idx_l1_default_active_minus1", 0, 14);
271  assert(uiCode <= 14);
272  pcPPS->setNumRefIdxL1DefaultActive(uiCode+1);
273
274  READ_SVLC_CHK(iCode, "init_qp_minus26", std::numeric_limits<Int>::min(), 25 );  pcPPS->setPicInitQPMinus26(iCode);
275  READ_FLAG( uiCode, "constrained_intra_pred_flag" );              pcPPS->setConstrainedIntraPred( uiCode ? true : false );
276  READ_FLAG( uiCode, "transform_skip_enabled_flag" );
277  pcPPS->setUseTransformSkip ( uiCode ? true : false );
278
279  READ_FLAG( uiCode, "cu_qp_delta_enabled_flag" );            pcPPS->setUseDQP( uiCode ? true : false );
280  if( pcPPS->getUseDQP() )
281  {
282    READ_UVLC( uiCode, "diff_cu_qp_delta_depth" );
283    pcPPS->setMaxCuDQPDepth( uiCode );
284  }
285  else
286  {
287    pcPPS->setMaxCuDQPDepth( 0 );
288  }
289  READ_SVLC_CHK( iCode, "pps_cb_qp_offset", -12, 12);
290  pcPPS->setQpOffset(COMPONENT_Cb, iCode);
291  assert( pcPPS->getQpOffset(COMPONENT_Cb) >= -12 );
292  assert( pcPPS->getQpOffset(COMPONENT_Cb) <=  12 );
293
294  READ_SVLC_CHK( iCode, "pps_cr_qp_offset", -12, 12);
295  pcPPS->setQpOffset(COMPONENT_Cr, iCode);
296  assert( pcPPS->getQpOffset(COMPONENT_Cr) >= -12 );
297  assert( pcPPS->getQpOffset(COMPONENT_Cr) <=  12 );
298
299  assert(MAX_NUM_COMPONENT<=3);
300
301  READ_FLAG( uiCode, "pps_slice_chroma_qp_offsets_present_flag" );
302  pcPPS->setSliceChromaQpFlag( uiCode ? true : false );
303
304  READ_FLAG( uiCode, "weighted_pred_flag" );          // Use of Weighting Prediction (P_SLICE)
305  pcPPS->setUseWP( uiCode==1 );
306  READ_FLAG( uiCode, "weighted_bipred_flag" );         // Use of Bi-Directional Weighting Prediction (B_SLICE)
307  pcPPS->setWPBiPred( uiCode==1 );
308
309  READ_FLAG( uiCode, "transquant_bypass_enabled_flag");
310  pcPPS->setTransquantBypassEnabledFlag(uiCode ? true : false);
311  READ_FLAG( uiCode, "tiles_enabled_flag"               );    pcPPS->setTilesEnabledFlag            ( uiCode == 1 );
312  READ_FLAG( uiCode, "entropy_coding_sync_enabled_flag" );    pcPPS->setEntropyCodingSyncEnabledFlag( uiCode == 1 );
313
314  if( pcPPS->getTilesEnabledFlag() )
315  {
316    READ_UVLC ( uiCode, "num_tile_columns_minus1" );                pcPPS->setNumTileColumnsMinus1( uiCode ); 
317    READ_UVLC ( uiCode, "num_tile_rows_minus1" );                   pcPPS->setNumTileRowsMinus1( uiCode ); 
318    READ_FLAG ( uiCode, "uniform_spacing_flag" );                   pcPPS->setTileUniformSpacingFlag( uiCode == 1 );
319
320    const UInt tileColumnsMinus1 = pcPPS->getNumTileColumnsMinus1();
321    const UInt tileRowsMinus1    = pcPPS->getNumTileRowsMinus1();
322 
323    if ( !pcPPS->getTileUniformSpacingFlag())
324    {
325      if (tileColumnsMinus1 > 0)
326      {
327        std::vector<Int> columnWidth(tileColumnsMinus1);
328        for(UInt i = 0; i < tileColumnsMinus1; i++)
329        { 
330          READ_UVLC( uiCode, "column_width_minus1" ); 
331          columnWidth[i] = uiCode+1;
332        }
333        pcPPS->setTileColumnWidth(columnWidth);
334      }
335
336      if (tileRowsMinus1 > 0)
337      {
338        std::vector<Int> rowHeight (tileRowsMinus1);
339        for(UInt i = 0; i < tileRowsMinus1; i++)
340        {
341          READ_UVLC( uiCode, "row_height_minus1" );
342          rowHeight[i] = uiCode + 1;
343        }
344        pcPPS->setTileRowHeight(rowHeight);
345      }
346    }
347
348    assert ((tileColumnsMinus1 + tileRowsMinus1) != 0);
349      READ_FLAG ( uiCode, "loop_filter_across_tiles_enabled_flag" );   pcPPS->setLoopFilterAcrossTilesEnabledFlag( uiCode ? true : false );
350    }
351  READ_FLAG( uiCode, "pps_loop_filter_across_slices_enabled_flag" );   pcPPS->setLoopFilterAcrossSlicesEnabledFlag( uiCode ? true : false );
352  READ_FLAG( uiCode, "deblocking_filter_control_present_flag" );       pcPPS->setDeblockingFilterControlPresentFlag( uiCode ? true : false );
353  if(pcPPS->getDeblockingFilterControlPresentFlag())
354  {
355    READ_FLAG( uiCode, "deblocking_filter_override_enabled_flag" );    pcPPS->setDeblockingFilterOverrideEnabledFlag( uiCode ? true : false );
356    READ_FLAG( uiCode, "pps_deblocking_filter_disabled_flag" );        pcPPS->setPPSDeblockingFilterDisabledFlag(uiCode ? true : false );
357    if(!pcPPS->getPPSDeblockingFilterDisabledFlag())
358    {
359      READ_SVLC ( iCode, "pps_beta_offset_div2" );                     pcPPS->setDeblockingFilterBetaOffsetDiv2( iCode );
360      READ_SVLC ( iCode, "pps_tc_offset_div2" );                       pcPPS->setDeblockingFilterTcOffsetDiv2( iCode );
361    }
362  }
363  READ_FLAG( uiCode, "pps_scaling_list_data_present_flag" );           pcPPS->setScalingListPresentFlag( uiCode ? true : false );
364  if(pcPPS->getScalingListPresentFlag ())
365  {
366    parseScalingList( &(pcPPS->getScalingList()) );
367  }
368
369  READ_FLAG( uiCode, "lists_modification_present_flag");
370  pcPPS->setListsModificationPresentFlag(uiCode);
371
372  READ_UVLC( uiCode, "log2_parallel_merge_level_minus2");
373  pcPPS->setLog2ParallelMergeLevelMinus2 (uiCode);
374
375  READ_FLAG( uiCode, "slice_segment_header_extension_present_flag");
376  pcPPS->setSliceHeaderExtensionPresentFlag(uiCode);
377
378  READ_FLAG( uiCode, "pps_extension_present_flag");
379  if (uiCode)
380  {
381#if NH_MV
382    READ_FLAG( uiCode, "pps_range_extensions_flag" ); pcPPS->setPpsRangeExtensionsFlag( uiCode == 1 );
383    READ_FLAG( uiCode, "pps_multilayer_extension_flag" ); pcPPS->setPpsMultilayerExtensionFlag( uiCode == 1 );
384    READ_FLAG( uiCode, "pps_3d_extension_flag" ); pcPPS->setPps3dExtensionFlag( uiCode == 1 );
385    READ_CODE( 5, uiCode, "pps_extension_5bits" ); pcPPS->setPpsExtension5bits( uiCode );
386    if ( pcPPS->getPpsRangeExtensionsFlag() )
387    { 
388      TComPPSRExt &ppsRangeExtension = pcPPS->getPpsRangeExtension();     
389
390      if (pcPPS->getUseTransformSkip())
391      {
392        READ_UVLC( uiCode, "log2_max_transform_skip_block_size_minus2");
393        ppsRangeExtension.setLog2MaxTransformSkipBlockSize(uiCode+2);
394      }
395
396      READ_FLAG( uiCode, "cross_component_prediction_enabled_flag");
397      ppsRangeExtension.setCrossComponentPredictionEnabledFlag(uiCode != 0);
398
399      READ_FLAG( uiCode, "chroma_qp_offset_list_enabled_flag");
400      if (uiCode == 0)
401      {
402        ppsRangeExtension.clearChromaQpOffsetList();
403        ppsRangeExtension.setDiffCuChromaQpOffsetDepth(0);
404      }
405      else
406      {
407        READ_UVLC(uiCode, "diff_cu_chroma_qp_offset_depth"); ppsRangeExtension.setDiffCuChromaQpOffsetDepth(uiCode);
408        UInt tableSizeMinus1 = 0;
409        READ_UVLC(tableSizeMinus1, "chroma_qp_offset_list_len_minus1");
410        assert(tableSizeMinus1 < MAX_QP_OFFSET_LIST_SIZE);
411
412        for (Int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx <= (tableSizeMinus1); cuChromaQpOffsetIdx++)
413        {
414          Int cbOffset;
415          Int crOffset;
416          READ_SVLC(cbOffset, "cb_qp_offset_list[i]");
417          assert(cbOffset >= -12 && cbOffset <= 12);
418          READ_SVLC(crOffset, "cr_qp_offset_list[i]");
419          assert(crOffset >= -12 && crOffset <= 12);
420          // table uses +1 for index (see comment inside the function)
421          ppsRangeExtension.setChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1, cbOffset, crOffset);
422        }
423        assert(ppsRangeExtension.getChromaQpOffsetListLen() == tableSizeMinus1 + 1);
424      }
425
426      READ_UVLC( uiCode, "log2_sao_offset_scale_luma");
427      ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_LUMA, uiCode);
428      READ_UVLC( uiCode, "log2_sao_offset_scale_chroma");
429      ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA, uiCode);    }
430
431    if ( pcPPS->getPpsMultilayerExtensionFlag() )
432    { 
433      parsePpsMultilayerExtension( pcPPS ); 
434    }
435
436    if ( pcPPS->getPps3dExtensionFlag() )
437    { 
438#if NH_3D
439      parsePps3dExtension( pcPPS );
440#endif
441    }
442#if NH_3D
443    if ( pcPPS->getPpsExtension5bits() )
444#else
445    if ( pcPPS->getPpsExtension5bits() || pcPPS->getPps3dExtensionFlag() )
446#endif
447    {
448      while ( xMoreRbspData() )
449      {
450        READ_FLAG( uiCode, "pps_extension_data_flag");
451      }
452    }
453
454#else
455#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
456    static const TChar *syntaxStrings[]={ "pps_range_extension_flag",
457                                         "pps_multilayer_extension_flag",
458                                         "pps_extension_6bits[0]",
459                                         "pps_extension_6bits[1]",
460                                         "pps_extension_6bits[2]",
461                                         "pps_extension_6bits[3]",
462                                         "pps_extension_6bits[4]",
463                                         "pps_extension_6bits[5]" };
464#endif
465
466    Bool pps_extension_flags[NUM_PPS_EXTENSION_FLAGS];
467    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++)
468    {
469      READ_FLAG( uiCode, syntaxStrings[i] );
470      pps_extension_flags[i] = uiCode!=0;
471    }
472
473    Bool bSkipTrailingExtensionBits=false;
474    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
475    {
476      if (pps_extension_flags[i])
477      {
478        switch (PPSExtensionFlagIndex(i))
479        {
480          case PPS_EXT__REXT:
481            {
482              TComPPSRExt &ppsRangeExtension = pcPPS->getPpsRangeExtension();
483            assert(!bSkipTrailingExtensionBits);
484
485            if (pcPPS->getUseTransformSkip())
486            {
487              READ_UVLC( uiCode, "log2_max_transform_skip_block_size_minus2");
488                ppsRangeExtension.setLog2MaxTransformSkipBlockSize(uiCode+2);
489            }
490
491            READ_FLAG( uiCode, "cross_component_prediction_enabled_flag");
492              ppsRangeExtension.setCrossComponentPredictionEnabledFlag(uiCode != 0);
493
494            READ_FLAG( uiCode, "chroma_qp_offset_list_enabled_flag");
495            if (uiCode == 0)
496            {
497                ppsRangeExtension.clearChromaQpOffsetList();
498                ppsRangeExtension.setDiffCuChromaQpOffsetDepth(0);
499            }
500            else
501            {
502                READ_UVLC(uiCode, "diff_cu_chroma_qp_offset_depth"); ppsRangeExtension.setDiffCuChromaQpOffsetDepth(uiCode);
503              UInt tableSizeMinus1 = 0;
504                READ_UVLC_CHK(tableSizeMinus1, "chroma_qp_offset_list_len_minus1", 0, MAX_QP_OFFSET_LIST_SIZE-1);
505              assert(tableSizeMinus1 < MAX_QP_OFFSET_LIST_SIZE);
506
507              for (Int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx <= (tableSizeMinus1); cuChromaQpOffsetIdx++)
508              {
509                Int cbOffset;
510                Int crOffset;
511                  READ_SVLC_CHK(cbOffset, "cb_qp_offset_list[i]", -12, 12);
512                assert(cbOffset >= -12 && cbOffset <= 12); 
513                  READ_SVLC_CHK(crOffset, "cr_qp_offset_list[i]", -12, 12);
514                assert(crOffset >= -12 && crOffset <= 12);
515                // table uses +1 for index (see comment inside the function)
516                  ppsRangeExtension.setChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1, cbOffset, crOffset);
517              }
518                assert(ppsRangeExtension.getChromaQpOffsetListLen() == tableSizeMinus1 + 1);
519            }
520
521            READ_UVLC( uiCode, "log2_sao_offset_scale_luma");
522              ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_LUMA, uiCode);
523            READ_UVLC( uiCode, "log2_sao_offset_scale_chroma");
524              ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA, uiCode);
525            }
526            break;
527          default:
528            bSkipTrailingExtensionBits=true;
529            break;
530        }
531      }
532    }
533    if (bSkipTrailingExtensionBits)
534    {
535      while ( xMoreRbspData() )
536      {
537        READ_FLAG( uiCode, "pps_extension_data_flag");
538      }
539    }
540#endif
541  }
542  xReadRbspTrailingBits();
543}
544
545#if NH_3D
546Void TDecCavlc::parsePps3dExtension( TComPPS* pcPPS )
547{
548  UInt uiCode = 0; 
549  //
550  TComDLT* pcDLT = pcPPS->getDLT();
551
552  READ_FLAG(uiCode, "dlts_present_flag");
553  pcDLT->setDltPresentFlag( (uiCode == 1) ? true : false );
554
555  if ( pcDLT->getDltPresentFlag() )
556  {
557    READ_CODE(6, uiCode, "pps_depth_layers_minus1");
558    pcDLT->setNumDepthViews( uiCode+1 );
559   
560    READ_CODE(4, uiCode, "pps_bit_depth_for_depth_layers_minus8");
561    pcDLT->setDepthViewBitDepth( (uiCode+8) );
562   
563    for( Int i = 0; i <= pcDLT->getNumDepthViews()-1; i++ )
564    {
565      Int layerId = pcDLT->getDepthIdxToLayerId(i);
566     
567      READ_FLAG(uiCode, "dlt_flag[i]");
568      pcDLT->setUseDLTFlag(layerId, (uiCode == 1) ? true : false);
569     
570      if ( pcDLT->getUseDLTFlag( layerId ) )
571      {
572        Bool bDltBitMapRepFlag    = false;
573        UInt uiMaxDiff            = MAX_INT;
574        UInt uiMinDiff            = 0;
575        UInt uiCodeLength         = 0;
576       
577        READ_FLAG(uiCode, "dlt_pred_flag[i]");
578       
579        if( uiCode )
580        {
581          assert( pcDLT->getUseDLTFlag( 1 ));
582        }
583        pcDLT->setInterViewDltPredEnableFlag( layerId, (uiCode == 1) ? true : false );
584       
585        if ( pcDLT->getInterViewDltPredEnableFlag( layerId ) == false )
586        {
587          READ_FLAG(uiCode, "dlt_val_flags_present_flag[i]");
588          bDltBitMapRepFlag = (uiCode == 1) ? true : false;
589        }
590        else
591        {
592          bDltBitMapRepFlag = false;
593        }
594       
595        UInt uiNumDepthValues = 0;
596        std::vector<Int> aiIdx2DepthValue(256, 0);
597       
598        // Bit map
599        if ( bDltBitMapRepFlag )
600        {
601          for (UInt d=0; d<256; d++)
602          {
603            READ_FLAG(uiCode, "dlt_value_flag[i][j]");
604            if (uiCode == 1)
605            {
606              aiIdx2DepthValue[uiNumDepthValues] = d;
607              uiNumDepthValues++;
608            }
609          }
610        }
611        // Diff Coding
612        else
613        {
614          READ_CODE(8, uiNumDepthValues, "num_val_delta_dlt");   // num_entry
615         
616          {
617            // The condition if( pcVPS->getNumDepthValues(i) > 0 ) is always true since for Single-view Diff Coding, there is at least one depth value in depth component.
618           
619            if (uiNumDepthValues > 1)
620            {
621              READ_CODE(8, uiCode, "max_diff");
622              uiMaxDiff = uiCode;
623            }
624            else
625            {
626              uiMaxDiff = 0;           // when there is only one value in DLT
627            }
628           
629            if (uiNumDepthValues > 2)
630            {
631              uiCodeLength = (UInt) gCeilLog2(uiMaxDiff + 1);
632              READ_CODE(uiCodeLength, uiCode, "min_diff_minus1");
633              uiMinDiff = uiCode + 1;
634            }
635            else
636            {
637              uiMinDiff = uiMaxDiff;   // when there are only one or two values in DLT
638            }
639           
640            READ_CODE(8, uiCode, "delta_dlt_val0");   // entry0
641            aiIdx2DepthValue[0] = uiCode;
642           
643            if (uiMaxDiff == uiMinDiff)
644            {
645              for (UInt d=1; d<uiNumDepthValues; d++)
646              {
647                aiIdx2DepthValue[d] = aiIdx2DepthValue[d-1] + uiMinDiff + 0;
648              }
649            }
650            else
651            {
652              uiCodeLength = (UInt) gCeilLog2(uiMaxDiff - uiMinDiff + 1);
653              for (UInt d=1; d<uiNumDepthValues; d++)
654              {
655                READ_CODE(uiCodeLength, uiCode, "delta_val_diff_minus_min[k]");
656                aiIdx2DepthValue[d] = aiIdx2DepthValue[d-1] + uiMinDiff + uiCode;
657              }
658            }
659           
660          }
661        }
662       
663        if( pcDLT->getInterViewDltPredEnableFlag( layerId ) )
664        {
665          // interpret decoded values as delta DLT
666          AOF( layerId > 1 );
667          // assumes ref layer id to be 1
668          std::vector<Int> viRefDLT = pcDLT->idx2DepthValue( 1 );
669          UInt uiRefNum = pcDLT->getNumDepthValues( 1 );
670          pcDLT->setDeltaDLT(layerId, viRefDLT, uiRefNum, aiIdx2DepthValue, uiNumDepthValues);
671        }
672        else
673        {
674          // store final DLT
675          pcDLT->setDepthLUTs(layerId, aiIdx2DepthValue, uiNumDepthValues);
676        }
677      }
678    }
679  }
680
681}
682#endif
683
684
685Void  TDecCavlc::parseVUI(TComVUI* pcVUI, TComSPS *pcSPS)
686{
687#if ENC_DEC_TRACE
688  fprintf( g_hTrace, "----------- vui_parameters -----------\n");
689#endif
690  UInt  uiCode;
691
692  READ_FLAG(     uiCode, "aspect_ratio_info_present_flag");           pcVUI->setAspectRatioInfoPresentFlag(uiCode);
693  if (pcVUI->getAspectRatioInfoPresentFlag())
694  {
695    READ_CODE(8, uiCode, "aspect_ratio_idc");                         pcVUI->setAspectRatioIdc(uiCode);
696    if (pcVUI->getAspectRatioIdc() == 255)
697    {
698      READ_CODE(16, uiCode, "sar_width");                             pcVUI->setSarWidth(uiCode);
699      READ_CODE(16, uiCode, "sar_height");                              pcVUI->setSarHeight(uiCode);
700    }
701  }
702
703  READ_FLAG(     uiCode, "overscan_info_present_flag");               pcVUI->setOverscanInfoPresentFlag(uiCode);
704  if (pcVUI->getOverscanInfoPresentFlag())
705  {
706    READ_FLAG(   uiCode, "overscan_appropriate_flag");                pcVUI->setOverscanAppropriateFlag(uiCode);
707  }
708
709  READ_FLAG(     uiCode, "video_signal_type_present_flag");           pcVUI->setVideoSignalTypePresentFlag(uiCode);
710#if NH_MV
711  assert( pcSPS->getLayerId() == 0 || !pcVUI->getVideoSignalTypePresentFlag() ); 
712#endif
713  if (pcVUI->getVideoSignalTypePresentFlag())
714  {
715    READ_CODE(3, uiCode, "video_format");                             pcVUI->setVideoFormat(uiCode);
716    READ_FLAG(   uiCode, "video_full_range_flag");                    pcVUI->setVideoFullRangeFlag(uiCode);
717    READ_FLAG(   uiCode, "colour_description_present_flag");          pcVUI->setColourDescriptionPresentFlag(uiCode);
718    if (pcVUI->getColourDescriptionPresentFlag())
719    {
720      READ_CODE(8, uiCode, "colour_primaries");                       pcVUI->setColourPrimaries(uiCode);
721      READ_CODE(8, uiCode, "transfer_characteristics");               pcVUI->setTransferCharacteristics(uiCode);
722      READ_CODE(8, uiCode, "matrix_coeffs");                          pcVUI->setMatrixCoefficients(uiCode);
723    }
724  }
725
726  READ_FLAG(     uiCode, "chroma_loc_info_present_flag");             pcVUI->setChromaLocInfoPresentFlag(uiCode);
727  if (pcVUI->getChromaLocInfoPresentFlag())
728  {
729    READ_UVLC(   uiCode, "chroma_sample_loc_type_top_field" );        pcVUI->setChromaSampleLocTypeTopField(uiCode);
730    READ_UVLC(   uiCode, "chroma_sample_loc_type_bottom_field" );     pcVUI->setChromaSampleLocTypeBottomField(uiCode);
731  }
732
733  READ_FLAG(     uiCode, "neutral_chroma_indication_flag");           pcVUI->setNeutralChromaIndicationFlag(uiCode);
734
735  READ_FLAG(     uiCode, "field_seq_flag");                           pcVUI->setFieldSeqFlag(uiCode);
736
737  READ_FLAG(uiCode, "frame_field_info_present_flag");                 pcVUI->setFrameFieldInfoPresentFlag(uiCode);
738
739  READ_FLAG(     uiCode, "default_display_window_flag");
740  if (uiCode != 0)
741  {
742    Window &defDisp = pcVUI->getDefaultDisplayWindow();
743#if NH_MV
744    defDisp.setScaledFlag( false ); 
745    READ_UVLC(   uiCode, "def_disp_win_left_offset" );                defDisp.setWindowLeftOffset  ( uiCode );
746    READ_UVLC(   uiCode, "def_disp_win_right_offset" );               defDisp.setWindowRightOffset ( uiCode );
747    READ_UVLC(   uiCode, "def_disp_win_top_offset" );                 defDisp.setWindowTopOffset   ( uiCode );
748    READ_UVLC(   uiCode, "def_disp_win_bottom_offset" );              defDisp.setWindowBottomOffset( uiCode );
749#else
750    READ_UVLC(   uiCode, "def_disp_win_left_offset" );                defDisp.setWindowLeftOffset  ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
751    READ_UVLC(   uiCode, "def_disp_win_right_offset" );               defDisp.setWindowRightOffset ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
752    READ_UVLC(   uiCode, "def_disp_win_top_offset" );                 defDisp.setWindowTopOffset   ( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
753    READ_UVLC(   uiCode, "def_disp_win_bottom_offset" );              defDisp.setWindowBottomOffset( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
754#endif
755  }
756
757  TimingInfo *timingInfo = pcVUI->getTimingInfo();
758  READ_FLAG(       uiCode, "vui_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
759  if(timingInfo->getTimingInfoPresentFlag())
760  {
761    READ_CODE( 32, uiCode, "vui_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
762    READ_CODE( 32, uiCode, "vui_time_scale");                       timingInfo->setTimeScale                  (uiCode);
763    READ_FLAG(     uiCode, "vui_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
764    if(timingInfo->getPocProportionalToTimingFlag())
765    {
766      READ_UVLC(   uiCode, "vui_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
767    }
768
769    READ_FLAG(     uiCode, "vui_hrd_parameters_present_flag");        pcVUI->setHrdParametersPresentFlag(uiCode);
770    if( pcVUI->getHrdParametersPresentFlag() )
771    {
772      parseHrdParameters( pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
773    }
774  }
775
776  READ_FLAG(     uiCode, "bitstream_restriction_flag");               pcVUI->setBitstreamRestrictionFlag(uiCode);
777  if (pcVUI->getBitstreamRestrictionFlag())
778  {
779    READ_FLAG(   uiCode, "tiles_fixed_structure_flag");               pcVUI->setTilesFixedStructureFlag(uiCode);
780    READ_FLAG(   uiCode, "motion_vectors_over_pic_boundaries_flag");  pcVUI->setMotionVectorsOverPicBoundariesFlag(uiCode);
781    READ_FLAG(   uiCode, "restricted_ref_pic_lists_flag");            pcVUI->setRestrictedRefPicListsFlag(uiCode);
782    READ_UVLC(   uiCode, "min_spatial_segmentation_idc");             pcVUI->setMinSpatialSegmentationIdc(uiCode);
783    assert(uiCode < 4096);
784    READ_UVLC(   uiCode, "max_bytes_per_pic_denom" );                 pcVUI->setMaxBytesPerPicDenom(uiCode);
785    READ_UVLC(   uiCode, "max_bits_per_min_cu_denom" );               pcVUI->setMaxBitsPerMinCuDenom(uiCode);
786    READ_UVLC(   uiCode, "log2_max_mv_length_horizontal" );           pcVUI->setLog2MaxMvLengthHorizontal(uiCode);
787    READ_UVLC(   uiCode, "log2_max_mv_length_vertical" );             pcVUI->setLog2MaxMvLengthVertical(uiCode);
788  }
789}
790
791Void TDecCavlc::parseHrdParameters(TComHRD *hrd, Bool commonInfPresentFlag, UInt maxNumSubLayersMinus1)
792{
793  UInt  uiCode;
794  if( commonInfPresentFlag )
795  {
796    READ_FLAG( uiCode, "nal_hrd_parameters_present_flag" );           hrd->setNalHrdParametersPresentFlag( uiCode == 1 ? true : false );
797    READ_FLAG( uiCode, "vcl_hrd_parameters_present_flag" );           hrd->setVclHrdParametersPresentFlag( uiCode == 1 ? true : false );
798    if( hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() )
799    {
800      READ_FLAG( uiCode, "sub_pic_hrd_params_present_flag" );         hrd->setSubPicCpbParamsPresentFlag( uiCode == 1 ? true : false );
801      if( hrd->getSubPicCpbParamsPresentFlag() )
802      {
803        READ_CODE( 8, uiCode, "tick_divisor_minus2" );                hrd->setTickDivisorMinus2( uiCode );
804        READ_CODE( 5, uiCode, "du_cpb_removal_delay_increment_length_minus1" ); hrd->setDuCpbRemovalDelayLengthMinus1( uiCode );
805        READ_FLAG( uiCode, "sub_pic_cpb_params_in_pic_timing_sei_flag" ); hrd->setSubPicCpbParamsInPicTimingSEIFlag( uiCode == 1 ? true : false );
806        READ_CODE( 5, uiCode, "dpb_output_delay_du_length_minus1"  ); hrd->setDpbOutputDelayDuLengthMinus1( uiCode );
807      }
808      READ_CODE( 4, uiCode, "bit_rate_scale" );                       hrd->setBitRateScale( uiCode );
809      READ_CODE( 4, uiCode, "cpb_size_scale" );                       hrd->setCpbSizeScale( uiCode );
810      if( hrd->getSubPicCpbParamsPresentFlag() )
811      {
812        READ_CODE( 4, uiCode, "cpb_size_du_scale" );                  hrd->setDuCpbSizeScale( uiCode );
813      }
814      READ_CODE( 5, uiCode, "initial_cpb_removal_delay_length_minus1" ); hrd->setInitialCpbRemovalDelayLengthMinus1( uiCode );
815      READ_CODE( 5, uiCode, "au_cpb_removal_delay_length_minus1" );      hrd->setCpbRemovalDelayLengthMinus1( uiCode );
816      READ_CODE( 5, uiCode, "dpb_output_delay_length_minus1" );       hrd->setDpbOutputDelayLengthMinus1( uiCode );
817    }
818  }
819  Int i, j, nalOrVcl;
820  for( i = 0; i <= maxNumSubLayersMinus1; i ++ )
821  {
822    READ_FLAG( uiCode, "fixed_pic_rate_general_flag" );                     hrd->setFixedPicRateFlag( i, uiCode == 1 ? true : false  );
823    if( !hrd->getFixedPicRateFlag( i ) )
824    {
825      READ_FLAG( uiCode, "fixed_pic_rate_within_cvs_flag" );                hrd->setFixedPicRateWithinCvsFlag( i, uiCode == 1 ? true : false  );
826    }
827    else
828    {
829      hrd->setFixedPicRateWithinCvsFlag( i, true );
830    }
831
832    hrd->setLowDelayHrdFlag( i, 0 ); // Infered to be 0 when not present
833    hrd->setCpbCntMinus1   ( i, 0 ); // Infered to be 0 when not present
834
835    if( hrd->getFixedPicRateWithinCvsFlag( i ) )
836    {
837      READ_UVLC( uiCode, "elemental_duration_in_tc_minus1" );             hrd->setPicDurationInTcMinus1( i, uiCode );
838    }
839    else
840    {
841      READ_FLAG( uiCode, "low_delay_hrd_flag" );                      hrd->setLowDelayHrdFlag( i, uiCode == 1 ? true : false  );
842    }
843    if (!hrd->getLowDelayHrdFlag( i ))
844    {
845      READ_UVLC( uiCode, "cpb_cnt_minus1" );                          hrd->setCpbCntMinus1( i, uiCode );
846    }
847
848    for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
849    {
850      if( ( ( nalOrVcl == 0 ) && ( hrd->getNalHrdParametersPresentFlag() ) ) ||
851          ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
852      {
853        for( j = 0; j <= ( hrd->getCpbCntMinus1( i ) ); j ++ )
854        {
855          READ_UVLC( uiCode, "bit_rate_value_minus1" );             hrd->setBitRateValueMinus1( i, j, nalOrVcl, uiCode );
856          READ_UVLC( uiCode, "cpb_size_value_minus1" );             hrd->setCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
857          if( hrd->getSubPicCpbParamsPresentFlag() )
858          {
859            READ_UVLC( uiCode, "cpb_size_du_value_minus1" );       hrd->setDuCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
860            READ_UVLC( uiCode, "bit_rate_du_value_minus1" );       hrd->setDuBitRateValueMinus1( i, j, nalOrVcl, uiCode );
861          }
862          READ_FLAG( uiCode, "cbr_flag" );                          hrd->setCbrFlag( i, j, nalOrVcl, uiCode == 1 ? true : false  );
863        }
864      }
865    }
866  }
867}
868
869Void TDecCavlc::parseSPS(TComSPS* pcSPS)
870{
871#if ENC_DEC_TRACE
872#if NH_MV_ENC_DEC_TRAC
873  tracePSHeader( "SPS", pcSPS->getLayerId() ); 
874#else
875  xTraceSPSHeader ();
876#endif
877#endif
878
879  UInt  uiCode;
880  READ_CODE( 4,  uiCode, "sps_video_parameter_set_id");          pcSPS->setVPSId        ( uiCode );
881
882#if NH_MV
883  if ( pcSPS->getLayerId() == 0 )
884  {
885#endif
886  READ_CODE_CHK( 3,  uiCode, "sps_max_sub_layers_minus1", 0, 6 );   pcSPS->setMaxTLayers   ( uiCode+1 );
887  assert(uiCode <= 6);
888#if NH_MV
889  }
890  else
891  {
892    READ_CODE( 3, uiCode, "sps_ext_or_max_sub_layers_minus1" ); pcSPS->setSpsExtOrMaxSubLayersMinus1( uiCode );   
893    pcSPS->inferSpsMaxSubLayersMinus1( false, NULL );
894  }
895  if ( !pcSPS->getMultiLayerExtSpsFlag() )
896  {
897#endif
898
899  READ_FLAG( uiCode, "sps_temporal_id_nesting_flag" );           pcSPS->setTemporalIdNestingFlag ( uiCode > 0 ? true : false );
900  if ( pcSPS->getMaxTLayers() == 1 )
901  {
902    // sps_temporal_id_nesting_flag must be 1 when sps_max_sub_layers_minus1 is 0
903    TDecConformanceCheck::checkRange(uiCode, "sps_temporal_id_nesting_flag", 1U, 1U);
904    assert( uiCode == 1 );
905  }
906
907  parsePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
908#if NH_MV
909    pcSPS->getPTL()->inferGeneralValues ( true, 0, NULL ); 
910    pcSPS->getPTL()->inferSubLayerValues( pcSPS->getSpsMaxSubLayersMinus1(), 0, NULL );
911  }
912#endif
913  READ_UVLC_CHK( uiCode, "sps_seq_parameter_set_id", 0, 15 );           pcSPS->setSPSId( uiCode );
914  assert(uiCode <= 15);
915#if NH_MV
916  if ( pcSPS->getMultiLayerExtSpsFlag() )
917  {
918    READ_FLAG( uiCode, "update_rep_format_flag" );               pcSPS->setUpdateRepFormatFlag( uiCode == 1 );
919    if ( pcSPS->getUpdateRepFormatFlag() )
920    { 
921      READ_CODE( 8, uiCode, "sps_rep_format_idx" );                pcSPS->setSpsRepFormatIdx( uiCode );
922    }
923  }
924  else
925  {
926#endif
927
928  READ_UVLC_CHK(     uiCode, "chroma_format_idc", 0, 3 );               pcSPS->setChromaFormatIdc( ChromaFormat(uiCode) );
929  assert(uiCode <= 3);
930  if( pcSPS->getChromaFormatIdc() == CHROMA_444 )
931  {
932    READ_FLAG_CHK(     uiCode, "separate_colour_plane_flag", 0, 0);
933    assert(uiCode == 0);
934  }
935
936  // pic_width_in_luma_samples and pic_height_in_luma_samples needs conformance checking - multiples of MinCbSizeY
937  READ_UVLC_CHK (    uiCode, "pic_width_in_luma_samples", 1, std::numeric_limits<UInt>::max()  );  pcSPS->setPicWidthInLumaSamples ( uiCode    );
938  READ_UVLC_CHK (    uiCode, "pic_height_in_luma_samples", 1, std::numeric_limits<UInt>::max() );  pcSPS->setPicHeightInLumaSamples( uiCode    );
939  READ_FLAG(     uiCode, "conformance_window_flag");
940  if (uiCode != 0)
941  {
942    Window &conf = pcSPS->getConformanceWindow();
943#if NH_MV
944    // Needs to be scaled later, when ChromaFormatIdc is known.
945    conf.setScaledFlag( false ); 
946    READ_UVLC(   uiCode, "conf_win_left_offset" );               conf.setWindowLeftOffset  ( uiCode  );
947    READ_UVLC(   uiCode, "conf_win_right_offset" );              conf.setWindowRightOffset ( uiCode  );
948    READ_UVLC(   uiCode, "conf_win_top_offset" );                conf.setWindowTopOffset   ( uiCode  );
949    READ_UVLC(   uiCode, "conf_win_bottom_offset" );             conf.setWindowBottomOffset( uiCode  );   
950  }
951#else
952    const UInt subWidthC  = TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc() );
953    const UInt subHeightC = TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc() );
954    READ_UVLC(   uiCode, "conf_win_left_offset" );               conf.setWindowLeftOffset  ( uiCode * subWidthC );
955    READ_UVLC(   uiCode, "conf_win_right_offset" );              conf.setWindowRightOffset ( uiCode * subWidthC );
956    READ_UVLC(   uiCode, "conf_win_top_offset" );                conf.setWindowTopOffset   ( uiCode * subHeightC );
957    READ_UVLC(   uiCode, "conf_win_bottom_offset" );             conf.setWindowBottomOffset( uiCode * subHeightC );
958    TDecConformanceCheck::checkRange<UInt>(conf.getWindowLeftOffset()+conf.getWindowRightOffset(), "conformance window width in pixels", 0, pcSPS->getPicWidthInLumaSamples()-1);
959#endif
960  }
961
962#if NH_MV
963  if ( !pcSPS->getMultiLayerExtSpsFlag() )
964  { 
965#endif
966
967  READ_UVLC_CHK(     uiCode, "bit_depth_luma_minus8", 0, 8 );
968  assert(uiCode <= 8);
969#if O0043_BEST_EFFORT_DECODING
970  pcSPS->setStreamBitDepth(CHANNEL_TYPE_LUMA, 8 + uiCode);
971  const UInt forceDecodeBitDepth = pcSPS->getForceDecodeBitDepth();
972  if (forceDecodeBitDepth != 0)
973  {
974    uiCode = forceDecodeBitDepth - 8;
975  }
976  assert(uiCode <= 8);
977#endif
978  pcSPS->setBitDepth(CHANNEL_TYPE_LUMA, 8 + uiCode);
979
980#if O0043_BEST_EFFORT_DECODING
981  pcSPS->setQpBDOffset(CHANNEL_TYPE_LUMA, (Int) (6*(pcSPS->getStreamBitDepth(CHANNEL_TYPE_LUMA)-8)) );
982#else
983  pcSPS->setQpBDOffset(CHANNEL_TYPE_LUMA, (Int) (6*uiCode) );
984#endif
985
986  READ_UVLC_CHK( uiCode,    "bit_depth_chroma_minus8", 0, 8 );
987  assert(uiCode <= 8);
988#if O0043_BEST_EFFORT_DECODING
989  pcSPS->setStreamBitDepth(CHANNEL_TYPE_CHROMA, 8 + uiCode);
990  if (forceDecodeBitDepth != 0)
991  {
992    uiCode = forceDecodeBitDepth - 8;
993  }
994  assert(uiCode <= 8);
995#endif
996  pcSPS->setBitDepth(CHANNEL_TYPE_CHROMA, 8 + uiCode);
997#if O0043_BEST_EFFORT_DECODING
998  pcSPS->setQpBDOffset(CHANNEL_TYPE_CHROMA,  (Int) (6*(pcSPS->getStreamBitDepth(CHANNEL_TYPE_CHROMA)-8)) );
999#else
1000  pcSPS->setQpBDOffset(CHANNEL_TYPE_CHROMA,  (Int) (6*uiCode) );
1001#endif
1002#if NH_MV
1003  }
1004#endif
1005
1006  READ_UVLC_CHK( uiCode,    "log2_max_pic_order_cnt_lsb_minus4", 0, 12 );   pcSPS->setBitsForPOC( 4 + uiCode );
1007
1008#if NH_MV
1009  if ( !pcSPS->getMultiLayerExtSpsFlag()) 
1010  { 
1011#endif
1012  UInt subLayerOrderingInfoPresentFlag;
1013  READ_FLAG(subLayerOrderingInfoPresentFlag, "sps_sub_layer_ordering_info_present_flag");
1014
1015  for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
1016  {
1017    READ_UVLC ( uiCode, "sps_max_dec_pic_buffering_minus1[i]");
1018    pcSPS->setMaxDecPicBuffering( uiCode + 1, i);
1019    READ_UVLC ( uiCode, "sps_max_num_reorder_pics[i]" );
1020    pcSPS->setNumReorderPics(uiCode, i);
1021    READ_UVLC ( uiCode, "sps_max_latency_increase_plus1[i]");
1022#if NH_MV
1023    pcSPS->setSpsMaxLatencyIncreasePlus1( uiCode, i );
1024#else
1025    pcSPS->setMaxLatencyIncreasePlus1( uiCode, i );
1026#endif
1027
1028    if (!subLayerOrderingInfoPresentFlag)
1029    {
1030      for (i++; i <= pcSPS->getMaxTLayers()-1; i++)
1031      {
1032        pcSPS->setMaxDecPicBuffering(pcSPS->getMaxDecPicBuffering(0), i);
1033        pcSPS->setNumReorderPics(pcSPS->getNumReorderPics(0), i);
1034#if NH_MV
1035        pcSPS->setSpsMaxLatencyIncreasePlus1(pcSPS->getSpsMaxLatencyIncreasePlus1(0), i);
1036#else
1037        pcSPS->setMaxLatencyIncreasePlus1(pcSPS->getMaxLatencyIncreasePlus1(0), i);
1038#endif
1039      }
1040      break;
1041    }
1042  }
1043#if NH_MV
1044  }
1045#endif
1046
1047  const UInt maxLog2CtbSize = TDecConformanceCheck::getMaxLog2CtbSize(*(pcSPS->getPTL()));
1048  const UInt minLog2CtbSize = TDecConformanceCheck::getMinLog2CtbSize(*(pcSPS->getPTL()));
1049  READ_UVLC_CHK( uiCode, "log2_min_luma_coding_block_size_minus3", 0, maxLog2CtbSize-3 );
1050  assert(uiCode <= maxLog2CtbSize-3);
1051  Int minCbLog2SizeY = uiCode + 3;
1052  pcSPS->setLog2MinCodingBlockSize(minCbLog2SizeY);
1053 
1054    // Difference + log2MinCUSize must be <= maxLog2CtbSize
1055  // Difference + log2MinCUSize must be >= minLog2CtbSize
1056  const UInt minLog2DiffMaxMinLumaCodingBlockSize = minLog2CtbSize < minCbLog2SizeY ? 0 : minLog2CtbSize - minCbLog2SizeY;
1057  const UInt maxLog2DiffMaxMinLumaCodingBlockSize = maxLog2CtbSize - minCbLog2SizeY;
1058
1059  READ_UVLC_CHK( uiCode, "log2_diff_max_min_luma_coding_block_size", minLog2DiffMaxMinLumaCodingBlockSize, maxLog2DiffMaxMinLumaCodingBlockSize);
1060  assert(uiCode >= minLog2DiffMaxMinLumaCodingBlockSize && uiCode <= maxLog2DiffMaxMinLumaCodingBlockSize);
1061  pcSPS->setLog2DiffMaxMinCodingBlockSize(uiCode);
1062
1063  const Int maxCUDepthDelta = uiCode;
1064  const Int ctbLog2SizeY = minCbLog2SizeY + maxCUDepthDelta;
1065  pcSPS->setMaxCUWidth  ( 1<<ctbLog2SizeY );
1066  pcSPS->setMaxCUHeight ( 1<<ctbLog2SizeY );
1067  READ_UVLC_CHK( uiCode, "log2_min_luma_transform_block_size_minus2", 0, minCbLog2SizeY-1-2 );
1068  const UInt minTbLog2SizeY = uiCode + 2;
1069  pcSPS->setQuadtreeTULog2MinSize( minTbLog2SizeY );
1070
1071  //  log2_diff <= Min(CtbLog2SizeY, 5) - minTbLog2SizeY
1072  READ_UVLC_CHK( uiCode, "log2_diff_max_min_luma_transform_block_size", 0, min<UInt>(5U, ctbLog2SizeY) - minTbLog2SizeY );
1073  pcSPS->setQuadtreeTULog2MaxSize( uiCode + pcSPS->getQuadtreeTULog2MinSize() );
1074  pcSPS->setMaxTrSize( 1<<(uiCode + pcSPS->getQuadtreeTULog2MinSize()) );
1075
1076  READ_UVLC_CHK( uiCode, "max_transform_hierarchy_depth_inter", 0, ctbLog2SizeY - minTbLog2SizeY);    pcSPS->setQuadtreeTUMaxDepthInter( uiCode+1 );
1077  READ_UVLC_CHK( uiCode, "max_transform_hierarchy_depth_intra", 0, ctbLog2SizeY - minTbLog2SizeY);    pcSPS->setQuadtreeTUMaxDepthIntra( uiCode+1 );
1078
1079  Int addCuDepth = max (0, minCbLog2SizeY - (Int)pcSPS->getQuadtreeTULog2MinSize() );
1080  pcSPS->setMaxTotalCUDepth( maxCUDepthDelta + addCuDepth  + getMaxCUDepthOffset(pcSPS->getChromaFormatIdc(), pcSPS->getQuadtreeTULog2MinSize()) );
1081
1082  READ_FLAG( uiCode, "scaling_list_enabled_flag" );                 pcSPS->setScalingListFlag ( uiCode );
1083  if(pcSPS->getScalingListFlag())
1084  {
1085#if NH_MV
1086    if ( pcSPS->getMultiLayerExtSpsFlag() )
1087    {   
1088      READ_FLAG( uiCode, "sps_infer_scaling_list_flag" ); pcSPS->setSpsInferScalingListFlag( uiCode == 1 );
1089    }
1090
1091    if ( pcSPS->getSpsInferScalingListFlag() )
1092    {
1093      READ_CODE( 6, uiCode, "sps_scaling_list_ref_layer_id" ); pcSPS->setSpsScalingListRefLayerId( uiCode );
1094    }
1095    else
1096    {   
1097#endif
1098    READ_FLAG( uiCode, "sps_scaling_list_data_present_flag" );                 pcSPS->setScalingListPresentFlag ( uiCode );
1099    if(pcSPS->getScalingListPresentFlag ())
1100    {
1101      parseScalingList( &(pcSPS->getScalingList()) );
1102    }
1103#if NH_MV
1104    }
1105#endif
1106  }
1107  READ_FLAG( uiCode, "amp_enabled_flag" );                          pcSPS->setUseAMP( uiCode );
1108  READ_FLAG( uiCode, "sample_adaptive_offset_enabled_flag" );       pcSPS->setUseSAO ( uiCode ? true : false );
1109
1110  READ_FLAG( uiCode, "pcm_enabled_flag" ); pcSPS->setUsePCM( uiCode ? true : false );
1111  if( pcSPS->getUsePCM() )
1112  {
1113#if O0043_BEST_EFFORT_DECODING
1114    READ_CODE_CHK( 4, uiCode, "pcm_sample_bit_depth_luma_minus1",   0, pcSPS->getStreamBitDepth(CHANNEL_TYPE_LUMA) );    pcSPS->setPCMBitDepth    ( CHANNEL_TYPE_LUMA, 1 + uiCode );
1115    READ_CODE_CHK( 4, uiCode, "pcm_sample_bit_depth_chroma_minus1", 0, pcSPS->getStreamBitDepth(CHANNEL_TYPE_LUMA) );    pcSPS->setPCMBitDepth    ( CHANNEL_TYPE_CHROMA, 1 + uiCode );
1116#else
1117    READ_CODE_CHK( 4, uiCode, "pcm_sample_bit_depth_luma_minus1",   0, pcSPS->getBitDepth(CHANNEL_TYPE_LUMA) );          pcSPS->setPCMBitDepth    ( CHANNEL_TYPE_LUMA, 1 + uiCode );
1118    READ_CODE_CHK( 4, uiCode, "pcm_sample_bit_depth_chroma_minus1", 0, pcSPS->getBitDepth(CHANNEL_TYPE_CHROMA) );        pcSPS->setPCMBitDepth    ( CHANNEL_TYPE_CHROMA, 1 + uiCode );
1119#endif
1120    READ_UVLC_CHK( uiCode, "log2_min_pcm_luma_coding_block_size_minus3", std::min<UInt>(minCbLog2SizeY, 5 )-3, std::min<UInt>(ctbLog2SizeY, 5)-3);
1121    const UInt log2MinIpcmCbSizeY = uiCode+3;
1122    pcSPS->setPCMLog2MinSize (log2MinIpcmCbSizeY);
1123    READ_UVLC_CHK( uiCode, "log2_diff_max_min_pcm_luma_coding_block_size", 0, (std::min<UInt>(ctbLog2SizeY,5) - log2MinIpcmCbSizeY) );
1124    pcSPS->setPCMLog2MaxSize ( uiCode+pcSPS->getPCMLog2MinSize() );
1125    READ_FLAG( uiCode, "pcm_loop_filter_disable_flag" );                 pcSPS->setPCMFilterDisableFlag ( uiCode ? true : false );
1126  }
1127
1128  READ_UVLC_CHK( uiCode, "num_short_term_ref_pic_sets", 0, 64 );
1129
1130  assert(uiCode <= 64);
1131#if NH_MV
1132  pcSPS->setNumShortTermRefPicSets( uiCode ); 
1133  pcSPS->initStRefPicSets();
1134
1135  for(Int i=0; i< pcSPS->getNumShortTermRefPicSets(); i++ )
1136  {
1137    parseShortTermRefPicSet(pcSPS,pcSPS->getStRefPicSet(i),i);
1138  }
1139
1140  READ_FLAG( uiCode, "long_term_ref_pics_present_flag" );          pcSPS->setLongTermRefPicsPresentFlag(uiCode);
1141  if (pcSPS->getLongTermRefPicsPresentFlag() )
1142#else
1143  pcSPS->createRPSList(uiCode);
1144
1145  TComRPSList* rpsList = pcSPS->getRPSList();
1146  TComReferencePictureSet* rps;
1147
1148  for(UInt i=0; i< rpsList->getNumberOfReferencePictureSets(); i++)
1149  {
1150    rps = rpsList->getReferencePictureSet(i);
1151    parseShortTermRefPicSet(pcSPS,rps,i);
1152  }
1153
1154  READ_FLAG( uiCode, "long_term_ref_pics_present_flag" );          pcSPS->setLongTermRefsPresent(uiCode);
1155  if (pcSPS->getLongTermRefsPresent())
1156#endif
1157  {
1158    READ_UVLC_CHK( uiCode, "num_long_term_ref_pics_sps", 0, 32 );
1159    pcSPS->setNumLongTermRefPicSPS(uiCode);
1160    for (UInt k = 0; k < pcSPS->getNumLongTermRefPicSPS(); k++)
1161    {
1162      READ_CODE( pcSPS->getBitsForPOC(), uiCode, "lt_ref_pic_poc_lsb_sps" );
1163      pcSPS->setLtRefPicPocLsbSps(k, uiCode);
1164      READ_FLAG( uiCode,  "used_by_curr_pic_lt_sps_flag[i]");
1165      pcSPS->setUsedByCurrPicLtSPSFlag(k, uiCode?1:0);
1166    }
1167  }
1168  READ_FLAG( uiCode, "sps_temporal_mvp_enabled_flag" );           pcSPS->setSPSTemporalMVPEnabledFlag(uiCode);
1169
1170  READ_FLAG( uiCode, "strong_intra_smoothing_enable_flag" );      pcSPS->setUseStrongIntraSmoothing(uiCode);
1171
1172  READ_FLAG( uiCode, "vui_parameters_present_flag" );             pcSPS->setVuiParametersPresentFlag(uiCode);
1173
1174  if (pcSPS->getVuiParametersPresentFlag())
1175  {
1176    parseVUI(pcSPS->getVuiParameters(), pcSPS);
1177  }
1178
1179  READ_FLAG( uiCode, "sps_extension_present_flag");
1180#if NH_MV
1181  pcSPS->setSpsExtensionPresentFlag( uiCode ); 
1182  if (pcSPS->getSpsExtensionPresentFlag( ) )
1183  { 
1184    READ_FLAG( uiCode, "sps_range_extensions_flag" );     pcSPS->setSpsRangeExtensionsFlag( uiCode == 1 );
1185    READ_FLAG( uiCode, "sps_multilayer_extension_flag" ); pcSPS->setSpsMultilayerExtensionFlag( uiCode == 1 );
1186    READ_FLAG( uiCode   , "sps_3d_extension_flag" );      pcSPS->setSps3dExtensionFlag( uiCode == 1 );
1187    READ_CODE( 5, uiCode, "sps_extension_5bits" )  ;      pcSPS->setSpsExtension5bits( uiCode );
1188
1189    if ( pcSPS->getSpsRangeExtensionsFlag() )
1190    {
1191      TComSPSRExt &spsRangeExtension = pcSPS->getSpsRangeExtension();
1192      READ_FLAG( uiCode, "transform_skip_rotation_enabled_flag");     spsRangeExtension.setTransformSkipRotationEnabledFlag(uiCode != 0);
1193      READ_FLAG( uiCode, "transform_skip_context_enabled_flag");      spsRangeExtension.setTransformSkipContextEnabledFlag (uiCode != 0);
1194      READ_FLAG( uiCode, "implicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_IMPLICIT, (uiCode != 0));
1195      READ_FLAG( uiCode, "explicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_EXPLICIT, (uiCode != 0));
1196      READ_FLAG( uiCode, "extended_precision_processing_flag");       spsRangeExtension.setExtendedPrecisionProcessingFlag (uiCode != 0);
1197      READ_FLAG( uiCode, "intra_smoothing_disabled_flag");            spsRangeExtension.setIntraSmoothingDisabledFlag      (uiCode != 0);
1198      READ_FLAG( uiCode, "high_precision_offsets_enabled_flag");      spsRangeExtension.setHighPrecisionOffsetsEnabledFlag (uiCode != 0);
1199      READ_FLAG( uiCode, "persistent_rice_adaptation_enabled_flag");  spsRangeExtension.setPersistentRiceAdaptationEnabledFlag (uiCode != 0);
1200      READ_FLAG( uiCode, "cabac_bypass_alignment_enabled_flag");      spsRangeExtension.setCabacBypassAlignmentEnabledFlag  (uiCode != 0);
1201    }
1202
1203    if ( pcSPS->getSpsMultilayerExtensionFlag() )
1204    {
1205      parseSpsMultilayerExtension( pcSPS ); 
1206    }
1207
1208    if ( pcSPS->getSps3dExtensionFlag() )
1209    {
1210#if NH_3D
1211      parseSps3dExtension( pcSPS ); 
1212#endif
1213    }
1214
1215#if NH_3D
1216    if ( pcSPS->getSpsExtension5bits() )
1217#else
1218    if ( pcSPS->getSpsExtension5bits() || pcSPS->getSps3dExtensionFlag() )
1219#endif
1220    { 
1221      while ( xMoreRbspData() )
1222      {
1223        READ_FLAG( uiCode, "sps_extension_data_flag");
1224      }
1225    }
1226  }
1227#else
1228  if (uiCode)
1229  {
1230
1231#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
1232    static const TChar *syntaxStrings[]={ "sps_range_extension_flag",
1233                                         "sps_multilayer_extension_flag",
1234                                         "sps_extension_6bits[0]",
1235                                         "sps_extension_6bits[1]",
1236                                         "sps_extension_6bits[2]",
1237                                         "sps_extension_6bits[3]",
1238                                         "sps_extension_6bits[4]",
1239                                         "sps_extension_6bits[5]" };
1240#endif
1241    Bool sps_extension_flags[NUM_SPS_EXTENSION_FLAGS];
1242
1243    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
1244    {
1245      READ_FLAG( uiCode, syntaxStrings[i] );
1246      sps_extension_flags[i] = uiCode!=0;
1247    }
1248
1249    Bool bSkipTrailingExtensionBits=false;
1250    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
1251    {
1252      if (sps_extension_flags[i])
1253      {
1254        switch (SPSExtensionFlagIndex(i))
1255        {
1256          case SPS_EXT__REXT:
1257            assert(!bSkipTrailingExtensionBits);
1258            {
1259              TComSPSRExt &spsRangeExtension = pcSPS->getSpsRangeExtension();
1260              READ_FLAG( uiCode, "transform_skip_rotation_enabled_flag");     spsRangeExtension.setTransformSkipRotationEnabledFlag(uiCode != 0);
1261              READ_FLAG( uiCode, "transform_skip_context_enabled_flag");      spsRangeExtension.setTransformSkipContextEnabledFlag (uiCode != 0);
1262              READ_FLAG( uiCode, "implicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_IMPLICIT, (uiCode != 0));
1263              READ_FLAG( uiCode, "explicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_EXPLICIT, (uiCode != 0));
1264              READ_FLAG( uiCode, "extended_precision_processing_flag");       spsRangeExtension.setExtendedPrecisionProcessingFlag (uiCode != 0);
1265              READ_FLAG( uiCode, "intra_smoothing_disabled_flag");            spsRangeExtension.setIntraSmoothingDisabledFlag      (uiCode != 0);
1266              READ_FLAG( uiCode, "high_precision_offsets_enabled_flag");      spsRangeExtension.setHighPrecisionOffsetsEnabledFlag (uiCode != 0);
1267              READ_FLAG( uiCode, "persistent_rice_adaptation_enabled_flag");  spsRangeExtension.setPersistentRiceAdaptationEnabledFlag (uiCode != 0);
1268              READ_FLAG( uiCode, "cabac_bypass_alignment_enabled_flag");      spsRangeExtension.setCabacBypassAlignmentEnabledFlag  (uiCode != 0);
1269            }
1270            break;
1271          default:
1272            bSkipTrailingExtensionBits=true;
1273            break;
1274        }
1275      }
1276    }
1277    if (bSkipTrailingExtensionBits)
1278    {
1279      while ( xMoreRbspData() )
1280      {
1281        READ_FLAG( uiCode, "sps_extension_data_flag");
1282      }
1283    }
1284  }
1285#endif
1286  xReadRbspTrailingBits();
1287}
1288
1289#if NH_MV
1290Void TDecCavlc::parseSpsMultilayerExtension( TComSPS* pcSPS )
1291{
1292  UInt uiCode; 
1293  READ_FLAG( uiCode, "inter_view_mv_vert_constraint_flag" );    pcSPS->setInterViewMvVertConstraintFlag(uiCode == 1 ? true : false); 
1294}
1295
1296#if NH_3D
1297Void TDecCavlc::parseSps3dExtension( TComSPS* pcSPS )
1298{ 
1299  TComSps3dExtension sps3dExt; 
1300  UInt uiCode; 
1301  for( Int d = 0; d  <=  1; d++ )
1302  {
1303    READ_FLAG( uiCode, "iv_di_mc_enabled_flag" );              sps3dExt.setIvDiMcEnabledFlag( d, uiCode == 1 );
1304    READ_FLAG( uiCode, "iv_mv_scal_enabled_flag" );            sps3dExt.setIvMvScalEnabledFlag( d, uiCode == 1 );
1305    if( d  ==  0 )
1306    {
1307      READ_UVLC( uiCode, "log2_sub_pb_size_minus3" );          sps3dExt.setLog2IvmcSubPbSizeMinus3( d, uiCode );
1308      READ_FLAG( uiCode, "iv_res_pred_enabled_flag" );         sps3dExt.setIvResPredEnabledFlag( d, uiCode == 1 );
1309      READ_FLAG( uiCode, "depth_ref_enabled_flag" );           sps3dExt.setDepthRefEnabledFlag( d, uiCode == 1 );
1310      READ_FLAG( uiCode, "vsp_mc_enabled_flag" );              sps3dExt.setVspMcEnabledFlag( d, uiCode == 1 );
1311      READ_FLAG( uiCode, "dbbp_enabled_flag" );                sps3dExt.setDbbpEnabledFlag( d, uiCode == 1 );
1312    }
1313    else 
1314    {
1315      READ_FLAG( uiCode, "tex_mc_enabled_flag" );              sps3dExt.setTexMcEnabledFlag( d, uiCode == 1 );
1316      READ_UVLC( uiCode, "log2_texmc_sub_pb_size_minus3" );    sps3dExt.setLog2TexmcSubPbSizeMinus3( d, uiCode );
1317      READ_FLAG( uiCode, "intra_contour_enabled_flag" );       sps3dExt.setIntraContourEnabledFlag( d, uiCode == 1 );
1318      READ_FLAG( uiCode, "intra_dc_only_wedge_enabled_flag" ); sps3dExt.setIntraDcOnlyWedgeEnabledFlag( d, uiCode == 1 );
1319      READ_FLAG( uiCode, "cqt_cu_part_pred_enabled_flag" );    sps3dExt.setCqtCuPartPredEnabledFlag( d, uiCode == 1 );
1320      READ_FLAG( uiCode, "inter_dc_only_enabled_flag" );       sps3dExt.setInterDcOnlyEnabledFlag( d, uiCode == 1 );
1321      READ_FLAG( uiCode, "skip_intra_enabled_flag" );          sps3dExt.setSkipIntraEnabledFlag( d, uiCode == 1 );
1322    }
1323  }
1324  pcSPS->setSps3dExtension( sps3dExt ); 
1325}
1326#endif
1327
1328Void TDecCavlc::parsePpsMultilayerExtension(TComPPS* pcPPS)
1329{
1330  UInt uiCode = 0; 
1331  READ_FLAG( uiCode, "poc_reset_info_present_flag" ); pcPPS->setPocResetInfoPresentFlag( uiCode == 1 );
1332  READ_FLAG( uiCode, "pps_infer_scaling_list_flag" ); pcPPS->setPpsInferScalingListFlag( uiCode == 1 );
1333  if (pcPPS->getPpsInferScalingListFlag())
1334  {
1335  READ_CODE( 6, uiCode, "pps_scaling_list_ref_layer_id" ); pcPPS->setPpsScalingListRefLayerId( uiCode );
1336  }
1337
1338  UInt numRefLocOffsets;; 
1339  READ_UVLC( numRefLocOffsets, "num_ref_loc_offsets" );
1340
1341  // All of the following stuff is not needed, but allowed to be present.
1342  for (Int i = 0; i < numRefLocOffsets; i++ )
1343  { 
1344    Int   iCode = 0; 
1345    READ_CODE( 6, uiCode, "ref_loc_offset_layer_id" ); 
1346    READ_FLAG( uiCode, "scaled_ref_layer_offset_present_flag" );
1347
1348    if (uiCode)
1349    {   
1350      READ_SVLC( iCode, "scaled_ref_layer_left_offset" );   
1351      READ_SVLC( iCode, "scaled_ref_layer_top_offset" );   
1352      READ_SVLC( iCode, "scaled_ref_layer_right_offset" ); 
1353      READ_SVLC( iCode, "scaled_ref_layer_bottom_offset" ); 
1354    }
1355
1356    READ_FLAG( uiCode, "ref_region_offset_present_flag" ); 
1357    if (uiCode)
1358    {   
1359      READ_SVLC( iCode, "ref_region_left_offset" );     
1360      READ_SVLC( iCode, "ref_region_top_offset" );     
1361      READ_SVLC( iCode, "ref_region_right_offset" );   
1362      READ_SVLC( iCode, "ref_region_bottom_offset" );   
1363    }
1364
1365    READ_FLAG( uiCode, "resample_phase_set_present_flag" );
1366    if (uiCode)
1367    {     
1368      READ_UVLC( uiCode, "phase_hor_luma" );             
1369      READ_UVLC( uiCode, "phase_ver_luma" );             
1370      READ_UVLC( uiCode, "phase_hor_chroma_plus8" );     
1371      READ_UVLC( uiCode, "phase_ver_chroma_plus8" );     
1372    }
1373  }
1374  READ_FLAG( uiCode, "colour_mapping_enabled_flag" );   
1375  // This is required to equal to 0 for Multiview Main profile.
1376  assert( uiCode == 0 ); 
1377}
1378
1379#endif
1380
1381Void TDecCavlc::parseVPS(TComVPS* pcVPS)
1382{
1383#if ENC_DEC_TRACE
1384#if NH_MV_ENC_DEC_TRAC
1385  tracePSHeader( "VPS", getDecTop()->getLayerId() ); 
1386#else
1387  xTraceVPSHeader ();
1388#endif
1389#endif
1390
1391  UInt  uiCode;
1392
1393  READ_CODE( 4,  uiCode,  "vps_video_parameter_set_id" );         pcVPS->setVPSId( uiCode );
1394#if NH_MV
1395  READ_FLAG( uiCode, "vps_base_layer_internal_flag" );            pcVPS->setVpsBaseLayerInternalFlag( uiCode == 1 );
1396  READ_FLAG( uiCode, "vps_base_layer_available_flag" );           pcVPS->setVpsBaseLayerAvailableFlag( uiCode == 1 );
1397#else
1398  READ_FLAG( uiCode,      "vps_base_layer_internal_flag" );       assert(uiCode == 1);
1399  READ_FLAG( uiCode,      "vps_base_layer_available_flag" );      assert(uiCode == 1);
1400#endif
1401#if NH_MV
1402  READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );              pcVPS->setMaxLayersMinus1( std::min( uiCode, (UInt) ( MAX_NUM_LAYER_IDS-1) )  );
1403#else
1404  READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );
1405#endif
1406  READ_CODE( 3,  uiCode,  "vps_max_sub_layers_minus1" );          pcVPS->setMaxTLayers( uiCode + 1 );    assert(uiCode+1 <= MAX_TLAYER);
1407  READ_FLAG(     uiCode,  "vps_temporal_id_nesting_flag" );       pcVPS->setTemporalNestingFlag( uiCode ? true:false );
1408  assert (pcVPS->getMaxTLayers()>1||pcVPS->getTemporalNestingFlag());
1409  READ_CODE( 16, uiCode,  "vps_reserved_0xffff_16bits" );         assert(uiCode == 0xffff);
1410  parsePTL ( pcVPS->getPTL(), true, pcVPS->getMaxTLayers()-1);
1411#if NH_MV
1412  pcVPS->getPTL()->inferGeneralValues ( true, 0, NULL );
1413  pcVPS->getPTL()->inferSubLayerValues( pcVPS->getMaxTLayers() - 1, 0, NULL );
1414#endif
1415  UInt subLayerOrderingInfoPresentFlag;
1416  READ_FLAG(subLayerOrderingInfoPresentFlag, "vps_sub_layer_ordering_info_present_flag");
1417  for(UInt i = 0; i <= pcVPS->getMaxTLayers()-1; i++)
1418  {
1419    READ_UVLC( uiCode,  "vps_max_dec_pic_buffering_minus1[i]" );    pcVPS->setMaxDecPicBuffering( uiCode + 1, i );
1420    READ_UVLC( uiCode,  "vps_max_num_reorder_pics[i]" );            pcVPS->setNumReorderPics( uiCode, i );
1421    READ_UVLC( uiCode,  "vps_max_latency_increase_plus1[i]" );      pcVPS->setMaxLatencyIncrease( uiCode, i );
1422
1423    if (!subLayerOrderingInfoPresentFlag)
1424    {
1425      for (i++; i <= pcVPS->getMaxTLayers()-1; i++)
1426      {
1427        pcVPS->setMaxDecPicBuffering(pcVPS->getMaxDecPicBuffering(0), i);
1428        pcVPS->setNumReorderPics(pcVPS->getNumReorderPics(0), i);
1429        pcVPS->setMaxLatencyIncrease(pcVPS->getMaxLatencyIncrease(0), i);
1430      }
1431      break;
1432    }
1433  }
1434
1435  assert( pcVPS->getNumHrdParameters() < MAX_VPS_OP_SETS_PLUS1 );
1436#if NH_MV
1437  assert( pcVPS->getVpsMaxLayerId() < MAX_VPS_NUH_LAYER_ID_PLUS1 );
1438  READ_CODE( 6, uiCode, "vps_max_layer_id" );   pcVPS->setVpsMaxLayerId( uiCode );
1439
1440  READ_UVLC(    uiCode, "vps_num_layer_sets_minus1" );  pcVPS->setVpsNumLayerSetsMinus1( uiCode );
1441  for( UInt opsIdx = 1; opsIdx <= pcVPS->getVpsNumLayerSetsMinus1(); opsIdx ++ )
1442  {
1443    for( UInt i = 0; i <= pcVPS->getVpsMaxLayerId(); i ++ )
1444#else
1445  assert( pcVPS->getMaxNuhReservedZeroLayerId() < MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1 );
1446  READ_CODE( 6, uiCode, "vps_max_layer_id" );                        pcVPS->setMaxNuhReservedZeroLayerId( uiCode );
1447  READ_UVLC(    uiCode, "vps_num_layer_sets_minus1" );               pcVPS->setMaxOpSets( uiCode + 1 );
1448  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getMaxOpSets() - 1 ); opsIdx ++ )
1449  {
1450    // Operation point set
1451    for( UInt i = 0; i <= pcVPS->getMaxNuhReservedZeroLayerId(); i ++ )
1452#endif
1453    {
1454      READ_FLAG( uiCode, "layer_id_included_flag[opsIdx][i]" );   pcVPS->setLayerIdIncludedFlag( uiCode == 1 ? true : false, opsIdx, i );
1455    }
1456  }
1457#if NH_MV
1458  pcVPS->deriveLayerSetLayerIdList(); 
1459#endif
1460
1461  TimingInfo *timingInfo = pcVPS->getTimingInfo();
1462  READ_FLAG(       uiCode, "vps_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
1463  if(timingInfo->getTimingInfoPresentFlag())
1464  {
1465    READ_CODE( 32, uiCode, "vps_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
1466    READ_CODE( 32, uiCode, "vps_time_scale");                       timingInfo->setTimeScale                  (uiCode);
1467    READ_FLAG(     uiCode, "vps_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
1468    if(timingInfo->getPocProportionalToTimingFlag())
1469    {
1470      READ_UVLC(   uiCode, "vps_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
1471    }
1472
1473    READ_UVLC( uiCode, "vps_num_hrd_parameters" );                  pcVPS->setNumHrdParameters( uiCode );
1474
1475    if( pcVPS->getNumHrdParameters() > 0 )
1476    {
1477      pcVPS->createHrdParamBuffer();
1478    }
1479    for( UInt i = 0; i < pcVPS->getNumHrdParameters(); i ++ )
1480    {
1481      READ_UVLC( uiCode, "hrd_layer_set_idx[i]" );                  pcVPS->setHrdOpSetIdx( uiCode, i );
1482      if( i > 0 )
1483      {
1484        READ_FLAG( uiCode, "cprms_present_flag[i]" );               pcVPS->setCprmsPresentFlag( uiCode == 1 ? true : false, i );
1485      }
1486      else
1487      {
1488        pcVPS->setCprmsPresentFlag( true, i );
1489      }
1490
1491      parseHrdParameters(pcVPS->getHrdParameters(i), pcVPS->getCprmsPresentFlag( i ), pcVPS->getMaxTLayers() - 1);
1492    }
1493  }
1494#if NH_MV
1495  READ_FLAG( uiCode,  "vps_extension_flag" );                      pcVPS->setVpsExtensionFlag( uiCode == 1 ? true : false );
1496  if ( pcVPS->getVpsExtensionFlag() )
1497#else
1498  READ_FLAG( uiCode,  "vps_extension_flag" );
1499  if (uiCode)
1500#endif
1501  {
1502#if NH_MV
1503    m_pcBitstream->readOutTrailingBits();
1504    parseVPSExtension( pcVPS );   
1505    READ_FLAG( uiCode,  "vps_extension2_flag" );
1506    if (uiCode)
1507    {
1508#if NH_3D
1509      READ_FLAG( uiCode,  "vps_3d_extension_flag" );
1510      if ( uiCode )
1511      {
1512        m_pcBitstream->readOutTrailingBits();
1513        pcVPS->createCamPars(pcVPS->getNumViews());
1514        parseVps3dExtension( pcVPS );   
1515      }
1516      READ_FLAG( uiCode,  "vps_extension3_flag" );
1517      if (uiCode)
1518      {     
1519#endif
1520#endif 
1521        while ( xMoreRbspData() )
1522        {
1523          READ_FLAG( uiCode, "vps_extension_data_flag");
1524        }
1525#if NH_MV
1526#if NH_3D
1527      }
1528#endif
1529    }
1530#endif
1531  }
1532
1533  xReadRbspTrailingBits();
1534}
1535
1536#if NH_MV
1537Void TDecCavlc::parseVPSExtension( TComVPS* pcVPS )
1538{
1539  UInt uiCode; 
1540
1541  if( pcVPS->getMaxLayersMinus1() > 0  &&  pcVPS->getVpsBaseLayerInternalFlag() )
1542  {
1543    parsePTL( pcVPS->getPTL( 1 ),0, pcVPS->getMaxSubLayersMinus1()  ); 
1544   
1545    pcVPS->getPTL( 1 )->inferGeneralValues ( false, 1, pcVPS->getPTL( 0 ) );
1546    pcVPS->getPTL( 1 )->inferSubLayerValues( pcVPS->getMaxSubLayersMinus1(), 1, pcVPS->getPTL( 0 ) );   
1547  }
1548
1549  READ_FLAG( uiCode, "splitting_flag" );                          pcVPS->setSplittingFlag( uiCode == 1 ? true : false );
1550
1551  for( Int sIdx = 0; sIdx < MAX_NUM_SCALABILITY_TYPES; sIdx++ )
1552  {
1553    READ_FLAG( uiCode,  "scalability_mask_flag[i]" );             pcVPS->setScalabilityMaskFlag( sIdx, uiCode == 1 ? true : false );     
1554  }
1555
1556  for( Int sIdx = 0; sIdx < pcVPS->getNumScalabilityTypes( ) - ( pcVPS->getSplittingFlag() ? 1 : 0 ); sIdx++ )
1557  {
1558    READ_CODE( 3, uiCode, "dimension_id_len_minus1[j]" );       pcVPS->setDimensionIdLen( sIdx, uiCode + 1 );
1559  }
1560
1561  if ( pcVPS->getSplittingFlag() )
1562  {
1563    pcVPS->setDimensionIdLen( pcVPS->getNumScalabilityTypes( ) - 1, pcVPS->inferLastDimsionIdLenMinus1() );       
1564  }
1565
1566  READ_FLAG( uiCode, "vps_nuh_layer_id_present_flag" );           pcVPS->setVpsNuhLayerIdPresentFlag( uiCode == 1 ? true : false );
1567
1568  for( Int i = 1; i <= pcVPS->getMaxLayersMinus1(); i++ )
1569  {
1570    if ( pcVPS->getVpsNuhLayerIdPresentFlag() )
1571    {
1572      READ_CODE( 6, uiCode, "layer_id_in_nuh[i]" );                pcVPS->setLayerIdInNuh( i, uiCode );
1573    }
1574    else
1575    {
1576      pcVPS->setLayerIdInNuh( i, i );; 
1577    }
1578
1579    pcVPS->setLayerIdInVps( pcVPS->getLayerIdInNuh( i ), i ); 
1580
1581    for( Int j = 0; j < pcVPS->getNumScalabilityTypes() ; j++ ) 
1582    {
1583      if ( !pcVPS->getSplittingFlag() )
1584      {
1585        READ_CODE( pcVPS->getDimensionIdLen( j ), uiCode, "dimension_id[i][j]" );  pcVPS->setDimensionId( i, j, uiCode );
1586      }
1587      else
1588      {
1589        pcVPS->setDimensionId( i, j, pcVPS->inferDimensionId( i, j)  );
1590      }
1591    }
1592  }
1593
1594  pcVPS->initNumViews(); 
1595
1596  READ_CODE( 4, uiCode, "view_id_len" ); pcVPS->setViewIdLen( uiCode );
1597
1598  if ( pcVPS->getViewIdLen( ) > 0 )
1599  {   
1600    for( Int i = 0; i < pcVPS->getNumViews(); i++ )
1601    {
1602      READ_CODE( pcVPS->getViewIdLen( ), uiCode, "view_id_val[i]" ); pcVPS->setViewIdVal( i, uiCode );
1603    }
1604  }
1605  else
1606  {
1607    for( Int i = 0; i < pcVPS->getNumViews(); i++ )
1608    {
1609      pcVPS->setViewIdVal( i, 0 ); 
1610    }
1611  }
1612
1613#if NH_3D
1614  pcVPS->initViewCompLayer( ); 
1615#endif
1616
1617  for( Int i = 1; i <= pcVPS->getMaxLayersMinus1(); i++ )
1618  {
1619    for( Int j = 0; j < i; j++ )
1620    {
1621      READ_FLAG( uiCode, "direct_dependency_flag[i][j]" );             pcVPS->setDirectDependencyFlag( i, j, uiCode );
1622    }
1623  }
1624  pcVPS->setRefLayers(); 
1625
1626  if ( pcVPS->getNumIndependentLayers() > 1 ) 
1627  {
1628    READ_UVLC( uiCode, "num_add_layer_sets"      ); pcVPS->setNumAddLayerSets( uiCode );
1629  }
1630  for (Int i = 0; i < pcVPS->getNumAddLayerSets(); i++)
1631  {
1632    for (Int j = 1; j < pcVPS->getNumIndependentLayers(); j++)
1633    {
1634      READ_CODE( pcVPS->getHighestLayerIdxPlus1Len( j ) , uiCode, "highest_layer_idx_plus1" ); pcVPS->setHighestLayerIdxPlus1( i, j, uiCode );
1635    }
1636    pcVPS->deriveAddLayerSetLayerIdList( i );
1637  }
1638
1639  READ_FLAG( uiCode, "vps_sub_layers_max_minus1_present_flag" ); pcVPS->setVpsSubLayersMaxMinus1PresentFlag( uiCode == 1 );
1640  if ( pcVPS->getVpsSubLayersMaxMinus1PresentFlag() )
1641  {
1642    for (Int i = 0; i <= pcVPS->getMaxLayersMinus1(); i++ )
1643    {
1644      READ_CODE( 3, uiCode, "sub_layers_vps_max_minus1" ); pcVPS->setSubLayersVpsMaxMinus1( i, uiCode );   
1645      pcVPS->checkSubLayersVpsMaxMinus1( i ); 
1646    }
1647  } 
1648  else
1649  {
1650    for (Int i = 0; i <= pcVPS->getMaxLayersMinus1(); i++ )
1651    {
1652      pcVPS->setSubLayersVpsMaxMinus1( i, pcVPS->getMaxTLayers( ) - 1);   
1653    }
1654  }
1655  READ_FLAG( uiCode, "max_tid_ref_present_flag" ); pcVPS->setMaxTidRefPresentFlag( uiCode == 1 );
1656
1657  if ( pcVPS->getMaxTidRefPresentFlag() )
1658  {   
1659    for( Int i = 0; i < pcVPS->getMaxLayersMinus1(); i++ )
1660    {
1661      for( Int j = i + 1; j <= pcVPS->getMaxLayersMinus1(); j++ )
1662      {
1663        if ( pcVPS->getDirectDependencyFlag(j,i) )
1664        {
1665          READ_CODE( 3, uiCode, "max_tid_il_ref_pics_plus1" ); pcVPS->setMaxTidIlRefPicsPlus1( i, j, uiCode );
1666        }
1667      }
1668    }
1669  }
1670
1671  READ_FLAG( uiCode, "all_ref_layers_active_flag" );             pcVPS->setAllRefLayersActiveFlag( uiCode == 1 );
1672  READ_UVLC( uiCode, "vps_num_profile_tier_level_minus1" );  pcVPS->setVpsNumProfileTierLevelMinus1( uiCode ); 
1673
1674  Int offsetVal =  ( pcVPS->getMaxLayersMinus1() > 0  &&  pcVPS->getVpsBaseLayerInternalFlag() ) ? 2 : 1;   
1675  for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 2 : 1; i <= pcVPS->getVpsNumProfileTierLevelMinus1(); i++ )
1676  {
1677    READ_FLAG(  uiCode, "vps_profile_present_flag[i]" );    pcVPS->setVpsProfilePresentFlag( i, uiCode == 1 );
1678    parsePTL ( pcVPS->getPTL( offsetVal ), pcVPS->getVpsProfilePresentFlag( i ), pcVPS->getMaxTLayers()-1);
1679    pcVPS->getPTL( offsetVal )->inferGeneralValues ( pcVPS->getVpsProfilePresentFlag( i ), offsetVal, pcVPS->getPTL( offsetVal - 1 ) );   
1680    pcVPS->getPTL( offsetVal )->inferSubLayerValues( pcVPS->getMaxSubLayersMinus1()      , offsetVal, pcVPS->getPTL( offsetVal - 1 ) );   
1681    offsetVal++;
1682  }
1683
1684
1685  if (pcVPS->getNumLayerSets() > 1)
1686  {
1687    READ_UVLC( uiCode, "num_add_olss" ); pcVPS->setNumAddOlss( uiCode );
1688    READ_CODE( 2, uiCode, "default_output_layer_idc" ); pcVPS->setDefaultOutputLayerIdc( std::min( uiCode, (UInt) 2 ) );   
1689  }
1690
1691  pcVPS->initTargetLayerIdLists( ); 
1692
1693
1694  pcVPS->setOutputLayerFlag(0, 0, pcVPS->inferOutputLayerFlag( 0, 0 )); 
1695  pcVPS->setLayerSetIdxForOlsMinus1(0, -1); 
1696
1697  pcVPS->deriveNecessaryLayerFlags( 0 ); 
1698  pcVPS->deriveTargetLayerIdList( 0 ); 
1699
1700  if (pcVPS->getVpsBaseLayerInternalFlag() )
1701  { 
1702    pcVPS->setProfileTierLevelIdx(0,0, pcVPS->inferProfileTierLevelIdx(0,0) );
1703  }
1704  for( Int i = 1; i < pcVPS->getNumOutputLayerSets( ); i++ )
1705  {
1706    if( pcVPS->getNumLayerSets() > 2 && i >= pcVPS->getNumLayerSets( ) )   
1707    {       
1708      READ_CODE( pcVPS->getLayerSetIdxForOlsMinus1Len( i ), uiCode, "layer_set_idx_for_ols_minus1[i]" ); pcVPS->setLayerSetIdxForOlsMinus1( i, uiCode ); 
1709    }
1710
1711    if ( i > pcVPS->getVpsNumLayerSetsMinus1() || pcVPS->getDefaultOutputLayerIdc() == 2 )
1712    {       
1713      for( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx( i ) ); j++ )
1714      {
1715        READ_FLAG( uiCode, "output_layer_flag" ); pcVPS->setOutputLayerFlag( i, j, uiCode == 1 ); 
1716      }
1717    }
1718    else
1719    { 
1720      for( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx( i ) ); j++ )
1721      {             
1722        pcVPS->setOutputLayerFlag(i,j, pcVPS->inferOutputLayerFlag( i, j )); 
1723      }
1724    }
1725    pcVPS->deriveNecessaryLayerFlags( i ); 
1726    pcVPS->deriveTargetLayerIdList( i ); 
1727
1728    for ( Int j = 0; j < pcVPS->getNumLayersInIdList( pcVPS->olsIdxToLsIdx(i)); j++ )
1729    {   
1730      if (pcVPS->getNecessaryLayerFlag( i, j ) && pcVPS->getVpsNumProfileTierLevelMinus1() > 0 )
1731      {
1732        READ_CODE( pcVPS->getProfileTierLevelIdxLen(), uiCode,"profile_tier_level_idx[ i ][ j ]" );   pcVPS->setProfileTierLevelIdx( i, j, uiCode ); 
1733      }
1734      if (pcVPS->getNecessaryLayerFlag( i, j ) && pcVPS->getVpsNumProfileTierLevelMinus1() == 0 )
1735      {
1736        pcVPS->setProfileTierLevelIdx( i , j, pcVPS->inferProfileTierLevelIdx( i, j) );
1737      }
1738    }
1739
1740    if( pcVPS->getNumOutputLayersInOutputLayerSet( i ) == 1 && pcVPS->getNumDirectRefLayers( pcVPS->getOlsHighestOutputLayerId( i ) ) > 0 )
1741    {
1742      READ_FLAG( uiCode, "alt_output_layer_flag[ i ]" ); pcVPS->setAltOutputLayerFlag( i, uiCode == 1 );
1743    }
1744  }
1745
1746  READ_UVLC( uiCode, "vps_num_rep_formats_minus1" ); pcVPS->setVpsNumRepFormatsMinus1( uiCode );
1747
1748  std::vector<TComRepFormat> repFormats;
1749  repFormats.resize( pcVPS->getVpsNumRepFormatsMinus1() + 1 ); 
1750  for (Int i = 0; i <= pcVPS->getVpsNumRepFormatsMinus1(); i++ )
1751  {     
1752    TComRepFormat* curRepFormat = &repFormats[i]; 
1753    TComRepFormat* prevRepFormat = i > 0 ? &repFormats[ i - 1] : NULL; 
1754    parseRepFormat( i, curRepFormat ,  prevRepFormat);     
1755  }
1756  pcVPS->setRepFormat( repFormats ); 
1757
1758  if ( pcVPS->getVpsNumRepFormatsMinus1() > 0 )
1759  {
1760    READ_FLAG( uiCode, "rep_format_idx_present_flag" ); pcVPS->setRepFormatIdxPresentFlag( uiCode == 1 );
1761  }
1762  if( pcVPS->getRepFormatIdxPresentFlag() ) 
1763  {
1764    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 0; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1765    {
1766      READ_CODE( pcVPS->getVpsRepFormatIdxLen(), uiCode, "vps_rep_format_idx[i]" ); pcVPS->setVpsRepFormatIdx( i, uiCode );
1767    }
1768  }
1769  else
1770  {
1771    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 0; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1772    {
1773      pcVPS->setVpsRepFormatIdx( i, pcVPS->inferVpsRepFormatIdx( i ) );
1774    }
1775  }
1776
1777  READ_FLAG( uiCode, "max_one_active_ref_layer_flag" ); pcVPS->setMaxOneActiveRefLayerFlag ( uiCode == 1 ); 
1778
1779  READ_FLAG( uiCode, "vps_poc_lsb_aligned_flag" ); pcVPS->setVpsPocLsbAlignedFlag( uiCode == 1 );
1780  for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1781  {
1782    if( pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) )  ==  0 )
1783    {     
1784      READ_FLAG( uiCode, "poc_lsb_not_present_flag" ); pcVPS->setPocLsbNotPresentFlag( i, uiCode == 1 );
1785    }
1786  }
1787
1788  parseDpbSize( pcVPS ); 
1789
1790  READ_UVLC( uiCode, "direct_dep_type_len_minus2")    ; pcVPS->setDirectDepTypeLenMinus2   ( uiCode ); 
1791
1792  READ_FLAG( uiCode, "default_direct_dependency_flag" ); pcVPS->setDefaultDirectDependencyFlag( uiCode == 1 );
1793  if ( pcVPS->getDefaultDirectDependencyFlag( ) )
1794  { 
1795    READ_CODE( pcVPS->getDirectDepTypeLenMinus2( ) + 2, uiCode, "default_direct_dependency_type" ); pcVPS->setDefaultDirectDependencyType( uiCode );
1796  }
1797
1798  for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ?  1 : 2; i <= pcVPS->getMaxLayersMinus1(); i++ )
1799  {
1800    for( Int j = pcVPS->getVpsBaseLayerInternalFlag() ?  0 : 1; j < i; j++ )
1801    {
1802      if (pcVPS->getDirectDependencyFlag( i, j) )
1803      {       
1804        if ( pcVPS->getDefaultDirectDependencyFlag( ) )
1805        { 
1806          pcVPS->setDirectDependencyType( i, j , pcVPS->getDefaultDirectDependencyType( ) );
1807        }
1808        else
1809        {
1810
1811          READ_CODE( pcVPS->getDirectDepTypeLenMinus2( ) + 2,  uiCode, "direct_dependency_type[i][j]" ); pcVPS->setDirectDependencyType( i, j , uiCode);
1812        }
1813      }
1814    }
1815  } 
1816  READ_UVLC( uiCode, "vps_non_vui_extension_length" ); pcVPS->setVpsNonVuiExtensionLength( uiCode ); 
1817  for ( Int i = 1; i <= pcVPS->getVpsNonVuiExtensionLength(); i++ )
1818  {
1819    READ_CODE( 8, uiCode, "vps_non_vui_extension_data_byte" );
1820  }
1821  READ_FLAG( uiCode, "vps_vui_present_flag" );  pcVPS->setVpsVuiPresentFlag( uiCode == 1 );
1822 
1823  TComVPSVUI vpsVui;
1824  if( pcVPS->getVpsVuiPresentFlag() )
1825  {
1826    m_pcBitstream->readOutTrailingBits(); // vps_vui_alignment_bit_equal_to_one
1827    parseVPSVUI( pcVPS, &vpsVui ); 
1828  }     
1829  else
1830  {   
1831    // inference of syntax elements that differ from default inference (as done in constructor), when VPS VUI is not present   
1832    vpsVui.setCrossLayerIrapAlignedFlag( false ); 
1833  }
1834  pcVPS->setVPSVUI( vpsVui ); 
1835  pcVPS->checkVPSExtensionSyntax(); 
1836}
1837
1838Void TDecCavlc::parseRepFormat( Int i, TComRepFormat* pcRepFormat, const TComRepFormat* pcPrevRepFormat )
1839{
1840  assert( pcRepFormat ); 
1841
1842  UInt uiCode; 
1843
1844  READ_CODE( 16, uiCode, "pic_width_vps_in_luma_samples" );  pcRepFormat->setPicWidthVpsInLumaSamples ( uiCode );
1845  READ_CODE( 16, uiCode, "pic_height_vps_in_luma_samples" ); pcRepFormat->setPicHeightVpsInLumaSamples( uiCode );
1846  READ_FLAG( uiCode, "chroma_and_bit_depth_vps_present_flag" ); pcRepFormat->setChromaAndBitDepthVpsPresentFlag( uiCode == 1 );
1847
1848  pcRepFormat->checkChromaAndBitDepthVpsPresentFlag( i ); 
1849
1850  if ( pcRepFormat->getChromaAndBitDepthVpsPresentFlag() )
1851  { 
1852    READ_CODE( 2,  uiCode, "chroma_format_vps_idc" );          pcRepFormat->setChromaFormatVpsIdc       ( uiCode );
1853    if ( pcRepFormat->getChromaFormatVpsIdc() == 3 )
1854    {
1855      READ_FLAG( uiCode, "separate_colour_plane_vps_flag" ); pcRepFormat->setSeparateColourPlaneVpsFlag( uiCode == 1 ); 
1856    }
1857    READ_CODE( 4,  uiCode, "bit_depth_vps_luma_minus8" );      pcRepFormat->setBitDepthVpsLumaMinus8    ( uiCode );
1858    READ_CODE( 4,  uiCode, "bit_depth_vps_chroma_minus8" );    pcRepFormat->setBitDepthVpsChromaMinus8  ( uiCode );
1859  }
1860  else
1861  {
1862    pcRepFormat->inferChromaAndBitDepth(pcPrevRepFormat ); 
1863  }
1864  READ_FLAG( uiCode, "conformance_window_vps_flag" ); pcRepFormat->setConformanceWindowVpsFlag( uiCode == 1 );
1865  if ( pcRepFormat->getConformanceWindowVpsFlag() )
1866  {
1867    READ_UVLC( uiCode, "conf_win_vps_left_offset" ); pcRepFormat->setConfWinVpsLeftOffset( uiCode );
1868    READ_UVLC( uiCode, "conf_win_vps_right_offset" ); pcRepFormat->setConfWinVpsRightOffset( uiCode );
1869    READ_UVLC( uiCode, "conf_win_vps_top_offset" ); pcRepFormat->setConfWinVpsTopOffset( uiCode );
1870    READ_UVLC( uiCode, "conf_win_vps_bottom_offset" ); pcRepFormat->setConfWinVpsBottomOffset( uiCode );
1871  }
1872}
1873
1874
1875Void TDecCavlc::parseVPSVUI( const TComVPS* pcVPS, TComVPSVUI* vpsVui )
1876{
1877  assert( pcVPS ); 
1878  assert( vpsVui ); 
1879 
1880  vpsVui->init(pcVPS->getNumAddLayerSets(),pcVPS->getMaxSubLayersMinus1() + 1, pcVPS->getMaxLayersMinus1() + 1 ); 
1881
1882  UInt uiCode; 
1883  READ_FLAG( uiCode, "cross_layer_pic_type_aligned_flag" ); vpsVui->setCrossLayerPicTypeAlignedFlag( uiCode == 1 );
1884  if ( !vpsVui->getCrossLayerPicTypeAlignedFlag() )
1885  { 
1886    READ_FLAG( uiCode, "cross_layer_irap_aligned_flag" ); vpsVui->setCrossLayerIrapAlignedFlag( uiCode == 1 );
1887  }
1888  if( vpsVui->getCrossLayerIrapAlignedFlag( ) )
1889  {
1890    READ_FLAG( uiCode, "all_layers_idr_aligned_flag" ); vpsVui->setAllLayersIdrAlignedFlag( uiCode == 1 );
1891  }
1892  READ_FLAG( uiCode, "bit_rate_present_vps_flag" ); vpsVui->setBitRatePresentVpsFlag( uiCode == 1 );
1893  READ_FLAG( uiCode, "pic_rate_present_vps_flag" ); vpsVui->setPicRatePresentVpsFlag( uiCode == 1 );
1894  if( vpsVui->getBitRatePresentVpsFlag( )  ||  vpsVui->getPicRatePresentVpsFlag( ) )
1895  {
1896    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i  <  pcVPS->getNumLayerSets(); i++ )
1897    {
1898      for( Int j = 0; j  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( i ); j++ ) 
1899      {
1900        if( vpsVui->getBitRatePresentVpsFlag( ) )
1901        {
1902          READ_FLAG( uiCode, "bit_rate_present_flag" ); vpsVui->setBitRatePresentFlag( i, j, uiCode == 1 );           
1903        }
1904        if( vpsVui->getPicRatePresentVpsFlag( )  )
1905        {
1906          READ_FLAG( uiCode, "pic_rate_present_flag" ); vpsVui->setPicRatePresentFlag( i, j, uiCode == 1 );
1907        }
1908        if( vpsVui->getBitRatePresentFlag( i, j ) )
1909        {
1910          READ_CODE( 16, uiCode, "avg_bit_rate" ); vpsVui->setAvgBitRate( i, j, uiCode );
1911          READ_CODE( 16, uiCode, "max_bit_rate" ); vpsVui->setMaxBitRate( i, j, uiCode );
1912        }
1913        if( vpsVui->getPicRatePresentFlag( i, j ) )
1914        {
1915          READ_CODE( 2,  uiCode, "constant_pic_rate_idc" ); vpsVui->setConstantPicRateIdc( i, j, uiCode );
1916          READ_CODE( 16, uiCode, "avg_pic_rate" );          vpsVui->setAvgPicRate( i, j, uiCode );
1917        }
1918      }
1919    }
1920  }
1921
1922  READ_FLAG( uiCode, "video_signal_info_idx_present_flag" ); vpsVui->setVideoSignalInfoIdxPresentFlag( uiCode == 1 );
1923  if( vpsVui->getVideoSignalInfoIdxPresentFlag() )
1924  {
1925    READ_CODE( 4, uiCode, "vps_num_video_signal_info_minus1" ); vpsVui->setVpsNumVideoSignalInfoMinus1( uiCode );
1926  }
1927  else
1928  {
1929    vpsVui->setVpsNumVideoSignalInfoMinus1( (pcVPS->getMaxLayersMinus1() - pcVPS->getVpsBaseLayerInternalFlag()) ? 0 : 1 );
1930  }
1931
1932  std::vector<TComVideoSignalInfo> videoSignalInfos; 
1933  videoSignalInfos.resize(vpsVui->getVpsNumVideoSignalInfoMinus1() + 1 );
1934
1935  for( Int i = 0; i <= vpsVui->getVpsNumVideoSignalInfoMinus1(); i++ )
1936  { 
1937    parseVideoSignalInfo( &videoSignalInfos[i] );     
1938  }
1939  vpsVui->setVideoSignalInfo( videoSignalInfos ); 
1940
1941  if( vpsVui->getVideoSignalInfoIdxPresentFlag() && vpsVui->getVpsNumVideoSignalInfoMinus1() > 0 )
1942  {
1943    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1944    {
1945      READ_CODE( 4, uiCode, "vps_video_signal_info_idx" ); vpsVui->setVpsVideoSignalInfoIdx( i, uiCode );
1946    }
1947  }
1948  else if ( !vpsVui->getVideoSignalInfoIdxPresentFlag() )
1949  {
1950    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1951    {
1952      vpsVui->setVpsVideoSignalInfoIdx( i, i );
1953    }
1954  }
1955  else
1956  {
1957    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
1958    {
1959      vpsVui->setVpsVideoSignalInfoIdx( i, 0 );
1960    }
1961  }
1962
1963  READ_FLAG( uiCode, "tiles_not_in_use_flag" ); vpsVui->setTilesNotInUseFlag( uiCode == 1 );
1964  if( !vpsVui->getTilesNotInUseFlag() ) 
1965  {     
1966    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
1967    {
1968      READ_FLAG( uiCode, "tiles_in_use_flag[i]" ); vpsVui->setTilesInUseFlag( i, uiCode == 1 );
1969      if( vpsVui->getTilesInUseFlag( i ) ) 
1970      {
1971        READ_FLAG( uiCode, "loop_filter_not_across_tiles_flag[i]" ); vpsVui->setLoopFilterNotAcrossTilesFlag( i, uiCode == 1 );
1972      }
1973    } 
1974    for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 1 : 2; i  <=  pcVPS->getMaxLayersMinus1(); i++ ) 
1975    {
1976      for( Int j = 0; j < pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) ) ; j++ )
1977      {
1978        Int layerIdx = pcVPS->getLayerIdInVps(pcVPS->getIdDirectRefLayer(pcVPS->getLayerIdInNuh( i ) , j  )); 
1979        if( vpsVui->getTilesInUseFlag( i )  &&  vpsVui->getTilesInUseFlag( layerIdx ) ) 
1980        {
1981          READ_FLAG( uiCode, "tile_boundaries_aligned_flag[i][j]" ); vpsVui->setTileBoundariesAlignedFlag( i, j, uiCode == 1 );
1982        }
1983      } 
1984    }
1985  } 
1986 
1987  READ_FLAG( uiCode, "wpp_not_in_use_flag" ); vpsVui->setWppNotInUseFlag( uiCode == 1 );
1988 
1989  if( !vpsVui->getWppNotInUseFlag( ))
1990  {
1991    for( Int i = 0; i  <=  pcVPS->getMaxLayersMinus1(); i++ ) 
1992    {
1993      READ_FLAG( uiCode, "wpp_in_use_flag[i]" ); vpsVui->setWppInUseFlag( i, uiCode == 1 );
1994    }
1995  }
1996  READ_FLAG( uiCode, "single_layer_for_non_irap_flag" ); vpsVui->setSingleLayerForNonIrapFlag( uiCode == 1 );
1997  READ_FLAG( uiCode, "higher_layer_irap_skip_flag" ); vpsVui->setHigherLayerIrapSkipFlag( uiCode == 1 );
1998  READ_FLAG( uiCode, "ilp_restricted_ref_layers_flag" ); vpsVui->setIlpRestrictedRefLayersFlag( uiCode == 1 );
1999
2000  if( vpsVui->getIlpRestrictedRefLayersFlag( ) )
2001  {
2002    for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
2003    {
2004      for( Int j = 0; j < pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i ) ); j++ )
2005      {
2006        if( pcVPS->getVpsBaseLayerInternalFlag() || pcVPS->getIdDirectRefLayer( pcVPS->getLayerIdInNuh( i ), j ) > 0 )
2007        {       
2008          READ_UVLC( uiCode, "min_spatial_segment_offset_plus1" ); vpsVui->setMinSpatialSegmentOffsetPlus1( i, j, uiCode );
2009          if( vpsVui->getMinSpatialSegmentOffsetPlus1( i, j ) > 0 )
2010          {
2011            READ_FLAG( uiCode, "ctu_based_offset_enabled_flag" ); vpsVui->setCtuBasedOffsetEnabledFlag( i, j, uiCode == 1 );
2012            if( vpsVui->getCtuBasedOffsetEnabledFlag( i, j ) )
2013            {
2014              READ_UVLC( uiCode, "min_horizontal_ctu_offset_plus1" ); vpsVui->setMinHorizontalCtuOffsetPlus1( i, j, uiCode );
2015            }
2016          }
2017        }
2018      }
2019    }
2020  }
2021
2022  READ_FLAG( uiCode, "vps_vui_bsp_hrd_present_flag" ); vpsVui->setVpsVuiBspHrdPresentFlag( uiCode == 1 );
2023  if ( vpsVui->getVpsVuiBspHrdPresentFlag( ) )
2024  {
2025    assert(pcVPS->getTimingInfo()->getTimingInfoPresentFlag() == 1);   
2026    parseVpsVuiBspHrdParameters( pcVPS, vpsVui );     
2027  }
2028  for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
2029  {
2030    if( pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i )) == 0 ) 
2031    {
2032      READ_FLAG( uiCode, "base_layer_parameter_set_compatibility_flag" ); vpsVui->setBaseLayerParameterSetCompatibilityFlag( i, uiCode == 1 );
2033    }
2034  } 
2035}
2036
2037Void TDecCavlc::parseVpsVuiBspHrdParameters( const TComVPS* pcVPS, TComVPSVUI* vpsVui )
2038{
2039  assert( pcVPS  ); 
2040  assert( vpsVui ); 
2041
2042  TComVpsVuiBspHrdParameters  vpsVuiBspHrdP;   
2043 
2044  UInt uiCode; 
2045  READ_UVLC( uiCode, "vps_num_add_hrd_params" ); vpsVuiBspHrdP.setVpsNumAddHrdParams( uiCode );
2046  vpsVuiBspHrdP.createAfterVpsNumAddHrdParams( pcVPS ); 
2047  for( Int i = pcVPS->getNumHrdParameters(); i < pcVPS->getNumHrdParameters() + vpsVuiBspHrdP.getVpsNumAddHrdParams(); i++ )
2048  { 
2049    if( i > 0 ) 
2050    {
2051      READ_FLAG( uiCode, "cprms_add_present_flag" ); vpsVuiBspHrdP.setCprmsAddPresentFlag( i, uiCode == 1 );
2052    }
2053    else
2054    {
2055       vpsVuiBspHrdP.setCprmsAddPresentFlag( i, true );
2056    }
2057
2058    READ_UVLC( uiCode, "num_sub_layer_hrd_minus1" ); vpsVuiBspHrdP.setNumSubLayerHrdMinus1( i, uiCode );
2059   
2060    TComHRD hrdParameters;
2061    parseHrdParameters( &hrdParameters, vpsVuiBspHrdP.getCprmsAddPresentFlag( i ), vpsVuiBspHrdP.getNumSubLayerHrdMinus1( i ) );     
2062    vpsVuiBspHrdP.setHrdParametermeters( i, hrdParameters ); 
2063  }
2064
2065  vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( 0, 0, 0);
2066  vpsVuiBspHrdP.createAfterNumPartitionsInSchemeMinus1( pcVPS, 0, 0 );
2067
2068  for( Int h = 0; h < pcVPS->getNumOutputLayerSets(); h++ )
2069  { 
2070    if ( h == 0)
2071    {
2072      vpsVuiBspHrdP.setNumSignalledPartitioningSchemes( h, 0 );
2073    }
2074    else
2075    {
2076      READ_UVLC( uiCode, "num_signalled_partitioning_schemes" ); vpsVuiBspHrdP.setNumSignalledPartitioningSchemes( h, uiCode );
2077    }   
2078    vpsVuiBspHrdP.createAfterNumSignalledPartitioningSchemes( pcVPS, h );
2079
2080    for( Int j = 0; j < vpsVuiBspHrdP.getNumSignalledPartitioningSchemes( h ) + 1; j++ )
2081    { 
2082      if ( j == 0 && h == 0 )
2083      {
2084        vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( h, j, uiCode );
2085      }
2086      else if( j == 0 )
2087      {
2088        vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( h, j, pcVPS->getNumLayersInIdList( h ) - 1 );
2089      }
2090      else
2091      {
2092        READ_UVLC( uiCode, "num_partitions_in_scheme_minus1" ); vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( h, j, uiCode );
2093      }
2094      vpsVuiBspHrdP.createAfterNumPartitionsInSchemeMinus1(pcVPS, h, j );
2095
2096      for( Int k = 0; k  <=  vpsVuiBspHrdP.getNumPartitionsInSchemeMinus1( h, j ); k++ ) 
2097      {
2098        for( Int r = 0; r < pcVPS->getNumLayersInIdList(pcVPS->olsIdxToLsIdx( h ) )   ; r++ ) 
2099        {
2100          if( h == 0 && j == 0 && k == 0 && r == 0 )
2101          {
2102             vpsVuiBspHrdP.setLayerIncludedInPartitionFlag( h, j, k, r, true );
2103          }
2104          else if ( h > 0 && j == 0 )
2105          {
2106             vpsVuiBspHrdP.setLayerIncludedInPartitionFlag( h, j, k, r, (k == r) );
2107          }
2108          else
2109          {
2110            READ_FLAG( uiCode, "layer_included_in_partition_flag" ); vpsVuiBspHrdP.setLayerIncludedInPartitionFlag( h, j, k, r, uiCode == 1 );
2111          }         
2112        }
2113      }
2114    } 
2115    if ( h > 0 )
2116    {
2117      for( Int i = 0; i < vpsVuiBspHrdP.getNumSignalledPartitioningSchemes( h ) + 1; i++ ) 
2118      {
2119        for( Int t = 0; t  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( pcVPS->olsIdxToLsIdx( i ) ); t++ )
2120        { 
2121          READ_UVLC( uiCode, "num_bsp_schedules_minus1" ); vpsVuiBspHrdP.setNumBspSchedulesMinus1( h, i, t, uiCode );
2122          vpsVuiBspHrdP.createAfterNumBspSchedulesMinus1(pcVPS, h, i, t );
2123          for( Int j = 0; j  <=  vpsVuiBspHrdP.getNumBspSchedulesMinus1( h, i, t ); j++ ) 
2124          {
2125            for( Int k = 0; k  <=  vpsVuiBspHrdP.getNumPartitionsInSchemeMinus1( h, j ); k++ )
2126            { 
2127              READ_CODE( vpsVuiBspHrdP.getBspHrdIdxLen( pcVPS ), uiCode, "bsp_hrd_idx" ); vpsVuiBspHrdP.setBspHrdIdx( h, i, t, j, k, uiCode );
2128              READ_UVLC( uiCode, "bsp_sched_idx" ); vpsVuiBspHrdP.setBspSchedIdx( h, i, t, j, k, uiCode );
2129            } 
2130          }
2131        } 
2132      }
2133    }
2134  } 
2135  vpsVui->setVpsVuiBspHrdParameters( vpsVuiBspHrdP ); 
2136}
2137
2138Void TDecCavlc::parseVideoSignalInfo( TComVideoSignalInfo* pcVideoSignalInfo ) 
2139{
2140  UInt uiCode; 
2141  READ_CODE( 3, uiCode, "video_vps_format" );             pcVideoSignalInfo->setVideoVpsFormat( uiCode );
2142  READ_FLAG( uiCode, "video_full_range_vps_flag" );       pcVideoSignalInfo->setVideoFullRangeVpsFlag( uiCode == 1 );
2143  READ_CODE( 8, uiCode, "colour_primaries_vps" );         pcVideoSignalInfo->setColourPrimariesVps( uiCode );
2144  READ_CODE( 8, uiCode, "transfer_characteristics_vps" ); pcVideoSignalInfo->setTransferCharacteristicsVps( uiCode );
2145  READ_CODE( 8, uiCode, "matrix_coeffs_vps" );            pcVideoSignalInfo->setMatrixCoeffsVps( uiCode );
2146}
2147
2148Void TDecCavlc::parseDpbSize( TComVPS* vps )
2149{
2150  UInt uiCode; 
2151  TComDpbSize dpbSize; 
2152 
2153  dpbSize.init( vps->getNumOutputLayerSets(), vps->getVpsMaxLayerId() + 1, vps->getMaxSubLayersMinus1() + 1 ) ;
2154
2155  for( Int i = 1; i < vps->getNumOutputLayerSets(); i++ )
2156  { 
2157    Int currLsIdx = vps->olsIdxToLsIdx( i ); 
2158    READ_FLAG( uiCode, "sub_layer_flag_info_present_flag" ); dpbSize.setSubLayerFlagInfoPresentFlag( i, uiCode == 1 );
2159    for( Int j = 0; j  <=  vps->getMaxSubLayersInLayerSetMinus1( currLsIdx ); j++ )
2160    { 
2161      if( j > 0  &&  dpbSize.getSubLayerFlagInfoPresentFlag( i )  ) 
2162      {
2163        READ_FLAG( uiCode, "sub_layer_dpb_info_present_flag" ); dpbSize.setSubLayerDpbInfoPresentFlag( i, j, uiCode == 1 );
2164      }
2165      if( dpbSize.getSubLayerDpbInfoPresentFlag( i, j ) )
2166      { 
2167        for( Int k = 0; k < vps->getNumLayersInIdList( currLsIdx ); k++ )   
2168        {
2169          if ( vps->getNecessaryLayerFlag( i, k ) && ( vps->getVpsBaseLayerInternalFlag() || ( vps->getLayerSetLayerIdList(vps->olsIdxToLsIdx(i),k) != 0 ) ))
2170          {
2171            READ_UVLC( uiCode, "max_vps_dec_pic_buffering_minus1" ); dpbSize.setMaxVpsDecPicBufferingMinus1( i, k, j, uiCode );
2172          }
2173          else
2174          {
2175            if ( vps->getNecessaryLayerFlag( i, k ) && ( j == 0 ) && ( k == 0 ))
2176            {
2177              dpbSize.setMaxVpsDecPicBufferingMinus1(i ,k, j, 0 );
2178            }
2179          }
2180        }
2181        READ_UVLC( uiCode, "max_vps_num_reorder_pics" ); dpbSize.setMaxVpsNumReorderPics( i, j, uiCode );
2182        READ_UVLC( uiCode, "max_vps_latency_increase_plus1" ); dpbSize.setMaxVpsLatencyIncreasePlus1( i, j, uiCode );
2183      }
2184      else
2185      {
2186        if ( j > 0 )
2187        {
2188          for( Int k = 0; k < vps->getNumLayersInIdList( vps->olsIdxToLsIdx( i ) ); k++ )   
2189          {
2190            if ( vps->getNecessaryLayerFlag(i, k ) )
2191            {           
2192              dpbSize.setMaxVpsDecPicBufferingMinus1( i, k, j, dpbSize.getMaxVpsDecPicBufferingMinus1( i,k, j - 1 ) );
2193            }
2194          }
2195          dpbSize.setMaxVpsNumReorderPics      ( i, j, dpbSize.getMaxVpsNumReorderPics      ( i, j - 1 ) );
2196          dpbSize.setMaxVpsLatencyIncreasePlus1( i, j, dpbSize.getMaxVpsLatencyIncreasePlus1( i, j - 1 ) );
2197        }
2198      }
2199    } 
2200  } 
2201  vps->setDpbSize( dpbSize ); 
2202}
2203
2204#if NH_3D
2205Void TDecCavlc::parseVps3dExtension( TComVPS* pcVPS )
2206{
2207  UInt uiCode;   
2208  READ_UVLC( uiCode, "cp_precision"); pcVPS->setCpPrecision( uiCode ) ;
2209 
2210  for (Int n = 1; n < pcVPS->getNumViews(); n++)
2211  {
2212    Int i      = pcVPS->getViewOIdxList( n );
2213    Int iInVps = pcVPS->getVoiInVps( i ); 
2214    READ_CODE( 6, uiCode, "num_cp" ); pcVPS->setNumCp( iInVps, uiCode );
2215
2216    if( pcVPS->getNumCp( iInVps ) > 0 )
2217    {
2218      READ_FLAG( uiCode, "cp_in_slice_segment_header_flag" ); pcVPS->setCpInSliceSegmentHeaderFlag( iInVps, uiCode == 1 );
2219      for( Int m = 0; m < pcVPS->getNumCp( iInVps ); m++ )
2220      {
2221        READ_UVLC( uiCode, "cp_ref_voi" ); pcVPS->setCpRefVoi( iInVps, m, uiCode );
2222        if( !pcVPS->getCpInSliceSegmentHeaderFlag( iInVps ) )
2223        {
2224          Int j      = pcVPS->getCpRefVoi( iInVps, m );
2225          Int jInVps = pcVPS->getVoiInVps( j ); 
2226          Int iCode;
2227          READ_SVLC( iCode, "vps_cp_scale" );                pcVPS->setVpsCpScale   ( iInVps, jInVps, iCode );
2228          READ_SVLC( iCode, "vps_cp_off" );                  pcVPS->setVpsCpOff     ( iInVps, jInVps, iCode );
2229          READ_SVLC( iCode, "vps_cp_inv_scale_plus_scale" ); pcVPS->setVpsCpInvScale( iInVps, jInVps, iCode - pcVPS->getVpsCpScale( iInVps, jInVps ) );
2230          READ_SVLC( iCode, "vps_cp_inv_off_plus_off" );     pcVPS->setVpsCpInvOff  ( iInVps, jInVps, iCode - pcVPS->getVpsCpOff  ( iInVps, jInVps ) );
2231        }
2232      }
2233    }   
2234  }
2235  pcVPS->deriveCpPresentFlag(); 
2236}
2237#endif
2238#endif
2239
2240#if NH_MV
2241Void TDecCavlc::parseFirstSliceSegmentInPicFlag(TComSlice* pcSlice )
2242{
2243#if ENC_DEC_TRACE
2244#if NH_MV
2245  tracePSHeader( "Slice", pcSlice->getLayerId() ); 
2246#else
2247  xTraceSliceHeader();
2248#endif
2249#endif
2250
2251  UInt uiCode;
2252  READ_FLAG( uiCode, "first_slice_segment_in_pic_flag" );
2253  pcSlice->setFirstSliceSegementInPicFlag( uiCode ); 
2254}
2255
2256Void TDecCavlc::parseSliceHeader (TComSlice* pcSlice, ParameterSetManager *parameterSetManager )
2257#else
2258Void TDecCavlc::parseSliceHeader (TComSlice* pcSlice, ParameterSetManager *parameterSetManager, const Int prevTid0POC)
2259#endif
2260{
2261  UInt  uiCode;
2262  Int   iCode;
2263
2264#if !NH_MV
2265#if ENC_DEC_TRACE
2266  xTraceSliceHeader();
2267#endif
2268#endif
2269  TComPPS* pps = NULL;
2270  TComSPS* sps = NULL;
2271#if NH_MV
2272  TComVPS* vps = NULL;
2273#endif
2274
2275#if NH_MV
2276  // This has been parsed before in parseFirstSliceSegmentFlag(TComSlice* pcSlice )
2277  UInt firstSliceSegmentInPic = pcSlice->getFirstSliceSegementInPicFlag();   
2278#else
2279  UInt firstSliceSegmentInPic;
2280  READ_FLAG( firstSliceSegmentInPic, "first_slice_segment_in_pic_flag" );
2281#endif
2282  if( pcSlice->getRapPicFlag())
2283  {
2284    READ_FLAG( uiCode, "no_output_of_prior_pics_flag" );  //ignored -- updated already
2285    pcSlice->setNoOutputPriorPicsFlag(uiCode ? true : false);
2286  }
2287  READ_UVLC (    uiCode, "slice_pic_parameter_set_id" );  pcSlice->setPPSId(uiCode);
2288  pps = parameterSetManager->getPPS(uiCode);
2289  //!KS: need to add error handling code here, if PPS is not available
2290  assert(pps!=0);
2291  sps = parameterSetManager->getSPS(pps->getSPSId());
2292  //!KS: need to add error handling code here, if SPS is not available
2293  assert(sps!=0);
2294#if NH_MV 
2295  vps = parameterSetManager->getVPS(sps->getVPSId());
2296  assert( vps != NULL ); 
2297 
2298  m_decTop->initFromActiveVps( vps );
2299 
2300  if ( !m_decTop->getIsInOwnTargetDecLayerIdList() )
2301  {
2302    return; 
2303  }
2304
2305  pcSlice->setDecodingProcess( m_decTop->getDecodingProcess() ); 
2306  Int targetOlsIdx = m_decTop->getTargetOlsIdx(); 
2307
2308  // Do inference
2309  sps->inferRepFormat  ( vps , pcSlice->getLayerId(), false ); 
2310  sps->inferScalingList( parameterSetManager->getActiveSPS( sps->getSpsScalingListRefLayerId() ) );   
2311  sps->inferSpsMaxDecPicBufferingMinus1( vps, targetOlsIdx, pcSlice->getLayerId(), false ); 
2312
2313  if( sps->getMultiLayerExtSpsFlag() )
2314  {
2315    sps->setTemporalIdNestingFlag( (sps->getMaxTLayers() > 1) ? vps->getTemporalNestingFlag() : true );
2316  }
2317
2318  if ( sps->getVuiParametersPresentFlag() )
2319  {
2320    sps->getVuiParameters()->inferVideoSignalInfo( vps, pcSlice->getLayerId() ); 
2321  }
2322
2323
2324  pcSlice->setVPS(vps);     
2325  pcSlice->setSPS(sps);
2326  pcSlice->setPPS(pps);
2327
2328  pcSlice->setViewId   ( vps->getViewId   ( pcSlice->getLayerId() )      );
2329  pcSlice->setViewIndex( vps->getViewIndex( pcSlice->getLayerId() )      ); 
2330#if NH_3D
2331  pcSlice->setIsDepth  ( vps->getDepthId  ( pcSlice->getLayerId() ) == 1 );
2332#endif
2333#endif
2334
2335  const ChromaFormat chFmt = sps->getChromaFormatIdc();
2336  const UInt numValidComp=getNumberValidComponents(chFmt);
2337  const Bool bChroma=(chFmt!=CHROMA_400);
2338
2339  if( pps->getDependentSliceSegmentsEnabledFlag() && ( !firstSliceSegmentInPic ))
2340  {
2341    READ_FLAG( uiCode, "dependent_slice_segment_flag" );       pcSlice->setDependentSliceSegmentFlag(uiCode ? true : false);
2342  }
2343  else
2344  {
2345    pcSlice->setDependentSliceSegmentFlag(false);
2346  }
2347  Int numCTUs = ((sps->getPicWidthInLumaSamples()+sps->getMaxCUWidth()-1)/sps->getMaxCUWidth())*((sps->getPicHeightInLumaSamples()+sps->getMaxCUHeight()-1)/sps->getMaxCUHeight());
2348  UInt sliceSegmentAddress = 0;
2349  Int bitsSliceSegmentAddress = 0;
2350  while(numCTUs>(1<<bitsSliceSegmentAddress))
2351  {
2352    bitsSliceSegmentAddress++;
2353  }
2354
2355  if(!firstSliceSegmentInPic)
2356  {
2357    READ_CODE( bitsSliceSegmentAddress, sliceSegmentAddress, "slice_segment_address" );
2358  }
2359  //set uiCode to equal slice start address (or dependent slice start address)
2360  pcSlice->setSliceSegmentCurStartCtuTsAddr( sliceSegmentAddress );// this is actually a Raster-Scan (RS) address, but we do not have the RS->TS conversion table defined yet.
2361  pcSlice->setSliceSegmentCurEndCtuTsAddr(numCTUs);                // Set end as the last CTU of the picture.
2362
2363
2364  if (!pcSlice->getDependentSliceSegmentFlag())
2365  {
2366    pcSlice->setSliceCurStartCtuTsAddr(sliceSegmentAddress); // this is actually a Raster-Scan (RS) address, but we do not have the RS->TS conversion table defined yet.
2367    pcSlice->setSliceCurEndCtuTsAddr(numCTUs);
2368  }
2369
2370  if(!pcSlice->getDependentSliceSegmentFlag())
2371  {
2372#if NH_MV   
2373    Int esb = 0; //Don't use i, otherwise will shadow something below
2374
2375    if ( pps->getNumExtraSliceHeaderBits() > esb )
2376    {
2377      esb++; 
2378      READ_FLAG( uiCode, "discardable_flag" ); pcSlice->setDiscardableFlag( uiCode == 1 );
2379      if ( uiCode == 1 )
2380      {
2381        assert(pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TRAIL_R &&
2382          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TSA_R &&
2383          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_STSA_R &&
2384          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RADL_R &&
2385          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RASL_R);
2386      }
2387    }
2388
2389    if ( pps->getNumExtraSliceHeaderBits() > esb )
2390    {
2391      esb++; 
2392      READ_FLAG( uiCode, "cross_layer_bla_flag" ); pcSlice->setCrossLayerBlaFlag( uiCode == 1 );
2393    }
2394    pcSlice->checkCrossLayerBlaFlag( ); 
2395
2396
2397    for (; esb < pps->getNumExtraSliceHeaderBits(); esb++)   
2398#else
2399    for (Int i = 0; i < pps->getNumExtraSliceHeaderBits(); i++)
2400#endif
2401    {
2402      READ_FLAG(uiCode, "slice_reserved_flag[]"); // ignored
2403    }
2404
2405    READ_UVLC (    uiCode, "slice_type" );            pcSlice->setSliceType((SliceType)uiCode);
2406    if( pps->getOutputFlagPresentFlag() )
2407    {
2408      READ_FLAG( uiCode, "pic_output_flag" );    pcSlice->setPicOutputFlag( uiCode ? true : false );
2409    }
2410    else
2411    {
2412      pcSlice->setPicOutputFlag( true );
2413    }
2414
2415    // if (separate_colour_plane_flag == 1)
2416    //   read colour_plane_id
2417    //   (separate_colour_plane_flag == 1) is not supported in this version of the standard.
2418
2419#if NH_MV
2420    if ( (pcSlice->getLayerId() > 0 && !vps->getPocLsbNotPresentFlag( pcSlice->getLayerIdInVps())) || !pcSlice->getIdrPicFlag() )
2421    {
2422      READ_CODE(sps->getBitsForPOC(), uiCode, "slice_pic_order_cnt_lsb"); pcSlice->setSlicePicOrderCntLsb( uiCode );
2423    } 
2424    else
2425    {
2426      pcSlice->setSlicePicOrderCntLsb( 0 );
2427    }
2428
2429    if( pcSlice->getNalUnitType()  != NAL_UNIT_CODED_SLICE_IDR_W_RADL  &&  pcSlice->getNalUnitType() !=  NAL_UNIT_CODED_SLICE_IDR_N_LP )
2430    {     
2431      READ_FLAG( uiCode, "short_term_ref_pic_set_sps_flag" ); pcSlice->setShortTermRefPicSetSpsFlag( uiCode == 1 );
2432      if( !pcSlice->getShortTermRefPicSetSpsFlag() )
2433      {
2434        parseShortTermRefPicSet( sps, pcSlice->getLocalStRps( ), sps->getNumShortTermRefPicSets() );
2435      }
2436      else if( sps->getNumShortTermRefPicSets() > 1 )
2437      {
2438        READ_CODE( pcSlice->getShortTermRefPicSetIdxLen() , uiCode, "short_term_ref_pic_set_idx" ); pcSlice->setShortTermRefPicSetIdx( uiCode );
2439      }
2440      if( sps->getLongTermRefPicsPresentFlag() )
2441      {
2442        if( sps->getNumLongTermRefPicsSps() > 0 )
2443        {
2444          READ_UVLC( uiCode, "num_long_term_sps" ); pcSlice->setNumLongTermSps( uiCode );
2445        }
2446        READ_UVLC( uiCode, "num_long_term_pics" ); pcSlice->setNumLongTermPics( uiCode );
2447        for( Int i = 0; i < pcSlice->getNumLongTermSps() + pcSlice->getNumLongTermPics( ); i++ )
2448        {
2449          if( i < pcSlice->getNumLongTermSps() )
2450          {
2451            if( sps->getNumLongTermRefPicsSps() > 1 )
2452            {
2453              READ_CODE( pcSlice->getLtIdxSpsLen() , uiCode, "lt_idx_sps" ); pcSlice->setLtIdxSps( i, uiCode );
2454            }
2455          }
2456          else
2457          {
2458            READ_CODE( pcSlice->getPocLsbLtLen() , uiCode, "poc_lsb_lt" ); pcSlice->setPocLsbLt( i, uiCode );
2459            READ_FLAG( uiCode, "used_by_curr_pic_lt_flag" ); pcSlice->setUsedByCurrPicLtFlag( i, uiCode == 1 );
2460          }
2461          READ_FLAG( uiCode, "delta_poc_msb_present_flag" ); pcSlice->setDeltaPocMsbPresentFlag( i, uiCode == 1 );
2462          if( pcSlice->getDeltaPocMsbPresentFlag( i ) )
2463          {
2464            READ_UVLC( uiCode, "delta_poc_msb_cycle_lt" ); pcSlice->setDeltaPocMsbCycleLt( i, uiCode );
2465          }
2466        }
2467      }
2468      if( sps->getSpsTemporalMvpEnabledFlag() )
2469      {
2470        READ_FLAG( uiCode, "slice_temporal_mvp_enabled_flag" ); pcSlice->setSliceTemporalMvpEnabledFlag( uiCode == 1 );
2471      }
2472    }
2473#else
2474if( pcSlice->getIdrPicFlag() )
2475{
2476  pcSlice->setPOC(0);
2477  TComReferencePictureSet* rps = pcSlice->getLocalRPS();
2478  (*rps)=TComReferencePictureSet();
2479  pcSlice->setRPS(rps);
2480}
2481else
2482{
2483  READ_CODE(sps->getBitsForPOC(), uiCode, "slice_pic_order_cnt_lsb");
2484  Int iPOClsb = uiCode;
2485  Int iPrevPOC = prevTid0POC;
2486  Int iMaxPOClsb = 1<< sps->getBitsForPOC();
2487  Int iPrevPOClsb = iPrevPOC & (iMaxPOClsb - 1);
2488  Int iPrevPOCmsb = iPrevPOC-iPrevPOClsb;
2489  Int iPOCmsb;
2490  if( ( iPOClsb  <  iPrevPOClsb ) && ( ( iPrevPOClsb - iPOClsb )  >=  ( iMaxPOClsb / 2 ) ) )
2491  {
2492    iPOCmsb = iPrevPOCmsb + iMaxPOClsb;
2493  }
2494  else if( (iPOClsb  >  iPrevPOClsb )  && ( (iPOClsb - iPrevPOClsb )  >  ( iMaxPOClsb / 2 ) ) )
2495  {
2496    iPOCmsb = iPrevPOCmsb - iMaxPOClsb;
2497  }
2498  else
2499  {
2500    iPOCmsb = iPrevPOCmsb;
2501  }
2502  if ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
2503    || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
2504    || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
2505  {
2506    // For BLA picture types, POCmsb is set to 0.
2507    iPOCmsb = 0;
2508  }
2509  pcSlice->setPOC              (iPOCmsb+iPOClsb);
2510
2511  TComReferencePictureSet* rps;
2512  rps = pcSlice->getLocalRPS();
2513  (*rps)=TComReferencePictureSet();
2514
2515  pcSlice->setRPS(rps);
2516  READ_FLAG( uiCode, "short_term_ref_pic_set_sps_flag" );
2517  if(uiCode == 0) // use short-term reference picture set explicitly signalled in slice header
2518  {
2519    parseShortTermRefPicSet(sps,rps, sps->getRPSList()->getNumberOfReferencePictureSets());
2520  }
2521  else // use reference to short-term reference picture set in PPS
2522  {
2523    Int numBits = 0;
2524    while ((1 << numBits) < sps->getRPSList()->getNumberOfReferencePictureSets())
2525    {
2526      numBits++;
2527    }
2528    if (numBits > 0)
2529    {
2530      READ_CODE( numBits, uiCode, "short_term_ref_pic_set_idx");
2531    }
2532    else
2533    {
2534      uiCode = 0;       
2535    }
2536    *rps = *(sps->getRPSList()->getReferencePictureSet(uiCode));
2537  }
2538  if(sps->getLongTermRefsPresent())
2539  {
2540    Int offset = rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures();
2541    UInt numOfLtrp = 0;
2542    UInt numLtrpInSPS = 0;
2543    if (sps->getNumLongTermRefPicSPS() > 0)
2544    {
2545      READ_UVLC( uiCode, "num_long_term_sps");
2546      numLtrpInSPS = uiCode;
2547      numOfLtrp += numLtrpInSPS;
2548      rps->setNumberOfLongtermPictures(numOfLtrp);
2549    }
2550    Int bitsForLtrpInSPS = 0;
2551    while (sps->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
2552    {
2553      bitsForLtrpInSPS++;
2554    }
2555    READ_UVLC( uiCode, "num_long_term_pics");             rps->setNumberOfLongtermPictures(uiCode);
2556    numOfLtrp += uiCode;
2557    rps->setNumberOfLongtermPictures(numOfLtrp);
2558    Int maxPicOrderCntLSB = 1 << sps->getBitsForPOC();
2559    Int prevDeltaMSB = 0, deltaPocMSBCycleLT = 0;
2560    for(Int j=offset+rps->getNumberOfLongtermPictures()-1, k = 0; k < numOfLtrp; j--, k++)
2561    {
2562      Int pocLsbLt;
2563      if (k < numLtrpInSPS)
2564      {
2565        uiCode = 0;
2566        if (bitsForLtrpInSPS > 0)
2567        {
2568          READ_CODE(bitsForLtrpInSPS, uiCode, "lt_idx_sps[i]");
2569        }
2570        Bool usedByCurrFromSPS=sps->getUsedByCurrPicLtSPSFlag(uiCode);
2571
2572        pocLsbLt = sps->getLtRefPicPocLsbSps(uiCode);
2573        rps->setUsed(j,usedByCurrFromSPS);
2574      }
2575      else
2576      {
2577        READ_CODE(sps->getBitsForPOC(), uiCode, "poc_lsb_lt"); pocLsbLt= uiCode;
2578        READ_FLAG( uiCode, "used_by_curr_pic_lt_flag");     rps->setUsed(j,uiCode);
2579      }
2580      READ_FLAG(uiCode,"delta_poc_msb_present_flag");
2581      Bool mSBPresentFlag = uiCode ? true : false;
2582      if(mSBPresentFlag)
2583      {
2584        READ_UVLC( uiCode, "delta_poc_msb_cycle_lt[i]" );
2585        Bool deltaFlag = false;
2586        //            First LTRP                               || First LTRP from SH
2587        if( (j == offset+rps->getNumberOfLongtermPictures()-1) || (j == offset+(numOfLtrp-numLtrpInSPS)-1) )
2588        {
2589          deltaFlag = true;
2590        }
2591        if(deltaFlag)
2592        {
2593          deltaPocMSBCycleLT = uiCode;
2594        }
2595        else
2596        {
2597          deltaPocMSBCycleLT = uiCode + prevDeltaMSB;
2598        }
2599
2600        Int pocLTCurr = pcSlice->getPOC() - deltaPocMSBCycleLT * maxPicOrderCntLSB
2601          - iPOClsb + pocLsbLt;
2602        rps->setPOC     (j, pocLTCurr);
2603        rps->setDeltaPOC(j, - pcSlice->getPOC() + pocLTCurr);
2604        rps->setCheckLTMSBPresent(j,true);
2605      }
2606      else
2607      {
2608            rps->setPOC     (j, pocLsbLt);
2609            rps->setDeltaPOC(j, - pcSlice->getPOC() + pocLsbLt);
2610        rps->setCheckLTMSBPresent(j,false);
2611
2612        // reset deltaPocMSBCycleLT for first LTRP from slice header if MSB not present
2613        if( j == offset+(numOfLtrp-numLtrpInSPS)-1 )
2614        {
2615          deltaPocMSBCycleLT = 0;
2616        }
2617      }
2618      prevDeltaMSB = deltaPocMSBCycleLT;
2619    }
2620    offset += rps->getNumberOfLongtermPictures();
2621    rps->setNumberOfPictures(offset);
2622  }
2623
2624  if ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
2625    || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
2626    || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
2627  {
2628    // In the case of BLA picture types, rps data is read from slice header but ignored
2629    rps = pcSlice->getLocalRPS();
2630    (*rps)=TComReferencePictureSet();
2631    pcSlice->setRPS(rps);
2632  }
2633      if (sps->getSPSTemporalMVPEnabledFlag())
2634  {
2635    READ_FLAG( uiCode, "slice_temporal_mvp_enabled_flag" );
2636    pcSlice->setEnableTMVPFlag( uiCode == 1 ? true : false );
2637  }
2638  else
2639  {
2640    pcSlice->setEnableTMVPFlag(false);
2641  }
2642}
2643#endif
2644
2645#if NH_MV
2646    Bool interLayerPredLayerIdcPresentFlag = false; 
2647    Int layerId       = pcSlice->getLayerId(); 
2648#if NH_3D
2649    if( pcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumRefListLayers( layerId ) > 0 )
2650#else
2651    if( pcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumDirectRefLayers( layerId ) > 0 )
2652#endif
2653    {   
2654      READ_FLAG( uiCode, "inter_layer_pred_enabled_flag" ); pcSlice->setInterLayerPredEnabledFlag( uiCode == 1 );
2655#if NH_3D
2656      if( pcSlice->getInterLayerPredEnabledFlag() && vps->getNumRefListLayers( layerId ) > 1 )
2657#else
2658      if( pcSlice->getInterLayerPredEnabledFlag() && vps->getNumDirectRefLayers( layerId ) > 1 )
2659#endif
2660      {           
2661        if( !vps->getMaxOneActiveRefLayerFlag()) 
2662        {
2663          READ_CODE( pcSlice->getNumInterLayerRefPicsMinus1Len( ), uiCode, "num_inter_layer_ref_pics_minus1" ); pcSlice->setNumInterLayerRefPicsMinus1( uiCode );
2664        }
2665#if NH_3D
2666        if ( pcSlice->getNumActiveRefLayerPics() != vps->getNumRefListLayers( layerId ) )
2667#else
2668        if ( pcSlice->getNumActiveRefLayerPics() != vps->getNumDirectRefLayers( layerId ) )
2669#endif
2670        {
2671          interLayerPredLayerIdcPresentFlag = true; 
2672          for( Int idx = 0; idx < pcSlice->getNumActiveRefLayerPics(); idx++ )   
2673          {
2674            READ_CODE( pcSlice->getInterLayerPredLayerIdcLen( ), uiCode, "inter_layer_pred_layer_idc" ); pcSlice->setInterLayerPredLayerIdc( idx, uiCode );
2675          }
2676        }
2677      } 
2678    }
2679    if ( !interLayerPredLayerIdcPresentFlag )
2680    {
2681      for( Int i = 0; i < pcSlice->getNumActiveRefLayerPics(); i++ )   
2682      {
2683        pcSlice->setInterLayerPredLayerIdc( i, pcSlice->getRefLayerPicIdc( i ) );
2684      }
2685    }
2686#if NH_3D
2687    if ( getDecTop()->decProcAnnexI() )
2688    {   
2689      pcSlice->deriveInCmpPredAndCpAvailFlag( );
2690      if ( pcSlice->getInCmpPredAvailFlag() )
2691      {
2692        READ_FLAG(uiCode, "in_comp_pred_flag");  pcSlice->setInCompPredFlag((Bool)uiCode);     
2693      }
2694      pcSlice->init3dToolParameters(); 
2695    }
2696#endif
2697#endif
2698    if(sps->getUseSAO())
2699    {
2700      READ_FLAG(uiCode, "slice_sao_luma_flag");  pcSlice->setSaoEnabledFlag(CHANNEL_TYPE_LUMA, (Bool)uiCode);
2701
2702      if (bChroma)
2703      {
2704        READ_FLAG(uiCode, "slice_sao_chroma_flag");  pcSlice->setSaoEnabledFlag(CHANNEL_TYPE_CHROMA, (Bool)uiCode);
2705      }
2706    }
2707
2708    if (pcSlice->getIdrPicFlag())
2709    {
2710      pcSlice->setEnableTMVPFlag(false);
2711    }
2712    if (!pcSlice->isIntra())
2713    {
2714
2715      READ_FLAG( uiCode, "num_ref_idx_active_override_flag");
2716      if (uiCode)
2717      {
2718        READ_UVLC (uiCode, "num_ref_idx_l0_active_minus1" );  pcSlice->setNumRefIdx( REF_PIC_LIST_0, uiCode + 1 );
2719        if (pcSlice->isInterB())
2720        {
2721          READ_UVLC (uiCode, "num_ref_idx_l1_active_minus1" );  pcSlice->setNumRefIdx( REF_PIC_LIST_1, uiCode + 1 );
2722        }
2723        else
2724        {
2725          pcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
2726        }
2727      }
2728      else
2729      {
2730        pcSlice->setNumRefIdx(REF_PIC_LIST_0, pps->getNumRefIdxL0DefaultActive());
2731        if (pcSlice->isInterB())
2732        {
2733          pcSlice->setNumRefIdx(REF_PIC_LIST_1, pps->getNumRefIdxL1DefaultActive());
2734        }
2735        else
2736        {
2737          pcSlice->setNumRefIdx(REF_PIC_LIST_1,0);
2738        }
2739      }
2740    }
2741    // }
2742#if NH_MV
2743   
2744    const Int numPicTotalCurr = pcSlice->getNumPicTotalCurr(); 
2745    if( pcSlice->getSliceType() == P_SLICE || pcSlice->getSliceType() == B_SLICE )
2746    {
2747      if( pps->getListsModificationPresentFlag() && numPicTotalCurr > 1 )
2748      {
2749        TComRefPicListModification* rplMod = pcSlice->getRefPicListModification();
2750        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l0" ); rplMod->setRefPicListModificationFlagL0( uiCode == 1 );
2751        if( rplMod->getRefPicListModificationFlagL0() )
2752        {
2753          for( Int i = 0; i  <=  pcSlice->getNumRefIdxL0ActiveMinus1( ); i++ )
2754          {
2755            READ_CODE( rplMod->getListEntryLXLen( numPicTotalCurr ) , uiCode, "list_entry_l0" ); rplMod->setListEntryL0( i, uiCode );
2756          }
2757        }
2758
2759        if( pcSlice->getSliceType() == B_SLICE )
2760        {
2761          READ_FLAG( uiCode, "ref_pic_list_modification_flag_l1" ); rplMod->setRefPicListModificationFlagL1( uiCode == 1 );
2762          if( rplMod->getRefPicListModificationFlagL1() )
2763          {
2764            for( Int i = 0; i  <=  pcSlice->getNumRefIdxL1ActiveMinus1( ); i++ )
2765            {
2766              READ_CODE( rplMod->getListEntryLXLen( numPicTotalCurr ), uiCode, "list_entry_l1" ); rplMod->setListEntryL1( i, uiCode );
2767            }
2768          }
2769        }
2770      }
2771    }
2772#else
2773    TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
2774    if(!pcSlice->isIntra())
2775    {
2776      if( !pps->getListsModificationPresentFlag() || pcSlice->getNumRpsCurrTempList() <= 1 )
2777      {
2778        refPicListModification->setRefPicListModificationFlagL0( 0 );
2779      }
2780      else
2781      {
2782        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l0" ); refPicListModification->setRefPicListModificationFlagL0( uiCode ? 1 : 0 );
2783      }
2784
2785      if(refPicListModification->getRefPicListModificationFlagL0())
2786      {
2787        uiCode = 0;
2788        Int i = 0;
2789        Int numRpsCurrTempList0 = pcSlice->getNumRpsCurrTempList();
2790        if ( numRpsCurrTempList0 > 1 )
2791        {
2792          Int length = 1;
2793          numRpsCurrTempList0 --;
2794          while ( numRpsCurrTempList0 >>= 1)
2795          {
2796            length ++;
2797          }
2798          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
2799          {
2800            READ_CODE( length, uiCode, "list_entry_l0" );
2801            refPicListModification->setRefPicSetIdxL0(i, uiCode );
2802          }
2803        }
2804        else
2805        {
2806          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
2807          {
2808            refPicListModification->setRefPicSetIdxL0(i, 0 );
2809          }
2810        }
2811      }
2812    }
2813    else
2814    {
2815      refPicListModification->setRefPicListModificationFlagL0(0);
2816    }
2817    if(pcSlice->isInterB())
2818    {
2819      if( !pps->getListsModificationPresentFlag() || pcSlice->getNumRpsCurrTempList() <= 1 )
2820      {
2821        refPicListModification->setRefPicListModificationFlagL1( 0 );
2822      }
2823      else
2824      {
2825        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l1" ); refPicListModification->setRefPicListModificationFlagL1( uiCode ? 1 : 0 );
2826      }
2827      if(refPicListModification->getRefPicListModificationFlagL1())
2828      {
2829        uiCode = 0;
2830        Int i = 0;
2831        Int numRpsCurrTempList1 = pcSlice->getNumRpsCurrTempList();
2832        if ( numRpsCurrTempList1 > 1 )
2833        {
2834          Int length = 1;
2835          numRpsCurrTempList1 --;
2836          while ( numRpsCurrTempList1 >>= 1)
2837          {
2838            length ++;
2839          }
2840          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
2841          {
2842            READ_CODE( length, uiCode, "list_entry_l1" );
2843            refPicListModification->setRefPicSetIdxL1(i, uiCode );
2844          }
2845        }
2846        else
2847        {
2848          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
2849          {
2850            refPicListModification->setRefPicSetIdxL1(i, 0 );
2851          }
2852        }
2853      }
2854    }
2855    else
2856    {
2857      refPicListModification->setRefPicListModificationFlagL1(0);
2858    }
2859#endif
2860
2861    if (pcSlice->isInterB())
2862    {
2863      READ_FLAG( uiCode, "mvd_l1_zero_flag" );       pcSlice->setMvdL1ZeroFlag( (uiCode ? true : false) );
2864    }
2865
2866    pcSlice->setCabacInitFlag( false ); // default
2867    if(pps->getCabacInitPresentFlag() && !pcSlice->isIntra())
2868    {
2869      READ_FLAG(uiCode, "cabac_init_flag");
2870      pcSlice->setCabacInitFlag( uiCode ? true : false );
2871    }
2872
2873    if ( pcSlice->getEnableTMVPFlag() )
2874    {
2875      if ( pcSlice->getSliceType() == B_SLICE )
2876      {
2877        READ_FLAG( uiCode, "collocated_from_l0_flag" );
2878        pcSlice->setColFromL0Flag(uiCode);
2879      }
2880      else
2881      {
2882        pcSlice->setColFromL0Flag( 1 );
2883      }
2884
2885      if ( pcSlice->getSliceType() != I_SLICE &&
2886          ((pcSlice->getColFromL0Flag() == 1 && pcSlice->getNumRefIdx(REF_PIC_LIST_0) > 1)||
2887           (pcSlice->getColFromL0Flag() == 0 && pcSlice->getNumRefIdx(REF_PIC_LIST_1) > 1)))
2888      {
2889        READ_UVLC( uiCode, "collocated_ref_idx" );
2890        pcSlice->setColRefIdx(uiCode);
2891      }
2892      else
2893      {
2894        pcSlice->setColRefIdx(0);
2895      }
2896    }
2897    if ( (pps->getUseWP() && pcSlice->getSliceType()==P_SLICE) || (pps->getWPBiPred() && pcSlice->getSliceType()==B_SLICE) )
2898    {
2899      xParsePredWeightTable(pcSlice, sps);
2900      pcSlice->initWpScaling(sps);
2901    }
2902
2903#if NH_3D
2904    else if(    pcSlice->getViewIndex() && ( pcSlice->getSliceType() == P_SLICE || pcSlice->getSliceType() == B_SLICE ) 
2905             && !pcSlice->getIsDepth() && vps->getNumRefListLayers( layerId ) > 0 
2906             && getDecTop()->decProcAnnexI()
2907           )
2908    {
2909      UInt uiCodeTmp = 0;
2910
2911      READ_FLAG ( uiCodeTmp, "slice_ic_enable_flag" );
2912      pcSlice->setApplyIC( uiCodeTmp );
2913
2914      if ( uiCodeTmp )
2915      {
2916        READ_FLAG ( uiCodeTmp, "slice_ic_disabled_merge_zero_idx_flag" );
2917        pcSlice->setIcSkipParseFlag( uiCodeTmp );
2918      }
2919    }
2920#endif
2921
2922    if (!pcSlice->isIntra())
2923    {
2924      READ_UVLC( uiCode, "five_minus_max_num_merge_cand");
2925#if NH_3D
2926      pcSlice->setMaxNumMergeCand(( ( pcSlice->getMpiFlag() || pcSlice->getIvMvPredFlag() || pcSlice->getViewSynthesisPredFlag() ) ? MRG_MAX_NUM_CANDS_MEM : MRG_MAX_NUM_CANDS) - uiCode);
2927#else
2928      pcSlice->setMaxNumMergeCand(MRG_MAX_NUM_CANDS - uiCode);
2929#endif
2930    }
2931
2932    READ_SVLC( iCode, "slice_qp_delta" );
2933    pcSlice->setSliceQp (26 + pps->getPicInitQPMinus26() + iCode);
2934
2935    assert( pcSlice->getSliceQp() >= -sps->getQpBDOffset(CHANNEL_TYPE_LUMA) );
2936    assert( pcSlice->getSliceQp() <=  51 );
2937
2938    if (pps->getSliceChromaQpFlag())
2939    {
2940      if (numValidComp>COMPONENT_Cb)
2941      {
2942        READ_SVLC( iCode, "slice_cb_qp_offset" );
2943        pcSlice->setSliceChromaQpDelta(COMPONENT_Cb, iCode );
2944        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cb) >= -12 );
2945        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cb) <=  12 );
2946        assert( (pps->getQpOffset(COMPONENT_Cb) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cb)) >= -12 );
2947        assert( (pps->getQpOffset(COMPONENT_Cb) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cb)) <=  12 );
2948      }
2949
2950      if (numValidComp>COMPONENT_Cr)
2951      {
2952        READ_SVLC( iCode, "slice_cr_qp_offset" );
2953        pcSlice->setSliceChromaQpDelta(COMPONENT_Cr, iCode );
2954        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cr) >= -12 );
2955        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cr) <=  12 );
2956        assert( (pps->getQpOffset(COMPONENT_Cr) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cr)) >= -12 );
2957        assert( (pps->getQpOffset(COMPONENT_Cr) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cr)) <=  12 );
2958      }
2959    }
2960
2961    if (pps->getPpsRangeExtension().getChromaQpOffsetListEnabledFlag())
2962    {
2963      READ_FLAG(uiCode, "cu_chroma_qp_offset_enabled_flag"); pcSlice->setUseChromaQpAdj(uiCode != 0);
2964    }
2965    else
2966    {
2967      pcSlice->setUseChromaQpAdj(false);
2968    }
2969
2970    if (pps->getDeblockingFilterControlPresentFlag())
2971    {
2972      if(pps->getDeblockingFilterOverrideEnabledFlag())
2973      {
2974        READ_FLAG ( uiCode, "deblocking_filter_override_flag" );        pcSlice->setDeblockingFilterOverrideFlag(uiCode ? true : false);
2975      }
2976      else
2977      {
2978        pcSlice->setDeblockingFilterOverrideFlag(0);
2979      }
2980      if(pcSlice->getDeblockingFilterOverrideFlag())
2981      {
2982        READ_FLAG ( uiCode, "slice_deblocking_filter_disabled_flag" );   pcSlice->setDeblockingFilterDisable(uiCode ? 1 : 0);
2983        if(!pcSlice->getDeblockingFilterDisable())
2984        {
2985          READ_SVLC( iCode, "slice_beta_offset_div2" );                       pcSlice->setDeblockingFilterBetaOffsetDiv2(iCode);
2986          assert(pcSlice->getDeblockingFilterBetaOffsetDiv2() >= -6 &&
2987                 pcSlice->getDeblockingFilterBetaOffsetDiv2() <=  6);
2988          READ_SVLC( iCode, "slice_tc_offset_div2" );                         pcSlice->setDeblockingFilterTcOffsetDiv2(iCode);
2989          assert(pcSlice->getDeblockingFilterTcOffsetDiv2() >= -6 &&
2990                 pcSlice->getDeblockingFilterTcOffsetDiv2() <=  6);
2991        }
2992      }
2993      else
2994      {
2995        pcSlice->setDeblockingFilterDisable       ( pps->getPPSDeblockingFilterDisabledFlag() );
2996        pcSlice->setDeblockingFilterBetaOffsetDiv2( pps->getDeblockingFilterBetaOffsetDiv2() );
2997        pcSlice->setDeblockingFilterTcOffsetDiv2  ( pps->getDeblockingFilterTcOffsetDiv2() );
2998      }
2999    }
3000    else
3001    {
3002      pcSlice->setDeblockingFilterDisable       ( false );
3003      pcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
3004      pcSlice->setDeblockingFilterTcOffsetDiv2  ( 0 );
3005    }
3006
3007    Bool isSAOEnabled = sps->getUseSAO() && (pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_LUMA) || (bChroma && pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_CHROMA)));
3008    Bool isDBFEnabled = (!pcSlice->getDeblockingFilterDisable());
3009
3010    if(pps->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
3011    {
3012      READ_FLAG( uiCode, "slice_loop_filter_across_slices_enabled_flag");
3013    }
3014    else
3015    {
3016      uiCode = pps->getLoopFilterAcrossSlicesEnabledFlag()?1:0;
3017    }
3018    pcSlice->setLFCrossSliceBoundaryFlag( (uiCode==1)?true:false);
3019
3020#if NH_3D
3021    if ( getDecTop()->decProcAnnexI() )
3022    {
3023      Int voiInVps = vps->getVoiInVps( pcSlice->getViewIndex() ); 
3024      if( vps->getCpInSliceSegmentHeaderFlag( voiInVps ) )
3025      {
3026        for( Int m = 0; m < vps->getNumCp( voiInVps ); m++ )
3027        {
3028          Int jInVps = vps->getVoiInVps( vps->getCpRefVoi( voiInVps, m ));
3029          READ_SVLC( iCode, "cp_scale" );                pcSlice->setCpScale   ( jInVps, iCode );
3030          READ_SVLC( iCode, "cp_off" );                  pcSlice->setCpOff     ( jInVps, iCode );
3031          READ_SVLC( iCode, "cp_inv_scale_plus_scale" ); pcSlice->setCpInvScale( jInVps, iCode - pcSlice->getCpScale   ( jInVps ));
3032          READ_SVLC( iCode, "cp_inv_off_plus_off" );     pcSlice->setCpInvOff  ( jInVps, iCode - pcSlice->getCpOff     ( jInVps ));
3033        }
3034      }
3035    }
3036#endif
3037
3038  }
3039
3040  std::vector<UInt> entryPointOffset;
3041  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
3042  {
3043    UInt numEntryPointOffsets;
3044    UInt offsetLenMinus1;
3045    READ_UVLC(numEntryPointOffsets, "num_entry_point_offsets");
3046    if (numEntryPointOffsets>0)
3047    {
3048      READ_UVLC(offsetLenMinus1, "offset_len_minus1");
3049      entryPointOffset.resize(numEntryPointOffsets);
3050      for (UInt idx=0; idx<numEntryPointOffsets; idx++)
3051      {
3052        READ_CODE(offsetLenMinus1+1, uiCode, "entry_point_offset_minus1");
3053        entryPointOffset[ idx ] = uiCode + 1;
3054      }
3055    }
3056  }
3057
3058  if(pps->getSliceHeaderExtensionPresentFlag())
3059  {
3060#if NH_MV
3061    READ_UVLC( uiCode, "slice_segment_header_extension_length" ); pcSlice->setSliceSegmentHeaderExtensionLength( uiCode );
3062    UInt posFollSliceSegHeaderExtLen = m_pcBitstream->getNumBitsRead();
3063
3064    if( pps->getPocResetInfoPresentFlag() )
3065    {
3066      READ_CODE( 2, uiCode, "poc_reset_idc" ); pcSlice->setPocResetIdc( uiCode );
3067    }
3068    else
3069    {
3070      pcSlice->setPocResetIdc( 0 );
3071    }
3072    pcSlice->checkPocResetIdc(); 
3073
3074#if NH_MV
3075    if ( pcSlice->getVPS()->getPocLsbNotPresentFlag(pcSlice->getLayerId()) && pcSlice->getSlicePicOrderCntLsb() > 0 )
3076#else
3077    if ( pcSlice->getVPS()->getPocLsbNotPresentFlag(pcSlice->getLayerId()) && slicePicOrderCntLsb > 0 )
3078#endif
3079    {
3080      assert( pcSlice->getPocResetIdc() != 2 );
3081    }
3082
3083    if( pcSlice->getPocResetIdc() !=  0 )
3084    {
3085      READ_CODE( 6, uiCode, "poc_reset_period_id" ); pcSlice->setPocResetPeriodId( uiCode );     
3086      pcSlice->setHasPocResetPeriodIdPresent( true );
3087    }
3088    else
3089    {
3090      // TODO Copy poc_reset_period from earlier picture
3091#if NH_MV
3092      pcSlice->setHasPocResetPeriodIdPresent( false);
3093#else
3094      pcSlice->setPocResetPeriodId( 0 );
3095#endif
3096    }
3097   
3098    if( pcSlice->getPocResetIdc() ==  3 ) 
3099    {
3100      READ_FLAG( uiCode, "full_poc_reset_flag" ); pcSlice->setFullPocResetFlag( uiCode == 1 );
3101      READ_CODE( pcSlice->getPocLsbValLen() , uiCode, "poc_lsb_val" ); pcSlice->setPocLsbVal( uiCode );
3102    }         
3103    pcSlice->checkPocLsbVal();       
3104
3105    if( !pcSlice->getPocMsbValRequiredFlag() && pcSlice->getVPS()->getVpsPocLsbAlignedFlag() )
3106    {
3107#if NH_MV
3108      READ_FLAG( uiCode, "poc_msb_cycle_val_present_flag" ); pcSlice->setPocMsbCycleValPresentFlag( uiCode == 1 );
3109#else
3110      READ_FLAG( uiCode, "poc_msb_val_present_flag" ); pcSlice->setPocMsbValPresentFlag( uiCode == 1 );
3111#endif
3112    }
3113    else
3114    {
3115#if NH_MV
3116      pcSlice->setPocMsbCycleValPresentFlag( pcSlice->inferPocMsbCycleValPresentFlag( ) ); 
3117#else
3118      pcSlice->setPocMsbValPresentFlag( pcSlice->inferPocMsbValPresentFlag( ) ); 
3119#endif
3120    }
3121
3122   
3123#if NH_MV
3124    if( pcSlice->getPocMsbCycleValPresentFlag() )
3125#else
3126    if( pcSlice->getPocMsbValPresentFlag() )
3127#endif
3128    {
3129#if NH_MV
3130      READ_UVLC( uiCode, "poc_msb_val" ); pcSlice->setPocMsbCycleVal( uiCode );
3131#else
3132      READ_UVLC( uiCode, "poc_msb_val" ); pcSlice->setPocMsbVal( uiCode );
3133#endif
3134    }
3135
3136    while( ( m_pcBitstream->getNumBitsRead() - posFollSliceSegHeaderExtLen ) < pcSlice->getSliceSegmentHeaderExtensionLength() * 8 )
3137    {
3138     READ_FLAG( uiCode, "slice_segment_header_extension_data_bit" );
3139    }
3140    assert( ( m_pcBitstream->getNumBitsRead() - posFollSliceSegHeaderExtLen ) == pcSlice->getSliceSegmentHeaderExtensionLength() * 8  ); 
3141  }
3142#else
3143
3144    READ_UVLC(uiCode,"slice_segment_header_extension_length");
3145    for(Int i=0; i<uiCode; i++)
3146    {
3147      UInt ignore;
3148      READ_CODE(8,ignore,"slice_segment_header_extension_data_byte");
3149    }
3150  }
3151#endif
3152#if RExt__DECODER_DEBUG_BIT_STATISTICS
3153  TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS,m_pcBitstream->readByteAlignment(),0);
3154#else
3155  m_pcBitstream->readByteAlignment();
3156#endif
3157
3158  pcSlice->clearSubstreamSizes();
3159
3160  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
3161  {
3162    Int endOfSliceHeaderLocation = m_pcBitstream->getByteLocation();
3163
3164    // Adjust endOfSliceHeaderLocation to account for emulation prevention bytes in the slice segment header
3165    for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
3166    {
3167      if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) < endOfSliceHeaderLocation )
3168      {
3169        endOfSliceHeaderLocation++;
3170      }
3171    }
3172
3173    Int  curEntryPointOffset     = 0;
3174    Int  prevEntryPointOffset    = 0;
3175    for (UInt idx=0; idx<entryPointOffset.size(); idx++)
3176    {
3177      curEntryPointOffset += entryPointOffset[ idx ];
3178
3179      Int emulationPreventionByteCount = 0;
3180      for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
3181      {
3182        if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) >= ( prevEntryPointOffset + endOfSliceHeaderLocation ) &&
3183             m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) <  ( curEntryPointOffset  + endOfSliceHeaderLocation ) )
3184        {
3185          emulationPreventionByteCount++;
3186        }
3187      }
3188
3189      entryPointOffset[ idx ] -= emulationPreventionByteCount;
3190      prevEntryPointOffset = curEntryPointOffset;
3191      pcSlice->addSubstreamSize(entryPointOffset [ idx ] );
3192    }
3193  }
3194
3195  return;
3196}
3197
3198Void TDecCavlc::parsePTL( TComPTL *rpcPTL, Bool profilePresentFlag, Int maxNumSubLayersMinus1 )
3199{
3200  UInt uiCode;
3201  if(profilePresentFlag)
3202  {
3203    parseProfileTier(rpcPTL->getGeneralPTL(), false);
3204  }
3205  READ_CODE( 8, uiCode, "general_level_idc" );    rpcPTL->getGeneralPTL()->setLevelIdc(Level::Name(uiCode));
3206
3207  for (Int i = 0; i < maxNumSubLayersMinus1; i++)
3208  {
3209    READ_FLAG( uiCode, "sub_layer_profile_present_flag[i]" ); rpcPTL->setSubLayerProfilePresentFlag(i, uiCode);
3210    READ_FLAG( uiCode, "sub_layer_level_present_flag[i]"   ); rpcPTL->setSubLayerLevelPresentFlag  (i, uiCode);
3211#if NH_MV
3212    // When profilePresentFlag is equal to 0, sub_layer_profile_present_flag[ i ] shall be equal to 0.
3213    assert( profilePresentFlag || !rpcPTL->getSubLayerProfilePresentFlag(i) );
3214#endif
3215  }
3216
3217  if (maxNumSubLayersMinus1 > 0)
3218  {
3219    for (Int i = maxNumSubLayersMinus1; i < 8; i++)
3220    {
3221      READ_CODE(2, uiCode, "reserved_zero_2bits");
3222      assert(uiCode == 0);
3223    }
3224  }
3225
3226  for(Int i = 0; i < maxNumSubLayersMinus1; i++)
3227  {
3228    if( rpcPTL->getSubLayerProfilePresentFlag(i) )         
3229    {
3230      parseProfileTier(rpcPTL->getSubLayerPTL(i), true);
3231    }
3232    if(rpcPTL->getSubLayerLevelPresentFlag(i))
3233    {
3234      READ_CODE( 8, uiCode, "sub_layer_level_idc[i]" );   rpcPTL->getSubLayerPTL(i)->setLevelIdc(Level::Name(uiCode));
3235    }
3236  }
3237}
3238
3239#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
3240Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl, const Bool bIsSubLayer)
3241#define PTL_TRACE_TEXT(txt) bIsSubLayer?("sub_layer_" txt) : ("general_" txt)
3242#else
3243Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl, const Bool /*bIsSubLayer*/)
3244#define PTL_TRACE_TEXT(txt) txt
3245#endif
3246{
3247  UInt uiCode;
3248  READ_CODE(2 , uiCode,   PTL_TRACE_TEXT("profile_space"                   )); ptl->setProfileSpace(uiCode);
3249  READ_FLAG(    uiCode,   PTL_TRACE_TEXT("tier_flag"                       )); ptl->setTierFlag    (uiCode ? Level::HIGH : Level::MAIN);
3250  READ_CODE(5 , uiCode,   PTL_TRACE_TEXT("profile_idc"                     )); ptl->setProfileIdc  (Profile::Name(uiCode));
3251  for(Int j = 0; j < 32; j++)
3252  {
3253    READ_FLAG(  uiCode,   PTL_TRACE_TEXT("profile_compatibility_flag[][j]" )); ptl->setProfileCompatibilityFlag(j, uiCode ? 1 : 0);
3254  }
3255  READ_FLAG(uiCode,       PTL_TRACE_TEXT("progressive_source_flag"         )); ptl->setProgressiveSourceFlag(uiCode ? true : false);
3256
3257  READ_FLAG(uiCode,       PTL_TRACE_TEXT("interlaced_source_flag"          )); ptl->setInterlacedSourceFlag(uiCode ? true : false);
3258
3259  READ_FLAG(uiCode,       PTL_TRACE_TEXT("non_packed_constraint_flag"      )); ptl->setNonPackedConstraintFlag(uiCode ? true : false);
3260
3261  READ_FLAG(uiCode,       PTL_TRACE_TEXT("frame_only_constraint_flag"      )); ptl->setFrameOnlyConstraintFlag(uiCode ? true : false);
3262#if NH_MV
3263  if( ptl->getV2ConstraintsPresentFlag() )
3264  {
3265    READ_FLAG( uiCode, "max_12bit_constraint_flag" );        ptl->setMax12bitConstraintFlag      ( uiCode == 1 );
3266    READ_FLAG( uiCode, "max_10bit_constraint_flag" );        ptl->setMax10bitConstraintFlag      ( uiCode == 1 );
3267    READ_FLAG( uiCode, "max_8bit_constraint_flag" );         ptl->setMax8bitConstraintFlag       ( uiCode == 1 );
3268    READ_FLAG( uiCode, "max_422chroma_constraint_flag" );    ptl->setMax422chromaConstraintFlag  ( uiCode == 1 );
3269    READ_FLAG( uiCode, "max_420chroma_constraint_flag" );    ptl->setMax420chromaConstraintFlag  ( uiCode == 1 );
3270    READ_FLAG( uiCode, "max_monochrome_constraint_flag" );   ptl->setMaxMonochromeConstraintFlag ( uiCode == 1 );
3271    READ_FLAG( uiCode, "intra_constraint_flag" );            ptl->setIntraConstraintFlag         ( uiCode == 1 );
3272    READ_FLAG( uiCode, "one_picture_only_constraint_flag" ); ptl->setOnePictureOnlyConstraintFlag( uiCode == 1 );
3273    READ_FLAG( uiCode, "lower_bit_rate_constraint_flag" );   ptl->setLowerBitRateConstraintFlag  ( uiCode == 1 );   
3274    READ_CODE(16, uiCode, "XXX_reserved_zero_34bits[0..15]");
3275    READ_CODE(16, uiCode, "XXX_reserved_zero_34bits[16..31]");
3276    READ_CODE(2 , uiCode, "XXX_reserved_zero_34bits[32..33]");
3277  }
3278  else
3279  {
3280    READ_CODE(16, uiCode, "XXX_reserved_zero_43bits[0..15]");
3281    READ_CODE(16, uiCode, "XXX_reserved_zero_43bits[16..31]");
3282    READ_CODE(11, uiCode, "XXX_reserved_zero_43bits[32..42]");
3283  }
3284  if( ptl->getInbldPresentFlag() )
3285  {
3286    READ_FLAG( uiCode, "inbld_flag" ); ptl->setInbldFlag( uiCode == 1 );
3287  }
3288  else
3289  {
3290    READ_FLAG(uiCode, "reserved_zero_bit");
3291  }
3292#else
3293
3294  if (ptl->getProfileIdc() == Profile::MAINREXT           || ptl->getProfileCompatibilityFlag(Profile::MAINREXT) ||
3295      ptl->getProfileIdc() == Profile::HIGHTHROUGHPUTREXT || ptl->getProfileCompatibilityFlag(Profile::HIGHTHROUGHPUTREXT))
3296  {
3297    UInt maxBitDepth=16;
3298    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_12bit_constraint_flag"       )); if (uiCode) maxBitDepth=12;
3299    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_10bit_constraint_flag"       )); if (uiCode) maxBitDepth=10;
3300    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_8bit_constraint_flag"        )); if (uiCode) maxBitDepth=8;
3301    ptl->setBitDepthConstraint(maxBitDepth);
3302    ChromaFormat chromaFmtConstraint=CHROMA_444;
3303    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_422chroma_constraint_flag"   )); if (uiCode) chromaFmtConstraint=CHROMA_422;
3304    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_420chroma_constraint_flag"   )); if (uiCode) chromaFmtConstraint=CHROMA_420;
3305    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_monochrome_constraint_flag"  )); if (uiCode) chromaFmtConstraint=CHROMA_400;
3306    ptl->setChromaFormatConstraint(chromaFmtConstraint);
3307    READ_FLAG(    uiCode, PTL_TRACE_TEXT("intra_constraint_flag"           )); ptl->setIntraConstraintFlag(uiCode != 0);
3308    READ_FLAG(    uiCode, PTL_TRACE_TEXT("one_picture_only_constraint_flag")); ptl->setOnePictureOnlyConstraintFlag(uiCode != 0);
3309    READ_FLAG(    uiCode, PTL_TRACE_TEXT("lower_bit_rate_constraint_flag"  )); ptl->setLowerBitRateConstraintFlag(uiCode != 0);
3310    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[0..15]"     ));
3311    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[16..31]"    ));
3312    READ_CODE(2,  uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[32..33]"    ));
3313  }
3314  else
3315  {
3316    ptl->setBitDepthConstraint((ptl->getProfileIdc() == Profile::MAIN10)?10:8);
3317    ptl->setChromaFormatConstraint(CHROMA_420);
3318    ptl->setIntraConstraintFlag(false);
3319    ptl->setLowerBitRateConstraintFlag(true);
3320    if (ptl->getProfileIdc() == Profile::MAIN10           || ptl->getProfileCompatibilityFlag(Profile::MAIN10))
3321    {
3322      READ_CODE(7, uiCode, PTL_TRACE_TEXT("reserved_zero_7bits"     ));
3323      READ_FLAG(    uiCode, PTL_TRACE_TEXT("one_picture_only_constraint_flag")); ptl->setOnePictureOnlyConstraintFlag(uiCode != 0);
3324      READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_35bits[0..15]"     ));
3325      READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_35bits[16..31]"    ));
3326      READ_CODE(3,  uiCode, PTL_TRACE_TEXT("reserved_zero_35bits[32..34]"    ));
3327    }
3328    else
3329    {
3330    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[0..15]"     ));
3331    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[16..31]"    ));
3332    READ_CODE(11, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[32..42]"    ));
3333  }
3334  }
3335
3336  if ((ptl->getProfileIdc() >= Profile::MAIN && ptl->getProfileIdc() <= Profile::HIGHTHROUGHPUTREXT) ||
3337       ptl->getProfileCompatibilityFlag(Profile::MAIN) ||
3338       ptl->getProfileCompatibilityFlag(Profile::MAIN10) ||
3339       ptl->getProfileCompatibilityFlag(Profile::MAINSTILLPICTURE) ||
3340       ptl->getProfileCompatibilityFlag(Profile::MAINREXT) ||
3341       ptl->getProfileCompatibilityFlag(Profile::HIGHTHROUGHPUTREXT) )
3342  {
3343    READ_FLAG(    uiCode, PTL_TRACE_TEXT("inbld_flag"                      )); assert(uiCode == 0);
3344  }
3345  else
3346  {
3347    READ_FLAG(    uiCode, PTL_TRACE_TEXT("reserved_zero_bit"               ));
3348  }
3349#undef PTL_TRACE_TEXT
3350#endif
3351}
3352
3353Void TDecCavlc::parseTerminatingBit( UInt& ruiBit )
3354{
3355  ruiBit = false;
3356  Int iBitsLeft = m_pcBitstream->getNumBitsLeft();
3357  if(iBitsLeft <= 8)
3358  {
3359    UInt uiPeekValue = m_pcBitstream->peekBits(iBitsLeft);
3360    if (uiPeekValue == (1<<(iBitsLeft-1)))
3361    {
3362      ruiBit = true;
3363    }
3364  }
3365}
3366
3367Void TDecCavlc::parseRemainingBytes( Bool noTrailingBytesExpected )
3368{
3369  if (noTrailingBytesExpected)
3370  {
3371    const UInt numberOfRemainingSubstreamBytes=m_pcBitstream->getNumBitsLeft();
3372    assert (numberOfRemainingSubstreamBytes == 0);
3373  }
3374  else
3375  {
3376    while (m_pcBitstream->getNumBitsLeft())
3377    {
3378      UInt trailingNullByte=m_pcBitstream->readByte();
3379      if (trailingNullByte!=0)
3380      {
3381        printf("Trailing byte should be 0, but has value %02x\n", trailingNullByte);
3382        assert(trailingNullByte==0);
3383      }
3384    }
3385  }
3386}
3387
3388#if NH_3D
3389Void TDecCavlc::parseDIS( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3390{
3391  assert(0);
3392}
3393#endif
3394
3395Void TDecCavlc::parseSkipFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3396{
3397  assert(0);
3398}
3399
3400Void TDecCavlc::parseCUTransquantBypassFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3401{
3402  assert(0);
3403}
3404
3405Void TDecCavlc::parseMVPIdx( Int& /*riMVPIdx*/ )
3406{
3407  assert(0);
3408}
3409
3410Void TDecCavlc::parseSplitFlag     ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3411{
3412  assert(0);
3413}
3414
3415Void TDecCavlc::parsePartSize( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3416{
3417  assert(0);
3418}
3419
3420Void TDecCavlc::parsePredMode( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3421{
3422  assert(0);
3423}
3424
3425/** Parse I_PCM information.
3426* \param pcCU pointer to CU
3427* \param uiAbsPartIdx CU index
3428* \param uiDepth CU depth
3429* \returns Void
3430*
3431* If I_PCM flag indicates that the CU is I_PCM, parse its PCM alignment bits and codes.
3432*/
3433Void TDecCavlc::parseIPCMInfo( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3434{
3435  assert(0);
3436}
3437
3438Void TDecCavlc::parseIntraDirLumaAng  ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3439{
3440  assert(0);
3441}
3442
3443Void TDecCavlc::parseIntraDirChroma( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3444{
3445  assert(0);
3446}
3447
3448Void TDecCavlc::parseInterDir( TComDataCU* /*pcCU*/, UInt& /*ruiInterDir*/, UInt /*uiAbsPartIdx*/ )
3449{
3450  assert(0);
3451}
3452
3453Void TDecCavlc::parseRefFrmIdx( TComDataCU* /*pcCU*/, Int& /*riRefFrmIdx*/, RefPicList /*eRefList*/ )
3454{
3455  assert(0);
3456}
3457
3458Void TDecCavlc::parseMvd( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiPartIdx*/, UInt /*uiDepth*/, RefPicList /*eRefList*/ )
3459{
3460  assert(0);
3461}
3462
3463Void TDecCavlc::parseCrossComponentPrediction( class TComTU& /*rTu*/, ComponentID /*compID*/ )
3464{
3465  assert(0);
3466}
3467
3468Void TDecCavlc::parseDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
3469{
3470  Int  iDQp;
3471
3472#if RExt__DECODER_DEBUG_BIT_STATISTICS || ENC_DEC_TRACE
3473  READ_SVLC(iDQp, "delta_qp");
3474#else
3475  xReadSvlc( iDQp );
3476#endif
3477
3478  Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA);
3479  const Int qp = (((Int) pcCU->getRefQP( uiAbsPartIdx ) + iDQp + 52 + 2*qpBdOffsetY )%(52+ qpBdOffsetY)) -  qpBdOffsetY;
3480
3481  const UInt maxCUDepth        = pcCU->getSlice()->getSPS()->getMaxTotalCUDepth();
3482  const UInt maxCuDQPDepth     = pcCU->getSlice()->getPPS()->getMaxCuDQPDepth();
3483  const UInt doubleDepthDifference = ((maxCUDepth - maxCuDQPDepth)<<1);
3484  const UInt uiAbsQpCUPartIdx = (uiAbsPartIdx>>doubleDepthDifference)<<doubleDepthDifference ;
3485  const UInt uiQpCUDepth =   min(uiDepth,pcCU->getSlice()->getPPS()->getMaxCuDQPDepth()) ;
3486
3487  pcCU->setQPSubParts( qp, uiAbsQpCUPartIdx, uiQpCUDepth );
3488}
3489
3490Void TDecCavlc::parseChromaQpAdjustment( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3491{
3492  assert(0);
3493}
3494
3495Void TDecCavlc::parseCoeffNxN( TComTU &/*rTu*/, ComponentID /*compID*/ )
3496{
3497  assert(0);
3498}
3499
3500Void TDecCavlc::parseTransformSubdivFlag( UInt& /*ruiSubdivFlag*/, UInt /*uiLog2TransformBlockSize*/ )
3501{
3502  assert(0);
3503}
3504
3505Void TDecCavlc::parseQtCbf( TComTU &/*rTu*/, const ComponentID /*compID*/, const Bool /*lowestLevel*/ )
3506{
3507  assert(0);
3508}
3509
3510Void TDecCavlc::parseQtRootCbf( UInt /*uiAbsPartIdx*/, UInt& /*uiQtRootCbf*/ )
3511{
3512  assert(0);
3513}
3514
3515Void TDecCavlc::parseTransformSkipFlags (TComTU &/*rTu*/, ComponentID /*component*/)
3516{
3517  assert(0);
3518}
3519
3520Void TDecCavlc::parseMergeFlag ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/, UInt /*uiPUIdx*/ )
3521{
3522  assert(0);
3523}
3524
3525Void TDecCavlc::parseMergeIndex ( TComDataCU* /*pcCU*/, UInt& /*ruiMergeIndex*/ )
3526{
3527  assert(0);
3528}
3529
3530#if NH_3D
3531Void TDecCavlc::parseARPW( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
3532{
3533  assert(0);
3534}
3535
3536Void TDecCavlc::parseICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
3537{
3538  assert(0);
3539}
3540
3541Void TDecCavlc::parseSDCFlag    ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
3542{
3543  assert(0);
3544}
3545
3546Void TDecCavlc::parseDBBPFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
3547{
3548  assert(0);
3549}
3550#endif
3551// ====================================================================================================================
3552// Protected member functions
3553// ====================================================================================================================
3554
3555//! parse explicit wp tables
3556Void TDecCavlc::xParsePredWeightTable( TComSlice* pcSlice, const TComSPS *sps )
3557{
3558        WPScalingParam *wp;
3559  const ChromaFormat    chFmt        = sps->getChromaFormatIdc();
3560  const Int             numValidComp = Int(getNumberValidComponents(chFmt));
3561  const Bool            bChroma      = (chFmt!=CHROMA_400);
3562  const SliceType       eSliceType   = pcSlice->getSliceType();
3563  const Int             iNbRef       = (eSliceType == B_SLICE ) ? (2) : (1);
3564        UInt            uiLog2WeightDenomLuma=0, uiLog2WeightDenomChroma=0;
3565        UInt            uiTotalSignalledWeightFlags = 0;
3566
3567  Int iDeltaDenom;
3568  // decode delta_luma_log2_weight_denom :
3569  READ_UVLC( uiLog2WeightDenomLuma, "luma_log2_weight_denom" );
3570  assert( uiLog2WeightDenomLuma <= 7 );
3571  if( bChroma )
3572  {
3573    READ_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );
3574    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)>=0);
3575    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)<=7);
3576    uiLog2WeightDenomChroma = (UInt)(iDeltaDenom + uiLog2WeightDenomLuma);
3577  }
3578
3579
3580
3581  for ( Int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ ) // loop over l0 and l1 syntax elements
3582  {
3583    RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
3584    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
3585    {
3586      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
3587
3588      wp[COMPONENT_Y].uiLog2WeightDenom = uiLog2WeightDenomLuma;
3589      for(Int j=1; j<numValidComp; j++)
3590      {
3591        wp[j].uiLog2WeightDenom = uiLog2WeightDenomChroma;
3592      }
3593
3594      UInt  uiCode;
3595      READ_FLAG( uiCode, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
3596      wp[COMPONENT_Y].bPresentFlag = ( uiCode == 1 );
3597      uiTotalSignalledWeightFlags += wp[COMPONENT_Y].bPresentFlag;
3598    }
3599    if ( bChroma )
3600    {
3601      UInt  uiCode;
3602      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
3603      {
3604        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
3605        READ_FLAG( uiCode, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
3606        for(Int j=1; j<numValidComp; j++)
3607        {
3608          wp[j].bPresentFlag = ( uiCode == 1 );
3609        }
3610        uiTotalSignalledWeightFlags += 2*wp[COMPONENT_Cb].bPresentFlag;
3611      }
3612    }
3613    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
3614    {
3615      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
3616      if ( wp[COMPONENT_Y].bPresentFlag )
3617      {
3618        Int iDeltaWeight;
3619        READ_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
3620        assert( iDeltaWeight >= -128 );
3621        assert( iDeltaWeight <=  127 );
3622        wp[COMPONENT_Y].iWeight = (iDeltaWeight + (1<<wp[COMPONENT_Y].uiLog2WeightDenom));
3623        READ_SVLC( wp[COMPONENT_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
3624        Int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<sps->getBitDepth(CHANNEL_TYPE_LUMA))/2 : 128;
3625        assert( wp[0].iOffset >= -range );
3626        assert( wp[0].iOffset <   range );
3627      }
3628      else
3629      {
3630        wp[COMPONENT_Y].iWeight = (1 << wp[COMPONENT_Y].uiLog2WeightDenom);
3631        wp[COMPONENT_Y].iOffset = 0;
3632      }
3633      if ( bChroma )
3634      {
3635        if ( wp[COMPONENT_Cb].bPresentFlag )
3636        {
3637          Int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<sps->getBitDepth(CHANNEL_TYPE_CHROMA))/2 : 128;
3638          for ( Int j=1 ; j<numValidComp ; j++ )
3639          {
3640            Int iDeltaWeight;
3641            READ_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
3642            assert( iDeltaWeight >= -128 );
3643            assert( iDeltaWeight <=  127 );
3644            wp[j].iWeight = (iDeltaWeight + (1<<wp[j].uiLog2WeightDenom));
3645
3646            Int iDeltaChroma;
3647            READ_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
3648            assert( iDeltaChroma >= -4*range);
3649            assert( iDeltaChroma <   4*range);
3650            Int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
3651            wp[j].iOffset = Clip3(-range, range-1, (iDeltaChroma + pred) );
3652          }
3653        }
3654        else
3655        {
3656          for ( Int j=1 ; j<numValidComp ; j++ )
3657          {
3658            wp[j].iWeight = (1 << wp[j].uiLog2WeightDenom);
3659            wp[j].iOffset = 0;
3660          }
3661        }
3662      }
3663    }
3664
3665    for ( Int iRefIdx=pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx<MAX_NUM_REF ; iRefIdx++ )
3666    {
3667      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
3668
3669      wp[0].bPresentFlag = false;
3670      wp[1].bPresentFlag = false;
3671      wp[2].bPresentFlag = false;
3672    }
3673  }
3674  assert(uiTotalSignalledWeightFlags<=24);
3675}
3676
3677/** decode quantization matrix
3678* \param scalingList quantization matrix information
3679*/
3680Void TDecCavlc::parseScalingList(TComScalingList* scalingList)
3681{
3682  UInt  code, sizeId, listId;
3683  Bool scalingListPredModeFlag;
3684  //for each size
3685  for(sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3686  {
3687    for(listId = 0; listId <  SCALING_LIST_NUM; listId++)
3688    {
3689      if ((sizeId==SCALING_LIST_32x32) && (listId%(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) != 0))
3690      {
3691        Int *src = scalingList->getScalingListAddress(sizeId, listId);
3692        const Int size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
3693        const Int *srcNextSmallerSize = scalingList->getScalingListAddress(sizeId-1, listId);
3694        for(Int i=0; i<size; i++)
3695        {
3696          src[i] = srcNextSmallerSize[i];
3697        }
3698        scalingList->setScalingListDC(sizeId,listId,(sizeId > SCALING_LIST_8x8) ? scalingList->getScalingListDC(sizeId-1, listId) : src[0]);
3699      }
3700      else
3701      {
3702        READ_FLAG( code, "scaling_list_pred_mode_flag");
3703        scalingListPredModeFlag = (code) ? true : false;
3704        scalingList->setScalingListPredModeFlag(sizeId, listId, scalingListPredModeFlag);
3705        if(!scalingListPredModeFlag) //Copy Mode
3706        {
3707          READ_UVLC( code, "scaling_list_pred_matrix_id_delta");
3708
3709          if (sizeId==SCALING_LIST_32x32)
3710          {
3711            code*=(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES); // Adjust the decoded code for this size, to cope with the missing 32x32 chroma entries.
3712          }
3713
3714          scalingList->setRefMatrixId (sizeId,listId,(UInt)((Int)(listId)-(code)));
3715          if( sizeId > SCALING_LIST_8x8 )
3716          {
3717            scalingList->setScalingListDC(sizeId,listId,((listId == scalingList->getRefMatrixId (sizeId,listId))? 16 :scalingList->getScalingListDC(sizeId, scalingList->getRefMatrixId (sizeId,listId))));
3718          }
3719          scalingList->processRefMatrix( sizeId, listId, scalingList->getRefMatrixId (sizeId,listId));
3720
3721        }
3722        else //DPCM Mode
3723        {
3724          xDecodeScalingList(scalingList, sizeId, listId);
3725        }
3726      }
3727    }
3728  }
3729
3730  return;
3731}
3732/** decode DPCM
3733* \param scalingList  quantization matrix information
3734* \param sizeId size index
3735* \param listId list index
3736*/
3737Void TDecCavlc::xDecodeScalingList(TComScalingList *scalingList, UInt sizeId, UInt listId)
3738{
3739  Int i,coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
3740  Int data;
3741  Int scalingListDcCoefMinus8 = 0;
3742  Int nextCoef = SCALING_LIST_START_VALUE;
3743  UInt* scan  = g_scanOrder[SCAN_UNGROUPED][SCAN_DIAG][sizeId==0 ? 2 : 3][sizeId==0 ? 2 : 3];
3744  Int *dst = scalingList->getScalingListAddress(sizeId, listId);
3745
3746  if( sizeId > SCALING_LIST_8x8 )
3747  {
3748    READ_SVLC( scalingListDcCoefMinus8, "scaling_list_dc_coef_minus8");
3749    scalingList->setScalingListDC(sizeId,listId,scalingListDcCoefMinus8 + 8);
3750    nextCoef = scalingList->getScalingListDC(sizeId,listId);
3751  }
3752
3753  for(i = 0; i < coefNum; i++)
3754  {
3755    READ_SVLC( data, "scaling_list_delta_coef");
3756    nextCoef = (nextCoef + data + 256 ) % 256;
3757    dst[scan[i]] = nextCoef;
3758  }
3759}
3760
3761Bool TDecCavlc::xMoreRbspData()
3762{
3763  Int bitsLeft = m_pcBitstream->getNumBitsLeft();
3764
3765  // if there are more than 8 bits, it cannot be rbsp_trailing_bits
3766  if (bitsLeft > 8)
3767  {
3768    return true;
3769  }
3770
3771  UChar lastByte = m_pcBitstream->peekBits(bitsLeft);
3772  Int cnt = bitsLeft;
3773
3774  // remove trailing bits equal to zero
3775  while ((cnt>0) && ((lastByte & 1) == 0))
3776  {
3777    lastByte >>= 1;
3778    cnt--;
3779  }
3780  // remove bit equal to one
3781  cnt--;
3782
3783  // we should not have a negative number of bits
3784  assert (cnt>=0);
3785
3786  // we have more data, if cnt is not zero
3787  return (cnt>0);
3788}
3789
3790Void TDecCavlc::parseExplicitRdpcmMode( TComTU& /*rTu*/, ComponentID /*compID*/ )
3791{
3792  assert(0);
3793}
3794//! \}
3795
Note: See TracBrowser for help on using the repository browser.