source: SHVCSoftware/branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp @ 890

Last change on this file since 890 was 889, checked in by qualcomm, 11 years ago

Fix for signalling of bitrate and picture rate info in VPS VUI to be more aligned to JCTVC-R1008

MACRO: SIGNALLING_BITRATE_PICRATE_FIX

submitted by Hendry (fhendry@…)

  • Property svn:eol-style set to native
File size: 156.1 KB
Line 
1/* The copyright in this software is being made available under the BSD
2* License, included below. This software may be subject to other third party
3* and contributor rights, including patent rights, and no such rights are
4* granted under this license.
5*
6* Copyright (c) 2010-2014, ITU/ISO/IEC
7* All rights reserved.
8*
9* Redistribution and use in source and binary forms, with or without
10* modification, are permitted provided that the following conditions are met:
11*
12*  * Redistributions of source code must retain the above copyright notice,
13*    this list of conditions and the following disclaimer.
14*  * Redistributions in binary form must reproduce the above copyright notice,
15*    this list of conditions and the following disclaimer in the documentation
16*    and/or other materials provided with the distribution.
17*  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18*    be used to endorse or promote products derived from this software without
19*    specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31* THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34/** \file     TDecCAVLC.cpp
35\brief    CAVLC decoder class
36*/
37
38#include "TDecCAVLC.h"
39#include "SEIread.h"
40#include "TDecSlice.h"
41#if Q0048_CGS_3D_ASYMLUT
42#include "../TLibCommon/TCom3DAsymLUT.h"
43#endif
44
45//! \ingroup TLibDecoder
46//! \{
47
48#if ENC_DEC_TRACE
49
50Void  xTraceSPSHeader (TComSPS *pSPS)
51{
52  fprintf( g_hTrace, "=========== Sequence Parameter Set ID: %d ===========\n", pSPS->getSPSId() );
53}
54
55Void  xTracePPSHeader (TComPPS *pPPS)
56{
57  fprintf( g_hTrace, "=========== Picture Parameter Set ID: %d ===========\n", pPPS->getPPSId() );
58}
59
60Void  xTraceSliceHeader (TComSlice *pSlice)
61{
62  fprintf( g_hTrace, "=========== Slice ===========\n");
63}
64
65#endif
66
67// ====================================================================================================================
68// Constructor / destructor / create / destroy
69// ====================================================================================================================
70
71TDecCavlc::TDecCavlc()
72{
73}
74
75TDecCavlc::~TDecCavlc()
76{
77
78}
79
80// ====================================================================================================================
81// Public member functions
82// ====================================================================================================================
83
84void TDecCavlc::parseShortTermRefPicSet( TComSPS* sps, TComReferencePictureSet* rps, Int idx )
85{
86  UInt code;
87  UInt interRPSPred;
88  if (idx > 0)
89  {
90    READ_FLAG(interRPSPred, "inter_ref_pic_set_prediction_flag");  rps->setInterRPSPrediction(interRPSPred);
91  }
92  else
93  {
94    interRPSPred = false;
95    rps->setInterRPSPrediction(false);
96  }
97
98  if (interRPSPred)
99  {
100    UInt bit;
101    if(idx == sps->getRPSList()->getNumberOfReferencePictureSets())
102    {
103      READ_UVLC(code, "delta_idx_minus1" ); // delta index of the Reference Picture Set used for prediction minus 1
104    }
105    else
106    {
107      code = 0;
108    }
109    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
110    Int rIdx =  idx - 1 - code;
111    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
112    TComReferencePictureSet*   rpsRef = sps->getRPSList()->getReferencePictureSet(rIdx);
113    Int k = 0, k0 = 0, k1 = 0;
114    READ_CODE(1, bit, "delta_rps_sign"); // delta_RPS_sign
115    READ_UVLC(code, "abs_delta_rps_minus1");  // absolute delta RPS minus 1
116    Int deltaRPS = (1 - 2 * bit) * (code + 1); // delta_RPS
117    for(Int j=0 ; j <= rpsRef->getNumberOfPictures(); j++)
118    {
119      READ_CODE(1, bit, "used_by_curr_pic_flag" ); //first bit is "1" if Idc is 1
120      Int refIdc = bit;
121      if (refIdc == 0)
122      {
123        READ_CODE(1, bit, "use_delta_flag" ); //second bit is "1" if Idc is 2, "0" otherwise.
124        refIdc = bit<<1; //second bit is "1" if refIdc is 2, "0" if refIdc = 0.
125      }
126      if (refIdc == 1 || refIdc == 2)
127      {
128        Int deltaPOC = deltaRPS + ((j < rpsRef->getNumberOfPictures())? rpsRef->getDeltaPOC(j) : 0);
129        rps->setDeltaPOC(k, deltaPOC);
130        rps->setUsed(k, (refIdc == 1));
131
132        if (deltaPOC < 0)
133        {
134          k0++;
135        }
136        else
137        {
138          k1++;
139        }
140        k++;
141      }
142      rps->setRefIdc(j,refIdc);
143    }
144    rps->setNumRefIdc(rpsRef->getNumberOfPictures()+1);
145    rps->setNumberOfPictures(k);
146    rps->setNumberOfNegativePictures(k0);
147    rps->setNumberOfPositivePictures(k1);
148    rps->sortDeltaPOC();
149  }
150  else
151  {
152    READ_UVLC(code, "num_negative_pics");           rps->setNumberOfNegativePictures(code);
153    READ_UVLC(code, "num_positive_pics");           rps->setNumberOfPositivePictures(code);
154    Int prev = 0;
155    Int poc;
156    for(Int j=0 ; j < rps->getNumberOfNegativePictures(); j++)
157    {
158      READ_UVLC(code, "delta_poc_s0_minus1");
159      poc = prev-code-1;
160      prev = poc;
161      rps->setDeltaPOC(j,poc);
162      READ_FLAG(code, "used_by_curr_pic_s0_flag");  rps->setUsed(j,code);
163    }
164    prev = 0;
165    for(Int j=rps->getNumberOfNegativePictures(); j < rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures(); j++)
166    {
167      READ_UVLC(code, "delta_poc_s1_minus1");
168      poc = prev+code+1;
169      prev = poc;
170      rps->setDeltaPOC(j,poc);
171      READ_FLAG(code, "used_by_curr_pic_s1_flag");  rps->setUsed(j,code);
172    }
173    rps->setNumberOfPictures(rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures());
174  }
175#if PRINT_RPS_INFO
176  rps->printDeltaPOC();
177#endif
178}
179
180Void TDecCavlc::parsePPS(TComPPS* pcPPS
181#if Q0048_CGS_3D_ASYMLUT
182  , TCom3DAsymLUT * pc3DAsymLUT , Int nLayerID
183#endif
184  )
185{
186#if ENC_DEC_TRACE
187  xTracePPSHeader (pcPPS);
188#endif
189  UInt  uiCode;
190
191  Int   iCode;
192
193  READ_UVLC( uiCode, "pps_pic_parameter_set_id");
194  assert(uiCode <= 63);
195  pcPPS->setPPSId (uiCode);
196
197  READ_UVLC( uiCode, "pps_seq_parameter_set_id");
198  assert(uiCode <= 15);
199  pcPPS->setSPSId (uiCode);
200
201  READ_FLAG( uiCode, "dependent_slice_segments_enabled_flag"    );    pcPPS->setDependentSliceSegmentsEnabledFlag   ( uiCode == 1 );
202  READ_FLAG( uiCode, "output_flag_present_flag" );                    pcPPS->setOutputFlagPresentFlag( uiCode==1 );
203
204  READ_CODE(3, uiCode, "num_extra_slice_header_bits");                pcPPS->setNumExtraSliceHeaderBits(uiCode);
205  READ_FLAG ( uiCode, "sign_data_hiding_flag" ); pcPPS->setSignHideFlag( uiCode );
206
207  READ_FLAG( uiCode,   "cabac_init_present_flag" );            pcPPS->setCabacInitPresentFlag( uiCode ? true : false );
208
209  READ_UVLC(uiCode, "num_ref_idx_l0_default_active_minus1");
210  assert(uiCode <= 14);
211  pcPPS->setNumRefIdxL0DefaultActive(uiCode+1);
212
213  READ_UVLC(uiCode, "num_ref_idx_l1_default_active_minus1");
214  assert(uiCode <= 14);
215  pcPPS->setNumRefIdxL1DefaultActive(uiCode+1);
216
217  READ_SVLC(iCode, "init_qp_minus26" );                            pcPPS->setPicInitQPMinus26(iCode);
218  READ_FLAG( uiCode, "constrained_intra_pred_flag" );              pcPPS->setConstrainedIntraPred( uiCode ? true : false );
219  READ_FLAG( uiCode, "transform_skip_enabled_flag" );
220  pcPPS->setUseTransformSkip ( uiCode ? true : false );
221
222  READ_FLAG( uiCode, "cu_qp_delta_enabled_flag" );            pcPPS->setUseDQP( uiCode ? true : false );
223  if( pcPPS->getUseDQP() )
224  {
225    READ_UVLC( uiCode, "diff_cu_qp_delta_depth" );
226    pcPPS->setMaxCuDQPDepth( uiCode );
227  }
228  else
229  {
230    pcPPS->setMaxCuDQPDepth( 0 );
231  }
232  READ_SVLC( iCode, "pps_cb_qp_offset");
233  pcPPS->setChromaCbQpOffset(iCode);
234  assert( pcPPS->getChromaCbQpOffset() >= -12 );
235  assert( pcPPS->getChromaCbQpOffset() <=  12 );
236
237  READ_SVLC( iCode, "pps_cr_qp_offset");
238  pcPPS->setChromaCrQpOffset(iCode);
239  assert( pcPPS->getChromaCrQpOffset() >= -12 );
240  assert( pcPPS->getChromaCrQpOffset() <=  12 );
241
242  READ_FLAG( uiCode, "pps_slice_chroma_qp_offsets_present_flag" );
243  pcPPS->setSliceChromaQpFlag( uiCode ? true : false );
244
245  READ_FLAG( uiCode, "weighted_pred_flag" );          // Use of Weighting Prediction (P_SLICE)
246  pcPPS->setUseWP( uiCode==1 );
247  READ_FLAG( uiCode, "weighted_bipred_flag" );         // Use of Bi-Directional Weighting Prediction (B_SLICE)
248  pcPPS->setWPBiPred( uiCode==1 );
249
250  READ_FLAG( uiCode, "transquant_bypass_enable_flag");
251  pcPPS->setTransquantBypassEnableFlag(uiCode ? true : false);
252  READ_FLAG( uiCode, "tiles_enabled_flag"               );    pcPPS->setTilesEnabledFlag            ( uiCode == 1 );
253  READ_FLAG( uiCode, "entropy_coding_sync_enabled_flag" );    pcPPS->setEntropyCodingSyncEnabledFlag( uiCode == 1 );
254
255  if( pcPPS->getTilesEnabledFlag() )
256  {
257    READ_UVLC ( uiCode, "num_tile_columns_minus1" );                pcPPS->setNumTileColumnsMinus1( uiCode ); 
258    READ_UVLC ( uiCode, "num_tile_rows_minus1" );                   pcPPS->setNumTileRowsMinus1( uiCode ); 
259    READ_FLAG ( uiCode, "uniform_spacing_flag" );                   pcPPS->setTileUniformSpacingFlag( uiCode == 1 );
260
261    if( !pcPPS->getTileUniformSpacingFlag())
262    {
263      std::vector<Int> columnWidth(pcPPS->getNumTileColumnsMinus1());
264      for(UInt i=0; i<pcPPS->getNumTileColumnsMinus1(); i++)
265      {
266        READ_UVLC( uiCode, "column_width_minus1" );
267        columnWidth[i] = uiCode+1;
268      }
269      pcPPS->setTileColumnWidth(columnWidth);
270
271      std::vector<Int> rowHeight (pcPPS->getTileNumRowsMinus1());
272      for(UInt i=0; i<pcPPS->getTileNumRowsMinus1(); i++)
273      {
274        READ_UVLC( uiCode, "row_height_minus1" );
275        rowHeight[i] = uiCode + 1;
276      }
277      pcPPS->setTileRowHeight(rowHeight);
278    }
279
280    if(pcPPS->getNumTileColumnsMinus1() !=0 || pcPPS->getTileNumRowsMinus1() !=0)
281    {
282      READ_FLAG ( uiCode, "loop_filter_across_tiles_enabled_flag" );   pcPPS->setLoopFilterAcrossTilesEnabledFlag( uiCode ? true : false );
283    }
284  }
285  READ_FLAG( uiCode, "loop_filter_across_slices_enabled_flag" );       pcPPS->setLoopFilterAcrossSlicesEnabledFlag( uiCode ? true : false );
286  READ_FLAG( uiCode, "deblocking_filter_control_present_flag" );       pcPPS->setDeblockingFilterControlPresentFlag( uiCode ? true : false );
287  if(pcPPS->getDeblockingFilterControlPresentFlag())
288  {
289    READ_FLAG( uiCode, "deblocking_filter_override_enabled_flag" );    pcPPS->setDeblockingFilterOverrideEnabledFlag( uiCode ? true : false );
290    READ_FLAG( uiCode, "pps_disable_deblocking_filter_flag" );         pcPPS->setPicDisableDeblockingFilterFlag(uiCode ? true : false );
291    if(!pcPPS->getPicDisableDeblockingFilterFlag())
292    {
293      READ_SVLC ( iCode, "pps_beta_offset_div2" );                     pcPPS->setDeblockingFilterBetaOffsetDiv2( iCode );
294      READ_SVLC ( iCode, "pps_tc_offset_div2" );                       pcPPS->setDeblockingFilterTcOffsetDiv2( iCode );
295    }
296  }
297
298#if SCALINGLIST_INFERRING
299  if( pcPPS->getLayerId() > 0 )
300  {
301    READ_FLAG( uiCode, "pps_infer_scaling_list_flag" );
302    pcPPS->setInferScalingListFlag( uiCode );
303  }
304
305  if( pcPPS->getInferScalingListFlag() )
306  {
307    READ_UVLC( uiCode, "pps_scaling_list_ref_layer_id" ); pcPPS->setScalingListRefLayerId( uiCode );
308
309    // The value of pps_scaling_list_ref_layer_id shall be in the range of 0 to 62, inclusive
310    assert( pcPPS->getScalingListRefLayerId() <= 62 );
311
312    pcPPS->setScalingListPresentFlag( false );
313  }
314  else
315  {
316#endif
317
318    READ_FLAG( uiCode, "pps_scaling_list_data_present_flag" );           pcPPS->setScalingListPresentFlag( uiCode ? true : false );
319
320    if(pcPPS->getScalingListPresentFlag ())
321    {
322      parseScalingList( pcPPS->getScalingList() );
323    }
324
325#if SCALINGLIST_INFERRING
326  }
327#endif
328
329  READ_FLAG( uiCode, "lists_modification_present_flag");
330  pcPPS->setListsModificationPresentFlag(uiCode);
331
332  READ_UVLC( uiCode, "log2_parallel_merge_level_minus2");
333  pcPPS->setLog2ParallelMergeLevelMinus2 (uiCode);
334
335  READ_FLAG( uiCode, "slice_segment_header_extension_present_flag");
336  pcPPS->setSliceHeaderExtensionPresentFlag(uiCode);
337
338  READ_FLAG( uiCode, "pps_extension_flag");
339#if POC_RESET_INFO_INFERENCE
340  pcPPS->setExtensionFlag( uiCode ? true : false );
341
342  if( pcPPS->getExtensionFlag() )
343#else
344  if (uiCode)
345#endif 
346  {
347#if P0166_MODIFIED_PPS_EXTENSION
348    UInt ppsExtensionTypeFlag[8];
349    for (UInt i = 0; i < 8; i++)
350    {
351      READ_FLAG( ppsExtensionTypeFlag[i], "pps_extension_type_flag" );
352    }
353#if !POC_RESET_IDC
354    if (ppsExtensionTypeFlag[1])
355    {
356#else
357    if( ppsExtensionTypeFlag[0] )
358    {
359      READ_FLAG( uiCode, "poc_reset_info_present_flag" );
360      pcPPS->setPocResetInfoPresentFlag(uiCode ? true : false);
361#if REF_REGION_OFFSET
362      READ_UVLC( uiCode,      "num_scaled_ref_layer_offsets" ); pcPPS->setNumScaledRefLayerOffsets(uiCode);
363      for(Int i = 0; i < pcPPS->getNumScaledRefLayerOffsets(); i++)
364      {
365        READ_CODE( 6, uiCode,  "scaled_ref_layer_id" );  pcPPS->setScaledRefLayerId( i, uiCode );
366        READ_FLAG( uiCode, "scaled_ref_layer_offset_present_flag" );   pcPPS->setScaledRefLayerOffsetPresentFlag( i, uiCode );
367        if (uiCode)
368        {
369          Window& scaledWindow = pcPPS->getScaledRefLayerWindow(i);
370          READ_SVLC( iCode, "scaled_ref_layer_left_offset" );    scaledWindow.setWindowLeftOffset  (iCode << 1);
371          READ_SVLC( iCode, "scaled_ref_layer_top_offset" );     scaledWindow.setWindowTopOffset   (iCode << 1);
372          READ_SVLC( iCode, "scaled_ref_layer_right_offset" );   scaledWindow.setWindowRightOffset (iCode << 1);
373          READ_SVLC( iCode, "scaled_ref_layer_bottom_offset" );  scaledWindow.setWindowBottomOffset(iCode << 1);
374#if P0312_VERT_PHASE_ADJ
375          READ_FLAG( uiCode, "vert_phase_position_enable_flag" ); scaledWindow.setVertPhasePositionEnableFlag(uiCode);  pcPPS->setVertPhasePositionEnableFlag( pcPPS->getScaledRefLayerId(i), uiCode);
376#endif
377        }
378        READ_FLAG( uiCode, "ref_region_offset_present_flag" );   pcPPS->setRefRegionOffsetPresentFlag( i, uiCode );
379        if (uiCode)
380        {
381          Window& refWindow = pcPPS->getRefLayerWindow(i);
382          READ_SVLC( iCode, "ref_region_left_offset" );    refWindow.setWindowLeftOffset  (iCode << 1);
383          READ_SVLC( iCode, "ref_region_top_offset" );     refWindow.setWindowTopOffset   (iCode << 1);
384          READ_SVLC( iCode, "ref_region_right_offset" );   refWindow.setWindowRightOffset (iCode << 1);
385          READ_SVLC( iCode, "ref_region_bottom_offset" );  refWindow.setWindowBottomOffset(iCode << 1);
386        }
387#if R0209_GENERIC_PHASE
388        READ_FLAG( uiCode, "resample_phase_set_present_flag" );   pcPPS->setResamplePhaseSetPresentFlag( i, uiCode );
389        if (uiCode)
390        {
391          READ_UVLC( uiCode, "phase_hor_luma" );    pcPPS->setPhaseHorLuma ( i, uiCode );
392          READ_UVLC( uiCode, "phase_ver_luma" );    pcPPS->setPhaseVerLuma ( i, uiCode );
393          READ_UVLC( uiCode, "phase_hor_chroma_plus8" );  pcPPS->setPhaseHorChroma (i, uiCode - 8);
394          READ_UVLC( uiCode, "phase_ver_chroma_plus8" );  pcPPS->setPhaseVerChroma (i, uiCode - 8);
395        }
396#endif
397      }
398#else
399#if MOVE_SCALED_OFFSET_TO_PPS
400      READ_UVLC( uiCode,      "num_scaled_ref_layer_offsets" ); pcPPS->setNumScaledRefLayerOffsets(uiCode);
401      for(Int i = 0; i < pcPPS->getNumScaledRefLayerOffsets(); i++)
402      {
403        Window& scaledWindow = pcPPS->getScaledRefLayerWindow(i);
404#if O0098_SCALED_REF_LAYER_ID
405        READ_CODE( 6,  uiCode,  "scaled_ref_layer_id" );       pcPPS->setScaledRefLayerId( i, uiCode );
406#endif
407        READ_SVLC( iCode, "scaled_ref_layer_left_offset" );    scaledWindow.setWindowLeftOffset  (iCode << 1);
408        READ_SVLC( iCode, "scaled_ref_layer_top_offset" );     scaledWindow.setWindowTopOffset   (iCode << 1);
409        READ_SVLC( iCode, "scaled_ref_layer_right_offset" );   scaledWindow.setWindowRightOffset (iCode << 1);
410        READ_SVLC( iCode, "scaled_ref_layer_bottom_offset" );  scaledWindow.setWindowBottomOffset(iCode << 1);
411#if P0312_VERT_PHASE_ADJ
412        READ_FLAG( uiCode, "vert_phase_position_enable_flag" ); scaledWindow.setVertPhasePositionEnableFlag(uiCode);  pcPPS->setVertPhasePositionEnableFlag( pcPPS->getScaledRefLayerId(i), uiCode);
413#endif
414      }
415#endif
416#endif
417#if Q0048_CGS_3D_ASYMLUT
418      READ_FLAG( uiCode , "colour_mapping_enabled_flag" ); 
419      pcPPS->setCGSFlag( uiCode );
420      if( pcPPS->getCGSFlag() )
421      {
422        xParse3DAsymLUT( pc3DAsymLUT );
423        pcPPS->setCGSOutputBitDepthY( pc3DAsymLUT->getOutputBitDepthY() );
424        pcPPS->setCGSOutputBitDepthC( pc3DAsymLUT->getOutputBitDepthC() );
425      }
426#endif
427#endif
428    }
429#if POC_RESET_INFO_INFERENCE
430    else  // Extension type 0 absent
431    {
432      pcPPS->setPocResetInfoPresentFlag( false );
433    }
434#endif
435    if (ppsExtensionTypeFlag[7])
436    {
437#endif
438
439      while ( xMoreRbspData() )
440      {
441        READ_FLAG( uiCode, "pps_extension_data_flag");
442      }
443#if P0166_MODIFIED_PPS_EXTENSION
444    }
445#endif
446  }
447#if POC_RESET_INFO_INFERENCE
448  if( !pcPPS->getExtensionFlag() )
449  {
450    pcPPS->setPocResetInfoPresentFlag( false );
451  }
452#endif
453}
454
455Void  TDecCavlc::parseVUI(TComVUI* pcVUI, TComSPS *pcSPS)
456{
457#if ENC_DEC_TRACE
458  fprintf( g_hTrace, "----------- vui_parameters -----------\n");
459#endif
460  UInt  uiCode;
461
462  READ_FLAG(     uiCode, "aspect_ratio_info_present_flag");           pcVUI->setAspectRatioInfoPresentFlag(uiCode);
463  if (pcVUI->getAspectRatioInfoPresentFlag())
464  {
465    READ_CODE(8, uiCode, "aspect_ratio_idc");                         pcVUI->setAspectRatioIdc(uiCode);
466    if (pcVUI->getAspectRatioIdc() == 255)
467    {
468      READ_CODE(16, uiCode, "sar_width");                             pcVUI->setSarWidth(uiCode);
469      READ_CODE(16, uiCode, "sar_height");                            pcVUI->setSarHeight(uiCode);
470    }
471  }
472
473  READ_FLAG(     uiCode, "overscan_info_present_flag");               pcVUI->setOverscanInfoPresentFlag(uiCode);
474  if (pcVUI->getOverscanInfoPresentFlag())
475  {
476    READ_FLAG(   uiCode, "overscan_appropriate_flag");                pcVUI->setOverscanAppropriateFlag(uiCode);
477  }
478
479  READ_FLAG(     uiCode, "video_signal_type_present_flag");           pcVUI->setVideoSignalTypePresentFlag(uiCode);
480  if (pcVUI->getVideoSignalTypePresentFlag())
481  {
482    READ_CODE(3, uiCode, "video_format");                             pcVUI->setVideoFormat(uiCode);
483    READ_FLAG(   uiCode, "video_full_range_flag");                    pcVUI->setVideoFullRangeFlag(uiCode);
484    READ_FLAG(   uiCode, "colour_description_present_flag");          pcVUI->setColourDescriptionPresentFlag(uiCode);
485    if (pcVUI->getColourDescriptionPresentFlag())
486    {
487      READ_CODE(8, uiCode, "colour_primaries");                       pcVUI->setColourPrimaries(uiCode);
488      READ_CODE(8, uiCode, "transfer_characteristics");               pcVUI->setTransferCharacteristics(uiCode);
489      READ_CODE(8, uiCode, "matrix_coefficients");                    pcVUI->setMatrixCoefficients(uiCode);
490    }
491  }
492
493  READ_FLAG(     uiCode, "chroma_loc_info_present_flag");             pcVUI->setChromaLocInfoPresentFlag(uiCode);
494  if (pcVUI->getChromaLocInfoPresentFlag())
495  {
496    READ_UVLC(   uiCode, "chroma_sample_loc_type_top_field" );        pcVUI->setChromaSampleLocTypeTopField(uiCode);
497    READ_UVLC(   uiCode, "chroma_sample_loc_type_bottom_field" );     pcVUI->setChromaSampleLocTypeBottomField(uiCode);
498  }
499
500  READ_FLAG(     uiCode, "neutral_chroma_indication_flag");           pcVUI->setNeutralChromaIndicationFlag(uiCode);
501
502  READ_FLAG(     uiCode, "field_seq_flag");                           pcVUI->setFieldSeqFlag(uiCode);
503
504  READ_FLAG(uiCode, "frame_field_info_present_flag");                 pcVUI->setFrameFieldInfoPresentFlag(uiCode);
505
506  READ_FLAG(     uiCode, "default_display_window_flag");
507  if (uiCode != 0)
508  {
509    Window &defDisp = pcVUI->getDefaultDisplayWindow();
510    READ_UVLC(   uiCode, "def_disp_win_left_offset" );                defDisp.setWindowLeftOffset  ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
511    READ_UVLC(   uiCode, "def_disp_win_right_offset" );               defDisp.setWindowRightOffset ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc()) );
512    READ_UVLC(   uiCode, "def_disp_win_top_offset" );                 defDisp.setWindowTopOffset   ( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
513    READ_UVLC(   uiCode, "def_disp_win_bottom_offset" );              defDisp.setWindowBottomOffset( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc()) );
514  }
515  TimingInfo *timingInfo = pcVUI->getTimingInfo();
516  READ_FLAG(       uiCode, "vui_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
517#if SVC_EXTENSION
518  if( pcSPS->getLayerId() > 0 )
519  {
520    assert( timingInfo->getTimingInfoPresentFlag() == false );
521  }
522#endif
523  if(timingInfo->getTimingInfoPresentFlag())
524  {
525    READ_CODE( 32, uiCode, "vui_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
526    READ_CODE( 32, uiCode, "vui_time_scale");                       timingInfo->setTimeScale                  (uiCode);
527    READ_FLAG(     uiCode, "vui_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
528    if(timingInfo->getPocProportionalToTimingFlag())
529    {
530      READ_UVLC(   uiCode, "vui_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
531    }
532    READ_FLAG(     uiCode, "hrd_parameters_present_flag");              pcVUI->setHrdParametersPresentFlag(uiCode);
533    if( pcVUI->getHrdParametersPresentFlag() )
534    {
535      parseHrdParameters( pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
536    }
537  }
538  READ_FLAG(     uiCode, "bitstream_restriction_flag");               pcVUI->setBitstreamRestrictionFlag(uiCode);
539  if (pcVUI->getBitstreamRestrictionFlag())
540  {
541    READ_FLAG(   uiCode, "tiles_fixed_structure_flag");               pcVUI->setTilesFixedStructureFlag(uiCode);
542    READ_FLAG(   uiCode, "motion_vectors_over_pic_boundaries_flag");  pcVUI->setMotionVectorsOverPicBoundariesFlag(uiCode);
543    READ_FLAG(   uiCode, "restricted_ref_pic_lists_flag");            pcVUI->setRestrictedRefPicListsFlag(uiCode);
544    READ_UVLC( uiCode, "min_spatial_segmentation_idc");            pcVUI->setMinSpatialSegmentationIdc(uiCode);
545    assert(uiCode < 4096);
546    READ_UVLC(   uiCode, "max_bytes_per_pic_denom" );                 pcVUI->setMaxBytesPerPicDenom(uiCode);
547    READ_UVLC(   uiCode, "max_bits_per_mincu_denom" );                pcVUI->setMaxBitsPerMinCuDenom(uiCode);
548    READ_UVLC(   uiCode, "log2_max_mv_length_horizontal" );           pcVUI->setLog2MaxMvLengthHorizontal(uiCode);
549    READ_UVLC(   uiCode, "log2_max_mv_length_vertical" );             pcVUI->setLog2MaxMvLengthVertical(uiCode);
550  }
551}
552
553Void TDecCavlc::parseHrdParameters(TComHRD *hrd, Bool commonInfPresentFlag, UInt maxNumSubLayersMinus1)
554{
555  UInt  uiCode;
556  if( commonInfPresentFlag )
557  {
558    READ_FLAG( uiCode, "nal_hrd_parameters_present_flag" );           hrd->setNalHrdParametersPresentFlag( uiCode == 1 ? true : false );
559    READ_FLAG( uiCode, "vcl_hrd_parameters_present_flag" );           hrd->setVclHrdParametersPresentFlag( uiCode == 1 ? true : false );
560    if( hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() )
561    {
562      READ_FLAG( uiCode, "sub_pic_cpb_params_present_flag" );         hrd->setSubPicCpbParamsPresentFlag( uiCode == 1 ? true : false );
563      if( hrd->getSubPicCpbParamsPresentFlag() )
564      {
565        READ_CODE( 8, uiCode, "tick_divisor_minus2" );                hrd->setTickDivisorMinus2( uiCode );
566        READ_CODE( 5, uiCode, "du_cpb_removal_delay_length_minus1" ); hrd->setDuCpbRemovalDelayLengthMinus1( uiCode );
567        READ_FLAG( uiCode, "sub_pic_cpb_params_in_pic_timing_sei_flag" ); hrd->setSubPicCpbParamsInPicTimingSEIFlag( uiCode == 1 ? true : false );
568        READ_CODE( 5, uiCode, "dpb_output_delay_du_length_minus1"  ); hrd->setDpbOutputDelayDuLengthMinus1( uiCode );
569      }
570      READ_CODE( 4, uiCode, "bit_rate_scale" );                       hrd->setBitRateScale( uiCode );
571      READ_CODE( 4, uiCode, "cpb_size_scale" );                       hrd->setCpbSizeScale( uiCode );
572      if( hrd->getSubPicCpbParamsPresentFlag() )
573      {
574        READ_CODE( 4, uiCode, "cpb_size_du_scale" );                  hrd->setDuCpbSizeScale( uiCode );
575      }
576      READ_CODE( 5, uiCode, "initial_cpb_removal_delay_length_minus1" ); hrd->setInitialCpbRemovalDelayLengthMinus1( uiCode );
577      READ_CODE( 5, uiCode, "au_cpb_removal_delay_length_minus1" );      hrd->setCpbRemovalDelayLengthMinus1( uiCode );
578      READ_CODE( 5, uiCode, "dpb_output_delay_length_minus1" );       hrd->setDpbOutputDelayLengthMinus1( uiCode );
579    }
580  }
581  Int i, j, nalOrVcl;
582  for( i = 0; i <= maxNumSubLayersMinus1; i ++ )
583  {
584    READ_FLAG( uiCode, "fixed_pic_rate_general_flag" );                     hrd->setFixedPicRateFlag( i, uiCode == 1 ? true : false  );
585    if( !hrd->getFixedPicRateFlag( i ) )
586    {
587      READ_FLAG( uiCode, "fixed_pic_rate_within_cvs_flag" );                hrd->setFixedPicRateWithinCvsFlag( i, uiCode == 1 ? true : false  );
588    }
589    else
590    {
591      hrd->setFixedPicRateWithinCvsFlag( i, true );
592    }
593    hrd->setLowDelayHrdFlag( i, 0 ); // Infered to be 0 when not present
594    hrd->setCpbCntMinus1   ( i, 0 ); // Infered to be 0 when not present
595    if( hrd->getFixedPicRateWithinCvsFlag( i ) )
596    {
597      READ_UVLC( uiCode, "elemental_duration_in_tc_minus1" );             hrd->setPicDurationInTcMinus1( i, uiCode );
598    }
599    else
600    {
601      READ_FLAG( uiCode, "low_delay_hrd_flag" );                      hrd->setLowDelayHrdFlag( i, uiCode == 1 ? true : false  );
602    }
603    if (!hrd->getLowDelayHrdFlag( i ))
604    {
605      READ_UVLC( uiCode, "cpb_cnt_minus1" );                          hrd->setCpbCntMinus1( i, uiCode );
606    }
607    for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
608    {
609      if( ( ( nalOrVcl == 0 ) && ( hrd->getNalHrdParametersPresentFlag() ) ) ||
610        ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
611      {
612        for( j = 0; j <= ( hrd->getCpbCntMinus1( i ) ); j ++ )
613        {
614          READ_UVLC( uiCode, "bit_rate_value_minus1" );             hrd->setBitRateValueMinus1( i, j, nalOrVcl, uiCode );
615          READ_UVLC( uiCode, "cpb_size_value_minus1" );             hrd->setCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
616          if( hrd->getSubPicCpbParamsPresentFlag() )
617          {
618            READ_UVLC( uiCode, "cpb_size_du_value_minus1" );       hrd->setDuCpbSizeValueMinus1( i, j, nalOrVcl, uiCode );
619            READ_UVLC( uiCode, "bit_rate_du_value_minus1" );       hrd->setDuBitRateValueMinus1( i, j, nalOrVcl, uiCode );
620          }
621          READ_FLAG( uiCode, "cbr_flag" );                          hrd->setCbrFlag( i, j, nalOrVcl, uiCode == 1 ? true : false  );
622        }
623      }
624    }
625  }
626}
627
628#if SVC_EXTENSION && !SPS_DPB_PARAMS
629Void TDecCavlc::parseSPS(TComSPS* pcSPS, ParameterSetManagerDecoder *parameterSetManager)
630#else
631Void TDecCavlc::parseSPS(TComSPS* pcSPS)
632#endif
633{
634#if ENC_DEC_TRACE
635  xTraceSPSHeader (pcSPS);
636#endif
637
638  UInt  uiCode;
639  READ_CODE( 4,  uiCode, "sps_video_parameter_set_id");          pcSPS->setVPSId        ( uiCode );
640#if SVC_EXTENSION
641  if(pcSPS->getLayerId() == 0)
642  {
643#endif
644    READ_CODE( 3,  uiCode, "sps_max_sub_layers_minus1" );          pcSPS->setMaxTLayers   ( uiCode+1 );
645    assert(uiCode <= 6);
646
647    READ_FLAG( uiCode, "sps_temporal_id_nesting_flag" );               pcSPS->setTemporalIdNestingFlag ( uiCode > 0 ? true : false );
648#if SVC_EXTENSION
649  }
650#if !SPS_DPB_PARAMS
651  else
652  {
653    pcSPS->setMaxTLayers           ( parameterSetManager->getPrefetchedVPS(pcSPS->getVPSId())->getMaxTLayers()          );
654    pcSPS->setTemporalIdNestingFlag( parameterSetManager->getPrefetchedVPS(pcSPS->getVPSId())->getTemporalNestingFlag() );
655  }
656#endif
657#endif
658
659#if !Q0177_SPS_TEMP_NESTING_FIX   //This part is not needed anymore as it is already covered by implementation in TDecTop::xActivateParameterSets()
660  if ( pcSPS->getMaxTLayers() == 1 )
661  {
662    // sps_temporal_id_nesting_flag must be 1 when sps_max_sub_layers_minus1 is 0
663#if SVC_EXTENSION
664#if !SPS_DPB_PARAMS
665    assert( pcSPS->getTemporalIdNestingFlag() == true );
666#endif
667#else
668    assert( uiCode == 1 );
669#endif
670  }
671#endif
672
673#ifdef SPS_PTL_FIX
674  if ( pcSPS->getLayerId() == 0)
675  {
676    parsePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
677  }
678#else
679  parsePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
680#endif
681
682  READ_UVLC(     uiCode, "sps_seq_parameter_set_id" );           pcSPS->setSPSId( uiCode );
683  assert(uiCode <= 15);
684
685#if REPN_FORMAT_IN_VPS
686  if( pcSPS->getLayerId() > 0 )
687  {
688    READ_FLAG( uiCode, "update_rep_format_flag" );
689    pcSPS->setUpdateRepFormatFlag( uiCode ? true : false );
690  }
691  else
692  {
693#if REP_FORMAT_FIX
694    pcSPS->setUpdateRepFormatFlag( false );
695#else
696    pcSPS->setUpdateRepFormatFlag( true );
697#endif
698  }
699#if O0096_REP_FORMAT_INDEX
700  if( pcSPS->getLayerId() == 0 )
701#else
702  if( pcSPS->getLayerId() == 0 || pcSPS->getUpdateRepFormatFlag() )
703#endif
704  {
705#endif
706#if AUXILIARY_PICTURES
707    READ_UVLC(     uiCode, "chroma_format_idc" );                  pcSPS->setChromaFormatIdc( ChromaFormat(uiCode) );
708#else
709    READ_UVLC(     uiCode, "chroma_format_idc" );                  pcSPS->setChromaFormatIdc( uiCode );
710#endif
711    assert(uiCode <= 3);
712    // in the first version we only support chroma_format_idc equal to 1 (4:2:0), so separate_colour_plane_flag cannot appear in the bitstream
713    assert (uiCode == 1);
714    if( uiCode == 3 )
715    {
716      READ_FLAG(     uiCode, "separate_colour_plane_flag");        assert(uiCode == 0);
717    }
718
719    READ_UVLC (    uiCode, "pic_width_in_luma_samples" );          pcSPS->setPicWidthInLumaSamples ( uiCode    );
720    READ_UVLC (    uiCode, "pic_height_in_luma_samples" );         pcSPS->setPicHeightInLumaSamples( uiCode    );
721#if REPN_FORMAT_IN_VPS
722  }
723#if O0096_REP_FORMAT_INDEX
724  else if ( pcSPS->getUpdateRepFormatFlag() )
725  {
726    READ_CODE(8, uiCode, "update_rep_format_index");
727    pcSPS->setUpdateRepFormatIndex(uiCode);
728  }
729#endif
730#endif
731
732#if R0156_CONF_WINDOW_IN_REP_FORMAT
733#if REPN_FORMAT_IN_VPS
734#if O0096_REP_FORMAT_INDEX
735  if( pcSPS->getLayerId() == 0 )
736#else
737  if(  pcSPS->getLayerId() == 0 || pcSPS->getUpdateRepFormatFlag() )
738#endif
739  {
740#endif
741#endif
742    READ_FLAG(     uiCode, "conformance_window_flag");
743    if (uiCode != 0)
744    {
745      Window &conf = pcSPS->getConformanceWindow();
746#if REPN_FORMAT_IN_VPS
747      READ_UVLC(   uiCode, "conf_win_left_offset" );               conf.setWindowLeftOffset  ( uiCode );
748      READ_UVLC(   uiCode, "conf_win_right_offset" );              conf.setWindowRightOffset ( uiCode );
749      READ_UVLC(   uiCode, "conf_win_top_offset" );                conf.setWindowTopOffset   ( uiCode );
750      READ_UVLC(   uiCode, "conf_win_bottom_offset" );             conf.setWindowBottomOffset( uiCode );
751#else
752      READ_UVLC(   uiCode, "conf_win_left_offset" );               conf.setWindowLeftOffset  ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc() ) );
753      READ_UVLC(   uiCode, "conf_win_right_offset" );              conf.setWindowRightOffset ( uiCode * TComSPS::getWinUnitX( pcSPS->getChromaFormatIdc() ) );
754      READ_UVLC(   uiCode, "conf_win_top_offset" );                conf.setWindowTopOffset   ( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc() ) );
755      READ_UVLC(   uiCode, "conf_win_bottom_offset" );             conf.setWindowBottomOffset( uiCode * TComSPS::getWinUnitY( pcSPS->getChromaFormatIdc() ) );
756#endif
757    }
758#if R0156_CONF_WINDOW_IN_REP_FORMAT
759#if REPN_FORMAT_IN_VPS
760  }
761#endif
762#endif
763
764#if REPN_FORMAT_IN_VPS
765#if O0096_REP_FORMAT_INDEX
766  if( pcSPS->getLayerId() == 0 )
767#else
768  if(  pcSPS->getLayerId() == 0 || pcSPS->getUpdateRepFormatFlag() )
769#endif
770  {
771#endif
772    READ_UVLC(     uiCode, "bit_depth_luma_minus8" );
773    assert(uiCode <= 6);
774    pcSPS->setBitDepthY( uiCode + 8 );
775    pcSPS->setQpBDOffsetY( (Int) (6*uiCode) );
776
777    READ_UVLC( uiCode,    "bit_depth_chroma_minus8" );
778    assert(uiCode <= 6);
779    pcSPS->setBitDepthC( uiCode + 8 );
780    pcSPS->setQpBDOffsetC( (Int) (6*uiCode) );
781#if REPN_FORMAT_IN_VPS
782  }
783#endif
784  READ_UVLC( uiCode,    "log2_max_pic_order_cnt_lsb_minus4" );   pcSPS->setBitsForPOC( 4 + uiCode );
785  assert(uiCode <= 12);
786
787#if SPS_DPB_PARAMS
788  if( pcSPS->getLayerId() == 0 ) 
789  {
790#endif
791    UInt subLayerOrderingInfoPresentFlag;
792    READ_FLAG(subLayerOrderingInfoPresentFlag, "sps_sub_layer_ordering_info_present_flag");
793
794    for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
795    {
796      READ_UVLC ( uiCode, "sps_max_dec_pic_buffering_minus1[i]");
797      pcSPS->setMaxDecPicBuffering( uiCode + 1, i);
798      READ_UVLC ( uiCode, "sps_num_reorder_pics[i]" );
799      pcSPS->setNumReorderPics(uiCode, i);
800      READ_UVLC ( uiCode, "sps_max_latency_increase_plus1[i]");
801      pcSPS->setMaxLatencyIncrease( uiCode, i );
802
803      if (!subLayerOrderingInfoPresentFlag)
804      {
805        for (i++; i <= pcSPS->getMaxTLayers()-1; i++)
806        {
807          pcSPS->setMaxDecPicBuffering(pcSPS->getMaxDecPicBuffering(0), i);
808          pcSPS->setNumReorderPics(pcSPS->getNumReorderPics(0), i);
809          pcSPS->setMaxLatencyIncrease(pcSPS->getMaxLatencyIncrease(0), i);
810        }
811        break;
812      }
813    }
814#if SPS_DPB_PARAMS
815  }
816#endif
817  READ_UVLC( uiCode, "log2_min_coding_block_size_minus3" );
818  Int log2MinCUSize = uiCode + 3;
819  pcSPS->setLog2MinCodingBlockSize(log2MinCUSize);
820  READ_UVLC( uiCode, "log2_diff_max_min_coding_block_size" );
821  pcSPS->setLog2DiffMaxMinCodingBlockSize(uiCode);
822
823  if (pcSPS->getPTL()->getGeneralPTL()->getLevelIdc() >= Level::LEVEL5)
824  {
825    assert(log2MinCUSize + pcSPS->getLog2DiffMaxMinCodingBlockSize() >= 5);
826  }
827
828  Int maxCUDepthDelta = uiCode;
829  pcSPS->setMaxCUWidth  ( 1<<(log2MinCUSize + maxCUDepthDelta) );
830  pcSPS->setMaxCUHeight ( 1<<(log2MinCUSize + maxCUDepthDelta) );
831  READ_UVLC( uiCode, "log2_min_transform_block_size_minus2" );   pcSPS->setQuadtreeTULog2MinSize( uiCode + 2 );
832
833  READ_UVLC( uiCode, "log2_diff_max_min_transform_block_size" ); pcSPS->setQuadtreeTULog2MaxSize( uiCode + pcSPS->getQuadtreeTULog2MinSize() );
834  pcSPS->setMaxTrSize( 1<<(uiCode + pcSPS->getQuadtreeTULog2MinSize()) );
835
836  READ_UVLC( uiCode, "max_transform_hierarchy_depth_inter" );    pcSPS->setQuadtreeTUMaxDepthInter( uiCode+1 );
837  READ_UVLC( uiCode, "max_transform_hierarchy_depth_intra" );    pcSPS->setQuadtreeTUMaxDepthIntra( uiCode+1 );
838
839  Int addCuDepth = max (0, log2MinCUSize - (Int)pcSPS->getQuadtreeTULog2MinSize() );
840  pcSPS->setMaxCUDepth( maxCUDepthDelta + addCuDepth );
841  READ_FLAG( uiCode, "scaling_list_enabled_flag" );                 pcSPS->setScalingListFlag ( uiCode );
842
843  if(pcSPS->getScalingListFlag())
844  {
845#if SCALINGLIST_INFERRING
846    if( pcSPS->getLayerId() > 0 )
847    {
848      READ_FLAG( uiCode, "sps_infer_scaling_list_flag" ); pcSPS->setInferScalingListFlag( uiCode );
849    }
850
851    if( pcSPS->getInferScalingListFlag() )
852    {
853      READ_UVLC( uiCode, "sps_scaling_list_ref_layer_id" ); pcSPS->setScalingListRefLayerId( uiCode );
854
855      // The value of pps_scaling_list_ref_layer_id shall be in the range of 0 to 62, inclusive
856      assert( pcSPS->getScalingListRefLayerId() <= 62 );
857
858      pcSPS->setScalingListPresentFlag( false );
859    }
860    else
861    {
862#endif
863      READ_FLAG( uiCode, "sps_scaling_list_data_present_flag" );                 pcSPS->setScalingListPresentFlag ( uiCode );
864      if(pcSPS->getScalingListPresentFlag ())
865      {
866        parseScalingList( pcSPS->getScalingList() );
867      }
868#if SCALINGLIST_INFERRING
869    }
870#endif
871  }
872  READ_FLAG( uiCode, "amp_enabled_flag" );                          pcSPS->setUseAMP( uiCode );
873  READ_FLAG( uiCode, "sample_adaptive_offset_enabled_flag" );       pcSPS->setUseSAO ( uiCode ? true : false );
874
875  READ_FLAG( uiCode, "pcm_enabled_flag" ); pcSPS->setUsePCM( uiCode ? true : false );
876  if( pcSPS->getUsePCM() )
877  {
878    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_luma_minus1" );          pcSPS->setPCMBitDepthLuma   ( 1 + uiCode );
879    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_chroma_minus1" );        pcSPS->setPCMBitDepthChroma ( 1 + uiCode );
880    READ_UVLC( uiCode, "log2_min_pcm_luma_coding_block_size_minus3" );   pcSPS->setPCMLog2MinSize (uiCode+3);
881    READ_UVLC( uiCode, "log2_diff_max_min_pcm_luma_coding_block_size" ); pcSPS->setPCMLog2MaxSize ( uiCode+pcSPS->getPCMLog2MinSize() );
882    READ_FLAG( uiCode, "pcm_loop_filter_disable_flag" );                 pcSPS->setPCMFilterDisableFlag ( uiCode ? true : false );
883  }
884
885  READ_UVLC( uiCode, "num_short_term_ref_pic_sets" );
886  assert(uiCode <= 64);
887  pcSPS->createRPSList(uiCode);
888
889  TComRPSList* rpsList = pcSPS->getRPSList();
890  TComReferencePictureSet* rps;
891
892  for(UInt i=0; i< rpsList->getNumberOfReferencePictureSets(); i++)
893  {
894    rps = rpsList->getReferencePictureSet(i);
895    parseShortTermRefPicSet(pcSPS,rps,i);
896  }
897  READ_FLAG( uiCode, "long_term_ref_pics_present_flag" );          pcSPS->setLongTermRefsPresent(uiCode);
898  if (pcSPS->getLongTermRefsPresent())
899  {
900    READ_UVLC( uiCode, "num_long_term_ref_pic_sps" );
901    pcSPS->setNumLongTermRefPicSPS(uiCode);
902    for (UInt k = 0; k < pcSPS->getNumLongTermRefPicSPS(); k++)
903    {
904      READ_CODE( pcSPS->getBitsForPOC(), uiCode, "lt_ref_pic_poc_lsb_sps" );
905      pcSPS->setLtRefPicPocLsbSps(k, uiCode);
906      READ_FLAG( uiCode,  "used_by_curr_pic_lt_sps_flag[i]");
907      pcSPS->setUsedByCurrPicLtSPSFlag(k, uiCode?1:0);
908    }
909  }
910  READ_FLAG( uiCode, "sps_temporal_mvp_enable_flag" );            pcSPS->setTMVPFlagsPresent(uiCode);
911  READ_FLAG( uiCode, "sps_strong_intra_smoothing_enable_flag" );  pcSPS->setUseStrongIntraSmoothing(uiCode);
912
913  READ_FLAG( uiCode, "vui_parameters_present_flag" );             pcSPS->setVuiParametersPresentFlag(uiCode);
914
915  if (pcSPS->getVuiParametersPresentFlag())
916  {
917    parseVUI(pcSPS->getVuiParameters(), pcSPS);
918  }
919
920  READ_FLAG( uiCode, "sps_extension_flag");
921
922#if SVC_EXTENSION
923  pcSPS->setExtensionFlag( uiCode ? true : false );
924
925  if( pcSPS->getExtensionFlag() )
926  {
927#if O0142_CONDITIONAL_SPS_EXTENSION
928    UInt spsExtensionTypeFlag[8];
929    for (UInt i = 0; i < 8; i++)
930    {
931      READ_FLAG( spsExtensionTypeFlag[i], "sps_extension_type_flag" );
932    }
933    if (spsExtensionTypeFlag[1])
934    {
935      parseSPSExtension( pcSPS );
936    }
937    if (spsExtensionTypeFlag[7])
938    {
939#else
940    parseSPSExtension( pcSPS );
941    READ_FLAG( uiCode, "sps_extension2_flag");
942    if(uiCode)
943    {
944#endif
945      while ( xMoreRbspData() )
946      {
947        READ_FLAG( uiCode, "sps_extension_data_flag");
948      }
949    }
950  }
951#else
952  if (uiCode)
953  {
954    while ( xMoreRbspData() )
955    {
956      READ_FLAG( uiCode, "sps_extension_data_flag");
957    }
958  }
959#endif
960}
961
962#if SVC_EXTENSION
963Void TDecCavlc::parseSPSExtension( TComSPS* pcSPS )
964{
965  UInt uiCode;
966  // more syntax elements to be parsed here
967
968  READ_FLAG( uiCode, "inter_view_mv_vert_constraint_flag" );
969  // Vertical MV component restriction is not used in SHVC CTC
970  assert( uiCode == 0 );
971
972#if !MOVE_SCALED_OFFSET_TO_PPS
973  if( pcSPS->getLayerId() > 0 )
974  {
975    Int iCode;
976    READ_UVLC( uiCode,      "num_scaled_ref_layer_offsets" ); pcSPS->setNumScaledRefLayerOffsets(uiCode);
977    for(Int i = 0; i < pcSPS->getNumScaledRefLayerOffsets(); i++)
978    {
979      Window& scaledWindow = pcSPS->getScaledRefLayerWindow(i);
980#if O0098_SCALED_REF_LAYER_ID
981      READ_CODE( 6,  uiCode,  "scaled_ref_layer_id" );       pcSPS->setScaledRefLayerId( i, uiCode );
982#endif
983      READ_SVLC( iCode, "scaled_ref_layer_left_offset" );    scaledWindow.setWindowLeftOffset  (iCode << 1);
984      READ_SVLC( iCode, "scaled_ref_layer_top_offset" );     scaledWindow.setWindowTopOffset   (iCode << 1);
985      READ_SVLC( iCode, "scaled_ref_layer_right_offset" );   scaledWindow.setWindowRightOffset (iCode << 1);
986      READ_SVLC( iCode, "scaled_ref_layer_bottom_offset" );  scaledWindow.setWindowBottomOffset(iCode << 1);
987#if P0312_VERT_PHASE_ADJ
988      READ_FLAG( uiCode, "vert_phase_position_enable_flag" ); scaledWindow.setVertPhasePositionEnableFlag(uiCode);  pcSPS->setVertPhasePositionEnableFlag( pcSPS->getScaledRefLayerId(i), uiCode);   
989#endif
990    }
991  }
992#endif
993}
994#endif
995
996Void TDecCavlc::parseVPS(TComVPS* pcVPS)
997{
998  UInt  uiCode;
999
1000  READ_CODE( 4,  uiCode,  "vps_video_parameter_set_id" );         pcVPS->setVPSId( uiCode );
1001#if VPS_RESERVED_FLAGS
1002  READ_FLAG( uiCode, "vps_base_layer_internal_flag");             pcVPS->setBaseLayerInternalFlag( uiCode ? true : false );
1003  READ_FLAG( uiCode, "vps_base_layer_available_flag");            pcVPS->setBaseLayerAvailableFlag( uiCode ? true : false );
1004#if VPS_AVC_BL_FLAG_REMOVAL
1005  pcVPS->setNonHEVCBaseLayerFlag( (pcVPS->getBaseLayerAvailableFlag() && !pcVPS->getBaseLayerInternalFlag()) ? true : false);
1006#endif
1007#else
1008  READ_CODE( 2,  uiCode,  "vps_reserved_three_2bits" );           assert(uiCode == 3);
1009#endif
1010#if SVC_EXTENSION
1011#if O0137_MAX_LAYERID
1012  READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );              pcVPS->setMaxLayers( min( 62u, uiCode) + 1 );
1013#else
1014  READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );              pcVPS->setMaxLayers( uiCode + 1 );
1015#endif
1016#else
1017  READ_CODE( 6,  uiCode,  "vps_reserved_zero_6bits" );            assert(uiCode == 0);
1018#endif
1019  READ_CODE( 3,  uiCode,  "vps_max_sub_layers_minus1" );          pcVPS->setMaxTLayers( uiCode + 1 ); assert(uiCode+1 <= MAX_TLAYER);
1020  READ_FLAG(     uiCode,  "vps_temporal_id_nesting_flag" );       pcVPS->setTemporalNestingFlag( uiCode ? true:false );
1021  assert (pcVPS->getMaxTLayers()>1||pcVPS->getTemporalNestingFlag());
1022#if !P0125_REVERT_VPS_EXTN_OFFSET_TO_RESERVED
1023#if VPS_EXTN_OFFSET
1024  READ_CODE( 16, uiCode,  "vps_extension_offset" );               pcVPS->setExtensionOffset( uiCode );
1025#else
1026  READ_CODE( 16, uiCode,  "vps_reserved_ffff_16bits" );           assert(uiCode == 0xffff);
1027#endif
1028#else
1029  READ_CODE( 16, uiCode,  "vps_reserved_ffff_16bits" );           assert(uiCode == 0xffff);
1030#endif
1031  parsePTL ( pcVPS->getPTL(), true, pcVPS->getMaxTLayers()-1);
1032  UInt subLayerOrderingInfoPresentFlag;
1033  READ_FLAG(subLayerOrderingInfoPresentFlag, "vps_sub_layer_ordering_info_present_flag");
1034  for(UInt i = 0; i <= pcVPS->getMaxTLayers()-1; i++)
1035  {
1036    READ_UVLC( uiCode,  "vps_max_dec_pic_buffering_minus1[i]" );     pcVPS->setMaxDecPicBuffering( uiCode + 1, i );
1037    READ_UVLC( uiCode,  "vps_num_reorder_pics[i]" );          pcVPS->setNumReorderPics( uiCode, i );
1038    READ_UVLC( uiCode,  "vps_max_latency_increase_plus1[i]" );      pcVPS->setMaxLatencyIncrease( uiCode, i );
1039
1040    if (!subLayerOrderingInfoPresentFlag)
1041    {
1042      for (i++; i <= pcVPS->getMaxTLayers()-1; i++)
1043      {
1044        pcVPS->setMaxDecPicBuffering(pcVPS->getMaxDecPicBuffering(0), i);
1045        pcVPS->setNumReorderPics(pcVPS->getNumReorderPics(0), i);
1046        pcVPS->setMaxLatencyIncrease(pcVPS->getMaxLatencyIncrease(0), i);
1047      }
1048      break;
1049    }
1050  }
1051
1052#if SVC_EXTENSION
1053  assert( pcVPS->getNumHrdParameters() < MAX_VPS_LAYER_SETS_PLUS1 );
1054  assert( pcVPS->getMaxLayerId()       < MAX_VPS_LAYER_ID_PLUS1 );
1055  READ_CODE( 6, uiCode, "vps_max_layer_id" );           pcVPS->setMaxLayerId( uiCode );
1056#if Q0078_ADD_LAYER_SETS
1057  READ_UVLC(uiCode, "vps_num_layer_sets_minus1");  pcVPS->setVpsNumLayerSetsMinus1(uiCode);
1058  pcVPS->setNumLayerSets(pcVPS->getVpsNumLayerSetsMinus1() + 1);
1059  for (UInt opsIdx = 1; opsIdx <= pcVPS->getVpsNumLayerSetsMinus1(); opsIdx++)
1060#else
1061  READ_UVLC(    uiCode, "vps_num_layer_sets_minus1" );  pcVPS->setNumLayerSets( uiCode + 1 );
1062  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getNumLayerSets() - 1 ); opsIdx ++ )
1063#endif
1064  {
1065    // Operation point set
1066    for( UInt i = 0; i <= pcVPS->getMaxLayerId(); i ++ )
1067#else
1068  assert( pcVPS->getNumHrdParameters() < MAX_VPS_OP_SETS_PLUS1 );
1069  assert( pcVPS->getMaxNuhReservedZeroLayerId() < MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1 );
1070  READ_CODE( 6, uiCode, "vps_max_nuh_reserved_zero_layer_id" );   pcVPS->setMaxNuhReservedZeroLayerId( uiCode );
1071  READ_UVLC(    uiCode, "vps_max_op_sets_minus1" );               pcVPS->setMaxOpSets( uiCode + 1 );
1072  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getMaxOpSets() - 1 ); opsIdx ++ )
1073  {
1074    // Operation point set
1075    for( UInt i = 0; i <= pcVPS->getMaxNuhReservedZeroLayerId(); i ++ )
1076#endif
1077    {
1078      READ_FLAG( uiCode, "layer_id_included_flag[opsIdx][i]" );   pcVPS->setLayerIdIncludedFlag( uiCode == 1 ? true : false, opsIdx, i );
1079    }
1080  }
1081#if DERIVE_LAYER_ID_LIST_VARIABLES
1082  pcVPS->deriveLayerIdListVariables();
1083#endif
1084  TimingInfo *timingInfo = pcVPS->getTimingInfo();
1085  READ_FLAG(       uiCode, "vps_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
1086  if(timingInfo->getTimingInfoPresentFlag())
1087  {
1088    READ_CODE( 32, uiCode, "vps_num_units_in_tick");                timingInfo->setNumUnitsInTick             (uiCode);
1089    READ_CODE( 32, uiCode, "vps_time_scale");                       timingInfo->setTimeScale                  (uiCode);
1090    READ_FLAG(     uiCode, "vps_poc_proportional_to_timing_flag");  timingInfo->setPocProportionalToTimingFlag(uiCode ? true : false);
1091    if(timingInfo->getPocProportionalToTimingFlag())
1092    {
1093      READ_UVLC(   uiCode, "vps_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
1094    }
1095    READ_UVLC( uiCode, "vps_num_hrd_parameters" );                  pcVPS->setNumHrdParameters( uiCode );
1096
1097    if( pcVPS->getNumHrdParameters() > 0 )
1098    {
1099      pcVPS->createHrdParamBuffer();
1100    }
1101    for( UInt i = 0; i < pcVPS->getNumHrdParameters(); i ++ )
1102    {
1103      READ_UVLC( uiCode, "hrd_op_set_idx" );                       pcVPS->setHrdOpSetIdx( uiCode, i );
1104      if( i > 0 )
1105      {
1106        READ_FLAG( uiCode, "cprms_present_flag[i]" );               pcVPS->setCprmsPresentFlag( uiCode == 1 ? true : false, i );
1107      }
1108      else
1109      {
1110        pcVPS->setCprmsPresentFlag( true, i );
1111      }
1112
1113      parseHrdParameters(pcVPS->getHrdParameters(i), pcVPS->getCprmsPresentFlag( i ), pcVPS->getMaxTLayers() - 1);
1114    }
1115  }
1116
1117#if SVC_EXTENSION
1118  READ_FLAG( uiCode,  "vps_extension_flag" );      pcVPS->setVpsExtensionFlag( uiCode ? true : false );
1119
1120  // When MaxLayersMinus1 is greater than 0, vps_extension_flag shall be equal to 1.
1121  if( pcVPS->getMaxLayers() > 1 )
1122  {
1123    assert( pcVPS->getVpsExtensionFlag() == true );
1124  }
1125
1126  if( pcVPS->getVpsExtensionFlag()  )
1127  {
1128    while ( m_pcBitstream->getNumBitsRead() % 8 != 0 )
1129    {
1130      READ_FLAG( uiCode, "vps_extension_alignment_bit_equal_to_one"); assert(uiCode == 1);
1131    }
1132    parseVPSExtension(pcVPS);
1133    READ_FLAG( uiCode, "vps_entension2_flag" );
1134    if(uiCode)
1135    {
1136      while ( xMoreRbspData() )
1137      {
1138        READ_FLAG( uiCode, "vps_extension_data_flag");
1139      }
1140    }
1141  }
1142  else
1143  {
1144    // set default parameters when syntax elements are not present
1145    defaultVPSExtension(pcVPS);   
1146  }
1147#else
1148  READ_FLAG( uiCode,  "vps_extension_flag" );
1149  if (uiCode)
1150  {
1151    while ( xMoreRbspData() )
1152    {
1153      READ_FLAG( uiCode, "vps_extension_data_flag");
1154    }
1155  }
1156#endif
1157
1158  return;
1159}
1160
1161#if SVC_EXTENSION
1162Void TDecCavlc::parseVPSExtension(TComVPS *vps)
1163{
1164  UInt uiCode;
1165  // ... More syntax elements to be parsed here
1166#if P0300_ALT_OUTPUT_LAYER_FLAG
1167  Int NumOutputLayersInOutputLayerSet[MAX_VPS_LAYER_SETS_PLUS1];
1168  Int OlsHighestOutputLayerId[MAX_VPS_LAYER_SETS_PLUS1];
1169#endif
1170#if LIST_OF_PTL
1171  if( vps->getMaxLayers() > 1 && vps->getBaseLayerInternalFlag() )
1172  {
1173    vps->setProfilePresentFlag(1, false);
1174    vps->getPTLForExtnPtr()->empty();
1175    vps->getPTLForExtnPtr()->resize(2);
1176    vps->getPTLForExtn(1)->copyProfileInfo( vps->getPTL() );
1177    parsePTL( vps->getPTLForExtn(1), vps->getProfilePresentFlag(1), vps->getMaxTLayers() - 1 );
1178  }
1179#endif
1180#if VPS_EXTN_MASK_AND_DIM_INFO
1181  UInt numScalabilityTypes = 0, i = 0, j = 0;
1182
1183#if !VPS_AVC_BL_FLAG_REMOVAL
1184  READ_FLAG( uiCode, "avc_base_layer_flag" ); vps->setAvcBaseLayerFlag(uiCode ? true : false);
1185#endif
1186
1187#if !P0307_REMOVE_VPS_VUI_OFFSET
1188#if O0109_MOVE_VPS_VUI_FLAG
1189  READ_FLAG( uiCode, "vps_vui_present_flag"); vps->setVpsVuiPresentFlag(uiCode ? true : false);
1190  if ( uiCode )
1191  {
1192#endif
1193#if VPS_VUI_OFFSET
1194    READ_CODE( 16, uiCode, "vps_vui_offset" );  vps->setVpsVuiOffset( uiCode );
1195#endif
1196#if O0109_MOVE_VPS_VUI_FLAG
1197  }
1198#endif
1199#endif
1200  READ_FLAG( uiCode, "splitting_flag" ); vps->setSplittingFlag(uiCode ? true : false);
1201
1202  for(i = 0; i < MAX_VPS_NUM_SCALABILITY_TYPES; i++)
1203  {
1204    READ_FLAG( uiCode, "scalability_mask[i]" ); vps->setScalabilityMask(i, uiCode ? true : false);
1205    numScalabilityTypes += uiCode;
1206  }
1207  vps->setNumScalabilityTypes(numScalabilityTypes);
1208
1209  for(j = 0; j < numScalabilityTypes - vps->getSplittingFlag(); j++)
1210  {
1211    READ_CODE( 3, uiCode, "dimension_id_len_minus1[j]" ); vps->setDimensionIdLen(j, uiCode + 1);
1212  }
1213
1214  // The value of dimBitOffset[ NumScalabilityTypes ] is set equal to 6.
1215  if(vps->getSplittingFlag())
1216  {
1217    UInt numBits = 0;
1218    for(j = 0; j < numScalabilityTypes - 1; j++)
1219    {
1220      numBits += vps->getDimensionIdLen(j);
1221    }
1222    assert( numBits < 6 );
1223    vps->setDimensionIdLen(numScalabilityTypes-1, 6 - numBits);
1224    numBits = 6;
1225  }
1226
1227  READ_FLAG( uiCode, "vps_nuh_layer_id_present_flag" ); vps->setNuhLayerIdPresentFlag(uiCode ? true : false);
1228  vps->setLayerIdInNuh(0, 0);
1229  vps->setLayerIdInVps(0, 0);
1230  for(i = 1; i < vps->getMaxLayers(); i++)
1231  {
1232    if( vps->getNuhLayerIdPresentFlag() )
1233    {
1234      READ_CODE( 6, uiCode, "layer_id_in_nuh[i]" ); vps->setLayerIdInNuh(i, uiCode);
1235      assert( uiCode > vps->getLayerIdInNuh(i-1) );
1236    }
1237    else
1238    {
1239      vps->setLayerIdInNuh(i, i);
1240    }
1241    vps->setLayerIdInVps(vps->getLayerIdInNuh(i), i);
1242
1243    if( !vps->getSplittingFlag() )
1244    {
1245      for(j = 0; j < numScalabilityTypes; j++)
1246      {
1247        READ_CODE( vps->getDimensionIdLen(j), uiCode, "dimension_id[i][j]" ); vps->setDimensionId(i, j, uiCode);
1248#if !AUXILIARY_PICTURES
1249        assert( uiCode <= vps->getMaxLayerId() );
1250#endif
1251      }
1252    }
1253  }
1254#endif
1255#if VIEW_ID_RELATED_SIGNALING
1256  // if ( pcVPS->getNumViews() > 1 )
1257  //   However, this is a bug in the text since, view_id_len_minus1 is needed to parse view_id_val.
1258  {
1259#if O0109_VIEW_ID_LEN
1260    READ_CODE( 4, uiCode, "view_id_len" ); vps->setViewIdLen( uiCode );
1261#else
1262    READ_CODE( 4, uiCode, "view_id_len_minus1" ); vps->setViewIdLenMinus1( uiCode );
1263#endif
1264  }
1265
1266#if O0109_VIEW_ID_LEN
1267  if ( vps->getViewIdLen() > 0 )
1268  {
1269    for(  i = 0; i < vps->getNumViews(); i++ )
1270    {
1271      READ_CODE( vps->getViewIdLen( ), uiCode, "view_id_val[i]" ); vps->setViewIdVal( i, uiCode );
1272    }
1273  }
1274#else
1275  for(  i = 0; i < vps->getNumViews(); i++ )
1276  {
1277    READ_CODE( vps->getViewIdLenMinus1( ) + 1, uiCode, "view_id_val[i]" ); vps->setViewIdVal( i, uiCode );
1278  }
1279#endif
1280#endif // view id related signaling
1281#if VPS_EXTN_DIRECT_REF_LAYERS
1282  // For layer 0
1283  vps->setNumDirectRefLayers(0, 0);
1284  // For other layers
1285  for( Int layerCtr = 1; layerCtr <= vps->getMaxLayers() - 1; layerCtr++)
1286  {
1287    UInt numDirectRefLayers = 0;
1288    for( Int refLayerCtr = 0; refLayerCtr < layerCtr; refLayerCtr++)
1289    {
1290      READ_FLAG(uiCode, "direct_dependency_flag[i][j]" ); vps->setDirectDependencyFlag(layerCtr, refLayerCtr, uiCode? true : false);
1291      if(uiCode)
1292      {
1293        vps->setRefLayerId(layerCtr, numDirectRefLayers, refLayerCtr);
1294        numDirectRefLayers++;
1295      }
1296    }
1297    vps->setNumDirectRefLayers(layerCtr, numDirectRefLayers);
1298  }
1299#endif
1300#if Q0078_ADD_LAYER_SETS
1301#if O0092_0094_DEPENDENCY_CONSTRAINT // Moved here
1302  vps->setNumRefLayers();
1303
1304  if (vps->getMaxLayers() > MAX_REF_LAYERS)
1305  {
1306    for (i = 1; i < vps->getMaxLayers(); i++)
1307    {
1308      assert(vps->getNumRefLayers(vps->getLayerIdInNuh(i)) <= MAX_REF_LAYERS);
1309    }
1310  }
1311#endif
1312  vps->setPredictedLayerIds();
1313  vps->setTreePartitionLayerIdList();
1314#endif
1315#if MOVE_ADDN_LS_SIGNALLING
1316#if Q0078_ADD_LAYER_SETS
1317  if (vps->getNumIndependentLayers() > 1)
1318  {
1319    READ_UVLC(uiCode, "num_add_layer_sets"); vps->setNumAddLayerSets(uiCode);
1320    for (i = 0; i < vps->getNumAddLayerSets(); i++)
1321    {
1322      for (j = 1; j < vps->getNumIndependentLayers(); j++)
1323      {
1324        int len = 1;
1325        while ((1 << len) < (vps->getNumLayersInTreePartition(j) + 1))
1326        {
1327          len++;
1328        }
1329        READ_CODE(len, uiCode, "highest_layer_idx_plus1[i][j]"); vps->setHighestLayerIdxPlus1(i, j, uiCode);
1330      }
1331    }
1332    vps->setNumLayerSets(vps->getNumLayerSets() + vps->getNumAddLayerSets());
1333    vps->setLayerIdIncludedFlagsForAddLayerSets();
1334  }
1335#endif
1336#endif
1337#if VPS_TSLAYERS
1338  READ_FLAG( uiCode, "vps_sub_layers_max_minus1_present_flag"); vps->setMaxTSLayersPresentFlag(uiCode ? true : false);
1339
1340  if (vps->getMaxTSLayersPresentFlag())
1341  {
1342    for(i = 0; i < vps->getMaxLayers(); i++)
1343    {
1344      READ_CODE( 3, uiCode, "sub_layers_vps_max_minus1[i]" ); vps->setMaxTSLayersMinus1(i, uiCode);
1345    }
1346  }
1347  else
1348  {
1349    for( i = 0; i < vps->getMaxLayers(); i++)
1350    {
1351      vps->setMaxTSLayersMinus1(i, vps->getMaxTLayers()-1);
1352    }
1353  }
1354#endif
1355  READ_FLAG( uiCode, "max_tid_ref_present_flag"); vps->setMaxTidRefPresentFlag(uiCode ? true : false);
1356  if (vps->getMaxTidRefPresentFlag())
1357  {
1358    for(i = 0; i < vps->getMaxLayers() - 1; i++)
1359    {
1360#if O0225_MAX_TID_FOR_REF_LAYERS
1361      for( j = i+1; j <= vps->getMaxLayers() - 1; j++)
1362      {
1363        if(vps->getDirectDependencyFlag(j, i))
1364        {
1365          READ_CODE( 3, uiCode, "max_tid_il_ref_pics_plus1[i][j]" ); vps->setMaxTidIlRefPicsPlus1(i, j, uiCode);         
1366        }
1367      }
1368#else
1369      READ_CODE( 3, uiCode, "max_tid_il_ref_pics_plus1[i]" ); vps->setMaxTidIlRefPicsPlus1(i, uiCode);
1370      assert( uiCode <= vps->getMaxTLayers());
1371#endif
1372    }
1373  }
1374  else
1375  {
1376    for(i = 0; i < vps->getMaxLayers() - 1; i++)
1377    {
1378#if O0225_MAX_TID_FOR_REF_LAYERS
1379      for( j = i+1; j <= vps->getMaxLayers() - 1; j++)
1380      {
1381        vps->setMaxTidIlRefPicsPlus1(i, j, 7);
1382      }
1383#else
1384      vps->setMaxTidIlRefPicsPlus1(i, 7);
1385#endif
1386    }
1387  }
1388  READ_FLAG( uiCode, "all_ref_layers_active_flag" ); vps->setIlpSshSignalingEnabledFlag(uiCode ? true : false);
1389#if VPS_EXTN_PROFILE_INFO
1390  // Profile-tier-level signalling
1391#if !VPS_EXTN_UEV_CODING
1392  READ_CODE( 10, uiCode, "vps_number_layer_sets_minus1" );     assert( uiCode == (vps->getNumLayerSets() - 1) );
1393  READ_CODE(  6, uiCode, "vps_num_profile_tier_level_minus1"); vps->setNumProfileTierLevel( uiCode + 1 );
1394#else
1395  READ_UVLC(  uiCode, "vps_num_profile_tier_level_minus1"); vps->setNumProfileTierLevel( uiCode + 1 );
1396#endif
1397#if PER_LAYER_PTL
1398  Int const numBitsForPtlIdx = vps->calculateLenOfSyntaxElement( vps->getNumProfileTierLevel() );
1399#endif
1400  vps->getPTLForExtnPtr()->resize(vps->getNumProfileTierLevel());
1401#if LIST_OF_PTL
1402  for(Int idx = vps->getBaseLayerInternalFlag() ? 2 : 1; idx <= vps->getNumProfileTierLevel() - 1; idx++)
1403#else
1404  for(Int idx = 1; idx <= vps->getNumProfileTierLevel() - 1; idx++)
1405#endif
1406  {
1407    READ_FLAG( uiCode, "vps_profile_present_flag[i]" ); vps->setProfilePresentFlag(idx, uiCode ? true : false);
1408    if( !vps->getProfilePresentFlag(idx) )
1409    {
1410#if P0048_REMOVE_PROFILE_REF
1411      // Copy profile information from previous one
1412      vps->getPTLForExtn(idx)->copyProfileInfo( (idx==1) ? vps->getPTL() : vps->getPTLForExtn( idx - 1 ) );
1413#else
1414      READ_CODE( 6, uiCode, "profile_ref_minus1[i]" ); vps->setProfileLayerSetRef(idx, uiCode + 1);
1415#if O0109_PROF_REF_MINUS1
1416      assert( vps->getProfileLayerSetRef(idx) <= idx );
1417#else
1418      assert( vps->getProfileLayerSetRef(idx) < idx );
1419#endif
1420      // Copy profile information as indicated
1421      vps->getPTLForExtn(idx)->copyProfileInfo( vps->getPTLForExtn( vps->getProfileLayerSetRef(idx) ) );
1422#endif
1423    }
1424    parsePTL( vps->getPTLForExtn(idx), vps->getProfilePresentFlag(idx), vps->getMaxTLayers() - 1 );
1425  }
1426#endif
1427
1428#if !MOVE_ADDN_LS_SIGNALLING
1429#if Q0078_ADD_LAYER_SETS
1430  if (vps->getNumIndependentLayers() > 1)
1431  {
1432    READ_UVLC(uiCode, "num_add_layer_sets"); vps->setNumAddLayerSets(uiCode);
1433    for (i = 0; i < vps->getNumAddLayerSets(); i++)
1434    {
1435      for (j = 1; j < vps->getNumIndependentLayers(); j++)
1436      {
1437        int len = 1;
1438        while ((1 << len) < (vps->getNumLayersInTreePartition(j) + 1))
1439        {
1440          len++;
1441        }
1442        READ_CODE(len, uiCode, "highest_layer_idx_plus1[i][j]"); vps->setHighestLayerIdxPlus1(i, j, uiCode);
1443      }
1444    }
1445    vps->setNumLayerSets(vps->getNumLayerSets() + vps->getNumAddLayerSets());
1446    vps->setLayerIdIncludedFlagsForAddLayerSets();
1447  }
1448#endif
1449#endif
1450
1451#if !VPS_EXTN_UEV_CODING
1452  READ_FLAG( uiCode, "more_output_layer_sets_than_default_flag" ); vps->setMoreOutputLayerSetsThanDefaultFlag( uiCode ? true : false );
1453  Int numOutputLayerSets = 0;
1454  if(! vps->getMoreOutputLayerSetsThanDefaultFlag() )
1455  {
1456    numOutputLayerSets = vps->getNumLayerSets();
1457  }
1458  else
1459  {
1460    READ_CODE( 10, uiCode, "num_add_output_layer_sets" );          vps->setNumAddOutputLayerSets( uiCode );
1461    numOutputLayerSets = vps->getNumLayerSets() + vps->getNumAddOutputLayerSets();
1462  }
1463#else
1464
1465#if Q0165_NUM_ADD_OUTPUT_LAYER_SETS
1466  if( vps->getNumLayerSets() > 1 )
1467  {
1468    READ_UVLC( uiCode, "num_add_olss" );            vps->setNumAddOutputLayerSets( uiCode );
1469    READ_CODE( 2, uiCode, "default_output_layer_idc" );   vps->setDefaultTargetOutputLayerIdc( uiCode );
1470  }
1471  else
1472  {
1473    vps->setNumAddOutputLayerSets( 0 );
1474  }
1475#else
1476  READ_UVLC( uiCode, "num_add_output_layer_sets" );          vps->setNumAddOutputLayerSets( uiCode );
1477#endif
1478
1479  // The value of num_add_olss shall be in the range of 0 to 1023, inclusive.
1480  assert( vps->getNumAddOutputLayerSets() >= 0 && vps->getNumAddOutputLayerSets() < 1024 );
1481
1482  Int numOutputLayerSets = vps->getNumLayerSets() + vps->getNumAddOutputLayerSets();
1483#endif
1484
1485#if P0295_DEFAULT_OUT_LAYER_IDC
1486#if !Q0165_NUM_ADD_OUTPUT_LAYER_SETS
1487  if( numOutputLayerSets > 1 )
1488  {
1489    READ_CODE( 2, uiCode, "default_target_output_layer_idc" );   vps->setDefaultTargetOutputLayerIdc( uiCode );
1490  }
1491#endif
1492  vps->setNumOutputLayerSets( numOutputLayerSets );
1493#if NECESSARY_LAYER_FLAG
1494  // Default output layer set
1495  vps->setOutputLayerSetIdx(0, 0);
1496  vps->setOutputLayerFlag(0, 0, true);
1497  vps->deriveNecessaryLayerFlag(0);
1498#if PER_LAYER_PTL
1499  vps->getProfileLevelTierIdx()->resize(numOutputLayerSets);
1500  vps->getProfileLevelTierIdx(0)->push_back( vps->getBaseLayerInternalFlag() && vps->getMaxLayers() > 1 ? 1 : 0);
1501#endif
1502#endif
1503  for(i = 1; i < numOutputLayerSets; i++)
1504  {
1505    if( i > (vps->getNumLayerSets() - 1) )
1506    {
1507      Int numBits = 1;
1508      while ((1 << numBits) < (vps->getNumLayerSets() - 1))
1509      {
1510        numBits++;
1511      }
1512      READ_CODE( numBits, uiCode, "layer_set_idx_for_ols_minus1");   vps->setOutputLayerSetIdx( i, uiCode + 1);
1513    }
1514    else
1515    {
1516      vps->setOutputLayerSetIdx( i, i );
1517    }
1518    Int layerSetIdxForOutputLayerSet = vps->getOutputLayerSetIdx(i);
1519#if Q0078_ADD_LAYER_SETS
1520    if ( i > vps->getVpsNumLayerSetsMinus1() || vps->getDefaultTargetOutputLayerIdc() >= 2 )
1521#else
1522    if ( i > (vps->getNumLayerSets() - 1) || vps->getDefaultTargetOutputLayerIdc() >= 2 )
1523#endif
1524    {
1525#if NUM_OL_FLAGS
1526      for(j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++)
1527#else
1528      for(j = 0; j < vps->getNumLayersInIdList(lsIdx) - 1; j++)
1529#endif
1530      {
1531        READ_FLAG( uiCode, "output_layer_flag[i][j]"); vps->setOutputLayerFlag(i, j, uiCode);
1532      }
1533    }
1534    else
1535    {
1536      // i <= (vps->getNumLayerSets() - 1)
1537      // Assign OutputLayerFlag depending on default_one_target_output_layer_flag
1538      if( vps->getDefaultTargetOutputLayerIdc() == 1 )
1539      {
1540        for(j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++)
1541        {
1542          vps->setOutputLayerFlag(i, j, (j == (vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet)-1)) && (vps->getDimensionId(j,1) == 0) );
1543        }
1544      }
1545      else if ( vps->getDefaultTargetOutputLayerIdc() == 0 )
1546      {
1547        for(j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++)
1548        {
1549          vps->setOutputLayerFlag(i, j, 1);
1550        }
1551      }
1552    }
1553#if NECESSARY_LAYER_FLAG
1554    vps->deriveNecessaryLayerFlag(i); 
1555#endif
1556#if PER_LAYER_PTL
1557    vps->getProfileLevelTierIdx(i)->assign(vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet), -1);
1558    for(j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet) ; j++)
1559    {
1560      if( vps->getNecessaryLayerFlag(i, j) )
1561      {
1562        READ_CODE( numBitsForPtlIdx, uiCode, "profile_level_tier_idx[i]" ); 
1563        vps->setProfileLevelTierIdx(i, j, uiCode );
1564      }
1565    }
1566#else
1567    Int numBits = 1;
1568    while ((1 << numBits) < (vps->getNumProfileTierLevel()))
1569    {
1570      numBits++;
1571    }
1572    READ_CODE( numBits, uiCode, "profile_level_tier_idx[i]" );     vps->setProfileLevelTierIdx(i, uiCode);
1573#endif
1574#if P0300_ALT_OUTPUT_LAYER_FLAG
1575    NumOutputLayersInOutputLayerSet[i] = 0;
1576    for (j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet); j++)
1577    {
1578      NumOutputLayersInOutputLayerSet[i] += vps->getOutputLayerFlag(i, j);
1579      if (vps->getOutputLayerFlag(i, j))
1580      {
1581        OlsHighestOutputLayerId[i] = vps->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, j);
1582      }
1583    }
1584    if (NumOutputLayersInOutputLayerSet[i] == 1 && vps->getNumDirectRefLayers(OlsHighestOutputLayerId[i]) > 0)
1585    {
1586      READ_FLAG(uiCode, "alt_output_layer_flag[i]");
1587      vps->setAltOuputLayerFlag(i, uiCode ? true : false);
1588    }
1589#if Q0165_OUTPUT_LAYER_SET
1590    assert( NumOutputLayersInOutputLayerSet[i]>0 );
1591#endif
1592
1593#endif
1594  }
1595#if NECESSARY_LAYER_FLAG
1596  vps->checkNecessaryLayerFlagCondition(); 
1597#endif
1598#else
1599  if( numOutputLayerSets > 1 )
1600  {
1601#if O0109_DEFAULT_ONE_OUT_LAYER_IDC
1602    READ_CODE( 2, uiCode, "default_one_target_output_layer_idc" );   vps->setDefaultOneTargetOutputLayerIdc( uiCode );
1603#else
1604    READ_FLAG( uiCode, "default_one_target_output_layer_flag" );   vps->setDefaultOneTargetOutputLayerFlag( uiCode ? true : false );
1605#endif
1606  }
1607  vps->setNumOutputLayerSets( numOutputLayerSets );
1608
1609  for(i = 1; i < numOutputLayerSets; i++)
1610  {
1611    if( i > (vps->getNumLayerSets() - 1) )
1612    {
1613      Int numBits = 1;
1614      while ((1 << numBits) < (vps->getNumLayerSets() - 1))
1615      {
1616        numBits++;
1617      }
1618      READ_CODE( numBits, uiCode, "output_layer_set_idx_minus1");   vps->setOutputLayerSetIdx( i, uiCode + 1);
1619      Int lsIdx = vps->getOutputLayerSetIdx(i);
1620#if NUM_OL_FLAGS
1621      for(j = 0; j < vps->getNumLayersInIdList(lsIdx) ; j++)
1622#else
1623      for(j = 0; j < vps->getNumLayersInIdList(lsIdx) - 1; j++)
1624#endif
1625      {
1626        READ_FLAG( uiCode, "output_layer_flag[i][j]"); vps->setOutputLayerFlag(i, j, uiCode);
1627      }
1628    }
1629    else
1630    {
1631#if VPS_DPB_SIZE_TABLE
1632      vps->setOutputLayerSetIdx( i, i );
1633#endif
1634      // i <= (vps->getNumLayerSets() - 1)
1635      // Assign OutputLayerFlag depending on default_one_target_output_layer_flag
1636      Int lsIdx = i;
1637#if O0109_DEFAULT_ONE_OUT_LAYER_IDC
1638      if( vps->getDefaultOneTargetOutputLayerIdc() == 1 )
1639      {
1640        for(j = 0; j < vps->getNumLayersInIdList(lsIdx); j++)
1641        {
1642#if O0135_DEFAULT_ONE_OUT_SEMANTIC
1643          vps->setOutputLayerFlag(i, j, (j == (vps->getNumLayersInIdList(lsIdx)-1)) && (vps->getDimensionId(j,1)==0) );
1644#else
1645          vps->setOutputLayerFlag(i, j, (j == (vps->getNumLayersInIdList(lsIdx)-1)));
1646#endif
1647        }
1648      }
1649      else if ( vps->getDefaultOneTargetOutputLayerIdc() == 0 )
1650      {
1651        for(j = 0; j < vps->getNumLayersInIdList(lsIdx); j++)
1652        {
1653          vps->setOutputLayerFlag(i, j, 1);
1654        }
1655      }
1656      else
1657      {
1658        // Other values of default_one_target_output_layer_idc than 0 and 1 are reserved for future use.
1659      }
1660#else
1661      if( vps->getDefaultOneTargetOutputLayerFlag() )
1662      {
1663        for(j = 0; j < vps->getNumLayersInIdList(lsIdx); j++)
1664        {
1665          vps->setOutputLayerFlag(i, j, (j == (vps->getNumLayersInIdList(lsIdx)-1)));
1666        }
1667      }
1668      else
1669      {
1670        for(j = 0; j < vps->getNumLayersInIdList(lsIdx); j++)
1671        {
1672          vps->setOutputLayerFlag(i, j, 1);
1673        }
1674      }
1675#endif
1676    }
1677    Int numBits = 1;
1678    while ((1 << numBits) < (vps->getNumProfileTierLevel()))
1679    {
1680      numBits++;
1681    }
1682    READ_CODE( numBits, uiCode, "profile_level_tier_idx[i]" );     vps->setProfileLevelTierIdx(i, uiCode);
1683  }
1684#endif
1685
1686#if !P0300_ALT_OUTPUT_LAYER_FLAG
1687#if O0153_ALT_OUTPUT_LAYER_FLAG
1688  if( vps->getMaxLayers() > 1 )
1689  {
1690    READ_FLAG( uiCode, "alt_output_layer_flag");
1691    vps->setAltOuputLayerFlag( uiCode ? true : false );
1692  }
1693#endif
1694#endif
1695
1696#if REPN_FORMAT_IN_VPS
1697#if Q0195_REP_FORMAT_CLEANUP
1698  READ_UVLC( uiCode, "vps_num_rep_formats_minus1" );
1699  vps->setVpsNumRepFormats( uiCode + 1 );
1700
1701  // The value of vps_num_rep_formats_minus1 shall be in the range of 0 to 255, inclusive.
1702  assert( vps->getVpsNumRepFormats() > 0 && vps->getVpsNumRepFormats() <= 256 );
1703
1704  for(i = 0; i < vps->getVpsNumRepFormats(); i++)
1705  {
1706    // Read rep_format_structures
1707    parseRepFormat( vps->getVpsRepFormat(i), i > 0 ? vps->getVpsRepFormat(i-1) : 0 );
1708  }
1709
1710  // Default assignment for layer 0
1711  vps->setVpsRepFormatIdx( 0, 0 );
1712
1713  if( vps->getVpsNumRepFormats() > 1 )
1714  {
1715    READ_FLAG( uiCode, "rep_format_idx_present_flag");
1716    vps->setRepFormatIdxPresentFlag( uiCode ? true : false );
1717  }
1718  else
1719  {
1720    // When not present, the value of rep_format_idx_present_flag is inferred to be equal to 0
1721    vps->setRepFormatIdxPresentFlag( false );
1722  }
1723
1724  if( vps->getRepFormatIdxPresentFlag() )
1725  {
1726    for(i = 1; i < vps->getMaxLayers(); i++)
1727    {
1728      Int numBits = 1;
1729      while ((1 << numBits) < (vps->getVpsNumRepFormats()))
1730      {
1731        numBits++;
1732      }
1733      READ_CODE( numBits, uiCode, "vps_rep_format_idx[i]" );
1734      vps->setVpsRepFormatIdx( i, uiCode );
1735    }
1736  }
1737  else
1738  {
1739    // When not present, the value of vps_rep_format_idx[ i ] is inferred to be equal to Min (i, vps_num_rep_formats_minus1)
1740    for(i = 1; i < vps->getMaxLayers(); i++)
1741    {
1742      vps->setVpsRepFormatIdx( i, min( (Int)i, vps->getVpsNumRepFormats()-1 ) );
1743    }
1744  }
1745#else
1746  READ_FLAG( uiCode, "rep_format_idx_present_flag");
1747  vps->setRepFormatIdxPresentFlag( uiCode ? true : false );
1748
1749  if( vps->getRepFormatIdxPresentFlag() )
1750  {
1751#if O0096_REP_FORMAT_INDEX
1752#if !VPS_EXTN_UEV_CODING
1753    READ_CODE( 8, uiCode, "vps_num_rep_formats_minus1" );
1754#else
1755    READ_UVLC( uiCode, "vps_num_rep_formats_minus1" );
1756#endif
1757#else
1758    READ_CODE( 4, uiCode, "vps_num_rep_formats_minus1" );
1759#endif
1760    vps->setVpsNumRepFormats( uiCode + 1 );
1761  }
1762  else
1763  {
1764    // default assignment
1765    assert (vps->getMaxLayers() <= 16);       // If max_layers_is more than 15, num_rep_formats has to be signaled
1766    vps->setVpsNumRepFormats( vps->getMaxLayers() );
1767  }
1768
1769  // The value of vps_num_rep_formats_minus1 shall be in the range of 0 to 255, inclusive.
1770  assert( vps->getVpsNumRepFormats() > 0 && vps->getVpsNumRepFormats() <= 256 );
1771
1772  for(i = 0; i < vps->getVpsNumRepFormats(); i++)
1773  {
1774    // Read rep_format_structures
1775    parseRepFormat( vps->getVpsRepFormat(i), i > 0 ? vps->getVpsRepFormat(i-1) : 0 );
1776  }
1777
1778  // Default assignment for layer 0
1779  vps->setVpsRepFormatIdx( 0, 0 );
1780  if( vps->getRepFormatIdxPresentFlag() )
1781  {
1782    for(i = 1; i < vps->getMaxLayers(); i++)
1783    {
1784      if( vps->getVpsNumRepFormats() > 1 )
1785      {
1786#if O0096_REP_FORMAT_INDEX
1787#if !VPS_EXTN_UEV_CODING
1788        READ_CODE( 8, uiCode, "vps_rep_format_idx[i]" );
1789#else
1790        Int numBits = 1;
1791        while ((1 << numBits) < (vps->getVpsNumRepFormats()))
1792        {
1793          numBits++;
1794        }
1795        READ_CODE( numBits, uiCode, "vps_rep_format_idx[i]" );
1796#endif
1797#else
1798        READ_CODE( 4, uiCode, "vps_rep_format_idx[i]" );
1799#endif
1800        vps->setVpsRepFormatIdx( i, uiCode );
1801      }
1802      else
1803      {
1804        // default assignment - only one rep_format() structure
1805        vps->setVpsRepFormatIdx( i, 0 );
1806      }
1807    }
1808  }
1809  else
1810  {
1811    // default assignment - each layer assigned each rep_format() structure in the order signaled
1812    for(i = 1; i < vps->getMaxLayers(); i++)
1813    {
1814      vps->setVpsRepFormatIdx( i, i );
1815    }
1816  }
1817#endif
1818#endif
1819#if RESOLUTION_BASED_DPB
1820  vps->assignSubDpbIndices();
1821#endif
1822  READ_FLAG(uiCode, "max_one_active_ref_layer_flag" );
1823  vps->setMaxOneActiveRefLayerFlag(uiCode);
1824#if O0062_POC_LSB_NOT_PRESENT_FLAG
1825  for(i = 1; i< vps->getMaxLayers(); i++)
1826  {
1827    if( vps->getNumDirectRefLayers( vps->getLayerIdInNuh(i) ) == 0  )
1828    {
1829      READ_FLAG(uiCode, "poc_lsb_not_present_flag[i]");
1830      vps->setPocLsbNotPresentFlag(i, uiCode);
1831    }
1832  }
1833#endif
1834#if O0215_PHASE_ALIGNMENT
1835  READ_FLAG( uiCode, "cross_layer_phase_alignment_flag"); vps->setPhaseAlignFlag( uiCode == 1 ? true : false );
1836#endif
1837
1838#if !IRAP_ALIGN_FLAG_IN_VPS_VUI
1839  READ_FLAG(uiCode, "cross_layer_irap_aligned_flag" );
1840  vps->setCrossLayerIrapAlignFlag(uiCode);
1841#endif
1842
1843#if VPS_DPB_SIZE_TABLE
1844  parseVpsDpbSizeTable(vps);
1845#endif
1846
1847#if VPS_EXTN_DIRECT_REF_LAYERS
1848  READ_UVLC( uiCode,           "direct_dep_type_len_minus2"); vps->setDirectDepTypeLen(uiCode+2);
1849#if O0096_DEFAULT_DEPENDENCY_TYPE
1850  READ_FLAG(uiCode, "default_direct_dependency_type_flag"); 
1851  vps->setDefaultDirectDependecyTypeFlag(uiCode == 1? true : false);
1852  if (vps->getDefaultDirectDependencyTypeFlag())
1853  {
1854    READ_CODE( vps->getDirectDepTypeLen(), uiCode, "default_direct_dependency_type" ); 
1855    vps->setDefaultDirectDependecyType(uiCode);
1856  }
1857#endif
1858  for(i = 1; i < vps->getMaxLayers(); i++)
1859  {
1860    for(j = 0; j < i; j++)
1861    {
1862      if (vps->getDirectDependencyFlag(i, j))
1863      {
1864#if O0096_DEFAULT_DEPENDENCY_TYPE
1865        if (vps->getDefaultDirectDependencyTypeFlag())
1866        {
1867          vps->setDirectDependencyType(i, j, vps->getDefaultDirectDependencyType());
1868        }
1869        else
1870        {
1871          READ_CODE( vps->getDirectDepTypeLen(), uiCode, "direct_dependency_type[i][j]" ); 
1872          vps->setDirectDependencyType(i, j, uiCode);
1873        }
1874#else
1875        READ_CODE( vps->getDirectDepTypeLen(), uiCode, "direct_dependency_type[i][j]" ); 
1876        vps->setDirectDependencyType(i, j, uiCode);
1877#endif
1878      }
1879    }
1880  }
1881#endif
1882#if !Q0078_ADD_LAYER_SETS
1883#if O0092_0094_DEPENDENCY_CONSTRAINT // Moved up
1884  vps->setNumRefLayers();
1885
1886  if(vps->getMaxLayers() > MAX_REF_LAYERS)
1887  {
1888    for(i = 1;i < vps->getMaxLayers(); i++)
1889    {
1890      assert( vps->getNumRefLayers(vps->getLayerIdInNuh(i)) <= MAX_REF_LAYERS);
1891    }
1892  }
1893#endif
1894#endif
1895
1896#if P0307_VPS_NON_VUI_EXTENSION
1897  READ_UVLC( uiCode,           "vps_non_vui_extension_length"); vps->setVpsNonVuiExtLength((Int)uiCode);
1898
1899  // The value of vps_non_vui_extension_length shall be in the range of 0 to 4096, inclusive.
1900  assert( vps->getVpsNonVuiExtLength() >= 0 && vps->getVpsNonVuiExtLength() <= 4096 );
1901
1902#if P0307_VPS_NON_VUI_EXT_UPDATE
1903  Int nonVuiExtByte = uiCode;
1904  for (i = 1; i <= nonVuiExtByte; i++)
1905  {
1906    READ_CODE( 8, uiCode, "vps_non_vui_extension_data_byte" ); //just parse and discard for now.
1907  }
1908#else
1909  if ( vps->getVpsNonVuiExtLength() > 0 )
1910  {
1911    printf("\n\nUp to the current spec, the value of vps_non_vui_extension_length is supposed to be 0\n");
1912  }
1913#endif
1914#endif
1915
1916#if !O0109_O0199_FLAGS_TO_VUI
1917#if M0040_ADAPTIVE_RESOLUTION_CHANGE
1918  READ_FLAG(uiCode, "single_layer_for_non_irap_flag" ); vps->setSingleLayerForNonIrapFlag(uiCode == 1 ? true : false);
1919#endif
1920#if HIGHER_LAYER_IRAP_SKIP_FLAG
1921  READ_FLAG(uiCode, "higher_layer_irap_skip_flag" ); vps->setHigherLayerIrapSkipFlag(uiCode == 1 ? true : false);
1922#endif
1923#endif
1924
1925#if P0307_REMOVE_VPS_VUI_OFFSET
1926  READ_FLAG( uiCode, "vps_vui_present_flag"); vps->setVpsVuiPresentFlag(uiCode ? true : false);
1927#endif
1928
1929#if O0109_MOVE_VPS_VUI_FLAG
1930  if ( vps->getVpsVuiPresentFlag() )
1931#else
1932  READ_FLAG( uiCode,  "vps_vui_present_flag" );
1933  if (uiCode)
1934#endif
1935  {
1936    while ( m_pcBitstream->getNumBitsRead() % 8 != 0 )
1937    {
1938      READ_FLAG( uiCode, "vps_vui_alignment_bit_equal_to_one"); assert(uiCode == 1);
1939    }
1940    parseVPSVUI(vps);
1941  }
1942  else
1943  {
1944    // set default values for VPS VUI
1945    defaultVPSVUI( vps );
1946  }
1947}
1948
1949Void TDecCavlc::defaultVPSExtension( TComVPS* vps )
1950{
1951  // set default parameters when they are not present
1952  Int i, j;
1953
1954  // When layer_id_in_nuh[ i ] is not present, the value is inferred to be equal to i.
1955  for(i = 0; i < vps->getMaxLayers(); i++)
1956  {
1957    vps->setLayerIdInNuh(i, i);
1958    vps->setLayerIdInVps(vps->getLayerIdInNuh(i), i);
1959  }
1960
1961  // When not present, sub_layers_vps_max_minus1[ i ] is inferred to be equal to vps_max_sub_layers_minus1.
1962  for( i = 0; i < vps->getMaxLayers(); i++)
1963  {
1964    vps->setMaxTSLayersMinus1(i, vps->getMaxTLayers()-1);
1965  }
1966
1967  // When not present, max_tid_il_ref_pics_plus1[ i ][ j ] is inferred to be equal to 7.
1968  for( i = 0; i < vps->getMaxLayers() - 1; i++ )
1969  {
1970#if O0225_MAX_TID_FOR_REF_LAYERS
1971    for( j = i + 1; j < vps->getMaxLayers(); j++ )
1972    {
1973      vps->setMaxTidIlRefPicsPlus1(i, j, 7);
1974    }
1975#else
1976    vps->setMaxTidIlRefPicsPlus1(i, 7);
1977#endif
1978  }
1979
1980  // When not present, the value of num_add_olss is inferred to be equal to 0.
1981  // NumOutputLayerSets = num_add_olss + NumLayerSets
1982  vps->setNumOutputLayerSets( vps->getNumLayerSets() );
1983
1984  // For i in the range of 0 to NumOutputLayerSets-1, inclusive, the variable LayerSetIdxForOutputLayerSet[ i ] is derived as specified in the following:
1985  // LayerSetIdxForOutputLayerSet[ i ] = ( i <= vps_number_layer_sets_minus1 ) ? i : layer_set_idx_for_ols_minus1[ i ] + 1
1986  for( i = 1; i < vps->getNumOutputLayerSets(); i++ )
1987  {
1988    vps->setOutputLayerSetIdx( i, i );
1989    Int lsIdx = vps->getOutputLayerSetIdx(i);
1990
1991    for( j = 0; j < vps->getNumLayersInIdList(lsIdx); j++ )
1992    {
1993      vps->setOutputLayerFlag(i, j, 1);
1994    }
1995  }
1996
1997  // The value of sub_layer_dpb_info_present_flag[ i ][ 0 ] for any possible value of i is inferred to be equal to 1
1998  // When not present, the value of sub_layer_dpb_info_present_flag[ i ][ j ] for j greater than 0 and any possible value of i, is inferred to be equal to be equal to 0.
1999  for( i = 1; i < vps->getNumOutputLayerSets(); i++ )
2000  {
2001    vps->setSubLayerDpbInfoPresentFlag( i, 0, true );
2002  }
2003
2004  // When not present, the value of vps_num_rep_formats_minus1 is inferred to be equal to MaxLayersMinus1.
2005  vps->setVpsNumRepFormats( vps->getMaxLayers() );
2006
2007  // When not present, the value of rep_format_idx_present_flag is inferred to be equal to 0
2008  vps->setRepFormatIdxPresentFlag( false );
2009
2010  if( !vps->getRepFormatIdxPresentFlag() )
2011  {
2012    // When not present, the value of vps_rep_format_idx[ i ] is inferred to be equal to Min(i, vps_num_rep_formats_minus1).
2013    for(i = 1; i < vps->getMaxLayers(); i++)
2014    {
2015      vps->setVpsRepFormatIdx( i, min( (Int)i, vps->getVpsNumRepFormats() - 1 ) );
2016    }
2017  }
2018
2019  // vps_poc_lsb_aligned_flag
2020  // When not present, vps_poc_lsb_aligned_flag is inferred to be equal to 0.
2021
2022#if O0062_POC_LSB_NOT_PRESENT_FLAG
2023  // When not present, poc_lsb_not_present_flag[ i ] is inferred to be equal to 0.
2024  for(i = 1; i< vps->getMaxLayers(); i++)
2025  {
2026    vps->setPocLsbNotPresentFlag(i, 0);
2027  }
2028#endif
2029
2030  // set default values for VPS VUI
2031  defaultVPSVUI( vps );
2032}
2033
2034Void TDecCavlc::defaultVPSVUI( TComVPS* vps )
2035{
2036  // When not present, the value of all_layers_idr_aligned_flag is inferred to be equal to 0.
2037  vps->setCrossLayerIrapAlignFlag( false );
2038
2039#if M0040_ADAPTIVE_RESOLUTION_CHANGE
2040  // When single_layer_for_non_irap_flag is not present, it is inferred to be equal to 0.
2041  vps->setSingleLayerForNonIrapFlag( false );
2042#endif
2043
2044#if HIGHER_LAYER_IRAP_SKIP_FLAG
2045  // When higher_layer_irap_skip_flag is not present it is inferred to be equal to 0
2046  vps->setHigherLayerIrapSkipFlag( false );
2047#endif
2048}
2049
2050#if REPN_FORMAT_IN_VPS
2051Void  TDecCavlc::parseRepFormat( RepFormat *repFormat, RepFormat *repFormatPrev )
2052{
2053  UInt uiCode;
2054#if REPN_FORMAT_CONTROL_FLAG 
2055  READ_CODE( 16, uiCode, "pic_width_vps_in_luma_samples" );        repFormat->setPicWidthVpsInLumaSamples ( uiCode );
2056  READ_CODE( 16, uiCode, "pic_height_vps_in_luma_samples" );       repFormat->setPicHeightVpsInLumaSamples( uiCode );
2057  READ_FLAG( uiCode, "chroma_and_bit_depth_vps_present_flag" );    repFormat->setChromaAndBitDepthVpsPresentFlag( uiCode ? true : false ); 
2058
2059  if( !repFormatPrev )
2060  {
2061    // The value of chroma_and_bit_depth_vps_present_flag of the first rep_format( ) syntax structure in the VPS shall be equal to 1
2062    assert( repFormat->getChromaAndBitDepthVpsPresentFlag() );
2063  }
2064
2065  if( repFormat->getChromaAndBitDepthVpsPresentFlag() )
2066  {
2067    READ_CODE( 2, uiCode, "chroma_format_vps_idc" );
2068#if AUXILIARY_PICTURES
2069    repFormat->setChromaFormatVpsIdc( ChromaFormat(uiCode) );
2070#else
2071    repFormat->setChromaFormatVpsIdc( uiCode );
2072#endif
2073
2074    if( repFormat->getChromaFormatVpsIdc() == 3 )
2075    {
2076      READ_FLAG( uiCode, "separate_colour_plane_vps_flag" );       repFormat->setSeparateColourPlaneVpsFlag( uiCode ? true : false );
2077    }
2078
2079    READ_CODE( 4, uiCode, "bit_depth_vps_luma_minus8" );           repFormat->setBitDepthVpsLuma  ( uiCode + 8 );
2080    READ_CODE( 4, uiCode, "bit_depth_vps_chroma_minus8" );         repFormat->setBitDepthVpsChroma( uiCode + 8 );
2081  }
2082  else if( repFormatPrev )
2083  {
2084    // chroma_and_bit_depth_vps_present_flag equal to 0 specifies that the syntax elements, chroma_format_vps_idc, separate_colour_plane_vps_flag, bit_depth_vps_luma_minus8, and
2085    // bit_depth_vps_chroma_minus8 are not present and inferred from the previous rep_format( ) syntax structure in the VPS.
2086
2087    repFormat->setChromaFormatVpsIdc        ( repFormatPrev->getChromaFormatVpsIdc() );
2088    repFormat->setSeparateColourPlaneVpsFlag( repFormatPrev->getSeparateColourPlaneVpsFlag() );
2089    repFormat->setBitDepthVpsLuma           ( repFormatPrev->getBitDepthVpsLuma() );
2090    repFormat->setBitDepthVpsChroma         ( repFormatPrev->getBitDepthVpsChroma() );
2091  }
2092
2093#else
2094#if AUXILIARY_PICTURES
2095  READ_CODE( 2, uiCode, "chroma_format_idc" );               repFormat->setChromaFormatVpsIdc( ChromaFormat(uiCode) );
2096#else
2097  READ_CODE( 2, uiCode, "chroma_format_idc" );               repFormat->setChromaFormatVpsIdc( uiCode );
2098#endif
2099
2100  if( repFormat->getChromaFormatVpsIdc() == 3 )
2101  {
2102    READ_FLAG( uiCode, "separate_colour_plane_flag");        repFormat->setSeparateColourPlaneVpsFlag(uiCode ? true : false);
2103  }
2104
2105  READ_CODE ( 16, uiCode, "pic_width_in_luma_samples" );     repFormat->setPicWidthVpsInLumaSamples ( uiCode );
2106  READ_CODE ( 16, uiCode, "pic_height_in_luma_samples" );    repFormat->setPicHeightVpsInLumaSamples( uiCode );
2107
2108  READ_CODE( 4, uiCode, "bit_depth_luma_minus8" );           repFormat->setBitDepthVpsLuma  ( uiCode + 8 );
2109  READ_CODE( 4, uiCode, "bit_depth_chroma_minus8" );         repFormat->setBitDepthVpsChroma( uiCode + 8 );
2110#endif
2111
2112#if R0156_CONF_WINDOW_IN_REP_FORMAT
2113  READ_FLAG( uiCode, "conformance_window_vps_flag" );
2114  if( uiCode != 0) 
2115  {
2116    Window &conf = repFormat->getConformanceWindowVps();
2117    READ_UVLC( uiCode, "conf_win_vps_left_offset" );         conf.setWindowLeftOffset  ( uiCode );
2118    READ_UVLC( uiCode, "conf_win_vps_right_offset" );        conf.setWindowRightOffset ( uiCode );
2119    READ_UVLC( uiCode, "conf_win_vps_top_offset" );          conf.setWindowTopOffset   ( uiCode );
2120    READ_UVLC( uiCode, "conf_win_vps_bottom_offset" );       conf.setWindowBottomOffset( uiCode );
2121  }
2122#endif
2123}
2124#endif
2125#if VPS_DPB_SIZE_TABLE
2126Void TDecCavlc::parseVpsDpbSizeTable( TComVPS *vps )
2127{
2128  UInt uiCode;
2129#if SUB_LAYERS_IN_LAYER_SET
2130  vps->calculateMaxSLInLayerSets();
2131#else
2132#if DPB_PARAMS_MAXTLAYERS
2133#if BITRATE_PICRATE_SIGNALLING
2134  Int * MaxSubLayersInLayerSetMinus1 = new Int[vps->getNumLayerSets()];
2135  for(Int i = 0; i < vps->getNumLayerSets(); i++)
2136#else
2137  Int * MaxSubLayersInLayerSetMinus1 = new Int[vps->getNumOutputLayerSets()];
2138  for(Int i = 1; i < vps->getNumOutputLayerSets(); i++)
2139#endif
2140  {
2141    UInt maxSLMinus1 = 0;
2142#if CHANGE_NUMSUBDPB_IDX
2143    Int optLsIdx = vps->getOutputLayerSetIdx( i );
2144#else
2145    Int optLsIdx = i;
2146#endif
2147#if BITRATE_PICRATE_SIGNALLING
2148    optLsIdx = i;
2149#endif
2150    for(Int k = 0; k < vps->getNumLayersInIdList(optLsIdx); k++ ) {
2151      Int  lId = vps->getLayerSetLayerIdList(optLsIdx, k);
2152      maxSLMinus1 = max(maxSLMinus1, vps->getMaxTSLayersMinus1(vps->getLayerIdInVps(lId)));
2153    }
2154    MaxSubLayersInLayerSetMinus1[ i ] = maxSLMinus1;
2155#if BITRATE_PICRATE_SIGNALLING
2156    vps->setMaxSLayersInLayerSetMinus1(i,MaxSubLayersInLayerSetMinus1[ i ]);
2157#endif
2158  }
2159#endif
2160#endif
2161
2162#if !RESOLUTION_BASED_DPB
2163  vps->deriveNumberOfSubDpbs();
2164#endif
2165  for(Int i = 1; i < vps->getNumOutputLayerSets(); i++)
2166  {
2167#if CHANGE_NUMSUBDPB_IDX
2168    Int layerSetIdxForOutputLayerSet = vps->getOutputLayerSetIdx( i );
2169#endif
2170    READ_FLAG( uiCode, "sub_layer_flag_info_present_flag[i]");  vps->setSubLayerFlagInfoPresentFlag( i, uiCode ? true : false );
2171#if SUB_LAYERS_IN_LAYER_SET
2172    for(Int j = 0; j <= vps->getMaxSLayersInLayerSetMinus1( layerSetIdxForOutputLayerSet ); j++)
2173#else
2174#if DPB_PARAMS_MAXTLAYERS
2175#if BITRATE_PICRATE_SIGNALLING
2176    for(Int j = 0; j <= MaxSubLayersInLayerSetMinus1[ vps->getOutputLayerSetIdx( i ) ]; j++)
2177#else
2178    for(Int j = 0; j <= MaxSubLayersInLayerSetMinus1[ i ]; j++)
2179#endif
2180#else
2181    for(Int j = 0; j <= vps->getMaxTLayers(); j++)
2182#endif
2183#endif
2184    {
2185      if( j > 0 && vps->getSubLayerFlagInfoPresentFlag(i) )
2186      {
2187        READ_FLAG( uiCode, "sub_layer_dpb_info_present_flag[i]");  vps->setSubLayerDpbInfoPresentFlag( i, j, uiCode ? true : false);
2188      }
2189      else
2190      {
2191        if( j == 0 )  // Always signal for the first sub-layer
2192        {
2193          vps->setSubLayerDpbInfoPresentFlag( i, j, true );
2194        }
2195        else // if (j != 0) && !vps->getSubLayerFlagInfoPresentFlag(i)
2196        {
2197          vps->setSubLayerDpbInfoPresentFlag( i, j, false );
2198        }
2199      }
2200      if( vps->getSubLayerDpbInfoPresentFlag(i, j) )  // If sub-layer DPB information is present
2201      {
2202#if CHANGE_NUMSUBDPB_IDX
2203        for(Int k = 0; k < vps->getNumSubDpbs(layerSetIdxForOutputLayerSet); k++)
2204#else
2205        for(Int k = 0; k < vps->getNumSubDpbs(i); k++)
2206#endif
2207        {
2208          READ_UVLC( uiCode, "max_vps_dec_pic_buffering_minus1[i][k][j]" ); vps->setMaxVpsDecPicBufferingMinus1( i, k, j, uiCode );
2209        }
2210        READ_UVLC( uiCode, "max_vps_num_reorder_pics[i][j]" );              vps->setMaxVpsNumReorderPics( i, j, uiCode);
2211#if RESOLUTION_BASED_DPB
2212        if( vps->getNumSubDpbs(layerSetIdxForOutputLayerSet) != vps->getNumLayersInIdList( layerSetIdxForOutputLayerSet ) ) 
2213        {
2214          for(Int k = 0; k < vps->getNumLayersInIdList( layerSetIdxForOutputLayerSet ); k++)
2215          {
2216            READ_UVLC( uiCode, "max_vps_layer_dec_pic_buff_minus1[i][k][j]" ); vps->setMaxVpsLayerDecPicBuffMinus1( i, k, j, uiCode);
2217          }
2218        }
2219        else  // vps->getNumSubDpbs(layerSetIdxForOutputLayerSet) == vps->getNumLayersInIdList( layerSetIdxForOutputLayerSet )
2220        {         
2221          for(Int k = 0; k < vps->getNumLayersInIdList( layerSetIdxForOutputLayerSet ); k++)
2222          {
2223            vps->setMaxVpsLayerDecPicBuffMinus1( i, k, j, vps->getMaxVpsDecPicBufferingMinus1( i, k, j));
2224          }
2225        }
2226#endif
2227        READ_UVLC( uiCode, "max_vps_latency_increase_plus1[i][j]" );        vps->setMaxVpsLatencyIncreasePlus1( i, j, uiCode);
2228      }
2229    }
2230    for(Int j = vps->getMaxTLayers(); j < MAX_TLAYER; j++)
2231    {
2232      vps->setSubLayerDpbInfoPresentFlag( i, j, false );
2233    }
2234  }
2235
2236#if !SUB_LAYERS_IN_LAYER_SET
2237#if BITRATE_PICRATE_SIGNALLING
2238  if( MaxSubLayersInLayerSetMinus1 )
2239  {
2240    delete [] MaxSubLayersInLayerSetMinus1;
2241  }
2242#endif
2243#endif
2244
2245  // Infer values when not signalled
2246  for(Int i = 1; i < vps->getNumOutputLayerSets(); i++)
2247  {
2248    Int layerSetIdxForOutputLayerSet = vps->getOutputLayerSetIdx( i );
2249    for(Int j = 0; j < MAX_TLAYER; j++)
2250    {
2251      if( !vps->getSubLayerDpbInfoPresentFlag(i, j) )  // If sub-layer DPB information is NOT present
2252      {
2253#if RESOLUTION_BASED_DPB
2254        for(Int k = 0; k < vps->getNumSubDpbs(layerSetIdxForOutputLayerSet); k++)
2255#else
2256        for(Int k = 0; k < vps->getNumLayersInIdList( layerSetIdxForOutputLayerSet ); k++)
2257#endif
2258        {
2259          vps->setMaxVpsDecPicBufferingMinus1( i, k, j, vps->getMaxVpsDecPicBufferingMinus1( i, k, j - 1 ) );
2260        }
2261        vps->setMaxVpsNumReorderPics( i, j, vps->getMaxVpsNumReorderPics( i, j - 1) );
2262#if RESOLUTION_BASED_DPB
2263        for(Int k = 0; k < vps->getNumLayersInIdList( layerSetIdxForOutputLayerSet ); k++)
2264        {
2265          vps->setMaxVpsLayerDecPicBuffMinus1( i, k, j, vps->getMaxVpsLayerDecPicBuffMinus1( i, k, j - 1));
2266        }
2267#endif
2268        vps->setMaxVpsLatencyIncreasePlus1( i, j, vps->getMaxVpsLatencyIncreasePlus1( i, j - 1 ) );
2269      }
2270    }
2271  }
2272}
2273#endif
2274
2275Void TDecCavlc::parseVPSVUI(TComVPS *vps)
2276{
2277  UInt i,j;
2278  UInt uiCode;
2279#if O0223_PICTURE_TYPES_ALIGN_FLAG
2280  READ_FLAG(uiCode, "cross_layer_pic_type_aligned_flag" );
2281  vps->setCrossLayerPictureTypeAlignFlag(uiCode);
2282  if (!uiCode) 
2283  {
2284#endif
2285#if IRAP_ALIGN_FLAG_IN_VPS_VUI
2286    READ_FLAG(uiCode, "cross_layer_irap_aligned_flag" );
2287    vps->setCrossLayerIrapAlignFlag(uiCode);
2288#if P0068_CROSS_LAYER_ALIGNED_IDR_ONLY_FOR_IRAP_FLAG
2289    if( uiCode )
2290    {
2291      READ_FLAG( uiCode, "all_layers_idr_aligned_flag" );
2292      vps->setCrossLayerAlignedIdrOnlyFlag(uiCode);
2293    }
2294#endif
2295#endif
2296#if O0223_PICTURE_TYPES_ALIGN_FLAG
2297  }
2298  else
2299  {
2300    vps->setCrossLayerIrapAlignFlag(true);
2301  }
2302#endif
2303
2304  READ_FLAG( uiCode,        "bit_rate_present_vps_flag" );  vps->setBitRatePresentVpsFlag( uiCode ? true : false );
2305  READ_FLAG( uiCode,        "pic_rate_present_vps_flag" );  vps->setPicRatePresentVpsFlag( uiCode ? true : false );
2306
2307#if SIGNALLING_BITRATE_PICRATE_FIX
2308  if ( vps->getBitRatePresentVpsFlag() || vps->getPicRatePresentVpsFlag() )
2309  {
2310    for( i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getNumLayerSets(); i++ )
2311    {
2312      for( Int j = 0; j  <=  vps->getMaxSLayersInLayerSetMinus1( i ); j++ ) 
2313      {
2314        if( vps->getBitRatePresentVpsFlag() )
2315        {
2316          READ_FLAG( uiCode, "bit_rate_present_flag[i][j]" ); vps->setBitRatePresentFlag( i, j, uiCode ? true : false );           
2317        }
2318        if( vps->getPicRatePresentVpsFlag( )  )
2319        {
2320          READ_FLAG( uiCode, "pic_rate_present_flag[i][j]" ); vps->setPicRatePresentFlag( i, j, uiCode ? true : false );
2321        }
2322        if( vps->getBitRatePresentFlag( i, j ) )
2323        {
2324          READ_CODE( 16, uiCode, "avg_bit_rate" ); vps->setAvgBitRate( i, j, uiCode );
2325          READ_CODE( 16, uiCode, "max_bit_rate" ); vps->setMaxBitRate( i, j, uiCode );
2326        }
2327        else
2328        {
2329          vps->setAvgBitRate( i, j, 0 );
2330          vps->setMaxBitRate( i, j, 0 );
2331        }
2332        if( vps->getPicRatePresentFlag( i, j ) )
2333        {
2334          READ_CODE( 2,  uiCode, "constant_pic_rate_idc" ); vps->setConstPicRateIdc( i, j, uiCode );
2335          READ_CODE( 16, uiCode, "avg_pic_rate" );          vps->setAvgPicRate( i, j, uiCode );
2336        }
2337        else
2338        {
2339          vps->setConstPicRateIdc( i, j, 0 );
2340          vps->setAvgPicRate( i, j, 0 );
2341        }
2342      }
2343    }
2344  }
2345#else
2346  Bool parseFlag = vps->getBitRatePresentVpsFlag() || vps->getPicRatePresentVpsFlag();
2347
2348#if Q0078_ADD_LAYER_SETS
2349#if R0227_BR_PR_ADD_LAYER_SET
2350  for( i = 0; i < vps->getNumLayerSets(); i++ )
2351#else
2352  for( i = 0; i <= vps->getVpsNumLayerSetsMinus1(); i++ )
2353#endif
2354#else
2355  for( i = 0; i < vps->getNumLayerSets(); i++ )
2356#endif
2357  {
2358#if BITRATE_PICRATE_SIGNALLING
2359    for( j = 0; j <= vps->getMaxSLayersInLayerSetMinus1(i); j++ )
2360#else
2361    for( j = 0; j < vps->getMaxTLayers(); j++ )
2362#endif
2363    {
2364      if( parseFlag && vps->getBitRatePresentVpsFlag() )
2365      {
2366        READ_FLAG( uiCode,        "bit_rate_present_flag[i][j]" );  vps->setBitRatePresentFlag( i, j, uiCode ? true : false );
2367      }
2368      else
2369      {
2370        vps->setBitRatePresentFlag( i, j, false );
2371      }
2372      if( parseFlag && vps->getPicRatePresentVpsFlag() )
2373      {
2374        READ_FLAG( uiCode,        "pic_rate_present_flag[i][j]" );  vps->setPicRatePresentFlag( i, j, uiCode ? true : false );
2375      }
2376      else
2377      {
2378        vps->setPicRatePresentFlag( i, j, false );
2379      }
2380      if( parseFlag && vps->getBitRatePresentFlag(i, j) )
2381      {
2382        READ_CODE( 16, uiCode,    "avg_bit_rate[i][j]" ); vps->setAvgBitRate( i, j, uiCode );
2383        READ_CODE( 16, uiCode,    "max_bit_rate[i][j]" ); vps->setMaxBitRate( i, j, uiCode );
2384      }
2385      else
2386      {
2387        vps->setAvgBitRate( i, j, 0 );
2388        vps->setMaxBitRate( i, j, 0 );
2389      }
2390      if( parseFlag && vps->getPicRatePresentFlag(i, j) )
2391      {
2392        READ_CODE( 2 , uiCode,    "constant_pic_rate_idc[i][j]" ); vps->setConstPicRateIdc( i, j, uiCode );
2393        READ_CODE( 16, uiCode,    "avg_pic_rate[i][j]"          ); vps->setAvgPicRate( i, j, uiCode );
2394      }
2395      else
2396      {
2397        vps->setConstPicRateIdc( i, j, 0 );
2398        vps->setAvgPicRate     ( i, j, 0 );
2399      }
2400    }
2401  }
2402#endif
2403#if VPS_VUI_VIDEO_SIGNAL_MOVE
2404  READ_FLAG( uiCode, "video_signal_info_idx_present_flag" ); vps->setVideoSigPresentVpsFlag( uiCode == 1 );
2405  if (vps->getVideoSigPresentVpsFlag())
2406  {
2407    READ_CODE(4, uiCode, "vps_num_video_signal_info_minus1" ); vps->setNumVideoSignalInfo(uiCode + 1);
2408  }
2409  else
2410  {
2411#if VPS_VUI_VST_PARAMS
2412    vps->setNumVideoSignalInfo(vps->getMaxLayers() - vps->getBaseLayerInternalFlag() ? 0 : 1);
2413#else
2414    vps->setNumVideoSignalInfo(vps->getMaxLayers());
2415#endif
2416  }
2417
2418  for(i = 0; i < vps->getNumVideoSignalInfo(); i++)
2419  {
2420    READ_CODE(3, uiCode, "video_vps_format" ); vps->setVideoVPSFormat(i,uiCode);
2421    READ_FLAG(uiCode, "video_full_range_vps_flag" ); vps->setVideoFullRangeVpsFlag(i,uiCode);
2422    READ_CODE(8, uiCode, "color_primaries_vps" ); vps->setColorPrimaries(i,uiCode);
2423    READ_CODE(8, uiCode, "transfer_characteristics_vps" ); vps->setTransCharacter(i,uiCode);
2424    READ_CODE(8, uiCode, "matrix_coeffs_vps" );vps->setMaxtrixCoeff(i,uiCode);
2425  }
2426#if VPS_VUI_VST_PARAMS
2427  if( vps->getVideoSigPresentVpsFlag() && vps->getNumVideoSignalInfo() > 1 )
2428  {
2429    for(i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++)
2430    {
2431      READ_CODE(4, uiCode, "vps_video_signal_info_idx" ); vps->setVideoSignalInfoIdx(i, uiCode);
2432    }
2433  }
2434  else if ( !vps->getVideoSigPresentVpsFlag() )
2435  {
2436    for(i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++)
2437    {
2438      vps->setVideoSignalInfoIdx( i, i );
2439    }
2440  }
2441  else // ( vps->getNumVideoSignalInfo() = 0 )
2442  {
2443    for(i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++)
2444    {
2445      vps->setVideoSignalInfoIdx( i, 0 );
2446    }
2447  }
2448#else
2449  if(!vps->getVideoSigPresentVpsFlag())
2450  {
2451    for (i=0; i < vps->getMaxLayers(); i++)
2452    {
2453      vps->setVideoSignalInfoIdx(i,i);
2454    }
2455  }
2456  else {
2457    vps->setVideoSignalInfoIdx(0,0);
2458    if (vps->getNumVideoSignalInfo() > 1 )
2459    {
2460      for (i=1; i < vps->getMaxLayers(); i++)
2461        READ_CODE(4, uiCode, "vps_video_signal_info_idx" ); vps->setVideoSignalInfoIdx(i, uiCode);
2462    }
2463    else {
2464      for (i=1; i < vps->getMaxLayers(); i++)
2465      {
2466        vps->setVideoSignalInfoIdx(i,0);
2467      }
2468    }
2469  }
2470#endif
2471#endif
2472#if VPS_VUI_TILES_NOT_IN_USE__FLAG
2473  UInt layerIdx;
2474  READ_FLAG( uiCode, "tiles_not_in_use_flag" ); vps->setTilesNotInUseFlag(uiCode == 1);
2475  if (!uiCode)
2476  {
2477    for(i = 0; i < vps->getMaxLayers(); i++)
2478    {
2479      READ_FLAG( uiCode, "tiles_in_use_flag[ i ]" ); vps->setTilesInUseFlag(i, (uiCode == 1));
2480      if (uiCode)
2481      {
2482        READ_FLAG( uiCode, "loop_filter_not_across_tiles_flag[ i ]" ); vps->setLoopFilterNotAcrossTilesFlag(i, (uiCode == 1));
2483      }
2484      else
2485      {
2486        vps->setLoopFilterNotAcrossTilesFlag(i, false);
2487      }
2488    }
2489#endif
2490
2491    for(i = 1; i < vps->getMaxLayers(); i++)
2492    {
2493      for(j = 0; j < vps->getNumDirectRefLayers(vps->getLayerIdInNuh(i)); j++)
2494      {
2495#if VPS_VUI_TILES_NOT_IN_USE__FLAG
2496        layerIdx = vps->getLayerIdInVps(vps->getRefLayerId(vps->getLayerIdInNuh(i), j));
2497        if (vps->getTilesInUseFlag(i) && vps->getTilesInUseFlag(layerIdx)) {
2498          READ_FLAG( uiCode, "tile_boundaries_aligned_flag[i][j]" ); vps->setTileBoundariesAlignedFlag(i,j,(uiCode == 1));
2499        }
2500#else
2501        READ_FLAG( uiCode, "tile_boundaries_aligned_flag[i][j]" ); vps->setTileBoundariesAlignedFlag(i,j,(uiCode == 1));
2502#endif
2503      }
2504    }
2505#if VPS_VUI_TILES_NOT_IN_USE__FLAG
2506  }
2507#endif
2508#if VPS_VUI_WPP_NOT_IN_USE__FLAG
2509  READ_FLAG( uiCode, "wpp_not_in_use_flag" ); vps->setWppNotInUseFlag(uiCode == 1);
2510  if (!uiCode)
2511  {
2512    for(i = 0; i < vps->getMaxLayers(); i++)
2513    {
2514      READ_FLAG( uiCode, "wpp_in_use_flag[ i ]" ); vps->setWppInUseFlag(i, (uiCode == 1));
2515    }
2516  }
2517#endif
2518
2519#if O0109_O0199_FLAGS_TO_VUI
2520#if M0040_ADAPTIVE_RESOLUTION_CHANGE
2521  READ_FLAG(uiCode, "single_layer_for_non_irap_flag" ); vps->setSingleLayerForNonIrapFlag(uiCode == 1 ? true : false);
2522#endif
2523#if HIGHER_LAYER_IRAP_SKIP_FLAG
2524  READ_FLAG(uiCode, "higher_layer_irap_skip_flag" ); vps->setHigherLayerIrapSkipFlag(uiCode == 1 ? true : false);
2525
2526  // When single_layer_for_non_irap_flag is equal to 0, higher_layer_irap_skip_flag shall be equal to 0
2527  if( !vps->getSingleLayerForNonIrapFlag() )
2528  {
2529    assert( !vps->getHigherLayerIrapSkipFlag() );
2530  }
2531#endif
2532#endif
2533#if P0312_VERT_PHASE_ADJ
2534  READ_FLAG( uiCode, "vps_vui_vert_phase_in_use_flag" ); vps->setVpsVuiVertPhaseInUseFlag(uiCode);
2535#endif
2536#if N0160_VUI_EXT_ILP_REF
2537  READ_FLAG( uiCode, "ilp_restricted_ref_layers_flag" ); vps->setIlpRestrictedRefLayersFlag( uiCode == 1 );
2538  if( vps->getIlpRestrictedRefLayersFlag())
2539  {
2540    for(i = 1; i < vps->getMaxLayers(); i++)
2541    {
2542      for(j = 0; j < vps->getNumDirectRefLayers(vps->getLayerIdInNuh(i)); j++)
2543      {
2544        READ_UVLC( uiCode, "min_spatial_segment_offset_plus1[i][j]" ); vps->setMinSpatialSegmentOffsetPlus1( i, j, uiCode );
2545        if( vps->getMinSpatialSegmentOffsetPlus1(i,j ) > 0 )
2546        {
2547          READ_FLAG( uiCode, "ctu_based_offset_enabled_flag[i][j]"); vps->setCtuBasedOffsetEnabledFlag(i, j, uiCode == 1 );
2548          if(vps->getCtuBasedOffsetEnabledFlag(i,j))
2549          {
2550            READ_UVLC( uiCode, "min_horizontal_ctu_offset_plus1[i][j]"); vps->setMinHorizontalCtuOffsetPlus1( i,j, uiCode );
2551          }
2552        }
2553      }
2554    }
2555  }
2556#endif
2557#if VPS_VUI_VIDEO_SIGNAL
2558#if VPS_VUI_VIDEO_SIGNAL_MOVE
2559#else
2560  READ_FLAG( uiCode, "video_signal_info_idx_present_flag" ); vps->setVideoSigPresentVpsFlag( uiCode == 1 );
2561  if (vps->getVideoSigPresentVpsFlag())
2562  {
2563    READ_CODE(4, uiCode, "vps_num_video_signal_info_minus1" ); vps->setNumVideoSignalInfo(uiCode + 1);
2564  }
2565  else
2566  {
2567    vps->setNumVideoSignalInfo(vps->getMaxLayers());
2568  }
2569
2570
2571  for(i = 0; i < vps->getNumVideoSignalInfo(); i++)
2572  {
2573    READ_CODE(3, uiCode, "video_vps_format" ); vps->setVideoVPSFormat(i,uiCode);
2574    READ_FLAG(uiCode, "video_full_range_vps_flag" ); vps->setVideoFullRangeVpsFlag(i,uiCode);
2575    READ_CODE(8, uiCode, "color_primaries_vps" ); vps->setColorPrimaries(i,uiCode);
2576    READ_CODE(8, uiCode, "transfer_characteristics_vps" ); vps->setTransCharacter(i,uiCode);
2577    READ_CODE(8, uiCode, "matrix_coeffs_vps" );vps->setMaxtrixCoeff(i,uiCode);
2578  }
2579  if(!vps->getVideoSigPresentVpsFlag())
2580  {
2581    for (i=0; i < vps->getMaxLayers(); i++)
2582    {
2583      vps->setVideoSignalInfoIdx(i,i);
2584    }
2585  }
2586  else {
2587    vps->setVideoSignalInfoIdx(0,0);
2588    if (vps->getNumVideoSignalInfo() > 1 )
2589    {
2590      for (i=1; i < vps->getMaxLayers(); i++)
2591        READ_CODE(4, uiCode, "vps_video_signal_info_idx" ); vps->setVideoSignalInfoIdx(i, uiCode);
2592    }
2593    else {
2594      for (i=1; i < vps->getMaxLayers(); i++)
2595      {
2596        vps->setVideoSignalInfoIdx(i,0);
2597      }
2598    }
2599  }
2600#endif
2601#endif
2602
2603#if O0164_MULTI_LAYER_HRD
2604  READ_FLAG(uiCode, "vps_vui_bsp_hrd_present_flag" ); vps->setVpsVuiBspHrdPresentFlag(uiCode);
2605  if (vps->getVpsVuiBspHrdPresentFlag())
2606  {
2607#if R0227_VUI_BSP_HRD_FLAG
2608    assert (vps->getTimingInfo()->getTimingInfoPresentFlag() == 1);
2609#endif
2610    READ_UVLC( uiCode, "vps_num_bsp_hrd_parameters_minus1" ); vps->setVpsNumBspHrdParametersMinus1(uiCode);
2611    vps->createBspHrdParamBuffer(vps->getVpsNumBspHrdParametersMinus1() + 1);
2612    for( i = 0; i <= vps->getVpsNumBspHrdParametersMinus1(); i++ )
2613    {
2614      if( i > 0 )
2615      {
2616        READ_FLAG( uiCode, "bsp_cprms_present_flag[i]" ); vps->setBspCprmsPresentFlag(i, uiCode);
2617      }
2618      parseHrdParameters(vps->getBspHrd(i), i==0 ? 1 : vps->getBspCprmsPresentFlag(i), vps->getMaxTLayers()-1);
2619    }
2620#if Q0078_ADD_LAYER_SETS
2621    for (UInt h = 1; h <= vps->getVpsNumLayerSetsMinus1(); h++)
2622#else
2623    for( UInt h = 1; h <= (vps->getNumLayerSets()-1); h++ )
2624#endif
2625    {
2626      READ_UVLC( uiCode, "num_bitstream_partitions[i]"); vps->setNumBitstreamPartitions(h, uiCode);
2627#if HRD_BPB
2628      Int chkPart=0;
2629#endif
2630      for( i = 0; i < vps->getNumBitstreamPartitions(h); i++ )
2631      {
2632        for( j = 0; j <= (vps->getMaxLayers()-1); j++ )
2633        {
2634          if( vps->getLayerIdIncludedFlag(h, j) )
2635          {
2636            READ_FLAG( uiCode, "layer_in_bsp_flag[h][i][j]" ); vps->setLayerInBspFlag(h, i, j, uiCode);
2637          }
2638        }
2639#if HRD_BPB
2640        chkPart+=vps->getLayerInBspFlag(h, i, j);
2641#endif
2642      }
2643#if HRD_BPB
2644      assert(chkPart<=1);
2645#endif
2646#if HRD_BPB
2647      if(vps->getNumBitstreamPartitions(h)==1)
2648      {
2649        Int chkPartition1=0; Int chkPartition2=0;
2650        for( j = 0; j <= (vps->getMaxLayers()-1); j++ )
2651        {
2652          if( vps->getLayerIdIncludedFlag(h, j) )
2653          {
2654            chkPartition1+=vps->getLayerInBspFlag(h, 0, j);
2655            chkPartition2++;
2656          }
2657        }
2658        assert(chkPartition1!=chkPartition2);
2659      }
2660#endif
2661      if (vps->getNumBitstreamPartitions(h))
2662      {
2663#if Q0182_MULTI_LAYER_HRD_UPDATE
2664        READ_UVLC( uiCode, "num_bsp_sched_combinations_minus1[h]"); vps->setNumBspSchedCombinations(h, uiCode + 1);
2665#else
2666        READ_UVLC( uiCode, "num_bsp_sched_combinations[h]"); vps->setNumBspSchedCombinations(h, uiCode);
2667#endif
2668        for( i = 0; i < vps->getNumBspSchedCombinations(h); i++ )
2669        {
2670          for( j = 0; j < vps->getNumBitstreamPartitions(h); j++ )
2671          {
2672            READ_UVLC( uiCode, "bsp_comb_hrd_idx[h][i][j]"); vps->setBspCombHrdIdx(h, i, j, uiCode);
2673#if HRD_BPB
2674            assert(uiCode <= vps->getVpsNumBspHrdParametersMinus1());
2675#endif
2676
2677            READ_UVLC( uiCode, "bsp_comb_sched_idx[h][i][j]"); vps->setBspCombSchedIdx(h, i, j, uiCode);
2678#if HRD_BPB
2679            assert(uiCode <= vps->getBspHrdParamBufferCpbCntMinus1(uiCode,vps->getMaxTLayers()-1));
2680#endif
2681          }
2682        }
2683      }
2684    }
2685  }
2686#endif
2687
2688#if P0182_VPS_VUI_PS_FLAG
2689  for(i = 1; i < vps->getMaxLayers(); i++)
2690  {
2691    if (vps->getNumRefLayers(vps->getLayerIdInNuh(i)) == 0)
2692    {
2693      READ_FLAG( uiCode, "base_layer_parameter_set_compatibility_flag" ); 
2694      vps->setBaseLayerPSCompatibilityFlag( i, uiCode );
2695    }
2696    else
2697    {
2698      vps->setBaseLayerPSCompatibilityFlag( i, 0 );
2699    }
2700  }
2701#endif
2702}
2703#endif //SVC_EXTENSION
2704
2705Void TDecCavlc::parseSliceHeader (TComSlice*& rpcSlice, ParameterSetManagerDecoder *parameterSetManager)
2706{
2707  UInt  uiCode;
2708  Int   iCode;
2709
2710#if ENC_DEC_TRACE
2711  xTraceSliceHeader(rpcSlice);
2712#endif
2713  TComPPS* pps = NULL;
2714  TComSPS* sps = NULL;
2715
2716  UInt firstSliceSegmentInPic;
2717  READ_FLAG( firstSliceSegmentInPic, "first_slice_segment_in_pic_flag" );
2718  if( rpcSlice->getRapPicFlag())
2719  {
2720    READ_FLAG( uiCode, "no_output_of_prior_pics_flag" );  //ignored -- updated already
2721#if SETTING_NO_OUT_PIC_PRIOR
2722    rpcSlice->setNoOutputPriorPicsFlag(uiCode ? true : false);
2723#else
2724    rpcSlice->setNoOutputPicPrior( false );
2725#endif
2726  }
2727  READ_UVLC (    uiCode, "slice_pic_parameter_set_id" );  rpcSlice->setPPSId(uiCode);
2728  pps = parameterSetManager->getPrefetchedPPS(uiCode);
2729  //!KS: need to add error handling code here, if PPS is not available
2730  assert(pps!=0);
2731  sps = parameterSetManager->getPrefetchedSPS(pps->getSPSId());
2732  //!KS: need to add error handling code here, if SPS is not available
2733  assert(sps!=0);
2734  rpcSlice->setSPS(sps);
2735  rpcSlice->setPPS(pps);
2736
2737#if R0227_REP_FORMAT_CONSTRAINT //Conformance checking for rep format -- rep format of current picture of current layer shall never be greater rep format defined in VPS for the current layer
2738  TComVPS* vps = NULL;
2739  vps = parameterSetManager->getPrefetchedVPS(sps->getVPSId());
2740#if R0279_REP_FORMAT_INBL
2741  if ( vps->getVpsExtensionFlag() == 1 && (rpcSlice->getLayerId() == 0 || sps->getV1CompatibleSPSFlag() == 1) )
2742  {
2743    assert( sps->getPicWidthInLumaSamples() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(rpcSlice->getLayerId()) )->getPicWidthVpsInLumaSamples() );
2744    assert( sps->getPicHeightInLumaSamples() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(rpcSlice->getLayerId()) )->getPicHeightVpsInLumaSamples() );
2745    assert( sps->getChromaFormatIdc() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(rpcSlice->getLayerId()) )->getChromaFormatVpsIdc() );
2746    assert( sps->getBitDepthY() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(rpcSlice->getLayerId()) )->getBitDepthVpsLuma() );
2747    assert( sps->getBitDepthC() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(rpcSlice->getLayerId()) )->getBitDepthVpsChroma() );
2748#else
2749  if ( rpcSlice->getLayerId() == 0 && vps->getVpsExtensionFlag() == 1 )
2750  {
2751    assert( sps->getPicWidthInLumaSamples() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(0) )->getPicWidthVpsInLumaSamples() );
2752    assert( sps->getPicHeightInLumaSamples() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(0) )->getPicHeightVpsInLumaSamples() );
2753    assert( sps->getChromaFormatIdc() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(0) )->getChromaFormatVpsIdc() );
2754    assert( sps->getBitDepthY() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(0) )->getBitDepthVpsLuma() );
2755    assert( sps->getBitDepthC() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(0) )->getBitDepthVpsChroma() );
2756#endif
2757  }
2758  else if ( vps->getVpsExtensionFlag() == 1 )
2759  {
2760    assert(vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : rpcSlice->getLayerId()))->getPicWidthVpsInLumaSamples() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(rpcSlice->getLayerId()))->getPicWidthVpsInLumaSamples());
2761    assert(vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : rpcSlice->getLayerId()))->getPicHeightVpsInLumaSamples() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(rpcSlice->getLayerId()))->getPicHeightVpsInLumaSamples());
2762    assert(vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : rpcSlice->getLayerId()))->getChromaFormatVpsIdc() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(rpcSlice->getLayerId()))->getChromaFormatVpsIdc());
2763    assert(vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : rpcSlice->getLayerId()))->getBitDepthVpsLuma() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(rpcSlice->getLayerId()))->getBitDepthVpsLuma());
2764    assert(vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : rpcSlice->getLayerId()))->getBitDepthVpsChroma() <= vps->getVpsRepFormat( vps->getVpsRepFormatIdx(rpcSlice->getLayerId()))->getBitDepthVpsChroma());
2765  }
2766#endif
2767
2768  if( pps->getDependentSliceSegmentsEnabledFlag() && ( !firstSliceSegmentInPic ))
2769  {
2770    READ_FLAG( uiCode, "dependent_slice_segment_flag" );       rpcSlice->setDependentSliceSegmentFlag(uiCode ? true : false);
2771  }
2772  else
2773  {
2774    rpcSlice->setDependentSliceSegmentFlag(false);
2775  }
2776#if REPN_FORMAT_IN_VPS
2777  Int numCTUs = ((rpcSlice->getPicWidthInLumaSamples()+sps->getMaxCUWidth()-1)/sps->getMaxCUWidth())*((rpcSlice->getPicHeightInLumaSamples()+sps->getMaxCUHeight()-1)/sps->getMaxCUHeight());
2778#else
2779  Int numCTUs = ((sps->getPicWidthInLumaSamples()+sps->getMaxCUWidth()-1)/sps->getMaxCUWidth())*((sps->getPicHeightInLumaSamples()+sps->getMaxCUHeight()-1)/sps->getMaxCUHeight());
2780#endif
2781  Int maxParts = (1<<(sps->getMaxCUDepth()<<1));
2782  UInt sliceSegmentAddress = 0;
2783  Int bitsSliceSegmentAddress = 0;
2784  while(numCTUs>(1<<bitsSliceSegmentAddress))
2785  {
2786    bitsSliceSegmentAddress++;
2787  }
2788
2789  if(!firstSliceSegmentInPic)
2790  {
2791    READ_CODE( bitsSliceSegmentAddress, sliceSegmentAddress, "slice_segment_address" );
2792  }
2793  //set uiCode to equal slice start address (or dependent slice start address)
2794  Int startCuAddress = maxParts*sliceSegmentAddress;
2795  rpcSlice->setSliceSegmentCurStartCUAddr( startCuAddress );
2796  rpcSlice->setSliceSegmentCurEndCUAddr(numCTUs*maxParts);
2797
2798  if (rpcSlice->getDependentSliceSegmentFlag())
2799  {
2800    rpcSlice->setNextSlice          ( false );
2801    rpcSlice->setNextSliceSegment ( true  );
2802  }
2803  else
2804  {
2805    rpcSlice->setNextSlice          ( true  );
2806    rpcSlice->setNextSliceSegment ( false );
2807
2808    rpcSlice->setSliceCurStartCUAddr(startCuAddress);
2809    rpcSlice->setSliceCurEndCUAddr(numCTUs*maxParts);
2810  }
2811
2812#if Q0142_POC_LSB_NOT_PRESENT
2813#if SHM_FIX7
2814  Int iPOClsb = 0;
2815#endif
2816#endif
2817
2818  if(!rpcSlice->getDependentSliceSegmentFlag())
2819  {
2820#if SVC_EXTENSION
2821#if POC_RESET_FLAG
2822    Int iBits = 0;
2823    if(rpcSlice->getPPS()->getNumExtraSliceHeaderBits() > iBits)
2824    {
2825      READ_FLAG(uiCode, "poc_reset_flag");      rpcSlice->setPocResetFlag( uiCode ? true : false );
2826      iBits++;
2827    }
2828    if(rpcSlice->getPPS()->getNumExtraSliceHeaderBits() > iBits)
2829    {
2830#if DISCARDABLE_PIC_RPS
2831      READ_FLAG(uiCode, "discardable_flag"); rpcSlice->setDiscardableFlag( uiCode ? true : false );
2832#else
2833      READ_FLAG(uiCode, "discardable_flag"); // ignored
2834#endif
2835      iBits++;
2836    }
2837#if O0149_CROSS_LAYER_BLA_FLAG
2838    if(rpcSlice->getPPS()->getNumExtraSliceHeaderBits() > iBits)
2839    {
2840      READ_FLAG(uiCode, "cross_layer_bla_flag");  rpcSlice->setCrossLayerBLAFlag( uiCode ? true : false );
2841      iBits++;
2842    }
2843#endif
2844    for (; iBits < rpcSlice->getPPS()->getNumExtraSliceHeaderBits(); iBits++)
2845    {
2846      READ_FLAG(uiCode, "slice_reserved_undetermined_flag[]"); // ignored
2847    }
2848#else
2849#if CROSS_LAYER_BLA_FLAG_FIX
2850    Int iBits = 0;
2851    if(rpcSlice->getPPS()->getNumExtraSliceHeaderBits() > iBits)
2852#else
2853    if(rpcSlice->getPPS()->getNumExtraSliceHeaderBits()>0)
2854#endif
2855    {
2856      READ_FLAG(uiCode, "discardable_flag"); // ignored
2857#if NON_REF_NAL_TYPE_DISCARDABLE
2858      rpcSlice->setDiscardableFlag( uiCode ? true : false );
2859      if (uiCode)
2860      {
2861        assert(rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TRAIL_R &&
2862          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TSA_R &&
2863          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_STSA_R &&
2864          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RADL_R &&
2865          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RASL_R);
2866      }
2867#endif
2868#if CROSS_LAYER_BLA_FLAG_FIX
2869      iBits++;
2870#endif
2871    }
2872#if CROSS_LAYER_BLA_FLAG_FIX
2873    if(rpcSlice->getPPS()->getNumExtraSliceHeaderBits() > iBits)
2874    {
2875      READ_FLAG(uiCode, "cross_layer_bla_flag");  rpcSlice->setCrossLayerBLAFlag( uiCode ? true : false );
2876      iBits++;
2877    }
2878    for ( ; iBits < rpcSlice->getPPS()->getNumExtraSliceHeaderBits(); iBits++)
2879#else
2880    for (Int i = 1; i < rpcSlice->getPPS()->getNumExtraSliceHeaderBits(); i++)
2881#endif
2882    {
2883      READ_FLAG(uiCode, "slice_reserved_undetermined_flag[]"); // ignored
2884    }
2885#endif
2886#else //SVC_EXTENSION
2887    for (Int i = 0; i < rpcSlice->getPPS()->getNumExtraSliceHeaderBits(); i++)
2888    {
2889      READ_FLAG(uiCode, "slice_reserved_undetermined_flag[]"); // ignored
2890    }
2891#endif //SVC_EXTENSION
2892
2893    READ_UVLC (    uiCode, "slice_type" );            rpcSlice->setSliceType((SliceType)uiCode);
2894    if( pps->getOutputFlagPresentFlag() )
2895    {
2896      READ_FLAG( uiCode, "pic_output_flag" );    rpcSlice->setPicOutputFlag( uiCode ? true : false );
2897    }
2898    else
2899    {
2900      rpcSlice->setPicOutputFlag( true );
2901    }
2902    // in the first version chroma_format_idc is equal to one, thus colour_plane_id will not be present
2903    assert (sps->getChromaFormatIdc() == 1 );
2904    // if( separate_colour_plane_flag  ==  1 )
2905    //   colour_plane_id                                      u(2)
2906
2907    if( rpcSlice->getIdrPicFlag() )
2908    {
2909      rpcSlice->setPOC(0);
2910      TComReferencePictureSet* rps = rpcSlice->getLocalRPS();
2911      rps->setNumberOfNegativePictures(0);
2912      rps->setNumberOfPositivePictures(0);
2913      rps->setNumberOfLongtermPictures(0);
2914      rps->setNumberOfPictures(0);
2915      rpcSlice->setRPS(rps);
2916    }
2917#if N0065_LAYER_POC_ALIGNMENT
2918#if !Q0142_POC_LSB_NOT_PRESENT
2919#if SHM_FIX7
2920    Int iPOClsb = 0;
2921#endif
2922#endif
2923#if O0062_POC_LSB_NOT_PRESENT_FLAG
2924    if( ( rpcSlice->getLayerId() > 0 && !rpcSlice->getVPS()->getPocLsbNotPresentFlag( rpcSlice->getVPS()->getLayerIdInVps(rpcSlice->getLayerId())) ) || !rpcSlice->getIdrPicFlag())
2925#else
2926    if( rpcSlice->getLayerId() > 0 || !rpcSlice->getIdrPicFlag() )
2927#endif
2928#else
2929    else
2930#endif
2931    {
2932      READ_CODE(sps->getBitsForPOC(), uiCode, "pic_order_cnt_lsb");
2933#if POC_RESET_IDC_DECODER
2934      rpcSlice->setPicOrderCntLsb( uiCode );
2935#endif
2936#if SHM_FIX7
2937      iPOClsb = uiCode;
2938#else
2939      Int iPOClsb = uiCode;
2940#endif
2941      Int iPrevPOC = rpcSlice->getPrevTid0POC();
2942      Int iMaxPOClsb = 1<< sps->getBitsForPOC();
2943      Int iPrevPOClsb = iPrevPOC & (iMaxPOClsb - 1);
2944      Int iPrevPOCmsb = iPrevPOC-iPrevPOClsb;
2945      Int iPOCmsb;
2946      if( ( iPOClsb  <  iPrevPOClsb ) && ( ( iPrevPOClsb - iPOClsb )  >=  ( iMaxPOClsb / 2 ) ) )
2947      {
2948        iPOCmsb = iPrevPOCmsb + iMaxPOClsb;
2949      }
2950      else if( (iPOClsb  >  iPrevPOClsb )  && ( (iPOClsb - iPrevPOClsb )  >  ( iMaxPOClsb / 2 ) ) )
2951      {
2952        iPOCmsb = iPrevPOCmsb - iMaxPOClsb;
2953      }
2954      else
2955      {
2956        iPOCmsb = iPrevPOCmsb;
2957      }
2958      if ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
2959        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
2960        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
2961      {
2962        // For BLA picture types, POCmsb is set to 0.
2963        iPOCmsb = 0;
2964      }
2965      rpcSlice->setPOC              (iPOCmsb+iPOClsb);
2966
2967#if N0065_LAYER_POC_ALIGNMENT
2968#if SHM_FIX7
2969    }
2970#endif
2971#if POC_RESET_IDC_DECODER
2972  else
2973  {
2974    rpcSlice->setPicOrderCntLsb( 0 );
2975  }
2976#endif
2977  if( !rpcSlice->getIdrPicFlag() )
2978  {
2979#endif
2980    TComReferencePictureSet* rps;
2981    rps = rpcSlice->getLocalRPS();
2982    rpcSlice->setRPS(rps);
2983    READ_FLAG( uiCode, "short_term_ref_pic_set_sps_flag" );
2984    if(uiCode == 0) // use short-term reference picture set explicitly signalled in slice header
2985    {
2986      parseShortTermRefPicSet(sps,rps, sps->getRPSList()->getNumberOfReferencePictureSets());
2987    }
2988    else // use reference to short-term reference picture set in PPS
2989    {
2990      Int numBits = 0;
2991      while ((1 << numBits) < rpcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets())
2992      {
2993        numBits++;
2994      }
2995      if (numBits > 0)
2996      {
2997        READ_CODE( numBits, uiCode, "short_term_ref_pic_set_idx");
2998      }
2999      else
3000      {
3001        uiCode = 0;       
3002      }
3003      *rps = *(sps->getRPSList()->getReferencePictureSet(uiCode));
3004    }
3005    if(sps->getLongTermRefsPresent())
3006    {
3007      Int offset = rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures();
3008      UInt numOfLtrp = 0;
3009      UInt numLtrpInSPS = 0;
3010      if (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > 0)
3011      {
3012        READ_UVLC( uiCode, "num_long_term_sps");
3013        numLtrpInSPS = uiCode;
3014        numOfLtrp += numLtrpInSPS;
3015        rps->setNumberOfLongtermPictures(numOfLtrp);
3016      }
3017      Int bitsForLtrpInSPS = 0;
3018      while (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
3019      {
3020        bitsForLtrpInSPS++;
3021      }
3022      READ_UVLC( uiCode, "num_long_term_pics");             rps->setNumberOfLongtermPictures(uiCode);
3023      numOfLtrp += uiCode;
3024      rps->setNumberOfLongtermPictures(numOfLtrp);
3025      Int maxPicOrderCntLSB = 1 << rpcSlice->getSPS()->getBitsForPOC();
3026      Int prevDeltaMSB = 0, deltaPocMSBCycleLT = 0;;
3027      for(Int j=offset+rps->getNumberOfLongtermPictures()-1, k = 0; k < numOfLtrp; j--, k++)
3028      {
3029        Int pocLsbLt;
3030        if (k < numLtrpInSPS)
3031        {
3032          uiCode = 0;
3033          if (bitsForLtrpInSPS > 0)
3034          {
3035            READ_CODE(bitsForLtrpInSPS, uiCode, "lt_idx_sps[i]");
3036          }
3037          Int usedByCurrFromSPS=rpcSlice->getSPS()->getUsedByCurrPicLtSPSFlag(uiCode);
3038
3039          pocLsbLt = rpcSlice->getSPS()->getLtRefPicPocLsbSps(uiCode);
3040          rps->setUsed(j,usedByCurrFromSPS);
3041        }
3042        else
3043        {
3044          READ_CODE(rpcSlice->getSPS()->getBitsForPOC(), uiCode, "poc_lsb_lt"); pocLsbLt= uiCode;
3045          READ_FLAG( uiCode, "used_by_curr_pic_lt_flag");     rps->setUsed(j,uiCode);
3046        }
3047        READ_FLAG(uiCode,"delta_poc_msb_present_flag");
3048        Bool mSBPresentFlag = uiCode ? true : false;
3049        if(mSBPresentFlag)
3050        {
3051          READ_UVLC( uiCode, "delta_poc_msb_cycle_lt[i]" );
3052          Bool deltaFlag = false;
3053          //            First LTRP                               || First LTRP from SH
3054          if( (j == offset+rps->getNumberOfLongtermPictures()-1) || (j == offset+(numOfLtrp-numLtrpInSPS)-1) )
3055          {
3056            deltaFlag = true;
3057          }
3058          if(deltaFlag)
3059          {
3060            deltaPocMSBCycleLT = uiCode;
3061          }
3062          else
3063          {
3064            deltaPocMSBCycleLT = uiCode + prevDeltaMSB;
3065          }
3066
3067          Int pocLTCurr = rpcSlice->getPOC() - deltaPocMSBCycleLT * maxPicOrderCntLSB
3068            - iPOClsb + pocLsbLt;
3069          rps->setPOC     (j, pocLTCurr);
3070          rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLTCurr);
3071          rps->setCheckLTMSBPresent(j,true);
3072        }
3073        else
3074        {
3075          rps->setPOC     (j, pocLsbLt);
3076          rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLsbLt);
3077          rps->setCheckLTMSBPresent(j,false);
3078
3079          // reset deltaPocMSBCycleLT for first LTRP from slice header if MSB not present
3080          if( j == offset+(numOfLtrp-numLtrpInSPS)-1 )
3081          {
3082            deltaPocMSBCycleLT = 0;
3083          }
3084        }
3085        prevDeltaMSB = deltaPocMSBCycleLT;
3086      }
3087      offset += rps->getNumberOfLongtermPictures();
3088      rps->setNumberOfPictures(offset);
3089    }
3090#if DPB_CONSTRAINTS
3091    if(rpcSlice->getVPS()->getVpsExtensionFlag()==1)
3092    {
3093#if Q0078_ADD_LAYER_SETS
3094      for (Int ii = 1; ii < (rpcSlice->getVPS()->getVpsNumLayerSetsMinus1() + 1); ii++)  // prevent assert error when num_add_layer_sets > 0
3095#else
3096      for (Int ii=1; ii< rpcSlice->getVPS()->getNumOutputLayerSets(); ii++ )
3097#endif
3098      {
3099        Int layerSetIdxForOutputLayerSet = rpcSlice->getVPS()->getOutputLayerSetIdx( ii );
3100        Int chkAssert=0;
3101        for(Int kk = 0; kk < rpcSlice->getVPS()->getNumLayersInIdList(layerSetIdxForOutputLayerSet); kk++)
3102        {
3103          if(rpcSlice->getLayerId()==rpcSlice->getVPS()->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, kk))
3104          {
3105            chkAssert=1;
3106          }
3107        }
3108        if(chkAssert)
3109        {
3110          // There may be something wrong here (layer id assumed to be layer idx?)
3111          assert(rps->getNumberOfNegativePictures() <= rpcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii , rpcSlice->getLayerId() , rpcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)));
3112          assert(rps->getNumberOfPositivePictures() <= rpcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii , rpcSlice->getLayerId() , rpcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)) - rps->getNumberOfNegativePictures());
3113          assert((rps->getNumberOfPositivePictures() + rps->getNumberOfNegativePictures() + rps->getNumberOfLongtermPictures()) <= rpcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii , rpcSlice->getLayerId() , rpcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)));
3114        }
3115      }
3116
3117
3118    }
3119    if(rpcSlice->getLayerId() == 0)
3120    {
3121      assert(rps->getNumberOfNegativePictures() <= rpcSlice->getSPS()->getMaxDecPicBuffering(rpcSlice->getSPS()->getMaxTLayers()-1) );
3122      assert(rps->getNumberOfPositivePictures() <= rpcSlice->getSPS()->getMaxDecPicBuffering(rpcSlice->getSPS()->getMaxTLayers()-1) -rps->getNumberOfNegativePictures());
3123      assert((rps->getNumberOfPositivePictures() + rps->getNumberOfNegativePictures() + rps->getNumberOfLongtermPictures()) <= rpcSlice->getSPS()->getMaxDecPicBuffering(rpcSlice->getSPS()->getMaxTLayers()-1));
3124    }
3125#endif
3126    if ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
3127      || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
3128      || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
3129    {
3130      // In the case of BLA picture types, rps data is read from slice header but ignored
3131      rps = rpcSlice->getLocalRPS();
3132      rps->setNumberOfNegativePictures(0);
3133      rps->setNumberOfPositivePictures(0);
3134      rps->setNumberOfLongtermPictures(0);
3135      rps->setNumberOfPictures(0);
3136      rpcSlice->setRPS(rps);
3137    }
3138    if (rpcSlice->getSPS()->getTMVPFlagsPresent())
3139    {
3140#if R0226_SLICE_TMVP
3141      READ_FLAG( uiCode, "slice_temporal_mvp_enabled_flag" );
3142#else
3143      READ_FLAG( uiCode, "slice_temporal_mvp_enable_flag" );
3144#endif
3145      rpcSlice->setEnableTMVPFlag( uiCode == 1 ? true : false );
3146    }
3147    else
3148    {
3149      rpcSlice->setEnableTMVPFlag(false);
3150    }
3151#if N0065_LAYER_POC_ALIGNMENT && !SHM_FIX7
3152  }
3153#endif
3154  }
3155
3156#if SVC_EXTENSION
3157  rpcSlice->setActiveNumILRRefIdx(0);
3158  if((rpcSlice->getLayerId() > 0) && !(rpcSlice->getVPS()->getIlpSshSignalingEnabledFlag()) && (rpcSlice->getNumILRRefIdx() > 0) )
3159  {
3160    READ_FLAG(uiCode,"inter_layer_pred_enabled_flag");
3161    rpcSlice->setInterLayerPredEnabledFlag(uiCode);
3162    if( rpcSlice->getInterLayerPredEnabledFlag())
3163    {
3164      if(rpcSlice->getNumILRRefIdx() > 1)
3165      {
3166        Int numBits = 1;
3167        while ((1 << numBits) < rpcSlice->getNumILRRefIdx())
3168        {
3169          numBits++;
3170        }
3171        if( !rpcSlice->getVPS()->getMaxOneActiveRefLayerFlag())
3172        {
3173          READ_CODE( numBits, uiCode,"num_inter_layer_ref_pics_minus1" );
3174          rpcSlice->setActiveNumILRRefIdx(uiCode + 1);
3175        }
3176        else
3177        {
3178#if P0079_DERIVE_NUMACTIVE_REF_PICS
3179          for( Int i = 0; i < rpcSlice->getNumILRRefIdx(); i++ ) 
3180          {
3181#if Q0060_MAX_TID_REF_EQUAL_TO_ZERO
3182            if((rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() || rpcSlice->getTLayer()==0) &&
3183              (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
3184#else
3185            if(rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() &&
3186              (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
3187#endif
3188            {         
3189              rpcSlice->setActiveNumILRRefIdx(1);
3190              break;
3191            }
3192          }
3193#else
3194          rpcSlice->setActiveNumILRRefIdx(1);
3195#endif
3196        }
3197
3198        if( rpcSlice->getActiveNumILRRefIdx() == rpcSlice->getNumILRRefIdx() )
3199        {
3200          for( Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
3201          {
3202            rpcSlice->setInterLayerPredLayerIdc(i,i);
3203          }
3204        }
3205        else
3206        {
3207          for(Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
3208          {
3209            READ_CODE( numBits,uiCode,"inter_layer_pred_layer_idc[i]" );
3210            rpcSlice->setInterLayerPredLayerIdc(uiCode,i);
3211          }
3212        }
3213      }
3214      else
3215      {
3216#if O0225_TID_BASED_IL_RPS_DERIV && TSLAYERS_IL_RPS
3217#if Q0060_MAX_TID_REF_EQUAL_TO_ZERO
3218        if((rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(0,rpcSlice->getLayerId()) >  rpcSlice->getTLayer() || rpcSlice->getTLayer()==0) &&
3219          (rpcSlice->getVPS()->getMaxTSLayersMinus1(0) >=  rpcSlice->getTLayer()) )
3220#else
3221        if( (rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(0,rpcSlice->getLayerId()) >  rpcSlice->getTLayer()) &&
3222          (rpcSlice->getVPS()->getMaxTSLayersMinus1(0) >=  rpcSlice->getTLayer()) )
3223#endif
3224        {
3225#endif
3226          rpcSlice->setActiveNumILRRefIdx(1);
3227          rpcSlice->setInterLayerPredLayerIdc(0,0);
3228#if O0225_TID_BASED_IL_RPS_DERIV && TSLAYERS_IL_RPS
3229        }
3230#endif
3231      }
3232    }
3233  }
3234  else if( rpcSlice->getVPS()->getIlpSshSignalingEnabledFlag() == true &&  (rpcSlice->getLayerId() > 0 ))
3235  {
3236    rpcSlice->setInterLayerPredEnabledFlag(true);
3237
3238#if O0225_TID_BASED_IL_RPS_DERIV && TSLAYERS_IL_RPS
3239    Int   numRefLayerPics = 0;
3240    Int   i = 0;
3241    Int   refLayerPicIdc  [MAX_VPS_LAYER_ID_PLUS1];
3242    for(i = 0, numRefLayerPics = 0;  i < rpcSlice->getNumILRRefIdx(); i++ ) 
3243    {
3244#if Q0060_MAX_TID_REF_EQUAL_TO_ZERO
3245      if((rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() || rpcSlice->getTLayer()==0) &&
3246        (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
3247#else
3248      if(rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() &&
3249        (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
3250#endif
3251      {         
3252        refLayerPicIdc[ numRefLayerPics++ ] = i;
3253      }
3254    }
3255    rpcSlice->setActiveNumILRRefIdx(numRefLayerPics);
3256    for( i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
3257    {
3258      rpcSlice->setInterLayerPredLayerIdc(refLayerPicIdc[i],i);
3259    }     
3260#else
3261    rpcSlice->setActiveNumILRRefIdx(rpcSlice->getNumILRRefIdx());
3262    for( Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
3263    {
3264      rpcSlice->setInterLayerPredLayerIdc(i,i);
3265    }
3266#endif
3267  }
3268#if P0312_VERT_PHASE_ADJ
3269    for(Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ ) 
3270    {
3271      UInt refLayerIdc = rpcSlice->getInterLayerPredLayerIdc(i);
3272#if !MOVE_SCALED_OFFSET_TO_PPS
3273      if( rpcSlice->getSPS()->getVertPhasePositionEnableFlag(refLayerIdc) )
3274#else
3275      if( rpcSlice->getPPS()->getVertPhasePositionEnableFlag(refLayerIdc) )
3276#endif
3277      {
3278        READ_FLAG( uiCode, "vert_phase_position_flag" ); rpcSlice->setVertPhasePositionFlag( uiCode? true : false, refLayerIdc );
3279      }
3280  }
3281#endif
3282#endif //SVC_EXTENSION
3283
3284  if(sps->getUseSAO())
3285  {
3286    READ_FLAG(uiCode, "slice_sao_luma_flag");  rpcSlice->setSaoEnabledFlag((Bool)uiCode);
3287#if AUXILIARY_PICTURES
3288    ChromaFormat format;
3289#if REPN_FORMAT_IN_VPS
3290#if O0096_REP_FORMAT_INDEX
3291    if( sps->getLayerId() == 0 )
3292    {
3293      format = sps->getChromaFormatIdc();
3294    }
3295    else
3296    {
3297      format = rpcSlice->getVPS()->getVpsRepFormat( sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : rpcSlice->getVPS()->getVpsRepFormatIdx(sps->getLayerId()) )->getChromaFormatVpsIdc();
3298#if Q0195_REP_FORMAT_CLEANUP
3299      assert( (sps->getUpdateRepFormatFlag()==false && rpcSlice->getVPS()->getVpsNumRepFormats()==1) || rpcSlice->getVPS()->getVpsNumRepFormats() > 1 ); //conformance check
3300#endif
3301    }
3302#else
3303    if( ( sps->getLayerId() == 0 ) || sps->getUpdateRepFormatFlag() )
3304    {
3305      format = sps->getChromaFormatIdc();
3306    }
3307    else
3308    {
3309      format = rpcSlice->getVPS()->getVpsRepFormat( rpcSlice->getVPS()->getVpsRepFormatIdx(sps->getLayerId()) )->getChromaFormatVpsIdc();
3310    }
3311#endif
3312#else
3313    format = sps->getChromaFormatIdc();
3314#endif
3315    if (format != CHROMA_400)
3316    {
3317#endif
3318      READ_FLAG(uiCode, "slice_sao_chroma_flag");  rpcSlice->setSaoEnabledFlagChroma((Bool)uiCode);
3319#if AUXILIARY_PICTURES
3320    }
3321    else
3322    {
3323      rpcSlice->setSaoEnabledFlagChroma(false);
3324    }
3325#endif
3326  }
3327
3328  if (rpcSlice->getIdrPicFlag())
3329  {
3330    rpcSlice->setEnableTMVPFlag(false);
3331  }
3332  if (!rpcSlice->isIntra())
3333  {
3334
3335    READ_FLAG( uiCode, "num_ref_idx_active_override_flag");
3336    if (uiCode)
3337    {
3338      READ_UVLC (uiCode, "num_ref_idx_l0_active_minus1" );  rpcSlice->setNumRefIdx( REF_PIC_LIST_0, uiCode + 1 );
3339      if (rpcSlice->isInterB())
3340      {
3341        READ_UVLC (uiCode, "num_ref_idx_l1_active_minus1" );  rpcSlice->setNumRefIdx( REF_PIC_LIST_1, uiCode + 1 );
3342      }
3343      else
3344      {
3345        rpcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
3346      }
3347    }
3348    else
3349    {
3350      rpcSlice->setNumRefIdx(REF_PIC_LIST_0, rpcSlice->getPPS()->getNumRefIdxL0DefaultActive());
3351      if (rpcSlice->isInterB())
3352      {
3353        rpcSlice->setNumRefIdx(REF_PIC_LIST_1, rpcSlice->getPPS()->getNumRefIdxL1DefaultActive());
3354      }
3355      else
3356      {
3357        rpcSlice->setNumRefIdx(REF_PIC_LIST_1,0);
3358      }
3359    }
3360  }
3361  // }
3362  TComRefPicListModification* refPicListModification = rpcSlice->getRefPicListModification();
3363  if(!rpcSlice->isIntra())
3364  {
3365    if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
3366    {
3367      refPicListModification->setRefPicListModificationFlagL0( 0 );
3368    }
3369    else
3370    {
3371      READ_FLAG( uiCode, "ref_pic_list_modification_flag_l0" ); refPicListModification->setRefPicListModificationFlagL0( uiCode ? 1 : 0 );
3372    }
3373
3374    if(refPicListModification->getRefPicListModificationFlagL0())
3375    {
3376      uiCode = 0;
3377      Int i = 0;
3378      Int numRpsCurrTempList0 = rpcSlice->getNumRpsCurrTempList();
3379      if ( numRpsCurrTempList0 > 1 )
3380      {
3381        Int length = 1;
3382        numRpsCurrTempList0 --;
3383        while ( numRpsCurrTempList0 >>= 1)
3384        {
3385          length ++;
3386        }
3387        for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
3388        {
3389          READ_CODE( length, uiCode, "list_entry_l0" );
3390          refPicListModification->setRefPicSetIdxL0(i, uiCode );
3391        }
3392      }
3393      else
3394      {
3395        for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
3396        {
3397          refPicListModification->setRefPicSetIdxL0(i, 0 );
3398        }
3399      }
3400    }
3401  }
3402  else
3403  {
3404    refPicListModification->setRefPicListModificationFlagL0(0);
3405  }
3406  if(rpcSlice->isInterB())
3407  {
3408    if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
3409    {
3410      refPicListModification->setRefPicListModificationFlagL1( 0 );
3411    }
3412    else
3413    {
3414      READ_FLAG( uiCode, "ref_pic_list_modification_flag_l1" ); refPicListModification->setRefPicListModificationFlagL1( uiCode ? 1 : 0 );
3415    }
3416    if(refPicListModification->getRefPicListModificationFlagL1())
3417    {
3418      uiCode = 0;
3419      Int i = 0;
3420      Int numRpsCurrTempList1 = rpcSlice->getNumRpsCurrTempList();
3421      if ( numRpsCurrTempList1 > 1 )
3422      {
3423        Int length = 1;
3424        numRpsCurrTempList1 --;
3425        while ( numRpsCurrTempList1 >>= 1)
3426        {
3427          length ++;
3428        }
3429        for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
3430        {
3431          READ_CODE( length, uiCode, "list_entry_l1" );
3432          refPicListModification->setRefPicSetIdxL1(i, uiCode );
3433        }
3434      }
3435      else
3436      {
3437        for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
3438        {
3439          refPicListModification->setRefPicSetIdxL1(i, 0 );
3440        }
3441      }
3442    }
3443  }
3444  else
3445  {
3446    refPicListModification->setRefPicListModificationFlagL1(0);
3447  }
3448  if (rpcSlice->isInterB())
3449  {
3450    READ_FLAG( uiCode, "mvd_l1_zero_flag" );       rpcSlice->setMvdL1ZeroFlag( (uiCode ? true : false) );
3451  }
3452
3453  rpcSlice->setCabacInitFlag( false ); // default
3454  if(pps->getCabacInitPresentFlag() && !rpcSlice->isIntra())
3455  {
3456    READ_FLAG(uiCode, "cabac_init_flag");
3457    rpcSlice->setCabacInitFlag( uiCode ? true : false );
3458  }
3459
3460  if ( rpcSlice->getEnableTMVPFlag() )
3461  {
3462#if SVC_EXTENSION && REF_IDX_MFM
3463    // set motion mapping flag
3464    rpcSlice->setMFMEnabledFlag( ( rpcSlice->getNumMotionPredRefLayers() > 0 && rpcSlice->getActiveNumILRRefIdx() && !rpcSlice->isIntra() ) ? true : false );
3465#endif
3466    if ( rpcSlice->getSliceType() == B_SLICE )
3467    {
3468      READ_FLAG( uiCode, "collocated_from_l0_flag" );
3469      rpcSlice->setColFromL0Flag(uiCode);
3470    }
3471    else
3472    {
3473      rpcSlice->setColFromL0Flag( 1 );
3474    }
3475
3476    if ( rpcSlice->getSliceType() != I_SLICE &&
3477      ((rpcSlice->getColFromL0Flag() == 1 && rpcSlice->getNumRefIdx(REF_PIC_LIST_0) > 1)||
3478      (rpcSlice->getColFromL0Flag() == 0 && rpcSlice->getNumRefIdx(REF_PIC_LIST_1) > 1)))
3479    {
3480      READ_UVLC( uiCode, "collocated_ref_idx" );
3481      rpcSlice->setColRefIdx(uiCode);
3482    }
3483    else
3484    {
3485      rpcSlice->setColRefIdx(0);
3486    }
3487  }
3488  if ( (pps->getUseWP() && rpcSlice->getSliceType()==P_SLICE) || (pps->getWPBiPred() && rpcSlice->getSliceType()==B_SLICE) )
3489  {
3490    xParsePredWeightTable(rpcSlice);
3491    rpcSlice->initWpScaling();
3492  }
3493  if (!rpcSlice->isIntra())
3494  {
3495    READ_UVLC( uiCode, "five_minus_max_num_merge_cand");
3496    rpcSlice->setMaxNumMergeCand(MRG_MAX_NUM_CANDS - uiCode);
3497  }
3498
3499  READ_SVLC( iCode, "slice_qp_delta" );
3500  rpcSlice->setSliceQp (26 + pps->getPicInitQPMinus26() + iCode);
3501
3502#if REPN_FORMAT_IN_VPS
3503#if O0194_DIFFERENT_BITDEPTH_EL_BL
3504  g_bitDepthYLayer[rpcSlice->getLayerId()] = rpcSlice->getBitDepthY();
3505  g_bitDepthCLayer[rpcSlice->getLayerId()] = rpcSlice->getBitDepthC();
3506#endif
3507  assert( rpcSlice->getSliceQp() >= -rpcSlice->getQpBDOffsetY() );
3508#else
3509  assert( rpcSlice->getSliceQp() >= -sps->getQpBDOffsetY() );
3510#endif
3511  assert( rpcSlice->getSliceQp() <=  51 );
3512
3513  if (rpcSlice->getPPS()->getSliceChromaQpFlag())
3514  {
3515    READ_SVLC( iCode, "slice_qp_delta_cb" );
3516    rpcSlice->setSliceQpDeltaCb( iCode );
3517    assert( rpcSlice->getSliceQpDeltaCb() >= -12 );
3518    assert( rpcSlice->getSliceQpDeltaCb() <=  12 );
3519    assert( (rpcSlice->getPPS()->getChromaCbQpOffset() + rpcSlice->getSliceQpDeltaCb()) >= -12 );
3520    assert( (rpcSlice->getPPS()->getChromaCbQpOffset() + rpcSlice->getSliceQpDeltaCb()) <=  12 );
3521
3522    READ_SVLC( iCode, "slice_qp_delta_cr" );
3523    rpcSlice->setSliceQpDeltaCr( iCode );
3524    assert( rpcSlice->getSliceQpDeltaCr() >= -12 );
3525    assert( rpcSlice->getSliceQpDeltaCr() <=  12 );
3526    assert( (rpcSlice->getPPS()->getChromaCrQpOffset() + rpcSlice->getSliceQpDeltaCr()) >= -12 );
3527    assert( (rpcSlice->getPPS()->getChromaCrQpOffset() + rpcSlice->getSliceQpDeltaCr()) <=  12 );
3528  }
3529
3530  if (rpcSlice->getPPS()->getDeblockingFilterControlPresentFlag())
3531  {
3532    if(rpcSlice->getPPS()->getDeblockingFilterOverrideEnabledFlag())
3533    {
3534      READ_FLAG ( uiCode, "deblocking_filter_override_flag" );        rpcSlice->setDeblockingFilterOverrideFlag(uiCode ? true : false);
3535    }
3536    else
3537    {
3538      rpcSlice->setDeblockingFilterOverrideFlag(0);
3539    }
3540    if(rpcSlice->getDeblockingFilterOverrideFlag())
3541    {
3542      READ_FLAG ( uiCode, "slice_disable_deblocking_filter_flag" );   rpcSlice->setDeblockingFilterDisable(uiCode ? 1 : 0);
3543      if(!rpcSlice->getDeblockingFilterDisable())
3544      {
3545        READ_SVLC( iCode, "slice_beta_offset_div2" );                       rpcSlice->setDeblockingFilterBetaOffsetDiv2(iCode);
3546        assert(rpcSlice->getDeblockingFilterBetaOffsetDiv2() >= -6 &&
3547          rpcSlice->getDeblockingFilterBetaOffsetDiv2() <=  6);
3548        READ_SVLC( iCode, "slice_tc_offset_div2" );                         rpcSlice->setDeblockingFilterTcOffsetDiv2(iCode);
3549        assert(rpcSlice->getDeblockingFilterTcOffsetDiv2() >= -6 &&
3550          rpcSlice->getDeblockingFilterTcOffsetDiv2() <=  6);
3551      }
3552    }
3553    else
3554    {
3555      rpcSlice->setDeblockingFilterDisable   ( rpcSlice->getPPS()->getPicDisableDeblockingFilterFlag() );
3556      rpcSlice->setDeblockingFilterBetaOffsetDiv2( rpcSlice->getPPS()->getDeblockingFilterBetaOffsetDiv2() );
3557      rpcSlice->setDeblockingFilterTcOffsetDiv2  ( rpcSlice->getPPS()->getDeblockingFilterTcOffsetDiv2() );
3558    }
3559  }
3560  else
3561  {
3562    rpcSlice->setDeblockingFilterDisable       ( false );
3563    rpcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
3564    rpcSlice->setDeblockingFilterTcOffsetDiv2  ( 0 );
3565  }
3566
3567  Bool isSAOEnabled = (!rpcSlice->getSPS()->getUseSAO())?(false):(rpcSlice->getSaoEnabledFlag()||rpcSlice->getSaoEnabledFlagChroma());
3568  Bool isDBFEnabled = (!rpcSlice->getDeblockingFilterDisable());
3569
3570  if(rpcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
3571  {
3572    READ_FLAG( uiCode, "slice_loop_filter_across_slices_enabled_flag");
3573  }
3574  else
3575  {
3576    uiCode = rpcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag()?1:0;
3577  }
3578  rpcSlice->setLFCrossSliceBoundaryFlag( (uiCode==1)?true:false);
3579
3580}
3581
3582UInt *entryPointOffset          = NULL;
3583UInt numEntryPointOffsets, offsetLenMinus1;
3584if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
3585{
3586  READ_UVLC(numEntryPointOffsets, "num_entry_point_offsets"); rpcSlice->setNumEntryPointOffsets ( numEntryPointOffsets );
3587  if (numEntryPointOffsets>0)
3588  {
3589    READ_UVLC(offsetLenMinus1, "offset_len_minus1");
3590  }
3591  entryPointOffset = new UInt[numEntryPointOffsets];
3592  for (UInt idx=0; idx<numEntryPointOffsets; idx++)
3593  {
3594    READ_CODE(offsetLenMinus1+1, uiCode, "entry_point_offset_minus1");
3595    entryPointOffset[ idx ] = uiCode + 1;
3596  }
3597}
3598else
3599{
3600  rpcSlice->setNumEntryPointOffsets ( 0 );
3601}
3602
3603#if POC_RESET_IDC_SIGNALLING
3604Int sliceHeaderExtensionLength = 0;
3605if(pps->getSliceHeaderExtensionPresentFlag())
3606{
3607  READ_UVLC( uiCode, "slice_header_extension_length"); sliceHeaderExtensionLength = uiCode;
3608}
3609else
3610{
3611  sliceHeaderExtensionLength = 0;
3612#if INFERENCE_POC_MSB_VAL_PRESENT
3613  rpcSlice->setPocMsbValPresentFlag( false );
3614#endif
3615}
3616UInt startBits = m_pcBitstream->getNumBitsRead();     // Start counter of # SH Extn bits
3617if( sliceHeaderExtensionLength > 0 )
3618{
3619  if( rpcSlice->getPPS()->getPocResetInfoPresentFlag() )
3620  {
3621    READ_CODE( 2, uiCode,       "poc_reset_idc"); rpcSlice->setPocResetIdc(uiCode);
3622#if POC_RESET_RESTRICTIONS
3623    /* The value of poc_reset_idc shall not be equal to 1 or 2 for a RASL picture, a RADL picture,
3624       a sub-layer non-reference picture, or a picture that has TemporalId greater than 0,
3625       or a picture that has discardable_flag equal to 1. */
3626    if( rpcSlice->getPocResetIdc() == 1 || rpcSlice->getPocResetIdc() == 2 )
3627    {
3628      assert( !rpcSlice->isRASL() );
3629      assert( !rpcSlice->isRADL() );
3630      assert( !rpcSlice->isSLNR() );
3631      assert( rpcSlice->getTLayer() == 0 );
3632      assert( rpcSlice->getDiscardableFlag() == 0 );
3633    }
3634
3635    // The value of poc_reset_idc of a CRA or BLA picture shall be less than 3.
3636    if( rpcSlice->getPocResetIdc() == 3)
3637    {
3638      assert( ! ( rpcSlice->isCRA() || rpcSlice->isBLA() ) );
3639    }
3640#endif
3641  }
3642  else
3643  {
3644    rpcSlice->setPocResetIdc( 0 );
3645  }
3646#if Q0142_POC_LSB_NOT_PRESENT
3647  if ( rpcSlice->getVPS()->getPocLsbNotPresentFlag(rpcSlice->getLayerId()) && iPOClsb > 0 )
3648  {
3649    assert( rpcSlice->getPocResetIdc() != 2 );
3650  }
3651#endif
3652  if( rpcSlice->getPocResetIdc() > 0 )
3653  {
3654    READ_CODE(6, uiCode,      "poc_reset_period_id"); rpcSlice->setPocResetPeriodId(uiCode);
3655  }
3656  else
3657  {
3658
3659    rpcSlice->setPocResetPeriodId( 0 );
3660  }
3661
3662  if (rpcSlice->getPocResetIdc() == 3)
3663  {
3664    READ_FLAG( uiCode,        "full_poc_reset_flag"); rpcSlice->setFullPocResetFlag((uiCode == 1) ? true : false);
3665    READ_CODE(rpcSlice->getSPS()->getBitsForPOC(), uiCode,"poc_lsb_val"); rpcSlice->setPocLsbVal(uiCode);
3666#if Q0142_POC_LSB_NOT_PRESENT
3667    if ( rpcSlice->getVPS()->getPocLsbNotPresentFlag(rpcSlice->getLayerId()) && rpcSlice->getFullPocResetFlag() )
3668    {
3669      assert( rpcSlice->getPocLsbVal() == 0 );
3670    }
3671#endif
3672  }
3673
3674  // Derive the value of PocMsbValRequiredFlag
3675  rpcSlice->setPocMsbValRequiredFlag( rpcSlice->getCraPicFlag() || rpcSlice->getBlaPicFlag()
3676    /* || related to vps_poc_lsb_aligned_flag */
3677    );
3678
3679  if( !rpcSlice->getPocMsbValRequiredFlag() /* vps_poc_lsb_aligned_flag */ )
3680  {
3681    READ_FLAG( uiCode,    "poc_msb_val_present_flag"); rpcSlice->setPocMsbValPresentFlag( uiCode ? true : false );
3682  }
3683  else
3684  {
3685#if POC_MSB_VAL_PRESENT_FLAG_SEM
3686    if( sliceHeaderExtensionLength == 0 )
3687    {
3688      rpcSlice->setPocMsbValPresentFlag( false );
3689    }
3690    else if( rpcSlice->getPocMsbValRequiredFlag() )
3691#else
3692    if( rpcSlice->getPocMsbValRequiredFlag() )
3693#endif
3694    {
3695      rpcSlice->setPocMsbValPresentFlag( true );
3696    }
3697    else
3698    {
3699      rpcSlice->setPocMsbValPresentFlag( false );
3700    }
3701  }
3702
3703#if !POC_RESET_IDC_DECODER
3704  Int maxPocLsb  = 1 << rpcSlice->getSPS()->getBitsForPOC();
3705#endif
3706  if( rpcSlice->getPocMsbValPresentFlag() )
3707  {
3708    READ_UVLC( uiCode,    "poc_msb_val");             rpcSlice->setPocMsbVal( uiCode );
3709
3710#if !POC_RESET_IDC_DECODER
3711    // Update POC of the slice based on this MSB val
3712    Int pocLsb     = rpcSlice->getPOC() % maxPocLsb;
3713    rpcSlice->setPOC((rpcSlice->getPocMsbVal() * maxPocLsb) + pocLsb);
3714  }
3715  else
3716  {
3717    rpcSlice->setPocMsbVal( rpcSlice->getPOC() / maxPocLsb );
3718#endif
3719  }
3720
3721  // Read remaining bits in the slice header extension.
3722  UInt endBits = m_pcBitstream->getNumBitsRead();
3723  Int counter = (endBits - startBits) % 8;
3724  if( counter )
3725  {
3726    counter = 8 - counter;
3727  }
3728
3729  while( counter )
3730  {
3731#if Q0146_SSH_EXT_DATA_BIT
3732    READ_FLAG( uiCode, "slice_segment_header_extension_data_bit" );
3733#else
3734    READ_FLAG( uiCode, "slice_segment_header_extension_reserved_bit" ); assert( uiCode == 1 );
3735#endif
3736    counter--;
3737  }
3738}
3739#else
3740if(pps->getSliceHeaderExtensionPresentFlag())
3741{
3742  READ_UVLC(uiCode,"slice_header_extension_length");
3743  for(Int i=0; i<uiCode; i++)
3744  {
3745    UInt ignore;
3746    READ_CODE(8,ignore,"slice_header_extension_data_byte");
3747  }
3748}
3749#endif
3750m_pcBitstream->readByteAlignment();
3751
3752if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
3753{
3754  Int endOfSliceHeaderLocation = m_pcBitstream->getByteLocation();
3755
3756  // Adjust endOfSliceHeaderLocation to account for emulation prevention bytes in the slice segment header
3757  for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
3758  {
3759    if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) < endOfSliceHeaderLocation )
3760    {
3761      endOfSliceHeaderLocation++;
3762    }
3763  }
3764
3765  Int  curEntryPointOffset     = 0;
3766  Int  prevEntryPointOffset    = 0;
3767  for (UInt idx=0; idx<numEntryPointOffsets; idx++)
3768  {
3769    curEntryPointOffset += entryPointOffset[ idx ];
3770
3771    Int emulationPreventionByteCount = 0;
3772    for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
3773    {
3774      if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) >= ( prevEntryPointOffset + endOfSliceHeaderLocation ) &&
3775        m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) <  ( curEntryPointOffset  + endOfSliceHeaderLocation ) )
3776      {
3777        emulationPreventionByteCount++;
3778      }
3779    }
3780
3781    entryPointOffset[ idx ] -= emulationPreventionByteCount;
3782    prevEntryPointOffset = curEntryPointOffset;
3783  }
3784
3785  if ( pps->getTilesEnabledFlag() )
3786  {
3787    rpcSlice->setTileLocationCount( numEntryPointOffsets );
3788
3789    UInt prevPos = 0;
3790    for (Int idx=0; idx<rpcSlice->getTileLocationCount(); idx++)
3791    {
3792      rpcSlice->setTileLocation( idx, prevPos + entryPointOffset [ idx ] );
3793      prevPos += entryPointOffset[ idx ];
3794    }
3795  }
3796  else if ( pps->getEntropyCodingSyncEnabledFlag() )
3797  {
3798    Int numSubstreams = rpcSlice->getNumEntryPointOffsets()+1;
3799    rpcSlice->allocSubstreamSizes(numSubstreams);
3800    UInt *pSubstreamSizes       = rpcSlice->getSubstreamSizes();
3801    for (Int idx=0; idx<numSubstreams-1; idx++)
3802    {
3803      if ( idx < numEntryPointOffsets )
3804      {
3805        pSubstreamSizes[ idx ] = ( entryPointOffset[ idx ] << 3 ) ;
3806      }
3807      else
3808      {
3809        pSubstreamSizes[ idx ] = 0;
3810      }
3811    }
3812  }
3813
3814  if (entryPointOffset)
3815  {
3816    delete [] entryPointOffset;
3817  }
3818}
3819
3820return;
3821}
3822
3823Void TDecCavlc::parsePTL( TComPTL *rpcPTL, Bool profilePresentFlag, Int maxNumSubLayersMinus1 )
3824{
3825  UInt uiCode;
3826  if(profilePresentFlag)
3827  {
3828    parseProfileTier(rpcPTL->getGeneralPTL());
3829  }
3830  READ_CODE( 8, uiCode, "general_level_idc" );    rpcPTL->getGeneralPTL()->setLevelIdc(uiCode);
3831
3832  for (Int i = 0; i < maxNumSubLayersMinus1; i++)
3833  {
3834    if(profilePresentFlag)
3835    {
3836      READ_FLAG( uiCode, "sub_layer_profile_present_flag[i]" ); rpcPTL->setSubLayerProfilePresentFlag(i, uiCode);
3837    }
3838    READ_FLAG( uiCode, "sub_layer_level_present_flag[i]"   ); rpcPTL->setSubLayerLevelPresentFlag  (i, uiCode);
3839  }
3840
3841  if (maxNumSubLayersMinus1 > 0)
3842  {
3843    for (Int i = maxNumSubLayersMinus1; i < 8; i++)
3844    {
3845      READ_CODE(2, uiCode, "reserved_zero_2bits");
3846      assert(uiCode == 0);
3847    }
3848  }
3849
3850  for(Int i = 0; i < maxNumSubLayersMinus1; i++)
3851  {
3852    if( profilePresentFlag && rpcPTL->getSubLayerProfilePresentFlag(i) )
3853    {
3854      parseProfileTier(rpcPTL->getSubLayerPTL(i));
3855    }
3856    if(rpcPTL->getSubLayerLevelPresentFlag(i))
3857    {
3858      READ_CODE( 8, uiCode, "sub_layer_level_idc[i]" );   rpcPTL->getSubLayerPTL(i)->setLevelIdc(uiCode);
3859    }
3860  }
3861}
3862
3863Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl)
3864{
3865  UInt uiCode;
3866  READ_CODE(2 , uiCode, "XXX_profile_space[]");   ptl->setProfileSpace(uiCode);
3867  READ_FLAG(    uiCode, "XXX_tier_flag[]"    );   ptl->setTierFlag    (uiCode ? 1 : 0);
3868  READ_CODE(5 , uiCode, "XXX_profile_idc[]"  );   ptl->setProfileIdc  (uiCode);
3869  for(Int j = 0; j < 32; j++)
3870  {
3871    READ_FLAG(  uiCode, "XXX_profile_compatibility_flag[][j]");   ptl->setProfileCompatibilityFlag(j, uiCode ? 1 : 0);
3872  }
3873  READ_FLAG(uiCode, "general_progressive_source_flag");
3874  ptl->setProgressiveSourceFlag(uiCode ? true : false);
3875
3876  READ_FLAG(uiCode, "general_interlaced_source_flag");
3877  ptl->setInterlacedSourceFlag(uiCode ? true : false);
3878
3879  READ_FLAG(uiCode, "general_non_packed_constraint_flag");
3880  ptl->setNonPackedConstraintFlag(uiCode ? true : false);
3881
3882  READ_FLAG(uiCode, "general_frame_only_constraint_flag");
3883  ptl->setFrameOnlyConstraintFlag(uiCode ? true : false);
3884
3885  READ_CODE(16, uiCode, "XXX_reserved_zero_44bits[0..15]");
3886  READ_CODE(16, uiCode, "XXX_reserved_zero_44bits[16..31]");
3887  READ_CODE(12, uiCode, "XXX_reserved_zero_44bits[32..43]");
3888}
3889
3890Void TDecCavlc::parseTerminatingBit( UInt& ruiBit )
3891{
3892  ruiBit = false;
3893  Int iBitsLeft = m_pcBitstream->getNumBitsLeft();
3894  if(iBitsLeft <= 8)
3895  {
3896    UInt uiPeekValue = m_pcBitstream->peekBits(iBitsLeft);
3897    if (uiPeekValue == (1<<(iBitsLeft-1)))
3898    {
3899      ruiBit = true;
3900    }
3901  }
3902}
3903
3904Void TDecCavlc::parseSkipFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3905{
3906  assert(0);
3907}
3908
3909Void TDecCavlc::parseCUTransquantBypassFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3910{
3911  assert(0);
3912}
3913
3914Void TDecCavlc::parseMVPIdx( Int& /*riMVPIdx*/ )
3915{
3916  assert(0);
3917}
3918
3919Void TDecCavlc::parseSplitFlag     ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3920{
3921  assert(0);
3922}
3923
3924Void TDecCavlc::parsePartSize( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3925{
3926  assert(0);
3927}
3928
3929Void TDecCavlc::parsePredMode( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3930{
3931  assert(0);
3932}
3933
3934/** Parse I_PCM information.
3935* \param pcCU pointer to CU
3936* \param uiAbsPartIdx CU index
3937* \param uiDepth CU depth
3938* \returns Void
3939*
3940* If I_PCM flag indicates that the CU is I_PCM, parse its PCM alignment bits and codes.
3941*/
3942Void TDecCavlc::parseIPCMInfo( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3943{
3944  assert(0);
3945}
3946
3947Void TDecCavlc::parseIntraDirLumaAng  ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3948{
3949  assert(0);
3950}
3951
3952Void TDecCavlc::parseIntraDirChroma( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
3953{
3954  assert(0);
3955}
3956
3957Void TDecCavlc::parseInterDir( TComDataCU* /*pcCU*/, UInt& /*ruiInterDir*/, UInt /*uiAbsPartIdx*/ )
3958{
3959  assert(0);
3960}
3961
3962Void TDecCavlc::parseRefFrmIdx( TComDataCU* /*pcCU*/, Int& /*riRefFrmIdx*/, RefPicList /*eRefList*/ )
3963{
3964  assert(0);
3965}
3966
3967Void TDecCavlc::parseMvd( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiPartIdx*/, UInt /*uiDepth*/, RefPicList /*eRefList*/ )
3968{
3969  assert(0);
3970}
3971
3972Void TDecCavlc::parseDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
3973{
3974  Int qp;
3975  Int  iDQp;
3976
3977  xReadSvlc( iDQp );
3978
3979#if REPN_FORMAT_IN_VPS
3980  Int qpBdOffsetY = pcCU->getSlice()->getQpBDOffsetY();
3981#else
3982  Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffsetY();
3983#endif
3984  qp = (((Int) pcCU->getRefQP( uiAbsPartIdx ) + iDQp + 52 + 2*qpBdOffsetY )%(52+ qpBdOffsetY)) -  qpBdOffsetY;
3985
3986  UInt uiAbsQpCUPartIdx = (uiAbsPartIdx>>((g_uiMaxCUDepth - pcCU->getSlice()->getPPS()->getMaxCuDQPDepth())<<1))<<((g_uiMaxCUDepth - pcCU->getSlice()->getPPS()->getMaxCuDQPDepth())<<1) ;
3987  UInt uiQpCUDepth =   min(uiDepth,pcCU->getSlice()->getPPS()->getMaxCuDQPDepth()) ;
3988
3989  pcCU->setQPSubParts( qp, uiAbsQpCUPartIdx, uiQpCUDepth );
3990}
3991
3992Void TDecCavlc::parseCoeffNxN( TComDataCU* /*pcCU*/, TCoeff* /*pcCoef*/, UInt /*uiAbsPartIdx*/, UInt /*uiWidth*/, UInt /*uiHeight*/, UInt /*uiDepth*/, TextType /*eTType*/ )
3993{
3994  assert(0);
3995}
3996
3997Void TDecCavlc::parseTransformSubdivFlag( UInt& /*ruiSubdivFlag*/, UInt /*uiLog2TransformBlockSize*/ )
3998{
3999  assert(0);
4000}
4001
4002Void TDecCavlc::parseQtCbf( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, TextType /*eType*/, UInt /*uiTrDepth*/, UInt /*uiDepth*/ )
4003{
4004  assert(0);
4005}
4006
4007Void TDecCavlc::parseQtRootCbf( UInt /*uiAbsPartIdx*/, UInt& /*uiQtRootCbf*/ )
4008{
4009  assert(0);
4010}
4011
4012Void TDecCavlc::parseTransformSkipFlags (TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*width*/, UInt /*height*/, UInt /*uiDepth*/, TextType /*eTType*/)
4013{
4014  assert(0);
4015}
4016
4017Void TDecCavlc::parseMergeFlag ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/, UInt /*uiPUIdx*/ )
4018{
4019  assert(0);
4020}
4021
4022Void TDecCavlc::parseMergeIndex ( TComDataCU* /*pcCU*/, UInt& /*ruiMergeIndex*/ )
4023{
4024  assert(0);
4025}
4026
4027// ====================================================================================================================
4028// Protected member functions
4029// ====================================================================================================================
4030
4031/** parse explicit wp tables
4032* \param TComSlice* pcSlice
4033* \returns Void
4034*/
4035Void TDecCavlc::xParsePredWeightTable( TComSlice* pcSlice )
4036{
4037  wpScalingParam  *wp;
4038  Bool            bChroma     = true; // color always present in HEVC ?
4039  SliceType       eSliceType  = pcSlice->getSliceType();
4040  Int             iNbRef       = (eSliceType == B_SLICE ) ? (2) : (1);
4041#if SVC_EXTENSION
4042  UInt            uiLog2WeightDenomLuma = 0, uiLog2WeightDenomChroma = 0;
4043#else
4044  UInt            uiLog2WeightDenomLuma, uiLog2WeightDenomChroma;
4045#endif
4046  UInt            uiTotalSignalledWeightFlags = 0;
4047
4048  Int iDeltaDenom;
4049#if AUXILIARY_PICTURES
4050  if (pcSlice->getChromaFormatIdc() == CHROMA_400)
4051  {
4052    bChroma = false;
4053  }
4054#endif
4055  // decode delta_luma_log2_weight_denom :
4056  READ_UVLC( uiLog2WeightDenomLuma, "luma_log2_weight_denom" );     // ue(v): luma_log2_weight_denom
4057  assert( uiLog2WeightDenomLuma <= 7 );
4058  if( bChroma )
4059  {
4060    READ_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );     // se(v): delta_chroma_log2_weight_denom
4061    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)>=0);
4062    assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)<=7);
4063    uiLog2WeightDenomChroma = (UInt)(iDeltaDenom + uiLog2WeightDenomLuma);
4064  }
4065
4066  for ( Int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ )
4067  {
4068    RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
4069    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
4070    {
4071      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
4072
4073      wp[0].uiLog2WeightDenom = uiLog2WeightDenomLuma;
4074#if AUXILIARY_PICTURES
4075      if (!bChroma)
4076      {
4077        wp[1].uiLog2WeightDenom = 0;
4078        wp[2].uiLog2WeightDenom = 0;
4079      }
4080      else
4081      {
4082#endif
4083        wp[1].uiLog2WeightDenom = uiLog2WeightDenomChroma;
4084        wp[2].uiLog2WeightDenom = uiLog2WeightDenomChroma;
4085#if AUXILIARY_PICTURES
4086      }
4087#endif
4088
4089      UInt  uiCode;
4090      READ_FLAG( uiCode, "luma_weight_lX_flag" );           // u(1): luma_weight_l0_flag
4091      wp[0].bPresentFlag = ( uiCode == 1 );
4092      uiTotalSignalledWeightFlags += wp[0].bPresentFlag;
4093    }
4094    if ( bChroma )
4095    {
4096      UInt  uiCode;
4097      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
4098      {
4099        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
4100        READ_FLAG( uiCode, "chroma_weight_lX_flag" );      // u(1): chroma_weight_l0_flag
4101        wp[1].bPresentFlag = ( uiCode == 1 );
4102        wp[2].bPresentFlag = ( uiCode == 1 );
4103        uiTotalSignalledWeightFlags += 2*wp[1].bPresentFlag;
4104      }
4105    }
4106    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
4107    {
4108      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
4109      if ( wp[0].bPresentFlag )
4110      {
4111        Int iDeltaWeight;
4112        READ_SVLC( iDeltaWeight, "delta_luma_weight_lX" );  // se(v): delta_luma_weight_l0[i]
4113        assert( iDeltaWeight >= -128 );
4114        assert( iDeltaWeight <=  127 );
4115        wp[0].iWeight = (iDeltaWeight + (1<<wp[0].uiLog2WeightDenom));
4116        READ_SVLC( wp[0].iOffset, "luma_offset_lX" );       // se(v): luma_offset_l0[i]
4117        assert( wp[0].iOffset >= -128 );
4118        assert( wp[0].iOffset <=  127 );
4119      }
4120      else
4121      {
4122        wp[0].iWeight = (1 << wp[0].uiLog2WeightDenom);
4123        wp[0].iOffset = 0;
4124      }
4125      if ( bChroma )
4126      {
4127        if ( wp[1].bPresentFlag )
4128        {
4129          for ( Int j=1 ; j<3 ; j++ )
4130          {
4131            Int iDeltaWeight;
4132            READ_SVLC( iDeltaWeight, "delta_chroma_weight_lX" );  // se(v): chroma_weight_l0[i][j]
4133            assert( iDeltaWeight >= -128 );
4134            assert( iDeltaWeight <=  127 );
4135            wp[j].iWeight = (iDeltaWeight + (1<<wp[1].uiLog2WeightDenom));
4136
4137            Int iDeltaChroma;
4138            READ_SVLC( iDeltaChroma, "delta_chroma_offset_lX" );  // se(v): delta_chroma_offset_l0[i][j]
4139            assert( iDeltaChroma >= -512 );
4140            assert( iDeltaChroma <=  511 );
4141            Int pred = ( 128 - ( ( 128*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
4142            wp[j].iOffset = Clip3(-128, 127, (iDeltaChroma + pred) );
4143          }
4144        }
4145        else
4146        {
4147          for ( Int j=1 ; j<3 ; j++ )
4148          {
4149            wp[j].iWeight = (1 << wp[j].uiLog2WeightDenom);
4150            wp[j].iOffset = 0;
4151          }
4152        }
4153      }
4154    }
4155
4156    for ( Int iRefIdx=pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx<MAX_NUM_REF ; iRefIdx++ )
4157    {
4158      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
4159
4160      wp[0].bPresentFlag = false;
4161      wp[1].bPresentFlag = false;
4162      wp[2].bPresentFlag = false;
4163    }
4164  }
4165  assert(uiTotalSignalledWeightFlags<=24);
4166}
4167
4168/** decode quantization matrix
4169* \param scalingList quantization matrix information
4170*/
4171Void TDecCavlc::parseScalingList(TComScalingList* scalingList)
4172{
4173  UInt  code, sizeId, listId;
4174  Bool scalingListPredModeFlag;
4175  //for each size
4176  for(sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
4177  {
4178    for(listId = 0; listId <  g_scalingListNum[sizeId]; listId++)
4179    {
4180      READ_FLAG( code, "scaling_list_pred_mode_flag");
4181      scalingListPredModeFlag = (code) ? true : false;
4182      if(!scalingListPredModeFlag) //Copy Mode
4183      {
4184        READ_UVLC( code, "scaling_list_pred_matrix_id_delta");
4185        scalingList->setRefMatrixId (sizeId,listId,(UInt)((Int)(listId)-(code)));
4186        if( sizeId > SCALING_LIST_8x8 )
4187        {
4188          scalingList->setScalingListDC(sizeId,listId,((listId == scalingList->getRefMatrixId (sizeId,listId))? 16 :scalingList->getScalingListDC(sizeId, scalingList->getRefMatrixId (sizeId,listId))));
4189        }
4190        scalingList->processRefMatrix( sizeId, listId, scalingList->getRefMatrixId (sizeId,listId));
4191
4192      }
4193      else //DPCM Mode
4194      {
4195        xDecodeScalingList(scalingList, sizeId, listId);
4196      }
4197    }
4198  }
4199
4200  return;
4201}
4202/** decode DPCM
4203* \param scalingList  quantization matrix information
4204* \param sizeId size index
4205* \param listId list index
4206*/
4207Void TDecCavlc::xDecodeScalingList(TComScalingList *scalingList, UInt sizeId, UInt listId)
4208{
4209  Int i,coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
4210  Int data;
4211  Int scalingListDcCoefMinus8 = 0;
4212  Int nextCoef = SCALING_LIST_START_VALUE;
4213  UInt* scan  = (sizeId == 0) ? g_auiSigLastScan [ SCAN_DIAG ] [ 1 ] :  g_sigLastScanCG32x32;
4214  Int *dst = scalingList->getScalingListAddress(sizeId, listId);
4215
4216  if( sizeId > SCALING_LIST_8x8 )
4217  {
4218    READ_SVLC( scalingListDcCoefMinus8, "scaling_list_dc_coef_minus8");
4219    scalingList->setScalingListDC(sizeId,listId,scalingListDcCoefMinus8 + 8);
4220    nextCoef = scalingList->getScalingListDC(sizeId,listId);
4221  }
4222
4223  for(i = 0; i < coefNum; i++)
4224  {
4225    READ_SVLC( data, "scaling_list_delta_coef");
4226    nextCoef = (nextCoef + data + 256 ) % 256;
4227    dst[scan[i]] = nextCoef;
4228  }
4229}
4230
4231Bool TDecCavlc::xMoreRbspData()
4232{
4233  Int bitsLeft = m_pcBitstream->getNumBitsLeft();
4234
4235  // if there are more than 8 bits, it cannot be rbsp_trailing_bits
4236  if (bitsLeft > 8)
4237  {
4238    return true;
4239  }
4240
4241  UChar lastByte = m_pcBitstream->peekBits(bitsLeft);
4242  Int cnt = bitsLeft;
4243
4244  // remove trailing bits equal to zero
4245  while ((cnt>0) && ((lastByte & 1) == 0))
4246  {
4247    lastByte >>= 1;
4248    cnt--;
4249  }
4250  // remove bit equal to one
4251  cnt--;
4252
4253  // we should not have a negative number of bits
4254  assert (cnt>=0);
4255
4256  // we have more data, if cnt is not zero
4257  return (cnt>0);
4258}
4259
4260#if Q0048_CGS_3D_ASYMLUT
4261Void TDecCavlc::xParse3DAsymLUT( TCom3DAsymLUT * pc3DAsymLUT )
4262{
4263#if R0150_CGS_SIGNAL_CONSTRAINTS
4264  UInt uiNumRefLayersM1;
4265  READ_UVLC( uiNumRefLayersM1 , "num_cm_ref_layers_minus1" );
4266  assert( uiNumRefLayersM1 <= 61 );
4267  for( UInt i = 0 ; i <= uiNumRefLayersM1 ; i++ )
4268  {
4269    UInt uiRefLayerId;
4270    READ_CODE( 6 , uiRefLayerId , "cm_ref_layer_id" );
4271    pc3DAsymLUT->addRefLayerId( uiRefLayerId );
4272  }
4273#endif
4274  UInt uiCurOctantDepth , uiCurPartNumLog2 , uiInputBitDepthM8 , uiOutputBitDepthM8 , uiResQaunBit;
4275#if R0300_CGS_RES_COEFF_CODING
4276  UInt uiDeltaBits; 
4277#endif
4278  READ_CODE( 2 , uiCurOctantDepth , "cm_octant_depth" ); 
4279  READ_CODE( 2 , uiCurPartNumLog2 , "cm_y_part_num_log2" );     
4280#if R0150_CGS_SIGNAL_CONSTRAINTS
4281  UInt uiChromaInputBitDepthM8 , uiChromaOutputBitDepthM8;
4282  READ_UVLC( uiInputBitDepthM8 , "cm_input_luma_bit_depth_minus8" );
4283  READ_UVLC( uiChromaInputBitDepthM8 , "cm_input_chroma_bit_depth_minus8" );
4284  READ_UVLC( uiOutputBitDepthM8 , "cm_output_luma_bit_depth_minus8" );
4285  READ_UVLC( uiChromaOutputBitDepthM8 , "cm_output_chroma_bit_depth_minus8" );
4286#else
4287  READ_CODE( 3 , uiInputBitDepthM8 , "cm_input_bit_depth_minus8" );
4288  Int iInputBitDepthCDelta;
4289  READ_SVLC(iInputBitDepthCDelta, "cm_input_bit_depth_chroma delta");
4290  READ_CODE( 3 , uiOutputBitDepthM8 , "cm_output_bit_depth_minus8" ); 
4291  Int iOutputBitDepthCDelta;
4292  READ_SVLC(iOutputBitDepthCDelta, "cm_output_bit_depth_chroma_delta");
4293#endif
4294  READ_CODE( 2 , uiResQaunBit , "cm_res_quant_bit" );
4295#if R0300_CGS_RES_COEFF_CODING
4296  READ_CODE( 2 , uiDeltaBits , "cm_flc_bits" );
4297  pc3DAsymLUT->setDeltaBits(uiDeltaBits + 1);
4298#endif
4299
4300#if R0151_CGS_3D_ASYMLUT_IMPROVE
4301#if R0150_CGS_SIGNAL_CONSTRAINTS
4302  Int nAdaptCThresholdU = 1 << ( uiChromaInputBitDepthM8 + 8 - 1 );
4303  Int nAdaptCThresholdV = 1 << ( uiChromaInputBitDepthM8 + 8 - 1 );
4304#else
4305  Int nAdaptCThresholdU = 1 << ( uiInputBitDepthM8 + 8 + iInputBitDepthCDelta - 1 );
4306  Int nAdaptCThresholdV = 1 << ( uiInputBitDepthM8 + 8 + iInputBitDepthCDelta - 1 );
4307#endif
4308  if( uiCurOctantDepth == 1 )
4309  {
4310    Int delta = 0;
4311    READ_SVLC( delta , "cm_adapt_threshold_u_delta" );
4312    nAdaptCThresholdU += delta;
4313    READ_SVLC( delta , "cm_adapt_threshold_v_delta" );
4314    nAdaptCThresholdV += delta;
4315  }
4316#endif
4317  pc3DAsymLUT->destroy();
4318  pc3DAsymLUT->create( uiCurOctantDepth , uiInputBitDepthM8 + 8 , 
4319#if R0150_CGS_SIGNAL_CONSTRAINTS
4320    uiChromaInputBitDepthM8 + 8 ,
4321#else
4322    uiInputBitDepthM8 + 8 + iInputBitDepthCDelta, 
4323#endif
4324    uiOutputBitDepthM8 + 8 , 
4325#if R0150_CGS_SIGNAL_CONSTRAINTS
4326    uiChromaOutputBitDepthM8 + 8 ,
4327#else
4328    uiOutputBitDepthM8 + 8 + iOutputBitDepthCDelta ,
4329#endif
4330    uiCurPartNumLog2
4331#if R0151_CGS_3D_ASYMLUT_IMPROVE
4332    , nAdaptCThresholdU , nAdaptCThresholdV
4333#endif   
4334    );
4335  pc3DAsymLUT->setResQuantBit( uiResQaunBit );
4336
4337#if R0164_CGS_LUT_BUGFIX_CHECK
4338  pc3DAsymLUT->xInitCuboids();
4339#endif
4340  xParse3DAsymLUTOctant( pc3DAsymLUT , 0 , 0 , 0 , 0 , 1 << pc3DAsymLUT->getCurOctantDepth() );
4341#if R0164_CGS_LUT_BUGFIX
4342#if R0164_CGS_LUT_BUGFIX_CHECK
4343  printf("============= Before 'xCuboidsFilledCheck()': ================\n");
4344  pc3DAsymLUT->display();
4345  pc3DAsymLUT->xCuboidsFilledCheck( false );
4346  printf("============= After 'xCuboidsFilledCheck()': =================\n");
4347  pc3DAsymLUT->display();
4348#endif
4349#endif
4350}
4351
4352Void TDecCavlc::xParse3DAsymLUTOctant( TCom3DAsymLUT * pc3DAsymLUT , Int nDepth , Int yIdx , Int uIdx , Int vIdx , Int nLength )
4353{
4354  UInt uiOctantSplit = nDepth < pc3DAsymLUT->getCurOctantDepth();
4355  if( nDepth < pc3DAsymLUT->getCurOctantDepth() )
4356    READ_FLAG( uiOctantSplit , "split_octant_flag" );
4357  Int nYPartNum = 1 << pc3DAsymLUT->getCurYPartNumLog2();
4358  if( uiOctantSplit )
4359  {
4360    Int nHalfLength = nLength >> 1;
4361    for( Int l = 0 ; l < 2 ; l++ )
4362    {
4363      for( Int m = 0 ; m < 2 ; m++ )
4364      {
4365        for( Int n = 0 ; n < 2 ; n++ )
4366        {
4367          xParse3DAsymLUTOctant( pc3DAsymLUT , nDepth + 1 , yIdx + l * nHalfLength * nYPartNum , uIdx + m * nHalfLength , vIdx + n * nHalfLength , nHalfLength );
4368        }
4369      }
4370    }
4371  }
4372  else
4373  {
4374#if R0300_CGS_RES_COEFF_CODING
4375    Int nFLCbits = pc3DAsymLUT->getMappingShift()-pc3DAsymLUT->getResQuantBit()-pc3DAsymLUT->getDeltaBits() ; 
4376    nFLCbits = nFLCbits >= 0 ? nFLCbits:0;
4377#endif
4378    for( Int l = 0 ; l < nYPartNum ; l++ )
4379    {
4380#if R0164_CGS_LUT_BUGFIX
4381      Int shift = pc3DAsymLUT->getCurOctantDepth() - nDepth ;
4382#endif
4383      for( Int nVertexIdx = 0 ; nVertexIdx < 4 ; nVertexIdx++ )
4384      {
4385        UInt uiCodeVertex = 0;
4386        Int deltaY = 0 , deltaU = 0 , deltaV = 0;
4387        READ_FLAG( uiCodeVertex , "coded_vertex_flag" );
4388        if( uiCodeVertex )
4389        {
4390#if R0151_CGS_3D_ASYMLUT_IMPROVE
4391#if R0300_CGS_RES_COEFF_CODING
4392          xReadParam( deltaY, nFLCbits );
4393          xReadParam( deltaU, nFLCbits );
4394          xReadParam( deltaV, nFLCbits );
4395#else
4396          xReadParam( deltaY );
4397          xReadParam( deltaU );
4398          xReadParam( deltaV );
4399#endif
4400#else
4401          READ_SVLC( deltaY , "resY" );
4402          READ_SVLC( deltaU , "resU" );
4403          READ_SVLC( deltaV , "resV" );
4404#endif
4405        }
4406#if R0164_CGS_LUT_BUGFIX
4407        pc3DAsymLUT->setCuboidVertexResTree( yIdx + (l<<shift) , uIdx , vIdx , nVertexIdx , deltaY , deltaU , deltaV );
4408        for (Int m = 1; m < (1<<shift); m++) {
4409          pc3DAsymLUT->setCuboidVertexResTree( yIdx + (l<<shift) + m , uIdx , vIdx , nVertexIdx , 0 , 0 , 0 );
4410#if R0164_CGS_LUT_BUGFIX_CHECK
4411          pc3DAsymLUT->xSetFilled( yIdx + (l<<shift) + m , uIdx , vIdx );
4412#endif
4413        }
4414#else
4415        pc3DAsymLUT->setCuboidVertexResTree( yIdx + l , uIdx , vIdx , nVertexIdx , deltaY , deltaU , deltaV );
4416#endif
4417      }
4418#if R0164_CGS_LUT_BUGFIX_CHECK
4419      pc3DAsymLUT->xSetExplicit( yIdx + (l<<shift) , uIdx , vIdx );
4420#endif
4421    }
4422#if R0164_CGS_LUT_BUGFIX
4423    for ( Int u=0 ; u<nLength ; u++ ) {
4424      for ( Int v=0 ; v<nLength ; v++ ) {
4425        if ( u!=0 || v!=0 ) {
4426          for ( Int y=0 ; y<nLength*nYPartNum ; y++ ) {
4427            for( Int nVertexIdx = 0 ; nVertexIdx < 4 ; nVertexIdx++ )
4428            {
4429              pc3DAsymLUT->setCuboidVertexResTree( yIdx + y , uIdx + u , vIdx + v , nVertexIdx , 0 , 0 , 0 );
4430#if R0164_CGS_LUT_BUGFIX_CHECK
4431              pc3DAsymLUT->xSetFilled( yIdx + y , uIdx + u , vIdx + v );
4432#endif
4433            }
4434          }
4435        }
4436      }
4437    }
4438#endif
4439  }
4440}
4441
4442#if R0151_CGS_3D_ASYMLUT_IMPROVE
4443#if R0300_CGS_RES_COEFF_CODING
4444Void TDecCavlc::xReadParam( Int& param, Int rParam )
4445#else
4446Void TDecCavlc::xReadParam( Int& param )
4447#endif
4448{
4449#if !R0300_CGS_RES_COEFF_CODING
4450  const UInt rParam = 7;
4451#endif
4452  UInt prefix;
4453  UInt codeWord ;
4454  UInt rSymbol;
4455  UInt sign;
4456
4457  READ_UVLC( prefix, "quotient")  ;
4458  READ_CODE (rParam, codeWord, "remainder");
4459  rSymbol = (prefix<<rParam) + codeWord;
4460
4461  if(rSymbol)
4462  {
4463    READ_FLAG(sign, "sign");
4464    param = sign ? -(Int)(rSymbol) : (Int)(rSymbol);
4465  }
4466  else param = 0;
4467}
4468#endif
4469#endif
4470//! \}
4471
Note: See TracBrowser for help on using the repository browser.