source: 3DVCSoftware/branches/HTM-15.0-dev0/source/Lib/TLibDecoder/TDecCAVLC.cpp @ 1317

Last change on this file since 1317 was 1317, checked in by tech, 9 years ago

Clean-ups. HLS.

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